]> Pileus Git - ~andy/gtk/blob - gtk/gtkprinteroption.c
Add a 'value' GObject property to GtkPrinterOption so that it can be used with g_obje...
[~andy/gtk] / gtk / gtkprinteroption.c
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, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include "config.h"
22 #include <string.h>
23 #include <gmodule.h>
24
25 #include "gtkintl.h"
26 #include "gtkprivate.h"
27 #include "gtkprinteroption.h"
28
29 /*****************************************
30  *            GtkPrinterOption           *
31  *****************************************/
32
33 enum {
34   CHANGED,
35   LAST_SIGNAL
36 };
37
38 enum {
39   PROP_0,
40   PROP_VALUE
41 };
42
43 static guint signals[LAST_SIGNAL] = { 0 };
44
45 static void gtk_printer_option_set_property (GObject      *object,
46                                              guint         prop_id,
47                                              const GValue *value,
48                                              GParamSpec   *pspec);
49 static void gtk_printer_option_get_property (GObject      *object,
50                                              guint         prop_id,
51                                              GValue       *value,
52                                              GParamSpec   *pspec);
53
54 G_DEFINE_TYPE (GtkPrinterOption, gtk_printer_option, G_TYPE_OBJECT)
55
56 static void
57 gtk_printer_option_finalize (GObject *object)
58 {
59   GtkPrinterOption *option = GTK_PRINTER_OPTION (object);
60   int i;
61   
62   g_free (option->name);
63   g_free (option->display_text);
64   g_free (option->value);
65   for (i = 0; i < option->num_choices; i++)
66     {
67       g_free (option->choices[i]);
68       g_free (option->choices_display[i]);
69     }
70   g_free (option->choices);
71   g_free (option->choices_display);
72   g_free (option->group);
73   
74   G_OBJECT_CLASS (gtk_printer_option_parent_class)->finalize (object);
75 }
76
77 static void
78 gtk_printer_option_init (GtkPrinterOption *option)
79 {
80   option->value = g_strdup ("");
81   option->activates_default = FALSE;
82 }
83
84 static void
85 gtk_printer_option_class_init (GtkPrinterOptionClass *class)
86 {
87   GObjectClass *gobject_class = (GObjectClass *)class;
88
89   gobject_class->finalize = gtk_printer_option_finalize;
90   gobject_class->set_property = gtk_printer_option_set_property;
91   gobject_class->get_property = gtk_printer_option_get_property;
92
93   signals[CHANGED] =
94     g_signal_new ("changed",
95                   G_TYPE_FROM_CLASS (class),
96                   G_SIGNAL_RUN_LAST,
97                   G_STRUCT_OFFSET (GtkPrinterOptionClass, changed),
98                   NULL, NULL,
99                   g_cclosure_marshal_VOID__VOID,
100                   G_TYPE_NONE, 0);
101
102   g_object_class_install_property (G_OBJECT_CLASS (class),
103                                    PROP_VALUE,
104                                    g_param_spec_string ("value",
105                                                         P_("Option Value"),
106                                                         P_("Value of the option"),
107                                                         "",
108                                                         GTK_PARAM_READWRITE));
109 }
110
111 GtkPrinterOption *
112 gtk_printer_option_new (const char *name, const char *display_text,
113                         GtkPrinterOptionType type)
114 {
115   GtkPrinterOption *option;
116
117   option = g_object_new (GTK_TYPE_PRINTER_OPTION, NULL);
118
119   option->name = g_strdup (name);
120   option->display_text = g_strdup (display_text);
121   option->type = type;
122   
123   return option;
124 }
125
126 static void
127 gtk_printer_option_set_property (GObject         *object,
128                                  guint            prop_id,
129                                  const GValue    *value,
130                                  GParamSpec      *pspec)
131 {
132   GtkPrinterOption *option = GTK_PRINTER_OPTION (object);
133
134   switch (prop_id)
135     {
136     case PROP_VALUE:
137       gtk_printer_option_set (option, g_value_get_string (value));
138       break;
139     default:
140       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
141       break;
142     }
143 }
144
145 static void
146 gtk_printer_option_get_property (GObject    *object,
147                                  guint       prop_id,
148                                  GValue     *value,
149                                  GParamSpec *pspec)
150 {
151   GtkPrinterOption *option = GTK_PRINTER_OPTION (object);
152
153   switch (prop_id)
154     {
155     case PROP_VALUE:
156       g_value_set_string (value, option->value);
157       break;
158     default:
159       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
160       break;
161     }
162 }
163
164 static void
165 emit_changed (GtkPrinterOption *option)
166 {
167   g_signal_emit (option, signals[CHANGED], 0);
168 }
169
170 void
171 gtk_printer_option_set (GtkPrinterOption *option,
172                         const char *value)
173 {
174   if (value == NULL)
175     value = "";
176   
177   if (strcmp (option->value, value) == 0)
178     return;
179
180   if ((option->type == GTK_PRINTER_OPTION_TYPE_PICKONE ||
181        option->type == GTK_PRINTER_OPTION_TYPE_ALTERNATIVE) &&
182       value != NULL)
183     {
184       int i;
185       
186       for (i = 0; i < option->num_choices; i++)
187         {
188           if (g_ascii_strcasecmp (value, option->choices[i]) == 0)
189             {
190               value = option->choices[i];
191               break;
192             }
193         }
194
195       if (i == option->num_choices)
196         return; /* Not found in available choices */
197     }
198   
199   g_free (option->value);
200   option->value = g_strdup (value);
201   
202   emit_changed (option);
203 }
204
205 void
206 gtk_printer_option_set_boolean (GtkPrinterOption *option,
207                                 gboolean value)
208 {
209   gtk_printer_option_set (option, value ? "True" : "False");
210 }
211
212 void
213 gtk_printer_option_set_has_conflict  (GtkPrinterOption *option,
214                                       gboolean  has_conflict)
215 {
216   has_conflict = has_conflict != 0;
217   
218   if (option->has_conflict == has_conflict)
219     return;
220
221   option->has_conflict = has_conflict;
222   emit_changed (option);
223 }
224
225 void
226 gtk_printer_option_clear_has_conflict (GtkPrinterOption     *option)
227 {
228   gtk_printer_option_set_has_conflict  (option, FALSE);
229 }
230
231 void
232 gtk_printer_option_allocate_choices (GtkPrinterOption     *option,
233                                      int num)
234 {
235   g_free (option->choices);
236   g_free (option->choices_display);
237
238   option->num_choices = num;
239   if (num == 0)
240     {
241       option->choices = NULL;
242       option->choices_display = NULL;
243     }
244   else
245     {
246       option->choices = g_new0 (char *, num);
247       option->choices_display = g_new0 (char *, num);
248     }
249 }
250
251 void
252 gtk_printer_option_choices_from_array (GtkPrinterOption   *option,
253                                        int                 num_choices,
254                                        char               *choices[],
255                                        char              *choices_display[])
256 {
257   int i;
258   
259   gtk_printer_option_allocate_choices (option, num_choices);
260   for (i = 0; i < num_choices; i++)
261     {
262       option->choices[i] = g_strdup (choices[i]);
263       option->choices_display[i] = g_strdup (choices_display[i]);
264     }
265 }
266
267 gboolean
268 gtk_printer_option_has_choice (GtkPrinterOption     *option,
269                                const char           *choice)
270 {
271   int i;
272   
273   for (i = 0; i < option->num_choices; i++)
274     {
275       if (strcmp (option->choices[i], choice) == 0)
276         return TRUE;
277     }
278   
279   return FALSE;
280 }
281
282 void
283 gtk_printer_option_set_activates_default (GtkPrinterOption *option,
284                                           gboolean          activates)
285 {
286   g_return_if_fail (GTK_IS_PRINTER_OPTION (option));
287
288   option->activates_default = activates;
289 }
290
291 gboolean
292 gtk_printer_option_get_activates_default (GtkPrinterOption *option)
293 {
294   g_return_val_if_fail (GTK_IS_PRINTER_OPTION (option), FALSE);
295
296   return option->activates_default;
297 }