geany  1.38
About: Geany is a text editor (using GTK2) with basic features of an integrated development environment (syntax highlighting, code folding, symbol name auto-completion, ...). F: office T: editor programming GTK+ IDE
  Fossies Dox: geany-1.38.tar.bz2  ("unofficial" and yet experimental doxygen-generated source code documentation)  

geanywraplabel.c
Go to the documentation of this file.
1/*
2 * geanywraplabel.c - this file is part of Geany, a fast and lightweight IDE
3 *
4 * Copyright 2009 The Geany contributors
5 *
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.
10 *
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.
15 *
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.
19 */
20
21/*
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)
24 */
25
26#ifdef HAVE_CONFIG_H
27# include "config.h"
28#endif
29
30#include "geanywraplabel.h"
31
32
34{
35 GtkLabelClass parent_class;
36};
37
38typedef struct
39{
43
45{
46 GtkLabel parent;
48};
49
50
51static gboolean geany_wrap_label_draw(GtkWidget *widget, cairo_t *cr);
52static void geany_wrap_label_get_preferred_width (GtkWidget *widget,
53 gint *minimal_width, gint *natural_width);
54static void geany_wrap_label_get_preferred_height (GtkWidget *widget,
55 gint *minimal_height, gint *natural_height);
56static void geany_wrap_label_get_preferred_width_for_height (GtkWidget *widget,
57 gint height, gint *minimal_width, gint *natural_width);
58static void geany_wrap_label_get_preferred_height_for_width (GtkWidget *widget,
59 gint width, gint *minimal_height, gint *natural_height);
60static GtkSizeRequestMode geany_wrap_label_get_request_mode(GtkWidget *widget);
61static void geany_wrap_label_size_allocate (GtkWidget *widget, GtkAllocation *alloc);
62static void geany_wrap_label_set_wrap_width (GtkWidget *widget, gint width);
63static void geany_wrap_label_label_notify (GObject *object, GParamSpec *pspec, gpointer data);
64
65G_DEFINE_TYPE(GeanyWrapLabel, geany_wrap_label, GTK_TYPE_LABEL)
66
67
69{
70 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
71
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;
79
80 g_type_class_add_private(klass, sizeof (GeanyWrapLabelPrivate));
81}
82
83
85{
86 self->priv = G_TYPE_INSTANCE_GET_PRIVATE(self,
88
89 self->priv->wrap_width = 0;
90 self->priv->wrap_height = 0;
91
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);
94}
95
96
97/* Sets the point at which the text should wrap. */
98static void geany_wrap_label_set_wrap_width(GtkWidget *widget, gint width)
99{
100 GeanyWrapLabel *self = GEANY_WRAP_LABEL(widget);
101 PangoLayout *layout;
102
103 if (width <= 0)
104 return;
105
106 layout = gtk_label_get_layout(GTK_LABEL(widget));
107
108 /*
109 * We may need to reset the wrap width, so do this regardless of whether
110 * or not we've changed the width.
111 */
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);
115
116 if (self->priv->wrap_width != width)
117 {
118 self->priv->wrap_width = width;
119 gtk_widget_queue_resize(widget);
120 }
121}
122
123
124/* updates the wrap width when the label text changes */
125static void geany_wrap_label_label_notify(GObject *object, GParamSpec *pspec, gpointer data)
126{
127 GeanyWrapLabel *self = GEANY_WRAP_LABEL(object);
128
129 geany_wrap_label_set_wrap_width(GTK_WIDGET(object), self->priv->wrap_width);
130}
131
132
133/* makes sure the layout is setup for rendering and chains to parent renderer */
134static gboolean geany_wrap_label_draw(GtkWidget *widget, cairo_t *cr)
135{
136 GeanyWrapLabel *self = GEANY_WRAP_LABEL(widget);
137 PangoLayout *layout = gtk_label_get_layout(GTK_LABEL(widget));
138
139 pango_layout_set_width(layout, self->priv->wrap_width * PANGO_SCALE);
140 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
141
142 return (* GTK_WIDGET_CLASS(geany_wrap_label_parent_class)->draw)(widget, cr);
143}
144
145
146static void geany_wrap_label_get_preferred_width (GtkWidget *widget,
147 gint *minimal_width, gint *natural_width)
148{
149 *minimal_width = *natural_width = 0;
150}
151
152
154 gint height, gint *minimal_width, gint *natural_width)
155{
156 PangoLayout *layout = gtk_label_get_layout(GTK_LABEL(widget));;
157
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);
161
162 *minimal_width = 0;
163}
164
165
166static void geany_wrap_label_get_preferred_height (GtkWidget *widget,
167 gint *minimal_height, gint *natural_height)
168{
169 *minimal_height = *natural_height = GEANY_WRAP_LABEL(widget)->priv->wrap_height;
170}
171
172
174 gint width, gint *minimal_height, gint *natural_height)
175{
176 PangoLayout *layout = gtk_label_get_layout(GTK_LABEL(widget));
177
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);
181
182 *minimal_height = *natural_height;
183}
184
185
186static GtkSizeRequestMode geany_wrap_label_get_request_mode(GtkWidget *widget)
187{
188 return GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
189}
190
191
192/* Sets the wrap width to the width allocated to us. */
193static void geany_wrap_label_size_allocate(GtkWidget *widget, GtkAllocation *alloc)
194{
195 GtkWidget *parent;
196
197 (* GTK_WIDGET_CLASS(geany_wrap_label_parent_class)->size_allocate)(widget, alloc);
198
199 geany_wrap_label_set_wrap_width(widget, alloc->width);
200
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));
206}
207
208
209GtkWidget *geany_wrap_label_new(const gchar *text)
210{
211 return g_object_new(GEANY_WRAP_LABEL_TYPE, "label", text, NULL);
212}
gchar * text
Definition: editor.c:83
static void geany_wrap_label_get_preferred_height_for_width(GtkWidget *widget, gint width, gint *minimal_height, gint *natural_height)
static void geany_wrap_label_init(GeanyWrapLabel *self)
static void geany_wrap_label_set_wrap_width(GtkWidget *widget, gint width)
static void geany_wrap_label_class_init(GeanyWrapLabelClass *klass)
static void geany_wrap_label_label_notify(GObject *object, GParamSpec *pspec, gpointer data)
static void geany_wrap_label_size_allocate(GtkWidget *widget, GtkAllocation *alloc)
static GtkSizeRequestMode geany_wrap_label_get_request_mode(GtkWidget *widget)
static void geany_wrap_label_get_preferred_width(GtkWidget *widget, gint *minimal_width, gint *natural_width)
static void geany_wrap_label_get_preferred_height(GtkWidget *widget, gint *minimal_height, gint *natural_height)
GtkWidget * geany_wrap_label_new(const gchar *text)
static void geany_wrap_label_get_preferred_width_for_height(GtkWidget *widget, gint height, gint *minimal_width, gint *natural_width)
static gboolean geany_wrap_label_draw(GtkWidget *widget, cairo_t *cr)
#define GEANY_WRAP_LABEL(obj)
#define GEANY_WRAP_LABEL_TYPE
#define NULL
Definition: rbtree.h:150
GtkLabelClass parent_class
GeanyWrapLabelPrivate * priv