I have a simple resizable dialog in which I can add (vertically) or clear some controls by clicking "Add" or "Clear" button.
When the number of controls in my vertical sizer becomes more than 4, I would like to display a verticall scrollbar so that, only 4 controls are shown at any time.
Here is a simple example. All works fine without any scroll bar.
But if I uncomment the 2 lines SetScrollRate in order to activate the vertical scrolling feature, my scrolling area becomes too smal and no button is displayed ! I have tried a lot of different stuff such as FitInside, Layout, Size Hints etc... but nothing works
Could you please help me to add the correct instructions in this code ?
Thanks in advance !
Code: Select all
#include <wx/wx.h>
// g++ -o dialog main.cpp `/usr/bin/wx-config --cxxflags --libs`
class MyDialog : public wxDialog {
public:
void on_add(wxCommandEvent& /*event*/) {
int nb = _button_box->GetItemCount();
// Activate vertical scroll bar up to 4 buttons
//if (nb == 4) _button_panel->SetScrollRate(0, 20);
wxButton* but = new wxButton(_button_panel, -1, wxString::Format(wxT("Button %d"), nb+1));
_button_box->Add(but, 0, wxALIGN_CENTER | wxALL, 5);
refresh();
}
void on_clear(wxCommandEvent& /*event*/) {
_button_box->Clear(true);
// Deactivate vertical scroll bar
//_button_panel->SetScrollRate(0, 0);
refresh();
}
void refresh() {
_global_box->SetSizeHints(this);
}
MyDialog() : wxDialog(NULL, -1, wxT("Test Dialog"),
wxDefaultPosition, wxSize(-1,-1),
wxCAPTION | wxRESIZE_BORDER) {
_global_box = new wxBoxSizer(wxVERTICAL);
wxBoxSizer* hbox = new wxBoxSizer(wxHORIZONTAL);
wxButton* add = new wxButton(this, 1, wxT("Add"), wxDefaultPosition, wxSize(60 , -1));
hbox->Add(add, 0, wxLEFT, 5) ;
wxButton* clear = new wxButton(this, 2, wxT("Clear"), wxDefaultPosition, wxSize(60 , -1));
hbox->Add(clear, 0, wxLEFT, 5) ;
_global_box->Add(hbox, 0, wxALIGN_CENTER | wxEXPAND | wxALL, 5) ;
_button_panel = new wxScrolledWindow(this, -1, wxDefaultPosition, wxSize(-1,-1), wxBORDER_SIMPLE);
_button_box = new wxBoxSizer(wxVERTICAL);
wxButton* but = new wxButton(_button_panel, -1, wxT("Button 1"));
_button_box->Add(but, 0, wxALIGN_CENTER | wxALL, 5);
but = new wxButton(_button_panel, -1, wxT("Button 2"));
_button_box->Add(but, 0, wxALIGN_CENTER | wxALL, 5);
_button_panel->SetSizer(_button_box);
_global_box->Add(_button_panel, 0, wxALIGN_CENTER | wxEXPAND | wxALL, 5);
wxSizer* buttons = CreateButtonSizer(wxOK | wxCANCEL);
_global_box->Add(buttons, 0, wxALIGN_CENTER | wxALL, 5);
SetSizer(_global_box);
Connect(1, wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction)
&MyDialog::on_add) ;
Connect(2, wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction)
&MyDialog::on_clear) ;
refresh();
}
private:
wxBoxSizer* _global_box;
wxBoxSizer* _button_box;
wxScrolledWindow* _button_panel;
};
class MyApp: public wxApp {
public:
bool OnInit() {
MyDialog *dialog = new MyDialog();
dialog->ShowModal();
return(false);
}
};
IMPLEMENT_APP(MyApp)