]> Pileus Git - ~andy/gtk/blob - gtk/gtkprinteroption.c
b2f4d106ecefb0cda0166f43b77c93345b74c9f6
[~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     {
183       int i;
184       
185       for (i = 0; i < option->num_choices; i++)
186         {
187           if (g_ascii_strcasecmp (value, option->choices[i]) == 0)
188             {
189               value = option->choices[i];
190               break;
191             }
192         }
193
194       if (i == option->num_choices)
195         return; /* Not found in available choices */
196     }
197           
198   g_free (option->value);
199   option->value = g_strdup (value);
200   
201   emit_changed (option);
202 }
203
204 void
205 gtk_printer_option_set_boolean (GtkPrinterOption *option,
206                                 gboolean value)
207 {
208   gtk_printer_option_set (option, value ? "True" : "False");
209 }
210
211 void
212 gtk_printer_option_set_has_conflict  (GtkPrinterOption *option,
213                                       gboolean  has_conflict)
214 {
215   has_conflict = has_conflict != 0;
216   
217   if (option->has_conflict == has_conflict)
218     return;
219
220   option->has_conflict = has_conflict;
221   emit_changed (option);
222 }
223
224 void
225 gtk_printer_option_clear_has_conflict (GtkPrinterOption     *option)
226 {
227   gtk_printer_option_set_has_conflict  (option, FALSE);
228 }
229
230 void
231 gtk_printer_option_allocate_choices (GtkPrinterOption     *option,
232                                      int num)
233 {
234   g_free (option->choices);
235   g_free (option->choices_display);
236
237   option->num_choices = num;
238   if (num == 0)
239     {
240       option->choices = NULL;
241       option->choices_display = NULL;
242     }
243   else
244     {
245       option->choices = g_new0 (char *, num);
246       option->choices_display = g_new0 (char *, num);
247     }
248 }
249
250 void
251 gtk_printer_option_choices_from_array (GtkPrinterOption   *option,
252                                        int                 num_choices,
253                                        char               *choices[],
254                                        char              *choices_display[])
255 {
256   int i;
257   
258   gtk_printer_option_allocate_choices (option, num_choices);
259   for (i = 0; i < num_choices; i++)
260     {
261       option->choices[i] = g_strdup (choices[i]);
262       option->choices_display[i] = g_strdup (choices_display[i]);
263     }
264 }
265
266 gboolean
267 gtk_printer_option_has_choice (GtkPrinterOption     *option,
268                                const char           *choice)
269 {
270   int i;
271   
272   for (i = 0; i < option->num_choices; i++)
273     {
274       if (strcmp (option->choices[i], choice) == 0)
275         return TRUE;
276     }
277   
278   return FALSE;
279 }
280
281 void
282 gtk_printer_option_set_activates_default (GtkPrinterOption *option,
283                                           gboolean          activates)
284 {
285   g_return_if_fail (GTK_IS_PRINTER_OPTION (option));
286
287   option->activates_default = activates;
288 }
289
290 gboolean
291 gtk_printer_option_get_activates_default (GtkPrinterOption *option)
292 {
293   g_return_val_if_fail (GTK_IS_PRINTER_OPTION (option), FALSE);
294
295   return option->activates_default;
296 }