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