gtkgamma.c 12.6 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4
/* GTK - The GIMP Toolkit
 * Copyright (C) 1997 David Mosberger
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10 11
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15 16 17
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
18
 */
19 20

/*
21
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22 23 24 25 26
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

27
#include <config.h>
Elliot Lee's avatar
Elliot Lee committed
28 29 30 31
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

32
#include "gtkalias.h"
Elliot Lee's avatar
Elliot Lee committed
33 34 35 36 37 38
#include "gtkgamma.h"
#include "gtkcurve.h"
#include "gtkdialog.h"
#include "gtkdrawingarea.h"
#include "gtkentry.h"
#include "gtkhbox.h"
Manish Singh's avatar
Manish Singh committed
39
#include "gtkimage.h"
Elliot Lee's avatar
Elliot Lee committed
40 41 42
#include "gtklabel.h"
#include "gtkmain.h"
#include "gtkradiobutton.h"
43
#include "gtkstock.h"
Elliot Lee's avatar
Elliot Lee committed
44 45 46
#include "gtktable.h"
#include "gtkvbox.h"
#include "gtkwindow.h"
47
#include "gtkintl.h"
Elliot Lee's avatar
Elliot Lee committed
48 49 50 51 52 53 54 55 56

static GtkVBoxClass *parent_class = NULL;


/* forward declarations: */
static void gtk_gamma_curve_class_init (GtkGammaCurveClass *class);
static void gtk_gamma_curve_init (GtkGammaCurve *curve);
static void gtk_gamma_curve_destroy (GtkObject *object);

57 58 59 60 61
static void curve_type_changed_callback (GtkWidget *w, gpointer data);
static void button_realize_callback     (GtkWidget *w);
static void button_toggled_callback     (GtkWidget *w, gpointer data);
static void button_clicked_callback     (GtkWidget *w, gpointer data);

Elliot Lee's avatar
Elliot Lee committed
62 63 64 65 66 67 68 69 70 71
enum
  {
    LINEAR = 0,
    SPLINE,
    FREE,
    GAMMA,
    RESET,
    NUM_XPMS
  };

72
static const char *const xpm[][27] =
Elliot Lee's avatar
Elliot Lee committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
  {
    /* spline: */
    {
      /* width height ncolors chars_per_pixel */
      "16 16 4 1",
      /* colors */
      ". c None",
      "B c #000000",
      "+ c #BC2D2D",
      "r c #FF0000",
      /* pixels */
      "..............BB",
      ".........rrrrrrB",
      ".......rr.......",
      ".....B+.........",
      "....BBB.........",
      "....+B..........",
      "....r...........",
      "...r............",
      "...r............",
      "..r.............",
      "..r.............",
      ".r..............",
      ".r..............",
      ".r..............",
      "B+..............",
      "BB.............."
    },
    /* linear: */
    {
      /* width height ncolors chars_per_pixel */
      "16 16 5 1",
      /* colors */
      ". c None", /* transparent */
      "B c #000000",
      "' c #7F7F7F",
      "+ c #824141",
      "r c #FF0000",
      /* pixels */
      "..............BB",
      "..............+B",
      "..............r.",
      ".............r..",
      ".............r..",
      "....'B'.....r...",
      "....BBB.....r...",
      "....+B+....r....",
      "....r.r....r....",
      "...r...r..r.....",
      "...r...r..r.....",
      "..r.....rB+.....",
      "..r.....BBB.....",
      ".r......'B'.....",
      "B+..............",
      "BB.............."
    },
    /* free: */
    {
      /* width height ncolors chars_per_pixel */
      "16 16 2 1",
      /* colors */
      ". c None",
      "r c #FF0000",
      /* pixels */
      "................",
      "................",
      "......r.........",
      "......r.........",
      ".......r........",
      ".......r........",
      ".......r........",
      "........r.......",
      "........r.......",
      "........r.......",
      ".....r...r.rrrrr",
      "....r....r......",
      "...r.....r......",
      "..r.......r.....",
      ".r........r.....",
      "r..............."
    },
    /* gamma: */
    {
      /* width height ncolors chars_per_pixel */
      "16 16 10 1",
      /* colors */
      ". c None",
      "B c #000000",
      "& c #171717",
      "# c #2F2F2F",
      "X c #464646",
      "= c #5E5E5E",
      "/ c #757575",
      "+ c #8C8C8C",
      "- c #A4A4A4",
      "` c #BBBBBB",
      /* pixels */
      "................",
      "................",
      "................",
      "....B=..+B+.....",
      "....X&`./&-.....",
      "...../+.XX......",
      "......B.B+......",
      "......X.X.......",
      "......X&+.......",
      "......-B........",
      "....../=........",
      "......#B........",
      "......BB........",
      "......B#........",
      "................",
      "................"
    },
    /* reset: */
    {
      /* width height ncolors chars_per_pixel */
      "16 16 4 1",
      /* colors */
      ". c None",
      "B c #000000",
      "+ c #824141",
      "r c #FF0000",
      /* pixels */
      "..............BB",
      "..............+B",
      ".............r..",
      "............r...",
      "...........r....",
      "..........r.....",
      ".........r......",
      "........r.......",
      ".......r........",
      "......r.........",
      ".....r..........",
      "....r...........",
      "...r............",
      "..r.............",
      "B+..............",
      "BB.............."
    }
  };

