views:

307

answers:

1

I have a Custom control derived from wxControl,inside a dialog with a vertical sizer

wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
Knob* newKnob = new Knob(panel,wxID_ANY,wxDefaultPosition,wxSize(40,40));
topSizer->Add(newKnob,0,wxALL|wxALIGN_CENTER);
panel->SetSizer(topSizer);
topSizer->Fit(panel);

//panel is a wxPanel inside the dialog

But for some reason ,the custom controls' size is set at (80,100).If i resize the dialog beyond that size its aligned to center as i specified.

EDIT: Am using wx 2.8.9 on windows Vista with visual studio 2005.

What could be missing?

+1  A: 

You didn't provide any information about the wxWidgets version or your platform / compiler, so I will answer for wxWidgets 2.8 and MSW.

The size you specify is not necessarily used for the control, since the best size for different platforms may well be different. Even on Windows the size will usually depend on the default GUI font. So there are various methods in wxWidgets that let windows specify their minimum, best or maximum size. If you have special requirements you may need to override some of them.

For your problem it looks like you want to have the control have a smaller size than the default best size of wxControl:

wxSize wxControl::DoGetBestSize() const
{
    return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT);
}

(taken from src/msw/control.cpp) with the values

#define DEFAULT_ITEM_WIDTH  100
#define DEFAULT_ITEM_HEIGHT 80

(taken from include/wx/msw/private.h).

If you override DoGetBestSize() to return your intended size it should work.

Edit:

You comment that you cannot override DoGetBestSize() since it is not virtual in wxControl. It is though, because it is introduced in wxWindow:

// get the size which best suits the window: for a control, it would be
// the minimal size which doesn't truncate the control, for a panel - the
// same size as it would have after a call to Fit()
virtual wxSize DoGetBestSize() const;

(taken from include/wx/window.h). wxWindow is a parent class of wxControl, so the method is indeed virtual and you can of course override it. Many controls do in fact, as a grep in the sources will show you.

mghie
+1 and thanks for pointing out the default width and height.Since i am deriving from wxControl i cannot override DoGetBestSize() since its not virtual in wxControl
NightCoder
Thanks for clearing the doubt
NightCoder