+extern "C" {
+static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu )
+{
+ if (g_isIdle)
+ wxapp_install_idle_handler();
+
+ int id = menu->FindMenuIdByMenuItem(widget);
+
+ /* should find it for normal (not popup) menu */
+ wxASSERT_MSG( (id != -1) || (menu->GetInvokingWindow() != NULL),
+ _T("menu item not found in gtk_menu_clicked_callback") );
+
+ if (!menu->IsEnabled(id))
+ return;
+
+ wxMenuItem* item = menu->FindChildItem( id );
+ wxCHECK_RET( item, wxT("error in menu item callback") );
+
+ if ( item->GetId() == wxGTK_TITLE_ID )
+ {
+ // ignore events from the menu title
+ return;
+ }
+
+ if (item->IsCheckable())
+ {
+ bool isReallyChecked = item->IsChecked(),
+ isInternallyChecked = item->wxMenuItemBase::IsChecked();
+
+ // ensure that the internal state is always consistent with what is
+ // shown on the screen
+ item->wxMenuItemBase::Check(isReallyChecked);
+
+ // we must not report the events for the radio button going up nor the
+ // events resulting from the calls to wxMenuItem::Check()
+ if ( (item->GetKind() == wxITEM_RADIO && !isReallyChecked) ||
+ (isInternallyChecked == isReallyChecked) )
+ {
+ return;
+ }
+ }
+
+
+ // Is this menu on a menubar? (possibly nested)
+ wxFrame* frame = NULL;
+ if(menu->IsAttached())
+ frame = menu->GetMenuBar()->GetFrame();
+
+ // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
+ // normally wxMenu::SendEvent() should be enough, if it doesn't work
+ // in wxGTK then we have a bug in wxMenu::GetInvokingWindow() which
+ // should be fixed instead of working around it here...
+ if (frame)
+ {
+ // If it is attached then let the frame send the event.
+ // Don't call frame->ProcessCommand(id) because it toggles
+ // checkable items and we've already done that above.
+ wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
+ commandEvent.SetEventObject(frame);
+ if (item->IsCheckable())
+ commandEvent.SetInt(item->IsChecked());
+ commandEvent.SetEventObject(menu);
+
+ frame->GetEventHandler()->ProcessEvent(commandEvent);
+ }
+ else
+ {
+ // otherwise let the menu have it
+ menu->SendEvent(id, item->IsCheckable() ? item->IsChecked() : -1);
+ }
+}
+}
+
+//-----------------------------------------------------------------------------
+// "select"
+//-----------------------------------------------------------------------------
+
+extern "C" {
+static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu )
+{
+ if (g_isIdle) wxapp_install_idle_handler();
+
+ int id = menu->FindMenuIdByMenuItem(widget);
+
+ wxASSERT( id != -1 ); // should find it!
+
+ if (!menu->IsEnabled(id))
+ return;
+
+ wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id );
+ event.SetEventObject( menu );
+
+ wxEvtHandler* handler = menu->GetEventHandler();
+ if (handler && handler->ProcessEvent(event))
+ return;
+
+ wxWindow *win = menu->GetInvokingWindow();
+ if (win) win->GetEventHandler()->ProcessEvent( event );
+}
+}
+
+//-----------------------------------------------------------------------------
+// "deselect"
+//-----------------------------------------------------------------------------
+
+extern "C" {
+static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu )
+{
+ if (g_isIdle) wxapp_install_idle_handler();
+
+ int id = menu->FindMenuIdByMenuItem(widget);
+
+ wxASSERT( id != -1 ); // should find it!
+
+ if (!menu->IsEnabled(id))
+ return;
+
+ wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 );
+ event.SetEventObject( menu );
+
+ wxEvtHandler* handler = menu->GetEventHandler();
+ if (handler && handler->ProcessEvent(event))
+ return;
+
+ wxWindow *win = menu->GetInvokingWindow();
+ if (win)
+ win->GetEventHandler()->ProcessEvent( event );
+}
+}
+
+//-----------------------------------------------------------------------------
+// wxMenuItem
+//-----------------------------------------------------------------------------
+
+IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
+
+wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
+ int id,
+ const wxString& name,
+ const wxString& help,
+ wxItemKind kind,
+ wxMenu *subMenu)
+{
+ return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
+}
+
+wxMenuItem::wxMenuItem(wxMenu *parentMenu,
+ int id,
+ const wxString& text,
+ const wxString& help,
+ wxItemKind kind,
+ wxMenu *subMenu)
+ : wxMenuItemBase(parentMenu, id, text, help, kind, subMenu)
+{
+ Init(text);
+}
+
+wxMenuItem::wxMenuItem(wxMenu *parentMenu,
+ int id,
+ const wxString& text,
+ const wxString& help,
+ bool isCheckable,
+ wxMenu *subMenu)
+ : wxMenuItemBase(parentMenu, id, text, help,
+ isCheckable ? wxITEM_CHECK : wxITEM_NORMAL, subMenu)
+{
+ Init(text);
+}
+
+void wxMenuItem::Init(const wxString& text)
+{
+ m_labelWidget = (GtkWidget *) NULL;
+ m_menuItem = (GtkWidget *) NULL;
+
+ DoSetText(text);
+}
+
+wxMenuItem::~wxMenuItem()
+{
+ // don't delete menu items, the menus take care of that
+}
+
+// return the menu item text without any menu accels
+/* static */
+wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
+{
+ wxString label;
+
+ for ( const wxChar *pc = text.c_str(); *pc; pc++ )
+ {
+ if ( *pc == wxT('\t'))
+ break;
+
+ if ( *pc == wxT('_') )
+ {
+ // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx"
+ pc++;
+ label += *pc;
+ continue;
+ }
+
+ if ( (*pc == wxT('&')) && (*(pc+1) != wxT('&')) )
+ {
+ // wxMSW escapes "&"
+ // "&" is doubled to indicate "&" instead of accelerator
+ continue;
+ }
+
+ label += *pc;
+ }
+
+ // wxPrintf( wxT("GetLabelFromText(): text %s label %s\n"), text.c_str(), label.c_str() );
+
+ return label;
+}
+
+void wxMenuItem::SetText( const wxString& string )
+{
+ wxString str = string;
+ if ( str.empty() && !IsSeparator() )
+ {
+ wxASSERT_MSG(wxIsStockID(GetId()), wxT("A non-stock menu item with an empty label?"));
+ str = wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR |
+ wxSTOCK_WITH_MNEMONIC);
+ }
+
+ // Some optimization to avoid flicker
+ wxString oldLabel = m_text;
+ oldLabel = wxStripMenuCodes(oldLabel);
+ oldLabel.Replace(wxT("_"), wxEmptyString);
+ wxString label1 = wxStripMenuCodes(str);
+ wxString oldhotkey = GetHotKey(); // Store the old hotkey in Ctrl-foo format
+ wxCharBuffer oldbuf = wxGTK_CONV( GetGtkHotKey(*this) ); // and as <control>foo
+
+ DoSetText(str);
+
+ if (oldLabel == label1 &&
+ oldhotkey == GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
+ return;
+
+ if (m_menuItem)
+ {
+ GtkLabel *label;
+ if (m_labelWidget)
+ label = (GtkLabel*) m_labelWidget;
+ else
+ label = GTK_LABEL( GTK_BIN(m_menuItem)->child );
+
+ // set new text
+ gtk_label_set( label, wxGTK_CONV( m_text ) );
+
+ // reparse key accel
+ (void)gtk_label_parse_uline (GTK_LABEL(label), wxGTK_CONV(m_text) );
+ gtk_accel_label_refetch( GTK_ACCEL_LABEL(label) );
+ }
+
+ guint accel_key;
+ GdkModifierType accel_mods;
+ gtk_accelerator_parse( (const char*) oldbuf, &accel_key, &accel_mods);
+ if (accel_key != 0)
+ {
+ gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem),
+ m_parentMenu->m_accel,
+ accel_key,
+ accel_mods );
+ }
+
+ wxCharBuffer buf = wxGTK_CONV( GetGtkHotKey(*this) );
+ gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods);
+ if (accel_key != 0)
+ {
+ gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem),
+ "activate",
+ m_parentMenu->m_accel,
+ accel_key,
+ accel_mods,
+ GTK_ACCEL_VISIBLE);
+ }
+}
+
+// it's valid for this function to be called even if m_menuItem == NULL
+void wxMenuItem::DoSetText( const wxString& str )
+{
+ // '\t' is the deliminator indicating a hot key
+ m_text.Empty();
+ const wxChar *pc = str;
+ while ( (*pc != wxT('\0')) && (*pc != wxT('\t')) )
+ {
+ if ((*pc == wxT('&')) && (*(pc+1) == wxT('&')))
+ {
+ // "&" is doubled to indicate "&" instead of accelerator
+ ++pc;
+ m_text << wxT('&');
+ }
+ else if (*pc == wxT('&'))
+ {
+ m_text << wxT('_');
+ }
+ else if ( *pc == wxT('_') ) // escape underscores
+ {
+ m_text << wxT("__");
+ }
+ else
+ {
+ m_text << *pc;
+ }
+ ++pc;
+ }
+
+ m_hotKey = wxEmptyString;
+
+ if(*pc == wxT('\t'))
+ {
+ pc++;
+ m_hotKey = pc;
+ }
+}
+
+#if wxUSE_ACCEL
+
+wxAcceleratorEntry *wxMenuItem::GetAccel() const
+{
+ if ( !GetHotKey() )
+ {
+ // nothing
+ return (wxAcceleratorEntry *)NULL;
+ }
+
+ // accelerator parsing code looks for them after a TAB, so insert a dummy
+ // one here
+ wxString label;
+ label << wxT('\t') << GetHotKey();
+
+ return wxAcceleratorEntry::Create(label);
+}
+
+#endif // wxUSE_ACCEL
+
+void wxMenuItem::Check( bool check )
+{
+ wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
+
+ if (check == m_isChecked)
+ return;
+
+ wxMenuItemBase::Check( check );
+
+ switch ( GetKind() )
+ {
+ case wxITEM_CHECK:
+ case wxITEM_RADIO:
+ gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check );
+ break;
+
+ default:
+ wxFAIL_MSG( _T("can't check this item") );
+ }
+}
+
+void wxMenuItem::Enable( bool enable )
+{
+ wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
+
+ gtk_widget_set_sensitive( m_menuItem, enable );
+ wxMenuItemBase::Enable( enable );
+}
+
+bool wxMenuItem::IsChecked() const
+{
+ wxCHECK_MSG( m_menuItem, false, wxT("invalid menu item") );
+
+ wxCHECK_MSG( IsCheckable(), false,
+ wxT("can't get state of uncheckable item!") );
+
+ return ((GtkCheckMenuItem*)m_menuItem)->active != 0;
+}
+
+//-----------------------------------------------------------------------------
+// wxMenu
+//-----------------------------------------------------------------------------