home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Milan_1991 / Devcon91.1 / Libraries / GadTools / gadget1b.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  11.1 KB  |  481 lines

  1. /*
  2. ** gadget1.c:  Simple example of GadTools gadgets.
  3. **
  4. ** (C) Copyright 1990, 1991, 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. BOOL HandleVanillaKey(struct Window *, UWORD);
  31. struct Gadget *CreateAllGadgets(struct Gadget **, void *, UWORD);
  32.  
  33. /*------------------------------------------------------------------------*/
  34.  
  35. /* Gadget defines of our choosing, to be used as GadgetID's: */
  36.  
  37. #define GAD_SLIDER    1
  38. #define GAD_STRING1    2
  39. #define GAD_STRING2    3
  40. #define GAD_STRING3    4
  41. #define GAD_BUTTON    5
  42.  
  43. /* Range for the slider: */
  44.  
  45. #define SLIDER_MIN    1
  46. #define SLIDER_MAX    20
  47.  
  48. /*------------------------------------------------------------------------*/
  49.  
  50. struct TextAttr Topaz80 =
  51. {
  52.     "topaz.font",    /* Name */
  53.     8,            /* YSize */
  54.     0,            /* Style */
  55.     0,            /* Flags */
  56. };
  57.  
  58. extern struct Library *SysBase;
  59. struct GfxBase *GfxBase = NULL;
  60. struct IntuitionBase *IntuitionBase = NULL;
  61. struct Library *GadToolsBase = NULL;
  62. struct TextFont *font = NULL;
  63. struct Screen *mysc = NULL;
  64. struct Gadget *glist = NULL;
  65. struct Window *mywin = NULL;
  66. void *vi = NULL;
  67.  
  68. BOOL terminated = FALSE;
  69. WORD slider_level = 5;
  70. struct Gadget *slidergad, *string1, *string2, *string3;
  71.  
  72. void main(void)
  73. {
  74.     struct IntuiMessage *imsg;
  75.     struct Gadget *gad;
  76.     ULONG imsgClass;
  77.     UWORD imsgCode;
  78.     UWORD topborder;
  79.  
  80.     /* Open all libraries: */
  81.  
  82.     if (!(GfxBase = (struct GfxBase *)
  83.     OpenLibrary("graphics.library", 36L)))
  84.     bail_out(20, "Requires V36 graphics.library");
  85.  
  86.     if (!(IntuitionBase = (struct IntuitionBase *)
  87.     OpenLibrary("intuition.library", 36L)))
  88.     bail_out(20, "Requires V36 intuition.library");
  89.  
  90.     if (!(GadToolsBase = OpenLibrary("gadtools.library", 36L)))
  91.     bail_out(20, "Requires V36 gadtools.library");
  92.  
  93.     /* Open topaz 8 font, so we can be sure it's openable
  94.      * when we later set ng_TextAttr to &Topaz80:
  95.      */
  96.     if (!(font = OpenFont(&Topaz80)))
  97.     bail_out(20, "Failed to open Topaz 80");
  98.  
  99.     if (!(mysc = LockPubScreen(NULL)))
  100.     bail_out(20, "Couldn't lock default public screen");
  101.  
  102.     if (!(vi = GetVisualInfo(mysc,
  103.     TAG_DONE)))
  104.     bail_out(20, "GetVisualInfo() failed");
  105.  
  106.     /* Here is how we can figure out ahead of time how tall the
  107.      * window's title bar will be:
  108.      */
  109.     topborder = mysc->WBorTop + (mysc->Font->ta_YSize + 1);
  110.  
  111.     if (!CreateAllGadgets(&glist, vi, topborder))
  112.     {
  113.     bail_out(20, "CreateAllGadgets() failed");
  114.     }
  115.  
  116.     if (!(mywin = OpenWindowTags(NULL,
  117.     WA_Width, 400,
  118.     WA_InnerHeight, 140,
  119.  
  120.     WA_Activate, TRUE,
  121.     WA_DragBar, TRUE,
  122.     WA_DepthGadget, TRUE,
  123.     WA_CloseGadget, TRUE,
  124.     WA_SizeGadget, TRUE,
  125.     WA_SimpleRefresh, TRUE,
  126.  
  127.     WA_IDCMP, CLOSEWINDOW | REFRESHWINDOW | VANILLAKEY |\
  128.         SLIDERIDCMP | STRINGIDCMP | BUTTONIDCMP,
  129.  
  130.     WA_MinWidth, 50,
  131.     WA_MinHeight, 50,
  132.     WA_Title, "GadTools Gadget Demo 1B",
  133.  
  134.     /* Gadgets go here, or in NewWindow.FirstGadget */
  135.     WA_Gadgets, glist,
  136.  
  137.     TAG_DONE)))
  138.     bail_out(20, "OpenWindow() failed");
  139.  
  140.     /* After window is open, we must call this GadTools refresh
  141.      * function.
  142.      */
  143.     GT_RefreshWindow(mywin, NULL);
  144.  
  145.     while (!terminated)
  146.     {
  147.     Wait (1 << mywin->UserPort->mp_SigBit);
  148.     /* GT_GetIMsg() returns a cooked-up IntuiMessage with
  149.      * more friendly information for complex gadget classes.  Use
  150.      * it wherever you get IntuiMessages:
  151.      */
  152.     while ((!terminated) && (imsg = GT_GetIMsg(mywin->UserPort)))
  153.     {
  154.         imsgClass = imsg->Class;
  155.         imsgCode = imsg->Code;
  156.         /* Presuming a gadget, of course, but no harm... */
  157.         gad = (struct Gadget *)imsg->IAddress;
  158.         /* Use the toolkit message-replying function here... */
  159.         GT_ReplyIMsg(imsg);
  160.         switch (imsgClass)
  161.         {
  162.         case GADGETDOWN:
  163.         case MOUSEMOVE:
  164.         case GADGETUP:
  165.             terminated = HandleGadgetEvent(mywin, gad, imsgCode);
  166.             break;
  167.  
  168.         case VANILLAKEY:
  169.             terminated = HandleVanillaKey(mywin, imsgCode);
  170.             break;
  171.  
  172.         case CLOSEWINDOW:
  173.             terminated = TRUE;
  174.             break;
  175.  
  176.         case REFRESHWINDOW:
  177.             /* You must use GT_BeginRefresh() where you would
  178.              * normally have your first BeginRefresh()
  179.              */
  180.             GT_BeginRefresh(mywin);
  181.             GT_EndRefresh(mywin, TRUE);
  182.             break;
  183.         }
  184.     }
  185.     }
  186.     bail_out(0, NULL);
  187. }
  188.  
  189. /*------------------------------------------------------------------------*/
  190.  
  191. /*/ bail_out()
  192.  *
  193.  * Function to close down or free any opened or allocated stuff, and then
  194.  * exit.
  195.  *
  196.  */
  197.  
  198. void bail_out(code, error)
  199.  
  200. int code;
  201. STRPTR error;
  202.  
  203. {
  204.     if (mywin)
  205.     {
  206.     CloseWindow(mywin);
  207.     }
  208.  
  209.     /* None of these two calls mind a NULL parameter, so it's not
  210.      * necessary to check for non-NULL before calling.  If we do that,
  211.      * we must be certain that the OpenLibrary() of GadTools succeeded,
  212.      * or else we would be jumping into outer space:
  213.      */
  214.     if (GadToolsBase)
  215.     {
  216.     FreeVisualInfo(vi);
  217.     FreeGadgets(glist);
  218.     CloseLibrary(GadToolsBase);
  219.     }
  220.  
  221.     if (mysc)
  222.     {
  223.     UnlockPubScreen(NULL, mysc);
  224.     }
  225.  
  226.     if (font)
  227.     {
  228.         CloseFont(font);
  229.     }
  230.  
  231.     if (IntuitionBase)
  232.     {
  233.     CloseLibrary(IntuitionBase);
  234.     }
  235.  
  236.     if (GfxBase)
  237.     {
  238.     CloseLibrary(GfxBase);
  239.     }
  240.  
  241.     if (error)
  242.     {
  243.     printf("Error: %s\n", error);
  244.     }
  245.     exit(code);
  246. }
  247.  
  248.  
  249. /*------------------------------------------------------------------------*/
  250.  
  251. /*/ HandleGadgetEvent()
  252.  *
  253.  * Function to handle a GADGETUP or GADGETDOWN event.  For toolkit gadgets,
  254.  * it is possible to use this function to handle MOUSEMOVEs as well, with
  255.  * little or no work.
  256.  *
  257.  */
  258.  
  259. BOOL HandleGadgetEvent(win, gad, code)
  260.  
  261. struct Window *win;
  262. struct Gadget *gad;
  263. UWORD code;
  264.  
  265. {
  266.     BOOL terminated = FALSE;
  267.  
  268.     switch (gad->GadgetID)
  269.     {
  270.     case GAD_SLIDER:
  271.         /* Sliders report their level in the IntuiMessage Code
  272.          * field:
  273.          */
  274.         printf("Slider at level %ld\n", code);
  275.         slider_level = code;
  276.         break;
  277.  
  278.     case GAD_STRING1:
  279.         /* String gadgets report GADGETUP's */
  280.         printf("String gadget 1: '%s'.\n",
  281.         ((struct StringInfo *)gad->SpecialInfo)->Buffer);
  282.         break;
  283.  
  284.     case GAD_STRING2:
  285.         /* String gadgets report GADGETUP's */
  286.         printf("String gadget 2: '%s'.\n",
  287.         ((struct StringInfo *)gad->SpecialInfo)->Buffer);
  288.         break;
  289.  
  290.     case GAD_STRING3:
  291.         /* String gadgets report GADGETUP's */
  292.         printf("String gadget 3: '%s'.\n",
  293.         ((struct StringInfo *)gad->SpecialInfo)->Buffer);
  294.         break;
  295.  
  296.     case GAD_BUTTON:
  297.         /* Buttons always report GADGETUP's, nothing
  298.          * fancy or different here
  299.          */
  300.         printf("Button was pressed.\n");
  301.         /* increment the slider, or wrap it around: */
  302.         if (++slider_level > SLIDER_MAX)
  303.         {
  304.         slider_level = SLIDER_MIN;
  305.         }
  306.         GT_SetGadgetAttrs(slidergad, win, NULL,
  307.         GTSL_Level, slider_level,
  308.         TAG_DONE);
  309.         break;
  310.     }
  311.     return(terminated);
  312. }
  313.  
  314.  
  315. /*------------------------------------------------------------------------*/
  316.  
  317. /*/ HandleVanillaKey()
  318.  *
  319.  * Function to handle vanilla keys.
  320.  *
  321.  */
  322.  
  323. BOOL HandleVanillaKey(win, code)
  324.  
  325. struct Window *win;
  326. UWORD code;
  327.  
  328. {
  329.     switch (code)
  330.     {
  331.     case 'v':
  332.         if (++slider_level > SLIDER_MAX)
  333.         slider_level = SLIDER_MAX;
  334.         GT_SetGadgetAttrs(slidergad, win, NULL,    
  335.         GTSL_Level, slider_level,
  336.         TAG_DONE);
  337.         break;
  338.  
  339.     case 'V':
  340.         if (--slider_level < SLIDER_MIN)
  341.         slider_level = SLIDER_MIN;
  342.         GT_SetGadgetAttrs(slidergad, win, NULL,    
  343.         GTSL_Level, slider_level,
  344.         TAG_DONE);
  345.         break;
  346.  
  347.     case 'f':
  348.     case 'F':
  349.         ActivateGadget(string1, win, NULL);
  350.         break;
  351.  
  352.     case 's':
  353.     case 'S':
  354.         ActivateGadget(string2, win, NULL);
  355.         break;
  356.  
  357.     case 't':
  358.     case 'T':
  359.         ActivateGadget(string3, win, NULL);
  360.         break;
  361.  
  362.     case 'c':
  363.     case 'C':
  364.         if (++slider_level > SLIDER_MAX)
  365.         {
  366.         slider_level = SLIDER_MIN;
  367.         }
  368.         GT_SetGadgetAttrs(slidergad, win, NULL,
  369.         GTSL_Level, slider_level,
  370.         TAG_DONE);
  371.         break;
  372.  
  373.     }
  374.     return(FALSE);
  375. }
  376.  
  377. /*------------------------------------------------------------------------*/
  378.  
  379. /*/ CreateAllGadgets()
  380.  *
  381.  * Here is where all the initialization and creation of toolkit gadgets
  382.  * take place.  This function requires a pointer to a NULL-initialized
  383.  * gadget list pointer.  It returns a pointer to the last created gadget,
  384.  * which can be checked for success/failure.
  385.  *
  386.  */
  387.  
  388. struct Gadget *CreateAllGadgets(glistptr, vi, topborder)
  389.  
  390. struct Gadget **glistptr;
  391. void *vi;
  392. UWORD topborder;
  393.  
  394. {
  395.     struct NewGadget ng;
  396.  
  397.     struct Gadget *gad;
  398.  
  399.     /* All the gadget creation calls accept a pointer to the previous
  400.      * gadget, and link the new gadget to that gadget's NextGadget field.
  401.      * Also, they exit gracefully, returning NULL, if any previous gadget
  402.      * was NULL.  This limits the amount of checking for failure that
  403.      * is needed.  You only need to check before you tweak any gadget
  404.      * structure or use any of its fields, and finally once at the end,
  405.      * before you add the gadgets.
  406.      */
  407.  
  408.     /* We obligingly perform the following operation, required of
  409.      * any program that uses the toolkit.  It gives the toolkit a
  410.      * place to stuff context data:
  411.      */
  412.     gad = CreateContext(glistptr);
  413.  
  414.     /* Since the NewGadget structure is unmodified by any of the
  415.      * CreateGadget() calls, we need only change those fields which
  416.      * are different.
  417.      */
  418.  
  419.     ng.ng_LeftEdge = 140;
  420.     ng.ng_TopEdge = 20+topborder;
  421.     ng.ng_Width = 200;
  422.     ng.ng_Height = 12;
  423.     ng.ng_GadgetText = "_Volume:   ";
  424.     ng.ng_TextAttr = &Topaz80;
  425.     ng.ng_VisualInfo = vi;
  426.     ng.ng_GadgetID = GAD_SLIDER;
  427.     ng.ng_Flags = NG_HIGHLABEL;
  428.  
  429.     slidergad = gad = CreateGadget(SLIDER_KIND, gad, &ng,
  430.     GTSL_Min, SLIDER_MIN,
  431.     GTSL_Max, SLIDER_MAX,
  432.     GTSL_Level, slider_level,
  433.     GTSL_LevelFormat, "%2ld",
  434.     GTSL_MaxLevelLen, 2,
  435.     GT_Underscore, '_',
  436.     TAG_DONE);
  437.  
  438.     ng.ng_TopEdge += 20;
  439.     ng.ng_Height = 14;
  440.     ng.ng_GadgetText = "_First:";
  441.     ng.ng_GadgetID = GAD_STRING1;
  442.     string1 = gad = CreateGadget(STRING_KIND, gad, &ng,
  443.     GTST_String, "Try pressing",
  444.     GTST_MaxChars, 50,
  445.     GT_Underscore, '_',
  446.     TAG_DONE);
  447.  
  448.     ng.ng_TopEdge += 20;
  449.     ng.ng_GadgetText = "_Second:";
  450.     ng.ng_GadgetID = GAD_STRING2;
  451.     string2 = gad = CreateGadget(STRING_KIND, gad, &ng,
  452.     GTST_String, "TAB or Shift-TAB",
  453.     GTST_MaxChars, 50,
  454.     GT_Underscore, '_',
  455.     TAG_DONE);
  456.  
  457.     ng.ng_TopEdge += 20;
  458.     ng.ng_GadgetText = "_Third:";
  459.     ng.ng_GadgetID = GAD_STRING3;
  460.     string3 = gad = CreateGadget(STRING_KIND, gad, &ng,
  461.     GTST_String, "To see what happens!",
  462.     GTST_MaxChars, 50,
  463.     GT_Underscore, '_',
  464.     TAG_DONE);
  465.  
  466.     ng.ng_LeftEdge += 50;
  467.     ng.ng_TopEdge += 20;
  468.     ng.ng_Width = 100;
  469.     ng.ng_Height = 12;
  470.     ng.ng_GadgetText = "_Click Here";
  471.     ng.ng_GadgetID = GAD_BUTTON;
  472.     ng.ng_Flags = 0;
  473.     gad = CreateGadget(BUTTON_KIND, gad, &ng,
  474.     GT_Underscore, '_',
  475.     TAG_DONE);
  476.  
  477.     return(gad);
  478. }
  479.  
  480. /*------------------------------------------------------------------------*/
  481.