Blame gtksourceview/gtksourcecompletionproposal.c

Packit a7d494
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- */
Packit a7d494
/* gtksourcecompletionproposal.c
Packit a7d494
 * This file is part of GtkSourceView
Packit a7d494
 *
Packit a7d494
 * Copyright (C) 2007 - 2009 Jesús Barbero Rodríguez <chuchiperriman@gmail.com>
Packit a7d494
 * Copyright (C) 2009 - Jesse van den Kieboom <jessevdk@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 "gtksourcecompletionproposal.h"
Packit a7d494
Packit a7d494
/**
Packit a7d494
 * SECTION:completionproposal
Packit a7d494
 * @title: GtkSourceCompletionProposal
Packit a7d494
 * @short_description: Completion proposal interface
Packit a7d494
 *
Packit a7d494
 * The proposal interface represents a completion item in the completion window.
Packit a7d494
 * It provides information on how to display the completion item and what action
Packit a7d494
 * should be taken when the completion item is activated.
Packit a7d494
 *
Packit a7d494
 * The proposal is displayed in the completion window with a label and
Packit a7d494
 * optionally an icon.
Packit a7d494
 * The label may be specified using plain text or markup by implementing
Packit a7d494
 * the corresponding get function. Only one of those get functions
Packit a7d494
 * should return a value different from %NULL.
Packit a7d494
 * The icon may be specified as a #GdkPixbuf, as an icon name or as a #GIcon by
Packit a7d494
 * implementing the corresponding get function. At most one of those get functions
Packit a7d494
 * should return a value different from %NULL, if they all return %NULL no icon
Packit a7d494
 * will be used.
Packit a7d494
 */
Packit a7d494
Packit a7d494
enum
Packit a7d494
{
Packit a7d494
	CHANGED,
Packit a7d494
	N_SIGNALS
Packit a7d494
};
Packit a7d494
Packit a7d494
static guint signals[N_SIGNALS];
Packit a7d494
Packit a7d494
typedef GtkSourceCompletionProposalIface GtkSourceCompletionProposalInterface;
Packit a7d494
Packit a7d494
G_DEFINE_INTERFACE (GtkSourceCompletionProposal, gtk_source_completion_proposal, G_TYPE_OBJECT)
Packit a7d494
Packit a7d494
static gchar *
Packit a7d494
gtk_source_completion_proposal_get_label_default (GtkSourceCompletionProposal *proposal)
Packit a7d494
{
Packit a7d494
	return NULL;
Packit a7d494
}
Packit a7d494
Packit a7d494
static gchar *
Packit a7d494
gtk_source_completion_proposal_get_markup_default (GtkSourceCompletionProposal *proposal)
Packit a7d494
{
Packit a7d494
	return NULL;
Packit a7d494
}
Packit a7d494
Packit a7d494
static gchar *
Packit a7d494
gtk_source_completion_proposal_get_text_default (GtkSourceCompletionProposal *proposal)
Packit a7d494
{
Packit a7d494
	return NULL;
Packit a7d494
}
Packit a7d494
Packit a7d494
static GdkPixbuf *
Packit a7d494
gtk_source_completion_proposal_get_icon_default (GtkSourceCompletionProposal *proposal)
Packit a7d494
{
Packit a7d494
	return NULL;
Packit a7d494
}
Packit a7d494
Packit a7d494
static const gchar *
Packit a7d494
gtk_source_completion_proposal_get_icon_name_default (GtkSourceCompletionProposal *proposal)
Packit a7d494
{
Packit a7d494
	return NULL;
Packit a7d494
}
Packit a7d494
Packit a7d494
static GIcon *
Packit a7d494
gtk_source_completion_proposal_get_gicon_default (GtkSourceCompletionProposal *proposal)
Packit a7d494
{
Packit a7d494
	return NULL;
Packit a7d494
}
Packit a7d494
Packit a7d494
static gchar *
Packit a7d494
gtk_source_completion_proposal_get_info_default (GtkSourceCompletionProposal *proposal)
Packit a7d494
{
Packit a7d494
	return NULL;
Packit a7d494
}
Packit a7d494
Packit a7d494
static guint
Packit a7d494
gtk_source_completion_proposal_hash_default (GtkSourceCompletionProposal *proposal)
Packit a7d494
{
Packit a7d494
	return g_direct_hash (proposal);
Packit a7d494
}
Packit a7d494
Packit a7d494
static gboolean
Packit a7d494
gtk_source_completion_proposal_equal_default (GtkSourceCompletionProposal *proposal,
Packit a7d494
                                              GtkSourceCompletionProposal *other)
