resizing the width of a SpinCtrl the elegant way

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
MoonKid
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 543
Joined: Wed Apr 05, 2006 9:39 am

resizing the width of a SpinCtrl the elegant way

Post by MoonKid »

I want to have SpinCtrl that just use nothing more then the width needed for (e.g.) 2 digits.

I know I can set SetMinSize() and use GetTextExtend("00") to compute the needed size of a string.

But the SpinButtons have a width, too. As a workaround I could create one time a spinbutton store its width and use this value to create the SpinCtrls the way I need them.

But the TextCtrl has a space (margin?) between its text and the border of itself.

All this values differs on different plattforms. So I need a plattform independend solution for this.

How could this be solved?

It would be easier if I could access the TextCtrl inside of the SpinCtrl directly. So it would be more logical to set the MinSize of the TextCtrl. But in this case there still would be the problem with the margins of the TextCtrl. And in my opinion it would be a dirty way if I just would add fixed pixel values (e.g. 10 px) as margins.

And creating my own SpinCtrl using TextCtrl and SpinButtons wouldn't solve all problems.

btw:
I used wx with C++ some years ago. Now I am using it with Python (wxPhoenix with Python3). But the problem is still the same when I interprete the docu files correct.
catalin
Moderator
Moderator
Posts: 1618
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: resizing the width of a SpinCtrl the elegant way

Post by catalin »

AFAIR a spinCtrl will have its initial width determined by its initial value. You might also need to change its default 'max' value.
Another possible way is to use wxControl::GetSizeFromTextSize(), see its docs and given example.
MoonKid
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 543
Joined: Wed Apr 05, 2006 9:39 am

Re: resizing the width of a SpinCtrl the elegant way

Post by MoonKid »

Ah nice. This works nearly perfect.

Currently I have not C++ environment to test it with the original wxWidgets.

But with wxPyhton (on KDE 4 with Kubuntu 14.04.2) the computed with needs only 1 pixel more. ;)

Code: Select all

size = spinCtrl.GetSizeFromTextSize(spinCtrl.GetTextExtent('00'))
spinCtrl.SetMinSize(size)
The last (right) pixels of the second 0 in the SpinControl are cutted by the SpinButtons. When I add manualy one pixel more to the width it looks pretty.

Sorry, for asking. But could you be so nice and check that behaviour with C++ wxWidgets and post your result (version, gui-backend, os, ...)?
It would be helpfull to find out if it is wxPython-related or wxWidgets-related.

EDIT:
I tested that on LXDE (Lubuntu 14.04.2), which means Gtk2 I think. There it works perfect without adding manualy a pixel more to the width. I used wxPhoenix (dev version of wxPython) with Python3.