Blame gtksourceview/gtksourcemark.c

Packit a7d494
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- */
Packit a7d494
/* gtksourcemark.c
Packit a7d494
 * This file is part of GtkSourceView
Packit a7d494
 *
Packit a7d494
 * Copyright (C) 2007 - Johannes Schmid <jhs@gnome.org>
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 "gtksourcemark.h"
Packit a7d494
#include "gtksourcebuffer.h"
Packit a7d494
#include "gtksourcebuffer-private.h"
Packit a7d494
#include "gtksourceview-i18n.h"
Packit a7d494
Packit a7d494
/**
Packit a7d494
 * SECTION:mark
Packit a7d494
 * @Short_description: Mark object for GtkSourceBuffer
Packit a7d494
 * @Title: GtkSourceMark
Packit a7d494
 * @See_also: #GtkSourceBuffer
Packit a7d494
 *
Packit a7d494
 * A #GtkSourceMark marks a position in the text where you want to display
Packit a7d494
 * additional info. It is based on #GtkTextMark and thus is still valid after
Packit a7d494
 * the text has changed though its position may change.
Packit a7d494
 *
Packit a7d494
 * #GtkSourceMarks are organised in categories which you have to set
Packit a7d494
 * when you create the mark. Each category can have a priority, a pixbuf and
Packit a7d494
 * other associated attributes. See gtk_source_view_set_mark_attributes().
Packit a7d494
 * The pixbuf will be displayed in the margin at the line where the mark
Packit a7d494
 * residents if the #GtkSourceView:show-line-marks property is set to %TRUE. If
Packit a7d494
 * there are multiple marks in the same line, the pixbufs will be drawn on top
Packit a7d494
 * of each other. The mark with the highest priority will be drawn on top.
Packit a7d494
 */
Packit a7d494
Packit a7d494
enum
Packit a7d494
{
Packit a7d494
	PROP_0,
Packit a7d494
	PROP_CATEGORY
Packit a7d494
};
Packit a7d494
Packit a7d494
struct _GtkSourceMarkPrivate
Packit a7d494
{
Packit a7d494
	gchar *category;
Packit a7d494
};
Packit a7d494
Packit a7d494
G_DEFINE_TYPE_WITH_PRIVATE (GtkSourceMark, gtk_source_mark, GTK_TYPE_TEXT_MARK);
Packit a7d494
Packit a7d494
static void
Packit a7d494
gtk_source_mark_set_property (GObject      *object,
Packit a7d494
			      guint         prop_id,
Packit a7d494
			      const GValue *value,
Packit a7d494
			      GParamSpec   *pspec)
Packit a7d494
{
Packit a7d494
	GtkSourceMarkPrivate *priv;
Packit a7d494
Packit a7d494
	g_return_if_fail (GTK_SOURCE_IS_MARK (object));
Packit a7d494
Packit a7d494
	priv = GTK_SOURCE_MARK (object)->priv;
Packit a7d494
Packit a7d494
	switch (prop_id)
Packit a7d494
	{
Packit a7d494
		case PROP_CATEGORY:
Packit a7d494
			g_return_if_fail (g_value_get_string (value) != NULL);
Packit a7d494
			g_free (priv->category);
Packit a7d494
			priv->category = g_value_dup_string (value);
Packit a7d494
			break;
Packit a7d494
		default:
Packit a7d494
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
Packit a7d494
							   prop_id,
Packit a7d494
							   pspec);
Packit a7d494
	}
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
gtk_source_mark_get_property (GObject    *object,
Packit a7d494
			      guint       prop_id,
Packit a7d494
			      GValue     *value,
Packit a7d494
			      GParamSpec *pspec)
