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