1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
---|
2 | /* |
---|
3 | * gnome-print-unit-selector.c: A unit selector for gnome-print |
---|
4 | * |
---|
5 | * This program is free software; you can redistribute it and/or |
---|
6 | * modify it under the terms of the GNU Library General Public License |
---|
7 | * as published by the Free Software Foundation; either version 2 of |
---|
8 | * the License, or (at your option) any later version. |
---|
9 | * |
---|
10 | * This program 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 |
---|
13 | * GNU Library General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU Library General Public |
---|
16 | * License along with this program; if not, write to the Free Software |
---|
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
18 | * |
---|
19 | * Authors: |
---|
20 | * James Henstridge <james@daa.com.au> |
---|
21 | * |
---|
22 | * Copyright (C) 1998 James Henstridge <james@daa.com.au> |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | #include <config.h> |
---|
27 | |
---|
28 | #include <string.h> |
---|
29 | #include <math.h> |
---|
30 | #include <gtk/gtk.h> |
---|
31 | |
---|
32 | #define GNOME_PRINT_UNSTABLE_API |
---|
33 | #include "gnome-print-unit-selector.h" |
---|
34 | |
---|
35 | #define noGPP_VERBOSE |
---|
36 | |
---|
37 | enum |
---|
38 | { |
---|
39 | COL_NAME, |
---|
40 | COL_UNIT, |
---|
41 | N_COLUMNS |
---|
42 | }; |
---|
43 | struct _GnomePrintUnitSelector { |
---|
44 | GtkHBox box; |
---|
45 | |
---|
46 | GtkWidget *combo; |
---|
47 | |
---|
48 | guint bases; |
---|
49 | GList *units; |
---|
50 | const GnomePrintUnit *unit; |
---|
51 | gdouble ctmscale, devicescale; |
---|
52 | guint plural : 1; |
---|
53 | guint abbr : 1; |
---|
54 | |
---|
55 | GList *adjustments; |
---|
56 | }; |
---|
57 | |
---|
58 | struct _GnomePrintUnitSelectorClass { |
---|
59 | GtkHBoxClass parent_class; |
---|
60 | |
---|
61 | void (* modified) (GnomePrintUnitSelector *selector); |
---|
62 | }; |
---|
63 | |
---|
64 | static void gnome_print_unit_selector_class_init (GnomePrintUnitSelectorClass *klass); |
---|
65 | static void gnome_print_unit_selector_init (GnomePrintUnitSelector *selector); |
---|
66 | static void gnome_print_unit_selector_finalize (GObject *object); |
---|
67 | |
---|
68 | static void gnome_print_unit_selector_recalculate_adjustments (GnomePrintUnitSelector *us, |
---|
69 | const GnomePrintUnit *unit); |
---|
70 | |
---|
71 | static GtkHBoxClass *unit_selector_parent_class; |
---|
72 | |
---|
73 | enum { |
---|
74 | GNOME_PRINT_UNIT_SELECTOR_MODIFIED, |
---|
75 | GNOME_PRINT_UNIT_SELECTOR_LAST_SIGNAL |
---|
76 | }; |
---|
77 | |
---|
78 | static guint gnome_print_unit_selector_signals [GNOME_PRINT_UNIT_SELECTOR_LAST_SIGNAL] = { 0 }; |
---|
79 | |
---|
80 | GType |
---|
81 | gnome_print_unit_selector_get_type (void) |
---|
82 | { |
---|
83 | static GType type = 0; |
---|
84 | if (!type) { |
---|
85 | static const GTypeInfo info = { |
---|
86 | sizeof (GnomePrintUnitSelectorClass), |
---|
87 | NULL, NULL, |
---|
88 | (GClassInitFunc) gnome_print_unit_selector_class_init, |
---|
89 | NULL, NULL, |
---|
90 | sizeof (GnomePrintUnitSelector), |
---|
91 | 0, |
---|
92 | (GInstanceInitFunc) gnome_print_unit_selector_init |
---|
93 | }; |
---|
94 | type = g_type_register_static (GTK_TYPE_HBOX, "GnomePrintUnitSelector", &info, 0); |
---|
95 | } |
---|
96 | return type; |
---|
97 | } |
---|
98 | |
---|
99 | static void |
---|
100 | gnome_print_unit_selector_class_init (GnomePrintUnitSelectorClass *klass) |
---|
101 | { |
---|
102 | GObjectClass *object_class; |
---|
103 | GtkWidgetClass *widget_class; |
---|
104 | |
---|
105 | object_class = G_OBJECT_CLASS (klass); |
---|
106 | widget_class = GTK_WIDGET_CLASS (klass); |
---|
107 | |
---|
108 | unit_selector_parent_class = g_type_class_peek_parent (klass); |
---|
109 | |
---|
110 | gnome_print_unit_selector_signals[GNOME_PRINT_UNIT_SELECTOR_MODIFIED] = |
---|
111 | g_signal_new ("modified", |
---|
112 | G_OBJECT_CLASS_TYPE (object_class), |
---|
113 | G_SIGNAL_RUN_FIRST, |
---|
114 | G_STRUCT_OFFSET (GnomePrintUnitSelectorClass, |
---|
115 | modified), |
---|
116 | NULL, NULL, |
---|
117 | g_cclosure_marshal_VOID__VOID, |
---|
118 | G_TYPE_NONE, 0); |
---|
119 | |
---|
120 | object_class->finalize = gnome_print_unit_selector_finalize; |
---|
121 | } |
---|
122 | |
---|
123 | static void |
---|
124 | cb_gpus_combo_changed (GtkWidget *combo, GnomePrintUnitSelector *us) |
---|
125 | { |
---|
126 | const GnomePrintUnit *unit; |
---|
127 | GtkTreeModel *model; |
---|
128 | GtkTreePath *path; |
---|
129 | GtkTreeIter iter; |
---|
130 | |
---|
131 | model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo)); |
---|
132 | path = gtk_tree_path_new_from_indices (gtk_combo_box_get_active (GTK_COMBO_BOX (combo)), |
---|
133 | -1); |
---|
134 | gtk_tree_model_get_iter (model, &iter, path); |
---|
135 | gtk_tree_path_free (path); |
---|
136 | |
---|
137 | gtk_tree_model_get (model, &iter, |
---|
138 | COL_UNIT, &unit, |
---|
139 | -1); |
---|
140 | |
---|
141 | g_return_if_fail (unit != NULL); |
---|
142 | |
---|
143 | #ifdef GPP_VERBOSE |
---|
144 | g_print ("Old unit %s new unit %s\n", us->unit->name, unit->name); |
---|
145 | #endif |
---|
146 | if (us->unit == unit) |
---|
147 | return; |
---|
148 | |
---|
149 | gnome_print_unit_selector_recalculate_adjustments (us, unit); |
---|
150 | |
---|
151 | g_signal_emit (G_OBJECT (us), |
---|
152 | gnome_print_unit_selector_signals[GNOME_PRINT_UNIT_SELECTOR_MODIFIED], |
---|
153 | 0); |
---|
154 | } |
---|
155 | |
---|
156 | static void |
---|
157 | gnome_print_unit_selector_init (GnomePrintUnitSelector *us) |
---|
158 | { |
---|
159 | GtkCellRenderer *cell; |
---|
160 | |
---|
161 | us->ctmscale = 1.0; |
---|
162 | us->devicescale = 1.0; |
---|
163 | us->abbr = FALSE; |
---|
164 | us->plural = TRUE; |
---|
165 | |
---|
166 | us->combo = gtk_combo_box_new_with_model ( |
---|
167 | (GtkTreeModel *)gtk_list_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_POINTER)); |
---|
168 | cell = gtk_cell_renderer_text_new (); |
---|
169 | gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (us->combo), cell, TRUE); |
---|
170 | gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (us->combo), cell, |
---|
171 | "text", 0, |
---|
172 | NULL); |
---|
173 | g_signal_connect (G_OBJECT (us->combo), |
---|
174 | "changed", G_CALLBACK (cb_gpus_combo_changed), us); |
---|
175 | gtk_widget_show (us->combo); |
---|
176 | gtk_box_pack_start (GTK_BOX (us), us->combo, TRUE, TRUE, 0); |
---|
177 | } |
---|
178 | |
---|
179 | static void |
---|
180 | gnome_print_unit_selector_finalize (GObject *object) |
---|
181 | { |
---|
182 | GnomePrintUnitSelector *selector; |
---|
183 | |
---|
184 | selector = GNOME_PRINT_UNIT_SELECTOR (object); |
---|
185 | |
---|
186 | if (selector->combo) { |
---|
187 | selector->combo = NULL; |
---|
188 | } |
---|
189 | |
---|
190 | while (selector->adjustments) { |
---|
191 | g_object_unref (G_OBJECT (selector->adjustments->data)); |
---|
192 | selector->adjustments = g_list_remove (selector->adjustments, selector->adjustments->data); |
---|
193 | } |
---|
194 | |
---|
195 | if (selector->units) { |
---|
196 | gnome_print_unit_free_list (selector->units); |
---|
197 | } |
---|
198 | |
---|
199 | selector->unit = NULL; |
---|
200 | |
---|
201 | G_OBJECT_CLASS (unit_selector_parent_class)->finalize (object); |
---|
202 | } |
---|
203 | |
---|
204 | GtkWidget * |
---|
205 | gnome_print_unit_selector_new (guint bases) |
---|
206 | { |
---|
207 | GnomePrintUnitSelector *us; |
---|
208 | |
---|
209 | us = g_object_new (GNOME_TYPE_PRINT_UNIT_SELECTOR, NULL); |
---|
210 | |
---|
211 | gnome_print_unit_selector_set_bases (us, bases); |
---|
212 | |
---|
213 | return (GtkWidget *) us; |
---|
214 | } |
---|
215 | |
---|
216 | static void |
---|
217 | gnome_print_unit_selector_recalculate_adjustments (GnomePrintUnitSelector *us, |
---|
218 | const GnomePrintUnit *unit) |
---|
219 | { |
---|
220 | GList *l; |
---|
221 | const GnomePrintUnit *old; |
---|
222 | |
---|
223 | old = us->unit; |
---|
224 | us->unit = unit; |
---|
225 | for (l = us->adjustments; l != NULL; l = l->next) { |
---|
226 | GtkAdjustment *adj; |
---|
227 | adj = GTK_ADJUSTMENT (l->data); |
---|
228 | #ifdef GPP_VERBOSE |
---|
229 | g_print ("Old val %g ... ", adj->value); |
---|
230 | #endif |
---|
231 | gnome_print_convert_distance_full (&adj->value, old, unit, |
---|
232 | us->ctmscale, us->devicescale); |
---|
233 | gnome_print_convert_distance_full (&adj->lower, old, unit, |
---|
234 | us->ctmscale, us->devicescale); |
---|
235 | gnome_print_convert_distance_full (&adj->upper, old, unit, |
---|
236 | us->ctmscale, us->devicescale); |
---|
237 | #ifdef GPP_VERBOSE |
---|
238 | g_print ("new val %g\n", adj->value); |
---|
239 | #endif |
---|
240 | gtk_adjustment_changed (adj); |
---|
241 | gtk_adjustment_value_changed (adj); |
---|
242 | |
---|
243 | } |
---|
244 | |
---|
245 | } |
---|
246 | |
---|
247 | const GnomePrintUnit * |
---|
248 | gnome_print_unit_selector_get_unit (GnomePrintUnitSelector *us) |
---|
249 | { |
---|
250 | g_return_val_if_fail (us != NULL, NULL); |
---|
251 | g_return_val_if_fail (GNOME_IS_PRINT_UNIT_SELECTOR (us), NULL); |
---|
252 | |
---|
253 | return us->unit; |
---|
254 | } |
---|
255 | |
---|
256 | static void |
---|
257 | gpus_rebuild_menu (GnomePrintUnitSelector *us) |
---|
258 | { |
---|
259 | GList *l; |
---|
260 | gint pos, p; |
---|
261 | GtkTreeModel *model; |
---|
262 | |
---|
263 | model = gtk_combo_box_get_model (GTK_COMBO_BOX (us->combo)); |
---|
264 | gtk_list_store_clear (GTK_LIST_STORE (model)); |
---|
265 | |
---|
266 | pos = p = 0; |
---|
267 | for (l = us->units; l != NULL; l = l->next) { |
---|
268 | const GnomePrintUnit *u; |
---|
269 | GtkTreeIter iter; |
---|
270 | gchar *name; |
---|
271 | |
---|
272 | u = l->data; |
---|
273 | |
---|
274 | name = gnome_print_unit_get_name (u, us->plural, us->abbr, 0); |
---|
275 | gtk_list_store_append (GTK_LIST_STORE (model), &iter); |
---|
276 | gtk_list_store_set (GTK_LIST_STORE (model), &iter, |
---|
277 | COL_NAME, name, |
---|
278 | COL_UNIT, u, |
---|
279 | -1); |
---|
280 | g_free (name); |
---|
281 | |
---|
282 | if (u == us->unit) |
---|
283 | pos = p; |
---|
284 | p += 1; |
---|
285 | } |
---|
286 | |
---|
287 | gtk_combo_box_set_active (GTK_COMBO_BOX (us->combo), pos); |
---|
288 | } |
---|
289 | |
---|
290 | void |
---|
291 | gnome_print_unit_selector_set_bases (GnomePrintUnitSelector *us, guint bases) |
---|
292 | { |
---|
293 | GList *units; |
---|
294 | |
---|
295 | g_return_if_fail (us != NULL); |
---|
296 | g_return_if_fail (GNOME_IS_PRINT_UNIT_SELECTOR (us)); |
---|
297 | |
---|
298 | if (bases == us->bases) |
---|
299 | return; |
---|
300 | |
---|
301 | units = gnome_print_unit_get_list (bases); |
---|
302 | g_return_if_fail (units != NULL); |
---|
303 | gnome_print_unit_free_list (us->units); |
---|
304 | us->units = units; |
---|
305 | us->unit = units->data; |
---|
306 | |
---|
307 | gpus_rebuild_menu (us); |
---|
308 | } |
---|
309 | |
---|
310 | void |
---|
311 | gnome_print_unit_selector_set_unit (GnomePrintUnitSelector *us, const GnomePrintUnit *unit) |
---|
312 | { |
---|
313 | gint pos; |
---|
314 | |
---|
315 | g_return_if_fail (us != NULL); |
---|
316 | g_return_if_fail (GNOME_IS_PRINT_UNIT_SELECTOR (us)); |
---|
317 | g_return_if_fail (unit != NULL); |
---|
318 | |
---|
319 | if (unit == us->unit) |
---|
320 | return; |
---|
321 | |
---|
322 | pos = g_list_index (us->units, unit); |
---|
323 | g_return_if_fail (pos >= 0); |
---|
324 | |
---|
325 | gnome_print_unit_selector_recalculate_adjustments (us, unit); |
---|
326 | gtk_combo_box_set_active (GTK_COMBO_BOX (us->combo), pos); |
---|
327 | } |
---|
328 | |
---|
329 | void |
---|
330 | gnome_print_unit_selector_add_adjustment (GnomePrintUnitSelector *us, GtkAdjustment *adj) |
---|
331 | { |
---|
332 | g_return_if_fail (us != NULL); |
---|
333 | g_return_if_fail (GNOME_IS_PRINT_UNIT_SELECTOR (us)); |
---|
334 | g_return_if_fail (adj != NULL); |
---|
335 | g_return_if_fail (GTK_IS_ADJUSTMENT (adj)); |
---|
336 | |
---|
337 | g_return_if_fail (!g_list_find (us->adjustments, adj)); |
---|
338 | |
---|
339 | g_object_ref (G_OBJECT (adj)); |
---|
340 | us->adjustments = g_list_prepend (us->adjustments, adj); |
---|
341 | } |
---|
342 | |
---|
343 | void |
---|
344 | gnome_print_unit_selector_remove_adjustment (GnomePrintUnitSelector *us, GtkAdjustment *adj) |
---|
345 | { |
---|
346 | g_return_if_fail (us != NULL); |
---|
347 | g_return_if_fail (GNOME_IS_PRINT_UNIT_SELECTOR (us)); |
---|
348 | g_return_if_fail (adj != NULL); |
---|
349 | g_return_if_fail (GTK_IS_ADJUSTMENT (adj)); |
---|
350 | |
---|
351 | g_return_if_fail (g_list_find (us->adjustments, adj)); |
---|
352 | |
---|
353 | us->adjustments = g_list_remove (us->adjustments, adj); |
---|
354 | g_object_unref (G_OBJECT (adj)); |
---|
355 | } |
---|