TweetFollow Us on Twitter

Tear-Off
Volume Number:3
Issue Number:12
Column Tag:Pascal Procedures

Tear-Off Menus Explained

By Darryl Lovato, TML Systems, MacTutor Contributing Editor

Hello once again, this month I would like to show how to implement tear-off menus. What is a tear-off menu? It is a menu which can be removed from the menu bar and used as a window. I first saw a tear off menu in a pre-release version of Hypercard by Bill Atkinson. Since I have had a lot of experience creating and using Menu and Window Definition routines, I thought I would try to duplicate his implementation of tear-offs so that MacTutor readers can see how it is done and use them in their own programs.

I assume that you know how menu and window definition routines work. If you are not familiar with them read my articles in the July, August, and October 1986 issues of MacTutor.

The Window Definition routine used in the example is fairly straight forward, and, in fact, really isn’t needed. It’s sole purpose is to NOT look like a regular window. To do this I made the window’s title bar small and solid black. The pattern drawing is not a part of the definition routine. It is handled in a update event as any other window’s contents. The window looks like the one in figure 1.

Figure 1

The Menu Definition routine used in the example is where the special tear-menu code is. The tear-off menu in figure 2 looks very similar to the tear-off window. The size and draw messages are just as you would expect. The choose message, however, has some special coding to implement the tear-off. When we get the choose message, and the mouse is outside of the menu rectangle we check to see how far outside it is. If it is more than 10 pixels outide of the menu rectange, then we do not return from the choose message routine. Instead, we drag a outline of the menu on the desktop region until the mouse comes up, the user moves the mouse over another menu, or the mouse is moved back onto the original menu. If the mouse comes up while we are tracking it, we send a message back to the program telling it to move and re-display the torn window at that point. We communicate this information by posting an event which is unused by the application. I picked event #12, but you could choose any of the application event numbers. We pass the top left coordinate of where to display the pallete window in the Event.message paramter.

When the main event loop gets event 12, it must move the menu to the location passed in the message and display the window.

Figure #2

There are a couple of things that need to be pointed out. First, drawing all the patterns is real slow (even on a MacII!!!). This could be improved a great deal by keeping a offscreen bitmap of the patterns and use copybits instead of drawing them. Second. I know I have done a big no-no by not making the menu and window defs code resources. Third, you should be able to edit the patterns by double clicking on them to bring up a pattern editor window. Fourth, it would be great if the patterns could be in color on a Mac II. I leave the above enhancements to you as an exersize.

The program I used to create the tear-off windows is given below. It was written is TML Pascal, but the techniques shown should be usefull no matter what language your using. Have fun!

Pascal Code Optimizations

I would like to share some secrets with MacTutor readers out there who strive for the fastest, smallest Pascal code. My tricks will not improve your code 500%, as re-designing a algorithm could, but they can help you get the most out of the code generated by your compiler. These tricks are for TML Pascal, but should help you generate better code in any Pascal.

1. Always use Inc(x) and Dec(x) procedures instead of x := x + 1 or x := x - 1. The inc and dec procedures generate one assembler instruction as opposed to 3 for x := x + 1. This saves 6 bytes each time you use it. When inc and dec are used in loops, this can save a lot of execution time. [Unfortuanately, these are not available in other implementations. -Ed]

2. If you pass a structure larger than 4 bytes to a procedure or function, pass it as a static paramter. When you use a static, instead of regular parameters, you save 18 bytes per paramter in code size. This saves a lot of execution time, too. The reason for this is that when you pass a large structure as a static parameter, it is not copied local to the procedure. This is especially usefull if you pass a lot of strings to procedures and functions.

3. In certain if..then..else statements, you can save code by restructuring the statement to eliminate the else, and 4 bytes!

4. For loops are code hogs! Because Pascal compilers check the bounds of ‘for’ loops before entering them, they generate a lot of error checking. By recoding a ‘for’ loop as a repeat loop you can save 16 bytes. Here is an example:

Instead of this:

 for x := 1 to 100 do
   begin
   end;

Do this:

 x := 1;
 repeat
   inc(x);
 until x = 100;

There are a lot of other optimizations that can be done to make faster, smaller Pascal code, but these are the ones that can be used the most. Of course, re-writting the code in assembler would make the biggest diffrence in code size and speed, but that can take a long time and there are a lot of people who do not understand 680x0 assembler.

[The following code has been sanitized to work in both TML Pascal and LS Pascal and has been tested in both. However, one of the functions that returns a rect as an argument tripped up the Turbo Pascal syntax checker, so some modification may be necessary to get the program compiled in Turbo. We are at a loss as to why Turbo should be more picky than LS Pascal, which I thought was the most fussy of all! The code should also work fine in MPW, but we didn’t take the time to actually try it. -Ed]

We welcome Darryl back to MacTutor and present him with our Program of the Month award for this important and timely example of tear-off menus. One reader wanted it so bad, he made me send an advance copy of the article next day air so his department could get to work on it. Thanks Darryl!

{1}
Listing 1 TML & LS Pascal Source Code
PROGRAM TearMenu;

{ Example of tear-off menus}
{ by Darryl Lovato of TML for MacTutor}
{ Inspired by HyperCard}
{ TML Pascal / LS Pascal Version }
{ Some modifications may be required for Turbo Pascal }
{ Turbo Pascal does not seem to like a rect as }
{ a return variable from a function. }

uses MacIntf;    { remove for LS Pascal }

{$T APPL TEAR}   { remove for LS Pascal }
{$B+}   { remove for LS Pascal }
{$L TearMenuRes} { remove for LS Pascal }

{global constants}
CONST
 AppleMenuID = 1;
 FileMenuID = 2;
 EditMenuID = 3;
 graphicalMenu = 4;
 WindResID = 1;
 AboutID = 3000;

