Use a GtkVBox to do TLW layout. Rework some of the remaining sizing code.
[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 commandEvent.SetEventObject(menu);
615
616 frame->GetEventHandler()->ProcessEvent(commandEvent);
617 }
618 else
619 {
620 // otherwise let the menu have it
621 menu->SendEvent(id, item->IsCheckable() ? item->IsChecked() : -1);
622 }
623 }
624 }
625
626 //-----------------------------------------------------------------------------
627 // "select"
628 //-----------------------------------------------------------------------------
629
630 extern "C" {
631 static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu )
632 {
633 int id = menu->FindMenuIdByMenuItem(widget);
634
635 wxASSERT( id != -1 ); // should find it!
636
637 if (!menu->IsEnabled(id))
638 return;
639
640 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id );
641 event.SetEventObject( menu );
642
643 wxEvtHandler* handler = menu->GetEventHandler();
644 if (handler && handler->ProcessEvent(event))
645 return;
646
647 wxWindow *win = menu->GetInvokingWindow();
648 if (win) win->GetEventHandler()->ProcessEvent( event );
649 }
650 }
651
652 //-----------------------------------------------------------------------------
653 // "deselect"
654 //-----------------------------------------------------------------------------
655
656 extern "C" {
657 static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu )
658 {
659 int id = menu->FindMenuIdByMenuItem(widget);
660
661 wxASSERT( id != -1 ); // should find it!
662
663 if (!menu->IsEnabled(id))
664 return;
665
666 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 );
667 event.SetEventObject( menu );
668
669 wxEvtHandler* handler = menu->GetEventHandler();
670 if (handler && handler->ProcessEvent(event))
671 return;
672
673 wxWindow *win = menu->GetInvokingWindow();
674 if (win)
675 win->GetEventHandler()->ProcessEvent( event );
676 }
677 }
678
679 //-----------------------------------------------------------------------------
680 // wxMenuItem
681 //-----------------------------------------------------------------------------
682
683 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
684
685 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
686 int id,
687 const wxString& name,
688 const wxString& help,
689 wxItemKind kind,
690 wxMenu *subMenu)
691 {
692 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
693 }
694
695 wxMenuItem::wxMenuItem(wxMenu *parentMenu,
696 int id,
697 const wxString& text,
698 const wxString& help,
699 wxItemKind kind,
700 wxMenu *subMenu)
701 : wxMenuItemBase(parentMenu, id, text, help, kind, subMenu)
702 {
703 Init(text);
704 }
705
706 wxMenuItem::wxMenuItem(wxMenu *parentMenu,
707 int id,
708 const wxString& text,
709 const wxString& help,
710 bool isCheckable,
711 wxMenu *subMenu)
712 : wxMenuItemBase(parentMenu, id, text, help,
713 isCheckable ? wxITEM_CHECK : wxITEM_NORMAL, subMenu)
714 {
715 Init(text);
716 }
717
718 void wxMenuItem::Init(const wxString& text)
719 {
720 m_labelWidget = (GtkWidget *) NULL;
721 m_menuItem = (GtkWidget *) NULL;
722
723 DoSetText(text);
724 }
725
726 wxMenuItem::~wxMenuItem()
727 {
728 // don't delete menu items, the menus take care of that
729 }
730
731 // return the menu item text without any menu accels
732 /* static */
733
734 wxString wxMenuItemBase::GetLabelText(const wxString& text)
735 {
736 // The argument to this function will now always be in wxWidgets standard label
737 // format, not GTK+ format, so we do what the other ports do.
738
739 return wxStripMenuCodes(text);
740
741 #if 0
742 wxString label;
743
744 for ( const wxChar *pc = text.c_str(); *pc; pc++ )
745 {
746 if ( *pc == wxT('\t'))
747 break;
748
749 if ( *pc == wxT('_') )
750 {
751 // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx"
752 pc++;
753 label += *pc;
754 continue;
755 }
756
757 if ( *pc == wxT('\\') )
758 {
759 // GTK 2.0 escapes "xxx/xxx" to "xxx\/xxx"
760 pc++;
761 label += *pc;
762 continue;
763 }
764
765 if ( (*pc == wxT('&')) && (*(pc+1) != wxT('&')) )
766 {
767 // wxMSW escapes "&"
768 // "&" is doubled to indicate "&" instead of accelerator
769 continue;
770 }
771
772 label += *pc;
773 }
774
775 // wxPrintf( wxT("GetLabelText(): text %s label %s\n"), text.c_str(), label.c_str() );
776
777 return label;
778 #endif
779 }
780
781 wxString wxMenuItem::GetItemLabel() const
782 {
783 wxString label = wxConvertFromGTKToWXLabel(m_text);
784 if (!m_hotKey.IsEmpty())
785 label = label + wxT("\t") + m_hotKey;
786 return label;
787 }
788
789 void wxMenuItem::SetItemLabel( const wxString& str )
790 {
791 // cache some data which must be used later
792 bool isstock = wxIsStockID(GetId());
793 const char *stockid = NULL;
794 if (isstock)
795 stockid = wxGetStockGtkID(GetId());
796
797 // Some optimization to avoid flicker
798 wxString oldLabel = m_text;
799 oldLabel = wxStripMenuCodes(oldLabel);
800 oldLabel.Replace(wxT("_"), wxT(""));
801 wxString label1 = wxStripMenuCodes(str);
802 wxString oldhotkey = GetHotKey(); // Store the old hotkey in Ctrl-foo format
803 wxCharBuffer oldbuf = wxGTK_CONV_SYS( GetGtkHotKey(*this) ); // and as <control>foo
804
805 DoSetText(str);
806
807 if (oldLabel == label1 &&
808 oldhotkey == GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
809 return;
810
811 if (m_menuItem)
812 {
813 GtkLabel *label;
814 if (m_labelWidget)
815 label = (GtkLabel*) m_labelWidget;
816 else
817 label = GTK_LABEL( GTK_BIN(m_menuItem)->child );
818
819 // stock menu items can have empty labels:
820 wxString text = m_text;
821 if (text.IsEmpty() && !IsSeparator())
822 {
823 wxASSERT_MSG(isstock, wxT("A non-stock menu item with an empty label?"));
824 text = wxGetStockLabel(GetId());
825
826 // need & => _ conversion
827 text = GTKProcessMenuItemLabel(text, NULL);
828 }
829
830 gtk_label_set_text_with_mnemonic( GTK_LABEL(label), wxGTK_CONV_SYS(text) );
831 }
832
833 // remove old accelerator from our parent's accelerator group, if present
834 guint accel_key;
835 GdkModifierType accel_mods;
836 if (oldbuf[(size_t)0] != '\0')
837 {
838 gtk_accelerator_parse( (const char*) oldbuf, &accel_key, &accel_mods);
839 if (accel_key != 0)
840 {
841 gtk_widget_remove_accelerator(m_menuItem,
842 m_parentMenu->m_accel,
843 accel_key,
844 accel_mods );
845 }
846 }
847 else if (isstock)
848 {
849 // if the accelerator was taken from a stock ID, just get it back from GTK+ stock
850 if (wxGetStockGtkAccelerator(stockid, &accel_mods, &accel_key))
851 gtk_widget_remove_accelerator( m_menuItem,
852 m_parentMenu->m_accel,
853 accel_key,
854 accel_mods );
855 }
856
857 // add new accelerator to our parent's accelerator group
858 wxCharBuffer buf = wxGTK_CONV_SYS( GetGtkHotKey(*this) );
859 if (buf[(size_t)0] != '\0')
860 {
861 gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods);
862 if (accel_key != 0)
863 {
864 gtk_widget_add_accelerator( m_menuItem,
865 "activate",
866 m_parentMenu->m_accel,
867 accel_key,
868 accel_mods,
869 GTK_ACCEL_VISIBLE);
870 }
871 }
872 else if (isstock)
873 {
874 // if the accelerator was taken from a stock ID, just get it back from GTK+ stock
875 if (wxGetStockGtkAccelerator(stockid, &accel_mods, &accel_key))
876 gtk_widget_remove_accelerator( m_menuItem,
877 m_parentMenu->m_accel,
878 accel_key,
879 accel_mods );
880 }
881 }
882
883 // NOTE: this function is different from the similar functions GTKProcessMnemonics()
884 // implemented in control.cpp and from wxMenuItemBase::GetLabelText...
885 // so there's no real code duplication
886 wxString wxMenuItem::GTKProcessMenuItemLabel(const wxString& str, wxString *hotKey)
887 {
888 wxString text;
889
890 // '\t' is the deliminator indicating a hot key
891 wxString::const_iterator pc = str.begin();
892 while ( pc != str.end() && *pc != wxT('\t') )
893 {
894 if (*pc == wxT('&'))
895 {
896 wxString::const_iterator next = pc + 1;
897 if (next != str.end() && *next == wxT('&'))
898 {
899 // "&" is doubled to indicate "&" instead of accelerator
900 ++pc;
901 text << wxT('&');
902 }
903 else
904 {
905 text << wxT('_');
906 }
907 }
908 else if ( *pc == wxT('_') ) // escape underscores
909 {
910 text << wxT("__");
911 }
912 else
913 {
914 text << *pc;
915 }
916 ++pc;
917 }
918
919 if (hotKey)
920 {
921 hotKey->Empty();
922 if(*pc == wxT('\t'))
923 {
924 pc++;
925 hotKey->assign(pc, str.end());
926 }
927 }
928
929 return text;
930 }
931
932 // it's valid for this function to be called even if m_menuItem == NULL
933 void wxMenuItem::DoSetText( const wxString& str )
934 {
935 m_text.Empty();
936 m_text = GTKProcessMenuItemLabel(str, &m_hotKey);
937 }
938
939 #if wxUSE_ACCEL
940
941 wxAcceleratorEntry *wxMenuItem::GetAccel() const
942 {
943 if ( !GetHotKey() )
944 {
945 // nothing
946 return NULL;
947 }
948
949 // accelerator parsing code looks for them after a TAB, so insert a dummy
950 // one here
951 wxString label;
952 label << wxT('\t') << GetHotKey();
953
954 return wxAcceleratorEntry::Create(label);
955 }
956
957 #endif // wxUSE_ACCEL
958
959 void wxMenuItem::Check( bool check )
960 {
961 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
962
963 if (check == m_isChecked)
964 return;
965
966 wxMenuItemBase::Check( check );
967
968 switch ( GetKind() )
969 {
970 case wxITEM_CHECK:
971 case wxITEM_RADIO:
972 gtk_check_menu_item_set_active( (GtkCheckMenuItem*)m_menuItem, (gint)check );
973 break;
974
975 default:
976 wxFAIL_MSG( _T("can't check this item") );
977 }
978 }
979
980 void wxMenuItem::Enable( bool enable )
981 {
982 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
983
984 gtk_widget_set_sensitive( m_menuItem, enable );
985 wxMenuItemBase::Enable( enable );
986 }
987
988 bool wxMenuItem::IsChecked() const
989 {
990 wxCHECK_MSG( m_menuItem, false, wxT("invalid menu item") );
991
992 wxCHECK_MSG( IsCheckable(), false,
993 wxT("can't get state of uncheckable item!") );
994
995 return ((GtkCheckMenuItem*)m_menuItem)->active != 0;
996 }
997
998 //-----------------------------------------------------------------------------
999 // wxMenu
1000 //-----------------------------------------------------------------------------
1001
1002 IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
1003
1004 void wxMenu::Init()
1005 {
1006 m_accel = gtk_accel_group_new();
1007 m_menu = gtk_menu_new();
1008 // NB: keep reference to the menu so that it is not destroyed behind
1009 // our back by GTK+ e.g. when it is removed from menubar:
1010 gtk_widget_ref(m_menu);
1011
1012 m_owner = (GtkWidget*) NULL;
1013
1014 // Tearoffs are entries, just like separators. So if we want this
1015 // menu to be a tear-off one, we just append a tearoff entry
1016 // immediately.
1017 if ( m_style & wxMENU_TEAROFF )
1018 {
1019 GtkWidget *tearoff = gtk_tearoff_menu_item_new();
1020
1021 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu), tearoff);
1022 }
1023
1024 m_prevRadio = NULL;
1025
1026 // append the title as the very first entry if we have it
1027 if ( !m_title.empty() )
1028 {
1029 Append(wxGTK_TITLE_ID, m_title);
1030 AppendSeparator();
1031 }
1032 }
1033
1034 wxMenu::~wxMenu()
1035 {
1036 if ( GTK_IS_WIDGET( m_menu ))
1037 {
1038 // see wxMenu::Init
1039 gtk_widget_unref( m_menu );
1040 g_object_unref( m_accel );
1041
1042 // if the menu is inserted in another menu at this time, there was
1043 // one more reference to it:
1044 if ( m_owner )
1045 gtk_widget_destroy( m_menu );
1046 }
1047
1048 // This must come after we release GTK resources above. Otherwise, GTK will
1049 // give warnings/errors when attempting to free accelerator resources from
1050 // child items that just were destroyed (the m_menu widget can contain
1051 // references to accelerators in child items. Problem detected when removing
1052 // a menu from a wxMenuBar, and the removed menu had submenus with accelerators.)
1053 WX_CLEAR_LIST(wxMenuItemList, m_items);
1054 }
1055
1056 void wxMenu::SetLayoutDirection(const wxLayoutDirection dir)
1057 {
1058 if ( m_owner )
1059 wxWindow::GTKSetLayout(m_owner, dir);
1060 //else: will be called later by wxMenuBar again
1061 }
1062
1063 wxLayoutDirection wxMenu::GetLayoutDirection() const
1064 {
1065 return wxWindow::GTKGetLayout(m_owner);
1066 }
1067
1068 bool wxMenu::GtkAppend(wxMenuItem *mitem, int pos)
1069 {
1070 GtkWidget *menuItem;
1071
1072 // cache some data used later
1073 wxString text = mitem->wxMenuItemBase::GetItemLabel();
1074 int id = mitem->GetId();
1075 bool isstock = wxIsStockID(id);
1076 const char *stockid = NULL;
1077 if (isstock)
1078 stockid = wxGetStockGtkID(mitem->GetId());
1079
1080 // stock menu items can have an empty label
1081 if (text.IsEmpty() && !mitem->IsSeparator())
1082 {
1083 wxASSERT_MSG(isstock, wxT("A non-stock menu item with an empty label?"));
1084 text = wxGetStockLabel(id);
1085
1086 // need & => _ conversion
1087 text = wxMenuItem::GTKProcessMenuItemLabel(text, NULL);
1088 }
1089
1090 if ( mitem->IsSeparator() )
1091 {
1092 menuItem = gtk_separator_menu_item_new();
1093 }
1094 else if ( mitem->GetBitmap().Ok() ||
1095 (mitem->GetKind() == wxITEM_NORMAL && isstock) )
1096 {
1097 wxBitmap bitmap(mitem->GetBitmap());
1098
1099 menuItem = gtk_image_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text ) );
1100
1101 GtkWidget *image;
1102 if ( !bitmap.Ok() )
1103 {
1104 // use stock bitmap for this item if available on the assumption
1105 // that it never hurts to follow GTK+ conventions more closely
1106 image = stockid ? gtk_image_new_from_stock(stockid, GTK_ICON_SIZE_MENU)
1107 : NULL;
1108 }
1109 else // we have a custom bitmap
1110 {
1111 wxASSERT_MSG( mitem->GetKind() == wxITEM_NORMAL,
1112 _T("only normal menu items can have bitmaps") );
1113
1114 if ( bitmap.HasPixbuf() )
1115 {
1116 image = gtk_image_new_from_pixbuf(bitmap.GetPixbuf());
1117 }
1118 else
1119 {
1120 GdkPixmap *gdk_pixmap = bitmap.GetPixmap();
1121 GdkBitmap *gdk_bitmap = bitmap.GetMask() ?
1122 bitmap.GetMask()->GetBitmap() :
1123 (GdkBitmap*) NULL;
1124 image = gtk_image_new_from_pixmap( gdk_pixmap, gdk_bitmap );
1125 }
1126 }
1127
1128 if ( image )
1129 {
1130 gtk_widget_show(image);
1131
1132 gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM(menuItem), image );
1133 }
1134
1135 m_prevRadio = NULL;
1136 }
1137 else // a normal item
1138 {
1139 // NB: 'text' variable has "_" instead of "&" after mitem->SetItemLabel()
1140 // so don't use it
1141
1142 switch ( mitem->GetKind() )
1143 {
1144 case wxITEM_CHECK:
1145 {
1146 menuItem = gtk_check_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text ) );
1147 m_prevRadio = NULL;
1148 break;
1149 }
1150
1151 case wxITEM_RADIO:
1152 {
1153 GSList *group = NULL;
1154 if ( m_prevRadio == NULL )
1155 {
1156 // start of a new radio group
1157 m_prevRadio = menuItem =
1158 gtk_radio_menu_item_new_with_mnemonic( group, wxGTK_CONV_SYS( text ) );
1159 }
1160 else // continue the radio group
1161 {
1162 group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (m_prevRadio));
1163 m_prevRadio = menuItem =
1164 gtk_radio_menu_item_new_with_mnemonic( group, wxGTK_CONV_SYS( text ) );
1165 }
1166 break;
1167 }
1168
1169 default:
1170 wxFAIL_MSG( _T("unexpected menu item kind") );
1171 // fall through
1172
1173 case wxITEM_NORMAL:
1174 {
1175 menuItem = gtk_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text ) );
1176 m_prevRadio = NULL;
1177 break;
1178 }
1179 }
1180
1181 }
1182
1183 guint accel_key;
1184 GdkModifierType accel_mods;
1185 wxCharBuffer buf = wxGTK_CONV_SYS( GetGtkHotKey(*mitem) );
1186
1187 // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetItemLabel().c_str(), GetGtkHotKey(*mitem).c_str() );
1188 if (buf[(size_t)0] != '\0')
1189 {
1190 gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods);
1191 if (accel_key != 0)
1192 {
1193 gtk_widget_add_accelerator (menuItem,
1194 "activate",
1195 m_accel,
1196 accel_key,
1197 accel_mods,
1198 GTK_ACCEL_VISIBLE);
1199 }
1200 }
1201 else if (isstock)
1202 {
1203 // if the accelerator was taken from a stock ID, just get it back from GTK+ stock
1204 if (wxGetStockGtkAccelerator(stockid, &accel_mods, &accel_key))
1205 gtk_widget_add_accelerator( menuItem,
1206 "activate",
1207 m_accel,
1208 accel_key,
1209 accel_mods,
1210 GTK_ACCEL_VISIBLE);
1211 }
1212
1213 if (pos == -1)
1214 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu), menuItem);
1215 else
1216 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu), menuItem, pos);
1217
1218 gtk_widget_show( menuItem );
1219
1220 if ( !mitem->IsSeparator() )
1221 {
1222 wxASSERT_MSG( menuItem, wxT("invalid menuitem") );
1223
1224 g_signal_connect (menuItem, "select",
1225 G_CALLBACK (gtk_menu_hilight_callback), this);
1226 g_signal_connect (menuItem, "deselect",
1227 G_CALLBACK (gtk_menu_nolight_callback), this);
1228
1229 if ( mitem->IsSubMenu() && mitem->GetKind() != wxITEM_RADIO && mitem->GetKind() != wxITEM_CHECK )
1230 {
1231 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), mitem->GetSubMenu()->m_menu );
1232
1233 gtk_widget_show( mitem->GetSubMenu()->m_menu );
1234
1235 // if adding a submenu to a menu already existing in the menu bar, we
1236 // must set invoking window to allow processing events from this
1237 // submenu
1238 if ( m_invokingWindow )
1239 wxMenubarSetInvokingWindow(mitem->GetSubMenu(), m_invokingWindow);
1240 }
1241 else
1242 {
1243 g_signal_connect (menuItem, "activate",
1244 G_CALLBACK (gtk_menu_clicked_callback),
1245 this);
1246 }
1247 }
1248
1249 mitem->SetMenuItem(menuItem);
1250
1251 if (ms_locked)
1252 {
1253 // This doesn't even exist!
1254 // gtk_widget_lock_accelerators(mitem->GetMenuItem());
1255 }
1256
1257 return true;
1258 }
1259
1260 wxMenuItem* wxMenu::DoAppend(wxMenuItem *mitem)
1261 {
1262 if (!GtkAppend(mitem))
1263 return NULL;
1264
1265 return wxMenuBase::DoAppend(mitem);
1266 }
1267
1268 wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
1269 {
1270 if ( !wxMenuBase::DoInsert(pos, item) )
1271 return NULL;
1272
1273 // TODO
1274 if ( !GtkAppend(item, (int)pos) )
1275 return NULL;
1276
1277 return item;
1278 }
1279
1280 wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
1281 {
1282 if ( !wxMenuBase::DoRemove(item) )
1283 return (wxMenuItem *)NULL;
1284
1285 // TODO: this code doesn't delete the item factory item and this seems
1286 // impossible as of GTK 1.2.6.
1287 gtk_widget_destroy( item->GetMenuItem() );
1288
1289 return item;
1290 }
1291
1292 int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
1293 {
1294 wxMenuItemList::compatibility_iterator node = m_items.GetFirst();
1295 while (node)
1296 {
1297 wxMenuItem *item = node->GetData();
1298 if (item->GetMenuItem() == menuItem)
1299 return item->GetId();
1300 node = node->GetNext();
1301 }
1302
1303 return wxNOT_FOUND;
1304 }
1305
1306 void wxMenu::Attach(wxMenuBarBase *menubar)
1307 {
1308 wxMenuBase::Attach(menubar);
1309
1310 // inherit layout direction from menubar.
1311 SetLayoutDirection(menubar->GetLayoutDirection());
1312 }
1313
1314 // ----------------------------------------------------------------------------
1315 // helpers
1316 // ----------------------------------------------------------------------------
1317
1318 #if wxUSE_ACCEL
1319
1320 static wxString GetGtkHotKey( const wxMenuItem& item )
1321 {
1322 wxString hotkey;
1323
1324 wxAcceleratorEntry *accel = item.GetAccel();
1325 if ( accel )
1326 {
1327 int flags = accel->GetFlags();
1328 if ( flags & wxACCEL_ALT )
1329 hotkey += wxT("<alt>");
1330 if ( flags & wxACCEL_CTRL )
1331 hotkey += wxT("<control>");
1332 if ( flags & wxACCEL_SHIFT )
1333 hotkey += wxT("<shift>");
1334
1335 int code = accel->GetKeyCode();
1336 switch ( code )
1337 {
1338 case WXK_F1:
1339 case WXK_F2:
1340 case WXK_F3:
1341 case WXK_F4:
1342 case WXK_F5:
1343 case WXK_F6:
1344 case WXK_F7:
1345 case WXK_F8:
1346 case WXK_F9:
1347 case WXK_F10:
1348 case WXK_F11:
1349 case WXK_F12:
1350 case WXK_F13:
1351 case WXK_F14:
1352 case WXK_F15:
1353 case WXK_F16:
1354 case WXK_F17:
1355 case WXK_F18:
1356 case WXK_F19:
1357 case WXK_F20:
1358 case WXK_F21:
1359 case WXK_F22:
1360 case WXK_F23:
1361 case WXK_F24:
1362 hotkey += wxString::Format(wxT("F%d"), code - WXK_F1 + 1);
1363 break;
1364
1365 // TODO: we should use gdk_keyval_name() (a.k.a.
1366 // XKeysymToString) here as well as hardcoding the keysym
1367 // names this might be not portable
1368 case WXK_INSERT:
1369 hotkey << wxT("Insert" );
1370 break;
1371 case WXK_DELETE:
1372 hotkey << wxT("Delete" );
1373 break;
1374 case WXK_UP:
1375 hotkey << wxT("Up" );
1376 break;
1377 case WXK_DOWN:
1378 hotkey << wxT("Down" );
1379 break;
1380 case WXK_PAGEUP:
1381 hotkey << wxT("Page_Up" );
1382 break;
1383 case WXK_PAGEDOWN:
1384 hotkey << wxT("Page_Down" );
1385 break;
1386 case WXK_LEFT:
1387 hotkey << wxT("Left" );
1388 break;
1389 case WXK_RIGHT:
1390 hotkey << wxT("Right" );
1391 break;
1392 case WXK_HOME:
1393 hotkey << wxT("Home" );
1394 break;
1395 case WXK_END:
1396 hotkey << wxT("End" );
1397 break;
1398 case WXK_RETURN:
1399 hotkey << wxT("Return" );
1400 break;
1401 case WXK_BACK:
1402 hotkey << wxT("BackSpace" );
1403 break;
1404 case WXK_TAB:
1405 hotkey << wxT("Tab" );
1406 break;
1407 case WXK_ESCAPE:
1408 hotkey << wxT("Esc" );
1409 break;
1410 case WXK_SPACE:
1411 hotkey << wxT("space" );
1412 break;
1413 case WXK_MULTIPLY:
1414 hotkey << wxT("Multiply" );
1415 break;
1416 case WXK_ADD:
1417 hotkey << wxT("Add" );
1418 break;
1419 case WXK_SEPARATOR:
1420 hotkey << wxT("Separator" );
1421 break;
1422 case WXK_SUBTRACT:
1423 hotkey << wxT("Subtract" );
1424 break;
1425 case WXK_DECIMAL:
1426 hotkey << wxT("Decimal" );
1427 break;
1428 case WXK_DIVIDE:
1429 hotkey << wxT("Divide" );
1430 break;
1431 case WXK_CANCEL:
1432 hotkey << wxT("Cancel" );
1433 break;
1434 case WXK_CLEAR:
1435 hotkey << wxT("Clear" );
1436 break;
1437 case WXK_MENU:
1438 hotkey << wxT("Menu" );
1439 break;
1440 case WXK_PAUSE:
1441 hotkey << wxT("Pause" );
1442 break;
1443 case WXK_CAPITAL:
1444 hotkey << wxT("Capital" );
1445 break;
1446 case WXK_SELECT:
1447 hotkey << wxT("Select" );
1448 break;
1449 case WXK_PRINT:
1450 hotkey << wxT("Print" );
1451 break;
1452 case WXK_EXECUTE:
1453 hotkey << wxT("Execute" );
1454 break;
1455 case WXK_SNAPSHOT:
1456 hotkey << wxT("Snapshot" );
1457 break;
1458 case WXK_HELP:
1459 hotkey << wxT("Help" );
1460 break;
1461 case WXK_NUMLOCK:
1462 hotkey << wxT("Num_Lock" );
1463 break;
1464 case WXK_SCROLL:
1465 hotkey << wxT("Scroll_Lock" );
1466 break;
1467 case WXK_NUMPAD_INSERT:
1468 hotkey << wxT("KP_Insert" );
1469 break;
1470 case WXK_NUMPAD_DELETE:
1471 hotkey << wxT("KP_Delete" );
1472 break;
1473 case WXK_NUMPAD_SPACE:
1474 hotkey << wxT("KP_Space" );
1475 break;
1476 case WXK_NUMPAD_TAB:
1477 hotkey << wxT("KP_Tab" );
1478 break;
1479 case WXK_NUMPAD_ENTER:
1480 hotkey << wxT("KP_Enter" );
1481 break;
1482 case WXK_NUMPAD_F1: case WXK_NUMPAD_F2: case WXK_NUMPAD_F3:
1483 case WXK_NUMPAD_F4:
1484 hotkey += wxString::Format(wxT("KP_F%d"), code - WXK_NUMPAD_F1 + 1);
1485 break;
1486 case WXK_NUMPAD_HOME:
1487 hotkey << wxT("KP_Home" );
1488 break;
1489 case WXK_NUMPAD_LEFT:
1490 hotkey << wxT("KP_Left" );
1491 break;
1492 case WXK_NUMPAD_UP:
1493 hotkey << wxT("KP_Up" );
1494 break;
1495 case WXK_NUMPAD_RIGHT:
1496 hotkey << wxT("KP_Right" );
1497 break;
1498 case WXK_NUMPAD_DOWN:
1499 hotkey << wxT("KP_Down" );
1500 break;
1501 case WXK_NUMPAD_PAGEUP:
1502 hotkey << wxT("KP_Page_Up" );
1503 break;
1504 case WXK_NUMPAD_PAGEDOWN:
1505 hotkey << wxT("KP_Page_Down" );
1506 break;
1507 case WXK_NUMPAD_END:
1508 hotkey << wxT("KP_End" );
1509 break;
1510 case WXK_NUMPAD_BEGIN:
1511 hotkey << wxT("KP_Begin" );
1512 break;
1513 case WXK_NUMPAD_EQUAL:
1514 hotkey << wxT("KP_Equal" );
1515 break;
1516 case WXK_NUMPAD_MULTIPLY:
1517 hotkey << wxT("KP_Multiply" );
1518 break;
1519 case WXK_NUMPAD_ADD:
1520 hotkey << wxT("KP_Add" );
1521 break;
1522 case WXK_NUMPAD_SEPARATOR:
1523 hotkey << wxT("KP_Separator" );
1524 break;
1525 case WXK_NUMPAD_SUBTRACT:
1526 hotkey << wxT("KP_Subtract" );
1527 break;
1528 case WXK_NUMPAD_DECIMAL:
1529 hotkey << wxT("KP_Decimal" );
1530 break;
1531 case WXK_NUMPAD_DIVIDE:
1532 hotkey << wxT("KP_Divide" );
1533 break;
1534 case WXK_NUMPAD0: case WXK_NUMPAD1: case WXK_NUMPAD2:
1535 case WXK_NUMPAD3: case WXK_NUMPAD4: case WXK_NUMPAD5:
1536 case WXK_NUMPAD6: case WXK_NUMPAD7: case WXK_NUMPAD8: case WXK_NUMPAD9:
1537 hotkey += wxString::Format(wxT("KP_%d"), code - WXK_NUMPAD0);
1538 break;
1539 case WXK_WINDOWS_LEFT:
1540 hotkey << wxT("Super_L" );
1541 break;
1542 case WXK_WINDOWS_RIGHT:
1543 hotkey << wxT("Super_R" );
1544 break;
1545 case WXK_WINDOWS_MENU:
1546 hotkey << wxT("Menu" );
1547 break;
1548 case WXK_COMMAND:
1549 hotkey << wxT("Command" );
1550 break;
1551 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
1552 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1553 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1554 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1555 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
1556 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1557 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1558 break;
1559 */
1560 // if there are any other keys wxAcceleratorEntry::Create() may
1561 // return, we should process them here
1562
1563 default:
1564 if ( code < 127 )
1565 {
1566 const wxString
1567 name = wxGTK_CONV_BACK_SYS(gdk_keyval_name((guint)code));
1568 if ( !name.empty() )
1569 {
1570 hotkey << name;
1571 break;
1572 }
1573 }
1574
1575 wxFAIL_MSG( wxT("unknown keyboard accel") );
1576 }
1577
1578 delete accel;
1579 }
1580
1581 return hotkey;
1582 }
1583
1584 #endif // wxUSE_ACCEL
1585
1586 // ----------------------------------------------------------------------------
1587 // Pop-up menu stuff
1588 // ----------------------------------------------------------------------------
1589
1590 #if wxUSE_MENUS_NATIVE
1591
1592 extern "C" WXDLLIMPEXP_CORE
1593 void gtk_pop_hide_callback( GtkWidget *WXUNUSED(widget), bool* is_waiting )
1594 {
1595 *is_waiting = false;
1596 }
1597
1598 WXDLLIMPEXP_CORE void SetInvokingWindow( wxMenu *menu, wxWindow* win )
1599 {
1600 menu->SetInvokingWindow( win );
1601
1602 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
1603 while (node)
1604 {
1605 wxMenuItem *menuitem = node->GetData();
1606 if (menuitem->IsSubMenu())
1607 {
1608 SetInvokingWindow( menuitem->GetSubMenu(), win );
1609 }
1610
1611 node = node->GetNext();
1612 }
1613 }
1614
1615 extern "C" WXDLLIMPEXP_CORE
1616 void wxPopupMenuPositionCallback( GtkMenu *menu,
1617 gint *x, gint *y,
1618 gboolean * WXUNUSED(whatever),
1619 gpointer user_data )
1620 {
1621 // ensure that the menu appears entirely on screen
1622 GtkRequisition req;
1623 gtk_widget_get_child_requisition(GTK_WIDGET(menu), &req);
1624
1625 wxSize sizeScreen = wxGetDisplaySize();
1626 wxPoint *pos = (wxPoint*)user_data;
1627
1628 gint xmax = sizeScreen.x - req.width,
1629 ymax = sizeScreen.y - req.height;
1630
1631 *x = pos->x < xmax ? pos->x : xmax;
1632 *y = pos->y < ymax ? pos->y : ymax;
1633 }
1634
1635 bool wxWindowGTK::DoPopupMenu( wxMenu *menu, int x, int y )
1636 {
1637 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid window") );
1638
1639 wxCHECK_MSG( menu != NULL, false, wxT("invalid popup-menu") );
1640
1641 // NOTE: if you change this code, you need to update
1642 // the same code in taskbar.cpp as well. This
1643 // is ugly code duplication, I know.
1644
1645 SetInvokingWindow( menu, this );
1646
1647 menu->UpdateUI();
1648
1649 bool is_waiting = true;
1650
1651 gulong handler = g_signal_connect (menu->m_menu, "hide",
1652 G_CALLBACK (gtk_pop_hide_callback),
1653 &is_waiting);
1654
1655 wxPoint pos;
1656 gpointer userdata;
1657 GtkMenuPositionFunc posfunc;
1658 if ( x == -1 && y == -1 )
1659 {
1660 // use GTK's default positioning algorithm
1661 userdata = NULL;
1662 posfunc = NULL;
1663 }
1664 else
1665 {
1666 pos = ClientToScreen(wxPoint(x, y));
1667 userdata = &pos;
1668 posfunc = wxPopupMenuPositionCallback;
1669 }
1670
1671 wxMenuEvent eventOpen(wxEVT_MENU_OPEN, -1, menu);
1672 DoCommonMenuCallbackCode(menu, eventOpen);
1673
1674 gtk_menu_popup(
1675 GTK_MENU(menu->m_menu),
1676 (GtkWidget *) NULL, // parent menu shell
1677 (GtkWidget *) NULL, // parent menu item
1678 posfunc, // function to position it
1679 userdata, // client data
1680 0, // button used to activate it
1681 gtk_get_current_event_time()
1682 );
1683
1684 while (is_waiting)
1685 {
1686 gtk_main_iteration();
1687 }
1688
1689 g_signal_handler_disconnect (menu->m_menu, handler);
1690
1691 wxMenuEvent eventClose(wxEVT_MENU_CLOSE, -1, menu);
1692 DoCommonMenuCallbackCode(menu, eventClose);
1693
1694 return true;
1695 }
1696
1697 #endif // wxUSE_MENUS_NATIVE
1698
1699 #ifdef __WXGTK20__
1700
1701 #include <gtk/gtk.h>
1702
1703 const char *wxGetStockGtkID(wxWindowID id)
1704 {
1705 #define STOCKITEM(wx,gtk) \
1706 case wx: \
1707 return gtk;
1708
1709 #define STOCKITEM_MISSING(wx) \
1710 case wx: \
1711 return NULL;
1712
1713 #if GTK_CHECK_VERSION(2,4,0)
1714 #define STOCKITEM_24(wx,gtk) STOCKITEM(wx,gtk)
1715 #else
1716 #define STOCKITEM_24(wx,gtk) STOCKITEM_MISSING(wx)
1717 #endif
1718
1719 #if GTK_CHECK_VERSION(2,6,0)
1720 #define STOCKITEM_26(wx,gtk) STOCKITEM(wx,gtk)
1721 #else
1722 #define STOCKITEM_26(wx,gtk) STOCKITEM_MISSING(wx)
1723 #endif
1724
1725 #if GTK_CHECK_VERSION(2,10,0)
1726 #define STOCKITEM_210(wx,gtk) STOCKITEM(wx,gtk)
1727 #else
1728 #define STOCKITEM_210(wx,gtk) STOCKITEM_MISSING(wx)
1729 #endif
1730
1731
1732 switch (id)
1733 {
1734 STOCKITEM_26(wxID_ABOUT, GTK_STOCK_ABOUT)
1735 STOCKITEM(wxID_ADD, GTK_STOCK_ADD)
1736 STOCKITEM(wxID_APPLY, GTK_STOCK_APPLY)
1737 STOCKITEM(wxID_BOLD, GTK_STOCK_BOLD)
1738 STOCKITEM(wxID_CANCEL, GTK_STOCK_CANCEL)
1739 STOCKITEM(wxID_CLEAR, GTK_STOCK_CLEAR)
1740 STOCKITEM(wxID_CLOSE, GTK_STOCK_CLOSE)
1741 STOCKITEM(wxID_COPY, GTK_STOCK_COPY)
1742 STOCKITEM(wxID_CUT, GTK_STOCK_CUT)
1743 STOCKITEM(wxID_DELETE, GTK_STOCK_DELETE)
1744 STOCKITEM_26(wxID_EDIT, GTK_STOCK_EDIT)
1745 STOCKITEM(wxID_FIND, GTK_STOCK_FIND)
1746 STOCKITEM_26(wxID_FILE, GTK_STOCK_FILE)
1747 STOCKITEM(wxID_REPLACE, GTK_STOCK_FIND_AND_REPLACE)
1748 STOCKITEM(wxID_BACKWARD, GTK_STOCK_GO_BACK)
1749 STOCKITEM(wxID_DOWN, GTK_STOCK_GO_DOWN)
1750 STOCKITEM(wxID_FORWARD, GTK_STOCK_GO_FORWARD)
1751 STOCKITEM(wxID_UP, GTK_STOCK_GO_UP)
1752 STOCKITEM(wxID_HELP, GTK_STOCK_HELP)
1753 STOCKITEM(wxID_HOME, GTK_STOCK_HOME)
1754 STOCKITEM_24(wxID_INDENT, GTK_STOCK_INDENT)
1755 STOCKITEM(wxID_INDEX, GTK_STOCK_INDEX)
1756 STOCKITEM(wxID_ITALIC, GTK_STOCK_ITALIC)
1757 STOCKITEM(wxID_JUSTIFY_CENTER, GTK_STOCK_JUSTIFY_CENTER)
1758 STOCKITEM(wxID_JUSTIFY_FILL, GTK_STOCK_JUSTIFY_FILL)
1759 STOCKITEM(wxID_JUSTIFY_LEFT, GTK_STOCK_JUSTIFY_LEFT)
1760 STOCKITEM(wxID_JUSTIFY_RIGHT, GTK_STOCK_JUSTIFY_RIGHT)
1761 STOCKITEM(wxID_NEW, GTK_STOCK_NEW)
1762 STOCKITEM(wxID_NO, GTK_STOCK_NO)
1763 STOCKITEM(wxID_OK, GTK_STOCK_OK)
1764 STOCKITEM(wxID_OPEN, GTK_STOCK_OPEN)
1765 STOCKITEM(wxID_PASTE, GTK_STOCK_PASTE)
1766 STOCKITEM(wxID_PREFERENCES, GTK_STOCK_PREFERENCES)
1767 STOCKITEM(wxID_PRINT, GTK_STOCK_PRINT)
1768 STOCKITEM(wxID_PREVIEW, GTK_STOCK_PRINT_PREVIEW)
1769 STOCKITEM(wxID_PROPERTIES, GTK_STOCK_PROPERTIES)
1770 STOCKITEM(wxID_EXIT, GTK_STOCK_QUIT)
1771 STOCKITEM(wxID_REDO, GTK_STOCK_REDO)
1772 STOCKITEM(wxID_REFRESH, GTK_STOCK_REFRESH)
1773 STOCKITEM(wxID_REMOVE, GTK_STOCK_REMOVE)
1774 STOCKITEM(wxID_REVERT_TO_SAVED, GTK_STOCK_REVERT_TO_SAVED)
1775 STOCKITEM(wxID_SAVE, GTK_STOCK_SAVE)
1776 STOCKITEM(wxID_SAVEAS, GTK_STOCK_SAVE_AS)
1777 STOCKITEM_210(wxID_SELECTALL, GTK_STOCK_SELECT_ALL)
1778 STOCKITEM(wxID_STOP, GTK_STOCK_STOP)
1779 STOCKITEM(wxID_UNDELETE, GTK_STOCK_UNDELETE)
1780 STOCKITEM(wxID_UNDERLINE, GTK_STOCK_UNDERLINE)
1781 STOCKITEM(wxID_UNDO, GTK_STOCK_UNDO)
1782 STOCKITEM_24(wxID_UNINDENT, GTK_STOCK_UNINDENT)
1783 STOCKITEM(wxID_YES, GTK_STOCK_YES)
1784 STOCKITEM(wxID_ZOOM_100, GTK_STOCK_ZOOM_100)
1785 STOCKITEM(wxID_ZOOM_FIT, GTK_STOCK_ZOOM_FIT)
1786 STOCKITEM(wxID_ZOOM_IN, GTK_STOCK_ZOOM_IN)
1787 STOCKITEM(wxID_ZOOM_OUT, GTK_STOCK_ZOOM_OUT)
1788
1789 default:
1790 wxFAIL_MSG( _T("invalid stock item ID") );
1791 break;
1792 };
1793
1794 #undef STOCKITEM
1795
1796 return NULL;
1797 }
1798
1799 bool wxGetStockGtkAccelerator(const char *id, GdkModifierType *mod, guint *key)
1800 {
1801 if (!id)
1802 return false;
1803
1804 GtkStockItem stock_item;
1805 if (gtk_stock_lookup (id, &stock_item))
1806 {
1807 if (key) *key = stock_item.keyval;
1808 if (mod) *mod = stock_item.modifier;
1809
1810 // some GTK stock items have zero values for the keyval;
1811 // it means that they do not have an accelerator...
1812 if (stock_item.keyval)
1813 return true;
1814 }
1815
1816 return false;
1817 }
1818
1819 #endif // __WXGTK20__
1820
1821 #endif // wxUSE_MENUS