Blame gtksourceview/gtksourcespacedrawer.c

Packit a7d494
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- */
Packit a7d494
/* gtksourcespacedrawer.c
Packit a7d494
 * This file is part of GtkSourceView
Packit a7d494
 *
Packit a7d494
 * Copyright (C) 2008, 2011, 2016 - Paolo Borelli <pborelli@gnome.org>
Packit a7d494
 * Copyright (C) 2008, 2010 - Ignacio Casal Quinteiro <icq@gnome.org>
Packit a7d494
 * Copyright (C) 2010 - Garret Regier
Packit a7d494
 * Copyright (C) 2013 - Arpad Borsos <arpad.borsos@googlemail.com>
Packit a7d494
 * Copyright (C) 2015, 2016 - Sébastien Wilmet <swilmet@gnome.org>
Packit a7d494
 * Copyright (C) 2016 - Christian Hergert <christian@hergert.me>
Packit a7d494
 *
Packit a7d494
 * GtkSourceView is free software; you can redistribute it and/or
Packit a7d494
 * modify it under the terms of the GNU Lesser General Public
Packit a7d494
 * License as published by the Free Software Foundation; either
Packit a7d494
 * version 2.1 of the License, or (at your option) any later version.
Packit a7d494
 *
Packit a7d494
 * GtkSourceView is distributed in the hope that it will be useful,
Packit a7d494
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit a7d494
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit a7d494
 * Lesser General Public License for more details.
Packit a7d494
 *
Packit a7d494
 * You should have received a copy of the GNU Lesser General Public
Packit a7d494
 * License along with this library; if not, write to the Free Software
Packit a7d494
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
Packit a7d494
 */
Packit a7d494
Packit a7d494
#ifdef HAVE_CONFIG_H
Packit a7d494
#include <config.h>
Packit a7d494
#endif
Packit a7d494
Packit a7d494
#include "gtksourcespacedrawer.h"
Packit a7d494
#include "gtksourcespacedrawer-private.h"
Packit a7d494
#include "gtksourcebuffer.h"
Packit a7d494
#include "gtksourceiter.h"
Packit a7d494
#include "gtksourcestylescheme.h"
Packit a7d494
#include "gtksourcetag.h"
Packit a7d494
Packit a7d494
/**
Packit a7d494
 * SECTION:spacedrawer
Packit a7d494
 * @Short_description: Represent white space characters with symbols
Packit a7d494
 * @Title: GtkSourceSpaceDrawer
Packit a7d494
 * @See_also: #GtkSourceView
Packit a7d494
 *
Packit a7d494
 * #GtkSourceSpaceDrawer provides a way to visualize white spaces, by drawing
Packit a7d494
 * symbols.
Packit a7d494
 *
Packit a7d494
 * Call gtk_source_view_get_space_drawer() to get the #GtkSourceSpaceDrawer
Packit a7d494
 * instance of a certain #GtkSourceView.
Packit a7d494
 *
Packit a7d494
 * By default, no white spaces are drawn because the
Packit a7d494
 * #GtkSourceSpaceDrawer:enable-matrix is %FALSE.
Packit a7d494
 *
Packit a7d494
 * To draw white spaces, gtk_source_space_drawer_set_types_for_locations() can
Packit a7d494
 * be called to set the #GtkSourceSpaceDrawer:matrix property (by default all
Packit a7d494
 * space types are enabled at all locations). Then call
Packit a7d494
 * gtk_source_space_drawer_set_enable_matrix().
Packit a7d494
 *
Packit a7d494
 * For a finer-grained method, there is also the GtkSourceTag's
Packit a7d494
 * #GtkSourceTag:draw-spaces property.
Packit a7d494
 *
Packit a7d494
 * # Example
Packit a7d494
 *
Packit a7d494
 * To draw non-breaking spaces everywhere and draw all types of trailing spaces
Packit a7d494
 * except newlines:
Packit a7d494
 * |[
Packit a7d494
 * gtk_source_space_drawer_set_types_for_locations (space_drawer,
Packit a7d494
 *                                                  GTK_SOURCE_SPACE_LOCATION_ALL,
Packit a7d494
 *                                                  GTK_SOURCE_SPACE_TYPE_NBSP);
Packit a7d494
 *
Packit a7d494
 * gtk_source_space_drawer_set_types_for_locations (space_drawer,
Packit a7d494
 *                                                  GTK_SOURCE_SPACE_LOCATION_TRAILING,
Packit a7d494
 *                                                  GTK_SOURCE_SPACE_TYPE_ALL &
Packit a7d494
 *                                                  ~GTK_SOURCE_SPACE_TYPE_NEWLINE);
Packit a7d494
 *
Packit a7d494
 * gtk_source_space_drawer_set_enable_matrix (space_drawer, TRUE);
Packit a7d494
 * ]|
Packit a7d494
 *
Packit a7d494
 * # Use-case: draw unwanted white spaces
Packit a7d494
 *
Packit a7d494
 * A possible use-case is to draw only unwanted white spaces. Examples:
Packit a7d494
 * - Draw all trailing spaces.
Packit a7d494
 * - If the indentation and alignment must be done with spaces, draw tabs.
Packit a7d494
 *
Packit a7d494
 * And non-breaking spaces can always be drawn, everywhere, to distinguish them
Packit a7d494
 * from normal spaces.
Packit a7d494
 */
Packit a7d494
Packit a7d494
/* A drawer specially designed for the International Space Station. It comes by
Packit a7d494
 * default with a DVD of Matrix, in case the astronauts are bored.
Packit a7d494
 */
