wxAuiNotebook: How to auto scroll to last opened one Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
csniper
Experienced Solver
Experienced Solver
Posts: 53
Joined: Mon Mar 13, 2017 8:27 am

wxAuiNotebook: How to auto scroll to last opened one

Post by csniper »

Any idea to automatically scroll to last opened one when there are lot of opened tabs and the last one is "hidden". I have to click the ">" button to scroll to the last tab I opened.

SetSelection seemed not working. Should I emit any Event?
User avatar
T-Rex
Moderator
Moderator
Posts: 1249
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine

Re: wxAuiNotebook: How to auto scroll to last opened one

Post by T-Rex »

Pretty sure that the code below can be improved much, but looks like it does what you want.

Code: Select all

wxAuiTabCtrl * GetTabCtrl(wxWindow * parent)
{
	for (auto node = parent->GetChildren().GetFirst(); node; node = node->GetNext())
	{
		if (!node->GetData()) continue;
		if (node->GetData()->IsKindOf(CLASSINFO(wxAuiTabCtrl))) return (wxAuiTabCtrl *)(node->GetData());
		wxAuiTabCtrl * found = GetTabCtrl(node->GetData());
		if (found) return found;
	}
	return nullptr;
}

void TestFrame::CreateControls()
{    
////@begin TestFrame content construction
    TestFrame* itemFrame1 = this;

    m_Notebook = new wxAuiNotebook( itemFrame1, ID_AUINOTEBOOK, wxDefaultPosition, wxDefaultSize, wxAUI_NB_DEFAULT_STYLE|wxAUI_NB_TOP );

////@end TestFrame content construction
	for (size_t i = 0; i < 20; ++i)
	{
		m_Notebook->AddPage(new wxPanel(m_Notebook, wxID_ANY), wxT("TAB"));
	}
	m_Notebook->AddPage(new wxPanel(m_Notebook, wxID_ANY), wxT("Last"));
	m_Notebook->SetSelection(m_Notebook->GetPageCount() - 1);

	wxAuiTabCtrl * tabCtrl = GetTabCtrl(m_Notebook);

	for (size_t i = 0; i < m_Notebook->GetPageCount()-1; ++i)
	{
		wxAuiNotebookEvent ev(wxEVT_AUINOTEBOOK_BUTTON, wxAUI_BUTTON_RIGHT);
		ev.SetSelection(m_Notebook->GetPageCount() - 1);
		ev.SetInt(wxAUI_BUTTON_RIGHT);
		ev.SetEventObject(tabCtrl);
		tabCtrl->GetEventHandler()->AddPendingEvent(ev);

	}
}
csniper
Experienced Solver
Experienced Solver
Posts: 53
Joined: Mon Mar 13, 2017 8:27 am

Re: wxAuiNotebook: How to auto scroll to last opened one

Post by csniper »

Thanks, @T-Rex. That's what I was thinking. Just wonder why it does not do it automatically.
csniper
Experienced Solver
Experienced Solver
Posts: 53
Joined: Mon Mar 13, 2017 8:27 am

Re: wxAuiNotebook: How to auto scroll to last opened one

Post by csniper »

After investigating wxAuiNotebook code, I finally hacked it by add the fucntion below to my wrapper class of wxAuiNotebook
void FocusSelection( ) {
if (GetPageCount() > 1) {
int activePage = m_tabs.GetActivePage();
if(activePage != -1) {
wxAuiTabCtrl* ctrl;
int ctrl_idx;
wxAuiNotebookPage& page_info = m_tabs.GetPage(activePage);
if (FindTab(page_info.window, &ctrl, &ctrl_idx)) {
ctrl->MakeTabVisible(ctrl_idx, ctrl);
}
}
}
}