gimppaletteeditor.c 28.8 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
17
 */
Sven Neumann's avatar
Sven Neumann committed
18

19 20
#include "config.h"

21 22
#include <string.h>

Sven Neumann's avatar
Sven Neumann committed
23 24
#include <gtk/gtk.h>

25
#include "libgimpcolor/gimpcolor.h"
26
#include "libgimpbase/gimpbase.h"
27 28
#include "libgimpwidgets/gimpwidgets.h"

29
#include "widgets-types.h"
Sven Neumann's avatar
Sven Neumann committed
30

31
#include "core/gimp.h"
32 33 34 35 36
#include "core/gimpcontainer.h"
#include "core/gimpcontext.h"
#include "core/gimpdatafactory.h"
#include "core/gimppalette.h"

37
#include "gimpdnd.h"
38
#include "gimpdocked.h"
39
#include "gimphelp-ids.h"
40
#include "gimppaletteeditor.h"
41
#include "gimppaletteview.h"
42
#include "gimpsessioninfo.h"
43
#include "gimpuimanager.h"
44
#include "gimpviewrendererpalette.h"
45
#include "gimpwidgets-utils.h"
46

47
#include "gimp-intl.h"
48

49

50
#define ENTRY_WIDTH  12
51
#define ENTRY_HEIGHT 10
52 53 54
#define SPACING       1
#define COLUMNS      16
#define ROWS         11
Elliot Lee's avatar
Elliot Lee committed
55

56 57
#define PREVIEW_WIDTH  ((ENTRY_WIDTH  + SPACING) * COLUMNS + 1)
#define PREVIEW_HEIGHT ((ENTRY_HEIGHT + SPACING) * ROWS    + 1)
Elliot Lee's avatar
Elliot Lee committed
58

59
#define EPSILON 1e-10
60

61
/*  local function prototypes  */
62

63
static void   gimp_palette_editor_docked_iface_init (GimpDockedInterface *face);
64 65 66 67 68

static GObject * gimp_palette_editor_constructor   (GType              type,
                                                    guint              n_params,
                                                    GObjectConstructParam *params);

69 70 71 72
static void   gimp_palette_editor_set_aux_info     (GimpDocked        *docked,
                                                    GList             *aux_info);
static GList *gimp_palette_editor_get_aux_info     (GimpDocked        *docked);

73 74
static void   gimp_palette_editor_destroy          (GtkObject         *object);
static void   gimp_palette_editor_unmap            (GtkWidget         *widget);
Elliot Lee's avatar
Elliot Lee committed
75

76 77
static void   gimp_palette_editor_set_data         (GimpDataEditor    *editor,
                                                    GimpData          *data);
78

79
static void   palette_editor_invalidate_preview    (GimpPalette       *palette,
80
                                                    GimpPaletteEditor *editor);
81 82 83

static void   palette_editor_viewport_size_allocate(GtkWidget         *widget,
                                                    GtkAllocation     *allocation,
84
                                                    GimpPaletteEditor *editor);
85

86
static gint   palette_editor_eventbox_button_press (GtkWidget         *widget,
87 88
                                                    GdkEventButton    *bevent,
                                                    GimpPaletteEditor *editor);
89
static void   palette_editor_drop_color            (GtkWidget         *widget,
90 91
                                                    gint               x,
                                                    gint               y,
92 93 94
                                                    const GimpRGB     *color,
                                                    gpointer           data);
static void   palette_editor_drop_palette          (GtkWidget         *widget,
95 96
                                                    gint               x,
                                                    gint               y,
97 98
                                                    GimpViewable      *viewable,
                                                    gpointer           data);
99 100 101 102

static void   palette_editor_entry_clicked         (GimpPaletteView   *view,
                                                    GimpPaletteEntry  *entry,
                                                    GdkModifierType    state,
103
                                                    GimpPaletteEditor *editor);
104 105 106 107 108 109 110 111 112 113 114 115
static void   palette_editor_entry_selected        (GimpPaletteView   *view,
                                                    GimpPaletteEntry  *entry,
                                                    GimpPaletteEditor *editor);
static void   palette_editor_entry_activated       (GimpPaletteView   *view,
                                                    GimpPaletteEntry  *entry,
                                                    GimpPaletteEditor *editor);
