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