[gedit/wip/use-gsv-search: 12/15] ViewFrame: async forward/backward search



commit 7b0f3ea1a21e61acfb75ce9739d021a7c019b0ec
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sun Jun 30 11:34:33 2013 +0200

    ViewFrame: async forward/backward search

 gedit/gedit-view-frame.c |  204 ++++++++++++++++++++++++++++++++++------------
 1 files changed, 150 insertions(+), 54 deletions(-)
---
diff --git a/gedit/gedit-view-frame.c b/gedit/gedit-view-frame.c
index e4d7b3d..08fadd5 100644
--- a/gedit/gedit-view-frame.c
+++ b/gedit/gedit-view-frame.c
@@ -224,57 +224,38 @@ set_entry_background (GeditViewFrame          *frame,
        }
 }
 
-static gboolean
-run_search (GeditViewFrame   *frame,
-            gboolean          search_backward,
-            gboolean          typing)
+static void
+finish_search (GeditViewFrame    *frame,
+              gboolean           found)
 {
-       const gchar   *entry_text;
-       GtkTextIter    start_at;
-       GtkTextIter    match_start;
-       GtkTextIter    match_end;
-       gboolean       found = FALSE;
-       GtkSourceBuffer *buffer;
-
-       g_return_val_if_fail (frame->priv->search_mode == SEARCH, FALSE);
+       const gchar *entry_text = gtk_entry_get_text (frame->priv->search_entry);
 
-       entry_text = gtk_entry_get_text (frame->priv->search_entry);
-
-       buffer = GTK_SOURCE_BUFFER (gedit_view_frame_get_document (frame));
-
-       if (typing)
-       {
-               gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (buffer),
-                                                 &start_at,
-                                                 frame->priv->start_mark);
-       }
-       else if (search_backward)
-       {
-               gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (buffer),
-                                                     &start_at,
-                                                     NULL);
-       }
-       else
+       if (found || (*entry_text == '\0'))
        {
-               gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (buffer),
-                                                     NULL,
-                                                     &start_at);
-       }
+               gedit_view_scroll_to_cursor (frame->priv->view);
 
-       if (search_backward)
-       {
-               found = gtk_source_buffer_backward_search (buffer,
-                                                          &start_at,
-                                                          &match_start,
-                                                          &match_end);
+               set_entry_background (frame, GEDIT_SEARCH_ENTRY_NORMAL);
        }
        else
        {
-               found = gtk_source_buffer_forward_search (buffer,
-                                                         &start_at,
-                                                         &match_start,
-                                                         &match_end);
+               set_entry_background (frame, GEDIT_SEARCH_ENTRY_NOT_FOUND);
        }
+}
+
+static void
+start_search_finished (GtkSourceBuffer *buffer,
+                      GAsyncResult    *result,
+                      GeditViewFrame  *frame)
+{
+       GtkTextIter match_start;
+       GtkTextIter match_end;
+       gboolean found;
+
+       found = gtk_source_buffer_forward_search_finish (buffer,
+                                                        result,
+                                                        &match_start,
+                                                        &match_end,
+                                                        NULL);
 
        if (found)
        {
@@ -282,25 +263,133 @@ run_search (GeditViewFrame   *frame,
                                              &match_start,
                                              &match_end);
        }
-       else if (typing)
+       else
        {
+               GtkTextIter start_at;
+
+               gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (buffer),
+                                                 &start_at,
+                                                 frame->priv->start_mark);
+
                gtk_text_buffer_select_range (GTK_TEXT_BUFFER (buffer),
                                              &start_at,
                                              &start_at);
        }
 
-       if (found || (*entry_text == '\0'))
-       {
-               gedit_view_scroll_to_cursor (frame->priv->view);
+       finish_search (frame, found);
+}
 
