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 /* wxMSW doesn't call callback here either
197 if (menu->m_callback)
199 (void) (*(menu->m_callback)) (*menu, event);
204 if (menu
->GetEventHandler()->ProcessEvent(event
)) return;
206 wxWindow
*win
= menu
->GetInvokingWindow();
207 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
210 //-----------------------------------------------------------------------------
212 //-----------------------------------------------------------------------------
214 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
,wxObject
)
216 wxMenuItem::wxMenuItem()
219 m_isCheckMenu
= FALSE
;
222 m_subMenu
= (wxMenu
*) NULL
;
223 m_menuItem
= (GtkWidget
*) NULL
;
226 void wxMenuItem::SetText(const wxString
& str
)
229 for ( const char *pc
= str
; *pc
!= '\0'; pc
++ ) {
237 void wxMenuItem::Check( bool check
)
239 wxCHECK_RET( IsCheckable(), "Can't check uncheckable item!" )
242 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
245 void wxMenuItem::Enable( bool enable
)
247 gtk_widget_set_sensitive( m_menuItem
, enable
);
248 m_isEnabled
= enable
;
251 bool wxMenuItem::IsChecked() const
253 wxCHECK( IsCheckable(), FALSE
); // can't get state of uncheckable item!
255 bool bIsChecked
= ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
257 wxASSERT( bIsChecked
== m_isChecked
); // consistency check
262 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
264 wxMenu::wxMenu( const wxString
& title
, const wxFunction func
)
267 m_items
.DeleteContents( TRUE
);
268 m_invokingWindow
= (wxWindow
*) NULL
;
269 m_menu
= gtk_menu_new(); // Do not show!
271 m_eventHandler
= this;
272 if (m_title
.IsNull()) m_title
= "";
280 void wxMenu::SetTitle( const wxString
& title
)
282 // Waiting for something better.
286 const wxString
wxMenu::GetTitle() const
291 void wxMenu::AppendSeparator()
293 wxMenuItem
*mitem
= new wxMenuItem();
294 mitem
->SetId(ID_SEPARATOR
);
296 GtkWidget
*menuItem
= gtk_menu_item_new();
297 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
298 gtk_widget_show( menuItem
);
299 mitem
->SetMenuItem(menuItem
);
300 m_items
.Append( mitem
);
303 void wxMenu::Append( int id
, const wxString
&item
, const wxString
&helpStr
, bool checkable
)
305 wxMenuItem
*mitem
= new wxMenuItem();
307 mitem
->SetText(item
);
308 mitem
->SetHelp(helpStr
);
309 mitem
->SetCheckable(checkable
);
310 const char *text
= mitem
->GetText();
311 GtkWidget
*menuItem
= checkable
? gtk_check_menu_item_new_with_label(text
)
312 : gtk_menu_item_new_with_label(text
);
314 mitem
->SetMenuItem(menuItem
);
316 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
317 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
320 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
321 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
324 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
325 gtk_widget_show( menuItem
);
326 m_items
.Append( mitem
);
329 void wxMenu::Append( int id
, const wxString
&text
, wxMenu
*subMenu
, const wxString
&helpStr
)
331 wxMenuItem
*mitem
= new wxMenuItem();
333 mitem
->SetText(text
);
335 GtkWidget
*menuItem
= gtk_menu_item_new_with_label(mitem
->GetText());
336 mitem
->SetHelp(helpStr
);
337 mitem
->SetMenuItem(menuItem
);
338 mitem
->SetSubMenu(subMenu
);
340 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), subMenu
->m_menu
);
341 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
342 gtk_widget_show( menuItem
);
343 m_items
.Append( mitem
);
346 int wxMenu::FindItem( const wxString itemString
) const
348 wxString
s( itemString
);
352 pos
= s
.First( '&' );
353 if (pos
!= -1) s
.Remove( pos
, 1 );
356 wxNode
*node
= m_items
.First();
359 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
360 if (item
->GetText() == s
)
361 return item
->GetId();
368 void wxMenu::Enable( int id
, bool enable
)
370 wxMenuItem
*item
= FindItem(id
);
372 item
->Enable(enable
);
375 bool wxMenu::IsEnabled( int id
) const
377 wxMenuItem
*item
= FindItem(id
);
379 return item
->IsEnabled();
384 void wxMenu::Check( int id
, bool enable
)
386 wxMenuItem
*item
= FindItem(id
);
391 bool wxMenu::IsChecked( int id
) const
393 wxMenuItem
*item
= FindItem(id
);
395 return item
->IsChecked();
400 void wxMenu::SetLabel( int id
, const wxString
&label
)
402 wxMenuItem
*item
= FindItem(id
);
404 item
->SetText(label
);
407 wxString
wxMenu::GetLabel( int id
) const
409 wxMenuItem
*item
= FindItem(id
);
410 if (item
) return item
->GetText();
414 void wxMenu::SetHelpString( int id
, const wxString
& helpString
)
416 wxMenuItem
*item
= FindItem(id
);
417 if (item
) item
->SetHelp( helpString
);
420 wxString
wxMenu::GetHelpString( int id
) const
422 wxMenuItem
*item
= FindItem(id
);
423 if (item
) return item
->GetHelp();
427 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
429 wxNode
*node
= m_items
.First();
432 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
433 if (item
->GetMenuItem() == menuItem
)
434 return item
->GetId();
441 wxMenuItem
*wxMenu::FindItem(int id
) const
443 wxNode
*node
= m_items
.First();
445 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
446 if ( item
->GetId() == id
)
451 wxLogDebug( "wxMenu::FindItem: item %d not found.", id
);
453 return (wxMenuItem
*) NULL
;
456 void wxMenu::SetInvokingWindow( wxWindow
*win
)
458 m_invokingWindow
= win
;
461 wxWindow
*wxMenu::GetInvokingWindow()
463 return m_invokingWindow
;