home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / progrmng / transklc.sit / DialogSkel.c.bin / DialogSkel.c
Encoding:
C/C++ Source or Header  |  1989-02-08  |  6.3 KB  |  390 lines  |  [TEXT/KAHL]

  1. /*
  2.     DialogSkel - TransSkel modeless dialog demonstration.
  3.     
  4.     Project should include DialogSkel.c, TransSkel.c (or a project
  5.     built from TransSkel.c), and MacTraps.
  6.  
  7.     21 Apr 1988    v1.0 Paul DuBois
  8.     29 Jan 1989 Conversion for TransSkel 2.0.  Integer should be a
  9.                 typedef for compiler 2-byte integer type.
  10. */
  11.  
  12. # include    <DialogMgr.h>
  13. # include    <EventMgr.h>
  14. # include    <MenuMgr.h>
  15. # include    <ToolBoxUtil.h>
  16. # include    <Pascal.h>
  17.  
  18.  
  19. # define    nil            (0L)
  20.  
  21. typedef    int    Integer;
  22.  
  23. typedef enum
  24. {
  25.     mDlogRes = 1000,
  26.     aboutAlrtRes    /* About... alert resource number */
  27. };
  28.  
  29.  
  30. typedef enum                /* File menu item numbers */
  31. {
  32.     showDlog1 = 1,
  33.     showDlog2,
  34.     /* --- */
  35.     quit = 4
  36. };
  37.  
  38.  
  39. typedef enum                 /* Edit menu item numbers */
  40. {
  41.     undo = 1,
  42.     /* --- */
  43.     cut = 3,
  44.     copy,
  45.     paste,
  46.     clear
  47. };
  48.  
  49.  
  50. typedef enum                /* dialog item numbers */
  51. {
  52.     button1 = 1,
  53.     edit1,
  54.     static1,
  55.     radio1,
  56.     radio2,
  57.     radio3,
  58.     check1,
  59.     check2,
  60.     user1
  61. };
  62.  
  63.  
  64. DialogPtr    mDlog1;
  65. DialogPtr    mDlog2;
  66. Integer        iconNum1 = 0;
  67. Integer        iconNum2 = 0;
  68.  
  69. pascal void DrawIcon (dlog, item)
  70. DialogPtr    dlog;
  71. Integer        item;
  72. {
  73. Handle    h;
  74. Handle    itemHandle;
  75. Integer    itemType;
  76. Rect    itemRect;
  77.  
  78.     GetDItem (dlog, item, &itemType, &itemHandle, &itemRect);
  79.     h = GetIcon (dlog == mDlog1 ? iconNum1 : iconNum2);
  80.     PlotIcon (&itemRect, h);
  81. }
  82.  
  83.  
  84. Boolean GetDCtl (dlog, item)
  85. DialogPtr    dlog;
  86. Integer        item;
  87. {
  88. Handle    itemHandle;
  89. Integer    itemType;
  90. Rect    itemRect;
  91.  
  92.     GetDItem (dlog, item, &itemType, &itemHandle, &itemRect);
  93.     return (GetCtlValue (itemHandle));
  94. }
  95.  
  96.  
  97. SetDCtl (dlog, item, value)
  98. DialogPtr    dlog;
  99. Integer        item;
  100. Boolean        value;
  101. {
  102. Handle    itemHandle;
  103. Integer    itemType;
  104. Rect    itemRect;
  105.  
  106.     GetDItem (dlog, item, &itemType, &itemHandle, &itemRect);
  107.     SetCtlValue (itemHandle, (Integer) value);
  108. }
  109.  
  110.  
  111. GetDText (dlog, item, str)
  112. DialogPtr    dlog;
  113. Integer        item;
  114. StringPtr    str;
  115. {
  116. Handle    itemHandle;
  117. Integer    itemType;
  118. Rect    itemRect;
  119.  
  120.     GetDItem (dlog, item, &itemType, &itemHandle, &itemRect);
  121.     GetIText (itemHandle, str);
  122. }
  123.  
  124.  
  125. SetDText (dlog, item, str)
  126. DialogPtr    dlog;
  127. Integer        item;
  128. Str255        str;
  129. {
  130. Handle    itemHandle;
  131. Integer    itemType;
  132. Rect    itemRect;
  133.  
  134.     GetDItem (dlog, item, &itemType, &itemHandle, &itemRect);
  135.     SetIText (itemHandle, str);
  136. }
  137.  
  138.  
  139. SetDProc (dlog, item, p)
  140. DialogPtr    dlog;
  141. Integer        item;
  142. ProcPtr        p;
  143. {
  144. Handle    itemHandle;
  145. Integer    itemType;
  146. Rect    itemRect;
  147.  
  148.     GetDItem (dlog, item, &itemType, &itemHandle, &itemRect);
  149.     SetDItem (dlog, item, itemType, p, &itemRect);
  150. }
  151.  
  152.  
  153. SetDRadio (dlog, item)
  154. DialogPtr    dlog;
  155. Integer        item;
  156. {
  157. DialogPtr    partner;
  158. Handle        itemHandle;
  159. Integer        itemType;
  160. Rect        itemRect;
  161. GrafPtr        tmpPort;
  162.  
  163.     partner = (DialogPtr) GetWRefCon (dlog);
  164.     SetDCtl (dlog, radio1, item == radio1);
  165.     SetDCtl (dlog, radio2, item == radio2);
  166.     SetDCtl (dlog, radio3, item == radio3);
  167.  
  168.     if (partner == mDlog1)
  169.         iconNum1 = item - radio1;
  170.     else
  171.         iconNum2 = item - radio1;
  172.  
  173.     GetDItem (partner, user1, &itemType, &itemHandle, &itemRect);
  174.     GetPort (&tmpPort);
  175.     SetPort (partner);
  176.     InvalRect (&itemRect);
  177.     SetPort (tmpPort);
  178. }
  179.  
  180.  
  181. Event (item, event)
  182. Integer        item;
  183. EventRecord    *event;
  184. {
  185. DialogPtr    actor, partner;
  186. Str255        title;
  187. Boolean        value;
  188.  
  189.     GetPort (&actor);
  190.     partner = (DialogPtr) GetWRefCon (actor);
  191.     switch (item)
  192.     {
  193.         case button1:
  194.             GetDText (actor, edit1, title);
  195.             SetWTitle (partner, title);
  196.             break;
  197.  
  198.         /* set radio buttons */
  199.  
  200.         case radio1:
  201.             SetDRadio (actor, radio1);
  202.             break;
  203.  
  204.         case radio2:
  205.             SetDRadio (actor, radio2);
  206.             break;
  207.  
  208.         case radio3:
  209.             SetDRadio (actor, radio3);
  210.             break;
  211.  
  212.         /* flip check boxes */
  213.  
  214.         case check1:
  215.             value = !GetDCtl (actor, item);
  216.             SetDCtl (actor, item, value);
  217.             if (value == false)
  218.                 HideWindow (partner);
  219.             else
  220.                 ShowWindow (partner);
  221.             break;
  222.  
  223.         case check2:
  224.             value = !GetDCtl (actor, check2);
  225.             SetDCtl (actor, check2, value);
  226.             ((WindowPeek) partner)->goAwayFlag = (char) (value ? 255 : 0);
  227.             break;
  228.     }
  229. }
  230.  
  231.  
  232. Close ()
  233. {
  234. DialogPtr    actor, partner;
  235.  
  236.     GetPort (&actor);
  237.     partner = (DialogPtr) GetWRefCon (actor);
  238.     HideWindow (actor);
  239.     SetDCtl (partner, check1, false);
  240. }
  241.  
  242.  
  243. Clobber ()
  244. {
  245. DialogPtr    theDialog;
  246.  
  247.     GetPort (&theDialog);
  248.     DisposDialog (theDialog);
  249. }
  250.  
  251.  
  252. /*
  253.     File menu handler
  254. */
  255.  
  256. DoFileMenu (item)
  257. Integer    item;
  258. {
  259.  
  260.     switch (item)
  261.     {
  262.         case showDlog1:
  263.             SelectWindow (mDlog1);
  264.             ShowWindow (mDlog1);
  265.             SetDCtl (mDlog2, check1, true);
  266.             break;
  267.         
  268.         case showDlog2:
  269.             SelectWindow (mDlog2);
  270.             ShowWindow (mDlog2);
  271.             SetDCtl (mDlog1, check1, true);
  272.             break;
  273.     
  274.         case quit:
  275.             SkelWhoa ();
  276.             break;
  277.     }
  278. }
  279.  
  280.  
  281. /*
  282.     Handle Edit menu items for text window
  283. */
  284.  
  285. DoEditMenu (item)
  286. Integer    item;
  287. {
  288. DialogPtr    theDialog;
  289.  
  290.     if (SystemEdit (item - 1))
  291.         return;
  292.     theDialog = (DialogPtr) FrontWindow ();
  293.     if (((WindowPeek) theDialog)->windowKind != dialogKind)
  294.         return;
  295.  
  296.     switch (item)
  297.     {
  298.         case cut:
  299.         {
  300.             DlgCut (theDialog);
  301.             (void) ZeroScrap ();
  302.             (void) TEToScrap ();
  303.             break;
  304.         }
  305.  
  306.         case copy:
  307.         {
  308.             DlgCopy (theDialog);
  309.             (void) ZeroScrap ();
  310.             (void) TEToScrap ();
  311.             break;
  312.         }
  313.  
  314.         case paste:
  315.         {
  316.             (void) TEFromScrap ();
  317.             DlgPaste (theDialog);
  318.             break;
  319.         }
  320.  
  321.         case clear:
  322.         {
  323.             DlgDelete (theDialog);
  324.             break;
  325.         }
  326.     }
  327. }
  328.  
  329.  
  330. /*
  331.     Handle selection of Aboutâ•” item from Apple menu
  332. */
  333.  
  334. DoAbout ()
  335. {
  336.     (void) Alert (aboutAlrtRes, nil);
  337. }
  338.  
  339.  
  340. DialogPtr DemoDialog (title, x, y)
  341. StringPtr    title;
  342. Integer        x, y;
  343. {
  344. DialogPtr    theDialog;
  345.  
  346.     theDialog = GetNewDialog (mDlogRes, nil, -1L);
  347.     MoveWindow (theDialog, x, y, false);
  348.     SetWTitle (theDialog, title);
  349.     (void) SkelDialog (theDialog, Event, Close, Clobber);
  350.     return (theDialog);
  351. }
  352.  
  353.  
  354. main ()
  355. {
  356. MenuHandle    m;
  357.  
  358.     SkelInit (6, nil);
  359.     SkelApple ("\pAbout DialogSkelâ•”", DoAbout);
  360.  
  361.     m = NewMenu (1000, "\pFile");
  362.     AppendMenu (m, "\pShow Dialog 1;Show Dialog 2;(-");
  363.     AppendMenu (m, "\pQuit/Q");
  364.     (void) SkelMenu (m, DoFileMenu, nil, false);
  365.  
  366.     m = NewMenu (1001, "\pEdit");
  367.     AppendMenu (m, "\p(Undo/Z;(-;Cut/X;Copy/C;Paste/V;Clear");
  368.     (void) SkelMenu (m, DoEditMenu, nil, true);
  369.     
  370.     mDlog1 = DemoDialog ("\pModeless Dialog 1", 50, 50);
  371.     mDlog2 = DemoDialog ("\pModeless Dialog 2", 150, 200);
  372.     SetWRefCon (mDlog1, mDlog2);
  373.     SetWRefCon (mDlog2, mDlog1);
  374.     SetDText (mDlog1, edit1, "\pModeless Dialog 2");
  375.     SetDText (mDlog2, edit1, "\pModeless Dialog 1");
  376.     SetDProc (mDlog1, user1, DrawIcon);
  377.     SetDProc (mDlog2, user1, DrawIcon);
  378.     SetDCtl (mDlog1, radio1, true);
  379.     SetDCtl (mDlog2, radio1, true);
  380.     SetDCtl (mDlog1, check1, true);
  381.     SetDCtl (mDlog2, check1, true);
  382.     SetDCtl (mDlog1, check2, true);
  383.     SetDCtl (mDlog2, check2, true);
  384.     ShowWindow (mDlog1);
  385.     ShowWindow (mDlog2);
  386.  
  387.     SkelMain ();
  388.     SkelClobber ();
  389. }
  390.