+ wxMenuList::compatibility_iterator node = m_menus.GetFirst();
+ while (node)
+ {
+ wxMenu *menu = node->GetData();
+ int res = FindMenuItemRecursive( menu, menuString, itemString);
+ if (res != -1)
+ return res;
+ node = node->GetNext();
+ }
+
+ return wxNOT_FOUND;
+}
+
+// Find a wxMenuItem using its id. Recurses down into sub-menus
+static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id)
+{
+ wxMenuItem* result = menu->FindChildItem(id);
+
+ wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
+ while ( node && result == NULL )
+ {
+ wxMenuItem *item = node->GetData();
+ if (item->IsSubMenu())
+ {
+ result = FindMenuItemByIdRecursive( item->GetSubMenu(), id );
+ }
+ node = node->GetNext();
+ }
+
+ return result;
+}
+
+wxMenuItem* wxMenuBar::FindItem( int id, wxMenu **menuForItem ) const
+{
+ wxMenuItem* result = 0;
+ wxMenuList::compatibility_iterator node = m_menus.GetFirst();
+ while (node && result == 0)
+ {
+ wxMenu *menu = node->GetData();
+ result = FindMenuItemByIdRecursive( menu, id );
+ node = node->GetNext();
+ }
+
+ if ( menuForItem )
+ {
+ *menuForItem = result ? result->GetMenu() : (wxMenu *)NULL;
+ }
+
+ return result;
+}
+
+void wxMenuBar::EnableTop( size_t pos, bool flag )
+{
+ wxMenuList::compatibility_iterator node = m_menus.Item( pos );
+
+ wxCHECK_RET( node, wxT("menu not found") );
+
+ wxMenu* menu = node->GetData();
+
+ if (menu->m_owner)
+ gtk_widget_set_sensitive( menu->m_owner, flag );
+}
+
+wxString wxMenuBar::GetMenuLabel( size_t pos ) const
+{
+ wxMenuList::compatibility_iterator node = m_menus.Item( pos );
+
+ wxCHECK_MSG( node, wxT("invalid"), wxT("menu not found") );
+
+ wxMenu* menu = node->GetData();
+
+ return wxConvertMnemonicsFromGTK(menu->GetTitle());
+}
+
+void wxMenuBar::SetMenuLabel( size_t pos, const wxString& label )
+{
+ wxMenuList::compatibility_iterator node = m_menus.Item( pos );
+
+ wxCHECK_RET( node, wxT("menu not found") );
+
+ wxMenu* menu = node->GetData();
+
+ const wxString str(wxConvertMnemonicsToGTK(label));
+
+ menu->SetTitle( str );
+
+ if (menu->m_owner)
+ gtk_label_set_text_with_mnemonic( GTK_LABEL( GTK_BIN(menu->m_owner)->child), wxGTK_CONV(str) );
+}
+
+//-----------------------------------------------------------------------------
+// "activate"
+//-----------------------------------------------------------------------------
+
+extern "C" {
+static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu )
+{
+ 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());
+
+ frame->HandleWindowEvent(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 )
+{
+ 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->SafelyProcessEvent(event))
+ return;
+
+ wxWindow *win = menu->GetInvokingWindow();
+ if (win) win->HandleWindowEvent( event );
+}
+}
+
+//-----------------------------------------------------------------------------
+// "deselect"
+//-----------------------------------------------------------------------------
+
+extern "C" {
+static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu )
+{
+ 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->SafelyProcessEvent(event))
+ return;
+
+ wxWindow *win = menu->GetInvokingWindow();
+ if (win)
+ win->HandleWindowEvent( 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);
+}
+
+#if WXWIN_COMPATIBILITY_2_8
+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);
+}
+#endif
+
+void wxMenuItem::Init(const wxString& text)
+{
+ 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::GetLabelText(const wxString& text)
+{
+ // The argument to this function will now always be in wxWidgets standard label
+ // format, not GTK+ format, so we do what the other ports do.
+
+ return wxStripMenuCodes(text);
+
+#if 0
+ 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('\\') )
+ {
+ // GTK 2.0 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("GetLabelText(): text %s label %s\n"), text.c_str(), label.c_str() );
+
+ return label;
+#endif
+}
+
+wxString wxMenuItem::GetItemLabel() const
+{
+ wxString label = wxConvertMnemonicsFromGTK(m_text);
+ if (!m_hotKey.IsEmpty())
+ label << "\t" << m_hotKey;
+ return label;
+}
+
+void wxMenuItem::SetItemLabel( const wxString& str )
+{
+ // cache some data which must be used later
+ bool isstock = wxIsStockID(GetId());
+ const char *stockid = NULL;
+ if (isstock)
+ stockid = wxGetStockGtkID(GetId());
+
+ // Some optimization to avoid flicker
+ wxString oldLabel = m_text;
+ oldLabel = wxStripMenuCodes(oldLabel);
+ oldLabel.Replace(wxT("_"), wxT(""));
+ wxString label1 = wxStripMenuCodes(str);
+#if wxUSE_ACCEL
+ wxString oldhotkey = m_hotKey; // Store the old hotkey in Ctrl-foo format
+ wxCharBuffer oldbuf = wxGTK_CONV_SYS( GetGtkHotKey(*this) ); // and as <control>foo
+#endif // wxUSE_ACCEL
+
+ DoSetText(str);
+
+#if wxUSE_ACCEL
+ if (oldLabel == label1 &&
+ oldhotkey == m_hotKey) // Make sure we can change a hotkey even if the label is unaltered
+ return;
+
+ if (m_menuItem)
+ {
+ // stock menu items can have empty labels:
+ wxString text = m_text;
+ if (text.IsEmpty() && !IsSeparator())
+ {
+ wxASSERT_MSG(isstock, wxT("A non-stock menu item with an empty label?"));
+ text = wxGetStockLabel(GetId());
+
+ // need & => _ conversion
+ text = GTKProcessMenuItemLabel(text, NULL);
+ }
+
+ GtkLabel* label = GTK_LABEL(GTK_BIN(m_menuItem)->child);
+ gtk_label_set_text_with_mnemonic(label, wxGTK_CONV_SYS(text));
+ }
+
+ // remove old accelerator from our parent's accelerator group, if present
+ guint accel_key;
+ GdkModifierType accel_mods;
+ if (oldbuf[(size_t)0] != '\0')
+ {
+ gtk_accelerator_parse( (const char*) oldbuf, &accel_key, &accel_mods);
+ if (accel_key != 0)
+ {
+ gtk_widget_remove_accelerator(m_menuItem,
+ m_parentMenu->m_accel,
+ accel_key,
+ accel_mods );
+ }
+ }
+ else if (isstock)
+ {
+ // if the accelerator was taken from a stock ID, just get it back from GTK+ stock
+ if (wxGetStockGtkAccelerator(stockid, &accel_mods, &accel_key))
+ gtk_widget_remove_accelerator( m_menuItem,
+ m_parentMenu->m_accel,
+ accel_key,
+ accel_mods );
+ }
+
+ // add new accelerator to our parent's accelerator group
+ wxCharBuffer buf = wxGTK_CONV_SYS( GetGtkHotKey(*this) );
+ if (buf[(size_t)0] != '\0')
+ {
+ gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods);
+ if (accel_key != 0)
+ {
+ gtk_widget_add_accelerator( m_menuItem,
+ "activate",
+ m_parentMenu->m_accel,
+ accel_key,
+ accel_mods,
+ GTK_ACCEL_VISIBLE);
+ }
+ }
+ else if (isstock)
+ {
+ // if the accelerator was taken from a stock ID, just get it back from GTK+ stock
+ if (wxGetStockGtkAccelerator(stockid, &accel_mods, &accel_key))
+ gtk_widget_remove_accelerator( m_menuItem,
+ m_parentMenu->m_accel,
+ accel_key,
+ accel_mods );
+ }
+#endif // wxUSE_ACCEL
+}
+
+// NOTE: this function is different from the similar functions GTKProcessMnemonics()
+// implemented in control.cpp and from wxMenuItemBase::GetLabelText...
+// so there's no real code duplication
+wxString wxMenuItem::GTKProcessMenuItemLabel(const wxString& str, wxString *hotKey)
+{
+ wxString text;
+
+ // '\t' is the deliminator indicating a hot key
+ wxString::const_iterator pc = str.begin();
+ while ( pc != str.end() && *pc != wxT('\t') )
+ {
+ if (*pc == wxT('&'))
+ {
+ wxString::const_iterator next = pc + 1;
+ if (next != str.end() && *next == wxT('&'))
+ {
+ // "&" is doubled to indicate "&" instead of accelerator
+ ++pc;
+ text << wxT('&');
+ }
+ else
+ {
+ text << wxT('_');
+ }
+ }
+ else if ( *pc == wxT('_') ) // escape underscores
+ {
+ text << wxT("__");
+ }
+ else
+ {
+ text << *pc;
+ }
+ ++pc;
+ }
+
+ if (hotKey)
+ {
+ hotKey->Empty();
+ if(*pc == wxT('\t'))
+ {
+ ++pc;
+ hotKey->assign(pc, str.end());
+ }
+ }
+
+ return text;
+}
+
+// it's valid for this function to be called even if m_menuItem == NULL
+void wxMenuItem::DoSetText( const wxString& str )
+{
+ m_text.Empty();
+ m_text = GTKProcessMenuItemLabel(str, &m_hotKey);
+}
+
+#if wxUSE_ACCEL
+
+wxAcceleratorEntry *wxMenuItem::GetAccel() const
+{
+ if (m_hotKey.empty())
+ {
+ // nothing
+ return NULL;
+ }
+
+ // accelerator parsing code looks for them after a TAB, so insert a dummy
+ // one here
+ wxString label;
+ label << wxT('\t') << m_hotKey;
+
+ 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_active( (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;
+}
+