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 / gadtools1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  24.4 KB  |  1,031 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.  */
  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. void 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. }
  351.  
  352. /*------------------------------------------------------------------------*/
  353.  
  354. /*/ bail_out()
  355.  *
  356.  * Function to close down or free any opened or allocated stuff, and then
  357.  * exit.
  358.  *
  359.  */
  360.  
  361. void bail_out(code, error)
  362.  
  363.     int code;
  364.     STRPTR error;
  365.  
  366. {
  367.  
  368.     if (mywin)
  369.     {
  370.     ClearMenuStrip(mywin);
  371.     CloseWindow(mywin);
  372.     }
  373.  
  374.     /* None of these three calls mind a NULL parameter, so it's not
  375.      * necessary to check for non-NULL before calling.  If we do that,
  376.      * we must be certain that the OpenLibrary() of GadTools succeeded,
  377.      * or else we would be jumping into outer space:
  378.      */
  379.     if (GadToolsBase)
  380.     {
  381.     FreeMenus(menu);
  382.     FreeVisualInfo(vi);
  383.     FreeGadgets(glist);
  384.     CloseLibrary(GadToolsBase);
  385.     }
  386.  
  387.     if (mysc)
  388.     {
  389.     UnlockPubScreen(NULL, mysc);
  390.     }
  391.  
  392.     if (RKey)
  393.     {
  394.     FreeRemember(&RKey, TRUE);
  395.     }
  396.  
  397.     if (font)
  398.     {
  399.         CloseFont(font);
  400.     }
  401.  
  402.  
  403.     if (IntuitionBase)
  404.     {
  405.     CloseLibrary(IntuitionBase);
  406.     }
  407.  
  408.     if (GfxBase)
  409.     {
  410.     CloseLibrary(GfxBase);
  411.     }
  412.  
  413.     if (error)
  414.     {
  415.     printf("Error: %s\n", error);
  416.     }
  417.     exit(code);
  418. }
  419.  
  420.  
  421. /*------------------------------------------------------------------------*/
  422.  
  423. /*/ HandleMenuEvent()
  424.  *
  425.  * This function handles IntuiMessage events of type MENUPICK.  It
  426.  * demonstrates the two most common uses for the Menu UserData field
  427.  * provided only by CreateMenus(), namely a place to store
  428.  * pointers-to-functions (for menu # 0), or as a place to store a
  429.  * constant to switch on (for menu # 1).
  430.  *
  431.  */
  432.  
  433. BOOL HandleMenuEvent(code)
  434.  
  435.     UWORD code;
  436.  
  437. {
  438.     struct MenuItem *item;
  439.     BOOL terminated = FALSE;
  440.  
  441.     printf("MENUPICK.  ");
  442.     /* Get all menu events including NextEvents until a terminating
  443.      * selection is made (such as Quit)
  444.      */
  445.     while ((code != MENUNULL) && (!terminated))
  446.     {
  447.     item = ItemAddress(menu, code);
  448.     if (MENUNUM(code) == 0)
  449.     {
  450.         /* I made menu # 0's UserData into function vectors.
  451.          * This is a good way to extract them and launch them
  452.          */
  453.         BOOL (*fptr)(UWORD) = MENU_USERDATA(item);
  454.         /* Invoke that function with the code, provided function exists. */
  455.         if (fptr)
  456.         {
  457.         terminated = (*fptr)(code);
  458.         }
  459.     }
  460.     else
  461.     {
  462.         /* I made menu #1's UserData into constants to switch on.
  463.          * Note that in C, switch demands an "int":
  464.          */
  465.         switch ( (int)MENU_USERDATA(item) )
  466.         {
  467.             case MENU_FOO_SET:
  468.             printf("Foo Set.  ");
  469.             /* Set the Foo checkbox */
  470.             GT_SetGadgetAttrs(checkgad, mywin, NULL,
  471.             GTCB_Checked, TRUE,
  472.             TAG_DONE);
  473.             foochecked = TRUE;
  474.             break;
  475.  
  476.         case MENU_FOO_CLEAR:
  477.             printf("Foo Clear.  ");
  478.             /* Clear the Foo checkbox */
  479.             GT_SetGadgetAttrs(checkgad, mywin, NULL,
  480.             GTCB_Checked, FALSE,
  481.             TAG_DONE);
  482.             foochecked = FALSE;
  483.             break;
  484.  
  485.         case MENU_FOO_TOGGLE:
  486.             printf("Foo Toggle.  ");
  487.             /* Toggle the foo checkbox */
  488.             foochecked = !foochecked;
  489.             GT_SetGadgetAttrs(checkgad, mywin, NULL,
  490.             GTCB_Checked, foochecked,
  491.             TAG_DONE);
  492.             break;
  493.  
  494.         case MENU_INCREASE:
  495.         printf("Slider Increase.  ");
  496.             if (sliderlevel < SLIDER_MAX)
  497.             {
  498.             sliderlevel += incr;
  499.             if (sliderlevel > SLIDER_MAX)
  500.                 sliderlevel = SLIDER_MAX;
  501.             /* Move the slider */
  502.             GT_SetGadgetAttrs(slidergad, mywin, NULL,
  503.                 GTSL_Level, sliderlevel,
  504.                 TAG_DONE);
  505.             }
  506.             break;
  507.  
  508.         case MENU_DECREASE:
  509.             printf("Slider Decrease.  ");
  510.             if (sliderlevel > SLIDER_MIN)
  511.             {
  512.             sliderlevel -= incr;
  513.             if (sliderlevel < SLIDER_MIN)
  514.                 sliderlevel = SLIDER_MIN;
  515.             /* Move the slider */
  516.             GT_SetGadgetAttrs(slidergad, mywin, NULL,
  517.                 GTSL_Level, sliderlevel,
  518.                 TAG_DONE);
  519.             }
  520.             break;
  521.  
  522.         case MENU_BY1:
  523.             printf("Change Slider By 1's.  ");
  524.             incr = 1;
  525.             break;
  526.  
  527.         case MENU_BY5:
  528.             printf("Change Slider By 5's.  ");
  529.             incr = 5;
  530.             break;
  531.  
  532.         case MENU_BY10:
  533.             printf("Change Slider By 10's.  ");
  534.             incr = 10;
  535.             break;
  536.  
  537.         case MENU_CONNECTED:
  538.             printf("MX Gadgets Connected?");
  539.             connected = (item->Flags & CHECKED);
  540.             break;
  541.  
  542.         case MENU_NEVER:
  543.             /* As this item is disabled, this switch can
  544.              * never happen
  545.              */
  546.             printf("Can't Get Me!  ");
  547.             break;
  548.         }
  549.     }
  550.     /* There may be more menu selections to process */
  551.     code = item->NextSelect;
  552.     }
  553.     printf("\n");
  554.  
  555.     return(terminated);
  556. }
  557.  
  558.  
  559. /*------------------------------------------------------------------------*/
  560.  
  561. /*/ OpenFunc()
  562.  *
  563.  * Function that prints out a message and returns.
  564.  *
  565.  */
  566.  
  567. BOOL OpenFunc(code)
  568.  
  569.     UWORD code;
  570.  
  571. {
  572.     printf("Called Open Function.  ");
  573.     return(FALSE);
  574. }
  575.  
  576. /*/ SaveFunc()
  577.  *
  578.  * Function that prints out a message and returns.
  579.  *
  580.  */
  581.  
  582. BOOL SaveFunc(code)
  583.  
  584.     UWORD code;
  585.  
  586. {
  587.     printf("Called Save Function.  ");
  588.     return(FALSE);
  589. }
  590.  
  591. /*/ SaveAsFunc()
  592.  *
  593.  * Function that prints out a message and returns.
  594.  *
  595.  */
  596.  
  597. BOOL SaveAsFunc(code)
  598.  
  599.     UWORD code;
  600.  
  601. {
  602.     printf("Called SaveAs Function.  ");
  603.     return(FALSE);
  604. }
  605.  
  606. /*/ PrintFunc()
  607.  *
  608.  * Function that can tell which sub-item was selected, and print
  609.  * different messages accordingly.
  610.  *
  611.  */
  612.  
  613. BOOL PrintFunc(code)
  614.  
  615.     UWORD code;
  616.  
  617. {
  618.     printf("Called Print Function - ");
  619.     if (SUBNUM(code) == 0)
  620.     printf("NLQ.  ");
  621.     else
  622.     printf("Draft.  ");
  623.     return(FALSE);
  624. }
  625.  
  626. /*/ QuitFunc()
  627.  *
  628.  * Function that prints out a message and returns TRUE, which, by our
  629.  * convention, signifies an action which should terminate the program.
  630.  *
  631.  */
  632.  
  633. BOOL QuitFunc(code)
  634.  
  635.     UWORD code;
  636.  
  637. {
  638.     printf("Called Quit Function.  ");
  639.     return(TRUE);
  640. }
  641.  
  642. /*------------------------------------------------------------------------*/
  643.  
  644. /*/ HandleMouseMove()
  645.  *
  646.  * Function to handle MOUSEMOVE events.  For toolkit gadgets, such events
  647.  * have a pointer to the gadget in the IAddress field of the IntuiMessage.
  648.  * (This is not true for MOUSEMOVEs from Intuition gadgets in general).
  649.  * This function could have been folded into HandleGadgetEvent().
  650.  *
  651.  */
  652.  
  653. BOOL HandleMouseMove(gad, code)
  654.  
  655.     struct Gadget *gad;
  656.     UWORD code;
  657.  
  658. {
  659.     BOOL terminated = FALSE;
  660.  
  661.     printf("MOUSEMOVE.  ");
  662.     switch (gad->GadgetID)
  663.     {
  664.     case GAD_SLIDER:
  665.         /* Slider level is in code.  Note
  666.          * that level is a SIGNED word, while Code
  667.          * is a UWORD.  Casting it back to WORD
  668.          * gives you back the sign.  I know it's
  669.          * a bit ugly - I'll think about other
  670.          * ways.
  671.          */
  672.         printf("Slider Level: %d\n", (WORD)code);
  673.         sliderlevel = (WORD)code;
  674.         break;
  675.     case GAD_SCROLLER:
  676.         /* Scroller level is in code: */
  677.         printf("Scroller Level: %d\n", code);
  678.         break;
  679.     }
  680.     return(terminated);
  681. }
  682.  
  683.  
  684. /*------------------------------------------------------------------------*/
  685.  
  686. /*/ HandleGadgetEvent()
  687.  *
  688.  * Function to handle a GADGETUP or GADGETDOWN event.  For toolkit gadgets,
  689.  * it is possible to use this function to handle MOUSEMOVEs as well, with
  690.  * little or no work.
  691.  *
  692.  */
  693.  
  694. BOOL HandleGadgetEvent(gad, code)
  695.  
  696.     struct Gadget *gad;
  697.     UWORD code;
  698.  
  699. {
  700.     BOOL terminated = FALSE;
  701.  
  702.     switch (gad->GadgetID)
  703.     {
  704.     case GAD_BUTTON:
  705.         /* Buttons always report GADGETUP's, nothing
  706.          * fancy or different here
  707.          */
  708.         printf("Button 'ClickMe'.\n");
  709.         /* Demonstrating the ability to change the
  710.          * displayed value in an integer gadget, we do
  711.          * this on every click of this button:
  712.          */
  713.         GT_SetGadgetAttrs(integergad, mywin, NULL,
  714.         GTIN_Number, 271828,
  715.         TAG_DONE);
  716.         break;
  717.  
  718.     case GAD_INTEGER:
  719.         /* String/Integer gadgets report GADGETUP's,
  720.          * fancy or different here:
  721.          */
  722.         printf("Integer gadget: %ld.\n",
  723.         ((struct StringInfo *)gad->SpecialInfo)->LongInt);
  724.         break;
  725.  
  726.     case GAD_CHECKBOX1:
  727.         /* Checkboxes report GADGETUP's, nothing
  728.          * fancy or different here:
  729.          */
  730.         printf("Foo is ");
  731.         if (!(gad->Flags & SELECTED))
  732.         {
  733.         foochecked = FALSE;
  734.         printf("not ");
  735.         }
  736.         else
  737.         {
  738.         foochecked = TRUE;
  739.         }
  740.         printf("selected.\n");
  741.         break;
  742.  
  743.     case GAD_CHECKBOX2:
  744.         printf("Bar is ");
  745.         if (!(gad->Flags & SELECTED))
  746.         printf("not ");
  747.         printf("selected.\n");
  748.         break;
  749.  
  750.     case GAD_CYCLE:
  751.         /* Cycle gadgets report the number (0..n-1)
  752.          * of the new active label in the code
  753.          * field:
  754.          */
  755.         printf("Cycle: '%s'.\n", MonthLabels[code]);
  756.         /* Here we demonstrate the ability to set
  757.          * the active choice in a set of Mutually
  758.          * Exclusive gadgets:
  759.          */
  760.         if ((connected) && (code < 7))
  761.         GT_SetGadgetAttrs(mxgad, mywin, NULL,
  762.             GTMX_Active, code,
  763.             TAG_DONE);
  764.         break;
  765.  
  766.     case GAD_MX:
  767.         /* MX gadgets report the number (0..n-1)
  768.          * of the new active label in the code
  769.          * field:
  770.          */
  771.         printf("MX: Day-of-week '%s'\n", DayLabels[code]);
  772.         /* Here we demonstrate the ability to set
  773.          * the active choice in an Cycle gadget:
  774.          */
  775.         if (connected)
  776.         GT_SetGadgetAttrs(cyclegad, mywin, NULL,
  777.             GTCY_Active, code,
  778.             TAG_DONE);
  779.         break;
  780.  
  781.     case GAD_SLIDER:
  782.         /* Slider level is in code.  Note that
  783.          * level is a signed WORD, while Code is a
  784.          * UWORD.  Casting it back to WORD gives
  785.          * you back the sign.
  786.          */
  787.         printf("Slider Level: %d\n", (WORD)code);
  788.         sliderlevel = (WORD)code;
  789.         if ((sliderlevel >= 0) && (sliderlevel <= 12))
  790.         {
  791.         GT_SetGadgetAttrs(lvgad, mywin, NULL,
  792.             GTLV_Selected, code,
  793.             TAG_DONE);
  794.         }
  795.         break;
  796.  
  797.     case GAD_SCROLLER:
  798.         /* Scroller level is in code: */
  799.         printf("Scroller Level: %d\n", code);
  800.         break;
  801.  
  802.     case GAD_PALETTE:
  803.         /* Palette's color is in code: */
  804.         printf("Palette:  selected color %d\n", code);
  805.         break;
  806.  
  807.     case GAD_LVSTRING:
  808.         /* String gadgets report GADGETUP's, nothing
  809.          * fancy or different here:
  810.          */
  811.         printf("LVString: '%s'.\n",
  812.         ((struct StringInfo *)gad->SpecialInfo)->Buffer);
  813.         break;
  814.  
  815.     case GAD_LISTVIEW:
  816.         /* ListView ordinal count is in code: */
  817.         printf("ListView: clicked on '%s'\n", MonthLabels[code]);
  818.         break;
  819.     }
  820.     return(terminated);
  821. }
  822.  
  823.  
  824. /*------------------------------------------------------------------------*/
  825.  
  826. /*/ CreateAllGadgets()
  827.  *
  828.  * Here is where all the initialization and creation of toolkit gadgets
  829.  * take place.  This function requires a pointer to a NULL-initialized
  830.  * gadget list pointer.  It returns a pointer to the last created gadget,
  831.  * which can be checked for success/failure.
  832.  *
  833.  */
  834.  
  835. struct Gadget *CreateAllGadgets(glistptr, vi, topborder)
  836.  
  837.     struct Gadget **glistptr;
  838.     void *vi;
  839.     UWORD topborder;
  840.  
  841. {
  842.     struct NewGadget ng;
  843.  
  844.     struct Gadget *gad;
  845.  
  846.     WORD index;
  847.     struct Node *node;
  848.  
  849.     /* All the gadget creation calls accept a pointer to the previous
  850.      * gadget, and link the new gadget to that gadget's NextGadget field.
  851.      * Also, they exit gracefully, returning NULL, if any previous gadget
  852.      * was NULL.  This limits the amount of checking for failure that
  853.      * is needed.  You only need to check before you tweak any gadget
  854.      * structure or use any of its fields, and finally once at the end,
  855.      * before you add the gadgets.
  856.      */
  857.  
  858.     /* We obligingly perform the following operation, required of
  859.      * any program that uses the toolkit.  It gives the toolkit a
  860.      * place to stuff context data:
  861.      */
  862.     gad = CreateContext(glistptr);
  863.     /* Fill out a NewGadget structure to describe the gadget we want
  864.      * to create:
  865.      */
  866.     /* Create a centered label (read-only) */
  867.     ng.ng_LeftEdge = 300;
  868.     ng.ng_TopEdge = 4+topborder;
  869.     ng.ng_Width = 0;
  870.     ng.ng_Height = 8;
  871.     ng.ng_GadgetText = "Gadget Toolkit Test";
  872.     ng.ng_TextAttr = &Topaz80;
  873.     ng.ng_GadgetID = 0;
  874.     ng.ng_Flags = PLACETEXT_IN | NG_HIGHLABEL;
  875.     ng.ng_VisualInfo = vi;
  876.     /* Text-Only gadget with GadgetText but no other text: */
  877.     gad = CreateGadget(TEXT_KIND, gad, &ng,
  878.     TAG_DONE);
  879.  
  880.     /* Since the NewGadget structure is unmodified by any of the
  881.      * CreateGadget() calls, we need only change those fields which
  882.      * are different.
  883.      */
  884.     ng.ng_LeftEdge = 10;
  885.     ng.ng_TopEdge = 19+topborder;
  886.     ng.ng_Width = 100;
  887.     ng.ng_Height = 12;
  888.     ng.ng_GadgetText = "ClickMe";
  889.     ng.ng_GadgetID = GAD_BUTTON;
  890.     ng.ng_Flags = 0;
  891.     gad = CreateGadget(BUTTON_KIND, gad, &ng,
  892.     TAG_DONE);
  893.  
  894.     ng.ng_LeftEdge = 400;
  895.     ng.ng_Height = 14;
  896.     ng.ng_GadgetText = "Month:";
  897.     ng.ng_GadgetID = GAD_CYCLE;
  898.     ng.ng_Flags = NG_HIGHLABEL;
  899.     cyclegad = gad = CreateGadget(CYCLE_KIND, gad, &ng,
  900.     GTCY_Labels, MonthLabels,
  901.     GTCY_Active, 3,
  902.     TAG_DONE);
  903.  
  904.     ng.ng_TopEdge = 69+topborder;
  905.     ng.ng_LeftEdge = 70;
  906.     ng.ng_GadgetText = "Foo:";
  907.     ng.ng_GadgetID = GAD_CHECKBOX1;
  908.     checkgad = gad = CreateGadget(CHECKBOX_KIND, gad, &ng,
  909.     GTCB_Checked, foochecked,
  910.     TAG_DONE);
  911.  
  912.     if (gad)
  913.     ng.ng_TopEdge += gad->Height;
  914.  
  915.     ng.ng_GadgetText = "Bar:";
  916.     ng.ng_GadgetID = GAD_CHECKBOX2;
  917.     gad = CreateGadget(CHECKBOX_KIND, gad, &ng,
  918.     GTCB_Checked, FALSE,
  919.     TAG_DONE);
  920.  
  921.     ng.ng_TopEdge = 99+topborder;
  922.     ng.ng_Width = 200;
  923.     ng.ng_GadgetText = "Type:";
  924.     ng.ng_GadgetID = GAD_INTEGER;
  925.     integergad = gad = CreateGadget(INTEGER_KIND, gad, &ng,
  926.     GTIN_Number, 54321,
  927.     GTIN_MaxChars, 10,
  928.     TAG_DONE);
  929.  
  930.     ng.ng_TopEdge = 117+topborder;
  931.     ng.ng_Height = 12;
  932.     ng.ng_GadgetText = "L:   ";
  933.     ng.ng_GadgetID = GAD_SLIDER;
  934.     slidergad = gad = CreateGadget(SLIDER_KIND, gad, &ng,
  935.     GTSL_Min, SLIDER_MIN,
  936.     GTSL_Max, SLIDER_MAX,
  937.     GTSL_Level, sliderlevel,
  938.     GTSL_LevelFormat, "%2ld",
  939.     GTSL_LevelPlace, PLACETEXT_LEFT,
  940.     GTSL_MaxLevelLen, 2,
  941.     GA_IMMEDIATE, TRUE,
  942.     GA_RELVERIFY, TRUE,
  943.     TAG_DONE);
  944.  
  945.     ng.ng_TopEdge = 133+topborder;
  946.     ng.ng_GadgetText = "Scroll:";
  947.     ng.ng_GadgetID = GAD_SCROLLER;
  948.     gad = CreateGadget(SCROLLER_KIND, gad, &ng,
  949.     GTSC_Top, 5,
  950.     GTSC_Total, 30,
  951.     GTSC_Visible, 10,
  952.     GTSC_Arrows, 13,
  953.     GA_RELVERIFY, TRUE,
  954.     TAG_DONE);
  955.  
  956.     ng.ng_TopEdge = 149+topborder;
  957.     ng.ng_Height = 8;
  958.     ng.ng_GadgetText = "Number:";
  959.     gad = CreateGadget(NUMBER_KIND, gad, &ng,
  960.     GTNM_Number, 314159,
  961.     TAG_DONE);
  962.  
  963.     ng.ng_LeftEdge = 400;
  964.     ng.ng_GadgetText = "Read:";
  965.     gad = CreateGadget(TEXT_KIND, gad, &ng,
  966.     GTTX_Text, "Read-Only Field!",
  967.     TAG_DONE);
  968.  
  969.     ng.ng_LeftEdge = 470;
  970.     ng.ng_TopEdge = 49+topborder;
  971.     ng.ng_GadgetID = GAD_MX;
  972.     mxgad = gad = CreateGadget(MX_KIND, gad, &ng,
  973.     GTMX_Labels, DayLabels,
  974.     GTMX_Active, 0,
  975.     GTMX_Spacing, 4,
  976.     TAG_DONE);
  977.  
  978.     NewList(&lh);
  979.     index = 0;
  980.     while (MonthLabels[index])
  981.     {
  982.     node = (struct Node *)AllocRemember(&RKey, sizeof(struct Node), MEMF_CLEAR);
  983.     node->ln_Name = MonthLabels[index++];
  984.      AddTail(&lh, node);
  985.     }
  986.  
  987.     /* Here's a string gadget to be attached to the listview: */
  988.     ng.ng_Width = 150;
  989.     ng.ng_Height = 14;
  990.     ng.ng_GadgetText = NULL;
  991.     ng.ng_GadgetID = GAD_LVSTRING;
  992.     stringgad = gad = CreateGadget(STRING_KIND, gad, &ng,
  993.     GTST_MaxChars, 50,
  994.     TAG_DONE);
  995.  
  996.     ng.ng_LeftEdge = 130;
  997.     ng.ng_TopEdge = 19+topborder;
  998.     ng.ng_Width = 150;
  999.     ng.ng_Height = 57;
  1000.     ng.ng_GadgetText = "Months:";
  1001.     ng.ng_GadgetID = GAD_LISTVIEW;
  1002.     ng.ng_Flags = NG_HIGHLABEL|PLACETEXT_LEFT;
  1003.     lvgad = gad = CreateGadget(LISTVIEW_KIND, gad, &ng,
  1004.     GTLV_Labels, &lh,
  1005.     GTLV_Top, 1,
  1006.     LAYOUTA_SPACING, 1,
  1007.     GTLV_ShowSelected, stringgad,
  1008.     GTLV_Selected, 3,
  1009.     GTLV_ScrollWidth, 18,
  1010.     TAG_DONE);
  1011.  
  1012.     ng.ng_LeftEdge = 320;
  1013.     ng.ng_TopEdge = 49+topborder;
  1014.     ng.ng_Width = 40;
  1015.     ng.ng_Height = 75;
  1016.     ng.ng_GadgetText = "Colors";
  1017.     ng.ng_GadgetID = GAD_PALETTE;
  1018.     ng.ng_Flags = NG_HIGHLABEL;
  1019.  
  1020.     gad = CreateGadget(PALETTE_KIND, gad, &ng,
  1021.     GTPA_Depth, mysc->BitMap.Depth,
  1022.     GTPA_Color, 1,
  1023.     GTPA_ColorOffset, 0,
  1024.     GTPA_IndicatorHeight, 15,
  1025.     TAG_DONE);
  1026.  
  1027.     return(gad);
  1028. }
  1029.  
  1030. /*------------------------------------------------------------------------*/
  1031.