Manish Singh's avatar
Manish Singh committed
216
GType
Elliot Lee's avatar
Elliot Lee committed
217 218
gtk_gamma_curve_get_type (void)
{
Manish Singh's avatar
Manish Singh committed
219
  static GType gamma_curve_type = 0;
Tim Janik's avatar
Tim Janik committed
220
  
Elliot Lee's avatar
Elliot Lee committed
221 222
  if (!gamma_curve_type)
    {
Manish Singh's avatar
Manish Singh committed
223
      static const GTypeInfo gamma_curve_info =
Elliot Lee's avatar
Elliot Lee committed
224 225
      {
	sizeof (GtkGammaCurveClass),
Manish Singh's avatar
Manish Singh committed
226 227 228 229 230 231 232 233
	NULL,		/* base_init */
	NULL,		/* base_finalize */
	(GClassInitFunc) gtk_gamma_curve_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
	sizeof (GtkGammaCurve),
	0,		/* n_preallocs */
	(GInstanceInitFunc) gtk_gamma_curve_init,
Elliot Lee's avatar
Elliot Lee committed
234
      };
Tim Janik's avatar
Tim Janik committed
235
      
Manish Singh's avatar
Manish Singh committed
236 237
      gamma_curve_type = g_type_register_static (GTK_TYPE_VBOX, "GtkGammaCurve",
						 &gamma_curve_info, 0);
Elliot Lee's avatar
Elliot Lee committed
238 239 240 241 242 243 244 245 246
    }
  return gamma_curve_type;
}

static void
gtk_gamma_curve_class_init (GtkGammaCurveClass *class)
{
  GtkObjectClass *object_class;

Manish Singh's avatar
Manish Singh committed
247
  parent_class = g_type_class_peek_parent (class);
Elliot Lee's avatar
Elliot Lee committed
248 249 250 251 252 253 254 255

  object_class = (GtkObjectClass *) class;
  object_class->destroy = gtk_gamma_curve_destroy;
}

static void
gtk_gamma_curve_init (GtkGammaCurve *curve)
{
256 257 258
  GtkWidget *vbox;
  int i;

Elliot Lee's avatar
Elliot Lee committed
259
  curve->gamma = 1.0;
260 261 262 263 264 265

  curve->table = gtk_table_new (1, 2, FALSE);
  gtk_table_set_col_spacings (GTK_TABLE (curve->table), 3);
  gtk_container_add (GTK_CONTAINER (curve), curve->table);

  curve->curve = gtk_curve_new ();
Manish Singh's avatar
Manish Singh committed
266 267
  g_signal_connect (curve->curve, "curve_type_changed",
		    G_CALLBACK (curve_type_changed_callback), curve);
268 269 270 271 272 273 274 275 276
  gtk_table_attach_defaults (GTK_TABLE (curve->table), curve->curve, 0, 1, 0, 1);

  vbox = gtk_vbox_new (/* homogeneous */ FALSE, /* spacing */ 3);
  gtk_table_attach (GTK_TABLE (curve->table), vbox, 1, 2, 0, 1, 0, 0, 0, 0);

  /* toggle buttons: */
  for (i = 0; i < 3; ++i)
    {
      curve->button[i] = gtk_toggle_button_new ();
Manish Singh's avatar
Manish Singh committed
277 278
      g_object_set_data (G_OBJECT (curve->button[i]), "_GtkGammaCurveIndex",
			 GINT_TO_POINTER (i));
279
      gtk_container_add (GTK_CONTAINER (vbox), curve->button[i]);
Manish Singh's avatar
Manish Singh committed
280 281 282 283
      g_signal_connect (curve->button[i], "realize",
			G_CALLBACK (button_realize_callback), NULL);
      g_signal_connect (curve->button[i], "toggled",
			G_CALLBACK (button_toggled_callback), curve);
284 285 286 287 288 289 290
      gtk_widget_show (curve->button[i]);
    }

  /* push buttons: */
  for (i = 3; i < 5; ++i)
    {
      curve->button[i] = gtk_button_new ();
Manish Singh's avatar
Manish Singh committed
291 292
      g_object_set_data (G_OBJECT (curve->button[i]), "_GtkGammaCurveIndex",
			 GINT_TO_POINTER (i));
293
      gtk_container_add (GTK_CONTAINER (vbox), curve->button[i]);
Manish Singh's avatar
Manish Singh committed
294 295 296 297
      g_signal_connect (curve->button[i], "realize",
			G_CALLBACK (button_realize_callback), NULL);
      g_signal_connect (curve->button[i], "clicked",
			G_CALLBACK (button_clicked_callback), curve);
298 299 300 301 302 303
      gtk_widget_show (curve->button[i]);
    }

  gtk_widget_show (vbox);
  gtk_widget_show (curve->table);
  gtk_widget_show (curve->curve);
Elliot Lee's avatar
Elliot Lee committed
304 305 306 307 308 309 310 311 312 313
}

