Custom wxControl Component Size Problem (Size Ignored) Topic is solved

Are you writing your own components and need help with how to set them up or have questions about the components you are deriving from ? Ask them here.
Post Reply
Xangis
Earned some good credits
Earned some good credits
Posts: 122
Joined: Fri Apr 14, 2006 9:49 pm
Location: Beaverton, OR
Contact:

Custom wxControl Component Size Problem (Size Ignored)

Post by Xangis »

Since I'm having trouble with keyboard events in a wxSlider control, I'm building one from scratch to get the behavior I'm looking for (including being based on float values and allowing bitmaps for slider and indicator).

It almost works right. The problem I'm having is that when I create the control, the specified size is ignored and instead I get a control that has a client size of 80x100 pixels, even though I specified a wxSize of 30x120. What have I missed?

Here's the control so far:

Code: Select all

class wxBitmapSlider : public wxControl ...

Code: Select all

BEGIN_EVENT_TABLE(wxBitmapSlider, wxSlider)
    EVT_PAINT(wxBitmapSlider::OnPaint)
    EVT_KEY_DOWN(wxBitmapSlider::OnKeyDown)
    EVT_KEY_UP(wxBitmapSlider::OnKeyUp)
    EVT_LEFT_UP(wxBitmapSlider::OnMouseRelease)
    EVT_MOTION(wxBitmapSlider::OnMouseMove)
    EVT_LEFT_DOWN(wxBitmapSlider::OnMouse)
END_EVENT_TABLE()

wxBitmapSlider::wxBitmapSlider( wxWindow* parent, wxWindowID id, float value, float minValue, float maxValue, const wxPoint &pos , const wxSize &size, long style)
{
    Create( parent, id, value, minValue, maxValue, pos, size, style );
}

void wxBitmapSlider::Create(wxWindow* parent, wxWindowID id, float value, float minValue, float maxValue, const wxPoint &pos, const wxSize &size, long style)
{
	_dragging = false;
	_style = style;
	_minValue = minValue;
	_maxValue = maxValue;
	_value = value;
        wxControl::Create( parent, id, pos, size, style|wxBORDER_NONE );
}

void wxBitmapSlider::OnPaint( wxPaintEvent& event )
{
    wxBufferedPaintDC dc(this);
    wxSize size = GetSize();
    // Bitmap drawing code omitted for brevity...
		if( _style & wxSL_VERTICAL )
		{
			dc.Clear();
			dc.SetBrush(*wxWHITE_BRUSH);
			dc.SetPen(*wxGREEN_PEN);
	                dc.DrawLine(size.x/2, 0, size.x/2, size.y);
			dc.DrawRectangle(size.x/2 - 3, (_value / _maxValue * (float)size.y), 7, 5);
		}
		else
		{
			dc.Clear();
			dc.SetBrush(*wxWHITE_BRUSH);
			dc.SetPen(*wxGREEN_PEN);
	                dc.DrawLine(0, size.y/2, size.x, size.y/2);
			dc.DrawRectangle((_value / _maxValue * (float)size.x), size.y/2 - 3, 5, 7 );
		}
    event.Skip();
}

// Range and value setters, getters, and mouse event handlers omitted for brevity.
Here's my creation call:

Code: Select all

new wxBitmapSlider( itemDialog1, ID_WHATEVER, 8192, 0, 16383, wxDefaultPosition, wxSize( 30, 120 ), wxSL_VERTICAL|wxSL_INVERSE );
The *only* thing I do related to size is pass it on to wxControl::Create in the Create call. Is there more I need to do to be able to specify the control's size?
WinVista/7: VC++ .Net 2010 / Ubuntu 11.04: gcc4.4.3 [2.8.12 on all]
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

Calling SetSize is often not enough; for instance, sizers will overwrite whatever you put there. Try calling SetMinSize and SetMaxSize in addition.
"Keyboard not detected. Press F1 to continue"
-- Windows
Xangis
Earned some good credits
Earned some good credits
Posts: 122
Joined: Fri Apr 14, 2006 9:49 pm
Location: Beaverton, OR
Contact:

Post by Xangis »

Turns out this our friends at StackOverflow have exactly the solution I was looking for:
http://stackoverflow.com/questions/1560 ... issing-out

After creating a DoGetBestSize override everything is perfect. :)
WinVista/7: VC++ .Net 2010 / Ubuntu 11.04: gcc4.4.3 [2.8.12 on all]
Post Reply