use (new) wxAcceleratorEntry::Create() instead of recently deprecated wxGetAccelFromS...
[wxWidgets.git] / src / gtk / menu.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/menu.cpp
3 // Purpose:
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 #include "wx/menu.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/intl.h"
17 #include "wx/log.h"
18 #include "wx/frame.h"
19 #include "wx/bitmap.h"
20 #endif
21
22 #include "wx/accel.h"
23 #include "wx/stockitem.h"
24 #include "wx/gtk/private.h"
25
26 // FIXME: is this right? somehow I don't think so (VZ)
27
28 #define gtk_accel_group_attach(g, o) gtk_window_add_accel_group((o), (g))
29 //#define gtk_accel_group_detach(g, o) gtk_window_remove_accel_group((o), (g))
30 //#define gtk_menu_ensure_uline_accel_group(m) gtk_menu_get_accel_group(m)
31
32 #define ACCEL_OBJECT GtkWindow
33 #define ACCEL_OBJECTS(a) (a)->acceleratables
34 #define ACCEL_OBJ_CAST(obj) ((GtkWindow*) obj)
35
36 // we use normal item but with a special id for the menu title
37 static const int wxGTK_TITLE_ID = -3;
38
39 //-----------------------------------------------------------------------------
40 // idle system
41 //-----------------------------------------------------------------------------
42
43 #if wxUSE_ACCEL
44 static wxString GetGtkHotKey( const wxMenuItem& item );
45 #endif
46
47 //-----------------------------------------------------------------------------
48 // idle system
49 //-----------------------------------------------------------------------------
50
51 static wxString wxReplaceUnderscore( const wxString& title )
52 {
53 const wxChar *pc;
54
55 // GTK 1.2 wants to have "_" instead of "&" for accelerators
56 wxString str;
57 pc = title;
58 while (*pc != wxT('\0'))
59 {
60 if ((*pc == wxT('&')) && (*(pc+1) == wxT('&')))
61 {
62 // "&" is doubled to indicate "&" instead of accelerator
63 ++pc;
64 str << wxT('&');
65 }
66 else if (*pc == wxT('&'))
67 {
68 str << wxT('_');
69 }
70 else
71 {
72 if ( *pc == wxT('_') )
73 {
74 // underscores must be doubled to prevent them from being
75 // interpreted as accelerator character prefix by GTK
76 str << *pc;
77 }
78
79 str << *pc;
80 }
81 ++pc;
82 }
83
84 // wxPrintf( wxT("before %s after %s\n"), title.c_str(), str.c_str() );
85
86 return str;
87 }
88
89 //-----------------------------------------------------------------------------
90 // activate message from GTK
91 //-----------------------------------------------------------------------------
92
93 static void DoCommonMenuCallbackCode(wxMenu *menu, wxMenuEvent& event)
94 {
95 if (g_isIdle)
96 wxapp_install_idle_handler();
97
98 event.SetEventObject( menu );
99
100 wxEvtHandler* handler = menu->GetEventHandler();
101 if (handler && handler->ProcessEvent(event))
102 return;
103
104 wxWindow *win = menu->GetInvokingWindow();
105 if (win)
106 win->GetEventHandler()->ProcessEvent( event );
107 }
108
109 extern "C" {
110
111 static void gtk_menu_open_callback( GtkWidget *widget, wxMenu *menu )
112 {
113 wxMenuEvent event(wxEVT_MENU_OPEN, -1, menu);
114
115 DoCommonMenuCallbackCode(menu, event);
116 }
117
118 static void gtk_menu_close_callback( GtkWidget *widget, wxMenuBar *menubar )
119 {
120 if ( !menubar->GetMenuCount() )
121 {
122 // if menubar is empty we can't call GetMenu(0) below
123 return;
124 }
125
126 wxMenuEvent event( wxEVT_MENU_CLOSE, -1, NULL );
127
128 DoCommonMenuCallbackCode(menubar->GetMenu(0), event);
129 }
130
131 }
132
133 //-----------------------------------------------------------------------------
134 // wxMenuBar
135 //-----------------------------------------------------------------------------
136
137 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
138
139 void wxMenuBar::Init(size_t n, wxMenu *menus[], const wxString titles[], long style)
140 {
141 // the parent window is known after wxFrame::SetMenu()
142 m_needParent = false;
143 m_style = style;
144 m_invokingWindow = (wxWindow*) NULL;
145
146 if (!PreCreation( (wxWindow*) NULL, wxDefaultPosition, wxDefaultSize ) ||
147 !CreateBase( (wxWindow*) NULL, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, wxT("menubar") ))
148 {
149 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
150 return;
151 }
152
153 m_menubar = gtk_menu_bar_new();
154
155 if (style & wxMB_DOCKABLE)
156 {
157 m_widget = gtk_handle_box_new();
158 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_menubar) );
159 gtk_widget_show( GTK_WIDGET(m_menubar) );
160 }
161 else
162 {
163 m_widget = GTK_WIDGET(m_menubar);
164 }
165
166 PostCreation();
167
168 ApplyWidgetStyle();
169
170 for (size_t i = 0; i < n; ++i )
171 Append(menus[i], titles[i]);
172
173 // VZ: for some reason connecting to menus "deactivate" doesn't work (we
174 // don't get it when the menu is dismissed by clicking outside the
175 // toolbar) so we connect to the global one, even if it means that we
176 // can't pass the menu which was closed in wxMenuEvent object
177 g_signal_connect (m_menubar, "deactivate",
178 G_CALLBACK (gtk_menu_close_callback), this);
179
180 }
181
182 wxMenuBar::wxMenuBar(size_t n, wxMenu *menus[], const wxString titles[], long style)
183 {
184 Init(n, menus, titles, style);
185 }
186
187 wxMenuBar::wxMenuBar(long style)
188 {
189 Init(0, NULL, NULL, style);
190 }
191
192 wxMenuBar::wxMenuBar()
193 {
194 Init(0, NULL, NULL, 0);
195 }
196
197 wxMenuBar::~wxMenuBar()
198 {
199 }
200
201 static void wxMenubarUnsetInvokingWindow( wxMenu *menu, wxWindow *win )
202 {
203 menu->SetInvokingWindow( (wxWindow*) NULL );
204
205 wxWindow *top_frame = win;
206 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
207 top_frame = top_frame->GetParent();
208
209 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
210 while (node)
211 {
212 wxMenuItem *menuitem = node->GetData();
213 if (menuitem->IsSubMenu())
214 wxMenubarUnsetInvokingWindow( menuitem->GetSubMenu(), win );
215 node = node->GetNext();
216 }
217 }
218
219 static void wxMenubarSetInvokingWindow( wxMenu *menu, wxWindow *win )
220 {
221 menu->SetInvokingWindow( win );
222
223 wxWindow *top_frame = win;
224 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
225 top_frame = top_frame->GetParent();
226
227 // support for native hot keys
228 ACCEL_OBJECT *obj = ACCEL_OBJ_CAST(top_frame->m_widget);
229 if ( !g_slist_find( ACCEL_OBJECTS(menu->m_accel), obj ) )
230 gtk_accel_group_attach( menu->m_accel, obj );
231
232 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
233 while (node)
234 {
235 wxMenuItem *menuitem = node->GetData();
236 if (menuitem->IsSubMenu())
237 wxMenubarSetInvokingWindow( menuitem->GetSubMenu(), win );
238 node = node->GetNext();
239 }
240 }
241
242 void wxMenuBar::SetInvokingWindow( wxWindow *win )
243 {
244 m_invokingWindow = win;
245 wxWindow *top_frame = win;
246 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
247 top_frame = top_frame->GetParent();
248
249 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
250 while (node)
251 {
252 wxMenu *menu = node->GetData();
253 wxMenubarSetInvokingWindow( menu, win );
254 node = node->GetNext();
255 }
256 }
257
258 void wxMenuBar::SetLayoutDirection(wxLayoutDirection dir)
259 {
260 if ( dir == wxLayout_Default )
261 {
262 const wxWindow *const frame = GetFrame();
263 if ( frame )
264 {
265 // inherit layout from frame.
266 dir = frame->GetLayoutDirection();
267 }
268 else // use global layout
269 {
270 dir = wxTheApp->GetLayoutDirection();
271 }
272 }
273
274 if ( dir == wxLayout_Default )
275 return;
276
277 GTKSetLayout(m_menubar, dir);
278
279 // also set the layout of all menus we already have (new ones will inherit
280 // the current layout)
281 for ( wxMenuList::compatibility_iterator node = m_menus.GetFirst();
282 node;
283 node = node->GetNext() )
284 {
285 wxMenu *const menu = node->GetData();
286 menu->SetLayoutDirection(dir);
287 }
288 }
289
290 wxLayoutDirection wxMenuBar::GetLayoutDirection() const
291 {
292 return GTKGetLayout(m_menubar);
293 }
294
295 void wxMenuBar::Attach(wxFrame *frame)
296 {
297 wxMenuBarBase::Attach(frame);
298
299 SetLayoutDirection(wxLayout_Default);
300 }
301
302 void wxMenuBar::UnsetInvokingWindow( wxWindow *win )
303 {
304 m_invokingWindow = (wxWindow*) NULL;
305 wxWindow *top_frame = win;
306 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
307 top_frame = top_frame->GetParent();
308
309 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
310 while (node)
311 {
312 wxMenu *menu = node->GetData();
313 wxMenubarUnsetInvokingWindow( menu, win );
314 node = node->GetNext();
315 }
316 }
317
318 bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
319 {
320 if ( !wxMenuBarBase::Append( menu, title ) )
321 return false;
322
323 return GtkAppend(menu, title);
324 }
325
326 bool wxMenuBar::GtkAppend(wxMenu *menu, const wxString& title, int pos)
327 {
328 wxString str( wxReplaceUnderscore( title ) );
329
330 // This doesn't have much effect right now.
331 menu->SetTitle( str );
332
333 // The "m_owner" is the "menu item"
334 menu->m_owner = gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str ) );
335 menu->SetLayoutDirection(GetLayoutDirection());
336
337 gtk_widget_show( menu->m_owner );
338
339 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu );
340
341 if (pos == -1)
342 gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar), menu->m_owner );
343 else
344 gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar), menu->m_owner, pos );
345
346 g_signal_connect (menu->m_owner, "activate",
347 G_CALLBACK (gtk_menu_open_callback),
348 menu);
349
350 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
351 // addings menu later on.
352 if (m_invokingWindow)
353 {
354 wxMenubarSetInvokingWindow( menu, m_invokingWindow );
355
356 // OPTIMISE ME: we should probably cache this, or pass it
357 // directly, but for now this is a minimal
358 // change to validate the new dynamic sizing.
359 // see (and refactor :) similar code in Remove
360 // below.
361
362 wxFrame *frame = wxDynamicCast( m_invokingWindow, wxFrame );
363
364 if( frame )
365 frame->UpdateMenuBarSize();
366 }
367
368 return true;
369 }
370
371 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
372 {
373 if ( !wxMenuBarBase::Insert(pos, menu, title) )
374 return false;
375
376 // TODO
377
378 if ( !GtkAppend(menu, title, (int)pos) )
379 return false;
380
381 return true;
382 }
383
384 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
385 {
386 // remove the old item and insert a new one
387 wxMenu *menuOld = Remove(pos);
388 if ( menuOld && !Insert(pos, menu, title) )
389 {
390 return (wxMenu*) NULL;
391 }
392
393 // either Insert() succeeded or Remove() failed and menuOld is NULL
394 return menuOld;
395 }
396
397 wxMenu *wxMenuBar::Remove(size_t pos)
398 {
399 wxMenu *menu = wxMenuBarBase::Remove(pos);
400 if ( !menu )
401 return (wxMenu*) NULL;
402
403 gtk_menu_item_remove_submenu( GTK_MENU_ITEM(menu->m_owner) );
404 gtk_container_remove(GTK_CONTAINER(m_menubar), menu->m_owner);
405
406 gtk_widget_destroy( menu->m_owner );
407 menu->m_owner = NULL;
408
409 if (m_invokingWindow)
410 {
411 // OPTIMISE ME: see comment in GtkAppend
412 wxFrame *frame = wxDynamicCast( m_invokingWindow, wxFrame );
413
414 if( frame )
415 frame->UpdateMenuBarSize();
416 }
417
418 return menu;
419 }
420
421 static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString )
422 {
423 if (wxMenuItem::GetLabelFromText(menu->GetTitle()) == wxMenuItem::GetLabelFromText(menuString))
424 {
425 int res = menu->FindItem( itemString );
426 if (res != wxNOT_FOUND)
427 return res;
428 }
429
430 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
431 while (node)
432 {
433 wxMenuItem *item = node->GetData();
434 if (item->IsSubMenu())
435 return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString);
436
437 node = node->GetNext();
438 }
439
440 return wxNOT_FOUND;
441 }
442
443 int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const
444 {
445 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
446 while (node)
447 {
448 wxMenu *menu = node->GetData();
449 int res = FindMenuItemRecursive( menu, menuString, itemString);
450 if (res != -1)
451 return res;
452 node = node->GetNext();
453 }
454
455 return wxNOT_FOUND;
456 }
457
458 // Find a wxMenuItem using its id. Recurses down into sub-menus
459 static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id)
460 {
461 wxMenuItem* result = menu->FindChildItem(id);
462
463 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
464 while ( node && result == NULL )
465 {
466 wxMenuItem *item = node->GetData();
467 if (item->IsSubMenu())
468 {
469 result = FindMenuItemByIdRecursive( item->GetSubMenu(), id );
470 }
471 node = node->GetNext();
472 }
473
474 return result;
475 }
476
477 wxMenuItem* wxMenuBar::FindItem( int id, wxMenu **menuForItem ) const
478 {
479 wxMenuItem* result = 0;
480 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
481 while (node && result == 0)
482 {
483 wxMenu *menu = node->GetData();
484 result = FindMenuItemByIdRecursive( menu, id );
485 node = node->GetNext();
486 }
487
488 if ( menuForItem )
489 {
490 *menuForItem = result ? result->GetMenu() : (wxMenu *)NULL;
491 }
492
493 return result;
494 }
495
496 void wxMenuBar::EnableTop( size_t pos, bool flag )
497 {
498 wxMenuList::compatibility_iterator node = m_menus.Item( pos );
499
500 wxCHECK_RET( node, wxT("menu not found") );
501
502 wxMenu* menu = node->GetData();
503
504 if (menu->m_owner)
505 gtk_widget_set_sensitive( menu->m_owner, flag );
506 }
507
508 wxString wxMenuBar::GetLabelTop( size_t pos ) const
509 {
510 wxMenuList::compatibility_iterator node = m_menus.Item( pos );
511
512 wxCHECK_MSG( node, wxT("invalid"), wxT("menu not found") );
513
514 wxMenu* menu = node->GetData();
515
516 wxString label;
517 wxString text( menu->GetTitle() );
518 for ( const wxChar *pc = text.c_str(); *pc; pc++ )
519 {
520 if ( *pc == wxT('_') )
521 {
522 // '_' is the escape character for GTK+
523 continue;
524 }
525
526 // don't remove ampersands '&' since if we have them in the menu title
527 // it means that they were doubled to indicate "&" instead of accelerator
528
529 label += *pc;
530 }
531
532 return label;
533 }
534
535 void wxMenuBar::SetLabelTop( size_t pos, const wxString& label )
536 {
537 wxMenuList::compatibility_iterator node = m_menus.Item( pos );
538
539 wxCHECK_RET( node, wxT("menu not found") );
540
541 wxMenu* menu = node->GetData();
542
543 const wxString str( wxReplaceUnderscore( label ) );
544
545 menu->SetTitle( str );
546
547 if (menu->m_owner)
548 gtk_label_set_text_with_mnemonic( GTK_LABEL( GTK_BIN(menu->m_owner)->child), wxGTK_CONV(str) );
549 }
550
551 //-----------------------------------------------------------------------------
552 // "activate"
553 //-----------------------------------------------------------------------------
554
555 extern "C" {
556 static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu )
557 {
558 if (g_isIdle)
559 wxapp_install_idle_handler();
560
561 int id = menu->FindMenuIdByMenuItem(widget);
562
563 /* should find it for normal (not popup) menu */
564 wxASSERT_MSG( (id != -1) || (menu->GetInvokingWindow() != NULL),
565 _T("menu item not found in gtk_menu_clicked_callback") );
566
567 if (!menu->IsEnabled(id))
568 return;
569
570 wxMenuItem* item = menu->FindChildItem( id );
571 wxCHECK_RET( item, wxT("error in menu item callback") );
572
573 if ( item->GetId() == wxGTK_TITLE_ID )
574 {
575 // ignore events from the menu title
576 return;
577 }
578
579 if (item->IsCheckable())
580 {
581 bool isReallyChecked = item->IsChecked(),
582 isInternallyChecked = item->wxMenuItemBase::IsChecked();
583
584 // ensure that the internal state is always consistent with what is
585 // shown on the screen
586 item->wxMenuItemBase::Check(isReallyChecked);
587
588 // we must not report the events for the radio button going up nor the
589 // events resulting from the calls to wxMenuItem::Check()
590 if ( (item->GetKind() == wxITEM_RADIO && !isReallyChecked) ||
591 (isInternallyChecked == isReallyChecked) )
592 {
593 return;
594 }
595 }
596
597
598 // Is this menu on a menubar? (possibly nested)
599 wxFrame* frame = NULL;
600 if(menu->IsAttached())
601 frame = menu->GetMenuBar()->GetFrame();
602
603 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
604 // normally wxMenu::SendEvent() should be enough, if it doesn't work
605 // in wxGTK then we have a bug in wxMenu::GetInvokingWindow() which
606 // should be fixed instead of working around it here...
607 if (frame)
608 {
609 // If it is attached then let the frame send the event.
610 // Don't call frame->ProcessCommand(id) because it toggles
611 // checkable items and we've already done that above.
612 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
613 commandEvent.SetEventObject(frame);
614 if (item->IsCheckable())
615 commandEvent.SetInt(item->IsChecked());
616 commandEvent.SetEventObject(menu);
617
618 frame->GetEventHandler()->ProcessEvent(commandEvent);
619 }
620 else
621 {
622 // otherwise let the menu have it
623 menu->SendEvent(id, item->IsCheckable() ? item->IsChecked() : -1);
624 }
625 }
626 }
627
628 //-----------------------------------------------------------------------------
629 // "select"
630 //-----------------------------------------------------------------------------
631
632 extern "C" {
633 static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu )
634 {
635 if (g_isIdle) wxapp_install_idle_handler();
636
637 int id = menu->FindMenuIdByMenuItem(widget);
638
639 wxASSERT( id != -1 ); // should find it!
640
641 if (!menu->IsEnabled(id))
642 return;
643
644 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id );
645 event.SetEventObject( menu );
646
647 wxEvtHandler* handler = menu->GetEventHandler();
648 if (handler && handler->ProcessEvent(event))
649 return;
650
651 wxWindow *win = menu->GetInvokingWindow();
652 if (win) win->GetEventHandler()->ProcessEvent( event );
653 }
654 }
655
656 //-----------------------------------------------------------------------------
657 // "deselect"
658 //-----------------------------------------------------------------------------
659
660 extern "C" {
661 static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu )
662 {
663 if (g_isIdle) wxapp_install_idle_handler();
664
665 int id = menu->FindMenuIdByMenuItem(widget);
666
667 wxASSERT( id != -1 ); // should find it!
668
669 if (!menu->IsEnabled(id))
670 return;
671
672 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 );
673 event.SetEventObject( menu );
674
675 wxEvtHandler* handler = menu->GetEventHandler();
676 if (handler && handler->ProcessEvent(event))
677 return;
678
679 wxWindow *win = menu->GetInvokingWindow();
680 if (win)
681 win->GetEventHandler()->ProcessEvent( event );
682 }
683 }
684
685 //-----------------------------------------------------------------------------
686 // wxMenuItem
687 //-----------------------------------------------------------------------------
688
689 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
690
691 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
692 int id,
693 const wxString& name,
694 const wxString& help,
695 wxItemKind kind,
696 wxMenu *subMenu)
697 {
698 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
699 }
700
701 wxMenuItem::wxMenuItem(wxMenu *parentMenu,
702 int id,
703 const wxString& text,
704 const wxString& help,
705 wxItemKind kind,
706 wxMenu *subMenu)
707 : wxMenuItemBase(parentMenu, id, text, help, kind, subMenu)
708 {
709 Init(text);
710 }
711
712 wxMenuItem::wxMenuItem(wxMenu *parentMenu,
713 int id,
714 const wxString& text,
715 const wxString& help,
716 bool isCheckable,
717 wxMenu *subMenu)
718 : wxMenuItemBase(parentMenu, id, text, help,
719 isCheckable ? wxITEM_CHECK : wxITEM_NORMAL, subMenu)
720 {
721 Init(text);
722 }
723
724 void wxMenuItem::Init(const wxString& text)
725 {
726 m_labelWidget = (GtkWidget *) NULL;
727 m_menuItem = (GtkWidget *) NULL;
728
729 DoSetText(text);
730 }
731
732 wxMenuItem::~wxMenuItem()
733 {
734 // don't delete menu items, the menus take care of that
735 }
736
737 // return the menu item text without any menu accels
738 /* static */
739 wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
740 {
741 wxString label;
742
743 for ( const wxChar *pc = text.c_str(); *pc; pc++ )
744 {
745 if ( *pc == wxT('\t'))
746 break;
747
748 if ( *pc == wxT('_') )
749 {
750 // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx"
751 pc++;
752 label += *pc;
753 continue;
754 }
755
756 if ( *pc == wxT('\\') )
757 {
758 // GTK 2.0 escapes "xxx/xxx" to "xxx\/xxx"
759 pc++;
760 label += *pc;
761 continue;
762 }
763
764 if ( (*pc == wxT('&')) && (*(pc+1) != wxT('&')) )
765 {
766 // wxMSW escapes "&"
767 // "&" is doubled to indicate "&" instead of accelerator
768 continue;
769 }
770
771 label += *pc;
772 }
773
774 // wxPrintf( wxT("GetLabelFromText(): text %s label %s\n"), text.c_str(), label.c_str() );
775
776 return label;
777 }
778
779 void wxMenuItem::SetText( const wxString& str )
780 {
781 // cache some data which must be used later
782 bool isstock = wxIsStockID(GetId());
783 const char *stockid = NULL;
784 if (isstock)
785 stockid = wxGetStockGtkID(GetId());
786
787 // Some optimization to avoid flicker
788 wxString oldLabel = m_text;
789 oldLabel = wxStripMenuCodes(oldLabel);
790 oldLabel.Replace(wxT("_"), wxT(""));
791 wxString label1 = wxStripMenuCodes(str);
792 wxString oldhotkey = GetHotKey(); // Store the old hotkey in Ctrl-foo format
793 wxCharBuffer oldbuf = wxGTK_CONV_SYS( GetGtkHotKey(*this) ); // and as <control>foo
794
795 DoSetText(str);
796
797 if (oldLabel == label1 &&
798 oldhotkey == GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
799 return;
800
801 if (m_menuItem)
802 {
803 GtkLabel *label;
804 if (m_labelWidget)
805 label = (GtkLabel*) m_labelWidget;
806 else
807 label = GTK_LABEL( GTK_BIN(m_menuItem)->child );
808
809 // stock menu items can have empty labels:
810 wxString text = m_text;
811 if (text.IsEmpty() && !IsSeparator())
812 {
813 wxASSERT_MSG(isstock, wxT("A non-stock menu item with an empty label?"));
814 text = wxGetStockLabel(GetId());
815
816 // need & => _ conversion
817 text = GTKProcessMenuItemLabel(text, NULL);
818 }
819
820 gtk_label_set_text_with_mnemonic( GTK_LABEL(label), wxGTK_CONV_SYS(text) );
821 }
822
823 // remove old accelerator from our parent's accelerator group, if present
824 guint accel_key;
825 GdkModifierType accel_mods;
826 if (oldbuf[(size_t)0] != '\0')
827 {
828 gtk_accelerator_parse( (const char*) oldbuf, &accel_key, &accel_mods);
829 if (accel_key != 0)
830 {
831 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem),
832 m_parentMenu->m_accel,
833 accel_key,
834 accel_mods );
835 }
836 }
837 else if (isstock)
838 {
839 // if the accelerator was taken from a stock ID, just get it back from GTK+ stock
840 if (wxGetStockGtkAccelerator(stockid, &accel_mods, &accel_key))
841 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem),
842 m_parentMenu->m_accel,
843 accel_key,
844 accel_mods );
845 }
846
847 // add new accelerator to our parent's accelerator group
848 wxCharBuffer buf = wxGTK_CONV_SYS( GetGtkHotKey(*this) );
849 if (buf[(size_t)0] != '\0')
850 {
851 gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods);
852 if (accel_key != 0)
853 {
854 gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem),
855 "activate",
856 m_parentMenu->m_accel,
857 accel_key,
858 accel_mods,
859 GTK_ACCEL_VISIBLE);
860 }
861 }
862 else if (isstock)
863 {
864 // if the accelerator was taken from a stock ID, just get it back from GTK+ stock
865 if (wxGetStockGtkAccelerator(stockid, &accel_mods, &accel_key))
866 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem),
867 m_parentMenu->m_accel,
868 accel_key,
869 accel_mods );
870 }
871 }
872
873 // NOTE: this function is different from the similar functions GTKProcessMnemonics()
874 // implemented in control.cpp and from wxMenuItemBase::GetLabelFromText...
875 // so there's no real code duplication
876 wxString wxMenuItem::GTKProcessMenuItemLabel(const wxString& str, wxString *hotKey)
877 {
878 wxString text;
879
880 // '\t' is the deliminator indicating a hot key
881 const wxChar *pc = str;
882 while ( (*pc != wxT('\0')) && (*pc != wxT('\t')) )
883 {
884 if ((*pc == wxT('&')) && (*(pc+1) == wxT('&')))
885 {
886 // "&" is doubled to indicate "&" instead of accelerator
887 ++pc;
888 text << wxT('&');
889 }
890 else if (*pc == wxT('&'))
891 {
892 text << wxT('_');
893 }
894 else if ( *pc == wxT('_') ) // escape underscores
895 {
896 text << wxT("__");
897 }
898 else
899 {
900 text << *pc;
901 }
902 ++pc;
903 }
904
905 if (hotKey)
906 {
907 hotKey->Empty();
908 if(*pc == wxT('\t'))
909 {
910 pc++;
911 *hotKey = pc;
912 }
913 }
914
915 return text;
916 }
917
918 // it's valid for this function to be called even if m_menuItem == NULL
919 void wxMenuItem::DoSetText( const wxString& str )
920 {
921 m_text.Empty();
922 m_text = GTKProcessMenuItemLabel(str, &m_hotKey);
923 }
924
925 #if wxUSE_ACCEL
926
927 wxAcceleratorEntry *wxMenuItem::GetAccel() const
928 {
929 if ( !GetHotKey() )
930 {
931 // nothing
932 return NULL;
933 }
934
935 // accelerator parsing code looks for them after a TAB, so insert a dummy
936 // one here
937 wxString label;
938 label << wxT('\t') << GetHotKey();
939
940 return wxAcceleratorEntry::Create(label);
941 }
942
943 #endif // wxUSE_ACCEL
944
945 void wxMenuItem::Check( bool check )
946 {
947 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
948
949 if (check == m_isChecked)
950 return;
951
952 wxMenuItemBase::Check( check );
953
954 switch ( GetKind() )
955 {
956 case wxITEM_CHECK:
957 case wxITEM_RADIO:
958 gtk_check_menu_item_set_active( (GtkCheckMenuItem*)m_menuItem, (gint)check );
959 break;
960
961 default:
962 wxFAIL_MSG( _T("can't check this item") );
963 }
964 }
965
966 void wxMenuItem::Enable( bool enable )
967 {
968 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
969
970 gtk_widget_set_sensitive( m_menuItem, enable );
971 wxMenuItemBase::Enable( enable );
972 }
973
974 bool wxMenuItem::IsChecked() const
975 {
976 wxCHECK_MSG( m_menuItem, false, wxT("invalid menu item") );
977
978 wxCHECK_MSG( IsCheckable(), false,
979 wxT("can't get state of uncheckable item!") );
980
981 return ((GtkCheckMenuItem*)m_menuItem)->active != 0;
982 }
983
984 //-----------------------------------------------------------------------------
985 // wxMenu
986 //-----------------------------------------------------------------------------
987
988 IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
989
990 void wxMenu::Init()
991 {
992 m_accel = gtk_accel_group_new();
993 m_menu = gtk_menu_new();
994 // NB: keep reference to the menu so that it is not destroyed behind
995 // our back by GTK+ e.g. when it is removed from menubar:
996 gtk_widget_ref(m_menu);
997
998 m_owner = (GtkWidget*) NULL;
999
1000 // Tearoffs are entries, just like separators. So if we want this
1001 // menu to be a tear-off one, we just append a tearoff entry
1002 // immediately.
1003 if ( m_style & wxMENU_TEAROFF )
1004 {
1005 GtkWidget *tearoff = gtk_tearoff_menu_item_new();
1006
1007 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu), tearoff);
1008 }
1009
1010 m_prevRadio = NULL;
1011
1012 // append the title as the very first entry if we have it
1013 if ( !m_title.empty() )
1014 {
1015 Append(wxGTK_TITLE_ID, m_title);
1016 AppendSeparator();
1017 }
1018 }
1019
1020 wxMenu::~wxMenu()
1021 {
1022 WX_CLEAR_LIST(wxMenuItemList, m_items);
1023
1024 if ( GTK_IS_WIDGET( m_menu ))
1025 {
1026 // see wxMenu::Init
1027 gtk_widget_unref( m_menu );
1028 // if the menu is inserted in another menu at this time, there was
1029 // one more reference to it:
1030 if ( m_owner )
1031 gtk_widget_destroy( m_menu );
1032 }
1033 }
1034
1035 void wxMenu::SetLayoutDirection(const wxLayoutDirection dir)
1036 {
1037 if ( m_owner )
1038 wxWindow::GTKSetLayout(m_owner, dir);
1039 //else: will be called later by wxMenuBar again
1040 }
1041
1042 wxLayoutDirection wxMenu::GetLayoutDirection() const
1043 {
1044 return wxWindow::GTKGetLayout(m_owner);
1045 }
1046
1047 bool wxMenu::GtkAppend(wxMenuItem *mitem, int pos)
1048 {
1049 GtkWidget *menuItem;
1050
1051 // cache some data used later
1052 wxString text = mitem->GetText();
1053 int id = mitem->GetId();
1054 bool isstock = wxIsStockID(id);
1055 const char *stockid = NULL;
1056 if (isstock)
1057 stockid = wxGetStockGtkID(mitem->GetId());
1058
1059 // stock menu items can have an empty label
1060 if (text.IsEmpty() && !mitem->IsSeparator())
1061 {
1062 wxASSERT_MSG(isstock, wxT("A non-stock menu item with an empty label?"));
1063 text = wxGetStockLabel(id);
1064
1065 // need & => _ conversion
1066 text = wxMenuItem::GTKProcessMenuItemLabel(text, NULL);
1067 }
1068
1069 if ( mitem->IsSeparator() )
1070 {
1071 menuItem = gtk_separator_menu_item_new();
1072 }
1073 else if ( mitem->GetBitmap().Ok() ||
1074 (mitem->GetKind() == wxITEM_NORMAL && isstock) )
1075 {
1076 wxBitmap bitmap(mitem->GetBitmap());
1077
1078 menuItem = gtk_image_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text ) );
1079
1080 GtkWidget *image;
1081 if ( !bitmap.Ok() )
1082 {
1083 // use stock bitmap for this item if available on the assumption
1084 // that it never hurts to follow GTK+ conventions more closely
1085 image = stockid ? gtk_image_new_from_stock(stockid, GTK_ICON_SIZE_MENU)
1086 : NULL;
1087 }
1088 else // we have a custom bitmap
1089 {
1090 wxASSERT_MSG( mitem->GetKind() == wxITEM_NORMAL,
1091 _T("only normal menu items can have bitmaps") );
1092
1093 if ( bitmap.HasPixbuf() )
1094 {
1095 image = gtk_image_new_from_pixbuf(bitmap.GetPixbuf());
1096 }
1097 else
1098 {
1099 GdkPixmap *gdk_pixmap = bitmap.GetPixmap();
1100 GdkBitmap *gdk_bitmap = bitmap.GetMask() ?
1101 bitmap.GetMask()->GetBitmap() :
1102 (GdkBitmap*) NULL;
1103 image = gtk_image_new_from_pixmap( gdk_pixmap, gdk_bitmap );
1104 }
1105 }
1106
1107 if ( image )
1108 {
1109 gtk_widget_show(image);
1110
1111 gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM(menuItem), image );
1112 }
1113
1114 m_prevRadio = NULL;
1115 }
1116 else // a normal item
1117 {
1118 // NB: 'text' variable has "_" instead of "&" after mitem->SetText()
1119 // so don't use it
1120
1121 switch ( mitem->GetKind() )
1122 {
1123 case wxITEM_CHECK:
1124 {
1125 menuItem = gtk_check_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text ) );
1126 m_prevRadio = NULL;
1127 break;
1128 }
1129
1130 case wxITEM_RADIO:
1131 {
1132 GSList *group = NULL;
1133 if ( m_prevRadio == NULL )
1134 {
1135 // start of a new radio group
1136 m_prevRadio = menuItem =
1137 gtk_radio_menu_item_new_with_mnemonic( group, wxGTK_CONV_SYS( text ) );
1138 }
1139 else // continue the radio group
1140 {
1141 group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (m_prevRadio));
1142 m_prevRadio = menuItem =
1143 gtk_radio_menu_item_new_with_mnemonic( group, wxGTK_CONV_SYS( text ) );
1144 }
1145 break;
1146 }
1147
1148 default:
1149 wxFAIL_MSG( _T("unexpected menu item kind") );
1150 // fall through
1151
1152 case wxITEM_NORMAL:
1153 {
1154 menuItem = gtk_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text ) );
1155 m_prevRadio = NULL;
1156 break;
1157 }
1158 }
1159
1160 }
1161
1162 guint accel_key;
1163 GdkModifierType accel_mods;
1164 wxCharBuffer buf = wxGTK_CONV_SYS( GetGtkHotKey(*mitem) );
1165
1166 // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetText().c_str(), GetGtkHotKey(*mitem).c_str() );
1167 if (buf[(size_t)0] != '\0')
1168 {
1169 gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods);
1170 if (accel_key != 0)
1171 {
1172 gtk_widget_add_accelerator (GTK_WIDGET(menuItem),
1173 "activate",
1174 m_accel,
1175 accel_key,
1176 accel_mods,
1177 GTK_ACCEL_VISIBLE);
1178 }
1179 }
1180 else if (isstock)
1181 {
1182 // if the accelerator was taken from a stock ID, just get it back from GTK+ stock
1183 if (wxGetStockGtkAccelerator(stockid, &accel_mods, &accel_key))
1184 gtk_widget_add_accelerator( GTK_WIDGET(menuItem),
1185 "activate",
1186 m_accel,
1187 accel_key,
1188 accel_mods,
1189 GTK_ACCEL_VISIBLE);
1190 }
1191
1192 if (pos == -1)
1193 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu), menuItem);
1194 else
1195 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu), menuItem, pos);
1196
1197 gtk_widget_show( menuItem );
1198
1199 if ( !mitem->IsSeparator() )
1200 {
1201 wxASSERT_MSG( menuItem, wxT("invalid menuitem") );
1202
1203 g_signal_connect (menuItem, "select",
1204 G_CALLBACK (gtk_menu_hilight_callback), this);
1205 g_signal_connect (menuItem, "deselect",
1206 G_CALLBACK (gtk_menu_nolight_callback), this);
1207
1208 if ( mitem->IsSubMenu() && mitem->GetKind() != wxITEM_RADIO && mitem->GetKind() != wxITEM_CHECK )
1209 {
1210 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), mitem->GetSubMenu()->m_menu );
1211
1212 gtk_widget_show( mitem->GetSubMenu()->m_menu );
1213
1214 // if adding a submenu to a menu already existing in the menu bar, we
1215 // must set invoking window to allow processing events from this
1216 // submenu
1217 if ( m_invokingWindow )
1218 wxMenubarSetInvokingWindow(mitem->GetSubMenu(), m_invokingWindow);
1219 }
1220 else
1221 {
1222 g_signal_connect (menuItem, "activate",
1223 G_CALLBACK (gtk_menu_clicked_callback),
1224 this);
1225 }
1226 }
1227
1228 mitem->SetMenuItem(menuItem);
1229
1230 if (ms_locked)
1231 {
1232 // This doesn't even exist!
1233 // gtk_widget_lock_accelerators(mitem->GetMenuItem());
1234 }
1235
1236 return true;
1237 }
1238
1239 wxMenuItem* wxMenu::DoAppend(wxMenuItem *mitem)
1240 {
1241 if (!GtkAppend(mitem))
1242 return NULL;
1243
1244 return wxMenuBase::DoAppend(mitem);
1245 }
1246
1247 wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
1248 {
1249 if ( !wxMenuBase::DoInsert(pos, item) )
1250 return NULL;
1251
1252 // TODO
1253 if ( !GtkAppend(item, (int)pos) )
1254 return NULL;
1255
1256 return item;
1257 }
1258
1259 wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
1260 {
1261 if ( !wxMenuBase::DoRemove(item) )
1262 return (wxMenuItem *)NULL;
1263
1264 // TODO: this code doesn't delete the item factory item and this seems
1265 // impossible as of GTK 1.2.6.
1266 gtk_widget_destroy( item->GetMenuItem() );
1267
1268 return item;
1269 }
1270
1271 int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
1272 {
1273 wxMenuItemList::compatibility_iterator node = m_items.GetFirst();
1274 while (node)
1275 {
1276 wxMenuItem *item = node->GetData();
1277 if (item->GetMenuItem() == menuItem)
1278 return item->GetId();
1279 node = node->GetNext();
1280 }
1281
1282 return wxNOT_FOUND;
1283 }
1284
1285 void wxMenu::Attach(wxMenuBarBase *menubar)
1286 {
1287 wxMenuBase::Attach(menubar);
1288
1289 // inherit layout direction from menubar.
1290 SetLayoutDirection(menubar->GetLayoutDirection());
1291 }
1292
1293 // ----------------------------------------------------------------------------
1294 // helpers
1295 // ----------------------------------------------------------------------------
1296
1297 #if wxUSE_ACCEL
1298
1299 static wxString GetGtkHotKey( const wxMenuItem& item )
1300 {
1301 wxString hotkey;
1302
1303 wxAcceleratorEntry *accel = item.GetAccel();
1304 if ( accel )
1305 {
1306 int flags = accel->GetFlags();
1307 if ( flags & wxACCEL_ALT )
1308 hotkey += wxT("<alt>");
1309 if ( flags & wxACCEL_CTRL )
1310 hotkey += wxT("<control>");
1311 if ( flags & wxACCEL_SHIFT )
1312 hotkey += wxT("<shift>");
1313
1314 int code = accel->GetKeyCode();
1315 switch ( code )
1316 {
1317 case WXK_F1:
1318 case WXK_F2:
1319 case WXK_F3:
1320 case WXK_F4:
1321 case WXK_F5:
1322 case WXK_F6:
1323 case WXK_F7:
1324 case WXK_F8:
1325 case WXK_F9:
1326 case WXK_F10:
1327 case WXK_F11:
1328 case WXK_F12:
1329 case WXK_F13:
1330 case WXK_F14:
1331 case WXK_F15:
1332 case WXK_F16:
1333 case WXK_F17:
1334 case WXK_F18:
1335 case WXK_F19:
1336 case WXK_F20:
1337 case WXK_F21:
1338 case WXK_F22:
1339 case WXK_F23:
1340 case WXK_F24:
1341 hotkey += wxString::Format(wxT("F%d"), code - WXK_F1 + 1);
1342 break;
1343
1344 // TODO: we should use gdk_keyval_name() (a.k.a.
1345 // XKeysymToString) here as well as hardcoding the keysym
1346 // names this might be not portable
1347 case WXK_INSERT:
1348 hotkey << wxT("Insert" );
1349 break;
1350 case WXK_DELETE:
1351 hotkey << wxT("Delete" );
1352 break;
1353 case WXK_UP:
1354 hotkey << wxT("Up" );
1355 break;
1356 case WXK_DOWN:
1357 hotkey << wxT("Down" );
1358 break;
1359 case WXK_PAGEUP:
1360 hotkey << wxT("PgUp" );
1361 break;
1362 case WXK_PAGEDOWN:
1363 hotkey << wxT("PgDn" );
1364 break;
1365 case WXK_LEFT:
1366 hotkey << wxT("Left" );
1367 break;
1368 case WXK_RIGHT:
1369 hotkey << wxT("Right" );
1370 break;
1371 case WXK_HOME:
1372 hotkey << wxT("Home" );
1373 break;
1374 case WXK_END:
1375 hotkey << wxT("End" );
1376 break;
1377 case WXK_RETURN:
1378 hotkey << wxT("Return" );
1379 break;
1380 case WXK_BACK:
1381 hotkey << wxT("BackSpace" );
1382 break;
1383 case WXK_TAB:
1384 hotkey << wxT("Tab" );
1385 break;
1386 case WXK_ESCAPE:
1387 hotkey << wxT("Esc" );
1388 break;
1389 case WXK_SPACE:
1390 hotkey << wxT("space" );
1391 break;
1392 case WXK_MULTIPLY:
1393 hotkey << wxT("Multiply" );
1394 break;
1395 case WXK_ADD:
1396 hotkey << wxT("Add" );
1397 break;
1398 case WXK_SEPARATOR:
1399 hotkey << wxT("Separator" );
1400 break;
1401 case WXK_SUBTRACT:
1402 hotkey << wxT("Subtract" );
1403 break;
1404 case WXK_DECIMAL:
1405 hotkey << wxT("Decimal" );
1406 break;
1407 case WXK_DIVIDE:
1408 hotkey << wxT("Divide" );
1409 break;
1410 case WXK_CANCEL:
1411 hotkey << wxT("Cancel" );
1412 break;
1413 case WXK_CLEAR:
1414 hotkey << wxT("Clear" );
1415 break;
1416 case WXK_MENU:
1417 hotkey << wxT("Menu" );
1418 break;
1419 case WXK_PAUSE:
1420 hotkey << wxT("Pause" );
1421 break;
1422 case WXK_CAPITAL:
1423 hotkey << wxT("Capital" );
1424 break;
1425 case WXK_SELECT:
1426 hotkey << wxT("Select" );
1427 break;
1428 case WXK_PRINT:
1429 hotkey << wxT("Print" );
1430 break;
1431 case WXK_EXECUTE:
1432 hotkey << wxT("Execute" );
1433 break;
1434 case WXK_SNAPSHOT:
1435 hotkey << wxT("Snapshot" );
1436 break;
1437 case WXK_HELP:
1438 hotkey << wxT("Help" );
1439 break;
1440 case WXK_NUMLOCK:
1441 hotkey << wxT("Num_Lock" );
1442 break;
1443 case WXK_SCROLL:
1444 hotkey << wxT("Scroll_Lock" );
1445 break;
1446 case WXK_NUMPAD_INSERT:
1447 hotkey << wxT("KP_Insert" );
1448 break;
1449 case WXK_NUMPAD_DELETE:
1450 hotkey << wxT("KP_Delete" );
1451 break;
1452 case WXK_NUMPAD_SPACE:
1453 hotkey << wxT("KP_Space" );
1454 break;
1455 case WXK_NUMPAD_TAB:
1456 hotkey << wxT("KP_Tab" );
1457 break;
1458 case WXK_NUMPAD_ENTER:
1459 hotkey << wxT("KP_Enter" );
1460 break;
1461 case WXK_NUMPAD_F1: case WXK_NUMPAD_F2: case WXK_NUMPAD_F3:
1462 case WXK_NUMPAD_F4:
1463 hotkey += wxString::Format(wxT("KP_F%d"), code - WXK_NUMPAD_F1 + 1);
1464 break;
1465 case WXK_NUMPAD_HOME:
1466 hotkey << wxT("KP_Home" );
1467 break;
1468 case WXK_NUMPAD_LEFT:
1469 hotkey << wxT("KP_Left" );
1470 break;
1471 case WXK_NUMPAD_UP:
1472 hotkey << wxT("KP_Up" );
1473 break;
1474 case WXK_NUMPAD_RIGHT:
1475 hotkey << wxT("KP_Right" );
1476 break;
1477 case WXK_NUMPAD_DOWN:
1478 hotkey << wxT("KP_Down" );
1479 break;
1480 case WXK_NUMPAD_PAGEUP:
1481 hotkey << wxT("KP_PgUp" );
1482 break;
1483 case WXK_NUMPAD_PAGEDOWN:
1484 hotkey << wxT("KP_PgDn" );
1485 break;
1486 case WXK_NUMPAD_END:
1487 hotkey << wxT("KP_End" );
1488 break;
1489 case WXK_NUMPAD_BEGIN:
1490 hotkey << wxT("KP_Begin" );
1491 break;
1492 case WXK_NUMPAD_EQUAL:
1493 hotkey << wxT("KP_Equal" );
1494 break;
1495 case WXK_NUMPAD_MULTIPLY:
1496 hotkey << wxT("KP_Multiply" );
1497 break;
1498 case WXK_NUMPAD_ADD:
1499 hotkey << wxT("KP_Add" );
1500 break;
1501 case WXK_NUMPAD_SEPARATOR:
1502 hotkey << wxT("KP_Separator" );
1503 break;
1504 case WXK_NUMPAD_SUBTRACT:
1505 hotkey << wxT("KP_Subtract" );
1506 break;
1507 case WXK_NUMPAD_DECIMAL:
1508 hotkey << wxT("KP_Decimal" );
1509 break;
1510 case WXK_NUMPAD_DIVIDE:
1511 hotkey << wxT("KP_Divide" );
1512 break;
1513 case WXK_NUMPAD0: case WXK_NUMPAD1: case WXK_NUMPAD2:
1514 case WXK_NUMPAD3: case WXK_NUMPAD4: case WXK_NUMPAD5:
1515 case WXK_NUMPAD6: case WXK_NUMPAD7: case WXK_NUMPAD8: case WXK_NUMPAD9:
1516 hotkey += wxString::Format(wxT("KP_%d"), code - WXK_NUMPAD0);
1517 break;
1518 case WXK_WINDOWS_LEFT:
1519 hotkey << wxT("Super_L" );
1520 break;
1521 case WXK_WINDOWS_RIGHT:
1522 hotkey << wxT("Super_R" );
1523 break;
1524 case WXK_WINDOWS_MENU:
1525 hotkey << wxT("Menu" );
1526 break;
1527 case WXK_COMMAND:
1528 hotkey << wxT("Command" );
1529 break;
1530 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
1531 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1532 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1533 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1534 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
1535 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1536 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1537 break;
1538 */
1539 // if there are any other keys wxAcceleratorEntry::Create() may
1540 // return, we should process them here
1541
1542 default:
1543 if ( code < 127 )
1544 {
1545 wxString name = wxGTK_CONV_BACK( gdk_keyval_name((guint)code) );
1546 if ( name )
1547 {
1548 hotkey << name;
1549 break;
1550 }
1551 }
1552
1553 wxFAIL_MSG( wxT("unknown keyboard accel") );
1554 }
1555
1556 delete accel;
1557 }
1558
1559 return hotkey;
1560 }
1561
1562 #endif // wxUSE_ACCEL
1563
1564 // ----------------------------------------------------------------------------
1565 // Pop-up menu stuff
1566 // ----------------------------------------------------------------------------
1567
1568 #if wxUSE_MENUS_NATIVE
1569
1570 extern "C" WXDLLIMPEXP_CORE
1571 void gtk_pop_hide_callback( GtkWidget *WXUNUSED(widget), bool* is_waiting )
1572 {
1573 *is_waiting = false;
1574 }
1575
1576 WXDLLIMPEXP_CORE void SetInvokingWindow( wxMenu *menu, wxWindow* win )
1577 {
1578 menu->SetInvokingWindow( win );
1579
1580 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
1581 while (node)
1582 {
1583 wxMenuItem *menuitem = node->GetData();
1584 if (menuitem->IsSubMenu())
1585 {
1586 SetInvokingWindow( menuitem->GetSubMenu(), win );
1587 }
1588
1589 node = node->GetNext();
1590 }
1591 }
1592
1593 extern "C" WXDLLIMPEXP_CORE
1594 void wxPopupMenuPositionCallback( GtkMenu *menu,
1595 gint *x, gint *y,
1596 gboolean * WXUNUSED(whatever),
1597 gpointer user_data )
1598 {
1599 // ensure that the menu appears entirely on screen
1600 GtkRequisition req;
1601 gtk_widget_get_child_requisition(GTK_WIDGET(menu), &req);
1602
1603 wxSize sizeScreen = wxGetDisplaySize();
1604 wxPoint *pos = (wxPoint*)user_data;
1605
1606 gint xmax = sizeScreen.x - req.width,
1607 ymax = sizeScreen.y - req.height;
1608
1609 *x = pos->x < xmax ? pos->x : xmax;
1610 *y = pos->y < ymax ? pos->y : ymax;
1611 }
1612
1613 bool wxWindowGTK::DoPopupMenu( wxMenu *menu, int x, int y )
1614 {
1615 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid window") );
1616
1617 wxCHECK_MSG( menu != NULL, false, wxT("invalid popup-menu") );
1618
1619 // NOTE: if you change this code, you need to update
1620 // the same code in taskbar.cpp as well. This
1621 // is ugly code duplication, I know.
1622
1623 SetInvokingWindow( menu, this );
1624
1625 menu->UpdateUI();
1626
1627 bool is_waiting = true;
1628
1629 gulong handler = g_signal_connect (menu->m_menu, "hide",
1630 G_CALLBACK (gtk_pop_hide_callback),
1631 &is_waiting);
1632
1633 wxPoint pos;
1634 gpointer userdata;
1635 GtkMenuPositionFunc posfunc;
1636 if ( x == -1 && y == -1 )
1637 {
1638 // use GTK's default positioning algorithm
1639 userdata = NULL;
1640 posfunc = NULL;
1641 }
1642 else
1643 {
1644 pos = ClientToScreen(wxPoint(x, y));
1645 userdata = &pos;
1646 posfunc = wxPopupMenuPositionCallback;
1647 }
1648
1649 wxMenuEvent eventOpen(wxEVT_MENU_OPEN, -1, menu);
1650 DoCommonMenuCallbackCode(menu, eventOpen);
1651
1652 gtk_menu_popup(
1653 GTK_MENU(menu->m_menu),
1654 (GtkWidget *) NULL, // parent menu shell
1655 (GtkWidget *) NULL, // parent menu item
1656 posfunc, // function to position it
1657 userdata, // client data
1658 0, // button used to activate it
1659 gtk_get_current_event_time()
1660 );
1661
1662 while (is_waiting)
1663 {
1664 gtk_main_iteration();
1665 }
1666
1667 g_signal_handler_disconnect (menu->m_menu, handler);
1668
1669 wxMenuEvent eventClose(wxEVT_MENU_CLOSE, -1, menu);
1670 DoCommonMenuCallbackCode(menu, eventClose);
1671
1672 return true;
1673 }
1674
1675 #endif // wxUSE_MENUS_NATIVE