~canonical-dx-team/ubuntu/maverick/gtk+2.0/menuproxy

« back to all changes in this revision

Viewing changes to examples/menu/itemfactory.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2007-05-04 12:24:25 UTC
  • mfrom: (1.1.21 upstream)
  • Revision ID: james.westby@ubuntu.com-20070504122425-0m8midgzrp40y8w2
Tags: 2.10.12-1ubuntu1
* Sync with Debian
* New upstream version:
  Fixed bugs:
  - 379414 file chooser warnings when changing path in the entry
  - 418585 GtkFileChooserDefault sizing code is not DPI independent
  - 419568 Crash in search if start with special letter
  - 435062 build dies with icon cache validation
  - 379399 Segfault to call gtk_print_operation_run twice.
  - 387889 cups backend has problems when there are too many printers
  - 418531 invalid read to gtkicontheme.c gtk_icon_theme_lookup_icon...
  - 423916 crash in color scheme code
  - 424042 Segmentation fault while quickly pressing Alt+arrows
  - 415260 Protect against negative indices when setting values in G...
  - 419171 XGetVisualInfo() may not set nxvisuals
  - 128852 Gdk cursors don't look good on win32
  - 344657 Ctrl-H doesn't toggle "Show Hidden Files" setting
  - 345345 PrintOperation::paginate is not emitted for class handler
  - 347567 GtkPrintOperation::end-print is not emitted if it's cance...
  - 369112 gtk_ui_manager_add_ui should accept unnamed separator
  - 392015 Selected menu item invisible on Windows Vista
  - 399253 MS-Windows Theme Bottom Tab placement rendering glitches
  - 399425 gtk_input_dialog_fill_axes() adds child to gtkscrolledwin...
  - 403251 [patch] little memory leak in GtkPrintJob
  - 403267 [patch] memory leak in GtkPageSetupUnixDialog
  - 403470 MS-Windows Theme tab placement other than on top leaks a ...
  - 404506 Windows system fonts that have multi-byte font names cann...
  - 405089 Incorrect window placement for GtkEventBox private window
  - 405515 Minor leak in gtkfilesystemmodel.c
  - 405539 gdk_pixbuf_save() for PNG saver can return FALSE without ...
  - 415681 gdk_window_clear_area includes an extra line and column o...
  - 418219 GtkRecentChooser should apply filter before sorting and c...
  - 418403 Scroll to printer after selecting it from settings
  - 421985 _gtk_print_operation_platform_backend_launch_preview
  - 421990 gtk_print_job_get_surface
  - 421993 gtk_print_operation_init
  - 423064 Conditional jump or move depends on uninitialised value(s...
  - 423722 Fix printing header in gtk-demo
  - 424168 gtk_print_operation_run on async preview
  - 425655 Don't install gtk+-unix-print-2.0.pc on non-UNIX platforms
  - 425786 GDK segfaults if XineramaQueryScreens fails
  - 428665 Lpr Backend gets stuck in infinite loop during gtk_enumer...
  - 429902 GtkPrintOperation leaks cairo contextes
  - 431997 First delay of GdkPixbufAnimationIter is wrong
  - 433242 Inconsistent scroll arrow position calculations
  - 433972 Placing gtk.Expander inside a gtk.TextView() changes gtk....
  - 434261 _gtk_toolbar_elide_underscores incorrectly handles some s...
  - 383354 ctrl-L should make 'Location' entry disappear
  - 418673 gtk_recent_manager_add_item
  - 429732 gtk_accel_group_finalize accesses invalid memory
  - 435028 WM_CLIENT_LEADER is wrong on the leader_window
  - 431067 Background of the header window is not updated
  - 338843 add recent files support inside the ui manager
  - 148535 add drop shadow to menus, tooltips, etc. under Windows XP
* debian/control.in:
  - Conflicts on ubuntulooks (<= 0.9.11-1)
* debian/patches/15_default-fallback-icon-theme.patch:
  - patch from Debian, fallback on gnome icon theme

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#include <gtk/gtk.h>
 
3
 
 
4
/* Obligatory basic callback */
 
5
static void print_hello( GtkWidget *w,
 
6
                         gpointer   data )
 
7
{
 
8
  g_message ("Hello, World!\n");
 
9
}
 
10
 
 
11
/* For the check button */
 
12
static void print_toggle( gpointer   callback_data,
 
13
                          guint      callback_action,
 
14
                          GtkWidget *menu_item )
 
15
{
 
16
   g_message ("Check button state - %d\n",
 
17
              GTK_CHECK_MENU_ITEM (menu_item)->active);
 
18
}
 
19
 
 
20
/* For the radio buttons */
 
21
static void print_selected( gpointer   callback_data,
 
22
                            guint      callback_action,
 
23
                            GtkWidget *menu_item )
 
24
{
 
25
   if(GTK_CHECK_MENU_ITEM(menu_item)->active)
 
26
     g_message ("Radio button %d selected\n", callback_action);
 
27
}
 
28
 
 
29
/* Our menu, an array of GtkItemFactoryEntry structures that defines each menu item */
 
30
static GtkItemFactoryEntry menu_items[] = {
 
31
  { "/_File",         NULL,         NULL,           0, "<Branch>" },
 
32
  { "/File/_New",     "<control>N", print_hello,    0, "<StockItem>", GTK_STOCK_NEW },
 
33
  { "/File/_Open",    "<control>O", print_hello,    0, "<StockItem>", GTK_STOCK_OPEN },
 
34
  { "/File/_Save",    "<control>S", print_hello,    0, "<StockItem>", GTK_STOCK_SAVE },
 
35
  { "/File/Save _As", NULL,         NULL,           0, "<Item>" },
 
36
  { "/File/sep1",     NULL,         NULL,           0, "<Separator>" },
 
37
  { "/File/_Quit",    "<CTRL>Q", gtk_main_quit, 0, "<StockItem>", GTK_STOCK_QUIT },
 
38
  { "/_Options",      NULL,         NULL,           0, "<Branch>" },
 
39
  { "/Options/tear",  NULL,         NULL,           0, "<Tearoff>" },
 
40
  { "/Options/Check", NULL,         print_toggle,   1, "<CheckItem>" },
 
41
  { "/Options/sep",   NULL,         NULL,           0, "<Separator>" },
 
42
  { "/Options/Rad1",  NULL,         print_selected, 1, "<RadioItem>" },
 
43
  { "/Options/Rad2",  NULL,         print_selected, 2, "/Options/Rad1" },
 
44
  { "/Options/Rad3",  NULL,         print_selected, 3, "/Options/Rad1" },
 
45
  { "/_Help",         NULL,         NULL,           0, "<LastBranch>" },
 
46
  { "/_Help/About",   NULL,         NULL,           0, "<Item>" },
 
47
};
 
48
 
 
49
static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
 
50
 
 
51
/* Returns a menubar widget made from the above menu */
 
52
static GtkWidget *get_menubar_menu( GtkWidget  *window )
 
53
{
 
54
  GtkItemFactory *item_factory;
 
55
  GtkAccelGroup *accel_group;
 
56
 
 
57
  /* Make an accelerator group (shortcut keys) */
 
58
  accel_group = gtk_accel_group_new ();
 
59
 
 
60
  /* Make an ItemFactory (that makes a menubar) */
 
61
  item_factory = gtk_item_factory_new (GTK_TYPE_MENU_BAR, "<main>",
 
62
                                       accel_group);
 
63
 
 
64
  /* This function generates the menu items. Pass the item factory,
 
65
     the number of items in the array, the array itself, and any
 
66
     callback data for the the menu items. */
 
67
  gtk_item_factory_create_items (item_factory, nmenu_items, menu_items, NULL);
 
68
 
 
69
  /* Attach the new accelerator group to the window. */
 
70
  gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
 
71
 
 
72
  /* Finally, return the actual menu bar created by the item factory. */
 
73
  return gtk_item_factory_get_widget (item_factory, "<main>");
 
74
}
 
75
 
 
76
/* Popup the menu when the popup button is pressed */
 
77
static gboolean popup_cb( GtkWidget *widget,
 
78
                          GdkEvent *event,
 
79
                          GtkWidget *menu )
 
80
{
 
81
   GdkEventButton *bevent = (GdkEventButton *)event;
 
82
  
 
83
   /* Only take button presses */
 
84
   if (event->type != GDK_BUTTON_PRESS)
 
85
     return FALSE;
 
86
  
 
87
   /* Show the menu */
 
88
   gtk_menu_popup (GTK_MENU(menu), NULL, NULL,
 
89
                   NULL, NULL, bevent->button, bevent->time);
 
90
  
 
91
   return TRUE;
 
92
}
 
93
 
 
94
/* Same as with get_menubar_menu() but just return a button with a signal to
 
95
   call a popup menu */
 
96
GtkWidget *get_popup_menu( void )
 
97
{
 
98
   GtkItemFactory *item_factory;
 
99
   GtkWidget *button, *menu;
 
100
  
 
101
   /* Same as before but don't bother with the accelerators */
 
102
   item_factory = gtk_item_factory_new (GTK_TYPE_MENU, "<main>",
 
103
                                        NULL);
 
104
   gtk_item_factory_create_items (item_factory, nmenu_items, menu_items, NULL);
 
105
   menu = gtk_item_factory_get_widget (item_factory, "<main>");
 
106
  
 
107
   /* Make a button to activate the popup menu */
 
108
   button = gtk_button_new_with_label ("Popup");
 
109
   /* Make the menu popup when clicked */
 
110
   g_signal_connect (G_OBJECT(button),
 
111
                     "event",
 
112
                     G_CALLBACK(popup_cb),
 
113
                     (gpointer) menu);
 
114
 
 
115
   return button;
 
116
}
 
117
 
 
118
/* Same again but return an option menu */
 
119
GtkWidget *get_option_menu( void )
 
120
{
 
121
   GtkItemFactory *item_factory;
 
122
   GtkWidget *option_menu;
 
123
  
 
124
   /* Same again, not bothering with the accelerators */
 
125
   item_factory = gtk_item_factory_new (GTK_TYPE_OPTION_MENU, "<main>",
 
126
                                        NULL);
 
127
   gtk_item_factory_create_items (item_factory, nmenu_items, menu_items, NULL);
 
128
   option_menu = gtk_item_factory_get_widget (item_factory, "<main>");
 
129
 
 
130
   return option_menu;
 
131
}
 
132
 
 
133
/* You have to start somewhere */
 
134
int main( int argc,
 
135
          char *argv[] )
 
136
{
 
137
  GtkWidget *window;
 
138
  GtkWidget *main_vbox;
 
139
  GtkWidget *menubar, *option_menu, *popup_button;
 
140
 
 
141
  /* Initialize GTK */
 
142
  gtk_init (&argc, &argv);
 
143
 
 
144
  /* Make a window */
 
145
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
 
146
  g_signal_connect (G_OBJECT (window), "destroy",
 
147
                    G_CALLBACK (gtk_main_quit),
 
148
                    NULL);
 
149
  gtk_window_set_title (GTK_WINDOW(window), "Item Factory");
 
150
  gtk_widget_set_size_request (GTK_WIDGET(window), 300, 200);
 
151
 
 
152
  /* Make a vbox to put the three menus in */
 
153
  main_vbox = gtk_vbox_new (FALSE, 1);
 
154
  gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 1);
 
155
  gtk_container_add (GTK_CONTAINER (window), main_vbox);
 
156
 
 
157
  /* Get the three types of menu */
 
158
  /* Note: all three menus are separately created, so they are not the
 
159
     same menu */
 
160
  menubar = get_menubar_menu (window);
 
161
  popup_button = get_popup_menu ();
 
162
  option_menu = get_option_menu ();
 
163
  
 
164
  /* Pack it all together */
 
165
  gtk_box_pack_start (GTK_BOX (main_vbox), menubar, FALSE, TRUE, 0);
 
166
  gtk_box_pack_end (GTK_BOX (main_vbox), popup_button, FALSE, TRUE, 0);
 
167
  gtk_box_pack_end (GTK_BOX (main_vbox), option_menu, FALSE, TRUE, 0);
 
168
 
 
169
  /* Show the widgets */
 
170
  gtk_widget_show_all (window);
 
171
  
 
172
  /* Finished! */
 
173
  gtk_main ();
 
174
 
 
175
  return 0;
 
176
}