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