home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 328_01 / wbutton.c < prev    next >
C/C++ Source or Header  |  1991-03-24  |  3KB  |  151 lines

  1. /* wbutton.c
  2.  *--------------------- 'BUTTON'  functions -----------------------------*
  3.  *
  4.  *    buttons are selectable choices onscreen
  5.  *        associated with either mouse or keyboard action.
  6.  *
  7.  *
  8.  *    wbutton_add ()         - add a button to the list.
  9.  *                     install button handler if not yet done
  10.  *                     add new button to linked list
  11.  *                     draws the button onscreen.
  12.  *
  13.  *                  OPTIONS:
  14.  *                    WBTN_BOX = draw a box around button
  15.  *
  16.  *     wbutton_frame () and
  17.  *    wbutton_draw  ()        - draw the button onscreen, draw box
  18.  *                  internal utilities.
  19.  *                  also called by
  20.  *                    wbutton_mark, _inactivate(), etc...
  21.  *
  22.  *    event_handler ()     - keyboard trap routine.
  23.  *
  24.  *    METHOD:
  25.  *        see definition of WBUTTON above.
  26.  *
  27.  *
  28.  *    these routines keep linked lists list of currently active buttons
  29.  *
  30.  *    one list is maintianed for each window.
  31.  *
  32.  *    when a button is added, wbutton_add()
  33.  *        allocates memory for WBUTTON struct and adds to linked list.
  34.  *        draws button onscreen.
  35.  *
  36.  */
  37. #include "wsys.h"
  38.  
  39.  
  40. void wbutton_add (char *utext, int ux, int uy, int ulen,
  41.             int uval,
  42.             unsigned char ustyle )
  43.     {
  44.     WBUTTON     *Bptr;
  45.  
  46.  
  47.     /* allocate storage and place new button in linked list
  48.      */
  49.     Bptr = wmalloc ( sizeof (WBUTTON), "wbutton_add" );
  50.     Bptr->Bchain = w0->winbutton;
  51.     w0->winbutton = Bptr;
  52.  
  53.  
  54.     Bptr-> Btext = utext;
  55.     _NORMALIZE ( Bptr-> Btext );
  56.  
  57.     Bptr-> Bval  = uval;
  58.     Bptr-> Bstyle= ustyle;
  59.  
  60.     Bptr-> Bx    = ux;
  61.     Bptr-> By    = uy;
  62.  
  63.  
  64.     Bptr->Blen   = ulen;
  65.  
  66.     /* compute location of end of button
  67.      * note that ulen includes terminal NULL.
  68.      * but stored value in WBUTTON strutcure is just the displayed bytes.
  69.      *
  70.      * so subtract 1 for terminal NULL .
  71.      */
  72.     Bptr-> Bxend = ux + ulen -1;
  73.     Bptr-> Byend = uy;
  74.  
  75.  
  76.     wbutton_draw ( Bptr, wbuttonattr );
  77.  
  78.     if ( ustyle & WBTN_BOX )
  79.         {
  80.         /* draw a box around button
  81.          */
  82.         --(Bptr->Bx);
  83.         --(Bptr->By);
  84.         ++(Bptr->Bxend);
  85.         ++(Bptr->Byend);
  86.  
  87.         wbutton_frame ( Bptr, SINGLE_BORDER, wreverseattr(wbuttonattr) );
  88.  
  89.         }
  90.  
  91.  
  92.  
  93.     Bptr-> Bstyle |= WBTN_ACTIVE + WBTN_BUTTON;
  94.  
  95.  
  96.     return;    /* wbutton_add */
  97.     }
  98.  
  99.  
  100.  
  101.  
  102. void wbutton_draw ( WBUTTON *Bptr, unsigned char new_attr )
  103.     {
  104.  
  105.  
  106.     unsigned char user_attr, user_x, user_y;
  107.  
  108.     int       len;
  109.  
  110.     user_attr = wgetattr();        /* save attribute & position */
  111.     user_x    = wherex();
  112.     user_y    = wherey();
  113.  
  114.     wsetattr ( new_attr );
  115.  
  116.     /* number of onscreen bytes,
  117.      * which doesn;t count terminal NULL
  118.      *
  119.      *
  120.      */
  121.     len = (Bptr-> Bxend) - (Bptr-> Bx);
  122.  
  123.     wgoto ( Bptr->Bx, Bptr->By );
  124.     
  125.     wputfl ( Bptr->Btext, len );    /* fixed len put */
  126.     
  127.     wsetattr ( user_attr );        /* restore ol attribute */
  128.     wgoto ( user_x, user_y );
  129.  
  130.  
  131.     return;    /* wbutton_draw */
  132.     }
  133.  
  134.  
  135.  
  136. void wbutton_frame
  137.      ( WBUTTON *Bptr, unsigned char type, unsigned char color )
  138.     {
  139.     int l, t, r, b;
  140.  
  141.     l = (w0-> winleft) + (Bptr->Bx);
  142.     t = (w0-> wintop ) + (Bptr->By);
  143.     r = (w0-> winleft) + (Bptr->Bxend)  -1;
  144.     b = (w0-> wintop) + (Bptr->Byend);
  145.  
  146.     wframe (l, t, r, b, type, color );
  147.     }
  148.  
  149.  
  150. /*---------------- end of WBUTTON.C ----------------------*/
  151.