Blame src/tepl-info-bar.c

Packit 116408
/* Code copied from Tepl:
Packit 116408
 * https://wiki.gnome.org/Projects/Tepl
Packit 116408
 * Do not modify, modify upstream and then copy it back here.
Packit 116408
 */
Packit 116408
Packit 116408
/*
Packit 116408
 * This file is part of Tepl, a text editor library.
Packit 116408
 *
Packit 116408
 * Copyright 2016, 2017 - Sébastien Wilmet <swilmet@gnome.org>
Packit 116408
 *
Packit 116408
 * Tepl is free software; you can redistribute it and/or modify it under
Packit 116408
 * the terms of the GNU Lesser General Public License as published by the
Packit 116408
 * Free Software Foundation; either version 2.1 of the License, or (at your
Packit 116408
 * option) any later version.
Packit 116408
 *
Packit 116408
 * Tepl is distributed in the hope that it will be useful, but WITHOUT ANY
Packit 116408
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
Packit 116408
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
Packit 116408
 * License for more details.
Packit 116408
 *
Packit 116408
 * You should have received a copy of the GNU Lesser General Public License
Packit 116408
 * along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit 116408
 */
Packit 116408
Packit 116408
#include "tepl-info-bar.h"
Packit 116408
Packit 116408
/*
Packit 116408
 * SECTION:info-bar
Packit 116408
 * @Short_description: Subclass of GtkInfoBar
Packit 116408
 * @Title: TeplInfoBar
Packit 116408
 *
Packit 116408
 * #TeplInfoBar is a subclass of #GtkInfoBar with a vertical action area and
Packit 116408
 * functions to ease the creation of info bars.
Packit 116408
 */
Packit 116408
Packit 116408
typedef struct _TeplInfoBarPrivate TeplInfoBarPrivate;
Packit 116408
Packit 116408
struct _TeplInfoBarPrivate
Packit 116408
{
Packit 116408
	/* Left: icon. Right: content_vgrid. */
Packit 116408
	GtkGrid *content_hgrid;
Packit 116408
Packit 116408
	/* Contains primary/secondary messages. */
Packit 116408
	GtkGrid *content_vgrid;
Packit 116408
Packit 116408
	guint close_button_added : 1;
Packit 116408
};
Packit 116408
Packit 116408
G_DEFINE_TYPE_WITH_PRIVATE (TeplInfoBar, tepl_info_bar, GTK_TYPE_INFO_BAR)
Packit 116408
Packit 116408
static void
Packit 116408
tepl_info_bar_response (GtkInfoBar *gtk_info_bar,
Packit 116408
			gint        response_id)
Packit 116408
{
Packit 116408
	TeplInfoBar *info_bar = TEPL_INFO_BAR (gtk_info_bar);
Packit 116408
	TeplInfoBarPrivate *priv = tepl_info_bar_get_instance_private (info_bar);
Packit 116408
Packit 116408
	if (response_id == GTK_RESPONSE_CLOSE &&
Packit 116408
	    priv->close_button_added)
Packit 116408
	{
Packit 116408
		gtk_widget_destroy (GTK_WIDGET (info_bar));
Packit 116408
Packit 116408
		/* No need to chain up, the widget is destroyed. */
Packit 116408
		return;
Packit 116408
	}
Packit 116408
Packit 116408
	if (GTK_INFO_BAR_CLASS (tepl_info_bar_parent_class)->response != NULL)
Packit 116408
	{
Packit 116408
		GTK_INFO_BAR_CLASS (tepl_info_bar_parent_class)->response (gtk_info_bar,
Packit 116408
									   response_id);
Packit 116408
	}
Packit 116408
}
Packit 116408
Packit 116408
static void
Packit 116408
tepl_info_bar_class_init (TeplInfoBarClass *klass)
Packit 116408
{
Packit 116408
	GtkInfoBarClass *info_bar_class = GTK_INFO_BAR_CLASS (klass);
Packit 116408
Packit 116408
	info_bar_class->response = tepl_info_bar_response;
Packit 116408
}
Packit 116408
Packit 116408
static void
Packit 116408
tepl_info_bar_init (TeplInfoBar *info_bar)
Packit 116408
{
Packit 116408
	TeplInfoBarPrivate *priv;
Packit 116408
	GtkWidget *action_area;
Packit 116408
	GtkWidget *content_area;
Packit 116408
Packit 116408
	priv = tepl_info_bar_get_instance_private (info_bar);
Packit 116408
Packit 116408
	_tepl_info_bar_set_size_request (GTK_INFO_BAR (info_bar));
Packit 116408
Packit 116408
	/* Change the buttons orientation to be vertical.
Packit 116408
	 *
Packit 116408
	 * With a small window, if 3 or more buttons are shown horizontally,
Packit 116408
	 * there is a ridiculous amount of space for the text. And it can get
Packit 116408
	 * worse since the button labels are translatable, in other languages it
Packit 116408
	 * can take even more place. If the buttons are packed vertically, there
Packit 116408
	 * is no problem.
Packit 116408
	 *
Packit 116408
	 * The GtkInfoBar implementation comes originally from gedit, and the
Packit 116408
	 * action area was vertical. Then IIRC a GNOME designer decided that the
Packit 116408
	 * action area must be horizontal, making the gedit info bars look
Packit 116408
	 * weird... So, come back to the original design.
Packit 116408
	 */
Packit 116408
	action_area = gtk_info_bar_get_action_area (GTK_INFO_BAR (info_bar));
Packit 116408
	if (GTK_IS_ORIENTABLE (action_area))
Packit 116408
	{
Packit 116408
		gtk_orientable_set_orientation (GTK_ORIENTABLE (action_area),
Packit 116408
						GTK_ORIENTATION_VERTICAL);
Packit 116408
	}
Packit 116408
	else
Packit 116408
	{
Packit 116408
		g_warning ("Failed to set vertical orientation to the GtkInfoBar action area.");
Packit 116408
	}
Packit 116408
Packit 116408
	/* hgrid */
Packit 116408
	priv->content_hgrid = GTK_GRID (gtk_grid_new ());
Packit 116408
	gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->content_hgrid),
