gimpcontainercombobox.c 16.3 KB
Newer Older
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
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimpcontainercombobox.c
 * Copyright (C) 2003 Michael Natterer <mitch@gimp.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <string.h>

#include <gtk/gtk.h>

#include "widgets-types.h"

#include "core/gimpcontainer.h"
#include "core/gimpcontext.h"
#include "core/gimpviewable.h"

#include "gimpcellrendererviewable.h"
#include "gimpcontainercombobox.h"
#include "gimpcontainerview.h"
37
#include "gimpviewrenderer.h"
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52


enum
{
  COLUMN_RENDERER,
  COLUMN_NAME,
  NUM_COLUMNS
};


static void     gimp_container_combo_box_class_init   (GimpContainerComboBoxClass *klass);
static void     gimp_container_combo_box_init         (GimpContainerComboBox  *view);

static void     gimp_container_combo_box_view_iface_init (GimpContainerViewInterface *view_iface);

53
54
static void     gimp_container_combo_box_unrealize    (GtkWidget              *widget);

55
static gpointer gimp_container_combo_box_insert_item  (GimpContainerView      *view,
56
57
                                                       GimpViewable           *viewable,
                                                       gint                    index);
58
static void     gimp_container_combo_box_remove_item  (GimpContainerView      *view,
59
60
                                                       GimpViewable           *viewable,
                                                       gpointer                insert_data);
61
static void     gimp_container_combo_box_reorder_item (GimpContainerView      *view,
62
63
64
                                                       GimpViewable           *viewable,
                                                       gint                    new_index,
                                                       gpointer                insert_data);
65
static void     gimp_container_combo_box_rename_item  (GimpContainerView      *view,
66
67
                                                       GimpViewable           *viewable,
                                                       gpointer                insert_data);
68
static gboolean  gimp_container_combo_box_select_item (GimpContainerView      *view,
69
70
                                                       GimpViewable           *viewable,
                                                       gpointer                insert_data);
71
72
73
static void     gimp_container_combo_box_clear_items  (GimpContainerView      *view);
static void gimp_container_combo_box_set_preview_size (GimpContainerView      *view);

74
75
static void     gimp_container_combo_box_changed      (GtkComboBox            *combo_box,
                                                       GimpContainerView      *view);
76
static void gimp_container_combo_box_renderer_update  (GimpViewRenderer       *renderer,
77
                                                       GimpContainerView      *view);
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


static GtkComboBoxClass           *parent_class      = NULL;
static GimpContainerViewInterface *parent_view_iface = NULL;


GType
gimp_container_combo_box_get_type (void)
{
  static GType view_type = 0;

  if (! view_type)
    {
      static const GTypeInfo view_info =
      {
        sizeof (GimpContainerComboBoxClass),
        NULL,           /* base_init      */
        NULL,           /* base_finalize  */
        (GClassInitFunc) gimp_container_combo_box_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data     */
        sizeof (GimpContainerComboBox),
        0,              /* n_preallocs    */
        (GInstanceInitFunc) gimp_container_combo_box_init,
      };

      static const GInterfaceInfo view_iface_info =
      {
        (GInterfaceInitFunc) gimp_container_combo_box_view_iface_init,
        NULL,           /* iface_finalize */
        NULL            /* iface_data     */
      };

      view_type = g_type_register_static (GTK_TYPE_COMBO_BOX,
                                          "GimpContainerComboBox",
                                          &view_info, 0);

      g_type_add_interface_static (view_type, GIMP_TYPE_CONTAINER_VIEW,
                                   &view_iface_info);
    }

  return view_type;
}

static void
gimp_container_combo_box_class_init (GimpContainerComboBoxClass *klass)
{
125
126
  GObjectClass   *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
127
128
129
130
131
132

  parent_class = g_type_class_peek_parent (klass);

  object_class->set_property = gimp_container_view_set_property;
  object_class->get_property = gimp_container_view_get_property;

133
134
  widget_class->unrealize    = gimp_container_combo_box_unrealize;

135
  gimp_container_view_install_properties (object_class);
136
137
138
139
140
141
142
143
144
145
}

