wxMenuItem code cleanup, removing duplicate and unneccessary code
[wxWidgets.git] / src / gtk / menu.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/menu.cpp
3 // Purpose: implementation of wxMenuBar and wxMenu classes for wxGTK
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_MENUS
14
15 #include "wx/menu.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/intl.h"
19 #include "wx/log.h"
20 #include "wx/frame.h"
21 #include "wx/bitmap.h"
22 #include "wx/app.h"
23 #endif
24
25 #include "wx/accel.h"
26 #include "wx/stockitem.h"
27 #include "wx/gtk/private.h"
28 #include "wx/gtk/private/mnemonics.h"
29
30 // we use normal item but with a special id for the menu title
31 static const int wxGTK_TITLE_ID = -3;
32
33 // forward declare it as it's used by wxMenuBar too when using Hildon
34 extern "C"
35 {
36 static void gtk_menu_clicked_callback(GtkWidget *widget, wxMenu *menu);
37 }
38
39 #if wxUSE_ACCEL
40 static void wxGetGtkAccel(const wxMenuItem*, guint*, GdkModifierType*);
41 #endif
42
43 static void DoCommonMenuCallbackCode(wxMenu *menu, wxMenuEvent& event)
44 {
45 event.SetEventObject( menu );
46
47 wxEvtHandler* handler = menu->GetEventHandler();
48 if (handler && handler->SafelyProcessEvent(event))
49 return;
50
51 wxWindow *win = menu->GetInvokingWindow();
52 if (win)
53 win->HandleWindowEvent( event );
54 }
55
56 //-----------------------------------------------------------------------------
57 // wxMenuBar
58 //-----------------------------------------------------------------------------
59
60 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
61
62 void wxMenuBar::Init(size_t n, wxMenu *menus[], const wxString titles[], long style)
63 {
64 m_invokingWindow = NULL;
65
66 #if wxUSE_LIBHILDON
67 // Hildon window uses a single menu instead of a menu bar, so wxMenuBar is
68 // the same as menu in this case
69 m_widget =
70 m_menubar = gtk_menu_new();
71 #else // !wxUSE_LIBHILDON
72 if (!PreCreation( NULL, wxDefaultPosition, wxDefaultSize ) ||
73 !CreateBase( NULL, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, wxT("menubar") ))
74 {
75 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
76 return;
77 }
78
79 m_menubar = gtk_menu_bar_new();
80
81 if (style & wxMB_DOCKABLE)
82 {
83 m_widget = gtk_handle_box_new();
84 gtk_container_add(GTK_CONTAINER(m_widget), m_menubar);
85 gtk_widget_show(m_menubar);
86 }
87 else
88 {
89 m_widget = m_menubar;
90 }
91
92 PostCreation();
93
94 ApplyWidgetStyle();
95 #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
96
97 for (size_t i = 0; i < n; ++i )
98 Append(menus[i], titles[i]);
99 }
100
101 wxMenuBar::wxMenuBar(size_t n, wxMenu *menus[], const wxString titles[], long style)
102 {
103 Init(n, menus, titles, style);
104 }
105
106 wxMenuBar::wxMenuBar(long style)
107 {
108 Init(0, NULL, NULL, style);
109 }
110
111 wxMenuBar::wxMenuBar()
112 {
113 Init(0, NULL, NULL, 0);
114 }
115
116 wxMenuBar::~wxMenuBar()
117 {
118 }
119
120 static void
121 wxMenubarUnsetInvokingWindow(wxMenu* menu, wxWindow* win, GtkWindow* tlw = NULL)
122 {
123 menu->SetInvokingWindow( (wxWindow*) NULL );
124
125 // support for native hot keys
126 if (menu->m_accel)
127 {
128 if (tlw == NULL)
129 tlw = GTK_WINDOW(wxGetTopLevelParent(win)->m_widget);
130 if (g_slist_find(menu->m_accel->acceleratables, tlw))
131 gtk_window_remove_accel_group(tlw, menu->m_accel);
132 }
133
134 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
135 while (node)
136 {
137 wxMenuItem *menuitem = node->GetData();
138 if (menuitem->IsSubMenu())
139 wxMenubarUnsetInvokingWindow(menuitem->GetSubMenu(), win, tlw);
140 node = node->GetNext();
141 }
142 }
143
144 static void
145 wxMenubarSetInvokingWindow(wxMenu* menu, wxWindow* win, GtkWindow* tlw = NULL)
146 {
147 menu->SetInvokingWindow( win );
148
149 // support for native hot keys
150 if (menu->m_accel)
151 {
152 if (tlw == NULL)
153 tlw = GTK_WINDOW(wxGetTopLevelParent(win)->m_widget);
154 if (!g_slist_find(menu->m_accel->acceleratables, tlw))
155 gtk_window_add_accel_group(tlw, menu->m_accel);
156 }
157
158 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
159 while (node)
160 {
161 wxMenuItem *menuitem = node->GetData();
162 if (menuitem->IsSubMenu())
163 wxMenubarSetInvokingWindow(menuitem->GetSubMenu(), win, tlw);
164 node = node->GetNext();
165 }
166 }
167
168 void wxMenuBar::SetInvokingWindow( wxWindow *win )
169 {
170 m_invokingWindow = win;
171
172 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
173 while (node)
174 {
175 wxMenu *menu = node->GetData();
176 wxMenubarSetInvokingWindow( menu, win );
177 node = node->GetNext();
178 }
179 }
180
181 void wxMenuBar::SetLayoutDirection(wxLayoutDirection dir)
182 {
183 if ( dir == wxLayout_Default )
184 {
185 const wxWindow *const frame = GetFrame();
186 if ( frame )
187 {
188 // inherit layout from frame.
189 dir = frame->GetLayoutDirection();
190 }
191 else // use global layout
192 {
193 dir = wxTheApp->GetLayoutDirection();
194 }
195 }
196
197 if ( dir == wxLayout_Default )
198 return;
199
200 GTKSetLayout(m_menubar, dir);
201
202 // also set the layout of all menus we already have (new ones will inherit
203 // the current layout)
204 for ( wxMenuList::compatibility_iterator node = m_menus.GetFirst();
205 node;
206 node = node->GetNext() )
207 {
208 wxMenu *const menu = node->GetData();
209 menu->SetLayoutDirection(dir);
210 }
211 }
212
213 wxLayoutDirection wxMenuBar::GetLayoutDirection() const
214 {
215 return GTKGetLayout(m_menubar);
216 }
217
218 void wxMenuBar::Attach(wxFrame *frame)
219 {
220 wxMenuBarBase::Attach(frame);
221
222 SetLayoutDirection(wxLayout_Default);
223 }
224
225 void wxMenuBar::UnsetInvokingWindow( wxWindow *win )
226 {
227 m_invokingWindow = (wxWindow*) NULL;
228
229 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
230 while (node)
231 {
232 wxMenu *menu = node->GetData();
233 wxMenubarUnsetInvokingWindow( menu, win );
234 node = node->GetNext();
235 }
236 }
237
238 bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
239 {
240 if ( !wxMenuBarBase::Append( menu, title ) )
241 return false;
242
243 return GtkAppend(menu, title);
244 }
245
246 bool wxMenuBar::GtkAppend(wxMenu *menu, const wxString& title, int pos)
247 {
248 menu->SetLayoutDirection(GetLayoutDirection());
249
250 #if wxUSE_LIBHILDON
251 // if the menu has only one item, append it directly to the top level menu
252 // instead of inserting a useless submenu
253 if ( menu->GetMenuItemCount() == 1 )
254 {
255 wxMenuItem * const item = menu->FindItemByPosition(0);
256
257 // remove both mnemonics and accelerator: neither is useful under Maemo
258 const wxString str(wxStripMenuCodes(item->GetItemLabel()));
259
260 if ( item->IsSubMenu() )
261 return GtkAppend(item->GetSubMenu(), str, pos);
262
263 menu->m_owner = gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str ) );
264
265 g_signal_connect(menu->m_owner, "activate",
266 G_CALLBACK(gtk_menu_clicked_callback), menu);
267 item->SetMenuItem(menu->m_owner);
268 }
269 else
270 #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
271 {
272 const wxString str(wxConvertMnemonicsToGTK(title));
273
274 // This doesn't have much effect right now.
275 menu->SetTitle( str );
276
277 // The "m_owner" is the "menu item"
278 menu->m_owner = gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str ) );
279
280 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu );
281 }
282
283 gtk_widget_show( menu->m_owner );
284
285 if (pos == -1)
286 gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar), menu->m_owner );
287 else
288 gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar), menu->m_owner, pos );
289
290 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
291 // addings menu later on.
292 if (m_invokingWindow)
293 wxMenubarSetInvokingWindow( menu, m_invokingWindow );
294
295 return true;
296 }
297
298 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
299 {
300 if ( !wxMenuBarBase::Insert(pos, menu, title) )
301 return false;
302
303 // TODO
304
305 if ( !GtkAppend(menu, title, (int)pos) )
306 return false;
307
308 return true;
309 }
310
311 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
312 {
313 // remove the old item and insert a new one
314 wxMenu *menuOld = Remove(pos);
315 if ( menuOld && !Insert(pos, menu, title) )
316 {
317 return (wxMenu*) NULL;
318 }
319
320 // either Insert() succeeded or Remove() failed and menuOld is NULL
321 return menuOld;
322 }
323
324 wxMenu *wxMenuBar::Remove(size_t pos)
325 {
326 wxMenu *menu = wxMenuBarBase::Remove(pos);
327 if ( !menu )
328 return (wxMenu*) NULL;
329
330 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu->m_owner), NULL);
331 gtk_container_remove(GTK_CONTAINER(m_menubar), menu->m_owner);
332
333 gtk_widget_destroy( menu->m_owner );
334 menu->m_owner = NULL;
335
336 if (m_invokingWindow)
337 wxMenubarUnsetInvokingWindow( menu, m_invokingWindow );
338
339 return menu;
340 }
341
342 static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString )
343 {
344 if (wxMenuItem::GetLabelText(wxConvertMnemonicsFromGTK(menu->GetTitle())) == wxMenuItem::GetLabelText(menuString))
345 {
346 int res = menu->FindItem( itemString );
347 if (res != wxNOT_FOUND)
348 return res;
349 }
350
351 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
352 while (node)
353 {
354 wxMenuItem *item = node->GetData();
355 if (item->IsSubMenu())
356 return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString);
357
358 node = node->GetNext();
359 }
360
361 return wxNOT_FOUND;
362 }
363
364 int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const
365 {
366 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
367 while (node)
368 {
369 wxMenu *menu = node->GetData();
370 int res = FindMenuItemRecursive( menu, menuString, itemString);
371 if (res != -1)
372 return res;
373 node = node->GetNext();
374 }
375
376 return wxNOT_FOUND;
377 }
378
379 // Find a wxMenuItem using its id. Recurses down into sub-menus
380 static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id)
381 {
382 wxMenuItem* result = menu->FindChildItem(id);
383
384 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
385 while ( node && result == NULL )
386 {
387 wxMenuItem *item = node->GetData();
388 if (item->IsSubMenu())
389 {
390 result = FindMenuItemByIdRecursive( item->GetSubMenu(), id );
391 }
392 node = node->GetNext();
393 }
394
395 return result;
396 }
397
398 wxMenuItem* wxMenuBar::FindItem( int id, wxMenu **menuForItem ) const
399 {
400 wxMenuItem* result = 0;
401 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
402 while (node && result == 0)
403 {
404 wxMenu *menu = node->GetData();
405 result = FindMenuItemByIdRecursive( menu, id );
406 node = node->GetNext();
407 }
408
409 if ( menuForItem )
410 {
411 *menuForItem = result ? result->GetMenu() : (wxMenu *)NULL;
412 }
413
414 return result;
415 }
416
417 void wxMenuBar::EnableTop( size_t pos, bool flag )
418 {
419 wxMenuList::compatibility_iterator node = m_menus.Item( pos );
420
421 wxCHECK_RET( node, wxT("menu not found") );
422
423 wxMenu* menu = node->GetData();
424
425 if (menu->m_owner)
426 gtk_widget_set_sensitive( menu->m_owner, flag );
427 }
428
429 wxString wxMenuBar::GetMenuLabel( size_t pos ) const
430 {
431 wxMenuList::compatibility_iterator node = m_menus.Item( pos );
432
433 wxCHECK_MSG( node, wxT("invalid"), wxT("menu not found") );
434
435 wxMenu* menu = node->GetData();
436
437 return wxConvertMnemonicsFromGTK(menu->GetTitle());
438 }
439
440 void wxMenuBar::SetMenuLabel( size_t pos, const wxString& label )
441 {
442 wxMenuList::compatibility_iterator node = m_menus.Item( pos );
443
444 wxCHECK_RET( node, wxT("menu not found") );
445
446 wxMenu* menu = node->GetData();
447
448 const wxString str(wxConvertMnemonicsToGTK(label));
449
450 menu->SetTitle( str );
451
452 if (menu->m_owner)
453 gtk_label_set_text_with_mnemonic( GTK_LABEL( GTK_BIN(menu->m_owner)->child), wxGTK_CONV(str) );
454 }
455
456 //-----------------------------------------------------------------------------
457 // "activate"
458 //-----------------------------------------------------------------------------
459
460 extern "C" {
461 static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu )
462 {
463 int id = menu->FindMenuIdByMenuItem(widget);
464
465 /* should find it for normal (not popup) menu */
466 wxASSERT_MSG( (id != -1) || (menu->GetInvokingWindow() != NULL),
467 _T("menu item not found in gtk_menu_clicked_callback") );
468
469 if (!menu->IsEnabled(id))
470 return;
471
472 wxMenuItem* item = menu->FindChildItem( id );
473 wxCHECK_RET( item, wxT("error in menu item callback") );
474
475 if ( item->GetId() == wxGTK_TITLE_ID )
476 {
477 // ignore events from the menu title
478 return;
479 }
480
481 if (item->IsCheckable())
482 {
483 bool isReallyChecked = item->IsChecked(),
484 isInternallyChecked = item->wxMenuItemBase::IsChecked();
485
486 // ensure that the internal state is always consistent with what is
487 // shown on the screen
488 item->wxMenuItemBase::Check(isReallyChecked);
489
490 // we must not report the events for the radio button going up nor the
491 // events resulting from the calls to wxMenuItem::Check()
492 if ( (item->GetKind() == wxITEM_RADIO && !isReallyChecked) ||
493 (isInternallyChecked == isReallyChecked) )
494 {
495 return;
496 }
497 }
498
499
500 // Is this menu on a menubar? (possibly nested)
501 wxFrame* frame = NULL;
502 if(menu->IsAttached())
503 frame = menu->GetMenuBar()->GetFrame();
504
505 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
506 // normally wxMenu::SendEvent() should be enough, if it doesn't work
507 // in wxGTK then we have a bug in wxMenu::GetInvokingWindow() which
508 // should be fixed instead of working around it here...
509 if (frame)
510 {
511 // If it is attached then let the frame send the event.
512 // Don't call frame->ProcessCommand(id) because it toggles
513 // checkable items and we've already done that above.
514 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
515 commandEvent.SetEventObject(frame);
516 if (item->IsCheckable())
517 commandEvent.SetInt(item->IsChecked());
518
519 frame->HandleWindowEvent(commandEvent);
520 }
521 else
522 {
523 // otherwise let the menu have it
524 menu->SendEvent(id, item->IsCheckable() ? item->IsChecked() : -1);
525 }
526 }
527 }
528
529 //-----------------------------------------------------------------------------
530 // "select"
531 //-----------------------------------------------------------------------------
532
533 extern "C" {
534 static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu )
535 {
536 int id = menu->FindMenuIdByMenuItem(widget);
537
538 wxASSERT( id != -1 ); // should find it!
539
540 if (!menu->IsEnabled(id))
541 return;
542
543 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id );
544 event.SetEventObject( menu );
545
546 wxEvtHandler* handler = menu->GetEventHandler();
547 if (handler && handler->SafelyProcessEvent(event))
548 return;
549
550 wxWindow *win = menu->GetInvokingWindow();
551 if (win) win->HandleWindowEvent( event );
552 }
553 }
554
555 //-----------------------------------------------------------------------------
556 // "deselect"
557 //-----------------------------------------------------------------------------
558
559 extern "C" {
560 static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu )
561 {
562 int id = menu->FindMenuIdByMenuItem(widget);
563
564 wxASSERT( id != -1 ); // should find it!
565
566 if (!menu->IsEnabled(id))
567 return;
568
569 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 );
570 event.SetEventObject( menu );
571
572 wxEvtHandler* handler = menu->GetEventHandler();
573 if (handler && handler->SafelyProcessEvent(event))
574 return;
575
576 wxWindow *win = menu->GetInvokingWindow();
577 if (win)
578 win->HandleWindowEvent( event );
579 }
580 }
581
582 //-----------------------------------------------------------------------------
583 // wxMenuItem
584 //-----------------------------------------------------------------------------
585
586 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
587
588 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
589 int id,
590 const wxString& name,
591 const wxString& help,
592 wxItemKind kind,
593 wxMenu *subMenu)
594 {
595 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
596 }
597
598 wxMenuItem::wxMenuItem(wxMenu *parentMenu,
599 int id,
600 const wxString& text,
601 const wxString& help,
602 wxItemKind kind,
603 wxMenu *subMenu)
604 : wxMenuItemBase(parentMenu, id, text, help, kind, subMenu)
605 {
606 m_menuItem = NULL;
607 }
608
609 #if WXWIN_COMPATIBILITY_2_8
610 wxMenuItem::wxMenuItem(wxMenu *parentMenu,
611 int id,
612 const wxString& text,
613 const wxString& help,
614 bool isCheckable,
615 wxMenu *subMenu)
616 : wxMenuItemBase(parentMenu, id, text, help,
617 isCheckable ? wxITEM_CHECK : wxITEM_NORMAL, subMenu)
618 {
619 m_menuItem = NULL;
620 }
621 #endif
622
623 wxMenuItem::~wxMenuItem()
624 {
625 // don't delete menu items, the menus take care of that
626 }
627
628 // return the menu item text without any menu accels
629 /* static */
630
631 wxString wxMenuItemBase::GetLabelText(const wxString& text)
632 {
633 // The argument to this function will now always be in wxWidgets standard label
634 // format, not GTK+ format, so we do what the other ports do.
635
636 return wxStripMenuCodes(text);
637 }
638
639 void wxMenuItem::SetItemLabel( const wxString& str )
640 {
641 #if wxUSE_ACCEL
642 if (m_menuItem)
643 {
644 // remove old accelerator
645 guint accel_key;
646 GdkModifierType accel_mods;
647 wxGetGtkAccel(this, &accel_key, &accel_mods);
648 if (accel_key)
649 {
650 gtk_widget_remove_accelerator(
651 m_menuItem, m_parentMenu->m_accel, accel_key, accel_mods);
652 }
653 }
654 #endif // wxUSE_ACCEL
655 wxMenuItemBase::SetItemLabel(str);
656 if (m_menuItem)
657 SetGtkLabel();
658 }
659
660 void wxMenuItem::SetGtkLabel()
661 {
662 const wxString text = wxConvertMnemonicsToGTK(m_text.BeforeFirst('\t'));
663 GtkLabel* label = GTK_LABEL(GTK_BIN(m_menuItem)->child);
664 gtk_label_set_text_with_mnemonic(label, wxGTK_CONV_SYS(text));
665 #if wxUSE_ACCEL
666 guint accel_key;
667 GdkModifierType accel_mods;
668 wxGetGtkAccel(this, &accel_key, &accel_mods);
669 if (accel_key)
670 {
671 gtk_widget_add_accelerator(
672 m_menuItem, "activate", m_parentMenu->m_accel,
673 accel_key, accel_mods, GTK_ACCEL_VISIBLE);
674 }
675 #endif // wxUSE_ACCEL
676 }
677
678 void wxMenuItem::SetBitmap(const wxBitmap& bitmap)
679 {
680 if (m_kind == wxITEM_NORMAL)
681 m_bitmap = bitmap;
682 else
683 wxFAIL_MSG("only normal menu items can have bitmaps");
684 }
685
686 void wxMenuItem::Check( bool check )
687 {
688 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
689
690 if (check == m_isChecked)
691 return;
692
693 wxMenuItemBase::Check( check );
694
695 switch ( GetKind() )
696 {
697 case wxITEM_CHECK:
698 case wxITEM_RADIO:
699 gtk_check_menu_item_set_active( (GtkCheckMenuItem*)m_menuItem, (gint)check );
700 break;
701
702 default:
703 wxFAIL_MSG( _T("can't check this item") );
704 }
705 }
706
707 void wxMenuItem::Enable( bool enable )
708 {
709 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
710
711 gtk_widget_set_sensitive( m_menuItem, enable );
712 wxMenuItemBase::Enable( enable );
713 }
714
715 bool wxMenuItem::IsChecked() const
716 {
717 wxCHECK_MSG( m_menuItem, false, wxT("invalid menu item") );
718
719 wxCHECK_MSG( IsCheckable(), false,
720 wxT("can't get state of uncheckable item!") );
721
722 return ((GtkCheckMenuItem*)m_menuItem)->active != 0;
723 }
724
725 //-----------------------------------------------------------------------------
726 // wxMenu
727 //-----------------------------------------------------------------------------
728
729 extern "C" {
730 // "map" from m_menu
731 static void menu_map(GtkWidget*, wxMenu* menu)
732 {
733 wxMenuEvent event(wxEVT_MENU_OPEN, menu->m_popupShown ? -1 : 0, menu);
734 DoCommonMenuCallbackCode(menu, event);
735 }
736
737 // "hide" from m_menu
738 static void menu_hide(GtkWidget*, wxMenu* menu)
739 {
740 wxMenuEvent event(wxEVT_MENU_CLOSE, menu->m_popupShown ? -1 : 0, menu);
741 menu->m_popupShown = false;
742 DoCommonMenuCallbackCode(menu, event);
743 }
744 }
745
746 IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
747
748 void wxMenu::Init()
749 {
750 m_popupShown = false;
751
752 m_accel = gtk_accel_group_new();
753 m_menu = gtk_menu_new();
754 // NB: keep reference to the menu so that it is not destroyed behind
755 // our back by GTK+ e.g. when it is removed from menubar:
756 g_object_ref(m_menu);
757 gtk_object_sink(GTK_OBJECT(m_menu));
758
759 m_owner = (GtkWidget*) NULL;
760
761 // Tearoffs are entries, just like separators. So if we want this
762 // menu to be a tear-off one, we just append a tearoff entry
763 // immediately.
764 if ( m_style & wxMENU_TEAROFF )
765 {
766 GtkWidget *tearoff = gtk_tearoff_menu_item_new();
767
768 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu), tearoff);
769 }
770
771 m_prevRadio = NULL;
772
773 // append the title as the very first entry if we have it
774 if ( !m_title.empty() )
775 {
776 Append(wxGTK_TITLE_ID, m_title);
777 AppendSeparator();
778 }
779
780 // "show" occurs for sub-menus which are not showing, so use "map" instead
781 g_signal_connect(m_menu, "map", G_CALLBACK(menu_map), this);
782 g_signal_connect(m_menu, "hide", G_CALLBACK(menu_hide), this);
783 }
784
785 wxMenu::~wxMenu()
786 {
787 // see wxMenu::Init
788 g_object_unref(m_menu);
789
790 // if the menu is inserted in another menu at this time, there was
791 // one more reference to it:
792 if (m_owner)
793 gtk_widget_destroy(m_menu);
794
795 g_object_unref(m_accel);
796 }
797
798 void wxMenu::SetLayoutDirection(const wxLayoutDirection dir)
799 {
800 if ( m_owner )
801 wxWindow::GTKSetLayout(m_owner, dir);
802 //else: will be called later by wxMenuBar again
803 }
804
805 wxLayoutDirection wxMenu::GetLayoutDirection() const
806 {
807 return wxWindow::GTKGetLayout(m_owner);
808 }
809
810 bool wxMenu::GtkAppend(wxMenuItem *mitem, int pos)
811 {
812 GtkWidget *menuItem;
813 GtkWidget* prevRadio = m_prevRadio;
814 m_prevRadio = NULL;
815 switch (mitem->GetKind())
816 {
817 case wxITEM_SEPARATOR:
818 menuItem = gtk_separator_menu_item_new();
819 break;
820 case wxITEM_CHECK:
821 menuItem = gtk_check_menu_item_new_with_label("");
822 break;
823 case wxITEM_RADIO:
824 {
825 GSList* group = NULL;
826 if (prevRadio)
827 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(prevRadio));
828 menuItem = gtk_radio_menu_item_new_with_label(group, "");
829 m_prevRadio = menuItem;
830 }
831 break;
832 default:
833 wxFAIL_MSG("unexpected menu item kind");
834 // fall through
835 case wxITEM_NORMAL:
836 const wxBitmap& bitmap = mitem->GetBitmap();
837 const char* stockid;
838 if (bitmap.IsOk())
839 {
840 GtkWidget* image;
841 if (bitmap.HasPixbuf())
842 image = gtk_image_new_from_pixbuf(bitmap.GetPixbuf());
843 else
844 {
845 GdkPixmap* mask = NULL;
846 if (bitmap.GetMask())
847 mask = bitmap.GetMask()->GetBitmap();
848 image = gtk_image_new_from_pixmap(bitmap.GetPixmap(), mask);
849 }
850 menuItem = gtk_image_menu_item_new_with_label("");
851 gtk_widget_show(image);
852 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menuItem), image);
853 }
854 else if ((stockid = wxGetStockGtkID(mitem->GetId())) != NULL)
855 // use stock bitmap for this item if available on the assumption
856 // that it never hurts to follow GTK+ conventions more closely
857 menuItem = gtk_image_menu_item_new_from_stock(stockid, NULL);
858 else
859 menuItem = gtk_menu_item_new_with_label("");
860 break;
861 }
862 mitem->SetMenuItem(menuItem);
863
864 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu), menuItem, pos);
865
866 gtk_widget_show( menuItem );
867
868 if ( !mitem->IsSeparator() )
869 {
870 mitem->SetGtkLabel();
871 g_signal_connect (menuItem, "select",
872 G_CALLBACK (gtk_menu_hilight_callback), this);
873 g_signal_connect (menuItem, "deselect",
874 G_CALLBACK (gtk_menu_nolight_callback), this);
875
876 if ( mitem->IsSubMenu() && mitem->GetKind() != wxITEM_RADIO && mitem->GetKind() != wxITEM_CHECK )
877 {
878 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), mitem->GetSubMenu()->m_menu );
879
880 gtk_widget_show( mitem->GetSubMenu()->m_menu );
881
882 // if adding a submenu to a menu already existing in the menu bar, we
883 // must set invoking window to allow processing events from this
884 // submenu
885 if ( m_invokingWindow )
886 wxMenubarSetInvokingWindow(mitem->GetSubMenu(), m_invokingWindow);
887 }
888 else
889 {
890 g_signal_connect (menuItem, "activate",
891 G_CALLBACK (gtk_menu_clicked_callback),
892 this);
893 }
894 }
895
896 return true;
897 }
898
899 wxMenuItem* wxMenu::DoAppend(wxMenuItem *mitem)
900 {
901 if (!GtkAppend(mitem))
902 return NULL;
903
904 return wxMenuBase::DoAppend(mitem);
905 }
906
907 wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
908 {
909 if ( !wxMenuBase::DoInsert(pos, item) )
910 return NULL;
911
912 // TODO
913 if ( !GtkAppend(item, (int)pos) )
914 return NULL;
915
916 return item;
917 }
918
919 wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
920 {
921 if ( !wxMenuBase::DoRemove(item) )
922 return NULL;
923
924 GtkWidget * const mitem = item->GetMenuItem();
925 if ( m_prevRadio == mitem )
926 {
927 // deleting an item starts a new radio group (has to as we shouldn't
928 // keep a deleted pointer anyhow)
929 m_prevRadio = NULL;
930 }
931
932 gtk_menu_item_set_submenu(GTK_MENU_ITEM(mitem), NULL);
933 gtk_widget_destroy(mitem);
934 item->SetMenuItem(NULL);
935
936 return item;
937 }
938
939 int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
940 {
941 wxMenuItemList::compatibility_iterator node = m_items.GetFirst();
942 while (node)
943 {
944 wxMenuItem *item = node->GetData();
945 if (item->GetMenuItem() == menuItem)
946 return item->GetId();
947 node = node->GetNext();
948 }
949
950 return wxNOT_FOUND;
951 }
952
953 void wxMenu::Attach(wxMenuBarBase *menubar)
954 {
955 wxMenuBase::Attach(menubar);
956
957 // inherit layout direction from menubar.
958 SetLayoutDirection(menubar->GetLayoutDirection());
959 }
960
961 // ----------------------------------------------------------------------------
962 // helpers
963 // ----------------------------------------------------------------------------
964
965 #if wxUSE_ACCEL
966
967 static wxString GetGtkHotKey( const wxMenuItem& item )
968 {
969 wxString hotkey;
970
971 wxAcceleratorEntry *accel = item.GetAccel();
972 if ( accel )
973 {
974 int flags = accel->GetFlags();
975 if ( flags & wxACCEL_ALT )
976 hotkey += wxT("<alt>");
977 if ( flags & wxACCEL_CTRL )
978 hotkey += wxT("<control>");
979 if ( flags & wxACCEL_SHIFT )
980 hotkey += wxT("<shift>");
981
982 int code = accel->GetKeyCode();
983 switch ( code )
984 {
985 case WXK_F1:
986 case WXK_F2:
987 case WXK_F3:
988 case WXK_F4:
989 case WXK_F5:
990 case WXK_F6:
991 case WXK_F7:
992 case WXK_F8:
993 case WXK_F9:
994 case WXK_F10:
995 case WXK_F11:
996 case WXK_F12:
997 case WXK_F13:
998 case WXK_F14:
999 case WXK_F15:
1000 case WXK_F16:
1001 case WXK_F17:
1002 case WXK_F18:
1003 case WXK_F19:
1004 case WXK_F20:
1005 case WXK_F21:
1006 case WXK_F22:
1007 case WXK_F23:
1008 case WXK_F24:
1009 hotkey += wxString::Format(wxT("F%d"), code - WXK_F1 + 1);
1010 break;
1011
1012 // TODO: we should use gdk_keyval_name() (a.k.a.
1013 // XKeysymToString) here as well as hardcoding the keysym
1014 // names this might be not portable
1015 case WXK_INSERT:
1016 hotkey << wxT("Insert" );
1017 break;
1018 case WXK_DELETE:
1019 hotkey << wxT("Delete" );
1020 break;
1021 case WXK_UP:
1022 hotkey << wxT("Up" );
1023 break;
1024 case WXK_DOWN:
1025 hotkey << wxT("Down" );
1026 break;
1027 case WXK_PAGEUP:
1028 hotkey << wxT("Page_Up" );
1029 break;
1030 case WXK_PAGEDOWN:
1031 hotkey << wxT("Page_Down" );
1032 break;
1033 case WXK_LEFT:
1034 hotkey << wxT("Left" );
1035 break;
1036 case WXK_RIGHT:
1037 hotkey << wxT("Right" );
1038 break;
1039 case WXK_HOME:
1040 hotkey << wxT("Home" );
1041 break;
1042 case WXK_END:
1043 hotkey << wxT("End" );
1044 break;
1045 case WXK_RETURN:
1046 hotkey << wxT("Return" );
1047 break;
1048 case WXK_BACK:
1049 hotkey << wxT("BackSpace" );
1050 break;
1051 case WXK_TAB:
1052 hotkey << wxT("Tab" );
1053 break;
1054 case WXK_ESCAPE:
1055 hotkey << wxT("Esc" );
1056 break;
1057 case WXK_SPACE:
1058 hotkey << wxT("space" );
1059 break;
1060 case WXK_MULTIPLY:
1061 hotkey << wxT("Multiply" );
1062 break;
1063 case WXK_ADD:
1064 hotkey << wxT("Add" );
1065 break;
1066 case WXK_SEPARATOR:
1067 hotkey << wxT("Separator" );
1068 break;
1069 case WXK_SUBTRACT:
1070 hotkey << wxT("Subtract" );
1071 break;
1072 case WXK_DECIMAL:
1073 hotkey << wxT("Decimal" );
1074 break;
1075 case WXK_DIVIDE:
1076 hotkey << wxT("Divide" );
1077 break;
1078 case WXK_CANCEL:
1079 hotkey << wxT("Cancel" );
1080 break;
1081 case WXK_CLEAR:
1082 hotkey << wxT("Clear" );
1083 break;
1084 case WXK_MENU:
1085 hotkey << wxT("Menu" );
1086 break;
1087 case WXK_PAUSE:
1088 hotkey << wxT("Pause" );
1089 break;
1090 case WXK_CAPITAL:
1091 hotkey << wxT("Capital" );
1092 break;
1093 case WXK_SELECT:
1094 hotkey << wxT("Select" );
1095 break;
1096 case WXK_PRINT:
1097 hotkey << wxT("Print" );
1098 break;
1099 case WXK_EXECUTE:
1100 hotkey << wxT("Execute" );
1101 break;
1102 case WXK_SNAPSHOT:
1103 hotkey << wxT("Snapshot" );
1104 break;
1105 case WXK_HELP:
1106 hotkey << wxT("Help" );
1107 break;
1108 case WXK_NUMLOCK:
1109 hotkey << wxT("Num_Lock" );
1110 break;
1111 case WXK_SCROLL:
1112 hotkey << wxT("Scroll_Lock" );
1113 break;
1114 case WXK_NUMPAD_INSERT:
1115 hotkey << wxT("KP_Insert" );
1116 break;
1117 case WXK_NUMPAD_DELETE:
1118 hotkey << wxT("KP_Delete" );
1119 break;
1120 case WXK_NUMPAD_SPACE:
1121 hotkey << wxT("KP_Space" );
1122 break;
1123 case WXK_NUMPAD_TAB:
1124 hotkey << wxT("KP_Tab" );
1125 break;
1126 case WXK_NUMPAD_ENTER:
1127 hotkey << wxT("KP_Enter" );
1128 break;
1129 case WXK_NUMPAD_F1: case WXK_NUMPAD_F2: case WXK_NUMPAD_F3:
1130 case WXK_NUMPAD_F4:
1131 hotkey += wxString::Format(wxT("KP_F%d"), code - WXK_NUMPAD_F1 + 1);
1132 break;
1133 case WXK_NUMPAD_HOME:
1134 hotkey << wxT("KP_Home" );
1135 break;
1136 case WXK_NUMPAD_LEFT:
1137 hotkey << wxT("KP_Left" );
1138 break;
1139 case WXK_NUMPAD_UP:
1140 hotkey << wxT("KP_Up" );
1141 break;
1142 case WXK_NUMPAD_RIGHT:
1143 hotkey << wxT("KP_Right" );
1144 break;
1145 case WXK_NUMPAD_DOWN:
1146 hotkey << wxT("KP_Down" );
1147 break;
1148 case WXK_NUMPAD_PAGEUP:
1149 hotkey << wxT("KP_Page_Up" );
1150 break;
1151 case WXK_NUMPAD_PAGEDOWN:
1152 hotkey << wxT("KP_Page_Down" );
1153 break;
1154 case WXK_NUMPAD_END:
1155 hotkey << wxT("KP_End" );
1156 break;
1157 case WXK_NUMPAD_BEGIN:
1158 hotkey << wxT("KP_Begin" );
1159 break;
1160 case WXK_NUMPAD_EQUAL:
1161 hotkey << wxT("KP_Equal" );
1162 break;
1163 case WXK_NUMPAD_MULTIPLY:
1164 hotkey << wxT("KP_Multiply" );
1165 break;
1166 case WXK_NUMPAD_ADD:
1167 hotkey << wxT("KP_Add" );
1168 break;
1169 case WXK_NUMPAD_SEPARATOR:
1170 hotkey << wxT("KP_Separator" );
1171 break;
1172 case WXK_NUMPAD_SUBTRACT:
1173 hotkey << wxT("KP_Subtract" );
1174 break;
1175 case WXK_NUMPAD_DECIMAL:
1176 hotkey << wxT("KP_Decimal" );
1177 break;
1178 case WXK_NUMPAD_DIVIDE:
1179 hotkey << wxT("KP_Divide" );
1180 break;
1181 case WXK_NUMPAD0: case WXK_NUMPAD1: case WXK_NUMPAD2:
1182 case WXK_NUMPAD3: case WXK_NUMPAD4: case WXK_NUMPAD5:
1183 case WXK_NUMPAD6: case WXK_NUMPAD7: case WXK_NUMPAD8: case WXK_NUMPAD9:
1184 hotkey += wxString::Format(wxT("KP_%d"), code - WXK_NUMPAD0);
1185 break;
1186 case WXK_WINDOWS_LEFT:
1187 hotkey << wxT("Super_L" );
1188 break;
1189 case WXK_WINDOWS_RIGHT:
1190 hotkey << wxT("Super_R" );
1191 break;
1192 case WXK_WINDOWS_MENU:
1193 hotkey << wxT("Menu" );
1194 break;
1195 case WXK_COMMAND:
1196 hotkey << wxT("Command" );
1197 break;
1198 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
1199 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1200 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1201 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1202 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
1203 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1204 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1205 break;
1206 */
1207 // if there are any other keys wxAcceleratorEntry::Create() may
1208 // return, we should process them here
1209
1210 default:
1211 if ( code < 127 )
1212 {
1213 const wxString
1214 name = wxGTK_CONV_BACK_SYS(gdk_keyval_name((guint)code));
1215 if ( !name.empty() )
1216 {
1217 hotkey << name;
1218 break;
1219 }
1220 }
1221
1222 wxFAIL_MSG( wxT("unknown keyboard accel") );
1223 }
1224
1225 delete accel;
1226 }
1227
1228 return hotkey;
1229 }
1230
1231 static void
1232 wxGetGtkAccel(const wxMenuItem* item, guint* accel_key, GdkModifierType* accel_mods)
1233 {
1234 *accel_key = 0;
1235 const wxString string = GetGtkHotKey(*item);
1236 if (!string.empty())
1237 gtk_accelerator_parse(wxGTK_CONV_SYS(string), accel_key, accel_mods);
1238 else
1239 {
1240 GtkStockItem stock_item;
1241 const char* stockid = wxGetStockGtkID(item->GetId());
1242 if (stockid && gtk_stock_lookup(stockid, &stock_item))
1243 {
1244 *accel_key = stock_item.keyval;
1245 *accel_mods = stock_item.modifier;
1246 }
1247 }
1248 }
1249 #endif // wxUSE_ACCEL
1250
1251 const char *wxGetStockGtkID(wxWindowID id)
1252 {
1253 #define STOCKITEM(wx,gtk) \
1254 case wx: \
1255 return gtk;
1256
1257 #if GTK_CHECK_VERSION(2,6,0)
1258 #define STOCKITEM_26(wx,gtk) STOCKITEM(wx,gtk)
1259 #else
1260 #define STOCKITEM_26(wx,gtk)
1261 #endif
1262
1263 #if GTK_CHECK_VERSION(2,10,0)
1264 #define STOCKITEM_210(wx,gtk) STOCKITEM(wx,gtk)
1265 #else
1266 #define STOCKITEM_210(wx,gtk)
1267 #endif
1268
1269
1270 switch (id)
1271 {
1272 STOCKITEM_26(wxID_ABOUT, GTK_STOCK_ABOUT)
1273 STOCKITEM(wxID_ADD, GTK_STOCK_ADD)
1274 STOCKITEM(wxID_APPLY, GTK_STOCK_APPLY)
1275 STOCKITEM(wxID_BOLD, GTK_STOCK_BOLD)
1276 STOCKITEM(wxID_CANCEL, GTK_STOCK_CANCEL)
1277 STOCKITEM(wxID_CLEAR, GTK_STOCK_CLEAR)
1278 STOCKITEM(wxID_CLOSE, GTK_STOCK_CLOSE)
1279 STOCKITEM(wxID_COPY, GTK_STOCK_COPY)
1280 STOCKITEM(wxID_CUT, GTK_STOCK_CUT)
1281 STOCKITEM(wxID_DELETE, GTK_STOCK_DELETE)
1282 STOCKITEM_26(wxID_EDIT, GTK_STOCK_EDIT)
1283 STOCKITEM(wxID_FIND, GTK_STOCK_FIND)
1284 STOCKITEM_26(wxID_FILE, GTK_STOCK_FILE)
1285 STOCKITEM(wxID_REPLACE, GTK_STOCK_FIND_AND_REPLACE)
1286 STOCKITEM(wxID_BACKWARD, GTK_STOCK_GO_BACK)
1287 STOCKITEM(wxID_DOWN, GTK_STOCK_GO_DOWN)
1288 STOCKITEM(wxID_FORWARD, GTK_STOCK_GO_FORWARD)
1289 STOCKITEM(wxID_UP, GTK_STOCK_GO_UP)
1290 STOCKITEM(wxID_HELP, GTK_STOCK_HELP)
1291 STOCKITEM(wxID_HOME, GTK_STOCK_HOME)
1292 STOCKITEM(wxID_INDENT, GTK_STOCK_INDENT)
1293 STOCKITEM(wxID_INDEX, GTK_STOCK_INDEX)
1294 STOCKITEM(wxID_ITALIC, GTK_STOCK_ITALIC)
1295 STOCKITEM(wxID_JUSTIFY_CENTER, GTK_STOCK_JUSTIFY_CENTER)
1296 STOCKITEM(wxID_JUSTIFY_FILL, GTK_STOCK_JUSTIFY_FILL)
1297 STOCKITEM(wxID_JUSTIFY_LEFT, GTK_STOCK_JUSTIFY_LEFT)
1298 STOCKITEM(wxID_JUSTIFY_RIGHT, GTK_STOCK_JUSTIFY_RIGHT)
1299 STOCKITEM(wxID_NEW, GTK_STOCK_NEW)
1300 STOCKITEM(wxID_NO, GTK_STOCK_NO)
1301 STOCKITEM(wxID_OK, GTK_STOCK_OK)
1302 STOCKITEM(wxID_OPEN, GTK_STOCK_OPEN)
1303 STOCKITEM(wxID_PASTE, GTK_STOCK_PASTE)
1304 STOCKITEM(wxID_PREFERENCES, GTK_STOCK_PREFERENCES)
1305 STOCKITEM(wxID_PRINT, GTK_STOCK_PRINT)
1306 STOCKITEM(wxID_PREVIEW, GTK_STOCK_PRINT_PREVIEW)
1307 STOCKITEM(wxID_PROPERTIES, GTK_STOCK_PROPERTIES)
1308 STOCKITEM(wxID_EXIT, GTK_STOCK_QUIT)
1309 STOCKITEM(wxID_REDO, GTK_STOCK_REDO)
1310 STOCKITEM(wxID_REFRESH, GTK_STOCK_REFRESH)
1311 STOCKITEM(wxID_REMOVE, GTK_STOCK_REMOVE)
1312 STOCKITEM(wxID_REVERT_TO_SAVED, GTK_STOCK_REVERT_TO_SAVED)
1313 STOCKITEM(wxID_SAVE, GTK_STOCK_SAVE)
1314 STOCKITEM(wxID_SAVEAS, GTK_STOCK_SAVE_AS)
1315 STOCKITEM_210(wxID_SELECTALL, GTK_STOCK_SELECT_ALL)
1316 STOCKITEM(wxID_STOP, GTK_STOCK_STOP)
1317 STOCKITEM(wxID_UNDELETE, GTK_STOCK_UNDELETE)
1318 STOCKITEM(wxID_UNDERLINE, GTK_STOCK_UNDERLINE)
1319 STOCKITEM(wxID_UNDO, GTK_STOCK_UNDO)
1320 STOCKITEM(wxID_UNINDENT, GTK_STOCK_UNINDENT)
1321 STOCKITEM(wxID_YES, GTK_STOCK_YES)
1322 STOCKITEM(wxID_ZOOM_100, GTK_STOCK_ZOOM_100)
1323 STOCKITEM(wxID_ZOOM_FIT, GTK_STOCK_ZOOM_FIT)
1324 STOCKITEM(wxID_ZOOM_IN, GTK_STOCK_ZOOM_IN)
1325 STOCKITEM(wxID_ZOOM_OUT, GTK_STOCK_ZOOM_OUT)
1326
1327 default:
1328 break;
1329 };
1330
1331 #undef STOCKITEM
1332
1333 return NULL;
1334 }
1335
1336 #endif // wxUSE_MENUS