~ubuntu-branches/ubuntu/trusty/gthumb/trusty

« back to all changes in this revision

Viewing changes to gthumb/gtk-utils.c

  • Committer: Package Import Robot
  • Author(s): Jackson Doak
  • Date: 2013-08-31 06:24:51 UTC
  • mfrom: (1.3.17) (5.2.24 sid)
  • Revision ID: package-import@ubuntu.com-20130831062451-0958soi720z0byp6
Tags: 3:3.2.5-1
* New upstream release. Closes: #716729 LP: #1201073
* Set myself as maintainer. Closes: #711827
* debian/control:
  - Build-depend on yelp-tools, libgstreamer1.0-dev, libsecret-1-dev,
    libgstreamer-plugins-base1.0-dev, libwebp-dev
  - Bump standards-version to 3.9.4
  - Bump debhelper to 8
  - Make git URLs canonical
  - Drop depend on libiptcdata0-dev. Closes: #697087
  - Drop depend on gnome keyring since libsecret replaces it
* debian/compat: Set as 8
* debian/rules: 
  - Add --enable-libwebp for WebP support
  - Add --disable-silent-rules for verbose building
  - Enable hardening
* Create debian/source/options
* Refresh all patches so they apply cleanly

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
#include <config.h>
23
23
#include <string.h>
 
24
#include "gth-image-utils.h"
24
25
#include "gtk-utils.h"
25
 
#include "pixbuf-utils.h"
 
26
 
26
27
 
27
28
#define REQUEST_ENTRY_WIDTH_IN_CHARS 40
 
29
#define GTHUMB_RESOURCE_BASE_PATH "/org/gnome/gThumb/resources/"
28
30
 
29
31
 
30
32
void
121
123
                         const char       *first_button_text,
122
124
                         ...)
123
125
{
124
 
        GtkWidget    *d;
125
 
        GtkWidget    *label;
126
 
        GtkWidget    *image;
127
 
        GtkWidget    *hbox;
128
 
        va_list       args;
129
 
        const gchar  *text;
130
 
        int           response_id;
131
 
        char         *escaped_message, *markup_text;
132
 
 
133
 
        g_return_val_if_fail (message != NULL, NULL);
134
 
 
135
 
        if (stock_id == NULL)
136
 
                stock_id = GTK_STOCK_DIALOG_INFO;
137
 
 
138
 
        d = gtk_dialog_new_with_buttons ("", parent, flags, NULL, NULL);
139
 
        gtk_window_set_resizable (GTK_WINDOW (d), FALSE);
 
126
        GtkBuilder  *builder;
 
127
        GtkWidget   *dialog;
 
128
        GtkWidget   *label;
 
129
        va_list      args;
 
130
        const gchar *text;
 
131
        int          response_id;
 
132
        char        *markup_text;
 
133
 
 
134
        builder = _gtk_builder_new_from_resource ("message-dialog.ui");
 
135
        dialog = _gtk_builder_get_widget (builder, "message_dialog");
 
136
        gtk_window_set_title (GTK_WINDOW (dialog), "");
 
137
        gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
 
138
        gtk_window_set_modal (GTK_WINDOW (dialog), (flags & GTK_DIALOG_MODAL));
 
139
        gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), (flags & GTK_DIALOG_DESTROY_WITH_PARENT));
 
140
        g_object_weak_ref (G_OBJECT (dialog), (GWeakNotify) g_object_unref, builder);
 
141
 
140
142
        if (flags & GTK_DIALOG_MODAL)
141
 
                _gtk_dialog_add_to_window_group (GTK_DIALOG (d));
142
 
 
143
 
        gtk_container_set_border_width (GTK_CONTAINER (d), 6);
144
 
        gtk_container_set_border_width (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (d))), 6);
145
 
        gtk_box_set_spacing (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (d))), 8);
146
 
 
147
 
        /* Add label and image */
