home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / dev / rkrm.lha / RKRM / GadTools / gadtoolsgadgets.c < prev    next >
C/C++ Source or Header  |  1992-09-03  |  14KB  |  431 lines

  1. ;/* gadtoolsgadgets.c - Compiled with SAS C v5.10b
  2. lc -b1 -cfistq -v -y -j73 gadtoolsgadgets
  3. blink FROM LIB:c.o gadtoolsgadgets.o TO gadtoolsgadgets LIB LIB:lc.lib LIB:amiga.lib
  4. quit
  5. */
  6.  
  7. /*
  8. Copyright (c) 1992 Commodore-Amiga, Inc.
  9.  
  10. This example is provided in electronic form by Commodore-Amiga, Inc. for
  11. use with the "Amiga ROM Kernel Reference Manual: Libraries", 3rd Edition,
  12. published by Addison-Wesley (ISBN 0-201-56774-1).
  13.  
  14. The "Amiga ROM Kernel Reference Manual: Libraries" contains additional
  15. information on the correct usage of the techniques and operating system
  16. functions presented in these examples.  The source and executable code
  17. of these examples may only be distributed in free electronic form, via
  18. bulletin board or as part of a fully non-commercial and freely
  19. redistributable diskette.  Both the source and executable code (including
  20. comments) must be included, without modification, in any copy.  This
  21. example may not be published in printed form or distributed with any
  22. commercial product.  However, the programming techniques and support
  23. routines set forth in these examples may be used in the development
  24. of original executable software products for Commodore Amiga computers.
  25.  
  26. All other rights reserved.
  27.  
  28. This example is provided "as-is" and is subject to change; no
  29. warranties are made.  All use is at your own risk. No liability or
  30. responsibility is assumed.
  31. */
  32.  
  33. /* gadtoolsgadgets.c
  34. ** Simple example of using a number of gadtools gadgets.
  35. */
  36. #define INTUI_V36_NAMES_ONLY
  37.  
  38. #include <exec/types.h>
  39. #include <intuition/intuition.h>
  40. #include <intuition/gadgetclass.h>
  41. #include <libraries/gadtools.h>
  42.  
  43. #include <clib/exec_protos.h>
  44. #include <clib/graphics_protos.h>
  45. #include <clib/intuition_protos.h>
  46. #include <clib/gadtools_protos.h>
  47.  
  48. #include <stdio.h>
  49.  
  50. #ifdef LATTICE
  51. int CXBRK(void)    { return(0); }  /* Disable Lattice CTRL/C handling */
  52. int chkabort(void) { return(0); }  /* really */
  53. #endif
  54.  
  55. /* Gadget defines of our choosing, to be used as GadgetID's,
  56. ** also used as the index into the gadget array my_gads[].
  57. */
  58. #define MYGAD_SLIDER    (0)
  59. #define MYGAD_STRING1   (1)
  60. #define MYGAD_STRING2   (2)
  61. #define MYGAD_STRING3   (3)
  62. #define MYGAD_BUTTON    (4)
  63.  
  64. /* Range for the slider: */
  65. #define SLIDER_MIN  (1)
  66. #define SLIDER_MAX (20)
  67.  
  68. struct TextAttr Topaz80 = { "topaz.font", 8, 0, 0, };
  69.  
  70. struct Library      *IntuitionBase;
  71. struct Library      *GfxBase;
  72. struct Library      *GadToolsBase;
  73.  
  74. /* Print any error message.  We could do more fancy handling (like
  75. ** an EasyRequest()), but this is only a demo.
  76. */
  77. void errorMessage(STRPTR error)
  78. {
  79. if (error)
  80.     printf("Error: %s\n", error);
  81. }
  82.  
  83. /*
  84. ** Function to handle a GADGETUP or GADGETDOWN event.  For GadTools gadgets,
  85. ** it is possible to use this function to handle MOUSEMOVEs as well, with
  86. ** little or no work.
  87. */
  88. VOID handleGadgetEvent(struct Window *win, struct Gadget *gad, UWORD code,
  89.     WORD *slider_level, struct Gadget *my_gads[])
  90. {
  91. switch (gad->GadgetID)
  92.     {
  93.     case MYGAD_SLIDER:
  94.         /* Sliders report their level in the IntuiMessage Code field: */
  95.         printf("Slider at level %ld\n", code);
  96.         *slider_level = code;
  97.         break;
  98.     case MYGAD_STRING1:
  99.         /* String gadgets report GADGETUP's */
  100.         printf("String gadget 1: '%s'.\n",
  101.                 ((struct StringInfo *)gad->SpecialInfo)->Buffer);
  102.         break;
  103.     case MYGAD_STRING2:
  104.         /* String gadgets report GADGETUP's */
  105.         printf("String gadget 2: '%s'.\n",
  106.                 ((struct StringInfo *)gad->SpecialInfo)->Buffer);
  107.         break;
  108.     case MYGAD_STRING3:
  109.         /* String gadgets report GADGETUP's */
  110.         printf("String gadget 3: '%s'.\n",
  111.                 ((struct StringInfo *)gad->SpecialInfo)->Buffer);
  112.         break;
  113.     case MYGAD_BUTTON:
  114.         /* Buttons report GADGETUP's (button resets slider to 10) */
  115.         printf("Button was pressed, slider reset to 10.\n");
  116.         *slider_level = 10;
  117.         GT_SetGadgetAttrs(my_gads[MYGAD_SLIDER], win, NULL,
  118.                             GTSL_Level, *slider_level,
  119.                             TAG_END);
  120.         break;
  121.     }
  122. }
  123.  
  124.  
  125. /*
  126. ** Function to handle vanilla keys.
  127. */
  128. VOID handleVanillaKey(struct Window *win, UWORD code,
  129.     WORD *slider_level, struct Gadget *my_gads[])
  130. {
  131. switch (code)
  132.     {
  133.     case 'v':
  134.         /* increase slider level, but not past maximum */
  135.         if (++*slider_level > SLIDER_MAX)
  136.             *slider_level = SLIDER_MAX;
  137.         GT_SetGadgetAttrs(my_gads[MYGAD_SLIDER], win, NULL,
  138.                             GTSL_Level, *slider_level,
  139.                             TAG_END);
  140.         break;
  141.     case 'V':
  142.         /* decrease slider level, but not past minimum */
  143.         if (--*slider_level < SLIDER_MIN)
  144.             *slider_level = SLIDER_MIN;
  145.         GT_SetGadgetAttrs(my_gads[MYGAD_SLIDER], win, NULL,
  146.                             GTSL_Level, *slider_level,
  147.                             TAG_END);
  148.         break;
  149.     case 'c':
  150.     case 'C':
  151.         /* button resets slider to 10 */
  152.         *slider_level = 10;
  153.         GT_SetGadgetAttrs(my_gads[MYGAD_SLIDER], win, NULL,
  154.                             GTSL_Level, *slider_level,
  155.                             TAG_END);
  156.         break;
  157.     case 'f':
  158.     case 'F':
  159.         ActivateGadget(my_gads[MYGAD_STRING1], win, NULL);
  160.         break;
  161.     case 's':
  162.     case 'S':
  163.         ActivateGadget(my_gads[MYGAD_STRING2], win, NULL);
  164.         break;
  165.     case 't':
  166.     case 'T':
  167.         ActivateGadget(my_gads[MYGAD_STRING3], win, NULL);
  168.         break;
  169.     }
  170. }
  171.  
  172.  
  173. /*
  174. ** Here is where all the initialization and creation of GadTools gadgets
  175. ** take place.  This function requires a pointer to a NULL-initialized
  176. ** gadget list pointer.  It returns a pointer to the last created gadget,
  177. ** which can be checked for success/failure.
  178. */
  179. struct Gadget *createAllGadgets(struct Gadget **glistptr, void *vi,
  180.     UWORD topborder, WORD slider_level, struct Gadget *my_gads[])
  181. {
  182. struct NewGadget ng;
  183. struct Gadget *gad;
  184.  
  185. /* All the gadget creation calls accept a pointer to the previous gadget, and
  186. ** link the new gadget to that gadget's NextGadget field.  Also, they exit
  187. ** gracefully, returning NULL, if any previous gadget was NULL.  This limits
  188. ** the amount of checking for failure that is needed.  You only need to check
  189. ** before you tweak any gadget structure or use any of its fields, and finally
  190. ** once at the end, before you add the gadgets.
  191. */
  192.  
  193. /* The following operation is required of any program that uses GadTools.
  194. ** It gives the toolkit a place to stuff context data.
  195. */
  196. gad = CreateContext(glistptr);
  197.  
  198. /* Since the NewGadget structure is unmodified by any of the CreateGadget()
  199. ** calls, we need only change those fields which are different.
  200. */
  201. ng.ng_LeftEdge   = 140;
  202. ng.ng_TopEdge    = 20+topborder;
  203. ng.ng_Width      = 200;
  204. ng.ng_Height     = 12;
  205. ng.ng_GadgetText = "_Volume:   ";
  206. ng.ng_TextAttr   = &Topaz80;
  207. ng.ng_VisualInfo = vi;
  208. ng.ng_GadgetID   = MYGAD_SLIDER;
  209. ng.ng_Flags      = NG_HIGHLABEL;
  210.  
  211. my_gads[MYGAD_SLIDER] = gad = CreateGadget(SLIDER_KIND, gad, &ng,
  212.                     GTSL_Min,         SLIDER_MIN,
  213.                     GTSL_Max,         SLIDER_MAX,
  214.                     GTSL_Level,       slider_level,
  215.                     GTSL_LevelFormat, "%2ld",
  216.                     GTSL_MaxLevelLen, 2,
  217.                     GT_Underscore,    '_',
  218.                     TAG_END);
  219.  
  220. ng.ng_TopEdge   += 20;
  221. ng.ng_Height     = 14;
  222. ng.ng_GadgetText = "_First:";
  223. ng.ng_GadgetID   = MYGAD_STRING1;
  224. my_gads[MYGAD_STRING1] = gad = CreateGadget(STRING_KIND, gad, &ng,
  225.                     GTST_String,   "Try pressing",
  226.                     GTST_MaxChars, 50,
  227.                     GT_Underscore, '_',
  228.                     TAG_END);
  229.  
  230. ng.ng_TopEdge   += 20;
  231. ng.ng_GadgetText = "_Second:";
  232. ng.ng_GadgetID   = MYGAD_STRING2;
  233. my_gads[MYGAD_STRING2] = gad = CreateGadget(STRING_KIND, gad, &ng,
  234.                     GTST_String,   "TAB or Shift-TAB",
  235.                     GTST_MaxChars, 50,
  236.                     GT_Underscore, '_',
  237.                     TAG_END);
  238.  
  239. ng.ng_TopEdge   += 20;
  240. ng.ng_GadgetText = "_Third:";
  241. ng.ng_GadgetID   = MYGAD_STRING3;
  242. my_gads[MYGAD_STRING3] = gad = CreateGadget(STRING_KIND, gad, &ng,
  243.                     GTST_String,   "To see what happens!",
  244.