The size of the wxwidgets custom control could not be set. What am I missing?

I have a custom control derived from wxControl inside a dialog box 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 size of the custom controls is set to (80,100). If I resize the dialog above this size, it is centered as I indicated.

EDIT: I am using wx 2.8.9 on Windows Vista with visual studio 2005.

What might be missing?

+2


source to share


1 answer


You have not provided any information about wxWidgets version or your platform / compiler, so I will be responsible for wxWidgets 2.8 and MSW.

The specified size is not necessarily used for driving, as the best size may differ for different platforms. Even on Windows, the size usually depends on the default GUI font. Thus, there are various methods in wxWidgets that allow Windows to determine 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 the control to be smaller than the default size wxControl

:

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

      

(taken from src/msw/control.cpp

) with 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 estimated size, it should work.

Edit:

You will comment that you cannot override DoGetBestSize()

as it is not virtual in wxControl

. However, since it's 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 the parent class wxControl

, so the method is really virtual and you can of course override it. In fact, many controls are shown grep

in the sources.

+4


source







All Articles