[evolution] EHTMLEditorSelection - Introduce e_html_editor_selection_is_collapsed and use it where possible



commit 0a4e3a2492e11481c6fd32fddad0f3faf7735b31
Author: Tomas Popela <tpopela redhat com>
Date:   Thu Jul 31 12:24:40 2014 +0200

    EHTMLEditorSelection - Introduce e_html_editor_selection_is_collapsed and use it where possible
    
    Before we were comparing the selection text to look if the selection is
    collapsed. Use webkit_dom_range_get_collapsed instead.

 e-util/e-html-editor-selection.c |   32 +++++++++++++++++++++++++++-----
 e-util/e-html-editor-selection.h |    2 ++
 e-util/e-html-editor-view.c      |    4 ++--
 3 files changed, 31 insertions(+), 7 deletions(-)
---
diff --git a/e-util/e-html-editor-selection.c b/e-util/e-html-editor-selection.c
index 0ae1780..a47fcf9 100644
--- a/e-util/e-html-editor-selection.c
+++ b/e-util/e-html-editor-selection.c
@@ -985,6 +985,28 @@ e_html_editor_selection_replace_caret_word (EHTMLEditorSelection *selection,
 }
 
 /**
+ * e_html_editor_selection_is_collapsed:
+ * @selection: an #EHTMLEditorSelection
+ *
+ * Returns if selection is collapsed.
+ *
+ * Returns: Whether the selection is collapsed (just caret) or not (someting is selected).
+ */
+gboolean
+e_html_editor_selection_is_collapsed (EHTMLEditorSelection *selection)
+{
+       WebKitDOMRange *range;
+
+       g_return_val_if_fail (E_IS_HTML_EDITOR_SELECTION (selection), TRUE);
+
+       range = html_editor_selection_get_current_range (selection);
+       if (!range)
+               return TRUE;
+
+       return webkit_dom_range_get_collapsed (range, NULL);
+}
+
+/**
  * e_html_editor_selection_get_string:
  * @selection: an #EHTMLEditorSelection
  *
@@ -2288,7 +2310,7 @@ e_html_editor_selection_get_font_color (EHTMLEditorSelection *selection,
        gchar *color;
        g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));
 
-       if (g_strcmp0 (e_html_editor_selection_get_string (selection), "") == 0) {
+       if (e_html_editor_selection_is_collapsed (selection)) {
                color = g_strdup (selection->priv->font_color);
        } else {
                color = get_font_property (selection, "color");
@@ -2559,7 +2581,7 @@ e_html_editor_selection_is_indented (EHTMLEditorSelection *selection)
        if (!range)
                return FALSE;
 
-       if (g_strcmp0 (e_html_editor_selection_get_string (selection), "") == 0) {
+       if (e_html_editor_selection_is_collapsed (selection)) {
                element = get_element_for_inspection (range);
                return element_has_class (element, "-x-evo-indented");
        } else {
@@ -3539,7 +3561,7 @@ e_html_editor_selection_set_monospaced (EHTMLEditorSelection *selection,
                        monospace, "size", font_size_str, NULL);
                g_free (font_size_str);
 
-               if (g_strcmp0 (e_html_editor_selection_get_string (selection), "") != 0) {
+               if (!e_html_editor_selection_is_collapsed (selection)) {
                        gchar *html, *outer_html;
 
                        webkit_dom_node_append_child (
@@ -3603,7 +3625,7 @@ e_html_editor_selection_set_monospaced (EHTMLEditorSelection *selection,
                if (font_size == 0)
                        font_size = E_HTML_EDITOR_SELECTION_FONT_SIZE_NORMAL;
 
-               if (g_strcmp0 (e_html_editor_selection_get_string (selection), "") != 0) {
+               if (!e_html_editor_selection_is_collapsed (selection)) {
                        gchar *html, *outer_html, *inner_html, *beginning, *end;
                        gchar *start_position, *end_position, *font_size_str;
                        WebKitDOMElement *wrapper;
@@ -5500,7 +5522,7 @@ e_html_editor_selection_wrap_lines (EHTMLEditorSelection *selection)
        g_object_unref (view);
 
        caret = e_html_editor_selection_save_caret_position (selection);
-       if (g_strcmp0 (e_html_editor_selection_get_string (selection), "") == 0) {
+       if (e_html_editor_selection_is_collapsed (selection)) {
                WebKitDOMNode *end_container;
                WebKitDOMNode *parent;
                WebKitDOMNode *paragraph;
diff --git a/e-util/e-html-editor-selection.h b/e-util/e-html-editor-selection.h
index 2914405..2bef77f 100644
--- a/e-util/e-html-editor-selection.h
+++ b/e-util/e-html-editor-selection.h
@@ -161,6 +161,8 @@ void                e_html_editor_selection_unlink  (EHTMLEditorSelection *selection);
 void           e_html_editor_selection_create_link
                                                (EHTMLEditorSelection *selection,
                                                 const gchar *uri);
+gboolean       e_html_editor_selection_is_collapsed
+                                               (EHTMLEditorSelection *selection);
 const gchar *  e_html_editor_selection_get_string
                                                (EHTMLEditorSelection *selection);
 void           e_html_editor_selection_replace (EHTMLEditorSelection *selection,
diff --git a/e-util/e-html-editor-view.c b/e-util/e-html-editor-view.c
index 2b21fa0..5e20758 100644
--- a/e-util/e-html-editor-view.c
+++ b/e-util/e-html-editor-view.c
@@ -2482,7 +2482,7 @@ html_editor_view_key_press_event (GtkWidget *widget,
 
                /* BackSpace pressed in the beginning of quoted content changes
                 * format to normal and inserts text into body */
-               if (g_strcmp0 (e_html_editor_selection_get_string (selection), "") == 0) {
+               if (e_html_editor_selection_is_collapsed (selection)) {
                        e_html_editor_selection_save (selection);
                        if (change_quoted_block_to_normal (view)) {
                                e_html_editor_selection_restore (selection);
@@ -4337,7 +4337,7 @@ html_editor_view_insert_converted_html_into_selection (EHTMLEditorView *view,
 
        g_free (inner_html);
 
-       has_selection = g_strcmp0 (e_html_editor_selection_get_string (selection), "") != 0;
+       has_selection = !e_html_editor_selection_is_collapsed (selection);
 
        range = html_editor_view_get_dom_range (view);
        node = webkit_dom_range_get_end_container (range, NULL);


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