{global variables}
VAR
 myMenus : ARRAY[AppleMenuID..EditMenuID] OF MenuHandle;
 Done : Boolean;
 RegWDEFWindow : WindowPtr;
 GrowArea : rect;
 DragArea : rect;
 myWindowPeek : WindowPeek;
 MyGraphicsMenu : menuhandle;
 currentPatWind : WindowPtr;

{ My Window Definition Function }

 FUNCTION MyWindowDef (varCode : Integer;
 theWindow : WindowPtr;
 message : Integer;
 param : LongInt) : LongInt;
 TYPE
 RectPtr = ^Rect;

 VAR
 aRectPtr : RectPtr;
 myWindowPeek : WindowPeek;

{ Nested procedures follow }
 PROCEDURE DoDrawMessage (WindToDraw : WindowPtr;
 DrawParam : LongInt);
 VAR
 TitleBarRect : Rect;
 CurrentY : Integer;
 index : Integer;
 GoAwayBox : Rect;
 Show : boolean;
 WindowRec : WindowPeek;
 BEGIN
 WindowRec := WindowPeek(WindToDraw);
 Show := WindowRec^.visible;
 IF Show THEN
 BEGIN
 TitleBarRect := WindowRec^.strucRgn^^.rgnBBox;
 IF DrawParam <> 0 THEN {just toggle goAway box}
 BEGIN
 WITH TitleBarRect DO
 BEGIN
 top := top + 3;
 left := left + 5;
 bottom := top + 8;
 right := left + 8;
 END;
 InsetRect(TitleBarRect, 1, 1);
 InvertRect(TitleBarRect);
 END
 ELSE {we need to draw the window frame}
 BEGIN
 PenNormal;
 FrameRect(TitleBarRect);
 TitleBarRect.bottom := TitleBarRect.top + 13;

 FrameRect(TitleBarRect);
 InsetRect(TitleBarRect, 1, 1); {shrink by 1}
 EraseRect(TitleBarRect);

 IF WindowRec^.hilited THEN
 BEGIN { add hiliting }
 FillRect(TitleBarRect, black);
 WITH TitleBarRect DO
 BEGIN
 top := top + 2;
 left := left + 4;
 bottom := top + 8;
 right := left + 8;
 END;
 PenMode(patXor);
 FrameRect(TitleBarRect);
 PenNormal;
 END;
 END;
 END;
 END;

 FUNCTION DoHitMessage (WindToTest : WindowPtr;
 theParam : LongInt) : LongInt;
 VAR
 globalPt : Point;
 aRect : Rect;
 GoAwayBox : Rect;
 tempRect : Rect;
 WindowRec : WindowPeek;
 BEGIN
 globalPt.h := LoWord(theParam);
 globalPt.v := HiWord(theParam);
 WindowRec := WindowPeek(WindToTest);
 aRect := WindowRec^.strucRgn^^.rgnBBox;
 aRect.bottom := aRect.top + 12; {create tBar Rect}
 tempRect := WindowRec^.strucRgn^^.rgnBBox;
 IF PtInRect(globalPt, tempRect) THEN {in structure rgn?}
 BEGIN
 tempRect := WindowRec^.contRgn^^.rgnBBox;
 IF PtInRect(globalPt,tempRect) THEN {if content rgn}
 DoHitMessage := wInContent
 ELSE IF PtInRect(globalPt, aRect) THEN {drag}
 BEGIN
 IF WindowRec^.hilited THEN
 BEGIN {we need to check go-away box}
 WITH aRect DO
 BEGIN
 top := top + 2;
 left := left + 4;
 bottom := top + 8;
 right := left + 8;
 END;
 IF PtInRect(globalPt, aRect) THEN
 DoHitMessage := wInGoAway
 ELSE
 DoHitMessage := wInDrag;
 END
 ELSE
 DoHitMessage := wInDrag;
 END
 ELSE {it was in our window frame}
 DoHitMessage := wNoHit;
 END
 ELSE {it wasn’t in our window at all}
 DoHitMessage := wNoHit;
END;

PROCEDURE DoCalcRgnsMessage (WindToCalc : WindowPtr);
 VAR
 tempRect : Rect;
 aWindowPeek : WindowPeek;
 aRgn : RgnHandle;
 WindowRec : WindowPeek;
BEGIN
 tempRect := WindToCalc^.PortRect;
 OffsetRect(tempRect, -WindToCalc^.PortBits.Bounds.Left, -WindToCalc^.PortBits.Bounds.Top);

 TempRect.top := TempRect.top - 1;
 WindowRec := WindowPeek(WindToCalc);
 RectRgn(WindowRec^.contRgn, tempRect);

 InsetRect(tempRect, -1, -1);
 tempRect.top := tempRect.top - 12;
 RectRgn(WindowRec^.strucRgn, tempRect);
END;

{ End of nested procedures }
BEGIN
 MyWindowDef := 0;
 CASE message OF
 wDraw : 
 DoDrawMessage(theWindow, param);
 wHit : 
 MyWindowDef := DoHitMessage(theWindow, param);
 wCalcRgns : 
 DoCalcRgnsMessage(theWindow);
 wNew : 
 ;
 wDispose : 
 ;
 wGrow : 
 ;
 END; {of case}
END;

{ -------------------------------------------- }
{function GetItemRect(item : integer) : rect; }

FUNCTION GetItemRect (item : integer) : rect;
VAR
 tempRect : Rect;
BEGIN
 WITH tempRect DO
 BEGIN
 top := (((item - 1) DIV 8) * 16) - 1;
 bottom := top + 17;
 left := (((item - 1) MOD 8) * 16) - 1;
 right := left + 17;
 END;
 GetItemRect := tempRect;
END;

{procedure DrawPatWindow;   }

PROCEDURE DrawPatWindow;
 VAR
 i : integer;
 currentPat : Pattern;
 currRect : Rect;
