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

serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d7f82635 was 9e84d2c, checked in by jxsvoboda <5887334+jxsvoboda@…>, 4 years ago

Popup windows event delivery is special

Popup windows don't get focus, yet they still receive events.

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