summaryrefslogtreecommitdiffstats
path: root/src/widgets/sp-xmlview-content.cpp
blob: d30f0bbe654acda26f7876e9a47023426113ae3c (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Specialization of GtkTextView for the XML tree view
 *
 * Authors:
 *   MenTaLguY <mental@rydia.net>
 *
 * Copyright (C) 2002 MenTaLguY
 *   Abhishek Sharma
 *
 * Released under the GNU GPL; see COPYING for details
 */

#include <cstring>
#include <glibmm/i18n.h>

#include "xml/node-event-vector.h"
#include "sp-xmlview-content.h"
#include "desktop-handles.h"
#include "document-private.h"
#include "document-undo.h"
#include "inkscape.h"
#include "verbs.h"

using Inkscape::DocumentUndo;

static void sp_xmlview_content_class_init (SPXMLViewContentClass * klass);
static void sp_xmlview_content_init (SPXMLViewContent * text);
static void sp_xmlview_content_destroy (GtkObject * object);

void sp_xmlview_content_changed (GtkTextBuffer *tb, SPXMLViewContent *text);

static void event_content_changed (Inkscape::XML::Node * repr, const gchar * old_content, const gchar * new_content, gpointer data);

static GtkTextViewClass * parent_class = NULL;

static Inkscape::XML::NodeEventVector repr_events = {
    NULL, /* child_added */
    NULL, /* child_removed */
    NULL, /* attr_changed */
    event_content_changed,
    NULL  /* order_changed */
};

GtkWidget *
sp_xmlview_content_new (Inkscape::XML::Node * repr)
{
    GtkTextBuffer *tb;
    SPXMLViewContent *text;

    tb = gtk_text_buffer_new (NULL);
    text = (SPXMLViewContent*)g_object_new (SP_TYPE_XMLVIEW_CONTENT, NULL);
    gtk_text_view_set_buffer (GTK_TEXT_VIEW (text), tb);
    gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (text), GTK_WRAP_CHAR);

    g_signal_connect (G_OBJECT (tb), "changed", G_CALLBACK (sp_xmlview_content_changed), text);

    /* should we alter the scrolling adjustments here? */

    sp_xmlview_content_set_repr (text, repr);

    return (GtkWidget *) text;
}

void
sp_xmlview_content_set_repr (SPXMLViewContent * text, Inkscape::XML::Node * repr)
{
    if ( repr == text->repr ) return;
    if (text->repr) {
        sp_repr_remove_listener_by_data (text->repr, text);
        Inkscape::GC::release(text->repr);
    }
    text->repr = repr;
    if (repr) {
        Inkscape::GC::anchor(repr);
        sp_repr_add_listener (repr, &repr_events, text);
        sp_repr_synthesize_events (repr, &repr_events, text);
    } else {
        gtk_text_buffer_set_text (gtk_text_view_get_buffer (GTK_TEXT_VIEW (text)), "", 0);
        gtk_text_view_set_editable (GTK_TEXT_VIEW (text), FALSE);
    }
}

GType sp_xmlview_content_get_type(void)
{
    static GType type = 0;

    if (!type) {
        GTypeInfo info = {
            sizeof(SPXMLViewContentClass),
            0, // base_init
            0, // base_finalize
            (GClassInitFunc)sp_xmlview_content_class_init,
            0, // class_finalize
            0, // class_data
            sizeof(SPXMLViewContent),
            0, // n_preallocs
            (GInstanceInitFunc)sp_xmlview_content_init,
            0 // value_table
        };
        type = g_type_register_static(GTK_TYPE_TEXT_VIEW, "SPXMLViewContent", &info, static_cast<GTypeFlags>(0));
    }

    return type;
}

void
sp_xmlview_content_class_init (SPXMLViewContentClass * klass)
{
    GtkObjectClass * object_class;

    object_class = (GtkObjectClass *) klass;

    parent_class = (GtkTextViewClass*)g_type_class_peek_parent (klass);

    object_class->destroy = sp_xmlview_content_destroy;
}

void
sp_xmlview_content_init (SPXMLViewContent *text)
{
    text->repr = NULL;
    text->blocked = FALSE;
}

void
sp_xmlview_content_destroy (GtkObject * object)
{
    SPXMLViewContent * text;

    text = SP_XMLVIEW_CONTENT (object);

    sp_xmlview_content_set_repr (text, NULL);

    GTK_OBJECT_CLASS (parent_class)->destroy (object);
}

void
event_content_changed (Inkscape::XML::Node * /*repr*/, const gchar * /*old_content*/, const gchar * new_content, gpointer data)
{
    SPXMLViewContent * text;
    text = SP_XMLVIEW_CONTENT (data);

    if (text->blocked) return;

    text->blocked = TRUE;

    if (new_content) {
        gtk_text_buffer_set_text (gtk_text_view_get_buffer (GTK_TEXT_VIEW (text)), new_content, strlen (new_content));
    } else {
        gtk_text_buffer_set_text (gtk_text_view_get_buffer (GTK_TEXT_VIEW (text)), "", 0);
    }
    gtk_text_view_set_editable (GTK_TEXT_VIEW (text), new_content != NULL);

    text->blocked = FALSE;
}

void
sp_xmlview_content_changed (GtkTextBuffer *tb, SPXMLViewContent *text)
{
    if (text->blocked) return;

    if (text->repr) {
        GtkTextIter start, end;
        gchar *data;
        text->blocked = TRUE;
        gtk_text_buffer_get_bounds (tb, &start, &end);
        data = gtk_text_buffer_get_text (tb, &start, &end, TRUE);
        text->repr->setContent(data);
        g_free (data);
        text->blocked = FALSE;
        DocumentUndo::done(sp_desktop_document(SP_ACTIVE_DESKTOP), SP_VERB_DIALOG_XML_EDITOR,
			   _("Type text in a text node"));
    }
}