home *** CD-ROM | disk | FTP | other *** search
- /********************************************************/
- /* */
- /* WindowMaker Code from Chapter Seven of */
- /* */
- /* *** The Macintosh Programming Primer *** */
- /* */
- /* Copyright 1989, Dave Mark and Cartwright Reed */
- /* */
- /* WindowMaker handles desk accessories, as well */
- /* as error checking and a few other things. Since */
- /* we went to press, we've made a few minor changes */
- /* to WindowMaker. The changes are commented and are */
- /* found in the HandleEvent routine. */
- /* */
- /* The WindowMaker project is a good place to start */
- /* with your own standalone application code. */
- /* */
- /********************************************************/
-
- #define BASE_RES_ID 400
- #define NIL_POINTER 0L
- #define MOVE_TO_FRONT (WindowPtr)-1L
- #define REMOVE_ALL_EVENTS 0
-
- #define APPLE_MENU_ID 400
- #define FILE_MENU_ID 401
- #define EDIT_MENU_ID 402
-
- #define ABOUT_ITEM 1
- #define ABOUT_ALERT 400
- #define ERROR_ALERT_ID 401
-
- #define NO_MBAR BASE_RES_ID
- #define NO_MENU BASE_RES_ID+1
- #define NO_PICTURE BASE_RES_ID+2
- #define NO_WIND BASE_RES_ID+3
-
- #define NEW_ITEM 1
- #define CLOSE_ITEM 2
- #define QUIT_ITEM 3
-
- #define UNDO_ITEM 1
- #define CUT_ITEM 3
- #define COPY_ITEM 4
- #define PASTE_ITEM 5
- #define CLEAR_ITEM 6
-
- #define DRAG_THRESHOLD 30
-
- #define WINDOW_HOME_LEFT 5
- #define WINDOW_HOME_TOP 45
- #define NEW_WINDOW_OFFSET 20
-
- #define MIN_SLEEP 0L
- #define NIL_MOUSE_REGION 0L
-
- #define LEAVE_WHERE_IT_IS FALSE
-
- #define WNE_TRAP_NUM 0x60
- #define UNIMPL_TRAP_NUM 0x9F
-
- #define NIL_STRING "\p"
- #define HOPELESSLY_FATAL_ERROR "\pGame over, man!"
-
- /* KI */
- #include <THINK.h>
- #include <Quickdraw.h>
- #include <Windows.h>
- #include <Events.h>
- #include <OSEvents.h>
- #include <Menus.h>
- #include <ToolUtils.h>
- #include <Dialogs.h>
- #include <OSUtils.h>
- #include <Desk.h>
- #include <Memory.h>
- #include <Fonts.h>
-
-
- Boolean gDone, gWNEImplemented;
- EventRecord gTheEvent;
- MenuHandle gAppleMenu, gEditMenu;
- PicHandle gMyPicture;
- Rect gDragRect;
- int gNewWindowLeft = WINDOW_HOME_LEFT, gNewWindowTop = WINDOW_HOME_TOP;
-
-
- /******************************** main *********/
-
- main()
- {
- ToolBoxInit();
- MenuBarInit();
- LoadPicture();
- SetUpDragRect();
-
- MainLoop();
- }
-
- /*********************************** ToolBoxInit */
-
- ToolBoxInit()
- {
- InitGraf( &thePort );
- InitFonts();
- FlushEvents( everyEvent, REMOVE_ALL_EVENTS );
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( NIL_POINTER );
- InitCursor();
- }
-
-
- /*********************************** MenuBarInit */
-
- MenuBarInit()
- {
- Handle myMenuBar;
-
- if ( ( myMenuBar = GetNewMBar( BASE_RES_ID ) ) == NIL_POINTER )
- ErrorHandler( NO_MBAR );
- SetMenuBar( myMenuBar );
- if ( ( gAppleMenu = GetMHandle( APPLE_MENU_ID ) ) == NIL_POINTER )
- ErrorHandler( NO_MENU );
- if ( ( gEditMenu = GetMHandle( EDIT_MENU_ID ) ) == NIL_POINTER )
- ErrorHandler( NO_MENU );
-
- AddResMenu( gAppleMenu, 'DRVR' );
- DrawMenuBar();
- }
-
-
- /******************************** LoadPicture *********/
-
- LoadPicture()
- {
-
- if ( ( gMyPicture = GetPicture( BASE_RES_ID ) ) == NIL_POINTER )
- ErrorHandler( NO_PICTURE );
- }
-
-
- /******************************** SetUpDragRect *********/
-
- SetUpDragRect()
- {
- gDragRect = screenBits.bounds;
- gDragRect.left += DRAG_THRESHOLD;
- gDragRect.right -= DRAG_THRESHOLD;
- gDragRect.bottom -= DRAG_THRESHOLD;
- }
-
-
- /******************************** MainLoop *********/
-
- MainLoop()
- {
- gDone = FALSE;
- gWNEImplemented = ( GetToolTrapAddress( WNE_TRAP_NUM ) !=
- GetToolTrapAddress( UNIMPL_TRAP_NUM ) );
- while ( gDone == FALSE )
- {
- HandleEvent();
- }
- }
-
-
- /************************************* HandleEvent */
-
- HandleEvent()
- {
- char theChar;
- GrafPtr oldPort; /* This variable is used in updateEvt handling -
- It is not in the book... */
-
- if ( gWNEImplemented )
- WaitNextEvent( everyEvent, &gTheEvent, MIN_SLEEP, NIL_MOUSE_REGION );
- else
- {
- SystemTask();
- GetNextEvent( everyEvent, &gTheEvent );
- }
-
- switch ( gTheEvent.what )
- {
- case mouseDown:
- HandleMouseDown();
- break;
- case keyDown:
- case autoKey:
- theChar = gTheEvent.message & charCodeMask;
- if (( gTheEvent.modifiers & cmdKey ) != 0)
- {
- AdjustMenus();
- HandleMenuChoice( MenuKey( theChar ) );
- }
- break;
- case updateEvt:
- /* This code is different than that found in the book -
- Use this version... */
- if (!IsDAWindow( gTheEvent.message ) )
- {
- GetPort( &oldPort );
- SetPort( (GrafPtr)gTheEvent.message );
- BeginUpdate( (WindowPtr)gTheEvent.message );
- DrawMyPicture( gMyPicture, gTheEvent.message );
- EndUpdate( (WindowPtr)gTheEvent.message );
- SetPort( oldPort );
- }
- break;
- }
- }
-
-
- /************************************* HandleMouseDown */
-
- HandleMouseDown()
- {
- WindowPtr whichWindow;
- short int thePart;
- long int menuChoice, windSize;
-
- thePart = FindWindow( gTheEvent.where, &whichWindow );
- switch ( thePart )
- {
- case inMenuBar:
- AdjustMenus();
- menuChoice = MenuSelect( gTheEvent.where );
- HandleMenuChoice( menuChoice );
- break;
- case inSysWindow:
- SystemClick( &gTheEvent, whichWindow );
- break;
- case inDrag:
- DragWindow( whichWindow, gTheEvent.where, &gDragRect );
- break;
- case inGoAway:
- if ( TrackGoAway( whichWindow, gTheEvent.where ) )
- DisposeWindow( whichWindow );
- break;
- case inContent:
- SelectWindow( whichWindow );
- break;
- }
- }
-
- /************************************* AdjustMenus */
-
- AdjustMenus()
- {
- if (IsDAWindow( FrontWindow() ) )
- {
- EnableItem(gEditMenu, UNDO_ITEM );
- EnableItem(gEditMenu, CUT_ITEM );
- EnableItem(gEditMenu, COPY_ITEM );
- EnableItem(gEditMenu, PASTE_ITEM );
- EnableItem(gEditMenu, CLEAR_ITEM );
- }
- else
- {
- DisableItem(gEditMenu, UNDO_ITEM );
- DisableItem(gEditMenu, CUT_ITEM );
- DisableItem(gEditMenu, COPY_ITEM );
- DisableItem(gEditMenu, PASTE_ITEM );
- DisableItem(gEditMenu, CLEAR_ITEM );
- }
- }
-
-
- /************************************* IsDAWindow */
-
- IsDAWindow( whichWindow )
- WindowPtr whichWindow;
- {
- if ( whichWindow == NIL_POINTER )
- return( FALSE );
- else /* DA windows have negative windowKinds */
- return( ( (WindowPeek)whichWindow )->windowKind < 0 );
- }
-
-
- /************************************* HandleMenuChoice */
-
- HandleMenuChoice( menuChoice )
- long int menuChoice;
- {
- int theMenu;
- int theItem;
-
- if ( menuChoice != 0 )
- {
- theMenu = HiWord( menuChoice );
- theItem = LoWord( menuChoice );
- switch ( theMenu )
- {
- case APPLE_MENU_ID :
- HandleAppleChoice( theItem );
- break;
- case FILE_MENU_ID :
- HandleFileChoice( theItem );
- break;
- case EDIT_MENU_ID :
- HandleEditChoice( theItem );
- break;
- }
- HiliteMenu( 0 );
- }
- }
-
-
- /******************************** HandleAppleChoice *******/
-
- HandleAppleChoice( theItem )
- int theItem;
- {
- Str255 accName;
- int accNumber;
-
- switch ( theItem )
- {
- case ABOUT_ITEM :
- NoteAlert( ABOUT_ALERT, NIL_POINTER );
- break;
- default :
- GetItem( gAppleMenu, theItem, accName );
- accNumber = OpenDeskAcc( accName );
- break;
- }
- }
-
-
- /******************************** HandleFileChoice *******/
-
- HandleFileChoice( theItem )
- int theItem;
- {
- WindowPtr whichWindow;
- switch ( theItem )
- {
- case NEW_ITEM :
- CreateWindow();
- break;
- case CLOSE_ITEM :
- if ( ( whichWindow = FrontWindow() ) != NIL_POINTER )
- DisposeWindow( whichWindow );
- break;
- case QUIT_ITEM :
- gDone = TRUE;
- break;
- }
- }
-
-
- /******************************** HandleEditChoice *******/
-
- HandleEditChoice( theItem )
- int theItem;
- {
- SystemEdit( theItem - 1 );
- }
-
-
- /************************************ CreateWindow */
-
- CreateWindow()
- {
- WindowPtr theNewestWindow;
-
- if ( ( theNewestWindow = GetNewWindow( BASE_RES_ID, NIL_POINTER,
- MOVE_TO_FRONT ) ) == NIL_POINTER )
- ErrorHandler( NO_WIND );
- if ( ( (screenBits.bounds.right - gNewWindowLeft) < DRAG_THRESHOLD ) ||
- ( ( screenBits.bounds.bottom - gNewWindowTop) < DRAG_THRESHOLD ) )
- {
- gNewWindowLeft = WINDOW_HOME_LEFT;
- gNewWindowTop = WINDOW_HOME_TOP;
- }
-
- MoveWindow( theNewestWindow, gNewWindowLeft, gNewWindowTop, LEAVE_WHERE_IT_IS );
- gNewWindowLeft += NEW_WINDOW_OFFSET;
- gNewWindowTop += NEW_WINDOW_OFFSET;
- ShowWindow( theNewestWindow );
- }
-
-
- /******************************** DrawMyPicture *********/
-
- DrawMyPicture( thePicture, pictureWindow )
- PicHandle thePicture;
- WindowPtr pictureWindow;
- {
- Rect myRect;
-
- myRect = pictureWindow->portRect;
- CenterPict( thePicture, &myRect );
- SetPort( pictureWindow );
- DrawPicture( thePicture, &myRect );
- }
-
-
- /******************************** CenterPict *********/
-
- CenterPict( thePicture, myRectPtr )
- PicHandle thePicture;
- Rect *myRectPtr;
- {
- Rect windRect, pictureRect;
-
- windRect = *myRectPtr;
- pictureRect = (**( thePicture )).picFrame;
- myRectPtr->top = (windRect.bottom - windRect.top - (pictureRect.bottom - pictureRect.top))
- / 2 + windRect.top;
- myRectPtr->bottom = myRectPtr->top + (pictureRect.bottom - pictureRect.top);
- myRectPtr->left = (windRect.right - windRect.left - (pictureRect.right - pictureRect.left))
- / 2 + windRect.left;
- myRectPtr->right = myRectPtr->left + (pictureRect.right - pictureRect.left);
- }
-
-
- /******************************** ErrorHandler *********/
-
- ErrorHandler( stringNum )
- int stringNum;
- {
- StringHandle errorStringH;
-
- if ( ( errorStringH = GetString( stringNum ) ) == NIL_POINTER )
- ParamText( HOPELESSLY_FATAL_ERROR, NIL_STRING, NIL_STRING, NIL_STRING );
- else
- {
- HLock( errorStringH );
- ParamText( *errorStringH, NIL_STRING, NIL_STRING, NIL_STRING );
- HUnlock( errorStringH );
- }
- StopAlert( ERROR_ALERT_ID, NIL_POINTER );
- ExitToShell();
- }