TweetFollow Us on Twitter

Search and Replace
Volume Number:5
Issue Number:5
Column Tag:Pascal Procedures

Related Info: Dialog Manager TextEdit Memory Manager

Modeless Search and Replace

By Mark McBride, Oxford, CA

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

Introduction

While working on development of a utility to speed integration of toolbox calls in MacFortran source code, I found myself looking for a search and replace routine. Perusal of Macintosh Revealed (Vol. 2) quickly led to the toolbox routine Munger (rhymes with plunger). A search of back issues of MacTutor also revealed the lack of a general discussion of Munger and doing find/replace (not even the generic multi-window text editor from the January 1987 issue). Thus, I decided to write a general purpose find/replace unit from a modeless dialog environment.

Basic Program Design

The find/replace unit and the accompanying example program are written in LS Pascal. Conversion to TML, Turbo, or MPW should be relatively straightforward. The find/replace unit is designed as a standalone unit that requires very little modification to the user’s program to add its capabilities. The modifications required include changing the main event loop to handle modeless dialog events, adding a new menu, and inserting calls to the find/replace unit in your program. When calling the find/replace routines the user passes the current event and a handle for the editText to be searched. The program contains a simple text edit window, a modeless Find dialog, and a modeless Replace Dialog. The program supports desk accessories; cut, copy, and paste within the program (but not with desk accessories); and should be screen and machine size independent. The program requires a 512Ke or later machine. If run on an old 512k machine, the program alerts the user and exits back to the Finder.

Simple Text Editor

The demonstration program uses the ROM85 library enhancements for the textedit window which allowed me to write a barebones editor sufficient for demonstration purposes. The text window can be resized, zoomed, and automatically scrolled. However, there are no scroll bars, thus movement to text outside the visible region must done with the arrow keys. Cut, copy, paste, and clear work properly. The text window contents cannot be loaded from disk, nor saved to disk. Upon startup (Figure 1), the text window contains Lincoln’s Gettysburg Address retrieved from the resource fork.

Figure 1.

Find/Replace Unit

The Find dialog allows the user to enter text characters and does an exact find. The search process can be initiated by clicking the ‘Find’ button or pressing the return key. The find routine works from the current selection point and does not wrap past the end of the textedit record. If found, the current selection is set to the string found and highlighted. If not found, the machine beeps. The find routine does not support wildcards nor non-printing characters (e.g., control characters).

The Search dialog allows the user to enter both the search and replace strings. Initially, the ‘Search’ button is highlighted as the default button. Once the search string has been found (and selected, the search routine is just the find routine), the ‘Replace’ button is highlighted. If the ‘Replace’ button is selected then the occurrence of the search string previously found is replaced. The replace string can be null but the find string must contain at least one character. When the replace operation is successfully completed, the ‘Search’ button returns to the default button. Although I have not included a ‘replace all’ button (which could easily be integrated), the user can continually press the return key searching and replacing. Dialog cut, copy, and paste from the text edit scrap is supported within both the Find and Replace dialogs.

Handling Modeless Dialogs

To properly handle the modeless find and replace dialogs four major aspects of modeless dialog event handling must be observed carefully: main event loop changes, window events, dialog events, and command keys. Each element is relatively straightforward, but the combination of the four can lead to some confusion and unnecessary coding.

Main Event Loop

When a program contains modeless dialogs, the toolbox routine IsDialogEvent should be called after calling GetNextEvent. Inside Mac (page I-416) warns that the program should call IsDialogEvent even if GetNextEvent returns false. When IsDialogEvent returns true the program should drop into its routine to handle events for the modeless dialog. Thus, the program should call GetNextEvent, then IsDialogEvent, and then start processing on the basis of the booleans returned by the two routines. If the program does a

{1}

if GetNextEvent(myEvent) then
 begin
 if IsDialogEvent(MyEvent) then
   begin
     Do_Modeless(MyEvent);
...

structure then null dialog events will not be passed to its modeless dialog routines. The alternative used by the program in the listing does the following:

{2}

EventFlag:=GetNextEvent(MyEvent);
if IsDialogEvent(MyEvent)then
  Do_Modeless(MyEvent)
else if EventFlag then
  begin
...

According to Inside Mac, IsDialogEvent returns true for:

• activate or update events for a dialog window

• mouse-down events in the content region of an active dialog window

• any other event when a dialog window is active.

However, the last statement is misleading.

Window Events

The structure region of a window is composed of two parts: the content region and the window frame (see Inside Mac, pages I-271,272). In the standard document window, the window frame contains the drag, go-away, and zoom regions (if zoom was desired). These regions do not overlap with the content region in the standard document window, although they could in a custom window definition. Thus, if your modeless dialog uses a standard document window (or a noGrowDocument proc) then you must be careful in handling events. A mouse-down event in the window frame of a modeless dialog will return false for IsDialogEvent. The main event loop must handle mouse-downs in the drag, go-away, and zoom boxes of a modeless dialog when using standard window types. This does not pose a particular problem since handling multiple windows for growing, dragging, and zooming is a standard Mac programming task.

Dialog Handler

When IsDialogEvent returns true, the user should pass control to a routine that handles the modeless dialog event. This routine should call the function

DialogSelect(theEvent,whichDialog,ItemHit):boolean.

DialogSelect will return a result of true with the dialog pointer in whichDialog and the item number in ItemHit when there is

• a mouse-down, key-down, or auto-key event in an enabled editText item. DialogSelect will take appropriate action within the editText item.

• a mouse-down event, then mouse released within an enabled control item. DialogSelect will call TrackControl while the mouse button is down.

• a mouse-down event in any other enabled item.

DialogSelect returns a result of false with the dialog pointer, whichDialog, undefined and ItemHit undefined when there is

• an activate or update event for the dialog window. DialogSelect will handle the redrawing of all items in the window (but will not handle redrawing of the default button).

• a mouse-down, key-down, or auto-key event in a disabled editText item.

• a key-down or auto-key event when no editText item exists.

• a mouse-down event in a control when the mouse is released outside the control.

• a mouse-down event in any other disabled item.

