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

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

Do not forget to clean up when exiting

  • Property mode set to 100644
File size: 21.5 KB
Line 
1/*
2 * Copyright (c) 2021 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 <congfx/console.h>
37#include <display.h>
38#include <errno.h>
39#include <gfx/bitmap.h>
40#include <gfx/context.h>
41#include <gfx/render.h>
42#include <io/kbd_event.h>
43#include <io/pos_event.h>
44#include <mem.h>
45#include <memgfx/memgc.h>
46#include <stdlib.h>
47#include <ui/control.h>
48#include <ui/resource.h>
49#include <ui/ui.h>
50#include <ui/wdecor.h>
51#include <ui/window.h>
52#include "../private/control.h"
53#include "../private/dummygc.h"
54#include "../private/resource.h"
55#include "../private/ui.h"
56#include "../private/wdecor.h"
57#include "../private/window.h"
58
59static void dwnd_close_event(void *);
60static void dwnd_focus_event(void *);
61static void dwnd_kbd_event(void *, kbd_event_t *);
62static void dwnd_pos_event(void *, pos_event_t *);
63static void dwnd_resize_event(void *, gfx_rect_t *);
64static void dwnd_unfocus_event(void *);
65
66static display_wnd_cb_t dwnd_cb = {
67 .close_event = dwnd_close_event,
68 .focus_event = dwnd_focus_event,
69 .kbd_event = dwnd_kbd_event,
70 .pos_event = dwnd_pos_event,
71 .resize_event = dwnd_resize_event,
72 .unfocus_event = dwnd_unfocus_event
73};
74
75static void wd_close(ui_wdecor_t *, void *);
76static void wd_move(ui_wdecor_t *, void *, gfx_coord2_t *);
77static void wd_resize(ui_wdecor_t *, void *, ui_wdecor_rsztype_t,
78 gfx_coord2_t *);
79static void wd_set_cursor(ui_wdecor_t *, void *, ui_stock_cursor_t);
80
81static ui_wdecor_cb_t wdecor_cb = {
82 .close = wd_close,
83 .move = wd_move,
84 .resize = wd_resize,
85 .set_cursor = wd_set_cursor
86};
87
88static void ui_window_invalidate(void *, gfx_rect_t *);
89static void ui_window_update(void *);
90static void ui_window_app_invalidate(void *, gfx_rect_t *);
91static void ui_window_app_update(void *);
92
93/** Initialize window parameters structure.
94 *
95 * Window parameters structure must always be initialized using this function
96 * first. By default, the window will be decorated. To get a non-decorated
97 * window, one needs to clear ui_wds_decorated
98 * (e.g. params->style &= ~ui_wds_decorated).
99 *
100 * @param params Window parameters structure
101 */
102void ui_wnd_params_init(ui_wnd_params_t *params)
103{
104 memset(params, 0, sizeof(ui_wnd_params_t));
105
106 /* Make window decorated by default. */
107 params->style = ui_wds_decorated;
108}
109
110/** Create new window.
111 *
112 * @param ui User interface
113 * @param params Window parameters
114 * @param rwindow Place to store pointer to new window
115 * @return EOK on success or an error code
116 */
117errno_t ui_window_create(ui_t *ui, ui_wnd_params_t *params,
118 ui_window_t **rwindow)
119{
120 ui_window_t *window;
121 display_info_t info;
122 gfx_coord2_t pos;
123 gfx_coord2_t scr_dims;
124 display_wnd_params_t dparams;
125 display_window_t *dwindow = NULL;
126 gfx_context_t *gc = NULL;
127 ui_resource_t *res = NULL;
128 ui_wdecor_t *wdecor = NULL;
129 dummy_gc_t *dgc = NULL;
130 gfx_bitmap_params_t bparams;
131 gfx_bitmap_alloc_t alloc;
132 gfx_bitmap_t *bmp = NULL;
133 mem_gc_t *memgc = NULL;
134 console_gc_t *cgc = NULL;
135 errno_t rc;
136
137 if (ui->root_wnd != NULL)
138 return EEXIST;
139
140 window = calloc(1, sizeof(ui_window_t));
141 if (window == NULL)
142 return ENOMEM;
143
144 display_wnd_params_init(&dparams);
145 dparams.rect = params->rect;
146 /* Only allow making the window larger */
147 gfx_rect_dims(&params->rect, &dparams.min_size);
148
149 if (ui->display != NULL) {
150 if (params->placement != ui_wnd_place_default) {
151 rc = display_get_info(ui->display, &info);
152 if (rc != EOK)
153 goto error;
154 }
155
156 if (params->placement == ui_wnd_place_full_screen) {
157 /* Make window the size of the screen */
158 gfx_rect_dims(&info.rect, &scr_dims);
159 gfx_coord2_add(&dparams.rect.p0, &scr_dims,
160 &dparams.rect.p1);
161 }
162
163 rc = display_window_create(ui->display, &dparams, &dwnd_cb,
164 (void *) window, &dwindow);
165 if (rc != EOK)
166 goto error;
167
168 if (params->placement != ui_wnd_place_default) {
169 pos.x = 0;
170 pos.y = 0;
171
172 switch (params->placement) {
173 case ui_wnd_place_default:
174 assert(false);
175 case ui_wnd_place_top_left:
176 case ui_wnd_place_full_screen:
177 pos.x = info.rect.p0.x - params->rect.p0.x;
178 pos.y = info.rect.p0.y - params->rect.p0.y;
179 break;
180 case ui_wnd_place_top_right:
181 pos.x = info.rect.p1.x - params->rect.p1.x;
182 pos.y = info.rect.p0.y - params->rect.p0.y;
183 break;
184 case ui_wnd_place_bottom_left:
185 pos.x = info.rect.p0.x - params->rect.p0.x;
186 pos.y = info.rect.p1.y - params->rect.p1.y;
187 break;
188 case ui_wnd_place_bottom_right:
189 pos.x = info.rect.p1.x - params->rect.p1.x;
190 pos.y = info.rect.p1.y - params->rect.p1.y;
191 break;
192 }
193
194 rc = display_window_move(dwindow, &pos);
195 if (rc != EOK)
196 goto error;
197 }
198
199 rc = display_window_get_gc(dwindow, &gc);
200 if (rc != EOK)
201 goto error;
202 } else if (ui->console != NULL) {
203 rc = console_gc_create(ui->console, NULL, &cgc);
204 if (rc != EOK)
205 goto error;
206
207 gc = console_gc_get_ctx(cgc);
208 } else {
209 /* Needed for unit tests */
210 rc = dummygc_create(&dgc);
211 if (rc != EOK)
212 goto error;
213
214 gc = dummygc_get_ctx(dgc);
215 }
216
217#ifdef CONFIG_UI_CS_RENDER
218 /* Create window bitmap */
219 gfx_bitmap_params_init(&bparams);
220#ifndef CONFIG_WIN_DOUBLE_BUF
221 /* Console does not support direct output */
222 if (ui->display != NULL)
223 bparams.flags |= bmpf_direct_output;
224#endif
225
226 /* Move rectangle so that top-left corner is 0,0 */
227 gfx_rect_rtranslate(&params->rect.p0, &params->rect, &bparams.rect);
228
229 rc = gfx_bitmap_create(gc, &bparams, NULL, &bmp);
230 if (rc != EOK)
231 goto error;
232
233 /* Create memory GC */
234 rc = gfx_bitmap_get_alloc(bmp, &alloc);
235 if (rc != EOK) {
236 gfx_bitmap_destroy(window->app_bmp);
237 return rc;
238 }
239
240 rc = mem_gc_create(&bparams.rect, &alloc, ui_window_invalidate,
241 ui_window_update, (void *) window, &memgc);
242 if (rc != EOK) {
243 gfx_bitmap_destroy(window->app_bmp);
244 return rc;
245 }
246
247 window->bmp = bmp;
248 window->mgc = memgc;
249 window->gc = mem_gc_get_ctx(memgc);
250 window->realgc = gc;
251#else
252 (void) ui_window_update;
253 (void) ui_window_invalidate;
254 (void) alloc;
255 (void) bparams;
256 window->gc = gc;
257#endif
258 window->cgc = cgc;
259
260 rc = ui_resource_create(window->gc, ui_is_textmode(ui), &res);
261 if (rc != EOK)
262 goto error;
263
264 rc = ui_wdecor_create(res, params->caption, params->style, &wdecor);
265 if (rc != EOK)
266 goto error;
267
268 ui_wdecor_set_rect(wdecor, &dparams.rect);
269 ui_wdecor_set_cb(wdecor, &wdecor_cb, (void *) window);
270 ui_wdecor_paint(wdecor);
271
272 window->ui = ui;
273 window->dwindow = dwindow;
274 window->rect = dparams.rect;
275
276 window->res = res;
277 window->wdecor = wdecor;
278 window->cursor = ui_curs_arrow;
279 *rwindow = window;
280
281 ui->root_wnd = window;
282 return EOK;
283error:
284 if (wdecor != NULL)
285 ui_wdecor_destroy(wdecor);
286 if (res != NULL)
287 ui_resource_destroy(res);
288 if (memgc != NULL)
289 mem_gc_delete(memgc);
290 if (bmp != NULL)
291 gfx_bitmap_destroy(bmp);
292 if (dgc != NULL)
293 dummygc_destroy(dgc);
294 if (cgc != NULL)
295 console_gc_delete(cgc);
296 if (dwindow != NULL)
297 display_window_destroy(dwindow);
298 free(window);
299 return rc;
300}
301
302/** Destroy window.
303 *
304 * @param window Window or @c NULL
305 */
306void ui_window_destroy(ui_window_t *window)
307{
308 if (window == NULL)
309 return;
310
311 ui_control_destroy(window->control);
312 ui_wdecor_destroy(window->wdecor);
313 ui_resource_destroy(window->res);
314 if (0 && window->app_mgc != NULL)
315 mem_gc_delete(window->app_mgc);
316 if (0 && window->app_bmp != NULL)
317 gfx_bitmap_destroy(window->app_bmp);
318 if (window->mgc != NULL) {
319 mem_gc_delete(window->mgc);
320 window->gc = NULL;
321 }
322 if (window->bmp != NULL)
323 gfx_bitmap_destroy(window->bmp);
324 gfx_context_delete(window->gc);
325 if (window->dwindow != NULL)
326 display_window_destroy(window->dwindow);
327 if (window->cgc != NULL)
328 console_gc_delete(window->cgc);
329 free(window);
330}
331
332/** Add control to window.
333 *
334 * Only one control can be added to a window. If more than one control
335 * is added, the results are undefined.
336 *
337 * @param fixed Fixed layout
338 * @param control Control
339 * @return EOK on success, ENOMEM if out of memory
340 */
341void ui_window_add(ui_window_t *window, ui_control_t *control)
342{
343 assert(window->control == NULL);
344
345 window->control = control;
346 control->elemp = (void *) window;
347}
348
349/** Remove control from window.
350 *
351 * @param window Window
352 * @param control Control
353 */
354void ui_window_remove(ui_window_t *window, ui_control_t *control)
355{
356 assert(window->control == control);
357 assert((ui_window_t *) control->elemp == window);
358
359 window->control = NULL;
360 control->elemp = NULL;
361}
362
363/** Resize/move window.
364 *
365 * Resize window to the dimensions of @a rect. If @a rect.p0 is not 0,0,
366 * the top-left corner of the window will move on the screen accordingly.
367 *
368 * @param window Window
369 * @param rect Rectangle
370 *
371 * @return EOK on success or an error code
372 */
373errno_t ui_window_resize(ui_window_t *window, gfx_rect_t *rect)
374{
375 gfx_coord2_t offs;
376 gfx_rect_t nrect;
377 gfx_rect_t arect;
378 gfx_bitmap_t *app_bmp = NULL;
379 gfx_bitmap_t *win_bmp = NULL;
380 gfx_bitmap_params_t app_params;
381 gfx_bitmap_params_t win_params;
382 gfx_bitmap_alloc_t app_alloc;
383 gfx_bitmap_alloc_t win_alloc;
384 errno_t rc;
385
386 /*
387 * Move rect so that p0=0,0 - keep window's coordinate system origin
388 * locked to top-left corner of the window.
389 */
390 offs = rect->p0;
391 gfx_rect_rtranslate(&offs, rect, &nrect);
392
393 /* mgc != NULL iff client-side rendering */
394 if (window->mgc != NULL) {
395 /* Resize window bitmap */
396 assert(window->bmp != NULL);
397
398 gfx_bitmap_params_init(&win_params);
399#ifndef CONFIG_WIN_DOUBLE_BUF
400 win_params.flags |= bmpf_direct_output;
401#endif
402 win_params.rect = nrect;
403
404 rc = gfx_bitmap_create(window->realgc, &win_params, NULL,
405 &win_bmp);
406 if (rc != EOK)
407 goto error;
408
409 rc = gfx_bitmap_get_alloc(win_bmp, &win_alloc);
410 if (rc != EOK)
411 goto error;
412 }
413
414 /* Application area GC? */
415 if (window->app_gc != NULL) {
416 /* Resize application bitmap */
417 assert(window->app_bmp != NULL);
418
419 gfx_bitmap_params_init(&app_params);
420
421 /*
422 * The bitmap will have the same dimensions as the
423 * application rectangle, but start at 0,0.
424 */
425 ui_wdecor_app_from_rect(window->wdecor->style, &nrect, &arect);
426 gfx_rect_rtranslate(&arect.p0, &arect, &app_params.rect);
427
428 rc = gfx_bitmap_create(window->gc, &app_params, NULL,
429 &app_bmp);
430 if (rc != EOK)
431 goto error;
432
433 rc = gfx_bitmap_get_alloc(app_bmp, &app_alloc);
434 if (rc != EOK)
435 goto error;
436 }
437
438 /* dwindow can be NULL in case of unit tests */
439 if (window->dwindow != NULL) {
440 rc = display_window_resize(window->dwindow, &offs, &nrect);
441 if (rc != EOK)
442 goto error;
443 }
444
445 /* CLient side rendering? */
446 if (window->mgc != NULL) {
447 mem_gc_retarget(window->mgc, &win_params.rect, &win_alloc);
448
449 gfx_bitmap_destroy(window->bmp);
450 window->bmp = win_bmp;
451 }
452
453 ui_wdecor_set_rect(window->wdecor, &nrect);
454 ui_wdecor_paint(window->wdecor);
455 gfx_update(window->gc);
456
457 /* Application area GC? */
458 if (window->app_gc != NULL) {
459 mem_gc_retarget(window->app_mgc, &app_params.rect, &app_alloc);
460
461 gfx_bitmap_destroy(window->app_bmp);
462 window->app_bmp = app_bmp;
463 }
464
465 return EOK;
466error:
467 if (app_bmp != NULL)
468 gfx_bitmap_destroy(app_bmp);
469 if (win_bmp != NULL)
470 gfx_bitmap_destroy(win_bmp);
471 return rc;
472}
473
474/** Set window callbacks.
475 *
476 * @param window Window
477 * @param cb Window decoration callbacks
478 * @param arg Callback argument
479 */
480void ui_window_set_cb(ui_window_t *window, ui_window_cb_t *cb, void *arg)
481{
482 window->cb = cb;
483 window->arg = arg;
484}
485
486/** Get UI resource from window.
487 *
488 * @param window Window
489 * @return UI resource
490 */
491ui_resource_t *ui_window_get_res(ui_window_t *window)
492{
493 return window->res;
494}
495
496/** Get window GC.
497 *
498 * @param window Window
499 * @return GC (relative to window)
500 */
501gfx_context_t *ui_window_get_gc(ui_window_t *window)
502{
503 return window->gc;
504}
505
506/** Get window application area GC
507 *
508 * @param window Window
509 * @param rgc Place to store GC (relative to application area)
510 * @return EOK on success or an error code
511 */
512errno_t ui_window_get_app_gc(ui_window_t *window, gfx_context_t **rgc)
513{
514 gfx_bitmap_params_t params;
515 gfx_bitmap_alloc_t alloc;
516 gfx_rect_t rect;
517 mem_gc_t *memgc;
518 errno_t rc;
519
520 if (window->app_gc == NULL) {
521 assert(window->app_bmp == NULL);
522
523 gfx_bitmap_params_init(&params);
524
525 /*
526 * The bitmap will have the same dimensions as the
527 * application rectangle, but start at 0,0.
528 */
529 ui_window_get_app_rect(window, &rect);
530 gfx_rect_rtranslate(&rect.p0, &rect, &params.rect);
531
532 rc = gfx_bitmap_create(window->gc, &params, NULL,
533 &window->app_bmp);
534 if (rc != EOK)
535 return rc;
536
537 rc = gfx_bitmap_get_alloc(window->app_bmp, &alloc);
538 if (rc != EOK) {
539 gfx_bitmap_destroy(window->app_bmp);
540 return rc;
541 }
542
543 rc = mem_gc_create(&params.rect, &alloc, ui_window_app_invalidate,
544 ui_window_app_update, (void *) window, &memgc);
545 if (rc != EOK) {
546 gfx_bitmap_destroy(window->app_bmp);
547 return rc;
548 }
549
550 window->app_mgc = memgc;
551 window->app_gc = mem_gc_get_ctx(memgc);
552 }
553
554 *rgc = window->app_gc;
555 return EOK;
556}
557
558/** Get window application rectangle
559 *
560 * @param window Window
561 * @param rect Place to store application rectangle
562 */
563void ui_window_get_app_rect(ui_window_t *window, gfx_rect_t *rect)
564{
565 ui_wdecor_geom_t geom;
566
567 ui_wdecor_get_geom(window->wdecor, &geom);
568 *rect = geom.app_area_rect;
569}
570
571/** Paint window
572 *
573 * @param window Window
574 * @return EOK on success or an error code
575 */
576errno_t ui_window_paint(ui_window_t *window)
577{
578 return ui_window_send_paint(window);
579}
580
581/** Handle window close event. */
582static void dwnd_close_event(void *arg)
583{
584 ui_window_t *window = (ui_window_t *) arg;
585
586 ui_window_send_close(window);
587}
588
589/** Handle window focus event. */
590static void dwnd_focus_event(void *arg)
591{
592 ui_window_t *window = (ui_window_t *) arg;
593
594 if (window->wdecor != NULL) {
595 ui_wdecor_set_active(window->wdecor, true);
596 ui_wdecor_paint(window->wdecor);
597 }
598
599 ui_window_send_focus(window);
600}
601
602/** Handle window keyboard event */
603static void dwnd_kbd_event(void *arg, kbd_event_t *kbd_event)
604{
605 ui_window_t *window = (ui_window_t *) arg;
606
607 (void) window;
608 ui_window_send_kbd(window, kbd_event);
609}
610
611/** Handle window position event */
612static void dwnd_pos_event(void *arg, pos_event_t *event)
613{
614 ui_window_t *window = (ui_window_t *) arg;
615
616 /* Make sure we don't process events until fully initialized */
617 if (window->wdecor == NULL)
618 return;
619
620 ui_wdecor_pos_event(window->wdecor, event);
621 ui_window_send_pos(window, event);
622}
623
624/** Handle window resize event */
625static void dwnd_resize_event(void *arg, gfx_rect_t *rect)
626{
627 ui_window_t *window = (ui_window_t *) arg;
628
629 /* Make sure we don't process events until fully initialized */
630 if (window->wdecor == NULL)
631 return;
632
633 if ((window->wdecor->style & ui_wds_resizable) == 0)
634 return;
635
636 (void) ui_window_resize(window, rect);
637 (void) ui_window_paint(window);
638}
639
640/** Handle window unfocus event. */
641static void dwnd_unfocus_event(void *arg)
642{
643 ui_window_t *window = (ui_window_t *) arg;
644
645 if (window->wdecor != NULL) {
646 ui_wdecor_set_active(window->wdecor, false);
647 ui_wdecor_paint(window->wdecor);
648 }
649
650 ui_window_send_unfocus(window);
651}
652
653/** Window decoration requested window closure.
654 *
655 * @param wdecor Window decoration
656 * @param arg Argument (window)
657 */
658static void wd_close(ui_wdecor_t *wdecor, void *arg)
659{
660 ui_window_t *window = (ui_window_t *) arg;
661
662 ui_window_send_close(window);
663}
664
665/** Window decoration requested window move.
666 *
667 * @param wdecor Window decoration
668 * @param arg Argument (window)
669 * @param pos Position where the title bar was pressed
670 */
671static void wd_move(ui_wdecor_t *wdecor, void *arg, gfx_coord2_t *pos)
672{
673 ui_window_t *window = (ui_window_t *) arg;
674
675 if (window->dwindow != NULL)
676 (void) display_window_move_req(window->dwindow, pos);
677}
678
679/** Window decoration requested window resize.
680 *
681 * @param wdecor Window decoration
682 * @param arg Argument (window)
683 * @param rsztype Resize type
684 * @param pos Position where the button was pressed
685 */
686static void wd_resize(ui_wdecor_t *wdecor, void *arg,
687 ui_wdecor_rsztype_t rsztype, gfx_coord2_t *pos)
688{
689 ui_window_t *window = (ui_window_t *) arg;
690
691 if (window->dwindow != NULL)
692 (void) display_window_resize_req(window->dwindow, rsztype, pos);
693}
694
695/** Window decoration requested changing cursor.
696 *
697 * @param wdecor Window decoration
698 * @param arg Argument (window)
699 * @param cursor Cursor to set
700 */
701static void wd_set_cursor(ui_wdecor_t *wdecor, void *arg,
702 ui_stock_cursor_t cursor)
703{
704 ui_window_t *window = (ui_window_t *) arg;
705 display_stock_cursor_t dcursor;
706
707 if (cursor == window->cursor)
708 return;
709
710 dcursor = dcurs_arrow;
711
712 switch (cursor) {
713 case ui_curs_arrow:
714 dcursor = dcurs_arrow;
715 break;
716 case ui_curs_size_ud:
717 dcursor = dcurs_size_ud;
718 break;
719 case ui_curs_size_lr:
720 dcursor = dcurs_size_lr;
721 break;
722 case ui_curs_size_uldr:
723 dcursor = dcurs_size_uldr;
724 break;
725 case ui_curs_size_urdl:
726 dcursor = dcurs_size_urdl;
727 break;
728 }
729
730 if (window->dwindow != NULL)
731 (void) display_window_set_cursor(window->dwindow, dcursor);
732 window->cursor = cursor;
733}
734
735/** Send window close event.
736 *
737 * @param window Window
738 */
739void ui_window_send_close(ui_window_t *window)
740{
741 if (window->cb != NULL && window->cb->close != NULL)
742 window->cb->close(window, window->arg);
743}
744
745/** Send window focus event.
746 *
747 * @param window Window
748 */
749void ui_window_send_focus(ui_window_t *window)
750{
751 if (window->cb != NULL && window->cb->focus != NULL)
752 window->cb->focus(window, window->arg);
753}
754
755/** Send window keyboard event.
756 *
757 * @param window Window
758 */
759void ui_window_send_kbd(ui_window_t *window, kbd_event_t *kbd)
760{
761 if (window->cb != NULL && window->cb->kbd != NULL)
762 window->cb->kbd(window, window->arg, kbd);
763}
764
765/** Send window paint event.
766 *
767 * @param window Window
768 */
769errno_t ui_window_send_paint(ui_window_t *window)
770{
771 if (window->cb != NULL && window->cb->paint != NULL)
772 return window->cb->paint(window, window->arg);
773 else
774 return ui_window_def_paint(window);
775}
776
777/** Send window position event.
778 *
779 * @param window Window
780 */
781void ui_window_send_pos(ui_window_t *window, pos_event_t *pos)
782{
783 if (window->cb != NULL && window->cb->pos != NULL)
784 window->cb->pos(window, window->arg, pos);
785 else
786 ui_window_def_pos(window, pos);
787}
788
789/** Send window unfocus event.
790 *
791 * @param window Window
792 */
793void ui_window_send_unfocus(ui_window_t *window)
794{
795 if (window->cb != NULL && window->cb->unfocus != NULL)
796 window->cb->unfocus(window, window->arg);
797}
798
799/** Default window paint routine.
800 *
801 * @param window Window
802 * @return EOK on success or an error code
803 */
804errno_t ui_window_def_paint(ui_window_t *window)
805{
806 gfx_rect_t app_rect;
807 errno_t rc;
808
809 rc = gfx_set_color(window->gc, window->res->wnd_face_color);
810 if (rc != EOK)
811 return rc;
812
813 ui_window_get_app_rect(window, &app_rect);
814
815 rc = gfx_fill_rect(window->gc, &app_rect);
816 if (rc != EOK)
817 return rc;
818
819 if (window->control != NULL)
820 return ui_control_paint(window->control);
821
822 rc = gfx_update(window->res->gc);
823 if (rc != EOK)
824 return rc;
825
826 return EOK;
827}
828
829/** Default window position event routine.
830 *
831 * @param window Window
832 * @return EOK on success or an error code
833 */
834void ui_window_def_pos(ui_window_t *window, pos_event_t *pos)
835{
836 if (window->control != NULL)
837 ui_control_pos_event(window->control, pos);
838}
839
840/** Window invalidate callback
841 *
842 * @param arg Argument (ui_window_t *)
843 * @param rect Rectangle to update
844 */
845static void ui_window_invalidate(void *arg, gfx_rect_t *rect)
846{
847 ui_window_t *window = (ui_window_t *) arg;
848 gfx_rect_t env;
849
850 gfx_rect_envelope(&window->dirty_rect, rect, &env);
851 window->dirty_rect = env;
852}
853
854/** Window update callback
855 *
856 * @param arg Argument (ui_window_t *)
857 */
858static void ui_window_update(void *arg)
859{
860 ui_window_t *window = (ui_window_t *) arg;
861
862 if (!gfx_rect_is_empty(&window->dirty_rect))
863 (void) gfx_bitmap_render(window->bmp, &window->dirty_rect, NULL);
864
865 window->dirty_rect.p0.x = 0;
866 window->dirty_rect.p0.y = 0;
867 window->dirty_rect.p1.x = 0;
868 window->dirty_rect.p1.y = 0;
869}
870
871/** Application area invalidate callback
872 *
873 * @param arg Argument (ui_window_t *)
874 * @param rect Rectangle to update
875 */
876static void ui_window_app_invalidate(void *arg, gfx_rect_t *rect)
877{
878 ui_window_t *window = (ui_window_t *) arg;
879 gfx_rect_t arect;
880
881 ui_window_get_app_rect(window, &arect);
882
883 /* Render bitmap rectangle inside the application area */
884 (void) gfx_bitmap_render(window->app_bmp, rect, &arect.p0);
885 /*
886 * TODO Update applications to call gfx_update(), then
887 * we can defer update to ui_window_app_update().
888 */
889 (void) gfx_update(window->res->gc);
890}
891
892/** Application area update callback
893 *
894 * @param arg Argument (ui_window_t *)
895 */
896static void ui_window_app_update(void *arg)
897{
898 ui_window_t *window = (ui_window_t *) arg;
899
900 /*
901 * Not used since display is updated immediately
902 * in ui_window_app_invalidate
903 */
904 (void) window;
905}
906
907/** @}
908 */
Note: See TracBrowser for help on using the repository browser.