static void
gimp_container_combo_box_init (GimpContainerComboBox *combo_box)
{
  GtkListStore    *store;
  GtkCellLayout   *layout;
  GtkCellRenderer *cell;

  store = gtk_list_store_new (NUM_COLUMNS,
146
                              GIMP_TYPE_VIEW_RENDERER,
147
148
149
150
151
152
153
154
155
156
157
158
159
160
                              G_TYPE_STRING);

  gtk_combo_box_set_model (GTK_COMBO_BOX (combo_box), GTK_TREE_MODEL (store));

  g_object_unref (store);

  layout = GTK_CELL_LAYOUT (combo_box);

  cell = gimp_cell_renderer_viewable_new ();
  gtk_cell_layout_pack_start (layout, cell, FALSE);
  gtk_cell_layout_set_attributes (layout, cell,
                                  "renderer", COLUMN_RENDERER,
                                  NULL);

161
162
  combo_box->viewable_renderer = cell;

163
164
165
  cell = g_object_new (GTK_TYPE_CELL_RENDERER_TEXT,
                       "ellipsize", PANGO_ELLIPSIZE_MIDDLE,
                       NULL);
166
167
168
169
  gtk_cell_layout_pack_start (layout, cell, TRUE);
  gtk_cell_layout_set_attributes (layout, cell,
                                  "text", COLUMN_NAME,
                                  NULL);
170
171
172
173
174

  g_signal_connect (combo_box, "changed",
                    G_CALLBACK (gimp_container_combo_box_changed),
                    combo_box);

175
176
177
178
179
180
181
}

static void
gimp_container_combo_box_view_iface_init (GimpContainerViewInterface *view_iface)
{
  parent_view_iface = g_type_interface_peek_parent (view_iface);

182
183
184
  if (! parent_view_iface)
    parent_view_iface = g_type_default_interface_peek (GIMP_TYPE_CONTAINER_VIEW);

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
  view_iface->insert_item      = gimp_container_combo_box_insert_item;
  view_iface->remove_item      = gimp_container_combo_box_remove_item;
  view_iface->reorder_item     = gimp_container_combo_box_reorder_item;
  view_iface->rename_item      = gimp_container_combo_box_rename_item;
  view_iface->select_item      = gimp_container_combo_box_select_item;
  view_iface->clear_items      = gimp_container_combo_box_clear_items;
  view_iface->set_preview_size = gimp_container_combo_box_set_preview_size;

  view_iface->insert_data_free = (GDestroyNotify) g_free;

}

GtkWidget *
gimp_container_combo_box_new (GimpContainer *container,
                              GimpContext   *context,
                              gint           preview_size,
                              gint           preview_border_width)
{
  GtkWidget         *combo_box;
  GimpContainerView *view;

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

  combo_box = g_object_new (GIMP_TYPE_CONTAINER_COMBO_BOX, NULL);

  view = GIMP_CONTAINER_VIEW (combo_box);

  gimp_container_view_set_preview_size (view,
                                        preview_size, preview_border_width);

  if (container)
    gimp_container_view_set_container (view, container);

  if (context)
    gimp_container_view_set_context (view, context);

  return combo_box;
}

static void
gimp_container_combo_box_set (GimpContainerComboBox *combo_box,
                              GtkTreeIter           *iter,
                              GimpViewable          *viewable)
{
231
232
233
234
235
236
  GimpContainerView *view = GIMP_CONTAINER_VIEW (combo_box);
  GtkTreeModel      *model;
  GimpViewRenderer  *renderer;
  gchar             *name;
  gint               preview_size;
  gint               border_width;
237
238
239
240
241
242
243

  model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));

  preview_size = gimp_container_view_get_preview_size (view, &border_width);

  name = gimp_viewable_get_description (viewable, NULL);

