]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/menu.cpp
Move some OLE utilities to oleutils.cpp and simplified date conversion
[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 {
515 GtkLabel *glabel = GTK_LABEL( GTK_BIN(menu->m_owner)->child );
516
517 /* set new text */
518 gtk_label_set( glabel, wxGTK_CONV( str ) );
519
520 /* reparse key accel */
521 (void)gtk_label_parse_uline (GTK_LABEL(glabel), wxGTK_CONV( str ) );
522 gtk_accel_label_refetch( GTK_ACCEL_LABEL(glabel) );
523 }
524
525 }
526
527 //-----------------------------------------------------------------------------
528 // "activate"
529 //-----------------------------------------------------------------------------
530
531 extern "C" {
532 static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu )
533 {
534 if (g_isIdle)
535 wxapp_install_idle_handler();
536
537 int id = menu->FindMenuIdByMenuItem(widget);
538
539 /* should find it for normal (not popup) menu */
540 wxASSERT_MSG( (id != -1) || (menu->GetInvokingWindow() != NULL),
541 _T("menu item not found in gtk_menu_clicked_callback") );
542
543 if (!menu->IsEnabled(id))
544 return;
545
546 wxMenuItem* item = menu->FindChildItem( id );
547 wxCHECK_RET( item, wxT("error in menu item callback") );
548
549 if ( item->GetId() == wxGTK_TITLE_ID )
550 {
551 // ignore events from the menu title
552 return;
553 }
554
555 if (item->IsCheckable())
556 {
557 bool isReallyChecked = item->IsChecked(),
558 isInternallyChecked = item->wxMenuItemBase::IsChecked();
559
560 // ensure that the internal state is always consistent with what is
561 // shown on the screen
562 item->wxMenuItemBase::Check(isReallyChecked);
563
564 // we must not report the events for the radio button going up nor the
565 // events resulting from the calls to wxMenuItem::Check()
566 if ( (item->GetKind() == wxITEM_RADIO && !isReallyChecked) ||
567 (isInternallyChecked == isReallyChecked) )
568 {
569 return;
570 }
571 }
572
573
574 // Is this menu on a menubar? (possibly nested)
575 wxFrame* frame = NULL;
576 if(menu->IsAttached())
577 frame = menu->GetMenuBar()->GetFrame();
578
579 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
580 // normally wxMenu::SendEvent() should be enough, if it doesn't work
581 // in wxGTK then we have a bug in wxMenu::GetInvokingWindow() which
582 // should be fixed instead of working around it here...
583 if (frame)
584 {
585 // If it is attached then let the frame send the event.
586 // Don't call frame->ProcessCommand(id) because it toggles
587 // checkable items and we've already done that above.
588 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
589 commandEvent.SetEventObject(frame);
590 if (item->IsCheckable())
591 commandEvent.SetInt(item->IsChecked());
592 commandEvent.SetEventObject(menu);
593
594 frame->GetEventHandler()->ProcessEvent(commandEvent);
595 }
596 else
597 {
598 // otherwise let the menu have it
599 menu->SendEvent(id, item->IsCheckable() ? item->IsChecked() : -1);
600 }
601 }
602 }
603
604 //-----------------------------------------------------------------------------
605 // "select"
606 //-----------------------------------------------------------------------------
607
608 extern "C" {
609 static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu )
610 {
611 if (g_isIdle) wxapp_install_idle_handler();
612
613 int id = menu->FindMenuIdByMenuItem(widget);
614
615 wxASSERT( id != -1 ); // should find it!
616
617 if (!menu->IsEnabled(id))
618 return;
619
620 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id );
621 event.SetEventObject( menu );
622
623 wxEvtHandler* handler = menu->GetEventHandler();
624 if (handler && handler->ProcessEvent(event))
625 return;
626
627 wxWindow *win = menu->GetInvokingWindow();
628 if (win) win->GetEventHandler()->ProcessEvent( event );
629 }
630 }
631
632 //-----------------------------------------------------------------------------
633 // "deselect"
634 //-----------------------------------------------------------------------------
635
636 extern "C" {
637 static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu )
638 {
639 if (g_isIdle) wxapp_install_idle_handler();
640
641 int id = menu->FindMenuIdByMenuItem(widget);
642
643 wxASSERT( id != -1 ); // should find it!
644
645 if (!menu->IsEnabled(id))
646 return;
647
648 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 );
649 event.SetEventObject( menu );
650
651 wxEvtHandler* handler = menu->GetEventHandler();
652 if (handler && handler->ProcessEvent(event))
653 return;
654
655 wxWindow *win = menu->GetInvokingWindow();
656 if (win)
657 win->GetEventHandler()->ProcessEvent( event );
658 }
659 }
660
661 //-----------------------------------------------------------------------------
662 // wxMenuItem
663 //-----------------------------------------------------------------------------
664
665 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
666
667 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
668 int id,
669 const wxString& name,
670 const wxString& help,
671 wxItemKind kind,
672 wxMenu *subMenu)
673 {
674 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
675 }
676
677 wxMenuItem::wxMenuItem(wxMenu *parentMenu,
678 int id,
679 const wxString& text,
680 const wxString& help,
681 wxItemKind kind,
682 wxMenu *subMenu)
683 : wxMenuItemBase(parentMenu, id, text, help, kind, subMenu)
684 {
685 Init(text);
686 }
687
688 wxMenuItem::wxMenuItem(wxMenu *parentMenu,
689 int id,
690 const wxString& text,
691 const wxString& help,
692 bool isCheckable,
693 wxMenu *subMenu)
694 : wxMenuItemBase(parentMenu, id, text, help,
695 isCheckable ? wxITEM_CHECK : wxITEM_NORMAL, subMenu)
696 {
697 Init(text);
698 }
699
700 void wxMenuItem::Init(const wxString& text)
701 {
702 m_labelWidget = (GtkWidget *) NULL;
703 m_menuItem = (GtkWidget *) NULL;
704
705 DoSetText(text);
706 }
707
708 wxMenuItem::~wxMenuItem()
709 {
710 // don't delete menu items, the menus take care of that
711 }
712
713 // return the menu item text without any menu accels
714 /* static */
715 wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
716 {
717 wxString label;
718
719 for ( const wxChar *pc = text.c_str(); *pc; pc++ )
720 {
721 if ( *pc == wxT('\t'))
722 break;
723
724 if ( *pc == wxT('_') )
725 {
726 // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx"
727 pc++;
728 label += *pc;
729 continue;
730 }
731
732 if ( *pc == wxT('\\') )
733 {
734 // GTK 2.0 escapes "xxx/xxx" to "xxx\/xxx"
735 pc++;
736 label += *pc;
737 continue;
738 }
739
740 if ( (*pc == wxT('&')) && (*(pc+1) != wxT('&')) )
741 {
742 // wxMSW escapes "&"
743 // "&" is doubled to indicate "&" instead of accelerator
744 continue;
745 }
746
747 label += *pc;
748 }
749
750 // wxPrintf( wxT("GetLabelFromText(): text %s label %s\n"), text.c_str(), label.c_str() );
751
752 return label;
753 }
754
755 void wxMenuItem::SetText( const wxString& str )
756 {
757 // Some optimization to avoid flicker
758 wxString oldLabel = m_text;
759 oldLabel = wxStripMenuCodes(oldLabel);
760 oldLabel.Replace(wxT("_"), wxT(""));
761 wxString label1 = wxStripMenuCodes(str);
762 wxString oldhotkey = GetHotKey(); // Store the old hotkey in Ctrl-foo format
763 wxCharBuffer oldbuf = wxGTK_CONV( GetGtkHotKey(*this) ); // and as <control>foo
764
765 DoSetText(str);
766
767 if (oldLabel == label1 &&
768 oldhotkey == GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
769 return;
770
771 if (m_menuItem)
772 {
773 GtkLabel *label;
774 if (m_labelWidget)
775 label = (GtkLabel*) m_labelWidget;
776 else
777 label = GTK_LABEL( GTK_BIN(m_menuItem)->child );
778
779 gtk_label_set_text_with_mnemonic( GTK_LABEL(label), wxGTK_CONV(m_text) );
780 }
781
782 guint accel_key;
783 GdkModifierType accel_mods;
784 gtk_accelerator_parse( (const char*) oldbuf, &accel_key, &accel_mods);
785 if (accel_key != 0)
786 {
787 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem),
788 m_parentMenu->m_accel,
789 accel_key,
790 accel_mods );
791 }
792
793 wxCharBuffer buf = wxGTK_CONV( GetGtkHotKey(*this) );
794 gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods);
795 if (accel_key != 0)
796 {
797 gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem),
798 "activate",
799 m_parentMenu->m_accel,
800 accel_key,
801 accel_mods,
802 GTK_ACCEL_VISIBLE);
803 }
804 }
805
806 // it's valid for this function to be called even if m_menuItem == NULL
807 void wxMenuItem::DoSetText( const wxString& str )
808 {
809 // '\t' is the deliminator indicating a hot key
810 m_text.Empty();
811 const wxChar *pc = str;
812 while ( (*pc != wxT('\0')) && (*pc != wxT('\t')) )
813 {
814 if ((*pc == wxT('&')) && (*(pc+1) == wxT('&')))
815 {
816 // "&" is doubled to indicate "&" instead of accelerator
817 ++pc;
818 m_text << wxT('&');
819 }
820 else if (*pc == wxT('&'))
821 {
822 m_text << wxT('_');
823 }
824 else if ( *pc == wxT('_') ) // escape underscores
825 {
826 m_text << wxT("__");
827 }
828 else
829 {
830 m_text << *pc;
831 }
832 ++pc;
833 }
834
835 m_hotKey = wxT("");
836
837 if(*pc == wxT('\t'))
838 {
839 pc++;
840 m_hotKey = pc;
841 }
842
843 // wxPrintf( wxT("DoSetText(): str %s m_text %s hotkey %s\n"), str.c_str(), m_text.c_str(), m_hotKey.c_str() );
844 }
845
846 #if wxUSE_ACCEL
847
848 wxAcceleratorEntry *wxMenuItem::GetAccel() const
849 {
850 if ( !GetHotKey() )
851 {
852 // nothing
853 return (wxAcceleratorEntry *)NULL;
854 }
855
856 // as wxGetAccelFromString() looks for TAB, insert a dummy one here
857 wxString label;
858 label << wxT('\t') << GetHotKey();
859
860 return wxGetAccelFromString(label);
861 }
862
863 #endif // wxUSE_ACCEL
864
865 void wxMenuItem::Check( bool check )
866 {
867 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
868
869 if (check == m_isChecked)
870 return;
871
872 wxMenuItemBase::Check( check );
873
874 switch ( GetKind() )
875 {
876 case wxITEM_CHECK:
877 case wxITEM_RADIO:
878 gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check );
879 break;
880
881 default:
882 wxFAIL_MSG( _T("can't check this item") );
883 }
884 }
885
886 void wxMenuItem::Enable( bool enable )
887 {
888 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
889
890 gtk_widget_set_sensitive( m_menuItem, enable );
891 wxMenuItemBase::Enable( enable );
892 }
893
894 bool wxMenuItem::IsChecked() const
895 {
896 wxCHECK_MSG( m_menuItem, FALSE, wxT("invalid menu item") );
897
898 wxCHECK_MSG( IsCheckable(), FALSE,
899 wxT("can't get state of uncheckable item!") );
900
901 return ((GtkCheckMenuItem*)m_menuItem)->active != 0;
902 }
903
904 //-----------------------------------------------------------------------------
905 // wxMenu
906 //-----------------------------------------------------------------------------
907
908 IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
909
910 void wxMenu::Init()
911 {
912 m_accel = gtk_accel_group_new();
913 m_menu = gtk_menu_new();
914 // NB: keep reference to the menu so that it is not destroyed behind
915 // our back by GTK+ e.g. when it is removed from menubar:
916 gtk_widget_ref(m_menu);
917
918 m_owner = (GtkWidget*) NULL;
919
920 // Tearoffs are entries, just like separators. So if we want this
921 // menu to be a tear-off one, we just append a tearoff entry
922 // immediately.
923 if ( m_style & wxMENU_TEAROFF )
924 {
925 GtkWidget *tearoff = gtk_tearoff_menu_item_new();
926
927 gtk_menu_append(GTK_MENU(m_menu), tearoff);
928 }
929
930 m_prevRadio = NULL;
931
932 // append the title as the very first entry if we have it
933 if ( !m_title.empty() )
934 {
935 Append(wxGTK_TITLE_ID, m_title);
936 AppendSeparator();
937 }
938 }
939
940 wxMenu::~wxMenu()
941 {
942 WX_CLEAR_LIST(wxMenuItemList, m_items);
943
944 if ( GTK_IS_WIDGET( m_menu ))
945 {
946 // see wxMenu::Init
947 gtk_widget_unref( m_menu );
948 // if the menu is inserted in another menu at this time, there was
949 // one more reference to it:
950 if ( m_owner )
951 gtk_widget_destroy( m_menu );
952 }
953 }
954
955 bool wxMenu::GtkAppend(wxMenuItem *mitem, int pos)
956 {
957 GtkWidget *menuItem;
958
959 wxString text;
960
961 if ( mitem->IsSeparator() )
962 {
963 menuItem = gtk_separator_menu_item_new();
964 }
965 else if (mitem->GetBitmap().Ok())
966 {
967 text = mitem->GetText();
968 const wxBitmap *bitmap = &mitem->GetBitmap();
969
970 menuItem = gtk_image_menu_item_new_with_mnemonic( wxGTK_CONV( text ) );
971
972 GtkWidget *image;
973 if (bitmap->HasPixbuf())
974 {
975 image = gtk_image_new_from_pixbuf(bitmap->GetPixbuf());
976 }
977 else
978 {
979 GdkPixmap *gdk_pixmap = bitmap->GetPixmap();
980 GdkBitmap *gdk_bitmap = bitmap->GetMask() ?
981 bitmap->GetMask()->GetBitmap() :
982 (GdkBitmap*) NULL;
983 image = gtk_image_new_from_pixmap( gdk_pixmap, gdk_bitmap );
984 }
985
986 gtk_widget_show(image);
987
988 gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM(menuItem), image );
989
990 m_prevRadio = NULL;
991 }
992 else // a normal item
993 {
994 // text has "_" instead of "&" after mitem->SetText() so don't use it
995 text = mitem->GetText() ;
996
997 switch ( mitem->GetKind() )
998 {
999 case wxITEM_CHECK:
1000 {
1001 menuItem = gtk_check_menu_item_new_with_mnemonic( wxGTK_CONV( text ) );
1002 m_prevRadio = NULL;
1003 break;
1004 }
1005
1006 case wxITEM_RADIO:
1007 {
1008 GSList *group = NULL;
1009 if ( m_prevRadio == NULL )
1010 {
1011 // start of a new radio group
1012 m_prevRadio = menuItem = gtk_radio_menu_item_new_with_mnemonic( group, wxGTK_CONV( text ) );
1013 }
1014 else // continue the radio group
1015 {
1016 group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (m_prevRadio));
1017 m_prevRadio = menuItem = gtk_radio_menu_item_new_with_mnemonic( group, wxGTK_CONV( text ) );
1018 }
1019 break;
1020 }
1021
1022 default:
1023 wxFAIL_MSG( _T("unexpected menu item kind") );
1024 // fall through
1025
1026 case wxITEM_NORMAL:
1027 {
1028 menuItem = gtk_menu_item_new_with_mnemonic( wxGTK_CONV( text ) );
1029 m_prevRadio = NULL;
1030 break;
1031 }
1032 }
1033
1034 }
1035
1036 guint accel_key;
1037 GdkModifierType accel_mods;
1038 wxCharBuffer buf = wxGTK_CONV( GetGtkHotKey(*mitem) );
1039
1040 // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetText().c_str(), GetGtkHotKey(*mitem).c_str() );
1041 gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods);
1042 if (accel_key != 0)
1043 {
1044 gtk_widget_add_accelerator (GTK_WIDGET(menuItem),
1045 "activate",
1046 m_accel,
1047 accel_key,
1048 accel_mods,
1049 GTK_ACCEL_VISIBLE);
1050 }
1051
1052 if (pos == -1)
1053 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu), menuItem);
1054 else
1055 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu), menuItem, pos);
1056
1057 gtk_widget_show( menuItem );
1058
1059 if ( !mitem->IsSeparator() )
1060 {
1061 wxASSERT_MSG( menuItem, wxT("invalid menuitem") );
1062
1063 g_signal_connect (menuItem, "select",
1064 G_CALLBACK (gtk_menu_hilight_callback), this);
1065 g_signal_connect (menuItem, "deselect",
1066 G_CALLBACK (gtk_menu_nolight_callback), this);
1067
1068 if ( mitem->IsSubMenu() && mitem->GetKind() != wxITEM_RADIO && mitem->GetKind() != wxITEM_CHECK )
1069 {
1070 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), mitem->GetSubMenu()->m_menu );
1071
1072 gtk_widget_show( mitem->GetSubMenu()->m_menu );
1073
1074 // if adding a submenu to a menu already existing in the menu bar, we
1075 // must set invoking window to allow processing events from this
1076 // submenu
1077 if ( m_invokingWindow )
1078 wxMenubarSetInvokingWindow(mitem->GetSubMenu(), m_invokingWindow);
1079 }
1080 else
1081 {
1082 g_signal_connect (menuItem, "activate",
1083 G_CALLBACK (gtk_menu_clicked_callback),
1084 this);
1085 }
1086 }
1087
1088 mitem->SetMenuItem(menuItem);
1089
1090 if (ms_locked)
1091 {
1092 // This doesn't even exist!
1093 // gtk_widget_lock_accelerators(mitem->GetMenuItem());
1094 }
1095
1096 return TRUE;
1097 }
1098
1099 wxMenuItem* wxMenu::DoAppend(wxMenuItem *mitem)
1100 {
1101 if (!GtkAppend(mitem))
1102 return NULL;
1103
1104 return wxMenuBase::DoAppend(mitem);
1105 }
1106
1107 wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
1108 {
1109 if ( !wxMenuBase::DoInsert(pos, item) )
1110 return NULL;
1111
1112 // TODO
1113 if ( !GtkAppend(item, (int)pos) )
1114 return NULL;
1115
1116 return item;
1117 }
1118
1119 wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
1120 {
1121 if ( !wxMenuBase::DoRemove(item) )
1122 return (wxMenuItem *)NULL;
1123
1124 // TODO: this code doesn't delete the item factory item and this seems
1125 // impossible as of GTK 1.2.6.
1126 gtk_widget_destroy( item->GetMenuItem() );
1127
1128 return item;
1129 }
1130
1131 int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
1132 {
1133 wxMenuItemList::compatibility_iterator node = m_items.GetFirst();
1134 while (node)
1135 {
1136 wxMenuItem *item = node->GetData();
1137 if (item->GetMenuItem() == menuItem)
1138 return item->GetId();
1139 node = node->GetNext();
1140 }
1141
1142 return wxNOT_FOUND;
1143 }
1144
1145 // ----------------------------------------------------------------------------
1146 // helpers
1147 // ----------------------------------------------------------------------------
1148
1149 #if wxUSE_ACCEL
1150
1151 static wxString GetGtkHotKey( const wxMenuItem& item )
1152 {
1153 wxString hotkey;
1154
1155 wxAcceleratorEntry *accel = item.GetAccel();
1156 if ( accel )
1157 {
1158 int flags = accel->GetFlags();
1159 if ( flags & wxACCEL_ALT )
1160 hotkey += wxT("<alt>");
1161 if ( flags & wxACCEL_CTRL )
1162 hotkey += wxT("<control>");
1163 if ( flags & wxACCEL_SHIFT )
1164 hotkey += wxT("<shift>");
1165
1166 int code = accel->GetKeyCode();
1167 switch ( code )
1168 {
1169 case WXK_F1:
1170 case WXK_F2:
1171 case WXK_F3:
1172 case WXK_F4:
1173 case WXK_F5:
1174 case WXK_F6:
1175 case WXK_F7:
1176 case WXK_F8:
1177 case WXK_F9:
1178 case WXK_F10:
1179 case WXK_F11:
1180 case WXK_F12:
1181 case WXK_F13:
1182 case WXK_F14:
1183 case WXK_F15:
1184 case WXK_F16:
1185 case WXK_F17:
1186 case WXK_F18:
1187 case WXK_F19:
1188 case WXK_F20:
1189 case WXK_F21:
1190 case WXK_F22:
1191 case WXK_F23:
1192 case WXK_F24:
1193 hotkey += wxString::Format(wxT("F%d"), code - WXK_F1 + 1);
1194 break;
1195
1196 // TODO: we should use gdk_keyval_name() (a.k.a.
1197 // XKeysymToString) here as well as hardcoding the keysym
1198 // names this might be not portable
1199 case WXK_INSERT:
1200 hotkey << wxT("Insert" );
1201 break;
1202 case WXK_DELETE:
1203 hotkey << wxT("Delete" );
1204 break;
1205 case WXK_UP:
1206 hotkey << wxT("Up" );
1207 break;
1208 case WXK_DOWN:
1209 hotkey << wxT("Down" );
1210 break;
1211 case WXK_PAGEUP:
1212 case WXK_PRIOR:
1213 hotkey << wxT("Prior" );
1214 break;
1215 case WXK_PAGEDOWN:
1216 case WXK_NEXT:
1217 hotkey << wxT("Next" );
1218 break;
1219 case WXK_LEFT:
1220 hotkey << wxT("Left" );
1221 break;
1222 case WXK_RIGHT:
1223 hotkey << wxT("Right" );
1224 break;
1225 case WXK_HOME:
1226 hotkey << wxT("Home" );
1227 break;
1228 case WXK_END:
1229 hotkey << wxT("End" );
1230 break;
1231 case WXK_RETURN:
1232 hotkey << wxT("Return" );
1233 break;
1234 case WXK_BACK:
1235 hotkey << wxT("BackSpace" );
1236 break;
1237 case WXK_TAB:
1238 hotkey << wxT("Tab" );
1239 break;
1240 case WXK_ESCAPE:
1241 hotkey << wxT("Esc" );
1242 break;
1243 case WXK_SPACE:
1244 hotkey << wxT("space" );
1245 break;
1246 case WXK_MULTIPLY:
1247 hotkey << wxT("Multiply" );
1248 break;
1249 case WXK_ADD:
1250 hotkey << wxT("Add" );
1251 break;
1252 case WXK_SEPARATOR:
1253 hotkey << wxT("Separator" );
1254 break;
1255 case WXK_SUBTRACT:
1256 hotkey << wxT("Subtract" );
1257 break;
1258 case WXK_DECIMAL:
1259 hotkey << wxT("Decimal" );
1260 break;
1261 case WXK_DIVIDE:
1262 hotkey << wxT("Divide" );
1263 break;
1264 case WXK_CANCEL:
1265 hotkey << wxT("Cancel" );
1266 break;
1267 case WXK_CLEAR:
1268 hotkey << wxT("Clear" );
1269 break;
1270 case WXK_MENU:
1271 hotkey << wxT("Menu" );
1272 break;
1273 case WXK_PAUSE:
1274 hotkey << wxT("Pause" );
1275 break;
1276 case WXK_CAPITAL:
1277 hotkey << wxT("Capital" );
1278 break;
1279 case WXK_SELECT:
1280 hotkey << wxT("Select" );
1281 break;
1282 case WXK_PRINT:
1283 hotkey << wxT("Print" );
1284 break;
1285 case WXK_EXECUTE:
1286 hotkey << wxT("Execute" );
1287 break;
1288 case WXK_SNAPSHOT:
1289 hotkey << wxT("Snapshot" );
1290 break;
1291 case WXK_HELP:
1292 hotkey << wxT("Help" );
1293 break;
1294 case WXK_NUMLOCK:
1295 hotkey << wxT("Num_Lock" );
1296 break;
1297 case WXK_SCROLL:
1298 hotkey << wxT("Scroll_Lock" );
1299 break;
1300 case WXK_NUMPAD_INSERT:
1301 hotkey << wxT("KP_Insert" );
1302 break;
1303 case WXK_NUMPAD_DELETE:
1304 hotkey << wxT("KP_Delete" );
1305 break;
1306 case WXK_NUMPAD_SPACE:
1307 hotkey << wxT("KP_Space" );
1308 break;
1309 case WXK_NUMPAD_TAB:
1310 hotkey << wxT("KP_Tab" );
1311 break;
1312 case WXK_NUMPAD_ENTER:
1313 hotkey << wxT("KP_Enter" );
1314 break;
1315 case WXK_NUMPAD_F1: case WXK_NUMPAD_F2: case WXK_NUMPAD_F3:
1316 case WXK_NUMPAD_F4:
1317 hotkey += wxString::Format(wxT("KP_F%d"), code - WXK_NUMPAD_F1 + 1);
1318 break;
1319 case WXK_NUMPAD_HOME:
1320 hotkey << wxT("KP_Home" );
1321 break;
1322 case WXK_NUMPAD_LEFT:
1323 hotkey << wxT("KP_Left" );
1324 break;
1325 case WXK_NUMPAD_UP:
1326 hotkey << wxT("KP_Up" );
1327 break;
1328 case WXK_NUMPAD_RIGHT:
1329 hotkey << wxT("KP_Right" );
1330 break;
1331 case WXK_NUMPAD_DOWN:
1332 hotkey << wxT("KP_Down" );
1333 break;
1334 case WXK_NUMPAD_PRIOR: case WXK_NUMPAD_PAGEUP:
1335 hotkey << wxT("KP_Prior" );
1336 break;
1337 case WXK_NUMPAD_NEXT: case WXK_NUMPAD_PAGEDOWN:
1338 hotkey << wxT("KP_Next" );
1339 break;
1340 case WXK_NUMPAD_END:
1341 hotkey << wxT("KP_End" );
1342 break;
1343 case WXK_NUMPAD_BEGIN:
1344 hotkey << wxT("KP_Begin" );
1345 break;
1346 case WXK_NUMPAD_EQUAL:
1347 hotkey << wxT("KP_Equal" );
1348 break;
1349 case WXK_NUMPAD_MULTIPLY:
1350 hotkey << wxT("KP_Multiply" );
1351 break;
1352 case WXK_NUMPAD_ADD:
1353 hotkey << wxT("KP_Add" );
1354 break;
1355 case WXK_NUMPAD_SEPARATOR:
1356 hotkey << wxT("KP_Separator" );
1357 break;
1358 case WXK_NUMPAD_SUBTRACT:
1359 hotkey << wxT("KP_Subtract" );
1360 break;
1361 case WXK_NUMPAD_DECIMAL:
1362 hotkey << wxT("KP_Decimal" );
1363 break;
1364 case WXK_NUMPAD_DIVIDE:
1365 hotkey << wxT("KP_Divide" );
1366 break;
1367 case WXK_NUMPAD0: case WXK_NUMPAD1: case WXK_NUMPAD2:
1368 case WXK_NUMPAD3: case WXK_NUMPAD4: case WXK_NUMPAD5:
1369 case WXK_NUMPAD6: case WXK_NUMPAD7: case WXK_NUMPAD8: case WXK_NUMPAD9:
1370 hotkey += wxString::Format(wxT("KP_%d"), code - WXK_NUMPAD0);
1371 break;
1372 case WXK_WINDOWS_LEFT:
1373 hotkey << wxT("Super_L" );
1374 break;
1375 case WXK_WINDOWS_RIGHT:
1376 hotkey << wxT("Super_R" );
1377 break;
1378 case WXK_WINDOWS_MENU:
1379 hotkey << wxT("Menu" );
1380 break;
1381 case WXK_COMMAND:
1382 hotkey << wxT("Command" );
1383 break;
1384 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
1385 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1386 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1387 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1388 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
1389 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1390 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1391 break;
1392 */
1393 // if there are any other keys wxGetAccelFromString() may
1394 // return, we should process them here
1395
1396 default:
1397 if ( code < 127 )
1398 {
1399 wxString name = wxGTK_CONV_BACK( gdk_keyval_name((guint)code) );
1400 if ( name )
1401 {
1402 hotkey << name;
1403 break;
1404 }
1405 }
1406
1407 wxFAIL_MSG( wxT("unknown keyboard accel") );
1408 }
1409
1410 delete accel;
1411 }
1412
1413 return hotkey;
1414 }
1415
1416 #endif // wxUSE_ACCEL
1417
1418 // ----------------------------------------------------------------------------
1419 // Pop-up menu stuff
1420 // ----------------------------------------------------------------------------
1421
1422 #if wxUSE_MENUS_NATIVE
1423
1424 extern "C"
1425 void gtk_pop_hide_callback( GtkWidget *WXUNUSED(widget), bool* is_waiting )
1426 {
1427 *is_waiting = FALSE;
1428 }
1429
1430 void SetInvokingWindow( wxMenu *menu, wxWindow* win )
1431 {
1432 menu->SetInvokingWindow( win );
1433
1434 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
1435 while (node)
1436 {
1437 wxMenuItem *menuitem = node->GetData();
1438 if (menuitem->IsSubMenu())
1439 {
1440 SetInvokingWindow( menuitem->GetSubMenu(), win );
1441 }
1442
1443 node = node->GetNext();
1444 }
1445 }
1446
1447 extern "C"
1448 void wxPopupMenuPositionCallback( GtkMenu *menu,
1449 gint *x, gint *y,
1450 gboolean * WXUNUSED(whatever),
1451 gpointer user_data )
1452 {
1453 // ensure that the menu appears entirely on screen
1454 GtkRequisition req;
1455 gtk_widget_get_child_requisition(GTK_WIDGET(menu), &req);
1456
1457 wxSize sizeScreen = wxGetDisplaySize();
1458 wxPoint *pos = (wxPoint*)user_data;
1459
1460 gint xmax = sizeScreen.x - req.width,
1461 ymax = sizeScreen.y - req.height;
1462
1463 *x = pos->x < xmax ? pos->x : xmax;
1464 *y = pos->y < ymax ? pos->y : ymax;
1465 }
1466
1467 bool wxWindowGTK::DoPopupMenu( wxMenu *menu, int x, int y )
1468 {
1469 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid window") );
1470
1471 wxCHECK_MSG( menu != NULL, false, wxT("invalid popup-menu") );
1472
1473 // NOTE: if you change this code, you need to update
1474 // the same code in taskbar.cpp as well. This
1475 // is ugly code duplication, I know.
1476
1477 SetInvokingWindow( menu, this );
1478
1479 menu->UpdateUI();
1480
1481 bool is_waiting = true;
1482
1483 gulong handler = g_signal_connect (menu->m_menu, "hide",
1484 G_CALLBACK (gtk_pop_hide_callback),
1485 &is_waiting);
1486
1487 wxPoint pos;
1488 gpointer userdata;
1489 GtkMenuPositionFunc posfunc;
1490 if ( x == -1 && y == -1 )
1491 {
1492 // use GTK's default positioning algorithm
1493 userdata = NULL;
1494 posfunc = NULL;
1495 }
1496 else
1497 {
1498 pos = ClientToScreen(wxPoint(x, y));
1499 userdata = &pos;
1500 posfunc = wxPopupMenuPositionCallback;
1501 }
1502
1503 wxMenuEvent eventOpen(wxEVT_MENU_OPEN, -1, menu);
1504 DoCommonMenuCallbackCode(menu, eventOpen);
1505
1506 gtk_menu_popup(
1507 GTK_MENU(menu->m_menu),
1508 (GtkWidget *) NULL, // parent menu shell
1509 (GtkWidget *) NULL, // parent menu item
1510 posfunc, // function to position it
1511 userdata, // client data
1512 0, // button used to activate it
1513 gtk_get_current_event_time()
1514 );
1515
1516 while (is_waiting)
1517 {
1518 gtk_main_iteration();
1519 }
1520
1521 g_signal_handler_disconnect (menu->m_menu, handler);
1522
1523 wxMenuEvent eventClose(wxEVT_MENU_CLOSE, -1, menu);
1524 DoCommonMenuCallbackCode(menu, eventClose);
1525
1526 return true;
1527 }
1528
1529 #endif // wxUSE_MENUS_NATIVE
1530