[evolution/449-support-markdown-in-composer] EMarkdownEditor: Implement more content editor functions



commit 1eb594e353634c8e331abed4be4714062a41c242
Author: Milan Crha <mcrha redhat com>
Date:   Tue Feb 8 15:01:23 2022 +0100

    EMarkdownEditor: Implement more content editor functions

 src/composer/e-composer-private.c         |  18 +--
 src/e-util/e-html-editor.c                |  47 ++++++++
 src/e-util/e-html-editor.h                |   4 +
 src/e-util/e-mail-signature-editor.c      |  12 +-
 src/e-util/e-markdown-editor.c            | 176 +++++++++++++++++++++++++++++-
 src/e-util/e-markdown-editor.h            |   4 +
 src/e-util/e-spell-text-view.c            |  36 ++++++
 src/e-util/e-spell-text-view.h            |   2 +
 src/e-util/test-html-editor-units-utils.c |  18 +--
 src/e-util/test-html-editor.c             |  18 +--
 src/mail/e-mail-notes.c                   |  12 +-
 11 files changed, 270 insertions(+), 77 deletions(-)
---
diff --git a/src/composer/e-composer-private.c b/src/composer/e-composer-private.c
index f7cc48e177..a8aa56c65a 100644
--- a/src/composer/e-composer-private.c
+++ b/src/composer/e-composer-private.c
@@ -259,23 +259,7 @@ e_composer_private_constructed (EMsgComposer *composer)
 
        focus_tracker = e_focus_tracker_new (GTK_WINDOW (composer));
 
-       action = e_html_editor_get_action (editor, "cut");
-       e_focus_tracker_set_cut_clipboard_action (focus_tracker, action);
-
-       action = e_html_editor_get_action (editor, "copy");
-       e_focus_tracker_set_copy_clipboard_action (focus_tracker, action);
-
-       action = e_html_editor_get_action (editor, "paste");
-       e_focus_tracker_set_paste_clipboard_action (focus_tracker, action);
-
-       action = e_html_editor_get_action (editor, "select-all");
-       e_focus_tracker_set_select_all_action (focus_tracker, action);
-
-       action = e_html_editor_get_action (editor, "undo");
-       e_focus_tracker_set_undo_action (focus_tracker, action);
-
-       action = e_html_editor_get_action (editor, "redo");
-       e_focus_tracker_set_redo_action (focus_tracker, action);
+       e_html_editor_connect_focus_tracker (editor, focus_tracker);
 
        priv->focus_tracker = focus_tracker;
 
diff --git a/src/e-util/e-html-editor.c b/src/e-util/e-html-editor.c
index b2898fd4b6..7ce754f2df 100644
--- a/src/e-util/e-html-editor.c
+++ b/src/e-util/e-html-editor.c
@@ -1309,6 +1309,43 @@ e_html_editor_new_finish (GAsyncResult *result,
        return e_simple_async_result_steal_user_data (eresult);
 }
 
