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