### 2.6.3 Example 5, Button with a Menu Again

Now we implement example 4 again using event handlers. This time, the widget behaves more or less like a menu button. I.e., when you press the left mouse button on the button, it pops up the menu. To achieve this, we have to discard the Button1 press events from the default event handler. We do so by invoking EZ_RemoveEvent after we handle the event.

    /************************** Example 5 ***********************/
    #include "EZ.h"                     /* the header file      */

    static char *colors[] = {  "red", "green", "blue", "yellow",
    			   "cyan", "magenta",  "white", NULL};

                                /* a very simple event handler */
    void buttonEventHandler(EZ_Widget *widget, void *data,
    			int eType, XEvent *xev)
    {
      if(widget)
        {
          if(eType == EZ_LEFT_BUTTON_PRESS)
    	{
    	  EZ_Widget *menu = (EZ_Widget *)data;
    	  EZ_DoPopup(menu, EZ_BOTTOM);
              /* disable all other event handlers on this event,
    	   * include the default one !! */
    	  EZ_RemoveEvent(xev);
    	}
        }
    }

    void menuCallBack(EZ_Widget *widget, void *data)
    {
      if(widget)
        {
          int i = EZ_GetWidgetReturnedData(widget);
          if( i >= 0 && i <= 6)    /* to make sure */
    	{
    	  EZ_Widget *tmp = (EZ_Widget *)data;
    	  if(tmp)
    	    {
    	      char str[64];
    	      sprintf(str,"A %s Button", colors[i]);
    	      EZ_ConfigureWidget(tmp, EZ_LABEL_STRING, str,
    				 EZ_FOREGROUND, colors[i],0);
    	    }
    	}
        }
    }

    main(int argc, char **argv)
    {
      EZ_Widget *menu, *button, *tmp;

      EZ_Initialize(argc, argv, 0);

      button = EZ_CreateWidget(EZ_WIDGET_NORMAL_BUTTON, NULL,
                               EZ_LABEL_STRING,"An Optional Button",
                               0); /* a button */

      menu = EZ_CreateWidget(EZ_WIDGET_POPUP_MENU, NULL, 0);

      EZ_AddEventHandler(button, buttonEventHandler, menu, 0);

      EZ_AddWidgetCallBack(menu, EZ_CALLBACK,
    		       menuCallBack, button, 0);
      {
        char **ptr = colors;
        int i = 0;
        while(*ptr)
          {
    	tmp = EZ_CreateWidget(EZ_WIDGET_MENU_NORMAL_BUTTON, menu,
                                  EZ_LABEL_STRING, *ptr,
                                  EZ_RETURN_VALUE, i++,
                                  0);
    	ptr++;
          }
      }

      EZ_DisplayWidget(button);
      EZ_EventMainLoop();
    }
    /************************** Example 5 ***********************/

HTML Documentation Maintainance:`Arturo Espinosa <arturo@nuclecu.unam.mx>`