home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / progrmng / popmncdf.sit / TestControl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-18  |  2.5 KB  |  146 lines  |  [TEXT/KAHL]

  1. /*
  2.  *  TestControl.c
  3.  */
  4.  
  5. #define ok        1
  6.  
  7. typedef struct {
  8.     int        jump;
  9.     long    add;
  10. } defRec, *defPtr, **defHdl;
  11.  
  12. defHdl        theDef;
  13. Boolean        DialogExists();
  14. Handle        GetItemHandle();
  15.  
  16. pascal long PopupMenu();
  17.  
  18. main()
  19. {
  20.     short        id, i;
  21.     DialogPtr    dlog;
  22.  
  23.     FlushEvents (everyEvent - diskMask, 0 );
  24.     InitGraf (&thePort);
  25.     InitFonts ();
  26.     InitWindows ();
  27.     InitMenus ();
  28.     TEInit ();
  29.     InitDialogs (nil);        /* no restart proc */
  30.     InitCursor ();
  31.  
  32.     theDef = (defHdl)GetResource('CDEF', 130);
  33.     (**theDef).jump = 0x4ef9;
  34.     (**theDef).add = (long)PopupMenu;
  35.  
  36.     if(!DialogExists(256))
  37.     {
  38.         SysBeep(3);
  39.         return;
  40.     }
  41.     
  42.     dlog = GetNewDialog(256, NULL, (WindowPtr) -1);
  43.     
  44.     PositionDialog(dlog);
  45.     
  46.     SetItemValue(dlog, 2, 1);
  47.     
  48.     ShowWindow(dlog);
  49.     
  50.     do
  51.     {
  52.         ModalDialog(NULL, &id);
  53.         if(id == 2)
  54.         {
  55.             SetItemValue(dlog, 2, !GetItemValue(dlog, 2));
  56.             HiliteItem(dlog, 3, GetItemValue(dlog, 2) ? 0 : 255);
  57.             i = Random() % 7;
  58.             i = Abs(i);        /* macros require separate lines sometimes */
  59.             if (i==0) i=1;
  60.             if (i==3) i=4;
  61.             SetItemValue(dlog, 3, i);
  62.         }
  63.     } while(id > ok);
  64.     
  65.     DisposDialog(dlog);
  66.     ReleaseResource(theDef);
  67. }
  68.  
  69. Handle    GetItemHandle(dlog, item, type, rect)
  70.     DialogPtr    dlog;
  71.     int            item;
  72.     short        *type;
  73.     Rect        *rect;
  74. {
  75.     Rect    r;
  76.     short    t;
  77.     Handle    hand;
  78.  
  79.     if(!rect)
  80.         rect = &r;
  81.     if(!type)
  82.         type = &t;
  83.     GetDItem(dlog, item, type, &hand, rect);
  84.     
  85.     return hand;
  86. }
  87.  
  88. GetItemValue(dlog, item)
  89.     DialogPtr    dlog;
  90.     int            item;
  91. {
  92.     return GetCtlValue((ControlHandle)GetItemHandle(dlog, item, NIL, NIL));
  93. }
  94.  
  95. SetItemValue(dlog, item, value)
  96.     DialogPtr    dlog;
  97.     int            item;
  98. {
  99.     SetCtlValue((ControlHandle)GetItemHandle(dlog, item, NIL, NIL), value);
  100. }
  101.  
  102. HiliteItem(dlog, item, value)
  103.     DialogPtr    dlog;
  104.     int            item;
  105.     int            value;
  106. {
  107.     Rect        rect;
  108.     short        type;
  109.     Handle        hand;
  110.     
  111.     hand = GetItemHandle(dlog, item, &type, &rect);
  112.  
  113.     type &= ~itemDisable;
  114.     if((type & ~3) == ctrlItem)
  115.         HiliteControl((ControlHandle)hand, value);
  116. }
  117.  
  118. PositionDialog(dptr)
  119. DialogPtr        dptr;
  120. {
  121.     GrafPtr                wPort;
  122.     int                    hPos, vPos;
  123.     Rect                wRect, dRect;
  124.     
  125.     GetWMgrPort(&wPort);
  126.     wRect = wPort->portRect;
  127.     wRect.top += GetMBarHeight();
  128.     dRect = dptr->portRect;
  129.     hPos = (wRect.left + wRect.right + dRect.left - dRect.right) / 2;
  130.     if(hPos < (wRect.left + 3))
  131.         hPos = wRect.left + 3;
  132.     vPos = (2 * wRect.top + wRect.bottom + dRect.top - dRect.bottom) / 3;
  133.     if(vPos < (wRect.top + 3))
  134.         vPos = wRect.top + 3;
  135.     MoveWindow(dptr, hPos, vPos, false);
  136. }
  137.  
  138. Boolean DialogExists(id)
  139.     short    id;
  140. {
  141.     DialogTHndl    hand;
  142.     if(!(hand = (DialogTHndl) GetResource('DLOG', id)))
  143.         return 0 != 0;
  144.     return GetResource('DITL', (*hand)->itemsID) != 0;
  145. }
  146.