How does the FromDIP function work under different DPI scaling?

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
ollydbg23
Super wx Problem Solver
Super wx Problem Solver
Posts: 438
Joined: Fri Dec 12, 2008 10:31 am

How does the FromDIP function work under different DPI scaling?

Post by ollydbg23 »

Hi, I'm under Win10, which has DPI scaling. I'm testing the wxWidgets' sample code named "display". To understand how the FromDIP works, I add a "Hello world" button to the sample code, here is the code changes:

Code: Select all

diff --git a/samples/display/display.cpp b/samples/display/display.cpp
index 58f18de..a981120 100644
--- a/samples/display/display.cpp
+++ b/samples/display/display.cpp
@@ -356,6 +356,8 @@ void MyFrame::PopuplateWithDisplayInfo()
         sizer->Add(new wxStaticText(page, wxID_ANY, "Current: "));
         sizer->Add(new wxStaticText(page, Display_CurrentMode, currentMode));
 
+        sizer->Add(new wxButton(page, wxID_ANY, _T("Hello World"), wxDefaultPosition, wxWindow::FromDIP(wxSize(250,50), 0)));
+
         sizerTop->Add(new wxButton(page, Display_ResetMode, "&Reset mode"),
                       wxSizerFlags().Centre().Border());
 #endif // wxUSE_DISPLAY
Now, when staring the "display" program, I have my monitor DPI as 100%, here is the screen shot, the button size is 250*50, which is correct. See image shot below:
2023-03-30 08 49 56.png
2023-03-30 08 49 56.png (14.17 KiB) Viewed 1355 times
Now, the program is still running, and I change the DPI to 150%, and the "display" program becomes below, the button size is now 375*75, which is 1.5 times of the original size. See image show below:
2023-03-30 08 50 39.png
2023-03-30 08 50 39.png (23.27 KiB) Viewed 1355 times
Finally, I just click the "Reset mode" button of the program, and the whole window becomes below, the button size is return back to 250*50. See image below:
2023-03-30 08 51 05.png
2023-03-30 08 51 05.png (25.63 KiB) Viewed 1346 times
Here is my question.

1, I'm not sure how the "Reset mode" button does, I see it just call the

Code: Select all

void MyFrame::OnResetMode(wxCommandEvent& WXUNUSED(event))
{
    wxDisplay dpy(m_book->GetSelection());

    dpy.ResetMode();
}

2, Why in the second screen shot, I have a 375*75 size button? From my understanding, the third screen shot is correct, while the second screen shot with 375*75 button is wrong.

I mean when I use such code:

Code: Select all

sizer->Add(new wxButton(page, wxID_ANY, _T("Hello World"), wxDefaultPosition, wxWindow::FromDIP(wxSize(250,50), 0)));
The actual size of the button should be always 250 physical pixel * 50 physical pixel on my screen, whenever I was in DPI scaling 100% or 150%.

The whole testing code was in: https://github.com/asmwarrior/cb_projec ... isplay.cpp, while the resource file is in its root folder.

Any help and suggestion are welcome, thanks!
Last edited by ollydbg23 on Thu Mar 30, 2023 4:33 am, edited 1 time in total.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7449
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: How does the FromDIP function work under different DPI scaling?

Post by ONEEYEMAN »

Hi,
Why do you think 150 scale is incorrect?

It is correct - it scales the size to 150 just like it should.

Thank you.
ollydbg23
Super wx Problem Solver
Super wx Problem Solver
Posts: 438
Joined: Fri Dec 12, 2008 10:31 am

Re: How does the FromDIP function work under different DPI scaling?

Post by ollydbg23 »

ONEEYEMAN wrote: Thu Mar 30, 2023 3:15 am Hi,
Why do you think 150 scale is incorrect?

It is correct - it scales the size to 150 just like it should.

Thank you.
In the second screen shot, the button size becomes 375*75 (physical pixel). I think this is not correct.

