[214aefb] | 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 Menu bar
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <adt/list.h>
|
---|
| 37 | #include <errno.h>
|
---|
| 38 | #include <gfx/color.h>
|
---|
| 39 | #include <gfx/context.h>
|
---|
| 40 | #include <gfx/render.h>
|
---|
| 41 | #include <gfx/text.h>
|
---|
| 42 | #include <io/pos_event.h>
|
---|
| 43 | #include <stdlib.h>
|
---|
| 44 | #include <str.h>
|
---|
| 45 | #include <ui/control.h>
|
---|
| 46 | #include <ui/paint.h>
|
---|
| 47 | #include <ui/menu.h>
|
---|
| 48 | #include <ui/menubar.h>
|
---|
[c68c18b9] | 49 | #include <ui/window.h>
|
---|
[214aefb] | 50 | #include "../private/menubar.h"
|
---|
| 51 | #include "../private/resource.h"
|
---|
| 52 |
|
---|
| 53 | enum {
|
---|
| 54 | menubar_hpad = 4,
|
---|
| 55 | menubar_vpad = 4,
|
---|
| 56 | menubar_hpad_text = 1,
|
---|
| 57 | menubar_vpad_text = 0
|
---|
| 58 | };
|
---|
| 59 |
|
---|
| 60 | static void ui_menu_bar_ctl_destroy(void *);
|
---|
| 61 | static errno_t ui_menu_bar_ctl_paint(void *);
|
---|
| 62 | static ui_evclaim_t ui_menu_bar_ctl_pos_event(void *, pos_event_t *);
|
---|
[62223ec] | 63 | static void ui_menu_bar_ctl_unfocus(void *);
|
---|
[214aefb] | 64 |
|
---|
| 65 | /** Menu bar control ops */
|
---|
| 66 | ui_control_ops_t ui_menu_bar_ops = {
|
---|
| 67 | .destroy = ui_menu_bar_ctl_destroy,
|
---|
| 68 | .paint = ui_menu_bar_ctl_paint,
|
---|
[62223ec] | 69 | .pos_event = ui_menu_bar_ctl_pos_event,
|
---|
| 70 | .unfocus = ui_menu_bar_ctl_unfocus
|
---|
[214aefb] | 71 | };
|
---|
| 72 |
|
---|
| 73 | /** Create new menu bar.
|
---|
| 74 | *
|
---|
[3c8c580] | 75 | * @param ui UI
|
---|
[c68c18b9] | 76 | * @param window Window that will contain the menu bar
|
---|
[214aefb] | 77 | * @param rmbar Place to store pointer to new menu bar
|
---|
| 78 | * @return EOK on success, ENOMEM if out of memory
|
---|
| 79 | */
|
---|
[c68c18b9] | 80 | errno_t ui_menu_bar_create(ui_t *ui, ui_window_t *window, ui_menu_bar_t **rmbar)
|
---|
[214aefb] | 81 | {
|
---|
| 82 | ui_menu_bar_t *mbar;
|
---|
| 83 | errno_t rc;
|
---|
| 84 |
|
---|
| 85 | mbar = calloc(1, sizeof(ui_menu_bar_t));
|
---|
| 86 | if (mbar == NULL)
|
---|
| 87 | return ENOMEM;
|
---|
| 88 |
|
---|
| 89 | rc = ui_control_new(&ui_menu_bar_ops, (void *) mbar, &mbar->control);
|
---|
| 90 | if (rc != EOK) {
|
---|
| 91 | free(mbar);
|
---|
| 92 | return rc;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[3c8c580] | 95 | mbar->ui = ui;
|
---|
[c68c18b9] | 96 | mbar->window = window;
|
---|
[214aefb] | 97 | list_initialize(&mbar->menus);
|
---|
| 98 | *rmbar = mbar;
|
---|
| 99 | return EOK;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | /** Destroy menu bar
|
---|
| 103 | *
|
---|
| 104 | * @param mbar Menu bar or @c NULL
|
---|
| 105 | */
|
---|
| 106 | void ui_menu_bar_destroy(ui_menu_bar_t *mbar)
|
---|
| 107 | {
|
---|
| 108 | ui_menu_t *menu;
|
---|
| 109 |
|
---|
| 110 | if (mbar == NULL)
|
---|
| 111 | return;
|
---|
| 112 |
|
---|
| 113 | /* Destroy menus */
|
---|
| 114 | menu = ui_menu_first(mbar);
|
---|
| 115 | while (menu != NULL) {
|
---|
| 116 | ui_menu_destroy(menu);
|
---|
| 117 | menu = ui_menu_first(mbar);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | ui_control_delete(mbar->control);
|
---|
| 121 | free(mbar);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | /** Get base control from menu bar.
|
---|
| 125 | *
|
---|
| 126 | * @param mbar Menu bar
|
---|
| 127 | * @return Control
|
---|
| 128 | */
|
---|
| 129 | ui_control_t *ui_menu_bar_ctl(ui_menu_bar_t *mbar)
|
---|
| 130 | {
|
---|
| 131 | return mbar->control;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | /** Set menu bar rectangle.
|
---|
| 135 | *
|
---|
| 136 | * @param mbar Menu bar
|
---|
| 137 | * @param rect New menu bar rectangle
|
---|
| 138 | */
|
---|
| 139 | void ui_menu_bar_set_rect(ui_menu_bar_t *mbar, gfx_rect_t *rect)
|
---|
| 140 | {
|
---|
| 141 | mbar->rect = *rect;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | /** Paint menu bar.
|
---|
| 145 | *
|
---|
| 146 | * @param mbar Menu bar
|
---|
| 147 | * @return EOK on success or an error code
|
---|
| 148 | */
|
---|
| 149 | errno_t ui_menu_bar_paint(ui_menu_bar_t *mbar)
|
---|
| 150 | {
|
---|
[c68c18b9] | 151 | ui_resource_t *res;
|
---|
[214aefb] | 152 | gfx_text_fmt_t fmt;
|
---|
| 153 | gfx_coord2_t pos;
|
---|
| 154 | gfx_coord2_t tpos;
|
---|
| 155 | gfx_rect_t rect;
|
---|
| 156 | gfx_color_t *bg_color;
|
---|
| 157 | ui_menu_t *menu;
|
---|
| 158 | const char *caption;
|
---|
| 159 | gfx_coord_t width;
|
---|
| 160 | gfx_coord_t hpad;
|
---|
| 161 | gfx_coord_t vpad;
|
---|
| 162 | errno_t rc;
|
---|
| 163 |
|
---|
[c68c18b9] | 164 | res = ui_window_get_res(mbar->window);
|
---|
| 165 |
|
---|
[214aefb] | 166 | /* Paint menu bar background */
|
---|
| 167 |
|
---|
[c68c18b9] | 168 | rc = gfx_set_color(res->gc, res->wnd_face_color);
|
---|
[214aefb] | 169 | if (rc != EOK)
|
---|
| 170 | goto error;
|
---|
| 171 |
|
---|
[c68c18b9] | 172 | rc = gfx_fill_rect(res->gc, &mbar->rect);
|
---|
[214aefb] | 173 | if (rc != EOK)
|
---|
| 174 | goto error;
|
---|
| 175 |
|
---|
[c68c18b9] | 176 | if (res->textmode) {
|
---|
[214aefb] | 177 | hpad = menubar_hpad_text;
|
---|
| 178 | vpad = menubar_vpad_text;
|
---|
| 179 | } else {
|
---|
| 180 | hpad = menubar_hpad;
|
---|
| 181 | vpad = menubar_vpad;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | pos = mbar->rect.p0;
|
---|
| 185 |
|
---|
| 186 | gfx_text_fmt_init(&fmt);
|
---|
| 187 | fmt.halign = gfx_halign_left;
|
---|
| 188 | fmt.valign = gfx_valign_top;
|
---|
| 189 |
|
---|
| 190 | menu = ui_menu_first(mbar);
|
---|
| 191 | while (menu != NULL) {
|
---|
| 192 | caption = ui_menu_caption(menu);
|
---|
[c68c18b9] | 193 | width = gfx_text_width(res->font, caption) + 2 * hpad;
|
---|
[214aefb] | 194 | tpos.x = pos.x + hpad;
|
---|
| 195 | tpos.y = pos.y + vpad;
|
---|
| 196 |
|
---|
| 197 | rect.p0 = pos;
|
---|
| 198 | rect.p1.x = rect.p0.x + width;
|
---|
| 199 | rect.p1.y = mbar->rect.p1.y;
|
---|
| 200 |
|
---|
| 201 | if (menu == mbar->selected) {
|
---|
[c68c18b9] | 202 | fmt.color = res->wnd_sel_text_color;
|
---|
| 203 | bg_color = res->wnd_sel_text_bg_color;
|
---|
[214aefb] | 204 | } else {
|
---|
[c68c18b9] | 205 | fmt.color = res->wnd_text_color;
|
---|
| 206 | bg_color = res->wnd_face_color;
|
---|
[214aefb] | 207 | }
|
---|
| 208 |
|
---|
[c68c18b9] | 209 | rc = gfx_set_color(res->gc, bg_color);
|
---|
[214aefb] | 210 | if (rc != EOK)
|
---|
| 211 | goto error;
|
---|
| 212 |
|
---|
[c68c18b9] | 213 | rc = gfx_fill_rect(res->gc, &rect);
|
---|
[214aefb] | 214 | if (rc != EOK)
|
---|
| 215 | goto error;
|
---|
| 216 |
|
---|
[c68c18b9] | 217 | rc = gfx_puttext(res->font, &tpos, &fmt, caption);
|
---|
[214aefb] | 218 | if (rc != EOK)
|
---|
| 219 | goto error;
|
---|
| 220 |
|
---|
| 221 | pos.x += width;
|
---|
| 222 | menu = ui_menu_next(menu);
|
---|
| 223 | }
|
---|
| 224 |
|
---|
[c68c18b9] | 225 | rc = gfx_update(res->gc);
|
---|
[214aefb] | 226 | if (rc != EOK)
|
---|
| 227 | goto error;
|
---|
| 228 |
|
---|
| 229 | return EOK;
|
---|
| 230 | error:
|
---|
| 231 | return rc;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | /** Select or deselect menu from menu bar.
|
---|
| 235 | *
|
---|
| 236 | * Select @a menu. If @a menu is @c NULL or it is already selected,
|
---|
| 237 | * then select none.
|
---|
| 238 | *
|
---|
| 239 | * @param mbar Menu bar
|
---|
[3c8c580] | 240 | * @param rect Menu bar entry rectangle
|
---|
[214aefb] | 241 | * @param menu Menu to select (or deselect if selected) or @c NULL
|
---|
| 242 | */
|
---|
[3c8c580] | 243 | void ui_menu_bar_select(ui_menu_bar_t *mbar, gfx_rect_t *rect,
|
---|
[214aefb] | 244 | ui_menu_t *menu)
|
---|
| 245 | {
|
---|
| 246 | ui_menu_t *old_menu;
|
---|
| 247 |
|
---|
| 248 | old_menu = mbar->selected;
|
---|
| 249 |
|
---|
| 250 | if (mbar->selected != menu)
|
---|
| 251 | mbar->selected = menu;
|
---|
| 252 | else
|
---|
| 253 | mbar->selected = NULL;
|
---|
| 254 |
|
---|
[3c8c580] | 255 | /* Close previously open menu */
|
---|
[214aefb] | 256 | if (old_menu != NULL)
|
---|
[3c8c580] | 257 | (void) ui_menu_close(old_menu);
|
---|
[214aefb] | 258 |
|
---|
| 259 | (void) ui_menu_bar_paint(mbar);
|
---|
| 260 |
|
---|
| 261 | if (mbar->selected != NULL) {
|
---|
[3c8c580] | 262 | (void) ui_menu_open(mbar->selected, rect);
|
---|
[214aefb] | 263 | }
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | /** Handle menu bar position event.
|
---|
| 267 | *
|
---|
| 268 | * @param mbar Menu bar
|
---|
[0262f16c] | 269 | * @param pos_event Position event
|
---|
[214aefb] | 270 | * @return @c ui_claimed iff the event is claimed
|
---|
| 271 | */
|
---|
[0262f16c] | 272 | ui_evclaim_t ui_menu_bar_pos_event(ui_menu_bar_t *mbar, pos_event_t *event)
|
---|
[214aefb] | 273 | {
|
---|
[c68c18b9] | 274 | ui_resource_t *res;
|
---|
[214aefb] | 275 | gfx_coord2_t pos;
|
---|
| 276 | gfx_rect_t rect;
|
---|
| 277 | ui_menu_t *menu;
|
---|
| 278 | const char *caption;
|
---|
| 279 | gfx_coord_t width;
|
---|
| 280 | gfx_coord_t hpad;
|
---|
[0262f16c] | 281 | gfx_coord2_t ppos;
|
---|
| 282 |
|
---|
[c68c18b9] | 283 | res = ui_window_get_res(mbar->window);
|
---|
| 284 |
|
---|
[0262f16c] | 285 | ppos.x = event->hpos;
|
---|
| 286 | ppos.y = event->vpos;
|
---|
[214aefb] | 287 |
|
---|
[c68c18b9] | 288 | if (res->textmode) {
|
---|
[214aefb] | 289 | hpad = menubar_hpad_text;
|
---|
| 290 | } else {
|
---|
| 291 | hpad = menubar_hpad;
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | pos = mbar->rect.p0;
|
---|
| 295 |
|
---|
| 296 | menu = ui_menu_first(mbar);
|
---|
| 297 | while (menu != NULL) {
|
---|
| 298 | caption = ui_menu_caption(menu);
|
---|
[c68c18b9] | 299 | width = gfx_text_width(res->font, caption) + 2 * hpad;
|
---|
[214aefb] | 300 |
|
---|
| 301 | rect.p0 = pos;
|
---|
| 302 | rect.p1.x = rect.p0.x + width;
|
---|
| 303 | rect.p1.y = mbar->rect.p1.y;
|
---|
| 304 |
|
---|
| 305 | /* Check if press is inside menu bar entry */
|
---|
[0262f16c] | 306 | if (event->type == POS_PRESS &&
|
---|
| 307 | gfx_pix_inside_rect(&ppos, &rect)) {
|
---|
[3c8c580] | 308 | ui_menu_bar_select(mbar, &rect, menu);
|
---|
[214aefb] | 309 | return ui_claimed;
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | pos.x += width;
|
---|
| 313 | menu = ui_menu_next(menu);
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 | return ui_unclaimed;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
[62223ec] | 319 | /** Handle menu bar window unfocus notification.
|
---|
| 320 | *
|
---|
| 321 | * @param mbar Menu bar
|
---|
| 322 | */
|
---|
| 323 | void ui_menu_bar_unfocus(ui_menu_bar_t *mbar)
|
---|
| 324 | {
|
---|
[3c8c580] | 325 | // ui_menu_bar_select(mbar, NULL, NULL);
|
---|
[62223ec] | 326 | }
|
---|
| 327 |
|
---|
[214aefb] | 328 | /** Destroy menu bar control.
|
---|
| 329 | *
|
---|
| 330 | * @param arg Argument (ui_menu_bar_t *)
|
---|
| 331 | */
|
---|
| 332 | void ui_menu_bar_ctl_destroy(void *arg)
|
---|
| 333 | {
|
---|
| 334 | ui_menu_bar_t *mbar = (ui_menu_bar_t *) arg;
|
---|
| 335 |
|
---|
| 336 | ui_menu_bar_destroy(mbar);
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 | /** Paint menu bar control.
|
---|
| 340 | *
|
---|
| 341 | * @param arg Argument (ui_menu_bar_t *)
|
---|
| 342 | * @return EOK on success or an error code
|
---|
| 343 | */
|
---|
| 344 | errno_t ui_menu_bar_ctl_paint(void *arg)
|
---|
| 345 | {
|
---|
| 346 | ui_menu_bar_t *mbar = (ui_menu_bar_t *) arg;
|
---|
| 347 |
|
---|
| 348 | return ui_menu_bar_paint(mbar);
|
---|
| 349 | }
|
---|
| 350 |
|
---|
| 351 | /** Handle menu bar control position event.
|
---|
| 352 | *
|
---|
| 353 | * @param arg Argument (ui_menu_bar_t *)
|
---|
| 354 | * @param pos_event Position event
|
---|
| 355 | * @return @c ui_claimed iff the event is claimed
|
---|
| 356 | */
|
---|
| 357 | ui_evclaim_t ui_menu_bar_ctl_pos_event(void *arg, pos_event_t *event)
|
---|
| 358 | {
|
---|
| 359 | ui_menu_bar_t *mbar = (ui_menu_bar_t *) arg;
|
---|
| 360 |
|
---|
| 361 | return ui_menu_bar_pos_event(mbar, event);
|
---|
| 362 | }
|
---|
| 363 |
|
---|
[62223ec] | 364 | /** Handle menu bar control window unfocus notification.
|
---|
| 365 | *
|
---|
| 366 | * @param arg Argument (ui_menu_bar_t *)
|
---|
| 367 | */
|
---|
| 368 | void ui_menu_bar_ctl_unfocus(void *arg)
|
---|
| 369 | {
|
---|
| 370 | ui_menu_bar_t *mbar = (ui_menu_bar_t *) arg;
|
---|
| 371 |
|
---|
| 372 | ui_menu_bar_unfocus(mbar);
|
---|
| 373 | }
|
---|
| 374 |
|
---|
[214aefb] | 375 | /** @}
|
---|
| 376 | */
|
---|