source: mainline/uspace/app/taskbar-cfg/smeedit.c@ 550ed86

topic/simplify-dev-export
Last change on this file since 550ed86 was 550ed86, checked in by Jiri Svoboda <jiri@…>, 20 months ago

Need to add new start menu entry to editor's list

  • Property mode set to 100644
File size: 8.8 KB
Line 
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
45static void wnd_close(ui_window_t *, void *);
46
47static ui_window_cb_t window_cb = {
48 .close = wnd_close
49};
50
51static void smeedit_ok_clicked(ui_pbutton_t *, void *);
52static void smeedit_cancel_clicked(ui_pbutton_t *, void *);
53
54/** OK button callbacks */
55ui_pbutton_cb_t smeedit_ok_button_cb = {
56 .clicked = smeedit_ok_clicked
57};
58
59/** Cancel button callbacks */
60ui_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 */
69static 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 */
85errno_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(&params);
118 params.caption = smentry != NULL ? "Edit Start Menu Entry" :
119 "Create Start Menu Entry";
120 if (ui_is_textmode(ui)) {
121 params.rect.p0.x = 0;
122 params.rect.p0.y = 0;
123 params.rect.p1.x = 50;
124 params.rect.p1.y = 12;
125 } else {
126 params.rect.p0.x = 0;
127 params.rect.p0.y = 0;
128 params.rect.p1.x = 370;
129 params.rect.p1.y = 200;
130 }
131
132 rc = ui_window_create(ui, &params, &window);
133 if (rc != EOK) {
134 printf("Error creating window.\n");
135 goto error;
136 }
137
138 ui_window_set_cb(window, &window_cb, (void *)smee);
139 smee->window = window;
140
141 res = ui_window_get_res(window);
142
143 rc = ui_fixed_create(&smee->fixed);
144 if (rc != EOK) {
145 printf("Error creating fixed layout.\n");
146 goto error;
147 }
148
149 /* Command to run label */
150
151 rc = ui_label_create(res, "Command to run:", &smee->lcmd);
152 if (rc != EOK)
153 goto error;
154
155 /* FIXME: Auto layout */
156 if (ui_is_textmode(ui)) {
157 rect.p0.x = 3;
158 rect.p0.y = 2;
159 rect.p1.x = 48;
160 rect.p1.y = 3;
161 } else {
162 rect.p0.x = 10;
163 rect.p0.y = 35;
164 rect.p1.x = 190;
165 rect.p1.y = 50;
166 }
167
168 ui_label_set_rect(smee->lcmd, &rect);
169
170 rc = ui_fixed_add(smee->fixed, ui_label_ctl(smee->lcmd));
171 if (rc != EOK) {
172 printf("Error adding control to layout.\n");
173 goto error;
174 }
175
176 /* Command entry */
177
178 rc = ui_entry_create(window, cmd, &smee->ecmd);
179 if (rc != EOK)
180 goto error;
181
182 /* FIXME: Auto layout */
183 if (ui_is_textmode(ui)) {
184 rect.p0.x = 3;
185 rect.p0.y = 3;
186 rect.p1.x = 48;
187 rect.p1.y = 4;
188 } else {
189 rect.p0.x = 10;
190 rect.p0.y = 50;
191 rect.p1.x = 360;
192 rect.p1.y = 75;
193 }
194
195 ui_entry_set_rect(smee->ecmd, &rect);
196
197 rc = ui_fixed_add(smee->fixed, ui_entry_ctl(smee->ecmd));
198 if (rc != EOK) {
199 printf("Error adding control to layout.\n");
200 goto error;
201 }
202
203 /* Caption label */
204
205 rc = ui_label_create(res, "Caption:", &smee->lcaption);
206 if (rc != EOK)
207 goto error;
208
209 /* FIXME: Auto layout */
210 if (ui_is_textmode(ui)) {
211 rect.p0.x = 3;
212 rect.p0.y = 5;
213 rect.p1.x = 20;
214 rect.p1.y = 6;
215 } else {
216 rect.p0.x = 10;
217 rect.p0.y = 95;
218 rect.p1.x = 190;
219 rect.p1.y = 110;
220 }
221
222 ui_label_set_rect(smee->lcaption, &rect);
223
224 rc = ui_fixed_add(smee->fixed, ui_label_ctl(smee->lcaption));
225 if (rc != EOK) {
226 printf("Error adding control to layout.\n");
227 goto error;
228 }
229
230 /* Caption entry */
231
232 rc = ui_entry_create(window, caption, &smee->ecaption);
233 if (rc != EOK)
234 goto error;
235
236 /* FIXME: Auto layout */
237 if (ui_is_textmode(ui)) {
238 rect.p0.x = 3;
239 rect.p0.y = 6;
240 rect.p1.x = 48;
241 rect.p1.y = 7;
242 } else {
243 rect.p0.x = 10;
244 rect.p0.y = 110;
245 rect.p1.x = 360;
246 rect.p1.y = 135;
247 }
248
249 ui_entry_set_rect(smee->ecaption, &rect);
250
251 rc = ui_fixed_add(smee->fixed, ui_entry_ctl(smee->ecaption));
252 if (rc != EOK) {
253 printf("Error adding control to layout.\n");
254 goto error;
255 }
256
257 /* OK button */
258
259 rc = ui_pbutton_create(res, "OK", &smee->bok);
260 if (rc != EOK)
261 goto error;
262
263 /* FIXME: Auto layout */
264 if (ui_is_textmode(ui)) {
265 rect.p0.x = 23;
266 rect.p0.y = 9;
267 rect.p1.x = 35;
268 rect.p1.y = 10;
269 } else {
270 rect.p0.x = 190;
271 rect.p0.y = 155;
272 rect.p1.x = 270;
273 rect.p1.y = 180;
274 }
275
276 ui_pbutton_set_cb(smee->bok, &smeedit_ok_button_cb, (void *)smee);
277 ui_pbutton_set_rect(smee->bok, &rect);
278 ui_pbutton_set_default(smee->bok, true);
279
280 rc = ui_fixed_add(smee->fixed, ui_pbutton_ctl(smee->bok));
281 if (rc != EOK) {
282 printf("Error adding control to layout.\n");
283 goto error;
284 }
285
286 /* Cancel button */
287
288 rc = ui_pbutton_create(res, "Cancel", &smee->bcancel);
289 if (rc != EOK)
290 goto error;
291
292 /* FIXME: Auto layout */
293 if (ui_is_textmode(ui)) {
294 rect.p0.x = 36;
295 rect.p0.y = 9;
296 rect.p1.x = 48;
297 rect.p1.y = 10;
298 } else {
299 rect.p0.x = 280;
300 rect.p0.y = 155;
301 rect.p1.x = 360;
302 rect.p1.y = 180;
303 }
304
305 ui_pbutton_set_cb(smee->bcancel, &smeedit_cancel_button_cb,
306 (void *)smee);
307 ui_pbutton_set_rect(smee->bcancel, &rect);
308
309 rc = ui_fixed_add(smee->fixed, ui_pbutton_ctl(smee->bcancel));
310 if (rc != EOK) {
311 printf("Error adding control to layout.\n");
312 goto error;
313 }
314
315 ui_window_add(window, ui_fixed_ctl(smee->fixed));
316
317 rc = ui_window_paint(window);
318 if (rc != EOK)
319 goto error;
320
321 *rsmee = smee;
322 return EOK;
323error:
324 if (smee->window != NULL)
325 ui_window_destroy(smee->window);
326 free(smee);
327 return rc;
328}
329
330/** Destroy Taskbar configuration window.
331 *
332 * @param smee Start menu entry edit dialog
333 */
334void smeedit_destroy(smeedit_t *smee)
335{
336 ui_window_destroy(smee->window);
337}
338
339/** OK button clicked.
340 *
341 * @params bok OK button
342 * @params arg Argument (smeedit_t *)
343 */
344static void smeedit_ok_clicked(ui_pbutton_t *bok, void *arg)
345{
346 smeedit_t *smee;
347 smenu_entry_t *entry;
348 startmenu_entry_t *smentry;
349 const char *cmd;
350 const char *caption;
351 errno_t rc;
352
353 (void)bok;
354 smee = (smeedit_t *)arg;
355
356 cmd = ui_entry_get_text(smee->ecmd);
357 caption = ui_entry_get_text(smee->ecaption);
358
359 if (smee->smentry == NULL) {
360 /* Create new entry */
361 rc = smenu_entry_create(smee->startmenu->tbarcfg->tbarcfg,
362 caption, cmd, &entry);
363 if (rc != EOK)
364 return;
365
366 rc = startmenu_insert(smee->startmenu, entry, &smentry);
367 if (rc != EOK)
368 return;
369
370 startmenu_repaint(smee->startmenu);
371 } else {
372 /* Edit existing entry */
373 rc = smenu_entry_set_cmd(smee->smentry->entry, cmd);
374 if (rc != EOK)
375 return;
376
377 smenu_entry_set_caption(smee->smentry->entry, caption);
378 if (rc != EOK)
379 return;
380
381 (void)smenu_entry_save(smee->smentry->entry);
382 startmenu_entry_update(smee->smentry);
383 }
384
385 smeedit_destroy(smee);
386}
387
388/** Cancel button clicked.
389 *
390 * @params bok OK button
391 * @params arg Argument (smeedit_t *)
392 */
393static void smeedit_cancel_clicked(ui_pbutton_t *bcancel, void *arg)
394{
395 smeedit_t *smee;
396
397 (void)bcancel;
398 smee = (smeedit_t *)arg;
399 smeedit_destroy(smee);
400}
401
402/** @}
403 */
Note: See TracBrowser for help on using the repository browser.