Thus, your modeless routine calls DialogSelect, letting the Dialog Manager handle actions while the user mouses around in the dialog. If the user indicates some action within an enabled dialog item, then the modeless routine takes appropriate action. If DialogSelect returns false then the modeless routine should do nothing. Two exceptions to handling returns of false from DialogSelect exist. First, if the modeless routine needs to take special actions for modeless update or activate events (e.g., redrawing the default button, showing selected text) then the routine must check for these types of events after DialogSelect returns false.

Second, DialogSelect does not check for the command key when handling key-down events. If some key-down events are special cases or if the command key equivalents for menu events (e.g., cut, copy, paste) are to be handled while the modeless dialog is the active window, then the modeless routine must check for key-down events prior to calling DialogSelect. The find/replace unit in the listing checks for edit menu command key equivalents, find/replace menu command key equivalents, and the return key as the default button.

Find/Replace Routines

Implementing a find/replace unit can take many alternative forms. Inside Mac does not specify user-interface guidelines for find/replace actions leading to many different solutions existing. The implementation given in the listing uses a single modeless dialog and follows a simple logic within three routines: Find, Search, and Replace. These routines provide the setup to access the toolbox routine Munger. Munger provides the core find and replace actions on the specified text. Switching between find and replace options alters the appearance and action of the modeless dialog.

Logic Used

When in the Find mode, the user can enter a text string (Figure 2). When the user clicks the find button (or presses the ‘return’ key) the editText record is searched forward from the current selection. If located, the find string is highlighted in the editText record. If the string is not in the current visible region, a call to TESelView scrolls the string into view (Figure 3). If the string is not found, then the machine beeps.

Figure 2.

Figure 3.

Figure 4.

When in the replace mode, the user must enter a find string while the replace string is optional (Figure 4). When the user clicks the find button (or presses ‘return’), starting at the current selection, the find routine tries to locate and highlight the find string. If found, the search routine changes the default button to ‘Replace’ (Figure 5). When the user clicks the replace button (or presses ‘return’) the find text is replaced with the replace text, leaving the current selection after the replacement text, and re-establishing the ‘Search’ button as the default button (Figure 6).

Figure 5.

Figure 6.

How It Works: Munger

The find, search, and replace routines serve to setup the proper information to send to and retrieve from the toolbox function Munger:

{3}

function Munger(h:handle; offset:longint; ptr1: ptr; len1: longint;
                          ptr2: ptr; len2: longint): longint.

The handle, h, specifies a handle to destination text generated as a relocatable block allocated by the memory manager. Offset refers to the starting location of the operation in the destination text and must be less than the length of the destination text. Ptr1, len1 refers to the target text and ptr2, len2 refers to the replacement text. The function value returned is negative if the operation failed and positive if the specified operation was successful. The behavior of Munger depends on the values passed. The six possible cases are:

Case 1: ptr1<>nil, len1>0; ptr2<>nil, len2>0: the destination text is searched from the offset specified to the end of the text, replacing the first occurrence of the target string found with the replacement text. The function returns the offset of the first byte past the replaced text.

Case 2: ptr1=nil, len1>0; ptr2<>nil, len2>0: the destination text from the specified offset to offset + len1 is replaced by the replacement text. The function returns the offset of the first byte past the replaced text.

Case 3: ptr1=nil, len1<0; ptr2<>nil, len2>0: the destination text from the specified offset to the end of the destination text is replaced by the replacement text. The function returns the offset of the first byte past the replaced text.

Case 4: len1=0; ptr2<>nil, len2>0: the replacement text is inserted into the destination text at the specified offset. The function returns the offset of the first byte past the replaced text.

Case 5: ptr1<>nil, len1>0; ptr2=nil: the destination text is searched for the target string. Munger returns the offset at the beginning of the found string.

Case 6: ptr1<>nil, len1>0; ptr2<>nil, len2=0: the target string is deleted from the destination text instead of replaced. The function returns the offset at the byte where the text was deleted.

The find routine uses Case 5 to perform the search. The find routine retrieves the find string from the dialog, checking for a target string of length greater than zero. The replace pointer is set to nil. If the current selection is not an insertion point, the offset is set to the end of the current selection for the find dialog. The target length is retrieved and the replace length is set to zero. Finally, Munger is called. If the target string is found, the current selection is set to the target and scrolled into view.

The search routine locates the target string using the find routine. If the find was successful, then the default button is set to the replace button and enabled. Once the target string is located, a user action to replace call the replace routine. The replace routine uses Case 1 for positive length replacement text and Case 6 for empty replacement text. The replace routine retrieves the target and replace text from the dialog edit fields, sets the offset to the current selection start of the destination text, and finally calls Munger. If Munger returns a positive value, the first occurrence of the target text was replaced. The editText is then recalibrated, the selection reset, and an invalrect call used to force an update of the editText window. Finally, the default button is set back to the Search button. If Munger returns a negative value, the replace failed and the machine beeps.

Concluding Remarks

The find/replace unit presented is simple in design, takes very little additional memory (approximately 5k), easily integrated into your application, and easily modified to suit your tastes. Most all the programming work is setting up the call to Munger and interpreting the results. The routine could also be changed to a modal dialog quickly. However, the modeless dialog environment seems more natural for a find/replace capability.

Listing:  EnvironIntf.p
{  File: EnvironsIntf.p}
{ Copyright Apple Computer, Inc. 1987}
{ All Rights Reserved}

UNIT EnvironsIntf;

INTERFACE
 CONST
 envMac = -1;    {returned by glue, if any}
 envXL = -2;{returned by glue, if any}
 envMachUnknown = 0;
 env512KE = 1;
 envMacPlus = 2;
 envSE = 3;
 envMacII = 4;
 envCPUUnknown = 0;{ CPU types }
 env68000 = 1;
 env68010 = 2;
 env68020 = 3;
 envUnknownKbd = 0;{ Keyboard types }
 envMacKbd = 1;
 envMacAndPad = 2;
 envMacPlusKbd = 3;
 envAExtendKbd = 4;
 envStandADBKbd = 5;
 { Errors }
 envNotPresent = -5500; { returned by glue. Official stuff}
 envBadSel = -5501;{ Selector non-positive }
 envSelTooBig = -5502;{ Selector bigger than call can take}

 TYPE
 SysEnvRec = RECORD
 environsVersion : INTEGER;
 machineType : INTEGER;
 systemVersion : INTEGER;
 processor : INTEGER;
 hasFPU : BOOLEAN;
 hasColorQD : BOOLEAN;
 keyBoardType : INTEGER;
 atDrvrVersNum : INTEGER;
 sysVRefNum : INTEGER;
 END;

 FUNCTION SysEnvirons (versionRequested : INTEGER;
 VAR theWorld : SysEnvRec) : OSErr;

