improve image display, particularly for large images, especially if taller than they...
[claws.git] / src / gtk / gtkutils.c
index 04cccf7b6f2784fe7ccde12efcb8b32339e71b13..f09d5a84fd265456d37fa39b4102da88d29eaee2 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2018 Hiroyuki Yamamoto and the Claws Mail team
+ * Copyright (C) 1999-2020 the Claws Mail team and Hiroyuki Yamamoto
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -125,7 +125,7 @@ void gtkut_stock_button_add_help(GtkWidget *bbox, GtkWidget **help_btn)
 
        *help_btn = gtk_button_new_from_stock(GTK_STOCK_HELP);
 
-       gtkut_widget_set_can_default(*help_btn, TRUE);
+       gtk_widget_set_can_default(*help_btn, TRUE);
        gtk_box_pack_end(GTK_BOX (bbox), *help_btn, TRUE, TRUE, 0);
        gtk_button_box_set_child_secondary(GTK_BUTTON_BOX (bbox),
                        *help_btn, TRUE);
@@ -162,20 +162,20 @@ void gtkut_stock_button_set_create(GtkWidget **bbox,
        gtk_box_set_spacing(GTK_BOX(*bbox), 5);
 
        *button1 = gtk_button_new_from_stock(label1);
-       gtkut_widget_set_can_default(*button1, TRUE);
+       gtk_widget_set_can_default(*button1, TRUE);
        gtk_box_pack_start(GTK_BOX(*bbox), *button1, TRUE, TRUE, 0);
        gtk_widget_show(*button1);
 
        if (button2) {
                *button2 = gtk_button_new_from_stock(label2);
-               gtkut_widget_set_can_default(*button2, TRUE);
+               gtk_widget_set_can_default(*button2, TRUE);
                gtk_box_pack_start(GTK_BOX(*bbox), *button2, TRUE, TRUE, 0);
                gtk_widget_show(*button2);
        }
 
        if (button3) {
                *button3 = gtk_button_new_from_stock(label3);
-               gtkut_widget_set_can_default(*button3, TRUE);
+               gtk_widget_set_can_default(*button3, TRUE);
                gtk_box_pack_start(GTK_BOX(*bbox), *button3, TRUE, TRUE, 0);
                gtk_widget_show(*button3);
        }
@@ -196,7 +196,7 @@ void gtkut_stock_with_text_button_set_create(GtkWidget **bbox,
        *button1 = gtk_button_new_with_mnemonic(text1);
        gtk_button_set_image(GTK_BUTTON(*button1),
                gtk_image_new_from_stock(label1, GTK_ICON_SIZE_BUTTON));
-       gtkut_widget_set_can_default(*button1, TRUE);
+       gtk_widget_set_can_default(*button1, TRUE);
        gtk_box_pack_start(GTK_BOX(*bbox), *button1, TRUE, TRUE, 0);
        gtk_widget_show(*button1);
 
@@ -204,7 +204,7 @@ void gtkut_stock_with_text_button_set_create(GtkWidget **bbox,
                *button2 = gtk_button_new_with_mnemonic(text2);
                gtk_button_set_image(GTK_BUTTON(*button2),
                        gtk_image_new_from_stock(label2, GTK_ICON_SIZE_BUTTON));
-               gtkut_widget_set_can_default(*button2, TRUE);
+               gtk_widget_set_can_default(*button2, TRUE);
                gtk_box_pack_start(GTK_BOX(*bbox), *button2, TRUE, TRUE, 0);
                gtk_widget_show(*button2);
        }
@@ -213,7 +213,7 @@ void gtkut_stock_with_text_button_set_create(GtkWidget **bbox,
                *button3 = gtk_button_new_with_mnemonic(text3);
                gtk_button_set_image(GTK_BUTTON(*button3),
                        gtk_image_new_from_stock(label3, GTK_ICON_SIZE_BUTTON));
-               gtkut_widget_set_can_default(*button3, TRUE);
+               gtk_widget_set_can_default(*button3, TRUE);
                gtk_box_pack_start(GTK_BOX(*bbox), *button3, TRUE, TRUE, 0);
                gtk_widget_show(*button3);
        }
@@ -757,7 +757,7 @@ static gboolean move_bar_cb(gpointer data)
        if (!GTK_IS_PROGRESS_BAR(w)) {
                return FALSE;
        }
-
+       gtk_progress_bar_set_pulse_step(GTK_PROGRESS_BAR(w), 0.1);
        gtk_progress_bar_pulse(GTK_PROGRESS_BAR(w));
        GTK_EVENTS_FLUSH();
        return TRUE;
@@ -863,7 +863,7 @@ void gtkut_set_widget_bgcolor_rgb(GtkWidget *widget, guint rgbvalue)
        newstyle->bg[GTK_STATE_PRELIGHT] = gdk_color;
        newstyle->bg[GTK_STATE_ACTIVE]   = gdk_color;
        gtk_widget_set_style(widget, newstyle);
-       gtk_style_unref(newstyle);
+       g_object_unref(newstyle);
 }
   
 /*!
@@ -1224,10 +1224,8 @@ GtkWidget *gtkut_get_link_btn(GtkWidget *window, const gchar *url, const gchar *
 {
        GtkWidget *btn;
        GtkWidget *btn_label;
-#if !GTK_CHECK_VERSION(3, 0, 0)
        GdkColormap *cmap;
        gboolean success[2];
-#endif
        GdkColor uri_color[2] = {{0, 0, 0, 0xffff}, {0, 0xffff, 0, 0}};
        gchar *local_url = NULL;
        if (!url)
@@ -1241,11 +1239,9 @@ GtkWidget *gtkut_get_link_btn(GtkWidget *window, const gchar *url, const gchar *
        btn = gtk_button_new_with_label(label?label:url);
        gtk_button_set_relief(GTK_BUTTON(btn), GTK_RELIEF_NONE);
        btn_label = gtk_bin_get_child(GTK_BIN((btn)));
-#if !GTK_CHECK_VERSION(3, 0, 0)
        cmap = gdk_drawable_get_colormap(gtk_widget_get_window(window));
        gdk_colormap_alloc_colors(cmap, uri_color, 2, FALSE, TRUE, success);
        if (success[0] == TRUE && success[1] == TRUE) {
-#endif
                GtkStyle *style;
                gtk_widget_ensure_style(btn_label);
                style = gtk_style_copy
@@ -1254,11 +1250,9 @@ GtkWidget *gtkut_get_link_btn(GtkWidget *window, const gchar *url, const gchar *
                style->fg[GTK_STATE_ACTIVE]   = uri_color[1];
                style->fg[GTK_STATE_PRELIGHT] = uri_color[0];
                gtk_widget_set_style(btn_label, style);
-               gtk_style_unref(style);
-#if !GTK_CHECK_VERSION(3, 0, 0)
+               g_object_unref(style);
        } else
                g_warning("color allocation failed");
-#endif
 
        g_signal_connect(G_OBJECT(btn), "enter",
                         G_CALLBACK(link_btn_enter), window);
@@ -1508,12 +1502,14 @@ gboolean gtkut_tree_model_text_iter_prev(GtkTreeModel *model,
                gtk_tree_model_get(model, &cur_iter, 0, &cur_value, -1);
 
                if (strcmp(text, cur_value) == 0) {
+                       g_free(cur_value);
                        if (count <= 0)
                                return FALSE;
 
                        return gtk_tree_model_iter_nth_child(model, iter, NULL, count - 1);
                }
 
+               g_free(cur_value);
                valid = gtk_tree_model_iter_next(model, &cur_iter);
                count++;
        }
@@ -1700,78 +1696,6 @@ claws_input_add    (gint       source,
   return result;
 }
 
-void gtkut_widget_set_mapped(GtkWidget *widget, gboolean mapped)
-{
-#if GTK_CHECK_VERSION(2,20,0)
-       gtk_widget_set_mapped(widget, mapped);
-#else
-       if (mapped)
-               GTK_WIDGET_SET_FLAGS(widget, GTK_MAPPED);
-       else
-               GTK_WIDGET_UNSET_FLAGS(widget, GTK_MAPPED);
-#endif
-}
-
-void gtkut_widget_set_realized(GtkWidget *widget, gboolean realized)
-{
-#if GTK_CHECK_VERSION(2,20,0)
-       gtk_widget_set_realized(widget, realized);
-#else
-       if (realized)
-               GTK_WIDGET_SET_FLAGS(widget, GTK_REALIZED);
-       else
-               GTK_WIDGET_UNSET_FLAGS(widget, GTK_REALIZED);
-#endif
-}
-
-void gtkut_widget_set_can_default(GtkWidget *widget, gboolean can_default)
-{
-#if GTK_CHECK_VERSION(2,20,0)
-       gtk_widget_set_can_default(widget, can_default);
-#else
-       if (can_default)
-               GTK_WIDGET_SET_FLAGS(widget, GTK_CAN_DEFAULT);
-       else
-               GTK_WIDGET_UNSET_FLAGS(widget, GTK_CAN_DEFAULT);
-#endif
-}
-
-void gtkut_widget_set_receives_default(GtkWidget *widget, gboolean receives_default)
-{
-#if GTK_CHECK_VERSION(2,20,0)
-       gtk_widget_set_receives_default(widget, receives_default);
-#else
-       if (receives_default)
-               GTK_WIDGET_SET_FLAGS(widget, GTK_RECEIVES_DEFAULT);
-       else
-               GTK_WIDGET_UNSET_FLAGS(widget, GTK_RECEIVES_DEFAULT);
-#endif
-}
-
-void gtkut_widget_set_can_focus(GtkWidget *widget, gboolean can_focus)
-{
-#if GTK_CHECK_VERSION(2,20,0)
-       gtk_widget_set_can_focus(widget, can_focus);
-#else
-       if (can_focus)
-               GTK_WIDGET_SET_FLAGS(widget, GTK_CAN_FOCUS);
-       else
-               GTK_WIDGET_UNSET_FLAGS(widget, GTK_CAN_FOCUS);
-#endif
-}
-
-void gtkut_widget_set_has_window(GtkWidget *widget, gboolean has_window)
-{
-#if GTK_CHECK_VERSION(2,20,0)
-       gtk_widget_set_has_window(widget, has_window);
-#else
-       if (has_window) /* Inverted logic there */
-               GTK_WIDGET_UNSET_FLAGS(widget, GTK_NO_WINDOW);
-       else
-               GTK_WIDGET_SET_FLAGS(widget, GTK_NO_WINDOW);
-#endif
-}
-
 /**
  * Load a pixbuf fitting inside the specified size. EXIF orientation is
  * respected if available.
@@ -1826,32 +1750,6 @@ GdkPixbuf *claws_load_pixbuf_fitting(GdkPixbuf *src_pixbuf, int box_width,
                }
        }
 
-       w = gdk_pixbuf_get_width(pixbuf);
-       h = gdk_pixbuf_get_height(pixbuf);
-
-       if (angle == 90 || angle == 270) {
-               avail_height = box_width;
-               avail_width = box_height;
-       } else {
-               avail_width = box_width;
-               avail_height = box_height;
-       }
-
-       /* Scale first */
-       if (box_width != -1 && box_height != -1 && avail_width - 100 > 0) {
-               if (w > avail_width) {
-                       h = (avail_width * h) / w;
-                       w = avail_width;
-               }
-               if (h > avail_height) {
-                       w = (avail_height * w) / h;
-                       h = avail_height;
-               }
-               t_pixbuf = gdk_pixbuf_scale_simple(pixbuf, 
-                       w, h, GDK_INTERP_BILINEAR);
-               g_object_unref(pixbuf);
-               pixbuf = t_pixbuf;
-       }
 
        /* Rotate if needed */
        if (angle != 0) {
@@ -1874,10 +1772,27 @@ GdkPixbuf *claws_load_pixbuf_fitting(GdkPixbuf *src_pixbuf, int box_width,
                pixbuf = t_pixbuf;
        }
 
+       w = gdk_pixbuf_get_width(pixbuf);
+       h = gdk_pixbuf_get_height(pixbuf);
+
+       avail_width = box_width;
+       avail_height = box_height;
+               
+       if (box_width != -1 && box_height != -1 && avail_width - 100 > 0) {
+               if (w > avail_width || h > avail_height) {
+                       h = (avail_width * h) / w;
+                       w = avail_width;
+               }
+               t_pixbuf = gdk_pixbuf_scale_simple(pixbuf, 
+                       w, h, GDK_INTERP_BILINEAR);
+               g_object_unref(pixbuf);
+               pixbuf = t_pixbuf;
+       }
+
        return pixbuf;
 }
 
