No scrollbar shown on wxScrolledWindow

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
akamed
Earned a small fee
Earned a small fee
Posts: 23
Joined: Wed Jan 13, 2021 5:59 pm

No scrollbar shown on wxScrolledWindow

Post by akamed » Mon May 03, 2021 8:40 pm

Hello

I am creating a complex layout and I don't find the way to show the scrollbar on the window that I want.
It shows the place for the scrollbar, but no scrollbar.
I have tried with simpler layouts and I was able to get it work, but now I don't find where is the error.
I don't know if there's a bug or I am doing something wrong (I suppose that's). Any help would be appreciated.
The code is down, under the screenshot.
I am using wxWidgets-3.1.2, windows 10 and Codeblocks.

Thanks!

Image

Code: Select all

// For compilers that support precompilation, includes "wx/wx.h".
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
    #include <wx/wx.h>

#endif

class MyApp: public wxApp
{
public:
    virtual bool OnInit();
};

class MyFrame: public wxFrame
{
public:
    MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size): wxFrame(NULL, wxID_ANY, title, pos, size){};
};

wxIMPLEMENT_APP(MyApp);

bool MyApp::OnInit()
{
    MyFrame *frame = new MyFrame( "Hello World", wxPoint(50, 50), wxSize(850, 640) );
    wxInitAllImageHandlers();

    wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer *MainSizer = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer *subSizerH = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer *subSizerV = new wxBoxSizer(wxVERTICAL);
    wxBoxSizer *subSubSizerV1 = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer *subSubSizerV2 = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer* sizerFour = new wxBoxSizer(wxVERTICAL);
    wxBoxSizer* sizerTwo = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer* sizerFive = new wxBoxSizer(wxHORIZONTAL);

    wxScrolledWindow* panelOne = new wxScrolledWindow(frame, wxID_ANY); // ### THE SUPERIOR PANEL
    wxScrolledWindow* panelTwo = new wxScrolledWindow(frame, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxHSCROLL); // ### THE CENTER LEFT PANEL
    wxScrolledWindow* panelThree = new wxScrolledWindow(frame, wxID_ANY);  //### THE CENTER RIGHT PANEL
    wxScrolledWindow* panelFour = new wxScrolledWindow(frame, wxID_ANY);  //### THE BOTTOM LEFT PANEL
    wxScrolledWindow* panelFive = new wxScrolledWindow(frame, wxID_ANY);  //### THE BOTTOM RIGHT PANEL


    panelOne->SetMinSize(wxSize(0, 80));
    panelOne->SetMaxSize(wxSize(2048, 80));
    panelTwo->SetMinSize(wxSize(0, 480)); //0, 480
    panelTwo->SetMaxSize(wxSize(2048, 1600)); //2048, 1600
    panelThree->SetMinSize(wxSize(200, 480)); //480
    panelThree->SetMaxSize(wxSize(200, 1600));
    panelFour->SetMinSize(wxSize(0, 100));
    panelFour->SetMaxSize(wxSize(2048, 800));
    panelFive->SetMinSize(wxSize(200, 100));
    panelFive->SetMaxSize(wxSize(200, 800));



    subSizerV->Add(panelOne, -1, wxALL|wxEXPAND, 0);
    subSizerV->Add(subSubSizerV1, 1, wxALL|wxEXPAND, 0);
    subSizerV->Add(subSubSizerV2, -1, wxALL|wxEXPAND, 0);
    subSubSizerV1->Add(panelTwo, -1, wxALL|wxEXPAND, 0);
    subSubSizerV1->Add(panelThree, -1, wxALL|wxEXPAND, 0);
    subSubSizerV2->Add(panelFour, -1, wxALL|wxEXPAND, 0);
    subSubSizerV2->Add(panelFive, -1, wxALL|wxEXPAND, 0);
    MainSizer->Add(subSizerV, -1, wxALL|wxEXPAND,0);
    MainSizer->Add(subSizerH, -1, wxALL|wxEXPAND,0);

    panelTwo->SetSizerAndFit(sizerTwo);
    panelFour->SetSizer(sizerFour);
    panelFive->SetSizer(sizerFive);
    frame->SetSizer(MainSizer);

    panelTwo->SetScrollbars(500, 20, 10, 50, 0, 0);
    panelTwo->ShowScrollbars(wxSHOW_SB_ALWAYS, wxSHOW_SB_NEVER);

    panelOne->SetBackgroundColour(*wxBLACK);
    panelTwo->SetBackgroundColour(*wxBLUE);
    panelThree->SetBackgroundColour(*wxGREEN);
    panelFour->SetBackgroundColour(*wxRED);
    panelFive->SetBackgroundColour(*wxYELLOW);



    frame->Show( true );
    return true;
}

