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

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

Viewer fullsreen mode

Fullscreen window placement is more of a stopgap. Proper
solution would probably be via maximizing the window.

  • Property mode set to 100644
File size: 16.4 KB
Line 
1/*
2 * Copyright (c) 2020 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 <display.h>
37#include <errno.h>
38#include <gfx/bitmap.h>
39#include <gfx/context.h>
40#include <gfx/render.h>
41#include <io/kbd_event.h>
42#include <io/pos_event.h>
43#include <mem.h>
44#include <memgfx/memgc.h>
45#include <stdlib.h>
46#include <ui/control.h>
47#include <ui/resource.h>
48#include <ui/wdecor.h>
49#include <ui/window.h>
50#include "../private/control.h"
51#include "../private/dummygc.h"
52#include "../private/resource.h"
53#include "../private/ui.h"
54#include "../private/wdecor.h"
55#include "../private/window.h"
56
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 *);
61static void dwnd_resize_event(void *, gfx_rect_t *);
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,
69 .resize_event = dwnd_resize_event,
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 *);
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);
78
79static ui_wdecor_cb_t wdecor_cb = {
80 .close = wd_close,
81 .move = wd_move,
82 .resize = wd_resize,
83 .set_cursor = wd_set_cursor
84};
85
86static void ui_window_app_update(void *, gfx_rect_t *);
87
88/** Initialize window parameters structure.
89 *
90 * Window parameters structure must always be initialized using this function
91 * first. By default, the window will be decorated. To get a non-decorated
92 * window, one needs to clear ui_wds_decorated
93 * (e.g. params->style &= ~ui_wds_decorated).
94 *
95 * @param params Window parameters structure
96 */
97void ui_wnd_params_init(ui_wnd_params_t *params)
98{
99 memset(params, 0, sizeof(ui_wnd_params_t));
100
101 /* Make window decorated by default. */
102 params->style = ui_wds_decorated;
103}
104
105/** Create new window.
106 *
107 * @param ui User interface
108 * @param params Window parameters
109 * @param rwindow Place to store pointer to new window
110 * @return EOK on success or an error code
111 */
112errno_t ui_window_create(ui_t *ui, ui_wnd_params_t *params,
113 ui_window_t **rwindow)
114{
115 ui_window_t *window;
116 display_info_t info;
117 gfx_coord2_t pos;
118 gfx_coord2_t scr_dims;
119 display_wnd_params_t dparams;
120 display_window_t *dwindow = NULL;
121 gfx_context_t *gc = NULL;
122 ui_resource_t *res = NULL;
123 ui_wdecor_t *wdecor = NULL;
124 dummy_gc_t *dgc = NULL;
125 errno_t rc;
126
127 window = calloc(1, sizeof(ui_window_t));
128 if (window == NULL)
129 return ENOMEM;
130
131 display_wnd_params_init(&dparams);
132 dparams.rect = params->rect;
133 /* Only allow making the window larger */
134 gfx_rect_dims(&params->rect, &dparams.min_size);
135
136 if (ui->display != NULL) {
137 if (params->placement != ui_wnd_place_default) {
138 rc = display_get_info(ui->display, &info);
139 if (rc != EOK)
140 goto error;
141 }
142
143 if (params->placement == ui_wnd_place_full_screen) {
144 /* Make window the size of the screen */
145 gfx_rect_dims(&info.rect, &scr_dims);
146 gfx_coord2_add(&dparams.rect.p0, &scr_dims,
147 &dparams.rect.p1);
148 }
149
150 rc = display_window_create(ui->display, &dparams, &dwnd_cb,
151 (void *) window, &dwindow);
152 if (rc != EOK)
153 goto error;
154
155 if (params->placement != ui_wnd_place_default) {
156 pos.x = 0;
157 pos.y = 0;
158
159 switch (params->placement) {
160 case ui_wnd_place_default:
161 assert(false);
162 case ui_wnd_place_top_left:
163 case ui_wnd_place_full_screen:
164 pos.x = info.rect.p0.x - params->rect.p0.x;
165 pos.y = info.rect.p0.y - params->rect.p0.y;
166 break;
167 case ui_wnd_place_top_right:
168 pos.x = info.rect.p1.x - params->rect.p1.x;
169 pos.y = info.rect.p0.y - params->rect.p0.y;
170 break;
171 case ui_wnd_place_bottom_left:
172 pos.x = info.rect.p0.x - params->rect.p0.x;
173 pos.y = info.rect.p1.y - params->rect.p1.y;
174 break;
175 case ui_wnd_place_bottom_right:
176 pos.x = info.rect.p1.x - params->rect.p1.x;
177 pos.y = info.rect.p1.y - params->rect.p1.y;
178 break;
179 }
180
181 rc = display_window_move(dwindow, &pos);
182 if (rc != EOK)
183 goto error;
184 }
185
186 rc = display_window_get_gc(dwindow, &gc);
187 if (rc != EOK)
188 goto error;
189 } else {
190 /* Needed for unit tests */
191 rc = dummygc_create(&dgc);
192 if (rc != EOK)
193 goto error;
194
195 gc = dummygc_get_ctx(dgc);
196 }
197
198 rc = ui_resource_create(gc, &res);
199 if (rc != EOK)
200 goto error;
201
202 rc = ui_wdecor_create(res, params->caption, params->style, &wdecor);
203 if (rc != EOK)
204 goto error;
205
206 ui_wdecor_set_rect(wdecor, &dparams.rect);
207 ui_wdecor_set_cb(wdecor, &wdecor_cb, (void *) window);
208 ui_wdecor_paint(wdecor);
209
210 window->ui = ui;
211 window->dwindow = dwindow;
212 window->rect = dparams.rect;
213 window->gc = gc;
214 window->res = res;
215 window->wdecor = wdecor;
216 window->cursor = ui_curs_arrow;
217 *rwindow = window;
218 return EOK;
219error:
220 if (wdecor != NULL)
221 ui_wdecor_destroy(wdecor);
222 if (res != NULL)
223 ui_resource_destroy(res);
224 if (dgc != NULL)
225 dummygc_destroy(dgc);
226 if (dwindow != NULL)
227 display_window_destroy(dwindow);
228 free(window);
229 return rc;
230}
231
232/** Destroy window.
233 *
234 * @param window Window or @c NULL
235 */
236void ui_window_destroy(ui_window_t *window)
237{
238 if (window == NULL)
239 return;
240
241 ui_control_destroy(window->control);
242 ui_wdecor_destroy(window->wdecor);
243 ui_resource_destroy(window->res);
244 gfx_context_delete(window->gc);
245 display_window_destroy(window->dwindow);
246 free(window);
247}
248
249/** Add control to window.
250 *
251 * Only one control can be added to a window. If more than one control
252 * is added, the results are undefined.
253 *
254 * @param fixed Fixed layout
255 * @param control Control
256 * @return EOK on success, ENOMEM if out of memory
257 */
258void ui_window_add(ui_window_t *window, ui_control_t *control)
259{
260 assert(window->control == NULL);
261
262 window->control = control;
263 control->elemp = (void *) window;
264}
265
266/** Remove control from window.
267 *
268 * @param window Window
269 * @param control Control
270 */
271void ui_window_remove(ui_window_t *window, ui_control_t *control)
272{
273 assert(window->control == control);
274 assert((ui_window_t *) control->elemp == window);
275
276 window->control = NULL;
277 control->elemp = NULL;
278}
279
280/** Resize/move window.
281 *
282 * Resize window to the dimensions of @a rect. If @a rect.p0 is not 0,0,
283 * the top-left corner of the window will move on the screen accordingly.
284 *
285 * @param window Window
286 * @param rect Rectangle
287 *
288 * @return EOK on success or an error code
289 */
290errno_t ui_window_resize(ui_window_t *window, gfx_rect_t *rect)
291{
292 gfx_coord2_t offs;
293 gfx_rect_t nrect;
294 errno_t rc;
295
296 /*
297 * Move rect so that p0=0,0 - keep window's coordinate system origin
298 * locked to top-left corner of the window.
299 */
300 offs = rect->p0;
301 gfx_rect_rtranslate(&offs, rect, &nrect);
302
303 /* dwindow can be NULL in case of unit tests */
304 if (window->dwindow != NULL) {
305 rc = display_window_resize(window->dwindow, &offs, &nrect);
306 if (rc != EOK)
307 return rc;
308 }
309
310 ui_wdecor_set_rect(window->wdecor, &nrect);
311 ui_wdecor_paint(window->wdecor);
312 return EOK;
313}
314
315/** Set window callbacks.
316 *
317 * @param window Window
318 * @param cb Window decoration callbacks
319 * @param arg Callback argument
320 */
321void ui_window_set_cb(ui_window_t *window, ui_window_cb_t *cb, void *arg)
322{
323 window->cb = cb;
324 window->arg = arg;
325}
326
327/** Get UI resource from window.
328 *
329 * @param window Window
330 * @return UI resource
331 */
332ui_resource_t *ui_window_get_res(ui_window_t *window)
333{
334 return window->res;
335}
336
337/** Get window GC.
338 *
339 * @param window Window
340 * @return GC (relative to window)
341 */
342gfx_context_t *ui_window_get_gc(ui_window_t *window)
343{
344 return window->gc;
345}
346
347/** Get window application area GC
348 *
349 * @param window Window
350 * @param rgc Place to store GC (relative to application area)
351 * @return EOK on success or an error code
352 */
353errno_t ui_window_get_app_gc(ui_window_t *window, gfx_context_t **rgc)
354{
355 gfx_bitmap_params_t params;
356 gfx_bitmap_alloc_t alloc;
357 gfx_rect_t rect;
358 mem_gc_t *memgc;
359 errno_t rc;
360
361 if (window->app_gc == NULL) {
362 assert(window->app_bmp == NULL);
363
364 gfx_bitmap_params_init(&params);
365
366 /*
367 * The bitmap will have the same dimensions as the
368 * application rectangle, but start at 0,0.
369 */
370 ui_window_get_app_rect(window, &rect);
371 gfx_rect_rtranslate(&rect.p0, &rect, &params.rect);
372
373 rc = gfx_bitmap_create(window->gc, &params, NULL,
374 &window->app_bmp);
375 if (rc != EOK)
376 return rc;
377
378 rc = gfx_bitmap_get_alloc(window->app_bmp, &alloc);
379 if (rc != EOK) {
380 gfx_bitmap_destroy(window->app_bmp);
381 return rc;
382 }
383
384 rc = mem_gc_create(&params.rect, &alloc, ui_window_app_update,
385 (void *) window, &memgc);
386 if (rc != EOK) {
387 gfx_bitmap_destroy(window->app_bmp);
388 return rc;
389 }
390
391 window->app_gc = mem_gc_get_ctx(memgc);
392 }
393
394 *rgc = window->app_gc;
395 return EOK;
396}
397
398/** Get window application rectangle
399 *
400 * @param window Window
401 * @param rect Place to store application rectangle
402 */
403void ui_window_get_app_rect(ui_window_t *window, gfx_rect_t *rect)
404{
405 ui_wdecor_geom_t geom;
406
407 ui_wdecor_get_geom(window->wdecor, &geom);
408 *rect = geom.app_area_rect;
409}
410
411/** Paint window
412 *
413 * @param window Window
414 * @return EOK on success or an error code
415 */
416errno_t ui_window_paint(ui_window_t *window)
417{
418 return ui_window_send_paint(window);
419}
420
421/** Handle window close event. */
422static void dwnd_close_event(void *arg)
423{
424 ui_window_t *window = (ui_window_t *) arg;
425
426 ui_window_send_close(window);
427}
428
429/** Handle window focus event. */
430static void dwnd_focus_event(void *arg)
431{
432 ui_window_t *window = (ui_window_t *) arg;
433
434 if (window->wdecor != NULL) {
435 ui_wdecor_set_active(window->wdecor, true);
436 ui_wdecor_paint(window->wdecor);
437 }
438
439 ui_window_send_focus(window);
440}
441
442/** Handle window keyboard event */
443static void dwnd_kbd_event(void *arg, kbd_event_t *kbd_event)
444{
445 ui_window_t *window = (ui_window_t *) arg;
446
447 (void) window;
448 ui_window_send_kbd(window, kbd_event);
449}
450
451/** Handle window position event */
452static void dwnd_pos_event(void *arg, pos_event_t *event)
453{
454 ui_window_t *window = (ui_window_t *) arg;
455
456 /* Make sure we don't process events until fully initialized */
457 if (window->wdecor == NULL)
458 return;
459
460 ui_wdecor_pos_event(window->wdecor, event);
461 ui_window_send_pos(window, event);
462}
463
464/** Handle window resize event */
465static void dwnd_resize_event(void *arg, gfx_rect_t *rect)
466{
467 ui_window_t *window = (ui_window_t *) arg;
468
469 /* Make sure we don't process events until fully initialized */
470 if (window->wdecor == NULL)
471 return;
472
473 if ((window->wdecor->style & ui_wds_resizable) == 0)
474 return;
475
476 (void) ui_window_resize(window, rect);
477 (void) ui_window_paint(window);
478}
479
480/** Handle window unfocus event. */
481static void dwnd_unfocus_event(void *arg)
482{
483 ui_window_t *window = (ui_window_t *) arg;
484
485 if (window->wdecor != NULL) {
486 ui_wdecor_set_active(window->wdecor, false);
487 ui_wdecor_paint(window->wdecor);
488 }
489
490 ui_window_send_unfocus(window);
491}
492
493/** Window decoration requested window closure.
494 *
495 * @param wdecor Window decoration
496 * @param arg Argument (window)
497 */
498static void wd_close(ui_wdecor_t *wdecor, void *arg)
499{
500 ui_window_t *window = (ui_window_t *) arg;
501
502 ui_window_send_close(window);
503}
504
505/** Window decoration requested window move.
506 *
507 * @param wdecor Window decoration
508 * @param arg Argument (window)
509 * @param pos Position where the title bar was pressed
510 */
511static void wd_move(ui_wdecor_t *wdecor, void *arg, gfx_coord2_t *pos)
512{
513 ui_window_t *window = (ui_window_t *) arg;
514
515 (void) display_window_move_req(window->dwindow, pos);
516}
517
518/** Window decoration requested window resize.
519 *
520 * @param wdecor Window decoration
521 * @param arg Argument (window)
522 * @param rsztype Resize type
523 * @param pos Position where the button was pressed
524 */
525static void wd_resize(ui_wdecor_t *wdecor, void *arg,
526 ui_wdecor_rsztype_t rsztype, gfx_coord2_t *pos)
527{
528 ui_window_t *window = (ui_window_t *) arg;
529
530 (void) display_window_resize_req(window->dwindow, rsztype, pos);
531}
532
533/** Window decoration requested changing cursor.
534 *
535 * @param wdecor Window decoration
536 * @param arg Argument (window)
537 * @param cursor Cursor to set
538 */
539static void wd_set_cursor(ui_wdecor_t *wdecor, void *arg,
540 ui_stock_cursor_t cursor)
541{
542 ui_window_t *window = (ui_window_t *) arg;
543 display_stock_cursor_t dcursor;
544
545 if (cursor == window->cursor)
546 return;
547
548 dcursor = dcurs_arrow;
549
550 switch (cursor) {
551 case ui_curs_arrow:
552 dcursor = dcurs_arrow;
553 break;
554 case ui_curs_size_ud:
555 dcursor = dcurs_size_ud;
556 break;
557 case ui_curs_size_lr:
558 dcursor = dcurs_size_lr;
559 break;
560 case ui_curs_size_uldr:
561 dcursor = dcurs_size_uldr;
562 break;
563 case ui_curs_size_urdl:
564 dcursor = dcurs_size_urdl;
565 break;
566 }
567
568 (void) display_window_set_cursor(window->dwindow, dcursor);
569 window->cursor = cursor;
570}
571
572/** Send window close event.
573 *
574 * @param window Window
575 */
576void ui_window_send_close(ui_window_t *window)
577{
578 if (window->cb != NULL && window->cb->close != NULL)
579 window->cb->close(window, window->arg);
580}
581
582/** Send window focus event.
583 *
584 * @param window Window
585 */
586void ui_window_send_focus(ui_window_t *window)
587{
588 if (window->cb != NULL && window->cb->focus != NULL)
589 window->cb->focus(window, window->arg);
590}
591
592/** Send window keyboard event.
593 *
594 * @param window Window
595 */
596void ui_window_send_kbd(ui_window_t *window, kbd_event_t *kbd)
597{
598 if (window->cb != NULL && window->cb->kbd != NULL)
599 window->cb->kbd(window, window->arg, kbd);
600}
601
602/** Send window paint event.
603 *
604 * @param window Window
605 */
606errno_t ui_window_send_paint(ui_window_t *window)
607{
608 if (window->cb != NULL && window->cb->paint != NULL)
609 return window->cb->paint(window, window->arg);
610 else
611 return ui_window_def_paint(window);
612}
613
614/** Send window position event.
615 *
616 * @param window Window
617 */
618void ui_window_send_pos(ui_window_t *window, pos_event_t *pos)
619{
620 if (window->cb != NULL && window->cb->pos != NULL)
621 window->cb->pos(window, window->arg, pos);
622 else
623 ui_window_def_pos(window, pos);
624}
625
626/** Send window unfocus event.
627 *
628 * @param window Window
629 */
630void ui_window_send_unfocus(ui_window_t *window)
631{
632 if (window->cb != NULL && window->cb->unfocus != NULL)
633 window->cb->unfocus(window, window->arg);
634}
635
636/** Default window paint routine.
637 *
638 * @param window Window
639 * @return EOK on success or an error code
640 */
641errno_t ui_window_def_paint(ui_window_t *window)
642{
643 gfx_rect_t app_rect;
644 errno_t rc;
645
646 rc = gfx_set_color(window->gc, window->res->wnd_face_color);
647 if (rc != EOK)
648 return rc;
649
650 ui_window_get_app_rect(window, &app_rect);
651
652 rc = gfx_fill_rect(window->gc, &app_rect);
653 if (rc != EOK)
654 return rc;
655
656 if (window->control != NULL)
657 return ui_control_paint(window->control);
658
659 return EOK;
660}
661
662/** Default window position event routine.
663 *
664 * @param window Window
665 * @return EOK on success or an error code
666 */
667void ui_window_def_pos(ui_window_t *window, pos_event_t *pos)
668{
669 if (window->control != NULL)
670 ui_control_pos_event(window->control, pos);
671}
672
673/** Application area update callback
674 *
675 * @param arg Argument (ui_window_t *)
676 * @param rect Rectangle to update
677 */
678static void ui_window_app_update(void *arg, gfx_rect_t *rect)
679{
680 ui_window_t *window = (ui_window_t *) arg;
681 gfx_rect_t arect;
682
683 ui_window_get_app_rect(window, &arect);
684
685 /* Render bitmap rectangle inside the application area */
686 (void) gfx_bitmap_render(window->app_bmp, rect, &arect.p0);
687}
688
689/** @}
690 */
Note: See TracBrowser for help on using the repository browser.