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