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 );
58 menu
->m_owner
= gtk_menu_item_new_with_label( WXSTRINGCAST(menu
->m_title
) );
59 gtk_widget_show( menu
->m_owner
);
60 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
62 gtk_menu_bar_append( GTK_MENU_BAR(m_menubar
), menu
->m_owner
);
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
;
73 wxNode
*node
= menu
->m_items
.First();
76 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
77 if (item
->IsSubMenu())
78 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
);
162 if (item
) return item
->GetText();
167 void wxMenuBar::SetLabel( int id
, const wxString
&label
)
169 wxMenuItem
* item
= FindMenuItemById( id
);
171 if (item
) item
->SetText( label
);
174 void wxMenuBar::EnableTop( int pos
, bool flag
)
176 wxNode
*node
= m_menus
.Nth( pos
);
178 wxCHECK_RET( node
, "menu not found" );
180 wxMenu
* menu
= (wxMenu
*)node
->Data();
182 if (menu
->m_owner
) gtk_widget_set_sensitive( menu
->m_owner
, flag
);
185 wxString
wxMenuBar::GetLabelTop( int pos
) const
187 wxNode
*node
= m_menus
.Nth( pos
);
189 wxCHECK_MSG( node
, "invalid", "menu not found" );
191 wxMenu
* menu
= (wxMenu
*)node
->Data();
193 return menu
->GetTitle();
196 void wxMenuBar::SetLabelTop( int pos
, const wxString
& label
)
198 wxNode
*node
= m_menus
.Nth( pos
);
200 wxCHECK_RET( node
, "menu not found" );
202 wxMenu
* menu
= (wxMenu
*)node
->Data();
204 menu
->SetTitle( label
);
207 //-----------------------------------------------------------------------------
209 //-----------------------------------------------------------------------------
211 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
213 int id
= menu
->FindMenuIdByMenuItem(widget
);
215 /* should find it for normal (not popup) menu */
216 wxASSERT( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
) );
218 if (!menu
->IsEnabled(id
)) return;
220 wxMenuItem
* item
= menu
->FindItem( id
);
221 wxCHECK_RET( item
, "error in menu item callback" );
223 if (item
->m_isCheckMenu
)
225 if (item
->m_isChecked
== item
->IsChecked())
227 /* the menu item has been checked by calling wxMenuItem->Check() */
232 /* the user pressed on the menu item -> report */
233 item
->m_isChecked
= item
->IsChecked(); /* make consistent again */
237 wxCommandEvent
event( wxEVT_COMMAND_MENU_SELECTED
, id
);
238 event
.SetEventObject( menu
);
241 if (menu
->m_callback
)
243 (void) (*(menu
->m_callback
)) (*menu
, event
);
247 if (menu
->GetEventHandler()->ProcessEvent(event
)) return;
249 wxWindow
*win
= menu
->GetInvokingWindow();
250 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
253 //-----------------------------------------------------------------------------
255 //-----------------------------------------------------------------------------
257 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
259 int id
= menu
->FindMenuIdByMenuItem(widget
);
261 wxASSERT( id
!= -1 ); // should find it!
263 if (!menu
->IsEnabled(id
)) return;
265 wxCommandEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
266 event
.SetEventObject( menu
);
269 /* wxMSW doesn't call callback here either
271 if (menu->m_callback)
273 (void) (*(menu->m_callback)) (*menu, event);
278 if (menu
->GetEventHandler()->ProcessEvent(event
)) return;
280 wxWindow
*win
= menu
->GetInvokingWindow();
281 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
284 //-----------------------------------------------------------------------------
286 //-----------------------------------------------------------------------------
288 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
,wxObject
)
290 wxMenuItem::wxMenuItem()
293 m_isCheckMenu
= FALSE
;
296 m_subMenu
= (wxMenu
*) NULL
;
297 m_menuItem
= (GtkWidget
*) NULL
;
300 /* it's valid for this function to be called even if m_menuItem == NULL */
301 void wxMenuItem::SetName( const wxString
& str
)
304 for ( const char *pc
= str
; *pc
!= '\0'; pc
++ )
306 if (*pc
== '&') pc
++; /* skip it */
312 GtkLabel
*label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
313 gtk_label_set( label
, m_text
.c_str());
317 void wxMenuItem::Check( bool check
)
319 wxCHECK_RET( m_menuItem
, "invalid menu item" );
321 wxCHECK_RET( IsCheckable(), "Can't check uncheckable item!" )
323 if (check
== m_isChecked
) return;
326 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
329 void wxMenuItem::Enable( bool enable
)
331 wxCHECK_RET( m_menuItem
, "invalid menu item" );
333 gtk_widget_set_sensitive( m_menuItem
, enable
);
334 m_isEnabled
= enable
;
337 bool wxMenuItem::IsChecked() const
339 wxCHECK_MSG( m_menuItem
, FALSE
, "invalid menu item" );
341 wxCHECK( IsCheckable(), FALSE
); // can't get state of uncheckable item!
343 bool bIsChecked
= ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
348 //-----------------------------------------------------------------------------
350 //-----------------------------------------------------------------------------
352 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
354 wxMenu::wxMenu( const wxString
& title
, const wxFunction func
)
357 m_items
.DeleteContents( TRUE
);
358 m_invokingWindow
= (wxWindow
*) NULL
;
359 m_menu
= gtk_menu_new(); // Do not show!
362 m_eventHandler
= this;
363 m_clientData
= (void*) NULL
;
365 if (m_title
.IsNull()) m_title
= "";
372 m_owner
= (GtkWidget
*) NULL
;
375 void wxMenu::SetTitle( const wxString
& title
)
377 /* Waiting for something better. */
381 const wxString
wxMenu::GetTitle() const
386 void wxMenu::AppendSeparator()
388 wxMenuItem
*mitem
= new wxMenuItem();
389 mitem
->SetId(ID_SEPARATOR
);
391 GtkWidget
*menuItem
= gtk_menu_item_new();
392 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
393 gtk_widget_show( menuItem
);
394 mitem
->SetMenuItem(menuItem
);
395 m_items
.Append( mitem
);
398 void wxMenu::Append( int id
, const wxString
&item
, const wxString
&helpStr
, bool checkable
)
400 wxMenuItem
*mitem
= new wxMenuItem();
402 mitem
->SetText(item
);
403 mitem
->SetHelp(helpStr
);
404 mitem
->SetCheckable(checkable
);
405 const char *text
= mitem
->GetText();
406 GtkWidget
*menuItem
= checkable
? gtk_check_menu_item_new_with_label(text
)
407 : gtk_menu_item_new_with_label(text
);
409 mitem
->SetMenuItem(menuItem
);
411 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
412 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
415 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
416 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
419 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
420 gtk_widget_show( menuItem
);
421 m_items
.Append( mitem
);
424 void wxMenu::Append( int id
, const wxString
&text
, wxMenu
*subMenu
, const wxString
&helpStr
)
426 wxMenuItem
*mitem
= new wxMenuItem();
428 mitem
->SetText(text
);
430 GtkWidget
*menuItem
= gtk_menu_item_new_with_label(mitem
->GetText());
431 mitem
->SetHelp(helpStr
);
432 mitem
->SetMenuItem(menuItem
);
433 mitem
->SetSubMenu(subMenu
);
435 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), subMenu
->m_menu
);
436 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
437 gtk_widget_show( menuItem
);
438 m_items
.Append( mitem
);
441 int wxMenu::FindItem( const wxString itemString
) const
443 wxString
s( itemString
);
448 pos
= s
.First( '&' );
449 if (pos
!= -1) s
.Remove( pos
, 1 );
452 wxNode
*node
= m_items
.First();
455 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
456 if (item
->GetText() == s
)
458 return item
->GetId();
466 void wxMenu::Enable( int id
, bool enable
)
468 wxMenuItem
*item
= FindItem(id
);
471 item
->Enable(enable
);
475 bool wxMenu::IsEnabled( int id
) const
477 wxMenuItem
*item
= FindItem(id
);
480 return item
->IsEnabled();
488 void wxMenu::Check( int id
, bool enable
)
490 wxMenuItem
*item
= FindItem(id
);
497 bool wxMenu::IsChecked( int id
) const
499 wxMenuItem
*item
= FindItem(id
);
502 return item
->IsChecked();
510 void wxMenu::SetLabel( int id
, const wxString
&label
)
512 wxMenuItem
*item
= FindItem(id
);
515 item
->SetText(label
);
519 wxString
wxMenu::GetLabel( int id
) const
521 wxMenuItem
*item
= FindItem(id
);
524 return item
->GetText();
532 void wxMenu::SetHelpString( int id
, const wxString
& helpString
)
534 wxMenuItem
*item
= FindItem(id
);
535 if (item
) item
->SetHelp( helpString
);
538 wxString
wxMenu::GetHelpString( int id
) const
540 wxMenuItem
*item
= FindItem(id
);
543 return item
->GetHelp();
551 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
553 wxNode
*node
= m_items
.First();
556 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
557 if (item
->GetMenuItem() == menuItem
)
558 return item
->GetId();
565 wxMenuItem
*wxMenu::FindItem(int id
) const
567 wxNode
*node
= m_items
.First();
570 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
571 if (item
->GetId() == id
)
578 /* Not finding anything here can be correct
579 * when search the entire menu system for
580 * an entry -> no error message. */
582 return (wxMenuItem
*) NULL
;
585 void wxMenu::SetInvokingWindow( wxWindow
*win
)
587 m_invokingWindow
= win
;
590 wxWindow
*wxMenu::GetInvokingWindow()
592 return m_invokingWindow
;