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