148
 
 
149
 
        image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_DIALOG);
150
 
        gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0.0);
151
 
 
152
 
        label = gtk_label_new ("");
153
 
 
154
 
        escaped_message = g_markup_escape_text (message, -1);
155
 
        if (secondary_message != NULL) {
156
 
                char *escaped_secondary_message = g_markup_escape_text (secondary_message, -1);
157
 
                markup_text = g_strdup_printf ("<span weight=\"bold\" size=\"larger\">%s</span>\n\n%s",
158
 
                                               escaped_message,
159
 
                                               escaped_secondary_message);
160
 
                g_free (escaped_secondary_message);
 
143
                _gtk_dialog_add_to_window_group (GTK_DIALOG (dialog));
 
144
 
 
145
        /* set the icon */
 
146
 
 
147
        gtk_image_set_from_stock (GTK_IMAGE (_gtk_builder_get_widget (builder, "icon_image")),
 
148
                                  stock_id,
 
149
                                  GTK_ICON_SIZE_DIALOG);
 
150
 
 
151
        /* set the message */
 
152
 
 
153
        label = _gtk_builder_get_widget (builder, "message_label");
 
154
 
 
155
        if (message != NULL) {
 
156
                char *escaped_message;
 
157
 
 
158
                escaped_message = g_markup_escape_text (message, -1);
 
159
                if (secondary_message != NULL) {
 
160
                        char *escaped_secondary_message;
 
161
 
 
162
                        escaped_secondary_message = g_markup_escape_text (secondary_message, -1);
 
163
                        markup_text = g_strdup_printf ("<span weight=\"bold\" size=\"larger\">%s</span>\n\n%s",
 
164
                                                       escaped_message,
 
165
                                                       escaped_secondary_message);
 
166
 
 
167
                        g_free (escaped_secondary_message);
 
168
                }
 
169
                else
 
170
                        markup_text = g_strdup_printf ("<span weight=\"bold\" size=\"larger\">%s</span>", escaped_message);
 
171
 
 
172
                g_free (escaped_message);
161
173
        }
162
174
        else
163
 
                markup_text = g_strdup (escaped_message);
 
175
                markup_text = g_markup_escape_text (secondary_message, -1);
 
176
 
164
177
        gtk_label_set_markup (GTK_LABEL (label), markup_text);
165
178
        g_free (markup_text);
166
 
        g_free (escaped_message);
167
 
 
168
 
        gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
169
 
        gtk_label_set_selectable (GTK_LABEL (label), TRUE);
170
 
 
171
 
        hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 18);
172
 
        gtk_container_set_border_width (GTK_CONTAINER (hbox), 6);
173
 
 
174
 
        gtk_box_pack_start (GTK_BOX (hbox), image,
175
 
                            FALSE, FALSE, 0);
176
 
 
177
 
        gtk_box_pack_start (GTK_BOX (hbox), label,
178
 
                            TRUE, TRUE, 0);
179
 
 
180
 
        gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (d))),
181
 
                            hbox,
182
 
                            FALSE, FALSE, 0);
183
 
 
184
 
        gtk_widget_show_all (hbox);
185
 
 
186
 
        /* Add buttons */
 
179
 
 
180
        /* add the buttons */
187
181
 
188
182
        if (first_button_text == NULL)
189
 
                return d;
 
183
                return dialog;
190
184
 
191
185
        va_start (args, first_button_text);
192
186
 
194
188
        response_id = va_arg (args, gint);
195
189
 
196
190
        while (text != NULL) {
197
 
                gtk_dialog_add_button (GTK_DIALOG (d), text, response_id);
 
191
                gtk_dialog_add_button (GTK_DIALOG (dialog), text, response_id);
198
192
 
199
 
                text = va_arg (args, gchar*);
 
193
                text = va_arg (args, char*);
200
194
                if (text == NULL)
201
195
                        break;
202
196
                response_id = va_arg (args, int);
204
198
 
205
199
        va_end (args);
206
200
 
207
 
        gtk_dialog_set_default_response (GTK_DIALOG (d), GTK_RESPONSE_YES);
208
 
 
209
 
        return d;
 
201
        return dialog;
210
202
}
211
203
 
