gimpcolormapeditor.c 25.5 KB
Newer Older
Sven Neumann's avatar
Sven Neumann committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

Michael Natterer's avatar
Michael Natterer committed
21 22 23 24
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

25 26 27 28 29
#ifdef __GNUC__
#warning GTK_DISABLE_DEPRECATED
#endif
#undef GTK_DISABLE_DEPRECATED

Sven Neumann's avatar
Sven Neumann committed
30
#include <gtk/gtk.h>
Michael Natterer's avatar
Michael Natterer committed
31
#include <gdk/gdkkeysyms.h>
Sven Neumann's avatar
Sven Neumann committed
32

33
#include "libgimpcolor/gimpcolor.h"
34
#include "libgimpmath/gimpmath.h"
35
#include "libgimpwidgets/gimpwidgets.h"
36

37
#include "widgets-types.h"
Sven Neumann's avatar
Sven Neumann committed
38

39
#include "core/gimp.h"
40
#include "core/gimpcontainer.h"
41
#include "core/gimpcontext.h"
42
#include "core/gimpimage.h"
43
#include "core/gimpimage-colormap.h"
44
#include "core/gimpmarshal.h"
45

46 47
#include "gimpcolormapeditor.h"
#include "gimpdnd.h"
48
#include "gimprender.h"
49
#include "gimpmenufactory.h"
50
#include "gimpuimanager.h"
Michael Natterer's avatar
Michael Natterer committed
51

52
#include "gimp-intl.h"
Michael Natterer's avatar
Michael Natterer committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70


/*  Add these features:
 *
 *  load/save colormaps
 *  requantize
 *  add color--by clicking in the checked region
 *  all changes need to flush colormap lookup cache
 */


enum
{
  SELECTED,
  LAST_SIGNAL
};


71 72 73 74 75 76
#define HAVE_COLORMAP(gimage) \
        (gimage != NULL && \
         gimp_image_base_type (gimage) == GIMP_INDEXED && \
         gimp_image_get_colormap (gimage) != NULL)


77 78
static void   gimp_colormap_editor_class_init (GimpColormapEditorClass *klass);
static void   gimp_colormap_editor_init       (GimpColormapEditor      *colormap_editor);
Michael Natterer's avatar
Michael Natterer committed
79

80 81 82 83
static GObject * gimp_colormap_editor_constructor  (GType               type,
                                                    guint               n_params,
                                                    GObjectConstructParam *params);

84 85 86
static void   gimp_colormap_editor_destroy         (GtkObject          *object);
static void   gimp_colormap_editor_unmap           (GtkWidget          *widget);

87 88 89
static void   gimp_colormap_editor_set_image       (GimpImageEditor    *editor,
                                                    GimpImage          *gimage);

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
static void   gimp_colormap_editor_draw            (GimpColormapEditor *editor);
static void   gimp_colormap_editor_draw_cell       (GimpColormapEditor *editor,
                                                    gint                col);
static void   gimp_colormap_editor_clear           (GimpColormapEditor *editor,
                                                    gint                start_row);
static void   gimp_colormap_editor_update_entries  (GimpColormapEditor *editor);
static void   gimp_colormap_editor_set_index       (GimpColormapEditor *editor,
                                                    gint                i);

static void   gimp_colormap_preview_size_allocate  (GtkWidget          *widget,
                                                    GtkAllocation      *allocation,
                                                    GimpColormapEditor *editor);
static gboolean
              gimp_colormap_preview_button_press   (GtkWidget          *widget,
                                                    GdkEventButton     *bevent,
                                                    GimpColormapEditor *editor);
static void   gimp_colormap_preview_drag_color     (GtkWidget          *widget,
                                                    GimpRGB            *color,
                                                    gpointer            data);
static void   gimp_colormap_preview_drop_color     (GtkWidget          *widget,
110 111
                                                    gint                x,
                                                    gint                y,
112 113 114 115 116
                                                    const GimpRGB      *color,
                                                    gpointer            data);

static void   gimp_colormap_adjustment_changed     (GtkAdjustment      *adjustment,
                                                    GimpColormapEditor *editor);
Sven Neumann's avatar
Sven Neumann committed
117
static void   gimp_colormap_hex_entry_changed      (GimpColorHexEntry  *entry,
118 119 120 121 122 123 124 125 126 127
                                                    GimpColormapEditor *editor);

static void   gimp_colormap_image_mode_changed     (GimpImage          *gimage,
                                                    GimpColormapEditor *editor);
