]> Pileus Git - ~andy/gtk/blob - gtk/gtkprinteroptionset.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtkprinteroptionset.c
1 /* GTK - The GIMP Toolkit
2  * gtkprintbackend.h: Abstract printer backend interfaces
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, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "config.h"
20 #include <string.h>
21 #include <glib.h>
22 #include <gmodule.h>
23
24 #include "gtkprinteroptionset.h"
25
26 /*****************************************
27  *         GtkPrinterOptionSet    *
28  *****************************************/
29
30 enum {
31   CHANGED,
32   LAST_SIGNAL
33 };
34
35 static guint signals[LAST_SIGNAL] = { 0 };
36
37 /* ugly side-effect of aliasing */
38 #undef gtk_printer_option_set
39
40 G_DEFINE_TYPE (GtkPrinterOptionSet, gtk_printer_option_set, G_TYPE_OBJECT)
41
42 static void
43 gtk_printer_option_set_finalize (GObject *object)
44 {
45   GtkPrinterOptionSet *set = GTK_PRINTER_OPTION_SET (object);
46
47   g_hash_table_destroy (set->hash);
48   g_ptr_array_foreach (set->array, (GFunc)g_object_unref, NULL);
49   g_ptr_array_free (set->array, TRUE);
50   
51   G_OBJECT_CLASS (gtk_printer_option_set_parent_class)->finalize (object);
52 }
53
54 static void
55 gtk_printer_option_set_init (GtkPrinterOptionSet *set)
56 {
57   set->array = g_ptr_array_new ();
58   set->hash = g_hash_table_new (g_str_hash, g_str_equal);
59 }
60
61 static void
62 gtk_printer_option_set_class_init (GtkPrinterOptionSetClass *class)
63 {
64   GObjectClass *gobject_class = (GObjectClass *)class;
65
66   gobject_class->finalize = gtk_printer_option_set_finalize;
67
68   signals[CHANGED] =
69     g_signal_new ("changed",
70                   G_TYPE_FROM_CLASS (gobject_class),
71                   G_SIGNAL_RUN_LAST,
72                   G_STRUCT_OFFSET (GtkPrinterOptionSetClass, changed),
73                   NULL, NULL,
74                   g_cclosure_marshal_VOID__VOID,
75                   G_TYPE_NONE, 0);
76 }
77
78
79 static void
80 emit_changed (GtkPrinterOptionSet *set)
81 {
82   g_signal_emit (set, signals[CHANGED], 0);
83 }
84
85 GtkPrinterOptionSet *
86 gtk_printer_option_set_new (void)
87 {
88   return g_object_new (GTK_TYPE_PRINTER_OPTION_SET, NULL);
89 }
90
91 void
92 gtk_printer_option_set_remove (GtkPrinterOptionSet *set,
93                                GtkPrinterOption    *option)
94 {
95   int i;
96   
97   for (i = 0; i < set->array->len; i++)
98     {
99       if (g_ptr_array_index (set->array, i) == option)
100         {
101           g_ptr_array_remove_index (set->array, i);
102           g_hash_table_remove (set->hash, option->name);
103           g_signal_handlers_disconnect_by_func (option, emit_changed, set);
104
105           g_object_unref (option);
106           break;
107         }
108     }
109 }
110
111 void
112 gtk_printer_option_set_add (GtkPrinterOptionSet *set,
113                             GtkPrinterOption    *option)
114 {
115   g_object_ref (option);
116   
117   if (gtk_printer_option_set_lookup (set, option->name))
118     gtk_printer_option_set_remove (set, option);
119     
120   g_ptr_array_add (set->array, option);
121   g_hash_table_insert (set->hash, option->name, option);
122   g_signal_connect_object (option, "changed", G_CALLBACK (emit_changed), set, G_CONNECT_SWAPPED);
123 }
124
125 GtkPrinterOption *
126 gtk_printer_option_set_lookup (GtkPrinterOptionSet *set,
127                                const char          *name)
128 {
129   gpointer ptr;
130
131   ptr = g_hash_table_lookup (set->hash, name);
132
133   return GTK_PRINTER_OPTION (ptr);
134 }
135
136 void
137 gtk_printer_option_set_clear_conflicts (GtkPrinterOptionSet *set)
138 {
139   gtk_printer_option_set_foreach (set,
140                                   (GtkPrinterOptionSetFunc)gtk_printer_option_clear_has_conflict,
141                                   NULL);
142 }
143
144 /**
145  * gtk_printer_option_set_get_groups:
146  *
147  * Return value: (element-type utf8) (transfer full):
148  */
149 GList *
150 gtk_printer_option_set_get_groups (GtkPrinterOptionSet *set)
151 {
152   GtkPrinterOption *option;
153   GList *list = NULL;
154   int i;
155
156   for (i = 0; i < set->array->len; i++)
157     {
158       option = g_ptr_array_index (set->array, i);
159
160       if (g_list_find_custom (list, option->group, (GCompareFunc)g_strcmp0) == NULL)
161         list = g_list_prepend (list, g_strdup (option->group));
162     }
163
164   return g_list_reverse (list);
165 }
166
167 void
168 gtk_printer_option_set_foreach_in_group (GtkPrinterOptionSet     *set,
169                                          const char              *group,
170                                          GtkPrinterOptionSetFunc  func,
171                                          gpointer                 user_data)
172 {
173   GtkPrinterOption *option;
174   int i;
175
176   for (i = 0; i < set->array->len; i++)
177     {
178       option = g_ptr_array_index (set->array, i);
179
180       if (group == NULL || g_strcmp0 (group, option->group) == 0)
181         func (option, user_data);
182     }
183 }
184
185 void
186 gtk_printer_option_set_foreach (GtkPrinterOptionSet *set,
187                                 GtkPrinterOptionSetFunc func,
188                                 gpointer user_data)
189 {
190   gtk_printer_option_set_foreach_in_group (set, NULL, func, user_data);
191 }