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