-               set_entry_background (frame, GEDIT_SEARCH_ENTRY_NORMAL);
+static void
+start_search (GeditViewFrame *frame)
+{
+       GtkTextIter start_at;
+       GtkSourceBuffer *buffer;
+
+       g_return_if_fail (frame->priv->search_mode == SEARCH);
+
+       buffer = GTK_SOURCE_BUFFER (gedit_view_frame_get_document (frame));
+
+       gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (buffer),
+                                         &start_at,
+                                         frame->priv->start_mark);
+
+       gtk_source_buffer_forward_search_async (buffer,
+                                               &start_at,
+                                               NULL,
+                                               (GAsyncReadyCallback)start_search_finished,
+                                               frame);
+}
+
+static void
+forward_search_finished (GtkSourceBuffer *buffer,
+                        GAsyncResult    *result,
+                        GeditViewFrame  *frame)
+{
+       GtkTextIter match_start;
+       GtkTextIter match_end;
+       gboolean found;
+
+       found = gtk_source_buffer_forward_search_finish (buffer,
+                                                        result,
+                                                        &match_start,
+                                                        &match_end,
+                                                        NULL);
+
+       if (found)
+       {
+               gtk_text_buffer_select_range (GTK_TEXT_BUFFER (buffer),
+                                             &match_start,
+                                             &match_end);
        }
-       else
+
+       finish_search (frame, found);
+}
+
+static void
+forward_search (GeditViewFrame *frame)
+{
+       GtkTextIter start_at;
+       GtkSourceBuffer *buffer;
+
+       g_return_if_fail (frame->priv->search_mode == SEARCH);
+
+       buffer = GTK_SOURCE_BUFFER (gedit_view_frame_get_document (frame));
+
+       gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (buffer),
+                                             NULL,
+                                             &start_at);
+
+       gtk_source_buffer_forward_search_async (buffer,
+                                               &start_at,
+                                               NULL,
+                                               (GAsyncReadyCallback)forward_search_finished,
+                                               frame);
+}
+
+static void
+backward_search_finished (GtkSourceBuffer *buffer,
+                         GAsyncResult    *result,
+                         GeditViewFrame  *frame)
+{
+       GtkTextIter match_start;
+       GtkTextIter match_end;
+       gboolean found;
+
+       found = gtk_source_buffer_backward_search_finish (buffer,
+                                                         result,
+                                                         &match_start,
+                                                         &match_end,
+                                                         NULL);
+
+       if (found)
        {
-               set_entry_background (frame, GEDIT_SEARCH_ENTRY_NOT_FOUND);
+               gtk_text_buffer_select_range (GTK_TEXT_BUFFER (buffer),
+                                             &match_start,
+                                             &match_end);
        }
 
-       return found;
+       finish_search (frame, found);
+}
+
+static void
+backward_search (GeditViewFrame *frame)
+{
+       GtkTextIter start_at;
+       GtkSourceBuffer *buffer;
+
+       g_return_if_fail (frame->priv->search_mode == SEARCH);
+
+       buffer = GTK_SOURCE_BUFFER (gedit_view_frame_get_document (frame));
+
+       gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (buffer),
+                                             &start_at,
+                                             NULL);
+
+       gtk_source_buffer_backward_search_async (buffer,
+                                                &start_at,
+                                                NULL,
+                                                (GAsyncReadyCallback)backward_search_finished,
+                                                frame);
 }
 
 static void
@@ -319,7 +408,14 @@ search_again (GeditViewFrame *frame,
                                               frame);
        }
 
-       run_search (frame, search_backward, FALSE);
+       if (search_backward)
+       {
+               backward_search (frame);
+       }
+       else
+       {
+               forward_search (frame);
+       }
 }
 
 static gboolean
@@ -791,7 +887,7 @@ search_init (GtkWidget      *entry,
        if (frame->priv->search_mode == SEARCH)
        {
                update_search (frame);
-               run_search (frame, FALSE, TRUE);
+               start_search (frame);
        }
        else if (*entry_text != '\0')
        {


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