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