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"
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 extern void wxapp_install_idle_handler();
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
,wxWindow
)
40 wxMenuBar::wxMenuBar( long style
)
42 /* the parent window is known after wxFrame::SetMenu() */
45 m_invokingWindow
= (wxWindow
*) NULL
;
47 if (!PreCreation( (wxWindow
*) NULL
, wxDefaultPosition
, wxDefaultSize
) ||
48 !CreateBase( (wxWindow
*) NULL
, -1, wxDefaultPosition
, wxDefaultSize
, style
, wxDefaultValidator
, wxT("menubar") ))
50 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
54 m_menus
.DeleteContents( TRUE
);
56 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
57 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
58 m_accel
= gtk_accel_group_new();
59 m_factory
= gtk_item_factory_new( GTK_TYPE_MENU_BAR
, "<main>", m_accel
);
60 m_menubar
= gtk_item_factory_get_widget( m_factory
, "<main>" );
62 m_menubar
= gtk_menu_bar_new();
65 if (style
& wxMB_DOCKABLE
)
67 m_widget
= gtk_handle_box_new();
68 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_menubar
) );
69 gtk_widget_show( GTK_WIDGET(m_menubar
) );
73 m_widget
= GTK_WIDGET(m_menubar
);
79 wxMenuBar::wxMenuBar()
81 /* the parent window is known after wxFrame::SetMenu() */
84 m_invokingWindow
= (wxWindow
*) NULL
;
86 if (!PreCreation( (wxWindow
*) NULL
, wxDefaultPosition
, wxDefaultSize
) ||
87 !CreateBase( (wxWindow
*) NULL
, -1, wxDefaultPosition
, wxDefaultSize
, 0, wxDefaultValidator
, wxT("menubar") ))
89 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
93 m_menus
.DeleteContents( TRUE
);
95 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
96 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
97 m_accel
= gtk_accel_group_new();
98 m_factory
= gtk_item_factory_new( GTK_TYPE_MENU_BAR
, "<main>", m_accel
);
99 m_menubar
= gtk_item_factory_get_widget( m_factory
, "<main>" );
101 m_menubar
= gtk_menu_bar_new();
104 m_widget
= GTK_WIDGET(m_menubar
);
109 wxMenuBar::~wxMenuBar()
111 // gtk_object_unref( GTK_OBJECT(m_factory) ); why not ?
114 static void wxMenubarUnsetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
116 menu
->SetInvokingWindow( (wxWindow
*) NULL
);
118 #if (GTK_MINOR_VERSION > 0)
119 wxWindow
*top_frame
= win
;
120 while (top_frame
->GetParent() && !(top_frame
->GetParent()->m_isFrame
))
121 top_frame
= top_frame
->GetParent();
123 /* support for native hot keys */
124 gtk_accel_group_detach( menu
->m_accel
, GTK_OBJECT(top_frame
->m_widget
) );
127 wxNode
*node
= menu
->GetItems().First();
130 wxMenuItem
*menuitem
= (wxMenuItem
*)node
->Data();
131 if (menuitem
->IsSubMenu())
132 wxMenubarUnsetInvokingWindow( menuitem
->GetSubMenu(), win
);
137 static void wxMenubarSetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
139 menu
->SetInvokingWindow( win
);
141 #if (GTK_MINOR_VERSION > 0)
142 wxWindow
*top_frame
= win
;
143 while (top_frame
->GetParent() && !(top_frame
->GetParent()->m_isFrame
))
144 top_frame
= top_frame
->GetParent();
146 /* support for native hot keys */
147 gtk_accel_group_attach( menu
->m_accel
, GTK_OBJECT(top_frame
->m_widget
) );
150 wxNode
*node
= menu
->GetItems().First();
153 wxMenuItem
*menuitem
= (wxMenuItem
*)node
->Data();
154 if (menuitem
->IsSubMenu())
155 wxMenubarSetInvokingWindow( menuitem
->GetSubMenu(), win
);
160 void wxMenuBar::SetInvokingWindow( wxWindow
*win
)
162 m_invokingWindow
= win
;
163 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
164 wxWindow
*top_frame
= win
;
165 while (top_frame
->GetParent() && !(top_frame
->GetParent()->m_isFrame
))
166 top_frame
= top_frame
->GetParent();
168 /* support for native key accelerators indicated by underscroes */
169 gtk_accel_group_attach( m_accel
, GTK_OBJECT(top_frame
->m_widget
) );
172 wxNode
*node
= m_menus
.First();
175 wxMenu
*menu
= (wxMenu
*)node
->Data();
176 wxMenubarSetInvokingWindow( menu
, win
);
181 void wxMenuBar::UnsetInvokingWindow( wxWindow
*win
)
183 m_invokingWindow
= (wxWindow
*) NULL
;
184 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
185 wxWindow
*top_frame
= win
;
186 while (top_frame
->GetParent() && !(top_frame
->GetParent()->m_isFrame
))
187 top_frame
= top_frame
->GetParent();
189 /* support for native key accelerators indicated by underscroes */
190 gtk_accel_group_detach( m_accel
, GTK_OBJECT(top_frame
->m_widget
) );
193 wxNode
*node
= m_menus
.First();
196 wxMenu
*menu
= (wxMenu
*)node
->Data();
197 wxMenubarUnsetInvokingWindow( menu
, win
);
202 bool wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
204 m_menus
.Append( menu
);
208 /* GTK 1.2 wants to have "_" instead of "&" for accelerators */
210 for ( pc
= title
; *pc
!= wxT('\0'); pc
++ )
214 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
217 else if (*pc
== wxT('/'))
225 if ( *pc
== wxT('_') )
227 // underscores must be doubled to prevent them from being
228 // interpreted as accelerator character prefix by GTK
237 /* this doesn't have much effect right now */
238 menu
->SetTitle( str
);
240 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
241 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
243 /* local buffer in multibyte form */
245 buf
<< wxT('/') << str
.c_str();
247 char *cbuf
= new char[buf
.Length()+1];
248 strcpy(cbuf
, buf
.mbc_str());
250 GtkItemFactoryEntry entry
;
251 entry
.path
= (gchar
*)cbuf
; // const_cast
252 entry
.accelerator
= (gchar
*) NULL
;
253 entry
.callback
= (GtkItemFactoryCallback
) NULL
;
254 entry
.callback_action
= 0;
255 entry
.item_type
= "<Branch>";
257 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
258 /* in order to get the pointer to the item we need the item text _without_ underscores */
259 wxString tmp
= wxT("<main>/");
260 for ( pc
= str
; *pc
!= wxT('\0'); pc
++ )
262 // contrary to the common sense, we must throw out _all_ underscores,
263 // (i.e. "Hello__World" => "HelloWorld" and not "Hello_World" as we
264 // might naively think). IMHO it's a bug in GTK+ (VZ)
265 while (*pc
== wxT('_'))
269 menu
->m_owner
= gtk_item_factory_get_item( m_factory
, tmp
.mb_str() );
270 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
274 menu
->m_owner
= gtk_menu_item_new_with_label( str
.mb_str() );
275 gtk_widget_show( menu
->m_owner
);
276 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
278 gtk_menu_bar_append( GTK_MENU_BAR(m_menubar
), menu
->m_owner
);
282 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
283 // adding menu later on.
284 if (m_invokingWindow
)
285 wxMenubarSetInvokingWindow( menu
, m_invokingWindow
);
290 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
292 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
295 wxFAIL_MSG(wxT("TODO"));
300 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
302 if ( !wxMenuBarBase::Replace(pos
, menu
, title
) )
305 wxFAIL_MSG(wxT("TODO"));
310 wxMenu
*wxMenuBar::Remove(size_t pos
)
312 if ( !wxMenuBarBase::Remove(pos
) )
315 wxFAIL_MSG(wxT("TODO"));
320 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
322 if (menu
->GetTitle() == menuString
)
324 int res
= menu
->FindItem( itemString
);
325 if (res
!= wxNOT_FOUND
)
329 wxNode
*node
= ((wxMenu
*)menu
)->GetItems().First(); // const_cast
332 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
333 if (item
->IsSubMenu())
334 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
342 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
344 wxNode
*node
= m_menus
.First();
347 wxMenu
*menu
= (wxMenu
*)node
->Data();
348 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
349 if (res
!= -1) return res
;
355 // Find a wxMenuItem using its id. Recurses down into sub-menus
356 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
358 wxMenuItem
* result
= menu
->FindItem(id
);
360 wxNode
*node
= ((wxMenu
*)menu
)->GetItems().First(); // const_cast
361 while ( node
&& result
== NULL
)
363 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
364 if (item
->IsSubMenu())
366 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
374 wxMenuItem
* wxMenuBar::FindItem( int id
, wxMenu
**menuForItem
) const
376 wxMenuItem
* result
= 0;
377 wxNode
*node
= m_menus
.First();
378 while (node
&& result
== 0)
380 wxMenu
*menu
= (wxMenu
*)node
->Data();
381 result
= FindMenuItemByIdRecursive( menu
, id
);
387 *menuForItem
= result
? result
->GetMenu() : (wxMenu
*)NULL
;
393 void wxMenuBar::EnableTop( size_t pos
, bool flag
)
395 wxMenuList::Node
*node
= m_menus
.Item( pos
);
397 wxCHECK_RET( node
, wxT("menu not found") );
399 wxMenu
* menu
= node
->GetData();
402 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
405 wxString
wxMenuBar::GetLabelTop( size_t pos
) const
407 wxMenuList::Node
*node
= m_menus
.Item( pos
);
409 wxCHECK_MSG( node
, wxT("invalid"), wxT("menu not found") );
411 wxMenu
* menu
= node
->GetData();
413 return menu
->GetTitle();
416 void wxMenuBar::SetLabelTop( size_t pos
, const wxString
& label
)
418 wxMenuList::Node
*node
= m_menus
.Item( pos
);
420 wxCHECK_RET( node
, wxT("menu not found") );
422 wxMenu
* menu
= node
->GetData();
424 menu
->SetTitle( label
);
427 //-----------------------------------------------------------------------------
429 //-----------------------------------------------------------------------------
431 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
433 if (g_isIdle
) wxapp_install_idle_handler();
435 int id
= menu
->FindMenuIdByMenuItem(widget
);
437 /* should find it for normal (not popup) menu */
438 wxASSERT( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
) );
440 if (!menu
->IsEnabled(id
))
443 wxMenuItem
* item
= menu
->FindItem( id
);
444 wxCHECK_RET( item
, wxT("error in menu item callback") );
446 if (item
->IsCheckable())
448 bool isReallyChecked
= item
->IsChecked();
449 if ( item
->wxMenuItemBase::IsChecked() == isReallyChecked
)
451 /* the menu item has been checked by calling wxMenuItem->Check() */
456 /* the user pressed on the menu item -> report and make consistent
458 item
->wxMenuItemBase::Check(isReallyChecked
);
462 wxCommandEvent
event( wxEVT_COMMAND_MENU_SELECTED
, id
);
463 event
.SetEventObject( menu
);
466 if (menu
->GetCallback())
468 (void) (*(menu
->GetCallback())) (*menu
, event
);
472 if (menu
->GetEventHandler()->ProcessEvent(event
))
475 wxWindow
*win
= menu
->GetInvokingWindow();
477 win
->GetEventHandler()->ProcessEvent( event
);
480 //-----------------------------------------------------------------------------
482 //-----------------------------------------------------------------------------
484 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
486 if (g_isIdle
) wxapp_install_idle_handler();
488 int id
= menu
->FindMenuIdByMenuItem(widget
);
490 wxASSERT( id
!= -1 ); // should find it!
492 if (!menu
->IsEnabled(id
))
495 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
496 event
.SetEventObject( menu
);
498 if (menu
->GetEventHandler()->ProcessEvent(event
))
501 wxWindow
*win
= menu
->GetInvokingWindow();
502 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
505 //-----------------------------------------------------------------------------
507 //-----------------------------------------------------------------------------
509 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
511 if (g_isIdle
) wxapp_install_idle_handler();
513 int id
= menu
->FindMenuIdByMenuItem(widget
);
515 wxASSERT( id
!= -1 ); // should find it!
517 if (!menu
->IsEnabled(id
))
520 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
521 event
.SetEventObject( menu
);
523 if (menu
->GetEventHandler()->ProcessEvent(event
))
526 wxWindow
*win
= menu
->GetInvokingWindow();
528 win
->GetEventHandler()->ProcessEvent( event
);
531 //-----------------------------------------------------------------------------
533 //-----------------------------------------------------------------------------
535 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxMenuItemBase
)
537 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
539 const wxString
& name
,
540 const wxString
& help
,
544 return new wxMenuItem(parentMenu
, id
, name
, help
, isCheckable
, subMenu
);
547 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
549 const wxString
& text
,
550 const wxString
& help
,
555 m_isCheckable
= isCheckable
;
559 m_parentMenu
= parentMenu
;
562 m_menuItem
= (GtkWidget
*) NULL
;
564 // call it after initializing m_menuItem to NULL
568 wxMenuItem::~wxMenuItem()
570 // don't delete menu items, the menus take care of that
573 // it's valid for this function to be called even if m_menuItem == NULL
574 void wxMenuItem::DoSetText( const wxString
& str
)
576 /* '\t' is the deliminator indicating a hot key */
578 const wxChar
*pc
= str
;
579 for (; (*pc
!= wxT('\0')) && (*pc
!= wxT('\t')); pc
++ )
583 #if (GTK_MINOR_VERSION > 0)
586 else if ( *pc
== wxT('_') ) // escape underscores
590 else if (*pc
== wxT('/')) /* we have to filter out slashes ... */
592 m_text
<< wxT('\\'); /* ... and replace them with back slashes */
599 /* only GTK 1.2 knows about hot keys */
601 #if (GTK_MINOR_VERSION > 0)
611 GtkLabel
*label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
612 gtk_label_set( label
, m_text
.mb_str());
616 void wxMenuItem::Check( bool check
)
618 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
620 wxCHECK_RET( IsCheckable(), wxT("Can't check uncheckable item!") )
622 if (check
== m_isChecked
)
625 wxMenuItemBase::Check( check
);
626 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
629 void wxMenuItem::Enable( bool enable
)
631 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
633 gtk_widget_set_sensitive( m_menuItem
, enable
);
634 wxMenuItemBase::Enable( enable
);
637 bool wxMenuItem::IsChecked() const
639 wxCHECK_MSG( m_menuItem
, FALSE
, wxT("invalid menu item") );
641 wxCHECK_MSG( IsCheckable(), FALSE
,
642 wxT("can't get state of uncheckable item!") );
644 return ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
647 //-----------------------------------------------------------------------------
649 //-----------------------------------------------------------------------------
651 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
654 wxMenu::Init( const wxString
& title
,
656 const wxFunction func
660 m_items
.DeleteContents( TRUE
);
661 m_invokingWindow
= (wxWindow
*) NULL
;
664 #if (GTK_MINOR_VERSION > 0)
665 m_accel
= gtk_accel_group_new();
666 m_factory
= gtk_item_factory_new( GTK_TYPE_MENU
, "<main>", m_accel
);
667 m_menu
= gtk_item_factory_get_widget( m_factory
, "<main>" );
669 m_menu
= gtk_menu_new(); // Do not show!
674 m_eventHandler
= this;
675 m_clientData
= (void*) NULL
;
677 if (m_title
.IsNull()) m_title
= wxT("");
678 if (m_title
!= wxT(""))
684 m_owner
= (GtkWidget
*) NULL
;
686 #if (GTK_MINOR_VERSION > 0)
687 /* Tearoffs are entries, just like separators. So if we want this
688 menu to be a tear-off one, we just append a tearoff entry
690 if(m_style
& wxMENU_TEAROFF
)
692 GtkItemFactoryEntry entry
;
693 entry
.path
= "/tearoff";
694 entry
.callback
= (GtkItemFactoryCallback
) NULL
;
695 entry
.callback_action
= 0;
696 entry
.item_type
= "<Tearoff>";
697 entry
.accelerator
= (gchar
*) NULL
;
698 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
699 //GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, "<main>/tearoff" );
706 wxNode
*node
= m_items
.First();
709 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
710 wxMenu
*submenu
= item
->GetSubMenu();
716 gtk_widget_destroy( m_menu
);
718 gtk_object_unref( GTK_OBJECT(m_factory
) );
721 void wxMenu::SetTitle( const wxString
& title
)
723 // TODO Waiting for something better
727 const wxString
wxMenu::GetTitle() const
732 void wxMenu::AppendSeparator()
734 wxMenuItem
*mitem
= new wxMenuItem(this, wxID_SEPARATOR
);
736 #if (GTK_MINOR_VERSION > 0)
737 GtkItemFactoryEntry entry
;
739 entry
.callback
= (GtkItemFactoryCallback
) NULL
;
740 entry
.callback_action
= 0;
741 entry
.item_type
= "<Separator>";
742 entry
.accelerator
= (gchar
*) NULL
;
744 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
746 /* this will be wrong for more than one separator. do we care? */
747 GtkWidget
*menuItem
= gtk_item_factory_get_widget( m_factory
, "<main>/sep" );
749 GtkWidget
*menuItem
= gtk_menu_item_new();
750 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
751 gtk_widget_show( menuItem
);
754 mitem
->SetMenuItem(menuItem
);
755 m_items
.Append( mitem
);
758 #if (GTK_MINOR_VERSION > 0) && wxUSE_ACCEL
759 static wxString
GetHotKey( const wxMenuItem
& item
)
763 // as wxGetAccelFromString() looks for TAB, insert a dummy one here
765 label
<< wxT('\t') << item
.GetHotKey();
767 // but if the hotkey is empty don't do anything
768 if ( label
.length() > 1 )
770 wxAcceleratorEntry
*accel
= wxGetAccelFromString(label
);
773 int flags
= accel
->GetFlags();
774 if ( flags
& wxACCEL_ALT
)
775 hotkey
+= wxT("<alt>");
776 if ( flags
& wxACCEL_CTRL
)
777 hotkey
+= wxT("<control>");
778 if ( flags
& wxACCEL_SHIFT
)
779 hotkey
+= wxT("<shift>");
781 int code
= accel
->GetKeyCode();
796 hotkey
<< wxT('F') << code
- WXK_F1
+ 1;
799 // if there are any other keys wxGetAccelFromString() may return,
800 // we should process them here
803 if ( wxIsalnum(code
) )
805 hotkey
<< (wxChar
)code
;
810 wxFAIL_MSG( wxT("unknown keyboard accel") );
819 #endif // wxUSE_ACCEL
821 void wxMenu::Append( int id
, const wxString
&item
, const wxString
&helpStr
, bool checkable
)
823 wxMenuItem
*mitem
= new wxMenuItem(this, id
, item
, helpStr
, checkable
);
825 #if (GTK_MINOR_VERSION > 0)
826 /* text has "_" instead of "&" after mitem->SetText() */
827 wxString
text( mitem
->GetText() );
829 /* local buffer in multibyte form */
832 strcat( buf
, text
.mb_str() );
834 GtkItemFactoryEntry entry
;
836 entry
.callback
= (GtkItemFactoryCallback
) gtk_menu_clicked_callback
;
837 entry
.callback_action
= 0;
839 entry
.item_type
= "<CheckItem>";
841 entry
.item_type
= "<Item>";
844 // due to an apparent bug in GTK+, we have to use a static buffer here -
845 // otherwise GTK+ 1.2.2 manages to override the memory we pass to it
847 static char s_accel
[32]; // must be big enough for <control><alt><shift>F12
848 strncpy(s_accel
, GetHotKey(*mitem
).mb_str(), WXSIZEOF(s_accel
));
849 entry
.accelerator
= s_accel
;
851 entry
.accelerator
= NULL
;
854 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
856 /* in order to get the pointer to the item we need the item text _without_ underscores */
857 wxString s
= wxT("<main>/");
858 for ( const wxChar
*pc
= text
; *pc
!= wxT('\0'); pc
++ )
860 while (*pc
== wxT('_')) pc
++; /* skip it */
864 GtkWidget
*menuItem
= gtk_item_factory_get_widget( m_factory
, s
.mb_str() );
868 GtkWidget
*menuItem
= checkable
? gtk_check_menu_item_new_with_label( mitem
->GetText().mb_str() )
869 : gtk_menu_item_new_with_label( mitem
->GetText().mb_str() );
871 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
872 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
875 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
876 gtk_widget_show( menuItem
);
880 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
881 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
884 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
885 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
888 mitem
->SetMenuItem(menuItem
);
890 m_items
.Append( mitem
);
893 void wxMenu::Append( int id
, const wxString
&item
, wxMenu
*subMenu
, const wxString
&helpStr
)
895 wxMenuItem
*mitem
= new wxMenuItem(this, id
, item
, helpStr
, FALSE
, subMenu
);
897 #if (GTK_MINOR_VERSION > 0)
898 /* text has "_" instead of "&" after mitem->SetText() */
899 wxString
text( mitem
->GetText() );
901 /* local buffer in multibyte form */
904 strcat( buf
, text
.mb_str() );
906 GtkItemFactoryEntry entry
;
908 entry
.callback
= (GtkItemFactoryCallback
) 0;
909 entry
.callback_action
= 0;
910 entry
.item_type
= "<Branch>";
912 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
914 /* in order to get the pointer to the item we need the item text _without_ underscores */
915 wxString s
= wxT("<main>/");
916 for ( const wxChar
*pc
= text
; *pc
!= wxT('\0'); pc
++ )
918 if (*pc
== wxT('_')) pc
++; /* skip it */
922 GtkWidget
*menuItem
= gtk_item_factory_get_item( m_factory
, s
.mb_str() );
926 GtkWidget
*menuItem
= gtk_menu_item_new_with_label(mitem
->GetText().mbc_str());
928 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
929 gtk_widget_show( menuItem
);
933 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
934 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
937 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
938 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
941 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), subMenu
->m_menu
);
943 mitem
->SetMenuItem(menuItem
);
945 m_items
.Append( mitem
);
948 void wxMenu::Append( wxMenuItem
*item
)
950 m_items
.Append( item
);
952 GtkWidget
*menuItem
= (GtkWidget
*) NULL
;
954 if (item
->IsSeparator())
955 menuItem
= gtk_menu_item_new();
956 else if (item
->IsSubMenu())
957 menuItem
= gtk_menu_item_new_with_label(item
->GetText().mbc_str());
959 menuItem
= item
->IsCheckable() ? gtk_check_menu_item_new_with_label(item
->GetText().mbc_str())
960 : gtk_menu_item_new_with_label(item
->GetText().mbc_str());
962 if (!item
->IsSeparator())
964 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
965 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
968 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
969 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
972 if (!item
->IsSubMenu())
974 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
975 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
980 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
981 gtk_widget_show( menuItem
);
982 item
->SetMenuItem(menuItem
);
985 void wxMenu::Delete( int id
)
987 wxNode
*node
= m_items
.First();
990 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
991 if (item
->GetId() == id
)
993 gtk_widget_destroy( item
->GetMenuItem() );
994 m_items
.DeleteNode( node
);
1001 int wxMenu::FindItem( const wxString itemString
) const
1003 wxString s
= wxT("");
1004 for ( const wxChar
*pc
= itemString
; *pc
!= wxT('\0'); pc
++ )
1006 if (*pc
== wxT('&'))
1009 #if (GTK_MINOR_VERSION > 0)
1016 wxNode
*node
= m_items
.First();
1019 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
1020 if (item
->GetText() == s
)
1022 return item
->GetId();
1024 node
= node
->Next();
1030 void wxMenu::Enable( int id
, bool enable
)
1032 wxMenuItem
*item
= FindItem(id
);
1034 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
1036 item
->Enable(enable
);
1039 bool wxMenu::IsEnabled( int id
) const
1041 wxMenuItem
*item
= FindItem(id
);
1043 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsEnabled: no such item") );
1045 return item
->IsEnabled();
1048 void wxMenu::Check( int id
, bool enable
)
1050 wxMenuItem
*item
= FindItem(id
);
1052 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
1054 item
->Check(enable
);
1057 bool wxMenu::IsChecked( int id
) const
1059 wxMenuItem
*item
= FindItem(id
);
1061 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsChecked: no such item") );
1063 return item
->IsChecked();
1066 void wxMenu::SetLabel( int id
, const wxString
&label
)
1068 wxMenuItem
*item
= FindItem(id
);
1070 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
1072 item
->SetText(label
);
1075 wxString
wxMenu::GetLabel( int id
) const
1077 wxMenuItem
*item
= FindItem(id
);
1079 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetLabel: no such item") );
1081 return item
->GetText();
1084 void wxMenu::SetHelpString( int id
, const wxString
& helpString
)
1086 wxMenuItem
*item
= FindItem(id
);
1088 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
1090 item
->SetHelp( helpString
);
1093 wxString
wxMenu::GetHelpString( int id
) const
1095 wxMenuItem
*item
= FindItem(id
);
1097 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetHelpString: no such item") );
1099 return item
->GetHelp();
1102 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
1104 wxNode
*node
= m_items
.First();
1107 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
1108 if (item
->GetMenuItem() == menuItem
)
1109 return item
->GetId();
1110 node
= node
->Next();
1116 wxMenuItem
*wxMenu::FindItem(int id
) const
1118 wxNode
*node
= m_items
.First();
1121 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
1122 if (item
->GetId() == id
)
1126 node
= node
->Next();
1129 /* Not finding anything here can be correct
1130 * when search the entire menu system for
1131 * an entry -> no error message. */
1133 return (wxMenuItem
*) NULL
;
1136 void wxMenu::SetInvokingWindow( wxWindow
*win
)
1138 m_invokingWindow
= win
;
1141 wxWindow
*wxMenu::GetInvokingWindow()
1143 return m_invokingWindow
;
1146 // Update a menu and all submenus recursively. source is the object that has
1147 // the update event handlers defined for it. If NULL, the menu or associated
1148 // window will be used.
1149 void wxMenu::UpdateUI(wxEvtHandler
* source
)
1151 if (!source
&& GetInvokingWindow())
1152 source
= GetInvokingWindow()->GetEventHandler();
1154 source
= GetEventHandler();
1158 wxNode
* node
= GetItems().First();
1161 wxMenuItem
* item
= (wxMenuItem
*) node
->Data();
1162 if ( !item
->IsSeparator() )
1164 wxWindowID id
= item
->GetId();
1165 wxUpdateUIEvent
event(id
);
1166 event
.SetEventObject( source
);
1168 if (source
->ProcessEvent(event
))
1170 if (event
.GetSetText())
1171 SetLabel(id
, event
.GetText());
1172 if (event
.GetSetChecked())
1173 Check(id
, event
.GetChecked());
1174 if (event
.GetSetEnabled())
1175 Enable(id
, event
.GetEnabled());
1178 if (item
->GetSubMenu())
1179 item
->GetSubMenu()->UpdateUI(source
);
1181 node
= node
->Next();