home *** CD-ROM | disk | FTP | other *** search
- /* Field - library routines for reading and writing the values of text
- * icons.
- */
-
- #include <stdio.h>
- #include <stdarg.h>
-
- #include "wimp.h"
- #include "wimpt.h"
- #include "bbc.h"
- #include "field.h"
-
- /* Copy at most size characters, then terminate with a NULL byte. Copying
- * stops when any control code is found in the source string. This is used
- * for copying both from and to icons. Returns the actual length of the
- * copied string.
- */
-
- static int cpystr(char *to, const char *from, size_t size)
- { int l = 0;
-
- while (*from >= ' ' && size--)
- { *to++ = *from++;
- ++l;
- }
-
- *to = '\0';
-
- return l;
- }
-
- /* Force an icon to redraw
- */
- static os_error *painticon(wimp_w w, wimp_i i)
- { wimp_icon icn;
- os_error *err;
-
- if (err = wimp_get_icon_info(w, i, &icn), err)
- return err;
-
- if (icn.flags & wimp_IBORDER)
- { wimp_redrawstr r;
- int pixx = 1 << bbc_vduvar(bbc_XEigFactor);
- int pixy = 1 << bbc_vduvar(bbc_YEigFactor);
- BOOL more;
-
- r.w = w;
- r.box = icn.box;
- r.box.x0 += pixx;
- r.box.y0 += pixy;
- r.box.x1 -= pixx;
- r.box.y1 -= pixy;
-
- if (err = wimp_update_wind(&r, &more), err)
- return err;
-
- while (more)
- wimp_get_rectangle(&r, &more);
-
- return NULL;
- }
- else
- return wimp_set_icon_state(w, i, 0, 0);
- }
-
- void field_settext(wimp_w w, wimp_i i, const char *text)
- { wimp_icon icn;
- wimp_caretstr cur;
- int l;
-
- wimpt_noerr(wimp_get_icon_info(w, i, &icn));
-
- if (icn.flags & wimp_ITEXT)
- { if (icn.flags & wimp_INDIRECT)
- l = cpystr(icn.data.indirecttext.buffer,
- text, icn.data.indirecttext.bufflen-1);
- else
- l = cpystr(icn.data.text, text, 11);
-
- /* Now patch up caret position if it's in this box */
-
- wimpt_noerr(wimp_get_caret_pos(&cur));
- if (cur.w == w && cur.i == i)
- { if (cur.index > l) cur.index = l;
- cur.height = -1;
- wimpt_noerr(wimp_set_caret_pos(&cur));
- }
-
- /* Now fool wimp into redrawing it */
-
- wimpt_noerr(painticon(w, i));
- }
-
- /* Else ignore it if it's a non text icon */
- }
-
- char *field_gettext(wimp_w w, wimp_i i, char *buffer, size_t bufsize)
- { wimp_icon icn;
-
- wimpt_noerr(wimp_get_icon_info(w, i, &icn));
-
- if (icn.flags & wimp_ITEXT)
- { if (icn.flags & wimp_INDIRECT)
- cpystr(buffer, icn.data.indirecttext.buffer, bufsize-1);
- else
- cpystr(buffer, icn.data.text, bufsize-1);
- }
-
- return buffer;
- }
-
- int field_printf(wimp_w w, wimp_i i, const char *fmt, ...)
- { va_list ap;
- char buffer[1024];
- int l;
-
- va_start(ap, fmt);
- l = vsprintf(buffer, fmt, ap);
- va_end(ap);
-
- field_settext(w, i, buffer);
- return l;
- }
-
- void field_setnumeric(wimp_w w, wimp_i i, int value)
- { field_printf(w, i, "%d", value);
- }
-
- int field_getnumeric(wimp_w w, wimp_i i)
- { char cbuf[256], *cb;
- int neg = FALSE, n = 0;
-
- cb = field_gettext(w, i, cbuf, 256);
-
- while (*cb == ' ')
- cb++;
-
- if (*cb == '-')
- { cb++;
- neg = TRUE;
- }
-
- while (*cb >= '0' && *cb <= '9')
- n = n * 10 + *cb++ - '0';
-
- return neg ? -n : n;
- }
-
- int field_addnumeric(wimp_w w, wimp_i i, int lo, int hi, int delta)
- { int v, ov;
-
- v = ov = field_getnumeric(w, i);
-
- v += delta;
-
- if (v < lo)
- v = lo;
- else if (v > hi)
- v = hi;
-
- if (ov != v)
- field_setnumeric(w, i, v);
-
- return v;
- }
-
- int field_getbool(wimp_w w, wimp_i i)
- { wimp_icon icn;
-
- wimpt_noerr(wimp_get_icon_info(w, i, &icn));
- return !!(icn.flags & wimp_ISELECTED);
- }
-
- void field_setbool(wimp_w w, wimp_i i, int flag)
- { if (flag != field_getbool(w, i))
- wimpt_noerr(wimp_set_icon_state(w, i,
- flag ? wimp_ISELECTED : 0, wimp_ISELECTED));
- }
-
- void field_fade(wimp_w w, wimp_i i, int f)
- { wimp_icon icn;
-
- wimpt_noerr(wimp_get_icon_info(w, i, &icn));
- if (!!(icn.flags & wimp_INOSELECT) != f)
- wimpt_noerr(wimp_set_icon_state(w, i,
- f ? wimp_INOSELECT : 0, wimp_INOSELECT));
- }
-
- int field_flipbool(wimp_w w, wimp_i i)
- { int l = field_getbool(w, i);
-
- field_setbool(w, i, !l);
- return l;
- }
-
- /* Used for decoding radio menus. The arguments after w are a list of
- * icons. The number returned is the index (from 0) in this list of the
- * first icon which is selected. The list is ended with a parameter of
- * either -1 or -2. If the parameter is -2 field_oneof will ensure that at
- * least one icon in the list is selected. If no icons are selected the
- * first icon in the list will be selected and 0 returned. If the list is
- * terminated with a -1 and no icons are selected -1 will be returned.
- */
-
- int field_oneof(wimp_w w, ...)
- { va_list ap;
- wimp_i first = -1, i;
- int idx = 0, sel = -1;
-
- va_start(ap, w);
- i = va_arg(ap, wimp_i);
-
- while (i >= 0)
- { if (first == -1)
- first = i;
-
- if (field_getbool(w, i))
- { sel = idx;
- break;
- }
-
- idx++;
- i = va_arg(ap, wimp_i);
- }
- va_end(ap);
-
- if (i == field_SETONE && first != -1) /* Got to end of list */
- { field_setbool(w, first, TRUE);
- sel = 0;
- }
-
- return sel;
- }
-