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