244
245
246
247
248
  renderer = gimp_view_renderer_new (G_TYPE_FROM_INSTANCE (viewable),
                                     preview_size, border_width,
                                     FALSE);
  gimp_view_renderer_set_viewable (renderer, viewable);
  gimp_view_renderer_remove_idle (renderer);
249
250
251

  g_signal_connect (renderer, "update",
                    G_CALLBACK (gimp_container_combo_box_renderer_update),
252
                    view);
253
254
255
256
257
258
259
260
261
262

  gtk_list_store_set (GTK_LIST_STORE (model), iter,
                      COLUMN_RENDERER, renderer,
                      COLUMN_NAME,     name,
                      -1);

  g_object_unref (renderer);
  g_free (name);
}

263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
static void
gimp_container_combo_box_unrealize (GtkWidget *widget)
{
  GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (widget));
  GtkTreeIter   iter;
  gboolean      iter_valid;

  for (iter_valid = gtk_tree_model_get_iter_first (model, &iter);
       iter_valid;
       iter_valid = gtk_tree_model_iter_next (model, &iter))
    {
      GimpViewRenderer *renderer;

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

      gimp_view_renderer_unrealize (renderer);
      g_object_unref (renderer);
    }

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

287
288
289
290
/*  GimpContainerView methods  */

static gpointer
gimp_container_combo_box_insert_item (GimpContainerView *view,
291
292
                                      GimpViewable      *viewable,
                                      gint               index)
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
{
  GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (view));
  GtkTreeIter  *iter;

  iter = g_new0 (GtkTreeIter, 1);

  if (index == -1)
    gtk_list_store_append (GTK_LIST_STORE (model), iter);
  else
    gtk_list_store_insert (GTK_LIST_STORE (model), iter, index);

  gimp_container_combo_box_set (GIMP_CONTAINER_COMBO_BOX (view),
                                iter, viewable);

  return (gpointer) iter;
}

static void
gimp_container_combo_box_remove_item (GimpContainerView *view,
312
313
                                      GimpViewable      *viewable,
                                      gpointer           insert_data)
314
315
316
317
318
{
  GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (view));
  GtkTreeIter  *iter  = insert_data;

  if (iter)
319
320
321
322
323
324
325
326
327
328
329
330
331
332
    {
      gtk_list_store_remove (GTK_LIST_STORE (model), iter);

#ifdef __GNUC__
#warning FIXME: remove this hack as soon as bug #149906 is fixed
#endif
      /*  if the store is empty after this remove, clear out renderers
       *  from all cells so they don't keep refing the viewables
       */
      if (! gtk_tree_model_iter_n_children (model, NULL))
        g_object_set (GIMP_CONTAINER_COMBO_BOX (view)->viewable_renderer,
                      "renderer", NULL,
                      NULL);
    }
333
334
335
336
}

static void
gimp_container_combo_box_reorder_item (GimpContainerView *view,
337
338
339
                                       GimpViewable      *viewable,
                                       gint               new_index,
                                       gpointer           insert_data)
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
{
  GtkTreeModel  *model     = gtk_combo_box_get_model (GTK_COMBO_BOX (view));
  GimpContainer *container = gimp_container_view_get_container (view);
  GtkTreeIter   *iter      = insert_data;

  if (!iter)
    return;

  if (new_index == -1 || new_index == container->num_children - 1)
    {
      gtk_list_store_move_before (GTK_LIST_STORE (model), iter, NULL);
    }
  else if (new_index == 0)
    {
      gtk_list_store_move_after (GTK_LIST_STORE (model), iter, NULL);
    }
  else
    {
      GtkTreePath *path;
      gint         old_index;

      path = gtk_tree_model_get_path (model, iter);
      old_index = gtk_tree_path_get_indices (path)[0];
      gtk_tree_path_free (path);

      if (new_index != old_index)
        {
          GtkTreeIter  place;

          path = gtk_tree_path_new_from_indices (new_index, -1);
          gtk_tree_model_get_iter (model, &place, path);
          gtk_tree_path_free (path);

          if (new_index > old_index)
            gtk_list_store_move_after (GTK_LIST_STORE (model), iter, &place);
          else
            gtk_list_store_move_before (GTK_LIST_STORE (model), iter, &place);
        }
    }
}

