From 6b4a5a128ef5d87374dfef017cd20e069c068a4b Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 12 Sep 2015 15:22:49 -0400 Subject: Add support for aspect ratio constraints. Currently only implemented on X11. --- pugl/pugl.h | 13 +++++++++++++ pugl/pugl_internal.h | 17 +++++++++++++++++ pugl/pugl_x11.c | 18 ++++++++++++++---- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/pugl/pugl.h b/pugl/pugl.h index 53115ff..3ff66f9 100644 --- a/pugl/pugl.h +++ b/pugl/pugl.h @@ -170,6 +170,19 @@ puglInitWindowSize(PuglView* view, int width, int height); PUGL_API void puglInitWindowMinSize(PuglView* view, int width, int height); +/** + Set the window aspect ratio range before creating a window. + + The x and y values here represent a ratio of width to height. To set a + fixed aspect ratio, set the minimum and maximum values to the same ratio. +*/ +PUGL_API void +puglInitWindowAspectRatio(PuglView* view, + int min_x, + int min_y, + int max_x, + int max_y); + /** Enable or disable resizing before creating a window. */ diff --git a/pugl/pugl_internal.h b/pugl/pugl_internal.h index f83f2c7..1006c90 100644 --- a/pugl/pugl_internal.h +++ b/pugl/pugl_internal.h @@ -55,6 +55,10 @@ struct PuglViewImpl { int height; int min_width; int min_height; + int min_aspect_x; + int min_aspect_y; + int max_aspect_x; + int max_aspect_y; int mods; bool mouse_in_view; bool ignoreKeyRepeat; @@ -99,6 +103,19 @@ puglInitWindowMinSize(PuglView* view, int width, int height) view->min_height = height; } +void +puglInitWindowAspectRatio(PuglView* view, + int min_x, + int min_y, + int max_x, + int max_y) +{ + view->min_aspect_x = min_x; + view->min_aspect_y = min_y; + view->max_aspect_x = max_x; + view->max_aspect_y = max_y; +} + void puglInitWindowParent(PuglView* view, PuglNativeWindow parent) { diff --git a/pugl/pugl_x11.c b/pugl/pugl_x11.c index bd85b37..6375609 100644 --- a/pugl/pugl_x11.c +++ b/pugl/pugl_x11.c @@ -214,10 +214,20 @@ puglCreateWindow(PuglView* view, const char* title) sizeHints.max_width = view->width; sizeHints.max_height = view->height; XSetNormalHints(impl->display, impl->win, &sizeHints); - } else if (view->min_width || view->min_height) { - sizeHints.flags = PMinSize; - sizeHints.min_width = view->min_width; - sizeHints.min_height = view->min_height; + } else { + if (view->min_width || view->min_height) { + sizeHints.flags = PMinSize; + sizeHints.min_width = view->min_width; + sizeHints.min_height = view->min_height; + } + if (view->min_aspect_x) { + sizeHints.flags |= PAspect; + sizeHints.min_aspect.x = view->min_aspect_x; + sizeHints.min_aspect.y = view->min_aspect_y; + sizeHints.max_aspect.x = view->max_aspect_x; + sizeHints.max_aspect.y = view->max_aspect_y; + } + XSetNormalHints(impl->display, impl->win, &sizeHints); } -- cgit v1.2.1