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