static void   palette_editor_entry_context         (GimpPaletteView   *view,
                                                    GimpPaletteEntry  *entry,
                                                    GimpPaletteEditor *editor);
static void   palette_editor_color_dropped         (GimpPaletteView   *view,
                                                    GimpPaletteEntry  *entry,
                                                    const GimpRGB     *color,
116
                                                    GimpPaletteEditor *editor);
117 118

static void   palette_editor_color_name_changed    (GtkWidget         *widget,
119
                                                    GimpPaletteEditor *editor);
120 121 122 123 124 125 126
static void   palette_editor_columns_changed       (GtkAdjustment     *adj,
                                                    GimpPaletteEditor *editor);

static void   palette_editor_resize                (GimpPaletteEditor *editor,
                                                    gint               width,
                                                    gdouble            zoom_factor);
static void   palette_editor_scroll_top_left       (GimpPaletteEditor *editor);
127

Elliot Lee's avatar
Elliot Lee committed
128

129 130 131
G_DEFINE_TYPE_WITH_CODE (GimpPaletteEditor, gimp_palette_editor,
                         GIMP_TYPE_DATA_EDITOR,
                         G_IMPLEMENT_INTERFACE (GIMP_TYPE_DOCKED,
132
                                                gimp_palette_editor_docked_iface_init))
133

134 135 136
#define parent_class gimp_palette_editor_parent_class

static GimpDockedInterface *parent_docked_iface = NULL;
137

138

139 140 141
static void
gimp_palette_editor_class_init (GimpPaletteEditorClass *klass)
{
142 143 144 145
  GObjectClass        *object_class     = G_OBJECT_CLASS (klass);
  GtkObjectClass      *gtk_object_class = GTK_OBJECT_CLASS (klass);
  GtkWidgetClass      *widget_class     = GTK_WIDGET_CLASS (klass);
  GimpDataEditorClass *editor_class     = GIMP_DATA_EDITOR_CLASS (klass);
146

147
  object_class->constructor = gimp_palette_editor_constructor;
148

149
  gtk_object_class->destroy = gimp_palette_editor_destroy;
150

151
  widget_class->unmap       = gimp_palette_editor_unmap;
Elliot Lee's avatar
Elliot Lee committed
152

153 154
  editor_class->set_data    = gimp_palette_editor_set_data;
  editor_class->title       = _("Palette Editor");
Elliot Lee's avatar
Elliot Lee committed
155 156
}

157 158
static void
gimp_palette_editor_init (GimpPaletteEditor *editor)
159
{
160
  GtkWidget *scrolled_win;
161 162
  GtkWidget *eventbox;
  GtkWidget *alignment;
163 164 165
  GtkWidget *hbox;
  GtkWidget *label;
  GtkWidget *spinbutton;
166

167 168 169 170
  editor->zoom_factor = 1.0;
  editor->col_width   = 0;
  editor->last_width  = 0;
  editor->columns     = COLUMNS;
171

172
  editor->scrolled_window = scrolled_win = gtk_scrolled_window_new (NULL, NULL);
173 174
  gtk_widget_set_size_request (scrolled_win, -1, PREVIEW_HEIGHT);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
175 176
                                  GTK_POLICY_AUTOMATIC,
                                  GTK_POLICY_AUTOMATIC);
177 178
  gtk_box_pack_start (GTK_BOX (editor), scrolled_win, TRUE, TRUE, 0);
  gtk_widget_show (scrolled_win);
179 180

  eventbox = gtk_event_box_new ();
181
  gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_win),
182
                                         eventbox);
183 184
  gtk_widget_show (eventbox);

185
  g_signal_connect (eventbox, "button-press-event",
186 187
                    G_CALLBACK (palette_editor_eventbox_button_press),
                    editor);
188
  g_signal_connect (eventbox->parent, "size-allocate",
189
                    G_CALLBACK (palette_editor_viewport_size_allocate),
190
                    editor);
191

192 193
  gimp_dnd_color_dest_add (eventbox, palette_editor_drop_color, editor);
  gimp_dnd_viewable_dest_add (eventbox, GIMP_TYPE_PALETTE,
194
                              palette_editor_drop_palette,
195 196
                              editor);

