1/* GTK - The GIMP Toolkit
2 * gtkprinteroption.c: Handling possible settings for a specific printer setting
3 * Copyright (C) 2006, Red Hat, Inc.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "config.h"
20#include <string.h>
21#include <gmodule.h>
22
23#include "gtkintl.h"
24#include "gtkprivate.h"
25#include "gtkprinteroption.h"
26
27/*****************************************
28 * GtkPrinterOption *
29 *****************************************/
30
31enum {
32 CHANGED,
33 LAST_SIGNAL
34};
35
36enum {
37 PROP_0,
38 PROP_VALUE
39};
40
41static guint signals[LAST_SIGNAL] = { 0 };
42
43static void gtk_printer_option_set_property (GObject *object,
44 guint prop_id,
45 const GValue *value,
46 GParamSpec *pspec);
47static void gtk_printer_option_get_property (GObject *object,
48 guint prop_id,
49 GValue *value,
50 GParamSpec *pspec);
51
52G_DEFINE_TYPE (GtkPrinterOption, gtk_printer_option, G_TYPE_OBJECT)
53
54static void
55gtk_printer_option_finalize (GObject *object)
56{
57 GtkPrinterOption *option = GTK_PRINTER_OPTION (object);
58 int i;
59
60 g_free (mem: option->name);
61 g_free (mem: option->display_text);
62 g_free (mem: option->value);
63 for (i = 0; i < option->num_choices; i++)
64 {
65 g_free (mem: option->choices[i]);
66 g_free (mem: option->choices_display[i]);
67 }
68 g_free (mem: option->choices);
69 g_free (mem: option->choices_display);
70 g_free (mem: option->group);
71
72 G_OBJECT_CLASS (gtk_printer_option_parent_class)->finalize (object);
73}
74
75static void
76gtk_printer_option_init (GtkPrinterOption *option)
77{
78 option->value = g_strdup (str: "");
79 option->activates_default = FALSE;
80}
81
82static void
83gtk_printer_option_class_init (GtkPrinterOptionClass *class)
84{
85 GObjectClass *gobject_class = (GObjectClass *)class;
86
87 gobject_class->finalize = gtk_printer_option_finalize;
88 gobject_class->set_property = gtk_printer_option_set_property;
89 gobject_class->get_property = gtk_printer_option_get_property;
90
91 signals[CHANGED] =
92 g_signal_new (I_("changed"),
93 G_TYPE_FROM_CLASS (class),
94 signal_flags: G_SIGNAL_RUN_LAST,
95 G_STRUCT_OFFSET (GtkPrinterOptionClass, changed),
96 NULL, NULL,
97 NULL,
98 G_TYPE_NONE, n_params: 0);
99
100 g_object_class_install_property (G_OBJECT_CLASS (class),
101 property_id: PROP_VALUE,
102 pspec: g_param_spec_string (name: "value",
103 P_("Option Value"),
104 P_("Value of the option"),
105 default_value: "",
106 GTK_PARAM_READWRITE));
107}
108
109GtkPrinterOption *
110gtk_printer_option_new (const char *name, const char *display_text,
111 GtkPrinterOptionType type)
112{
113 GtkPrinterOption *option;
114
115 option = g_object_new (GTK_TYPE_PRINTER_OPTION, NULL);
116
117 option->name = g_strdup (str: name);
118 option->display_text = g_strdup (str: display_text);
119 option->type = type;
120
121 return option;
122}
123
124static void
125gtk_printer_option_set_property (GObject *object,
126 guint prop_id,
127 const GValue *value,
128 GParamSpec *pspec)
129{
130 GtkPrinterOption *option = GTK_PRINTER_OPTION (object);
131
132 switch (prop_id)
133 {
134 case PROP_VALUE:
135 gtk_printer_option_set (option, value: g_value_get_string (value));
136 break;
137 default:
138 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
139 break;
140 }
141}
142
143static void
144gtk_printer_option_get_property (GObject *object,
145 guint prop_id,
146 GValue *value,
147 GParamSpec *pspec)
148{
149 GtkPrinterOption *option = GTK_PRINTER_OPTION (object);
150
151 switch (prop_id)
152 {
153 case PROP_VALUE:
154 g_value_set_string (value, v_string: option->value);
155 break;
156 default:
157 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
158 break;
159 }
160}
161
162static void
163emit_changed (GtkPrinterOption *option)
164{
165 g_signal_emit (instance: option, signal_id: signals[CHANGED], detail: 0);
166}
167
168void
169gtk_printer_option_set (GtkPrinterOption *option,
170 const char *value)
171{
172 if (value == NULL)
173 value = "";
174
175 if (strcmp (s1: option->value, s2: value) == 0)
176 return;
177
178 if ((option->type == GTK_PRINTER_OPTION_TYPE_PICKONE ||
179 option->type == GTK_PRINTER_OPTION_TYPE_ALTERNATIVE))
180 {
181 int i;
182
183 for (i = 0; i < option->num_choices; i++)
184 {
185 if (g_ascii_strcasecmp (s1: value, s2: option->choices[i]) == 0)
186 {
187 value = option->choices[i];
188 break;
189 }
190 }
191
192 if (i == option->num_choices)
193 return; /* Not found in available choices */
194 }
195
196 g_free (mem: option->value);
197 option->value = g_strdup (str: value);
198
199 emit_changed (option);
200}
201
202void
203gtk_printer_option_set_boolean (GtkPrinterOption *option,
204 gboolean value)
205{
206 gtk_printer_option_set (option, value: value ? "True" : "False");
207}
208
209void
210gtk_printer_option_set_has_conflict (GtkPrinterOption *option,
211 gboolean has_conflict)
212{
213 has_conflict = has_conflict != 0;
214
215 if (option->has_conflict == has_conflict)
216 return;
217
218 option->has_conflict = has_conflict;
219 emit_changed (option);
220}
221
222void
223gtk_printer_option_clear_has_conflict (GtkPrinterOption *option)
224{
225 gtk_printer_option_set_has_conflict (option, FALSE);
226}
227
228void
229gtk_printer_option_allocate_choices (GtkPrinterOption *option,
230 int num)
231{
232 g_free (mem: option->choices);
233 g_free (mem: option->choices_display);
234
235 option->num_choices = num;
236 if (num == 0)
237 {
238 option->choices = NULL;
239 option->choices_display = NULL;
240 }
241 else
242 {
243 option->choices = g_new0 (char *, num);
244 option->choices_display = g_new0 (char *, num);
245 }
246}
247
248void
249gtk_printer_option_choices_from_array (GtkPrinterOption *option,
250 int num_choices,
251 const char **choices,
252 const char **choices_display)
253{
254 int i;
255
256 gtk_printer_option_allocate_choices (option, num: num_choices);
257 for (i = 0; i < num_choices; i++)
258 {
259 option->choices[i] = g_strdup (str: choices[i]);
260 option->choices_display[i] = g_strdup (str: choices_display[i]);
261 }
262}
263
264gboolean
265gtk_printer_option_has_choice (GtkPrinterOption *option,
266 const char *choice)
267{
268 int i;
269
270 for (i = 0; i < option->num_choices; i++)
271 {
272 if (strcmp (s1: option->choices[i], s2: choice) == 0)
273 return TRUE;
274 }
275
276 return FALSE;
277}
278
279void
280gtk_printer_option_set_activates_default (GtkPrinterOption *option,
281 gboolean activates)
282{
283 g_return_if_fail (GTK_IS_PRINTER_OPTION (option));
284
285 option->activates_default = activates;
286}
287
288gboolean
289gtk_printer_option_get_activates_default (GtkPrinterOption *option)
290{
291 g_return_val_if_fail (GTK_IS_PRINTER_OPTION (option), FALSE);
292
293 return option->activates_default;
294}
295

source code of gtk/gtk/gtkprinteroption.c