~ubuntu-branches/ubuntu/lucid/gtkhtml3.14/lucid

« back to all changes in this revision

Viewing changes to components/html-editor/htmlsourceview.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2009-06-02 11:18:16 UTC
  • mfrom: (1.2.12 upstream)
  • Revision ID: james.westby@ubuntu.com-20090602111816-t7rv5r7z58i0lkb4
Tags: 1:3.27.2-0ubuntu1
* New upstream version
* debian/rules:
  - updated library version

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
 
/*  This file is part of the GtkHTML library.
3
 
 
4
 
    Copyright (C) 2000 Helix Code, Inc.
5
 
 
6
 
    This library is free software; you can redistribute it and/or
7
 
    modify it under the terms of the GNU Library General Public
8
 
    License as published by the Free Software Foundation; either
9
 
    version 2 of the License, or (at your option) any later version.
10
 
 
11
 
    This library is distributed in the hope that it will be useful,
12
 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 
    Library General Public License for more details.
15
 
 
16
 
    You should have received a copy of the GNU Library General Public License
17
 
    along with this library; see the file COPYING.LIB.  If not, write to
18
 
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
 
    Boston, MA 02110-1301, USA.
20
 
 
21
 
    Author: Larry Ewing <lewing@ximian.com>
22
 
 
23
 
*/
24
 
 
25
 
#include <config.h>
26
 
#ifdef GNOME_GTKHTML_EDITOR_SHLIB
27
 
#include <glib/gi18n-lib.h>
28
 
#else
29
 
#include <glib/gi18n.h>
30
 
#endif
31
 
#include <string.h>
32
 
#include <fcntl.h>
33
 
#include <sys/stat.h>
34
 
#include <libgnome/gnome-util.h>
35
 
#include <bonobo/bonobo-stream.h>
36
 
#include <bonobo/bonobo-stream-memory.h>
37
 
#include <bonobo/bonobo-exception.h>
38
 
 
39
 
#include "gtkhtml.h"
40
 
#include "htmlengine.h"
41
 
#include "gtkhtml-stream.h"
42
 
#include "htmlsourceview.h"
43
 
#include "e-html-utils.h"
44
 
 
45
 
struct _HTMLSourceViewPrivate {
46
 
        GtkHTML   *html;
47
 
        CORBA_Object pstream;
48
 
        char    *content_type;
49
 
        gint     current_interval;
50
 
        gint     timer_id;
51
 
        gboolean as_html;
52
 
};
53
 
 
54
 
enum {
55
 
        UPDATE,
56
 
        LAST_SIGNAL
57
 
};
58
 
 
59
 
static GtkObject *parent_class;
60
 
static guint      signals [LAST_SIGNAL] = { 0 };
61
 
 
62
 
static void
63
 
html_source_view_real_update (HTMLSourceView *view)
64
 
{
65
 
}
66
 
 
67
 
static void
68
 
html_source_view_load (HTMLSourceView *view)
69
 
{
70
 
        BonoboObject *smem;
71
 
        GtkHTMLStream *hstream;
72
 
        CORBA_Environment ev;
73
 
        CORBA_Object pstream;
74
 
        const char *text;
75
 
        char *html;
76
 
 
77
 
        CORBA_exception_init (&ev);
78
 
 
79
 
        pstream = view->priv->pstream;
80
 
        smem = bonobo_stream_mem_create (NULL, 1, FALSE, TRUE);
81
 
        Bonobo_PersistStream_save (pstream, BONOBO_OBJREF (smem), view->priv->content_type, &ev);
82
 
        /* Terminate the buffer for e_text_to_html */
83
 
        bonobo_stream_client_write (BONOBO_OBJREF (smem), "", 1, &ev);
84
 
 
85
 
        text = bonobo_stream_mem_get_buffer (BONOBO_STREAM_MEM (smem));
86
 
 
87
 
        if (!view->priv->as_html) {
88
 
                html = e_text_to_html_full (text, E_TEXT_TO_HTML_PRE | E_TEXT_TO_HTML_CONVERT_SPACES, 0);
89
 
        } else {
90
 
                html = g_strdup (text);
91
 
        }
92
 
 
93
 
        hstream = gtk_html_begin (view->priv->html);
94
 
        view->priv->html->engine->newPage = FALSE;
95
 
 
96
 
        gtk_html_stream_write (hstream, html, strlen (html));
97
 
        gtk_html_stream_close (hstream, GTK_HTML_STREAM_OK);
98
 
 
99
 
        bonobo_object_unref (BONOBO_OBJECT (smem));
100
 
        g_free (html);
101
 
 
102
 
        CORBA_exception_free (&ev);
103
 
}
104
 
 
105
 
static gint
106
 
html_source_view_timeout (gpointer *data)
107
 
{
108
 
        HTMLSourceView *view;
109
 
 
110
 
        g_return_val_if_fail (HTML_IS_SOURCE_VIEW (data), FALSE);
111
 
 
112
 
        view = HTML_SOURCE_VIEW (data);
113
 
        html_source_view_load (view);
114
 
 
115
 
        return TRUE;
116
 
}
117
 
 
118
 
void
119
 
html_source_view_set_timeout (HTMLSourceView *view, guint timeout)
120
 
{
121
 
        if (view->priv->timer_id)
122
 
                g_source_remove (view->priv->timer_id);
123
 
 
124
 
        view->priv->current_interval = timeout;
125
 
        view->priv->timer_id = g_timeout_add (timeout, (GtkFunction)html_source_view_timeout, view);
126
 
}
127
 
 
128
 
