Page 1 of 1

adding wxScrolledWindow into a sizer

Posted: Tue Nov 25, 2014 12:43 pm
by greekfellows
I'm trying to make such a wxWindow-derived window that will later be added into another wxFrame-derived frame:
layout.PNG
layout.PNG (11.03 KiB) Viewed 1437 times
Here's the code for the window:

myWindow.h

Code: Select all

#include <wx/wx.h>

class myWindow: public wxWindow {
public:
	wxBoxSizer *mainVBox, *addRemoveHBox;
	wxButton *addButton, *removeButton;
	
	wxScrolledWindow *myScrolledWindow;
	wxBoxSizer *scrolledWindowSizer;
	wxStaticText *label0, *label1, *label2, *label3, *label4, *label5, *label6, *label7, *label8, *label9;

	myWindow() {}
	myWindow(wxWindow *parent);

	void init();
};
myWindow.cpp

Code: Select all

#include "./myWindow.h"

myWindow::myWindow(wxWindow *parent) : wxWindow(parent, -1, wxDefaultPosition, wxDefaultSize) {
	init();
}

void myWindow::init() {
	mainVBox = new wxBoxSizer(wxVERTICAL);
	addRemoveHBox = new wxBoxSizer(wxHORIZONTAL);

	myScrolledWindow = new wxScrolledWindow(this, wxID_ANY);
	scrolledWindowSizer = new wxBoxSizer(wxVERTICAL);

	label0 = new wxStaticText(this, wxID_ANY, wxString("label 0"));
	label1 = new wxStaticText(this, wxID_ANY, wxString("label 1"));
	label2 = new wxStaticText(this, wxID_ANY, wxString("label 2"));
	label3 = new wxStaticText(this, wxID_ANY, wxString("label 3"));
	label4 = new wxStaticText(this, wxID_ANY, wxString("label 4"));
	label5 = new wxStaticText(this, wxID_ANY, wxString("label 5"));
	label6 = new wxStaticText(this, wxID_ANY, wxString("label 6"));
	label7 = new wxStaticText(this, wxID_ANY, wxString("label 7"));
	label8 = new wxStaticText(this, wxID_ANY, wxString("label 8"));
	label9 = new wxStaticText(this, wxID_ANY, wxString("label 9"));

	scrolledWindowSizer->Add(label0, 0, wxALIGN_LEFT | wxALL, 10);
	scrolledWindowSizer->Add(label1, 0, wxALIGN_LEFT | wxALL, 10);
	scrolledWindowSizer->Add(label2, 0, wxALIGN_LEFT | wxALL, 10);
	scrolledWindowSizer->Add(label3, 0, wxALIGN_LEFT | wxALL, 10);
	scrolledWindowSizer->Add(label4, 0, wxALIGN_LEFT | wxALL, 10);
	scrolledWindowSizer->Add(label5, 0, wxALIGN_LEFT | wxALL, 10);
	scrolledWindowSizer->Add(label6, 0, wxALIGN_LEFT | wxALL, 10);
	scrolledWindowSizer->Add(label7, 0, wxALIGN_LEFT | wxALL, 10);
	scrolledWindowSizer->Add(label8, 0, wxALIGN_LEFT | wxALL, 10);
	scrolledWindowSizer->Add(label9, 0, wxALIGN_LEFT | wxALL, 10);

	myScrolledWindow->SetSizer(scrolledWindowSizer);
	scrolledWindowSizer->FitInside(myScrolledWindow);
	myScrolledWindow->FitInside();
	myScrolledWindow->SetScrollRate(10, 10);
	myScrolledWindow->ShowScrollbars(wxScrollbarVisibility::wxSHOW_SB_ALWAYS, wxScrollbarVisibility::wxSHOW_SB_ALWAYS);

	addButton = new wxButton(this, wxID_ANY, wxT("add"), wxDefaultPosition, wxSize(60, 30));
	removeButton = new wxButton(this, wxID_ANY, wxT("remove"), wxDefaultPosition, wxSize(60, 30));
	addRemoveHBox->Add(addButton, 1, wxALL, 10);
	addRemoveHBox->Add(removeButton, 1, wxTOP | wxBOTTOM | wxRIGHT, 10);

	mainVBox->Add(myScrolledWindow, 0, wxALIGN_CENTER, 0);
	mainVBox->Add(addRemoveHBox, 0, wxALIGN_CENTER, 0);
	mainVBox->FitInside(this);
	SetSizer(mainVBox);
	Center();
}
myFrame.h:

