]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/menu.cpp
Allow wxGridCellNumberEditor::StartingKey to work when the control is
[wxWidgets.git] / src / gtk1 / menu.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: menu.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "menu.h"
12 #pragma implementation "menuitem.h"
13 #endif
14
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
17
18 #include "wx/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
50 static const int wxGTK_TITLE_ID = -3;
51
52 //-----------------------------------------------------------------------------
53 // idle system
54 //-----------------------------------------------------------------------------
55
56 extern void wxapp_install_idle_handler();
57 extern bool g_isIdle;
58
59 #if wxUSE_ACCEL
60 static wxString GetGtkHotKey( const wxMenuItem& item );
61 #endif
62
63 //-----------------------------------------------------------------------------
64 // substitute for missing GtkPixmapMenuItem
65 //-----------------------------------------------------------------------------
66
67 #ifndef __WXGTK20__
68
69 #define GTK_TYPE_PIXMAP_MENU_ITEM (gtk_pixmap_menu_item_get_type ())
70 #define GTK_PIXMAP_MENU_ITEM(obj) (GTK_CHECK_CAST ((obj), GTK_TYPE_PIXMAP_MENU_ITEM, GtkPixmapMenuItem))
71 #define GTK_PIXMAP_MENU_ITEM_CLASS(klass) (GTK_CHECK_CLASS_CAST ((klass), GTK_TYPE_PIXMAP_MENU_ITEM, GtkPixmapMenuItemClass))
72 #define GTK_IS_PIXMAP_MENU_ITEM(obj) (GTK_CHECK_TYPE ((obj), GTK_TYPE_PIXMAP_MENU_ITEM))
73 #define GTK_IS_PIXMAP_MENU_ITEM_CLASS(klass) (GTK_CHECK_CLASS_TYPE ((klass), GTK_TYPE_PIXMAP_MENU_ITEM))
74 //#define GTK_PIXMAP_MENU_ITEM_GET_CLASS(obj) (GTK_CHECK_GET_CLASS ((obj), GTK_TYPE_PIXMAP_MENU_ITEM))
75 #define GTK_PIXMAP_MENU_ITEM_GET_CLASS(obj) (GTK_PIXMAP_MENU_ITEM_CLASS( GTK_OBJECT_GET_CLASS(obj)))
76
77 #ifndef GTK_MENU_ITEM_GET_CLASS
78 #define GTK_MENU_ITEM_GET_CLASS(obj) (GTK_MENU_ITEM_CLASS( GTK_OBJECT_GET_CLASS(obj)))
79 #endif
80
81 typedef struct _GtkPixmapMenuItem GtkPixmapMenuItem;
82 typedef struct _GtkPixmapMenuItemClass GtkPixmapMenuItemClass;
83
84 struct _GtkPixmapMenuItem
85 {
86 GtkMenuItem menu_item;
87
88 GtkWidget *pixmap;
89 };
90
91 struct _GtkPixmapMenuItemClass
92 {
93 GtkMenuItemClass parent_class;
94
95 guint orig_toggle_size;
96 guint have_pixmap_count;
97 };
98
99 extern "C" {
100 GtkType gtk_pixmap_menu_item_get_type (void);
101 GtkWidget* gtk_pixmap_menu_item_new (void);
102 void gtk_pixmap_menu_item_set_pixmap (GtkPixmapMenuItem *menu_item,
103 GtkWidget *pixmap);
104 }
105
106 #endif // !__WXGTK20__
107
108 //-----------------------------------------------------------------------------
109 // idle system
110 //-----------------------------------------------------------------------------
111
112 static 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
154 extern "C" {
155 static 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
175 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
176
177 void 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
215 wxMenuBar::wxMenuBar(size_t n, wxMenu *menus[], const wxString titles[], long style)
216 {
217 Init(n, menus, titles, style);
218 }
219
220 wxMenuBar::wxMenuBar(long style)
221 {
222 Init(0, NULL, NULL, style);
223 }
224
225 wxMenuBar::wxMenuBar()
226 {
227 Init(0, NULL, NULL, 0);
228 }
229
230 wxMenuBar::~wxMenuBar()
231 {
232 }
233
234 static 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
257 static 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
280 void 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
303 void 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
324 bool 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
332 bool 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
394 bool 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
407 wxMenu *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
420 wxMenu *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
444 static 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
466 int 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
482 static 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
500 wxMenuItem* 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
519 void 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
531 wxString 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
558 void 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
588 extern "C" {
589 static 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
665 extern "C" {
666 static 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
693 extern "C" {
694 static 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
722 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
723
724 wxMenuItem *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
734 wxMenuItem::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
745 wxMenuItem::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
757 void wxMenuItem::Init(const wxString& text)
758 {
759 m_labelWidget = (GtkWidget *) NULL;
760 m_menuItem = (GtkWidget *) NULL;
761
762 DoSetText(text);
763 }
764
765 wxMenuItem::~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 */
772 wxString 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
814 void wxMenuItem::SetText( const wxString& str )
815 {
816 // Some optimization to avoid flicker
817 wxString oldLabel = m_text;
818 oldLabel = wxStripMenuCodes(oldLabel);
819 oldLabel.Replace(wxT("_"), wxT(""));
820 wxString label1 = wxStripMenuCodes(str);
821 wxString oldhotkey = GetHotKey(); // Store the old hotkey in Ctrl-foo format
822 wxCharBuffer oldbuf = wxGTK_CONV( GetGtkHotKey(*this) ); // and as <control>foo
823
824 DoSetText(str);
825
826 if (oldLabel == label1 &&
827 oldhotkey == GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
828 return;
829
830 if (m_menuItem)
831 {
832 GtkLabel *label;
833 if (m_labelWidget)
834 label = (GtkLabel*) m_labelWidget;
835 else
836 label = GTK_LABEL( GTK_BIN(m_menuItem)->child );
837
838 #ifdef __WXGTK20__
839 gtk_label_set_text_with_mnemonic( GTK_LABEL(label), wxGTK_CONV(m_text) );
840 #else
841 // set new text
842 gtk_label_set( label, wxGTK_CONV( m_text ) );
843
844 // reparse key accel
845 (void)gtk_label_parse_uline (GTK_LABEL(label), wxGTK_CONV(m_text) );
846 gtk_accel_label_refetch( GTK_ACCEL_LABEL(label) );
847 #endif
848 }
849
850 guint accel_key;
851 GdkModifierType accel_mods;
852 gtk_accelerator_parse( (const char*) oldbuf, &accel_key, &accel_mods);
853 if (accel_key != 0)
854 {
855 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem),
856 m_parentMenu->m_accel,
857 accel_key,
858 accel_mods );
859 }
860
861 wxCharBuffer buf = wxGTK_CONV( GetGtkHotKey(*this) );
862 gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods);
863 if (accel_key != 0)
864 {
865 gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem),
866 "activate",
867 m_parentMenu->m_accel,
868 accel_key,
869 accel_mods,
870 GTK_ACCEL_VISIBLE);
871 }
872 }
873
874 // it's valid for this function to be called even if m_menuItem == NULL
875 void wxMenuItem::DoSetText( const wxString& str )
876 {
877 // '\t' is the deliminator indicating a hot key
878 m_text.Empty();
879 const wxChar *pc = str;
880 while ( (*pc != wxT('\0')) && (*pc != wxT('\t')) )
881 {
882 if ((*pc == wxT('&')) && (*(pc+1) == wxT('&')))
883 {
884 // "&" is doubled to indicate "&" instead of accelerator
885 ++pc;
886 m_text << wxT('&');
887 }
888 else if (*pc == wxT('&'))
889 {
890 m_text << wxT('_');
891 }
892 else if ( *pc == wxT('_') ) // escape underscores
893 {
894 m_text << wxT("__");
895 }
896 else
897 {
898 m_text << *pc;
899 }
900 ++pc;
901 }
902
903 m_hotKey = wxT("");
904
905 if(*pc == wxT('\t'))
906 {
907 pc++;
908 m_hotKey = pc;
909 }
910
911 // wxPrintf( wxT("DoSetText(): str %s m_text %s hotkey %s\n"), str.c_str(), m_text.c_str(), m_hotKey.c_str() );
912 }
913
914 #if wxUSE_ACCEL
915
916 wxAcceleratorEntry *wxMenuItem::GetAccel() const
917 {
918 if ( !GetHotKey() )
919 {
920 // nothing
921 return (wxAcceleratorEntry *)NULL;
922 }
923
924 // as wxGetAccelFromString() looks for TAB, insert a dummy one here
925 wxString label;
926 label << wxT('\t') << GetHotKey();
927
928 return wxGetAccelFromString(label);
929 }
930
931 #endif // wxUSE_ACCEL
932
933 void wxMenuItem::Check( bool check )
934 {
935 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
936
937 if (check == m_isChecked)
938 return;
939
940 wxMenuItemBase::Check( check );
941
942 switch ( GetKind() )
943 {
944 case wxITEM_CHECK:
945 case wxITEM_RADIO:
946 gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check );
947 break;
948
949 default:
950 wxFAIL_MSG( _T("can't check this item") );
951 }
952 }
953
954 void wxMenuItem::Enable( bool enable )
955 {
956 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
957
958 gtk_widget_set_sensitive( m_menuItem, enable );
959 wxMenuItemBase::Enable( enable );
960 }
961
962 bool wxMenuItem::IsChecked() const
963 {
964 wxCHECK_MSG( m_menuItem, FALSE, wxT("invalid menu item") );
965
966 wxCHECK_MSG( IsCheckable(), FALSE,
967 wxT("can't get state of uncheckable item!") );
968
969 return ((GtkCheckMenuItem*)m_menuItem)->active != 0;
970 }
971
972 //-----------------------------------------------------------------------------
973 // wxMenu
974 //-----------------------------------------------------------------------------
975
976 IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
977
978 void wxMenu::Init()
979 {
980 m_accel = gtk_accel_group_new();
981 m_menu = gtk_menu_new();
982 // NB: keep reference to the menu so that it is not destroyed behind
983 // our back by GTK+ e.g. when it is removed from menubar:
984 gtk_widget_ref(m_menu);
985
986 m_owner = (GtkWidget*) NULL;
987
988 // Tearoffs are entries, just like separators. So if we want this
989 // menu to be a tear-off one, we just append a tearoff entry
990 // immediately.
991 if ( m_style & wxMENU_TEAROFF )
992 {
993 GtkWidget *tearoff = gtk_tearoff_menu_item_new();
994
995 gtk_menu_append(GTK_MENU(m_menu), tearoff);
996 }
997
998 m_prevRadio = NULL;
999
1000 // append the title as the very first entry if we have it
1001 if ( !m_title.empty() )
1002 {
1003 Append(wxGTK_TITLE_ID, m_title);
1004 AppendSeparator();
1005 }
1006 }
1007
1008 wxMenu::~wxMenu()
1009 {
1010 WX_CLEAR_LIST(wxMenuItemList, m_items);
1011
1012 if ( GTK_IS_WIDGET( m_menu ))
1013 {
1014 // see wxMenu::Init
1015 gtk_widget_unref( m_menu );
1016 // if the menu is inserted in another menu at this time, there was
1017 // one more reference to it:
1018 if ( m_owner )
1019 gtk_widget_destroy( m_menu );
1020 }
1021 }
1022
1023 bool wxMenu::GtkAppend(wxMenuItem *mitem, int pos)
1024 {
1025 GtkWidget *menuItem;
1026
1027 wxString text;
1028 #ifndef __WXGTK20__
1029 GtkLabel* label;
1030 #endif
1031
1032 if ( mitem->IsSeparator() )
1033 {
1034 #ifdef __WXGTK20__
1035 menuItem = gtk_separator_menu_item_new();
1036 #else
1037 // TODO
1038 menuItem = gtk_menu_item_new();
1039 #endif
1040 }
1041 else if (mitem->GetBitmap().Ok())
1042 {
1043 text = mitem->GetText();
1044 const wxBitmap *bitmap = &mitem->GetBitmap();
1045
1046 #ifdef __WXGTK20__
1047 menuItem = gtk_image_menu_item_new_with_mnemonic( wxGTK_CONV( text ) );
1048
1049 GtkWidget *image;
1050 if (bitmap->HasPixbuf())
1051 {
1052 image = gtk_image_new_from_pixbuf(bitmap->GetPixbuf());
1053 }
1054 else
1055 {
1056 GdkPixmap *gdk_pixmap = bitmap->GetPixmap();
1057 GdkBitmap *gdk_bitmap = bitmap->GetMask() ?
1058 bitmap->GetMask()->GetBitmap() :
1059 (GdkBitmap*) NULL;
1060 image = gtk_image_new_from_pixmap( gdk_pixmap, gdk_bitmap );
1061 }
1062
1063 gtk_widget_show(image);
1064
1065 gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM(menuItem), image );
1066
1067 #else
1068 GdkPixmap *gdk_pixmap = bitmap->GetPixmap();
1069 GdkBitmap *gdk_bitmap = bitmap->GetMask() ? bitmap->GetMask()->GetBitmap() : (GdkBitmap*) NULL;
1070
1071 menuItem = gtk_pixmap_menu_item_new ();
1072 label = GTK_LABEL(gtk_accel_label_new ( wxGTK_CONV( text ) ));
1073 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1074 gtk_container_add (GTK_CONTAINER (menuItem), GTK_WIDGET(label));
1075
1076 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), menuItem);
1077
1078 gtk_widget_show (GTK_WIDGET(label));
1079
1080 mitem->SetLabelWidget(GTK_WIDGET(label));
1081
1082 GtkWidget* pixmap = gtk_pixmap_new( gdk_pixmap, gdk_bitmap );
1083 gtk_widget_show(pixmap);
1084 gtk_pixmap_menu_item_set_pixmap(GTK_PIXMAP_MENU_ITEM( menuItem ), pixmap);
1085
1086 #endif
1087
1088 m_prevRadio = NULL;
1089 }
1090 else // a normal item
1091 {
1092 // text has "_" instead of "&" after mitem->SetText() so don't use it
1093 text = mitem->GetText() ;
1094
1095 switch ( mitem->GetKind() )
1096 {
1097 case wxITEM_CHECK:
1098 {
1099 #ifdef __WXGTK20__
1100 menuItem = gtk_check_menu_item_new_with_mnemonic( wxGTK_CONV( text ) );
1101 #else
1102 menuItem = gtk_check_menu_item_new_with_label( wxGTK_CONV( text ) );
1103 label = GTK_LABEL( GTK_BIN(menuItem)->child );
1104 // set new text
1105 gtk_label_set_text( label, wxGTK_CONV( text ) );
1106 #endif
1107 m_prevRadio = NULL;
1108 break;
1109 }
1110
1111 case wxITEM_RADIO:
1112 {
1113 GSList *group = NULL;
1114 if ( m_prevRadio == NULL )
1115 {
1116 // start of a new radio group
1117 #ifdef __WXGTK20__
1118 m_prevRadio = menuItem = gtk_radio_menu_item_new_with_mnemonic( group, wxGTK_CONV( text ) );
1119 #else
1120 m_prevRadio = menuItem = gtk_radio_menu_item_new_with_label( group, wxGTK_CONV( text ) );
1121 label = GTK_LABEL( GTK_BIN(menuItem)->child );
1122 // set new text
1123 gtk_label_set_text( label, wxGTK_CONV( text ) );
1124 #endif
1125 }
1126 else // continue the radio group
1127 {
1128 #ifdef __WXGTK20__
1129 group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (m_prevRadio));
1130 m_prevRadio = menuItem = gtk_radio_menu_item_new_with_mnemonic( group, wxGTK_CONV( text ) );
1131 #else
1132 group = gtk_radio_menu_item_group (GTK_RADIO_MENU_ITEM (m_prevRadio));
1133 m_prevRadio = menuItem = gtk_radio_menu_item_new_with_label( group, wxGTK_CONV( text ) );
1134 label = GTK_LABEL( GTK_BIN(menuItem)->child );
1135 #endif
1136 }
1137 break;
1138 }
1139
1140 default:
1141 wxFAIL_MSG( _T("unexpected menu item kind") );
1142 // fall through
1143
1144 case wxITEM_NORMAL:
1145 {
1146 #ifdef __WXGTK20__
1147 menuItem = gtk_menu_item_new_with_mnemonic( wxGTK_CONV( text ) );
1148 #else
1149 menuItem = gtk_menu_item_new_with_label( wxGTK_CONV( text ) );
1150 label = GTK_LABEL( GTK_BIN(menuItem)->child );
1151 #endif
1152 m_prevRadio = NULL;
1153 break;
1154 }
1155 }
1156
1157 }
1158
1159 guint accel_key;
1160 GdkModifierType accel_mods;
1161 wxCharBuffer buf = wxGTK_CONV( GetGtkHotKey(*mitem) );
1162
1163 // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetText().c_str(), GetGtkHotKey(*mitem).c_str() );
1164 gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods);
1165 if (accel_key != 0)
1166 {
1167 gtk_widget_add_accelerator (GTK_WIDGET(menuItem),
1168 "activate",
1169 m_accel,
1170 accel_key,
1171 accel_mods,
1172 GTK_ACCEL_VISIBLE);
1173 }
1174
1175 if (pos == -1)
1176 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu), menuItem);
1177 else
1178 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu), menuItem, pos);
1179
1180 gtk_widget_show( menuItem );
1181
1182 if ( !mitem->IsSeparator() )
1183 {
1184 wxASSERT_MSG( menuItem, wxT("invalid menuitem") );
1185
1186 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
1187 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
1188 (gpointer)this );
1189
1190 gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
1191 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
1192 (gpointer)this );
1193
1194 if ( mitem->IsSubMenu() && mitem->GetKind() != wxITEM_RADIO && mitem->GetKind() != wxITEM_CHECK )
1195 {
1196 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), mitem->GetSubMenu()->m_menu );
1197
1198 gtk_widget_show( mitem->GetSubMenu()->m_menu );
1199
1200 // if adding a submenu to a menu already existing in the menu bar, we
1201 // must set invoking window to allow processing events from this
1202 // submenu
1203 if ( m_invokingWindow )
1204 wxMenubarSetInvokingWindow(mitem->GetSubMenu(), m_invokingWindow);
1205 }
1206 else
1207 {
1208 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
1209 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
1210 (gpointer)this );
1211 }
1212 #ifndef __WXGTK20__
1213 guint accel_key = gtk_label_parse_uline (GTK_LABEL(label), wxGTK_CONV( text ) );
1214 if (accel_key != GDK_VoidSymbol)
1215 {
1216 gtk_widget_add_accelerator (menuItem,
1217 "activate_item",
1218 gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menu)),
1219 accel_key,
1220 GDK_MOD1_MASK,
1221 GTK_ACCEL_LOCKED);
1222 }
1223 #endif
1224 }
1225
1226 mitem->SetMenuItem(menuItem);
1227
1228 if (ms_locked)
1229 {
1230 // This doesn't even exist!
1231 // gtk_widget_lock_accelerators(mitem->GetMenuItem());
1232 }
1233
1234 return TRUE;
1235 }
1236
1237 wxMenuItem* wxMenu::DoAppend(wxMenuItem *mitem)
1238 {
1239 if (!GtkAppend(mitem))
1240 return NULL;
1241
1242 return wxMenuBase::DoAppend(mitem);
1243 }
1244
1245 wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
1246 {
1247 if ( !wxMenuBase::DoInsert(pos, item) )
1248 return NULL;
1249
1250 // TODO
1251 if ( !GtkAppend(item, (int)pos) )
1252 return NULL;
1253
1254 return item;
1255 }
1256
1257 wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
1258 {
1259 if ( !wxMenuBase::DoRemove(item) )
1260 return (wxMenuItem *)NULL;
1261
1262 // TODO: this code doesn't delete the item factory item and this seems
1263 // impossible as of GTK 1.2.6.
1264 gtk_widget_destroy( item->GetMenuItem() );
1265
1266 return item;
1267 }
1268
1269 int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
1270 {
1271 wxMenuItemList::compatibility_iterator node = m_items.GetFirst();
1272 while (node)
1273 {
1274 wxMenuItem *item = node->GetData();
1275 if (item->GetMenuItem() == menuItem)
1276 return item->GetId();
1277 node = node->GetNext();
1278 }
1279
1280 return wxNOT_FOUND;
1281 }
1282
1283 // ----------------------------------------------------------------------------
1284 // helpers
1285 // ----------------------------------------------------------------------------
1286
1287 #if wxUSE_ACCEL
1288
1289 static wxString GetGtkHotKey( const wxMenuItem& item )
1290 {
1291 wxString hotkey;
1292
1293 wxAcceleratorEntry *accel = item.GetAccel();
1294 if ( accel )
1295 {
1296 int flags = accel->GetFlags();
1297 if ( flags & wxACCEL_ALT )
1298 hotkey += wxT("<alt>");
1299 if ( flags & wxACCEL_CTRL )
1300 hotkey += wxT("<control>");
1301 if ( flags & wxACCEL_SHIFT )
1302 hotkey += wxT("<shift>");
1303
1304 int code = accel->GetKeyCode();
1305 switch ( code )
1306 {
1307 case WXK_F1:
1308 case WXK_F2:
1309 case WXK_F3:
1310 case WXK_F4:
1311 case WXK_F5:
1312 case WXK_F6:
1313 case WXK_F7:
1314 case WXK_F8:
1315 case WXK_F9:
1316 case WXK_F10:
1317 case WXK_F11:
1318 case WXK_F12:
1319 case WXK_F13:
1320 case WXK_F14:
1321 case WXK_F15:
1322 case WXK_F16:
1323 case WXK_F17:
1324 case WXK_F18:
1325 case WXK_F19:
1326 case WXK_F20:
1327 case WXK_F21:
1328 case WXK_F22:
1329 case WXK_F23:
1330 case WXK_F24:
1331 hotkey += wxString::Format(wxT("F%d"), code - WXK_F1 + 1);
1332 break;
1333
1334 // TODO: we should use gdk_keyval_name() (a.k.a.
1335 // XKeysymToString) here as well as hardcoding the keysym
1336 // names this might be not portable
1337 case WXK_INSERT:
1338 hotkey << wxT("Insert" );
1339 break;
1340 case WXK_DELETE:
1341 hotkey << wxT("Delete" );
1342 break;
1343 case WXK_UP:
1344 hotkey << wxT("Up" );
1345 break;
1346 case WXK_DOWN:
1347 hotkey << wxT("Down" );
1348 break;
1349 case WXK_PAGEUP:
1350 case WXK_PRIOR:
1351 hotkey << wxT("Prior" );
1352 break;
1353 case WXK_PAGEDOWN:
1354 case WXK_NEXT:
1355 hotkey << wxT("Next" );
1356 break;
1357 case WXK_LEFT:
1358 hotkey << wxT("Left" );
1359 break;
1360 case WXK_RIGHT:
1361 hotkey << wxT("Right" );
1362 break;
1363 case WXK_HOME:
1364 hotkey << wxT("Home" );
1365 break;
1366 case WXK_END:
1367 hotkey << wxT("End" );
1368 break;
1369 case WXK_RETURN:
1370 hotkey << wxT("Return" );
1371 break;
1372 case WXK_BACK:
1373 hotkey << wxT("BackSpace" );
1374 break;
1375 case WXK_TAB:
1376 hotkey << wxT("Tab" );
1377 break;
1378 case WXK_ESCAPE:
1379 hotkey << wxT("Esc" );
1380 break;
1381 case WXK_SPACE:
1382 hotkey << wxT("space" );
1383 break;
1384 case WXK_MULTIPLY:
1385 hotkey << wxT("Multiply" );
1386 break;
1387 case WXK_ADD:
1388 hotkey << wxT("Add" );
1389 break;
1390 case WXK_SEPARATOR:
1391 hotkey << wxT("Separator" );
1392 break;
1393 case WXK_SUBTRACT:
1394 hotkey << wxT("Subtract" );
1395 break;
1396 case WXK_DECIMAL:
1397 hotkey << wxT("Decimal" );
1398 break;
1399 case WXK_DIVIDE:
1400 hotkey << wxT("Divide" );
1401 break;
1402 case WXK_CANCEL:
1403 hotkey << wxT("Cancel" );
1404 break;
1405 case WXK_CLEAR:
1406 hotkey << wxT("Clear" );
1407 break;
1408 case WXK_MENU:
1409 hotkey << wxT("Menu" );
1410 break;
1411 case WXK_PAUSE:
1412 hotkey << wxT("Pause" );
1413 break;
1414 case WXK_CAPITAL:
1415 hotkey << wxT("Capital" );
1416 break;
1417 case WXK_SELECT:
1418 hotkey << wxT("Select" );
1419 break;
1420 case WXK_PRINT:
1421 hotkey << wxT("Print" );
1422 break;
1423 case WXK_EXECUTE:
1424 hotkey << wxT("Execute" );
1425 break;
1426 case WXK_SNAPSHOT:
1427 hotkey << wxT("Snapshot" );
1428 break;
1429 case WXK_HELP:
1430 hotkey << wxT("Help" );
1431 break;
1432 case WXK_NUMLOCK:
1433 hotkey << wxT("Num_Lock" );
1434 break;
1435 case WXK_SCROLL:
1436 hotkey << wxT("Scroll_Lock" );
1437 break;
1438 case WXK_NUMPAD_INSERT:
1439 hotkey << wxT("KP_Insert" );
1440 break;
1441 case WXK_NUMPAD_DELETE:
1442 hotkey << wxT("KP_Delete" );
1443 break;
1444 case WXK_NUMPAD_SPACE:
1445 hotkey << wxT("KP_Space" );
1446 break;
1447 case WXK_NUMPAD_TAB:
1448 hotkey << wxT("KP_Tab" );
1449 break;
1450 case WXK_NUMPAD_ENTER:
1451 hotkey << wxT("KP_Enter" );
1452 break;
1453 case WXK_NUMPAD_F1: case WXK_NUMPAD_F2: case WXK_NUMPAD_F3:
1454 case WXK_NUMPAD_F4:
1455 hotkey += wxString::Format(wxT("KP_F%d"), code - WXK_NUMPAD_F1 + 1);
1456 break;
1457 case WXK_NUMPAD_HOME:
1458 hotkey << wxT("KP_Home" );
1459 break;
1460 case WXK_NUMPAD_LEFT:
1461 hotkey << wxT("KP_Left" );
1462 break;
1463 case WXK_NUMPAD_UP:
1464 hotkey << wxT("KP_Up" );
1465 break;
1466 case WXK_NUMPAD_RIGHT:
1467 hotkey << wxT("KP_Right" );
1468 break;
1469 case WXK_NUMPAD_DOWN:
1470 hotkey << wxT("KP_Down" );
1471 break;
1472 case WXK_NUMPAD_PRIOR: case WXK_NUMPAD_PAGEUP:
1473 hotkey << wxT("KP_Prior" );
1474 break;
1475 case WXK_NUMPAD_NEXT: case WXK_NUMPAD_PAGEDOWN:
1476 hotkey << wxT("KP_Next" );
1477 break;
1478 case WXK_NUMPAD_END:
1479 hotkey << wxT("KP_End" );
1480 break;
1481 case WXK_NUMPAD_BEGIN:
1482 hotkey << wxT("KP_Begin" );
1483 break;
1484 case WXK_NUMPAD_EQUAL:
1485 hotkey << wxT("KP_Equal" );
1486 break;
1487 case WXK_NUMPAD_MULTIPLY:
1488 hotkey << wxT("KP_Multiply" );
1489 break;
1490 case WXK_NUMPAD_ADD:
1491 hotkey << wxT("KP_Add" );
1492 break;
1493 case WXK_NUMPAD_SEPARATOR:
1494 hotkey << wxT("KP_Separator" );
1495 break;
1496 case WXK_NUMPAD_SUBTRACT:
1497 hotkey << wxT("KP_Subtract" );
1498 break;
1499 case WXK_NUMPAD_DECIMAL:
1500 hotkey << wxT("KP_Decimal" );
1501 break;
1502 case WXK_NUMPAD_DIVIDE:
1503 hotkey << wxT("KP_Divide" );
1504 break;
1505 case WXK_NUMPAD0: case WXK_NUMPAD1: case WXK_NUMPAD2:
1506 case WXK_NUMPAD3: case WXK_NUMPAD4: case WXK_NUMPAD5:
1507 case WXK_NUMPAD6: case WXK_NUMPAD7: case WXK_NUMPAD8: case WXK_NUMPAD9:
1508 hotkey += wxString::Format(wxT("KP_%d"), code - WXK_NUMPAD0);
1509 break;
1510 case WXK_WINDOWS_LEFT:
1511 hotkey << wxT("Super_L" );
1512 break;
1513 case WXK_WINDOWS_RIGHT:
1514 hotkey << wxT("Super_R" );
1515 break;
1516 case WXK_WINDOWS_MENU:
1517 hotkey << wxT("Menu" );
1518 break;
1519 case WXK_COMMAND:
1520 hotkey << wxT("Command" );
1521 break;
1522 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
1523 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1524 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1525 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1526 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
1527 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1528 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1529 break;
1530 */
1531 // if there are any other keys wxGetAccelFromString() may
1532 // return, we should process them here
1533
1534 default:
1535 if ( code < 127 )
1536 {
1537 wxString name = wxGTK_CONV_BACK( gdk_keyval_name((guint)code) );
1538 if ( name )
1539 {
1540 hotkey << name;
1541 break;
1542 }
1543 }
1544
1545 wxFAIL_MSG( wxT("unknown keyboard accel") );
1546 }
1547
1548 delete accel;
1549 }
1550
1551 return hotkey;
1552 }
1553
1554 #endif // wxUSE_ACCEL
1555
1556
1557 //-----------------------------------------------------------------------------
1558 // substitute for missing GtkPixmapMenuItem
1559 //-----------------------------------------------------------------------------
1560
1561 #ifndef __WXGTK20__
1562
1563 /*
1564 * Copyright (C) 1998, 1999, 2000 Free Software Foundation
1565 * All rights reserved.
1566 *
1567 * This file is part of the Gnome Library.
1568 *
1569 * The Gnome Library is free software; you can redistribute it and/or
1570 * modify it under the terms of the GNU Library General Public License as
1571 * published by the Free Software Foundation; either version 2 of the
1572 * License, or (at your option) any later version.
1573 *
1574 * The Gnome Library is distributed in the hope that it will be useful,
1575 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1576 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1577 * Library General Public License for more details.
1578 *
1579 * You should have received a copy of the GNU Library General Public
1580 * License along with the Gnome Library; see the file COPYING.LIB. If not,
1581 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1582 * Boston, MA 02111-1307, USA.
1583 */
1584 /*
1585 @NOTATION@
1586 */
1587
1588 /* Author: Dietmar Maurer <dm@vlsivie.tuwien.ac.at> */
1589
1590 #include <gtk/gtkaccellabel.h>
1591 #include <gtk/gtksignal.h>
1592 #include <gtk/gtkmenuitem.h>
1593 #include <gtk/gtkmenu.h>
1594 #include <gtk/gtkcontainer.h>
1595
1596 extern "C"
1597 {
1598
1599 static void gtk_pixmap_menu_item_class_init (GtkPixmapMenuItemClass *klass);
1600 static void gtk_pixmap_menu_item_init (GtkPixmapMenuItem *menu_item);
1601 static void gtk_pixmap_menu_item_draw (GtkWidget *widget,
1602 GdkRectangle *area);
1603 static gint gtk_pixmap_menu_item_expose (GtkWidget *widget,
1604 GdkEventExpose *event);
1605
1606 /* we must override the following functions */
1607
1608 static void gtk_pixmap_menu_item_map (GtkWidget *widget);
1609 static void gtk_pixmap_menu_item_size_allocate (GtkWidget *widget,
1610 GtkAllocation *allocation);
1611 static void gtk_pixmap_menu_item_forall (GtkContainer *container,
1612 gboolean include_internals,
1613 GtkCallback callback,
1614 gpointer callback_data);
1615 static void gtk_pixmap_menu_item_size_request (GtkWidget *widget,
1616 GtkRequisition *requisition);
1617 static void gtk_pixmap_menu_item_remove (GtkContainer *container,
1618 GtkWidget *child);
1619
1620 static void changed_have_pixmap_status (GtkPixmapMenuItem *menu_item);
1621
1622 static GtkMenuItemClass *parent_class = NULL;
1623
1624 } // extern "C"
1625
1626 #define BORDER_SPACING 3
1627 #define PMAP_WIDTH 20
1628
1629 GtkType
1630 gtk_pixmap_menu_item_get_type (void)
1631 {
1632 static GtkType pixmap_menu_item_type = 0;
1633
1634 if (!pixmap_menu_item_type)
1635 {
1636 GtkTypeInfo pixmap_menu_item_info =
1637 {
1638 (char *)"GtkPixmapMenuItem",
1639 sizeof (GtkPixmapMenuItem),
1640 sizeof (GtkPixmapMenuItemClass),
1641 (GtkClassInitFunc) gtk_pixmap_menu_item_class_init,
1642 (GtkObjectInitFunc) gtk_pixmap_menu_item_init,
1643 /* reserved_1 */ NULL,
1644 /* reserved_2 */ NULL,
1645 (GtkClassInitFunc) NULL,
1646 };
1647
1648 pixmap_menu_item_type = gtk_type_unique (gtk_menu_item_get_type (),
1649 &pixmap_menu_item_info);
1650 }
1651
1652 return pixmap_menu_item_type;
1653 }
1654
1655 extern "C" {
1656
1657 /**
1658 * gtk_pixmap_menu_item_new
1659 *
1660 * Creates a new pixmap menu item. Use gtk_pixmap_menu_item_set_pixmap()
1661 * to set the pixmap wich is displayed at the left side.
1662 *
1663 * Returns:
1664 * &GtkWidget pointer to new menu item
1665 **/
1666
1667 GtkWidget*
1668 gtk_pixmap_menu_item_new (void)
1669 {
1670 return GTK_WIDGET (gtk_type_new (gtk_pixmap_menu_item_get_type ()));
1671 }
1672
1673 static void
1674 gtk_pixmap_menu_item_class_init (GtkPixmapMenuItemClass *klass)
1675 {
1676 GtkObjectClass *object_class;
1677 GtkWidgetClass *widget_class;
1678 GtkMenuItemClass *menu_item_class;
1679 GtkContainerClass *container_class;
1680
1681 object_class = (GtkObjectClass*) klass;
1682 widget_class = (GtkWidgetClass*) klass;
1683 menu_item_class = (GtkMenuItemClass*) klass;
1684 container_class = (GtkContainerClass*) klass;
1685
1686 parent_class = (GtkMenuItemClass*) gtk_type_class (gtk_menu_item_get_type ());
1687
1688 widget_class->draw = gtk_pixmap_menu_item_draw;
1689 widget_class->expose_event = gtk_pixmap_menu_item_expose;
1690 widget_class->map = gtk_pixmap_menu_item_map;
1691 widget_class->size_allocate = gtk_pixmap_menu_item_size_allocate;
1692 widget_class->size_request = gtk_pixmap_menu_item_size_request;
1693
1694 container_class->forall = gtk_pixmap_menu_item_forall;
1695 container_class->remove = gtk_pixmap_menu_item_remove;
1696
1697 klass->orig_toggle_size = menu_item_class->toggle_size;
1698 klass->have_pixmap_count = 0;
1699 }
1700
1701 static void
1702 gtk_pixmap_menu_item_init (GtkPixmapMenuItem *menu_item)
1703 {
1704 GtkMenuItem *mi;
1705
1706 mi = GTK_MENU_ITEM (menu_item);
1707
1708 menu_item->pixmap = NULL;
1709 }
1710
1711 static void
1712 gtk_pixmap_menu_item_draw (GtkWidget *widget,
1713 GdkRectangle *area)
1714 {
1715 g_return_if_fail (widget != NULL);
1716 g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (widget));
1717 g_return_if_fail (area != NULL);
1718
1719 if (GTK_WIDGET_CLASS (parent_class)->draw)
1720 (* GTK_WIDGET_CLASS (parent_class)->draw) (widget, area);
1721
1722 if (GTK_WIDGET_DRAWABLE (widget) &&
1723 GTK_PIXMAP_MENU_ITEM(widget)->pixmap) {
1724 gtk_widget_draw(GTK_WIDGET(GTK_PIXMAP_MENU_ITEM(widget)->pixmap),NULL);
1725 }
1726 }
1727
1728 static gint
1729 gtk_pixmap_menu_item_expose (GtkWidget *widget,
1730 GdkEventExpose *event)
1731 {
1732 g_return_val_if_fail (widget != NULL, FALSE);
1733 g_return_val_if_fail (GTK_IS_PIXMAP_MENU_ITEM (widget), FALSE);
1734 g_return_val_if_fail (event != NULL, FALSE);
1735
1736 if (GTK_WIDGET_CLASS (parent_class)->expose_event)
1737 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
1738
1739 if (GTK_WIDGET_DRAWABLE (widget) &&
1740 GTK_PIXMAP_MENU_ITEM(widget)->pixmap) {
1741 gtk_widget_draw(GTK_WIDGET(GTK_PIXMAP_MENU_ITEM(widget)->pixmap),NULL);
1742 }
1743
1744 return FALSE;
1745 }
1746
1747 /**
1748 * gtk_pixmap_menu_item_set_pixmap
1749 * @menu_item: Pointer to the pixmap menu item
1750 * @pixmap: Pointer to a pixmap widget
1751 *
1752 * Set the pixmap of the menu item.
1753 *
1754 **/
1755
1756 void
1757 gtk_pixmap_menu_item_set_pixmap (GtkPixmapMenuItem *menu_item,
1758 GtkWidget *pixmap)
1759 {
1760 g_return_if_fail (menu_item != NULL);
1761 g_return_if_fail (pixmap != NULL);
1762 g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (menu_item));
1763 g_return_if_fail (GTK_IS_WIDGET (pixmap));
1764 g_return_if_fail (menu_item->pixmap == NULL);
1765
1766 gtk_widget_set_parent (pixmap, GTK_WIDGET (menu_item));
1767 menu_item->pixmap = pixmap;
1768
1769 if (GTK_WIDGET_REALIZED (pixmap->parent) &&
1770 !GTK_WIDGET_REALIZED (pixmap))
1771 gtk_widget_realize (pixmap);
1772
1773 if (GTK_WIDGET_VISIBLE (pixmap->parent)) {
1774 if (GTK_WIDGET_MAPPED (pixmap->parent) &&
1775 GTK_WIDGET_VISIBLE(pixmap) &&
1776 !GTK_WIDGET_MAPPED (pixmap))
1777 gtk_widget_map (pixmap);
1778 }
1779
1780 changed_have_pixmap_status(menu_item);
1781
1782 if (GTK_WIDGET_VISIBLE (pixmap) && GTK_WIDGET_VISIBLE (menu_item))
1783 gtk_widget_queue_resize (pixmap);
1784 }
1785
1786 static void
1787 gtk_pixmap_menu_item_map (GtkWidget *widget)
1788 {
1789 GtkPixmapMenuItem *menu_item;
1790
1791 g_return_if_fail (widget != NULL);
1792 g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (widget));
1793
1794 menu_item = GTK_PIXMAP_MENU_ITEM(widget);
1795
1796 GTK_WIDGET_CLASS(parent_class)->map(widget);
1797
1798 if (menu_item->pixmap &&
1799 GTK_WIDGET_VISIBLE (menu_item->pixmap) &&
1800 !GTK_WIDGET_MAPPED (menu_item->pixmap))
1801 gtk_widget_map (menu_item->pixmap);
1802 }
1803
1804 static void
1805 gtk_pixmap_menu_item_size_allocate (GtkWidget *widget,
1806 GtkAllocation *allocation)
1807 {
1808 GtkPixmapMenuItem *pmenu_item;
1809
1810 pmenu_item = GTK_PIXMAP_MENU_ITEM(widget);
1811
1812 if (pmenu_item->pixmap && GTK_WIDGET_VISIBLE(pmenu_item))
1813 {
1814 GtkAllocation child_allocation;
1815 int border_width;
1816
1817 border_width = GTK_CONTAINER (widget)->border_width;
1818
1819 child_allocation.width = pmenu_item->pixmap->requisition.width;
1820 child_allocation.height = pmenu_item->pixmap->requisition.height;
1821 child_allocation.x = border_width + BORDER_SPACING;
1822 child_allocation.y = (border_width + BORDER_SPACING
1823 + (((allocation->height - child_allocation.height) - child_allocation.x)
1824 / 2)); /* center pixmaps vertically */
1825 gtk_widget_size_allocate (pmenu_item->pixmap, &child_allocation);
1826 }
1827
1828 if (GTK_WIDGET_CLASS (parent_class)->size_allocate)
1829 GTK_WIDGET_CLASS(parent_class)->size_allocate (widget, allocation);
1830 }
1831
1832 static void
1833 gtk_pixmap_menu_item_forall (GtkContainer *container,
1834 gboolean include_internals,
1835 GtkCallback callback,
1836 gpointer callback_data)
1837 {
1838 GtkPixmapMenuItem *menu_item;
1839
1840 g_return_if_fail (container != NULL);
1841 g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (container));
1842 g_return_if_fail (callback != NULL);
1843
1844 menu_item = GTK_PIXMAP_MENU_ITEM (container);
1845
1846 if (menu_item->pixmap)
1847 (* callback) (menu_item->pixmap, callback_data);
1848
1849 GTK_CONTAINER_CLASS(parent_class)->forall(container,include_internals,
1850 callback,callback_data);
1851 }
1852
1853 static void
1854 gtk_pixmap_menu_item_size_request (GtkWidget *widget,
1855 GtkRequisition *requisition)
1856 {
1857 GtkPixmapMenuItem *menu_item;
1858 GtkRequisition req = {0, 0};
1859
1860 g_return_if_fail (widget != NULL);
1861 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1862 g_return_if_fail (requisition != NULL);
1863
1864 GTK_WIDGET_CLASS(parent_class)->size_request(widget,requisition);
1865
1866 menu_item = GTK_PIXMAP_MENU_ITEM (widget);
1867
1868 if (menu_item->pixmap)
1869 gtk_widget_size_request(menu_item->pixmap, &req);
1870
1871 requisition->height = MAX(req.height + GTK_CONTAINER(widget)->border_width + BORDER_SPACING, (unsigned int) requisition->height);
1872 requisition->width += (req.width + GTK_CONTAINER(widget)->border_width + BORDER_SPACING);
1873 }
1874
1875 static void
1876 gtk_pixmap_menu_item_remove (GtkContainer *container,
1877 GtkWidget *child)
1878 {
1879 GtkBin *bin;
1880 gboolean widget_was_visible;
1881
1882 g_return_if_fail (container != NULL);
1883 g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (container));
1884 g_return_if_fail (child != NULL);
1885 g_return_if_fail (GTK_IS_WIDGET (child));
1886
1887 bin = GTK_BIN (container);
1888 g_return_if_fail ((bin->child == child ||
1889 (GTK_PIXMAP_MENU_ITEM(container)->pixmap == child)));
1890
1891 widget_was_visible = GTK_WIDGET_VISIBLE (child);
1892
1893 gtk_widget_unparent (child);
1894 if (bin->child == child)
1895 bin->child = NULL;
1896 else {
1897 GTK_PIXMAP_MENU_ITEM(container)->pixmap = NULL;
1898 changed_have_pixmap_status(GTK_PIXMAP_MENU_ITEM(container));
1899 }
1900
1901 if (widget_was_visible)
1902 gtk_widget_queue_resize (GTK_WIDGET (container));
1903 }
1904
1905
1906 /* important to only call this if there was actually a _change_ in pixmap == NULL */
1907 static void
1908 changed_have_pixmap_status (GtkPixmapMenuItem *menu_item)
1909 {
1910 if (menu_item->pixmap != NULL) {
1911 GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->have_pixmap_count += 1;
1912
1913 if (GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->have_pixmap_count == 1) {
1914 /* Install pixmap toggle size */
1915 GTK_MENU_ITEM_GET_CLASS(menu_item)->toggle_size = MAX(GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->orig_toggle_size, PMAP_WIDTH);
1916 }
1917 } else {
1918 GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->have_pixmap_count -= 1;
1919
1920 if (GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->have_pixmap_count == 0) {
1921 /* Install normal toggle size */
1922 GTK_MENU_ITEM_GET_CLASS(menu_item)->toggle_size = GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->orig_toggle_size;
1923 }
1924 }
1925
1926 /* Note that we actually need to do this for _all_ GtkPixmapMenuItem
1927 whenever the klass->toggle_size changes; but by doing it anytime
1928 this function is called, we get the same effect, just because of
1929 how the preferences option to show pixmaps works. Bogus, broken.
1930 */
1931 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(menu_item)))
1932 gtk_widget_queue_resize(GTK_WIDGET(menu_item));
1933 }
1934
1935 } // extern "C"
1936
1937 #endif // !__WXGTK20__
1938