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 <mem.h>
|
---|
40 | #include <stdlib.h>
|
---|
41 | #include <ui/resource.h>
|
---|
42 | #include <ui/wdecor.h>
|
---|
43 | #include <ui/window.h>
|
---|
44 | #include "../private/dummygc.h"
|
---|
45 | #include "../private/ui.h"
|
---|
46 | #include "../private/window.h"
|
---|
47 |
|
---|
48 | /** Initialize window parameters structure.
|
---|
49 | *
|
---|
50 | * Window parameters structure must always be initialized using this function
|
---|
51 | * first.
|
---|
52 | *
|
---|
53 | * @param params Window parameters structure
|
---|
54 | */
|
---|
55 | void ui_window_params_init(ui_window_params_t *params)
|
---|
56 | {
|
---|
57 | memset(params, 0, sizeof(ui_window_params_t));
|
---|
58 | }
|
---|
59 |
|
---|
60 | /** Create new window.
|
---|
61 | *
|
---|
62 | * @param ui User interface
|
---|
63 | * @param params Window parameters
|
---|
64 | * @param rwindow Place to store pointer to new window
|
---|
65 | * @return EOK on success or an error code
|
---|
66 | */
|
---|
67 | errno_t ui_window_create(ui_t *ui, ui_window_params_t *params,
|
---|
68 | ui_window_t **rwindow)
|
---|
69 | {
|
---|
70 | ui_window_t *window;
|
---|
71 | display_wnd_params_t dparams;
|
---|
72 | display_window_t *dwindow = NULL;
|
---|
73 | gfx_context_t *gc = NULL;
|
---|
74 | ui_resource_t *res = NULL;
|
---|
75 | ui_wdecor_t *wdecor = NULL;
|
---|
76 | dummy_gc_t *dgc = NULL;
|
---|
77 | errno_t rc;
|
---|
78 |
|
---|
79 | window = calloc(1, sizeof(ui_window_t));
|
---|
80 | if (window == NULL)
|
---|
81 | return ENOMEM;
|
---|
82 |
|
---|
83 | display_wnd_params_init(&dparams);
|
---|
84 |
|
---|
85 | if (ui->display != NULL) {
|
---|
86 | rc = display_window_create(ui->display, &dparams, NULL, NULL,
|
---|
87 | &dwindow);
|
---|
88 | if (rc != EOK)
|
---|
89 | goto error;
|
---|
90 |
|
---|
91 | rc = display_window_get_gc(dwindow, &gc);
|
---|
92 | if (rc != EOK)
|
---|
93 | goto error;
|
---|
94 | } else {
|
---|
95 | /* Needed for unit tests */
|
---|
96 | rc = dummygc_create(&dgc);
|
---|
97 | if (rc != EOK)
|
---|
98 | goto error;
|
---|
99 |
|
---|
100 | gc = dummygc_get_ctx(dgc);
|
---|
101 | }
|
---|
102 |
|
---|
103 | rc = ui_resource_create(gc, &res);
|
---|
104 | if (rc != EOK)
|
---|
105 | goto error;
|
---|
106 |
|
---|
107 | rc = ui_wdecor_create(res, params->caption, &wdecor);
|
---|
108 | if (rc != EOK)
|
---|
109 | goto error;
|
---|
110 |
|
---|
111 | window->ui = ui;
|
---|
112 | window->dwindow = dwindow;
|
---|
113 | window->gc = gc;
|
---|
114 | window->res = res;
|
---|
115 | window->wdecor = wdecor;
|
---|
116 | *rwindow = window;
|
---|
117 | return EOK;
|
---|
118 | error:
|
---|
119 | if (wdecor != NULL)
|
---|
120 | ui_wdecor_destroy(wdecor);
|
---|
121 | if (res != NULL)
|
---|
122 | ui_resource_destroy(res);
|
---|
123 | if (dgc != NULL)
|
---|
124 | dummygc_destroy(dgc);
|
---|
125 | if (dwindow != NULL)
|
---|
126 | display_window_destroy(dwindow);
|
---|
127 | free(window);
|
---|
128 | return rc;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /** Destroy window.
|
---|
132 | *
|
---|
133 | * @param window Window or @c NULL
|
---|
134 | */
|
---|
135 | void ui_window_destroy(ui_window_t *window)
|
---|
136 | {
|
---|
137 | if (window == NULL)
|
---|
138 | return;
|
---|
139 |
|
---|
140 | ui_wdecor_destroy(window->wdecor);
|
---|
141 | ui_resource_destroy(window->res);
|
---|
142 | gfx_context_delete(window->gc);
|
---|
143 | display_window_destroy(window->dwindow);
|
---|
144 | free(window);
|
---|
145 | }
|
---|
146 |
|
---|
147 | /** @}
|
---|
148 | */
|
---|