File: WebKitEditorState.cpp

package info (click to toggle)
webkit2gtk 2.48.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 429,764 kB
  • sloc: cpp: 3,697,587; javascript: 194,444; ansic: 169,997; python: 46,499; asm: 19,295; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; sh: 2,098; java: 1,993; lex: 1,327; pascal: 366; makefile: 298
file content (276 lines) | stat: -rw-r--r-- 8,382 bytes parent folder | download | duplicates (7)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * Copyright (C) 2015 Igalia S.L.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "config.h"
#include "WebKitEditorState.h"

#include "EditorState.h"
#include "WebKitEditorStatePrivate.h"
#include "WebPageProxy.h"
#include <glib/gi18n-lib.h>
#include <wtf/glib/WTFGType.h>

using namespace WebKit;

/**
 * WebKitEditorState:
 * @See_also: #WebKitWebView
 *
 * Web editor state.
 *
 * WebKitEditorState represents the state of a #WebKitWebView editor.
 * Use webkit_web_view_get_editor_state() to get the WebKitEditorState
 * of a #WebKitWebView.
 *
 * Since: 2.10
 */

enum {
    CHANGED,
    LAST_SIGNAL
};

enum {
    PROP_0,
    PROP_TYPING_ATTRIBUTES,
    N_PROPERTIES,
};

static std::array<GParamSpec*, N_PROPERTIES> sObjProperties;

struct _WebKitEditorStatePrivate {
    WebPageProxy* page;
    unsigned typingAttributes;
    unsigned isCutAvailable : 1;
    unsigned isCopyAvailable : 1;
    unsigned isPasteAvailable : 1;
    unsigned isUndoAvailable : 1;
    unsigned isRedoAvailable : 1;
};

static std::array<unsigned, LAST_SIGNAL> signals;

WEBKIT_DEFINE_FINAL_TYPE(WebKitEditorState, webkit_editor_state, G_TYPE_OBJECT, GObject)

static void webkitEditorStateGetProperty(GObject* object, guint propId, GValue* value, GParamSpec* paramSpec)
{
    WebKitEditorState* editorState = WEBKIT_EDITOR_STATE(object);

    switch (propId) {
    case PROP_TYPING_ATTRIBUTES:
        g_value_set_uint(value, webkit_editor_state_get_typing_attributes(editorState));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
    }
}

static void webkit_editor_state_class_init(WebKitEditorStateClass* editorStateClass)
{
    GObjectClass* objectClass = G_OBJECT_CLASS(editorStateClass);
    objectClass->get_property = webkitEditorStateGetProperty;

    /**
     * WebKitEditorState:typing-attributes:
     *
     * Bitmask of #WebKitEditorTypingAttributes flags.
     * See webkit_editor_state_get_typing_attributes() for more information.
     *
     * Since: 2.10
     */
    sObjProperties[PROP_TYPING_ATTRIBUTES] =
        g_param_spec_uint(
            "typing-attributes",
            nullptr,
            nullptr,
            0, G_MAXUINT, 0,
            WEBKIT_PARAM_READABLE);

    g_object_class_install_properties(objectClass, N_PROPERTIES, sObjProperties.data());

    /**
     * WebKitEditorState::changed:
     * @editor_state: the #WebKitEditorState on which the signal is emitted
     *
     * Emitted when the #WebKitEdtorState is changed.
     *
     * Since: 2.44
     */
    signals[CHANGED] =
        g_signal_new("changed",
            G_TYPE_FROM_CLASS(editorStateClass),
            G_SIGNAL_RUN_LAST,
            0, 0,
            nullptr,
            g_cclosure_marshal_VOID__VOID,
            G_TYPE_NONE, 0);
}

static void webkitEditorStateSetTypingAttributes(WebKitEditorState* editorState, unsigned typingAttributes)
{
    if (typingAttributes == editorState->priv->typingAttributes)
        return;

    editorState->priv->typingAttributes = typingAttributes;
    g_object_notify_by_pspec(G_OBJECT(editorState), sObjProperties[PROP_TYPING_ATTRIBUTES]);
}

WebKitEditorState* webkitEditorStateCreate(WebPageProxy& page)
{
    WebKitEditorState* editorState = WEBKIT_EDITOR_STATE(g_object_new(WEBKIT_TYPE_EDITOR_STATE, nullptr));
    editorState->priv->page = &page;
    editorState->priv->typingAttributes = WEBKIT_EDITOR_TYPING_ATTRIBUTE_NONE;
    webkitEditorStateChanged(editorState, page.editorState());
    return editorState;
}

