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 Lesser 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 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser 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.
20 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
21 * file for a list of people on the GTK+ Team. See the ChangeLog
22 * files for a list of changes. These files are distributed with
23 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include "gdk/gdkkeysyms.h"
30 #include "gdk/gdki18n.h"
31 #include <pango/pango.h>
42 static void gtk_label_class_init (GtkLabelClass *klass);
43 static void gtk_label_init (GtkLabel *label);
44 static void gtk_label_set_arg (GtkObject *object,
47 static void gtk_label_get_arg (GtkObject *object,
50 static void gtk_label_finalize (GObject *object);
51 static void gtk_label_size_request (GtkWidget *widget,
52 GtkRequisition *requisition);
53 static void gtk_label_style_set (GtkWidget *widget,
54 GtkStyle *previous_style);
55 static void gtk_label_direction_changed (GtkWidget *widget,
56 GtkTextDirection previous_dir);
57 static gint gtk_label_expose (GtkWidget *widget,
58 GdkEventExpose *event);
60 static GtkMiscClass *parent_class = NULL;
63 gtk_label_get_type (void)
65 static GtkType label_type = 0;
69 static const GTypeInfo label_info =
71 sizeof (GtkLabelClass),
73 NULL, /* base_finalize */
74 (GClassInitFunc) gtk_label_class_init,
75 NULL, /* class_finalize */
76 NULL, /* class_data */
79 (GInstanceInitFunc) gtk_label_init,
82 label_type = g_type_register_static (GTK_TYPE_MISC, "GtkLabel", &label_info, 0);
89 gtk_label_class_init (GtkLabelClass *class)
91 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
92 GtkObjectClass *object_class;
93 GtkWidgetClass *widget_class;
95 object_class = (GtkObjectClass*) class;
96 widget_class = (GtkWidgetClass*) class;
98 parent_class = gtk_type_class (GTK_TYPE_MISC);
100 gtk_object_add_arg_type ("GtkLabel::label", GTK_TYPE_STRING, GTK_ARG_READWRITE, ARG_LABEL);
101 gtk_object_add_arg_type ("GtkLabel::pattern", GTK_TYPE_STRING, GTK_ARG_READWRITE, ARG_PATTERN);
102 gtk_object_add_arg_type ("GtkLabel::justify", GTK_TYPE_JUSTIFICATION, GTK_ARG_READWRITE, ARG_JUSTIFY);
103 gtk_object_add_arg_type ("GtkLabel::wrap", GTK_TYPE_BOOL, GTK_ARG_READWRITE, ARG_WRAP);
105 gobject_class->finalize = gtk_label_finalize;
107 object_class->set_arg = gtk_label_set_arg;
108 object_class->get_arg = gtk_label_get_arg;
110 widget_class->size_request = gtk_label_size_request;
111 widget_class->style_set = gtk_label_style_set;
112 widget_class->direction_changed = gtk_label_direction_changed;
113 widget_class->expose_event = gtk_label_expose;
117 gtk_label_set_arg (GtkObject *object,
123 label = GTK_LABEL (object);
128 gtk_label_set_text (label, GTK_VALUE_STRING (*arg));
131 gtk_label_set_pattern (label, GTK_VALUE_STRING (*arg));
134 gtk_label_set_justify (label, GTK_VALUE_ENUM (*arg));
137 gtk_label_set_line_wrap (label, GTK_VALUE_BOOL (*arg));
145 gtk_label_get_arg (GtkObject *object,
151 label = GTK_LABEL (object);
156 GTK_VALUE_STRING (*arg) = g_strdup (label->label);
159 GTK_VALUE_STRING (*arg) = g_strdup (label->pattern);
162 GTK_VALUE_ENUM (*arg) = label->jtype;
165 GTK_VALUE_BOOL (*arg) = label->wrap;
168 arg->type = GTK_TYPE_INVALID;
174 gtk_label_init (GtkLabel *label)
176 GTK_WIDGET_SET_FLAGS (label, GTK_NO_WINDOW);
179 label->pattern = NULL;
181 label->jtype = GTK_JUSTIFY_CENTER;
184 label->layout = NULL;
187 gtk_label_set_text (label, "");
191 gtk_label_new (const gchar *str)
195 label = gtk_type_new (GTK_TYPE_LABEL);
198 gtk_label_set_text (label, str);
200 return GTK_WIDGET (label);
204 gtk_label_set_text_internal (GtkLabel *label,
207 g_free (label->label);
212 g_object_unref (G_OBJECT (label->layout));
213 label->layout = NULL;
216 gtk_widget_queue_resize (GTK_WIDGET (label));
220 gtk_label_set_text (GtkLabel *label,
223 g_return_if_fail (GTK_IS_LABEL (label));
225 gtk_label_set_text_internal (label, g_strdup (str ? str : ""));
229 * gtk_label_set_attributes:
230 * @label: a #GtkLabel
231 * @attrs: a #PangoAttrList
233 * Sets a #PangoAttrList; the attributes in the list are applied to the
237 gtk_label_set_attributes (GtkLabel *label,
238 PangoAttrList *attrs)
240 g_return_if_fail (GTK_IS_LABEL (label));
243 pango_attr_list_ref (attrs);
246 pango_attr_list_unref (label->attrs);
248 label->attrs = attrs;
252 set_markup (GtkLabel *label,
257 GError *error = NULL;
258 PangoAttrList *attrs = NULL;
259 gunichar accel_char = 0;
264 if (!pango_parse_markup (str,
266 with_uline ? '_' : 0,
269 with_uline ? &accel_char : NULL,
272 g_warning ("Failed to set label from markup due to error parsing markup: %s",
274 g_error_free (error);
279 gtk_label_set_text (label, text);
283 gtk_label_set_attributes (label, attrs);
284 pango_attr_list_unref (attrs);
288 return gdk_keyval_to_lower (gdk_unicode_to_keyval (accel_char));
290 return GDK_VoidSymbol;
294 * gtk_label_set_markup:
295 * @label: a #GtkLabel
296 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
298 * Parses @str which is marked up with the Pango text markup language,
299 * setting the label's text and attribute list based on the parse results.
302 gtk_label_set_markup (GtkLabel *label,
305 g_return_if_fail (GTK_IS_LABEL (label));
307 set_markup (label, str, FALSE);
311 * gtk_label_set_markup:
312 * @label: a #GtkLabel
313 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
315 * Parses @str which is marked up with the Pango text markup language,
316 * setting the label's text and attribute list based on the parse results.
317 * If characters in @str are preceded by an underscore, they are underlined
318 * indicating that they represent a keyboard accelerator, and the GDK
319 * keyval for the first underlined accelerator is returned. If there are
320 * no underlines in the text, GDK_VoidSymbol will be returned.
323 gtk_label_set_markup_with_accel (GtkLabel *label,
326 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_VoidSymbol);
328 return set_markup (label, str, TRUE);
332 * gtk_label_get_text:
333 * @label: a #GtkLabel
335 * Fetches the text from a label widget
337 * Return value: the text in the label widget. This value must
338 * be freed with g_free().
341 gtk_label_get_text (GtkLabel *label)
343 g_return_val_if_fail (label != NULL, NULL);
344 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
346 return g_strdup (label->label);
350 gtk_label_set_pattern (GtkLabel *label,
351 const gchar *pattern)
353 g_return_if_fail (GTK_IS_LABEL (label));
355 g_free (label->pattern);
356 label->pattern = g_strdup (pattern);
358 gtk_widget_queue_resize (GTK_WIDGET (label));
362 gtk_label_set_justify (GtkLabel *label,
363 GtkJustification jtype)
365 g_return_if_fail (GTK_IS_LABEL (label));
366 g_return_if_fail (jtype >= GTK_JUSTIFY_LEFT && jtype <= GTK_JUSTIFY_FILL);
368 if ((GtkJustification) label->jtype != jtype)
370 label->jtype = jtype;
374 /* No real need to be this drastic, but easier than duplicating the code */
375 g_object_unref (G_OBJECT (label->layout));
376 label->layout = NULL;
379 gtk_widget_queue_resize (GTK_WIDGET (label));
384 gtk_label_set_line_wrap (GtkLabel *label,
387 g_return_if_fail (GTK_IS_LABEL (label));
389 wrap = wrap != FALSE;
391 if (label->wrap != wrap)
395 gtk_widget_queue_resize (GTK_WIDGET (label));
400 gtk_label_get (GtkLabel *label,
403 g_return_if_fail (label != NULL);
404 g_return_if_fail (GTK_IS_LABEL (label));
405 g_return_if_fail (str != NULL);
411 gtk_label_finalize (GObject *object)
415 g_return_if_fail (GTK_IS_LABEL (object));
417 label = GTK_LABEL (object);
419 g_free (label->label);
420 g_free (label->pattern);
423 g_object_unref (G_OBJECT (label->layout));
426 pango_attr_list_unref (label->attrs);
428 G_OBJECT_CLASS (parent_class)->finalize (object);
432 gtk_label_pattern_to_attrs (GtkLabel *label,
433 PangoAttrList *attrs)
438 const char *p = label->label;
439 const char *q = label->pattern;
443 while (*p && *q && *q != '_')
445 p = g_utf8_next_char (p);
449 while (*p && *q && *q == '_')
451 p = g_utf8_next_char (p);
457 PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_LOW);
458 attr->start_index = start - label->label;
459 attr->end_index = p - label->label;
461 pango_attr_list_insert (attrs, attr);
470 gtk_label_size_request (GtkWidget *widget,
471 GtkRequisition *requisition)
474 PangoRectangle logical_rect;
476 g_return_if_fail (GTK_IS_LABEL (widget));
477 g_return_if_fail (requisition != NULL);
479 label = GTK_LABEL (widget);
482 * There are a number of conditions which will necessitate re-filling
486 * 2. justification changed either from to to GTK_JUSTIFY_FILL.
489 * These have been detected elsewhere, and label->words will be zero,
490 * if one of the above has occured.
492 * Additionally, though, if GTK_JUSTIFY_FILL, we need to re-fill if:
494 * 4. gtk_widget_set_usize has changed the requested width.
495 * 5. gtk_misc_set_padding has changed xpad.
496 * 6. maybe others?...
498 * Too much of a pain to detect all these case, so always re-fill. I
499 * don't think it's really that slow.
502 requisition->width = label->misc.xpad;
503 requisition->height = label->misc.ypad;
507 PangoAlignment align = PANGO_ALIGN_LEFT; /* Quiet gcc */
508 PangoAttrList *attrs = NULL;
510 label->layout = gtk_widget_create_pango_layout (widget, label->label);
512 /* FIXME move to a model where the pattern isn't stored
513 * permanently, and just modifes or creates the AttrList
516 attrs = pango_attr_list_copy (label->attrs);
517 else if (label->pattern)
518 attrs = pango_attr_list_new ();
521 gtk_label_pattern_to_attrs (label, attrs);
525 pango_layout_set_attributes (label->layout, attrs);
526 pango_attr_list_unref (attrs);
529 switch (label->jtype)
531 case GTK_JUSTIFY_LEFT:
532 align = PANGO_ALIGN_LEFT;
534 case GTK_JUSTIFY_RIGHT:
535 align = PANGO_ALIGN_RIGHT;
537 case GTK_JUSTIFY_CENTER:
538 align = PANGO_ALIGN_LEFT;
540 case GTK_JUSTIFY_FILL:
541 /* FIXME: This just doesn't work to do this */
542 align = PANGO_ALIGN_LEFT;
543 pango_layout_set_justify (label->layout, TRUE);
546 g_assert_not_reached();
549 pango_layout_set_alignment (label->layout, align);
554 GtkWidgetAuxInfo *aux_info;
555 gint longest_paragraph;
559 aux_info = gtk_object_get_data (GTK_OBJECT (widget), "gtk-aux-info");
560 if (aux_info && aux_info->width > 0)
562 pango_layout_set_width (label->layout, aux_info->width * PANGO_SCALE);
563 pango_layout_get_extents (label->layout, NULL, &logical_rect);
565 requisition->width += aux_info->width;
566 requisition->height += PANGO_PIXELS (logical_rect.height);
570 pango_layout_set_width (label->layout, -1);
571 pango_layout_get_extents (label->layout, NULL, &logical_rect);
573 width = logical_rect.width;
574 height = logical_rect.height;
576 /* Try to guess a reasonable maximum width
578 longest_paragraph = width;
581 PANGO_SCALE * gdk_string_width (GTK_WIDGET (label)->style->font,
582 "This long string gives a good enough length for any line to have."));
584 PANGO_SCALE * (gdk_screen_width () + 1) / 2);
586 pango_layout_set_width (label->layout, width);
587 pango_layout_get_extents (label->layout, NULL, &logical_rect);
588 real_width = logical_rect.width;
589 height = logical_rect.height;
591 /* Unfortunately, the above may leave us with a very unbalanced looking paragraph,
592 * so we try short search for a narrower width that leaves us with the same height
594 if (longest_paragraph > 0)
596 gint nlines, perfect_width;
598 nlines = pango_layout_get_line_count (label->layout);
599 perfect_width = (longest_paragraph + nlines - 1) / nlines;
601 if (perfect_width < width)
603 pango_layout_set_width (label->layout, perfect_width);
604 pango_layout_get_extents (label->layout, NULL, &logical_rect);
606 if (logical_rect.height <= height)
608 width = perfect_width;
609 real_width = logical_rect.width;
610 height = logical_rect.height;
614 gint mid_width = (perfect_width + width) / 2;
616 if (mid_width > perfect_width)
618 pango_layout_set_width (label->layout, mid_width);
619 pango_layout_get_extents (label->layout, NULL, &logical_rect);
621 if (logical_rect.height <= height)
624 real_width = logical_rect.width;
625 height = logical_rect.height;
631 pango_layout_set_width (label->layout, width);
633 requisition->width += PANGO_PIXELS (real_width);
634 requisition->height += PANGO_PIXELS (height);
637 else /* !label->wrap */
639 pango_layout_set_width (label->layout, -1);
640 pango_layout_get_extents (label->layout, NULL, &logical_rect);
642 requisition->width += PANGO_PIXELS (logical_rect.width);
643 requisition->height += PANGO_PIXELS (logical_rect.height);
648 gtk_label_style_set (GtkWidget *widget,
649 GtkStyle *previous_style)
653 g_return_if_fail (GTK_IS_LABEL (widget));
655 label = GTK_LABEL (widget);
657 if (previous_style && label->layout)
658 pango_layout_context_changed (label->layout);
662 gtk_label_direction_changed (GtkWidget *widget,
663 GtkTextDirection previous_dir)
665 GtkLabel *label = GTK_LABEL (widget);
668 pango_layout_context_changed (label->layout);
670 GTK_WIDGET_CLASS (parent_class)->direction_changed (widget, previous_dir);
675 gtk_label_paint_word (GtkLabel *label,
681 GtkWidget *widget = GTK_WIDGET (label);
682 GtkLabelULine *uline;
685 tmp_str = gdk_wcstombs (word->beginning);
688 gtk_paint_string (widget->style, widget->window, widget->state,
689 area, widget, "label",
696 for (uline = word->uline; uline; uline = uline->next)
697 gtk_paint_hline (widget->style, widget->window,
700 x + uline->x1, x + uline->x2, y + uline->y);
705 gtk_label_expose (GtkWidget *widget,
706 GdkEventExpose *event)
713 g_return_val_if_fail (GTK_IS_LABEL (widget), FALSE);
714 g_return_val_if_fail (event != NULL, FALSE);
716 label = GTK_LABEL (widget);
718 /* if label->layout is NULL it means we got a set_text since
719 * our last size request, so a resize should be queued,
720 * which means a full expose is in the queue anyway.
722 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget) &&
723 label->layout && label->label && (*label->label != '\0'))
725 misc = GTK_MISC (widget);
727 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
728 xalign = misc->xalign;
730 xalign = 1. - misc->xalign;
732 x = floor (widget->allocation.x + (gint)misc->xpad
733 + ((widget->allocation.width - widget->requisition.width) * xalign)
736 y = floor (widget->allocation.y + (gint)misc->ypad
737 + ((widget->allocation.height - widget->requisition.height) * misc->yalign)
741 gtk_paint_layout (widget->style,
743 GTK_WIDGET_STATE (widget),
755 gtk_label_parse_uline (GtkLabel *label,
758 guint accel_key = GDK_VoidSymbol;
763 gchar *dest, *pattern_dest;
766 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_VoidSymbol);
767 g_return_val_if_fail (str != NULL, GDK_VoidSymbol);
769 /* Convert text to wide characters */
771 new_str = g_new (gchar, strlen (str) + 1);
772 pattern = g_new (gchar, g_utf8_strlen (str, -1) + 1);
778 pattern_dest = pattern;
785 c = g_utf8_get_char (src);
786 if (c == (gunichar)-1)
788 g_warning ("Invalid input string");
791 return GDK_VoidSymbol;
793 next_src = g_utf8_next_char (src);
798 *pattern_dest++ = ' ';
801 *pattern_dest++ = '_';
802 if (accel_key == GDK_VoidSymbol)
803 accel_key = gdk_keyval_to_lower (c);
806 while (src < next_src)
820 while (src < next_src)
823 *pattern_dest++ = ' ';
830 gtk_label_set_text_internal (label, new_str);
831 gtk_label_set_pattern (label, pattern);