197
  alignment = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
198 199 200
  gtk_container_add (GTK_CONTAINER (eventbox), alignment);
  gtk_widget_show (alignment);

201 202 203 204 205 206 207 208 209 210 211 212
  editor->view = gimp_view_new_full_by_types (GIMP_TYPE_PALETTE_VIEW,
                                              GIMP_TYPE_PALETTE,
                                              PREVIEW_WIDTH, PREVIEW_HEIGHT, 0,
                                              FALSE, TRUE, FALSE);
  gimp_view_renderer_palette_set_cell_size
    (GIMP_VIEW_RENDERER_PALETTE (GIMP_VIEW (editor->view)->renderer), -1);
  gimp_view_renderer_palette_set_draw_grid
    (GIMP_VIEW_RENDERER_PALETTE (GIMP_VIEW (editor->view)->renderer), TRUE);
  gtk_container_add (GTK_CONTAINER (alignment), editor->view);
  gtk_widget_show (editor->view);

  g_signal_connect (editor->view, "entry-clicked",
213 214
                    G_CALLBACK (palette_editor_entry_clicked),
                    editor);
215
  g_signal_connect (editor->view, "entry-selected",
216 217
                    G_CALLBACK (palette_editor_entry_selected),
                    editor);
218
  g_signal_connect (editor->view, "entry-activated",
219 220
                    G_CALLBACK (palette_editor_entry_activated),
                    editor);
221
  g_signal_connect (editor->view, "entry-context",
222 223
                    G_CALLBACK (palette_editor_entry_context),
                    editor);
224
  g_signal_connect (editor->view, "color-dropped",
225 226
                    G_CALLBACK (palette_editor_color_dropped),
                    editor);
227

228
  gimp_dnd_viewable_dest_add (editor->view, GIMP_TYPE_PALETTE,
229
                              palette_editor_drop_palette,
230 231
                              editor);

232 233 234 235
  hbox = gtk_hbox_new (FALSE, 2);
  gtk_box_pack_start (GTK_BOX (editor), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);

236
  /*  The color name entry  */
237 238 239 240 241
  editor->color_name = gtk_entry_new ();
  gtk_box_pack_start (GTK_BOX (hbox), editor->color_name, TRUE, TRUE, 0);
  gtk_entry_set_text (GTK_ENTRY (editor->color_name), _("Undefined"));
  gtk_widget_set_sensitive (editor->color_name, FALSE);
  gtk_widget_show (editor->color_name);
242

243 244 245
  g_signal_connect (editor->color_name, "changed",
                    G_CALLBACK (palette_editor_color_name_changed),
                    editor);
246

247 248 249 250 251 252 253 254 255
  label = gtk_label_new (_("Columns:"));
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

  spinbutton = gimp_spin_button_new ((GtkObject **) &editor->columns_data,
                                     0, 0, 64, 1, 4, 4, 1, 0);
  gtk_box_pack_start (GTK_BOX (hbox), spinbutton, FALSE, FALSE, 0);
  gtk_widget_show (spinbutton);

256
  g_signal_connect (editor->columns_data, "value-changed",
257 258
                    G_CALLBACK (palette_editor_columns_changed),
                    editor);
259 260 261
}

static void
262
gimp_palette_editor_docked_iface_init (GimpDockedInterface *iface)
263
{
264
  parent_docked_iface = g_type_interface_peek_parent (iface);
265

266 267 268
  if (! parent_docked_iface)
    parent_docked_iface = g_type_default_interface_peek (GIMP_TYPE_DOCKED);

269 270
  iface->set_aux_info = gimp_palette_editor_set_aux_info;
  iface->get_aux_info = gimp_palette_editor_get_aux_info;
271 272 273 274 275 276 277 278 279 280 281 282 283
}

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

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

  editor = GIMP_PALETTE_EDITOR (object);
284

