[gtksourceview/wip/search: 27/36] test-search-ui: display the occurrence position



commit ae05912746778f05830ad00affdf32c04a2aa000
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Tue Jul 2 23:40:22 2013 +0200

    test-search-ui: display the occurrence position

 tests/test-search-ui.c  |  108 +++++++++++++++++++++++++++++++++++------------
 tests/test-search-ui.ui |    2 +-
 2 files changed, 82 insertions(+), 28 deletions(-)
---
diff --git a/tests/test-search-ui.c b/tests/test-search-ui.c
index 9fa028e..ba999cc 100644
--- a/tests/test-search-ui.c
+++ b/tests/test-search-ui.c
@@ -27,7 +27,8 @@ typedef struct
        GtkSourceView *view;
        GtkSourceBuffer *buffer;
        GtkEntry *replace_entry;
-} ReplaceWidgets;
+       GtkLabel *label;
+} TestSearchUI;
 
 static void
 open_file (GtkSourceBuffer *buffer,
@@ -57,18 +58,47 @@ open_file (GtkSourceBuffer *buffer,
 }
 
 static void
-on_occurrences_count_notify_cb (GtkSourceBuffer *buffer,
-                               GParamSpec      *spec,
-                               GtkLabel        *label)
+update_label (GtkSourceBuffer *buffer,
+             GtkLabel        *label)
 {
-       guint occurrences_count = gtk_source_buffer_get_search_occurrences_count (buffer);
-       gchar *text = g_strdup_printf ("%u occurrences", occurrences_count);
+       guint occurrences_count;
+       GtkTextIter select_start;
+       GtkTextIter select_end;
+       gint occurrence_pos;
+       gchar *text;
+
+       occurrences_count = gtk_source_buffer_get_search_occurrences_count (buffer);
+
+       gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (buffer),
+                                             &select_start,
+                                             &select_end);
+
+       occurrence_pos = gtk_source_buffer_get_search_occurrence_position (buffer,
+                                                                          &select_start,
+                                                                          &select_end);
+
+       if (occurrence_pos > 0)
+       {
+               text = g_strdup_printf ("%d of %u", occurrence_pos, occurrences_count);
+       }
+       else
+       {
+               text = g_strdup_printf ("%u occurrences", occurrences_count);
+       }
 
        gtk_label_set_text (label, text);
        g_free (text);
 }
 
 static void
+on_occurrences_count_notify_cb (GtkSourceBuffer *buffer,
+                               GParamSpec      *spec,
+                               GtkLabel        *label)
+{
+       update_label (buffer, label);
+}
+
+static void
 on_search_entry_text_notify_cb (GtkEntry        *entry,
                                GParamSpec      *spec,
                                GtkSourceBuffer *buffer)
@@ -156,32 +186,50 @@ on_button_next_clicked_cb (GtkButton   *button,
 }
 
 static void
