summaryrefslogtreecommitdiffstats
path: root/app/widgets/gimptooleditor.c
blob: eecc67646c828c8e2eb72c7983c772dec330d1fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
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
680
681
682
683
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
717
718
719
720
721
722
723
724
725
726
727
728
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
758
759
760
761
762
763
764
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
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimptooleditor.c
 * Copyright (C) 2001-2009 Michael Natterer <mitch@gimp.org>
 *                         Stephen Griffiths <scgmk5@gmail.com>
 *
 * 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 3 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, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <gegl.h>
#include <gtk/gtk.h>

#include "libgimpconfig/gimpconfig.h"
#include "libgimpwidgets/gimpwidgets.h"

#include "widgets-types.h"

#include "core/gimp.h"
#include "core/gimpcontainer.h"
#include "core/gimpcontext.h"
#include "core/gimptoolgroup.h"
#include "core/gimptreehandler.h"

#include "tools/gimp-tools.h"

#include "gimpcontainertreestore.h"
#include "gimpcontainerview.h"
#include "gimpdnd.h"
#include "gimpviewrenderer.h"
#include "gimptooleditor.h"
#include "gimphelp-ids.h"
#include "gimpwidgets-utils.h"

#include "gimp-intl.h"


struct _GimpToolEditorPrivate
{
  GimpContainer   *container;
  GimpContext     *context;

  GtkWidget       *scrolled;

  GtkWidget       *new_group_button;
  GtkWidget       *raise_button;
  GtkWidget       *lower_button;
  GtkWidget       *delete_button;
  GtkWidget       *reset_button;

  GimpTreeHandler *tool_item_notify_handler;

  /* State of tools at creation of the editor, stored to support
   * reverting changes
   */
  gchar           *initial_tool_state;
};


/*  local function prototypes  */

static void            gimp_tool_editor_view_iface_init           (GimpContainerViewInterface *iface);

static void            gimp_tool_editor_constructed               (GObject                    *object);

static gboolean        gimp_tool_editor_select_item               (GimpContainerView          *view,
                                                                   GimpViewable               *viewable,
                                                                   gpointer                    insert_data);
static void            gimp_tool_editor_set_container             (GimpContainerView          *container_view,
                                                                   GimpContainer              *container);
static void            gimp_tool_editor_set_context               (GimpContainerView          *container_view,
                                                                   GimpContext                *context);

static gboolean        gimp_tool_editor_drop_possible             (GimpContainerTreeView      *tree_view,
                                                                   GimpDndType                 src_type,
                                                                   GimpViewable               *src_viewable,
                                                                   GimpViewable               *dest_viewable,
                                                                   GtkTreePath                *drop_path,
                                                                   GtkTreeViewDropPosition     drop_pos,
                                                                   GtkTreeViewDropPosition    *return_drop_pos,
                                                                   GdkDragAction              *return_drag_action);
static void            gimp_tool_editor_drop_viewable             (GimpContainerTreeView      *tree_view,
                                                                   GimpViewable               *src_viewable,
                                                                   GimpViewable               *dest_viewable,
                                                                   GtkTreeViewDropPosition     drop_pos);

static void            gimp_tool_editor_tool_item_notify         (GimpToolItem                *tool_item,
                                                                  GParamSpec                  *pspec,
                                                                  GimpToolEditor              *tool_editor);

static void            gimp_tool_editor_eye_data_func            (GtkTreeViewColumn           *tree_column,
                                                                  GtkCellRenderer             *cell,
                                                                  GtkTreeModel                *tree_model,
                                                                  GtkTreeIter                 *iter,
                                                                  gpointer                     data);
static void            gimp_tool_editor_eye_clicked              (GtkCellRendererToggle       *toggle,
                                                                  gchar                       *path_str,
                                                                  GdkModifierType              state,
                                                                  GimpToolEditor              *tool_editor);

static void            gimp_tool_editor_new_group_clicked        (GtkButton                   *button,
                                                                  GimpToolEditor              *tool_editor);