285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
  gimp_editor_add_action_button (GIMP_EDITOR (editor), "palette-editor",
                                 "palette-editor-edit-color", NULL);

  gimp_editor_add_action_button (GIMP_EDITOR (editor), "palette-editor",
                                 "palette-editor-new-color-fg",
                                 "palette-editor-new-color-bg",
                                 GDK_CONTROL_MASK,
                                 NULL);

  gimp_editor_add_action_button (GIMP_EDITOR (editor), "palette-editor",
                                 "palette-editor-delete-color", NULL);

  gimp_editor_add_action_button (GIMP_EDITOR (editor), "palette-editor",
                                 "palette-editor-zoom-out", NULL);

  gimp_editor_add_action_button (GIMP_EDITOR (editor), "palette-editor",
                                 "palette-editor-zoom-in", NULL);

  gimp_editor_add_action_button (GIMP_EDITOR (editor), "palette-editor",
                                 "palette-editor-zoom-all", NULL);
305

306
  return object;
307 308
}

309 310
#define AUX_INFO_ZOOM_FACTOR "zoom-factor"

311 312 313 314 315 316 317
static void
gimp_palette_editor_set_aux_info (GimpDocked *docked,
                                  GList      *aux_info)
{
  GimpPaletteEditor *editor = GIMP_PALETTE_EDITOR (docked);
  GList             *list;

318
  parent_docked_iface->set_aux_info (docked, aux_info);
319 320 321 322 323

  for (list = aux_info; list; list = g_list_next (list))
    {
      GimpSessionInfoAux *aux = list->data;

324
      if (! strcmp (aux->name, AUX_INFO_ZOOM_FACTOR))
325 326 327 328 329 330 331 332 333 334 335 336 337
        {
          gdouble zoom_factor;

          zoom_factor = g_ascii_strtod (aux->value, NULL);

          editor->zoom_factor = CLAMP (zoom_factor, 0.1, 4.0);
        }
    }
}

static GList *
gimp_palette_editor_get_aux_info (GimpDocked *docked)
{
338 339
  GimpPaletteEditor *editor = GIMP_PALETTE_EDITOR (docked);
  GList             *aux_info;
340

341
  aux_info = parent_docked_iface->get_aux_info (docked);
342 343 344 345 346 347 348 349

  if (editor->zoom_factor != 1.0)
    {
      GimpSessionInfoAux *aux;
      gchar               value[G_ASCII_DTOSTR_BUF_SIZE];

      g_ascii_formatd (value, sizeof (value), "%.2f", editor->zoom_factor);

350
      aux = gimp_session_info_aux_new (AUX_INFO_ZOOM_FACTOR, value);
351 352 353 354 355 356
      aux_info = g_list_append (aux_info, aux);
    }

  return aux_info;
}

357 358 359
static void
gimp_palette_editor_destroy (GtkObject *object)
{
360
  GimpPaletteEditor *editor = GIMP_PALETTE_EDITOR (object);
361

362
  if (editor->color_dialog)
363
    {
364
      gtk_widget_destroy (editor->color_dialog);
365
      editor->color_dialog = NULL;
366
    }
367

368 369
  GTK_OBJECT_CLASS (parent_class)->destroy (object);
}
370

371 372 373
static void
gimp_palette_editor_unmap (GtkWidget *widget)
{
374
  GimpPaletteEditor *editor = GIMP_PALETTE_EDITOR (widget);
375

376
  if (editor->color_dialog)
377
    gtk_widget_hide (editor->color_dialog);
378

379 380
  GTK_WIDGET_CLASS (parent_class)->unmap (widget);
}
381

