Remove duplicate NEWS item
[geany-mirror.git] / src / geanywraplabel.c
blob37e7ffd87413e1ab3247f8074aa45a4528a2282d
1 /*
2 * geanywraplabel.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2009-2011 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2009-2011 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * A GtkLabel subclass that can wrap to any width, unlike GtkLabel which has a fixed wrap point.
24 * (inspired by libview's WrapLabel, http://view.sourceforge.net)
28 #include "geany.h"
29 #include "utils.h"
30 #include "geanywraplabel.h"
34 struct _GeanyWrapLabelClass
36 GtkLabelClass parent_class;
39 typedef struct
41 gint wrap_width;
42 gint wrap_height;
43 } GeanyWrapLabelPrivate;
45 struct _GeanyWrapLabel
47 GtkLabel parent;
48 GeanyWrapLabelPrivate *priv;
52 static void geany_wrap_label_size_request (GtkWidget *widget, GtkRequisition *req);
53 static void geany_wrap_label_size_allocate (GtkWidget *widget, GtkAllocation *alloc);
54 static gboolean geany_wrap_label_expose (GtkWidget *widget, GdkEventExpose *event);
55 static void geany_wrap_label_set_wrap_width (GtkWidget *widget, gint width);
56 static void geany_wrap_label_label_notify (GObject *object, GParamSpec *pspec, gpointer data);
58 G_DEFINE_TYPE(GeanyWrapLabel, geany_wrap_label, GTK_TYPE_LABEL)
61 static void geany_wrap_label_class_init(GeanyWrapLabelClass *klass)
63 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
65 widget_class->size_request = geany_wrap_label_size_request;
66 widget_class->size_allocate = geany_wrap_label_size_allocate;
67 widget_class->expose_event = geany_wrap_label_expose;
69 g_type_class_add_private(klass, sizeof (GeanyWrapLabelPrivate));
73 static void geany_wrap_label_init(GeanyWrapLabel *self)
75 self->priv = G_TYPE_INSTANCE_GET_PRIVATE(self,
76 GEANY_WRAP_LABEL_TYPE, GeanyWrapLabelPrivate);
78 self->priv->wrap_width = 0;
79 self->priv->wrap_height = 0;
81 g_signal_connect(self, "notify::label", G_CALLBACK(geany_wrap_label_label_notify), NULL);
82 gtk_misc_set_alignment(GTK_MISC(self), 0.0, 0.0);
86 /* Sets the point at which the text should wrap. */
87 static void geany_wrap_label_set_wrap_width(GtkWidget *widget, gint width)
89 GeanyWrapLabel *self = GEANY_WRAP_LABEL(widget);
90 PangoLayout *layout;
92 if (width <= 0)
93 return;
95 layout = gtk_label_get_layout(GTK_LABEL(widget));
98 * We may need to reset the wrap width, so do this regardless of whether
99 * or not we've changed the width.
101 pango_layout_set_width(layout, width * PANGO_SCALE);
102 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
103 pango_layout_get_pixel_size(layout, NULL, &self->priv->wrap_height);
105 if (self->priv->wrap_width != width)
107 self->priv->wrap_width = width;
108 gtk_widget_queue_resize(widget);
113 /* updates the wrap width when the label text changes */
114 static void geany_wrap_label_label_notify(GObject *object, GParamSpec *pspec, gpointer data)
116 GeanyWrapLabel *self = GEANY_WRAP_LABEL(object);
118 geany_wrap_label_set_wrap_width(GTK_WIDGET(object), self->priv->wrap_width);
122 /* makes sure the layout is setup for rendering and chains to parent renderer */
123 static gboolean geany_wrap_label_expose(GtkWidget *widget, GdkEventExpose *event)
125 GeanyWrapLabel *self = GEANY_WRAP_LABEL(widget);
126 PangoLayout *layout = gtk_label_get_layout(GTK_LABEL(widget));
128 pango_layout_set_width(layout, self->priv->wrap_width * PANGO_SCALE);
129 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
131 return (* GTK_WIDGET_CLASS(geany_wrap_label_parent_class)->expose_event)(widget, event);
135 /* Forces the height to be the size necessary for the Pango layout, while allowing the
136 * width to be flexible. */
137 static void geany_wrap_label_size_request(GtkWidget *widget, GtkRequisition *req)
139 req->width = 0;
140 req->height = GEANY_WRAP_LABEL(widget)->priv->wrap_height;
144 /* Sets the wrap width to the width allocated to us. */
145 static void geany_wrap_label_size_allocate(GtkWidget *widget, GtkAllocation *alloc)
147 (* GTK_WIDGET_CLASS(geany_wrap_label_parent_class)->size_allocate)(widget, alloc);
149 geany_wrap_label_set_wrap_width(widget, alloc->width);
153 GtkWidget *geany_wrap_label_new(const gchar *text)
155 return g_object_new(GEANY_WRAP_LABEL_TYPE, "label", text, NULL);