static void            gimp_tool_editor_raise_clicked            (GtkButton                   *button,
                                                                  GimpToolEditor              *tool_editor);
static void            gimp_tool_editor_raise_extend_clicked     (GtkButton                   *button,
                                                                  GdkModifierType              mask,
                                                                  GimpToolEditor              *tool_editor);
static void            gimp_tool_editor_lower_clicked            (GtkButton                   *button,
                                                                  GimpToolEditor              *tool_editor);
static void            gimp_tool_editor_lower_extend_clicked     (GtkButton                   *button,
                                                                  GdkModifierType              mask,
                                                                  GimpToolEditor              *tool_editor);
static void            gimp_tool_editor_delete_clicked           (GtkButton                   *button,
                                                                  GimpToolEditor              *tool_editor);
static void            gimp_tool_editor_reset_clicked            (GtkButton                   *button,
                                                                  GimpToolEditor              *tool_editor);

static GimpToolItem  * gimp_tool_editor_get_selected_tool_item   (GimpToolEditor              *tool_editor);
static GimpContainer * gimp_tool_editor_get_tool_item_container  (GimpToolEditor              *tool_editor,
                                                                  GimpToolItem                *tool_item);

static void            gimp_tool_editor_update_container         (GimpToolEditor              *tool_editor);
static void            gimp_tool_editor_update_sensitivity       (GimpToolEditor              *tool_editor);


G_DEFINE_TYPE_WITH_CODE (GimpToolEditor, gimp_tool_editor,
                         GIMP_TYPE_CONTAINER_TREE_VIEW,
                         G_ADD_PRIVATE (GimpToolEditor)
                         G_IMPLEMENT_INTERFACE (GIMP_TYPE_CONTAINER_VIEW,
                                                gimp_tool_editor_view_iface_init))

#define parent_class gimp_tool_editor_parent_class

static GimpContainerViewInterface *parent_view_iface = NULL;


/*  private functions  */

static void
gimp_tool_editor_class_init (GimpToolEditorClass *klass)
{
  GObjectClass               *object_class    = G_OBJECT_CLASS (klass);
  GimpContainerTreeViewClass *tree_view_class = GIMP_CONTAINER_TREE_VIEW_CLASS (klass);

  object_class->constructed      = gimp_tool_editor_constructed;

  tree_view_class->drop_possible = gimp_tool_editor_drop_possible;
  tree_view_class->drop_viewable = gimp_tool_editor_drop_viewable;
}

static void
gimp_tool_editor_view_iface_init (GimpContainerViewInterface *iface)
{
  parent_view_iface = g_type_interface_peek_parent (iface);

  if (! parent_view_iface)
    parent_view_iface = g_type_default_interface_peek (GIMP_TYPE_CONTAINER_VIEW);

  iface->select_item   = gimp_tool_editor_select_item;
  iface->set_container = gimp_tool_editor_set_container;
  iface->set_context   = gimp_tool_editor_set_context;
}

static void
gimp_tool_editor_init (GimpToolEditor *tool_editor)
{
  tool_editor->priv = gimp_tool_editor_get_instance_private (tool_editor);
}