-on_replace_cb (GtkButton      *button,
-              ReplaceWidgets *widgets)
+on_replace_cb (GtkButton    *button,
+              TestSearchUI *data)
 {
-       gtk_source_buffer_search_replace (widgets->buffer,
+       gtk_source_buffer_search_replace (data->buffer,
                                          NULL,
                                          NULL,
-                                         gtk_entry_get_text (widgets->replace_entry),
+                                         gtk_entry_get_text (data->replace_entry),
                                          -1);
 
-       gtk_source_buffer_forward_search_async (widgets->buffer,
+       gtk_source_buffer_forward_search_async (data->buffer,
                                                NULL,
                                                NULL,
                                                (GAsyncReadyCallback)forward_search_finished,
-                                               widgets->view);
+                                               data->view);
 }
 
 static void
-on_replace_all_cb (GtkButton      *button,
-                  ReplaceWidgets *widgets)
+on_replace_all_cb (GtkButton    *button,
+                  TestSearchUI *data)
 {
-       gtk_source_buffer_search_replace_all (widgets->buffer,
-                                             gtk_entry_get_text (widgets->replace_entry),
+       gtk_source_buffer_search_replace_all (data->buffer,
+                                             gtk_entry_get_text (data->replace_entry),
                                              -1);
 }
 
 static void
+on_mark_set_cb (GtkTextBuffer *buffer,
+               GtkTextIter   *location,
+               GtkTextMark   *mark,
+               TestSearchUI  *data)
+{
+       GtkTextMark *insert;
+       GtkTextMark *selection_bound;
+
+       insert = gtk_text_buffer_get_insert (buffer);
+       selection_bound = gtk_text_buffer_get_selection_bound (buffer);
+
+       if (mark == insert || mark == selection_bound)
+       {
+               update_label (data->buffer, data->label);
+       }
+}
+
+static void
 create_window (void)
 {
        GtkBuilder *builder;
@@ -190,7 +238,7 @@ create_window (void)
        GtkSourceView *source_view;
        GtkSourceBuffer *source_buffer;
        GtkSearchEntry *search_entry;
-       GtkLabel *label_occurrences_count;
+       GtkLabel *label_occurrences;
        GtkCheckButton *match_case;
        GtkCheckButton *at_word_boundaries;
        GtkCheckButton *wrap_around;
@@ -200,7 +248,7 @@ create_window (void)
        GtkButton *button_replace;
        GtkButton *button_replace_all;
        PangoFontDescription *font;
-       ReplaceWidgets *replace_widgets;
+       TestSearchUI *data;
 
        builder = gtk_builder_new ();
 
@@ -216,7 +264,7 @@ create_window (void)
        window = GTK_WINDOW (gtk_builder_get_object (builder, "window"));
        source_view = GTK_SOURCE_VIEW (gtk_builder_get_object (builder, "source_view"));
        search_entry = GTK_SEARCH_ENTRY (gtk_builder_get_object (builder, "search_entry"));
-       label_occurrences_count = GTK_LABEL (gtk_builder_get_object (builder, "label_occurrences_count"));
+       label_occurrences = GTK_LABEL (gtk_builder_get_object (builder, "label_occurrences"));
        match_case = GTK_CHECK_BUTTON (gtk_builder_get_object (builder, "checkbutton_match_case"));
        at_word_boundaries = GTK_CHECK_BUTTON (gtk_builder_get_object (builder, 
"checkbutton_at_word_boundaries"));
        wrap_around = GTK_CHECK_BUTTON (gtk_builder_get_object (builder, "checkbutton_wrap_around"));
@@ -243,6 +291,12 @@ create_window (void)
 
        open_file (source_buffer, TOP_SRCDIR "/gtksourceview/gtksourcesearch.c");
 
+       data = g_slice_new (TestSearchUI);
+       data->view = source_view;
+       data->buffer = source_buffer;
+       data->replace_entry = replace_entry;
+       data->label = label_occurrences;
+
        g_signal_connect (window,
                          "destroy",
                          G_CALLBACK (gtk_main_quit),
@@ -256,7 +310,7 @@ create_window (void)
        g_signal_connect (source_buffer,
                          "notify::search-occurrences-count",
                          G_CALLBACK (on_occurrences_count_notify_cb),
-                         label_occurrences_count);
+                         label_occurrences);
 
        g_signal_connect (button_previous,
                          "clicked",
@@ -280,20 +334,20 @@ create_window (void)
                                source_buffer, "search-wrap-around",
                                G_BINDING_SYNC_CREATE);
 
-       replace_widgets = g_slice_new (ReplaceWidgets);
-       replace_widgets->view = source_view;
-       replace_widgets->buffer = source_buffer;
-       replace_widgets->replace_entry = replace_entry;
-
        g_signal_connect (button_replace,
                          "clicked",
                          G_CALLBACK (on_replace_cb),
-                         replace_widgets);
+                         data);
 
        g_signal_connect (button_replace_all,
                          "clicked",
                          G_CALLBACK (on_replace_all_cb),
-                         replace_widgets);
+                         data);
+
+       g_signal_connect (source_buffer,
+                         "mark-set",
+                         G_CALLBACK (on_mark_set_cb),
+                         data);
 
        g_object_unref (builder);
 }
diff --git a/tests/test-search-ui.ui b/tests/test-search-ui.ui
index d171529..4deb16f 100644
--- a/tests/test-search-ui.ui
+++ b/tests/test-search-ui.ui
@@ -102,7 +102,7 @@
               </packing>
             </child>
             <child>
-              <object class="GtkLabel" id="label_occurrences_count">
+              <object class="GtkLabel" id="label_occurrences">
                 <property name="visible">True</property>
                 <property name="can_focus">False</property>
                 <property name="label">0 occurrences</property>



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