Packit 116408
					GTK_ORIENTATION_HORIZONTAL);
Packit 116408
	gtk_grid_set_column_spacing (priv->content_hgrid, 16);
Packit 116408
	gtk_widget_show (GTK_WIDGET (priv->content_hgrid));
Packit 116408
Packit 116408
	content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
Packit 116408
	gtk_container_add (GTK_CONTAINER (content_area),
Packit 116408
			   GTK_WIDGET (priv->content_hgrid));
Packit 116408
Packit 116408
	/* vgrid */
Packit 116408
	priv->content_vgrid = GTK_GRID (gtk_grid_new ());
Packit 116408
	gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->content_vgrid),
Packit 116408
					GTK_ORIENTATION_VERTICAL);
Packit 116408
	gtk_grid_set_row_spacing (priv->content_vgrid, 6);
Packit 116408
	gtk_widget_show (GTK_WIDGET (priv->content_vgrid));
Packit 116408
Packit 116408
	gtk_container_add (GTK_CONTAINER (priv->content_hgrid),
Packit 116408
			   GTK_WIDGET (priv->content_vgrid));
Packit 116408
}
Packit 116408
Packit 116408
/**
Packit 116408
 * tepl_info_bar_new:
Packit 116408
 *
Packit 116408
 * Returns: a new #TeplInfoBar.
Packit 116408
 * Since: 1.0
Packit 116408
 */
Packit 116408
TeplInfoBar *
Packit 116408
tepl_info_bar_new (void)
Packit 116408
{
Packit 116408
	return g_object_new (TEPL_TYPE_INFO_BAR, NULL);
Packit 116408
}
Packit 116408
Packit 116408
/**
Packit 116408
 * tepl_info_bar_new_simple:
Packit 116408
 * @msg_type: the message type.
Packit 116408
 * @primary_msg: the primary message.
Packit 116408
 * @secondary_msg: (nullable): the secondary message, or %NULL.
Packit 116408
 *
Packit 116408
 * Creates a new #TeplInfoBar with an icon (depending on @msg_type), a primary
Packit 116408
 * message and a secondary message.
Packit 116408
 *
Packit 116408
 * Returns: a new #TeplInfoBar.
Packit 116408
 * Since: 2.0
Packit 116408
 */
Packit 116408
TeplInfoBar *
Packit 116408
tepl_info_bar_new_simple (GtkMessageType  msg_type,
Packit 116408
			  const gchar    *primary_msg,
Packit 116408
			  const gchar    *secondary_msg)
Packit 116408
{
Packit 116408
	TeplInfoBar *info_bar;
Packit 116408
Packit 116408
	g_return_val_if_fail (primary_msg != NULL, NULL);
Packit 116408
Packit 116408
	info_bar = tepl_info_bar_new ();
Packit 116408
Packit 116408
	gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), msg_type);
Packit 116408
	tepl_info_bar_add_icon (info_bar);
Packit 116408
	tepl_info_bar_add_primary_message (info_bar, primary_msg);
Packit 116408
Packit 116408
	if (secondary_msg != NULL)
