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

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

Print text as text in textmode UI. Make calculator smaller in text mode.

  • Property mode set to 100644
File size: 21.4 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;
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 rc = ui_resource_create(window->gc, ui_is_textmode(ui), &res);
259 if (rc != EOK)
260 goto error;
261
262 rc = ui_wdecor_create(res, params->caption, params->style, &wdecor);
263 if (rc != EOK)
264 goto error;
265
266 ui_wdecor_set_rect(wdecor, &dparams.rect);
267 ui_wdecor_set_cb(wdecor, &wdecor_cb, (void *) window);
268 ui_wdecor_paint(wdecor);
269
270 window->ui = ui;
271 window->dwindow = dwindow;
272 window->rect = dparams.rect;
273
274 window->res = res;
275 window->wdecor = wdecor;
276 window->cursor = ui_curs_arrow;
277 *rwindow = window;
278
279 ui->root_wnd = window;
280 return EOK;
281error:
282 if (wdecor != NULL)
283 ui_wdecor_destroy(wdecor);
284 if (res != NULL)
285 ui_resource_destroy(res);
286 if (memgc != NULL)
287 mem_gc_delete(memgc);
288 if (bmp != NULL)
289 gfx_bitmap_destroy(bmp);
290 if (dgc != NULL)
291 dummygc_destroy(dgc);
292 if (dwindow != NULL)
293 display_window_destroy(dwindow);
294 free(window);
295 return rc;
296}
297
298/** Destroy window.
299 *
300 * @param window Window or @c NULL
301 */
302void ui_window_destroy(ui_window_t *window)
303{
304 if (window == NULL)
305 return;
306
307 ui_control_destroy(window->control);
308 ui_wdecor_destroy(window->wdecor);
309 ui_resource_destroy(window->res);
310 if (0 && window->app_mgc != NULL)
311 mem_gc_delete(window->app_mgc);
312 if (0 && window->app_bmp != NULL)
313 gfx_bitmap_destroy(window->app_bmp);
314 if (window->mgc != NULL) {
315 mem_gc_delete(window->mgc);
316 window->gc = NULL;
317 }
318 if (window->bmp != NULL)
319 gfx_bitmap_destroy(window->bmp);
320 gfx_context_delete(window->gc);
321 if (window->dwindow != NULL)
322 display_window_destroy(window->dwindow);
323 free(window);
324}
325
326/** Add control to window.
327 *
328 * Only one control can be added to a window. If more than one control
329 * is added, the results are undefined.
330 *
331 * @param fixed Fixed layout
332 * @param control Control
333 * @return EOK on success, ENOMEM if out of memory
334 */
335void ui_window_add(ui_window_t *window, ui_control_t *control)
336{
337 assert(window->control == NULL);
338
339 window->control = control;
340 control->elemp = (void *) window;
341}
342
343/** Remove control from window.
344 *
345 * @param window Window
346 * @param control Control
347 */
348void ui_window_remove(ui_window_t *window, ui_control_t *control)
349{
350 assert(window->control == control);
351 assert((ui_window_t *) control->elemp == window);
352
353 window->control = NULL;
354 control->elemp = NULL;
355}
356
357/** Resize/move window.
358 *
359 * Resize window to the dimensions of @a rect. If @a rect.p0 is not 0,0,
360 * the top-left corner of the window will move on the screen accordingly.
361 *
362 * @param window Window
363 * @param rect Rectangle
364 *
365 * @return EOK on success or an error code
366 */
367errno_t ui_window_resize(ui_window_t *window, gfx_rect_t *rect)
368{
369 gfx_coord2_t offs;
370 gfx_rect_t nrect;
371 gfx_rect_t arect;
372 gfx_bitmap_t *app_bmp = NULL;
373 gfx_bitmap_t *win_bmp = NULL;
374 gfx_bitmap_params_t app_params;
375 gfx_bitmap_params_t win_params;
376 gfx_bitmap_alloc_t app_alloc;
377 gfx_bitmap_alloc_t win_alloc;
378 errno_t rc;
379
380 /*
381 * Move rect so that p0=0,0 - keep window's coordinate system origin
382 * locked to top-left corner of the window.
383 */
384 offs = rect->p0;
385 gfx_rect_rtranslate(&offs, rect, &nrect);
386
387 /* mgc != NULL iff client-side rendering */
388 if (window->mgc != NULL) {
389 /* Resize window bitmap */
390 assert(window->bmp != NULL);
391
392 gfx_bitmap_params_init(&win_params);
393#ifndef CONFIG_WIN_DOUBLE_BUF
394 win_params.flags |= bmpf_direct_output;
395#endif
396 win_params.rect = nrect;
397
398 rc = gfx_bitmap_create(window->realgc, &win_params, NULL,
399 &win_bmp);
400 if (rc != EOK)
401 goto error;
402
403 rc = gfx_bitmap_get_alloc(win_bmp, &win_alloc);
404 if (rc != EOK)
405 goto error;
406 }
407
408 /* Application area GC? */
409 if (window->app_gc != NULL) {
410 /* Resize application bitmap */
411 assert(window->app_bmp != NULL);
412
413 gfx_bitmap_params_init(&app_params);
414
415 /*
416 * The bitmap will have the same dimensions as the
417 * application rectangle, but start at 0,0.
418 */
419 ui_wdecor_app_from_rect(window->wdecor->style, &nrect, &arect);
420 gfx_rect_rtranslate(&arect.p0, &arect, &app_params.rect);
421
422 rc = gfx_bitmap_create(window->gc, &app_params, NULL,
423 &app_bmp);
424 if (rc != EOK)
425 goto error;
426
427 rc = gfx_bitmap_get_alloc(app_bmp, &app_alloc);
428 if (rc != EOK)
429 goto error;
430 }
431
432 /* dwindow can be NULL in case of unit tests */
433 if (window->dwindow != NULL) {
434 rc = display_window_resize(window->dwindow, &offs, &nrect);
435 if (rc != EOK)
436 goto error;
437 }
438
439 /* CLient side rendering? */
440 if (window->mgc != NULL) {
441 mem_gc_retarget(window->mgc, &win_params.rect, &win_alloc);
442
443 gfx_bitmap_destroy(window->bmp);
444 window->bmp = win_bmp;
445 }
446
447 ui_wdecor_set_rect(window->wdecor, &nrect);
448 ui_wdecor_paint(window->wdecor);
449 gfx_update(window->gc);
450
451 /* Application area GC? */
452 if (window->app_gc != NULL) {
453 mem_gc_retarget(window->app_mgc, &app_params.rect, &app_alloc);
454
455 gfx_bitmap_destroy(window->app_bmp);
456 window->app_bmp = app_bmp;
457 }
458
459 return EOK;
460error:
461 if (app_bmp != NULL)
462 gfx_bitmap_destroy(app_bmp);
463 if (win_bmp != NULL)
464 gfx_bitmap_destroy(win_bmp);
465 return rc;
466}
467
468/** Set window callbacks.
469 *
470 * @param window Window
471 * @param cb Window decoration callbacks
472 * @param arg Callback argument
473 */
474void ui_window_set_cb(ui_window_t *window, ui_window_cb_t *cb, void *arg)
475{
476 window->cb = cb;
477 window->arg = arg;
478}
479
480/** Get UI resource from window.
481 *
482 * @param window Window
483 * @return UI resource
484 */
485ui_resource_t *ui_window_get_res(ui_window_t *window)
486{
487 return window->res;
488}
489
490/** Get window GC.
491 *
492 * @param window Window
493 * @return GC (relative to window)
494 */
495gfx_context_t *ui_window_get_gc(ui_window_t *window)
496{
497 return window->gc;
498}
499
500/** Get window application area GC
501 *
502 * @param window Window
503 * @param rgc Place to store GC (relative to application area)
504 * @return EOK on success or an error code
505 */
506errno_t ui_window_get_app_gc(ui_window_t *window, gfx_context_t **rgc)
507{
508 gfx_bitmap_params_t params;
509 gfx_bitmap_alloc_t alloc;
510 gfx_rect_t rect;
511 mem_gc_t *memgc;
512 errno_t rc;
513
514 if (window->app_gc == NULL) {
515 assert(window->app_bmp == NULL);
516
517 gfx_bitmap_params_init(&params);
518
519 /*
520 * The bitmap will have the same dimensions as the
521 * application rectangle, but start at 0,0.
522 */
523 ui_window_get_app_rect(window, &rect);
524 gfx_rect_rtranslate(&rect.p0, &rect, &params.rect);
525
526 rc = gfx_bitmap_create(window->gc, &params, NULL,
527 &window->app_bmp);
528 if (rc != EOK)
529 return rc;
530
531 rc = gfx_bitmap_get_alloc(window->app_bmp, &alloc);
532 if (rc != EOK) {
533 gfx_bitmap_destroy(window->app_bmp);
534 return rc;
535 }
536
537 rc = mem_gc_create(&params.rect, &alloc, ui_window_app_invalidate,
538 ui_window_app_update, (void *) window, &memgc);
539 if (rc != EOK) {
540 gfx_bitmap_destroy(window->app_bmp);
541 return rc;
542 }
543
544 window->app_mgc = memgc;
545 window->app_gc = mem_gc_get_ctx(memgc);
546 }
547
548 *rgc = window->app_gc;
549 return EOK;
550}
551
552/** Get window application rectangle
553 *
554 * @param window Window
555 * @param rect Place to store application rectangle
556 */
557void ui_window_get_app_rect(ui_window_t *window, gfx_rect_t *rect)
558{
559 ui_wdecor_geom_t geom;
560
561 ui_wdecor_get_geom(window->wdecor, &geom);
562 *rect = geom.app_area_rect;
563}
564
565/** Paint window
566 *
567 * @param window Window
568 * @return EOK on success or an error code
569 */
570errno_t ui_window_paint(ui_window_t *window)
571{
572 return ui_window_send_paint(window);
573}
574
575/** Handle window close event. */
576static void dwnd_close_event(void *arg)
577{
578 ui_window_t *window = (ui_window_t *) arg;
579
580 ui_window_send_close(window);
581}
582
583/** Handle window focus event. */
584static void dwnd_focus_event(void *arg)
585{
586 ui_window_t *window = (ui_window_t *) arg;
587
588 if (window->wdecor != NULL) {
589 ui_wdecor_set_active(window->wdecor, true);
590 ui_wdecor_paint(window->wdecor);
591 }
592
593 ui_window_send_focus(window);
594}
595
596/** Handle window keyboard event */
597static void dwnd_kbd_event(void *arg, kbd_event_t *kbd_event)
598{
599 ui_window_t *window = (ui_window_t *) arg;
600
601 (void) window;
602 ui_window_send_kbd(window, kbd_event);
603}
604
605/** Handle window position event */
606static void dwnd_pos_event(void *arg, pos_event_t *event)
607{
608 ui_window_t *window = (ui_window_t *) arg;
609
610 /* Make sure we don't process events until fully initialized */
611 if (window->wdecor == NULL)
612 return;
613
614 ui_wdecor_pos_event(window->wdecor, event);
615 ui_window_send_pos(window, event);
616}
617
618/** Handle window resize event */
619static void dwnd_resize_event(void *arg, gfx_rect_t *rect)
620{
621 ui_window_t *window = (ui_window_t *) arg;
622
623 /* Make sure we don't process events until fully initialized */
624 if (window->wdecor == NULL)
625 return;
626
627 if ((window->wdecor->style & ui_wds_resizable) == 0)
628 return;
629
630 (void) ui_window_resize(window, rect);
631 (void) ui_window_paint(window);
632}
633
634/** Handle window unfocus event. */
635static void dwnd_unfocus_event(void *arg)
636{
637 ui_window_t *window = (ui_window_t *) arg;
638
639 if (window->wdecor != NULL) {
640 ui_wdecor_set_active(window->wdecor, false);
641 ui_wdecor_paint(window->wdecor);
642 }
643
644 ui_window_send_unfocus(window);
645}
646
647/** Window decoration requested window closure.
648 *
649 * @param wdecor Window decoration
650 * @param arg Argument (window)
651 */
652static void wd_close(ui_wdecor_t *wdecor, void *arg)
653{
654 ui_window_t *window = (ui_window_t *) arg;
655
656 ui_window_send_close(window);
657}
658
659/** Window decoration requested window move.
660 *
661 * @param wdecor Window decoration
662 * @param arg Argument (window)
663 * @param pos Position where the title bar was pressed
664 */
665static void wd_move(ui_wdecor_t *wdecor, void *arg, gfx_coord2_t *pos)
666{
667 ui_window_t *window = (ui_window_t *) arg;
668
669 if (window->dwindow != NULL)
670 (void) display_window_move_req(window->dwindow, pos);
671}
672
673/** Window decoration requested window resize.
674 *
675 * @param wdecor Window decoration
676 * @param arg Argument (window)
677 * @param rsztype Resize type
678 * @param pos Position where the button was pressed
679 */
680static void wd_resize(ui_wdecor_t *wdecor, void *arg,
681 ui_wdecor_rsztype_t rsztype, gfx_coord2_t *pos)
682{
683 ui_window_t *window = (ui_window_t *) arg;
684
685 if (window->dwindow != NULL)
686 (void) display_window_resize_req(window->dwindow, rsztype, pos);
687}
688
689/** Window decoration requested changing cursor.
690 *
691 * @param wdecor Window decoration
692 * @param arg Argument (window)
693 * @param cursor Cursor to set
694 */
695static void wd_set_cursor(ui_wdecor_t *wdecor, void *arg,
696 ui_stock_cursor_t cursor)
697{
698 ui_window_t *window = (ui_window_t *) arg;
699 display_stock_cursor_t dcursor;
700
701 if (cursor == window->cursor)
702 return;
703
704 dcursor = dcurs_arrow;
705
706 switch (cursor) {
707 case ui_curs_arrow:
708 dcursor = dcurs_arrow;
709 break;
710 case ui_curs_size_ud:
711 dcursor = dcurs_size_ud;
712 break;
713 case ui_curs_size_lr:
714 dcursor = dcurs_size_lr;
715 break;
716 case ui_curs_size_uldr:
717 dcursor = dcurs_size_uldr;
718 break;
719 case ui_curs_size_urdl:
720 dcursor = dcurs_size_urdl;
721 break;
722 }
723
724 if (window->dwindow != NULL)
725 (void) display_window_set_cursor(window->dwindow, dcursor);
726 window->cursor = cursor;
727}
728
729/** Send window close event.
730 *
731 * @param window Window
732 */
733void ui_window_send_close(ui_window_t *window)
734{
735 if (window->cb != NULL && window->cb->close != NULL)
736 window->cb->close(window, window->arg);
737}
738
739/** Send window focus event.
740 *
741 * @param window Window
742 */
743void ui_window_send_focus(ui_window_t *window)
744{
745 if (window->cb != NULL && window->cb->focus != NULL)
746 window->cb->focus(window, window->arg);
747}
748
749/** Send window keyboard event.
750 *
751 * @param window Window
752 */
753void ui_window_send_kbd(ui_window_t *window, kbd_event_t *kbd)
754{
755 if (window->cb != NULL && window->cb->kbd != NULL)
756 window->cb->kbd(window, window->arg, kbd);
757}
758
759/** Send window paint event.
760 *
761 * @param window Window
762 */
763errno_t ui_window_send_paint(ui_window_t *window)
764{
765 if (window->cb != NULL && window->cb->paint != NULL)
766 return window->cb->paint(window, window->arg);
767 else
768 return ui_window_def_paint(window);
769}
770
771/** Send window position event.
772 *
773 * @param window Window
774 */
775void ui_window_send_pos(ui_window_t *window, pos_event_t *pos)
776{
777 if (window->cb != NULL && window->cb->pos != NULL)
778 window->cb->pos(window, window->arg, pos);
779 else
780 ui_window_def_pos(window, pos);
781}
782
783/** Send window unfocus event.
784 *
785 * @param window Window
786 */
787void ui_window_send_unfocus(ui_window_t *window)
788{
789 if (window->cb != NULL && window->cb->unfocus != NULL)
790 window->cb->unfocus(window, window->arg);
791}
792
793/** Default window paint routine.
794 *
795 * @param window Window
796 * @return EOK on success or an error code
797 */
798errno_t ui_window_def_paint(ui_window_t *window)
799{
800 gfx_rect_t app_rect;
801 errno_t rc;
802
803 rc = gfx_set_color(window->gc, window->res->wnd_face_color);
804 if (rc != EOK)
805 return rc;
806
807 ui_window_get_app_rect(window, &app_rect);
808
809 rc = gfx_fill_rect(window->gc, &app_rect);
810 if (rc != EOK)
811 return rc;
812
813 if (window->control != NULL)
814 return ui_control_paint(window->control);
815
816 rc = gfx_update(window->res->gc);
817 if (rc != EOK)
818 return rc;
819
820 return EOK;
821}
822
823/** Default window position event routine.
824 *
825 * @param window Window
826 * @return EOK on success or an error code
827 */
828void ui_window_def_pos(ui_window_t *window, pos_event_t *pos)
829{
830 if (window->control != NULL)
831 ui_control_pos_event(window->control, pos);
832}
833
834/** Window invalidate callback
835 *
836 * @param arg Argument (ui_window_t *)
837 * @param rect Rectangle to update
838 */
839static void ui_window_invalidate(void *arg, gfx_rect_t *rect)
840{
841 ui_window_t *window = (ui_window_t *) arg;
842 gfx_rect_t env;
843
844 gfx_rect_envelope(&window->dirty_rect, rect, &env);
845 window->dirty_rect = env;
846}
847
848/** Window update callback
849 *
850 * @param arg Argument (ui_window_t *)
851 */
852static void ui_window_update(void *arg)
853{
854 ui_window_t *window = (ui_window_t *) arg;
855
856 if (!gfx_rect_is_empty(&window->dirty_rect))
857 (void) gfx_bitmap_render(window->bmp, &window->dirty_rect, NULL);
858
859 window->dirty_rect.p0.x = 0;
860 window->dirty_rect.p0.y = 0;
861 window->dirty_rect.p1.x = 0;
862 window->dirty_rect.p1.y = 0;
863}
864
865/** Application area invalidate callback
866 *
867 * @param arg Argument (ui_window_t *)
868 * @param rect Rectangle to update
869 */
870static void ui_window_app_invalidate(void *arg, gfx_rect_t *rect)
871{
872 ui_window_t *window = (ui_window_t *) arg;
873 gfx_rect_t arect;
874
875 ui_window_get_app_rect(window, &arect);
876
877 /* Render bitmap rectangle inside the application area */
878 (void) gfx_bitmap_render(window->app_bmp, rect, &arect.p0);
879 /*
880 * TODO Update applications to call gfx_update(), then
881 * we can defer update to ui_window_app_update().
882 */
883 (void) gfx_update(window->res->gc);
884}
885
886/** Application area update callback
887 *
888 * @param arg Argument (ui_window_t *)
889 */
890static void ui_window_app_update(void *arg)
891{
892 ui_window_t *window = (ui_window_t *) arg;
893
894 /*
895 * Not used since display is updated immediately
896 * in ui_window_app_invalidate
897 */
898 (void) window;
899}
900
901/** @}
902 */
Note: See TracBrowser for help on using the repository browser.