]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: menu.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
96fd301f | 5 | // Id: $Id$ |
a81258be | 6 | // Copyright: (c) 1998 Robert Roebling |
96fd301f | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
c801d85f KB |
10 | #ifdef __GNUG__ |
11 | #pragma implementation "menu.h" | |
6ca41e57 | 12 | #pragma implementation "menuitem.h" |
c801d85f KB |
13 | #endif |
14 | ||
96fd301f | 15 | #include "wx/log.h" |
30dea054 | 16 | #include "wx/intl.h" |
06cfab17 | 17 | #include "wx/app.h" |
37d403aa | 18 | #include "wx/bitmap.h" |
3dfac970 | 19 | #include "wx/menu.h" |
c801d85f | 20 | |
974e8d94 VZ |
21 | #if wxUSE_ACCEL |
22 | #include "wx/accel.h" | |
23 | #endif // wxUSE_ACCEL | |
24 | ||
9e691f46 VZ |
25 | #include "wx/gtk/private.h" |
26 | ||
cc47eed9 | 27 | #include <gdk/gdkkeysyms.h> |
9e691f46 VZ |
28 | |
29 | // FIXME: is this right? somehow I don't think so (VZ) | |
30 | #ifdef __WXGTK20__ | |
31 | #include <glib-object.h> | |
32 | ||
33 | #define gtk_accel_group_attach(g, o) _gtk_accel_group_attach((g), (o)) | |
34 | #define gtk_accel_group_detach(g, o) _gtk_accel_group_detach((g), (o)) | |
35 | #define gtk_menu_ensure_uline_accel_group(m) gtk_menu_get_accel_group(m) | |
36 | ||
37 | #define ACCEL_OBJECT GObject | |
38 | #define ACCEL_OBJECTS(a) (a)->acceleratables | |
39 | #define ACCEL_OBJ_CAST(obj) G_OBJECT(obj) | |
40 | #else // GTK+ 1.x | |
41 | #define ACCEL_OBJECT GtkObject | |
42 | #define ACCEL_OBJECTS(a) (a)->attach_objects | |
43 | #define ACCEL_OBJ_CAST(obj) GTK_OBJECT(obj) | |
44 | #endif | |
83624f79 | 45 | |
acfd422a RR |
46 | //----------------------------------------------------------------------------- |
47 | // idle system | |
48 | //----------------------------------------------------------------------------- | |
49 | ||
50 | extern void wxapp_install_idle_handler(); | |
51 | extern bool g_isIdle; | |
52 | ||
9e691f46 VZ |
53 | #if GTK_CHECK_VERSION(1, 2, 0) && wxUSE_ACCEL |
54 | static wxString GetHotKey( const wxMenuItem& item ); | |
717a57c2 VZ |
55 | #endif |
56 | ||
cc47eed9 RR |
57 | //----------------------------------------------------------------------------- |
58 | // substitute for missing GtkPixmapMenuItem | |
59 | //----------------------------------------------------------------------------- | |
37d403aa | 60 | |
9e691f46 VZ |
61 | // FIXME: I can't make this compile with GTK+ 2.0, disabling for now (VZ) |
62 | #ifndef __WXGTK20__ | |
63 | #define USE_MENU_BITMAPS | |
64 | #endif | |
65 | ||
66 | #ifdef USE_MENU_BITMAPS | |
67 | ||
cc47eed9 RR |
68 | #define GTK_TYPE_PIXMAP_MENU_ITEM (gtk_pixmap_menu_item_get_type ()) |
69 | #define GTK_PIXMAP_MENU_ITEM(obj) (GTK_CHECK_CAST ((obj), GTK_TYPE_PIXMAP_MENU_ITEM, GtkPixmapMenuItem)) | |
37d403aa | 70 | #define GTK_PIXMAP_MENU_ITEM_CLASS(klass) (GTK_CHECK_CLASS_CAST ((klass), GTK_TYPE_PIXMAP_MENU_ITEM, GtkPixmapMenuItemClass)) |
cc47eed9 | 71 | #define GTK_IS_PIXMAP_MENU_ITEM(obj) (GTK_CHECK_TYPE ((obj), GTK_TYPE_PIXMAP_MENU_ITEM)) |
37d403aa JS |
72 | #define GTK_IS_PIXMAP_MENU_ITEM_CLASS(klass) (GTK_CHECK_CLASS_TYPE ((klass), GTK_TYPE_PIXMAP_MENU_ITEM)) |
73 | //#define GTK_PIXMAP_MENU_ITEM_GET_CLASS(obj) (GTK_CHECK_GET_CLASS ((obj), GTK_TYPE_PIXMAP_MENU_ITEM)) | |
cc47eed9 | 74 | #define GTK_PIXMAP_MENU_ITEM_GET_CLASS(obj) (GTK_PIXMAP_MENU_ITEM_CLASS( GTK_OBJECT_GET_CLASS(obj))) |
37d403aa JS |
75 | |
76 | #ifndef GTK_MENU_ITEM_GET_CLASS | |
77 | #define GTK_MENU_ITEM_GET_CLASS(obj) (GTK_MENU_ITEM_CLASS( GTK_OBJECT_GET_CLASS(obj))) | |
78 | #endif | |
79 | ||
80 | typedef struct _GtkPixmapMenuItem GtkPixmapMenuItem; | |
81 | typedef struct _GtkPixmapMenuItemClass GtkPixmapMenuItemClass; | |
82 | ||
83 | struct _GtkPixmapMenuItem | |
84 | { | |
cc47eed9 | 85 | GtkMenuItem menu_item; |
37d403aa | 86 | |
cc47eed9 | 87 | GtkWidget *pixmap; |
37d403aa JS |
88 | }; |
89 | ||
90 | struct _GtkPixmapMenuItemClass | |
91 | { | |
cc47eed9 | 92 | GtkMenuItemClass parent_class; |
37d403aa | 93 | |
cc47eed9 RR |
94 | guint orig_toggle_size; |
95 | guint have_pixmap_count; | |
37d403aa JS |
96 | }; |
97 | ||
98 | ||
8f262dc5 | 99 | GtkType gtk_pixmap_menu_item_get_type (void); |
cc47eed9 RR |
100 | GtkWidget* gtk_pixmap_menu_item_new (void); |
101 | void gtk_pixmap_menu_item_set_pixmap (GtkPixmapMenuItem *menu_item, | |
8f262dc5 | 102 | GtkWidget *pixmap); |
37d403aa | 103 | |
9e691f46 VZ |
104 | #endif // USE_MENU_BITMAPS |
105 | ||
f6bcfd97 BP |
106 | //----------------------------------------------------------------------------- |
107 | // idle system | |
108 | //----------------------------------------------------------------------------- | |
109 | ||
110 | static wxString wxReplaceUnderscore( const wxString& title ) | |
111 | { | |
112 | const wxChar *pc; | |
2368dcda | 113 | |
f6bcfd97 BP |
114 | /* GTK 1.2 wants to have "_" instead of "&" for accelerators */ |
115 | wxString str; | |
116 | for ( pc = title; *pc != wxT('\0'); pc++ ) | |
117 | { | |
118 | if (*pc == wxT('&')) | |
119 | { | |
4e9cbd33 | 120 | #if GTK_CHECK_VERSION(1, 2, 0) |
f6bcfd97 | 121 | str << wxT('_'); |
4e9cbd33 | 122 | #endif |
f6bcfd97 | 123 | } |
4e9cbd33 VS |
124 | #if GTK_CHECK_VERSION(2, 0, 0) |
125 | else if (*pc == wxT('/')) | |
126 | { | |
127 | str << wxT("\\/"); | |
128 | } | |
129 | else if (*pc == wxT('\\')) | |
130 | { | |
131 | str << wxT("\\\\"); | |
132 | } | |
133 | #elif GTK_CHECK_VERSION(1, 2, 0) | |
f6bcfd97 BP |
134 | else if (*pc == wxT('/')) |
135 | { | |
136 | str << wxT('\\'); | |
f6bcfd97 | 137 | } |
4e9cbd33 | 138 | #endif |
f6bcfd97 BP |
139 | else |
140 | { | |
141 | #if __WXGTK12__ | |
142 | if ( *pc == wxT('_') ) | |
143 | { | |
144 | // underscores must be doubled to prevent them from being | |
145 | // interpreted as accelerator character prefix by GTK | |
146 | str << *pc; | |
147 | } | |
148 | #endif // GTK+ 1.2 | |
149 | ||
150 | str << *pc; | |
151 | } | |
152 | } | |
153 | return str; | |
154 | } | |
155 | ||
c801d85f KB |
156 | //----------------------------------------------------------------------------- |
157 | // wxMenuBar | |
158 | //----------------------------------------------------------------------------- | |
159 | ||
160 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow) | |
161 | ||
3502e687 RR |
162 | wxMenuBar::wxMenuBar( long style ) |
163 | { | |
1e133b7d | 164 | /* the parent window is known after wxFrame::SetMenu() */ |
23280650 | 165 | m_needParent = FALSE; |
ae53c98c | 166 | m_style = style; |
9c884972 | 167 | m_invokingWindow = (wxWindow*) NULL; |
23280650 | 168 | |
4dcaf11a | 169 | if (!PreCreation( (wxWindow*) NULL, wxDefaultPosition, wxDefaultSize ) || |
223d09f6 | 170 | !CreateBase( (wxWindow*) NULL, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, wxT("menubar") )) |
4dcaf11a | 171 | { |
223d09f6 | 172 | wxFAIL_MSG( wxT("wxMenuBar creation failed") ); |
455fadaa | 173 | return; |
4dcaf11a | 174 | } |
3502e687 RR |
175 | |
176 | m_menus.DeleteContents( TRUE ); | |
177 | ||
1e133b7d | 178 | /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */ |
9e691f46 | 179 | #if GTK_CHECK_VERSION(1, 2, 1) |
1e133b7d RR |
180 | m_accel = gtk_accel_group_new(); |
181 | m_factory = gtk_item_factory_new( GTK_TYPE_MENU_BAR, "<main>", m_accel ); | |
182 | m_menubar = gtk_item_factory_get_widget( m_factory, "<main>" ); | |
23280650 | 183 | #else |
3502e687 | 184 | m_menubar = gtk_menu_bar_new(); |
1e133b7d | 185 | #endif |
3502e687 RR |
186 | |
187 | if (style & wxMB_DOCKABLE) | |
188 | { | |
189 | m_widget = gtk_handle_box_new(); | |
c626a8b7 VZ |
190 | gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_menubar) ); |
191 | gtk_widget_show( GTK_WIDGET(m_menubar) ); | |
3502e687 RR |
192 | } |
193 | else | |
194 | { | |
195 | m_widget = GTK_WIDGET(m_menubar); | |
196 | } | |
197 | ||
198 | PostCreation(); | |
c4608a8a | 199 | |
db434467 | 200 | ApplyWidgetStyle(); |
3502e687 RR |
201 | } |
202 | ||
96fd301f | 203 | wxMenuBar::wxMenuBar() |
c801d85f | 204 | { |
1e133b7d RR |
205 | /* the parent window is known after wxFrame::SetMenu() */ |
206 | m_needParent = FALSE; | |
ae53c98c | 207 | m_style = 0; |
9c884972 | 208 | m_invokingWindow = (wxWindow*) NULL; |
23280650 | 209 | |
4dcaf11a | 210 | if (!PreCreation( (wxWindow*) NULL, wxDefaultPosition, wxDefaultSize ) || |
223d09f6 | 211 | !CreateBase( (wxWindow*) NULL, -1, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, wxT("menubar") )) |
4dcaf11a | 212 | { |
223d09f6 | 213 | wxFAIL_MSG( wxT("wxMenuBar creation failed") ); |
455fadaa | 214 | return; |
4dcaf11a | 215 | } |
974e8d94 | 216 | |
83624f79 | 217 | m_menus.DeleteContents( TRUE ); |
96fd301f | 218 | |
1e133b7d | 219 | /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */ |
9e691f46 | 220 | #if GTK_CHECK_VERSION(1, 2, 1) |
1e133b7d RR |
221 | m_accel = gtk_accel_group_new(); |
222 | m_factory = gtk_item_factory_new( GTK_TYPE_MENU_BAR, "<main>", m_accel ); | |
223 | m_menubar = gtk_item_factory_get_widget( m_factory, "<main>" ); | |
23280650 | 224 | #else |
83624f79 | 225 | m_menubar = gtk_menu_bar_new(); |
1e133b7d | 226 | #endif |
8bbe427f | 227 | |
828f655f | 228 | m_widget = GTK_WIDGET(m_menubar); |
96fd301f | 229 | |
83624f79 | 230 | PostCreation(); |
c4608a8a | 231 | |
db434467 | 232 | ApplyWidgetStyle(); |
6de97a3b | 233 | } |
c801d85f | 234 | |
1e133b7d RR |
235 | wxMenuBar::~wxMenuBar() |
236 | { | |
d1b15f03 | 237 | // gtk_object_unref( GTK_OBJECT(m_factory) ); why not ? |
1e133b7d RR |
238 | } |
239 | ||
5bd9e519 RR |
240 | static void wxMenubarUnsetInvokingWindow( wxMenu *menu, wxWindow *win ) |
241 | { | |
242 | menu->SetInvokingWindow( (wxWindow*) NULL ); | |
243 | ||
5bd9e519 | 244 | wxWindow *top_frame = win; |
8487f887 | 245 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) |
bd77da97 | 246 | top_frame = top_frame->GetParent(); |
5bd9e519 | 247 | |
fab591c5 | 248 | /* support for native hot keys */ |
9e691f46 | 249 | gtk_accel_group_detach( menu->m_accel, ACCEL_OBJ_CAST(top_frame->m_widget) ); |
5bd9e519 | 250 | |
1987af7e | 251 | wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst(); |
5bd9e519 RR |
252 | while (node) |
253 | { | |
1987af7e | 254 | wxMenuItem *menuitem = node->GetData(); |
5bd9e519 RR |
255 | if (menuitem->IsSubMenu()) |
256 | wxMenubarUnsetInvokingWindow( menuitem->GetSubMenu(), win ); | |
1987af7e | 257 | node = node->GetNext(); |
5bd9e519 RR |
258 | } |
259 | } | |
260 | ||
261 | static void wxMenubarSetInvokingWindow( wxMenu *menu, wxWindow *win ) | |
262 | { | |
263 | menu->SetInvokingWindow( win ); | |
264 | ||
9e691f46 | 265 | #if GTK_CHECK_VERSION(1, 2, 1) |
5bd9e519 | 266 | wxWindow *top_frame = win; |
8487f887 | 267 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) |
bd77da97 | 268 | top_frame = top_frame->GetParent(); |
5bd9e519 RR |
269 | |
270 | /* support for native hot keys */ | |
9e691f46 VZ |
271 | ACCEL_OBJECT *obj = ACCEL_OBJ_CAST(top_frame->m_widget); |
272 | if ( !g_slist_find( ACCEL_OBJECTS(menu->m_accel), obj ) ) | |
b4da05a6 | 273 | gtk_accel_group_attach( menu->m_accel, obj ); |
9e691f46 | 274 | #endif // GTK+ 1.2.1+ |
5bd9e519 | 275 | |
1987af7e | 276 | wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst(); |
5bd9e519 RR |
277 | while (node) |
278 | { | |
1987af7e | 279 | wxMenuItem *menuitem = node->GetData(); |
5bd9e519 RR |
280 | if (menuitem->IsSubMenu()) |
281 | wxMenubarSetInvokingWindow( menuitem->GetSubMenu(), win ); | |
1987af7e | 282 | node = node->GetNext(); |
5bd9e519 RR |
283 | } |
284 | } | |
285 | ||
286 | void wxMenuBar::SetInvokingWindow( wxWindow *win ) | |
287 | { | |
9c884972 | 288 | m_invokingWindow = win; |
9e691f46 | 289 | #if GTK_CHECK_VERSION(1, 2, 1) |
5bd9e519 | 290 | wxWindow *top_frame = win; |
8487f887 | 291 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) |
bd77da97 | 292 | top_frame = top_frame->GetParent(); |
5bd9e519 RR |
293 | |
294 | /* support for native key accelerators indicated by underscroes */ | |
9e691f46 VZ |
295 | ACCEL_OBJECT *obj = ACCEL_OBJ_CAST(top_frame->m_widget); |
296 | if ( !g_slist_find( ACCEL_OBJECTS(m_accel), obj ) ) | |
b4da05a6 | 297 | gtk_accel_group_attach( m_accel, obj ); |
9e691f46 | 298 | #endif // GTK+ 1.2.1+ |
5bd9e519 | 299 | |
1987af7e | 300 | wxMenuList::Node *node = m_menus.GetFirst(); |
5bd9e519 RR |
301 | while (node) |
302 | { | |
1987af7e | 303 | wxMenu *menu = node->GetData(); |
5bd9e519 | 304 | wxMenubarSetInvokingWindow( menu, win ); |
1987af7e | 305 | node = node->GetNext(); |
5bd9e519 RR |
306 | } |
307 | } | |
308 | ||
309 | void wxMenuBar::UnsetInvokingWindow( wxWindow *win ) | |
310 | { | |
9c884972 | 311 | m_invokingWindow = (wxWindow*) NULL; |
9e691f46 | 312 | #if GTK_CHECK_VERSION(1, 2, 1) |
5bd9e519 | 313 | wxWindow *top_frame = win; |
8487f887 | 314 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) |
bd77da97 | 315 | top_frame = top_frame->GetParent(); |
5bd9e519 RR |
316 | |
317 | /* support for native key accelerators indicated by underscroes */ | |
9e691f46 VZ |
318 | gtk_accel_group_detach( m_accel, ACCEL_OBJ_CAST(top_frame->m_widget) ); |
319 | #endif // GTK+ 1.2.1+ | |
5bd9e519 | 320 | |
1987af7e | 321 | wxMenuList::Node *node = m_menus.GetFirst(); |
5bd9e519 RR |
322 | while (node) |
323 | { | |
1987af7e | 324 | wxMenu *menu = node->GetData(); |
5bd9e519 | 325 | wxMenubarUnsetInvokingWindow( menu, win ); |
1987af7e | 326 | node = node->GetNext(); |
5bd9e519 RR |
327 | } |
328 | } | |
329 | ||
3dfac970 | 330 | bool wxMenuBar::Append( wxMenu *menu, const wxString &title ) |
c801d85f | 331 | { |
f03ec224 VZ |
332 | if ( !wxMenuBarBase::Append( menu, title ) ) |
333 | return FALSE; | |
334 | ||
335 | return GtkAppend(menu, title); | |
336 | } | |
23280650 | 337 | |
f03ec224 VZ |
338 | bool wxMenuBar::GtkAppend(wxMenu *menu, const wxString& title) |
339 | { | |
f6bcfd97 | 340 | wxString str( wxReplaceUnderscore( title ) ); |
1e133b7d RR |
341 | |
342 | /* this doesn't have much effect right now */ | |
343 | menu->SetTitle( str ); | |
23280650 | 344 | |
1e133b7d | 345 | /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */ |
9e691f46 | 346 | #if GTK_CHECK_VERSION(1, 2, 1) |
1e133b7d | 347 | |
2b2edbed | 348 | wxString buf; |
223d09f6 | 349 | buf << wxT('/') << str.c_str(); |
c980c992 | 350 | |
fab591c5 RR |
351 | /* local buffer in multibyte form */ |
352 | char cbuf[400]; | |
353 | strcpy(cbuf, wxGTK_CONV(buf) ); | |
c980c992 | 354 | |
1e133b7d | 355 | GtkItemFactoryEntry entry; |
c980c992 | 356 | entry.path = (gchar *)cbuf; // const_cast |
1e133b7d RR |
357 | entry.accelerator = (gchar*) NULL; |
358 | entry.callback = (GtkItemFactoryCallback) NULL; | |
359 | entry.callback_action = 0; | |
90350682 | 360 | entry.item_type = (char *)"<Branch>"; |
2b2edbed | 361 | |
1e133b7d | 362 | gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */ |
1e133b7d | 363 | /* in order to get the pointer to the item we need the item text _without_ underscores */ |
223d09f6 | 364 | wxString tmp = wxT("<main>/"); |
f6bcfd97 | 365 | const wxChar *pc; |
223d09f6 | 366 | for ( pc = str; *pc != wxT('\0'); pc++ ) |
1e133b7d | 367 | { |
455fadaa VZ |
368 | // contrary to the common sense, we must throw out _all_ underscores, |
369 | // (i.e. "Hello__World" => "HelloWorld" and not "Hello_World" as we | |
974e8d94 | 370 | // might naively think). IMHO it's a bug in GTK+ (VZ) |
223d09f6 | 371 | while (*pc == wxT('_')) |
455fadaa | 372 | pc++; |
2b2edbed | 373 | tmp << *pc; |
034be888 | 374 | } |
fab591c5 | 375 | menu->m_owner = gtk_item_factory_get_item( m_factory, wxGTK_CONV( tmp ) ); |
1e133b7d | 376 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu ); |
1e133b7d | 377 | #else |
96fd301f | 378 | |
fab591c5 | 379 | menu->m_owner = gtk_menu_item_new_with_label( wxGTK_CONV( str ) ); |
2b1c162e RR |
380 | gtk_widget_show( menu->m_owner ); |
381 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu ); | |
96fd301f | 382 | |
2b1c162e | 383 | gtk_menu_bar_append( GTK_MENU_BAR(m_menubar), menu->m_owner ); |
23280650 | 384 | |
1e133b7d | 385 | #endif |
9c884972 RR |
386 | |
387 | // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables | |
388 | // adding menu later on. | |
389 | if (m_invokingWindow) | |
390 | wxMenubarSetInvokingWindow( menu, m_invokingWindow ); | |
3dfac970 VZ |
391 | |
392 | return TRUE; | |
393 | } | |
394 | ||
395 | bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) | |
396 | { | |
397 | if ( !wxMenuBarBase::Insert(pos, menu, title) ) | |
398 | return FALSE; | |
399 | ||
f03ec224 VZ |
400 | // GTK+ doesn't have a function to insert a menu using GtkItemFactory (as |
401 | // of version 1.2.6), so we first append the item and then change its | |
402 | // index | |
403 | if ( !GtkAppend(menu, title) ) | |
404 | return FALSE; | |
405 | ||
186baeb2 RR |
406 | if (pos+1 >= m_menus.GetCount()) |
407 | return TRUE; | |
408 | ||
f03ec224 VZ |
409 | GtkMenuShell *menu_shell = GTK_MENU_SHELL(m_factory->widget); |
410 | gpointer data = g_list_last(menu_shell->children)->data; | |
411 | menu_shell->children = g_list_remove(menu_shell->children, data); | |
412 | menu_shell->children = g_list_insert(menu_shell->children, data, pos); | |
413 | ||
414 | return TRUE; | |
3dfac970 VZ |
415 | } |
416 | ||
417 | wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) | |
418 | { | |
f03ec224 VZ |
419 | // remove the old item and insert a new one |
420 | wxMenu *menuOld = Remove(pos); | |
421 | if ( menuOld && !Insert(pos, menu, title) ) | |
422 | { | |
1d62a8b4 | 423 | return (wxMenu*) NULL; |
f03ec224 | 424 | } |
3dfac970 | 425 | |
f03ec224 VZ |
426 | // either Insert() succeeded or Remove() failed and menuOld is NULL |
427 | return menuOld; | |
3dfac970 VZ |
428 | } |
429 | ||
430 | wxMenu *wxMenuBar::Remove(size_t pos) | |
431 | { | |
f03ec224 VZ |
432 | wxMenu *menu = wxMenuBarBase::Remove(pos); |
433 | if ( !menu ) | |
1d62a8b4 | 434 | return (wxMenu*) NULL; |
f03ec224 | 435 | |
589c9fff | 436 | /* |
c4608a8a | 437 | GtkMenuShell *menu_shell = GTK_MENU_SHELL(m_factory->widget); |
589c9fff | 438 | |
1d62a8b4 RR |
439 | printf( "factory entries before %d\n", (int)g_slist_length(m_factory->items) ); |
440 | printf( "menu shell entries before %d\n", (int)g_list_length( menu_shell->children ) ); | |
589c9fff | 441 | */ |
c4608a8a | 442 | |
1d62a8b4 RR |
443 | // unparent calls unref() and that would delete the widget so we raise |
444 | // the ref count to 2 artificially before invoking unparent. | |
445 | gtk_widget_ref( menu->m_menu ); | |
446 | gtk_widget_unparent( menu->m_menu ); | |
c4608a8a | 447 | |
1d62a8b4 | 448 | gtk_widget_destroy( menu->m_owner ); |
c4608a8a | 449 | |
589c9fff | 450 | /* |
1d62a8b4 RR |
451 | printf( "factory entries after %d\n", (int)g_slist_length(m_factory->items) ); |
452 | printf( "menu shell entries after %d\n", (int)g_list_length( menu_shell->children ) ); | |
589c9fff | 453 | */ |
c4608a8a | 454 | |
1d62a8b4 | 455 | return menu; |
6de97a3b | 456 | } |
96fd301f | 457 | |
716b7364 | 458 | static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString ) |
c801d85f | 459 | { |
f6bcfd97 | 460 | if (wxMenuItem::GetLabelFromText(menu->GetTitle()) == wxMenuItem::GetLabelFromText(menuString)) |
83624f79 RR |
461 | { |
462 | int res = menu->FindItem( itemString ); | |
c626a8b7 VZ |
463 | if (res != wxNOT_FOUND) |
464 | return res; | |
83624f79 | 465 | } |
c626a8b7 | 466 | |
1987af7e | 467 | wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst(); |
83624f79 RR |
468 | while (node) |
469 | { | |
1987af7e | 470 | wxMenuItem *item = node->GetData(); |
83624f79 RR |
471 | if (item->IsSubMenu()) |
472 | return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString); | |
2b1c162e | 473 | |
1987af7e | 474 | node = node->GetNext(); |
83624f79 RR |
475 | } |
476 | ||
c626a8b7 VZ |
477 | return wxNOT_FOUND; |
478 | } | |
479 | ||
c801d85f KB |
480 | int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const |
481 | { | |
1987af7e | 482 | wxMenuList::Node *node = m_menus.GetFirst(); |
83624f79 RR |
483 | while (node) |
484 | { | |
1987af7e | 485 | wxMenu *menu = node->GetData(); |
83624f79 | 486 | int res = FindMenuItemRecursive( menu, menuString, itemString); |
1987af7e VZ |
487 | if (res != -1) |
488 | return res; | |
489 | node = node->GetNext(); | |
83624f79 | 490 | } |
1987af7e VZ |
491 | |
492 | return wxNOT_FOUND; | |
6de97a3b | 493 | } |
c801d85f | 494 | |
c626a8b7 | 495 | // Find a wxMenuItem using its id. Recurses down into sub-menus |
96fd301f | 496 | static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id) |
716b7364 | 497 | { |
717a57c2 | 498 | wxMenuItem* result = menu->FindChildItem(id); |
716b7364 | 499 | |
1987af7e | 500 | wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst(); |
c626a8b7 | 501 | while ( node && result == NULL ) |
83624f79 | 502 | { |
1987af7e | 503 | wxMenuItem *item = node->GetData(); |
83624f79 | 504 | if (item->IsSubMenu()) |
c626a8b7 | 505 | { |
83624f79 | 506 | result = FindMenuItemByIdRecursive( item->GetSubMenu(), id ); |
c626a8b7 | 507 | } |
1987af7e | 508 | node = node->GetNext(); |
83624f79 | 509 | } |
96fd301f | 510 | |
83624f79 | 511 | return result; |
6de97a3b | 512 | } |
716b7364 | 513 | |
3dfac970 | 514 | wxMenuItem* wxMenuBar::FindItem( int id, wxMenu **menuForItem ) const |
716b7364 | 515 | { |
83624f79 | 516 | wxMenuItem* result = 0; |
1987af7e | 517 | wxMenuList::Node *node = m_menus.GetFirst(); |
83624f79 RR |
518 | while (node && result == 0) |
519 | { | |
1987af7e | 520 | wxMenu *menu = node->GetData(); |
83624f79 | 521 | result = FindMenuItemByIdRecursive( menu, id ); |
1987af7e | 522 | node = node->GetNext(); |
83624f79 | 523 | } |
c626a8b7 | 524 | |
3dfac970 VZ |
525 | if ( menuForItem ) |
526 | { | |
527 | *menuForItem = result ? result->GetMenu() : (wxMenu *)NULL; | |
528 | } | |
c626a8b7 | 529 | |
3dfac970 | 530 | return result; |
bbe0af5b RR |
531 | } |
532 | ||
3dfac970 | 533 | void wxMenuBar::EnableTop( size_t pos, bool flag ) |
bbe0af5b | 534 | { |
3dfac970 | 535 | wxMenuList::Node *node = m_menus.Item( pos ); |
c626a8b7 | 536 | |
223d09f6 | 537 | wxCHECK_RET( node, wxT("menu not found") ); |
c626a8b7 | 538 | |
3dfac970 | 539 | wxMenu* menu = node->GetData(); |
c626a8b7 VZ |
540 | |
541 | if (menu->m_owner) | |
542 | gtk_widget_set_sensitive( menu->m_owner, flag ); | |
bbe0af5b RR |
543 | } |
544 | ||
3dfac970 | 545 | wxString wxMenuBar::GetLabelTop( size_t pos ) const |
bbe0af5b | 546 | { |
3dfac970 | 547 | wxMenuList::Node *node = m_menus.Item( pos ); |
c626a8b7 | 548 | |
223d09f6 | 549 | wxCHECK_MSG( node, wxT("invalid"), wxT("menu not found") ); |
c626a8b7 | 550 | |
3dfac970 | 551 | wxMenu* menu = node->GetData(); |
c626a8b7 | 552 | |
f6bcfd97 BP |
553 | wxString label; |
554 | wxString text( menu->GetTitle() ); | |
f6bcfd97 BP |
555 | for ( const wxChar *pc = text.c_str(); *pc; pc++ ) |
556 | { | |
557 | if ( *pc == wxT('_') || *pc == wxT('&') ) | |
558 | { | |
559 | // '_' is the escape character for GTK+ and '&' is the one for | |
560 | // wxWindows - skip both of them | |
561 | continue; | |
562 | } | |
563 | ||
564 | label += *pc; | |
565 | } | |
f6bcfd97 BP |
566 | |
567 | return label; | |
bbe0af5b RR |
568 | } |
569 | ||
3dfac970 | 570 | void wxMenuBar::SetLabelTop( size_t pos, const wxString& label ) |
bbe0af5b | 571 | { |
3dfac970 | 572 | wxMenuList::Node *node = m_menus.Item( pos ); |
c626a8b7 | 573 | |
223d09f6 | 574 | wxCHECK_RET( node, wxT("menu not found") ); |
c626a8b7 | 575 | |
3dfac970 | 576 | wxMenu* menu = node->GetData(); |
c626a8b7 | 577 | |
f6bcfd97 BP |
578 | wxString str( wxReplaceUnderscore( label ) ); |
579 | ||
580 | menu->SetTitle( str ); | |
581 | ||
582 | if (menu->m_owner) | |
583 | { | |
584 | GtkLabel *label = GTK_LABEL( GTK_BIN(menu->m_owner)->child ); | |
585 | ||
586 | /* set new text */ | |
fab591c5 | 587 | gtk_label_set( label, wxGTK_CONV( str ) ); |
f6bcfd97 BP |
588 | |
589 | /* reparse key accel */ | |
fab591c5 | 590 | (void)gtk_label_parse_uline (GTK_LABEL(label), wxGTK_CONV( str ) ); |
f6bcfd97 BP |
591 | gtk_accel_label_refetch( GTK_ACCEL_LABEL(label) ); |
592 | } | |
593 | ||
bbe0af5b RR |
594 | } |
595 | ||
c801d85f | 596 | //----------------------------------------------------------------------------- |
cf7a7e13 | 597 | // "activate" |
c801d85f KB |
598 | //----------------------------------------------------------------------------- |
599 | ||
6de97a3b | 600 | static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu ) |
c801d85f | 601 | { |
1e6feb95 VZ |
602 | if (g_isIdle) |
603 | wxapp_install_idle_handler(); | |
c4608a8a | 604 | |
83624f79 | 605 | int id = menu->FindMenuIdByMenuItem(widget); |
96fd301f | 606 | |
83624f79 | 607 | /* should find it for normal (not popup) menu */ |
1e6feb95 VZ |
608 | wxASSERT_MSG( (id != -1) || (menu->GetInvokingWindow() != NULL), |
609 | _T("menu item not found in gtk_menu_clicked_callback") ); | |
96fd301f | 610 | |
c626a8b7 VZ |
611 | if (!menu->IsEnabled(id)) |
612 | return; | |
96fd301f | 613 | |
717a57c2 | 614 | wxMenuItem* item = menu->FindChildItem( id ); |
223d09f6 | 615 | wxCHECK_RET( item, wxT("error in menu item callback") ); |
c626a8b7 VZ |
616 | |
617 | if (item->IsCheckable()) | |
2d17d68f | 618 | { |
705dd80a VZ |
619 | bool isReallyChecked = item->IsChecked(), |
620 | isInternallyChecked = item->wxMenuItemBase::IsChecked(); | |
621 | ||
622 | // ensure that the internal state is always consistent with what is | |
623 | // shown on the screen | |
624 | item->wxMenuItemBase::Check(isReallyChecked); | |
625 | ||
626 | // we must not report the events for the radio button going up nor the | |
627 | // events resulting from the calls to wxMenuItem::Check() | |
628 | if ( (item->GetKind() == wxITEM_RADIO && !isReallyChecked) || | |
629 | (isInternallyChecked == isReallyChecked) ) | |
2d17d68f | 630 | { |
2d17d68f | 631 | return; |
c626a8b7 | 632 | } |
705dd80a VZ |
633 | |
634 | // the user pressed on the menu item: report the event below | |
2d17d68f RR |
635 | } |
636 | ||
705dd80a | 637 | menu->SendEvent(id, item->IsCheckable() ? item->IsChecked() : -1); |
cf7a7e13 RR |
638 | } |
639 | ||
640 | //----------------------------------------------------------------------------- | |
641 | // "select" | |
642 | //----------------------------------------------------------------------------- | |
643 | ||
644 | static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu ) | |
645 | { | |
acfd422a RR |
646 | if (g_isIdle) wxapp_install_idle_handler(); |
647 | ||
83624f79 RR |
648 | int id = menu->FindMenuIdByMenuItem(widget); |
649 | ||
650 | wxASSERT( id != -1 ); // should find it! | |
cf7a7e13 | 651 | |
c626a8b7 VZ |
652 | if (!menu->IsEnabled(id)) |
653 | return; | |
cf7a7e13 | 654 | |
342b6a2f | 655 | wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id ); |
83624f79 | 656 | event.SetEventObject( menu ); |
cf7a7e13 | 657 | |
c626a8b7 VZ |
658 | if (menu->GetEventHandler()->ProcessEvent(event)) |
659 | return; | |
6de97a3b | 660 | |
83624f79 RR |
661 | wxWindow *win = menu->GetInvokingWindow(); |
662 | if (win) win->GetEventHandler()->ProcessEvent( event ); | |
6de97a3b | 663 | } |
c801d85f | 664 | |
cd743a6f RR |
665 | //----------------------------------------------------------------------------- |
666 | // "deselect" | |
667 | //----------------------------------------------------------------------------- | |
668 | ||
669 | static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu ) | |
670 | { | |
acfd422a RR |
671 | if (g_isIdle) wxapp_install_idle_handler(); |
672 | ||
cd743a6f RR |
673 | int id = menu->FindMenuIdByMenuItem(widget); |
674 | ||
675 | wxASSERT( id != -1 ); // should find it! | |
676 | ||
c626a8b7 VZ |
677 | if (!menu->IsEnabled(id)) |
678 | return; | |
cd743a6f RR |
679 | |
680 | wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 ); | |
681 | event.SetEventObject( menu ); | |
682 | ||
c626a8b7 VZ |
683 | if (menu->GetEventHandler()->ProcessEvent(event)) |
684 | return; | |
cd743a6f RR |
685 | |
686 | wxWindow *win = menu->GetInvokingWindow(); | |
c626a8b7 VZ |
687 | if (win) |
688 | win->GetEventHandler()->ProcessEvent( event ); | |
cd743a6f RR |
689 | } |
690 | ||
cf7a7e13 | 691 | //----------------------------------------------------------------------------- |
db1b4961 | 692 | // wxMenuItem |
cf7a7e13 RR |
693 | //----------------------------------------------------------------------------- |
694 | ||
7dbf5360 | 695 | IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject) |
974e8d94 VZ |
696 | |
697 | wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu, | |
698 | int id, | |
699 | const wxString& name, | |
700 | const wxString& help, | |
d65c269b | 701 | wxItemKind kind, |
974e8d94 VZ |
702 | wxMenu *subMenu) |
703 | { | |
d65c269b | 704 | return new wxMenuItem(parentMenu, id, name, help, kind, subMenu); |
974e8d94 | 705 | } |
96fd301f | 706 | |
974e8d94 VZ |
707 | wxMenuItem::wxMenuItem(wxMenu *parentMenu, |
708 | int id, | |
709 | const wxString& text, | |
710 | const wxString& help, | |
d65c269b | 711 | wxItemKind kind, |
974e8d94 | 712 | wxMenu *subMenu) |
d65c269b | 713 | : wxMenuItemBase(parentMenu, id, text, help, kind, subMenu) |
2368dcda | 714 | { |
092f7536 | 715 | Init(text); |
2368dcda VZ |
716 | } |
717 | ||
718 | wxMenuItem::wxMenuItem(wxMenu *parentMenu, | |
719 | int id, | |
720 | const wxString& text, | |
721 | const wxString& help, | |
722 | bool isCheckable, | |
723 | wxMenu *subMenu) | |
724 | : wxMenuItemBase(parentMenu, id, text, help, | |
725 | isCheckable ? wxITEM_CHECK : wxITEM_NORMAL, subMenu) | |
726 | { | |
092f7536 | 727 | Init(text); |
2368dcda VZ |
728 | } |
729 | ||
092f7536 | 730 | void wxMenuItem::Init(const wxString& text) |
c801d85f | 731 | { |
37d403aa | 732 | m_labelWidget = (GtkWidget *) NULL; |
83624f79 | 733 | m_menuItem = (GtkWidget *) NULL; |
974e8d94 | 734 | |
092f7536 | 735 | DoSetText(text); |
6de97a3b | 736 | } |
c801d85f | 737 | |
d1b15f03 RR |
738 | wxMenuItem::~wxMenuItem() |
739 | { | |
740 | // don't delete menu items, the menus take care of that | |
741 | } | |
742 | ||
717a57c2 | 743 | // return the menu item text without any menu accels |
3b59cdbf VZ |
744 | /* static */ |
745 | wxString wxMenuItemBase::GetLabelFromText(const wxString& text) | |
717a57c2 VZ |
746 | { |
747 | wxString label; | |
2368dcda | 748 | |
3b59cdbf | 749 | for ( const wxChar *pc = text.c_str(); *pc; pc++ ) |
717a57c2 | 750 | { |
d76419bd | 751 | if ( *pc == wxT('_') ) |
717a57c2 | 752 | { |
d76419bd RR |
753 | // wxGTK escapes "xxx_xxx" to "xxx__xxx" |
754 | pc++; | |
755 | label += *pc; | |
756 | continue; | |
757 | } | |
2368dcda | 758 | |
d76419bd RR |
759 | if ( *pc == wxT('&') ) |
760 | { | |
761 | // wxMSW escapes & | |
717a57c2 VZ |
762 | continue; |
763 | } | |
764 | ||
765 | label += *pc; | |
766 | } | |
717a57c2 VZ |
767 | |
768 | return label; | |
769 | } | |
770 | ||
771 | void wxMenuItem::SetText( const wxString& str ) | |
772 | { | |
5869f93f JS |
773 | // Some optimization to avoid flicker |
774 | wxString oldLabel = m_text; | |
775 | oldLabel = wxStripMenuCodes(oldLabel.BeforeFirst('\t')); | |
776 | oldLabel.Replace(wxT("_"), wxT("")); | |
777 | wxString label1 = wxStripMenuCodes(str.BeforeFirst('\t')); | |
778 | if (oldLabel == label1) | |
779 | return; | |
780 | ||
717a57c2 | 781 | DoSetText(str); |
354aa1e3 RR |
782 | |
783 | if (m_menuItem) | |
784 | { | |
37d403aa JS |
785 | GtkLabel *label; |
786 | if (m_labelWidget) | |
787 | label = (GtkLabel*) m_labelWidget; | |
788 | else | |
789 | label = GTK_LABEL( GTK_BIN(m_menuItem)->child ); | |
717a57c2 VZ |
790 | |
791 | /* set new text */ | |
fab591c5 | 792 | gtk_label_set( label, wxGTK_CONV( m_text ) ); |
717a57c2 VZ |
793 | |
794 | /* reparse key accel */ | |
fab591c5 | 795 | (void)gtk_label_parse_uline (GTK_LABEL(label), wxGTK_CONV( m_text ) ); |
354aa1e3 RR |
796 | gtk_accel_label_refetch( GTK_ACCEL_LABEL(label) ); |
797 | } | |
798 | } | |
799 | ||
c626a8b7 | 800 | // it's valid for this function to be called even if m_menuItem == NULL |
974e8d94 | 801 | void wxMenuItem::DoSetText( const wxString& str ) |
716b7364 | 802 | { |
ab46dc18 | 803 | /* '\t' is the deliminator indicating a hot key */ |
974e8d94 | 804 | m_text.Empty(); |
ab46dc18 | 805 | const wxChar *pc = str; |
223d09f6 | 806 | for (; (*pc != wxT('\0')) && (*pc != wxT('\t')); pc++ ) |
83624f79 | 807 | { |
4e9cbd33 | 808 | #if GTK_CHECK_VERSION(1, 2, 0) |
223d09f6 | 809 | if (*pc == wxT('&')) |
23280650 | 810 | { |
223d09f6 | 811 | m_text << wxT('_'); |
572d7461 | 812 | } |
223d09f6 | 813 | else if ( *pc == wxT('_') ) // escape underscores |
572d7461 | 814 | { |
223d09f6 | 815 | m_text << wxT("__"); |
572d7461 | 816 | } |
4e9cbd33 VS |
817 | #else // GTK+ < 1.2.0 |
818 | if (*pc == wxT('&')) | |
819 | { | |
820 | } | |
821 | #endif | |
822 | #if GTK_CHECK_VERSION(2, 0, 0) | |
823 | else if (*pc == wxT('/')) // we have to escape slashes | |
824 | { | |
825 | m_text << wxT("\\/"); | |
826 | } | |
827 | else if (*pc == wxT('\\')) // we have to double backslashes | |
828 | { | |
829 | m_text << wxT("\\\\"); | |
830 | } | |
831 | #elif GTK_CHECK_VERSION(1, 2, 0) | |
223d09f6 | 832 | else if (*pc == wxT('/')) /* we have to filter out slashes ... */ |
23280650 | 833 | { |
223d09f6 | 834 | m_text << wxT('\\'); /* ... and replace them with back slashes */ |
034be888 | 835 | } |
4e9cbd33 | 836 | #endif |
d38ceae8 | 837 | else |
354aa1e3 | 838 | m_text << *pc; |
83624f79 | 839 | } |
23280650 | 840 | |
223d09f6 | 841 | m_hotKey = wxT(""); |
9e691f46 | 842 | |
223d09f6 | 843 | if(*pc == wxT('\t')) |
d7dbc98a KB |
844 | { |
845 | pc++; | |
846 | m_hotKey = pc; | |
847 | } | |
716b7364 RR |
848 | } |
849 | ||
717a57c2 VZ |
850 | #if wxUSE_ACCEL |
851 | ||
852 | wxAcceleratorEntry *wxMenuItem::GetAccel() const | |
853 | { | |
1987af7e | 854 | if ( !GetHotKey() ) |
717a57c2 VZ |
855 | { |
856 | // nothing | |
857 | return (wxAcceleratorEntry *)NULL; | |
858 | } | |
859 | ||
860 | // as wxGetAccelFromString() looks for TAB, insert a dummy one here | |
861 | wxString label; | |
1987af7e | 862 | label << wxT('\t') << GetHotKey(); |
717a57c2 VZ |
863 | |
864 | return wxGetAccelFromString(label); | |
865 | } | |
866 | ||
867 | #endif // wxUSE_ACCEL | |
868 | ||
96fd301f | 869 | void wxMenuItem::Check( bool check ) |
716b7364 | 870 | { |
223d09f6 | 871 | wxCHECK_RET( m_menuItem, wxT("invalid menu item") ); |
db1b4961 | 872 | |
974e8d94 VZ |
873 | if (check == m_isChecked) |
874 | return; | |
2d17d68f | 875 | |
974e8d94 | 876 | wxMenuItemBase::Check( check ); |
0472ece7 | 877 | |
24bcaec3 | 878 | switch ( GetKind() ) |
0472ece7 | 879 | { |
24bcaec3 VZ |
880 | case wxITEM_CHECK: |
881 | case wxITEM_RADIO: | |
882 | gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check ); | |
883 | break; | |
884 | ||
885 | default: | |
886 | wxFAIL_MSG( _T("can't check this item") ); | |
0472ece7 | 887 | } |
716b7364 RR |
888 | } |
889 | ||
8bbe427f VZ |
890 | void wxMenuItem::Enable( bool enable ) |
891 | { | |
223d09f6 | 892 | wxCHECK_RET( m_menuItem, wxT("invalid menu item") ); |
db1b4961 | 893 | |
83624f79 | 894 | gtk_widget_set_sensitive( m_menuItem, enable ); |
974e8d94 | 895 | wxMenuItemBase::Enable( enable ); |
a9c96bcc RR |
896 | } |
897 | ||
96fd301f | 898 | bool wxMenuItem::IsChecked() const |
716b7364 | 899 | { |
223d09f6 | 900 | wxCHECK_MSG( m_menuItem, FALSE, wxT("invalid menu item") ); |
db1b4961 | 901 | |
974e8d94 VZ |
902 | wxCHECK_MSG( IsCheckable(), FALSE, |
903 | wxT("can't get state of uncheckable item!") ); | |
96fd301f | 904 | |
974e8d94 | 905 | return ((GtkCheckMenuItem*)m_menuItem)->active != 0; |
716b7364 RR |
906 | } |
907 | ||
354aa1e3 RR |
908 | wxString wxMenuItem::GetFactoryPath() const |
909 | { | |
d76419bd RR |
910 | /* in order to get the pointer to the item we need the item text |
911 | _without_ underscores */ | |
354aa1e3 | 912 | wxString path( wxT("<main>/") ); |
717a57c2 | 913 | |
d76419bd RR |
914 | for ( const wxChar *pc = m_text.c_str(); *pc; pc++ ) |
915 | { | |
916 | if ( *pc == wxT('_') || *pc == wxT('&') ) | |
917 | { | |
918 | // remove '_' and '&' unconditionally | |
919 | continue; | |
920 | } | |
2368dcda | 921 | |
d76419bd RR |
922 | path += *pc; |
923 | } | |
2368dcda | 924 | |
354aa1e3 RR |
925 | return path; |
926 | } | |
927 | ||
db1b4961 | 928 | //----------------------------------------------------------------------------- |
83624f79 | 929 | // wxMenu |
db1b4961 RR |
930 | //----------------------------------------------------------------------------- |
931 | ||
c801d85f KB |
932 | IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler) |
933 | ||
717a57c2 | 934 | void wxMenu::Init() |
c801d85f | 935 | { |
034be888 RR |
936 | m_accel = gtk_accel_group_new(); |
937 | m_factory = gtk_item_factory_new( GTK_TYPE_MENU, "<main>", m_accel ); | |
938 | m_menu = gtk_item_factory_get_widget( m_factory, "<main>" ); | |
8bbe427f | 939 | |
2b1c162e | 940 | m_owner = (GtkWidget*) NULL; |
2b2edbed | 941 | |
2b2edbed KB |
942 | /* Tearoffs are entries, just like separators. So if we want this |
943 | menu to be a tear-off one, we just append a tearoff entry | |
944 | immediately. */ | |
945 | if(m_style & wxMENU_TEAROFF) | |
946 | { | |
947 | GtkItemFactoryEntry entry; | |
90350682 | 948 | entry.path = (char *)"/tearoff"; |
2b2edbed KB |
949 | entry.callback = (GtkItemFactoryCallback) NULL; |
950 | entry.callback_action = 0; | |
90350682 | 951 | entry.item_type = (char *)"<Tearoff>"; |
2b2edbed KB |
952 | entry.accelerator = (gchar*) NULL; |
953 | gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */ | |
23280650 | 954 | //GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, "<main>/tearoff" ); |
2b2edbed | 955 | } |
c801d85f | 956 | |
717a57c2 VZ |
957 | // append the title as the very first entry if we have it |
958 | if ( !!m_title ) | |
d1b15f03 | 959 | { |
717a57c2 VZ |
960 | Append(-2, m_title); |
961 | AppendSeparator(); | |
d1b15f03 | 962 | } |
717a57c2 | 963 | } |
15a2076a | 964 | |
717a57c2 VZ |
965 | wxMenu::~wxMenu() |
966 | { | |
a583e623 | 967 | m_items.Clear(); |
f03ec224 | 968 | |
d1b15f03 | 969 | gtk_widget_destroy( m_menu ); |
974e8d94 | 970 | |
d1b15f03 | 971 | gtk_object_unref( GTK_OBJECT(m_factory) ); |
c2dd8380 GL |
972 | } |
973 | ||
32db328c | 974 | bool wxMenu::GtkAppend(wxMenuItem *mitem) |
c2dd8380 | 975 | { |
717a57c2 | 976 | GtkWidget *menuItem; |
96fd301f | 977 | |
9e691f46 | 978 | #if defined(USE_MENU_BITMAPS) || !GTK_CHECK_VERSION(1, 2, 0) |
37d403aa | 979 | bool appended = FALSE; |
9e691f46 | 980 | #endif |
37d403aa | 981 | |
24bcaec3 VZ |
982 | // does this item terminate the current radio group? |
983 | bool endOfRadioGroup = TRUE; | |
d65c269b | 984 | |
717a57c2 VZ |
985 | if ( mitem->IsSeparator() ) |
986 | { | |
717a57c2 | 987 | GtkItemFactoryEntry entry; |
90350682 | 988 | entry.path = (char *)"/sep"; |
717a57c2 VZ |
989 | entry.callback = (GtkItemFactoryCallback) NULL; |
990 | entry.callback_action = 0; | |
90350682 | 991 | entry.item_type = (char *)"<Separator>"; |
717a57c2 VZ |
992 | entry.accelerator = (gchar*) NULL; |
993 | ||
994 | gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */ | |
995 | ||
996 | /* this will be wrong for more than one separator. do we care? */ | |
997 | menuItem = gtk_item_factory_get_widget( m_factory, "<main>/sep" ); | |
24bcaec3 VZ |
998 | |
999 | // we might have a separator inside a radio group | |
1000 | endOfRadioGroup = FALSE; | |
717a57c2 VZ |
1001 | } |
1002 | else if ( mitem->IsSubMenu() ) | |
837904f2 | 1003 | { |
717a57c2 VZ |
1004 | /* text has "_" instead of "&" after mitem->SetText() */ |
1005 | wxString text( mitem->GetText() ); | |
974e8d94 | 1006 | |
717a57c2 VZ |
1007 | /* local buffer in multibyte form */ |
1008 | char buf[200]; | |
1009 | strcpy( buf, "/" ); | |
fab591c5 | 1010 | strcat( buf, wxGTK_CONV( text ) ); |
50592885 | 1011 | |
717a57c2 VZ |
1012 | GtkItemFactoryEntry entry; |
1013 | entry.path = buf; | |
1014 | entry.callback = (GtkItemFactoryCallback) 0; | |
1015 | entry.callback_action = 0; | |
90350682 | 1016 | entry.item_type = (char *)"<Branch>"; |
a583e623 | 1017 | entry.accelerator = (gchar*) NULL; |
50592885 | 1018 | |
717a57c2 | 1019 | gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */ |
50592885 | 1020 | |
717a57c2 | 1021 | wxString path( mitem->GetFactoryPath() ); |
fab591c5 | 1022 | menuItem = gtk_item_factory_get_item( m_factory, wxGTK_CONV( path ) ); |
50592885 | 1023 | |
1987af7e | 1024 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), mitem->GetSubMenu()->m_menu ); |
e7200790 VZ |
1025 | |
1026 | // if adding a submenu to a menu already existing in the menu bar, we | |
1027 | // must set invoking window to allow processing events from this | |
1028 | // submenu | |
1029 | if ( m_invokingWindow ) | |
1030 | wxMenubarSetInvokingWindow(mitem->GetSubMenu(), m_invokingWindow); | |
837904f2 | 1031 | } |
9e691f46 | 1032 | #ifdef USE_MENU_BITMAPS |
37d403aa JS |
1033 | else if (mitem->GetBitmap().Ok()) // An item with bitmap |
1034 | { | |
cc47eed9 RR |
1035 | wxString text( mitem->GetText() ); |
1036 | const wxBitmap *bitmap = &mitem->GetBitmap(); | |
2368dcda | 1037 | |
cc47eed9 | 1038 | menuItem = gtk_pixmap_menu_item_new (); |
fab591c5 | 1039 | GtkWidget *label = gtk_accel_label_new ( wxGTK_CONV( text ) ); |
cc47eed9 RR |
1040 | gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5); |
1041 | gtk_container_add (GTK_CONTAINER (menuItem), label); | |
fab591c5 | 1042 | guint accel_key = gtk_label_parse_uline (GTK_LABEL(label), wxGTK_CONV( text ) ); |
cc47eed9 RR |
1043 | gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), menuItem); |
1044 | if (accel_key != GDK_VoidSymbol) | |
8f262dc5 | 1045 | { |
cc47eed9 | 1046 | gtk_widget_add_accelerator (menuItem, |
8f262dc5 VZ |
1047 | "activate_item", |
1048 | gtk_menu_ensure_uline_accel_group (GTK_MENU (m_menu)), | |
1049 | accel_key, 0, | |
1050 | GTK_ACCEL_LOCKED); | |
cc47eed9 RR |
1051 | } |
1052 | gtk_widget_show (label); | |
37d403aa | 1053 | |
cc47eed9 | 1054 | mitem->SetLabelWidget(label); |
37d403aa | 1055 | |
cc47eed9 RR |
1056 | GtkWidget* pixmap = gtk_pixmap_new( bitmap->GetPixmap(), bitmap->GetMask() ? bitmap->GetMask()->GetBitmap() : (GdkBitmap* )NULL); |
1057 | gtk_widget_show(pixmap); | |
1058 | gtk_pixmap_menu_item_set_pixmap(GTK_PIXMAP_MENU_ITEM( menuItem ), pixmap); | |
37d403aa JS |
1059 | |
1060 | gtk_signal_connect( GTK_OBJECT(menuItem), "activate", | |
1061 | GTK_SIGNAL_FUNC(gtk_menu_clicked_callback), | |
1062 | (gpointer)this ); | |
1063 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); | |
1064 | gtk_widget_show( menuItem ); | |
1065 | ||
37d403aa JS |
1066 | appended = TRUE; // We've done this, don't do it again |
1067 | } | |
9e691f46 | 1068 | #endif // USE_MENU_BITMAPS |
717a57c2 VZ |
1069 | else // a normal item |
1070 | { | |
982f23fd | 1071 | // text has "_" instead of "&" after mitem->SetText() so don't use it |
717a57c2 VZ |
1072 | wxString text( mitem->GetText() ); |
1073 | ||
8f262dc5 VZ |
1074 | // buffers containing the menu item path and type in multibyte form |
1075 | char bufPath[256], | |
1076 | bufType[256]; | |
1077 | ||
1078 | strcpy( bufPath, "/" ); | |
1079 | strncat( bufPath, wxGTK_CONV(text), WXSIZEOF(bufPath) - 2 ); | |
1080 | bufPath[WXSIZEOF(bufPath) - 1] = '\0'; | |
717a57c2 VZ |
1081 | |
1082 | GtkItemFactoryEntry entry; | |
8f262dc5 | 1083 | entry.path = bufPath; |
717a57c2 VZ |
1084 | entry.callback = (GtkItemFactoryCallback) gtk_menu_clicked_callback; |
1085 | entry.callback_action = 0; | |
d65c269b VZ |
1086 | |
1087 | wxString pathRadio; | |
1088 | const char *item_type; | |
1089 | switch ( mitem->GetKind() ) | |
1090 | { | |
546bfbea | 1091 | case wxITEM_CHECK: |
d65c269b VZ |
1092 | item_type = "<CheckItem>"; |
1093 | break; | |
1094 | ||
546bfbea | 1095 | case wxITEM_RADIO: |
d65c269b VZ |
1096 | if ( m_pathLastRadio.empty() ) |
1097 | { | |
1098 | // start of a new radio group | |
1099 | item_type = "<RadioItem>"; | |
8f262dc5 | 1100 | m_pathLastRadio = bufPath + 1; |
d65c269b VZ |
1101 | } |
1102 | else // continue the radio group | |
1103 | { | |
1104 | pathRadio = m_pathLastRadio; | |
fab591c5 RR |
1105 | pathRadio.Replace(wxT("_"), wxT("")); |
1106 | pathRadio.Prepend(wxT("<main>/")); | |
982f23fd | 1107 | |
8f262dc5 VZ |
1108 | strncpy(bufType, wxGTK_CONV(pathRadio), WXSIZEOF(bufType)); |
1109 | bufType[WXSIZEOF(bufType) - 1] = '\0'; | |
1110 | item_type = bufType; | |
d65c269b VZ |
1111 | } |
1112 | ||
24bcaec3 VZ |
1113 | // continue the existing radio group, if any |
1114 | endOfRadioGroup = FALSE; | |
d65c269b VZ |
1115 | break; |
1116 | ||
1117 | default: | |
1118 | wxFAIL_MSG( _T("unexpected menu item kind") ); | |
1119 | // fall through | |
1120 | ||
546bfbea | 1121 | case wxITEM_NORMAL: |
d65c269b VZ |
1122 | item_type = "<Item>"; |
1123 | break; | |
1124 | } | |
1125 | ||
1126 | entry.item_type = (char *)item_type; // cast needed for GTK+ | |
a583e623 | 1127 | entry.accelerator = (gchar*) NULL; |
23280650 | 1128 | |
974e8d94 | 1129 | #if wxUSE_ACCEL |
717a57c2 VZ |
1130 | // due to an apparent bug in GTK+, we have to use a static buffer here - |
1131 | // otherwise GTK+ 1.2.2 manages to override the memory we pass to it | |
1132 | // somehow! (VZ) | |
982f23fd | 1133 | char s_accel[50]; // should be big enough, we check for overruns |
3ca6a5f0 | 1134 | wxString tmp( GetHotKey(*mitem) ); |
fab591c5 | 1135 | strncpy(s_accel, wxGTK_CONV( tmp ), WXSIZEOF(s_accel)); |
982f23fd | 1136 | s_accel[WXSIZEOF(s_accel) - 1] = '\0'; |
717a57c2 VZ |
1137 | entry.accelerator = s_accel; |
1138 | #else // !wxUSE_ACCEL | |
1139 | entry.accelerator = (char*) NULL; | |
1140 | #endif // wxUSE_ACCEL/!wxUSE_ACCEL | |
96fd301f | 1141 | |
717a57c2 | 1142 | gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */ |
cf7a7e13 | 1143 | |
717a57c2 | 1144 | wxString path( mitem->GetFactoryPath() ); |
fab591c5 | 1145 | menuItem = gtk_item_factory_get_widget( m_factory, wxGTK_CONV( path ) ); |
717a57c2 | 1146 | } |
23280650 | 1147 | |
717a57c2 VZ |
1148 | if ( !mitem->IsSeparator() ) |
1149 | { | |
1150 | gtk_signal_connect( GTK_OBJECT(menuItem), "select", | |
1151 | GTK_SIGNAL_FUNC(gtk_menu_hilight_callback), | |
1152 | (gpointer)this ); | |
837904f2 | 1153 | |
717a57c2 VZ |
1154 | gtk_signal_connect( GTK_OBJECT(menuItem), "deselect", |
1155 | GTK_SIGNAL_FUNC(gtk_menu_nolight_callback), | |
1156 | (gpointer)this ); | |
1157 | } | |
23280650 | 1158 | |
837904f2 | 1159 | mitem->SetMenuItem(menuItem); |
837904f2 | 1160 | |
24bcaec3 | 1161 | if ( endOfRadioGroup ) |
d65c269b VZ |
1162 | { |
1163 | m_pathLastRadio.clear(); | |
1164 | } | |
d65c269b | 1165 | |
32db328c | 1166 | return TRUE; |
6de97a3b | 1167 | } |
c801d85f | 1168 | |
32db328c | 1169 | bool wxMenu::DoAppend(wxMenuItem *mitem) |
828f655f | 1170 | { |
32db328c | 1171 | return GtkAppend(mitem) && wxMenuBase::DoAppend(mitem); |
c33c4050 RR |
1172 | } |
1173 | ||
717a57c2 | 1174 | bool wxMenu::DoInsert(size_t pos, wxMenuItem *item) |
c33c4050 | 1175 | { |
717a57c2 VZ |
1176 | if ( !wxMenuBase::DoInsert(pos, item) ) |
1177 | return FALSE; | |
c626a8b7 | 1178 | |
32db328c VZ |
1179 | #ifdef __WXGTK12__ |
1180 | // GTK+ doesn't have a function to insert a menu using GtkItemFactory (as | |
1181 | // of version 1.2.6), so we first append the item and then change its | |
1182 | // index | |
1183 | if ( !GtkAppend(item) ) | |
1184 | return FALSE; | |
1185 | ||
f6bcfd97 BP |
1186 | if ( m_style & wxMENU_TEAROFF ) |
1187 | { | |
1188 | // change the position as the first item is the tear-off marker | |
1189 | pos++; | |
1190 | } | |
1191 | ||
32db328c VZ |
1192 | GtkMenuShell *menu_shell = GTK_MENU_SHELL(m_factory->widget); |
1193 | gpointer data = g_list_last(menu_shell->children)->data; | |
1194 | menu_shell->children = g_list_remove(menu_shell->children, data); | |
1195 | menu_shell->children = g_list_insert(menu_shell->children, data, pos); | |
1196 | ||
1197 | return TRUE; | |
1198 | #else // GTK < 1.2 | |
1199 | // this should be easy to do... | |
1200 | wxFAIL_MSG( wxT("not implemented") ); | |
c626a8b7 | 1201 | |
717a57c2 | 1202 | return FALSE; |
96fa7876 | 1203 | #endif // GTK 1.2/1.0 |
c33c4050 RR |
1204 | } |
1205 | ||
717a57c2 | 1206 | wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) |
c33c4050 | 1207 | { |
717a57c2 VZ |
1208 | if ( !wxMenuBase::DoRemove(item) ) |
1209 | return (wxMenuItem *)NULL; | |
c626a8b7 | 1210 | |
717a57c2 VZ |
1211 | // TODO: this code doesn't delete the item factory item and this seems |
1212 | // impossible as of GTK 1.2.6. | |
1213 | gtk_widget_destroy( item->GetMenuItem() ); | |
c626a8b7 | 1214 | |
717a57c2 | 1215 | return item; |
c33c4050 RR |
1216 | } |
1217 | ||
96fd301f VZ |
1218 | int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const |
1219 | { | |
83624f79 RR |
1220 | wxNode *node = m_items.First(); |
1221 | while (node) | |
1222 | { | |
1223 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
1224 | if (item->GetMenuItem() == menuItem) | |
1225 | return item->GetId(); | |
1226 | node = node->Next(); | |
1227 | } | |
96fd301f | 1228 | |
c626a8b7 | 1229 | return wxNOT_FOUND; |
6de97a3b | 1230 | } |
c801d85f | 1231 | |
717a57c2 VZ |
1232 | // ---------------------------------------------------------------------------- |
1233 | // helpers | |
1234 | // ---------------------------------------------------------------------------- | |
1235 | ||
9e691f46 | 1236 | #if GTK_CHECK_VERSION(1, 2, 0) && wxUSE_ACCEL |
a070d8ce | 1237 | |
717a57c2 | 1238 | static wxString GetHotKey( const wxMenuItem& item ) |
c801d85f | 1239 | { |
717a57c2 VZ |
1240 | wxString hotkey; |
1241 | ||
1242 | wxAcceleratorEntry *accel = item.GetAccel(); | |
1243 | if ( accel ) | |
83624f79 | 1244 | { |
717a57c2 VZ |
1245 | int flags = accel->GetFlags(); |
1246 | if ( flags & wxACCEL_ALT ) | |
1247 | hotkey += wxT("<alt>"); | |
1248 | if ( flags & wxACCEL_CTRL ) | |
1249 | hotkey += wxT("<control>"); | |
1250 | if ( flags & wxACCEL_SHIFT ) | |
1251 | hotkey += wxT("<shift>"); | |
1252 | ||
1253 | int code = accel->GetKeyCode(); | |
1254 | switch ( code ) | |
c626a8b7 | 1255 | { |
717a57c2 VZ |
1256 | case WXK_F1: |
1257 | case WXK_F2: | |
1258 | case WXK_F3: | |
1259 | case WXK_F4: | |
1260 | case WXK_F5: | |
1261 | case WXK_F6: | |
1262 | case WXK_F7: | |
1263 | case WXK_F8: | |
1264 | case WXK_F9: | |
1265 | case WXK_F10: | |
1266 | case WXK_F11: | |
1267 | case WXK_F12: | |
1268 | hotkey << wxT('F') << code - WXK_F1 + 1; | |
1269 | break; | |
2368dcda | 1270 | |
a070d8ce VZ |
1271 | // TODO: we should use gdk_keyval_name() (a.k.a. |
1272 | // XKeysymToString) here as well as hardcoding the keysym | |
1273 | // names this might be not portable | |
3ca6a5f0 BP |
1274 | case WXK_NUMPAD_INSERT: |
1275 | hotkey << wxT("KP_Insert" ); | |
1276 | break; | |
1277 | case WXK_NUMPAD_DELETE: | |
1278 | hotkey << wxT("KP_Delete" ); | |
1279 | break; | |
1280 | case WXK_INSERT: | |
1281 | hotkey << wxT("Insert" ); | |
1282 | break; | |
1283 | case WXK_DELETE: | |
1284 | hotkey << wxT("Delete" ); | |
1285 | break; | |
96fd301f | 1286 | |
a070d8ce VZ |
1287 | // if there are any other keys wxGetAccelFromString() may |
1288 | // return, we should process them here | |
8bbe427f | 1289 | |
717a57c2 | 1290 | default: |
a070d8ce | 1291 | if ( code < 127 ) |
717a57c2 | 1292 | { |
a070d8ce VZ |
1293 | gchar *name = gdk_keyval_name((guint)code); |
1294 | if ( name ) | |
1295 | { | |
1296 | hotkey << name; | |
1297 | break; | |
1298 | } | |
717a57c2 | 1299 | } |
c801d85f | 1300 | |
717a57c2 VZ |
1301 | wxFAIL_MSG( wxT("unknown keyboard accel") ); |
1302 | } | |
c801d85f | 1303 | |
717a57c2 | 1304 | delete accel; |
631f1bfe | 1305 | } |
717a57c2 VZ |
1306 | |
1307 | return hotkey; | |
631f1bfe | 1308 | } |
a070d8ce | 1309 | |
717a57c2 | 1310 | #endif // wxUSE_ACCEL |
631f1bfe | 1311 | |
37d403aa | 1312 | |
cc47eed9 RR |
1313 | //----------------------------------------------------------------------------- |
1314 | // substitute for missing GtkPixmapMenuItem | |
1315 | //----------------------------------------------------------------------------- | |
37d403aa | 1316 | |
9e691f46 VZ |
1317 | #ifdef USE_MENU_BITMAPS |
1318 | ||
37d403aa JS |
1319 | /* |
1320 | * Copyright (C) 1998, 1999, 2000 Free Software Foundation | |
1321 | * All rights reserved. | |
1322 | * | |
1323 | * This file is part of the Gnome Library. | |
1324 | * | |
1325 | * The Gnome Library is free software; you can redistribute it and/or | |
1326 | * modify it under the terms of the GNU Library General Public License as | |
1327 | * published by the Free Software Foundation; either version 2 of the | |
1328 | * License, or (at your option) any later version. | |
1329 | * | |
1330 | * The Gnome Library is distributed in the hope that it will be useful, | |
1331 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
1332 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
1333 | * Library General Public License for more details. | |
1334 | * | |
1335 | * You should have received a copy of the GNU Library General Public | |
1336 | * License along with the Gnome Library; see the file COPYING.LIB. If not, | |
1337 | * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
1338 | * Boston, MA 02111-1307, USA. | |
1339 | */ | |
1340 | /* | |
1341 | @NOTATION@ | |
1342 | */ | |
1343 | ||
1344 | /* Author: Dietmar Maurer <dm@vlsivie.tuwien.ac.at> */ | |
1345 | ||
37d403aa JS |
1346 | #include <gtk/gtkaccellabel.h> |
1347 | #include <gtk/gtksignal.h> | |
1348 | #include <gtk/gtkmenuitem.h> | |
1349 | #include <gtk/gtkmenu.h> | |
1350 | #include <gtk/gtkcontainer.h> | |
1351 | ||
90350682 VZ |
1352 | extern "C" |
1353 | { | |
1354 | ||
37d403aa JS |
1355 | static void gtk_pixmap_menu_item_class_init (GtkPixmapMenuItemClass *klass); |
1356 | static void gtk_pixmap_menu_item_init (GtkPixmapMenuItem *menu_item); | |
1357 | static void gtk_pixmap_menu_item_draw (GtkWidget *widget, | |
8f262dc5 | 1358 | GdkRectangle *area); |
37d403aa | 1359 | static gint gtk_pixmap_menu_item_expose (GtkWidget *widget, |
8f262dc5 | 1360 | GdkEventExpose *event); |
37d403aa JS |
1361 | |
1362 | /* we must override the following functions */ | |
1363 | ||
1364 | static void gtk_pixmap_menu_item_map (GtkWidget *widget); | |
1365 | static void gtk_pixmap_menu_item_size_allocate (GtkWidget *widget, | |
8f262dc5 | 1366 | GtkAllocation *allocation); |
37d403aa | 1367 | static void gtk_pixmap_menu_item_forall (GtkContainer *container, |
8f262dc5 VZ |
1368 | gboolean include_internals, |
1369 | GtkCallback callback, | |
1370 | gpointer callback_data); | |
37d403aa | 1371 | static void gtk_pixmap_menu_item_size_request (GtkWidget *widget, |
8f262dc5 | 1372 | GtkRequisition *requisition); |
37d403aa | 1373 | static void gtk_pixmap_menu_item_remove (GtkContainer *container, |
8f262dc5 | 1374 | GtkWidget *child); |
37d403aa JS |
1375 | |
1376 | static void changed_have_pixmap_status (GtkPixmapMenuItem *menu_item); | |
1377 | ||
1378 | static GtkMenuItemClass *parent_class = NULL; | |
1379 | ||
90350682 VZ |
1380 | } |
1381 | ||
37d403aa JS |
1382 | #define BORDER_SPACING 3 |
1383 | #define PMAP_WIDTH 20 | |
1384 | ||
1385 | GtkType | |
1386 | gtk_pixmap_menu_item_get_type (void) | |
1387 | { | |
1388 | static GtkType pixmap_menu_item_type = 0; | |
1389 | ||
1390 | if (!pixmap_menu_item_type) | |
1391 | { | |
1392 | GtkTypeInfo pixmap_menu_item_info = | |
1393 | { | |
90350682 | 1394 | (char *)"GtkPixmapMenuItem", |
37d403aa JS |
1395 | sizeof (GtkPixmapMenuItem), |
1396 | sizeof (GtkPixmapMenuItemClass), | |
1397 | (GtkClassInitFunc) gtk_pixmap_menu_item_class_init, | |
1398 | (GtkObjectInitFunc) gtk_pixmap_menu_item_init, | |
1399 | /* reserved_1 */ NULL, | |
1400 | /* reserved_2 */ NULL, | |
1401 | (GtkClassInitFunc) NULL, | |
1402 | }; | |
1403 | ||
2368dcda | 1404 | pixmap_menu_item_type = gtk_type_unique (gtk_menu_item_get_type (), |
8f262dc5 | 1405 | &pixmap_menu_item_info); |
37d403aa JS |
1406 | } |
1407 | ||
1408 | return pixmap_menu_item_type; | |
1409 | } | |
1410 | ||
1411 | /** | |
1412 | * gtk_pixmap_menu_item_new | |
1413 | * | |
2368dcda | 1414 | * Creates a new pixmap menu item. Use gtk_pixmap_menu_item_set_pixmap() |
37d403aa JS |
1415 | * to set the pixmap wich is displayed at the left side. |
1416 | * | |
1417 | * Returns: | |
1418 | * &GtkWidget pointer to new menu item | |
1419 | **/ | |
1420 | ||
1421 | GtkWidget* | |
1422 | gtk_pixmap_menu_item_new (void) | |
1423 | { | |
1424 | return GTK_WIDGET (gtk_type_new (gtk_pixmap_menu_item_get_type ())); | |
1425 | } | |
1426 | ||
1427 | static void | |
1428 | gtk_pixmap_menu_item_class_init (GtkPixmapMenuItemClass *klass) | |
1429 | { | |
1430 | GtkObjectClass *object_class; | |
1431 | GtkWidgetClass *widget_class; | |
1432 | GtkMenuItemClass *menu_item_class; | |
1433 | GtkContainerClass *container_class; | |
1434 | ||
1435 | object_class = (GtkObjectClass*) klass; | |
1436 | widget_class = (GtkWidgetClass*) klass; | |
1437 | menu_item_class = (GtkMenuItemClass*) klass; | |
1438 | container_class = (GtkContainerClass*) klass; | |
1439 | ||
1440 | parent_class = (GtkMenuItemClass*) gtk_type_class (gtk_menu_item_get_type ()); | |
1441 | ||
1442 | widget_class->draw = gtk_pixmap_menu_item_draw; | |
1443 | widget_class->expose_event = gtk_pixmap_menu_item_expose; | |
1444 | widget_class->map = gtk_pixmap_menu_item_map; | |
1445 | widget_class->size_allocate = gtk_pixmap_menu_item_size_allocate; | |
1446 | widget_class->size_request = gtk_pixmap_menu_item_size_request; | |
1447 | ||
1448 | container_class->forall = gtk_pixmap_menu_item_forall; | |
1449 | container_class->remove = gtk_pixmap_menu_item_remove; | |
1450 | ||
1451 | klass->orig_toggle_size = menu_item_class->toggle_size; | |
1452 | klass->have_pixmap_count = 0; | |
1453 | } | |
1454 | ||
1455 | static void | |
1456 | gtk_pixmap_menu_item_init (GtkPixmapMenuItem *menu_item) | |
1457 | { | |
1458 | GtkMenuItem *mi; | |
1459 | ||
1460 | mi = GTK_MENU_ITEM (menu_item); | |
1461 | ||
1462 | menu_item->pixmap = NULL; | |
1463 | } | |
1464 | ||
1465 | static void | |
1466 | gtk_pixmap_menu_item_draw (GtkWidget *widget, | |
8f262dc5 | 1467 | GdkRectangle *area) |
37d403aa JS |
1468 | { |
1469 | g_return_if_fail (widget != NULL); | |
1470 | g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (widget)); | |
1471 | g_return_if_fail (area != NULL); | |
1472 | ||
1473 | if (GTK_WIDGET_CLASS (parent_class)->draw) | |
1474 | (* GTK_WIDGET_CLASS (parent_class)->draw) (widget, area); | |
1475 | ||
2368dcda | 1476 | if (GTK_WIDGET_DRAWABLE (widget) && |
37d403aa JS |
1477 | GTK_PIXMAP_MENU_ITEM(widget)->pixmap) { |
1478 | gtk_widget_draw(GTK_WIDGET(GTK_PIXMAP_MENU_ITEM(widget)->pixmap),NULL); | |
1479 | } | |
1480 | } | |
1481 | ||
1482 | static gint | |
1483 | gtk_pixmap_menu_item_expose (GtkWidget *widget, | |
8f262dc5 | 1484 | GdkEventExpose *event) |
37d403aa JS |
1485 | { |
1486 | g_return_val_if_fail (widget != NULL, FALSE); | |
1487 | g_return_val_if_fail (GTK_IS_PIXMAP_MENU_ITEM (widget), FALSE); | |
1488 | g_return_val_if_fail (event != NULL, FALSE); | |
1489 | ||
1490 | if (GTK_WIDGET_CLASS (parent_class)->expose_event) | |
1491 | (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event); | |
1492 | ||
2368dcda | 1493 | if (GTK_WIDGET_DRAWABLE (widget) && |
37d403aa JS |
1494 | GTK_PIXMAP_MENU_ITEM(widget)->pixmap) { |
1495 | gtk_widget_draw(GTK_WIDGET(GTK_PIXMAP_MENU_ITEM(widget)->pixmap),NULL); | |
1496 | } | |
1497 | ||
1498 | return FALSE; | |
1499 | } | |
1500 | ||
1501 | /** | |
1502 | * gtk_pixmap_menu_item_set_pixmap | |
1503 | * @menu_item: Pointer to the pixmap menu item | |
1504 | * @pixmap: Pointer to a pixmap widget | |
1505 | * | |
1506 | * Set the pixmap of the menu item. | |
1507 | * | |
1508 | **/ | |
1509 | ||
1510 | void | |
1511 | gtk_pixmap_menu_item_set_pixmap (GtkPixmapMenuItem *menu_item, | |
8f262dc5 | 1512 | GtkWidget *pixmap) |
37d403aa JS |
1513 | { |
1514 | g_return_if_fail (menu_item != NULL); | |
1515 | g_return_if_fail (pixmap != NULL); | |
1516 | g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (menu_item)); | |
1517 | g_return_if_fail (GTK_IS_WIDGET (pixmap)); | |
1518 | g_return_if_fail (menu_item->pixmap == NULL); | |
1519 | ||
1520 | gtk_widget_set_parent (pixmap, GTK_WIDGET (menu_item)); | |
1521 | menu_item->pixmap = pixmap; | |
1522 | ||
1523 | if (GTK_WIDGET_REALIZED (pixmap->parent) && | |
1524 | !GTK_WIDGET_REALIZED (pixmap)) | |
1525 | gtk_widget_realize (pixmap); | |
2368dcda VZ |
1526 | |
1527 | if (GTK_WIDGET_VISIBLE (pixmap->parent)) { | |
37d403aa | 1528 | if (GTK_WIDGET_MAPPED (pixmap->parent) && |
8f262dc5 | 1529 | GTK_WIDGET_VISIBLE(pixmap) && |
37d403aa JS |
1530 | !GTK_WIDGET_MAPPED (pixmap)) |
1531 | gtk_widget_map (pixmap); | |
1532 | } | |
1533 | ||
1534 | changed_have_pixmap_status(menu_item); | |
2368dcda | 1535 | |
37d403aa JS |
1536 | if (GTK_WIDGET_VISIBLE (pixmap) && GTK_WIDGET_VISIBLE (menu_item)) |
1537 | gtk_widget_queue_resize (pixmap); | |
1538 | } | |
1539 | ||
1540 | static void | |
1541 | gtk_pixmap_menu_item_map (GtkWidget *widget) | |
1542 | { | |
1543 | GtkPixmapMenuItem *menu_item; | |
1544 | ||
1545 | g_return_if_fail (widget != NULL); | |
1546 | g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (widget)); | |
1547 | ||
1548 | menu_item = GTK_PIXMAP_MENU_ITEM(widget); | |
1549 | ||
1550 | GTK_WIDGET_CLASS(parent_class)->map(widget); | |
1551 | ||
1552 | if (menu_item->pixmap && | |
1553 | GTK_WIDGET_VISIBLE (menu_item->pixmap) && | |
1554 | !GTK_WIDGET_MAPPED (menu_item->pixmap)) | |
1555 | gtk_widget_map (menu_item->pixmap); | |
1556 | } | |
1557 | ||
1558 | static void | |
1559 | gtk_pixmap_menu_item_size_allocate (GtkWidget *widget, | |
8f262dc5 | 1560 | GtkAllocation *allocation) |
37d403aa JS |
1561 | { |
1562 | GtkPixmapMenuItem *pmenu_item; | |
1563 | ||
1564 | pmenu_item = GTK_PIXMAP_MENU_ITEM(widget); | |
1565 | ||
1566 | if (pmenu_item->pixmap && GTK_WIDGET_VISIBLE(pmenu_item)) | |
1567 | { | |
1568 | GtkAllocation child_allocation; | |
1569 | int border_width; | |
1570 | ||
1571 | border_width = GTK_CONTAINER (widget)->border_width; | |
1572 | ||
1573 | child_allocation.width = pmenu_item->pixmap->requisition.width; | |
1574 | child_allocation.height = pmenu_item->pixmap->requisition.height; | |
1575 | child_allocation.x = border_width + BORDER_SPACING; | |
1576 | child_allocation.y = (border_width + BORDER_SPACING | |
8f262dc5 VZ |
1577 | + (((allocation->height - child_allocation.height) - child_allocation.x) |
1578 | / 2)); /* center pixmaps vertically */ | |
37d403aa JS |
1579 | gtk_widget_size_allocate (pmenu_item->pixmap, &child_allocation); |
1580 | } | |
1581 | ||
1582 | if (GTK_WIDGET_CLASS (parent_class)->size_allocate) | |
1583 | GTK_WIDGET_CLASS(parent_class)->size_allocate (widget, allocation); | |
1584 | } | |
1585 | ||
1586 | static void | |
1587 | gtk_pixmap_menu_item_forall (GtkContainer *container, | |
8f262dc5 VZ |
1588 | gboolean include_internals, |
1589 | GtkCallback callback, | |
1590 | gpointer callback_data) | |
37d403aa JS |
1591 | { |
1592 | GtkPixmapMenuItem *menu_item; | |
1593 | ||
1594 | g_return_if_fail (container != NULL); | |
1595 | g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (container)); | |
1596 | g_return_if_fail (callback != NULL); | |
1597 | ||
1598 | menu_item = GTK_PIXMAP_MENU_ITEM (container); | |
1599 | ||
1600 | if (menu_item->pixmap) | |
1601 | (* callback) (menu_item->pixmap, callback_data); | |
1602 | ||
1603 | GTK_CONTAINER_CLASS(parent_class)->forall(container,include_internals, | |
8f262dc5 | 1604 | callback,callback_data); |
37d403aa JS |
1605 | } |
1606 | ||
1607 | static void | |
1608 | gtk_pixmap_menu_item_size_request (GtkWidget *widget, | |
8f262dc5 | 1609 | GtkRequisition *requisition) |
37d403aa JS |
1610 | { |
1611 | GtkPixmapMenuItem *menu_item; | |
1612 | GtkRequisition req = {0, 0}; | |
1613 | ||
1614 | g_return_if_fail (widget != NULL); | |
1615 | g_return_if_fail (GTK_IS_MENU_ITEM (widget)); | |
1616 | g_return_if_fail (requisition != NULL); | |
1617 | ||
1618 | GTK_WIDGET_CLASS(parent_class)->size_request(widget,requisition); | |
1619 | ||
1620 | menu_item = GTK_PIXMAP_MENU_ITEM (widget); | |
2368dcda | 1621 | |
37d403aa JS |
1622 | if (menu_item->pixmap) |
1623 | gtk_widget_size_request(menu_item->pixmap, &req); | |
1624 | ||
1625 | requisition->height = MAX(req.height + GTK_CONTAINER(widget)->border_width + BORDER_SPACING, (unsigned int) requisition->height); | |
1626 | requisition->width += (req.width + GTK_CONTAINER(widget)->border_width + BORDER_SPACING); | |
1627 | } | |
1628 | ||
1629 | static void | |
1630 | gtk_pixmap_menu_item_remove (GtkContainer *container, | |
8f262dc5 | 1631 | GtkWidget *child) |
37d403aa JS |
1632 | { |
1633 | GtkBin *bin; | |
1634 | gboolean widget_was_visible; | |
1635 | ||
1636 | g_return_if_fail (container != NULL); | |
1637 | g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (container)); | |
1638 | g_return_if_fail (child != NULL); | |
1639 | g_return_if_fail (GTK_IS_WIDGET (child)); | |
1640 | ||
1641 | bin = GTK_BIN (container); | |
2368dcda | 1642 | g_return_if_fail ((bin->child == child || |
8f262dc5 | 1643 | (GTK_PIXMAP_MENU_ITEM(container)->pixmap == child))); |
37d403aa JS |
1644 | |
1645 | widget_was_visible = GTK_WIDGET_VISIBLE (child); | |
2368dcda | 1646 | |
37d403aa JS |
1647 | gtk_widget_unparent (child); |
1648 | if (bin->child == child) | |
2368dcda | 1649 | bin->child = NULL; |
37d403aa JS |
1650 | else { |
1651 | GTK_PIXMAP_MENU_ITEM(container)->pixmap = NULL; | |
1652 | changed_have_pixmap_status(GTK_PIXMAP_MENU_ITEM(container)); | |
1653 | } | |
2368dcda | 1654 | |
37d403aa JS |
1655 | if (widget_was_visible) |
1656 | gtk_widget_queue_resize (GTK_WIDGET (container)); | |
1657 | } | |
1658 | ||
1659 | ||
1660 | /* important to only call this if there was actually a _change_ in pixmap == NULL */ | |
1661 | static void | |
1662 | changed_have_pixmap_status (GtkPixmapMenuItem *menu_item) | |
1663 | { | |
1664 | if (menu_item->pixmap != NULL) { | |
1665 | GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->have_pixmap_count += 1; | |
1666 | ||
1667 | if (GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->have_pixmap_count == 1) { | |
1668 | /* Install pixmap toggle size */ | |
1669 | GTK_MENU_ITEM_GET_CLASS(menu_item)->toggle_size = MAX(GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->orig_toggle_size, PMAP_WIDTH); | |
1670 | } | |
1671 | } else { | |
1672 | GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->have_pixmap_count -= 1; | |
1673 | ||
1674 | if (GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->have_pixmap_count == 0) { | |
1675 | /* Install normal toggle size */ | |
2368dcda | 1676 | GTK_MENU_ITEM_GET_CLASS(menu_item)->toggle_size = GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->orig_toggle_size; |
37d403aa JS |
1677 | } |
1678 | } | |
1679 | ||
1680 | /* Note that we actually need to do this for _all_ GtkPixmapMenuItem | |
1681 | whenever the klass->toggle_size changes; but by doing it anytime | |
1682 | this function is called, we get the same effect, just because of | |
1683 | how the preferences option to show pixmaps works. Bogus, broken. | |
1684 | */ | |
2368dcda | 1685 | if (GTK_WIDGET_VISIBLE(GTK_WIDGET(menu_item))) |
37d403aa JS |
1686 | gtk_widget_queue_resize(GTK_WIDGET(menu_item)); |
1687 | } | |
1688 | ||
9e691f46 | 1689 | #endif // USE_MENU_BITMAPS |
37d403aa | 1690 |