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