[evolution/webkit-composer: 47/54] Make text-alignment buttons work



commit b6634ae8598f5938e82d8ee683f6b2e04f8cc23d
Author: Dan VrÃtil <dvratil redhat com>
Date:   Fri Aug 3 11:38:07 2012 +0200

    Make text-alignment buttons work
    
    The 'current-value' property of the action group is binded
    with 'alignment' property of EEditorSelection.

 widgets/editor/e-editor-actions.c   |   38 +++---------
 widgets/editor/e-editor-selection.c |  113 ++++++++++++++++++++++++++++++++++-
 widgets/editor/e-editor-selection.h |    7 ++
 3 files changed, 126 insertions(+), 32 deletions(-)
---
diff --git a/widgets/editor/e-editor-actions.c b/widgets/editor/e-editor-actions.c
index c1ec31b..8278831 100644
--- a/widgets/editor/e-editor-actions.c
+++ b/widgets/editor/e-editor-actions.c
@@ -774,35 +774,6 @@ action_insert_text_file_cb (GtkAction *action,
 }
 
 static void
-action_justify_cb (GtkRadioAction *action,
-                   GtkRadioAction *current,
-                   EEditor *editor)
-{
-	WebKitDOMDocument *document;
-	const gchar *command;
-
-	document = webkit_web_view_get_dom_document (
-			WEBKIT_WEB_VIEW (e_editor_get_editor_widget (editor)));
-
-	switch (gtk_radio_action_get_current_value (current))
-	{
-		case E_EDITOR_SELECTION_ALIGNMENT_CENTER:
-			command = "justifyCenter";
-			break;
-
-		case E_EDITOR_SELECTION_ALIGNMENT_LEFT:
-			command = "justifyLeft";
-			break;
-
-		case E_EDITOR_SELECTION_ALIGNMENT_RIGHT:
-			command = "justifyRight";
-			break;
-	}
-
-	webkit_dom_document_exec_command (document, command, FALSE, "");
-}
-
-static void
 action_language_cb (GtkToggleAction *action,
                     EEditor *editor)
 {
@@ -2078,7 +2049,7 @@ editor_actions_init (EEditor *editor)
 		action_group, core_justify_entries,
 		G_N_ELEMENTS (core_justify_entries),
 		E_EDITOR_SELECTION_ALIGNMENT_LEFT,
-		G_CALLBACK (action_justify_cb), editor);
+		NULL, NULL);
 	gtk_action_group_add_radio_actions (
 		action_group, core_mode_entries,
 		G_N_ELEMENTS (core_mode_entries),
@@ -2200,6 +2171,13 @@ editor_actions_init (EEditor *editor)
 		ACTION (PASTE), "sensitive",
 		G_BINDING_SYNC_CREATE);
 
