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