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