212
204
 
417
409
}
418
410
 
419
411
 
420
 
static GdkPixbuf *
421
 
get_themed_icon_pixbuf (GThemedIcon  *icon,
422
 
                        int           size,
423
 
                        GtkIconTheme *icon_theme)
424
 
{
425
 
        char        **icon_names;
426
 
        GtkIconInfo  *icon_info;
427
 
        GdkPixbuf    *pixbuf;
428
 
        GError       *error = NULL;
429
 
 
430
 
        g_object_get (icon, "names", &icon_names, NULL);
431
 
 
432
 
        if (icon_theme == NULL)
433
 
                icon_theme = gtk_icon_theme_get_default ();
434
 
        icon_info = gtk_icon_theme_choose_icon (icon_theme, (const char **)icon_names, size, 0);
435
 
        if (icon_info == NULL)
436
 
                icon_info = gtk_icon_theme_lookup_icon (icon_theme, "text-x-generic", size, GTK_ICON_LOOKUP_USE_BUILTIN);
437
 
 
438
 
        pixbuf = gtk_icon_info_load_icon (icon_info, &error);
439
 
        if (pixbuf == NULL) {
440
 
                g_warning ("could not load icon pixbuf: %s\n", error->message);
441
 
                g_clear_error (&error);
442
 
        }
443
 
 
444
 
        gtk_icon_info_free (icon_info);
445
 
        g_strfreev (icon_names);
446
 
 
447
 
        return pixbuf;
448
 
}
449
 
 
450
 
 
451
 
static GdkPixbuf *
452
 
get_file_icon_pixbuf (GFileIcon *icon,
453
 
                      int        size)
454
 
{
455
 
        GdkPixbuf *pixbuf = NULL;
456
 
        GFile     *file;
457
 
 
458
 
        file = g_file_icon_get_file (icon);
459
 
        if (file != NULL) {
460
 
                char *filename;
461
 
 
462
 
                filename = g_file_get_path (file);
463
 
                if (filename != NULL)
464
 
                        pixbuf = gdk_pixbuf_new_from_file_at_size (filename, size, -1, NULL);
465
 
 
466
 
                g_free (filename);
467
 
        }
468
 
 
469
 
        return pixbuf;
470
 
}
471
 
 
472
 
 
473
 
GdkPixbuf *
474
 
_g_icon_get_pixbuf (GIcon        *icon,
475
 
                    int           size,
476
 
                    GtkIconTheme *theme)
477
 