static void
gimp_container_combo_box_rename_item (GimpContainerView *view,
383
384
                                      GimpViewable      *viewable,
                                      gpointer           insert_data)
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
{
  GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (view));
  GtkTreeIter  *iter  = insert_data;

  if (iter)
    {
      gchar *name = gimp_viewable_get_description (viewable, NULL);

      gtk_list_store_set (GTK_LIST_STORE (model), iter,
                          COLUMN_NAME, name,
                          -1);

      g_free (name);
    }
}

static gboolean
gimp_container_combo_box_select_item (GimpContainerView *view,
403
404
                                      GimpViewable      *viewable,
                                      gpointer           insert_data)
405
406
407
408
{
  GtkComboBox *combo_box = GTK_COMBO_BOX (view);
  GtkTreeIter *iter      = insert_data;

409
410
411
412
  g_signal_handlers_block_by_func (combo_box,
                                   gimp_container_combo_box_changed,
                                   view);

413
414
415
416
417
418
  if (iter)
    {
      gtk_combo_box_set_active_iter (combo_box, iter);
    }
  else
    {
419
      gtk_combo_box_set_active (combo_box, -1);
420
421
    }

422
423
424
425
  g_signal_handlers_unblock_by_func (combo_box,
                                     gimp_container_combo_box_changed,
                                     view);

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
  return TRUE;
}

static void
gimp_container_combo_box_clear_items (GimpContainerView *view)
{
  GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (view));

  gtk_list_store_clear (GTK_LIST_STORE (model));

  parent_view_iface->clear_items (view);
}

static void
gimp_container_combo_box_set_preview_size (GimpContainerView *view)
{
  GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (view));
  GtkTreeIter   iter;
  gboolean      iter_valid;
  gint          preview_size;
  gint          border_width;

  preview_size = gimp_container_view_get_preview_size (view, &border_width);

  for (iter_valid = gtk_tree_model_get_iter_first (model, &iter);
       iter_valid;
       iter_valid = gtk_tree_model_iter_next (model, &iter))
    {
454
      GimpViewRenderer *renderer;
455
456
457
458
459

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

460
      gimp_view_renderer_set_size (renderer, preview_size, border_width);
461
462
463
464
465
      g_object_unref (renderer);
    }
}

static void
466
467
gimp_container_combo_box_changed (GtkComboBox       *combo_box,
                                  GimpContainerView *view)
468
{
469
  GtkTreeIter iter;
470
471
472
473
474
475

  if (parent_class->changed)
    parent_class->changed (combo_box);

  if (gtk_combo_box_get_active_iter (combo_box, &iter))
    {
476
      GimpViewRenderer *renderer;
477
478
479
480
481

      gtk_tree_model_get (gtk_combo_box_get_model (combo_box), &iter,
                          COLUMN_RENDERER, &renderer,
                          -1);

482
      gimp_container_view_item_selected (view, renderer->viewable);
483
484
485
      g_object_unref (renderer);
    }
}
486
487

static void
488
489
gimp_container_combo_box_renderer_update (GimpViewRenderer  *renderer,
                                          GimpContainerView *view)
490
491
492
493
494
495
496
497
498
499
500
501
502
{
  GtkTreeIter *iter = gimp_container_view_lookup (view, renderer->viewable);

  if (iter)
    {
      GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (view));
      GtkTreePath  *path;

      path = gtk_tree_model_get_path (model, iter);
      gtk_tree_model_row_changed (model, path, iter);
      gtk_tree_path_free (path);
    }
}