[gtksourceview/wip/regex-search: 8/10] Add gtk_source_buffer_disable_search_highlighting()



commit fd5b4006b05511924a41c2a7626d5f190cced062
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Thu Aug 1 14:15:15 2013 +0200

    Add gtk_source_buffer_disable_search_highlighting()
    
    The buffer must have a list of search contexts. It is done with
    _gtk_source_buffer_add_search_context(), and the buffer holds a weak
    reference to the context. It must not be the other way around, the
    context must have a strong reference to the buffer, because the context
    needs the buffer. On the other hand, the buffer can work without search
    contexts.

 docs/reference/gtksourceview-3.0-sections.txt |    1 +
 gtksourceview/gtksourcebuffer-private.h       |    4 +
 gtksourceview/gtksourcebuffer.c               |   82 ++++++++++++++++++++++++-
 gtksourceview/gtksourcebuffer.h               |    2 +
 gtksourceview/gtksourcesearchcontext.c        |   19 ++++++-
 5 files changed, 105 insertions(+), 3 deletions(-)
---
diff --git a/docs/reference/gtksourceview-3.0-sections.txt b/docs/reference/gtksourceview-3.0-sections.txt
index cde933e..a063ba9 100644
--- a/docs/reference/gtksourceview-3.0-sections.txt
+++ b/docs/reference/gtksourceview-3.0-sections.txt
@@ -26,6 +26,7 @@ gtk_source_buffer_iter_has_context_class
 gtk_source_buffer_get_context_classes_at_iter
 gtk_source_buffer_iter_forward_to_context_class_toggle
 gtk_source_buffer_iter_backward_to_context_class_toggle
+gtk_source_buffer_disable_search_highlighting
 <SUBSECTION>
 gtk_source_buffer_get_max_undo_levels
 gtk_source_buffer_set_max_undo_levels
