changeset 32562:4547ab591cfc

WebKit knows the current formatting of the selection better than we do, so don't bother saving it ourselves.
author Elliott Sales de Andrade <qulogic@pidgin.im>
date Mon, 27 Feb 2012 06:35:27 +0000
parents be0162cb27c4
children e63ca20258fb
files pidgin/gtkwebview.c pidgin/gtkwebview.h pidgin/gtkwebviewtoolbar.c
diffstat 3 files changed, 87 insertions(+), 105 deletions(-) [+]
line wrap: on
line diff
--- a/pidgin/gtkwebview.c	Mon Feb 27 03:15:42 2012 +0000
+++ b/pidgin/gtkwebview.c	Mon Feb 27 06:35:27 2012 +0000
@@ -68,16 +68,7 @@
 	GtkWebViewButtons format_functions;
 	struct {
 		gboolean wbfo:1;	/* Whole buffer formatting only. */
-		gboolean bold:1;
-		gboolean italic:1;
-		gboolean underline:1;
-		gboolean strike:1;
-		gchar *forecolor;
-		gchar *backcolor;
 		gchar *background;
-		gchar *fontface;
-		int fontsize;
-		/*GtkTextTag *link;*/
 	} edit;
 
 } GtkWebViewPriv;
@@ -319,21 +310,6 @@
 	if (!webkit_web_view_get_editable(WEBKIT_WEB_VIEW(webview)))
 		return;
 
-	priv->edit.bold = FALSE;
-	priv->edit.italic = FALSE;
-	priv->edit.underline = FALSE;
-	priv->edit.strike = FALSE;
-	priv->edit.fontsize = 0;
-
-	g_free(priv->edit.fontface);
-	priv->edit.fontface = NULL;
-
-	g_free(priv->edit.forecolor);
-	priv->edit.forecolor = NULL;
-
-	g_free(priv->edit.backcolor);
-	priv->edit.backcolor = NULL;
-
 	g_free(priv->edit.background);
 	priv->edit.background = NULL;
 
@@ -545,6 +521,7 @@
 
 	return g_string_free(str, FALSE);
 }
+
 void
 gtk_webview_safe_execute_script(GtkWebView *webview, const char *script)
 {
@@ -611,7 +588,8 @@
 	}
 }
 
-void gtk_webview_page_up(GtkWebView *webview)
+void
+gtk_webview_page_up(GtkWebView *webview)
 {
 	GtkWebViewPriv *priv = GTK_WEBVIEW_GET_PRIVATE(webview);
 	GtkAdjustment *vadj = priv->vadj;
@@ -628,7 +606,8 @@
 	gtk_adjustment_set_value(vadj, scroll_val);
 }
 
