+#if GTK_CHECK_VERSION(1, 2, 1)
+ wxWindow *top_frame = win;
+ while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
+ top_frame = top_frame->GetParent();
+
+ /* support for native hot keys */
+ ACCEL_OBJECT *obj = ACCEL_OBJ_CAST(top_frame->m_widget);
+ if ( !g_slist_find( ACCEL_OBJECTS(menu->m_accel), obj ) )
+ gtk_accel_group_attach( menu->m_accel, obj );
+#endif // GTK+ 1.2.1+
+
+ wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst();
+ while (node)
+ {
+ wxMenuItem *menuitem = node->GetData();
+ if (menuitem->IsSubMenu())
+ wxMenubarSetInvokingWindow( menuitem->GetSubMenu(), win );
+ node = node->GetNext();
+ }
+}
+
+void wxMenuBar::SetInvokingWindow( wxWindow *win )
+{
+ m_invokingWindow = win;
+#if GTK_CHECK_VERSION(1, 2, 1)
+ wxWindow *top_frame = win;
+ while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
+ top_frame = top_frame->GetParent();
+
+ /* support for native key accelerators indicated by underscroes */
+ ACCEL_OBJECT *obj = ACCEL_OBJ_CAST(top_frame->m_widget);
+ if ( !g_slist_find( ACCEL_OBJECTS(m_accel), obj ) )
+ gtk_accel_group_attach( m_accel, obj );
+#endif // GTK+ 1.2.1+
+
+ wxMenuList::Node *node = m_menus.GetFirst();
+ while (node)
+ {
+ wxMenu *menu = node->GetData();
+ wxMenubarSetInvokingWindow( menu, win );
+ node = node->GetNext();
+ }
+}
+
+void wxMenuBar::UnsetInvokingWindow( wxWindow *win )
+{
+ m_invokingWindow = (wxWindow*) NULL;
+#if GTK_CHECK_VERSION(1, 2, 1)
+ wxWindow *top_frame = win;
+ while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
+ top_frame = top_frame->GetParent();
+
+ /* support for native key accelerators indicated by underscroes */
+ gtk_accel_group_detach( m_accel, ACCEL_OBJ_CAST(top_frame->m_widget) );
+#endif // GTK+ 1.2.1+
+
+ wxMenuList::Node *node = m_menus.GetFirst();
+ while (node)
+ {
+ wxMenu *menu = node->GetData();
+ wxMenubarUnsetInvokingWindow( menu, win );
+ node = node->GetNext();
+ }
+}
+
+bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
+{
+ if ( !wxMenuBarBase::Append( menu, title ) )
+ return FALSE;
+
+ return GtkAppend(menu, title);
+}
+
+bool wxMenuBar::GtkAppend(wxMenu *menu, const wxString& title)
+{
+ wxString str( wxReplaceUnderscore( title ) );
+
+ /* this doesn't have much effect right now */
+ menu->SetTitle( str );
+
+ /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
+#if GTK_CHECK_VERSION(1, 2, 1)
+
+ wxString buf;
+ buf << wxT('/') << str.c_str();
+
+ /* local buffer in multibyte form */
+ char cbuf[400];
+ strcpy(cbuf, wxGTK_CONV(buf) );
+
+ GtkItemFactoryEntry entry;
+ entry.path = (gchar *)cbuf; // const_cast
+ entry.accelerator = (gchar*) NULL;
+ entry.callback = (GtkItemFactoryCallback) NULL;
+ entry.callback_action = 0;
+ entry.item_type = (char *)"<Branch>";
+
+ gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
+ /* in order to get the pointer to the item we need the item text _without_ underscores */
+ wxString tmp = wxT("<main>/");
+ const wxChar *pc;
+ for ( pc = str; *pc != wxT('\0'); pc++ )
+ {
+ // contrary to the common sense, we must throw out _all_ underscores,
+ // (i.e. "Hello__World" => "HelloWorld" and not "Hello_World" as we
+ // might naively think). IMHO it's a bug in GTK+ (VZ)
+ while (*pc == wxT('_'))
+ pc++;
+ tmp << *pc;
+ }
+ menu->m_owner = gtk_item_factory_get_item( m_factory, wxGTK_CONV( tmp ) );
+ gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu );
+#else
+
+ menu->m_owner = gtk_menu_item_new_with_label( wxGTK_CONV( str ) );
+ gtk_widget_show( menu->m_owner );
+ gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu );
+
+ gtk_menu_bar_append( GTK_MENU_BAR(m_menubar), menu->m_owner );
+
+#endif
+
+ // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
+ // addings menu later on.
+ if (m_invokingWindow)
+ {
+ wxMenubarSetInvokingWindow( menu, m_invokingWindow );
+
+ // OPTIMISE ME: we should probably cache this, or pass it
+ // directly, but for now this is a minimal
+ // change to validate the new dynamic sizing.
+ // see (and refactor :) similar code in Remove
+ // below.
+
+ wxFrame *frame = wxDynamicCast( m_invokingWindow, wxFrame );
+
+ if( frame )
+ frame->UpdateMenuBarSize();
+ }
+
+ return TRUE;
+}
+
+bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
+{
+ if ( !wxMenuBarBase::Insert(pos, menu, title) )
+ return FALSE;
+
+ // GTK+ doesn't have a function to insert a menu using GtkItemFactory (as
+ // of version 1.2.6), so we first append the item and then change its
+ // index
+ if ( !GtkAppend(menu, title) )
+ return FALSE;
+
+ if (pos+1 >= m_menus.GetCount())
+ return TRUE;
+
+ GtkMenuShell *menu_shell = GTK_MENU_SHELL(m_factory->widget);
+ gpointer data = g_list_last(menu_shell->children)->data;
+ menu_shell->children = g_list_remove(menu_shell->children, data);
+ menu_shell->children = g_list_insert(menu_shell->children, data, pos);
+
+ return TRUE;
+}
+
+wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
+{
+ // remove the old item and insert a new one
+ wxMenu *menuOld = Remove(pos);
+ if ( menuOld && !Insert(pos, menu, title) )
+ {
+ return (wxMenu*) NULL;
+ }
+
+ // either Insert() succeeded or Remove() failed and menuOld is NULL
+ return menuOld;
+}
+
+wxMenu *wxMenuBar::Remove(size_t pos)
+{
+ wxMenu *menu = wxMenuBarBase::Remove(pos);
+ if ( !menu )
+ return (wxMenu*) NULL;
+
+/*
+ GtkMenuShell *menu_shell = GTK_MENU_SHELL(m_factory->widget);
+
+ printf( "factory entries before %d\n", (int)g_slist_length(m_factory->items) );
+ printf( "menu shell entries before %d\n", (int)g_list_length( menu_shell->children ) );
+*/
+
+ // unparent calls unref() and that would delete the widget so we raise
+ // the ref count to 2 artificially before invoking unparent.
+ gtk_widget_ref( menu->m_menu );
+ gtk_widget_unparent( menu->m_menu );
+
+ gtk_widget_destroy( menu->m_owner );
+
+/*
+ printf( "factory entries after %d\n", (int)g_slist_length(m_factory->items) );
+ printf( "menu shell entries after %d\n", (int)g_list_length( menu_shell->children ) );
+*/
+
+ if (m_invokingWindow)
+ {
+ // OPTIMISE ME: see comment in GtkAppend
+
+ wxFrame *frame = wxDynamicCast( m_invokingWindow, wxFrame );
+
+ if( frame )
+ frame->UpdateMenuBarSize();
+ }
+
+ return menu;