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