| 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 entry edit dialog
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <gfx/coord.h>
|
|---|
| 36 | #include <stdio.h>
|
|---|
| 37 | #include <stdlib.h>
|
|---|
| 38 | #include <ui/fixed.h>
|
|---|
| 39 | #include <ui/resource.h>
|
|---|
| 40 | #include <ui/window.h>
|
|---|
| 41 | #include "taskbar-cfg.h"
|
|---|
| 42 | #include "smeedit.h"
|
|---|
| 43 |
|
|---|
| 44 | static void wnd_close(ui_window_t *, void *);
|
|---|
| 45 |
|
|---|
| 46 | static ui_window_cb_t window_cb = {
|
|---|
| 47 | .close = wnd_close
|
|---|
| 48 | };
|
|---|
| 49 |
|
|---|
| 50 | static void smeedit_ok_clicked(ui_pbutton_t *, void *);
|
|---|
| 51 | static void smeedit_cancel_clicked(ui_pbutton_t *, void *);
|
|---|
| 52 |
|
|---|
| 53 | /** OK button callbacks */
|
|---|
| 54 | ui_pbutton_cb_t smeedit_ok_button_cb = {
|
|---|
| 55 | .clicked = smeedit_ok_clicked
|
|---|
| 56 | };
|
|---|
| 57 |
|
|---|
| 58 | /** Cancel button callbacks */
|
|---|
| 59 | ui_pbutton_cb_t smeedit_cancel_button_cb = {
|
|---|
| 60 | .clicked = smeedit_cancel_clicked
|
|---|
| 61 | };
|
|---|
| 62 |
|
|---|
| 63 | /** Window close button was clicked.
|
|---|
| 64 | *
|
|---|
| 65 | * @param window Window
|
|---|
| 66 | * @param arg Argument (tbcfg)
|
|---|
| 67 | */
|
|---|
| 68 | static void wnd_close(ui_window_t *window, void *arg)
|
|---|
| 69 | {
|
|---|
| 70 | smeedit_t *smee = (smeedit_t *)arg;
|
|---|
| 71 |
|
|---|
| 72 | (void)window;
|
|---|
| 73 | smeedit_destroy(smee);
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | /** Create start menu entry edit dialog.
|
|---|
| 77 | *
|
|---|
| 78 | * @param smenu Start menu
|
|---|
| 79 | * @param smentry Start menu entry to edit or @c NULL if creating
|
|---|
| 80 | * a new entry
|
|---|
| 81 | * @param rsmee Place to store pointer to new start menu entry edit window
|
|---|
| 82 | * @return EOK on success or an error code
|
|---|
| 83 | */
|
|---|
| 84 | errno_t smeedit_create(startmenu_t *smenu, startmenu_entry_t *smentry,
|
|---|
| 85 | smeedit_t **rsmee)
|
|---|
| 86 | {
|
|---|
| 87 | ui_wnd_params_t params;
|
|---|
| 88 | ui_t *ui;
|
|---|
| 89 | ui_window_t *window = NULL;
|
|---|
| 90 | smeedit_t *smee = NULL;
|
|---|
| 91 | gfx_rect_t rect;
|
|---|
| 92 | ui_resource_t *res;
|
|---|
| 93 | const char *cmd;
|
|---|
| 94 | const char *caption;
|
|---|
| 95 | errno_t rc;
|
|---|
| 96 |
|
|---|
| 97 | ui = smenu->tbarcfg->ui;
|
|---|
| 98 |
|
|---|
| 99 | if (smentry != NULL) {
|
|---|
| 100 | cmd = smenu_entry_get_cmd(smentry->entry);
|
|---|
| 101 | caption = smenu_entry_get_caption(smentry->entry);
|
|---|
| 102 | } else {
|
|---|
| 103 | cmd = "";
|
|---|
| 104 | caption = "";
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | smee = calloc(1, sizeof(smeedit_t));
|
|---|
| 108 | if (smee == NULL) {
|
|---|
| 109 | printf("Out of memory.\n");
|
|---|
| 110 | return ENOMEM;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | smee->smentry = smentry;
|
|---|
| 114 |
|
|---|
| 115 | ui_wnd_params_init(¶ms);
|
|---|
| 116 | params.caption = "Edit Start Menu Entry";
|
|---|
| 117 | if (ui_is_textmode(ui)) {
|
|---|
| 118 | params.rect.p0.x = 0;
|
|---|
| 119 | params.rect.p0.y = 0;
|
|---|
| 120 | params.rect.p1.x = 50;
|
|---|
| 121 | params.rect.p1.y = 12;
|
|---|
| 122 | } else {
|
|---|
| 123 | params.rect.p0.x = 0;
|
|---|
| 124 | params.rect.p0.y = 0;
|
|---|
| 125 | params.rect.p1.x = 370;
|
|---|
| 126 | params.rect.p1.y = 200;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | rc = ui_window_create(ui, ¶ms, &window);
|
|---|
| 130 | if (rc != EOK) {
|
|---|
| 131 | printf("Error creating window.\n");
|
|---|
| 132 | goto error;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | ui_window_set_cb(window, &window_cb, (void *)smee);
|
|---|
| 136 | smee->window = window;
|
|---|
| 137 |
|
|---|
| 138 | res = ui_window_get_res(window);
|
|---|
| 139 |
|
|---|
| 140 | rc = ui_fixed_create(&smee->fixed);
|
|---|
| 141 | if (rc != EOK) {
|
|---|
| 142 | printf("Error creating fixed layout.\n");
|
|---|
| 143 | goto error;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | /* Command to run label */
|
|---|
| 147 |
|
|---|
| 148 | rc = ui_label_create(res, "Command to run:", &smee->lcmd);
|
|---|
| 149 | if (rc != EOK)
|
|---|
| 150 | goto error;
|
|---|
| 151 |
|
|---|
| 152 | /* FIXME: Auto layout */
|
|---|
| 153 | if (ui_is_textmode(ui)) {
|
|---|
| 154 | rect.p0.x = 3;
|
|---|
| 155 | rect.p0.y = 2;
|
|---|
| 156 | rect.p1.x = 48;
|
|---|
| 157 | rect.p1.y = 3;
|
|---|
| 158 | } else {
|
|---|
| 159 | rect.p0.x = 10;
|
|---|
| 160 | rect.p0.y = 35;
|
|---|
| 161 | rect.p1.x = 190;
|
|---|
| 162 | rect.p1.y = 50;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | ui_label_set_rect(smee->lcmd, &rect);
|
|---|
| 166 |
|
|---|
| 167 | rc = ui_fixed_add(smee->fixed, ui_label_ctl(smee->lcmd));
|
|---|
| 168 | if (rc != EOK) {
|
|---|
| 169 | printf("Error adding control to layout.\n");
|
|---|
| 170 | goto error;
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | /* Command entry */
|
|---|
| 174 |
|
|---|
| 175 | rc = ui_entry_create(window, cmd, &smee->ecmd);
|
|---|
| 176 | if (rc != EOK)
|
|---|
| 177 | goto error;
|
|---|
| 178 |
|
|---|
| 179 | /* FIXME: Auto layout */
|
|---|
| 180 | if (ui_is_textmode(ui)) {
|
|---|
| 181 | rect.p0.x = 3;
|
|---|
| 182 | rect.p0.y = 3;
|
|---|
| 183 | rect.p1.x = 48;
|
|---|
| 184 | rect.p1.y = 4;
|
|---|
| 185 | } else {
|
|---|
| 186 | rect.p0.x = 10;
|
|---|
| 187 | rect.p0.y = 50;
|
|---|
| 188 | rect.p1.x = 360;
|
|---|
| 189 | rect.p1.y = 75;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | ui_entry_set_rect(smee->ecmd, &rect);
|
|---|
| 193 |
|
|---|
| 194 | rc = ui_fixed_add(smee->fixed, ui_entry_ctl(smee->ecmd));
|
|---|
| 195 | if (rc != EOK) {
|
|---|
| 196 | printf("Error adding control to layout.\n");
|
|---|
| 197 | goto error;
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | /* Caption label */
|
|---|
| 201 |
|
|---|
| 202 | rc = ui_label_create(res, "Caption:", &smee->lcaption);
|
|---|
| 203 | if (rc != EOK)
|
|---|
| 204 | goto error;
|
|---|
| 205 |
|
|---|
| 206 | /* FIXME: Auto layout */
|
|---|
| 207 | if (ui_is_textmode(ui)) {
|
|---|
| 208 | rect.p0.x = 3;
|
|---|
| 209 | rect.p0.y = 5;
|
|---|
| 210 | rect.p1.x = 20;
|
|---|
| 211 | rect.p1.y = 6;
|
|---|
| 212 | } else {
|
|---|
| 213 | rect.p0.x = 10;
|
|---|
| 214 | rect.p0.y = 95;
|
|---|
| 215 | rect.p1.x = 190;
|
|---|
| 216 | rect.p1.y = 110;
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | ui_label_set_rect(smee->lcaption, &rect);
|
|---|
| 220 |
|
|---|
| 221 | rc = ui_fixed_add(smee->fixed, ui_label_ctl(smee->lcaption));
|
|---|
| 222 | if (rc != EOK) {
|
|---|
| 223 | printf("Error adding control to layout.\n");
|
|---|
| 224 | goto error;
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | /* Caption entry */
|
|---|
| 228 |
|
|---|
| 229 | rc = ui_entry_create(window, caption, &smee->ecaption);
|
|---|
| 230 | if (rc != EOK)
|
|---|
| 231 | goto error;
|
|---|
| 232 |
|
|---|
| 233 | /* FIXME: Auto layout */
|
|---|
| 234 | if (ui_is_textmode(ui)) {
|
|---|
| 235 | rect.p0.x = 3;
|
|---|
| 236 | rect.p0.y = 6;
|
|---|
| 237 | rect.p1.x = 48;
|
|---|
| 238 | rect.p1.y = 7;
|
|---|
| 239 | } else {
|
|---|
| 240 | rect.p0.x = 10;
|
|---|
| 241 | rect.p0.y = 110;
|
|---|
| 242 | rect.p1.x = 360;
|
|---|
| 243 | rect.p1.y = 135;
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | ui_entry_set_rect(smee->ecaption, &rect);
|
|---|
| 247 |
|
|---|
| 248 | rc = ui_fixed_add(smee->fixed, ui_entry_ctl(smee->ecaption));
|
|---|
| 249 | if (rc != EOK) {
|
|---|
| 250 | printf("Error adding control to layout.\n");
|
|---|
| 251 | goto error;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | /* OK button */
|
|---|
| 255 |
|
|---|
| 256 | rc = ui_pbutton_create(res, "OK", &smee->bok);
|
|---|
| 257 | if (rc != EOK)
|
|---|
| 258 | goto error;
|
|---|
| 259 |
|
|---|
| 260 | /* FIXME: Auto layout */
|
|---|
| 261 | if (ui_is_textmode(ui)) {
|
|---|
| 262 | rect.p0.x = 23;
|
|---|
| 263 | rect.p0.y = 9;
|
|---|
| 264 | rect.p1.x = 35;
|
|---|
| 265 | rect.p1.y = 10;
|
|---|
| 266 | } else {
|
|---|
| 267 | rect.p0.x = 190;
|
|---|
| 268 | rect.p0.y = 155;
|
|---|
| 269 | rect.p1.x = 270;
|
|---|
| 270 | rect.p1.y = 180;
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | ui_pbutton_set_cb(smee->bok, &smeedit_ok_button_cb, (void *)smee);
|
|---|
| 274 | ui_pbutton_set_rect(smee->bok, &rect);
|
|---|
| 275 | ui_pbutton_set_default(smee->bok, true);
|
|---|
| 276 |
|
|---|
| 277 | rc = ui_fixed_add(smee->fixed, ui_pbutton_ctl(smee->bok));
|
|---|
| 278 | if (rc != EOK) {
|
|---|
| 279 | printf("Error adding control to layout.\n");
|
|---|
| 280 | goto error;
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | /* Cancel button */
|
|---|
| 284 |
|
|---|
| 285 | rc = ui_pbutton_create(res, "Cancel", &smee->bcancel);
|
|---|
| 286 | if (rc != EOK)
|
|---|
| 287 | goto error;
|
|---|
| 288 |
|
|---|
| 289 | /* FIXME: Auto layout */
|
|---|
| 290 | if (ui_is_textmode(ui)) {
|
|---|
| 291 | rect.p0.x = 36;
|
|---|
| 292 | rect.p0.y = 9;
|
|---|
| 293 | rect.p1.x = 48;
|
|---|
| 294 | rect.p1.y = 10;
|
|---|
| 295 | } else {
|
|---|
| 296 | rect.p0.x = 280;
|
|---|
| 297 | rect.p0.y = 155;
|
|---|
| 298 | rect.p1.x = 360;
|
|---|
| 299 | rect.p1.y = 180;
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 | ui_pbutton_set_cb(smee->bcancel, &smeedit_cancel_button_cb,
|
|---|
| 303 | (void *)smee);
|
|---|
| 304 | ui_pbutton_set_rect(smee->bcancel, &rect);
|
|---|
| 305 |
|
|---|
| 306 | rc = ui_fixed_add(smee->fixed, ui_pbutton_ctl(smee->bcancel));
|
|---|
| 307 | if (rc != EOK) {
|
|---|
| 308 | printf("Error adding control to layout.\n");
|
|---|
| 309 | goto error;
|
|---|
| 310 | }
|
|---|
| 311 |
|
|---|
| 312 | ui_window_add(window, ui_fixed_ctl(smee->fixed));
|
|---|
| 313 |
|
|---|
| 314 | rc = ui_window_paint(window);
|
|---|
| 315 | if (rc != EOK)
|
|---|
| 316 | goto error;
|
|---|
| 317 |
|
|---|
| 318 | *rsmee = smee;
|
|---|
| 319 | return EOK;
|
|---|
| 320 | error:
|
|---|
| 321 | if (smee->window != NULL)
|
|---|
| 322 | ui_window_destroy(smee->window);
|
|---|
| 323 | free(smee);
|
|---|
| 324 | return rc;
|
|---|
| 325 | }
|
|---|
| 326 |
|
|---|
| 327 | /** Destroy Taskbar configuration window.
|
|---|
| 328 | *
|
|---|
| 329 | * @param smee Start menu entry edit dialog
|
|---|
| 330 | */
|
|---|
| 331 | void smeedit_destroy(smeedit_t *smee)
|
|---|
| 332 | {
|
|---|
| 333 | ui_window_destroy(smee->window);
|
|---|
| 334 | }
|
|---|
| 335 |
|
|---|
| 336 | /** OK button clicked.
|
|---|
| 337 | *
|
|---|
| 338 | * @params bok OK button
|
|---|
| 339 | * @params arg Argument (smeedit_t *)
|
|---|
| 340 | */
|
|---|
| 341 | static void smeedit_ok_clicked(ui_pbutton_t *bok, void *arg)
|
|---|
| 342 | {
|
|---|
| 343 | smeedit_t *smee;
|
|---|
| 344 | const char *cmd;
|
|---|
| 345 | const char *caption;
|
|---|
| 346 | errno_t rc;
|
|---|
| 347 |
|
|---|
| 348 | (void)bok;
|
|---|
| 349 | smee = (smeedit_t *)arg;
|
|---|
| 350 |
|
|---|
| 351 | cmd = ui_entry_get_text(smee->ecmd);
|
|---|
| 352 | caption = ui_entry_get_text(smee->ecaption);
|
|---|
| 353 |
|
|---|
| 354 | rc = smenu_entry_set_cmd(smee->smentry->entry, cmd);
|
|---|
| 355 | if (rc != EOK)
|
|---|
| 356 | return;
|
|---|
| 357 |
|
|---|
| 358 | smenu_entry_set_caption(smee->smentry->entry, caption);
|
|---|
| 359 | if (rc != EOK)
|
|---|
| 360 | return;
|
|---|
| 361 |
|
|---|
| 362 | (void)smenu_entry_save(smee->smentry->entry);
|
|---|
| 363 |
|
|---|
| 364 | smeedit_destroy(smee);
|
|---|
| 365 | }
|
|---|
| 366 |
|
|---|
| 367 | /** Cancel button clicked.
|
|---|
| 368 | *
|
|---|
| 369 | * @params bok OK button
|
|---|
| 370 | * @params arg Argument (smeedit_t *)
|
|---|
| 371 | */
|
|---|
| 372 | static void smeedit_cancel_clicked(ui_pbutton_t *bcancel, void *arg)
|
|---|
| 373 | {
|
|---|
| 374 | smeedit_t *smee;
|
|---|
| 375 |
|
|---|
| 376 | (void)bcancel;
|
|---|
| 377 | smee = (smeedit_t *)arg;
|
|---|
| 378 | smeedit_destroy(smee);
|
|---|
| 379 | }
|
|---|
| 380 |
|
|---|
| 381 | /** @}
|
|---|
| 382 | */
|
|---|