2008-01-09 13:24:36 +00:00
|
|
|
/*
|
|
|
|
* geanywraplabel.c - this file is part of Geany, a fast and lightweight IDE
|
|
|
|
*
|
2012-06-18 01:13:05 +02:00
|
|
|
* Copyright 2009-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
|
|
|
|
* Copyright 2009-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
|
2008-01-09 13:24:36 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A GtkLabel subclass that can wrap to any width, unlike GtkLabel which has a fixed wrap point.
|
|
|
|
* (inspired by libview's WrapLabel, http://view.sourceforge.net)
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2010-10-06 15:32:40 +00:00
|
|
|
#include "geany.h"
|
2009-04-05 21:07:40 +00:00
|
|
|
#include "utils.h"
|
2008-01-09 13:24:36 +00:00
|
|
|
#include "geanywraplabel.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-01-23 13:35:08 +00:00
|
|
|
struct _GeanyWrapLabelClass
|
|
|
|
{
|
|
|
|
GtkLabelClass parent_class;
|
|
|
|
};
|
|
|
|
|
2008-01-09 13:24:36 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2011-12-17 23:10:03 +01:00
|
|
|
gint wrap_width;
|
2012-05-30 01:23:27 +02:00
|
|
|
gint wrap_height;
|
2008-01-09 13:24:36 +00:00
|
|
|
} GeanyWrapLabelPrivate;
|
|
|
|
|
2010-04-01 15:57:19 +00:00
|
|
|
struct _GeanyWrapLabel
|
|
|
|
{
|
|
|
|
GtkLabel parent;
|
|
|
|
GeanyWrapLabelPrivate *priv;
|
|
|
|
};
|
|
|
|
|
2008-01-09 13:24:36 +00:00
|
|
|
|
2008-01-23 13:35:08 +00:00
|
|
|
static void geany_wrap_label_size_request (GtkWidget *widget, GtkRequisition *req);
|
|
|
|
static void geany_wrap_label_size_allocate (GtkWidget *widget, GtkAllocation *alloc);
|
2012-05-30 01:32:08 +02:00
|
|
|
static gboolean geany_wrap_label_expose (GtkWidget *widget, GdkEventExpose *event);
|
2011-12-17 23:10:03 +01:00
|
|
|
static void geany_wrap_label_set_wrap_width (GtkWidget *widget, gint width);
|
2011-05-10 23:51:01 +00:00
|
|
|
static void geany_wrap_label_label_notify (GObject *object, GParamSpec *pspec, gpointer data);
|
2008-01-09 13:24:36 +00:00
|
|
|
|
2010-04-25 17:43:39 +00:00
|
|
|
G_DEFINE_TYPE(GeanyWrapLabel, geany_wrap_label, GTK_TYPE_LABEL)
|
2008-01-09 13:24:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void geany_wrap_label_class_init(GeanyWrapLabelClass *klass)
|
|
|
|
{
|
2008-01-23 13:35:08 +00:00
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
|
2008-01-09 13:24:36 +00:00
|
|
|
|
2008-01-23 13:35:08 +00:00
|
|
|
widget_class->size_request = geany_wrap_label_size_request;
|
|
|
|
widget_class->size_allocate = geany_wrap_label_size_allocate;
|
2012-05-30 01:32:08 +02:00
|
|
|
widget_class->expose_event = geany_wrap_label_expose;
|
2008-01-23 13:35:08 +00:00
|
|
|
|
2008-01-09 13:24:36 +00:00
|
|
|
g_type_class_add_private(klass, sizeof (GeanyWrapLabelPrivate));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void geany_wrap_label_init(GeanyWrapLabel *self)
|
|
|
|
{
|
2010-04-01 15:57:19 +00:00
|
|
|
self->priv = G_TYPE_INSTANCE_GET_PRIVATE(self,
|
|
|
|
GEANY_WRAP_LABEL_TYPE, GeanyWrapLabelPrivate);
|
2008-01-09 13:24:36 +00:00
|
|
|
|
2012-05-30 01:31:06 +02:00
|
|
|
self->priv->wrap_width = 0;
|
|
|
|
self->priv->wrap_height = 0;
|
2011-05-10 23:51:01 +00:00
|
|
|
|
|
|
|
g_signal_connect(self, "notify::label", G_CALLBACK(geany_wrap_label_label_notify), NULL);
|
|
|
|
gtk_misc_set_alignment(GTK_MISC(self), 0.0, 0.0);
|
2008-01-09 13:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Sets the point at which the text should wrap. */
|
2011-12-17 23:10:03 +01:00
|
|
|
static void geany_wrap_label_set_wrap_width(GtkWidget *widget, gint width)
|
2008-01-09 13:24:36 +00:00
|
|
|
{
|
2012-05-30 01:31:06 +02:00
|
|
|
GeanyWrapLabel *self = GEANY_WRAP_LABEL(widget);
|
2012-05-30 01:23:27 +02:00
|
|
|
PangoLayout *layout;
|
2008-01-23 13:35:08 +00:00
|
|
|
|
2011-12-17 23:10:03 +01:00
|
|
|
if (width <= 0)
|
2008-01-09 13:24:36 +00:00
|
|
|
return;
|
|
|
|
|
2012-05-30 01:23:27 +02:00
|
|
|
layout = gtk_label_get_layout(GTK_LABEL(widget));
|
|
|
|
|
2008-01-09 13:24:36 +00:00
|
|
|
/*
|
|
|
|
* We may need to reset the wrap width, so do this regardless of whether
|
|
|
|
* or not we've changed the width.
|
|
|
|
*/
|
2012-05-30 01:23:27 +02:00
|
|
|
pango_layout_set_width(layout, width * PANGO_SCALE);
|
2012-05-30 01:32:08 +02:00
|
|
|
pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
|
2012-05-30 01:31:06 +02:00
|
|
|
pango_layout_get_pixel_size(layout, NULL, &self->priv->wrap_height);
|
2008-01-09 13:24:36 +00:00
|
|
|
|
2012-05-30 01:31:06 +02:00
|
|
|
if (self->priv->wrap_width != width)
|
2008-01-09 13:24:36 +00:00
|
|
|
{
|
2012-05-30 01:31:06 +02:00
|
|
|
self->priv->wrap_width = width;
|
2008-01-09 13:24:36 +00:00
|
|
|
gtk_widget_queue_resize(widget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-10 23:51:01 +00:00
|
|
|
/* updates the wrap width when the label text changes */
|
|
|
|
static void geany_wrap_label_label_notify(GObject *object, GParamSpec *pspec, gpointer data)
|
|
|
|
{
|
2012-05-30 01:31:06 +02:00
|
|
|
GeanyWrapLabel *self = GEANY_WRAP_LABEL(object);
|
2011-05-10 23:51:01 +00:00
|
|
|
|
2012-05-30 01:31:06 +02:00
|
|
|
geany_wrap_label_set_wrap_width(GTK_WIDGET(object), self->priv->wrap_width);
|
2011-05-10 23:51:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-30 01:32:08 +02:00
|
|
|
/* makes sure the layout is setup for rendering and chains to parent renderer */
|
|
|
|
static gboolean geany_wrap_label_expose(GtkWidget *widget, GdkEventExpose *event)
|
|
|
|
{
|
|
|
|
GeanyWrapLabel *self = GEANY_WRAP_LABEL(widget);
|
|
|
|
PangoLayout *layout = gtk_label_get_layout(GTK_LABEL(widget));
|
|
|
|
|
|
|
|
pango_layout_set_width(layout, self->priv->wrap_width * PANGO_SCALE);
|
|
|
|
pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
|
|
|
|
|
|
|
|
return (* GTK_WIDGET_CLASS(geany_wrap_label_parent_class)->expose_event)(widget, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-09 13:24:36 +00:00
|
|
|
/* Forces the height to be the size necessary for the Pango layout, while allowing the
|
|
|
|
* width to be flexible. */
|
2008-01-23 13:35:08 +00:00
|
|
|
static void geany_wrap_label_size_request(GtkWidget *widget, GtkRequisition *req)
|
2008-01-09 13:24:36 +00:00
|
|
|
{
|
2012-05-30 01:23:27 +02:00
|
|
|
req->width = 0;
|
2012-05-30 01:31:06 +02:00
|
|
|
req->height = GEANY_WRAP_LABEL(widget)->priv->wrap_height;
|
2008-01-09 13:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-23 13:35:08 +00:00
|
|
|
/* Sets the wrap width to the width allocated to us. */
|
|
|
|
static void geany_wrap_label_size_allocate(GtkWidget *widget, GtkAllocation *alloc)
|
2008-01-09 13:24:36 +00:00
|
|
|
{
|
2009-02-05 19:04:54 +00:00
|
|
|
(* GTK_WIDGET_CLASS(geany_wrap_label_parent_class)->size_allocate)(widget, alloc);
|
2008-01-09 13:24:36 +00:00
|
|
|
|
|
|
|
geany_wrap_label_set_wrap_width(widget, alloc->width);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GtkWidget *geany_wrap_label_new(const gchar *text)
|
|
|
|
{
|
2011-05-10 23:51:01 +00:00
|
|
|
return g_object_new(GEANY_WRAP_LABEL_TYPE, "label", text, NULL);
|
2008-01-09 13:24:36 +00:00
|
|
|
}
|