source: trunk/third/gtk/gtk/gtkgamma.c @ 14482

Revision 14482, 12.4 KB checked in by ghudson, 25 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r14481, which included commits to RCS files with non-trunk default branches.
Line 
1/* GTK - The GIMP Toolkit
2 * Copyright (C) 1997 David Mosberger
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 */
19
20/*
21 * Modified by the GTK+ Team and others 1997-1999.  See the AUTHORS
22 * file for a list of people on the GTK+ Team.  See the ChangeLog
23 * files for a list of changes.  These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
25 */
26
27#include <string.h>
28#include <stdlib.h>
29#include <stdio.h>
30
31#include "gtkgamma.h"
32#include "gtkcurve.h"
33#include "gtkdialog.h"
34#include "gtkdrawingarea.h"
35#include "gtkentry.h"
36#include "gtkhbox.h"
37#include "gtklabel.h"
38#include "gtkmain.h"
39#include "gtkpixmap.h"
40#include "gtkradiobutton.h"
41#include "gtksignal.h"
42#include "gtktable.h"
43#include "gtkvbox.h"
44#include "gtkwindow.h"
45#include "gtkintl.h"
46
47static GtkVBoxClass *parent_class = NULL;
48
49
50/* forward declarations: */
51static void gtk_gamma_curve_class_init (GtkGammaCurveClass *class);
52static void gtk_gamma_curve_init (GtkGammaCurve *curve);
53static void gtk_gamma_curve_destroy (GtkObject *object);
54
55static void curve_type_changed_callback (GtkWidget *w, gpointer data);
56static void button_realize_callback     (GtkWidget *w);
57static void button_toggled_callback     (GtkWidget *w, gpointer data);
58static void button_clicked_callback     (GtkWidget *w, gpointer data);
59
60enum
61  {
62    LINEAR = 0,
63    SPLINE,
64    FREE,
65    GAMMA,
66    RESET,
67    NUM_XPMS
68  };
69
70static const char *xpm[][27] =
71  {
72    /* spline: */
73    {
74      /* width height ncolors chars_per_pixel */
75      "16 16 4 1",
76      /* colors */
77      ". c None",
78      "B c #000000",
79      "+ c #BC2D2D",
80      "r c #FF0000",
81      /* pixels */
82      "..............BB",
83      ".........rrrrrrB",
84      ".......rr.......",
85      ".....B+.........",
86      "....BBB.........",
87      "....+B..........",
88      "....r...........",
89      "...r............",
90      "...r............",
91      "..r.............",
92      "..r.............",
93      ".r..............",
94      ".r..............",
95      ".r..............",
96      "B+..............",
97      "BB.............."
98    },
99    /* linear: */
100    {
101      /* width height ncolors chars_per_pixel */
102      "16 16 5 1",
103      /* colors */
104      ". c None", /* transparent */
105      "B c #000000",
106      "' c #7F7F7F",
107      "+ c #824141",
108      "r c #FF0000",
109      /* pixels */
110      "..............BB",
111      "..............+B",
112      "..............r.",
113      ".............r..",
114      ".............r..",
115      "....'B'.....r...",
116      "....BBB.....r...",
117      "....+B+....r....",
118      "....r.r....r....",
119      "...r...r..r.....",
120      "...r...r..r.....",
121      "..r.....rB+.....",
122      "..r.....BBB.....",
123      ".r......'B'.....",
124      "B+..............",
125      "BB.............."
126    },
127    /* free: */
128    {
129      /* width height ncolors chars_per_pixel */
130      "16 16 2 1",
131      /* colors */
132      ". c None",
133      "r c #FF0000",
134      /* pixels */
135      "................",
136      "................",
137      "......r.........",
138      "......r.........",
139      ".......r........",
140      ".......r........",
141      ".......r........",
142      "........r.......",
143      "........r.......",
144      "........r.......",
145      ".....r...r.rrrrr",
146      "....r....r......",
147      "...r.....r......",
148      "..r.......r.....",
149      ".r........r.....",
150      "r..............."
151    },
152    /* gamma: */
153    {
154      /* width height ncolors chars_per_pixel */
155      "16 16 10 1",
156      /* colors */
157      ". c None",
158      "B c #000000",
159      "& c #171717",
160      "# c #2F2F2F",
161      "X c #464646",
162      "= c #5E5E5E",
163      "/ c #757575",
164      "+ c #8C8C8C",
165      "- c #A4A4A4",
166      "` c #BBBBBB",
167      /* pixels */
168      "................",
169      "................",
170      "................",
171      "....B=..+B+.....",
172      "....X&`./&-.....",
173      "...../+.XX......",
174      "......B.B+......",
175      "......X.X.......",
176      "......X&+.......",
177      "......-B........",
178      "....../=........",
179      "......#B........",
180      "......BB........",
181      "......B#........",
182      "................",
183      "................"
184    },
185    /* reset: */
186    {
187      /* width height ncolors chars_per_pixel */
188      "16 16 4 1",
189      /* colors */
190      ". c None",
191      "B c #000000",
192      "+ c #824141",
193      "r c #FF0000",
194      /* pixels */
195      "..............BB",
196      "..............+B",
197      ".............r..",
198      "............r...",
199      "...........r....",
200      "..........r.....",
201      ".........r......",
202      "........r.......",
203      ".......r........",
204      "......r.........",
205      ".....r..........",
206      "....r...........",
207      "...r............",
208      "..r.............",
209      "B+..............",
210      "BB.............."
211    }
212  };
213
214guint
215gtk_gamma_curve_get_type (void)
216{
217  static guint gamma_curve_type = 0;
218
219  if (!gamma_curve_type)
220    {
221      static const GtkTypeInfo gamma_curve_info =
222      {
223        "GtkGammaCurve",
224        sizeof (GtkGammaCurve),
225        sizeof (GtkGammaCurveClass),
226        (GtkClassInitFunc) gtk_gamma_curve_class_init,
227        (GtkObjectInitFunc) gtk_gamma_curve_init,
228        /* reserved_1 */ NULL,
229        /* reserved_2 */ NULL,
230        (GtkClassInitFunc) NULL,
231      };
232
233      gamma_curve_type =
234        gtk_type_unique (gtk_vbox_get_type (), &gamma_curve_info);
235    }
236  return gamma_curve_type;
237}
238
239static void
240gtk_gamma_curve_class_init (GtkGammaCurveClass *class)
241{
242  GtkObjectClass *object_class;
243
244  parent_class = gtk_type_class (gtk_vbox_get_type ());
245
246  object_class = (GtkObjectClass *) class;
247  object_class->destroy = gtk_gamma_curve_destroy;
248}
249
250static void
251gtk_gamma_curve_init (GtkGammaCurve *curve)
252{
253  GtkWidget *vbox;
254  int i;
255
256  curve->gamma = 1.0;
257
258  curve->table = gtk_table_new (1, 2, FALSE);
259  gtk_table_set_col_spacings (GTK_TABLE (curve->table), 3);
260  gtk_container_add (GTK_CONTAINER (curve), curve->table);
261
262  curve->curve = gtk_curve_new ();
263  gtk_signal_connect (GTK_OBJECT (curve->curve), "curve_type_changed",
264                      (GtkSignalFunc) curve_type_changed_callback, curve);
265  gtk_table_attach_defaults (GTK_TABLE (curve->table), curve->curve, 0, 1, 0, 1);
266
267  vbox = gtk_vbox_new (/* homogeneous */ FALSE, /* spacing */ 3);
268  gtk_table_attach (GTK_TABLE (curve->table), vbox, 1, 2, 0, 1, 0, 0, 0, 0);
269
270  /* toggle buttons: */
271  for (i = 0; i < 3; ++i)
272    {
273      curve->button[i] = gtk_toggle_button_new ();
274      gtk_object_set_data (GTK_OBJECT (curve->button[i]), "_GtkGammaCurveIndex",
275                           GINT_TO_POINTER (i));
276      gtk_container_add (GTK_CONTAINER (vbox), curve->button[i]);
277      gtk_signal_connect (GTK_OBJECT (curve->button[i]), "realize",
278                          (GtkSignalFunc) button_realize_callback, 0);
279      gtk_signal_connect (GTK_OBJECT (curve->button[i]), "toggled",
280                          (GtkSignalFunc) button_toggled_callback, curve);
281      gtk_widget_show (curve->button[i]);
282    }
283
284  /* push buttons: */
285  for (i = 3; i < 5; ++i)
286    {
287      curve->button[i] = gtk_button_new ();
288      gtk_object_set_data (GTK_OBJECT (curve->button[i]), "_GtkGammaCurveIndex",
289                           GINT_TO_POINTER (i));
290      gtk_container_add (GTK_CONTAINER (vbox), curve->button[i]);
291      gtk_signal_connect (GTK_OBJECT (curve->button[i]), "realize",
292                          (GtkSignalFunc) button_realize_callback, 0);
293      gtk_signal_connect (GTK_OBJECT (curve->button[i]), "clicked",
294                          (GtkSignalFunc) button_clicked_callback, curve);
295      gtk_widget_show (curve->button[i]);
296    }
297
298  gtk_widget_show (vbox);
299  gtk_widget_show (curve->table);
300  gtk_widget_show (curve->curve);
301}
302
303static void
304button_realize_callback (GtkWidget *w)
305{
306  GtkWidget *pixmap;
307  GdkBitmap *mask;
308  GdkPixmap *pm;
309  int i;
310
311  i = GPOINTER_TO_INT (gtk_object_get_data (GTK_OBJECT (w), "_GtkGammaCurveIndex"));
312  pm = gdk_pixmap_create_from_xpm_d (w->window, &mask,
313                                     &w->style->bg[GTK_STATE_NORMAL], (gchar **)xpm[i]);
314
315  pixmap = gtk_pixmap_new (pm, mask);
316  gtk_container_add (GTK_CONTAINER (w), pixmap);
317  gtk_widget_show (pixmap);
318
319  gdk_pixmap_unref (pm);
320  gdk_bitmap_unref (mask);   /* a bitmap is really just a special pixmap */
321}
322
323static void
324button_toggled_callback (GtkWidget *w, gpointer data)
325{
326  GtkGammaCurve *c = data;
327  GtkCurveType type;
328  int active, i;
329
330  if (!GTK_TOGGLE_BUTTON (w)->active)
331    return;
332
333  active = GPOINTER_TO_INT (gtk_object_get_data (GTK_OBJECT (w), "_GtkGammaCurveIndex"));
334
335  for (i = 0; i < 3; ++i)
336    if ((i != active) && GTK_TOGGLE_BUTTON (c->button[i])->active)
337      break;
338
339  if (i < 3)
340    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (c->button[i]), FALSE);
341
342  switch (active)
343    {
344    case 0:  type = GTK_CURVE_TYPE_SPLINE; break;
345    case 1:  type = GTK_CURVE_TYPE_LINEAR; break;
346    default: type = GTK_CURVE_TYPE_FREE; break;
347    }
348  gtk_curve_set_curve_type (GTK_CURVE (c->curve), type);
349}
350
351static void
352gamma_cancel_callback (GtkWidget *w, gpointer data)
353{
354  GtkGammaCurve *c = data;
355
356  gtk_widget_destroy (c->gamma_dialog);
357  c->gamma_dialog = 0;
358}
359
360static void
361gamma_ok_callback (GtkWidget *w, gpointer data)
362{
363  GtkGammaCurve *c = data;
364  gchar *start, *end;
365  gfloat v;
366
367  start = gtk_entry_get_text (GTK_ENTRY (c->gamma_text));
368  if (start)
369    {
370      v = strtod (start, &end);
371      if (end > start && v > 0.0)
372        c->gamma = v;
373    }
374  gtk_curve_set_gamma (GTK_CURVE (c->curve), c->gamma);
375  gamma_cancel_callback (w, data);
376}
377
378static void
379button_clicked_callback (GtkWidget *w, gpointer data)
380{
381  GtkGammaCurve *c = data;
382  int active;
383
384  active = GPOINTER_TO_INT (gtk_object_get_data (GTK_OBJECT (w), "_GtkGammaCurveIndex"));
385  if (active == 3)
386    {
387      /* set gamma */
388      if (c->gamma_dialog)
389        return;
390      else
391        {
392          GtkWidget *vbox, *hbox, *label, *button;
393          gchar buf[64];
394         
395          c->gamma_dialog = gtk_dialog_new ();
396          gtk_window_set_title (GTK_WINDOW (c->gamma_dialog), _("Gamma"));
397          vbox = GTK_DIALOG (c->gamma_dialog)->vbox;
398         
399          hbox = gtk_hbox_new (/* homogeneous */ FALSE, 0);
400          gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 2);
401          gtk_widget_show (hbox);
402         
403          label = gtk_label_new (_("Gamma value"));
404          gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 2);
405          gtk_widget_show (label);
406         
407          sprintf (buf, "%g", c->gamma);
408          c->gamma_text = gtk_entry_new ();
409          gtk_entry_set_text (GTK_ENTRY (c->gamma_text), buf);
410          gtk_box_pack_start (GTK_BOX (hbox), c->gamma_text, TRUE, TRUE, 2);
411          gtk_widget_show (c->gamma_text);
412         
413          /* fill in action area: */
414          hbox = GTK_DIALOG (c->gamma_dialog)->action_area;
415         
416          button = gtk_button_new_with_label (_("OK"));
417          GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
418          gtk_signal_connect (GTK_OBJECT (button), "clicked",
419                              (GtkSignalFunc) gamma_ok_callback, c);
420          gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
421          gtk_widget_grab_default (button);
422          gtk_widget_show (button);
423         
424          button = gtk_button_new_with_label (_("Cancel"));
425          gtk_signal_connect (GTK_OBJECT (button), "clicked",
426                              (GtkSignalFunc) gamma_cancel_callback, c);
427          gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
428          gtk_widget_show (button);
429         
430          gtk_widget_show (c->gamma_dialog);
431        }
432    }
433  else
434    {
435      /* reset */
436      gtk_curve_reset (GTK_CURVE (c->curve));
437    }
438}
439
440static void
441curve_type_changed_callback (GtkWidget *w, gpointer data)
442{
443  GtkGammaCurve *c = data;
444  GtkCurveType new_type;
445  int active;
446
447  new_type = GTK_CURVE (w)->curve_type;
448  switch (new_type)
449    {
450    case GTK_CURVE_TYPE_SPLINE: active = 0; break;
451    case GTK_CURVE_TYPE_LINEAR: active = 1; break;
452    default:                    active = 2; break;
453    }
454  if (!GTK_TOGGLE_BUTTON (c->button[active])->active)
455    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (c->button[active]), TRUE);
456}
457
458GtkWidget*
459gtk_gamma_curve_new (void)
460{
461  return gtk_type_new (gtk_gamma_curve_get_type ());
462}
463
464static void
465gtk_gamma_curve_destroy (GtkObject *object)
466{
467  GtkGammaCurve *c;
468
469  g_return_if_fail (object != NULL);
470  g_return_if_fail (GTK_IS_GAMMA_CURVE (object));
471
472  c = GTK_GAMMA_CURVE (object);
473
474  if (c->gamma_dialog)
475    gtk_widget_destroy (c->gamma_dialog);
476
477  if (GTK_OBJECT_CLASS (parent_class)->destroy)
478    (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
479}
480
Note: See TracBrowser for help on using the repository browser.