BEGIN
 FOR i := 1 TO 96 DO
 BEGIN
 currRect := GetItemRect(i);
 FrameRect(currRect);
 GetIndPattern(currentPat, 100, i);
 FillRect(currRect, currentPat);
 FrameRect(currRect);
 END;
END;

{function GetMItemRect(whichRect:Integer;myRect:Rect):Rect;}

FUNCTION GetMItemRect (whichRect : Integer;
 myRect : Rect) : Rect;
 VAR
 ItemRect : Rect;
BEGIN
 ItemRect := GetItemRect(whichRect);
 OffSetRect(itemRect, myRect.left, myRect.top);
 GetMItemRect := ItemRect;
END;

{procedure drawItem(myRect : rect; myItem : integer); }

PROCEDURE drawItem (myRect : rect;
 myItem : integer);
 VAR
 currentPat : pattern;
BEGIN
 IF (myItem > 0) AND (myItem < 97) THEN
 BEGIN
 myRect := GetMItemRect(myItem, myRect);
 GetIndPattern(currentPat, 100, myItem);
 FillRect(myRect, currentPat);
 FrameRect(myRect);
 END;
END;

{procedure clearitem(myRect : Rect; lastCell : integer); }

PROCEDURE clearitem (myRect : Rect;
 lastCell : integer);
BEGIN
 DrawItem(myRect, lastCell - 9);
 DrawItem(myRect, lastCell - 8);
 DrawItem(myRect, lastCell - 7);
 DrawItem(myRect, lastCell - 1);
 DrawItem(myRect, lastCell);
 DrawItem(myRect, lastCell + 1);
 DrawItem(myRect, lastCell + 7);
 DrawItem(myRect, lastCell + 8);
 DrawItem(myRect, lastCell + 9);
END;

{Menu Definition Routine   }

PROCEDURE MyMenuDef (message : Integer;
 theMenu : MenuHandle;
 VAR menuRect : Rect;
 hitPt : Point;
 VAR whichItem : Integer);

{ Begin nested procedrues }
 PROCEDURE DoDrawMessage (myMenu : MenuHandle;
 myRect : Rect);
 CONST
 MBarHeight = 20;
 VAR
 whichRect : Integer;
 currentPat : Pattern;
 currRect : Rect;
 BEGIN
 FOR whichRect := 1 TO 96 DO
 Drawitem(myRect, whichRect);
 END;

 FUNCTION DoChooseMessage (myMenu : MenuHandle;
 myRect : Rect;
 myPoint : Point;
 oldItem : Integer) : Integer;
 VAR
 currRect : Rect;
 alldone : boolean;
 whichRect : Integer;
 oldRect : Rect;
 mPt : Point;
 lastPt : Point;
 lastRect : Rect;
 menuPt : Point;
 tempRect : Rect;
 exitrect : rect;
 saveClip : RgnHandle;
 io : integer;
 BEGIN
 ClipRect(myRect);
 whichRect := 1;
 alldone := false;
 REPEAT
 currRect := GetMItemRect(whichRect, myRect);
 IF PtInRect(myPoint, currRect) THEN
 alldone := true
 ELSE
 whichRect := whichRect + 1;
 UNTIL ((AllDone) OR (whichRect > 96));
 IF AllDone THEN { if we are in a item}
 BEGIN
 IF (whichRect <> oldItem) THEN
 BEGIN
 IF (oldItem <> 0) THEN
 ClearItem(myRect, oldItem);
 InsetRect(currRect, -6, -6);
 PenSize(6, 6);
 PenPat(white);
 FrameRect(currRect);
 PenNormal;
 InsetRect(currRect, -1, -1);
 FrameRect(currRect);
 END;
 DoChooseMessage := whichRect;
 END
 ELSE { we are not in a item}
 BEGIN
 IF oldItem <> 0 THEN { invert the old item}
 clearitem(myRect, oldItem);
 DoChooseMessage := 0;

 PenMode(notPatXOR);
 penpat(gray);
 exitrect := myrect;
 InsetRect(ExitRect, -10, -10);
 ExitRect.top := 20;
 menuPt.h := myRect.left + ((myRect.right - myRect.left) DIV 2);
 menuPt.v := myRect.top + ((myRect.bottom - myRect.top) DIV 2);
 SetRect(tempRect, 0, 0, 0, 0);
 lastRect := tempRect;
 ClipRect(screenbits.bounds);
 REPEAT
 GetMouse(mPt);
 LocalToGlobal(mPt);
 IF ((Longint(mpt) <> Longint(lastPt)) AND (NOT PtInRect(mpt, ExitRect)) 
AND (mPt.v > 20)) THEN
 BEGIN
 lastPt := mPt;
 tempRect := myRect;
 OffSetRect(tempRect, mPt.h - menuPt.h, mPt.v - menuPt.v);
 IF tempRect.top < 20 THEN
 BEGIN
 tempRect.top := 20;
 tempRect.bottom := 20 + 202;
 END;
 FrameRect(lastRect);
 FrameRect(tempRect);
 lastRect := tempRect;
 END;
 UNTIL (NOT button) OR ptInRect(mPt, exitrect) OR (mPt.v < 21);
 FrameRect(lastRect);
 PenNormal;
 IF (NOT PtInRect(mpt, ExitRect)) AND (mPt.v > 20) THEN
 BEGIN
 lastrect.top := lastrect.top + 12;
 io := PostEvent(12,Longint(lastRect.topleft));
        { this communicates back to the main event}
        { loop that a window was just torn from the}
        { menu.  We pass the new topLeft in the message}
 END;
 END;
 END;

 PROCEDURE DoSizeMessage (VAR myMenu : MenuHandle);
 BEGIN
 WITH myMenu^^ DO
 BEGIN
 menuWidth := 127;
 menuHeight := 191;
 END;
 END;