Packit 116408
	{
Packit 116408
		tepl_info_bar_add_secondary_message (info_bar, secondary_msg);
Packit 116408
	}
Packit 116408
Packit 116408
	return info_bar;
Packit 116408
}
Packit 116408
Packit 116408
static const gchar *
Packit 116408
get_icon_name (TeplInfoBar *info_bar)
Packit 116408
{
Packit 116408
	GtkMessageType msg_type;
Packit 116408
Packit 116408
	msg_type = gtk_info_bar_get_message_type (GTK_INFO_BAR (info_bar));
Packit 116408
Packit 116408
	switch (msg_type)
Packit 116408
	{
Packit 116408
		case GTK_MESSAGE_INFO:
Packit 116408
			return "dialog-information";
Packit 116408
Packit 116408
		case GTK_MESSAGE_WARNING:
Packit 116408
			return "dialog-warning";
Packit 116408
Packit 116408
		case GTK_MESSAGE_QUESTION:
Packit 116408
			return "dialog-question";
Packit 116408
Packit 116408
		case GTK_MESSAGE_ERROR:
Packit 116408
			return "dialog-error";
Packit 116408
Packit 116408
		case GTK_MESSAGE_OTHER:
Packit 116408
		default:
Packit 116408
			/* No icon */
Packit 116408
			break;
Packit 116408
	}
Packit 116408
Packit 116408
	return NULL;
Packit 116408
}
Packit 116408
Packit 116408
/**
Packit 116408
 * tepl_info_bar_add_icon:
Packit 116408
 * @info_bar: a #TeplInfoBar.
Packit 116408
 *
Packit 116408
 * Adds an icon on the left, determined by the message type. So before calling
Packit 116408
 * this function, gtk_info_bar_set_message_type() must have been called.
Packit 116408
 *
Packit 116408
 * The icon is not updated when the message type changes. Another #TeplInfoBar
Packit 116408
 * must be created in that case.
Packit 116408
 *
Packit 116408
 * Since: 2.0
Packit 116408
 */
Packit 116408
void
Packit 116408
tepl_info_bar_add_icon (TeplInfoBar *info_bar)
Packit 116408
{
Packit 116408
	TeplInfoBarPrivate *priv;
Packit 116408
	const gchar *icon_name;
Packit 116408
	GtkWidget *image;
Packit 116408
Packit 116408
	g_return_if_fail (TEPL_IS_INFO_BAR (info_bar));
Packit 116408
Packit 116408
	priv = tepl_info_bar_get_instance_private (info_bar);
Packit 116408
Packit 116408
	icon_name = get_icon_name (info_bar);
Packit 116408
	if (icon_name == NULL)
Packit 116408
	{
Packit 116408
		return;
Packit 116408
	}
Packit 116408
Packit 116408
	image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_DIALOG);
Packit 116408
	gtk_widget_set_valign (image, GTK_ALIGN_START);
Packit 116408
	gtk_widget_show (image);
Packit 116408
Packit 116408
	gtk_grid_attach_next_to (priv->content_hgrid,
Packit 116408
				 image,
Packit 116408
				 GTK_WIDGET (priv->content_vgrid),
Packit 116408
				 GTK_POS_LEFT,
Packit 116408
				 1,
Packit 116408
				 1);
Packit 116408
}
Packit 116408
Packit 116408
/**
Packit 116408
 * tepl_info_bar_add_primary_message:
Packit 116408
 * @info_bar: a #TeplInfoBar.
Packit 116408
 * @primary_msg: a primary message.
Packit 116408
 *
Packit 116408
 * Adds a primary message.
Packit 116408
 * Since: 2.0
Packit 116408
 */
Packit 116408
void
Packit 116408
tepl_info_bar_add_primary_message (TeplInfoBar *info_bar,
Packit 116408
				   const gchar *primary_msg)
Packit 116408
{
Packit 116408
	TeplInfoBarPrivate *priv;
Packit 116408
	gchar *primary_msg_escaped;
Packit 116408
	gchar *primary_markup;
Packit 116408
	GtkLabel *primary_label;
Packit 116408
Packit 116408
	g_return_if_fail (TEPL_IS_INFO_BAR (info_bar));
Packit 116408
	g_return_if_fail (primary_msg != NULL);
Packit 116408
Packit 116408
	priv = tepl_info_bar_get_instance_private (info_bar);
Packit 116408
Packit 116408
	primary_msg_escaped = g_markup_escape_text (primary_msg, -1);
Packit 116408
	primary_markup = g_strdup_printf ("%s", primary_msg_escaped);
Packit 116408
	primary_label = tepl_info_bar_create_label ();
Packit 116408
	gtk_label_set_markup (primary_label, primary_markup);
Packit 116408
	g_free (primary_markup);
Packit 116408
	g_free (primary_msg_escaped);
Packit 116408
Packit 116408
	gtk_widget_show (GTK_WIDGET (primary_label));
Packit 116408
	gtk_container_add (GTK_CONTAINER (priv->content_vgrid),
Packit 116408
			   GTK_WIDGET (primary_label));
Packit 116408
}
Packit 116408
Packit 116408
/**
Packit 116408
 * tepl_info_bar_add_secondary_message:
Packit 116408
 * @info_bar: a #TeplInfoBar.
Packit 116408
 * @secondary_msg: a secondary message.
Packit 116408
 *
Packit 116408
 * Adds a secondary message.
Packit 116408
 * Since: 2.0
Packit 116408
 */
