1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "menu.h"
18 //-----------------------------------------------------------------------------
20 //-----------------------------------------------------------------------------
22 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
,wxWindow
)
24 wxMenuBar::wxMenuBar(void)
26 m_needParent
= FALSE
; // hmmm
28 PreCreation( NULL
, -1, wxDefaultPosition
, wxDefaultSize
, 0, "menu" );
30 m_menus
.DeleteContents( TRUE
);
32 m_widget
= gtk_handle_box_new();
34 m_menubar
= gtk_menu_bar_new();
36 gtk_container_add( GTK_CONTAINER(m_widget
), m_menubar
);
38 gtk_widget_show( m_menubar
);
45 void wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
47 m_menus
.Append( menu
);
48 menu
->m_title
= title
; // ??????
52 pos
= menu
->m_title
.First( '&' );
53 if (pos
!= wxString::npos
) menu
->m_title
.Remove( pos
, 1 );
54 } while (pos
!= wxString::npos
);
57 root_menu
= gtk_menu_item_new_with_label( WXSTRINGCAST(menu
->m_title
) );
58 gtk_widget_show( root_menu
);
59 gtk_menu_item_set_submenu( GTK_MENU_ITEM(root_menu
), menu
->m_menu
);
61 gtk_menu_bar_append( GTK_MENU_BAR(m_menubar
), root_menu
);
64 int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
66 if (menu
->m_title
== menuString
)
68 int res
= menu
->FindItem( itemString
);
69 if (res
!= -1) return res
;
71 wxNode
*node
= menu
->m_items
.First();
74 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
75 if (item
->m_subMenu
) return FindMenuItemRecursive( item
->m_subMenu
, menuString
, itemString
);
81 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
83 wxNode
*node
= m_menus
.First();
86 wxMenu
*menu
= (wxMenu
*)node
->Data();
87 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
88 if (res
!= -1) return res
;
95 //-----------------------------------------------------------------------------
97 //-----------------------------------------------------------------------------
99 void gtk_menu_clicked_callback( GtkWidget
*widget
, gpointer data
)
101 wxMenu
*menu
= (wxMenu
*)data
;
102 int id
= menu
->FindMenuIdByMenuItem(widget
);
104 if (!menu
->Enabled(id
)) return;
106 wxCommandEvent
event( wxEVENT_TYPE_MENU_COMMAND
, id
);
107 event
.SetEventObject( menu
);
109 wxWindow
*win
= menu
->GetInvokingWindow();
110 if (win
) win
->ProcessEvent( event
);
113 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
,wxObject
)
115 wxMenuItem::wxMenuItem(void)
119 m_isCheckMenu
= FALSE
;
127 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
129 wxMenu::wxMenu( const wxString
&title
)
132 m_items
.DeleteContents( TRUE
);
133 m_invokingWindow
= NULL
;
134 m_menu
= gtk_menu_new(); // Do not show!
137 void wxMenu::AppendSeparator(void)
139 wxMenuItem
*mitem
= new wxMenuItem();
140 mitem
->m_id
= ID_SEPARATOR
;
142 mitem
->m_helpStr
= "";
143 mitem
->m_isCheckMenu
= FALSE
;
144 mitem
->m_isEnabled
= TRUE
;
145 mitem
->m_menuItem
= gtk_menu_item_new();
146 gtk_menu_append( GTK_MENU(m_menu
), mitem
->m_menuItem
);
147 gtk_widget_show( mitem
->m_menuItem
);
148 m_items
.Append( mitem
);
151 void wxMenu::Append( int id
, const wxString
&item
, const wxString
&helpStr
, bool checkable
)
153 wxMenuItem
*mitem
= new wxMenuItem();
156 mitem
->m_text
= item
;
159 pos
= mitem
->m_text
.First( '&' );
160 if (pos
!= wxString::npos
) mitem
->m_text
.Remove( pos
, 1 );
161 } while (pos
!= wxString::npos
);
163 mitem
->m_helpStr
= helpStr
;
164 mitem
->m_isCheckMenu
= checkable
;
165 mitem
->m_isEnabled
= TRUE
;
167 mitem
->m_menuItem
= gtk_check_menu_item_new_with_label( WXSTRINGCAST(mitem
->m_text
) );
169 mitem
->m_menuItem
= gtk_menu_item_new_with_label( WXSTRINGCAST(mitem
->m_text
) );
171 gtk_signal_connect( GTK_OBJECT(mitem
->m_menuItem
), "activate",
172 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
), (gpointer
*)this );
174 gtk_menu_append( GTK_MENU(m_menu
), mitem
->m_menuItem
);
175 gtk_widget_show( mitem
->m_menuItem
);
176 m_items
.Append( mitem
);
179 void wxMenu::Append( int id
, const wxString
&item
, wxMenu
*subMenu
, const wxString
&helpStr
)
181 wxMenuItem
*mitem
= new wxMenuItem();
183 mitem
->m_text
= item
;
184 mitem
->m_isEnabled
= TRUE
;
188 pos
= mitem
->m_text
.First( '&' );
189 if (pos
!= wxString::npos
) mitem
->m_text
.Remove( pos
, 1 );
190 } while (pos
!= wxString::npos
);
192 mitem
->m_helpStr
= helpStr
;
193 mitem
->m_menuItem
= gtk_menu_item_new_with_label( WXSTRINGCAST(mitem
->m_text
) );
195 mitem
->m_subMenu
= subMenu
;
196 gtk_menu_item_set_submenu( GTK_MENU_ITEM(mitem
->m_menuItem
), subMenu
->m_menu
);
197 gtk_menu_append( GTK_MENU(m_menu
), mitem
->m_menuItem
);
198 gtk_widget_show( mitem
->m_menuItem
);
199 m_items
.Append( mitem
);
202 int wxMenu::FindItem( const wxString itemString
) const
204 wxString
s( itemString
);
208 pos
= s
.First( '&' );
209 if (pos
!= wxString::npos
) s
.Remove( pos
, 1 );
210 } while (pos
!= wxString::npos
);
212 wxNode
*node
= m_items
.First();
215 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
216 if (item
->m_text
== s
) return item
->m_id
;
222 void wxMenu::Check( int WXUNUSED(id
), bool WXUNUSED(Flag
) )
227 void wxMenu::Enable( int id
, bool enable
)
229 wxNode
*node
= m_items
.First();
232 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
233 if (item
->m_id
== id
)
235 item
->m_isEnabled
= enable
;
242 bool wxMenu::Enabled( int id
) const
244 wxNode
*node
= m_items
.First();
247 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
248 if (item
->m_id
== id
) return item
->m_isEnabled
;
254 void wxMenu::SetLabel( int id
, const wxString
&label
)
259 pos
= s
.First( '&' );
260 if (pos
!= wxString::npos
) s
.Remove( pos
, 1 );
261 } while (pos
!= wxString::npos
);
263 wxNode
*node
= m_items
.First();
266 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
268 if (item
->m_id
== id
)
270 gtk_label_set( GTK_LABEL( GTK_BIN(item
->m_menuItem
)->child
), WXSTRINGCAST(s
) );
276 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
278 wxNode
*node
= m_items
.First();
281 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
282 if (item
->m_menuItem
== menuItem
) return item
->m_id
;
288 void wxMenu::SetInvokingWindow( wxWindow
*win
)
290 m_invokingWindow
= win
;
293 wxWindow
*wxMenu::GetInvokingWindow(void)
295 return m_invokingWindow
;