Use pre-compiled 'grep' binary from MSYS2 on Windows
[geany-mirror.git] / src / geanywraplabel.c
blob0b0d113e763538301932b3190efa17f547595825
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)
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #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 #if GTK_CHECK_VERSION(3, 0, 0)
53 static gboolean geany_wrap_label_draw(GtkWidget *widget, cairo_t *cr);
54 static void geany_wrap_label_get_preferred_width (GtkWidget *widget,
55 gint *minimal_width, gint *natural_width);
56 static void geany_wrap_label_get_preferred_height (GtkWidget *widget,
57 gint *minimal_height, gint *natural_height);
58 static void geany_wrap_label_get_preferred_width_for_height (GtkWidget *widget,
59 gint height, gint *minimal_width, gint *natural_width);
60 static void geany_wrap_label_get_preferred_height_for_width (GtkWidget *widget,
61 gint width, gint *minimal_height, gint *natural_height);
62 static GtkSizeRequestMode geany_wrap_label_get_request_mode(GtkWidget *widget);
63 #else
64 static gboolean geany_wrap_label_expose (GtkWidget *widget, GdkEventExpose *event);
65 static void geany_wrap_label_size_request (GtkWidget *widget, GtkRequisition *req);
66 #endif
67 static void geany_wrap_label_size_allocate (GtkWidget *widget, GtkAllocation *alloc);
68 static void geany_wrap_label_set_wrap_width (GtkWidget *widget, gint width);
69 static void geany_wrap_label_label_notify (GObject *object, GParamSpec *pspec, gpointer data);
71 G_DEFINE_TYPE(GeanyWrapLabel, geany_wrap_label, GTK_TYPE_LABEL)
74 static void geany_wrap_label_class_init(GeanyWrapLabelClass *klass)
76 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
78 widget_class->size_allocate = geany_wrap_label_size_allocate;
79 #if GTK_CHECK_VERSION(3, 0, 0)
80 widget_class->draw = geany_wrap_label_draw;
81 widget_class->get_preferred_width = geany_wrap_label_get_preferred_width;
82 widget_class->get_preferred_width_for_height = geany_wrap_label_get_preferred_width_for_height;
83 widget_class->get_preferred_height = geany_wrap_label_get_preferred_height;
84 widget_class->get_preferred_height_for_width = geany_wrap_label_get_preferred_height_for_width;
85 widget_class->get_request_mode = geany_wrap_label_get_request_mode;
86 #else
87 widget_class->size_request = geany_wrap_label_size_request;
88 widget_class->expose_event = geany_wrap_label_expose;
89 #endif
91 g_type_class_add_private(klass, sizeof (GeanyWrapLabelPrivate));
95 static void geany_wrap_label_init(GeanyWrapLabel *self)
97 self->priv = G_TYPE_INSTANCE_GET_PRIVATE(self,
98 GEANY_WRAP_LABEL_TYPE, GeanyWrapLabelPrivate);
100 self->priv->wrap_width = 0;
101 self->priv->wrap_height = 0;
103 g_signal_connect(self, "notify::label", G_CALLBACK(geany_wrap_label_label_notify), NULL);
104 gtk_misc_set_alignment(GTK_MISC(self), 0.0, 0.0);
108 /* Sets the point at which the text should wrap. */
109 static void geany_wrap_label_set_wrap_width(GtkWidget *widget, gint width)
111 GeanyWrapLabel *self = GEANY_WRAP_LABEL(widget);
112 PangoLayout *layout;
114 if (width <= 0)
115 return;
117 layout = gtk_label_get_layout(GTK_LABEL(widget));
120 * We may need to reset the wrap width, so do this regardless of whether
121 * or not we've changed the width.
123 pango_layout_set_width(layout, width * PANGO_SCALE);
124 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
125 pango_layout_get_pixel_size(layout, NULL, &self->priv->wrap_height);
127 if (self->priv->wrap_width != width)
129 self->priv->wrap_width = width;
130 gtk_widget_queue_resize(widget);
135 /* updates the wrap width when the label text changes */
136 static void geany_wrap_label_label_notify(GObject *object, GParamSpec *pspec, gpointer data)
138 GeanyWrapLabel *self = GEANY_WRAP_LABEL(object);
140 geany_wrap_label_set_wrap_width(GTK_WIDGET(object), self->priv->wrap_width);
144 #if GTK_CHECK_VERSION(3, 0, 0)
145 /* makes sure the layout is setup for rendering and chains to parent renderer */
146 static gboolean geany_wrap_label_draw(GtkWidget *widget, cairo_t *cr)
148 GeanyWrapLabel *self = GEANY_WRAP_LABEL(widget);
149 PangoLayout *layout = gtk_label_get_layout(GTK_LABEL(widget));
151 pango_layout_set_width(layout, self->priv->wrap_width * PANGO_SCALE);
152 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
154 return (* GTK_WIDGET_CLASS(geany_wrap_label_parent_class)->draw)(widget, cr);
158 static void geany_wrap_label_get_preferred_width (GtkWidget *widget,
159 gint *minimal_width, gint *natural_width)
161 *minimal_width = *natural_width = 0;
165 static void geany_wrap_label_get_preferred_width_for_height (GtkWidget *widget,
166 gint height, gint *minimal_width, gint *natural_width)
168 PangoLayout *layout = gtk_label_get_layout(GTK_LABEL(widget));;
170 pango_layout_set_height(layout, height * PANGO_SCALE);
171 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
172 pango_layout_get_pixel_size(layout, natural_width, NULL);
174 *minimal_width = 0;
178 static void geany_wrap_label_get_preferred_height (GtkWidget *widget,
179 gint *minimal_height, gint *natural_height)
181 *minimal_height = *natural_height = GEANY_WRAP_LABEL(widget)->priv->wrap_height;
185 static void geany_wrap_label_get_preferred_height_for_width (GtkWidget *widget,
186 gint width, gint *minimal_height, gint *natural_height)
188 PangoLayout *layout = gtk_label_get_layout(GTK_LABEL(widget));
190 pango_layout_set_width(layout, width * PANGO_SCALE);
191 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
192 pango_layout_get_pixel_size(layout, NULL, natural_height);
194 *minimal_height = *natural_height;
198 static GtkSizeRequestMode geany_wrap_label_get_request_mode(GtkWidget *widget)
200 return GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
203 #else /* GTK3 */
205 /* makes sure the layout is setup for rendering and chains to parent renderer */
206 static gboolean geany_wrap_label_expose(GtkWidget *widget, GdkEventExpose *event)
208 GeanyWrapLabel *self = GEANY_WRAP_LABEL(widget);
209 PangoLayout *layout = gtk_label_get_layout(GTK_LABEL(widget));
211 pango_layout_set_width(layout, self->priv->wrap_width * PANGO_SCALE);
212 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
214 return (* GTK_WIDGET_CLASS(geany_wrap_label_parent_class)->expose_event)(widget, event);
218 /* Forces the height to be the size necessary for the Pango layout, while allowing the
219 * width to be flexible. */
220 static void geany_wrap_label_size_request(GtkWidget *widget, GtkRequisition *req)
222 req->width = 0;
223 req->height = GEANY_WRAP_LABEL(widget)->priv->wrap_height;
225 #endif /* GTK3 */
228 /* Sets the wrap width to the width allocated to us. */
229 static void geany_wrap_label_size_allocate(GtkWidget *widget, GtkAllocation *alloc)
231 (* GTK_WIDGET_CLASS(geany_wrap_label_parent_class)->size_allocate)(widget, alloc);
233 geany_wrap_label_set_wrap_width(widget, alloc->width);
235 #if GTK_CHECK_VERSION(3, 0, 0)
237 /* ask the parent to recompute our size, because it seems GTK3 size
238 * caching is too aggressive */
239 GtkWidget *parent = gtk_widget_get_parent(widget);
240 if (GTK_IS_CONTAINER(parent))
241 gtk_container_check_resize(GTK_CONTAINER(parent));
243 #endif
247 GtkWidget *geany_wrap_label_new(const gchar *text)
249 return g_object_new(GEANY_WRAP_LABEL_TYPE, "label", text, NULL);