c++wxwidgetswxnotebook

wxPanel inside wxNotebookPage not displaying correctly


I'm somewhat new to wxWidgets and have been trying to create a notebook. The problem is that the wxPanel does not stretch to fill its parent which is the wxNotebookPage. How do I properly use panels and notebooks together while maintaining tab traversal and keyboard events?

This is my current constructor for the wxFrame

cMain::cMain() : wxFrame(nullptr, wxID_ANY, "Frame", wxDefaultPosition, wxSize(800, 500))
{
    CreateStatusBar();
    SetStatusText(wxT("Ready"), 0);
    m_notebook1 = new wxNotebook(this, wxID_ANY, wxDefaultPosition, wxDefaultSize);
    m_notebookPage1 = new wxNotebookPage(m_notebook1, -1);
    m_notebook1->AddPage(m_notebookPage1, L"Tab 1");
    m_panel1 = new wxPanel(m_notebookPage1, 4000, wxDefaultPosition, wxDefaultSize, wxWANTS_CHARS);
    m_btn1 = new wxButton(m_panel1, 10000, "Do String", wxPoint(10, 10), wxSize(150, 50));
    m_btn2 = new wxButton(m_panel1, 10001, "Load Plugins", wxPoint(165, 10), wxSize(150, 50));
    m_btn3 = new wxButton(m_panel1, 10002, "Index Files", wxPoint(320, 10), wxSize(150, 50));
    
    // Sizer, Panel and List
    m_sizer1 = new wxBoxSizer(wxVERTICAL);
    m_list1 = new wxListBox(m_panel1, 10005, wxPoint(10, 65), wxSize(760, 345));
    m_sizer1->Add(m_list1, wxSizerFlags(1).Expand().Border(wxTOP, 70));
    m_panel1->SetSizer(m_sizer1);
    m_sizer1->SetSizeHints(this);
    m_panel1->Bind(wxEVT_CHAR_HOOK, &cMain::OnChar, this);
    m_panel1->SetBackgroundColour(wxColour(0xFAF9F6));

    m_pMenuBar = new wxMenuBar();

    // File Menu
    m_pFileMenu = new wxMenu();
    m_pFileMenu->Append(wxID_NEW, _T("&New"), _T("Creates a new cache file"));
    m_pFileMenu->Append(wxID_OPEN, _T("&Open"), _T("Opens an existing cache file"));
    m_pFileMenu->Append(wxID_SAVE, _T("&Save"), _T("Save the content"));
    m_pFileMenu->Append(wxID_SAVEAS, _T("&Save As"), _T("Save a copy of the content"));
    m_pFileMenu->AppendSeparator();
    m_pFileMenu->Append(1000, _T("&Add File(s)"), _T("Manually add files"));
    m_pFileMenu->Append(1001, _T("&Search for File(s)"), _T("Search for files"));
    m_pFileMenu->Append(1002, _T("&Add Tree of File(s)"), _T("Add an entire directory of files"));
    m_pFileMenu->Enable(1000, false);
    m_pFileMenu->Enable(1001, false);
    m_pFileMenu->Enable(1002, false);
    m_pMenuBar->Append(m_pFileMenu, _T("&File"));

    // About Menu
    m_pHelpMenu = new wxMenu();
    m_pHelpMenu->Append(wxID_ABOUT, _T("&About"), _T("Shows information about the application"));
    m_pMenuBar->Append(m_pHelpMenu, _T("&Info"));

    // Plugins Menu
    m_pPluginsMenu = new wxMenu();
    m_pPluginsMenu->Append(1003, _T("&Import"), _T("Imports a plugin"));
    m_pPluginsMenu->Append(1004, _T("&Reload All"), _T("Reloads all plugins"));
    m_pPluginsMenu->AppendSeparator();
    m_pMenuBar->Append(m_pPluginsMenu, _T("&Plugins"));
    SetMenuBar(m_pMenuBar);
}

Problem


Solution

  • The reason this doesn't work is that m_notebookPage1 does nothing with its children by default, as you haven't given it any sizer — so the child just remains at its default position (0,0) with the default size.

    You can fix it by creating a sizer, adding m_panel1 to this sizer and associating the sizer with m_notebookPage1 but the simplest solution is to just get rid of m_notebookPage1 entirely as this is just an unnecessary extra window: you can pass m_panel1 itself directly to AddPage(). Of course, remember to create the panel with the notebook as the parent if you do this.