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