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