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

  1. ;/* simplegtgadget.c -- execute me to compile me
  2. lc -b1 -cfistq -v -y simplegtgadget
  3. blink FROM LIB:c.o simplegtgadget.o TO simplegtgadget LIB LIB:lc.lib LIB:amiga.lib
  4. quit
  5. **
  6. ** The example listed here shows how to use the NewGadget structure and
  7. ** the GadTools library functions discussed above to create a simple
  8. ** button gadget.
  9. **
  10. ** Simple example of a GadTools gadget.  Compiled with SAS C v5.10a
  11. */
  12. #define INTUI_V36_NAMES_ONLY
  13.  
  14. #include <exec/types.h>
  15. #include <intuition/intuition.h>
  16. #include <intuition/gadgetclass.h>
  17. #include <libraries/gadtools.h>
  18.  
  19. #include <clib/exec_protos.h>
  20. #include <clib/intuition_protos.h>
  21. #include <clib/gadtools_protos.h>
  22.  
  23. #include <stdio.h>
  24.  
  25. #ifdef LATTICE
  26. int CXBRK(void)    { return(0); }  /* Disable Lattice CTRL/C handling */
  27. int chkabort(void) { return(0); }  /* really */
  28. #endif
  29.  
  30. /* Gadget defines of our choosing, to be used as GadgetID's. */
  31. #define MYGAD_BUTTON    (4)
  32.  
  33. VOID process_window_events(struct Window *);
  34. VOID gadtoolsWindow(VOID);
  35.  
  36. struct TextAttr Topaz80 = { "topaz.font", 8, 0, 0, };
  37.  
  38. struct Library *IntuitionBase;
  39. struct Library *GadToolsBase;
  40.  
  41.  
  42. /*
  43. ** Open all libraries and run.  Clean up when finished or on error..
  44. */
  45. void main(void)
  46. {
  47. if ( (IntuitionBase = OpenLibrary("intuition.library", 37)) != NULL )
  48.     {
  49.     if ( (GadToolsBase = OpenLibrary("gadtools.library", 37)) != NULL )
  50.         {
  51.         gadtoolsWindow();
  52.  
  53.         CloseLibrary(GadToolsBase);
  54.         }
  55.     CloseLibrary(IntuitionBase);
  56.     }
  57. }
  58.  
  59.  
  60. /*
  61. ** Prepare for using GadTools, set up gadgets and open window.
  62. ** Clean up and when done or on error.
  63. */
  64. VOID gadtoolsWindow(VOID)
  65. {
  66. struct Screen    *mysc;
  67. struct Window    *mywin;
  68. struct Gadget    *glist, *gad;
  69. struct NewGadget ng;
  70. void             *vi;
  71.  
  72. glist = NULL;
  73.  
  74. if ( (mysc = LockPubScreen(NULL)) != NULL )
  75.     {
  76.     if ( (vi = GetVisualInfo(mysc, TAG_END)) != NULL )
  77.         {
  78.         /* GadTools gadgets require this step to be taken */
  79.         gad = CreateContext(&glist);
  80.  
  81.         /* create a button gadget centered below the window title */
  82.         ng.ng_TextAttr   = &Topaz80;
  83.         ng.ng_VisualInfo = vi;
  84.         ng.ng_LeftEdge   = 150;
  85.         ng.ng_TopEdge    = 20 + mysc->WBorTop + (mysc->Font->ta_YSize + 1);
  86.         ng.ng_Width      = 100;
  87.         ng.ng_Height     = 12;
  88.         ng.ng_GadgetText = "Click Here";
  89.         ng.ng_GadgetID   = MYGAD_BUTTON;
  90.         ng.ng_Flags      = 0;
  91.         gad = CreateGadget(BUTTON_KIND, gad, &ng, TAG_END);
  92.  
  93.         if (gad != NULL)
  94.             {
  95.             if ( (mywin = OpenWindowTags(NULL,
  96.                     WA_Title,     "GadTools Gadget Demo",
  97.                     WA_Gadgets,   glist,      WA_AutoAdjust,    TRUE,
  98.                     WA_Width,       400,      WA_InnerHeight,    100,
  99.                     WA_DragBar,    TRUE,      WA_DepthGadget,   TRUE,
  100.                     WA_Activate,   TRUE,      WA_CloseGadget,   TRUE,
  101.                     WA_IDCMP, IDCMP_CLOSEWINDOW |
  102.                               IDCMP_REFRESHWINDOW | BUTTONIDCMP,
  103.                     WA_PubScreen,   mysc,
  104.                     TAG_END)) != NULL )
  105.                 {
  106.                 GT_RefreshWindow(mywin, NULL);
  107.  
  108.                 process_window_events(mywin);
  109.  
  110.                 CloseWindow(mywin);
  111.                 }
  112.             }
  113.         /* FreeGadgets() must be called after the context has been
  114.         ** created.  It does nothing if glist is NULL
  115.         */
  116.         FreeGadgets(glist);
  117.         FreeVisualInfo(vi);
  118.         }
  119.     UnlockPubScreen(NULL, mysc);
  120.     }
  121. }
  122.  
  123. /*
  124. ** Standard message handling loop with GadTools message handling functions
  125. ** used (GT_GetIMsg() and GT_ReplyIMsg()).
  126. */
  127. VOID process_window_events(struct Window *mywin)
  128. {
  129. struct IntuiMessage *imsg;
  130. struct Gadget *gad;
  131. BOOL  terminated = FALSE;
  132.  
  133. while (!terminated)
  134.     {
  135.     Wait (1 << mywin->UserPort->mp_SigBit);
  136.  
  137.     /* Use GT_GetIMsg() and GT_ReplyIMsg() for handling */
  138.     /* IntuiMessages with GadTools gadgets.             */
  139.     while ((!terminated) && (imsg = GT_GetIMsg(mywin->UserPort)))
  140.         {
  141.         /* GT_ReplyIMsg() at end of loop */
  142.  
  143.         switch (imsg->Class)
  144.             {
  145.             case IDCMP_GADGETUP:       /* Buttons only report GADGETUP */
  146.                 gad = (struct Gadget *)imsg->IAddress;
  147.                 if (gad->GadgetID == MYGAD_BUTTON)
  148.                         printf("Button was pressed.\n");
  149.                 break;
  150.             case IDCMP_CLOSEWINDOW:
  151.                 terminated = TRUE;
  152.                 break;
  153.             case IDCMP_REFRESHWINDOW:
  154.                 /* This handling is REQUIRED with GadTools. */
  155.                 GT_BeginRefresh(mywin);
  156.                 GT_EndRefresh(mywin, TRUE);
  157.                 break;
  158.             }
  159.         /* Use the toolkit message-replying function here... */
  160.         GT_ReplyIMsg(imsg);
  161.         }
  162.     }
  163. }
  164.