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