Packit 116408
void
Packit 116408
tepl_info_bar_add_secondary_message (TeplInfoBar *info_bar,
Packit 116408
				     const gchar *secondary_msg)
Packit 116408
{
Packit 116408
	TeplInfoBarPrivate *priv;
Packit 116408
	gchar *secondary_msg_escaped;
Packit 116408
	gchar *secondary_markup;
Packit 116408
	GtkLabel *secondary_label;
Packit 116408
Packit 116408
	g_return_if_fail (TEPL_IS_INFO_BAR (info_bar));
Packit 116408
	g_return_if_fail (secondary_msg != NULL);
Packit 116408
Packit 116408
	priv = tepl_info_bar_get_instance_private (info_bar);
Packit 116408
Packit 116408
	secondary_msg_escaped = g_markup_escape_text (secondary_msg, -1);
Packit 116408
	secondary_markup = g_strdup_printf ("<small>%s</small>", secondary_msg_escaped);
Packit 116408
	secondary_label = tepl_info_bar_create_label ();
Packit 116408
	gtk_label_set_markup (secondary_label, secondary_markup);
Packit 116408
	g_free (secondary_markup);
Packit 116408
	g_free (secondary_msg_escaped);
Packit 116408
Packit 116408
	gtk_widget_show (GTK_WIDGET (secondary_label));
Packit 116408
	gtk_container_add (GTK_CONTAINER (priv->content_vgrid),
Packit 116408
			   GTK_WIDGET (secondary_label));
Packit 116408
}
Packit 116408
Packit 116408
/**
Packit 116408
 * tepl_info_bar_add_content_widget:
Packit 116408
 * @info_bar: a #TeplInfoBar.
Packit 116408
 * @content: a #GtkWidget.
Packit 116408
 *
Packit 116408
 * Adds @content to @info_bar.
Packit 116408
 *
Packit 116408
 * #TeplInfoBar has an internal container, to be able to add the icon and add
Packit 116408
 * primary or secondary messages. The internal container is added to the content
Packit 116408
 * area, as returned by gtk_info_bar_get_content_area(). So if you use a
Packit 116408
 * #TeplInfoBar and you need to add a custom #GtkWidget, it is better to use
Packit 116408
 * this function instead of adding the #GtkWidget directly to the content area.
Packit 116408
 *
Packit 116408
 * Since: 2.0
Packit 116408
 */
Packit 116408
void
Packit 116408
tepl_info_bar_add_content_widget (TeplInfoBar *info_bar,
Packit 116408
				  GtkWidget   *content)
Packit 116408
{
Packit 116408
	TeplInfoBarPrivate *priv;
Packit 116408
Packit 116408
	g_return_if_fail (TEPL_IS_INFO_BAR (info_bar));
Packit 116408
	g_return_if_fail (GTK_IS_WIDGET (content));
Packit 116408
Packit 116408
	priv = tepl_info_bar_get_instance_private (info_bar);
Packit 116408
Packit 116408
	gtk_container_add (GTK_CONTAINER (priv->content_vgrid), content);
Packit 116408
}
Packit 116408
Packit 116408
/**
Packit 116408
 * tepl_info_bar_add_close_button:
Packit 116408
 * @info_bar: a #TeplInfoBar.
Packit 116408
 *
Packit 116408
 * Calls gtk_info_bar_set_show_close_button(), and additionnally closes the
Packit 116408
 * @info_bar when the #GtkInfoBar::response signal is received with the
Packit 116408
 * @response_id %GTK_RESPONSE_CLOSE.
Packit 116408
 *
Packit 116408
 * Since: 2.0
Packit 116408
 */
