home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / NDK / NDK_3.5 / Examples / Reaction / CheckBox / checkboxexample.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-11-02  |  6.3 KB  |  281 lines

  1. /**
  2. *
  3. * COPYRIGHT:
  4. *
  5. *   Unless otherwise noted, all files are Copyright (c) 1999 Amiga, Inc.
  6. *   All rights reserved.
  7. *
  8. * DISCLAIMER:
  9. *
  10. *   This software is provided "as is". No representations or warranties
  11. *   are made with respect to the accuracy, reliability, performance,
  12. *   currentness, or operation of this software, and all use is at your
  13. *   own risk. Neither Amiga nor the authors assume any responsibility
  14. *   or liability whatsoever with respect to your use of this software.
  15. *
  16.  
  17.  **  CheckBoxExample.c -- CheckBox class Example.
  18.  **
  19.  **  This is a simple example testing some of the capabilities of the
  20.  **  CheckBox gadget class.
  21.  **
  22.  **  This code opens a window and then creates 2 CheckBox gadgets which
  23.  **  are subsequently attached to the window's gadget list.
  24.  **/
  25.  
  26. /* system includes
  27.  */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31.  
  32. #include <exec/types.h>
  33. #include <exec/memory.h>
  34. #include <intuition/intuition.h>
  35. #include <intuition/gadgetclass.h>
  36. #include <libraries/gadtools.h>
  37. #include <graphics/gfxbase.h>
  38. #include <graphics/text.h>
  39. #include <graphics/gfxmacros.h>
  40. #include <utility/tagitem.h>
  41. #include <workbench/startup.h>
  42. #include <workbench/workbench.h>
  43.  
  44. #include <proto/intuition.h>
  45. #include <proto/graphics.h>
  46. #include <proto/exec.h>
  47. #include <proto/dos.h>
  48. #include <proto/utility.h>
  49. #include <proto/wb.h>
  50. #include <proto/icon.h>
  51.  
  52. #include <clib/alib_protos.h>
  53.  
  54. /* ReAction includes
  55.  */
  56. #define ALL_REACTION_CLASSES
  57. #define ALL_REACTION_MACROS
  58. #include <reaction/reaction.h>
  59.  
  60.  
  61. enum
  62. {
  63.     GID_MAIN=0,
  64.     GID_CHECKBOX1,
  65.     GID_CHECKBOX2,
  66.     GID_DOWN,
  67.     GID_UP,
  68.     GID_QUIT,
  69.     GID_LAST
  70. };
  71.  
  72. enum
  73. {
  74.     WID_MAIN=0,
  75.     WID_LAST
  76. };
  77.  
  78. enum
  79. {
  80.     OID_MAIN=0,
  81.     OID_LAST
  82. };
  83.  
  84. struct Library *WindowBase;
  85. struct Library *LayoutBase;
  86. struct Library *ButtonBase;
  87. struct Library *CheckBoxBase;
  88. struct Library *LabelBase;
  89.  
  90. void openlibs(void) 
  91. {
  92.     /* Open the classes - typically not required to be done manually.
  93.      * SAS/C or DICE AutoInit can do this for you if linked with the
  94.      * supplied reaction.lib
  95.      */
  96.     
  97.     WindowBase = OpenLibrary("window.class",44);
  98.     LayoutBase = OpenLibrary("gadgets/layout.gadget",44);
  99.     ButtonBase = OpenLibrary("gadgets/button.gadget",44);
  100.     CheckBoxBase = OpenLibrary("gadgets/checkbox.gadget",44);
  101.     LabelBase = OpenLibrary("images/label.image",44);
  102.  
  103. }
  104.  
  105. void closelibs(void) 
  106. {
  107.     /* Close the classes.
  108.      */
  109.     CloseLibrary(LabelBase);
  110.     CloseLibrary(CheckBoxBase);
  111.     CloseLibrary(ButtonBase );
  112.     CloseLibrary(LayoutBase );
  113.     CloseLibrary(WindowBase );
  114. }
  115.  
  116. int main(void)
  117. {
  118.     struct MsgPort *AppPort;
  119.  
  120.     struct Window *windows[WID_LAST];
  121.  
  122.     struct Gadget *gadgets[GID_LAST];
  123.  
  124.     Object *objects[OID_LAST];
  125.  
  126.     /* make sure our classes opened... */
  127.     openlibs();
  128.     if (!ButtonBase || !CheckBoxBase || !WindowBase || !LayoutBase || !LabelBase)
  129.         return(30);
  130.     else if ( AppPort = CreateMsgPort() )
  131.     {
  132.         /* Create the window object.
  133.          */
  134.         objects[OID_MAIN] = WindowObject,
  135.             WA_ScreenTitle, "Reaction",
  136.             WA_Title, "Reaction CheckBox Example",
  137.             WA_Activate, TRUE,
  138.             WA_DepthGadget, TRUE,
  139.             WA_DragBar, TRUE,
  140.             WA_CloseGadget, TRUE,
  141.             WA_SizeGadget, TRUE,
  142.             WINDOW_IconifyGadget, TRUE,
  143.             WINDOW_IconTitle, "CheckBox",
  144.             WINDOW_AppPort, AppPort,
  145.             WINDOW_Position, WPOS_CENTERMOUSE,
  146.             WINDOW_ParentGroup, gadgets[GID_MAIN] = VGroupObject,
  147.                 LAYOUT_SpaceOuter, TRUE,
  148.                 LAYOUT_DeferLayout, TRUE,
  149.  
  150.                 LAYOUT_AddChild, gadgets[GID_CHECKBOX1] = CheckBoxObject,
  151.                     GA_ID, GID_CHECKBOX1,
  152.                     GA_RelVerify, TRUE,
  153.                     GA_Text, "CheckBox _1",
  154.                     CHECKBOX_TextPlace, PLACETEXT_RIGHT,
  155.                 CheckBoxEnd,
  156.                 CHILD_NominalSize, TRUE,
  157.  
  158.                 LAYOUT_AddChild, gadgets[GID_CHECKBOX2] = CheckBoxObject,
  159.                     GA_ID, GID_CHECKBOX2,
  160.                     GA_RelVerify, TRUE,
  161.                     GA_Text, "CheckBox _2",
  162.                     CHECKBOX_TextPlace, PLACETEXT_LEFT,
  163.                 CheckBoxEnd,
  164.  
  165.                 LAYOUT_AddChild, VGroupObject,
  166.                     GA_BackFill, NULL,
  167.                     LAYOUT_SpaceOuter, TRUE,
  168.                     LAYOUT_VertAlignment, LALIGN_CENTER,
  169.                     LAYOUT_HorizAlignment, LALIGN_CENTER,
  170.                     LAYOUT_BevelStyle, BVS_FIELD,
  171.  
  172.                     LAYOUT_AddImage, LabelObject,
  173.                         LABEL_Text, "The checkbox may have its label placed\n",
  174.                         LABEL_Text, "either on the left or right side.\n",
  175.                         LABEL_Text, " \n",
  176.                         LABEL_Text, "You may click the label text as well\n",
  177.                         LABEL_Text, "as the check box itself.\n",
  178.                     LabelEnd,
  179.                 LayoutEnd,
  180.  
  181.                 LAYOUT_AddChild, ButtonObject,
  182.                     GA_ID, GID_QUIT,
  183.                     GA_RelVerify, TRUE,
  184.                     GA_Text,"_Quit",
  185.                 ButtonEnd,
  186.                 CHILD_WeightedHeight, 0,
  187.  
  188.             EndGroup,
  189.         EndWindow;
  190.  
  191.          /*  Object creation sucessful?
  192.           */
  193.         if (objects[OID_MAIN])
  194.         {
  195.             /*  Open the window.
  196.              */
  197.             if (windows[WID_MAIN] = (struct Window *) RA_OpenWindow(objects[OID_MAIN]))
  198.             {
  199.                 ULONG wait, signal, app = (1L << AppPort->mp_SigBit);
  200.                 ULONG done = FALSE;
  201.                 ULONG result;
  202.                 UWORD code;
  203.  
  204.                  /* Obtain the window wait signal mask.
  205.                  */
  206.                 GetAttr(WINDOW_SigMask, objects[OID_MAIN], &signal);
  207.  
  208.                 /* Input Event Loop
  209.                  */
  210.                 while (!done)
  211.                 {
  212.                     wait = Wait( signal | SIGBREAKF_CTRL_C | app );
  213.  
  214.                     if ( wait & SIGBREAKF_CTRL_C )
  215.                     {
  216.                         done = TRUE;
  217.                     }
  218.                     else
  219.                     {
  220.                         while ( (result = RA_HandleInput(objects[OID_MAIN], &code) ) != WMHI_LASTMSG )
  221.                         {
  222.                             switch (result & WMHI_CLASSMASK)
  223.                             {
  224.                                 case WMHI_CLOSEWINDOW:
  225.                                     windows[WID_MAIN] = NULL;
  226.                                     done = TRUE;
  227.                                     break;
  228.  
  229.                                 case WMHI_GADGETUP:
  230.                                     switch (result & WMHI_GADGETMASK)
  231.                                     {
  232.                                         case GID_CHECKBOX1:
  233.                                             /* code is TRUE or FALSE depending on check state */
  234.                                             break;
  235.  
  236.                                         case GID_CHECKBOX2:
  237.                                             /* code is TRUE or FALSE depending on check state */
  238.                                             break;
  239.  
  240.                                         case GID_QUIT:
  241.                                             done = TRUE;
  242.                                             break;
  243.                                     }
  244.                                     break;
  245.  
  246.                                 case WMHI_ICONIFY:
  247.                                     RA_Iconify(objects[OID_MAIN]);
  248.                                     windows[WID_MAIN] = NULL;
  249.                                     break;
  250.  
  251.                                 case WMHI_UNICONIFY:
  252.                                     windows[WID_MAIN] = (struct Window *) RA_OpenWindow(objects[OID_MAIN]);
  253.  
  254.                                     if (windows[WID_MAIN])
  255.                                     {
  256.                                         GetAttr(WINDOW_SigMask, objects[OID_MAIN], &signal);
  257.                                     }
  258.                                     else
  259.                                     {
  260.                                         done = TRUE;    // error re-opening window!
  261.                                     }
  262.                                      break;
  263.                             }
  264.                         }
  265.                     }
  266.                 }
  267.             }
  268.  
  269.             /* Disposing of the window object will also close the window if it is
  270.              * already opened, and it will dispose of the layout object attached to it.
  271.              */
  272.             DisposeObject(objects[OID_MAIN]);
  273.         }
  274.  
  275.         DeleteMsgPort(AppPort);
  276.     }
  277.     closelibs();
  278.  
  279.     return(0);
  280. }
  281.