1
/* GIMP - The GNU Image Manipulation Program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
4
* gimpcontainertreestore.c
5
* Copyright (C) 2010 Michael Natterer <mitch@gimp.org>
7
* This program is free software: you can redistribute it and/or modify
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
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program. If not, see <http://www.gnu.org/licenses/>.
27
#include "widgets-types.h"
29
#include "core/gimpcontainer.h"
30
#include "core/gimpviewable.h"
32
#include "gimpcellrendererviewable.h"
33
#include "gimpcontainertreestore.h"
34
#include "gimpcontainerview.h"
35
#include "gimpviewrenderer.h"
46
typedef struct _GimpContainerTreeStorePrivate GimpContainerTreeStorePrivate;
48
struct _GimpContainerTreeStorePrivate
50
GimpContainerView *container_view;
51
GList *renderer_cells;
55
#define GET_PRIVATE(store) \
56
G_TYPE_INSTANCE_GET_PRIVATE (store, \
57
GIMP_TYPE_CONTAINER_TREE_STORE, \
58
GimpContainerTreeStorePrivate)
61
static void gimp_container_tree_store_constructed (GObject *object);
62
static void gimp_container_tree_store_finalize (GObject *object);
63
static void gimp_container_tree_store_set_property (GObject *object,
67
static void gimp_container_tree_store_get_property (GObject *object,
72
static void gimp_container_tree_store_set (GimpContainerTreeStore *store,
74
GimpViewable *viewable);
75
static void gimp_container_tree_store_renderer_update (GimpViewRenderer *renderer,
76
GimpContainerTreeStore *store);
79
G_DEFINE_TYPE (GimpContainerTreeStore, gimp_container_tree_store,
82
#define parent_class gimp_container_tree_store_parent_class
86
gimp_container_tree_store_class_init (GimpContainerTreeStoreClass *klass)
88
GObjectClass *object_class = G_OBJECT_CLASS (klass);
90
object_class->constructed = gimp_container_tree_store_constructed;
91
object_class->finalize = gimp_container_tree_store_finalize;
92
object_class->set_property = gimp_container_tree_store_set_property;
93
object_class->get_property = gimp_container_tree_store_get_property;
95
g_object_class_install_property (object_class, PROP_CONTAINER_VIEW,
96
g_param_spec_object ("container-view",
98
GIMP_TYPE_CONTAINER_VIEW,
99
GIMP_PARAM_READWRITE |
100
G_PARAM_CONSTRUCT_ONLY));
102
g_object_class_install_property (object_class, PROP_USE_NAME,
103
g_param_spec_boolean ("use-name",
106
GIMP_PARAM_READWRITE));
108
g_type_class_add_private (klass, sizeof (GimpContainerTreeStorePrivate));
112
gimp_container_tree_store_init (GimpContainerTreeStore *store)
117
gimp_container_tree_store_constructed (GObject *object)
119
if (G_OBJECT_CLASS (parent_class)->constructed)
120
G_OBJECT_CLASS (parent_class)->constructed (object);
124
gimp_container_tree_store_finalize (GObject *object)
126
GimpContainerTreeStorePrivate *private = GET_PRIVATE (object);
128
if (private->renderer_cells)
130
g_list_free (private->renderer_cells);
131
private->renderer_cells = NULL;
134
G_OBJECT_CLASS (parent_class)->finalize (object);
138
gimp_container_tree_store_set_property (GObject *object,
143
GimpContainerTreeStorePrivate *private = GET_PRIVATE (object);
147
case PROP_CONTAINER_VIEW:
148
private->container_view = g_value_get_object (value); /* don't ref */
151
private->use_name = g_value_get_boolean (value);
155
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
161
gimp_container_tree_store_get_property (GObject *object,
166
GimpContainerTreeStorePrivate *private = GET_PRIVATE (object);
170
case PROP_CONTAINER_VIEW:
171
g_value_set_object (value, private->container_view);
174
g_value_set_boolean (value, private->use_name);
178
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
184
/* public functions */
187
gimp_container_tree_store_new (GimpContainerView *container_view,
191
GimpContainerTreeStore *store;
193
g_return_val_if_fail (GIMP_IS_CONTAINER_VIEW (container_view), NULL);
194
g_return_val_if_fail (n_columns >= GIMP_CONTAINER_TREE_STORE_N_COLUMNS, NULL);
195
g_return_val_if_fail (types != NULL, NULL);
197
store = g_object_new (GIMP_TYPE_CONTAINER_TREE_STORE,
198
"container-view", container_view,
201
gtk_tree_store_set_column_types (GTK_TREE_STORE (store), n_columns, types);
203
return GTK_TREE_MODEL (store);
207
gimp_container_tree_store_add_renderer_cell (GimpContainerTreeStore *store,
208
GtkCellRenderer *cell)
210
GimpContainerTreeStorePrivate *private;
212
g_return_if_fail (GIMP_IS_CONTAINER_TREE_STORE (store));
213
g_return_if_fail (GIMP_IS_CELL_RENDERER_VIEWABLE (cell));
215
private = GET_PRIVATE (store);
217
private->renderer_cells = g_list_prepend (private->renderer_cells, cell);
221
gimp_container_tree_store_set_use_name (GimpContainerTreeStore *store,
224
GimpContainerTreeStorePrivate *private;
226
g_return_if_fail (GIMP_IS_CONTAINER_TREE_STORE (store));
228
private = GET_PRIVATE (store);
230
if (private->use_name != use_name)
232
private->use_name = use_name ? TRUE : FALSE;
233
g_object_notify (G_OBJECT (store), "use-name");
238
gimp_container_tree_store_get_use_name (GimpContainerTreeStore *store)
240
g_return_val_if_fail (GIMP_IS_CONTAINER_TREE_STORE (store), FALSE);
242
return GET_PRIVATE (store)->use_name;
246
gimp_container_tree_store_set_context_foreach (GtkTreeModel *model,
251
GimpContext *context = data;
252
GimpViewRenderer *renderer;
254
gtk_tree_model_get (model, iter,
255
GIMP_CONTAINER_TREE_STORE_COLUMN_RENDERER, &renderer,
258
gimp_view_renderer_set_context (renderer, context);
260
g_object_unref (renderer);
266
gimp_container_tree_store_set_context (GimpContainerTreeStore *store,
267
GimpContext *context)
269
g_return_if_fail (GIMP_IS_CONTAINER_TREE_STORE (store));
271
gtk_tree_model_foreach (GTK_TREE_MODEL (store),
272
gimp_container_tree_store_set_context_foreach,
277
gimp_container_tree_store_insert_item (GimpContainerTreeStore *store,
278
GimpViewable *viewable,
284
g_return_val_if_fail (GIMP_IS_CONTAINER_TREE_STORE (store), NULL);
287
gtk_tree_store_append (GTK_TREE_STORE (store), &iter, parent);
289
gtk_tree_store_insert (GTK_TREE_STORE (store), &iter, parent, index);
291
gimp_container_tree_store_set (store, &iter, viewable);
293
return gtk_tree_iter_copy (&iter);
297
gimp_container_tree_store_remove_item (GimpContainerTreeStore *store,
298
GimpViewable *viewable,
303
gtk_tree_store_remove (GTK_TREE_STORE (store), iter);
305
/* If the store is empty after this remove, clear out renderers
306
* from all cells so they don't keep refing the viewables
309
if (! gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL))
311
GimpContainerTreeStorePrivate *private = GET_PRIVATE (store);
314
for (list = private->renderer_cells; list; list = list->next)
315
g_object_set (list->data, "renderer", NULL, NULL);
321
gimp_container_tree_store_reorder_item (GimpContainerTreeStore *store,
322
GimpViewable *viewable,
326
GimpContainerTreeStorePrivate *private;
327
GimpViewable *parent;
328
GimpContainer *container;
330
g_return_if_fail (GIMP_IS_CONTAINER_TREE_STORE (store));
332
private = GET_PRIVATE (store);
337
parent = gimp_viewable_get_parent (viewable);
340
container = gimp_viewable_get_children (parent);
342
container = gimp_container_view_get_container (private->container_view);
344
if (new_index == -1 ||
345
new_index == gimp_container_get_n_children (container) - 1)
347
gtk_tree_store_move_before (GTK_TREE_STORE (store), iter, NULL);
349
else if (new_index == 0)
351
gtk_tree_store_move_after (GTK_TREE_STORE (store), iter, NULL);
356
GtkTreeIter place_iter;
361
path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), iter);
362
indices = gtk_tree_path_get_indices (path);
364
depth = gtk_tree_path_get_depth (path);
366
old_index = indices[depth - 1];
368
if (new_index != old_index)
370
indices[depth - 1] = new_index;
372
gtk_tree_model_get_iter (GTK_TREE_MODEL (store), &place_iter, path);
374
if (new_index > old_index)
375
gtk_tree_store_move_after (GTK_TREE_STORE (store),
378
gtk_tree_store_move_before (GTK_TREE_STORE (store),
382
gtk_tree_path_free (path);
387
gimp_container_tree_store_rename_item (GimpContainerTreeStore *store,
388
GimpViewable *viewable,
391
gboolean new_name_shorter = FALSE;
393
g_return_val_if_fail (GIMP_IS_CONTAINER_TREE_STORE (store), FALSE);
397
GimpContainerTreeStorePrivate *private = GET_PRIVATE (store);
401
if (private->use_name)
402
name = (gchar *) gimp_object_get_name (viewable);
404
name = gimp_viewable_get_description (viewable, NULL);
406
gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
407
GIMP_CONTAINER_TREE_STORE_COLUMN_NAME, &old_name,
410
gtk_tree_store_set (GTK_TREE_STORE (store), iter,
411
GIMP_CONTAINER_TREE_STORE_COLUMN_NAME, name,
414
if (name && old_name && strlen (name) < strlen (old_name))
415
new_name_shorter = TRUE;
417
if (! private->use_name)
423
return new_name_shorter;
427
gimp_container_tree_store_clear_items (GimpContainerTreeStore *store)
429
g_return_if_fail (GIMP_IS_CONTAINER_TREE_STORE (store));
431
gtk_tree_store_clear (GTK_TREE_STORE (store));
433
/* If the store is empty after this remove, clear out renderers
434
* from all cells so they don't keep refing the viewables
437
if (! gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL))
439
GimpContainerTreeStorePrivate *private = GET_PRIVATE (store);
442
for (list = private->renderer_cells; list; list = list->next)
443
g_object_set (list->data, "renderer", NULL, NULL);
451
} SetSizeForeachData;
454
gimp_container_tree_store_set_view_size_foreach (GtkTreeModel *model,
459
SetSizeForeachData *size_data = data;
460
GimpViewRenderer *renderer;
462
gtk_tree_model_get (model, iter,
463
GIMP_CONTAINER_TREE_STORE_COLUMN_RENDERER, &renderer,
466
gimp_view_renderer_set_size (renderer,
467
size_data->view_size,
468
size_data->border_width);
470
g_object_unref (renderer);
476
gimp_container_tree_store_set_view_size (GimpContainerTreeStore *store)
478
GimpContainerTreeStorePrivate *private;
479
SetSizeForeachData size_data;
481
g_return_if_fail (GIMP_IS_CONTAINER_TREE_STORE (store));
483
private = GET_PRIVATE (store);
485
size_data.view_size =
486
gimp_container_view_get_view_size (private->container_view,
487
&size_data.border_width);
489
gtk_tree_model_foreach (GTK_TREE_MODEL (store),
490
gimp_container_tree_store_set_view_size_foreach,
495
/* private functions */
498
gimp_container_tree_store_columns_init (GType *types,
501
g_return_if_fail (types != NULL);
502
g_return_if_fail (n_types != NULL);
503
g_return_if_fail (*n_types == 0);
505
g_assert (GIMP_CONTAINER_TREE_STORE_COLUMN_RENDERER ==
506
gimp_container_tree_store_columns_add (types, n_types,
507
GIMP_TYPE_VIEW_RENDERER));
509
g_assert (GIMP_CONTAINER_TREE_STORE_COLUMN_NAME ==
510
gimp_container_tree_store_columns_add (types, n_types,
513
g_assert (GIMP_CONTAINER_TREE_STORE_COLUMN_NAME_ATTRIBUTES ==
514
gimp_container_tree_store_columns_add (types, n_types,
515
PANGO_TYPE_ATTR_LIST));
517
g_assert (GIMP_CONTAINER_TREE_STORE_COLUMN_NAME_SENSITIVE ==
518
gimp_container_tree_store_columns_add (types, n_types,
521
g_assert (GIMP_CONTAINER_TREE_STORE_COLUMN_USER_DATA ==
522
gimp_container_tree_store_columns_add (types, n_types,
527
gimp_container_tree_store_columns_add (GType *types,
531
g_return_val_if_fail (types != NULL, 0);
532
g_return_val_if_fail (n_types != NULL, 0);
533
g_return_val_if_fail (*n_types >= 0, 0);
535
types[*n_types] = type;
542
gimp_container_tree_store_set (GimpContainerTreeStore *store,
544
GimpViewable *viewable)
546
GimpContainerTreeStorePrivate *private = GET_PRIVATE (store);
547
GimpContext *context;
548
GimpViewRenderer *renderer;
553
context = gimp_container_view_get_context (private->container_view);
555
view_size = gimp_container_view_get_view_size (private->container_view,
558
renderer = gimp_view_renderer_new (context,
559
G_TYPE_FROM_INSTANCE (viewable),
560
view_size, border_width,
562
gimp_view_renderer_set_viewable (renderer, viewable);
563
gimp_view_renderer_remove_idle (renderer);
565
g_signal_connect (renderer, "update",
566
G_CALLBACK (gimp_container_tree_store_renderer_update),
569
if (private->use_name)
570
name = (gchar *) gimp_object_get_name (viewable);
572
name = gimp_viewable_get_description (viewable, NULL);
574
gtk_tree_store_set (GTK_TREE_STORE (store), iter,
575
GIMP_CONTAINER_TREE_STORE_COLUMN_RENDERER, renderer,
576
GIMP_CONTAINER_TREE_STORE_COLUMN_NAME, name,
577
GIMP_CONTAINER_TREE_STORE_COLUMN_NAME_SENSITIVE, TRUE,
580
if (! private->use_name)
583
g_object_unref (renderer);
587
gimp_container_tree_store_renderer_update (GimpViewRenderer *renderer,
588
GimpContainerTreeStore *store)
590
GimpContainerTreeStorePrivate *private = GET_PRIVATE (store);
593
iter = gimp_container_view_lookup (private->container_view,
600
path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), iter);
601
gtk_tree_model_row_changed (GTK_TREE_MODEL (store), path, iter);
602
gtk_tree_path_free (path);