]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/menu.cpp
Correct printf parameter mismatch in wxWndProc.
[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(wxConvertFromGTKToWXLabel(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 wxConvertFromGTKToWXLabel(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 bool wxMenu::GtkAppend(wxMenuItem *mitem, int pos)
968 {
969 GtkWidget *menuItem;
970
971 wxString text;
972 GtkLabel* label = NULL;
973
974 if ( mitem->IsSeparator() )
975 {
976 // TODO
977 menuItem = gtk_menu_item_new();
978 }
979 else if (mitem->GetBitmap().Ok())
980 {
981 text = mitem->wxMenuItemBase::GetItemLabel();
982 const wxBitmap *bitmap = &mitem->GetBitmap();
983
984 // TODO
985 wxUnusedVar(bitmap);
986 menuItem = gtk_menu_item_new_with_label( wxGTK_CONV( text ) );
987 label = GTK_LABEL( GTK_BIN(menuItem)->child );
988
989 m_prevRadio = NULL;
990 }
991 else // a normal item
992 {
993 // text has "_" instead of "&" after mitem->SetItemLabel() so don't use it
994 text = mitem->wxMenuItemBase::GetItemLabel() ;
995
996 switch ( mitem->GetKind() )
997 {
998 case wxITEM_CHECK:
999 {
1000 menuItem = gtk_check_menu_item_new_with_label( wxGTK_CONV( text ) );
1001 label = GTK_LABEL( GTK_BIN(menuItem)->child );
1002 // set new text
1003 gtk_label_set_text( label, wxGTK_CONV( text ) );
1004 m_prevRadio = NULL;
1005 break;
1006 }
1007
1008 case wxITEM_RADIO:
1009 {
1010 GSList *group = NULL;
1011 if ( m_prevRadio == NULL )
1012 {
1013 // start of a new radio group
1014 m_prevRadio = menuItem = gtk_radio_menu_item_new_with_label( group, wxGTK_CONV( text ) );
1015 label = GTK_LABEL( GTK_BIN(menuItem)->child );
1016 // set new text
1017 gtk_label_set_text( label, wxGTK_CONV( text ) );
1018 }
1019 else // continue the radio group
1020 {
1021 group = gtk_radio_menu_item_group (GTK_RADIO_MENU_ITEM (m_prevRadio));
1022 m_prevRadio = menuItem = gtk_radio_menu_item_new_with_label( group, wxGTK_CONV( text ) );
1023 label = GTK_LABEL( GTK_BIN(menuItem)->child );
1024 }
1025 break;
1026 }
1027
1028 default:
1029 wxFAIL_MSG( wxT("unexpected menu item kind") );
1030 // fall through
1031
1032 case wxITEM_NORMAL:
1033 {
1034 menuItem = gtk_menu_item_new_with_label( wxGTK_CONV( text ) );
1035 label = GTK_LABEL( GTK_BIN(menuItem)->child );
1036 m_prevRadio = NULL;
1037 break;
1038 }
1039 }
1040
1041 }
1042
1043 guint accel_key;
1044 GdkModifierType accel_mods;
1045 wxCharBuffer buf = wxGTK_CONV( GetGtkHotKey(*mitem) );
1046
1047 // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetItemLabel().c_str(), GetGtkHotKey(*mitem).c_str() );
1048 gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods);
1049 if (accel_key != 0)
1050 {
1051 gtk_widget_add_accelerator (GTK_WIDGET(menuItem),
1052 "activate",
1053 m_accel,
1054 accel_key,
1055 accel_mods,
1056 GTK_ACCEL_VISIBLE);
1057 }
1058
1059 if (pos == -1)
1060 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu), menuItem);
1061 else
1062 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu), menuItem, pos);
1063
1064 gtk_widget_show( menuItem );
1065
1066 if ( !mitem->IsSeparator() )
1067 {
1068 wxASSERT_MSG( menuItem, wxT("invalid menuitem") );
1069
1070 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
1071 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
1072 (gpointer)this );
1073
1074 gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
1075 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
1076 (gpointer)this );
1077
1078 if ( mitem->IsSubMenu() && mitem->GetKind() != wxITEM_RADIO && mitem->GetKind() != wxITEM_CHECK )
1079 {
1080 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), mitem->GetSubMenu()->m_menu );
1081
1082 gtk_widget_show( mitem->GetSubMenu()->m_menu );
1083 }
1084 else
1085 {
1086 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
1087 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
1088 (gpointer)this );
1089 }
1090
1091 guint accel_key = gtk_label_parse_uline (GTK_LABEL(label), wxGTK_CONV( text ) );
1092 if (accel_key != GDK_VoidSymbol)
1093 {
1094 gtk_widget_add_accelerator (menuItem,
1095 "activate_item",
1096 gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menu)),
1097 accel_key,
1098 GDK_MOD1_MASK,
1099 GTK_ACCEL_LOCKED);
1100 }
1101 }
1102
1103 mitem->SetMenuItem(menuItem);
1104
1105 if (ms_locked)
1106 {
1107 // This doesn't even exist!
1108 // gtk_widget_lock_accelerators(mitem->GetMenuItem());
1109 }
1110
1111 return true;
1112 }
1113
1114 wxMenuItem* wxMenu::DoAppend(wxMenuItem *mitem)
1115 {
1116 if (!GtkAppend(mitem))
1117 return NULL;
1118
1119 return wxMenuBase::DoAppend(mitem);
1120 }
1121
1122 wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
1123 {
1124 if ( !wxMenuBase::DoInsert(pos, item) )
1125 return NULL;
1126
1127 // TODO
1128 if ( !GtkAppend(item, (int)pos) )
1129 return NULL;
1130
1131 return item;
1132 }
1133
1134 wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
1135 {
1136 if ( !wxMenuBase::DoRemove(item) )
1137 return NULL;
1138
1139 // TODO: this code doesn't delete the item factory item and this seems
1140 // impossible as of GTK 1.2.6.
1141 gtk_widget_destroy( item->GetMenuItem() );
1142
1143 return item;
1144 }
1145
1146 int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
1147 {
1148 wxMenuItemList::compatibility_iterator node = m_items.GetFirst();
1149 while (node)
1150 {
1151 wxMenuItem *item = node->GetData();
1152 if (item->GetMenuItem() == menuItem)
1153 return item->GetId();
1154 node = node->GetNext();
1155 }
1156
1157 return wxNOT_FOUND;
1158 }
1159
1160 // ----------------------------------------------------------------------------
1161 // helpers
1162 // ----------------------------------------------------------------------------
1163
1164 #if wxUSE_ACCEL
1165
1166 static wxString GetGtkHotKey( const wxMenuItem& item )
1167 {
1168 wxString hotkey;
1169
1170 wxAcceleratorEntry *accel = item.GetAccel();
1171 if ( accel )
1172 {
1173 int flags = accel->GetFlags();
1174 if ( flags & wxACCEL_ALT )
1175 hotkey += wxT("<alt>");
1176 if ( flags & wxACCEL_CTRL )
1177 hotkey += wxT("<control>");
1178 if ( flags & wxACCEL_SHIFT )
1179 hotkey += wxT("<shift>");
1180
1181 int code = accel->GetKeyCode();
1182 switch ( code )
1183 {
1184 case WXK_F1:
1185 case WXK_F2:
1186 case WXK_F3:
1187 case WXK_F4:
1188 case WXK_F5:
1189 case WXK_F6:
1190 case WXK_F7:
1191 case WXK_F8:
1192 case WXK_F9:
1193 case WXK_F10:
1194 case WXK_F11:
1195 case WXK_F12:
1196 case WXK_F13:
1197 case WXK_F14:
1198 case WXK_F15:
1199 case WXK_F16:
1200 case WXK_F17:
1201 case WXK_F18:
1202 case WXK_F19:
1203 case WXK_F20:
1204 case WXK_F21:
1205 case WXK_F22:
1206 case WXK_F23:
1207 case WXK_F24:
1208 hotkey += wxString::Format(wxT("F%d"), code - WXK_F1 + 1);
1209 break;
1210
1211 // TODO: we should use gdk_keyval_name() (a.k.a.
1212 // XKeysymToString) here as well as hardcoding the keysym
1213 // names this might be not portable
1214 case WXK_INSERT:
1215 hotkey << wxT("Insert" );
1216 break;
1217 case WXK_DELETE:
1218 hotkey << wxT("Delete" );
1219 break;
1220 case WXK_UP:
1221 hotkey << wxT("Up" );
1222 break;
1223 case WXK_DOWN:
1224 hotkey << wxT("Down" );
1225 break;
1226 case WXK_PAGEUP:
1227 hotkey << wxT("Page_Up" );
1228 break;
1229 case WXK_PAGEDOWN:
1230 hotkey << wxT("Page_Down" );
1231 break;
1232 case WXK_LEFT:
1233 hotkey << wxT("Left" );
1234 break;
1235 case WXK_RIGHT:
1236 hotkey << wxT("Right" );
1237 break;
1238 case WXK_HOME:
1239 hotkey << wxT("Home" );
1240 break;
1241 case WXK_END:
1242 hotkey << wxT("End" );
1243 break;
1244 case WXK_RETURN:
1245 hotkey << wxT("Return" );
1246 break;
1247 case WXK_BACK:
1248 hotkey << wxT("BackSpace" );
1249 break;
1250 case WXK_TAB:
1251 hotkey << wxT("Tab" );
1252 break;
1253 case WXK_ESCAPE:
1254 hotkey << wxT("Esc" );
1255 break;
1256 case WXK_SPACE:
1257 hotkey << wxT("space" );
1258 break;
1259 case WXK_MULTIPLY:
1260 hotkey << wxT("Multiply" );
1261 break;
1262 case WXK_ADD:
1263 hotkey << wxT("Add" );
1264 break;
1265 case WXK_SEPARATOR:
1266 hotkey << wxT("Separator" );
1267 break;
1268 case WXK_SUBTRACT:
1269 hotkey << wxT("Subtract" );
1270 break;
1271 case WXK_DECIMAL:
1272 hotkey << wxT("Decimal" );
1273 break;
1274 case WXK_DIVIDE:
1275 hotkey << wxT("Divide" );
1276 break;
1277 case WXK_CANCEL:
1278 hotkey << wxT("Cancel" );
1279 break;
1280 case WXK_CLEAR:
1281 hotkey << wxT("Clear" );
1282 break;
1283 case WXK_MENU:
1284 hotkey << wxT("Menu" );
1285 break;
1286 case WXK_PAUSE:
1287 hotkey << wxT("Pause" );
1288 break;
1289 case WXK_CAPITAL:
1290 hotkey << wxT("Capital" );
1291 break;
1292 case WXK_SELECT:
1293 hotkey << wxT("Select" );
1294 break;
1295 case WXK_PRINT:
1296 hotkey << wxT("Print" );
1297 break;
1298 case WXK_EXECUTE:
1299 hotkey << wxT("Execute" );
1300 break;
1301 case WXK_SNAPSHOT:
1302 hotkey << wxT("Snapshot" );
1303 break;
1304 case WXK_HELP:
1305 hotkey << wxT("Help" );
1306 break;
1307 case WXK_NUMLOCK:
1308 hotkey << wxT("Num_Lock" );
1309 break;
1310 case WXK_SCROLL:
1311 hotkey << wxT("Scroll_Lock" );
1312 break;
1313 case WXK_NUMPAD_INSERT:
1314 hotkey << wxT("KP_Insert" );
1315 break;
1316 case WXK_NUMPAD_DELETE:
1317 hotkey << wxT("KP_Delete" );
1318 break;
1319 case WXK_NUMPAD_SPACE:
1320 hotkey << wxT("KP_Space" );
1321 break;
1322 case WXK_NUMPAD_TAB:
1323 hotkey << wxT("KP_Tab" );
1324 break;
1325 case WXK_NUMPAD_ENTER:
1326 hotkey << wxT("KP_Enter" );
1327 break;
1328 case WXK_NUMPAD_F1: case WXK_NUMPAD_F2: case WXK_NUMPAD_F3:
1329 case WXK_NUMPAD_F4:
1330 hotkey += wxString::Format(wxT("KP_F%d"), code - WXK_NUMPAD_F1 + 1);
1331 break;
1332 case WXK_NUMPAD_HOME:
1333 hotkey << wxT("KP_Home" );
1334 break;
1335 case WXK_NUMPAD_LEFT:
1336 hotkey << wxT("KP_Left" );
1337 break;
1338 case WXK_NUMPAD_UP:
1339 hotkey << wxT("KP_Up" );
1340 break;
1341 case WXK_NUMPAD_RIGHT:
1342 hotkey << wxT("KP_Right" );
1343 break;
1344 case WXK_NUMPAD_DOWN:
1345 hotkey << wxT("KP_Down" );
1346 break;
1347 case WXK_NUMPAD_PAGEUP:
1348 hotkey << wxT("KP_Page_Up" );
1349 break;
1350 case WXK_NUMPAD_PAGEDOWN:
1351 hotkey << wxT("KP_Page_Down" );
1352 break;
1353 case WXK_NUMPAD_END:
1354 hotkey << wxT("KP_End" );
1355 break;
1356 case WXK_NUMPAD_BEGIN:
1357 hotkey << wxT("KP_Begin" );
1358 break;
1359 case WXK_NUMPAD_EQUAL:
1360 hotkey << wxT("KP_Equal" );
1361 break;
1362 case WXK_NUMPAD_MULTIPLY:
1363 hotkey << wxT("KP_Multiply" );
1364 break;
1365 case WXK_NUMPAD_ADD:
1366 hotkey << wxT("KP_Add" );
1367 break;
1368 case WXK_NUMPAD_SEPARATOR:
1369 hotkey << wxT("KP_Separator" );
1370 break;
1371 case WXK_NUMPAD_SUBTRACT:
1372 hotkey << wxT("KP_Subtract" );
1373 break;
1374 case WXK_NUMPAD_DECIMAL:
1375 hotkey << wxT("KP_Decimal" );
1376 break;
1377 case WXK_NUMPAD_DIVIDE:
1378 hotkey << wxT("KP_Divide" );
1379 break;
1380 case WXK_NUMPAD0: case WXK_NUMPAD1: case WXK_NUMPAD2:
1381 case WXK_NUMPAD3: case WXK_NUMPAD4: case WXK_NUMPAD5:
1382 case WXK_NUMPAD6: case WXK_NUMPAD7: case WXK_NUMPAD8: case WXK_NUMPAD9:
1383 hotkey += wxString::Format(wxT("KP_%d"), code - WXK_NUMPAD0);
1384 break;
1385 case WXK_WINDOWS_LEFT:
1386 hotkey << wxT("Super_L" );
1387 break;
1388 case WXK_WINDOWS_RIGHT:
1389 hotkey << wxT("Super_R" );
1390 break;
1391 case WXK_WINDOWS_MENU:
1392 hotkey << wxT("Menu" );
1393 break;
1394 case WXK_COMMAND:
1395 hotkey << wxT("Command" );
1396 break;
1397 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
1398 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1399 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1400 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1401 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
1402 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1403 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1404 break;
1405 */
1406 // if there are any other keys wxAcceleratorEntry::Create() may
1407 // return, we should process them here
1408
1409 default:
1410 if ( code < 127 )
1411 {
1412 wxString name = wxGTK_CONV_BACK( gdk_keyval_name((guint)code) );
1413 if ( !name.empty() )
1414 {
1415 hotkey << name;
1416 break;
1417 }
1418 }
1419
1420 wxFAIL_MSG( wxT("unknown keyboard accel") );
1421 }
1422
1423 delete accel;
1424 }
1425
1426 return hotkey;
1427 }
1428
1429 #endif // wxUSE_ACCEL
1430
1431 // ----------------------------------------------------------------------------
1432 // Pop-up menu stuff
1433 // ----------------------------------------------------------------------------
1434
1435 #if wxUSE_MENUS_NATIVE
1436
1437 extern "C" WXDLLIMPEXP_CORE
1438 void gtk_pop_hide_callback( GtkWidget *WXUNUSED(widget), bool* is_waiting )
1439 {
1440 *is_waiting = false;
1441 }
1442
1443 extern "C" WXDLLIMPEXP_CORE
1444 void wxPopupMenuPositionCallback( GtkMenu *menu,
1445 gint *x, gint *y,
1446 gpointer user_data )
1447 {
1448 // ensure that the menu appears entirely on screen
1449 GtkRequisition req;
1450 gtk_widget_get_child_requisition(GTK_WIDGET(menu), &req);
1451
1452 wxSize sizeScreen = wxGetDisplaySize();
1453 wxPoint *pos = (wxPoint*)user_data;
1454
1455 gint xmax = sizeScreen.x - req.width,
1456 ymax = sizeScreen.y - req.height;
1457
1458 *x = pos->x < xmax ? pos->x : xmax;
1459 *y = pos->y < ymax ? pos->y : ymax;
1460 }
1461
1462 bool wxWindowGTK::DoPopupMenu( wxMenu *menu, int x, int y )
1463 {
1464 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid window") );
1465
1466 wxCHECK_MSG( menu != NULL, false, wxT("invalid popup-menu") );
1467
1468 // NOTE: if you change this code, you need to update
1469 // the same code in taskbar.cpp as well. This
1470 // is ugly code duplication, I know.
1471
1472 menu->UpdateUI();
1473
1474 bool is_waiting = true;
1475
1476 gulong handler = gtk_signal_connect( GTK_OBJECT(menu->m_menu),
1477 "hide",
1478 GTK_SIGNAL_FUNC(gtk_pop_hide_callback),
1479 (gpointer)&is_waiting );
1480
1481 wxPoint pos;
1482 gpointer userdata;
1483 GtkMenuPositionFunc posfunc;
1484 if ( x == -1 && y == -1 )
1485 {
1486 // use GTK's default positioning algorithm
1487 userdata = NULL;
1488 posfunc = NULL;
1489 }
1490 else
1491 {
1492 pos = ClientToScreen(wxPoint(x, y));
1493 userdata = &pos;
1494 posfunc = wxPopupMenuPositionCallback;
1495 }
1496
1497 wxMenuEvent eventOpen(wxEVT_MENU_OPEN, -1, menu);
1498 DoCommonMenuCallbackCode(menu, eventOpen);
1499
1500 gtk_menu_popup(
1501 GTK_MENU(menu->m_menu),
1502 NULL, // parent menu shell
1503 NULL, // parent menu item
1504 posfunc, // function to position it
1505 userdata, // client data
1506 0, // button used to activate it
1507 wxGtkTimeLastClick // the time of activation
1508 );
1509
1510 while (is_waiting)
1511 {
1512 gtk_main_iteration();
1513 }
1514
1515 gtk_signal_disconnect(GTK_OBJECT(menu->m_menu), handler);
1516
1517 wxMenuEvent eventClose(wxEVT_MENU_CLOSE, -1, menu);
1518 DoCommonMenuCallbackCode(menu, eventClose);
1519
1520 return true;
1521 }
1522
1523 #endif // wxUSE_MENUS_NATIVE