static void
gimp_tool_editor_constructed (GObject *object)
{
  GimpToolEditor        *tool_editor    = GIMP_TOOL_EDITOR (object);
  GimpContainerTreeView *tree_view      = GIMP_CONTAINER_TREE_VIEW (object);
  GimpContainerView     *container_view = GIMP_CONTAINER_VIEW (object);
  gint                   view_size;
  gint                   border_width;

  G_OBJECT_CLASS (parent_class)->constructed (object);

  view_size = gimp_container_view_get_view_size (container_view,
                                                 &border_width);

  gimp_editor_set_show_name (GIMP_EDITOR (tool_editor), FALSE);

  gtk_tree_view_set_level_indentation (tree_view->view,
                                       0.8 * (view_size + 2 * border_width));

  gimp_dnd_viewable_dest_add (GTK_WIDGET (tree_view->view),
                              GIMP_TYPE_TOOL_ITEM,
                              NULL, NULL);

  /* construct tree view */
  {
    GtkTreeViewColumn *column;
    GtkCellRenderer   *eye_cell;
    GtkStyle          *tree_style;
    GtkIconSize        icon_size;

    tree_style = gtk_widget_get_style (GTK_WIDGET (tool_editor));

    icon_size = gimp_get_icon_size (GTK_WIDGET (tool_editor),
                                    GIMP_ICON_VISIBLE,
                                    GTK_ICON_SIZE_BUTTON,
                                    view_size -
                                    2 * tree_style->xthickness,
                                    view_size -
                                    2 * tree_style->ythickness);

    column = gtk_tree_view_column_new ();
    gtk_tree_view_insert_column (tree_view->view, column, 0);

    eye_cell = gimp_cell_renderer_toggle_new (GIMP_ICON_VISIBLE);
    g_object_set (eye_cell, "stock-size", icon_size, NULL);
    gtk_tree_view_column_pack_start (column, eye_cell, FALSE);
    gtk_tree_view_column_set_cell_data_func  (column, eye_cell,
                                              gimp_tool_editor_eye_data_func,
                                              tree_view, NULL);

    gimp_container_tree_view_add_toggle_cell (tree_view, eye_cell);

    g_signal_connect (eye_cell, "clicked",
                      G_CALLBACK (gimp_tool_editor_eye_clicked),
                      tool_editor);
  }

  /* buttons */
  tool_editor->priv->new_group_button =
    gimp_editor_add_button (GIMP_EDITOR (tool_editor), GIMP_ICON_FOLDER_NEW,
                            _("Create a new tool group"), NULL,
                            G_CALLBACK (gimp_tool_editor_new_group_clicked),
                            NULL,
                            G_OBJECT (tool_editor));

  tool_editor->priv->raise_button =
    gimp_editor_add_button (GIMP_EDITOR (tool_editor), GIMP_ICON_GO_UP,
                            _("Raise this item"),
                            _("Raise this item to the top"),
                            G_CALLBACK (gimp_tool_editor_raise_clicked),
                            G_CALLBACK (gimp_tool_editor_raise_extend_clicked),
                            G_OBJECT (tool_editor));

  tool_editor->priv->lower_button =
    gimp_editor_add_button (GIMP_EDITOR (tool_editor), GIMP_ICON_GO_DOWN,
                            _("Lower this item"),
                            _("Lower this item to the bottom"),
                            G_CALLBACK (gimp_tool_editor_lower_clicked),
                            G_CALLBACK (gimp_tool_editor_lower_extend_clicked),
                            G_OBJECT (tool_editor));

  tool_editor->priv->delete_button =
    gimp_editor_add_button (GIMP_EDITOR (tool_editor), GIMP_ICON_EDIT_DELETE,
                            _("Delete this tool group"), NULL,
                            G_CALLBACK (gimp_tool_editor_delete_clicked),
                            NULL,
                            G_OBJECT (tool_editor));

  tool_editor->priv->reset_button =
    gimp_editor_add_button (GIMP_EDITOR (tool_editor), GIMP_ICON_RESET,
                            _("Reset tool order and visibility"), NULL,
                            G_CALLBACK (gimp_tool_editor_reset_clicked),
                            NULL,
                            G_OBJECT (tool_editor));

  gimp_tool_editor_update_sensitivity (tool_editor);
}

static gboolean
gimp_tool_editor_select_item (GimpContainerView *container_view,
                              GimpViewable      *viewable,
                              gpointer           insert_data)
{
  GimpToolEditor *tool_editor = GIMP_TOOL_EDITOR (container_view);
  gboolean        result;

  result = parent_view_iface->select_item (container_view,
                                           viewable, insert_data);

  gimp_tool_editor_update_sensitivity (tool_editor);

  return result;
}

static void
gimp_tool_editor_set_container (GimpContainerView *container_view,
                                GimpContainer     *container)
{
  GimpToolEditor *tool_editor = GIMP_TOOL_EDITOR (container_view);

  parent_view_iface->set_container (container_view, container);

  gimp_tool_editor_update_container (tool_editor);
}