static void   gimp_colormap_image_colormap_changed (GimpImage          *gimage,
                                                    gint                ncol,
                                                    GimpColormapEditor *editor);


static guint editor_signals[LAST_SIGNAL] = { 0 };
Michael Natterer's avatar
Michael Natterer committed
128

129
static GimpImageEditorClass *parent_class = NULL;
Michael Natterer's avatar
Michael Natterer committed
130 131


132
GType
133
gimp_colormap_editor_get_type (void)
Michael Natterer's avatar
Michael Natterer committed
134
{
135
  static GType type = 0;
Michael Natterer's avatar
Michael Natterer committed
136

137
  if (! type)
Michael Natterer's avatar
Michael Natterer committed
138
    {
139
      static const GTypeInfo info =
Michael Natterer's avatar
Michael Natterer committed
140
      {
141
        sizeof (GimpColormapEditorClass),
142 143 144 145 146 147 148 149
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) gimp_colormap_editor_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data     */
        sizeof (GimpColormapEditor),
        0,              /* n_preallocs    */
        (GInstanceInitFunc) gimp_colormap_editor_init,
Michael Natterer's avatar
Michael Natterer committed
150 151
      };

152 153 154
      type = g_type_register_static (GIMP_TYPE_IMAGE_EDITOR,
                                     "GimpColormapEditor",
                                     &info, 0);
Michael Natterer's avatar
Michael Natterer committed
155 156
    }

157
  return type;
Michael Natterer's avatar
Michael Natterer committed
158 159 160
}

static void
161
gimp_colormap_editor_class_init (GimpColormapEditorClass* klass)
Michael Natterer's avatar
Michael Natterer committed
162
{
163 164 165 166
  GObjectClass         *object_class       = G_OBJECT_CLASS (klass);
  GtkObjectClass       *gtk_object_class   = GTK_OBJECT_CLASS (klass);
  GtkWidgetClass       *widget_class       = GTK_WIDGET_CLASS (klass);
  GimpImageEditorClass *image_editor_class = GIMP_IMAGE_EDITOR_CLASS (klass);
167

168
  parent_class = g_type_class_peek_parent (klass);
Michael Natterer's avatar
Michael Natterer committed
169

170
  editor_signals[SELECTED] =
171
    g_signal_new ("selected",
172 173 174 175 176 177
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpColormapEditorClass, selected),
                  NULL, NULL,
                  gimp_marshal_VOID__FLAGS,
                  G_TYPE_NONE, 1,
178
                  GDK_TYPE_MODIFIER_TYPE);
Michael Natterer's avatar
Michael Natterer committed
179

180 181 182
  object_class->constructor     = gimp_colormap_editor_constructor;

  gtk_object_class->destroy     = gimp_colormap_editor_destroy;
183 184

  widget_class->unmap           = gimp_colormap_editor_unmap;
185

186
  image_editor_class->set_image = gimp_colormap_editor_set_image;
187

188
  klass->selected               = NULL;
Michael Natterer's avatar
Michael Natterer committed
189 190 191
}

static void
Michael Natterer's avatar
Michael Natterer committed
192
gimp_colormap_editor_init (GimpColormapEditor *editor)
Michael Natterer's avatar
Michael Natterer committed
193
{
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
  GtkWidget *frame;
  GtkWidget *table;

  editor->col_index     = 0;
  editor->dnd_col_index = 0;
  editor->xn            = 0;
  editor->yn            = 0;
  editor->cellsize      = 0;

  /*  The palette frame  */
  frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
  gtk_box_pack_start (GTK_BOX (editor), frame, TRUE, TRUE, 0);
  gtk_widget_show (frame);

209 210 211 212 213 214 215 216
  editor->preview = gtk_preview_new (GTK_PREVIEW_COLOR);
  gtk_widget_set_size_request (editor->preview, -1, 60);
  gtk_preview_set_expand (GTK_PREVIEW (editor->preview), TRUE);
  gtk_widget_add_events (editor->preview, GDK_BUTTON_PRESS_MASK);
  gtk_container_add (GTK_CONTAINER (frame), editor->preview);
  gtk_widget_show (editor->preview);

  g_signal_connect_after (editor->preview, "size-allocate",
217 218 219
                          G_CALLBACK (gimp_colormap_preview_size_allocate),
                          editor);

220
  g_signal_connect (editor->preview, "button-press-event",
221 222 223
                    G_CALLBACK (gimp_colormap_preview_button_press),
                    editor);

224
  gimp_dnd_color_source_add (editor->preview, gimp_colormap_preview_drag_color,
225
                             editor);
226
  gimp_dnd_color_dest_add (editor->preview, gimp_colormap_preview_drop_color,
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
                           editor);

  /*  Some helpful hints  */
  table = gtk_table_new (2, 2, FALSE);
  gtk_table_set_col_spacing (GTK_TABLE (table), 0, 4);
  gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
  gtk_box_pack_end (GTK_BOX (editor), table, FALSE, FALSE, 0);
  gtk_widget_show (table);

  editor->index_spinbutton =
    gimp_spin_button_new ((GtkObject **) &editor->index_adjustment,
                          0, 0, 0, 1, 10, 10, 1.0, 0);
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 0,
                             _("Color index:"), 0.0, 0.5,
                             editor->index_spinbutton, 1, TRUE);

