Sprocket Menus 4
Volume Number: 11
Issue Number: 8
Column Tag: Getting Started
Sprocket Menus, Part 4
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 code to our SprocketPicText project to implement the Style and
most of the Size submenus that appear underneath the Text menu when a TTextWindow
is the frontmost window. This month, we’re going to finish up the Size menu by
implementing the Other... item. We’ll also add code to implement the Font submenu.
Finally, we’ll add the code to the TPictureWindow to implement the two items in the
Picture window that appears whenever a TPictureWindow is the frontmost window.
A DLOG and a DITL
We’ll start off by adding a single DLOG resource and its associated DITL resource to
SprocketStarter.rsrc. The two resources will implement a dialog that prompts the
user to type in a new font size. The dialog will be brought up when the user selects
Other... from the Size menu.
• Start by duplicating last month’s SprocketStarter folder (be sure you remove
objects first if you care about disk space).
Don’t duplicate the Sprocket folder, since it hasn’t changed from
last month. Last month’s Sprocket-Starter folder was named
“SprocketPicText.04/25/95”. Name the new copy
“SprocketPicText.05/31/95”.
Open up your new Sprocket-Starter folder and launch either the
68K or PowerMac SprocketStarter project. When your project window appears,
double-click on the file named SprocketStarter.rsrc to launch your favorite
resource editor.
• Create a new DLOG with a resource ID of 1000, a top of 71, left of 97, bottom of
146, and a right of 363. Be sure you select the modal dialog window type.
• Create a DITL with a resource ID of 1000.
• Create a pushbutton (item#1) with the text OK, a top of 42, left of 184, bottom
of 62, and a right of 253.
• Create a pushbutton (item#2) with the text Cancel, a top of 42, left of 99,
bottom of 62, and a right of 168.
• Create a static text item (item#3) with the text Font size:, a top of 13, left of
13, bottom of 29, and a right of 81.
• Create an editable text item (item#4) with no text, a top of 13, left of 91,
bottom of 29, and a right of 250.
Figure 1 shows the finished dialog, as displayed by Resorcerer.
Figure 1. The DLOG and DITL resources, as displayed by Resorcerer.
TextWindow.h
Quit your resource editor, saving your changes, and return to CodeWarrior. Now open
the file TextWindow.h and make these changes:
• Add these lines to the top of the file, just before the enum:
#include
const long kCancelButtonPressed = 0L;
const short iNumberEditTextField = 4;
const short kGetNumberDialogResID = 1000;
• Add these member function declarations to the TTextWindow class:
virtual long DoNumberDialog( void );
virtual void SetOtherMenuItemString( void );
virtual void PascalStringCat( Str255 dest, Str255 source );
virtual void AdjustFontMenu( void );
virtual Boolean DoMenuSelection( short menu, short item );
TextWindow.cp
Close TextWindow.h and open the file TextWindow.cp. Make these changes:
• Add to the file the member function
TTextWindow::DoNumberDialog()
Here’s the source code:
long
TTextWindow::DoNumberDialog( void )
{
Boolean dialogDone = false;
Str255 text;
Handle itemHandle;
short itemHit, itemType;
Rect itemRect;
long returnValue;
DialogPtr dialog;
dialog = GetNewDialog( kGetNumberDialogResID, NULL,
(WindowPtr)-1L );
if ( dialog == NULL )
return kCancelButtonPressed;
SetDialogDefaultItem( dialog, ok );
SetDialogCancelItem( dialog, cancel );
NumToString( (long)fCurrentFontSize, text );
GetDialogItem( dialog, iNumberEditTextField, &itemType,
&itemHandle, &itemRect );
SetDialogItemText( itemHandle, text );
SelectDialogItemText( dialog, iNumberEditTextField, 0, 32767 );
ShowWindow( dialog );
do
{
ModalDialog( NULL, &itemHit );
if ( itemHit == ok )
{
GetDialogItem( dialog, iNumberEditTextField, &itemType,
&itemHandle, &itemRect );
GetDialogItemText( itemHandle, text );
StringToNum( text, &returnValue );
if ( (returnValue < kMinimumFontSize) ||
(returnValue > kMaximumFontSize) )
{
SysBeep( 20 );
SelectDialogItemText( dialog, iNumberEditTextField,
0, 32767 );
itemHit = iNumberEditTextField;
}
else
dialogDone = true;
}
else if ( itemHit == cancel )
{
returnValue = kCancelButtonPressed;
dialogDone = true;
}
} while ( ! dialogDone );
DisposeDialog( dialog );
return returnValue;
}
This function implements the dialog box whose resources you created earlier.
The dialog allows the reader to enter a new font size for the frontmost text window.
The user must enter a size between kMinimumFontSize and kMaximumFontSize,
otherwise we’ll beep and highlight the editable text field.
We’ll start off by converting the current text size, stored in the data member
fCurrentFontSize, into a Str255 and we’ll place the Str255 version of the number
in the dialog’s editable text field. We’ll then highlight the text field so if the user
wants to type a new value, they don’t have to select the text first to replace it.
Inside the dialog loop, if the user presses the OK button, we’ll convert the text
field to a long, then check to be sure it’s in range. If so, we’ll set dialogDone to true
so we drop out of the loop.
If they hit the Cancel button, we’ll set the return value to
kCancelButtonPressed, which has been pre-defined as 0L, which we know won’t be a
legal font size.
Finally, once we drop out of the loop, we’ll dispose of the dialog and return
returnValue.
• Add to the file the member function
TTextWindow::PascalStringCat()
Here’s the source code:
void
TTextWindow::PascalStringCat( Str255 dest, Str255 source )
{
unsigned char i, destStringLength, sourceStringLength;
destStringLength = dest[0];
sourceStringLength = source[0];
if ( sourceStringLength <= 0 )
return;
for ( i=1; i<=sourceStringLength; i++ )