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