{
478
 
        GdkPixbuf *pixbuf;
479
 
        int        w, h;
480
 
 
481
 
        if (icon == NULL)
482
 
                return NULL;
483
 
 
484
 
        pixbuf = NULL;
485
 
        if (G_IS_THEMED_ICON (icon))
486
 
                pixbuf = get_themed_icon_pixbuf (G_THEMED_ICON (icon), size, theme);
487
 
        else if (G_IS_FILE_ICON (icon))
488
 
                pixbuf = get_file_icon_pixbuf (G_FILE_ICON (icon), size);
489
 
 
490
 
        if (pixbuf == NULL)
491
 
                return NULL;
492
 
 
493
 
        w = gdk_pixbuf_get_width (pixbuf);
494
 
        h = gdk_pixbuf_get_height (pixbuf);
495
 
        if (scale_keeping_ratio (&w, &h, size, size, FALSE)) {
496
 
                GdkPixbuf *scaled;
497
 
 
498
 
                scaled = gdk_pixbuf_scale_simple (pixbuf, w, h, GDK_INTERP_BILINEAR);
499
 
                g_object_unref (pixbuf);
500
 
                pixbuf = scaled;
501
 
        }
502
 
 
503
 
        return pixbuf;
504
 
}
505
 
 
506
 
 
507
412
void
508
413
_gtk_dialog_add_to_window_group (GtkDialog *dialog)
509
414
{
518
423
 
519
424
 
520
425
GdkPixbuf *
 
426
_g_icon_get_pixbuf (GIcon        *icon,
 
427
                    int           icon_size,
 
428
                    GtkIconTheme *icon_theme)
 
429
{
 
430
        GdkPixbuf   *pixbuf = NULL;
 
431
        GtkIconInfo *icon_info;
 
432
 
 
433
        if (icon_theme == NULL)
 
434
                icon_theme = gtk_icon_theme_get_default ();
 
435
 
 
436
        icon_info = gtk_icon_theme_lookup_by_gicon (icon_theme,
 
437
                                                    icon,
 
438
                                                    icon_size,
 
439
                                                    GTK_ICON_LOOKUP_USE_BUILTIN);
 
440
 
 
441
        if (icon_info != NULL) {
 
442
                GError *error = NULL;
 
443
 
 
444
                pixbuf = gtk_icon_info_load_icon (icon_info, &error);
 
445
                if (error != NULL) {
 
446
                        g_print ("%s\n", error->message);
 
447
                        g_error_free (error);
 
448
                }
 
449
 
 
450
                gtk_icon_info_free (icon_info);
 
451
        }
 
452
 
 
453
        return pixbuf;
 
454
}
 
455
 
 
456
 
 
457
GdkPixbuf *
521
458
get_mime_type_pixbuf (const char   *mime_type,
522
459
                      int           icon_size,
523
460
                      GtkIconTheme *icon_theme)
536
473
}
537
474
 
538
475
 
539
 
int
540
 
_gtk_icon_get_pixel_size (GtkWidget   *widget,
541
 
                          GtkIconSize  size)
542
 
{
543
 
        int icon_width, icon_height;
544
 
 
545
 
        gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (widget),
546
 
                                           size,
547
 
                                           &icon_width, &icon_height);
548
 
        return MAX (icon_width, icon_height);
549
 
}
550
 
 
551
 
 
552
476
void
553
477
show_help_dialog (GtkWindow  *parent,
554
478
                  const char *section)
556
480
        char   *uri;
557
481
        GError *error = NULL;
558
482
 
559
 
        uri = g_strconcat ("ghelp:gthumb", section ? "?" : NULL, section, NULL);
 
483
        uri = g_strconcat ("help:gthumb", section ? "/" : NULL, section, NULL);
560
484
        if (! gtk_show_uri (gtk_window_get_screen (parent), uri, GDK_CURRENT_TIME, &error)) {
561
485
                GtkWidget *dialog;
562
486
 
673
597
}
674
598
 
675
599
 
 
600
GtkBuilder *
 
601
_gtk_builder_new_from_resource (const char *resource_path)
 
602
{
 
603
        GtkBuilder *builder;
 
604
        char       *full_path;
 
605
        GError     *error = NULL;
 
606
 
 
607
        builder = gtk_builder_new ();
 
608
        full_path = g_strconcat (GTHUMB_RESOURCE_BASE_PATH, resource_path, NULL);
 
609
        if (! gtk_builder_add_from_resource (builder, full_path, &error)) {
 
610
                g_warning ("%s\n", error->message);
 
611
                g_clear_error (&error);
 
612
        }
 
613
        g_free (full_path);
 
614
 
 
615
        return builder;
 
616
}
 
617
 
 
618
 
676
619
GtkWidget *
677
620
_gtk_builder_get_widget (GtkBuilder *builder,
678
621
                         const char *name)
805
748
}
806
749
 
807
750
 
 
751
int
 
752
_gtk_widget_lookup_for_size (GtkWidget   *widget,
 
753
                             GtkIconSize  icon_size)
 
