]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/menu.cpp
b7f16438561bc3f7d2781b886b4b5f6bf6e72616
[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/log.h"
16 #include "wx/intl.h"
17 #include "wx/app.h"
18 #include "wx/menu.h"
19
20 #if wxUSE_ACCEL
21 #include "wx/accel.h"
22 #endif // wxUSE_ACCEL
23
24 #include <gdk/gdk.h>
25 #include <gtk/gtk.h>
26
27 //-----------------------------------------------------------------------------
28 // idle system
29 //-----------------------------------------------------------------------------
30
31 extern void wxapp_install_idle_handler();
32 extern bool g_isIdle;
33
34 #if (GTK_MINOR_VERSION > 0) && wxUSE_ACCEL
35 static wxString GetHotKey( const wxMenuItem& item );
36 #endif
37
38 //-----------------------------------------------------------------------------
39 // wxMenuBar
40 //-----------------------------------------------------------------------------
41
42 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
43
44 wxMenuBar::wxMenuBar( long style )
45 {
46 /* the parent window is known after wxFrame::SetMenu() */
47 m_needParent = FALSE;
48 m_style = style;
49 m_invokingWindow = (wxWindow*) NULL;
50
51 if (!PreCreation( (wxWindow*) NULL, wxDefaultPosition, wxDefaultSize ) ||
52 !CreateBase( (wxWindow*) NULL, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, wxT("menubar") ))
53 {
54 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
55 return;
56 }
57
58 m_menus.DeleteContents( TRUE );
59
60 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
61 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
62 m_accel = gtk_accel_group_new();
63 m_factory = gtk_item_factory_new( GTK_TYPE_MENU_BAR, "<main>", m_accel );
64 m_menubar = gtk_item_factory_get_widget( m_factory, "<main>" );
65 #else
66 m_menubar = gtk_menu_bar_new();
67 #endif
68
69 if (style & wxMB_DOCKABLE)
70 {
71 m_widget = gtk_handle_box_new();
72 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_menubar) );
73 gtk_widget_show( GTK_WIDGET(m_menubar) );
74 }
75 else
76 {
77 m_widget = GTK_WIDGET(m_menubar);
78 }
79
80 PostCreation();
81 }
82
83 wxMenuBar::wxMenuBar()
84 {
85 /* the parent window is known after wxFrame::SetMenu() */
86 m_needParent = FALSE;
87 m_style = 0;
88 m_invokingWindow = (wxWindow*) NULL;
89
90 if (!PreCreation( (wxWindow*) NULL, wxDefaultPosition, wxDefaultSize ) ||
91 !CreateBase( (wxWindow*) NULL, -1, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, wxT("menubar") ))
92 {
93 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
94 return;
95 }
96
97 m_menus.DeleteContents( TRUE );
98
99 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
100 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
101 m_accel = gtk_accel_group_new();
102 m_factory = gtk_item_factory_new( GTK_TYPE_MENU_BAR, "<main>", m_accel );
103 m_menubar = gtk_item_factory_get_widget( m_factory, "<main>" );
104 #else
105 m_menubar = gtk_menu_bar_new();
106 #endif
107
108 m_widget = GTK_WIDGET(m_menubar);
109
110 PostCreation();
111 }
112
113 wxMenuBar::~wxMenuBar()
114 {
115 // gtk_object_unref( GTK_OBJECT(m_factory) ); why not ?
116 }
117
118 static void wxMenubarUnsetInvokingWindow( wxMenu *menu, wxWindow *win )
119 {
120 menu->SetInvokingWindow( (wxWindow*) NULL );
121
122 #if (GTK_MINOR_VERSION > 0)
123 wxWindow *top_frame = win;
124 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
125 top_frame = top_frame->GetParent();
126
127 /* support for native hot keys */
128 gtk_accel_group_detach( menu->m_accel, GTK_OBJECT(top_frame->m_widget) );
129 #endif
130
131 wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst();
132 while (node)
133 {
134 wxMenuItem *menuitem = node->GetData();
135 if (menuitem->IsSubMenu())
136 wxMenubarUnsetInvokingWindow( menuitem->GetSubMenu(), win );
137 node = node->GetNext();
138 }
139 }
140
141 static void wxMenubarSetInvokingWindow( wxMenu *menu, wxWindow *win )
142 {
143 menu->SetInvokingWindow( win );
144
145 #if (GTK_MINOR_VERSION > 0)
146 wxWindow *top_frame = win;
147 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
148 top_frame = top_frame->GetParent();
149
150 /* support for native hot keys */
151 gtk_accel_group_attach( menu->m_accel, GTK_OBJECT(top_frame->m_widget) );
152 #endif
153
154 wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst();
155 while (node)
156 {
157 wxMenuItem *menuitem = node->GetData();
158 if (menuitem->IsSubMenu())
159 wxMenubarSetInvokingWindow( menuitem->GetSubMenu(), win );
160 node = node->GetNext();
161 }
162 }
163
164 void wxMenuBar::SetInvokingWindow( wxWindow *win )
165 {
166 m_invokingWindow = win;
167 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
168 wxWindow *top_frame = win;
169 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
170 top_frame = top_frame->GetParent();
171
172 /* support for native key accelerators indicated by underscroes */
173 gtk_accel_group_attach( m_accel, GTK_OBJECT(top_frame->m_widget) );
174 #endif
175
176 wxMenuList::Node *node = m_menus.GetFirst();
177 while (node)
178 {
179 wxMenu *menu = node->GetData();
180 wxMenubarSetInvokingWindow( menu, win );
181 node = node->GetNext();
182 }
183 }
184
185 void wxMenuBar::UnsetInvokingWindow( wxWindow *win )
186 {
187 m_invokingWindow = (wxWindow*) NULL;
188 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
189 wxWindow *top_frame = win;
190 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
191 top_frame = top_frame->GetParent();
192
193 /* support for native key accelerators indicated by underscroes */
194 gtk_accel_group_detach( m_accel, GTK_OBJECT(top_frame->m_widget) );
195 #endif
196
197 wxMenuList::Node *node = m_menus.GetFirst();
198 while (node)
199 {
200 wxMenu *menu = node->GetData();
201 wxMenubarUnsetInvokingWindow( menu, win );
202 node = node->GetNext();
203 }
204 }
205
206 bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
207 {
208 if ( !wxMenuBarBase::Append( menu, title ) )
209 return FALSE;
210
211 return GtkAppend(menu, title);
212 }
213
214 bool wxMenuBar::GtkAppend(wxMenu *menu, const wxString& title)
215 {
216 const wxChar *pc;
217
218 /* GTK 1.2 wants to have "_" instead of "&" for accelerators */
219 wxString str;
220 for ( pc = title; *pc != wxT('\0'); pc++ )
221 {
222 if (*pc == wxT('&'))
223 {
224 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
225 str << wxT('_');
226 }
227 else if (*pc == wxT('/'))
228 {
229 str << wxT('\\');
230 #endif
231 }
232 else
233 {
234 #if __WXGTK12__
235 if ( *pc == wxT('_') )
236 {
237 // underscores must be doubled to prevent them from being
238 // interpreted as accelerator character prefix by GTK
239 str << *pc;
240 }
241 #endif // GTK+ 1.2
242
243 str << *pc;
244 }
245 }
246
247 /* this doesn't have much effect right now */
248 menu->SetTitle( str );
249
250 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
251 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
252
253 /* local buffer in multibyte form */
254 wxString buf;
255 buf << wxT('/') << str.c_str();
256
257 char *cbuf = new char[buf.Length()+1];
258 strcpy(cbuf, buf.mbc_str());
259
260 GtkItemFactoryEntry entry;
261 entry.path = (gchar *)cbuf; // const_cast
262 entry.accelerator = (gchar*) NULL;
263 entry.callback = (GtkItemFactoryCallback) NULL;
264 entry.callback_action = 0;
265 entry.item_type = "<Branch>";
266
267 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
268 /* in order to get the pointer to the item we need the item text _without_ underscores */
269 wxString tmp = wxT("<main>/");
270 for ( pc = str; *pc != wxT('\0'); pc++ )
271 {
272 // contrary to the common sense, we must throw out _all_ underscores,
273 // (i.e. "Hello__World" => "HelloWorld" and not "Hello_World" as we
274 // might naively think). IMHO it's a bug in GTK+ (VZ)
275 while (*pc == wxT('_'))
276 pc++;
277 tmp << *pc;
278 }
279 menu->m_owner = gtk_item_factory_get_item( m_factory, tmp.mb_str() );
280 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu );
281 delete [] cbuf;
282 #else
283
284 menu->m_owner = gtk_menu_item_new_with_label( str.mb_str() );
285 gtk_widget_show( menu->m_owner );
286 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu );
287
288 gtk_menu_bar_append( GTK_MENU_BAR(m_menubar), menu->m_owner );
289
290 #endif
291
292 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
293 // adding menu later on.
294 if (m_invokingWindow)
295 wxMenubarSetInvokingWindow( menu, m_invokingWindow );
296
297 return TRUE;
298 }
299
300 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
301 {
302 if ( !wxMenuBarBase::Insert(pos, menu, title) )
303 return FALSE;
304
305 #if __WXGTK12__
306 // GTK+ doesn't have a function to insert a menu using GtkItemFactory (as
307 // of version 1.2.6), so we first append the item and then change its
308 // index
309 if ( !GtkAppend(menu, title) )
310 return FALSE;
311
312 GtkMenuShell *menu_shell = GTK_MENU_SHELL(m_factory->widget);
313 gpointer data = g_list_last(menu_shell->children)->data;
314 menu_shell->children = g_list_remove(menu_shell->children, data);
315 menu_shell->children = g_list_insert(menu_shell->children, data, pos);
316
317 return TRUE;
318 #else // GTK < 1.2
319 // this should be easy to do with GTK 1.0 - can use standard functions for
320 // this and don't need any hacks like above, but as I don't have GTK 1.0
321 // any more I can't do it
322 wxFAIL_MSG( wxT("TODO") );
323
324 return FALSE;
325 #endif // GTK 1.2/1.0
326 }
327
328 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
329 {
330 if ( !wxMenuBarBase::Replace(pos, menu, title) )
331 return FALSE;
332
333 // remove the old item and insert a new one
334 wxMenu *menuOld = Remove(pos);
335 if ( menuOld && !Insert(pos, menu, title) )
336 {
337 return NULL;
338 }
339
340 // either Insert() succeeded or Remove() failed and menuOld is NULL
341 return menuOld;
342 }
343
344 wxMenu *wxMenuBar::Remove(size_t pos)
345 {
346 wxMenu *menu = wxMenuBarBase::Remove(pos);
347 if ( !menu )
348 return FALSE;
349
350 #ifdef __WXGTK12__
351 // gtk_item_factory_delete_entry() is buggy as of GTK+ 1.2.6, so don't use
352 // it but delete the widget manually instead
353 wxString path = _T("<main>/"),
354 title = menu->GetTitle();
355 for ( const wxChar *p = title.c_str(); *p; p++ )
356 {
357 if ( *p != _T('_') )
358 path += *p;
359 }
360
361 GtkWidget *widget = gtk_item_factory_get_item(m_factory, path.mb_str());
362 if ( widget )
363 {
364 gtk_widget_destroy(widget);
365
366 return menu;
367 }
368
369 // shouldn't happen (FIXME but does now)
370 wxFAIL_MSG( _T("gtk_item_factory_get_item() failed") );
371 #else // GTK < 1.2
372 // this should be very simple to implement
373 wxFAIL_MSG( wxT("TODO") );
374 #endif // GTK 1.2/1.0
375
376 return NULL;
377 }
378
379 static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString )
380 {
381 if (menu->GetTitle() == menuString)
382 {
383 int res = menu->FindItem( itemString );
384 if (res != wxNOT_FOUND)
385 return res;
386 }
387
388 wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst();
389 while (node)
390 {
391 wxMenuItem *item = node->GetData();
392 if (item->IsSubMenu())
393 return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString);
394
395 node = node->GetNext();
396 }
397
398 return wxNOT_FOUND;
399 }
400
401 int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const
402 {
403 wxMenuList::Node *node = m_menus.GetFirst();
404 while (node)
405 {
406 wxMenu *menu = node->GetData();
407 int res = FindMenuItemRecursive( menu, menuString, itemString);
408 if (res != -1)
409 return res;
410 node = node->GetNext();
411 }
412
413 return wxNOT_FOUND;
414 }
415
416 // Find a wxMenuItem using its id. Recurses down into sub-menus
417 static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id)
418 {
419 wxMenuItem* result = menu->FindChildItem(id);
420
421 wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst();
422 while ( node && result == NULL )
423 {
424 wxMenuItem *item = node->GetData();
425 if (item->IsSubMenu())
426 {
427 result = FindMenuItemByIdRecursive( item->GetSubMenu(), id );
428 }
429 node = node->GetNext();
430 }
431
432 return result;
433 }
434
435 wxMenuItem* wxMenuBar::FindItem( int id, wxMenu **menuForItem ) const
436 {
437 wxMenuItem* result = 0;
438 wxMenuList::Node *node = m_menus.GetFirst();
439 while (node && result == 0)
440 {
441 wxMenu *menu = node->GetData();
442 result = FindMenuItemByIdRecursive( menu, id );
443 node = node->GetNext();
444 }
445
446 if ( menuForItem )
447 {
448 *menuForItem = result ? result->GetMenu() : (wxMenu *)NULL;
449 }
450
451 return result;
452 }
453
454 void wxMenuBar::EnableTop( size_t pos, bool flag )
455 {
456 wxMenuList::Node *node = m_menus.Item( pos );
457
458 wxCHECK_RET( node, wxT("menu not found") );
459
460 wxMenu* menu = node->GetData();
461
462 if (menu->m_owner)
463 gtk_widget_set_sensitive( menu->m_owner, flag );
464 }
465
466 wxString wxMenuBar::GetLabelTop( size_t pos ) const
467 {
468 wxMenuList::Node *node = m_menus.Item( pos );
469
470 wxCHECK_MSG( node, wxT("invalid"), wxT("menu not found") );
471
472 wxMenu* menu = node->GetData();
473
474 return menu->GetTitle();
475 }
476
477 void wxMenuBar::SetLabelTop( size_t pos, const wxString& label )
478 {
479 wxMenuList::Node *node = m_menus.Item( pos );
480
481 wxCHECK_RET( node, wxT("menu not found") );
482
483 wxMenu* menu = node->GetData();
484
485 menu->SetTitle( label );
486 }
487
488 //-----------------------------------------------------------------------------
489 // "activate"
490 //-----------------------------------------------------------------------------
491
492 static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu )
493 {
494 if (g_isIdle) wxapp_install_idle_handler();
495
496 int id = menu->FindMenuIdByMenuItem(widget);
497
498 /* should find it for normal (not popup) menu */
499 wxASSERT( (id != -1) || (menu->GetInvokingWindow() != NULL) );
500
501 if (!menu->IsEnabled(id))
502 return;
503
504 wxMenuItem* item = menu->FindChildItem( id );
505 wxCHECK_RET( item, wxT("error in menu item callback") );
506
507 if (item->IsCheckable())
508 {
509 bool isReallyChecked = item->IsChecked();
510 if ( item->wxMenuItemBase::IsChecked() == isReallyChecked )
511 {
512 /* the menu item has been checked by calling wxMenuItem->Check() */
513 return;
514 }
515 else
516 {
517 /* the user pressed on the menu item -> report and make consistent
518 * again */
519 item->wxMenuItemBase::Check(isReallyChecked);
520 }
521 }
522
523 wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, id );
524 event.SetEventObject( menu );
525 event.SetInt(id );
526
527 #if wxUSE_MENU_CALLBACK
528 if (menu->GetCallback())
529 {
530 (void) (*(menu->GetCallback())) (*menu, event);
531 return;
532 }
533 #endif // wxUSE_MENU_CALLBACK
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 // "select"
545 //-----------------------------------------------------------------------------
546
547 static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu )
548 {
549 if (g_isIdle) wxapp_install_idle_handler();
550
551 int id = menu->FindMenuIdByMenuItem(widget);
552
553 wxASSERT( id != -1 ); // should find it!
554
555 if (!menu->IsEnabled(id))
556 return;
557
558 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id );
559 event.SetEventObject( menu );
560
561 if (menu->GetEventHandler()->ProcessEvent(event))
562 return;
563
564 wxWindow *win = menu->GetInvokingWindow();
565 if (win) win->GetEventHandler()->ProcessEvent( event );
566 }
567
568 //-----------------------------------------------------------------------------
569 // "deselect"
570 //-----------------------------------------------------------------------------
571
572 static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu )
573 {
574 if (g_isIdle) wxapp_install_idle_handler();
575
576 int id = menu->FindMenuIdByMenuItem(widget);
577
578 wxASSERT( id != -1 ); // should find it!
579
580 if (!menu->IsEnabled(id))
581 return;
582
583 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 );
584 event.SetEventObject( menu );
585
586 if (menu->GetEventHandler()->ProcessEvent(event))
587 return;
588
589 wxWindow *win = menu->GetInvokingWindow();
590 if (win)
591 win->GetEventHandler()->ProcessEvent( event );
592 }
593
594 //-----------------------------------------------------------------------------
595 // wxMenuItem
596 //-----------------------------------------------------------------------------
597
598 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxMenuItemBase)
599
600 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
601 int id,
602 const wxString& name,
603 const wxString& help,
604 bool isCheckable,
605 wxMenu *subMenu)
606 {
607 return new wxMenuItem(parentMenu, id, name, help, isCheckable, subMenu);
608 }
609
610 wxMenuItem::wxMenuItem(wxMenu *parentMenu,
611 int id,
612 const wxString& text,
613 const wxString& help,
614 bool isCheckable,
615 wxMenu *subMenu)
616 {
617 m_id = id;
618 m_isCheckable = isCheckable;
619 m_isChecked = FALSE;
620 m_isEnabled = TRUE;
621 m_subMenu = subMenu;
622 m_parentMenu = parentMenu;
623 m_help = help;
624
625 m_menuItem = (GtkWidget *) NULL;
626
627 DoSetText(text);
628 }
629
630 wxMenuItem::~wxMenuItem()
631 {
632 // don't delete menu items, the menus take care of that
633 }
634
635 // return the menu item text without any menu accels
636 wxString wxMenuItem::GetLabel() const
637 {
638 wxString label;
639 #if (GTK_MINOR_VERSION > 0)
640 for ( const wxChar *pc = m_text.c_str(); *pc; pc++ )
641 {
642 if ( *pc == wxT('_') )
643 {
644 // this is the escape character for GTK+ - skip it
645 continue;
646 }
647
648 label += *pc;
649 }
650 #else // GTK+ 1.0
651 label = m_text;
652 #endif // GTK+ 1.2/1.0
653
654 return label;
655 }
656
657 void wxMenuItem::SetText( const wxString& str )
658 {
659 DoSetText(str);
660
661 if (m_menuItem)
662 {
663 GtkLabel *label = GTK_LABEL( GTK_BIN(m_menuItem)->child );
664
665 /* set new text */
666 gtk_label_set( label, m_text.mb_str());
667
668 /* reparse key accel */
669 (void)gtk_label_parse_uline (GTK_LABEL(label), m_text.mb_str() );
670 gtk_accel_label_refetch( GTK_ACCEL_LABEL(label) );
671 }
672 }
673
674 // it's valid for this function to be called even if m_menuItem == NULL
675 void wxMenuItem::DoSetText( const wxString& str )
676 {
677 /* '\t' is the deliminator indicating a hot key */
678 m_text.Empty();
679 const wxChar *pc = str;
680 for (; (*pc != wxT('\0')) && (*pc != wxT('\t')); pc++ )
681 {
682 if (*pc == wxT('&'))
683 {
684 #if (GTK_MINOR_VERSION > 0)
685 m_text << wxT('_');
686 }
687 else if ( *pc == wxT('_') ) // escape underscores
688 {
689 m_text << wxT("__");
690 }
691 else if (*pc == wxT('/')) /* we have to filter out slashes ... */
692 {
693 m_text << wxT('\\'); /* ... and replace them with back slashes */
694 #endif
695 }
696 else
697 m_text << *pc;
698 }
699
700 /* only GTK 1.2 knows about hot keys */
701 m_hotKey = wxT("");
702 #if (GTK_MINOR_VERSION > 0)
703 if(*pc == wxT('\t'))
704 {
705 pc++;
706 m_hotKey = pc;
707 }
708 #endif
709 }
710
711 #if wxUSE_ACCEL
712
713 wxAcceleratorEntry *wxMenuItem::GetAccel() const
714 {
715 if ( !GetHotKey() )
716 {
717 // nothing
718 return (wxAcceleratorEntry *)NULL;
719 }
720
721 // as wxGetAccelFromString() looks for TAB, insert a dummy one here
722 wxString label;
723 label << wxT('\t') << GetHotKey();
724
725 return wxGetAccelFromString(label);
726 }
727
728 #endif // wxUSE_ACCEL
729
730 void wxMenuItem::Check( bool check )
731 {
732 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
733
734 wxCHECK_RET( IsCheckable(), wxT("Can't check uncheckable item!") )
735
736 if (check == m_isChecked)
737 return;
738
739 wxMenuItemBase::Check( check );
740 gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check );
741 }
742
743 void wxMenuItem::Enable( bool enable )
744 {
745 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
746
747 gtk_widget_set_sensitive( m_menuItem, enable );
748 wxMenuItemBase::Enable( enable );
749 }
750
751 bool wxMenuItem::IsChecked() const
752 {
753 wxCHECK_MSG( m_menuItem, FALSE, wxT("invalid menu item") );
754
755 wxCHECK_MSG( IsCheckable(), FALSE,
756 wxT("can't get state of uncheckable item!") );
757
758 return ((GtkCheckMenuItem*)m_menuItem)->active != 0;
759 }
760
761 wxString wxMenuItem::GetFactoryPath() const
762 {
763 /* in order to get the pointer to the item we need the item text _without_
764 underscores */
765 wxString path( wxT("<main>/") );
766 path += GetLabel();
767
768 return path;
769 }
770
771 //-----------------------------------------------------------------------------
772 // wxMenu
773 //-----------------------------------------------------------------------------
774
775 IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
776
777 void wxMenu::Init()
778 {
779 #if (GTK_MINOR_VERSION > 0)
780 m_accel = gtk_accel_group_new();
781 m_factory = gtk_item_factory_new( GTK_TYPE_MENU, "<main>", m_accel );
782 m_menu = gtk_item_factory_get_widget( m_factory, "<main>" );
783 #else
784 m_menu = gtk_menu_new(); // Do not show!
785 #endif
786
787 m_owner = (GtkWidget*) NULL;
788
789 #if (GTK_MINOR_VERSION > 0)
790 /* Tearoffs are entries, just like separators. So if we want this
791 menu to be a tear-off one, we just append a tearoff entry
792 immediately. */
793 if(m_style & wxMENU_TEAROFF)
794 {
795 GtkItemFactoryEntry entry;
796 entry.path = "/tearoff";
797 entry.callback = (GtkItemFactoryCallback) NULL;
798 entry.callback_action = 0;
799 entry.item_type = "<Tearoff>";
800 entry.accelerator = (gchar*) NULL;
801 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
802 //GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, "<main>/tearoff" );
803 }
804 #endif
805
806 // append the title as the very first entry if we have it
807 if ( !!m_title )
808 {
809 Append(-2, m_title);
810 AppendSeparator();
811 }
812 }
813
814 wxMenu::~wxMenu()
815 {
816 m_items.Clear();
817
818 gtk_widget_destroy( m_menu );
819
820 gtk_object_unref( GTK_OBJECT(m_factory) );
821 }
822
823 bool wxMenu::DoAppend(wxMenuItem *mitem)
824 {
825 GtkWidget *menuItem;
826
827 if ( mitem->IsSeparator() )
828 {
829 #if (GTK_MINOR_VERSION > 0)
830 GtkItemFactoryEntry entry;
831 entry.path = "/sep";
832 entry.callback = (GtkItemFactoryCallback) NULL;
833 entry.callback_action = 0;
834 entry.item_type = "<Separator>";
835 entry.accelerator = (gchar*) NULL;
836
837 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
838
839 /* this will be wrong for more than one separator. do we care? */
840 menuItem = gtk_item_factory_get_widget( m_factory, "<main>/sep" );
841 #else // GTK+ 1.0
842 menuItem = gtk_menu_item_new();
843 #endif // GTK 1.2/1.0
844 }
845 else if ( mitem->IsSubMenu() )
846 {
847 #if (GTK_MINOR_VERSION > 0)
848 /* text has "_" instead of "&" after mitem->SetText() */
849 wxString text( mitem->GetText() );
850
851 /* local buffer in multibyte form */
852 char buf[200];
853 strcpy( buf, "/" );
854 strcat( buf, text.mb_str() );
855
856 GtkItemFactoryEntry entry;
857 entry.path = buf;
858 entry.callback = (GtkItemFactoryCallback) 0;
859 entry.callback_action = 0;
860 entry.item_type = "<Branch>";
861 entry.accelerator = (gchar*) NULL;
862
863 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
864
865 wxString path( mitem->GetFactoryPath() );
866 menuItem = gtk_item_factory_get_item( m_factory, path.mb_str() );
867 #else // GTK+ 1.0
868 menuItem = gtk_menu_item_new_with_label(mitem->GetText().mbc_str());
869 #endif // GTK 1.2/1.0
870
871 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), mitem->GetSubMenu()->m_menu );
872 }
873 else // a normal item
874 {
875 #if (GTK_MINOR_VERSION > 0)
876 /* text has "_" instead of "&" after mitem->SetText() */
877 wxString text( mitem->GetText() );
878
879 /* local buffer in multibyte form */
880 char buf[200];
881 strcpy( buf, "/" );
882 strcat( buf, text.mb_str() );
883
884 GtkItemFactoryEntry entry;
885 entry.path = buf;
886 entry.callback = (GtkItemFactoryCallback) gtk_menu_clicked_callback;
887 entry.callback_action = 0;
888 if ( mitem->IsCheckable() )
889 entry.item_type = "<CheckItem>";
890 else
891 entry.item_type = "<Item>";
892 entry.accelerator = (gchar*) NULL;
893
894 #if wxUSE_ACCEL
895 // due to an apparent bug in GTK+, we have to use a static buffer here -
896 // otherwise GTK+ 1.2.2 manages to override the memory we pass to it
897 // somehow! (VZ)
898 static char s_accel[32]; // must be big enough for <control><alt><shift>F12
899 strncpy(s_accel, GetHotKey(*mitem).mb_str(), WXSIZEOF(s_accel));
900 entry.accelerator = s_accel;
901 #else // !wxUSE_ACCEL
902 entry.accelerator = (char*) NULL;
903 #endif // wxUSE_ACCEL/!wxUSE_ACCEL
904
905 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
906
907 wxString path( mitem->GetFactoryPath() );
908 menuItem = gtk_item_factory_get_widget( m_factory, path.mb_str() );
909 #else // GTK+ 1.0
910 menuItem = checkable ? gtk_check_menu_item_new_with_label( mitem->GetText().mb_str() )
911 : gtk_menu_item_new_with_label( mitem->GetText().mb_str() );
912
913 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
914 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
915 (gpointer)this );
916 #endif // GTK+ 1.2/1.0
917 }
918
919 if ( !mitem->IsSeparator() )
920 {
921 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
922 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
923 (gpointer)this );
924
925 gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
926 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
927 (gpointer)this );
928 }
929
930 #if GTK_MINOR_VERSION == 0
931 gtk_menu_append( GTK_MENU(m_menu), menuItem );
932 gtk_widget_show( menuItem );
933 #endif // GTK+ 1.0
934
935 mitem->SetMenuItem(menuItem);
936
937 return wxMenuBase::DoAppend(mitem);
938 }
939
940 // VZ: this seems to be GTK+ 1.0 only code, I don't understand why there were
941 // both specialized versions of Append() and this one before my changes,
942 // but it seems that the others are better...
943 #if 0
944 void wxMenu::Append( wxMenuItem *item )
945 {
946 GtkWidget *menuItem = (GtkWidget*) NULL;
947
948 if (item->IsSeparator())
949 menuItem = gtk_menu_item_new();
950 else if (item->IsSubMenu())
951 menuItem = gtk_menu_item_new_with_label(item->GetText().mbc_str());
952 else
953 menuItem = item->IsCheckable() ? gtk_check_menu_item_new_with_label(item->GetText().mbc_str())
954 : gtk_menu_item_new_with_label(item->GetText().mbc_str());
955
956 if (!item->IsSeparator())
957 {
958 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
959 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
960 (gpointer*)this );
961
962 gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
963 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
964 (gpointer*)this );
965
966 if (!item->IsSubMenu())
967 {
968 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
969 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
970 (gpointer*)this );
971 }
972 }
973
974 gtk_menu_append( GTK_MENU(m_menu), menuItem );
975 gtk_widget_show( menuItem );
976
977 item->SetMenuItem(menuItem);
978 }
979 #endif // 0
980
981 bool wxMenu::DoInsert(size_t pos, wxMenuItem *item)
982 {
983 if ( !wxMenuBase::DoInsert(pos, item) )
984 return FALSE;
985
986 wxFAIL_MSG(wxT("not implemented"));
987
988 return FALSE;
989 }
990
991 wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
992 {
993 if ( !wxMenuBase::DoRemove(item) )
994 return (wxMenuItem *)NULL;
995
996 // TODO: this code doesn't delete the item factory item and this seems
997 // impossible as of GTK 1.2.6.
998 gtk_widget_destroy( item->GetMenuItem() );
999
1000 return item;
1001 }
1002
1003 int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
1004 {
1005 wxNode *node = m_items.First();
1006 while (node)
1007 {
1008 wxMenuItem *item = (wxMenuItem*)node->Data();
1009 if (item->GetMenuItem() == menuItem)
1010 return item->GetId();
1011 node = node->Next();
1012 }
1013
1014 return wxNOT_FOUND;
1015 }
1016
1017 // ----------------------------------------------------------------------------
1018 // helpers
1019 // ----------------------------------------------------------------------------
1020
1021 #if (GTK_MINOR_VERSION > 0) && wxUSE_ACCEL
1022 static wxString GetHotKey( const wxMenuItem& item )
1023 {
1024 wxString hotkey;
1025
1026 wxAcceleratorEntry *accel = item.GetAccel();
1027 if ( accel )
1028 {
1029 int flags = accel->GetFlags();
1030 if ( flags & wxACCEL_ALT )
1031 hotkey += wxT("<alt>");
1032 if ( flags & wxACCEL_CTRL )
1033 hotkey += wxT("<control>");
1034 if ( flags & wxACCEL_SHIFT )
1035 hotkey += wxT("<shift>");
1036
1037 int code = accel->GetKeyCode();
1038 switch ( code )
1039 {
1040 case WXK_F1:
1041 case WXK_F2:
1042 case WXK_F3:
1043 case WXK_F4:
1044 case WXK_F5:
1045 case WXK_F6:
1046 case WXK_F7:
1047 case WXK_F8:
1048 case WXK_F9:
1049 case WXK_F10:
1050 case WXK_F11:
1051 case WXK_F12:
1052 hotkey << wxT('F') << code - WXK_F1 + 1;
1053 break;
1054
1055 // if there are any other keys wxGetAccelFromString() may return,
1056 // we should process them here
1057
1058 default:
1059 if ( wxIsalnum(code) )
1060 {
1061 hotkey << (wxChar)code;
1062
1063 break;
1064 }
1065
1066 wxFAIL_MSG( wxT("unknown keyboard accel") );
1067 }
1068
1069 delete accel;
1070 }
1071
1072 return hotkey;
1073 }
1074 #endif // wxUSE_ACCEL
1075