#!/usr/bin/perl -w use Gtk; use strict; set_locale Gtk; init Gtk; my $false = 0; my $true = 1; my $i; my $window; my $menu; my $menubar; my $root_menu; my $menu_item; my $vbox; my $button; # create a new window $window = new Gtk::Window( 'toplevel' ); $window->set_usize( 200, 100 ); $window->set_title( "GTK Menu Test" ); $window->signal_connect( 'delete_event', sub { Gtk->exit( 0 ); } ); # 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 = new Gtk::Menu(); # 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. for ( $i = 0; $i < 3; $i++ ) { my $buffer; # Copy the names into the buffer $buffer = "Test-undermenu - $i"; # Create a new menu-item with a name... $menu_item = new Gtk::MenuItem( $buffer ); # and add it to the menu. $menu->append( $menu_item ); # Do something interesting when the menuitem is selected $menu_item->signal_connect( 'activate', sub { print( "$buffer\n" ); } ); # Show the widget $menu_item->show(); } # 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 = new Gtk::MenuItem( "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 = new Gtk::VBox( $false, 0 ); $window->add( $vbox ); $vbox->show(); # Create a menu-bar to hold the menus and add it to our main window $menubar = new Gtk::MenuBar(); $vbox->pack_start( $menubar, $false, $false, 2 ); $menubar->show(); # Create a button to which to attach menu as a popup $button = new Gtk::Button( "Press Me" ); $button->signal_connect( '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 =) $menubar->append( $root_menu ); # always display the window as the last step so it all splashes on # the screen at once. $window->show(); main Gtk; exit( 0 ); ### Subroutines # Respond to a button-press by posting a menu passed in as widget. # Note that the "widget" argument is the menu being posted, NOT # the button that was pressed. sub button_press { my ( $button, $menu, $event ) = @_; if ( defined( $event->{ 'type' } ) and ( $event->{ 'type' } eq 'button_press' ) ) { $menu->popup( undef, undef, $event->{'time'}, $event->{'button'}, undef ); # Tell calling code that we have handled this event; the buck # stops here. return ( $true ); } # Tell calling code that we have not handled this event; pass it on. return ( $false ); } # END EXAMPLE PROGRAM