]> Dogcows Code - chaz/tint2/blob - src/taskbar/task.c
fixed bug with active task
[chaz/tint2] / src / taskbar / task.c
1 /**************************************************************************
2 *
3 * Tint2 : task
4 *
5 * Copyright (C) 2007 Pål Staurland (staura@gmail.com)
6 * Modified (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 **************************************************************************/
20
21 #include <X11/Xlib.h>
22 #include <X11/Xutil.h>
23 #include <X11/Xatom.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <glib.h>
28 #include <Imlib2.h>
29
30 #include "window.h"
31 #include "task.h"
32 #include "server.h"
33 #include "panel.h"
34
35
36
37 void add_task (Window win)
38 {
39 Task *new_tsk;
40 int desktop, monitor;
41
42 if (!win || window_is_hidden (win) || win == window.main_win) return;
43
44 new_tsk = malloc(sizeof(Task));
45 new_tsk->win = win;
46 new_tsk->title = 0;
47 new_tsk->icon_data = 0;
48
49 get_icon(new_tsk);
50 get_title(new_tsk);
51 memcpy(&new_tsk->area, &g_task.area, sizeof(Area));
52 desktop = window_get_desktop (new_tsk->win);
53 monitor = window_get_monitor (new_tsk->win);
54
55 //if (panel.mode == MULTI_MONITOR) monitor = window_get_monitor (new_tsk->win);
56 //else monitor = 0;
57 //printf("task %s : desktop %d, monitor %d\n", new_tsk->title, desktop, monitor);
58
59 XSelectInput (server.dsp, new_tsk->win, PropertyChangeMask|StructureNotifyMask);
60
61 if (desktop == 0xFFFFFFFF) {
62 if (new_tsk->title) {
63 free (new_tsk->title);
64 new_tsk->title = 0;
65 }
66 if (new_tsk->icon_data) {
67 free (new_tsk->icon_data);
68 new_tsk->icon_data = 0;
69 }
70 free(new_tsk);
71 fprintf(stderr, "task on all desktop : ignored\n");
72 return;
73 }
74
75 //printf("add_task %d %s\n", index(desktop, monitor), new_tsk->title);
76 Taskbar *tskbar;
77 tskbar = &panel.taskbar[index(desktop, monitor)];
78 new_tsk->area.parent = tskbar;
79 tskbar->area.list = g_slist_append(tskbar->area.list, new_tsk);
80
81 if (resize_tasks (tskbar))
82 set_redraw (&tskbar->area);
83 }
84
85
86 void remove_task (Task *tsk)
87 {
88 if (!tsk) return;
89
90 Taskbar *tskbar;
91 tskbar = (Taskbar*)tsk->area.parent;
92 tskbar->area.list = g_slist_remove(tskbar->area.list, tsk);
93 resize_tasks (tskbar);
94 set_redraw (&tskbar->area);
95 //printf("remove_task %d %s\n", index(tskbar->desktop, tskbar->monitor), tsk->title);
96
97 if (tsk->title) {
98 free (tsk->title);
99 tsk->title = 0;
100 }
101 if (tsk->icon_data) {
102 free (tsk->icon_data);
103 tsk->icon_data = 0;
104 }
105 XFreePixmap (server.dsp, tsk->area.pix.pmap);
106 XFreePixmap (server.dsp, tsk->area.pix_active.pmap);
107 free(tsk);
108 }
109
110
111 void get_title(Task *tsk)
112 {
113 if (!g_task.text) return;
114
115 char *title, *name;
116
117 name = server_get_property (tsk->win, server.atom._NET_WM_VISIBLE_NAME, server.atom.UTF8_STRING, 0);
118 if (!name || !strlen(name)) {
119 name = server_get_property (tsk->win, server.atom._NET_WM_NAME, server.atom.UTF8_STRING, 0);
120 if (!name || !strlen(name)) {
121 name = server_get_property (tsk->win, server.atom.WM_NAME, XA_STRING, 0);
122 if (!name || !strlen(name)) {
123 name = malloc(10);
124 strcpy(name, "Untitled");
125 }
126 }
127 }
128
129 // add space before title
130 title = malloc(strlen(name)+2);
131 if (g_task.icon) strcpy(title, " ");
132 else title[0] = 0;
133 strcat(title, name);
134 if (name) XFree (name);
135
136 if (tsk->title)
137 free(tsk->title);
138 tsk->title = title;
139 }
140
141
142 void get_icon (Task *tsk)
143 {
144 if (!g_task.icon) return;
145
146 long *data;
147 int num;
148
149 data = server_get_property (tsk->win, server.atom._NET_WM_ICON, XA_CARDINAL, &num);
150 if (data) {
151 printf("get_icon plein\n");
152 // ARGB
153 int w, h;
154 long *tmp_data;
155 tmp_data = get_best_icon (data, get_icon_count (data, num), num, &w, &h, g_task.icon_size1);
156
157 tsk->icon_width = w;
158 tsk->icon_height = h;
159 tsk->icon_data = malloc (w * h * sizeof (long));
160 memcpy (tsk->icon_data, tmp_data, w * h * sizeof (long));
161
162 XFree (data);
163 }
164 else {
165 //XWMHints *hints;
166 //hints = XGetWMHints(server.dsp, tkwin);
167 //if (hints != NULL) {
168 // XFree(hints);
169 //}
170 printf("get_icon vide\n");
171 // XChangeProperty (display, windowH, XInternAtom (display, "_NET_WM_ICON", False), XA_CARDINAL, 32, PropModeReplace, (unsigned char*) data, dataSize);
172 return;
173 }
174 }
175
176
177 void draw_task_icon (Task *tsk, int text_width, int active)
178 {
179 if (tsk->icon_data == 0) get_icon (tsk);
180 if (tsk->icon_data == 0) return;
181
182 Pixmap *pmap = (active == 0) ? (&tsk->area.pix.pmap) : (&tsk->area.pix_active.pmap);
183
184 /* Find pos */
185 int pos_x;
186 if (g_task.centered) {
187 if (g_task.text)
188 pos_x = (tsk->area.width - text_width - g_task.icon_size1) / 2;
189 else
190 pos_x = (tsk->area.width - g_task.icon_size1) / 2;
191 }
192 else pos_x = g_task.area.paddingx + g_task.area.pix.border.width;
193
194 /* Render */
195 Imlib_Image icon;
196 Imlib_Color_Modifier cmod;
197 DATA8 red[256], green[256], blue[256], alpha[256];
198
199 // TODO: cpu improvement : compute only when icon changed
200 DATA32 *data;
201 /* do we have 64bit? => long = 8bit */
202 if (sizeof(long) != 4) {
203 int length = tsk->icon_width * tsk->icon_height;
204 data = malloc(sizeof(DATA32) * length);
205 int i;
206 for (i = 0; i < length; ++i)
207 data[i] = tsk->icon_data[i];
208 }
209 else data = (DATA32 *) tsk->icon_data;
210
211 icon = imlib_create_image_using_data (tsk->icon_width, tsk->icon_height, data);
212 imlib_context_set_image (icon);
213 imlib_context_set_drawable (*pmap);
214
215 cmod = imlib_create_color_modifier ();
216 imlib_context_set_color_modifier (cmod);
217 imlib_image_set_has_alpha (1);
218 imlib_get_color_modifier_tables (red, green, blue, alpha);
219
220 int i, opacity;
221 opacity = (active == 0) ? (255*g_task.font.alpha) : (255*g_task.font_active.alpha);
222 for (i = 127; i < 256; i++) alpha[i] = opacity;
223
224 imlib_set_color_modifier_tables (red, green, blue, alpha);
225
226 //imlib_render_image_on_drawable (pos_x, pos_y);
227 imlib_render_image_on_drawable_at_size (pos_x, g_task.icon_posy, g_task.icon_size1, g_task.icon_size1);
228
229 imlib_free_color_modifier ();
230 imlib_free_image ();
231 if (sizeof(long) != 4) free(data);
232 }
233
234
235 void draw_foreground_task (void *obj, cairo_t *c, int active)
236 {
237 Task *tsk = obj;
238 PangoLayout *layout;
239 config_color *config_text;
240 int width, height;
241
242 if (g_task.text) {
243 /* Layout */
244 layout = pango_cairo_create_layout (c);
245 pango_layout_set_font_description (layout, g_task.font_desc);
246 pango_layout_set_text (layout, tsk->title, -1);
247
248 /* Drawing width and Cut text */
249 pango_layout_set_width (layout, ((Taskbar*)tsk->area.parent)->text_width * PANGO_SCALE);
250 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
251
252 /* Center text */
253 if (g_task.centered) pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
254 else pango_layout_set_alignment (layout, PANGO_ALIGN_LEFT);
255
256 pango_layout_get_pixel_size (layout, &width, &height);
257
258 if (active) config_text = &g_task.font_active;
259 else config_text = &g_task.font;
260
261 cairo_set_source_rgba (c, config_text->color[0], config_text->color[1], config_text->color[2], config_text->alpha);
262
263 pango_cairo_update_layout (c, layout);
264 cairo_move_to (c, g_task.text_posx, g_task.text_posy);
265 pango_cairo_show_layout (c, layout);
266
267 if (g_task.font_shadow) {
268 cairo_set_source_rgba (c, 0.0, 0.0, 0.0, 0.5);
269 pango_cairo_update_layout (c, layout);
270 cairo_move_to (c, g_task.text_posx + 1, g_task.text_posy + 1);
271 pango_cairo_show_layout (c, layout);
272 }
273 g_object_unref (layout);
274 }
275
276 if (g_task.icon) {
277 // icon use same opacity as text
278 draw_task_icon (tsk, width, active);
279 }
280 }
281
This page took 0.04682 seconds and 5 git commands to generate.