IMPLEMENTATION
 FUNCTION SysEnvirons;
 External;
END.
Listing Globals.Pas:
UNIT SearchGlobs;

{File name:Globals.Pas}
{Function: Global Variables and Constants for Search Program}
{History: 7/02/88 MEM Initial Variables Added.   }
{                       }

INTERFACE

 USES
 ROM85;
 CONST
{window constants}
 minWidth = 80;
 minHeight = 80;
 mBarHeightGlob = $BAA;
 sBarWidth = 16;
 TxtMargin = 4;
 TxtWindID = 2;
 AboutID = 20;
 AlertID = 6;
 I_OK = 1;
 SpeechID = 100;
{Menu Resource ID’s}
 AppleID = 201;
 FileID = 202;
 EditID = 203;
 SearchID = 204;
{Menu Choice Constants}
 I_About = 1;
 I_Quit = 1;
 I_Undo = 1;
 I_Cut = 3;
 I_Copy = 4;
 I_Paste = 5;
 I_Clear = 6;
 I_Find = 1;
 I_Replace = 2;
 I_Show_Text = 4;
{Global Variables}
 VAR
{misc stuff}
 doneFlag : boolean;
 mBarHeight : integer;
{menu stuff}
 AppleMenu : MenuHandle;
 FileMenu : MenuHandle;
 EditMenu : MenuHandle;
 SearchMenu : MenuHandle;
{rectangles}
 DragRect : Rect;
 GrowRect : Rect;
 ScreenRect : Rect;
 TempRect : Rect;
{window stuff}
 MyWindow : WindowPtr;
{text edit stuff}
 hte : TEHandle;
{String stuff}
 theString : Str255;

IMPLEMENTATION
END.
Listing: Inits.Pas
UNIT Inits;
{File name:Init.Pas}
{Function: Initialization Routines for Search Program}
{History: 7/02/88 MEM All routines Added.   }

INTERFACE

 USES ROM85, EnvironsIntf, SearchGlobs;

 PROCEDURE InitMac;
 PROCEDURE CheckEnvirons;
 PROCEDURE InitTxtWind;
 PROCEDURE InitRects;
 PROCEDURE InitMenus;

IMPLEMENTATION
 PROCEDURE Crash;
 BEGIN
 ExitToShell;
 END;

 PROCEDURE InitMac;
 BEGIN
 MoreMasters;
 MoreMasters;
 MoreMasters;
 initCursor;
 FlushEvents(everyEvent, 0);
 doneFlag := false;
 END;

 PROCEDURE CheckEnvirons;
 VAR
 MyWorld : SysEnvRec;
 Version : integer;
 Err1, Err2 : OSErr;
 BEGIN
 Version := 1;
 Err1 := SysEnvirons(Version, MyWorld);
 IF (MyWorld.machineType < 1) THEN
 BEGIN
 ParamText(‘Program Requires Mac 512Ke or newer machine’, ‘’, ‘’, ‘’);
 Err2 := NoteAlert(AlertID, NIL);
 ExitToShell;
 END;
 END;

 PROCEDURE InitRects;
 VAR
 MemPtr : ^Integer;
 BEGIN
 MemPtr := pointer(MBarHeightGlob);
 mBarHeight := MemPtr^;
 screenrect := ScreenBits.Bounds;
 SetRect(DragRect, ScreenRect.left + 4, Screenrect.top + mBarHeight + 
4, Screenrect.right - 4, Screenrect.bottom - 4);
 SetRect(GrowRect, ScreenRect.left + minWidth, Screenrect.top + minHeight, 
Screenrect.right - 8, Screenrect.bottom - 8);
 END;

 PROCEDURE InitTxtWind;
 VAR
 ItemHdl : Handle;
 BEGIN
 MyWindow := GetNewWindow(TxtWindID, NIL, Pointer(-1));
 SelectWindow(MyWindow);
 SetPort(MyWindow);
 WITH MyWindow^.portRect DO
 SetRect(TempRect, 0, 0, right - sBarWidth - 1, bottom - sBarWidth - 
1);
 InsetRect(TempRect, TxtMargin, TxtMargin);
 hte := TENew(TempRect, TempRect);
 HLock(Handle(hte));
 hte^^.txFont := geneva;
 hte^^.fontAscent := 12;
 hte^^.lineHeight := 12 + 3 + 1; {ascent + descent + leading}
 HUnLock(Handle(hte));
 ItemHdl := GetResource(‘TEXT’, SpeechID);
 IF ItemHdl = NIL THEN
 BEGIN
 theString := ‘Search and Replace Demo program.  Use find and replace 
to change text in this window.’;
 TESetText(Pointer(Ord4(@theString) + 1), length(theString), hte);
 END
 ELSE
 BEGIN
 DetachResource(ItemHdl);
 SetHandleSize(hte^^.hText, SizeResource(ItemHdl));
 hte^^.hText := ItemHdl;
 hte^^.teLength := SizeResource(ItemHdl);
 ReleaseResource(ItemHdl);
 END;
 TESetSelect(0, 0, hte);
 TECalText(hte);
 TEAutoView(true, hte);
 TESelView(hte);
 TextFont(applFont);
 END;

 PROCEDURE InitMenus;
 VAR
 tempMenu : MenuHandle;
 BEGIN
 ClearMenuBar;
 tempMenu := GetMenu(AppleID);
 AddResMenu(tempMenu, ‘DRVR’);
 InsertMenu(tempMenu, 0);
 AppleMenu := tempMenu;
 tempMenu := GetMenu(FileID);
 InsertMenu(tempMenu, 0);
 FileMenu := tempMenu;
 tempMenu := GetMenu(EditID);
 InsertMenu(tempMenu, 0);
 EditMenu := tempMenu;
 tempMenu := GetMenu(SearchID);
 InsertMenu(tempMenu, 0);
 SearchMenu := tempMenu;
 DrawMenuBar;
 DisableItem(EditMenu, I_Undo);
 END;

END.
Listing: Utils.Pas