-void gtk_webview_page_down(GtkWebView *webview)
+void
+gtk_webview_page_down(GtkWebView *webview)
 {
 	GtkWebViewPriv *priv = GTK_WEBVIEW_GET_PRIVATE(webview);
 	GtkAdjustment *vadj = priv->vadj;
@@ -657,12 +636,12 @@
 void
 gtk_webview_setup_entry(GtkWebView *webview, PurpleConnectionFlags flags)
 {
-	GtkWebViewPriv *priv = GTK_WEBVIEW_GET_PRIVATE(webview);
 	GtkWebViewButtons buttons;
 
 	if (flags & PURPLE_CONNECTION_HTML) {
 		char color[8];
 		GdkColor fg_color, bg_color;
+		gboolean bold, italic, underline, strike;
 
 		buttons = GTK_WEBVIEW_ALL;
 
@@ -676,17 +655,19 @@
 		if (flags & PURPLE_CONNECTION_NO_URLDESC)
 			buttons &= ~GTK_WEBVIEW_LINKDESC;
 
+		gtk_webview_get_current_format(webview, &bold, &italic, &underline, &strike);
+
 		gtk_webview_set_format_functions(webview, GTK_WEBVIEW_ALL);
-		if (purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/conversations/send_bold") != priv->edit.bold)
+		if (purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/conversations/send_bold") != bold)
 			gtk_webview_toggle_bold(webview);
 
-		if (purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/conversations/send_italic") != priv->edit.italic)
+		if (purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/conversations/send_italic") != italic)
 			gtk_webview_toggle_italic(webview);
 
-		if (purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/conversations/send_underline") != priv->edit.underline)
+		if (purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/conversations/send_underline") != underline)
 			gtk_webview_toggle_underline(webview);
 
-		if (purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/conversations/send_strike") != priv->edit.strike)
+		if (purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/conversations/send_strike") != strike)
 			gtk_webview_toggle_strike(webview);
 
 		gtk_webview_toggle_fontface(webview,
@@ -705,24 +686,26 @@
 		{
 			gdk_color_parse(purple_prefs_get_string(PIDGIN_PREFS_ROOT "/conversations/fgcolor"),
 							&fg_color);
-			g_snprintf(color, sizeof(color), "#%02x%02x%02x",
-									fg_color.red   / 256,
-									fg_color.green / 256,
-									fg_color.blue  / 256);
+			g_snprintf(color, sizeof(color),
+			           "#%02x%02x%02x",
+			           fg_color.red   / 256,
+			           fg_color.green / 256,
+			           fg_color.blue  / 256);
 		} else
 			strcpy(color, "");
 
 		gtk_webview_toggle_forecolor(webview, color);
 
-		if(!(flags & PURPLE_CONNECTION_NO_BGCOLOR) &&
-		   strcmp(purple_prefs_get_string(PIDGIN_PREFS_ROOT "/conversations/bgcolor"), "") != 0)
+		if (!(flags & PURPLE_CONNECTION_NO_BGCOLOR) &&
+		    strcmp(purple_prefs_get_string(PIDGIN_PREFS_ROOT "/conversations/bgcolor"), "") != 0)
 		{
 			gdk_color_parse(purple_prefs_get_string(PIDGIN_PREFS_ROOT "/conversations/bgcolor"),
 							&bg_color);
-			g_snprintf(color, sizeof(color), "#%02x%02x%02x",
-									bg_color.red   / 256,
-									bg_color.green / 256,
-									bg_color.blue  / 256);
+			g_snprintf(color, sizeof(color),
+			           "#%02x%02x%02x",
+			           bg_color.red   / 256,
+			           bg_color.green / 256,
+			           bg_color.blue  / 256);
 		} else
 			strcpy(color, "");
 
@@ -777,50 +760,63 @@
                                gboolean *italic, gboolean *underline,
                                gboolean *strike)
 {
-	GtkWebViewPriv *priv = GTK_WEBVIEW_GET_PRIVATE(webview);
+	WebKitDOMDocument *dom;
+	dom = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(webview));
+
 	if (bold)
-		*bold = priv->edit.bold;
+		*bold = webkit_dom_document_query_command_state(dom, "bold");
 	if (italic)
-		*italic = priv->edit.italic;
+		*italic = webkit_dom_document_query_command_state(dom, "italic");
 	if (underline)
-		*underline = priv->edit.underline;
+		*underline = webkit_dom_document_query_command_state(dom, "underline");
 	if (strike)
-		*strike = priv->edit.strike;
+		*strike = webkit_dom_document_query_command_state(dom, "strikethrough");
 }
 
-const char *
+char *
 gtk_webview_get_current_fontface(GtkWebView *webview)
 {
-	GtkWebViewPriv *priv = GTK_WEBVIEW_GET_PRIVATE(webview);
-	return priv->edit.fontface;
+	WebKitDOMDocument *dom;
+	dom = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(webview));
+	return webkit_dom_document_query_command_value(dom, "fontName");
 }
 
-const char *
+char *
 gtk_webview_get_current_forecolor(GtkWebView *webview)
 {
-	GtkWebViewPriv *priv = GTK_WEBVIEW_GET_PRIVATE(webview);
-	return priv->edit.forecolor;
+	WebKitDOMDocument *dom;
+	dom = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(webview));
+	return webkit_dom_document_query_command_value(dom, "foreColor");
 }
 
-const char *
+char *
 gtk_webview_get_current_backcolor(GtkWebView *webview)
 {
-	GtkWebViewPriv *priv = GTK_WEBVIEW_GET_PRIVATE(webview);
-	return priv->edit.backcolor;
+	WebKitDOMDocument *dom;
+	dom = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(webview));
+	return webkit_dom_document_query_command_value(dom, "backColor");
 }
 
-const char *
+char *
 gtk_webview_get_current_background(GtkWebView *webview)
 {
 	GtkWebViewPriv *priv = GTK_WEBVIEW_GET_PRIVATE(webview);
-	return priv->edit.background;
+	return g_strdup(priv->edit.background);
 }
 
 gint
 gtk_webview_get_current_fontsize(GtkWebView *webview)
 {
-	GtkWebViewPriv *priv = GTK_WEBVIEW_GET_PRIVATE(webview);
-	return priv->edit.fontsize;
+	WebKitDOMDocument *dom;
+	gchar *text;
+	gint size;
+
+	dom = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(webview));
+	text = webkit_dom_document_query_command_value(dom, "fontSize");
+	size = atoi(text);
+	g_free(text);
+
+	return size;
 }
 
 gboolean
@@ -845,11 +841,8 @@
 void
 gtk_webview_toggle_bold(GtkWebView *webview)
 {
-	GtkWebViewPriv *priv = GTK_WEBVIEW_GET_PRIVATE(webview);
 	WebKitDOMDocument *dom;
 
-	priv->edit.bold = !priv->edit.bold;
-
 	dom = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(webview));
 	webkit_dom_document_exec_command(dom, "bold", FALSE, "");
 }
@@ -857,11 +850,8 @@
 void
 gtk_webview_toggle_italic(GtkWebView *webview)
 {
-	GtkWebViewPriv *priv = GTK_WEBVIEW_GET_PRIVATE(webview);
 	WebKitDOMDocument *dom;
 
-	priv->edit.italic = !priv->edit.italic;
-
 	dom = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(webview));
 	webkit_dom_document_exec_command(dom, "italic", FALSE, "");
 }
@@ -869,11 +859,8 @@
 void
 gtk_webview_toggle_underline(GtkWebView *webview)
 {
-	GtkWebViewPriv *priv = GTK_WEBVIEW_GET_PRIVATE(webview);
 	WebKitDOMDocument *dom;
 
-	priv->edit.underline = !priv->edit.underline;
-
 	dom = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(webview));
 	webkit_dom_document_exec_command(dom, "underline", FALSE, "");
 }
@@ -881,11 +868,8 @@
 void
 gtk_webview_toggle_strike(GtkWebView *webview)
 {
-	GtkWebViewPriv *priv = GTK_WEBVIEW_GET_PRIVATE(webview);
 	WebKitDOMDocument *dom;
 
-	priv->edit.strike = !priv->edit.strike;
-
 	dom = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(webview));
 	webkit_dom_document_exec_command(dom, "strikethrough", FALSE, "");
 }
@@ -893,14 +877,10 @@
 gboolean
 gtk_webview_toggle_forecolor(GtkWebView *webview, const char *color)
 {
-	GtkWebViewPriv *priv = GTK_WEBVIEW_GET_PRIVATE(webview);
 	WebKitDOMDocument *dom;
 
-	g_free(priv->edit.forecolor);
-	priv->edit.forecolor = g_strdup(color);
-
 	dom = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(webview));
-	webkit_dom_document_exec_command(dom, "forecolor", FALSE, color);
+	webkit_dom_document_exec_command(dom, "foreColor", FALSE, color);
 
 	return FALSE;
 }
@@ -908,14 +888,10 @@
 gboolean
 gtk_webview_toggle_backcolor(GtkWebView *webview, const char *color)
 {
-	GtkWebViewPriv *priv = GTK_WEBVIEW_GET_PRIVATE(webview);
 	WebKitDOMDocument *dom;
 
-	g_free(priv->edit.backcolor);
-	priv->edit.backcolor = g_strdup(color);
-
 	dom = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(webview));
-	webkit_dom_document_exec_command(dom, "backcolor", FALSE, color);
+	webkit_dom_document_exec_command(dom, "backColor", FALSE, color);
 
 	return FALSE;
 }
