From 8451ca3b8a96f0a79fdbbe836763e83e4f667509 Mon Sep 17 00:00:00 2001 From: Tomasz Wasilczyk Date: Thu, 3 Apr 2014 23:53:45 +0200 Subject: Smiley manager: fill TODOs, do some polishing --- pidgin/gtksmiley-manager.c | 55 +++++++++++++++++++++++++++------------------- pidgin/gtkutils.c | 47 +++++++++++++++++++++++++++++++++++++++ pidgin/gtkutils.h | 4 ++++ 3 files changed, 83 insertions(+), 23 deletions(-) (limited to 'pidgin') diff --git a/pidgin/gtksmiley-manager.c b/pidgin/gtksmiley-manager.c index d40c6bb952..bff8e90f05 100644 --- a/pidgin/gtksmiley-manager.c +++ b/pidgin/gtksmiley-manager.c @@ -21,7 +21,6 @@ */ #include "internal.h" -#include "pidgin.h" #include "debug.h" #include "http.h" @@ -32,7 +31,6 @@ #include "gtksmiley-manager.h" #include "gtkutils.h" -#include "gtkwebview.h" #include "pidginstock.h" #include "gtk3compat.h" @@ -98,23 +96,21 @@ edit_dialog_update_thumb(SmileyEditDialog *edit_dialog) { GdkPixbuf *pixbuf = NULL; - /* TODO: don't scale if smaller */ - if (edit_dialog->filename) { - pixbuf = pidgin_pixbuf_new_from_file_at_scale( - edit_dialog->filename, 64, 64, TRUE); + if (edit_dialog->new_image) { + pixbuf = pidgin_pixbuf_from_imgstore(edit_dialog->new_image); + } else if (edit_dialog->filename) { + pixbuf = pidgin_pixbuf_new_from_file(edit_dialog->filename); if (!pixbuf) { g_free(edit_dialog->filename); edit_dialog->filename = NULL; } - } else if (edit_dialog->new_image) { - GdkPixbuf *tmp; - tmp = pidgin_pixbuf_from_imgstore(edit_dialog->new_image); - if (tmp) { - pixbuf = gdk_pixbuf_scale_simple(tmp, - 64, 64, GDK_INTERP_HYPER); - g_object_unref(tmp); - } } + + if (pixbuf) { + pixbuf = pidgin_pixbuf_scale_down(pixbuf, 64, 64, + GDK_INTERP_HYPER, TRUE); + } + if (!pixbuf) { GtkIconSize icon_size = gtk_icon_size_from_name(PIDGIN_ICON_SIZE_TANGO_SMALL); @@ -222,6 +218,11 @@ pidgin_smiley_edit_dialog_set_image(SmileyEditDialog *edit_dialog, if (edit_dialog->new_image) purple_imgstore_unref(edit_dialog->new_image); + if (edit_dialog->smiley) { + g_object_set_data(G_OBJECT(edit_dialog->smiley), + "pidgin-smiley-manager-list-thumb", NULL); + } + /* check, if image is valid */ if (image) tmp = pidgin_pixbuf_from_imgstore(image); @@ -394,9 +395,11 @@ pidgin_smiley_edit(SmileyManager *manager, PurpleSmiley *smiley) pidgin_set_accessible_label(GTK_WIDGET(edit_dialog->shortcut), label); #if GTK_CHECK_VERSION(3,0,0) - gtk_grid_attach_next_to(GTK_GRID(hbox), GTK_WIDGET(edit_dialog->shortcut), NULL, GTK_POS_RIGHT, 1, 1); + gtk_grid_attach_next_to(GTK_GRID(hbox), + GTK_WIDGET(edit_dialog->shortcut), NULL, GTK_POS_RIGHT, 1, 1); #else - gtk_box_pack_end(GTK_BOX(hbox), GTK_WIDGET(edit_dialog->shortcut), FALSE, FALSE, 0); + gtk_box_pack_end(GTK_BOX(hbox), GTK_WIDGET(edit_dialog->shortcut), + FALSE, FALSE, 0); #endif gtk_widget_show(GTK_WIDGET(edit_dialog->shortcut)); @@ -446,7 +449,8 @@ smiley_list_dnd_url_got(PurpleHttpConnection *http_conn, return; image_data = purple_http_response_get_data(response, &image_size); - image = purple_imgstore_new((gpointer)image_data, image_size, NULL); + image = purple_imgstore_new(g_memdup(image_data, image_size), + image_size, NULL); if (!image) return; @@ -572,9 +576,17 @@ pidgin_smiley_manager_list_add(SmileyManager *manager, PurpleSmiley *smiley) GdkPixbuf *smiley_image; GtkTreeIter iter; - /* TODO: maybe some cache? */ - smiley_image = pidgin_pixbuf_new_from_file_at_scale( - purple_smiley_get_path(smiley), 22, 22, TRUE); + smiley_image = g_object_get_data(G_OBJECT(smiley), + "pidgin-smiley-manager-list-thumb"); + if (smiley_image == NULL) { + smiley_image = pidgin_pixbuf_new_from_file( + purple_smiley_get_path(smiley)); + smiley_image = pidgin_pixbuf_scale_down(smiley_image, + 22, 22, GDK_INTERP_BILINEAR, TRUE); + g_object_set_data_full(G_OBJECT(smiley), + "pidgin-smiley-manager-list-thumb", + smiley_image, g_object_unref); + } gtk_list_store_append(manager->model, &iter); gtk_list_store_set(manager->model, &iter, @@ -582,9 +594,6 @@ pidgin_smiley_manager_list_add(SmileyManager *manager, PurpleSmiley *smiley) SMILEY_LIST_MODEL_SHORTCUT, purple_smiley_get_shortcut(smiley), SMILEY_LIST_MODEL_PURPLESMILEY, smiley, -1); - - if (smiley_image) - g_object_unref(smiley_image); } static void diff --git a/pidgin/gtkutils.c b/pidgin/gtkutils.c index 3837a99bb9..61151b20bd 100644 --- a/pidgin/gtkutils.c +++ b/pidgin/gtkutils.c @@ -3173,6 +3173,53 @@ GdkPixbuf *pidgin_pixbuf_new_from_file_at_scale(const char *filename, int width, return pixbuf; } +GdkPixbuf * +pidgin_pixbuf_scale_down(GdkPixbuf *src, guint max_width, guint max_height, + GdkInterpType interp_type, gboolean preserve_ratio) +{ + guint cur_w, cur_h; + GdkPixbuf *dst; + + g_return_val_if_fail(src != NULL, NULL); + + if (max_width == 0 || max_height == 0) { + g_object_unref(src); + g_return_val_if_reached(NULL); + } + + cur_w = gdk_pixbuf_get_width(src); + cur_h = gdk_pixbuf_get_width(src); + + if (cur_w <= max_width && cur_h <= max_height) + return src; + + /* cur_ratio = cur_w / cur_h + * max_ratio = max_w / max_h + */ + + if (!preserve_ratio) { + cur_w = MIN(cur_w, max_width); + cur_h = MIN(cur_h, max_height); + } else if ((guint64)cur_w * max_height > (guint64)max_width * cur_h) { + /* cur_w / cur_h > max_width / max_height */ + cur_h = (guint64)max_width * cur_h / cur_w; + cur_w = max_width; + } else { + cur_w = (guint64)max_height * cur_w / cur_h; + cur_h = max_height; + } + + if (cur_w <= 0) + cur_w = 1; + if (cur_h <= 0) + cur_h = 1; + + dst = gdk_pixbuf_scale_simple(src, cur_w, cur_h, interp_type); + g_object_unref(src); + + return dst; +} + static void url_copy(GtkWidget *w, gchar *url) { diff --git a/pidgin/gtkutils.h b/pidgin/gtkutils.h index cc201cf1bf..0e6d4cf423 100644 --- a/pidgin/gtkutils.h +++ b/pidgin/gtkutils.h @@ -945,6 +945,10 @@ GdkPixbuf *pidgin_pixbuf_new_from_file_at_size(const char *filename, int width, */ GdkPixbuf *pidgin_pixbuf_new_from_file_at_scale(const char *filename, int width, int height, gboolean preserve_aspect_ratio); +GdkPixbuf * +pidgin_pixbuf_scale_down(GdkPixbuf *src, guint max_width, guint max_height, + GdkInterpType interp_type, gboolean preserve_ratio); + /** * pidgin_make_scrollable: * @child: The child widget -- cgit v1.2.1