+/**
+ * e_html_editor_connect_focus_tracker:
+ * @editor: an #EHTMLEditor
+ * @focus_tracker: an #EFocusTracker
+ *
+ * Connects @editor actions and widgets to the @focus_tracker.
+ *
+ * Since: 3.44
+ **/
+void
+e_html_editor_connect_focus_tracker (EHTMLEditor *editor,
+                                    EFocusTracker *focus_tracker)
+{
+       g_return_if_fail (E_IS_HTML_EDITOR (editor));
+       g_return_if_fail (E_IS_FOCUS_TRACKER (focus_tracker));
+
+       e_focus_tracker_set_cut_clipboard_action (focus_tracker,
+               e_html_editor_get_action (editor, "cut"));
+
+       e_focus_tracker_set_copy_clipboard_action (focus_tracker,
+               e_html_editor_get_action (editor, "copy"));
+
+       e_focus_tracker_set_paste_clipboard_action (focus_tracker,
+               e_html_editor_get_action (editor, "paste"));
+
+       e_focus_tracker_set_select_all_action (focus_tracker,
+               e_html_editor_get_action (editor, "select-all"));
+
+       e_focus_tracker_set_undo_action (focus_tracker,
+               e_html_editor_get_action (editor, "undo"));
+
+       e_focus_tracker_set_redo_action (focus_tracker,
+               e_html_editor_get_action (editor, "redo"));
+
+       e_markdown_editor_connect_focus_tracker (E_MARKDOWN_EDITOR (editor->priv->markdown_editor), 
focus_tracker);
+}
+
 /**
  * e_html_editor_get_content_box:
  * @editor: an #EHTMLEditor
@@ -1675,6 +1712,7 @@ e_html_editor_set_mode (EHTMLEditor *editor,
 
        if (cnt_editor) {
                if (cnt_editor != editor->priv->use_content_editor) {
+                       EContentEditorInterface *iface;
                        gboolean is_focused = FALSE;
 
                        if (editor->priv->use_content_editor) {
@@ -1717,6 +1755,15 @@ e_html_editor_set_mode (EHTMLEditor *editor,
 
                        if (is_focused)
                                e_content_editor_grab_focus (cnt_editor);
+
+                       /* Disable spell-check dialog when the content editor doesn't
+                          support moving between misspelled words. */
+                       iface = E_CONTENT_EDITOR_GET_IFACE (cnt_editor);
+
+                       gtk_action_set_visible (e_html_editor_get_action (editor, "spell-check"),
+                               iface && iface->spell_check_next_word && iface->spell_check_prev_word);
+
+                       e_content_editor_clear_undo_redo_history (cnt_editor);
                }
 
                editor->priv->mode = mode;
diff --git a/src/e-util/e-html-editor.h b/src/e-util/e-html-editor.h
index fba394b6db..b30250bdf3 100644
--- a/src/e-util/e-html-editor.h
+++ b/src/e-util/e-html-editor.h
@@ -30,6 +30,7 @@
 #include <e-util/e-activity.h>
 #include <e-util/e-activity-bar.h>
 #include <e-util/e-content-editor.h>
+#include <e-util/e-focus-tracker.h>
 
 /* Standard GObject macros */
 #define E_TYPE_HTML_EDITOR \
@@ -79,6 +80,9 @@ void          e_html_editor_new               (GAsyncReadyCallback callback,
                                                 gpointer user_data);
 GtkWidget *    e_html_editor_new_finish        (GAsyncResult *result,
                                                 GError **error);
+void           e_html_editor_connect_focus_tracker
+                                               (EHTMLEditor *editor,
+                                                EFocusTracker *focus_tracker);
 GtkWidget *    e_html_editor_get_content_box   (EHTMLEditor *editor);
 EContentEditor *
                e_html_editor_get_content_editor
diff --git a/src/e-util/e-mail-signature-editor.c b/src/e-util/e-mail-signature-editor.c
index e3789a1999..6f4361a05d 100644
--- a/src/e-util/e-mail-signature-editor.c
+++ b/src/e-util/e-mail-signature-editor.c
@@ -597,17 +597,7 @@ mail_signature_editor_constructed (GObject *object)
        /* Configure an EFocusTracker to manage selection actions. */
        focus_tracker = e_focus_tracker_new (GTK_WINDOW (window));
 
-       action = e_html_editor_get_action (editor, "cut");
-       e_focus_tracker_set_cut_clipboard_action (focus_tracker, action);
-
-       action = e_html_editor_get_action (editor, "copy");
-       e_focus_tracker_set_copy_clipboard_action (focus_tracker, action);
-
-       action = e_html_editor_get_action (editor, "paste");
-       e_focus_tracker_set_paste_clipboard_action (focus_tracker, action);
-
-       action = e_html_editor_get_action (editor, "select-all");
-       e_focus_tracker_set_select_all_action (focus_tracker, action);
+       e_html_editor_connect_focus_tracker (editor, focus_tracker);
 
        window->priv->focus_tracker = focus_tracker;
 