PB
Part Of The Furniture
Part Of The Furniture
Posts: 3041
Joined: Sun Jan 03, 2010 5:45 pm

Re: No scrollbar shown on wxScrolledWindow

Post by PB » Mon May 03, 2021 9:01 pm

I do not think you are supposed to do calls like Set/ShowScrollbars() with wxScrolled.

With wxScrolled, you need to:
1. If its contents is in a sizer, call its FitInside() so wxScrolled can compute its virtual size based on the sizer. If not (e.g., custom drawing or your empty panels) call SetVirtualSize() directly with the appropriate size.
2. Call SetScrollRate() with a reasonable value for scroll steps.

The scrollbars will be automatically shown/hidden based on whether the virtual size is larger/smaller than the actual client size.

BTW, your example is overly complex, you could use just one window to demonstrate the scrollbar "issue".

User avatar
doublemax
Moderator
Moderator
Posts: 16012
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: No scrollbar shown on wxScrolledWindow

Post by doublemax » Mon May 03, 2021 9:20 pm

Code: Select all

panelTwo->SetSizerAndFit(sizerTwo);
This is the main issue. If you assign a sizer to a scrolled window, the sizer takes control over the virtual size calculation. But the sizer is empty and therefore its "best size" is (0,0). If you comment out this line, the scrollbar(s) will appear as you expect.
Use the source, Luke!

akamed
Earned a small fee
Earned a small fee
Posts: 23
Joined: Wed Jan 13, 2021 5:59 pm

Re: No scrollbar shown on wxScrolledWindow

Post by akamed » Mon May 03, 2021 9:45 pm

Thanks PB for your answer.

As doublemax said:
doublemax wrote:
Mon May 03, 2021 9:20 pm

Code: Select all

panelTwo->SetSizerAndFit(sizerTwo);
This is the main issue. If you assign a sizer to a scrolled window, the sizer takes control over the virtual size calculation. But the sizer is empty and therefore its "best size" is (0,0). If you comment out this line, the scrollbar(s) will appear as you expect.
So, doublemax, how can I put a sizer into a scrolled window then?

User avatar
doublemax
Moderator
Moderator
Posts: 16012
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: No scrollbar shown on wxScrolledWindow

Post by doublemax » Mon May 03, 2021 10:03 pm

akamed wrote:
Mon May 03, 2021 9:45 pm
So, doublemax, how can I put a sizer into a scrolled window then?
Just as you did it. But as long as it's empty you'll get the same result. You'll only see scrollbars when the content requires more space than available.
Use the source, Luke!

akamed
Earned a small fee
Earned a small fee
Posts: 23
Joined: Wed Jan 13, 2021 5:59 pm

Re: No scrollbar shown on wxScrolledWindow

Post by akamed » Tue May 04, 2021 7:31 am

doublemax wrote:
Mon May 03, 2021 10:03 pm
akamed wrote:
Mon May 03, 2021 9:45 pm
So, doublemax, how can I put a sizer into a scrolled window then?
Just as you did it. But as long as it's empty you'll get the same result. You'll only see scrollbars when the content requires more space than available.
Thanks doublemax, I will try it in my full layout...

Post Reply