+
+GtkType gtk_pixmap_menu_item_get_type (void);
+GtkWidget* gtk_pixmap_menu_item_new (void);
+void gtk_pixmap_menu_item_set_pixmap (GtkPixmapMenuItem *menu_item,
+ GtkWidget *pixmap);
+
+#endif // USE_MENU_BITMAPS
+
+//-----------------------------------------------------------------------------
+// idle system
+//-----------------------------------------------------------------------------
+
+static wxString wxReplaceUnderscore( const wxString& title )
+{
+ const wxChar *pc;
+
+ /* GTK 1.2 wants to have "_" instead of "&" for accelerators */
+ wxString str;
+ pc = title;
+ while (*pc != wxT('\0'))
+ {
+ if ((*pc == wxT('&')) && (*(pc+1) == wxT('&')))
+ {
+ // "&" is doubled to indicate "&" instead of accelerator
+ ++pc;
+ str << wxT('&');
+ }
+ else if (*pc == wxT('&'))
+ {
+#if GTK_CHECK_VERSION(1, 2, 0)
+ str << wxT('_');
+#endif
+ }
+#if GTK_CHECK_VERSION(2, 0, 0)
+ else if (*pc == wxT('/'))
+ {
+ str << wxT("\\/");
+ }
+ else if (*pc == wxT('\\'))
+ {
+ str << wxT("\\\\");
+ }
+#elif GTK_CHECK_VERSION(1, 2, 0)
+ else if (*pc == wxT('/'))
+ {
+ str << wxT('\\');
+ }
+#endif
+ else
+ {
+#ifdef __WXGTK12__
+ if ( *pc == wxT('_') )
+ {
+ // underscores must be doubled to prevent them from being
+ // interpreted as accelerator character prefix by GTK
+ str << *pc;
+ }
+#endif // GTK+ 1.2
+
+ str << *pc;
+ }
+ ++pc;
+ }
+ return str;
+}
+
+//-----------------------------------------------------------------------------
+// activate message from GTK
+//-----------------------------------------------------------------------------
+
+static void gtk_menu_open_callback( GtkWidget *widget, wxMenu *menu )
+{
+ if (g_isIdle) wxapp_install_idle_handler();
+
+ wxMenuEvent event( wxEVT_MENU_OPEN, -1, menu );
+ event.SetEventObject( menu );
+
+ wxEvtHandler* handler = menu->GetEventHandler();
+ if (handler && handler->ProcessEvent(event))
+ return;
+
+ wxWindow *win = menu->GetInvokingWindow();
+ if (win) win->GetEventHandler()->ProcessEvent( event );
+}
+
+//-----------------------------------------------------------------------------
+// wxMenuBar
+//-----------------------------------------------------------------------------
+
+IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
+
+wxMenuBar::wxMenuBar( long style )
+{
+ /* the parent window is known after wxFrame::SetMenu() */
+ m_needParent = FALSE;
+ m_style = style;
+ m_invokingWindow = (wxWindow*) NULL;
+
+ if (!PreCreation( (wxWindow*) NULL, wxDefaultPosition, wxDefaultSize ) ||
+ !CreateBase( (wxWindow*) NULL, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, wxT("menubar") ))
+ {
+ wxFAIL_MSG( wxT("wxMenuBar creation failed") );
+ return;
+ }
+
+ /* 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)
+ m_accel = gtk_accel_group_new();
+ m_factory = gtk_item_factory_new( GTK_TYPE_MENU_BAR, "<main>", m_accel );
+ m_menubar = gtk_item_factory_get_widget( m_factory, "<main>" );
+#else
+ m_menubar = gtk_menu_bar_new();
+#endif
+
+ if (style & wxMB_DOCKABLE)
+ {
+ m_widget = gtk_handle_box_new();
+ gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_menubar) );
+ gtk_widget_show( GTK_WIDGET(m_menubar) );
+ }
+ else
+ {
+ m_widget = GTK_WIDGET(m_menubar);
+ }
+
+ PostCreation();
+
+ ApplyWidgetStyle();
+}
+
+wxMenuBar::wxMenuBar()
+{
+ /* the parent window is known after wxFrame::SetMenu() */
+ m_needParent = FALSE;
+ m_style = 0;
+ m_invokingWindow = (wxWindow*) NULL;
+
+ if (!PreCreation( (wxWindow*) NULL, wxDefaultPosition, wxDefaultSize ) ||
+ !CreateBase( (wxWindow*) NULL, -1, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, wxT("menubar") ))
+ {
+ wxFAIL_MSG( wxT("wxMenuBar creation failed") );
+ return;
+ }
+
+ /* 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)
+ m_accel = gtk_accel_group_new();
+ m_factory = gtk_item_factory_new( GTK_TYPE_MENU_BAR, "<main>", m_accel );
+ m_menubar = gtk_item_factory_get_widget( m_factory, "<main>" );
+#else
+ m_menubar = gtk_menu_bar_new();
+#endif
+
+ m_widget = GTK_WIDGET(m_menubar);
+
+ PostCreation();
+
+ ApplyWidgetStyle();
+}
+
+wxMenuBar::~wxMenuBar()
+{
+// gtk_object_unref( GTK_OBJECT(m_factory) ); why not ?
+}
+
+static void wxMenubarUnsetInvokingWindow( wxMenu *menu, wxWindow *win )
+{
+ menu->SetInvokingWindow( (wxWindow*) NULL );
+
+ wxWindow *top_frame = win;
+ while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
+ top_frame = top_frame->GetParent();
+
+ /* support for native hot keys */
+ gtk_accel_group_detach( menu->m_accel, ACCEL_OBJ_CAST(top_frame->m_widget) );
+
+ wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
+ while (node)
+ {
+ wxMenuItem *menuitem = node->GetData();
+ if (menuitem->IsSubMenu())
+ wxMenubarUnsetInvokingWindow( menuitem->GetSubMenu(), win );
+ node = node->GetNext();
+ }
+}
+
+static void wxMenubarSetInvokingWindow( wxMenu *menu, wxWindow *win )
+{
+ menu->SetInvokingWindow( 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 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::compatibility_iterator 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::compatibility_iterator 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::compatibility_iterator 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
+
+ gtk_signal_connect( GTK_OBJECT(menu->m_owner), "activate",
+ GTK_SIGNAL_FUNC(gtk_menu_open_callback),
+ (gpointer)menu );
+
+ // 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;
+}
+
+static wxMenu *CopyMenu (wxMenu *menu)
+{
+ wxMenu *menucopy = new wxMenu ();
+ wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
+ while (node)
+ {
+ wxMenuItem *item = node->GetData();
+ int itemid = item->GetId();
+ wxString text = item->GetText();
+ text.Replace(wxT("_"), wxT("&"));
+ wxMenu *submenu = item->GetSubMenu();
+ if (!submenu)
+ {
+ wxMenuItem* itemcopy = new wxMenuItem(menucopy,
+ itemid, text,
+ menu->GetHelpString(itemid));
+ itemcopy->SetBitmap(item->GetBitmap());
+ itemcopy->SetCheckable(item->IsCheckable());
+ menucopy->Append(itemcopy);
+ }
+ else
+ menucopy->Append (itemid, text, CopyMenu(submenu),
+ menu->GetHelpString(itemid));
+
+ node = node->GetNext();
+ }
+
+ return menucopy;
+}
+
+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 ) );
+*/
+
+ wxMenu *menucopy = CopyMenu( menu );
+
+ // 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 );
+ delete menu;
+
+ menu = menucopy;
+/*
+ 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;
+}
+
+static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString )
+{
+ if (wxMenuItem::GetLabelFromText(menu->GetTitle()) == wxMenuItem::GetLabelFromText(menuString))
+ {
+ int res = menu->FindItem( itemString );
+ if (res != wxNOT_FOUND)
+ return res;
+ }
+
+ wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
+ while (node)
+ {
+ wxMenuItem *item = node->GetData();
+ if (item->IsSubMenu())
+ return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString);
+
+ node = node->GetNext();
+ }
+
+ return wxNOT_FOUND;
+}
+