754
{
 
755
        int w, h;
 
756
        gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (widget),
 
757
                                           icon_size,
 
758
                                           &w, &h);
 
759
        return MAX (w, h);
 
760
}
 
761
 
 
762
 
808
763
void
809
764
_gtk_tree_path_list_free (GList *list)
810
765
{
816
771
int
817
772
_gtk_paned_get_position2 (GtkPaned *paned)
818
773
{
819
 
        int             pos;
820
 
        GtkWidget      *w;
821
 
        GtkRequisition  requisition;
822
 
        int             size;
 
774
        int           pos;
 
775
        GtkAllocation allocation;
 
776
        int           size;
823
777
 
824
778
        if (! gtk_widget_get_visible (GTK_WIDGET (paned)))
825
779
                return 0;
828
782
        if (pos == 0)
829
783
                return 0;
830
784
 
831
 
        w = gtk_widget_get_toplevel (GTK_WIDGET (paned));
832
 
        if (! gtk_widget_is_toplevel (w))
833
 
                return 0;
834
 
 
835
 
        gtk_window_get_size (GTK_WINDOW (w), &(requisition.width), &(requisition.height));
 
785
        gtk_widget_get_allocation (GTK_WIDGET (paned), &allocation);
836
786
        if (gtk_orientable_get_orientation (GTK_ORIENTABLE (paned)) == GTK_ORIENTATION_HORIZONTAL)
837
 
                size = requisition.width;
 
787
                size = allocation.width;
838
788
        else
839
 
                size = requisition.height;
 
789
                size = allocation.height;
840
790
 
841
791
        if (size == 0)
842
792
                return 0;
846
796
 
847
797
 
848
798
void
849
 
_gtk_paned_set_position2 (GtkPaned *paned,
850
 
                          int       pos)
851
 
{
852
 
        GtkWidget     *top_level;
853
 
        GtkAllocation  allocation;
854
 
        int            size;
855
 
 
856
 
        top_level = gtk_widget_get_toplevel (GTK_WIDGET (paned));
857
 
        if (! gtk_widget_is_toplevel (top_level))
858
 
                return;
859
 
 
860
 
        gtk_widget_get_allocation (top_level, &allocation);
861
 
        if (gtk_orientable_get_orientation (GTK_ORIENTABLE (paned)) == GTK_ORIENTATION_HORIZONTAL)
862
 
                size = allocation.width;
863
 
        else
864
 
                size = allocation.height;
865
 
 
866
 
        if (pos > 0)
867
 
                gtk_paned_set_position (paned, size - pos);
868
 
}
869
 
 
870
 
 
871
 
void
872
799
_g_launch_command (GtkWidget  *parent,
873
800
                   const char *command,
874
801
                   const char *name,
1036
963
}
1037
964
 
1038
965
 
1039
 
GtkAdjustment *
1040
 
gimp_scale_entry_new (GtkWidget *parent_box,
1041
 
                      GtkLabel  *label,
1042
 
                      float      value,
1043
 
                      float      lower,
1044
 
                      float      upper,
1045
 
                      float      step_increment,
1046
 
                      float      page_increment,
1047
 
                      int        digits)
1048
 
{
1049
 
        GtkAdjustment *adj;
1050
 
        GtkWidget     *spinbutton;
1051
 
        GtkWidget     *scale;
1052
 
        GtkWidget     *hbox;
1053
 
 
1054
 
        adj = gtk_adjustment_new (value, lower, upper,
1055
 
                                  step_increment, page_increment,
1056
 
                                  0.0);
1057
 
 
1058
 
        spinbutton = gtk_spin_button_new (GTK_ADJUSTMENT (adj), page_increment, 0);
1059
 
        gtk_spin_button_set_digits (GTK_SPIN_BUTTON (spinbutton), digits);
1060
 
        gtk_entry_set_width_chars (GTK_ENTRY (spinbutton), 4);
1061
 
 
1062
 
        scale = gtk_scale_new (GTK_ORIENTATION_HORIZONTAL, GTK_ADJUSTMENT (adj));
1063
 
        gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE);
1064
 
        gtk_scale_set_digits (GTK_SCALE (scale), digits);
1065
 
 
1066
 
        hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
1067
 
        gtk_box_pack_start (GTK_BOX (hbox), scale, TRUE, TRUE, 0);
1068
 
        gtk_box_pack_start (GTK_BOX (hbox), spinbutton, FALSE, FALSE, 0);
1069
 
 
1070
 
        gtk_box_pack_start (GTK_BOX (parent_box), hbox, TRUE, TRUE, 0);
1071
 
        gtk_widget_show_all (hbox);
1072
 
 
1073
 
        if (label != NULL)
1074
 
                gtk_label_set_mnemonic_widget (label, scale);
1075
 
 
1076
 
        return (GtkAdjustment *) adj;
1077
 
}
1078
 
 
1079
 
 
1080
966
static gboolean
1081
967
_gdk_rgba_shade (GdkRGBA *color,
1082
968
                 GdkRGBA *result,
1112
998
{
1113
999
        return _gdk_rgba_shade (color, result, 1.2);
1114
1000
}
 
1001
 
 
1002
 
 
1003
GtkIconTheme *
 
1004
_gtk_widget_get_icon_theme (GtkWidget *widget)
 
1005
{
 
1006
        GdkScreen *screen;
 
1007
 
 
1008
        screen = gtk_widget_get_screen (widget);
 
1009
        if (screen == NULL)
 
1010
                return NULL;
 
1011
 
 
1012
        return gtk_icon_theme_get_for_screen (screen);
 
1013
}
 
1014
 
 
1015
 
 
1016
void
 
1017
_gtk_combo_box_add_image_sizes (GtkComboBox *combo_box,
 
1018
                                int          active_width,
 
1019
                                int          active_height)
 
1020
{
 
1021
        GtkListStore *list_store;
 
1022
        int           active_index;
 
1023
        int           i;
 
1024
 
 
1025
        list_store = GTK_LIST_STORE (gtk_combo_box_get_model (combo_box));
 
1026
        active_index = 0;
 
1027
        for (i = 0; i < G_N_ELEMENTS (ImageSizeValues); i++) {
 
1028
                GtkTreeIter  iter;
 
1029
                char        *name;
 
1030
 
 
1031
                gtk_list_store_append (list_store, &iter);
 
1032
 
 
1033
                if ((ImageSizeValues[i].width == active_width) && (ImageSizeValues[i].height == active_height))
 
1034
                        active_index = i;
 
1035
 
 
1036
                /* Translators: this is an image size, such as 1024 × 768 */
 
1037
                name = g_strdup_printf (_("%d × %d"), ImageSizeValues[i].width, ImageSizeValues[i].height);
 
1038
                gtk_list_store_set (list_store, &iter,
 
1039
                                    0, name,
 
1040
                                    -1);
 
1041
 
 
1042
                g_free (name);
 
1043
        }
 
1044
        gtk_combo_box_set_active (combo_box, active_index);
 
1045
}
 
1046
 
 
1047
 
 
1048
gboolean
 
1049
_gtk_file_chooser_set_file_parent (GtkFileChooser   *chooser,
 
1050
                                   GFile            *file,
 
1051
                                   GError          **error)
 
1052
{
 
1053
        GFile *parent;
 
1054
        gboolean result;
 
1055
 
 
1056
        parent = g_file_get_parent (file);
 
1057
        result = gtk_file_chooser_set_file (chooser, parent, error);
 
1058
 
 
1059
        if (parent != NULL)
 
1060
                g_object_unref (parent);
 
1061
 
 
1062
        return result;
 
1063
}
 
1064