home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1991 …esperately Seeking Seven / Desperately Seeking Seven.2mg / Dev.CD.8 / Essentials / Tools / DTS.Samples / SC01Shell / C.Shell / Shell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-25  |  15.9 KB  |  559 lines  |  [04] ASCII Text (0x0000)

  1. /*
  2. *   Standard application Shell  - C Version
  3. *   By: Apple II Developer Technical Support
  4. *
  5. *       v3.0    Mensch, KAAR, Luther
  6. */
  7.  
  8. /*
  9. *    Copyright (c) Apple Computer, Inc. 1988-1990
  10. *               All Rights Reserved
  11. *
  12. *    Developer Technical Support Apple II Sample Code
  13. *
  14. *   ------------------------------------------------------
  15. *
  16. *   This program and its derivatives are licensed only for
  17. *   use on Apple computers.
  18. *
  19. *   Works based on this program must contain and
  20. *   conspicuously display this notice.
  21. *
  22. *   This software is provided for your evaluation and to
  23. *   assist you in developing software for the Apple IIGS
  24. *   computer.
  25. *
  26. *   DISCLAIMER OF WARRANTY
  27. *
  28. *   THE SOFTWARE IS PROVIDED "AS IS" WITHOUT
  29. *   WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  30. *   WITH RESPECT TO ITS MERCHANTABILITY OR ITS FITNESS
  31. *   FOR ANY PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO
  32. *   THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
  33. *   YOU.  SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU (AND
  34. *   NOT APPLE OR AN APPLE AUTHORIZED REPRESENTATIVE)
  35. *   ASSUME THE ENTIRE COST OF ALL NECESSARY SERVICING,
  36. *   REPAIR OR CORRECTION.
  37. *
  38. *   Apple does not warrant that the functions
  39. *   contained in the Software will meet your requirements
  40. *   or that the operation of the Software will be
  41. *   uninterrupted or error free or that defects in the
  42. *   Software will be corrected.
  43. *
  44. *   SOME STATES DO NOT ALLOW THE EXCLUSION
  45. *   OF IMPLIED WARRANTIES, SO THE ABOVE EXCLUSION MAY
  46. *   NOT APPLY TO YOU.  THIS WARRANTY GIVES YOU SPECIFIC
  47. *   LEGAL RIGHTS AND YOU MAY ALSO HAVE OTHER RIGHTS
  48. *   WHICH VARY FROM STATE TO STATE.
  49. */
  50.  
  51. #include <types.h>
  52. #include <gsos.h>                             
  53. #include <locator.h>
  54.  
  55. #if 0
  56. #include <adb.h>
  57. #endif
  58.  
  59. #include <intmath.h>
  60. #include <texttool.h>
  61. #include <memory.h>
  62.  
  63. #if 0
  64. #include <sane.h>
  65. #include <ace.h>
  66. #include <resources.h>
  67. #endif
  68.  
  69. #include <misctool.h>
  70.  
  71. #if 0
  72. #include <scheduler.h>
  73. #include <loader.h>
  74. #endif
  75.  
  76. #include <quickdraw.h>
  77.  
  78. #if 0
  79. #include <qdaux.h>
  80. #endif
  81.  
  82. #include <event.h>
  83.  
  84. #if 0
  85. #include <control.h>
  86. #endif
  87.  
  88. #include <window.h>
  89. #include <menu.h>
  90.  
  91. #if 0
  92. #include <lineedit.h>
  93. #include <dialog.h>
  94. #include <sound.h>
  95. #include <notesyn.h>
  96. #include <noteseq.h>
  97. #include <midi.h>
  98. #include <Stdfile.h>
  99. #include <scrap.h>
  100. #endif
  101.  
  102. #include <desk.h>
  103.  
  104. #if 0
  105. #include <list.h>
  106. #include <font.h>
  107. #include <print.h>
  108. #include <textedit.h>
  109. #include <video.h>
  110. #endif
  111.  
  112. /* part of C libraries */
  113. extern int  _toolErr;
  114.  
  115. /******************************************************************************
  116. *   Application specific defines
  117. */
  118.  
  119. /*  menu item numbers for standard DA menu items */
  120. #define UndoID  250 
  121. #define CutID   251
  122. #define CopyID  252
  123. #define PasteID 253
  124. #define ClearID 254
  125. #define CloseID 255
  126.  
  127. /* application menu item numbers */
  128. #define AboutID 0x1101
  129. #define QuitID  0x1202
  130.  
  131. /* menu numbers */
  132. #define AppleMenuID 0x1100
  133. #define FileMenuID  0x1200
  134. #define EditMenuID  0x1300
  135.  
  136. /* resource ID numbers */
  137. #define BaseResID       0x00000000L /* start of resource ID numbers */
  138. #define MenuBarOneRID   0x00001000L /* resource ID of menu bar */
  139.  
  140. /* Let TaskMaster do it all! */
  141. #define MyTaskMask      0x001FFFFFL
  142.  
  143.  
  144. /******************************************************************************
  145. *   Standard global variables here
  146. */
  147.  
  148. int     MyMemoryID;                 /* application's memory ID */
  149. boolean Done;                       /* True after Quit menu item is selected */
  150. long    ToolRecRef;                 /* Ref returned by StartUpTools */
  151. int     WindowKind;                 /* active window's kind */
  152.  
  153.  
  154. /*    The following is the record that is used by TaskMaster to return
  155.       events. It is similar to a regular event record, except that there are
  156.       additional fields at the end. The first additional field is used to
  157.       convey some TaskMaster specific data back to the application. The second
  158.       additional field is called the TaskMask and is used to tell TaskMaster
  159.       what situations to handle.  In this shell, we tell TaskMaster to handle
  160.       everything by setting all currently defined bits to 1 (MyTaskMask). */
  161.  
  162. WmTaskRec MyEvent = {
  163.     0,
  164.     0L,
  165.     0L,
  166.     0,0,
  167.     0,
  168.     0L,
  169.     MyTaskMask
  170. };
  171.  
  172.  
  173. /******************************************************************************
  174. *
  175. *   doQuit:     Set the Done flag to true. This tells the Event loop to exit.
  176. *
  177. *   Inputs:     NONE
  178. *   Outputs:    Done set to true
  179. *   Calls:      NONE
  180. */
  181.  
  182. doQuit()
  183. {
  184.     Done = true;
  185. }
  186.  
  187.  
  188. /******************************************************************************
  189. *
  190. *   doAbout:    Bring up an Alert Dialog box with our about message in it.
  191. *
  192. *   Inputs:     NONE
  193. *   Outputs:    NONE
  194. *   Calls:      NONE
  195. */
  196.  
  197. doAbout()
  198. {
  199.     /* alertFlag of 4 = reference is a Resource ID */
  200.     AlertWindow(4,NIL,BaseResID+1); /* toss result returned by this call */
  201. }
  202.  
  203.  
  204. /******************************************************************************
  205. *
  206. * doMenu:       This routine is called when TaskMaster returns a menu
  207. *               event. It takes the menu item that was hit and calls the
  208. *               proper routine, and then unhilites the menu when it is done.
  209. *
  210. *   Inputs:     TaskData holds menu item selected.
  211. *   Outputs:    NONE
  212. *   Calls:      doAbout, doQuit
  213. */
  214.  
  215. doMenu()
  216. {
  217.     int menuNum;
  218.     int itemNum;
  219.     
  220.     menuNum = HiWord(MyEvent.wmTaskData);   /* get menu ID */
  221.     itemNum = LoWord(MyEvent.wmTaskData);   /* and item ID from MyEvent */
  222.     
  223.     switch (itemNum) {
  224.         case AboutID:
  225.             doAbout();  /* show About alert */
  226.             break;
  227.         case QuitID:
  228.             doQuit();   /* set Done flag */
  229.             break;
  230.         case UndoID:
  231.         case CutID:
  232.         case CopyID:
  233.         case PasteID:
  234.         case ClearID:
  235.         case CloseID:   /* close taken care of by TaskMaster */
  236.             break;
  237.         default:
  238.             /* alertflag of 4 = reference is a Resource ID */
  239.             AlertWindow(4,NIL,BaseResID+2);
  240.             break;  /* toss the result returned by this call */
  241.     }
  242.  
  243.     /* The routine has been called. Unhilite the menu and return to the */
  244.     /* Main Event Loop. */
  245.     
  246.     HiliteMenu(false,menuNum);
  247. }
  248.  
  249.  
  250. /******************************************************************************
  251. *
  252. *   doSysChange:    Called by testTopWindow when the active window
  253. *                   has changed to or from a system window.
  254. *
  255. *   Inputs:         Bit 15 of WindowKind is 0 if top window is an application
  256. *                   window, 1 if top window is a system window.
  257. *   Outputs:        NONE
  258. *   Calls:          NONE
  259. */
  260.  
  261. doSysChange()
  262. {
  263.     if (WindowKind & 0x8000) {
  264.         EnableMItem(UndoID);
  265.         EnableMItem(CutID);
  266.         EnableMItem(CopyID);
  267.         EnableMItem(PasteID);
  268.         EnableMItem(ClearID);
  269.         EnableMItem(CloseID);
  270.         
  271.         /* if your edit menu has items that are selectable when a NDA is not
  272.            the active window, remove the next two lines. */
  273.         SetMenuFlag(enableMenu,EditMenuID);
  274.         HiliteMenu(false,EditMenuID);
  275.     }
  276.     else {
  277.         DisableMItem(UndoID);
  278.         DisableMItem(CutID);
  279.         DisableMItem(CopyID);
  280.         DisableMItem(PasteID);
  281.         DisableMItem(ClearID);
  282.         DisableMItem(CloseID);
  283.     
  284.         /* if your edit menu has items that are selectable when a NDA is not
  285.            the active window, remove the next two lines. */
  286.         SetMenuFlag(disableMenu,EditMenuID);
  287.         HiliteMenu(false,EditMenuID);
  288.     }
  289. }
  290.  
  291.  
  292. /******************************************************************************
  293. *
  294. *   testTopWindow:  This routine is called on every time through the event
  295. *                   loop.  If the type to the top window has changed from
  296. *                   application window to system window or back, this routine
  297. *                   will call doSysChange.
  298. *
  299. *   Inputs:         NONE
  300. *   Outputs:        NONE
  301. *   Calls:          doSysChange
  302. */
  303.  
  304. testTopWindow()
  305. {
  306.     WindowPtr       tempWindowPtr;
  307.     unsigned int    tempWindowKind;
  308.     
  309.     tempWindowPtr = FrontWindow();      /* get active window's grafPort */
  310.  
  311.     tempWindowKind = 0;             /* force to application window kind */
  312.     if (tempWindowPtr)                  /* if there is an active window */
  313.         tempWindowKind = GetWKind(tempWindowPtr);   /* get its kind */          
  314.     
  315.     if (tempWindowKind != WindowKind) {
  316.         WindowKind = tempWindowKind;
  317.         doSysChange();
  318.     }
  319. }
  320.  
  321.  
  322. /******************************************************************************
  323. *
  324. *   closeTools: Shut down the tools I started.
  325. *
  326. *   Inputs:     NONE
  327. *   Outputs:    NONE
  328. *   Calls:      NONE
  329. */
  330.  
  331. closeTools()
  332. {
  333.     /* shut down tools started by StartUpTools */
  334.     ShutDownTools(refIsHandle,ToolRecRef);
  335.     
  336.     /* shut down Memory Manager and Tool Locator */
  337.     MMShutDown(MyMemoryID);
  338.     TLShutDown();
  339. }
  340.  
  341.  
  342. /******************************************************************************
  343. *
  344. *   closeApp:   Close down things. This disposes of all items and
  345. *               memory that we allocated. Usually undoes what was done
  346. *               in initApp.  We don't close our window since _WindShutDown
  347. *               does it for us.
  348. *
  349. *   Inputs:     NONE
  350. *   Outputs:    NONE
  351. *   Calls:      NONE
  352. */                  
  353.  
  354. closeApp()
  355. {
  356.     /* do nothing in this shell */
  357. }
  358.  
  359.  
  360. /******************************************************************************
  361. *
  362. *   eventLoop:  Main Event Loop. Handle things until user selects Quit.
  363. *
  364. *   Inputs:     NONE
  365. *   Outputs:    NONE
  366. *   Calls:      testTopWindow, doMenu
  367. */
  368.  
  369. eventLoop()
  370. {
  371.     word taskCode;
  372.     
  373.     do {
  374.         testTopWindow();        /* test top window to see if it's a NDA */
  375.         
  376.         /* Get event from TaskMaster */
  377.         taskCode = TaskMaster(everyEvent,&MyEvent);
  378.         switch (taskCode) {     /* handle the event for this taskcode */
  379.             /*  With most of these events, we do nothing (in fact, most
  380.                 applications will never see some of these events). You
  381.                 should cut the case labels for events your application does
  382.                 not use out of this switch statement.  Any of these events
  383.                 your application does use should call a function to handle
  384.                 the event and then use a break statement to terminate the
  385.                 rest of the switch statement.
  386.             */
  387.             case nullEvt:
  388.             case mouseDownEvt:
  389.             case mouseUpEvt:
  390.             case keyDownEvt:
  391.             case autoKeyEvt:
  392.             case updateEvt: 
  393.             case activateEvt:
  394.             case switchEvt:
  395.             case deskAccEvt:
  396.             case driverEvt:
  397.             case app1Evt:
  398.             case app2Evt:
  399.             case app3Evt:
  400.             case app4Evt:
  401.             case wInDesk:
  402.                 break;
  403.             case wInMenuBar:    /* do "In system menu bar" events and */
  404.             case wInSpecial:    /* "Item ID selected was 250-255" events */
  405.                 doMenu();
  406.                 break;
  407.             case wClickCalled:
  408.             case wInContent:
  409.             case wInDrag:
  410.             case wInGrow:
  411.             case wInGoAway:
  412.             case wInZoom:
  413.             case wInInfo:
  414.                 break;
  415.             case wInDeskItem:
  416.             case wInFrame:
  417.             case wInactMenu:
  418.             case wClosedNDA:
  419.             case wCalledSysEdit:
  420.             case wTrackZoom:
  421.             case wHitFrame:
  422.             case wInControl:
  423.             case wInControlMenu:
  424.                 break; 
  425.         }
  426.     } while (!Done);            /* Loop until "Quit" is selected */
  427. }
  428.  
  429.  
  430. /******************************************************************************
  431. *
  432. *   initApp:    Perform any application specific initialization. For
  433. *               this app,  we do is initialize the Done to false,
  434. *               set WindowKind to an application window kind, and initialize
  435. *               all of the menus.
  436. *
  437. *               You might use this procedure to create windows,
  438. *               initialize variables and allocate memory needed for
  439. *               the entire program.
  440. *
  441. *   Inputs:     NONE
  442. *   Outputs:    NONE
  443. *   Calls:      NONE
  444. */                  
  445.  
  446. initApp()
  447. {
  448.     Done = false;                   /* we aren't done yet */
  449.     
  450.     WindowKind = 0;                 /* window kind  = application */
  451.  
  452.     /* create default system menu bar from a resource
  453.        and make it the current menu bar */
  454.        
  455.     SetSysBar(NewMenuBar2(refIsResource,MenuBarOneRID,NIL));
  456.     SetMenuBar(NIL);
  457.     
  458.     RefreshDesktop(NIL);            /* redraw the desktop */
  459.     
  460.     InitCursor();                   /* normal arrow cursor */
  461.     
  462.     FixAppleMenu(AppleMenuID);      /* add NDAs to Apple menu */
  463.     FixMenuBar();                   /* set menu bar height and toss result */
  464.     DrawMenuBar();                  /* draw the menu bar */
  465. }
  466.  
  467.  
  468. /******************************************************************************
  469. *
  470. *   errorCheck: This routine is called by initTools to check for startup
  471. *               errors. An error message is shown and everything is
  472. *               shut down if any errors are detected.
  473. *
  474. *   Inputs:     where = the reference number that tells you where in the
  475. *               initTools procedure the error happened.
  476. *   Outputs:    NONE (program exits)
  477. *   Calls:      closeTools
  478. */
  479.  
  480. /*  The error message is done in this strange way (stuffing hex into the
  481.     middle of a string with Int2Hex) because doing it with a sprintf will
  482.     link in about 20k of code just for this operation.  We don't want to
  483.     do that if we don't have to.  If your application has a need for printf,
  484.     then you may wish to change the way that this works, since you will be
  485.     linking in the 20k for something else anyway.  If you don't do any other
  486.     printf type things, leave this routine the way it is.
  487. */
  488.  
  489. char errStr[] = 
  490.     "Fatal error $xxxx has occurred at xxxx. Press any key to exit:";
  491.  
  492. void errorCheck(where)
  493.     int where;
  494. {
  495.     int theError;
  496.     
  497.     if (_toolErr) {
  498.         theError = _toolErr;                /* store the error number */
  499.         Int2Hex(theError,errStr + 13, 4);   /* Stick error # into a string */
  500.         Int2Hex(where, errStr + 34, 4);     /* Stick loc # into a string */
  501.         
  502.         GrafOff();                          /* Turn off Super Hires */
  503.         WriteCString(errStr);               /* write errStr to text screen */
  504.         SysBeep();                          /* ring the bell */
  505.         ReadChar(noEcho);                   /* wait for keypress & toss it */
  506.  
  507.         closeTools();                       /* ShutDown my Tools */
  508.         exit(1);                            /* quit with APW status = 1 */
  509.     }
  510. }
  511.  
  512.  
  513. /******************************************************************************
  514. *
  515. * initTools:    Load and startup the tools needed. errorCheck is called
  516. *               after each startup to check for errors.
  517. *
  518. *   Inputs:     NONE
  519. *   Outputs:    NONE
  520. *   Calls:      errorCheck
  521. */                  
  522.  
  523. initTools()
  524. {
  525.     TLStartUp();                /* start up Tool Locator   */
  526.     errorCheck(1);              /* Make sure all is OK */
  527.     
  528.     MyMemoryID = MMStartUp();   /* start up Memory Manager & get Memory ID */
  529.     errorCheck(2);              /* Make sure all is OK     */
  530.  
  531.     /* Start up the rest of the tools */
  532.     ToolRecRef = StartUpTools(MyMemoryID,refIsResource,BaseResID+1);
  533.     errorCheck(3);              /* Make sure all is OK */
  534. }
  535.  
  536.  
  537. /******************************************************************************
  538. *
  539. *   main:       This is the main routine. It calls functions to startup
  540. *               the tools, initialize application specific data, run the
  541. *               main eventLoop, close the application, and shutdown the tools.
  542. *               
  543. *   Inputs:     NONE
  544. *   Outputs:    NONE
  545. *   Calls:      initTools, initApp, eventLoop, closeApp, closeTools
  546. */                  
  547.  
  548. main()
  549. {
  550.     initTools();            /* Initialize tools. */
  551.     initApp();              /* Initialize application specific stuff. */
  552.  
  553.     eventLoop();            /* Do application stuff until user wants to */
  554.                             /* do something else!                       */
  555.  
  556.     closeApp();             /* ShutDown application specific things. */
  557.     closeTools();           /* ShutDown the tools. */
  558. }
  559.