1 /**************************************************************************
5 * Copyright (C) 2007 Pål Staurland (staura@gmail.com)
6 * Modified (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
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.
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 **************************************************************************/
22 #include <X11/Xutil.h>
23 #include <X11/Xatom.h>
37 void add_task (Window win
)
40 int desktop
, monitor
, all_desktop
;
43 if (window_is_hidden (win
) || win
== window
.main_win
) return;
45 desktop
= window_get_desktop (win
);
46 if (desktop
== 0xFFFFFFFF) {
53 if (panel
.mode
== MULTI_MONITOR
) monitor
= window_get_monitor (win
);
57 new_tsk
= malloc(sizeof(Task
));
59 new_tsk
->all_desktop
= all_desktop
;
61 new_tsk
->icon_data
= 0;
65 memcpy(&new_tsk
->area
, &g_task
.area
, sizeof(Area
));
67 //printf("task %s : desktop %d, monitor %d\n", new_tsk->title, desktop, monitor);
68 XSelectInput (server
.dsp
, new_tsk
->win
, PropertyChangeMask
|StructureNotifyMask
);
70 Taskbar
*tskbar
= &panel
.taskbar
[index(desktop
, monitor
)];
71 new_tsk
->area
.parent
= tskbar
;
72 tskbar
->area
.list
= g_slist_append(tskbar
->area
.list
, new_tsk
);
74 if (resize_tasks (tskbar
))
75 set_redraw (&tskbar
->area
);
79 if (desktop
< server
.nb_desktop
)
85 void remove_task (Task
*tsk
)
91 Window win
= tsk
->win
;
92 int desktop
= 0, all_desktop
= tsk
->all_desktop
;
93 int monitor
= ((Taskbar
*)tsk
->area
.parent
)->monitor
;
97 tskbar
= &panel
.taskbar
[index(desktop
, monitor
)];
99 for (l0
= tskbar
->area
.list
; l0
; ) {
102 if (win
== tsk2
->win
)
107 tskbar
= (Taskbar
*)tsk
->area
.parent
;
109 tskbar
->area
.list
= g_slist_remove(tskbar
->area
.list
, tsk2
);
110 resize_tasks (tskbar
);
111 set_redraw (&tskbar
->area
);
112 //printf("remove_task %d %s\n", index(tskbar->desktop, tskbar->monitor), tsk->title);
114 if (tsk2
== panel
.task_active
)
115 panel
.task_active
= 0;
116 if (tsk2
== panel
.task_drag
)
122 free (tsk2
->icon_data
);
124 XFreePixmap (server
.dsp
, tsk2
->area
.pix
.pmap
);
125 XFreePixmap (server
.dsp
, tsk2
->area
.pix_active
.pmap
);
130 if (desktop
< server
.nb_desktop
)
136 void get_title(Task
*tsk
)
138 if (!g_task
.text
) return;
142 name
= server_get_property (tsk
->win
, server
.atom
._NET_WM_VISIBLE_NAME
, server
.atom
.UTF8_STRING
, 0);
143 if (!name
|| !strlen(name
)) {
144 name
= server_get_property (tsk
->win
, server
.atom
._NET_WM_NAME
, server
.atom
.UTF8_STRING
, 0);
145 if (!name
|| !strlen(name
)) {
146 name
= server_get_property (tsk
->win
, server
.atom
.WM_NAME
, XA_STRING
, 0);
147 if (!name
|| !strlen(name
)) {
149 strcpy(name
, "Untitled");
154 // add space before title
155 title
= malloc(strlen(name
)+2);
156 if (g_task
.icon
) strcpy(title
, " ");
159 if (name
) XFree (name
);
167 void get_icon (Task
*tsk
)
169 if (!g_task
.icon
) return;
174 data
= server_get_property (tsk
->win
, server
.atom
._NET_WM_ICON
, XA_CARDINAL
, &num
);
176 //printf("get_icon plein\n");
180 tmp_data
= get_best_icon (data
, get_icon_count (data
, num
), num
, &w
, &h
, g_task
.icon_size1
);
183 tsk
->icon_height
= h
;
184 tsk
->icon_data
= malloc (w
* h
* sizeof (long));
185 memcpy (tsk
->icon_data
, tmp_data
, w
* h
* sizeof (long));
190 //printf("get_icon vide\n");
191 XWMHints
*hints
= XGetWMHints(server
.dsp
, tsk
->win
);
193 if (hints
->flags
& IconPixmapHint
) {
201 void draw_task_icon (Task
*tsk
, int text_width
, int active
)
203 if (tsk
->icon_data
== 0) get_icon (tsk
);
204 if (tsk
->icon_data
== 0) return;
206 Pixmap
*pmap
= (active
== 0) ? (&tsk
->area
.pix
.pmap
) : (&tsk
->area
.pix_active
.pmap
);
210 if (g_task
.centered
) {
212 pos_x
= (tsk
->area
.width
- text_width
- g_task
.icon_size1
) / 2;
214 pos_x
= (tsk
->area
.width
- g_task
.icon_size1
) / 2;
216 else pos_x
= g_task
.area
.paddingxlr
+ g_task
.area
.pix
.border
.width
;
220 Imlib_Color_Modifier cmod
;
221 DATA8 red
[256], green
[256], blue
[256], alpha
[256];
223 // TODO: cpu improvement : compute only when icon changed
225 /* do we have 64bit? => long = 8bit */
226 if (sizeof(long) != 4) {
227 int length
= tsk
->icon_width
* tsk
->icon_height
;
228 data
= malloc(sizeof(DATA32
) * length
);
230 for (i
= 0; i
< length
; ++i
)
231 data
[i
] = tsk
->icon_data
[i
];
233 else data
= (DATA32
*) tsk
->icon_data
;
235 icon
= imlib_create_image_using_data (tsk
->icon_width
, tsk
->icon_height
, data
);
236 imlib_context_set_image (icon
);
237 imlib_context_set_drawable (*pmap
);
239 cmod
= imlib_create_color_modifier ();
240 imlib_context_set_color_modifier (cmod
);
241 imlib_image_set_has_alpha (1);
242 imlib_get_color_modifier_tables (red
, green
, blue
, alpha
);
245 opacity
= (active
== 0) ? (255*g_task
.font
.alpha
) : (255*g_task
.font_active
.alpha
);
246 for (i
= 127; i
< 256; i
++) alpha
[i
] = opacity
;
248 imlib_set_color_modifier_tables (red
, green
, blue
, alpha
);
250 //imlib_render_image_on_drawable (pos_x, pos_y);
251 imlib_render_image_on_drawable_at_size (pos_x
, g_task
.icon_posy
, g_task
.icon_size1
, g_task
.icon_size1
);
253 imlib_free_color_modifier ();
255 if (sizeof(long) != 4) free(data
);
259 void draw_foreground_task (void *obj
, cairo_t
*c
, int active
)
263 config_color
*config_text
;
268 layout
= pango_cairo_create_layout (c
);
269 pango_layout_set_font_description (layout
, g_task
.font_desc
);
270 pango_layout_set_text (layout
, tsk
->title
, -1);
272 /* Drawing width and Cut text */
273 pango_layout_set_width (layout
, ((Taskbar
*)tsk
->area
.parent
)->text_width
* PANGO_SCALE
);
274 pango_layout_set_ellipsize (layout
, PANGO_ELLIPSIZE_END
);
277 if (g_task
.centered
) pango_layout_set_alignment (layout
, PANGO_ALIGN_CENTER
);
278 else pango_layout_set_alignment (layout
, PANGO_ALIGN_LEFT
);
280 pango_layout_get_pixel_size (layout
, &width
, &height
);
282 if (active
) config_text
= &g_task
.font_active
;
283 else config_text
= &g_task
.font
;
285 cairo_set_source_rgba (c
, config_text
->color
[0], config_text
->color
[1], config_text
->color
[2], config_text
->alpha
);
287 pango_cairo_update_layout (c
, layout
);
288 cairo_move_to (c
, g_task
.text_posx
, g_task
.text_posy
);
289 pango_cairo_show_layout (c
, layout
);
291 if (g_task
.font_shadow
) {
292 cairo_set_source_rgba (c
, 0.0, 0.0, 0.0, 0.5);
293 pango_cairo_update_layout (c
, layout
);
294 cairo_move_to (c
, g_task
.text_posx
+ 1, g_task
.text_posy
+ 1);
295 pango_cairo_show_layout (c
, layout
);
297 g_object_unref (layout
);
301 // icon use same opacity as text
302 draw_task_icon (tsk
, width
, active
);