pidgin: ac4e17d5: Setup WBFO in a WebView.

qulogic at pidgin.im qulogic at pidgin.im
Mon Jun 4 04:25:56 EDT 2012


----------------------------------------------------------------------
Revision: ac4e17d54aff8b9275bd29092a2c23bc9f132028
Parent:   f100b48348f534758d716bab2a2389d9468b38f6
Author:   qulogic at pidgin.im
Date:     06/03/12 03:40:47
Branch:   im.pidgin.pidgin
URL: http://d.pidgin.im/viewmtn/revision/info/ac4e17d54aff8b9275bd29092a2c23bc9f132028

Changelog: 

Setup WBFO in a WebView.

Though, really, I wish execCommand could work on arbitrary ranges
instead of needed to save and restore the selection.

Changes against parent f100b48348f534758d716bab2a2389d9468b38f6

  patched  pidgin/gtkwebview.c

-------------- next part --------------
============================================================
--- pidgin/gtkwebview.c	31b13c0d659cab9d3e2f93cd155df4bdf371c510
+++ pidgin/gtkwebview.c	8cbb4c8b795ee7287a2d92cb7a811a6c3a495671
@@ -868,116 +868,114 @@ gtk_webview_clear_formatting(GtkWebView 
 	g_object_unref(object);
 }
 
-void
-gtk_webview_toggle_bold(GtkWebView *webview)
+static void
+do_formatting(GtkWebView *webview, const char *name, const char *value)
 {
+	GtkWebViewPriv *priv = GTK_WEBVIEW_GET_PRIVATE(webview);
 	WebKitDOMDocument *dom;
+	WebKitDOMDOMWindow *win = NULL;
+	WebKitDOMDOMSelection *sel;
+	WebKitDOMRange *range = NULL;
 
 	dom = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(webview));
-	webkit_dom_document_exec_command(dom, "bold", FALSE, "");
+
+	if (priv->edit.wbfo) {
+		win = webkit_dom_document_get_default_view(dom);
+		sel = webkit_dom_dom_window_get_selection(win);
+		range = webkit_dom_dom_selection_get_range_at(sel, 0, NULL);
+		webkit_web_view_select_all(WEBKIT_WEB_VIEW(webview));
+	}
+
+	webkit_dom_document_exec_command(dom, name, FALSE, value);
+
+	if (priv->edit.wbfo) {
+		sel = webkit_dom_dom_window_get_selection(win);
+		webkit_dom_dom_selection_remove_all_ranges(sel);
+		webkit_dom_dom_selection_add_range(sel, range);
+	}
 }
 
 void
-gtk_webview_toggle_italic(GtkWebView *webview)
+gtk_webview_toggle_bold(GtkWebView *webview)
 {
-	WebKitDOMDocument *dom;
+	do_formatting(webview, "bold", "");
+}
 
-	dom = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(webview));
-	webkit_dom_document_exec_command(dom, "italic", FALSE, "");
+void
+gtk_webview_toggle_italic(GtkWebView *webview)
+{
+	do_formatting(webview, "italic", "");
 }
 
 void
 gtk_webview_toggle_underline(GtkWebView *webview)
 {
-	WebKitDOMDocument *dom;
-
-	dom = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(webview));
-	webkit_dom_document_exec_command(dom, "underline", FALSE, "");
+	do_formatting(webview, "underline", "");
 }
 
 void
 gtk_webview_toggle_strike(GtkWebView *webview)
 {
-	WebKitDOMDocument *dom;
-
-	dom = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(webview));
-	webkit_dom_document_exec_command(dom, "strikethrough", FALSE, "");
+	do_formatting(webview, "strikethrough", "");
 }
 
 gboolean
 gtk_webview_toggle_forecolor(GtkWebView *webview, const char *color)
 {
-	WebKitDOMDocument *dom;
+	do_formatting(webview, "foreColor", color);
 
-	dom = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(webview));
-	webkit_dom_document_exec_command(dom, "foreColor", FALSE, color);
-
 	return FALSE;
 }
 
 gboolean
 gtk_webview_toggle_backcolor(GtkWebView *webview, const char *color)
 {
-	WebKitDOMDocument *dom;
+	do_formatting(webview, "backColor", color);
 
-	dom = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(webview));
-	webkit_dom_document_exec_command(dom, "backColor", FALSE, color);
-
 	return FALSE;
 }
 
 gboolean
 gtk_webview_toggle_fontface(GtkWebView *webview, const char *face)
 {
-	WebKitDOMDocument *dom;
+	do_formatting(webview, "fontName", face);
 
-	dom = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(webview));
-	webkit_dom_document_exec_command(dom, "fontName", FALSE, face);
-
 	return FALSE;
 }
 
 void
 gtk_webview_font_set_size(GtkWebView *webview, gint size)
 {
-	WebKitDOMDocument *dom;
-	char *tmp;
-
-	dom = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(webview));
-	tmp = g_strdup_printf("%d", size);
-	webkit_dom_document_exec_command(dom, "fontSize", FALSE, tmp);
+	char *tmp = g_strdup_printf("%d", size);
+	do_formatting(webview, "fontSize", tmp);
 	g_free(tmp);
 }
 
 void
 gtk_webview_font_shrink(GtkWebView *webview)
 {
-	WebKitDOMDocument *dom;
 	gint fontsize;
 	char *tmp;
 
 	fontsize = gtk_webview_get_current_fontsize(webview);
 	fontsize = MAX(fontsize - 1, 1);
 
-	dom = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(webview));
 	tmp = g_strdup_printf("%d", fontsize);
-	webkit_dom_document_exec_command(dom, "fontSize", FALSE, tmp);
+	do_formatting(webview, "fontSize", tmp);
 	g_free(tmp);
 }
 
 void
 gtk_webview_font_grow(GtkWebView *webview)
 {
-	WebKitDOMDocument *dom;
 	gint fontsize;
 	char *tmp;
 
 	fontsize = gtk_webview_get_current_fontsize(webview);
 	fontsize = MIN(fontsize + 1, MAX_FONT_SIZE);
 
-	dom = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(webview));
 	tmp = g_strdup_printf("%d", fontsize);
-	webkit_dom_document_exec_command(dom, "fontSize", FALSE, tmp);
+	do_formatting(webview, "fontSize", tmp);
 	g_free(tmp);
 }
 


More information about the Commits mailing list