From: Volker Bartheld on
Hi!

I experiemented with creating my own custom control: A slider that
displays its value below. As a first step, I derived a class from
wxControl, implemented Create() and defined a vertical sizer to which a
wxSlider and a wxStatic text are added. Unfortunately, the static text
is always displayed ontop of the slider instead of below (see code at
the bottom of this post).

The various custom control samples all seem to be completely
owner-drawn, overriding OnPaint(). I wanted to use wxSlider and
wxStaticText's native renderers. Is that wrong?

Thanks a lot in advance for any insights.

Volker

<wxAdvancedSlider.cpp>
#include <wx/wx.h>

class wxAdvancedSlider : public wxControl
{
DECLARE_DYNAMIC_CLASS(wxAdvancedSlider);
DECLARE_EVENT_TABLE();
public:
wxAdvancedSlider() : m_pSlider(NULL), m_pText(NULL) { }
wxAdvancedSlider(wxWindow* parent, wxWindowID id, int value , int minValue, int maxValue, const wxPoint& pos=wxDefaultPosition, const wxSize& size=wxDefaultSize, long style=wxSL_HORIZONTAL, const wxValidator& validator=wxDefaultValidator, const wxString& name=wxT("AdvancedSlider"))
{
Create(parent, id, value, minValue, maxValue, pos, size, style, validator, name);
}
bool Create(wxWindow* parent, wxWindowID id, int value , int minValue, int maxValue, const wxPoint& pos=wxDefaultPosition, const wxSize& size=wxDefaultSize, long style=wxSL_HORIZONTAL, const wxValidator& validator=wxDefaultValidator, const wxString& name=wxT("AdvancedSlider"))
{
if(!wxControl::Create(parent, id, pos, size, wxNO_BORDER, validator)) return false;
wxBoxSizer* pMainSizer=new wxBoxSizer(wxVERTICAL);
SetSizer(pMainSizer);
m_pSlider=new wxSlider(this, wxID_ANY, value, minValue, maxValue, pos, size, style);
m_pText=new wxStaticText(this, wxID_ANY, name);
pMainSizer->Add(m_pSlider, 0, wxGROW|wxALIGN_CENTER_VERTICAL, 5);
pMainSizer->Add(m_pText, 0, wxALIGN_CENTER, 0);
SetSizer(pMainSizer);
SetInitialSize(size);
return true;
}
virtual wxSize DoGetBestSize() const
{
return GetSizer()->GetMinSize();
}
wxSlider* m_pSlider;
wxStaticText* m_pText;
};

IMPLEMENT_DYNAMIC_CLASS(wxAdvancedSlider, wxControl);
BEGIN_EVENT_TABLE(wxAdvancedSlider, wxControl)
END_EVENT_TABLE()

class TestDialog : public wxDialog
{
DECLARE_DYNAMIC_CLASS(TestDialog)
DECLARE_EVENT_TABLE()
public:
TestDialog(wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxDEFAULT_DIALOG_STYLE, const wxString& name = wxT("dialogBox"))
: wxDialog(parent, id, title, pos, size, style, name)
{
wxBoxSizer *pSizerMain = new wxBoxSizer(wxVERTICAL);
SetSizer(pSizerMain);
pSizerMain->Add(new wxAdvancedSlider(this, wxID_ANY, 0, 0, 10));
pSizerMain->SetSizeHints(this);
}
void OnClose(wxCloseEvent &event) { if(IsModal()) EndModal(0); else Destroy(); }
TestDialog::TestDialog() {}
}; // class TestDialog : public wxDialog
IMPLEMENT_DYNAMIC_CLASS(TestDialog, wxDialog)
BEGIN_EVENT_TABLE(TestDialog, wxDialog)
EVT_CLOSE(TestDialog::OnClose)
END_EVENT_TABLE()

class MyApp : public wxApp
{
public:
virtual bool OnInit()
{
if(!wxApp::OnInit()) return false;
TestDialog* dialog=new TestDialog(NULL, wxID_ANY, _T("Test Dialog"));
dialog->Show(true);
return true;
}
}; // class MyApp : public wxApp
IMPLEMENT_APP(MyApp)
</wxAdvancedSlider.cpp>

__
Mail replies to/an V B A R T H E L D at G M X dot D E
From: Volker Bartheld on
Hi!

>As a first step, I derived a class from
>wxControl, implemented Create() and defined a vertical sizer to which a
>wxSlider and a wxStatic text are added. Unfortunately, the static text
>is always displayed ontop of the slider instead of below (see code at
>the bottom of this post).

I ended up with deriving my "Control" from wxPanel:

<wxAdvancedSlider.cpp>
class wxAdvancedSlider : public wxPanel
{
DECLARE_DYNAMIC_CLASS(wxAdvancedSlider);
DECLARE_EVENT_TABLE();
public:
wxAdvancedSlider() : m_pSlider(NULL), m_pText(NULL) { }
wxAdvancedSlider(wxWindow* parent, wxWindowID id, int value , int minValue, int maxValue, const wxPoint& pos=wxDefaultPosition, const wxSize& size=wxDefaultSize, long style=wxSL_HORIZONTAL, const wxValidator& validator=wxDefaultValidator, const wxString& name=wxT("AdvancedSlider"))
{
Create(parent, id, value, minValue, maxValue, pos, size, style, validator, name);
}
bool Create(wxWindow* parent, wxWindowID id, int value , int minValue, int maxValue, const wxPoint& pos=wxDefaultPosition, const wxSize& size=wxDefaultSize, long style=wxSL_HORIZONTAL, const wxValidator& validator=wxDefaultValidator, const wxString& name=wxT("AdvancedSlider"))
{
if(!wxPanel::Create(parent, id, pos)) return false;
wxBoxSizer* pMainSizer=new wxBoxSizer(wxVERTICAL);
m_pSlider=new wxSlider(this, wxID_ANY, value, minValue, maxValue, pos, size, style, validator, name);
m_pText=new wxStaticText(this, wxID_ANY, name);
pMainSizer->Add(m_pSlider, 0, wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_TOP, 0);
pMainSizer->Add(m_pText, 0, wxALIGN_BOTTOM|wxALIGN_CENTER_HORIZONTAL, 0);
SetSizer(pMainSizer);
SetInitialSize(size);
return true;
}

private:
wxSlider* m_pSlider;
wxStaticText* m_pText;
};

IMPLEMENT_DYNAMIC_CLASS(wxAdvancedSlider, wxPanel);
BEGIN_EVENT_TABLE(wxAdvancedSlider, wxPanel)
END_EVENT_TABLE()
</wxAdvancedSlider.cpp>

However, there might be unwanted side effects of that inheritage.

Cheers,
Volker
__
Mail replies to/an V B A R T H E L D at G M X dot D E
 | 
Pages: 1
Prev: Kubuntu installation
Next: in search of wisdom