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"
20 //-----------------------------------------------------------------------------
22 //-----------------------------------------------------------------------------
24 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
,wxWindow
)
26 wxMenuBar::wxMenuBar()
28 m_needParent
= FALSE
; // hmmm
30 PreCreation( (wxWindow
*) NULL
, -1, wxDefaultPosition
, wxDefaultSize
, 0, "menu" );
32 m_menus
.DeleteContents( TRUE
);
34 m_widget
= gtk_handle_box_new();
36 m_menubar
= gtk_menu_bar_new();
38 gtk_container_add( GTK_CONTAINER(m_widget
), m_menubar
);
40 gtk_widget_show( m_menubar
);
47 void wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
49 m_menus
.Append( menu
);
50 menu
->m_title
= title
; // ??????
54 pos
= menu
->m_title
.First( '&' );
55 if (pos
!= -1) menu
->m_title
.Remove( pos
, 1 );
59 root_menu
= gtk_menu_item_new_with_label( WXSTRINGCAST(menu
->m_title
) );
60 gtk_widget_show( root_menu
);
61 gtk_menu_item_set_submenu( GTK_MENU_ITEM(root_menu
), menu
->m_menu
);
63 gtk_menu_bar_append( GTK_MENU_BAR(m_menubar
), root_menu
);
66 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
68 if (menu
->m_title
== menuString
)
70 int res
= menu
->FindItem( itemString
);
71 if (res
!= -1) return res
;
73 wxNode
*node
= menu
->m_items
.First();
76 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
77 if (item
->IsSubMenu())
78 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
84 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
86 wxNode
*node
= m_menus
.First();
89 wxMenu
*menu
= (wxMenu
*)node
->Data();
90 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
91 if (res
!= -1) return res
;
97 // Find a wxMenuItem using its id. Recurses down into sub-menus
98 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
100 wxMenuItem
* result
= menu
->FindItem(id
);
102 wxNode
*node
= menu
->m_items
.First();
103 while ( node
&& result
== NULL
) {
104 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
105 if ( item
->IsSubMenu() )
106 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
113 wxMenuItem
* wxMenuBar::FindMenuItemById( int id
) const
115 wxMenuItem
* result
= 0;
116 wxNode
*node
= m_menus
.First();
117 while (node
&& result
== 0)
119 wxMenu
*menu
= (wxMenu
*)node
->Data();
120 result
= FindMenuItemByIdRecursive( menu
, id
);
126 void wxMenuBar::Check( int id
, bool check
)
128 wxMenuItem
* item
= FindMenuItemById( id
);
129 if (item
) item
->Check(check
);
132 bool wxMenuBar::Checked( int id
) const
134 wxMenuItem
* item
= FindMenuItemById( id
);
135 if (item
) return item
->IsChecked();
139 void wxMenuBar::Enable( int id
, bool enable
)
141 wxMenuItem
* item
= FindMenuItemById( id
);
142 if (item
) item
->Enable(enable
);
145 bool wxMenuBar::Enabled( int id
) const
147 wxMenuItem
* item
= FindMenuItemById( id
);
148 if (item
) return item
->IsEnabled();
152 //-----------------------------------------------------------------------------
154 //-----------------------------------------------------------------------------
156 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
158 int id
= menu
->FindMenuIdByMenuItem(widget
);
160 wxASSERT( id
!= -1 ); // should find it!
162 if (!menu
->IsEnabled(id
)) return;
164 wxCommandEvent
event( wxEVT_COMMAND_MENU_SELECTED
, id
);
165 event
.SetEventObject( menu
);
168 if (menu
->m_callback
)
170 (void) (*(menu
->m_callback
)) (*menu
, event
);
174 if (menu
->GetEventHandler()->ProcessEvent(event
)) return;
176 wxWindow
*win
= menu
->GetInvokingWindow();
177 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
180 //-----------------------------------------------------------------------------
182 //-----------------------------------------------------------------------------
184 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
186 int id
= menu
->FindMenuIdByMenuItem(widget
);
188 wxASSERT( id
!= -1 ); // should find it!
190 if (!menu
->IsEnabled(id
)) return;
192 wxCommandEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
193 event
.SetEventObject( menu
);
196 if (menu
->m_callback
)
198 (void) (*(menu
->m_callback
)) (*menu
, event
);
202 if (menu
->GetEventHandler()->ProcessEvent(event
)) return;
204 wxWindow
*win
= menu
->GetInvokingWindow();
205 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
208 //-----------------------------------------------------------------------------
210 //-----------------------------------------------------------------------------
212 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
,wxObject
)
214 wxMenuItem::wxMenuItem()
217 m_isCheckMenu
= FALSE
;
220 m_subMenu
= (wxMenu
*) NULL
;
221 m_menuItem
= (GtkWidget
*) NULL
;
224 void wxMenuItem::SetText(const wxString
& str
)
227 for ( const char *pc
= str
; *pc
!= '\0'; pc
++ ) {
235 void wxMenuItem::Check( bool check
)
237 wxCHECK_RET( IsCheckable(), "Can't check uncheckable item!" )
240 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
243 void wxMenuItem::Enable( bool enable
)
245 gtk_widget_set_sensitive( m_menuItem
, enable
);
246 m_isEnabled
= enable
;
249 bool wxMenuItem::IsChecked() const
251 wxCHECK( IsCheckable(), FALSE
); // can't get state of uncheckable item!
253 bool bIsChecked
= ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
255 wxASSERT( bIsChecked
== m_isChecked
); // consistency check
260 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
262 wxMenu::wxMenu( const wxString
& title
, const wxFunction func
)
265 m_items
.DeleteContents( TRUE
);
266 m_invokingWindow
= (wxWindow
*) NULL
;
267 m_menu
= gtk_menu_new(); // Do not show!
269 m_eventHandler
= this;
270 if (m_title
.IsNull()) m_title
= "";
278 void wxMenu::SetTitle( const wxString
& title
)
280 // Waiting for something better.
284 const wxString
wxMenu::GetTitle() const
289 void wxMenu::AppendSeparator()
291 wxMenuItem
*mitem
= new wxMenuItem();
292 mitem
->SetId(ID_SEPARATOR
);
294 GtkWidget
*menuItem
= gtk_menu_item_new();
295 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
296 gtk_widget_show( menuItem
);
297 mitem
->SetMenuItem(menuItem
);
298 m_items
.Append( mitem
);
301 void wxMenu::Append( int id
, const wxString
&item
, const wxString
&helpStr
, bool checkable
)
303 wxMenuItem
*mitem
= new wxMenuItem();
305 mitem
->SetText(item
);
306 mitem
->SetHelp(helpStr
);
307 mitem
->SetCheckable(checkable
);
308 const char *text
= mitem
->GetText();
309 GtkWidget
*menuItem
= checkable
? gtk_check_menu_item_new_with_label(text
)
310 : gtk_menu_item_new_with_label(text
);
312 mitem
->SetMenuItem(menuItem
);
314 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
315 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
318 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
319 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
322 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
323 gtk_widget_show( menuItem
);
324 m_items
.Append( mitem
);
327 void wxMenu::Append( int id
, const wxString
&text
, wxMenu
*subMenu
, const wxString
&helpStr
)
329 wxMenuItem
*mitem
= new wxMenuItem();
331 mitem
->SetText(text
);
333 GtkWidget
*menuItem
= gtk_menu_item_new_with_label(mitem
->GetText());
334 mitem
->SetHelp(helpStr
);
335 mitem
->SetMenuItem(menuItem
);
336 mitem
->SetSubMenu(subMenu
);
338 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), subMenu
->m_menu
);
339 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
340 gtk_widget_show( menuItem
);
341 m_items
.Append( mitem
);
344 int wxMenu::FindItem( const wxString itemString
) const
346 wxString
s( itemString
);
350 pos
= s
.First( '&' );
351 if (pos
!= -1) s
.Remove( pos
, 1 );
354 wxNode
*node
= m_items
.First();
357 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
358 if (item
->GetText() == s
)
359 return item
->GetId();
366 void wxMenu::Enable( int id
, bool enable
)
368 wxMenuItem
*item
= FindItem(id
);
370 item
->Enable(enable
);
373 bool wxMenu::IsEnabled( int id
) const
375 wxMenuItem
*item
= FindItem(id
);
377 return item
->IsEnabled();
382 void wxMenu::Check( int id
, bool enable
)
384 wxMenuItem
*item
= FindItem(id
);
389 bool wxMenu::IsChecked( int id
) const
391 wxMenuItem
*item
= FindItem(id
);
393 return item
->IsChecked();
398 void wxMenu::SetLabel( int id
, const wxString
&label
)
400 wxMenuItem
*item
= FindItem(id
);
402 item
->SetText(label
);
405 wxString
wxMenu::GetLabel( int id
) const
407 wxMenuItem
*item
= FindItem(id
);
408 if (item
) return item
->GetText();
412 void wxMenu::SetHelpString( int id
, const wxString
& helpString
)
414 wxMenuItem
*item
= FindItem(id
);
415 if (item
) item
->SetHelp( helpString
);
418 wxString
wxMenu::GetHelpString( int id
) const
420 wxMenuItem
*item
= FindItem(id
);
421 if (item
) return item
->GetHelp();
425 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
427 wxNode
*node
= m_items
.First();
430 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
431 if (item
->GetMenuItem() == menuItem
)
432 return item
->GetId();
439 wxMenuItem
*wxMenu::FindItem(int id
) const
441 wxNode
*node
= m_items
.First();
443 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
444 if ( item
->GetId() == id
)
449 wxLogDebug( "wxMenu::FindItem: item %d not found.", id
);
451 return (wxMenuItem
*) NULL
;
454 void wxMenu::SetInvokingWindow( wxWindow
*win
)
456 m_invokingWindow
= win
;
459 wxWindow
*wxMenu::GetInvokingWindow()
461 return m_invokingWindow
;