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

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

Window placement

Needed to recreate the current 'desktop' and keep CI tests working

  • Property mode set to 100644
File size: 13.8 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_unfocus_event(void *);
62
63static display_wnd_cb_t dwnd_cb = {
64 .close_event = dwnd_close_event,
65 .focus_event = dwnd_focus_event,
66 .kbd_event = dwnd_kbd_event,
67 .pos_event = dwnd_pos_event,
68 .unfocus_event = dwnd_unfocus_event
69};
70
71static void wd_close(ui_wdecor_t *, void *);
72static void wd_move(ui_wdecor_t *, void *, gfx_coord2_t *);
73
74static ui_wdecor_cb_t wdecor_cb = {
75 .close = wd_close,
76 .move = wd_move
77};
78
79static void ui_window_app_update(void *, gfx_rect_t *);
80
81/** Initialize window parameters structure.
82 *
83 * Window parameters structure must always be initialized using this function
84 * first.
85 *
86 * @param params Window parameters structure
87 */
88void ui_wnd_params_init(ui_wnd_params_t *params)
89{
90 memset(params, 0, sizeof(ui_wnd_params_t));
91}
92
93/** Create new window.
94 *
95 * @param ui User interface
96 * @param params Window parameters
97 * @param rwindow Place to store pointer to new window
98 * @return EOK on success or an error code
99 */
100errno_t ui_window_create(ui_t *ui, ui_wnd_params_t *params,
101 ui_window_t **rwindow)
102{
103 ui_window_t *window;
104 display_info_t info;
105 gfx_coord2_t pos;
106 display_wnd_params_t dparams;
107 display_window_t *dwindow = NULL;
108 gfx_context_t *gc = NULL;
109 ui_resource_t *res = NULL;
110 ui_wdecor_t *wdecor = NULL;
111 dummy_gc_t *dgc = NULL;
112 errno_t rc;
113
114 window = calloc(1, sizeof(ui_window_t));
115 if (window == NULL)
116 return ENOMEM;
117
118 display_wnd_params_init(&dparams);
119 dparams.rect = params->rect;
120
121 if (ui->display != NULL) {
122 rc = display_window_create(ui->display, &dparams, &dwnd_cb,
123 (void *) window, &dwindow);
124 if (rc != EOK)
125 goto error;
126
127 if (params->placement != ui_wnd_place_default) {
128 rc = display_get_info(ui->display, &info);
129 if (rc != EOK)
130 goto error;
131
132 pos.x = 0;
133 pos.y = 0;
134
135 switch (params->placement) {
136 case ui_wnd_place_default:
137 assert(false);
138 case ui_wnd_place_top_left:
139 pos.x = info.rect.p0.x - params->rect.p0.x;
140 pos.y = info.rect.p0.y - params->rect.p0.y;
141 break;
142 case ui_wnd_place_top_right:
143 pos.x = info.rect.p1.x - params->rect.p1.x;
144 pos.y = info.rect.p0.y - params->rect.p0.y;
145 break;
146 case ui_wnd_place_bottom_left:
147 pos.x = info.rect.p0.x - params->rect.p0.x;
148 pos.y = info.rect.p1.y - params->rect.p1.y;
149 break;
150 case ui_wnd_place_bottom_right:
151 pos.x = info.rect.p1.x - params->rect.p1.x;
152 pos.y = info.rect.p1.y - params->rect.p1.y;
153 break;
154 }
155
156 rc = display_window_move(dwindow, &pos);
157 if (rc != EOK)
158 goto error;
159 }
160
161 rc = display_window_get_gc(dwindow, &gc);
162 if (rc != EOK)
163 goto error;
164 } else {
165 /* Needed for unit tests */
166 rc = dummygc_create(&dgc);
167 if (rc != EOK)
168 goto error;
169
170 gc = dummygc_get_ctx(dgc);
171 }
172
173 rc = ui_resource_create(gc, &res);
174 if (rc != EOK)
175 goto error;
176
177 rc = ui_wdecor_create(res, params->caption, &wdecor);
178 if (rc != EOK)
179 goto error;
180
181 ui_wdecor_set_rect(wdecor, &params->rect);
182 ui_wdecor_set_cb(wdecor, &wdecor_cb, (void *) window);
183 ui_wdecor_paint(wdecor);
184
185 window->ui = ui;
186 window->dwindow = dwindow;
187 window->gc = gc;
188 window->res = res;
189 window->wdecor = wdecor;
190 *rwindow = window;
191 return EOK;
192error:
193 if (wdecor != NULL)
194 ui_wdecor_destroy(wdecor);
195 if (res != NULL)
196 ui_resource_destroy(res);
197 if (dgc != NULL)
198 dummygc_destroy(dgc);
199 if (dwindow != NULL)
200 display_window_destroy(dwindow);
201 free(window);
202 return rc;
203}
204
205/** Destroy window.
206 *
207 * @param window Window or @c NULL
208 */
209void ui_window_destroy(ui_window_t *window)
210{
211 if (window == NULL)
212 return;
213
214 ui_control_destroy(window->control);
215 ui_wdecor_destroy(window->wdecor);
216 ui_resource_destroy(window->res);
217 gfx_context_delete(window->gc);
218 display_window_destroy(window->dwindow);
219 free(window);
220}
221
222/** Add control to window.
223 *
224 * Only one control can be added to a window. If more than one control
225 * is added, the results are undefined.
226 *
227 * @param fixed Fixed layout
228 * @param control Control
229 * @return EOK on success, ENOMEM if out of memory
230 */
231void ui_window_add(ui_window_t *window, ui_control_t *control)
232{
233 assert(window->control == NULL);
234
235 window->control = control;
236 control->elemp = (void *) window;
237}
238
239/** Remove control from window.
240 *
241 * @param window Window
242 * @param control Control
243 */
244void ui_window_remove(ui_window_t *window, ui_control_t *control)
245{
246 assert(window->control == control);
247 assert((ui_window_t *) control->elemp == window);
248
249 window->control = NULL;
250 control->elemp = NULL;
251}
252
253/** Resize/move window.
254 *
255 * Resize window to the dimensions of @a rect. If @a rect.p0 is not 0,0,
256 * the top-left corner of the window will move on the screen accordingly.
257 *
258 * @param window Window
259 * @param rect Rectangle
260 *
261 * @return EOK on success or an error code
262 */
263errno_t ui_window_resize(ui_window_t *window, gfx_rect_t *rect)
264{
265 gfx_coord2_t offs;
266 gfx_rect_t nrect;
267 errno_t rc;
268
269 /*
270 * Move rect so that p0=0,0 - keep window's coordinate system origin
271 * locked to top-left corner of the window.
272 */
273 offs = rect->p0;
274 gfx_rect_rtranslate(&offs, rect, &nrect);
275
276 /* dwindow can be NULL in case of unit tests */
277 if (window->dwindow != NULL) {
278 rc = display_window_resize(window->dwindow, &offs, &nrect);
279 if (rc != EOK)
280 return rc;
281 }
282
283 ui_wdecor_set_rect(window->wdecor, &nrect);
284 ui_wdecor_paint(window->wdecor);
285 return EOK;
286}
287
288/** Set window callbacks.
289 *
290 * @param window Window
291 * @param cb Window decoration callbacks
292 * @param arg Callback argument
293 */
294void ui_window_set_cb(ui_window_t *window, ui_window_cb_t *cb, void *arg)
295{
296 window->cb = cb;
297 window->arg = arg;
298}
299
300/** Get UI resource from window.
301 *
302 * @param window Window
303 * @return UI resource
304 */
305ui_resource_t *ui_window_get_res(ui_window_t *window)
306{
307 return window->res;
308}
309
310/** Get window GC.
311 *
312 * @param window Window
313 * @return GC (relative to window)
314 */
315gfx_context_t *ui_window_get_gc(ui_window_t *window)
316{
317 return window->gc;
318}
319
320/** Get window application area GC
321 *
322 * @param window Window
323 * @param rgc Place to store GC (relative to application area)
324 * @return EOK on success or an error code
325 */
326errno_t ui_window_get_app_gc(ui_window_t *window, gfx_context_t **rgc)
327{
328 gfx_bitmap_params_t params;
329 gfx_bitmap_alloc_t alloc;
330 gfx_rect_t rect;
331 mem_gc_t *memgc;
332 errno_t rc;
333
334 if (window->app_gc == NULL) {
335 assert(window->app_bmp == NULL);
336
337 gfx_bitmap_params_init(&params);
338
339 /*
340 * The bitmap will have the same dimensions as the
341 * application rectangle, but start at 0,0.
342 */
343 ui_window_get_app_rect(window, &rect);
344 gfx_rect_rtranslate(&rect.p0, &rect, &params.rect);
345
346 rc = gfx_bitmap_create(window->gc, &params, NULL,
347 &window->app_bmp);
348 if (rc != EOK)
349 return rc;
350
351 rc = gfx_bitmap_get_alloc(window->app_bmp, &alloc);
352 if (rc != EOK) {
353 gfx_bitmap_destroy(window->app_bmp);
354 return rc;
355 }
356
357 rc = mem_gc_create(&params.rect, &alloc, ui_window_app_update,
358 (void *) window, &memgc);
359 if (rc != EOK) {
360 gfx_bitmap_destroy(window->app_bmp);
361 return rc;
362 }
363
364 window->app_gc = mem_gc_get_ctx(memgc);
365 }
366
367 *rgc = window->app_gc;
368 return EOK;
369}
370
371/** Get window application rectangle
372 *
373 * @param window Window
374 * @param rect Place to store application rectangle
375 */
376void ui_window_get_app_rect(ui_window_t *window, gfx_rect_t *rect)
377{
378 ui_wdecor_geom_t geom;
379
380 ui_wdecor_get_geom(window->wdecor, &geom);
381 *rect = geom.app_area_rect;
382}
383
384/** Paint window
385 *
386 * @param window Window
387 * @return EOK on success or an error code
388 */
389errno_t ui_window_paint(ui_window_t *window)
390{
391 return ui_window_send_paint(window);
392}
393
394/** Handle window close event. */
395static void dwnd_close_event(void *arg)
396{
397 ui_window_t *window = (ui_window_t *) arg;
398
399 ui_window_send_close(window);
400}
401
402/** Handle window focus event. */
403static void dwnd_focus_event(void *arg)
404{
405 ui_window_t *window = (ui_window_t *) arg;
406
407 if (window->wdecor != NULL) {
408 ui_wdecor_set_active(window->wdecor, true);
409 ui_wdecor_paint(window->wdecor);
410 }
411
412 ui_window_send_focus(window);
413}
414
415/** Handle window keyboard event */
416static void dwnd_kbd_event(void *arg, kbd_event_t *kbd_event)
417{
418 ui_window_t *window = (ui_window_t *) arg;
419
420 (void) window;
421 ui_window_send_kbd(window, kbd_event);
422}
423
424/** Handle window position event */
425static void dwnd_pos_event(void *arg, pos_event_t *event)
426{
427 ui_window_t *window = (ui_window_t *) arg;
428
429 /* Make sure we don't process events until fully initialized */
430 if (window->wdecor == NULL)
431 return;
432
433 ui_wdecor_pos_event(window->wdecor, event);
434 ui_window_send_pos(window, event);
435}
436
437/** Handle window unfocus event. */
438static void dwnd_unfocus_event(void *arg)
439{
440 ui_window_t *window = (ui_window_t *) arg;
441
442 if (window->wdecor != NULL) {
443 ui_wdecor_set_active(window->wdecor, false);
444 ui_wdecor_paint(window->wdecor);
445 }
446
447 ui_window_send_unfocus(window);
448}
449
450/** Window decoration requested window closure.
451 *
452 * @param wdecor Window decoration
453 * @param arg Argument (demo)
454 */
455static void wd_close(ui_wdecor_t *wdecor, void *arg)
456{
457 ui_window_t *window = (ui_window_t *) arg;
458
459 ui_window_send_close(window);
460}
461
462/** Window decoration requested window move.
463 *
464 * @param wdecor Window decoration
465 * @param arg Argument (demo)
466 * @param pos Position where the title bar was pressed
467 */
468static void wd_move(ui_wdecor_t *wdecor, void *arg, gfx_coord2_t *pos)
469{
470 ui_window_t *window = (ui_window_t *) arg;
471
472 (void) display_window_move_req(window->dwindow, pos);
473}
474
475/** Send window close event.
476 *
477 * @param window Window
478 */
479void ui_window_send_close(ui_window_t *window)
480{
481 if (window->cb != NULL && window->cb->close != NULL)
482 window->cb->close(window, window->arg);
483}
484
485/** Send window focus event.
486 *
487 * @param window Window
488 */
489void ui_window_send_focus(ui_window_t *window)
490{
491 if (window->cb != NULL && window->cb->focus != NULL)
492 window->cb->focus(window, window->arg);
493}
494
495/** Send window keyboard event.
496 *
497 * @param window Window
498 */
499void ui_window_send_kbd(ui_window_t *window, kbd_event_t *kbd)
500{
501 if (window->cb != NULL && window->cb->kbd != NULL)
502 window->cb->kbd(window, window->arg, kbd);
503}
504
505/** Send window paint event.
506 *
507 * @param window Window
508 */
509errno_t ui_window_send_paint(ui_window_t *window)
510{
511 if (window->cb != NULL && window->cb->paint != NULL)
512 return window->cb->paint(window, window->arg);
513 else
514 return ui_window_def_paint(window);
515}
516
517/** Send window position event.
518 *
519 * @param window Window
520 */
521void ui_window_send_pos(ui_window_t *window, pos_event_t *pos)
522{
523 if (window->cb != NULL && window->cb->pos != NULL)
524 window->cb->pos(window, window->arg, pos);
525 else
526 ui_window_def_pos(window, pos);
527}
528
529/** Send window unfocus event.
530 *
531 * @param window Window
532 */
533void ui_window_send_unfocus(ui_window_t *window)
534{
535 if (window->cb != NULL && window->cb->unfocus != NULL)
536 window->cb->unfocus(window, window->arg);
537}
538
539/** Default window paint routine.
540 *
541 * @param window Window
542 * @return EOK on success or an error code
543 */
544errno_t ui_window_def_paint(ui_window_t *window)
545{
546 gfx_rect_t app_rect;
547 errno_t rc;
548
549 rc = gfx_set_color(window->gc, window->res->wnd_face_color);
550 if (rc != EOK)
551 return rc;
552
553 ui_window_get_app_rect(window, &app_rect);
554
555 rc = gfx_fill_rect(window->gc, &app_rect);
556 if (rc != EOK)
557 return rc;
558
559 if (window->control != NULL)
560 return ui_control_paint(window->control);
561
562 return EOK;
563}
564
565/** Default window position event routine.
566 *
567 * @param window Window
568 * @return EOK on success or an error code
569 */
570void ui_window_def_pos(ui_window_t *window, pos_event_t *pos)
571{
572 if (window->control != NULL)
573 ui_control_pos_event(window->control, pos);
574}
575
576/** Application area update callback
577 *
578 * @param arg Argument (ui_window_t *)
579 * @param rect Rectangle to update
580 */
581static void ui_window_app_update(void *arg, gfx_rect_t *rect)
582{
583 ui_window_t *window = (ui_window_t *) arg;
584 gfx_rect_t arect;
585
586 ui_window_get_app_rect(window, &arect);
587
588 /* Render bitmap rectangle inside the application area */
589 (void) gfx_bitmap_render(window->app_bmp, rect, &arect.p0);
590}
591
592/** @}
593 */
Note: See TracBrowser for help on using the repository browser.