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
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
29 static void gtk_label_class_init (GtkLabelClass *klass);
30 static void gtk_label_init (GtkLabel *label);
31 static void gtk_label_set_arg (GtkObject *object,
34 static void gtk_label_get_arg (GtkObject *object,
37 static void gtk_label_finalize (GtkObject *object);
38 static void gtk_label_size_request (GtkWidget *widget,
39 GtkRequisition *requisition);
40 static gint gtk_label_expose (GtkWidget *widget,
41 GdkEventExpose *event);
42 static void gtk_label_state_changed (GtkWidget *widget,
43 guint previous_state);
44 static void gtk_label_style_set (GtkWidget *widget,
45 GtkStyle *previous_style);
49 static GtkMiscClass *parent_class = NULL;
53 gtk_label_get_type (void)
55 static GtkType label_type = 0;
59 GtkTypeInfo label_info =
63 sizeof (GtkLabelClass),
64 (GtkClassInitFunc) gtk_label_class_init,
65 (GtkObjectInitFunc) gtk_label_init,
66 /* reversed_1 */ NULL,
67 /* reversed_2 */ NULL,
68 (GtkClassInitFunc) NULL,
71 label_type = gtk_type_unique (gtk_misc_get_type (), &label_info);
72 gtk_type_set_chunk_alloc (label_type, 32);
79 gtk_label_class_init (GtkLabelClass *class)
81 GtkObjectClass *object_class;
82 GtkWidgetClass *widget_class;
84 object_class = (GtkObjectClass*) class;
85 widget_class = (GtkWidgetClass*) class;
87 parent_class = gtk_type_class (gtk_misc_get_type ());
89 gtk_object_add_arg_type ("GtkLabel::label", GTK_TYPE_STRING, GTK_ARG_READWRITE, ARG_LABEL);
90 gtk_object_add_arg_type ("GtkLabel::justify", GTK_TYPE_JUSTIFICATION, GTK_ARG_READWRITE, ARG_JUSTIFY);
92 object_class->set_arg = gtk_label_set_arg;
93 object_class->get_arg = gtk_label_get_arg;
94 object_class->finalize = gtk_label_finalize;
96 widget_class->size_request = gtk_label_size_request;
97 widget_class->expose_event = gtk_label_expose;
98 widget_class->style_set = gtk_label_style_set;
99 widget_class->state_changed = gtk_label_state_changed;
103 gtk_label_set_arg (GtkObject *object,
109 label = GTK_LABEL (object);
114 gtk_label_set (label, GTK_VALUE_STRING (*arg));
117 gtk_label_set_justify (label, GTK_VALUE_ENUM (*arg));
125 gtk_label_get_arg (GtkObject *object,
131 label = GTK_LABEL (object);
136 GTK_VALUE_STRING (*arg) = g_strdup (label->label);
139 GTK_VALUE_ENUM (*arg) = label->jtype;
142 arg->type = GTK_TYPE_INVALID;
148 gtk_label_init (GtkLabel *label)
150 GTK_WIDGET_SET_FLAGS (label, GTK_NO_WINDOW);
154 label->max_width = 0;
155 label->jtype = GTK_JUSTIFY_CENTER;
156 label->needs_clear = FALSE;
158 gtk_label_set (label, "");
162 gtk_label_new (const gchar *str)
166 g_return_val_if_fail (str != NULL, NULL);
168 label = gtk_type_new (gtk_label_get_type ());
170 gtk_label_set (label, str);
172 return GTK_WIDGET (label);
176 gtk_label_set (GtkLabel *label,
181 g_return_if_fail (label != NULL);
182 g_return_if_fail (GTK_IS_LABEL (label));
183 g_return_if_fail (str != NULL);
186 g_free (label->label);
187 label->label = g_strdup (str);
190 g_slist_free (label->row);
192 label->row = g_slist_append (label->row, label->label);
194 while ((p = strchr(p, '\n')))
195 label->row = g_slist_append (label->row, ++p);
197 if (GTK_WIDGET_VISIBLE (label))
199 if (GTK_WIDGET_MAPPED (label))
200 gdk_window_clear_area (GTK_WIDGET (label)->window,
201 GTK_WIDGET (label)->allocation.x,
202 GTK_WIDGET (label)->allocation.y,
203 GTK_WIDGET (label)->allocation.width,
204 GTK_WIDGET (label)->allocation.height);
206 gtk_widget_queue_resize (GTK_WIDGET (label));
211 gtk_label_set_justify (GtkLabel *label,
212 GtkJustification jtype)
214 g_return_if_fail (label != NULL);
215 g_return_if_fail (GTK_IS_LABEL (label));
217 if ((GtkJustification) label->jtype != jtype)
219 label->jtype = jtype;
221 if (GTK_WIDGET_VISIBLE (label))
223 if (GTK_WIDGET_MAPPED (label))
224 gdk_window_clear_area (GTK_WIDGET (label)->window,
225 GTK_WIDGET (label)->allocation.x,
226 GTK_WIDGET (label)->allocation.y,
227 GTK_WIDGET (label)->allocation.width,
228 GTK_WIDGET (label)->allocation.height);
230 gtk_widget_queue_resize (GTK_WIDGET (label));
236 gtk_label_get (GtkLabel *label,
239 g_return_if_fail (label != NULL);
240 g_return_if_fail (GTK_IS_LABEL (label));
241 g_return_if_fail (str != NULL);
248 gtk_label_finalize (GtkObject *object)
252 g_return_if_fail (object != NULL);
253 g_return_if_fail (GTK_IS_LABEL (object));
255 label = GTK_LABEL (object);
257 g_free (label->label);
258 g_slist_free (label->row);
260 (* GTK_OBJECT_CLASS (parent_class)->finalize) (object);
264 gtk_label_size_request (GtkWidget *widget,
265 GtkRequisition *requisition)
271 g_return_if_fail (widget != NULL);
272 g_return_if_fail (GTK_IS_LABEL (widget));
273 g_return_if_fail (requisition != NULL);
275 label = GTK_LABEL (widget);
283 gdk_text_width (GTK_WIDGET (label)->style->font,
285 (gchar*) row->next->data - (gchar*) row->data - 1));
287 width = MAX (width, gdk_string_width (GTK_WIDGET (label)->style->font, row->data));
291 label->max_width = width;
292 requisition->width = width + label->misc.xpad * 2;
293 requisition->height = ((GTK_WIDGET (label)->style->font->ascent +
294 GTK_WIDGET (label)->style->font->descent + 2) *
295 g_slist_length(label->row) +
296 label->misc.ypad * 2);
300 gtk_label_expose (GtkWidget *widget,
301 GdkEventExpose *event)
311 g_return_val_if_fail (widget != NULL, FALSE);
312 g_return_val_if_fail (GTK_IS_LABEL (widget), FALSE);
313 g_return_val_if_fail (event != NULL, FALSE);
315 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
317 label = GTK_LABEL (widget);
318 misc = GTK_MISC (widget);
320 state = widget->state;
325 gdk_gc_set_clip_rectangle (widget->style->white_gc, &event->area);
326 gdk_gc_set_clip_rectangle (widget->style->fg_gc[state], &event->area);
328 /* We clear the whole allocation here so that if a partial
329 * expose is triggered we don't just clear part and mess up
330 * when the queued redraw comes along. (There will always
331 * be a complete queued redraw when the needs_clear flag
334 if (label->needs_clear)
336 gdk_window_clear_area (widget->window,
337 widget->allocation.x,
338 widget->allocation.y,
339 widget->allocation.width,
340 widget->allocation.height);
342 label->needs_clear = FALSE;
345 x = widget->allocation.x + misc->xpad +
346 (widget->allocation.width - (label->max_width + label->misc.xpad * 2))
347 * misc->xalign + 0.5;
349 y = (widget->allocation.y * (1.0 - misc->yalign) +
350 (widget->allocation.y + widget->allocation.height -
351 (widget->requisition.height - misc->ypad * 2)) *
352 misc->yalign + widget->style->font->ascent) + 1.5;
355 while (row && row->next)
357 len = (gchar*) row->next->data - (gchar*) row->data - 1;
360 if (label->jtype == GTK_JUSTIFY_CENTER)
361 offset = (label->max_width - gdk_text_width (widget->style->font, row->data, len)) / 2;
363 else if (label->jtype == GTK_JUSTIFY_RIGHT)
364 offset = (label->max_width - gdk_text_width (widget->style->font, row->data, len));
366 if (state == GTK_STATE_INSENSITIVE)
367 gdk_draw_text (widget->window, widget->style->font,
368 widget->style->white_gc,
369 offset + x + 1, y + 1, row->data, len);
371 gdk_draw_text (widget->window, widget->style->font,
372 widget->style->fg_gc[state],
373 offset + x, y, row->data, len);
375 y += widget->style->font->ascent + widget->style->font->descent + 2;
379 * COMMENT: we can avoid gdk_text_width() calls here storing in label->row
380 * the widths of the rows calculated in gtk_label_set.
381 * Once we have a wrapping interface we can support GTK_JUSTIFY_FILL.
385 if (label->jtype == GTK_JUSTIFY_CENTER)
386 offset = (label->max_width - gdk_string_width (widget->style->font, row->data)) / 2;
388 else if (label->jtype == GTK_JUSTIFY_RIGHT)
389 offset = (label->max_width - gdk_string_width (widget->style->font, row->data));
391 if (state == GTK_STATE_INSENSITIVE)
392 gdk_draw_string (widget->window, widget->style->font,
393 widget->style->white_gc,
394 offset + x + 1, y + 1, row->data);
396 gdk_draw_string (widget->window, widget->style->font,
397 widget->style->fg_gc[state],
398 offset + x, y, row->data);
400 gdk_gc_set_clip_mask (widget->style->white_gc, NULL);
401 gdk_gc_set_clip_mask (widget->style->fg_gc[state], NULL);
408 gtk_label_state_changed (GtkWidget *widget,
409 guint previous_state)
411 if (GTK_WIDGET_DRAWABLE (widget))
412 GTK_LABEL (widget)->needs_clear = TRUE;
416 gtk_label_style_set (GtkWidget *widget,
417 GtkStyle *previous_style)
419 if (GTK_WIDGET_DRAWABLE (widget))
420 GTK_LABEL (widget)->needs_clear = TRUE;