382 383 384 385
static void
gimp_palette_editor_set_data (GimpDataEditor *editor,
                              GimpData       *data)
{
386
  GimpPaletteEditor *palette_editor = GIMP_PALETTE_EDITOR (editor);
387

388
  g_signal_handlers_block_by_func (palette_editor->columns_data,
389 390 391
                                   palette_editor_columns_changed,
                                   editor);

392 393
  if (editor->data)
    {
394
      if (palette_editor->color_dialog)
395
        {
396
          gtk_widget_destroy (palette_editor->color_dialog);
397
          palette_editor->color_dialog = NULL;
398 399
        }

400
      g_signal_handlers_disconnect_by_func (editor->data,
401 402 403
                                            palette_editor_invalidate_preview,
                                            editor);

404
      gtk_adjustment_set_value (palette_editor->columns_data, 0);
405
    }
406

407 408
  GIMP_DATA_EDITOR_CLASS (parent_class)->set_data (editor, data);

409 410 411
  gimp_view_set_viewable (GIMP_VIEW (palette_editor->view),
                          GIMP_VIEWABLE (data));

412 413
  if (editor->data)
    {
414
      GimpPalette *palette = GIMP_PALETTE (editor->data);
415

416
      g_signal_connect (editor->data, "invalidate-preview",
417 418 419
                        G_CALLBACK (palette_editor_invalidate_preview),
                        editor);

420 421 422
      gtk_adjustment_set_value (palette_editor->columns_data,
                                palette->n_columns);

423 424
      palette_editor_scroll_top_left (palette_editor);

425 426 427
      palette_editor_invalidate_preview (GIMP_PALETTE (editor->data),
                                         palette_editor);
    }
428

429
  g_signal_handlers_unblock_by_func (palette_editor->columns_data,
430 431
                                     palette_editor_columns_changed,
                                     editor);
432 433
}

434
/*  public functions  */
435

436
GtkWidget *
437 438
gimp_palette_editor_new (Gimp            *gimp,
                         GimpMenuFactory *menu_factory)
439
{
440 441 442 443 444 445
  GimpPalette *palette;

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);

  palette = gimp_context_get_palette (gimp_get_user_context (gimp));

446 447 448 449 450
  return g_object_new (GIMP_TYPE_PALETTE_EDITOR,
                       "menu-factory",    menu_factory,
                       "menu-identifier", "<PaletteEditor>",
                       "ui-path",         "/palette-editor-popup",
                       "data-factory",    gimp->palette_factory,
451
                       "data",            palette,
452
                       NULL);
453 454
}

455
void
456 457 458
gimp_palette_editor_pick_color (GimpPaletteEditor  *editor,
                                const GimpRGB      *color,
                                GimpColorPickState  pick_state)
459
{
460 461
  g_return_if_fail (GIMP_IS_PALETTE_EDITOR (editor));
  g_return_if_fail (color != NULL);
462

463 464
  if (GIMP_DATA_EDITOR (editor)->data_editable)
    {
465 466 467 468 469
      GimpPaletteEntry *entry;
      GimpData         *data;
      gint              index = -1;

      data = gimp_data_editor_get_data (GIMP_DATA_EDITOR (editor));
470

471 472 473
      switch (pick_state)
        {
        case GIMP_COLOR_PICK_STATE_NEW:
474 475 476
          if (editor->color)
            index = editor->color->position + 1;

477 478
          entry = gimp_palette_add_entry (GIMP_PALETTE (data), index,
                                          NULL, color);
479 480
          gimp_palette_view_select_entry (GIMP_PALETTE_VIEW (editor->view),
                                          entry);
481 482 483 484
          break;

        case GIMP_COLOR_PICK_STATE_UPDATE:
          editor->color->color = *color;
485
          gimp_data_dirty (data);
486 487
          break;
        }
488
    }
489 490
}

491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
void
gimp_palette_editor_zoom (GimpPaletteEditor  *editor,
                          GimpZoomType        zoom_type)
{
  GimpPalette *palette;
  gdouble      zoom_factor;

  g_return_if_fail (GIMP_IS_PALETTE_EDITOR (editor));

  palette = GIMP_PALETTE (GIMP_DATA_EDITOR (editor)->data);

  if (! palette)
    return;

  zoom_factor = editor->zoom_factor;

  switch (zoom_type)
    {
    case GIMP_ZOOM_IN:
      zoom_factor += 0.1;
      break;

    case GIMP_ZOOM_OUT:
      zoom_factor -= 0.1;
      break;

517
    case GIMP_ZOOM_TO: /* used as ZOOM_ALL */
518
      {
519 520
        gint height  = editor->view->parent->parent->parent->allocation.height;
        gint columns = palette->n_columns ? palette->n_columns : COLUMNS;
521 522
        gint rows;

523
        rows = palette->n_colors / columns;
524

525 526
        if (palette->n_colors % columns)
          rows += 1;
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542

        rows = MAX (1, rows);

        zoom_factor = (((gdouble) height - 2 * SPACING) /
                       (gdouble) rows - SPACING) / ENTRY_HEIGHT;
      }
      break;
    }

  zoom_factor = CLAMP (zoom_factor, 0.1, 4.0);

  if (palette->n_columns)
    editor->columns = palette->n_columns;
  else
    editor->columns = COLUMNS;

543
  palette_editor_resize (editor, editor->last_width, zoom_factor);
544 545 546 547

  palette_editor_scroll_top_left (editor);
}