Packit a7d494
Packit a7d494
/*
Packit a7d494
#define ENABLE_PROFILE
Packit a7d494
*/
Packit a7d494
#undef ENABLE_PROFILE
Packit a7d494
Packit a7d494
struct _GtkSourceSpaceDrawerPrivate
Packit a7d494
{
Packit a7d494
	GtkSourceSpaceTypeFlags *matrix;
Packit a7d494
	GdkRGBA *color;
Packit a7d494
	guint enable_matrix : 1;
Packit a7d494
};
Packit a7d494
Packit a7d494
enum
Packit a7d494
{
Packit a7d494
	PROP_0,
Packit a7d494
	PROP_ENABLE_MATRIX,
Packit a7d494
	PROP_MATRIX,
Packit a7d494
	N_PROPERTIES
Packit a7d494
};
Packit a7d494
Packit a7d494
static GParamSpec *properties[N_PROPERTIES];
Packit a7d494
Packit a7d494
G_DEFINE_TYPE_WITH_PRIVATE (GtkSourceSpaceDrawer, gtk_source_space_drawer, G_TYPE_OBJECT)
Packit a7d494
Packit a7d494
static gint
Packit a7d494
get_number_of_locations (void)
Packit a7d494
{
Packit a7d494
	gint num;
Packit a7d494
	gint flags;
Packit a7d494
Packit a7d494
	num = 0;
Packit a7d494
	flags = GTK_SOURCE_SPACE_LOCATION_ALL;
Packit a7d494
Packit a7d494
	while (flags != 0)
Packit a7d494
	{
Packit a7d494
		flags >>= 1;
Packit a7d494
		num++;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	return num;
Packit a7d494
}
Packit a7d494
Packit a7d494
static GVariant *
Packit a7d494
get_default_matrix (void)
Packit a7d494
{
Packit a7d494
	GVariantBuilder builder;
Packit a7d494
	gint num_locations;
Packit a7d494
	gint i;
Packit a7d494
Packit a7d494
	g_variant_builder_init (&builder, G_VARIANT_TYPE ("au"));
Packit a7d494
Packit a7d494
	num_locations = get_number_of_locations ();
Packit a7d494
Packit a7d494
	for (i = 0; i < num_locations; i++)
Packit a7d494
	{
Packit a7d494
		GVariant *space_types;
Packit a7d494
Packit a7d494
		space_types = g_variant_new_uint32 (GTK_SOURCE_SPACE_TYPE_ALL);
Packit a7d494
Packit a7d494
		g_variant_builder_add_value (&builder, space_types);
Packit a7d494
	}
Packit a7d494
Packit a7d494
	return g_variant_builder_end (&builder);
Packit a7d494
}
Packit a7d494
Packit a7d494
static gboolean
Packit a7d494
is_zero_matrix (GtkSourceSpaceDrawer *drawer)
Packit a7d494
{
Packit a7d494
	gint num_locations;
Packit a7d494
	gint i;
Packit a7d494
Packit a7d494
	num_locations = get_number_of_locations ();
Packit a7d494
Packit a7d494
	for (i = 0; i < num_locations; i++)
Packit a7d494
	{
Packit a7d494
		if (drawer->priv->matrix[i] != 0)
Packit a7d494
		{
Packit a7d494
			return FALSE;
Packit a7d494
		}
Packit a7d494
	}
Packit a7d494
Packit a7d494
	return TRUE;
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
set_zero_matrix (GtkSourceSpaceDrawer *drawer)
Packit a7d494
{
Packit a7d494
	gint num_locations;
Packit a7d494
	gint i;
Packit a7d494
	gboolean changed = FALSE;
Packit a7d494
Packit a7d494
	num_locations = get_number_of_locations ();
Packit a7d494
Packit a7d494
	for (i = 0; i < num_locations; i++)
Packit a7d494
	{
Packit a7d494
		if (drawer->priv->matrix[i] != 0)
Packit a7d494
		{
Packit a7d494
			drawer->priv->matrix[i] = 0;
Packit a7d494
			changed = TRUE;
Packit a7d494
		}
Packit a7d494
	}
Packit a7d494
Packit a7d494
	if (changed)
Packit a7d494
	{
Packit a7d494
		g_object_notify_by_pspec (G_OBJECT (drawer), properties[PROP_MATRIX]);
Packit a7d494
	}
Packit a7d494
}
Packit a7d494
Packit a7d494
/* AND */
Packit a7d494
static GtkSourceSpaceTypeFlags
Packit a7d494
get_types_at_all_locations (GtkSourceSpaceDrawer        *drawer,
Packit a7d494
			    GtkSourceSpaceLocationFlags  locations)
Packit a7d494
{
Packit a7d494
	GtkSourceSpaceTypeFlags ret = GTK_SOURCE_SPACE_TYPE_ALL;
Packit a7d494
	gint index;
Packit a7d494
	gint num_locations;
Packit a7d494
	gboolean found;
Packit a7d494
Packit a7d494
	index = 0;
Packit a7d494
	num_locations = get_number_of_locations ();
Packit a7d494
	found = FALSE;
Packit a7d494
Packit a7d494
	while (locations != 0 && index < num_locations)
Packit a7d494
	{
Packit a7d494
		if ((locations & 1) == 1)
Packit a7d494
		{
Packit a7d494
			ret &= drawer->priv->matrix[index];
Packit a7d494
			found = TRUE;
Packit a7d494
		}
Packit a7d494
Packit a7d494
		locations >>= 1;
Packit a7d494
		index++;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	return found ? ret : GTK_SOURCE_SPACE_TYPE_NONE;
Packit a7d494
}
Packit a7d494
Packit a7d494
/* OR */
Packit a7d494
static GtkSourceSpaceTypeFlags
Packit a7d494
get_types_at_any_locations (GtkSourceSpaceDrawer        *drawer,
Packit a7d494
			    GtkSourceSpaceLocationFlags  locations)
Packit a7d494
{
Packit a7d494
	GtkSourceSpaceTypeFlags ret = GTK_SOURCE_SPACE_TYPE_NONE;
Packit a7d494
	gint index;
Packit a7d494
	gint num_locations;
Packit a7d494
Packit a7d494
	index = 0;
Packit a7d494
	num_locations = get_number_of_locations ();
Packit a7d494
Packit a7d494
	while (locations != 0 && index < num_locations)
Packit a7d494
	{
Packit a7d494
		if ((locations & 1) == 1)
Packit a7d494
		{
Packit a7d494
			ret |= drawer->priv->matrix[index];
Packit a7d494
		}
Packit a7d494
Packit a7d494
		locations >>= 1;
Packit a7d494
		index++;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	return ret;
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
gtk_source_space_drawer_get_property (GObject    *object,
Packit a7d494
				      guint       prop_id,
Packit a7d494
				      GValue     *value,
Packit a7d494
				      GParamSpec *pspec)
Packit a7d494
{
Packit a7d494
	GtkSourceSpaceDrawer *drawer = GTK_SOURCE_SPACE_DRAWER (object);
Packit a7d494
Packit a7d494
	switch (prop_id)
Packit a7d494
	{
Packit a7d494
		case PROP_ENABLE_MATRIX:
Packit a7d494
			g_value_set_boolean (value, gtk_source_space_drawer_get_enable_matrix (drawer));
Packit a7d494
			break;
Packit a7d494
Packit a7d494
		case PROP_MATRIX:
Packit a7d494
			g_value_set_variant (value, gtk_source_space_drawer_get_matrix (drawer));
Packit a7d494
			break;
Packit a7d494
Packit a7d494
		default:
Packit a7d494
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit a7d494
			break;
Packit a7d494
	}
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
gtk_source_space_drawer_set_property (GObject      *object,
Packit a7d494
				      guint         prop_id,
Packit a7d494
				      const GValue *value,
Packit a7d494
				      GParamSpec   *pspec)
Packit a7d494
{
Packit a7d494
	GtkSourceSpaceDrawer *drawer = GTK_SOURCE_SPACE_DRAWER (object);
Packit a7d494
Packit a7d494
	switch (prop_id)
Packit a7d494
	{
Packit a7d494
		case PROP_ENABLE_MATRIX:
Packit a7d494
			gtk_source_space_drawer_set_enable_matrix (drawer, g_value_get_boolean (value));
Packit a7d494
			break;
Packit a7d494
Packit a7d494
		case PROP_MATRIX:
Packit a7d494
			gtk_source_space_drawer_set_matrix (drawer, g_value_get_variant (value));
Packit a7d494
			break;
Packit a7d494
Packit a7d494
		default:
Packit a7d494
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit a7d494
			break;
Packit a7d494
	}
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
gtk_source_space_drawer_finalize (GObject *object)
Packit a7d494
{
Packit a7d494
	GtkSourceSpaceDrawer *drawer = GTK_SOURCE_SPACE_DRAWER (object);
Packit a7d494
Packit a7d494
	g_free (drawer->priv->matrix);
Packit a7d494
Packit a7d494
	if (drawer->priv->color != NULL)
Packit a7d494
	{
Packit a7d494
		gdk_rgba_free (drawer->priv->color);
Packit a7d494
	}
Packit a7d494
Packit a7d494
	G_OBJECT_CLASS (gtk_source_space_drawer_parent_class)->finalize (object);
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
gtk_source_space_drawer_class_init (GtkSourceSpaceDrawerClass *klass)
Packit a7d494
{
Packit a7d494
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
Packit a7d494
Packit a7d494
	object_class->get_property = gtk_source_space_drawer_get_property;
Packit a7d494
	object_class->set_property = gtk_source_space_drawer_set_property;
Packit a7d494
	object_class->finalize = gtk_source_space_drawer_finalize;
Packit a7d494
Packit a7d494
	/**
Packit a7d494
	 * GtkSourceSpaceDrawer:enable-matrix:
Packit a7d494
	 *
Packit a7d494
	 * Whether the #GtkSourceSpaceDrawer:matrix property is enabled.
Packit a7d494
	 *
Packit a7d494
	 * Since: 3.24
Packit a7d494
	 */
Packit a7d494
	properties[PROP_ENABLE_MATRIX] =
Packit a7d494
		g_param_spec_boolean ("enable-matrix",
Packit a7d494
				      "Enable Matrix",
Packit a7d494
				      "",
Packit a7d494
				      FALSE,
Packit a7d494
				      G_PARAM_READWRITE |
Packit a7d494
				      G_PARAM_CONSTRUCT |
Packit a7d494
				      G_PARAM_STATIC_STRINGS);
Packit a7d494
Packit a7d494
	/**
Packit a7d494
	 * GtkSourceSpaceDrawer:matrix:
Packit a7d494
	 *
Packit a7d494
	 * The :matrix property is a #GVariant property to specify where and
Packit a7d494
	 * what kind of white spaces to draw.
Packit a7d494
	 *
Packit a7d494
	 * The #GVariant is of type `"au"`, an array of unsigned integers. Each
Packit a7d494
	 * integer is a combination of #GtkSourceSpaceTypeFlags. There is one
Packit a7d494
	 * integer for each #GtkSourceSpaceLocationFlags, in the same order as
Packit a7d494
	 * they are defined in the enum (%GTK_SOURCE_SPACE_LOCATION_NONE and
Packit a7d494
	 * %GTK_SOURCE_SPACE_LOCATION_ALL are not taken into account).
Packit a7d494
	 *
Packit a7d494
	 * If the array is shorter than the number of locations, then the value
Packit a7d494
	 * for the missing locations will be %GTK_SOURCE_SPACE_TYPE_NONE.
Packit a7d494
	 *
Packit a7d494
	 * By default, %GTK_SOURCE_SPACE_TYPE_ALL is set for all locations.
Packit a7d494
	 *
Packit a7d494
	 * Since: 3.24
Packit a7d494
	 */
Packit a7d494
	properties[PROP_MATRIX] =
Packit a7d494
		g_param_spec_variant ("matrix",
Packit a7d494
				      "Matrix",
Packit a7d494
				      "",
Packit a7d494
				      G_VARIANT_TYPE ("au"),
Packit a7d494
				      get_default_matrix (),
Packit a7d494
				      G_PARAM_READWRITE |
Packit a7d494
				      G_PARAM_CONSTRUCT |
Packit a7d494
				      G_PARAM_STATIC_STRINGS);
Packit a7d494
Packit a7d494
	g_object_class_install_properties (object_class, N_PROPERTIES, properties);
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
gtk_source_space_drawer_init (GtkSourceSpaceDrawer *drawer)
Packit a7d494
{
Packit a7d494
	drawer->priv = gtk_source_space_drawer_get_instance_private (drawer);
Packit a7d494
Packit a7d494
	drawer->priv->matrix = g_new0 (GtkSourceSpaceTypeFlags, get_number_of_locations ());
Packit a7d494
}
Packit a7d494
Packit a7d494
/**
Packit a7d494
 * gtk_source_space_drawer_new:
Packit a7d494
 *
Packit a7d494
 * Creates a new #GtkSourceSpaceDrawer object. Useful for storing space drawing
Packit a7d494
 * settings independently of a #GtkSourceView.
Packit a7d494
 *
Packit a7d494
 * Returns: a new #GtkSourceSpaceDrawer.
Packit a7d494
 * Since: 3.24
Packit a7d494
 */
Packit a7d494
GtkSourceSpaceDrawer *
Packit a7d494
gtk_source_space_drawer_new (void)
Packit a7d494
{
Packit a7d494
	return g_object_new (GTK_SOURCE_TYPE_SPACE_DRAWER, NULL);
Packit a7d494
}
Packit a7d494
Packit a7d494
static GtkSourceSpaceLocationFlags
Packit a7d494
get_nonzero_locations_for_draw_spaces_flags (GtkSourceSpaceDrawer *drawer)
Packit a7d494
{
Packit a7d494
	GtkSourceSpaceLocationFlags locations = GTK_SOURCE_SPACE_LOCATION_NONE;
Packit a7d494
	GtkSourceSpaceTypeFlags types;
Packit a7d494
Packit a7d494
	types = gtk_source_space_drawer_get_types_for_locations (drawer, GTK_SOURCE_SPACE_LOCATION_LEADING);
Packit a7d494
	if (types != GTK_SOURCE_SPACE_TYPE_NONE)
Packit a7d494
	{
Packit a7d494
		locations |= GTK_SOURCE_SPACE_LOCATION_LEADING;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	types = gtk_source_space_drawer_get_types_for_locations (drawer, GTK_SOURCE_SPACE_LOCATION_INSIDE_TEXT);
Packit a7d494
	if (types != GTK_SOURCE_SPACE_TYPE_NONE)
Packit a7d494
	{
Packit a7d494
		locations |= GTK_SOURCE_SPACE_LOCATION_INSIDE_TEXT;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	types = gtk_source_space_drawer_get_types_for_locations (drawer, GTK_SOURCE_SPACE_LOCATION_TRAILING);
Packit a7d494
	if (types != GTK_SOURCE_SPACE_TYPE_NONE)
Packit a7d494
	{
Packit a7d494
		locations |= GTK_SOURCE_SPACE_LOCATION_TRAILING;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	return locations;
Packit a7d494
}
Packit a7d494
Packit a7d494
GtkSourceDrawSpacesFlags
Packit a7d494
_gtk_source_space_drawer_get_flags (GtkSourceSpaceDrawer *drawer)
Packit a7d494
{
Packit a7d494
	GtkSourceSpaceLocationFlags locations;
Packit a7d494
	GtkSourceSpaceTypeFlags common_types;
Packit a7d494
	GtkSourceDrawSpacesFlags flags = 0;
Packit a7d494
Packit a7d494
	g_return_val_if_fail (GTK_SOURCE_IS_SPACE_DRAWER (drawer), 0);
Packit a7d494
Packit a7d494
	if (!drawer->priv->enable_matrix)
Packit a7d494
	{
Packit a7d494
		return 0;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	locations = get_nonzero_locations_for_draw_spaces_flags (drawer);
Packit a7d494
	common_types = gtk_source_space_drawer_get_types_for_locations (drawer, locations);
Packit a7d494
Packit a7d494
	if (locations & GTK_SOURCE_SPACE_LOCATION_LEADING)
Packit a7d494
	{
Packit a7d494
		flags |= GTK_SOURCE_DRAW_SPACES_LEADING;
Packit a7d494
	}
Packit a7d494
	if (locations & GTK_SOURCE_SPACE_LOCATION_INSIDE_TEXT)
Packit a7d494
	{
Packit a7d494
		flags |= GTK_SOURCE_DRAW_SPACES_TEXT;
Packit a7d494
	}
Packit a7d494
	if (locations & GTK_SOURCE_SPACE_LOCATION_TRAILING)
Packit a7d494
	{
Packit a7d494
		flags |= GTK_SOURCE_DRAW_SPACES_TRAILING;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	if (common_types & GTK_SOURCE_SPACE_TYPE_SPACE)
Packit a7d494
	{
Packit a7d494
		flags |= GTK_SOURCE_DRAW_SPACES_SPACE;
Packit a7d494
	}
Packit a7d494
	if (common_types & GTK_SOURCE_SPACE_TYPE_TAB)
Packit a7d494
	{
Packit a7d494
		flags |= GTK_SOURCE_DRAW_SPACES_TAB;
Packit a7d494
	}
Packit a7d494
	if (common_types & GTK_SOURCE_SPACE_TYPE_NEWLINE)
Packit a7d494
	{
Packit a7d494
		flags |= GTK_SOURCE_DRAW_SPACES_NEWLINE;
Packit a7d494
	}
Packit a7d494
	if (common_types & GTK_SOURCE_SPACE_TYPE_NBSP)
Packit a7d494
	{
Packit a7d494
		flags |= GTK_SOURCE_DRAW_SPACES_NBSP;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	return flags;
Packit a7d494
}
Packit a7d494
Packit a7d494
static GtkSourceSpaceLocationFlags
Packit a7d494
get_locations_from_draw_spaces_flags (GtkSourceDrawSpacesFlags flags)
Packit a7d494
{
Packit a7d494
	GtkSourceSpaceLocationFlags locations = GTK_SOURCE_SPACE_LOCATION_NONE;
Packit a7d494
Packit a7d494
	if (flags & GTK_SOURCE_DRAW_SPACES_LEADING)
Packit a7d494
	{
Packit a7d494
		locations |= GTK_SOURCE_SPACE_LOCATION_LEADING;
Packit a7d494
	}
Packit a7d494
	if (flags & GTK_SOURCE_DRAW_SPACES_TEXT)
Packit a7d494
	{
Packit a7d494
		locations |= GTK_SOURCE_SPACE_LOCATION_INSIDE_TEXT;
Packit a7d494
	}
Packit a7d494
	if (flags & GTK_SOURCE_DRAW_SPACES_TRAILING)
Packit a7d494
	{
Packit a7d494
		locations |= GTK_SOURCE_SPACE_LOCATION_TRAILING;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	if (locations == GTK_SOURCE_SPACE_LOCATION_NONE)
Packit a7d494
	{
Packit a7d494
		locations = (GTK_SOURCE_SPACE_LOCATION_LEADING |
Packit a7d494
			     GTK_SOURCE_SPACE_LOCATION_INSIDE_TEXT |
Packit a7d494
			     GTK_SOURCE_SPACE_LOCATION_TRAILING);
Packit a7d494
	}
Packit a7d494
Packit a7d494
	return locations;
Packit a7d494
}
Packit a7d494
Packit a7d494
static GtkSourceSpaceTypeFlags
Packit a7d494
get_space_types_from_draw_spaces_flags (GtkSourceDrawSpacesFlags flags)
Packit a7d494
{
Packit a7d494
	GtkSourceSpaceTypeFlags types = GTK_SOURCE_SPACE_TYPE_NONE;
Packit a7d494
Packit a7d494
	if (flags & GTK_SOURCE_DRAW_SPACES_SPACE)
Packit a7d494
	{
Packit a7d494
		types |= GTK_SOURCE_SPACE_TYPE_SPACE;
Packit a7d494
	}
Packit a7d494
	if (flags & GTK_SOURCE_DRAW_SPACES_TAB)
Packit a7d494
	{
Packit a7d494
		types |= GTK_SOURCE_SPACE_TYPE_TAB;
Packit a7d494
	}
Packit a7d494
	if (flags & GTK_SOURCE_DRAW_SPACES_NEWLINE)
Packit a7d494
	{
Packit a7d494
		types |= GTK_SOURCE_SPACE_TYPE_NEWLINE;
Packit a7d494
	}
Packit a7d494
	if (flags & GTK_SOURCE_DRAW_SPACES_NBSP)
Packit a7d494
	{
Packit a7d494
		types |= GTK_SOURCE_SPACE_TYPE_NBSP;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	return types;
Packit a7d494
}
Packit a7d494
Packit a7d494
void
Packit a7d494
_gtk_source_space_drawer_set_flags (GtkSourceSpaceDrawer     *drawer,
Packit a7d494
				    GtkSourceDrawSpacesFlags  flags)
Packit a7d494
{
Packit a7d494
	GtkSourceSpaceLocationFlags locations;
Packit a7d494
	GtkSourceSpaceTypeFlags types;
Packit a7d494
Packit a7d494
	g_return_if_fail (GTK_SOURCE_IS_SPACE_DRAWER (drawer));
Packit a7d494
Packit a7d494
	gtk_source_space_drawer_set_types_for_locations (drawer,
Packit a7d494
							 GTK_SOURCE_SPACE_LOCATION_ALL,
Packit a7d494
							 GTK_SOURCE_SPACE_TYPE_NONE);
Packit a7d494
Packit a7d494
	locations = get_locations_from_draw_spaces_flags (flags);
Packit a7d494
	types = get_space_types_from_draw_spaces_flags (flags);
Packit a7d494
	gtk_source_space_drawer_set_types_for_locations (drawer, locations, types);
Packit a7d494
Packit a7d494
	gtk_source_space_drawer_set_enable_matrix (drawer, TRUE);
Packit a7d494
}
Packit a7d494
Packit a7d494
/**
Packit a7d494
 * gtk_source_space_drawer_get_types_for_locations:
Packit a7d494
 * @drawer: a #GtkSourceSpaceDrawer.
Packit a7d494
 * @locations: one or several #GtkSourceSpaceLocationFlags.
Packit a7d494
 *
Packit a7d494
 * If only one location is specified, this function returns what kind of
Packit a7d494
 * white spaces are drawn at that location. The value is retrieved from the
Packit a7d494
 * #GtkSourceSpaceDrawer:matrix property.
Packit a7d494
 *
Packit a7d494
 * If several locations are specified, this function returns the logical AND for
Packit a7d494
 * those locations. Which means that if a certain kind of white space is present
Packit a7d494
 * in the return value, then that kind of white space is drawn at all the
Packit a7d494
 * specified @locations.
Packit a7d494
 *
Packit a7d494
 * Returns: a combination of #GtkSourceSpaceTypeFlags.
Packit a7d494
 * Since: 3.24
Packit a7d494
 */
Packit a7d494
GtkSourceSpaceTypeFlags
Packit a7d494
gtk_source_space_drawer_get_types_for_locations (GtkSourceSpaceDrawer        *drawer,
Packit a7d494
						 GtkSourceSpaceLocationFlags  locations)
Packit a7d494
{
Packit a7d494
	g_return_val_if_fail (GTK_SOURCE_IS_SPACE_DRAWER (drawer), GTK_SOURCE_SPACE_TYPE_NONE);
Packit a7d494
Packit a7d494
	return get_types_at_all_locations (drawer, locations);
Packit a7d494
}
Packit a7d494
Packit a7d494
/**
Packit a7d494
 * gtk_source_space_drawer_set_types_for_locations:
Packit a7d494
 * @drawer: a #GtkSourceSpaceDrawer.
Packit a7d494
 * @locations: one or several #GtkSourceSpaceLocationFlags.
Packit a7d494
 * @types: a combination of #GtkSourceSpaceTypeFlags.
Packit a7d494
 *
Packit a7d494
 * Modifies the #GtkSourceSpaceDrawer:matrix property at the specified
Packit a7d494
 * @locations.
Packit a7d494
 *
Packit a7d494
 * Since: 3.24
Packit a7d494
 */
Packit a7d494
void
Packit a7d494
gtk_source_space_drawer_set_types_for_locations (GtkSourceSpaceDrawer        *drawer,
Packit a7d494
						 GtkSourceSpaceLocationFlags  locations,
Packit a7d494
						 GtkSourceSpaceTypeFlags      types)
Packit a7d494
{
Packit a7d494
	gint index;
Packit a7d494
	gint num_locations;
Packit a7d494
	gboolean changed = FALSE;
Packit a7d494
Packit a7d494
	g_return_if_fail (GTK_SOURCE_IS_SPACE_DRAWER (drawer));
Packit a7d494
Packit a7d494
	index = 0;
Packit a7d494
	num_locations = get_number_of_locations ();
Packit a7d494
Packit a7d494
	while (locations != 0 && index < num_locations)
Packit a7d494
	{
Packit a7d494
		if ((locations & 1) == 1 &&
Packit a7d494
		    drawer->priv->matrix[index] != types)
Packit a7d494
		{
Packit a7d494
			drawer->priv->matrix[index] = types;
Packit a7d494
			changed = TRUE;
Packit a7d494
		}
Packit a7d494
Packit a7d494
		locations >>= 1;
Packit a7d494
		index++;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	if (changed)
Packit a7d494
	{
Packit a7d494
		g_object_notify_by_pspec (G_OBJECT (drawer), properties[PROP_MATRIX]);
Packit a7d494
	}
Packit a7d494
}
Packit a7d494
Packit a7d494
/**
Packit a7d494
 * gtk_source_space_drawer_get_matrix:
Packit a7d494
 * @drawer: a #GtkSourceSpaceDrawer.
Packit a7d494
 *
Packit a7d494
 * Gets the value of the #GtkSourceSpaceDrawer:matrix property, as a #GVariant.
Packit a7d494
 * An empty array can be returned in case the matrix is a zero matrix.
Packit a7d494
 *
Packit a7d494
 * The gtk_source_space_drawer_get_types_for_locations() function may be more
Packit a7d494
 * convenient to use.
Packit a7d494
 *
Packit a7d494
 * Returns: the #GtkSourceSpaceDrawer:matrix value as a new floating #GVariant
Packit a7d494
 *   instance.
Packit a7d494
 * Since: 3.24
Packit a7d494
 */
Packit a7d494
GVariant *
Packit a7d494
gtk_source_space_drawer_get_matrix (GtkSourceSpaceDrawer *drawer)
Packit a7d494
{
Packit a7d494
	GVariantBuilder builder;
Packit a7d494
	gint num_locations;
Packit a7d494
	gint i;
Packit a7d494
Packit a7d494
	g_return_val_if_fail (GTK_SOURCE_IS_SPACE_DRAWER (drawer), NULL);
Packit a7d494
Packit a7d494
	if (is_zero_matrix (drawer))
Packit a7d494
	{
Packit a7d494
		return g_variant_new ("au", NULL);
Packit a7d494
	}
Packit a7d494
Packit a7d494
	g_variant_builder_init (&builder, G_VARIANT_TYPE ("au"));
Packit a7d494
Packit a7d494
	num_locations = get_number_of_locations ();
Packit a7d494
Packit a7d494
	for (i = 0; i < num_locations; i++)
Packit a7d494
	{
Packit a7d494
		GVariant *space_types;
Packit a7d494
Packit a7d494
		space_types = g_variant_new_uint32 (drawer->priv->matrix[i]);
Packit a7d494
Packit a7d494
		g_variant_builder_add_value (&builder, space_types);
Packit a7d494
	}
Packit a7d494
Packit a7d494
	return g_variant_builder_end (&builder);
Packit a7d494
}
Packit a7d494
Packit a7d494
/**
Packit a7d494
 * gtk_source_space_drawer_set_matrix:
Packit a7d494
 * @drawer: a #GtkSourceSpaceDrawer.
Packit a7d494
 * @matrix: (transfer floating) (nullable): the new matrix value, or %NULL.
Packit a7d494
 *
Packit a7d494
 * Sets a new value to the #GtkSourceSpaceDrawer:matrix property, as a
Packit a7d494
 * #GVariant. If @matrix is %NULL, then an empty array is set.
Packit a7d494
 *
Packit a7d494
 * If @matrix is floating, it is consumed.
Packit a7d494
 *
Packit a7d494
 * The gtk_source_space_drawer_set_types_for_locations() function may be more
Packit a7d494
 * convenient to use.
Packit a7d494
 *
Packit a7d494
 * Since: 3.24
Packit a7d494
 */
Packit a7d494
void
Packit a7d494
gtk_source_space_drawer_set_matrix (GtkSourceSpaceDrawer *drawer,
Packit a7d494
				    GVariant             *matrix)
Packit a7d494
{
Packit a7d494
	gint num_locations;
Packit a7d494
	gint index;
Packit a7d494
	GVariantIter iter;
Packit a7d494
	gboolean changed = FALSE;
Packit a7d494
Packit a7d494
	g_return_if_fail (GTK_SOURCE_IS_SPACE_DRAWER (drawer));
Packit a7d494
Packit a7d494
	if (matrix == NULL)
Packit a7d494
	{
Packit a7d494
		set_zero_matrix (drawer);
Packit a7d494
		return;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	g_return_if_fail (g_variant_is_of_type (matrix, G_VARIANT_TYPE ("au")));
Packit a7d494
Packit a7d494
	g_variant_iter_init (&iter, matrix);
Packit a7d494
Packit a7d494
	num_locations = get_number_of_locations ();
Packit a7d494
	index = 0;
Packit a7d494
	while (index < num_locations)
Packit a7d494
	{
Packit a7d494
		GVariant *child;
Packit a7d494
		guint32 space_types;
Packit a7d494
Packit a7d494
		child = g_variant_iter_next_value (&iter);
Packit a7d494
		if (child == NULL)
Packit a7d494
		{
Packit a7d494
			break;
Packit a7d494
		}
Packit a7d494
Packit a7d494
		space_types = g_variant_get_uint32 (child);
Packit a7d494
Packit a7d494
		if (drawer->priv->matrix[index] != space_types)
Packit a7d494
		{
Packit a7d494
			drawer->priv->matrix[index] = space_types;
Packit a7d494
			changed = TRUE;
Packit a7d494
		}
Packit a7d494
Packit a7d494
		g_variant_unref (child);
Packit a7d494
		index++;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	while (index < num_locations)
Packit a7d494
	{
Packit a7d494
		if (drawer->priv->matrix[index] != 0)
Packit a7d494
		{
Packit a7d494
			drawer->priv->matrix[index] = 0;
Packit a7d494
			changed = TRUE;
Packit a7d494
		}
Packit a7d494
Packit a7d494
		index++;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	if (changed)
Packit a7d494
	{
Packit a7d494
		g_object_notify_by_pspec (G_OBJECT (drawer), properties[PROP_MATRIX]);
Packit a7d494
	}
Packit a7d494
Packit a7d494
	if (g_variant_is_floating (matrix))
Packit a7d494
	{
Packit a7d494
		g_variant_ref_sink (matrix);
Packit a7d494
		g_variant_unref (matrix);
Packit a7d494
	}
Packit a7d494
}
Packit a7d494
Packit a7d494
/**
Packit a7d494
 * gtk_source_space_drawer_get_enable_matrix:
Packit a7d494
 * @drawer: a #GtkSourceSpaceDrawer.
Packit a7d494
 *
Packit a7d494
 * Returns: whether the #GtkSourceSpaceDrawer:matrix property is enabled.
Packit a7d494
 * Since: 3.24
Packit a7d494
 */
Packit a7d494
gboolean
Packit a7d494
gtk_source_space_drawer_get_enable_matrix (GtkSourceSpaceDrawer *drawer)
Packit a7d494
{
Packit a7d494
	g_return_val_if_fail (GTK_SOURCE_IS_SPACE_DRAWER (drawer), FALSE);
Packit a7d494
Packit a7d494
	return drawer->priv->enable_matrix;
Packit a7d494
}
Packit a7d494
Packit a7d494
/**
Packit a7d494
 * gtk_source_space_drawer_set_enable_matrix:
Packit a7d494
 * @drawer: a #GtkSourceSpaceDrawer.
Packit a7d494
 * @enable_matrix: the new value.
Packit a7d494
 *
Packit a7d494
 * Sets whether the #GtkSourceSpaceDrawer:matrix property is enabled.
Packit a7d494
 *
Packit a7d494
 * Since: 3.24
Packit a7d494
 */
Packit a7d494
void
Packit a7d494
gtk_source_space_drawer_set_enable_matrix (GtkSourceSpaceDrawer *drawer,
Packit a7d494
					   gboolean              enable_matrix)
Packit a7d494
{
Packit a7d494
	g_return_if_fail (GTK_SOURCE_IS_SPACE_DRAWER (drawer));
Packit a7d494
Packit a7d494
	enable_matrix = enable_matrix != FALSE;
Packit a7d494
Packit a7d494
	if (drawer->priv->enable_matrix != enable_matrix)
Packit a7d494
	{
Packit a7d494
		drawer->priv->enable_matrix = enable_matrix;
Packit a7d494
		g_object_notify_by_pspec (G_OBJECT (drawer), properties[PROP_ENABLE_MATRIX]);
Packit a7d494
	}
Packit a7d494
}
Packit a7d494
Packit a7d494
static gboolean
Packit a7d494
matrix_get_mapping (GValue   *value,
Packit a7d494
		    GVariant *variant,
Packit a7d494
		    gpointer  user_data)
Packit a7d494
{
Packit a7d494
	g_value_set_variant (value, variant);
Packit a7d494
	return TRUE;
Packit a7d494
}
Packit a7d494
Packit a7d494
static GVariant *
Packit a7d494
matrix_set_mapping (const GValue       *value,
Packit a7d494
		    const GVariantType *expected_type,
Packit a7d494
		    gpointer            user_data)
Packit a7d494
{
Packit a7d494
	return g_value_dup_variant (value);
Packit a7d494
}
Packit a7d494
Packit a7d494
/**
Packit a7d494
 * gtk_source_space_drawer_bind_matrix_setting:
Packit a7d494
 * @drawer: a #GtkSourceSpaceDrawer object.
Packit a7d494
 * @settings: a #GSettings object.
Packit a7d494
 * @key: the @settings key to bind.
Packit a7d494
 * @flags: flags for the binding.
Packit a7d494
 *
Packit a7d494
 * Binds the #GtkSourceSpaceDrawer:matrix property to a #GSettings key.
Packit a7d494
 *
Packit a7d494
 * The #GSettings key must be of the same type as the
Packit a7d494
 * #GtkSourceSpaceDrawer:matrix property, that is, `"au"`.
Packit a7d494
 *
Packit a7d494
 * The g_settings_bind() function cannot be used, because the default GIO
Packit a7d494
 * mapping functions don't support #GVariant properties (maybe it will be
Packit a7d494
 * supported by a future GIO version, in which case this function can be
Packit a7d494
 * deprecated).
Packit a7d494
 *
Packit a7d494
 * Since: 3.24
Packit a7d494
 */
Packit a7d494
void
Packit a7d494
gtk_source_space_drawer_bind_matrix_setting (GtkSourceSpaceDrawer *drawer,
Packit a7d494
					     GSettings            *settings,
Packit a7d494
					     const gchar          *key,
Packit a7d494
					     GSettingsBindFlags    flags)
Packit a7d494
{
Packit a7d494
	GVariant *value;
Packit a7d494
Packit a7d494
	g_return_if_fail (GTK_SOURCE_IS_SPACE_DRAWER (drawer));
Packit a7d494
	g_return_if_fail (G_IS_SETTINGS (settings));
Packit a7d494
	g_return_if_fail (key != NULL);
Packit a7d494
	g_return_if_fail ((flags & G_SETTINGS_BIND_INVERT_BOOLEAN) == 0);
Packit a7d494
Packit a7d494
	value = g_settings_get_value (settings, key);
Packit a7d494
	if (!g_variant_is_of_type (value, G_VARIANT_TYPE ("au")))
Packit a7d494
	{
Packit a7d494
		g_warning ("%s(): the GSettings key must be of type \"au\".", G_STRFUNC);
Packit a7d494
		g_variant_unref (value);
Packit a7d494
		return;
Packit a7d494
	}
Packit a7d494
	g_variant_unref (value);
Packit a7d494
Packit a7d494
	g_settings_bind_with_mapping (settings, key,
Packit a7d494
				      drawer, "matrix",
Packit a7d494
				      flags,
Packit a7d494
				      matrix_get_mapping,
Packit a7d494
				      matrix_set_mapping,
Packit a7d494
				      NULL, NULL);
Packit a7d494
}
Packit a7d494
Packit a7d494
void
Packit a7d494
_gtk_source_space_drawer_update_color (GtkSourceSpaceDrawer *drawer,
Packit a7d494
				       GtkSourceView        *view)
Packit a7d494
{
Packit a7d494
	GtkSourceBuffer *buffer;
Packit a7d494
	GtkSourceStyleScheme *style_scheme;
Packit a7d494
Packit a7d494
	g_return_if_fail (GTK_SOURCE_IS_SPACE_DRAWER (drawer));
Packit a7d494
	g_return_if_fail (GTK_SOURCE_IS_VIEW (view));
Packit a7d494
Packit a7d494
	if (drawer->priv->color != NULL)
Packit a7d494
	{
Packit a7d494
		gdk_rgba_free (drawer->priv->color);
Packit a7d494
		drawer->priv->color = NULL;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	buffer = GTK_SOURCE_BUFFER (gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)));
Packit a7d494
	style_scheme = gtk_source_buffer_get_style_scheme (buffer);
Packit a7d494
Packit a7d494
	if (style_scheme != NULL)
Packit a7d494
	{
Packit a7d494
		GtkSourceStyle *style;
Packit a7d494
Packit a7d494
		style = _gtk_source_style_scheme_get_draw_spaces_style (style_scheme);
Packit a7d494
Packit a7d494
		if (style != NULL)
Packit a7d494
		{
Packit a7d494
			gchar *color_str = NULL;
Packit a7d494
			gboolean color_set;
Packit a7d494
			GdkRGBA color;
Packit a7d494
Packit a7d494
			g_object_get (style,
Packit a7d494
				      "foreground", &color_str,
Packit a7d494
				      "foreground-set", &color_set,
Packit a7d494
				      NULL);
Packit a7d494
Packit a7d494
			if (color_set &&
Packit a7d494
			    color_str != NULL &&
Packit a7d494
			    gdk_rgba_parse (&color, color_str))
Packit a7d494
			{
Packit a7d494
				drawer->priv->color = gdk_rgba_copy (&color;;
Packit a7d494
			}
Packit a7d494
Packit a7d494
			g_free (color_str);
Packit a7d494
		}
Packit a7d494
	}
Packit a7d494
Packit a7d494
	if (drawer->priv->color == NULL)
Packit a7d494
	{
Packit a7d494
		GtkStyleContext *context;
Packit a7d494
		GdkRGBA color;
Packit a7d494
Packit a7d494
		context = gtk_widget_get_style_context (GTK_WIDGET (view));
Packit a7d494
		gtk_style_context_save (context);
Packit a7d494
		gtk_style_context_set_state (context, GTK_STATE_FLAG_INSENSITIVE);
Packit a7d494
		gtk_style_context_get_color (context,
Packit a7d494
					     gtk_style_context_get_state (context),
Packit a7d494
					     &color;;
Packit a7d494
		gtk_style_context_restore (context);
Packit a7d494
Packit a7d494
		drawer->priv->color = gdk_rgba_copy (&color;;
Packit a7d494
	}
Packit a7d494
}
Packit a7d494
Packit a7d494
static inline gboolean
Packit a7d494
is_tab (gunichar ch)
Packit a7d494
{
Packit a7d494
	return ch == '\t';
Packit a7d494
}
Packit a7d494
Packit a7d494
static inline gboolean
Packit a7d494
is_nbsp (gunichar ch)
Packit a7d494
{
Packit a7d494
	return g_unichar_break_type (ch) == G_UNICODE_BREAK_NON_BREAKING_GLUE;
Packit a7d494
}
Packit a7d494
Packit a7d494
static inline gboolean
Packit a7d494
is_narrowed_nbsp (gunichar ch)
Packit a7d494
{
Packit a7d494
	return ch == 0x202F;
Packit a7d494
}
Packit a7d494
Packit a7d494
static inline gboolean
Packit a7d494
is_space (gunichar ch)
Packit a7d494
{
Packit a7d494
	return g_unichar_type (ch) == G_UNICODE_SPACE_SEPARATOR;
Packit a7d494
}
Packit a7d494
Packit a7d494
static gboolean
Packit a7d494
is_newline (const GtkTextIter *iter)
Packit a7d494
{
Packit a7d494
	if (gtk_text_iter_is_end (iter))
Packit a7d494
	{
Packit a7d494
		GtkSourceBuffer *buffer;
Packit a7d494
Packit a7d494
		buffer = GTK_SOURCE_BUFFER (gtk_text_iter_get_buffer (iter));
Packit a7d494
Packit a7d494
		return gtk_source_buffer_get_implicit_trailing_newline (buffer);
Packit a7d494
	}
Packit a7d494
Packit a7d494
	return gtk_text_iter_ends_line (iter);
Packit a7d494
}
Packit a7d494
Packit a7d494
static inline gboolean
Packit a7d494
is_whitespace (gunichar ch)
Packit a7d494
{
Packit a7d494
	return (g_unichar_isspace (ch) || is_nbsp (ch) || is_space (ch));
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
draw_space_at_pos (cairo_t      *cr,
Packit a7d494
		   GdkRectangle  rect)
Packit a7d494
{
Packit a7d494
	gint x, y;
Packit a7d494
	gdouble w;
Packit a7d494
Packit a7d494
	x = rect.x;
Packit a7d494
	y = rect.y + rect.height * 2 / 3;
Packit a7d494
Packit a7d494
	w = rect.width;
Packit a7d494
Packit a7d494
	cairo_save (cr);
Packit a7d494
	cairo_move_to (cr, x + w * 0.5, y);
Packit a7d494
	cairo_arc (cr, x + w * 0.5, y, 0.8, 0, 2 * G_PI);
Packit a7d494
	cairo_stroke (cr);
Packit a7d494
	cairo_restore (cr);
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
draw_tab_at_pos (cairo_t      *cr,
Packit a7d494
		 GdkRectangle  rect)
Packit a7d494
{
Packit a7d494
	gint x, y;
Packit a7d494
	gdouble w, h;
Packit a7d494
Packit a7d494
	x = rect.x;
Packit a7d494
	y = rect.y + rect.height * 2 / 3;
Packit a7d494
Packit a7d494
	w = rect.width;
Packit a7d494
	h = rect.height;
Packit a7d494
Packit a7d494
	cairo_save (cr);
Packit a7d494
	cairo_move_to (cr, x + w * 1 / 8, y);
Packit a7d494
	cairo_rel_line_to (cr, w * 6 / 8, 0);
Packit a7d494
	cairo_rel_line_to (cr, -h * 1 / 4, -h * 1 / 4);
Packit a7d494
	cairo_rel_move_to (cr, +h * 1 / 4, +h * 1 / 4);
Packit a7d494
	cairo_rel_line_to (cr, -h * 1 / 4, +h * 1 / 4);
Packit a7d494
	cairo_stroke (cr);
Packit a7d494
	cairo_restore (cr);
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
draw_newline_at_pos (cairo_t      *cr,
Packit a7d494
		     GdkRectangle  rect)
Packit a7d494
{
Packit a7d494
	gint x, y;
Packit a7d494
	gdouble w, h;
Packit a7d494
Packit a7d494
	x = rect.x;
Packit a7d494
	y = rect.y + rect.height / 3;
Packit a7d494
Packit a7d494
	w = 2 * rect.width;
Packit a7d494
	h = rect.height;
Packit a7d494
Packit a7d494
	cairo_save (cr);
Packit a7d494
Packit a7d494
	if (gtk_widget_get_default_direction () == GTK_TEXT_DIR_LTR)
Packit a7d494
	{
Packit a7d494
		cairo_move_to (cr, x + w * 7 / 8, y);
Packit a7d494
		cairo_rel_line_to (cr, 0, h * 1 / 3);
Packit a7d494
		cairo_rel_line_to (cr, -w * 6 / 8, 0);
Packit a7d494
		cairo_rel_line_to (cr, +h * 1 / 4, -h * 1 / 4);
Packit a7d494
		cairo_rel_move_to (cr, -h * 1 / 4, +h * 1 / 4);
Packit a7d494
		cairo_rel_line_to (cr, +h * 1 / 4, +h * 1 / 4);
Packit a7d494
	}
Packit a7d494
	else
Packit a7d494
	{
Packit a7d494
		cairo_move_to (cr, x + w * 1 / 8, y);
Packit a7d494
		cairo_rel_line_to (cr, 0, h * 1 / 3);
Packit a7d494
		cairo_rel_line_to (cr, w * 6 / 8, 0);
Packit a7d494
		cairo_rel_line_to (cr, -h * 1 / 4, -h * 1 / 4);
Packit a7d494
		cairo_rel_move_to (cr, +h * 1 / 4, +h * 1 / 4);
Packit a7d494
		cairo_rel_line_to (cr, -h * 1 / 4, -h * 1 / 4);
Packit a7d494
	}
Packit a7d494
Packit a7d494
	cairo_stroke (cr);
Packit a7d494
	cairo_restore (cr);
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
draw_nbsp_at_pos (cairo_t      *cr,
Packit a7d494
		  GdkRectangle  rect,
Packit a7d494
		  gboolean      narrowed)
Packit a7d494
{
Packit a7d494
	gint x, y;
Packit a7d494
	gdouble w, h;
Packit a7d494
Packit a7d494
	x = rect.x;
Packit a7d494
	y = rect.y + rect.height / 2;
Packit a7d494
Packit a7d494
	w = rect.width;
Packit a7d494
	h = rect.height;
Packit a7d494
Packit a7d494
	cairo_save (cr);
Packit a7d494
	cairo_move_to (cr, x + w * 1 / 6, y);
Packit a7d494
	cairo_rel_line_to (cr, w * 4 / 6, 0);
Packit a7d494
	cairo_rel_line_to (cr, -w * 2 / 6, +h * 1 / 4);
Packit a7d494
	cairo_rel_line_to (cr, -w * 2 / 6, -h * 1 / 4);
Packit a7d494
Packit a7d494
	if (narrowed)
Packit a7d494
	{
Packit a7d494
		cairo_fill (cr);
Packit a7d494
	}
Packit a7d494
	else
Packit a7d494
	{
Packit a7d494
		cairo_stroke (cr);
Packit a7d494
	}
Packit a7d494
Packit a7d494
	cairo_restore (cr);
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
draw_whitespace_at_iter (GtkTextView *text_view,
Packit a7d494
			 GtkTextIter *iter,
Packit a7d494
			 cairo_t     *cr)
Packit a7d494
{
Packit a7d494
	gunichar ch;
Packit a7d494
	GdkRectangle rect;
Packit a7d494
Packit a7d494
	gtk_text_view_get_iter_location (text_view, iter, &rect);
Packit a7d494
Packit a7d494
	/* If the space is at a line-wrap position, or if the character is a
Packit a7d494
	 * newline, we get 0 width so we fallback to the height.
Packit a7d494
	 */
Packit a7d494
	if (rect.width == 0)
Packit a7d494
	{
Packit a7d494
		rect.width = rect.height;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	ch = gtk_text_iter_get_char (iter);
Packit a7d494
Packit a7d494
	if (is_tab (ch))
Packit a7d494
	{
Packit a7d494
		draw_tab_at_pos (cr, rect);
Packit a7d494
	}
Packit a7d494
	else if (is_nbsp (ch))
Packit a7d494
	{
Packit a7d494
		draw_nbsp_at_pos (cr, rect, is_narrowed_nbsp (ch));
Packit a7d494
	}
Packit a7d494
	else if (is_space (ch))
Packit a7d494
	{
Packit a7d494
		draw_space_at_pos (cr, rect);
Packit a7d494
	}
Packit a7d494
	else if (is_newline (iter))
Packit a7d494
	{
Packit a7d494
		draw_newline_at_pos (cr, rect);
Packit a7d494
	}
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
draw_spaces_tag_foreach (GtkTextTag *tag,
Packit a7d494
			 gboolean   *found)
Packit a7d494
{
Packit a7d494
	if (*found)
Packit a7d494
	{
Packit a7d494
		return;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	if (GTK_SOURCE_IS_TAG (tag))
Packit a7d494
	{
Packit a7d494
		gboolean draw_spaces_set;
Packit a7d494
Packit a7d494
		g_object_get (tag,
Packit a7d494
			      "draw-spaces-set", &draw_spaces_set,
Packit a7d494
			      NULL);
Packit a7d494
Packit a7d494
		if (draw_spaces_set)
Packit a7d494
		{
Packit a7d494
			*found = TRUE;
Packit a7d494
		}
Packit a7d494
	}
Packit a7d494
}
Packit a7d494
Packit a7d494
static gboolean
Packit a7d494
buffer_has_draw_spaces_tag (GtkTextBuffer *buffer)
Packit a7d494
{
Packit a7d494
	GtkTextTagTable *table;
Packit a7d494
	gboolean found = FALSE;
Packit a7d494
Packit a7d494
	table = gtk_text_buffer_get_tag_table (buffer);
Packit a7d494
	gtk_text_tag_table_foreach (table,
Packit a7d494
				    (GtkTextTagTableForeach) draw_spaces_tag_foreach,
Packit a7d494
				    &found);
Packit a7d494
Packit a7d494
	return found;
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
space_needs_drawing_according_to_tag (const GtkTextIter *iter,
Packit a7d494
				      gboolean          *has_tag,
Packit a7d494
				      gboolean          *needs_drawing)
Packit a7d494
{
Packit a7d494
	GSList *tags;
Packit a7d494
	GSList *l;
Packit a7d494
Packit a7d494
	*has_tag = FALSE;
Packit a7d494
	*needs_drawing = FALSE;
Packit a7d494
Packit a7d494
	tags = gtk_text_iter_get_tags (iter);
Packit a7d494
	tags = g_slist_reverse (tags);
Packit a7d494
Packit a7d494
	for (l = tags; l != NULL; l = l->next)
Packit a7d494
	{
Packit a7d494
		GtkTextTag *tag = l->data;
Packit a7d494
Packit a7d494
		if (GTK_SOURCE_IS_TAG (tag))
Packit a7d494
		{
Packit a7d494
			gboolean draw_spaces_set;
Packit a7d494
			gboolean draw_spaces;
Packit a7d494
Packit a7d494
			g_object_get (tag,
Packit a7d494
				      "draw-spaces-set", &draw_spaces_set,
Packit a7d494
				      "draw-spaces", &draw_spaces,
Packit a7d494
				      NULL);
Packit a7d494
Packit a7d494
			if (draw_spaces_set)
Packit a7d494
			{
Packit a7d494
				*has_tag = TRUE;
Packit a7d494
				*needs_drawing = draw_spaces;
Packit a7d494
				break;
Packit a7d494
			}
Packit a7d494
		}
Packit a7d494
	}
Packit a7d494
Packit a7d494
	g_slist_free (tags);
Packit a7d494
}
Packit a7d494
Packit a7d494
static GtkSourceSpaceLocationFlags
Packit a7d494
get_iter_locations (const GtkTextIter *iter,
Packit a7d494
		    const GtkTextIter *leading_end,
Packit a7d494
		    const GtkTextIter *trailing_start)
Packit a7d494
{
Packit a7d494
	GtkSourceSpaceLocationFlags iter_locations = GTK_SOURCE_SPACE_LOCATION_NONE;
Packit a7d494
Packit a7d494
	if (gtk_text_iter_compare (iter, leading_end) < 0)
Packit a7d494
	{
Packit a7d494
		iter_locations |= GTK_SOURCE_SPACE_LOCATION_LEADING;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	if (gtk_text_iter_compare (trailing_start, iter) <= 0)
Packit a7d494
	{
Packit a7d494
		iter_locations |= GTK_SOURCE_SPACE_LOCATION_TRAILING;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	/* Neither leading nor trailing, must be in text. */
Packit a7d494
	if (iter_locations == GTK_SOURCE_SPACE_LOCATION_NONE)
Packit a7d494
	{
Packit a7d494
		iter_locations = GTK_SOURCE_SPACE_LOCATION_INSIDE_TEXT;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	return iter_locations;
Packit a7d494
}
Packit a7d494
Packit a7d494
static GtkSourceSpaceTypeFlags
Packit a7d494
get_iter_space_type (const GtkTextIter *iter)
Packit a7d494
{
Packit a7d494
	gunichar ch;
Packit a7d494
Packit a7d494
	ch = gtk_text_iter_get_char (iter);
Packit a7d494
Packit a7d494
	if (is_tab (ch))
Packit a7d494
	{
Packit a7d494
		return GTK_SOURCE_SPACE_TYPE_TAB;
Packit a7d494
	}
Packit a7d494
	else if (is_nbsp (ch))
Packit a7d494
	{
Packit a7d494
		return GTK_SOURCE_SPACE_TYPE_NBSP;
Packit a7d494
	}
Packit a7d494
	else if (is_space (ch))
Packit a7d494
	{
Packit a7d494
		return GTK_SOURCE_SPACE_TYPE_SPACE;
Packit a7d494
	}
Packit a7d494
	else if (is_newline (iter))
Packit a7d494
	{
Packit a7d494
		return GTK_SOURCE_SPACE_TYPE_NEWLINE;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	return GTK_SOURCE_SPACE_TYPE_NONE;
Packit a7d494
}
Packit a7d494
Packit a7d494
static gboolean
Packit a7d494
space_needs_drawing_according_to_matrix (GtkSourceSpaceDrawer *drawer,
Packit a7d494
					 const GtkTextIter    *iter,
Packit a7d494
					 const GtkTextIter    *leading_end,
Packit a7d494
					 const GtkTextIter    *trailing_start)
Packit a7d494
{
Packit a7d494
	GtkSourceSpaceLocationFlags iter_locations;
Packit a7d494
	GtkSourceSpaceTypeFlags iter_space_type;
Packit a7d494
	GtkSourceSpaceTypeFlags allowed_space_types;
Packit a7d494
Packit a7d494
	iter_locations = get_iter_locations (iter, leading_end, trailing_start);
Packit a7d494
	iter_space_type = get_iter_space_type (iter);
Packit a7d494
	allowed_space_types = get_types_at_any_locations (drawer, iter_locations);
Packit a7d494
Packit a7d494
	return (iter_space_type & allowed_space_types) != 0;
Packit a7d494
}
Packit a7d494
Packit a7d494
static gboolean
Packit a7d494
space_needs_drawing (GtkSourceSpaceDrawer *drawer,
Packit a7d494
		     const GtkTextIter    *iter,
Packit a7d494
		     const GtkTextIter    *leading_end,
Packit a7d494
		     const GtkTextIter    *trailing_start)
Packit a7d494
{
Packit a7d494
	gboolean has_tag;
Packit a7d494
	gboolean needs_drawing;
Packit a7d494
Packit a7d494
	/* Check the GtkSourceTag:draw-spaces property (higher priority) */
Packit a7d494
	space_needs_drawing_according_to_tag (iter, &has_tag, &needs_drawing);
Packit a7d494
	if (has_tag)
Packit a7d494
	{
Packit a7d494
		return needs_drawing;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	/* Check the matrix */
Packit a7d494
	return (drawer->priv->enable_matrix &&
Packit a7d494
		space_needs_drawing_according_to_matrix (drawer, iter, leading_end, trailing_start));
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
get_line_end (GtkTextView       *text_view,
Packit a7d494
	      const GtkTextIter *start_iter,
Packit a7d494
	      GtkTextIter       *line_end,
Packit a7d494
	      gint               max_x,
Packit a7d494
	      gint               max_y,
Packit a7d494
	      gboolean           is_wrapping)
Packit a7d494
{
Packit a7d494
	gint min;
Packit a7d494
	gint max;
Packit a7d494
	GdkRectangle rect;
Packit a7d494
Packit a7d494
	*line_end = *start_iter;
Packit a7d494
	if (!gtk_text_iter_ends_line (line_end))
Packit a7d494
	{
Packit a7d494
		gtk_text_iter_forward_to_line_end (line_end);
Packit a7d494
	}
Packit a7d494
Packit a7d494
	/* Check if line_end is inside the bounding box anyway. */
Packit a7d494
	gtk_text_view_get_iter_location (text_view, line_end, &rect);
Packit a7d494
	if (( is_wrapping && rect.y < max_y) ||
Packit a7d494
	    (!is_wrapping && rect.x < max_x))
Packit a7d494
	{
Packit a7d494
		return;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	min = gtk_text_iter_get_line_offset (start_iter);
Packit a7d494
	max = gtk_text_iter_get_line_offset (line_end);
Packit a7d494
Packit a7d494
	while (max >= min)
Packit a7d494
	{
Packit a7d494
		gint i;
Packit a7d494
Packit a7d494
		i = (min + max) >> 1;
Packit a7d494
		gtk_text_iter_set_line_offset (line_end, i);
Packit a7d494
		gtk_text_view_get_iter_location (text_view, line_end, &rect);
Packit a7d494
Packit a7d494
		if (( is_wrapping && rect.y < max_y) ||
Packit a7d494
		    (!is_wrapping && rect.x < max_x))
Packit a7d494
		{
Packit a7d494
			min = i + 1;
Packit a7d494
		}
Packit a7d494
		else if (( is_wrapping && rect.y > max_y) ||
Packit a7d494
			 (!is_wrapping && rect.x > max_x))
Packit a7d494
		{
Packit a7d494
			max = i - 1;
Packit a7d494
		}
Packit a7d494
		else
Packit a7d494
		{
Packit a7d494
			break;
Packit a7d494
		}
Packit a7d494
	}
Packit a7d494
}
Packit a7d494
Packit a7d494
void
Packit a7d494
_gtk_source_space_drawer_draw (GtkSourceSpaceDrawer *drawer,
Packit a7d494
			       GtkSourceView        *view,
Packit a7d494
			       cairo_t              *cr)
Packit a7d494
{
Packit a7d494
	GtkTextView *text_view;
Packit a7d494
	GtkTextBuffer *buffer;
Packit a7d494
	GdkRectangle clip;
Packit a7d494
	gint min_x;
Packit a7d494
	gint min_y;
Packit a7d494
	gint max_x;
Packit a7d494
	gint max_y;
Packit a7d494
	GtkTextIter start;
Packit a7d494
	GtkTextIter end;
Packit a7d494
	GtkTextIter iter;
Packit a7d494
	GtkTextIter leading_end;
Packit a7d494
	GtkTextIter trailing_start;
Packit a7d494
	GtkTextIter line_end;
Packit a7d494
	gboolean is_wrapping;
Packit a7d494
Packit a7d494
#ifdef ENABLE_PROFILE
Packit a7d494
	static GTimer *timer = NULL;
Packit a7d494
	if (timer == NULL)
Packit a7d494
	{
Packit a7d494
		timer = g_timer_new ();
Packit a7d494
	}
Packit a7d494
Packit a7d494
	g_timer_start (timer);
Packit a7d494
#endif
Packit a7d494
Packit a7d494
	g_return_if_fail (GTK_SOURCE_IS_SPACE_DRAWER (drawer));
Packit a7d494
	g_return_if_fail (GTK_SOURCE_IS_VIEW (view));
Packit a7d494
	g_return_if_fail (cr != NULL);
Packit a7d494
Packit a7d494
	if (drawer->priv->color == NULL)
Packit a7d494
	{
Packit a7d494
		g_warning ("GtkSourceSpaceDrawer: color not set.");
Packit a7d494
		return;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	text_view = GTK_TEXT_VIEW (view);
Packit a7d494
	buffer = gtk_text_view_get_buffer (text_view);
Packit a7d494
Packit a7d494
	if ((!drawer->priv->enable_matrix || is_zero_matrix (drawer)) &&
Packit a7d494
	    !buffer_has_draw_spaces_tag (buffer))
Packit a7d494
	{
Packit a7d494
		return;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	if (!gdk_cairo_get_clip_rectangle (cr, &clip))
Packit a7d494
	{
Packit a7d494
		return;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	is_wrapping = gtk_text_view_get_wrap_mode (text_view) != GTK_WRAP_NONE;
Packit a7d494
Packit a7d494
	min_x = clip.x;
Packit a7d494
	min_y = clip.y;
Packit a7d494
	max_x = min_x + clip.width;
Packit a7d494
	max_y = min_y + clip.height;
Packit a7d494
Packit a7d494
	gtk_text_view_get_iter_at_location (text_view, &start, min_x, min_y);
Packit a7d494
	gtk_text_view_get_iter_at_location (text_view, &end, max_x, max_y);
Packit a7d494
Packit a7d494
	cairo_save (cr);
Packit a7d494
	gdk_cairo_set_source_rgba (cr, drawer->priv->color);
Packit a7d494
	cairo_set_line_width (cr, 0.8);
Packit a7d494
	cairo_translate (cr, -0.5, -0.5);
Packit a7d494
Packit a7d494
	iter = start;
Packit a7d494
	_gtk_source_iter_get_leading_spaces_end_boundary (&iter, &leading_end);
Packit a7d494
	_gtk_source_iter_get_trailing_spaces_start_boundary (&iter, &trailing_start);
Packit a7d494
	get_line_end (text_view, &iter, &line_end, max_x, max_y, is_wrapping);
Packit a7d494
Packit a7d494
	while (TRUE)
Packit a7d494
	{
Packit a7d494
		gunichar ch = gtk_text_iter_get_char (&iter);
Packit a7d494
		gint ly;
Packit a7d494
Packit a7d494
		/* Allow end iter, to draw implicit trailing newline. */
Packit a7d494
		if ((is_whitespace (ch) || gtk_text_iter_is_end (&iter)) &&
Packit a7d494
		    space_needs_drawing (drawer, &iter, &leading_end, &trailing_start))
Packit a7d494
		{
Packit a7d494
			draw_whitespace_at_iter (text_view, &iter, cr);
Packit a7d494
		}
Packit a7d494
Packit a7d494
		if (gtk_text_iter_is_end (&iter) ||
Packit a7d494
		    gtk_text_iter_compare (&iter, &end) >= 0)
Packit a7d494
		{
Packit a7d494
			break;
Packit a7d494
		}
Packit a7d494
Packit a7d494
		gtk_text_iter_forward_char (&iter);
Packit a7d494
Packit a7d494
		if (gtk_text_iter_compare (&iter, &line_end) > 0)
Packit a7d494
		{
Packit a7d494
			GtkTextIter next_iter = iter;
Packit a7d494
Packit a7d494
			/* Move to the first iter in the exposed area of the
Packit a7d494
			 * next line.
Packit a7d494
			 */
Packit a7d494
			if (!gtk_text_iter_starts_line (&next_iter))
Packit a7d494
			{
Packit a7d494
				/* We're trying to move forward on the last
Packit a7d494
				 * line of the buffer, so we can stop now.
Packit a7d494
				 */
Packit a7d494
				if (!gtk_text_iter_forward_line (&next_iter))
Packit a7d494
				{
Packit a7d494
					break;
Packit a7d494
				}
Packit a7d494
			}
Packit a7d494
Packit a7d494
			gtk_text_view_get_line_yrange (text_view, &next_iter, &ly, NULL);
Packit a7d494
			gtk_text_view_get_iter_at_location (text_view, &next_iter, min_x, ly);
Packit a7d494
Packit a7d494
			/* Move back one char otherwise tabs may not be redrawn. */
Packit a7d494
			if (!gtk_text_iter_starts_line (&next_iter))
Packit a7d494
			{
Packit a7d494
				gtk_text_iter_backward_char (&next_iter);
Packit a7d494
			}
Packit a7d494
Packit a7d494
			/* Ensure that we have actually advanced, since the
Packit a7d494
			 * above backward_char() is dangerous and can lead to
Packit a7d494
			 * infinite loops.
Packit a7d494
			 */
Packit a7d494
			if (gtk_text_iter_compare (&next_iter, &iter) > 0)
Packit a7d494
			{
Packit a7d494
				iter = next_iter;
Packit a7d494
			}
Packit a7d494
Packit a7d494
			_gtk_source_iter_get_leading_spaces_end_boundary (&iter, &leading_end);
Packit a7d494
			_gtk_source_iter_get_trailing_spaces_start_boundary (&iter, &trailing_start);
Packit a7d494
			get_line_end (text_view, &iter, &line_end, max_x, max_y, is_wrapping);
Packit a7d494
		}
Packit a7d494
	};
Packit a7d494
Packit a7d494
	cairo_restore (cr);
Packit a7d494
Packit a7d494
#ifdef ENABLE_PROFILE
Packit a7d494
	g_timer_stop (timer);
Packit a7d494
Packit a7d494
	/* Same indentation as similar features in gtksourceview.c. */
Packit a7d494
	g_print ("    %s time: %g (sec * 1000)\n",
Packit a7d494
		 G_STRFUNC,
Packit a7d494
		 g_timer_elapsed (timer, NULL) * 1000);
Packit a7d494
#endif
Packit a7d494
}