]> Pileus Git - ~andy/gtk/blob - gtk/gtkprinteroption.c
Revert name change
[~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 }
67
68 static void
69 gtk_printer_option_class_init (GtkPrinterOptionClass *class)
70 {
71   GObjectClass *gobject_class = (GObjectClass *)class;
72
73   gobject_class->finalize = gtk_printer_option_finalize;
74
75   signals[CHANGED] =
76     g_signal_new ("changed",
77                   G_TYPE_FROM_CLASS (class),
78                   G_SIGNAL_RUN_LAST,
79                   G_STRUCT_OFFSET (GtkPrinterOptionClass, changed),
80                   NULL, NULL,
81                   g_cclosure_marshal_VOID__VOID,
82                   G_TYPE_NONE, 0);
83 }
84
85 GtkPrinterOption *
86 gtk_printer_option_new (const char *name, const char *display_text,
87                         GtkPrinterOptionType type)
88 {
89   GtkPrinterOption *option;
90
91   option = g_object_new (GTK_TYPE_PRINTER_OPTION, NULL);
92
93   option->name = g_strdup (name);
94   option->display_text = g_strdup (display_text);
95   option->type = type;
96   
97   return option;
98 }
99
100 static void
101 emit_changed (GtkPrinterOption *option)
102 {
103   g_signal_emit (option, signals[CHANGED], 0);
104 }
105
106 void
107 gtk_printer_option_set (GtkPrinterOption *option,
108                         const char *value)
109 {
110   if (value == NULL)
111     value = "";
112   
113   if (strcmp (option->value, value) == 0)
114     return;
115
116   if ((option->type == GTK_PRINTER_OPTION_TYPE_PICKONE ||
117        option->type == GTK_PRINTER_OPTION_TYPE_ALTERNATIVE) &&
118       value != NULL)
119     {
120       int i;
121       
122       for (i = 0; i < option->num_choices; i++)
123         {
124           if (g_ascii_strcasecmp (value, option->choices[i]) == 0)
125             {
126               value = option->choices[i];
127               break;
128             }
129         }
130
131       if (i == option->num_choices)
132         return; /* Not found in available choices */
133     }
134   
135   g_free (option->value);
136   option->value = g_strdup (value);
137   
138   emit_changed (option);
139 }
140
141 void
142 gtk_printer_option_set_boolean (GtkPrinterOption *option,
143                                 gboolean value)
144 {
145   gtk_printer_option_set (option, value ? "True" : "False");
146 }
147
148 void
149 gtk_printer_option_set_has_conflict  (GtkPrinterOption *option,
150                                       gboolean  has_conflict)
151 {
152   has_conflict = has_conflict != 0;
153   
154   if (option->has_conflict == has_conflict)
155     return;
156
157   option->has_conflict = has_conflict;
158   emit_changed (option);
159 }
160
161 void
162 gtk_printer_option_clear_has_conflict (GtkPrinterOption     *option)
163 {
164   gtk_printer_option_set_has_conflict  (option, FALSE);
165 }
166
167 void
168 gtk_printer_option_allocate_choices (GtkPrinterOption     *option,
169                                      int num)
170 {
171   g_free (option->choices);
172   g_free (option->choices_display);
173
174   option->num_choices = num;
175   if (num == 0)
176     {
177       option->choices = NULL;
178       option->choices_display = NULL;
179     }
180   else
181     {
182       option->choices = g_new0 (char *, num);
183       option->choices_display = g_new0 (char *, num);
184     }
185 }
186
187 void
188 gtk_printer_option_choices_from_array (GtkPrinterOption   *option,
189                                        int                 num_choices,
190                                        char               *choices[],
191                                        char              *choices_display[])
192 {
193   int i;
194   
195   gtk_printer_option_allocate_choices (option, num_choices);
196   for (i = 0; i < num_choices; i++)
197     {
198       option->choices[i] = g_strdup (choices[i]);
199       option->choices_display[i] = g_strdup (choices_display[i]);
200     }
201 }
202
203 gboolean
204 gtk_printer_option_has_choice (GtkPrinterOption     *option,
205                                const char           *choice)
206 {
207   int i;
208   
209   for (i = 0; i < option->num_choices; i++)
210     {
211       if (strcmp (option->choices[i], choice) == 0)
212         return TRUE;
213     }
214   
215   return FALSE;
216 }
217
218
219 #define __GTK_PRINTER_OPTION_C__
220 #include "gtkaliasdef.c"