home *** CD-ROM | disk | FTP | other *** search
- # ifdef THINK_C
- /*
- * FILE: macapp.c
- * AUTHOR: R. Gonzalez
- * CREATED: August 28, 1990
- *
- * Defines simple Macintosh application methods. Requires header
- * files defined in MacHeaders; calls Macintosh Toolbox ROM routines.
- * Presently this class makes use of the generic console environment
- * provided by Think C when an stdio function call precedes the
- * usual Toolbox initializations. Mixing the generic console
- * environment with Toolbox calls like this is not recommended by
- * Think C, but appears to work OK at the present.
- *
- * Future versions of the Mac_App class may forego the use of stdio
- * functions, instead using standard Macintosh windows and dialogs
- * for I/O.
- */
-
- # include <oops.h>
- # include <string.h>
- # include <stdio.h>
- # include <console.h>
- # include "macapp.h"
- # include "morestr.h"
-
- # define MAX_LENGTH 80
- # define NIL_POINTER 0L
- # define REMOVE_ALL_EVENTS 0
- # define WNE_TRAP_NUM 0x60
- # define UNIMPL_TRAP_NUM 0x9F
- # define SLEEP 0L
- # define NIL_MOUSE_RGN 0L
-
- /************************************************************************
- * initialize application - programmer should override: first call
- * inherited method, then add new menu items.
- ************************************************************************/
- boolean Mac_App::init(void)
- {
- Generic_App::init();
-
- printf("\n"); /* create console window before Toolbox inits */
- cgotoxy(1,1,stdout); /* move cursor to origin of console */
- chide(stdout); /* hide console window */
-
- /* Initialize Toolbox: */
- InitGraf(&thePort);
- InitFonts();
- FlushEvents(everyEvent,REMOVE_ALL_EVENTS);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(NIL_POINTER);
- InitCursor();
-
- menu_bar = new(Menu_Bar);
- menu_bar->init();
-
- return TRUE;
- }
-
- /************************************************************************
- * execute application - code adapted from Mark & Reed's "Macintosh
- * Programming Primer", Addison-Wesley 1989. No need to override.
- ************************************************************************/
- void Mac_App::run(void)
- {
- EventRecord theEvent;
- boolean wNEImplemented;
- WindowPtr whichWindow;
- short int thePart;
- char command[MAX_LENGTH],
- junk[MAX_LENGTH];
- int menu_num,
- item;
- comfunc command_ptr;
-
- /* check whether WaitNextEvent() is implemented: */
- wNEImplemented = (NGetTrapAddress(WNE_TRAP_NUM,ToolTrap) !=
- NGetTrapAddress(UNIMPL_TRAP_NUM,ToolTrap));
-
- menu_bar->set(menu);
-
- while (!done)
- {
- if (wNEImplemented)
- WaitNextEvent(everyEvent,&theEvent,SLEEP,NIL_MOUSE_RGN);
- else
- {
- SystemTask();
- GetNextEvent(everyEvent,&theEvent);
- }
- switch (theEvent.what)
- {
- case mouseDown:
- thePart = FindWindow(theEvent.where,&whichWindow);
- if (thePart == inMenuBar)
- {
- menu_bar->get_command(command,theEvent);
- if (strcmp(command,"") != 0)
- {
- menu_num = menu->get_menu(command);
- item = menu->get_item(command);
- cshow(stdout); /* show console */
- command_ptr =
- menu->get_command_ptr(menu_num,item);
- if (!(*command_ptr)(this))
- respond("Illegal usage.");
- respond("");
- if (!done)
- query("Press RETURN to continue: ",junk);
- cgotoxy(1,1,stdout);
- ccleos(stdout); /* clear console */
- chide(stdout);
- InitCursor();
- HiliteMenu(0); /* unhilite menu */
- }
- }
- break;
- default:
- break;
- }
- }
- }
-
- /************************************************************************
- * get string from user. No need to override.
- ************************************************************************/
- void Mac_App::query(char *question,char answer[])
- {
- printf("%s",question);
- get_line(stdin,answer,MAX_LENGTH);
- }
-
- /************************************************************************
- * reply to user. No need to override.
- ************************************************************************/
- void Mac_App::respond(char *response)
- {
- puts(response);
- }
-
- /************************************************************************
- * terminate application. Programmer should call inherited method
- * if the derived application overrides it.
- ************************************************************************/
- boolean Mac_App::destroy(void)
- {
- Generic_App::destroy();
- menu_bar->destroy();
- delete(menu_bar);
-
- return TRUE;
- }
-
- # endif
-