How to size font on a panel with OnSize event

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
salmonswes
Earned a small fee
Earned a small fee
Posts: 11
Joined: Thu Feb 21, 2019 9:41 pm

How to size font on a panel with OnSize event

Post by salmonswes »

Hello, I am having problems with having font size correctly with a panel OnSize event. What I want is I want the font to size with some type of ratio (can be defined later it isn't super important right now). The problem that I am having is that when I catch the size event, edit my font, the font never actually gets changed on the panel. The on size method that I have currently is as shown


In this code snip, windowsizex and windowsizey are both variables that hold the current window size. When I print out the wxLogDebug for the resizefactor it comes out to values that would make sense to me given how much the panel has been resized. The sizer variable is the "topsizer" that I am using on my panel.

Code: Select all


void TestGUI::OnSize(wxSizeEvent& event)
{
    wxLogDebug("On Size Event!");
    int tempx = windowsizex;
    int tempy = windowsizey;
    float resizefactor = 0;
    wxLogDebug("%d" , tempx);
    wxLogDebug("%d" , tempy);
    wxSize winsize = event.GetSize();
    windowsizex = winsize.GetWidth();
    windowsizey = winsize.GetHeight();


    if(tempx != 0 && tempy != 0)
    {
        resizefactor = (((pow(sqrt(tempx / windowsizex),2)) + (pow(sqrt(tempy/windowsizey),2))) / (sqrt(2)));
        resizefactor = (((windowsizex / tempx) + (windowsizey/ tempy))/1.4);
        wxLogDebug("%.6f", resizefactor);
        myFont->Scale(resizefactor);
        SetFont(*myFont);
        TestPanel->SetFont(*myFont);
        //Refresh();
    }
    sizer->Layout();
    Refresh();
    event.Skip();



}

Any help with this topic would be awesome! :D :D
catalin
Moderator
Moderator
Posts: 1618
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: How to size font on a panel with OnSize event

Post by catalin »

You showed too little code. What are TestGUI and TestPanel and what exactly should they use *myFont for?
If you do custom painting, do you set the font of the wxDC to the font of the calling window in your paint handler?
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to size font on a panel with OnSize event

Post by doublemax »

Most likely the reason is:

When you change the font for a window, it will *not* recursively change the font for all its children. Only when a new wxWindow is created, it will by default inherit the font from its parent. If TestGUI has children and you want to change their font size, you have to do that yourself for all of them.

Apart from that:
- you should not use wxFont::Scale() every time. This will accumulate an error and over time you will get bad results. Make your calculations from a base size and set an absolute font size

- calling sizer->Layout should be superfluous
Use the source, Luke!
salmonswes
Earned a small fee
Earned a small fee
Posts: 11
Joined: Thu Feb 21, 2019 9:41 pm

Re: How to size font on a panel with OnSize event

Post by salmonswes »

doublemax wrote: Thu Feb 28, 2019 11:19 pm Most likely the reason is:

When you change the font for a window, it will *not* recursively change the font for all its children. Only when a new wxWindow is created, it will by default inherit the font from its parent. If TestGUI has children and you want to change their font size, you have to do that yourself for all of them.

Apart from that:
- you should not use wxFont::Scale() every time. This will accumulate an error and over time you will get bad results. Make your calculations from a base size and set an absolute font size

- calling sizer->Layout should be superfluous
Is there a simple way to recursively go through all of the panels children without having to use their IDs?
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to size font on a panel with OnSize event

Post by doublemax »

salmonswes wrote: Fri Mar 01, 2019 3:27 pm
doublemax wrote: Thu Feb 28, 2019 11:19 pm Most likely the reason is:

When you change the font for a window, it will *not* recursively change the font for all its children. Only when a new wxWindow is created, it will by default inherit the font from its parent. If TestGUI has children and you want to change their font size, you have to do that yourself for all of them.

Apart from that:
- you should not use wxFont::Scale() every time. This will accumulate an error and over time you will get bad results. Make your calculations from a base size and set an absolute font size

- calling sizer->Layout should be superfluous
Is there a simple way to recursively go through all of the panels children without having to use their IDs?
https://wiki.wxwidgets.org/Catching_key ... ve_connect

This code shows how to connect an event handler recursively. The principle is the same.
Use the source, Luke!
Post Reply