[evolution] Introduce e_html_editor_selection_set_on_point and use it where possible



commit 488329b01cdbb8ce6b817059942f641c27d31177
Author: Tomas Popela <tpopela redhat com>
Date:   Thu Feb 12 13:22:33 2015 +0100

    Introduce e_html_editor_selection_set_on_point and use it where possible
    
    This function will move the caret on given coordinates. It uses the
    webkit_dom_dom_document_caret_range_from_point. We can finally
    drop the e_html_editor_view_get_element_under_mouse_click the was there
    for workaround this.

 e-util/e-html-editor-hrule-dialog.c |   12 ++++++-
 e-util/e-html-editor-link-dialog.c  |   25 +++++++++++----
 e-util/e-html-editor-selection.c    |   58 ++++++++++++++++++++++++++--------
 e-util/e-html-editor-selection.h    |    4 ++
 e-util/e-html-editor-view.c         |   34 +++-----------------
 e-util/e-html-editor-view.h         |    3 --
 6 files changed, 82 insertions(+), 54 deletions(-)
---
diff --git a/e-util/e-html-editor-hrule-dialog.c b/e-util/e-html-editor-hrule-dialog.c
index 9ace655..51660bd 100644
--- a/e-util/e-html-editor-hrule-dialog.c
+++ b/e-util/e-html-editor-hrule-dialog.c
@@ -218,6 +218,8 @@ html_editor_hrule_dialog_show (GtkWidget *widget)
        WebKitDOMDOMWindow *window;
        WebKitDOMDOMSelection *selection;
        WebKitDOMElement *rule;
+       WebKitDOMRange *range;
+       WebKitDOMNode *node;
 
        dialog = E_HTML_EDITOR_HRULE_DIALOG (widget);
        editor = e_html_editor_dialog_get_editor (E_HTML_EDITOR_DIALOG (dialog));
@@ -233,7 +235,15 @@ html_editor_hrule_dialog_show (GtkWidget *widget)
                return;
        }
 
-       rule = e_html_editor_view_get_element_under_mouse_click (view);
+       range = webkit_dom_dom_selection_get_range_at (selection, 0, NULL);
+       node = webkit_dom_range_get_common_ancestor_container (range, NULL);
+       if (node && !WEBKIT_DOM_IS_HTMLHR_ELEMENT (node)) {
+               rule = e_html_editor_dom_node_find_parent_element (node, "A");
+               if (rule && !WEBKIT_DOM_IS_HTML_ANCHOR_ELEMENT (rule))
+                       rule = NULL;
+       } else
+               rule = WEBKIT_DOM_ELEMENT (node);
+
        if (!rule) {
                WebKitDOMElement *caret, *parent, *element;
 
diff --git a/e-util/e-html-editor-link-dialog.c b/e-util/e-html-editor-link-dialog.c
index 89113e3..3cd0a60 100644
--- a/e-util/e-html-editor-link-dialog.c
+++ b/e-util/e-html-editor-link-dialog.c
@@ -149,10 +149,16 @@ html_editor_link_dialog_ok (EHTMLEditorLinkDialog *dialog)
                                webkit_dom_range_get_end_offset (range, NULL),
                                NULL);
                } else {
+                       WebKitDOMNode *node;
+
                        /* get element that was clicked on */
-                       link = e_html_editor_view_get_element_under_mouse_click (view);
-                       if (!WEBKIT_DOM_IS_HTML_ANCHOR_ELEMENT (link))
-                               link = NULL;
+                       node = webkit_dom_range_get_common_ancestor_container (range, NULL);
+                       if (node && !WEBKIT_DOM_IS_HTML_ANCHOR_ELEMENT (node)) {
+                               link = e_html_editor_dom_node_find_parent_element (node, "A");
+                               if (link && !WEBKIT_DOM_IS_HTML_ANCHOR_ELEMENT (link))
+                                       link = NULL;
+                       } else
+                               link = WEBKIT_DOM_ELEMENT (node);
                }
        }
 
@@ -257,10 +263,15 @@ html_editor_link_dialog_show (GtkWidget *widget)
                        link = e_html_editor_dom_node_find_child_element (
                                        WEBKIT_DOM_NODE (fragment), "A");
                } else {
-                       /* get element that was clicked on */
-                       link = e_html_editor_view_get_element_under_mouse_click (view);
-                       if (!WEBKIT_DOM_IS_HTML_ANCHOR_ELEMENT (link))
-                               link = NULL;
+                       WebKitDOMNode *node;
+
+                       node = webkit_dom_range_get_common_ancestor_container (range, NULL);
+                       if (node && !WEBKIT_DOM_IS_HTML_ANCHOR_ELEMENT (node)) {
+                               link = e_html_editor_dom_node_find_parent_element (node, "A");
+                               if (link && !WEBKIT_DOM_IS_HTML_ANCHOR_ELEMENT (link))
+                                       link = NULL;
+                       } else
+                               link = WEBKIT_DOM_ELEMENT (node);
                }
        }
 
