gimpcolordisplayeditor.c 29.2 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3 4 5
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimpcolordisplayeditor.c
 * Copyright (C) 2003 Michael Natterer <mitch@gimp.org>
Manish Singh's avatar
Manish Singh committed
6
 *
7
 * This program is free software: you can redistribute it and/or modify
Manish Singh's avatar
Manish Singh committed
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
Manish Singh's avatar
Manish Singh committed
10 11 12 13 14 15 16 17
 * (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
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
Manish Singh's avatar
Manish Singh committed
19
 */
20

21
#include "config.h"
22

23
#include <gegl.h>
24 25
#include <gtk/gtk.h>

26 27
#include "libgimpcolor/gimpcolor.h"
#include "libgimpconfig/gimpconfig.h"
28
#include "libgimpwidgets/gimpwidgets.h"
29

30
#include "widgets-types.h"
31

32 33 34 35 36 37
#include "propgui/propgui-types.h"

#include "core/gimp.h"

#include "propgui/gimppropgui.h"

38
#include "gimpcolordisplayeditor.h"
39
#include "gimpeditor.h"
40

41
#include "gimp-intl.h"
42

43

44
#define LIST_WIDTH  200
45 46 47
#define LIST_HEIGHT 100


48
enum
49
{
50
  SRC_COLUMN_NAME,
51
  SRC_COLUMN_ICON,
52 53 54
  SRC_COLUMN_TYPE,
  N_SRC_COLUMNS
};
55

56 57
enum
{
58
  DEST_COLUMN_ENABLED,
59
  DEST_COLUMN_NAME,
60
  DEST_COLUMN_ICON,
61 62
  DEST_COLUMN_FILTER,
  N_DEST_COLUMNS
63 64
};

65

66
static void   gimp_color_display_editor_dispose        (GObject               *object);
67

68 69 70 71 72 73 74 75 76 77
static void   gimp_color_display_editor_add_clicked    (GtkWidget             *widget,
                                                        GimpColorDisplayEditor *editor);
static void   gimp_color_display_editor_remove_clicked (GtkWidget             *widget,
                                                        GimpColorDisplayEditor *editor);
static void   gimp_color_display_editor_up_clicked     (GtkWidget             *widget,
                                                        GimpColorDisplayEditor *editor);
static void   gimp_color_display_editor_down_clicked   (GtkWidget             *widget,
                                                        GimpColorDisplayEditor *editor);
static void   gimp_color_display_editor_reset_clicked  (GtkWidget             *widget,
                                                        GimpColorDisplayEditor *editor);
78

79 80 81 82
static void   gimp_color_display_editor_src_changed    (GtkTreeSelection       *sel,
                                                        GimpColorDisplayEditor *editor);
static void   gimp_color_display_editor_dest_changed   (GtkTreeSelection       *sel,
                                                        GimpColorDisplayEditor *editor);
83

84 85 86 87 88 89 90 91 92 93 94
static void   gimp_color_display_editor_added          (GimpColorDisplayStack  *stack,
                                                        GimpColorDisplay       *display,
                                                        gint                    position,
                                                        GimpColorDisplayEditor *editor);
static void   gimp_color_display_editor_removed        (GimpColorDisplayStack  *stack,
                                                        GimpColorDisplay       *display,
                                                        GimpColorDisplayEditor *editor);
static void   gimp_color_display_editor_reordered      (GimpColorDisplayStack  *stack,
                                                        GimpColorDisplay       *display,
                                                        gint                    position,
                                                        GimpColorDisplayEditor *editor);
95

96
static void   gimp_color_display_editor_enabled        (GimpColorDisplay       *display,
97
                                                        GParamSpec             *pspec,
98 99 100 101 102
                                                        GimpColorDisplayEditor *editor);
static void   gimp_color_display_editor_enable_toggled (GtkCellRendererToggle  *toggle,
                                                        const gchar            *path,
                                                        GimpColorDisplayEditor *editor);

103
static void   gimp_color_display_editor_update_buttons (GimpColorDisplayEditor *editor);
104

105

106
G_DEFINE_TYPE (GimpColorDisplayEditor, gimp_color_display_editor, GTK_TYPE_BOX)
107

108
#define parent_class gimp_color_display_editor_parent_class
109 110


111 112 113
static void
gimp_color_display_editor_class_init (GimpColorDisplayEditorClass *klass)
{
114
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
115

116
  object_class->dispose = gimp_color_display_editor_dispose;
117
}
118

119
static void
120
gimp_color_display_editor_init (GimpColorDisplayEditor *editor)
121
{
Sven Neumann's avatar
Sven Neumann committed
122
  GtkWidget         *paned;
123 124 125 126 127 128 129 130 131
  GtkWidget         *hbox;
  GtkWidget         *ed;
  GtkWidget         *scrolled_win;
  GtkWidget         *tv;
  GtkWidget         *vbox;
  GtkWidget         *image;
  GtkTreeViewColumn *column;
  GtkCellRenderer   *rend;

132 133 134
  gtk_orientable_set_orientation (GTK_ORIENTABLE (editor),
                                  GTK_ORIENTATION_VERTICAL);

135
  paned = gtk_paned_new (GTK_ORIENTATION_VERTICAL);
136
  gtk_paned_set_wide_handle (GTK_PANED (paned), TRUE);
Sven Neumann's avatar
Sven Neumann committed
137 138
  gtk_box_pack_start (GTK_BOX (editor), paned, TRUE, TRUE, 0);
  gtk_widget_show (paned);
139

140
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
Sven Neumann's avatar
Sven Neumann committed
141
  gtk_paned_pack1 (GTK_PANED (paned), hbox, FALSE, FALSE);
142 143
  gtk_widget_show (hbox);

144
  scrolled_win = gtk_scrolled_window_new (NULL, NULL);
145 146
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
                                       GTK_SHADOW_IN);
147
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
148 149
                                  GTK_POLICY_AUTOMATIC,
                                  GTK_POLICY_AUTOMATIC);
