/pidgin/main: e96acbe0e928: Move caret selection into GtkWebView...

Elliott Sales de Andrade qulogic at pidgin.im
Mon Jul 9 02:50:23 EDT 2012


Changeset: e96acbe0e928b7ad8003930cda792e4f6ff353c9
Author:	 Elliott Sales de Andrade <qulogic at pidgin.im>
Date:	 2012-07-08 23:41 -0400
Branch:	 default
URL: http://hg.pidgin.im/pidgin/main/rev/e96acbe0e928

Description:

Move caret selection into GtkWebView code.

diffstat:

 pidgin/gtkwebview.c          |  14 ++++++++++++++
 pidgin/gtkwebview.h          |  11 +++++++++++
 pidgin/plugins/xmppconsole.c |  17 +++--------------
 3 files changed, 28 insertions(+), 14 deletions(-)

diffs (96 lines):

diff --git a/pidgin/gtkwebview.c b/pidgin/gtkwebview.c
--- a/pidgin/gtkwebview.c
+++ b/pidgin/gtkwebview.c
@@ -563,6 +563,20 @@
 }
 
 void
+gtk_webview_load_html_string_with_selection(GtkWebView *webview, const char *html)
+{
+	gtk_webview_load_html_string(webview, html);
+	gtk_webview_safe_execute_script(webview,
+		"var s = window.getSelection();"
+		"var r = document.createRange();"
+		"var n = document.getElementById('caret');"
+		"r.setStartBefore(n);"
+		"r.setEndBefore(n);"
+		"s.removeAllRanges();"
+		"s.addRange(r);");
+}
+
+void
 gtk_webview_append_html(GtkWebView *webview, const char *html)
 {
 	GtkWebViewPriv *priv = GTK_WEBVIEW_GET_PRIVATE(webview);
diff --git a/pidgin/gtkwebview.h b/pidgin/gtkwebview.h
--- a/pidgin/gtkwebview.h
+++ b/pidgin/gtkwebview.h
@@ -122,6 +122,17 @@
 void gtk_webview_load_html_string(GtkWebView *webview, const char *html);
 
 /**
+ * Requests loading of the given content and sets the selection. You must
+ * include an anchor tag with id='caret' in the HTML string, which will be
+ * used to set the selection. This tag is then removed so that querying the
+ * WebView's HTML contents will no longer return it.
+ *
+ * @param webview The GtkWebView object
+ * @param html    The HTML content to load
+ */
+void gtk_webview_load_html_string_with_selection(GtkWebView *webview, const char *html);
+
+/**
  * Execute the JavaScript only after the webkit_webview_load_string
  * loads completely. We also guarantee that the scripts are executed
  * in the order they are called here. This is useful to avoid race
diff --git a/pidgin/plugins/xmppconsole.c b/pidgin/plugins/xmppconsole.c
--- a/pidgin/plugins/xmppconsole.c
+++ b/pidgin/plugins/xmppconsole.c
@@ -59,14 +59,6 @@
 	"span.value { color: #324aa4; }" \
 	"span.xmlns { color: #2cb12f; font-weight: bold;}" \
 "</style></head></html>"
-#define SET_CURSOR \
-	"var s = window.getSelection();" \
-	"var r = document.createRange();" \
-	"var n = document.getElementById('caret');" \
-	"r.setStartBefore(n);" \
-	"r.setEndBefore(n);" \
-	"s.removeAllRanges();" \
-	"s.addRange(r);"
 
 static char *
 xmlnode_to_pretty_str(xmlnode *node, int *len)
@@ -349,9 +341,8 @@
 				 g_random_int(),
 				 gtk_combo_box_get_active_text(GTK_COMBO_BOX(type_combo)));
 
-	gtk_webview_load_html_string(GTK_WEBVIEW(console->entry), stanza);
+	gtk_webview_load_html_string_with_selection(GTK_WEBVIEW(console->entry), stanza);
 	gtk_widget_grab_focus(console->entry);
-	gtk_webview_safe_execute_script(GTK_WEBVIEW(console->entry), SET_CURSOR);
 	g_free(to);
 	g_free(stanza);
 
@@ -509,9 +500,8 @@
 	                         *priority ? priority : "",
 	                         *priority ? "</priority>" : "");
 
-	gtk_webview_load_html_string(GTK_WEBVIEW(console->entry), stanza);
+	gtk_webview_load_html_string_with_selection(GTK_WEBVIEW(console->entry), stanza);
 	gtk_widget_grab_focus(console->entry);
-	gtk_webview_safe_execute_script(GTK_WEBVIEW(console->entry), SET_CURSOR);
 	g_free(stanza);
 	g_free(to);
 	g_free(status);
@@ -654,9 +644,8 @@
 	                         *thread ? thread : "",
 	                         *thread ? "</thread>" : "");
 
-	gtk_webview_load_html_string(GTK_WEBVIEW(console->entry), stanza);
+	gtk_webview_load_html_string_with_selection(GTK_WEBVIEW(console->entry), stanza);
 	gtk_widget_grab_focus(console->entry);
-	gtk_webview_safe_execute_script(GTK_WEBVIEW(console->entry), SET_CURSOR);
 	g_free(stanza);
 	g_free(to);
 	g_free(body);



More information about the Commits mailing list