~ubuntu-branches/ubuntu/maverick/gimp/maverick-updates

« back to all changes in this revision

Viewing changes to app/widgets/gimpdatafactoryview.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2005-12-09 19:44:52 UTC
  • Revision ID: james.westby@ubuntu.com-20051209194452-yggpemjlofpjqyf4
Tags: upstream-2.2.9
ImportĀ upstreamĀ versionĀ 2.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* The GIMP -- an image manipulation program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
4
 * gimpdatafactoryview.c
 
5
 * Copyright (C) 2001-2003 Michael Natterer <mitch@gimp.org>
 
6
 *
 
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 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
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.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include <string.h>
 
25
 
 
26
#include <gtk/gtk.h>
 
27
 
 
28
#include "libgimpwidgets/gimpwidgets.h"
 
29
 
 
30
#include "widgets-types.h"
 
31
 
 
32
#include "config/gimpbaseconfig.h"
 
33
 
 
34
#include "core/gimp.h"
 
35
#include "core/gimpcontainer.h"
 
36
#include "core/gimpcontext.h"
 
37
#include "core/gimpdata.h"
 
38
#include "core/gimpdatafactory.h"
 
39
#include "core/gimpmarshal.h"
 
40
 
 
41
#include "gimpcontainerview.h"
 
42
#include "gimpdatafactoryview.h"
 
43
#include "gimpcontainergridview.h"
 
44
#include "gimpcontainertreeview.h"
 
45
#include "gimpdnd.h"
 
46
#include "gimpviewrenderer.h"
 
47
#include "gimpuimanager.h"
 
48
#include "gimpwidgets-utils.h"
 
49
 
 
50
#include "gimp-intl.h"
 
51
 
 
52
 
 
53
static void   gimp_data_factory_view_class_init (GimpDataFactoryViewClass *klass);
 
54
static void   gimp_data_factory_view_init       (GimpDataFactoryView      *view);
 
55
 
 
56
static void   gimp_data_factory_view_activate_item  (GimpContainerEditor *editor,
 
57
                                                     GimpViewable        *viewable);
 
58
static void gimp_data_factory_view_tree_name_edited (GtkCellRendererText *cell,
 
59
                                                     const gchar         *path,
 
60
                                                     const gchar         *name,
 
61
                                                     GimpDataFactoryView *view);
 
62
 
 
63
 
 
64
static GimpContainerEditorClass *parent_class = NULL;
 
65
 
 
66
 
 
67
GType
 
68
gimp_data_factory_view_get_type (void)
 
69
{
 
70
  static GType view_type = 0;
 
71
 
 
72
  if (! view_type)
 
73
    {
 
74
      static const GTypeInfo view_info =
 
75
      {
 
76
        sizeof (GimpDataFactoryViewClass),
 
77
        NULL,           /* base_init */
 
78
        NULL,           /* base_finalize */
 
79
        (GClassInitFunc) gimp_data_factory_view_class_init,
 
80
        NULL,           /* class_finalize */
 
81
        NULL,           /* class_data */
 
82
        sizeof (GimpDataFactoryView),
 
83
        0,              /* n_preallocs */
 
84
        (GInstanceInitFunc) gimp_data_factory_view_init,
 
85
      };
 
86
 
 
87
      view_type = g_type_register_static (GIMP_TYPE_CONTAINER_EDITOR,
 
88
                                          "GimpDataFactoryView",
 
89
                                          &view_info, 0);
 
90
    }
 
91
 
 
92
  return view_type;
 
93
}
 
94
 
 
95
static void
 
96
gimp_data_factory_view_class_init (GimpDataFactoryViewClass *klass)
 
97
{
 
98
  GimpContainerEditorClass *editor_class = GIMP_CONTAINER_EDITOR_CLASS (klass);
 
99
 
 
100
  parent_class = g_type_class_peek_parent (klass);
 
101
 
 
102
  editor_class->activate_item = gimp_data_factory_view_activate_item;
 
103
}
 
104
 
 
105
static void
 
106
gimp_data_factory_view_init (GimpDataFactoryView *view)
 
107
{
 
108
  view->edit_button      = NULL;
 
109
  view->new_button       = NULL;
 
110
  view->duplicate_button = NULL;
 
111
  view->delete_button    = NULL;
 
112
  view->refresh_button   = NULL;
 
113
}
 
114
 
 
115
GtkWidget *
 
