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 Popup window
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <errno.h>
|
---|
37 | #include <gfx/context.h>
|
---|
38 | //#include <io/kbd_event.h>
|
---|
39 | #include <io/pos_event.h>
|
---|
40 | #include <mem.h>
|
---|
41 | #include <stdlib.h>
|
---|
42 | #include <ui/control.h>
|
---|
43 | #include <ui/popup.h>
|
---|
44 | #include <ui/ui.h>
|
---|
45 | #include <ui/window.h>
|
---|
46 | #include "../private/popup.h"
|
---|
47 |
|
---|
48 | static void ui_popup_window_close(ui_window_t *, void *);
|
---|
49 | static void ui_popup_window_pos(ui_window_t *, void *, pos_event_t *);
|
---|
50 |
|
---|
51 | static ui_window_cb_t ui_popup_window_cb = {
|
---|
52 | .close = ui_popup_window_close,
|
---|
53 | .pos = ui_popup_window_pos
|
---|
54 | };
|
---|
55 |
|
---|
56 | /** Initialize popup parameters structure.
|
---|
57 | *
|
---|
58 | * Popup parameters structure must always be initialized using this function
|
---|
59 | * first.
|
---|
60 | *
|
---|
61 | * @param params Popup parameters structure
|
---|
62 | */
|
---|
63 | void ui_popup_params_init(ui_popup_params_t *params)
|
---|
64 | {
|
---|
65 | memset(params, 0, sizeof(ui_popup_params_t));
|
---|
66 | }
|
---|
67 |
|
---|
68 | /** Create new popup window.
|
---|
69 | *
|
---|
70 | * @param ui User interface
|
---|
71 | * @param parent Parent window
|
---|
72 | * @param params Popup parameters
|
---|
73 | * @param rpopup Place to store pointer to new popup window
|
---|
74 | * @return EOK on success or an error code
|
---|
75 | */
|
---|
76 | errno_t ui_popup_create(ui_t *ui, ui_window_t *parent,
|
---|
77 | ui_popup_params_t *params, ui_popup_t **rpopup)
|
---|
78 | {
|
---|
79 | ui_popup_t *popup;
|
---|
80 | ui_window_t *window = NULL;
|
---|
81 | ui_wnd_params_t wparams;
|
---|
82 | gfx_coord2_t parent_pos;
|
---|
83 | errno_t rc;
|
---|
84 |
|
---|
85 | popup = calloc(1, sizeof(ui_popup_t));
|
---|
86 | if (popup == NULL)
|
---|
87 | return ENOMEM;
|
---|
88 |
|
---|
89 | rc = ui_window_get_pos(parent, &parent_pos);
|
---|
90 | if (rc != EOK)
|
---|
91 | goto error;
|
---|
92 |
|
---|
93 | ui_wnd_params_init(&wparams);
|
---|
94 | wparams.rect = params->rect;
|
---|
95 | wparams.caption = "";
|
---|
96 | wparams.style &= ~ui_wds_decorated;
|
---|
97 | wparams.placement = ui_wnd_place_popup;
|
---|
98 | wparams.flags |= ui_wndf_popup;
|
---|
99 |
|
---|
100 | /* Compute position of parent rectangle relative to the screen */
|
---|
101 | gfx_rect_translate(&parent_pos, ¶ms->place, &wparams.prect);
|
---|
102 |
|
---|
103 | rc = ui_window_create(ui, &wparams, &window);
|
---|
104 | if (rc != EOK)
|
---|
105 | goto error;
|
---|
106 |
|
---|
107 | popup->ui = ui;
|
---|
108 | popup->parent = parent;
|
---|
109 | popup->window = window;
|
---|
110 |
|
---|
111 | ui_window_set_cb(window, &ui_popup_window_cb, popup);
|
---|
112 |
|
---|
113 | *rpopup = popup;
|
---|
114 | return EOK;
|
---|
115 | error:
|
---|
116 | free(popup);
|
---|
117 | return rc;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /** Destroy popup window.
|
---|
121 | *
|
---|
122 | * @param popup Popup window or @c NULL
|
---|
123 | */
|
---|
124 | void ui_popup_destroy(ui_popup_t *popup)
|
---|
125 | {
|
---|
126 | if (popup == NULL)
|
---|
127 | return;
|
---|
128 |
|
---|
129 | ui_window_destroy(popup->window);
|
---|
130 | free(popup);
|
---|
131 | }
|
---|
132 |
|
---|
133 | /** Add control to popup window.
|
---|
134 | *
|
---|
135 | * Only one control can be added to a popup window. If more than one control
|
---|
136 | * is added, the results are undefined.
|
---|
137 | *
|
---|
138 | * @param popup Popup window
|
---|
139 | * @param control Control
|
---|
140 | * @return EOK on success, ENOMEM if out of memory
|
---|
141 | */
|
---|
142 | void ui_popup_add(ui_popup_t *popup, ui_control_t *control)
|
---|
143 | {
|
---|
144 | ui_window_add(popup->window, control);
|
---|
145 | }
|
---|
146 |
|
---|
147 | /** Remove control from popup window.
|
---|
148 | *
|
---|
149 | * @param popup Popup window
|
---|
150 | * @param control Control
|
---|
151 | */
|
---|
152 | void ui_popup_remove(ui_popup_t *popup, ui_control_t *control)
|
---|
153 | {
|
---|
154 | ui_window_remove(popup->window, control);
|
---|
155 | }
|
---|
156 |
|
---|
157 | /** Set popup window callbacks.
|
---|
158 | *
|
---|
159 | * @param popup Popup window
|
---|
160 | * @param cb Popup window callbacks
|
---|
161 | * @param arg Callback argument
|
---|
162 | */
|
---|
163 | void ui_popup_set_cb(ui_popup_t *popup, ui_popup_cb_t *cb, void *arg)
|
---|
164 | {
|
---|
165 | popup->cb = cb;
|
---|
166 | popup->arg = arg;
|
---|
167 | }
|
---|
168 |
|
---|
169 | /** Get UI resource from popup window.
|
---|
170 | *
|
---|
171 | * @param window Window
|
---|
172 | * @return UI resource
|
---|
173 | */
|
---|
174 | ui_resource_t *ui_popup_get_res(ui_popup_t *popup)
|
---|
175 | {
|
---|
176 | return ui_window_get_res(popup->window);
|
---|
177 | }
|
---|
178 |
|
---|
179 | /** Get popup window GC.
|
---|
180 | *
|
---|
181 | * @param popup Popup window
|
---|
182 | * @return GC (relative to popup window)
|
---|
183 | */
|
---|
184 | gfx_context_t *ui_popup_get_gc(ui_popup_t *popup)
|
---|
185 | {
|
---|
186 | return ui_window_get_gc(popup->window);
|
---|
187 | }
|
---|
188 |
|
---|
189 | /** Handle close event in popup window.
|
---|
190 | *
|
---|
191 | * @param window Window
|
---|
192 | * @param arg Argument (ui_popup_t *)
|
---|
193 | */
|
---|
194 | static void ui_popup_window_close(ui_window_t *window, void *arg)
|
---|
195 | {
|
---|
196 | ui_popup_t *popup = (ui_popup_t *)arg;
|
---|
197 |
|
---|
198 | if (popup->cb != NULL && popup->cb->close != NULL)
|
---|
199 | popup->cb->close(popup, popup->arg);
|
---|
200 | }
|
---|
201 |
|
---|
202 | /** Handle position event in popup window.
|
---|
203 | *
|
---|
204 | * @param window Window
|
---|
205 | * @param arg Argument (ui_popup_t *)
|
---|
206 | * @param event Position event
|
---|
207 | */
|
---|
208 | static void ui_popup_window_pos(ui_window_t *window, void *arg,
|
---|
209 | pos_event_t *event)
|
---|
210 | {
|
---|
211 | ui_popup_t *popup = (ui_popup_t *)arg;
|
---|
212 |
|
---|
213 | if (popup->cb != NULL && popup->cb->pos != NULL)
|
---|
214 | popup->cb->pos(popup, popup->arg, event);
|
---|
215 | }
|
---|
216 |
|
---|
217 | /** @}
|
---|
218 | */
|
---|