home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Pascal / Snippets / PopUpTest / PopUpTest.p < prev    next >
Encoding:
Text File  |  1995-12-08  |  6.6 KB  |  275 lines  |  [TEXT/CWIE]

  1. {
  2. I don't remember where I found this, but it was an old MPW Pascal program which has
  3. been completely refurbished to run native in CW7.
  4.  
  5. It primarily demonstrates the use of the dialog manager.  The dialog it displays includes
  6. editable fields, pop-ups, pop-ups with submenus, pop-ups with editable text, pop-ups which
  7. display at a mouseDown location, disabling buttons, etc., etc...
  8.  
  9. Original author unknown.
  10.  
  11. Changes made 12/5/95 by Bill Catambay, catambay@aol.com
  12. }
  13.  
  14. Program PopUpTest;
  15.  
  16. USES 
  17.     QuickDraw, Types, Events, Menus, OSUtils, Fonts, Windows, Resources,
  18.     ToolUtils, Sound, Palettes, Memory, Dialogs, Devices, Processes, TextEdit;
  19.  
  20. const
  21.     dialogID = 128;
  22.     firstMenu = 2;        {item numbers}
  23.     secondMenu = 3;
  24.     hideSecond = 4;
  25.     disableControl = 5;
  26.     disableMenu = 6;
  27.     resetMenu = 7;
  28.     speedLabel = 8;
  29.     speedMenu = 10;
  30.     ctlTitleMenu = 11;
  31.     altCTitle = 12;
  32.     invisibleMenu = 13;
  33.     frameItem = 14;
  34.     emptyMenu = 15;
  35.     reportItem = 16;
  36.  
  37. type
  38.     popupPrivateData = record
  39.         mHandle: MenuHandle;
  40.         mID: Integer;
  41.         end;
  42.     popupPrivateDataPtr = ^popupPrivateData;
  43.     popupPrivateDataHdl = ^popupPrivateDataPtr;
  44.  
  45. var
  46.     theDialog: DialogPtr;
  47.     itemHit: Integer;
  48.     i: Integer;
  49.     theEvent: EventRecord;
  50.     pt: Point;
  51.  
  52. Procedure Initialize;
  53.  
  54. VAR
  55.     theWorld:     SysEnvRec;
  56.     error:         OSErr;
  57.  
  58.     BEGIN
  59.     error := SysEnvirons(1, theWorld);
  60.     IF NOT theWorld.hasColorQD THEN 
  61.         BEGIN
  62.         SysBeep (50);
  63.         ExitToShell;                        
  64.         END;
  65.     InitGraf(@qd.thePort);
  66.     InitFonts;
  67.     InitWindows;
  68.     InitMenus;
  69.     TEInit;
  70.     InitDialogs(NIL);
  71.     InitCursor;
  72.     END;    { Initialize }
  73.  
  74.  
  75. function GetControlHandle (item: Integer): ControlHandle;
  76.  
  77. var
  78.     kind: Integer;
  79.     h: Handle;
  80.     r: Rect;
  81.     
  82.     begin
  83.     GetDialogItem(theDialog, item, kind, h, r);
  84.     if BAND(kind, $FC) = ctrlItem then
  85.         GetControlHandle := ControlHandle(h)
  86.     else
  87.         GetControlHandle := nil;
  88.     end;
  89.  
  90. Function FilterProc (dlg: DialogPtr; var evt: EventRecord; var itemHit: Integer): Boolean;
  91.     
  92.     begin
  93.     FilterProc := False;
  94.     theEvent := evt;
  95.     end;
  96.  
  97. procedure DrawFrame (theWindow: WindowPtr; itemNo: Integer);
  98.  
  99. var
  100.     itemType: Integer;
  101.     itemHandle: Handle;
  102.     itemRect: Rect;
  103.     
  104.     begin
  105.     PenNormal;
  106.     GetDialogItem(theWindow, itemNo, itemType, itemHandle, itemRect);
  107.     FrameRect(itemRect);
  108.     end;
  109.  
  110. procedure ReportControl (theDialog: DialogPtr; item: Integer);
  111.  
  112. var
  113.     aString: Str255;
  114.     value: Integer;
  115.     hiByte: Integer;
  116.     mString: Str255;
  117.     loByte: Integer;
  118.     iString: Str255;
  119.     itemKind: Integer;
  120.     itemHandle: Handle;
  121.     itemRect: Rect;
  122.     itemRgn: RgnHandle;
  123.     
  124.     begin
  125.     NumToString(item, aString);
  126.     value := GetControlValue(GetControlHandle(item));
  127.     hiByte := BSR(value, 8);
  128.     loByte := BAND(value, $FF);
  129.     NumToString(hiByte, mString);
  130.     NumToString(loByte, iString);
  131.     ParamText(aString, mString, iString, '');
  132.     GetDialogItem(theDialog, reportItem, itemKind, itemHandle, itemRect);
  133.     itemRgn := NewRgn;
  134.     RectRgn(itemRgn, itemRect);
  135.     UpdateDialog(theDialog, itemRgn);
  136.     DisposeRgn(itemRgn);
  137.     end;
  138.  
  139. procedure RecursiveGetMenu (menuH: MenuHandle);
  140.  
  141. var
  142.     i: Integer;
  143.     cmd, mark: Char;
  144.     
  145.     begin
  146.     if menuH <> nil then
  147.         begin
  148.         InsertMenu(menuH, -1);
  149.         for i := 1 to CountMItems(menuH) do
  150.             begin
  151.             GetItemMark(menuH, i, mark);
  152.             GetItemCmd(menuH, i, cmd);
  153.             if cmd = CHR($1B) then
  154.                 RecursiveGetMenu(GetMenu(ORD(mark)));
  155.             end;
  156.         end;
  157.     end;
  158.  
  159. function GetDPopUpMenuID (item: Integer): integer;
  160.     
  161.     begin
  162.     GetDPopUpMenuID := popupPrivateDataHdl(GetControlHandle(item)^^.contrlData)^^.mID;
  163.     end;
  164.  
  165. procedure GetDPopUpMenu (item: Integer);
  166.         
  167. var
  168.     menuID: Integer;
  169.     menuH: MenuHandle;
  170.     
  171.     begin
  172.     menuID := GetDPopUpMenuID(item);
  173.     menuH := GetMenu(menuID);
  174.     RecursiveGetMenu(menuH);
  175.     end;
  176.  
  177. Var
  178.     DrawFrame_UPP:     UserItemUPP;
  179.     FilterProc_UPP: ModalFilterUPP;
  180.     itemType:         Integer;
  181.     itemHandle:     Handle;
  182.     itemRect:         Rect;
  183.  
  184. begin
  185. initialize;
  186. DrawFrame_UPP := NewUserItemProc(@DrawFrame);
  187. FilterProc_UPP := NewModalFilterProc(@FilterProc);
  188. theDialog := GetNewDialog(dialogID, nil, POINTER(-1));
  189. SetPort(theDialog);
  190. GetDPopUpMenu(firstMenu);
  191. GetDPopUpMenu(secondMenu);
  192. GetDPopUpMenu(speedMenu);
  193. AppendMenu(GetMenu(GetDPopUpMenuID(speedMenu)), '123');
  194. AppendMenu(GetMenu(GetDPopUpMenuID(speedMenu)), '456');
  195. GetDPopUpMenu(ctlTitleMenu);
  196. GetDPopUpMenu(invisibleMenu);
  197. GetDPopUpMenu(emptyMenu);
  198. GetDialogItem(theDialog, frameitem, itemType, itemHandle, itemRect);
  199. SetDialogItem(theDialog, frameitem, itemType,Handle(Drawframe_UPP), itemRect);
  200. TextFont(geneva);    
  201. TextSize(9);
  202. ShowWindow(theDialog);
  203. for i := 1 to 3 do    
  204.     if EventAvail(everyEvent, theEvent) then ;
  205. with DialogPeek(theDialog)^.textH^^ do
  206.     begin
  207.     txFont := theDialog^.txFont;
  208.     txSize := theDialog^.txSize;
  209.     end;
  210. repeat
  211.     ModalDialog(FilterProc_UPP, itemHit);
  212.     case itemHit of
  213.         firstMenu, 
  214.         secondMenu, 
  215.         speedMenu, 
  216.         ctlTitleMenu, 
  217.         emptyMenu:     ReportControl(theDialog, itemHit);
  218.         
  219.         frameItem:    begin
  220.                     pt := theEvent.where;
  221.                     GlobalToLocal(pt);
  222.                     MoveControl(GetControlHandle(invisibleMenu), pt.h, pt.v);
  223.                     i := TrackControl(GetControlHandle(invisibleMenu), pt, POINTER(-1));
  224.                     ReportControl(theDialog, invisibleMenu);
  225.                     end;
  226.  
  227.         hideSecond:    begin
  228.                     SetControlValue(GetControlHandle(hideSecond), 1 - GetControlValue(GetControlHandle(hideSecond)));
  229.                     if GetControlValue(GetControlHandle(hideSecond)) = 1 then
  230.                         HideControl(GetControlHandle(secondMenu))
  231.                     else
  232.                         ShowControl(GetControlHandle(secondMenu));
  233.                     end;
  234.  
  235.         disableControl: 
  236.                     begin
  237.                     SetControlValue(GetControlHandle(disableControl), 1 - GetControlValue(GetControlHandle(disableControl)));
  238.                     if GetControlValue(GetControlHandle(disableControl)) = 1 then
  239.                         HiliteControl(GetControlHandle(secondMenu), 255)
  240.                     else
  241.                         HiliteControl(GetControlHandle(secondMenu), 0);
  242.     
  243.                     if GetControlValue(GetControlHandle(disableControl)) = 1 then
  244.                         HiliteControl(GetControlHandle(speedMenu), 255)
  245.                     else
  246.                         HiliteControl(GetControlHandle(speedMenu), 0);
  247.                     end;
  248.  
  249.         disableMenu:begin
  250.                     SetControlValue(GetControlHandle(disableMenu), 1 - GetControlValue(GetControlHandle(disableMenu)));
  251.                     if GetControlValue(GetControlHandle(disableMenu)) = 1 then
  252.                         DisableItem(GetMenu(GetDPopUpMenuID(secondMenu)), 0)
  253.                     else
  254.                         EnableItem(GetMenu(GetDPopUpMenuID(secondMenu)), 0);
  255.                     Draw1Control(GetControlHandle(secondMenu));    {Control manager has to be informed…}
  256.                     end;
  257.  
  258.         resetMenu:    begin
  259.                     SetControlValue(GetControlHandle(secondMenu), 3);
  260.                     ReportControl(theDialog, secondMenu);
  261.                     end;
  262.  
  263.         altCTitle:     begin
  264.                     SetControlValue(GetControlHandle(altCTitle), 1 - GetControlValue(GetControlHandle(altCTitle)));
  265.                     if GetControlValue(GetControlHandle(altCTitle)) = 0 then
  266.                         SetControlTitle(GetControlHandle(ctlTitleMenu), 'T1:')
  267.                     else
  268.                         SetControlTitle(GetControlHandle(ctlTitleMenu), 'Alt T2:');
  269.                     end;
  270.  
  271.         otherwise    ;
  272.         {CASE}        end;
  273. until ItemHit = OK;
  274. DisposeDialog(theDialog);
  275. end.