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