1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "menu.h"
19 //-----------------------------------------------------------------------------
21 //-----------------------------------------------------------------------------
23 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
,wxWindow
)
25 wxMenuBar::wxMenuBar()
27 m_needParent
= FALSE
; // hmmm
29 PreCreation( NULL
, -1, wxDefaultPosition
, wxDefaultSize
, 0, "menu" );
31 m_menus
.DeleteContents( TRUE
);
33 m_widget
= gtk_handle_box_new();
35 m_menubar
= gtk_menu_bar_new();
37 gtk_container_add( GTK_CONTAINER(m_widget
), m_menubar
);
39 gtk_widget_show( m_menubar
);
46 void wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
48 m_menus
.Append( menu
);
49 menu
->m_title
= title
; // ??????
53 pos
= menu
->m_title
.First( '&' );
54 if (pos
!= -1) menu
->m_title
.Remove( pos
, 1 );
58 root_menu
= gtk_menu_item_new_with_label( WXSTRINGCAST(menu
->m_title
) );
59 gtk_widget_show( root_menu
);
60 gtk_menu_item_set_submenu( GTK_MENU_ITEM(root_menu
), menu
->m_menu
);
62 gtk_menu_bar_append( GTK_MENU_BAR(m_menubar
), root_menu
);
65 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
67 if (menu
->m_title
== menuString
)
69 int res
= menu
->FindItem( itemString
);
70 if (res
!= -1) return res
;
72 wxNode
*node
= menu
->m_items
.First();
75 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
76 if (item
->IsSubMenu())
77 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
83 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
85 wxNode
*node
= m_menus
.First();
88 wxMenu
*menu
= (wxMenu
*)node
->Data();
89 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
90 if (res
!= -1) return res
;
96 // Find a wxMenuItem using its id. Recurses down into sub-menus
97 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
99 wxMenuItem
* result
= menu
->FindItem(id
);
101 wxNode
*node
= menu
->m_items
.First();
102 while ( node
&& result
== NULL
) {
103 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
104 if ( item
->IsSubMenu() )
105 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
112 wxMenuItem
* wxMenuBar::FindMenuItemById( int id
) const
114 wxMenuItem
* result
= 0;
115 wxNode
*node
= m_menus
.First();
116 while (node
&& result
== 0)
118 wxMenu
*menu
= (wxMenu
*)node
->Data();
119 result
= FindMenuItemByIdRecursive( menu
, id
);
125 void wxMenuBar::Check( int id
, bool check
)
127 wxMenuItem
* item
= FindMenuItemById( id
);
128 if (item
) item
->Check(check
);
131 bool wxMenuBar::Checked( int id
) const
133 wxMenuItem
* item
= FindMenuItemById( id
);
134 if (item
) return item
->IsChecked();
138 void wxMenuBar::Enable( int id
, bool enable
)
140 wxMenuItem
* item
= FindMenuItemById( id
);
141 if (item
) item
->Enable(enable
);
144 bool wxMenuBar::Enabled( int id
) const
146 wxMenuItem
* item
= FindMenuItemById( id
);
147 if (item
) return item
->IsEnabled();
151 //-----------------------------------------------------------------------------
153 //-----------------------------------------------------------------------------
155 void gtk_menu_clicked_callback( GtkWidget
*widget
, gpointer data
)
157 wxMenu
*menu
= (wxMenu
*)data
;
158 int id
= menu
->FindMenuIdByMenuItem(widget
);
160 wxASSERT( id
!= -1 ); // should find it!
162 if (!menu
->IsEnabled(id
))
165 wxCommandEvent
event( wxEVENT_TYPE_MENU_COMMAND
, id
);
166 event
.SetEventObject( menu
);
168 wxWindow
*win
= menu
->GetInvokingWindow();
169 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
172 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
,wxObject
)
174 wxMenuItem::wxMenuItem()
177 m_isCheckMenu
= FALSE
;
184 void wxMenuItem::SetText(const wxString
& str
)
186 for ( const char *pc
= str
; *pc
!= '\0'; pc
++ ) {
194 void wxMenuItem::Check( bool check
)
196 wxCHECK_RET( IsCheckable(), "can't check uncheckable item!" )
199 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
202 bool wxMenuItem::IsChecked() const
204 wxCHECK( IsCheckable(), FALSE
); // can't get state of uncheckable item!
206 bool bIsChecked
= ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
208 wxASSERT( bIsChecked
== m_isChecked
); // consistency check
213 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
215 wxMenu::wxMenu( const wxString
&title
)
218 m_items
.DeleteContents( TRUE
);
219 m_invokingWindow
= NULL
;
220 m_menu
= gtk_menu_new(); // Do not show!
223 void wxMenu::AppendSeparator()
225 wxMenuItem
*mitem
= new wxMenuItem();
226 mitem
->SetId(ID_SEPARATOR
);
228 GtkWidget
*menuItem
= gtk_menu_item_new();
229 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
230 gtk_widget_show( menuItem
);
231 mitem
->SetMenuItem(menuItem
);
232 m_items
.Append( mitem
);
235 void wxMenu::Append( int id
, const wxString
&item
, const wxString
&helpStr
, bool checkable
)
237 wxMenuItem
*mitem
= new wxMenuItem();
239 mitem
->SetText(item
);
240 mitem
->SetHelpString(helpStr
);
241 mitem
->SetCheckable(checkable
);
242 const char *text
= mitem
->GetText();
243 GtkWidget
*menuItem
= checkable
? gtk_check_menu_item_new_with_label(text
)
244 : gtk_menu_item_new_with_label(text
);
245 mitem
->SetMenuItem(menuItem
);
247 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
248 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
251 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
252 gtk_widget_show( menuItem
);
253 m_items
.Append( mitem
);
256 void wxMenu::Append( int id
, const wxString
&text
, wxMenu
*subMenu
, const wxString
&helpStr
)
258 wxMenuItem
*mitem
= new wxMenuItem();
260 mitem
->SetText(text
);
262 GtkWidget
*menuItem
= gtk_menu_item_new_with_label(mitem
->GetText());
263 mitem
->SetHelpString(helpStr
);
264 mitem
->SetMenuItem(menuItem
);
265 mitem
->SetSubMenu(subMenu
);
267 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), subMenu
->m_menu
);
268 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
269 gtk_widget_show( menuItem
);
270 m_items
.Append( mitem
);
273 int wxMenu::FindItem( const wxString itemString
) const
275 wxString
s( itemString
);
279 pos
= s
.First( '&' );
280 if (pos
!= -1) s
.Remove( pos
, 1 );
283 wxNode
*node
= m_items
.First();
286 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
287 if (item
->GetText() == s
)
288 return item
->GetId();
295 void wxMenu::Enable( int id
, bool enable
)
297 wxMenuItem
*item
= FindItem(id
);
299 item
->Enable(enable
);
302 bool wxMenu::IsEnabled( int id
) const
304 wxMenuItem
*item
= FindItem(id
);
306 return item
->IsEnabled();
311 void wxMenu::Check( int id
, bool enable
)
313 wxMenuItem
*item
= FindItem(id
);
318 bool wxMenu::IsChecked( int id
) const
320 wxMenuItem
*item
= FindItem(id
);
322 return item
->IsChecked();
327 void wxMenu::SetLabel( int id
, const wxString
&label
)
329 wxMenuItem
*item
= FindItem(id
);
331 item
->SetText(label
);
334 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
336 wxNode
*node
= m_items
.First();
339 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
340 if (item
->GetMenuItem() == menuItem
)
341 return item
->GetId();
348 wxMenuItem
*wxMenu::FindItem(int id
) const
350 wxNode
*node
= m_items
.First();
352 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
353 if ( item
->GetId() == id
)
358 wxLogDebug("wxMenu::FindItem: item %d not found.", id
);
363 void wxMenu::SetInvokingWindow( wxWindow
*win
)
365 m_invokingWindow
= win
;
368 wxWindow
*wxMenu::GetInvokingWindow()
370 return m_invokingWindow
;