150
  gtk_box_pack_start (GTK_BOX (hbox), scrolled_win, TRUE, TRUE, 0);
151
  gtk_widget_show (scrolled_win);
152

153
  editor->src = gtk_list_store_new (N_SRC_COLUMNS,
154
                                    G_TYPE_STRING,
155
                                    G_TYPE_STRING,
156
                                    G_TYPE_GTYPE);
157 158
  gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (editor->src),
                                        SRC_COLUMN_NAME, GTK_SORT_ASCENDING);
159 160
  tv = gtk_tree_view_new_with_model (GTK_TREE_MODEL (editor->src));
  g_object_unref (editor->src);
161 162 163 164

  gtk_widget_set_size_request (tv, LIST_WIDTH, LIST_HEIGHT);
  gtk_tree_view_set_headers_clickable (GTK_TREE_VIEW (tv), FALSE);

165 166 167 168 169 170 171
  column = gtk_tree_view_column_new ();
  gtk_tree_view_column_set_title (column, _("Available Filters"));
  gtk_tree_view_append_column (GTK_TREE_VIEW (tv), column);

  rend = gtk_cell_renderer_pixbuf_new ();
  gtk_tree_view_column_pack_start (column, rend, FALSE);
  gtk_tree_view_column_set_attributes (column, rend,
172
                                       "icon-name", SRC_COLUMN_ICON,
173 174 175 176 177 178 179 180
                                       NULL);

  rend = gtk_cell_renderer_text_new ();
  gtk_tree_view_column_pack_start (column, rend, TRUE);
  gtk_tree_view_column_set_attributes (column, rend,
                                       "text", SRC_COLUMN_NAME,
                                       NULL);

181 182 183
  gtk_container_add (GTK_CONTAINER (scrolled_win), tv);
  gtk_widget_show (tv);

184
  editor->src_sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (tv));
185

186 187 188
  g_signal_connect (editor->src_sel, "changed",
                    G_CALLBACK (gimp_color_display_editor_src_changed),
                    editor);
189

190 191
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
  gtk_box_set_homogeneous (GTK_BOX (vbox), TRUE);
192 193 194
  gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 0);
  gtk_widget_show (vbox);

195
  editor->add_button = gtk_button_new ();
196
  gtk_box_pack_start (GTK_BOX (vbox), editor->add_button, TRUE, FALSE, 0);