-#if (defined USE_GNUTLS && GLIB_CHECK_VERSION(2,22,0))
+#if defined USE_GNUTLS
 static void auto_configure_done(const gchar *hostname, gint port, gboolean ssl, AutoConfigureData *data)
 {
        gboolean smtp = strcmp(data->tls_service, "submission") == 0 ? TRUE : FALSE;
@@ -2059,3 +1974,144 @@ gboolean auto_configure_service_sync(const gchar *service, const gchar *domain,
        return result;
 }
 #endif
+
+gpointer gtkut_tree_view_get_selected_pointer(GtkTreeView *view,
+               gint column, GtkTreeModel **_model, GtkTreeSelection **_selection,
+               GtkTreeIter *_iter)
+{
+       GtkTreeIter iter;
+       GtkTreeModel *model;
+       GtkTreeSelection *sel;
+       gpointer ptr;
+       GType type;
+
+       cm_return_val_if_fail(view != NULL, NULL);
+       cm_return_val_if_fail(column >= 0, NULL);
+
+       model = gtk_tree_view_get_model(view);
+       if (_model != NULL)
+               *_model = model;
+
+       sel = gtk_tree_view_get_selection(view);
+       if (_selection != NULL)
+               *_selection = sel;
+
+       if (!gtk_tree_selection_get_selected(sel, NULL, &iter))
+               return NULL; /* No row selected */
+
+       if (_iter != NULL)
+               *_iter = iter;
+
+       if (gtk_tree_selection_count_selected_rows(sel) > 1)
+               return NULL; /* Can't work with multiselect */
+
+       cm_return_val_if_fail(
+                       gtk_tree_model_get_n_columns(model) > column,
+                       NULL);
+
+       type = gtk_tree_model_get_column_type(model, column);
+       cm_return_val_if_fail(
+                       type == G_TYPE_POINTER || type == G_TYPE_STRING,
+                       NULL);
+
+       gtk_tree_model_get(model, &iter, column, &ptr, -1);
+
+       return ptr;
+}
+
+static GList *get_predefined_times(void)
+{
+       int h,m;
+       GList *times = NULL;
+       for (h = 0; h < 24; h++) {
+               for (m = 0; m < 60; m += 15) {
+                       gchar *tmp = g_strdup_printf("%02d:%02d", h, m);
+                       times = g_list_append(times, tmp);
+               }
+       }
+       return times;
+}
+
+static int get_list_item_num(int h, int m)
+{
+       if (m % 15 != 0)
+               return -1;
+
+       return (h*4 + m/15);
+}
+
+GtkWidget *gtkut_time_select_combo_new()
+{
+       GtkWidget *combo = gtk_combo_box_text_new_with_entry();
+       GList *times = get_predefined_times();
+
+       gtk_combo_box_set_active(GTK_COMBO_BOX(combo), -1);
+       combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(combo), times);
+
+       list_free_strings_full(times);
+
+       return combo;
+}
+
+
+void gtkut_time_select_select_by_time(GtkComboBox *combo, int hour, int minute)
+{
+       gchar *time_text = g_strdup_printf("%02d:%02d", hour, minute);
+       gint num = get_list_item_num(hour, minute);
+
+       if (num > -1)
+               combobox_select_by_text(combo, time_text);
+       else
+               gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo))), time_text);
+
+       g_free(time_text);
+}
+
+static void get_time_from_combo(GtkComboBox *combo, int *h, int *m)
+{
+       gchar *tmp;
+       gchar **parts;
+
+       if (!h || !m) 
+               return;
+
+       tmp = gtk_editable_get_chars(GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(combo))), 0, -1);
+       parts = g_strsplit(tmp, ":", 2);
+       if (parts[0] && parts[1] && *parts[0] && *parts[1]) {
+               *h = atoi(parts[0]);
+               *m = atoi(parts[1]);
+       }
+       g_strfreev(parts);
+       g_free(tmp);
+}
+
+gboolean gtkut_time_select_get_time(GtkComboBox *combo, int *hour, int *minute)
+{
+       const gchar *value = gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo))));
+
+       if (value == NULL || strlen(value) != 5)
+               return FALSE;
+
+       if (hour == NULL || minute == NULL)
+               return FALSE;
+
+       get_time_from_combo(combo, hour, minute);
+
+       if (*hour < 0 || *hour > 23)
+               return FALSE;
+       if (*minute < 0 || *minute > 59)
+               return FALSE;
+
+       return TRUE;
+}
+
+void gtk_calendar_select_today(GtkCalendar *calendar)
+{
+       time_t t = time (NULL);
+       struct tm buft;
+       struct tm *lt = localtime_r (&t, &buft);
+
+       mktime(lt);
+       gtk_calendar_select_day(calendar, lt->tm_mday);
+       gtk_calendar_select_month(calendar, lt->tm_mon, lt->tm_year + 1900);
+}