source: mainline/uspace/lib/ui/src/window.c@ 0576df9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0576df9 was 0576df9, checked in by Jiri Svoboda <jiri@…>, 5 years ago

Decoding images without libdraw

  • Property mode set to 100644
File size: 12.9 KB
Line 
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/bitmap.h>
39#include <gfx/context.h>
40#include <gfx/render.h>
41#include <io/kbd_event.h>
42#include <io/pos_event.h>
43#include <mem.h>
44#include <memgfx/memgc.h>
45#include <stdlib.h>
46#include <ui/control.h>
47#include <ui/resource.h>
48#include <ui/wdecor.h>
49#include <ui/window.h>
50#include "../private/control.h"
51#include "../private/dummygc.h"
52#include "../private/resource.h"
53#include "../private/ui.h"
54#include "../private/wdecor.h"
55#include "../private/window.h"
56
57static void dwnd_close_event(void *);
58static void dwnd_focus_event(void *);
59static void dwnd_kbd_event(void *, kbd_event_t *);
60static void dwnd_pos_event(void *, pos_event_t *);
61static void dwnd_unfocus_event(void *);
62
63static display_wnd_cb_t dwnd_cb = {
64 .close_event = dwnd_close_event,
65 .focus_event = dwnd_focus_event,
66 .kbd_event = dwnd_kbd_event,
67 .pos_event = dwnd_pos_event,
68 .unfocus_event = dwnd_unfocus_event
69};
70
71static void wd_close(ui_wdecor_t *, void *);
72static void wd_move(ui_wdecor_t *, void *, gfx_coord2_t *);
73
74static ui_wdecor_cb_t wdecor_cb = {
75 .close = wd_close,
76 .move = wd_move
77};
78
79static void ui_window_app_update(void *, gfx_rect_t *);
80
81/** Initialize window parameters structure.
82 *
83 * Window parameters structure must always be initialized using this function
84 * first.
85 *
86 * @param params Window parameters structure
87 */
88void ui_wnd_params_init(ui_wnd_params_t *params)
89{
90 memset(params, 0, sizeof(ui_wnd_params_t));
91}
92
93/** Create new window.
94 *
95 * @param ui User interface
96 * @param params Window parameters
97 * @param rwindow Place to store pointer to new window
98 * @return EOK on success or an error code
99 */
100errno_t ui_window_create(ui_t *ui, ui_wnd_params_t *params,
101 ui_window_t **rwindow)
102{
103 ui_window_t *window;
104 display_wnd_params_t dparams;
105 display_window_t *dwindow = NULL;
106 gfx_context_t *gc = NULL;
107 ui_resource_t *res = NULL;
108 ui_wdecor_t *wdecor = NULL;
109 dummy_gc_t *dgc = NULL;
110 errno_t rc;
111
112 window = calloc(1, sizeof(ui_window_t));
113 if (window == NULL)
114 return ENOMEM;
115
116 display_wnd_params_init(&dparams);
117 dparams.rect = params->rect;
118
119 if (ui->display != NULL) {
120 rc = display_window_create(ui->display, &dparams, &dwnd_cb,
121 (void *) window, &dwindow);
122 if (rc != EOK)
123 goto error;
124
125 rc = display_window_get_gc(dwindow, &gc);
126 if (rc != EOK)
127 goto error;
128 } else {
129 /* Needed for unit tests */
130 rc = dummygc_create(&dgc);
131 if (rc != EOK)
132 goto error;
133
134 gc = dummygc_get_ctx(dgc);
135 }
136
137 rc = ui_resource_create(gc, &res);
138 if (rc != EOK)
139 goto error;
140
141 rc = ui_wdecor_create(res, params->caption, &wdecor);
142 if (rc != EOK)
143 goto error;
144
145 ui_wdecor_set_rect(wdecor, &params->rect);
146 ui_wdecor_set_cb(wdecor, &wdecor_cb, (void *) window);
147 ui_wdecor_paint(wdecor);
148
149 window->ui = ui;
150 window->dwindow = dwindow;
151 window->gc = gc;
152 window->res = res;
153 window->wdecor = wdecor;
154 *rwindow = window;
155 return EOK;
156error:
157 if (wdecor != NULL)
158 ui_wdecor_destroy(wdecor);
159 if (res != NULL)
160 ui_resource_destroy(res);
161 if (dgc != NULL)
162 dummygc_destroy(dgc);
163 if (dwindow != NULL)
164 display_window_destroy(dwindow);
165 free(window);
166 return rc;
167}
168
169/** Destroy window.
170 *
171 * @param window Window or @c NULL
172 */
173void ui_window_destroy(ui_window_t *window)
174{
175 if (window == NULL)
176 return;
177
178 ui_control_destroy(window->control);
179 ui_wdecor_destroy(window->wdecor);
180 ui_resource_destroy(window->res);
181 gfx_context_delete(window->gc);
182 display_window_destroy(window->dwindow);
183 free(window);
184}
185
186/** Add control to window.
187 *
188 * Only one control can be added to a window. If more than one control
189 * is added, the results are undefined.
190 *
191 * @param fixed Fixed layout
192 * @param control Control
193 * @return EOK on success, ENOMEM if out of memory
194 */
195void ui_window_add(ui_window_t *window, ui_control_t *control)
196{
197 assert(window->control == NULL);
198
199 window->control = control;
200 control->elemp = (void *) window;
201}
202
203/** Remove control from window.
204 *
205 * @param window Window
206 * @param control Control
207 */
208void ui_window_remove(ui_window_t *window, ui_control_t *control)
209{
210 assert(window->control == control);
211 assert((ui_window_t *) control->elemp == window);
212
213 window->control = NULL;
214 control->elemp = NULL;
215}
216
217/** Resize/move window.
218 *
219 * Resize window to the dimensions of @a rect. If @a rect.p0 is not 0,0,
220 * the top-left corner of the window will move on the screen accordingly.
221 *
222 * @param window Window
223 * @param rect Rectangle
224 *
225 * @return EOK on success or an error code
226 */
227errno_t ui_window_resize(ui_window_t *window, gfx_rect_t *rect)
228{
229 gfx_coord2_t offs;
230 gfx_rect_t nrect;
231 errno_t rc;
232
233 /*
234 * Move rect so that p0=0,0 - keep window's coordinate system origin
235 * locked to top-left corner of the window.
236 */
237 offs = rect->p0;
238 gfx_rect_rtranslate(&offs, rect, &nrect);
239
240 /* dwindow can be NULL in case of unit tests */
241 if (window->dwindow != NULL) {
242 rc = display_window_resize(window->dwindow, &offs, &nrect);
243 if (rc != EOK)
244 return rc;
245 }
246
247 ui_wdecor_set_rect(window->wdecor, &nrect);
248 ui_wdecor_paint(window->wdecor);
249 return EOK;
250}
251
252/** Set window callbacks.
253 *
254 * @param window Window
255 * @param cb Window decoration callbacks
256 * @param arg Callback argument
257 */
258void ui_window_set_cb(ui_window_t *window, ui_window_cb_t *cb, void *arg)
259{
260 window->cb = cb;
261 window->arg = arg;
262}
263
264/** Get UI resource from window.
265 *
266 * @param window Window
267 * @return UI resource
268 */
269ui_resource_t *ui_window_get_res(ui_window_t *window)
270{
271 return window->res;
272}
273
274/** Get window GC.
275 *
276 * @param window Window
277 * @return GC (relative to window)
278 */
279gfx_context_t *ui_window_get_gc(ui_window_t *window)
280{
281 return window->gc;
282}
283
284/** Get window application area GC
285 *
286 * @param window Window
287 * @param rgc Place to store GC (relative to application area)
288 * @return EOK on success or an error code
289 */
290errno_t ui_window_get_app_gc(ui_window_t *window, gfx_context_t **rgc)
291{
292 gfx_bitmap_params_t params;
293 gfx_bitmap_alloc_t alloc;
294 gfx_rect_t rect;
295 mem_gc_t *memgc;
296 errno_t rc;
297
298 if (window->app_gc == NULL) {
299 assert(window->app_bmp == NULL);
300
301 gfx_bitmap_params_init(&params);
302
303 /*
304 * The bitmap will have the same dimensions as the
305 * application rectangle, but start at 0,0.
306 */
307 ui_window_get_app_rect(window, &rect);
308 gfx_rect_rtranslate(&rect.p0, &rect, &params.rect);
309
310 rc = gfx_bitmap_create(window->gc, &params, NULL,
311 &window->app_bmp);
312 if (rc != EOK)
313 return rc;
314
315 rc = gfx_bitmap_get_alloc(window->app_bmp, &alloc);
316 if (rc != EOK) {
317 gfx_bitmap_destroy(window->app_bmp);
318 return rc;
319 }
320
321 rc = mem_gc_create(&params.rect, &alloc, ui_window_app_update,
322 (void *) window, &memgc);
323 if (rc != EOK) {
324 gfx_bitmap_destroy(window->app_bmp);
325 return rc;
326 }
327
328 window->app_gc = mem_gc_get_ctx(memgc);
329 }
330
331 *rgc = window->app_gc;
332 return EOK;
333}
334
335/** Get window application rectangle
336 *
337 * @param window Window
338 * @param rect Place to store application rectangle
339 */
340void ui_window_get_app_rect(ui_window_t *window, gfx_rect_t *rect)
341{
342 ui_wdecor_geom_t geom;
343
344 ui_wdecor_get_geom(window->wdecor, &geom);
345 *rect = geom.app_area_rect;
346}
347
348/** Paint window
349 *
350 * @param window Window
351 * @return EOK on success or an error code
352 */
353errno_t ui_window_paint(ui_window_t *window)
354{
355 return ui_window_send_paint(window);
356}
357
358/** Handle window close event. */
359static void dwnd_close_event(void *arg)
360{
361 ui_window_t *window = (ui_window_t *) arg;
362
363 ui_window_send_close(window);
364}
365
366/** Handle window focus event. */
367static void dwnd_focus_event(void *arg)
368{
369 ui_window_t *window = (ui_window_t *) arg;
370
371 if (window->wdecor != NULL) {
372 ui_wdecor_set_active(window->wdecor, true);
373 ui_wdecor_paint(window->wdecor);
374 }
375
376 ui_window_send_focus(window);
377}
378
379/** Handle window keyboard event */
380static void dwnd_kbd_event(void *arg, kbd_event_t *kbd_event)
381{
382 ui_window_t *window = (ui_window_t *) arg;
383
384 (void) window;
385 ui_window_send_kbd(window, kbd_event);
386}
387
388/** Handle window position event */
389static void dwnd_pos_event(void *arg, pos_event_t *event)
390{
391 ui_window_t *window = (ui_window_t *) arg;
392
393 /* Make sure we don't process events until fully initialized */
394 if (window->wdecor == NULL)
395 return;
396
397 ui_wdecor_pos_event(window->wdecor, event);
398 ui_window_send_pos(window, event);
399}
400
401/** Handle window unfocus event. */
402static void dwnd_unfocus_event(void *arg)
403{
404 ui_window_t *window = (ui_window_t *) arg;
405
406 if (window->wdecor != NULL) {
407 ui_wdecor_set_active(window->wdecor, false);
408 ui_wdecor_paint(window->wdecor);
409 }
410
411 ui_window_send_unfocus(window);
412}
413
414/** Window decoration requested window closure.
415 *
416 * @param wdecor Window decoration
417 * @param arg Argument (demo)
418 */
419static void wd_close(ui_wdecor_t *wdecor, void *arg)
420{
421 ui_window_t *window = (ui_window_t *) arg;
422
423 ui_window_send_close(window);
424}
425
426/** Window decoration requested window move.
427 *
428 * @param wdecor Window decoration
429 * @param arg Argument (demo)
430 * @param pos Position where the title bar was pressed
431 */
432static void wd_move(ui_wdecor_t *wdecor, void *arg, gfx_coord2_t *pos)
433{
434 ui_window_t *window = (ui_window_t *) arg;
435
436 (void) display_window_move_req(window->dwindow, pos);
437}
438
439/** Send window close event.
440 *
441 * @param window Window
442 */
443void ui_window_send_close(ui_window_t *window)
444{
445 if (window->cb != NULL && window->cb->close != NULL)
446 window->cb->close(window, window->arg);
447}
448
449/** Send window focus event.
450 *
451 * @param window Window
452 */
453void ui_window_send_focus(ui_window_t *window)
454{
455 if (window->cb != NULL && window->cb->focus != NULL)
456 window->cb->focus(window, window->arg);
457}
458
459/** Send window keyboard event.
460 *
461 * @param window Window
462 */
463void ui_window_send_kbd(ui_window_t *window, kbd_event_t *kbd)
464{
465 if (window->cb != NULL && window->cb->kbd != NULL)
466 window->cb->kbd(window, window->arg, kbd);
467}
468
469/** Send window paint event.
470 *
471 * @param window Window
472 */
473errno_t ui_window_send_paint(ui_window_t *window)
474{
475 if (window->cb != NULL && window->cb->paint != NULL)
476 return window->cb->paint(window, window->arg);
477 else
478 return ui_window_def_paint(window);
479}
480
481/** Send window position event.
482 *
483 * @param window Window
484 */
485void ui_window_send_pos(ui_window_t *window, pos_event_t *pos)
486{
487 if (window->cb != NULL && window->cb->pos != NULL)
488 window->cb->pos(window, window->arg, pos);
489 else
490 ui_window_def_pos(window, pos);
491}
492
493/** Send window unfocus event.
494 *
495 * @param window Window
496 */
497void ui_window_send_unfocus(ui_window_t *window)
498{
499 if (window->cb != NULL && window->cb->unfocus != NULL)
500 window->cb->unfocus(window, window->arg);
501}
502
503/** Default window paint routine.
504 *
505 * @param window Window
506 * @return EOK on success or an error code
507 */
508errno_t ui_window_def_paint(ui_window_t *window)
509{
510 gfx_rect_t app_rect;
511 errno_t rc;
512
513 rc = gfx_set_color(window->gc, window->res->wnd_face_color);
514 if (rc != EOK)
515 return rc;
516
517 ui_window_get_app_rect(window, &app_rect);
518
519 rc = gfx_fill_rect(window->gc, &app_rect);
520 if (rc != EOK)
521 return rc;
522
523 if (window->control != NULL)
524 return ui_control_paint(window->control);
525
526 return EOK;
527}
528
529/** Default window position event routine.
530 *
531 * @param window Window
532 * @return EOK on success or an error code
533 */
534void ui_window_def_pos(ui_window_t *window, pos_event_t *pos)
535{
536 if (window->control != NULL)
537 ui_control_pos_event(window->control, pos);
538}
539
540/** Application area update callback
541 *
542 * @param arg Argument (ui_window_t *)
543 * @param rect Rectangle to update
544 */
545static void ui_window_app_update(void *arg, gfx_rect_t *rect)
546{
547 ui_window_t *window = (ui_window_t *) arg;
548 gfx_rect_t arect;
549
550 ui_window_get_app_rect(window, &arect);
551
552 /* Render bitmap rectangle inside the application area */
553 (void) gfx_bitmap_render(window->app_bmp, rect, &arect.p0);
554}
555
556/** @}
557 */
Note: See TracBrowser for help on using the repository browser.