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

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

Specify parent window when creating popup

This will be used in conjunction with ui_wnd_popup_params_t.place
(a rectangle relative to the parent window) to determine where on
the screen the popup window should appear.

  • Property mode set to 100644
File size: 4.8 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 errno_t rc;
81
82 popup = calloc(1, sizeof(ui_popup_t));
83 if (popup == NULL)
84 return ENOMEM;
85
86 ui_wnd_params_init(&wparams);
87 wparams.rect = params->rect;
88 wparams.caption = "";
89 wparams.style &= ~ui_wds_decorated;
90
91 rc = ui_window_create(ui, &wparams, &window);
92 if (rc != EOK)
93 goto error;
94
95 popup->ui = ui;
96 popup->parent = parent;
97 popup->window = window;
98
99 ui_window_set_cb(window, &ui_popup_window_cb, popup);
100
101 *rpopup = popup;
102 return EOK;
103error:
104 free(popup);
105 return rc;
106}
107
108/** Destroy popup window.
109 *
110 * @param popup Popup window or @c NULL
111 */
112void ui_popup_destroy(ui_popup_t *popup)
113{
114 if (popup == NULL)
115 return;
116
117 ui_window_destroy(popup->window);
118 free(popup);
119}
120
121/** Add control to popup window.
122 *
123 * Only one control can be added to a popup window. If more than one control
124 * is added, the results are undefined.
125 *
126 * @param popup Popup window
127 * @param control Control
128 * @return EOK on success, ENOMEM if out of memory
129 */
130void ui_popup_add(ui_popup_t *popup, ui_control_t *control)
131{
132 ui_window_add(popup->window, control);
133}
134
135/** Remove control from popup window.
136 *
137 * @param popup Popup window
138 * @param control Control
139 */
140void ui_popup_remove(ui_popup_t *popup, ui_control_t *control)
141{
142 ui_window_remove(popup->window, control);
143}
144
145/** Set popup window callbacks.
146 *
147 * @param popup Popup window
148 * @param cb Popup window callbacks
149 * @param arg Callback argument
150 */
151void ui_popup_set_cb(ui_popup_t *popup, ui_popup_cb_t *cb, void *arg)
152{
153 popup->cb = cb;
154 popup->arg = arg;
155}
156
157/** Get UI resource from popup window.
158 *
159 * @param window Window
160 * @return UI resource
161 */
162ui_resource_t *ui_popup_get_res(ui_popup_t *popup)
163{
164 return ui_window_get_res(popup->window);
165}
166
167/** Get popup window GC.
168 *
169 * @param popup Popup window
170 * @return GC (relative to popup window)
171 */
172gfx_context_t *ui_popup_get_gc(ui_popup_t *popup)
173{
174 return ui_window_get_gc(popup->window);
175}
176
177/** Handle position event in popup window.
178 *
179 * @param window Window
180 * @param arg Argument (ui_popup_t *)
181 * @param event Position event
182 */
183static void ui_popup_window_pos(ui_window_t *window, void *arg,
184 pos_event_t *event)
185{
186 ui_popup_t *popup = (ui_popup_t *)arg;
187
188 if (popup->cb != NULL && popup->cb->pos != NULL)
189 popup->cb->pos(popup, popup->arg, event);
190}
191
192/** @}
193 */
Note: See TracBrowser for help on using the repository browser.