197 198
  gtk_widget_set_sensitive (editor->add_button, FALSE);
  gtk_widget_show (editor->add_button);
199

200
  image = gtk_image_new_from_icon_name (GIMP_ICON_GO_NEXT,
201
                                        GTK_ICON_SIZE_BUTTON);
202
  gtk_container_add (GTK_CONTAINER (editor->add_button), image);
203 204
  gtk_widget_show (image);

205 206 207
  g_signal_connect (editor->add_button, "clicked",
                    G_CALLBACK (gimp_color_display_editor_add_clicked),
                    editor);
208

209
  editor->remove_button = gtk_button_new ();
210
  gtk_box_pack_start (GTK_BOX (vbox), editor->remove_button, TRUE, FALSE, 0);
211 212
  gtk_widget_set_sensitive (editor->remove_button, FALSE);
  gtk_widget_show (editor->remove_button);
213

214
  image = gtk_image_new_from_icon_name (GIMP_ICON_GO_PREVIOUS,
215
                                        GTK_ICON_SIZE_BUTTON);
216
  gtk_container_add (GTK_CONTAINER (editor->remove_button), image);
217 218
  gtk_widget_show (image);

219 220 221
  g_signal_connect (editor->remove_button, "clicked",
                    G_CALLBACK (gimp_color_display_editor_remove_clicked),
                    editor);
222

223 224 225
  ed = gimp_editor_new ();
  gtk_box_pack_start (GTK_BOX (hbox), ed, TRUE, TRUE, 0);
  gtk_widget_show (ed);
226

227 228
  editor->up_button =
    gimp_editor_add_button (GIMP_EDITOR (ed),
229
                            GIMP_ICON_GO_UP,
230 231
                            _("Move the selected filter up"),
                            NULL,
232
                            G_CALLBACK (gimp_color_display_editor_up_clicked),
233
                            NULL,
234
                            editor);
235

236 237
  editor->down_button =
    gimp_editor_add_button (GIMP_EDITOR (ed),
238
                            GIMP_ICON_GO_DOWN,
239 240
                            _("Move the selected filter down"),
                            NULL,
241
                            G_CALLBACK (gimp_color_display_editor_down_clicked),
242
                            NULL,
243
                            editor);
244

245 246
  gtk_widget_set_sensitive (editor->up_button,   FALSE);
  gtk_widget_set_sensitive (editor->down_button, FALSE);
247 248

  scrolled_win = gtk_scrolled_window_new (NULL, NULL);
249 250
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
                                       GTK_SHADOW_IN);
251
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
252 253
                                  GTK_POLICY_AUTOMATIC,
                                  GTK_POLICY_AUTOMATIC);
254
  gtk_box_pack_start (GTK_BOX (ed), scrolled_win, TRUE, TRUE, 0);
255
  gtk_widget_show (scrolled_win);
256

257
  editor->dest = gtk_list_store_new (N_DEST_COLUMNS,
258 259
                                     G_TYPE_BOOLEAN,
                                     G_TYPE_STRING,
260
                                     G_TYPE_STRING,
261
                                     GIMP_TYPE_COLOR_DISPLAY);
262 263
  tv = gtk_tree_view_new_with_model (GTK_TREE_MODEL (editor->dest));
  g_object_unref (editor->dest);
264

265 266 267
  gtk_widget_set_size_request (tv, LIST_WIDTH, LIST_HEIGHT);
  gtk_tree_view_set_headers_clickable (GTK_TREE_VIEW (tv), FALSE);

268 269 270 271 272 273
  rend = gtk_cell_renderer_toggle_new ();

  g_signal_connect (rend, "toggled",
                    G_CALLBACK (gimp_color_display_editor_enable_toggled),
                    editor);

274 275 276 277 278 279
  column = gtk_tree_view_column_new_with_attributes (NULL, rend,
                                                     "active",
                                                     DEST_COLUMN_ENABLED,
                                                     NULL);
  gtk_tree_view_insert_column (GTK_TREE_VIEW (tv), column, 0);

280
  image = gtk_image_new_from_icon_name (GIMP_ICON_VISIBLE,
281
                                        GTK_ICON_SIZE_MENU);
