home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 177.lha / Button / but.c < prev    next >
C/C++ Source or Header  |  1988-04-28  |  13KB  |  689 lines

  1. /*  "ram:but.c"  10/15/88 */
  2.  
  3. /*-------------------------------------------------------------------------+
  4.  | This is an example of how 'radio buttons' could be implemented using    |
  5.  | gadgets. I use the GadgetID and UserData fields of the gadget structure |
  6.  | to keep track of each set of radio buttons. When the user clicks on a   |
  7.  | radio button a varaible is set and an optional function is called with  |
  8.  | the variable as an argument. In order for this to work you must call    |
  9.  | HandleGadgets() when the appropriate gadget related message comes across|
  10.  | the IDCMP. There is a function called SetRadioButtons() sets the initial|
  11.  | state of the buttons. If you modify the radio button variable for a set |
  12.  | of buttons then you will have to call SetRadioButtons() to display the  |
  13.  | proper state of the radio buttons. To terminate the program press a key.|
  14.  | Look below for more comments.                                           |
  15.  |                                                                         |
  16.  |     This program is in the public domain, and may not be sold.          |
  17.  |                                                                         |
  18.  |          Written by Robert W. Albrecht                                  |
  19.  |          1457 Zion Way                                                  |
  20.  |          Ventura CA 93003                                               |
  21.  +-------------------------------------------------------------------------*/
  22.  
  23.  
  24. /* WARNING: This program contains bit maps in static data and must be
  25.  * linked with the +Cdb option (Aztec) or ATOM'd (Lettuce) to work with
  26.  * expanded memory. */
  27.  
  28.  
  29. #include <intuition/intuition.h>
  30. #include <exec/types.h>
  31.  
  32. #define INTUITION_REV 33L
  33. #define GRAPHICS_REV 33L
  34. #define DISKFONT_REV 33L
  35.  
  36. #define ZILCH 0
  37.  
  38. extern struct Library *OpenLibrary();
  39. extern struct Window *OpenWindow();
  40. extern struct Screen *OpenScreen();
  41. extern struct TextFont *OpenFont();
  42. extern struct Message *GetMsg();
  43. extern struct ViewPort *ViewPortAddress();
  44. extern long Wait();
  45.  
  46. /*** *** *** *** *** *** *** DATA *** *** *** *** *** *** *** ***/
  47.  
  48. /* GLOBAL LIBRARY BASE POINTERS */
  49. struct IntuitionBase *IntuitionBase = 0L;
  50. struct GfxBase *GfxBase = 0L;
  51. struct DiskfontBase *DiskfontBase = 0L;
  52.  
  53. /* Resources */
  54. struct Window *window;
  55. struct Screen *screen;
  56. struct TextFont *font;
  57.  
  58.  
  59. UWORD _colors[16] =
  60.    {
  61.    0xe,
  62.    0x0,
  63.    0xeee,
  64.    0xddd,
  65.    0xccc,
  66.    0xbbb,
  67.    0xaaa,
  68.    0x999,
  69.    0x97,
  70.    0x888,
  71.    0xaaa,
  72.    0xfca,
  73.    0xfe0,
  74.    0xccc,
  75.    0x888,
  76.    0x444,
  77.    };
  78.  
  79. void test_func(i)
  80. short i;
  81. {
  82. }
  83.  
  84. /* bit maps in butimg.c */
  85. extern struct Image button_out, button_pressed;
  86.  
  87. #define RADIO_BUTTON 0x8000
  88. #define RADIO_ACTIVATE (TOGGLESELECT | GADGIMMEDIATE)
  89. #define DUAL_IMAGE (GADGHIMAGE | GADGIMAGE)
  90.  
  91. /* There must be one of these structures for each set of radio buttons.
  92.  * The 'value' field must point to a separate variable for each set of
  93.  * radio buttons. When a button is pressed the 'value' field will be
  94.  * changed to the GadgetID & ~RADIO_BUTTON. Also if the 'func' field is
  95.  * non-NULL the attached function is called with '*value' as the argument.
  96.  */
  97.  
  98. typedef struct
  99. {
  100. void (*func)();       /* Function to call */
  101. UWORD *value;         /* Value to change */
  102. } RadioInfo;
  103.  
  104. UWORD variable = 5;  /* Set the initial state to what you want. */
  105.  
  106. RadioInfo rad_info =
  107. {
  108. test_func,
  109. &variable,
  110. };
  111.  
  112. /* It is important that the gadget ID be 'ord' with RADIO_BUTTON for the
  113.  * functions to know this is a radio button. Also be sure you set the
  114.  * TOGGLESELECT activation flag. The buttons only make sense if they have
  115.  * two visual states, use GadgetRender and SelectRender. */
  116.  
  117. struct Gadget buttons[] =
  118. {
  119.  
  120.    { /* 0 */
  121.    &buttons[1],
  122.    20,20,22,16,
  123.    DUAL_IMAGE,
  124.    RADIO_ACTIVATE,
  125.    BOOLGADGET,
  126.    (APTR)&button_out,
  127.    (APTR)&button_pressed,
  128.    NULL,
  129.    0L,
  130.    NULL,
  131.    RADIO_BUTTON | 0,
  132.    (APTR)&rad_info,
  133.    },
  134.  
  135.    { /* 1 */
  136.    &buttons[2],
  137.    50,20,22,16,
  138.    DUAL_IMAGE,
  139.    RADIO_ACTIVATE ,
  140.    BOOLGADGET,
  141.    (APTR)&button_out,
  142.    (APTR)&button_pressed,
  143.    NULL,
  144.    0L,
  145.    NULL,
  146.    RADIO_BUTTON | 1,
  147.    (APTR)&rad_info,
  148.    },
  149.  
  150.    { /* 2 */
  151.    &buttons[3],
  152.    80,20,22,16,
  153.    DUAL_IMAGE,
  154.    RADIO_ACTIVATE,
  155.    BOOLGADGET,
  156.    (APTR)&button_out,
  157.    (APTR)&button_pressed,
  158.    NULL,
  159.    0L,
  160.    NULL,
  161.    RADIO_BUTTON | 2,
  162.    (APTR)&rad_info,
  163.    },
  164.  
  165.    { /* 3 */
  166.    &buttons[4],
  167.    110,20,22,16,
  168.    DUAL_IMAGE,
  169.    RADIO_ACTIVATE,
  170.    BOOLGADGET,
  171.    (APTR)&button_out,
  172.    (APTR)&button_pressed,
  173.    NULL,
  174.    0L,
  175.    NULL,
  176.    RADIO_BUTTON | 3,
  177.    (APTR)&rad_info,
  178.    },
  179.    {  /* 4 */
  180.    &buttons[5],
  181.    140,20,22,16,
  182.    DUAL_IMAGE,
  183.    RADIO_ACTIVATE,
  184.    BOOLGADGET,
  185.    (APTR)&button_out,
  186.    (APTR)&button_pressed,
  187.    NULL,
  188.    0L,
  189.    NULL,
  190.    RADIO_BUTTON | 4,
  191.    (APTR)&rad_info,
  192.    },
  193.    {  /* 5 */
  194.    &buttons[6],
  195.    20,40,22,16,
  196.    DUAL_IMAGE,
  197.    RADIO_ACTIVATE,
  198.    BOOLGADGET,
  199.    (APTR)&button_out,
  200.    (APTR)&button_pressed,
  201.    NULL,
  202.    0L,
  203.    NULL,
  204.    RADIO_BUTTON | 5,
  205.    (APTR)&rad_info,
  206.    },
  207.    {  /* 6 */
  208.    &buttons[7],
  209.    50,40,22,16,
  210.    DUAL_IMAGE,
  211.    RADIO_ACTIVATE,
  212.    BOOLGADGET,
  213.    (APTR)&button_out,
  214.    (APTR)&button_pressed,
  215.    NULL,
  216.    0L,
  217.    NULL,
  218.    RADIO_BUTTON | 6,
  219.    (APTR)&rad_info,
  220.    },
  221.    {  /* 7 */
  222.    &buttons[8],
  223.    80,40,22,16,
  224.    DUAL_IMAGE,
  225.    RADIO_ACTIVATE,
  226.    BOOLGADGET,
  227.    (APTR)&button_out,
  228.    (APTR)&button_pressed,
  229.    NULL,
  230.    0L,
  231.    NULL,
  232.    RADIO_BUTTON | 7,
  233.    (APTR)&rad_info,
  234.    },
  235.    {  /* 8 */
  236.    &buttons[9],
  237.    110,40,22,16,
  238.    DUAL_IMAGE,
  239.    RADIO_ACTIVATE,
  240.    BOOLGADGET,
  241.    (APTR)&button_out,
  242.    (APTR)&button_pressed,
  243.    NULL,
  244.    0L,
  245.    NULL,
  246.    RADIO_BUTTON | 8,
  247.    (APTR)&rad_info,
  248.    },
  249.    {  /* 9 */
  250.    &buttons[10],
  251.    140,40,22,16,
  252.    DUAL_IMAGE,
  253.    RADIO_ACTIVATE,
  254.    BOOLGADGET,
  255.    (APTR)&button_out,
  256.    (APTR)&button_pressed,
  257.    NULL,
  258.    0L,
  259.    NULL,
  260.    RADIO_BUTTON | 9,
  261.    (APTR)&rad_info,
  262.    },
  263.    {  /* 10 */
  264.    &buttons[11],
  265.    20,60,22,16,
  266.    DUAL_IMAGE,
  267.    RADIO_ACTIVATE,
  268.    BOOLGADGET,
  269.    (APTR)&button_out,
  270.    (APTR)&button_pressed,
  271.    NULL,
  272.    0L,
  273.    NULL,
  274.    RADIO_BUTTON | 10,
  275.    (APTR)&rad_info,
  276.    },
  277.    {  /* 11 */
  278.    &buttons[12],
  279.    50,60,22,16,
  280.    DUAL_IMAGE,
  281.    RADIO_ACTIVATE,
  282.    BOOLGADGET,
  283.    (APTR)&button_out,
  284.    (APTR)&button_pressed,
  285.    NULL,
  286.    0L,
  287.    NULL,
  288.    RADIO_BUTTON | 11,
  289.    (APTR)&rad_info,
  290.    },
  291.    {  /* 12 */
  292.    &buttons[13],
  293.    80,60,22,16,
  294.    DUAL_IMAGE,
  295.    RADIO_ACTIVATE,
  296.    BOOLGADGET,
  297.    (APTR)&button_out,
  298.    (APTR)&button_pressed,
  299.    NULL,
  300.    0L,
  301.    NULL,
  302.    RADIO_BUTTON | 12,
  303.    (APTR)&rad_info,
  304.    },
  305.    {  /* 13 */
  306.    &buttons[14],
  307.    110,60,22,16,
  308.    DUAL_IMAGE,
  309.    RADIO_ACTIVATE,
  310.    BOOLGADGET,
  311.    (APTR)&button_out,
  312.    (APTR)&button_pressed,
  313.    NULL,
  314.    0L,
  315.    NULL,
  316.    RADIO_BUTTON | 13,
  317.    (APTR)&rad_info,
  318.    },
  319.    {  /* 14 */
  320.    &buttons[15],
  321.    140,60,22,16,
  322.    DUAL_IMAGE,
  323.    RADIO_ACTIVATE,
  324.    BOOLGADGET,
  325.    (APTR)&button_out,
  326.    (APTR)&button_pressed,
  327.    NULL,
  328.    0L,
  329.    NULL,
  330.    RADIO_BUTTON | 14,
  331.    (APTR)&rad_info,
  332.    },
  333.    { /* 15 */
  334.    &buttons[16],
  335.    20,80,22,16,
  336.    DUAL_IMAGE,
  337.    RADIO_ACTIVATE,
  338.    BOOLGADGET,
  339.    (APTR)&button_out,
  340.    (APTR)&button_pressed,
  341.    NULL,
  342.    0L,
  343.    NULL,
  344.    RADIO_BUTTON | 15,
  345.    (APTR)&rad_info,
  346.    },
  347.  
  348.    { /* 16 */
  349.    &buttons[17],
  350.    50,80,22,16,
  351.    DUAL_IMAGE,
  352.    RADIO_ACTIVATE,
  353.    BOOLGADGET,
  354.    (APTR)&button_out,
  355.    (APTR)&button_pressed,
  356.    NULL,
  357.    0L,
  358.    NULL,
  359.    RADIO_BUTTON | 16,
  360.    (APTR)&rad_info,
  361.    },
  362.  
  363.    { /* 17 */
  364.    &buttons[18],
  365.    80,80,22,16,
  366.    DUAL_IMAGE,
  367.    RADIO_ACTIVATE,
  368.    BOOLGADGET,
  369.    (APTR)&button_out,
  370.    (APTR)&button_pressed,
  371.    NULL,
  372.    0L,
  373.    NULL,
  374.    RADIO_BUTTON | 17,
  375.    (APTR)&rad_info,
  376.    },
  377.  
  378.    { /* 18 */
  379.    &buttons[19],
  380.    110,80,22,16,
  381.    DUAL_IMAGE,
  382.    RADIO_ACTIVATE,
  383.    BOOLGADGET,
  384.    (APTR)&button_out,
  385.    (APTR)&button_pressed,
  386.    NULL,
  387.    0L,
  388.    NULL,
  389.    RADIO_BUTTON | 18,
  390.    (APTR)&rad_info,
  391.    },
  392.    {  /* 19 */
  393.    &buttons[20],
  394.    140,80,22,16,
  395.    DUAL_IMAGE,
  396.    RADIO_ACTIVATE,
  397.    BOOLGADGET,
  398.    (APTR)&button_out,
  399.    (APTR)&button_pressed,
  400.    NULL,
  401.    0L,
  402.    NULL,
  403.    RADIO_BUTTON | 19,
  404.    (APTR)&rad_info,
  405.    },
  406.    {  /* 20 */
  407.    &buttons[21],
  408.    20,100,22,16,
  409.    DUAL_IMAGE,
  410.    RADIO_ACTIVATE,
  411.    BOOLGADGET,
  412.    (APTR)&button_out,
  413.    (APTR)&button_pressed,
  414.    NULL,
  415.    0L,
  416.    NULL,
  417.    RADIO_BUTTON | 20,
  418.    (APTR)&rad_info,
  419.    },
  420.    {  /* 21 */
  421.    &buttons[22],
  422.    50,100,22,16,
  423.    DUAL_IMAGE,
  424.    RADIO_ACTIVATE,
  425.    BOOLGADGET,
  426.    (APTR)&button_out,
  427.    (APTR)&button_pressed,
  428.    NULL,
  429.    0L,
  430.    NULL,
  431.    RADIO_BUTTON | 21,
  432.    (APTR)&rad_info,
  433.    },
  434.    {  /* 22 */
  435.    &buttons[23],
  436.    80,100,22,16,
  437.    DUAL_IMAGE,
  438.    RADIO_ACTIVATE,
  439.    BOOLGADGET,
  440.    (APTR)&button_out,
  441.    (APTR)&button_pressed,
  442.    NULL,
  443.    0L,
  444.    NULL,
  445.    RADIO_BUTTON | 22,
  446.    (APTR)&rad_info,
  447.    },
  448.    {  /* 23 */
  449.    &buttons[24],
  450.    110,100,22,16,
  451.    DUAL_IMAGE,
  452.    RADIO_ACTIVATE,
  453.    BOOLGADGET,
  454.    (APTR)&button_out,
  455.    (APTR)&button_pressed,
  456.    NULL,
  457.    0L,
  458.    NULL,
  459.    RADIO_BUTTON | 23,
  460.    (APTR)&rad_info,
  461.    },
  462.    {   /* 24 */
  463.    NULL,
  464.    140,100,22,16,
  465.    DUAL_IMAGE,
  466.    RADIO_ACTIVATE,
  467.    BOOLGADGET,
  468.    (APTR)&button_out,
  469.    (APTR)&button_pressed,
  470.    NULL,
  471.    0L,
  472.    NULL,
  473.    RADIO_BUTTON | 24,
  474.    (APTR)&rad_info,
  475.    },
  476. };
  477.  
  478.  
  479. /* TextAttr Declaration */
  480. static struct TextAttr _ta =
  481. {
  482. (STRPTR) "topaz.font",
  483. 8,
  484. FS_NORMAL,
  485. ZILCH | FPF_ROMFONT,
  486. };
  487.  
  488. /* NewScreen DECLARATION */
  489. static struct NewScreen _ns =
  490. {
  491. 0, 0, 640, 400, 4,
  492. (UBYTE) 1,
  493. (UBYTE) 0,
  494. ZILCH | HIRES | LACE,
  495. CUSTOMSCREEN,
  496. &_ta,
  497. (UBYTE *) "Button Test",
  498. 0L,
  499. 0L,
  500. };
  501.  
  502.  
  503. /* NewWindow DECLARATIONS */
  504. static struct NewWindow _nw =
  505. {
  506. 0, 0, 640, 400,
  507. (UBYTE) 0,
  508. (UBYTE) 1,
  509. ZILCH | GADGETDOWN | GADGETUP | RAWKEY,
  510. ZILCH | BACKDROP | BORDERLESS | ACTIVATE
  511.  | SMART_REFRESH,
  512. buttons,
  513. 0L,
  514. 0L,
  515. 0L,
  516. 0L,
  517. 2, 2,
  518. 640, 400,
  519. CUSTOMSCREEN,
  520. },
  521. ;
  522.  
  523.  
  524. /*** *** *** *** *** *** _initialize() *** *** *** *** *** *** *** ***/
  525.  
  526. struct Window *_initialize()
  527. {
  528. extern void _goodbye();
  529. struct ViewPort *vp;
  530.  
  531. /* OPEN THE LIBRARIES */
  532. IntuitionBase =
  533. (struct IntuitionBase*)OpenLibrary("intuition.library",(long) INTUITION_REV);
  534. if( !IntuitionBase ) goto abort;
  535.  
  536. GfxBase =
  537. (struct GfxBase*)OpenLibrary("graphics.library", (long) GRAPHICS_REV);
  538. if( !GfxBase ) goto abort;
  539.  
  540. /* OPEN THE FONT */
  541. if( !( font = OpenFont( &_ta ) ) )
  542.    goto abort;
  543.  
  544. /* OPEN THE SCREEN */
  545. if( !(screen = OpenScreen( &_ns ) ) )
  546.    goto abort;
  547.  
  548. /* OPEN THE WINDOW */
  549. _nw.Screen = screen;
  550. if( !( window = OpenWindow( &_nw ) ) )
  551.    goto abort;
  552.  
  553. SetFont(window->RPort,font);
  554.  
  555. /* LOAD COLORS INTO VIEWPORT */
  556. vp = ViewPortAddress(window);
  557. LoadRGB4(vp,_colors,16L);
  558.  
  559. return(window);
  560.  
  561. abort:
  562. _goodbye();
  563. return(0L);
  564. }
  565.  
  566.  
  567. /*** *** *** *** *** *** _goodbye *** *** *** *** *** *** *** *** ***/
  568.  
  569. void _goodbye()
  570. {
  571.  
  572. if( font )
  573.    CloseFont(font);
  574. if( window )
  575.    CloseWindow(window);
  576. if( screen )
  577.    CloseScreen(screen);
  578.  
  579. if( IntuitionBase )
  580.    CloseLibrary(IntuitionBase);
  581. if( GfxBase )
  582.    CloseLibrary(GfxBase);
  583. if( DiskfontBase )
  584.       CloseLibrary( DiskfontBase);
  585. }
  586.  
  587.  
  588. main()
  589. {
  590. extern void HandleGadgets();
  591. extern void SetRadioButtons();
  592.  
  593. struct Window *w;
  594. struct IntuiMessage I, *msg;
  595.  
  596. if( w = _initialize() )
  597.    {
  598.  
  599.    SetRadioButtons(w);
  600.  
  601.    for(;;)
  602.       {
  603.       Wait( 1L<<w->UserPort->mp_SigBit );
  604.       while( msg = (struct IntuiMessage *) GetMsg(w->UserPort) )
  605.          {
  606.          I = *msg;
  607.          ReplyMsg(msg);
  608.  
  609.          if( I.Class == RAWKEY )
  610.             goto stop;
  611.          else if( I.Class == GADGETDOWN )
  612.          /* You should call this function when you get gadget related
  613.           * messages. It will take care of the 'radio buttons' for you. */
  614.             HandleGadgets(&I);
  615.          }
  616.       }
  617.  
  618.    stop:
  619.    _goodbye();
  620.    }
  621. }
  622.  
  623. /* Takes care of radio button IO */
  624.  
  625. void HandleGadgets(i)
  626. struct IntuiMessage *i;
  627. {
  628. register struct Gadget *this_one = (struct Gadget *)i->IAddress;
  629.  
  630. /* Test to see if this is a radio button */
  631.  
  632. if( this_one->GadgetID & RADIO_BUTTON )
  633.    {
  634.    register struct Gadget *g = i->IDCMPWindow->FirstGadget;
  635.    register RadioInfo *info = (RadioInfo *)this_one->UserData;
  636.  
  637.    /* find old button and turn it off */
  638.    for(; g; g = g->NextGadget)
  639.       if( g->GadgetID & RADIO_BUTTON )
  640.          if( *info->value == (g->GadgetID & ~RADIO_BUTTON) &&
  641.              g->UserData == ((APTR)info) )
  642.                break;
  643.  
  644.    if( !g ) return; /* Abortion */
  645.  
  646.  
  647.    /* Modify the gadget images */
  648.    if( g != this_one )
  649.       {
  650.       g->Flags &= ~SELECTED;
  651.       RefreshGList(g,i->IDCMPWindow,0L,1);
  652.       }
  653.    else if( !(g->Flags & SELECTED) )
  654.       {
  655.       g->Flags |= SELECTED;
  656.       RefreshGList(g,i->IDCMPWindow,0L,1);
  657.       }
  658.  
  659.    /* Set the value of the radio button varaible */
  660.    *info->value = (this_one->GadgetID & ~RADIO_BUTTON);
  661.  
  662.    /* Call user function if there is one */
  663.    if( info->func )
  664.       (*info->func)(*info->value);
  665.    }
  666. }
  667.  
  668. /* Call this function after you change the radio button variable or
  669.  * to initialize the visual state. */
  670.  
  671. void SetRadioButtons(w)
  672. struct Window *w;
  673. {
  674. register struct Gadget *g = w->FirstGadget;
  675.  
  676. for(; g; g = g->NextGadget )
  677.    if( g->GadgetID & RADIO_BUTTON )
  678.       {
  679.       if( (*((RadioInfo *)g->UserData)->value) ==
  680.           (g->GadgetID & ~RADIO_BUTTON) )
  681.          g->Flags |= SELECTED;
  682.       else
  683.          g->Flags &= ~SELECTED;
  684.       RefreshGList(g,w,0L,1);
  685.       }
  686. }
  687.  
  688. /* Have fun girls and boys */
  689.