source: mainline/uspace/app/taskbar/taskbar.c@ 1099f25

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1099f25 was 1b92d4b, checked in by Jiri Svoboda <jiri@…>, 3 years ago

Update window buttons based on window added/removed events

  • Property mode set to 100644
File size: 6.0 KB
Line 
1/*
2 * Copyright (c) 2022 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
30 * @{
31 */
32/** @file Task Bar
33 */
34
35#include <gfx/coord.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <str.h>
39#include <ui/fixed.h>
40#include <ui/label.h>
41#include <ui/resource.h>
42#include <ui/ui.h>
43#include <ui/window.h>
44#include <wndmgt.h>
45#include "clock.h"
46#include "taskbar.h"
47#include "wndlist.h"
48
49static void wnd_close(ui_window_t *, void *);
50
51static ui_window_cb_t window_cb = {
52 .close = wnd_close
53};
54
55/** Window close button was clicked.
56 *
57 * @param window Window
58 * @param arg Argument (taskbar)
59 */
60static void wnd_close(ui_window_t *window, void *arg)
61{
62 taskbar_t *taskbar = (taskbar_t *) arg;
63
64 ui_quit(taskbar->ui);
65}
66
67/** Create task bar.
68 *
69 * @param display_spec Display specification
70 * @param wndmgt_svc Window management service (or WNDMGT_DEFAULT)
71 * @param rtaskbar Place to store pointer to new task bar
72 * @return @c EOK on success or an error coe
73 */
74errno_t taskbar_create(const char *display_spec, const char *wndmgt_svc,
75 taskbar_t **rtaskbar)
76{
77 ui_wnd_params_t params;
78 taskbar_t *taskbar = NULL;
79 gfx_rect_t scr_rect;
80 gfx_rect_t rect;
81 ui_resource_t *ui_res;
82 errno_t rc;
83
84 taskbar = calloc(1, sizeof(taskbar_t));
85 if (taskbar == NULL) {
86 rc = ENOMEM;
87 goto error;
88 }
89
90 rc = ui_create(display_spec, &taskbar->ui);
91 if (rc != EOK) {
92 printf("Error creating UI on display %s.\n", display_spec);
93 goto error;
94 }
95
96 rc = ui_get_rect(taskbar->ui, &scr_rect);
97 if (rc != EOK) {
98 if (str_cmp(display_spec, UI_DISPLAY_NULL) != 0) {
99 printf("Error getting screen dimensions.\n");
100 goto error;
101 }
102
103 /* For the sake of unit tests */
104 scr_rect.p0.x = 0;
105 scr_rect.p0.y = 0;
106 scr_rect.p1.x = 100;
107 scr_rect.p1.y = 100;
108 }
109
110 ui_wnd_params_init(&params);
111 params.caption = "Task Bar";
112 params.placement = ui_wnd_place_bottom_left;
113 params.style &= ~ui_wds_titlebar;
114
115 params.rect.p0.x = 0;
116 params.rect.p0.y = 0;
117 params.rect.p1.x = scr_rect.p1.x - scr_rect.p0.x;
118
119 if (ui_is_textmode(taskbar->ui)) {
120 params.rect.p1.y = 1;
121 params.style &= ~ui_wds_frame;
122 } else {
123 params.rect.p1.y = 32;
124 }
125
126 rc = ui_window_create(taskbar->ui, &params, &taskbar->window);
127 if (rc != EOK) {
128 printf("Error creating window.\n");
129 goto error;
130 }
131
132 ui_window_set_cb(taskbar->window, &window_cb, (void *) &taskbar);
133 ui_res = ui_window_get_res(taskbar->window);
134
135 rc = ui_fixed_create(&taskbar->fixed);
136 if (rc != EOK) {
137 printf("Error creating fixed layout.\n");
138 goto error;
139 }
140
141 rc = ui_label_create(ui_res, "HelenOS", &taskbar->label);
142 if (rc != EOK) {
143 printf("Error creating label.\n");
144 goto error;
145 }
146
147 ui_window_get_app_rect(taskbar->window, &rect);
148 if (ui_is_textmode(taskbar->ui)) {
149 rect.p0.x += 1;
150 } else {
151 rect.p0.x += 10;
152 }
153 ui_label_set_rect(taskbar->label, &rect);
154 ui_label_set_halign(taskbar->label, gfx_halign_left);
155 ui_label_set_valign(taskbar->label, gfx_valign_center);
156
157 rc = ui_fixed_add(taskbar->fixed, ui_label_ctl(taskbar->label));
158 if (rc != EOK) {
159 printf("Error adding control to layout.\n");
160 ui_label_destroy(taskbar->label);
161 goto error;
162 }
163
164 rc = wndlist_create(taskbar->window, taskbar->fixed, &taskbar->wndlist);
165 if (rc != EOK) {
166 printf("Error creating window list.\n");
167 goto error;
168 }
169
170 rc = wndlist_open_wm(taskbar->wndlist, wndmgt_svc);
171 if (rc != EOK) {
172 printf("Error attaching window management service.\n");
173 goto error;
174 }
175
176 rc = taskbar_clock_create(taskbar->window, &taskbar->clock);
177 if (rc != EOK)
178 goto error;
179
180 if (ui_is_textmode(taskbar->ui)) {
181 rect.p0.x = params.rect.p1.x - 10;
182 rect.p0.y = 0;
183 rect.p1.x = params.rect.p1.x;
184 rect.p1.y = 1;
185 } else {
186 rect.p0.x = params.rect.p1.x - 80;
187 rect.p0.y = 4;
188 rect.p1.x = params.rect.p1.x - 4;
189 rect.p1.y = 32 - 4;
190 }
191
192 taskbar_clock_set_rect(taskbar->clock, &rect);
193
194 rc = ui_fixed_add(taskbar->fixed, taskbar_clock_ctl(taskbar->clock));
195 if (rc != EOK) {
196 printf("Error adding control to layout.\n");
197 taskbar_clock_destroy(taskbar->clock);
198 goto error;
199 }
200
201 ui_window_add(taskbar->window, ui_fixed_ctl(taskbar->fixed));
202
203 rc = ui_window_paint(taskbar->window);
204 if (rc != EOK) {
205 printf("Error painting window.\n");
206 goto error;
207 }
208
209 *rtaskbar = taskbar;
210 return EOK;
211error:
212 if (taskbar->clock != NULL)
213 taskbar_clock_destroy(taskbar->clock);
214 if (taskbar->wndlist != NULL)
215 wndlist_destroy(taskbar->wndlist);
216 if (taskbar->window != NULL)
217 ui_window_destroy(taskbar->window);
218 if (taskbar->ui != NULL)
219 ui_destroy(taskbar->ui);
220 return rc;
221
222}
223
224/** Destroy task bar. */
225void taskbar_destroy(taskbar_t *taskbar)
226{
227 ui_fixed_remove(taskbar->fixed, taskbar_clock_ctl(taskbar->clock));
228 taskbar_clock_destroy(taskbar->clock);
229 ui_window_destroy(taskbar->window);
230 ui_destroy(taskbar->ui);
231}
232
233/** @}
234 */
Note: See TracBrowser for help on using the repository browser.