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