[gedit/wip/search] Show regex error in the search and replace dialog



commit 3280c4c34b48ab077d59b30af383f42c278c4b52
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Mon Aug 19 17:45:27 2013 +0200

    Show regex error in the search and replace dialog
    
    The error appears as a symbolic icon in the GtkEntry (secondary icon, on
    the right). The error message is in the tooltip.
    
    The secondary icon is better in my opinion, because it doesn't move the
    text while we are typing it.
    
    Depending on the regex error type, the error icon is shown in the search
    entry, or the replace entry.

 gedit/gedit-commands-search.c |   92 ++++++++++++++++++++++++++++++++++++++---
 gedit/gedit-replace-dialog.c  |   34 +++++++++++++++
 gedit/gedit-replace-dialog.h  |    6 +++
 3 files changed, 126 insertions(+), 6 deletions(-)
---
diff --git a/gedit/gedit-commands-search.c b/gedit/gedit-commands-search.c
index dddad6e..84b783d 100644
--- a/gedit/gedit-commands-search.c
+++ b/gedit/gedit-commands-search.c
@@ -523,6 +523,91 @@ do_replace_all (GeditReplaceDialog *dialog,
 }
 
 static void
+update_regex_error (GeditReplaceDialog     *dialog,
+                   GtkSourceSearchContext *search_context)
+{
+       GError *regex_error = gtk_source_search_context_get_regex_error (search_context);
+       GtkSourceRegexSearchState regex_state = gtk_source_search_context_get_regex_state (search_context);
+
+       gedit_replace_dialog_set_search_error (dialog, NULL);
+       gedit_replace_dialog_set_replace_error (dialog, NULL);
+
+       if (regex_error == NULL)
+       {
+               return;
+       }
+
+       switch (regex_state)
+       {
+               case GTK_SOURCE_REGEX_SEARCH_COMPILATION_ERROR:
+               case GTK_SOURCE_REGEX_SEARCH_MATCHING_ERROR:
+                       gedit_replace_dialog_set_search_error (dialog, regex_error->message);
+                       break;
+
+               case GTK_SOURCE_REGEX_SEARCH_REPLACE_ERROR:
+                       gedit_replace_dialog_set_replace_error (dialog, regex_error->message);
+                       break;
+
+               default:
+                       g_return_if_reached ();
+       }
+
+       g_error_free (regex_error);
+}
+
+static void
+regex_error_notify_cb (GtkSourceSearchContext *search_context,
+                      GParamSpec             *pspec,
+                      GeditReplaceDialog     *dialog)
+{
+       GeditDocument *doc;
+       GtkSourceSearchContext *doc_search_context;
+
+       doc = GEDIT_DOCUMENT (gtk_source_search_context_get_buffer (search_context));
+       doc_search_context = _gedit_document_get_search_context (doc);
+
+       if (search_context == doc_search_context)
+       {
+               update_regex_error (dialog, search_context);
+       }
+}
+
+static void
+search_context_finalized_cb (GeditReplaceDialog *dialog,
+                            GObject            *where_the_object_was)
+{
+       gedit_replace_dialog_set_search_error (dialog, NULL);
+       gedit_replace_dialog_set_replace_error (dialog, NULL);
+}
+
+static void
+create_search_context (GeditReplaceDialog      *dialog,
+                      GeditDocument           *doc,
+                      GtkSourceSearchSettings *search_settings)
+{
+       GtkSourceSearchContext *search_context;
+
+       search_context = gtk_source_search_context_new (GTK_SOURCE_BUFFER (doc),
+                                                       search_settings);
+
+       g_signal_connect_object (search_context,
+                                "notify::regex-error",
+                                G_CALLBACK (regex_error_notify_cb),
+                                dialog,
+                                0);
+
+       g_object_weak_ref (G_OBJECT (search_context),
+                          (GWeakNotify)search_context_finalized_cb,
+                          dialog);
+
+       update_regex_error (dialog, search_context);
+
+       _gedit_document_set_search_context (doc, search_context);
+
+       g_object_unref (search_context);
+}
+
+static void
 replace_dialog_response_cb (GeditReplaceDialog *dialog,
                            gint                response_id,
                            GeditWindow        *window)
@@ -541,12 +626,7 @@ replace_dialog_response_cb (GeditReplaceDialog *dialog,
        if (search_context == NULL ||
            search_settings != gtk_source_search_context_get_settings (search_context))
        {
-               search_context = gtk_source_search_context_new (GTK_SOURCE_BUFFER (doc),
-                                                               search_settings);
-
-               _gedit_document_set_search_context (doc, search_context);
-
-               g_object_unref (search_context);
+               create_search_context (dialog, doc, search_settings);
        }
 
        switch (response_id)
diff --git a/gedit/gedit-replace-dialog.c b/gedit/gedit-replace-dialog.c
index 55fb669..ea57364 100644
--- a/gedit/gedit-replace-dialog.c
+++ b/gedit/gedit-replace-dialog.c
@@ -322,4 +322,38 @@ gedit_replace_dialog_set_search_text (GeditReplaceDialog *dialog,
                            search_text);
 }
 
+static void
+set_error (GtkEntry    *entry,
+          const gchar *error_msg)
+{
+       if (error_msg == NULL || error_msg[0] == '\0')
+       {
+               gtk_entry_set_icon_from_gicon (entry, GTK_ENTRY_ICON_SECONDARY, NULL);
+               gtk_entry_set_icon_tooltip_text (entry, GTK_ENTRY_ICON_SECONDARY, NULL);
+       }
+       else
+       {
+               GIcon *icon = g_themed_icon_new_with_default_fallbacks ("dialog-error-symbolic");
+
+               gtk_entry_set_icon_from_gicon (entry, GTK_ENTRY_ICON_SECONDARY, icon);
+               gtk_entry_set_icon_tooltip_text (entry, GTK_ENTRY_ICON_SECONDARY, error_msg);
+
+               g_object_unref (icon);
+       }
+}
+
+void
+gedit_replace_dialog_set_search_error (GeditReplaceDialog *dialog,
+                                      const gchar        *error_msg)
+{
+       set_error (GTK_ENTRY (dialog->priv->search_text_entry), error_msg);
+}
+
+void
+gedit_replace_dialog_set_replace_error (GeditReplaceDialog *dialog,
+                                       const gchar        *error_msg)
+{
+       set_error (GTK_ENTRY (dialog->priv->replace_text_entry), error_msg);
+}
+
 /* ex:set ts=8 noet: */
diff --git a/gedit/gedit-replace-dialog.h b/gedit/gedit-replace-dialog.h
index 241b6b2..9690d2f 100644
--- a/gedit/gedit-replace-dialog.h
+++ b/gedit/gedit-replace-dialog.h
@@ -99,6 +99,12 @@ GtkSourceSearchSettings      *gedit_replace_dialog_get_search_settings       (GeditReplaceD
 void                    gedit_replace_dialog_set_search_text           (GeditReplaceDialog *dialog,
                                                                         const gchar        *search_text);
 
+void                    gedit_replace_dialog_set_search_error          (GeditReplaceDialog *dialog,
+                                                                        const gchar        *error_msg);
+
+void                    gedit_replace_dialog_set_replace_error         (GeditReplaceDialog *dialog,
+                                                                        const gchar        *error_msg);
+
 G_END_DECLS
 
 #endif  /* __GEDIT_REPLACE_DIALOG_H__  */


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