wxScrolledWindow Example with Sizer Topic is solved

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Post Reply
mmccarty12
Knows some wx things
Knows some wx things
Posts: 30
Joined: Mon May 12, 2008 5:51 pm

wxScrolledWindow Example with Sizer

Post by mmccarty12 » Tue Apr 14, 2009 4:34 pm

I am submitting here an example of what I did to get the wxScrolledWindow to work with sizers. It is a simple example that creates a dialog that contains the following in the given heirarchy

Code: Select all

   topSizer
      titleSizer
         wxStaticText
      middleSizer
         wxScrolledWindow - m_sw
            scrolledWindowSizer - m_swSizer
      buttonSizer
         Ok Button
         Add New Row Button - adds a new row to the wxScrolledWindow

Code: Select all

/*****************************************************
* Name:				ScrolledWindowWithSizer.cpp
* Purpose:		To demonstrate an example of how to use
*							wxScrolledWindow within projects.
* Author:			Michael McCarty
* Created:		2009-04-14
* Copyright:
* License:		Not Applicable
*****************************************************/

#include <wx/wx.h>

//helper functions
enum wxbuildinfoformat { short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}


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

class ScrolledWindowWithSizerDialog: public wxDialog
{
    public:
        ScrolledWindowWithSizerDialog(wxWindow* parent,wxWindowID id = -1);
        virtual ~ScrolledWindowWithSizerDialog();

    private:
        void OnQuit(wxCommandEvent& event);
        void OnAbout(wxCommandEvent& event);
        void OnOkButtonClicked(wxCommandEvent& event);
        void OnAddButtonClicked(wxCommandEvent& event);
        void OnRowButtonClicked(wxCommandEvent& event);

        wxSize tcSize;
        wxSize buttonSize;
        wxSize stSize;
        wxSize swSize;
        wxFlexGridSizer* m_swSizer;
        wxScrolledWindow* m_sw;

        unsigned int counter;

        DECLARE_EVENT_TABLE()
};

IMPLEMENT_APP(ScrolledWindowWithSizerApp);

bool ScrolledWindowWithSizerApp::OnInit()
{
    bool wxsOK = true;
    wxInitAllImageHandlers();

    if ( wxsOK )
    {
        ScrolledWindowWithSizerDialog Dlg(0);
        SetTopWindow(&Dlg);
        Dlg.ShowModal();
        wxsOK = false;
    }

    return wxsOK;
}

BEGIN_EVENT_TABLE(ScrolledWindowWithSizerDialog,wxDialog)
END_EVENT_TABLE()

ScrolledWindowWithSizerDialog::ScrolledWindowWithSizerDialog(wxWindow* parent,wxWindowID id)
{
    counter = 0;
    tcSize = wxSize(150, -1);
    buttonSize = wxSize(200, -1);
    stSize = wxSize(75, -1);
    swSize = wxSize(-1, 300);
    Create(parent, id, _("wxWidgets app"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE, _T("id"));

    wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL);
    wxBoxSizer* titleSizer = new wxBoxSizer(wxHORIZONTAL);
    wxStaticText* titleST = new wxStaticText(this, wxNewId(), wxT("This is the title section of the dialog"));
    titleSizer->Add(titleST, 0, wxALL|wxALIGN_CENTER_HORIZONTAL, 5, NULL);

    wxBoxSizer* middleSizer = new wxBoxSizer(wxVERTICAL);

    m_sw = new wxScrolledWindow(this, wxNewId(), wxDefaultPosition, wxDefaultSize, wxVSCROLL);
    m_sw->SetMinSize(wxSize(-1, 200));

    m_swSizer = new wxFlexGridSizer(0, 3, 5, 5);
    m_sw->SetSizer(m_swSizer);

    middleSizer->Add(m_sw, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxGROW, 5, NULL);

    wxBoxSizer* buttonSizer = new wxBoxSizer(wxHORIZONTAL);
    wxButton* OkButton = new wxButton(this, wxID_OK, wxT("OK"));
    OkButton->Connect(wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction)&ScrolledWindowWithSizerDialog::OnOkButtonClicked, NULL, this);
    wxButton* AddButton = new wxButton(this, wxNewId(), wxT("Add Row"));
    AddButton->Connect(wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction)&ScrolledWindowWithSizerDialog::OnAddButtonClicked, NULL, this);
    buttonSizer->Add(OkButton, 0, wxALL, 5, NULL);
    buttonSizer->Add(AddButton, 0, wxALL, 5, NULL);

    topSizer->Add(titleST, 0, wxALL|wxALIGN_CENTER_HORIZONTAL, 5, NULL);
    topSizer->Add(middleSizer, 0, wxALL|wxALIGN_CENTER_HORIZONTAL, 5, NULL);
    topSizer->Add(buttonSizer, 0, wxALL|wxALIGN_CENTER_HORIZONTAL, 5, NULL);

    SetSizer(topSizer);
    topSizer->Fit(this);
    topSizer->SetSizeHints(this);
    Center();
}

ScrolledWindowWithSizerDialog::~ScrolledWindowWithSizerDialog()
{
}

void ScrolledWindowWithSizerDialog::OnAbout(wxCommandEvent& event)
{
    wxString msg = wxbuildinfo(long_f);
    wxMessageBox(msg, _("Welcome to..."));
}

