]>
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" |
30dea054 | 18 | #include "wx/intl.h" |
c801d85f KB |
19 | |
20 | //----------------------------------------------------------------------------- | |
21 | // wxMenuBar | |
22 | //----------------------------------------------------------------------------- | |
23 | ||
24 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow) | |
25 | ||
96fd301f | 26 | wxMenuBar::wxMenuBar() |
c801d85f KB |
27 | { |
28 | m_needParent = FALSE; // hmmm | |
96fd301f | 29 | |
c67daf87 | 30 | PreCreation( (wxWindow *) NULL, -1, wxDefaultPosition, wxDefaultSize, 0, "menu" ); |
c801d85f KB |
31 | |
32 | m_menus.DeleteContents( TRUE ); | |
96fd301f | 33 | |
c801d85f | 34 | m_widget = gtk_handle_box_new(); |
96fd301f | 35 | |
c801d85f | 36 | m_menubar = gtk_menu_bar_new(); |
96fd301f | 37 | |
c801d85f | 38 | gtk_container_add( GTK_CONTAINER(m_widget), m_menubar ); |
96fd301f | 39 | |
c801d85f | 40 | gtk_widget_show( m_menubar ); |
96fd301f | 41 | |
c801d85f KB |
42 | PostCreation(); |
43 | ||
44 | Show( TRUE ); | |
6de97a3b | 45 | } |
c801d85f KB |
46 | |
47 | void wxMenuBar::Append( wxMenu *menu, const wxString &title ) | |
48 | { | |
49 | m_menus.Append( menu ); | |
50 | menu->m_title = title; // ?????? | |
96fd301f | 51 | |
d355d3fe | 52 | int pos; |
c801d85f KB |
53 | do { |
54 | pos = menu->m_title.First( '&' ); | |
d355d3fe RR |
55 | if (pos != -1) menu->m_title.Remove( pos, 1 ); |
56 | } while (pos != -1); | |
96fd301f | 57 | |
c801d85f KB |
58 | GtkWidget *root_menu; |
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 ); | |
96fd301f | 62 | |
c801d85f | 63 | gtk_menu_bar_append( GTK_MENU_BAR(m_menubar), root_menu ); |
6de97a3b | 64 | } |
96fd301f | 65 | |
716b7364 | 66 | static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString ) |
c801d85f KB |
67 | { |
68 | if (menu->m_title == menuString) | |
69 | { | |
70 | int res = menu->FindItem( itemString ); | |
71 | if (res != -1) return res; | |
6de97a3b | 72 | } |
c801d85f KB |
73 | wxNode *node = menu->m_items.First(); |
74 | while (node) | |
75 | { | |
76 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
96fd301f VZ |
77 | if (item->IsSubMenu()) |
78 | return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString); | |
c801d85f | 79 | node = node->Next(); |
6de97a3b | 80 | } |
c801d85f | 81 | return -1; |
6de97a3b | 82 | } |
c801d85f KB |
83 | |
84 | int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const | |
85 | { | |
86 | wxNode *node = m_menus.First(); | |
87 | while (node) | |
88 | { | |
89 | wxMenu *menu = (wxMenu*)node->Data(); | |
90 | int res = FindMenuItemRecursive( menu, menuString, itemString); | |
91 | if (res != -1) return res; | |
92 | node = node->Next(); | |
6de97a3b | 93 | } |
c801d85f | 94 | return -1; |
6de97a3b | 95 | } |
c801d85f | 96 | |
716b7364 | 97 | // Find a wxMenuItem using its id. Recurses down into sub-menus |
96fd301f | 98 | static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id) |
716b7364 | 99 | { |
96fd301f | 100 | wxMenuItem* result = menu->FindItem(id); |
716b7364 RR |
101 | |
102 | wxNode *node = menu->m_items.First(); | |
96fd301f | 103 | while ( node && result == NULL ) { |
716b7364 | 104 | wxMenuItem *item = (wxMenuItem*)node->Data(); |
96fd301f VZ |
105 | if ( item->IsSubMenu() ) |
106 | result = FindMenuItemByIdRecursive( item->GetSubMenu(), id ); | |
716b7364 | 107 | node = node->Next(); |
6de97a3b | 108 | } |
96fd301f | 109 | |
716b7364 | 110 | return result; |
6de97a3b | 111 | } |
716b7364 RR |
112 | |
113 | wxMenuItem* wxMenuBar::FindMenuItemById( int id ) const | |
114 | { | |
115 | wxMenuItem* result = 0; | |
116 | wxNode *node = m_menus.First(); | |
117 | while (node && result == 0) | |
118 | { | |
119 | wxMenu *menu = (wxMenu*)node->Data(); | |
120 | result = FindMenuItemByIdRecursive( menu, id ); | |
121 | node = node->Next(); | |
122 | } | |
123 | return result; | |
124 | } | |
125 | ||
54ff4a70 RR |
126 | void wxMenuBar::Check( int id, bool check ) |
127 | { | |
128 | wxMenuItem* item = FindMenuItemById( id ); | |
129 | if (item) item->Check(check); | |
6de97a3b | 130 | } |
54ff4a70 RR |
131 | |
132 | bool wxMenuBar::Checked( int id ) const | |
716b7364 RR |
133 | { |
134 | wxMenuItem* item = FindMenuItemById( id ); | |
135 | if (item) return item->IsChecked(); | |
136 | return FALSE; | |
6de97a3b | 137 | } |
716b7364 | 138 | |
54ff4a70 RR |
139 | void wxMenuBar::Enable( int id, bool enable ) |
140 | { | |
141 | wxMenuItem* item = FindMenuItemById( id ); | |
142 | if (item) item->Enable(enable); | |
6de97a3b | 143 | } |
54ff4a70 RR |
144 | |
145 | bool wxMenuBar::Enabled( int id ) const | |
716b7364 RR |
146 | { |
147 | wxMenuItem* item = FindMenuItemById( id ); | |
148 | if (item) return item->IsEnabled(); | |
149 | return FALSE; | |
6de97a3b | 150 | } |
716b7364 | 151 | |
c801d85f | 152 | //----------------------------------------------------------------------------- |
cf7a7e13 | 153 | // "activate" |
c801d85f KB |
154 | //----------------------------------------------------------------------------- |
155 | ||
6de97a3b | 156 | static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu ) |
c801d85f | 157 | { |
c801d85f | 158 | int id = menu->FindMenuIdByMenuItem(widget); |
96fd301f VZ |
159 | |
160 | wxASSERT( id != -1 ); // should find it! | |
161 | ||
6de97a3b | 162 | if (!menu->IsEnabled(id)) return; |
96fd301f | 163 | |
cf7a7e13 RR |
164 | wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, id ); |
165 | event.SetEventObject( menu ); | |
166 | event.SetInt(id ); | |
167 | ||
168 | if (menu->m_callback) | |
169 | { | |
170 | (void) (*(menu->m_callback)) (*menu, event); | |
171 | return; | |
172 | } | |
173 | ||
174 | if (menu->GetEventHandler()->ProcessEvent(event)) return; | |
175 | ||
176 | wxWindow *win = menu->GetInvokingWindow(); | |
177 | if (win) win->GetEventHandler()->ProcessEvent( event ); | |
178 | } | |
179 | ||
180 | //----------------------------------------------------------------------------- | |
181 | // "select" | |
182 | //----------------------------------------------------------------------------- | |
183 | ||
184 | static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu ) | |
185 | { | |
186 | int id = menu->FindMenuIdByMenuItem(widget); | |
187 | ||
188 | wxASSERT( id != -1 ); // should find it! | |
189 | ||
190 | if (!menu->IsEnabled(id)) return; | |
191 | ||
192 | wxCommandEvent event( wxEVT_MENU_HIGHLIGHT, id ); | |
c801d85f KB |
193 | event.SetEventObject( menu ); |
194 | event.SetInt(id ); | |
6de97a3b RR |
195 | |
196 | if (menu->m_callback) | |
197 | { | |
198 | (void) (*(menu->m_callback)) (*menu, event); | |
199 | return; | |
200 | } | |
201 | ||
202 | if (menu->GetEventHandler()->ProcessEvent(event)) return; | |
203 | ||
c801d85f | 204 | wxWindow *win = menu->GetInvokingWindow(); |
cf4219e7 | 205 | if (win) win->GetEventHandler()->ProcessEvent( event ); |
6de97a3b | 206 | } |
c801d85f | 207 | |
cf7a7e13 RR |
208 | //----------------------------------------------------------------------------- |
209 | // wxMenu | |
210 | //----------------------------------------------------------------------------- | |
211 | ||
c801d85f | 212 | IMPLEMENT_DYNAMIC_CLASS(wxMenuItem,wxObject) |
96fd301f VZ |
213 | |
214 | wxMenuItem::wxMenuItem() | |
c801d85f | 215 | { |
96fd301f | 216 | m_id = ID_SEPARATOR; |
c801d85f | 217 | m_isCheckMenu = FALSE; |
96fd301f VZ |
218 | m_isChecked = FALSE; |
219 | m_isEnabled = TRUE; | |
c67daf87 UR |
220 | m_subMenu = (wxMenu *) NULL; |
221 | m_menuItem = (GtkWidget *) NULL; | |
6de97a3b | 222 | } |
c801d85f | 223 | |
96fd301f | 224 | void wxMenuItem::SetText(const wxString& str) |
716b7364 | 225 | { |
30dea054 | 226 | m_text = ""; |
96fd301f VZ |
227 | for ( const char *pc = str; *pc != '\0'; pc++ ) { |
228 | if ( *pc == '&' ) | |
229 | pc++; // skip it | |
230 | ||
231 | m_text << *pc; | |
716b7364 RR |
232 | } |
233 | } | |
234 | ||
96fd301f | 235 | void wxMenuItem::Check( bool check ) |
716b7364 | 236 | { |
30f82ea4 | 237 | wxCHECK_RET( IsCheckable(), "Can't check uncheckable item!" ) |
96fd301f VZ |
238 | |
239 | m_isChecked = check; | |
240 | gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check ); | |
716b7364 RR |
241 | } |
242 | ||
a9c96bcc RR |
243 | void wxMenuItem::Enable( bool enable ) |
244 | { | |
245 | gtk_widget_set_sensitive( m_menuItem, enable ); | |
246 | m_isEnabled = enable; | |
247 | } | |
248 | ||
96fd301f | 249 | bool wxMenuItem::IsChecked() const |
716b7364 | 250 | { |
96fd301f VZ |
251 | wxCHECK( IsCheckable(), FALSE ); // can't get state of uncheckable item! |
252 | ||
253 | bool bIsChecked = ((GtkCheckMenuItem*)m_menuItem)->active != 0; | |
254 | ||
255 | wxASSERT( bIsChecked == m_isChecked ); // consistency check | |
256 | ||
257 | return bIsChecked; | |
716b7364 RR |
258 | } |
259 | ||
c801d85f KB |
260 | IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler) |
261 | ||
6de97a3b | 262 | wxMenu::wxMenu( const wxString& title, const wxFunction func ) |
c801d85f KB |
263 | { |
264 | m_title = title; | |
265 | m_items.DeleteContents( TRUE ); | |
c67daf87 | 266 | m_invokingWindow = (wxWindow *) NULL; |
c801d85f | 267 | m_menu = gtk_menu_new(); // Do not show! |
6de97a3b RR |
268 | m_callback = func; |
269 | m_eventHandler = this; | |
270 | if (m_title.IsNull()) m_title = ""; | |
271 | if (m_title != "") | |
272 | { | |
273 | Append(-2, m_title); | |
274 | AppendSeparator(); | |
275 | } | |
276 | } | |
c801d85f | 277 | |
c2dd8380 GL |
278 | void wxMenu::SetTitle( const wxString& title ) |
279 | { | |
280 | // Waiting for something better. | |
281 | m_title = title; | |
282 | } | |
283 | ||
284 | const wxString wxMenu::GetTitle() const | |
285 | { | |
286 | return m_title; | |
287 | } | |
288 | ||
96fd301f | 289 | void wxMenu::AppendSeparator() |
c801d85f KB |
290 | { |
291 | wxMenuItem *mitem = new wxMenuItem(); | |
96fd301f VZ |
292 | mitem->SetId(ID_SEPARATOR); |
293 | ||
294 | GtkWidget *menuItem = gtk_menu_item_new(); | |
295 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); | |
296 | gtk_widget_show( menuItem ); | |
297 | mitem->SetMenuItem(menuItem); | |
c801d85f | 298 | m_items.Append( mitem ); |
6de97a3b | 299 | } |
c801d85f | 300 | |
debe6624 | 301 | void wxMenu::Append( int id, const wxString &item, const wxString &helpStr, bool checkable ) |
c801d85f KB |
302 | { |
303 | wxMenuItem *mitem = new wxMenuItem(); | |
96fd301f VZ |
304 | mitem->SetId(id); |
305 | mitem->SetText(item); | |
c33c4050 | 306 | mitem->SetHelp(helpStr); |
96fd301f VZ |
307 | mitem->SetCheckable(checkable); |
308 | const char *text = mitem->GetText(); | |
309 | GtkWidget *menuItem = checkable ? gtk_check_menu_item_new_with_label(text) | |
310 | : gtk_menu_item_new_with_label(text); | |
a9c96bcc | 311 | |
96fd301f VZ |
312 | mitem->SetMenuItem(menuItem); |
313 | ||
314 | gtk_signal_connect( GTK_OBJECT(menuItem), "activate", | |
315 | GTK_SIGNAL_FUNC(gtk_menu_clicked_callback), | |
316 | (gpointer*)this ); | |
317 | ||
cf7a7e13 RR |
318 | gtk_signal_connect( GTK_OBJECT(menuItem), "select", |
319 | GTK_SIGNAL_FUNC(gtk_menu_hilight_callback), | |
320 | (gpointer*)this ); | |
321 | ||
96fd301f VZ |
322 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); |
323 | gtk_widget_show( menuItem ); | |
c801d85f | 324 | m_items.Append( mitem ); |
6de97a3b | 325 | } |
c801d85f | 326 | |
96fd301f | 327 | void wxMenu::Append( int id, const wxString &text, wxMenu *subMenu, const wxString &helpStr ) |
c801d85f KB |
328 | { |
329 | wxMenuItem *mitem = new wxMenuItem(); | |
96fd301f VZ |
330 | mitem->SetId(id); |
331 | mitem->SetText(text); | |
332 | ||
333 | GtkWidget *menuItem = gtk_menu_item_new_with_label(mitem->GetText()); | |
c33c4050 | 334 | mitem->SetHelp(helpStr); |
96fd301f VZ |
335 | mitem->SetMenuItem(menuItem); |
336 | mitem->SetSubMenu(subMenu); | |
337 | ||
338 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), subMenu->m_menu ); | |
339 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); | |
340 | gtk_widget_show( menuItem ); | |
c801d85f | 341 | m_items.Append( mitem ); |
6de97a3b | 342 | } |
c801d85f KB |
343 | |
344 | int wxMenu::FindItem( const wxString itemString ) const | |
345 | { | |
346 | wxString s( itemString ); | |
96fd301f | 347 | |
d355d3fe | 348 | int pos; |
c801d85f KB |
349 | do { |
350 | pos = s.First( '&' ); | |
d355d3fe RR |
351 | if (pos != -1) s.Remove( pos, 1 ); |
352 | } while (pos != -1); | |
96fd301f | 353 | |
c801d85f KB |
354 | wxNode *node = m_items.First(); |
355 | while (node) | |
356 | { | |
357 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
96fd301f VZ |
358 | if (item->GetText() == s) |
359 | return item->GetId(); | |
c801d85f | 360 | node = node->Next(); |
6de97a3b | 361 | } |
96fd301f | 362 | |
c801d85f | 363 | return -1; |
6de97a3b | 364 | } |
c801d85f | 365 | |
96fd301f | 366 | void wxMenu::Enable( int id, bool enable ) |
716b7364 | 367 | { |
96fd301f VZ |
368 | wxMenuItem *item = FindItem(id); |
369 | if ( item ) | |
370 | item->Enable(enable); | |
6de97a3b | 371 | } |
716b7364 | 372 | |
96fd301f | 373 | bool wxMenu::IsEnabled( int id ) const |
e2414cbe | 374 | { |
96fd301f VZ |
375 | wxMenuItem *item = FindItem(id); |
376 | if ( item ) | |
377 | return item->IsEnabled(); | |
378 | else | |
379 | return FALSE; | |
6de97a3b | 380 | } |
e2414cbe | 381 | |
96fd301f | 382 | void wxMenu::Check( int id, bool enable ) |
c801d85f | 383 | { |
96fd301f VZ |
384 | wxMenuItem *item = FindItem(id); |
385 | if ( item ) | |
386 | item->Check(enable); | |
6de97a3b | 387 | } |
c801d85f | 388 | |
96fd301f | 389 | bool wxMenu::IsChecked( int id ) const |
c801d85f | 390 | { |
96fd301f VZ |
391 | wxMenuItem *item = FindItem(id); |
392 | if ( item ) | |
393 | return item->IsChecked(); | |
394 | else | |
395 | return FALSE; | |
6de97a3b | 396 | } |
c801d85f | 397 | |
debe6624 | 398 | void wxMenu::SetLabel( int id, const wxString &label ) |
c801d85f | 399 | { |
96fd301f | 400 | wxMenuItem *item = FindItem(id); |
c33c4050 | 401 | if (item) |
96fd301f | 402 | item->SetText(label); |
6de97a3b | 403 | } |
96fd301f | 404 | |
c33c4050 RR |
405 | wxString wxMenu::GetLabel( int id ) const |
406 | { | |
407 | wxMenuItem *item = FindItem(id); | |
408 | if (item) return item->GetText(); | |
409 | return ""; | |
410 | } | |
411 | ||
412 | void wxMenu::SetHelpString( int id, const wxString& helpString ) | |
413 | { | |
414 | wxMenuItem *item = FindItem(id); | |
415 | if (item) item->SetHelp( helpString ); | |
416 | } | |
417 | ||
418 | wxString wxMenu::GetHelpString( int id ) const | |
419 | { | |
420 | wxMenuItem *item = FindItem(id); | |
421 | if (item) return item->GetHelp(); | |
422 | return ""; | |
423 | } | |
424 | ||
96fd301f VZ |
425 | int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const |
426 | { | |
c801d85f KB |
427 | wxNode *node = m_items.First(); |
428 | while (node) | |
429 | { | |
430 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
96fd301f VZ |
431 | if (item->GetMenuItem() == menuItem) |
432 | return item->GetId(); | |
c801d85f | 433 | node = node->Next(); |
6de97a3b | 434 | } |
96fd301f VZ |
435 | |
436 | return -1; | |
6de97a3b | 437 | } |
c801d85f | 438 | |
96fd301f | 439 | wxMenuItem *wxMenu::FindItem(int id) const |
c801d85f KB |
440 | { |
441 | wxNode *node = m_items.First(); | |
96fd301f | 442 | while (node) { |
c801d85f | 443 | wxMenuItem *item = (wxMenuItem*)node->Data(); |
96fd301f VZ |
444 | if ( item->GetId() == id ) |
445 | return item; | |
c801d85f | 446 | node = node->Next(); |
6de97a3b | 447 | } |
96fd301f | 448 | |
30f82ea4 | 449 | wxLogDebug( "wxMenu::FindItem: item %d not found.", id); |
96fd301f | 450 | |
c67daf87 | 451 | return (wxMenuItem *) NULL; |
96fd301f | 452 | } |
c801d85f KB |
453 | |
454 | void wxMenu::SetInvokingWindow( wxWindow *win ) | |
455 | { | |
456 | m_invokingWindow = win; | |
6de97a3b | 457 | } |
c801d85f | 458 | |
96fd301f | 459 | wxWindow *wxMenu::GetInvokingWindow() |
c801d85f KB |
460 | { |
461 | return m_invokingWindow; | |
6de97a3b | 462 | } |
c801d85f KB |
463 | |
464 |