]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: menu.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Created: 01/02/97 | |
96fd301f | 6 | // Id: $Id$ |
c801d85f | 7 | // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem |
96fd301f | 8 | // Licence: wxWindows licence |
c801d85f KB |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "menu.h" | |
14 | #endif | |
15 | ||
16 | #include "wx/menu.h" | |
96fd301f | 17 | #include "wx/log.h" |
c801d85f KB |
18 | |
19 | //----------------------------------------------------------------------------- | |
20 | // wxMenuBar | |
21 | //----------------------------------------------------------------------------- | |
22 | ||
23 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow) | |
24 | ||
96fd301f | 25 | wxMenuBar::wxMenuBar() |
c801d85f KB |
26 | { |
27 | m_needParent = FALSE; // hmmm | |
96fd301f | 28 | |
c801d85f KB |
29 | PreCreation( NULL, -1, wxDefaultPosition, wxDefaultSize, 0, "menu" ); |
30 | ||
31 | m_menus.DeleteContents( TRUE ); | |
96fd301f | 32 | |
c801d85f | 33 | m_widget = gtk_handle_box_new(); |
96fd301f | 34 | |
c801d85f | 35 | m_menubar = gtk_menu_bar_new(); |
96fd301f | 36 | |
c801d85f | 37 | gtk_container_add( GTK_CONTAINER(m_widget), m_menubar ); |
96fd301f | 38 | |
c801d85f | 39 | gtk_widget_show( m_menubar ); |
96fd301f | 40 | |
c801d85f KB |
41 | PostCreation(); |
42 | ||
43 | Show( TRUE ); | |
44 | }; | |
45 | ||
46 | void wxMenuBar::Append( wxMenu *menu, const wxString &title ) | |
47 | { | |
48 | m_menus.Append( menu ); | |
49 | menu->m_title = title; // ?????? | |
96fd301f | 50 | |
d355d3fe | 51 | int pos; |
c801d85f KB |
52 | do { |
53 | pos = menu->m_title.First( '&' ); | |
d355d3fe RR |
54 | if (pos != -1) menu->m_title.Remove( pos, 1 ); |
55 | } while (pos != -1); | |
96fd301f | 56 | |
c801d85f KB |
57 | GtkWidget *root_menu; |
58 | root_menu = gtk_menu_item_new_with_label( WXSTRINGCAST(menu->m_title) ); | |
59 | gtk_widget_show( root_menu ); | |
60 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(root_menu), menu->m_menu ); | |
96fd301f | 61 | |
c801d85f KB |
62 | gtk_menu_bar_append( GTK_MENU_BAR(m_menubar), root_menu ); |
63 | }; | |
96fd301f | 64 | |
716b7364 | 65 | static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString ) |
c801d85f KB |
66 | { |
67 | if (menu->m_title == menuString) | |
68 | { | |
69 | int res = menu->FindItem( itemString ); | |
70 | if (res != -1) return res; | |
71 | }; | |
72 | wxNode *node = menu->m_items.First(); | |
73 | while (node) | |
74 | { | |
75 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
96fd301f VZ |
76 | if (item->IsSubMenu()) |
77 | return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString); | |
c801d85f KB |
78 | node = node->Next(); |
79 | }; | |
80 | return -1; | |
81 | }; | |
82 | ||
83 | int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const | |
84 | { | |
85 | wxNode *node = m_menus.First(); | |
86 | while (node) | |
87 | { | |
88 | wxMenu *menu = (wxMenu*)node->Data(); | |
89 | int res = FindMenuItemRecursive( menu, menuString, itemString); | |
90 | if (res != -1) return res; | |
91 | node = node->Next(); | |
92 | }; | |
93 | return -1; | |
94 | }; | |
95 | ||
716b7364 | 96 | // Find a wxMenuItem using its id. Recurses down into sub-menus |
96fd301f | 97 | static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id) |
716b7364 | 98 | { |
96fd301f | 99 | wxMenuItem* result = menu->FindItem(id); |
716b7364 RR |
100 | |
101 | wxNode *node = menu->m_items.First(); | |
96fd301f | 102 | while ( node && result == NULL ) { |
716b7364 | 103 | wxMenuItem *item = (wxMenuItem*)node->Data(); |
96fd301f VZ |
104 | if ( item->IsSubMenu() ) |
105 | result = FindMenuItemByIdRecursive( item->GetSubMenu(), id ); | |
716b7364 RR |
106 | node = node->Next(); |
107 | }; | |
96fd301f | 108 | |
716b7364 RR |
109 | return result; |
110 | }; | |
111 | ||
112 | wxMenuItem* wxMenuBar::FindMenuItemById( int id ) const | |
113 | { | |
114 | wxMenuItem* result = 0; | |
115 | wxNode *node = m_menus.First(); | |
116 | while (node && result == 0) | |
117 | { | |
118 | wxMenu *menu = (wxMenu*)node->Data(); | |
119 | result = FindMenuItemByIdRecursive( menu, id ); | |
120 | node = node->Next(); | |
121 | } | |
122 | return result; | |
123 | } | |
124 | ||
125 | bool wxMenuBar::IsChecked( int id ) const | |
126 | { | |
127 | wxMenuItem* item = FindMenuItemById( id ); | |
128 | if (item) return item->IsChecked(); | |
129 | return FALSE; | |
130 | } | |
131 | ||
132 | bool wxMenuBar::IsEnabled( int id ) const | |
133 | { | |
134 | wxMenuItem* item = FindMenuItemById( id ); | |
135 | if (item) return item->IsEnabled(); | |
136 | return FALSE; | |
137 | } | |
138 | ||
c801d85f KB |
139 | //----------------------------------------------------------------------------- |
140 | // wxMenu | |
141 | //----------------------------------------------------------------------------- | |
142 | ||
143 | void gtk_menu_clicked_callback( GtkWidget *widget, gpointer data ) | |
144 | { | |
145 | wxMenu *menu = (wxMenu*)data; | |
146 | int id = menu->FindMenuIdByMenuItem(widget); | |
96fd301f VZ |
147 | |
148 | wxASSERT( id != -1 ); // should find it! | |
149 | ||
150 | if (!menu->IsEnabled(id)) | |
151 | return; | |
152 | ||
c801d85f KB |
153 | wxCommandEvent event( wxEVENT_TYPE_MENU_COMMAND, id ); |
154 | event.SetEventObject( menu ); | |
155 | event.SetInt(id ); | |
156 | wxWindow *win = menu->GetInvokingWindow(); | |
cf4219e7 | 157 | if (win) win->GetEventHandler()->ProcessEvent( event ); |
c801d85f KB |
158 | }; |
159 | ||
160 | IMPLEMENT_DYNAMIC_CLASS(wxMenuItem,wxObject) | |
96fd301f VZ |
161 | |
162 | wxMenuItem::wxMenuItem() | |
c801d85f | 163 | { |
96fd301f | 164 | m_id = ID_SEPARATOR; |
c801d85f | 165 | m_isCheckMenu = FALSE; |
96fd301f VZ |
166 | m_isChecked = FALSE; |
167 | m_isEnabled = TRUE; | |
c801d85f | 168 | m_subMenu = NULL; |
c801d85f KB |
169 | m_menuItem = NULL; |
170 | }; | |
171 | ||
96fd301f | 172 | void wxMenuItem::SetText(const wxString& str) |
716b7364 | 173 | { |
96fd301f VZ |
174 | for ( const char *pc = str; *pc != '\0'; pc++ ) { |
175 | if ( *pc == '&' ) | |
176 | pc++; // skip it | |
177 | ||
178 | m_text << *pc; | |
716b7364 RR |
179 | } |
180 | } | |
181 | ||
96fd301f | 182 | void wxMenuItem::Check( bool check ) |
716b7364 | 183 | { |
96fd301f VZ |
184 | wxCHECK_RET( IsCheckable(), "can't check uncheckable item!" ) |
185 | ||
186 | m_isChecked = check; | |
187 | gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check ); | |
716b7364 RR |
188 | } |
189 | ||
96fd301f | 190 | bool wxMenuItem::IsChecked() const |
716b7364 | 191 | { |
96fd301f VZ |
192 | wxCHECK( IsCheckable(), FALSE ); // can't get state of uncheckable item! |
193 | ||
194 | bool bIsChecked = ((GtkCheckMenuItem*)m_menuItem)->active != 0; | |
195 | ||
196 | wxASSERT( bIsChecked == m_isChecked ); // consistency check | |
197 | ||
198 | return bIsChecked; | |
716b7364 RR |
199 | } |
200 | ||
c801d85f KB |
201 | IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler) |
202 | ||
203 | wxMenu::wxMenu( const wxString &title ) | |
204 | { | |
205 | m_title = title; | |
206 | m_items.DeleteContents( TRUE ); | |
207 | m_invokingWindow = NULL; | |
208 | m_menu = gtk_menu_new(); // Do not show! | |
209 | }; | |
210 | ||
96fd301f | 211 | void wxMenu::AppendSeparator() |
c801d85f KB |
212 | { |
213 | wxMenuItem *mitem = new wxMenuItem(); | |
96fd301f VZ |
214 | mitem->SetId(ID_SEPARATOR); |
215 | ||
216 | GtkWidget *menuItem = gtk_menu_item_new(); | |
217 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); | |
218 | gtk_widget_show( menuItem ); | |
219 | mitem->SetMenuItem(menuItem); | |
c801d85f KB |
220 | m_items.Append( mitem ); |
221 | }; | |
222 | ||
debe6624 | 223 | void wxMenu::Append( int id, const wxString &item, const wxString &helpStr, bool checkable ) |
c801d85f KB |
224 | { |
225 | wxMenuItem *mitem = new wxMenuItem(); | |
96fd301f VZ |
226 | mitem->SetId(id); |
227 | mitem->SetText(item); | |
228 | mitem->SetHelpString(helpStr); | |
229 | mitem->SetCheckable(checkable); | |
230 | const char *text = mitem->GetText(); | |
231 | GtkWidget *menuItem = checkable ? gtk_check_menu_item_new_with_label(text) | |
232 | : gtk_menu_item_new_with_label(text); | |
233 | mitem->SetMenuItem(menuItem); | |
234 | ||
235 | gtk_signal_connect( GTK_OBJECT(menuItem), "activate", | |
236 | GTK_SIGNAL_FUNC(gtk_menu_clicked_callback), | |
237 | (gpointer*)this ); | |
238 | ||
239 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); | |
240 | gtk_widget_show( menuItem ); | |
c801d85f KB |
241 | m_items.Append( mitem ); |
242 | }; | |
243 | ||
96fd301f | 244 | void wxMenu::Append( int id, const wxString &text, wxMenu *subMenu, const wxString &helpStr ) |
c801d85f KB |
245 | { |
246 | wxMenuItem *mitem = new wxMenuItem(); | |
96fd301f VZ |
247 | mitem->SetId(id); |
248 | mitem->SetText(text); | |
249 | ||
250 | GtkWidget *menuItem = gtk_menu_item_new_with_label(mitem->GetText()); | |
251 | mitem->SetHelpString(helpStr); | |
252 | mitem->SetMenuItem(menuItem); | |
253 | mitem->SetSubMenu(subMenu); | |
254 | ||
255 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), subMenu->m_menu ); | |
256 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); | |
257 | gtk_widget_show( menuItem ); | |
c801d85f KB |
258 | m_items.Append( mitem ); |
259 | }; | |
260 | ||
261 | int wxMenu::FindItem( const wxString itemString ) const | |
262 | { | |
263 | wxString s( itemString ); | |
96fd301f | 264 | |
d355d3fe | 265 | int pos; |
c801d85f KB |
266 | do { |
267 | pos = s.First( '&' ); | |
d355d3fe RR |
268 | if (pos != -1) s.Remove( pos, 1 ); |
269 | } while (pos != -1); | |
96fd301f | 270 | |
c801d85f KB |
271 | wxNode *node = m_items.First(); |
272 | while (node) | |
273 | { | |
274 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
96fd301f VZ |
275 | if (item->GetText() == s) |
276 | return item->GetId(); | |
c801d85f KB |
277 | node = node->Next(); |
278 | }; | |
96fd301f | 279 | |
c801d85f KB |
280 | return -1; |
281 | }; | |
282 | ||
96fd301f | 283 | void wxMenu::Enable( int id, bool enable ) |
716b7364 | 284 | { |
96fd301f VZ |
285 | wxMenuItem *item = FindItem(id); |
286 | if ( item ) | |
287 | item->Enable(enable); | |
288 | }; | |
716b7364 | 289 | |
96fd301f | 290 | bool wxMenu::IsEnabled( int id ) const |
e2414cbe | 291 | { |
96fd301f VZ |
292 | wxMenuItem *item = FindItem(id); |
293 | if ( item ) | |
294 | return item->IsEnabled(); | |
295 | else | |
296 | return FALSE; | |
e2414cbe RR |
297 | }; |
298 | ||
96fd301f | 299 | void wxMenu::Check( int id, bool enable ) |
c801d85f | 300 | { |
96fd301f VZ |
301 | wxMenuItem *item = FindItem(id); |
302 | if ( item ) | |
303 | item->Check(enable); | |
c801d85f KB |
304 | }; |
305 | ||
96fd301f | 306 | bool wxMenu::IsChecked( int id ) const |
c801d85f | 307 | { |
96fd301f VZ |
308 | wxMenuItem *item = FindItem(id); |
309 | if ( item ) | |
310 | return item->IsChecked(); | |
311 | else | |
312 | return FALSE; | |
c801d85f KB |
313 | }; |
314 | ||
debe6624 | 315 | void wxMenu::SetLabel( int id, const wxString &label ) |
c801d85f | 316 | { |
96fd301f VZ |
317 | wxMenuItem *item = FindItem(id); |
318 | if ( item ) | |
319 | item->SetText(label); | |
320 | }; | |
321 | ||
322 | int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const | |
323 | { | |
c801d85f KB |
324 | wxNode *node = m_items.First(); |
325 | while (node) | |
326 | { | |
327 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
96fd301f VZ |
328 | if (item->GetMenuItem() == menuItem) |
329 | return item->GetId(); | |
c801d85f KB |
330 | node = node->Next(); |
331 | }; | |
96fd301f VZ |
332 | |
333 | return -1; | |
c801d85f KB |
334 | }; |
335 | ||
96fd301f | 336 | wxMenuItem *wxMenu::FindItem(int id) const |
c801d85f KB |
337 | { |
338 | wxNode *node = m_items.First(); | |
96fd301f | 339 | while (node) { |
c801d85f | 340 | wxMenuItem *item = (wxMenuItem*)node->Data(); |
96fd301f VZ |
341 | if ( item->GetId() == id ) |
342 | return item; | |
c801d85f KB |
343 | node = node->Next(); |
344 | }; | |
96fd301f VZ |
345 | |
346 | wxLogDebug("wxMenu::FindItem: item %d not found.", id); | |
347 | ||
348 | return NULL; | |
349 | } | |
c801d85f KB |
350 | |
351 | void wxMenu::SetInvokingWindow( wxWindow *win ) | |
352 | { | |
353 | m_invokingWindow = win; | |
354 | }; | |
355 | ||
96fd301f | 356 | wxWindow *wxMenu::GetInvokingWindow() |
c801d85f KB |
357 | { |
358 | return m_invokingWindow; | |
359 | }; | |
360 | ||
361 |