Because I think when DPI scale 150% is used, the actual size of the button on the screen should always be 250*50 physical pixel. That's why the FromDIP function is used for.
Last edited by ollydbg23 on Thu Mar 30, 2023 11:05 pm, edited 1 time in total.
ollydbg23
Super wx Problem Solver
Super wx Problem Solver
Posts: 438
Joined: Fri Dec 12, 2008 10:31 am

Re: How does the FromDIP function work under different DPI scaling?

Post by ollydbg23 »

It looks like the "display" sample does nothing when it got the DPI change event, see the code below:

Code: Select all

void MyFrame::OnDPIChanged(wxDPIChangedEvent& event)
{
    wxLogStatus(this, "DPI changed: was %d*%d, now %d*%d",
                event.GetOldDPI().x, event.GetOldDPI().y,
                event.GetNewDPI().x, event.GetNewDPI().y);

    event.Skip();
}
This code just show some text in the status bar.

So, in a real application code, we need to call the "ResetMode" code to reset all the UI of the application? Just like the "Reset mode" button did.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7449
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: How does the FromDIP function work under different DPI scaling?

Post by ONEEYEMAN »

Hi,
Is the status bar rescales itself? Or it's just the text there?

Thank you.
User avatar
doublemax
Moderator
Moderator
Posts: 19103
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How does the FromDIP function work under different DPI scaling?

Post by doublemax »

If you use absolute sizes anywhere, you have to recalculate and re-set them when the dpi changes.

Ideally you shouldn't use absolute sizes at all. But i know it can't always be avoided.
Use the source, Luke!
ollydbg23
Super wx Problem Solver
Super wx Problem Solver
Posts: 438
Joined: Fri Dec 12, 2008 10:31 am

Re: How does the FromDIP function work under different DPI scaling?

Post by ollydbg23 »

ONEEYEMAN wrote: Thu Mar 30, 2023 12:34 pm Hi,
Is the status bar rescales itself? Or it's just the text there?

Thank you.
The status bar re-scale itself correctly.
ollydbg23
Super wx Problem Solver
Super wx Problem Solver
Posts: 438
Joined: Fri Dec 12, 2008 10:31 am

Re: How does the FromDIP function work under different DPI scaling?

Post by ollydbg23 »

doublemax wrote: Thu Mar 30, 2023 4:30 pm If you use absolute sizes anywhere, you have to recalculate and re-set them when the dpi changes.

Ideally you shouldn't use absolute sizes at all. But i know it can't always be avoided.
Yes, you are correct.

In the wx-user mail-list, wx developer VZ gave me good explanation(see here: What is the correct way to response to the wxEVT_DPI_CHANGED event?), so I think I understand it right now.

Thanks.
ollydbg23
Super wx Problem Solver
Super wx Problem Solver
Posts: 438
Joined: Fri Dec 12, 2008 10:31 am

Re: How does the FromDIP function work under different DPI scaling?

Post by ollydbg23 »

ONEEYEMAN wrote: Thu Mar 30, 2023 3:15 am Hi,
Why do you think 150 scale is incorrect?

It is correct - it scales the size to 150 just like it should.

Thank you.
I finally think you are correct! Thanks.
ollydbg23
Super wx Problem Solver
Super wx Problem Solver
Posts: 438
Joined: Fri Dec 12, 2008 10:31 am

Re: How does the FromDIP function work under different DPI scaling?

Post by ollydbg23 »

Guys, I'm sorry that I have make a big mistake.

In my test code, it has such line:

Code: Select all

sizer->Add(new wxButton(page, wxID_ANY, _T("Hello World"), wxDefaultPosition, wxWindow::FromDIP(wxSize(250,50), 0)));
Which is totally wrong here, because I have used the wrong argument for the FromDIP( wxSize(250,50), 0 ), the second argument 0 is totally wrong.

Now, I have removed the second argument, and the result is: when I click the ResetMode button, I don't have the reduced button size, which is correct!

I'm sorry about that, I think it was a copy and paste error. Also, when we pass the second 0 argument, can the function report something, because it will call a function below:

Code: Select all

static wxSize wxWindow::FromDIP( const wxSize &  sz, const wxWindow * w) 	
Where the w pointer is 0.
Post Reply