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

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

Client-side UI rendering

It is possible to turn on and off and if turned on, one can also
enable or disable window double buffering (currently both options
are build-time).

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