Handle Enter/Escape keys in message dialog.
[helenos.git] / uspace / app / taskbar-cfg / taskbar-cfg.c
blob8b71265d2b67e93ea6cd94f9d6709af9c4c3acf5
1 /*
2 * Copyright (c) 2023 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 /** @addtogroup taskbar-cfg
30 * @{
32 /** @file Taskbar configuration utility (UI)
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/tabset.h>
41 #include <ui/ui.h>
42 #include <ui/window.h>
43 #include "taskbar-cfg.h"
44 #include "startmenu.h"
46 static void wnd_close(ui_window_t *, void *);
48 static ui_window_cb_t window_cb = {
49 .close = wnd_close
52 /** Window close button was clicked.
54 * @param window Window
55 * @param arg Argument (tbcfg)
57 static void wnd_close(ui_window_t *window, void *arg)
59 taskbar_cfg_t *tbcfg = (taskbar_cfg_t *) arg;
61 ui_quit(tbcfg->ui);
64 /** Create Taskbar configuration window.
66 * @param display_spec Display specification
67 * @param rdcfg Place to store pointer to new taskbar configuration
68 * @return EOK on success or an error code
70 errno_t taskbar_cfg_create(const char *display_spec, taskbar_cfg_t **rdcfg)
72 ui_t *ui = NULL;
73 ui_wnd_params_t params;
74 ui_window_t *window = NULL;
75 taskbar_cfg_t *tbcfg = NULL;
76 gfx_rect_t rect;
77 ui_resource_t *ui_res;
78 errno_t rc;
80 tbcfg = calloc(1, sizeof(taskbar_cfg_t));
81 if (tbcfg == NULL) {
82 printf("Out of memory.\n");
83 return ENOMEM;
86 rc = ui_create(display_spec, &ui);
87 if (rc != EOK) {
88 printf("Error creating UI on display %s.\n", display_spec);
89 goto error;
92 ui_wnd_params_init(&params);
93 params.caption = "Taskbar Configuration";
94 if (ui_is_textmode(ui)) {
95 params.rect.p0.x = 0;
96 params.rect.p0.y = 0;
97 params.rect.p1.x = 70;
98 params.rect.p1.y = 23;
99 } else {
100 params.rect.p0.x = 0;
101 params.rect.p0.y = 0;
102 params.rect.p1.x = 470;
103 params.rect.p1.y = 350;
106 tbcfg->ui = ui;
108 rc = ui_window_create(ui, &params, &window);
109 if (rc != EOK) {
110 printf("Error creating window.\n");
111 goto error;
114 ui_window_set_cb(window, &window_cb, (void *)tbcfg);
115 tbcfg->window = window;
117 ui_res = ui_window_get_res(window);
119 rc = ui_fixed_create(&tbcfg->fixed);
120 if (rc != EOK) {
121 printf("Error creating fixed layout.\n");
122 return rc;
125 rc = ui_tab_set_create(ui_res, &tbcfg->tabset);
126 if (rc != EOK) {
127 printf("Error creating tab set.\n");
128 return rc;
131 ui_window_get_app_rect(window, &rect);
132 ui_tab_set_set_rect(tbcfg->tabset, &rect);
134 rc = ui_fixed_add(tbcfg->fixed, ui_tab_set_ctl(tbcfg->tabset));
135 if (rc != EOK) {
136 printf("Error adding control to layout.\n");
137 return rc;
140 rc = startmenu_create(tbcfg, &tbcfg->startmenu);
141 if (rc != EOK)
142 goto error;
144 ui_window_add(window, ui_fixed_ctl(tbcfg->fixed));
146 *rdcfg = tbcfg;
147 return EOK;
148 error:
149 if (tbcfg->startmenu != NULL)
150 startmenu_destroy(tbcfg->startmenu);
151 if (tbcfg->tabset != NULL)
152 ui_tab_set_destroy(tbcfg->tabset);
153 if (tbcfg->fixed != NULL)
154 ui_fixed_destroy(tbcfg->fixed);
155 if (tbcfg->ui != NULL)
156 ui_destroy(ui);
157 free(tbcfg);
158 return rc;
161 /** Open Taskbar configuration.
163 * @param tbcfg Taskbar configuration dialog
164 * @param cfg_repo Path to configuration repository
165 * @return EOK on success or an error code
167 errno_t taskbar_cfg_open(taskbar_cfg_t *tbcfg, const char *cfg_repo)
169 errno_t rc;
171 rc = tbarcfg_open(cfg_repo, &tbcfg->tbarcfg);
172 if (rc != EOK) {
173 rc = tbarcfg_create(cfg_repo, &tbcfg->tbarcfg);
174 if (rc != EOK) {
175 printf("Error opening Taskbar configuration.\n");
176 goto error;
180 return EOK;
181 error:
182 return rc;
185 /** Populate task configuration from configuration repository.
187 * @param tbcfg Taskbar configuration dialog
188 * @return EOK on success or an error code
190 errno_t taskbar_cfg_populate(taskbar_cfg_t *tbcfg)
192 errno_t rc;
194 rc = startmenu_populate(tbcfg->startmenu, tbcfg->tbarcfg);
195 if (rc != EOK)
196 return rc;
198 rc = ui_window_paint(tbcfg->window);
199 if (rc != EOK) {
200 printf("Error painting window.\n");
201 return rc;
204 return EOK;
207 /** Destroy Taskbar configuration window.
209 * @param tbcfg Taskbar configuration window
211 void taskbar_cfg_destroy(taskbar_cfg_t *tbcfg)
213 if (tbcfg->tbarcfg != NULL)
214 tbarcfg_close(tbcfg->tbarcfg);
215 ui_window_destroy(tbcfg->window);
216 ui_destroy(tbcfg->ui);
219 /** @}