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