UNIT Utils;
{File name:Utils.Pas}
{Function: General Utility Routines for Search Program}
{History: 7/02/88 MEM Alert Error Routine Added.   }
{                       }

INTERFACE

 USES ROM85, SearchGlobs, Find_Dialog;

 PROCEDURE Do_About;
 PROCEDURE Do_Menu (theMenu, theItem : integer);
 PROCEDURE FrameDItem (dLog : DialogPtr; iNum : integer);
 PROCEDURE Update_Txt;
 PROCEDURE Act_Txt (theEvent : EventRecord);
 PROCEDURE FixText;
 PROCEDURE Grow_Txt (hSize, vSize : integer);
 PROCEDURE Do_Quit;

IMPLEMENTATION
 PROCEDURE Do_About;
 VAR
 oldPort : GrafPtr;
 dlg : DialogPtr;
 itemHit : integer;
 BEGIN
 GetPort(OldPort);
 dlg := GetNewDialog(AboutID, NIL, pointer(-1));
 FrameDItem(dlg, I_OK);
 ModalDialog(NIL, itemHit);
 DisposDialog(dlg);
 SetPort(OldPort);
 END;

 PROCEDURE Do_Menu;
 VAR
 DaNum : integer;
 Bool : boolean;
 DAName : Str255;
 oldPort : GrafPtr;
 BEGIN
 CASE theMenu OF
 AppleID : 
 BEGIN
 CASE theItem OF
 I_About : 
 BEGIN
 Do_About;
 END;
 OTHERWISE
 BEGIN
 GetPort(oldPort);
 GetItem(AppleMenu, theItem, DAName);
 DaNum := OpenDeskAcc(DAName);
 SetPort(oldPort);
 END;
 END;
 END;
 FIleID : 
 BEGIN
 CASE theItem OF
 I_Quit : 
 doneFlag := TRUE;
 OTHERWISE
 BEGIN
 END;
 END;
 END;
 EditID : 
 BEGIN
 IF NOT SystemEdit(theItem - 1) THEN
 BEGIN
 CASE theItem OF
 I_Cut : 
 TECut(hte);
 I_Copy : 
 TECopy(hte);
 I_Paste : 
 TEPaste(hte);
 I_Clear : 
 TEDelete(hte);
 OTHERWISE
 BEGIN
 END;
 END;
 END;
 END;
 SearchID : 
 BEGIN
 CASE theItem OF
 I_Find : 
 Open_Find(I_Find);
 I_Replace : 
 Open_Find(I_Replace);
 I_Show_Text : 
 BEGIN
 ShowWindow(MyWindow);
 SelectWindow(MyWindow);
 END;
 OTHERWISE
 BEGIN
 END;
 END;
 END;
 OTHERWISE
 BEGIN
 END;
 END;
 HiliteMenu(0);
 END;

 PROCEDURE FrameDItem;
 VAR
 iBox : Rect;
 iType : integer;
 iHandle : Handle;
 oldPenState : PenState;
 oldPort : GrafPtr;
 BEGIN
 GetPort(oldPort);
 SetPort(dlog);
 GetPenState(oldPenState);
 GetDItem(dLog, iNum, iType, iHandle, iBox);
 InsetRect(iBox, -4, -4);
 PenSize(3, 3);
 FrameRoundRect(iBox, 16, 16);
 SetPenState(oldPenState);
 SetPort(oldPort)
 END;

 PROCEDURE UpDate_Txt;
 VAR
 oldPort : GrafPtr;
 BEGIN
 GetPort(oldPort);
 SetPort(MyWindow);
 BeginUpDate(MyWindow);
 TEUpdate(hte^^.viewRect, hte);
 DrawGrowIcon(MyWindow);
 EndUpDate(MyWindow);
 SetPort(oldPort);
 END;

 PROCEDURE Act_Txt;
 BEGIN
 SetPort(MyWindow);
 IF Odd(theEvent.modifiers) THEN
 BEGIN
 TEActivate(hte);
 DrawGrowIcon(MyWindow);
 DisableItem(EditMenu, I_Undo);
 END
 ELSE
 BEGIN
 TEDeactivate(hte);
 DrawGrowIcon(MyWindow);
 EnableItem(EditMenu, I_Undo);
 END;
 END;

 PROCEDURE FixText;
 BEGIN
 HLock(Handle(hte));
 WITH hte^^ DO
 BEGIN
 viewRect := MyWindow^.portRect;
 WITH viewRect DO
 BEGIN
 right := right - sBarWidth - 1;
 bottom := bottom - sBarWidth - 1;
 bottom := (bottom DIV lineHeight) * lineHeight
 END;
 destRect := viewRect;
 InsetRect(destRect, TxtMargin, TxtMargin);
 TECalText(hte);
 TESelView(hte);
 END;
 HUnlock(Handle(hte));
 END;

 PROCEDURE Grow_Txt;
 BEGIN
 InvalRect(MyWindow^.portRect);
 EraseRect(MyWindow^.portRect);
 SizeWindow(MyWindow, hSize, vSize, true);
 InvalRect(MyWindow^.portRect);
 FixText;
 END;

 PROCEDURE Do_Quit;
 BEGIN
 TEDispose(hte);
 DisposeWindow(MyWindow);
 ExitToShell;
 END;
END.                                  {End of unit}
Listing:  Find.Pas
UNIT Find_Dialog;
{File name:Find.Pas   }
{Function: Handle a modeless dialog}
{History: 7/03/88 MEM - Made the Find dialog functional    }
INTERFACE

 USES ROM85;

 PROCEDURE Init_Find;
 PROCEDURE Act_Find;
 PROCEDURE Open_Find (Action : integer);
 PROCEDURE Do_Find (theEvent : EventRecord; hte : TEHandle);
 PROCEDURE Close_Find;