Packit 116408
void
Packit 116408
tepl_info_bar_add_close_button (TeplInfoBar *info_bar)
Packit 116408
{
Packit 116408
	TeplInfoBarPrivate *priv;
Packit 116408
Packit 116408
	g_return_if_fail (TEPL_IS_INFO_BAR (info_bar));
Packit 116408
Packit 116408
	priv = tepl_info_bar_get_instance_private (info_bar);
Packit 116408
Packit 116408
	gtk_info_bar_set_show_close_button (GTK_INFO_BAR (info_bar), TRUE);
Packit 116408
Packit 116408
	priv->close_button_added = TRUE;
Packit 116408
}
Packit 116408
Packit 116408
/**
Packit 116408
 * tepl_info_bar_create_label:
Packit 116408
 *
Packit 116408
 * Utility function to create a #GtkLabel suitable for a #GtkInfoBar. The
Packit 116408
 * wrapping and alignment is configured. The label is also set as selectable,
Packit 116408
 * for example to copy an error message and search an explanation on the web.
Packit 116408
 *
Packit 116408
 * Returns: (transfer floating): a new #GtkLabel suitable for a #GtkInfoBar.
Packit 116408
 * Since: 1.0
Packit 116408
 */
Packit 116408
GtkLabel *
Packit 116408
tepl_info_bar_create_label (void)
Packit 116408
{
Packit 116408
	GtkLabel *label;
Packit 116408
Packit 116408
	label = GTK_LABEL (gtk_label_new (NULL));
Packit 116408
	gtk_widget_set_halign (GTK_WIDGET (label), GTK_ALIGN_START);
Packit 116408
	gtk_label_set_xalign (label, 0.0);
Packit 116408
	gtk_label_set_line_wrap (label, TRUE);
Packit 116408
	gtk_label_set_line_wrap_mode (label, PANGO_WRAP_WORD_CHAR);
Packit 116408
	gtk_label_set_selectable (label, TRUE);
Packit 116408
Packit 116408
	/* Since the wrapping is enabled, we need to set a minimum width.
Packit 116408
	 *
Packit 116408
	 * If a minimum width is not set, adding an info bar to a container
Packit 116408
	 * (e.g. a TeplTab) can make the GtkWindow height to grow. Because
Packit 116408
	 * without a minimum width (and without ellipsization), when the user
Packit 116408
	 * resizes the window (e.g. reducing the width) the widgets inside the
Packit 116408
	 * window must be able to be drawn. When the info bar must be drawn with
Packit 116408
	 * a width of e.g. 20 pixels, it takes a huge height because of the text
Packit 116408
	 * wrapping. So by setting a minimum width to the label, the maximum
Packit 116408
	 * height that the info bar can take is limited, so in most cases the
Packit 116408
	 * GtkWindow current height is sufficient to draw the info bar with its
Packit 116408
	 * maximum height.
Packit 116408
	 *
Packit 116408
	 * See:
Packit 116408
	 * https://wiki.gnome.org/HowDoI/Labels
Packit 116408
	 *
Packit 116408
	 * There is also a safety net in tepl_tab_add_info_bar() which calls
Packit 116408
	 * gtk_widget_set_size_request() on the GtkInfoBar, to set a minimum
Packit 116408
	 * width.
Packit 116408
	 */
Packit 116408
	gtk_label_set_width_chars (label, 30);
Packit 116408
Packit 116408
	return label;
Packit 116408
}
Packit 116408
Packit 116408
void
Packit 116408
_tepl_info_bar_set_size_request (GtkInfoBar *info_bar)
Packit 116408
{
Packit 116408
	gint min_width;
Packit 116408
	gint min_height;
Packit 116408
Packit 116408
	g_return_if_fail (GTK_IS_INFO_BAR (info_bar));
Packit 116408
Packit 116408
	gtk_widget_get_size_request (GTK_WIDGET (info_bar), &min_width, &min_height);
Packit 116408
Packit 116408
	/* If min_width != -1, gtk_widget_set_size_request() has already been
Packit 116408
	 * called, so don't change the value.
Packit 116408
	 */
Packit 116408
	if (min_width == -1)
Packit 116408
	{
Packit 116408
		/* Safety net to avoid in most cases the GtkWindow height to
Packit 116408
		 * grow.
Packit 116408
		 *
Packit 116408
		 * The gtk_label_set_width_chars() call in
Packit 116408
		 * tepl_info_bar_create_label() fixes the problem at the root,
Packit 116408
		 * but we cannot enforce all GtkLabel of @info_bar to be created
Packit 116408
		 * with tepl_info_bar_create_label(), so a safety net is better.
Packit 116408
		 */
Packit 116408
		gtk_widget_set_size_request (GTK_WIDGET (info_bar), 300, min_height);
Packit 116408
	}
Packit 116408
}