243
  g_signal_connect (editor->index_adjustment, "value-changed",
244 245 246
                    G_CALLBACK (gimp_colormap_adjustment_changed),
                    editor);

Sven Neumann's avatar
Sven Neumann committed
247 248
  editor->color_entry = gimp_color_hex_entry_new ();
  gtk_entry_set_width_chars (GTK_ENTRY (editor->color_entry), 12);
249
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 1,
250
                             _("HTML notation:"), 0.0, 0.5,
251 252
                             editor->color_entry, 1, TRUE);

253
  g_signal_connect (editor->color_entry, "color-changed",
Sven Neumann's avatar
Sven Neumann committed
254
                    G_CALLBACK (gimp_colormap_hex_entry_changed),
255
                    editor);
256 257 258 259 260 261 262 263 264 265 266 267 268
}

static GObject *
gimp_colormap_editor_constructor (GType                  type,
                                  guint                  n_params,
                                  GObjectConstructParam *params)
{
  GObject            *object;
  GimpColormapEditor *editor;

  object = G_OBJECT_CLASS (parent_class)->constructor (type, n_params, params);

  editor = GIMP_COLORMAP_EDITOR (object);
Michael Natterer's avatar
Michael Natterer committed
269 270

  editor->edit_button =
271 272 273
    gimp_editor_add_action_button (GIMP_EDITOR (editor), "colormap-editor",
                                   "colormap-editor-edit-color",
                                   NULL);
Michael Natterer's avatar
Michael Natterer committed
274 275

  editor->add_button =
276 277 278 279 280 281 282
    gimp_editor_add_action_button (GIMP_EDITOR (editor), "colormap-editor",
                                   "colormap-editor-add-color-from-fg",
                                   "colormap-editor-add-color-from-bg",
                                   GDK_CONTROL_MASK,
                                   NULL);

  return object;
Michael Natterer's avatar
Michael Natterer committed
283 284
}

285 286 287
static void
gimp_colormap_editor_destroy (GtkObject *object)
{
288
  GimpColormapEditor *editor = GIMP_COLORMAP_EDITOR (object);
289

290
  if (editor->color_dialog)
291
    {
292
      gtk_widget_destroy (editor->color_dialog);
293
      editor->color_dialog = NULL;
294 295 296 297 298 299 300 301
    }

  GTK_OBJECT_CLASS (parent_class)->destroy (object);
}

static void
gimp_colormap_editor_unmap (GtkWidget *widget)
{
302
  GimpColormapEditor *editor = GIMP_COLORMAP_EDITOR (widget);
303

304
  if (editor->color_dialog)
305
    gtk_widget_hide (editor->color_dialog);
306 307 308 309

  GTK_WIDGET_CLASS (parent_class)->unmap (widget);
}

310 311
static void
gimp_colormap_editor_set_image (GimpImageEditor *image_editor,
312
                                GimpImage       *gimage)