{ End of nested procedures }
BEGIN
 CASE message OF
 mSizeMsg : 
 DoSizeMessage(theMenu);
 mDrawMsg : 
 DoDrawMessage(theMenu, menuRect);
 mChooseMsg : 
 whichItem := DoChooseMessage(theMenu, menuRect, hitPt, whichItem);
 END;
END;

{ ---------------------------------- }
{ShowAbout procedure  }

PROCEDURE ShowAbout;
 VAR
 theDlog : DialogPtr;
 theItem : Integer;
BEGIN
 theDlog := GetNewDialog(AboutID, NIL, Pointer(-1));
 ModalDialog(NIL, theItem);
 DisposDialog(theDlog);
END;

{  ProcessMenu procedure   }

PROCEDURE ProcessMenu (codeWord : Longint);
 TYPE
 PatPtr = ^Pattern;
 VAR
 menuNum : Integer;
 itemNum : Integer;
 NameHolder : str255;
 dummy : Integer;
 yuck : boolean;
 myPattern : Pattern;
 DeskPatternPtr : PatPtr;
 savePort, aPort : grafPtr;
 theRgn1, theRgn2 : RgnHandle;
BEGIN
IF codeWord <> 0 THEN
 BEGIN
 menuNum := HiWord(codeWord);
 itemNum := LoWord(codeWord);
 CASE menuNum OF { the different menus}
 AppleMenuID : 
 IF itemNum < 3 THEN
 ShowAbout
 ELSE
 BEGIN
 GetItem(myMenus[AppleMenuID], itemNum, NameHolder);
 dummy := OpenDeskAcc(NameHolder);
 END;
 FileMenuID : 
 Done := true;
 EditMenuID : 
 yuck := SystemEdit(itemNum - 1);
 GraphicalMenu : 
 IF ItemNum <> 0 THEN
 BEGIN
 GetIndPattern(myPattern, 100, ItemNum);
 SetPort(currentPatWind);
 BackPat(myPattern);
 EraseRect(currentPatWind^.portRect);
 END;
 END;
 HiliteMenu(0);
 END;
END;

{Deal With Mouse Downs procedure }

 PROCEDURE DealWithMouseDowns (theEvent : EventRecord);
 VAR
 location : Integer;
 windowPointedTo : WindowPtr;
 mouseLoc : point;
 windowLoc : integer;
 VandH : Longint;
 Height : Integer;
 Width : Integer;
 currRect, myRect : Rect;
 newcell, LastCell : integer;
 thePt, LastPt : Point;
 i : integer;
 myPattern : Pattern;
 BEGIN
 mouseLoc := theEvent.where;
 windowLoc := FindWindow(mouseLoc, windowPointedTo);
 CASE windowLoc OF
 inMenuBar : 
 ProcessMenu(MenuSelect(mouseLoc));
 inSysWindow : 
 SystemClick(theEvent, windowPointedTo);
 inContent : 
 IF windowPointedTo <> FrontWindow THEN
 SelectWindow(windowPointedTo)
 ELSE
 BEGIN
 IF RegWDEFWindow = windowPointedTo THEN
 BEGIN
 SetPort(RegWDEFWindow);
 GetMouse(lastPt);
 newCell := 0;
 lastCell := 0;
 myRect := RegWDEFWindow^.portRect;
 WHILE waitmouseup DO {track in pattern wind}
 BEGIN
 GetMouse(thePt);
 IF NOT PtInRect(thePt, myRect) THEN
 BEGIN {we moved outside the window}
 IF lastCell <> 0 THEN
 clearItem(myRect, lastCell);
 lastCell := 0;
 END
 ELSE
 BEGIN
 FOR i := 1 TO 96 DO
 IF PtInRect(thePt, GetItemRect(i)) THEN
 newCell := i;
 IF newCell <> lastCell THEN
 BEGIN
 IF (lastCell <> 0) THEN
 Clearitem(myRect, lastCell);
 currRect := GetItemRect(newCell);
 InsetRect(currRect, -6, -6);
 PenSize(6, 6);
 PenPat(white);
 FrameRect(currRect);
 PenNormal;
 InsetRect(currRect, -1, -1);
 FrameRect(currRect);
 lastCell := newCell;
 END;
 END;
 END;
 Clearitem(myRect, lastCell);
 GetIndPattern(myPattern, 100, newCell);
 SetPort(currentPatWind);
 BackPat(myPattern);
 EraseRect(currentPatWind^.portRect);
 END;
 END;
 inDrag : 
 BEGIN
 DragWindow(windowPointedTo, mouseLoc, DragArea);
 SelectWindow(windowPointedTo);
 END;
 inGoAway : 
 IF TrackGoAway(windowPointedTo, mouseLoc) THEN
 HideWindow(windowPointedTo);
 END;
END;

{Deal With Key Downs procedure}

 PROCEDURE DealWithKeyDowns (theEvent : EventRecord);
 TYPE
 Trick = PACKED RECORD
 CASE boolean OF
 true:(
 long : Longint
 );
 false : (
 chr3, chr2, chr1, chr0 : char
 )
 END;
 VAR
 CharCode : char;
 TrickVar : Trick;
 BEGIN
 TrickVar.long := theEvent.message;
 CharCode := TrickVar.chr0;
 IF BitAnd(theEvent.modifiers, CmdKey) = CmdKey THEN           
 {check for a menu selection}
 ProcessMenu(MenuKey(CharCode))
 END;

{Deal With Updates procedure}

 PROCEDURE DealWithUpdates (theEvent : EventRecord);
 VAR
 UpDateWindow : WindowPtr;
 tempPort : WindowPtr;

 BEGIN
 UpDateWindow := WindowPtr(theEvent.message);
 GetPort(tempPort);
 SetPort(UpDateWindow);
 BeginUpDate(UpDateWindow);
 EraseRect(UpDateWindow^.portRect);
 IF UpdateWindow <> currentPatWind THEN
 DrawPatWindow;
 EndUpDate(UpDateWindow);
 SetPort(tempPort);
 END;

