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