313
{
314
  GimpColormapEditor *editor = GIMP_COLORMAP_EDITOR (image_editor);
315 316 317 318

  if (image_editor->gimage)
    {
      g_signal_handlers_disconnect_by_func (image_editor->gimage,
319 320
                                            gimp_colormap_image_mode_changed,
                                            editor);
321
      g_signal_handlers_disconnect_by_func (image_editor->gimage,
322 323
                                            gimp_colormap_image_colormap_changed,
                                            editor);
324

325
      if (editor->color_dialog)
326
        gtk_widget_hide (editor->color_dialog);
327

328
      if (! HAVE_COLORMAP (gimage))
329 330 331
        {
          editor->index_adjustment->upper = 0;
          gtk_adjustment_changed (editor->index_adjustment);
332

333 334 335
          if (GTK_WIDGET_MAPPED (GTK_WIDGET (editor)))
            gimp_colormap_editor_clear (editor, -1);
        }
336 337 338 339 340 341 342 343 344
    }

  GIMP_IMAGE_EDITOR_CLASS (parent_class)->set_image (image_editor, gimage);

  editor->col_index     = 0;
  editor->dnd_col_index = 0;

  if (gimage)
    {
345
      g_signal_connect (gimage, "mode-changed",
346 347
                        G_CALLBACK (gimp_colormap_image_mode_changed),
                        editor);
348
      g_signal_connect (gimage, "colormap-changed",
349 350
                        G_CALLBACK (gimp_colormap_image_colormap_changed),
                        editor);
351

352
      if (HAVE_COLORMAP (gimage))
353 354
        {
          gimp_colormap_editor_draw (editor);
355

356
          editor->index_adjustment->upper =
357 358
            gimp_image_get_colormap_size (gimage) - 1;

359 360
          gtk_adjustment_changed (editor->index_adjustment);
        }
361 362 363 364 365
    }

  gimp_colormap_editor_update_entries (editor);
}

366 367 368

/*  public functions  */

369
GtkWidget *
370
gimp_colormap_editor_new (GimpMenuFactory *menu_factory)
Michael Natterer's avatar
Michael Natterer committed
371
{
372
  g_return_val_if_fail (GIMP_IS_MENU_FACTORY (menu_factory), NULL);
Michael Natterer's avatar
Michael Natterer committed
373

374 375 376 377 378
  return g_object_new (GIMP_TYPE_COLORMAP_EDITOR,
                       "menu-factory",    menu_factory,
                       "menu-identifier", "<ColormapEditor>",
                       "ui-path",         "/colormap-editor-popup",
                       NULL);
Michael Natterer's avatar
Michael Natterer committed
379 380 381
}

void
382 383
gimp_colormap_editor_selected (GimpColormapEditor *editor,
                               GdkModifierType     state)
Michael Natterer's avatar
Michael Natterer committed
384
{
385
  g_return_if_fail (GIMP_IS_COLORMAP_EDITOR (editor));
Michael Natterer's avatar
Michael Natterer committed
386

387
  g_signal_emit (editor, editor_signals[SELECTED], 0, state);
Michael Natterer's avatar
Michael Natterer committed
388 389
}

390
gint
391
gimp_colormap_editor_col_index (GimpColormapEditor *editor)
392
{
393
  g_return_val_if_fail (GIMP_IS_COLORMAP_EDITOR (editor), 0);
394

395
  return editor->col_index;
Michael Natterer's avatar
Michael Natterer committed
396 397
}

398 399 400

/*  private functions  */

Michael Natterer's avatar
Michael Natterer committed
401 402 403
#define MIN_CELL_SIZE 4

