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"
19 //-----------------------------------------------------------------------------
21 //-----------------------------------------------------------------------------
23 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
,wxWindow
)
25 wxMenuBar::wxMenuBar()
27 m_needParent
= FALSE
; // hmmm
29 PreCreation( (wxWindow
*) NULL
, -1, wxDefaultPosition
, wxDefaultSize
, 0, "menu" );
31 m_menus
.DeleteContents( TRUE
);
33 m_menubar
= gtk_menu_bar_new();
35 m_widget
= GTK_WIDGET(m_menubar
);
42 void wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
44 m_menus
.Append( menu
);
45 menu
->m_title
= title
; // ??????
49 pos
= menu
->m_title
.First( '&' );
50 if (pos
!= -1) menu
->m_title
.Remove( pos
, 1 );
54 root_menu
= gtk_menu_item_new_with_label( WXSTRINGCAST(menu
->m_title
) );
55 gtk_widget_show( root_menu
);
56 gtk_menu_item_set_submenu( GTK_MENU_ITEM(root_menu
), menu
->m_menu
);
58 gtk_menu_bar_append( GTK_MENU_BAR(m_menubar
), root_menu
);
61 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
63 if (menu
->m_title
== menuString
)
65 int res
= menu
->FindItem( itemString
);
66 if (res
!= -1) return res
;
68 wxNode
*node
= menu
->m_items
.First();
71 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
72 if (item
->IsSubMenu())
73 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
79 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
81 wxNode
*node
= m_menus
.First();
84 wxMenu
*menu
= (wxMenu
*)node
->Data();
85 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
86 if (res
!= -1) return res
;
92 // Find a wxMenuItem using its id. Recurses down into sub-menus
93 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
95 wxMenuItem
* result
= menu
->FindItem(id
);
97 wxNode
*node
= menu
->m_items
.First();
98 while ( node
&& result
== NULL
) {
99 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
100 if ( item
->IsSubMenu() )
101 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
108 wxMenuItem
* wxMenuBar::FindMenuItemById( int id
) const
110 wxMenuItem
* result
= 0;
111 wxNode
*node
= m_menus
.First();
112 while (node
&& result
== 0)
114 wxMenu
*menu
= (wxMenu
*)node
->Data();
115 result
= FindMenuItemByIdRecursive( menu
, id
);
121 void wxMenuBar::Check( int id
, bool check
)
123 wxMenuItem
* item
= FindMenuItemById( id
);
124 if (item
) item
->Check(check
);
127 bool wxMenuBar::Checked( int id
) const
129 wxMenuItem
* item
= FindMenuItemById( id
);
130 if (item
) return item
->IsChecked();
134 void wxMenuBar::Enable( int id
, bool enable
)
136 wxMenuItem
* item
= FindMenuItemById( id
);
137 if (item
) item
->Enable(enable
);
140 bool wxMenuBar::Enabled( int id
) const
142 wxMenuItem
* item
= FindMenuItemById( id
);
143 if (item
) return item
->IsEnabled();
147 //-----------------------------------------------------------------------------
149 //-----------------------------------------------------------------------------
151 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
153 int id
= menu
->FindMenuIdByMenuItem(widget
);
155 // should find it for normal (not popup) menu
156 wxASSERT( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
) );
158 if (!menu
->IsEnabled(id
)) return;
160 wxCommandEvent
event( wxEVT_COMMAND_MENU_SELECTED
, id
);
161 event
.SetEventObject( menu
);
164 if (menu
->m_callback
)
166 (void) (*(menu
->m_callback
)) (*menu
, event
);
170 if (menu
->GetEventHandler()->ProcessEvent(event
)) return;
172 wxWindow
*win
= menu
->GetInvokingWindow();
173 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
176 //-----------------------------------------------------------------------------
178 //-----------------------------------------------------------------------------
180 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
182 int id
= menu
->FindMenuIdByMenuItem(widget
);
184 wxASSERT( id
!= -1 ); // should find it!
186 if (!menu
->IsEnabled(id
)) return;
188 wxCommandEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
189 event
.SetEventObject( menu
);
192 /* wxMSW doesn't call callback here either
193 if (menu->m_callback)
195 (void) (*(menu->m_callback)) (*menu, event);
200 if (menu
->GetEventHandler()->ProcessEvent(event
)) return;
202 wxWindow
*win
= menu
->GetInvokingWindow();
203 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
206 //-----------------------------------------------------------------------------
208 //-----------------------------------------------------------------------------
210 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
,wxObject
)
212 wxMenuItem::wxMenuItem()
215 m_isCheckMenu
= FALSE
;
218 m_subMenu
= (wxMenu
*) NULL
;
219 m_menuItem
= (GtkWidget
*) NULL
;
222 // it's valid for this function to be called even if m_menuItem == NULL
223 void wxMenuItem::SetName(const wxString
& str
)
226 for ( const char *pc
= str
; *pc
!= '\0'; pc
++ )
236 GtkLabel
*label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
238 gtk_label_set( label
, m_text
.c_str());
242 void wxMenuItem::Check( bool check
)
244 wxCHECK_RET( m_menuItem
, "invalid menu item" );
246 wxCHECK_RET( IsCheckable(), "Can't check uncheckable item!" )
249 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
252 void wxMenuItem::Enable( bool enable
)
254 wxCHECK_RET( m_menuItem
, "invalid menu item" );
256 gtk_widget_set_sensitive( m_menuItem
, enable
);
257 m_isEnabled
= enable
;
260 bool wxMenuItem::IsChecked() const
262 wxCHECK_MSG( m_menuItem
, FALSE
, "invalid menu item" );
264 wxCHECK( IsCheckable(), FALSE
); // can't get state of uncheckable item!
266 bool bIsChecked
= ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
268 wxASSERT( bIsChecked
== m_isChecked
); // consistency check
273 //-----------------------------------------------------------------------------
275 //-----------------------------------------------------------------------------
277 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
279 wxMenu::wxMenu( const wxString
& title
, const wxFunction func
)
282 m_items
.DeleteContents( TRUE
);
283 m_invokingWindow
= (wxWindow
*) NULL
;
284 m_menu
= gtk_menu_new(); // Do not show!
287 m_eventHandler
= this;
288 m_clientData
= (void*) NULL
;
290 if (m_title
.IsNull()) m_title
= "";
298 void wxMenu::SetTitle( const wxString
& title
)
300 // Waiting for something better.
304 const wxString
wxMenu::GetTitle() const
309 void wxMenu::AppendSeparator()
311 wxMenuItem
*mitem
= new wxMenuItem();
312 mitem
->SetId(ID_SEPARATOR
);
314 GtkWidget
*menuItem
= gtk_menu_item_new();
315 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
316 gtk_widget_show( menuItem
);
317 mitem
->SetMenuItem(menuItem
);
318 m_items
.Append( mitem
);
321 void wxMenu::Append( int id
, const wxString
&item
, const wxString
&helpStr
, bool checkable
)
323 wxMenuItem
*mitem
= new wxMenuItem();
325 mitem
->SetText(item
);
326 mitem
->SetHelp(helpStr
);
327 mitem
->SetCheckable(checkable
);
328 const char *text
= mitem
->GetText();
329 GtkWidget
*menuItem
= checkable
? gtk_check_menu_item_new_with_label(text
)
330 : gtk_menu_item_new_with_label(text
);
332 mitem
->SetMenuItem(menuItem
);
334 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
335 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
338 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
339 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
342 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
343 gtk_widget_show( menuItem
);
344 m_items
.Append( mitem
);
347 void wxMenu::Append( int id
, const wxString
&text
, wxMenu
*subMenu
, const wxString
&helpStr
)
349 wxMenuItem
*mitem
= new wxMenuItem();
351 mitem
->SetText(text
);
353 GtkWidget
*menuItem
= gtk_menu_item_new_with_label(mitem
->GetText());
354 mitem
->SetHelp(helpStr
);
355 mitem
->SetMenuItem(menuItem
);
356 mitem
->SetSubMenu(subMenu
);
358 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), subMenu
->m_menu
);
359 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
360 gtk_widget_show( menuItem
);
361 m_items
.Append( mitem
);
364 int wxMenu::FindItem( const wxString itemString
) const
366 wxString
s( itemString
);
370 pos
= s
.First( '&' );
371 if (pos
!= -1) s
.Remove( pos
, 1 );
374 wxNode
*node
= m_items
.First();
377 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
378 if (item
->GetText() == s
)
379 return item
->GetId();
386 void wxMenu::Enable( int id
, bool enable
)
388 wxMenuItem
*item
= FindItem(id
);
390 item
->Enable(enable
);
393 bool wxMenu::IsEnabled( int id
) const
395 wxMenuItem
*item
= FindItem(id
);
397 return item
->IsEnabled();
402 void wxMenu::Check( int id
, bool enable
)
404 wxMenuItem
*item
= FindItem(id
);
409 bool wxMenu::IsChecked( int id
) const
411 wxMenuItem
*item
= FindItem(id
);
413 return item
->IsChecked();
418 void wxMenu::SetLabel( int id
, const wxString
&label
)
420 wxMenuItem
*item
= FindItem(id
);
422 item
->SetText(label
);
425 wxString
wxMenu::GetLabel( int id
) const
427 wxMenuItem
*item
= FindItem(id
);
428 if (item
) return item
->GetText();
432 void wxMenu::SetHelpString( int id
, const wxString
& helpString
)
434 wxMenuItem
*item
= FindItem(id
);
435 if (item
) item
->SetHelp( helpString
);
438 wxString
wxMenu::GetHelpString( int id
) const
440 wxMenuItem
*item
= FindItem(id
);
441 if (item
) return item
->GetHelp();
445 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
447 wxNode
*node
= m_items
.First();
450 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
451 if (item
->GetMenuItem() == menuItem
)
452 return item
->GetId();
459 wxMenuItem
*wxMenu::FindItem(int id
) const
461 wxNode
*node
= m_items
.First();
463 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
464 if ( item
->GetId() == id
)
469 // Not finding anything here can be correct
470 // when search the entire menu system for
471 // an entry -> no error message.
473 return (wxMenuItem
*) NULL
;
476 void wxMenu::SetInvokingWindow( wxWindow
*win
)
478 m_invokingWindow
= win
;
481 wxWindow
*wxMenu::GetInvokingWindow()
483 return m_invokingWindow
;