]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: menu.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
96fd301f | 5 | // Id: $Id$ |
a81258be | 6 | // Copyright: (c) 1998 Robert Roebling |
96fd301f | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
c801d85f KB |
10 | #ifdef __GNUG__ |
11 | #pragma implementation "menu.h" | |
6ca41e57 | 12 | #pragma implementation "menuitem.h" |
c801d85f KB |
13 | #endif |
14 | ||
15 | #include "wx/menu.h" | |
96fd301f | 16 | #include "wx/log.h" |
30dea054 | 17 | #include "wx/intl.h" |
06cfab17 | 18 | #include "wx/app.h" |
c801d85f | 19 | |
83624f79 RR |
20 | #include "gdk/gdk.h" |
21 | #include "gtk/gtk.h" | |
22 | ||
c801d85f KB |
23 | //----------------------------------------------------------------------------- |
24 | // wxMenuBar | |
25 | //----------------------------------------------------------------------------- | |
26 | ||
27 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow) | |
28 | ||
3502e687 RR |
29 | wxMenuBar::wxMenuBar( long style ) |
30 | { | |
31 | m_needParent = FALSE; // hmmm | |
32 | ||
33 | PreCreation( (wxWindow *) NULL, -1, wxDefaultPosition, wxDefaultSize, 0, "menu" ); | |
34 | ||
35 | m_menus.DeleteContents( TRUE ); | |
36 | ||
37 | m_menubar = gtk_menu_bar_new(); | |
38 | ||
39 | if (style & wxMB_DOCKABLE) | |
40 | { | |
41 | m_widget = gtk_handle_box_new(); | |
42 | gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_menubar) ); | |
43 | gtk_widget_show( GTK_WIDGET(m_menubar) ); | |
44 | } | |
45 | else | |
46 | { | |
47 | m_widget = GTK_WIDGET(m_menubar); | |
48 | } | |
49 | ||
50 | PostCreation(); | |
51 | ||
52 | Show( TRUE ); | |
53 | } | |
54 | ||
96fd301f | 55 | wxMenuBar::wxMenuBar() |
c801d85f | 56 | { |
83624f79 | 57 | m_needParent = FALSE; // hmmm |
96fd301f | 58 | |
83624f79 | 59 | PreCreation( (wxWindow *) NULL, -1, wxDefaultPosition, wxDefaultSize, 0, "menu" ); |
c801d85f | 60 | |
83624f79 | 61 | m_menus.DeleteContents( TRUE ); |
96fd301f | 62 | |
83624f79 | 63 | m_menubar = gtk_menu_bar_new(); |
8bbe427f | 64 | |
3502e687 | 65 | m_widget = GTK_WIDGET(m_menubar); |
96fd301f | 66 | |
83624f79 | 67 | PostCreation(); |
c801d85f | 68 | |
83624f79 | 69 | Show( TRUE ); |
6de97a3b | 70 | } |
c801d85f KB |
71 | |
72 | void wxMenuBar::Append( wxMenu *menu, const wxString &title ) | |
73 | { | |
83624f79 RR |
74 | m_menus.Append( menu ); |
75 | menu->m_title = title; | |
96fd301f | 76 | |
83624f79 RR |
77 | int pos; |
78 | do | |
79 | { | |
80 | pos = menu->m_title.First( '&' ); | |
81 | if (pos != -1) menu->m_title.Remove( pos, 1 ); | |
82 | } while (pos != -1); | |
96fd301f | 83 | |
2b1c162e RR |
84 | menu->m_owner = gtk_menu_item_new_with_label( WXSTRINGCAST(menu->m_title) ); |
85 | gtk_widget_show( menu->m_owner ); | |
86 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu ); | |
96fd301f | 87 | |
2b1c162e | 88 | gtk_menu_bar_append( GTK_MENU_BAR(m_menubar), menu->m_owner ); |
6de97a3b | 89 | } |
96fd301f | 90 | |
716b7364 | 91 | static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString ) |
c801d85f | 92 | { |
83624f79 RR |
93 | if (menu->m_title == menuString) |
94 | { | |
95 | int res = menu->FindItem( itemString ); | |
96 | if (res != -1) return res; | |
97 | } | |
98 | ||
99 | wxNode *node = menu->m_items.First(); | |
100 | while (node) | |
101 | { | |
102 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
103 | if (item->IsSubMenu()) | |
104 | return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString); | |
2b1c162e | 105 | |
83624f79 RR |
106 | node = node->Next(); |
107 | } | |
108 | ||
109 | return -1; | |
6de97a3b | 110 | } |
c801d85f KB |
111 | |
112 | int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const | |
113 | { | |
83624f79 RR |
114 | wxNode *node = m_menus.First(); |
115 | while (node) | |
116 | { | |
117 | wxMenu *menu = (wxMenu*)node->Data(); | |
118 | int res = FindMenuItemRecursive( menu, menuString, itemString); | |
119 | if (res != -1) return res; | |
120 | node = node->Next(); | |
121 | } | |
122 | return -1; | |
6de97a3b | 123 | } |
c801d85f | 124 | |
83624f79 | 125 | /* Find a wxMenuItem using its id. Recurses down into sub-menus */ |
96fd301f | 126 | static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id) |
716b7364 | 127 | { |
83624f79 | 128 | wxMenuItem* result = menu->FindItem(id); |
716b7364 | 129 | |
83624f79 RR |
130 | wxNode *node = menu->m_items.First(); |
131 | while ( node && result == NULL ) | |
132 | { | |
133 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
134 | if (item->IsSubMenu()) | |
135 | { | |
136 | result = FindMenuItemByIdRecursive( item->GetSubMenu(), id ); | |
137 | } | |
138 | node = node->Next(); | |
139 | } | |
96fd301f | 140 | |
83624f79 | 141 | return result; |
6de97a3b | 142 | } |
716b7364 RR |
143 | |
144 | wxMenuItem* wxMenuBar::FindMenuItemById( int id ) const | |
145 | { | |
83624f79 RR |
146 | wxMenuItem* result = 0; |
147 | wxNode *node = m_menus.First(); | |
148 | while (node && result == 0) | |
149 | { | |
150 | wxMenu *menu = (wxMenu*)node->Data(); | |
151 | result = FindMenuItemByIdRecursive( menu, id ); | |
152 | node = node->Next(); | |
153 | } | |
154 | ||
155 | return result; | |
716b7364 RR |
156 | } |
157 | ||
54ff4a70 RR |
158 | void wxMenuBar::Check( int id, bool check ) |
159 | { | |
83624f79 RR |
160 | wxMenuItem* item = FindMenuItemById( id ); |
161 | if (item) item->Check(check); | |
6de97a3b | 162 | } |
54ff4a70 RR |
163 | |
164 | bool wxMenuBar::Checked( int id ) const | |
716b7364 | 165 | { |
83624f79 RR |
166 | wxMenuItem* item = FindMenuItemById( id ); |
167 | if (item) return item->IsChecked(); | |
168 | return FALSE; | |
6de97a3b | 169 | } |
716b7364 | 170 | |
54ff4a70 RR |
171 | void wxMenuBar::Enable( int id, bool enable ) |
172 | { | |
83624f79 RR |
173 | wxMenuItem* item = FindMenuItemById( id ); |
174 | if (item) item->Enable(enable); | |
6de97a3b | 175 | } |
54ff4a70 RR |
176 | |
177 | bool wxMenuBar::Enabled( int id ) const | |
716b7364 | 178 | { |
83624f79 RR |
179 | wxMenuItem* item = FindMenuItemById( id ); |
180 | if (item) return item->IsEnabled(); | |
342b6a2f | 181 | |
83624f79 | 182 | return FALSE; |
6de97a3b | 183 | } |
716b7364 | 184 | |
bbe0af5b RR |
185 | wxString wxMenuBar::GetLabel( int id ) const |
186 | { | |
187 | wxMenuItem* item = FindMenuItemById( id ); | |
2b1c162e | 188 | |
bbe0af5b | 189 | if (item) return item->GetText(); |
2b1c162e | 190 | |
342b6a2f | 191 | return wxString(""); |
bbe0af5b RR |
192 | } |
193 | ||
194 | void wxMenuBar::SetLabel( int id, const wxString &label ) | |
195 | { | |
196 | wxMenuItem* item = FindMenuItemById( id ); | |
2b1c162e | 197 | |
c330a2cf | 198 | if (item) item->SetText( label ); |
bbe0af5b RR |
199 | } |
200 | ||
2b1c162e | 201 | void wxMenuBar::EnableTop( int pos, bool flag ) |
bbe0af5b | 202 | { |
2b1c162e RR |
203 | wxNode *node = m_menus.Nth( pos ); |
204 | ||
205 | wxCHECK_RET( node, "menu not found" ); | |
206 | ||
207 | wxMenu* menu = (wxMenu*)node->Data(); | |
208 | ||
209 | if (menu->m_owner) gtk_widget_set_sensitive( menu->m_owner, flag ); | |
bbe0af5b RR |
210 | } |
211 | ||
2b1c162e | 212 | wxString wxMenuBar::GetLabelTop( int pos ) const |
bbe0af5b | 213 | { |
2b1c162e RR |
214 | wxNode *node = m_menus.Nth( pos ); |
215 | ||
216 | wxCHECK_MSG( node, "invalid", "menu not found" ); | |
217 | ||
218 | wxMenu* menu = (wxMenu*)node->Data(); | |
219 | ||
220 | return menu->GetTitle(); | |
bbe0af5b RR |
221 | } |
222 | ||
2b1c162e | 223 | void wxMenuBar::SetLabelTop( int pos, const wxString& label ) |
bbe0af5b | 224 | { |
2b1c162e RR |
225 | wxNode *node = m_menus.Nth( pos ); |
226 | ||
227 | wxCHECK_RET( node, "menu not found" ); | |
228 | ||
229 | wxMenu* menu = (wxMenu*)node->Data(); | |
230 | ||
231 | menu->SetTitle( label ); | |
bbe0af5b RR |
232 | } |
233 | ||
342b6a2f RR |
234 | void wxMenuBar::SetHelpString( int id, const wxString& helpString ) |
235 | { | |
236 | wxMenuItem* item = FindMenuItemById( id ); | |
237 | ||
238 | if (item) item->SetHelp( helpString ); | |
239 | } | |
240 | ||
241 | wxString wxMenuBar::GetHelpString( int id ) const | |
242 | { | |
243 | wxMenuItem* item = FindMenuItemById( id ); | |
244 | ||
245 | if (item) | |
246 | return item->GetHelp(); | |
247 | else | |
248 | return wxString(""); | |
249 | } | |
250 | ||
c801d85f | 251 | //----------------------------------------------------------------------------- |
cf7a7e13 | 252 | // "activate" |
c801d85f KB |
253 | //----------------------------------------------------------------------------- |
254 | ||
6de97a3b | 255 | static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu ) |
c801d85f | 256 | { |
83624f79 | 257 | int id = menu->FindMenuIdByMenuItem(widget); |
96fd301f | 258 | |
83624f79 RR |
259 | /* should find it for normal (not popup) menu */ |
260 | wxASSERT( (id != -1) || (menu->GetInvokingWindow() != NULL) ); | |
96fd301f | 261 | |
83624f79 | 262 | if (!menu->IsEnabled(id)) return; |
96fd301f | 263 | |
2d17d68f RR |
264 | wxMenuItem* item = menu->FindItem( id ); |
265 | wxCHECK_RET( item, "error in menu item callback" ); | |
266 | ||
267 | if (item->m_isCheckMenu) | |
268 | { | |
269 | if (item->m_isChecked == item->IsChecked()) | |
270 | { | |
271 | /* the menu item has been checked by calling wxMenuItem->Check() */ | |
272 | return; | |
273 | } | |
274 | else | |
275 | { | |
276 | /* the user pressed on the menu item -> report */ | |
f5abe911 | 277 | item->m_isChecked = item->IsChecked(); /* make consistent again */ |
2d17d68f RR |
278 | } |
279 | } | |
280 | ||
83624f79 RR |
281 | wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, id ); |
282 | event.SetEventObject( menu ); | |
283 | event.SetInt(id ); | |
8bbe427f | 284 | |
83624f79 RR |
285 | if (menu->m_callback) |
286 | { | |
287 | (void) (*(menu->m_callback)) (*menu, event); | |
288 | return; | |
289 | } | |
cf7a7e13 | 290 | |
83624f79 | 291 | if (menu->GetEventHandler()->ProcessEvent(event)) return; |
cf7a7e13 | 292 | |
83624f79 RR |
293 | wxWindow *win = menu->GetInvokingWindow(); |
294 | if (win) win->GetEventHandler()->ProcessEvent( event ); | |
cf7a7e13 RR |
295 | } |
296 | ||
297 | //----------------------------------------------------------------------------- | |
298 | // "select" | |
299 | //----------------------------------------------------------------------------- | |
300 | ||
301 | static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu ) | |
302 | { | |
83624f79 RR |
303 | int id = menu->FindMenuIdByMenuItem(widget); |
304 | ||
305 | wxASSERT( id != -1 ); // should find it! | |
cf7a7e13 | 306 | |
83624f79 | 307 | if (!menu->IsEnabled(id)) return; |
cf7a7e13 | 308 | |
342b6a2f | 309 | wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id ); |
83624f79 | 310 | event.SetEventObject( menu ); |
cf7a7e13 | 311 | |
83624f79 | 312 | /* wxMSW doesn't call callback here either |
8bbe427f | 313 | |
83624f79 RR |
314 | if (menu->m_callback) |
315 | { | |
316 | (void) (*(menu->m_callback)) (*menu, event); | |
317 | return; | |
318 | } | |
13439807 | 319 | */ |
6de97a3b | 320 | |
83624f79 | 321 | if (menu->GetEventHandler()->ProcessEvent(event)) return; |
6de97a3b | 322 | |
83624f79 RR |
323 | wxWindow *win = menu->GetInvokingWindow(); |
324 | if (win) win->GetEventHandler()->ProcessEvent( event ); | |
6de97a3b | 325 | } |
c801d85f | 326 | |
cd743a6f RR |
327 | //----------------------------------------------------------------------------- |
328 | // "deselect" | |
329 | //----------------------------------------------------------------------------- | |
330 | ||
331 | static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu ) | |
332 | { | |
333 | int id = menu->FindMenuIdByMenuItem(widget); | |
334 | ||
335 | wxASSERT( id != -1 ); // should find it! | |
336 | ||
337 | if (!menu->IsEnabled(id)) return; | |
338 | ||
339 | wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 ); | |
340 | event.SetEventObject( menu ); | |
341 | ||
342 | if (menu->GetEventHandler()->ProcessEvent(event)) return; | |
343 | ||
344 | wxWindow *win = menu->GetInvokingWindow(); | |
345 | if (win) win->GetEventHandler()->ProcessEvent( event ); | |
346 | } | |
347 | ||
cf7a7e13 | 348 | //----------------------------------------------------------------------------- |
db1b4961 | 349 | // wxMenuItem |
cf7a7e13 RR |
350 | //----------------------------------------------------------------------------- |
351 | ||
c801d85f | 352 | IMPLEMENT_DYNAMIC_CLASS(wxMenuItem,wxObject) |
96fd301f VZ |
353 | |
354 | wxMenuItem::wxMenuItem() | |
c801d85f | 355 | { |
83624f79 RR |
356 | m_id = ID_SEPARATOR; |
357 | m_isCheckMenu = FALSE; | |
358 | m_isChecked = FALSE; | |
359 | m_isEnabled = TRUE; | |
360 | m_subMenu = (wxMenu *) NULL; | |
361 | m_menuItem = (GtkWidget *) NULL; | |
6de97a3b | 362 | } |
c801d85f | 363 | |
83624f79 RR |
364 | /* it's valid for this function to be called even if m_menuItem == NULL */ |
365 | void wxMenuItem::SetName( const wxString& str ) | |
716b7364 | 366 | { |
83624f79 RR |
367 | m_text = ""; |
368 | for ( const char *pc = str; *pc != '\0'; pc++ ) | |
369 | { | |
370 | if (*pc == '&') pc++; /* skip it */ | |
371 | m_text << *pc; | |
372 | } | |
96fd301f | 373 | |
83624f79 RR |
374 | if (m_menuItem) |
375 | { | |
376 | GtkLabel *label = GTK_LABEL( GTK_BIN(m_menuItem)->child ); | |
377 | gtk_label_set( label, m_text.c_str()); | |
378 | } | |
716b7364 RR |
379 | } |
380 | ||
96fd301f | 381 | void wxMenuItem::Check( bool check ) |
716b7364 | 382 | { |
83624f79 | 383 | wxCHECK_RET( m_menuItem, "invalid menu item" ); |
db1b4961 | 384 | |
83624f79 | 385 | wxCHECK_RET( IsCheckable(), "Can't check uncheckable item!" ) |
96fd301f | 386 | |
2d17d68f RR |
387 | if (check == m_isChecked) return; |
388 | ||
83624f79 RR |
389 | m_isChecked = check; |
390 | gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check ); | |
716b7364 RR |
391 | } |
392 | ||
8bbe427f VZ |
393 | void wxMenuItem::Enable( bool enable ) |
394 | { | |
83624f79 | 395 | wxCHECK_RET( m_menuItem, "invalid menu item" ); |
db1b4961 | 396 | |
83624f79 RR |
397 | gtk_widget_set_sensitive( m_menuItem, enable ); |
398 | m_isEnabled = enable; | |
a9c96bcc RR |
399 | } |
400 | ||
96fd301f | 401 | bool wxMenuItem::IsChecked() const |
716b7364 | 402 | { |
83624f79 | 403 | wxCHECK_MSG( m_menuItem, FALSE, "invalid menu item" ); |
db1b4961 | 404 | |
83624f79 | 405 | wxCHECK( IsCheckable(), FALSE ); // can't get state of uncheckable item! |
96fd301f | 406 | |
83624f79 | 407 | bool bIsChecked = ((GtkCheckMenuItem*)m_menuItem)->active != 0; |
96fd301f | 408 | |
83624f79 | 409 | return bIsChecked; |
716b7364 RR |
410 | } |
411 | ||
db1b4961 | 412 | //----------------------------------------------------------------------------- |
83624f79 | 413 | // wxMenu |
db1b4961 RR |
414 | //----------------------------------------------------------------------------- |
415 | ||
c801d85f KB |
416 | IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler) |
417 | ||
6de97a3b | 418 | wxMenu::wxMenu( const wxString& title, const wxFunction func ) |
c801d85f | 419 | { |
83624f79 RR |
420 | m_title = title; |
421 | m_items.DeleteContents( TRUE ); | |
422 | m_invokingWindow = (wxWindow *) NULL; | |
423 | m_menu = gtk_menu_new(); // Do not show! | |
8bbe427f | 424 | |
83624f79 RR |
425 | m_callback = func; |
426 | m_eventHandler = this; | |
427 | m_clientData = (void*) NULL; | |
8bbe427f | 428 | |
83624f79 RR |
429 | if (m_title.IsNull()) m_title = ""; |
430 | if (m_title != "") | |
431 | { | |
432 | Append(-2, m_title); | |
433 | AppendSeparator(); | |
434 | } | |
2b1c162e RR |
435 | |
436 | m_owner = (GtkWidget*) NULL; | |
6de97a3b | 437 | } |
c801d85f | 438 | |
c2dd8380 GL |
439 | void wxMenu::SetTitle( const wxString& title ) |
440 | { | |
83624f79 RR |
441 | /* Waiting for something better. */ |
442 | m_title = title; | |
c2dd8380 GL |
443 | } |
444 | ||
445 | const wxString wxMenu::GetTitle() const | |
446 | { | |
83624f79 | 447 | return m_title; |
c2dd8380 GL |
448 | } |
449 | ||
96fd301f | 450 | void wxMenu::AppendSeparator() |
c801d85f | 451 | { |
83624f79 RR |
452 | wxMenuItem *mitem = new wxMenuItem(); |
453 | mitem->SetId(ID_SEPARATOR); | |
96fd301f | 454 | |
83624f79 RR |
455 | GtkWidget *menuItem = gtk_menu_item_new(); |
456 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); | |
457 | gtk_widget_show( menuItem ); | |
458 | mitem->SetMenuItem(menuItem); | |
459 | m_items.Append( mitem ); | |
6de97a3b | 460 | } |
c801d85f | 461 | |
debe6624 | 462 | void wxMenu::Append( int id, const wxString &item, const wxString &helpStr, bool checkable ) |
c801d85f | 463 | { |
83624f79 RR |
464 | wxMenuItem *mitem = new wxMenuItem(); |
465 | mitem->SetId(id); | |
466 | mitem->SetText(item); | |
467 | mitem->SetHelp(helpStr); | |
468 | mitem->SetCheckable(checkable); | |
469 | const char *text = mitem->GetText(); | |
470 | GtkWidget *menuItem = checkable ? gtk_check_menu_item_new_with_label(text) | |
471 | : gtk_menu_item_new_with_label(text); | |
8bbe427f | 472 | |
83624f79 | 473 | mitem->SetMenuItem(menuItem); |
96fd301f | 474 | |
83624f79 RR |
475 | gtk_signal_connect( GTK_OBJECT(menuItem), "activate", |
476 | GTK_SIGNAL_FUNC(gtk_menu_clicked_callback), | |
477 | (gpointer*)this ); | |
96fd301f | 478 | |
83624f79 RR |
479 | gtk_signal_connect( GTK_OBJECT(menuItem), "select", |
480 | GTK_SIGNAL_FUNC(gtk_menu_hilight_callback), | |
481 | (gpointer*)this ); | |
cf7a7e13 | 482 | |
cd743a6f RR |
483 | gtk_signal_connect( GTK_OBJECT(menuItem), "deselect", |
484 | GTK_SIGNAL_FUNC(gtk_menu_nolight_callback), | |
485 | (gpointer*)this ); | |
486 | ||
83624f79 RR |
487 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); |
488 | gtk_widget_show( menuItem ); | |
489 | m_items.Append( mitem ); | |
6de97a3b | 490 | } |
c801d85f | 491 | |
96fd301f | 492 | void wxMenu::Append( int id, const wxString &text, wxMenu *subMenu, const wxString &helpStr ) |
c801d85f | 493 | { |
83624f79 RR |
494 | wxMenuItem *mitem = new wxMenuItem(); |
495 | mitem->SetId(id); | |
496 | mitem->SetText(text); | |
96fd301f | 497 | |
83624f79 RR |
498 | GtkWidget *menuItem = gtk_menu_item_new_with_label(mitem->GetText()); |
499 | mitem->SetHelp(helpStr); | |
500 | mitem->SetMenuItem(menuItem); | |
501 | mitem->SetSubMenu(subMenu); | |
96fd301f | 502 | |
cd743a6f RR |
503 | gtk_signal_connect( GTK_OBJECT(menuItem), "select", |
504 | GTK_SIGNAL_FUNC(gtk_menu_hilight_callback), | |
505 | (gpointer*)this ); | |
506 | ||
507 | gtk_signal_connect( GTK_OBJECT(menuItem), "deselect", | |
508 | GTK_SIGNAL_FUNC(gtk_menu_nolight_callback), | |
509 | (gpointer*)this ); | |
510 | ||
83624f79 RR |
511 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), subMenu->m_menu ); |
512 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); | |
513 | gtk_widget_show( menuItem ); | |
514 | m_items.Append( mitem ); | |
6de97a3b | 515 | } |
c801d85f KB |
516 | |
517 | int wxMenu::FindItem( const wxString itemString ) const | |
518 | { | |
83624f79 | 519 | wxString s( itemString ); |
96fd301f | 520 | |
83624f79 RR |
521 | int pos; |
522 | do | |
523 | { | |
524 | pos = s.First( '&' ); | |
525 | if (pos != -1) s.Remove( pos, 1 ); | |
526 | } while (pos != -1); | |
96fd301f | 527 | |
83624f79 RR |
528 | wxNode *node = m_items.First(); |
529 | while (node) | |
530 | { | |
531 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
532 | if (item->GetText() == s) | |
533 | { | |
534 | return item->GetId(); | |
535 | } | |
536 | node = node->Next(); | |
537 | } | |
96fd301f | 538 | |
83624f79 | 539 | return -1; |
6de97a3b | 540 | } |
c801d85f | 541 | |
96fd301f | 542 | void wxMenu::Enable( int id, bool enable ) |
716b7364 | 543 | { |
83624f79 RR |
544 | wxMenuItem *item = FindItem(id); |
545 | if (item) | |
546 | { | |
547 | item->Enable(enable); | |
548 | } | |
6de97a3b | 549 | } |
716b7364 | 550 | |
96fd301f | 551 | bool wxMenu::IsEnabled( int id ) const |
e2414cbe | 552 | { |
83624f79 RR |
553 | wxMenuItem *item = FindItem(id); |
554 | if (item) | |
555 | { | |
556 | return item->IsEnabled(); | |
557 | } | |
558 | else | |
559 | { | |
560 | return FALSE; | |
561 | } | |
6de97a3b | 562 | } |
e2414cbe | 563 | |
96fd301f | 564 | void wxMenu::Check( int id, bool enable ) |
c801d85f | 565 | { |
83624f79 RR |
566 | wxMenuItem *item = FindItem(id); |
567 | if (item) | |
568 | { | |
569 | item->Check(enable); | |
570 | } | |
6de97a3b | 571 | } |
c801d85f | 572 | |
96fd301f | 573 | bool wxMenu::IsChecked( int id ) const |
c801d85f | 574 | { |
83624f79 RR |
575 | wxMenuItem *item = FindItem(id); |
576 | if (item) | |
577 | { | |
578 | return item->IsChecked(); | |
579 | } | |
580 | else | |
581 | { | |
582 | return FALSE; | |
583 | } | |
6de97a3b | 584 | } |
c801d85f | 585 | |
debe6624 | 586 | void wxMenu::SetLabel( int id, const wxString &label ) |
c801d85f | 587 | { |
83624f79 RR |
588 | wxMenuItem *item = FindItem(id); |
589 | if (item) | |
590 | { | |
591 | item->SetText(label); | |
592 | } | |
6de97a3b | 593 | } |
96fd301f | 594 | |
c33c4050 RR |
595 | wxString wxMenu::GetLabel( int id ) const |
596 | { | |
83624f79 RR |
597 | wxMenuItem *item = FindItem(id); |
598 | if (item) | |
599 | { | |
600 | return item->GetText(); | |
601 | } | |
602 | else | |
603 | { | |
604 | return ""; | |
605 | } | |
c33c4050 RR |
606 | } |
607 | ||
608 | void wxMenu::SetHelpString( int id, const wxString& helpString ) | |
609 | { | |
83624f79 RR |
610 | wxMenuItem *item = FindItem(id); |
611 | if (item) item->SetHelp( helpString ); | |
c33c4050 RR |
612 | } |
613 | ||
614 | wxString wxMenu::GetHelpString( int id ) const | |
615 | { | |
83624f79 RR |
616 | wxMenuItem *item = FindItem(id); |
617 | if (item) | |
618 | { | |
619 | return item->GetHelp(); | |
620 | } | |
621 | else | |
622 | { | |
623 | return ""; | |
624 | } | |
c33c4050 RR |
625 | } |
626 | ||
96fd301f VZ |
627 | int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const |
628 | { | |
83624f79 RR |
629 | wxNode *node = m_items.First(); |
630 | while (node) | |
631 | { | |
632 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
633 | if (item->GetMenuItem() == menuItem) | |
634 | return item->GetId(); | |
635 | node = node->Next(); | |
636 | } | |
96fd301f | 637 | |
83624f79 | 638 | return -1; |
6de97a3b | 639 | } |
c801d85f | 640 | |
96fd301f | 641 | wxMenuItem *wxMenu::FindItem(int id) const |
c801d85f | 642 | { |
83624f79 RR |
643 | wxNode *node = m_items.First(); |
644 | while (node) | |
645 | { | |
646 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
647 | if (item->GetId() == id) | |
648 | { | |
649 | return item; | |
650 | } | |
651 | node = node->Next(); | |
652 | } | |
96fd301f | 653 | |
83624f79 RR |
654 | /* Not finding anything here can be correct |
655 | * when search the entire menu system for | |
656 | * an entry -> no error message. */ | |
8bbe427f | 657 | |
83624f79 | 658 | return (wxMenuItem *) NULL; |
96fd301f | 659 | } |
c801d85f KB |
660 | |
661 | void wxMenu::SetInvokingWindow( wxWindow *win ) | |
662 | { | |
83624f79 | 663 | m_invokingWindow = win; |
6de97a3b | 664 | } |
c801d85f | 665 | |
96fd301f | 666 | wxWindow *wxMenu::GetInvokingWindow() |
c801d85f | 667 | { |
83624f79 | 668 | return m_invokingWindow; |
6de97a3b | 669 | } |
c801d85f | 670 | |
631f1bfe JS |
671 | // Update a menu and all submenus recursively. |
672 | // source is the object that has the update event handlers | |
673 | // defined for it. If NULL, the menu or associated window | |
674 | // will be used. | |
675 | void wxMenu::UpdateUI(wxEvtHandler* source) | |
676 | { | |
677 | if (!source && GetInvokingWindow()) | |
678 | source = GetInvokingWindow()->GetEventHandler(); | |
679 | if (!source) | |
680 | source = GetEventHandler(); | |
681 | if (!source) | |
682 | source = this; | |
683 | ||
684 | wxNode* node = GetItems().First(); | |
685 | while (node) | |
686 | { | |
687 | wxMenuItem* item = (wxMenuItem*) node->Data(); | |
688 | if ( !item->IsSeparator() ) | |
689 | { | |
690 | wxWindowID id = item->GetId(); | |
691 | wxUpdateUIEvent event(id); | |
692 | event.SetEventObject( source ); | |
693 | ||
694 | if (source->ProcessEvent(event)) | |
695 | { | |
696 | if (event.GetSetText()) | |
697 | SetLabel(id, event.GetText()); | |
698 | if (event.GetSetChecked()) | |
699 | Check(id, event.GetChecked()); | |
700 | if (event.GetSetEnabled()) | |
701 | Enable(id, event.GetEnabled()); | |
702 | } | |
703 | ||
704 | if (item->GetSubMenu()) | |
705 | item->GetSubMenu()->UpdateUI(source); | |
706 | } | |
707 | node = node->Next(); | |
708 | } | |
709 | } | |
710 | ||
c801d85f | 711 |