static void
404
gimp_colormap_editor_draw (GimpColormapEditor *editor)
Michael Natterer's avatar
Michael Natterer committed
405 406
{
  GimpImage *gimage;
407 408 409 410
  gint       i, j, k, l, b;
  gint       col;
  guchar    *row;
  gint       cellsize, ncol, xn, yn, width, height;
Michael Natterer's avatar
Michael Natterer committed
411

412
  gimage = GIMP_IMAGE_EDITOR (editor)->gimage;
Michael Natterer's avatar
Michael Natterer committed
413

414 415 416
  width  = editor->preview->allocation.width;
  height = editor->preview->allocation.height;
  ncol   = gimp_image_get_colormap_size (gimage);
Michael Natterer's avatar
Michael Natterer committed
417

418
  if (! ncol)
Michael Natterer's avatar
Michael Natterer committed
419
    {
420
      gimp_colormap_editor_clear (editor, -1);
Michael Natterer's avatar
Michael Natterer committed
421 422 423
      return;
    }

424
  cellsize = sqrt (width * height / ncol);
Michael Natterer's avatar
Michael Natterer committed
425
  while (cellsize >= MIN_CELL_SIZE
426
         && (xn = width / cellsize) * (yn = height / cellsize) < ncol)
Michael Natterer's avatar
Michael Natterer committed
427 428 429 430 431 432 433 434
    cellsize--;

  if (cellsize < MIN_CELL_SIZE)
    {
      cellsize = MIN_CELL_SIZE;
      xn = yn = ceil (sqrt (ncol));
    }

435 436
  yn     = ((ncol + xn - 1) / xn);

437 438 439 440 441 442 443 444 445
  /* We used to render just multiples of "cellsize" here, but the
   *  colormap as dockable looks better if it always fills the
   *  available allocation->width (which should always be larger than
   *  "xn * cellsize"). Defensively, we use MAX(width,xn*cellsize)
   *  below   --Mitch
   *

   width  = xn * cellsize;
   height = yn * cellsize; */
446

447 448 449
  editor->xn       = xn;
  editor->yn       = yn;
  editor->cellsize = cellsize;
Michael Natterer's avatar
Michael Natterer committed
450

451
  row = g_new (guchar, MAX (width, xn * cellsize) * 3);
Michael Natterer's avatar
Michael Natterer committed
452 453 454 455
  col = 0;
  for (i = 0; i < yn; i++)
    {
      for (j = 0; j < xn && col < ncol; j++, col++)
456 457 458 459 460
        {
          for (k = 0; k < cellsize; k++)
            for (b = 0; b < 3; b++)
              row[(j * cellsize + k) * 3 + b] = gimage->cmap[col * 3 + b];
        }
Michael Natterer's avatar
Michael Natterer committed
461 462

      for (k = 0; k < cellsize; k++)
463 464 465 466 467 468
        {
          for (l = j * cellsize; l < width; l++)
            for (b = 0; b < 3; b++)
              row[l * 3 + b] = (((((i * cellsize + k) & 0x4) ?
                                  (l) :
                                  (l + 0x4)) & 0x4) ?
469 470
                                gimp_render_blend_light_check[0] :
                                gimp_render_blend_dark_check[0]);
471

472
          gtk_preview_draw_row (GTK_PREVIEW (editor->preview), row, 0,
473 474 475
                                i * cellsize + k,
                                width);
        }
Michael Natterer's avatar
Michael Natterer committed
476
    }
477

478
  gimp_colormap_editor_clear (editor, yn * cellsize);
479

480
  gimp_colormap_editor_draw_cell (editor, editor->col_index);
Michael Natterer's avatar
Michael Natterer committed
481 482

  g_free (row);
483
  gtk_widget_queue_draw (editor->preview);
Michael Natterer's avatar
Michael Natterer committed
484 485 486
}

static void
487 488
gimp_colormap_editor_draw_cell (GimpColormapEditor *editor,
                                gint                col)
Michael Natterer's avatar
Michael Natterer committed
489
{
490 491 492 493 494
  GimpImage *gimage;
  guchar    *row;
  gint       cellsize, x, y, k;

  gimage = GIMP_IMAGE_EDITOR (editor)->gimage;
495

496
  cellsize = editor->cellsize;
497
  row = g_new (guchar, cellsize * 3);
498 499
  x = (col % editor->xn) * cellsize;
  y = (col / editor->xn) * cellsize;
Michael Natterer's avatar
Michael Natterer committed
500

501
  if (col == editor->col_index)
Michael Natterer's avatar
Michael Natterer committed
502 503
    {
      for(k = 0; k < cellsize; k++)
504
        row[k*3] = row[k*3+1] = row[k*3+2] = (k & 1) * 255;
505
      gtk_preview_draw_row (GTK_PREVIEW (editor->preview), row, x, y, cellsize);
Michael Natterer's avatar
Michael Natterer committed
506 507

      if (!(cellsize & 1))
508 509
        for (k = 0; k < cellsize; k++)
          row[k*3] = row[k*3+1] = row[k*3+2] = ((x+y+1) & 1) * 255;
510
      gtk_preview_draw_row (GTK_PREVIEW (editor->preview), row,
511
                            x, y+cellsize-1, cellsize);
Michael Natterer's avatar
Michael Natterer committed
512 513 514

      row[0]=row[1]=row[2]=255;
      row[cellsize*3-3] = row[cellsize*3-2] = row[cellsize*3-1]
515
        = 255 * (cellsize & 1);
Michael Natterer's avatar
Michael Natterer committed
516
      for (k = 1; k < cellsize - 1; k++)
517 518 519 520 521
        {
          row[k*3]   = gimage->cmap[col * 3];
          row[k*3+1] = gimage->cmap[col * 3 + 1];
          row[k*3+2] = gimage->cmap[col * 3 + 2];
        }
Michael Natterer's avatar
Michael Natterer committed
522
      for (k = 1; k < cellsize - 1; k+=2)
523
        gtk_preview_draw_row (GTK_PREVIEW (editor->preview), row,
524
                              x, y+k, cellsize);
Michael Natterer's avatar
Michael Natterer committed
525 526 527

      row[0] = row[1] = row[2] = 0;
      row[cellsize*3-3] = row[cellsize*3-2] = row[cellsize*3-1]
528
        = 255 * ((cellsize+1) & 1);
Michael Natterer's avatar
Michael Natterer committed
529
      for (k = 2; k < cellsize - 1; k += 2)
530
        gtk_preview_draw_row (GTK_PREVIEW (editor->preview), row,
531
                              x, y+k, cellsize);
Michael Natterer's avatar
Michael Natterer committed
532 533 534 535
    }
  else
    {
      for (k = 0; k < cellsize; k++)
536 537 538 539 540
        {
          row[k*3]   = gimage->cmap[col * 3];
          row[k*3+1] = gimage->cmap[col * 3 + 1];
          row[k*3+2] = gimage->cmap[col * 3 + 2];
        }
Michael Natterer's avatar
Michael Natterer committed
541
      for (k = 0; k < cellsize; k++)
542
        gtk_preview_draw_row (GTK_PREVIEW (editor->preview), row,
543
                              x, y+k, cellsize);
Michael Natterer's avatar
Michael Natterer committed
544
    }
545

546
  gtk_widget_queue_draw_area (editor->preview,
547 548
                              x, y,
                              cellsize, cellsize);
Michael Natterer's avatar
Michael Natterer committed
549
}
550