static void
gimp_tool_editor_set_context (GimpContainerView *container_view,
                              GimpContext       *context)
{
  GimpToolEditor *tool_editor = GIMP_TOOL_EDITOR (container_view);

  parent_view_iface->set_context (container_view, context);

  gimp_tool_editor_update_container (tool_editor);
}

static gboolean
gimp_tool_editor_drop_possible (GimpContainerTreeView   *tree_view,
                                GimpDndType              src_type,
                                GimpViewable            *src_viewable,
                                GimpViewable            *dest_viewable,
                                GtkTreePath             *drop_path,
                                GtkTreeViewDropPosition  drop_pos,
                                GtkTreeViewDropPosition *return_drop_pos,
                                GdkDragAction           *return_drag_action)
{
  if (GIMP_CONTAINER_TREE_VIEW_CLASS (parent_class)->drop_possible (
        tree_view,
        src_type, src_viewable, dest_viewable, drop_path, drop_pos,
        return_drop_pos, return_drag_action))
    {
      if (gimp_viewable_get_parent (dest_viewable)       ||
          (gimp_viewable_get_children (dest_viewable)    &&
           (drop_pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER ||
            drop_pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)))
        {
          return ! gimp_viewable_get_children (src_viewable);
        }

      return TRUE;
    }

  return FALSE;
}

static void
gimp_tool_editor_drop_viewable (GimpContainerTreeView   *tree_view,
                                GimpViewable            *src_viewable,
                                GimpViewable            *dest_viewable,
                                GtkTreeViewDropPosition  drop_pos)
{
  GimpContainerView *container_view = GIMP_CONTAINER_VIEW (tree_view);

  GIMP_CONTAINER_TREE_VIEW_CLASS (parent_class)->drop_viewable (tree_view,
                                                                src_viewable,
                                                                dest_viewable,
                                                                drop_pos);

  gimp_container_view_select_item (container_view, src_viewable);
}

static void
gimp_tool_editor_new_group_clicked (GtkButton      *button,
                                    GimpToolEditor *tool_editor)
{
  GimpContainerView *container_view = GIMP_CONTAINER_VIEW (tool_editor);
  GimpContainer     *container;
  GimpToolItem      *tool_item;
  GimpToolGroup     *group;
  gint               index = 0;

  tool_item = gimp_tool_editor_get_selected_tool_item (tool_editor);

  if (tool_item)
    {
      if (gimp_viewable_get_parent (GIMP_VIEWABLE (tool_item)) != NULL)
        return;

      container = gimp_tool_editor_get_tool_item_container (tool_editor,
                                                            tool_item);

      index = gimp_container_get_child_index (container,
                                              GIMP_OBJECT (tool_item));
    }
  else
    {
      container = tool_editor->priv->container;
    }

  if (container)
    {
      group = gimp_tool_group_new ();

      gimp_container_insert (container, GIMP_OBJECT (group), index);

      g_object_unref (group);

      gimp_container_view_select_item (container_view, GIMP_VIEWABLE (group));
    }
}

static void
gimp_tool_editor_raise_clicked (GtkButton      *button,
                                GimpToolEditor *tool_editor)
{
  GimpToolItem *tool_item;

  tool_item = gimp_tool_editor_get_selected_tool_item (tool_editor);

  if (tool_item)
    {
      GimpContainer *container;
      gint           index;

      container = gimp_tool_editor_get_tool_item_container (tool_editor,
                                                            tool_item);

      index = gimp_container_get_child_index (container,
                                              GIMP_OBJECT (tool_item));

      if (index > 0)
        {
          gimp_container_reorder (container,
                                  GIMP_OBJECT (tool_item), index - 1);
        }
    }
}

