How to get the initial size of a wxTextCtrl later after its creation? Topic is solved

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.
Post Reply
tomay3000
I live to help wx-kind
I live to help wx-kind
Posts: 191
Joined: Mon Apr 24, 2017 4:23 am

How to get the initial size of a wxTextCtrl later after its creation?

Post by tomay3000 » Wed May 12, 2021 6:46 am

Hello,
I need to get the initial size of a wxTextCtrl later after its size has been changed so I can resize it back to the initial size ?
TIA.

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 5156
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: How to get the initial size of a wxTextCtrl later after its creation?

Post by ONEEYEMAN » Wed May 12, 2021 11:51 am

Hi,.
Why?
Can't you call SetMinSize()/SetMaxSize()?
Thank you.

tomay3000
I live to help wx-kind
I live to help wx-kind
Posts: 191
Joined: Mon Apr 24, 2017 4:23 am

Re: How to get the initial size of a wxTextCtrl later after its creation?

Post by tomay3000 » Wed May 12, 2021 3:13 pm

Hello,
sorry for the misunderstanding, what I meant is the default control size given by the system after creation using wxDefaultSize
Because, using wxDefaultSize outside the constructor or Create later has no meaning.
I wanted to know if there is a method I am not seeing that does this!
After some resizing, I just want to give back the default system size to the control.

TIA.

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 5156
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: How to get the initial size of a wxTextCtrl later after its creation?

Post by ONEEYEMAN » Wed May 12, 2021 3:45 pm

Hi,
But why?
What is so important with the wxTextCtrl that it has to remain the same size on resize?
Now, here is another trick question - do you want it to keep the same size when the parent window is moved to another monitor with different DPI?

Thank you.

User avatar
doublemax
Moderator
Moderator
Posts: 16022
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to get the initial size of a wxTextCtrl later after its creation?

Post by doublemax » Wed May 12, 2021 4:16 pm

tomay3000 wrote:
Wed May 12, 2021 3:13 pm
After some resizing, I just want to give back the default system size to the control.
I guess that means you're not using sizers? Otherwise you'd never this question ;)

Did you try just SetSize(wxDefaultSize) ?
Use the source, Luke!

tomay3000
I live to help wx-kind
I live to help wx-kind
Posts: 191
Joined: Mon Apr 24, 2017 4:23 am

Re: How to get the initial size of a wxTextCtrl later after its creation?

Post by tomay3000 » Wed May 12, 2021 4:31 pm