548 549 550 551 552 553 554 555 556 557 558
gint
gimp_palette_editor_get_index (GimpPaletteEditor *editor,
                               const GimpRGB     *search)
{
  GimpPalette *palette;
  gint         index = 0;

  g_return_val_if_fail (GIMP_IS_PALETTE_EDITOR (editor), -1);

  palette = GIMP_PALETTE (GIMP_DATA_EDITOR (editor)->data);

559
  if (! palette || palette->n_colors == 0)
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
    return -1;

  if (editor->color)
    index = editor->color->position;

  if (search)
    {
      if (! editor->color ||
          gimp_rgb_distance (&editor->color->color, search) > EPSILON)
        {
          GList *list;

          for (list = palette->colors; list; list = g_list_next (list))
            {
              GimpPaletteEntry *entry = list->data;

              if (gimp_rgb_distance (&entry->color, search) < EPSILON)
                {
                  index = entry->position;
                  break;
                }
            }
        }
    }

  return index;
}

gboolean
gimp_palette_editor_set_index (GimpPaletteEditor *editor,
                               gint               index,
                               GimpRGB           *color)
{
  GimpPalette *palette;
  GList       *list;

  g_return_val_if_fail (GIMP_IS_PALETTE_EDITOR (editor), FALSE);

  palette = GIMP_PALETTE (GIMP_DATA_EDITOR (editor)->data);

600
  if (! palette || palette->n_colors == 0)
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
    return FALSE;

  index = CLAMP (index, 0, palette->n_colors - 1);

  list = g_list_nth (palette->colors, index);

  gimp_palette_view_select_entry (GIMP_PALETTE_VIEW (editor->view),
                                  list->data);

  if (color)
    *color = editor->color->color;

  return TRUE;
}

gint
gimp_palette_editor_max_index (GimpPaletteEditor *editor)
{
  GimpPalette *palette;

  g_return_val_if_fail (GIMP_IS_PALETTE_EDITOR (editor), -1);

  palette = GIMP_PALETTE (GIMP_DATA_EDITOR (editor)->data);

625
  if (! palette || palette->n_colors == 0)
626 627
    return -1;

628
  return palette->n_colors - 1;
629 630
}

631 632 633

/*  private functions  */

634 635
static void
palette_editor_invalidate_preview (GimpPalette       *palette,
636
                                   GimpPaletteEditor *editor)
637
{
638 639 640 641
  if (palette->n_columns)
    editor->columns = palette->n_columns;
  else
    editor->columns = COLUMNS;
642

643
  palette_editor_resize (editor, editor->last_width, editor->zoom_factor);
644
}
645

646 647 648 649
static void
palette_editor_viewport_size_allocate (GtkWidget         *widget,
                                       GtkAllocation     *allocation,
                                       GimpPaletteEditor *editor)
Elliot Lee's avatar
Elliot Lee committed
650
{
651
  if (widget->allocation.width != editor->last_width)
652
    {
653 654
      palette_editor_resize (editor, widget->allocation.width,
                             editor->zoom_factor);
655
    }
Elliot Lee's avatar
Elliot Lee committed
656 657
}

658 659
static gboolean
palette_editor_eventbox_button_press (GtkWidget         *widget,
660 661
                                      GdkEventButton    *bevent,
                                      GimpPaletteEditor *editor)
662
{
663
  if (bevent->button == 3 && bevent->type == GDK_BUTTON_PRESS)
Elliot Lee's avatar
Elliot Lee committed
664
    {
665
      return gimp_editor_popup_menu (GIMP_EDITOR (editor), NULL, NULL);
Elliot Lee's avatar
Elliot Lee committed
666 667
    }

668
  return TRUE;
Elliot Lee's avatar
Elliot Lee committed
669 670 671
}

