3
* Copyright (C) 2014 Christian Hergert <christian@hergert.me>
5
* This program is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19
#define G_LOG_DOMAIN "gb-html-view"
21
#include <glib/gi18n.h>
22
#include <gtksourceview/gtksourcefile.h>
24
#include <webkit2/webkit2.h>
26
#include "gb-editor-document.h"
27
#include "gb-html-document.h"
28
#include "gb-html-view.h"
29
#include "gb-widget.h"
33
GbView parent_instance;
35
/* Objects owned by view */
36
GbHtmlDocument *document;
38
/* References owned by Gtk template */
39
WebKitWebView *web_view1;
40
WebKitWebView *web_view2;
45
G_DEFINE_TYPE (GbHtmlView, gb_html_view, GB_TYPE_VIEW)
53
static GParamSpec *gParamSpecs [LAST_PROP];
56
gb_html_view_changed (GbHtmlView *self,
57
GtkTextBuffer *buffer)
60
gchar *base_uri = NULL;
64
g_return_if_fail (GB_IS_HTML_VIEW (self));
65
g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
67
if (GB_IS_EDITOR_DOCUMENT (self->document))
71
file = ide_buffer_get_file (IDE_BUFFER (self->document));
77
location = ide_file_get_file (file);
80
base_uri = g_file_get_uri (location);
84
content = gb_html_document_get_content (self->document);
85
webkit_web_view_load_html (self->web_view1, content, base_uri);
86
if (self->web_view2 != NULL)
87
webkit_web_view_load_html (self->web_view2, content, base_uri);
96
gb_html_view_connect (GbHtmlView *self,
97
GbHtmlDocument *document)
99
GtkTextBuffer *buffer;
101
g_return_if_fail (GB_IS_HTML_VIEW (self));
102
g_return_if_fail (GB_IS_HTML_DOCUMENT (document));
104
buffer = gb_html_document_get_buffer (document);
108
g_signal_connect_object (buffer,
110
G_CALLBACK (gb_html_view_changed),
114
gb_html_view_changed (self, buffer);
118
gb_html_view_disconnect (GbHtmlView *self,
119
GbHtmlDocument *document)
121
GtkTextBuffer *buffer;
123
g_return_if_fail (GB_IS_HTML_VIEW (self));
124
g_return_if_fail (GB_IS_HTML_DOCUMENT (document));
126
buffer = gb_html_document_get_buffer (document);
130
g_signal_handlers_disconnect_by_func (buffer,
131
G_CALLBACK (gb_html_view_changed),
136
gb_html_view_get_document (GbView *view)
138
GbHtmlView *self = (GbHtmlView *)view;
140
g_return_val_if_fail (GB_IS_HTML_VIEW (self), NULL);
142
return GB_DOCUMENT (self->document);
146
gb_html_view_set_document (GbHtmlView *self,
147
GbDocument *document)
149
g_return_if_fail (GB_IS_HTML_VIEW (self));
150
g_return_if_fail (GB_IS_DOCUMENT (document));
152
if (!GB_IS_HTML_DOCUMENT (document))
154
g_warning ("GbHtmlView does not know how to handle a document "
156
g_type_name (G_TYPE_FROM_INSTANCE (document)));
160
if (document != (GbDocument *)self->document)
164
gb_html_view_disconnect (self, self->document);
165
g_clear_object (&self->document);
170
self->document = g_object_ref (document);
171
gb_html_view_connect (self, self->document);
174
g_object_notify_by_pspec (G_OBJECT (self), gParamSpecs [PROP_DOCUMENT]);
179
gb_html_view_create_split (GbView *view)
183
g_assert (GB_IS_HTML_VIEW (view));
185
ret = g_object_new (GB_TYPE_HTML_VIEW,
186
"document", gb_html_view_get_document (view),
194
gb_html_view_set_split_view (GbView *view,
197
GbHtmlView *self = (GbHtmlView *)view;
199
g_assert (GB_IS_HTML_VIEW (self));
201
g_return_if_fail (GB_IS_HTML_VIEW (view));
203
if (split_view && (self->web_view2 != NULL))
206
if (!split_view && (self->web_view2 == NULL))
211
GtkTextBuffer *buffer = gb_html_document_get_buffer (self->document);
215
self->web_view2 = g_object_new (WEBKIT_TYPE_WEB_VIEW,
218
gtk_container_add_with_properties (GTK_CONTAINER (self->paned), GTK_WIDGET (self->web_view2),
222
gtk_widget_grab_focus (GTK_WIDGET (self->web_view2));
223
gb_html_view_changed (self, buffer);
227
GtkWidget *copy = GTK_WIDGET (self->web_view2);
229
self->web_view2 = NULL;
230
gtk_container_remove (GTK_CONTAINER (self->paned), copy);
231
gtk_widget_grab_focus (GTK_WIDGET (self->web_view1));
236
gb_html_view_refresh (GSimpleAction *action,
237
GVariant *parameters,
240
GtkTextBuffer *buffer;
241
GbHtmlView *self = user_data;
243
g_return_if_fail (GB_IS_HTML_VIEW (self));
248
buffer = gb_html_document_get_buffer (self->document);
252
gb_html_view_changed (self, buffer);
256
gb_html_view_finalize (GObject *object)
258
GbHtmlView *self = (GbHtmlView *)object;
260
g_clear_object (&self->document);
262
G_OBJECT_CLASS (gb_html_view_parent_class)->finalize (object);
266
gb_html_view_get_property (GObject *object,
271
GbHtmlView *self = GB_HTML_VIEW (object);
276
g_value_set_object (value,
277
gb_html_view_get_document (GB_VIEW (self)));
281
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
286
gb_html_view_set_property (GObject *object,
291
GbHtmlView *self = GB_HTML_VIEW (object);
296
gb_html_view_set_document (self, g_value_get_object (value));
300
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
305
gb_html_view_class_init (GbHtmlViewClass *klass)
307
GObjectClass *object_class = G_OBJECT_CLASS (klass);
308
GbViewClass *view_class = GB_VIEW_CLASS (klass);
310
object_class->finalize = gb_html_view_finalize;
311
object_class->get_property = gb_html_view_get_property;
312
object_class->set_property = gb_html_view_set_property;
314
view_class->get_document = gb_html_view_get_document;
315
view_class->create_split = gb_html_view_create_split;
316
view_class->set_split_view = gb_html_view_set_split_view;
318
gParamSpecs [PROP_DOCUMENT] =
319
g_param_spec_object ("document",
321
_("The document to view as HTML."),
323
(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
325
g_object_class_install_properties (object_class, LAST_PROP, gParamSpecs);
327
GB_WIDGET_CLASS_TEMPLATE (klass, "gb-html-view.ui");
328
GB_WIDGET_CLASS_BIND (klass, GbHtmlView, web_view1);
329
GB_WIDGET_CLASS_BIND (klass, GbHtmlView, paned);
331
g_type_ensure (WEBKIT_TYPE_WEB_VIEW);
335
gb_html_view_init (GbHtmlView *self)
337
static const GActionEntry entries[] = {
338
{ "refresh", gb_html_view_refresh },
340
GSimpleActionGroup *actions;
343
gtk_widget_init_template (GTK_WIDGET (self));
345
controls = gb_view_get_controls (GB_VIEW (self));
347
actions = g_simple_action_group_new ();
348
g_action_map_add_action_entries (G_ACTION_MAP (actions), entries,
349
G_N_ELEMENTS (entries), self);
350
gtk_widget_insert_action_group (GTK_WIDGET (self), "html-view",
351
G_ACTION_GROUP (actions));
352
gtk_widget_insert_action_group (controls, "html-view",
353
G_ACTION_GROUP (actions));
354
g_object_unref (actions);