Michael Natterer's avatar
Michael Natterer committed
551
static void
552 553
gimp_colormap_editor_clear (GimpColormapEditor *editor,
                            gint                start_row)
Michael Natterer's avatar
Michael Natterer committed
554
{
555 556 557 558
  gint    i, j;
  gint    offset;
  gint    width, height;
  guchar *row = NULL;
Michael Natterer's avatar
Michael Natterer committed
559

560 561
  width  = editor->preview->allocation.width;
  height = editor->preview->allocation.height;
562 563 564 565 566 567 568

  if (start_row < 0)
    start_row = 0;

  if (start_row >= height)
    return;

Michael Natterer's avatar
Michael Natterer committed
569 570 571
  if (width > 0)
    row = g_new (guchar, width * 3);

572 573 574
  if (start_row & 0x3)
    {
      offset = (start_row & 0x4) ? 0x4 : 0x0;
Michael Natterer's avatar
Michael Natterer committed
575

576
      for (j = 0; j < width; j++)
577 578 579
        {
          row[j * 3 + 0] = row[j * 3 + 1] = row[j * 3 + 2] =
            ((j + offset) & 0x4) ?
580
            gimp_render_blend_dark_check[0] : gimp_render_blend_light_check[0];
581
        }
582 583

      for (j = 0; j < (4 - (start_row & 0x3)) && start_row + j < height; j++)
584
        gtk_preview_draw_row (GTK_PREVIEW (editor->preview), row,
585
                              0, start_row + j, width);
586 587 588 589 590

      start_row += (4 - (start_row & 0x3));
    }

  for (i = start_row; i < height; i += 4)
Michael Natterer's avatar
Michael Natterer committed
591 592 593 594
    {
      offset = (i & 0x4) ? 0x4 : 0x0;

      for (j = 0; j < width; j++)
595 596 597
        {
          row[j * 3 + 0] = row[j * 3 + 1] = row[j * 3 + 2] =
            ((j + offset) & 0x4) ?
598
            gimp_render_blend_dark_check[0] : gimp_render_blend_light_check[0];
599
        }
Michael Natterer's avatar
Michael Natterer committed
600

601
      for (j = 0; j < 4 && i + j < height; j++)
602
        gtk_preview_draw_row (GTK_PREVIEW (editor->preview), row,
603
                              0, i + j, width);
Michael Natterer's avatar
Michael Natterer committed
604
    }
605

Michael Natterer's avatar
Michael Natterer committed
606 607
  if (width > 0)
    g_free (row);
608

609
  gtk_widget_queue_draw (editor->preview);
Michael Natterer's avatar
Michael Natterer committed
610 611 612
}

