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