Aug 92 Tips
Volume Number: | | 8
|
Issue Number: | | 4
|
Column Tag: | | Tips & Tidbits
|
Tips & Tidbits ![](img001.gif)
By Neil Ticktin, Editor-in-Chief
MacTutor has a new column called Tips and Tidbits. This column is your opportunity to spread the word about little bits of information that you find out about. These tidbits can be programming related or they can be user tips that are particularly useful to programmers.
We really want to hear from you. MacTutor will pay $25 for every tip used, and the Tip of the Month will receive $50. To submit a tip, send in a letter to the magazine. Remember, AppleLink is our preferred method of communication, but feel free to send something via US Mail. If you do send mail, enclose a printed copy and a disk copy of the letter so that it does not have to be retyped.
Let us know what you think. Remember, this is your magazine. Try em out and let us know...
![](img002.gif)
Tip of the month
A frequently asked question is How to make a menu bar within a window, but makeit SIMPLE? I have found the answer deep in the UPMG folder on the 7.0 Beta-Bang CD-Rom. This folder is a must read for finding clever and simple answers to many questions of this kind. I have written the code below for better readability, but the credit for the trick must be given to the UPMG file. Before calling PopUpMenuSelect, install a MenuHook. In the hook, if the mouse is out of the menu rect (and out of the menu title rect), post a mouseUp event to exitPopUpMenuSelect.
Doing it this way, the user can browse between pop-up menus without releasing themouse button, exactly like in the real menu bar. Warning: abuse of this trick will quickly drive your program to a very disconcerting user-interface. Are you really sure you need a menu bar in thiswindow?
Benoît Widemann
/* 1 */
// Global variables
short gCurrentMenu;// current menu index
MenuHandle gMenu[NUMBER_OF_MENUS];
Rect gMenuTitleRect[NUMBER_OF_MENUS]; // menus title rects in window
// There was a click in window's menubar
// Returns the item selected or 0 if no selection
// If the returned item is non-zero, the menu index is gCurrentMenu
short windowMenuSelect(WindowPtr theWindow)
{
ProcPtr saveMenuHook;
Point p;
short selectedItem;
SetWRefCon(theWindow, SetCurrentA5()); // save our A5 in window refcon
saveMenuHook = MenuHook;// save current value in MenuHook
MenuHook = (ProcPtr)myMenuHook; // install our menu hook
SetPort(theWindow);
while (Button()){ // don't use StillDown here!!
GetMouse(&p);
for (gCurrentMenu=0; gCurrentMenu<NUMBER_OF_MENUS; gCurrentMenu++)
{
// find in which menu title rect is the mouse
if (PtInRect(p, &gMenuTitleRect[gCurrentMenu])) {
InvertRect(&gMenuTitleRect[gCurrentMenu]);
p.h = gMenuTitleRect[gCurrentMenu].left+1;
p.v = gMenuTitleRect[gCurrentMenu].bottom+1;
LocalToGlobal(&p);
// use only the item part returned
selectedItem = (short)(PopUpMenuSelect(gMenu[gCurrentMenu], p.v, p.h,
0) &
0x0000FFFF);
InvertRect(&gMenuTitleRect[gCurrentMenu]);
break;
}
}
}
MenuHook = saveMenuHook;// restore Menu Hook
return(selectedItem);
}
// the menu hook
pascal void myMenuHook()
{
Point p;
long sysA5;
sysA5 = SetA5(GetWRefCon(FrontWindow())); // restore our A5
GetMouse(&p);
if (p.v < 20 && !PtInRect(p, &gMenuTitleRect[gCurrentMenu])) {
(void)PostEvent(mouseUp, nil); // exit PopUpMenuSelect
}
(void)SetA5(sysA5); // restore system A5
}
NEED FOR SPEED
To execute system routines efficiently, you can by-pass the trap dispatcher byjumping to the routine yourself. Most practicle in tight loops. Take for example the BLockMove routine. Rewrite it as MyBlockMove:
/* 2 */
CONST
kBlockMoveNum = $A02E;
{Get the BlockMove Trap word from Inside Macintosh Volume III, Appendix
C}
VAR
blockMoveAddress:LongInt;
BEGIN
blockMoveAddress := GetTrapAddress(kBlockMoveNum);
start loop...
MyBlockMove(source,dest,size, blockMoveAddress);
end loop...
END
Write MyBlockMove in assembler like this:
;3
PROCEDURE MyBlockMove
MOVE.L A2,D2 /*Save off A2. We'll need it later
MOVE.L (A7)+,D1 /*Move Return address into D1
MOVEA.L (A7),A2 /*Put BlockMove address into A2
MOVE.L (A7)+,D0 /*Place size into D0
MOVEA.L (A7)+,A1 /*Place destination Ptr into A1
MOVEA.L (A7)+,A0 /*Place source Ptr into A0
MOVE.L D1,(A7) /*BlockMove trashes D1 & D2.
/*Save D1(Return address) on stack
MOVE.L D2,-(A7) /*Save D2(value of A2) on stack
JSR (A2) /*Jump to BlockMove
MOVEA.L (A7)+,A2 /*Restore A2
MOVEA.L (A7)+,A1 /*Place Return address into A1
JMP (A1) /*Jump to Return Address
MyBlockMove must be in a resident segment!!! For any questions, send an applelink to ISS.
Mark Hajek