Debian Codegraph @

src/gutenprintui2/gammacurve.c

raw symbols SCIP
1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1997 David Mosberger
3 * 2004 Roger Leigh
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 <https://www.gnu.org/licenses/>.
17 */
18
19 /*
20 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
21 * file for a list of people on the GTK+ Team. See the ChangeLog
22 * files for a list of changes. These files are distributed with
23 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
24 */
25
26 /*
27 * Incorporated into Gutenprint by Roger Leigh in 2004. Subsequent
28 * changes are by the Gutenprint team.
29 */
30
31 #include <config.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35
36 #include <gutenprint/gutenprint-intl-internal.h>
37 #include <gutenprintui2/gutenprintui.h>
38 #include "gutenprintui-internal.h"
39
40 static GtkVBoxClass *parent_class = NULL;
41
42
43 /* forward declarations: */
44 static void stpui_gamma_curve_class_init (StpuiGammaCurveClass *class, void *classdata);
45 static void stpui_gamma_curve_init (StpuiGammaCurve *curve, gpointer g_class);
46 static void stpui_gamma_curve_destroy (GtkObject *object);
47
48 static void curve_type_changed_callback (GtkWidget *w, gpointer data);
49 static void button_realize_callback (GtkWidget *w);
50 static void button_toggled_callback (GtkWidget *w, gpointer data);
51 static void button_clicked_callback (GtkWidget *w, gpointer data);
52
53 enum
54 {
55 LINEAR = 0,
56 SPLINE,
57 FREE,
58 GAMMA,
59 RESET,
60 NUM_XPMS
61 };
62
63 static const char *xpm[][27] =
64 {
65 /* spline: */
66 {
67 /* width height ncolors chars_per_pixel */
68 "16 16 4 1",
69 /* colors */
70 ". c None",
71 "B c #000000",
72 "+ c #BC2D2D",
73 "r c #FF0000",
74 /* pixels */
75 "..............BB",
76 ".........rrrrrrB",
77 ".......rr.......",
78 ".....B+.........",
79 "....BBB.........",
80 "....+B..........",
81 "....r...........",
82 "...r............",
83 "...r............",
84 "..r.............",
85 "..r.............",
86 ".r..............",
87 ".r..............",
88 ".r..............",
89 "B+..............",
90 "BB.............."
91 },
92 /* linear: */
93 {
94 /* width height ncolors chars_per_pixel */
95 "16 16 5 1",
96 /* colors */
97 ". c None", /* transparent */
98 "B c #000000",
99 "' c #7F7F7F",
100 "+ c #824141",
101 "r c #FF0000",
102 /* pixels */
103 "..............BB",
104 "..............+B",
105 "..............r.",
106 ".............r..",
107 ".............r..",
108 "....'B'.....r...",
109 "....BBB.....r...",
110 "....+B+....r....",
111 "....r.r....r....",
112 "...r...r..r.....",
113 "...r...r..r.....",
114 "..r.....rB+.....",
115 "..r.....BBB.....",
116 ".r......'B'.....",
117 "B+..............",
118 "BB.............."
119 },
120 /* free: */
121 {
122 /* width height ncolors chars_per_pixel */
123 "16 16 2 1",
124 /* colors */
125 ". c None",
126 "r c #FF0000",
127 /* pixels */
128 "................",
129 "................",
130 "......r.........",
131 "......r.........",
132 ".......r........",
133 ".......r........",
134 ".......r........",
135 "........r.......",
136 "........r.......",
137 "........r.......",
138 ".....r...r.rrrrr",
139 "....r....r......",
140 "...r.....r......",
141 "..r.......r.....",
142 ".r........r.....",
143 "r..............."
144 },
145 /* gamma: */
146 {
147 /* width height ncolors chars_per_pixel */
148 "16 16 10 1",
149 /* colors */
150 ". c None",
151 "B c #000000",
152 "& c #171717",
153 "# c #2F2F2F",
154 "X c #464646",
155 "= c #5E5E5E",
156 "/ c #757575",
157 "+ c #8C8C8C",
158 "- c #A4A4A4",
159 "` c #BBBBBB",
160 /* pixels */
161 "................",
162 "................",
163 "................",
164 "....B=..+B+.....",
165 "....X&`./&-.....",
166 "...../+.XX......",
167 "......B.B+......",
168 "......X.X.......",
169 "......X&+.......",
170 "......-B........",
171 "....../=........",
172 "......#B........",
173 "......BB........",
174 "......B#........",
175 "................",
176 "................"
177 },
178 /* reset: */
179 {
180 /* width height ncolors chars_per_pixel */
181 "16 16 4 1",
182 /* colors */
183 ". c None",
184 "B c #000000",
185 "+ c #824141",
186 "r c #FF0000",
187 /* pixels */
188 "..............BB",
189 "..............+B",
190 ".............r..",
191 "............r...",
192 "...........r....",
193 "..........r.....",
194 ".........r......",
195 "........r.......",
196 ".......r........",
197 "......r.........",
198 ".....r..........",
199 "....r...........",
200 "...r............",
201 "..r.............",
202 "B+..............",
203 "BB.............."
204 }
205 };
206
207 GType
208 stpui_gamma_curve_get_type (void)
209 {
210 static GType gamma_curve_type = 0;
211
212 if (!gamma_curve_type)
213 {
214 static const GTypeInfo gamma_curve_info =
215 {
216 sizeof (StpuiGammaCurveClass),
217 NULL, /* base_init */
218 NULL, /* base_finalize */
219 (GClassInitFunc) stpui_gamma_curve_class_init,
220 NULL, /* class_finalize */
221 NULL, /* class_data */
222 sizeof (StpuiGammaCurve),
223 0, /* n_preallocs */
224 (GInstanceInitFunc) stpui_gamma_curve_init,
225 NULL, /* value_table */
226 };
227
228 gamma_curve_type = g_type_register_static (GTK_TYPE_VBOX, "StpuiGammaCurve",
229 &gamma_curve_info, 0);
230 }
231 return gamma_curve_type;
232 }
233
234 static void
235 stpui_gamma_curve_class_init (StpuiGammaCurveClass *class, void *classdata)
236 {
237 GtkObjectClass *object_class;
238 (void)classdata;
239
240 parent_class = g_type_class_peek_parent (class);
241
242 object_class = (GtkObjectClass *) class;
243 object_class->destroy = stpui_gamma_curve_destroy;
244 }
245
246 static void
247 stpui_gamma_curve_init (StpuiGammaCurve *curve, gpointer g_class)
248 {
249 GtkWidget *vbox;
250 int i;
251 (void)g_class;
252
253 curve->gamma = 1.0;
254
255 curve->table = gtk_table_new (1, 2, FALSE);
256 gtk_table_set_col_spacings (GTK_TABLE (curve->table), 3);
257 gtk_container_add (GTK_CONTAINER (curve), curve->table);
258
259 curve->curve = stpui_curve_new ();
260 g_signal_connect (curve->curve, "curve_type_changed",
261 G_CALLBACK (curve_type_changed_callback), curve);
262 gtk_table_attach_defaults (GTK_TABLE (curve->table), curve->curve, 0, 1, 0, 1);
263
264 vbox = gtk_vbox_new (/* homogeneous */ FALSE, /* spacing */ 3);
265 gtk_table_attach (GTK_TABLE (curve->table), vbox, 1, 2, 0, 1, 0, 0, 0, 0);
266
267 /* toggle buttons: */
268 for (i = 0; i < 3; ++i)
269 {
270 curve->button[i] = gtk_toggle_button_new ();
271 g_object_set_data (G_OBJECT (curve->button[i]), "_StpuiGammaCurveIndex",
272 GINT_TO_POINTER (i));
273 gtk_container_add (GTK_CONTAINER (vbox), curve->button[i]);
274 g_signal_connect (curve->button[i], "realize",
275 G_CALLBACK (button_realize_callback), NULL);
276 g_signal_connect (curve->button[i], "toggled",
277 G_CALLBACK (button_toggled_callback), curve);
278 gtk_widget_show (curve->button[i]);
279 }
280
281 /* push buttons: */
282 for (i = 3; i < 5; ++i)
283 {
284 curve->button[i] = gtk_button_new ();
285 g_object_set_data (G_OBJECT (curve->button[i]), "_StpuiGammaCurveIndex",
286 GINT_TO_POINTER (i));
287 gtk_container_add (GTK_CONTAINER (vbox), curve->button[i]);
288 g_signal_connect (curve->button[i], "realize",
289 G_CALLBACK (button_realize_callback), NULL);
290 g_signal_connect (curve->button[i], "clicked",
291 G_CALLBACK (button_clicked_callback), curve);
292 gtk_widget_show (curve->button[i]);
293 }
294
295 gtk_widget_show (vbox);
296 gtk_widget_show (curve->table);
297 gtk_widget_show (curve->curve);
298 }
299
300 static void
301 button_realize_callback (GtkWidget *w)
302 {
303 GtkWidget *pixmap;
304 GdkBitmap *mask;
305 GdkPixmap *pm;
306 int i;
307
308 i = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (w), "_StpuiGammaCurveIndex"));
309 pm = gdk_pixmap_create_from_xpm_d (w->window, &mask,
310 &w->style->bg[GTK_STATE_NORMAL],
311 (gchar **) cast_safe(xpm[i]));
312
313 pixmap = gtk_image_new_from_pixmap (pm, mask);
314 gtk_container_add (GTK_CONTAINER (w), pixmap);
315 gtk_widget_show (pixmap);
316
317 g_object_unref (pm);
318 g_object_unref (mask);
319 }
320
321 static void
322 button_toggled_callback (GtkWidget *w, gpointer data)
323 {
324 StpuiGammaCurve *c = data;
325 StpuiCurveType type;
326 int active, i;
327
328 if (!GTK_TOGGLE_BUTTON (w)->active)
329 return;
330
331 active = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (w), "_StpuiGammaCurveIndex"));
332
333 for (i = 0; i < 3; ++i)
334 if ((i != active) && GTK_TOGGLE_BUTTON (c->button[i])->active)
335 break;
336
337 if (i < 3)
338 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (c->button[i]), FALSE);
339
340 switch (active)
341 {
342 case 0: type = STPUI_CURVE_TYPE_SPLINE; break;
343 case 1: type = STPUI_CURVE_TYPE_LINEAR; break;
344 default: type = STPUI_CURVE_TYPE_FREE; break;
345 }
346 stpui_curve_set_curve_type (STPUI_CURVE (c->curve), type);
347 }
348
349 static void
350 gamma_cancel_callback (GtkWidget *w, gpointer data)
351 {
352 (void)w;
353 StpuiGammaCurve *c = data;
354
355 gtk_widget_destroy (c->gamma_dialog);
356 }
357
358 static void
359 gamma_ok_callback (GtkWidget *w, gpointer data)
360 {
361 StpuiGammaCurve *c = data;
362 const gchar *start;
363 gchar *end;
364 gfloat v;
365
366 start = gtk_entry_get_text (GTK_ENTRY (c->gamma_text));
367 if (start)
368 {
369 v = g_strtod (start, &end);
370 if (end > start && v > 0.0)
371 c->gamma = v;
372 }
373 stpui_curve_set_gamma (STPUI_CURVE (c->curve), c->gamma);
374 gamma_cancel_callback (w, data);
375 }
376
377 static void
378 button_clicked_callback (GtkWidget *w, gpointer data)
379 {
380 StpuiGammaCurve *c = data;
381 int active;
382
383 active = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (w), "_StpuiGammaCurveIndex"));
384 if (active == 3)
385 {
386 /* set gamma */
387 if (c->gamma_dialog)
388 return;
389 else
390 {
391 GtkWidget *vbox, *hbox, *label, *button;
392 gchar buf[64];
393
394 c->gamma_dialog = gtk_dialog_new ();
395 gtk_window_set_screen (GTK_WINDOW (c->gamma_dialog),
396 gtk_widget_get_screen (w));
397 gtk_window_set_title (GTK_WINDOW (c->gamma_dialog), _("Gamma"));
398 g_object_add_weak_pointer (G_OBJECT (c->gamma_dialog),
399 (gpointer *)&c->gamma_dialog);
400
401 vbox = GTK_DIALOG (c->gamma_dialog)->vbox;
402
403 hbox = gtk_hbox_new (/* homogeneous */ FALSE, 0);
404 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 2);
405 gtk_widget_show (hbox);
406
407 label = gtk_label_new_with_mnemonic (_("_Gamma value"));
408 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 2);
409 gtk_widget_show (label);
410
411 sprintf (buf, "%g", c->gamma);
412 c->gamma_text = gtk_entry_new ();
413 gtk_label_set_mnemonic_widget (GTK_LABEL (label), c->gamma_text);
414 gtk_entry_set_text (GTK_ENTRY (c->gamma_text), buf);
415 gtk_box_pack_start (GTK_BOX (hbox), c->gamma_text, TRUE, TRUE, 2);
416 gtk_widget_show (c->gamma_text);
417
418 /* fill in action area: */
419 hbox = GTK_DIALOG (c->gamma_dialog)->action_area;
420
421 button = gtk_button_new_from_stock (GTK_STOCK_CANCEL);
422 g_signal_connect (button, "clicked",
423 G_CALLBACK (gamma_cancel_callback), c);
424 gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
425 gtk_widget_show (button);
426
427 button = gtk_button_new_from_stock (GTK_STOCK_OK);
428 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
429 g_signal_connect (button, "clicked",
430 G_CALLBACK (gamma_ok_callback), c);
431 gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
432 gtk_widget_grab_default (button);
433 gtk_widget_show (button);
434
435 gtk_widget_show (c->gamma_dialog);
436 }
437 }
438 else
439 {
440 /* reset */
441 stpui_curve_reset (STPUI_CURVE (c->curve));
442 }
443 }
444
445 static void
446 curve_type_changed_callback (GtkWidget *w, gpointer data)
447 {
448 StpuiGammaCurve *c = data;
449 StpuiCurveType new_type;
450 int active;
451
452 new_type = STPUI_CURVE (w)->curve_type;
453 switch (new_type)
454 {
455 case STPUI_CURVE_TYPE_SPLINE: active = 0; break;
456 case STPUI_CURVE_TYPE_LINEAR: active = 1; break;
457 default: active = 2; break;
458 }
459 if (!GTK_TOGGLE_BUTTON (c->button[active])->active)
460 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (c->button[active]), TRUE);
461 }
462
463 GtkWidget*
464 stpui_gamma_curve_new (void)
465 {
466 return g_object_new (STPUI_TYPE_GAMMA_CURVE, NULL);
467 }
468
469 static void
470 stpui_gamma_curve_destroy (GtkObject *object)
471 {
472 StpuiGammaCurve *c;
473
474 g_return_if_fail (STPUI_IS_GAMMA_CURVE (object));
475
476 c = STPUI_GAMMA_CURVE (object);
477
478 if (c->gamma_dialog)
479 gtk_widget_destroy (c->gamma_dialog);
480
481 if (GTK_OBJECT_CLASS (parent_class)->destroy)
482 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
483 }