-IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
-
-wxMenuBar::wxMenuBar(void)
-{
- m_needParent = FALSE; // hmmm
-
- PreCreation( NULL, -1, wxDefaultPosition, wxDefaultSize, 0, "menu" );
-
- m_menus.DeleteContents( TRUE );
-
- m_widget = gtk_handle_box_new();
-
- m_menubar = gtk_menu_bar_new();
-
- gtk_container_add( GTK_CONTAINER(m_widget), m_menubar );
-
- gtk_widget_show( m_menubar );
-
- PostCreation();
-
- Show( TRUE );
-};
-
-void wxMenuBar::Append( wxMenu *menu, const wxString &title )
-{
- m_menus.Append( menu );
- menu->m_title = title; // ??????
-
- size_t pos;
- do {
- pos = menu->m_title.First( '&' );
- if (pos != wxString::npos) menu->m_title.Remove( pos, 1 );
- } while (pos != wxString::npos);
-
- GtkWidget *root_menu;
- root_menu = gtk_menu_item_new_with_label( WXSTRINGCAST(menu->m_title) );
- gtk_widget_show( root_menu );
- gtk_menu_item_set_submenu( GTK_MENU_ITEM(root_menu), menu->m_menu );
-
- gtk_menu_bar_append( GTK_MENU_BAR(m_menubar), root_menu );
-};
-
-int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString )
-{
- if (menu->m_title == menuString)
- {
- int res = menu->FindItem( itemString );
- if (res != -1) return res;
- };
- wxNode *node = menu->m_items.First();
- while (node)
- {
- wxMenuItem *item = (wxMenuItem*)node->Data();
- if (item->m_subMenu) return FindMenuItemRecursive( item->m_subMenu, menuString, itemString );
- node = node->Next();
- };
- return -1;
-};
+wxMenuBar::~wxMenuBar()
+{
+ if (m_widget)
+ {
+ // Work around a probable bug in Ubuntu 12.04 which causes a warning if
+ // gtk_widget_destroy() is called on a wxMenuBar attached to a frame
+ GtkWidget* widget = m_widget;
+ m_widget = NULL;
+ g_object_unref(widget);
+ }
+}
+
+void wxMenuBar::Init(size_t n, wxMenu *menus[], const wxString titles[], long style)
+{
+#if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
+ // Hildon window uses a single menu instead of a menu bar, so wxMenuBar is
+ // the same as menu in this case
+ m_widget =
+ m_menubar = gtk_menu_new();
+#else // !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
+ if (!PreCreation( NULL, wxDefaultPosition, wxDefaultSize ) ||
+ !CreateBase( NULL, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, wxT("menubar") ))
+ {
+ wxFAIL_MSG( wxT("wxMenuBar creation failed") );
+ return;
+ }
+
+ m_menubar = gtk_menu_bar_new();
+
+ if (style & wxMB_DOCKABLE)
+ {
+ m_widget = gtk_handle_box_new();
+ gtk_container_add(GTK_CONTAINER(m_widget), m_menubar);
+ gtk_widget_show(m_menubar);
+ }
+ else
+ {
+ m_widget = m_menubar;
+ }
+
+ PostCreation();
+
+ GTKApplyWidgetStyle();
+#endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2/!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
+
+ g_object_ref_sink(m_widget);
+
+ for (size_t i = 0; i < n; ++i )
+ Append(menus[i], titles[i]);
+}
+
+wxMenuBar::wxMenuBar(size_t n, wxMenu *menus[], const wxString titles[], long style)
+{
+ Init(n, menus, titles, style);
+}
+
+wxMenuBar::wxMenuBar(long style)
+{
+ Init(0, NULL, NULL, style);
+}
+
+wxMenuBar::wxMenuBar()
+{
+ Init(0, NULL, NULL, 0);
+}
+
+// recursive helpers for wxMenuBar::Attach() and Detach(): they are called to
+// associate the menus with the frame they belong to or dissociate them from it
+namespace
+{
+
+void
+DetachFromFrame(wxMenu* menu, wxFrame* frame)
+{
+ // support for native hot keys
+ if (menu->m_accel)
+ {
+ // Note that wxGetTopLevelParent() is really needed because this frame
+ // can be an MDI child frame which is a fake frame and not a TLW at all
+ GtkWindow * const tlw = GTK_WINDOW(wxGetTopLevelParent(frame)->m_widget);
+ if (g_slist_find(menu->m_accel->acceleratables, tlw))
+ gtk_window_remove_accel_group(tlw, menu->m_accel);
+ }
+
+ wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
+ while (node)
+ {
+ wxMenuItem *menuitem = node->GetData();
+ if (menuitem->IsSubMenu())
+ DetachFromFrame(menuitem->GetSubMenu(), frame);
+ node = node->GetNext();
+ }
+}
+
+void
+AttachToFrame(wxMenu* menu, wxFrame* frame)
+{
+ // support for native hot keys
+ if (menu->m_accel)
+ {
+ GtkWindow * const tlw = GTK_WINDOW(wxGetTopLevelParent(frame)->m_widget);
+ if (!g_slist_find(menu->m_accel->acceleratables, tlw))
+ gtk_window_add_accel_group(tlw, menu->m_accel);
+ }
+
+ wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
+ while (node)
+ {
+ wxMenuItem *menuitem = node->GetData();
+ if (menuitem->IsSubMenu())
+ AttachToFrame(menuitem->GetSubMenu(), frame);
+ node = node->GetNext();
+ }
+}
+
+} // anonymous namespace
+
+void wxMenuBar::SetLayoutDirection(wxLayoutDirection dir)
+{
+ if ( dir == wxLayout_Default )
+ {
+ const wxWindow *const frame = GetFrame();
+ if ( frame )
+ {
+ // inherit layout from frame.
+ dir = frame->GetLayoutDirection();
+ }
+ else // use global layout
+ {
+ dir = wxTheApp->GetLayoutDirection();
+ }
+ }
+
+ if ( dir == wxLayout_Default )
+ return;
+
+ GTKSetLayout(m_menubar, dir);
+
+ // also set the layout of all menus we already have (new ones will inherit
+ // the current layout)
+ for ( wxMenuList::compatibility_iterator node = m_menus.GetFirst();
+ node;
+ node = node->GetNext() )
+ {
+ wxMenu *const menu = node->GetData();
+ menu->SetLayoutDirection(dir);
+ }
+}
+
+wxLayoutDirection wxMenuBar::GetLayoutDirection() const
+{
+ return GTKGetLayout(m_menubar);
+}
+
+void wxMenuBar::Attach(wxFrame *frame)
+{
+ wxMenuBarBase::Attach(frame);
+
+ wxMenuList::compatibility_iterator node = m_menus.GetFirst();
+ while (node)
+ {
+ wxMenu *menu = node->GetData();
+ AttachToFrame( menu, frame );
+ node = node->GetNext();
+ }
+
+ SetLayoutDirection(wxLayout_Default);
+}
+
+void wxMenuBar::Detach()
+{
+ wxMenuList::compatibility_iterator node = m_menus.GetFirst();
+ while (node)
+ {
+ wxMenu *menu = node->GetData();
+ DetachFromFrame( menu, m_menuBarFrame );
+ node = node->GetNext();
+ }
+
+ wxMenuBarBase::Detach();
+}
+
+bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
+{
+ if (wxMenuBarBase::Append(menu, title))
+ {
+ GtkAppend(menu, title);
+ return true;
+ }
+ return false;
+}
+
+void wxMenuBar::GtkAppend(wxMenu* menu, const wxString& title, int pos)
+{
+ menu->SetLayoutDirection(GetLayoutDirection());
+
+#if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
+ // if the menu has only one item, append it directly to the top level menu
+ // instead of inserting a useless submenu
+ if ( menu->GetMenuItemCount() == 1 )
+ {
+ wxMenuItem * const item = menu->FindItemByPosition(0);
+
+ // remove both mnemonics and accelerator: neither is useful under Maemo
+ const wxString str(wxStripMenuCodes(item->GetItemLabel()));
+
+ if ( item->IsSubMenu() )
+ {
+ GtkAppend(item->GetSubMenu(), str, pos);
+ return;
+ }
+
+ menu->m_owner = gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str ) );
+
+ g_signal_connect(menu->m_owner, "activate",
+ G_CALLBACK(menuitem_activate), item);
+ item->SetMenuItem(menu->m_owner);
+ }
+ else
+#endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2 /!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
+ {
+ const wxString str(wxConvertMnemonicsToGTK(title));
+
+ // This doesn't have much effect right now.
+ menu->SetTitle( str );
+
+ // The "m_owner" is the "menu item"
+ menu->m_owner = gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str ) );
+
+ gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu );
+ }
+
+ gtk_widget_show( menu->m_owner );
+
+ if (pos == -1)
+ gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar), menu->m_owner );
+ else
+ gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar), menu->m_owner, pos );
+
+ if ( m_menuBarFrame )
+ AttachToFrame( menu, m_menuBarFrame );
+}
+
+bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
+{
+ if (wxMenuBarBase::Insert(pos, menu, title))
+ {
+ GtkAppend(menu, title, int(pos));
+ return true;
+ }
+ return false;
+}
+
+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 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 NULL;
+
+ gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu->m_owner), NULL);
+ gtk_container_remove(GTK_CONTAINER(m_menubar), menu->m_owner);
+
+ gtk_widget_destroy( menu->m_owner );
+ menu->m_owner = NULL;
+
+ DetachFromFrame( menu, m_menuBarFrame );
+
+ return menu;
+}
+
+static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString )
+{
+ if (wxMenuItem::GetLabelText(menu->GetTitle()) == wxMenuItem::GetLabelText(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;
+}