[gedit/wip/search-fixes] Create the SearchContext in GeditReplaceDialog



commit e0756afe5452a7506d77416c11e4bedc419dd997
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sun Aug 25 00:33:15 2013 +0200

    Create the SearchContext in GeditReplaceDialog
    
    Not finished! The SearchContext's signals must be disconnected on tab
    changed.

 gedit/gedit-commands-search.c |  124 -------------------------
 gedit/gedit-replace-dialog.c  |  201 ++++++++++++++++++++++++++++++++++-------
 gedit/gedit-replace-dialog.h  |    6 --
 3 files changed, 167 insertions(+), 164 deletions(-)
---
diff --git a/gedit/gedit-commands-search.c b/gedit/gedit-commands-search.c
index f6c0fa2..429577b 100644
--- a/gedit/gedit-commands-search.c
+++ b/gedit/gedit-commands-search.c
@@ -491,112 +491,12 @@ 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)
-{
-       GtkSourceSearchContext *search_context;
-       GtkSourceSearchSettings *search_settings;
-
-       search_settings = gedit_replace_dialog_get_search_settings (dialog);
-
-       search_context = _gedit_document_get_search_context (doc);
-
-       if (search_context != NULL &&
-           search_settings == gtk_source_search_context_get_settings (search_context))
-       {
-               return;
-       }
-
-       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)
 {
-       GeditDocument *doc;
-
        gedit_debug (DEBUG_COMMANDS);
 
-       doc = gedit_window_get_active_document (window);
-       create_search_context (dialog, doc);
-
        switch (response_id)
        {
                case GEDIT_REPLACE_DIALOG_FIND_RESPONSE:
@@ -631,18 +531,6 @@ replace_dialog_destroyed (GeditWindow        *window,
                           NULL);
 }
 
-static void
-active_tab_changed_cb (GeditWindow        *window,
-                      GeditTab           *tab,
-                      GeditReplaceDialog *replace_dialog)
-{
-       if (tab != NULL)
-       {
-               GeditDocument *doc = gedit_tab_get_document (tab);
-               create_search_context (replace_dialog, doc);
-       }
-}
-
 static GtkWidget *
 create_dialog (GeditWindow *window)
 {
@@ -661,12 +549,6 @@ create_dialog (GeditWindow *window)
                           (GWeakNotify) replace_dialog_destroyed,
                           window);
 
-       g_signal_connect_object (window,
-                                "active-tab-changed",
-                                G_CALLBACK (active_tab_changed_cb),
-                                dialog,
-                                0);
-
        return dialog;
 }
 
