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