home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff276.lzh / DateRequester / MRGadgets.c < prev    next >
C/C++ Source or Header  |  1989-11-09  |  8KB  |  259 lines

  1. /*  MRGadgets - Miscellaneous gadget support routines.
  2.  *  Author:     Mark R. Rinfret
  3.  *  Date:       09/02/89
  4.  *
  5.  *  This package contains a set of routines which assist in the use and
  6.  *  management of gadgets. Many of these were developed while writing
  7.  *  MRBackup.
  8.  *
  9.  *  Note that an attempt was made to maintain consistency in the order
  10.  *  that parameters are passed.  For most routines, the parameter list
  11.  *  will begin with
  12.  *      (gadget, window, requester, ...)
  13.  *  followed by any additional required parameters. 
  14.  */
  15.  
  16. #include <intuition/intuition.h>
  17. #include "Strings.h"
  18. #include "MRGadgets.h"
  19.  
  20. /*  FUNCTION
  21.         EraseGadgetBox - clear the area inside a gadget's border.
  22.  
  23.     SYNOPSIS
  24.         void EraseGadgetBox(gadget, window, requester)
  25.              struct Gadget      *gadget;
  26.              struct Window      *window;
  27.              struct Requester   *requester;
  28.  
  29.     DESCRIPTION
  30.         EraseGadgetBox must be called with a gadget for which a border
  31.         has been defined.  It erases the area contained by the border.
  32.         This routine supports other routines, such as SetOptionGadget.
  33.         If <requester> is non-null, the <requester>'s RastPort is used.
  34.         Otherwise, the <window>'s rastport is used.
  35.  
  36. */
  37.  
  38. void
  39. EraseGadgetBox(gadget, window, requester)
  40.     struct Gadget       *gadget; 
  41.     struct Window       *window;
  42.     struct Requester    *requester;
  43. {
  44.     struct RastPort     *rp;
  45.     ULONG               savePen;
  46.     ULONG               xmin,ymin,xmax,ymax ;
  47.  
  48.     rp = SelectRastPort(window, requester);
  49.     xmin = gadget->LeftEdge;
  50.     xmax = xmin + gadget->Width;
  51.     ymin = gadget->TopEdge;
  52.     ymax = ymin + gadget->Height;
  53.     savePen = rp->FgPen;
  54.     SetAPen(rp, 0L);
  55.     SetDrMd(rp, JAM1);
  56.     RectFill(rp, xmin, ymin, xmax, ymax);
  57.     SetAPen(rp, savePen);
  58. }
  59. /*  FUNCTION
  60.         GetGadget - get gadget pointer, given gadget ID.
  61.  
  62.     SYNOPSIS
  63.         struct Gadget *GetGadget(id, window)
  64.                                  int            id;
  65.                                  struct Window *window;
  66.  
  67.     DESCRIPTION
  68.         GetGadget attempts to locate a non-system gadget in <window>
  69.         that has the specified <id>. If found, a pointer to the gadget
  70.         is found. Otherwise, NULL is returned.
  71. */
  72. struct Gadget *
  73. GetGadget(id, window)
  74.     int             id;
  75.     struct Window   *window;
  76. {
  77.     struct Gadget   *testGadget;
  78.  
  79.     for (testGadget = window->FirstGadget; testGadget; 
  80.          testGadget = testGadget->NextGadget) {
  81.         /* All system gadget types have high bit set (I think...). */
  82.         if ( testGadget->GadgetType & 0x8000 ) 
  83.             continue;
  84.         if ( testGadget->GadgetID == id ) 
  85.             break;
  86.     }
  87.     return testGadget;
  88. }
  89.  
  90. /*  FUNCTION
  91.         ResetStringInfo - reset information in a StringInfo structure.
  92.  
  93.     SYNOPSIS
  94.         void ResetStringInfo(s)
  95.              struct StringInfo *s;
  96.  
  97.     DESCRIPTION
  98.         ResetStringInfo resets certain parameters in the StringInfo
  99.         structure pointed to by <s>, including:
  100.  
  101.             UndoBuffer
  102.             DispPos
  103.             UndoPos
  104.             NumChars
  105. */
  106. void
  107. ResetStringInfo(s)
  108.     struct StringInfo *s;
  109. {
  110.     *(s->UndoBuffer) = '\0';
  111.     s->BufferPos = 0;
  112.     s->DispPos = 0;
  113.     s->UndoPos = 0;
  114.     s->NumChars = strlen(s->Buffer);
  115. }
  116.  
  117. /* Indicate that a gadget is selected by turning on its highlight
  118.  * and SELECTED flags.
  119.  * Called with:
  120.  *      gadget:     pointer to gadget structure
  121.  *      window:     pointer to window containing gadget
  122.  *      state
  123.  */
  124. /*  FUNCTION
  125.         SelectGadget - set a gadget to the SELECTED or !SELECTED state.
  126.  
  127.     SYNOPSIS
  128.         void SelectGadget(gadget, window, requester, state)
  129.              struct Gadget      *gadget;
  130.              struct Window      *window;
  131.              struct Requester   *requester;
  132.              BOOL               state;
  133.  
  134.     DESCRIPTION
  135.         SelectGadget removes the <gadget> from the <window's> gadget list,
  136.         sets or clears the SELECTED bit according to <state>, then adds
  137.         the gadget back to the gadget list and refreshes list. If the
  138.         gadget belongs to a requester, then <requester> must be supplied.
  139.         Otherwise, it must be NULL.
  140. */
  141. void
  142. SelectGadget(gadget, window, requester, state)
  143.     struct Gadget       *gadget; 
  144.     struct Window       *window; 
  145.     struct Requester    *requester;
  146.     BOOL                state;
  147. {
  148.     long position;
  149.  
  150.     position = RemoveGadget(window, gadget);
  151.     if (state)
  152.         gadget->Flags |= SELECTED;
  153.     else
  154.         gadget->Flags &= ~SELECTED;
  155.     AddGadget(window, gadget, position);
  156.     RefreshGList(gadget, window, NULL, 1L);
  157. }
  158.  
  159.  
  160. /*  FUNCTION
  161.         SetOptionGadget - set string value for multi-option gadget.
  162.  
  163.     SYNOPSIS
  164.         void SetOptionGadget(gadget, window, requester, option)
  165.                 struct Gadget       *gadget;
  166.                 struct Window       *window;
  167.                 struct Requester    *requester;
  168.                 char                *option;
  169.  
  170.     DESCRIPTION
  171.         SetOptionGadget sets the text string of the -last- IntuiText entry
  172.         of the <gadget> to the string value in <option>.  This supports
  173.         the cycling of mode values in a boolean gadget.  The <gadget> must
  174.         reside in <window> or a <requester> that belongs to <window>. If
  175.         the gadget is not part of a requester, <requester> must be NULL.
  176.  
  177. */
  178. void
  179. SetOptionGadget(gadget, window, requester, option)
  180.     struct Gadget       *gadget; 
  181.     struct Window       *window;
  182.     struct Requester    *requester;
  183.     char                *option;
  184. {
  185.     struct IntuiText *itp;
  186.     long              position;
  187.  
  188.     /* This is IMPORTANT! The IntuiText structure we are going to modify
  189.        MUST be the last entry in the list.  The first entry encountered
  190.        whose NextText field is NULL is the entry we are looking for.
  191.      */
  192.  
  193.     for (itp = gadget->GadgetText; itp && itp->NextText;
  194.          itp = itp->NextText) ;
  195.  
  196.     if (itp)
  197.         EraseGadgetBox(gadget, window, requester);
  198.         position = RemoveGList(window, gadget, 1L);
  199.         itp->IText = (UBYTE *) option;
  200.         AddGList(window, gadget, position, 1L, requester);
  201.         RefreshGList(gadget, window, requester, 1L);
  202. }
  203.  
  204. /*  FUNCTION
  205.         SetStringGadget - set the value of a string gadget.
  206.  
  207.     SYNOPSIS
  208.         void SetStringGadget(gadget, window, requester, s)
  209.              struct Gadget      *gadget;
  210.              struct Window      *window;
  211.              struct Requester   *requester;
  212.              char               *s;
  213.  
  214.     DESCRIPTION
  215.         SetStringGadget sets the string value of a <gadget>, which
  216.         belongs to <window>, to the character string pointed to by <s>.
  217.         It does this in a "polite" way, first removing the gadget from
  218.         the list, modifying it, then adding it back and refreshing the
  219.         gadget list. 
  220.  
  221.         If the <gadget> belongs to a requester, <requester> must contain
  222.         the address of that requester.  Otherwise, it must be NULL.
  223.  
  224.         If <window> is NULL, the gadget is modified without attempting
  225.         to remove/restore it to/from a window gadget list.
  226. */
  227. void
  228. SetStringGadget(gadget, window, requester, s)
  229.     struct Gadget       *gadget;
  230.     struct Window       *window;
  231.     struct Requester    *requester;
  232.     char                *s;
  233. {
  234.     char    *gs;                        /* pointer to gadget's text */
  235.     int     max;
  236.     ULONG   position;
  237.     struct StringInfo *sInfo;
  238.     char    *s1;
  239.  
  240.     /* Make sure we are trying to modify a string gadget. If we aren't,
  241.      * just don't do anything.
  242.      */
  243.     if (gadget->GadgetType & STRGADGET) {
  244.         gs = (char *) GadgetString(gadget);
  245.         sInfo = (struct StringInfo *) (gadget->SpecialInfo);
  246.         max = sInfo->MaxChars;
  247.         if (window)
  248.             position = RemoveGList(window, gadget, 1L);
  249.         strncpy(gs, s, max);            /* Don't exceed gadget capacity. */
  250.         if (s1 = index(gs, '\n'))       /* Eliminate newline characters. */
  251.             *s1 = '\0';
  252.         ResetStringInfo(sInfo);
  253.         if (window) {
  254.             AddGList(window, gadget, position, 1L, requester);
  255.             RefreshGList(gadget, window, requester, 1L);
  256.         }
  257.     }
  258. }
  259.