home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff225.lzh / MyMenu / MyMenu-Handler.c < prev    next >
C/C++ Source or Header  |  1989-06-24  |  6KB  |  187 lines

  1. /* Copyright ) Darin Johnson, 1989 */
  2. /*                                 */
  3. /* Permission is granted to use    */
  4. /* this program and to freely copy */
  5. /* it and/or source code as long   */
  6. /* as these notices remain.        */
  7. /* No charges for these copies may */
  8. /* be made, except for handling    */
  9. /* and distribution fees.          */
  10.  
  11. /* This is the main process for MyMenu.  This gets loaded and run in the */
  12. /* background by MyMenu.  When started, it finishes initializing itself, */
  13. /* and starts monitoring the WorkBench IDCMP port.  If any messages show */
  14. /* up that we are interested in, we run them as a CLI or WorkBench       */
  15. /* process.  We don't do any memory allocation or cleanup and leave that */
  16. /* all to MyMenu (in order to save some space).  Communication with      */
  17. /* MyMenu is done via a public port - never used as a port, but it holds */
  18. /* "global" variables.                                                   */
  19.  
  20. #include <exec/types.h>
  21. #include <intuition/intuition.h>
  22. #include <libraries/dos.h>
  23. #include <libraries/dosextens.h>
  24. #include <workbench/icon.h>
  25. #include <functions.h>
  26. #include "mymenu.h"
  27.  
  28. static char *copyright = "Copyright ) Darin Johnson, 1989";
  29.  
  30. struct IntuitionBase *IntuitionBase;
  31.  
  32. #ifdef DO_WB
  33. struct IconBase *IconBase;    /* already defined in Manx C */
  34. #endif
  35.  
  36. struct MMData *MM;        /* data shared with other tasks */
  37. struct Process *MyProcess;
  38. #ifdef DO_WB
  39. int wb_cnt;            /* number of wb processes run */
  40. struct MsgPort *wb_reply_port;    /* replies from terminating WB processes */
  41. #endif
  42.  
  43. #ifdef DO_WB
  44.   /* error messages */
  45. struct IntuiText wb_open_msg_5 = { 3,1,JAM2,24,36,NULL,
  46.     (UBYTE*)"Do you REALLY want to quit now?", NULL };
  47. struct IntuiText wb_open_msg_4 = { 0,1,JAM2,7,28,NULL,
  48.     (UBYTE*)"the program finishes after MyMenu.", &wb_open_msg_5 };
  49. struct IntuiText wb_open_msg_3 = { 0,1,JAM2,7,20,NULL,
  50.     (UBYTE*)"still be open.  This can cause a crash if", &wb_open_msg_4 };
  51. struct IntuiText wb_open_msg_2 = { 0,1,JAM2,7,12,NULL,
  52.     (UBYTE*)"A WorkBench program started by MyMenu may", &wb_open_msg_3 };
  53. struct IntuiText wb_open_msg = { 3,1,JAM2,142,4,NULL,
  54.     (UBYTE*)"WARNING!!", &wb_open_msg_2 };
  55. struct IntuiText true_msg = { 0,1,JAM2,7,3,NULL,
  56.     (UBYTE*)"Yes, quit MyMenu", NULL };
  57. struct IntuiText false_msg = { 0,1,JAM2,7,3,NULL,
  58.     (UBYTE*)"No! I didn't mean it", NULL };
  59. #endif
  60.  
  61. /* returns an error to MyMenu if we get one during initialization */
  62. error(code)
  63. char code;
  64. {
  65.   MM->error_code = code;
  66.   if (IntuitionBase)
  67.     CloseLibrary(IntuitionBase);
  68. #ifdef DO_WB
  69.   if (IconBase)
  70.     CloseLibrary(IconBase);
  71. #endif
  72.     /* coordinate with add_handler() in MyMenu */
  73.   Signal(MM->parent_task, 1 << MM->parent_sig);
  74.     /* now coordinate with remove_handler() in MyMenu */
  75.   Wait(SIGBREAKF_CTRL_C);    /* wait for parent */
  76.   Signal(MM->parent_task, 1 << MM->parent_sig);  
  77.   _exit(0);
  78. }
  79.  
  80. /* add personalized menus to menustrip */
  81. add_menu_strip() {
  82.   struct Menu *menu_strip;
  83.   ULONG ilock;
  84.   menu_strip = MM->WBWindow->MenuStrip;
  85.   ilock = LockIBase(0L);
  86.   ClearMenuStrip(MM->WBWindow);
  87.   MM->prev_menu->NextMenu = MM->my_menu;
  88.   SetMenuStrip(MM->WBWindow, menu_strip);
  89.   UnlockIBase(ilock);
  90. }
  91.  
  92. /* remove our personalized menus from menustrip */
  93. del_menu_strip() {
  94.   struct Menu *menu_strip;
  95.   ULONG ilock;
  96.   menu_strip = MM->WBWindow->MenuStrip;
  97.   ilock = LockIBase(0L);
  98.   ClearMenuStrip(MM->WBWindow);
  99.   MM->prev_menu->NextMenu = NULL;
  100.   SetMenuStrip(MM->WBWindow, menu_strip);
  101.   UnlockIBase(ilock);
  102. }
  103.  
  104. /* main program - initialize and start up monitor */
  105. _main() {
  106.     /* find data set up by parent */
  107.   MM = (struct MMData *)FindPort(MYMENU_NAME);
  108.   if (!MM)
  109.     _exit(0);    /* some one ran us directly, bail out */
  110.  
  111.     /* finish initialization */
  112.   MM->handler_task = FindTask(NULL);
  113.   MyProcess = (struct Process *)MM->handler_task;
  114.   MyProcess->pr_ConsoleTask = NULL;
  115.   MyProcess->pr_CLI = NULL;
  116.   MyProcess->pr_WindowPtr = (APTR)-1;    /* disable requesters for FindIt() */
  117.   MyProcess->pr_CurrentDir = NULL;
  118.  
  119.     /* open necessary libraries */  
  120.   IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 0);
  121.   if (IntuitionBase == NULL)
  122.     error(ERR_LIB);
  123. #ifdef DO_WB
  124.   IconBase = (struct IconBase *)OpenLibrary(ICONNAME, 0);
  125.   if (IconBase == NULL)
  126.     error(ERR_LIB);
  127. #endif
  128.  
  129.   if (!MM->WBWindow)
  130.     error(ERR_WIN);
  131.   if (!setup_mon())
  132.     error(ERR_MON);
  133.  
  134.   MM->error_code = ERR_OK;
  135.  
  136.     /* signal parent that we're fully weaned */
  137.   Signal(MM->parent_task, 1 << MM->parent_sig);
  138.  
  139.   add_menu_strip();
  140.  
  141. #ifdef DO_WB
  142.   wb_cnt = 0;
  143.     /* create port AFTER setup_mon() so we don't get same signal */
  144.   wb_reply_port = CreatePort(WBPORT_NAME, 0);
  145.   NewList(&wb_reply_port->mp_MsgList);    /* is this necessary? */
  146. #endif
  147.  
  148. restart:
  149.     monitor();
  150. #ifdef DO_WB
  151.       /* see if we can leave gracefully */
  152.       /* we don't want to terminate and then have a WB reply message */
  153.     if ((wb_cnt > 0) && (AutoRequest(NULL,&wb_open_msg,
  154.                 &true_msg,&false_msg,0,0,370,80)==FALSE))
  155.     {
  156.       MM->error_code = ERR_WB_OPEN;
  157.       Signal(MM->parent_task, 1 << MM->parent_sig);  
  158.       goto restart;
  159.     }
  160. #endif
  161.  
  162.     /* clean everything up */
  163.   MM->error_code = ERR_OK;    /* let parent know that we finished up */
  164.   finish_mon();
  165.  
  166.   del_menu_strip();
  167.   CloseLibrary(IntuitionBase);
  168. #ifdef DO_WB
  169.   CloseLibrary(IconBase);
  170. #endif
  171.  
  172.     /* Forbid until we terminate, so that we don't get UnLoadSeg'ed to soon */
  173.   Forbid();
  174.  
  175. #ifdef DO_WB
  176.   {
  177.     register struct Message *msg;
  178.     while ((msg=GetMsg(wb_reply_port))!=0)
  179.       wbfree(msg);
  180.     DeletePort(wb_reply_port);
  181.   }
  182. #endif
  183.  
  184.     /* signal parent that we are done */
  185.   Signal(MM->parent_task, 1 << MM->parent_sig);  
  186. }
  187.