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