static void
613
gimp_colormap_editor_update_entries (GimpColormapEditor *editor)
Michael Natterer's avatar
Michael Natterer committed
614
{
615
  GimpImage *gimage = GIMP_IMAGE_EDITOR (editor)->gimage;
616

617
  if (! HAVE_COLORMAP (gimage))
Michael Natterer's avatar
Michael Natterer committed
618
    {
619 620
      gtk_widget_set_sensitive (editor->index_spinbutton, FALSE);
      gtk_widget_set_sensitive (editor->color_entry, FALSE);
621

622 623
      gtk_adjustment_set_value (editor->index_adjustment, 0);
      gtk_entry_set_text (GTK_ENTRY (editor->color_entry), "");
Michael Natterer's avatar
Michael Natterer committed
624 625 626
    }
  else
    {
627
      gchar  *string;
Michael Natterer's avatar
Michael Natterer committed
628 629
      guchar *col;

630
      gtk_adjustment_set_value (editor->index_adjustment, editor->col_index);
Michael Natterer's avatar
Michael Natterer committed
631

632
      col = &gimage->cmap[editor->col_index * 3];
633

634
      string = g_strdup_printf ("%02x%02x%02x", col[0], col[1], col[2]);
635
      gtk_entry_set_text (GTK_ENTRY (editor->color_entry), string);
Michael Natterer's avatar
Michael Natterer committed
636
      g_free (string);
637

638 639
      gtk_widget_set_sensitive (editor->index_spinbutton, TRUE);
      gtk_widget_set_sensitive (editor->color_entry, TRUE);
Michael Natterer's avatar
Michael Natterer committed
640 641 642 643
    }
}

static void
644 645
gimp_colormap_editor_set_index (GimpColormapEditor *editor,
                                gint                i)
Michael Natterer's avatar
Michael Natterer committed
646
{
647
  if (i != editor->col_index)
Michael Natterer's avatar
Michael Natterer committed
648
    {
649
      gint old = editor->col_index;
650

651 652
      editor->col_index     = i;
      editor->dnd_col_index = i;
653

654 655
      gimp_colormap_editor_draw_cell (editor, old);
      gimp_colormap_editor_draw_cell (editor, i);
656

657
      gimp_colormap_editor_update_entries (editor);
Michael Natterer's avatar
Michael Natterer committed
658 659 660 661
    }
}

static void
662 663 664
gimp_colormap_preview_size_allocate (GtkWidget          *widget,
                                     GtkAllocation      *alloc,
                                     GimpColormapEditor *editor)
Michael Natterer's avatar
Michael Natterer committed
665
{
666
  GimpImage *gimage = GIMP_IMAGE_EDITOR (editor)->gimage;
667

668
  if (HAVE_COLORMAP (gimage))
669 670 671
    gimp_colormap_editor_draw (editor);
  else
    gimp_colormap_editor_clear (editor, -1);
Michael Natterer's avatar
Michael Natterer committed
672 673
}

674 675 676 677 678

static gboolean
gimp_colormap_preview_button_press (GtkWidget          *widget,
                                    GdkEventButton     *bevent,
                                    GimpColormapEditor *editor)
Michael Natterer's avatar
Michael Natterer committed
679
{
680 681
  GimpImage *gimage;
  guint      col;
Michael Natterer's avatar
Michael Natterer committed
682

683
  gimage = GIMP_IMAGE_EDITOR (editor)->gimage;
Michael Natterer's avatar
Michael Natterer committed
684

685
  if (! HAVE_COLORMAP (gimage))
686
    return TRUE;
Michael Natterer's avatar
Michael Natterer committed
687

688
  if (!(bevent->y < editor->cellsize * editor->yn
689
        && bevent->x < editor->cellsize * editor->xn))
690
    return TRUE;
Michael Natterer's avatar
Michael Natterer committed
691

692 693
  col = (editor->xn * ((gint) bevent->y / editor->cellsize) +
         ((gint) bevent->x / editor->cellsize));
Michael Natterer's avatar
Michael Natterer committed
694

695
  if (col >= gimp_image_get_colormap_size (gimage))
696
    return TRUE;
Michael Natterer's avatar
Michael Natterer committed
697

698
  switch (bevent->button)
Michael Natterer's avatar
Michael Natterer committed
699
    {
700 701
    case 1:
      gimp_colormap_editor_set_index (editor, col);
702
      gimp_colormap_editor_selected (editor, bevent->state);
Michael Natterer's avatar
Michael Natterer committed
703 704

      if (bevent->type == GDK_2BUTTON_PRESS)
705 706 707
        {
          GtkAction *action;

708 709 710
          action = gimp_ui_manager_find_action (GIMP_EDITOR (editor)->ui_manager,
                                                "colormap-editor",
                                                "colormap-editor-edit-color");
Michael Natterer's avatar
Michael Natterer committed
711

712 713 714
          if (action)
            gtk_action_activate (action);
        }
Michael Natterer's avatar
Michael Natterer committed
715
      return TRUE;
Michael Natterer's avatar
Michael Natterer committed
716

717 718 719
    case 2:
      editor->dnd_col_index = col;
      break;
Michael Natterer's avatar
Michael Natterer committed
720

721
    case 3:
722
      gimp_colormap_editor_set_index (editor, col);
723
      gimp_editor_popup_menu (GIMP_EDITOR (editor), NULL, NULL);
724
      return TRUE;
Michael Natterer's avatar
Michael Natterer committed
725

726 727 728
    default:
      break;
    }
Michael Natterer's avatar
Michael Natterer committed
729

730
  return FALSE;
731
}
732

