Update French translation
[geany-mirror.git] / src / geanywraplabel.c
blob76e56344e51644c06b1ee222788b92c83f577a02
1 /*
2 * geanywraplabel.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2009 The Geany contributors
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 * A GtkLabel subclass that can wrap to any width, unlike GtkLabel which has a fixed wrap point.
23 * (inspired by libview's WrapLabel, http://view.sourceforge.net)
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include "geanywraplabel.h"
33 struct _GeanyWrapLabelClass
35 GtkLabelClass parent_class;
38 typedef struct
40 gint wrap_width;
41 gint wrap_height;
42 } GeanyWrapLabelPrivate;
44 struct _GeanyWrapLabel
46 GtkLabel parent;
47 GeanyWrapLabelPrivate *priv;
51 static gboolean geany_wrap_label_draw(GtkWidget *widget, cairo_t *cr);
52 static void geany_wrap_label_get_preferred_width (GtkWidget *widget,
53 gint *minimal_width, gint *natural_width);
54 static void geany_wrap_label_get_preferred_height (GtkWidget *widget,
55 gint *minimal_height, gint *natural_height);
56 static void geany_wrap_label_get_preferred_width_for_height (GtkWidget *widget,
57 gint height, gint *minimal_width, gint *natural_width);
58 static void geany_wrap_label_get_preferred_height_for_width (GtkWidget *widget,
59 gint width, gint *minimal_height, gint *natural_height);
60 static GtkSizeRequestMode geany_wrap_label_get_request_mode(GtkWidget *widget);
61 static void geany_wrap_label_size_allocate (GtkWidget *widget, GtkAllocation *alloc);
62 static void geany_wrap_label_set_wrap_width (GtkWidget *widget, gint width);
63 static void geany_wrap_label_label_notify (GObject *object, GParamSpec *pspec, gpointer data);
65 G_DEFINE_TYPE(GeanyWrapLabel, geany_wrap_label, GTK_TYPE_LABEL)
68 static void geany_wrap_label_class_init(GeanyWrapLabelClass *klass)
70 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
72 widget_class->size_allocate = geany_wrap_label_size_allocate;
73 widget_class->draw = geany_wrap_label_draw;
74 widget_class->get_preferred_width = geany_wrap_label_get_preferred_width;
75 widget_class->get_preferred_width_for_height = geany_wrap_label_get_preferred_width_for_height;
76 widget_class->get_preferred_height = geany_wrap_label_get_preferred_height;
77 widget_class->get_preferred_height_for_width = geany_wrap_label_get_preferred_height_for_width;
78 widget_class->get_request_mode = geany_wrap_label_get_request_mode;
80 g_type_class_add_private(klass, sizeof (GeanyWrapLabelPrivate));
84 static void geany_wrap_label_init(GeanyWrapLabel *self)
86 self->priv = G_TYPE_INSTANCE_GET_PRIVATE(self,
87 GEANY_WRAP_LABEL_TYPE, GeanyWrapLabelPrivate);
89 self->priv->wrap_width = 0;
90 self->priv->wrap_height = 0;
92 g_signal_connect(self, "notify::label", G_CALLBACK(geany_wrap_label_label_notify), NULL);
93 gtk_misc_set_alignment(GTK_MISC(self), 0.0, 0.0);
97 /* Sets the point at which the text should wrap. */
98 static void geany_wrap_label_set_wrap_width(GtkWidget *widget, gint width)
100 GeanyWrapLabel *self = GEANY_WRAP_LABEL(widget);
101 PangoLayout *layout;
103 if (width <= 0)
104 return;
106 layout = gtk_label_get_layout(GTK_LABEL(widget));
109 * We may need to reset the wrap width, so do this regardless of whether
110 * or not we've changed the width.
112 pango_layout_set_width(layout, width * PANGO_SCALE);
113 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
114 pango_layout_get_pixel_size(layout, NULL, &self->priv->wrap_height);
116 if (self->priv->wrap_width != width)
118 self->priv->wrap_width = width;
119 gtk_widget_queue_resize(widget);
124 /* updates the wrap width when the label text changes */
125 static void geany_wrap_label_label_notify(GObject *object, GParamSpec *pspec, gpointer data)
127 GeanyWrapLabel *self = GEANY_WRAP_LABEL(object);
129 geany_wrap_label_set_wrap_width(GTK_WIDGET(object), self->priv->wrap_width);
133 /* makes sure the layout is setup for rendering and chains to parent renderer */
134 static gboolean geany_wrap_label_draw(GtkWidget *widget, cairo_t *cr)
136 GeanyWrapLabel *self = GEANY_WRAP_LABEL(widget);
137 PangoLayout *layout = gtk_label_get_layout(GTK_LABEL(widget));
139 pango_layout_set_width(layout, self->priv->wrap_width * PANGO_SCALE);
140 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
142 return (* GTK_WIDGET_CLASS(geany_wrap_label_parent_class)->draw)(widget, cr);
146 static void geany_wrap_label_get_preferred_width (GtkWidget *widget,
147 gint *minimal_width, gint *natural_width)
149 *minimal_width = *natural_width = 0;
153 static void geany_wrap_label_get_preferred_width_for_height (GtkWidget *widget,
154 gint height, gint *minimal_width, gint *natural_width)
156 PangoLayout *layout = gtk_label_get_layout(GTK_LABEL(widget));;
158 pango_layout_set_height(layout, height * PANGO_SCALE);
159 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
160 pango_layout_get_pixel_size(layout, natural_width, NULL);
162 *minimal_width = 0;
166 static void geany_wrap_label_get_preferred_height (GtkWidget *widget,
167 gint *minimal_height, gint *natural_height)
169 *minimal_height = *natural_height = GEANY_WRAP_LABEL(widget)->priv->wrap_height;
173 static void geany_wrap_label_get_preferred_height_for_width (GtkWidget *widget,
174 gint width, gint *minimal_height, gint *natural_height)
176 PangoLayout *layout = gtk_label_get_layout(GTK_LABEL(widget));
178 pango_layout_set_width(layout, width * PANGO_SCALE);
179 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
180 pango_layout_get_pixel_size(layout, NULL, natural_height);
182 *minimal_height = *natural_height;
186 static GtkSizeRequestMode geany_wrap_label_get_request_mode(GtkWidget *widget)
188 return GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
192 /* Sets the wrap width to the width allocated to us. */
193 static void geany_wrap_label_size_allocate(GtkWidget *widget, GtkAllocation *alloc)
195 GtkWidget *parent;
197 (* GTK_WIDGET_CLASS(geany_wrap_label_parent_class)->size_allocate)(widget, alloc);
199 geany_wrap_label_set_wrap_width(widget, alloc->width);
201 /* ask the parent to recompute our size, because it seems GTK size
202 * caching is too aggressive */
203 parent = gtk_widget_get_parent(widget);
204 if (GTK_IS_CONTAINER(parent))
205 gtk_container_check_resize(GTK_CONTAINER(parent));
209 GtkWidget *geany_wrap_label_new(const gchar *text)
211 return g_object_new(GEANY_WRAP_LABEL_TYPE, "label", text, NULL);