]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/menu.cpp
Someone forgot wxT()
[wxWidgets.git] / src / gtk / menu.cpp
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/log.h"
16 #include "wx/intl.h"
17 #include "wx/app.h"
18 #include "wx/menu.h"
19
20 #if wxUSE_ACCEL
21 #include "wx/accel.h"
22 #endif // wxUSE_ACCEL
23
24 #include "gdk/gdk.h"
25 #include "gtk/gtk.h"
26
27 //-----------------------------------------------------------------------------
28 // idle system
29 //-----------------------------------------------------------------------------
30
31 extern void wxapp_install_idle_handler();
32 extern bool g_isIdle;
33
34 //-----------------------------------------------------------------------------
35 // wxMenuBar
36 //-----------------------------------------------------------------------------
37
38 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
39
40 wxMenuBar::wxMenuBar( long style )
41 {
42 /* the parent window is known after wxFrame::SetMenu() */
43 m_needParent = FALSE;
44 m_style = style;
45 m_invokingWindow = (wxWindow*) NULL;
46
47 if (!PreCreation( (wxWindow*) NULL, wxDefaultPosition, wxDefaultSize ) ||
48 !CreateBase( (wxWindow*) NULL, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, wxT("menubar") ))
49 {
50 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
51 return;
52 }
53
54 m_menus.DeleteContents( TRUE );
55
56 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
57 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
58 m_accel = gtk_accel_group_new();
59 m_factory = gtk_item_factory_new( GTK_TYPE_MENU_BAR, "<main>", m_accel );
60 m_menubar = gtk_item_factory_get_widget( m_factory, "<main>" );
61 #else
62 m_menubar = gtk_menu_bar_new();
63 #endif
64
65 if (style & wxMB_DOCKABLE)
66 {
67 m_widget = gtk_handle_box_new();
68 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_menubar) );
69 gtk_widget_show( GTK_WIDGET(m_menubar) );
70 }
71 else
72 {
73 m_widget = GTK_WIDGET(m_menubar);
74 }
75
76 PostCreation();
77 }
78
79 wxMenuBar::wxMenuBar()
80 {
81 /* the parent window is known after wxFrame::SetMenu() */
82 m_needParent = FALSE;
83 m_style = 0;
84 m_invokingWindow = (wxWindow*) NULL;
85
86 if (!PreCreation( (wxWindow*) NULL, wxDefaultPosition, wxDefaultSize ) ||
87 !CreateBase( (wxWindow*) NULL, -1, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, wxT("menubar") ))
88 {
89 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
90 return;
91 }
92
93 m_menus.DeleteContents( TRUE );
94
95 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
96 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
97 m_accel = gtk_accel_group_new();
98 m_factory = gtk_item_factory_new( GTK_TYPE_MENU_BAR, "<main>", m_accel );
99 m_menubar = gtk_item_factory_get_widget( m_factory, "<main>" );
100 #else
101 m_menubar = gtk_menu_bar_new();
102 #endif
103
104 m_widget = GTK_WIDGET(m_menubar);
105
106 PostCreation();
107 }
108
109 wxMenuBar::~wxMenuBar()
110 {
111 // gtk_object_unref( GTK_OBJECT(m_factory) ); why not ?
112 }
113
114 static void wxMenubarUnsetInvokingWindow( wxMenu *menu, wxWindow *win )
115 {
116 menu->SetInvokingWindow( (wxWindow*) NULL );
117
118 #if (GTK_MINOR_VERSION > 0)
119 wxWindow *top_frame = win;
120 while (top_frame->GetParent() && !(top_frame->GetParent()->m_isFrame))
121 top_frame = top_frame->GetParent();
122
123 /* support for native hot keys */
124 gtk_accel_group_detach( menu->m_accel, GTK_OBJECT(top_frame->m_widget) );
125 #endif
126
127 wxNode *node = menu->GetItems().First();
128 while (node)
129 {
130 wxMenuItem *menuitem = (wxMenuItem*)node->Data();
131 if (menuitem->IsSubMenu())
132 wxMenubarUnsetInvokingWindow( menuitem->GetSubMenu(), win );
133 node = node->Next();
134 }
135 }
136
137 static void wxMenubarSetInvokingWindow( wxMenu *menu, wxWindow *win )
138 {
139 menu->SetInvokingWindow( win );
140
141 #if (GTK_MINOR_VERSION > 0)
142 wxWindow *top_frame = win;
143 while (top_frame->GetParent() && !(top_frame->GetParent()->m_isFrame))
144 top_frame = top_frame->GetParent();
145
146 /* support for native hot keys */
147 gtk_accel_group_attach( menu->m_accel, GTK_OBJECT(top_frame->m_widget) );
148 #endif
149
150 wxNode *node = menu->GetItems().First();
151 while (node)
152 {
153 wxMenuItem *menuitem = (wxMenuItem*)node->Data();
154 if (menuitem->IsSubMenu())
155 wxMenubarSetInvokingWindow( menuitem->GetSubMenu(), win );
156 node = node->Next();
157 }
158 }
159
160 void wxMenuBar::SetInvokingWindow( wxWindow *win )
161 {
162 m_invokingWindow = win;
163 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
164 wxWindow *top_frame = win;
165 while (top_frame->GetParent() && !(top_frame->GetParent()->m_isFrame))
166 top_frame = top_frame->GetParent();
167
168 /* support for native key accelerators indicated by underscroes */
169 gtk_accel_group_attach( m_accel, GTK_OBJECT(top_frame->m_widget) );
170 #endif
171
172 wxNode *node = m_menus.First();
173 while (node)
174 {
175 wxMenu *menu = (wxMenu*)node->Data();
176 wxMenubarSetInvokingWindow( menu, win );
177 node = node->Next();
178 }
179 }
180
181 void wxMenuBar::UnsetInvokingWindow( wxWindow *win )
182 {
183 m_invokingWindow = (wxWindow*) NULL;
184 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
185 wxWindow *top_frame = win;
186 while (top_frame->GetParent() && !(top_frame->GetParent()->m_isFrame))
187 top_frame = top_frame->GetParent();
188
189 /* support for native key accelerators indicated by underscroes */
190 gtk_accel_group_detach( m_accel, GTK_OBJECT(top_frame->m_widget) );
191 #endif
192
193 wxNode *node = m_menus.First();
194 while (node)
195 {
196 wxMenu *menu = (wxMenu*)node->Data();
197 wxMenubarUnsetInvokingWindow( menu, win );
198 node = node->Next();
199 }
200 }
201
202 bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
203 {
204 m_menus.Append( menu );
205
206 const wxChar *pc;
207
208 /* GTK 1.2 wants to have "_" instead of "&" for accelerators */
209 wxString str;
210 for ( pc = title; *pc != wxT('\0'); pc++ )
211 {
212 if (*pc == wxT('&'))
213 {
214 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
215 str << wxT('_');
216 }
217 else if (*pc == wxT('/'))
218 {
219 str << wxT('\\');
220 #endif
221 }
222 else
223 {
224 #if __WXGTK12__
225 if ( *pc == wxT('_') )
226 {
227 // underscores must be doubled to prevent them from being
228 // interpreted as accelerator character prefix by GTK
229 str << *pc;
230 }
231 #endif // GTK+ 1.2
232
233 str << *pc;
234 }
235 }
236
237 /* this doesn't have much effect right now */
238 menu->SetTitle( str );
239
240 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
241 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
242
243 /* local buffer in multibyte form */
244 wxString buf;
245 buf << wxT('/') << str.c_str();
246
247 char *cbuf = new char[buf.Length()+1];
248 strcpy(cbuf, buf.mbc_str());
249
250 GtkItemFactoryEntry entry;
251 entry.path = (gchar *)cbuf; // const_cast
252 entry.accelerator = (gchar*) NULL;
253 entry.callback = (GtkItemFactoryCallback) NULL;
254 entry.callback_action = 0;
255 entry.item_type = "<Branch>";
256
257 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
258 /* in order to get the pointer to the item we need the item text _without_ underscores */
259 wxString tmp = wxT("<main>/");
260 for ( pc = str; *pc != wxT('\0'); pc++ )
261 {
262 // contrary to the common sense, we must throw out _all_ underscores,
263 // (i.e. "Hello__World" => "HelloWorld" and not "Hello_World" as we
264 // might naively think). IMHO it's a bug in GTK+ (VZ)
265 while (*pc == wxT('_'))
266 pc++;
267 tmp << *pc;
268 }
269 menu->m_owner = gtk_item_factory_get_item( m_factory, tmp.mb_str() );
270 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu );
271 delete [] cbuf;
272 #else
273
274 menu->m_owner = gtk_menu_item_new_with_label( str.mb_str() );
275 gtk_widget_show( menu->m_owner );
276 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu );
277
278 gtk_menu_bar_append( GTK_MENU_BAR(m_menubar), menu->m_owner );
279
280 #endif
281
282 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
283 // adding menu later on.
284 if (m_invokingWindow)
285 wxMenubarSetInvokingWindow( menu, m_invokingWindow );
286
287 return TRUE;
288 }
289
290 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
291 {
292 if ( !wxMenuBarBase::Insert(pos, menu, title) )
293 return FALSE;
294
295 wxFAIL_MSG(wxT("TODO"));
296
297 return FALSE;
298 }
299
300 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
301 {
302 if ( !wxMenuBarBase::Replace(pos, menu, title) )
303 return FALSE;
304
305 wxFAIL_MSG(wxT("TODO"));
306
307 return NULL;
308 }
309
310 wxMenu *wxMenuBar::Remove(size_t pos)
311 {
312 if ( !wxMenuBarBase::Remove(pos) )
313 return FALSE;
314
315 wxFAIL_MSG(wxT("TODO"));
316
317 return NULL;
318 }
319
320 static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString )
321 {
322 if (menu->GetTitle() == menuString)
323 {
324 int res = menu->FindItem( itemString );
325 if (res != wxNOT_FOUND)
326 return res;
327 }
328
329 wxNode *node = ((wxMenu *)menu)->GetItems().First(); // const_cast
330 while (node)
331 {
332 wxMenuItem *item = (wxMenuItem*)node->Data();
333 if (item->IsSubMenu())
334 return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString);
335
336 node = node->Next();
337 }
338
339 return wxNOT_FOUND;
340 }
341
342 int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const
343 {
344 wxNode *node = m_menus.First();
345 while (node)
346 {
347 wxMenu *menu = (wxMenu*)node->Data();
348 int res = FindMenuItemRecursive( menu, menuString, itemString);
349 if (res != -1) return res;
350 node = node->Next();
351 }
352 return -1;
353 }
354
355 // Find a wxMenuItem using its id. Recurses down into sub-menus
356 static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id)
357 {
358 wxMenuItem* result = menu->FindItem(id);
359
360 wxNode *node = ((wxMenu *)menu)->GetItems().First(); // const_cast
361 while ( node && result == NULL )
362 {
363 wxMenuItem *item = (wxMenuItem*)node->Data();
364 if (item->IsSubMenu())
365 {
366 result = FindMenuItemByIdRecursive( item->GetSubMenu(), id );
367 }
368 node = node->Next();
369 }
370
371 return result;
372 }
373
374 wxMenuItem* wxMenuBar::FindItem( int id, wxMenu **menuForItem ) const
375 {
376 wxMenuItem* result = 0;
377 wxNode *node = m_menus.First();
378 while (node && result == 0)
379 {
380 wxMenu *menu = (wxMenu*)node->Data();
381 result = FindMenuItemByIdRecursive( menu, id );
382 node = node->Next();
383 }
384
385 if ( menuForItem )
386 {
387 *menuForItem = result ? result->GetMenu() : (wxMenu *)NULL;
388 }
389
390 return result;
391 }
392
393 void wxMenuBar::EnableTop( size_t pos, bool flag )
394 {
395 wxMenuList::Node *node = m_menus.Item( pos );
396
397 wxCHECK_RET( node, wxT("menu not found") );
398
399 wxMenu* menu = node->GetData();
400
401 if (menu->m_owner)
402 gtk_widget_set_sensitive( menu->m_owner, flag );
403 }
404
405 wxString wxMenuBar::GetLabelTop( size_t pos ) const
406 {
407 wxMenuList::Node *node = m_menus.Item( pos );
408
409 wxCHECK_MSG( node, wxT("invalid"), wxT("menu not found") );
410
411 wxMenu* menu = node->GetData();
412
413 return menu->GetTitle();
414 }
415
416 void wxMenuBar::SetLabelTop( size_t pos, const wxString& label )
417 {
418 wxMenuList::Node *node = m_menus.Item( pos );
419
420 wxCHECK_RET( node, wxT("menu not found") );
421
422 wxMenu* menu = node->GetData();
423
424 menu->SetTitle( label );
425 }
426
427 //-----------------------------------------------------------------------------
428 // "activate"
429 //-----------------------------------------------------------------------------
430
431 static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu )
432 {
433 if (g_isIdle) wxapp_install_idle_handler();
434
435 int id = menu->FindMenuIdByMenuItem(widget);
436
437 /* should find it for normal (not popup) menu */
438 wxASSERT( (id != -1) || (menu->GetInvokingWindow() != NULL) );
439
440 if (!menu->IsEnabled(id))
441 return;
442
443 wxMenuItem* item = menu->FindItem( id );
444 wxCHECK_RET( item, wxT("error in menu item callback") );
445
446 if (item->IsCheckable())
447 {
448 bool isReallyChecked = item->IsChecked();
449 if ( item->wxMenuItemBase::IsChecked() == isReallyChecked )
450 {
451 /* the menu item has been checked by calling wxMenuItem->Check() */
452 return;
453 }
454 else
455 {
456 /* the user pressed on the menu item -> report and make consistent
457 * again */
458 item->wxMenuItemBase::Check(isReallyChecked);
459 }
460 }
461
462 wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, id );
463 event.SetEventObject( menu );
464 event.SetInt(id );
465
466 if (menu->GetCallback())
467 {
468 (void) (*(menu->GetCallback())) (*menu, event);
469 return;
470 }
471
472 if (menu->GetEventHandler()->ProcessEvent(event))
473 return;
474
475 wxWindow *win = menu->GetInvokingWindow();
476 if (win)
477 win->GetEventHandler()->ProcessEvent( event );
478 }
479
480 //-----------------------------------------------------------------------------
481 // "select"
482 //-----------------------------------------------------------------------------
483
484 static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu )
485 {
486 if (g_isIdle) wxapp_install_idle_handler();
487
488 int id = menu->FindMenuIdByMenuItem(widget);
489
490 wxASSERT( id != -1 ); // should find it!
491
492 if (!menu->IsEnabled(id))
493 return;
494
495 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id );
496 event.SetEventObject( menu );
497
498 if (menu->GetEventHandler()->ProcessEvent(event))
499 return;
500
501 wxWindow *win = menu->GetInvokingWindow();
502 if (win) win->GetEventHandler()->ProcessEvent( event );
503 }
504
505 //-----------------------------------------------------------------------------
506 // "deselect"
507 //-----------------------------------------------------------------------------
508
509 static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu )
510 {
511 if (g_isIdle) wxapp_install_idle_handler();
512
513 int id = menu->FindMenuIdByMenuItem(widget);
514
515 wxASSERT( id != -1 ); // should find it!
516
517 if (!menu->IsEnabled(id))
518 return;
519
520 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 );
521 event.SetEventObject( menu );
522
523 if (menu->GetEventHandler()->ProcessEvent(event))
524 return;
525
526 wxWindow *win = menu->GetInvokingWindow();
527 if (win)
528 win->GetEventHandler()->ProcessEvent( event );
529 }
530
531 //-----------------------------------------------------------------------------
532 // wxMenuItem
533 //-----------------------------------------------------------------------------
534
535 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxMenuItemBase)
536
537 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
538 int id,
539 const wxString& name,
540 const wxString& help,
541 bool isCheckable,
542 wxMenu *subMenu)
543 {
544 return new wxMenuItem(parentMenu, id, name, help, isCheckable, subMenu);
545 }
546
547 wxMenuItem::wxMenuItem(wxMenu *parentMenu,
548 int id,
549 const wxString& text,
550 const wxString& help,
551 bool isCheckable,
552 wxMenu *subMenu)
553 {
554 m_id = id;
555 m_isCheckable = isCheckable;
556 m_isChecked = FALSE;
557 m_isEnabled = TRUE;
558 m_subMenu = subMenu;
559 m_parentMenu = parentMenu;
560 m_help = help;
561
562 m_menuItem = (GtkWidget *) NULL;
563
564 // call it after initializing m_menuItem to NULL
565 DoSetText(text);
566 }
567
568 wxMenuItem::~wxMenuItem()
569 {
570 // don't delete menu items, the menus take care of that
571 }
572
573 // it's valid for this function to be called even if m_menuItem == NULL
574 void wxMenuItem::DoSetText( const wxString& str )
575 {
576 /* '\t' is the deliminator indicating a hot key */
577 m_text.Empty();
578 const wxChar *pc = str;
579 for (; (*pc != wxT('\0')) && (*pc != wxT('\t')); pc++ )
580 {
581 if (*pc == wxT('&'))
582 {
583 #if (GTK_MINOR_VERSION > 0)
584 m_text << wxT('_');
585 }
586 else if ( *pc == wxT('_') ) // escape underscores
587 {
588 m_text << wxT("__");
589 }
590 else if (*pc == wxT('/')) /* we have to filter out slashes ... */
591 {
592 m_text << wxT('\\'); /* ... and replace them with back slashes */
593 #endif
594 }
595 else
596 m_text << *pc;
597 }
598
599 /* only GTK 1.2 knows about hot keys */
600 m_hotKey = wxT("");
601 #if (GTK_MINOR_VERSION > 0)
602 if(*pc == wxT('\t'))
603 {
604 pc++;
605 m_hotKey = pc;
606 }
607 #endif
608
609 if (m_menuItem)
610 {
611 GtkLabel *label = GTK_LABEL( GTK_BIN(m_menuItem)->child );
612 gtk_label_set( label, m_text.mb_str());
613 }
614 }
615
616 void wxMenuItem::Check( bool check )
617 {
618 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
619
620 wxCHECK_RET( IsCheckable(), wxT("Can't check uncheckable item!") )
621
622 if (check == m_isChecked)
623 return;
624
625 wxMenuItemBase::Check( check );
626 gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check );
627 }
628
629 void wxMenuItem::Enable( bool enable )
630 {
631 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
632
633 gtk_widget_set_sensitive( m_menuItem, enable );
634 wxMenuItemBase::Enable( enable );
635 }
636
637 bool wxMenuItem::IsChecked() const
638 {
639 wxCHECK_MSG( m_menuItem, FALSE, wxT("invalid menu item") );
640
641 wxCHECK_MSG( IsCheckable(), FALSE,
642 wxT("can't get state of uncheckable item!") );
643
644 return ((GtkCheckMenuItem*)m_menuItem)->active != 0;
645 }
646
647 //-----------------------------------------------------------------------------
648 // wxMenu
649 //-----------------------------------------------------------------------------
650
651 IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
652
653 void
654 wxMenu::Init( const wxString& title,
655 long style,
656 const wxFunction func
657 )
658 {
659 m_title = title;
660 m_items.DeleteContents( TRUE );
661 m_invokingWindow = (wxWindow *) NULL;
662 m_style = style;
663
664 #if (GTK_MINOR_VERSION > 0)
665 m_accel = gtk_accel_group_new();
666 m_factory = gtk_item_factory_new( GTK_TYPE_MENU, "<main>", m_accel );
667 m_menu = gtk_item_factory_get_widget( m_factory, "<main>" );
668 #else
669 m_menu = gtk_menu_new(); // Do not show!
670 #endif
671
672 m_callback = func;
673
674 m_eventHandler = this;
675 m_clientData = (void*) NULL;
676
677 if (m_title.IsNull()) m_title = wxT("");
678 if (m_title != wxT(""))
679 {
680 Append(-2, m_title);
681 AppendSeparator();
682 }
683
684 m_owner = (GtkWidget*) NULL;
685
686 #if (GTK_MINOR_VERSION > 0)
687 /* Tearoffs are entries, just like separators. So if we want this
688 menu to be a tear-off one, we just append a tearoff entry
689 immediately. */
690 if(m_style & wxMENU_TEAROFF)
691 {
692 GtkItemFactoryEntry entry;
693 entry.path = "/tearoff";
694 entry.callback = (GtkItemFactoryCallback) NULL;
695 entry.callback_action = 0;
696 entry.item_type = "<Tearoff>";
697 entry.accelerator = (gchar*) NULL;
698 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
699 //GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, "<main>/tearoff" );
700 }
701 #endif
702 }
703
704 wxMenu::~wxMenu()
705 {
706 wxNode *node = m_items.First();
707 while (node)
708 {
709 wxMenuItem *item = (wxMenuItem*)node->Data();
710 wxMenu *submenu = item->GetSubMenu();
711 if (submenu)
712 delete submenu;
713 node = node->Next();
714 }
715
716 gtk_widget_destroy( m_menu );
717
718 gtk_object_unref( GTK_OBJECT(m_factory) );
719 }
720
721 void wxMenu::SetTitle( const wxString& title )
722 {
723 // TODO Waiting for something better
724 m_title = title;
725 }
726
727 const wxString wxMenu::GetTitle() const
728 {
729 return m_title;
730 }
731
732 void wxMenu::AppendSeparator()
733 {
734 wxMenuItem *mitem = new wxMenuItem(this, wxID_SEPARATOR);
735
736 #if (GTK_MINOR_VERSION > 0)
737 GtkItemFactoryEntry entry;
738 entry.path = "/sep";
739 entry.callback = (GtkItemFactoryCallback) NULL;
740 entry.callback_action = 0;
741 entry.item_type = "<Separator>";
742 entry.accelerator = (gchar*) NULL;
743
744 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
745
746 /* this will be wrong for more than one separator. do we care? */
747 GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, "<main>/sep" );
748 #else
749 GtkWidget *menuItem = gtk_menu_item_new();
750 gtk_menu_append( GTK_MENU(m_menu), menuItem );
751 gtk_widget_show( menuItem );
752 #endif
753
754 mitem->SetMenuItem(menuItem);
755 m_items.Append( mitem );
756 }
757
758 #if (GTK_MINOR_VERSION > 0) && wxUSE_ACCEL
759 static wxString GetHotKey( const wxMenuItem& item )
760 {
761 wxString hotkey;
762
763 // as wxGetAccelFromString() looks for TAB, insert a dummy one here
764 wxString label;
765 label << wxT('\t') << item.GetHotKey();
766
767 // but if the hotkey is empty don't do anything
768 if ( label.length() > 1 )
769 {
770 wxAcceleratorEntry *accel = wxGetAccelFromString(label);
771 if ( accel )
772 {
773 int flags = accel->GetFlags();
774 if ( flags & wxACCEL_ALT )
775 hotkey += wxT("<alt>");
776 if ( flags & wxACCEL_CTRL )
777 hotkey += wxT("<control>");
778 if ( flags & wxACCEL_SHIFT )
779 hotkey += wxT("<shift>");
780
781 int code = accel->GetKeyCode();
782 switch ( code )
783 {
784 case WXK_F1:
785 case WXK_F2:
786 case WXK_F3:
787 case WXK_F4:
788 case WXK_F5:
789 case WXK_F6:
790 case WXK_F7:
791 case WXK_F8:
792 case WXK_F9:
793 case WXK_F10:
794 case WXK_F11:
795 case WXK_F12:
796 hotkey << wxT('F') << code - WXK_F1 + 1;
797 break;
798
799 // if there are any other keys wxGetAccelFromString() may return,
800 // we should process them here
801
802 default:
803 if ( wxIsalnum(code) )
804 {
805 hotkey << (wxChar)code;
806
807 break;
808 }
809
810 wxFAIL_MSG( wxT("unknown keyboard accel") );
811 }
812
813 delete accel;
814 }
815 }
816
817 return hotkey;
818 }
819 #endif // wxUSE_ACCEL
820
821 void wxMenu::Append( int id, const wxString &item, const wxString &helpStr, bool checkable )
822 {
823 wxMenuItem *mitem = new wxMenuItem(this, id, item, helpStr, checkable);
824
825 #if (GTK_MINOR_VERSION > 0)
826 /* text has "_" instead of "&" after mitem->SetText() */
827 wxString text( mitem->GetText() );
828
829 /* local buffer in multibyte form */
830 char buf[200];
831 strcpy( buf, "/" );
832 strcat( buf, text.mb_str() );
833
834 GtkItemFactoryEntry entry;
835 entry.path = buf;
836 entry.callback = (GtkItemFactoryCallback) gtk_menu_clicked_callback;
837 entry.callback_action = 0;
838 if (checkable)
839 entry.item_type = "<CheckItem>";
840 else
841 entry.item_type = "<Item>";
842
843 #if wxUSE_ACCEL
844 // due to an apparent bug in GTK+, we have to use a static buffer here -
845 // otherwise GTK+ 1.2.2 manages to override the memory we pass to it
846 // somehow! (VZ)
847 static char s_accel[32]; // must be big enough for <control><alt><shift>F12
848 strncpy(s_accel, GetHotKey(*mitem).mb_str(), WXSIZEOF(s_accel));
849 entry.accelerator = s_accel;
850 #else
851 entry.accelerator = NULL;
852 #endif
853
854 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
855
856 /* in order to get the pointer to the item we need the item text _without_ underscores */
857 wxString s = wxT("<main>/");
858 for ( const wxChar *pc = text; *pc != wxT('\0'); pc++ )
859 {
860 while (*pc == wxT('_')) pc++; /* skip it */
861 s << *pc;
862 }
863
864 GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, s.mb_str() );
865
866 #else
867
868 GtkWidget *menuItem = checkable ? gtk_check_menu_item_new_with_label( mitem->GetText().mb_str() )
869 : gtk_menu_item_new_with_label( mitem->GetText().mb_str() );
870
871 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
872 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
873 (gpointer)this );
874
875 gtk_menu_append( GTK_MENU(m_menu), menuItem );
876 gtk_widget_show( menuItem );
877
878 #endif
879
880 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
881 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
882 (gpointer)this );
883
884 gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
885 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
886 (gpointer)this );
887
888 mitem->SetMenuItem(menuItem);
889
890 m_items.Append( mitem );
891 }
892
893 void wxMenu::Append( int id, const wxString &item, wxMenu *subMenu, const wxString &helpStr )
894 {
895 wxMenuItem *mitem = new wxMenuItem(this, id, item, helpStr, FALSE, subMenu);
896
897 #if (GTK_MINOR_VERSION > 0)
898 /* text has "_" instead of "&" after mitem->SetText() */
899 wxString text( mitem->GetText() );
900
901 /* local buffer in multibyte form */
902 char buf[200];
903 strcpy( buf, "/" );
904 strcat( buf, text.mb_str() );
905
906 GtkItemFactoryEntry entry;
907 entry.path = buf;
908 entry.callback = (GtkItemFactoryCallback) 0;
909 entry.callback_action = 0;
910 entry.item_type = "<Branch>";
911
912 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
913
914 /* in order to get the pointer to the item we need the item text _without_ underscores */
915 wxString s = wxT("<main>/");
916 for ( const wxChar *pc = text; *pc != wxT('\0'); pc++ )
917 {
918 if (*pc == wxT('_')) pc++; /* skip it */
919 s << *pc;
920 }
921
922 GtkWidget *menuItem = gtk_item_factory_get_item( m_factory, s.mb_str() );
923
924 #else
925
926 GtkWidget *menuItem = gtk_menu_item_new_with_label(mitem->GetText().mbc_str());
927
928 gtk_menu_append( GTK_MENU(m_menu), menuItem );
929 gtk_widget_show( menuItem );
930
931 #endif
932
933 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
934 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
935 (gpointer*)this );
936
937 gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
938 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
939 (gpointer*)this );
940
941 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), subMenu->m_menu );
942
943 mitem->SetMenuItem(menuItem);
944
945 m_items.Append( mitem );
946 }
947
948 void wxMenu::Append( wxMenuItem *item )
949 {
950 m_items.Append( item );
951
952 GtkWidget *menuItem = (GtkWidget*) NULL;
953
954 if (item->IsSeparator())
955 menuItem = gtk_menu_item_new();
956 else if (item->IsSubMenu())
957 menuItem = gtk_menu_item_new_with_label(item->GetText().mbc_str());
958 else
959 menuItem = item->IsCheckable() ? gtk_check_menu_item_new_with_label(item->GetText().mbc_str())
960 : gtk_menu_item_new_with_label(item->GetText().mbc_str());
961
962 if (!item->IsSeparator())
963 {
964 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
965 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
966 (gpointer*)this );
967
968 gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
969 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
970 (gpointer*)this );
971
972 if (!item->IsSubMenu())
973 {
974 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
975 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
976 (gpointer*)this );
977 }
978 }
979
980 gtk_menu_append( GTK_MENU(m_menu), menuItem );
981 gtk_widget_show( menuItem );
982 item->SetMenuItem(menuItem);
983 }
984
985 void wxMenu::Delete( int id )
986 {
987 wxNode *node = m_items.First();
988 while (node)
989 {
990 wxMenuItem *item = (wxMenuItem*)node->Data();
991 if (item->GetId() == id)
992 {
993 gtk_widget_destroy( item->GetMenuItem() );
994 m_items.DeleteNode( node );
995 return;
996 }
997 node = node->Next();
998 }
999 }
1000
1001 int wxMenu::FindItem( const wxString itemString ) const
1002 {
1003 wxString s = wxT("");
1004 for ( const wxChar *pc = itemString; *pc != wxT('\0'); pc++ )
1005 {
1006 if (*pc == wxT('&'))
1007 {
1008 pc++; /* skip it */
1009 #if (GTK_MINOR_VERSION > 0)
1010 s << wxT('_');
1011 #endif
1012 }
1013 s << *pc;
1014 }
1015
1016 wxNode *node = m_items.First();
1017 while (node)
1018 {
1019 wxMenuItem *item = (wxMenuItem*)node->Data();
1020 if (item->GetText() == s)
1021 {
1022 return item->GetId();
1023 }
1024 node = node->Next();
1025 }
1026
1027 return wxNOT_FOUND;
1028 }
1029
1030 void wxMenu::Enable( int id, bool enable )
1031 {
1032 wxMenuItem *item = FindItem(id);
1033
1034 wxCHECK_RET( item, wxT("wxMenu::Enable: no such item") );
1035
1036 item->Enable(enable);
1037 }
1038
1039 bool wxMenu::IsEnabled( int id ) const
1040 {
1041 wxMenuItem *item = FindItem(id);
1042
1043 wxCHECK_MSG( item, FALSE, wxT("wxMenu::IsEnabled: no such item") );
1044
1045 return item->IsEnabled();
1046 }
1047
1048 void wxMenu::Check( int id, bool enable )
1049 {
1050 wxMenuItem *item = FindItem(id);
1051
1052 wxCHECK_RET( item, wxT("wxMenu::Check: no such item") );
1053
1054 item->Check(enable);
1055 }
1056
1057 bool wxMenu::IsChecked( int id ) const
1058 {
1059 wxMenuItem *item = FindItem(id);
1060
1061 wxCHECK_MSG( item, FALSE, wxT("wxMenu::IsChecked: no such item") );
1062
1063 return item->IsChecked();
1064 }
1065
1066 void wxMenu::SetLabel( int id, const wxString &label )
1067 {
1068 wxMenuItem *item = FindItem(id);
1069
1070 wxCHECK_RET( item, wxT("wxMenu::SetLabel: no such item") );
1071
1072 item->SetText(label);
1073 }
1074
1075 wxString wxMenu::GetLabel( int id ) const
1076 {
1077 wxMenuItem *item = FindItem(id);
1078
1079 wxCHECK_MSG( item, wxT(""), wxT("wxMenu::GetLabel: no such item") );
1080
1081 return item->GetText();
1082 }
1083
1084 void wxMenu::SetHelpString( int id, const wxString& helpString )
1085 {
1086 wxMenuItem *item = FindItem(id);
1087
1088 wxCHECK_RET( item, wxT("wxMenu::SetHelpString: no such item") );
1089
1090 item->SetHelp( helpString );
1091 }
1092
1093 wxString wxMenu::GetHelpString( int id ) const
1094 {
1095 wxMenuItem *item = FindItem(id);
1096
1097 wxCHECK_MSG( item, wxT(""), wxT("wxMenu::GetHelpString: no such item") );
1098
1099 return item->GetHelp();
1100 }
1101
1102 int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
1103 {
1104 wxNode *node = m_items.First();
1105 while (node)
1106 {
1107 wxMenuItem *item = (wxMenuItem*)node->Data();
1108 if (item->GetMenuItem() == menuItem)
1109 return item->GetId();
1110 node = node->Next();
1111 }
1112
1113 return wxNOT_FOUND;
1114 }
1115
1116 wxMenuItem *wxMenu::FindItem(int id) const
1117 {
1118 wxNode *node = m_items.First();
1119 while (node)
1120 {
1121 wxMenuItem *item = (wxMenuItem*)node->Data();
1122 if (item->GetId() == id)
1123 {
1124 return item;
1125 }
1126 node = node->Next();
1127 }
1128
1129 /* Not finding anything here can be correct
1130 * when search the entire menu system for
1131 * an entry -> no error message. */
1132
1133 return (wxMenuItem *) NULL;
1134 }
1135
1136 void wxMenu::SetInvokingWindow( wxWindow *win )
1137 {
1138 m_invokingWindow = win;
1139 }
1140
1141 wxWindow *wxMenu::GetInvokingWindow()
1142 {
1143 return m_invokingWindow;
1144 }
1145
1146 // Update a menu and all submenus recursively. source is the object that has
1147 // the update event handlers defined for it. If NULL, the menu or associated
1148 // window will be used.
1149 void wxMenu::UpdateUI(wxEvtHandler* source)
1150 {
1151 if (!source && GetInvokingWindow())
1152 source = GetInvokingWindow()->GetEventHandler();
1153 if (!source)
1154 source = GetEventHandler();
1155 if (!source)
1156 source = this;
1157
1158 wxNode* node = GetItems().First();
1159 while (node)
1160 {
1161 wxMenuItem* item = (wxMenuItem*) node->Data();
1162 if ( !item->IsSeparator() )
1163 {
1164 wxWindowID id = item->GetId();
1165 wxUpdateUIEvent event(id);
1166 event.SetEventObject( source );
1167
1168 if (source->ProcessEvent(event))
1169 {
1170 if (event.GetSetText())
1171 SetLabel(id, event.GetText());
1172 if (event.GetSetChecked())
1173 Check(id, event.GetChecked());
1174 if (event.GetSetEnabled())
1175 Enable(id, event.GetEnabled());
1176 }
1177
1178 if (item->GetSubMenu())
1179 item->GetSubMenu()->UpdateUI(source);
1180 }
1181 node = node->Next();
1182 }
1183 }
1184