home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / hypercar / mactool / thinkcgu.sit / app ƒ / macapp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-20  |  4.2 KB  |  158 lines

  1. # ifdef        THINK_C
  2. /*
  3. *    FILE:        macapp.c
  4. *    AUTHOR:        R. Gonzalez
  5. *    CREATED:    August 28, 1990
  6. *
  7. *    Defines simple Macintosh application methods.  Requires header
  8. *    files defined in MacHeaders; calls Macintosh Toolbox ROM routines.
  9. *    Presently this class makes use of the generic console environment
  10. *    provided by Think C when an stdio function call precedes the
  11. *    usual Toolbox initializations.  Mixing the generic console 
  12. *    environment with Toolbox calls like this is not recommended by
  13. *    Think C, but appears to work OK at the present.
  14. *
  15. *    Future versions of the Mac_App class may forego the use of stdio
  16. *    functions, instead using standard Macintosh windows and dialogs
  17. *    for I/O.
  18. */
  19.  
  20. # include    <oops.h>
  21. # include    <string.h>
  22. # include    <stdio.h>
  23. # include    <console.h>
  24. # include    "macapp.h"
  25. # include    "morestr.h"
  26.  
  27. # define    MAX_LENGTH            80
  28. # define    NIL_POINTER            0L
  29. # define    REMOVE_ALL_EVENTS    0
  30. # define    WNE_TRAP_NUM        0x60
  31. # define    UNIMPL_TRAP_NUM        0x9F
  32. # define    SLEEP                0L
  33. # define    NIL_MOUSE_RGN        0L
  34.  
  35. /************************************************************************
  36. *    initialize application - programmer should override: first call
  37. *    inherited method, then add new menu items.
  38. ************************************************************************/
  39. boolean    Mac_App::init(void)
  40. {
  41.     Generic_App::init();
  42.     
  43.     printf("\n");        /* create console window before Toolbox inits */
  44.     cgotoxy(1,1,stdout);    /* move cursor to origin of console */
  45.     chide(stdout);            /* hide console window */
  46.         
  47.     /* Initialize Toolbox: */
  48.     InitGraf(&thePort);
  49.     InitFonts();
  50.     FlushEvents(everyEvent,REMOVE_ALL_EVENTS);
  51.     InitWindows();
  52.     InitMenus();
  53.     TEInit();
  54.     InitDialogs(NIL_POINTER);
  55.     InitCursor();
  56.     
  57.     menu_bar = new(Menu_Bar);
  58.     menu_bar->init();
  59.     
  60.     return TRUE;
  61. }
  62.  
  63. /************************************************************************
  64. *    execute application - code adapted from Mark & Reed's "Macintosh
  65. *    Programming Primer", Addison-Wesley 1989.  No need to override.
  66. ************************************************************************/
  67. void    Mac_App::run(void)
  68. {
  69.     EventRecord    theEvent;
  70.     boolean        wNEImplemented;
  71.     WindowPtr    whichWindow;
  72.     short int    thePart;
  73.     char        command[MAX_LENGTH],
  74.                 junk[MAX_LENGTH];
  75.     int            menu_num,
  76.                 item;
  77.     comfunc        command_ptr;
  78.     
  79.     /* check whether WaitNextEvent() is implemented: */
  80.     wNEImplemented = (NGetTrapAddress(WNE_TRAP_NUM,ToolTrap) !=
  81.         NGetTrapAddress(UNIMPL_TRAP_NUM,ToolTrap));
  82.         
  83.     menu_bar->set(menu);
  84.     
  85.     while (!done)
  86.     {
  87.         if (wNEImplemented)
  88.             WaitNextEvent(everyEvent,&theEvent,SLEEP,NIL_MOUSE_RGN);
  89.         else
  90.         {
  91.             SystemTask();
  92.             GetNextEvent(everyEvent,&theEvent);
  93.         }
  94.         switch (theEvent.what)
  95.         {
  96.             case mouseDown:
  97.                 thePart = FindWindow(theEvent.where,&whichWindow);
  98.                 if (thePart == inMenuBar)
  99.                 {
  100.                     menu_bar->get_command(command,theEvent);
  101.                     if (strcmp(command,"") != 0)
  102.                     {
  103.                         menu_num = menu->get_menu(command);
  104.                         item = menu->get_item(command);
  105.                         cshow(stdout);        /* show console */
  106.                         command_ptr =
  107.                             menu->get_command_ptr(menu_num,item);
  108.                         if (!(*command_ptr)(this))
  109.                             respond("Illegal usage.");
  110.                         respond("");
  111.                         if (!done)
  112.                             query("Press RETURN to continue: ",junk);
  113.                         cgotoxy(1,1,stdout);
  114.                         ccleos(stdout);        /* clear console */
  115.                         chide(stdout);
  116.                         InitCursor();
  117.                         HiliteMenu(0);        /* unhilite menu */
  118.                     }
  119.                 }
  120.                 break;
  121.             default:
  122.                 break;
  123.         }
  124.     }
  125. }
  126.  
  127. /************************************************************************
  128. *    get string from user.  No need to override.
  129. ************************************************************************/
  130. void    Mac_App::query(char *question,char answer[])
  131. {
  132.     printf("%s",question);
  133.     get_line(stdin,answer,MAX_LENGTH);
  134. }
  135.  
  136. /************************************************************************
  137. *    reply to user.  No need to override.
  138. ************************************************************************/
  139. void    Mac_App::respond(char *response)
  140. {
  141.     puts(response);
  142. }
  143.  
  144. /************************************************************************
  145. *    terminate application.  Programmer should call inherited method
  146. *    if the derived application overrides it.
  147. ************************************************************************/
  148. boolean    Mac_App::destroy(void)
  149. {
  150.     Generic_App::destroy();
  151.     menu_bar->destroy();
  152.     delete(menu_bar);
  153.     
  154.     return TRUE;
  155. }
  156.  
  157. # endif
  158.