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