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