IMPLEMENTATION
 CONST
 FindID = 4;
 I_Find = 1;
 I_Replace = 2;
 I_Cancel = 3;
 I_Find_Label = 4;
 I_Find_Text = 5;
 I_Replace_Label = 6;
 I_Replace_Text = 7;
 Active = 0;
 InActive = 255;
 AlertID = 6;
 VAR
 MyDialog : DialogPtr;
 ExitDialog : Boolean;
 tempRect : Rect;
 DType : Integer;
 Index : Integer;
 DItem : Handle;
 CItem, CTempItem : controlhandle;
 itemHit : Integer;
 temp : Integer;
 Bolden : integer;
 Opened : integer;
 theString : Str255;

 PROCEDURE FrameItem (inum : integer);
 VAR
 iBox : Rect;
 iType : integer;
 iHandle : Handle;
 oldPenState : PenState;
 oldPort : GrafPtr;
 BEGIN
 GetPort(oldPort);
 SetPort(MyDialog);
 GetPenState(oldPenState);
 GetDItem(MyDialog, inum, iType, iHandle, iBox);
 InsetRect(iBox, -4, -4);
 PenSize(3, 3);
 FrameRoundRect(iBox, 16, 16);
 SetPenState(oldPenState);
 SetPort(oldPort)
 END;

 PROCEDURE DeFrameItem (inum : integer);
 VAR
 iBox : Rect;
 iType : integer;
 iHandle : Handle;
 oldPenState : PenState;
 oldPort : GrafPtr;
 BEGIN
 GetPort(oldPort);
 SetPort(MyDialog);
 GetPenState(oldPenState);
 GetDItem(MyDialog, inum, iType, iHandle, iBox);
 InsetRect(iBox, -4, -4);
 PenSize(3, 3);
 PenMode(PatBic);
 FrameRoundRect(iBox, 16, 16);
 SetPenState(oldPenState);
 SetPort(oldPort)
 END;

 PROCEDURE Init_Find;
 BEGIN
 MyDialog := NIL;
 END;

 PROCEDURE Act_Find;
 BEGIN
 IF (MyDialog <> NIL) THEN
 ShowWindow(MyDialog);
 END;

 PROCEDURE Open_Find;
 VAR
 ThisEditText : TEHandle;
 TheDialogPtr : DialogPeek;
 BEGIN
 IF (MyDialog = NIL) THEN
 BEGIN
 MyDialog := GetNewDialog(FindID, NIL, Pointer(-1));
 ShowWindow(MyDialog);
 SelectWindow(MyDialog);
 SetPort(MyDialog);
 TheDialogPtr := DialogPeek(MyDialog);
 ThisEditText := TheDialogPtr^.textH;
 HLock(Handle(ThisEditText));
 ThisEditText^^.txSize := 12;
 TextSize(12);
 ThisEditText^^.txFont := systemFont;
 TextFont(systemFont);
 ThisEditText^^.txFont := 0;
 ThisEditText^^.fontAscent := 12;
 ThisEditText^^.lineHeight := 12 + 3 + 1;
 HUnLock(Handle(ThisEditText));
 GetDItem(MyDialog, I_Replace_Text, DType, DItem, tempRect);
 SetIText(DItem, ‘’);
 SelIText(MyDialog, I_Replace_Text, 0, 0);
 GetDItem(MyDialog, I_Find_Text, DType, DItem, tempRect);
 SetIText(DItem, ‘’);
 SelIText(MyDialog, I_Find_Text, 0, 0);
 Opened := action;
 Bolden := I_Find;
 END
 ELSE
 BEGIN
 Opened := action;
 SelectWindow(MyDialog);
 END;
 IF (action = I_FInd) THEN
 BEGIN
 DeFrameItem(I_Replace);
 HideDItem(MyDialog, I_Replace);
 HideDItem(MyDialog, I_Replace_Label);
 HideDItem(MyDialog, I_Replace_Text);
 GetDItem(MyDialog, I_Find, DType, DItem, tempRect);
 GetIText(DItem, theString);
 SetCTitle(ControlHandle(DItem), ‘Find’);
 SetWTitle(MyDialog, ‘Find’);
 Bolden := I_Find;
 END
 ELSE
 BEGIN
 ShowDItem(MyDialog, I_Replace);
 SHowDItem(MyDialog, I_Replace_Label);
 SHowDItem(MyDialog, I_Replace_Text);
 GetDItem(MyDialog, I_Replace, DType, DItem, tempRect);
 HiLiteControl(ControlHandle(DItem), InActive);
 GetDItem(MyDialog, I_Find, DType, DItem, tempRect);
 SetCTitle(ControlHandle(DItem), ‘Search’);
 SetWTitle(MyDialog, ‘Replace’);
 END;
 DrawDialog(MyDialog);
 FrameItem(Bolden);
 ShowWindow(MyDialog);
 SelectWindow(MyDialog);
 END;

 PROCEDURE Close_Find;
 BEGIN
 IF (MyDialog <> NIL) THEN
 BEGIN
 DisposDialog(MyDialog);
 Opened := 0;
 MyDialog := NIL
 END;
 END;

 FUNCTION Find (hte : TEHandle) : boolean;
 VAR
 theFindString : Str255;
 StartAt, EndAt, targetlen, replacelen, FoundAt : longint;
 destText : Handle;
 targetPtr, replacePtr : Ptr;
 BEGIN
 Find := false;
 GetDItem(MyDialog, I_Find_Text, Dtype, DItem, tempRect);
 GetIText(DItem, theFindString);
 IF (theFindString = ‘’) THEN
 BEGIN
 ParamText(‘You have not specified anything to Find’, ‘’, ‘’, ‘’);
 temp := NoteAlert(AlertID, NIL);
 END
 ELSE
 BEGIN
 destText := Handle(hte^^.hText);
 targetPtr := pointer(Ord(@theFindString) + 1);
 replacePtr := NIL;
 IF (hte^^.selStart < hte^^.selEnd) THEN
 IF (opened = I_Find) THEN
 StartAt := hte^^.selEnd
 ELSE
 StartAt := hte^^.selStart
 ELSE
 StartAt := hte^^.selStart;
 targetlen := length(theFindString);
 replacelen := 0;
 FoundAt := Munger(destText, StartAt, targetPtr, targetlen, replacePtr, 
replacelen);
 IF (FoundAt >= 0) THEN
 BEGIN
 TESetSelect(FoundAt, FoundAt + targetlen, hte);
 TESelView(hte);
 TEActivate(hte);
 Find := true;
 END
 ELSE
 SysBeep(1);
 END;
 END;

 PROCEDURE Replace (hte : TEHandle);
 VAR
 theFindString, theReplString : Str255;
 StartAt, EndAt, targetlen, replacelen, FoundAt : longint;
 destText : Handle;
 targetPtr, replacePtr : Ptr;
 OldPort : GrafPtr;
 BEGIN
 GetDItem(MyDialog, I_Find_Text, Dtype, DItem, tempRect);
 GetIText(DItem, theFindString);
 GetDItem(MyDialog, I_Replace_Text, Dtype, DItem, tempRect);
 GetIText(DItem, theReplString);
 destText := Handle(hte^^.hText);
 targetPtr := pointer(Ord(@theFindString) + 1);
 targetlen := length(theFindString);
 replacePtr := pointer(Ord(@theReplString) + 1);
 replacelen := length(theReplString);
 StartAt := hte^^.selStart;
 FoundAt := Munger(destText, StartAt, targetPtr, targetlen, replacePtr, 
replacelen);
 IF (FoundAt >= 0) THEN
 BEGIN
 TECalText(hte);
 TESetSelect(FoundAt, FoundAt, hte);
 TESelView(hte);
 GetPort(OldPort);
 SetPort(hte^^.inPort);
 InvalRect(hte^^.viewRect);
 SetPort(OldPort);
 TEActivate(hte);
 GetDItem(MyDialog, I_Replace, DType, DItem, tempRect);
 HiLiteControl(ControlHandle(DItem), Inactive);
 DeFrameItem(I_Replace);
 FrameItem(I_Find);
 Bolden := I_Find;
 END
 ELSE
 BEGIN
 Sysbeep(1);
 GetDItem(MyDialog, I_Replace, DType, DItem, tempRect);
 HiLiteControl(ControlHandle(DItem), Inactive);
 IF (bolden = I_Replace) THEN
 DeFrameItem(I_Replace);
 FrameItem(I_Find);
 Bolden := I_Find;
 END;
 END;

 PROCEDURE Search (hte : TEHandle);
 VAR
 theReplString : Str255;
 success : boolean;
 BEGIN
 GetDItem(MyDialog, I_Replace_Text, Dtype, DItem, tempRect);
 GetIText(DItem, theReplString);
 success := Find(hte);
 IF success THEN
 BEGIN
 GetDItem(MyDialog, I_Replace, DType, DItem, tempRect);
 HiLiteControl(ControlHandle(DItem), Active);
 DeFrameItem(I_Find);
 FrameItem(I_Replace);
 Bolden := I_Replace;
 END;
 END;

 PROCEDURE Do_Find;
 CONST
 ReturnKey = 13;
 LABEL
 1;
 VAR
 Index : integer;
 myPt : Point;
 ExitDialog : boolean;
 CmdDown : boolean;
 success : boolean;
 chCode : integer;
 MyCmdKey : char;
 whichDialog : DialogPtr;
 BEGIN
 ExitDialog := false;
 IF (MyDialog <> NIL) THEN
 BEGIN
 IF (theEvent.what = KeyDown) THEN
 BEGIN
 CmdDown := odd(theEvent.modifiers DIV CmdKey);
 chCode := BitAnd(theEvent.message, CharCodeMask);
 MyCmdKey := CHR(chCode);
 IF CmdDown THEN
 BEGIN
 IF ((MyCmdKey = ‘x’) OR (MyCmdKey = ‘X’)) THEN
 DlgCut(MyDialog)
 ELSE IF ((MyCmdKey = ‘c’) OR (MyCmdKey = ‘C’)) THEN
 DlgCopy(MyDialog)
 ELSE IF ((MyCmdKey = ‘v’) OR (MyCmdKey = ‘V’)) THEN
 DlgPaste(MyDialog)
 ELSE IF ((MyCmdKey = ‘f’) OR (MyCmdKey = ‘F’)) THEN
 Open_Find(I_Find)
 ELSE IF ((MyCmdKey = ‘r’) OR (MyCmdKey = ‘R’)) THEN
 Open_Find(I_Replace);
 GOTO 1
 END
 ELSE IF (chCode = ReturnKey) THEN
 BEGIN
 IF (bolden = I_Find) THEN
 IF (Opened = I_Find) THEN
 success := Find(hte)
 ELSE
 Search(hte)
 ELSE
 Replace(hte);
 GOTO 1;
 END
 END;
 IF DialogSelect(theEvent, whichDialog, itemHit) THEN
 BEGIN
 GetDItem(whichDialog, itemHit, DType, DItem, tempRect);
 CItem := Pointer(DItem);
 IF (ItemHit = I_Cancel) THEN
 ExitDialog := true;
 IF (ItemHit = I_Find) THEN
 IF (Opened = I_Find) THEN
 success := Find(hte)
 ELSE
 Search(hte);
 IF (ItemHit = I_Replace) THEN
 Replace(hte);
 END
 ELSE IF (theEvent.what = UpDateEvt) THEN
 FrameItem(Bolden)
 ELSE IF (theEvent.what = ActivateEvt) AND (Opened = I_Find) AND Odd(theEvent.modifiers) 