diff --git a/e-util/e-html-editor-selection.c b/e-util/e-html-editor-selection.c
index 1fbf479..61f7f7c 100644
--- a/e-util/e-html-editor-selection.c
+++ b/e-util/e-html-editor-selection.c
@@ -4288,7 +4288,7 @@ void
 e_html_editor_selection_unlink (EHTMLEditorSelection *selection)
 {
        EHTMLEditorView *view;
-       EHTMLEditorViewCommand command;
+       gchar *text;
        WebKitDOMDocument *document;
        WebKitDOMDOMWindow *window;
        WebKitDOMDOMSelection *dom_selection;
@@ -4301,6 +4301,7 @@ e_html_editor_selection_unlink (EHTMLEditorSelection *selection)
        g_return_if_fail (view != NULL);
 
        document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));
+       g_object_unref (view);
        window = webkit_dom_document_get_default_view (document);
        dom_selection = webkit_dom_dom_window_get_selection (window);
 
@@ -4309,21 +4310,23 @@ e_html_editor_selection_unlink (EHTMLEditorSelection *selection)
                        webkit_dom_range_get_start_container (range, NULL), "A");
 
        if (!link) {
-               gchar *text;
+               WebKitDOMNode *node;
+
                /* get element that was clicked on */
-               link = e_html_editor_view_get_element_under_mouse_click (view);
-               if (!WEBKIT_DOM_IS_HTML_ANCHOR_ELEMENT (link))
-                       link = NULL;
-
-               text = webkit_dom_html_element_get_inner_text (
-                               WEBKIT_DOM_HTML_ELEMENT (link));
-               webkit_dom_html_element_set_outer_html (WEBKIT_DOM_HTML_ELEMENT (link), text, NULL);
-               g_free (text);
-       } else {
-               command = E_HTML_EDITOR_VIEW_COMMAND_UNLINK;
-               e_html_editor_view_exec_command (view, command, NULL);
+               node = webkit_dom_range_get_common_ancestor_container (range, NULL);
+               if (node && !WEBKIT_DOM_IS_HTML_ANCHOR_ELEMENT (node)) {
+                       link = e_html_editor_dom_node_find_parent_element (node, "A");
+                       if (link && !WEBKIT_DOM_IS_HTML_ANCHOR_ELEMENT (link))
+                               return;
+               } else
+                       link = WEBKIT_DOM_ELEMENT (node);
        }
