gimpdataeditor.c 14 KB
Newer Older
1 2 3 4
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimpdataeditor.c
5
 * Copyright (C) 2002-2004 Michael Natterer <mitch@gimp.org>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * 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"

24 25
#include <string.h>

26 27 28 29 30 31 32
#include <gtk/gtk.h>

#include "libgimpwidgets/gimpwidgets.h"

#include "widgets-types.h"

#include "core/gimp.h"
33
#include "core/gimpcontainer.h"
34 35
#include "core/gimpcontext.h"
#include "core/gimpdata.h"
36
#include "core/gimpdatafactory.h"
37 38

#include "gimpdataeditor.h"
39
#include "gimpdocked.h"
40
#include "gimpmenufactory.h"
41
#include "gimpsessioninfo.h"
42
#include "gimpuimanager.h"
43

44
#include "gimp-intl.h"
45 46


47 48 49
enum
{
  PROP_0,
Michael Natterer's avatar
Michael Natterer committed
50 51
  PROP_DATA_FACTORY,
  PROP_DATA
52 53 54
};


55 56 57
static void       gimp_data_editor_class_init (GimpDataEditorClass *klass);
static void       gimp_data_editor_init       (GimpDataEditor      *view);

58
static void       gimp_data_editor_docked_iface_init (GimpDockedInterface *docked_iface);
59

60 61 62
static GObject  * gimp_data_editor_constructor       (GType            type,
                                                      guint            n_params,
                                                      GObjectConstructParam *params);
63 64 65 66
static void       gimp_data_editor_set_property      (GObject        *object,
                                                      guint           property_id,
                                                      const GValue   *value,
                                                      GParamSpec     *pspec);
Michael Natterer's avatar
Michael Natterer committed
67 68 69 70
static void       gimp_data_editor_get_property      (GObject        *object,
                                                      guint           property_id,
                                                      GValue         *value,
                                                      GParamSpec     *pspec);
71 72
static void       gimp_data_editor_dispose           (GObject        *object);

73 74 75 76
static void       gimp_data_editor_set_aux_info      (GimpDocked     *docked,
                                                      GList          *aux_info);
static GList    * gimp_data_editor_get_aux_info      (GimpDocked     *docked);

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
static void       gimp_data_editor_real_set_data     (GimpDataEditor *editor,
                                                      GimpData       *data);

static void       gimp_data_editor_name_activate     (GtkWidget      *widget,
                                                      GimpDataEditor *editor);
static gboolean   gimp_data_editor_name_focus_out    (GtkWidget      *widget,
                                                      GdkEvent       *event,
                                                      GimpDataEditor *editor);

static void       gimp_data_editor_data_name_changed (GimpObject     *object,
                                                      GimpDataEditor *editor);

static void       gimp_data_editor_save_clicked      (GtkWidget      *widget,
                                                      GimpDataEditor *editor);
static void       gimp_data_editor_revert_clicked    (GtkWidget      *widget,
                                                      GimpDataEditor *editor);
93
static void       gimp_data_editor_save_dirty        (GimpDataEditor *editor);
94 95 96 97 98 99 100 101


static GimpEditorClass *parent_class = NULL;


GType
gimp_data_editor_get_type (void)
{
102
  static GType type = 0;
103

104
  if (! type)
105 106 107 108 109 110 111 112 113 114 115 116 117
    {
      static const GTypeInfo view_info =
      {
        sizeof (GimpDataEditorClass),
        NULL,           /* base_init */
        NULL,           /* base_finalize */
        (GClassInitFunc) gimp_data_editor_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data */
        sizeof (GimpDataEditor),
        0,              /* n_preallocs */
        (GInstanceInitFunc) gimp_data_editor_init,
      };
118 119 120 121 122 123
      static const GInterfaceInfo docked_iface_info =
      {
        (GInterfaceInitFunc) gimp_data_editor_docked_iface_init,
        NULL,           /* iface_finalize */
        NULL            /* iface_data     */
      };
124

125 126 127 128 129 130
      type = g_type_register_static (GIMP_TYPE_EDITOR,
                                     "GimpDataEditor",
                                     &view_info, 0);

      g_type_add_interface_static (type, GIMP_TYPE_DOCKED,
                                   &docked_iface_info);
131 132
    }

133
  return type;
134 135 136 137 138
}

