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