]> Pileus Git - ~andy/gtk/blob - gtk/gtkprinteroption.c
Merge branch 'master' into toolpalette
[~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 "gtkprinteroption.h"
26 #include "gtkalias.h"
27
28 /*****************************************
29  *            GtkPrinterOption           *
30  *****************************************/
31
32 enum {
33   CHANGED,
34   LAST_SIGNAL
35 };
36
37 static guint signals[LAST_SIGNAL] = { 0 };
38
39 G_DEFINE_TYPE (GtkPrinterOption, gtk_printer_option, G_TYPE_OBJECT)
40
41 static void
42 gtk_printer_option_finalize (GObject *object)
43 {
44   GtkPrinterOption *option = GTK_PRINTER_OPTION (object);
45   int i;
46   
47   g_free (option->name);
48   g_free (option->display_text);
49   g_free (option->value);
50   for (i = 0; i < option->num_choices; i++)
51     {
52       g_free (option->choices[i]);
53       g_free (option->choices_display[i]);
54     }
55   g_free (option->choices);
56   g_free (option->choices_display);
57   g_free (option->group);
58   
59   G_OBJECT_CLASS (gtk_printer_option_parent_class)->finalize (object);
60 }
61
62 static void
63 gtk_printer_option_init (GtkPrinterOption *option)
64 {
65   option->value = g_strdup ("");
66   option->activates_default = FALSE;
67 }
68
69 static void
70 gtk_printer_option_class_init (GtkPrinterOptionClass *class)
71 {
72   GObjectClass *gobject_class = (GObjectClass *)class;
73
74   gobject_class->finalize = gtk_printer_option_finalize;
75
76   signals[CHANGED] =
77     g_signal_new ("changed",
78                   G_TYPE_FROM_CLASS (class),
79                   G_SIGNAL_RUN_LAST,
80                   G_STRUCT_OFFSET (GtkPrinterOptionClass, changed),
81                   NULL, NULL,
82                   g_cclosure_marshal_VOID__VOID,
83                   G_TYPE_NONE, 0);
84 }
85
86 GtkPrinterOption *
87 gtk_printer_option_new (const char *name, const char *display_text,
88                         GtkPrinterOptionType type)
89 {
90   GtkPrinterOption *option;
91
92   option = g_object_new (GTK_TYPE_PRINTER_OPTION, NULL);
93
94   option->name = g_strdup (name);
95   option->display_text = g_strdup (display_text);
96   option->type = type;
97   
98   return option;
99 }
100
101 static void
102 emit_changed (GtkPrinterOption *option)
103 {
104   g_signal_emit (option, signals[CHANGED], 0);
105 }
106
107 void
108 gtk_printer_option_set (GtkPrinterOption *option,
109                         const char *value)
110 {
111   if (value == NULL)
112     value = "";
113   
114   if (strcmp (option->value, value) == 0)
115     return;
116
117   if ((option->type == GTK_PRINTER_OPTION_TYPE_PICKONE ||
118        option->type == GTK_PRINTER_OPTION_TYPE_ALTERNATIVE) &&
119       value != NULL)
120     {
121       int i;
122       
123       for (i = 0; i < option->num_choices; i++)
124         {
125           if (g_ascii_strcasecmp (value, option->choices[i]) == 0)
126             {
127               value = option->choices[i];
128               break;
129             }
130         }
131
132       if (i == option->num_choices)
133         return; /* Not found in available choices */
134     }
135   
136   g_free (option->value);
137   option->value = g_strdup (value);
138   
139   emit_changed (option);
140 }
141
142 void
143 gtk_printer_option_set_boolean (GtkPrinterOption *option,
144                                 gboolean value)
145 {
146   gtk_printer_option_set (option, value ? "True" : "False");
147 }
148
149 void
150 gtk_printer_option_set_has_conflict  (GtkPrinterOption *option,
151                                       gboolean  has_conflict)
152 {
153   has_conflict = has_conflict != 0;
154   
155   if (option->has_conflict == has_conflict)
156     return;
157
158   option->has_conflict = has_conflict;
159   emit_changed (option);
160 }
161
162 void
163 gtk_printer_option_clear_has_conflict (GtkPrinterOption     *option)
164 {
165   gtk_printer_option_set_has_conflict  (option, FALSE);
166 }
167
168 void
169 gtk_printer_option_allocate_choices (GtkPrinterOption     *option,
170                                      int num)
171 {
172   g_free (option->choices);
173   g_free (option->choices_display);
174
175   option->num_choices = num;
176   if (num == 0)
177     {
178       option->choices = NULL;
179       option->choices_display = NULL;
180     }
181   else
182     {
183       option->choices = g_new0 (char *, num);
184       option->choices_display = g_new0 (char *, num);
185     }
186 }
187
188 void
189 gtk_printer_option_choices_from_array (GtkPrinterOption   *option,
190                                        int                 num_choices,
191                                        char               *choices[],
192                                        char              *choices_display[])
193 {
194   int i;
195   
196   gtk_printer_option_allocate_choices (option, num_choices);
197   for (i = 0; i < num_choices; i++)
198     {
199       option->choices[i] = g_strdup (choices[i]);
200       option->choices_display[i] = g_strdup (choices_display[i]);
201     }
202 }
203
204 gboolean
205 gtk_printer_option_has_choice (GtkPrinterOption     *option,
206                                const char           *choice)
207 {
208   int i;
209   
210   for (i = 0; i < option->num_choices; i++)
211     {
212       if (strcmp (option->choices[i], choice) == 0)
213         return TRUE;
214     }
215   
216   return FALSE;
217 }
218
219 void
220 gtk_printer_option_set_activates_default (GtkPrinterOption *option,
221                                           gboolean          activates)
222 {
223   g_return_if_fail (GTK_IS_PRINTER_OPTION (option));
224
225   option->activates_default = activates;
226 }
227
228 gboolean
229 gtk_printer_option_get_activates_default (GtkPrinterOption *option)
230 {
231   g_return_val_if_fail (GTK_IS_PRINTER_OPTION (option), FALSE);
232
233   return option->activates_default;
234 }
235
236
237 #define __GTK_PRINTER_OPTION_C__
238 #include "gtkaliasdef.c"