]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/menu.cpp
Add missing WXK constants for the control keys
[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 #include "wx/gtk1/private/mnemonics.h"
29
30 #include <gdk/gdkkeysyms.h>
31
32 #define ACCEL_OBJECT GtkObject
33 #define ACCEL_OBJECTS(a) (a)->attach_objects
34 #define ACCEL_OBJ_CAST(obj) GTK_OBJECT(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 // defined in window.cpp
40 extern guint32 wxGtkTimeLastClick;
41
42 //-----------------------------------------------------------------------------
43 // idle system
44 //-----------------------------------------------------------------------------
45
46 extern void wxapp_install_idle_handler();
47 extern bool g_isIdle;
48
49 #if wxUSE_ACCEL
50 static wxString GetGtkHotKey( const wxMenuItem& item );
51 #endif
52
53 //-----------------------------------------------------------------------------
54 // idle system
55 //-----------------------------------------------------------------------------
56
57 static wxString wxReplaceUnderscore( const wxString& title )
58 {
59 const wxChar *pc;
60
61 // GTK 1.2 wants to have "_" instead of "&" for accelerators
62 wxString str;
63 pc = title;
64 while (*pc != wxT('\0'))
65 {
66 if ((*pc == wxT('&')) && (*(pc+1) == wxT('&')))
67 {
68 // "&" is doubled to indicate "&" instead of accelerator
69 ++pc;
70 str << wxT('&');
71 }
72 else if (*pc == wxT('&'))
73 {
74 str << wxT('_');
75 }
76 else
77 {
78 if ( *pc == wxT('_') )
79 {
80 // underscores must be doubled to prevent them from being
81 // interpreted as accelerator character prefix by GTK
82 str << *pc;
83 }
84
85 str << *pc;
86 }
87 ++pc;
88 }
89
90 // wxPrintf( wxT("before %s after %s\n"), title.c_str(), str.c_str() );
91
92 return str;
93 }
94
95 static wxString wxConvertFromGTKToWXLabel(const wxString& gtkLabel)
96 {
97 wxString label;
98 for ( const wxChar *pc = gtkLabel.c_str(); *pc; pc++ )
99 {
100 // '_' is the escape character for GTK+.
101
102 if ( *pc == wxT('_') && *(pc+1) == wxT('_'))
103 {
104 // An underscore was escaped.
105 label += wxT('_');
106 pc++;
107 }
108 else if ( *pc == wxT('_') )
109 {
110 // Convert GTK+ hotkey symbol to wxWidgets/Windows standard
111 label += wxT('&');
112 }
113 else if ( *pc == wxT('&') )
114 {
115 // Double the ampersand to escape it as far as wxWidgets is concerned
116 label += wxT("&&");
117 }
118 else
119 {
120 // don't remove ampersands '&' since if we have them in the menu title
121 // it means that they were doubled to indicate "&" instead of accelerator
122 label += *pc;
123 }
124 }
125
126 return label;
127 }
128
129
130 //-----------------------------------------------------------------------------
131 // activate message from GTK
132 //-----------------------------------------------------------------------------
133
134 static void DoCommonMenuCallbackCode(wxMenu *menu, wxMenuEvent& event)
135 {
136 if (g_isIdle)
137 wxapp_install_idle_handler();
138
139 event.SetEventObject( menu );
140
141 wxEvtHandler* handler = menu->GetEventHandler();
142 if (handler && handler->ProcessEvent(event))
143 return;
144
145 wxWindow *win = menu->GetWindow();
146 if (win)
147 win->HandleWindowEvent( event );
148 }
149
150 extern "C" {
151
152 static void gtk_menu_open_callback( GtkWidget *WXUNUSED(widget), wxMenu *menu )
153 {
154 wxMenuEvent event(wxEVT_MENU_OPEN, -1, menu);
155
156 DoCommonMenuCallbackCode(menu, event);
157 }
158
159 static void gtk_menu_close_callback( GtkWidget *WXUNUSED(widget), wxMenuBar *menubar )
160 {
161 if ( !menubar->GetMenuCount() )
162 {
163 // if menubar is empty we can't call GetMenu(0) below
164 return;
165 }
166
167 wxMenuEvent event( wxEVT_MENU_CLOSE, -1, NULL );
168
169 DoCommonMenuCallbackCode(menubar->GetMenu(0), event);
170 }
171
172 }
173
174 //-----------------------------------------------------------------------------
175 // wxMenuBar
176 //-----------------------------------------------------------------------------
177
178 void wxMenuBar::Init(size_t n, wxMenu *menus[], const wxString titles[], long style)
179 {
180 // the parent window is known after wxFrame::SetMenu()
181 m_needParent = false;
182 m_style = style;
183
184 if (!PreCreation( NULL, wxDefaultPosition, wxDefaultSize ) ||
185 !CreateBase( NULL, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, wxT("menubar") ))
186 {
187 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
188 return;
189 }
190
191 m_menubar = gtk_menu_bar_new();
192 m_accel = gtk_accel_group_new();
193
194 if (style & wxMB_DOCKABLE)
195 {
196 m_widget = gtk_handle_box_new();
197 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_menubar) );
198 gtk_widget_show( GTK_WIDGET(m_menubar) );
199 }
200 else
201 {
202 m_widget = GTK_WIDGET(m_menubar);
203 }
204
205 PostCreation();
206
207 ApplyWidgetStyle();
208
209 for (size_t i = 0; i < n; ++i )
210 Append(menus[i], titles[i]);
211
212 // VZ: for some reason connecting to menus "deactivate" doesn't work (we
213 // don't get it when the menu is dismissed by clicking outside the
214 // toolbar) so we connect to the global one, even if it means that we
215 // can't pass the menu which was closed in wxMenuEvent object
216 gtk_signal_connect( GTK_OBJECT(GTK_MENU_SHELL(m_menubar)),
217 "deactivate",
218 GTK_SIGNAL_FUNC(gtk_menu_close_callback),
219 (gpointer)this );
220
221 }
222
223 wxMenuBar::wxMenuBar(size_t n, wxMenu *menus[], const wxString titles[], long style)
224 {
225 Init(n, menus, titles, style);
226 }
227
228 wxMenuBar::wxMenuBar(long style)
229 {
230 Init(0, NULL, NULL, style);
231 }
232
233 wxMenuBar::wxMenuBar()
234 {
235 Init(0, NULL, NULL, 0);
236 }
237
238 wxMenuBar::~wxMenuBar()
239 {
240 }
241
242 static void DetachFromFrame( wxMenu *menu, wxWindow *win )
243 {
244 wxWindow *top_frame = win;
245 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
246 top_frame = top_frame->GetParent();
247
248 // support for native hot keys
249 gtk_accel_group_detach( menu->m_accel, ACCEL_OBJ_CAST(top_frame->m_widget) );
250
251 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
252 while (node)
253 {
254 wxMenuItem *menuitem = node->GetData();
255 if (menuitem->IsSubMenu())
256 DetachFromFrame( menuitem->GetSubMenu(), win );
257 node = node->GetNext();
258 }
259 }
260
261 static void AttachToFrame( wxMenu *menu, wxWindow *win )
262 {
263 wxWindow *top_frame = win;
264 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
265 top_frame = top_frame->GetParent();
266
267 // support for native hot keys
268 ACCEL_OBJECT *obj = ACCEL_OBJ_CAST(top_frame->m_widget);
269 if ( !g_slist_find( ACCEL_OBJECTS(menu->m_accel), obj ) )
270 gtk_accel_group_attach( menu->m_accel, obj );
271
272 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
273 while (node)
274 {
275 wxMenuItem *menuitem = node->GetData();
276 if (menuitem->IsSubMenu())
277 AttachToFrame( menuitem->GetSubMenu(), win );
278 node = node->GetNext();
279 }
280 }
281
282 void wxMenuBar::Attach( wxFrame *win )
283 {
284 wxMenuBarBase::Attach(win);
285
286 wxWindow *top_frame = win;
287 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
288 top_frame = top_frame->GetParent();
289
290 // support for native key accelerators indicated by underscroes
291 ACCEL_OBJECT *obj = ACCEL_OBJ_CAST(top_frame->m_widget);
292 if ( !g_slist_find( ACCEL_OBJECTS(m_accel), obj ) )
293 gtk_accel_group_attach( m_accel, obj );
294
295 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
296 while (node)
297 {
298 wxMenu *menu = node->GetData();
299 AttachToFrame( menu, win );
300 node = node->GetNext();
301 }
302 }
303
304 void wxMenuBar::Detach()
305 {
306 wxWindow *top_frame = m_menuBarFrame;
307 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
308 top_frame = top_frame->GetParent();
309
310 // support for native key accelerators indicated by underscroes
311 gtk_accel_group_detach( m_accel, ACCEL_OBJ_CAST(top_frame->m_widget) );
312
313 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
314 while (node)
315 {
316 wxMenu *menu = node->GetData();
317 DetachFromFrame( menu, top_frame );
318 node = node->GetNext();
319 }
320
321 wxMenuBarBase::Detach();
322 }
323
324 bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
325 {
326 if ( !wxMenuBarBase::Append( menu, title ) )
327 return false;
328
329 return GtkAppend(menu, title);
330 }
331
332 bool wxMenuBar::GtkAppend(wxMenu *menu, const wxString& title, int pos)
333 {
334 wxString str( wxReplaceUnderscore( title ) );
335
336 // This doesn't have much effect right now.
337 menu->SetTitle( str );
338
339 // The "m_owner" is the "menu item"
340 menu->m_owner = gtk_menu_item_new_with_label( wxGTK_CONV( str ) );
341 GtkLabel *label = GTK_LABEL( GTK_BIN(menu->m_owner)->child );
342 // set new text
343 gtk_label_set_text( label, wxGTK_CONV( str ) );
344 // reparse key accel
345 guint accel_key = gtk_label_parse_uline (GTK_LABEL(label), wxGTK_CONV( str ) );
346 if (accel_key != GDK_VoidSymbol)
347 {
348 gtk_widget_add_accelerator (menu->m_owner,
349 "activate_item",
350 m_accel, //gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menubar)),
351 accel_key,
352 GDK_MOD1_MASK,
353 GTK_ACCEL_LOCKED);
354 }
355
356 gtk_widget_show( menu->m_owner );
357
358 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu );
359
360 if (pos == -1)
361 gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar), menu->m_owner );
362 else
363 gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar), menu->m_owner, pos );
364
365 gtk_signal_connect( GTK_OBJECT(menu->m_owner), "activate",
366 GTK_SIGNAL_FUNC(gtk_menu_open_callback),
367 (gpointer)menu );
368
369 if (m_menuBarFrame)
370 {
371 AttachToFrame( menu, m_menuBarFrame );
372
373 // OPTIMISE ME: we should probably cache this, or pass it
374 // directly, but for now this is a minimal
375 // change to validate the new dynamic sizing.
376 // see (and refactor :) similar code in Remove
377 // below.
378
379 m_menuBarFrame->UpdateMenuBarSize();
380 }
381
382 return true;
383 }
384
385 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
386 {
387 if ( !wxMenuBarBase::Insert(pos, menu, title) )
388 return false;
389
390 // TODO
391
392 if ( !GtkAppend(menu, title, (int)pos) )
393 return false;
394
395 return true;
396 }
397
398 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
399 {
400 // remove the old item and insert a new one
401 wxMenu *menuOld = Remove(pos);
402 if ( menuOld && !Insert(pos, menu, title) )
403 {
404 return NULL;
405 }
406
407 // either Insert() succeeded or Remove() failed and menuOld is NULL
408 return menuOld;
409 }
410
411 wxMenu *wxMenuBar::Remove(size_t pos)
412 {
413 wxMenu *menu = wxMenuBarBase::Remove(pos);
414 if ( !menu )
415 return NULL;
416
417 gtk_menu_item_remove_submenu( GTK_MENU_ITEM(menu->m_owner) );
418 gtk_container_remove(GTK_CONTAINER(m_menubar), menu->m_owner);
419
420 gtk_widget_destroy( menu->m_owner );
421 menu->m_owner = NULL;
422
423 if (m_menuBarFrame)
424 {
425 // OPTIMISE ME: see comment in GtkAppend
426 m_menuBarFrame->UpdateMenuBarSize();
427 }
428
429 return menu;
430 }
431
432 static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString )
433 {
434 if (wxMenuItem::GetLabelText(menu->GetTitle()) == wxMenuItem::GetLabelText(menuString))
435 {
436 int res = menu->FindItem( itemString );
437 if (res != wxNOT_FOUND)
438 return res;
439 }
440
441 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
442 while (node)
443 {
444 wxMenuItem *item = node->GetData();
445 if (item->IsSubMenu())
446 return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString);
447
448 node = node->GetNext();
449 }
450
451 return wxNOT_FOUND;
452 }
453
454 int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const
455 {
456 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
457 while (node)
458 {
459 wxMenu *menu = node->GetData();
460 int res = FindMenuItemRecursive( menu, menuString, itemString);
461 if (res != -1)
462 return res;
463 node = node->GetNext();
464 }
465
466 return wxNOT_FOUND;
467 }
468
469 // Find a wxMenuItem using its id. Recurses down into sub-menus
470 static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id)
471 {
472 wxMenuItem* result = menu->FindChildItem(id);
473
474 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
475 while ( node && result == NULL )
476 {
477 wxMenuItem *item = node->GetData();
478 if (item->IsSubMenu())
479 {
480 result = FindMenuItemByIdRecursive( item->GetSubMenu(), id );
481 }
482 node = node->GetNext();
483 }
484
485 return result;
486 }
487
488 wxMenuItem* wxMenuBar::FindItem( int id, wxMenu **menuForItem ) const
489 {
490 wxMenuItem* result = 0;
491 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
492 while (node && result == 0)
493 {
494 wxMenu *menu = node->GetData();
495 result = FindMenuItemByIdRecursive( menu, id );
496 node = node->GetNext();
497 }
498
499 if ( menuForItem )
500 {
501 *menuForItem = result ? result->GetMenu() : NULL;
502 }
503
504 return result;
505 }
506
507 void wxMenuBar::EnableTop( size_t pos, bool flag )
508 {
509 wxMenuList::compatibility_iterator node = m_menus.Item( pos );
510
511 wxCHECK_RET( node, wxT("menu not found") );
512
513 wxMenu* menu = node->GetData();
514
515 if (menu->m_owner)
516 gtk_widget_set_sensitive( menu->m_owner, flag );
517 }
518
519 wxString wxMenuBar::GetMenuLabel( size_t pos ) const
520 {
521 wxMenuList::compatibility_iterator node = m_menus.Item( pos );
522
523 wxCHECK_MSG( node, wxT("invalid"), wxT("menu not found") );
524
525 wxMenu* menu = node->GetData();
526
527 return menu->GetTitle();
528 }
529
530 void wxMenuBar::SetMenuLabel( size_t pos, const wxString& label )
531 {
532 wxMenuList::compatibility_iterator node = m_menus.Item( pos );
533
534 wxCHECK_RET( node, wxT("menu not found") );
535
536 wxMenu* menu = node->GetData();
537
538 const wxString str( wxReplaceUnderscore( label ) );
539
540 menu->SetTitle( str );
541
542 if (menu->m_owner)
543 {
544 GtkLabel *glabel = GTK_LABEL( GTK_BIN(menu->m_owner)->child );
545
546 /* set new text */
547 gtk_label_set( glabel, wxGTK_CONV( str ) );
548
549 /* reparse key accel */
550 (void)gtk_label_parse_uline (GTK_LABEL(glabel), wxGTK_CONV( str ) );
551 gtk_accel_label_refetch( GTK_ACCEL_LABEL(glabel) );
552 }
553
554 }
555
556 //-----------------------------------------------------------------------------
557 // "activate"
558 //-----------------------------------------------------------------------------
559
560 extern "C" {
561 static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu )
562 {
563 if (g_isIdle)
564 wxapp_install_idle_handler();
565
566 int id = menu->FindMenuIdByMenuItem(widget);
567
568 /* should find it for normal (not popup) menu */
569 wxASSERT_MSG( (id != -1) || (menu->GetWindow() != NULL),
570 wxT("menu item not found in gtk_menu_clicked_callback") );
571
572 if (!menu->IsEnabled(id))
573 return;
574
575 wxMenuItem* item = menu->FindChildItem( id );
576 wxCHECK_RET( item, wxT("error in menu item callback") );
577
578 if ( item->GetId() == wxGTK_TITLE_ID )
579 {
580 // ignore events from the menu title
581 return;
582 }
583
584 if (item->IsCheckable())
585 {
586 bool isReallyChecked = item->IsChecked(),
587 isInternallyChecked = item->wxMenuItemBase::IsChecked();
588
589 // ensure that the internal state is always consistent with what is
590 // shown on the screen
591 item->wxMenuItemBase::Check(isReallyChecked);
592
593 // we must not report the events for the radio button going up nor the
594 // events resulting from the calls to wxMenuItem::Check()
595 if ( (item->GetKind() == wxITEM_RADIO && !isReallyChecked) ||
596 (isInternallyChecked == isReallyChecked) )
597 {
598 return;
599 }
600 }
601
602
603 // Is this menu on a menubar? (possibly nested)
604 wxFrame* frame = NULL;
605 if(menu->IsAttached())
606 frame = menu->GetMenuBar()->GetFrame();
607
608 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
609 // normally wxMenu::SendEvent() should be enough, if it doesn't work
610 // in wxGTK then we have a bug in wxMenu::GetWindow() which
611 // should be fixed instead of working around it here...
612 if (frame)
613 {
614 // If it is attached then let the frame send the event.
615 // Don't call frame->ProcessCommand(id) because it toggles
616 // checkable items and we've already done that above.
617 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
618 commandEvent.SetEventObject(frame);
619 if (item->IsCheckable())
620 commandEvent.SetInt(item->IsChecked());
621 commandEvent.SetEventObject(menu);
622
623 frame->HandleWindowEvent(commandEvent);
624 }
625 else
626 {
627 // otherwise let the menu have it
628 menu->SendEvent(id, item->IsCheckable() ? item->IsChecked() : -1);
629 }
630 }
631 }
632
633 //-----------------------------------------------------------------------------
634 // "select"
635 //-----------------------------------------------------------------------------
636
637 extern "C" {
638 static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu )
639 {
640 if (g_isIdle) wxapp_install_idle_handler();
641
642 int id = menu->FindMenuIdByMenuItem(widget);
643
644 wxASSERT( id != -1 ); // should find it!
645
646 if (!menu->IsEnabled(id))
647 return;
648
649 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id );
650 event.SetEventObject( menu );
651
652 wxEvtHandler* handler = menu->GetEventHandler();
653 if (handler && handler->ProcessEvent(event))
654 return;
655
656 wxWindow *win = menu->GetWindow();
657 if (win) win->HandleWindowEvent( event );
658 }
659 }
660
661 //-----------------------------------------------------------------------------
662 // "deselect"
663 //-----------------------------------------------------------------------------
664
665 extern "C" {
666 static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu )
667 {
668 if (g_isIdle) wxapp_install_idle_handler();
669
670 int id = menu->FindMenuIdByMenuItem(widget);
671
672 wxASSERT( id != -1 ); // should find it!
673
674 if (!menu->IsEnabled(id))
675 return;
676
677 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 );
678 event.SetEventObject( menu );
679
680 wxEvtHandler* handler = menu->GetEventHandler();
681 if (handler && handler->ProcessEvent(event))
682 return;
683
684 wxWindow *win = menu->GetWindow();
685 if (win)
686 win->HandleWindowEvent( event );
687 }
688 }
689
690 //-----------------------------------------------------------------------------
691 // wxMenuItem
692 //-----------------------------------------------------------------------------
693
694 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
695 int id,
696 const wxString& name,
697 const wxString& help,
698 wxItemKind kind,
699 wxMenu *subMenu)
700 {
701 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
702 }
703
704 wxMenuItem::wxMenuItem(wxMenu *parentMenu,
705 int id,
706 const wxString& text,
707 const wxString& help,
708 wxItemKind kind,
709 wxMenu *subMenu)
710 : wxMenuItemBase(parentMenu, id, text, help, kind, subMenu)
711 {
712 Init();
713 }
714
715 wxMenuItem::wxMenuItem(wxMenu *parentMenu,
716 int id,
717 const wxString& text,
718 const wxString& help,
719 bool isCheckable,
720 wxMenu *subMenu)
721 : wxMenuItemBase(parentMenu, id, text, help,
722 isCheckable ? wxITEM_CHECK : wxITEM_NORMAL, subMenu)
723 {
724 Init();
725 }
726
727 void wxMenuItem::Init()
728 {
729 m_labelWidget = NULL;
730 m_menuItem = NULL;
731
732 DoSetText(m_text);
733 }
734
735 wxMenuItem::~wxMenuItem()
736 {
737 // don't delete menu items, the menus take care of that
738 }
739
740 wxString wxMenuItem::GetItemLabel() const
741 {
742 wxString label = wxConvertFromGTKToWXLabel(m_text);
743 if (!m_hotKey.IsEmpty())
744 label = label + wxT("\t") + m_hotKey;
745 return label;
746 }
747
748 void wxMenuItem::SetItemLabel( const wxString& string )
749 {
750 wxString str = string;
751 if ( str.empty() && !IsSeparator() )
752 {
753 wxASSERT_MSG(wxIsStockID(GetId()), wxT("A non-stock menu item with an empty label?"));
754 str = wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR |
755 wxSTOCK_WITH_MNEMONIC);
756 }
757
758 // Some optimization to avoid flicker
759 wxString oldLabel = m_text;
760 oldLabel = wxStripMenuCodes(oldLabel);
761 oldLabel.Replace(wxT("_"), wxEmptyString);
762 wxString label1 = wxStripMenuCodes(str);
763 wxString oldhotkey = GetHotKey(); // Store the old hotkey in Ctrl-foo format
764 wxCharBuffer oldbuf = wxGTK_CONV( GetGtkHotKey(*this) ); // and as <control>foo
765
766 DoSetText(str);
767
768 if (oldLabel == label1 &&
769 oldhotkey == GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
770 return;
771
772 if (m_menuItem)
773 {
774 GtkLabel *label;
775 if (m_labelWidget)
776 label = (GtkLabel*) m_labelWidget;
777 else
778 label = GTK_LABEL( GTK_BIN(m_menuItem)->child );
779
780 // set new text
781 gtk_label_set( label, wxGTK_CONV( m_text ) );
782
783 // reparse key accel
784 (void)gtk_label_parse_uline (GTK_LABEL(label), wxGTK_CONV(m_text) );
785 gtk_accel_label_refetch( GTK_ACCEL_LABEL(label) );
786 }
787
788 guint accel_key;
789 GdkModifierType accel_mods;
790 gtk_accelerator_parse( (const char*) oldbuf, &accel_key, &accel_mods);
791 if (accel_key != 0)
792 {
793 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem),
794 m_parentMenu->m_accel,
795 accel_key,
796 accel_mods );
797 }
798
799 wxCharBuffer buf = wxGTK_CONV( GetGtkHotKey(*this) );
800 gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods);
801 if (accel_key != 0)
802 {
803 gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem),
804 "activate",
805 m_parentMenu->m_accel,
806 accel_key,
807 accel_mods,
808 GTK_ACCEL_VISIBLE);
809 }
810 }
811
812 // it's valid for this function to be called even if m_menuItem == NULL
813 void wxMenuItem::DoSetText( const wxString& str )
814 {
815 // '\t' is the deliminator indicating a hot key
816 wxString text;
817 text.reserve(str.length());
818
819 const wxChar *pc = str;
820 while ( (*pc != wxT('\0')) && (*pc != wxT('\t')) )
821 {
822 if ((*pc == wxT('&')) && (*(pc+1) == wxT('&')))
823 {
824 // "&" is doubled to indicate "&" instead of accelerator
825 ++pc;
826 text << wxT('&');
827 }
828 else if (*pc == wxT('&'))
829 {
830 text << wxT('_');
831 }
832 else if ( *pc == wxT('_') ) // escape underscores
833 {
834 text << wxT("__");
835 }
836 else
837 {
838 text << *pc;
839 }
840 ++pc;
841 }
842
843 m_hotKey = wxEmptyString;
844
845 if ( *pc == wxT('\t') )
846 {
847 pc++;
848 m_hotKey = pc;
849 }
850
851 m_text = text;
852 }
853
854 #if wxUSE_ACCEL
855
856 wxAcceleratorEntry *wxMenuItem::GetAccel() const
857 {
858 if ( !GetHotKey() )
859 {
860 // nothing
861 return NULL;
862 }
863
864 // accelerator parsing code looks for them after a TAB, so insert a dummy
865 // one here
866 wxString label;
867 label << wxT('\t') << GetHotKey();
868
869 return wxAcceleratorEntry::Create(label);
870 }
871
872 #endif // wxUSE_ACCEL
873
874 void wxMenuItem::Check( bool check )
875 {
876 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
877
878 if (check == m_isChecked)
879 return;
880
881 wxMenuItemBase::Check( check );
882
883 switch ( GetKind() )
884 {
885 case wxITEM_CHECK:
886 case wxITEM_RADIO:
887 gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check );
888 break;
889
890 default:
891 wxFAIL_MSG( wxT("can't check this item") );
892 }
893 }
894
895 void wxMenuItem::Enable( bool enable )
896 {
897 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
898
899 gtk_widget_set_sensitive( m_menuItem, enable );
900 wxMenuItemBase::Enable( enable );
901 }
902
903 bool wxMenuItem::IsChecked() const
904 {
905 wxCHECK_MSG( m_menuItem, false, wxT("invalid menu item") );
906
907 wxCHECK_MSG( IsCheckable(), false,
908 wxT("can't get state of uncheckable item!") );
909
910 return ((GtkCheckMenuItem*)m_menuItem)->active != 0;
911 }
912
913 //-----------------------------------------------------------------------------
914 // wxMenu
915 //-----------------------------------------------------------------------------
916
917 void wxMenu::Init()
918 {
919 m_accel = gtk_accel_group_new();
920 m_menu = gtk_menu_new();
921 // NB: keep reference to the menu so that it is not destroyed behind
922 // our back by GTK+ e.g. when it is removed from menubar:
923 gtk_widget_ref(m_menu);
924
925 m_owner = NULL;
926
927 // Tearoffs are entries, just like separators. So if we want this
928 // menu to be a tear-off one, we just append a tearoff entry
929 // immediately.
930 if ( m_style & wxMENU_TEAROFF )
931 {
932 GtkWidget *tearoff = gtk_tearoff_menu_item_new();
933
934 gtk_menu_append(GTK_MENU(m_menu), tearoff);
935 }
936
937 m_prevRadio = NULL;
938
939 // append the title as the very first entry if we have it
940 if ( !m_title.empty() )
941 {
942 Append(wxGTK_TITLE_ID, m_title);
943 AppendSeparator();
944 }
945 }
946
947 wxMenu::~wxMenu()
948 {
949 WX_CLEAR_LIST(wxMenuItemList, m_items);
950
951 if ( GTK_IS_WIDGET( m_menu ))
952 {
953 // see wxMenu::Init
954 gtk_widget_unref( m_menu );
955 // if the menu is inserted in another menu at this time, there was
956 // one more reference to it:
957 if ( m_owner )
958 gtk_widget_destroy( m_menu );
959 }
960 }
961
962 wxString wxMenu::GetTitle() const
963 {
964 return wxConvertMnemonicsFromGTK(wxMenuBase::GetTitle());
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().IsOk())
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