Scrolling with SetTargetWindow (small layout issue)
Posted: Tue Dec 31, 2019 10:34 am
Hello all,
Starting from the scrolling code at https://wiki.wxwidgets.org/Scrolling (to which I've made as few changes as possible...), I wanted to test the use of SetTargetWindow. The final goal is to scroll a left panel with a right wxScrolledWindow (so don't pay attention to the right series of buttons).
That works pretty well but if you play enough with the frame size and the scrollbar and use a special combination of them, the layout of the left panel is bad (see the image below: whatever the scrollbar position, we should never have this empty space before the left series of buttons). The runnable code is here an below:
Any help will be appreciated.
Have a good day and a great new year's eve!
Pascal
[Edit] To compare the layout issue with the normal scrolling behaviour, comment this line in the main:
Starting from the scrolling code at https://wiki.wxwidgets.org/Scrolling (to which I've made as few changes as possible...), I wanted to test the use of SetTargetWindow. The final goal is to scroll a left panel with a right wxScrolledWindow (so don't pay attention to the right series of buttons).
That works pretty well but if you play enough with the frame size and the scrollbar and use a special combination of them, the layout of the left panel is bad (see the image below: whatever the scrollbar position, we should never have this empty space before the left series of buttons). The runnable code is here an below:
Code: Select all
#include "wx/wx.h"
#include <iostream>
class ScrolledWidgetsPane : public wxScrolledWindow
{
public:
ScrolledWidgetsPane(wxWindow* parent, wxWindowID id) : wxScrolledWindow(parent, id)
{
// the sizer will take care of determining the needed scroll size
// (if you don't use sizers you will need to manually set the viewport size)
wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
// add a series of widgets
for (int w = 1; w <= 20; w++)
{
wxButton* b = new wxButton(this, wxID_ANY, wxString::Format(wxT("Button %i"), w));
sizer->Add(b, 0, wxALL, 3);
}
this->SetSizer(sizer);
// this part makes the scrollbars show up
this->FitInside(); // ask the sizer about the needed size
this->SetScrollRate(5, 5);
}
};
class MyPane : public wxPanel
{
public:
MyPane(wxWindow* parent, wxWindowID id) : wxPanel(parent, id)
{
// the sizer will take care of determining the needed scroll size
// (if you don't use sizers you will need to manually set the viewport size)
wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
// add a series of widgets
for (int w = 1; w <= 20; w++)
{
wxButton* b = new wxButton(this, wxID_ANY, wxString::Format(wxT("Button %i"), w));
sizer->Add(b, 0, wxALL, 3);
}
this->SetSizer(sizer);
// this part makes the scrollbars show up
this->FitInside(); // ask the sizer about the needed size
}
};
// A sample app that adds the scrolled pane to a frame to make this code runnable
class MyApp : public wxApp
{
wxFrame *frame;
public:
bool OnInit()
{
wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
frame = new wxFrame((wxFrame *)NULL, -1, wxT("Scrolling Widgets"), wxPoint(50, 50), wxSize(650, 650));
MyPane* pane = new MyPane(frame, wxID_ANY);
sizer->Add(pane, 1, wxEXPAND);
ScrolledWidgetsPane* scroll = new ScrolledWidgetsPane(frame, wxID_ANY);
sizer->Add(scroll, 1, wxEXPAND);
scroll->SetTargetWindow(pane);
frame->SetSizer(sizer);
frame->Show();
return true;
}
};
IMPLEMENT_APP(MyApp)
Have a good day and a great new year's eve!
Pascal
[Edit] To compare the layout issue with the normal scrolling behaviour, comment this line in the main:
Code: Select all
scroll->SetTargetWindow(pane);