Packit a7d494
{
Packit a7d494
	return g_direct_equal (proposal, other);
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
gtk_source_completion_proposal_default_init (GtkSourceCompletionProposalIface *iface)
Packit a7d494
{
Packit a7d494
	static gboolean initialized = FALSE;
Packit a7d494
Packit a7d494
	iface->get_label = gtk_source_completion_proposal_get_label_default;
Packit a7d494
	iface->get_markup = gtk_source_completion_proposal_get_markup_default;
Packit a7d494
	iface->get_text = gtk_source_completion_proposal_get_text_default;
Packit a7d494
	iface->get_icon = gtk_source_completion_proposal_get_icon_default;
Packit a7d494
	iface->get_icon_name = gtk_source_completion_proposal_get_icon_name_default;
Packit a7d494
	iface->get_gicon = gtk_source_completion_proposal_get_gicon_default;
Packit a7d494
	iface->get_info = gtk_source_completion_proposal_get_info_default;
Packit a7d494
	iface->hash = gtk_source_completion_proposal_hash_default;
Packit a7d494
	iface->equal = gtk_source_completion_proposal_equal_default;
Packit a7d494
Packit a7d494
	if (!initialized)
Packit a7d494
	{
Packit a7d494
		/**
Packit a7d494
		 * GtkSourceCompletionProposal::changed:
Packit a7d494
		 * @proposal: The #GtkSourceCompletionProposal
Packit a7d494
		 *
Packit a7d494
		 * Emitted when the proposal has changed. The completion popup
Packit a7d494
		 * will react to this by updating the shown information.
Packit a7d494
		 *
Packit a7d494
		 */
Packit a7d494
		signals[CHANGED] =
Packit a7d494
			g_signal_new ("changed",
Packit a7d494
			      G_TYPE_FROM_INTERFACE (iface),
Packit a7d494
			      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
Packit a7d494
			      G_STRUCT_OFFSET (GtkSourceCompletionProposalIface, changed),
Packit a7d494
			      NULL, NULL, NULL,
Packit a7d494
			      G_TYPE_NONE, 0);
Packit a7d494
Packit a7d494
		initialized = TRUE;
Packit a7d494
	}
Packit a7d494
}
Packit a7d494
Packit a7d494
/**
Packit a7d494
 * gtk_source_completion_proposal_get_label:
Packit a7d494
 * @proposal: a #GtkSourceCompletionProposal.
Packit a7d494
 *
Packit a7d494
 * Gets the label of @proposal. The label is shown in the list of proposals as
Packit a7d494
 * plain text. If you need any markup (such as bold or italic text), you have
Packit a7d494
 * to implement gtk_source_completion_proposal_get_markup(). The returned string
Packit a7d494
 * must be freed with g_free().
Packit a7d494
 *
Packit a7d494
 * Returns: a new string containing the label of @proposal.
Packit a7d494
 */
Packit a7d494
gchar *
Packit a7d494
gtk_source_completion_proposal_get_label (GtkSourceCompletionProposal *proposal)
Packit a7d494
{
Packit a7d494
	g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION_PROPOSAL (proposal), NULL);
Packit a7d494
Packit a7d494
	return GTK_SOURCE_COMPLETION_PROPOSAL_GET_INTERFACE (proposal)->get_label (proposal);
Packit a7d494
}
Packit a7d494
Packit a7d494
/**
Packit a7d494
 * gtk_source_completion_proposal_get_markup:
Packit a7d494
 * @proposal: a #GtkSourceCompletionProposal.
Packit a7d494
 *
Packit a7d494
 * Gets the label of @proposal with markup. The label is shown in the list of
Packit a7d494
 * proposals and may contain markup. This will be used instead of
Packit a7d494
 * gtk_source_completion_proposal_get_label() if implemented. The returned string
Packit a7d494
 * must be freed with g_free().
Packit a7d494
 *
Packit a7d494
 * Returns: a new string containing the label of @proposal with markup.
Packit a7d494
 */
Packit a7d494
gchar *
Packit a7d494
gtk_source_completion_proposal_get_markup (GtkSourceCompletionProposal *proposal)
Packit a7d494
{
Packit a7d494
	g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION_PROPOSAL (proposal), NULL);
