home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / TRSICAT.LZX / CATS_CD2_TRSI / Reference_Library / lib_examples / appmenuitem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-21  |  4.2 KB  |  103 lines

  1. /* This example shows how to create an AppMenuItem.  The example adds a
  2.  * menu item named "Browse Files" to the Workbench Tools menu.  (All
  3.  * AppMenuItems appear in the Workbench Tools menu.)  When the menu item
  4.  * is activated, the example program receives a message from Workbench
  5.  * and then attempts to start up an instance of the More program. (The
  6.  * More program is in the Utilities directory of the Workbench disk.)
  7.  *
  8.  * The example starts up the More program as a separate, asynchronous
  9.  * process using the new SystemTags() function of Release 2 AmigaDOS.
  10.  * For more about the SystemTags() function refer to the AmigaDOS
  11.  * Manual, 3rd Edition from Bantam Books.  When the AppMenuItem has been
  12.  * activated five times, the program exits after freeing any system
  13.  * resources it has used.
  14.  */
  15.  
  16. /* appmenuitem.c - Compiled under SAS C 5.10 with lc -L appmenuitem.c       */
  17. /* Requires Kickstart version 37 or later.  Works from the Shell (CLI) only */
  18.  
  19. #include <exec/types.h>          /* Need this for the Amiga variable types  */
  20. #include <workbench/workbench.h> /* This has DiskObject and AppIcon structs */
  21. #include <workbench/startup.h>   /* This has WBStartup and WBArg structs    */
  22. #include <exec/libraries.h>
  23. #include <dos/dostags.h>
  24. #include <stdio.h>
  25. #include <clib/dos_protos.h>
  26. #include <clib/exec_protos.h>    /* Exec message, port and library functions*/
  27. #include <clib/wb_protos.h>      /* AppMenuItem function protos             */
  28.  
  29. #ifdef LATTICE
  30. int CXBRK(void) { return(0); }   /* Disable Lattice CTRL/C handling */
  31. int chkabort(void) { return(0); }/* really */
  32. #endif
  33.  
  34. extern struct Library *SysBase;
  35. struct Library *WorkbenchBase;
  36.  
  37. void main(int argc, char **argv)
  38. {
  39. struct MsgPort      *myport=NULL;
  40. struct AppMenuItem *appitem=NULL;
  41. struct AppMessage   *appmsg=NULL;
  42. LONG result, x, count=0L;
  43. BOOL success=0L;
  44. BPTR file;
  45.  
  46. if (WorkbenchBase = OpenLibrary("workbench.library",37))
  47.   {
  48.   /* The CreateMsgPort() function is in Exec version 37 and later only */
  49.   if(myport = CreateMsgPort())
  50.     {
  51.     /* Add our own AppMenuItem to the Workbench Tools Menu */
  52.     appitem=AddAppMenuItemA(0L,                   /* Our ID# for item */
  53.                     (ULONG)"SYS:Utilities/More",  /* Our UserData     */
  54.                            "Browse Files",        /* MenuItem Text    */
  55.                             myport,NULL);         /* MsgPort, no tags */
  56.     if(appitem)
  57.       {
  58.       printf("Select Workbench Tools demo menuitem 'Browse Files'\n");
  59.  
  60.       /* For this example, we allow the AppMenuItem to be selected */
  61.       /* only once, then we remove it and exit                     */
  62.       WaitPort(myport);
  63.       while((appmsg=(struct AppMessage *)GetMsg(myport)) && (count<1))
  64.         {
  65.         /* Handle messages from the AppMenuItem - we have only one  */
  66.         /* item so we don't have to check its appmsg->am_ID number. */
  67.         /* We'll System() the command string that we passed as      */
  68.         /* userdata when we added the menu item.                    */
  69.         /* We find our userdata pointer in appmsg->am_UserData      */
  70.  
  71.         printf("User picked AppMenuItem with %ld icons selected\n",
  72.                                                 appmsg->am_NumArgs);
  73.         for(x=0;x<appmsg->am_NumArgs;x++)
  74.            printf("  #%ld name='%s'\n",x+1,appmsg->am_ArgList[x].wa_Name);
  75.  
  76.         count++;
  77.         if( file=Open("CON:0/40/640/150/AppMenu Example/auto/close/wait",
  78.                          MODE_OLDFILE)  )     /* for any stdio output */
  79.           {
  80.           result=SystemTags((UBYTE *)appmsg->am_UserData,SYS_Input,file,
  81.                                                          SYS_Output,NULL,
  82.                                                          SYS_Asynch,TRUE,
  83.                                                          TAG_DONE);
  84.           /* If Asynch System() itself fails, we must close file */
  85.           if(result == -1) Close(file);
  86.           }
  87.         ReplyMsg((struct Message *)appmsg);
  88.         }
  89.       success=RemoveAppMenuItem(appitem);
  90.       }
  91.  
  92.     /* Clear away any messages that arrived at the last moment */
  93.     /* and let Workbench know we're done with the messages     */
  94.     while(appmsg=(struct AppMessage *)GetMsg(myport))
  95.       {
  96.       ReplyMsg((struct Message *)appmsg);
  97.       }
  98.     DeleteMsgPort(myport);
  99.     }
  100.   CloseLibrary(WorkbenchBase);
  101.   }
  102. }
  103.