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