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 //-----------------------------------------------------------------------------
160 //-----------------------------------------------------------------------------
162 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
166 int id
= menu
->FindMenuIdByMenuItem(widget
);
168 /* should find it for normal (not popup) menu */
169 wxASSERT( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
) );
171 if (!menu
->IsEnabled(id
)) return;
173 wxMenuItem
* item
= menu
->FindItem( id
);
174 wxCHECK_RET( item
, "error in menu item callback" );
176 if (item
->m_isCheckMenu
)
178 if (item
->m_isChecked
== item
->IsChecked())
180 /* the menu item has been checked by calling wxMenuItem->Check() */
185 /* the user pressed on the menu item -> report */
186 item
->m_isChecked
= item
->IsChecked(); /* make consistent again */
190 wxCommandEvent
event( wxEVT_COMMAND_MENU_SELECTED
, id
);
191 event
.SetEventObject( menu
);
194 if (menu
->m_callback
)
196 (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 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
212 int id
= menu
->FindMenuIdByMenuItem(widget
);
214 wxASSERT( id
!= -1 ); // should find it!
216 if (!menu
->IsEnabled(id
)) return;
218 wxCommandEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
219 event
.SetEventObject( menu
);
222 /* wxMSW doesn't call callback here either
224 if (menu->m_callback)
226 (void) (*(menu->m_callback)) (*menu, event);
231 if (menu
->GetEventHandler()->ProcessEvent(event
)) return;
233 wxWindow
*win
= menu
->GetInvokingWindow();
234 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
237 //-----------------------------------------------------------------------------
239 //-----------------------------------------------------------------------------
241 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
,wxObject
)
243 wxMenuItem::wxMenuItem()
246 m_isCheckMenu
= FALSE
;
249 m_subMenu
= (wxMenu
*) NULL
;
250 m_menuItem
= (GtkWidget
*) NULL
;
253 /* it's valid for this function to be called even if m_menuItem == NULL */
254 void wxMenuItem::SetName( const wxString
& str
)
257 for ( const char *pc
= str
; *pc
!= '\0'; pc
++ )
259 if (*pc
== '&') pc
++; /* skip it */
265 GtkLabel
*label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
266 gtk_label_set( label
, m_text
.c_str());
270 void wxMenuItem::Check( bool check
)
272 wxCHECK_RET( m_menuItem
, "invalid menu item" );
274 wxCHECK_RET( IsCheckable(), "Can't check uncheckable item!" )
276 if (check
== m_isChecked
) return;
279 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
282 void wxMenuItem::Enable( bool enable
)
284 wxCHECK_RET( m_menuItem
, "invalid menu item" );
286 gtk_widget_set_sensitive( m_menuItem
, enable
);
287 m_isEnabled
= enable
;
290 bool wxMenuItem::IsChecked() const
292 wxCHECK_MSG( m_menuItem
, FALSE
, "invalid menu item" );
294 wxCHECK( IsCheckable(), FALSE
); // can't get state of uncheckable item!
296 bool bIsChecked
= ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
301 //-----------------------------------------------------------------------------
303 //-----------------------------------------------------------------------------
305 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
307 wxMenu::wxMenu( const wxString
& title
, const wxFunction func
)
310 m_items
.DeleteContents( TRUE
);
311 m_invokingWindow
= (wxWindow
*) NULL
;
312 m_menu
= gtk_menu_new(); // Do not show!
315 m_eventHandler
= this;
316 m_clientData
= (void*) NULL
;
318 if (m_title
.IsNull()) m_title
= "";
326 void wxMenu::SetTitle( const wxString
& title
)
328 /* Waiting for something better. */
332 const wxString
wxMenu::GetTitle() const
337 void wxMenu::AppendSeparator()
339 wxMenuItem
*mitem
= new wxMenuItem();
340 mitem
->SetId(ID_SEPARATOR
);
342 GtkWidget
*menuItem
= gtk_menu_item_new();
343 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
344 gtk_widget_show( menuItem
);
345 mitem
->SetMenuItem(menuItem
);
346 m_items
.Append( mitem
);
349 void wxMenu::Append( int id
, const wxString
&item
, const wxString
&helpStr
, bool checkable
)
351 wxMenuItem
*mitem
= new wxMenuItem();
353 mitem
->SetText(item
);
354 mitem
->SetHelp(helpStr
);
355 mitem
->SetCheckable(checkable
);
356 const char *text
= mitem
->GetText();
357 GtkWidget
*menuItem
= checkable
? gtk_check_menu_item_new_with_label(text
)
358 : gtk_menu_item_new_with_label(text
);
360 mitem
->SetMenuItem(menuItem
);
362 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
363 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
366 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
367 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
370 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
371 gtk_widget_show( menuItem
);
372 m_items
.Append( mitem
);
375 void wxMenu::Append( int id
, const wxString
&text
, wxMenu
*subMenu
, const wxString
&helpStr
)
377 wxMenuItem
*mitem
= new wxMenuItem();
379 mitem
->SetText(text
);
381 GtkWidget
*menuItem
= gtk_menu_item_new_with_label(mitem
->GetText());
382 mitem
->SetHelp(helpStr
);
383 mitem
->SetMenuItem(menuItem
);
384 mitem
->SetSubMenu(subMenu
);
386 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), subMenu
->m_menu
);
387 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
388 gtk_widget_show( menuItem
);
389 m_items
.Append( mitem
);
392 int wxMenu::FindItem( const wxString itemString
) const
394 wxString
s( itemString
);
399 pos
= s
.First( '&' );
400 if (pos
!= -1) s
.Remove( pos
, 1 );
403 wxNode
*node
= m_items
.First();
406 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
407 if (item
->GetText() == s
)
409 return item
->GetId();
417 void wxMenu::Enable( int id
, bool enable
)
419 wxMenuItem
*item
= FindItem(id
);
422 item
->Enable(enable
);
426 bool wxMenu::IsEnabled( int id
) const
428 wxMenuItem
*item
= FindItem(id
);
431 return item
->IsEnabled();
439 void wxMenu::Check( int id
, bool enable
)
441 wxMenuItem
*item
= FindItem(id
);
448 bool wxMenu::IsChecked( int id
) const
450 wxMenuItem
*item
= FindItem(id
);
453 return item
->IsChecked();
461 void wxMenu::SetLabel( int id
, const wxString
&label
)
463 wxMenuItem
*item
= FindItem(id
);
466 item
->SetText(label
);
470 wxString
wxMenu::GetLabel( int id
) const
472 wxMenuItem
*item
= FindItem(id
);
475 return item
->GetText();
483 void wxMenu::SetHelpString( int id
, const wxString
& helpString
)
485 wxMenuItem
*item
= FindItem(id
);
486 if (item
) item
->SetHelp( helpString
);
489 wxString
wxMenu::GetHelpString( int id
) const
491 wxMenuItem
*item
= FindItem(id
);
494 return item
->GetHelp();
502 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
504 wxNode
*node
= m_items
.First();
507 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
508 if (item
->GetMenuItem() == menuItem
)
509 return item
->GetId();
516 wxMenuItem
*wxMenu::FindItem(int id
) const
518 wxNode
*node
= m_items
.First();
521 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
522 if (item
->GetId() == id
)
529 /* Not finding anything here can be correct
530 * when search the entire menu system for
531 * an entry -> no error message. */
533 return (wxMenuItem
*) NULL
;
536 void wxMenu::SetInvokingWindow( wxWindow
*win
)
538 m_invokingWindow
= win
;
541 wxWindow
*wxMenu::GetInvokingWindow()
543 return m_invokingWindow
;