diff --git a/src/e-util/e-markdown-editor.c b/src/e-util/e-markdown-editor.c
index 929d1747f5..4126000bb0 100644
--- a/src/e-util/e-markdown-editor.c
+++ b/src/e-util/e-markdown-editor.c
@@ -16,6 +16,7 @@
 #include "e-misc-utils.h"
 #include "e-spell-text-view.h"
 #include "e-web-view.h"
+#include "e-widget-undo.h"
 
 #include "e-markdown-editor.h"
 
@@ -339,6 +340,29 @@ e_markdown_editor_get_content (EContentEditor *cnt_editor,
                }
        }
 
+       if ((flags & E_CONTENT_EDITOR_GET_RAW_BODY_STRIPPED) != 0) {
+               gchar *text;
+
+               text = e_markdown_editor_dup_text (E_MARKDOWN_EDITOR (cnt_editor));
+
+               if (text) {
+                       gchar *separator;
+
+                       separator = strstr (text, "-- \n");
+
+                       if (separator)
+                               *separator = '\0';
+               }
+
+               if (text) {
+                       e_content_editor_util_take_content_data (content_hash,
+                                       E_CONTENT_EDITOR_GET_RAW_BODY_STRIPPED, text, g_free);
+               } else {
+                       e_content_editor_util_put_content_data (content_hash,
+                                       E_CONTENT_EDITOR_GET_RAW_BODY_STRIPPED, "");
+               }
+       }
+
        task = g_task_new (cnt_editor, cancellable, callback, user_data);
        g_task_set_source_tag (task, e_markdown_editor_get_content);
        g_task_return_pointer (task, content_hash, (GDestroyNotify) e_content_editor_util_free_content_hash);
@@ -355,6 +379,131 @@ e_markdown_editor_get_content_finish (EContentEditor *cnt_editor,
        return g_task_propagate_pointer (G_TASK (result), error);
 }
 
