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