116
gimp_data_factory_view_new (GimpViewType      view_type,
 
117
                            GimpDataFactory  *factory,
 
118
                            GimpContext      *context,
 
119
                            gint              preview_size,
 
120
                            gint              preview_border_width,
 
121
                            GimpMenuFactory  *menu_factory,
 
122
                            const gchar      *menu_identifier,
 
123
                            const gchar      *ui_identifier,
 
124
                            const gchar      *action_group)
 
125
{
 
126
  GimpDataFactoryView *factory_view;
 
127
 
 
128
  factory_view = g_object_new (GIMP_TYPE_DATA_FACTORY_VIEW, NULL);
 
129
 
 
130
  if (! gimp_data_factory_view_construct (factory_view,
 
131
                                          view_type,
 
132
                                          factory,
 
133
                                          context,
 
134
                                          preview_size,
 
135
                                          preview_border_width,
 
136
                                          menu_factory,
 
137
                                          menu_identifier,
 
138
                                          ui_identifier,
 
139
                                          action_group))
 
140
    {
 
141
      g_object_unref (factory_view);
 
142
      return NULL;
 
143
    }
 
144
 
 
145
  return GTK_WIDGET (factory_view);
 
146
}
 
147
 
 
148
gboolean
 
149
gimp_data_factory_view_construct (GimpDataFactoryView *factory_view,
 
150
                                  GimpViewType         view_type,
 
151
                                  GimpDataFactory     *factory,
 
152
                                  GimpContext         *context,
 
153
                                  gint                 preview_size,
 
154
                                  gint                 preview_border_width,
 
155
                                  GimpMenuFactory     *menu_factory,
 
156
                                  const gchar         *menu_identifier,
 
157
                                  const gchar         *ui_identifier,
 
158
                                  const gchar         *action_group)
 
159
{
 
160
  GimpContainerEditor *editor;
 
161
  gchar               *str;
 
162
 
 
163
  g_return_val_if_fail (GIMP_IS_DATA_FACTORY_VIEW (factory_view), FALSE);
 
164
  g_return_val_if_fail (GIMP_IS_DATA_FACTORY (factory), FALSE);
 
165
  g_return_val_if_fail (preview_size >  0 &&
 
166
                        preview_size <= GIMP_VIEWABLE_MAX_PREVIEW_SIZE, FALSE);
 
167
  g_return_val_if_fail (preview_border_width >= 0 &&
 
168
                        preview_border_width <= GIMP_VIEW_MAX_BORDER_WIDTH,
 
169
                        FALSE);
 
170
 
 
171
  factory_view->factory = factory;
 
172
 
 
173
  if (! gimp_container_editor_construct (GIMP_CONTAINER_EDITOR (factory_view),
 
174
                                         view_type,
 
175
                                         factory->container, context,
 
176
                                         preview_size, preview_border_width,
 
177
                                         menu_factory, menu_identifier,
 
178
                                         ui_identifier))
 
179
    {
 
180
      return FALSE;
 
181
    }
 
182
 
 
183
  editor = GIMP_CONTAINER_EDITOR (factory_view);
 
184
 
 
185
  if (GIMP_IS_CONTAINER_TREE_VIEW (editor->view))
 
186
    {
 
187
      GimpContainerTreeView *tree_view;
 
188
 
 
189
      tree_view = GIMP_CONTAINER_TREE_VIEW (editor->view);
 
190
 
 
191
      tree_view->name_cell->mode = GTK_CELL_RENDERER_MODE_EDITABLE;
 
192
      GTK_CELL_RENDERER_TEXT (tree_view->name_cell)->editable = TRUE;
 
193
 
 
194
      tree_view->editable_cells = g_list_prepend (tree_view->editable_cells,
 
195
                                                  tree_view->name_cell);
 
196
 
 
197
      g_signal_connect (tree_view->name_cell, "edited",
 
198
                        G_CALLBACK (gimp_data_factory_view_tree_name_edited),
 
199
                        factory_view);
 
200
    }
 
201
 
 
202
  str = g_strdup_printf ("%s-edit", action_group);
 
203
  factory_view->edit_button =
 
204
    gimp_editor_add_action_button (GIMP_EDITOR (editor->view), action_group,
 
205
                                   str, NULL);
 
206
  g_free (str);
 
207
 
 
208
  if (factory_view->factory->data_new_func)
 
209
    {
 
210
      str = g_strdup_printf ("%s-new", action_group);
 
211
      factory_view->new_button =
 
212
        gimp_editor_add_action_button (GIMP_EDITOR (editor->view), action_group,
 
213
                                       str, NULL);
 
214
      g_free (str);
 
215
    }
 
216
 
 
217
  str = g_strdup_printf ("%s-duplicate", action_group);
 
218
  factory_view->duplicate_button =
 
219
    gimp_editor_add_action_button (GIMP_EDITOR (editor->view), action_group,
 
220
                                   str, NULL);
 
221
  g_free (str);
 
222
 
 
223
  str = g_strdup_printf ("%s-delete", action_group);
 
224
  factory_view->delete_button =
 
225
    gimp_editor_add_action_button (GIMP_EDITOR (editor->view), action_group,
 
226
                                   str, NULL);
 
227
  g_free (str);
 
228
 
 
229
  str = g_strdup_printf ("%s-refresh", action_group);
 
230
  factory_view->refresh_button =
 
231
    gimp_editor_add_action_button (GIMP_EDITOR (editor->view), action_group,
 
232
                                   str, NULL);
 
233
  g_free (str);
 
234
 
 
235
  gimp_container_view_enable_dnd (editor->view,
 
236
                                  GTK_BUTTON (factory_view->edit_button),
 
237
                                  factory->container->children_type);
 
238
  gimp_container_view_enable_dnd (editor->view,
 
239
                                  GTK_BUTTON (factory_view->duplicate_button),
 
240
                                  factory->container->children_type);
 
241
  gimp_container_view_enable_dnd (editor->view,
 
242
                                  GTK_BUTTON (factory_view->delete_button),
 
243
                                  factory->container->children_type);
 
244
 
 
245
  gimp_ui_manager_update (GIMP_EDITOR (editor->view)->ui_manager, editor);
 
246
 
 
247
  return TRUE;
 
248
}
 
