home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 20 / AACD20.BIN / AACD / Programming / ReactionExample / example.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-06  |  6.0 KB  |  278 lines

  1. /*
  2. ** This is a simple example about how to write applications using
  3. ** ReAction and GUIs build with ReActor.
  4. */
  5.  
  6. #define NO_INLINE_STDARG
  7. #include "example_gui.h"
  8. #include <dos/dos.h>
  9. #include <classes/window.h>
  10. #include <proto/exec.h>
  11. #include <proto/intuition.h>
  12. #include <proto/locale.h>
  13. #include <proto/resource.h>
  14. #include <proto/gadtools.h>
  15. #include <clib/alib_protos.h>
  16. #include <stdio.h>
  17.  
  18.  
  19. struct Screen  *glbScreen;
  20. struct MsgPort *glbAppPort;
  21. struct Catalog *glbCatalog;
  22. RESOURCEFILE   *glbResource;
  23. struct Window  *glbWindow;
  24. struct Gadget **glbGadgets;
  25. Object *glbWinobj;
  26.  
  27. APTR glbVisualInfo;
  28. struct Menu *glbMenu;
  29.  
  30. enum { MENU_IGNORE_ID, MENU_ABOUT_ID, MENU_QUIT_ID };
  31.  
  32.  
  33. /**********************************************************************/
  34.  
  35. void sleep(void)
  36. {
  37.     if(glbWinobj)
  38.         SetAttrs(glbWinobj, WA_BusyPointer,TRUE, TAG_DONE);
  39. }
  40.  
  41.  
  42. void wakeup(void)
  43. {
  44.     if(glbWinobj)
  45.         SetAttrs(glbWinobj, WA_BusyPointer,FALSE, TAG_DONE);
  46. }
  47.  
  48. /**********************************************************************/
  49.  
  50. void vreport(const char *fstr, APTR args)
  51. {
  52.     struct EasyStruct req =
  53.     {
  54.         sizeof(struct EasyStruct),
  55.         0,
  56.         "HowToReAction",
  57.         (char*)fstr,
  58.         "Ok"
  59.     };
  60.     EasyRequestArgs(glbWindow, &req, NULL, args);
  61. }
  62.  
  63.  
  64. void report(const char *fstr, ...)
  65. {
  66.     vreport(fstr, (&fstr) + 1);
  67.     /*            ^^^^^^^^^^^
  68.     **This solution only works on Amiga 68k and PPC. */
  69. }
  70.  
  71. /**********************************************************************/
  72.  
  73. int openGUI(void)
  74. {
  75.     /* Get a pointer to the default public screen. */
  76.     glbScreen = LockPubScreen(NULL);
  77.     if(!glbScreen)
  78.     {
  79.         fputs("Failed to lock default public screen.\n", stderr);
  80.         return FALSE;
  81.     }
  82.  
  83.     /* We need an application message port for iconification. */
  84.     glbAppPort = CreateMsgPort();
  85.     if(!glbAppPort)
  86.     {
  87.         fputs("Failed to create message port.\n", stderr);
  88.         return FALSE;
  89.     }
  90.  
  91.     /* Open the catalog if existing. */
  92.     glbCatalog = OpenCatalogA(NULL, "example.catalog", NULL);
  93.     /* Open the RCTResource which is linked to our executable. */
  94.     glbResource = RL_OpenResource(RCTResource, glbScreen, glbCatalog);
  95.     if(!glbResource)
  96.     {
  97.         fputs("Failed to create GUI resource.\n", stderr);
  98.         return FALSE;
  99.     }
  100.  
  101.     /* To use the menu layout functions of gadtools, we have to obtain
  102.     ** some information about the screen. */
  103.     glbVisualInfo = GetVisualInfoA(glbScreen, NULL);
  104.     if(glbVisualInfo)
  105.     {
  106.         struct NewMenu newmenus[] = {
  107.             { NM_TITLE, "Project", NULL, 0, 0, NULL },
  108.             { NM_ITEM,  "About",   "?",  0, 0, (APTR)MENU_ABOUT_ID },
  109.             { NM_ITEM,  "Quit",    "Q",  0, 0, (APTR)MENU_QUIT_ID  },
  110.             { NM_END }
  111.         };
  112.         glbMenu = CreateMenus(newmenus, GTMN_FullMenu,TRUE, TAG_DONE);
  113.         if(glbMenu)
  114.         {
  115.             LayoutMenus(glbMenu, glbVisualInfo,
  116.                 GTMN_NewLookMenus, TRUE,
  117.                 TAG_DONE);
  118.         }
  119.     }
  120.  
  121.     /* Create the window. */
  122.     glbWinobj = RL_NewObject(glbResource, WIN_MAIN_ID,
  123.         WINDOW_AppPort, glbAppPort,
  124.         WINDOW_MenuStrip, glbMenu,
  125.         WA_PubScreen, glbScreen,
  126.         TAG_DONE);
  127.     if(!glbWinobj)
  128.     {
  129.         fputs("Failed to create window object.\n", stderr);
  130.         return FALSE;
  131.     }
  132.  
  133.     /* Obtain the array of gadgets of our window. From now on you can
  134.     ** access the gadgets through glbGadgets[GAD_ID]. */
  135.     glbGadgets = (struct Gadget**) RL_GetObjectArray(glbResource, glbWinobj, GROUP_MAIN_ID);
  136.     if(!glbGadgets)
  137.     {
  138.         fputs("Could not obtain gadget array.\n", stderr);
  139.         return FALSE;
  140.     }
  141.  
  142.     /* Open the window (make it visible). */
  143.     glbWindow = (struct Window*) DoMethod(glbWinobj, WM_OPEN);
  144.     if(!glbWindow)
  145.     {
  146.         fputs("Failed to open window.\n", stderr);
  147.         return FALSE;
  148.     }
  149.  
  150.     /* Now everything is setup correctly. */
  151.     return TRUE;
  152. }
  153.  
  154.  
  155. void closeGUI(void)
  156. {
  157.     /* Close the window if open. */
  158.     if(glbWindow)
  159.     {
  160.         DoMethod(glbWinobj, WM_CLOSE);
  161.         glbWindow = NULL;
  162.     }
  163.     /* Free all created objects. */
  164.     RL_CloseResource(glbResource);
  165.     glbGadgets = NULL;
  166.     glbWinobj = NULL;
  167.     glbResource = NULL;
  168.     /* Destroy the menu. */
  169.     FreeMenus(glbMenu);
  170.     FreeVisualInfo(glbVisualInfo);
  171.     glbMenu = NULL;
  172.     glbVisualInfo = NULL;
  173.     /* Close the catalog. */
  174.     CloseCatalog(glbCatalog);
  175.     glbCatalog = NULL;
  176.     /* Destroy the application message port. */
  177.     DeleteMsgPort(glbAppPort);
  178.     glbAppPort = NULL;
  179.     /* Unlock the default public screen. */
  180.     UnlockPubScreen(NULL, glbScreen);
  181.     glbScreen = NULL;
  182. }
  183.  
  184.  
  185. void loopGUI(void)
  186. {
  187.     BOOL running = TRUE;
  188.     ULONG winsig = 1 << glbWindow->UserPort->mp_SigBit;
  189.     ULONG appsig = 1 << glbAppPort->mp_SigBit;
  190.     ULONG sigs, result;
  191.     UWORD code;
  192.     struct MenuItem *msel;
  193.  
  194.     while(running)
  195.     {
  196.         sigs = Wait(SIGBREAKF_CTRL_C | winsig | appsig);
  197.         /* Handle CTRL-C. */
  198.         if(sigs & SIGBREAKF_CTRL_C)
  199.         {
  200.             running = FALSE;
  201.         }
  202.         /* Handle messages for our window and the application port. */
  203.         if((sigs & winsig) || (sigs & appsig))
  204.         {
  205.             /* Process all pending messages.
  206.             ** Both ports are handled by this method WM_HANDLEINPUT.
  207.             ** The winobj knows of our app-Port, because we provided it in RL_NewObject(). */
  208.             while((result = DoMethod(glbWinobj, WM_HANDLEINPUT, &code)) != WMHI_LASTMSG)
  209.             {
  210.                 switch(result & WMHI_CLASSMASK)
  211.                 {
  212.                     case WMHI_CLOSEWINDOW:
  213.                         running = FALSE;
  214.                         break;
  215.  
  216.                     case WMHI_GADGETUP:
  217.                         switch(result & RL_GADGETMASK)
  218.                         {
  219.                             case GAD_CANCEL_ID:
  220.                                 running = FALSE;
  221.                                 break;
  222.                             /* ... other gadgets can be handled here ... */
  223.                         }
  224.                         break;
  225.  
  226.                     case WMHI_MENUPICK:
  227.                         msel = ItemAddress(glbMenu, result & WMHI_MENUMASK);
  228.                         if(msel)
  229.                         {
  230.                             switch((ULONG)GTMENUITEM_USERDATA(msel))
  231.                             {
  232.                                 case MENU_ABOUT_ID:
  233.                                     sleep();
  234.                                     report("HowToReAction 1.1\nAuthor: m.poellmann@haage-partner.com");
  235.                                     wakeup();
  236.                                     break;
  237.  
  238.                                 case MENU_QUIT_ID:
  239.                                     running = FALSE;
  240.                                     break;
  241.                             }
  242.                         }
  243.                         break;
  244.  
  245.                     case WMHI_ICONIFY:
  246.                         DoMethod(glbWinobj, WM_ICONIFY);
  247.                         glbWindow = NULL;
  248.                         winsig = 0;
  249.                         break;
  250.  
  251.                     case WMHI_UNICONIFY:
  252.                         glbWindow = (struct Window*) DoMethod(glbWinobj, WM_OPEN);
  253.                         if(!glbWindow)
  254.                         {
  255.                             fputs("Failed to reopen window.\n", stderr);
  256.                             running = FALSE;
  257.                         }
  258.                         else
  259.                         {
  260.                             winsig = 1 << glbWindow->UserPort->mp_SigBit;
  261.                         }
  262.                         break;
  263.                 }
  264.             }
  265.         }
  266.     }
  267. }
  268.  
  269.  
  270. int main(void)
  271. {
  272.     if(openGUI())
  273.         loopGUI();
  274.     closeGUI();
  275.     return 0;
  276. }
  277.  
  278.