home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Libraries / Mercutio 1.2b20 / Sample Code / PopupDialog.p < prev   
Encoding:
Text File  |  1994-09-18  |  6.9 KB  |  243 lines  |  [TEXT/PJMM]

  1. UNIT PopupDialog;
  2. INTERFACE
  3.     USES
  4.         MercutioAPI;
  5.     CONST
  6.         Off = 0;
  7.         On = 1;
  8.         Disable = 255;
  9.  
  10.         enterKey = 3;
  11.         helpKey = 5;
  12.         deleteKey = 8;
  13.         tabKey = 9;
  14.         linefeedKey = 10;
  15.         CR = 13;
  16.         return = 13;
  17.  
  18.         periodKey = 46;
  19.         slashKey = 47;
  20.         questionmarkKey = 63;
  21.  
  22.         PopupDLOG = 3001;
  23.         popupTitleItem = 3;
  24.         defaultItem = 5;
  25.  
  26.     PROCEDURE doPopupDialog (theMenu: menuHandle);
  27.  
  28. IMPLEMENTATION
  29.  
  30.     TYPE
  31.         dialogGlobals = RECORD
  32.                 hMenu: menuHandle;
  33.                 popUpRect: rect;
  34.                 popSelected: integer;
  35.             END;
  36.         dialogGlobalsPtr = ^dialogGlobals;
  37.  
  38.     PROCEDURE FakeClick (VAR theDialog: DialogPtr; theButton: Integer);
  39. { select/deselect a button in a dialog }
  40.         VAR
  41.             thetype: Integer;
  42.             itemHdl: Handle;
  43.             iRect: Rect;
  44.             Ticks: LongInt;
  45.     BEGIN
  46.         GetDItem(theDialog, theButton, thetype, ItemHdl, iRect);
  47.         HiliteControl(ControlHandle(ItemHdl), On);
  48.         Delay(8, Ticks);
  49.         HiliteControl(ControlHandle(ItemHdl), Off);
  50.     END; { of proc FakeClick }
  51.  
  52.     PROCEDURE DrawDefaultBtn (theDialog: DialogPtr; ItemNumber: Integer);
  53.   { heavy black outline }
  54.         VAR
  55.             iType: Integer;
  56.             itmHdl: Handle;
  57.             thePen: PenState;
  58.             iRect: Rect;
  59.     BEGIN
  60.         SetPort(theDialog);                 { set window to current graf port }
  61.         GetDItem(theDialog, ItemNumber, iType, itmHdl, iRect); { get location }
  62.         GetPenState(thePen);                               { save current pen }
  63.         PenNormal;
  64.         PenSize(3, 3);
  65.         InsetRect(iRect, -4, -4);
  66.         FrameRoundRect(iRect, 16, 16);
  67.         SetPenState(thePen);                               { restore old pen }
  68.     END;  { of proc DrawDefaultBtn }
  69.  
  70.  
  71.     PROCEDURE UpdatePopUp (theDialog: DialogPtr; Item: Integer);
  72. {   draw popup menu }
  73.         VAR
  74.             aRect: Rect;
  75.             theGlobals: dialogGlobalsPtr;
  76.             IsNovice: boolean;
  77.             thetype: Integer;
  78.             itmHdl: Handle;
  79.             iRect: Rect;
  80.     BEGIN
  81.         theGlobals := dialogGlobalsPtr(GetWRefCon(theDialog));
  82.         GetDItem(theDialog, popupTitleItem, theType, ItmHdl, aRect);
  83.         insetRect(aRect, -2, -2);
  84.         eraseRect(aRect);
  85.         insetRect(aRect, 2, 2);
  86.  
  87.         MDEF_CalcItemSize(theGlobals^.hMenu, theGlobals^.popSelected, aRect);
  88.  
  89.         MoveTo(aRect.left + 2, aRect.bottom);
  90.         LineTo(aRect.right, aRect.bottom);
  91.         MoveTo(aRect.right, aRect.top + 2);
  92.         LineTo(aRect.right, aRect.bottom);
  93.  
  94.         MoveTo(aRect.Left + 6, aRect.Bottom - 5);    { move to text position }
  95.         FillRect(aRect, white);
  96.         MDEF_DrawItem(theGlobals^.hMenu, theGlobals^.PopSelected, aRect);
  97.  
  98.         FrameRect(aRect);
  99.         CheckItem(theGlobals^.hMenu, theGlobals^.PopSelected, true);
  100.         SetDItem(theDialog, popupTitleItem, theType, ItmHdl, aRect);
  101.     END;
  102.  
  103.  
  104.     PROCEDURE doPopupDialog (theMenu: menuHandle);
  105.         VAR
  106.             theDialog: dialogPtr;
  107.             itemHit: integer;
  108.             iType: integer;
  109.             iRect: rect;
  110.             iHandle: handle;
  111.             popupRect: rect;
  112.             theEvent: eventRecord;
  113.             theGlobals: dialogGlobals;
  114.             eventOccured: boolean;
  115.             popRect: rect;
  116.             theErr, result: longint;
  117.             ShiftUsed, CmdKeyUsed, BadKey: boolean;
  118.             thePoint: point;
  119.             OldPort: GrafPtr;
  120.             theKey: integer;
  121.             tempStr: str255;
  122.             mouseLoc: point;                { the area it was in        }
  123.             whichWindow: WindowPtr;            { Dummy,cause we have no windows}
  124.             DragBounds: rect;
  125.             windowLoc: integer;
  126.     BEGIN
  127.         DragBounds := thePort^.portBits.bounds;
  128.         theDialog := GetNewDialog(PopupDLOG, NIL, pointer(-1));
  129.         IF theDialog = NIL THEN
  130.             BEGIN
  131.                 sysbeep(1);
  132.                 exit(doPopupDialog);
  133.             END;
  134.         SetPort(theDialog);
  135.  
  136.         theGlobals.hMenu := theMenu;
  137.         SetWRefCon(theDialog, longint(@theGlobals));
  138.  
  139.         CalcMenuSize(theMenu);
  140.         GetDItem(theDialog, popupTitleItem, iType, iHandle, iRect);
  141.         SetRect(theGlobals.PopUpRect, iRect.left + 3, iRect.top, iRect.left + theMenu^^.menuwidth + 4, iRect.bottom + 2);
  142.         GetDItem(theDialog, popupTitleItem, iType, iHandle, iRect);
  143.         SetDItem(theDialog, popupTitleItem, iType, iHandle, theGlobals.PopUpRect);
  144.  
  145.         GetDItem(theDialog, popupTitleItem, iType, iHandle, iRect);  { get item's rect }
  146.         SetDItem(theDialog, popupTitleItem, userItem, Handle(@UpdatePopup), iRect);
  147.  
  148.         GetDItem(theDialog, defaultItem, iType, iHandle, iRect);  { get item's rect }
  149.         SetDItem(theDialog, defaultItem, userItem, Handle(@DrawDefaultBtn), iRect);
  150.  
  151.         ShowWindow(theDialog);
  152.  
  153.         REPEAT
  154.             eventOccured := GetNextEvent(everyEvent, theEvent);
  155.             IF eventOccured THEN
  156.                 BEGIN
  157.                     CASE theEvent.what OF
  158.                         keydown, autokey:{ any key down! }
  159.                             BEGIN
  160.                                 theKey := BitAnd(theEvent.message, charCodeMask);        { decode char }
  161.                                 CmdKeyUsed := (BitAnd(theEvent.modifiers, cmdKey) <> 0);    { cmd key down? }
  162.                                 ShiftUsed := (BitAnd(theEvent.modifiers, shiftKey) <> 0);        { shift key down? }
  163.                                 CASE theKey OF
  164.                                     enterKey, CR:    { OK Button equivalents }
  165.                                         BEGIN
  166.                                             itemHit := OK;
  167.                                             FakeClick(theDialog, itemHit);
  168.                                         END;
  169.                                     OTHERWISE
  170.                                         IF CmdKeyUsed & (theKey = periodKey) THEN
  171.                                             BEGIN
  172.                                                 FakeClick(theDialog, Cancel);
  173.                                                 itemHit := Cancel;
  174.                                             END;
  175.                                 END;
  176.                             END;
  177.                         mouseDown:            { we had a mouse-down theEvent    }
  178.                             BEGIN
  179.                                 mouseLoc := theEvent.where;    { wheres the pesky mouse    }
  180.                                 windowLoc := FindWindow(mouseLoc, whichWindow); { find out where }
  181.                                 CASE windowLoc OF        { now case on the location    }
  182.                                     inMenuBar: 
  183.                                         BEGIN
  184.                                             sysbeep(1);
  185.                                         END;
  186.                                     inSysWindow: 
  187.                                         SystemClick(theEvent, whichWindow); {It was in a desk acc    }
  188.                                     inContent: 
  189.                                         IF FrontWindow <> whichWindow THEN
  190.                                             SelectWindow(whichWindow)
  191.                                         ELSE
  192.                                             BEGIN
  193.                                                 IF isDialogEvent(theEvent) & DialogSelect(theEvent, theDialog, itemHit) THEN
  194.                                                     CASE itemHit OF
  195.                                                         popupTitleItem: 
  196.                                                             BEGIN
  197.                                                                 GetDItem(theDialog, popupTitleitem, iType, iHandle, iRect);
  198.                                                                 InsetRect(iRect, 1, 1);
  199.                                                                 InvertRect(iRect);
  200.                                                                 popRect := theGlobals.PopUpRect;
  201.                                                                 LocalToGlobal(popRect.TopLeft);
  202.                                                                 LocalToGlobal(popRect.BotRight);
  203.                                                                 Result := PopUpMenuSelect(theGlobals.hMenu, popRect.Top, popRect.Left, theGlobals.popSelected);
  204.                                                                 InvertRect(iRect);
  205.                                                                 IF (Result > 0) AND (LoWord(Result) <> theGlobals.Popselected) THEN
  206.                                                                     BEGIN
  207.                                                                         CheckItem(theGlobals.hMenu, theGlobals.popSelected, False);
  208.                                                                         theGlobals.popSelected := LoWord(Result);
  209.                                                                         UpdatePopUp(theDialog, popupTitleItem);
  210.                                                                         itemHit := PopupTitleItem;    { pass number of popupmenu selected }
  211.                                                                     END;
  212.                                                             END;
  213.                                                         OTHERWISE
  214.                                                             ;
  215.                                                     END;
  216.                                             END;
  217.                                     inGoAway: 
  218.                                         BEGIN
  219.                                             IF TrackGoAway(whichWindow, theEvent.where) THEN
  220.                                                 sysbeep(1);
  221.                                         END;
  222.                                     inDrag: 
  223.                                         DragWindow(whichWindow, theEvent.where, DragBounds);
  224.                                     inGrow: 
  225.                                         BEGIN
  226.                                             sysbeep(1);
  227.                                         END;
  228.                                     OTHERWISE
  229.                                         BEGIN
  230.                                         END;
  231.                                 END;
  232.                             END;
  233.                         OTHERWISE
  234.                             IF DialogSelect(theEvent, theDialog, itemHit) THEN
  235.                                 BEGIN
  236.                                 END;
  237.                     END
  238.                 END;
  239.         UNTIL itemhit IN [1, 2];
  240.         DisposDialog(theDialog);
  241.     END;
  242.  
  243. END.