summaryrefslogtreecommitdiffstats
path: root/app/widgets/gimpcompressioncombobox.c
blob: 917237ebaea43f2ae143c3fb462d67d72a5608a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimpcompressioncombobox.c
 * Copyright (C) 2004, 2008  Sven Neumann <sven@gimp.org>
 *
 * 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
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "stdlib.h"

#include <gegl.h>
#include <gtk/gtk.h>

#include "libgimpwidgets/gimpwidgets.h"

#include "widgets-types.h"

#include "gimpcompressioncombobox.h"

#include "gimp-intl.h"


enum
{
  COLUMN_ID,
  COLUMN_LABEL,
  N_COLUMNS
};


/*  local function prototypes  */

static void       gimp_compression_combo_box_constructed    (GObject      *object);

static gboolean   gimp_compression_combo_box_separator_func (GtkTreeModel *model,
                                                             GtkTreeIter  *iter,
                                                             gpointer      data);


G_DEFINE_TYPE (GimpCompressionComboBox, gimp_compression_combo_box,
               GIMP_TYPE_STRING_COMBO_BOX)

#define parent_class gimp_compression_combo_box_parent_class


/*  private functions  */

static void
gimp_compression_combo_box_class_init (GimpCompressionComboBoxClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->constructed = gimp_compression_combo_box_constructed;
}

static void
gimp_compression_combo_box_init (GimpCompressionComboBox *combo_box)
{
}

static void
gimp_compression_combo_box_constructed (GObject *object)
{
  GimpCompressionComboBox *combo_box = GIMP_COMPRESSION_COMBO_BOX (object);
  GtkCellLayout           *layout;
  GtkCellRenderer         *cell;
  GtkListStore            *store;
  GtkTreeIter              iter;

  G_OBJECT_CLASS (parent_class)->constructed (object);

  store = gtk_list_store_new (N_COLUMNS,
                              G_TYPE_STRING,   /* ID    */
                              G_TYPE_STRING);  /* LABEL */

  gtk_combo_box_set_model (GTK_COMBO_BOX (combo_box), GTK_TREE_MODEL (store));
  g_object_unref (store);

  gtk_combo_box_set_row_separator_func (
    GTK_COMBO_BOX (combo_box),
    gimp_compression_combo_box_separator_func,
    NULL,
    NULL);

  gtk_list_store_append (store, &iter);
  gtk_list_store_set    (store, &iter,
                         COLUMN_ID,    "none",
                         COLUMN_LABEL, C_("compression", "None"),
                         -1);

  gtk_list_store_append (store, &iter);
  gtk_list_store_set    (store, &iter,
                         COLUMN_ID,    NULL,
                         COLUMN_LABEL, NULL,
                         -1);

  gtk_list_store_append (store, &iter);
  gtk_list_store_set    (store, &iter,
                         COLUMN_ID,    "fast",
                         COLUMN_LABEL, C_("compression", "Best performance"),
                         -1);

  gtk_list_store_append (store, &iter);
  gtk_list_store_set    (store, &iter,
                         COLUMN_ID,    "balanced",
                         COLUMN_LABEL, C_("compression", "Balanced"),
                         -1);

  gtk_list_store_append (store, &iter);
  gtk_list_store_set    (store, &iter,
                         COLUMN_ID,    "best",
                         COLUMN_LABEL, C_("compression", "Best compression"),
                         -1);

  gtk_combo_box_set_entry_text_column (GTK_COMBO_BOX (combo_box),
                                       COLUMN_LABEL);

  layout = GTK_CELL_LAYOUT (combo_box);

  cell = gtk_cell_renderer_text_new ();

  gtk_cell_layout_clear (layout);
  gtk_cell_layout_pack_start (layout, cell, TRUE);
  gtk_cell_layout_set_attributes (layout, cell,
                                  "text", COLUMN_LABEL,
                                  NULL);
}

static gboolean
gimp_compression_combo_box_separator_func (GtkTreeModel *model,
                                           GtkTreeIter  *iter,
                                           gpointer      data)
{
  gchar    *value;
  gboolean  result;

  gtk_tree_model_get (model, iter, COLUMN_ID, &value, -1);

  result = ! value;

  g_free (value);

  return result;
}


/*  public functions  */

GtkWidget *
gimp_compression_combo_box_new (void)
{
  return g_object_new (GIMP_TYPE_COMPRESSION_COMBO_BOX,
                       "has-entry",    TRUE,
                       "id-column",    COLUMN_ID,
                       "label-column", COLUMN_LABEL,
                       NULL);
}

void
gimp_compression_combo_box_set_compression (GimpCompressionComboBox *combo_box,
                                            const gchar             *compression)
{
  g_return_if_fail (GIMP_IS_COMPRESSION_COMBO_BOX (combo_box));
  g_return_if_fail (compression != NULL);

  if (! gimp_string_combo_box_set_active (GIMP_STRING_COMBO_BOX (combo_box),
                                          compression))
    {
      GtkWidget *entry;

      entry = gtk_bin_get_child (GTK_BIN (combo_box));

      gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), -1);

      gtk_entry_set_text (GTK_ENTRY (entry), compression);
    }
}

gchar *
gimp_compression_combo_box_get_compression (GimpCompressionComboBox *combo_box)
{
  gchar *result;

  g_return_val_if_fail (GIMP_IS_COMPRESSION_COMBO_BOX (combo_box), NULL);

  result = gimp_string_combo_box_get_active (GIMP_STRING_COMBO_BOX (combo_box));

  if (! result)
    {
      GtkWidget *entry;

      entry = gtk_bin_get_child (GTK_BIN (combo_box));

      result = g_strdup (gtk_entry_get_text (GTK_ENTRY (entry)));
    }

  return result;
}