+static void
+e_markdown_editor_move_caret_on_coordinates (EContentEditor *cnt_editor,
+                                            gint x,
+                                            gint y,
+                                            gboolean cancel_if_not_collapsed)
+{
+}
+
+static void
+e_markdown_editor_cut (EContentEditor *cnt_editor)
+{
+       /* Handled by GtktextView itself */
+}
+
+static void
+e_markdown_editor_copy (EContentEditor *cnt_editor)
+{
+       /* Handled by GtktextView itself */
+}
+
+static void
+e_markdown_editor_paste (EContentEditor *cnt_editor)
+{
+       /* Handled by GtktextView itself */
+}
+
+static void
+e_markdown_editor_paste_primary (EContentEditor *cnt_editor)
+{
+       /* Handled by GtktextView itself */
+}
+
+static void
+e_markdown_editor_undo (EContentEditor *cnt_editor)
+{
+       EMarkdownEditor *self = E_MARKDOWN_EDITOR (cnt_editor);
+
+       e_widget_undo_do_undo (GTK_WIDGET (self->priv->text_view));
+}
+
+static void
+e_markdown_editor_redo (EContentEditor *cnt_editor)
+{
+       EMarkdownEditor *self = E_MARKDOWN_EDITOR (cnt_editor);
+
+       e_widget_undo_do_redo (GTK_WIDGET (self->priv->text_view));
+}
+
+static void
+e_markdown_editor_clear_undo_redo_history (EContentEditor *cnt_editor)
+{
+       EMarkdownEditor *self = E_MARKDOWN_EDITOR (cnt_editor);
+
+       e_widget_undo_reset (GTK_WIDGET (self->priv->text_view));
+}
+
+static void
+e_markdown_editor_set_spell_checking_languages (EContentEditor *cnt_editor,
+                                               const gchar **languages)
+{
+       EMarkdownEditor *self = E_MARKDOWN_EDITOR (cnt_editor);
+
+       e_spell_text_view_set_languages (self->priv->text_view, languages);
+}
+
+static gchar *
+e_markdown_editor_get_caret_word (EContentEditor *cnt_editor)
+{
+       return NULL;
+}
+
+static void
+e_markdown_editor_replace_caret_word (EContentEditor *cnt_editor,
+                                     const gchar *replacement)
+{
+}
+
+static void
+e_markdown_editor_select_all (EContentEditor *cnt_editor)
+{
+       EMarkdownEditor *self = E_MARKDOWN_EDITOR (cnt_editor);
+
+       g_signal_emit_by_name (self->priv->text_view, "select-all", 0, TRUE, NULL);
+}
+
+static void
+e_markdown_editor_find (EContentEditor *cnt_editor,
+                       guint32 flags,
+                       const gchar *text)
+{
+}
+
+static void
+e_markdown_editor_replace (EContentEditor *cnt_editor,
+                          const gchar *replacement)
+{
+}
+
+static void
+e_markdown_editor_replace_all (EContentEditor *cnt_editor,
+                              guint32 flags,
+                              const gchar *find_text,
+                              const gchar *replace_with)
+{
+}
+
+static gchar *
+e_markdown_editor_get_current_signature_uid (EContentEditor *cnt_editor)
+{
+       return NULL;
+}
+
+static gchar *
+e_markdown_editor_insert_signature (EContentEditor *cnt_editor,
+                                   const gchar *content,
+                                   gboolean is_html,
+                                   gboolean can_reposition_caret,
+                                   const gchar *signature_id,
+                                   gboolean *set_signature_from_message,
+                                   gboolean *check_if_signature_is_changed,
+                                   gboolean *ignore_next_signature_change)
+{
+       return NULL;
+}
+
 static gboolean
 e_markdown_editor_can_copy (EMarkdownEditor *self)
 {
@@ -376,13 +525,13 @@ e_markdown_editor_can_paste (EMarkdownEditor *self)
 static gboolean
 e_markdown_editor_can_redo (EMarkdownEditor *self)
 {
-       return self->priv->can_redo;
+       return e_widget_undo_has_redo (GTK_WIDGET (self->priv->text_view));
 }
 
 static gboolean
 e_markdown_editor_can_undo (EMarkdownEditor *self)
 {
-       return self->priv->can_undo;
+       return e_widget_undo_has_undo (GTK_WIDGET (self->priv->text_view));
 }
 
 static gboolean
@@ -1362,7 +1511,7 @@ e_markdown_editor_content_editor_init (EContentEditorInterface *iface)
        iface->insert_content = e_markdown_editor_insert_content;
        iface->get_content = e_markdown_editor_get_content;
        iface->get_content_finish = e_markdown_editor_get_content_finish;
-       /*iface->move_caret_on_coordinates = e_markdown_editor_move_caret_on_coordinates;
+       iface->move_caret_on_coordinates = e_markdown_editor_move_caret_on_coordinates;
        iface->cut = e_markdown_editor_cut;
        iface->copy = e_markdown_editor_copy;
        iface->paste = e_markdown_editor_paste;
@@ -1378,7 +1527,7 @@ e_markdown_editor_content_editor_init (EContentEditorInterface *iface)
        iface->replace = e_markdown_editor_replace;
        iface->replace_all = e_markdown_editor_replace_all;
        iface->get_current_signature_uid = e_markdown_editor_get_current_signature_uid;
-       iface->insert_signature = e_markdown_editor_insert_signature;*/
+       iface->insert_signature = e_markdown_editor_insert_signature;
 }
 
 static void
@@ -1407,6 +1556,25 @@ e_markdown_editor_new (void)
        return g_object_new (E_TYPE_MARKDOWN_EDITOR, NULL);
 }
 
+/**
+ * e_markdown_editor_connect_focus_tracker:
+ * @self: an #EMarkdownEditor
+ * @focus_tracker: an #EFocusTracker
+ *
+ * Connects @self widgets to the @focus_tracker.
+ *
+ * Since: 3.44
+ **/
+void
+e_markdown_editor_connect_focus_tracker (EMarkdownEditor *self,
+                                        EFocusTracker *focus_tracker)
+{
+       g_return_if_fail (E_IS_MARKDOWN_EDITOR (self));
+       g_return_if_fail (E_IS_FOCUS_TRACKER (focus_tracker));
+
+       e_widget_undo_attach (GTK_WIDGET (self->priv->text_view), focus_tracker);
+}
+
 /**
  * e_markdown_editor_get_text_view:
  * @self: an #EMarkdownEditor
diff --git a/src/e-util/e-markdown-editor.h b/src/e-util/e-markdown-editor.h
index 075e7af259..c42959a36d 100644
--- a/src/e-util/e-markdown-editor.h
+++ b/src/e-util/e-markdown-editor.h
@@ -13,6 +13,8 @@
 
 #include <gtk/gtk.h>
 
+#include <e-util/e-focus-tracker.h>
+
 /* Standard GObject macros */
 #define E_TYPE_MARKDOWN_EDITOR \
        (e_markdown_editor_get_type ())
