]> Dogcows Code - chaz/tint2/blob - src/taskbar/taskbar.c
fixed bug : clock resize when date changed, number of desktop changed
[chaz/tint2] / src / taskbar / taskbar.c
1 /**************************************************************************
2 *
3 * Tint2 : taskbar
4 *
5 * Copyright (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 **************************************************************************/
19
20 #include <X11/Xlib.h>
21 #include <X11/Xutil.h>
22 #include <X11/Xatom.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <glib.h>
27 #include <Imlib2.h>
28
29 #include "task.h"
30 #include "taskbar.h"
31 #include "server.h"
32 #include "window.h"
33 #include "panel.h"
34
35
36
37 void init_taskbar()
38 {
39 Panel *panel;
40 int i, j;
41
42 for (i=0 ; i < nb_panel ; i++) {
43 panel = &panel1[i];
44
45 // taskbar
46 panel->g_taskbar._resize = resize_taskbar;
47 panel->g_taskbar.posy = panel->area.pix.border.width + panel->area.paddingy;
48 panel->g_taskbar.height = panel->area.height - (2 * panel->g_taskbar.posy);
49 panel->g_taskbar.redraw = 1;
50
51 // task
52 panel->g_task.area._draw_foreground = draw_foreground_task;
53 panel->g_task.area.posy = panel->g_taskbar.posy + panel->g_taskbar.pix.border.width + panel->g_taskbar.paddingy;
54 panel->g_task.area.height = panel->area.height - (2 * panel->g_task.area.posy);
55 panel->g_task.area.use_active = 1;
56 panel->g_task.area.redraw = 1;
57
58 if (panel->g_task.area.pix.border.rounded > panel->g_task.area.height/2) {
59 panel->g_task.area.pix.border.rounded = panel->g_task.area.height/2;
60 panel->g_task.area.pix_active.border.rounded = panel->g_task.area.pix.border.rounded;
61 }
62
63 // compute vertical position : text and icon
64 int height_ink, height;
65 get_text_size(panel->g_task.font_desc, &height_ink, &height, panel->area.height, "TAjpg", 5);
66
67 if (!panel->g_task.maximum_width)
68 panel->g_task.maximum_width = server.monitor[panel->monitor].width;
69
70 // add task_icon_size
71 panel->g_task.text_posx = panel->g_task.area.paddingxlr + panel->g_task.area.pix.border.width;
72 panel->g_task.text_posy = (panel->g_task.area.height - height) / 2.0;
73 if (panel->g_task.icon) {
74 panel->g_task.icon_size1 = panel->g_task.area.height - (2 * panel->g_task.area.paddingy);
75 panel->g_task.text_posx += panel->g_task.icon_size1;
76 panel->g_task.icon_posy = (panel->g_task.area.height - panel->g_task.icon_size1) / 2;
77 }
78 //printf("monitor %d, task_maximum_width %d\n", panel->monitor, panel->g_task.maximum_width);
79
80 Taskbar *tskbar;
81 panel->nb_desktop = server.nb_desktop;
82 panel->taskbar = calloc(panel->nb_desktop, sizeof(Taskbar));
83 for (j=0 ; j < panel->nb_desktop ; j++) {
84 tskbar = &panel->taskbar[j];
85 memcpy(&tskbar->area, &panel->g_taskbar, sizeof(Area));
86 tskbar->desktop = j;
87 }
88 }
89 }
90
91
92 void cleanup_taskbar()
93 {
94 Panel *panel;
95 int i, j;
96 GSList *l0;
97 Task *tsk;
98
99 for (i=0 ; i < nb_panel ; i++) {
100 panel = &panel1[i];
101
102 for (j=0 ; j < panel->nb_desktop ; j++) {
103 l0 = panel->taskbar[j].area.list;
104 while (l0) {
105 tsk = l0->data;
106 l0 = l0->next;
107 // careful : remove_task change l0->next
108 remove_task (tsk);
109 }
110 free_area (&panel->taskbar[j].area);
111 }
112 }
113
114 for (i=0 ; i < nb_panel ; i++) {
115 panel = &panel1[i];
116 if (panel->taskbar) {
117 free(panel->taskbar);
118 panel->taskbar = 0;
119 }
120 }
121 }
122
123
124 Task *task_get_task (Window win)
125 {
126 Task *tsk;
127 GSList *l0;
128 int i, j;
129
130 for (i=0 ; i < nb_panel ; i++) {
131 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
132 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
133 tsk = l0->data;
134 if (win == tsk->win)
135 return tsk;
136 }
137 }
138 }
139 return 0;
140 }
141
142
143 void task_refresh_tasklist ()
144 {
145 Window *win, active_win;
146 int num_results, i, j, k;
147 GSList *l0;
148 Task *tsk;
149
150 win = server_get_property (server.root_win, server.atom._NET_CLIENT_LIST, XA_WINDOW, &num_results);
151
152 if (!win) return;
153
154 // Remove any old and set active win
155 active_win = window_get_active ();
156 if (task_active) {
157 task_active->area.is_active = 0;
158 task_active = 0;
159 }
160
161 for (i=0 ; i < nb_panel ; i++) {
162 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
163 l0 = panel1[i].taskbar[j].area.list;
164 while (l0) {
165 tsk = l0->data;
166 l0 = l0->next;
167
168 if (tsk->win == active_win) {
169 tsk->area.is_active = 1;
170 task_active = tsk;
171 }
172
173 for (k = 0; k < num_results; k++) {
174 if (tsk->win == win[k]) break;
175 }
176 // careful : remove_task change l0->next
177 if (k == num_results) remove_task (tsk);
178 }
179 }
180 }
181
182 // Add any new
183 for (i = 0; i < num_results; i++)
184 if (!task_get_task (win[i]))
185 add_task (win[i]);
186
187 XFree (win);
188 }
189
190
191 int resize_tasks (Taskbar *taskbar)
192 {
193 int ret, task_count, pixel_width, modulo_width=0;
194 int x, taskbar_width;
195 Task *tsk;
196 Panel *panel = (Panel*)taskbar->area.panel;
197 GSList *l;
198
199 // new task width for 'desktop'
200 task_count = g_slist_length(taskbar->area.list);
201
202 if (!task_count) pixel_width = panel->g_task.maximum_width;
203 else {
204 taskbar_width = taskbar->area.width - (2 * panel->g_taskbar.pix.border.width) - (2 * panel->g_taskbar.paddingxlr);
205 if (task_count>1) taskbar_width -= ((task_count-1) * panel->g_taskbar.paddingx);
206
207 pixel_width = taskbar_width / task_count;
208 if (pixel_width > panel->g_task.maximum_width)
209 pixel_width = panel->g_task.maximum_width;
210 else
211 modulo_width = taskbar_width % task_count;
212 }
213 //printf("monitor %d, resize_tasks %d %d\n", panel->monitor, task_count, pixel_width);
214
215 if ((taskbar->task_width == pixel_width) && (taskbar->task_modulo == modulo_width)) {
216 ret = 0;
217 }
218 else {
219 ret = 1;
220 taskbar->task_width = pixel_width;
221 taskbar->task_modulo = modulo_width;
222 taskbar->text_width = pixel_width - panel->g_task.text_posx - panel->g_task.area.pix.border.width - panel->g_task.area.paddingx;
223 }
224
225 // change pos_x and width for all tasks
226 x = taskbar->area.posx + taskbar->area.pix.border.width + taskbar->area.paddingxlr;
227 for (l = taskbar->area.list; l ; l = l->next) {
228 tsk = l->data;
229 tsk->area.posx = x;
230 tsk->area.width = pixel_width;
231 if (modulo_width) {
232 tsk->area.width++;
233 modulo_width--;
234 }
235
236 x += tsk->area.width + panel->g_taskbar.paddingx;
237 }
238 return ret;
239 }
240
241
242 // initialise taskbar posx and width
243 void resize_taskbar(void *obj)
244 {
245 Panel *panel = ((Area*)obj)->panel;
246 int taskbar_width, modulo_width, taskbar_on_screen;
247
248 if (panel_mode == MULTI_DESKTOP) taskbar_on_screen = panel->nb_desktop;
249 else taskbar_on_screen = 1;
250
251 taskbar_width = panel->area.width - (2 * panel->area.paddingxlr) - (2 * panel->area.pix.border.width);
252 if (time1_format)
253 taskbar_width -= (panel->clock.area.width + panel->area.paddingx);
254 //taskbar_width -= (panel->trayer.area.width + panel->area.paddingx);
255
256 taskbar_width = (taskbar_width - ((taskbar_on_screen-1) * panel->area.paddingx)) / taskbar_on_screen;
257
258 if (taskbar_on_screen > 1)
259 modulo_width = (taskbar_width - ((taskbar_on_screen-1) * panel->area.paddingx)) % taskbar_on_screen;
260 else
261 modulo_width = 0;
262
263 int i, modulo=0, posx=0;
264 for (i=0 ; i < panel->nb_desktop ; i++) {
265 if ((i % taskbar_on_screen) == 0) {
266 posx = panel->area.pix.border.width + panel->area.paddingxlr;
267 modulo = modulo_width;
268 }
269 else posx += taskbar_width + panel->area.paddingx;
270
271 panel->taskbar[i].area.posx = posx;
272 panel->taskbar[i].area.width = taskbar_width;
273 if (modulo) {
274 panel->taskbar[i].area.width++;
275 modulo--;
276 }
277
278 set_redraw (&panel->taskbar[i].area);
279 resize_tasks(&panel->taskbar[i]);
280 }
281 }
282
283
This page took 0.047618 seconds and 5 git commands to generate.