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