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