source: mainline/uspace/lib/ui/src/popup.c@ b4b4dafe

Last change on this file since b4b4dafe was b4b4dafe, checked in by Jiri Svoboda <jiri@…>, 4 years ago

Popup windows event delivery is special

Popup windows don't get focus, yet they still receive events.

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