@@ -934,14 +910,10 @@
 gboolean
 gtk_webview_toggle_fontface(GtkWebView *webview, const char *face)
 {
-	GtkWebViewPriv *priv = GTK_WEBVIEW_GET_PRIVATE(webview);
 	WebKitDOMDocument *dom;
 
-	g_free(priv->edit.fontface);
-	priv->edit.fontface = g_strdup(face);
-
 	dom = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(webview));
-	webkit_dom_document_exec_command(dom, "fontname", FALSE, face);
+	webkit_dom_document_exec_command(dom, "fontName", FALSE, face);
 
 	return FALSE;
 }
@@ -949,15 +921,12 @@
 void
 gtk_webview_font_set_size(GtkWebView *webview, gint size)
 {
-	GtkWebViewPriv *priv = GTK_WEBVIEW_GET_PRIVATE(webview);
 	WebKitDOMDocument *dom;
 	char *tmp;
 
-	priv->edit.fontsize = size;
-
 	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);
+	webkit_dom_document_exec_command(dom, "fontSize", FALSE, tmp);
 	g_free(tmp);
 }
 
@@ -966,13 +935,15 @@
 {
 	GtkWebViewPriv *priv = GTK_WEBVIEW_GET_PRIVATE(webview);
 	WebKitDOMDocument *dom;
+	gint fontsize;
 	char *tmp;
 
-	priv->edit.fontsize = MAX(priv->edit.fontsize - 1, 1);
+	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", priv->edit.fontsize);
-	webkit_dom_document_exec_command(dom, "fontsize", FALSE, tmp);
+	tmp = g_strdup_printf("%d", fontsize);
+	webkit_dom_document_exec_command(dom, "fontSize", FALSE, tmp);
 	g_free(tmp);
 }
 
