Update of Russian translation
[geany-mirror.git] / src / geanywraplabel.c
blobb4380c2fd64f375a6e87322bbd4d369889563d94
1 /*
2 * geanywraplabel.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2009-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2009-2012 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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 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"
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 #if GTK_CHECK_VERSION(3, 0, 0)
52 static gboolean geany_wrap_label_draw(GtkWidget *widget, cairo_t *cr);
53 static void geany_wrap_label_get_preferred_width (GtkWidget *widget,
54 gint *minimal_width, gint *natural_width);
55 static void geany_wrap_label_get_preferred_height (GtkWidget *widget,
56 gint *minimal_height, gint *natural_height);
57 static void geany_wrap_label_get_preferred_width_for_height (GtkWidget *widget,
58 gint height, gint *minimal_width, gint *natural_width);
59 static void geany_wrap_label_get_preferred_height_for_width (GtkWidget *widget,
60 gint width, gint *minimal_height, gint *natural_height);
61 static GtkSizeRequestMode geany_wrap_label_get_request_mode(GtkWidget *widget);
62 #else
63 static gboolean geany_wrap_label_expose (GtkWidget *widget, GdkEventExpose *event);
64 static void geany_wrap_label_size_request (GtkWidget *widget, GtkRequisition *req);
65 #endif
66 static void geany_wrap_label_size_allocate (GtkWidget *widget, GtkAllocation *alloc);
67 static void geany_wrap_label_set_wrap_width (GtkWidget *widget, gint width);
68 static void geany_wrap_label_label_notify (GObject *object, GParamSpec *pspec, gpointer data);
70 G_DEFINE_TYPE(GeanyWrapLabel, geany_wrap_label, GTK_TYPE_LABEL)
73 static void geany_wrap_label_class_init(GeanyWrapLabelClass *klass)
75 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
77 widget_class->size_allocate = geany_wrap_label_size_allocate;
78 #if GTK_CHECK_VERSION(3, 0, 0)
79 widget_class->draw = geany_wrap_label_draw;
80 widget_class->get_preferred_width = geany_wrap_label_get_preferred_width;
81 widget_class->get_preferred_width_for_height = geany_wrap_label_get_preferred_width_for_height;
82 widget_class->get_preferred_height = geany_wrap_label_get_preferred_height;
83 widget_class->get_preferred_height_for_width = geany_wrap_label_get_preferred_height_for_width;
84 widget_class->get_request_mode = geany_wrap_label_get_request_mode;
85 #else
86 widget_class->size_request = geany_wrap_label_size_request;
87 widget_class->expose_event = geany_wrap_label_expose;
88 #endif
90 g_type_class_add_private(klass, sizeof (GeanyWrapLabelPrivate));
94 static void geany_wrap_label_init(GeanyWrapLabel *self)
96 self->priv = G_TYPE_INSTANCE_GET_PRIVATE(self,
97 GEANY_WRAP_LABEL_TYPE, GeanyWrapLabelPrivate);
99 self->priv->wrap_width = 0;
100 self->priv->wrap_height = 0;
102 g_signal_connect(self, "notify::label", G_CALLBACK(geany_wrap_label_label_notify), NULL);
103 gtk_misc_set_alignment(GTK_MISC(self), 0.0, 0.0);
107 /* Sets the point at which the text should wrap. */
108 static void geany_wrap_label_set_wrap_width(GtkWidget *widget, gint width)
110 GeanyWrapLabel *self = GEANY_WRAP_LABEL(widget);
111 PangoLayout *layout;
113 if (width <= 0)
114 return;
116 layout = gtk_label_get_layout(GTK_LABEL(widget));
119 * We may need to reset the wrap width, so do this regardless of whether
120 * or not we've changed the width.
122 pango_layout_set_width(layout, width * PANGO_SCALE);
123 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
124 pango_layout_get_pixel_size(layout, NULL, &self->priv->wrap_height);
126 if (self->priv->wrap_width != width)
128 self->priv->wrap_width = width;
129 gtk_widget_queue_resize(widget);
134 /* updates the wrap width when the label text changes */
135 static void geany_wrap_label_label_notify(GObject *object, GParamSpec *pspec, gpointer data)
137 GeanyWrapLabel *self = GEANY_WRAP_LABEL(object);
139 geany_wrap_label_set_wrap_width(GTK_WIDGET(object), self->priv->wrap_width);
143 #if GTK_CHECK_VERSION(3, 0, 0)
144 /* makes sure the layout is setup for rendering and chains to parent renderer */
145 static gboolean geany_wrap_label_draw(GtkWidget *widget, cairo_t *cr)
147 GeanyWrapLabel *self = GEANY_WRAP_LABEL(widget);
148 PangoLayout *layout = gtk_label_get_layout(GTK_LABEL(widget));
150 pango_layout_set_width(layout, self->priv->wrap_width * PANGO_SCALE);
151 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
153 return (* GTK_WIDGET_CLASS(geany_wrap_label_parent_class)->draw)(widget, cr);
157 static void geany_wrap_label_get_preferred_width (GtkWidget *widget,
158 gint *minimal_width, gint *natural_width)
160 *minimal_width = *natural_width = 0;
164 static void geany_wrap_label_get_preferred_width_for_height (GtkWidget *widget,
165 gint height, gint *minimal_width, gint *natural_width)
167 PangoLayout *layout = gtk_label_get_layout(GTK_LABEL(widget));;
169 pango_layout_set_height(layout, height * PANGO_SCALE);
170 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
171 pango_layout_get_pixel_size(layout, natural_width, NULL);
173 *minimal_width = 0;
177 static void geany_wrap_label_get_preferred_height (GtkWidget *widget,
178 gint *minimal_height, gint *natural_height)
180 *minimal_height = *natural_height = GEANY_WRAP_LABEL(widget)->priv->wrap_height;
184 static void geany_wrap_label_get_preferred_height_for_width (GtkWidget *widget,
185 gint width, gint *minimal_height, gint *natural_height)
187 PangoLayout *layout = gtk_label_get_layout(GTK_LABEL(widget));
189 pango_layout_set_width(layout, width * PANGO_SCALE);
190 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
191 pango_layout_get_pixel_size(layout, NULL, natural_height);
193 *minimal_height = *natural_height;
197 static GtkSizeRequestMode geany_wrap_label_get_request_mode(GtkWidget *widget)
199 return GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
202 #else /* GTK3 */
204 /* makes sure the layout is setup for rendering and chains to parent renderer */
205 static gboolean geany_wrap_label_expose(GtkWidget *widget, GdkEventExpose *event)
207 GeanyWrapLabel *self = GEANY_WRAP_LABEL(widget);
208 PangoLayout *layout = gtk_label_get_layout(GTK_LABEL(widget));
210 pango_layout_set_width(layout, self->priv->wrap_width * PANGO_SCALE);
211 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
213 return (* GTK_WIDGET_CLASS(geany_wrap_label_parent_class)->expose_event)(widget, event);
217 /* Forces the height to be the size necessary for the Pango layout, while allowing the
218 * width to be flexible. */
219 static void geany_wrap_label_size_request(GtkWidget *widget, GtkRequisition *req)
221 req->width = 0;
222 req->height = GEANY_WRAP_LABEL(widget)->priv->wrap_height;
224 #endif /* GTK3 */
227 /* Sets the wrap width to the width allocated to us. */
228 static void geany_wrap_label_size_allocate(GtkWidget *widget, GtkAllocation *alloc)
230 (* GTK_WIDGET_CLASS(geany_wrap_label_parent_class)->size_allocate)(widget, alloc);
232 geany_wrap_label_set_wrap_width(widget, alloc->width);
234 #if GTK_CHECK_VERSION(3, 0, 0)
236 /* ask the parent to recompute our size, because it seems GTK3 size
237 * caching is too aggressive */
238 GtkWidget *parent = gtk_widget_get_parent(widget);
239 if (GTK_IS_CONTAINER(parent))
240 gtk_container_check_resize(GTK_CONTAINER(parent));
242 #endif
246 GtkWidget *geany_wrap_label_new(const gchar *text)
248 return g_object_new(GEANY_WRAP_LABEL_TYPE, "label", text, NULL);