249
 
 
250
static void
 
251
gimp_data_factory_view_activate_item (GimpContainerEditor *editor,
 
252
                                      GimpViewable        *viewable)
 
253
{
 
254
  GimpDataFactoryView *view = GIMP_DATA_FACTORY_VIEW (editor);
 
255
  GimpData            *data = GIMP_DATA (viewable);
 
256
 
 
257
  if (GIMP_CONTAINER_EDITOR_CLASS (parent_class)->activate_item)
 
258
    GIMP_CONTAINER_EDITOR_CLASS (parent_class)->activate_item (editor, viewable);
 
259
 
 
260
  if (data && gimp_container_have (view->factory->container,
 
261
                                   GIMP_OBJECT (data)))
 
262
    {
 
263
      if (view->edit_button && GTK_WIDGET_SENSITIVE (view->edit_button))
 
264
        gtk_button_clicked (GTK_BUTTON (view->edit_button));
 
265
    }
 
266
}
 
267
 
 
268
static void
 
269
gimp_data_factory_view_tree_name_edited (GtkCellRendererText *cell,
 
270
                                         const gchar         *path_str,
 
271
                                         const gchar         *new_name,
 
272
                                         GimpDataFactoryView *view)
 
273
{
 
274
  GimpContainerTreeView *tree_view;
 
275
  GtkTreePath           *path;
 
276
  GtkTreeIter            iter;
 
277
 
 
278
  tree_view = GIMP_CONTAINER_TREE_VIEW (GIMP_CONTAINER_EDITOR (view)->view);
 
279
 
 
280
  path = gtk_tree_path_new_from_string (path_str);
 
281
 
 
282
  if (gtk_tree_model_get_iter (tree_view->model, &iter, path))
 
283
    {
 
284
      GimpViewRenderer *renderer;
 
285
      GimpData         *data;
 
286
      const gchar      *old_name;
 
287
 
 
288
      gtk_tree_model_get (tree_view->model, &iter,
 
289
                          tree_view->model_column_renderer, &renderer,
 
290
                          -1);
 
291
 
 
292
      data = GIMP_DATA (renderer->viewable);
 
293
 
 
294
      old_name = gimp_object_get_name (GIMP_OBJECT (data));
 
295
 
 
296
      if (! old_name) old_name = "";
 
297
      if (! new_name) new_name = "";
 
298
 
 
299
      if (data->writable && strcmp (old_name, new_name))
 
300
        {
 
301
          gimp_object_set_name (GIMP_OBJECT (data), new_name);
 
302
        }
 
303
      else
 
304
        {
 
305
          gchar *name = gimp_viewable_get_description (renderer->viewable, NULL);
 
306
 
 
307
          gtk_list_store_set (GTK_LIST_STORE (tree_view->model), &iter,
 
308
                              tree_view->model_column_name, name,
 
309
                              -1);
 
310
          g_free (name);
 
311
        }
 
312
 
 
313
      g_object_unref (renderer);
 
314
    }
 
315
 
 
316
  gtk_tree_path_free (path);
 
317
}