Packit a7d494
Packit a7d494
	return GTK_SOURCE_COMPLETION_PROPOSAL_GET_INTERFACE (proposal)->get_markup (proposal);
Packit a7d494
}
Packit a7d494
Packit a7d494
/**
Packit a7d494
 * gtk_source_completion_proposal_get_text:
Packit a7d494
 * @proposal: a #GtkSourceCompletionProposal.
Packit a7d494
 *
Packit a7d494
 * Gets the text of @proposal. The text that is inserted into
Packit a7d494
 * the text buffer when the proposal is activated by the default activation.
Packit a7d494
 * You are free to implement a custom activation handler in the provider and
Packit a7d494
 * not implement this function. For more information, see
Packit a7d494
 * gtk_source_completion_provider_activate_proposal(). The returned string must
Packit a7d494
 * be freed with g_free().
Packit a7d494
 *
Packit a7d494
 * Returns: a new string containing the text of @proposal.
Packit a7d494
 */
Packit a7d494
gchar *
Packit a7d494
gtk_source_completion_proposal_get_text (GtkSourceCompletionProposal *proposal)
Packit a7d494
{
Packit a7d494
	g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION_PROPOSAL (proposal), NULL);
Packit a7d494
Packit a7d494
	return GTK_SOURCE_COMPLETION_PROPOSAL_GET_INTERFACE (proposal)->get_text (proposal);
Packit a7d494
}
Packit a7d494
Packit a7d494
/**
Packit a7d494
 * gtk_source_completion_proposal_get_icon:
Packit a7d494
 * @proposal: a #GtkSourceCompletionProposal.
Packit a7d494
 *
Packit a7d494
 * Gets the #GdkPixbuf for the icon of @proposal.
Packit a7d494
 *
Packit a7d494
 * Returns: (nullable) (transfer none): A #GdkPixbuf with the icon of @proposal.
Packit a7d494
 */
Packit a7d494
GdkPixbuf *
Packit a7d494
gtk_source_completion_proposal_get_icon (GtkSourceCompletionProposal *proposal)
Packit a7d494
{
Packit a7d494
	g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION_PROPOSAL (proposal), NULL);
Packit a7d494
Packit a7d494
	return GTK_SOURCE_COMPLETION_PROPOSAL_GET_INTERFACE (proposal)->get_icon (proposal);
Packit a7d494
}
Packit a7d494
Packit a7d494
/**
Packit a7d494
 * gtk_source_completion_proposal_get_icon_name:
Packit a7d494
 * @proposal: a #GtkSourceCompletionProposal.
Packit a7d494
 *
Packit a7d494
 * Gets the icon name of @proposal.
Packit a7d494
 *
Packit a7d494
 * Returns: (nullable) (transfer none): The icon name of @proposal.
Packit a7d494
 *
Packit a7d494
 * Since: 3.18
Packit a7d494
 */
Packit a7d494
const gchar *
Packit a7d494
gtk_source_completion_proposal_get_icon_name (GtkSourceCompletionProposal *proposal)
Packit a7d494
{
Packit a7d494
	g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION_PROPOSAL (proposal), NULL);
Packit a7d494
Packit a7d494
	return GTK_SOURCE_COMPLETION_PROPOSAL_GET_INTERFACE (proposal)->get_icon_name (proposal);
Packit a7d494
}
Packit a7d494
Packit a7d494
/**
Packit a7d494
 * gtk_source_completion_proposal_get_gicon:
Packit a7d494
 * @proposal: a #GtkSourceCompletionProposal.
Packit a7d494
 *
Packit a7d494
 * Gets the #GIcon for the icon of @proposal.
Packit a7d494
 *
Packit a7d494
 * Returns: (nullable) (transfer none): A #GIcon with the icon of @proposal.
Packit a7d494
 *
Packit a7d494
 * Since: 3.18
Packit a7d494
 */
Packit a7d494
GIcon *
Packit a7d494
gtk_source_completion_proposal_get_gicon (GtkSourceCompletionProposal *proposal)
Packit a7d494
{
Packit a7d494
	g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION_PROPOSAL (proposal), NULL);
Packit a7d494
Packit a7d494
	return GTK_SOURCE_COMPLETION_PROPOSAL_GET_INTERFACE (proposal)->get_gicon (proposal);
