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