/* * Copyright 2016 (c) Anna Schumaker. */ #include #include #include #include #define ARTWORK_PREVIEW_SIZE 150 static struct album *artwork_current = NULL; static unsigned int artwork_timeout = 0; static cairo_surface_t *__gui_artwork_scale(cairo_surface_t *orig, int new_h) { int old_h = cairo_image_surface_get_height(orig); int old_w = cairo_image_surface_get_width(orig); int new_w = (old_w * new_h) / old_h; int scale = gtk_widget_get_scale_factor(GTK_WIDGET(gui_artwork())); cairo_content_t content = cairo_surface_get_content(orig); cairo_surface_t *scaled = cairo_surface_create_similar(orig, content, new_w, new_h); cairo_t *cairo = cairo_create(scaled); cairo_scale(cairo, (double)(scale * new_w) / old_w, (double)(scale * new_h) / old_h); cairo_set_source_surface(cairo, orig, 0, 0); cairo_paint(cairo); cairo_destroy(cairo); return scaled; } static cairo_surface_t *__gui_artwork_get(gchar *path, int new_h) { GdkPixbuf *pixbuf = path ? gdk_pixbuf_new_from_file(path, NULL) : NULL; cairo_surface_t *surface = NULL; cairo_surface_t *scaled = NULL; if (!pixbuf) return NULL; surface = gdk_cairo_surface_create_from_pixbuf(pixbuf, 0, NULL); if (surface) { scaled = __gui_artwork_scale(surface, new_h); cairo_surface_destroy(surface); } g_object_unref(G_OBJECT(pixbuf)); return scaled; } static bool __gui_artwork_set_path(GtkImage *image, gchar *path, int new_h) { cairo_surface_t *surface = __gui_artwork_get(path, new_h); bool status = surface != NULL; if (surface) { gtk_image_set_from_surface(image, surface); cairo_surface_destroy(surface); } else gtk_image_set_from_icon_name(image, "image-missing", GTK_ICON_SIZE_DIALOG); return status; } static gboolean __gui_artwork_timeout(gpointer data) { struct track *track = audio_cur_track(); int height = gui_builder_widget_height("artwork"); gchar *path; bool status; if (!track || track->tr_album == artwork_current) return G_SOURCE_REMOVE; path = album_artwork_path(track->tr_album); status = __gui_artwork_set_path(gui_artwork(), path, height); gtk_widget_set_sensitive(GTK_WIDGET(gui_artwork()), status); artwork_current = status ? track->tr_album : NULL; artwork_timeout = status ? artwork_timeout : 0; g_free(path); return status ? G_SOURCE_CONTINUE : G_SOURCE_REMOVE; } void __gui_artwork_update_preview(GtkFileChooser *chooser, gpointer data) { GtkWidget *preview = gtk_file_chooser_get_preview_widget(chooser); gchar *path = gtk_file_chooser_get_preview_filename(chooser); __gui_artwork_set_path(GTK_IMAGE(preview), path, ARTWORK_PREVIEW_SIZE); g_free(path); } void __gui_artwork_select_cover(GtkButton *button) { struct track *track = audio_cur_track(); GtkWidget *preview, *dialog; GtkFileFilter *filter; gchar *path; if (!track) return; filter = gtk_file_filter_new(); preview = gtk_image_new_from_icon_name("", GTK_ICON_SIZE_DIALOG); dialog = gtk_file_chooser_dialog_new("Choose an image", gui_window(), GTK_FILE_CHOOSER_ACTION_OPEN, _("_Cancel"), GTK_RESPONSE_CANCEL, _("_Open"), GTK_RESPONSE_ACCEPT, NULL); gtk_file_filter_add_mime_type(filter, "image/*"); gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter); gtk_file_chooser_set_show_hidden(GTK_FILE_CHOOSER(dialog), true); gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(dialog), preview); gtk_file_chooser_set_use_preview_label(GTK_FILE_CHOOSER(dialog), false); g_signal_connect(dialog, "update-preview", (GCallback)__gui_artwork_update_preview, NULL); if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { path = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); gui_artwork_import(track, path); g_free(path); } gtk_widget_destroy(dialog); } void gui_artwork_set_cover(void) { if (__gui_artwork_timeout(NULL) != G_SOURCE_CONTINUE) return; artwork_timeout = g_timeout_add(2000, __gui_artwork_timeout, NULL); } void gui_artwork_import(struct track *track, gchar *path) { album_artwork_import(track->tr_album, path); if (track == audio_cur_track()) { artwork_current = NULL; gui_artwork_set_cover(); } }