282 283 284
  gtk_tree_view_column_set_widget (column, image);
  gtk_widget_show (image);

285 286 287 288 289 290 291
  column = gtk_tree_view_column_new ();
  gtk_tree_view_column_set_title (column, _("Active Filters"));
  gtk_tree_view_append_column (GTK_TREE_VIEW (tv), column);

  rend = gtk_cell_renderer_pixbuf_new ();
  gtk_tree_view_column_pack_start (column, rend, FALSE);
  gtk_tree_view_column_set_attributes (column, rend,
292
                                       "icon-name", DEST_COLUMN_ICON,
293 294 295 296 297 298 299
                                       NULL);

  rend = gtk_cell_renderer_text_new ();
  gtk_tree_view_column_pack_start (column, rend, TRUE);
  gtk_tree_view_column_set_attributes (column, rend,
                                       "text", DEST_COLUMN_NAME,
                                       NULL);
300

301 302 303
  gtk_container_add (GTK_CONTAINER (scrolled_win), tv);
  gtk_widget_show (tv);

304
  editor->dest_sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (tv));
305

306 307 308
  g_signal_connect (editor->dest_sel, "changed",
                    G_CALLBACK (gimp_color_display_editor_dest_changed),
                    editor);
Manish Singh's avatar
Manish Singh committed
309

310 311
  /*  the config frame  */

312
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
Sven Neumann's avatar
Sven Neumann committed
313 314 315
  gtk_paned_pack2 (GTK_PANED (paned), vbox, TRUE, FALSE);
  gtk_widget_show (vbox);

316
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
Sven Neumann's avatar
Sven Neumann committed
317 318 319
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);

320
  editor->config_frame = gimp_frame_new (NULL);
Sven Neumann's avatar
Sven Neumann committed
321
  gtk_box_pack_start (GTK_BOX (vbox), editor->config_frame, TRUE, TRUE, 0);
322
  gtk_widget_show (editor->config_frame);
323

324
  editor->config_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
325 326
  gtk_container_add (GTK_CONTAINER (editor->config_frame), editor->config_box);
  gtk_widget_show (editor->config_box);
327

328
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
329
  gtk_box_pack_end (GTK_BOX (editor->config_box), hbox, FALSE, FALSE, 0);
330
  gtk_widget_show (hbox);
331

332
  editor->reset_button = gtk_button_new_with_mnemonic (_("_Reset"));
333 334
  gtk_box_pack_end (GTK_BOX (hbox), editor->reset_button, FALSE, FALSE, 0);
  gtk_widget_show (editor->reset_button);
335

336
  gimp_help_set_help_data (editor->reset_button,
337 338 339
                           _("Reset the selected filter to default values"),
                           NULL);

340 341 342
  g_signal_connect (editor->reset_button, "clicked",
                    G_CALLBACK (gimp_color_display_editor_reset_clicked),
                    editor);
343

344
  gimp_color_display_editor_dest_changed (editor->dest_sel, editor);
345 346 347
}

static void
348
gimp_color_display_editor_dispose (GObject *object)
349
{
350
  GimpColorDisplayEditor *editor = GIMP_COLOR_DISPLAY_EDITOR (object);
351

352 353 354 355 356 357 358
  if (editor->selected)
    {
      g_object_remove_weak_pointer (G_OBJECT (editor->selected),
                                    (gpointer) &editor->selected);
      editor->selected = NULL;
    }

359 360 361
  g_clear_object (&editor->stack);
  g_clear_object (&editor->config);
  g_clear_object (&editor->managed);
362

363
  G_OBJECT_CLASS (parent_class)->dispose (object);
364 365
}

366
GtkWidget *
367 368
gimp_color_display_editor_new (Gimp                  *gimp,
                               GimpColorDisplayStack *stack,
369 370
                               GimpColorConfig       *config,
                               GimpColorManaged      *managed)
