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

Last change on this file since 6baab83 was 6baab83, checked in by Jiri Svoboda <jiri@…>, 4 years ago

Set menu popup position based on parent window position

Added a method for getting the position of a display window.
This is then combined with the menu bar entry rectangle (which is
relative to the parent window) to compute a screen-relative
rectangle close to which the popup should be placed.

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