THEN
 BEGIN
 GetDItem(MyDialog, I_Find, DType, DItem, tempRect);
 GetIText(DItem, theString);
 IF length(theString) > 0 THEN
 SelIText(MyDialog, I_Find_Text, 0, length(theString));
 END;
 END;
1 :
 IF ExitDialog THEN
 BEGIN
 Close_Find;
 END;
 END;

END.
Listing: Search.Pas
PROGRAM Searcher;
{Program name:Searcher.Pas   }
{This is the main program and event loop.  }
{History: 7/03/88 MEM Original Coding  }
{                       }
 USES
 ROM85, SearchGlobs, Inits, Find_Dialog, Utils;
 VAR
 theEvent : EventRecord;
 thecode : integer;
 theWindow : WindowPtr;
 thePeek : WindowPeek;
 OldPort : GrafPtr;
 menuResult : longint;
 newSize : longint;
 theMenu, theItem : integer;
 chCode : integer;
 ch : char;
 GlobalPt, localPt : Point;
 EventFlag : boolean;
 extend : boolean;
BEGIN
 InitMac;
 InitRects;
 InitMenus;
 InitTxtWind;
 Init_Find;
 REPEAT
 IF (hte <> NIL) THEN
 TEIdle(hte);
 SystemTask;
 EventFlag := GetNextEvent(everyEvent, theEvent);
 thecode := FindWindow(theEvent.where, theWindow);
 IF IsDialogEvent(theEvent) THEN
 Do_Find(theEvent, hte)
 ELSE IF EventFlag THEN
 BEGIN
 CASE theEvent.what OF
 MouseDown : 
 BEGIN
 IF (thecode = inMenuBar) THEN
 BEGIN
 menuResult := MenuSelect(theEvent.Where);
 theMenu := HiWord(menuResult);
 theItem := LoWord(menuResult);
 Do_Menu(theMenu, theItem);
 END;
 IF (thecode = InDrag) THEN
 DragWindow(theWindow, theEvent.where, DragRect);
 IF (thecode = inGrow) THEN
 BEGIN
 newSize := GrowWindow(theWindow, theEvent.where, GrowRect);
 IF newSize <> 0 THEN
 Grow_Txt(LoWord(newSize), HiWord(newSize))
 END;
 IF (thecode = inGoAway) THEN
 BEGIN
 IF TrackGoAway(theWindow, theEvent.where) THEN
 HideWindow(theWindow);
 END;
 IF ((thecode = inZoomIn) OR (thecode = inZoomOut)) THEN
 BEGIN
 IF TrackBox(thewindow, theEvent.where, theCode) THEN
 BEGIN
 GetPort(OldPort);
 SetPort(MyWindow);
 EraseRect(MyWindow^.portRect);
 ZoomWindow(Mywindow, thecode, TRUE);
 WITH MyWindow^.portRect DO
 Grow_Txt(right - left, bottom - top);
 SetPort(OldPort);
 END;
 END;
 IF (thecode = inContent) THEN
 BEGIN
 IF (theWindow <> FrontWindow) THEN
 SelectWindow(theWindow)
 ELSE
 BEGIN
 globalPt := theEvent.where;
 localPt := globalPt;
 GlobalToLocal(localPt);
 IF PtInRect(localPt, hte^^.viewRect) THEN
 BEGIN
 extend := (BitAnd(theEvent.modifiers, ShiftKey) <> 0);
 TEClick(localPt, extend, hte);
 END;
 END;
 END;
 IF (thecode = inSysWindow) THEN
 SystemClick(theEvent, theWindow);
 END;
 KeyDown, AutoKey : 
 BEGIN
 WITH theEvent DO
 BEGIN
 chCode := BitAnd(message, CharCodeMask);
 ch := CHR(chCode);
 IF (Odd(modifiers DIV CmdKey)) THEN
 BEGIN
 menuResult := MenuKey(ch);
 theMenu := HiWord(menuResult);
 theItem := LoWord(menuResult);
 IF (theMenu <> 0) THEN
 Do_Menu(theMenu, theItem)
 ELSE IF ((ch = ‘x’) OR (ch = ‘X’)) AND (hte <> NIL) THEN
 TECut(hte)
 ELSE IF ((ch = ‘c’) OR (ch = ‘C’)) AND (hte <> NIL) THEN
 TECopy(hte)
 ELSE IF ((ch = ‘v’) OR (ch = ‘V’)) AND (hte <> NIL) THEN
 TEPaste(hte);
 END
 ELSE IF (hte <> NIL) THEN
 TEKey(ch, hte);
 END;
 END;
 UpDateEvt : 
 BEGIN
 Update_Txt;
 END;
 ActivateEvt : 
 BEGIN
 Act_Txt(theEvent);
 END;
 OTHERWISE
 BEGIN
 END;
 END;
 END;
 UNTIL doneFlag;
 Do_Quit;