371
{
372
  GimpColorDisplayEditor *editor;
373 374
  GType                  *display_types;
  guint                   n_display_types;
375 376
  gint                    i;
  GList                  *list;
377

378
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
379
  g_return_val_if_fail (GIMP_IS_COLOR_DISPLAY_STACK (stack), NULL);
380 381
  g_return_val_if_fail (GIMP_IS_COLOR_CONFIG (config), NULL);
  g_return_val_if_fail (GIMP_IS_COLOR_MANAGED (managed), NULL);
382

383
  editor = g_object_new (GIMP_TYPE_COLOR_DISPLAY_EDITOR, NULL);
384

385
  editor->gimp    = gimp;
386 387 388
  editor->stack   = g_object_ref (stack);
  editor->config  = g_object_ref (config);
  editor->managed = g_object_ref (managed);
389

390
  display_types = g_type_children (GIMP_TYPE_COLOR_DISPLAY, &n_display_types);
391

392
  for (i = 0; i < n_display_types; i++)
393
    {
394
      GimpColorDisplayClass *display_class;
395
      GtkTreeIter            iter;
396

397
      display_class = g_type_class_ref (display_types[i]);
398

399
      gtk_list_store_append (editor->src, &iter);
400

401
      gtk_list_store_set (editor->src, &iter,
402
                          SRC_COLUMN_ICON, display_class->icon_name,
403 404
                          SRC_COLUMN_NAME, display_class->name,
                          SRC_COLUMN_TYPE, display_types[i],
405
                          -1);
406

407
      g_type_class_unref (display_class);
408
    }
409

410
  g_free (display_types);
411

412 413 414
  for (list = gimp_color_display_stack_get_filters (stack);
       list;
       list = g_list_next (list))
415
    {
416
      GimpColorDisplay *display = list->data;
417
      GtkTreeIter       iter;
418
      gboolean          enabled;
419
      const gchar      *name;
420
      const gchar      *icon_name;
421

422 423
      enabled = gimp_color_display_get_enabled (display);

424
      name      = GIMP_COLOR_DISPLAY_GET_CLASS (display)->name;
425
      icon_name = GIMP_COLOR_DISPLAY_GET_CLASS (display)->icon_name;
426

427
      gtk_list_store_append (editor->dest, &iter);
428

429
      gtk_list_store_set (editor->dest, &iter,
430
                          DEST_COLUMN_ENABLED, enabled,
431
                          DEST_COLUMN_ICON,    icon_name,
432 433
                          DEST_COLUMN_NAME,    name,
                          DEST_COLUMN_FILTER,  display,
434
                          -1);
435

436
      g_signal_connect_object (display, "notify::enabled",
437
                               G_CALLBACK (gimp_color_display_editor_enabled),
438
                               G_OBJECT (editor), 0);
439
    }
440 441 442 443 444 445 446 447 448 449 450 451

  g_signal_connect_object (stack, "added",
                           G_CALLBACK (gimp_color_display_editor_added),
                           G_OBJECT (editor), 0);
  g_signal_connect_object (stack, "removed",
                           G_CALLBACK (gimp_color_display_editor_removed),
                           G_OBJECT (editor), 0);
  g_signal_connect_object (stack, "reordered",
                           G_CALLBACK (gimp_color_display_editor_reordered),
                           G_OBJECT (editor), 0);

  return GTK_WIDGET (editor);
452 453 454
}

static void
455
gimp_color_display_editor_add_clicked (GtkWidget              *widget,
456
                                       GimpColorDisplayEditor *editor)
457
{
458 459
  GtkTreeModel *model;
  GtkTreeIter   iter;
460

461
  if (gtk_tree_selection_get_selected (editor->src_sel, &model, &iter))
462
    {
463
      GimpColorDisplay *display;
464
      GType             type;
465

466
      gtk_tree_model_get (model, &iter, SRC_COLUMN_TYPE, &type, -1);
467

468 469 470 471
      display = g_object_new (type,
                              "color-config",  editor->config,
                              "color-managed", editor->managed,
                              NULL);
472

473 474 475 476 477
      if (display)
        {
          gimp_color_display_stack_add (editor->stack, display);
          g_object_unref (display);
        }
478
    }
479 480 481
}

static void
482 483
gimp_color_display_editor_remove_clicked (GtkWidget             *widget,
                                          GimpColorDisplayEditor *editor)
484
{
485 486
  if (editor->selected)
    gimp_color_display_stack_remove (editor->stack, editor->selected);
487 488 489
}

