r2228: Made 'Automatic' an icon size, rather than a separate option.
[rox-filer.git] / ROX-Filer / src / wrapped.c
blob3fd3c8c15fa2f952a82a909ef79b88c99a6c8c57
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2002, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* wrapped.c - a replacement for GtkLabel that wraps sensibly */
24 #include "config.h"
26 #include <gtk/gtk.h>
28 #include "global.h"
30 #include "wrapped.h"
32 static gpointer parent_class = NULL;
34 /* Static prototypes */
35 static void wrapped_label_finialize(GObject *object);
36 static void wrapped_label_class_init(gpointer gclass, gpointer data);
37 static void wrapped_label_init(GTypeInstance *object, gpointer gclass);
38 static void wrapped_label_size_request(GtkWidget *widget,
39 GtkRequisition *requisition);
40 static gint wrapped_label_expose(GtkWidget *widget, GdkEventExpose *event);
41 static void wrapped_label_style_set(GtkWidget *widget,
42 GtkStyle *previous_style);
45 /****************************************************************
46 * EXTERNAL INTERFACE *
47 ****************************************************************/
49 GtkWidget *wrapped_label_new(const char *text, gint width)
51 WrappedLabel *wl;
53 wl = g_object_new(wrapped_label_get_type(), NULL);
54 wl->width = width;
56 wrapped_label_set_text(wl, text);
58 return GTK_WIDGET(wl);
61 void wrapped_label_set_text(WrappedLabel *wl, const char *text)
63 GtkWidget *widget = GTK_WIDGET(wl);
65 if (!wl->layout)
67 wl->layout = gtk_widget_create_pango_layout(widget, text);
68 pango_layout_set_width(wl->layout, wl->width * PANGO_SCALE);
69 pango_layout_set_wrap(wl->layout, PANGO_WRAP_WORD);
70 pango_layout_set_alignment(wl->layout, PANGO_ALIGN_CENTER);
73 gtk_widget_queue_resize(widget);
76 /****************************************************************
77 * INTERNAL FUNCTIONS *
78 ****************************************************************/
80 static void wrapped_label_finialize(GObject *object)
82 WrappedLabel *wl = (WrappedLabel *) object;
84 if (wl->layout)
86 g_object_unref(G_OBJECT(wl->layout));
87 wl->layout = NULL;
90 G_OBJECT_CLASS(parent_class)->finalize(object);
93 static void wrapped_label_class_init(gpointer gclass, gpointer data)
95 GObjectClass *object = (GObjectClass *) gclass;
96 GtkWidgetClass *widget_class = (GtkWidgetClass *) gclass;
98 parent_class = g_type_class_peek_parent(gclass);
100 object->finalize = wrapped_label_finialize;
102 widget_class->size_request = wrapped_label_size_request;
103 widget_class->expose_event = wrapped_label_expose;
104 widget_class->style_set = wrapped_label_style_set;
107 static void wrapped_label_init(GTypeInstance *object, gpointer gclass)
109 WrappedLabel *wl = (WrappedLabel *) object;
110 GtkWidget *widget = (GtkWidget *) object;
112 GTK_WIDGET_SET_FLAGS(widget, GTK_NO_WINDOW);
114 wl->layout = NULL;
115 wl->width = -1;
118 GType wrapped_label_get_type(void)
120 static GType type = 0;
122 if (!type)
124 static const GTypeInfo info =
126 sizeof (WrappedLabelClass),
127 NULL, /* base_init */
128 NULL, /* base_finalise */
129 wrapped_label_class_init,
130 NULL, /* class_finalise */
131 NULL, /* class_data */
132 sizeof(WrappedLabel),
133 0, /* n_preallocs */
134 wrapped_label_init
137 type = g_type_register_static(GTK_TYPE_WIDGET, "WrappedLabel",
138 &info, 0);
141 return type;
144 static void wrapped_label_size_request(GtkWidget *widget,
145 GtkRequisition *requisition)
147 PangoRectangle logical_rect;
148 WrappedLabel *wl = (WrappedLabel *) widget;
150 g_return_if_fail(wl->layout != NULL);
152 pango_layout_get_extents(wl->layout, NULL, &logical_rect);
154 wl->x_off = PANGO_PIXELS(logical_rect.x);
155 wl->y_off = PANGO_PIXELS(logical_rect.y);
157 requisition->width = PANGO_PIXELS(logical_rect.width);
158 requisition->height = PANGO_PIXELS(logical_rect.height);
161 static gint wrapped_label_expose(GtkWidget *widget, GdkEventExpose *event)
163 WrappedLabel *wl = (WrappedLabel *) widget;
165 g_return_val_if_fail(event != NULL, TRUE);
166 g_return_val_if_fail(wl->layout != NULL, TRUE);
168 gtk_paint_layout(widget->style,
169 widget->window,
170 GTK_WIDGET_STATE(widget),
171 FALSE,
172 &event->area,
173 widget,
174 "label",
175 widget->allocation.x - wl->x_off,
176 widget->allocation.y - wl->y_off,
177 wl->layout);
179 return FALSE;
182 static void wrapped_label_style_set(GtkWidget *widget,
183 GtkStyle *previous_style)
185 WrappedLabel *wl = (WrappedLabel *) widget;
187 if (wl->layout)
189 const gchar *txt;
190 PangoLayout *old = wl->layout;
192 txt = pango_layout_get_text(wl->layout);
194 wl->layout = NULL;
195 wrapped_label_set_text(wl, txt);
197 g_object_unref(old);