gimpxmpmodeltext.c 3.56 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
/* gimpxmpmodeltext.c - custom text widget linked to the xmp model
 *
 * Copyright (C) 2010, Róman Joost <romanofski@gimp.org>
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <string.h>

#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>

#include "xmp-schemas.h"
#include "xmp-model.h"

#include "gimpxmpmodelwidget.h"
#include "gimpxmpmodeltext.h"


static void   gimp_xmp_model_text_iface_init    (GimpXmpModelWidgetInterface *iface);

34 35
static void   gimp_xmp_model_text_constructed (GObject *object);

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
static void   gimp_xmp_model_text_changed       (GtkTextBuffer               *text_buffer,
                                                 gpointer                    *user_data);

void          gimp_xmp_model_text_set_text      (GimpXmpModelWidget          *widget,
                                                 const gchar                 *tree_value);


G_DEFINE_TYPE_WITH_CODE (GimpXmpModelText, gimp_xmp_model_text,
                         GTK_TYPE_TEXT_VIEW,
                         G_IMPLEMENT_INTERFACE (GIMP_TYPE_XMP_MODEL_WIDGET,
                                                gimp_xmp_model_text_iface_init))

#define parent_class gimp_xmp_model_text_parent_class


static void
gimp_xmp_model_text_class_init (GimpXmpModelTextClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

56
  object_class->constructed  = gimp_xmp_model_text_constructed;
57 58 59 60 61 62
  object_class->set_property = gimp_xmp_model_widget_set_property;
  object_class->get_property = gimp_xmp_model_widget_get_property;

  gimp_xmp_model_widget_install_properties (object_class);
}

63 64 65 66 67 68
static void
gimp_xmp_model_text_iface_init (GimpXmpModelWidgetInterface *iface)
{
  iface->widget_set_text = gimp_xmp_model_text_set_text;
}

69 70 71 72 73 74 75 76 77 78 79 80
static void
gimp_xmp_model_text_init (GimpXmpModelText *text)
{
  GtkTextBuffer *text_buffer;

  text_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text));
  g_signal_connect (text_buffer, "end-user-action",
                    G_CALLBACK (gimp_xmp_model_text_changed),
                    text);
}

static void
81
gimp_xmp_model_text_constructed (GObject *object)
82
{
83 84 85 86
  if (G_OBJECT_CLASS (parent_class)->constructed)
    G_OBJECT_CLASS (parent_class)->constructed (object);

  gimp_xmp_model_widget_constructor (object);
87 88 89 90 91 92 93
}

static void
gimp_xmp_model_text_changed (GtkTextBuffer *text_buffer,
                             gpointer      *user_data)
{
  GimpXmpModelText *text = GIMP_XMP_MODEL_TEXT (user_data);
94 95 96
  GtkTextIter       start;
  GtkTextIter       end;
  const gchar      *value;
97 98 99

  gtk_text_buffer_get_bounds (text_buffer, &start, &end);
  value = gtk_text_buffer_get_text (text_buffer, &start, &end, FALSE);
100 101

  gimp_xmp_model_widget_changed (GIMP_XMP_MODEL_WIDGET (text), value);
102 103 104 105 106 107 108 109 110 111 112
}

void
gimp_xmp_model_text_set_text (GimpXmpModelWidget *widget,
                              const gchar        *tree_value)
{
  GtkTextBuffer *text_buffer;

  text_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (widget));
  gtk_text_buffer_set_text (text_buffer, tree_value, -1);
}