]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/menu.cpp
Corrected some things in, and some thing revealed by
[wxWidgets.git] / src / gtk / menu.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: menu.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "menu.h"
12 #pragma implementation "menuitem.h"
13 #endif
14
15 #include "wx/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 (wxMenu*) NULL;
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 (wxMenu*) 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 (wxMenu*) NULL;
349
350 GtkMenuShell *menu_shell = GTK_MENU_SHELL(m_factory->widget);
351 printf( "factory entries before %d\n", (int)g_slist_length(m_factory->items) );
352 printf( "menu shell entries before %d\n", (int)g_list_length( menu_shell->children ) );
353
354 // unparent calls unref() and that would delete the widget so we raise
355 // the ref count to 2 artificially before invoking unparent.
356 gtk_widget_ref( menu->m_menu );
357 gtk_widget_unparent( menu->m_menu );
358
359 gtk_widget_destroy( menu->m_owner );
360
361 printf( "factory entries after %d\n", (int)g_slist_length(m_factory->items) );
362 printf( "menu shell entries after %d\n", (int)g_list_length( menu_shell->children ) );
363
364 return menu;
365 }
366
367 static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString )
368 {
369 if (menu->GetTitle() == menuString)
370 {
371 int res = menu->FindItem( itemString );
372 if (res != wxNOT_FOUND)
373 return res;
374 }
375
376 wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst();
377 while (node)
378 {
379 wxMenuItem *item = node->GetData();
380 if (item->IsSubMenu())
381 return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString);
382
383 node = node->GetNext();
384 }
385
386 return wxNOT_FOUND;
387 }
388
389 int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const
390 {
391 wxMenuList::Node *node = m_menus.GetFirst();
392 while (node)
393 {
394 wxMenu *menu = node->GetData();
395 int res = FindMenuItemRecursive( menu, menuString, itemString);
396 if (res != -1)
397 return res;
398 node = node->GetNext();
399 }
400
401 return wxNOT_FOUND;
402 }
403
404 // Find a wxMenuItem using its id. Recurses down into sub-menus
405 static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id)
406 {
407 wxMenuItem* result = menu->FindChildItem(id);
408
409 wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst();
410 while ( node && result == NULL )
411 {
412 wxMenuItem *item = node->GetData();
413 if (item->IsSubMenu())
414 {
415 result = FindMenuItemByIdRecursive( item->GetSubMenu(), id );
416 }
417 node = node->GetNext();
418 }
419
420 return result;
421 }
422
423 wxMenuItem* wxMenuBar::FindItem( int id, wxMenu **menuForItem ) const
424 {
425 wxMenuItem* result = 0;
426 wxMenuList::Node *node = m_menus.GetFirst();
427 while (node && result == 0)
428 {
429 wxMenu *menu = node->GetData();
430 result = FindMenuItemByIdRecursive( menu, id );
431 node = node->GetNext();
432 }
433
434 if ( menuForItem )
435 {
436 *menuForItem = result ? result->GetMenu() : (wxMenu *)NULL;
437 }
438
439 return result;
440 }
441
442 void wxMenuBar::EnableTop( size_t pos, bool flag )
443 {
444 wxMenuList::Node *node = m_menus.Item( pos );
445
446 wxCHECK_RET( node, wxT("menu not found") );
447
448 wxMenu* menu = node->GetData();
449
450 if (menu->m_owner)
451 gtk_widget_set_sensitive( menu->m_owner, flag );
452 }
453
454 wxString wxMenuBar::GetLabelTop( size_t pos ) const
455 {
456 wxMenuList::Node *node = m_menus.Item( pos );
457
458 wxCHECK_MSG( node, wxT("invalid"), wxT("menu not found") );
459
460 wxMenu* menu = node->GetData();
461
462 return menu->GetTitle();
463 }
464
465 void wxMenuBar::SetLabelTop( size_t pos, const wxString& label )
466 {
467 wxMenuList::Node *node = m_menus.Item( pos );
468
469 wxCHECK_RET( node, wxT("menu not found") );
470
471 wxMenu* menu = node->GetData();
472
473 menu->SetTitle( label );
474 }
475
476 //-----------------------------------------------------------------------------
477 // "activate"
478 //-----------------------------------------------------------------------------
479
480 static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu )
481 {
482 if (g_isIdle) wxapp_install_idle_handler();
483
484 int id = menu->FindMenuIdByMenuItem(widget);
485
486 /* should find it for normal (not popup) menu */
487 wxASSERT( (id != -1) || (menu->GetInvokingWindow() != NULL) );
488
489 if (!menu->IsEnabled(id))
490 return;
491
492 wxMenuItem* item = menu->FindChildItem( id );
493 wxCHECK_RET( item, wxT("error in menu item callback") );
494
495 if (item->IsCheckable())
496 {
497 bool isReallyChecked = item->IsChecked();
498 if ( item->wxMenuItemBase::IsChecked() == isReallyChecked )
499 {
500 /* the menu item has been checked by calling wxMenuItem->Check() */
501 return;
502 }
503 else
504 {
505 /* the user pressed on the menu item -> report and make consistent
506 * again */
507 item->wxMenuItemBase::Check(isReallyChecked);
508 }
509 }
510
511 wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, id );
512 event.SetEventObject( menu );
513 event.SetInt(id );
514
515 #if wxUSE_MENU_CALLBACK
516 if (menu->GetCallback())
517 {
518 (void) (*(menu->GetCallback())) (*menu, event);
519 return;
520 }
521 #endif // wxUSE_MENU_CALLBACK
522
523 if (menu->GetEventHandler()->ProcessEvent(event))
524 return;
525
526 wxWindow *win = menu->GetInvokingWindow();
527 if (win)
528 win->GetEventHandler()->ProcessEvent( event );
529 }
530
531 //-----------------------------------------------------------------------------
532 // "select"
533 //-----------------------------------------------------------------------------
534
535 static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu )
536 {
537 if (g_isIdle) wxapp_install_idle_handler();
538
539 int id = menu->FindMenuIdByMenuItem(widget);
540
541 wxASSERT( id != -1 ); // should find it!
542
543 if (!menu->IsEnabled(id))
544 return;
545
546 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id );
547 event.SetEventObject( menu );
548
549 if (menu->GetEventHandler()->ProcessEvent(event))
550 return;
551
552 wxWindow *win = menu->GetInvokingWindow();
553 if (win) win->GetEventHandler()->ProcessEvent( event );
554 }
555
556 //-----------------------------------------------------------------------------
557 // "deselect"
558 //-----------------------------------------------------------------------------
559
560 static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu )
561 {
562 if (g_isIdle) wxapp_install_idle_handler();
563
564 int id = menu->FindMenuIdByMenuItem(widget);
565
566 wxASSERT( id != -1 ); // should find it!
567
568 if (!menu->IsEnabled(id))
569 return;
570
571 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 );
572 event.SetEventObject( menu );
573
574 if (menu->GetEventHandler()->ProcessEvent(event))
575 return;
576
577 wxWindow *win = menu->GetInvokingWindow();
578 if (win)
579 win->GetEventHandler()->ProcessEvent( event );
580 }
581
582 //-----------------------------------------------------------------------------
583 // wxMenuItem
584 //-----------------------------------------------------------------------------
585
586 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxMenuItemBase)
587
588 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
589 int id,
590 const wxString& name,
591 const wxString& help,
592 bool isCheckable,
593 wxMenu *subMenu)
594 {
595 return new wxMenuItem(parentMenu, id, name, help, isCheckable, subMenu);
596 }
597
598 wxMenuItem::wxMenuItem(wxMenu *parentMenu,
599 int id,
600 const wxString& text,
601 const wxString& help,
602 bool isCheckable,
603 wxMenu *subMenu)
604 {
605 m_id = id;
606 m_isCheckable = isCheckable;
607 m_isChecked = FALSE;
608 m_isEnabled = TRUE;
609 m_subMenu = subMenu;
610 m_parentMenu = parentMenu;
611 m_help = help;
612
613 m_menuItem = (GtkWidget *) NULL;
614
615 DoSetText(text);
616 }
617
618 wxMenuItem::~wxMenuItem()
619 {
620 // don't delete menu items, the menus take care of that
621 }
622
623 // return the menu item text without any menu accels
624 wxString wxMenuItem::GetLabel() const
625 {
626 wxString label;
627 #if (GTK_MINOR_VERSION > 0)
628 for ( const wxChar *pc = m_text.c_str(); *pc; pc++ )
629 {
630 if ( *pc == wxT('_') )
631 {
632 // this is the escape character for GTK+ - skip it
633 continue;
634 }
635
636 label += *pc;
637 }
638 #else // GTK+ 1.0
639 label = m_text;
640 #endif // GTK+ 1.2/1.0
641
642 return label;
643 }
644
645 void wxMenuItem::SetText( const wxString& str )
646 {
647 DoSetText(str);
648
649 if (m_menuItem)
650 {
651 GtkLabel *label = GTK_LABEL( GTK_BIN(m_menuItem)->child );
652
653 /* set new text */
654 gtk_label_set( label, m_text.mb_str());
655
656 /* reparse key accel */
657 (void)gtk_label_parse_uline (GTK_LABEL(label), m_text.mb_str() );
658 gtk_accel_label_refetch( GTK_ACCEL_LABEL(label) );
659 }
660 }
661
662 // it's valid for this function to be called even if m_menuItem == NULL
663 void wxMenuItem::DoSetText( const wxString& str )
664 {
665 /* '\t' is the deliminator indicating a hot key */
666 m_text.Empty();
667 const wxChar *pc = str;
668 for (; (*pc != wxT('\0')) && (*pc != wxT('\t')); pc++ )
669 {
670 if (*pc == wxT('&'))
671 {
672 #if (GTK_MINOR_VERSION > 0)
673 m_text << wxT('_');
674 }
675 else if ( *pc == wxT('_') ) // escape underscores
676 {
677 m_text << wxT("__");
678 }
679 else if (*pc == wxT('/')) /* we have to filter out slashes ... */
680 {
681 m_text << wxT('\\'); /* ... and replace them with back slashes */
682 #endif
683 }
684 else
685 m_text << *pc;
686 }
687
688 /* only GTK 1.2 knows about hot keys */
689 m_hotKey = wxT("");
690 #if (GTK_MINOR_VERSION > 0)
691 if(*pc == wxT('\t'))
692 {
693 pc++;
694 m_hotKey = pc;
695 }
696 #endif
697 }
698
699 #if wxUSE_ACCEL
700
701 wxAcceleratorEntry *wxMenuItem::GetAccel() const
702 {
703 if ( !GetHotKey() )
704 {
705 // nothing
706 return (wxAcceleratorEntry *)NULL;
707 }
708
709 // as wxGetAccelFromString() looks for TAB, insert a dummy one here
710 wxString label;
711 label << wxT('\t') << GetHotKey();
712
713 return wxGetAccelFromString(label);
714 }
715
716 #endif // wxUSE_ACCEL
717
718 void wxMenuItem::Check( bool check )
719 {
720 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
721
722 wxCHECK_RET( IsCheckable(), wxT("Can't check uncheckable item!") )
723
724 if (check == m_isChecked)
725 return;
726
727 wxMenuItemBase::Check( check );
728 gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check );
729 }
730
731 void wxMenuItem::Enable( bool enable )
732 {
733 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
734
735 gtk_widget_set_sensitive( m_menuItem, enable );
736 wxMenuItemBase::Enable( enable );
737 }
738
739 bool wxMenuItem::IsChecked() const
740 {
741 wxCHECK_MSG( m_menuItem, FALSE, wxT("invalid menu item") );
742
743 wxCHECK_MSG( IsCheckable(), FALSE,
744 wxT("can't get state of uncheckable item!") );
745
746 return ((GtkCheckMenuItem*)m_menuItem)->active != 0;
747 }
748
749 wxString wxMenuItem::GetFactoryPath() const
750 {
751 /* in order to get the pointer to the item we need the item text _without_
752 underscores */
753 wxString path( wxT("<main>/") );
754 path += GetLabel();
755
756 return path;
757 }
758
759 //-----------------------------------------------------------------------------
760 // wxMenu
761 //-----------------------------------------------------------------------------
762
763 IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
764
765 void wxMenu::Init()
766 {
767 #if (GTK_MINOR_VERSION > 0)
768 m_accel = gtk_accel_group_new();
769 m_factory = gtk_item_factory_new( GTK_TYPE_MENU, "<main>", m_accel );
770 m_menu = gtk_item_factory_get_widget( m_factory, "<main>" );
771 #else
772 m_menu = gtk_menu_new(); // Do not show!
773 #endif
774
775 m_owner = (GtkWidget*) NULL;
776
777 #if (GTK_MINOR_VERSION > 0)
778 /* Tearoffs are entries, just like separators. So if we want this
779 menu to be a tear-off one, we just append a tearoff entry
780 immediately. */
781 if(m_style & wxMENU_TEAROFF)
782 {
783 GtkItemFactoryEntry entry;
784 entry.path = "/tearoff";
785 entry.callback = (GtkItemFactoryCallback) NULL;
786 entry.callback_action = 0;
787 entry.item_type = "<Tearoff>";
788 entry.accelerator = (gchar*) NULL;
789 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
790 //GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, "<main>/tearoff" );
791 }
792 #endif
793
794 // append the title as the very first entry if we have it
795 if ( !!m_title )
796 {
797 Append(-2, m_title);
798 AppendSeparator();
799 }
800 }
801
802 wxMenu::~wxMenu()
803 {
804 m_items.Clear();
805
806 gtk_widget_destroy( m_menu );
807
808 gtk_object_unref( GTK_OBJECT(m_factory) );
809 }
810
811 bool wxMenu::GtkAppend(wxMenuItem *mitem)
812 {
813 GtkWidget *menuItem;
814
815 if ( mitem->IsSeparator() )
816 {
817 #if (GTK_MINOR_VERSION > 0)
818 GtkItemFactoryEntry entry;
819 entry.path = "/sep";
820 entry.callback = (GtkItemFactoryCallback) NULL;
821 entry.callback_action = 0;
822 entry.item_type = "<Separator>";
823 entry.accelerator = (gchar*) NULL;
824
825 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
826
827 /* this will be wrong for more than one separator. do we care? */
828 menuItem = gtk_item_factory_get_widget( m_factory, "<main>/sep" );
829 #else // GTK+ 1.0
830 menuItem = gtk_menu_item_new();
831 #endif // GTK 1.2/1.0
832 }
833 else if ( mitem->IsSubMenu() )
834 {
835 #if (GTK_MINOR_VERSION > 0)
836 /* text has "_" instead of "&" after mitem->SetText() */
837 wxString text( mitem->GetText() );
838
839 /* local buffer in multibyte form */
840 char buf[200];
841 strcpy( buf, "/" );
842 strcat( buf, text.mb_str() );
843
844 GtkItemFactoryEntry entry;
845 entry.path = buf;
846 entry.callback = (GtkItemFactoryCallback) 0;
847 entry.callback_action = 0;
848 entry.item_type = "<Branch>";
849 entry.accelerator = (gchar*) NULL;
850
851 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
852
853 wxString path( mitem->GetFactoryPath() );
854 menuItem = gtk_item_factory_get_item( m_factory, path.mb_str() );
855 #else // GTK+ 1.0
856 menuItem = gtk_menu_item_new_with_label(mitem->GetText().mbc_str());
857 #endif // GTK 1.2/1.0
858
859 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), mitem->GetSubMenu()->m_menu );
860 }
861 else // a normal item
862 {
863 #if (GTK_MINOR_VERSION > 0)
864 /* text has "_" instead of "&" after mitem->SetText() */
865 wxString text( mitem->GetText() );
866
867 /* local buffer in multibyte form */
868 char buf[200];
869 strcpy( buf, "/" );
870 strcat( buf, text.mb_str() );
871
872 GtkItemFactoryEntry entry;
873 entry.path = buf;
874 entry.callback = (GtkItemFactoryCallback) gtk_menu_clicked_callback;
875 entry.callback_action = 0;
876 if ( mitem->IsCheckable() )
877 entry.item_type = "<CheckItem>";
878 else
879 entry.item_type = "<Item>";
880 entry.accelerator = (gchar*) NULL;
881
882 #if wxUSE_ACCEL
883 // due to an apparent bug in GTK+, we have to use a static buffer here -
884 // otherwise GTK+ 1.2.2 manages to override the memory we pass to it
885 // somehow! (VZ)
886 static char s_accel[32]; // must be big enough for <control><alt><shift>F12
887 strncpy(s_accel, GetHotKey(*mitem).mb_str(), WXSIZEOF(s_accel));
888 entry.accelerator = s_accel;
889 #else // !wxUSE_ACCEL
890 entry.accelerator = (char*) NULL;
891 #endif // wxUSE_ACCEL/!wxUSE_ACCEL
892
893 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
894
895 wxString path( mitem->GetFactoryPath() );
896 menuItem = gtk_item_factory_get_widget( m_factory, path.mb_str() );
897 #else // GTK+ 1.0
898 menuItem = checkable ? gtk_check_menu_item_new_with_label( mitem->GetText().mb_str() )
899 : gtk_menu_item_new_with_label( mitem->GetText().mb_str() );
900
901 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
902 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
903 (gpointer)this );
904 #endif // GTK+ 1.2/1.0
905 }
906
907 if ( !mitem->IsSeparator() )
908 {
909 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
910 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
911 (gpointer)this );
912
913 gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
914 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
915 (gpointer)this );
916 }
917
918 #if GTK_MINOR_VERSION == 0
919 gtk_menu_append( GTK_MENU(m_menu), menuItem );
920 gtk_widget_show( menuItem );
921 #endif // GTK+ 1.0
922
923 mitem->SetMenuItem(menuItem);
924
925 return TRUE;
926 }
927
928 bool wxMenu::DoAppend(wxMenuItem *mitem)
929 {
930 return GtkAppend(mitem) && wxMenuBase::DoAppend(mitem);
931 }
932
933 bool wxMenu::DoInsert(size_t pos, wxMenuItem *item)
934 {
935 if ( !wxMenuBase::DoInsert(pos, item) )
936 return FALSE;
937
938 #ifdef __WXGTK12__
939 // GTK+ doesn't have a function to insert a menu using GtkItemFactory (as
940 // of version 1.2.6), so we first append the item and then change its
941 // index
942 if ( !GtkAppend(item) )
943 return FALSE;
944
945 GtkMenuShell *menu_shell = GTK_MENU_SHELL(m_factory->widget);
946 gpointer data = g_list_last(menu_shell->children)->data;
947 menu_shell->children = g_list_remove(menu_shell->children, data);
948 menu_shell->children = g_list_insert(menu_shell->children, data, pos);
949
950 return TRUE;
951 #else // GTK < 1.2
952 // this should be easy to do...
953 wxFAIL_MSG( wxT("not implemented") );
954 #endif // GTK 1.2/1.0
955
956 return FALSE;
957 }
958
959 wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
960 {
961 if ( !wxMenuBase::DoRemove(item) )
962 return (wxMenuItem *)NULL;
963
964 // TODO: this code doesn't delete the item factory item and this seems
965 // impossible as of GTK 1.2.6.
966 gtk_widget_destroy( item->GetMenuItem() );
967
968 return item;
969 }
970
971 int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
972 {
973 wxNode *node = m_items.First();
974 while (node)
975 {
976 wxMenuItem *item = (wxMenuItem*)node->Data();
977 if (item->GetMenuItem() == menuItem)
978 return item->GetId();
979 node = node->Next();
980 }
981
982 return wxNOT_FOUND;
983 }
984
985 // ----------------------------------------------------------------------------
986 // helpers
987 // ----------------------------------------------------------------------------
988
989 #if (GTK_MINOR_VERSION > 0) && wxUSE_ACCEL
990 static wxString GetHotKey( const wxMenuItem& item )
991 {
992 wxString hotkey;
993
994 wxAcceleratorEntry *accel = item.GetAccel();
995 if ( accel )
996 {
997 int flags = accel->GetFlags();
998 if ( flags & wxACCEL_ALT )
999 hotkey += wxT("<alt>");
1000 if ( flags & wxACCEL_CTRL )
1001 hotkey += wxT("<control>");
1002 if ( flags & wxACCEL_SHIFT )
1003 hotkey += wxT("<shift>");
1004
1005 int code = accel->GetKeyCode();
1006 switch ( code )
1007 {
1008 case WXK_F1:
1009 case WXK_F2:
1010 case WXK_F3:
1011 case WXK_F4:
1012 case WXK_F5:
1013 case WXK_F6:
1014 case WXK_F7:
1015 case WXK_F8:
1016 case WXK_F9:
1017 case WXK_F10:
1018 case WXK_F11:
1019 case WXK_F12:
1020 hotkey << wxT('F') << code - WXK_F1 + 1;
1021 break;
1022
1023 // if there are any other keys wxGetAccelFromString() may return,
1024 // we should process them here
1025
1026 default:
1027 if ( wxIsalnum(code) )
1028 {
1029 hotkey << (wxChar)code;
1030
1031 break;
1032 }
1033
1034 wxFAIL_MSG( wxT("unknown keyboard accel") );
1035 }
1036
1037 delete accel;
1038 }
1039
1040 return hotkey;
1041 }
1042 #endif // wxUSE_ACCEL
1043