static void
490 491
gimp_color_display_editor_up_clicked (GtkWidget             *widget,
                                      GimpColorDisplayEditor *editor)
492
{
493 494
  if (editor->selected)
    gimp_color_display_stack_reorder_up (editor->stack, editor->selected);
495
}
Manish Singh's avatar
Manish Singh committed
496

497
static void
498 499
gimp_color_display_editor_down_clicked (GtkWidget             *widget,
                                        GimpColorDisplayEditor *editor)
500
{
501 502
  if (editor->selected)
    gimp_color_display_stack_reorder_down (editor->stack, editor->selected);
503 504
}

505
static void
506 507
gimp_color_display_editor_reset_clicked (GtkWidget             *widget,
                                         GimpColorDisplayEditor *editor)
508
{
509 510
  if (editor->selected)
    gimp_color_display_configure_reset (editor->selected);
511 512 513
}

static void
514 515
gimp_color_display_editor_src_changed (GtkTreeSelection       *sel,
                                       GimpColorDisplayEditor *editor)
516
{
517 518
  GtkTreeModel *model;
  GtkTreeIter   iter;
Sven Neumann's avatar
Sven Neumann committed
519
  gchar        *tip  = NULL;
520
  const gchar  *name = NULL;
521

522 523
  if (gtk_tree_selection_get_selected (sel, &model, &iter))
    {
524
      GValue val = G_VALUE_INIT;
525

526
      gtk_tree_model_get_value (model, &iter, SRC_COLUMN_NAME, &val);
527

528
      name = g_value_get_string (&val);
529

Sven Neumann's avatar
Sven Neumann committed
530 531
      tip = g_strdup_printf (_("Add '%s' to the list of active filters"), name);

532 533
      g_value_unset (&val);
    }
534

535
  gtk_widget_set_sensitive (editor->add_button, name != NULL);
Sven Neumann's avatar
Sven Neumann committed
536 537 538

  gimp_help_set_help_data (editor->add_button, tip, NULL);
  g_free (tip);
539 540 541
}

static void
542 543
gimp_color_display_editor_dest_changed (GtkTreeSelection       *sel,
                                        GimpColorDisplayEditor *editor)
Manish Singh's avatar
Manish Singh committed
544
{
545 546
  GtkTreeModel     *model;
  GtkTreeIter       iter;
547
  GimpColorDisplay *display = NULL;
Sven Neumann's avatar
Sven Neumann committed
548
  gchar            *tip     = NULL;
549

550
  if (editor->selected)
551
    {
552 553 554
      g_object_remove_weak_pointer (G_OBJECT (editor->selected),
                                    (gpointer) &editor->selected);
      editor->selected = NULL;
555 556
    }

557 558
  if (gtk_tree_selection_get_selected (sel, &model, &iter))
    {
559
      GValue val = G_VALUE_INIT;
560

561
      gtk_tree_model_get_value (model, &iter, DEST_COLUMN_FILTER, &val);
562

563
      display = g_value_get_object (&val);
564

565
      g_value_unset (&val);
Sven Neumann's avatar
Sven Neumann committed
566 567 568

      tip = g_strdup_printf (_("Remove '%s' from the list of active filters"),
                             GIMP_COLOR_DISPLAY_GET_CLASS (display)->name);
569
    }
570

Sven Neumann's avatar
Sven Neumann committed
571 572 573
  gimp_help_set_help_data (editor->remove_button, tip, NULL);
  g_free (tip);

574 575
  gtk_widget_set_sensitive (editor->remove_button, display != NULL);
  gtk_widget_set_sensitive (editor->reset_button,  display != NULL);
576

577 578 579
  if (editor->config_widget)
    gtk_container_remove (GTK_CONTAINER (editor->config_box),
                          editor->config_widget);
580

581
  if (display)
582
    {
583
      editor->selected = display;
584

585
      g_object_add_weak_pointer (G_OBJECT (display),
586
                                 (gpointer) &editor->selected);
587

588
      editor->config_widget = gimp_color_display_configure (display);
589

590 591 592 593 594 595 596 597 598 599
      if (! editor->config_widget)
        {
          editor->config_widget =
            gimp_prop_gui_new (G_OBJECT (display),
                               G_TYPE_FROM_INSTANCE (display), 0,
                               NULL,
                               gimp_get_user_context (editor->gimp),
                               NULL, NULL, NULL);
        }

Sven Neumann's avatar
Sven Neumann committed
600 601
      gtk_frame_set_label (GTK_FRAME (editor->config_frame),
                           GIMP_COLOR_DISPLAY_GET_CLASS (display)->name);
602 603 604
    }
  else
    {
Sven Neumann's avatar
Sven Neumann committed
605
      editor->config_widget = NULL;
606

607
      gtk_frame_set_label (GTK_FRAME (editor->config_frame),
Sven Neumann's avatar
Sven Neumann committed
608
                           _("No filter selected"));
609 610
    }

611
  if (editor->config_widget)
612
    {
613
      gtk_box_pack_start (GTK_BOX (editor->config_box), editor->config_widget,
614
                          FALSE, FALSE, 0);
615
      gtk_widget_show (editor->config_widget);
616

617 618
      g_object_add_weak_pointer (G_OBJECT (editor->config_widget),
                                 (gpointer) &editor->config_widget);
619
    }
620

621
  gimp_color_display_editor_update_buttons (editor);
Manish Singh's avatar
Manish Singh committed
622
}
623 624

