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