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

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

Make fixed layout a UI control and hook it up to the window

  • Property mode set to 100644
File size: 10.0 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/context.h>
39#include <gfx/render.h>
40#include <io/kbd_event.h>
41#include <io/pos_event.h>
42#include <mem.h>
43#include <stdlib.h>
44#include <ui/control.h>
45#include <ui/resource.h>
46#include <ui/wdecor.h>
47#include <ui/window.h>
48#include "../private/control.h"
49#include "../private/dummygc.h"
50#include "../private/resource.h"
51#include "../private/ui.h"
52#include "../private/wdecor.h"
53#include "../private/window.h"
54
55static void dwnd_close_event(void *);
56static void dwnd_focus_event(void *);
57static void dwnd_kbd_event(void *, kbd_event_t *);
58static void dwnd_pos_event(void *, pos_event_t *);
59static void dwnd_unfocus_event(void *);
60
61static display_wnd_cb_t dwnd_cb = {
62 .close_event = dwnd_close_event,
63 .focus_event = dwnd_focus_event,
64 .kbd_event = dwnd_kbd_event,
65 .pos_event = dwnd_pos_event,
66 .unfocus_event = dwnd_unfocus_event
67};
68
69static void wd_close(ui_wdecor_t *, void *);
70static void wd_move(ui_wdecor_t *, void *, gfx_coord2_t *);
71
72static ui_wdecor_cb_t wdecor_cb = {
73 .close = wd_close,
74 .move = wd_move
75};
76
77/** Initialize window parameters structure.
78 *
79 * Window parameters structure must always be initialized using this function
80 * first.
81 *
82 * @param params Window parameters structure
83 */
84void ui_wnd_params_init(ui_wnd_params_t *params)
85{
86 memset(params, 0, sizeof(ui_wnd_params_t));
87}
88
89/** Create new window.
90 *
91 * @param ui User interface
92 * @param params Window parameters
93 * @param rwindow Place to store pointer to new window
94 * @return EOK on success or an error code
95 */
96errno_t ui_window_create(ui_t *ui, ui_wnd_params_t *params,
97 ui_window_t **rwindow)
98{
99 ui_window_t *window;
100 display_wnd_params_t dparams;
101 display_window_t *dwindow = NULL;
102 gfx_context_t *gc = NULL;
103 ui_resource_t *res = NULL;
104 ui_wdecor_t *wdecor = NULL;
105 dummy_gc_t *dgc = NULL;
106 errno_t rc;
107
108 window = calloc(1, sizeof(ui_window_t));
109 if (window == NULL)
110 return ENOMEM;
111
112 display_wnd_params_init(&dparams);
113 dparams.rect = params->rect;
114
115 if (ui->display != NULL) {
116 rc = display_window_create(ui->display, &dparams, &dwnd_cb,
117 (void *) window, &dwindow);
118 if (rc != EOK)
119 goto error;
120
121 rc = display_window_get_gc(dwindow, &gc);
122 if (rc != EOK)
123 goto error;
124 } else {
125 /* Needed for unit tests */
126 rc = dummygc_create(&dgc);
127 if (rc != EOK)
128 goto error;
129
130 gc = dummygc_get_ctx(dgc);
131 }
132
133 rc = ui_resource_create(gc, &res);
134 if (rc != EOK)
135 goto error;
136
137 rc = ui_wdecor_create(res, params->caption, &wdecor);
138 if (rc != EOK)
139 goto error;
140
141 ui_wdecor_set_rect(wdecor, &params->rect);
142 ui_wdecor_set_cb(wdecor, &wdecor_cb, (void *) window);
143 ui_wdecor_paint(wdecor);
144
145 window->ui = ui;
146 window->dwindow = dwindow;
147 window->gc = gc;
148 window->res = res;
149 window->wdecor = wdecor;
150 *rwindow = window;
151 return EOK;
152error:
153 if (wdecor != NULL)
154 ui_wdecor_destroy(wdecor);
155 if (res != NULL)
156 ui_resource_destroy(res);
157 if (dgc != NULL)
158 dummygc_destroy(dgc);
159 if (dwindow != NULL)
160 display_window_destroy(dwindow);
161 free(window);
162 return rc;
163}
164
165/** Destroy window.
166 *
167 * @param window Window or @c NULL
168 */
169void ui_window_destroy(ui_window_t *window)
170{
171 if (window == NULL)
172 return;
173
174 ui_control_destroy(window->control);
175 ui_wdecor_destroy(window->wdecor);
176 ui_resource_destroy(window->res);
177 gfx_context_delete(window->gc);
178 display_window_destroy(window->dwindow);
179 free(window);
180}
181
182/** Add control to window.
183 *
184 * Only one control can be added to a window. If more than one control
185 * is added, the results are undefined.
186 *
187 * @param fixed Fixed layout
188 * @param control Control
189 * @return EOK on success, ENOMEM if out of memory
190 */
191void ui_window_add(ui_window_t *window, ui_control_t *control)
192{
193 assert(window->control == NULL);
194
195 window->control = control;
196 control->elemp = (void *) window;
197}
198
199/** Remove control from window.
200 *
201 * @param window Window
202 * @param control Control
203 */
204void ui_window_remove(ui_window_t *window, ui_control_t *control)
205{
206 assert(window->control == control);
207 assert((ui_window_t *) control->elemp == window);
208
209 window->control = NULL;
210 control->elemp = NULL;
211}
212
213/** Set window callbacks.
214 *
215 * @param window Window
216 * @param cb Window decoration callbacks
217 * @param arg Callback argument
218 */
219void ui_window_set_cb(ui_window_t *window, ui_window_cb_t *cb, void *arg)
220{
221 window->cb = cb;
222 window->arg = arg;
223}
224
225ui_resource_t *ui_window_get_res(ui_window_t *window)
226{
227 return window->res;
228}
229
230gfx_context_t *ui_window_get_gc(ui_window_t *window)
231{
232 return window->gc;
233}
234
235void ui_window_get_app_rect(ui_window_t *window, gfx_rect_t *rect)
236{
237 ui_wdecor_geom_t geom;
238
239 ui_wdecor_get_geom(window->wdecor, &geom);
240 *rect = geom.app_area_rect;
241}
242
243errno_t ui_window_paint(ui_window_t *window)
244{
245 return ui_window_send_paint(window);
246}
247
248/** Handle window close event. */
249static void dwnd_close_event(void *arg)
250{
251 ui_window_t *window = (ui_window_t *) arg;
252
253 ui_window_send_close(window);
254}
255
256/** Handle window focus event. */
257static void dwnd_focus_event(void *arg)
258{
259 ui_window_t *window = (ui_window_t *) arg;
260
261 if (window->wdecor != NULL) {
262 ui_wdecor_set_active(window->wdecor, true);
263 ui_wdecor_paint(window->wdecor);
264 }
265
266 ui_window_send_focus(window);
267}
268
269/** Handle window keyboard event */
270static void dwnd_kbd_event(void *arg, kbd_event_t *kbd_event)
271{
272 ui_window_t *window = (ui_window_t *) arg;
273
274 (void) window;
275 ui_window_send_kbd(window, kbd_event);
276}
277
278/** Handle window position event */
279static void dwnd_pos_event(void *arg, pos_event_t *event)
280{
281 ui_window_t *window = (ui_window_t *) arg;
282
283 /* Make sure we don't process events until fully initialized */
284 if (window->wdecor == NULL)
285 return;
286
287 ui_wdecor_pos_event(window->wdecor, event);
288 ui_window_send_pos(window, event);
289}
290
291/** Handle window unfocus event. */
292static void dwnd_unfocus_event(void *arg)
293{
294 ui_window_t *window = (ui_window_t *) arg;
295
296 if (window->wdecor != NULL) {
297 ui_wdecor_set_active(window->wdecor, false);
298 ui_wdecor_paint(window->wdecor);
299 }
300
301 ui_window_send_unfocus(window);
302}
303
304/** Window decoration requested window closure.
305 *
306 * @param wdecor Window decoration
307 * @param arg Argument (demo)
308 */
309static void wd_close(ui_wdecor_t *wdecor, void *arg)
310{
311 ui_window_t *window = (ui_window_t *) arg;
312
313 ui_window_send_close(window);
314}
315
316/** Window decoration requested window move.
317 *
318 * @param wdecor Window decoration
319 * @param arg Argument (demo)
320 * @param pos Position where the title bar was pressed
321 */
322static void wd_move(ui_wdecor_t *wdecor, void *arg, gfx_coord2_t *pos)
323{
324 ui_window_t *window = (ui_window_t *) arg;
325
326 (void) display_window_move_req(window->dwindow, pos);
327}
328
329/** Send window close event.
330 *
331 * @param window Window
332 */
333void ui_window_send_close(ui_window_t *window)
334{
335 if (window->cb != NULL && window->cb->close != NULL)
336 window->cb->close(window, window->arg);
337}
338
339/** Send window focus event.
340 *
341 * @param window Window
342 */
343void ui_window_send_focus(ui_window_t *window)
344{
345 if (window->cb != NULL && window->cb->focus != NULL)
346 window->cb->focus(window, window->arg);
347}
348
349/** Send window keyboard event.
350 *
351 * @param window Window
352 */
353void ui_window_send_kbd(ui_window_t *window, kbd_event_t *kbd)
354{
355 if (window->cb != NULL && window->cb->kbd != NULL)
356 window->cb->kbd(window, window->arg, kbd);
357}
358
359/** Send window paint event.
360 *
361 * @param window Window
362 */
363errno_t ui_window_send_paint(ui_window_t *window)
364{
365 if (window->cb != NULL && window->cb->paint != NULL)
366 return window->cb->paint(window, window->arg);
367 else
368 return ui_window_def_paint(window);
369}
370
371/** Send window position event.
372 *
373 * @param window Window
374 */
375void ui_window_send_pos(ui_window_t *window, pos_event_t *pos)
376{
377 if (window->cb != NULL && window->cb->pos != NULL)
378 window->cb->pos(window, window->arg, pos);
379 else
380 ui_window_def_pos(window, pos);
381}
382
383/** Send window unfocus event.
384 *
385 * @param window Window
386 */
387void ui_window_send_unfocus(ui_window_t *window)
388{
389 if (window->cb != NULL && window->cb->unfocus != NULL)
390 window->cb->unfocus(window, window->arg);
391}
392
393/** Default window paint routine.
394 *
395 * @param window Window
396 * @return EOK on success or an error code
397 */
398errno_t ui_window_def_paint(ui_window_t *window)
399{
400 gfx_rect_t app_rect;
401 errno_t rc;
402
403 rc = gfx_set_color(window->gc, window->res->wnd_face_color);
404 if (rc != EOK)
405 return rc;
406
407 ui_window_get_app_rect(window, &app_rect);
408
409 rc = gfx_fill_rect(window->gc, &app_rect);
410 if (rc != EOK)
411 return rc;
412
413 if (window->control != NULL)
414 return ui_control_paint(window->control);
415
416 return EOK;
417}
418
419/** Default window position event routine.
420 *
421 * @param window Window
422 * @return EOK on success or an error code
423 */
424void ui_window_def_pos(ui_window_t *window, pos_event_t *pos)
425{
426 if (window->control != NULL)
427 ui_control_pos_event(window->control, pos);
428}
429
430/** @}
431 */
Note: See TracBrowser for help on using the repository browser.