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