diff --git a/gtksourceview/gtksourcebuffer-private.h b/gtksourceview/gtksourcebuffer-private.h
index 9273380..10ac6d4 100644
--- a/gtksourceview/gtksourcebuffer-private.h
+++ b/gtksourceview/gtksourcebuffer-private.h
@@ -46,6 +46,10 @@ GtkSourceMark                *_gtk_source_buffer_source_mark_prev            
(GtkSourceBuffer        *bu
 G_GNUC_INTERNAL
 GtkTextTag             *_gtk_source_buffer_get_bracket_match_tag       (GtkSourceBuffer        *buffer);
 
+G_GNUC_INTERNAL
+void                    _gtk_source_buffer_add_search_context          (GtkSourceBuffer        *buffer,
+                                                                        GtkSourceSearchContext 
*search_context);
+
 G_END_DECLS
 
 #endif /* __GTK_SOURCE_BUFFER_PRIVATE_H__ */
diff --git a/gtksourceview/gtksourcebuffer.c b/gtksourceview/gtksourcebuffer.c
index 737cf48..1178dcd 100644
--- a/gtksourceview/gtksourcebuffer.c
+++ b/gtksourceview/gtksourcebuffer.c
@@ -153,6 +153,8 @@ struct _GtkSourceBufferPrivate
        GtkSourceUndoManager  *undo_manager;
        gint                   max_undo_levels;
 
+       GList                 *search_contexts;
+
        guint                  highlight_syntax : 1;
        guint                  highlight_brackets : 1;
        guint                  constructed : 1;
@@ -467,6 +469,14 @@ set_undo_manager (GtkSourceBuffer      *buffer,
 }
 
 static void
+search_context_weak_notify_cb (GtkSourceBuffer *buffer,
+                              GObject         *search_context)
+{
+       buffer->priv->search_contexts = g_list_remove (buffer->priv->search_contexts,
+                                                      search_context);
+}
+
+static void
 gtk_source_buffer_init (GtkSourceBuffer *buffer)
 {
        GtkSourceBufferPrivate *priv = gtk_source_buffer_get_instance_private (buffer);
@@ -509,6 +519,7 @@ static void
 gtk_source_buffer_dispose (GObject *object)
 {
        GtkSourceBuffer *buffer;
+       GList *l;
 
        g_return_if_fail (object != NULL);
        g_return_if_fail (GTK_SOURCE_IS_BUFFER (object));
@@ -529,6 +540,18 @@ gtk_source_buffer_dispose (GObject *object)
        g_clear_object (&buffer->priv->language);
        g_clear_object (&buffer->priv->style_scheme);
 
+       for (l = buffer->priv->search_contexts; l != NULL; l = l->next)
+       {
+               GtkSourceSearchContext *search_context = l->data;
+
+               g_object_weak_unref (G_OBJECT (search_context),
+                                    (GWeakNotify)search_context_weak_notify_cb,
+                                    buffer);
+       }
+
+       g_list_free (buffer->priv->search_contexts);
+       buffer->priv->search_contexts = NULL;
+
        G_OBJECT_CLASS (gtk_source_buffer_parent_class)->dispose (object);
 }
 
@@ -1539,6 +1562,8 @@ _gtk_source_buffer_update_highlight (GtkSourceBuffer   *buffer,
                                     const GtkTextIter *end,
                                     gboolean           synchronous)
 {
+       GList *l;
+
        g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
 
        if (buffer->priv->highlight_engine != NULL)
@@ -1549,8 +1574,15 @@ _gtk_source_buffer_update_highlight (GtkSourceBuffer   *buffer,
                                                     synchronous);
        }
 
-       /* TODO: update highlighting for the currently highlighted search
-        * context. */
+       for (l = buffer->priv->search_contexts; l != NULL; l = l->next)
+       {
+               GtkSourceSearchContext *search_context = l->data;
+
+               _gtk_source_search_context_update_highlight (search_context,
+                                                            start,
+                                                            end,
+                                                            synchronous);
+       }
 }
 
 /**
@@ -2491,3 +2523,49 @@ gtk_source_buffer_get_undo_manager (GtkSourceBuffer *buffer)
 
        return buffer->priv->undo_manager;
 }
+
+void
+_gtk_source_buffer_add_search_context (GtkSourceBuffer        *buffer,
+                                      GtkSourceSearchContext *search_context)
+{
+       g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
+       g_return_if_fail (GTK_SOURCE_IS_SEARCH_CONTEXT (search_context));
+       g_return_if_fail (gtk_source_search_context_get_buffer (search_context) == buffer);
+
+       if (g_list_find (buffer->priv->search_contexts, search_context) != NULL)
+       {
+               return;
+       }
+
+       buffer->priv->search_contexts = g_list_prepend (buffer->priv->search_contexts,
+                                                       search_context);
+
+       g_object_weak_ref (G_OBJECT (search_context),
+                          (GWeakNotify)search_context_weak_notify_cb,
+                          buffer);
+}
+
+/**
+ * gtk_source_buffer_disable_search_highlighting:
+ * @buffer: a #GtkSourceBuffer.
+ *
+ * Disables the search occurrences highlighting, for all
+ * #GtkSourceSearchContext<!-- -->s attached to @buffer. The search highlighting
+ * can be activated again with gtk_source_search_context_set_highlight().
+ *
+ * Since: 3.10
+ */
+void
+gtk_source_buffer_disable_search_highlighting (GtkSourceBuffer *buffer)
+{
+       GList *l;
+
+       g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
+
+       for (l = buffer->priv->search_contexts; l != NULL; l = l->next)
+       {
+               GtkSourceSearchContext *search_context = l->data;
+
+               gtk_source_search_context_set_highlight (search_context, FALSE);
+       }
+}
diff --git a/gtksourceview/gtksourcebuffer.h b/gtksourceview/gtksourcebuffer.h
index 9026ca2..5810d0d 100644
--- a/gtksourceview/gtksourcebuffer.h
+++ b/gtksourceview/gtksourcebuffer.h
@@ -180,6 +180,8 @@ GtkSourceUndoManager        *gtk_source_buffer_get_undo_manager                     
(GtkSourceBuffer        *buf
 void                    gtk_source_buffer_set_undo_manager                     (GtkSourceBuffer        
*buffer,
                                                                                 GtkSourceUndoManager   
*manager);
 
+void                    gtk_source_buffer_disable_search_highlighting          (GtkSourceBuffer        
*buffer);
+
 G_END_DECLS
 
 #endif /* __GTK_SOURCE_BUFFER_H__ */
diff --git a/gtksourceview/gtksourcesearchcontext.c b/gtksourceview/gtksourcesearchcontext.c
index b715548..6ec7259 100644
--- a/gtksourceview/gtksourcesearchcontext.c
+++ b/gtksourceview/gtksourcesearchcontext.c
@@ -22,6 +22,7 @@
 #include "gtksourcesearchcontext.h"
 #include "gtksourcesearchsettings.h"
 #include "gtksourcebuffer.h"
+#include "gtksourcebuffer-private.h"
 #include "gtksourcestylescheme.h"
 #include "gtksourcestyle-private.h"
 #include "gtksourceutils.h"
@@ -57,6 +58,19 @@
  * matches, use gtk_source_search_context_replace() and
  * gtk_source_search_context_replace_all().
  *
+ * The search occurrences are highlighted by default. To disable it, use
+ * gtk_source_search_context_set_highlight(). The
+ * gtk_source_buffer_disable_search_highlighting() convenience function permits
+ * to disable the search highlighting in a buffer. The
+ * #GtkSourceSearchContext:highlight property is in the #GtkSourceSearchContext
+ * class. If the property was in #GtkSourceSearchSettings, calling
+ * gtk_source_buffer_disable_search_highlighting() might disable the
+ * highlighting in other buffers, which would be undesirable. It is therefore
+ * better to have the appearance settings in the #GtkSourceSearchContext class.
+ * You can enable the search highlighting for several
+ * #GtkSourceSearchContext<!-- -->s attached to the same buffer. But, currently,
+ * the same highlighting style is applied.
+ *
  * In the GtkSourceView source code, there is an example of how to use the
  * search and replace API: see the tests/test-search.c file. It is a mini
  * application for the search and replace, with a basic user interface.
@@ -2437,6 +2451,8 @@ set_buffer (GtkSourceSearchContext *search,
                                 G_CALLBACK (sync_found_tag),
                                 search,
                                 G_CONNECT_SWAPPED);
+
+       _gtk_source_buffer_add_search_context (buffer, search);
 }
 
 static gint
@@ -3492,7 +3508,8 @@ _gtk_source_search_context_update_highlight (GtkSourceSearchContext *search,
        g_return_if_fail (end != NULL);
 
        if (dispose_has_run (search) ||
-           is_text_region_empty (search->priv->scan_region))
+           is_text_region_empty (search->priv->scan_region) ||
+           !search->priv->highlight)
        {
                return;
        }


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