1/*
2 * Copyright (C) 2015 Red Hat Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2,1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "config.h"
21#include "WebKitWebEditor.h"
22
23#include "WebKitWebEditorPrivate.h"
24#include "WebKitWebPagePrivate.h"
25#include <wtf/glib/WTFGType.h>
26
27using namespace WebKit;
28using namespace WebCore;
29
30/**
31 * SECTION: WebKitWebEditor
32 * @Short_description: Access to editing capabilities of a #WebKitWebPage
33 * @Title: WebKitWebEditor
34 * @See_also: #WebKitWebPage
35 *
36 * The WebKitWebEditor provides access to various editing capabilities of
37 * a #WebKitWebPage such as a possibility to react to the current selection in
38 * #WebKitWebPage.
39 *
40 * Since: 2.10
41 */
42enum {
43 SELECTION_CHANGED,
44
45 LAST_SIGNAL
46};
47
48struct _WebKitWebEditorPrivate {
49 WebKitWebPage* webPage;
50};
51
52static guint signals[LAST_SIGNAL] = { 0, };
53
54WEBKIT_DEFINE_TYPE(WebKitWebEditor, webkit_web_editor, G_TYPE_OBJECT)
55
56static void webkit_web_editor_class_init(WebKitWebEditorClass* klass)
57{
58 /**
59 * WebKitWebEditor::selection-changed:
60 * @editor: the #WebKitWebEditor on which the signal is emitted
61 *
62 * This signal is emitted for every selection change inside a #WebKitWebPage
63 * as well as for every caret position change as the caret is a collapsed
64 * selection.
65 *
66 * Since: 2.10
67 */
68 signals[SELECTION_CHANGED] = g_signal_new(
69 "selection-changed",
70 G_TYPE_FROM_CLASS(klass),
71 G_SIGNAL_RUN_LAST,
72 0, nullptr, nullptr,
73 g_cclosure_marshal_VOID__VOID,
74 G_TYPE_NONE, 0);
75}
76
77class PageEditorClient final : public API::InjectedBundle::EditorClient {
78public:
79 explicit PageEditorClient(WebKitWebEditor* editor)
80 : m_editor(editor)
81 {
82 }
83
84private:
85 void didChangeSelection(WebPage&, StringImpl*) override
86 {
87 g_signal_emit(m_editor, signals[SELECTION_CHANGED], 0);
88 }
89
90 WebKitWebEditor* m_editor;
91};
92
93WebKitWebEditor* webkitWebEditorCreate(WebKitWebPage* webPage)
94{
95 WebKitWebEditor* editor = WEBKIT_WEB_EDITOR(g_object_new(WEBKIT_TYPE_WEB_EDITOR, nullptr));
96 editor->priv->webPage = webPage;
97 webkitWebPageGetPage(webPage)->setInjectedBundleEditorClient(std::make_unique<PageEditorClient>(editor));
98 return editor;
99}
100
101/**
102 * webkit_web_editor_get_page:
103 * @editor: a #WebKitWebEditor
104 *
105 * Gets the #WebKitWebPage that is associated with the #WebKitWebEditor that can
106 * be used to access the #WebKitDOMDocument currently loaded into it.
107 *
108 * Returns: (transfer none): the associated #WebKitWebPage
109 *
110 * Since: 2.10
111 */
112WebKitWebPage* webkit_web_editor_get_page(WebKitWebEditor* editor)
113{
114 g_return_val_if_fail(WEBKIT_IS_WEB_EDITOR(editor), nullptr);
115
116 return editor->priv->webPage;
117}
118