Next Previous Contents

13. Menu Widget

This section is under construction yet. But the example is available though.

-- example-start menu menu.e
class MENU
inherit
   GTK_CONSTANTS
   VEGTK_MAIN
   VEGTK_CALLBACK_HANDLER
creation
   make
feature {ANY} -- Creation
   make is
   local
      window : GTK_WINDOW;
      menu : GTK_MENU;
      menu_bar : GTK_MENU_BAR;
      menu_item, root_menu : GTK_MENU_ITEM;
      vbox : GTK_VBOX;
      button : GTK_BUTTON;
      buf : STRING;
      i : INTEGER;
   do
      -- Initialize gtk+ (and subsequently gdk)
      vegtk_init;

      -- Create a window to put all the widgets in
      -- connect gtk_main_quit() to the "destroy" event of
      -- the window to handle window manager close-window-events
      -- create a new window
      !!window.make (GTK_WINDOW_TOPLEVEL);
      window.set_usize (200,100);
      window.set_title ("GTK Menu Test");
      signal_connect (window, "destroy",$destroy);

      -- Init the menu-widget, and remember -- never
      -- show the menu widget!!
      -- This is the menu that holds the menu items, the one that
      -- will pop up when you click on the "Root Menu" in the app
      !!menu.make;

      -- Next we make a little loop that makes three menu-entries for
      -- "test-menu". Notice the call to append.  Here we are adding a list of
      -- menu items to our menu.  Normally, we'd also catch the "clicked"
      -- signal on each of the menu items and setup a callback for it,
      -- but it's omitted here to save space.

      from
         i := 1;
      until
         i > 3;
      loop
         -- Copy the names to the buf.
         !!buf.make_from_string ("Test-undermenu - ");
         buf.append_integer (i);

         -- Create a new menu-item with a name...
         !!menu_item.make_with_label(buf);

         -- ...and add it to the menu.
         menu.append(menu_item);

         -- Do something interesting when the menuitem is selected
         signal_connect_with_data (menu_item,"activate", $menuitem_response, buf);

         -- Show the widget
         menu_item.show;
         i := i+1;
      end

      -- This is the root menu, and will be the label
      -- displayed on the menu bar.  There won't be a signal handler attached,
      -- as it only pops up the rest of the menu when pressed. */
      !!root_menu.make_with_label("Root Menu");

      root_menu.show;

      -- Now we specify that we want our newly created "menu" to be the menu
      -- for the "root menu"
      root_menu.set_submenu (menu);

      -- A vbox to put a menu and a button in:
      !!vbox.make (FALSE, 0);
      window.add (vbox);
      vbox.show;

      -- Create a menu-bar to hold the menus and add it to our main window
      !!menu_bar.make;
      vbox.pack_start(menu_bar, FALSE, FALSE, 2);
      menu_bar.show;

      -- Create a button to which to attach menu as a popup
      !!button.make_with_label("press me");
      signal_connect_with_data (button, "event",$button_press, menu);
      vbox.pack_end (button, TRUE, TRUE, 2);
      button.show;

      -- And finally we append the menu-item to the menu-bar -- this is the
      -- "root" menu-item I have been raving about =)
      menu_bar.append (root_menu);

      -- always display the window as the last step so it all splashes on
      -- the screen at once. */
      window.show;

      gtk_main;
   end -- make

feature {NONE} -- Callbacks

   destroy is
   do
      gtk_main_quit;
   end

   button_press (data : ANY; cb_data : VEGTK_CALLBACK_DATA) is
   -- Respond to a button-press by posting a menu passed in as widget.
   --
   -- Note that the "menu" is the menu being posted, NOT
   -- the button that was pressed.
   local
      menu : GTK_MENU;
      event : GDK_EVENT_BUTTON;
   do
      event ?= cb_data.get_event;

      if event /= Void and then event.type.is_equal (GDK_BUTTON_PRESS) then
         menu ?= data;
         menu.popup (Void, Void, event.button, event.time);
         -- Tell calling code that we have handled this event; the buck
         -- stops here.
         cb_data.set_return_value_boolean (TRUE);
      else
         -- Tell calling code that we have not handled this event; pass it on.
         cb_data.set_return_value_boolean (False);
      end
   end


   menuitem_response (data : ANY) is
      -- Print a string when a menu item is selected
   do
      print(data);
      print ("%N");
   end
end -- class MENU
-- example-end

Next Previous Contents