@@ -981,13 +952,15 @@
 {
 	GtkWebViewPriv *priv = GTK_WEBVIEW_GET_PRIVATE(webview);
 	WebKitDOMDocument *dom;
+	gint fontsize;
 	char *tmp;
 
-	priv->edit.fontsize = MIN(priv->edit.fontsize + 1, MAX_FONT_SIZE);
+	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", priv->edit.fontsize);
-	webkit_dom_document_exec_command(dom, "fontsize", FALSE, tmp);
+	tmp = g_strdup_printf("%d", fontsize);
+	webkit_dom_document_exec_command(dom, "fontSize", FALSE, tmp);
 	g_free(tmp);
 }
 
--- a/pidgin/gtkwebview.h	Mon Feb 27 03:15:42 2012 +0000
+++ b/pidgin/gtkwebview.h	Mon Feb 27 06:35:27 2012 +0000
@@ -242,7 +242,7 @@
  *
  * @return A string containing the font face or @c NULL if none is set.
  */
-const char *gtk_webview_get_current_fontface(GtkWebView *webview);
+char *gtk_webview_get_current_fontface(GtkWebView *webview);
 
 /**
  * Returns a string containing the selected foreground color at the current
@@ -252,7 +252,7 @@
  *
  * @return A string containing the foreground color or @c NULL if none is set.
  */
-const char *gtk_webview_get_current_forecolor(GtkWebView *webview);
+char *gtk_webview_get_current_forecolor(GtkWebView *webview);
 
 /**
  * Returns a string containing the selected font background color at the current
@@ -262,7 +262,7 @@
  *
  * @return A string containing the background color or @c NULL if none is set.
  */
-const char *gtk_webview_get_current_backcolor(GtkWebView *webview);
+char *gtk_webview_get_current_backcolor(GtkWebView *webview);
 
 /**
  * Returns a string containing the selected background color at the current
@@ -272,7 +272,7 @@
  *
  * @return A string containg the background color or @c NULL if none is set.
  */
