1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "menu.h"
12 #pragma implementation "menuitem.h"
18 #include "wx/bitmap.h"
25 #include "wx/gtk/private.h"
27 #include <gdk/gdkkeysyms.h>
29 // FIXME: is this right? somehow I don't think so (VZ)
31 #include <glib-object.h>
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)
37 #define ACCEL_OBJECT GObject
38 #define ACCEL_OBJECTS(a) (a)->acceleratables
39 #define ACCEL_OBJ_CAST(obj) G_OBJECT(obj)
41 #define ACCEL_OBJECT GtkObject
42 #define ACCEL_OBJECTS(a) (a)->attach_objects
43 #define ACCEL_OBJ_CAST(obj) GTK_OBJECT(obj)
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
50 extern void wxapp_install_idle_handler();
53 #if GTK_CHECK_VERSION(1, 2, 0) && wxUSE_ACCEL
54 static wxString
GetHotKey( const wxMenuItem
& item
);
57 //-----------------------------------------------------------------------------
58 // substitute for missing GtkPixmapMenuItem
59 //-----------------------------------------------------------------------------
61 // FIXME: I can't make this compile with GTK+ 2.0, disabling for now (VZ)
63 #define USE_MENU_BITMAPS
66 #ifdef USE_MENU_BITMAPS
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)))
76 #ifndef GTK_MENU_ITEM_GET_CLASS
77 #define GTK_MENU_ITEM_GET_CLASS(obj) (GTK_MENU_ITEM_CLASS( GTK_OBJECT_GET_CLASS(obj)))
80 typedef struct _GtkPixmapMenuItem GtkPixmapMenuItem
;
81 typedef struct _GtkPixmapMenuItemClass GtkPixmapMenuItemClass
;
83 struct _GtkPixmapMenuItem
85 GtkMenuItem menu_item
;
90 struct _GtkPixmapMenuItemClass
92 GtkMenuItemClass parent_class
;
94 guint orig_toggle_size
;
95 guint have_pixmap_count
;
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
,
104 #endif // USE_MENU_BITMAPS
106 //-----------------------------------------------------------------------------
108 //-----------------------------------------------------------------------------
110 static wxString
wxReplaceUnderscore( const wxString
& title
)
114 /* GTK 1.2 wants to have "_" instead of "&" for accelerators */
116 for ( pc
= title
; *pc
!= wxT('\0'); pc
++ )
120 #if GTK_CHECK_VERSION(1, 2, 1)
123 else if (*pc
== wxT('/'))
131 if ( *pc
== wxT('_') )
133 // underscores must be doubled to prevent them from being
134 // interpreted as accelerator character prefix by GTK
145 //-----------------------------------------------------------------------------
147 //-----------------------------------------------------------------------------
149 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
,wxWindow
)
151 wxMenuBar::wxMenuBar( long style
)
153 /* the parent window is known after wxFrame::SetMenu() */
154 m_needParent
= FALSE
;
156 m_invokingWindow
= (wxWindow
*) NULL
;
158 if (!PreCreation( (wxWindow
*) NULL
, wxDefaultPosition
, wxDefaultSize
) ||
159 !CreateBase( (wxWindow
*) NULL
, -1, wxDefaultPosition
, wxDefaultSize
, style
, wxDefaultValidator
, wxT("menubar") ))
161 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
165 m_menus
.DeleteContents( TRUE
);
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>" );
173 m_menubar
= gtk_menu_bar_new();
176 if (style
& wxMB_DOCKABLE
)
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
) );
184 m_widget
= GTK_WIDGET(m_menubar
);
192 wxMenuBar::wxMenuBar()
194 /* the parent window is known after wxFrame::SetMenu() */
195 m_needParent
= FALSE
;
197 m_invokingWindow
= (wxWindow
*) NULL
;
199 if (!PreCreation( (wxWindow
*) NULL
, wxDefaultPosition
, wxDefaultSize
) ||
200 !CreateBase( (wxWindow
*) NULL
, -1, wxDefaultPosition
, wxDefaultSize
, 0, wxDefaultValidator
, wxT("menubar") ))
202 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
206 m_menus
.DeleteContents( TRUE
);
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>" );
214 m_menubar
= gtk_menu_bar_new();
217 m_widget
= GTK_WIDGET(m_menubar
);
224 wxMenuBar::~wxMenuBar()
226 // gtk_object_unref( GTK_OBJECT(m_factory) ); why not ?
229 static void wxMenubarUnsetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
231 menu
->SetInvokingWindow( (wxWindow
*) NULL
);
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();
238 /* support for native hot keys */
239 gtk_accel_group_detach( menu
->m_accel
, ACCEL_OBJ_CAST(top_frame
->m_widget
) );
242 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetFirst();
245 wxMenuItem
*menuitem
= node
->GetData();
246 if (menuitem
->IsSubMenu())
247 wxMenubarUnsetInvokingWindow( menuitem
->GetSubMenu(), win
);
248 node
= node
->GetNext();
252 static void wxMenubarSetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
254 menu
->SetInvokingWindow( win
);
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();
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+
267 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetFirst();
270 wxMenuItem
*menuitem
= node
->GetData();
271 if (menuitem
->IsSubMenu())
272 wxMenubarSetInvokingWindow( menuitem
->GetSubMenu(), win
);
273 node
= node
->GetNext();
277 void wxMenuBar::SetInvokingWindow( wxWindow
*win
)
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();
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+
291 wxMenuList::Node
*node
= m_menus
.GetFirst();
294 wxMenu
*menu
= node
->GetData();
295 wxMenubarSetInvokingWindow( menu
, win
);
296 node
= node
->GetNext();
300 void wxMenuBar::UnsetInvokingWindow( wxWindow
*win
)
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();
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+
312 wxMenuList::Node
*node
= m_menus
.GetFirst();
315 wxMenu
*menu
= node
->GetData();
316 wxMenubarUnsetInvokingWindow( menu
, win
);
317 node
= node
->GetNext();
321 bool wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
323 if ( !wxMenuBarBase::Append( menu
, title
) )
326 return GtkAppend(menu
, title
);
329 bool wxMenuBar::GtkAppend(wxMenu
*menu
, const wxString
& title
)
331 wxString
str( wxReplaceUnderscore( title
) );
333 /* this doesn't have much effect right now */
334 menu
->SetTitle( str
);
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)
339 /* local buffer in multibyte form */
341 buf
<< wxT('/') << str
.c_str();
343 char *cbuf
= new char[buf
.Length()+1];
344 strcpy(cbuf
, buf
.mbc_str());
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>";
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>/");
357 for ( pc
= str
; *pc
!= wxT('\0'); pc
++ )
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('_'))
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
);
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
);
375 gtk_menu_bar_append( GTK_MENU_BAR(m_menubar
), menu
->m_owner
);
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
);
387 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
389 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
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
396 if ( !GtkAppend(menu
, title
) )
399 if (pos
+1 >= m_menus
.GetCount())
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
);
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") );
415 #endif // GTK 1.2/1.0
418 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
420 // remove the old item and insert a new one
421 wxMenu
*menuOld
= Remove(pos
);
422 if ( menuOld
&& !Insert(pos
, menu
, title
) )
424 return (wxMenu
*) NULL
;
427 // either Insert() succeeded or Remove() failed and menuOld is NULL
431 wxMenu
*wxMenuBar::Remove(size_t pos
)
433 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
435 return (wxMenu
*) NULL
;
438 GtkMenuShell *menu_shell = GTK_MENU_SHELL(m_factory->widget);
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 ) );
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
);
449 gtk_widget_destroy( menu
->m_owner
);
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 ) );
459 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
461 if (wxMenuItem::GetLabelFromText(menu
->GetTitle()) == wxMenuItem::GetLabelFromText(menuString
))
463 int res
= menu
->FindItem( itemString
);
464 if (res
!= wxNOT_FOUND
)
468 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetFirst();
471 wxMenuItem
*item
= node
->GetData();
472 if (item
->IsSubMenu())
473 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
475 node
= node
->GetNext();
481 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
483 wxMenuList::Node
*node
= m_menus
.GetFirst();
486 wxMenu
*menu
= node
->GetData();
487 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
490 node
= node
->GetNext();
496 // Find a wxMenuItem using its id. Recurses down into sub-menus
497 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
499 wxMenuItem
* result
= menu
->FindChildItem(id
);
501 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetFirst();
502 while ( node
&& result
== NULL
)
504 wxMenuItem
*item
= node
->GetData();
505 if (item
->IsSubMenu())
507 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
509 node
= node
->GetNext();
515 wxMenuItem
* wxMenuBar::FindItem( int id
, wxMenu
**menuForItem
) const
517 wxMenuItem
* result
= 0;
518 wxMenuList::Node
*node
= m_menus
.GetFirst();
519 while (node
&& result
== 0)
521 wxMenu
*menu
= node
->GetData();
522 result
= FindMenuItemByIdRecursive( menu
, id
);
523 node
= node
->GetNext();
528 *menuForItem
= result
? result
->GetMenu() : (wxMenu
*)NULL
;
534 void wxMenuBar::EnableTop( size_t pos
, bool flag
)
536 wxMenuList::Node
*node
= m_menus
.Item( pos
);
538 wxCHECK_RET( node
, wxT("menu not found") );
540 wxMenu
* menu
= node
->GetData();
543 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
546 wxString
wxMenuBar::GetLabelTop( size_t pos
) const
548 wxMenuList::Node
*node
= m_menus
.Item( pos
);
550 wxCHECK_MSG( node
, wxT("invalid"), wxT("menu not found") );
552 wxMenu
* menu
= node
->GetData();
555 wxString
text( menu
->GetTitle() );
556 #if GTK_CHECK_VERSION(1, 2, 0)
557 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
559 if ( *pc
== wxT('_') || *pc
== wxT('&') )
561 // '_' is the escape character for GTK+ and '&' is the one for
562 // wxWindows - skip both of them
570 #endif // GTK+ 1.2/1.0
575 void wxMenuBar::SetLabelTop( size_t pos
, const wxString
& label
)
577 wxMenuList::Node
*node
= m_menus
.Item( pos
);
579 wxCHECK_RET( node
, wxT("menu not found") );
581 wxMenu
* menu
= node
->GetData();
583 wxString
str( wxReplaceUnderscore( label
) );
585 menu
->SetTitle( str
);
589 GtkLabel
*label
= GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
);
592 gtk_label_set( label
, str
.mb_str());
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
) );
601 //-----------------------------------------------------------------------------
603 //-----------------------------------------------------------------------------
605 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
608 wxapp_install_idle_handler();
610 int id
= menu
->FindMenuIdByMenuItem(widget
);
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") );
616 if (!menu
->IsEnabled(id
))
619 wxMenuItem
* item
= menu
->FindChildItem( id
);
620 wxCHECK_RET( item
, wxT("error in menu item callback") );
622 if (item
->IsCheckable())
624 bool isReallyChecked
= item
->IsChecked();
625 if ( item
->wxMenuItemBase::IsChecked() == isReallyChecked
)
627 /* the menu item has been checked by calling wxMenuItem->Check() */
632 /* the user pressed on the menu item -> report and make consistent
634 item
->wxMenuItemBase::Check(isReallyChecked
);
638 menu
->SendEvent(item
->GetId(), item
->IsCheckable() ? item
->IsChecked() : -1);
641 //-----------------------------------------------------------------------------
643 //-----------------------------------------------------------------------------
645 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
647 if (g_isIdle
) wxapp_install_idle_handler();
649 int id
= menu
->FindMenuIdByMenuItem(widget
);
651 wxASSERT( id
!= -1 ); // should find it!
653 if (!menu
->IsEnabled(id
))
656 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
657 event
.SetEventObject( menu
);
659 if (menu
->GetEventHandler()->ProcessEvent(event
))
662 wxWindow
*win
= menu
->GetInvokingWindow();
663 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
666 //-----------------------------------------------------------------------------
668 //-----------------------------------------------------------------------------
670 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
672 if (g_isIdle
) wxapp_install_idle_handler();
674 int id
= menu
->FindMenuIdByMenuItem(widget
);
676 wxASSERT( id
!= -1 ); // should find it!
678 if (!menu
->IsEnabled(id
))
681 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
682 event
.SetEventObject( menu
);
684 if (menu
->GetEventHandler()->ProcessEvent(event
))
687 wxWindow
*win
= menu
->GetInvokingWindow();
689 win
->GetEventHandler()->ProcessEvent( event
);
692 //-----------------------------------------------------------------------------
694 //-----------------------------------------------------------------------------
696 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
698 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
700 const wxString
& name
,
701 const wxString
& help
,
705 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
708 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
710 const wxString
& text
,
711 const wxString
& help
,
714 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
716 m_labelWidget
= (GtkWidget
*) NULL
;
717 m_menuItem
= (GtkWidget
*) NULL
;
722 wxMenuItem::~wxMenuItem()
724 // don't delete menu items, the menus take care of that
727 // return the menu item text without any menu accels
729 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
733 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
735 if ( *pc
== wxT('_') )
737 // wxGTK escapes "xxx_xxx" to "xxx__xxx"
743 if ( *pc
== wxT('&') )
755 void wxMenuItem::SetText( const wxString
& str
)
763 label
= (GtkLabel
*) m_labelWidget
;
765 label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
768 gtk_label_set( label
, m_text
.mb_str());
770 /* reparse key accel */
771 (void)gtk_label_parse_uline (GTK_LABEL(label
), m_text
.mb_str() );
772 gtk_accel_label_refetch( GTK_ACCEL_LABEL(label
) );
776 // it's valid for this function to be called even if m_menuItem == NULL
777 void wxMenuItem::DoSetText( const wxString
& str
)
779 /* '\t' is the deliminator indicating a hot key */
781 const wxChar
*pc
= str
;
782 for (; (*pc
!= wxT('\0')) && (*pc
!= wxT('\t')); pc
++ )
786 #if GTK_CHECK_VERSION(1, 2, 0)
789 else if ( *pc
== wxT('_') ) // escape underscores
793 else if (*pc
== wxT('/')) /* we have to filter out slashes ... */
795 m_text
<< wxT('\\'); /* ... and replace them with back slashes */
796 #endif // GTK+ 1.2.0+
802 /* only GTK 1.2 knows about hot keys */
805 #if GTK_CHECK_VERSION(1, 2, 0)
811 #endif // GTK+ 1.2.0+
816 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
821 return (wxAcceleratorEntry
*)NULL
;
824 // as wxGetAccelFromString() looks for TAB, insert a dummy one here
826 label
<< wxT('\t') << GetHotKey();
828 return wxGetAccelFromString(label
);
831 #endif // wxUSE_ACCEL
833 void wxMenuItem::Check( bool check
)
835 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
837 wxCHECK_RET( IsCheckable(), wxT("Can't check uncheckable item!") )
839 if (check
== m_isChecked
)
842 wxMenuItemBase::Check( check
);
843 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
846 void wxMenuItem::Enable( bool enable
)
848 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
850 gtk_widget_set_sensitive( m_menuItem
, enable
);
851 wxMenuItemBase::Enable( enable
);
854 bool wxMenuItem::IsChecked() const
856 wxCHECK_MSG( m_menuItem
, FALSE
, wxT("invalid menu item") );
858 wxCHECK_MSG( IsCheckable(), FALSE
,
859 wxT("can't get state of uncheckable item!") );
861 return ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
864 wxString
wxMenuItem::GetFactoryPath() const
866 /* in order to get the pointer to the item we need the item text
867 _without_ underscores */
868 wxString
path( wxT("<main>/") );
870 for ( const wxChar
*pc
= m_text
.c_str(); *pc
; pc
++ )
872 if ( *pc
== wxT('_') || *pc
== wxT('&') )
874 // remove '_' and '&' unconditionally
884 //-----------------------------------------------------------------------------
886 //-----------------------------------------------------------------------------
888 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
892 #if GTK_CHECK_VERSION(1, 2, 0)
893 m_accel
= gtk_accel_group_new();
894 m_factory
= gtk_item_factory_new( GTK_TYPE_MENU
, "<main>", m_accel
);
895 m_menu
= gtk_item_factory_get_widget( m_factory
, "<main>" );
897 m_menu
= gtk_menu_new(); // Do not show!
900 m_owner
= (GtkWidget
*) NULL
;
902 #if GTK_CHECK_VERSION(1, 2, 0)
903 /* Tearoffs are entries, just like separators. So if we want this
904 menu to be a tear-off one, we just append a tearoff entry
906 if(m_style
& wxMENU_TEAROFF
)
908 GtkItemFactoryEntry entry
;
909 entry
.path
= (char *)"/tearoff";
910 entry
.callback
= (GtkItemFactoryCallback
) NULL
;
911 entry
.callback_action
= 0;
912 entry
.item_type
= (char *)"<Tearoff>";
913 entry
.accelerator
= (gchar
*) NULL
;
914 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
915 //GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, "<main>/tearoff" );
917 #endif // GTK+ 1.2.0+
919 // append the title as the very first entry if we have it
931 gtk_widget_destroy( m_menu
);
933 gtk_object_unref( GTK_OBJECT(m_factory
) );
936 bool wxMenu::GtkAppend(wxMenuItem
*mitem
)
940 #if defined(USE_MENU_BITMAPS) || !GTK_CHECK_VERSION(1, 2, 0)
941 bool appended
= FALSE
;
944 #if GTK_CHECK_VERSION(1, 2, 0)
945 // is this a radio item?
946 bool isRadio
= FALSE
;
947 #endif // GTK+ >= 1.2
949 if ( mitem
->IsSeparator() )
951 #if GTK_CHECK_VERSION(1, 2, 0)
952 GtkItemFactoryEntry entry
;
953 entry
.path
= (char *)"/sep";
954 entry
.callback
= (GtkItemFactoryCallback
) NULL
;
955 entry
.callback_action
= 0;
956 entry
.item_type
= (char *)"<Separator>";
957 entry
.accelerator
= (gchar
*) NULL
;
959 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
961 /* this will be wrong for more than one separator. do we care? */
962 menuItem
= gtk_item_factory_get_widget( m_factory
, "<main>/sep" );
964 menuItem
= gtk_menu_item_new();
965 #endif // GTK 1.2/1.0
967 else if ( mitem
->IsSubMenu() )
969 #if GTK_CHECK_VERSION(1, 2, 0)
970 /* text has "_" instead of "&" after mitem->SetText() */
971 wxString
text( mitem
->GetText() );
973 /* local buffer in multibyte form */
976 strcat( buf
, text
.mb_str() );
978 GtkItemFactoryEntry entry
;
980 entry
.callback
= (GtkItemFactoryCallback
) 0;
981 entry
.callback_action
= 0;
982 entry
.item_type
= (char *)"<Branch>";
983 entry
.accelerator
= (gchar
*) NULL
;
985 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
987 wxString
path( mitem
->GetFactoryPath() );
988 menuItem
= gtk_item_factory_get_item( m_factory
, path
.mb_str() );
990 menuItem
= gtk_menu_item_new_with_label(mitem
->GetText().mbc_str());
991 #endif // GTK 1.2/1.0
993 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), mitem
->GetSubMenu()->m_menu
);
995 // if adding a submenu to a menu already existing in the menu bar, we
996 // must set invoking window to allow processing events from this
998 if ( m_invokingWindow
)
999 wxMenubarSetInvokingWindow(mitem
->GetSubMenu(), m_invokingWindow
);
1001 #ifdef USE_MENU_BITMAPS
1002 else if (mitem
->GetBitmap().Ok()) // An item with bitmap
1004 wxString
text( mitem
->GetText() );
1005 const wxBitmap
*bitmap
= &mitem
->GetBitmap();
1007 menuItem
= gtk_pixmap_menu_item_new ();
1008 GtkWidget
*label
= gtk_accel_label_new (text
.mb_str());
1009 gtk_misc_set_alignment (GTK_MISC (label
), 0.0, 0.5);
1010 gtk_container_add (GTK_CONTAINER (menuItem
), label
);
1011 guint accel_key
= gtk_label_parse_uline (GTK_LABEL(label
), text
.mb_str() );
1012 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label
), menuItem
);
1013 if (accel_key
!= GDK_VoidSymbol
)
1015 gtk_widget_add_accelerator (menuItem
,
1017 gtk_menu_ensure_uline_accel_group (GTK_MENU (m_menu
)),
1021 gtk_widget_show (label
);
1023 mitem
->SetLabelWidget(label
);
1025 GtkWidget
* pixmap
= gtk_pixmap_new( bitmap
->GetPixmap(), bitmap
->GetMask() ? bitmap
->GetMask()->GetBitmap() : (GdkBitmap
* )NULL
);
1026 gtk_widget_show(pixmap
);
1027 gtk_pixmap_menu_item_set_pixmap(GTK_PIXMAP_MENU_ITEM( menuItem
), pixmap
);
1029 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
1030 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
1032 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
1033 gtk_widget_show( menuItem
);
1035 appended
= TRUE
; // We've done this, don't do it again
1037 #endif // USE_MENU_BITMAPS
1038 else // a normal item
1040 #if GTK_CHECK_VERSION(1, 2, 0)
1041 /* text has "_" instead of "&" after mitem->SetText() */
1042 wxString
text( mitem
->GetText() );
1044 /* local buffer in multibyte form */
1047 strncat( buf
, text
.mb_str(), WXSIZEOF(buf
) - 2 );
1048 buf
[WXSIZEOF(buf
) - 1] = '\0';
1050 GtkItemFactoryEntry entry
;
1052 entry
.callback
= (GtkItemFactoryCallback
) gtk_menu_clicked_callback
;
1053 entry
.callback_action
= 0;
1056 const char *item_type
;
1057 switch ( mitem
->GetKind() )
1060 item_type
= "<CheckItem>";
1064 if ( m_pathLastRadio
.empty() )
1066 // start of a new radio group
1067 item_type
= "<RadioItem>";
1068 m_pathLastRadio
= buf
+ 1;
1070 else // continue the radio group
1072 pathRadio
= m_pathLastRadio
;
1073 pathRadio
.Replace("_", "");
1074 pathRadio
.Prepend("<main>/");
1075 item_type
= pathRadio
;
1078 // remember that this one was a radio item to avoid resetting
1079 // m_pathLastRadio below
1084 wxFAIL_MSG( _T("unexpected menu item kind") );
1088 item_type
= "<Item>";
1092 entry
.item_type
= (char *)item_type
; // cast needed for GTK+
1093 entry
.accelerator
= (gchar
*) NULL
;
1096 // due to an apparent bug in GTK+, we have to use a static buffer here -
1097 // otherwise GTK+ 1.2.2 manages to override the memory we pass to it
1099 static char s_accel
[50]; // must be big enougg
1100 wxString
tmp( GetHotKey(*mitem
) );
1101 strncpy(s_accel
, tmp
.mb_str(), WXSIZEOF(s_accel
));
1102 entry
.accelerator
= s_accel
;
1103 #else // !wxUSE_ACCEL
1104 entry
.accelerator
= (char*) NULL
;
1105 #endif // wxUSE_ACCEL/!wxUSE_ACCEL
1107 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
1109 wxString
path( mitem
->GetFactoryPath() );
1110 menuItem
= gtk_item_factory_get_widget( m_factory
, path
.mb_str() );
1112 menuItem
= checkable
? gtk_check_menu_item_new_with_label( mitem
->GetText().mb_str() )
1113 : gtk_menu_item_new_with_label( mitem
->GetText().mb_str() );
1115 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
1116 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
1118 #endif // GTK+ 1.2/1.0
1121 if ( !mitem
->IsSeparator() )
1123 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
1124 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
1127 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
1128 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
1132 #if !GTK_CHECK_VERSION(1, 2, 0)
1135 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
1136 gtk_widget_show( menuItem
);
1140 mitem
->SetMenuItem(menuItem
);
1142 #if GTK_CHECK_VERSION(1, 2, 0)
1145 m_pathLastRadio
.clear();
1147 #endif // GTK+ >= 1.2
1152 bool wxMenu::DoAppend(wxMenuItem
*mitem
)
1154 return GtkAppend(mitem
) && wxMenuBase::DoAppend(mitem
);
1157 bool wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1159 if ( !wxMenuBase::DoInsert(pos
, item
) )
1163 // GTK+ doesn't have a function to insert a menu using GtkItemFactory (as
1164 // of version 1.2.6), so we first append the item and then change its
1166 if ( !GtkAppend(item
) )
1169 if ( m_style
& wxMENU_TEAROFF
)
1171 // change the position as the first item is the tear-off marker
1175 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL(m_factory
->widget
);
1176 gpointer data
= g_list_last(menu_shell
->children
)->data
;
1177 menu_shell
->children
= g_list_remove(menu_shell
->children
, data
);
1178 menu_shell
->children
= g_list_insert(menu_shell
->children
, data
, pos
);
1182 // this should be easy to do...
1183 wxFAIL_MSG( wxT("not implemented") );
1186 #endif // GTK 1.2/1.0
1189 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1191 if ( !wxMenuBase::DoRemove(item
) )
1192 return (wxMenuItem
*)NULL
;
1194 // TODO: this code doesn't delete the item factory item and this seems
1195 // impossible as of GTK 1.2.6.
1196 gtk_widget_destroy( item
->GetMenuItem() );
1201 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
1203 wxNode
*node
= m_items
.First();
1206 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
1207 if (item
->GetMenuItem() == menuItem
)
1208 return item
->GetId();
1209 node
= node
->Next();
1215 // ----------------------------------------------------------------------------
1217 // ----------------------------------------------------------------------------
1219 #if GTK_CHECK_VERSION(1, 2, 0) && wxUSE_ACCEL
1221 static wxString
GetHotKey( const wxMenuItem
& item
)
1225 wxAcceleratorEntry
*accel
= item
.GetAccel();
1228 int flags
= accel
->GetFlags();
1229 if ( flags
& wxACCEL_ALT
)
1230 hotkey
+= wxT("<alt>");
1231 if ( flags
& wxACCEL_CTRL
)
1232 hotkey
+= wxT("<control>");
1233 if ( flags
& wxACCEL_SHIFT
)
1234 hotkey
+= wxT("<shift>");
1236 int code
= accel
->GetKeyCode();
1251 hotkey
<< wxT('F') << code
- WXK_F1
+ 1;
1254 // TODO: we should use gdk_keyval_name() (a.k.a.
1255 // XKeysymToString) here as well as hardcoding the keysym
1256 // names this might be not portable
1257 case WXK_NUMPAD_INSERT
:
1258 hotkey
<< wxT("KP_Insert" );
1260 case WXK_NUMPAD_DELETE
:
1261 hotkey
<< wxT("KP_Delete" );
1264 hotkey
<< wxT("Insert" );
1267 hotkey
<< wxT("Delete" );
1270 // if there are any other keys wxGetAccelFromString() may
1271 // return, we should process them here
1276 gchar
*name
= gdk_keyval_name((guint
)code
);
1284 wxFAIL_MSG( wxT("unknown keyboard accel") );
1293 #endif // wxUSE_ACCEL
1296 //-----------------------------------------------------------------------------
1297 // substitute for missing GtkPixmapMenuItem
1298 //-----------------------------------------------------------------------------
1300 #ifdef USE_MENU_BITMAPS
1303 * Copyright (C) 1998, 1999, 2000 Free Software Foundation
1304 * All rights reserved.
1306 * This file is part of the Gnome Library.
1308 * The Gnome Library is free software; you can redistribute it and/or
1309 * modify it under the terms of the GNU Library General Public License as
1310 * published by the Free Software Foundation; either version 2 of the
1311 * License, or (at your option) any later version.
1313 * The Gnome Library is distributed in the hope that it will be useful,
1314 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1315 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1316 * Library General Public License for more details.
1318 * You should have received a copy of the GNU Library General Public
1319 * License along with the Gnome Library; see the file COPYING.LIB. If not,
1320 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1321 * Boston, MA 02111-1307, USA.
1327 /* Author: Dietmar Maurer <dm@vlsivie.tuwien.ac.at> */
1329 #include <gtk/gtkaccellabel.h>
1330 #include <gtk/gtksignal.h>
1331 #include <gtk/gtkmenuitem.h>
1332 #include <gtk/gtkmenu.h>
1333 #include <gtk/gtkcontainer.h>
1338 static void gtk_pixmap_menu_item_class_init (GtkPixmapMenuItemClass
*klass
);
1339 static void gtk_pixmap_menu_item_init (GtkPixmapMenuItem
*menu_item
);
1340 static void gtk_pixmap_menu_item_draw (GtkWidget
*widget
,
1341 GdkRectangle
*area
);
1342 static gint
gtk_pixmap_menu_item_expose (GtkWidget
*widget
,
1343 GdkEventExpose
*event
);
1345 /* we must override the following functions */
1347 static void gtk_pixmap_menu_item_map (GtkWidget
*widget
);
1348 static void gtk_pixmap_menu_item_size_allocate (GtkWidget
*widget
,
1349 GtkAllocation
*allocation
);
1350 static void gtk_pixmap_menu_item_forall (GtkContainer
*container
,
1351 gboolean include_internals
,
1352 GtkCallback callback
,
1353 gpointer callback_data
);
1354 static void gtk_pixmap_menu_item_size_request (GtkWidget
*widget
,
1355 GtkRequisition
*requisition
);
1356 static void gtk_pixmap_menu_item_remove (GtkContainer
*container
,
1359 static void changed_have_pixmap_status (GtkPixmapMenuItem
*menu_item
);
1361 static GtkMenuItemClass
*parent_class
= NULL
;
1365 #define BORDER_SPACING 3
1366 #define PMAP_WIDTH 20
1369 gtk_pixmap_menu_item_get_type (void)
1371 static GtkType pixmap_menu_item_type
= 0;
1373 if (!pixmap_menu_item_type
)
1375 GtkTypeInfo pixmap_menu_item_info
=
1377 (char *)"GtkPixmapMenuItem",
1378 sizeof (GtkPixmapMenuItem
),
1379 sizeof (GtkPixmapMenuItemClass
),
1380 (GtkClassInitFunc
) gtk_pixmap_menu_item_class_init
,
1381 (GtkObjectInitFunc
) gtk_pixmap_menu_item_init
,
1382 /* reserved_1 */ NULL
,
1383 /* reserved_2 */ NULL
,
1384 (GtkClassInitFunc
) NULL
,
1387 pixmap_menu_item_type
= gtk_type_unique (gtk_menu_item_get_type (),
1388 &pixmap_menu_item_info
);
1391 return pixmap_menu_item_type
;
1395 * gtk_pixmap_menu_item_new
1397 * Creates a new pixmap menu item. Use gtk_pixmap_menu_item_set_pixmap()
1398 * to set the pixmap wich is displayed at the left side.
1401 * &GtkWidget pointer to new menu item
1405 gtk_pixmap_menu_item_new (void)
1407 return GTK_WIDGET (gtk_type_new (gtk_pixmap_menu_item_get_type ()));
1411 gtk_pixmap_menu_item_class_init (GtkPixmapMenuItemClass
*klass
)
1413 GtkObjectClass
*object_class
;
1414 GtkWidgetClass
*widget_class
;
1415 GtkMenuItemClass
*menu_item_class
;
1416 GtkContainerClass
*container_class
;
1418 object_class
= (GtkObjectClass
*) klass
;
1419 widget_class
= (GtkWidgetClass
*) klass
;
1420 menu_item_class
= (GtkMenuItemClass
*) klass
;
1421 container_class
= (GtkContainerClass
*) klass
;
1423 parent_class
= (GtkMenuItemClass
*) gtk_type_class (gtk_menu_item_get_type ());
1425 widget_class
->draw
= gtk_pixmap_menu_item_draw
;
1426 widget_class
->expose_event
= gtk_pixmap_menu_item_expose
;
1427 widget_class
->map
= gtk_pixmap_menu_item_map
;
1428 widget_class
->size_allocate
= gtk_pixmap_menu_item_size_allocate
;
1429 widget_class
->size_request
= gtk_pixmap_menu_item_size_request
;
1431 container_class
->forall
= gtk_pixmap_menu_item_forall
;
1432 container_class
->remove
= gtk_pixmap_menu_item_remove
;
1434 klass
->orig_toggle_size
= menu_item_class
->toggle_size
;
1435 klass
->have_pixmap_count
= 0;
1439 gtk_pixmap_menu_item_init (GtkPixmapMenuItem
*menu_item
)
1443 mi
= GTK_MENU_ITEM (menu_item
);
1445 menu_item
->pixmap
= NULL
;
1449 gtk_pixmap_menu_item_draw (GtkWidget
*widget
,
1452 g_return_if_fail (widget
!= NULL
);
1453 g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (widget
));
1454 g_return_if_fail (area
!= NULL
);
1456 if (GTK_WIDGET_CLASS (parent_class
)->draw
)
1457 (* GTK_WIDGET_CLASS (parent_class
)->draw
) (widget
, area
);
1459 if (GTK_WIDGET_DRAWABLE (widget
) &&
1460 GTK_PIXMAP_MENU_ITEM(widget
)->pixmap
) {
1461 gtk_widget_draw(GTK_WIDGET(GTK_PIXMAP_MENU_ITEM(widget
)->pixmap
),NULL
);
1466 gtk_pixmap_menu_item_expose (GtkWidget
*widget
,
1467 GdkEventExpose
*event
)
1469 g_return_val_if_fail (widget
!= NULL
, FALSE
);
1470 g_return_val_if_fail (GTK_IS_PIXMAP_MENU_ITEM (widget
), FALSE
);
1471 g_return_val_if_fail (event
!= NULL
, FALSE
);
1473 if (GTK_WIDGET_CLASS (parent_class
)->expose_event
)
1474 (* GTK_WIDGET_CLASS (parent_class
)->expose_event
) (widget
, event
);
1476 if (GTK_WIDGET_DRAWABLE (widget
) &&
1477 GTK_PIXMAP_MENU_ITEM(widget
)->pixmap
) {
1478 gtk_widget_draw(GTK_WIDGET(GTK_PIXMAP_MENU_ITEM(widget
)->pixmap
),NULL
);
1485 * gtk_pixmap_menu_item_set_pixmap
1486 * @menu_item: Pointer to the pixmap menu item
1487 * @pixmap: Pointer to a pixmap widget
1489 * Set the pixmap of the menu item.
1494 gtk_pixmap_menu_item_set_pixmap (GtkPixmapMenuItem
*menu_item
,
1497 g_return_if_fail (menu_item
!= NULL
);
1498 g_return_if_fail (pixmap
!= NULL
);
1499 g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (menu_item
));
1500 g_return_if_fail (GTK_IS_WIDGET (pixmap
));
1501 g_return_if_fail (menu_item
->pixmap
== NULL
);
1503 gtk_widget_set_parent (pixmap
, GTK_WIDGET (menu_item
));
1504 menu_item
->pixmap
= pixmap
;
1506 if (GTK_WIDGET_REALIZED (pixmap
->parent
) &&
1507 !GTK_WIDGET_REALIZED (pixmap
))
1508 gtk_widget_realize (pixmap
);
1510 if (GTK_WIDGET_VISIBLE (pixmap
->parent
)) {
1511 if (GTK_WIDGET_MAPPED (pixmap
->parent
) &&
1512 GTK_WIDGET_VISIBLE(pixmap
) &&
1513 !GTK_WIDGET_MAPPED (pixmap
))
1514 gtk_widget_map (pixmap
);
1517 changed_have_pixmap_status(menu_item
);
1519 if (GTK_WIDGET_VISIBLE (pixmap
) && GTK_WIDGET_VISIBLE (menu_item
))
1520 gtk_widget_queue_resize (pixmap
);
1524 gtk_pixmap_menu_item_map (GtkWidget
*widget
)
1526 GtkPixmapMenuItem
*menu_item
;
1528 g_return_if_fail (widget
!= NULL
);
1529 g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (widget
));
1531 menu_item
= GTK_PIXMAP_MENU_ITEM(widget
);
1533 GTK_WIDGET_CLASS(parent_class
)->map(widget
);
1535 if (menu_item
->pixmap
&&
1536 GTK_WIDGET_VISIBLE (menu_item
->pixmap
) &&
1537 !GTK_WIDGET_MAPPED (menu_item
->pixmap
))
1538 gtk_widget_map (menu_item
->pixmap
);
1542 gtk_pixmap_menu_item_size_allocate (GtkWidget
*widget
,
1543 GtkAllocation
*allocation
)
1545 GtkPixmapMenuItem
*pmenu_item
;
1547 pmenu_item
= GTK_PIXMAP_MENU_ITEM(widget
);
1549 if (pmenu_item
->pixmap
&& GTK_WIDGET_VISIBLE(pmenu_item
))
1551 GtkAllocation child_allocation
;
1554 border_width
= GTK_CONTAINER (widget
)->border_width
;
1556 child_allocation
.width
= pmenu_item
->pixmap
->requisition
.width
;
1557 child_allocation
.height
= pmenu_item
->pixmap
->requisition
.height
;
1558 child_allocation
.x
= border_width
+ BORDER_SPACING
;
1559 child_allocation
.y
= (border_width
+ BORDER_SPACING
1560 + (((allocation
->height
- child_allocation
.height
) - child_allocation
.x
)
1561 / 2)); /* center pixmaps vertically */
1562 gtk_widget_size_allocate (pmenu_item
->pixmap
, &child_allocation
);
1565 if (GTK_WIDGET_CLASS (parent_class
)->size_allocate
)
1566 GTK_WIDGET_CLASS(parent_class
)->size_allocate (widget
, allocation
);
1570 gtk_pixmap_menu_item_forall (GtkContainer
*container
,
1571 gboolean include_internals
,
1572 GtkCallback callback
,
1573 gpointer callback_data
)
1575 GtkPixmapMenuItem
*menu_item
;
1577 g_return_if_fail (container
!= NULL
);
1578 g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (container
));
1579 g_return_if_fail (callback
!= NULL
);
1581 menu_item
= GTK_PIXMAP_MENU_ITEM (container
);
1583 if (menu_item
->pixmap
)
1584 (* callback
) (menu_item
->pixmap
, callback_data
);
1586 GTK_CONTAINER_CLASS(parent_class
)->forall(container
,include_internals
,
1587 callback
,callback_data
);
1591 gtk_pixmap_menu_item_size_request (GtkWidget
*widget
,
1592 GtkRequisition
*requisition
)
1594 GtkPixmapMenuItem
*menu_item
;
1595 GtkRequisition req
= {0, 0};
1597 g_return_if_fail (widget
!= NULL
);
1598 g_return_if_fail (GTK_IS_MENU_ITEM (widget
));
1599 g_return_if_fail (requisition
!= NULL
);
1601 GTK_WIDGET_CLASS(parent_class
)->size_request(widget
,requisition
);
1603 menu_item
= GTK_PIXMAP_MENU_ITEM (widget
);
1605 if (menu_item
->pixmap
)
1606 gtk_widget_size_request(menu_item
->pixmap
, &req
);
1608 requisition
->height
= MAX(req
.height
+ GTK_CONTAINER(widget
)->border_width
+ BORDER_SPACING
, (unsigned int) requisition
->height
);
1609 requisition
->width
+= (req
.width
+ GTK_CONTAINER(widget
)->border_width
+ BORDER_SPACING
);
1613 gtk_pixmap_menu_item_remove (GtkContainer
*container
,
1617 gboolean widget_was_visible
;
1619 g_return_if_fail (container
!= NULL
);
1620 g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (container
));
1621 g_return_if_fail (child
!= NULL
);
1622 g_return_if_fail (GTK_IS_WIDGET (child
));
1624 bin
= GTK_BIN (container
);
1625 g_return_if_fail ((bin
->child
== child
||
1626 (GTK_PIXMAP_MENU_ITEM(container
)->pixmap
== child
)));
1628 widget_was_visible
= GTK_WIDGET_VISIBLE (child
);
1630 gtk_widget_unparent (child
);
1631 if (bin
->child
== child
)
1634 GTK_PIXMAP_MENU_ITEM(container
)->pixmap
= NULL
;
1635 changed_have_pixmap_status(GTK_PIXMAP_MENU_ITEM(container
));
1638 if (widget_was_visible
)
1639 gtk_widget_queue_resize (GTK_WIDGET (container
));
1643 /* important to only call this if there was actually a _change_ in pixmap == NULL */
1645 changed_have_pixmap_status (GtkPixmapMenuItem
*menu_item
)
1647 if (menu_item
->pixmap
!= NULL
) {
1648 GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item
)->have_pixmap_count
+= 1;
1650 if (GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item
)->have_pixmap_count
== 1) {
1651 /* Install pixmap toggle size */
1652 GTK_MENU_ITEM_GET_CLASS(menu_item
)->toggle_size
= MAX(GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item
)->orig_toggle_size
, PMAP_WIDTH
);
1655 GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item
)->have_pixmap_count
-= 1;
1657 if (GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item
)->have_pixmap_count
== 0) {
1658 /* Install normal toggle size */
1659 GTK_MENU_ITEM_GET_CLASS(menu_item
)->toggle_size
= GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item
)->orig_toggle_size
;
1663 /* Note that we actually need to do this for _all_ GtkPixmapMenuItem
1664 whenever the klass->toggle_size changes; but by doing it anytime
1665 this function is called, we get the same effect, just because of
1666 how the preferences option to show pixmaps works. Bogus, broken.
1668 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(menu_item
)))
1669 gtk_widget_queue_resize(GTK_WIDGET(menu_item
));
1672 #endif // USE_MENU_BITMAPS