void webkitEditorStateChanged(WebKitEditorState* editorState, const EditorState& newState)
{
    if (!newState.hasPostLayoutData())
        return;

    unsigned typingAttributes = WEBKIT_EDITOR_TYPING_ATTRIBUTE_NONE;
    const auto& postLayoutData = *newState.postLayoutData;
    if (postLayoutData.typingAttributes.contains(WebKit::TypingAttribute::Bold))
        typingAttributes |= WEBKIT_EDITOR_TYPING_ATTRIBUTE_BOLD;
    if (postLayoutData.typingAttributes.contains(WebKit::TypingAttribute::Italics))
        typingAttributes |= WEBKIT_EDITOR_TYPING_ATTRIBUTE_ITALIC;
    if (postLayoutData.typingAttributes.contains(WebKit::TypingAttribute::Underline))
        typingAttributes |= WEBKIT_EDITOR_TYPING_ATTRIBUTE_UNDERLINE;
    if (postLayoutData.typingAttributes.contains(WebKit::TypingAttribute::StrikeThrough))
        typingAttributes |= WEBKIT_EDITOR_TYPING_ATTRIBUTE_STRIKETHROUGH;

    webkitEditorStateSetTypingAttributes(editorState, typingAttributes);

    editorState->priv->isCutAvailable = postLayoutData.canCut;
    editorState->priv->isCopyAvailable = postLayoutData.canCopy;
    editorState->priv->isPasteAvailable = postLayoutData.canPaste;

    editorState->priv->isUndoAvailable = editorState->priv->page->canUndo();
    editorState->priv->isRedoAvailable = editorState->priv->page->canRedo();

    g_signal_emit(editorState, signals[CHANGED], 0, NULL);
}

/**
 * webkit_editor_state_get_typing_attributes:
 * @editor_state: a #WebKitEditorState
 *
 * Gets the typing attributes at the current cursor position.
 *
 * If there is a selection, this returns the typing attributes
 * of the selected text. Note that in case of a selection,
 * typing attributes are considered active only when they are
 * present throughout the selection.
 *
 * Returns: a bitmask of #WebKitEditorTypingAttributes flags
 *
 * Since: 2.10
 */
guint webkit_editor_state_get_typing_attributes(WebKitEditorState* editorState)
{
    g_return_val_if_fail(WEBKIT_IS_EDITOR_STATE(editorState), WEBKIT_EDITOR_TYPING_ATTRIBUTE_NONE);

    return editorState->priv->typingAttributes;
}

/**
 * webkit_editor_state_is_cut_available:
 * @editor_state: a #WebKitEditorState
 *
 * Gets whether a cut command can be issued.
 *
 * Returns: %TRUE if cut is currently available
 *
 * Since: 2.20
 */
gboolean webkit_editor_state_is_cut_available(WebKitEditorState* editorState)
{
    g_return_val_if_fail(WEBKIT_IS_EDITOR_STATE(editorState), FALSE);

    return editorState->priv->isCutAvailable;
}

/**
 * webkit_editor_state_is_copy_available:
 * @editor_state: a #WebKitEditorState
 *
 * Gets whether a copy command can be issued.
 *
 * Returns: %TRUE if copy is currently available
 *
 * Since: 2.20
 */
gboolean webkit_editor_state_is_copy_available(WebKitEditorState* editorState)
{
    g_return_val_if_fail(WEBKIT_IS_EDITOR_STATE(editorState), FALSE);

    return editorState->priv->isCopyAvailable;
}

/**
 * webkit_editor_state_is_paste_available:
 * @editor_state: a #WebKitEditorState
 *
 * Gets whether a paste command can be issued.
 *
 * Returns: %TRUE if paste is currently available
 *
 * Since: 2.20
 */
gboolean webkit_editor_state_is_paste_available(WebKitEditorState* editorState)
{
    g_return_val_if_fail(WEBKIT_IS_EDITOR_STATE(editorState), FALSE);

    return editorState->priv->isPasteAvailable;
}

/**
 * webkit_editor_state_is_undo_available:
 * @editor_state: a #WebKitEditorState
 *
 * Gets whether an undo command can be issued.
 *
 * Returns: %TRUE if undo is currently available
 *
 * Since: 2.20
 */
gboolean webkit_editor_state_is_undo_available(WebKitEditorState* editorState)
{
    g_return_val_if_fail(WEBKIT_IS_EDITOR_STATE(editorState), FALSE);

    return editorState->priv->isUndoAvailable;
}

/**
 * webkit_editor_state_is_redo_available:
 * @editor_state: a #WebKitEditorState
 *
 * Gets whether a redo command can be issued.
 *
 * Returns: %TRUE if redo is currently available
 *
 * Since: 2.20
 */
gboolean webkit_editor_state_is_redo_available(WebKitEditorState* editorState)
{
    g_return_val_if_fail(WEBKIT_IS_EDITOR_STATE(editorState), FALSE);

    return editorState->priv->isRedoAvailable;
}