home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / TransSkel / Demos / C Demos / Button / Dialog2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-16  |  3.0 KB  |  129 lines  |  [TEXT/KAHL]

  1. /*
  2.  * Dialog 2
  3.  *
  4.  * This dialog consists of an edittext item for entering a search
  5.  * string, a statictext item for titling the edittext item, a Cancel
  6.  * button, a Find button, and a user item for outlining the default
  7.  * button.
  8.  * 
  9.  * The Cancel button is the default when the edittext item is empty.
  10.  * The Find button is the default when the edittext item contains text.
  11.  *
  12.  * This dialog demonstrates:
  13.  * - How to associate an outlining function with an arbitrary button.
  14.  * - How to change which button is the default, which includes moving
  15.  * the outlining function from one button to another.
  16.  */
  17.  
  18. # include    "TransSkel.h"
  19.  
  20. # include    "Button.h"
  21.  
  22.  
  23. typedef enum
  24. {
  25.     findItem = 1,
  26.     cancelItem,
  27.     staticTextItem,
  28.     editTextItem,
  29.     outlineItem
  30. };
  31.  
  32.  
  33. static short    defaultButton = 0;
  34.  
  35.  
  36. /*
  37.  * Draw heavy outline around default dialog button.  This function is
  38.  * associated with the user item when there is a default button, and
  39.  * is called by ModalDialog() when the outline needs redrawing.
  40.  */
  41.  
  42. static pascal void
  43. OutlineButton (DialogPtr dlog, short item)
  44. {
  45.     SkelDrawButtonOutline (SkelGetDlogCtl (dlog, defaultButton));
  46. }
  47.  
  48.  
  49. /*
  50.  * Set up button given by item number as default button.
  51.  * Install an outlining procedure to associate outline user item with
  52.  * the given button and draw the outline item immediately.
  53.  */
  54.  
  55. static void
  56. SetDefaultButton (DialogPtr dlog, short item)
  57. {
  58. Rect    r;
  59.  
  60.     defaultButton = item;
  61.     SkelGetDlogRect (dlog, defaultButton, &r);    /* get button rect */
  62.     InsetRect (&r, -4, -4);                        /* expand it */
  63.     SkelSetDlogRect (dlog, outlineItem, &r);    /* use for outline item */
  64.     SkelSetDlogProc (dlog, outlineItem, OutlineButton);
  65.     SkelDrawButtonOutline (SkelGetDlogCtl (dlog, defaultButton));
  66. }
  67.  
  68.  
  69. void
  70. DoDialog2 (void)
  71. {
  72. ModalFilterProcPtr    filter;
  73. DialogPtr    dlog;
  74. GrafPtr    savePort;
  75. short    item;
  76. short    newDefault;
  77. Str255    str;
  78.  
  79.     dlog = GetNewDialog (dlog2Res, nil, (WindowPtr) -1L);
  80.     if (dlog == (DialogPtr) nil)
  81.     {
  82.         SysBeep (1);
  83.         return;
  84.     }
  85.  
  86.     SkelPositionWindow (dlog, skelPositionOnMainDevice, horizRatio, vertRatio);
  87.  
  88.     GetPort (&savePort);
  89.     SetPort (dlog);
  90.  
  91.     SetDefaultButton (dlog, cancelItem);
  92.  
  93.     ShowWindow (dlog);
  94.  
  95.     for (;;)
  96.     {
  97.         /*
  98.          * Get the standard filter and specify the default button so it will
  99.          * map return/enter.  Map escape and command-period onto the cancel
  100.          * button.
  101.          */
  102.         filter = SkelDlogFilter (nil, false);
  103.         SkelDlogDefaultItem (defaultButton);
  104.         SkelDlogCancelItem (cancelItem);
  105.         ModalDialog (filter, &item);
  106.         SkelRmveDlogFilter ();
  107.  
  108.         if (item == findItem || item == cancelItem)
  109.             break;
  110.  
  111.         /*
  112.          * Select Find button as default if editText item is
  113.          * non-empty.  Select Cancel button otherwise.  But
  114.          * don't actually do anything if the default button's
  115.          * already set correctly.
  116.          */
  117.  
  118.         SkelGetDlogStr (dlog, editTextItem, str);
  119.         newDefault = (str[0] > 0 ? findItem : cancelItem);
  120.         if (newDefault != defaultButton)
  121.         {
  122.             SkelEraseButtonOutline (SkelGetDlogCtl (dlog, defaultButton));
  123.             SetDefaultButton (dlog, newDefault);
  124.         }
  125.     }
  126.     DisposeDialog (dlog);
  127.     SetPort (savePort);
  128. }
  129.