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