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