Mercurial > audlegacy
annotate audacious/skinwin.c @ 2184:7d40f0a290b9 trunk
[svn] - break out string-related functions from util.c
author | nenolod |
---|---|
date | Wed, 20 Dec 2006 06:45:56 -0800 |
parents | f18a5b617c34 |
children |
rev | line source |
---|---|
0 | 1 /* BMP - Cross-platform multimedia player |
2 * Copyright (C) 2003-2004 BMP development team. | |
3 * | |
4 * Based on XMMS: | |
5 * Copyright (C) 1998-2003 XMMS development team. | |
6 * | |
7 * This program is free software; you can redistribute it and/or modify | |
8 * it under the terms of the GNU General Public License as published by | |
2105
f18a5b617c34
[svn] - move to GPLv2-only. Based on my interpretation of the license, we are
nenolod
parents:
1640
diff
changeset
|
9 * the Free Software Foundation; under version 2 of the License. |
0 | 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 * | |
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 | |
1459 | 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
0 | 19 */ |
20 | |
21 #ifdef HAVE_CONFIG_H | |
22 # include "config.h" | |
23 #endif | |
24 | |
25 #include "skinwin.h" | |
26 | |
27 #include <glib.h> | |
452
623391b6463b
[svn] Make "Unarchived Winamp 2.x skin" &c translatable.
nenolod
parents:
451
diff
changeset
|
28 #include <glib/gi18n.h> |
0 | 29 #include <gtk/gtk.h> |
1640 | 30 |
31 #include "platform/smartinclude.h" | |
32 | |
0 | 33 #include <stdlib.h> |
34 #include <string.h> | |
35 | |
36 #include "main.h" | |
1547 | 37 #include "widgets/widgetcore.h" |
0 | 38 #include "util.h" |
39 | |
653 | 40 #define EXTENSION_TARGETS 7 |
41 | |
42 static gchar *ext_targets[EXTENSION_TARGETS] = { "bmp", "xpm", "png", "svg", | |
43 "gif", "jpg", "jpeg" }; | |
0 | 44 |
45 #define THUMBNAIL_WIDTH 90 | |
46 #define THUMBNAIL_HEIGHT 40 | |
47 | |
48 | |
49 enum SkinViewCols { | |
50 SKIN_VIEW_COL_PREVIEW, | |
453
257a8d2047fb
[svn] added a new column in the treeview model to store the skin name
giacomo
parents:
452
diff
changeset
|
51 SKIN_VIEW_COL_FORMATTEDNAME, |
0 | 52 SKIN_VIEW_COL_NAME, |
53 SKIN_VIEW_N_COLS | |
54 }; | |
55 | |
56 | |
57 GList *skinlist = NULL; | |
58 | |
59 | |
60 | |
61 static gchar * | |
62 get_thumbnail_filename(const gchar * path) | |
63 { | |
64 gchar *basename, *pngname, *thumbname; | |
65 | |
66 g_return_val_if_fail(path != NULL, NULL); | |
67 | |
68 basename = g_path_get_basename(path); | |
69 pngname = g_strconcat(basename, ".png", NULL); | |
70 | |
71 thumbname = g_build_filename(bmp_paths[BMP_PATH_SKIN_THUMB_DIR], | |
72 pngname, NULL); | |
73 | |
74 g_free(basename); | |
75 g_free(pngname); | |
76 | |
77 return thumbname; | |
78 } | |
79 | |
80 | |
81 static GdkPixbuf * | |
82 skin_get_preview(const gchar * path) | |
83 { | |
84 GdkPixbuf *preview = NULL; | |
85 gchar *dec_path, *preview_path; | |
86 gboolean is_archive = FALSE; | |
653 | 87 gint i = 0; |
88 gchar buf[60]; /* gives us lots of room */ | |
0 | 89 |
653 | 90 if (file_is_archive(path)) |
91 { | |
0 | 92 if (!(dec_path = archive_decompress(path))) |
93 return NULL; | |
94 | |
95 is_archive = TRUE; | |
96 } | |
653 | 97 else |
98 { | |
0 | 99 dec_path = g_strdup(path); |
100 } | |
101 | |
653 | 102 for (i = 0; i < EXTENSION_TARGETS; i++) |
103 { | |
104 sprintf(buf, "main.%s", ext_targets[i]); | |
0 | 105 |
674
9e553cf3d756
[svn] - Really fix preview generation for PNG/SVG based skins.
nenolod
parents:
653
diff
changeset
|
106 if ((preview_path = find_file_recursively(dec_path, buf)) != NULL) |
653 | 107 break; |
108 } | |
109 | |
110 if (preview_path) | |
111 { | |
0 | 112 preview = gdk_pixbuf_new_from_file(preview_path, NULL); |
113 g_free(preview_path); | |
114 } | |
115 | |
116 if (is_archive) | |
117 del_directory(dec_path); | |
118 | |
119 g_free(dec_path); | |
120 | |
121 return preview; | |
122 } | |
123 | |
124 | |
125 static GdkPixbuf * | |
126 skin_get_thumbnail(const gchar * path) | |
127 { | |
128 GdkPixbuf *scaled = NULL; | |
129 GdkPixbuf *preview; | |
130 gchar *thumbname; | |
131 | |
132 g_return_val_if_fail(path != NULL, NULL); | |
133 | |
134 if (g_str_has_suffix(path, BMP_SKIN_THUMB_DIR_BASENAME)) | |
135 return NULL; | |
136 | |
137 thumbname = get_thumbnail_filename(path); | |
138 | |
139 if (g_file_test(thumbname, G_FILE_TEST_EXISTS)) { | |
140 scaled = gdk_pixbuf_new_from_file(thumbname, NULL); | |
141 g_free(thumbname); | |
142 return scaled; | |
143 } | |
144 | |
145 if (!(preview = skin_get_preview(path))) { | |
146 g_free(thumbname); | |
147 return NULL; | |
148 } | |
149 | |
150 scaled = gdk_pixbuf_scale_simple(preview, | |
151 THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, | |
152 GDK_INTERP_BILINEAR); | |
153 g_object_unref(preview); | |
154 | |
155 gdk_pixbuf_save(scaled, thumbname, "png", NULL, NULL); | |
156 g_free(thumbname); | |
157 | |
158 return scaled; | |
159 } | |
160 | |
161 static void | |
162 skinlist_add(const gchar * filename) | |
163 { | |
164 SkinNode *node; | |
165 gchar *basename; | |
166 | |
167 g_return_if_fail(filename != NULL); | |
168 | |
169 node = g_new0(SkinNode, 1); | |
170 node->path = g_strdup(filename); | |
171 | |
172 basename = g_path_get_basename(filename); | |
173 | |
174 if (file_is_archive(filename)) { | |
175 node->name = archive_basename(basename); | |
452
623391b6463b
[svn] Make "Unarchived Winamp 2.x skin" &c translatable.
nenolod
parents:
451
diff
changeset
|
176 node->desc = _("Archived Winamp 2.x skin"); |
0 | 177 g_free(basename); |
178 } | |
179 else { | |
180 node->name = basename; | |
452
623391b6463b
[svn] Make "Unarchived Winamp 2.x skin" &c translatable.
nenolod
parents:
451
diff
changeset
|
181 node->desc = _("Unarchived Winamp 2.x skin"); |
0 | 182 } |
183 | |
184 skinlist = g_list_prepend(skinlist, node); | |
185 } | |
186 | |
187 static gboolean | |
188 scan_skindir_func(const gchar * path, const gchar * basename, gpointer data) | |
189 { | |
190 if (g_file_test(path, G_FILE_TEST_IS_REGULAR)) { | |
191 if (file_is_archive(path)) { | |
192 skinlist_add(path); | |
193 } | |
194 } | |
195 else if (g_file_test(path, G_FILE_TEST_IS_DIR)) { | |
196 skinlist_add(path); | |
197 } | |
198 | |
199 return FALSE; | |
200 } | |
201 | |
202 static void | |
203 scan_skindir(const gchar * path) | |
204 { | |
205 GError *error = NULL; | |
206 | |
207 g_return_if_fail(path != NULL); | |
208 | |
209 if (path[0] == '.') | |
210 return; | |
211 | |
212 if (!dir_foreach(path, scan_skindir_func, NULL, &error)) { | |
213 g_warning("Failed to open directory (%s): %s", path, error->message); | |
214 g_error_free(error); | |
215 return; | |
216 } | |
217 } | |
218 | |
219 static gint | |
220 skinlist_compare_func(gconstpointer a, gconstpointer b) | |
221 { | |
222 g_return_val_if_fail(a != NULL && SKIN_NODE(a)->name != NULL, 1); | |
223 g_return_val_if_fail(b != NULL && SKIN_NODE(b)->name != NULL, 1); | |
224 return strcasecmp(SKIN_NODE(a)->name, SKIN_NODE(b)->name); | |
225 } | |
226 | |
227 static void | |
228 skin_free_func(gpointer data) | |
229 { | |
230 g_return_if_fail(data != NULL); | |
231 g_free(SKIN_NODE(data)->name); | |
232 g_free(SKIN_NODE(data)->path); | |
233 g_free(data); | |
234 } | |
235 | |
236 | |
237 static void | |
238 skinlist_clear(void) | |
239 { | |
240 if (!skinlist) | |
241 return; | |
242 | |
243 g_list_foreach(skinlist, (GFunc) skin_free_func, NULL); | |
244 g_list_free(skinlist); | |
245 skinlist = NULL; | |
246 } | |
247 | |
248 void | |
249 skinlist_update(void) | |
250 { | |
251 gchar *skinsdir; | |
252 | |
253 skinlist_clear(); | |
254 | |
255 scan_skindir(bmp_paths[BMP_PATH_USER_SKIN_DIR]); | |
256 scan_skindir(DATA_DIR G_DIR_SEPARATOR_S BMP_SKIN_DIR_BASENAME); | |
257 | |
258 skinsdir = getenv("SKINSDIR"); | |
259 if (skinsdir) { | |
260 gchar **dir_list = g_strsplit(skinsdir, ":", 0); | |
261 gchar **dir; | |
262 | |
263 for (dir = dir_list; *dir; dir++) | |
264 scan_skindir(*dir); | |
265 g_strfreev(dir_list); | |
266 } | |
267 | |
268 skinlist = g_list_sort(skinlist, skinlist_compare_func); | |
269 | |
270 g_assert(skinlist != NULL); | |
271 } | |
272 | |
273 void | |
962
a341792533a6
[svn] - Let's not let the user press the skin refresh button while already refreshing ;)
nhjm449
parents:
674
diff
changeset
|
274 skin_view_update(GtkTreeView * treeview, GtkWidget * refresh_button) |
0 | 275 { |
276 GtkTreeSelection *selection = NULL; | |
277 GtkListStore *store; | |
278 GtkTreeIter iter, iter_current_skin; | |
642
880cd914c3fd
[svn] Robustness fix; skinning dialog should deal with incorrect current skin setting gracefully. By external contributor Daniel Drake (Gentoo).
chainsaw
parents:
639
diff
changeset
|
279 gboolean have_current_skin = FALSE; |
0 | 280 GtkTreePath *path; |
281 | |
282 GdkPixbuf *thumbnail; | |
453
257a8d2047fb
[svn] added a new column in the treeview model to store the skin name
giacomo
parents:
452
diff
changeset
|
283 gchar *formattedname; |
0 | 284 gchar *name; |
285 GList *entry; | |
286 | |
287 gtk_widget_set_sensitive(GTK_WIDGET(treeview), FALSE); | |
962
a341792533a6
[svn] - Let's not let the user press the skin refresh button while already refreshing ;)
nhjm449
parents:
674
diff
changeset
|
288 gtk_widget_set_sensitive(GTK_WIDGET(refresh_button), FALSE); |
0 | 289 |
290 store = GTK_LIST_STORE(gtk_tree_view_get_model(treeview)); | |
291 | |
292 gtk_list_store_clear(store); | |
293 | |
294 skinlist_update(); | |
295 | |
296 for (entry = skinlist; entry; entry = g_list_next(entry)) { | |
297 thumbnail = skin_get_thumbnail(SKIN_NODE(entry->data)->path); | |
298 | |
453
257a8d2047fb
[svn] added a new column in the treeview model to store the skin name
giacomo
parents:
452
diff
changeset
|
299 formattedname = g_strdup_printf("<big><b>%s</b></big>\n<i>%s</i>", |
449 | 300 SKIN_NODE(entry->data)->name, SKIN_NODE(entry->data)->desc); |
453
257a8d2047fb
[svn] added a new column in the treeview model to store the skin name
giacomo
parents:
452
diff
changeset
|
301 name = SKIN_NODE(entry->data)->name; |
0 | 302 |
303 gtk_list_store_append(store, &iter); | |
304 gtk_list_store_set(store, &iter, | |
305 SKIN_VIEW_COL_PREVIEW, thumbnail, | |
453
257a8d2047fb
[svn] added a new column in the treeview model to store the skin name
giacomo
parents:
452
diff
changeset
|
306 SKIN_VIEW_COL_FORMATTEDNAME, formattedname, |
0 | 307 SKIN_VIEW_COL_NAME, name, -1); |
639
9d2c175e458e
[svn] More skin-related fixups. One segfault fix by Michael Hanselmann and a fix by Daniel Drake to also display skins that lack a thumbnail.
chainsaw
parents:
453
diff
changeset
|
308 if (thumbnail) |
9d2c175e458e
[svn] More skin-related fixups. One segfault fix by Michael Hanselmann and a fix by Daniel Drake to also display skins that lack a thumbnail.
chainsaw
parents:
453
diff
changeset
|
309 g_object_unref(thumbnail); |
453
257a8d2047fb
[svn] added a new column in the treeview model to store the skin name
giacomo
parents:
452
diff
changeset
|
310 g_free(formattedname); |
0 | 311 |
312 if (g_strstr_len(bmp_active_skin->path, | |
453
257a8d2047fb
[svn] added a new column in the treeview model to store the skin name
giacomo
parents:
452
diff
changeset
|
313 strlen(bmp_active_skin->path), name) ) { |
642
880cd914c3fd
[svn] Robustness fix; skinning dialog should deal with incorrect current skin setting gracefully. By external contributor Daniel Drake (Gentoo).
chainsaw
parents:
639
diff
changeset
|
314 iter_current_skin = iter; |
880cd914c3fd
[svn] Robustness fix; skinning dialog should deal with incorrect current skin setting gracefully. By external contributor Daniel Drake (Gentoo).
chainsaw
parents:
639
diff
changeset
|
315 have_current_skin = TRUE; |
880cd914c3fd
[svn] Robustness fix; skinning dialog should deal with incorrect current skin setting gracefully. By external contributor Daniel Drake (Gentoo).
chainsaw
parents:
639
diff
changeset
|
316 } |
0 | 317 |
318 while (gtk_events_pending()) | |
319 gtk_main_iteration(); | |
320 } | |
321 | |
642
880cd914c3fd
[svn] Robustness fix; skinning dialog should deal with incorrect current skin setting gracefully. By external contributor Daniel Drake (Gentoo).
chainsaw
parents:
639
diff
changeset
|
322 if (have_current_skin) { |
880cd914c3fd
[svn] Robustness fix; skinning dialog should deal with incorrect current skin setting gracefully. By external contributor Daniel Drake (Gentoo).
chainsaw
parents:
639
diff
changeset
|
323 selection = gtk_tree_view_get_selection(treeview); |
880cd914c3fd
[svn] Robustness fix; skinning dialog should deal with incorrect current skin setting gracefully. By external contributor Daniel Drake (Gentoo).
chainsaw
parents:
639
diff
changeset
|
324 gtk_tree_selection_select_iter(selection, &iter_current_skin); |
0 | 325 |
642
880cd914c3fd
[svn] Robustness fix; skinning dialog should deal with incorrect current skin setting gracefully. By external contributor Daniel Drake (Gentoo).
chainsaw
parents:
639
diff
changeset
|
326 path = gtk_tree_model_get_path(GTK_TREE_MODEL(store), |
880cd914c3fd
[svn] Robustness fix; skinning dialog should deal with incorrect current skin setting gracefully. By external contributor Daniel Drake (Gentoo).
chainsaw
parents:
639
diff
changeset
|
327 &iter_current_skin); |
880cd914c3fd
[svn] Robustness fix; skinning dialog should deal with incorrect current skin setting gracefully. By external contributor Daniel Drake (Gentoo).
chainsaw
parents:
639
diff
changeset
|
328 gtk_tree_view_scroll_to_cell(treeview, path, NULL, TRUE, 0.5, 0.5); |
880cd914c3fd
[svn] Robustness fix; skinning dialog should deal with incorrect current skin setting gracefully. By external contributor Daniel Drake (Gentoo).
chainsaw
parents:
639
diff
changeset
|
329 gtk_tree_path_free(path); |
880cd914c3fd
[svn] Robustness fix; skinning dialog should deal with incorrect current skin setting gracefully. By external contributor Daniel Drake (Gentoo).
chainsaw
parents:
639
diff
changeset
|
330 } |
0 | 331 |
332 gtk_widget_set_sensitive(GTK_WIDGET(treeview), TRUE); | |
962
a341792533a6
[svn] - Let's not let the user press the skin refresh button while already refreshing ;)
nhjm449
parents:
674
diff
changeset
|
333 gtk_widget_set_sensitive(GTK_WIDGET(refresh_button), TRUE); |
0 | 334 } |
335 | |
336 | |
337 static void | |
338 skin_view_on_cursor_changed(GtkTreeView * treeview, | |
339 gpointer data) | |
340 { | |
341 GtkTreeModel *model; | |
342 GtkTreeSelection *selection; | |
343 GtkTreeIter iter; | |
344 | |
345 GList *node; | |
453
257a8d2047fb
[svn] added a new column in the treeview model to store the skin name
giacomo
parents:
452
diff
changeset
|
346 gchar *name; |
0 | 347 gchar *comp = NULL; |
348 | |
349 selection = gtk_tree_view_get_selection(treeview); | |
350 if (!gtk_tree_selection_get_selected(selection, &model, &iter)) | |
351 return; | |
352 | |
453
257a8d2047fb
[svn] added a new column in the treeview model to store the skin name
giacomo
parents:
452
diff
changeset
|
353 gtk_tree_model_get(model, &iter, SKIN_VIEW_COL_NAME, &name, -1); |
450
dd84f79979b4
[svn] Dissect the pango markup using scanf and various string delimiter hacks.
nenolod
parents:
449
diff
changeset
|
354 |
0 | 355 for (node = skinlist; node; node = g_list_next(node)) { |
356 comp = SKIN_NODE(node->data)->path; | |
357 if (g_strrstr(comp, name)) | |
358 break; | |
359 } | |
360 | |
453
257a8d2047fb
[svn] added a new column in the treeview model to store the skin name
giacomo
parents:
452
diff
changeset
|
361 g_free(name); |
0 | 362 |
363 bmp_active_skin_load(comp); | |
364 } | |
365 | |
366 | |
367 void | |
368 skin_view_realize(GtkTreeView * treeview) | |
369 { | |
370 GtkListStore *store; | |
371 GtkTreeViewColumn *column; | |
372 GtkCellRenderer *renderer; | |
373 GtkTreeSelection *selection; | |
374 | |
375 gtk_widget_show_all(GTK_WIDGET(treeview)); | |
376 | |
377 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(treeview), TRUE); | |
378 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview), FALSE); | |
379 | |
380 store = gtk_list_store_new(SKIN_VIEW_N_COLS, GDK_TYPE_PIXBUF, | |
453
257a8d2047fb
[svn] added a new column in the treeview model to store the skin name
giacomo
parents:
452
diff
changeset
|
381 G_TYPE_STRING , G_TYPE_STRING); |
0 | 382 gtk_tree_view_set_model(treeview, GTK_TREE_MODEL(store)); |
383 | |
384 column = gtk_tree_view_column_new(); | |
385 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE); | |
386 gtk_tree_view_column_set_spacing(column, 16); | |
387 gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), | |
388 GTK_TREE_VIEW_COLUMN(column)); | |
389 | |
390 renderer = gtk_cell_renderer_pixbuf_new(); | |
391 gtk_tree_view_column_pack_start(column, renderer, FALSE); | |
392 gtk_tree_view_column_set_attributes(column, renderer, "pixbuf", | |
393 SKIN_VIEW_COL_PREVIEW, NULL); | |
394 | |
395 renderer = gtk_cell_renderer_text_new(); | |
396 gtk_tree_view_column_pack_start(column, renderer, TRUE); | |
449 | 397 gtk_tree_view_column_set_attributes(column, renderer, "markup", |
453
257a8d2047fb
[svn] added a new column in the treeview model to store the skin name
giacomo
parents:
452
diff
changeset
|
398 SKIN_VIEW_COL_FORMATTEDNAME, NULL); |
0 | 399 |
400 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview)); | |
401 gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE); | |
402 | |
403 g_signal_connect(treeview, "cursor-changed", | |
404 G_CALLBACK(skin_view_on_cursor_changed), NULL); | |
405 } |