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