@@ -49,6 +51,8 @@ struct _EMarkdownEditorClass {
 
 GType          e_markdown_editor_get_type              (void) G_GNUC_CONST;
 GtkWidget *    e_markdown_editor_new                   (void);
+void           e_markdown_editor_connect_focus_tracker (EMarkdownEditor *self,
+                                                        EFocusTracker *focus_tracker);
 GtkTextView *  e_markdown_editor_get_text_view         (EMarkdownEditor *self);
 GtkToolbar *   e_markdown_editor_get_action_toolbar    (EMarkdownEditor *self);
 void           e_markdown_editor_set_text              (EMarkdownEditor *self,
diff --git a/src/e-util/e-spell-text-view.c b/src/e-util/e-spell-text-view.c
index ecb22c4c4a..0a2a80de21 100644
--- a/src/e-util/e-spell-text-view.c
+++ b/src/e-util/e-spell-text-view.c
@@ -149,3 +149,39 @@ e_spell_text_view_set_enabled (GtkTextView *text_view,
        gspell_text_view_set_inline_spell_checking (spell_view, enabled);
 #endif /* HAVE_GSPELL */
 }
+
+/**
+ * e_spell_text_view_set_languages:
+ * @text_view: a #GtkTextView
+ * @languages: (nullable): languages to set, or %NULL to unset any previous
+ *
+ * Sets @languages for inline spell checking for the @text_view.
+ * This can be used only after calling e_spell_text_view_attach().
+ *
+ * Since: 3.44
+ **/
+void
+e_spell_text_view_set_languages (GtkTextView *text_view,
+                                const gchar **languages)
+{
+#ifdef HAVE_GSPELL
+       GspellTextBuffer *spell_buffer;
+       GspellChecker *checker = NULL;
+       GtkTextBuffer *text_buffer;
+       guint ii;
+
+       for (ii = 0; !checker && languages && languages[ii]; ii++) {
+               const GspellLanguage *language;
+
+               language = gspell_language_lookup (languages[ii]);
+
+               if (language)
+                       checker = gspell_checker_new (language);
+       }
+
+       text_buffer = gtk_text_view_get_buffer (text_view);
+       spell_buffer = gspell_text_buffer_get_from_gtk_text_buffer (text_buffer);
+       gspell_text_buffer_set_spell_checker (spell_buffer, checker);
+       g_clear_object (&checker);
+#endif /* HAVE_GSPELL */
+}
diff --git a/src/e-util/e-spell-text-view.h b/src/e-util/e-spell-text-view.h
index 9d0461012b..7026fd0e4c 100644
--- a/src/e-util/e-spell-text-view.h
+++ b/src/e-util/e-spell-text-view.h
@@ -31,6 +31,8 @@ void          e_spell_text_view_attach        (GtkTextView *text_view);
 gboolean       e_spell_text_view_get_enabled   (GtkTextView *text_view);
 void           e_spell_text_view_set_enabled   (GtkTextView *text_view,
                                                 gboolean enabled);
+void           e_spell_text_view_set_languages (GtkTextView *text_view,
+                                                const gchar **languages);
 
 G_END_DECLS
 
diff --git a/src/e-util/test-html-editor-units-utils.c b/src/e-util/test-html-editor-units-utils.c
index abbeef8130..8912e64c38 100644
--- a/src/e-util/test-html-editor-units-utils.c
+++ b/src/e-util/test-html-editor-units-utils.c
@@ -292,23 +292,7 @@ test_utils_html_editor_created_cb (GObject *source_object,
 
        fixture->focus_tracker = e_focus_tracker_new (GTK_WINDOW (fixture->window));
 
-       e_focus_tracker_set_cut_clipboard_action (fixture->focus_tracker,
-               e_html_editor_get_action (fixture->editor, "cut"));
-
-       e_focus_tracker_set_copy_clipboard_action (fixture->focus_tracker,
-               e_html_editor_get_action (fixture->editor, "copy"));
-
-       e_focus_tracker_set_paste_clipboard_action (fixture->focus_tracker,
-               e_html_editor_get_action (fixture->editor, "paste"));
-
-       e_focus_tracker_set_select_all_action (fixture->focus_tracker,
-               e_html_editor_get_action (fixture->editor, "select-all"));
-
-       e_focus_tracker_set_undo_action (fixture->focus_tracker,
-               e_html_editor_get_action (fixture->editor, "undo"));
-
-       e_focus_tracker_set_redo_action (fixture->focus_tracker,
-               e_html_editor_get_action (fixture->editor, "redo"));
+       e_html_editor_connect_focus_tracker (fixture->editor, fixture->focus_tracker);
 
        /* Make sure this is off */
        test_utils_fixture_change_setting_boolean (fixture,
diff --git a/src/e-util/test-html-editor.c b/src/e-util/test-html-editor.c
index 03da2690cc..f8a9bb8cf6 100644
--- a/src/e-util/test-html-editor.c
+++ b/src/e-util/test-html-editor.c
@@ -592,23 +592,7 @@ create_new_editor_cb (GObject *source_object,
        focus_tracker = e_focus_tracker_new (GTK_WINDOW (widget));
        g_object_set_data_full (G_OBJECT (widget), "e-focus-tracker", focus_tracker, g_object_unref);
 
-       e_focus_tracker_set_cut_clipboard_action (focus_tracker,
-               e_html_editor_get_action (editor, "cut"));
-
-       e_focus_tracker_set_copy_clipboard_action (focus_tracker,
-               e_html_editor_get_action (editor, "copy"));
-
-       e_focus_tracker_set_paste_clipboard_action (focus_tracker,
-               e_html_editor_get_action (editor, "paste"));
-
-       e_focus_tracker_set_select_all_action (focus_tracker,
-               e_html_editor_get_action (editor, "select-all"));
-
-       e_focus_tracker_set_undo_action (focus_tracker,
-               e_html_editor_get_action (editor, "undo"));
-
-       e_focus_tracker_set_redo_action (focus_tracker,
-               e_html_editor_get_action (editor, "redo"));
+       e_html_editor_connect_focus_tracker (editor, focus_tracker);
 
        g_signal_connect_swapped (
                widget, "destroy",
diff --git a/src/mail/e-mail-notes.c b/src/mail/e-mail-notes.c
index ccdad4243d..fb241041c4 100644
--- a/src/mail/e-mail-notes.c
+++ b/src/mail/e-mail-notes.c
@@ -1125,17 +1125,7 @@ e_mail_notes_editor_new_with_editor (EHTMLEditor *html_editor,
        /* Configure an EFocusTracker to manage selection actions. */
        focus_tracker = e_focus_tracker_new (GTK_WINDOW (notes_editor));
 
-       action = e_html_editor_get_action (notes_editor->editor, "cut");
-       e_focus_tracker_set_cut_clipboard_action (focus_tracker, action);
-
-       action = e_html_editor_get_action (notes_editor->editor, "copy");
-       e_focus_tracker_set_copy_clipboard_action (focus_tracker, action);
-
-       action = e_html_editor_get_action (notes_editor->editor, "paste");
-       e_focus_tracker_set_paste_clipboard_action (focus_tracker, action);
-
-       action = e_html_editor_get_action (notes_editor->editor, "select-all");
-       e_focus_tracker_set_select_all_action (focus_tracker, action);
+       e_html_editor_connect_focus_tracker (notes_editor->editor, focus_tracker);
 
        notes_editor->focus_tracker = focus_tracker;
 


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