home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Extras / BOOPSI / GI1 / Examples / Button_Test.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-27  |  10.8 KB  |  420 lines

  1. /*****************************************************************************
  2.  *
  3.  * COPYRIGHT: Unless otherwise noted, all files are Copyright (c) 1992-1999
  4.  * Amiga, Inc. All rights reserved.
  5.  *
  6.  * DISCLAIMER: This software is provided "as is".  No representations or
  7.  * warranties are made with respect to the accuracy, reliability,
  8.  * performance, currentness, or operation of this software, and all use is at
  9.  * your own risk. Neither Amiga nor the authors assume any responsibility
  10.  * or liability whatsoever with respect to your use of this software.
  11.  *
  12.  *****************************************************************************
  13.  * button_test.c
  14.  * test program for the button.gadget
  15.  * Written by David N. Junod
  16.  *
  17.  */
  18.  
  19. #include <dos/dos.h>
  20. #include <exec/types.h>
  21. #include <exec/libraries.h>
  22. #include <intuition/classes.h>
  23. #include <intuition/intuition.h>
  24. #include <intuition/gadgetclass.h>
  25. #include <intuition/intuitionbase.h>
  26. #include <gadgets/button.h>
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29.  
  30. #include <clib/macros.h>
  31. #include <clib/dos_protos.h>
  32. #include <clib/exec_protos.h>
  33. #include <clib/intuition_protos.h>
  34.  
  35. #include <pragmas/dos_pragmas.h>
  36. #include <pragmas/exec_pragmas.h>
  37. #include <pragmas/intuition_pragmas.h>
  38.  
  39. /*****************************************************************************/
  40.  
  41. #define    INBORDER    TRUE
  42.  
  43. /*****************************************************************************/
  44.  
  45. #define    IDCMP_FLAGS    IDCMP_CLOSEWINDOW | IDCMP_VANILLAKEY | IDCMP_GADGETUP | IDCMP_GADGETDOWN \
  46.             | IDCMP_MOUSEMOVE | IDCMP_INTUITICKS | IDCMP_MOUSEBUTTONS
  47.  
  48. /*****************************************************************************/
  49.  
  50. struct ClassLibrary *openclass (STRPTR name, ULONG version);
  51.  
  52. /*****************************************************************************/
  53.  
  54. static UWORD chip imrewData[14] =
  55. {
  56.  /* Plane 0 */
  57.     0x0301, 0x8000, 0x0F07, 0x8000, 0x3F1F, 0x8000, 0xFF7F, 0x8000,
  58.     0x3F1F, 0x8000, 0x0F07, 0x8000, 0x0301, 0x8000
  59. };
  60.  
  61. struct Image im1 =
  62. {
  63.     0, 0,            /* LeftEdge, TopEdge */
  64.     17, 7, 1,            /* Width, Height, Depth */
  65.     imrewData,            /* ImageData */
  66.     0x0001, 0x0000,        /* PlanePick, PlaneOnOff */
  67.     NULL            /* NextImage */
  68. };
  69.  
  70. /*****************************************************************************/
  71.  
  72. static UWORD chip impp1Data[14] =
  73. {
  74.  /* Plane 0 */
  75.     0xE383, 0x0000, 0xE383, 0xC000, 0xE383, 0xF000, 0xE383, 0xFC00,
  76.     0xE383, 0xF000, 0xE383, 0xC000, 0xE383, 0x0000
  77. };
  78.  
  79. struct Image impp1 =
  80. {
  81.     0, 0,            /* LeftEdge, TopEdge */
  82.     22, 7, 1,            /* Width, Height, Depth */
  83.     impp1Data,            /* ImageData */
  84.     0x0001, 0x0000,        /* PlanePick, PlaneOnOff */
  85.     NULL            /* NextImage */
  86. };
  87.  
  88. /*****************************************************************************/
  89.  
  90. static UWORD chip impp2Data[28] =
  91. {
  92.  /* Plane 0 */
  93.     0x0003, 0x0000, 0x0003, 0xC000, 0x0003, 0xF000, 0x0003, 0xFC00,
  94.     0x0003, 0xF000, 0x0003, 0xC000, 0x0003, 0x0000,
  95.  /* Plane 1 */
  96.     0xE380, 0x0000, 0xE380, 0x0000, 0xE380, 0x0000, 0xE380, 0x0000,
  97.     0xE380, 0x0000, 0xE380, 0x0000, 0xE380, 0x0000
  98. };
  99.  
  100. struct Image impp2 =
  101. {
  102.     0, 0,            /* LeftEdge, TopEdge */
  103.     22, 7, 2,            /* Width, Height, Depth */
  104.     impp2Data,            /* ImageData */
  105.     0x0003, 0x0000,        /* PlanePick, PlaneOnOff */
  106.     NULL            /* NextImage */
  107. };
  108.  
  109. /*****************************************************************************/
  110.  
  111. static UWORD chip impp3Data[28] =
  112. {
  113.  /* Plane 0 */
  114.     0xE380, 0x0000, 0xE380, 0x0000, 0xE380, 0x0000, 0xE380, 0x0000,
  115.     0xE380, 0x0000, 0xE380, 0x0000, 0xE380, 0x0000,
  116.  /* Plane 1 */
  117.     0x0003, 0x0000, 0x0003, 0xC000, 0x0003, 0xF000, 0x0003, 0xFC00,
  118.     0x0003, 0xF000, 0x0003, 0xC000, 0x0003, 0x0000
  119. };
  120.  
  121. struct Image impp3 =
  122. {
  123.     0, 0,            /* LeftEdge, TopEdge */
  124.     22, 7, 2,            /* Width, Height, Depth */
  125.     impp3Data,            /* ImageData */
  126.     0x0003, 0x0000,        /* PlanePick, PlaneOnOff */
  127.     NULL            /* NextImage */
  128. };
  129.  
  130. /*****************************************************************************/
  131.  
  132. static UWORD chip imffData[14] =
  133. {
  134.  /* Plane 0 */
  135.     0xC060, 0x0000, 0xF078, 0x0000, 0xFC7E, 0x0000, 0xFF7F, 0x8000,
  136.     0xFC7E, 0x0000, 0xF078, 0x0000, 0xC060, 0x0000
  137. };
  138.  
  139. struct Image im4 =
  140. {
  141.     0, 0,            /* LeftEdge, TopEdge */
  142.     17, 7, 1,            /* Width, Height, Depth */
  143.     imffData,            /* ImageData */
  144.     0x0001, 0x0000,        /* PlanePick, PlaneOnOff */
  145.     NULL            /* NextImage */
  146. };
  147.  
  148. /*****************************************************************************/
  149.  
  150. static UWORD chip imstopData[7] =
  151. {
  152.  /* Plane 0 */
  153.     0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF
  154. };
  155.  
  156. struct Image im5 =
  157. {
  158.     0, 0,            /* LeftEdge, TopEdge */
  159.     9, 7, 1,            /* Width, Height, Depth */
  160.     imstopData,            /* ImageData */
  161.     0x0000, 0x0001,        /* PlanePick, PlaneOnOff */
  162.     NULL            /* NextImage */
  163. };
  164.  
  165. /*****************************************************************************/
  166.  
  167. extern struct Library *SysBase, *DOSBase;
  168. struct Library *IntuitionBase;
  169. char *buttonClassName = "button.gadget";
  170. struct Image *impp[3] = {&impp1, &impp2, &impp3};
  171.  
  172. #define    GID_REWIND    0
  173. #define    GID_PP        1
  174. #define    GID_FFORWARD    2
  175. #define    GID_STOP    3
  176. #define    NUMGADS        4
  177. struct Gadget *gads[NUMGADS];
  178.  
  179. /*****************************************************************************/
  180.  
  181. static void creategads (struct Window *win)
  182. {
  183.     struct Gadget *g;
  184.     LONG x, w, h;
  185.  
  186.     /* Determine the width and offset of the gadgets */
  187.     for (g = win->FirstGadget, w = 30, h = win->BorderTop - 2; g; g = g->NextGadget)
  188.     {
  189.     if ((g->GadgetType & GTYP_SYSGADGET) && (g->GadgetType & GTYP_CLOSE))
  190.     {
  191.         x = g->LeftEdge + g->Width - 1;
  192.         w = MAX (w, g->Width);
  193.         break;
  194.     }
  195.     }
  196.  
  197.     /* Create the rewind button */
  198.     gads[GID_REWIND] = NewObject (NULL, buttonClassName,
  199.                   GA_Top, 1,
  200.                   GA_Left, x,
  201.                   GA_Width, w,
  202.                   GA_Height, h,
  203.                   GA_TopBorder, TRUE,
  204.                   GA_Immediate, TRUE,
  205.                   GA_RelVerify, TRUE,
  206.                   GA_ID, GID_REWIND,
  207.                   BUTTON_Glyph, &im1,
  208.                   TAG_DONE);
  209.  
  210.     /* Create the play/pause button */
  211.     x += w;
  212.     gads[GID_PP] = NewObject (NULL, buttonClassName,
  213.                   GA_Previous, gads[GID_PP - 1],
  214.                   GA_Top, 1,
  215.                   GA_Left, x,
  216.                   GA_Width, w,
  217.                   GA_Height, h,
  218.                   GA_TopBorder, TRUE,
  219.                   GA_Immediate, TRUE,
  220.                   GA_RelVerify, TRUE,
  221.                   BUTTON_PushButton, TRUE,
  222.                   GA_ID, GID_PP,
  223.                   BUTTON_Glyph, impp,
  224.                   BUTTON_Array, 3,
  225.                   TAG_DONE);
  226.  
  227.     /* Create the fast forward button */
  228.     x += w;
  229.     gads[GID_FFORWARD] = NewObject (NULL, buttonClassName,
  230.                     GA_Previous, gads[GID_FFORWARD - 1],
  231.                     GA_Top, 1,
  232.                     GA_Left, x,
  233.                     GA_Width, w,
  234.                     GA_Height, h,
  235.                     GA_TopBorder, TRUE,
  236.                     GA_Immediate, TRUE,
  237.                     GA_RelVerify, TRUE,
  238.                     GA_ID, GID_FFORWARD,
  239.                     BUTTON_Glyph, &im4,
  240.                     TAG_DONE);
  241.  
  242.     /* Create the stop button */
  243.     x += w;
  244.     gads[GID_STOP] = NewObject (NULL, buttonClassName,
  245.                 GA_Previous, gads[GID_STOP - 1],
  246.                 GA_Top, 1,
  247.                 GA_Left, x,
  248.                 GA_Width, w,
  249.                 GA_Height, h,
  250.                 GA_TopBorder, TRUE,
  251.                 GA_Immediate, TRUE,
  252.                 GA_RelVerify, TRUE,
  253.                 GA_Selected, TRUE,
  254.                 BUTTON_PushButton, TRUE,
  255.                 GA_ID, GID_STOP,
  256.                 BUTTON_Glyph, &im5,
  257.                 TAG_DONE);
  258.  
  259.     /* Add and refresh the gadget list */
  260.     AddGList (win, gads[0], 0, -1, NULL);
  261.     RefreshGList (gads[0], win, NULL, -1);
  262. }
  263.  
  264. /*****************************************************************************/
  265.  
  266. static void selectgad (struct Window *win, UWORD sel, BOOL leave)
  267. {
  268.     register int i;
  269.  
  270.     for (i = 0; i < NUMGADS; i++)
  271.     {
  272.     if ((i != sel) || ((i == sel) && leave))
  273.     {
  274.         if (!((i == sel) && (gads[i]->Flags & GFLG_SELECTED)))
  275.         SetGadgetAttrs (gads[i], win, NULL, GA_Selected, (i == sel), TAG_DONE);
  276.     }
  277.     }
  278. }
  279.  
  280. /*****************************************************************************/
  281.  
  282. static void deletegads (struct Window *win)
  283. {
  284.     register int i;
  285.  
  286.     for (i = 0; i < NUMGADS; i++)
  287.     {
  288.     if (gads[i])
  289.     {
  290.         RemoveGList (win, gads[i], 1);
  291.         DisposeObject (gads[i]);
  292.     }
  293.     }
  294. }
  295.  
  296. /*****************************************************************************/
  297.  
  298. void main (int argc, char **argv)
  299. {
  300.     struct IntuiMessage *imsg;
  301.     struct ClassLibrary *gLib;
  302.     struct Screen *scr;
  303.     struct Window *win;
  304.     struct Gadget *g;
  305.     BOOL going = TRUE;
  306.     ULONG sigr;
  307.  
  308.     if (IntuitionBase = OpenLibrary ("intuition.library", 37))
  309.     {
  310.     scr = ((struct IntuitionBase *) IntuitionBase)->FirstScreen;
  311.  
  312.     if (gLib = openclass ("gadgets/button.gadget", 37))
  313.     {
  314.         if (win = OpenWindowTags (NULL,
  315.                       WA_InnerWidth,    202,
  316.                       WA_Height,    scr->BarHeight + 1,
  317.                       WA_IDCMP,        IDCMP_FLAGS,
  318.                       WA_DragBar,    TRUE,
  319.                       WA_DepthGadget,    TRUE,
  320.                       WA_CloseGadget,    TRUE,
  321.                       WA_SimpleRefresh,    TRUE,
  322.                       WA_NoCareRefresh,    TRUE,
  323.                       WA_Activate,    TRUE,
  324.                       WA_CustomScreen,    scr,
  325.                       TAG_DONE))
  326.         {
  327.         creategads (win);
  328.         {
  329.             while (going)
  330.             {
  331.             sigr = Wait ((1L << win->UserPort->mp_SigBit | SIGBREAKF_CTRL_C));
  332.  
  333.             if (sigr & SIGBREAKF_CTRL_C)
  334.                 going = FALSE;
  335.  
  336.             while (imsg = (struct IntuiMessage *) GetMsg (win->UserPort))
  337.             {
  338.                 switch (imsg->Class)
  339.                 {
  340.                 case IDCMP_CLOSEWINDOW:
  341.                     going = FALSE;
  342.                     break;
  343.  
  344.                 case IDCMP_VANILLAKEY:
  345.                     switch (imsg->Code)
  346.                     {
  347.                     case 27:
  348.                     case 'q':
  349.                     case 'Q':
  350.                         going = FALSE;
  351.                         break;
  352.                     }
  353.                     break;
  354.  
  355.                 case IDCMP_GADGETDOWN:
  356.                     g = (struct Gadget *) imsg->IAddress;
  357.                     switch (g->GadgetID)
  358.                     {
  359.                     case GID_REWIND:    /* Rew */
  360.                     case GID_FFORWARD:    /* FF */
  361.                         Printf ("v id=%ld code=%04lx : %04lx\n",
  362.                             (ULONG) g->GadgetID, (ULONG) imsg->Code, (ULONG) g->SpecialInfo);
  363.                         break;
  364.                     }
  365.                     break;
  366.  
  367.                 case IDCMP_GADGETUP:
  368.                     Printf ("^ id=%ld code=%04lx : %04lx\n",
  369.                         (ULONG) g->GadgetID, (ULONG) imsg->Code, (ULONG) g->SpecialInfo);
  370.                     switch (g->GadgetID)
  371.                     {
  372.                     case GID_PP:    /* Play */
  373.                     case GID_STOP:    /* Stop */
  374.                         selectgad (win, g->GadgetID, TRUE);
  375.                         break;
  376.                     }
  377.                     g = NULL;
  378.                     break;
  379.                 }
  380.  
  381.                 ReplyMsg ((struct Message *) imsg);
  382.             }
  383.             }
  384.             deletegads (win);
  385.         }
  386.         CloseWindow (win);
  387.         }
  388.         else
  389.         Printf ("couldn't open the window\n");
  390.  
  391.         CloseLibrary ((struct Library *) gLib);
  392.     }
  393.     else
  394.         Printf ("couldn't open classes:gadgets/button.gadget\n");
  395.  
  396.     CloseLibrary (IntuitionBase);
  397.     }
  398. }
  399.  
  400. /*****************************************************************************/
  401.  
  402. /* Try opening the class library from a number of common places */
  403. struct ClassLibrary *openclass (STRPTR name, ULONG version)
  404. {
  405.     struct ExecBase *SysBase = (*((struct ExecBase **) 4));
  406.     struct Library *retval;
  407.     UBYTE buffer[256];
  408.  
  409.     if ((retval = OpenLibrary (name, version)) == NULL)
  410.     {
  411.     sprintf (buffer, ":classes/%s", name);
  412.     if ((retval = OpenLibrary (buffer, version)) == NULL)
  413.     {
  414.         sprintf (buffer, "classes/%s", name);
  415.         retval = OpenLibrary (buffer, version);
  416.     }
  417.     }
  418.     return (struct ClassLibrary *) retval;
  419. }
  420.