Code: Select all

#include <wx/wx.h>
#include "./myWindow.h"

class myFrame: public wxFrame {
public:
	wxBoxSizer *mainHBox;
	myWindow* window;

	myFrame();
};

#endif
myFrame.cpp:

Code: Select all

#include "./myApp.h"

DECLARE_APP(myApp)

myFrame::myFrame() : wxFrame(NULL, wxID_ANY, wxString("")) {
	mainHBox = new wxBoxSizer(wxHORIZONTAL);
	window = new myWindow(this);
	mainHBox->Add(window, 0, wxALIGN_CENTER | wxALL, 10);
	mainHBox->SetSizeHints(this);
	SetSizer(mainHBox);
	Center();
}
myApp.h:

Code: Select all

#include <wx/wxprec.h>
#include <wx/wx.h>

#include "myFrame.h"

class myApp : public wxApp {
public:
	myFrame *frame;

	virtual bool OnInit();
};

#endif
myApp.cpp:

Code: Select all

#include "myApp.h"

IMPLEMENT_APP(myApp)

bool myApp::OnInit() {
	if (!wxApp::OnInit()) {
		return false;
	}

	frame = new myFrame();
	frame->Show(true);

	return true;
}
However, the resulting window looks something like this:
wronged1.PNG
wronged1.PNG (7.32 KiB) Viewed 1437 times
Resizing doesn't seem to help either:
wronged2.PNG
wronged2.PNG (8.77 KiB) Viewed 1437 times
There are 2 problems I recognize here:
  • myWindow overlaps with the 2 buttons, which shouldn't be happening;
  • the scroll bars for myWindow are invisible, even when ShowScrollBars() is called and the contents of the scrolled window is much larger than its virtual size.
What mistakes did I make? What should I do to implement my design?

Re: adding wxScrolledWindow into a sizer

Posted: Tue Nov 25, 2014 1:11 pm
by RemcoM
WARNING!: I'm not an expert.

What i would do:

Use a master flexgridsizer, 2 rows, 1 col, grow able rows is 0, expand is true.

Row one, another flexgridsizer, 1 row, 1 col, grow able row 0, col 0, expand true.
Inhere place your MyScrollWindow, maybe expand is true.

Row two, place here your boxsizer with the buttons.

Not tested, to be honest.

Re: adding wxScrolledWindow into a sizer

Posted: Tue Nov 25, 2014 1:48 pm
by doublemax

Code: Select all

myScrolledWindow = new wxScrolledWindow(this, wxID_ANY);
scrolledWindowSizer = new wxBoxSizer(wxVERTICAL);

label0 = new wxStaticText(this, wxID_ANY, wxString("label 0"));
label1 = new wxStaticText(this, wxID_ANY, wxString("label 1"));
I assume these static texts should be inside the wxScrolledWindow? Then they should have myScrolledWindow as parent.

Code: Select all

mainVBox->Add(myScrolledWindow, 0, wxALIGN_CENTER, 0);
If the wxScrolledWindow should take over all available space, it should look like this:

Code: Select all

mainVBox->Add(myScrolledWindow, 1, wxEXPAND, 0);

Re: adding wxScrolledWindow into a sizer

Posted: Tue Nov 25, 2014 3:14 pm
by greekfellows
@doublemax thanks! those were the actual problems... I was very careless during all that copying-and-pasting.

@RemcoM yes, actually I used a design similar to yours in the actual code, but here I omitted all that unnecessary layout stuff to keep the code readable. thanks anyway!