It has nothing to do with monitor or DPI.
The main reason to all of this is to provide it as a sample (I want to demonstrate something), when the user checks the checkbox it became huge, and when the user unchecks the checks box again its size has to go back to it normal size again (But I don't know what is the default size).

PB
Part Of The Furniture
Part Of The Furniture
Posts: 3042
Joined: Sun Jan 03, 2010 5:45 pm

Re: How to get the initial size of a wxTextCtrl later after its creation?

Post by PB » Wed May 12, 2021 4:37 pm

I thought, that unless specified otherwise, default size = best size = minimal size, i.e., obtainable by GetBestSize()?

However, I do not understand how you want to increase size of a wxCheckBox...

tomay3000
I live to help wx-kind
I live to help wx-kind
Posts: 191
Joined: Mon Apr 24, 2017 4:23 am

Re: How to get the initial size of a wxTextCtrl later after its creation?

Post by tomay3000 » Wed May 12, 2021 4:47 pm

doublemax wrote:
Wed May 12, 2021 4:16 pm
Did you try just SetSize(wxDefaultSize) ?
Before My application was crashing using SetSize(wxDefaultSize) and I thought it is not the correct way to use it.
PB wrote:
Wed May 12, 2021 4:37 pm
I thought, that unless specified otherwise, default size = best size = minimal size, i.e., obtainable by GetBestSize()?

However, I do not understand how you want to increase size of a wxCheckBox...
But now, both SetSize(wxDefaultSize) and GetBestSize() worked for me.
This is really weird.

User avatar
doublemax
Moderator
Moderator
Posts: 16022
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to get the initial size of a wxTextCtrl later after its creation?

Post by doublemax » Wed May 12, 2021 4:49 pm

I just tried it myself and SetSize(wxDefaultSize) did not work for me. Use GetBestSize().
If you're unsure if the window has a minimal size set, call SetMinSize( wxDefaultSize ) first.
Use the source, Luke!

tomay3000
I live to help wx-kind
I live to help wx-kind
Posts: 191
Joined: Mon Apr 24, 2017 4:23 am

Re: How to get the initial size of a wxTextCtrl later after its creation?

Post by tomay3000 » Wed May 12, 2021 5:10 pm

doublemax wrote:
Wed May 12, 2021 4:49 pm
I just tried it myself and SetSize(wxDefaultSize) did not work for me. Use GetBestSize().
If you're unsure if the window has a minimal size set, call SetMinSize( wxDefaultSize ) first.
Yes, SetSize(wxDefaultSize) seemed working and tricked me again, and caused the application to crash.

User avatar
doublemax
Moderator
Moderator
Posts: 16022
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to get the initial size of a wxTextCtrl later after its creation?

Post by doublemax » Wed May 12, 2021 5:15 pm

Even if it doesn't work as expected, it shouldn't crash. There must be something else going on.
Use the source, Luke!

tomay3000
I live to help wx-kind
I live to help wx-kind
Posts: 191
Joined: Mon Apr 24, 2017 4:23 am

Re: How to get the initial size of a wxTextCtrl later after its creation?

Post by tomay3000 » Thu May 13, 2021 2:11 pm

Hello,
Using wxWidgets v3.1.6 (dev)

This minimal patch (which I should have uploaded in the first place, sorry for wasting your time) explains what I was trying to achieve:
minimal.patch
(2.57 KiB) Downloaded 16 times
But when I have tried it one more time, SetSize(wxDefaultSize); seems working.

Also, there is this problem that drove me nuts:
When I check the checkbox, the text control should resize to the desired size, that is (900, 300), but it is not. And if I comment out m_panel->Layout(); and recompile, it will this time, but when I uncheck the check box back the text control won't get to its default size. And only If I resize the main frame the text control will get back it original size (Because Layout() has been called implicitly).

what I'm missing here.

Thank in advance.

User avatar
doublemax
Moderator
Moderator
Posts: 16022
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to get the initial size of a wxTextCtrl later after its creation?

Post by doublemax » Thu May 13, 2021 2:39 pm

The code you posted works fine for me.

However, if you work with dynamic fontsizes, instead of using hard-coded sizes, why don't you leave it to the control?

How about this:

Code: Select all

void MyFrame::OnToggleBigSize(wxCommandEvent&)
{
    wxFont font = m_txtCtrl->GetFont();
    if( m_bigSizeCheck->GetValue() )
        font.SetPointSize(font.GetPointSize() * 5);
    else
        font.SetPointSize(font.GetPointSize() / 5);

    m_txtCtrl->InvalidateBestSize();
    m_txtCtrl->SetFont(font);

    m_panel->Layout();
}
Use the source, Luke!

tomay3000
I live to help wx-kind
I live to help wx-kind
Posts: 191
Joined: Mon Apr 24, 2017 4:23 am

Re: How to get the initial size of a wxTextCtrl later after its creation?

Post by tomay3000 » Thu May 13, 2021 5:00 pm

Thank you.

The solution to my problem is that I should replace every SetSize() by SetMinSize() because the sizer will always override the size set by SetSize().
And, as said in wxWindow::GetBestSize() documentation https://docs.wxwidgets.org/trunk/classw ... 9e319e35e3
Notice that the best size respects the minimal and maximal size explicitly set for the window, if any. So even if some window believes that it needs 200 pixels horizontally, calling SetMaxSize() with a width of 100 would ensure that GetBestSize() returns the width of at most 100 pixels.
So in my case since I am using (gonna use) SetMinSize(), then GetBestSize() is avoidable.

BN: SetSize() documentation never mentioned that it is useless to call it in controls added to a containing Sizer and should use SetMinSize() instead.

Post Reply