]> Pileus Git - ~andy/gtk/blob - examples/menu/itemfactory.c
threads example from Erik Mouw. New question on GtkLabel background
[~andy/gtk] / examples / menu / itemfactory.c
1 /* example-start menu itemfactory.c */
2
3 #include <gtk/gtk.h>
4 #include <strings.h>
5
6 /* Obligatory basic callback */
7 static void print_hello( GtkWidget *w,
8                          gpointer   data )
9 {
10   g_message ("Hello, World!\n");
11 }
12
13 /* This is the GtkItemFactoryEntry structure used to generate new menus.
14    Item 1: The menu path. The letter after the underscore indicates an
15            accelerator key once the menu is open.
16    Item 2: The accelerator key for the entry
17    Item 3: The callback function.
18    Item 4: The callback action.  This changes the parameters with
19            which the function is called.  The default is 0.
20    Item 5: The item type, used to define what kind of an item it is.
21            Here are the possible values:
22
23            NULL               -> "<Item>"
24            ""                 -> "<Item>"
25            "<Title>"          -> create a title item
26            "<Item>"           -> create a simple item
27            "<CheckItem>"      -> create a check item
28            "<ToggleItem>"     -> create a toggle item
29            "<RadioItem>"      -> create a radio item
30            <path>             -> path of a radio item to link against
31            "<Separator>"      -> create a separator
32            "<Branch>"         -> create an item to hold sub items (optional)
33            "<LastBranch>"     -> create a right justified branch 
34 */
35
36 static GtkItemFactoryEntry menu_items[] = {
37   { "/_File",         NULL,         NULL, 0, "<Branch>" },
38   { "/File/_New",     "<control>N", print_hello, 0, NULL },
39   { "/File/_Open",    "<control>O", print_hello, 0, NULL },
40   { "/File/_Save",    "<control>S", print_hello, 0, NULL },
41   { "/File/Save _As", NULL,         NULL, 0, NULL },
42   { "/File/sep1",     NULL,         NULL, 0, "<Separator>" },
43   { "/File/Quit",     "<control>Q", gtk_main_quit, 0, NULL },
44   { "/_Options",      NULL,         NULL, 0, "<Branch>" },
45   { "/Options/Test",  NULL,         NULL, 0, NULL },
46   { "/_Help",         NULL,         NULL, 0, "<LastBranch>" },
47   { "/_Help/About",   NULL,         NULL, 0, NULL },
48 };
49
50
51 void get_main_menu( GtkWidget  *window,
52                     GtkWidget **menubar )
53 {
54   GtkItemFactory *item_factory;
55   GtkAccelGroup *accel_group;
56   gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
57
58   accel_group = gtk_accel_group_new ();
59
60   /* This function initializes the item factory.
61      Param 1: The type of menu - can be GTK_TYPE_MENU_BAR, GTK_TYPE_MENU,
62               or GTK_TYPE_OPTION_MENU.
63      Param 2: The path of the menu.
64      Param 3: A pointer to a gtk_accel_group.  The item factory sets up
65               the accelerator table while generating menus.
66   */
67
68   item_factory = gtk_item_factory_new (GTK_TYPE_MENU_BAR, "<main>", 
69                                        accel_group);
70
71   /* This function generates the menu items. Pass the item factory,
72      the number of items in the array, the array itself, and any
73      callback data for the the menu items. */
74   gtk_item_factory_create_items (item_factory, nmenu_items, menu_items, NULL);
75
76   /* Attach the new accelerator group to the window. */
77   gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
78
79   if (menubar)
80     /* Finally, return the actual menu bar created by the item factory. */ 
81     *menubar = gtk_item_factory_get_widget (item_factory, "<main>");
82 }
83
84 int main( int argc,
85           char *argv[] )
86 {
87   GtkWidget *window;
88   GtkWidget *main_vbox;
89   GtkWidget *menubar;
90   
91   gtk_init (&argc, &argv);
92   
93   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
94   gtk_signal_connect (GTK_OBJECT (window), "destroy", 
95                       GTK_SIGNAL_FUNC (gtk_main_quit), 
96                       "WM destroy");
97   gtk_window_set_title (GTK_WINDOW(window), "Item Factory");
98   gtk_widget_set_usize (GTK_WIDGET(window), 300, 200);
99   
100   main_vbox = gtk_vbox_new (FALSE, 1);
101   gtk_container_border_width (GTK_CONTAINER (main_vbox), 1);
102   gtk_container_add (GTK_CONTAINER (window), main_vbox);
103   gtk_widget_show (main_vbox);
104   
105   get_main_menu (window, &menubar);
106   gtk_box_pack_start (GTK_BOX (main_vbox), menubar, FALSE, TRUE, 0);
107   gtk_widget_show (menubar);
108   
109   gtk_widget_show (window);
110   gtk_main ();
111   
112   return(0);
113 }
114 /* example-end */