-       g_object_unref (view);
+
+       text = webkit_dom_html_element_get_inner_text (
+                       WEBKIT_DOM_HTML_ELEMENT (link));
+       webkit_dom_html_element_set_outer_html (
+               WEBKIT_DOM_HTML_ELEMENT (link), text, NULL);
+       g_free (text);
 }
 
 /**
@@ -6336,6 +6339,33 @@ e_html_editor_selection_restore (EHTMLEditorSelection *selection)
        webkit_dom_dom_selection_add_range (dom_selection, range);
 }
 
+void
+e_html_editor_selection_set_on_point (EHTMLEditorSelection *selection,
+                                      guint x,
+                                      guint y)
+{
+       EHTMLEditorView *view;
+       WebKitDOMRange *range;
+       WebKitDOMDocument *document;
+       WebKitDOMDOMWindow *dom_window;
+       WebKitDOMDOMSelection *dom_selection;
+
+       g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));
+
+       view = e_html_editor_selection_ref_html_editor_view (selection);
+       g_return_if_fail (view != NULL);
+
+       document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));
+       g_object_unref (view);
+
+       dom_window = webkit_dom_document_get_default_view (document);
+       dom_selection = webkit_dom_dom_window_get_selection (dom_window);
+
+       range = webkit_dom_document_caret_range_from_point (document, x, y);
+       webkit_dom_dom_selection_remove_all_ranges (dom_selection);
+       webkit_dom_dom_selection_add_range (dom_selection, range);
+}
+
 static void
 html_editor_selection_modify (EHTMLEditorSelection *selection,
                               const gchar *alter,
diff --git a/e-util/e-html-editor-selection.h b/e-util/e-html-editor-selection.h
index 5ae9492..4d0ce52 100644
--- a/e-util/e-html-editor-selection.h
+++ b/e-util/e-html-editor-selection.h
@@ -240,6 +240,10 @@ WebKitDOMElement *
                                                 WebKitDOMElement *paragraph);
 void           e_html_editor_selection_save    (EHTMLEditorSelection *selection);
 void           e_html_editor_selection_restore (EHTMLEditorSelection *selection);
+void           e_html_editor_selection_set_on_point
+                                               (EHTMLEditorSelection *selection,
+                                                guint x,
+                                                guint y);
 void           e_html_editor_selection_move    (EHTMLEditorSelection *selection,
                                                 gboolean forward,
                                                 EHTMLEditorSelectionGranularity granularity);
diff --git a/e-util/e-html-editor-view.c b/e-util/e-html-editor-view.c
index b82b4c1..8fd9be6 100644
--- a/e-util/e-html-editor-view.c
+++ b/e-util/e-html-editor-view.c
@@ -89,8 +89,6 @@ struct _EHTMLEditorViewPrivate {
 
        EHTMLEditorSelection *selection;
 
-       WebKitDOMElement *element_under_mouse;
-
        GHashTable *inline_images;
 
        GSettings *mail_settings;
@@ -2575,14 +2573,11 @@ html_editor_view_constructed (GObject *object)
 }
 
 static void
-html_editor_view_save_element_under_mouse_click (GtkWidget *widget)
+html_editor_view_move_selection_on_point (GtkWidget *widget)
 {
        gint x, y;
        GdkDeviceManager *device_manager;
        GdkDevice *pointer;
-       EHTMLEditorView *view;
-       WebKitDOMDocument *document;
-       WebKitDOMElement *element;
 
        g_return_if_fail (E_IS_HTML_EDITOR_VIEW (widget));
 
@@ -2592,11 +2587,8 @@ html_editor_view_save_element_under_mouse_click (GtkWidget *widget)
        gdk_window_get_device_position (
                gtk_widget_get_window (GTK_WIDGET (widget)), pointer, &x, &y, NULL);
 
-       document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (widget));
-       element = webkit_dom_document_element_from_point (document, x, y);
-
-       view = E_HTML_EDITOR_VIEW (widget);
-       view->priv->element_under_mouse = element;
+       e_html_editor_selection_set_on_point (
+               e_html_editor_view_get_selection (E_HTML_EDITOR_VIEW (widget)), x, y);
 }
 
 static gboolean
@@ -2610,7 +2602,7 @@ html_editor_view_button_press_event (GtkWidget *widget,
                g_signal_emit (widget, signals[PASTE_PRIMARY_CLIPBOARD], 0);
                event_handled = TRUE;
        } else if (event->button == 3) {
-               html_editor_view_save_element_under_mouse_click (widget);
+               html_editor_view_move_selection_on_point (widget);
                g_signal_emit (
                        widget, signals[POPUP_EVENT],
                        0, event, &event_handled);
@@ -3014,7 +3006,7 @@ html_editor_view_key_press_event (GtkWidget *widget,
        if (event->keyval == GDK_KEY_Menu) {
                gboolean event_handled;
 
-               html_editor_view_save_element_under_mouse_click (widget);
+               html_editor_view_move_selection_on_point (widget);
                g_signal_emit (
                        widget, signals[POPUP_EVENT],
                        0, event, &event_handled);
@@ -8674,22 +8666,6 @@ e_html_editor_view_update_fonts (EHTMLEditorView *view)
 }
 
 /**
- * e_html_editor_view_get_element_under_mouse_click:
- * @view: an #EHTMLEditorView
- *
- * Returns DOM element, that was clicked on.
- *
- * Returns: DOM element on that was clicked.
- */
-WebKitDOMElement *
-e_html_editor_view_get_element_under_mouse_click (EHTMLEditorView *view)
-{
-       g_return_val_if_fail (E_IS_HTML_EDITOR_VIEW (view), NULL);
-
-       return view->priv->element_under_mouse;
-}
-
-/**
  * e_html_editor_view_check_magic_links
  * @view: an #EHTMLEditorView
  * @include_space: If TRUE the pattern for link expects space on end
diff --git a/e-util/e-html-editor-view.h b/e-util/e-html-editor-view.h
index bf6ae00..86e33d1 100644
--- a/e-util/e-html-editor-view.h
+++ b/e-util/e-html-editor-view.h
@@ -140,9 +140,6 @@ void                e_html_editor_view_embed_styles (EHTMLEditorView *view);
 void           e_html_editor_view_remove_embed_styles
                                                (EHTMLEditorView *view);
 void           e_html_editor_view_update_fonts (EHTMLEditorView *view);
-WebKitDOMElement *
-               e_html_editor_view_get_element_under_mouse_click
-                                               (EHTMLEditorView *view);
 void           e_html_editor_view_check_magic_links
                                                (EHTMLEditorView *view,
                                                 gboolean while_typing);


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