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