Packit a7d494
}
Packit a7d494
Packit a7d494
/**
Packit a7d494
 * gtk_source_completion_proposal_get_info:
Packit a7d494
 * @proposal: a #GtkSourceCompletionProposal.
Packit a7d494
 *
Packit a7d494
 * Gets extra information associated to the proposal. This information will be
Packit a7d494
 * used to present the user with extra, detailed information about the
Packit a7d494
 * selected proposal. The returned string must be freed with g_free().
Packit a7d494
 *
Packit a7d494
 * Returns: (nullable) (transfer full): a newly-allocated string containing
Packit a7d494
 * extra information of @proposal or %NULL if no extra information is associated
Packit a7d494
 * to @proposal.
Packit a7d494
 */
Packit a7d494
gchar *
Packit a7d494
gtk_source_completion_proposal_get_info (GtkSourceCompletionProposal *proposal)
Packit a7d494
{
Packit a7d494
	g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION_PROPOSAL (proposal), NULL);
Packit a7d494
Packit a7d494
	return GTK_SOURCE_COMPLETION_PROPOSAL_GET_INTERFACE (proposal)->get_info (proposal);
Packit a7d494
}
Packit a7d494
Packit a7d494
/**
Packit a7d494
 * gtk_source_completion_proposal_hash:
Packit a7d494
 * @proposal: a #GtkSourceCompletionProposal.
Packit a7d494
 *
Packit a7d494
 * Get the hash value of @proposal. This is used to (together with
Packit a7d494
 * gtk_source_completion_proposal_equal()) to match proposals in the completion
Packit a7d494
 * model. By default, it uses a direct hash (g_direct_hash()).
Packit a7d494
 *
Packit a7d494
 * Returns: The hash value of @proposal.
Packit a7d494
 */
Packit a7d494
guint
Packit a7d494
gtk_source_completion_proposal_hash (GtkSourceCompletionProposal *proposal)
Packit a7d494
{
Packit a7d494
	g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION_PROPOSAL (proposal), 0);
Packit a7d494
Packit a7d494
	return GTK_SOURCE_COMPLETION_PROPOSAL_GET_INTERFACE (proposal)->hash (proposal);
Packit a7d494
}
Packit a7d494
Packit a7d494
/**
Packit a7d494
 * gtk_source_completion_proposal_equal:
Packit a7d494
 * @proposal: a #GtkSourceCompletionProposal.
Packit a7d494
 * @other: a #GtkSourceCompletionProposal.
Packit a7d494
 *
Packit a7d494
 * Get whether two proposal objects are the same.  This is used to (together
Packit a7d494
 * with gtk_source_completion_proposal_hash()) to match proposals in the
Packit a7d494
 * completion model. By default, it uses direct equality (g_direct_equal()).
Packit a7d494
 *
Packit a7d494
 * Returns: %TRUE if @proposal and @object are the same proposal
Packit a7d494
 */
Packit a7d494
gboolean
Packit a7d494
gtk_source_completion_proposal_equal (GtkSourceCompletionProposal *proposal,
Packit a7d494
                                      GtkSourceCompletionProposal *other)
Packit a7d494
{
Packit a7d494
	g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION_PROPOSAL (proposal), FALSE);
Packit a7d494
	g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION_PROPOSAL (other), FALSE);
Packit a7d494
Packit a7d494
	return GTK_SOURCE_COMPLETION_PROPOSAL_GET_INTERFACE (proposal)->equal (proposal, other);
Packit a7d494
}
Packit a7d494
Packit a7d494
/**
Packit a7d494
 * gtk_source_completion_proposal_changed:
Packit a7d494
 * @proposal: a #GtkSourceCompletionProposal.
Packit a7d494
 *
Packit a7d494
 * Emits the "changed" signal on @proposal. This should be called by
Packit a7d494
 * implementations whenever the name, icon or info of the proposal has
Packit a7d494
 * changed.
Packit a7d494
 */
Packit a7d494
void
Packit a7d494
gtk_source_completion_proposal_changed (GtkSourceCompletionProposal *proposal)
Packit a7d494
{
Packit a7d494
	g_return_if_fail (GTK_SOURCE_IS_COMPLETION_PROPOSAL (proposal));
Packit a7d494
	g_signal_emit (proposal, signals[CHANGED], 0);
Packit a7d494
}