home *** CD-ROM | disk | FTP | other *** search
/ The AGA Experience 2 / agavol2.iso / software / utilities / demos / stormc-demo / examples / gadtools / gadtools.c < prev    next >
C/C++ Source or Header  |  1996-01-18  |  25KB  |  986 lines

  1. /*
  2. ** gadtools1.c:  Complex GadTools example.
  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.  
  13. #include <exec/types.h>
  14. #include <exec/memory.h>
  15. #include <intuition/gadgetclass.h>
  16. #include <utility/tagitem.h>
  17. #include <clib/macros.h>
  18. #include <string.h>
  19. #include <stdio.h>
  20.  
  21. #include <libraries/gadtools.h>
  22.  
  23. #include <pragma/exec_lib.h>
  24. #include <pragma/graphics_lib.h>
  25. #include <pragma/intuition_lib.h>
  26. #include <pragma/gadtools_lib.h>
  27. #include <clib/alib_protos.h>
  28.  
  29. #include <graphics/gfxmacros.h>
  30.  
  31. #include <wbstartup.h>
  32.  
  33. extern struct Library *GadToolsBase;
  34.  
  35. /*------------------------------------------------------------------------*/
  36.  
  37. int main(void);
  38. void bail_out(int code, STRPTR error);
  39. BOOL HandleMenuEvent(UWORD code);
  40. BOOL OpenFunc(UWORD code);
  41. BOOL SaveFunc(UWORD code);
  42. BOOL SaveAsFunc(UWORD code);
  43. BOOL PrintFunc(UWORD code);
  44. BOOL QuitFunc(UWORD code);
  45. BOOL HandleMouseMove(struct Gadget *gad, UWORD code);
  46. BOOL HandleGadgetEvent(struct Gadget *gad, UWORD code);
  47. struct Gadget *CreateAllGadgets(struct Gadget **glistptr, 
  48.     void *vi, UWORD topborder);
  49.  
  50. /*------------------------------------------------------------------------*/
  51.  
  52. /*/ Gadget defines of our choosing, to be used as GadgetID's: */
  53.  
  54. #define GAD_BUTTON    1
  55. #define GAD_INTEGER    2
  56. #define GAD_CHECKBOX1    3
  57. #define GAD_CHECKBOX2    4
  58. #define GAD_CYCLE    5
  59. #define GAD_MX        6
  60. #define GAD_SLIDER    7
  61. #define GAD_SCROLLER    8
  62. #define GAD_LVSTRING    9
  63. #define GAD_LISTVIEW    10
  64. #define GAD_PALETTE    11
  65.  
  66.  
  67. /* Menu defines of our choosing, to be placed in the NewMenu's UserData
  68.  * field:
  69.  */
  70. #define MENU_FOO_SET    1
  71. #define MENU_FOO_CLEAR    2
  72. #define MENU_FOO_TOGGLE    3
  73. #define MENU_INCREASE    4
  74. #define MENU_DECREASE    5
  75. #define MENU_BY1    6
  76. #define MENU_BY5    7
  77. #define MENU_BY10    8
  78. #define MENU_CONNECTED    9
  79. #define MENU_NEVER    10
  80.  
  81. /* Since NewMenu.nm_UserData are supposed to be of type (void *), we
  82.  * make this abbreviation do our casting.
  83.  */
  84.  
  85. #define V(x) ((void *)x)
  86.  
  87. /*------------------------------------------------------------------------*/
  88.  
  89. /* Here we specify what we want our menus to contain: */
  90.  
  91. struct NewMenu mynewmenu[] =
  92. {
  93.     { NM_TITLE, "Project",     0 , 0, 0, 0,},
  94.     {  NM_ITEM, "Open...",    "O", 0, 0, OpenFunc,},
  95.     {  NM_ITEM, "Save",         0 , 0, 0, SaveFunc,},
  96.     {  NM_ITEM, "Save As...",    0 , 0, 0, SaveAsFunc,},
  97.     {  NM_ITEM, NM_BARLABEL,     0 , 0, 0, 0,},
  98.     {  NM_ITEM, "Print",     0 , 0, 0, 0,},
  99.     {   NM_SUB, "NLQ",         0 , 0, 0, PrintFunc,},
  100.     {   NM_SUB, "Draft",     0 , 0, 0, PrintFunc,},
  101.     {  NM_ITEM, NM_BARLABEL,     0 , 0, 0, 0,},
  102.     {  NM_ITEM, "Quit...",    "Q", 0, 0, QuitFunc,},
  103.  
  104.     { NM_TITLE, "Gadgets",     0 , 0, 0, 0,},
  105.     {  NM_ITEM, "Foo",         0 , 0, 0, 0,},
  106.     {   NM_SUB, "Set",        "S", 0, 0, V(MENU_FOO_SET),},
  107.     {   NM_SUB, "Clear",    "C", 0, 0, V(MENU_FOO_CLEAR),},
  108.     {   NM_SUB, "Toggle",    "T", 0, 0, V(MENU_FOO_TOGGLE),},
  109.     {  NM_ITEM, "Slider",     0 , 0, 0, 0,},
  110.     {   NM_SUB, "Increase",    "I", 0, 0, V(MENU_INCREASE),},
  111.     {   NM_SUB, "Decrease",    "D", 0, 0, V(MENU_DECREASE),},
  112.     {   NM_SUB, NM_BARLABEL,     0 , 0, 0, 0,},
  113.     {   NM_SUB, "By 1s",     0 , CHECKIT|CHECKED, ~0x0008, V(MENU_BY1),},
  114.     {   NM_SUB, "By 5s",     0 , CHECKIT, ~0x0010, V(MENU_BY5),},
  115.     {   NM_SUB, "By 10s",     0 , CHECKIT, ~0x0020, V(MENU_BY10),},
  116.     {  NM_ITEM, "MX Gadgets",     0 , 0, 0, 0,},
  117.     {   NM_SUB, "Connected?",     0 , CHECKIT|MENUTOGGLE, 0, V(MENU_CONNECTED),},
  118.     {  NM_ITEM, "Not Me!",     0 , NM_ITEMDISABLED, 0, V(MENU_NEVER)},
  119.  
  120.     {   NM_END, 0,         0 , 0, 0, 0},
  121. };
  122.  
  123. /*------------------------------------------------------------------------*/
  124.  
  125. struct NewWindow mynewwin =
  126. {
  127.     0, 0,        /* LeftEdge, TopEdge */
  128.     600, 166,        /* Width, Height */
  129.     -1, -1,             /* DetailPen, BlockPen */
  130.     MENUPICK | MOUSEBUTTONS | GADGETUP | GADGETDOWN | MOUSEMOVE |
  131.     CLOSEWINDOW | REFRESHWINDOW | INTUITICKS, /* IDCMPFlags */
  132.     ACTIVATE | WINDOWDRAG | WINDOWSIZING | WINDOWDEPTH | WINDOWCLOSE |
  133.     SIMPLE_REFRESH,    /* Flags */
  134.     NULL,        /* FirstGadget */
  135.     NULL,        /* CheckMark */
  136.     "Fancy GadTools Demo",    /* Title */
  137.     NULL,        /* Screen */
  138.     NULL,        /* BitMap */    
  139.     50, 50,    /* MinWidth, MinHeight */
  140.     640, 200,    /* MaxWidth, MaxHeight */
  141.     WBENCHSCREEN,    /* Type */
  142. };
  143.  
  144. struct TextAttr Topaz80 =
  145. {
  146.     "topaz.font",    /* Name */
  147.     8,            /* YSize */
  148.     0,            /* Style */
  149.     0,            /* Flags */
  150. };
  151.  
  152. /*------------------------------------------------------------------------*/
  153.  
  154. STRPTR MonthLabels[] =
  155. {
  156.     "January",
  157.     "February",
  158.     "March",
  159.     "April",
  160.     "May",
  161.     "June",
  162.     "July",
  163.     "August",
  164.     "September",
  165.     "October",
  166.     "November",
  167.     "December",
  168.     NULL,
  169. };
  170.  
  171. STRPTR DayLabels[] =
  172. {
  173.     "Monday",
  174.     "Tuesday",
  175.     "Wednesday",
  176.     "Thursday",
  177.     "Friday",
  178.     "Saturday",
  179.     "Sunday",
  180.     NULL,
  181. };
  182.  
  183. struct List lh;
  184.  
  185. /*------------------------------------------------------------------------*/
  186.  
  187. /* These are all the things to be allocated/opened, and later
  188.  * freed/closed:
  189.  */
  190.  
  191. struct GfxBase *GfxBase = NULL;
  192. struct IntuitionBase *IntuitionBase = NULL;
  193. struct Library *GadToolsBase = NULL;
  194. struct TextFont *font = NULL;
  195. struct Screen *mysc = NULL;
  196. struct Remember *RKey = NULL;
  197. struct Gadget *glist = NULL;
  198. struct Menu *menu = NULL;
  199. struct Window *mywin = NULL;
  200. void *vi = NULL;
  201.  
  202. /*------------------------------------------------------------------------*/
  203.  
  204. BOOL terminated;
  205.  
  206. /*------------------------------------------------------------------------*/
  207.  
  208. /* We need the following information about individual gadgets to
  209.  * demonstrate our ability to manipulate them with the GT_SetGadgetAttrs()
  210.  * function:
  211.  */
  212.  
  213. WORD sliderlevel = 7;
  214. WORD incr = 1;
  215. struct Gadget *integergad, *mxgad, *checkgad, *slidergad, *cyclegad,
  216.     *lvgad, *stringgad;
  217. BOOL foochecked = TRUE;
  218. BOOL connected = FALSE;
  219.  
  220. #define SLIDER_MIN    0
  221. #define SLIDER_MAX    49
  222.  
  223. /*------------------------------------------------------------------------*/
  224.  
  225. int main(void)
  226. {
  227.     struct IntuiMessage *imsg;
  228.     struct Gadget *gad;
  229.     ULONG imsgClass;
  230.     UWORD imsgCode;
  231.     UWORD topborder;
  232.  
  233.     terminated = FALSE;
  234.  
  235.     /* Open all libraries: */
  236.  
  237.     if (!(GfxBase = (struct GfxBase *)
  238.     OpenLibrary("graphics.library", 36L)))
  239.     bail_out(20, "Requires V36 graphics.library");
  240.  
  241.     if (!(IntuitionBase = (struct IntuitionBase *)
  242.     OpenLibrary("intuition.library", 36L)))
  243.     bail_out(20, "Requires V36 intuition.library");
  244.  
  245.     if (!(GadToolsBase = OpenLibrary("gadtools.library", 36L)))
  246.     bail_out(20, "Requires V36 gadtools.library");
  247.  
  248.     /* Open topaz 8 font: */
  249.  
  250.     if (!(font = OpenFont(&Topaz80)))
  251.     bail_out(20, "Failed to open font");
  252.  
  253.     if (!(mysc = LockPubScreen(NULL)))
  254.     bail_out(20, "Couldn't lock default public screen");
  255.  
  256.     if (!(vi = GetVisualInfo(mysc,
  257.     TAG_DONE)))
  258.     bail_out(20, "GetVisualInfo() failed");
  259.  
  260.     topborder = mysc->WBorTop + (mysc->Font->ta_YSize + 1);
  261.  
  262.     /* Build and layout menus: */
  263.     if (!(menu = CreateMenus(mynewmenu,
  264.     GTMN_FrontPen, 0,
  265.     TAG_DONE)))
  266.     bail_out(20, "CreateMenus() failed");
  267.  
  268.     if (!LayoutMenus(menu, vi,
  269.     TAG_DONE))
  270.     bail_out(20, "LayoutMenus() failed");
  271.  
  272.     if (!CreateAllGadgets(&glist, vi, topborder))
  273.     {
  274.     bail_out(20, "CreateAllGadgets() failed");
  275.     }
  276.  
  277.     /* I could equally well use the {WA_Gadgets, glist} TagItem */
  278.     mynewwin.FirstGadget = glist;
  279.  
  280.     /* I've actually put the inner-height in mynewwin.Height, so
  281.      * I'll override that value with a W_INNERHEIGHT:
  282.      */
  283.     /* Open the window: */
  284.     if (!(mywin = OpenWindowTags(&mynewwin,
  285.     WA_InnerHeight, mynewwin.Height,
  286.     WA_AutoAdjust, TRUE,
  287.     WA_PubScreen, mysc,
  288.     TAG_DONE)))
  289.     bail_out(20, "OpenWindow() failed");
  290.  
  291.     /* After window is open, we must call this GadTools refresh
  292.      * function.
  293.      */
  294.     GT_RefreshWindow(mywin, NULL);
  295.  
  296.     SetMenuStrip(mywin, menu);
  297.  
  298.     while (!terminated)
  299.     {
  300.     Wait (1 << mywin->UserPort->mp_SigBit);
  301.     /* GT_GetIMsg() returns a cooked-up IntuiMessage with
  302.      * more friendly information for complex gadget classes.  Use
  303.      * it wherever you get IntuiMessages:
  304.      */
  305.     while ((!terminated) && (imsg = GT_GetIMsg(mywin->UserPort)))
  306.     {
  307.         imsgClass = imsg->Class;
  308.         imsgCode = imsg->Code;
  309.         /* Presuming a gadget, of course, but no harm... */
  310.         gad = (struct Gadget *)imsg->IAddress;
  311.         /* Use the toolkit message-replying function here... */
  312.         GT_ReplyIMsg(imsg);
  313.         switch (imsgClass)
  314.         {
  315.         case MENUPICK:
  316.             terminated = HandleMenuEvent(imsgCode);
  317.             break;
  318.  
  319.         case MOUSEMOVE:
  320.             terminated = HandleMouseMove(gad, imsgCode);
  321.             break;
  322.  
  323.         case GADGETUP:
  324.             printf("GADGETUP.  ");
  325.             terminated = HandleGadgetEvent(gad, imsgCode);
  326.             break;
  327.  
  328.         case GADGETDOWN:
  329.             printf("GADGETDOWN.  ");
  330.             terminated = HandleGadgetEvent(gad, imsgCode);
  331.             break;
  332.  
  333.         case CLOSEWINDOW:
  334.             printf("CLOSEWINDOW.\n");
  335.             terminated = TRUE;
  336.             break;
  337.  
  338.         case REFRESHWINDOW:
  339.             printf("REFRESHWINDOW.\n");
  340.             /* You must use GT_BeginRefresh() where you would
  341.              * normally have your first BeginRefresh()
  342.              */
  343.             GT_BeginRefresh(mywin);
  344.             GT_EndRefresh(mywin, TRUE);
  345.             break;
  346.         }
  347.     }
  348.     }
  349.     bail_out(0, NULL);
  350. return 0;
  351. }
  352.  
  353. /*------------------------------------------------------------------------*/
  354.  
  355. /*/ bail_out()
  356.  *
  357.  * Function to close down or free any opened or allocated stuff, and then
  358.  * exit.
  359.  *
  360.  */
  361.  
  362. void bail_out( int code, STRPTR error)
  363. {
  364.  
  365.     if (mywin)
  366.     {
  367.     ClearMenuStrip(mywin);
  368.     CloseWindow(mywin);
  369.     }
  370.  
  371.     /* None of these three calls mind a NULL parameter, so it's not
  372.      * necessary to check for non-NULL before calling.  If we do that,
  373.      * we must be certain that the OpenLibrary() of GadTools succeeded,
  374.      * or else we would be jumping into outer space:
  375.      */
  376.     if (GadToolsBase)
  377.     {
  378.     FreeMenus(menu);
  379.     FreeVisualInfo(vi);
  380.     FreeGadgets(glist);
  381.     CloseLibrary(GadToolsBase);
  382.     }
  383.  
  384.     if (mysc)
  385.     {
  386.     UnlockPubScreen(NULL, mysc);
  387.     }
  388.  
  389.     if (RKey)
  390.     {
  391.     FreeRemember(&RKey, TRUE);
  392.     }
  393.  
  394.     if (font)
  395.     {
  396.         CloseFont(font);
  397.     }
  398.  
  399.  
  400.     if (IntuitionBase)
  401.     {
  402.     CloseLibrary( ( struct Library *)IntuitionBase);
  403.     }
  404.  
  405.     if (GfxBase)
  406.     {
  407.     CloseLibrary( ( struct Library *)GfxBase);
  408.     }
  409.  
  410.     if (error)
  411.     {
  412.     printf("Error: %s\n", error);
  413.     }
  414.     exit(code);
  415. }
  416.  
  417.  
  418. /*------------------------------------------------------------------------*/
  419.  
  420. /*/ HandleMenuEvent()
  421.  *
  422.  * This function handles IntuiMessage events of type MENUPICK.  It
  423.  * demonstrates the two most common uses for the Menu UserData field
  424.  * provided only by CreateMenus(), namely a place to store
  425.  * pointers-to-functions (for menu # 0), or as a place to store a
  426.  * constant to switch on (for menu # 1).
  427.  *
  428.  */
  429.  
  430. BOOL HandleMenuEvent( UWORD code)
  431. {
  432.     struct MenuItem *item;
  433.     BOOL terminated = FALSE;
  434.  
  435.     printf("MENUPICK.  ");
  436.     /* Get all menu events including NextEvents until a terminating
  437.      * selection is made (such as Quit)
  438.      */
  439.     while ((code != MENUNULL) && (!terminated))
  440.     {
  441.     item = ItemAddress(menu, code);
  442.     if (MENUNUM(code) != 0)
  443.     {
  444.         /* I made menu #1's UserData into constants to switch on.
  445.          * Note that in C, switch demands an "int":
  446.          */
  447.         switch ( (int)MENU_USERDATA(item) )
  448.         {
  449.             case MENU_FOO_SET:
  450.             printf("Foo Set.  ");
  451.             /* Set the Foo checkbox */
  452.             GT_SetGadgetAttrs(checkgad, mywin, NULL,
  453.             GTCB_Checked, TRUE,
  454.             TAG_DONE);
  455.             foochecked = TRUE;
  456.             break;
  457.  
  458.         case MENU_FOO_CLEAR:
  459.             printf("Foo Clear.  ");
  460.             /* Clear the Foo checkbox */
  461.             GT_SetGadgetAttrs(checkgad, mywin, NULL,
  462.             GTCB_Checked, FALSE,
  463.             TAG_DONE);
  464.             foochecked = FALSE;
  465.             break;
  466.  
  467.         case MENU_FOO_TOGGLE:
  468.             printf("Foo Toggle.  ");
  469.             /* Toggle the foo checkbox */
  470.             foochecked = !foochecked;
  471.             GT_SetGadgetAttrs(checkgad, mywin, NULL,
  472.             GTCB_Checked, foochecked,
  473.             TAG_DONE);
  474.             break;
  475.  
  476.         case MENU_INCREASE:
  477.         printf("Slider Increase.  ");
  478.             if (sliderlevel < SLIDER_MAX)
  479.             {
  480.             sliderlevel += incr;
  481.             if (sliderlevel > SLIDER_MAX)
  482.                 sliderlevel = SLIDER_MAX;
  483.             /* Move the slider */
  484.             GT_SetGadgetAttrs(slidergad, mywin, NULL,
  485.                 GTSL_Level, sliderlevel,
  486.                 TAG_DONE);
  487.             }
  488.             break;
  489.  
  490.         case MENU_DECREASE:
  491.             printf("Slider Decrease.  ");
  492.             if (sliderlevel > SLIDER_MIN)
  493.             {
  494.             sliderlevel -= incr;
  495.             if (sliderlevel < SLIDER_MIN)
  496.                 sliderlevel = SLIDER_MIN;
  497.             /* Move the slider */
  498.             GT_SetGadgetAttrs(slidergad, mywin, NULL,
  499.                 GTSL_Level, sliderlevel,
  500.                 TAG_DONE);
  501.             }
  502.             break;
  503.  
  504.         case MENU_BY1:
  505.             printf("Change Slider By 1's.  ");
  506.             incr = 1;
  507.             break;
  508.  
  509.         case MENU_BY5:
  510.             printf("Change Slider By 5's.  ");
  511.             incr = 5;
  512.             break;
  513.  
  514.         case MENU_BY10:
  515.             printf("Change Slider By 10's.  ");
  516.             incr = 10;
  517.             break;
  518.  
  519.         case MENU_CONNECTED:
  520.             printf("MX Gadgets Connected?");
  521.             connected = (item->Flags & CHECKED);
  522.             break;
  523.  
  524.         case MENU_NEVER:
  525.             /* As this item is disabled, this switch can
  526.              * never happen
  527.              */
  528.             printf("Can't Get Me!  ");
  529.             break;
  530.         }
  531.     }
  532.     /* There may be more menu selections to process */
  533.     code = item->NextSelect;
  534.     }
  535.     printf("\n");
  536.  
  537.     return(terminated);
  538. }
  539.  
  540.  
  541. /*------------------------------------------------------------------------*/
  542.  
  543. /*/ OpenFunc()
  544.  *
  545.  * Function that prints out a message and returns.
  546.  *
  547.  */
  548.  
  549. BOOL OpenFunc( UWORD code)
  550. {
  551.     printf("Called Open Function.  ");
  552.     return(FALSE);
  553. }
  554.  
  555. /*/ SaveFunc()
  556.  *
  557.  * Function that prints out a message and returns.
  558.  *
  559.  */
  560.  
  561. BOOL SaveFunc( UWORD code)
  562. {
  563.     printf("Called Save Function.  ");
  564.     return(FALSE);
  565. }
  566.  
  567. /*/ SaveAsFunc()
  568.  *
  569.  * Function that prints out a message and returns.
  570.  *
  571.  */
  572.  
  573. BOOL SaveAsFunc( UWORD code)
  574. {
  575.     printf("Called SaveAs Function.  ");
  576.     return(FALSE);
  577. }
  578.  
  579. /*/ PrintFunc()
  580.  *
  581.  * Function that can tell which sub-item was selected, and print
  582.  * different messages accordingly.
  583.  *
  584.  */
  585.  
  586. BOOL PrintFunc( UWORD code)
  587. {
  588.     printf("Called Print Function - ");
  589.     if (SUBNUM(code) == 0)
  590.     printf("NLQ.  ");
  591.     else
  592.     printf("Draft.  ");
  593.     return(FALSE);
  594. }
  595.  
  596. /*/ QuitFunc()
  597.  *
  598.  * Function that prints out a message and returns TRUE, which, by our
  599.  * convention, signifies an action which should terminate the program.
  600.  *
  601.  */
  602.  
  603. BOOL QuitFunc( UWORD code)
  604. {
  605.     printf("Called Quit Function.  ");
  606.     return(TRUE);
  607. }
  608.  
  609. /*------------------------------------------------------------------------*/
  610.  
  611. /*/ HandleMouseMove()
  612.  *
  613.  * Function to handle MOUSEMOVE events.  For toolkit gadgets, such events
  614.  * have a pointer to the gadget in the IAddress field of the IntuiMessage.
  615.  * (This is not true for MOUSEMOVEs from Intuition gadgets in general).
  616.  * This function could have been folded into HandleGadgetEvent().
  617.  *
  618.  */
  619.  
  620. BOOL HandleMouseMove( struct Gadget *gad,  UWORD code)
  621. {
  622.     BOOL terminated = FALSE;
  623.  
  624.     printf("MOUSEMOVE.  ");
  625.     switch (gad->GadgetID)
  626.     {
  627.     case GAD_SLIDER:
  628.         /* Slider level is in code.  Note
  629.          * that level is a SIGNED word, while Code
  630.          * is a UWORD.  Casting it back to WORD
  631.          * gives you back the sign.  I know it's
  632.          * a bit ugly - I'll think about other
  633.          * ways.
  634.          */
  635.         printf("Slider Level: %d\n", (WORD)code);
  636.         sliderlevel = (WORD)code;
  637.         break;
  638.     case GAD_SCROLLER:
  639.         /* Scroller level is in code: */
  640.         printf("Scroller Level: %d\n", code);
  641.         break;
  642.     }
  643.     return(terminated);
  644. }
  645.  
  646.  
  647. /*------------------------------------------------------------------------*/
  648.  
  649. /*/ HandleGadgetEvent()
  650.  *
  651.  * Function to handle a GADGETUP or GADGETDOWN event.  For toolkit gadgets,
  652.  * it is possible to use this function to handle MOUSEMOVEs as well, with
  653.  * little or no work.
  654.  *
  655.  */
  656.  
  657. BOOL HandleGadgetEvent( struct Gadget *gad, UWORD code)
  658. {
  659.     BOOL terminated = FALSE;
  660.  
  661.     switch (gad->GadgetID)
  662.     {
  663.     case GAD_BUTTON:
  664.         /* Buttons always report GADGETUP's, nothing
  665.          * fancy or different here
  666.          */
  667.         printf("Button 'ClickMe'.\n");
  668.         /* Demonstrating the ability to change the
  669.  
  670.          * displayed value in an integer gadget, we do
  671.          * this on every click of this button:
  672.          */
  673.         GT_SetGadgetAttrs(integergad, mywin, NULL,
  674.         GTIN_Number, 271828,
  675.         TAG_DONE);
  676.         break;
  677.  
  678.     case GAD_INTEGER:
  679.         /* String/Integer gadgets report GADGETUP's,
  680.          * fancy or different here:
  681.          */
  682.         printf("Integer gadget: %ld.\n",
  683.         ((struct StringInfo *)gad->SpecialInfo)->LongInt);
  684.         break;
  685.  
  686.     case GAD_CHECKBOX1:
  687.         /* Checkboxes report GADGETUP's, nothing
  688.          * fancy or different here:
  689.          */
  690.         printf("Foo is ");
  691.         if (!(gad->Flags & SELECTED))
  692.         {
  693.         foochecked = FALSE;
  694.         printf("not ");
  695.         }
  696.         else
  697.         {
  698.         foochecked = TRUE;
  699.         }
  700.         printf("selected.\n");
  701.         break;
  702.  
  703.     case GAD_CHECKBOX2:
  704.         printf("Bar is ");
  705.         if (!(gad->Flags & SELECTED))
  706.         printf("not ");
  707.         printf("selected.\n");
  708.         break;
  709.  
  710.     case GAD_CYCLE:
  711.         /* Cycle gadgets report the number (0..n-1)
  712.          * of the new active label in the code
  713.          * field:
  714.          */
  715.         printf("Cycle: '%s'.\n", MonthLabels[code]);
  716.         /* Here we demonstrate the ability to set
  717.          * the active choice in a set of Mutually
  718.          * Exclusive gadgets:
  719.          */
  720.         if ((connected) && (code < 7))
  721.         GT_SetGadgetAttrs(mxgad, mywin, NULL,
  722.             GTMX_Active, code,
  723.             TAG_DONE);
  724.         break;
  725.  
  726.     case GAD_MX:
  727.         /* MX gadgets report the number (0..n-1)
  728.          * of the new active label in the code
  729.          * field:
  730.          */
  731.         printf("MX: Day-of-week '%s'\n", DayLabels[code]);
  732.         /* Here we demonstrate the ability to set
  733.          * the active choice in an Cycle gadget:
  734.          */
  735.         if (connected)
  736.         GT_SetGadgetAttrs(cyclegad, mywin, NULL,
  737.             GTCY_Active, code,
  738.             TAG_DONE);
  739.         break;
  740.  
  741.     case GAD_SLIDER:
  742.         /* Slider level is in code.  Note that
  743.          * level is a signed WORD, while Code is a
  744.          * UWORD.  Casting it back to WORD gives
  745.          * you back the sign.
  746.          */
  747.         printf("Slider Level: %d\n", (WORD)code);
  748.         sliderlevel = (WORD)code;
  749.         if ((sliderlevel >= 0) && (sliderlevel <= 12))
  750.         {
  751.         GT_SetGadgetAttrs(lvgad, mywin, NULL,
  752.             GTLV_Selected, code,
  753.             TAG_DONE);
  754.         }
  755.         break;
  756.  
  757.     case GAD_SCROLLER:
  758.         /* Scroller level is in code: */
  759.         printf("Scroller Level: %d\n", code);
  760.         break;
  761.  
  762.     case GAD_PALETTE:
  763.         /* Palette's color is in code: */
  764.         printf("Palette:  selected color %d\n", code);
  765.         break;
  766.  
  767.     case GAD_LVSTRING:
  768.         /* String gadgets report GADGETUP's, nothing
  769.          * fancy or different here:
  770.          */
  771.         printf("LVString: '%s'.\n",
  772.         ((struct StringInfo *)gad->SpecialInfo)->Buffer);
  773.         break;
  774.  
  775.     case GAD_LISTVIEW:
  776.         /* ListView ordinal count is in code: */
  777.         printf("ListView: clicked on '%s'\n", MonthLabels[code]);
  778.         break;
  779.     }
  780.     return(terminated);
  781. }
  782.  
  783.  
  784. /*------------------------------------------------------------------------*/
  785.  
  786. /*/ CreateAllGadgets()
  787.  *
  788.  * Here is where all the initialization and creation of toolkit gadgets
  789.  * take place.  This function requires a pointer to a NULL-initialized
  790.  * gadget list pointer.  It returns a pointer to the last created gadget,
  791.  * which can be checked for success/failure.
  792.  *
  793.  */
  794.  
  795. struct Gadget *CreateAllGadgets( struct Gadget **glistptr, void *vi, UWORD topborder)
  796. {
  797.     struct NewGadget ng;
  798.  
  799.     struct Gadget *gad;
  800.  
  801.     WORD index;
  802.     struct Node *node;
  803.  
  804.     /* All the gadget creation calls accept a pointer to the previous
  805.      * gadget, and link the new gadget to that gadget's NextGadget field.
  806.      * Also, they exit gracefully, returning NULL, if any previous gadget
  807.      * was NULL.  This limits the amount of checking for failure that
  808.      * is needed.  You only need to check before you tweak any gadget
  809.      * structure or use any of its fields, and finally once at the end,
  810.      * before you add the gadgets.
  811.      */
  812.  
  813.     /* We obligingly perform the following operation, required of
  814.      * any program that uses the toolkit.  It gives the toolkit a
  815.      * place to stuff context data:
  816.      */
  817.     gad = CreateContext(glistptr);
  818.     /* Fill out a NewGadget structure to describe the gadget we want
  819.      * to create:
  820.      */
  821.     /* Create a centered label (read-only) */
  822.     ng.ng_LeftEdge = 300;
  823.     ng.ng_TopEdge = 4+topborder;
  824.     ng.ng_Width = 0;
  825.     ng.ng_Height = 8;
  826.     ng.ng_GadgetText = "Gadget Toolkit Test";
  827.     ng.ng_TextAttr = &Topaz80;
  828.     ng.ng_GadgetID = 0;
  829.     ng.ng_Flags = PLACETEXT_IN | NG_HIGHLABEL;
  830.     ng.ng_VisualInfo = vi;
  831.     /* Text-Only gadget with GadgetText but no other text: */
  832.     gad = CreateGadget(TEXT_KIND, gad, &ng,
  833.     TAG_DONE);
  834.  
  835.     /* Since the NewGadget structure is unmodified by any of the
  836.      * CreateGadget() calls, we need only change those fields which
  837.      * are different.
  838.      */
  839.     ng.ng_LeftEdge = 10;
  840.     ng.ng_TopEdge = 19+topborder;
  841.     ng.ng_Width = 100;
  842.     ng.ng_Height = 12;
  843.     ng.ng_GadgetText = "ClickMe";
  844.     ng.ng_GadgetID = GAD_BUTTON;
  845.     ng.ng_Flags = 0;
  846.     gad = CreateGadget(BUTTON_KIND, gad, &ng,
  847.     TAG_DONE);
  848.  
  849.     ng.ng_LeftEdge = 400;
  850.     ng.ng_Height = 14;
  851.     ng.ng_GadgetText = "Month:";
  852.     ng.ng_GadgetID = GAD_CYCLE;
  853.     ng.ng_Flags = NG_HIGHLABEL;
  854.     cyclegad = gad = CreateGadget(CYCLE_KIND, gad, &ng,
  855.     GTCY_Labels, MonthLabels,
  856.     GTCY_Active, 3,
  857.     TAG_DONE);
  858.  
  859.     ng.ng_TopEdge = 69+topborder;
  860.     ng.ng_LeftEdge = 70;
  861.     ng.ng_GadgetText = "Foo:";
  862.     ng.ng_GadgetID = GAD_CHECKBOX1;
  863.     checkgad = gad = CreateGadget(CHECKBOX_KIND, gad, &ng,
  864.     GTCB_Checked, foochecked,
  865.     TAG_DONE);
  866.  
  867.     if (gad)
  868.     ng.ng_TopEdge += gad->Height;
  869.  
  870.     ng.ng_GadgetText = "Bar:";
  871.     ng.ng_GadgetID = GAD_CHECKBOX2;
  872.     gad = CreateGadget(CHECKBOX_KIND, gad, &ng,
  873.     GTCB_Checked, FALSE,
  874.     TAG_DONE);
  875.  
  876.     ng.ng_TopEdge = 99+topborder;
  877.     ng.ng_Width = 200;
  878.     ng.ng_GadgetText = "Type:";
  879.     ng.ng_GadgetID = GAD_INTEGER;
  880.     integergad = gad = CreateGadget(INTEGER_KIND, gad, &ng,
  881.     GTIN_Number, 54321,
  882.     GTIN_MaxChars, 10,
  883.     TAG_DONE);
  884.  
  885.     ng.ng_TopEdge = 117+topborder;
  886.     ng.ng_Height = 12;
  887.     ng.ng_GadgetText = "L:   ";
  888.     ng.ng_GadgetID = GAD_SLIDER;
  889.     slidergad = gad = CreateGadget(SLIDER_KIND, gad, &ng,
  890.     GTSL_Min, SLIDER_MIN,
  891.     GTSL_Max, SLIDER_MAX,
  892.     GTSL_Level, sliderlevel,
  893.     GTSL_LevelFormat, "%2ld",
  894.     GTSL_LevelPlace, PLACETEXT_LEFT,
  895.     GTSL_MaxLevelLen, 2,
  896.     GA_IMMEDIATE, TRUE,
  897.     GA_RELVERIFY, TRUE,
  898.     TAG_DONE);
  899.  
  900.     ng.ng_TopEdge = 133+topborder;
  901.     ng.ng_GadgetText = "Scroll:";
  902.     ng.ng_GadgetID = GAD_SCROLLER;
  903.     gad = CreateGadget(SCROLLER_KIND, gad, &ng,
  904.     GTSC_Top, 5,
  905.     GTSC_Total, 30,
  906.     GTSC_Visible, 10,
  907.     GTSC_Arrows, 13,
  908.     GA_RELVERIFY, TRUE,
  909.     TAG_DONE);
  910.  
  911.     ng.ng_TopEdge = 149+topborder;
  912.     ng.ng_Height = 8;
  913.     ng.ng_GadgetText = "Number:";
  914.     gad = CreateGadget(NUMBER_KIND, gad, &ng,
  915.     GTNM_Number, 314159,
  916.     TAG_DONE);
  917.  
  918.     ng.ng_LeftEdge = 400;
  919.     ng.ng_GadgetText = "Read:";
  920.     gad = CreateGadget(TEXT_KIND, gad, &ng,
  921.     GTTX_Text, "Read-Only Field!",
  922.     TAG_DONE);
  923.  
  924.     ng.ng_LeftEdge = 470;
  925.     ng.ng_TopEdge = 49+topborder;
  926.     ng.ng_GadgetID = GAD_MX;
  927.     mxgad = gad = CreateGadget(MX_KIND, gad, &ng,
  928.     GTMX_Labels, DayLabels,
  929.     GTMX_Active, 0,
  930.     GTMX_Spacing, 4,
  931.     TAG_DONE);
  932.  
  933.     NewList(&lh);
  934.     index = 0;
  935.     while (MonthLabels[index])
  936.     {
  937.     node = (struct Node *)AllocRemember(&RKey, sizeof(struct Node), MEMF_CLEAR);
  938.     node->ln_Name = MonthLabels[index++];
  939.      AddTail(&lh, node);
  940.     }
  941.  
  942.     /* Here's a string gadget to be attached to the listview: */
  943.     ng.ng_Width = 150;
  944.     ng.ng_Height = 14;
  945.     ng.ng_GadgetText = NULL;
  946.     ng.ng_GadgetID = GAD_LVSTRING;
  947.     stringgad = gad = CreateGadget(STRING_KIND, gad, &ng,
  948.     GTST_MaxChars, 50,
  949.     TAG_DONE);
  950.  
  951.     ng.ng_LeftEdge = 130;
  952.     ng.ng_TopEdge = 19+topborder;
  953.     ng.ng_Width = 150;
  954.     ng.ng_Height = 57;
  955.     ng.ng_GadgetText = "Months:";
  956.     ng.ng_GadgetID = GAD_LISTVIEW;
  957.     ng.ng_Flags = NG_HIGHLABEL|PLACETEXT_LEFT;
  958.     lvgad = gad = CreateGadget(LISTVIEW_KIND, gad, &ng,
  959.     GTLV_Labels, &lh,
  960.     GTLV_Top, 1,
  961.     LAYOUTA_SPACING, 1,
  962.     GTLV_ShowSelected, stringgad,
  963.     GTLV_Selected, 3,
  964.     GTLV_ScrollWidth, 18,
  965.     TAG_DONE);
  966.  
  967.     ng.ng_LeftEdge = 320;
  968.     ng.ng_TopEdge = 49+topborder;
  969.     ng.ng_Width = 40;
  970.     ng.ng_Height = 75;
  971.     ng.ng_GadgetText = "Colors";
  972.     ng.ng_GadgetID = GAD_PALETTE;
  973.     ng.ng_Flags = NG_HIGHLABEL;
  974.  
  975.     gad = CreateGadget(PALETTE_KIND, gad, &ng,
  976.     GTPA_Depth, mysc->BitMap.Depth,
  977.     GTPA_Color, 1,
  978.     GTPA_ColorOffset, 0,
  979.     GTPA_IndicatorHeight, 15,
  980.     TAG_DONE);
  981.  
  982.     return(gad);
  983. }
  984.  
  985. /*------------------------------------------------------------------------*/
  986.