[gtksourceview/wip/search] Add API to enable/disable search highlighting



commit 463185fdc4969cbf418197e0d46a7288b2445f8f
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Jul 12 21:36:02 2013 +0200

    Add API to enable/disable search highlighting
    
    And add a button in test-search-ui to test the new setting.

 docs/reference/gtksourceview-3.0-sections.txt |    2 +
 gtksourceview/gtksourcebuffer.c               |   72 ++++++++++++++++++++++++-
 gtksourceview/gtksourcebuffer.h               |    5 ++
 gtksourceview/gtksourcesearch.c               |   29 ++++++++++
 gtksourceview/gtksourcesearch.h               |    7 +++
 tests/test-search-ui.c                        |    9 +++
 tests/test-search-ui.ui                       |   24 +++++++-
 7 files changed, 143 insertions(+), 5 deletions(-)
---
diff --git a/docs/reference/gtksourceview-3.0-sections.txt b/docs/reference/gtksourceview-3.0-sections.txt
index a432ef3..e04cebc 100644
--- a/docs/reference/gtksourceview-3.0-sections.txt
+++ b/docs/reference/gtksourceview-3.0-sections.txt
@@ -46,6 +46,8 @@ gtk_source_buffer_set_search_at_word_boundaries
 gtk_source_buffer_get_search_at_word_boundaries
 gtk_source_buffer_set_search_wrap_around
 gtk_source_buffer_get_search_wrap_around
+gtk_source_buffer_set_highlight_search
+gtk_source_buffer_get_highlight_search
 gtk_source_buffer_get_search_occurrences_count
 gtk_source_buffer_get_search_occurrence_position
 gtk_source_buffer_forward_search
