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/.
32 #include "gtkaccellabel.h"
34 #include "gtkmainprivate.h"
35 #include "gtkmarshalers.h"
37 #include "gtkwindow.h"
38 #include "gtkclipboard.h"
39 #include "gtkimagemenuitem.h"
41 #include "gtkseparatormenuitem.h"
42 #include "gtktextutil.h"
43 #include "gtkmenuitem.h"
44 #include "gtkmenushellprivate.h"
45 #include "gtknotebook.h"
47 #include "gtkbindings.h"
48 #include "gtkbuildable.h"
51 #include "gtktooltip.h"
52 #include "gtkprivate.h"
53 #include "gtktypebuiltins.h"
55 #include "a11y/gtklabelaccessible.h"
59 * @Short_description: A widget that displays a small to medium amount of text
62 * The #GtkLabel widget displays a small amount of text. As the name
63 * implies, most labels are used to label another widget such as a
64 * #GtkButton, a #GtkMenuItem, or a #GtkOptionMenu.
66 * <refsect2 id="GtkLabel-BUILDER-UI">
67 * <title>GtkLabel as GtkBuildable</title>
69 * The GtkLabel implementation of the GtkBuildable interface supports a
70 * custom <attributes> element, which supports any number of <attribute>
71 * elements. the <attribute> element has attributes named name, value,
72 * start and end and allows you to specify #PangoAttribute values for this label.
75 * <title>A UI definition fragment specifying Pango attributes</title>
76 * <programlisting><![CDATA[
77 * <object class="GtkLabel">
79 * <attribute name="weight" value="PANGO_WEIGHT_BOLD"/>
80 * <attribute name="background" value="red" start="5" end="10"/>"
83 * ]]></programlisting>
85 * The start and end attributes specify the range of characters to which the
86 * Pango attribute applies. If start and end are not specified, the attribute is
87 * applied to the whole text. Note that specifying ranges does not make much
88 * sense with translatable attributes. Use markup embedded in the translatable
93 * <title>Mnemonics</title>
95 * Labels may contain <firstterm>mnemonics</firstterm>. Mnemonics are
96 * underlined characters in the label, used for keyboard navigation.
97 * Mnemonics are created by providing a string with an underscore before
98 * the mnemonic character, such as <literal>"_File"</literal>, to the
99 * functions gtk_label_new_with_mnemonic() or
100 * gtk_label_set_text_with_mnemonic().
102 * Mnemonics automatically activate any activatable widget the label is
103 * inside, such as a #GtkButton; if the label is not inside the
104 * mnemonic's target widget, you have to tell the label about the target
105 * using gtk_label_set_mnemonic_widget(). Here's a simple example where
106 * the label is inside a button:
110 * // Pressing Alt+H will activate this button
111 * button = gtk_button_new (<!-- -->);
112 * label = gtk_label_new_with_mnemonic ("_Hello");
113 * gtk_container_add (GTK_CONTAINER (button), label);
117 * There's a convenience function to create buttons with a mnemonic label
122 * // Pressing Alt+H will activate this button
123 * button = gtk_button_new_with_mnemonic ("_Hello");
127 * To create a mnemonic for a widget alongside the label, such as a
128 * #GtkEntry, you have to point the label at the entry with
129 * gtk_label_set_mnemonic_widget():
133 * // Pressing Alt+H will focus the entry
134 * entry = gtk_entry_new (<!-- -->);
135 * label = gtk_label_new_with_mnemonic ("_Hello");
136 * gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
142 * <title>Markup (styled text)</title>
144 * To make it easy to format text in a label (changing colors, fonts,
145 * etc.), label text can be provided in a simple <link
146 * linkend="PangoMarkupFormat">markup format</link>.
147 * Here's how to create a label with a small font:
151 * label = gtk_label_new (NULL);
152 * gtk_label_set_markup (GTK_LABEL (label), "<small>Small text</small>");
157 * linkend="PangoMarkupFormat">complete documentation</link> of available
158 * tags in the Pango manual.)
160 * The markup passed to gtk_label_set_markup() must be valid; for example,
161 * literal <, > and & characters must be escaped as \<,
162 * \gt;, and \&. If you pass text obtained from the user, file,
163 * or a network to gtk_label_set_markup(), you'll want to escape it with
164 * g_markup_escape_text() or g_markup_printf_escaped().
166 * Markup strings are just a convenient way to set the #PangoAttrList on
167 * a label; gtk_label_set_attributes() may be a simpler way to set
168 * attributes in some cases. Be careful though; #PangoAttrList tends to
169 * cause internationalization problems, unless you're applying attributes
170 * to the entire string (i.e. unless you set the range of each attribute
171 * to [0, %G_MAXINT)). The reason is that specifying the start_index and
172 * end_index for a #PangoAttribute requires knowledge of the exact string
173 * being displayed, so translations will cause problems.
177 * <title>Selectable labels</title>
178 * Labels can be made selectable with gtk_label_set_selectable().
179 * Selectable labels allow the user to copy the label contents to
180 * the clipboard. Only labels that contain useful-to-copy information
181 * — such as error messages — should be made selectable.
183 * <refsect2 id="label-text-layout">
184 * <title>Text layout</title>
186 * A label can contain any number of paragraphs, but will have
187 * performance problems if it contains more than a small number.
188 * Paragraphs are separated by newlines or other paragraph separators
189 * understood by Pango.
191 * Labels can automatically wrap text if you call
192 * gtk_label_set_line_wrap().
194 * gtk_label_set_justify() sets how the lines in a label align
195 * with one another. If you want to set how the label as a whole
196 * aligns in its available space, see gtk_misc_set_alignment().
198 * The #GtkLabel:width-chars and #GtkLabel:max-width-chars properties
199 * can be used to control the size allocation of ellipsized or wrapped
200 * labels. For ellipsizing labels, if either is specified (and less
201 * than the actual text size), it is used as the minimum width, and the actual
202 * text size is used as the natural width of the label. For wrapping labels,
203 * width-chars is used as the minimum width, if specified, and max-width-chars
204 * is used as the natural width. Even if max-width-chars specified, wrapping
205 * labels will be rewrapped to use all of the available width.
208 * Note that the interpretation of #GtkLabel:width-chars and
209 * #GtkLabel:max-width-chars has changed a bit with the introduction of
210 * <link linkend="geometry-management">width-for-height geometry management.</link>
215 * <title>Links</title>
217 * Since 2.18, GTK+ supports markup for clickable hyperlinks in addition
218 * to regular Pango markup. The markup for links is borrowed from HTML, using the
219 * <tag>a</tag> with href and title attributes. GTK+ renders links similar to the
220 * way they appear in web browsers, with colored, underlined text. The title
221 * attribute is displayed as a tooltip on the link. An example looks like this:
223 * <informalexample><programlisting>
224 * gtk_label_set_markup (label, "Go to the <a href="http://www.gtk.org" title="<i>Our</i> website">GTK+ website</a> for more...");
225 * </programlisting></informalexample>
227 * It is possible to implement custom handling for links and their tooltips with
228 * the #GtkLabel::activate-link signal and the gtk_label_get_current_uri() function.
234 /*rint() is only available in GCC and/or C99*/
235 #if (__STDC_VERSION__ < 199901L && !defined __GNUC__)
236 double rint(double x)
238 if (ceil(x+0.5) == floor(x+0.5))
240 int a = (int)ceil(x);
253 struct _GtkLabelPrivate
255 GtkLabelSelectionInfo *select_info;
256 GtkWidget *mnemonic_widget;
257 GtkWindow *mnemonic_window;
259 PangoAttrList *attrs;
260 PangoAttrList *effective_attrs;
268 guint mnemonics_visible : 1;
271 guint use_underline : 1;
272 guint use_markup : 1;
274 guint single_line_mode : 1;
275 guint have_transform : 1;
278 guint pattern_set : 1;
279 guint track_links : 1;
281 guint mnemonic_keyval;
284 gint max_width_chars;
287 /* Notes about the handling of links:
289 * Links share the GtkLabelSelectionInfo struct with selectable labels.
290 * There are some new fields for links. The links field contains the list
291 * of GtkLabelLink structs that describe the links which are embedded in
292 * the label. The active_link field points to the link under the mouse
293 * pointer. For keyboard navigation, the 'focus' link is determined by
294 * finding the link which contains the selection_anchor position.
295 * The link_clicked field is used with button press and release events
296 * to ensure that pressing inside a link and releasing outside of it
297 * does not activate the link.
299 * Links are rendered with the link-color/visited-link-color colors
300 * that are determined by the style and with an underline. When the mouse
301 * pointer is over a link, the pointer is changed to indicate the link,
302 * and the background behind the link is rendered with the base[PRELIGHT]
303 * color. While a button is pressed over a link, the background is rendered
304 * with the base[ACTIVE] color.
306 * Labels with links accept keyboard focus, and it is possible to move
307 * the focus between the embedded links using Tab/Shift-Tab. The focus
308 * is indicated by a focus rectangle that is drawn around the link text.
309 * Pressing Enter activates the focussed link, and there is a suitable
310 * context menu for links that can be opened with the Menu key. Pressing
311 * Control-C copies the link URI to the clipboard.
313 * In selectable labels with links, link functionality is only available
314 * when the selection is empty.
319 gchar *title; /* the title attribute, used as tooltip */
320 gboolean visited; /* get set when the link is activated; this flag
321 * gets preserved over later set_markup() calls
323 gint start; /* position of the link in the PangoLayout */
327 struct _GtkLabelSelectionInfo
330 gint selection_anchor;
332 GtkWidget *popup_menu;
335 GtkLabelLink *active_link;
341 guint select_words : 1;
342 guint selectable : 1;
343 guint link_clicked : 1;
351 ACTIVATE_CURRENT_LINK,
366 PROP_MNEMONIC_KEYVAL,
367 PROP_MNEMONIC_WIDGET,
368 PROP_CURSOR_POSITION,
369 PROP_SELECTION_BOUND,
372 PROP_SINGLE_LINE_MODE,
374 PROP_MAX_WIDTH_CHARS,
375 PROP_TRACK_VISITED_LINKS
378 /* When rotating ellipsizable text we want the natural size to request
379 * more to ensure the label wont ever ellipsize in an allocation of full natural size.
381 #define ROTATION_ELLIPSIZE_PADDING 2
383 static guint signals[LAST_SIGNAL] = { 0 };
385 static const GdkColor default_link_color = { 0, 0, 0, 0xeeee };
386 static const GdkColor default_visited_link_color = { 0, 0x5555, 0x1a1a, 0x8b8b };
388 static void gtk_label_set_property (GObject *object,
392 static void gtk_label_get_property (GObject *object,
396 static void gtk_label_finalize (GObject *object);
397 static void gtk_label_destroy (GtkWidget *widget);
398 static void gtk_label_size_allocate (GtkWidget *widget,
399 GtkAllocation *allocation);
400 static void gtk_label_state_changed (GtkWidget *widget,
402 static void gtk_label_style_updated (GtkWidget *widget);
403 static void gtk_label_direction_changed (GtkWidget *widget,
404 GtkTextDirection previous_dir);
405 static gint gtk_label_draw (GtkWidget *widget,
407 static gboolean gtk_label_focus (GtkWidget *widget,
408 GtkDirectionType direction);
410 static void gtk_label_realize (GtkWidget *widget);
411 static void gtk_label_unrealize (GtkWidget *widget);
412 static void gtk_label_map (GtkWidget *widget);
413 static void gtk_label_unmap (GtkWidget *widget);
415 static gboolean gtk_label_button_press (GtkWidget *widget,
416 GdkEventButton *event);
417 static gboolean gtk_label_button_release (GtkWidget *widget,
418 GdkEventButton *event);
419 static gboolean gtk_label_motion (GtkWidget *widget,
420 GdkEventMotion *event);
421 static gboolean gtk_label_leave_notify (GtkWidget *widget,
422 GdkEventCrossing *event);
424 static void gtk_label_grab_focus (GtkWidget *widget);
426 static gboolean gtk_label_query_tooltip (GtkWidget *widget,
429 gboolean keyboard_tip,
430 GtkTooltip *tooltip);
432 static void gtk_label_set_text_internal (GtkLabel *label,
434 static void gtk_label_set_label_internal (GtkLabel *label,
436 static void gtk_label_set_use_markup_internal (GtkLabel *label,
438 static void gtk_label_set_use_underline_internal (GtkLabel *label,
440 static void gtk_label_set_attributes_internal (GtkLabel *label,
441 PangoAttrList *attrs);
442 static void gtk_label_set_uline_text_internal (GtkLabel *label,
444 static void gtk_label_set_pattern_internal (GtkLabel *label,
445 const gchar *pattern,
446 gboolean is_mnemonic);
447 static void gtk_label_set_markup_internal (GtkLabel *label,
449 gboolean with_uline);
450 static void gtk_label_recalculate (GtkLabel *label);
451 static void gtk_label_hierarchy_changed (GtkWidget *widget,
452 GtkWidget *old_toplevel);
453 static void gtk_label_screen_changed (GtkWidget *widget,
454 GdkScreen *old_screen);
455 static gboolean gtk_label_popup_menu (GtkWidget *widget);
457 static void gtk_label_create_window (GtkLabel *label);
458 static void gtk_label_destroy_window (GtkLabel *label);
459 static void gtk_label_ensure_select_info (GtkLabel *label);
460 static void gtk_label_clear_select_info (GtkLabel *label);
461 static void gtk_label_update_cursor (GtkLabel *label);
462 static void gtk_label_clear_layout (GtkLabel *label);
463 static void gtk_label_ensure_layout (GtkLabel *label);
464 static void gtk_label_select_region_index (GtkLabel *label,
469 static gboolean gtk_label_mnemonic_activate (GtkWidget *widget,
470 gboolean group_cycling);
471 static void gtk_label_setup_mnemonic (GtkLabel *label,
473 static void gtk_label_drag_data_get (GtkWidget *widget,
474 GdkDragContext *context,
475 GtkSelectionData *selection_data,
479 static void gtk_label_buildable_interface_init (GtkBuildableIface *iface);
480 static gboolean gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
483 const gchar *tagname,
484 GMarkupParser *parser,
487 static void gtk_label_buildable_custom_finished (GtkBuildable *buildable,
490 const gchar *tagname,
494 static void connect_mnemonics_visible_notify (GtkLabel *label);
495 static gboolean separate_uline_pattern (const gchar *str,
501 /* For selectable labels: */
502 static void gtk_label_move_cursor (GtkLabel *label,
503 GtkMovementStep step,
505 gboolean extend_selection);
506 static void gtk_label_copy_clipboard (GtkLabel *label);
507 static void gtk_label_select_all (GtkLabel *label);
508 static void gtk_label_do_popup (GtkLabel *label,
509 GdkEventButton *event);
510 static gint gtk_label_move_forward_word (GtkLabel *label,
512 static gint gtk_label_move_backward_word (GtkLabel *label,
516 static void gtk_label_rescan_links (GtkLabel *label);
517 static void gtk_label_clear_links (GtkLabel *label);
518 static gboolean gtk_label_activate_link (GtkLabel *label,
520 static void gtk_label_activate_current_link (GtkLabel *label);
521 static GtkLabelLink *gtk_label_get_current_link (GtkLabel *label);
522 static void gtk_label_get_link_colors (GtkWidget *widget,
523 GdkColor **link_color,
524 GdkColor **visited_link_color);
525 static void emit_activate_link (GtkLabel *label,
528 static GtkSizeRequestMode gtk_label_get_request_mode (GtkWidget *widget);
529 static void gtk_label_get_preferred_width (GtkWidget *widget,
532 static void gtk_label_get_preferred_height (GtkWidget *widget,
535 static void gtk_label_get_preferred_width_for_height (GtkWidget *widget,
538 gint *natural_width);
539 static void gtk_label_get_preferred_height_for_width (GtkWidget *widget,
541 gint *minimum_height,
542 gint *natural_height);
544 static GtkBuildableIface *buildable_parent_iface = NULL;
546 G_DEFINE_TYPE_WITH_CODE (GtkLabel, gtk_label, GTK_TYPE_MISC,
547 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
548 gtk_label_buildable_interface_init))
551 add_move_binding (GtkBindingSet *binding_set,
554 GtkMovementStep step,
557 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
559 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
563 G_TYPE_BOOLEAN, FALSE);
565 /* Selection-extending version */
566 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
570 G_TYPE_BOOLEAN, TRUE);
574 gtk_label_class_init (GtkLabelClass *class)
576 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
577 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
578 GtkBindingSet *binding_set;
580 gobject_class->set_property = gtk_label_set_property;
581 gobject_class->get_property = gtk_label_get_property;
582 gobject_class->finalize = gtk_label_finalize;
584 widget_class->destroy = gtk_label_destroy;
585 widget_class->size_allocate = gtk_label_size_allocate;
586 widget_class->state_changed = gtk_label_state_changed;
587 widget_class->style_updated = gtk_label_style_updated;
588 widget_class->query_tooltip = gtk_label_query_tooltip;
589 widget_class->direction_changed = gtk_label_direction_changed;
590 widget_class->draw = gtk_label_draw;
591 widget_class->realize = gtk_label_realize;
592 widget_class->unrealize = gtk_label_unrealize;
593 widget_class->map = gtk_label_map;
594 widget_class->unmap = gtk_label_unmap;
595 widget_class->button_press_event = gtk_label_button_press;
596 widget_class->button_release_event = gtk_label_button_release;
597 widget_class->motion_notify_event = gtk_label_motion;
598 widget_class->leave_notify_event = gtk_label_leave_notify;
599 widget_class->hierarchy_changed = gtk_label_hierarchy_changed;
600 widget_class->screen_changed = gtk_label_screen_changed;
601 widget_class->mnemonic_activate = gtk_label_mnemonic_activate;
602 widget_class->drag_data_get = gtk_label_drag_data_get;
603 widget_class->grab_focus = gtk_label_grab_focus;
604 widget_class->popup_menu = gtk_label_popup_menu;
605 widget_class->focus = gtk_label_focus;
606 widget_class->get_request_mode = gtk_label_get_request_mode;
607 widget_class->get_preferred_width = gtk_label_get_preferred_width;
608 widget_class->get_preferred_height = gtk_label_get_preferred_height;
609 widget_class->get_preferred_width_for_height = gtk_label_get_preferred_width_for_height;
610 widget_class->get_preferred_height_for_width = gtk_label_get_preferred_height_for_width;
612 class->move_cursor = gtk_label_move_cursor;
613 class->copy_clipboard = gtk_label_copy_clipboard;
614 class->activate_link = gtk_label_activate_link;
617 * GtkLabel::move-cursor:
618 * @entry: the object which received the signal
619 * @step: the granularity of the move, as a #GtkMovementStep
620 * @count: the number of @step units to move
621 * @extend_selection: %TRUE if the move should extend the selection
623 * The ::move-cursor signal is a
624 * <link linkend="keybinding-signals">keybinding signal</link>
625 * which gets emitted when the user initiates a cursor movement.
626 * If the cursor is not visible in @entry, this signal causes
627 * the viewport to be moved instead.
629 * Applications should not connect to it, but may emit it with
630 * g_signal_emit_by_name() if they need to control the cursor
633 * The default bindings for this signal come in two variants,
634 * the variant with the Shift modifier extends the selection,
635 * the variant without the Shift modifer does not.
636 * There are too many key combinations to list them all here.
638 * <listitem>Arrow keys move by individual characters/lines</listitem>
639 * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
640 * <listitem>Home/End keys move to the ends of the buffer</listitem>
643 signals[MOVE_CURSOR] =
644 g_signal_new (I_("move-cursor"),
645 G_OBJECT_CLASS_TYPE (gobject_class),
646 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
647 G_STRUCT_OFFSET (GtkLabelClass, move_cursor),
649 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
651 GTK_TYPE_MOVEMENT_STEP,
656 * GtkLabel::copy-clipboard:
657 * @label: the object which received the signal
659 * The ::copy-clipboard signal is a
660 * <link linkend="keybinding-signals">keybinding signal</link>
661 * which gets emitted to copy the selection to the clipboard.
663 * The default binding for this signal is Ctrl-c.
665 signals[COPY_CLIPBOARD] =
666 g_signal_new (I_("copy-clipboard"),
667 G_OBJECT_CLASS_TYPE (gobject_class),
668 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
669 G_STRUCT_OFFSET (GtkLabelClass, copy_clipboard),
671 _gtk_marshal_VOID__VOID,
675 * GtkLabel::populate-popup:
676 * @label: The label on which the signal is emitted
677 * @menu: the menu that is being populated
679 * The ::populate-popup signal gets emitted before showing the
680 * context menu of the label. Note that only selectable labels
681 * have context menus.
683 * If you need to add items to the context menu, connect
684 * to this signal and append your menuitems to the @menu.
686 signals[POPULATE_POPUP] =
687 g_signal_new (I_("populate-popup"),
688 G_OBJECT_CLASS_TYPE (gobject_class),
690 G_STRUCT_OFFSET (GtkLabelClass, populate_popup),
692 _gtk_marshal_VOID__OBJECT,
697 * GtkLabel::activate-current-link:
698 * @label: The label on which the signal was emitted
700 * A <link linkend="keybinding-signals">keybinding signal</link>
701 * which gets emitted when the user activates a link in the label.
703 * Applications may also emit the signal with g_signal_emit_by_name()
704 * if they need to control activation of URIs programmatically.
706 * The default bindings for this signal are all forms of the Enter key.
710 signals[ACTIVATE_CURRENT_LINK] =
711 g_signal_new_class_handler ("activate-current-link",
712 G_TYPE_FROM_CLASS (gobject_class),
713 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
714 G_CALLBACK (gtk_label_activate_current_link),
716 _gtk_marshal_VOID__VOID,
720 * GtkLabel::activate-link:
721 * @label: The label on which the signal was emitted
722 * @uri: the URI that is activated
724 * The signal which gets emitted to activate a URI.
725 * Applications may connect to it to override the default behaviour,
726 * which is to call gtk_show_uri().
728 * Returns: %TRUE if the link has been activated
732 signals[ACTIVATE_LINK] =
733 g_signal_new ("activate-link",
734 G_TYPE_FROM_CLASS (gobject_class),
736 G_STRUCT_OFFSET (GtkLabelClass, activate_link),
737 _gtk_boolean_handled_accumulator, NULL,
738 _gtk_marshal_BOOLEAN__STRING,
739 G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
741 g_object_class_install_property (gobject_class,
743 g_param_spec_string ("label",
745 P_("The text of the label"),
747 GTK_PARAM_READWRITE));
748 g_object_class_install_property (gobject_class,
750 g_param_spec_boxed ("attributes",
752 P_("A list of style attributes to apply to the text of the label"),
753 PANGO_TYPE_ATTR_LIST,
754 GTK_PARAM_READWRITE));
755 g_object_class_install_property (gobject_class,
757 g_param_spec_boolean ("use-markup",
759 P_("The text of the label includes XML markup. See pango_parse_markup()"),
761 GTK_PARAM_READWRITE));
762 g_object_class_install_property (gobject_class,
764 g_param_spec_boolean ("use-underline",
766 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
768 GTK_PARAM_READWRITE));
770 g_object_class_install_property (gobject_class,
772 g_param_spec_enum ("justify",
774 P_("The alignment of the lines in the text of the label relative to each other. This does NOT affect the alignment of the label within its allocation. See GtkMisc::xalign for that"),
775 GTK_TYPE_JUSTIFICATION,
777 GTK_PARAM_READWRITE));
779 g_object_class_install_property (gobject_class,
781 g_param_spec_string ("pattern",
783 P_("A string with _ characters in positions correspond to characters in the text to underline"),
785 GTK_PARAM_WRITABLE));
787 g_object_class_install_property (gobject_class,
789 g_param_spec_boolean ("wrap",
791 P_("If set, wrap lines if the text becomes too wide"),
793 GTK_PARAM_READWRITE));
795 * GtkLabel:wrap-mode:
797 * If line wrapping is on (see the #GtkLabel:wrap property) this controls
798 * how the line wrapping is done. The default is %PANGO_WRAP_WORD, which
799 * means wrap on word boundaries.
803 g_object_class_install_property (gobject_class,
805 g_param_spec_enum ("wrap-mode",
806 P_("Line wrap mode"),
807 P_("If wrap is set, controls how linewrapping is done"),
808 PANGO_TYPE_WRAP_MODE,
810 GTK_PARAM_READWRITE));
811 g_object_class_install_property (gobject_class,
813 g_param_spec_boolean ("selectable",
815 P_("Whether the label text can be selected with the mouse"),
817 GTK_PARAM_READWRITE));
818 g_object_class_install_property (gobject_class,
819 PROP_MNEMONIC_KEYVAL,
820 g_param_spec_uint ("mnemonic-keyval",
822 P_("The mnemonic accelerator key for this label"),
826 GTK_PARAM_READABLE));
827 g_object_class_install_property (gobject_class,
828 PROP_MNEMONIC_WIDGET,
829 g_param_spec_object ("mnemonic-widget",
830 P_("Mnemonic widget"),
831 P_("The widget to be activated when the label's mnemonic "
834 GTK_PARAM_READWRITE));
836 g_object_class_install_property (gobject_class,
837 PROP_CURSOR_POSITION,
838 g_param_spec_int ("cursor-position",
839 P_("Cursor Position"),
840 P_("The current position of the insertion cursor in chars"),
844 GTK_PARAM_READABLE));
846 g_object_class_install_property (gobject_class,
847 PROP_SELECTION_BOUND,
848 g_param_spec_int ("selection-bound",
849 P_("Selection Bound"),
850 P_("The position of the opposite end of the selection from the cursor in chars"),
854 GTK_PARAM_READABLE));
857 * GtkLabel:ellipsize:
859 * The preferred place to ellipsize the string, if the label does
860 * not have enough room to display the entire string, specified as a
861 * #PangoEllisizeMode.
863 * Note that setting this property to a value other than
864 * %PANGO_ELLIPSIZE_NONE has the side-effect that the label requests
865 * only enough space to display the ellipsis "...". In particular, this
866 * means that ellipsizing labels do not work well in notebook tabs, unless
867 * the tab's #GtkNotebook:tab-expand property is set to %TRUE. Other ways
868 * to set a label's width are gtk_widget_set_size_request() and
869 * gtk_label_set_width_chars().
873 g_object_class_install_property (gobject_class,
875 g_param_spec_enum ("ellipsize",
877 P_("The preferred place to ellipsize the string, if the label does not have enough room to display the entire string"),
878 PANGO_TYPE_ELLIPSIZE_MODE,
879 PANGO_ELLIPSIZE_NONE,
880 GTK_PARAM_READWRITE));
883 * GtkLabel:width-chars:
885 * The desired width of the label, in characters. If this property is set to
886 * -1, the width will be calculated automatically.
888 * See the section on <link linkend="label-text-layout">text layout</link>
889 * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
890 * determine the width of ellipsized and wrapped labels.
894 g_object_class_install_property (gobject_class,
896 g_param_spec_int ("width-chars",
897 P_("Width In Characters"),
898 P_("The desired width of the label, in characters"),
902 GTK_PARAM_READWRITE));
905 * GtkLabel:single-line-mode:
907 * Whether the label is in single line mode. In single line mode,
908 * the height of the label does not depend on the actual text, it
909 * is always set to ascent + descent of the font. This can be an
910 * advantage in situations where resizing the label because of text
911 * changes would be distracting, e.g. in a statusbar.
915 g_object_class_install_property (gobject_class,
916 PROP_SINGLE_LINE_MODE,
917 g_param_spec_boolean ("single-line-mode",
918 P_("Single Line Mode"),
919 P_("Whether the label is in single line mode"),
921 GTK_PARAM_READWRITE));
926 * The angle that the baseline of the label makes with the horizontal,
927 * in degrees, measured counterclockwise. An angle of 90 reads from
928 * from bottom to top, an angle of 270, from top to bottom. Ignored
929 * if the label is selectable, wrapped, or ellipsized.
933 g_object_class_install_property (gobject_class,
935 g_param_spec_double ("angle",
937 P_("Angle at which the label is rotated"),
941 GTK_PARAM_READWRITE));
944 * GtkLabel:max-width-chars:
946 * The desired maximum width of the label, in characters. If this property
947 * is set to -1, the width will be calculated automatically.
949 * See the section on <link linkend="label-text-layout">text layout</link>
950 * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
951 * determine the width of ellipsized and wrapped labels.
955 g_object_class_install_property (gobject_class,
956 PROP_MAX_WIDTH_CHARS,
957 g_param_spec_int ("max-width-chars",
958 P_("Maximum Width In Characters"),
959 P_("The desired maximum width of the label, in characters"),
963 GTK_PARAM_READWRITE));
966 * GtkLabel:track-visited-links:
968 * Set this property to %TRUE to make the label track which links
969 * have been clicked. It will then apply the ::visited-link-color
970 * color, instead of ::link-color.
974 g_object_class_install_property (gobject_class,
975 PROP_TRACK_VISITED_LINKS,
976 g_param_spec_boolean ("track-visited-links",
977 P_("Track visited links"),
978 P_("Whether visited links should be tracked"),
980 GTK_PARAM_READWRITE));
985 binding_set = gtk_binding_set_by_class (class);
987 /* Moving the insertion point */
988 add_move_binding (binding_set, GDK_KEY_Right, 0,
989 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
991 add_move_binding (binding_set, GDK_KEY_Left, 0,
992 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
994 add_move_binding (binding_set, GDK_KEY_KP_Right, 0,
995 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
997 add_move_binding (binding_set, GDK_KEY_KP_Left, 0,
998 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
1000 add_move_binding (binding_set, GDK_KEY_f, GDK_CONTROL_MASK,
1001 GTK_MOVEMENT_LOGICAL_POSITIONS, 1);
1003 add_move_binding (binding_set, GDK_KEY_b, GDK_CONTROL_MASK,
1004 GTK_MOVEMENT_LOGICAL_POSITIONS, -1);
1006 add_move_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK,
1007 GTK_MOVEMENT_WORDS, 1);
1009 add_move_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK,
1010 GTK_MOVEMENT_WORDS, -1);
1012 add_move_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK,
1013 GTK_MOVEMENT_WORDS, 1);
1015 add_move_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK,
1016 GTK_MOVEMENT_WORDS, -1);
1019 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
1021 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1023 G_TYPE_BOOLEAN, FALSE);
1025 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
1027 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1029 G_TYPE_BOOLEAN, TRUE);
1031 gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
1033 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1035 G_TYPE_BOOLEAN, FALSE);
1037 gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
1039 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1041 G_TYPE_BOOLEAN, TRUE);
1044 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1046 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1048 G_TYPE_BOOLEAN, FALSE);
1050 gtk_binding_entry_add_signal (binding_set, GDK_KEY_backslash, GDK_CONTROL_MASK,
1052 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1054 G_TYPE_BOOLEAN, FALSE);
1056 add_move_binding (binding_set, GDK_KEY_f, GDK_MOD1_MASK,
1057 GTK_MOVEMENT_WORDS, 1);
1059 add_move_binding (binding_set, GDK_KEY_b, GDK_MOD1_MASK,
1060 GTK_MOVEMENT_WORDS, -1);
1062 add_move_binding (binding_set, GDK_KEY_Home, 0,
1063 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1065 add_move_binding (binding_set, GDK_KEY_End, 0,
1066 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1068 add_move_binding (binding_set, GDK_KEY_KP_Home, 0,
1069 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1071 add_move_binding (binding_set, GDK_KEY_KP_End, 0,
1072 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1074 add_move_binding (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK,
1075 GTK_MOVEMENT_BUFFER_ENDS, -1);
1077 add_move_binding (binding_set, GDK_KEY_End, GDK_CONTROL_MASK,
1078 GTK_MOVEMENT_BUFFER_ENDS, 1);
1080 add_move_binding (binding_set, GDK_KEY_KP_Home, GDK_CONTROL_MASK,
1081 GTK_MOVEMENT_BUFFER_ENDS, -1);
1083 add_move_binding (binding_set, GDK_KEY_KP_End, GDK_CONTROL_MASK,
1084 GTK_MOVEMENT_BUFFER_ENDS, 1);
1087 gtk_binding_entry_add_signal (binding_set, GDK_KEY_c, GDK_CONTROL_MASK,
1088 "copy-clipboard", 0);
1090 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Return, 0,
1091 "activate-current-link", 0);
1092 gtk_binding_entry_add_signal (binding_set, GDK_KEY_ISO_Enter, 0,
1093 "activate-current-link", 0);
1094 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Enter, 0,
1095 "activate-current-link", 0);
1097 g_type_class_add_private (class, sizeof (GtkLabelPrivate));
1099 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_LABEL_ACCESSIBLE);
1103 gtk_label_set_property (GObject *object,
1105 const GValue *value,
1108 GtkLabel *label = GTK_LABEL (object);
1113 gtk_label_set_label (label, g_value_get_string (value));
1115 case PROP_ATTRIBUTES:
1116 gtk_label_set_attributes (label, g_value_get_boxed (value));
1118 case PROP_USE_MARKUP:
1119 gtk_label_set_use_markup (label, g_value_get_boolean (value));
1121 case PROP_USE_UNDERLINE:
1122 gtk_label_set_use_underline (label, g_value_get_boolean (value));
1125 gtk_label_set_justify (label, g_value_get_enum (value));
1128 gtk_label_set_pattern (label, g_value_get_string (value));
1131 gtk_label_set_line_wrap (label, g_value_get_boolean (value));
1133 case PROP_WRAP_MODE:
1134 gtk_label_set_line_wrap_mode (label, g_value_get_enum (value));
1136 case PROP_SELECTABLE:
1137 gtk_label_set_selectable (label, g_value_get_boolean (value));
1139 case PROP_MNEMONIC_WIDGET:
1140 gtk_label_set_mnemonic_widget (label, (GtkWidget*) g_value_get_object (value));
1142 case PROP_ELLIPSIZE:
1143 gtk_label_set_ellipsize (label, g_value_get_enum (value));
1145 case PROP_WIDTH_CHARS:
1146 gtk_label_set_width_chars (label, g_value_get_int (value));
1148 case PROP_SINGLE_LINE_MODE:
1149 gtk_label_set_single_line_mode (label, g_value_get_boolean (value));
1152 gtk_label_set_angle (label, g_value_get_double (value));
1154 case PROP_MAX_WIDTH_CHARS:
1155 gtk_label_set_max_width_chars (label, g_value_get_int (value));
1157 case PROP_TRACK_VISITED_LINKS:
1158 gtk_label_set_track_visited_links (label, g_value_get_boolean (value));
1161 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1167 gtk_label_get_property (GObject *object,
1172 GtkLabel *label = GTK_LABEL (object);
1173 GtkLabelPrivate *priv = label->priv;
1178 g_value_set_string (value, priv->label);
1180 case PROP_ATTRIBUTES:
1181 g_value_set_boxed (value, priv->attrs);
1183 case PROP_USE_MARKUP:
1184 g_value_set_boolean (value, priv->use_markup);
1186 case PROP_USE_UNDERLINE:
1187 g_value_set_boolean (value, priv->use_underline);
1190 g_value_set_enum (value, priv->jtype);
1193 g_value_set_boolean (value, priv->wrap);
1195 case PROP_WRAP_MODE:
1196 g_value_set_enum (value, priv->wrap_mode);
1198 case PROP_SELECTABLE:
1199 g_value_set_boolean (value, gtk_label_get_selectable (label));
1201 case PROP_MNEMONIC_KEYVAL:
1202 g_value_set_uint (value, priv->mnemonic_keyval);
1204 case PROP_MNEMONIC_WIDGET:
1205 g_value_set_object (value, (GObject*) priv->mnemonic_widget);
1207 case PROP_CURSOR_POSITION:
1208 g_value_set_int (value, _gtk_label_get_cursor_position (label));
1210 case PROP_SELECTION_BOUND:
1211 g_value_set_int (value, _gtk_label_get_selection_bound (label));
1213 case PROP_ELLIPSIZE:
1214 g_value_set_enum (value, priv->ellipsize);
1216 case PROP_WIDTH_CHARS:
1217 g_value_set_int (value, gtk_label_get_width_chars (label));
1219 case PROP_SINGLE_LINE_MODE:
1220 g_value_set_boolean (value, gtk_label_get_single_line_mode (label));
1223 g_value_set_double (value, gtk_label_get_angle (label));
1225 case PROP_MAX_WIDTH_CHARS:
1226 g_value_set_int (value, gtk_label_get_max_width_chars (label));
1228 case PROP_TRACK_VISITED_LINKS:
1229 g_value_set_boolean (value, gtk_label_get_track_visited_links (label));
1232 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1238 gtk_label_init (GtkLabel *label)
1240 GtkLabelPrivate *priv;
1242 label->priv = G_TYPE_INSTANCE_GET_PRIVATE (label,
1247 gtk_widget_set_has_window (GTK_WIDGET (label), FALSE);
1249 priv->width_chars = -1;
1250 priv->max_width_chars = -1;
1253 priv->jtype = GTK_JUSTIFY_LEFT;
1255 priv->wrap_mode = PANGO_WRAP_WORD;
1256 priv->ellipsize = PANGO_ELLIPSIZE_NONE;
1258 priv->use_underline = FALSE;
1259 priv->use_markup = FALSE;
1260 priv->pattern_set = FALSE;
1261 priv->track_links = TRUE;
1263 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
1264 priv->layout = NULL;
1268 priv->mnemonic_widget = NULL;
1269 priv->mnemonic_window = NULL;
1271 priv->mnemonics_visible = TRUE;
1273 gtk_label_set_text (label, "");
1278 gtk_label_buildable_interface_init (GtkBuildableIface *iface)
1280 buildable_parent_iface = g_type_interface_peek_parent (iface);
1282 iface->custom_tag_start = gtk_label_buildable_custom_tag_start;
1283 iface->custom_finished = gtk_label_buildable_custom_finished;
1287 GtkBuilder *builder;
1289 PangoAttrList *attrs;
1292 static PangoAttribute *
1293 attribute_from_text (GtkBuilder *builder,
1298 PangoAttribute *attribute = NULL;
1300 PangoLanguage *language;
1301 PangoFontDescription *font_desc;
1303 GValue val = { 0, };
1305 if (!gtk_builder_value_from_string_type (builder, PANGO_TYPE_ATTR_TYPE, name, &val, error))
1308 type = g_value_get_enum (&val);
1309 g_value_unset (&val);
1313 /* PangoAttrLanguage */
1314 case PANGO_ATTR_LANGUAGE:
1315 if ((language = pango_language_from_string (value)))
1317 attribute = pango_attr_language_new (language);
1318 g_value_init (&val, G_TYPE_INT);
1322 case PANGO_ATTR_STYLE:
1323 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STYLE, value, &val, error))
1324 attribute = pango_attr_style_new (g_value_get_enum (&val));
1326 case PANGO_ATTR_WEIGHT:
1327 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_WEIGHT, value, &val, error))
1328 attribute = pango_attr_weight_new (g_value_get_enum (&val));
1330 case PANGO_ATTR_VARIANT:
1331 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_VARIANT, value, &val, error))
1332 attribute = pango_attr_variant_new (g_value_get_enum (&val));
1334 case PANGO_ATTR_STRETCH:
1335 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STRETCH, value, &val, error))
1336 attribute = pango_attr_stretch_new (g_value_get_enum (&val));
1338 case PANGO_ATTR_UNDERLINE:
1339 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_UNDERLINE, value, &val, NULL))
1340 attribute = pango_attr_underline_new (g_value_get_enum (&val));
1343 /* XXX: allow boolean for backwards compat, so ignore error */
1344 /* Deprecate this somehow */
1345 g_value_unset (&val);
1346 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1347 attribute = pango_attr_underline_new (g_value_get_boolean (&val));
1350 case PANGO_ATTR_STRIKETHROUGH:
1351 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1352 attribute = pango_attr_strikethrough_new (g_value_get_boolean (&val));
1354 case PANGO_ATTR_GRAVITY:
1355 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY, value, &val, error))
1356 attribute = pango_attr_gravity_new (g_value_get_enum (&val));
1358 case PANGO_ATTR_GRAVITY_HINT:
1359 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY_HINT,
1360 value, &val, error))
1361 attribute = pango_attr_gravity_hint_new (g_value_get_enum (&val));
1363 /* PangoAttrString */
1364 case PANGO_ATTR_FAMILY:
1365 attribute = pango_attr_family_new (value);
1366 g_value_init (&val, G_TYPE_INT);
1370 case PANGO_ATTR_SIZE:
1371 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1372 value, &val, error))
1373 attribute = pango_attr_size_new (g_value_get_int (&val));
1375 case PANGO_ATTR_ABSOLUTE_SIZE:
1376 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1377 value, &val, error))
1378 attribute = pango_attr_size_new_absolute (g_value_get_int (&val));
1381 /* PangoAttrFontDesc */
1382 case PANGO_ATTR_FONT_DESC:
1383 if ((font_desc = pango_font_description_from_string (value)))
1385 attribute = pango_attr_font_desc_new (font_desc);
1386 pango_font_description_free (font_desc);
1387 g_value_init (&val, G_TYPE_INT);
1391 /* PangoAttrColor */
1392 case PANGO_ATTR_FOREGROUND:
1393 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1394 value, &val, error))
1396 color = g_value_get_boxed (&val);
1397 attribute = pango_attr_foreground_new (color->red, color->green, color->blue);
1400 case PANGO_ATTR_BACKGROUND:
1401 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1402 value, &val, error))
1404 color = g_value_get_boxed (&val);
1405 attribute = pango_attr_background_new (color->red, color->green, color->blue);
1408 case PANGO_ATTR_UNDERLINE_COLOR:
1409 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1410 value, &val, error))
1412 color = g_value_get_boxed (&val);
1413 attribute = pango_attr_underline_color_new (color->red, color->green, color->blue);
1416 case PANGO_ATTR_STRIKETHROUGH_COLOR:
1417 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1418 value, &val, error))
1420 color = g_value_get_boxed (&val);
1421 attribute = pango_attr_strikethrough_color_new (color->red, color->green, color->blue);
1425 /* PangoAttrShape */
1426 case PANGO_ATTR_SHAPE:
1427 /* Unsupported for now */
1429 /* PangoAttrFloat */
1430 case PANGO_ATTR_SCALE:
1431 if (gtk_builder_value_from_string_type (builder, G_TYPE_DOUBLE,
1432 value, &val, error))
1433 attribute = pango_attr_scale_new (g_value_get_double (&val));
1436 case PANGO_ATTR_INVALID:
1437 case PANGO_ATTR_LETTER_SPACING:
1438 case PANGO_ATTR_RISE:
1439 case PANGO_ATTR_FALLBACK:
1444 g_value_unset (&val);
1451 pango_start_element (GMarkupParseContext *context,
1452 const gchar *element_name,
1453 const gchar **names,
1454 const gchar **values,
1458 PangoParserData *data = (PangoParserData*)user_data;
1459 GValue val = { 0, };
1461 gint line_number, char_number;
1463 if (strcmp (element_name, "attribute") == 0)
1465 PangoAttribute *attr = NULL;
1466 const gchar *name = NULL;
1467 const gchar *value = NULL;
1468 const gchar *start = NULL;
1469 const gchar *end = NULL;
1470 guint start_val = 0;
1471 guint end_val = G_MAXUINT;
1473 for (i = 0; names[i]; i++)
1475 if (strcmp (names[i], "name") == 0)
1477 else if (strcmp (names[i], "value") == 0)
1479 else if (strcmp (names[i], "start") == 0)
1481 else if (strcmp (names[i], "end") == 0)
1485 g_markup_parse_context_get_position (context,
1490 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1491 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1493 line_number, char_number, names[i], "attribute");
1498 if (!name || !value)
1500 g_markup_parse_context_get_position (context,
1505 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1506 "%s:%d:%d <%s> requires attribute \"%s\"",
1508 line_number, char_number, "attribute",
1509 name ? "value" : "name");
1515 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1516 start, &val, error))
1518 start_val = g_value_get_uint (&val);
1519 g_value_unset (&val);
1524 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1527 end_val = g_value_get_uint (&val);
1528 g_value_unset (&val);
1531 attr = attribute_from_text (data->builder, name, value, error);
1535 attr->start_index = start_val;
1536 attr->end_index = end_val;
1539 data->attrs = pango_attr_list_new ();
1541 pango_attr_list_insert (data->attrs, attr);
1544 else if (strcmp (element_name, "attributes") == 0)
1547 g_warning ("Unsupported tag for GtkLabel: %s\n", element_name);
1550 static const GMarkupParser pango_parser =
1552 pango_start_element,
1556 gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
1557 GtkBuilder *builder,
1559 const gchar *tagname,
1560 GMarkupParser *parser,
1563 if (buildable_parent_iface->custom_tag_start (buildable, builder, child,
1564 tagname, parser, data))
1567 if (strcmp (tagname, "attributes") == 0)
1569 PangoParserData *parser_data;
1571 parser_data = g_slice_new0 (PangoParserData);
1572 parser_data->builder = g_object_ref (builder);
1573 parser_data->object = g_object_ref (buildable);
1574 *parser = pango_parser;
1575 *data = parser_data;
1582 gtk_label_buildable_custom_finished (GtkBuildable *buildable,
1583 GtkBuilder *builder,
1585 const gchar *tagname,
1588 PangoParserData *data;
1590 buildable_parent_iface->custom_finished (buildable, builder, child,
1591 tagname, user_data);
1593 if (strcmp (tagname, "attributes") == 0)
1595 data = (PangoParserData*)user_data;
1599 gtk_label_set_attributes (GTK_LABEL (buildable), data->attrs);
1600 pango_attr_list_unref (data->attrs);
1603 g_object_unref (data->object);
1604 g_object_unref (data->builder);
1605 g_slice_free (PangoParserData, data);
1612 * @str: The text of the label
1614 * Creates a new label with the given text inside it. You can
1615 * pass %NULL to get an empty label widget.
1617 * Return value: the new #GtkLabel
1620 gtk_label_new (const gchar *str)
1624 label = g_object_new (GTK_TYPE_LABEL, NULL);
1627 gtk_label_set_text (label, str);
1629 return GTK_WIDGET (label);
1633 * gtk_label_new_with_mnemonic:
1634 * @str: The text of the label, with an underscore in front of the
1635 * mnemonic character
1637 * Creates a new #GtkLabel, containing the text in @str.
1639 * If characters in @str are preceded by an underscore, they are
1640 * underlined. If you need a literal underscore character in a label, use
1641 * '__' (two underscores). The first underlined character represents a
1642 * keyboard accelerator called a mnemonic. The mnemonic key can be used
1643 * to activate another widget, chosen automatically, or explicitly using
1644 * gtk_label_set_mnemonic_widget().
1646 * If gtk_label_set_mnemonic_widget() is not called, then the first
1647 * activatable ancestor of the #GtkLabel will be chosen as the mnemonic
1648 * widget. For instance, if the label is inside a button or menu item,
1649 * the button or menu item will automatically become the mnemonic widget
1650 * and be activated by the mnemonic.
1652 * Return value: the new #GtkLabel
1655 gtk_label_new_with_mnemonic (const gchar *str)
1659 label = g_object_new (GTK_TYPE_LABEL, NULL);
1662 gtk_label_set_text_with_mnemonic (label, str);
1664 return GTK_WIDGET (label);
1668 gtk_label_mnemonic_activate (GtkWidget *widget,
1669 gboolean group_cycling)
1671 GtkLabel *label = GTK_LABEL (widget);
1672 GtkLabelPrivate *priv = label->priv;
1675 if (priv->mnemonic_widget)
1676 return gtk_widget_mnemonic_activate (priv->mnemonic_widget, group_cycling);
1678 /* Try to find the widget to activate by traversing the
1679 * widget's ancestry.
1681 parent = gtk_widget_get_parent (widget);
1683 if (GTK_IS_NOTEBOOK (parent))
1688 if (gtk_widget_get_can_focus (parent) ||
1689 (!group_cycling && GTK_WIDGET_GET_CLASS (parent)->activate_signal) ||
1690 GTK_IS_NOTEBOOK (gtk_widget_get_parent (parent)) ||
1691 GTK_IS_MENU_ITEM (parent))
1692 return gtk_widget_mnemonic_activate (parent, group_cycling);
1693 parent = gtk_widget_get_parent (parent);
1696 /* barf if there was nothing to activate */
1697 g_warning ("Couldn't find a target for a mnemonic activation.");
1698 gtk_widget_error_bell (widget);
1704 gtk_label_setup_mnemonic (GtkLabel *label,
1707 GtkLabelPrivate *priv = label->priv;
1708 GtkWidget *widget = GTK_WIDGET (label);
1709 GtkWidget *toplevel;
1710 GtkWidget *mnemonic_menu;
1712 mnemonic_menu = g_object_get_data (G_OBJECT (label), "gtk-mnemonic-menu");
1714 if (last_key != GDK_KEY_VoidSymbol)
1716 if (priv->mnemonic_window)
1718 gtk_window_remove_mnemonic (priv->mnemonic_window,
1721 priv->mnemonic_window = NULL;
1725 _gtk_menu_shell_remove_mnemonic (GTK_MENU_SHELL (mnemonic_menu),
1728 mnemonic_menu = NULL;
1732 if (priv->mnemonic_keyval == GDK_KEY_VoidSymbol)
1735 connect_mnemonics_visible_notify (GTK_LABEL (widget));
1737 toplevel = gtk_widget_get_toplevel (widget);
1738 if (gtk_widget_is_toplevel (toplevel))
1740 GtkWidget *menu_shell;
1742 menu_shell = gtk_widget_get_ancestor (widget,
1743 GTK_TYPE_MENU_SHELL);
1747 _gtk_menu_shell_add_mnemonic (GTK_MENU_SHELL (menu_shell),
1748 priv->mnemonic_keyval,
1750 mnemonic_menu = menu_shell;
1753 if (!GTK_IS_MENU (menu_shell))
1755 gtk_window_add_mnemonic (GTK_WINDOW (toplevel),
1756 priv->mnemonic_keyval,
1758 priv->mnemonic_window = GTK_WINDOW (toplevel);
1763 g_object_set_data (G_OBJECT (label), I_("gtk-mnemonic-menu"), mnemonic_menu);
1767 gtk_label_hierarchy_changed (GtkWidget *widget,
1768 GtkWidget *old_toplevel)
1770 GtkLabel *label = GTK_LABEL (widget);
1771 GtkLabelPrivate *priv = label->priv;
1773 gtk_label_setup_mnemonic (label, priv->mnemonic_keyval);
1777 label_shortcut_setting_apply (GtkLabel *label)
1779 gtk_label_recalculate (label);
1780 if (GTK_IS_ACCEL_LABEL (label))
1781 gtk_accel_label_refetch (GTK_ACCEL_LABEL (label));
1785 label_shortcut_setting_traverse_container (GtkWidget *widget,
1788 if (GTK_IS_LABEL (widget))
1789 label_shortcut_setting_apply (GTK_LABEL (widget));
1790 else if (GTK_IS_CONTAINER (widget))
1791 gtk_container_forall (GTK_CONTAINER (widget),
1792 label_shortcut_setting_traverse_container, data);
1796 label_shortcut_setting_changed (GtkSettings *settings)
1800 list = gtk_window_list_toplevels ();
1802 for (l = list; l ; l = l->next)
1804 GtkWidget *widget = l->data;
1806 if (gtk_widget_get_settings (widget) == settings)
1807 gtk_container_forall (GTK_CONTAINER (widget),
1808 label_shortcut_setting_traverse_container, NULL);
1815 mnemonics_visible_apply (GtkWidget *widget,
1816 gboolean mnemonics_visible)
1818 GtkLabel *label = GTK_LABEL (widget);
1819 GtkLabelPrivate *priv = label->priv;
1821 mnemonics_visible = mnemonics_visible != FALSE;
1823 if (priv->mnemonics_visible != mnemonics_visible)
1825 priv->mnemonics_visible = mnemonics_visible;
1827 gtk_label_recalculate (label);
1832 label_mnemonics_visible_traverse_container (GtkWidget *widget,
1835 gboolean mnemonics_visible = GPOINTER_TO_INT (data);
1837 _gtk_label_mnemonics_visible_apply_recursively (widget, mnemonics_visible);
1841 _gtk_label_mnemonics_visible_apply_recursively (GtkWidget *widget,
1842 gboolean mnemonics_visible)
1844 if (GTK_IS_LABEL (widget))
1845 mnemonics_visible_apply (widget, mnemonics_visible);
1846 else if (GTK_IS_CONTAINER (widget))
1847 gtk_container_forall (GTK_CONTAINER (widget),
1848 label_mnemonics_visible_traverse_container,
1849 GINT_TO_POINTER (mnemonics_visible));
1853 label_mnemonics_visible_changed (GtkWindow *window,
1857 gboolean mnemonics_visible;
1859 g_object_get (window, "mnemonics-visible", &mnemonics_visible, NULL);
1861 gtk_container_forall (GTK_CONTAINER (window),
1862 label_mnemonics_visible_traverse_container,
1863 GINT_TO_POINTER (mnemonics_visible));
1867 gtk_label_screen_changed (GtkWidget *widget,
1868 GdkScreen *old_screen)
1870 GtkSettings *settings;
1871 gboolean shortcuts_connected;
1873 if (!gtk_widget_has_screen (widget))
1876 settings = gtk_widget_get_settings (widget);
1878 shortcuts_connected =
1879 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (settings),
1880 "gtk-label-shortcuts-connected"));
1882 if (! shortcuts_connected)
1884 g_signal_connect (settings, "notify::gtk-enable-mnemonics",
1885 G_CALLBACK (label_shortcut_setting_changed),
1887 g_signal_connect (settings, "notify::gtk-enable-accels",
1888 G_CALLBACK (label_shortcut_setting_changed),
1891 g_object_set_data (G_OBJECT (settings), "gtk-label-shortcuts-connected",
1892 GINT_TO_POINTER (TRUE));
1895 label_shortcut_setting_apply (GTK_LABEL (widget));
1900 label_mnemonic_widget_weak_notify (gpointer data,
1901 GObject *where_the_object_was)
1903 GtkLabel *label = data;
1904 GtkLabelPrivate *priv = label->priv;
1906 priv->mnemonic_widget = NULL;
1907 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1911 * gtk_label_set_mnemonic_widget:
1912 * @label: a #GtkLabel
1913 * @widget: (allow-none): the target #GtkWidget
1915 * If the label has been set so that it has an mnemonic key (using
1916 * i.e. gtk_label_set_markup_with_mnemonic(),
1917 * gtk_label_set_text_with_mnemonic(), gtk_label_new_with_mnemonic()
1918 * or the "use_underline" property) the label can be associated with a
1919 * widget that is the target of the mnemonic. When the label is inside
1920 * a widget (like a #GtkButton or a #GtkNotebook tab) it is
1921 * automatically associated with the correct widget, but sometimes
1922 * (i.e. when the target is a #GtkEntry next to the label) you need to
1923 * set it explicitly using this function.
1925 * The target widget will be accelerated by emitting the
1926 * GtkWidget::mnemonic-activate signal on it. The default handler for
1927 * this signal will activate the widget if there are no mnemonic collisions
1928 * and toggle focus between the colliding widgets otherwise.
1931 gtk_label_set_mnemonic_widget (GtkLabel *label,
1934 GtkLabelPrivate *priv;
1936 g_return_if_fail (GTK_IS_LABEL (label));
1941 g_return_if_fail (GTK_IS_WIDGET (widget));
1943 if (priv->mnemonic_widget)
1945 gtk_widget_remove_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1946 g_object_weak_unref (G_OBJECT (priv->mnemonic_widget),
1947 label_mnemonic_widget_weak_notify,
1950 priv->mnemonic_widget = widget;
1951 if (priv->mnemonic_widget)
1953 g_object_weak_ref (G_OBJECT (priv->mnemonic_widget),
1954 label_mnemonic_widget_weak_notify,
1956 gtk_widget_add_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1959 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1963 * gtk_label_get_mnemonic_widget:
1964 * @label: a #GtkLabel
1966 * Retrieves the target of the mnemonic (keyboard shortcut) of this
1967 * label. See gtk_label_set_mnemonic_widget().
1969 * Return value: (transfer none): the target of the label's mnemonic,
1970 * or %NULL if none has been set and the default algorithm will be used.
1973 gtk_label_get_mnemonic_widget (GtkLabel *label)
1975 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1977 return label->priv->mnemonic_widget;
1981 * gtk_label_get_mnemonic_keyval:
1982 * @label: a #GtkLabel
1984 * If the label has been set so that it has an mnemonic key this function
1985 * returns the keyval used for the mnemonic accelerator. If there is no
1986 * mnemonic set up it returns #GDK_VoidSymbol.
1988 * Returns: GDK keyval usable for accelerators, or #GDK_VoidSymbol
1991 gtk_label_get_mnemonic_keyval (GtkLabel *label)
1993 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_KEY_VoidSymbol);
1995 return label->priv->mnemonic_keyval;
1999 gtk_label_set_text_internal (GtkLabel *label,
2002 GtkLabelPrivate *priv = label->priv;
2004 g_free (priv->text);
2008 gtk_label_select_region_index (label, 0, 0);
2012 gtk_label_set_label_internal (GtkLabel *label,
2015 GtkLabelPrivate *priv = label->priv;
2017 g_free (priv->label);
2021 g_object_notify (G_OBJECT (label), "label");
2025 gtk_label_set_use_markup_internal (GtkLabel *label,
2028 GtkLabelPrivate *priv = label->priv;
2031 if (priv->use_markup != val)
2033 priv->use_markup = val;
2035 g_object_notify (G_OBJECT (label), "use-markup");
2040 gtk_label_set_use_underline_internal (GtkLabel *label,
2043 GtkLabelPrivate *priv = label->priv;
2046 if (priv->use_underline != val)
2048 priv->use_underline = val;
2050 g_object_notify (G_OBJECT (label), "use-underline");
2055 gtk_label_compose_effective_attrs (GtkLabel *label)
2057 GtkLabelPrivate *priv = label->priv;
2058 PangoAttrIterator *iter;
2059 PangoAttribute *attr;
2060 GSList *iter_attrs, *l;
2064 if (priv->effective_attrs)
2066 if ((iter = pango_attr_list_get_iterator (priv->attrs)))
2070 iter_attrs = pango_attr_iterator_get_attrs (iter);
2071 for (l = iter_attrs; l; l = l->next)
2074 pango_attr_list_insert (priv->effective_attrs, attr);
2076 g_slist_free (iter_attrs);
2078 while (pango_attr_iterator_next (iter));
2079 pango_attr_iterator_destroy (iter);
2083 priv->effective_attrs =
2084 pango_attr_list_ref (priv->attrs);
2089 gtk_label_set_attributes_internal (GtkLabel *label,
2090 PangoAttrList *attrs)
2092 GtkLabelPrivate *priv = label->priv;
2095 pango_attr_list_ref (attrs);
2098 pango_attr_list_unref (priv->attrs);
2099 priv->attrs = attrs;
2101 g_object_notify (G_OBJECT (label), "attributes");
2105 /* Calculates text, attrs and mnemonic_keyval from
2106 * label, use_underline and use_markup
2109 gtk_label_recalculate (GtkLabel *label)
2111 GtkLabelPrivate *priv = label->priv;
2112 guint keyval = priv->mnemonic_keyval;
2114 if (priv->use_markup)
2115 gtk_label_set_markup_internal (label, priv->label, priv->use_underline);
2118 if (priv->use_underline)
2119 gtk_label_set_uline_text_internal (label, priv->label);
2122 if (priv->effective_attrs)
2123 pango_attr_list_unref (priv->effective_attrs);
2124 priv->effective_attrs = NULL;
2125 gtk_label_set_text_internal (label, g_strdup (priv->label));
2129 gtk_label_compose_effective_attrs (label);
2131 if (!priv->use_underline)
2132 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
2134 if (keyval != priv->mnemonic_keyval)
2136 gtk_label_setup_mnemonic (label, keyval);
2137 g_object_notify (G_OBJECT (label), "mnemonic-keyval");
2140 gtk_label_clear_layout (label);
2141 gtk_label_clear_select_info (label);
2142 gtk_widget_queue_resize (GTK_WIDGET (label));
2146 * gtk_label_set_text:
2147 * @label: a #GtkLabel
2148 * @str: The text you want to set
2150 * Sets the text within the #GtkLabel widget. It overwrites any text that
2153 * This will also clear any previously set mnemonic accelerators.
2156 gtk_label_set_text (GtkLabel *label,
2159 g_return_if_fail (GTK_IS_LABEL (label));
2161 g_object_freeze_notify (G_OBJECT (label));
2163 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2164 gtk_label_set_use_markup_internal (label, FALSE);
2165 gtk_label_set_use_underline_internal (label, FALSE);
2167 gtk_label_recalculate (label);
2169 g_object_thaw_notify (G_OBJECT (label));
2173 * gtk_label_set_attributes:
2174 * @label: a #GtkLabel
2175 * @attrs: a #PangoAttrList
2177 * Sets a #PangoAttrList; the attributes in the list are applied to the
2180 * <note><para>The attributes set with this function will be applied
2181 * and merged with any other attributes previously effected by way
2182 * of the #GtkLabel:use-underline or #GtkLabel:use-markup properties.
2183 * While it is not recommended to mix markup strings with manually set
2184 * attributes, if you must; know that the attributes will be applied
2185 * to the label after the markup string is parsed.</para></note>
2188 gtk_label_set_attributes (GtkLabel *label,
2189 PangoAttrList *attrs)
2191 g_return_if_fail (GTK_IS_LABEL (label));
2193 gtk_label_set_attributes_internal (label, attrs);
2195 gtk_label_recalculate (label);
2197 gtk_label_clear_layout (label);
2198 gtk_widget_queue_resize (GTK_WIDGET (label));
2202 * gtk_label_get_attributes:
2203 * @label: a #GtkLabel
2205 * Gets the attribute list that was set on the label using
2206 * gtk_label_set_attributes(), if any. This function does
2207 * not reflect attributes that come from the labels markup
2208 * (see gtk_label_set_markup()). If you want to get the
2209 * effective attributes for the label, use
2210 * pango_layout_get_attribute (gtk_label_get_layout (label)).
2212 * Return value: (transfer none): the attribute list, or %NULL
2216 gtk_label_get_attributes (GtkLabel *label)
2218 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2220 return label->priv->attrs;
2224 * gtk_label_set_label:
2225 * @label: a #GtkLabel
2226 * @str: the new text to set for the label
2228 * Sets the text of the label. The label is interpreted as
2229 * including embedded underlines and/or Pango markup depending
2230 * on the values of the #GtkLabel:use-underline" and
2231 * #GtkLabel:use-markup properties.
2234 gtk_label_set_label (GtkLabel *label,
2237 g_return_if_fail (GTK_IS_LABEL (label));
2239 g_object_freeze_notify (G_OBJECT (label));
2241 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2242 gtk_label_recalculate (label);
2244 g_object_thaw_notify (G_OBJECT (label));
2248 * gtk_label_get_label:
2249 * @label: a #GtkLabel
2251 * Fetches the text from a label widget including any embedded
2252 * underlines indicating mnemonics and Pango markup. (See
2253 * gtk_label_get_text()).
2255 * Return value: the text of the label widget. This string is
2256 * owned by the widget and must not be modified or freed.
2259 gtk_label_get_label (GtkLabel *label)
2261 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2263 return label->priv->label;
2271 GdkColor *link_color;
2272 GdkColor *visited_link_color;
2276 start_element_handler (GMarkupParseContext *context,
2277 const gchar *element_name,
2278 const gchar **attribute_names,
2279 const gchar **attribute_values,
2283 GtkLabelPrivate *priv;
2284 UriParserData *pdata = user_data;
2286 if (strcmp (element_name, "a") == 0)
2289 const gchar *uri = NULL;
2290 const gchar *title = NULL;
2291 gboolean visited = FALSE;
2295 GdkColor *color = NULL;
2297 g_markup_parse_context_get_position (context, &line_number, &char_number);
2299 for (i = 0; attribute_names[i] != NULL; i++)
2301 const gchar *attr = attribute_names[i];
2303 if (strcmp (attr, "href") == 0)
2304 uri = attribute_values[i];
2305 else if (strcmp (attr, "title") == 0)
2306 title = attribute_values[i];
2311 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
2312 "Attribute '%s' is not allowed on the <a> tag "
2313 "on line %d char %d",
2314 attr, line_number, char_number);
2323 G_MARKUP_ERROR_INVALID_CONTENT,
2324 "Attribute 'href' was missing on the <a> tag "
2325 "on line %d char %d",
2326 line_number, char_number);
2331 priv = pdata->label->priv;
2332 if (priv->track_links && priv->select_info)
2335 for (l = priv->select_info->links; l; l = l->next)
2338 if (strcmp (uri, link->uri) == 0)
2340 visited = link->visited;
2347 color = pdata->visited_link_color;
2349 color = pdata->link_color;
2351 g_string_append_printf (pdata->new_str,
2352 "<span color=\"#%04x%04x%04x\" underline=\"single\">",
2357 link = g_new0 (GtkLabelLink, 1);
2358 link->uri = g_strdup (uri);
2359 link->title = g_strdup (title);
2360 link->visited = visited;
2361 pdata->links = g_list_append (pdata->links, link);
2367 g_string_append_c (pdata->new_str, '<');
2368 g_string_append (pdata->new_str, element_name);
2370 for (i = 0; attribute_names[i] != NULL; i++)
2372 const gchar *attr = attribute_names[i];
2373 const gchar *value = attribute_values[i];
2376 newvalue = g_markup_escape_text (value, -1);
2378 g_string_append_c (pdata->new_str, ' ');
2379 g_string_append (pdata->new_str, attr);
2380 g_string_append (pdata->new_str, "=\"");
2381 g_string_append (pdata->new_str, newvalue);
2382 g_string_append_c (pdata->new_str, '\"');
2386 g_string_append_c (pdata->new_str, '>');
2391 end_element_handler (GMarkupParseContext *context,
2392 const gchar *element_name,
2396 UriParserData *pdata = user_data;
2398 if (!strcmp (element_name, "a"))
2399 g_string_append (pdata->new_str, "</span>");
2402 g_string_append (pdata->new_str, "</");
2403 g_string_append (pdata->new_str, element_name);
2404 g_string_append_c (pdata->new_str, '>');
2409 text_handler (GMarkupParseContext *context,
2415 UriParserData *pdata = user_data;
2418 newtext = g_markup_escape_text (text, text_len);
2419 g_string_append (pdata->new_str, newtext);
2423 static const GMarkupParser markup_parser =
2425 start_element_handler,
2426 end_element_handler,
2433 xml_isspace (gchar c)
2435 return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
2439 link_free (GtkLabelLink *link)
2442 g_free (link->title);
2447 gtk_label_get_link_colors (GtkWidget *widget,
2448 GdkColor **link_color,
2449 GdkColor **visited_link_color)
2451 GtkStyleContext *context;
2453 context = gtk_widget_get_style_context (widget);
2454 gtk_style_context_get_style (context,
2455 "link-color", link_color,
2456 "visited-link-color", visited_link_color,
2459 *link_color = gdk_color_copy (&default_link_color);
2460 if (!*visited_link_color)
2461 *visited_link_color = gdk_color_copy (&default_visited_link_color);
2465 parse_uri_markup (GtkLabel *label,
2471 GMarkupParseContext *context = NULL;
2472 const gchar *p, *end;
2473 gboolean needs_root = TRUE;
2475 UriParserData pdata;
2477 length = strlen (str);
2481 pdata.label = label;
2483 pdata.new_str = g_string_sized_new (length);
2485 gtk_label_get_link_colors (GTK_WIDGET (label), &pdata.link_color, &pdata.visited_link_color);
2487 while (p != end && xml_isspace (*p))
2490 if (end - p >= 8 && strncmp (p, "<markup>", 8) == 0)
2493 context = g_markup_parse_context_new (&markup_parser, 0, &pdata, NULL);
2497 if (!g_markup_parse_context_parse (context, "<markup>", -1, error))
2501 if (!g_markup_parse_context_parse (context, str, length, error))
2506 if (!g_markup_parse_context_parse (context, "</markup>", -1, error))
2510 if (!g_markup_parse_context_end_parse (context, error))
2513 g_markup_parse_context_free (context);
2515 *new_str = g_string_free (pdata.new_str, FALSE);
2516 *links = pdata.links;
2518 gdk_color_free (pdata.link_color);
2519 gdk_color_free (pdata.visited_link_color);
2524 g_markup_parse_context_free (context);
2525 g_string_free (pdata.new_str, TRUE);
2526 g_list_foreach (pdata.links, (GFunc)link_free, NULL);
2527 g_list_free (pdata.links);
2528 gdk_color_free (pdata.link_color);
2529 gdk_color_free (pdata.visited_link_color);
2535 gtk_label_ensure_has_tooltip (GtkLabel *label)
2537 GtkLabelPrivate *priv = label->priv;
2539 gboolean has_tooltip = FALSE;
2541 for (l = priv->select_info->links; l; l = l->next)
2543 GtkLabelLink *link = l->data;
2551 gtk_widget_set_has_tooltip (GTK_WIDGET (label), has_tooltip);
2555 gtk_label_set_markup_internal (GtkLabel *label,
2557 gboolean with_uline)
2559 GtkLabelPrivate *priv = label->priv;
2561 GError *error = NULL;
2562 PangoAttrList *attrs = NULL;
2563 gunichar accel_char = 0;
2565 GList *links = NULL;
2567 if (!parse_uri_markup (label, str, &new_str, &links, &error))
2569 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2571 g_error_free (error);
2575 gtk_label_clear_links (label);
2578 gtk_label_ensure_select_info (label);
2579 priv->select_info->links = links;
2580 gtk_label_ensure_has_tooltip (label);
2585 gboolean enable_mnemonics;
2586 gboolean auto_mnemonics;
2588 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2589 "gtk-enable-mnemonics", &enable_mnemonics,
2590 "gtk-auto-mnemonics", &auto_mnemonics,
2593 if (!(enable_mnemonics && priv->mnemonics_visible &&
2595 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2596 (!priv->mnemonic_widget ||
2597 gtk_widget_is_sensitive (priv->mnemonic_widget))))))
2603 if (separate_uline_pattern (new_str, &key, &tmp, &pattern))
2612 if (!pango_parse_markup (new_str,
2614 with_uline ? '_' : 0,
2617 with_uline ? &accel_char : NULL,
2620 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2623 g_error_free (error);
2630 gtk_label_set_text_internal (label, text);
2634 if (priv->effective_attrs)
2635 pango_attr_list_unref (priv->effective_attrs);
2636 priv->effective_attrs = attrs;
2639 if (accel_char != 0)
2640 priv->mnemonic_keyval = gdk_keyval_to_lower (gdk_unicode_to_keyval (accel_char));
2642 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
2646 * gtk_label_set_markup:
2647 * @label: a #GtkLabel
2648 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
2650 * Parses @str which is marked up with the <link
2651 * linkend="PangoMarkupFormat">Pango text markup language</link>, setting the
2652 * label's text and attribute list based on the parse results. If the @str is
2653 * external data, you may need to escape it with g_markup_escape_text() or
2654 * g_markup_printf_escaped()<!-- -->:
2658 * markup = g_markup_printf_escaped ("<span style=\"italic\">%s</span>", str);
2659 * gtk_label_set_markup (GTK_LABEL (label), markup);
2664 gtk_label_set_markup (GtkLabel *label,
2667 g_return_if_fail (GTK_IS_LABEL (label));
2669 g_object_freeze_notify (G_OBJECT (label));
2671 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2672 gtk_label_set_use_markup_internal (label, TRUE);
2673 gtk_label_set_use_underline_internal (label, FALSE);
2675 gtk_label_recalculate (label);
2677 g_object_thaw_notify (G_OBJECT (label));
2681 * gtk_label_set_markup_with_mnemonic:
2682 * @label: a #GtkLabel
2683 * @str: a markup string (see
2684 * <link linkend="PangoMarkupFormat">Pango markup format</link>)
2686 * Parses @str which is marked up with the
2687 * <link linkend="PangoMarkupFormat">Pango text markup language</link>,
2688 * setting the label's text and attribute list based on the parse results.
2689 * If characters in @str are preceded by an underscore, they are underlined
2690 * indicating that they represent a keyboard accelerator called a mnemonic.
2692 * The mnemonic key can be used to activate another widget, chosen
2693 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
2696 gtk_label_set_markup_with_mnemonic (GtkLabel *label,
2699 g_return_if_fail (GTK_IS_LABEL (label));
2701 g_object_freeze_notify (G_OBJECT (label));
2703 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2704 gtk_label_set_use_markup_internal (label, TRUE);
2705 gtk_label_set_use_underline_internal (label, TRUE);
2707 gtk_label_recalculate (label);
2709 g_object_thaw_notify (G_OBJECT (label));
2713 * gtk_label_get_text:
2714 * @label: a #GtkLabel
2716 * Fetches the text from a label widget, as displayed on the
2717 * screen. This does not include any embedded underlines
2718 * indicating mnemonics or Pango markup. (See gtk_label_get_label())
2720 * Return value: the text in the label widget. This is the internal
2721 * string used by the label, and must not be modified.
2724 gtk_label_get_text (GtkLabel *label)
2726 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2728 return label->priv->text;
2731 static PangoAttrList *
2732 gtk_label_pattern_to_attrs (GtkLabel *label,
2733 const gchar *pattern)
2735 GtkLabelPrivate *priv = label->priv;
2737 const char *p = priv->text;
2738 const char *q = pattern;
2739 PangoAttrList *attrs;
2741 attrs = pango_attr_list_new ();
2745 while (*p && *q && *q != '_')
2747 p = g_utf8_next_char (p);
2751 while (*p && *q && *q == '_')
2753 p = g_utf8_next_char (p);
2759 PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_LOW);
2760 attr->start_index = start - priv->text;
2761 attr->end_index = p - priv->text;
2763 pango_attr_list_insert (attrs, attr);
2773 gtk_label_set_pattern_internal (GtkLabel *label,
2774 const gchar *pattern,
2775 gboolean is_mnemonic)
2777 GtkLabelPrivate *priv = label->priv;
2778 PangoAttrList *attrs;
2779 gboolean enable_mnemonics;
2780 gboolean auto_mnemonics;
2782 if (priv->pattern_set)
2787 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2788 "gtk-enable-mnemonics", &enable_mnemonics,
2789 "gtk-auto-mnemonics", &auto_mnemonics,
2792 if (enable_mnemonics && priv->mnemonics_visible && pattern &&
2794 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2795 (!priv->mnemonic_widget ||
2796 gtk_widget_is_sensitive (priv->mnemonic_widget)))))
2797 attrs = gtk_label_pattern_to_attrs (label, pattern);
2802 attrs = gtk_label_pattern_to_attrs (label, pattern);
2804 if (priv->effective_attrs)
2805 pango_attr_list_unref (priv->effective_attrs);
2806 priv->effective_attrs = attrs;
2810 * gtk_label_set_pattern:
2811 * @label: The #GtkLabel you want to set the pattern to.
2812 * @pattern: The pattern as described above.
2814 * The pattern of underlines you want under the existing text within the
2815 * #GtkLabel widget. For example if the current text of the label says
2816 * "FooBarBaz" passing a pattern of "___ ___" will underline
2817 * "Foo" and "Baz" but not "Bar".
2820 gtk_label_set_pattern (GtkLabel *label,
2821 const gchar *pattern)
2823 GtkLabelPrivate *priv;
2825 g_return_if_fail (GTK_IS_LABEL (label));
2829 priv->pattern_set = FALSE;
2833 gtk_label_set_pattern_internal (label, pattern, FALSE);
2834 priv->pattern_set = TRUE;
2837 gtk_label_recalculate (label);
2839 gtk_label_clear_layout (label);
2840 gtk_widget_queue_resize (GTK_WIDGET (label));
2845 * gtk_label_set_justify:
2846 * @label: a #GtkLabel
2847 * @jtype: a #GtkJustification
2849 * Sets the alignment of the lines in the text of the label relative to
2850 * each other. %GTK_JUSTIFY_LEFT is the default value when the
2851 * widget is first created with gtk_label_new(). If you instead want
2852 * to set the alignment of the label as a whole, use
2853 * gtk_misc_set_alignment() instead. gtk_label_set_justify() has no
2854 * effect on labels containing only a single line.
2857 gtk_label_set_justify (GtkLabel *label,
2858 GtkJustification jtype)
2860 GtkLabelPrivate *priv;
2862 g_return_if_fail (GTK_IS_LABEL (label));
2863 g_return_if_fail (jtype >= GTK_JUSTIFY_LEFT && jtype <= GTK_JUSTIFY_FILL);
2867 if ((GtkJustification) priv->jtype != jtype)
2869 priv->jtype = jtype;
2871 /* No real need to be this drastic, but easier than duplicating the code */
2872 gtk_label_clear_layout (label);
2874 g_object_notify (G_OBJECT (label), "justify");
2875 gtk_widget_queue_resize (GTK_WIDGET (label));
2880 * gtk_label_get_justify:
2881 * @label: a #GtkLabel
2883 * Returns the justification of the label. See gtk_label_set_justify().
2885 * Return value: #GtkJustification
2888 gtk_label_get_justify (GtkLabel *label)
2890 g_return_val_if_fail (GTK_IS_LABEL (label), 0);
2892 return label->priv->jtype;
2896 * gtk_label_set_ellipsize:
2897 * @label: a #GtkLabel
2898 * @mode: a #PangoEllipsizeMode
2900 * Sets the mode used to ellipsize (add an ellipsis: "...") to the text
2901 * if there is not enough space to render the entire string.
2906 gtk_label_set_ellipsize (GtkLabel *label,
2907 PangoEllipsizeMode mode)
2909 GtkLabelPrivate *priv;
2911 g_return_if_fail (GTK_IS_LABEL (label));
2912 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE && mode <= PANGO_ELLIPSIZE_END);
2916 if ((PangoEllipsizeMode) priv->ellipsize != mode)
2918 priv->ellipsize = mode;
2920 /* No real need to be this drastic, but easier than duplicating the code */
2921 gtk_label_clear_layout (label);
2923 g_object_notify (G_OBJECT (label), "ellipsize");
2924 gtk_widget_queue_resize (GTK_WIDGET (label));
2929 * gtk_label_get_ellipsize:
2930 * @label: a #GtkLabel
2932 * Returns the ellipsizing position of the label. See gtk_label_set_ellipsize().
2934 * Return value: #PangoEllipsizeMode
2939 gtk_label_get_ellipsize (GtkLabel *label)
2941 g_return_val_if_fail (GTK_IS_LABEL (label), PANGO_ELLIPSIZE_NONE);
2943 return label->priv->ellipsize;
2947 * gtk_label_set_width_chars:
2948 * @label: a #GtkLabel
2949 * @n_chars: the new desired width, in characters.
2951 * Sets the desired width in characters of @label to @n_chars.
2956 gtk_label_set_width_chars (GtkLabel *label,
2959 GtkLabelPrivate *priv;
2961 g_return_if_fail (GTK_IS_LABEL (label));
2965 if (priv->width_chars != n_chars)
2967 priv->width_chars = n_chars;
2968 g_object_notify (G_OBJECT (label), "width-chars");
2969 gtk_widget_queue_resize (GTK_WIDGET (label));
2974 * gtk_label_get_width_chars:
2975 * @label: a #GtkLabel
2977 * Retrieves the desired width of @label, in characters. See
2978 * gtk_label_set_width_chars().
2980 * Return value: the width of the label in characters.
2985 gtk_label_get_width_chars (GtkLabel *label)
2987 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2989 return label->priv->width_chars;
2993 * gtk_label_set_max_width_chars:
2994 * @label: a #GtkLabel
2995 * @n_chars: the new desired maximum width, in characters.
2997 * Sets the desired maximum width in characters of @label to @n_chars.
3002 gtk_label_set_max_width_chars (GtkLabel *label,
3005 GtkLabelPrivate *priv;
3007 g_return_if_fail (GTK_IS_LABEL (label));
3011 if (priv->max_width_chars != n_chars)
3013 priv->max_width_chars = n_chars;
3015 g_object_notify (G_OBJECT (label), "max-width-chars");
3016 gtk_widget_queue_resize (GTK_WIDGET (label));
3021 * gtk_label_get_max_width_chars:
3022 * @label: a #GtkLabel
3024 * Retrieves the desired maximum width of @label, in characters. See
3025 * gtk_label_set_width_chars().
3027 * Return value: the maximum width of the label in characters.
3032 gtk_label_get_max_width_chars (GtkLabel *label)
3034 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
3036 return label->priv->max_width_chars;
3040 * gtk_label_set_line_wrap:
3041 * @label: a #GtkLabel
3042 * @wrap: the setting
3044 * Toggles line wrapping within the #GtkLabel widget. %TRUE makes it break
3045 * lines if text exceeds the widget's size. %FALSE lets the text get cut off
3046 * by the edge of the widget if it exceeds the widget size.
3048 * Note that setting line wrapping to %TRUE does not make the label
3049 * wrap at its parent container's width, because GTK+ widgets
3050 * conceptually can't make their requisition depend on the parent
3051 * container's size. For a label that wraps at a specific position,
3052 * set the label's width using gtk_widget_set_size_request().
3055 gtk_label_set_line_wrap (GtkLabel *label,
3058 GtkLabelPrivate *priv;
3060 g_return_if_fail (GTK_IS_LABEL (label));
3064 wrap = wrap != FALSE;
3066 if (priv->wrap != wrap)
3070 gtk_label_clear_layout (label);
3071 gtk_widget_queue_resize (GTK_WIDGET (label));
3072 g_object_notify (G_OBJECT (label), "wrap");
3077 * gtk_label_get_line_wrap:
3078 * @label: a #GtkLabel
3080 * Returns whether lines in the label are automatically wrapped.
3081 * See gtk_label_set_line_wrap().
3083 * Return value: %TRUE if the lines of the label are automatically wrapped.
3086 gtk_label_get_line_wrap (GtkLabel *label)
3088 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
3090 return label->priv->wrap;
3094 * gtk_label_set_line_wrap_mode:
3095 * @label: a #GtkLabel
3096 * @wrap_mode: the line wrapping mode
3098 * If line wrapping is on (see gtk_label_set_line_wrap()) this controls how
3099 * the line wrapping is done. The default is %PANGO_WRAP_WORD which means
3100 * wrap on word boundaries.
3105 gtk_label_set_line_wrap_mode (GtkLabel *label,
3106 PangoWrapMode wrap_mode)
3108 GtkLabelPrivate *priv;
3110 g_return_if_fail (GTK_IS_LABEL (label));
3114 if (priv->wrap_mode != wrap_mode)
3116 priv->wrap_mode = wrap_mode;
3117 g_object_notify (G_OBJECT (label), "wrap-mode");
3119 gtk_widget_queue_resize (GTK_WIDGET (label));
3124 * gtk_label_get_line_wrap_mode:
3125 * @label: a #GtkLabel
3127 * Returns line wrap mode used by the label. See gtk_label_set_line_wrap_mode().
3129 * Return value: %TRUE if the lines of the label are automatically wrapped.
3134 gtk_label_get_line_wrap_mode (GtkLabel *label)
3136 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
3138 return label->priv->wrap_mode;
3142 gtk_label_destroy (GtkWidget *widget)
3144 GtkLabel *label = GTK_LABEL (widget);
3146 gtk_label_set_mnemonic_widget (label, NULL);
3148 GTK_WIDGET_CLASS (gtk_label_parent_class)->destroy (widget);
3152 gtk_label_finalize (GObject *object)
3154 GtkLabel *label = GTK_LABEL (object);
3155 GtkLabelPrivate *priv = label->priv;
3157 g_free (priv->label);
3158 g_free (priv->text);
3161 g_object_unref (priv->layout);
3164 pango_attr_list_unref (priv->attrs);
3166 if (priv->effective_attrs)
3167 pango_attr_list_unref (priv->effective_attrs);
3169 gtk_label_clear_links (label);
3170 g_free (priv->select_info);
3172 G_OBJECT_CLASS (gtk_label_parent_class)->finalize (object);
3176 gtk_label_clear_layout (GtkLabel *label)
3178 GtkLabelPrivate *priv = label->priv;
3182 g_object_unref (priv->layout);
3183 priv->layout = NULL;
3185 //gtk_label_clear_links (label);
3189 static PangoFontMetrics *
3190 get_font_metrics (PangoContext *context, GtkWidget *widget)
3192 GtkStyleContext *style_context;
3193 const PangoFontDescription *font;
3194 PangoFontMetrics *retval;
3196 style_context = gtk_widget_get_style_context (widget);
3197 font = gtk_style_context_get_font (style_context, GTK_STATE_FLAG_NORMAL);
3199 retval = pango_context_get_metrics (context,
3201 pango_context_get_language (context));
3207 * gtk_label_get_measuring_layout:
3209 * @existing_layout: %NULL or an existing layout already in use.
3210 * @width: the width to measure with in pango units, or -1 for infinite
3211 * @height: the height to measure with in pango units, or -1 for infinite
3213 * Gets a layout that can be used for measuring sizes. The returned
3214 * layout will be identical to the label's layout except for the
3215 * layout's width, which will be set to @width. Do not modify the returned
3218 * Returns: a new reference to a pango layout
3220 static PangoLayout *
3221 gtk_label_get_measuring_layout (GtkLabel * label,
3222 PangoLayout *existing_layout,
3226 GtkLabelPrivate *priv = label->priv;
3227 PangoRectangle rect;
3230 if (existing_layout != NULL)
3232 if (existing_layout != priv->layout)
3234 pango_layout_set_width (existing_layout, width);
3235 pango_layout_set_height (existing_layout, height);
3236 return existing_layout;
3239 g_object_unref (existing_layout);
3242 gtk_label_ensure_layout (label);
3244 if (pango_layout_get_width (priv->layout) == width &&
3245 pango_layout_get_height (priv->layout) == height)
3247 g_object_ref (priv->layout);
3248 return priv->layout;
3251 /* We can use the label's own layout if we're not allocated a size yet,
3252 * because we don't need it to be properly setup at that point.
3253 * This way we can make use of caching upon the label's creation.
3255 if (gtk_widget_get_allocated_width (GTK_WIDGET (label)) <= 1)
3257 g_object_ref (priv->layout);
3258 pango_layout_set_width (priv->layout, width);
3259 pango_layout_set_height (priv->layout, height);
3260 return priv->layout;
3263 /* oftentimes we want to measure a width that is far wider than the current width,
3264 * even though the layout would not change if we made it wider. In that case, we
3265 * can just return the current layout, because for measuring purposes, it will be
3268 pango_layout_get_extents (priv->layout, NULL, &rect);
3269 if ((width == -1 || rect.width <= width) &&
3270 (height == -1 || rect.height <= height) &&
3271 !pango_layout_is_wrapped (priv->layout) &&
3272 !pango_layout_is_ellipsized (priv->layout))
3274 g_object_ref (priv->layout);
3275 return priv->layout;
3278 copy = pango_layout_copy (priv->layout);
3279 pango_layout_set_width (copy, width);
3280 pango_layout_set_height (copy, height);
3285 gtk_label_update_layout_width (GtkLabel *label)
3287 GtkLabelPrivate *priv = label->priv;
3288 GtkWidget *widget = GTK_WIDGET (label);
3290 g_assert (priv->layout);
3292 if (priv->ellipsize || priv->wrap)
3294 PangoRectangle logical;
3298 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3300 width = gtk_widget_get_allocated_width (GTK_WIDGET (label)) - xpad * 2;
3301 height = gtk_widget_get_allocated_height (GTK_WIDGET (label)) - ypad * 2;
3303 if (priv->have_transform)
3305 PangoContext *context = gtk_widget_get_pango_context (widget);
3306 const PangoMatrix *matrix = pango_context_get_matrix (context);
3307 const gdouble dx = matrix->xx; /* cos (M_PI * angle / 180) */
3308 const gdouble dy = matrix->xy; /* sin (M_PI * angle / 180) */
3310 pango_layout_set_width (priv->layout, -1);
3311 pango_layout_set_height (priv->layout, -1);
3312 pango_layout_get_pixel_extents (priv->layout, NULL, &logical);
3314 if (fabs (dy) < 0.01)
3316 if (logical.width > width)
3317 pango_layout_set_width (priv->layout, width * PANGO_SCALE);
3319 else if (fabs (dx) < 0.01)
3321 if (logical.width > height)
3322 pango_layout_set_width (priv->layout, height * PANGO_SCALE);
3326 gdouble x0, y0, x1, y1, length;
3331 y0 = dx ? x0 * dy / dx : G_MAXDOUBLE;
3332 vertical = fabs (y0) > height / 2;
3337 x0 = dy ? y0 * dx / dy : G_MAXDOUBLE;
3340 length = 2 * sqrt (x0 * x0 + y0 * y0);
3341 pango_layout_set_width (priv->layout, rint (length * PANGO_SCALE));
3342 pango_layout_get_pixel_size (priv->layout, NULL, &cy);
3349 y0 = height/2 + y1 - y0;
3354 x0 = width/2 + x1 - x0;
3358 length = length - sqrt (x0 * x0 + y0 * y0) * 2;
3359 pango_layout_set_width (priv->layout, rint (length * PANGO_SCALE));
3364 pango_layout_set_width (priv->layout, width * PANGO_SCALE);
3365 pango_layout_set_height (priv->layout, priv->ellipsize ? height * PANGO_SCALE : -1);
3370 pango_layout_set_width (priv->layout, -1);
3371 pango_layout_set_height (priv->layout, -1);
3376 gtk_label_ensure_layout (GtkLabel *label)
3378 GtkLabelPrivate *priv = label->priv;
3382 widget = GTK_WIDGET (label);
3384 rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
3388 PangoAlignment align = PANGO_ALIGN_LEFT; /* Quiet gcc */
3389 gdouble angle = gtk_label_get_angle (label);
3391 if (angle != 0.0 && !priv->select_info)
3393 PangoMatrix matrix = PANGO_MATRIX_INIT;
3395 /* We rotate the standard singleton PangoContext for the widget,
3396 * depending on the fact that it's meant pretty much exclusively
3399 pango_matrix_rotate (&matrix, angle);
3401 pango_context_set_matrix (gtk_widget_get_pango_context (widget), &matrix);
3403 priv->have_transform = TRUE;
3407 if (priv->have_transform)
3408 pango_context_set_matrix (gtk_widget_get_pango_context (widget), NULL);
3410 priv->have_transform = FALSE;
3413 priv->layout = gtk_widget_create_pango_layout (widget, priv->text);
3415 if (priv->effective_attrs)
3416 pango_layout_set_attributes (priv->layout, priv->effective_attrs);
3418 gtk_label_rescan_links (label);
3420 switch (priv->jtype)
3422 case GTK_JUSTIFY_LEFT:
3423 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3425 case GTK_JUSTIFY_RIGHT:
3426 align = rtl ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
3428 case GTK_JUSTIFY_CENTER:
3429 align = PANGO_ALIGN_CENTER;
3431 case GTK_JUSTIFY_FILL:
3432 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3433 pango_layout_set_justify (priv->layout, TRUE);
3436 g_assert_not_reached();
3439 pango_layout_set_alignment (priv->layout, align);
3440 pango_layout_set_ellipsize (priv->layout, priv->ellipsize);
3441 pango_layout_set_wrap (priv->layout, priv->wrap_mode);
3442 pango_layout_set_single_paragraph_mode (priv->layout, priv->single_line_mode);
3444 gtk_label_update_layout_width (label);
3449 get_single_line_height (GtkWidget *widget,
3450 PangoLayout *layout)
3452 PangoContext *context;
3453 PangoFontMetrics *metrics;
3454 gint ascent, descent;
3456 context = pango_layout_get_context (layout);
3457 metrics = get_font_metrics (context, widget);
3458 ascent = pango_font_metrics_get_ascent (metrics);
3459 descent = pango_font_metrics_get_descent (metrics);
3460 pango_font_metrics_unref (metrics);
3462 return ascent + descent;
3465 static GtkSizeRequestMode
3466 gtk_label_get_request_mode (GtkWidget *widget)
3468 GtkLabel *label = GTK_LABEL (widget);
3469 gdouble angle = gtk_label_get_angle (label);
3471 if (label->priv->wrap)
3472 return (angle == 90 || angle == 270) ?
3473 GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT :
3474 GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
3476 return GTK_SIZE_REQUEST_CONSTANT_SIZE;
3480 get_size_for_allocation (GtkLabel *label,
3481 GtkOrientation orientation,
3486 GtkLabelPrivate *priv = label->priv;
3487 PangoLayout *layout;
3490 layout = gtk_label_get_measuring_layout (label, NULL, allocation * PANGO_SCALE, -1);
3492 pango_layout_get_pixel_size (layout, NULL, &text_height);
3495 *minimum_size = text_height;
3499 if (priv->ellipsize && priv->wrap)
3501 layout = gtk_label_get_measuring_layout (label, layout, allocation * PANGO_SCALE, G_MAXINT);
3502 pango_layout_get_pixel_size (layout, NULL, &text_height);
3505 *natural_size = text_height;
3508 g_object_unref (layout);
3512 get_char_pixels (GtkWidget *label,
3513 PangoLayout *layout)
3515 PangoContext *context;
3516 PangoFontMetrics *metrics;
3517 gint char_width, digit_width;
3519 context = pango_layout_get_context (layout);
3520 metrics = get_font_metrics (context, GTK_WIDGET (label));
3521 char_width = pango_font_metrics_get_approximate_char_width (metrics);
3522 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
3523 pango_font_metrics_unref (metrics);
3525 return MAX (char_width, digit_width);;
3529 gtk_label_get_preferred_layout_size (GtkLabel *label,
3530 PangoRectangle *required,
3531 PangoRectangle *natural)
3533 GtkLabelPrivate *priv = label->priv;
3534 PangoLayout *layout;
3536 /* "width-chars" Hard-coded minimum width:
3537 * - minimum size should be MAX (width-chars, strlen ("..."));
3538 * - natural size should be MAX (width-chars, strlen (priv->text));
3540 * "max-width-chars" User specified maximum size requisition
3541 * - minimum size should be MAX (width-chars, 0)
3542 * - natural size should be MIN (max-width-chars, strlen (priv->text))
3544 * For ellipsizing labels; if max-width-chars is specified: either it is used as
3545 * a minimum size or the label text as a minimum size (natural size still overflows).
3547 * For wrapping labels; A reasonable minimum size is useful to naturally layout
3548 * interfaces automatically. In this case if no "width-chars" is specified, the minimum
3549 * width will default to the wrap guess that gtk_label_ensure_layout() does.
3552 /* Start off with the pixel extents of an as-wide-as-possible layout */
3553 layout = gtk_label_get_measuring_layout (label, NULL, -1, -1);
3555 pango_layout_get_extents (layout, NULL, natural);
3556 natural->x = natural->y = 0;
3559 natural->height = get_single_line_height (GTK_WIDGET (label), layout);
3561 if (priv->ellipsize || priv->wrap)
3563 /* a layout with width 0 will be as small as humanly possible */
3564 layout = gtk_label_get_measuring_layout (label, layout, 0, -1);
3566 pango_layout_get_extents (layout, NULL, required);
3568 /* can happen when Pango decides to ellipsize text */
3569 if (required->width > natural->width)
3570 required->width = natural->width;
3572 required->x = required->y = 0;
3573 required->height = natural->height;
3577 *required = *natural;
3580 if (priv->width_chars > -1 || priv->max_width_chars > -1)
3584 char_pixels = get_char_pixels (GTK_WIDGET (label), layout);
3586 if (priv->width_chars > -1)
3587 required->width = MAX (required->width, char_pixels * priv->width_chars);
3589 if (priv->max_width_chars > -1)
3590 natural->width = MIN (natural->width, priv->max_width_chars * char_pixels);
3591 natural->width = MAX (natural->width, required->width);
3594 g_object_unref (layout);
3598 gtk_label_get_preferred_size (GtkWidget *widget,
3599 GtkOrientation orientation,
3603 GtkLabel *label = GTK_LABEL (widget);
3604 GtkLabelPrivate *priv = label->priv;
3605 PangoRectangle required_rect;
3606 PangoRectangle natural_rect;
3609 gtk_label_get_preferred_layout_size (label, &required_rect, &natural_rect);
3611 /* Now that we have minimum and natural sizes in pango extents, apply a possible transform */
3612 if (priv->have_transform)
3615 PangoContext *context;
3616 const PangoMatrix *matrix;
3618 copy = pango_layout_copy (priv->layout);
3619 context = pango_layout_get_context (copy);
3620 matrix = pango_context_get_matrix (context);
3622 pango_layout_set_width (copy, -1);
3623 pango_layout_set_ellipsize (copy, PANGO_ELLIPSIZE_NONE);
3625 pango_layout_get_extents (copy, NULL, &natural_rect);
3626 g_object_unref (copy);
3628 pango_matrix_transform_rectangle (matrix, &required_rect);
3629 pango_matrix_transform_rectangle (matrix, &natural_rect);
3631 /* Bump the natural size in case of ellipsize to ensure pango has
3632 * enough space in the angles (note, we could alternatively set the
3633 * layout to not ellipsize when we know we have been allocated our
3634 * full natural size, or it may be that pango needs a fix here).
3636 if (priv->ellipsize && priv->angle != 0 && priv->angle != 90 &&
3637 priv->angle != 180 && priv->angle != 270 && priv->angle != 360)
3639 /* For some reason we only need this at about 110 degrees, and only
3640 * when gaining in height
3642 natural_rect.height += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3643 natural_rect.width += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3647 required_rect.width = PANGO_PIXELS_CEIL (required_rect.width);
3648 required_rect.height = PANGO_PIXELS_CEIL (required_rect.height);
3650 natural_rect.width = PANGO_PIXELS_CEIL (natural_rect.width);
3651 natural_rect.height = PANGO_PIXELS_CEIL (natural_rect.height);
3653 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3655 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3657 /* Note, we cant use get_size_for_allocation() when rotating
3658 * ellipsized labels.
3660 if (!(priv->ellipsize && priv->have_transform) &&
3661 (priv->angle == 90 || priv->angle == 270))
3663 /* Doing a h4w request on a rotated label here, return the
3664 * required width for the minimum height.
3666 get_size_for_allocation (label,
3667 GTK_ORIENTATION_VERTICAL,
3668 required_rect.height,
3669 minimum_size, natural_size);
3674 /* Normal desired width */
3675 *minimum_size = required_rect.width;
3676 *natural_size = natural_rect.width;
3679 *minimum_size += xpad * 2;
3680 *natural_size += xpad * 2;
3682 else /* GTK_ORIENTATION_VERTICAL */
3684 /* Note, we cant use get_size_for_allocation() when rotating
3685 * ellipsized labels.
3687 if (!(priv->ellipsize && priv->have_transform) &&
3688 (priv->angle == 0 || priv->angle == 180 || priv->angle == 360))
3690 /* Doing a w4h request on a label here, return the required
3691 * height for the minimum width.
3693 get_size_for_allocation (label,
3694 GTK_ORIENTATION_HORIZONTAL,
3695 required_rect.width,
3696 minimum_size, natural_size);
3700 /* A vertically rotated label does w4h, so return the base
3701 * desired height (text length)
3703 *minimum_size = required_rect.height;
3704 *natural_size = natural_rect.height;
3707 *minimum_size += ypad * 2;
3708 *natural_size += ypad * 2;
3714 gtk_label_get_preferred_width (GtkWidget *widget,
3718 gtk_label_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
3722 gtk_label_get_preferred_height (GtkWidget *widget,
3726 gtk_label_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
3730 gtk_label_get_preferred_width_for_height (GtkWidget *widget,
3732 gint *minimum_width,
3733 gint *natural_width)
3735 GtkLabel *label = GTK_LABEL (widget);
3736 GtkLabelPrivate *priv = label->priv;
3738 if (priv->wrap && (priv->angle == 90 || priv->angle == 270))
3742 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3745 gtk_label_clear_layout (label);
3747 get_size_for_allocation (label, GTK_ORIENTATION_VERTICAL,
3748 MAX (1, height - (ypad * 2)),
3749 minimum_width, natural_width);
3752 *minimum_width += xpad * 2;
3755 *natural_width += xpad * 2;
3758 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_width, natural_width);
3762 gtk_label_get_preferred_height_for_width (GtkWidget *widget,
3764 gint *minimum_height,
3765 gint *natural_height)
3767 GtkLabel *label = GTK_LABEL (widget);
3768 GtkLabelPrivate *priv = label->priv;
3770 if (priv->wrap && (priv->angle == 0 || priv->angle == 180 || priv->angle == 360))
3774 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3777 gtk_label_clear_layout (label);
3779 get_size_for_allocation (label, GTK_ORIENTATION_HORIZONTAL,
3780 MAX (1, width - xpad * 2),
3781 minimum_height, natural_height);
3784 *minimum_height += ypad * 2;
3787 *natural_height += ypad * 2;
3790 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, minimum_height, natural_height);
3794 gtk_label_size_allocate (GtkWidget *widget,
3795 GtkAllocation *allocation)
3797 GtkLabel *label = GTK_LABEL (widget);
3798 GtkLabelPrivate *priv = label->priv;
3800 GTK_WIDGET_CLASS (gtk_label_parent_class)->size_allocate (widget, allocation);
3803 gtk_label_update_layout_width (label);
3805 if (priv->select_info && priv->select_info->window)
3807 gdk_window_move_resize (priv->select_info->window,
3811 allocation->height);
3816 gtk_label_update_cursor (GtkLabel *label)
3818 GtkLabelPrivate *priv = label->priv;
3821 if (!priv->select_info)
3824 widget = GTK_WIDGET (label);
3826 if (gtk_widget_get_realized (widget))
3828 GdkDisplay *display;
3831 if (gtk_widget_is_sensitive (widget))
3833 display = gtk_widget_get_display (widget);
3835 if (priv->select_info->active_link)
3836 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
3837 else if (priv->select_info->selectable)
3838 cursor = gdk_cursor_new_for_display (display, GDK_XTERM);
3845 gdk_window_set_cursor (priv->select_info->window, cursor);
3848 g_object_unref (cursor);
3853 gtk_label_state_changed (GtkWidget *widget,
3854 GtkStateType prev_state)
3856 GtkLabel *label = GTK_LABEL (widget);
3857 GtkLabelPrivate *priv = label->priv;
3859 if (priv->select_info)
3861 if (!gtk_widget_is_sensitive (widget))
3862 gtk_label_select_region (label, 0, 0);
3864 gtk_label_update_cursor (label);
3867 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed)
3868 GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed (widget, prev_state);
3872 gtk_label_style_updated (GtkWidget *widget)
3874 GtkLabel *label = GTK_LABEL (widget);
3876 GTK_WIDGET_CLASS (gtk_label_parent_class)->style_updated (widget);
3878 /* We have to clear the layout, fonts etc. may have changed */
3879 gtk_label_clear_layout (label);
3883 gtk_label_direction_changed (GtkWidget *widget,
3884 GtkTextDirection previous_dir)
3886 GtkLabel *label = GTK_LABEL (widget);
3887 GtkLabelPrivate *priv = label->priv;
3890 pango_layout_context_changed (priv->layout);
3892 GTK_WIDGET_CLASS (gtk_label_parent_class)->direction_changed (widget, previous_dir);
3896 get_layout_location (GtkLabel *label,
3900 GtkAllocation allocation;
3903 GtkLabelPrivate *priv;
3904 gint req_width, x, y;
3907 gfloat xalign, yalign;
3908 PangoRectangle logical;
3910 misc = GTK_MISC (label);
3911 widget = GTK_WIDGET (label);
3914 gtk_misc_get_alignment (misc, &xalign, &yalign);
3915 gtk_misc_get_padding (misc, &xpad, &ypad);
3917 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
3918 xalign = 1.0 - xalign;
3920 pango_layout_get_extents (priv->layout, NULL, &logical);
3922 if (priv->have_transform)
3924 PangoContext *context = gtk_widget_get_pango_context (widget);
3925 const PangoMatrix *matrix = pango_context_get_matrix (context);
3926 pango_matrix_transform_rectangle (matrix, &logical);
3929 pango_extents_to_pixels (&logical, NULL);
3931 req_width = logical.width;
3932 req_height = logical.height;
3934 req_width += 2 * xpad;
3935 req_height += 2 * ypad;
3937 gtk_widget_get_allocation (widget, &allocation);
3939 x = floor (allocation.x + xpad + xalign * (allocation.width - req_width) - logical.x);
3942 /* bgo#315462 - For single-line labels, *do* align the requisition with
3943 * respect to the allocation, even if we are under-allocated. For multi-line
3944 * labels, always show the top of the text when they are under-allocated. The
3945 * rationale is this:
3947 * - Single-line labels appear in GtkButtons, and it is very easy to get them
3948 * to be smaller than their requisition. The button may clip the label, but
3949 * the label will still be able to show most of itself and the focus
3950 * rectangle. Also, it is fairly easy to read a single line of clipped text.
3952 * - Multi-line labels should not be clipped to showing "something in the
3953 * middle". You want to read the first line, at least, to get some context.
3955 if (pango_layout_get_line_count (priv->layout) == 1)
3956 y = floor (allocation.y + ypad + (allocation.height - req_height) * yalign) - logical.y;
3958 y = floor (allocation.y + ypad + MAX ((allocation.height - req_height) * yalign, 0)) - logical.y;
3968 draw_insertion_cursor (GtkLabel *label,
3970 GdkRectangle *cursor_location,
3971 gboolean is_primary,
3972 PangoDirection direction,
3973 gboolean draw_arrow)
3975 GtkWidget *widget = GTK_WIDGET (label);
3976 GtkTextDirection text_dir;
3978 if (direction == PANGO_DIRECTION_LTR)
3979 text_dir = GTK_TEXT_DIR_LTR;
3981 text_dir = GTK_TEXT_DIR_RTL;
3983 gtk_draw_insertion_cursor (widget, cr, cursor_location,
3984 is_primary, text_dir, draw_arrow);
3987 static PangoDirection
3988 get_cursor_direction (GtkLabel *label)
3990 GtkLabelPrivate *priv = label->priv;
3993 g_assert (priv->select_info);
3995 gtk_label_ensure_layout (label);
3997 for (l = pango_layout_get_lines_readonly (priv->layout); l; l = l->next)
3999 PangoLayoutLine *line = l->data;
4001 /* If priv->select_info->selection_end is at the very end of
4002 * the line, we don't know if the cursor is on this line or
4003 * the next without looking ahead at the next line. (End
4004 * of paragraph is different from line break.) But it's
4005 * definitely in this paragraph, which is good enough
4006 * to figure out the resolved direction.
4008 if (line->start_index + line->length >= priv->select_info->selection_end)
4009 return line->resolved_dir;
4012 return PANGO_DIRECTION_LTR;
4016 gtk_label_draw_cursor (GtkLabel *label, cairo_t *cr, gint xoffset, gint yoffset)
4018 GtkLabelPrivate *priv = label->priv;
4021 if (priv->select_info == NULL)
4024 widget = GTK_WIDGET (label);
4026 if (gtk_widget_is_drawable (widget))
4028 PangoDirection keymap_direction;
4029 PangoDirection cursor_direction;
4030 PangoRectangle strong_pos, weak_pos;
4031 gboolean split_cursor;
4032 PangoRectangle *cursor1 = NULL;
4033 PangoRectangle *cursor2 = NULL;
4034 GdkRectangle cursor_location;
4035 PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
4036 PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
4038 keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gtk_widget_get_display (widget)));
4039 cursor_direction = get_cursor_direction (label);
4041 gtk_label_ensure_layout (label);
4043 pango_layout_get_cursor_pos (priv->layout, priv->select_info->selection_end,
4044 &strong_pos, &weak_pos);
4046 g_object_get (gtk_widget_get_settings (widget),
4047 "gtk-split-cursor", &split_cursor,
4050 dir1 = cursor_direction;
4054 cursor1 = &strong_pos;
4056 if (strong_pos.x != weak_pos.x ||
4057 strong_pos.y != weak_pos.y)
4059 dir2 = (cursor_direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
4060 cursor2 = &weak_pos;
4065 if (keymap_direction == cursor_direction)
4066 cursor1 = &strong_pos;
4068 cursor1 = &weak_pos;
4071 cursor_location.x = xoffset + PANGO_PIXELS (cursor1->x);
4072 cursor_location.y = yoffset + PANGO_PIXELS (cursor1->y);
4073 cursor_location.width = 0;
4074 cursor_location.height = PANGO_PIXELS (cursor1->height);
4076 draw_insertion_cursor (label, cr,
4077 &cursor_location, TRUE, dir1,
4078 dir2 != PANGO_DIRECTION_NEUTRAL);
4080 if (dir2 != PANGO_DIRECTION_NEUTRAL)
4082 cursor_location.x = xoffset + PANGO_PIXELS (cursor2->x);
4083 cursor_location.y = yoffset + PANGO_PIXELS (cursor2->y);
4084 cursor_location.width = 0;
4085 cursor_location.height = PANGO_PIXELS (cursor2->height);
4087 draw_insertion_cursor (label, cr,
4088 &cursor_location, FALSE, dir2,
4094 static GtkLabelLink *
4095 gtk_label_get_focus_link (GtkLabel *label)
4097 GtkLabelPrivate *priv = label->priv;
4098 GtkLabelSelectionInfo *info = priv->select_info;
4104 if (info->selection_anchor != info->selection_end)
4107 for (l = info->links; l; l = l->next)
4109 GtkLabelLink *link = l->data;
4110 if (link->start <= info->selection_anchor &&
4111 info->selection_anchor <= link->end)
4119 gtk_label_draw (GtkWidget *widget,
4122 GtkLabel *label = GTK_LABEL (widget);
4123 GtkLabelPrivate *priv = label->priv;
4124 GtkLabelSelectionInfo *info = priv->select_info;
4125 GtkAllocation allocation;
4126 GtkStyleContext *context;
4127 GtkStateFlags state;
4130 gtk_label_ensure_layout (label);
4132 if (priv->text && (*priv->text != '\0'))
4134 GdkRGBA bg_color, fg_color;
4136 get_layout_location (label, &x, &y);
4138 context = gtk_widget_get_style_context (widget);
4139 gtk_widget_get_allocation (widget, &allocation);
4141 cairo_translate (cr, -allocation.x, -allocation.y);
4143 state = gtk_widget_get_state_flags (widget);
4144 gtk_style_context_set_state (context, state);
4146 gtk_render_layout (context, cr,
4151 (info->selection_anchor != info->selection_end))
4154 cairo_region_t *clip;
4156 range[0] = info->selection_anchor;
4157 range[1] = info->selection_end;
4159 if (range[0] > range[1])
4161 gint tmp = range[0];
4162 range[0] = range[1];
4166 clip = gdk_pango_layout_get_clip_region (priv->layout,
4171 /* FIXME should use gtk_paint, but it can't use a clip region */
4174 gdk_cairo_region (cr, clip);
4177 state = GTK_STATE_FLAG_SELECTED;
4179 if (gtk_widget_has_focus (widget))
4180 state |= GTK_STATE_FLAG_FOCUSED;
4182 gtk_style_context_get_color (context, state, &fg_color);
4183 gtk_style_context_get_background_color (context, state, &bg_color);
4185 gdk_cairo_set_source_rgba (cr, &bg_color);
4188 gdk_cairo_set_source_rgba (cr, &fg_color);
4189 cairo_move_to (cr, x, y);
4190 _gtk_pango_fill_layout (cr, priv->layout);
4193 cairo_region_destroy (clip);
4197 GtkLabelLink *focus_link;
4198 GtkLabelLink *active_link;
4200 cairo_region_t *clip;
4202 GdkColor *text_color;
4203 GdkColor *link_color;
4204 GdkColor *visited_link_color;
4206 if (info->selectable && gtk_widget_has_focus (widget))
4207 gtk_label_draw_cursor (label, cr, x, y);
4209 focus_link = gtk_label_get_focus_link (label);
4210 active_link = info->active_link;
4216 range[0] = active_link->start;
4217 range[1] = active_link->end;
4221 clip = gdk_pango_layout_get_clip_region (priv->layout,
4225 gdk_cairo_region (cr, clip);
4227 cairo_region_destroy (clip);
4229 gtk_label_get_link_colors (widget, &link_color, &visited_link_color);
4230 if (active_link->visited)
4231 text_color = visited_link_color;
4233 text_color = link_color;
4235 if (info->link_clicked)
4236 state = GTK_STATE_FLAG_ACTIVE;
4238 state = GTK_STATE_FLAG_PRELIGHT;
4240 gtk_style_context_get_background_color (context, state, &bg_color);
4242 gdk_cairo_set_source_rgba (cr, &bg_color);
4245 gdk_cairo_set_source_color (cr, text_color);
4246 cairo_move_to (cr, x, y);
4247 _gtk_pango_fill_layout (cr, priv->layout);
4249 gdk_color_free (link_color);
4250 gdk_color_free (visited_link_color);
4255 if (focus_link && gtk_widget_has_visible_focus (widget))
4257 range[0] = focus_link->start;
4258 range[1] = focus_link->end;
4260 clip = gdk_pango_layout_get_clip_region (priv->layout,
4264 cairo_region_get_extents (clip, &rect);
4266 state = gtk_widget_get_state_flags (widget);
4267 gtk_style_context_set_state (context, state);
4269 gtk_render_focus (context, cr,
4271 rect.width, rect.height);
4273 cairo_region_destroy (clip);
4282 separate_uline_pattern (const gchar *str,
4287 gboolean underscore;
4290 gchar *pattern_dest;
4292 *accel_key = GDK_KEY_VoidSymbol;
4293 *new_str = g_new (gchar, strlen (str) + 1);
4294 *pattern = g_new (gchar, g_utf8_strlen (str, -1) + 1);
4300 pattern_dest = *pattern;
4305 const gchar *next_src;
4307 c = g_utf8_get_char (src);
4308 if (c == (gunichar)-1)
4310 g_warning ("Invalid input string");
4316 next_src = g_utf8_next_char (src);
4321 *pattern_dest++ = ' ';
4324 *pattern_dest++ = '_';
4325 if (*accel_key == GDK_KEY_VoidSymbol)
4326 *accel_key = gdk_keyval_to_lower (gdk_unicode_to_keyval (c));
4329 while (src < next_src)
4343 while (src < next_src)
4346 *pattern_dest++ = ' ';
4358 gtk_label_set_uline_text_internal (GtkLabel *label,
4361 GtkLabelPrivate *priv = label->priv;
4362 guint accel_key = GDK_KEY_VoidSymbol;
4366 g_return_if_fail (GTK_IS_LABEL (label));
4367 g_return_if_fail (str != NULL);
4369 /* Split text into the base text and a separate pattern
4372 if (!separate_uline_pattern (str, &accel_key, &new_str, &pattern))
4375 gtk_label_set_text_internal (label, new_str);
4376 gtk_label_set_pattern_internal (label, pattern, TRUE);
4377 priv->mnemonic_keyval = accel_key;
4383 * gtk_label_set_text_with_mnemonic:
4384 * @label: a #GtkLabel
4387 * Sets the label's text from the string @str.
4388 * If characters in @str are preceded by an underscore, they are underlined
4389 * indicating that they represent a keyboard accelerator called a mnemonic.
4390 * The mnemonic key can be used to activate another widget, chosen
4391 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
4394 gtk_label_set_text_with_mnemonic (GtkLabel *label,
4397 g_return_if_fail (GTK_IS_LABEL (label));
4398 g_return_if_fail (str != NULL);
4400 g_object_freeze_notify (G_OBJECT (label));
4402 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
4403 gtk_label_set_use_markup_internal (label, FALSE);
4404 gtk_label_set_use_underline_internal (label, TRUE);
4406 gtk_label_recalculate (label);
4408 g_object_thaw_notify (G_OBJECT (label));
4412 gtk_label_realize (GtkWidget *widget)
4414 GtkLabel *label = GTK_LABEL (widget);
4415 GtkLabelPrivate *priv = label->priv;
4417 GTK_WIDGET_CLASS (gtk_label_parent_class)->realize (widget);
4419 if (priv->select_info)
4420 gtk_label_create_window (label);
4424 gtk_label_unrealize (GtkWidget *widget)
4426 GtkLabel *label = GTK_LABEL (widget);
4427 GtkLabelPrivate *priv = label->priv;
4429 if (priv->select_info)
4430 gtk_label_destroy_window (label);
4432 GTK_WIDGET_CLASS (gtk_label_parent_class)->unrealize (widget);
4436 gtk_label_map (GtkWidget *widget)
4438 GtkLabel *label = GTK_LABEL (widget);
4439 GtkLabelPrivate *priv = label->priv;
4441 GTK_WIDGET_CLASS (gtk_label_parent_class)->map (widget);
4443 if (priv->select_info)
4444 gdk_window_show (priv->select_info->window);
4448 gtk_label_unmap (GtkWidget *widget)
4450 GtkLabel *label = GTK_LABEL (widget);
4451 GtkLabelPrivate *priv = label->priv;
4453 if (priv->select_info)
4454 gdk_window_hide (priv->select_info->window);
4456 GTK_WIDGET_CLASS (gtk_label_parent_class)->unmap (widget);
4460 window_to_layout_coords (GtkLabel *label,
4464 GtkAllocation allocation;
4468 widget = GTK_WIDGET (label);
4470 /* get layout location in widget->window coords */
4471 get_layout_location (label, &lx, &ly);
4473 gtk_widget_get_allocation (widget, &allocation);
4477 *x += allocation.x; /* go to widget->window */
4478 *x -= lx; /* go to layout */
4483 *y += allocation.y; /* go to widget->window */
4484 *y -= ly; /* go to layout */
4490 layout_to_window_coords (GtkLabel *label,
4497 widget = GTK_WIDGET (label);
4499 /* get layout location in widget->window coords */
4500 get_layout_location (label, &lx, &ly);
4504 *x += lx; /* go to widget->window */
4505 *x -= widget->allocation.x; /* go to selection window */
4510 *y += ly; /* go to widget->window */
4511 *y -= widget->allocation.y; /* go to selection window */
4517 get_layout_index (GtkLabel *label,
4522 GtkLabelPrivate *priv = label->priv;
4524 const gchar *cluster;
4525 const gchar *cluster_end;
4530 gtk_label_ensure_layout (label);
4532 window_to_layout_coords (label, &x, &y);
4537 inside = pango_layout_xy_to_index (priv->layout,
4541 cluster = priv->text + *index;
4542 cluster_end = cluster;
4545 cluster_end = g_utf8_next_char (cluster_end);
4549 *index += (cluster_end - cluster);
4555 gtk_label_select_word (GtkLabel *label)
4557 GtkLabelPrivate *priv = label->priv;
4560 gint start_index = gtk_label_move_backward_word (label, priv->select_info->selection_end);
4561 gint end_index = gtk_label_move_forward_word (label, priv->select_info->selection_end);
4563 min = MIN (priv->select_info->selection_anchor,
4564 priv->select_info->selection_end);
4565 max = MAX (priv->select_info->selection_anchor,
4566 priv->select_info->selection_end);
4568 min = MIN (min, start_index);
4569 max = MAX (max, end_index);
4571 gtk_label_select_region_index (label, min, max);
4575 gtk_label_grab_focus (GtkWidget *widget)
4577 GtkLabel *label = GTK_LABEL (widget);
4578 GtkLabelPrivate *priv = label->priv;
4579 gboolean select_on_focus;
4582 if (priv->select_info == NULL)
4585 GTK_WIDGET_CLASS (gtk_label_parent_class)->grab_focus (widget);
4587 if (priv->select_info->selectable)
4589 g_object_get (gtk_widget_get_settings (widget),
4590 "gtk-label-select-on-focus",
4594 if (select_on_focus && !priv->in_click)
4595 gtk_label_select_region (label, 0, -1);
4599 if (priv->select_info->links && !priv->in_click)
4601 link = priv->select_info->links->data;
4602 priv->select_info->selection_anchor = link->start;
4603 priv->select_info->selection_end = link->start;
4609 gtk_label_focus (GtkWidget *widget,
4610 GtkDirectionType direction)
4612 GtkLabel *label = GTK_LABEL (widget);
4613 GtkLabelPrivate *priv = label->priv;
4614 GtkLabelSelectionInfo *info = priv->select_info;
4615 GtkLabelLink *focus_link;
4618 if (!gtk_widget_is_focus (widget))
4620 gtk_widget_grab_focus (widget);
4623 focus_link = gtk_label_get_focus_link (label);
4624 if (focus_link && direction == GTK_DIR_TAB_BACKWARD)
4626 l = g_list_last (info->links);
4627 focus_link = l->data;
4628 info->selection_anchor = focus_link->start;
4629 info->selection_end = focus_link->start;
4639 if (info->selectable)
4643 if (info->selection_anchor != info->selection_end)
4646 index = info->selection_anchor;
4648 if (direction == GTK_DIR_TAB_FORWARD)
4649 for (l = info->links; l; l = l->next)
4651 GtkLabelLink *link = l->data;
4653 if (link->start > index)
4655 gtk_label_select_region_index (label, link->start, link->start);
4659 else if (direction == GTK_DIR_TAB_BACKWARD)
4660 for (l = g_list_last (info->links); l; l = l->prev)
4662 GtkLabelLink *link = l->data;
4664 if (link->end < index)
4666 gtk_label_select_region_index (label, link->start, link->start);
4675 focus_link = gtk_label_get_focus_link (label);
4678 case GTK_DIR_TAB_FORWARD:
4681 l = g_list_find (info->links, focus_link);
4688 case GTK_DIR_TAB_BACKWARD:
4691 l = g_list_find (info->links, focus_link);
4695 l = g_list_last (info->links);
4704 focus_link = l->data;
4705 info->selection_anchor = focus_link->start;
4706 info->selection_end = focus_link->start;
4707 gtk_widget_queue_draw (widget);
4719 gtk_label_button_press (GtkWidget *widget,
4720 GdkEventButton *event)
4722 GtkLabel *label = GTK_LABEL (widget);
4723 GtkLabelPrivate *priv = label->priv;
4724 GtkLabelSelectionInfo *info = priv->select_info;
4731 if (info->active_link)
4733 if (event->button == 1)
4735 info->link_clicked = 1;
4736 gtk_widget_queue_draw (widget);
4738 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4740 info->link_clicked = 1;
4741 gtk_label_do_popup (label, event);
4746 if (!info->selectable)
4749 info->in_drag = FALSE;
4750 info->select_words = FALSE;
4752 if (event->button == 1)
4754 if (!gtk_widget_has_focus (widget))
4756 priv->in_click = TRUE;
4757 gtk_widget_grab_focus (widget);
4758 priv->in_click = FALSE;
4761 if (event->type == GDK_3BUTTON_PRESS)
4763 gtk_label_select_region_index (label, 0, strlen (priv->text));
4767 if (event->type == GDK_2BUTTON_PRESS)
4769 info->select_words = TRUE;
4770 gtk_label_select_word (label);
4774 get_layout_index (label, event->x, event->y, &index);
4776 min = MIN (info->selection_anchor, info->selection_end);
4777 max = MAX (info->selection_anchor, info->selection_end);
4779 if ((info->selection_anchor != info->selection_end) &&
4780 (event->state & GDK_SHIFT_MASK))
4782 if (index > min && index < max)
4784 /* truncate selection, but keep it as big as possible */
4785 if (index - min > max - index)
4792 /* extend (same as motion) */
4793 min = MIN (min, index);
4794 max = MAX (max, index);
4797 /* ensure the anchor is opposite index */
4805 gtk_label_select_region_index (label, min, max);
4809 if (event->type == GDK_3BUTTON_PRESS)
4810 gtk_label_select_region_index (label, 0, strlen (priv->text));
4811 else if (event->type == GDK_2BUTTON_PRESS)
4812 gtk_label_select_word (label);
4813 else if (min < max && min <= index && index <= max)
4815 info->in_drag = TRUE;
4816 info->drag_start_x = event->x;
4817 info->drag_start_y = event->y;
4820 /* start a replacement */
4821 gtk_label_select_region_index (label, index, index);
4826 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4828 gtk_label_do_popup (label, event);
4836 gtk_label_button_release (GtkWidget *widget,
4837 GdkEventButton *event)
4840 GtkLabel *label = GTK_LABEL (widget);
4841 GtkLabelPrivate *priv = label->priv;
4842 GtkLabelSelectionInfo *info = priv->select_info;
4852 get_layout_index (label, event->x, event->y, &index);
4853 gtk_label_select_region_index (label, index, index);
4858 if (event->button != 1)
4861 if (info->active_link &&
4862 info->selection_anchor == info->selection_end &&
4865 emit_activate_link (label, info->active_link);
4866 info->link_clicked = 0;
4871 /* The goal here is to return TRUE iff we ate the
4872 * button press to start selecting.
4878 connect_mnemonics_visible_notify (GtkLabel *label)
4880 GtkLabelPrivate *priv = label->priv;
4881 GtkWidget *toplevel;
4884 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
4886 if (!GTK_IS_WINDOW (toplevel))
4889 /* always set up this widgets initial value */
4890 priv->mnemonics_visible =
4891 gtk_window_get_mnemonics_visible (GTK_WINDOW (toplevel));
4894 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (toplevel),
4895 "gtk-label-mnemonics-visible-connected"));
4899 g_signal_connect (toplevel,
4900 "notify::mnemonics-visible",
4901 G_CALLBACK (label_mnemonics_visible_changed),
4903 g_object_set_data (G_OBJECT (toplevel),
4904 "gtk-label-mnemonics-visible-connected",
4905 GINT_TO_POINTER (1));
4910 drag_begin_cb (GtkWidget *widget,
4911 GdkDragContext *context,
4914 GtkLabel *label = GTK_LABEL (widget);
4915 GtkLabelPrivate *priv = label->priv;
4916 cairo_surface_t *surface = NULL;
4918 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
4920 if ((priv->select_info->selection_anchor !=
4921 priv->select_info->selection_end) &&
4927 start = MIN (priv->select_info->selection_anchor,
4928 priv->select_info->selection_end);
4929 end = MAX (priv->select_info->selection_anchor,
4930 priv->select_info->selection_end);
4932 len = strlen (priv->text);
4940 surface = _gtk_text_util_create_drag_icon (widget,
4947 gtk_drag_set_icon_surface (context, surface);
4948 cairo_surface_destroy (surface);
4952 gtk_drag_set_icon_default (context);
4957 gtk_label_motion (GtkWidget *widget,
4958 GdkEventMotion *event)
4960 GtkLabel *label = GTK_LABEL (widget);
4961 GtkLabelPrivate *priv = label->priv;
4962 GtkLabelSelectionInfo *info = priv->select_info;
4968 if (info->links && !info->in_drag)
4972 gboolean found = FALSE;
4974 if (info->selection_anchor == info->selection_end)
4976 if (get_layout_index (label, event->x, event->y, &index))
4978 for (l = info->links; l != NULL; l = l->next)
4981 if (index >= link->start && index <= link->end)
4992 if (info->active_link != link)
4994 info->link_clicked = 0;
4995 info->active_link = link;
4996 gtk_label_update_cursor (label);
4997 gtk_widget_queue_draw (widget);
5002 if (info->active_link != NULL)
5004 info->link_clicked = 0;
5005 info->active_link = NULL;
5006 gtk_label_update_cursor (label);
5007 gtk_widget_queue_draw (widget);
5012 if (!info->selectable)
5015 if ((event->state & GDK_BUTTON1_MASK) == 0)
5020 if (gtk_drag_check_threshold (widget,
5023 event->x, event->y))
5025 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
5027 gtk_target_list_add_text_targets (target_list, 0);
5029 g_signal_connect (widget, "drag-begin",
5030 G_CALLBACK (drag_begin_cb), NULL);
5031 gtk_drag_begin (widget, target_list,
5033 1, (GdkEvent *)event);
5035 info->in_drag = FALSE;
5037 gtk_target_list_unref (target_list);
5044 gdk_window_get_device_position (info->window, event->device, &x, &y, NULL);
5045 get_layout_index (label, x, y, &index);
5047 if (info->select_words)
5050 gint old_min, old_max;
5053 min = gtk_label_move_backward_word (label, index);
5054 max = gtk_label_move_forward_word (label, index);
5056 anchor = info->selection_anchor;
5057 end = info->selection_end;
5059 old_min = MIN (anchor, end);
5060 old_max = MAX (anchor, end);
5067 else if (old_max < max)
5072 else if (anchor == old_min)
5083 gtk_label_select_region_index (label, anchor, end);
5086 gtk_label_select_region_index (label, info->selection_anchor, index);
5093 gtk_label_leave_notify (GtkWidget *widget,
5094 GdkEventCrossing *event)
5096 GtkLabel *label = GTK_LABEL (widget);
5097 GtkLabelPrivate *priv = label->priv;
5099 if (priv->select_info)
5101 priv->select_info->active_link = NULL;
5102 gtk_label_update_cursor (label);
5103 gtk_widget_queue_draw (widget);
5106 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event)
5107 return GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event (widget, event);
5113 gtk_label_create_window (GtkLabel *label)
5115 GtkLabelPrivate *priv = label->priv;
5116 GtkAllocation allocation;
5118 GdkWindowAttr attributes;
5119 gint attributes_mask;
5121 g_assert (priv->select_info);
5122 widget = GTK_WIDGET (label);
5123 g_assert (gtk_widget_get_realized (widget));
5125 if (priv->select_info->window)
5128 gtk_widget_get_allocation (widget, &allocation);
5130 attributes.x = allocation.x;
5131 attributes.y = allocation.y;
5132 attributes.width = allocation.width;
5133 attributes.height = allocation.height;
5134 attributes.window_type = GDK_WINDOW_CHILD;
5135 attributes.wclass = GDK_INPUT_ONLY;
5136 attributes.override_redirect = TRUE;
5137 attributes.event_mask = gtk_widget_get_events (widget) |
5138 GDK_BUTTON_PRESS_MASK |
5139 GDK_BUTTON_RELEASE_MASK |
5140 GDK_LEAVE_NOTIFY_MASK |
5141 GDK_BUTTON_MOTION_MASK |
5142 GDK_POINTER_MOTION_MASK |
5143 GDK_POINTER_MOTION_HINT_MASK;
5144 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
5145 if (gtk_widget_is_sensitive (widget))
5147 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
5149 attributes_mask |= GDK_WA_CURSOR;
5153 priv->select_info->window = gdk_window_new (gtk_widget_get_window (widget),
5154 &attributes, attributes_mask);
5155 gdk_window_set_user_data (priv->select_info->window, widget);
5157 if (attributes_mask & GDK_WA_CURSOR)
5158 g_object_unref (attributes.cursor);
5162 gtk_label_destroy_window (GtkLabel *label)
5164 GtkLabelPrivate *priv = label->priv;
5166 g_assert (priv->select_info);
5168 if (priv->select_info->window == NULL)
5171 gdk_window_set_user_data (priv->select_info->window, NULL);
5172 gdk_window_destroy (priv->select_info->window);
5173 priv->select_info->window = NULL;
5177 gtk_label_ensure_select_info (GtkLabel *label)
5179 GtkLabelPrivate *priv = label->priv;
5181 if (priv->select_info == NULL)
5183 priv->select_info = g_new0 (GtkLabelSelectionInfo, 1);
5185 gtk_widget_set_can_focus (GTK_WIDGET (label), TRUE);
5187 if (gtk_widget_get_realized (GTK_WIDGET (label)))
5188 gtk_label_create_window (label);
5190 if (gtk_widget_get_mapped (GTK_WIDGET (label)))
5191 gdk_window_show (priv->select_info->window);
5196 gtk_label_clear_select_info (GtkLabel *label)
5198 GtkLabelPrivate *priv = label->priv;
5200 if (priv->select_info == NULL)
5203 if (!priv->select_info->selectable && !priv->select_info->links)
5205 gtk_label_destroy_window (label);
5207 g_free (priv->select_info);
5208 priv->select_info = NULL;
5210 gtk_widget_set_can_focus (GTK_WIDGET (label), FALSE);
5215 * gtk_label_set_selectable:
5216 * @label: a #GtkLabel
5217 * @setting: %TRUE to allow selecting text in the label
5219 * Selectable labels allow the user to select text from the label, for
5223 gtk_label_set_selectable (GtkLabel *label,
5226 GtkLabelPrivate *priv;
5227 gboolean old_setting;
5229 g_return_if_fail (GTK_IS_LABEL (label));
5233 setting = setting != FALSE;
5234 old_setting = priv->select_info && priv->select_info->selectable;
5238 gtk_label_ensure_select_info (label);
5239 priv->select_info->selectable = TRUE;
5240 gtk_label_update_cursor (label);
5246 /* unselect, to give up the selection */
5247 gtk_label_select_region (label, 0, 0);
5249 priv->select_info->selectable = FALSE;
5250 gtk_label_clear_select_info (label);
5251 gtk_label_update_cursor (label);
5254 if (setting != old_setting)
5256 g_object_freeze_notify (G_OBJECT (label));
5257 g_object_notify (G_OBJECT (label), "selectable");
5258 g_object_notify (G_OBJECT (label), "cursor-position");
5259 g_object_notify (G_OBJECT (label), "selection-bound");
5260 g_object_thaw_notify (G_OBJECT (label));
5261 gtk_widget_queue_draw (GTK_WIDGET (label));
5266 * gtk_label_get_selectable:
5267 * @label: a #GtkLabel
5269 * Gets the value set by gtk_label_set_selectable().
5271 * Return value: %TRUE if the user can copy text from the label
5274 gtk_label_get_selectable (GtkLabel *label)
5276 GtkLabelPrivate *priv;
5278 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5282 return priv->select_info && priv->select_info->selectable;
5286 * gtk_label_set_angle:
5287 * @label: a #GtkLabel
5288 * @angle: the angle that the baseline of the label makes with
5289 * the horizontal, in degrees, measured counterclockwise
5291 * Sets the angle of rotation for the label. An angle of 90 reads from
5292 * from bottom to top, an angle of 270, from top to bottom. The angle
5293 * setting for the label is ignored if the label is selectable,
5294 * wrapped, or ellipsized.
5299 gtk_label_set_angle (GtkLabel *label,
5302 GtkLabelPrivate *priv;
5304 g_return_if_fail (GTK_IS_LABEL (label));
5308 /* Canonicalize to [0,360]. We don't canonicalize 360 to 0, because
5309 * double property ranges are inclusive, and changing 360 to 0 would
5310 * make a property editor behave strangely.
5312 if (angle < 0 || angle > 360.0)
5313 angle = angle - 360. * floor (angle / 360.);
5315 if (priv->angle != angle)
5317 priv->angle = angle;
5319 gtk_label_clear_layout (label);
5320 gtk_widget_queue_resize (GTK_WIDGET (label));
5322 g_object_notify (G_OBJECT (label), "angle");
5327 * gtk_label_get_angle:
5328 * @label: a #GtkLabel
5330 * Gets the angle of rotation for the label. See
5331 * gtk_label_set_angle().
5333 * Return value: the angle of rotation for the label
5338 gtk_label_get_angle (GtkLabel *label)
5340 g_return_val_if_fail (GTK_IS_LABEL (label), 0.0);
5342 return label->priv->angle;
5346 gtk_label_set_selection_text (GtkLabel *label,
5347 GtkSelectionData *selection_data)
5349 GtkLabelPrivate *priv = label->priv;
5351 if ((priv->select_info->selection_anchor !=
5352 priv->select_info->selection_end) &&
5358 start = MIN (priv->select_info->selection_anchor,
5359 priv->select_info->selection_end);
5360 end = MAX (priv->select_info->selection_anchor,
5361 priv->select_info->selection_end);
5363 len = strlen (priv->text);
5371 gtk_selection_data_set_text (selection_data,
5378 gtk_label_drag_data_get (GtkWidget *widget,
5379 GdkDragContext *context,
5380 GtkSelectionData *selection_data,
5384 gtk_label_set_selection_text (GTK_LABEL (widget), selection_data);
5388 get_text_callback (GtkClipboard *clipboard,
5389 GtkSelectionData *selection_data,
5391 gpointer user_data_or_owner)
5393 gtk_label_set_selection_text (GTK_LABEL (user_data_or_owner), selection_data);
5397 clear_text_callback (GtkClipboard *clipboard,
5398 gpointer user_data_or_owner)
5401 GtkLabelPrivate *priv;
5403 label = GTK_LABEL (user_data_or_owner);
5406 if (priv->select_info)
5408 priv->select_info->selection_anchor = priv->select_info->selection_end;
5410 gtk_widget_queue_draw (GTK_WIDGET (label));
5415 gtk_label_select_region_index (GtkLabel *label,
5419 GtkLabelPrivate *priv;
5421 g_return_if_fail (GTK_IS_LABEL (label));
5425 if (priv->select_info && priv->select_info->selectable)
5427 GtkClipboard *clipboard;
5429 if (priv->select_info->selection_anchor == anchor_index &&
5430 priv->select_info->selection_end == end_index)
5433 g_object_freeze_notify (G_OBJECT (label));
5435 if (priv->select_info->selection_anchor != anchor_index)
5436 g_object_notify (G_OBJECT (label), "selection-bound");
5437 if (priv->select_info->selection_end != end_index)
5438 g_object_notify (G_OBJECT (label), "cursor-position");
5440 priv->select_info->selection_anchor = anchor_index;
5441 priv->select_info->selection_end = end_index;
5443 if (gtk_widget_has_screen (GTK_WIDGET (label)))
5444 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label),
5445 GDK_SELECTION_PRIMARY);
5449 if (anchor_index != end_index)
5451 GtkTargetList *list;
5452 GtkTargetEntry *targets;
5455 list = gtk_target_list_new (NULL, 0);
5456 gtk_target_list_add_text_targets (list, 0);
5457 targets = gtk_target_table_new_from_list (list, &n_targets);
5460 gtk_clipboard_set_with_owner (clipboard,
5463 clear_text_callback,
5466 gtk_target_table_free (targets, n_targets);
5467 gtk_target_list_unref (list);
5472 gtk_clipboard_get_owner (clipboard) == G_OBJECT (label))
5473 gtk_clipboard_clear (clipboard);
5476 gtk_widget_queue_draw (GTK_WIDGET (label));
5478 g_object_thaw_notify (G_OBJECT (label));
5483 * gtk_label_select_region:
5484 * @label: a #GtkLabel
5485 * @start_offset: start offset (in characters not bytes)
5486 * @end_offset: end offset (in characters not bytes)
5488 * Selects a range of characters in the label, if the label is selectable.
5489 * See gtk_label_set_selectable(). If the label is not selectable,
5490 * this function has no effect. If @start_offset or
5491 * @end_offset are -1, then the end of the label will be substituted.
5494 gtk_label_select_region (GtkLabel *label,
5498 GtkLabelPrivate *priv;
5500 g_return_if_fail (GTK_IS_LABEL (label));
5504 if (priv->text && priv->select_info)
5506 if (start_offset < 0)
5507 start_offset = g_utf8_strlen (priv->text, -1);
5510 end_offset = g_utf8_strlen (priv->text, -1);
5512 gtk_label_select_region_index (label,
5513 g_utf8_offset_to_pointer (priv->text, start_offset) - priv->text,
5514 g_utf8_offset_to_pointer (priv->text, end_offset) - priv->text);
5519 * gtk_label_get_selection_bounds:
5520 * @label: a #GtkLabel
5521 * @start: (out): return location for start of selection, as a character offset
5522 * @end: (out): return location for end of selection, as a character offset
5524 * Gets the selected range of characters in the label, returning %TRUE
5525 * if there's a selection.
5527 * Return value: %TRUE if selection is non-empty
5530 gtk_label_get_selection_bounds (GtkLabel *label,
5534 GtkLabelPrivate *priv;
5536 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5540 if (priv->select_info == NULL)
5542 /* not a selectable label */
5552 gint start_index, end_index;
5553 gint start_offset, end_offset;
5556 start_index = MIN (priv->select_info->selection_anchor,
5557 priv->select_info->selection_end);
5558 end_index = MAX (priv->select_info->selection_anchor,
5559 priv->select_info->selection_end);
5561 len = strlen (priv->text);
5563 if (end_index > len)
5566 if (start_index > len)
5569 start_offset = g_utf8_strlen (priv->text, start_index);
5570 end_offset = g_utf8_strlen (priv->text, end_index);
5572 if (start_offset > end_offset)
5574 gint tmp = start_offset;
5575 start_offset = end_offset;
5580 *start = start_offset;
5585 return start_offset != end_offset;
5591 * gtk_label_get_layout:
5592 * @label: a #GtkLabel
5594 * Gets the #PangoLayout used to display the label.
5595 * The layout is useful to e.g. convert text positions to
5596 * pixel positions, in combination with gtk_label_get_layout_offsets().
5597 * The returned layout is owned by the @label so need not be
5598 * freed by the caller. The @label is free to recreate its layout at
5599 * any time, so it should be considered read-only.
5601 * Return value: (transfer none): the #PangoLayout for this label
5604 gtk_label_get_layout (GtkLabel *label)
5606 GtkLabelPrivate *priv;
5608 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
5612 gtk_label_ensure_layout (label);
5614 return priv->layout;
5618 * gtk_label_get_layout_offsets:
5619 * @label: a #GtkLabel
5620 * @x: (out) (allow-none): location to store X offset of layout, or %NULL
5621 * @y: (out) (allow-none): location to store Y offset of layout, or %NULL
5623 * Obtains the coordinates where the label will draw the #PangoLayout
5624 * representing the text in the label; useful to convert mouse events
5625 * into coordinates inside the #PangoLayout, e.g. to take some action
5626 * if some part of the label is clicked. Of course you will need to
5627 * create a #GtkEventBox to receive the events, and pack the label
5628 * inside it, since labels are a #GTK_NO_WINDOW widget. Remember
5629 * when using the #PangoLayout functions you need to convert to
5630 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
5633 gtk_label_get_layout_offsets (GtkLabel *label,
5637 g_return_if_fail (GTK_IS_LABEL (label));
5639 gtk_label_ensure_layout (label);
5641 get_layout_location (label, x, y);
5645 * gtk_label_set_use_markup:
5646 * @label: a #GtkLabel
5647 * @setting: %TRUE if the label's text should be parsed for markup.
5649 * Sets whether the text of the label contains markup in <link
5650 * linkend="PangoMarkupFormat">Pango's text markup
5651 * language</link>. See gtk_label_set_markup().
5654 gtk_label_set_use_markup (GtkLabel *label,
5657 g_return_if_fail (GTK_IS_LABEL (label));
5659 g_object_freeze_notify (G_OBJECT (label));
5661 gtk_label_set_use_markup_internal (label, setting);
5662 gtk_label_recalculate (label);
5664 g_object_thaw_notify (G_OBJECT (label));
5668 * gtk_label_get_use_markup:
5669 * @label: a #GtkLabel
5671 * Returns whether the label's text is interpreted as marked up with
5672 * the <link linkend="PangoMarkupFormat">Pango text markup
5673 * language</link>. See gtk_label_set_use_markup ().
5675 * Return value: %TRUE if the label's text will be parsed for markup.
5678 gtk_label_get_use_markup (GtkLabel *label)
5680 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5682 return label->priv->use_markup;
5686 * gtk_label_set_use_underline:
5687 * @label: a #GtkLabel
5688 * @setting: %TRUE if underlines in the text indicate mnemonics
5690 * If true, an underline in the text indicates the next character should be
5691 * used for the mnemonic accelerator key.
5694 gtk_label_set_use_underline (GtkLabel *label,
5697 g_return_if_fail (GTK_IS_LABEL (label));
5699 g_object_freeze_notify (G_OBJECT (label));
5701 gtk_label_set_use_underline_internal (label, setting);
5702 gtk_label_recalculate (label);
5704 g_object_thaw_notify (G_OBJECT (label));
5708 * gtk_label_get_use_underline:
5709 * @label: a #GtkLabel
5711 * Returns whether an embedded underline in the label indicates a
5712 * mnemonic. See gtk_label_set_use_underline().
5714 * Return value: %TRUE whether an embedded underline in the label indicates
5715 * the mnemonic accelerator keys.
5718 gtk_label_get_use_underline (GtkLabel *label)
5720 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5722 return label->priv->use_underline;
5726 * gtk_label_set_single_line_mode:
5727 * @label: a #GtkLabel
5728 * @single_line_mode: %TRUE if the label should be in single line mode
5730 * Sets whether the label is in single line mode.
5735 gtk_label_set_single_line_mode (GtkLabel *label,
5736 gboolean single_line_mode)
5738 GtkLabelPrivate *priv;
5740 g_return_if_fail (GTK_IS_LABEL (label));
5744 single_line_mode = single_line_mode != FALSE;
5746 if (priv->single_line_mode != single_line_mode)
5748 priv->single_line_mode = single_line_mode;
5750 gtk_label_clear_layout (label);
5751 gtk_widget_queue_resize (GTK_WIDGET (label));
5753 g_object_notify (G_OBJECT (label), "single-line-mode");
5758 * gtk_label_get_single_line_mode:
5759 * @label: a #GtkLabel
5761 * Returns whether the label is in single line mode.
5763 * Return value: %TRUE when the label is in single line mode.
5768 gtk_label_get_single_line_mode (GtkLabel *label)
5770 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5772 return label->priv->single_line_mode;
5775 /* Compute the X position for an offset that corresponds to the "more important
5776 * cursor position for that offset. We use this when trying to guess to which
5777 * end of the selection we should go to when the user hits the left or
5781 get_better_cursor (GtkLabel *label,
5786 GtkLabelPrivate *priv = label->priv;
5787 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5788 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5789 PangoDirection cursor_direction = get_cursor_direction (label);
5790 gboolean split_cursor;
5791 PangoRectangle strong_pos, weak_pos;
5793 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5794 "gtk-split-cursor", &split_cursor,
5797 gtk_label_ensure_layout (label);
5799 pango_layout_get_cursor_pos (priv->layout, index,
5800 &strong_pos, &weak_pos);
5804 *x = strong_pos.x / PANGO_SCALE;
5805 *y = strong_pos.y / PANGO_SCALE;
5809 if (keymap_direction == cursor_direction)
5811 *x = strong_pos.x / PANGO_SCALE;
5812 *y = strong_pos.y / PANGO_SCALE;
5816 *x = weak_pos.x / PANGO_SCALE;
5817 *y = weak_pos.y / PANGO_SCALE;
5824 gtk_label_move_logically (GtkLabel *label,
5828 GtkLabelPrivate *priv = label->priv;
5829 gint offset = g_utf8_pointer_to_offset (priv->text,
5830 priv->text + start);
5834 PangoLogAttr *log_attrs;
5838 gtk_label_ensure_layout (label);
5840 length = g_utf8_strlen (priv->text, -1);
5842 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5844 while (count > 0 && offset < length)
5848 while (offset < length && !log_attrs[offset].is_cursor_position);
5852 while (count < 0 && offset > 0)
5856 while (offset > 0 && !log_attrs[offset].is_cursor_position);
5864 return g_utf8_offset_to_pointer (priv->text, offset) - priv->text;
5868 gtk_label_move_visually (GtkLabel *label,
5872 GtkLabelPrivate *priv = label->priv;
5879 int new_index, new_trailing;
5880 gboolean split_cursor;
5883 gtk_label_ensure_layout (label);
5885 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5886 "gtk-split-cursor", &split_cursor,
5893 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5894 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5896 strong = keymap_direction == get_cursor_direction (label);
5901 pango_layout_move_cursor_visually (priv->layout, strong, index, 0, 1, &new_index, &new_trailing);
5906 pango_layout_move_cursor_visually (priv->layout, strong, index, 0, -1, &new_index, &new_trailing);
5910 if (new_index < 0 || new_index == G_MAXINT)
5915 while (new_trailing--)
5916 index = g_utf8_next_char (priv->text + new_index) - priv->text;
5923 gtk_label_move_forward_word (GtkLabel *label,
5926 GtkLabelPrivate *priv = label->priv;
5927 gint new_pos = g_utf8_pointer_to_offset (priv->text,
5928 priv->text + start);
5931 length = g_utf8_strlen (priv->text, -1);
5932 if (new_pos < length)
5934 PangoLogAttr *log_attrs;
5937 gtk_label_ensure_layout (label);
5939 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5941 /* Find the next word end */
5943 while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
5949 return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
5954 gtk_label_move_backward_word (GtkLabel *label,
5957 GtkLabelPrivate *priv = label->priv;
5958 gint new_pos = g_utf8_pointer_to_offset (priv->text,
5959 priv->text + start);
5963 PangoLogAttr *log_attrs;
5966 gtk_label_ensure_layout (label);
5968 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5972 /* Find the previous word beginning */
5973 while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
5979 return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
5983 gtk_label_move_cursor (GtkLabel *label,
5984 GtkMovementStep step,
5986 gboolean extend_selection)
5988 GtkLabelPrivate *priv = label->priv;
5992 if (priv->select_info == NULL)
5995 old_pos = new_pos = priv->select_info->selection_end;
5997 if (priv->select_info->selection_end != priv->select_info->selection_anchor &&
6000 /* If we have a current selection and aren't extending it, move to the
6001 * start/or end of the selection as appropriate
6005 case GTK_MOVEMENT_VISUAL_POSITIONS:
6008 gint anchor_x, anchor_y;
6009 gboolean end_is_left;
6011 get_better_cursor (label, priv->select_info->selection_end, &end_x, &end_y);
6012 get_better_cursor (label, priv->select_info->selection_anchor, &anchor_x, &anchor_y);
6014 end_is_left = (end_y < anchor_y) || (end_y == anchor_y && end_x < anchor_x);
6017 new_pos = end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
6019 new_pos = !end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
6022 case GTK_MOVEMENT_LOGICAL_POSITIONS:
6023 case GTK_MOVEMENT_WORDS:
6025 new_pos = MIN (priv->select_info->selection_end, priv->select_info->selection_anchor);
6027 new_pos = MAX (priv->select_info->selection_end, priv->select_info->selection_anchor);
6029 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
6030 case GTK_MOVEMENT_PARAGRAPH_ENDS:
6031 case GTK_MOVEMENT_BUFFER_ENDS:
6032 /* FIXME: Can do better here */
6033 new_pos = count < 0 ? 0 : strlen (priv->text);
6035 case GTK_MOVEMENT_DISPLAY_LINES:
6036 case GTK_MOVEMENT_PARAGRAPHS:
6037 case GTK_MOVEMENT_PAGES:
6038 case GTK_MOVEMENT_HORIZONTAL_PAGES:
6046 case GTK_MOVEMENT_LOGICAL_POSITIONS:
6047 new_pos = gtk_label_move_logically (label, new_pos, count);
6049 case GTK_MOVEMENT_VISUAL_POSITIONS:
6050 new_pos = gtk_label_move_visually (label, new_pos, count);
6051 if (new_pos == old_pos)
6053 if (!extend_selection)
6055 if (!gtk_widget_keynav_failed (GTK_WIDGET (label),
6057 GTK_DIR_RIGHT : GTK_DIR_LEFT))
6059 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
6062 gtk_widget_child_focus (toplevel,
6064 GTK_DIR_RIGHT : GTK_DIR_LEFT);
6069 gtk_widget_error_bell (GTK_WIDGET (label));
6073 case GTK_MOVEMENT_WORDS:
6076 new_pos = gtk_label_move_forward_word (label, new_pos);
6081 new_pos = gtk_label_move_backward_word (label, new_pos);
6084 if (new_pos == old_pos)
6085 gtk_widget_error_bell (GTK_WIDGET (label));
6087 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
6088 case GTK_MOVEMENT_PARAGRAPH_ENDS:
6089 case GTK_MOVEMENT_BUFFER_ENDS:
6090 /* FIXME: Can do better here */
6091 new_pos = count < 0 ? 0 : strlen (priv->text);
6092 if (new_pos == old_pos)
6093 gtk_widget_error_bell (GTK_WIDGET (label));
6095 case GTK_MOVEMENT_DISPLAY_LINES:
6096 case GTK_MOVEMENT_PARAGRAPHS:
6097 case GTK_MOVEMENT_PAGES:
6098 case GTK_MOVEMENT_HORIZONTAL_PAGES:
6103 if (extend_selection)
6104 gtk_label_select_region_index (label,
6105 priv->select_info->selection_anchor,
6108 gtk_label_select_region_index (label, new_pos, new_pos);
6112 gtk_label_copy_clipboard (GtkLabel *label)
6114 GtkLabelPrivate *priv = label->priv;
6116 if (priv->text && priv->select_info)
6120 GtkClipboard *clipboard;
6122 start = MIN (priv->select_info->selection_anchor,
6123 priv->select_info->selection_end);
6124 end = MAX (priv->select_info->selection_anchor,
6125 priv->select_info->selection_end);
6127 len = strlen (priv->text);
6135 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6138 gtk_clipboard_set_text (clipboard, priv->text + start, end - start);
6143 link = gtk_label_get_focus_link (label);
6145 gtk_clipboard_set_text (clipboard, link->uri, -1);
6151 gtk_label_select_all (GtkLabel *label)
6153 GtkLabelPrivate *priv = label->priv;
6155 gtk_label_select_region_index (label, 0, strlen (priv->text));
6158 /* Quick hack of a popup menu
6161 activate_cb (GtkWidget *menuitem,
6164 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6165 g_signal_emit_by_name (label, signal);
6169 append_action_signal (GtkLabel *label,
6171 const gchar *stock_id,
6172 const gchar *signal,
6175 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6177 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
6178 g_signal_connect (menuitem, "activate",
6179 G_CALLBACK (activate_cb), label);
6181 gtk_widget_set_sensitive (menuitem, sensitive);
6183 gtk_widget_show (menuitem);
6184 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6188 popup_menu_detach (GtkWidget *attach_widget,
6191 GtkLabel *label = GTK_LABEL (attach_widget);
6192 GtkLabelPrivate *priv = label->priv;
6194 if (priv->select_info)
6195 priv->select_info->popup_menu = NULL;
6199 popup_position_func (GtkMenu *menu,
6207 GtkAllocation allocation;
6211 label = GTK_LABEL (user_data);
6212 widget = GTK_WIDGET (label);
6214 g_return_if_fail (gtk_widget_get_realized (widget));
6216 screen = gtk_widget_get_screen (widget);
6217 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
6219 gtk_widget_get_allocation (widget, &allocation);
6224 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
6227 gtk_widget_get_allocation (widget, &allocation);
6229 *x += allocation.width / 2;
6230 *y += allocation.height;
6232 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
6233 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
6237 open_link_activate_cb (GtkMenuItem *menu_item,
6242 link = gtk_label_get_current_link (label);
6245 emit_activate_link (label, link);
6249 copy_link_activate_cb (GtkMenuItem *menu_item,
6252 GtkClipboard *clipboard;
6255 uri = gtk_label_get_current_uri (label);
6258 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6259 gtk_clipboard_set_text (clipboard, uri, -1);
6264 gtk_label_popup_menu (GtkWidget *widget)
6266 gtk_label_do_popup (GTK_LABEL (widget), NULL);
6272 gtk_label_do_popup (GtkLabel *label,
6273 GdkEventButton *event)
6275 GtkLabelPrivate *priv = label->priv;
6276 GtkWidget *menuitem;
6279 gboolean have_selection;
6282 if (!priv->select_info)
6285 if (priv->select_info->popup_menu)
6286 gtk_widget_destroy (priv->select_info->popup_menu);
6288 priv->select_info->popup_menu = menu = gtk_menu_new ();
6290 gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (label), popup_menu_detach);
6293 priv->select_info->selection_anchor != priv->select_info->selection_end;
6297 if (priv->select_info->link_clicked)
6298 link = priv->select_info->active_link;
6303 link = gtk_label_get_focus_link (label);
6305 if (!have_selection && link)
6308 menuitem = gtk_image_menu_item_new_with_mnemonic (_("_Open Link"));
6309 gtk_widget_show (menuitem);
6310 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6312 g_signal_connect (G_OBJECT (menuitem), "activate",
6313 G_CALLBACK (open_link_activate_cb), label);
6315 image = gtk_image_new_from_stock (GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_MENU);
6316 gtk_widget_show (image);
6317 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6319 /* Copy Link Address */
6320 menuitem = gtk_image_menu_item_new_with_mnemonic (_("Copy _Link Address"));
6321 gtk_widget_show (menuitem);
6322 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6324 g_signal_connect (G_OBJECT (menuitem), "activate",
6325 G_CALLBACK (copy_link_activate_cb), label);
6327 image = gtk_image_new_from_stock (GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
6328 gtk_widget_show (image);
6329 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6333 append_action_signal (label, menu, GTK_STOCK_CUT, "cut-clipboard", FALSE);
6334 append_action_signal (label, menu, GTK_STOCK_COPY, "copy-clipboard", have_selection);
6335 append_action_signal (label, menu, GTK_STOCK_PASTE, "paste-clipboard", FALSE);
6337 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
6338 gtk_widget_set_sensitive (menuitem, FALSE);
6339 gtk_widget_show (menuitem);
6340 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6342 menuitem = gtk_separator_menu_item_new ();
6343 gtk_widget_show (menuitem);
6344 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6346 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
6347 g_signal_connect_swapped (menuitem, "activate",
6348 G_CALLBACK (gtk_label_select_all), label);
6349 gtk_widget_show (menuitem);
6350 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6353 g_signal_emit (label, signals[POPULATE_POPUP], 0, menu);
6356 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6358 event->button, event->time);
6361 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6362 popup_position_func, label,
6363 0, gtk_get_current_event_time ());
6364 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
6369 gtk_label_clear_links (GtkLabel *label)
6371 GtkLabelPrivate *priv = label->priv;
6373 if (!priv->select_info)
6376 g_list_foreach (priv->select_info->links, (GFunc)link_free, NULL);
6377 g_list_free (priv->select_info->links);
6378 priv->select_info->links = NULL;
6379 priv->select_info->active_link = NULL;
6383 gtk_label_rescan_links (GtkLabel *label)
6385 GtkLabelPrivate *priv = label->priv;
6386 PangoLayout *layout = priv->layout;
6387 PangoAttrList *attlist;
6388 PangoAttrIterator *iter;
6391 if (!priv->select_info || !priv->select_info->links)
6394 attlist = pango_layout_get_attributes (layout);
6396 if (attlist == NULL)
6399 iter = pango_attr_list_get_iterator (attlist);
6401 links = priv->select_info->links;
6405 PangoAttribute *underline;
6406 PangoAttribute *color;
6408 underline = pango_attr_iterator_get (iter, PANGO_ATTR_UNDERLINE);
6409 color = pango_attr_iterator_get (iter, PANGO_ATTR_FOREGROUND);
6411 if (underline != NULL && color != NULL)
6414 PangoRectangle start_pos;
6415 PangoRectangle end_pos;
6418 pango_attr_iterator_range (iter, &start, &end);
6419 pango_layout_index_to_pos (layout, start, &start_pos);
6420 pango_layout_index_to_pos (layout, end, &end_pos);
6424 g_warning ("Ran out of links");
6428 links = links->next;
6429 link->start = start;
6432 } while (pango_attr_iterator_next (iter));
6434 pango_attr_iterator_destroy (iter);
6438 gtk_label_activate_link (GtkLabel *label,
6441 GtkWidget *widget = GTK_WIDGET (label);
6442 GError *error = NULL;
6444 if (!gtk_show_uri (gtk_widget_get_screen (widget),
6445 uri, gtk_get_current_event_time (), &error))
6447 g_warning ("Unable to show '%s': %s", uri, error->message);
6448 g_error_free (error);
6455 emit_activate_link (GtkLabel *label,
6458 GtkLabelPrivate *priv = label->priv;
6461 g_signal_emit (label, signals[ACTIVATE_LINK], 0, link->uri, &handled);
6462 if (handled && priv->track_links && !link->visited)
6464 link->visited = TRUE;
6465 /* FIXME: shouldn't have to redo everything here */
6466 gtk_label_recalculate (label);
6471 gtk_label_activate_current_link (GtkLabel *label)
6474 GtkWidget *widget = GTK_WIDGET (label);
6476 link = gtk_label_get_focus_link (label);
6480 emit_activate_link (label, link);
6484 GtkWidget *toplevel;
6486 GtkWidget *default_widget, *focus_widget;
6488 toplevel = gtk_widget_get_toplevel (widget);
6489 if (GTK_IS_WINDOW (toplevel))
6491 window = GTK_WINDOW (toplevel);
6495 default_widget = gtk_window_get_default_widget (window);
6496 focus_widget = gtk_window_get_focus (window);
6498 if (default_widget != widget &&
6499 !(widget == focus_widget && (!default_widget || !gtk_widget_is_sensitive (default_widget))))
6500 gtk_window_activate_default (window);
6506 static GtkLabelLink *
6507 gtk_label_get_current_link (GtkLabel *label)
6509 GtkLabelPrivate *priv = label->priv;
6512 if (!priv->select_info)
6515 if (priv->select_info->link_clicked)
6516 link = priv->select_info->active_link;
6518 link = gtk_label_get_focus_link (label);
6524 * gtk_label_get_current_uri:
6525 * @label: a #GtkLabel
6527 * Returns the URI for the currently active link in the label.
6528 * The active link is the one under the mouse pointer or, in a
6529 * selectable label, the link in which the text cursor is currently
6532 * This function is intended for use in a #GtkLabel::activate-link handler
6533 * or for use in a #GtkWidget::query-tooltip handler.
6535 * Returns: the currently active URI. The string is owned by GTK+ and must
6536 * not be freed or modified.
6541 gtk_label_get_current_uri (GtkLabel *label)
6545 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
6547 link = gtk_label_get_current_link (label);
6556 * gtk_label_set_track_visited_links:
6557 * @label: a #GtkLabel
6558 * @track_links: %TRUE to track visited links
6560 * Sets whether the label should keep track of clicked
6561 * links (and use a different color for them).
6566 gtk_label_set_track_visited_links (GtkLabel *label,
6567 gboolean track_links)
6569 GtkLabelPrivate *priv;
6571 g_return_if_fail (GTK_IS_LABEL (label));
6575 track_links = track_links != FALSE;
6577 if (priv->track_links != track_links)
6579 priv->track_links = track_links;
6581 /* FIXME: shouldn't have to redo everything here */
6582 gtk_label_recalculate (label);
6584 g_object_notify (G_OBJECT (label), "track-visited-links");
6589 * gtk_label_get_track_visited_links:
6590 * @label: a #GtkLabel
6592 * Returns whether the label is currently keeping track
6595 * Returns: %TRUE if clicked links are remembered
6600 gtk_label_get_track_visited_links (GtkLabel *label)
6602 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
6604 return label->priv->track_links;
6608 gtk_label_query_tooltip (GtkWidget *widget,
6611 gboolean keyboard_tip,
6612 GtkTooltip *tooltip)
6614 GtkLabel *label = GTK_LABEL (widget);
6615 GtkLabelPrivate *priv = label->priv;
6616 GtkLabelSelectionInfo *info = priv->select_info;
6620 if (info && info->links)
6624 if (info->selection_anchor == info->selection_end)
6625 index = info->selection_anchor;
6629 if (!get_layout_index (label, x, y, &index))
6635 for (l = info->links; l != NULL; l = l->next)
6637 GtkLabelLink *link = l->data;
6638 if (index >= link->start && index <= link->end)
6642 gtk_tooltip_set_markup (tooltip, link->title);
6651 return GTK_WIDGET_CLASS (gtk_label_parent_class)->query_tooltip (widget,
6658 _gtk_label_get_cursor_position (GtkLabel *label)
6660 GtkLabelPrivate *priv = label->priv;
6662 if (priv->select_info && priv->select_info->selectable)
6663 return g_utf8_pointer_to_offset (priv->text,
6664 priv->text + priv->select_info->selection_end);
6670 _gtk_label_get_selection_bound (GtkLabel *label)
6672 GtkLabelPrivate *priv = label->priv;
6674 if (priv->select_info && priv->select_info->selectable)
6675 return g_utf8_pointer_to_offset (priv->text,
6676 priv->text + priv->select_info->selection_anchor);