static void
gimp_data_editor_class_init (GimpDataEditorClass *klass)
{
139
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
140 141 142

  parent_class = g_type_class_peek_parent (klass);

143
  object_class->constructor  = gimp_data_editor_constructor;
144
  object_class->set_property = gimp_data_editor_set_property;
Michael Natterer's avatar
Michael Natterer committed
145
  object_class->get_property = gimp_data_editor_get_property;
146
  object_class->dispose      = gimp_data_editor_dispose;
147

148 149 150 151 152 153
  klass->set_data            = gimp_data_editor_real_set_data;

  g_object_class_install_property (object_class, PROP_DATA_FACTORY,
                                   g_param_spec_object ("data-factory",
                                                        NULL, NULL,
                                                        GIMP_TYPE_DATA_FACTORY,
Michael Natterer's avatar
Michael Natterer committed
154
                                                        G_PARAM_READWRITE |
155
                                                        G_PARAM_CONSTRUCT_ONLY));
Michael Natterer's avatar
Michael Natterer committed
156 157 158 159 160 161

  g_object_class_install_property (object_class, PROP_DATA,
                                   g_param_spec_object ("data",
                                                        NULL, NULL,
                                                        GIMP_TYPE_DATA,
                                                        G_PARAM_READWRITE));
162 163 164 165 166
}

static void
gimp_data_editor_init (GimpDataEditor *editor)
{
167
  editor->data_factory  = NULL;
168 169
  editor->data          = NULL;
  editor->data_editable = FALSE;
170 171

  editor->name_entry = gtk_entry_new ();
172
  gtk_box_pack_start (GTK_BOX (editor), editor->name_entry, FALSE, FALSE, 0);
173 174
  gtk_widget_show (editor->name_entry);

175
  g_signal_connect (editor->name_entry, "activate",
176 177
		    G_CALLBACK (gimp_data_editor_name_activate),
                    editor);
178
  g_signal_connect (editor->name_entry, "focus_out_event",
179 180
		    G_CALLBACK (gimp_data_editor_name_focus_out),
                    editor);
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
}

static void
gimp_data_editor_docked_iface_init (GimpDockedInterface *docked_iface)
{
  docked_iface->set_aux_info = gimp_data_editor_set_aux_info;
  docked_iface->get_aux_info = gimp_data_editor_get_aux_info;
}

static GObject *
gimp_data_editor_constructor (GType                  type,
                              guint                  n_params,
                              GObjectConstructParam *params)
{
  GObject        *object;
  GimpDataEditor *editor;

  object = G_OBJECT_CLASS (parent_class)->constructor (type, n_params, params);

  editor = GIMP_DATA_EDITOR (object);
201

202
  editor->save_button =
203 204 205 206 207 208 209
    gimp_editor_add_button (GIMP_EDITOR (editor),
                            GTK_STOCK_SAVE,
                            _("Save"), NULL,
                            G_CALLBACK (gimp_data_editor_save_clicked),
                            NULL,
                            editor);

210
  editor->revert_button =
211 212 213 214 215 216
    gimp_editor_add_button (GIMP_EDITOR (editor),
                            GTK_STOCK_REVERT_TO_SAVED,
                            _("Revert"), NULL,
                            G_CALLBACK (gimp_data_editor_revert_clicked),
                            NULL,
                            editor);
217 218 219 220
  /*
   * Set insensitive because revert buttons are not yet implemented.
   */
  gtk_widget_set_sensitive (editor->revert_button, FALSE);
221

222
  return object;
223 224
}

