Skip to content

Commit

Permalink
Add GLFW_WIN32_DPI_RESIZE
Browse files Browse the repository at this point in the history
Related to #676.
Related to #1115.
  • Loading branch information
elmindreda committed Aug 22, 2018
1 parent 1ad8227 commit cd89176
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 4 deletions.
10 changes: 10 additions & 0 deletions docs/window.dox
Expand Up @@ -479,6 +479,15 @@ ASCII encoded class and instance parts of the ICCCM `WM_CLASS` window property.
These are set with @ref glfwWindowHintString.


@subsubsection window_hints_win32 Win32 specific window hints

@anchor GLFW_WIN32_DPI_RESIZE
__GLFW_WIN32_DPI_RESIZE__ specifies whether to scale the window content size by
the content scale of the monitor it is placed on. This includes scaling it at
creation. Possible values are `GLFW_TRUE` and `GLFW_FALSE`. This is ignored on
other platforms.


@subsubsection window_hints_values Supported and default values

Window hint | Default value | Supported values
Expand Down Expand Up @@ -523,6 +532,7 @@ GLFW_COCOA_FRAME_NAME | `""` | A UTF-8 encoded fr
GLFW_COCOA_GRAPHICS_SWITCHING | `GLFW_FALSE` | `GLFW_TRUE` or `GLFW_FALSE`
GLFW_X11_CLASS_NAME | `""` | An ASCII encoded `WM_CLASS` class name
GLFW_X11_INSTANCE_NAME | `""` | An ASCII encoded `WM_CLASS` instance name
GLFW_WIN32_DPI_RESIZE | `GLFW_FALSE` | `GLFW_TRUE` or `GLFW_FALSE`


@section window_events Window event processing
Expand Down
2 changes: 2 additions & 0 deletions examples/boing.c
Expand Up @@ -627,6 +627,8 @@ int main( void )
if( !glfwInit() )
exit( EXIT_FAILURE );

glfwWindowHint(GLFW_WIN32_DPI_RESIZE, GLFW_TRUE);

window = glfwCreateWindow( 400, 400, "Boing (classic Amiga demo)", NULL, NULL );
if (!window)
{
Expand Down
2 changes: 2 additions & 0 deletions include/GLFW/glfw3.h
Expand Up @@ -980,6 +980,8 @@ extern "C" {

#define GLFW_X11_CLASS_NAME 0x00024001
#define GLFW_X11_INSTANCE_NAME 0x00024002

#define GLFW_WIN32_DPI_RESIZE 0x00025001
/*! @} */

#define GLFW_NO_API 0
Expand Down
3 changes: 3 additions & 0 deletions src/internal.h
Expand Up @@ -276,6 +276,9 @@ struct _GLFWwndconfig
char className[256];
char instanceName[256];
} x11;
struct {
GLFWbool dpiResize;
} win32;
};

// Context configuration
Expand Down
1 change: 1 addition & 0 deletions src/win32_platform.h
Expand Up @@ -306,6 +306,7 @@ typedef struct _GLFWwindowWin32
GLFWbool maximized;
// Whether to enable framebuffer transparency on DWM
GLFWbool transparent;
GLFWbool dpiResize;

// The last received cursor position, regardless of source
int lastCursorPosX, lastCursorPosY;
Expand Down
30 changes: 26 additions & 4 deletions src/win32_window.c
Expand Up @@ -1063,6 +1063,9 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg,

case WM_GETDPISCALEDSIZE:
{
if (window->win32.dpiResize)
break;

// Adjust the window size to keep the client area size constant
if (_glfwIsWindows10CreatorsUpdateOrGreaterWin32())
{
Expand Down Expand Up @@ -1230,15 +1233,34 @@ static int createNativeWindow(_GLFWwindow* window,
WM_COPYGLOBALDATA, MSGFLT_ALLOW, NULL);
}

// Adjust window size to account for the DPI scaled window frame
window->win32.dpiResize = wndconfig->win32.dpiResize;

// Adjust window size to account for DPI scaling of the window frame and
// optionally DPI scaling of the client area
// This cannot be done until we know what monitor it was placed on
if (_glfwIsWindows10AnniversaryUpdateOrGreaterWin32() && !window->monitor)
if (!window->monitor)
{
RECT rect = { 0, 0, wndconfig->width, wndconfig->height };

if (wndconfig->win32.dpiResize)
{
float xscale, yscale;
_glfwPlatformGetWindowContentScale(window, &xscale, &yscale);
rect.right = (int) (rect.right * xscale);
rect.bottom = (int) (rect.bottom * yscale);
}

ClientToScreen(window->win32.handle, (POINT*) &rect.left);
ClientToScreen(window->win32.handle, (POINT*) &rect.right);
AdjustWindowRectExForDpi(&rect, style, FALSE, exStyle,
GetDpiForWindow(window->win32.handle));

if (_glfwIsWindows10AnniversaryUpdateOrGreaterWin32())
{
AdjustWindowRectExForDpi(&rect, style, FALSE, exStyle,
GetDpiForWindow(window->win32.handle));
}
else
AdjustWindowRectEx(&rect, style, FALSE, exStyle);

SetWindowPos(window->win32.handle, NULL,
rect.left, rect.top,
rect.right - rect.left, rect.bottom - rect.top,
Expand Down
3 changes: 3 additions & 0 deletions src/window.c
Expand Up @@ -369,6 +369,9 @@ GLFWAPI void glfwWindowHint(int hint, int value)
case GLFW_COCOA_GRAPHICS_SWITCHING:
_glfw.hints.context.nsgl.offline = value ? GLFW_TRUE : GLFW_FALSE;
return;
case GLFW_WIN32_DPI_RESIZE:
_glfw.hints.window.win32.dpiResize = value ? GLFW_TRUE : GLFW_FALSE;
return;
case GLFW_CENTER_CURSOR:
_glfw.hints.window.centerCursor = value ? GLFW_TRUE : GLFW_FALSE;
return;
Expand Down

0 comments on commit cd89176

Please sign in to comment.