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