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