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