[evolution] EHTMLEditorView - Move between the table cells with tabulator



commit aa6f1bbdc0210d1215ce9e0eb5a5dc9e9b870d45
Author: Tomas Popela <tpopela redhat com>
Date:   Fri Mar 6 08:55:49 2015 +0100

    EHTMLEditorView - Move between the table cells with tabulator
    
    Also don't jump around UI when pressing Shift + Tab outside of table.

 composer/e-msg-composer.c   |    5 --
 e-util/e-html-editor-view.c |  122 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 119 insertions(+), 8 deletions(-)
---
diff --git a/composer/e-msg-composer.c b/composer/e-msg-composer.c
index 92dc2c6..81ead4d 100644
--- a/composer/e-msg-composer.c
+++ b/composer/e-msg-composer.c
@@ -2541,11 +2541,6 @@ msg_composer_key_press_event (GtkWidget *widget,
        }
 
        if (gtk_widget_is_focus (GTK_WIDGET (view))) {
-               if (event->keyval == GDK_KEY_ISO_Left_Tab) {
-                       gtk_widget_grab_focus (input_widget);
-                       return TRUE;
-               }
-
                if ((((event)->state & GDK_SHIFT_MASK) &&
                    ((event)->keyval == GDK_KEY_Insert)) ||
                    (((event)->state & GDK_CONTROL_MASK) &&
diff --git a/e-util/e-html-editor-view.c b/e-util/e-html-editor-view.c
index 918096d..f0611e1 100644
--- a/e-util/e-html-editor-view.c
+++ b/e-util/e-html-editor-view.c
@@ -3109,6 +3109,112 @@ fix_structure_after_delete_before_quoted_content (EHTMLEditorView *view)
 }
 
 static gboolean
+selection_is_in_table (WebKitDOMDocument *document)
+{
+       WebKitDOMDOMWindow *window;
+       WebKitDOMDOMSelection *selection;
+       WebKitDOMNode *node, *parent;
+       WebKitDOMRange *range;
+
+       window = webkit_dom_document_get_default_view (document);
+       selection = webkit_dom_dom_window_get_selection (window);
+
+       if (webkit_dom_dom_selection_get_range_count (selection) < 1)
+               return FALSE;
+
+       range = webkit_dom_dom_selection_get_range_at (selection, 0, NULL);
+       node = webkit_dom_range_get_start_container (range, NULL);
+
+       parent = node;
+       while (parent && !WEBKIT_DOM_IS_HTML_BODY_ELEMENT (parent)) {
+               if (WEBKIT_DOM_IS_HTML_TABLE_ROW_ELEMENT (parent))
+                       return TRUE;
+               if (WEBKIT_DOM_IS_HTML_TABLE_CELL_ELEMENT (parent))
+                       return TRUE;
+               if (WEBKIT_DOM_IS_HTML_TABLE_ELEMENT (parent))
+                       return TRUE;
+               parent = webkit_dom_node_get_parent_node (parent);
+       }
+
+       return FALSE;
+}
+
+static void
+jump_to_next_table_cell (EHTMLEditorView *view,
+                         gboolean jump_back)
+{
+       WebKitDOMDocument *document;
+       WebKitDOMDOMWindow *window;
+       WebKitDOMDOMSelection *selection;
+       WebKitDOMNode *node, *cell;
+       WebKitDOMRange *range;
+
+       document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));
+       window = webkit_dom_document_get_default_view (document);
+       selection = webkit_dom_dom_window_get_selection (window);
+
+       if (webkit_dom_dom_selection_get_range_count (selection) < 1)
+               return;
+
+       range = webkit_dom_dom_selection_get_range_at (selection, 0, NULL);
+       node = webkit_dom_range_get_start_container (range, NULL);
+
+       cell = node;
+       while (cell && !WEBKIT_DOM_IS_HTML_TABLE_CELL_ELEMENT (cell)) {
+               cell = webkit_dom_node_get_parent_node (cell);
+       }
+
+       if (!WEBKIT_DOM_IS_HTML_TABLE_CELL_ELEMENT (cell))
+               return;
+
+       if (jump_back) {
+               /* Get previous cell */
+               node = webkit_dom_node_get_previous_sibling (cell);
+               if (!node || !WEBKIT_DOM_IS_HTML_TABLE_CELL_ELEMENT (node)) {
+                       /* No cell, go one row up. */
+                       node = webkit_dom_node_get_parent_node (cell);
+                       node = webkit_dom_node_get_previous_sibling (node);
+                       if (node && WEBKIT_DOM_IS_HTML_TABLE_ROW_ELEMENT (node)) {
+                               node = webkit_dom_node_get_last_child (node);
+                       } else {
+                               /* No row above, move to the block before table. */
+                               node = webkit_dom_node_get_parent_node (cell);
+                               while (!WEBKIT_DOM_IS_HTML_BODY_ELEMENT (webkit_dom_node_get_parent_node 
(node)))
+                                       node = webkit_dom_node_get_parent_node (node);
+
+                               node = webkit_dom_node_get_previous_sibling (node);
+                       }
+               }
+       } else {
+               /* Get next cell */
+               node = webkit_dom_node_get_next_sibling (cell);
+               if (!node || !WEBKIT_DOM_IS_HTML_TABLE_CELL_ELEMENT (node)) {
+                       /* No cell, go one row below. */
+                       node = webkit_dom_node_get_parent_node (cell);
+                       node = webkit_dom_node_get_next_sibling (node);
+                       if (node && WEBKIT_DOM_IS_HTML_TABLE_ROW_ELEMENT (node)) {
+                               node = webkit_dom_node_get_first_child (node);
+                       } else {
+                               /* No row below, move to the block after table. */
+                               node = webkit_dom_node_get_parent_node (cell);
+                               while (!WEBKIT_DOM_IS_HTML_BODY_ELEMENT (webkit_dom_node_get_parent_node 
(node)))
+                                       node = webkit_dom_node_get_parent_node (node);
+
+                               node = webkit_dom_node_get_next_sibling (node);
+                       }
+               }
+       }
+
+       if (!node)
+               return;
+
+       webkit_dom_range_select_node_contents (range, node, NULL);
+       webkit_dom_range_collapse (range, TRUE, NULL);
+       webkit_dom_dom_selection_remove_all_ranges (selection);
+       webkit_dom_dom_selection_add_range (selection, range);
+}
+
+static gboolean
 html_editor_view_key_press_event (GtkWidget *widget,
                                   GdkEventKey *event)
 {
@@ -3131,9 +3237,19 @@ html_editor_view_key_press_event (GtkWidget *widget,
                return event_handled;
        }
 
-       if (event->keyval == GDK_KEY_Tab)
-               return e_html_editor_view_exec_command (
-                       view, E_HTML_EDITOR_VIEW_COMMAND_INSERT_TEXT, "\t");
+       if (event->keyval == GDK_KEY_Tab || event->keyval == GDK_KEY_ISO_Left_Tab) {
+               WebKitDOMDocument *document;
+
+               document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));
+               if (selection_is_in_table (document)) {
+                       jump_to_next_table_cell (view, event->keyval == GDK_KEY_ISO_Left_Tab);
+                       return TRUE;
+               } else if (event->keyval == GDK_KEY_Tab)
+                       return e_html_editor_view_exec_command (
+                               view, E_HTML_EDITOR_VIEW_COMMAND_INSERT_TEXT, "\t");
+               else
+                       return TRUE;
+       }
 
        if (is_return_key (event)) {
                EHTMLEditorSelection *selection;


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