2 * geanywraplabel.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2009-2010 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2009-2010 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.
25 * A GtkLabel subclass that can wrap to any width, unlike GtkLabel which has a fixed wrap point.
26 * (inspired by libview's WrapLabel, http://view.sourceforge.net)
32 #include "geanywraplabel.h"
36 #define GEANY_WRAP_LABEL_GET_PRIVATE(obj) (GEANY_WRAP_LABEL(obj)->priv)
39 struct _GeanyWrapLabelClass
41 GtkLabelClass parent_class
;
47 } GeanyWrapLabelPrivate
;
49 struct _GeanyWrapLabel
52 GeanyWrapLabelPrivate
*priv
;
56 static void geany_wrap_label_size_request (GtkWidget
*widget
, GtkRequisition
*req
);
57 static void geany_wrap_label_size_allocate (GtkWidget
*widget
, GtkAllocation
*alloc
);
58 static void geany_wrap_label_set_wrap_width (GtkWidget
*widget
, gsize width
);
60 G_DEFINE_TYPE(GeanyWrapLabel
, geany_wrap_label
, GTK_TYPE_LABEL
)
63 static void geany_wrap_label_class_init(GeanyWrapLabelClass
*klass
)
65 GtkWidgetClass
*widget_class
= GTK_WIDGET_CLASS(klass
);
67 widget_class
->size_request
= geany_wrap_label_size_request
;
68 widget_class
->size_allocate
= geany_wrap_label_size_allocate
;
70 g_type_class_add_private(klass
, sizeof (GeanyWrapLabelPrivate
));
74 static void geany_wrap_label_init(GeanyWrapLabel
*self
)
76 GeanyWrapLabelPrivate
*priv
;
78 self
->priv
= G_TYPE_INSTANCE_GET_PRIVATE(self
,
79 GEANY_WRAP_LABEL_TYPE
, GeanyWrapLabelPrivate
);
86 /* Sets the point at which the text should wrap. */
87 static void geany_wrap_label_set_wrap_width(GtkWidget
*widget
, gsize width
)
89 GeanyWrapLabelPrivate
*priv
;
95 * We may need to reset the wrap width, so do this regardless of whether
96 * or not we've changed the width.
98 pango_layout_set_width(gtk_label_get_layout(GTK_LABEL(widget
)), width
* PANGO_SCALE
);
100 priv
= GEANY_WRAP_LABEL_GET_PRIVATE(widget
);
101 if (priv
->wrap_width
!= width
)
103 priv
->wrap_width
= width
;
104 gtk_widget_queue_resize(widget
);
109 /* Forces the height to be the size necessary for the Pango layout, while allowing the
110 * width to be flexible. */
111 static void geany_wrap_label_size_request(GtkWidget
*widget
, GtkRequisition
*req
)
115 pango_layout_get_pixel_size(gtk_label_get_layout(GTK_LABEL(widget
)), NULL
, &height
);
118 req
->height
= height
;
122 /* Sets the wrap width to the width allocated to us. */
123 static void geany_wrap_label_size_allocate(GtkWidget
*widget
, GtkAllocation
*alloc
)
125 (* GTK_WIDGET_CLASS(geany_wrap_label_parent_class
)->size_allocate
)(widget
, alloc
);
127 geany_wrap_label_set_wrap_width(widget
, alloc
->width
);
131 void geany_wrap_label_set_text(GtkLabel
*label
, const gchar
*text
)
133 GeanyWrapLabelPrivate
*priv
= GEANY_WRAP_LABEL_GET_PRIVATE(label
);
135 gtk_label_set_text(label
, text
);
136 geany_wrap_label_set_wrap_width(GTK_WIDGET(label
), priv
->wrap_width
);
140 GtkWidget
*geany_wrap_label_new(const gchar
*text
)
142 GtkWidget
*l
= g_object_new(GEANY_WRAP_LABEL_TYPE
, NULL
);
145 gtk_label_set_text(GTK_LABEL(l
), text
);
147 pango_layout_set_wrap(gtk_label_get_layout(GTK_LABEL(l
)), PANGO_WRAP_WORD_CHAR
);
148 gtk_misc_set_alignment(GTK_MISC(l
), 0.0, 0.0);