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();
159 wxString
wxMenuBar::GetLabel( int id
) const
161 wxMenuItem
* item
= FindMenuItemById( id
);
163 if (item
) return item
->GetText();
168 void wxMenuBar::SetLabel( int id
, const wxString
&label
)
170 wxMenuItem
* item
= FindMenuItemById( id
);
172 if (item
) item
->SetText( label
);
175 void wxMenuBar::EnableTop( int pos
, bool flag
)
177 wxNode
*node
= m_menus
.Nth( pos
);
179 wxCHECK_RET( node
, "menu not found" );
181 wxMenu
* menu
= (wxMenu
*)node
->Data();
183 if (menu
->m_owner
) gtk_widget_set_sensitive( menu
->m_owner
, flag
);
186 wxString
wxMenuBar::GetLabelTop( int pos
) const
188 wxNode
*node
= m_menus
.Nth( pos
);
190 wxCHECK_MSG( node
, "invalid", "menu not found" );
192 wxMenu
* menu
= (wxMenu
*)node
->Data();
194 return menu
->GetTitle();
197 void wxMenuBar::SetLabelTop( int pos
, const wxString
& label
)
199 wxNode
*node
= m_menus
.Nth( pos
);
201 wxCHECK_RET( node
, "menu not found" );
203 wxMenu
* menu
= (wxMenu
*)node
->Data();
205 menu
->SetTitle( label
);
208 void wxMenuBar::SetHelpString( int id
, const wxString
& helpString
)
210 wxMenuItem
* item
= FindMenuItemById( id
);
212 if (item
) item
->SetHelp( helpString
);
215 wxString
wxMenuBar::GetHelpString( int id
) const
217 wxMenuItem
* item
= FindMenuItemById( id
);
220 return item
->GetHelp();
225 //-----------------------------------------------------------------------------
227 //-----------------------------------------------------------------------------
229 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
231 int id
= menu
->FindMenuIdByMenuItem(widget
);
233 /* should find it for normal (not popup) menu */
234 wxASSERT( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
) );
236 if (!menu
->IsEnabled(id
)) return;
238 wxMenuItem
* item
= menu
->FindItem( id
);
239 wxCHECK_RET( item
, "error in menu item callback" );
241 if (item
->m_isCheckMenu
)
243 if (item
->m_isChecked
== item
->IsChecked())
245 /* the menu item has been checked by calling wxMenuItem->Check() */
250 /* the user pressed on the menu item -> report */
251 item
->m_isChecked
= item
->IsChecked(); /* make consistent again */
255 wxCommandEvent
event( wxEVT_COMMAND_MENU_SELECTED
, id
);
256 event
.SetEventObject( menu
);
259 if (menu
->m_callback
)
261 (void) (*(menu
->m_callback
)) (*menu
, event
);
265 if (menu
->GetEventHandler()->ProcessEvent(event
)) return;
267 wxWindow
*win
= menu
->GetInvokingWindow();
268 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
271 //-----------------------------------------------------------------------------
273 //-----------------------------------------------------------------------------
275 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
277 int id
= menu
->FindMenuIdByMenuItem(widget
);
279 wxASSERT( id
!= -1 ); // should find it!
281 if (!menu
->IsEnabled(id
)) return;
283 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
284 event
.SetEventObject( menu
);
286 /* wxMSW doesn't call callback here either
288 if (menu->m_callback)
290 (void) (*(menu->m_callback)) (*menu, event);
295 if (menu
->GetEventHandler()->ProcessEvent(event
)) return;
297 wxWindow
*win
= menu
->GetInvokingWindow();
298 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
301 //-----------------------------------------------------------------------------
303 //-----------------------------------------------------------------------------
305 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
,wxObject
)
307 wxMenuItem::wxMenuItem()
310 m_isCheckMenu
= FALSE
;
313 m_subMenu
= (wxMenu
*) NULL
;
314 m_menuItem
= (GtkWidget
*) NULL
;
317 /* it's valid for this function to be called even if m_menuItem == NULL */
318 void wxMenuItem::SetName( const wxString
& str
)
321 for ( const char *pc
= str
; *pc
!= '\0'; pc
++ )
323 if (*pc
== '&') pc
++; /* skip it */
329 GtkLabel
*label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
330 gtk_label_set( label
, m_text
.c_str());
334 void wxMenuItem::Check( bool check
)
336 wxCHECK_RET( m_menuItem
, "invalid menu item" );
338 wxCHECK_RET( IsCheckable(), "Can't check uncheckable item!" )
340 if (check
== m_isChecked
) return;
343 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
346 void wxMenuItem::Enable( bool enable
)
348 wxCHECK_RET( m_menuItem
, "invalid menu item" );
350 gtk_widget_set_sensitive( m_menuItem
, enable
);
351 m_isEnabled
= enable
;
354 bool wxMenuItem::IsChecked() const
356 wxCHECK_MSG( m_menuItem
, FALSE
, "invalid menu item" );
358 wxCHECK( IsCheckable(), FALSE
); // can't get state of uncheckable item!
360 bool bIsChecked
= ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
365 //-----------------------------------------------------------------------------
367 //-----------------------------------------------------------------------------
369 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
371 wxMenu::wxMenu( const wxString
& title
, const wxFunction func
)
374 m_items
.DeleteContents( TRUE
);
375 m_invokingWindow
= (wxWindow
*) NULL
;
376 m_menu
= gtk_menu_new(); // Do not show!
379 m_eventHandler
= this;
380 m_clientData
= (void*) NULL
;
382 if (m_title
.IsNull()) m_title
= "";
389 m_owner
= (GtkWidget
*) NULL
;
392 void wxMenu::SetTitle( const wxString
& title
)
394 /* Waiting for something better. */
398 const wxString
wxMenu::GetTitle() const
403 void wxMenu::AppendSeparator()
405 wxMenuItem
*mitem
= new wxMenuItem();
406 mitem
->SetId(ID_SEPARATOR
);
408 GtkWidget
*menuItem
= gtk_menu_item_new();
409 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
410 gtk_widget_show( menuItem
);
411 mitem
->SetMenuItem(menuItem
);
412 m_items
.Append( mitem
);
415 void wxMenu::Append( int id
, const wxString
&item
, const wxString
&helpStr
, bool checkable
)
417 wxMenuItem
*mitem
= new wxMenuItem();
419 mitem
->SetText(item
);
420 mitem
->SetHelp(helpStr
);
421 mitem
->SetCheckable(checkable
);
422 const char *text
= mitem
->GetText();
423 GtkWidget
*menuItem
= checkable
? gtk_check_menu_item_new_with_label(text
)
424 : gtk_menu_item_new_with_label(text
);
426 mitem
->SetMenuItem(menuItem
);
428 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
429 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
432 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
433 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
436 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
437 gtk_widget_show( menuItem
);
438 m_items
.Append( mitem
);
441 void wxMenu::Append( int id
, const wxString
&text
, wxMenu
*subMenu
, const wxString
&helpStr
)
443 wxMenuItem
*mitem
= new wxMenuItem();
445 mitem
->SetText(text
);
447 GtkWidget
*menuItem
= gtk_menu_item_new_with_label(mitem
->GetText());
448 mitem
->SetHelp(helpStr
);
449 mitem
->SetMenuItem(menuItem
);
450 mitem
->SetSubMenu(subMenu
);
452 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), subMenu
->m_menu
);
453 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
454 gtk_widget_show( menuItem
);
455 m_items
.Append( mitem
);
458 int wxMenu::FindItem( const wxString itemString
) const
460 wxString
s( itemString
);
465 pos
= s
.First( '&' );
466 if (pos
!= -1) s
.Remove( pos
, 1 );
469 wxNode
*node
= m_items
.First();
472 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
473 if (item
->GetText() == s
)
475 return item
->GetId();
483 void wxMenu::Enable( int id
, bool enable
)
485 wxMenuItem
*item
= FindItem(id
);
488 item
->Enable(enable
);
492 bool wxMenu::IsEnabled( int id
) const
494 wxMenuItem
*item
= FindItem(id
);
497 return item
->IsEnabled();
505 void wxMenu::Check( int id
, bool enable
)
507 wxMenuItem
*item
= FindItem(id
);
514 bool wxMenu::IsChecked( int id
) const
516 wxMenuItem
*item
= FindItem(id
);
519 return item
->IsChecked();
527 void wxMenu::SetLabel( int id
, const wxString
&label
)
529 wxMenuItem
*item
= FindItem(id
);
532 item
->SetText(label
);
536 wxString
wxMenu::GetLabel( int id
) const
538 wxMenuItem
*item
= FindItem(id
);
541 return item
->GetText();
549 void wxMenu::SetHelpString( int id
, const wxString
& helpString
)
551 wxMenuItem
*item
= FindItem(id
);
552 if (item
) item
->SetHelp( helpString
);
555 wxString
wxMenu::GetHelpString( int id
) const
557 wxMenuItem
*item
= FindItem(id
);
560 return item
->GetHelp();
568 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
570 wxNode
*node
= m_items
.First();
573 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
574 if (item
->GetMenuItem() == menuItem
)
575 return item
->GetId();
582 wxMenuItem
*wxMenu::FindItem(int id
) const
584 wxNode
*node
= m_items
.First();
587 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
588 if (item
->GetId() == id
)
595 /* Not finding anything here can be correct
596 * when search the entire menu system for
597 * an entry -> no error message. */
599 return (wxMenuItem
*) NULL
;
602 void wxMenu::SetInvokingWindow( wxWindow
*win
)
604 m_invokingWindow
= win
;
607 wxWindow
*wxMenu::GetInvokingWindow()
609 return m_invokingWindow
;