Packit a7d494
{
Packit a7d494
	GtkSourceMark *mark;
Packit a7d494
Packit a7d494
	g_return_if_fail (GTK_SOURCE_IS_MARK (object));
Packit a7d494
Packit a7d494
	mark = GTK_SOURCE_MARK (object);
Packit a7d494
Packit a7d494
	switch (prop_id)
Packit a7d494
	{
Packit a7d494
		case PROP_CATEGORY:
Packit a7d494
			g_value_set_string (value,
Packit a7d494
					    gtk_source_mark_get_category (mark));
Packit a7d494
			break;
Packit a7d494
		default:
Packit a7d494
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
Packit a7d494
							   prop_id,
Packit a7d494
							   pspec);
Packit a7d494
	}
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
gtk_source_mark_finalize (GObject *object)
Packit a7d494
{
Packit a7d494
	GtkSourceMark *mark = GTK_SOURCE_MARK (object);
Packit a7d494
Packit a7d494
	g_free (mark->priv->category);
Packit a7d494
Packit a7d494
	G_OBJECT_CLASS (gtk_source_mark_parent_class)->finalize (object);
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
gtk_source_mark_class_init (GtkSourceMarkClass *klass)
Packit a7d494
{
Packit a7d494
	GObjectClass *object_class;
Packit a7d494
Packit a7d494
	object_class = G_OBJECT_CLASS (klass);
Packit a7d494
Packit a7d494
	object_class->set_property = gtk_source_mark_set_property;
Packit a7d494
	object_class->get_property = gtk_source_mark_get_property;
Packit a7d494
	object_class->finalize = gtk_source_mark_finalize;
Packit a7d494
Packit a7d494
	/**
Packit a7d494
	 * GtkSourceMark:category:
Packit a7d494
	 *
Packit a7d494
	 * The category of the #GtkSourceMark, classifies the mark and controls
Packit a7d494
	 * which pixbuf is used and with which priority it is drawn.
Packit a7d494
	 */
Packit a7d494
	g_object_class_install_property (object_class,
Packit a7d494
					 PROP_CATEGORY,
Packit a7d494
					 g_param_spec_string ("category",
Packit a7d494
							      "Category",
Packit a7d494
							      "The mark category",
Packit a7d494
							      NULL,
Packit a7d494
							      G_PARAM_READWRITE |
Packit a7d494
							      G_PARAM_CONSTRUCT_ONLY |
Packit a7d494
							      G_PARAM_STATIC_STRINGS));
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
gtk_source_mark_init (GtkSourceMark *mark)
Packit a7d494
{
Packit a7d494
	mark->priv = gtk_source_mark_get_instance_private (mark);
Packit a7d494
}
Packit a7d494
Packit a7d494
/**
Packit a7d494
 * gtk_source_mark_new:
Packit a7d494
 * @name: Name of the #GtkSourceMark, can be NULL when not using a name
Packit a7d494
 * @category: is used to classify marks according to common characteristics
Packit a7d494
 * (e.g. all the marks representing a bookmark could belong to the "bookmark"
Packit a7d494
 * category, or all the marks representing a compilation error could belong to
Packit a7d494
 * "error" category).
Packit a7d494
 *
Packit a7d494
 * Creates a text mark. Add it to a buffer using gtk_text_buffer_add_mark().
Packit a7d494
 * If name is NULL, the mark is anonymous; otherwise, the mark can be retrieved
Packit a7d494
 * by name using gtk_text_buffer_get_mark().
Packit a7d494
 * Normally marks are created using the utility function
Packit a7d494
 * gtk_source_buffer_create_source_mark().
Packit a7d494
 *
Packit a7d494
 * Returns: a new #GtkSourceMark that can be added using gtk_text_buffer_add_mark().
Packit a7d494
 *
Packit a7d494
 * Since: 2.2
Packit a7d494
 */
Packit a7d494
GtkSourceMark *
Packit a7d494
gtk_source_mark_new (const gchar *name,
Packit a7d494
		     const gchar *category)
Packit a7d494
{
Packit a7d494
	g_return_val_if_fail (category != NULL, NULL);
Packit a7d494
Packit a7d494
	return GTK_SOURCE_MARK (g_object_new (GTK_SOURCE_TYPE_MARK,
Packit a7d494
					      "category", category,
Packit a7d494
					      "name", name,
Packit a7d494
					      "left-gravity", TRUE,
Packit a7d494
					       NULL));
Packit a7d494
}
Packit a7d494
Packit a7d494
/**
Packit a7d494
 * gtk_source_mark_get_category:
Packit a7d494
 * @mark: a #GtkSourceMark.
Packit a7d494
 *
Packit a7d494
 * Returns the mark category.
Packit a7d494
 *
Packit a7d494
 * Returns: the category of the #GtkSourceMark.
Packit a7d494
 *
Packit a7d494
 * Since: 2.2
Packit a7d494
 */
Packit a7d494
const gchar *
Packit a7d494
gtk_source_mark_get_category (GtkSourceMark *mark)
Packit a7d494
{
Packit a7d494
	g_return_val_if_fail (GTK_SOURCE_IS_MARK (mark), NULL);
Packit a7d494
Packit a7d494
	return mark->priv->category;
Packit a7d494
}
Packit a7d494
Packit a7d494
/**
Packit a7d494
 * gtk_source_mark_next:
Packit a7d494
 * @mark: a #GtkSourceMark.
Packit a7d494
 * @category: (nullable): a string specifying the mark category, or %NULL.
Packit a7d494
 *
Packit a7d494
 * Returns the next #GtkSourceMark in the buffer or %NULL if the mark
Packit a7d494
 * was not added to a buffer. If there is no next mark, %NULL will be returned.
Packit a7d494
 *
Packit a7d494
 * If @category is %NULL, looks for marks of any category.
Packit a7d494
 *
Packit a7d494
 * Returns: (nullable) (transfer none): the next #GtkSourceMark, or %NULL.
Packit a7d494
 *
Packit a7d494
 * Since: 2.2
Packit a7d494
 */
Packit a7d494
GtkSourceMark *
Packit a7d494
gtk_source_mark_next (GtkSourceMark *mark,
Packit a7d494
		      const gchar   *category)
Packit a7d494
{
Packit a7d494
	GtkTextBuffer *buffer;
Packit a7d494
Packit a7d494
	g_return_val_if_fail (GTK_SOURCE_IS_MARK (mark), NULL);
Packit a7d494
Packit a7d494
	buffer = gtk_text_mark_get_buffer (GTK_TEXT_MARK (mark));
Packit a7d494
Packit a7d494
	if (buffer == NULL)
Packit a7d494
	{
Packit a7d494
		return NULL;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	return _gtk_source_buffer_source_mark_next (GTK_SOURCE_BUFFER (buffer),
Packit a7d494
						    mark,
Packit a7d494
						    category);
Packit a7d494
}
Packit a7d494
Packit a7d494
/**
Packit a7d494
 * gtk_source_mark_prev:
Packit a7d494
 * @mark: a #GtkSourceMark.
Packit a7d494
 * @category: a string specifying the mark category, or %NULL.
Packit a7d494
 *
Packit a7d494
 * Returns the previous #GtkSourceMark in the buffer or %NULL if the mark
Packit a7d494
 * was not added to a buffer. If there is no previous mark, %NULL is returned.
Packit a7d494
 *
Packit a7d494
 * If @category is %NULL, looks for marks of any category
Packit a7d494
 *
Packit a7d494
 * Returns: (nullable) (transfer none): the previous #GtkSourceMark, or %NULL.
Packit a7d494
 *
Packit a7d494
 * Since: 2.2
Packit a7d494
 */
Packit a7d494
GtkSourceMark *
Packit a7d494
gtk_source_mark_prev (GtkSourceMark *mark,
Packit a7d494
		      const gchar   *category)
Packit a7d494
{
Packit a7d494
	GtkTextBuffer *buffer;
Packit a7d494
Packit a7d494
	g_return_val_if_fail (GTK_SOURCE_IS_MARK (mark), NULL);
Packit a7d494
Packit a7d494
	buffer = gtk_text_mark_get_buffer (GTK_TEXT_MARK (mark));
Packit a7d494
Packit a7d494
	if (buffer == NULL)
Packit a7d494
	{
Packit a7d494
		return NULL;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	return _gtk_source_buffer_source_mark_prev (GTK_SOURCE_BUFFER (buffer),
Packit a7d494
						    mark,
Packit a7d494
						    category);
Packit a7d494
}
Packit a7d494