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