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