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