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