-const char *gtk_webview_get_current_background(GtkWebView *webview);
+char *gtk_webview_get_current_background(GtkWebView *webview);
 
 /**
  * Returns a integer containing the selected HTML font size at the current
--- a/pidgin/gtkwebviewtoolbar.c	Mon Feb 27 03:15:42 2012 +0000
+++ b/pidgin/gtkwebviewtoolbar.c	Mon Feb 27 06:35:27 2012 +0000
@@ -253,7 +253,7 @@
 	priv = GTK_WEBVIEWTOOLBAR_GET_PRIVATE(toolbar);
 
 	if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(font))) {
-		const char *fontname = gtk_webview_get_current_fontface(GTK_WEBVIEW(toolbar->webview));
+		char *fontname = gtk_webview_get_current_fontface(GTK_WEBVIEW(toolbar->webview));
 
 		if (!priv->font_dialog) {
 			priv->font_dialog = gtk_font_selection_dialog_new(_("Select Font"));
@@ -279,6 +279,8 @@
 		}
 
 		gtk_window_present(GTK_WINDOW(priv->font_dialog));
+
+		g_free(fontname);
 	} else {
 		cancel_toolbar_font(font, toolbar);
 	}
@@ -341,7 +343,7 @@
 	if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(color))) {
 		GtkWidget *colorsel;
 		GdkColor fgcolor;
-		const char *color = gtk_webview_get_current_forecolor(GTK_WEBVIEW(toolbar->webview));
+		char *color = gtk_webview_get_current_forecolor(GTK_WEBVIEW(toolbar->webview));
 
 		if (!priv->fgcolor_dialog) {
 			priv->fgcolor_dialog = gtk_color_selection_dialog_new(_("Select Text Color"));
@@ -360,6 +362,8 @@
 		}
 
 		gtk_window_present(GTK_WINDOW(priv->fgcolor_dialog));
+
+		g_free(color);
 	} else {
 		cancel_toolbar_fgcolor(color, toolbar);
 	}
@@ -432,7 +436,7 @@
 	if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(color))) {
 		GtkWidget *colorsel;
 		GdkColor bgcolor;
-		const char *color = gtk_webview_get_current_backcolor(GTK_WEBVIEW(toolbar->webview));
+		char *color = gtk_webview_get_current_backcolor(GTK_WEBVIEW(toolbar->webview));
 
 		if (!priv->bgcolor_dialog) {
 			priv->bgcolor_dialog = gtk_color_selection_dialog_new(_("Select Background Color"));
@@ -451,6 +455,8 @@
 		}
 
 		gtk_window_present(GTK_WINDOW(priv->bgcolor_dialog));
+
+		g_free(color);
 	} else {
 		cancel_toolbar_bgcolor(color, toolbar);
 	}
@@ -1098,8 +1104,7 @@
 {
 	GtkWebViewToolbarPriv *priv = GTK_WEBVIEWTOOLBAR_GET_PRIVATE(toolbar);
 	gboolean bold, italic, underline, strike;
-	const char *tmp;
-	const char *tmp2;
+	char *tmp, *tmp2;
 	GtkLabel *label = GTK_LABEL(priv->font_label);
 
 	gtk_label_set_label(label, _("_Font"));
@@ -1158,6 +1163,7 @@
 		gtk_label_set_markup_with_mnemonic(label, markup);
 		g_free(markup);
 	}
+	g_free(tmp);
 
 	tmp = gtk_webview_get_current_forecolor(GTK_WEBVIEW(toolbar->webview));
 	toggle_button_set_active_block(GTK_TOGGLE_BUTTON(priv->fgcolor),
@@ -1168,6 +1174,7 @@
 		gtk_label_set_markup_with_mnemonic(label, markup);
 		g_free(markup);
 	}
+	g_free(tmp);
 
 	tmp = gtk_webview_get_current_backcolor(GTK_WEBVIEW(toolbar->webview));
 	tmp2 = gtk_webview_get_current_background(GTK_WEBVIEW(toolbar->webview));
@@ -1179,6 +1186,8 @@
 		gtk_label_set_markup_with_mnemonic(label, markup);
 		g_free(markup);
 	}
+	g_free(tmp);
+	g_free(tmp2);
 }
 
 static void