+	/* This is connected to JUSTIFY_LEFT action only, but
+	 * it automatically applies on all actions in the group. */
+	g_object_bind_property (
+		editor->priv->selection, "alignment",
+		ACTION (JUSTIFY_LEFT), "current-value",
+		G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);
+
 	g_object_bind_property (
 		editor->priv->selection, "bold",
 		ACTION (BOLD), "active",
diff --git a/widgets/editor/e-editor-selection.c b/widgets/editor/e-editor-selection.c
index a98ff90..78eba97 100644
--- a/widgets/editor/e-editor-selection.c
+++ b/widgets/editor/e-editor-selection.c
@@ -52,6 +52,7 @@ G_DEFINE_TYPE (
 enum {
 	PROP_0,
 	PROP_WEBVIEW,
+	PROP_ALIGNMENT,
 	PROP_BACKGROUND_COLOR,
 	PROP_BOLD,
 	PROP_FONT_NAME,
@@ -133,6 +134,7 @@ static void
 webview_selection_changed (WebKitWebView *webview,
 			   EEditorSelection *selection)
 {
+	g_object_notify (G_OBJECT (selection), "alignment");
 	g_object_notify (G_OBJECT (selection), "background-color");
 	g_object_notify (G_OBJECT (selection), "bold");
 	g_object_notify (G_OBJECT (selection), "font-name");
@@ -168,6 +170,12 @@ e_editor_selection_get_property (GObject *object,
 	EEditorSelection *selection = E_EDITOR_SELECTION (object);
 
 	switch (property_id) {
+		case PROP_ALIGNMENT:
+			g_value_set_int (value,
+					e_editor_selection_get_alignment (
+					selection));
+			return;
+
 		case PROP_BACKGROUND_COLOR:
 			g_value_set_string (value,
 				e_editor_selection_get_background_color (
@@ -252,6 +260,11 @@ e_editor_selection_set_property (GObject *object,
 				selection, g_value_get_object (value));
 			return;
 
+		case PROP_ALIGNMENT:
+			e_editor_selection_set_alignment (
+				selection, g_value_get_int (value));
+			return;
+
 		case PROP_BACKGROUND_COLOR:
 			e_editor_selection_set_background_color (
 				selection, g_value_get_string (value));
@@ -339,13 +352,25 @@ e_editor_selection_class_init (EEditorSelectionClass *klass)
 		PROP_WEBVIEW,
 		g_param_spec_object (
 			"webview",
-		        NULL,
-		        NULL,
+			NULL,
+			NULL,
 		        WEBKIT_TYPE_WEB_VIEW,
 		        G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
 
 	g_object_class_install_property (
 		object_class,
+		PROP_ALIGNMENT,
+		g_param_spec_int (
+			"alignment",
+			NULL,
+			NULL,
+			E_EDITOR_SELECTION_ALIGNMENT_LEFT,
+			E_EDITOR_SELECTION_ALIGNMENT_RIGHT,
+			E_EDITOR_SELECTION_ALIGNMENT_LEFT,
+			G_PARAM_READWRITE));
+
+	g_object_class_install_property (
+		object_class,
 		PROP_BACKGROUND_COLOR,
 		g_param_spec_string (
 			"background-color",
@@ -530,6 +555,90 @@ e_editor_selection_replace (EEditorSelection *selection,
 	webkit_dom_range_insert_node (range, WEBKIT_DOM_NODE (frag), NULL);
 }
 
+EEditorSelectionAlignment
+e_editor_selection_get_alignment (EEditorSelection *selection)
+{
+	WebKitDOMRange *range;
+	WebKitDOMNode *node;
+	WebKitDOMElement *element;
+	WebKitDOMCSSStyleDeclaration *style;
+	gchar *value;
+	EEditorSelectionAlignment alignment;
+
+	g_return_val_if_fail (
+		E_IS_EDITOR_SELECTION (selection),
+		E_EDITOR_SELECTION_ALIGNMENT_LEFT);
+
+	range = editor_selection_get_current_range (selection);
+	if (!range) {
+		return E_EDITOR_SELECTION_ALIGNMENT_LEFT;
+	}
+
+	node = webkit_dom_range_get_start_container (range, NULL);
+	if (!node) {
+		return E_EDITOR_SELECTION_ALIGNMENT_LEFT;
+	}
+
+	if (!WEBKIT_DOM_IS_ELEMENT (node)) {
+		element = webkit_dom_node_get_parent_element (node);
+	} else {
+		element = WEBKIT_DOM_ELEMENT (node);
+	}
+
+	style = webkit_dom_element_get_style (element);
+	value = webkit_dom_css_style_declaration_get_property_value (
+				style, "text-align");
+
+	if (!value || !*value ||
+	    (g_ascii_strncasecmp (value, "left", 4) == 0)) {
+		alignment = E_EDITOR_SELECTION_ALIGNMENT_LEFT;
+	} else if (g_ascii_strncasecmp (value, "center", 6) == 0) {
+		alignment = E_EDITOR_SELECTION_ALIGNMENT_CENTER;
+	} else if (g_ascii_strncasecmp (value, "right", 5) == 0) {
+		alignment = E_EDITOR_SELECTION_ALIGNMENT_RIGHT;
+	} else {
+		alignment = E_EDITOR_SELECTION_ALIGNMENT_LEFT;
+	}
+
+	g_free (value);
+
+	return alignment;
+}
+
+void
+e_editor_selection_set_alignment (EEditorSelection *selection,
+				  EEditorSelectionAlignment alignment)
+{
+	WebKitDOMDocument *document;
+	const gchar *command;
+
+	g_return_if_fail (E_IS_EDITOR_SELECTION (selection));
+
+	if (e_editor_selection_get_alignment (selection) == alignment) {
+		return;
+	}
+
+	switch (alignment) {
+		case E_EDITOR_SELECTION_ALIGNMENT_CENTER:
+			command = "justifyCenter";
+			break;
+
+		case E_EDITOR_SELECTION_ALIGNMENT_LEFT:
+			command = "justifyLeft";
+			break;
+
+		case E_EDITOR_SELECTION_ALIGNMENT_RIGHT:
+			command = "justifyRight";
+			break;
+	}
+
+	document = webkit_web_view_get_dom_document (selection->priv->webview);
+	webkit_dom_document_exec_command (document, command, FALSE, "");
+
+	g_object_notify (G_OBJECT (selection), "alignment");
+}
+
+
 const gchar *
 e_editor_selection_get_background_color	(EEditorSelection *selection)
 {
diff --git a/widgets/editor/e-editor-selection.h b/widgets/editor/e-editor-selection.h
index ce3732f..897d2eb 100644
--- a/widgets/editor/e-editor-selection.h
+++ b/widgets/editor/e-editor-selection.h
@@ -95,6 +95,13 @@ GType			e_editor_selection_get_type 	(void);
 
 EEditorSelection *	e_editor_selection_new		(WebKitWebView *parent_view);
 
+void			e_editor_selection_set_alignment
+							(EEditorSelection *selection,
+							 EEditorSelectionAlignment alignment);
+EEditorSelectionAlignment
+			e_editor_selection_get_alignment
+							(EEditorSelection *selection);
+
 void			e_editor_selection_set_background_color
 							(EEditorSelection *selection,
 							 const gchar *color);



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