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