1999-02-01 14:47:05 +00:00
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
/* Obligatory basic callback */
|
1999-11-13 23:06:46 +00:00
|
|
|
static void print_hello( GtkWidget *w,
|
|
|
|
gpointer data )
|
|
|
|
{
|
|
|
|
g_message ("Hello, World!\n");
|
1999-02-01 14:47:05 +00:00
|
|
|
}
|
|
|
|
|
2002-08-26 11:35:57 +00:00
|
|
|
/* For the check button */
|
2005-01-03 19:26:36 +00:00
|
|
|
static void print_toggle( gpointer callback_data,
|
|
|
|
guint callback_action,
|
|
|
|
GtkWidget *menu_item )
|
2002-08-26 11:35:57 +00:00
|
|
|
{
|
|
|
|
g_message ("Check button state - %d\n",
|
2005-01-03 19:26:36 +00:00
|
|
|
GTK_CHECK_MENU_ITEM (menu_item)->active);
|
2002-08-26 11:35:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* For the radio buttons */
|
2005-01-03 19:26:36 +00:00
|
|
|
static void print_selected( gpointer callback_data,
|
|
|
|
guint callback_action,
|
|
|
|
GtkWidget *menu_item )
|
2002-08-26 11:35:57 +00:00
|
|
|
{
|
|
|
|
if(GTK_CHECK_MENU_ITEM(menu_item)->active)
|
2005-01-03 19:26:36 +00:00
|
|
|
g_message ("Radio button %d selected\n", callback_action);
|
2002-08-26 11:35:57 +00:00
|
|
|
}
|
1999-02-01 14:47:05 +00:00
|
|
|
|
2002-08-26 11:35:57 +00:00
|
|
|
/* Our menu, an array of GtkItemFactoryEntry structures that defines each menu item */
|
1999-02-01 14:47:05 +00:00
|
|
|
static GtkItemFactoryEntry menu_items[] = {
|
2002-08-26 11:35:57 +00:00
|
|
|
{ "/_File", NULL, NULL, 0, "<Branch>" },
|
2003-01-23 21:08:59 +00:00
|
|
|
{ "/File/_New", "<control>N", print_hello, 0, "<StockItem>", GTK_STOCK_NEW },
|
|
|
|
{ "/File/_Open", "<control>O", print_hello, 0, "<StockItem>", GTK_STOCK_OPEN },
|
|
|
|
{ "/File/_Save", "<control>S", print_hello, 0, "<StockItem>", GTK_STOCK_SAVE },
|
2002-08-26 11:35:57 +00:00
|
|
|
{ "/File/Save _As", NULL, NULL, 0, "<Item>" },
|
|
|
|
{ "/File/sep1", NULL, NULL, 0, "<Separator>" },
|
2003-01-14 23:33:03 +00:00
|
|
|
{ "/File/_Quit", "<CTRL>Q", gtk_main_quit, 0, "<StockItem>", GTK_STOCK_QUIT },
|
2002-08-26 11:35:57 +00:00
|
|
|
{ "/_Options", NULL, NULL, 0, "<Branch>" },
|
|
|
|
{ "/Options/tear", NULL, NULL, 0, "<Tearoff>" },
|
|
|
|
{ "/Options/Check", NULL, print_toggle, 1, "<CheckItem>" },
|
|
|
|
{ "/Options/sep", NULL, NULL, 0, "<Separator>" },
|
|
|
|
{ "/Options/Rad1", NULL, print_selected, 1, "<RadioItem>" },
|
|
|
|
{ "/Options/Rad2", NULL, print_selected, 2, "/Options/Rad1" },
|
|
|
|
{ "/Options/Rad3", NULL, print_selected, 3, "/Options/Rad1" },
|
|
|
|
{ "/_Help", NULL, NULL, 0, "<LastBranch>" },
|
|
|
|
{ "/_Help/About", NULL, NULL, 0, "<Item>" },
|
1999-02-01 14:47:05 +00:00
|
|
|
};
|
|
|
|
|
2002-08-26 11:35:57 +00:00
|
|
|
static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
|
1999-02-01 14:47:05 +00:00
|
|
|
|
2002-08-26 11:35:57 +00:00
|
|
|
/* Returns a menubar widget made from the above menu */
|
2005-01-03 19:26:36 +00:00
|
|
|
static GtkWidget *get_menubar_menu( GtkWidget *window )
|
1999-11-13 23:06:46 +00:00
|
|
|
{
|
1999-02-01 14:47:05 +00:00
|
|
|
GtkItemFactory *item_factory;
|
|
|
|
GtkAccelGroup *accel_group;
|
|
|
|
|
2002-08-26 11:35:57 +00:00
|
|
|
/* Make an accelerator group (shortcut keys) */
|
1999-11-13 23:06:46 +00:00
|
|
|
accel_group = gtk_accel_group_new ();
|
1999-02-01 14:47:05 +00:00
|
|
|
|
2002-08-26 11:35:57 +00:00
|
|
|
/* Make an ItemFactory (that makes a menubar) */
|
|
|
|
item_factory = gtk_item_factory_new (GTK_TYPE_MENU_BAR, "<main>",
|
|
|
|
accel_group);
|
1999-02-01 14:47:05 +00:00
|
|
|
|
|
|
|
/* This function generates the menu items. Pass the item factory,
|
|
|
|
the number of items in the array, the array itself, and any
|
|
|
|
callback data for the the menu items. */
|
1999-11-13 23:06:46 +00:00
|
|
|
gtk_item_factory_create_items (item_factory, nmenu_items, menu_items, NULL);
|
1999-02-01 14:47:05 +00:00
|
|
|
|
|
|
|
/* Attach the new accelerator group to the window. */
|
1999-11-13 23:06:46 +00:00
|
|
|
gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
|
1999-02-01 14:47:05 +00:00
|
|
|
|
2002-08-26 11:35:57 +00:00
|
|
|
/* Finally, return the actual menu bar created by the item factory. */
|
|
|
|
return gtk_item_factory_get_widget (item_factory, "<main>");
|
1999-02-01 14:47:05 +00:00
|
|
|
}
|
|
|
|
|
2002-08-26 11:35:57 +00:00
|
|
|
/* Popup the menu when the popup button is pressed */
|
2005-01-03 19:26:36 +00:00
|
|
|
static gboolean popup_cb( GtkWidget *widget,
|
|
|
|
GdkEvent *event,
|
|
|
|
GtkWidget *menu )
|
2002-08-26 11:35:57 +00:00
|
|
|
{
|
|
|
|
GdkEventButton *bevent = (GdkEventButton *)event;
|
|
|
|
|
|
|
|
/* Only take button presses */
|
2005-01-03 19:26:36 +00:00
|
|
|
if (event->type != GDK_BUTTON_PRESS)
|
2002-08-26 11:35:57 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* Show the menu */
|
2005-01-03 19:26:36 +00:00
|
|
|
gtk_menu_popup (GTK_MENU(menu), NULL, NULL,
|
|
|
|
NULL, NULL, bevent->button, bevent->time);
|
2002-08-26 11:35:57 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Same as with get_menubar_menu() but just return a button with a signal to
|
|
|
|
call a popup menu */
|
2005-01-03 19:26:36 +00:00
|
|
|
GtkWidget *get_popup_menu( void )
|
2002-08-26 11:35:57 +00:00
|
|
|
{
|
|
|
|
GtkItemFactory *item_factory;
|
|
|
|
GtkWidget *button, *menu;
|
|
|
|
|
|
|
|
/* Same as before but don't bother with the accelerators */
|
|
|
|
item_factory = gtk_item_factory_new (GTK_TYPE_MENU, "<main>",
|
|
|
|
NULL);
|
|
|
|
gtk_item_factory_create_items (item_factory, nmenu_items, menu_items, NULL);
|
2005-01-03 19:26:36 +00:00
|
|
|
menu = gtk_item_factory_get_widget (item_factory, "<main>");
|
2002-08-26 11:35:57 +00:00
|
|
|
|
|
|
|
/* Make a button to activate the popup menu */
|
2005-01-03 19:26:36 +00:00
|
|
|
button = gtk_button_new_with_label ("Popup");
|
2002-08-26 11:35:57 +00:00
|
|
|
/* Make the menu popup when clicked */
|
2005-01-03 19:26:36 +00:00
|
|
|
g_signal_connect (G_OBJECT(button),
|
|
|
|
"event",
|
|
|
|
G_CALLBACK(popup_cb),
|
|
|
|
(gpointer) menu);
|
2002-08-26 11:35:57 +00:00
|
|
|
|
|
|
|
return button;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Same again but return an option menu */
|
2005-01-03 19:26:36 +00:00
|
|
|
GtkWidget *get_option_menu( void )
|
2002-08-26 11:35:57 +00:00
|
|
|
{
|
|
|
|
GtkItemFactory *item_factory;
|
|
|
|
GtkWidget *option_menu;
|
|
|
|
|
|
|
|
/* Same again, not bothering with the accelerators */
|
|
|
|
item_factory = gtk_item_factory_new (GTK_TYPE_OPTION_MENU, "<main>",
|
|
|
|
NULL);
|
|
|
|
gtk_item_factory_create_items (item_factory, nmenu_items, menu_items, NULL);
|
2005-01-03 19:26:36 +00:00
|
|
|
option_menu = gtk_item_factory_get_widget (item_factory, "<main>");
|
2002-08-26 11:35:57 +00:00
|
|
|
|
|
|
|
return option_menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* You have to start somewhere */
|
1999-11-13 23:06:46 +00:00
|
|
|
int main( int argc,
|
|
|
|
char *argv[] )
|
|
|
|
{
|
1999-02-01 14:47:05 +00:00
|
|
|
GtkWidget *window;
|
|
|
|
GtkWidget *main_vbox;
|
2002-08-26 11:35:57 +00:00
|
|
|
GtkWidget *menubar, *option_menu, *popup_button;
|
|
|
|
|
|
|
|
/* Initialize GTK */
|
1999-11-13 23:06:46 +00:00
|
|
|
gtk_init (&argc, &argv);
|
2002-08-26 11:35:57 +00:00
|
|
|
|
|
|
|
/* Make a window */
|
1999-11-13 23:06:46 +00:00
|
|
|
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
2002-08-26 11:35:57 +00:00
|
|
|
g_signal_connect (G_OBJECT (window), "destroy",
|
|
|
|
G_CALLBACK (gtk_main_quit),
|
|
|
|
NULL);
|
|
|
|
gtk_window_set_title (GTK_WINDOW(window), "Item Factory");
|
|
|
|
gtk_widget_set_size_request (GTK_WIDGET(window), 300, 200);
|
|
|
|
|
|
|
|
/* Make a vbox to put the three menus in */
|
1999-11-13 23:06:46 +00:00
|
|
|
main_vbox = gtk_vbox_new (FALSE, 1);
|
2002-02-16 23:52:30 +00:00
|
|
|
gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 1);
|
1999-11-13 23:06:46 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (window), main_vbox);
|
2002-08-26 11:35:57 +00:00
|
|
|
|
|
|
|
/* Get the three types of menu */
|
|
|
|
/* Note: all three menus are separately created, so they are not the
|
|
|
|
same menu */
|
|
|
|
menubar = get_menubar_menu (window);
|
2005-01-03 19:26:36 +00:00
|
|
|
popup_button = get_popup_menu ();
|
|
|
|
option_menu = get_option_menu ();
|
1999-02-01 14:47:05 +00:00
|
|
|
|
2002-08-26 11:35:57 +00:00
|
|
|
/* Pack it all together */
|
1999-11-13 23:06:46 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (main_vbox), menubar, FALSE, TRUE, 0);
|
2002-08-26 11:35:57 +00:00
|
|
|
gtk_box_pack_end (GTK_BOX (main_vbox), popup_button, FALSE, TRUE, 0);
|
|
|
|
gtk_box_pack_end (GTK_BOX (main_vbox), option_menu, FALSE, TRUE, 0);
|
2002-02-19 01:25:26 +00:00
|
|
|
|
2002-08-26 11:35:57 +00:00
|
|
|
/* Show the widgets */
|
|
|
|
gtk_widget_show_all (window);
|
1999-02-01 14:47:05 +00:00
|
|
|
|
2002-08-26 11:35:57 +00:00
|
|
|
/* Finished! */
|
|
|
|
gtk_main ();
|
|
|
|
|
2005-01-03 19:26:36 +00:00
|
|
|
return 0;
|
1999-02-01 14:47:05 +00:00
|
|
|
}
|