summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorBST 2002 Tony Gale <gale@gtk.org>2002-08-26 11:35:57 +0000
committerTony Gale <gale@src.gnome.org>2002-08-26 11:35:57 +0000
commit5abc7156b6880b213ce770580fa01f82d11052da (patch)
tree1eaa2349feb627ac7130930cc6d9c7c3771fef12 /examples
parent7049e0cb59270dfd6885724805f056f0d133f72a (diff)
downloadgdk-pixbuf-5abc7156b6880b213ce770580fa01f82d11052da.tar.gz
cleanups
Mon Aug 26 12:21:16 BST 2002 Tony Gale <gale@gtk.org> * docs/tutorial/package-db-tutorial.sh: cleanups * docs/tutorial/gtk-tut.sgml: new content for the ItemFactory section. Originally from Robert Cleaver Ancell.
Diffstat (limited to 'examples')
-rw-r--r--examples/menu/itemfactory.c192
1 files changed, 127 insertions, 65 deletions
diff --git a/examples/menu/itemfactory.c b/examples/menu/itemfactory.c
index 76f566e87..c4559c227 100644
--- a/examples/menu/itemfactory.c
+++ b/examples/menu/itemfactory.c
@@ -9,63 +9,58 @@ static void print_hello( GtkWidget *w,
g_message ("Hello, World!\n");
}
-/* This is the GtkItemFactoryEntry structure used to generate new menus.
- Item 1: The menu path. The letter after the underscore indicates an
- accelerator key once the menu is open.
- Item 2: The accelerator key for the entry
- Item 3: The callback function.
- Item 4: The callback action. This changes the parameters with
- which the function is called. The default is 0.
- Item 5: The item type, used to define what kind of an item it is.
- Here are the possible values:
-
- NULL -> "<Item>"
- "" -> "<Item>"
- "<Title>" -> create a title item
- "<Item>" -> create a simple item
- "<CheckItem>" -> create a check item
- "<ToggleItem>" -> create a toggle item
- "<RadioItem>" -> create a radio item
- <path> -> path of a radio item to link against
- "<Separator>" -> create a separator
- "<Branch>" -> create an item to hold sub items (optional)
- "<LastBranch>" -> create a right justified branch
-*/
+/* For the check button */
+static void print_toggle(gpointer callback_data,
+ guint callback_action,
+ GtkWidget *menu_item)
+{
+ g_message ("Check button state - %d\n",
+ GTK_CHECK_MENU_ITEM(menu_item)->active);
+}
+
+/* For the radio buttons */
+static void print_selected(gpointer callback_data,
+ guint callback_action,
+ GtkWidget *menu_item)
+{
+ if(GTK_CHECK_MENU_ITEM(menu_item)->active)
+ g_message("Radio button %d selected\n", callback_action);
+}
+/* Our menu, an array of GtkItemFactoryEntry structures that defines each menu item */
static GtkItemFactoryEntry menu_items[] = {
- { "/_File", NULL, NULL, 0, "<Branch>" },
- { "/File/_New", "<control>N", print_hello, 0, NULL },
- { "/File/_Open", "<control>O", print_hello, 0, NULL },
- { "/File/_Save", "<control>S", print_hello, 0, NULL },
- { "/File/Save _As", NULL, NULL, 0, NULL },
- { "/File/sep1", NULL, NULL, 0, "<Separator>" },
- { "/File/Quit", "<control>Q", gtk_main_quit, 0, NULL },
- { "/_Options", NULL, NULL, 0, "<Branch>" },
- { "/Options/Test", NULL, NULL, 0, NULL },
- { "/_Help", NULL, NULL, 0, "<LastBranch>" },
- { "/_Help/About", NULL, NULL, 0, NULL },
+ { "/_File", NULL, NULL, 0, "<Branch>" },
+ { "/File/_New", "<control>N", print_hello, 0, "<Item>" },
+ { "/File/_Open", "<control>O", print_hello, 0, "<Item>" },
+ { "/File/_Save", "<control>S", print_hello, 0, "<Item>" },
+ { "/File/Save _As", NULL, NULL, 0, "<Item>" },
+ { "/File/sep1", NULL, NULL, 0, "<Separator>" },
+ { "/File/Quit", "<control>Q", gtk_main_quit, 0, "<Item>" },
+ { "/_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>" },
};
+static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
-void get_main_menu( GtkWidget *window,
- GtkWidget **menubar )
+/* Returns a menubar widget made from the above menu */
+GtkWidget *get_menubar_menu( GtkWidget *window)
{
GtkItemFactory *item_factory;
GtkAccelGroup *accel_group;
- gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
+ /* Make an accelerator group (shortcut keys) */
accel_group = gtk_accel_group_new ();
- /* This function initializes the item factory.
- Param 1: The type of menu - can be GTK_TYPE_MENU_BAR, GTK_TYPE_MENU,
- or GTK_TYPE_OPTION_MENU.
- Param 2: The path of the menu.
- Param 3: A pointer to a gtk_accel_group. The item factory sets up
- the accelerator table while generating menus.
- */
-
- item_factory = gtk_item_factory_new (GTK_TYPE_MENU_BAR, "<main>",
- accel_group);
+ /* Make an ItemFactory (that makes a menubar) */
+ item_factory = gtk_item_factory_new (GTK_TYPE_MENU_BAR, "<main>",
+ accel_group);
/* This function generates the menu items. Pass the item factory,
the number of items in the array, the array itself, and any
@@ -75,39 +70,106 @@ void get_main_menu( GtkWidget *window,
/* Attach the new accelerator group to the window. */
gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
- if (menubar)
- /* Finally, return the actual menu bar created by the item factory. */
- *menubar = gtk_item_factory_get_widget (item_factory, "<main>");
+ /* Finally, return the actual menu bar created by the item factory. */
+ return gtk_item_factory_get_widget (item_factory, "<main>");
}
+/* Popup the menu when the popup button is pressed */
+static gint popup_cb(GtkWidget *widget, GdkEvent *event, GtkWidget *menu)
+{
+ GdkEventButton *bevent = (GdkEventButton *)event;
+
+ /* Only take button presses */
+ if(event->type != GDK_BUTTON_PRESS)
+ return FALSE;
+
+ /* Show the menu */
+ gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
+ NULL, NULL, bevent->button, bevent->time);
+
+ return TRUE;
+}
+
+/* Same as with get_menubar_menu() but just return a button with a signal to
+ call a popup menu */
+GtkWidget *get_popup_menu(void)
+{
+ 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);
+ menu = gtk_item_factory_get_widget(item_factory, "<main>");
+
+ /* Make a button to activate the popup menu */
+ button = gtk_button_new_with_label("Popup");
+ /* Make the menu popup when clicked */
+ g_signal_connect(G_OBJECT(button),
+ "event",
+ G_CALLBACK(popup_cb),
+ (gpointer) menu);
+
+ return button;
+}
+
+/* Same again but return an option menu */
+GtkWidget *get_option_menu(void)
+{
+ 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);
+ option_menu = gtk_item_factory_get_widget(item_factory, "<main>");
+
+ return option_menu;
+}
+
+/* You have to start somewhere */
int main( int argc,
char *argv[] )
{
GtkWidget *window;
GtkWidget *main_vbox;
- GtkWidget *menubar;
-
+ GtkWidget *menubar, *option_menu, *popup_button;
+
+ /* Initialize GTK */
gtk_init (&argc, &argv);
-
+
+ /* Make a window */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
- 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);
-
+ 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 */
main_vbox = gtk_vbox_new (FALSE, 1);
gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 1);
gtk_container_add (GTK_CONTAINER (window), main_vbox);
- gtk_widget_show (main_vbox);
+
+ /* 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);
+ popup_button = get_popup_menu();
+ option_menu = get_option_menu();
- get_main_menu (window, &menubar);
+ /* Pack it all together */
gtk_box_pack_start (GTK_BOX (main_vbox), menubar, FALSE, TRUE, 0);
- gtk_widget_show (menubar);
-
- gtk_widget_show (window);
+ 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);
- gtk_main ();
+ /* Show the widgets */
+ gtk_widget_show_all (window);
- return 0;
+ /* Finished! */
+ gtk_main ();
+
+ return(0);
}