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