static void
625 626 627 628
gimp_color_display_editor_added (GimpColorDisplayStack  *stack,
                                 GimpColorDisplay       *display,
                                 gint                    position,
                                 GimpColorDisplayEditor *editor)
629
{
630
  GtkTreeIter  iter;
631
  gboolean     enabled;
632
  const gchar *name;
633
  const gchar *icon_name;
634

635 636
  enabled = gimp_color_display_get_enabled (display);

637
  name      = GIMP_COLOR_DISPLAY_GET_CLASS (display)->name;
638
  icon_name = GIMP_COLOR_DISPLAY_GET_CLASS (display)->icon_name;
639 640 641 642

  gtk_list_store_insert (editor->dest, &iter, position);

  gtk_list_store_set (editor->dest, &iter,
643
                      DEST_COLUMN_ENABLED, enabled,
644
                      DEST_COLUMN_ICON,    icon_name,
645 646
                      DEST_COLUMN_NAME,    name,
                      DEST_COLUMN_FILTER,  display,
647 648
                      -1);

649
  g_signal_connect_object (display, "notify::enabled",
650
                           G_CALLBACK (gimp_color_display_editor_enabled),
651 652
                           G_OBJECT (editor), 0);

653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
  gimp_color_display_editor_update_buttons (editor);
}

static void
gimp_color_display_editor_removed (GimpColorDisplayStack  *stack,
                                   GimpColorDisplay       *display,
                                   GimpColorDisplayEditor *editor)
{
  GtkTreeIter iter;
  gboolean    iter_valid;

  for (iter_valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (editor->dest),
                                                   &iter);
       iter_valid;
       iter_valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (editor->dest),
                                              &iter))
    {
      GimpColorDisplay *display2;

      gtk_tree_model_get (GTK_TREE_MODEL (editor->dest), &iter,
                          DEST_COLUMN_FILTER, &display2,
                          -1);

      g_object_unref (display2);

      if (display == display2)
        {
680
          g_signal_handlers_disconnect_by_func (display,
681
                                                gimp_color_display_editor_enabled,
682 683
                                                editor);

684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
          gtk_list_store_remove (editor->dest, &iter);

          gimp_color_display_editor_update_buttons (editor);
          break;
        }
    }
}

