[evolution/webkit-composer: 34/111] Implement 'Paste Quotation' action



commit 48dcc6cdac1da60c4b14c7c0604bbab64ff0a0bf
Author: Dan VrÃtil <dvratil redhat com>
Date:   Thu Aug 9 12:49:39 2012 +0200

    Implement 'Paste Quotation' action

 e-util/e-editor-actions.c   |    9 +-----
 e-util/e-editor-selection.c |   15 +++++++++++
 e-util/e-editor-widget.c    |   58 +++++++++++++++++++++++++++++++++++++++++++
 e-util/e-editor-widget.h    |    5 +++
 4 files changed, 80 insertions(+), 7 deletions(-)
---
diff --git a/e-util/e-editor-actions.c b/e-util/e-editor-actions.c
index c09c63d..6627c19 100644
--- a/e-util/e-editor-actions.c
+++ b/e-util/e-editor-actions.c
@@ -889,13 +889,8 @@ static void
 action_paste_quote_cb (GtkAction *action,
                        EEditor *editor)
 {
-	/* FIXME WEBKIT */
-	/*
-	GtkHTML *html;
-
-	html = gtkhtml_editor_get_html (editor);
-	gtk_html_paste (html, TRUE);
-	*/
+	e_editor_widget_paste_clipboard_quoted (
+		e_editor_get_editor_widget (editor));
 }
 
 static void
diff --git a/e-util/e-editor-selection.c b/e-util/e-editor-selection.c
index a9ee54d..e592ed8 100644
--- a/e-util/e-editor-selection.c
+++ b/e-util/e-editor-selection.c
@@ -123,6 +123,21 @@ get_has_style (EEditorSelection *selection,
 		result = ((tag_len == strlen (element_tag)) &&
 				(g_ascii_strncasecmp (element_tag, style_tag, tag_len) == 0));
 
+		/* Special case: <blockquote type=cite> marks quotation, while
+		 * just <blockquote> is used for indentation. If the <blockquote>
+		 * has type=cite, then ignore it */
+		if (result && g_ascii_strncasecmp (element_tag, "blockquote", 10) == 0) {
+			if (webkit_dom_element_has_attribute (element, "type")) {
+				gchar *type;
+				type = webkit_dom_element_get_attribute (
+						element, "type");
+				if (g_ascii_strncasecmp (type, "cite", 4) == 0) {
+					result = FALSE;
+				}
+				g_free (type);
+			}
+		}
+
 		g_free (element_tag);
 
 		if (result) {
diff --git a/e-util/e-editor-widget.c b/e-util/e-editor-widget.c
index f5fed8c..5cbe9ec 100644
--- a/e-util/e-editor-widget.c
+++ b/e-util/e-editor-widget.c
@@ -297,6 +297,50 @@ editor_widget_key_release_event (GtkWidget *gtk_widget,
 }
 
 static void
+clipboard_text_received (GtkClipboard *clipboard,
+			 const gchar *text,
+			 EEditorWidget *widget)
+{
+	gchar *html, *escaped_text;
+	WebKitDOMDocument *document;
+	WebKitDOMElement *element;
+
+	/* This is a little trick to escape any HTML characters (like <, > or &).
+	 * <textarea> automatically replaces all these unsafe characters
+	 * by &lt;, &gt; etc. */
+	document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (widget));
+	element = webkit_dom_document_create_element (document, "TEXTAREA", NULL);
+	webkit_dom_html_element_set_inner_html (
+		WEBKIT_DOM_HTML_ELEMENT (element), text, NULL);
+	escaped_text = webkit_dom_html_element_get_inner_html (
+		WEBKIT_DOM_HTML_ELEMENT (element));
+	g_object_unref (element);
+
+	html = g_strconcat (
+		"<blockquote type=\"cite\"><pre>",
+		escaped_text, "</pre></blockquote>", NULL);
+	e_editor_selection_insert_html (widget->priv->selection, html);
+
+	g_free (escaped_text);
+	g_free (html);
+}
+
+static void
+editor_widget_paste_clipboard_quoted (EEditorWidget *widget)
+{
+	GtkClipboard *clipboard;
+
+	clipboard = gtk_clipboard_get_for_display (
+			gdk_display_get_default (),
+			GDK_SELECTION_CLIPBOARD);
+
+	gtk_clipboard_request_text (
+		clipboard,
+		(GtkClipboardTextReceivedFunc) clipboard_text_received,
+		widget);
+}
+
+static void
 e_editor_widget_get_property (GObject *object,
 			      guint property_id,
 			      GValue *value,
@@ -430,6 +474,8 @@ e_editor_widget_class_init (EEditorWidgetClass *klass)
 	widget_class = GTK_WIDGET_CLASS (klass);
 	widget_class->key_release_event = editor_widget_key_release_event;
 
+	klass->paste_clipboard_quoted = editor_widget_paste_clipboard_quoted;
+
 	g_object_class_install_property (
 		object_class,
 		PROP_MODE,
@@ -777,3 +823,15 @@ e_editor_widget_set_text_html (EEditorWidget *widget,
 	webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (widget), text, NULL);
 }
 
+void
+e_editor_widget_paste_clipboard_quoted (EEditorWidget *widget)
+{
+	EEditorWidgetClass *klass;
+
+	g_return_if_fail (E_IS_EDITOR_WIDGET (widget));
+
+	klass = E_EDITOR_WIDGET_GET_CLASS (widget);
+	g_return_if_fail (klass->paste_clipboard_quoted != NULL);
+
+	klass->paste_clipboard_quoted (widget);
+}
diff --git a/e-util/e-editor-widget.h b/e-util/e-editor-widget.h
index 0affadf..1144129 100644
--- a/e-util/e-editor-widget.h
+++ b/e-util/e-editor-widget.h
@@ -72,6 +72,8 @@ struct _EEditorWidget {
 
 struct _EEditorWidgetClass {
 	WebKitWebViewClass parent_class;
+
+	void	(*paste_clipboard_quoted)	(EEditorWidget *widget);
 };
 
 GType		e_editor_widget_get_type 	(void);
@@ -116,6 +118,9 @@ gchar *		e_editor_widget_get_text_plain	(EEditorWidget *widget);
 void		e_editor_widget_set_text_html	(EEditorWidget *widget,
 						 const gchar *text);
 
+void		e_editor_widget_paste_clipboard_quoted
+						(EEditorWidget *widget);
+
 G_END_DECLS
 
 #endif /* E_EDITOR_WIDGET_H */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]