static void
gimp_tool_editor_raise_extend_clicked (GtkButton       *button,
                                       GdkModifierType  mask,
                                       GimpToolEditor  *tool_editor)
{
  GimpToolItem *tool_item;

  tool_item = gimp_tool_editor_get_selected_tool_item (tool_editor);

  if (tool_item && (mask & GDK_SHIFT_MASK))
    {
      GimpContainer *container;
      gint           index;

      container = gimp_tool_editor_get_tool_item_container (tool_editor,
                                                            tool_item);

      index = gimp_container_get_child_index (container,
                                              GIMP_OBJECT (tool_item));

      if (index > 0)
        {
          gimp_container_reorder (container,
                                  GIMP_OBJECT (tool_item), 0);
        }
    }
}

static void
gimp_tool_editor_lower_clicked (GtkButton      *button,
                                GimpToolEditor *tool_editor)
{
  GimpToolItem *tool_item;

  tool_item = gimp_tool_editor_get_selected_tool_item (tool_editor);

  if (tool_item)
    {
      GimpContainer *container;
      gint           index;

      container = gimp_tool_editor_get_tool_item_container (tool_editor,
                                                            tool_item);

      index = gimp_container_get_child_index (container,
                                              GIMP_OBJECT (tool_item));

      if (index + 1 < gimp_container_get_n_children (container))
        {
          gimp_container_reorder (container,
                                  GIMP_OBJECT (tool_item), index + 1);
        }
    }
}

static void
gimp_tool_editor_lower_extend_clicked (GtkButton       *button,
                                       GdkModifierType  mask,
                                       GimpToolEditor  *tool_editor)
{
  GimpToolItem *tool_item;

  tool_item = gimp_tool_editor_get_selected_tool_item (tool_editor);

  if (tool_item && (mask & GDK_SHIFT_MASK))
    {
      GimpContainer *container;
      gint           index;

      container = gimp_tool_editor_get_tool_item_container (tool_editor,
                                                            tool_item);

      index = gimp_container_get_n_children (container) - 1;
      index = MAX (index, 0);

      gimp_container_reorder (container,
                              GIMP_OBJECT (tool_item), index);
    }
}

static void
gimp_tool_editor_delete_clicked (GtkButton      *button,
                                 GimpToolEditor *tool_editor)
{
  GimpContainerView *container_view = GIMP_CONTAINER_VIEW (tool_editor);
  GimpToolItem     *tool_item;

  tool_item = gimp_tool_editor_get_selected_tool_item (tool_editor);

  if (tool_item)
    {
      GimpContainer *src_container;
      GimpContainer *dest_container;
      gint           index;
      gint           dest_index;

      src_container  = gimp_viewable_get_children (GIMP_VIEWABLE (tool_item));
      dest_container = gimp_tool_editor_get_tool_item_container (tool_editor,
                                                                 tool_item);

      if (! src_container)
        return;

      index      = gimp_container_get_child_index (dest_container,
                                                   GIMP_OBJECT (tool_item));
      dest_index = index;

      g_object_ref (tool_item);

      gimp_container_freeze (src_container);
      gimp_container_freeze (dest_container);

      gimp_container_remove (dest_container, GIMP_OBJECT (tool_item));

      while (! gimp_container_is_empty (src_container))
        {
          GimpObject *object = gimp_container_get_first_child (src_container);

          g_object_ref (object);

          gimp_container_remove (src_container,  object);
          gimp_container_insert (dest_container, object, dest_index++);

          g_object_unref (object);
        }

      gimp_container_thaw (dest_container);
      gimp_container_thaw (src_container);

      gimp_container_view_select_item (
        container_view,
        GIMP_VIEWABLE (gimp_container_get_child_by_index (dest_container,
                                                          index)));

      g_object_unref (tool_item);
    }
}

static void
gimp_tool_editor_reset_clicked (GtkButton      *button,
                                GimpToolEditor *tool_editor)
{
  gimp_tools_reset (tool_editor->priv->context->gimp,
                    tool_editor->priv->container,
                    FALSE);
}