static void
button_realize_callback (GtkWidget *w)
{
  GtkWidget *pixmap;
  GdkBitmap *mask;
  GdkPixmap *pm;
  int i;

Manish Singh's avatar
Manish Singh committed
314
  i = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (w), "_GtkGammaCurveIndex"));
Elliot Lee's avatar
Elliot Lee committed
315
  pm = gdk_pixmap_create_from_xpm_d (w->window, &mask,
316
				     &w->style->bg[GTK_STATE_NORMAL], (gchar **)xpm[i]);
Elliot Lee's avatar
Elliot Lee committed
317

Manish Singh's avatar
Manish Singh committed
318
  pixmap = gtk_image_new_from_pixmap (pm, mask);
Elliot Lee's avatar
Elliot Lee committed
319 320 321
  gtk_container_add (GTK_CONTAINER (w), pixmap);
  gtk_widget_show (pixmap);

Manish Singh's avatar
Manish Singh committed
322 323
  g_object_unref (pm);
  g_object_unref (mask);
Elliot Lee's avatar
Elliot Lee committed
324 325 326 327 328 329 330 331 332 333 334 335
}

static void
button_toggled_callback (GtkWidget *w, gpointer data)
{
  GtkGammaCurve *c = data;
  GtkCurveType type;
  int active, i;

  if (!GTK_TOGGLE_BUTTON (w)->active)
    return;

Manish Singh's avatar
Manish Singh committed
336
  active = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (w), "_GtkGammaCurveIndex"));
Elliot Lee's avatar
Elliot Lee committed
337 338 339 340 341 342

  for (i = 0; i < 3; ++i)
    if ((i != active) && GTK_TOGGLE_BUTTON (c->button[i])->active)
      break;

  if (i < 3)
343
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (c->button[i]), FALSE);
Elliot Lee's avatar
Elliot Lee committed
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365

  switch (active)
    {
    case 0:  type = GTK_CURVE_TYPE_SPLINE; break;
    case 1:  type = GTK_CURVE_TYPE_LINEAR; break;
    default: type = GTK_CURVE_TYPE_FREE; break;
    }
  gtk_curve_set_curve_type (GTK_CURVE (c->curve), type);
}

static void
gamma_cancel_callback (GtkWidget *w, gpointer data)
{
  GtkGammaCurve *c = data;

  gtk_widget_destroy (c->gamma_dialog);
}

static void
gamma_ok_callback (GtkWidget *w, gpointer data)
{
  GtkGammaCurve *c = data;
366 367
  const gchar *start;
  gchar *end;
Elliot Lee's avatar
Elliot Lee committed
368 369 370 371 372
  gfloat v;

  start = gtk_entry_get_text (GTK_ENTRY (c->gamma_text));
  if (start)
    {
373
      v = g_strtod (start, &end);
Elliot Lee's avatar
Elliot Lee committed
374 375 376 377 378 379 380 381 382 383 384 385 386
      if (end > start && v > 0.0)
	c->gamma = v;
    }
  gtk_curve_set_gamma (GTK_CURVE (c->curve), c->gamma);
  gamma_cancel_callback (w, data);
}

static void
button_clicked_callback (GtkWidget *w, gpointer data)
{
  GtkGammaCurve *c = data;
  int active;

Manish Singh's avatar
Manish Singh committed
387
  active = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (w), "_GtkGammaCurveIndex"));
