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



commit 2720a338cc1e8b9043dec75a5224d8ebe364c290
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Aug 16 16:33:54 2013 +0200

    Display regex error in the search and replace dialog

 gedit/gedit-commands-search.c |   75 +++++++++++++++++++++++++++++++++++++---
 gedit/gedit-replace-dialog.c  |   21 +++++++++++
 gedit/gedit-replace-dialog.h  |    3 ++
 gedit/gedit-replace-dialog.ui |   40 +++++++++++++++++++++-
 4 files changed, 132 insertions(+), 7 deletions(-)
---
diff --git a/gedit/gedit-commands-search.c b/gedit/gedit-commands-search.c
index dddad6e..008e164 100644
--- a/gedit/gedit-commands-search.c
+++ b/gedit/gedit-commands-search.c
@@ -523,6 +523,74 @@ 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);
+
+       if (regex_error == NULL)
+       {
+               gedit_replace_dialog_set_error (dialog, NULL);
+       }
+       else
+       {
+               gedit_replace_dialog_set_error (dialog, regex_error->message);
+               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_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 +609,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..692541f 100644
--- a/gedit/gedit-replace-dialog.c
+++ b/gedit/gedit-replace-dialog.c
@@ -49,6 +49,8 @@ struct _GeditReplaceDialogPrivate
        GtkWidget *regex_checkbutton;
        GtkWidget *backwards_checkbutton;
        GtkWidget *wrap_around_checkbutton;
+       GtkWidget *error_bar;
+       GtkLabel *error_label;
 
        GtkSourceSearchSettings *search_settings;
 
@@ -148,6 +150,8 @@ gedit_replace_dialog_class_init (GeditReplaceDialogClass *klass)
        gtk_widget_class_bind_template_child_private (widget_class, GeditReplaceDialog, regex_checkbutton);
        gtk_widget_class_bind_template_child_private (widget_class, GeditReplaceDialog, 
backwards_checkbutton);
        gtk_widget_class_bind_template_child_private (widget_class, GeditReplaceDialog, 
wrap_around_checkbutton);
+       gtk_widget_class_bind_template_child_private (widget_class, GeditReplaceDialog, error_bar);
+       gtk_widget_class_bind_template_child_private (widget_class, GeditReplaceDialog, error_label);
 }
 
 static void
@@ -266,6 +270,7 @@ gedit_replace_dialog_init (GeditReplaceDialog *dlg)
                                G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
 
        gtk_widget_show_all (GTK_WIDGET (dlg));
+       gtk_widget_hide (dlg->priv->error_bar);
 }
 
 GtkWidget *
@@ -322,4 +327,20 @@ gedit_replace_dialog_set_search_text (GeditReplaceDialog *dialog,
                            search_text);
 }
 
+void
+gedit_replace_dialog_set_error (GeditReplaceDialog *dialog,
+                               const gchar        *error_msg)
+{
+       gtk_label_set_text (dialog->priv->error_label, error_msg);
+
+       if (error_msg != NULL && error_msg[0] != '\0')
+       {
+               gtk_widget_show (dialog->priv->error_bar);
+       }
+       else
+       {
+               gtk_widget_hide (dialog->priv->error_bar);
+       }
+}
+
 /* ex:set ts=8 noet: */
diff --git a/gedit/gedit-replace-dialog.h b/gedit/gedit-replace-dialog.h
index 241b6b2..443ee0c 100644
--- a/gedit/gedit-replace-dialog.h
+++ b/gedit/gedit-replace-dialog.h
@@ -99,6 +99,9 @@ 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_error                 (GeditReplaceDialog *dialog,
+                                                                        const gchar        *error_msg);
+
 G_END_DECLS
 
 #endif  /* __GEDIT_REPLACE_DIALOG_H__  */
diff --git a/gedit/gedit-replace-dialog.ui b/gedit/gedit-replace-dialog.ui
index 9b900d3..439ebb9 100644
--- a/gedit/gedit-replace-dialog.ui
+++ b/gedit/gedit-replace-dialog.ui
@@ -142,6 +142,44 @@
               </packing>
             </child>
             <child>
+              <object class="GtkBox" id="error_bar">
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <property name="spacing">12</property>
+                <child>
+                  <object class="GtkImage" id="image1">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="yalign">0</property>
+                    <property name="icon_name">dialog-error</property>
+                  </object>
+                  <packing>
+                    <property name="expand">False</property>
+                    <property name="fill">True</property>
+                    <property name="position">0</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkLabel" id="error_label">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="wrap">True</property>
+                    <property name="max_width_chars">60</property>
+                  </object>
+                  <packing>
+                    <property name="expand">False</property>
+                    <property name="fill">True</property>
+                    <property name="position">1</property>
+                  </packing>
+                </child>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">True</property>
+                <property name="position">1</property>
+              </packing>
+            </child>
+            <child>
               <object class="GtkBox" id="vbox3">
                 <property name="visible">True</property>
                 <property name="can_focus">False</property>
@@ -237,7 +275,7 @@
               <packing>
                 <property name="expand">False</property>
                 <property name="fill">True</property>
-                <property name="position">1</property>
+                <property name="position">2</property>
               </packing>
             </child>
           </object>


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