225 226 227 228 229 230 231 232 233 234 235 236 237
static void
gimp_data_editor_set_property (GObject      *object,
                               guint         property_id,
                               const GValue *value,
                               GParamSpec   *pspec)
{
  GimpDataEditor *editor = GIMP_DATA_EDITOR (object);

  switch (property_id)
    {
    case PROP_DATA_FACTORY:
      editor->data_factory = g_value_get_object (value);
      break;
Michael Natterer's avatar
Michael Natterer committed
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
    case PROP_DATA:
      gimp_data_editor_set_data (editor,
                                 (GimpData *) g_value_get_object (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_data_editor_get_property (GObject    *object,
                               guint       property_id,
                               GValue     *value,
                               GParamSpec *pspec)
{
  GimpDataEditor *editor = GIMP_DATA_EDITOR (object);

  switch (property_id)
    {
    case PROP_DATA_FACTORY:
      g_value_set_object (value, editor->data_factory);
      break;
    case PROP_DATA:
      g_value_set_object (value, editor->data);
      break;
264 265 266 267 268 269
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
static void
gimp_data_editor_dispose (GObject *object)
{
  GimpDataEditor *editor = GIMP_DATA_EDITOR (object);

  if (editor->data)
    {
      /* Save dirty data before we clear out */
      gimp_data_editor_save_dirty (editor);
      gimp_data_editor_set_data (editor, NULL);
    }

  G_OBJECT_CLASS (parent_class)->dispose (object);
}

285 286
#define AUX_INFO_CURRENT_DATA "current-data"

287 288 289 290 291 292 293 294 295 296 297
static void
gimp_data_editor_set_aux_info (GimpDocked *docked,
                               GList      *aux_info)
{
  GimpDataEditor *editor = GIMP_DATA_EDITOR (docked);
  GList          *list;

  for (list = aux_info; list; list = g_list_next (list))
    {
      GimpSessionInfoAux *aux = list->data;

298
      if (! strcmp (aux->name, AUX_INFO_CURRENT_DATA))
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
        {
          GimpData *data;

          data = (GimpData *)
            gimp_container_get_child_by_name (editor->data_factory->container,
                                              aux->value);

          if (data)
            gimp_data_editor_set_data (editor, data);
        }
    }
}

static GList *
gimp_data_editor_get_aux_info (GimpDocked *docked)
{
  GimpDataEditor *editor   = GIMP_DATA_EDITOR (docked);
  GList          *aux_info = NULL;

  if (editor->data)
    {
      GimpSessionInfoAux *aux;
321
      const gchar        *value;
322

323
      value = gimp_object_get_name (GIMP_OBJECT (editor->data));
324

325
      aux = gimp_session_info_aux_new (AUX_INFO_CURRENT_DATA, value);
326 327 328 329 330 331
      aux_info = g_list_append (aux_info, aux);
    }

  return aux_info;
}

332 333 334 335 336 337
static void
gimp_data_editor_real_set_data (GimpDataEditor *editor,
                                GimpData       *data)
{
  if (editor->data)
    {
338
      g_signal_handlers_disconnect_by_func (editor->data,
339 340 341
                                            gimp_data_editor_data_name_changed,
                                            editor);

342
      g_object_unref (editor->data);
343 344 345 346 347 348
    }

  editor->data = data;

  if (editor->data)
    {
349
      g_object_ref (editor->data);
350

351
      g_signal_connect (editor->data, "name_changed",
352 353 354
                        G_CALLBACK (gimp_data_editor_data_name_changed),
                        editor);

355 356 357 358 359 360
      gtk_entry_set_text (GTK_ENTRY (editor->name_entry),
                          gimp_object_get_name (GIMP_OBJECT (editor->data)));
    }
  else
    {
      gtk_entry_set_text (GTK_ENTRY (editor->name_entry), "");
361 362
    }

363
  editor->data_editable = (editor->data && editor->data->writable);
364 365

  gtk_widget_set_sensitive (editor->name_entry, editor->data_editable);
366 367 368 369 370 371 372
}

void
gimp_data_editor_set_data (GimpDataEditor *editor,
                           GimpData       *data)
{
  g_return_if_fail (GIMP_IS_DATA_EDITOR (editor));
373
  g_return_if_fail (data == NULL || GIMP_IS_DATA (data));
374 375 376
  g_return_if_fail (data == NULL ||
                    g_type_is_a (G_TYPE_FROM_INSTANCE (data),
                                 editor->data_factory->container->children_type));
377 378

  if (editor->data != data)
Michael Natterer's avatar
Michael Natterer committed
379 380 381 382
    {
      GIMP_DATA_EDITOR_GET_CLASS (editor)->set_data (editor, data);

      g_object_notify (G_OBJECT (editor), "data");
383 384 385 386

      if (GIMP_EDITOR (editor)->ui_manager)
        gimp_ui_manager_update (GIMP_EDITOR (editor)->ui_manager,
                                GIMP_EDITOR (editor)->popup_data);
Michael Natterer's avatar
Michael Natterer committed
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
}

GimpData *
gimp_data_editor_get_data (GimpDataEditor *editor)
{
  g_return_val_if_fail (GIMP_IS_DATA_EDITOR (editor), NULL);

  return editor->data;
}


/*  private functions  */

static void
gimp_data_editor_name_activate (GtkWidget      *widget,
                                GimpDataEditor *editor)
{
  if (editor->data)
    {
      const gchar *entry_text;

      entry_text = gtk_entry_get_text (GTK_ENTRY (widget));

      gimp_object_set_name (GIMP_OBJECT (editor->data), entry_text);
    }
}

static gboolean
gimp_data_editor_name_focus_out (GtkWidget      *widget,
                                 GdkEvent       *event,
                                 GimpDataEditor *editor)
{
  gimp_data_editor_name_activate (widget, editor);

  return FALSE;
}

static void
gimp_data_editor_data_name_changed (GimpObject     *object,
                                    GimpDataEditor *editor)
{
  gtk_entry_set_text (GTK_ENTRY (editor->name_entry),
		      gimp_object_get_name (object));
}

static void
gimp_data_editor_save_clicked (GtkWidget      *widget,
                               GimpDataEditor *editor)
436 437 438 439 440 441 442 443 444 445 446 447 448
{
  gimp_data_editor_save_dirty (editor);
}

static void
gimp_data_editor_revert_clicked (GtkWidget      *widget,
                                 GimpDataEditor *editor)
{
  g_print ("TODO: implement revert\n");
}

static void
gimp_data_editor_save_dirty (GimpDataEditor *editor)
449
{
450
  GimpData *data = editor->data;
451

452
  if (data && data->dirty && data->writable)
453
    gimp_data_factory_data_save_single (editor->data_factory, data);
454
}