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