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"
23 //-----------------------------------------------------------------------------
25 //-----------------------------------------------------------------------------
27 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
,wxWindow
)
29 wxMenuBar::wxMenuBar()
31 m_needParent
= FALSE
; // hmmm
33 PreCreation( (wxWindow
*) NULL
, -1, wxDefaultPosition
, wxDefaultSize
, 0, "menu" );
35 m_menus
.DeleteContents( TRUE
);
37 m_menubar
= gtk_menu_bar_new();
39 m_widget
= GTK_WIDGET(m_menubar
);
46 void wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
48 m_menus
.Append( menu
);
49 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
;
74 wxNode
*node
= menu
->m_items
.First();
77 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
78 if (item
->IsSubMenu())
79 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
86 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
88 wxNode
*node
= m_menus
.First();
91 wxMenu
*menu
= (wxMenu
*)node
->Data();
92 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
93 if (res
!= -1) return res
;
99 /* Find a wxMenuItem using its id. Recurses down into sub-menus */
100 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
102 wxMenuItem
* result
= menu
->FindItem(id
);
104 wxNode
*node
= menu
->m_items
.First();
105 while ( node
&& result
== NULL
)
107 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
108 if (item
->IsSubMenu())
110 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
118 wxMenuItem
* wxMenuBar::FindMenuItemById( int id
) const
120 wxMenuItem
* result
= 0;
121 wxNode
*node
= m_menus
.First();
122 while (node
&& result
== 0)
124 wxMenu
*menu
= (wxMenu
*)node
->Data();
125 result
= FindMenuItemByIdRecursive( menu
, id
);
132 void wxMenuBar::Check( int id
, bool check
)
134 wxMenuItem
* item
= FindMenuItemById( id
);
135 if (item
) item
->Check(check
);
138 bool wxMenuBar::Checked( int id
) const
140 wxMenuItem
* item
= FindMenuItemById( id
);
141 if (item
) return item
->IsChecked();
145 void wxMenuBar::Enable( int id
, bool enable
)
147 wxMenuItem
* item
= FindMenuItemById( id
);
148 if (item
) item
->Enable(enable
);
151 bool wxMenuBar::Enabled( int id
) const
153 wxMenuItem
* item
= FindMenuItemById( id
);
154 if (item
) return item
->IsEnabled();
158 wxString
wxMenuBar::GetLabel( int id
) const
160 wxMenuItem
* item
= FindMenuItemById( id
);
161 if (item
) return item
->GetText();
165 void wxMenuBar::SetLabel( int id
, const wxString
&label
)
167 wxMenuItem
* item
= FindMenuItemById( id
);
168 if (item
) return item
->SetText( label
);
171 void wxMenuBar::EnableTop( int WXUNUSED(pos
), bool WXUNUSED(flag
) )
175 wxString
wxMenuBar::GetLabelTop( int WXUNUSED(pos
) ) const
180 void wxMenuBar::SetLabelTop( int WXUNUSED(pos
), const wxString
& WXUNUSED(label
) )
184 //-----------------------------------------------------------------------------
186 //-----------------------------------------------------------------------------
188 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
190 int id
= menu
->FindMenuIdByMenuItem(widget
);
192 /* should find it for normal (not popup) menu */
193 wxASSERT( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
) );
195 if (!menu
->IsEnabled(id
)) return;
197 wxMenuItem
* item
= menu
->FindItem( id
);
198 wxCHECK_RET( item
, "error in menu item callback" );
200 if (item
->m_isCheckMenu
)
202 if (item
->m_isChecked
== item
->IsChecked())
204 /* the menu item has been checked by calling wxMenuItem->Check() */
209 /* the user pressed on the menu item -> report */
210 item
->m_isChecked
= item
->IsChecked(); /* make consistent again */
214 wxCommandEvent
event( wxEVT_COMMAND_MENU_SELECTED
, id
);
215 event
.SetEventObject( menu
);
218 if (menu
->m_callback
)
220 (void) (*(menu
->m_callback
)) (*menu
, event
);
224 if (menu
->GetEventHandler()->ProcessEvent(event
)) return;
226 wxWindow
*win
= menu
->GetInvokingWindow();
227 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
230 //-----------------------------------------------------------------------------
232 //-----------------------------------------------------------------------------
234 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
236 int id
= menu
->FindMenuIdByMenuItem(widget
);
238 wxASSERT( id
!= -1 ); // should find it!
240 if (!menu
->IsEnabled(id
)) return;
242 wxCommandEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
243 event
.SetEventObject( menu
);
246 /* wxMSW doesn't call callback here either
248 if (menu->m_callback)
250 (void) (*(menu->m_callback)) (*menu, event);
255 if (menu
->GetEventHandler()->ProcessEvent(event
)) return;
257 wxWindow
*win
= menu
->GetInvokingWindow();
258 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
261 //-----------------------------------------------------------------------------
263 //-----------------------------------------------------------------------------
265 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
,wxObject
)
267 wxMenuItem::wxMenuItem()
270 m_isCheckMenu
= FALSE
;
273 m_subMenu
= (wxMenu
*) NULL
;
274 m_menuItem
= (GtkWidget
*) NULL
;
277 /* it's valid for this function to be called even if m_menuItem == NULL */
278 void wxMenuItem::SetName( const wxString
& str
)
281 for ( const char *pc
= str
; *pc
!= '\0'; pc
++ )
283 if (*pc
== '&') pc
++; /* skip it */
289 GtkLabel
*label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
290 gtk_label_set( label
, m_text
.c_str());
294 void wxMenuItem::Check( bool check
)
296 wxCHECK_RET( m_menuItem
, "invalid menu item" );
298 wxCHECK_RET( IsCheckable(), "Can't check uncheckable item!" )
300 if (check
== m_isChecked
) return;
303 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
306 void wxMenuItem::Enable( bool enable
)
308 wxCHECK_RET( m_menuItem
, "invalid menu item" );
310 gtk_widget_set_sensitive( m_menuItem
, enable
);
311 m_isEnabled
= enable
;
314 bool wxMenuItem::IsChecked() const
316 wxCHECK_MSG( m_menuItem
, FALSE
, "invalid menu item" );
318 wxCHECK( IsCheckable(), FALSE
); // can't get state of uncheckable item!
320 bool bIsChecked
= ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
325 //-----------------------------------------------------------------------------
327 //-----------------------------------------------------------------------------
329 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
331 wxMenu::wxMenu( const wxString
& title
, const wxFunction func
)
334 m_items
.DeleteContents( TRUE
);
335 m_invokingWindow
= (wxWindow
*) NULL
;
336 m_menu
= gtk_menu_new(); // Do not show!
339 m_eventHandler
= this;
340 m_clientData
= (void*) NULL
;
342 if (m_title
.IsNull()) m_title
= "";
350 void wxMenu::SetTitle( const wxString
& title
)
352 /* Waiting for something better. */
356 const wxString
wxMenu::GetTitle() const
361 void wxMenu::AppendSeparator()
363 wxMenuItem
*mitem
= new wxMenuItem();
364 mitem
->SetId(ID_SEPARATOR
);
366 GtkWidget
*menuItem
= gtk_menu_item_new();
367 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
368 gtk_widget_show( menuItem
);
369 mitem
->SetMenuItem(menuItem
);
370 m_items
.Append( mitem
);
373 void wxMenu::Append( int id
, const wxString
&item
, const wxString
&helpStr
, bool checkable
)
375 wxMenuItem
*mitem
= new wxMenuItem();
377 mitem
->SetText(item
);
378 mitem
->SetHelp(helpStr
);
379 mitem
->SetCheckable(checkable
);
380 const char *text
= mitem
->GetText();
381 GtkWidget
*menuItem
= checkable
? gtk_check_menu_item_new_with_label(text
)
382 : gtk_menu_item_new_with_label(text
);
384 mitem
->SetMenuItem(menuItem
);
386 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
387 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
390 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
391 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
394 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
395 gtk_widget_show( menuItem
);
396 m_items
.Append( mitem
);
399 void wxMenu::Append( int id
, const wxString
&text
, wxMenu
*subMenu
, const wxString
&helpStr
)
401 wxMenuItem
*mitem
= new wxMenuItem();
403 mitem
->SetText(text
);
405 GtkWidget
*menuItem
= gtk_menu_item_new_with_label(mitem
->GetText());
406 mitem
->SetHelp(helpStr
);
407 mitem
->SetMenuItem(menuItem
);
408 mitem
->SetSubMenu(subMenu
);
410 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), subMenu
->m_menu
);
411 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
412 gtk_widget_show( menuItem
);
413 m_items
.Append( mitem
);
416 int wxMenu::FindItem( const wxString itemString
) const
418 wxString
s( itemString
);
423 pos
= s
.First( '&' );
424 if (pos
!= -1) s
.Remove( pos
, 1 );
427 wxNode
*node
= m_items
.First();
430 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
431 if (item
->GetText() == s
)
433 return item
->GetId();
441 void wxMenu::Enable( int id
, bool enable
)
443 wxMenuItem
*item
= FindItem(id
);
446 item
->Enable(enable
);
450 bool wxMenu::IsEnabled( int id
) const
452 wxMenuItem
*item
= FindItem(id
);
455 return item
->IsEnabled();
463 void wxMenu::Check( int id
, bool enable
)
465 wxMenuItem
*item
= FindItem(id
);
472 bool wxMenu::IsChecked( int id
) const
474 wxMenuItem
*item
= FindItem(id
);
477 return item
->IsChecked();
485 void wxMenu::SetLabel( int id
, const wxString
&label
)
487 wxMenuItem
*item
= FindItem(id
);
490 item
->SetText(label
);
494 wxString
wxMenu::GetLabel( int id
) const
496 wxMenuItem
*item
= FindItem(id
);
499 return item
->GetText();
507 void wxMenu::SetHelpString( int id
, const wxString
& helpString
)
509 wxMenuItem
*item
= FindItem(id
);
510 if (item
) item
->SetHelp( helpString
);
513 wxString
wxMenu::GetHelpString( int id
) const
515 wxMenuItem
*item
= FindItem(id
);
518 return item
->GetHelp();
526 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
528 wxNode
*node
= m_items
.First();
531 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
532 if (item
->GetMenuItem() == menuItem
)
533 return item
->GetId();
540 wxMenuItem
*wxMenu::FindItem(int id
) const
542 wxNode
*node
= m_items
.First();
545 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
546 if (item
->GetId() == id
)
553 /* Not finding anything here can be correct
554 * when search the entire menu system for
555 * an entry -> no error message. */
557 return (wxMenuItem
*) NULL
;
560 void wxMenu::SetInvokingWindow( wxWindow
*win
)
562 m_invokingWindow
= win
;
565 wxWindow
*wxMenu::GetInvokingWindow()
567 return m_invokingWindow
;