wxNotebook tab - have to click another tab and click back 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.
Post Reply
daddydave
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 214
Joined: Wed Jun 15, 2005 3:31 am
Location: United States
Contact:

wxNotebook tab - have to click another tab and click back

Post by daddydave » Mon Sep 26, 2005 1:12 pm

After opening a file which is supposed to populate the first tab in a wxNotebook, that tab does not display its content until I click on another tab and then click back to the original (tab zero). I thought this was a Freeze()/Thaw() issue but finally I commented out all the Freeze() calls I had. For a while, I thought it was something in my OnPageChanged (and I did have some bugs in it that I fixed), but I am calling UpdateCurrentPage directly after loading the file now. I tried a Refresh and Update of both the frame and the panel, but got nothing from it. Finally I found a simple workaround (I thought I had tried this already, but apparently not) which works:

Code: Select all

nbLearn->SetSelection(1); // select the second tab
nbLearn->SetSelection(0); // go back to first tab
But normally I shouldn't have to do this, right?

Note: all tabs are using the same panel, in case that matters.

Here is the (mostly) relevant code:

Code: Select all

void frmLearn::OnOpen(wxCommandEvent& event)
{
    wxFileDialog *OpenDialog = new wxFileDialog(this, "Choose a pack of cards", "", "", 
			"Tilde separated values (*.tsv)|*.tsv", wxOPEN, wxDefaultPosition);
			

    if (OpenDialog->ShowModal() == wxID_OK) // if the user click "Open" instead of "cancel"
    {
        LoadPack(OpenDialog->GetPath());
        SetTitle(OpenDialog->GetFilename() + _(" - Card Liberty (Learn)"));
        SetStatusText(_("Opened deck ") + OpenDialog->GetFilename());
    }

    event.Skip();
}

// ...

void frmLearn::OnNotebookPageChanged(wxNotebookEvent& event)
{

    if ( this->wxWindow::IsShown() )   // prevents crash when event fires during construction
    {
        int sel = event.GetSelection();
        m_currentBox = sel + 1; // no box zero
        DrawCurrentPage();
    }
    this->Refresh();
    this->Update();
    event.Skip();
}


// ....

void frmLearn::LoadPack(const wxString& cardPackPath)
{
    wxString str;
    wxTextFile file;
    if (file.Open(cardPackPath))
    {
        for ( str = file.GetFirstLine(); !file.Eof(); str = file.GetNextLine() )
        {
            LoadCard(str);
        }
        LoadCard(str);
        file.Close();
        m_numBoxes = 5; //  Recommended number of boxes may vary per pack
        m_currentBox = 1; // no box zero
        nbLearn->SetSelection(1); // added to troubleshoot not showing the
// tab zero correctly until clicking on another tab first --- and this works around the problem FINALLY! but why do I have to do this?

        nbLearn->SetSelection(0);
        DrawCurrentPage();

    }
    else
    {
        SetStatusText(_("Unable to open ") + cardPackPath);
        wxMessageDialog(this, 
                        _("Unable to open ") + cardPackPath,
                        _("Error"),
                        wxOK);
    }
}

// ...

void frmLearn::DrawCurrentPage()
{
    int cardsInBox = m_cardList->CountBox(m_currentBox);
    wxString st;
    st << cardsInBox << _(" cards in Section ") << m_currentBox;
    SetStatusText(st);
    if (cardsInBox > 0)
    {
        m_ndx = m_cardList->GetBoxCard(m_currentBox, 0);
        txtQuestion->Thaw();
        txtAnswer->Thaw();
        btnCheckAnswer->Thaw();
        txtQuestion->SetValue((*m_cardList)[m_ndx].GetQuestion());
        txtAnswer->SetValue(wxT(""));
        btnCheckAnswer->SetLabel(_("Check &Answer"));
    }
    else
    {
        txtQuestion->SetValue(_("No cards in this section"));
//        txtAnswer->Freeze();
//        btnCheckAnswer->Freeze();
    }
    this->Refresh();
    this->Update();
}


daddydave
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 214
Joined: Wed Jun 15, 2005 3:31 am
Location: United States
Contact:

Post by daddydave » Mon Sep 26, 2005 5:23 pm

This is apparently asked a lot on the mailing lists and normally it does not get an answer, but I did happen upon this one which I almost overlooked:

http://thread.gmane.org/gmane.comp.lib. ... neral/5835

Oddly, the original question is gone, so I have to guess at what the problem was, but it still came up searching for wxNotebook setselection refresh.

It says

Hi,
try to place a wxPanel on your wxFrame, then the wxNotebook on this panel.
I also had problems (with the focus among notebook page's widgets, for
example) before doing this.

HTH,

Cristina.
Since I had a panel on the notebook, I had not noticed my frame itself did not have a panel. So I will try that as soon as I can and let you know how it goes.

daddydave
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 214
Joined: Wed Jun 15, 2005 3:31 am
Location: United States
Contact:

Post by daddydave » Wed Sep 28, 2005 5:34 pm

The tip I found didn't help. Even if the notebook has a parent panel I still have the same issue.

As I said, it works if I do this:

Code: Select all

nbLearn->SetSelection(1); // select the second tab
nbLearn->SetSelection(0); // go back to first tab
 
But it doesn't make sense to me to have to do that just do get tab 0 to display properly, and it is looking like I may have to do this with other changes I make to the control content also.

Post Reply