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
517 attrs = pango_attr_list_copy (label->attrs);
518 pango_layout_set_attributes (label->layout, attrs);
521 attrs = pango_attr_list_new ();
523 gtk_label_pattern_to_attrs (label, attrs);
524 pango_attr_list_unref (attrs);
526 switch (label->jtype)
528 case GTK_JUSTIFY_LEFT:
529 align = PANGO_ALIGN_LEFT;
531 case GTK_JUSTIFY_RIGHT:
532 align = PANGO_ALIGN_RIGHT;
534 case GTK_JUSTIFY_CENTER:
535 align = PANGO_ALIGN_LEFT;
537 case GTK_JUSTIFY_FILL:
538 /* FIXME: This just doesn't work to do this */
539 align = PANGO_ALIGN_LEFT;
540 pango_layout_set_justify (label->layout, TRUE);
543 g_assert_not_reached();
546 pango_layout_set_alignment (label->layout, align);
551 GtkWidgetAuxInfo *aux_info;
552 gint longest_paragraph;
556 aux_info = gtk_object_get_data (GTK_OBJECT (widget), "gtk-aux-info");
557 if (aux_info && aux_info->width > 0)
559 pango_layout_set_width (label->layout, aux_info->width * PANGO_SCALE);
560 pango_layout_get_extents (label->layout, NULL, &logical_rect);
562 requisition->width += aux_info->width;
563 requisition->height += PANGO_PIXELS (logical_rect.height);
567 pango_layout_set_width (label->layout, -1);
568 pango_layout_get_extents (label->layout, NULL, &logical_rect);
570 width = logical_rect.width;
571 height = logical_rect.height;
573 /* Try to guess a reasonable maximum width
575 longest_paragraph = width;
578 PANGO_SCALE * gdk_string_width (GTK_WIDGET (label)->style->font,
579 "This long string gives a good enough length for any line to have."));
581 PANGO_SCALE * (gdk_screen_width () + 1) / 2);
583 pango_layout_set_width (label->layout, width);
584 pango_layout_get_extents (label->layout, NULL, &logical_rect);
585 real_width = logical_rect.width;
586 height = logical_rect.height;
588 /* Unfortunately, the above may leave us with a very unbalanced looking paragraph,
589 * so we try short search for a narrower width that leaves us with the same height
591 if (longest_paragraph > 0)
593 gint nlines, perfect_width;
595 nlines = pango_layout_get_line_count (label->layout);
596 perfect_width = (longest_paragraph + nlines - 1) / nlines;
598 if (perfect_width < width)
600 pango_layout_set_width (label->layout, perfect_width);
601 pango_layout_get_extents (label->layout, NULL, &logical_rect);
603 if (logical_rect.height <= height)
605 width = perfect_width;
606 real_width = logical_rect.width;
607 height = logical_rect.height;
611 gint mid_width = (perfect_width + width) / 2;
613 if (mid_width > perfect_width)
615 pango_layout_set_width (label->layout, mid_width);
616 pango_layout_get_extents (label->layout, NULL, &logical_rect);
618 if (logical_rect.height <= height)
621 real_width = logical_rect.width;
622 height = logical_rect.height;
628 pango_layout_set_width (label->layout, width);
630 requisition->width += PANGO_PIXELS (real_width);
631 requisition->height += PANGO_PIXELS (height);
634 else /* !label->wrap */
636 pango_layout_set_width (label->layout, -1);
637 pango_layout_get_extents (label->layout, NULL, &logical_rect);
639 requisition->width += PANGO_PIXELS (logical_rect.width);
640 requisition->height += PANGO_PIXELS (logical_rect.height);
645 gtk_label_style_set (GtkWidget *widget,
646 GtkStyle *previous_style)
650 g_return_if_fail (GTK_IS_LABEL (widget));
652 label = GTK_LABEL (widget);
654 if (previous_style && label->layout)
655 pango_layout_context_changed (label->layout);
659 gtk_label_direction_changed (GtkWidget *widget,
660 GtkTextDirection previous_dir)
662 GtkLabel *label = GTK_LABEL (widget);
665 pango_layout_context_changed (label->layout);
667 GTK_WIDGET_CLASS (parent_class)->direction_changed (widget, previous_dir);
672 gtk_label_paint_word (GtkLabel *label,
678 GtkWidget *widget = GTK_WIDGET (label);
679 GtkLabelULine *uline;
682 tmp_str = gdk_wcstombs (word->beginning);
685 gtk_paint_string (widget->style, widget->window, widget->state,
686 area, widget, "label",
693 for (uline = word->uline; uline; uline = uline->next)
694 gtk_paint_hline (widget->style, widget->window,
697 x + uline->x1, x + uline->x2, y + uline->y);
702 gtk_label_expose (GtkWidget *widget,
703 GdkEventExpose *event)
710 g_return_val_if_fail (GTK_IS_LABEL (widget), FALSE);
711 g_return_val_if_fail (event != NULL, FALSE);
713 label = GTK_LABEL (widget);
715 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget) &&
716 label->label && (*label->label != '\0'))
718 misc = GTK_MISC (widget);
720 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
721 xalign = misc->xalign;
723 xalign = 1. - misc->xalign;
725 x = floor (widget->allocation.x + (gint)misc->xpad
726 + ((widget->allocation.width - widget->requisition.width) * xalign)
729 y = floor (widget->allocation.y + (gint)misc->ypad
730 + ((widget->allocation.height - widget->requisition.height) * misc->yalign)
734 gtk_paint_layout (widget->style,
736 GTK_WIDGET_STATE (widget),
748 gtk_label_parse_uline (GtkLabel *label,
751 guint accel_key = GDK_VoidSymbol;
756 gchar *dest, *pattern_dest;
759 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_VoidSymbol);
760 g_return_val_if_fail (str != NULL, GDK_VoidSymbol);
762 /* Convert text to wide characters */
764 new_str = g_new (gchar, strlen (str) + 1);
765 pattern = g_new (gchar, g_utf8_strlen (str, -1) + 1);
771 pattern_dest = pattern;
778 c = g_utf8_get_char (src);
779 if (c == (gunichar)-1)
781 g_warning ("Invalid input string");
784 return GDK_VoidSymbol;
786 next_src = g_utf8_next_char (src);
791 *pattern_dest++ = ' ';
794 *pattern_dest++ = '_';
795 if (accel_key == GDK_VoidSymbol)
796 accel_key = gdk_keyval_to_lower (c);
799 while (src < next_src)
813 while (src < next_src)
816 *pattern_dest++ = ' ';
823 gtk_label_set_text_internal (label, new_str);
824 gtk_label_set_pattern (label, pattern);