home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-06-27 | 14.7 KB | 549 lines | [TEXT/PJMM] |
- unit NuDialogUtils;
-
- interface
-
- { Getting and setting the handle of any DITL }
- procedure SetDItemHdl (dItem: Integer; newHdl: Handle);
- function GetDItemHdl (dItem: Integer): Handle;
-
- {User item procs}
- procedure FrameItemProc (window: WindowPtr; item: Integer);
- procedure DottedFrameItemProc (window: WindowPtr; item: Integer);
- procedure BoldItemProc (window: WindowPtr; item: Integer);
- procedure ListRedrawProc (window: WindowPtr; list: ListHandle; item: Integer);
-
- {Utilities expect the port to be set to current dialog}
- function GetDItemFrame (item: Integer): Rect;
- procedure SetDItemFrame (item: Integer; newRect: Rect);
- function VisibleDItem (item: Integer): Boolean;
- procedure SetDItemProc (item: Integer; proc: ProcPtr);
- procedure SetDIconID (item: Integer; icon: Integer);
- procedure SetDItemText (item: Integer; theString: Str255);
- function GetDItemText (item: Integer): Str255;
- procedure SelectDEditText (item, selStart, selEnd: Integer);
- function NewDTextList (item, rows, columns: Integer; scrollHoriz, scrollVert: Boolean): ListHandle; {ideally, item height should be n*cell_height+2}
- function GetDControlHandle (item: Integer): ControlHandle;
- function GetDControlValue (item: Integer): Integer;
- procedure SetDControlValue (item, value: Integer);
- function GetDControlMin (item: Integer): Integer;
- procedure SetDControlMin (item, value: Integer);
- function GetDControlMax (item: Integer): Integer;
- procedure SetDControlMax (item, value: Integer);
- procedure RadioButtonDClick (first, last, item: Integer);
- function GetDRadioGroupValue (first, last: Integer): Integer; {zero-based, -1 if no RB pushed}
- procedure CheckboxDClick (item: Integer);
- function GetDControlEnable (item: Integer): Boolean;
- procedure SetDControlEnable (item: Integer; enable: Boolean);
- procedure RefreshDControl (item: Integer);
- function TestDControlChanged (item: Integer; saveLoc: Ptr): Boolean; {saveLoc points to integer — if nil, use high bytes of contrlRfcon}
- procedure UpdateParamText (theString: Str255; whichParam, whichItem: Integer);
-
- {Move a dialog to the proper position}
- procedure PositionDialog (dlg: DialogPtr);
- procedure PositionAlertTemplate (templateID: Integer);
-
- {Hilite a button to give feedback for key–equivalent hits}
- procedure FlashButton (item: Integer);
-
- {Do standard return/enter and command-period/escape key filtering}
- function OKCancelKeyFilter (dlg: DialogPtr; var evt: EventRecord; var itemHit: INTEGER; ignoreReturn, ignoreCancel: Boolean): BOOLEAN;
-
- {Hide/show entire groups of dialog items.}
- type
- DItemIDSet = set of 1..255;
- procedure ShowDItemSet (items: DItemIDSet);
- procedure HideDItemSet (items: DItemIDSet);
- function GetIBeamRegion (items: DItemIDSet): RgnHandle; {Make a region from rects of group of dialog items.}
-
- {Hide/show list box scroll bars.}
- procedure HideLBScrollBars (theLB: ListHandle);
- procedure ShowLBScrollBars (theLB: ListHandle);
-
- implementation
-
- uses
- Script;
-
- function GetDItemHdl (dItem: Integer): Handle;
- var
- dPtr: DialogPtr;
- dType: Integer;
- dHndl: Handle;
- dRect: Rect;
- begin
- GetPort(GrafPtr(dPtr));
- GetDItem(dPtr, dItem, dType, dHndl, dRect);
- GetDItemHdl := dHndl;
- end; { GetDItemHdl }
-
- procedure SetDItemHdl (dItem: Integer; newHdl: Handle);
- var
- dPtr: DialogPtr;
- dType: Integer;
- dHndl: Handle;
- dRect: Rect;
- begin
- GetPort(GrafPtr(dPtr));
- GetDItem(dPtr, dItem, dType, dHndl, dRect);
- SetDItem(dPtr, dItem, dType, newHdl, dRect);
- end; { GetDItemHdl }
-
- procedure FrameItemProc (window: WindowPtr; item: Integer);
- begin
- PenNormal;
- FrameRect(GetDItemFrame(item));
- end;
-
- procedure DottedFrameItemProc (window: WindowPtr; item: Integer);
- begin
- PenNormal;
- PenPat(gray);
- FrameRect(GetDItemFrame(item));
- PenPat(black);
- end;
-
- procedure BoldItemProc (window: WindowPtr; item: Integer);
- begin
- PenNormal;
- PenSize(3, 3);
- FrameRoundRect(GetDItemFrame(item), 16, 16);
- end;
-
- procedure ListRedrawProc (window: WindowPtr; list: ListHandle; item: Integer);
- var
- itemRect: Rect;
- begin
- itemRect := GetDItemFrame(item);
- if itemRect.left <= 8192 then {it hasn't been hidden}
- LUpdate(list^^.port^.visRgn, list);
- PenNormal;
- FrameRect(itemRect);
- end;
-
- function GetDItemFrame (item: Integer): Rect;
- var
- theDialog: DialogPtr;
- itemKind: Integer;
- itemHandle: Handle;
- itemRect: Rect;
- begin
- GetPort(GrafPtr(theDialog));
- GetDItem(theDialog, item, itemKind, itemHandle, itemRect);
- GetDItemFrame := itemRect;
- end;
-
- procedure SetDItemFrame (item: Integer; newRect: Rect);
- var
- theDialog: DialogPtr;
- itemKind: Integer;
- itemHandle: Handle;
- itemRect: Rect;
- begin
- GetPort(GrafPtr(theDialog));
- GetDItem(theDialog, item, itemKind, itemHandle, itemRect);
- SetDItem(theDialog, item, itemKind, itemHandle, newRect);
- end;
-
- function VisibleDItem (item: Integer): Boolean;
- begin
- VisibleDItem := GetDItemFrame(item).left > 8192;
- end;
-
- procedure SetDItemProc (item: Integer; proc: ProcPtr);
- var
- theDialog: DialogPtr;
- itemKind: Integer;
- itemHandle: Handle;
- itemRect: Rect;
- begin
- GetPort(GrafPtr(theDialog));
- GetDItem(theDialog, item, itemKind, itemHandle, itemRect);
- SetDItem(theDialog, item, itemKind, Handle(proc), itemRect);
- end;
-
- procedure SetDIconID (item: Integer; icon: Integer);
- var
- theDialog: DialogPtr;
- itemKind: Integer;
- itemHandle: Handle;
- itemRect: Rect;
- begin
- GetPort(GrafPtr(theDialog));
- GetDItem(theDialog, item, itemKind, itemHandle, itemRect);
- SetDItem(theDialog, item, itemKind, GetIcon(icon), itemRect);
- end;
-
- procedure SetDItemText (item: Integer; theString: Str255);
- var
- theDialog: DialogPtr;
- itemKind: Integer;
- itemHandle: Handle;
- itemRect: Rect;
- begin
- GetPort(GrafPtr(theDialog));
- GetDItem(theDialog, item, itemKind, itemHandle, itemRect);
- SetIText(itemHandle, theString);
- end;
-
- function GetDItemText (item: Integer): Str255;
- var
- theDialog: DialogPtr;
- itemKind: Integer;
- itemHandle: Handle;
- itemRect: Rect;
- theString: Str255;
- begin
- GetPort(GrafPtr(theDialog));
- GetDItem(theDialog, item, itemKind, itemHandle, itemRect);
- GetIText(itemHandle, theString);
- GetDItemText := theString;
- end;
-
- procedure SelectDEditText (item, selStart, selEnd: Integer);
- var
- theDialog: DialogPtr;
- begin
- GetPort(GrafPtr(theDialog));
- SelIText(theDialog, item, selStart, selEnd);
- end;
-
- function NewDTextList (item, rows, columns: Integer; scrollHoriz, scrollVert: Boolean): ListHandle;
- var
- theDialog: DialogPtr;
- itemKind: Integer;
- itemHandle: Handle;
- itemRect: Rect;
- dataBounds: Rect;
- cSize: Point;
- begin
- GetPort(GrafPtr(theDialog));
- GetDItem(theDialog, item, itemKind, itemHandle, itemRect);
- InsetRect(itemRect, 1, 1);
- if scrollVert then
- itemRect.right := itemRect.right - 15;
- if scrollHoriz then
- itemRect.bottom := itemRect.bottom - 15;
- cSize := Point(0);
- SetRect(dataBounds, 0, 0, columns, rows);
- NewDTextList := LNew(itemRect, dataBounds, cSize, 0, theDialog, True, False, scrollHoriz, scrollVert);
- end;
-
- function GetDControlHandle (item: Integer): ControlHandle;
- var
- theDialog: DialogPtr;
- kind: Integer;
- h: Handle;
- r: Rect;
- begin
- GetPort(GrafPtr(theDialog));
- GetDItem(theDialog, item, kind, h, r);
- if BAND(kind, $FC) = ctrlItem then
- GetDControlHandle := ControlHandle(h)
- else
- GetDControlHandle := nil;
- end;
-
- function GetDControlValue (item: Integer): Integer;
- begin
- GetDControlValue := GetCtlValue(GetDControlHandle(item));
- end;
-
- procedure SetDControlValue (item, value: Integer);
- begin
- SetCtlValue(GetDControlHandle(item), value);
- end;
-
- function GetDControlMin (item: Integer): Integer;
- begin
- GetDControlMin := GetCtlMin(GetDControlHandle(item));
- end;
-
- procedure SetDControlMin (item, value: Integer);
- begin
- SetCtlMin(GetDControlHandle(item), value);
- end;
-
- function GetDControlMax (item: Integer): Integer;
- begin
- GetDControlMax := GetCtlMax(GetDControlHandle(item));
- end;
-
- procedure SetDControlMax (item, value: Integer);
- begin
- SetCtlMax(GetDControlHandle(item), value);
- end;
-
- procedure RadioButtonDClick (first, last, item: Integer);
- var
- i: Integer;
- begin
- for i := first to last do
- SetDControlValue(i, ORD(i = item));
- end;
-
- function GetDRadioGroupValue (first, last: Integer): Integer;
- var
- i: Integer;
- begin
- GetDRadioGroupValue := -1;
- for i := first to last do
- if GetDControlValue(i) = 1 then
- begin
- GetDRadioGroupValue := i - first;
- Leave;
- end;
- end;
-
- procedure CheckboxDClick (item: Integer);
- var
- h: ControlHandle;
- begin
- h := GetDControlHandle(item);
- SetCtlValue(h, 1 - GetCtlValue(h));
- end;
-
- function GetDControlEnable (item: Integer): Boolean;
- var
- handle: ControlHandle;
- begin
- handle := GetDControlHandle(item);
- if handle <> nil then
- GetDControlEnable := handle^^.contrlHilite <> 255
- else
- GetDControlEnable := False;
- end;
-
- procedure SetDControlEnable (item: Integer; enable: Boolean);
- var
- hilite: Integer;
- begin
- if enable then
- hilite := 0
- else
- hilite := 255;
- HiliteControl(GetDControlHandle(item), hilite);
- end;
-
- procedure RefreshDControl (item: Integer);
- begin
- Draw1Control(GetDControlHandle(item));
- end;
-
- function TestDControlChanged (item: Integer; saveLoc: Ptr): Boolean;
- type
- IntPtr = ^Integer;
- var
- controlH: ControlHandle;
- changed: Boolean;
- begin
- controlH := GetDControlHandle(item);
- with controlH^^ do
- begin
- if saveLoc = nil then
- saveLoc := @contrlRfcon;
- changed := contrlValue <> IntPtr(saveLoc)^;
- IntPtr(saveLoc)^ := contrlValue;
- end;
- TestDControlChanged := changed;
- end;
-
-
- procedure UpdateParamText (theString: Str255; whichParam, whichItem: Integer);
- const
- DAStrings = $AA0;
- type
- ParamTextArray = array[0..3] of StringHandle;
- ParamTextArrayPtr = ^ParamTextArray;
- var
- s: array[0..3] of Str255;
- J: integer;
- theDialog: DialogPtr;
- itemKind: Integer;
- itemHandle: Handle;
- itemRect: Rect;
- begin {UpdateParamText}
- for J := 0 to 3 do
- if ParamTextArrayPtr(DAStrings)^[J] = nil then
- s[J] := ''
- else
- s[J] := ParamTextArrayPtr(DAStrings)^[J]^^;
- if s[whichParam] <> theString then
- begin
- s[whichParam] := theString;
- ParamText(s[0], s[1], s[2], s[3]);
- GetPort(GrafPtr(theDialog));
- GetDItem(theDialog, whichItem, itemKind, itemHandle, itemRect);
- InvalRect(itemRect); {let update event force the redrawing}
- end;
- end; {UpdateParamText}
-
-
- procedure PositionDialog (dlg: DialogPtr); {Revised to account for structure region.}
- var
- deskRect: Rect;
- vSpace, hSpace: Integer;
- vStruc, hStruc: Integer;
- vOffset, hOffset: Integer;
- begin
- deskRect := screenBits.bounds;
- with deskRect do
- begin
- top := top + GetMBarHeight;
- vSpace := bottom - top;
- hSpace := right - left;
- end;
- MoveWindow(dlg, -8192, -8192, False); {Move it way off screen.}
- ShowHide(dlg, true); {Get the regions built, without generating any activate events.}
- {Calculate the dialog’s entire structure size.}
- with WindowPeek(dlg)^.strucRgn^^.rgnBBox do
- begin
- vStruc := bottom - top;
- hStruc := right - left;
- end;
- {Compute the borders around the contents but within the structure. This is how much we need}
- {to offset the MoveWindow, which works relative to the contents, to account for the structure.}
- with WindowPeek(dlg)^.contRgn^^.rgnBBox do
- begin
- hOffset := (hStruc - (right - left)) div 2; {1/2 on right, 1/2 on left…}
- vOffset := vStruc - (bottom - top) - hOffset; {and 1/2 on bottom.}
- end;
- ShowHide(dlg, false); {Make it disappear, quietly.}
- {Now the available space is the screen less the structure.}
- vSpace := vSpace - vStruc;
- hSpace := hSpace - hStruc;
- {We can now “center” the window in a visually attractive manner.}
- with deskRect do
- MoveWindow(dlg, left + hOffset + (hSpace div 2), top + vOffset + (vSpace div 3), False);
- end;
-
- procedure PositionAlertTemplate (templateID: Integer);
- var
- theTemplate: AlertTHndl;
- deskRect: Rect;
- vSpace, hSpace: Integer;
- vOffset, hOffset: Integer;
- begin
- theTemplate := AlertTHndl(GetResource('ALRT', templateID));
- if theTemplate <> nil then
- begin
- deskRect := screenBits.bounds;
- with deskRect do
- begin
- top := top + GetMBarHeight;
- vSpace := bottom - top;
- hSpace := right - left;
- end;
- with theTemplate^^, boundsRect do
- begin
- vSpace := vSpace - bottom + top;
- hSpace := hSpace - right + left;
- OffsetRect(boundsRect, -left, -top); {normalize top left to zero zero}
- end;
- with deskRect do
- OffsetRect(theTemplate^^.boundsRect, left + hSpace div 2, top + vSpace div 3);
- end;
- end;
-
- procedure FlashButton (item: Integer);
- const
- hiliteTicks = 8;
- var
- button: ControlHandle;
- time: Longint;
- begin
- button := GetDControlHandle(item);
- HiliteControl(button, 1);
- Delay(hiliteTicks, time);
- HiliteControl(button, 0);
- end;
-
- {This depends upon the standard OK and cancel item assignments.}
- function OKCancelKeyFilter (dlg: DialogPtr; var evt: EventRecord; var itemHit: INTEGER; ignoreReturn, ignoreCancel: Boolean): BOOLEAN;
- var
- theCharCode: Integer;
- fakeItem: Integer;
- begin
- OKCancelKeyFilter := False;
- if evt.what = keyDown then
- begin
- theCharCode := BAND(evt.message, charCodeMask);
- if GetDControlEnable(OK) & ((theCharCode = $03) | ((theCharCode = $0D) & not ignoreReturn)) then
- fakeItem := OK
- else if GetDControlEnable(cancel) & not ignoreCancel & ((theCharCode = ORD('.')) & (BAND(evt.modifiers, cmdKey) <> 0) | (theCharCode = $1B)) then
- fakeItem := cancel
- else
- fakeItem := 0;
- if fakeItem > 0 then
- begin
- FlashButton(fakeItem);
- itemHit := fakeItem;
- OKCancelKeyFilter := True;
- end;
- end;
- end;
-
- procedure ShowDItemSet (items: DItemIDSet);
- var
- theDialog: DialogPtr;
- i: Integer;
- begin
- GetPort(GrafPtr(theDialog));
- for i := 1 to 255 do
- if i in items then
- ShowDItem(theDialog, i);
- end;
-
- procedure HideDItemSet (items: DItemIDSet);
- var
- theDialog: DialogPtr;
- i: Integer;
- begin
- GetPort(GrafPtr(theDialog));
- for i := 1 to 255 do
- if i in items then
- HideDItem(theDialog, i);
- end;
-
-
- function GetIBeamRegion (items: DItemIDSet): RgnHandle;
- var
- J: INTEGER;
- myRegion: RgnHandle;
- begin {GetIBeamRegion}
- myRegion := NewRgn;
- OpenRgn;
- for J := 1 to 255 do
- if J in items then
- FrameRect(GetDItemFrame(J));
- CloseRgn(myRegion);
- GetIBeamRegion := myRegion;
- end; {GetIBeamRegion}
-
-
- procedure HideLBScrollBars (theLB: ListHandle);
- begin
- HLock(Handle(theLB));
- with theLB^^ do
- begin
- if vScroll <> nil then
- HideControl(vScroll);
- if hScroll <> nil then
- HideControl(hScroll);
- end;
- HUnlock(Handle(theLB));
- end;
-
- procedure ShowLBScrollBars (theLB: ListHandle);
- begin
- HLock(Handle(theLB));
- with theLB^^ do
- begin
- if vScroll <> nil then
- ShowControl(vScroll);
- if hScroll <> nil then
- ShowControl(hScroll);
- end;
- HUnlock(Handle(theLB));
- end;
-
- end.