home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Frameworks / TransSkel 3.24 / Demos / C Demos / Button / Modal3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-11  |  5.9 KB  |  228 lines  |  [TEXT/CWIE]

  1. /*
  2.  * Modal Dialog 3
  3.  *
  4.  * This dialog consists of:
  5.  * - A Dismiss pushbutton for dismissing the dialog.
  6.  * - Three pushbuttons, Button 1, Button 2, and Button 3, any
  7.  * of which may be selected as the default button.  It is also
  8.  * possible for none of the buttons to be selected.
  9.  * - Four radio buttons used to select which of Button[1-3]
  10.  * is the default, or that none of them is.
  11.  * - Three checkboxes for activating or deactivating Button[1-3].
  12.  * - A couple of static text items for titling the radio button
  13.  * and check box sets.
  14.  * - A user item that's used for drawing the outline around the
  15.  * default button.
  16.  *
  17.  * This dialog demonstrates:
  18.  * - How to associate an outlining function with an arbitrary button.
  19.  * - How to change which button is the default, including how to move
  20.  * the outlining function from one button to another.
  21.  * - How to make sure the outline is redrawn properly when the hiliting
  22.  * state of the outlined button changes.
  23.  *
  24.  * The routines that associate the outline item with buttons or dissociate
  25.  * the item from buttons are written in a more modular fashion than for
  26.  * Dialog 2.  They're also written more generally, since they must handle
  27.  * the case where the default button can be set explicitly to "none".
  28.  */
  29.  
  30. # include    "TransSkel.h"
  31.  
  32. # include    "Button.h"
  33.  
  34.  
  35. typedef enum
  36. {
  37.     iPushDismiss = 1,
  38.     iPushButton1,
  39.     iPushButton2,
  40.     iPushButton3,
  41.     iRadioStaticText,
  42.     iRadioButton1,
  43.     iRadioButton2,
  44.     iRadioButton3,
  45.     iRadioNone,
  46.     iCheckStaticText,
  47.     iCheckButton1,
  48.     iCheckButton2,
  49.     iCheckButton3,
  50.     iOutline
  51. };
  52.  
  53.  
  54. static short        defaultButton = 0;
  55.  
  56.  
  57. static pascal void OutlineButton (DialogPtr dlog, short item);
  58.  
  59.  
  60. /*
  61.  * Set up a variable to point to the drawing procedure.  For 68K code this
  62.  * is just a direct pointer to OutlineButton().  For PowerPC code it is a
  63.  * routine descriptor into which the address of OutlineButton() is stuffed.
  64.  */
  65.  
  66. # if skelPPC        /* PowerPC code */
  67.  
  68. static RoutineDescriptor    drawDesc =
  69.         BUILD_ROUTINE_DESCRIPTOR(uppUserItemProcInfo, OutlineButton);
  70. static UserItemUPP    drawProc = (UserItemUPP) &drawDesc;
  71.  
  72. # else                /* 68K code */
  73.  
  74. static UserItemUPP    drawProc = OutlineButton;
  75.  
  76. # endif
  77.  
  78.  
  79. /*
  80.  * Draw heavy outline around default dialog button.  This function is
  81.  * associated with the user item when there is a default button, and
  82.  * is called by ModalDialog() when the outline needs redrawing.
  83.  */
  84.  
  85. static pascal void
  86. OutlineButton (DialogPtr dlog, short item)
  87. {
  88.     SkelDrawButtonOutline (SkelGetDlogCtl (dlog, defaultButton));
  89. }
  90.  
  91.  
  92. /*
  93.  * Install outlining procedure to associate outline user item with
  94.  * the given button and draw the outline item immediately.
  95.  */
  96.  
  97. static void
  98. InstallOutliner (DialogPtr dlog, short item)
  99. {
  100. Rect    r;
  101.  
  102.     SkelGetDlogRect (dlog, item, &r);            /* get button rect */
  103.     InsetRect (&r, -4, -4);                        /* expand it */
  104.     SkelSetDlogRect (dlog, iOutline, &r);        /* use for outline item */
  105.     SkelSetDlogProc (dlog, iOutline, drawProc);
  106.     SkelDrawButtonOutline (SkelGetDlogCtl (dlog, defaultButton));
  107. }
  108.  
  109.  
  110. /*
  111.  * Remove outlining procedure from current default and erase the
  112.  * current outline.
  113.  */
  114.  
  115. static void
  116. RemoveOutliner (DialogPtr dlog)
  117. {
  118.     SkelSetDlogProc (dlog, iOutline, nil);
  119.     SkelEraseButtonOutline (SkelGetDlogCtl (dlog, defaultButton));
  120. }
  121.  
  122.  
  123. /*
  124.  * Set up button given by item number as default button:
  125.  * - Remove the outliner for the current default, if there is one.
  126.  * - Install an outliner for the new default, if there is one.
  127.  */
  128.  
  129. static void
  130. SetDefaultButton (DialogPtr dlog, short item)
  131. {
  132.     if (defaultButton != 0)
  133.         RemoveOutliner (dlog);
  134.     defaultButton = item;
  135.     if (defaultButton != 0)
  136.         InstallOutliner (dlog, defaultButton);
  137. }
  138.  
  139.  
  140. void
  141. DoModal3 (void)
  142. {
  143. ModalFilterUPP    filter;
  144. DialogPtr    dlog;
  145. GrafPtr    savePort;
  146. short    item;
  147. short    value;
  148. short    hilite;
  149. short    loop;
  150.  
  151.     dlog = GetNewDialog (modal3Res, nil, (WindowPtr) -1L);
  152.     if (dlog == (DialogPtr) nil)
  153.     {
  154.         SysBeep (1);
  155.         return;
  156.     }
  157.  
  158.     SkelPositionWindow (dlog, skelPositionOnMainDevice, horizRatio, vertRatio);
  159.  
  160.     GetPort (&savePort);
  161.     SetPort (dlog);
  162.  
  163.     SetDefaultButton (dlog, iPushButton1);
  164.     SkelSetDlogCtlValue (dlog, iCheckButton1, 1);
  165.     SkelSetDlogCtlValue (dlog, iCheckButton2, 1);
  166.     SkelSetDlogCtlValue (dlog, iCheckButton3, 1);
  167.     SkelSetDlogRadioButtonSet (dlog, iRadioButton1, iRadioNone, iRadioButton1);
  168.  
  169.     ShowWindow (dlog);
  170.  
  171.     loop = 1;
  172.     while (loop)
  173.     {
  174.         /*
  175.          * Get the standard filter and specify the default button so it will
  176.          * map return/enter.  If the default is zero, mapping is turned off.
  177.          *
  178.          * Note: the mapping causes the default button to flash when return/enter
  179.          * are typed, but the dialog isn't dismissed until Dismiss is clicked,
  180.          * which is unlike normal dialog operation.
  181.          */
  182.         filter = SkelDlogFilter (nil, false);
  183.         SkelDlogDefaultItem (defaultButton);    /* turns off if zero */
  184.         ModalDialog (filter, &item);
  185.         SkelRmveDlogFilter ();
  186.  
  187.         switch (item)
  188.         {
  189.         case iPushDismiss:
  190.             loop = 0;
  191.             break;
  192.         case iPushButton1:        /* ignore hits in these items */
  193.         case iPushButton2:
  194.         case iPushButton3:
  195.             break;
  196.         case iRadioButton1:
  197.         case iRadioButton2:
  198.         case iRadioButton3:
  199.             SkelSetDlogRadioButtonSet (dlog, iRadioButton1, iRadioNone, item);
  200.             /* remap item number from radio button range into pushbutton range */
  201.             item += iPushButton1 - iRadioButton1;
  202.             SetDefaultButton (dlog, item);
  203.             break;
  204.         case iRadioNone:
  205.             SkelSetDlogRadioButtonSet (dlog, iRadioButton1, iRadioNone, item);
  206.             SetDefaultButton (dlog, 0);    /* no default button */
  207.             break;
  208.         case iCheckButton1:
  209.         case iCheckButton2:
  210.         case iCheckButton3:
  211.             value = SkelToggleDlogCtlValue (dlog, item);
  212.             hilite = (value ? normalHilite : dimHilite);
  213.             /* remap item number from checkbox range into pushbutton range */
  214.             item += iPushButton1 - iCheckButton1;
  215.             /*
  216.              * Set button's hiliting state to make it active or inactive.
  217.              * If the new state is different than previous and the button
  218.              * is the default, redraw the outline.
  219.              */
  220.             if (SkelSetDlogCtlHilite (dlog, item, hilite) && item == defaultButton)
  221.                 SkelDrawButtonOutline (SkelGetDlogCtl (dlog, item));
  222.             break;
  223.         }
  224.     }
  225.     DisposeDialog (dlog);
  226.     SetPort (savePort);
  227. }
  228.