diff --git a/gtksourceview/gtksourcebuffer.c b/gtksourceview/gtksourcebuffer.c
index d99fc81..9031614 100644
--- a/gtksourceview/gtksourcebuffer.c
+++ b/gtksourceview/gtksourcebuffer.c
@@ -162,6 +162,7 @@ enum {
        PROP_CAN_REDO,
        PROP_HIGHLIGHT_SYNTAX,
        PROP_HIGHLIGHT_MATCHING_BRACKETS,
+       PROP_HIGHLIGHT_SEARCH,
        PROP_MAX_UNDO_LEVELS,
        PROP_LANGUAGE,
        PROP_STYLE_SCHEME,
@@ -321,6 +322,19 @@ gtk_source_buffer_class_init (GtkSourceBufferClass *klass)
                                                               G_PARAM_READWRITE));
 
        /**
+        * GtkSourceBuffer:highlight-search:
+        *
+        * Whether to highlight search occurrences in the buffer.
+        */
+       g_object_class_install_property (object_class,
+                                        PROP_HIGHLIGHT_SEARCH,
+                                        g_param_spec_boolean ("highlight-search",
+                                                              _("Highlight Search"),
+                                                              _("Whether to highlight search occurrences"),
+                                                              TRUE,
+                                                              G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
+
+       /**
         * GtkSourceBuffer:max-undo-levels:
         *
         * Number of undo levels for the buffer. -1 means no limit. This property
@@ -669,12 +683,17 @@ gtk_source_buffer_set_property (GObject      *object,
        {
                case PROP_HIGHLIGHT_SYNTAX:
                        gtk_source_buffer_set_highlight_syntax (source_buffer,
-                                                             g_value_get_boolean (value));
+                                                               g_value_get_boolean (value));
                        break;
 
                case PROP_HIGHLIGHT_MATCHING_BRACKETS:
                        gtk_source_buffer_set_highlight_matching_brackets (source_buffer,
-                                                               g_value_get_boolean (value));
+                                                                          g_value_get_boolean (value));
+                       break;
+
+               case PROP_HIGHLIGHT_SEARCH:
+                       _gtk_source_search_set_highlight (source_buffer->priv->search,
+                                                         g_value_get_boolean (value));
                        break;
 
                case PROP_MAX_UNDO_LEVELS:
@@ -747,6 +766,10 @@ gtk_source_buffer_get_property (GObject    *object,
                                             source_buffer->priv->highlight_brackets);
                        break;
 
+               case PROP_HIGHLIGHT_SEARCH:
+                       g_value_set_boolean (value, _gtk_source_search_get_highlight 
(source_buffer->priv->search));
+                       break;
+
                case PROP_MAX_UNDO_LEVELS:
                        g_value_set_int (value,
                                         source_buffer->priv->max_undo_levels);
@@ -2853,6 +2876,51 @@ gtk_source_buffer_get_search_wrap_around (GtkSourceBuffer *buffer)
 }
 
 /**
+ * gtk_source_buffer_set_highlight_search:
+ * @buffer: a #GtkSourceBuffer.
+ * @highlight: the setting.
+ *
+ * Enables or disables search highlighting. If you disable the search
+ * highlighting, you can still use the other search and replace functions.
+ *
+ * Since: 3.10
+ */
+void
+gtk_source_buffer_set_highlight_search (GtkSourceBuffer *buffer,
+                                       gboolean         highlight)
+{
+       gboolean cur_val;
+
+       g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
+
+       highlight = highlight != FALSE;
+
+       cur_val = _gtk_source_search_get_highlight (buffer->priv->search);
+
+       if (cur_val != highlight)
+       {
+               _gtk_source_search_set_highlight (buffer->priv->search, highlight);
+
+               g_object_notify (G_OBJECT (buffer), "highlight-search");
+       }
+}
+
+/**
+ * gtk_source_buffer_get_highlight_search:
+ * @buffer: a #GtkSourceBuffer.
+ *
+ * Returns: whether to highlight search occurrences.
+ * Since: 3.10
+ */
+gboolean
+gtk_source_buffer_get_highlight_search (GtkSourceBuffer *buffer)
+{
+       g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), FALSE);
+
+       return _gtk_source_search_get_highlight (buffer->priv->search);
+}
+
+/**
  * gtk_source_buffer_get_search_occurrences_count:
  * @buffer: a #GtkSourceBuffer.
  *
diff --git a/gtksourceview/gtksourcebuffer.h b/gtksourceview/gtksourcebuffer.h
index 44dfa4f..632a80d 100644
--- a/gtksourceview/gtksourcebuffer.h
+++ b/gtksourceview/gtksourcebuffer.h
@@ -200,6 +200,11 @@ void                        gtk_source_buffer_set_search_wrap_around               
(GtkSourceBuffer        *buffer,
 
 gboolean                gtk_source_buffer_get_search_wrap_around               (GtkSourceBuffer        
*buffer);
 
+void                    gtk_source_buffer_set_highlight_search                 (GtkSourceBuffer        
*buffer,
+                                                                                gboolean                
highlight);
+
+gboolean                gtk_source_buffer_get_highlight_search                 (GtkSourceBuffer        
*buffer);
+
 guint                   gtk_source_buffer_get_search_occurrences_count         (GtkSourceBuffer        
*buffer);
 
 gint                    gtk_source_buffer_get_search_occurrence_position       (GtkSourceBuffer        
*buffer,
diff --git a/gtksourceview/gtksourcesearch.c b/gtksourceview/gtksourcesearch.c
index 8d3c5d8..d087c63 100644
--- a/gtksourceview/gtksourcesearch.c
+++ b/gtksourceview/gtksourcesearch.c
@@ -145,6 +145,7 @@ struct _GtkSourceSearchPrivate
        GtkTextSearchFlags flags;
        guint at_word_boundaries : 1;
        guint wrap_around : 1;
+       guint highlight : 1;
 };
 
 /* Data for the asynchronous forward and backward search tasks. */
@@ -181,6 +182,12 @@ sync_found_tag (GtkSourceSearch *search)
                return;
        }
 
+       if (!search->priv->highlight)
+       {
+               _gtk_source_style_apply (NULL, search->priv->found_tag);
+               return;
+       }
+
        style_scheme = gtk_source_buffer_get_style_scheme (GTK_SOURCE_BUFFER (search->priv->buffer));
 
        if (style_scheme != NULL)
@@ -1757,6 +1764,28 @@ _gtk_source_search_get_wrap_around (GtkSourceSearch *search)
        return search->priv->wrap_around;
 }
 
