[evolution/webkit-composer: 25/111] Make changing color work



commit dffff9e734e313ed670aa7581494b0c5f6ffffeb
Author: Dan VrÃtil <dvratil redhat com>
Date:   Fri Aug 3 17:01:59 2012 +0200

    Make changing color work
    
    The 'current-color' property of EColorCombo is binded to 'font-color'
    property of EEditorSelection, so that any change is handled automatically.

 e-util/e-color-combo.c      |    5 +-
 e-util/e-editor-selection.c |   97 +++++++++++++++++++++++++++----------------
 e-util/e-editor-selection.h |    7 ++-
 e-util/e-editor.c           |    4 ++
 4 files changed, 71 insertions(+), 42 deletions(-)
---
diff --git a/e-util/e-color-combo.c b/e-util/e-color-combo.c
index bfc8544..400d606 100644
--- a/e-util/e-color-combo.c
+++ b/e-util/e-color-combo.c
@@ -558,7 +558,7 @@ e_color_combo_class_init (EColorComboClass *klass)
 			"current-color",
 			"Current color",
 			"The currently selected color",
-			GDK_TYPE_COLOR,
+			GDK_TYPE_RGBA,
 			G_PARAM_READWRITE));
 
 	g_object_class_install_property (
@@ -568,7 +568,7 @@ e_color_combo_class_init (EColorComboClass *klass)
 			"default-color",
 			"Default color",
 			"The color associated with the default button",
-			GDK_TYPE_COLOR,
+			GDK_TYPE_RGBA,
 			G_PARAM_CONSTRUCT |
 			G_PARAM_READWRITE));
 
@@ -837,7 +837,6 @@ e_color_combo_set_current_color (EColorCombo *combo,
 		gdk_rgba_free (combo->priv->current_color);
 	}
 
