home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / OTL-MC7.DMS / in.adf / ansicdemo.lha / ANSI-C / gadtools / gadget1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  9.3 KB  |  375 lines

  1. /*
  2. ** gadget1.c:  Simple example of GadTools gadgets.
  3. **
  4. ** (C) Copyright 1990, Commodore-Amiga, Inc.
  5. **
  6. ** Executables based on this information may be used in software
  7. ** for Commodore Amiga computers.  All other rights reserved.
  8. ** This information is provided "as is"; no warranties are made.  All
  9. ** use is at your own risk. No liability or responsibility is assumed.
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <exec/types.h>
  14. #include <intuition/intuition.h>
  15. #include <intuition/gadgetclass.h>
  16. #include <libraries/gadtools.h>
  17.  
  18. #include <pragma/exec_lib.h>
  19. #include <pragma/graphics_lib.h>
  20. #include <pragma/intuition_lib.h>
  21. #include <pragma/gadtools_lib.h>
  22.  
  23. /*------------------------------------------------------------------------*/
  24.  
  25. int main( void);
  26. void bail_out(int, STRPTR);
  27. BOOL HandleGadgetEvent(struct Window *, struct Gadget *, UWORD);
  28. struct Gadget *CreateAllGadgets(struct Gadget **, void *, UWORD);
  29.  
  30. /*------------------------------------------------------------------------*/
  31.  
  32. /* Gadget defines of our choosing, to be used as GadgetID's: */
  33.  
  34. #define GAD_SLIDER    1
  35. #define GAD_STRING    2
  36. #define GAD_BUTTON    3
  37.  
  38. /* Range for the slider: */
  39.  
  40. #define SLIDER_MIN    1
  41. #define SLIDER_MAX    20
  42.  
  43. /*------------------------------------------------------------------------*/
  44.  
  45. struct TextAttr Topaz80 =
  46. {
  47.     "topaz.font",    /* Name */
  48.     8,            /* YSize */
  49.     0,            /* Style */
  50.     0,            /* Flags */
  51. };
  52.  
  53. extern struct Library *SysBase;
  54. struct GfxBase *GfxBase = NULL;
  55. struct IntuitionBase *IntuitionBase = NULL;
  56. struct Library *GadToolsBase = NULL;
  57. struct TextFont *font = NULL;
  58. struct Screen *mysc = NULL;
  59. struct Gadget *glist = NULL;
  60. struct Window *mywin = NULL;
  61. void *vi = NULL;
  62.  
  63. BOOL terminated = FALSE;
  64. WORD slider_level = 5;
  65. struct Gadget *slidergad;
  66.  
  67. int main( void)
  68. {
  69.     struct IntuiMessage *imsg;
  70.     struct Gadget *gad;
  71.     ULONG imsgClass;
  72.     UWORD imsgCode;
  73.     UWORD topborder;
  74.  
  75.     /* Open all libraries: */
  76.  
  77.     if (!(GfxBase = (struct GfxBase *)
  78.     OpenLibrary("graphics.library", 36L)))
  79.     bail_out(20, "Requires V36 graphics.library");
  80.  
  81.     if (!(IntuitionBase = (struct IntuitionBase *)
  82.     OpenLibrary("intuition.library", 36L)))
  83.     bail_out(20, "Requires V36 intuition.library");
  84.  
  85.     if (!(GadToolsBase = OpenLibrary("gadtools.library", 36L)))
  86.     bail_out(20, "Requires V36 gadtools.library");
  87.  
  88.     /* Open topaz 8 font, so we can be sure it's openable
  89.      * when we later set ng_TextAttr to &Topaz80:
  90.      */
  91.     if (!(font = OpenFont(&Topaz80)))
  92.     bail_out(20, "Failed to open Topaz 80");
  93.  
  94.     if (!(mysc = LockPubScreen(NULL)))
  95.     bail_out(20, "Couldn't lock default public screen");
  96.  
  97.     if (!(vi = GetVisualInfo(mysc,
  98.     TAG_DONE)))
  99.     bail_out(20, "GetVisualInfo() failed");
  100.  
  101.     /* Here is how we can figure out ahead of time how tall the
  102.      * window's title bar will be:
  103.      */
  104.     topborder = mysc->WBorTop + (mysc->Font->ta_YSize + 1);
  105.  
  106.     if (!CreateAllGadgets(&glist, vi, topborder))
  107.     {
  108.     bail_out(20, "CreateAllGadgets() failed");
  109.     }
  110.  
  111.     if (!(mywin = OpenWindowTags(NULL,
  112.     WA_Width, 400,
  113.     WA_InnerHeight, 100,
  114.  
  115.     WA_Activate, TRUE,
  116.     WA_DragBar, TRUE,
  117.     WA_DepthGadget, TRUE,
  118.     WA_CloseGadget, TRUE,
  119.     WA_SizeGadget, TRUE,
  120.     WA_SimpleRefresh, TRUE,
  121.  
  122.     WA_IDCMP, CLOSEWINDOW | REFRESHWINDOW | \
  123.         SLIDERIDCMP | STRINGIDCMP | BUTTONIDCMP,
  124.  
  125.     WA_MinWidth, 50,
  126.     WA_MinHeight, 50,
  127.     WA_Title, "GadTools Gadget Demo 1",
  128.  
  129.     /* Gadgets go here, or in NewWindow.FirstGadget */
  130.     WA_Gadgets, glist,
  131.  
  132.     TAG_DONE)))
  133.     bail_out(20, "OpenWindow() failed");
  134.  
  135.     /* After window is open, we must call this GadTools refresh
  136.      * function.
  137.      */
  138.     GT_RefreshWindow(mywin, NULL);
  139.  
  140.     while (!terminated)
  141.     {
  142.     Wait (1 << mywin->UserPort->mp_SigBit);
  143.     /* GT_GetIMsg() returns a cooked-up IntuiMessage with
  144.      * more friendly information for complex gadget classes.  Use
  145.      * it wherever you get IntuiMessages:
  146.      */
  147.     while ((!terminated) && (imsg = GT_GetIMsg(mywin->UserPort)))
  148.     {
  149.         imsgClass = imsg->Class;
  150.         imsgCode = imsg->Code;
  151.         /* Presuming a gadget, of course, but no harm... */
  152.         gad = (struct Gadget *)imsg->IAddress;
  153.         /* Use the toolkit message-replying function here... */
  154.         GT_ReplyIMsg(imsg);
  155.         switch (imsgClass)
  156.         {
  157.         case GADGETUP:
  158.             printf("GADGETUP.  ");
  159.             terminated = HandleGadgetEvent(mywin, gad, imsgCode);
  160.             break;
  161.  
  162.         case GADGETDOWN:
  163.             printf("GADGETDOWN.  ");
  164.             terminated = HandleGadgetEvent(mywin, gad, imsgCode);
  165.             break;
  166.  
  167.         case MOUSEMOVE:
  168.             printf("MOUSEMOVE.  ");
  169.             terminated = HandleGadgetEvent(mywin, gad, imsgCode);
  170.             break;
  171.  
  172.         case CLOSEWINDOW:
  173.             printf("CLOSEWINDOW.\n");
  174.             terminated = TRUE;
  175.             break;
  176.  
  177.         case REFRESHWINDOW:
  178.             printf("REFRESHWINDOW.\n");
  179.             /* You must use GT_BeginRefresh() where you would
  180.              * normally have your first BeginRefresh()
  181.              */
  182.             GT_BeginRefresh(mywin);
  183.             GT_EndRefresh(mywin, TRUE);
  184.             break;
  185.         }
  186.     }
  187.     }
  188.     bail_out(0, NULL);
  189. return 0;
  190. }
  191.  
  192. /*------------------------------------------------------------------------*/
  193.  
  194. /*/ bail_out()
  195.  *
  196.  * Function to close down or free any opened or allocated stuff, and then
  197.  * exit.
  198.  *
  199.  */
  200.  
  201. void bail_out( int code, STRPTR error)
  202. {
  203.     if (mywin)
  204.     {
  205.     CloseWindow(mywin);
  206.     }
  207.  
  208.     /* None of these two calls mind a NULL parameter, so it's not
  209.      * necessary to check for non-NULL before calling.  If we do that,
  210.      * we must be certain that the OpenLibrary() of GadTools succeeded,
  211.      * or else we would be jumping into outer space:
  212.      */
  213.     if (GadToolsBase)
  214.     {
  215.     FreeVisualInfo(vi);
  216.     FreeGadgets(glist);
  217.     CloseLibrary(GadToolsBase);
  218.     }
  219.  
  220.     if (mysc)
  221.     {
  222.     UnlockPubScreen(NULL, mysc);
  223.     }
  224.  
  225.     if (font)
  226.     {
  227.         CloseFont(font);
  228.     }
  229.  
  230.     if (IntuitionBase)
  231.     {
  232.     CloseLibrary( ( struct Library *)IntuitionBase);
  233.     }
  234.  
  235.     if (GfxBase)
  236.     {
  237.     CloseLibrary( ( struct Library *)GfxBase);
  238.     }
  239.  
  240.     if (error)
  241.     {
  242.     printf("Error: %s\n", error);
  243.     }
  244.     exit(code);
  245. }
  246.  
  247.  
  248. /*------------------------------------------------------------------------*/
  249.  
  250. /*/ HandleGadgetEvent()
  251.  *
  252.  * Function to handle a GADGETUP or GADGETDOWN event.  For toolkit gadgets,
  253.  * it is possible to use this function to handle MOUSEMOVEs as well, with
  254.  * little or no work.
  255.  *
  256.  */
  257.  
  258. BOOL HandleGadgetEvent( struct Window *win, struct Gadget *gad, UWORD code)
  259. {
  260.     BOOL terminated = FALSE;
  261.  
  262.     switch (gad->GadgetID)
  263.     {
  264.     case GAD_SLIDER:
  265.         /* Sliders report their level in the IntuiMessage Code
  266.          * field:
  267.          */
  268.         printf("Slider at level %ld\n", code);
  269.         slider_level = code;
  270.         break;
  271.  
  272.     case GAD_STRING:
  273.         /* String gadgets report GADGETUP's */
  274.         printf("String gadget: '%s'.\n",
  275.         ((struct StringInfo *)gad->SpecialInfo)->Buffer);
  276.         break;
  277.  
  278.     case GAD_BUTTON:
  279.         /* Buttons always report GADGETUP's, nothing
  280.          * fancy or different here
  281.          */
  282.         printf("Button was pressed.\n");
  283.         /* increment the slider, or wrap it around: */
  284.         if (++slider_level > SLIDER_MAX)
  285.         {
  286.         slider_level = SLIDER_MIN;
  287.         }
  288.         GT_SetGadgetAttrs(slidergad, win, NULL,
  289.         GTSL_Level, slider_level,
  290.         TAG_DONE);
  291.         break;
  292.     }
  293.     return(terminated);
  294. }
  295.  
  296.  
  297. /*------------------------------------------------------------------------*/
  298.  
  299. /*/ CreateAllGadgets()
  300.  *
  301.  * Here is where all the initialization and creation of toolkit gadgets
  302.  * take place.  This function requires a pointer to a NULL-initialized
  303.  * gadget list pointer.  It returns a pointer to the last created gadget,
  304.  * which can be checked for success/failure.
  305.  *
  306.  */
  307.  
  308. struct Gadget *CreateAllGadgets( struct Gadget **glistptr, void *vi, UWORD topborder)
  309. {
  310.     struct NewGadget ng;
  311.  
  312.     struct Gadget *gad;
  313.  
  314.     /* All the gadget creation calls accept a pointer to the previous
  315.      * gadget, and link the new gadget to that gadget's NextGadget field.
  316.      * Also, they exit gracefully, returning NULL, if any previous gadget
  317.      * was NULL.  This limits the amount of checking for failure that
  318.      * is needed.  You only need to check before you tweak any gadget
  319.      * structure or use any of its fields, and finally once at the end,
  320.      * before you add the gadgets.
  321.      */
  322.  
  323.     /* We obligingly perform the following operation, required of
  324.      * any program that uses the toolkit.  It gives the toolkit a
  325.      * place to stuff context data:
  326.      */
  327.     gad = CreateContext(glistptr);
  328.  
  329.     /* Since the NewGadget structure is unmodified by any of the
  330.      * CreateGadget() calls, we need only change those fields which
  331.      * are different.
  332.      */
  333.  
  334.     ng.ng_LeftEdge = 100;
  335.     ng.ng_TopEdge = 20+topborder;
  336.     ng.ng_Width = 200;
  337.     ng.ng_Height = 12;
  338.     ng.ng_GadgetText = "Speed:   ";
  339.     ng.ng_TextAttr = &Topaz80;
  340.     ng.ng_VisualInfo = vi;
  341.     ng.ng_GadgetID = GAD_SLIDER;
  342.     ng.ng_Flags = NG_HIGHLABEL;
  343.  
  344.     slidergad = gad = CreateGadget(SLIDER_KIND, gad, &ng,
  345.     GTSL_Min, SLIDER_MIN,
  346.     GTSL_Max, SLIDER_MAX,
  347.     GTSL_Level, slider_level,
  348.     GTSL_LevelFormat, "%2ld",
  349.     GTSL_MaxLevelLen, 2,
  350.     TAG_DONE);
  351.  
  352.     ng.ng_TopEdge = 40+topborder;
  353.     ng.ng_Height = 14;
  354.     ng.ng_GadgetText = "Type Here:";
  355.     ng.ng_GadgetID = GAD_STRING;
  356.     gad = CreateGadget(STRING_KIND, gad, &ng,
  357.     GTST_String, "Hello World!",
  358.     GTST_MaxChars, 50,
  359.     TAG_DONE);
  360.  
  361.     ng.ng_LeftEdge += 50;
  362.     ng.ng_TopEdge = 60+topborder;
  363.     ng.ng_Width = 100;
  364.     ng.ng_Height = 12;
  365.     ng.ng_GadgetText = "Click Here";
  366.     ng.ng_GadgetID = GAD_BUTTON;
  367.     ng.ng_Flags = 0;
  368.     gad = CreateGadget(BUTTON_KIND, gad, &ng,
  369.     TAG_DONE);
  370.  
  371.     return(gad);
  372. }
  373.  
  374. /*------------------------------------------------------------------------*/
  375.