WebKitEditorState.cpp [plain text]
#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;
enum {
PROP_0,
PROP_TYPING_ATTRIBUTES
};
struct _WebKitEditorStatePrivate {
WebPageProxy* page;
unsigned typingAttributes;
unsigned isCutAvailable : 1;
unsigned isCopyAvailable : 1;
unsigned isPasteAvailable : 1;
unsigned isUndoAvailable : 1;
unsigned isRedoAvailable : 1;
};
WEBKIT_DEFINE_TYPE(WebKitEditorState, webkit_editor_state, G_TYPE_OBJECT)
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;
g_object_class_install_property(
objectClass,
PROP_TYPING_ATTRIBUTES,
g_param_spec_uint(
"typing-attributes",
_("Typing Attributes"),
_("Flags with the typing attributes"),
0, G_MAXUINT, 0,
WEBKIT_PARAM_READABLE));
}
static void webkitEditorStateSetTypingAttributes(WebKitEditorState* editorState, unsigned typingAttributes)
{
if (typingAttributes == editorState->priv->typingAttributes)
return;
editorState->priv->typingAttributes = typingAttributes;
g_object_notify(G_OBJECT(editorState), "typing-attributes");
}
WebKitEditorState* webkitEditorStateCreate(WebPageProxy& page)
{
WebKitEditorState* editorState = WEBKIT_EDITOR_STATE(g_object_new(WEBKIT_TYPE_EDITOR_STATE, nullptr));
editorState->priv->page = &page;
webkitEditorStateChanged(editorState, page.editorState());
return editorState;
}
void webkitEditorStateChanged(WebKitEditorState* editorState, const EditorState& newState)
{
if (newState.isMissingPostLayoutData)
return;
unsigned typingAttributes = WEBKIT_EDITOR_TYPING_ATTRIBUTE_NONE;
const auto& postLayoutData = newState.postLayoutData();
if (postLayoutData.typingAttributes & AttributeBold)
typingAttributes |= WEBKIT_EDITOR_TYPING_ATTRIBUTE_BOLD;
if (postLayoutData.typingAttributes & AttributeItalics)
typingAttributes |= WEBKIT_EDITOR_TYPING_ATTRIBUTE_ITALIC;
if (postLayoutData.typingAttributes & AttributeUnderline)
typingAttributes |= WEBKIT_EDITOR_TYPING_ATTRIBUTE_UNDERLINE;
if (postLayoutData.typingAttributes & AttributeStrikeThrough)
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();
}
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;
}
gboolean webkit_editor_state_is_cut_available(WebKitEditorState* editorState)
{
g_return_val_if_fail(WEBKIT_IS_EDITOR_STATE(editorState), FALSE);
return editorState->priv->isCutAvailable;
}
gboolean webkit_editor_state_is_copy_available(WebKitEditorState* editorState)
{
g_return_val_if_fail(WEBKIT_IS_EDITOR_STATE(editorState), FALSE);
return editorState->priv->isCopyAvailable;
}
gboolean webkit_editor_state_is_paste_available(WebKitEditorState* editorState)
{
g_return_val_if_fail(WEBKIT_IS_EDITOR_STATE(editorState), FALSE);
return editorState->priv->isPasteAvailable;
}
gboolean webkit_editor_state_is_undo_available(WebKitEditorState* editorState)
{
g_return_val_if_fail(WEBKIT_IS_EDITOR_STATE(editorState), FALSE);
return editorState->priv->isUndoAvailable;
}
gboolean webkit_editor_state_is_redo_available(WebKitEditorState* editorState)
{
g_return_val_if_fail(WEBKIT_IS_EDITOR_STATE(editorState), FALSE);
return editorState->priv->isRedoAvailable;
}