static void
672 673 674 675 676
palette_editor_drop_color (GtkWidget     *widget,
                           gint           x,
                           gint           y,
                           const GimpRGB *color,
                           gpointer       data)
Elliot Lee's avatar
Elliot Lee committed
677
{
678
  GimpPaletteEditor *editor = GIMP_PALETTE_EDITOR (data);
Elliot Lee's avatar
Elliot Lee committed
679

680
  if (GIMP_DATA_EDITOR (editor)->data_editable)
681
    {
682 683
      GimpPalette      *palette = GIMP_PALETTE (GIMP_DATA_EDITOR (editor)->data);
      GimpPaletteEntry *entry;
684

685 686
      entry = gimp_palette_add_entry (palette, -1, NULL, color);
      gimp_palette_view_select_entry (GIMP_PALETTE_VIEW (editor->view), entry);
Elliot Lee's avatar
Elliot Lee committed
687 688 689 690
    }
}

static void
691 692 693
palette_editor_drop_palette (GtkWidget    *widget,
                             gint          x,
                             gint          y,
694 695
                             GimpViewable *viewable,
                             gpointer      data)
Elliot Lee's avatar
Elliot Lee committed
696
{
697 698
  gimp_data_editor_set_data (GIMP_DATA_EDITOR (data), GIMP_DATA (viewable));
}
Elliot Lee's avatar
Elliot Lee committed
699

700

701
/*  palette view callbacks  */
Elliot Lee's avatar
Elliot Lee committed
702

703
static void
704 705 706 707
palette_editor_entry_clicked (GimpPaletteView   *view,
                              GimpPaletteEntry  *entry,
                              GdkModifierType    state,
                              GimpPaletteEditor *editor)
708
{
709 710 711 712
  if (entry)
    {
      Gimp        *gimp;
      GimpContext *user_context;
Elliot Lee's avatar
Elliot Lee committed
713

714 715
      gimp         = GIMP_DATA_EDITOR (editor)->data_factory->gimp;
      user_context = gimp_get_user_context (gimp);
716

717 718 719 720 721
      if (state & GDK_CONTROL_MASK)
        gimp_context_set_background (user_context, &entry->color);
      else
        gimp_context_set_foreground (user_context, &entry->color);
    }
722 723 724
}

static void
725 726 727
palette_editor_entry_selected (GimpPaletteView   *view,
                               GimpPaletteEntry  *entry,
                               GimpPaletteEditor *editor)
728
{
729
  GimpDataEditor *data_editor = GIMP_DATA_EDITOR (editor);
730 731 732 733 734
  GimpPalette    *palette;

  palette = GIMP_PALETTE (data_editor->data);

  if (editor->color != entry)
735
    {
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
      editor->color = entry;

      g_signal_handlers_block_by_func (editor->color_name,
                                       palette_editor_color_name_changed,
                                       editor);

      gtk_entry_set_text (GTK_ENTRY (editor->color_name),
                          entry ? entry->name : _("Undefined"));

      g_signal_handlers_unblock_by_func (editor->color_name,
                                         palette_editor_color_name_changed,
                                         editor);

      gtk_widget_set_sensitive (editor->color_name,
                                entry && data_editor->data_editable);
751 752 753

      gimp_ui_manager_update (GIMP_EDITOR (editor)->ui_manager,
                              GIMP_EDITOR (editor)->popup_data);
754
    }
Elliot Lee's avatar
Elliot Lee committed
755 756
}

757 758 759 760 761 762 763 764
static void
palette_editor_entry_activated (GimpPaletteView   *view,
                                GimpPaletteEntry  *entry,
                                GimpPaletteEditor *editor)
{
  if (GIMP_DATA_EDITOR (editor)->data_editable && entry == editor->color)
    {
      GtkAction *action;
765

766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
      action = gimp_ui_manager_find_action (GIMP_EDITOR (editor)->ui_manager,
                                            "palette-editor",
                                            "palette-editor-edit-color");

      if (action)
        gtk_action_activate (action);
    }
}

static void
palette_editor_entry_context (GimpPaletteView   *view,
                              GimpPaletteEntry  *entry,
                              GimpPaletteEditor *editor)
{
  gimp_editor_popup_menu (GIMP_EDITOR (editor), NULL, NULL);
}

