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