Mercurial > audlegacy
annotate src/audacious/widgets/playlist_list.c @ 2418:1b109bc34ede trunk
[svn] - fix: W:playlist_list.c(162) [get_transparency_pixmap]: possible type-punned pointer dereference
author | nenolod |
---|---|
date | Fri, 26 Jan 2007 18:10:18 -0800 |
parents | ab2b1b6f6179 |
children | af8b86badfdb |
rev | line source |
---|---|
2313 | 1 /* Audacious - Cross-platform multimedia player |
2 * Copyright (C) 2005-2006 Audacious development team. | |
3 * | |
4 * BMP - Cross-platform multimedia player | |
5 * Copyright (C) 2003-2004 BMP development team. | |
6 * | |
7 * Based on XMMS: | |
8 * Copyright (C) 1998-2003 XMMS development team. | |
9 * | |
10 * This program is free software; you can redistribute it and/or modify | |
11 * it under the terms of the GNU General Public License as published by | |
12 * the Free Software Foundation; either version 2 of the License, or | |
13 * (at your option) any later version. | |
14 * | |
15 * This program is distributed in the hope that it will be useful, | |
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 * GNU General Public License for more details. | |
19 * | |
20 * You should have received a copy of the GNU General Public License | |
21 * along with this program; if not, write to the Free Software | |
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
23 */ | |
24 | |
25 /* | |
26 * A note about Pango and some funky spacey fonts: Weirdly baselined | |
27 * fonts, or fonts with weird ascents or descents _will_ display a | |
28 * little bit weird in the playlist widget, but the display engine | |
29 * won't make it look too bad, just a little deranged. I honestly | |
30 * don't think it's worth fixing (around...), it doesn't have to be | |
31 * perfectly fitting, just the general look has to be ok, which it | |
32 * IMHO is. | |
33 * | |
34 * A second note: The numbers aren't perfectly aligned, but in the | |
35 * end it looks better when using a single Pango layout for each | |
36 * number. | |
37 */ | |
38 | |
39 #include "widgetcore.h" | |
40 | |
41 #include <stdlib.h> | |
42 #include <string.h> | |
43 | |
44 #include "main.h" | |
45 #include "input.h" | |
2385
ab2b1b6f6179
[svn] - add missing inclusion of strings.h where necessary, do not export main.h and export strings.h
giacomo
parents:
2361
diff
changeset
|
46 #include "strings.h" |
2313 | 47 #include "playback.h" |
48 #include "playlist.h" | |
49 #include "ui_playlist.h" | |
50 #include "util.h" | |
51 | |
52 #include "debug.h" | |
53 | |
54 static PangoFontDescription *playlist_list_font = NULL; | |
55 static gint ascent, descent, width_delta_digit_one; | |
56 static gboolean has_slant; | |
57 static guint padding; | |
58 | |
59 /* FIXME: the following globals should not be needed. */ | |
60 static gint width_approx_letters; | |
61 static gint width_colon, width_colon_third; | |
62 static gint width_approx_digits, width_approx_digits_half; | |
63 | |
64 GdkPixmap *rootpix; | |
65 | |
66 void playlist_list_draw(Widget * w); | |
67 | |
68 /* Sort of stolen from XChat, but not really, as theres uses Xlib */ | |
69 static void | |
70 shade_gdkimage_generic (GdkVisual *visual, GdkImage *ximg, int bpl, int w, int h, int rm, int gm, int bm, int bg) | |
71 { | |
72 int x, y; | |
73 int bgr = (256 - rm) * (bg & visual->red_mask); | |
74 int bgg = (256 - gm) * (bg & visual->green_mask); | |
75 int bgb = (256 - bm) * (bg & visual->blue_mask); | |
76 | |
77 for (x = 0; x < w; x++) | |
78 { | |
79 for (y = 0; y < h; y++) | |
80 { | |
81 unsigned long pixel = gdk_image_get_pixel (ximg, x, y); | |
82 int r, g, b; | |
83 | |
84 r = rm * (pixel & visual->red_mask) + bgr; | |
85 g = gm * (pixel & visual->green_mask) + bgg; | |
86 b = bm * (pixel & visual->blue_mask) + bgb; | |
87 | |
88 gdk_image_put_pixel (ximg, x, y, | |
89 ((r >> 8) & visual->red_mask) | | |
90 ((g >> 8) & visual->green_mask) | | |
91 ((b >> 8) & visual->blue_mask)); | |
92 } | |
93 } | |
94 } | |
95 | |
96 /* and this is definately mine... -nenolod */ | |
97 GdkPixmap * | |
98 shade_pixmap(GdkPixmap *in, gint x, gint y, gint x_offset, gint y_offset, gint w, gint h, GdkColor *shade_color) | |
99 { | |
100 GdkImage *ximg; | |
101 GdkPixmap *p; | |
102 GdkGC *gc; | |
103 | |
104 g_return_val_if_fail(in != NULL, NULL); | |
105 | |
106 p = gdk_pixmap_new(in, w, h, -1); | |
107 gc = gdk_gc_new(p); | |
108 | |
109 gdk_draw_drawable(p, gc, in, x, y, 0, 0, w, h); | |
110 | |
111 gdk_error_trap_push(); | |
112 | |
113 ximg = gdk_drawable_copy_to_image(in, NULL, x, y, 0, 0, w, h); /* copy */ | |
114 | |
115 gdk_error_trap_pop(); | |
116 | |
117 if (GDK_IS_IMAGE(ximg)) | |
118 { | |
119 shade_gdkimage_generic(gdk_drawable_get_visual(GDK_WINDOW(playlistwin->window)), | |
120 ximg, ximg->bpl, w, h, 60, 60, 60, shade_color->pixel); | |
121 | |
122 gdk_draw_image(p, gc, ximg, 0, 0, x, y, w, h); | |
123 | |
124 g_object_unref(ximg); | |
125 } | |
126 else { | |
127 cfg.playlist_transparent = FALSE; | |
128 } | |
129 | |
130 g_object_unref(in); | |
131 g_object_unref(gc); | |
132 | |
133 return p; | |
134 } | |
135 | |
136 #ifdef GDK_WINDOWING_X11 | |
137 | |
138 #include <gdk/gdkx.h> | |
139 #include <X11/Xlib.h> | |
140 #include <X11/Xatom.h> | |
141 | |
142 GdkDrawable *get_transparency_pixmap(void) | |
143 { | |
144 GdkDrawable *root; | |
2418
1b109bc34ede
[svn] - fix: W:playlist_list.c(162) [get_transparency_pixmap]: possible type-punned pointer dereference
nenolod
parents:
2385
diff
changeset
|
145 guchar *pm = NULL; |
2313 | 146 GdkAtom prop_type; |
147 gint prop_size; | |
148 GdkPixmap *pixmap; | |
149 gboolean ret; | |
2418
1b109bc34ede
[svn] - fix: W:playlist_list.c(162) [get_transparency_pixmap]: possible type-punned pointer dereference
nenolod
parents:
2385
diff
changeset
|
150 XID *pixmaps; |
2313 | 151 |
152 root = gdk_get_default_root_window(); | |
153 | |
154 pixmap = NULL; | |
155 pixmaps = NULL; | |
156 | |
157 gdk_error_trap_push(); | |
158 | |
159 ret = gdk_property_get(root, gdk_atom_intern("_XROOTPMAP_ID", TRUE), | |
160 0, 0, INT_MAX - 3, | |
161 FALSE, | |
162 &prop_type, NULL, &prop_size, | |
2418
1b109bc34ede
[svn] - fix: W:playlist_list.c(162) [get_transparency_pixmap]: possible type-punned pointer dereference
nenolod
parents:
2385
diff
changeset
|
163 &pm); |
2313 | 164 |
165 gdk_error_trap_pop(); | |
166 | |
2418
1b109bc34ede
[svn] - fix: W:playlist_list.c(162) [get_transparency_pixmap]: possible type-punned pointer dereference
nenolod
parents:
2385
diff
changeset
|
167 if (pm != NULL) |
1b109bc34ede
[svn] - fix: W:playlist_list.c(162) [get_transparency_pixmap]: possible type-punned pointer dereference
nenolod
parents:
2385
diff
changeset
|
168 pixmaps = (XID *) pm; |
1b109bc34ede
[svn] - fix: W:playlist_list.c(162) [get_transparency_pixmap]: possible type-punned pointer dereference
nenolod
parents:
2385
diff
changeset
|
169 |
2313 | 170 if ((ret == TRUE) && (prop_type == GDK_TARGET_PIXMAP) && (prop_size >= sizeof(XID)) && (pixmaps != NULL)) |
171 { | |
172 pixmap = gdk_pixmap_foreign_new_for_display(gdk_drawable_get_display(root), | |
173 pixmaps[0]); | |
174 | |
175 if (pixmaps != NULL) | |
176 g_free(pixmaps); | |
177 } | |
178 | |
179 return GDK_DRAWABLE(pixmap); | |
180 } | |
181 | |
182 static GdkFilterReturn | |
183 root_event_cb (GdkXEvent *xev, GdkEventProperty *event, gpointer data) | |
184 { | |
185 XEvent *xevent = (XEvent *)xev; | |
186 | |
187 if (xevent->type == PropertyNotify) | |
188 { | |
189 Atom at = XInternAtom (xevent->xproperty.display, "_XROOTPMAP_ID", True); | |
190 | |
191 if (at == xevent->xproperty.atom) | |
192 { | |
193 if (rootpix != NULL) | |
194 g_object_unref(rootpix); | |
195 | |
196 rootpix = shade_pixmap(get_transparency_pixmap(), 0, 0, 0, 0, gdk_screen_width(), gdk_screen_height(), | |
197 skin_get_color(bmp_active_skin, SKIN_PLEDIT_NORMALBG)); | |
198 | |
199 if (cfg.playlist_transparent) | |
200 { | |
201 playlistwin_update_list(playlist_get_active()); | |
202 draw_playlist_window(TRUE); | |
203 } | |
204 } | |
205 } | |
206 | |
207 return GDK_FILTER_CONTINUE; | |
208 } | |
209 | |
210 #else | |
211 | |
212 GdkPixmap *get_transparency_pixmap(void) | |
213 { | |
214 return NULL; | |
215 } | |
216 | |
217 #endif | |
218 | |
219 static gboolean | |
220 playlist_list_auto_drag_down_func(gpointer data) | |
221 { | |
222 PlayList_List *pl = data; | |
223 | |
224 if (pl->pl_auto_drag_down) { | |
225 playlist_list_move_down(pl); | |
226 pl->pl_first++; | |
227 playlistwin_update_list(playlist_get_active()); | |
228 return TRUE; | |
229 } | |
230 return FALSE; | |
231 } | |
232 | |
233 static gboolean | |
234 playlist_list_auto_drag_up_func(gpointer data) | |
235 { | |
236 PlayList_List *pl = data; | |
237 | |
238 if (pl->pl_auto_drag_up) { | |
239 playlist_list_move_up(pl); | |
240 pl->pl_first--; | |
241 playlistwin_update_list(playlist_get_active()); | |
242 return TRUE; | |
243 | |
244 } | |
245 return FALSE; | |
246 } | |
247 | |
248 void | |
249 playlist_list_move_up(PlayList_List * pl) | |
250 { | |
251 GList *list; | |
252 Playlist *playlist = playlist_get_active(); | |
253 | |
254 if (!playlist) | |
255 return; | |
256 | |
257 PLAYLIST_LOCK(playlist->mutex); | |
258 if ((list = playlist->entries) == NULL) { | |
259 PLAYLIST_UNLOCK(playlist->mutex); | |
260 return; | |
261 } | |
262 if (PLAYLIST_ENTRY(list->data)->selected) { | |
263 /* We are at the top */ | |
264 PLAYLIST_UNLOCK(playlist->mutex); | |
265 return; | |
266 } | |
267 while (list) { | |
268 if (PLAYLIST_ENTRY(list->data)->selected) | |
269 glist_moveup(list); | |
270 list = g_list_next(list); | |
271 } | |
272 PLAYLIST_UNLOCK(playlist->mutex); | |
273 if (pl->pl_prev_selected != -1) | |
274 pl->pl_prev_selected--; | |
275 if (pl->pl_prev_min != -1) | |
276 pl->pl_prev_min--; | |
277 if (pl->pl_prev_max != -1) | |
278 pl->pl_prev_max--; | |
279 } | |
280 | |
281 void | |
282 playlist_list_move_down(PlayList_List * pl) | |
283 { | |
284 GList *list; | |
285 Playlist *playlist = playlist_get_active(); | |
286 | |
287 if (!playlist) | |
288 return; | |
289 | |
290 PLAYLIST_LOCK(playlist->mutex); | |
291 | |
292 if (!(list = g_list_last(playlist->entries))) { | |
293 PLAYLIST_UNLOCK(playlist->mutex); | |
294 return; | |
295 } | |
296 | |
297 if (PLAYLIST_ENTRY(list->data)->selected) { | |
298 /* We are at the bottom */ | |
299 PLAYLIST_UNLOCK(playlist->mutex); | |
300 return; | |
301 } | |
302 | |
303 while (list) { | |
304 if (PLAYLIST_ENTRY(list->data)->selected) | |
305 glist_movedown(list); | |
306 list = g_list_previous(list); | |
307 } | |
308 | |
309 PLAYLIST_UNLOCK(playlist->mutex); | |
310 | |
311 if (pl->pl_prev_selected != -1) | |
312 pl->pl_prev_selected++; | |
313 if (pl->pl_prev_min != -1) | |
314 pl->pl_prev_min++; | |
315 if (pl->pl_prev_max != -1) | |
316 pl->pl_prev_max++; | |
317 } | |
318 | |
319 static void | |
320 playlist_list_button_press_cb(GtkWidget * widget, | |
321 GdkEventButton * event, | |
322 PlayList_List * pl) | |
323 { | |
2356
4b2c7d9523e7
[svn] right-clicking on playlist entry automatically selects the entry if it was unselected before, closes #728
mf0102
parents:
2313
diff
changeset
|
324 gint nr; |
2313 | 325 Playlist *playlist = playlist_get_active(); |
326 | |
2356
4b2c7d9523e7
[svn] right-clicking on playlist entry automatically selects the entry if it was unselected before, closes #728
mf0102
parents:
2313
diff
changeset
|
327 nr = playlist_list_get_playlist_position(pl, event->x, event->y); |
4b2c7d9523e7
[svn] right-clicking on playlist entry automatically selects the entry if it was unselected before, closes #728
mf0102
parents:
2313
diff
changeset
|
328 if (nr == -1) |
4b2c7d9523e7
[svn] right-clicking on playlist entry automatically selects the entry if it was unselected before, closes #728
mf0102
parents:
2313
diff
changeset
|
329 return; |
2313 | 330 |
2356
4b2c7d9523e7
[svn] right-clicking on playlist entry automatically selects the entry if it was unselected before, closes #728
mf0102
parents:
2313
diff
changeset
|
331 if (event->button == 3) |
4b2c7d9523e7
[svn] right-clicking on playlist entry automatically selects the entry if it was unselected before, closes #728
mf0102
parents:
2313
diff
changeset
|
332 { |
4b2c7d9523e7
[svn] right-clicking on playlist entry automatically selects the entry if it was unselected before, closes #728
mf0102
parents:
2313
diff
changeset
|
333 GList* selection = playlist_get_selected(playlist); |
4b2c7d9523e7
[svn] right-clicking on playlist entry automatically selects the entry if it was unselected before, closes #728
mf0102
parents:
2313
diff
changeset
|
334 if (g_list_find(selection, GINT_TO_POINTER(nr)) == NULL) |
4b2c7d9523e7
[svn] right-clicking on playlist entry automatically selects the entry if it was unselected before, closes #728
mf0102
parents:
2313
diff
changeset
|
335 { |
4b2c7d9523e7
[svn] right-clicking on playlist entry automatically selects the entry if it was unselected before, closes #728
mf0102
parents:
2313
diff
changeset
|
336 playlist_select_all(playlist, FALSE); |
4b2c7d9523e7
[svn] right-clicking on playlist entry automatically selects the entry if it was unselected before, closes #728
mf0102
parents:
2313
diff
changeset
|
337 playlist_select_range(playlist, nr, nr, TRUE); |
4b2c7d9523e7
[svn] right-clicking on playlist entry automatically selects the entry if it was unselected before, closes #728
mf0102
parents:
2313
diff
changeset
|
338 } |
4b2c7d9523e7
[svn] right-clicking on playlist entry automatically selects the entry if it was unselected before, closes #728
mf0102
parents:
2313
diff
changeset
|
339 } |
4b2c7d9523e7
[svn] right-clicking on playlist entry automatically selects the entry if it was unselected before, closes #728
mf0102
parents:
2313
diff
changeset
|
340 else if (event->button == 1) |
4b2c7d9523e7
[svn] right-clicking on playlist entry automatically selects the entry if it was unselected before, closes #728
mf0102
parents:
2313
diff
changeset
|
341 { |
2313 | 342 if (!(event->state & GDK_CONTROL_MASK)) |
343 playlist_select_all(playlist, FALSE); | |
344 | |
345 if (event->state & GDK_SHIFT_MASK && pl->pl_prev_selected != -1) { | |
346 playlist_select_range(playlist, pl->pl_prev_selected, nr, TRUE); | |
347 pl->pl_prev_min = pl->pl_prev_selected; | |
348 pl->pl_prev_max = nr; | |
349 pl->pl_drag_pos = nr - pl->pl_first; | |
350 } | |
351 else { | |
352 if (playlist_select_invert(playlist, nr)) { | |
353 if (event->state & GDK_CONTROL_MASK) { | |
354 if (pl->pl_prev_min == -1) { | |
355 pl->pl_prev_min = pl->pl_prev_selected; | |
356 pl->pl_prev_max = pl->pl_prev_selected; | |
357 } | |
358 if (nr < pl->pl_prev_min) | |
359 pl->pl_prev_min = nr; | |
360 else if (nr > pl->pl_prev_max) | |
361 pl->pl_prev_max = nr; | |
362 } | |
363 else | |
364 pl->pl_prev_min = -1; | |
365 pl->pl_prev_selected = nr; | |
366 pl->pl_drag_pos = nr - pl->pl_first; | |
367 } | |
368 } | |
369 if (event->type == GDK_2BUTTON_PRESS) { | |
370 /* | |
371 * Ungrab the pointer to prevent us from | |
372 * hanging on to it during the sometimes slow | |
373 * playback_initiate(). | |
374 */ | |
375 gdk_pointer_ungrab(GDK_CURRENT_TIME); | |
376 gdk_flush(); | |
377 playlist_set_position(playlist, nr); | |
378 if (!playback_get_playing()) | |
379 playback_initiate(); | |
380 } | |
381 | |
382 pl->pl_dragging = TRUE; | |
383 } | |
2356
4b2c7d9523e7
[svn] right-clicking on playlist entry automatically selects the entry if it was unselected before, closes #728
mf0102
parents:
2313
diff
changeset
|
384 |
4b2c7d9523e7
[svn] right-clicking on playlist entry automatically selects the entry if it was unselected before, closes #728
mf0102
parents:
2313
diff
changeset
|
385 playlistwin_update_list(playlist); |
2313 | 386 } |
387 | |
388 gint | |
389 playlist_list_get_playlist_position(PlayList_List * pl, | |
390 gint x, | |
391 gint y) | |
392 { | |
393 gint iy, length; | |
394 gint ret; | |
395 Playlist *playlist = playlist_get_active(); | |
396 | |
397 if (!widget_contains(WIDGET(pl), x, y) || !pl->pl_fheight) | |
398 return -1; | |
399 | |
400 if ((length = playlist_get_length(playlist)) == 0) | |
401 return -1; | |
402 iy = y - pl->pl_widget.y; | |
403 | |
404 ret = (iy / pl->pl_fheight) + pl->pl_first; | |
405 | |
2356
4b2c7d9523e7
[svn] right-clicking on playlist entry automatically selects the entry if it was unselected before, closes #728
mf0102
parents:
2313
diff
changeset
|
406 if (ret > length - 1) |
2313 | 407 ret = -1; |
408 | |
409 return ret; | |
410 } | |
411 | |
412 static void | |
413 playlist_list_motion_cb(GtkWidget * widget, | |
414 GdkEventMotion * event, | |
415 PlayList_List * pl) | |
416 { | |
417 gint nr, y, off, i; | |
418 | |
419 if (pl->pl_dragging) { | |
420 y = event->y - pl->pl_widget.y; | |
421 nr = (y / pl->pl_fheight); | |
422 if (nr < 0) { | |
423 nr = 0; | |
424 if (!pl->pl_auto_drag_up) { | |
425 pl->pl_auto_drag_up = TRUE; | |
426 pl->pl_auto_drag_up_tag = | |
427 g_timeout_add(100, playlist_list_auto_drag_up_func, pl); | |
428 } | |
429 } | |
430 else if (pl->pl_auto_drag_up) | |
431 pl->pl_auto_drag_up = FALSE; | |
432 | |
433 if (nr >= pl->pl_num_visible) { | |
434 nr = pl->pl_num_visible - 1; | |
435 if (!pl->pl_auto_drag_down) { | |
436 pl->pl_auto_drag_down = TRUE; | |
437 pl->pl_auto_drag_down_tag = | |
438 g_timeout_add(100, playlist_list_auto_drag_down_func, | |
439 pl); | |
440 } | |
441 } | |
442 else if (pl->pl_auto_drag_down) | |
443 pl->pl_auto_drag_down = FALSE; | |
444 | |
445 off = nr - pl->pl_drag_pos; | |
446 if (off) { | |
447 for (i = 0; i < abs(off); i++) { | |
448 if (off < 0) | |
449 playlist_list_move_up(pl); | |
450 else | |
451 playlist_list_move_down(pl); | |
452 | |
453 } | |
454 playlistwin_update_list(playlist_get_active()); | |
455 } | |
456 pl->pl_drag_pos = nr; | |
457 } | |
458 } | |
459 | |
460 static void | |
461 playlist_list_button_release_cb(GtkWidget * widget, | |
462 GdkEventButton * event, | |
463 PlayList_List * pl) | |
464 { | |
465 pl->pl_dragging = FALSE; | |
466 pl->pl_auto_drag_down = FALSE; | |
467 pl->pl_auto_drag_up = FALSE; | |
468 } | |
469 | |
470 static void | |
471 playlist_list_draw_string(PlayList_List * pl, | |
472 PangoFontDescription * font, | |
473 gint line, | |
474 gint width, | |
475 const gchar * text, | |
476 guint ppos) | |
477 { | |
478 guint plist_length_int; | |
479 Playlist *playlist = playlist_get_active(); | |
480 | |
481 PangoLayout *layout; | |
482 | |
483 REQUIRE_LOCK(playlist->mutex); | |
484 | |
485 if (cfg.show_numbers_in_pl) { | |
486 gchar *pos_string = g_strdup_printf(cfg.show_separator_in_pl == TRUE ? "%d" : "%d.", ppos); | |
487 plist_length_int = | |
488 gint_count_digits(playlist_get_length_nolock(playlist)) + !cfg.show_separator_in_pl + 1; /* cf.show_separator_in_pl will be 0 if false */ | |
489 | |
490 padding = plist_length_int; | |
491 padding = ((padding + 1) * width_approx_digits); | |
492 | |
493 layout = gtk_widget_create_pango_layout(playlistwin, pos_string); | |
494 pango_layout_set_font_description(layout, playlist_list_font); | |
495 pango_layout_set_width(layout, plist_length_int * 100); | |
496 | |
497 pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT); | |
498 gdk_draw_layout(pl->pl_widget.parent, pl->pl_widget.gc, | |
499 pl->pl_widget.x + | |
500 (width_approx_digits * | |
501 (-1 + plist_length_int - strlen(pos_string))) + | |
502 (width_approx_digits / 4), | |
503 pl->pl_widget.y + (line - 1) * pl->pl_fheight + | |
504 ascent + abs(descent), layout); | |
505 g_free(pos_string); | |
506 g_object_unref(layout); | |
507 | |
508 if (!cfg.show_separator_in_pl) | |
509 padding -= (width_approx_digits * 1.5); | |
510 } | |
511 else { | |
512 padding = 3; | |
513 } | |
514 | |
515 width -= padding; | |
516 | |
517 layout = gtk_widget_create_pango_layout(playlistwin, text); | |
518 | |
519 pango_layout_set_font_description(layout, playlist_list_font); | |
520 pango_layout_set_width(layout, width * PANGO_SCALE); | |
521 pango_layout_set_single_paragraph_mode(layout, TRUE); | |
522 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); | |
523 gdk_draw_layout(pl->pl_widget.parent, pl->pl_widget.gc, | |
524 pl->pl_widget.x + padding + (width_approx_letters / 4), | |
525 pl->pl_widget.y + (line - 1) * pl->pl_fheight + | |
526 ascent + abs(descent), layout); | |
527 | |
528 g_object_unref(layout); | |
529 } | |
530 | |
531 void | |
532 playlist_list_draw(Widget * w) | |
533 { | |
534 Playlist *playlist = playlist_get_active(); | |
535 PlayList_List *pl = PLAYLIST_LIST(w); | |
536 GList *list; | |
537 GdkGC *gc; | |
538 GdkPixmap *obj; | |
539 PangoLayout *layout; | |
540 gchar *title; | |
541 gint width, height; | |
542 gint i, max_first; | |
543 guint padding, padding_dwidth, padding_plength; | |
544 guint max_time_len = 0; | |
545 gfloat queue_tailpadding = 0; | |
546 gint tpadding; | |
547 gsize tpadding_dwidth = 0; | |
548 gint x, y; | |
549 guint tail_width; | |
550 guint tail_len; | |
551 | |
552 gchar tail[100]; | |
553 gchar queuepos[255]; | |
554 gchar length[40]; | |
555 | |
556 gchar **frags; | |
557 gchar *frag0; | |
558 | |
559 gint plw_w, plw_h; | |
560 | |
561 GdkRectangle *playlist_rect; | |
562 | |
563 gc = pl->pl_widget.gc; | |
564 | |
565 width = pl->pl_widget.width; | |
566 height = pl->pl_widget.height; | |
567 | |
568 obj = pl->pl_widget.parent; | |
569 | |
570 plw_w = playlistwin_get_width(); | |
571 plw_h = playlistwin_get_height(); | |
572 | |
573 playlist_rect = g_new0(GdkRectangle, 1); | |
574 | |
575 playlist_rect->x = 0; | |
576 playlist_rect->y = 0; | |
577 playlist_rect->width = plw_w - 17; | |
578 playlist_rect->height = plw_h - 36; | |
579 | |
580 gdk_gc_set_clip_origin(gc, 31, 58); | |
581 gdk_gc_set_clip_rectangle(gc, playlist_rect); | |
582 | |
583 if (cfg.playlist_transparent == FALSE) | |
584 { | |
585 gdk_gc_set_foreground(gc, | |
586 skin_get_color(bmp_active_skin, | |
587 SKIN_PLEDIT_NORMALBG)); | |
588 gdk_draw_rectangle(obj, gc, TRUE, pl->pl_widget.x, pl->pl_widget.y, | |
589 width, height); | |
590 } | |
591 else | |
592 { | |
593 if (!rootpix) | |
594 rootpix = shade_pixmap(get_transparency_pixmap(), 0, 0, 0, 0, gdk_screen_width(), gdk_screen_height(), | |
595 skin_get_color(bmp_active_skin, SKIN_PLEDIT_NORMALBG)); | |
596 gdk_draw_drawable(obj, gc, rootpix, cfg.playlist_x + pl->pl_widget.x, | |
597 cfg.playlist_y + pl->pl_widget.y, pl->pl_widget.x, pl->pl_widget.y, | |
598 width, height); | |
599 } | |
600 | |
601 if (!playlist_list_font) { | |
602 g_critical("Couldn't open playlist font"); | |
603 return; | |
604 } | |
605 | |
606 pl->pl_fheight = (ascent + abs(descent)); | |
607 pl->pl_num_visible = height / pl->pl_fheight; | |
608 | |
609 max_first = playlist_get_length(playlist) - pl->pl_num_visible; | |
610 max_first = MAX(max_first, 0); | |
611 | |
612 pl->pl_first = CLAMP(pl->pl_first, 0, max_first); | |
613 | |
614 PLAYLIST_LOCK(playlist->mutex); | |
615 list = playlist->entries; | |
616 list = g_list_nth(list, pl->pl_first); | |
617 | |
618 /* It sucks having to run the iteration twice but this is the only | |
619 way you can reliably get the maximum width so we can get our | |
620 playlist nice and aligned... -- plasmaroo */ | |
621 | |
622 for (i = pl->pl_first; | |
623 list && i < pl->pl_first + pl->pl_num_visible; | |
624 list = g_list_next(list), i++) { | |
625 PlaylistEntry *entry = list->data; | |
626 | |
627 if (entry->length != -1) | |
628 { | |
629 g_snprintf(length, sizeof(length), "%d:%-2.2d", | |
630 entry->length / 60000, (entry->length / 1000) % 60); | |
631 tpadding_dwidth = MAX(tpadding_dwidth, strlen(length)); | |
632 } | |
633 } | |
634 | |
635 /* Reset */ | |
636 list = playlist->entries; | |
637 list = g_list_nth(list, pl->pl_first); | |
638 | |
639 for (i = pl->pl_first; | |
640 list && i < pl->pl_first + pl->pl_num_visible; | |
641 list = g_list_next(list), i++) { | |
642 gint pos; | |
643 PlaylistEntry *entry = list->data; | |
644 | |
645 if (entry->selected) { | |
646 gdk_gc_set_foreground(gc, | |
647 skin_get_color(bmp_active_skin, | |
648 SKIN_PLEDIT_SELECTEDBG)); | |
649 gdk_draw_rectangle(obj, gc, TRUE, pl->pl_widget.x, | |
650 pl->pl_widget.y + | |
651 ((i - pl->pl_first) * pl->pl_fheight), | |
652 width, pl->pl_fheight); | |
653 } | |
654 | |
655 /* FIXME: entry->title should NEVER be NULL, and there should | |
656 NEVER be a need to do a UTF-8 conversion. Playlist title | |
657 strings should be kept properly. */ | |
658 | |
659 if (!entry->title) { | |
660 gchar *basename = g_path_get_basename(entry->filename); | |
661 title = filename_to_utf8(basename); | |
662 g_free(basename); | |
663 } | |
664 else | |
665 title = str_to_utf8(entry->title); | |
666 | |
667 title = convert_title_text(title); | |
668 | |
669 pos = playlist_get_queue_position(playlist, entry); | |
670 | |
671 tail[0] = 0; | |
672 queuepos[0] = 0; | |
673 length[0] = 0; | |
674 | |
675 if (pos != -1) | |
676 g_snprintf(queuepos, sizeof(queuepos), "%d", pos + 1); | |
677 | |
678 if (entry->length != -1) | |
679 { | |
680 g_snprintf(length, sizeof(length), "%d:%-2.2d", | |
681 entry->length / 60000, (entry->length / 1000) % 60); | |
682 } | |
683 | |
2361
f24ae4f40e29
[svn] - security and warning fixes from ssommer@suse
nenolod
parents:
2356
diff
changeset
|
684 strncat(tail, length, sizeof(tail) - 1); |
2313 | 685 tail_len = strlen(tail); |
686 | |
687 max_time_len = MAX(max_time_len, tail_len); | |
688 | |
689 if (pos != -1 && tpadding_dwidth <= 0) | |
690 tail_width = width - (width_approx_digits * (strlen(queuepos) + 2.25)); | |
691 else if (pos != -1) | |
692 tail_width = width - (width_approx_digits * (tpadding_dwidth + strlen(queuepos) + 4)); | |
693 else if (tpadding_dwidth > 0) | |
694 tail_width = width - (width_approx_digits * (tpadding_dwidth + 2.5)); | |
695 else | |
696 tail_width = width; | |
697 | |
698 if (i == playlist_get_position_nolock(playlist)) | |
699 gdk_gc_set_foreground(gc, | |
700 skin_get_color(bmp_active_skin, | |
701 SKIN_PLEDIT_CURRENT)); | |
702 else | |
703 gdk_gc_set_foreground(gc, | |
704 skin_get_color(bmp_active_skin, | |
705 SKIN_PLEDIT_NORMAL)); | |
706 playlist_list_draw_string(pl, playlist_list_font, | |
707 i - pl->pl_first, tail_width, title, | |
708 i + 1); | |
709 | |
710 x = pl->pl_widget.x + width - width_approx_digits * 2; | |
711 y = pl->pl_widget.y + ((i - pl->pl_first) - | |
712 1) * pl->pl_fheight + ascent; | |
713 | |
714 frags = NULL; | |
715 frag0 = NULL; | |
716 | |
717 if ((strlen(tail) > 0) && (tail != NULL)) { | |
718 frags = g_strsplit(tail, ":", 0); | |
719 frag0 = g_strconcat(frags[0], ":", NULL); | |
720 | |
721 layout = gtk_widget_create_pango_layout(playlistwin, frags[1]); | |
722 pango_layout_set_font_description(layout, playlist_list_font); | |
723 pango_layout_set_width(layout, tail_len * 100); | |
724 pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT); | |
725 gdk_draw_layout(obj, gc, x - (0.5 * width_approx_digits), | |
726 y + abs(descent), layout); | |
727 g_object_unref(layout); | |
728 | |
729 layout = gtk_widget_create_pango_layout(playlistwin, frag0); | |
730 pango_layout_set_font_description(layout, playlist_list_font); | |
731 pango_layout_set_width(layout, tail_len * 100); | |
732 pango_layout_set_alignment(layout, PANGO_ALIGN_RIGHT); | |
733 gdk_draw_layout(obj, gc, x - (0.75 * width_approx_digits), | |
734 y + abs(descent), layout); | |
735 g_object_unref(layout); | |
736 | |
737 g_free(frag0); | |
738 g_strfreev(frags); | |
739 } | |
740 | |
741 if (pos != -1) { | |
742 | |
743 /* DON'T remove the commented code yet please -- Milosz */ | |
744 | |
745 if (tpadding_dwidth > 0) | |
746 queue_tailpadding = tpadding_dwidth + 1; | |
747 else | |
748 queue_tailpadding = -0.75; | |
749 | |
750 gdk_draw_rectangle(obj, gc, FALSE, | |
751 x - | |
752 (((queue_tailpadding + | |
753 strlen(queuepos)) * | |
754 width_approx_digits) + | |
755 (width_approx_digits / 4)), | |
756 y + abs(descent), | |
757 (strlen(queuepos)) * | |
758 width_approx_digits + | |
759 (width_approx_digits / 2), | |
760 pl->pl_fheight - 2); | |
761 | |
762 layout = | |
763 gtk_widget_create_pango_layout(playlistwin, queuepos); | |
764 pango_layout_set_font_description(layout, playlist_list_font); | |
765 pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER); | |
766 | |
767 gdk_draw_layout(obj, gc, | |
768 x - | |
769 ((queue_tailpadding + | |
770 strlen(queuepos)) * width_approx_digits) + | |
771 (width_approx_digits / 4), | |
772 y + abs(descent), layout); | |
773 g_object_unref(layout); | |
774 } | |
775 | |
776 g_free(title); | |
777 } | |
778 | |
779 | |
780 /* | |
781 * Drop target hovering over the playlist, so draw some hint where the | |
782 * drop will occur. | |
783 * | |
784 * This is (currently? unfixably?) broken when dragging files from Qt/KDE apps, | |
785 * probably due to DnD signaling problems (actually i have no clue). | |
786 * | |
787 */ | |
788 | |
789 if (pl->pl_drag_motion) { | |
790 guint pos, plength, lpadding; | |
791 gint x, y, plx, ply; | |
792 | |
793 if (cfg.show_numbers_in_pl) { | |
794 lpadding = gint_count_digits(playlist_get_length_nolock(playlist)) + 1; | |
795 lpadding = ((lpadding + 1) * width_approx_digits); | |
796 } | |
797 else { | |
798 lpadding = 3; | |
799 }; | |
800 | |
801 /* We already hold the mutex and have the playlist locked, so call | |
802 the non-locking function. */ | |
803 plength = playlist_get_length_nolock(playlist); | |
804 | |
805 x = pl->drag_motion_x; | |
806 y = pl->drag_motion_y; | |
807 | |
808 plx = pl->pl_widget.x; | |
809 ply = pl->pl_widget.y; | |
810 | |
811 if ((x > pl->pl_widget.x) && !(x > pl->pl_widget.width)) { | |
812 | |
813 if ((y > pl->pl_widget.y) | |
814 && !(y > (pl->pl_widget.height + ply))) { | |
815 | |
816 pos = ((y - ((Widget *) pl)->y) / pl->pl_fheight) + | |
817 pl->pl_first; | |
818 | |
819 if (pos > (plength)) { | |
820 pos = plength; | |
821 } | |
822 | |
823 gdk_gc_set_foreground(gc, | |
824 skin_get_color(bmp_active_skin, | |
825 SKIN_PLEDIT_CURRENT)); | |
826 | |
827 gdk_draw_line(obj, gc, pl->pl_widget.x, | |
828 pl->pl_widget.y + ((pos - pl->pl_first) * pl->pl_fheight), | |
829 pl->pl_widget.width + pl->pl_widget.x - 1, | |
830 pl->pl_widget.y + | |
831 ((pos - pl->pl_first) * pl->pl_fheight)); | |
832 } | |
833 | |
834 } | |
835 | |
836 /* When dropping on the borders of the playlist, outside the text area, | |
837 * files get appended at the end of the list. Show that too. | |
838 */ | |
839 | |
840 if ((y < ply) || (y > pl->pl_widget.height + ply)) { | |
841 if ((y >= 0) || (y <= (pl->pl_widget.height + ply))) { | |
842 pos = plength; | |
843 gdk_gc_set_foreground(gc, | |
844 skin_get_color(bmp_active_skin, | |
845 SKIN_PLEDIT_CURRENT)); | |
846 | |
847 gdk_draw_line(obj, gc, pl->pl_widget.x, | |
848 pl->pl_widget.y + | |
849 ((pos - pl->pl_first) * pl->pl_fheight), | |
850 pl->pl_widget.width + pl->pl_widget.x - 1, | |
851 pl->pl_widget.y + | |
852 ((pos - pl->pl_first) * pl->pl_fheight)); | |
853 | |
854 } | |
855 } | |
856 } | |
857 | |
858 gdk_gc_set_foreground(gc, | |
859 skin_get_color(bmp_active_skin, | |
860 SKIN_PLEDIT_NORMAL)); | |
861 | |
862 if (cfg.show_numbers_in_pl) | |
863 { | |
864 padding_plength = playlist_get_length_nolock(playlist); | |
865 | |
866 if (padding_plength == 0) { | |
867 padding_dwidth = 0; | |
868 } | |
869 else { | |
870 padding_dwidth = gint_count_digits(playlist_get_length_nolock(playlist)); | |
871 } | |
872 | |
873 padding = | |
874 (padding_dwidth * | |
875 width_approx_digits) + width_approx_digits; | |
876 | |
877 | |
878 /* For italic or oblique fonts we add another half of the | |
879 * approximate width */ | |
880 if (has_slant) | |
881 padding += width_approx_digits_half; | |
882 | |
883 if (cfg.show_separator_in_pl) { | |
884 gdk_draw_line(obj, gc, | |
885 pl->pl_widget.x + padding, | |
886 pl->pl_widget.y, | |
887 pl->pl_widget.x + padding, | |
888 pl->pl_widget.y + pl->pl_widget.height - 1); | |
889 } | |
890 } | |
891 | |
892 if (tpadding_dwidth != 0) | |
893 { | |
894 tpadding = (tpadding_dwidth * width_approx_digits) + (width_approx_digits * 1.5); | |
895 | |
896 if (has_slant) | |
897 tpadding += width_approx_digits_half; | |
898 | |
899 if (cfg.show_separator_in_pl) { | |
900 gdk_draw_line(obj, gc, | |
901 pl->pl_widget.x + pl->pl_widget.width - tpadding, | |
902 pl->pl_widget.y, | |
903 pl->pl_widget.x + pl->pl_widget.width - tpadding, | |
904 pl->pl_widget.y + pl->pl_widget.height - 1); | |
905 } | |
906 } | |
907 | |
908 gdk_gc_set_clip_origin(gc, 0, 0); | |
909 gdk_gc_set_clip_rectangle(gc, NULL); | |
910 | |
911 PLAYLIST_UNLOCK(playlist->mutex); | |
912 | |
913 gdk_flush(); | |
914 | |
915 g_free(playlist_rect); | |
916 } | |
917 | |
918 | |
919 PlayList_List * | |
920 create_playlist_list(GList ** wlist, | |
921 GdkPixmap * parent, | |
922 GdkGC * gc, | |
923 gint x, gint y, | |
924 gint w, gint h) | |
925 { | |
926 PlayList_List *pl; | |
927 | |
928 pl = g_new0(PlayList_List, 1); | |
929 widget_init(&pl->pl_widget, parent, gc, x, y, w, h, TRUE); | |
930 | |
931 pl->pl_widget.button_press_cb = | |
932 (WidgetButtonPressFunc) playlist_list_button_press_cb; | |
933 pl->pl_widget.button_release_cb = | |
934 (WidgetButtonReleaseFunc) playlist_list_button_release_cb; | |
935 pl->pl_widget.motion_cb = (WidgetMotionFunc) playlist_list_motion_cb; | |
936 pl->pl_widget.draw = playlist_list_draw; | |
937 | |
938 pl->pl_prev_selected = -1; | |
939 pl->pl_prev_min = -1; | |
940 pl->pl_prev_max = -1; | |
941 | |
942 widget_list_add(wlist, WIDGET(pl)); | |
943 | |
944 #ifdef GDK_WINDOWING_X11 | |
945 gdk_window_set_events (gdk_get_default_root_window(), GDK_PROPERTY_CHANGE_MASK); | |
946 gdk_window_add_filter (gdk_get_default_root_window(), (GdkFilterFunc)root_event_cb, pl); | |
947 #endif | |
948 | |
949 return pl; | |
950 } | |
951 | |
952 void | |
953 playlist_list_set_font(const gchar * font) | |
954 { | |
955 | |
956 /* Welcome to bad hack central 2k3 */ | |
957 | |
958 gchar *font_lower; | |
959 gint width_temp; | |
960 gint width_temp_0; | |
961 | |
962 playlist_list_font = pango_font_description_from_string(font); | |
963 | |
964 text_get_extents(font, | |
965 "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz ", | |
966 &width_approx_letters, NULL, &ascent, &descent); | |
967 | |
968 width_approx_letters = (width_approx_letters / 53); | |
969 | |
970 /* Experimental: We don't weigh the 1 into total because it's width is almost always | |
971 * very different from the rest | |
972 */ | |
973 text_get_extents(font, "023456789", &width_approx_digits, NULL, NULL, | |
974 NULL); | |
975 width_approx_digits = (width_approx_digits / 9); | |
976 | |
977 /* Precache some often used calculations */ | |
978 width_approx_digits_half = width_approx_digits / 2; | |
979 | |
980 /* FIXME: We assume that any other number is broader than the "1" */ | |
981 text_get_extents(font, "1", &width_temp, NULL, NULL, NULL); | |
982 text_get_extents(font, "2", &width_temp_0, NULL, NULL, NULL); | |
983 | |
984 if (abs(width_temp_0 - width_temp) < 2) { | |
985 width_delta_digit_one = 0; | |
986 } | |
987 else { | |
988 width_delta_digit_one = ((width_temp_0 - width_temp) / 2) + 2; | |
989 } | |
990 | |
991 text_get_extents(font, ":", &width_colon, NULL, NULL, NULL); | |
992 width_colon_third = width_colon / 4; | |
993 | |
994 font_lower = g_utf8_strdown(font, strlen(font)); | |
995 /* This doesn't take any i18n into account, but i think there is none with TTF fonts | |
996 * FIXME: This can probably be retrieved trough Pango too | |
997 */ | |
998 has_slant = g_strstr_len(font_lower, strlen(font_lower), "oblique") | |
999 || g_strstr_len(font_lower, strlen(font_lower), "italic"); | |
1000 | |
1001 g_free(font_lower); | |
1002 } |