home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / TRSICAT.LZX / CATS_CD2_TRSI / Reference_Library / lib_examples / gadtoolsgadgets.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-21  |  13.0 KB  |  406 lines

  1. /* gadtoolsgadgets.c
  2. ** Simple example of using a number of gadtools gadgets.
  3. **
  4. ** Here's a working example showing how to set up and use a linked list
  5. ** of GadTools gadgets complete with keyboard shortcuts.
  6. **
  7. ** Compiled with SAS C v5.10a
  8. ** lc -b1 -cfistq -v -y gadtoolsgadgets
  9. ** blink FROM LIB:c.o gadtoolsgadgets.o TO gadtoolsgadgets LIB LIB:lc.lib LIB:amiga.lib
  10. */
  11. #define INTUI_V36_NAMES_ONLY
  12.  
  13. #include <exec/types.h>
  14. #include <intuition/intuition.h>
  15. #include <intuition/gadgetclass.h>
  16. #include <libraries/gadtools.h>
  17.  
  18. #include <clib/exec_protos.h>
  19. #include <clib/graphics_protos.h>
  20. #include <clib/intuition_protos.h>
  21. #include <clib/gadtools_protos.h>
  22.  
  23. #include <stdio.h>
  24.  
  25. #ifdef LATTICE
  26. int CXBRK(void)    { return(0); }  /* Disable Lattice CTRL/C handling */
  27. int chkabort(void) { return(0); }  /* really */
  28. #endif
  29.  
  30. /* Gadget defines of our choosing, to be used as GadgetID's,
  31. ** also used as the index into the gadget array my_gads[].
  32. */
  33. #define MYGAD_SLIDER    (0)
  34. #define MYGAD_STRING1   (1)
  35. #define MYGAD_STRING2   (2)
  36. #define MYGAD_STRING3   (3)
  37. #define MYGAD_BUTTON    (4)
  38.  
  39. /* Range for the slider: */
  40. #define SLIDER_MIN  (1)
  41. #define SLIDER_MAX (20)
  42.  
  43. struct TextAttr Topaz80 = { "topaz.font", 8, 0, 0, };
  44.  
  45. struct Library      *IntuitionBase;
  46. struct Library      *GfxBase;
  47. struct Library      *GadToolsBase;
  48.  
  49. /* Print any error message.  We could do more fancy handling (like
  50. ** an EasyRequest()), but this is only a demo.
  51. */
  52. void errorMessage(STRPTR error)
  53. {
  54. if (error)
  55.     printf("Error: %s\n", error);
  56. }
  57.  
  58. /*
  59. ** Function to handle a GADGETUP or GADGETDOWN event.  For GadTools gadgets,
  60. ** it is possible to use this function to handle MOUSEMOVEs as well, with
  61. ** little or no work.
  62. */
  63. VOID handleGadgetEvent(struct Window *win, struct Gadget *gad, UWORD code,
  64.     WORD *slider_level, struct Gadget *my_gads[])
  65. {
  66. switch (gad->GadgetID)
  67.     {
  68.     case MYGAD_SLIDER:
  69.         /* Sliders report their level in the IntuiMessage Code field: */
  70.         printf("Slider at level %ld\n", code);
  71.         *slider_level = code;
  72.         break;
  73.     case MYGAD_STRING1:
  74.         /* String gadgets report GADGETUP's */
  75.         printf("String gadget 1: '%s'.\n",
  76.                 ((struct StringInfo *)gad->SpecialInfo)->Buffer);
  77.         break;
  78.     case MYGAD_STRING2:
  79.         /* String gadgets report GADGETUP's */
  80.         printf("String gadget 2: '%s'.\n",
  81.                 ((struct StringInfo *)gad->SpecialInfo)->Buffer);
  82.         break;
  83.     case MYGAD_STRING3:
  84.         /* String gadgets report GADGETUP's */
  85.         printf("String gadget 3: '%s'.\n",
  86.                 ((struct StringInfo *)gad->SpecialInfo)->Buffer);
  87.         break;
  88.     case MYGAD_BUTTON:
  89.         /* Buttons report GADGETUP's (button resets slider to 10) */
  90.         printf("Button was pressed, slider reset to 10.\n");
  91.         *slider_level = 10;
  92.         GT_SetGadgetAttrs(my_gads[MYGAD_SLIDER], win, NULL,
  93.                             GTSL_Level, *slider_level,
  94.                             TAG_END);
  95.         break;
  96.     }
  97. }
  98.  
  99.  
  100. /*
  101. ** Function to handle vanilla keys.
  102. */
  103. VOID handleVanillaKey(struct Window *win, UWORD code,
  104.     WORD *slider_level, struct Gadget *my_gads[])
  105. {
  106. switch (code)
  107.     {
  108.     case 'v':
  109.         /* increase slider level, but not past maximum */
  110.         if (++*slider_level > SLIDER_MAX)
  111.             *slider_level = SLIDER_MAX;
  112.         GT_SetGadgetAttrs(my_gads[MYGAD_SLIDER], win, NULL,
  113.                             GTSL_Level, *slider_level,
  114.                             TAG_END);
  115.         break;
  116.     case 'V':
  117.         /* decrease slider level, but not past minimum */
  118.         if (--*slider_level < SLIDER_MIN)
  119.             *slider_level = SLIDER_MIN;
  120.         GT_SetGadgetAttrs(my_gads[MYGAD_SLIDER], win, NULL,
  121.                             GTSL_Level, *slider_level,
  122.                             TAG_END);
  123.         break;
  124.     case 'c':
  125.     case 'C':
  126.         /* button resets slider to 10 */
  127.         *slider_level = 10;
  128.         GT_SetGadgetAttrs(my_gads[MYGAD_SLIDER], win, NULL,
  129.                             GTSL_Level, *slider_level,
  130.                             TAG_END);
  131.         break;
  132.     case 'f':
  133.     case 'F':
  134.         ActivateGadget(my_gads[MYGAD_STRING1], win, NULL);
  135.         break;
  136.     case 's':
  137.     case 'S':
  138.         ActivateGadget(my_gads[MYGAD_STRING2], win, NULL);
  139.         break;
  140.     case 't':
  141.     case 'T':
  142.         ActivateGadget(my_gads[MYGAD_STRING3], win, NULL);
  143.         break;
  144.     }
  145. }
  146.  
  147.  
  148. /*
  149. ** Here is where all the initialization and creation of GadTools gadgets
  150. ** take place.  This function requires a pointer to a NULL-initialized
  151. ** gadget list pointer.  It returns a pointer to the last created gadget,
  152. ** which can be checked for success/failure.
  153. */
  154. struct Gadget *createAllGadgets(struct Gadget **glistptr, void *vi,
  155.     UWORD topborder, WORD slider_level, struct Gadget *my_gads[])
  156. {
  157. struct NewGadget ng;
  158. struct Gadget *gad;
  159.  
  160. /* All the gadget creation calls accept a pointer to the previous gadget, and
  161. ** link the new gadget to that gadget's NextGadget field.  Also, they exit
  162. ** gracefully, returning NULL, if any previous gadget was NULL.  This limits
  163. ** the amount of checking for failure that is needed.  You only need to check
  164. ** before you tweak any gadget structure or use any of its fields, and finally
  165. ** once at the end, before you add the gadgets.
  166. */
  167.  
  168. /* The following operation is required of any program that uses GadTools.
  169. ** It gives the toolkit a place to stuff context data.
  170. */
  171. gad = CreateContext(glistptr);
  172.  
  173. /* Since the NewGadget structure is unmodified by any of the CreateGadget()
  174. ** calls, we need only change those fields which are different.
  175. */
  176. ng.ng_LeftEdge   = 140;
  177. ng.ng_TopEdge    = 20+topborder;
  178. ng.ng_Width      = 200;
  179. ng.ng_Height     = 12;
  180. ng.ng_GadgetText = "_Volume:   ";
  181. ng.ng_TextAttr   = &Topaz80;
  182. ng.ng_VisualInfo = vi;
  183. ng.ng_GadgetID   = MYGAD_SLIDER;
  184. ng.ng_Flags      = NG_HIGHLABEL;
  185.  
  186. my_gads[MYGAD_SLIDER] = gad = CreateGadget(SLIDER_KIND, gad, &ng,
  187.                     GTSL_Min,         SLIDER_MIN,
  188.                     GTSL_Max,         SLIDER_MAX,
  189.                     GTSL_Level,       slider_level,
  190.                     GTSL_LevelFormat, "%2ld",
  191.                     GTSL_MaxLevelLen, 2,
  192.                     GT_Underscore,    '_',
  193.                     TAG_END);
  194.  
  195. ng.ng_TopEdge   += 20;
  196. ng.ng_Height     = 14;
  197. ng.ng_GadgetText = "_First:";
  198. ng.ng_GadgetID   = MYGAD_STRING1;
  199. my_gads[MYGAD_STRING1] = gad = CreateGadget(STRING_KIND, gad, &ng,
  200.                     GTST_String,   "Try pressing",
  201.                     GTST_MaxChars, 50,
  202.                     GT_Underscore, '_',
  203.                     TAG_END);
  204.  
  205. ng.ng_TopEdge   += 20;
  206. ng.ng_GadgetText = "_Second:";
  207. ng.ng_GadgetID   = MYGAD_STRING2;
  208. my_gads[MYGAD_STRING2] = gad = CreateGadget(STRING_KIND, gad, &ng,
  209.                     GTST_String,   "TAB or Shift-TAB",
  210.                     GTST_MaxChars, 50,
  211.                     GT_Underscore, '_',
  212.                     TAG_END);
  213.  
  214. ng.ng_TopEdge   += 20;
  215. ng.ng_GadgetText = "_Third:";
  216. ng.ng_GadgetID   = MYGAD_STRING3;
  217. my_gads[MYGAD_STRING3] = gad = CreateGadget(STRING_KIND, gad, &ng,
  218.                     GTST_String,   "To see what happens!",
  219.                     GTST_MaxChars, 50,
  220.                     GT_Underscore, '_',
  221.                     TAG_END);
  222.  
  223. ng.ng_LeftEdge  += 50;
  224. ng.ng_TopEdge   += 20;
  225. ng.ng_Width      = 100;
  226. ng.ng_Height     = 12;
  227. ng.ng_GadgetText = "_Click Here";
  228. ng.ng_GadgetID   = MYGAD_BUTTON;
  229. ng.ng_Flags      = 0;
  230. gad = CreateGadget(BUTTON_KIND, gad, &ng,
  231.                     GT_Underscore, '_',
  232.                     TAG_END);
  233. return(gad);
  234. }
  235.  
  236. /*
  237. ** Standard message handling loop with GadTools message handling functions
  238. ** used (GT_GetIMsg() and GT_ReplyIMsg()).
  239. */
  240. VOID process_window_events(struct Window *mywin,
  241.     WORD *slider_level, struct Gadget *my_gads[])
  242. {
  243. struct IntuiMessage *imsg;
  244. ULONG imsgClass;
  245. UWORD imsgCode;
  246. struct Gadget *gad;
  247. BOOL terminated = FALSE;
  248.  
  249. while (!terminated)
  250.     {
  251.     Wait (1 << mywin->UserPort->mp_SigBit);
  252.  
  253.     /* GT_GetIMsg() returns an IntuiMessage with more friendly information for
  254.     ** complex gadget classes.  Use it wherever you get IntuiMessages where
  255.     ** using GadTools gadgets.
  256.     */
  257.     while ((!terminated) &&
  258.            (imsg = GT_GetIMsg(mywin->UserPort)))
  259.         {
  260.         /* Presuming a gadget, of course, but no harm...
  261.         ** Only dereference this value (gad) where the Class specifies
  262.         ** that it is a gadget event.
  263.         */
  264.         gad = (struct Gadget *)imsg->IAddress;
  265.  
  266.         imsgClass = imsg->Class;
  267.         imsgCode = imsg->Code;
  268.  
  269.         /* Use the toolkit message-replying function here... */
  270.         GT_ReplyIMsg(imsg);
  271.  
  272.         switch (imsgClass)
  273.             {
  274.             /*  --- WARNING --- WARNING --- WARNING --- WARNING --- WARNING ---
  275.             ** GadTools puts the gadget address into IAddress of IDCMP_MOUSEMOVE
  276.             ** messages.  This is NOT true for standard Intuition messages,
  277.             ** but is an added feature of GadTools.
  278.             */
  279.             case IDCMP_GADGETDOWN:
  280.             case IDCMP_MOUSEMOVE:
  281.             case IDCMP_GADGETUP:
  282.                 handleGadgetEvent(mywin, gad, imsgCode, slider_level, my_gads);
  283.                 break;
  284.             case IDCMP_VANILLAKEY:
  285.                 handleVanillaKey(mywin, imsgCode, slider_level, my_gads);
  286.                 break;
  287.             case IDCMP_CLOSEWINDOW:
  288.                 terminated = TRUE;
  289.                 break;
  290.             case IDCMP_REFRESHWINDOW:
  291.                 /* With GadTools, the application must use GT_BeginRefresh()
  292.                 ** where it would normally have used BeginRefresh()
  293.                 */
  294.                 GT_BeginRefresh(mywin);
  295.                 GT_EndRefresh(mywin, TRUE);
  296.                 break;
  297.             }
  298.         }
  299.     }
  300. }
  301.  
  302. /*
  303. ** Prepare for using GadTools, set up gadgets and open window.
  304. ** Clean up and when done or on error.
  305. */
  306. VOID gadtoolsWindow(VOID)
  307. {
  308. struct TextFont *font;
  309. struct Screen   *mysc;
  310. struct Window   *mywin;
  311. struct Gadget   *glist, *my_gads[4];
  312. void            *vi;
  313. WORD            slider_level = 5;
  314. UWORD           topborder;
  315.  
  316. /* Open topaz 8 font, so we can be sure it's openable
  317. ** when we later set ng_TextAttr to &Topaz80:
  318. */
  319. if (NULL == (font = OpenFont(&Topaz80)))
  320.     errorMessage( "Failed to open Topaz 80");
  321. else
  322.     {
  323.     if (NULL == (mysc = LockPubScreen(NULL)))
  324.         errorMessage( "Couldn't lock default public screen");
  325.     else
  326.         {
  327.         if (NULL == (vi = GetVisualInfo(mysc, TAG_END)))
  328.             errorMessage( "GetVisualInfo() failed");
  329.         else
  330.             {
  331.             /* Here is how we can figure out ahead of time how tall the  */
  332.             /* window's title bar will be:                               */
  333.             topborder = mysc->WBorTop + (mysc->Font->ta_YSize + 1);
  334.  
  335.             if (NULL == createAllGadgets(&glist, vi, topborder,
  336.                                          slider_level, my_gads))
  337.                 errorMessage( "createAllGadgets() failed");
  338.             else
  339.                 {
  340.                 if (NULL == (mywin = OpenWindowTags(NULL,
  341.                         WA_Title,     "GadTools Gadget Demo",
  342.                         WA_Gadgets,   glist,      WA_AutoAdjust,    TRUE,
  343.                         WA_Width,       400,      WA_MinWidth,        50,
  344.                         WA_InnerHeight, 140,      WA_MinHeight,       50,
  345.                         WA_DragBar,    TRUE,      WA_DepthGadget,   TRUE,
  346.                         WA_Activate,   TRUE,      WA_CloseGadget,   TRUE,
  347.                         WA_SizeGadget, TRUE,      WA_SimpleRefresh, TRUE,
  348.                         WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW |
  349.                             IDCMP_VANILLAKEY | SLIDERIDCMP | STRINGIDCMP |
  350.                             BUTTONIDCMP,
  351.                         WA_PubScreen, mysc,
  352.                         TAG_END)))
  353.                     errorMessage( "OpenWindow() failed");
  354.                 else
  355.                     {
  356.                     /* After window is open, gadgets must be refreshed with a
  357.                     ** call to the GadTools refresh window function.
  358.                     */
  359.                     GT_RefreshWindow(mywin, NULL);
  360.  
  361.                     process_window_events(mywin, &slider_level, my_gads);
  362.  
  363.                     CloseWindow(mywin);
  364.                     }
  365.                 }
  366.             /* FreeGadgets() even if createAllGadgets() fails, as some
  367.             ** of the gadgets may have been created...If glist is NULL
  368.             ** then FreeGadgets() will do nothing.
  369.             */
  370.             FreeGadgets(glist);
  371.             FreeVisualInfo(vi);
  372.             }
  373.         UnlockPubScreen(NULL, mysc);
  374.         }
  375.     CloseFont(font);
  376.     }
  377. }
  378.  
  379.  
  380. /*
  381. ** Open all libraries and run.  Clean up when finished or on error..
  382. */
  383. void main(void)
  384. {
  385. if (NULL == (IntuitionBase = OpenLibrary("intuition.library", 37)))
  386.     errorMessage( "Requires V37 intuition.library");
  387. else
  388.     {
  389.     if (NULL == (GfxBase = OpenLibrary("graphics.library", 37)))
  390.         errorMessage( "Requires V37 graphics.library");
  391.     else
  392.         {
  393.         if (NULL == (GadToolsBase = OpenLibrary("gadtools.library", 37)))
  394.             errorMessage( "Requires V37 gadtools.library");
  395.         else
  396.             {
  397.             gadtoolsWindow();
  398.  
  399.             CloseLibrary(GadToolsBase);
  400.             }
  401.         CloseLibrary(GfxBase);
  402.         }
  403.     CloseLibrary(IntuitionBase);
  404.     }
  405. }
  406.