void ScrolledWindowWithSizerDialog::OnOkButtonClicked(wxCommandEvent& event)
{
    EndModal(wxID_OK);
}

void ScrolledWindowWithSizerDialog::OnAddButtonClicked(wxCommandEvent& event)
{
    counter++;
    wxStaticText* st = new wxStaticText(m_sw, wxID_ANY, wxString::Format("Row %u", counter), wxDefaultPosition, stSize);
    wxTextCtrl* tc = new wxTextCtrl(m_sw, wxID_ANY, wxString::Format("Text in Row %u", counter), wxDefaultPosition, tcSize);
    wxButton* btn = new wxButton(m_sw, counter, wxString::Format("Row %u Button", counter));
    btn->Connect(wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction)&ScrolledWindowWithSizerDialog::OnRowButtonClicked, NULL, this);
    m_sw->GetSizer()->Add(st, 0, wxLEFT|wxRIGHT|wxALIGN_CENTER_VERTICAL, 5, NULL);
    m_sw->GetSizer()->Add(tc, 0, wxLEFT|wxRIGHT|wxALIGN_CENTER_VERTICAL, 5, NULL);
    m_sw->GetSizer()->Add(btn, 0, wxLEFT|wxRIGHT|wxEXPAND|wxALIGN_CENTER_VERTICAL, 5, NULL);
    // the + 5 in (btn->GetSize().GetHeight() + 5) accounts for the space when creating
    // m_swSizer = new wxFlexGridSizer(0, 3, 5, 5); on the third parameter.
    // The + N should reflect the gaps given for the sizer.  Why it is not + 10, I do not know.  It would make
    // sense since the gap is for both the vertical sides.  But this setup only increments 1 row at a time, changing
    // the value makes odd row adjustments.

    m_sw->SetScrollbars(0, (btn->GetSize().GetHeight() + 5), 0, ((btn->GetSize().GetHeight()) + 5)*counter, 0, 0);

    // For this example, this was the minimum necessary for a clean refresh.
    this->GetSizer()->SetSizeHints(this);
    Layout();
}

void ScrolledWindowWithSizerDialog::OnRowButtonClicked(wxCommandEvent& event)
{
    wxMessageBox(wxString::Format("Row Button %d clicked", event.GetId()));
}

I am also attaching the file for downloading.
Attachments
ScrolledWindowWithSizer.cpp
This contains all the source code to run the sample.
(5.57 KiB) Downloaded 412 times
I am offended when the truth offends you.

OS: Centos 5
Widgets Version: wxWidgets.2.8.7
Widgets Version: wxWidgets.2.8.9
Compiler: gcc (GCC) 4.1.2 20071124 (Red Hat 4.1.2-42)
Compiler:g++ (GCC) 4.1.2 20071124 (Red Hat 4.1.2-42)

dlchnr
I live to help wx-kind
I live to help wx-kind
Posts: 188
Joined: Tue Jan 27, 2009 6:45 pm
Location: Germany
Contact:

Post by dlchnr » Thu Apr 16, 2009 2:38 pm

Hi mmccarty12,

you haven't told, which problem you have!

But one can guess, that something is wrong with your Scrollbars.

I use also a wxScrolledWindow, but haven't implemented scrolling until now, I will it do in the future (a reason, why I'm interested in this thread).

Having no experience I can give only a hint, something I've noticed. There are three ways to set the size of the scrolling area according to
http://docs.wxwidgets.org/trunk/classwx ... l#_details
and you use two of them - Sizers and SetScrollbars.
May be your problem arise because of interaction of these two methodes.

mmccarty12
Knows some wx things
Knows some wx things
Posts: 30
Joined: Mon May 12, 2008 5:51 pm

Post by mmccarty12 » Mon May 04, 2009 6:26 pm

dlchnr,

I think you misunderstand this post. While I was trying to get wxScrollbars to work as advertised and the way I wanted them to for the purposes of my project, I came up with the solution posted here for those who have similar problems.

From the examples distributed with the code, I was unable to get wxScrollWindow to work the way I wanted, so I spent the better part of a day trying to figure it out. Also, I could not find anything here in the forums that gave me a clue as to how to set up wxScrolledWindow using sizers. As well as spending some search time on the web.

To save others the same trouble as me in trying to get wxScrolledWindow to work with sizers, I decided to post this tested code so they would not have to jump through as many hoops as I did.

There is nothing wrong with the submitted code. It works in its entirety.
It was tested on two different Linux boxes using wxMotif and wxGTK. The place where this example was created does not have a windows system or mac system so I could not test it on them, but I have to assume that as long as the library links are correct, it will work.

Sorry for the confusion.
I am offended when the truth offends you.

OS: Centos 5
Widgets Version: wxWidgets.2.8.7
Widgets Version: wxWidgets.2.8.9
Compiler: gcc (GCC) 4.1.2 20071124 (Red Hat 4.1.2-42)
Compiler:g++ (GCC) 4.1.2 20071124 (Red Hat 4.1.2-42)

Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria » Mon May 04, 2009 11:46 pm

Moving to "the code dump" in this case :)

Post Reply