@@ -696,7 +578,6 @@ _gedit_cmd_search_replace (GtkAction   *action,
 {
        gpointer data;
        GtkWidget *replace_dialog;
-       GeditDocument *doc;
 
        gedit_debug (DEBUG_COMMANDS);
 
@@ -713,11 +594,6 @@ _gedit_cmd_search_replace (GtkAction   *action,
                replace_dialog = GTK_WIDGET (data);
        }
 
-       doc = gedit_window_get_active_document (window);
-       g_return_if_fail (doc != NULL);
-
-       create_search_context (GEDIT_REPLACE_DIALOG (replace_dialog), doc);
-
        gtk_widget_show (replace_dialog);
        last_search_data_restore_position (GEDIT_REPLACE_DIALOG (replace_dialog));
        gedit_replace_dialog_present_with_time (GEDIT_REPLACE_DIALOG (replace_dialog),
diff --git a/gedit/gedit-replace-dialog.c b/gedit/gedit-replace-dialog.c
index 6e71841..e0cd3e2 100644
--- a/gedit/gedit-replace-dialog.c
+++ b/gedit/gedit-replace-dialog.c
@@ -88,11 +88,149 @@ gedit_replace_dialog_delete_event (GtkWidget   *widget,
 }
 
 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);
+       }
+}
+
+static void
+set_search_error (GeditReplaceDialog *dialog,
+                 const gchar        *error_msg)
+{
+       set_error (GTK_ENTRY (dialog->priv->search_text_entry), error_msg);
+}
+
+static void
+set_replace_error (GeditReplaceDialog *dialog,
+                  const gchar        *error_msg)
+{
+       set_error (GTK_ENTRY (dialog->priv->replace_text_entry), error_msg);
+}
+
+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);
+
+       set_search_error (dialog, NULL);
+       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:
+                       set_search_error (dialog, regex_error->message);
+                       break;
+
+               case GTK_SOURCE_REGEX_SEARCH_REPLACE_ERROR:
+                       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)
+{
+       set_search_error (dialog, NULL);
+       set_replace_error (dialog, NULL);
+}
+
+static void
+create_search_context (GeditReplaceDialog *dialog,
+                      GeditDocument      *doc)
+{
+       GtkSourceSearchContext *search_context = _gedit_document_get_search_context (doc);
+
+       if (search_context != NULL &&
+           dialog->priv->search_settings == gtk_source_search_context_get_settings (search_context))
+       {
+               return;
+       }
+
+       search_context = gtk_source_search_context_new (GTK_SOURCE_BUFFER (doc),
+                                                       dialog->priv->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
 gedit_replace_dialog_response (GtkDialog *dialog,
                                gint       response_id)
 {
        GeditReplaceDialog *dlg = GEDIT_REPLACE_DIALOG (dialog);
        const gchar *str;
+       GeditWindow *window;
+       GeditDocument *doc = NULL;
+
+       window = get_gedit_window (GEDIT_REPLACE_DIALOG (dialog));
+
+       if (window != NULL)
+       {
+               doc = gedit_window_get_active_document (window);
+       }
+
+       if (doc != NULL)
+       {
+               create_search_context (GEDIT_REPLACE_DIALOG (dialog), doc);
+       }
 
        switch (response_id)
        {
@@ -118,6 +256,32 @@ gedit_replace_dialog_response (GtkDialog *dialog,
 }
 
 static void
+active_tab_changed_cb (GeditWindow        *window,
+                      GeditTab           *tab,
+                      GeditReplaceDialog *dialog)
+{
+       GeditDocument *doc = gedit_tab_get_document (tab);
+       create_search_context (dialog, doc);
+}
+
+static void
+gedit_replace_dialog_constructed (GObject *object)
+{
+       GeditReplaceDialog *dialog = GEDIT_REPLACE_DIALOG (object);
+       GeditWindow *window = get_gedit_window (dialog);
+
+       g_return_if_fail (GEDIT_IS_WINDOW (window));
+
+       g_signal_connect_object (window,
+                                "active-tab-changed",
+                                G_CALLBACK (active_tab_changed_cb),
+                                dialog,
+                                0);
+
+       G_OBJECT_CLASS (gedit_replace_dialog_parent_class)->constructed (object);
+}
+
+static void
 gedit_replace_dialog_dispose (GObject *object)
 {
        GeditReplaceDialog *dialog = GEDIT_REPLACE_DIALOG (object);
@@ -134,6 +298,7 @@ gedit_replace_dialog_class_init (GeditReplaceDialogClass *klass)
        GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
        GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (klass);
 
+       gobject_class->constructed = gedit_replace_dialog_constructed;
        gobject_class->dispose = gedit_replace_dialog_dispose;
        widget_class->delete_event = gedit_replace_dialog_delete_event;
        dialog_class->response = gedit_replace_dialog_response;
@@ -262,6 +427,8 @@ show_cb (GeditReplaceDialog *dialog)
                g_free (escaped_selection);
        }
 
+       create_search_context (dialog, doc);
+
        g_free (selection);
 }
 
@@ -392,38 +559,4 @@ gedit_replace_dialog_get_search_text (GeditReplaceDialog *dialog)
        return gtk_entry_get_text (GTK_ENTRY (dialog->priv->search_text_entry));
 }
 
-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 38a5032..f510832 100644
--- a/gedit/gedit-replace-dialog.h
+++ b/gedit/gedit-replace-dialog.h
@@ -99,12 +99,6 @@ gboolean              gedit_replace_dialog_get_backwards             (GeditReplaceDialog 
*dialog);
 
 GtkSourceSearchSettings        *gedit_replace_dialog_get_search_settings       (GeditReplaceDialog *dialog);
 
-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]