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