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