END.

 
AAPL
$106.98
Apple Inc.
-0.36
MSFT
$46.05
Microsoft Corpora
-0.57
GOOG
$550.31
Google Inc.
+0.98

MacTech Search:
Community Search:

Software Updates via MacUpdate

Cocktail 8.0.1 - General maintenance and...
Cocktail is a general purpose utility for OS X that lets you clean, repair and optimize your Mac. It is a powerful digital toolset that helps hundreds of thousands of Mac users around the world get... Read more
LibreOffice 4.3.3.2 - Free Open Source o...
LibreOffice is an office suite (word processor, spreadsheet, presentations, drawing tool) compatible with other major office suites. The Document Foundation is coordinating development and... Read more
VMware Fusion 7.0.1 - Run Windows apps a...
VMware Fusion allows you to create a Virtual Machine on your Mac and run Windows (including Windows 8.1) and Windows software on your Mac. Run your favorite Windows applications alongside Mac... Read more
OneNote 15.3.2 - Free digital notebook f...
OneNote is your very own digital notebook. With OneNote, you can capture that flash of genius, that moment of inspiration, or that list of errands that's too important to forget. Whether you're at... Read more
Audio Hijack Pro 2.11.4 - Record and enh...
Audio Hijack Pro drastically changes the way you use audio on your computer, giving you the freedom to listen to audio when you want and how you want. Record and enhance any audio with Audio Hijack... Read more
Iridient Developer 3.0.0 beta 3 - Powerf...
Iridient Developer (was RAW Developer) is a powerful image conversion application designed specifically for OS X. Iridient Developer gives advanced photographers total control over every aspect of... Read more
TextWrangler 4.5.11 - Free general purpo...
TextWrangler is the powerful general purpose text editor, and Unix and server administrator's tool. Oh, and also, like the best things in life, it's free. TextWrangler is the "little brother" to... Read more
NeoFinder 6.6 - Catalog your external me...
NeoFinder (formerly CDFinder) rapidly organizes your data, either on external or internal disks, or any other volumes. It catalogs all your data, so you stay in control of your data archive or disk... Read more
Chromium 38.0.2125.111 - Fast and stable...
Chromium is an open-source browser project that aims to build a safer, faster, and more stable way for all Internet users to experience the web. FreeSMUG-Free OpenSource Mac User Group build is... Read more
Default Folder X 4.6.11 - Enhances Open...
Default Folder X attaches a toolbar to the right side of the Open and Save dialogs in any OS X-native application. The toolbar gives you fast access to various folders and commands. You just click... Read more

Latest Forum Discussions

See All

