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