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