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