static void
gimp_tool_editor_tool_item_notify (GimpToolItem   *tool_item,
                                   GParamSpec     *pspec,
                                   GimpToolEditor *tool_editor)
{
  GimpContainerTreeView *tree_view      = GIMP_CONTAINER_TREE_VIEW (tool_editor);
  GimpContainerView     *container_view = GIMP_CONTAINER_VIEW (tool_editor);
  GtkTreeIter           *iter;

  iter = gimp_container_view_lookup (container_view,
                                     GIMP_VIEWABLE (tool_item));

  if (iter)
    {
      GtkTreePath *path;

      path = gtk_tree_model_get_path (tree_view->model, iter);

      gtk_tree_model_row_changed (tree_view->model, path, iter);

      gtk_tree_path_free (path);
    }
}

static void
gimp_tool_editor_eye_data_func (GtkTreeViewColumn *tree_column,
                                GtkCellRenderer   *cell,
                                GtkTreeModel      *tree_model,
                                GtkTreeIter       *iter,
                                gpointer           data)
{
  GimpViewRenderer *renderer;
  GimpToolItem     *tool_item;

  gtk_tree_model_get (tree_model, iter,
                      GIMP_CONTAINER_TREE_STORE_COLUMN_RENDERER, &renderer,
                      -1);

  tool_item = GIMP_TOOL_ITEM (renderer->viewable);

  g_object_set (cell,
                "active",       gimp_tool_item_get_visible (tool_item),
                "inconsistent", gimp_tool_item_get_visible (tool_item) &&
                                ! gimp_tool_item_get_shown (tool_item),
                NULL);

  g_object_unref (renderer);
}

static void
gimp_tool_editor_eye_clicked (GtkCellRendererToggle *toggle,
                              gchar                 *path_str,
                              GdkModifierType        state,
                              GimpToolEditor        *tool_editor)
{
  GimpContainerTreeView *tree_view = GIMP_CONTAINER_TREE_VIEW (tool_editor);
  GtkTreePath           *path;
  GtkTreeIter            iter;

  path = gtk_tree_path_new_from_string (path_str);

  if (gtk_tree_model_get_iter (tree_view->model, &iter, path))
    {
      GimpViewRenderer *renderer;
      GimpToolItem     *tool_item;
      gboolean          active;

      gtk_tree_model_get (tree_view->model, &iter,
                          GIMP_CONTAINER_TREE_STORE_COLUMN_RENDERER, &renderer,
                          -1);

      tool_item = GIMP_TOOL_ITEM (renderer->viewable);

      g_object_get (toggle,
                    "active", &active,
                    NULL);

      gimp_tool_item_set_visible (tool_item, ! active);

      g_object_unref (renderer);
    }

  gtk_tree_path_free (path);
}

static GimpToolItem *
gimp_tool_editor_get_selected_tool_item (GimpToolEditor *tool_editor)
{
  GimpContainerTreeView *tree_view = GIMP_CONTAINER_TREE_VIEW (tool_editor);

  if (tool_editor->priv->container)
    {
      GimpViewRenderer *renderer;
      GimpToolItem     *tool_item;
      GtkTreeSelection *selection;
      GtkTreeModel     *model;
      GtkTreeIter       iter;

      selection = gtk_tree_view_get_selection (tree_view->view);

      if (! gtk_tree_selection_get_selected (selection, &model, &iter))
        return NULL;

      gtk_tree_model_get (model, &iter,
                          GIMP_CONTAINER_TREE_STORE_COLUMN_RENDERER, &renderer,
                          -1);

      tool_item = GIMP_TOOL_ITEM (renderer->viewable);

      g_object_unref (renderer);

      return tool_item;
    }

  return NULL;
}

static GimpContainer *
gimp_tool_editor_get_tool_item_container (GimpToolEditor *tool_editor,
                                          GimpToolItem   *tool_item)
{
  GimpViewable *parent;

  parent = gimp_viewable_get_parent (GIMP_VIEWABLE (tool_item));

  if (parent)
    {
      return gimp_viewable_get_children (parent);
    }
  else
    {
      return tool_editor->priv->container;
    }
}

