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