{MainEventLoop procedure  }

 PROCEDURE MainEventLoop;
 VAR
 Event : EventRecord;
 ProcessIt : boolean;
 where : Point;
 BEGIN
 REPEAT
 SystemTask;
 IF GetNextEvent(everyEvent, Event) THEN
 CASE Event.what OF
 mouseDown : 
 DealWithMouseDowns(Event);
 AutoKey : 
 DealWithKeyDowns(Event);
 KeyDown : 
 DealWithKeyDowns(Event);
 UpdateEvt : 
 DealWithUpdates(Event);
 12 : 
 BEGIN { we return this when a window torn}
 where := Point(Event.message);
 HideWindow(RegWDefWindow);
 MoveWindow(RegWDefWindow, where.h, where.v, true);
 ShowWindow(RegWDEFWindow);
 END;
 OTHERWISE
 BEGIN
 END;
 END; {of case}
 UNTIL Done;
 END;

{SetupMemory procedure      }

 PROCEDURE SetupMemory;
 VAR
 x : Longint;
 BEGIN
{x := ORD4(ApplicZone) + 128000;}
{SetApplLimit(Pointer(x));}
 MaxApplZone;
 MoreMasters;
 MoreMasters;
 MoreMasters;
 END;

{ SetupLimits    }

 PROCEDURE SetupLimits;
 VAR
 Screen : Rect;
 BEGIN
 Screen := ScreenBits.bounds;
 WITH Screen DO
 BEGIN
 SetRect(DragArea, left+4, top+24, right-4, bottom-4);
 SetRect(GrowArea, left, top + 24, right, bottom);
 END;
 END;

{MakeMenus procedure   }

 PROCEDURE MakeMenus;
 VAR
 index : Integer;
 BEGIN
 FOR index := AppleMenuID TO EditMenuID DO
 BEGIN
 myMenus[index] := GetMenu(index);
 InsertMenu(myMenus[index], 0);
 END;
 AddResMenu(myMenus[AppleMenuID], ‘DRVR’);
 MyGraphicsMenu := NewMenu(4, ‘Graphics’);
 MyGraphicsMenu^^.menuProc := NewHandle(0);
 MyGraphicsMenu^^.menuProc^ := Ptr(@MyMenuDef);
 CalcMenuSize(MyGraphicsMenu);
 Insertmenu(MyGraphicsMenu, 0);
 DrawMenuBar;
 END;

 PROCEDURE crash;
 BEGIN
 ExitToShell;
 END;

{ ---------------------------------- }
{Main Program Excecution Starts Here }

BEGIN
 InitGraf(@thePort);
 InitFonts;
 InitWindows;
 InitMenus;
 TEInit;
 InitDialogs(@crash);
 InitCursor;
 Done := false;
 FlushEvents(everyEvent, 0);

 SetupLimits;
 SetupMemory;
 MakeMenus;

 RegWDEFWindow := GetNewWindow(WindResID, NIL, Pointer(-1));
 myWindowPeek := WindowPeek(RegWDEFWindow);
 myWindowPeek^.windowDefProc := NewHandle(0);
 myWindowPeek^.windowDefProc^ := Ptr(@MyWindowDef);
 SetWRefCon(RegWDEFWIndow, Ord4(MyGraphicsMenu));
 currentPatWind := GetNewWindow(2, NIL, pointer(-1));
 SetPort(currentPatWind);
 BackPat(gray);
 EraseRect(currentPatWind^.portRect);

 MainEventLoop;
END. {thats all folkes!}
{2}
Listing 2: TML Link File (Ignore for LS Pascal )
!PAS$Xfer

/Globals -4
TearMenu
PAS$Library
macintf
MacIntfGlue
/Resources
TearMenuRes
/Bundle
/Type  APPL TEAR
/End


Listing 3: REZ version resource file

resource ‘BNDL’ (128) {
 ‘TEAR’,
 0,
 { /* array TypeArray: 2 elements */
 /* [1] */
 ‘ICN#’,
 { /* array IDArray: 1 elements */
 /* [1] */
 0, 128
 },
 /* [2] */
 ‘FREF’,
 { /* array IDArray: 1 elements */
 /* [1] */
 0, 128
 }
 }
};

resource ‘DITL’ (3000) {
 { /* array DITLarray: 3 elements */
 /* [1] */
 {155, 142, 180, 205},
 Button {
 enabled,
 “Okay”
 },
 /* [2] */
 {6, 5, 112, 361},
 StaticText {
 disabled,
 “                                 - Tear “
 “Menu -\n\nWritten by Darryl Lovato of TML “
 “Systems, Inc.\n\nThis program shows how to”
 “ make a window tear off of a menu.  Idea”
 “ from Hypercard by Bill Atkinson.\n”
 },
 /* [3] */
 {115, 5, 149, 361},
 StaticText {
 enabled,
 “Complete Pascal source code for this pro”
 “gram is available from MacTutor Magazine”
 “.”
 }
 }
};

resource ‘DLOG’ (3000, preload) {
 {58, 74, 244, 436},
 dBoxProc,
 visible,
 goAway,
 0x0,
 3000,
 “New Dialog”
};