Michael Natterer's avatar
Michael Natterer committed
733
static void
734 735 736
gimp_colormap_preview_drag_color (GtkWidget *widget,
                                  GimpRGB   *color,
                                  gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
737
{
738 739
  GimpColormapEditor *editor;
  GimpImage          *gimage;
Michael Natterer's avatar
Michael Natterer committed
740

741
  editor = GIMP_COLORMAP_EDITOR (data);
742
  gimage = GIMP_IMAGE_EDITOR (editor)->gimage;
Michael Natterer's avatar
Michael Natterer committed
743

744 745
  if (HAVE_COLORMAP (gimage))
    gimp_image_get_colormap_entry (gimage, editor->dnd_col_index, color);
Michael Natterer's avatar
Michael Natterer committed
746 747 748
}

static void
749
gimp_colormap_preview_drop_color (GtkWidget     *widget,
750 751
                                  gint           x,
                                  gint           y,
752 753
                                  const GimpRGB *color,
                                  gpointer       data)
Michael Natterer's avatar
Michael Natterer committed
754
{
755
  GimpColormapEditor *editor;
Michael Natterer's avatar
Michael Natterer committed
756 757
  GimpImage          *gimage;

758
  editor = GIMP_COLORMAP_EDITOR (data);
759
  gimage = GIMP_IMAGE_EDITOR (editor)->gimage;
Michael Natterer's avatar
Michael Natterer committed
760

761
  if (HAVE_COLORMAP (gimage) && gimp_image_get_colormap_size (gimage) < 256)
Michael Natterer's avatar
Michael Natterer committed
762
    {
763
      gimp_image_add_colormap_entry (gimage, color);
Michael Natterer's avatar
Michael Natterer committed
764 765 766
    }
}

767 768 769
static void
gimp_colormap_adjustment_changed (GtkAdjustment      *adjustment,
                                  GimpColormapEditor *editor)
Michael Natterer's avatar
Michael Natterer committed
770
{
771 772
  GimpImage *gimage = GIMP_IMAGE_EDITOR (editor)->gimage;

773
  if (HAVE_COLORMAP (gimage))
774
    {
775
      gimp_colormap_editor_set_index (editor, adjustment->value + 0.5);
Michael Natterer's avatar
Michael Natterer committed
776

777 778
      gimp_colormap_editor_update_entries (editor);
    }
779
}
Michael Natterer's avatar
Michael Natterer committed
780

781
static void
Sven Neumann's avatar
Sven Neumann committed
782 783
gimp_colormap_hex_entry_changed (GimpColorHexEntry  *entry,
                                 GimpColormapEditor *editor)
784
{
785
  GimpImage *gimage = GIMP_IMAGE_EDITOR (editor)->gimage;
786 787

  if (gimage)
788
    {
Sven Neumann's avatar
Sven Neumann committed
789
      GimpRGB color;
790

Sven Neumann's avatar
Sven Neumann committed
791
      gimp_color_hex_entry_get_color (entry, &color);
Michael Natterer's avatar
Michael Natterer committed
792

Sven Neumann's avatar
Sven Neumann committed
793 794
      gimp_image_set_colormap_entry (gimage, editor->col_index, &color, TRUE);
      gimp_image_flush (gimage);
795
    }
796
}
Michael Natterer's avatar
Michael Natterer committed
797

798 799 800 801
static void
gimp_colormap_image_mode_changed (GimpImage          *gimage,
                                  GimpColormapEditor *editor)
{
802 803 804
  if (editor->color_dialog)
    gtk_widget_hide (editor->color_dialog);

805 806
  gimp_colormap_image_colormap_changed (gimage, -1, editor);
}