[f7a90df] | 1 | /*
|
---|
| 2 | * Copyright (c) 2020 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup libui
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file Window
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <display.h>
|
---|
| 37 | #include <errno.h>
|
---|
| 38 | #include <gfx/context.h>
|
---|
[f03d1308] | 39 | #include <io/kbd_event.h>
|
---|
[d284ce9] | 40 | #include <io/pos_event.h>
|
---|
[f7a90df] | 41 | #include <mem.h>
|
---|
| 42 | #include <stdlib.h>
|
---|
| 43 | #include <ui/resource.h>
|
---|
| 44 | #include <ui/wdecor.h>
|
---|
| 45 | #include <ui/window.h>
|
---|
| 46 | #include "../private/dummygc.h"
|
---|
| 47 | #include "../private/ui.h"
|
---|
[d284ce9] | 48 | #include "../private/wdecor.h"
|
---|
[f7a90df] | 49 | #include "../private/window.h"
|
---|
| 50 |
|
---|
[d284ce9] | 51 | static void dwnd_close_event(void *);
|
---|
| 52 | static void dwnd_focus_event(void *);
|
---|
| 53 | static void dwnd_kbd_event(void *, kbd_event_t *);
|
---|
| 54 | static void dwnd_pos_event(void *, pos_event_t *);
|
---|
| 55 | static void dwnd_unfocus_event(void *);
|
---|
| 56 |
|
---|
| 57 | static display_wnd_cb_t dwnd_cb = {
|
---|
| 58 | .close_event = dwnd_close_event,
|
---|
| 59 | .focus_event = dwnd_focus_event,
|
---|
| 60 | .kbd_event = dwnd_kbd_event,
|
---|
| 61 | .pos_event = dwnd_pos_event,
|
---|
| 62 | .unfocus_event = dwnd_unfocus_event
|
---|
| 63 | };
|
---|
| 64 |
|
---|
| 65 | static void wd_close(ui_wdecor_t *, void *);
|
---|
| 66 | static void wd_move(ui_wdecor_t *, void *, gfx_coord2_t *);
|
---|
| 67 |
|
---|
| 68 | static ui_wdecor_cb_t wdecor_cb = {
|
---|
| 69 | .close = wd_close,
|
---|
| 70 | .move = wd_move
|
---|
| 71 | };
|
---|
| 72 |
|
---|
[f7a90df] | 73 | /** Initialize window parameters structure.
|
---|
| 74 | *
|
---|
| 75 | * Window parameters structure must always be initialized using this function
|
---|
| 76 | * first.
|
---|
| 77 | *
|
---|
| 78 | * @param params Window parameters structure
|
---|
| 79 | */
|
---|
[d284ce9] | 80 | void ui_wnd_params_init(ui_wnd_params_t *params)
|
---|
[f7a90df] | 81 | {
|
---|
[d284ce9] | 82 | memset(params, 0, sizeof(ui_wnd_params_t));
|
---|
[f7a90df] | 83 | }
|
---|
| 84 |
|
---|
| 85 | /** Create new window.
|
---|
| 86 | *
|
---|
| 87 | * @param ui User interface
|
---|
| 88 | * @param params Window parameters
|
---|
| 89 | * @param rwindow Place to store pointer to new window
|
---|
| 90 | * @return EOK on success or an error code
|
---|
| 91 | */
|
---|
[d284ce9] | 92 | errno_t ui_window_create(ui_t *ui, ui_wnd_params_t *params,
|
---|
[f7a90df] | 93 | ui_window_t **rwindow)
|
---|
| 94 | {
|
---|
| 95 | ui_window_t *window;
|
---|
| 96 | display_wnd_params_t dparams;
|
---|
| 97 | display_window_t *dwindow = NULL;
|
---|
| 98 | gfx_context_t *gc = NULL;
|
---|
| 99 | ui_resource_t *res = NULL;
|
---|
| 100 | ui_wdecor_t *wdecor = NULL;
|
---|
| 101 | dummy_gc_t *dgc = NULL;
|
---|
| 102 | errno_t rc;
|
---|
| 103 |
|
---|
| 104 | window = calloc(1, sizeof(ui_window_t));
|
---|
| 105 | if (window == NULL)
|
---|
| 106 | return ENOMEM;
|
---|
| 107 |
|
---|
| 108 | display_wnd_params_init(&dparams);
|
---|
[d284ce9] | 109 | dparams.rect = params->rect;
|
---|
[f7a90df] | 110 |
|
---|
| 111 | if (ui->display != NULL) {
|
---|
[d284ce9] | 112 | rc = display_window_create(ui->display, &dparams, &dwnd_cb,
|
---|
| 113 | (void *) window, &dwindow);
|
---|
[f7a90df] | 114 | if (rc != EOK)
|
---|
| 115 | goto error;
|
---|
| 116 |
|
---|
| 117 | rc = display_window_get_gc(dwindow, &gc);
|
---|
| 118 | if (rc != EOK)
|
---|
| 119 | goto error;
|
---|
| 120 | } else {
|
---|
| 121 | /* Needed for unit tests */
|
---|
| 122 | rc = dummygc_create(&dgc);
|
---|
| 123 | if (rc != EOK)
|
---|
| 124 | goto error;
|
---|
| 125 |
|
---|
| 126 | gc = dummygc_get_ctx(dgc);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | rc = ui_resource_create(gc, &res);
|
---|
| 130 | if (rc != EOK)
|
---|
| 131 | goto error;
|
---|
| 132 |
|
---|
| 133 | rc = ui_wdecor_create(res, params->caption, &wdecor);
|
---|
| 134 | if (rc != EOK)
|
---|
| 135 | goto error;
|
---|
| 136 |
|
---|
[d284ce9] | 137 | ui_wdecor_set_rect(wdecor, ¶ms->rect);
|
---|
| 138 | ui_wdecor_set_cb(wdecor, &wdecor_cb, (void *) window);
|
---|
| 139 | ui_wdecor_paint(wdecor);
|
---|
| 140 |
|
---|
[f7a90df] | 141 | window->ui = ui;
|
---|
| 142 | window->dwindow = dwindow;
|
---|
| 143 | window->gc = gc;
|
---|
| 144 | window->res = res;
|
---|
| 145 | window->wdecor = wdecor;
|
---|
| 146 | *rwindow = window;
|
---|
| 147 | return EOK;
|
---|
| 148 | error:
|
---|
| 149 | if (wdecor != NULL)
|
---|
| 150 | ui_wdecor_destroy(wdecor);
|
---|
| 151 | if (res != NULL)
|
---|
| 152 | ui_resource_destroy(res);
|
---|
| 153 | if (dgc != NULL)
|
---|
| 154 | dummygc_destroy(dgc);
|
---|
| 155 | if (dwindow != NULL)
|
---|
| 156 | display_window_destroy(dwindow);
|
---|
| 157 | free(window);
|
---|
| 158 | return rc;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | /** Destroy window.
|
---|
| 162 | *
|
---|
| 163 | * @param window Window or @c NULL
|
---|
| 164 | */
|
---|
| 165 | void ui_window_destroy(ui_window_t *window)
|
---|
| 166 | {
|
---|
| 167 | if (window == NULL)
|
---|
| 168 | return;
|
---|
| 169 |
|
---|
| 170 | ui_wdecor_destroy(window->wdecor);
|
---|
| 171 | ui_resource_destroy(window->res);
|
---|
| 172 | gfx_context_delete(window->gc);
|
---|
| 173 | display_window_destroy(window->dwindow);
|
---|
| 174 | free(window);
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[d284ce9] | 177 | /** Set window callbacks.
|
---|
| 178 | *
|
---|
| 179 | * @param window Window
|
---|
| 180 | * @param cb Window decoration callbacks
|
---|
| 181 | * @param arg Callback argument
|
---|
| 182 | */
|
---|
| 183 | void ui_window_set_cb(ui_window_t *window, ui_window_cb_t *cb, void *arg)
|
---|
| 184 | {
|
---|
| 185 | window->cb = cb;
|
---|
| 186 | window->arg = arg;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | ui_resource_t *ui_window_get_res(ui_window_t *window)
|
---|
| 190 | {
|
---|
| 191 | return window->res;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | gfx_context_t *ui_window_get_gc(ui_window_t *window)
|
---|
| 195 | {
|
---|
| 196 | return window->gc;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | void ui_window_get_app_rect(ui_window_t *window, gfx_rect_t *rect)
|
---|
| 200 | {
|
---|
| 201 | ui_wdecor_geom_t geom;
|
---|
| 202 |
|
---|
| 203 | ui_wdecor_get_geom(window->wdecor, &geom);
|
---|
| 204 | *rect = geom.app_area_rect;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | /** Handle window close event. */
|
---|
| 208 | static void dwnd_close_event(void *arg)
|
---|
| 209 | {
|
---|
| 210 | ui_window_t *window = (ui_window_t *) arg;
|
---|
| 211 |
|
---|
| 212 | ui_window_close(window);
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | /** Handle window focus event. */
|
---|
| 216 | static void dwnd_focus_event(void *arg)
|
---|
| 217 | {
|
---|
| 218 | ui_window_t *window = (ui_window_t *) arg;
|
---|
| 219 |
|
---|
| 220 | if (window->wdecor != NULL) {
|
---|
| 221 | ui_wdecor_set_active(window->wdecor, true);
|
---|
| 222 | ui_wdecor_paint(window->wdecor);
|
---|
| 223 | }
|
---|
[f03d1308] | 224 |
|
---|
| 225 | ui_window_focus(window);
|
---|
[d284ce9] | 226 | }
|
---|
| 227 |
|
---|
| 228 | /** Handle window keyboard event */
|
---|
| 229 | static void dwnd_kbd_event(void *arg, kbd_event_t *kbd_event)
|
---|
| 230 | {
|
---|
| 231 | ui_window_t *window = (ui_window_t *) arg;
|
---|
| 232 |
|
---|
| 233 | (void) window;
|
---|
[f03d1308] | 234 | ui_window_kbd(window, kbd_event);
|
---|
[d284ce9] | 235 | }
|
---|
| 236 |
|
---|
| 237 | /** Handle window position event */
|
---|
| 238 | static void dwnd_pos_event(void *arg, pos_event_t *event)
|
---|
| 239 | {
|
---|
| 240 | ui_window_t *window = (ui_window_t *) arg;
|
---|
| 241 |
|
---|
| 242 | /* Make sure we don't process events until fully initialized */
|
---|
| 243 | if (window->wdecor == NULL)
|
---|
| 244 | return;
|
---|
| 245 |
|
---|
| 246 | ui_wdecor_pos_event(window->wdecor, event);
|
---|
| 247 | ui_window_pos(window, event);
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | /** Handle window unfocus event. */
|
---|
| 251 | static void dwnd_unfocus_event(void *arg)
|
---|
| 252 | {
|
---|
| 253 | ui_window_t *window = (ui_window_t *) arg;
|
---|
| 254 |
|
---|
| 255 | if (window->wdecor != NULL) {
|
---|
| 256 | ui_wdecor_set_active(window->wdecor, false);
|
---|
| 257 | ui_wdecor_paint(window->wdecor);
|
---|
| 258 | }
|
---|
[f03d1308] | 259 |
|
---|
| 260 | ui_window_unfocus(window);
|
---|
[d284ce9] | 261 | }
|
---|
| 262 |
|
---|
| 263 | /** Window decoration requested window closure.
|
---|
| 264 | *
|
---|
| 265 | * @param wdecor Window decoration
|
---|
| 266 | * @param arg Argument (demo)
|
---|
| 267 | */
|
---|
| 268 | static void wd_close(ui_wdecor_t *wdecor, void *arg)
|
---|
| 269 | {
|
---|
| 270 | ui_window_t *window = (ui_window_t *) arg;
|
---|
| 271 |
|
---|
| 272 | ui_window_close(window);
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | /** Window decoration requested window move.
|
---|
| 276 | *
|
---|
| 277 | * @param wdecor Window decoration
|
---|
| 278 | * @param arg Argument (demo)
|
---|
| 279 | * @param pos Position where the title bar was pressed
|
---|
| 280 | */
|
---|
| 281 | static void wd_move(ui_wdecor_t *wdecor, void *arg, gfx_coord2_t *pos)
|
---|
| 282 | {
|
---|
| 283 | ui_window_t *window = (ui_window_t *) arg;
|
---|
| 284 |
|
---|
| 285 | (void) display_window_move_req(window->dwindow, pos);
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | /** Send window close event.
|
---|
| 289 | *
|
---|
| 290 | * @param window Window
|
---|
| 291 | */
|
---|
| 292 | void ui_window_close(ui_window_t *window)
|
---|
| 293 | {
|
---|
| 294 | if (window->cb != NULL && window->cb->close != NULL)
|
---|
| 295 | window->cb->close(window, window->arg);
|
---|
| 296 | }
|
---|
| 297 |
|
---|
[f03d1308] | 298 | /** Send window focus event.
|
---|
| 299 | *
|
---|
| 300 | * @param window Window
|
---|
| 301 | */
|
---|
| 302 | void ui_window_focus(ui_window_t *window)
|
---|
| 303 | {
|
---|
| 304 | if (window->cb != NULL && window->cb->focus != NULL)
|
---|
| 305 | window->cb->focus(window, window->arg);
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | /** Send window keyboard event.
|
---|
| 309 | *
|
---|
| 310 | * @param window Window
|
---|
| 311 | */
|
---|
| 312 | void ui_window_kbd(ui_window_t *window, kbd_event_t *kbd)
|
---|
| 313 | {
|
---|
| 314 | if (window->cb != NULL && window->cb->kbd != NULL)
|
---|
| 315 | window->cb->kbd(window, window->arg, kbd);
|
---|
| 316 | }
|
---|
| 317 |
|
---|
[d284ce9] | 318 | /** Send window position event.
|
---|
| 319 | *
|
---|
| 320 | * @param window Window
|
---|
| 321 | */
|
---|
| 322 | void ui_window_pos(ui_window_t *window, pos_event_t *pos)
|
---|
| 323 | {
|
---|
| 324 | if (window->cb != NULL && window->cb->pos != NULL)
|
---|
| 325 | window->cb->pos(window, window->arg, pos);
|
---|
| 326 | }
|
---|
| 327 |
|
---|
[f03d1308] | 328 | /** Send window unfocus event.
|
---|
| 329 | *
|
---|
| 330 | * @param window Window
|
---|
| 331 | */
|
---|
| 332 | void ui_window_unfocus(ui_window_t *window)
|
---|
| 333 | {
|
---|
| 334 | if (window->cb != NULL && window->cb->unfocus != NULL)
|
---|
| 335 | window->cb->unfocus(window, window->arg);
|
---|
| 336 | }
|
---|
| 337 |
|
---|
[f7a90df] | 338 | /** @}
|
---|
| 339 | */
|
---|