don't use implicit wxString->char*/wchar_t* conversion, it will not be available...
[wxWidgets.git] / src / gtk / menu.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/menu.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #include "wx/menu.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/intl.h"
17 #include "wx/log.h"
18 #include "wx/frame.h"
19 #include "wx/bitmap.h"
20 #include "wx/app.h"
21 #endif
22
23 #include "wx/accel.h"
24 #include "wx/stockitem.h"
25 #include "wx/gtk/private.h"
26
27 #ifdef __WXGTK20__
28 #include <gdk/gdktypes.h>
29 #endif
30
31 // FIXME: is this right? somehow I don't think so (VZ)
32
33 #define gtk_accel_group_attach(g, o) gtk_window_add_accel_group((o), (g))
34 //#define gtk_accel_group_detach(g, o) gtk_window_remove_accel_group((o), (g))
35 //#define gtk_menu_ensure_uline_accel_group(m) gtk_menu_get_accel_group(m)
36
37 #define ACCEL_OBJECT GtkWindow
38 #define ACCEL_OBJECTS(a) (a)->acceleratables
39 #define ACCEL_OBJ_CAST(obj) ((GtkWindow*) obj)
40
41 // we use normal item but with a special id for the menu title
42 static const int wxGTK_TITLE_ID = -3;
43
44 //-----------------------------------------------------------------------------
45 // idle system
46 //-----------------------------------------------------------------------------
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 // GTK 1.2 wants to have "_" instead of "&" for accelerators
59 wxString str;
60
61 for ( wxString::const_iterator pc = title.begin(); pc != title.end(); ++pc )
62 {
63 if ((*pc == wxT('&')) && (pc+1 != title.end()) && (*(pc+1) == wxT('&')))
64 {
65 // "&" is doubled to indicate "&" instead of accelerator
66 ++pc;
67 str << wxT('&');
68 }
69 else if (*pc == wxT('&'))
70 {
71 str << wxT('_');
72 }
73 else
74 {
75 if ( *pc == wxT('_') )
76 {
77 // underscores must be doubled to prevent them from being
78 // interpreted as accelerator character prefix by GTK
79 str << *pc;
80 }
81
82 str << *pc;
83 }
84 }
85
86 // wxPrintf( wxT("before %s after %s\n"), title.c_str(), str.c_str() );
87
88 return str;
89 }
90
91 //-----------------------------------------------------------------------------
92 // activate message from GTK
93 //-----------------------------------------------------------------------------
94
95 static void DoCommonMenuCallbackCode(wxMenu *menu, wxMenuEvent& event)
96 {
97 event.SetEventObject( menu );
98
99 wxEvtHandler* handler = menu->GetEventHandler();
100 if (handler && handler->ProcessEvent(event))
101 return;
102
103 wxWindow *win = menu->GetInvokingWindow();
104 if (win)
105 win->GetEventHandler()->ProcessEvent( event );
106 }
107
108 extern "C" {
109
110 static void gtk_menu_open_callback( GtkWidget *widget, wxMenu *menu )
111 {
112 wxMenuEvent event(wxEVT_MENU_OPEN, -1, menu);
113
114 DoCommonMenuCallbackCode(menu, event);
115 }
116
117 static void gtk_menu_close_callback( GtkWidget *widget, wxMenuBar *menubar )
118 {
119 if ( !menubar->GetMenuCount() )
120 {
121 // if menubar is empty we can't call GetMenu(0) below
122 return;
123 }
124
125 wxMenuEvent event( wxEVT_MENU_CLOSE, -1, NULL );
126
127 DoCommonMenuCallbackCode(menubar->GetMenu(0), event);
128 }
129
130 }
131
132 //-----------------------------------------------------------------------------
133 // wxMenuBar
134 //-----------------------------------------------------------------------------
135
136 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
137
138 void wxMenuBar::Init(size_t n, wxMenu *menus[], const wxString titles[], long style)
139 {
140 m_style = style;
141 m_invokingWindow = NULL;
142
143 if (!PreCreation( NULL, wxDefaultPosition, wxDefaultSize ) ||
144 !CreateBase( NULL, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, wxT("menubar") ))
145 {
146 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
147 return;
148 }
149
150 m_menubar = gtk_menu_bar_new();
151
152 if (style & wxMB_DOCKABLE)
153 {
154 m_widget = gtk_handle_box_new();
155 gtk_container_add(GTK_CONTAINER(m_widget), m_menubar);
156 gtk_widget_show(m_menubar);
157 }
158 else
159 {
160 m_widget = m_menubar;
161 }
162
163 PostCreation();
164
165 ApplyWidgetStyle();
166
167 for (size_t i = 0; i < n; ++i )
168 Append(menus[i], titles[i]);
169
170 // VZ: for some reason connecting to menus "deactivate" doesn't work (we
171 // don't get it when the menu is dismissed by clicking outside the
172 // toolbar) so we connect to the global one, even if it means that we
173 // can't pass the menu which was closed in wxMenuEvent object
174 g_signal_connect (m_menubar, "deactivate",
175 G_CALLBACK (gtk_menu_close_callback), this);
176
177 }
178
179 wxMenuBar::wxMenuBar(size_t n, wxMenu *menus[], const wxString titles[], long style)
180 {
181 Init(n, menus, titles, style);
182 }
183
184 wxMenuBar::wxMenuBar(long style)
185 {
186 Init(0, NULL, NULL, style);
187 }
188
189 wxMenuBar::wxMenuBar()
190 {
191 Init(0, NULL, NULL, 0);
192 }
193
194 wxMenuBar::~wxMenuBar()
195 {
196 }
197
198 static void wxMenubarUnsetInvokingWindow( wxMenu *menu, wxWindow *win )
199 {
200 menu->SetInvokingWindow( (wxWindow*) NULL );
201
202 wxWindow *top_frame = win;
203 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
204 top_frame = top_frame->GetParent();
205
206 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
207 while (node)
208 {
209 wxMenuItem *menuitem = node->GetData();
210 if (menuitem->IsSubMenu())
211 wxMenubarUnsetInvokingWindow( menuitem->GetSubMenu(), win );
212 node = node->GetNext();
213 }
214 }
215
216 static void wxMenubarSetInvokingWindow( wxMenu *menu, wxWindow *win )
217 {
218 menu->SetInvokingWindow( win );
219
220 wxWindow *top_frame = win;
221 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
222 top_frame = top_frame->GetParent();
223
224 // support for native hot keys
225 ACCEL_OBJECT *obj = ACCEL_OBJ_CAST(top_frame->m_widget);
226 if ( !g_slist_find( ACCEL_OBJECTS(menu->m_accel), obj ) )
227 gtk_accel_group_attach( menu->m_accel, obj );
228
229 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
230 while (node)
231 {
232 wxMenuItem *menuitem = node->GetData();
233 if (menuitem->IsSubMenu())
234 wxMenubarSetInvokingWindow( menuitem->GetSubMenu(), win );
235 node = node->GetNext();
236 }
237 }
238
239 void wxMenuBar::SetInvokingWindow( wxWindow *win )
240 {
241 m_invokingWindow = win;
242 wxWindow *top_frame = win;
243 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
244 top_frame = top_frame->GetParent();
245
246 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
247 while (node)
248 {
249 wxMenu *menu = node->GetData();
250 wxMenubarSetInvokingWindow( menu, win );
251 node = node->GetNext();
252 }
253 }
254
255 void wxMenuBar::SetLayoutDirection(wxLayoutDirection dir)
256 {
257 if ( dir == wxLayout_Default )
258 {
259 const wxWindow *const frame = GetFrame();
260 if ( frame )
261 {
262 // inherit layout from frame.
263 dir = frame->GetLayoutDirection();
264 }
265 else // use global layout
266 {
267 dir = wxTheApp->GetLayoutDirection();
268 }
269 }
270
271 if ( dir == wxLayout_Default )
272 return;
273
274 GTKSetLayout(m_menubar, dir);
275
276 // also set the layout of all menus we already have (new ones will inherit
277 // the current layout)
278 for ( wxMenuList::compatibility_iterator node = m_menus.GetFirst();
279 node;
280 node = node->GetNext() )
281 {
282 wxMenu *const menu = node->GetData();
283 menu->SetLayoutDirection(dir);
284 }
285 }
286
287 wxLayoutDirection wxMenuBar::GetLayoutDirection() const
288 {
289 return GTKGetLayout(m_menubar);
290 }
291
292 void wxMenuBar::Attach(wxFrame *frame)
293 {
294 wxMenuBarBase::Attach(frame);
295
296 SetLayoutDirection(wxLayout_Default);
297 }
298
299 void wxMenuBar::UnsetInvokingWindow( wxWindow *win )
300 {
301 m_invokingWindow = (wxWindow*) NULL;
302 wxWindow *top_frame = win;
303 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
304 top_frame = top_frame->GetParent();
305
306 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
307 while (node)
308 {
309 wxMenu *menu = node->GetData();
310 wxMenubarUnsetInvokingWindow( menu, win );
311 node = node->GetNext();
312 }
313 }
314
315 bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
316 {
317 if ( !wxMenuBarBase::Append( menu, title ) )
318 return false;
319
320 return GtkAppend(menu, title);
321 }
322
323 bool wxMenuBar::GtkAppend(wxMenu *menu, const wxString& title, int pos)
324 {
325 wxString str( wxReplaceUnderscore( title ) );
326
327 // This doesn't have much effect right now.
328 menu->SetTitle( str );
329
330 // The "m_owner" is the "menu item"
331 menu->m_owner = gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str ) );
332 menu->SetLayoutDirection(GetLayoutDirection());
333
334 gtk_widget_show( menu->m_owner );
335
336 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu );
337
338 if (pos == -1)
339 gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar), menu->m_owner );
340 else
341 gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar), menu->m_owner, pos );
342
343 g_signal_connect (menu->m_owner, "activate",
344 G_CALLBACK (gtk_menu_open_callback),
345 menu);
346
347 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
348 // addings menu later on.
349 if (m_invokingWindow)
350 {
351 wxMenubarSetInvokingWindow( menu, m_invokingWindow );
352
353 // OPTIMISE ME: we should probably cache this, or pass it
354 // directly, but for now this is a minimal
355 // change to validate the new dynamic sizing.
356 // see (and refactor :) similar code in Remove
357 // below.
358
359 wxFrame *frame = wxDynamicCast( m_invokingWindow, wxFrame );
360
361 if( frame )
362 frame->UpdateMenuBarSize();
363 }
364
365 return true;
366 }
367
368 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
369 {
370 if ( !wxMenuBarBase::Insert(pos, menu, title) )
371 return false;
372
373 // TODO
374
375 if ( !GtkAppend(menu, title, (int)pos) )
376 return false;
377
378 return true;
379 }
380
381 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
382 {
383 // remove the old item and insert a new one
384 wxMenu *menuOld = Remove(pos);
385 if ( menuOld && !Insert(pos, menu, title) )
386 {
387 return (wxMenu*) NULL;
388 }
389
390 // either Insert() succeeded or Remove() failed and menuOld is NULL
391 return menuOld;
392 }
393
394 wxMenu *wxMenuBar::Remove(size_t pos)
395 {
396 wxMenu *menu = wxMenuBarBase::Remove(pos);
397 if ( !menu )
398 return (wxMenu*) NULL;
399
400 gtk_menu_item_remove_submenu( GTK_MENU_ITEM(menu->m_owner) );
401 gtk_container_remove(GTK_CONTAINER(m_menubar), menu->m_owner);
402
403 gtk_widget_destroy( menu->m_owner );
404 menu->m_owner = NULL;
405
406 if (m_invokingWindow)
407 {
408 // OPTIMISE ME: see comment in GtkAppend
409 wxFrame *frame = wxDynamicCast( m_invokingWindow, wxFrame );
410
411 if( frame )
412 frame->UpdateMenuBarSize();
413 }
414
415 return menu;
416 }
417
418 static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString )
419 {
420 if (wxMenuItem::GetLabelFromText(menu->GetTitle()) == wxMenuItem::GetLabelFromText(menuString))
421 {
422 int res = menu->FindItem( itemString );
423 if (res != wxNOT_FOUND)
424 return res;
425 }
426
427 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
428 while (node)
429 {
430 wxMenuItem *item = node->GetData();
431 if (item->IsSubMenu())
432 return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString);
433
434 node = node->GetNext();
435 }
436
437 return wxNOT_FOUND;
438 }
439
440 int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const
441 {
442 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
443 while (node)
444 {
445 wxMenu *menu = node->GetData();
446 int res = FindMenuItemRecursive( menu, menuString, itemString);
447 if (res != -1)
448 return res;
449 node = node->GetNext();
450 }
451
452 return wxNOT_FOUND;
453 }
454
455 // Find a wxMenuItem using its id. Recurses down into sub-menus
456 static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id)
457 {
458 wxMenuItem* result = menu->FindChildItem(id);
459
460 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
461 while ( node && result == NULL )
462 {
463 wxMenuItem *item = node->GetData();
464 if (item->IsSubMenu())
465 {
466 result = FindMenuItemByIdRecursive( item->GetSubMenu(), id );
467 }
468 node = node->GetNext();
469 }
470
471 return result;
472 }
473
474 wxMenuItem* wxMenuBar::FindItem( int id, wxMenu **menuForItem ) const
475 {
476 wxMenuItem* result = 0;
477 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
478 while (node && result == 0)
479 {
480 wxMenu *menu = node->GetData();
481 result = FindMenuItemByIdRecursive( menu, id );
482 node = node->GetNext();
483 }
484
485 if ( menuForItem )
486 {
487 *menuForItem = result ? result->GetMenu() : (wxMenu *)NULL;
488 }
489
490 return result;
491 }
492
493 void wxMenuBar::EnableTop( size_t pos, bool flag )
494 {
495 wxMenuList::compatibility_iterator node = m_menus.Item( pos );
496
497 wxCHECK_RET( node, wxT("menu not found") );
498
499 wxMenu* menu = node->GetData();
500
501 if (menu->m_owner)
502 gtk_widget_set_sensitive( menu->m_owner, flag );
503 }
504
505 wxString wxMenuBar::GetLabelTop( size_t pos ) const
506 {
507 wxMenuList::compatibility_iterator node = m_menus.Item( pos );
508
509 wxCHECK_MSG( node, wxT("invalid"), wxT("menu not found") );
510
511 wxMenu* menu = node->GetData();
512
513 wxString label;
514 wxString text( menu->GetTitle() );
515 for ( const wxChar *pc = text.c_str(); *pc; pc++ )
516 {
517 if ( *pc == wxT('_') )
518 {
519 // '_' is the escape character for GTK+
520 continue;
521 }
522
523 // don't remove ampersands '&' since if we have them in the menu title
524 // it means that they were doubled to indicate "&" instead of accelerator
525
526 label += *pc;
527 }
528
529 return label;
530 }
531
532 void wxMenuBar::SetLabelTop( 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 gtk_label_set_text_with_mnemonic( GTK_LABEL( GTK_BIN(menu->m_owner)->child), wxGTK_CONV(str) );
546 }
547
548 //-----------------------------------------------------------------------------
549 // "activate"
550 //-----------------------------------------------------------------------------
551
552 extern "C" {
553 static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu )
554 {
555 int id = menu->FindMenuIdByMenuItem(widget);
556
557 /* should find it for normal (not popup) menu */
558 wxASSERT_MSG( (id != -1) || (menu->GetInvokingWindow() != NULL),
559 _T("menu item not found in gtk_menu_clicked_callback") );
560
561 if (!menu->IsEnabled(id))
562 return;
563
564 wxMenuItem* item = menu->FindChildItem( id );
565 wxCHECK_RET( item, wxT("error in menu item callback") );
566
567 if ( item->GetId() == wxGTK_TITLE_ID )
568 {
569 // ignore events from the menu title
570 return;
571 }
572
573 if (item->IsCheckable())
574 {
575 bool isReallyChecked = item->IsChecked(),
576 isInternallyChecked = item->wxMenuItemBase::IsChecked();
577
578 // ensure that the internal state is always consistent with what is
579 // shown on the screen
580 item->wxMenuItemBase::Check(isReallyChecked);
581
582 // we must not report the events for the radio button going up nor the
583 // events resulting from the calls to wxMenuItem::Check()
584 if ( (item->GetKind() == wxITEM_RADIO && !isReallyChecked) ||
585 (isInternallyChecked == isReallyChecked) )
586 {
587 return;
588 }
589 }
590
591
592 // Is this menu on a menubar? (possibly nested)
593 wxFrame* frame = NULL;
594 if(menu->IsAttached())
595 frame = menu->GetMenuBar()->GetFrame();
596
597 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
598 // normally wxMenu::SendEvent() should be enough, if it doesn't work
599 // in wxGTK then we have a bug in wxMenu::GetInvokingWindow() which
600 // should be fixed instead of working around it here...
601 if (frame)
602 {
603 // If it is attached then let the frame send the event.
604 // Don't call frame->ProcessCommand(id) because it toggles
605 // checkable items and we've already done that above.
606 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
607 commandEvent.SetEventObject(frame);
608 if (item->IsCheckable())
609 commandEvent.SetInt(item->IsChecked());
610 commandEvent.SetEventObject(menu);
611
612 frame->GetEventHandler()->ProcessEvent(commandEvent);
613 }
614 else
615 {
616 // otherwise let the menu have it
617 menu->SendEvent(id, item->IsCheckable() ? item->IsChecked() : -1);
618 }
619 }
620 }
621
622 //-----------------------------------------------------------------------------
623 // "select"
624 //-----------------------------------------------------------------------------
625
626 extern "C" {
627 static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu )
628 {
629 int id = menu->FindMenuIdByMenuItem(widget);
630
631 wxASSERT( id != -1 ); // should find it!
632
633 if (!menu->IsEnabled(id))
634 return;
635
636 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id );
637 event.SetEventObject( menu );
638
639 wxEvtHandler* handler = menu->GetEventHandler();
640 if (handler && handler->ProcessEvent(event))
641 return;
642
643 wxWindow *win = menu->GetInvokingWindow();
644 if (win) win->GetEventHandler()->ProcessEvent( event );
645 }
646 }
647
648 //-----------------------------------------------------------------------------
649 // "deselect"
650 //-----------------------------------------------------------------------------
651
652 extern "C" {
653 static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu )
654 {
655 int id = menu->FindMenuIdByMenuItem(widget);
656
657 wxASSERT( id != -1 ); // should find it!
658
659 if (!menu->IsEnabled(id))
660 return;
661
662 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 );
663 event.SetEventObject( menu );
664
665 wxEvtHandler* handler = menu->GetEventHandler();
666 if (handler && handler->ProcessEvent(event))
667 return;
668
669 wxWindow *win = menu->GetInvokingWindow();
670 if (win)
671 win->GetEventHandler()->ProcessEvent( event );
672 }
673 }
674
675 //-----------------------------------------------------------------------------
676 // wxMenuItem
677 //-----------------------------------------------------------------------------
678
679 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
680
681 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
682 int id,
683 const wxString& name,
684 const wxString& help,
685 wxItemKind kind,
686 wxMenu *subMenu)
687 {
688 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
689 }
690
691 wxMenuItem::wxMenuItem(wxMenu *parentMenu,
692 int id,
693 const wxString& text,
694 const wxString& help,
695 wxItemKind kind,
696 wxMenu *subMenu)
697 : wxMenuItemBase(parentMenu, id, text, help, kind, subMenu)
698 {
699 Init(text);
700 }
701
702 wxMenuItem::wxMenuItem(wxMenu *parentMenu,
703 int id,
704 const wxString& text,
705 const wxString& help,
706 bool isCheckable,
707 wxMenu *subMenu)
708 : wxMenuItemBase(parentMenu, id, text, help,
709 isCheckable ? wxITEM_CHECK : wxITEM_NORMAL, subMenu)
710 {
711 Init(text);
712 }
713
714 void wxMenuItem::Init(const wxString& text)
715 {
716 m_labelWidget = (GtkWidget *) NULL;
717 m_menuItem = (GtkWidget *) NULL;
718
719 DoSetText(text);
720 }
721
722 wxMenuItem::~wxMenuItem()
723 {
724 // don't delete menu items, the menus take care of that
725 }
726
727 // return the menu item text without any menu accels
728 /* static */
729 wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
730 {
731 wxString label;
732
733 for ( const wxChar *pc = text.c_str(); *pc; pc++ )
734 {
735 if ( *pc == wxT('\t'))
736 break;
737
738 if ( *pc == wxT('_') )
739 {
740 // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx"
741 pc++;
742 label += *pc;
743 continue;
744 }
745
746 if ( *pc == wxT('\\') )
747 {
748 // GTK 2.0 escapes "xxx/xxx" to "xxx\/xxx"
749 pc++;
750 label += *pc;
751 continue;
752 }
753
754 if ( (*pc == wxT('&')) && (*(pc+1) != wxT('&')) )
755 {
756 // wxMSW escapes "&"
757 // "&" is doubled to indicate "&" instead of accelerator
758 continue;
759 }
760
761 label += *pc;
762 }
763
764 // wxPrintf( wxT("GetLabelFromText(): text %s label %s\n"), text.c_str(), label.c_str() );
765
766 return label;
767 }
768
769 void wxMenuItem::SetText( const wxString& str )
770 {
771 // cache some data which must be used later
772 bool isstock = wxIsStockID(GetId());
773 const char *stockid = NULL;
774 if (isstock)
775 stockid = wxGetStockGtkID(GetId());
776
777 // Some optimization to avoid flicker
778 wxString oldLabel = m_text;
779 oldLabel = wxStripMenuCodes(oldLabel);
780 oldLabel.Replace(wxT("_"), wxT(""));
781 wxString label1 = wxStripMenuCodes(str);
782 wxString oldhotkey = GetHotKey(); // Store the old hotkey in Ctrl-foo format
783 wxCharBuffer oldbuf = wxGTK_CONV_SYS( GetGtkHotKey(*this) ); // and as <control>foo
784
785 DoSetText(str);
786
787 if (oldLabel == label1 &&
788 oldhotkey == GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
789 return;
790
791 if (m_menuItem)
792 {
793 GtkLabel *label;
794 if (m_labelWidget)
795 label = (GtkLabel*) m_labelWidget;
796 else
797 label = GTK_LABEL( GTK_BIN(m_menuItem)->child );
798
799 // stock menu items can have empty labels:
800 wxString text = m_text;
801 if (text.IsEmpty() && !IsSeparator())
802 {
803 wxASSERT_MSG(isstock, wxT("A non-stock menu item with an empty label?"));
804 text = wxGetStockLabel(GetId());
805
806 // need & => _ conversion
807 text = GTKProcessMenuItemLabel(text, NULL);
808 }
809
810 gtk_label_set_text_with_mnemonic( GTK_LABEL(label), wxGTK_CONV_SYS(text) );
811 }
812
813 // remove old accelerator from our parent's accelerator group, if present
814 guint accel_key;
815 GdkModifierType accel_mods;
816 if (oldbuf[(size_t)0] != '\0')
817 {
818 gtk_accelerator_parse( (const char*) oldbuf, &accel_key, &accel_mods);
819 if (accel_key != 0)
820 {
821 gtk_widget_remove_accelerator(m_menuItem,
822 m_parentMenu->m_accel,
823 accel_key,
824 accel_mods );
825 }
826 }
827 else if (isstock)
828 {
829 // if the accelerator was taken from a stock ID, just get it back from GTK+ stock
830 if (wxGetStockGtkAccelerator(stockid, &accel_mods, &accel_key))
831 gtk_widget_remove_accelerator( m_menuItem,
832 m_parentMenu->m_accel,
833 accel_key,
834 accel_mods );
835 }
836
837 // add new accelerator to our parent's accelerator group
838 wxCharBuffer buf = wxGTK_CONV_SYS( GetGtkHotKey(*this) );
839 if (buf[(size_t)0] != '\0')
840 {
841 gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods);
842 if (accel_key != 0)
843 {
844 gtk_widget_add_accelerator( m_menuItem,
845 "activate",
846 m_parentMenu->m_accel,
847 accel_key,
848 accel_mods,
849 GTK_ACCEL_VISIBLE);
850 }
851 }
852 else if (isstock)
853 {
854 // if the accelerator was taken from a stock ID, just get it back from GTK+ stock
855 if (wxGetStockGtkAccelerator(stockid, &accel_mods, &accel_key))
856 gtk_widget_remove_accelerator( m_menuItem,
857 m_parentMenu->m_accel,
858 accel_key,
859 accel_mods );
860 }
861 }
862
863 // NOTE: this function is different from the similar functions GTKProcessMnemonics()
864 // implemented in control.cpp and from wxMenuItemBase::GetLabelFromText...
865 // so there's no real code duplication
866 wxString wxMenuItem::GTKProcessMenuItemLabel(const wxString& str, wxString *hotKey)
867 {
868 wxString text;
869
870 // '\t' is the deliminator indicating a hot key
871 const wxChar *pc = str;
872 while ( (*pc != wxT('\0')) && (*pc != wxT('\t')) )
873 {
874 if ((*pc == wxT('&')) && (*(pc+1) == wxT('&')))
875 {
876 // "&" is doubled to indicate "&" instead of accelerator
877 ++pc;
878 text << wxT('&');
879 }
880 else if (*pc == wxT('&'))
881 {
882 text << wxT('_');
883 }
884 else if ( *pc == wxT('_') ) // escape underscores
885 {
886 text << wxT("__");
887 }
888 else
889 {
890 text << *pc;
891 }
892 ++pc;
893 }
894
895 if (hotKey)
896 {
897 hotKey->Empty();
898 if(*pc == wxT('\t'))
899 {
900 pc++;
901 *hotKey = pc;
902 }
903 }
904
905 return text;
906 }
907
908 // it's valid for this function to be called even if m_menuItem == NULL
909 void wxMenuItem::DoSetText( const wxString& str )
910 {
911 m_text.Empty();
912 m_text = GTKProcessMenuItemLabel(str, &m_hotKey);
913 }
914
915 #if wxUSE_ACCEL
916
917 wxAcceleratorEntry *wxMenuItem::GetAccel() const
918 {
919 if ( !GetHotKey() )
920 {
921 // nothing
922 return NULL;
923 }
924
925 // accelerator parsing code looks for them after a TAB, so insert a dummy
926 // one here
927 wxString label;
928 label << wxT('\t') << GetHotKey();
929
930 return wxAcceleratorEntry::Create(label);
931 }
932
933 #endif // wxUSE_ACCEL
934
935 void wxMenuItem::Check( bool check )
936 {
937 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
938
939 if (check == m_isChecked)
940 return;
941
942 wxMenuItemBase::Check( check );
943
944 switch ( GetKind() )
945 {
946 case wxITEM_CHECK:
947 case wxITEM_RADIO:
948 gtk_check_menu_item_set_active( (GtkCheckMenuItem*)m_menuItem, (gint)check );
949 break;
950
951 default:
952 wxFAIL_MSG( _T("can't check this item") );
953 }
954 }
955
956 void wxMenuItem::Enable( bool enable )
957 {
958 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
959
960 gtk_widget_set_sensitive( m_menuItem, enable );
961 wxMenuItemBase::Enable( enable );
962 }
963
964 bool wxMenuItem::IsChecked() const
965 {
966 wxCHECK_MSG( m_menuItem, false, wxT("invalid menu item") );
967
968 wxCHECK_MSG( IsCheckable(), false,
969 wxT("can't get state of uncheckable item!") );
970
971 return ((GtkCheckMenuItem*)m_menuItem)->active != 0;
972 }
973
974 //-----------------------------------------------------------------------------
975 // wxMenu
976 //-----------------------------------------------------------------------------
977
978 IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
979
980 void wxMenu::Init()
981 {
982 m_accel = gtk_accel_group_new();
983 m_menu = gtk_menu_new();
984 // NB: keep reference to the menu so that it is not destroyed behind
985 // our back by GTK+ e.g. when it is removed from menubar:
986 gtk_widget_ref(m_menu);
987
988 m_owner = (GtkWidget*) NULL;
989
990 // Tearoffs are entries, just like separators. So if we want this
991 // menu to be a tear-off one, we just append a tearoff entry
992 // immediately.
993 if ( m_style & wxMENU_TEAROFF )
994 {
995 GtkWidget *tearoff = gtk_tearoff_menu_item_new();
996
997 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu), tearoff);
998 }
999
1000 m_prevRadio = NULL;
1001
1002 // append the title as the very first entry if we have it
1003 if ( !m_title.empty() )
1004 {
1005 Append(wxGTK_TITLE_ID, m_title);
1006 AppendSeparator();
1007 }
1008 }
1009
1010 wxMenu::~wxMenu()
1011 {
1012 WX_CLEAR_LIST(wxMenuItemList, m_items);
1013
1014 if ( GTK_IS_WIDGET( m_menu ))
1015 {
1016 // see wxMenu::Init
1017 gtk_widget_unref( m_menu );
1018 g_object_unref( m_accel );
1019
1020 // if the menu is inserted in another menu at this time, there was
1021 // one more reference to it:
1022 if ( m_owner )
1023 gtk_widget_destroy( m_menu );
1024 }
1025 }
1026
1027 void wxMenu::SetLayoutDirection(const wxLayoutDirection dir)
1028 {
1029 if ( m_owner )
1030 wxWindow::GTKSetLayout(m_owner, dir);
1031 //else: will be called later by wxMenuBar again
1032 }
1033
1034 wxLayoutDirection wxMenu::GetLayoutDirection() const
1035 {
1036 return wxWindow::GTKGetLayout(m_owner);
1037 }
1038
1039 bool wxMenu::GtkAppend(wxMenuItem *mitem, int pos)
1040 {
1041 GtkWidget *menuItem;
1042
1043 // cache some data used later
1044 wxString text = mitem->GetText();
1045 int id = mitem->GetId();
1046 bool isstock = wxIsStockID(id);
1047 const char *stockid = NULL;
1048 if (isstock)
1049 stockid = wxGetStockGtkID(mitem->GetId());
1050
1051 // stock menu items can have an empty label
1052 if (text.IsEmpty() && !mitem->IsSeparator())
1053 {
1054 wxASSERT_MSG(isstock, wxT("A non-stock menu item with an empty label?"));
1055 text = wxGetStockLabel(id);
1056
1057 // need & => _ conversion
1058 text = wxMenuItem::GTKProcessMenuItemLabel(text, NULL);
1059 }
1060
1061 if ( mitem->IsSeparator() )
1062 {
1063 menuItem = gtk_separator_menu_item_new();
1064 }
1065 else if ( mitem->GetBitmap().Ok() ||
1066 (mitem->GetKind() == wxITEM_NORMAL && isstock) )
1067 {
1068 wxBitmap bitmap(mitem->GetBitmap());
1069
1070 menuItem = gtk_image_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text ) );
1071
1072 GtkWidget *image;
1073 if ( !bitmap.Ok() )
1074 {
1075 // use stock bitmap for this item if available on the assumption
1076 // that it never hurts to follow GTK+ conventions more closely
1077 image = stockid ? gtk_image_new_from_stock(stockid, GTK_ICON_SIZE_MENU)
1078 : NULL;
1079 }
1080 else // we have a custom bitmap
1081 {
1082 wxASSERT_MSG( mitem->GetKind() == wxITEM_NORMAL,
1083 _T("only normal menu items can have bitmaps") );
1084
1085 if ( bitmap.HasPixbuf() )
1086 {
1087 image = gtk_image_new_from_pixbuf(bitmap.GetPixbuf());
1088 }
1089 else
1090 {
1091 GdkPixmap *gdk_pixmap = bitmap.GetPixmap();
1092 GdkBitmap *gdk_bitmap = bitmap.GetMask() ?
1093 bitmap.GetMask()->GetBitmap() :
1094 (GdkBitmap*) NULL;
1095 image = gtk_image_new_from_pixmap( gdk_pixmap, gdk_bitmap );
1096 }
1097 }
1098
1099 if ( image )
1100 {
1101 gtk_widget_show(image);
1102
1103 gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM(menuItem), image );
1104 }
1105
1106 m_prevRadio = NULL;
1107 }
1108 else // a normal item
1109 {
1110 // NB: 'text' variable has "_" instead of "&" after mitem->SetText()
1111 // so don't use it
1112
1113 switch ( mitem->GetKind() )
1114 {
1115 case wxITEM_CHECK:
1116 {
1117 menuItem = gtk_check_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text ) );
1118 m_prevRadio = NULL;
1119 break;
1120 }
1121
1122 case wxITEM_RADIO:
1123 {
1124 GSList *group = NULL;
1125 if ( m_prevRadio == NULL )
1126 {
1127 // start of a new radio group
1128 m_prevRadio = menuItem =
1129 gtk_radio_menu_item_new_with_mnemonic( group, wxGTK_CONV_SYS( text ) );
1130 }
1131 else // continue the radio group
1132 {
1133 group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (m_prevRadio));
1134 m_prevRadio = menuItem =
1135 gtk_radio_menu_item_new_with_mnemonic( group, wxGTK_CONV_SYS( text ) );
1136 }
1137 break;
1138 }
1139
1140 default:
1141 wxFAIL_MSG( _T("unexpected menu item kind") );
1142 // fall through
1143
1144 case wxITEM_NORMAL:
1145 {
1146 menuItem = gtk_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text ) );
1147 m_prevRadio = NULL;
1148 break;
1149 }
1150 }
1151
1152 }
1153
1154 guint accel_key;
1155 GdkModifierType accel_mods;
1156 wxCharBuffer buf = wxGTK_CONV_SYS( GetGtkHotKey(*mitem) );
1157
1158 // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetText().c_str(), GetGtkHotKey(*mitem).c_str() );
1159 if (buf[(size_t)0] != '\0')
1160 {
1161 gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods);
1162 if (accel_key != 0)
1163 {
1164 gtk_widget_add_accelerator (menuItem,
1165 "activate",
1166 m_accel,
1167 accel_key,
1168 accel_mods,
1169 GTK_ACCEL_VISIBLE);
1170 }
1171 }
1172 else if (isstock)
1173 {
1174 // if the accelerator was taken from a stock ID, just get it back from GTK+ stock
1175 if (wxGetStockGtkAccelerator(stockid, &accel_mods, &accel_key))
1176 gtk_widget_add_accelerator( menuItem,
1177 "activate",
1178 m_accel,
1179 accel_key,
1180 accel_mods,
1181 GTK_ACCEL_VISIBLE);
1182 }
1183
1184 if (pos == -1)
1185 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu), menuItem);
1186 else
1187 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu), menuItem, pos);
1188
1189 gtk_widget_show( menuItem );
1190
1191 if ( !mitem->IsSeparator() )
1192 {
1193 wxASSERT_MSG( menuItem, wxT("invalid menuitem") );
1194
1195 g_signal_connect (menuItem, "select",
1196 G_CALLBACK (gtk_menu_hilight_callback), this);
1197 g_signal_connect (menuItem, "deselect",
1198 G_CALLBACK (gtk_menu_nolight_callback), this);
1199
1200 if ( mitem->IsSubMenu() && mitem->GetKind() != wxITEM_RADIO && mitem->GetKind() != wxITEM_CHECK )
1201 {
1202 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), mitem->GetSubMenu()->m_menu );
1203
1204 gtk_widget_show( mitem->GetSubMenu()->m_menu );
1205
1206 // if adding a submenu to a menu already existing in the menu bar, we
1207 // must set invoking window to allow processing events from this
1208 // submenu
1209 if ( m_invokingWindow )
1210 wxMenubarSetInvokingWindow(mitem->GetSubMenu(), m_invokingWindow);
1211 }
1212 else
1213 {
1214 g_signal_connect (menuItem, "activate",
1215 G_CALLBACK (gtk_menu_clicked_callback),
1216 this);
1217 }
1218 }
1219
1220 mitem->SetMenuItem(menuItem);
1221
1222 if (ms_locked)
1223 {
1224 // This doesn't even exist!
1225 // gtk_widget_lock_accelerators(mitem->GetMenuItem());
1226 }
1227
1228 return true;
1229 }
1230
1231 wxMenuItem* wxMenu::DoAppend(wxMenuItem *mitem)
1232 {
1233 if (!GtkAppend(mitem))
1234 return NULL;
1235
1236 return wxMenuBase::DoAppend(mitem);
1237 }
1238
1239 wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
1240 {
1241 if ( !wxMenuBase::DoInsert(pos, item) )
1242 return NULL;
1243
1244 // TODO
1245 if ( !GtkAppend(item, (int)pos) )
1246 return NULL;
1247
1248 return item;
1249 }
1250
1251 wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
1252 {
1253 if ( !wxMenuBase::DoRemove(item) )
1254 return (wxMenuItem *)NULL;
1255
1256 // TODO: this code doesn't delete the item factory item and this seems
1257 // impossible as of GTK 1.2.6.
1258 gtk_widget_destroy( item->GetMenuItem() );
1259
1260 return item;
1261 }
1262
1263 int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
1264 {
1265 wxMenuItemList::compatibility_iterator node = m_items.GetFirst();
1266 while (node)
1267 {
1268 wxMenuItem *item = node->GetData();
1269 if (item->GetMenuItem() == menuItem)
1270 return item->GetId();
1271 node = node->GetNext();
1272 }
1273
1274 return wxNOT_FOUND;
1275 }
1276
1277 void wxMenu::Attach(wxMenuBarBase *menubar)
1278 {
1279 wxMenuBase::Attach(menubar);
1280
1281 // inherit layout direction from menubar.
1282 SetLayoutDirection(menubar->GetLayoutDirection());
1283 }
1284
1285 // ----------------------------------------------------------------------------
1286 // helpers
1287 // ----------------------------------------------------------------------------
1288
1289 #if wxUSE_ACCEL
1290
1291 static wxString GetGtkHotKey( const wxMenuItem& item )
1292 {
1293 wxString hotkey;
1294
1295 wxAcceleratorEntry *accel = item.GetAccel();
1296 if ( accel )
1297 {
1298 int flags = accel->GetFlags();
1299 if ( flags & wxACCEL_ALT )
1300 hotkey += wxT("<alt>");
1301 if ( flags & wxACCEL_CTRL )
1302 hotkey += wxT("<control>");
1303 if ( flags & wxACCEL_SHIFT )
1304 hotkey += wxT("<shift>");
1305
1306 int code = accel->GetKeyCode();
1307 switch ( code )
1308 {
1309 case WXK_F1:
1310 case WXK_F2:
1311 case WXK_F3:
1312 case WXK_F4:
1313 case WXK_F5:
1314 case WXK_F6:
1315 case WXK_F7:
1316 case WXK_F8:
1317 case WXK_F9:
1318 case WXK_F10:
1319 case WXK_F11:
1320 case WXK_F12:
1321 case WXK_F13:
1322 case WXK_F14:
1323 case WXK_F15:
1324 case WXK_F16:
1325 case WXK_F17:
1326 case WXK_F18:
1327 case WXK_F19:
1328 case WXK_F20:
1329 case WXK_F21:
1330 case WXK_F22:
1331 case WXK_F23:
1332 case WXK_F24:
1333 hotkey += wxString::Format(wxT("F%d"), code - WXK_F1 + 1);
1334 break;
1335
1336 // TODO: we should use gdk_keyval_name() (a.k.a.
1337 // XKeysymToString) here as well as hardcoding the keysym
1338 // names this might be not portable
1339 case WXK_INSERT:
1340 hotkey << wxT("Insert" );
1341 break;
1342 case WXK_DELETE:
1343 hotkey << wxT("Delete" );
1344 break;
1345 case WXK_UP:
1346 hotkey << wxT("Up" );
1347 break;
1348 case WXK_DOWN:
1349 hotkey << wxT("Down" );
1350 break;
1351 case WXK_PAGEUP:
1352 hotkey << wxT("Page_Up" );
1353 break;
1354 case WXK_PAGEDOWN:
1355 hotkey << wxT("Page_Down" );
1356 break;
1357 case WXK_LEFT:
1358 hotkey << wxT("Left" );
1359 break;
1360 case WXK_RIGHT:
1361 hotkey << wxT("Right" );
1362 break;
1363 case WXK_HOME:
1364 hotkey << wxT("Home" );
1365 break;
1366 case WXK_END:
1367 hotkey << wxT("End" );
1368 break;
1369 case WXK_RETURN:
1370 hotkey << wxT("Return" );
1371 break;
1372 case WXK_BACK:
1373 hotkey << wxT("BackSpace" );
1374 break;
1375 case WXK_TAB:
1376 hotkey << wxT("Tab" );
1377 break;
1378 case WXK_ESCAPE:
1379 hotkey << wxT("Esc" );
1380 break;
1381 case WXK_SPACE:
1382 hotkey << wxT("space" );
1383 break;
1384 case WXK_MULTIPLY:
1385 hotkey << wxT("Multiply" );
1386 break;
1387 case WXK_ADD:
1388 hotkey << wxT("Add" );
1389 break;
1390 case WXK_SEPARATOR:
1391 hotkey << wxT("Separator" );
1392 break;
1393 case WXK_SUBTRACT:
1394 hotkey << wxT("Subtract" );
1395 break;
1396 case WXK_DECIMAL:
1397 hotkey << wxT("Decimal" );
1398 break;
1399 case WXK_DIVIDE:
1400 hotkey << wxT("Divide" );
1401 break;
1402 case WXK_CANCEL:
1403 hotkey << wxT("Cancel" );
1404 break;
1405 case WXK_CLEAR:
1406 hotkey << wxT("Clear" );
1407 break;
1408 case WXK_MENU:
1409 hotkey << wxT("Menu" );
1410 break;
1411 case WXK_PAUSE:
1412 hotkey << wxT("Pause" );
1413 break;
1414 case WXK_CAPITAL:
1415 hotkey << wxT("Capital" );
1416 break;
1417 case WXK_SELECT:
1418 hotkey << wxT("Select" );
1419 break;
1420 case WXK_PRINT:
1421 hotkey << wxT("Print" );
1422 break;
1423 case WXK_EXECUTE:
1424 hotkey << wxT("Execute" );
1425 break;
1426 case WXK_SNAPSHOT:
1427 hotkey << wxT("Snapshot" );
1428 break;
1429 case WXK_HELP:
1430 hotkey << wxT("Help" );
1431 break;
1432 case WXK_NUMLOCK:
1433 hotkey << wxT("Num_Lock" );
1434 break;
1435 case WXK_SCROLL:
1436 hotkey << wxT("Scroll_Lock" );
1437 break;
1438 case WXK_NUMPAD_INSERT:
1439 hotkey << wxT("KP_Insert" );
1440 break;
1441 case WXK_NUMPAD_DELETE:
1442 hotkey << wxT("KP_Delete" );
1443 break;
1444 case WXK_NUMPAD_SPACE:
1445 hotkey << wxT("KP_Space" );
1446 break;
1447 case WXK_NUMPAD_TAB:
1448 hotkey << wxT("KP_Tab" );
1449 break;
1450 case WXK_NUMPAD_ENTER:
1451 hotkey << wxT("KP_Enter" );
1452 break;
1453 case WXK_NUMPAD_F1: case WXK_NUMPAD_F2: case WXK_NUMPAD_F3:
1454 case WXK_NUMPAD_F4:
1455 hotkey += wxString::Format(wxT("KP_F%d"), code - WXK_NUMPAD_F1 + 1);
1456 break;
1457 case WXK_NUMPAD_HOME:
1458 hotkey << wxT("KP_Home" );
1459 break;
1460 case WXK_NUMPAD_LEFT:
1461 hotkey << wxT("KP_Left" );
1462 break;
1463 case WXK_NUMPAD_UP:
1464 hotkey << wxT("KP_Up" );
1465 break;
1466 case WXK_NUMPAD_RIGHT:
1467 hotkey << wxT("KP_Right" );
1468 break;
1469 case WXK_NUMPAD_DOWN:
1470 hotkey << wxT("KP_Down" );
1471 break;
1472 case WXK_NUMPAD_PAGEUP:
1473 hotkey << wxT("KP_Page_Up" );
1474 break;
1475 case WXK_NUMPAD_PAGEDOWN:
1476 hotkey << wxT("KP_Page_Down" );
1477 break;
1478 case WXK_NUMPAD_END:
1479 hotkey << wxT("KP_End" );
1480 break;
1481 case WXK_NUMPAD_BEGIN:
1482 hotkey << wxT("KP_Begin" );
1483 break;
1484 case WXK_NUMPAD_EQUAL:
1485 hotkey << wxT("KP_Equal" );
1486 break;
1487 case WXK_NUMPAD_MULTIPLY:
1488 hotkey << wxT("KP_Multiply" );
1489 break;
1490 case WXK_NUMPAD_ADD:
1491 hotkey << wxT("KP_Add" );
1492 break;
1493 case WXK_NUMPAD_SEPARATOR:
1494 hotkey << wxT("KP_Separator" );
1495 break;
1496 case WXK_NUMPAD_SUBTRACT:
1497 hotkey << wxT("KP_Subtract" );
1498 break;
1499 case WXK_NUMPAD_DECIMAL:
1500 hotkey << wxT("KP_Decimal" );
1501 break;
1502 case WXK_NUMPAD_DIVIDE:
1503 hotkey << wxT("KP_Divide" );
1504 break;
1505 case WXK_NUMPAD0: case WXK_NUMPAD1: case WXK_NUMPAD2:
1506 case WXK_NUMPAD3: case WXK_NUMPAD4: case WXK_NUMPAD5:
1507 case WXK_NUMPAD6: case WXK_NUMPAD7: case WXK_NUMPAD8: case WXK_NUMPAD9:
1508 hotkey += wxString::Format(wxT("KP_%d"), code - WXK_NUMPAD0);
1509 break;
1510 case WXK_WINDOWS_LEFT:
1511 hotkey << wxT("Super_L" );
1512 break;
1513 case WXK_WINDOWS_RIGHT:
1514 hotkey << wxT("Super_R" );
1515 break;
1516 case WXK_WINDOWS_MENU:
1517 hotkey << wxT("Menu" );
1518 break;
1519 case WXK_COMMAND:
1520 hotkey << wxT("Command" );
1521 break;
1522 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
1523 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1524 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1525 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1526 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
1527 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1528 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1529 break;
1530 */
1531 // if there are any other keys wxAcceleratorEntry::Create() may
1532 // return, we should process them here
1533
1534 default:
1535 if ( code < 127 )
1536 {
1537 const wxString
1538 name = wxGTK_CONV_BACK_SYS(gdk_keyval_name((guint)code));
1539 if ( !name.empty() )
1540 {
1541 hotkey << name;
1542 break;
1543 }
1544 }
1545
1546 wxFAIL_MSG( wxT("unknown keyboard accel") );
1547 }
1548
1549 delete accel;
1550 }
1551
1552 return hotkey;
1553 }
1554
1555 #endif // wxUSE_ACCEL
1556
1557 // ----------------------------------------------------------------------------
1558 // Pop-up menu stuff
1559 // ----------------------------------------------------------------------------
1560
1561 #if wxUSE_MENUS_NATIVE
1562
1563 extern "C" WXDLLIMPEXP_CORE
1564 void gtk_pop_hide_callback( GtkWidget *WXUNUSED(widget), bool* is_waiting )
1565 {
1566 *is_waiting = false;
1567 }
1568
1569 WXDLLIMPEXP_CORE void SetInvokingWindow( wxMenu *menu, wxWindow* win )
1570 {
1571 menu->SetInvokingWindow( win );
1572
1573 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
1574 while (node)
1575 {
1576 wxMenuItem *menuitem = node->GetData();
1577 if (menuitem->IsSubMenu())
1578 {
1579 SetInvokingWindow( menuitem->GetSubMenu(), win );
1580 }
1581
1582 node = node->GetNext();
1583 }
1584 }
1585
1586 extern "C" WXDLLIMPEXP_CORE
1587 void wxPopupMenuPositionCallback( GtkMenu *menu,
1588 gint *x, gint *y,
1589 gboolean * WXUNUSED(whatever),
1590 gpointer user_data )
1591 {
1592 // ensure that the menu appears entirely on screen
1593 GtkRequisition req;
1594 gtk_widget_get_child_requisition(GTK_WIDGET(menu), &req);
1595
1596 wxSize sizeScreen = wxGetDisplaySize();
1597 wxPoint *pos = (wxPoint*)user_data;
1598
1599 gint xmax = sizeScreen.x - req.width,
1600 ymax = sizeScreen.y - req.height;
1601
1602 *x = pos->x < xmax ? pos->x : xmax;
1603 *y = pos->y < ymax ? pos->y : ymax;
1604 }
1605
1606 bool wxWindowGTK::DoPopupMenu( wxMenu *menu, int x, int y )
1607 {
1608 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid window") );
1609
1610 wxCHECK_MSG( menu != NULL, false, wxT("invalid popup-menu") );
1611
1612 // NOTE: if you change this code, you need to update
1613 // the same code in taskbar.cpp as well. This
1614 // is ugly code duplication, I know.
1615
1616 SetInvokingWindow( menu, this );
1617
1618 menu->UpdateUI();
1619
1620 bool is_waiting = true;
1621
1622 gulong handler = g_signal_connect (menu->m_menu, "hide",
1623 G_CALLBACK (gtk_pop_hide_callback),
1624 &is_waiting);
1625
1626 wxPoint pos;
1627 gpointer userdata;
1628 GtkMenuPositionFunc posfunc;
1629 if ( x == -1 && y == -1 )
1630 {
1631 // use GTK's default positioning algorithm
1632 userdata = NULL;
1633 posfunc = NULL;
1634 }
1635 else
1636 {
1637 pos = ClientToScreen(wxPoint(x, y));
1638 userdata = &pos;
1639 posfunc = wxPopupMenuPositionCallback;
1640 }
1641
1642 wxMenuEvent eventOpen(wxEVT_MENU_OPEN, -1, menu);
1643 DoCommonMenuCallbackCode(menu, eventOpen);
1644
1645 gtk_menu_popup(
1646 GTK_MENU(menu->m_menu),
1647 (GtkWidget *) NULL, // parent menu shell
1648 (GtkWidget *) NULL, // parent menu item
1649 posfunc, // function to position it
1650 userdata, // client data
1651 0, // button used to activate it
1652 gtk_get_current_event_time()
1653 );
1654
1655 while (is_waiting)
1656 {
1657 gtk_main_iteration();
1658 }
1659
1660 g_signal_handler_disconnect (menu->m_menu, handler);
1661
1662 wxMenuEvent eventClose(wxEVT_MENU_CLOSE, -1, menu);
1663 DoCommonMenuCallbackCode(menu, eventClose);
1664
1665 return true;
1666 }
1667
1668 #endif // wxUSE_MENUS_NATIVE
1669
1670 #ifdef __WXGTK20__
1671
1672 #include <gtk/gtk.h>
1673
1674 const char *wxGetStockGtkID(wxWindowID id)
1675 {
1676 #define STOCKITEM(wx,gtk) \
1677 case wx: \
1678 return gtk;
1679
1680 #define STOCKITEM_MISSING(wx) \
1681 case wx: \
1682 return NULL;
1683
1684 #if GTK_CHECK_VERSION(2,4,0)
1685 #define STOCKITEM_24(wx,gtk) STOCKITEM(wx,gtk)
1686 #else
1687 #define STOCKITEM_24(wx,gtk) STOCKITEM_MISSING(wx)
1688 #endif
1689
1690 #if GTK_CHECK_VERSION(2,6,0)
1691 #define STOCKITEM_26(wx,gtk) STOCKITEM(wx,gtk)
1692 #else
1693 #define STOCKITEM_26(wx,gtk) STOCKITEM_MISSING(wx)
1694 #endif
1695
1696 #if GTK_CHECK_VERSION(2,10,0)
1697 #define STOCKITEM_210(wx,gtk) STOCKITEM(wx,gtk)
1698 #else
1699 #define STOCKITEM_210(wx,gtk) STOCKITEM_MISSING(wx)
1700 #endif
1701
1702
1703 switch (id)
1704 {
1705 STOCKITEM_26(wxID_ABOUT, GTK_STOCK_ABOUT)
1706 STOCKITEM(wxID_ADD, GTK_STOCK_ADD)
1707 STOCKITEM(wxID_APPLY, GTK_STOCK_APPLY)
1708 STOCKITEM(wxID_BOLD, GTK_STOCK_BOLD)
1709 STOCKITEM(wxID_CANCEL, GTK_STOCK_CANCEL)
1710 STOCKITEM(wxID_CLEAR, GTK_STOCK_CLEAR)
1711 STOCKITEM(wxID_CLOSE, GTK_STOCK_CLOSE)
1712 STOCKITEM(wxID_COPY, GTK_STOCK_COPY)
1713 STOCKITEM(wxID_CUT, GTK_STOCK_CUT)
1714 STOCKITEM(wxID_DELETE, GTK_STOCK_DELETE)
1715 STOCKITEM_26(wxID_EDIT, GTK_STOCK_EDIT)
1716 STOCKITEM(wxID_FIND, GTK_STOCK_FIND)
1717 STOCKITEM_26(wxID_FILE, GTK_STOCK_FILE)
1718 STOCKITEM(wxID_REPLACE, GTK_STOCK_FIND_AND_REPLACE)
1719 STOCKITEM(wxID_BACKWARD, GTK_STOCK_GO_BACK)
1720 STOCKITEM(wxID_DOWN, GTK_STOCK_GO_DOWN)
1721 STOCKITEM(wxID_FORWARD, GTK_STOCK_GO_FORWARD)
1722 STOCKITEM(wxID_UP, GTK_STOCK_GO_UP)
1723 STOCKITEM(wxID_HELP, GTK_STOCK_HELP)
1724 STOCKITEM(wxID_HOME, GTK_STOCK_HOME)
1725 STOCKITEM_24(wxID_INDENT, GTK_STOCK_INDENT)
1726 STOCKITEM(wxID_INDEX, GTK_STOCK_INDEX)
1727 STOCKITEM(wxID_ITALIC, GTK_STOCK_ITALIC)
1728 STOCKITEM(wxID_JUSTIFY_CENTER, GTK_STOCK_JUSTIFY_CENTER)
1729 STOCKITEM(wxID_JUSTIFY_FILL, GTK_STOCK_JUSTIFY_FILL)
1730 STOCKITEM(wxID_JUSTIFY_LEFT, GTK_STOCK_JUSTIFY_LEFT)
1731 STOCKITEM(wxID_JUSTIFY_RIGHT, GTK_STOCK_JUSTIFY_RIGHT)
1732 STOCKITEM(wxID_NEW, GTK_STOCK_NEW)
1733 STOCKITEM(wxID_NO, GTK_STOCK_NO)
1734 STOCKITEM(wxID_OK, GTK_STOCK_OK)
1735 STOCKITEM(wxID_OPEN, GTK_STOCK_OPEN)
1736 STOCKITEM(wxID_PASTE, GTK_STOCK_PASTE)
1737 STOCKITEM(wxID_PREFERENCES, GTK_STOCK_PREFERENCES)
1738 STOCKITEM(wxID_PRINT, GTK_STOCK_PRINT)
1739 STOCKITEM(wxID_PREVIEW, GTK_STOCK_PRINT_PREVIEW)
1740 STOCKITEM(wxID_PROPERTIES, GTK_STOCK_PROPERTIES)
1741 STOCKITEM(wxID_EXIT, GTK_STOCK_QUIT)
1742 STOCKITEM(wxID_REDO, GTK_STOCK_REDO)
1743 STOCKITEM(wxID_REFRESH, GTK_STOCK_REFRESH)
1744 STOCKITEM(wxID_REMOVE, GTK_STOCK_REMOVE)
1745 STOCKITEM(wxID_REVERT_TO_SAVED, GTK_STOCK_REVERT_TO_SAVED)
1746 STOCKITEM(wxID_SAVE, GTK_STOCK_SAVE)
1747 STOCKITEM(wxID_SAVEAS, GTK_STOCK_SAVE_AS)
1748 STOCKITEM_210(wxID_SELECTALL, GTK_STOCK_SELECT_ALL)
1749 STOCKITEM(wxID_STOP, GTK_STOCK_STOP)
1750 STOCKITEM(wxID_UNDELETE, GTK_STOCK_UNDELETE)
1751 STOCKITEM(wxID_UNDERLINE, GTK_STOCK_UNDERLINE)
1752 STOCKITEM(wxID_UNDO, GTK_STOCK_UNDO)
1753 STOCKITEM_24(wxID_UNINDENT, GTK_STOCK_UNINDENT)
1754 STOCKITEM(wxID_YES, GTK_STOCK_YES)
1755 STOCKITEM(wxID_ZOOM_100, GTK_STOCK_ZOOM_100)
1756 STOCKITEM(wxID_ZOOM_FIT, GTK_STOCK_ZOOM_FIT)
1757 STOCKITEM(wxID_ZOOM_IN, GTK_STOCK_ZOOM_IN)
1758 STOCKITEM(wxID_ZOOM_OUT, GTK_STOCK_ZOOM_OUT)
1759
1760 default:
1761 wxFAIL_MSG( _T("invalid stock item ID") );
1762 break;
1763 };
1764
1765 #undef STOCKITEM
1766
1767 return NULL;
1768 }
1769
1770 bool wxGetStockGtkAccelerator(const char *id, GdkModifierType *mod, guint *key)
1771 {
1772 if (!id)
1773 return false;
1774
1775 GtkStockItem stock_item;
1776 if (gtk_stock_lookup (id, &stock_item))
1777 {
1778 if (key) *key = stock_item.keyval;
1779 if (mod) *mod = stock_item.modifier;
1780
1781 // some GTK stock items have zero values for the keyval;
1782 // it means that they do not have an accelerator...
1783 if (stock_item.keyval)
1784 return true;
1785 }
1786
1787 return false;
1788 }
1789
1790 #endif // __WXGTK20__