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