From 2cb5bf0dcc0623e2326deb7442968cfa63c3e632 Mon Sep 17 00:00:00 2001 From: Andy McFadden Date: Fri, 7 Jan 2022 11:34:46 -0800 Subject: [PATCH] Fix file viewer layout If scaling > 100% is enabled on the primary display, the controls in the dialog window get bigger. The file viewer uses hard-coded values for the initial and minimum window size, which causes the expanded controls to draw on top of each other. (Issue #53.) We now query the system for the scale factor, and adjust the window size accordingly. --- app/Preferences.cpp | 12 ++++++++---- app/ViewFilesDialog.cpp | 8 ++++++-- util/Util.cpp | 21 +++++++++++++++++++++ util/Util.h | 7 +++++++ 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/app/Preferences.cpp b/app/Preferences.cpp index e64263f..b84e980 100644 --- a/app/Preferences.cpp +++ b/app/Preferences.cpp @@ -247,16 +247,20 @@ Preferences::Preferences(void) SetPrefString(kPrViewTextTypeFace, L"Courier New"); SetPrefLong(kPrViewTextPointSize, 10); - long width = 680 + /* exact width for 80-column text */ - ::GetSystemMetrics(SM_CXVSCROLL); - long height = 516; /* exact height for file viewer to show IIgs graphic */ + + float scaleFactor = GetDesktopScaleFactor(); + /* exact width for 80-column text; note text is scaled up */ + long width = (int)(680 * scaleFactor) + ::GetSystemMetrics(SM_CXVSCROLL); + /* exact height for file viewer to show IIgs graphic at 1.0x */ + long height = (int)(516 * scaleFactor); + if (GetSystemMetrics(SM_CXSCREEN) < width) width = GetSystemMetrics(SM_CXSCREEN); if (GetSystemMetrics(SM_CYSCREEN) < height) height = GetSystemMetrics(SM_CYSCREEN); // may overlap system bar - //width = 640; height = 480; SetPrefLong(kPrFileViewerWidth, width); SetPrefLong(kPrFileViewerHeight, height); + SetPrefLong(kPrDiskImageCreateFormat, -1); } diff --git a/app/ViewFilesDialog.cpp b/app/ViewFilesDialog.cpp index 57482fc..156a261 100644 --- a/app/ViewFilesDialog.cpp +++ b/app/ViewFilesDialog.cpp @@ -154,8 +154,12 @@ void ViewFilesDialog::OnCancel(void) void ViewFilesDialog::OnGetMinMaxInfo(MINMAXINFO* pMMI) { - pMMI->ptMinTrackSize.x = 664; - pMMI->ptMinTrackSize.y = 200; + float scaleFactor = GetDesktopScaleFactor(); + + // 664x200 was determined empirically to fit all controls. The initial + // window size is set over in the Preferences constructor. + pMMI->ptMinTrackSize.x = (int)(664 * scaleFactor); + pMMI->ptMinTrackSize.y = (int)(200 * scaleFactor); } void ViewFilesDialog::OnSize(UINT nType, int cx, int cy) diff --git a/util/Util.cpp b/util/Util.cpp index 3b01a6a..96c04da 100644 --- a/util/Util.cpp +++ b/util/Util.cpp @@ -417,6 +417,27 @@ void CheckedLoadString(CString* pString, UINT nID) } } +float GetDesktopScaleFactor() +{ + // The current scale factor for controls is determined by the settings + // for the main display at the time the application was launched. If we + // don't do this, the controls will get all scrunched together if the + // scale is over 100% (issue #53). The settings API doesn't seem to + // allow scaling things down, so we're only worried about expansion here. + // + // https://docs.microsoft.com/en-us/windows-hardware/manufacture/desktop/dpi-related-apis-and-registry-settings?view=windows-11#primary-display-dpi-scale-factor + // Get desktop dc + HDC desktopDc = ::GetDC(NULL); + // Get native resolution + int horizontalDPI = GetDeviceCaps(desktopDc, LOGPIXELSX); + int verticalDPI = GetDeviceCaps(desktopDc, LOGPIXELSY); + + float scaleFactor = horizontalDPI / 96.0f; + LOGD("Native DPI: %dx%d, scaleFactor=%.3f", + horizontalDPI, verticalDPI, scaleFactor); + return scaleFactor; +} + /* * =========================================================================== diff --git a/util/Util.h b/util/Util.h index 2f62b7a..80edebe 100644 --- a/util/Util.h +++ b/util/Util.h @@ -199,6 +199,13 @@ bool IsWin9x(void); */ void CheckedLoadString(CString* pString, UINT nID); +/* + * Returns the desktop scaling factor (usually 1.0). Assumes vertical and + * horizontal scaling is identical. + */ +float GetDesktopScaleFactor(); + + /* * ==================================== * Miscellaneous functions