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