1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * gimpcompressioncombobox.c
5  * Copyright (C) 2004, 2008  Sven Neumann <sven@gimp.org>
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
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  */
20 
21 #include "config.h"
22 
23 #include "stdlib.h"
24 
25 #include <gegl.h>
26 #include <gtk/gtk.h>
27 
28 #include "libgimpwidgets/gimpwidgets.h"
29 
30 #include "widgets-types.h"
31 
32 #include "gimpcompressioncombobox.h"
33 
34 #include "gimp-intl.h"
35 
36 
37 enum
38 {
39   COLUMN_ID,
40   COLUMN_LABEL,
41   N_COLUMNS
42 };
43 
44 
45 /*  local function prototypes  */
46 
47 static void       gimp_compression_combo_box_constructed    (GObject      *object);
48 
49 static gboolean   gimp_compression_combo_box_separator_func (GtkTreeModel *model,
50                                                              GtkTreeIter  *iter,
51                                                              gpointer      data);
52 
53 
G_DEFINE_TYPE(GimpCompressionComboBox,gimp_compression_combo_box,GIMP_TYPE_STRING_COMBO_BOX)54 G_DEFINE_TYPE (GimpCompressionComboBox, gimp_compression_combo_box,
55                GIMP_TYPE_STRING_COMBO_BOX)
56 
57 #define parent_class gimp_compression_combo_box_parent_class
58 
59 
60 /*  private functions  */
61 
62 static void
63 gimp_compression_combo_box_class_init (GimpCompressionComboBoxClass *klass)
64 {
65   GObjectClass *object_class = G_OBJECT_CLASS (klass);
66 
67   object_class->constructed = gimp_compression_combo_box_constructed;
68 }
69 
70 static void
gimp_compression_combo_box_init(GimpCompressionComboBox * combo_box)71 gimp_compression_combo_box_init (GimpCompressionComboBox *combo_box)
72 {
73 }
74 
75 static void
gimp_compression_combo_box_constructed(GObject * object)76 gimp_compression_combo_box_constructed (GObject *object)
77 {
78   GimpCompressionComboBox *combo_box = GIMP_COMPRESSION_COMBO_BOX (object);
79   GtkCellLayout           *layout;
80   GtkCellRenderer         *cell;
81   GtkListStore            *store;
82   GtkTreeIter              iter;
83 
84   G_OBJECT_CLASS (parent_class)->constructed (object);
85 
86   store = gtk_list_store_new (N_COLUMNS,
87                               G_TYPE_STRING,   /* ID    */
88                               G_TYPE_STRING);  /* LABEL */
89 
90   gtk_combo_box_set_model (GTK_COMBO_BOX (combo_box), GTK_TREE_MODEL (store));
91   g_object_unref (store);
92 
93   gtk_combo_box_set_row_separator_func (
94     GTK_COMBO_BOX (combo_box),
95     gimp_compression_combo_box_separator_func,
96     NULL,
97     NULL);
98 
99   gtk_list_store_append (store, &iter);
100   gtk_list_store_set    (store, &iter,
101                          COLUMN_ID,    "none",
102                          COLUMN_LABEL, C_("compression", "None"),
103                          -1);
104 
105   gtk_list_store_append (store, &iter);
106   gtk_list_store_set    (store, &iter,
107                          COLUMN_ID,    NULL,
108                          COLUMN_LABEL, NULL,
109                          -1);
110 
111   gtk_list_store_append (store, &iter);
112   gtk_list_store_set    (store, &iter,
113                          COLUMN_ID,    "fast",
114                          COLUMN_LABEL, C_("compression", "Best performance"),
115                          -1);
116 
117   gtk_list_store_append (store, &iter);
118   gtk_list_store_set    (store, &iter,
119                          COLUMN_ID,    "balanced",
120                          COLUMN_LABEL, C_("compression", "Balanced"),
121                          -1);
122 
123   gtk_list_store_append (store, &iter);
124   gtk_list_store_set    (store, &iter,
125                          COLUMN_ID,    "best",
126                          COLUMN_LABEL, C_("compression", "Best compression"),
127                          -1);
128 
129   gtk_combo_box_set_entry_text_column (GTK_COMBO_BOX (combo_box),
130                                        COLUMN_LABEL);
131 
132   layout = GTK_CELL_LAYOUT (combo_box);
133 
134   cell = gtk_cell_renderer_text_new ();
135 
136   gtk_cell_layout_clear (layout);
137   gtk_cell_layout_pack_start (layout, cell, TRUE);
138   gtk_cell_layout_set_attributes (layout, cell,
139                                   "text", COLUMN_LABEL,
140                                   NULL);
141 }
142 
143 static gboolean
gimp_compression_combo_box_separator_func(GtkTreeModel * model,GtkTreeIter * iter,gpointer data)144 gimp_compression_combo_box_separator_func (GtkTreeModel *model,
145                                            GtkTreeIter  *iter,
146                                            gpointer      data)
147 {
148   gchar    *value;
149   gboolean  result;
150 
151   gtk_tree_model_get (model, iter, COLUMN_ID, &value, -1);
152 
153   result = ! value;
154 
155   g_free (value);
156 
157   return result;
158 }
159 
160 
161 /*  public functions  */
162 
163 GtkWidget *
gimp_compression_combo_box_new(void)164 gimp_compression_combo_box_new (void)
165 {
166   return g_object_new (GIMP_TYPE_COMPRESSION_COMBO_BOX,
167                        "has-entry",    TRUE,
168                        "id-column",    COLUMN_ID,
169                        "label-column", COLUMN_LABEL,
170                        NULL);
171 }
172 
173 void
gimp_compression_combo_box_set_compression(GimpCompressionComboBox * combo_box,const gchar * compression)174 gimp_compression_combo_box_set_compression (GimpCompressionComboBox *combo_box,
175                                             const gchar             *compression)
176 {
177   g_return_if_fail (GIMP_IS_COMPRESSION_COMBO_BOX (combo_box));
178   g_return_if_fail (compression != NULL);
179 
180   if (! gimp_string_combo_box_set_active (GIMP_STRING_COMBO_BOX (combo_box),
181                                           compression))
182     {
183       GtkWidget *entry;
184 
185       entry = gtk_bin_get_child (GTK_BIN (combo_box));
186 
187       gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), -1);
188 
189       gtk_entry_set_text (GTK_ENTRY (entry), compression);
190     }
191 }
192 
193 gchar *
gimp_compression_combo_box_get_compression(GimpCompressionComboBox * combo_box)194 gimp_compression_combo_box_get_compression (GimpCompressionComboBox *combo_box)
195 {
196   gchar *result;
197 
198   g_return_val_if_fail (GIMP_IS_COMPRESSION_COMBO_BOX (combo_box), NULL);
199 
200   result = gimp_string_combo_box_get_active (GIMP_STRING_COMBO_BOX (combo_box));
201 
202   if (! result)
203     {
204       GtkWidget *entry;
205 
206       entry = gtk_bin_get_child (GTK_BIN (combo_box));
207 
208       result = g_strdup (gtk_entry_get_text (GTK_ENTRY (entry)));
209     }
210 
211   return result;
212 }
213