+void
+_gtk_source_search_set_highlight (GtkSourceSearch *search,
+                                 gboolean         highlight)
+{
+       g_return_if_fail (GTK_SOURCE_IS_SEARCH (search));
+
+       search->priv->highlight = highlight;
+
+       if (search->priv->found_tag != NULL)
+       {
+               sync_found_tag (search);
+       }
+}
+
+gboolean
+_gtk_source_search_get_highlight (GtkSourceSearch *search)
+{
+       g_return_val_if_fail (GTK_SOURCE_IS_SEARCH (search), FALSE);
+
+       return search->priv->highlight;
+}
+
 guint
 _gtk_source_search_get_occurrences_count (GtkSourceSearch *search)
 {
diff --git a/gtksourceview/gtksourcesearch.h b/gtksourceview/gtksourcesearch.h
index 19c6cfa..f745a4f 100644
--- a/gtksourceview/gtksourcesearch.h
+++ b/gtksourceview/gtksourcesearch.h
@@ -86,6 +86,13 @@ G_GNUC_INTERNAL
 gboolean               _gtk_source_search_get_wrap_around              (GtkSourceSearch        *search);
 
 G_GNUC_INTERNAL
+void                   _gtk_source_search_set_highlight                (GtkSourceSearch        *search,
+                                                                        gboolean                highlight);
+
+G_GNUC_INTERNAL
+gboolean               _gtk_source_search_get_highlight                (GtkSourceSearch        *search);
+
+G_GNUC_INTERNAL
 guint                  _gtk_source_search_get_occurrences_count        (GtkSourceSearch        *search);
 
 G_GNUC_INTERNAL
diff --git a/tests/test-search-ui.c b/tests/test-search-ui.c
index a45f687..c953845 100644
--- a/tests/test-search-ui.c
+++ b/tests/test-search-ui.c
@@ -307,6 +307,14 @@ mark_set_cb (GtkTextBuffer *buffer,
 }
 
 static void
+highlight_toggled_cb (TestSearchUI    *search,
+                     GtkToggleButton *button)
+{
+       gtk_source_buffer_set_highlight_search (search->priv->source_buffer,
+                                               gtk_toggle_button_get_active (button));
+}
+
+static void
 match_case_toggled_cb (TestSearchUI    *search,
                       GtkToggleButton *button)
 {
@@ -365,6 +373,7 @@ test_search_ui_class_init (TestSearchUIClass *klass)
         * g_object_bind_property(), between the check buttons and the source
         * buffer. But GtkBuilder and Glade don't support that yet.
         */
+       gtk_widget_class_bind_callback (widget_class, highlight_toggled_cb);
        gtk_widget_class_bind_callback (widget_class, match_case_toggled_cb);
        gtk_widget_class_bind_callback (widget_class, at_word_boundaries_toggled_cb);
        gtk_widget_class_bind_callback (widget_class, wrap_around_toggled_cb);
diff --git a/tests/test-search-ui.ui b/tests/test-search-ui.ui
index 3d6bbdd..7da2630 100644
--- a/tests/test-search-ui.ui
+++ b/tests/test-search-ui.ui
@@ -160,7 +160,7 @@
           </object>
           <packing>
             <property name="left_attach">0</property>
-            <property name="top_attach">0</property>
+            <property name="top_attach">1</property>
             <property name="width">1</property>
             <property name="height">1</property>
           </packing>
@@ -177,7 +177,7 @@
           </object>
           <packing>
             <property name="left_attach">0</property>
-            <property name="top_attach">1</property>
+            <property name="top_attach">2</property>
             <property name="width">1</property>
             <property name="height">1</property>
           </packing>
@@ -195,7 +195,25 @@
           </object>
           <packing>
             <property name="left_attach">0</property>
-            <property name="top_attach">2</property>
+            <property name="top_attach">3</property>
+            <property name="width">1</property>
+            <property name="height">1</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkCheckButton" id="checkbutton_highlight">
+            <property name="label">Highlight search occurrences</property>
+            <property name="visible">True</property>
+            <property name="can_focus">True</property>
+            <property name="receives_default">False</property>
+            <property name="xalign">0</property>
+            <property name="active">True</property>
+            <property name="draw_indicator">True</property>
+            <signal name="toggled" handler="highlight_toggled_cb" object="TestSearchUI" swapped="yes"/>
+          </object>
+          <packing>
+            <property name="left_attach">0</property>
+            <property name="top_attach">0</property>
             <property name="width">1</property>
             <property name="height">1</property>
           </packing>


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