static void
gimp_color_display_editor_reordered (GimpColorDisplayStack  *stack,
                                     GimpColorDisplay       *display,
                                     gint                    position,
                                     GimpColorDisplayEditor *editor)
{
  GtkTreeIter iter;
  gboolean    iter_valid;

  for (iter_valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (editor->dest),
                                                   &iter);
       iter_valid;
       iter_valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (editor->dest),
                                              &iter))
    {
      GimpColorDisplay *display2;

      gtk_tree_model_get (GTK_TREE_MODEL (editor->dest), &iter,
                          DEST_COLUMN_FILTER, &display2,
                          -1);

      g_object_unref (display2);

      if (display == display2)
        {
717
          GList       *filters = gimp_color_display_stack_get_filters (stack);
718 719 720 721 722 723 724 725 726 727
          GtkTreePath *path;
          gint         old_position;

          path = gtk_tree_model_get_path (GTK_TREE_MODEL (editor->dest), &iter);
          old_position = gtk_tree_path_get_indices (path)[0];
          gtk_tree_path_free (path);

          if (position == old_position)
            return;

728
          if (position == -1 || position == g_list_length (filters) - 1)
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
            {
              gtk_list_store_move_before (editor->dest, &iter, NULL);
            }
          else if (position == 0)
            {
              gtk_list_store_move_after (editor->dest, &iter, NULL);
            }
          else
            {
              GtkTreeIter place_iter;

              path = gtk_tree_path_new_from_indices (position, -1);
              gtk_tree_model_get_iter (GTK_TREE_MODEL (editor->dest),
                                       &place_iter, path);
              gtk_tree_path_free (path);

              if (position > old_position)
                gtk_list_store_move_after (editor->dest, &iter, &place_iter);
              else
                gtk_list_store_move_before (editor->dest, &iter, &place_iter);
            }

          gimp_color_display_editor_update_buttons (editor);

          return;
        }
    }
}

758
static void
759
gimp_color_display_editor_enabled (GimpColorDisplay       *display,
760
                                   GParamSpec             *pspec,
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
                                   GimpColorDisplayEditor *editor)
{
  GtkTreeIter iter;
  gboolean    iter_valid;

  for (iter_valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (editor->dest),
                                                   &iter);
       iter_valid;
       iter_valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (editor->dest),
                                              &iter))
    {
      GimpColorDisplay *display2;

      gtk_tree_model_get (GTK_TREE_MODEL (editor->dest), &iter,
                          DEST_COLUMN_FILTER,  &display2,
                          -1);

      g_object_unref (display2);

      if (display == display2)
        {
782
          gboolean enabled = gimp_color_display_get_enabled (display);
783 784 785 786

          gtk_list_store_set (editor->dest, &iter,
                              DEST_COLUMN_ENABLED, enabled,
                              -1);
787 788 789 790 791 792 793 794 795 796 797

          break;
        }
    }
}

static void
gimp_color_display_editor_enable_toggled (GtkCellRendererToggle  *toggle,
                                          const gchar            *path_str,
                                          GimpColorDisplayEditor *editor)
{
798
  GtkTreePath *path = gtk_tree_path_new_from_string (path_str);
799 800 801 802 803 804 805 806 807 808 809 810 811
  GtkTreeIter  iter;

  if (gtk_tree_model_get_iter (GTK_TREE_MODEL (editor->dest), &iter, path))
    {
      GimpColorDisplay *display;
      gboolean          enabled;

      gtk_tree_model_get (GTK_TREE_MODEL (editor->dest), &iter,
                          DEST_COLUMN_FILTER,  &display,
                          DEST_COLUMN_ENABLED, &enabled,
                          -1);

      gimp_color_display_set_enabled (display, ! enabled);
812 813

      g_object_unref (display);
814 815 816 817 818
    }

  gtk_tree_path_free (path);
}

819 820 821 822 823 824 825 826 827 828
static void
gimp_color_display_editor_update_buttons (GimpColorDisplayEditor *editor)
{
  GtkTreeModel *model;
  GtkTreeIter   iter;
  gboolean      up_sensitive   = FALSE;
  gboolean      down_sensitive = FALSE;

  if (gtk_tree_selection_get_selected (editor->dest_sel, &model, &iter))
    {
829
      GList       *filters = gimp_color_display_stack_get_filters (editor->stack);
830 831
      GtkTreePath *path    = gtk_tree_model_get_path (model, &iter);
      gint        *indices = gtk_tree_path_get_indices (path);
832 833

      up_sensitive   = indices[0] > 0;
834
      down_sensitive = indices[0] < (g_list_length (filters) - 1);
835 836 837 838 839 840

      gtk_tree_path_free (path);
    }

  gtk_widget_set_sensitive (editor->up_button,   up_sensitive);
  gtk_widget_set_sensitive (editor->down_button, down_sensitive);
841
}