]> Pileus Git - ~andy/gtk/blob - gtk/gtkprinteroption.c
Merge the gtk-printing branch. For more detailed ChangeLog entries, see
[~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       value != NULL)
118     {
119       int i;
120       
121       for (i = 0; i < option->num_choices; i++)
122         {
123           if (g_ascii_strcasecmp (value, option->choices[i]) == 0)
124             {
125               value = option->choices[i];
126               break;
127             }
128         }
129
130       if (i == option->num_choices)
131         return; /* Not found in availible choices */
132     }
133   
134   g_free (option->value);
135   option->value = g_strdup (value);
136   
137   emit_changed (option);
138 }
139
140 void
141 gtk_printer_option_set_boolean (GtkPrinterOption *option,
142                                 gboolean value)
143 {
144   gtk_printer_option_set (option, value ? "True" : "False");
145 }
146
147 void
148 gtk_printer_option_set_has_conflict  (GtkPrinterOption *option,
149                                       gboolean  has_conflict)
150 {
151   has_conflict = has_conflict != 0;
152   
153   if (option->has_conflict == has_conflict)
154     return;
155
156   option->has_conflict = has_conflict;
157   emit_changed (option);
158 }
159
160 void
161 gtk_printer_option_clear_has_conflict (GtkPrinterOption     *option)
162 {
163   gtk_printer_option_set_has_conflict  (option, FALSE);
164 }
165
166 void
167 gtk_printer_option_allocate_choices (GtkPrinterOption     *option,
168                                      int num)
169 {
170   g_free (option->choices);
171   g_free (option->choices_display);
172
173   option->num_choices = num;
174   if (num == 0)
175     {
176       option->choices = NULL;
177       option->choices_display = NULL;
178     }
179   else
180     {
181       option->choices = g_new0 (char *, num);
182       option->choices_display = g_new0 (char *, num);
183     }
184 }
185
186 void
187 gtk_printer_option_choices_from_array (GtkPrinterOption   *option,
188                                        int                 num_choices,
189                                        char               *choices[],
190                                        char              *choices_display[])
191 {
192   int i;
193   
194   gtk_printer_option_allocate_choices (option, num_choices);
195   for (i = 0; i < num_choices; i++)
196     {
197       option->choices[i] = g_strdup (choices[i]);
198       option->choices_display[i] = g_strdup (choices_display[i]);
199     }
200 }
201
202 gboolean
203 gtk_printer_option_has_choice (GtkPrinterOption     *option,
204                                const char           *choice)
205 {
206   int i;
207   
208   for (i = 0; i < option->num_choices; i++)
209     {
210       if (strcmp (option->choices[i], choice) == 0)
211         return TRUE;
212     }
213   
214   return FALSE;
215 }
216
217
218 #define __GTK_PRINTER_OPTION_C__
219 #include "gtkaliasdef.c"