Audio Defence : Zombie Arena (Games)
Audio Defence : Zombie Arena 1.0 Device: iOS Universal Category: Games Price: $4.99, Version: 1.0 (iTunes) Description: A zombie shooter audio game. Made from gut-wrenching 3D binaural sound, for a new kind of weird immersion. You... | Read more »
RPG Asdivine Hearts (Games)
RPG Asdivine Hearts 1.1.0 Device: iOS Universal Category: Games Price: $3.99, Version: 1.1.0 (iTunes) Description: SPECIAL PRICE50% OFF (USD 7.99 -> USD 3.99)!!! Travel alongside four companions and a cat in the adventure of a... | Read more »
Haunt the House: Terrortown (Games)
Haunt the House: Terrortown 1.0.1 Device: iOS Universal Category: Games Price: $.99, Version: 1.0.1 (iTunes) Description: 66.6% OFF! SPECIAL SPOOKY HALLOWEEN LAUNCH PRICE! 66.6% OFF! ...What was that sound? Is somebody there? | Read more »
SAS: Zombie Assault 4 Review
SAS: Zombie Assault 4 Review By Jennifer Allen on October 30th, 2014 Our Rating: :: FLAWED SHOOTERUniversal App - Designed for iPhone and iPad Shoot everything that moves in this fun, if flawed, twin-stick shooter.   | Read more »
Naailde the Witch Review
Naailde the Witch Review By Amy Solomon on October 30th, 2014 Our Rating: :: PITCH-PERFECT STORYTELLINGUniversal App - Designed for iPhone and iPad Marvelous storytelling, narration, and moving illustrations make this storybook... | Read more »
1st & Goal Review
1st & Goal Review By Andrew Fisher on October 30th, 2014 Our Rating: :: FOR THE D&D LOVING QBUniversal App - Designed for iPhone and iPad 1st & Goal is a board gamer’s football game, a football fan’s board game, and... | Read more »
French Developer Pated Unveils Seashine
French Developer Pated Unveils Seashine Posted by Ellis Spice on October 30th, 2014 [ permalink ] French one-man studio Pated has unveiled Seashine, “a poetic journey into the abyss.” Players take on the role of a jellyfish strugglin | Read more »
Agents of Storm: Tips, Tricks, and Strat...
Calling all agents: Would you like to see what we thought of this rather pretty base builder? Check out our Agents of Storm review! Have you downloaded Agents of Storm, been bowled over by the graphics, and aren’t quite sure what to do next? Never... | Read more »
Any.DO 2.0 Hopes to Help Manage Producti...
Any.DO 2.0 Hopes to Help Manage Productivity Posted by Ellis Spice on October 30th, 2014 [ permalink ] iPhone App - Designed for the iPhone, compatible with the iPad | Read more »
Base Busters Review
Base Busters Review By Jennifer Allen on October 30th, 2014 Our Rating: :: FUN BUT RESTRICTED MIXUniversal App - Designed for iPhone and iPad Mixing up two forms of tower defense gaming and collectible cards, Base Busters is a fun... | Read more »

Price Scanner via MacPrices.net

Apple Regains Momentum As Windows Stutters An...
The latest smartphone sales data from Kantar Worldpanel ComTech, for the three months to March 2014, shows Apple performing strongly in the first quarter of the year, with sales bouncing back in... Read more
Worldwide Smartphone Shipments Increase 25.2%...
New smartphone releases and an increased emphasis on emerging markets drove global smartphone shipments above 300 million units for the second consecutive quarter, according to preliminary data from... Read more
Apple now offering refurbished 2014 15-inch M...
The Apple Store is now offering Apple Certified Refurbished 2014 15″ Retina MacBook Pros for up to $400 off the cost of new models. An Apple one-year warranty is included with each model, and... Read more
Apple drops prices on refurbished 2013 Retina...
The Apple Store has dropped prices on 2013 Apple Certified Refurbished 13″ and 15″ Retina MacBook Pros, with Retina models now available starting at $999. Apple’s one-year warranty is standard, and... Read more
New 2.8GHz Mac mini on sale for $949, save $5...
Abt Electronics has the new 2.8GHz Mac mini in stock and on sale for $949.05 including free shipping. Their price is $50 off MSRP, and it’s the lowest price available for this model from any reseller... Read more
Sale! 3.7GHz Quad Core Mac Pro available for...
 B&H Photo has the 3.7GHz Quad Core Mac Pro on sale for $2649 including free shipping plus NY sales tax only. Their price is $350 off MSRP, and it’s the lowest price for this model from any... Read more
Mujjo Steps Up The Game With Refined Touchscr...
Netherlands based Mujjo have just launched their Refined Touchscreen Gloves, stepping up their game. The gloves feature a updated elegant design that takes these knitted gloves to the next level. A... Read more
Sale! Preorder the new 27-inch 5K iMac for $2...
 Abt Electronics has the new 27″ 3.5GHz 5K iMac on sale and available for preorder for $2374.05 including free shipping. Their price is $125 off MSRP, and it’s the lowest price available for this... Read more
Simplex Solutions Inc. Brings Secure Web Surf...
New York based Simplex Solutions Inc. has announced the release and immediate availability of Private Browser 1.0, its revolutionary new secure web browser developed for iPhone, iPad and iPod touch... Read more
Save up to $180 off MSRP with an Apple refurb...
The Apple Store has Apple Certified Refurbished 2014 MacBook Airs available for up to $180 off the cost of new models. An Apple one-year warranty is included with each MacBook, and shipping is free.... Read more

Jobs Board

Position Opening at *Apple* - Apple (United...
**Job Summary** Every day, business customers come to the Apple Store to discover what powerful, easy-to-use Apple products can do for them. As a Business Leader, Read more
Sr. Manager, *Apple* Deployment Programs fo...
**Job Summary** Apple is seeking candidates for a new position on the Education Content and Technology team. iPad and Mac is in the hands of millions of teachers and Read more
*Apple* Solutions Consultant (ASC) - Apple I...
…important role that the ASC serves is that of providing an excellent Apple Customer Experience. Responsibilities include: * Promoting Apple products and solutions Read more
*Apple* Solutions Consultant (ASC) - Apple I...
…important role that the ASC serves is that of providing an excellent Apple Customer Experience. Responsibilities include: * Promoting Apple products and solutions Read more
*Apple* Solutions Consultant (ASC) - Apple I...
…important role that the ASC serves is that of providing an excellent Apple Customer Experience. Responsibilities include: * Promoting Apple products and solutions Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.