-
 	combo->priv->current_color = gdk_rgba_copy (color);
 
 	gtk_color_chooser_set_rgba (
diff --git a/e-util/e-editor-selection.c b/e-util/e-editor-selection.c
index 8eda4f5..73f144d 100644
--- a/e-util/e-editor-selection.c
+++ b/e-util/e-editor-selection.c
@@ -23,6 +23,8 @@
 #include "e-editor-selection.h"
 #include "e-editor.h"
 
+#include <e-util/e-util.h>
+
 #include <webkit/webkit.h>
 #include <webkit/webkitdom.h>
 #include <string.h>
@@ -69,6 +71,8 @@ enum {
 	PROP_UNDERLINE,
 };
 
+static const GdkRGBA black = { 0 };
+
 static WebKitDOMElement *
 find_parent_element_by_type (WebKitDOMNode *node, GType type)
 {
@@ -145,6 +149,32 @@ get_has_style (EEditorSelection *selection,
 	return result;
 }
 
+static gchar *
+get_font_property (EEditorSelection *selection,
+		   const gchar *font_property)
+{
+	WebKitDOMRange *range;
+	WebKitDOMNode *node;
+	WebKitDOMElement *element;
+	gchar *value;
+
+	range = editor_selection_get_current_range (selection);
+	if (!range) {
+		return NULL;
+	}
+
+	node = webkit_dom_range_get_common_ancestor_container (range, NULL);
+	element = find_parent_element_by_type (
+			node, WEBKIT_TYPE_DOM_HTML_FONT_ELEMENT);
+	if (!element) {
+		return NULL;
+	}
+
+	g_object_get (G_OBJECT (element), font_property, &value, NULL);
+
+	return value;
+}
+
 static void
 webview_selection_changed (WebKitWebView *webview,
 			   EEditorSelection *selection)
@@ -183,6 +213,7 @@ e_editor_selection_get_property (GObject *object,
 				 GValue *value,
 				 GParamSpec *pspec)
 {
+	GdkRGBA rgba = { 0 };
 	EEditorSelection *selection = E_EDITOR_SELECTION (object);
 
 	switch (property_id) {
@@ -212,8 +243,8 @@ e_editor_selection_get_property (GObject *object,
 			return;
 
 		case PROP_FONT_COLOR:
-			g_value_set_string (value,
-				e_editor_selection_get_font_color (selection));
+			e_editor_selection_get_font_color (selection, &rgba);
+			g_value_set_boxed (value, &rgba);
 			return;
 
 		case PROP_BLOCK_FORMAT:
@@ -296,7 +327,7 @@ e_editor_selection_set_property (GObject *object,
 
 		case PROP_FONT_COLOR:
 			e_editor_selection_set_font_color (
-				selection, g_value_get_string (value));
+				selection, g_value_get_boxed (value));
 			return;
 
 		case PROP_FONT_NAME:
@@ -433,11 +464,11 @@ e_editor_selection_class_init (EEditorSelectionClass *klass)
 	g_object_class_install_property (
 		object_class,
 		PROP_FONT_COLOR,
-		g_param_spec_string (
+		g_param_spec_boxed (
 			"font-color",
 			NULL,
 			NULL,
-			NULL,
+			GDK_TYPE_RGBA,
 			G_PARAM_READWRITE));
 
 	g_object_class_install_property (
@@ -835,36 +866,42 @@ e_editor_selection_set_bold (EEditorSelection *selection,
 	g_object_notify (G_OBJECT (selection), "bold");
 }
 
-const gchar *
-e_editor_selection_get_font_color (EEditorSelection *selection)
+void
+e_editor_selection_get_font_color (EEditorSelection *selection,
+				   GdkRGBA *rgba)
 {
-	WebKitDOMNode *node;
-	WebKitDOMRange *range;
-	WebKitDOMCSSStyleDeclaration *css;
-
-	g_return_val_if_fail (E_IS_EDITOR_SELECTION (selection), NULL);
-
-	range = editor_selection_get_current_range (selection);
-	node = webkit_dom_range_get_common_ancestor_container (range, NULL);
+	gchar *color;
+	g_return_if_fail (E_IS_EDITOR_SELECTION (selection));
 
-	g_free (selection->priv->font_color);
-	css = webkit_dom_element_get_style (WEBKIT_DOM_ELEMENT (node));
-	selection->priv->font_color =
-		webkit_dom_css_style_declaration_get_property_value (css, "color");
+	color = get_font_property (selection, "color");
+	if (!color) {
+		*rgba = black;
+		return;
+	}
 
-	return selection->priv->font_color;
+	gdk_rgba_parse (rgba, color);
+	g_free (color);
 }
 
 void
 e_editor_selection_set_font_color (EEditorSelection *selection,
-				   const gchar *color)
+				   const GdkRGBA *rgba)
 {
 	WebKitDOMDocument *document;
+	gchar *color;
 
 	g_return_if_fail (E_IS_EDITOR_SELECTION (selection));
 
+	if (!rgba) {
+		rgba = &black;
+	}
+
+	color = g_strdup_printf ("#%06x", e_rgba_to_value ((GdkRGBA *) rgba));
+
 	document = webkit_web_view_get_dom_document (selection->priv->webview);
-	webkit_dom_document_exec_command (document, "foreColor", FALSE, "");
+	webkit_dom_document_exec_command (document, "foreColor", FALSE, color);
+
+	g_free (color);
 
 	g_object_notify (G_OBJECT (selection), "font-color");
 }
@@ -906,9 +943,6 @@ e_editor_selection_set_font_name (EEditorSelection *selection,
 guint
 e_editor_selection_get_font_size (EEditorSelection *selection)
 {
-	WebKitDOMNode *node;
-	WebKitDOMElement *element;
-	WebKitDOMRange *range;
 	gchar *size;
 	gint size_int;
 
@@ -916,20 +950,11 @@ e_editor_selection_get_font_size (EEditorSelection *selection)
 		E_IS_EDITOR_SELECTION (selection),
 		E_EDITOR_SELECTION_FONT_SIZE_NORMAL);
 
-	range = editor_selection_get_current_range (selection);
-	if (!range) {
-		return E_EDITOR_SELECTION_FONT_SIZE_NORMAL;
-	}
-
-	node = webkit_dom_range_get_common_ancestor_container (range, NULL);
-	element = find_parent_element_by_type (
-			node, WEBKIT_TYPE_DOM_HTML_FONT_ELEMENT);
-	if (!element) {
+	size = get_font_property (selection, "size");
+	if (!size) {
 		return E_EDITOR_SELECTION_FONT_SIZE_NORMAL;
 	}
 
-	size = webkit_dom_html_font_element_get_size (
-			WEBKIT_DOM_HTML_FONT_ELEMENT (element));
 	size_int = atoi (size);
 	g_free (size);
 
diff --git a/e-util/e-editor-selection.h b/e-util/e-editor-selection.h
index 63b6737..eab701e 100644
--- a/e-util/e-editor-selection.h
+++ b/e-util/e-editor-selection.h
@@ -130,9 +130,10 @@ guint			e_editor_selection_get_font_size
 
 void			e_editor_selection_set_font_color
 							(EEditorSelection *selection,
-							 const gchar *color);
-const gchar *		e_editor_selection_get_font_color
-							(EEditorSelection *selection);
+							 const GdkRGBA *color);
+void			e_editor_selection_get_font_color
+							(EEditorSelection *selection,
+							 GdkRGBA *color);
 
 void			e_editor_selection_set_block_format
 							(EEditorSelection *selection,
diff --git a/e-util/e-editor.c b/e-util/e-editor.c
index c558e52..238f166 100644
--- a/e-util/e-editor.c
+++ b/e-util/e-editor.c
@@ -185,6 +185,10 @@ editor_constructed (GObject *object)
 	gtk_toolbar_insert (toolbar, tool_item, 0);
 	priv->color_combo_box = g_object_ref (widget);
 	gtk_widget_show_all (GTK_WIDGET (tool_item));
+	g_object_bind_property (
+		priv->color_combo_box, "current-color",
+		priv->selection, "font-color",
+		G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);
 
 	tool_item = gtk_tool_item_new ();
 	widget = e_action_combo_box_new_with_action (



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