From db02fe3f85165b09954d01d44a497abf8155b326 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 16 Nov 2021 04:49:07 +0100 Subject: Window: allow setting window minimum/max size and position --- include/mgl/window/window.h | 12 +++++++++--- src/window/window.c | 40 +++++++++++++++++++++++++++++++--------- tests/main.c | 2 +- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/include/mgl/window/window.h b/include/mgl/window/window.h index a63bc03..20c700a 100644 --- a/include/mgl/window/window.h +++ b/include/mgl/window/window.h @@ -34,9 +34,15 @@ struct mgl_window { mgl_clock frame_timer; }; -int mgl_window_create(mgl_window *self, const char *title, int width, int height); -/* if |parent_window| is 0 then the root window is used */ -int mgl_window_create_with_params(mgl_window *self, const char *title, int width, int height, mgl_window_handle parent_window); +typedef struct { + mgl_vec2i position; + mgl_vec2i size; + mgl_vec2i min_size; /* (0, 0) = no limit */ + mgl_vec2i max_size; /* (0, 0) = no limit */ + mgl_window_handle parent_window; /* 0 = root window */ +} mgl_window_create_params; + +int mgl_window_create(mgl_window *self, const char *title, const mgl_window_create_params *params); int mgl_window_init_from_existing_window(mgl_window *self, mgl_window_handle existing_window); void mgl_window_deinit(mgl_window *self); diff --git a/src/window/window.c b/src/window/window.c index 5cc7ba1..da163da 100644 --- a/src/window/window.c +++ b/src/window/window.c @@ -208,11 +208,7 @@ static void mgl_window_on_resize(mgl_window *self, int width, int height) { mgl_window_set_view(self, &view); } -int mgl_window_create(mgl_window *self, const char *title, int width, int height) { - return mgl_window_create_with_params(self, title, width, height, 0); -} - -static int mgl_window_init(mgl_window *self, const char *title, int width, int height, mgl_window_handle parent_window, Window existing_window) { +static int mgl_window_init(mgl_window *self, const char *title, const mgl_window_create_params *params, Window existing_window) { self->window = 0; self->context = NULL; self->open = false; @@ -235,6 +231,7 @@ static int mgl_window_init(mgl_window *self, const char *title, int width, int h mgl_context *context = mgl_get_context(); + Window parent_window = params ? params->parent_window : None; if(parent_window == 0) parent_window = DefaultRootWindow(context->connection); @@ -272,7 +269,8 @@ static int mgl_window_init(mgl_window *self, const char *title, int width, int h XFreeColormap(context->connection, color_map); self->window = existing_window; } else { - self->window = XCreateWindow(context->connection, parent_window, 0, 0, width, height, 0, + self->window = XCreateWindow(context->connection, parent_window, params->position.x, params->position.y, + params->size.x, params->size.y, 0, ((XVisualInfo*)context->visual_info)->depth, InputOutput, ((XVisualInfo*)context->visual_info)->visual, CWColormap | CWEventMask | CWBitGravity, &window_attr); XFreeColormap(context->connection, color_map); @@ -286,6 +284,30 @@ static int mgl_window_init(mgl_window *self, const char *title, int width, int h XMapWindow(context->connection, self->window); } + if(params) { + XSizeHints *size_hints = XAllocSizeHints(); + if(size_hints) { + size_hints->width = params->size.x; + size_hints->min_width = params->min_size.x; + size_hints->max_width = params->max_size.x; + + size_hints->height = params->size.y; + size_hints->min_height = params->min_size.y; + size_hints->max_height = params->max_size.y; + + size_hints->flags = PSize; + if(size_hints->min_width || size_hints->min_height) + size_hints->flags |= PMinSize; + if(size_hints->max_width || size_hints->max_height) + size_hints->flags |= PMaxSize; + + XSetWMNormalHints(context->connection, self->window, size_hints); + XFree(size_hints); + } else { + fprintf(stderr, "Warning: failed to set window size hints\n"); + } + } + /* TODO: Call XGetWMProtocols and add wm_delete_window_atom on top, to not overwrite existing wm protocol atoms */ Atom wm_protocol_atoms[2] = { context->wm_delete_window_atom, @@ -337,12 +359,12 @@ static int mgl_window_init(mgl_window *self, const char *title, int width, int h return 0; } -int mgl_window_create_with_params(mgl_window *self, const char *title, int width, int height, mgl_window_handle parent_window) { - return mgl_window_init(self, title, width, height, parent_window, None); +int mgl_window_create(mgl_window *self, const char *title, const mgl_window_create_params *params) { + return mgl_window_init(self, title, params, None); } int mgl_window_init_from_existing_window(mgl_window *self, mgl_window_handle existing_window) { - return mgl_window_init(self, "", 0, 0, 0, existing_window); + return mgl_window_init(self, "", NULL, existing_window); } void mgl_window_deinit(mgl_window *self) { diff --git a/tests/main.c b/tests/main.c index f76f384..51487dc 100644 --- a/tests/main.c +++ b/tests/main.c @@ -214,7 +214,7 @@ int main(int argc, char **argv) { mgl_clock_init(&userdata.clock); mgl_window window; - if(mgl_window_create(&window, "mgl", 1280, 720) != 0) + if(mgl_window_create(&window, "mgl", &(mgl_window_create_params){ .size = {1280, 720}, .min_size = { 1000, 1000 } }) != 0) return 1; if(mgl_texture_init(&texture) != 0) -- cgit v1.2.3