home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / program / code / andylib / c / field < prev    next >
Encoding:
Text File  |  1994-04-09  |  5.0 KB  |  234 lines

  1. /* Field    - library routines for reading and writing the values of text
  2.  *            icons.
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdarg.h>
  7.  
  8. #include "wimp.h"
  9. #include "wimpt.h"
  10. #include "bbc.h"
  11. #include "field.h"
  12.  
  13. /* Copy at most size characters, then terminate with a NULL byte.  Copying
  14.  * stops when any control code is found in the source string.  This is used
  15.  * for copying both from and to icons.  Returns the actual length of the
  16.  * copied string.
  17.  */
  18.  
  19. static int cpystr(char *to, const char *from, size_t size)
  20. {  int l = 0;
  21.  
  22.    while (*from >= ' ' && size--)
  23.    {  *to++ = *from++;
  24.       ++l;
  25.    }
  26.  
  27.    *to = '\0';
  28.  
  29.    return l;
  30. }
  31.  
  32. /* Force an icon to redraw
  33.  */
  34. static os_error *painticon(wimp_w w, wimp_i i)
  35. {  wimp_icon icn;
  36.    os_error *err;
  37.  
  38.    if (err = wimp_get_icon_info(w, i, &icn), err)
  39.       return err;
  40.  
  41.    if (icn.flags & wimp_IBORDER)
  42.    {  wimp_redrawstr r;
  43.       int pixx = 1 << bbc_vduvar(bbc_XEigFactor);
  44.       int pixy = 1 << bbc_vduvar(bbc_YEigFactor);
  45.       BOOL more;
  46.  
  47.       r.w = w;
  48.       r.box = icn.box;
  49.       r.box.x0 += pixx;
  50.       r.box.y0 += pixy;
  51.       r.box.x1 -= pixx;
  52.       r.box.y1 -= pixy;
  53.  
  54.       if (err = wimp_update_wind(&r, &more), err)
  55.          return err;
  56.  
  57.       while (more)
  58.          wimp_get_rectangle(&r, &more);
  59.  
  60.       return NULL;
  61.    }
  62.    else
  63.       return wimp_set_icon_state(w, i, 0, 0);
  64. }
  65.  
  66. void field_settext(wimp_w w, wimp_i i, const char *text)
  67. {  wimp_icon icn;
  68.    wimp_caretstr cur;
  69.    int l;
  70.  
  71.    wimpt_noerr(wimp_get_icon_info(w, i, &icn));
  72.  
  73.    if (icn.flags & wimp_ITEXT)
  74.    {  if (icn.flags & wimp_INDIRECT)
  75.          l = cpystr(icn.data.indirecttext.buffer,
  76.                     text, icn.data.indirecttext.bufflen-1);
  77.       else
  78.          l = cpystr(icn.data.text, text, 11);
  79.  
  80.       /* Now patch up caret position if it's in this box */
  81.  
  82.       wimpt_noerr(wimp_get_caret_pos(&cur));
  83.       if (cur.w == w && cur.i == i)
  84.       {  if (cur.index > l) cur.index = l;
  85.          cur.height = -1;
  86.          wimpt_noerr(wimp_set_caret_pos(&cur));
  87.       }
  88.  
  89.       /* Now fool wimp into redrawing it */
  90.  
  91.       wimpt_noerr(painticon(w, i));
  92.    }
  93.  
  94.    /* Else ignore it if it's a non text icon */
  95. }
  96.  
  97. char *field_gettext(wimp_w w, wimp_i i, char *buffer, size_t bufsize)
  98. {  wimp_icon icn;
  99.  
  100.    wimpt_noerr(wimp_get_icon_info(w, i, &icn));
  101.  
  102.    if (icn.flags & wimp_ITEXT)
  103.    {  if (icn.flags & wimp_INDIRECT)
  104.          cpystr(buffer, icn.data.indirecttext.buffer, bufsize-1);
  105.       else
  106.          cpystr(buffer, icn.data.text, bufsize-1);
  107.    }
  108.  
  109.    return buffer;
  110. }
  111.  
  112. int field_printf(wimp_w w, wimp_i i, const char *fmt, ...)
  113. {  va_list ap;
  114.    char buffer[1024];
  115.    int l;
  116.  
  117.    va_start(ap, fmt);
  118.    l = vsprintf(buffer, fmt, ap);
  119.    va_end(ap);
  120.  
  121.    field_settext(w, i, buffer);
  122.    return l;
  123. }
  124.  
  125. void field_setnumeric(wimp_w w, wimp_i i, int value)
  126. {  field_printf(w, i, "%d", value);
  127. }
  128.  
  129. int field_getnumeric(wimp_w w, wimp_i i)
  130. {  char cbuf[256], *cb;
  131.    int neg = FALSE, n = 0;
  132.  
  133.    cb = field_gettext(w, i, cbuf, 256);
  134.  
  135.    while (*cb == ' ')
  136.       cb++;
  137.  
  138.    if (*cb == '-')
  139.    {  cb++;
  140.       neg = TRUE;
  141.    }
  142.  
  143.    while (*cb >= '0' && *cb <= '9')
  144.       n = n * 10 + *cb++ - '0';
  145.  
  146.    return neg ? -n : n;
  147. }
  148.  
  149. int field_addnumeric(wimp_w w, wimp_i i, int lo, int hi, int delta)
  150. {  int v, ov;
  151.  
  152.    v = ov = field_getnumeric(w, i);
  153.  
  154.    v += delta;
  155.  
  156.    if (v < lo)
  157.       v = lo;
  158.    else if (v > hi)
  159.       v = hi;
  160.  
  161.    if (ov != v)
  162.       field_setnumeric(w, i, v);
  163.  
  164.    return v;
  165. }
  166.  
  167. int field_getbool(wimp_w w, wimp_i i)
  168. {  wimp_icon icn;
  169.  
  170.    wimpt_noerr(wimp_get_icon_info(w, i, &icn));
  171.    return !!(icn.flags & wimp_ISELECTED);
  172. }
  173.  
  174. void field_setbool(wimp_w w, wimp_i i, int flag)
  175. {  if (flag != field_getbool(w, i))
  176.       wimpt_noerr(wimp_set_icon_state(w, i,
  177.                   flag ? wimp_ISELECTED : 0, wimp_ISELECTED));
  178. }
  179.  
  180. void field_fade(wimp_w w, wimp_i i, int f)
  181. {  wimp_icon icn;
  182.  
  183.    wimpt_noerr(wimp_get_icon_info(w, i, &icn));
  184.    if (!!(icn.flags & wimp_INOSELECT) != f)
  185.       wimpt_noerr(wimp_set_icon_state(w, i,
  186.                   f ? wimp_INOSELECT : 0, wimp_INOSELECT));
  187. }
  188.  
  189. int field_flipbool(wimp_w w, wimp_i i)
  190. {  int l = field_getbool(w, i);
  191.  
  192.    field_setbool(w, i, !l);
  193.    return l;
  194. }
  195.  
  196. /* Used for decoding radio menus.  The arguments after w are a list of
  197.  * icons.  The number returned is the index (from 0) in this list of the
  198.  * first icon which is selected.  The list is ended with a parameter of
  199.  * either -1 or -2.  If the parameter is -2 field_oneof will ensure that at
  200.  * least one icon in the list is selected.  If no icons are selected the
  201.  * first icon in the list will be selected and 0 returned.  If the list is
  202.  * terminated with a -1 and no icons are selected -1 will be returned.
  203.  */
  204.  
  205. int field_oneof(wimp_w w, ...)
  206. {  va_list ap;
  207.    wimp_i first = -1, i;
  208.    int idx = 0, sel = -1;
  209.  
  210.    va_start(ap, w);
  211.    i = va_arg(ap, wimp_i);
  212.  
  213.    while (i >= 0)
  214.    {  if (first == -1)
  215.          first = i;
  216.  
  217.       if (field_getbool(w, i))
  218.       {  sel = idx;
  219.          break;
  220.       }
  221.  
  222.       idx++;
  223.       i = va_arg(ap, wimp_i);
  224.    }
  225.    va_end(ap);
  226.  
  227.    if (i == field_SETONE && first != -1)    /* Got to end of list */
  228.    {  field_setbool(w, first, TRUE);
  229.       sel = 0;
  230.    }
  231.  
  232.    return sel;
  233. }
  234.