static void
palette_editor_color_dropped (GimpPaletteView   *view,
                              GimpPaletteEntry  *entry,
                              const GimpRGB     *color,
                              GimpPaletteEditor *editor)
{
  if (GIMP_DATA_EDITOR (editor)->data_editable)
    {
      GimpPalette *palette = GIMP_PALETTE (GIMP_DATA_EDITOR (editor)->data);
      gint         pos     = -1;

      if (entry)
        pos = entry->position;

      entry = gimp_palette_add_entry (palette, pos, NULL, color);
      gimp_palette_view_select_entry (GIMP_PALETTE_VIEW (editor->view), entry);
    }
}


/*  color name and columns callbacks  */
804

Elliot Lee's avatar
Elliot Lee committed
805
static void
806
palette_editor_color_name_changed (GtkWidget         *widget,
807
                                   GimpPaletteEditor *editor)
Elliot Lee's avatar
Elliot Lee committed
808
{
809 810 811
  if (GIMP_DATA_EDITOR (editor)->data)
    {
      GimpPalette *palette;
812

813
      palette = GIMP_PALETTE (GIMP_DATA_EDITOR (editor)->data);
814

815 816
      if (editor->color->name)
        g_free (editor->color->name);
Michael Natterer's avatar
Michael Natterer committed
817

818 819
      editor->color->name =
        g_strdup (gtk_entry_get_text (GTK_ENTRY (editor->color_name)));
Michael Natterer's avatar
Michael Natterer committed
820

821 822
      gimp_data_dirty (GIMP_DATA (palette));
    }
823
}
Elliot Lee's avatar
Elliot Lee committed
824

825 826 827 828
static void
palette_editor_columns_changed (GtkAdjustment     *adj,
                                GimpPaletteEditor *editor)
{
829 830
  if (GIMP_DATA_EDITOR (editor)->data)
    {
831
      GimpPalette *palette = GIMP_PALETTE (GIMP_DATA_EDITOR (editor)->data);
832

833
      gimp_palette_set_columns (palette, ROUND (adj->value));
834 835 836
    }
}

837

838
/*  misc utils  */
839 840

static void
841 842 843
palette_editor_resize (GimpPaletteEditor *editor,
                       gint               width,
                       gdouble            zoom_factor)
844
{
845 846 847 848
  GimpPalette *palette;
  gint         rows;
  gint         preview_width;
  gint         preview_height;
849

850
  palette = GIMP_PALETTE (GIMP_DATA_EDITOR (editor)->data);
851

852 853
  if (! palette)
    return;
854

855 856 857
  editor->zoom_factor = zoom_factor;
  editor->last_width  = width;
  editor->col_width   = width / (editor->columns + 1) - SPACING;
858

859 860
  if (editor->col_width < 0)
    editor->col_width = 0;
861

862 863 864
  rows = palette->n_colors / editor->columns;
  if (palette->n_colors % editor->columns)
    rows += 1;
865

866 867 868
  preview_width  = (editor->col_width + SPACING) * editor->columns;
  preview_height = (rows *
                    (SPACING + (gint) (ENTRY_HEIGHT * editor->zoom_factor)));
869

870 871
  if (preview_height > GIMP_VIEWABLE_MAX_PREVIEW_SIZE)
    preview_height = ((GIMP_VIEWABLE_MAX_PREVIEW_SIZE - SPACING) / rows) * rows;
872

873 874 875
  gimp_view_renderer_set_size_full (GIMP_VIEW (editor->view)->renderer,
                                    preview_width  + SPACING,
                                    preview_height + SPACING, 0);
876 877 878
}

static void
879
palette_editor_scroll_top_left (GimpPaletteEditor *palette_editor)
880
{
881 882
  GtkAdjustment *hadj;
  GtkAdjustment *vadj;
883

884 885 886 887 888 889 890 891 892 893
  if (! palette_editor->scrolled_window)
    return;

  hadj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (palette_editor->scrolled_window));
  vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (palette_editor->scrolled_window));

  if (hadj)
    gtk_adjustment_set_value (hadj, 0.0);
  if (vadj)
    gtk_adjustment_set_value (vadj, 0.0);
894
}