home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Pascal / Snippets / ColorIcosahedron 1.0 / Reusable units / MyFakeAlert.p next >
Encoding:
Text File  |  1995-10-24  |  7.0 KB  |  252 lines  |  [TEXT/PJMM]

  1. {    In-memory item list for dialog with four items:}
  2.  
  3. {    1    "^0^1^2^3" (static text)}
  4. {    2    Button 1}
  5. {    3    Button 2}
  6. {    4    Button 3}
  7.  
  8. {    The caller of FakeAlert passes the four strings that are to be}
  9. {    substituted into the first item, the number of buttons that}
  10. {    should be used, and the titles to put into each button.}
  11. {    A copy of the item list is hacked to use the right number of}
  12. {    buttons.}
  13.  
  14. {    Thanks to Erik Kilk and Jason Haines.  Some of the stuff to do}
  15. {    this is modified from code they wrote.}
  16.  
  17. {    Ported to LightSpeed Pascal 8 January 1987 Owen Hartnett    }
  18. {    Some modifications by Ingemar Ragnemalm 1993:}
  19. {    • Filter function for default button framing and cmd-period support.}
  20. {    • Adding parameters for the new functions}
  21. {    • Simpler calls for common usages.}
  22. {Change sept -95: Centers the dialog.}
  23.  
  24. unit MyFakeAlert;
  25.  
  26. interface
  27.  
  28. {Advanced interface:}
  29.     function MyFakeAlert (s1, s2, s3, s4: Str255; nButtons, defButton, cancelButton: integer; t1, t2, t3: Str255): integer;
  30. {Simple call for displaying a single string:}
  31.     procedure ReportStr (str: str255);
  32. {Simple call for a yes/no question:}
  33.     function QuestionStr (str: str255): boolean;
  34.  
  35. implementation
  36.  
  37.     var
  38.         itemList: array[0..32] of integer;
  39.  
  40.         savePort: GrafPtr;
  41.         theDialog: DialogPtr;
  42.         iListHandle: Handle;
  43.         bounds: Rect;
  44.         itemHit: integer;
  45.  
  46.         gDefButton, gCancelButton: integer;
  47.  
  48. {process return and command-period}
  49.     function Filter (theDialog: DialogPtr; var theEvent: EventRecord; var itemHit: integer): boolean;
  50.         var
  51.             theChar: Char;
  52.             kind: integer;
  53.             item: Handle;
  54.             box: Rect;
  55.     begin
  56.         if theEvent.what = keyDown then
  57.             begin
  58.                 theChar := Char(BitAnd(theEvent.message, charCodeMask));
  59.                 if BitAnd(theEvent.modifiers, cmdkey) <> 0 then
  60.                     if theChar = '.' then
  61.                         begin
  62.                             itemHit := gCancelButton + 1;
  63.  
  64.                             GetDItem(theDialog, gCancelButton + 1, kind, item, box);
  65.                             HiliteControl(ControlHandle(item), 1);
  66.  
  67.                             Filter := true;
  68.                             exit(Filter);
  69.                         end;
  70.                 if (theChar = char(13)) or (theChar = char(3)) then
  71.                     begin
  72.                         itemHit := gDefButton + 1;
  73.  
  74.                         GetDItem(theDialog, gDefButton + 1, kind, item, box);
  75.                         HiliteControl(ControlHandle(item), 1);
  76.  
  77.                         Filter := true;
  78.                         exit(Filter);
  79.                     end;
  80.             end;
  81.         Filter := false;
  82.     end;
  83.  
  84.  
  85.  
  86.     procedure InitItemList;
  87.  
  88. {This proc performs static initializations on ItemList    }
  89.  
  90.     begin
  91.         itemList[0] := 3;                    { max number of items - 1 }
  92.         itemList[1] := 0;                    {    statText item}
  93.                                         { reserve a long for item handle }
  94.         itemList[2] := 0;                    { display rectangle }
  95.         itemList[3] := 10; {top = 10}
  96.         itemList[4] := 27; {left = 27}
  97.         itemList[5] := 90; {bot = 61}
  98.         itemList[6] := 225; {right = 225}
  99.         itemList[7] := $8808;    { 8 + 128 = statText (disabled), title 8 bytes long }
  100.         itemList[8] := $5e30;    { ^0^1^2^3 }
  101.         itemList[9] := $5e31;
  102.         itemList[10] := $5e32;
  103.         itemList[11] := $5e33;
  104. {    first button}
  105.         itemList[12] := 0;                { reserve a long for item handle }
  106.         itemList[13] := 0;
  107.         itemList[14] := 104;                { display rectangle }
  108.         itemList[15] := 140;
  109.         itemList[16] := 124;
  110.         itemList[17] := 210;
  111.         itemList[18] := $400;        { 4 = pushButton, title is 0 bytes long}
  112. {    second button}
  113.         itemList[19] := 0;                { reserve a long for item handle }
  114.         itemList[20] := 0;
  115.         itemList[21] := 104;                { display rectangle }
  116.         itemList[22] := 30;
  117.         itemList[23] := 124;
  118.         itemList[24] := 100;
  119.         itemList[25] := $400;        { 4 = pushButton, title is 0 bytes long}
  120. {    third button}
  121.         itemList[26] := 0;                { reserve a long for item handle }
  122.         itemList[27] := 0;
  123.         itemList[28] := 72;                { display rectangle }
  124.         itemList[29] := 30;
  125.         itemList[30] := 92;
  126.         itemList[31] := 100;
  127.         itemList[32] := $400;        { 4 = pushButton, title is 0 bytes long}
  128.     end;
  129.  
  130. {    Set dialog button title and draw bold outline if makeBold true.}
  131. {    This must be done after the window is shown or else the bold}
  132. {    outline won't show up (which is probably the wrong way to do it).}
  133.  
  134.     procedure SetDControl (theDialog: DialogPtr; itemNo: integer; title: Str255; makeBold: Boolean);
  135.         var
  136.             itemHandle: Handle;
  137.             itemType: integer;
  138.             itemRect: Rect;
  139.             pState: PenState;
  140.  
  141.     begin
  142.         GetDItem(theDialog, itemNo, itemType, itemHandle, itemRect);
  143.         SetCTitle(ControlHandle(itemHandle), title);
  144.         if makeBold then
  145.             begin
  146.                 GetPenState(pState);
  147.                 PenNormal;
  148.                 PenSize(3, 3);
  149.                 InsetRect(itemRect, -4, -4);
  150.                 FrameRoundRect(itemRect, 16, 16);
  151.                 SetPenState(pState);
  152.             end;
  153.     end;
  154.  
  155. {    Fake an alert, using an in-memory window and item list.}
  156. {    The message to be presented is constructed from the first}
  157. {    four arguments.  nButtons is the number of buttons to use,}
  158. {    defButton is the default button, the next three args are}
  159. {    the titles to put into the buttons.  The return value is}
  160. {    the button number (1..nButtons).  This must be interpreted}
  161. {    by the caller, since the buttons may be given arbitrary}
  162. {    titles.}
  163.  
  164. {    nButtons should be between 1 and 3, inclusive.}
  165. {    defButton should be between 1 and nButtons, inclusive.}
  166.  
  167.     function MyFakeAlert;
  168.         var
  169.             savePort: GrafPtr;
  170.             theDialog: DialogPtr;
  171.             iListHandle: Handle;
  172.             bounds: Rect;
  173.             itemHit: integer;
  174.  
  175.         procedure FakeBarf;
  176.         begin
  177.             SysBeep(1);
  178.             exit(MyFakeAlert);
  179.         end;
  180.  
  181.     begin
  182.         gDefButton := defbutton;
  183.         gCancelButton := cancelbutton;
  184.  
  185.         InitItemList;
  186.         InitCursor;
  187.         GetPort(savePort);
  188.         iListHandle := NewHandle(longint(512));
  189.         if iListHandle = nil then
  190.             FakeBarf;
  191.         HLock(iListHandle);
  192.         itemList[0] := nButtons;                                { = number items - 1 }
  193.         BlockMove(@itemList[0], iListHandle^, longint(512));
  194.         SetRect(bounds, 115, 80, 355, 220);
  195.  
  196. {Center!}
  197.         OffsetRect(bounds, -bounds.left, -bounds.top);
  198.         OffsetRect(bounds, -(bounds.right - bounds.left) div 2 + (screenBits.Bounds.right - screenBits.bounds.left) div 2, 0);
  199.         OffsetRect(bounds, 0, -(bounds.bottom - bounds.top) div 2 + (screenBits.Bounds.bottom - screenBits.bounds.top - 20) div 2 + 20);
  200.  
  201.         theDialog := NewDialog(nil, bounds, '', false, dBoxProc, WindowPtr(-1), false, longint(0), iListHandle);
  202.         if theDialog = nil then
  203.             FakeBarf;
  204.         ParamText(s1, s2, s3, s4);                            { construct message }
  205.         SetPort(theDialog);
  206.         ShowWindow(theDialog);
  207.  
  208.         case nButtons of                { set button titles }
  209.             3: 
  210.                 begin
  211.                     SetDControl(theDialog, 4, t3, defButton = 3);
  212.                     SetDControl(theDialog, 3, t2, defButton = 2);
  213.                     SetDControl(theDialog, 2, t1, defButton = 1);
  214.                 end;
  215.             2: 
  216.                 begin
  217.                     SetDControl(theDialog, 3, t2, defButton = 2);
  218.                     SetDControl(theDialog, 2, t1, defButton = 1);
  219.                 end;
  220.             1: 
  221.                 SetDControl(theDialog, 2, t1, defButton = 1);
  222.         end;
  223.  
  224. {    ModalDialog returns 1 if return/enter hit, which, since}
  225. {    the statText item is first, can be unambiguously}
  226. {    interpreted as "choose default".}
  227.  
  228.         ModalDialog(@Filter, itemHit);
  229.         if itemHit = 1 then
  230.             itemHit := defButton
  231.         else
  232.             itemHit := itemHit - 1;
  233.         HUnlock(iListHandle);
  234.         DisposDialog(theDialog);
  235.         SetPort(savePort);
  236.         MyFakeAlert := itemHit;
  237.     end;
  238.  
  239. {Single text message:}
  240.     procedure ReportStr (str: str255);
  241.         var
  242.             itemHit: integer;
  243.     begin
  244.         itemHit := MyFakeAlert(str, '', '', '', 1, 1, 0, 'OK', '', '');
  245.     end;
  246. {A yes/no question:}
  247.     function QuestionStr (str: str255): boolean;
  248.     begin
  249.         QuestionStr := 1 = MyFakeAlert(str, '', '', '', 2, 1, 2, 'Yes', 'No', '');
  250.     end;
  251.  
  252. end.