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