Elliot Lee's avatar
Elliot Lee committed
388
  if (active == 3)
389 390 391 392 393 394 395 396 397 398
    {
      /* set gamma */
      if (c->gamma_dialog)
	return;
      else
	{
	  GtkWidget *vbox, *hbox, *label, *button;
	  gchar buf[64];
	  
	  c->gamma_dialog = gtk_dialog_new ();
399 400
	  gtk_window_set_screen (GTK_WINDOW (c->gamma_dialog),
				 gtk_widget_get_screen (w));
401
	  gtk_window_set_title (GTK_WINDOW (c->gamma_dialog), _("Gamma"));
402 403 404
	  g_object_add_weak_pointer (G_OBJECT (c->gamma_dialog),
				     (gpointer *)&c->gamma_dialog);
	  
405 406 407 408 409 410
	  vbox = GTK_DIALOG (c->gamma_dialog)->vbox;
	  
	  hbox = gtk_hbox_new (/* homogeneous */ FALSE, 0);
	  gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 2);
	  gtk_widget_show (hbox);
	  
411
	  label = gtk_label_new_with_mnemonic (_("_Gamma value"));
412 413 414 415 416
	  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 2);
	  gtk_widget_show (label);
	  
	  sprintf (buf, "%g", c->gamma);
	  c->gamma_text = gtk_entry_new ();
417
          gtk_label_set_mnemonic_widget (GTK_LABEL (label), c->gamma_text);
418 419 420 421 422 423
	  gtk_entry_set_text (GTK_ENTRY (c->gamma_text), buf);
	  gtk_box_pack_start (GTK_BOX (hbox), c->gamma_text, TRUE, TRUE, 2);
	  gtk_widget_show (c->gamma_text);
	  
	  /* fill in action area: */
	  hbox = GTK_DIALOG (c->gamma_dialog)->action_area;
424

425
          button = gtk_button_new_from_stock (GTK_STOCK_CANCEL);
Manish Singh's avatar
Manish Singh committed
426 427
	  g_signal_connect (button, "clicked",
			    G_CALLBACK (gamma_cancel_callback), c);
428 429
	  gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
	  gtk_widget_show (button);
430
	  
431
          button = gtk_button_new_from_stock (GTK_STOCK_OK);
432
	  GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
Manish Singh's avatar
Manish Singh committed
433 434
	  g_signal_connect (button, "clicked",
			    G_CALLBACK (gamma_ok_callback), c);
435 436 437 438 439 440 441
	  gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
	  gtk_widget_grab_default (button);
	  gtk_widget_show (button);
	  
	  gtk_widget_show (c->gamma_dialog);
	}
    }
Elliot Lee's avatar
Elliot Lee committed
442
  else
443 444 445 446
    {
      /* reset */
      gtk_curve_reset (GTK_CURVE (c->curve));
    }
Elliot Lee's avatar
Elliot Lee committed
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
}

static void
curve_type_changed_callback (GtkWidget *w, gpointer data)
{
  GtkGammaCurve *c = data;
  GtkCurveType new_type;
  int active;

  new_type = GTK_CURVE (w)->curve_type;
  switch (new_type)
    {
    case GTK_CURVE_TYPE_SPLINE: active = 0; break;
    case GTK_CURVE_TYPE_LINEAR: active = 1; break;
    default:		        active = 2; break;
    }
  if (!GTK_TOGGLE_BUTTON (c->button[active])->active)
464
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (c->button[active]), TRUE);
Elliot Lee's avatar
Elliot Lee committed
465 466 467 468 469
}

GtkWidget*
gtk_gamma_curve_new (void)
{
Manish Singh's avatar
Manish Singh committed
470
  return g_object_new (GTK_TYPE_GAMMA_CURVE, NULL);
Elliot Lee's avatar
Elliot Lee committed
471 472 473 474 475 476 477 478 479 480 481 482
}

static void
gtk_gamma_curve_destroy (GtkObject *object)
{
  GtkGammaCurve *c;

  g_return_if_fail (GTK_IS_GAMMA_CURVE (object));

  c = GTK_GAMMA_CURVE (object);

  if (c->gamma_dialog)
483
    gtk_widget_destroy (c->gamma_dialog);
Elliot Lee's avatar
Elliot Lee committed
484 485 486 487

  if (GTK_OBJECT_CLASS (parent_class)->destroy)
    (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
}
Elliot Lee's avatar
Elliot Lee committed
488