void
129
 
html_source_view_set_mode (HTMLSourceView *view, gboolean as_html)
130
 
{
131
 
        view->priv->as_html = as_html;
132
 
}
133
 
 
134
 
void
135
 
html_source_view_set_source (HTMLSourceView *view, BonoboWidget *control, char *content_type)
136
 
{
137
 
        CORBA_Object interface;
138
 
        CORBA_Environment ev;
139
 
        g_return_if_fail (HTML_IS_SOURCE_VIEW (view));
140
 
 
141
 
        CORBA_exception_init (&ev);
142
 
        interface = Bonobo_Unknown_queryInterface (bonobo_widget_get_objref (control),
143
 
                                                   "IDL:Bonobo/PersistStream:1.0", &ev);
144
 
        if (BONOBO_EX (&ev) || interface == CORBA_OBJECT_NIL) {
145
 
                g_warning ("Couldn't find persist stream interface");
146
 
                return;
147
 
        }
148
 
 
149
 
        g_free (view->priv->content_type);
150
 
        view->priv->content_type = g_strdup (content_type);
151
 
 
152
 
        view->priv->pstream = interface;
153
 
 
154
 
        html_source_view_set_timeout (view, view->priv->current_interval);
155
 
}
156
 
 
157
 
GtkWidget *
158
 
html_source_view_new (void)
159
 
{
160
 
        GtkWidget *view;
161
 
 
162
 
        view = GTK_WIDGET (g_object_new (html_source_view_get_type (), NULL));
163
 
 
164
 
        return view;
165
 
}
166
 
 
167
 
static void
168
 
html_source_view_init (HTMLSourceView *view)
169
 
{
170
 
        GtkWidget *html;
171
 
        GtkWidget *scroll;
172
 
 
173
 
        view->priv = g_new0 (HTMLSourceViewPrivate, 1);
174
 
 
175
 
        view->priv->content_type = NULL;
176
 
        view->priv->html = GTK_HTML (html = gtk_html_new ());
177
 
        view->priv->current_interval = 500;
178
 
        view->priv->as_html = FALSE;
179
 
 
180
 
        scroll = gtk_scrolled_window_new (NULL, NULL);
181
 
        gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll),
182
 
                                        GTK_POLICY_AUTOMATIC,
183
 
                                        GTK_POLICY_AUTOMATIC);
184
 
 
185
 
        gtk_container_add (GTK_CONTAINER (scroll), html);
186
 
 
187
 
        gtk_box_pack_start (GTK_BOX (view), scroll,
188
 
                            TRUE, TRUE, 0);
189
 
 
190
 
        gtk_widget_show_all (scroll);
191
 
}
192
 
 
193
 
static void
194
 
html_source_view_destroy (GtkObject *object)
195
 
{
196
 
        HTMLSourceView *view = HTML_SOURCE_VIEW (object);
197
 
        HTMLSourceViewPrivate *priv = view->priv;
198
 
 
199
 
        if (priv) {
200
 
                if (priv->timer_id)
201
 
                        g_source_remove (priv->timer_id);
202
 
                priv->timer_id = 0;
203
 
 
204
 
                if (priv->pstream != CORBA_OBJECT_NIL) {
205
 
                        CORBA_Environment ev;
206
 
 
207
 
                        CORBA_exception_init (&ev);
208
 
                        Bonobo_Unknown_unref (priv->pstream, &ev);
209
 
                        CORBA_Object_release (priv->pstream, &ev);
210
 
                        CORBA_exception_free (&ev);
211
 
                }
212
 
 
213
 
                g_free (view->priv);
214
 
                view->priv = NULL;
215
 
        }
216
 
 
217
 
        if (GTK_OBJECT_CLASS (parent_class)->destroy != NULL)
218
 
                (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
219
 
}
220
 
 
221
 
static void
222
 
html_source_view_class_init (HTMLSourceViewClass *klass)
223
 
{
224
 
        GtkObjectClass *object_class;
225
 
 
226
 
        object_class = (GtkObjectClass *) klass;
227
 
        parent_class = gtk_type_class (GTK_TYPE_VBOX);
228
 
 
229
 
        signals [UPDATE] = g_signal_new ("update",
230
 
                                         G_TYPE_FROM_CLASS (object_class),
231
 
                                         G_SIGNAL_RUN_FIRST,
232
 
                                         G_STRUCT_OFFSET (HTMLSourceViewClass, update),
233
 
                                         NULL, NULL,
234
 
                                         g_cclosure_marshal_VOID__VOID,
235
 
                                         G_TYPE_NONE, 0);
236
 
 
237
 
        object_class->destroy = html_source_view_destroy;
238
 
        klass->update = html_source_view_real_update;
239
 
}
240
 
 
241
 
GType
242
 
html_source_view_get_type (void)
243
 
{
244
 
        static GType view_type = 0;
245
 
 
246
 
        if (!view_type) {
247
 
                GTypeInfo view_info = {
248
 
                        sizeof (HTMLSourceViewClass),
249
 
                        NULL,
250
 
                        NULL,
251
 
                        (GClassInitFunc) html_source_view_class_init,
252
 
                        NULL,
253
 
                        NULL,
254
 
                        sizeof (HTMLSourceView),
255
 
                        1,
256
 
                        (GInstanceInitFunc) html_source_view_init,
257
 
                };
258
 
 
259
 
                view_type = g_type_register_static (GTK_TYPE_VBOX, "HTMLView", &view_info, 0);
260
 
        }
261
 
 
262
 
        return view_type;
263
 
}