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