diff options
author | dec05eba <dec05eba@protonmail.com> | 2021-11-16 05:20:43 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2021-11-16 05:20:43 +0100 |
commit | 8df2299beba0f1465c3167edb67dd593c5c9e8db (patch) | |
tree | 6ca4fb34609919ba9c2b92b3a6f0636331d120c7 /src | |
parent | db02fe3f85165b09954d01d44a497abf8155b326 (diff) |
Window: add function to set window position, size and size limits
Diffstat (limited to 'src')
-rw-r--r-- | src/window/window.c | 76 |
1 files changed, 46 insertions, 30 deletions
diff --git a/src/window/window.c b/src/window/window.c index da163da..15faff3 100644 --- a/src/window/window.c +++ b/src/window/window.c @@ -216,6 +216,13 @@ static int mgl_window_init(mgl_window *self, const char *title, const mgl_window self->frame_time_limit = 0.0; mgl_clock_init(&self->frame_timer); + mgl_vec2i window_size = params ? params->size : (mgl_vec2i){ 0, 0 }; + if(window_size.x <= 0 || window_size.y <= 0) { + window_size.x = 1; + window_size.y = 1; + } + self->size = window_size; + self->context = malloc(sizeof(x11_context)); if(!self->context) { fprintf(stderr, "Failed to allocate x11 context\n"); @@ -270,7 +277,7 @@ static int mgl_window_init(mgl_window *self, const char *title, const mgl_window self->window = existing_window; } else { self->window = XCreateWindow(context->connection, parent_window, params->position.x, params->position.y, - params->size.x, params->size.y, 0, + window_size.x, window_size.y, 0, ((XVisualInfo*)context->visual_info)->depth, InputOutput, ((XVisualInfo*)context->visual_info)->visual, CWColormap | CWEventMask | CWBitGravity, &window_attr); XFreeColormap(context->connection, color_map); @@ -284,29 +291,8 @@ static int mgl_window_init(mgl_window *self, const char *title, const mgl_window 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"); - } - } + if(params) + mgl_window_set_size_limits(self, params->min_size, params->min_size); /* TODO: Call XGetWMProtocols and add wm_delete_window_atom on top, to not overwrite existing wm protocol atoms */ Atom wm_protocol_atoms[2] = { @@ -326,17 +312,12 @@ static int mgl_window_init(mgl_window *self, const char *title, const mgl_window context->gl.glEnableClientState(GL_TEXTURE_COORD_ARRAY); context->gl.glEnableClientState(GL_COLOR_ARRAY); - XWindowAttributes gwa; - gwa.width = 0; - gwa.height = 0; - XGetWindowAttributes(context->connection, self->window, &gwa); - Window dummy_w; int dummy_i; unsigned int dummy_u; XQueryPointer(context->connection, self->window, &dummy_w, &dummy_w, &dummy_i, &dummy_i, &self->cursor_position.x, &self->cursor_position.y, &dummy_u); - mgl_window_on_resize(self, gwa.width, gwa.height); + mgl_window_on_resize(self, self->size.x, self->size.y); x11_context->xim = XOpenIM(context->connection, NULL, NULL, NULL); if(!x11_context->xim) { @@ -363,6 +344,7 @@ int mgl_window_create(mgl_window *self, const char *title, const mgl_window_crea return mgl_window_init(self, title, params, None); } +/* TODO: Test this */ int mgl_window_init_from_existing_window(mgl_window *self, mgl_window_handle existing_window) { return mgl_window_init(self, "", NULL, existing_window); } @@ -687,3 +669,37 @@ void mgl_window_set_framerate_limit(mgl_window *self, int fps) { else self->frame_time_limit = 1.0 / fps; } + +void mgl_window_set_position(mgl_window *self, mgl_vec2i position) { + XMoveWindow(mgl_get_context()->connection, self->window, position.x, position.y); +} + +void mgl_window_set_size(mgl_window *self, mgl_vec2i size) { + XResizeWindow(mgl_get_context()->connection, self->window, size.x, size.y); +} + +void mgl_window_set_size_limits(mgl_window *self, mgl_vec2i minimum, mgl_vec2i maximum) { + XSizeHints *size_hints = XAllocSizeHints(); + if(size_hints) { + size_hints->width = self->size.x; + size_hints->min_width = minimum.x; + size_hints->max_width = maximum.x; + + size_hints->height = self->size.y; + size_hints->min_height = minimum.y; + size_hints->max_height = maximum.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; + + mgl_context *context = mgl_get_context(); + XSetWMNormalHints(context->connection, self->window, size_hints); + XFree(size_hints); + XSync(context->connection, False); + } else { + fprintf(stderr, "Warning: failed to set window size hints\n"); + } +} |