static void
gimp_tool_editor_update_container (GimpToolEditor *tool_editor)
{
  GimpContainerView *container_view = GIMP_CONTAINER_VIEW (tool_editor);
  GimpContainer     *container;
  GimpContext       *context;

  g_clear_pointer (&tool_editor->priv->tool_item_notify_handler,
                   gimp_tree_handler_disconnect);

  g_clear_pointer (&tool_editor->priv->initial_tool_state, g_free);

  container = gimp_container_view_get_container (container_view);
  context   = gimp_container_view_get_context   (container_view);

  if (container && context)
    {
      GString          *string;
      GimpConfigWriter *writer;

      tool_editor->priv->container = container;
      tool_editor->priv->context   = context;

      tool_editor->priv->tool_item_notify_handler = gimp_tree_handler_connect (
        container, "notify",
        G_CALLBACK (gimp_tool_editor_tool_item_notify),
        tool_editor);

      /* save initial tool order */
      string = g_string_new (NULL);

      writer = gimp_config_writer_new_string (string);

      gimp_tools_serialize (context->gimp, container, writer);

      gimp_config_writer_finish (writer, NULL, NULL);

      tool_editor->priv->initial_tool_state = g_string_free (string, FALSE);
    }
}

static void
gimp_tool_editor_update_sensitivity (GimpToolEditor *tool_editor)
{
  GimpToolItem *tool_item;

  tool_item = gimp_tool_editor_get_selected_tool_item (tool_editor);

  if (tool_item)
    {
      GimpContainer *container;
      gint           index;

      container = gimp_tool_editor_get_tool_item_container (tool_editor,
                                                            tool_item);

      index = gimp_container_get_child_index (container,
                                              GIMP_OBJECT (tool_item));

      gtk_widget_set_sensitive (
        tool_editor->priv->new_group_button,
        gimp_viewable_get_parent (GIMP_VIEWABLE (tool_item)) == NULL);

      gtk_widget_set_sensitive (
        tool_editor->priv->raise_button,
        index > 0);

      gtk_widget_set_sensitive (
        tool_editor->priv->lower_button,
        index < gimp_container_get_n_children (container) - 1);

      gtk_widget_set_sensitive (
        tool_editor->priv->delete_button,
        gimp_viewable_get_children (GIMP_VIEWABLE (tool_item)) != NULL);
    }
  else
    {
      gtk_widget_set_sensitive (tool_editor->priv->new_group_button, TRUE);
      gtk_widget_set_sensitive (tool_editor->priv->raise_button,     FALSE);
      gtk_widget_set_sensitive (tool_editor->priv->lower_button,     FALSE);
      gtk_widget_set_sensitive (tool_editor->priv->delete_button,    FALSE);
    }
}


/*  public functions  */

GtkWidget *
gimp_tool_editor_new (GimpContainer *container,
                      GimpContext   *context,
                      gint           view_size,
                      gint           view_border_width)
{
  GimpContainerView *container_view;

  g_return_val_if_fail (GIMP_IS_CONTAINER (container), NULL);
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);

  container_view = g_object_new (GIMP_TYPE_TOOL_EDITOR,
                                 "view-size",         view_size,
                                 "view-border-width", view_border_width,
                                 NULL);

  gimp_container_view_set_context     (container_view, context);
  gimp_container_view_set_container   (container_view, container);
  gimp_container_view_set_reorderable (container_view, TRUE);

  return GTK_WIDGET (container_view);
}

/**
 * gimp_tool_editor_revert_changes:
 * @tool_editor:
 *
 * Reverts the tool order and visibility to the state at creation.
 **/
void
gimp_tool_editor_revert_changes (GimpToolEditor *tool_editor)
{
  GScanner *scanner;

  g_return_if_fail (GIMP_IS_TOOL_EDITOR (tool_editor));

  scanner = gimp_scanner_new_string (tool_editor->priv->initial_tool_state, -1,
                                     NULL);

  gimp_tools_deserialize (tool_editor->priv->context->gimp,
                          tool_editor->priv->container,
                          scanner);

  gimp_scanner_destroy (scanner);
}