| 1 | /*
|
|---|
| 2 | * Copyright (c) 2023 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 taskbar-cfg
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file Start menu configuration tab
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <gfx/coord.h>
|
|---|
| 36 | #include <loc.h>
|
|---|
| 37 | #include <stdio.h>
|
|---|
| 38 | #include <stdlib.h>
|
|---|
| 39 | #include <str.h>
|
|---|
| 40 | #include <tbarcfg/tbarcfg.h>
|
|---|
| 41 | #include <ui/control.h>
|
|---|
| 42 | #include <ui/label.h>
|
|---|
| 43 | #include <ui/list.h>
|
|---|
| 44 | #include <ui/pbutton.h>
|
|---|
| 45 | #include <ui/resource.h>
|
|---|
| 46 | #include <ui/tab.h>
|
|---|
| 47 | #include <ui/window.h>
|
|---|
| 48 | #include "startmenu.h"
|
|---|
| 49 | #include "taskbar-cfg.h"
|
|---|
| 50 |
|
|---|
| 51 | static void startmenu_entry_selected(ui_list_entry_t *, void *);
|
|---|
| 52 | static void startmenu_new_entry_clicked(ui_pbutton_t *, void *);
|
|---|
| 53 | static void startmenu_delete_entry_clicked(ui_pbutton_t *, void *);
|
|---|
| 54 |
|
|---|
| 55 | /** Entry list callbacks */
|
|---|
| 56 | ui_list_cb_t startmenu_entry_list_cb = {
|
|---|
| 57 | .selected = startmenu_entry_selected
|
|---|
| 58 | };
|
|---|
| 59 |
|
|---|
| 60 | /** New entry button callbacks */
|
|---|
| 61 | ui_pbutton_cb_t startmenu_new_entry_button_cb = {
|
|---|
| 62 | .clicked = startmenu_new_entry_clicked
|
|---|
| 63 | };
|
|---|
| 64 |
|
|---|
| 65 | /** Remove seat button callbacks */
|
|---|
| 66 | ui_pbutton_cb_t startmenu_delete_entry_button_cb = {
|
|---|
| 67 | .clicked = startmenu_delete_entry_clicked
|
|---|
| 68 | };
|
|---|
| 69 |
|
|---|
| 70 | /** Create start menu configuration tab
|
|---|
| 71 | *
|
|---|
| 72 | * @param tbcfg Taskbar configuration dialog
|
|---|
| 73 | * @param rsmenu Place to store pointer to new start menu configuration tab
|
|---|
| 74 | * @return EOK on success or an error code
|
|---|
| 75 | */
|
|---|
| 76 | errno_t startmenu_create(taskbar_cfg_t *tbcfg, startmenu_t **rsmenu)
|
|---|
| 77 | {
|
|---|
| 78 | ui_resource_t *ui_res;
|
|---|
| 79 | startmenu_t *smenu;
|
|---|
| 80 | gfx_rect_t rect;
|
|---|
| 81 | errno_t rc;
|
|---|
| 82 |
|
|---|
| 83 | ui_res = ui_window_get_res(tbcfg->window);
|
|---|
| 84 |
|
|---|
| 85 | smenu = calloc(1, sizeof(startmenu_t));
|
|---|
| 86 | if (smenu == NULL) {
|
|---|
| 87 | printf("Out of memory.\n");
|
|---|
| 88 | return ENOMEM;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | smenu->tbarcfg = tbcfg;
|
|---|
| 92 |
|
|---|
| 93 | /* 'Start Menu' tab */
|
|---|
| 94 |
|
|---|
| 95 | rc = ui_tab_create(tbcfg->tabset, "Start Menu", &smenu->tab);
|
|---|
| 96 | if (rc != EOK)
|
|---|
| 97 | goto error;
|
|---|
| 98 |
|
|---|
| 99 | rc = ui_fixed_create(&smenu->fixed);
|
|---|
| 100 | if (rc != EOK) {
|
|---|
| 101 | printf("Error creating fixed layout.\n");
|
|---|
| 102 | goto error;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | /* 'Start menu entries:' label */
|
|---|
| 106 |
|
|---|
| 107 | rc = ui_label_create(ui_res, "Start menu entries:",
|
|---|
| 108 | &smenu->entries_label);
|
|---|
| 109 | if (rc != EOK) {
|
|---|
| 110 | printf("Error creating label.\n");
|
|---|
| 111 | goto error;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | if (ui_resource_is_textmode(ui_res)) {
|
|---|
| 115 | rect.p0.x = 4;
|
|---|
| 116 | rect.p0.y = 4;
|
|---|
| 117 | rect.p1.x = 36;
|
|---|
| 118 | rect.p1.y = 5;
|
|---|
| 119 | } else {
|
|---|
| 120 | rect.p0.x = 20;
|
|---|
| 121 | rect.p0.y = 60;
|
|---|
| 122 | rect.p1.x = 360;
|
|---|
| 123 | rect.p1.y = 80;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | ui_label_set_rect(smenu->entries_label, &rect);
|
|---|
| 127 |
|
|---|
| 128 | rc = ui_fixed_add(smenu->fixed, ui_label_ctl(smenu->entries_label));
|
|---|
| 129 | if (rc != EOK) {
|
|---|
| 130 | printf("Error adding control to layout.\n");
|
|---|
| 131 | goto error;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | /* List of entries */
|
|---|
| 135 |
|
|---|
| 136 | rc = ui_list_create(tbcfg->window, false, &smenu->entries_list);
|
|---|
| 137 | if (rc != EOK) {
|
|---|
| 138 | printf("Error creating list.\n");
|
|---|
| 139 | goto error;
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | if (ui_resource_is_textmode(ui_res)) {
|
|---|
| 143 | rect.p0.x = 4;
|
|---|
| 144 | rect.p0.y = 5;
|
|---|
| 145 | rect.p1.x = 56;
|
|---|
| 146 | rect.p1.y = 10;
|
|---|
| 147 | } else {
|
|---|
| 148 | rect.p0.x = 20;
|
|---|
| 149 | rect.p0.y = 80;
|
|---|
| 150 | rect.p1.x = 360;
|
|---|
| 151 | rect.p1.y = 180;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | ui_list_set_rect(smenu->entries_list, &rect);
|
|---|
| 155 |
|
|---|
| 156 | rc = ui_fixed_add(smenu->fixed, ui_list_ctl(smenu->entries_list));
|
|---|
| 157 | if (rc != EOK) {
|
|---|
| 158 | printf("Error adding control to layout.\n");
|
|---|
| 159 | goto error;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | ui_list_set_cb(smenu->entries_list, &startmenu_entry_list_cb,
|
|---|
| 163 | (void *)smenu);
|
|---|
| 164 |
|
|---|
| 165 | /* 'New Entry' button */
|
|---|
| 166 |
|
|---|
| 167 | rc = ui_pbutton_create(ui_res, "New...", &smenu->new_entry);
|
|---|
| 168 | if (rc != EOK) {
|
|---|
| 169 | printf("Error creating button.\n");
|
|---|
| 170 | goto error;
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | if (ui_resource_is_textmode(ui_res)) {
|
|---|
| 174 | rect.p0.x = 58;
|
|---|
| 175 | rect.p0.y = 5;
|
|---|
| 176 | rect.p1.x = 68;
|
|---|
| 177 | rect.p1.y = 6;
|
|---|
| 178 | } else {
|
|---|
| 179 | rect.p0.x = 370;
|
|---|
| 180 | rect.p0.y = 80;
|
|---|
| 181 | rect.p1.x = 450;
|
|---|
| 182 | rect.p1.y = 105;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | ui_pbutton_set_rect(smenu->new_entry, &rect);
|
|---|
| 186 |
|
|---|
| 187 | rc = ui_fixed_add(smenu->fixed, ui_pbutton_ctl(smenu->new_entry));
|
|---|
| 188 | if (rc != EOK) {
|
|---|
| 189 | printf("Error adding control to layout.\n");
|
|---|
| 190 | goto error;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | ui_pbutton_set_cb(smenu->new_entry, &startmenu_new_entry_button_cb,
|
|---|
| 194 | (void *)smenu);
|
|---|
| 195 |
|
|---|
| 196 | /* 'Delete Entry' button */
|
|---|
| 197 |
|
|---|
| 198 | rc = ui_pbutton_create(ui_res, "Delete", &smenu->delete_entry);
|
|---|
| 199 | if (rc != EOK) {
|
|---|
| 200 | printf("Error creating button.\n");
|
|---|
| 201 | goto error;
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | if (ui_resource_is_textmode(ui_res)) {
|
|---|
| 205 | rect.p0.x = 58;
|
|---|
| 206 | rect.p0.y = 7;
|
|---|
| 207 | rect.p1.x = 68;
|
|---|
| 208 | rect.p1.y = 8;
|
|---|
| 209 | } else {
|
|---|
| 210 | rect.p0.x = 370;
|
|---|
| 211 | rect.p0.y = 110;
|
|---|
| 212 | rect.p1.x = 450;
|
|---|
| 213 | rect.p1.y = 135;
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | ui_pbutton_set_rect(smenu->delete_entry, &rect);
|
|---|
| 217 |
|
|---|
| 218 | rc = ui_fixed_add(smenu->fixed, ui_pbutton_ctl(smenu->delete_entry));
|
|---|
| 219 | if (rc != EOK) {
|
|---|
| 220 | printf("Error adding control to layout.\n");
|
|---|
| 221 | goto error;
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | ui_pbutton_set_cb(smenu->delete_entry,
|
|---|
| 225 | &startmenu_delete_entry_button_cb, (void *)smenu);
|
|---|
| 226 |
|
|---|
| 227 | ui_tab_add(smenu->tab, ui_fixed_ctl(smenu->fixed));
|
|---|
| 228 |
|
|---|
| 229 | *rsmenu = smenu;
|
|---|
| 230 | return EOK;
|
|---|
| 231 | error:
|
|---|
| 232 | if (smenu->delete_entry != NULL)
|
|---|
| 233 | ui_pbutton_destroy(smenu->delete_entry);
|
|---|
| 234 | if (smenu->new_entry != NULL)
|
|---|
| 235 | ui_pbutton_destroy(smenu->new_entry);
|
|---|
| 236 | if (smenu->entries_label != NULL)
|
|---|
| 237 | ui_label_destroy(smenu->entries_label);
|
|---|
| 238 | if (smenu->entries_list != NULL)
|
|---|
| 239 | ui_list_destroy(smenu->entries_list);
|
|---|
| 240 | if (smenu->fixed != NULL)
|
|---|
| 241 | ui_fixed_destroy(smenu->fixed);
|
|---|
| 242 | free(smenu);
|
|---|
| 243 | return rc;
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | /** Populate start menu tab with start menu configuration data
|
|---|
| 247 | *
|
|---|
| 248 | * @param smenu Start menu configuration tab
|
|---|
| 249 | * @param tbarcfg Taskbar configuration
|
|---|
| 250 | * @return EOK on success or an error code
|
|---|
| 251 | */
|
|---|
| 252 | errno_t startmenu_populate(startmenu_t *smenu, tbarcfg_t *tbarcfg)
|
|---|
| 253 | {
|
|---|
| 254 | smenu_entry_t *entry;
|
|---|
| 255 | startmenu_entry_t *smentry;
|
|---|
| 256 | const char *caption;
|
|---|
| 257 | const char *cmd;
|
|---|
| 258 | errno_t rc;
|
|---|
| 259 |
|
|---|
| 260 | entry = tbarcfg_smenu_first(tbarcfg);
|
|---|
| 261 | while (entry != NULL) {
|
|---|
| 262 | caption = smenu_entry_get_caption(entry);
|
|---|
| 263 | cmd = smenu_entry_get_cmd(entry);
|
|---|
| 264 |
|
|---|
| 265 | rc = startmenu_insert(smenu, caption, cmd, &smentry);
|
|---|
| 266 | if (rc != EOK)
|
|---|
| 267 | return rc;
|
|---|
| 268 |
|
|---|
| 269 | entry = tbarcfg_smenu_next(entry);
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | return EOK;
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | /** Destroy start menu configuration tab.
|
|---|
| 276 | *
|
|---|
| 277 | * @param smenu Start menu configuration tab
|
|---|
| 278 | */
|
|---|
| 279 | void startmenu_destroy(startmenu_t *smenu)
|
|---|
| 280 | {
|
|---|
| 281 | ui_list_entry_t *lentry;
|
|---|
| 282 | startmenu_entry_t *entry;
|
|---|
| 283 |
|
|---|
| 284 | lentry = ui_list_first(smenu->entries_list);
|
|---|
| 285 | while (lentry != NULL) {
|
|---|
| 286 | entry = (startmenu_entry_t *)ui_list_entry_get_arg(lentry);
|
|---|
| 287 | free(entry->caption);
|
|---|
| 288 | free(entry->cmd);
|
|---|
| 289 | free(entry);
|
|---|
| 290 | ui_list_entry_delete(lentry);
|
|---|
| 291 | lentry = ui_list_first(smenu->entries_list);
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | /* This will automatically destroy all controls in the tab */
|
|---|
| 295 | ui_tab_destroy(smenu->tab);
|
|---|
| 296 | free(smenu);
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 | /** Insert new entry into entries list.
|
|---|
| 300 | *
|
|---|
| 301 | * @param smenu Start menu configuration tab
|
|---|
| 302 | * @param caption Entry caption
|
|---|
| 303 | * @param cmd Command to run
|
|---|
| 304 | * @param rentry Place to store pointer to new entry or NULL
|
|---|
| 305 | * @return EOK on success or an error code
|
|---|
| 306 | */
|
|---|
| 307 | errno_t startmenu_insert(startmenu_t *smenu, const char *caption,
|
|---|
| 308 | const char *cmd, startmenu_entry_t **rentry)
|
|---|
| 309 | {
|
|---|
| 310 | startmenu_entry_t *entry;
|
|---|
| 311 | ui_list_entry_attr_t attr;
|
|---|
| 312 | errno_t rc;
|
|---|
| 313 |
|
|---|
| 314 | entry = calloc(1, sizeof(startmenu_entry_t));
|
|---|
| 315 | if (entry == NULL)
|
|---|
| 316 | return ENOMEM;
|
|---|
| 317 |
|
|---|
| 318 | entry->startmenu = smenu;
|
|---|
| 319 | entry->caption = str_dup(caption);
|
|---|
| 320 | if (entry->caption == NULL) {
|
|---|
| 321 | free(entry);
|
|---|
| 322 | return ENOMEM;
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | entry->cmd = str_dup(cmd);
|
|---|
| 326 | if (entry->caption == NULL) {
|
|---|
| 327 | free(entry->caption);
|
|---|
| 328 | free(entry);
|
|---|
| 329 | return ENOMEM;
|
|---|
| 330 | }
|
|---|
| 331 |
|
|---|
| 332 | ui_list_entry_attr_init(&attr);
|
|---|
| 333 | attr.caption = caption;
|
|---|
| 334 | attr.arg = (void *)entry;
|
|---|
| 335 | rc = ui_list_entry_append(smenu->entries_list, &attr, &entry->lentry);
|
|---|
| 336 | if (rc != EOK) {
|
|---|
| 337 | free(entry->caption);
|
|---|
| 338 | free(entry->cmd);
|
|---|
| 339 | free(entry);
|
|---|
| 340 | return rc;
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 | if (rentry != NULL)
|
|---|
| 344 | *rentry = entry;
|
|---|
| 345 | return EOK;
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 | /** Entry in entry list is selected.
|
|---|
| 349 | *
|
|---|
| 350 | * @param lentry UI list entry
|
|---|
| 351 | * @param arg Argument (dcfg_seats_entry_t *)
|
|---|
| 352 | */
|
|---|
| 353 | static void startmenu_entry_selected(ui_list_entry_t *lentry, void *arg)
|
|---|
| 354 | {
|
|---|
| 355 | (void)lentry;
|
|---|
| 356 | (void)arg;
|
|---|
| 357 | }
|
|---|
| 358 |
|
|---|
| 359 | /** New Entry' button clicked.
|
|---|
| 360 | *
|
|---|
| 361 | * @param pbutton Push button
|
|---|
| 362 | * @param arg Argument (dcfg_seats_entry_t *)
|
|---|
| 363 | */
|
|---|
| 364 | static void startmenu_new_entry_clicked(ui_pbutton_t *pbutton, void *arg)
|
|---|
| 365 | {
|
|---|
| 366 | (void)pbutton;
|
|---|
| 367 | (void)arg;
|
|---|
| 368 | }
|
|---|
| 369 |
|
|---|
| 370 | /** "Delete Entry' button clicked.
|
|---|
| 371 | *
|
|---|
| 372 | * @param pbutton Push button
|
|---|
| 373 | * @param arg Argument (dcfg_seats_entry_t *)
|
|---|
| 374 | */
|
|---|
| 375 | static void startmenu_delete_entry_clicked(ui_pbutton_t *pbutton, void *arg)
|
|---|
| 376 | {
|
|---|
| 377 | (void)pbutton;
|
|---|
| 378 | (void)arg;
|
|---|
| 379 | }
|
|---|
| 380 |
|
|---|
| 381 | /** @}
|
|---|
| 382 | */
|
|---|