Sprocket Menus 3
Volume Number: | | 11
|
Issue Number: | | 7
|
Column Tag: | | Getting Started
|
Sprocket Menus, Part 3
By Dave Mark, MacTech Magazine Regular Contributing Author
Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.
Last month, we added a new class to our Drag Manager friendly picture window program. In addition to the Drag Manager friendly text window, we also added a pair of menus to the program. The Text menu appeared when the frontmost window was a text window, and the Picture window appeared when the frontmost window was a picture window.
We learned about static data members and member functions, and added the data member fgMenu to both the TTextWindow and TPictureWindow classes. fgMenu contains the MenuHandle for either the Text or Picture window. We also added a static member function named SetUpStaticMenu() to both classes. SetUpStaticMenu() registers the commands associated with its classes menu (go back a few columns if you dont know about menu commands and CMNU resources). Since fgMenu is static, it is global to the class, and not to a specific object. That means we dont need to create a new Text menu for each TTextWindow object we create. By having a single menu per class, we can register the menu commands at initialization, then leave the commands registered as we delete the menu from the menu bar and reinsert it, depending on the type of the frontmost window.
Static members are important. Well be adding more of them in this months column, so before you read on, be sure you have this concept down.
When you ran last months program, the Text menu was incomplete. It featured three items, Font, Size, and Style, each of which was a hierarchical menu without a corresponding submenu.
This month, well add the three submenus, then add the code to bring the Size and Style submenus to life. As youll see, this is no trivial task. Lets get started.
Sprocket Resources
Well base this months program on the Sprocket and SprocketStarter code we worked with last month. If you are downloading the code, look for folders labeled Sprocket.02/01/95 and SprocketPicText.03/25/95. Duplicate the SprocketPicText.03/25/95 folder and name the copy SprocketPicText.04/25/95. Since we wont be making changes to Sprocket (except for a minor bug fix), theres no need to duplicate the Sprocket.02/01/95 folder.
Launch your favorite resource editor and open the file SprocketStarter.rsrc inside the SprocketPicText.04/25/95 folder, inside the SprocketStarter subfolder. Youre going to create two new CMNU resources, one each for the Size and Style submenus.
Why not a CMNU for the Font submenu? A CMNU resource give syou a way to assign a command to each of your menus items. Since the contents of a Font menu cant be predicted in advance, a CMNU doesnt really make sense. Well base the Font submenu on the Font Menu resource that already exists in StandardMenus.rsrc inside the Sprocket.02/01/95 folder.
Figure 1. The Size CMNU resource. Notice that all the sizes are in outline font.
Figure 1 shows the CMNU resource for the Size submenu. When you create this resource, be sure to change the resource id to 132 in both places (select Edit Menu & MDEF ID... from the MENU menu and Get Resource Info from the Resource menu). Change the style of each of the six size items to outline (choose Outline from the Style menu). Be sure to assign a command number to each of the 9 non-separator line items. Figure 2 shows my list of items and command numbers:
Figure 2. The command numbers for the items in the Size CMNU resource.
Figure 3 shows the CMNU resource for the Style submenu. Once again, be sure to change the resource id to 133 in both places. Change the style of each item to reflect its name (i.e., change Bold to boldface, Italic to italic, etc.)
Figure 3. The Style CMNU resource. Notice that each item reflects its style.
Figure 4 shows the command numbers for the Style items. Be sure not to assign a command number to the separator line.
Figure 4. The command numbers for the items in the Style CMNU resource.
Before you leave your resource editor, open up the File CMNU (resource id 1000). Notice that the command-key equivalent for New Picture Window is set to AppleT. Change this to AppleR. By convention, AppleT is the command key equivalent for a Style menus Plain Text item.
If you happen to have a copy of Inside Macintosh: Macintosh Toolbox Essentials, you should check out the chapter on the Menu Manager, Chapter 3. Theres a lot of useful information in this chapter. In particular, look for pictures of sample Font, Size, and Style menus, along with descriptions of the rules you should follow when implementing these menus.
Modify TextWindow.h
Your next job is to update TextWindow.h to reflect the changes weve made to the TTextWindow class. Heres a new version of the enum at the beginning of TextWindow.h:
enum
{
mText = 1002,
cFont = 1004,
cSize = 1005,
cStyle = 1006,
mTextFont= 131,
mTextFontSize = 132,
cFontSize9 = 1007,
cFontSize10= 1008,
cFontSize12= 1009,
cFontSize18= 1010,
cFontSize24= 1011,
cFontSize36= 1012,
cFontSizeLarger = 1013,
cFontSizeSmaller= 1014,
cFontSizeOther = 1015,
mTextFontStyle = 133,
cFontStylePlain = 1016,
cFontStyleBold = 1017,
cFontStyleItalic= 1018,
cFontStyleUnderline = 1019,
cFontStyleOutline = 1020,
cFontStyleShadow= 1021
};
Basically, weve added constants for each of the menus and for each of the command numbers associated with the Size and Style CMNUs. Since the Font menu is implemented by a MENU instead of a CMNU, the Font menu has no command number constants.
Heres our new version of the TTextWindow class definition:
class TTextWindow : public TWindow
{
public:
TTextWindow();
virtual ~TTextWindow();
virtual WindowPtr MakeNewWindow( WindowPtr behindWindow );
virtual void Draw(void);
virtual void Activate( Boolean activating );
virtual void Click( EventRecord * anEvent );
virtual void AdjustMenusBeforeMenuSelection( void );
virtual Boolean DoMenuCommand( unsigned long menuCommand );
virtual void ClickAndDrag( EventRecord *eventPtr );
virtualOSErr DragEnterWindow( DragReference dragRef );
virtualOSErr DragInWindow( DragReference dragRef );
virtualOSErr DragLeaveWindow( DragReference dragRef );
virtualOSErr HandleDrop( DragReference dragRef );
// Non-TWindow methods...
virtual void AdjustSizeMenu( void );
virtual void SetNewTextSize( short newSize );
virtual void AdjustStyleMenu( void );
virtual void DoStyleCommand(unsigned long menuCommand);
virtual Boolean IsTextFlavorAvailable( DragReference
dragRef );
virtual Boolean IsMouseInContentRgn( DragReference
dragRef );
static voidSetUpStaticMenu( void );
protected:
static MenuHandle fgMenu;
static MenuHandle fgFontSubMenu;
static MenuHandle fgSizeSubMenu;
static MenuHandle fgStyleSubMenu;
static unsigned longfgWindowTitleCount;
BooleanfCanAcceptDrag;
Handle fDraggedTextHandle;
BooleanfIsWindowHighlighted;
short fCurrentFontSize;
short fCurrentFontStyle;
};
The new member functions and data members are set in boldface. There are six new member functions and five new data members. AdjustMenusBeforeMenuSelection() gets called automatically when a command key equivalent is typed or when a mouseDown occurs in the menu bar. Well use this routine to add or remove check marks from items, and to enable or disable menu items as appropriate.
DoMenuCommand() takes a menu command and dispatches it. For example, if you select Bold from the Style submenu, DoMenuCommand() will run the code that changes the text in the text window to boldface. Note that DoMenuCommand() does not add the checkmark to the Bold item in the Style submenu. Thats the job of AdjustMenusBeforeMenuSelection(). This is an important concept. Menu adjustment happens immediately before a menu is selected. Menu commands are executed immediately after the item is selected.
AdjustSizeMenu() does the check marks and enabling/disabling of the Size menu. SetNewTextSize() takes a new font size and changes the font size of the current text window.
AdjustSizeMenu() does the check marks and enabling/disabling of the Style menu. DoStyleCommand() changes the style of the text in the current text window.
fgFontSubMenu, fgSizeSubMenu, and fgStyleSubMenu each hold their respective MenuHandles and are each static. As youll see, fgSizeSubMenu and fgStyleSubMenu are based on CMNU resources and will have their commands registered. fgFontSubMenu is based on a MENU resource, and well use AddResMenu() to add the currently installed fonts to the menu.
fCurrentFontSize holds the size of the text in its window. The bits in fCurrentFontStyle hold the style settings of the text in its window. If all the bits are off (if fCurrentFontStyle is 0), the style is Plain Text. Otherwise, well have to check individual bits to see if individual styles are turned on.
Modify TextWindow.cp
Add these two constants to the top of the file, just after the other const definitions:
const short kMinimumFontSize = 6;
const short kMaximumFontSize = 255;
Next, add these three static definitions just after the definition of TTextWindow::fgMenu. Once you get your code working, try commenting these three lines out to see what happens.
MenuHandleTTextWindow::fgFontSubMenu;
MenuHandleTTextWindow::fgSizeSubMenu;
MenuHandleTTextWindow::fgStyleSubMenu;
Add this code to MakeNewWindow(), just before the call to ShowWindow():
fCurrentFontSize = 18;
TextSize( fCurrentFontSize );
fCurrentFontStyle = 0;
The first two lines initialize fCurrentFontSize and set the newly created windows text size. The last line sets fCurrentFontStyle to Plain Text, which is the default style for the new window.
Go to the bottom of the file and add this code to SetUpStaticMenu():
TTextWindow::fgSizeSubMenu =
gMenuBar->GetMenuFromCMNU( mTextFontSize );
InsertMenu( TTextWindow::fgSizeSubMenu, -1 );
TTextWindow::fgStyleSubMenu =
gMenuBar->GetMenuFromCMNU( mTextFontStyle );
InsertMenu( TTextWindow::fgStyleSubMenu, -1 );
TTextWindow::fgFontSubMenu = GetMenu( mTextFont );
InsertMenu( TTextWindow::fgFontSubMenu, -1 );
AppendResMenu( TTextWindow::fgFontSubMenu, 'FONT' );
The first two lines load the Size CMNU resource, register its commands, and add it to the Menu Managers data structure as a hierarchical menu. The next two lines do the same thing with the Style CMNU. The last three lines load the Font MENU, add it as a hierarchical menu, then adds all the current fonts to the menu. Note that AppenResMenu() will add both FONT and FOND resources, whether you specify FONT or FOND.
Next comes a small bug fix from last month. The routine Activate() calls gMenuBar->Invalidate() after it inserts the new Text menu. This eventually forces the menu bar to redraw itself, removing the Text title. Unfortunately, we forgot to add the Invalidate() call when we deactivate. Heres the new version of Activate() with the new lines shown in boldface:
void
TTextWindow::Activate( Boolean activating )
{
if ( activating )
{
InsertMenu( fgMenu, 0 );
gMenuBar->Invalidate();
}
else
{
DeleteMenu( mText );
gMenuBar->Invalidate();
}
}
Next, youll add the six new functions we added to the class definition in TextWindow.h:
virtual void AdjustMenusBeforeMenuSelection( void );
virtual Boolean DoMenuCommand( unsigned long menuCommand );
virtual void AdjustSizeMenu( void );
virtual void SetNewTextSize( short newSize );
virtual void AdjustStyleMenu( void );
virtual void DoStyleCommand( unsigned long menuCommand );
Heres the code for AdjustMenusBeforeMenuSelection(). All it does is call the two menu adjustment routines:
void
TTextWindow::AdjustMenusBeforeMenuSelection( void )
{
AdjustSizeMenu();
AdjustStyleMenu();
}
Heres the code for DoMenuCommand(). This routine gets called when a menu item is selected that has a registered command associated with it and when this window object is the frontmost window.
Boolean
TTextWindow::DoMenuCommand(unsigned long menuCommand)
{
switch (menuCommand)
{
Since we have overridden the TWindow function of the same name, we need to either include code that handles the cClose command, or else call the inherited TWindow::DoMenuCommand() function.
case cClose:
if (this->Close() && this->DeleteAfterClose())
delete this;
return true;
break;
These commands call SetNewTextSize(), passing the font size associated with each command.
case cFontSize9:
SetNewTextSize( 9 );
return true;
break;
case cFontSize10:
SetNewTextSize( 10 );
return true;
break;
case cFontSize12:
SetNewTextSize( 12 );
return true;
break;
case cFontSize18:
SetNewTextSize( 18 );
return true;
break;
case cFontSize24:
SetNewTextSize( 24 );
return true;
break;
case cFontSize36:
SetNewTextSize( 36 );
return true;
break;
These commands make the font size one larger or one smaller:
case cFontSizeLarger:
SetNewTextSize( fCurrentFontSize + 1 );
return true;
break;
case cFontSizeSmaller:
SetNewTextSize( fCurrentFontSize - 1 );
return true;
break;
This command is supposed to put up a dialog box that prompts you for a specific font size. Since I didnt want to clutter up this article with simple dialog handling code (oooh, that sounded good! What really happened was I ran out of time <g>), well just respond to this command by passing in a font size of 22. Ill try to add the dialog code to next months column.
case cFontSizeOther:
SetNewTextSize( 22 );
return true;
break;
Each of these next commands gets sent on to DoStyleCommand():
case cFontStylePlain:
case cFontStyleBold:
case cFontStyleItalic:
case cFontStyleUnderline:
case cFontStyleOutline:
case cFontStyleShadow:
DoStyleCommand( menuCommand );
return true;
break;
}
If we handled any of these commands, well have already returned true. If not, well return false, telling the command dispatcher we didnt handle the command.
return false;
}
Heres the code for AdjustSizeMenu():
void
TTextWindow::AdjustSizeMenu( void )
{
Well start by checking to see if the current font is the smallest font size allowed by our application. I didnt pick 6 for any particular reason. If were at the smallest possible size, we need to disable the Smaller menu item, otherwise well enable it.
if ( fCurrentFontSize == kMinimumFontSize )
gMenuBar->EnableCommand( cFontSizeSmaller, false );
else
gMenuBar->EnableCommand( cFontSizeSmaller, true );
We then do the same thing for the Larger menu item, disabling it if we are already at the largest allowed size.
if ( fCurrentFontSize == kMaximumFontSize )
gMenuBar->EnableCommand( cFontSizeLarger, false );
else
gMenuBar->EnableCommand( cFontSizeLarger, true );
Next, well remove the check mark from all the checkable menu items. Note that this list includes the Other... item, which will be checked if the font size is not one of the six weve called out. The first parameter is the items command number. The second parameter is true if the item is to be enabled, and false otherwise. The third parameter is true if you want a check mark, false otherwise.
gMenuBar->EnableAndCheckCommand( cFontSize9, true, false );
gMenuBar->EnableAndCheckCommand( cFontSize10, true, false );
gMenuBar->EnableAndCheckCommand( cFontSize12, true, false );
gMenuBar->EnableAndCheckCommand( cFontSize18, true, false );
gMenuBar->EnableAndCheckCommand( cFontSize24, true, false );
gMenuBar->EnableAndCheckCommand( cFontSize36, true, false );
gMenuBar->EnableAndCheckCommand(cFontSizeOther,true,false);
Finally, well add the check mark to the item that reflects the current font size.
switch ( fCurrentFontSize )
{
case 9:
gMenuBar->EnableAndCheckCommand( cFontSize9, true, true );
break;
case 10:
gMenuBar->EnableAndCheckCommand( cFontSize10, true, true );
break;
case 12:
gMenuBar->EnableAndCheckCommand( cFontSize12, true, true );
break;
case 18:
gMenuBar->EnableAndCheckCommand( cFontSize18, true, true );
break;
case 24:
gMenuBar->EnableAndCheckCommand( cFontSize24, true, true );
break;
case 36:
gMenuBar->EnableAndCheckCommand( cFontSize36, true, true );
break;
default:
gMenuBar->EnableAndCheckCommand(cFontSizeOther,true,true);
break;
}
}
Note that we really should have changed the Other... item to reflect the current font size, assuming it is not one of the six size menu items. For example, if the current font size is 22, the item should read something like Other (22)... and if the size is one of the built in fonts, the item should read Other... Unfortunately, I didnt get a chance to add this code. If you want to add it, this routine is the place to add it. Ill try to get to it next month.
Heres the code for SetNewTextSize():
void
TTextWindow::SetNewTextSize( short newSize )
{
GrafPtroldPort;
Well start b y saving off the old port and making the current window the current port.
GetPort( &oldPort );
SetPort( fWindow );
Next, well be sure that the specified new font size is in bounds.
if ( newSize < kMinimumFontSize )
newSize = kMinimumFontSize;
if ( newSize > kMaximumFontSize )
newSize = kMaximumFontSize;
Now we call TextSize() to make the new size this ports current size, then update the value of fCurrentFontSize, so this value stays with our object.
TextSize( newSize );
fCurrentFontSize = newSize;
Finally, well force a redraw and set the port back to whatever it was.
InvalRect( &(fWindow->portRect) );
SetPort( oldPort );
}
Heres the code for AdjustStyleMenu():
void
TTextWindow::AdjustStyleMenu( void )
{
Well first clear all the Style item check marks.
gMenuBar->EnableAndCheckCommand( cFontStylePlain,
true, false );
gMenuBar->EnableAndCheckCommand( cFontStyleBold,
true, false );
gMenuBar->EnableAndCheckCommand( cFontStyleItalic,
true, false );
gMenuBar->EnableAndCheckCommand( cFontStyleUnderline,
true, false);
gMenuBar->EnableAndCheckCommand(cFontStyleOutline,
true, false );
gMenuBar->EnableAndCheckCommand( cFontStyleShadow,
true, false );
If the current style is Plain Text, well check the Plain Text item.
if ( fCurrentFontStyle == 0 )
gMenuBar->EnableAndCheckCommand( cFontStylePlain,
true, true );
Otherwise, well check each of the five style bits we handle (we dont handle condensed, extended, or color styles), checking all that apply.
else
{
if ( fCurrentFontStyle & bold )
gMenuBar->EnableAndCheckCommand( cFontStyleBold,
true, true );
if ( fCurrentFontStyle & italic )
gMenuBar->EnableAndCheckCommand( cFontStyleItalic,
true, true );
if ( fCurrentFontStyle & underline )
gMenuBar->EnableAndCheckCommand( cFontStyleUnderline,
true, true );
if ( fCurrentFontStyle & outline )
gMenuBar->EnableAndCheckCommand( cFontStyleOutline,
true, true );
if ( fCurrentFontStyle & shadow )
gMenuBar->EnableAndCheckCommand( cFontStyleShadow,
true, true );
}
}
Heres the code for DoStyleCommand():
void
TTextWindow::DoStyleCommand( unsigned long styleCommand )
{
GrafPtroldPort;
Well first save off the old port and make our window object the current port.
GetPort( &oldPort );
SetPort( fWindow );
Next, well check to see which command we are handling. If the command was cFontStylePlain, well set fCurrentFontStyle to 0. Otherwise, well need to flip the bit associated with the command. If the bit was set, well clear it. If the bit was clear, well set it.
switch( styleCommand )
{
case cFontStylePlain:
fCurrentFontStyle = 0;
break;
case cFontStyleBold:
if ( fCurrentFontStyle & bold )
fCurrentFontStyle -= bold;
else
fCurrentFontStyle += bold;
break;
case cFontStyleItalic:
if ( fCurrentFontStyle & italic )
fCurrentFontStyle -= italic;
else
fCurrentFontStyle += italic;
break;
case cFontStyleUnderline:
if ( fCurrentFontStyle & underline )
fCurrentFontStyle -= underline;
else
fCurrentFontStyle += underline;
break;
case cFontStyleOutline:
if ( fCurrentFontStyle & outline )
fCurrentFontStyle -= outline;
else
fCurrentFontStyle += outline;
break;
case cFontStyleShadow:
if ( fCurrentFontStyle & shadow )
fCurrentFontStyle -= shadow;
else
fCurrentFontStyle += shadow;
break;
}
Finally, well set the window to the new style, force a redraw, then reset the current port.
TextFace( fCurrentFontStyle );
InvalRect( &(fWindow->portRect) );
SetPort( oldPort );
}
SprocketMain.cp
You should never have to change the source code that makes up the Sprocket framework. Unless (you saw this coming, right?) you encounter a bug that you need to fix before the next version of Sprocket rolls out the door. Yup. Weve hit just such a beast. If you compile and run just what you have so far, the routine AdjustMenusBeforeMenuSelection() will only get called when you type a command key equivalent. Heres a temporary fix to get your code up and running until I get hold of the real fix. Remember, this is just a patch I put together and is not intended as a permanent fix. When you make this change, be sure you comment out the old code and mark it well. You might find yourself changing it again when I get the real fix from Dave F.
Chances are, when you edit SprocketMain.cp, youll see this icon in the lower left corner of the window.
This is CodeWarriors way of letting you know that this file is marked as read only. To override this, click in the icon so it looks like this:
Youll now be able to edit the file.
Find the routine HandleMouseDown(). Its toward the bottom of the file. Find the switch statement, and the inMenuBar case. This is the code youre going to change:
if (wobj != nil)
wobj->AdjustMenusBeforeMenuSelection();
Change the two instances of wObj to topWindowObj, so the lines look like this:
if (topWindowObj != nil )
topWindowObj->AdjustMenusBeforeMenuSelection();
Thats it! Save your changes and run the sucker!
Running the Program
When you run the program, a text window will appear, just like it did last month. This time, however, when you click on the Text menu, a submenu will be attached to each of the items Font, Size, and Style. Test out the Size and Style menus. Select 9 point, then select Smaller repeatedly until the item dims. Select each of the styles, making sure that the check marks that appear make sense.
Create a second text window (AppleN will do the trick) and give it a unique size and style. When you switch back and forth between the two text windows, the Style and Size menus should change to reflect the frontmost window.
Till Next Month
There are definitely a few things we didnt get to this month. We need to implement the code thatll bring the Font menu to life. We also need to add the dialog box that appears when you select Other... from the Size menu. We also need to change Other... to reflect the current font size, assuming the font size is not one of the called out font sizes. While were at it, we might as well implement the two items in the Picture.