home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Pascal / Snippets / TruchetTiles / T_DLOGUnit.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1994-08-09  |  7.8 KB  |  228 lines  |  [TEXT/PJMM]

  1. unit DlogStuff;        {this is a subset of my generic DlogStuff Unit}
  2. interface
  3.     uses
  4.         PrintTraps, Quickdraw, Picker, Palettes, globs;
  5.  
  6.     procedure DoMessage (mes0: str255; mes1: str255; mes2: str255; mes3: str255);
  7.  
  8.     procedure CenterDLOG (theDlog: DialogPtr);
  9.     procedure DrawDefaultBtn (theItem: integer; thisDlog: DialogPtr);
  10.     procedure ClickButton (Dptr: DialogPtr; ItemNo: integer);
  11.  
  12.     procedure PushRadioButton (theDlog: dialogPtr; item, first, last: integer);
  13.     procedure CheckABox (theDlog: dialogPtr; ItemNum: integer; HighLite: boolean);
  14.     procedure TrackScroll (theControl: ControlHandle; partCode: Integer);
  15.  
  16.     procedure WriteLabel (theStr: Str255; theRect: rect; toTheRight: boolean);
  17.  
  18.     function String2Int (theStr: str255): integer;
  19.     function Int2String (theInt: integer): str255;
  20.  
  21. implementation
  22.  
  23.  
  24. {=======================================================================================    }
  25. {This procedure  assumes that you have called New Dialog or GetNewDialog and}
  26. {the dialog is still invisible. Further, it assumes that the upper-left corner }
  27. {coordinates of the dialog are at 0,0, that the dialog's dimensions do not exceed}
  28. {that of the screen, and the there is a global value which delineates the height}
  29. {of the menu bar.    If all of the preceeding is true, this procedure will center}
  30. {the dialog horizontally, and 1/3 of the way down from the bottom of the menu}
  31. {bar vertically. Note that vertical positioning does not take into account any of}
  32. {a dialog's structure region; it only centers on the 'draw-able' portRect.}
  33.     procedure CenterDLOG; {(theDlog: DialogPtr)}
  34.         var
  35.             offSet: point;
  36.     begin
  37.         offSet.h := (ScreenBits.bounds.right - theDlog^.portRect.right) div 2;
  38.         offSet.v := gdh^^.MenuHeight + (ScreenBits.bounds.bottom - theDlog^.portRect.bottom) div 3;
  39.  
  40.         MoveWindow(theDlog, offSet.h, offSet.v, true);
  41.     end;
  42. {=======================================================================================    }
  43.     procedure DoMessage; {(mes0 : str255;mes1 : str255; mes2 : str255;mes3 : str255);}
  44.         const
  45.             MessageDialog = 258;
  46.         var
  47.             dialogP: DialogPtr;
  48.             item: integer;
  49.     begin
  50.         ParamText(mes0, mes1, mes2, mes3);
  51.         dialogP := GetNewDialog(MessageDialog, nil, pointer(-1));
  52.         if dialogP = nil then
  53.             begin
  54.                 SysBeep(5);
  55.                 ExitToShell;
  56.             end;
  57.         CenterDLOG(dialogP);
  58.         ShowWindow(dialogP);
  59.         InitCursor;
  60.         ModalDialog(nil, item);
  61.         DisposDialog(dialogP);
  62.     end; {DoMessage}
  63.  
  64.  
  65. {=======================================================================================    }
  66.     procedure DrawDefaultBtn; {(theItem : integer; thisDlog : DialogPtr);}
  67.         var
  68.             OptType: Integer;
  69.             OptBox: Rect;
  70.             ItemHdl: Handle;
  71.             oldDlog: DialogPtr;
  72.  
  73.     begin
  74.         GetPort(oldDlog);
  75.         SetPort(thisDlog);{ set window to current graf port }
  76. {Note: GetDItem gets info about dialogs}
  77.         GetDItem(thisDlog, theItem, OptType, ItemHdl, OptBox);  { get item location }
  78.         Pensize(3, 3);                                     { no wimpy outlines here }
  79.         InsetRect(OptBox, -4, -4);                 { set rectangle around button }
  80.         FrameRoundRect(OptBox, 16, 16);         { draw the sucker! }
  81.         PenSize(1, 1);                            { reset the PenSize}
  82.         SetPort(oldDlog);                            { RESET to the original port}
  83.     end; { of proc DrawDefaultBtn }
  84.  
  85. {=======================================================================================    }
  86.     procedure ClickButton; {(Dptr : DialogPtr; ItemNo : integer);}
  87. {    Inside Macintosh leaves out the fact that if you use a filter procedure        }
  88. {in the ModalDialog call you need to simulate a clicking of the OK button when    }
  89. {the return key is hit.  This one of two possible techniques where we directly    }
  90. {highlight and unhighlight the button.  The other technique would be to add a     }
  91. {mouse down event to the event queue in which the mouse coordinates are         }
  92. {somewhere inside of the OK button.  JWIND}
  93.  
  94.         var
  95.             IType: integer;
  96.             ButtonHandle: Handle;
  97.             Box: rect;
  98.             L: LongInt;
  99.  
  100.     begin
  101.         GetDItem(Dptr, ItemNo, IType, ButtonHandle, Box);
  102.         HiliteControl(ControlHandle(ButtonHandle), 253);
  103.         Delay(8, L);
  104.         HiliteControl(ControlHandle(ButtonHandle), 0);
  105.     end; { ClickButton }
  106.  
  107. {=======================================================================================    }
  108.     procedure PushRadioButton; {(theDlog : dialogPtr; item, first, last : integer)}
  109.  
  110.         var
  111.             index: integer;        {index through the loop}
  112.             itemtype: integer;        {the dialog items type}
  113.             itemhandle: handle;        {the dialog items handle}
  114.             itemrect: rect;        {the dialog items rect}
  115.             itemcntlhand: controlhandle; {we convert the items handle to a cntl handle}
  116.  
  117.     begin
  118.         for index := first to last do {do it for all items in the group}
  119.             begin
  120.                 GetDItem(theDlog, index, itemtype, itemhandle, itemrect); {get the handle}
  121.                 itemcntlhand := controlhandle(itemhandle); {convert it to a cntl handle}
  122.                 if (index = item) then
  123.                     begin
  124.                         SetCtlValue(itemcntlhand, 1); {hilite the control}
  125.                     end
  126.                 else
  127.                     SetCtlValue(itemcntlhand, 0); {unlilite the control}
  128.             end;
  129.     end;
  130.  
  131. {=======================================================================================    }
  132.     procedure CheckABox;{(theDlog:dialogPtr; ItemNum : integer;HighLite : boolean);}
  133.         var
  134.             itemtype: integer;                {the dialog items type}
  135.             itemhandle: handle;                {the dialog items handle}
  136.             itemrect: rect;                    {the dialog items rect}
  137.             itemcntlhand: controlhandle;    {we convert the items handle to a cntl handle}
  138.  
  139.     begin
  140.         GetDItem(theDlog, ItemNum, itemtype, itemhandle, itemrect); {get the handle}
  141.         itemcntlhand := controlhandle(itemhandle); {convert it to a cntl handle}
  142.         if HighLite then
  143.             begin
  144.                 SetCtlValue(itemcntlhand, 1); {hilite the control}
  145.             end
  146.         else
  147.             begin
  148.                 SetCtlValue(itemcntlhand, 0); {unlilite the control}
  149.             end;
  150.     end;
  151.  
  152. {=======================================================================================    }
  153.     procedure TrackScroll; {(theControl: ControlHandle; partCode: Integer)}
  154.         var
  155.             min, max, amount, startValue: Integer;
  156.             up: Boolean;
  157.     begin
  158.         up := partcode in [inUpButton, inPageUp];
  159.         min := GetCtlMin(theControl);
  160.         max := GetCtlMax(theControl);
  161.         startValue := GetCtlValue(theControl);
  162.         if ((up and (startValue > min)) or ((not up) and (startValue < max))) and (partCode <> 0) then
  163.             begin
  164.                 if up then
  165.                     amount := -1
  166.                 else
  167.                     amount := 1;
  168.                 if partCode in [inPageUp, inPagedown] then
  169.                     amount := round(amount * 2)
  170.                 else
  171.                     amount := round(amount * 1);
  172.                 SetCtlValue(theControl, amount + startValue);
  173.             end;
  174.     end; {of TrackScroll}
  175.  
  176. {=======================================================================================    }
  177. {write a label, to the right or left of a rectangle}
  178.     procedure WriteLabel; {(theStr: Str255; theRect: rect; toTheRight: boolean);}
  179.         const
  180.             vertOff = 4;
  181.             horizOff = 6;
  182.         var
  183.             aHandle: handle;
  184.             aPt: point;
  185.     begin
  186.         with theRect do
  187.             if ToTheRight then
  188.                 begin
  189.                     SetPt(aPt, right + horizOff, bottom - vertOff);
  190.                     left := right + 1;
  191.                     right := right + (StringWidth(theStr) + horizOff + 8);
  192.                 end
  193.             else    {it's to the left}
  194.                 begin
  195.                     SetPt(aPt, left - (horizOff + StringWidth(theStr)), bottom - vertOff);
  196.                     right := left - 4;
  197.                     left := left - (StringWidth(theStr) + horizOff + 8);
  198.                 end;{if ToTheRight…}
  199.         EraseRect(theRect);
  200.         MoveTo(aPt.h, aPt.v);
  201.         WriteDraw(theStr);
  202.     end;{WriteLabel}
  203.  
  204. {=======================================================================================    }
  205.     function String2Int;{(theStr) : integer;}
  206.         var
  207.             aLongInt: longint;
  208.     begin
  209.         StringToNum(theStr, aLongInt);
  210.         if aLongInt > maxInt then
  211.             begin
  212.                 DoMessage('That number is too big.', 'It must be less than 32767', '', '');
  213.                 aLongInt := 0;
  214.             end;
  215.         String2Int := aLongInt;
  216.     end;{String2Int}
  217.  
  218. {=======================================================================================    }
  219.     function Int2String; {(theInt) : str255;}
  220.         var
  221.             aLongInt: longint;
  222.             aStr: str255;
  223.     begin
  224.         aLongInt := theInt;
  225.         NumToString(aLongInt, aStr);
  226.         Int2String := aStr;
  227.     end;{String2Int}
  228. end.    {DlogStuff unit}