0

I'm building an app using wxWidgets. I have a wxTextCtrl created like so:

Expression = new wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, 
    wxTE_READONLY | wxNO_BORDER | wxTE_RIGHT );
Expression->SetForegroundColour(wxColour(55, 55, 55));
Expression->Bind(wxEVT_TEXT, [this](wxCommandEvent& evt) { Expression_Update(); evt.Skip(); });
Expression->Bind(wxEVT_SIZE, [this](wxSizeEvent& evt) { Expression_Update(); evt.Skip(); });
sizer->Add(Expression, 10, wxEXPAND);

It is bind to an Expression_Update() function which is supposed to scale its font correctly.

void Main::Expression_Update()
{
    wxString text = Expression->GetValue();
    const int MaxWidth = GetSize().GetWidth();

    int X, Y = Expression->GetSize().GetHeight();

    wxFont* font = new wxFont(
        wxSize(wxSize(0, Y / 1.3)),
        wxFONTFAMILY_SWISS,
        wxFONTSTYLE_NORMAL,
        wxFONTWEIGHT_BOLD,
        false,
        "Calibri"
    );

    Expression->GetTextExtent(text, &X, nullptr, NULL, NULL, font);

    if ((X + EXP_ANTI_CLIPPING_COEF) > MaxWidth)
    {
        do
        {
            int const fontHeight = font->GetPixelSize().GetHeight();
            font->SetPixelSize(wxSize(0, fontHeight - 2));
            Expression->GetTextExtent(text, &X, nullptr, NULL, NULL, font);
        } while ((X + EXP_ANTI_CLIPPING_COEF) > MaxWidth);
    }

    Expression->SetFont(*font);
    delete font;
}

This code works perfectly fine. However, if I add the wxTE_MULTILINE flag to the constructor:

Expression = new wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, 
    wxTE_READONLY | wxNO_BORDER | wxTE_RIGHT | wxTE_MULTILINE );

...when I resize the window, the wxTextCtrl grows larger and larger until it fills up the entire frame. I did some debugging myself, and I figured out the problem:

int X, Y = Expression->GetSize().GetHeight();

Without the wxTE_MULTILINE flag, Y has a starting value of 88 and changes correctly based on the control's height when I resize. But with the multiline flag set, Y starts with 88, and when I resize the window, regardless if the window height increased or not, the value changes to 164, and when I resize again, to 308, and so on.

TLDR: The GetSize().GetHeight() method returns an incorrect height value that causes the font to grow larger and larger until the text control fills up the entire window.

  • what platform and wx version? What do you need to scale font yourself? What is your monitor resolution? – Igor Jul 24 '21 at 14:22
  • I'm on Windows 10, I don't know my wxWidgets version and don't know how to check it (should be the latest), I need to scale the font to make it larger if the window gets taller and smaller if the text is too big to fit in the window, my monitor resolution is 1920x1080. – giorgio_developer Jul 24 '21 at 20:26
  • @giorgio_developet, are making a software for a blind people? Do you plan to have HiDPI support? – Igor Jul 25 '21 at 14:29
  • I don't know what HiDPI is, and no, I'm not making a software for blind people, it's just a calculator application. – giorgio_developer Jul 25 '21 at 14:49
  • HiDPI means "High Definition Monitor". In regards to font - why not make a font big initially with setting the window size big. And then call `SetSizeHints()`. In regards to your problem - you are adding the control with wxEXPAND flag to (presumably) horizontal sizer. Try to change the proportion and remove that flag. – Igor Jul 25 '21 at 16:17
  • The `wxTextCtrl` is added to a vertical `wxBoxSizer`. I tried removing the `wxEXPAND` flag, but not only does the problem still persist, but it is also glitched and far off to the left of the screen. – giorgio_developer Jul 26 '21 at 08:17

0 Answers0