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