1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 static void gtk_label_class_init (GtkLabelClass *klass);
23 static void gtk_label_init (GtkLabel *label);
24 static void gtk_label_destroy (GtkObject *object);
25 static void gtk_label_size_request (GtkWidget *widget,
26 GtkRequisition *requisition);
27 static gint gtk_label_expose (GtkWidget *widget,
28 GdkEventExpose *event);
31 static GtkMiscClass *parent_class = NULL;
37 static guint label_type = 0;
41 GtkTypeInfo label_info =
45 sizeof (GtkLabelClass),
46 (GtkClassInitFunc) gtk_label_class_init,
47 (GtkObjectInitFunc) gtk_label_init,
51 label_type = gtk_type_unique (gtk_misc_get_type (), &label_info);
58 gtk_label_class_init (GtkLabelClass *class)
60 GtkObjectClass *object_class;
61 GtkWidgetClass *widget_class;
63 object_class = (GtkObjectClass*) class;
64 widget_class = (GtkWidgetClass*) class;
66 parent_class = gtk_type_class (gtk_misc_get_type ());
68 object_class->destroy = gtk_label_destroy;
70 widget_class->size_request = gtk_label_size_request;
71 widget_class->expose_event = gtk_label_expose;
75 gtk_label_init (GtkLabel *label)
77 GTK_WIDGET_SET_FLAGS (label, GTK_NO_WINDOW);
81 label->jtype = GTK_JUSTIFY_CENTER;
85 gtk_label_new (const char *str)
89 g_return_val_if_fail (str != NULL, NULL);
91 label = gtk_type_new (gtk_label_get_type ());
93 gtk_label_set (label, str);
95 return GTK_WIDGET (label);
99 gtk_label_set (GtkLabel *label,
104 g_return_if_fail (label != NULL);
105 g_return_if_fail (GTK_IS_LABEL (label));
106 g_return_if_fail (str != NULL);
109 g_free (label->label);
110 label->label = g_strdup (str);
113 g_slist_free (label->row);
115 label->row = g_slist_append (label->row, label->label);
117 while ((p = strchr(p, '\n')))
118 label->row = g_slist_append (label->row, ++p);
120 if (GTK_WIDGET_VISIBLE (label))
122 if (GTK_WIDGET_MAPPED (label))
123 gdk_window_clear_area (GTK_WIDGET (label)->window,
124 GTK_WIDGET (label)->allocation.x,
125 GTK_WIDGET (label)->allocation.y,
126 GTK_WIDGET (label)->allocation.width,
127 GTK_WIDGET (label)->allocation.height);
129 gtk_widget_queue_resize (GTK_WIDGET (label));
134 gtk_label_set_justify (GtkLabel *label, GtkJustification jtype)
136 g_return_if_fail (label != NULL);
137 g_return_if_fail (GTK_IS_LABEL (label));
139 if ((GtkJustification) label->jtype != jtype)
141 label->jtype = jtype;
143 if (GTK_WIDGET_VISIBLE (label))
145 if (GTK_WIDGET_MAPPED (label))
146 gdk_window_clear_area (GTK_WIDGET (label)->window,
147 GTK_WIDGET (label)->allocation.x,
148 GTK_WIDGET (label)->allocation.y,
149 GTK_WIDGET (label)->allocation.width,
150 GTK_WIDGET (label)->allocation.height);
152 gtk_widget_queue_resize (GTK_WIDGET (label));
158 gtk_label_get (GtkLabel *label,
161 g_return_if_fail (label != NULL);
162 g_return_if_fail (GTK_IS_LABEL (label));
163 g_return_if_fail (str != NULL);
170 gtk_label_destroy (GtkObject *object)
174 g_return_if_fail (object != NULL);
175 g_return_if_fail (GTK_IS_LABEL (object));
177 label = GTK_LABEL (object);
179 if (GTK_WIDGET (object)->parent &&
180 GTK_WIDGET_MAPPED (object))
181 gtk_widget_unmap (GTK_WIDGET (object));
183 g_free (label->label);
184 g_slist_free (label->row);
186 if (GTK_OBJECT_CLASS (parent_class)->destroy)
187 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
191 gtk_label_size_request (GtkWidget *widget,
192 GtkRequisition *requisition)
198 g_return_if_fail (widget != NULL);
199 g_return_if_fail (GTK_IS_LABEL (widget));
200 g_return_if_fail (requisition != NULL);
202 label = GTK_LABEL (widget);
210 gdk_text_width (GTK_WIDGET (label)->style->font, row->data,
211 (gchar*) row->next->data - (gchar*) row->data));
213 width = MAX (width, gdk_string_width (GTK_WIDGET (label)->style->font, row->data));
217 requisition->width = width + label->misc.xpad * 2;
218 requisition->height = ((GTK_WIDGET (label)->style->font->ascent +
219 GTK_WIDGET (label)->style->font->descent + 2) *
220 g_slist_length(label->row) +
221 label->misc.ypad * 2);
225 gtk_label_expose (GtkWidget *widget,
226 GdkEventExpose *event)
237 g_return_val_if_fail (widget != NULL, FALSE);
238 g_return_val_if_fail (GTK_IS_LABEL (widget), FALSE);
239 g_return_val_if_fail (event != NULL, FALSE);
241 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
243 label = GTK_LABEL (widget);
244 misc = GTK_MISC (widget);
246 state = widget->state;
247 if (!GTK_WIDGET_IS_SENSITIVE (widget))
248 state = GTK_STATE_INSENSITIVE;
250 maxl = widget->requisition.width - misc->xpad * 2;
255 gdk_gc_set_clip_rectangle (widget->style->white_gc, &event->area);
256 gdk_gc_set_clip_rectangle (widget->style->fg_gc[state], &event->area);
258 x = widget->allocation.x + misc->xpad +
259 (widget->allocation.width - widget->requisition.width)
260 * misc->xalign + 0.5;
262 y = (widget->allocation.y * (1.0 - misc->yalign) +
263 (widget->allocation.y + widget->allocation.height -
264 (widget->requisition.height - misc->ypad * 2)) *
265 misc->yalign + widget->style->font->ascent) + 1.5;
268 while (row && row->next)
270 len = (gchar*) row->next->data - (gchar*) row->data;
273 if (label->jtype == GTK_JUSTIFY_CENTER)
274 offset = (maxl - gdk_text_width (widget->style->font, row->data, len)) / 2;
276 else if (label->jtype == GTK_JUSTIFY_RIGHT)
277 offset = (maxl - gdk_text_width (widget->style->font, row->data, len));
279 if (state == GTK_STATE_INSENSITIVE)
280 gdk_draw_text (widget->window, widget->style->font,
281 widget->style->white_gc,
282 offset + x + 1, y + 1, row->data, len);
284 gdk_draw_text (widget->window, widget->style->font,
285 widget->style->fg_gc[state],
286 offset + x, y, row->data, len);
288 y += widget->style->font->ascent + widget->style->font->descent + 2;
292 * COMMENT: we can avoid gdk_text_width() calls here storing in label->row
293 * the widths of the rows calculated in gtk_label_set.
294 * Once we have a wrapping interface we can support GTK_JUSTIFY_FILL.
298 if (label->jtype == GTK_JUSTIFY_CENTER)
299 offset = (maxl - gdk_string_width (widget->style->font, row->data)) / 2;
301 else if (label->jtype == GTK_JUSTIFY_RIGHT)
302 offset = (maxl - gdk_string_width (widget->style->font, row->data));
304 if (state == GTK_STATE_INSENSITIVE)
305 gdk_draw_string (widget->window, widget->style->font,
306 widget->style->white_gc,
307 offset + x + 1, y + 1, row->data);
309 gdk_draw_string (widget->window, widget->style->font,
310 widget->style->fg_gc[state],
311 offset + x, y, row->data);
313 gdk_gc_set_clip_mask (widget->style->white_gc, NULL);
314 gdk_gc_set_clip_mask (widget->style->fg_gc[state], NULL);