resource ‘FREF’ (128) {
 ‘APPL’,
 0,
 “”
};
resource ‘ICN#’ (128, purgeable, preload) {
 { /* array: 2 elements */
 /* [1] */
 $”000F F400 007A FD00 01FF F780 05FF 7FE0"
 $”07FF FFB0 0BFB FFD8 0FFF B578 1FDF F6FC”
 $”1F7E E8BC 1E77 A074 3E37 807C 1C00 006C”
 $”3FF0 3E3C 2E38 633C 3DE0 3C24 1E60 4C24"
 $”1400 4004 1C00 0004 0C00 4008 0400 0038"
 $”0404 6028 0001 8038 0200 0078 0008 1050"
 $”0103 C060 0180 0140 0780 0040 7D20 0440"
 $”4510 0820 4887 E050 4980 0088 7FFF FFF8",
 /* [2] */
 $”000F F400 007F FD00 01FF FF80 05FF FFE0"
 $”07FF FFF0 0FFF FFF8 0FFF FFF8 1FFF FFFC”
 $”1FFF FFFC 1FFF FFFC 3FFF FFFC 1FFF FFFC”
 $”3FFF FFFC 3FFF FFFC 3FFF FFFC 1FFF FFFC”
 $”1FFF FFFC 1FFF FFFC 0FFF FFF8 07FF FFF8"
 $”07FF FFF8 03FF FFF8 03FF FFF8 01FF FFF0"
 $”01FF FFE0 01FF FFC0 07FF FFC0 7FFF FFC0"
 $”7FFF FFE0 7FFF FFF0 7FFF FFF8 7FFF FFF8"
 }
};
resource ‘MENU’ (1) {
 1,
 textMenuProc,
 0x7FFFFFFD,
 enabled,
 apple,
 { /* array: 2 elements */
 /* [1] */
 “About Tear Menu...”, noIcon, “”, “”, plain,
 /* [2] */
 “-”, noIcon, “”, “”, plain
 }
};
resource ‘MENU’ (2) {
 2,
 textMenuProc,
 0x7FFFFFF5,
 enabled,
 “File”,
 { /* array: 1 elements */
 /* [1] */
 “Quit”, noIcon, “Q”, “”, plain
 }
};
resource ‘MENU’ (3) {
 3,
 textMenuProc,
 0x7FFFFFFD,
 enabled,
 “Edit”,
 { /* array: 6 elements */
 /* [1] */
 “Undo”, noIcon, “Z”, “”, plain,
 /* [2] */
 “-”, noIcon, “”, “”, plain,
 /* [3] */
 “Cut”, noIcon, “X”, “”, plain,
 /* [4] */
 “Copy”, noIcon, “C”, “”, plain,
 /* [5] */
 “Paste”, noIcon, “V”, “”, plain,
 /* [6] */
 “Clear”, noIcon, “”, “”, plain
 }
};
resource ‘WIND’ (1) {
 {100, 100, 292, 228},
 documentProc,
 invisible,
 goAway,
 0x0,
 “”
};
resource ‘WIND’ (2) {
 {81, 143, 224, 327},
 documentProc,
 visible,
 goAway,
 0x0,
 “Current Pattern”
};
data ‘TEAR’ (0) {
 $”1B54 6561 7220 4D65 6E75 2C20 6279 2044" 
 $”6172 7279 6C20 4C6F 7661 746F” 
};
resource ‘PAT#’ (100, purgeable) {
 { /* array PatArray: 96 elements */
 /* [1] */
 $”FFFF FFFF FFFF FFFF”,
 /* [2] */
 $”DDFF 77FF DDFF 77FF”,
 /* [3] */
 $”DD77 DD77 DD77 DD77",
 /* [4] */
 $”AA55 AA55 AA55 AA55",
 /* [5] */
 $”55FF 55FF 55FF 55FF”,
 /* [6] */
 $”AAAA AAAA AAAA AAAA”,
 /* [7] */
 $”EEDD BB77 EEDD BB77",
 /* [8] */
 $”8888 8888 8888 8888",
 /* [9] */
 $”B130 031B D8C0 0C8D”,
 /* [10] */
 $”8010 0220 0108 4004",
 /* [11] */
 $”FF88 8888 FF88 8888",
 /* [12] */
 $”FF80 8080 FF08 0808",
 /* [13] */
 $”80",
 /* [14] */
 $”8040 2000 0204 08",
 /* [15] */
 $”8244 3944 8201 0101",
 /* [16] */
 $”F874 2247 8F17 2271",
 /* [17] */
 $”55A0 4040 550A 0404",
 /* [18] */
 $”2050 8888 8888 0502",
 /* [19] */
 $”BF00 BFBF B0B0 B0B0",
 /* [20] */
 $””,
 /* [21] */
 $”8000 0800 8000 08",
 /* [22] */
 $”8800 2200 8800 22",
 /* [23] */
 $”8822 8822 8822 8822",
 /* [24] */
 $”AA00 AA00 AA00 AA”,
 /* [25] */
 $”FF00 FF00 FF00 FF”,
 /* [26] */
 $”1122 4488 1122 4488",
 /* [27] */
 $”FF00 0000 FF”,
 /* [28] */
 $”0102 0408 1020 4080",
 /* [29] */
 $”AA00 8000 8800 80",
 /* [30] */
 $”FF80 8080 8080 8080",
 /* [31] */
 $”081C 22C1 8001 0204",
 /* [32] */
 $”8814 2241 8800 AA”,
 /* [33] */
 $”40A0 0000 040A”,
 /* [34] */
 $”0384 4830 0C02 0101",
 /* [35] */
 $”8080 413E 0808 14E3",
 /* [36] */
 $”1020 54AA FF02 0408",
 /* [37] */
 $”8000 0000 08",
 /* [38] */
 $”80",
 /* [39] */
 $”0002 4000 0000 0080",
 /* [40] */
 $”0002 4000 0000 0880",
 /* [41] */
 $”8000 1000 8002 0020",
 /* [42] */
 $”8002 0010 8002 0010",
 /* [43] */
 $”0092 0000 9200 0092",
 /* [44] */
 $”0092 0008 8220 0092",
 /* [45] */
 $”0092 0028 8228 0092",
 /* [46] */
 $”1082 1082 1082 1082",
 /* [47] */
 $”2288 2288 2288 2288",
 /* [48] */
 $”2288 22A8 2288 2288",
 /* [49] */
 $”2288 22A8 2288 2A88",
 /* [50] */
 $”228A 22A8 2288 2A88",
 /* [51] */
 $”228A 22A8 2288 AA88",
 /* [52] */
 $”228A A2A8 2288 AA88",
 /* [53] */
 $”228A AAA8 2288 AAA8",
 /* [54] */
 $”228A AAA8 228A AAA8",
 /* [55] */
 $”AA11 AA45 AA11 AA54",
 /* [56] */
 $”AA51 AA45 AA11 AA55",
 /* [57] */
 $”AA55 AA45 AA55 AA54",
 /* [58] */
 $”AA55 AA45 AA55 AA55",
 /* [59] */
 $”AA55 AA55 AA55 AA55",
 /* [60] */
 $”AA55 BA55 AA55 AA55",
 /* [61] */
 $”AA55 BA55 AA55 AB55",
 /* [62] */
 $”AA55 BA55 AA55 AB75",
 /* [63] */
 $”EE55 BA55 EA55 AB55",
 /* [64] */
 $”EE55 BA55 EE55 AB55",
 /* [65] */
 $”EE55 BA55 EE55 BB55",
 /* [66] */
 $”EE55 BB55 EE55 BB55",
 /* [67] */
 $”EE55 BB55 EE5D BB55",
 /* [68] */
 $”EE55 BB55 EE5D FB55",
 /* [69] */
 $”EE55 FB55 EE5D FB55",
 /* [70] */
 $”EE55 FF55 EE55 FF55",
 /* [71] */
 $”FF55 FF55 FF55 FF55",
 /* [72] */
 $”FF55 FF5D FF55 FF55",
 /* [73] */
 $”FF55 FF5D FF55 FFD5",
 /* [74] */
 $”FF55 FFDD FF55 FFD5",
 /* [75] */
 $”FF55 FFDD FF55 FFDD”,
 /* [76] */
 $”FF57 FF5D FF75 FFD5",
 /* [77] */
 $”FF57 FF7D FF7D FFD7",
 /* [78] */
 $”FFDD FFFF FFF7 FF7F”,
 /* [79] */
 $”FFDF FFFF FFF7 FF7F”,
 /* [80] */
 $”FFFF FFF7 FFFF FF7F”,
 /* [81] */
 $”FFFF FFFF FFFF FF7F”,
 /* [82] */
 $”FFFF FFFF FFFF FFFF”,
 /* [83] */
 $”7FBF DFBF 6BDD BD7B”,
 /* [84] */
 $”FF03 0509 1121 4181",
 /* [85] */
 $”FF83 4529 1129 4583",
 /* [86] */
 $”FF93 5539 FF39 5593",
 /* [87] */
 $”FE93 5539 EF39 5593",
 /* [88] */
 $”FE93 1139 EF39 1193",
 /* [89] */
 $”1010 1038 EF38 1010",
 /* [90] */
 $”1192 5438 EF38 5492",
 /* [91] */
 $”1192 5438 EF38 5492",
 /* [92] */
 $”55AA 55BA 6DBA 55AA”,
 /* [93] */
 $”5FA9 51A3 458A 55AA”,
 /* [94] */
 $”55EA 7DA6 65BE 57AA”,
 /* [95] */
 $”55AA 4182 4182 55AA”,
 /* [96] */
 $”46A6 FFFF 4686 57AE”
 }
 /** Extra bytes follow.. */
 /* $”0000 0034 0001 1400"
};
 
AAPL
$112.65
Apple Inc.
+3.24
MSFT
$47.52
Microsoft Corpora
+1.78
GOOG
$511.10
Google Inc.
+6.21

MacTech Search:
Community Search:

Software Updates via MacUpdate

Mellel 3.3.7 - Powerful word processor w...
Mellel is the leading word processor for OS X and has been widely considered the industry standard since its inception. Mellel focuses on writers and scholars for technical writing and multilingual... Read more
ScreenFlow 5.0.1 - Create screen recordi...
Save 10% with the exclusive MacUpdate coupon code: AFMacUpdate10 Buy now! ScreenFlow is powerful, easy-to-use screencasting software for the Mac. With ScreenFlow you can record the contents of your... Read more
Simon 4.0 - Monitor changes and crashes...
Simon monitors websites and alerts you of crashes and changes. Select pages to monitor, choose your alert options, and customize your settings. Simon does the rest. Keep a watchful eye on your... Read more
BBEdit 11.0.2 - Powerful text and HTML e...
BBEdit is the leading professional HTML and text editor for the Mac. Specifically crafted in response to the needs of Web authors and software developers, this award-winning product provides a... Read more
ExpanDrive 4.2.1 - Access cloud storage...
ExpanDrive builds cloud storage in every application, acts just like a USB drive plugged into your Mac. With ExpanDrive, you can securely access any remote file server directly from the Finder or... Read more
Adobe After Effects CC 2014 13.2 - Creat...
After Effects CC 2014 is available as part of Adobe Creative Cloud for as little as $19.99/month (or $9.99/month if you're a previous After Effects customer). After Effects CS6 is still available... Read more
Command-C 1.1.7 - Clipboard sharing tool...
Command-C is a revolutionary app which makes easy to share your clipboard between iOS and OS X using your local WiFi network, even if the app is not currently opened. Copy anything (text, pictures,... Read more
Tidy Up 4.0.2 - Find duplicate files and...
Tidy Up is a complete duplicate finder and disk-tidiness utility. With Tidy Up you can search for duplicate files and packages by the owner application, content, type, creator, extension, time... Read more
Typinator 6.3 - Speedy and reliable text...
Typinator turbo-charges your typing productivity. Type a little. Typinator does the rest. We've all faced projects that require repetitive typing tasks. With Typinator, you can store commonly used... Read more
GraphicConverter 9.5 - Graphics editor w...
GraphicConverter is an all-purpose image-editing program that can import 200 different graphic-based formats, edit the image, and export it to any of 80 available file formats. The high-end editing... Read more

Latest Forum Discussions

See All

Shift - Photo Filters Designed By You (...
Shift - Photo Filters Designed By You 1.0 Device: iOS Universal Category: Photography Price: $.99, Version: 1.0 (iTunes) Description: | Read more »
Elastic Drums (Music)
Elastic Drums 1.0 Device: iOS iPhone Category: Music Price: $3.99, Version: 1.0 (iTunes) Description: *** Introduction price 3,99$ instead of 7,99$ *** Elastic Drums is a music app with 6 channels of synthesized drum sounds, a step... | Read more »
Fireworks Simulator (Games)
Fireworks Simulator 1.0.8 Device: iOS Universal Category: Games Price: $.99, Version: 1.0.8 (iTunes) Description: *** 50% discount – For a short time only *** You can play Fireworks Simulator on these devices: - iPhone 5, 5s, 5c, 6,... | Read more »
Nicky's Gift (Games)
Nicky's Gift 1.0 Device: iOS Universal Category: Games Price: $.99, Version: 1.0 (iTunes) Description: Everybody! Merry Christmas! There's 48 levels in the game. Let's go! Nicky's Gift | Read more »
The Hit List — Simply Powerful Tasks, To...
The Hit List — Simply Powerful Tasks, To-Dos, Projects, & Reminders 2.0 Device: iOS iPhone Category: Productivity Price: $9.99, Version: 2.0 (iTunes) Description: >> LAUNCH SPECIAL: The Hit List 2 for iPhone is ONLY $9.99... | Read more »
Mahjong Journey Review
Mahjong Journey Review By Jennifer Allen on December 18th, 2014 Our Rating: :: STEADY MATCHINGiPad Only App - Designed for the iPad Aimed at the more laid back gamer, Mahjong Journey isn’t for everyone, but those looking for some... | Read more »
Emoji Type - custom keyboard with predic...
Emoji Type - custom keyboard with predictive emojis 0.4.0 Device: iOS iPhone Category: Utilities Price: $.99, Version: 0.4.0 (iTunes) Description: Emoji Type is custom keyboard for iOS 8 that auto suggests emojis as you type. ABOUT... | Read more »
Game of the Year 2014 – 148Apps Staff Pi...
The end of 2014 is almost here, which can only mean one thing. Okay it can mean a lot of things, but in this specific context it means Game of the Year lists! Which is why the 148Apps staff have all picked their favorites from the past year. And why... | Read more »
UponPixels Review
UponPixels Review By Jennifer Allen on December 18th, 2014 Our Rating: :: CREATIVE TYPOGRAPHYUniversal App - Designed for iPhone and iPad Add cool typography and objects to your photos with the easy to use UponPixels.   | Read more »
The Vikings are Coming! CastleStorm’s Ne...
The Vikings are Coming! CastleStorm’s New Update Adds a Survival Mode Posted by Jessica Fisher on December 18th, 2014 [ permalink ] | Read more »

Price Scanner via MacPrices.net

Save up to $400 on MacBooks with Apple Certif...
The Apple Store has Apple Certified Refurbished 2014 MacBook Pros and MacBook Airs available for up to $400 off the cost of new models. An Apple one-year warranty is included with each model, and... Read more
Save up to $300 on Macs, $30 on iPads with Ap...
Purchase a new Mac or iPad at The Apple Store for Education and take up to $300 off MSRP. All teachers, students, and staff of any educational institution qualify for the discount. Shipping is free,... Read more
iOS and Android OS Targeted by Man-in-the-Mid...
Cloud services security provider Akamai Technologies, Inc. has released, through the company’s Prolexic Security Engineering & Research Team (PLXsert), a new cybersecurity threat advisory. The... Read more
KMI MIDI K-Board Great Gift for Amateur &...
The K-Board is a MIDI Nano keyboard for music creation for iPad, Android, And computers; the easiest way to make music with iPads & Android tablets, and Mac, Windows, or Linux computers. Ultra-... Read more
Amazon offers 15-inch 2.2GHz Retina MacBook P...
 Amazon.com has the 15″ 2.2GHz Retina MacBook Pro on sale for $1699 including free shipping. Their price is $300 off MSRP. Stock is limited, so act now if you’re interested. Read more
Holiday sales continue: MacBook Pros for up t...
 B&H Photo has new MacBook Pros on sale for up to $300 off MSRP as part of their Holiday pricing. Shipping is free, and B&H charges NY sales tax only: - 15″ 2.2GHz Retina MacBook Pro: $1699... Read more
Holiday sale: Mac minis available for up to $...
 B&H Photo has new 2014 Mac minis on sale for up to $80 off MSRP. Shipping is free, and B&H charges NY sales tax only: - 1.4GHz Mac mini: $459 $40 off MSRP - 2.6GHz Mac mini: $629 $70 off... Read more
Google Search App For iOS Gets A Major Makeov...
Google has given iOS users an early Christmas present with a substantial update of it’s not-very-often-upgraded Google Search app. Google Search has been my go-to tool for Web searches since it was... Read more
ShopKeep Apple Pay And Chip Card Reader Avail...
ShopKeep, a cloud-based technology provider to more than 10,000 small business owners to manage retail shops and restaurants with iPads, has released its new Apple Pay and chip card reader. This... Read more
Holiday sale! 27-inch 5K iMac for $2299, save...
 B&H Photo has the 27″ 3.5GHz 5K iMac in stock today and on sale for $2299 including free shipping plus NY sales tax only. Their price is $200 off MSRP, and it’s the lowest price available for... Read more

Jobs Board

Project Manager, *Apple* Financial Services...
**Job Summary** Apple Financial Services (AFS) offers consumers, businesses and educational institutions ways to finance Apple purchases. We work with national and Read more
*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
*Apple* Retail - Multiple Positions (US) - A...
Job Description: Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, Read more
*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.