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, see <http://www.gnu.org/licenses/>.Free
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
31 #include "gtkaccellabel.h"
33 #include "gtkmarshalers.h"
35 #include "gtkwindow.h"
36 #include "gtkclipboard.h"
37 #include "gtkimagemenuitem.h"
39 #include "gtkseparatormenuitem.h"
40 #include "gtktextutil.h"
42 #include "gtkmenuitem.h"
43 #include "gtkmenushellprivate.h"
44 #include "gtknotebook.h"
46 #include "gtkbindings.h"
47 #include "gtkbuildable.h"
50 #include "gtktooltip.h"
51 #include "gtkprivate.h"
52 #include "gtktypebuiltins.h"
55 #include "a11y/gtklabelaccessible.h"
57 /* this is in case rint() is not provided by the compiler,
58 * such as in the case of C89 compilers, like MSVC
60 #include "fallback-c89.c"
64 * @Short_description: A widget that displays a small to medium amount of text
67 * The #GtkLabel widget displays a small amount of text. As the name
68 * implies, most labels are used to label another widget such as a
69 * #GtkButton, a #GtkMenuItem, or a #GtkOptionMenu.
71 * <refsect2 id="GtkLabel-BUILDER-UI">
72 * <title>GtkLabel as GtkBuildable</title>
74 * The GtkLabel implementation of the GtkBuildable interface supports a
75 * custom <attributes> element, which supports any number of <attribute>
76 * elements. the <attribute> element has attributes named name, value,
77 * start and end and allows you to specify #PangoAttribute values for this label.
80 * <title>A UI definition fragment specifying Pango attributes</title>
81 * <programlisting><![CDATA[
82 * <object class="GtkLabel">
84 * <attribute name="weight" value="PANGO_WEIGHT_BOLD"/>
85 * <attribute name="background" value="red" start="5" end="10"/>"
88 * ]]></programlisting>
90 * The start and end attributes specify the range of characters to which the
91 * Pango attribute applies. If start and end are not specified, the attribute is
92 * applied to the whole text. Note that specifying ranges does not make much
93 * sense with translatable attributes. Use markup embedded in the translatable
98 * <title>Mnemonics</title>
100 * Labels may contain <firstterm>mnemonics</firstterm>. Mnemonics are
101 * underlined characters in the label, used for keyboard navigation.
102 * Mnemonics are created by providing a string with an underscore before
103 * the mnemonic character, such as <literal>"_File"</literal>, to the
104 * functions gtk_label_new_with_mnemonic() or
105 * gtk_label_set_text_with_mnemonic().
107 * Mnemonics automatically activate any activatable widget the label is
108 * inside, such as a #GtkButton; if the label is not inside the
109 * mnemonic's target widget, you have to tell the label about the target
110 * using gtk_label_set_mnemonic_widget(). Here's a simple example where
111 * the label is inside a button:
115 * // Pressing Alt+H will activate this button
116 * button = gtk_button_new (<!-- -->);
117 * label = gtk_label_new_with_mnemonic ("_Hello");
118 * gtk_container_add (GTK_CONTAINER (button), label);
122 * There's a convenience function to create buttons with a mnemonic label
127 * // Pressing Alt+H will activate this button
128 * button = gtk_button_new_with_mnemonic ("_Hello");
132 * To create a mnemonic for a widget alongside the label, such as a
133 * #GtkEntry, you have to point the label at the entry with
134 * gtk_label_set_mnemonic_widget():
138 * // Pressing Alt+H will focus the entry
139 * entry = gtk_entry_new (<!-- -->);
140 * label = gtk_label_new_with_mnemonic ("_Hello");
141 * gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
147 * <title>Markup (styled text)</title>
149 * To make it easy to format text in a label (changing colors, fonts,
150 * etc.), label text can be provided in a simple <link
151 * linkend="PangoMarkupFormat">markup format</link>.
152 * Here's how to create a label with a small font:
156 * label = gtk_label_new (NULL);
157 * gtk_label_set_markup (GTK_LABEL (label), "<small>Small text</small>");
162 * linkend="PangoMarkupFormat">complete documentation</link> of available
163 * tags in the Pango manual.)
165 * The markup passed to gtk_label_set_markup() must be valid; for example,
166 * literal <, > and & characters must be escaped as \<,
167 * \gt;, and \&. If you pass text obtained from the user, file,
168 * or a network to gtk_label_set_markup(), you'll want to escape it with
169 * g_markup_escape_text() or g_markup_printf_escaped().
171 * Markup strings are just a convenient way to set the #PangoAttrList on
172 * a label; gtk_label_set_attributes() may be a simpler way to set
173 * attributes in some cases. Be careful though; #PangoAttrList tends to
174 * cause internationalization problems, unless you're applying attributes
175 * to the entire string (i.e. unless you set the range of each attribute
176 * to [0, %G_MAXINT)). The reason is that specifying the start_index and
177 * end_index for a #PangoAttribute requires knowledge of the exact string
178 * being displayed, so translations will cause problems.
182 * <title>Selectable labels</title>
183 * Labels can be made selectable with gtk_label_set_selectable().
184 * Selectable labels allow the user to copy the label contents to
185 * the clipboard. Only labels that contain useful-to-copy information
186 * — such as error messages — should be made selectable.
188 * <refsect2 id="label-text-layout">
189 * <title>Text layout</title>
191 * A label can contain any number of paragraphs, but will have
192 * performance problems if it contains more than a small number.
193 * Paragraphs are separated by newlines or other paragraph separators
194 * understood by Pango.
196 * Labels can automatically wrap text if you call
197 * gtk_label_set_line_wrap().
199 * gtk_label_set_justify() sets how the lines in a label align
200 * with one another. If you want to set how the label as a whole
201 * aligns in its available space, see gtk_misc_set_alignment().
203 * The #GtkLabel:width-chars and #GtkLabel:max-width-chars properties
204 * can be used to control the size allocation of ellipsized or wrapped
205 * labels. For ellipsizing labels, if either is specified (and less
206 * than the actual text size), it is used as the minimum width, and the actual
207 * text size is used as the natural width of the label. For wrapping labels,
208 * width-chars is used as the minimum width, if specified, and max-width-chars
209 * is used as the natural width. Even if max-width-chars specified, wrapping
210 * labels will be rewrapped to use all of the available width.
213 * Note that the interpretation of #GtkLabel:width-chars and
214 * #GtkLabel:max-width-chars has changed a bit with the introduction of
215 * <link linkend="geometry-management">width-for-height geometry management.</link>
220 * <title>Links</title>
222 * Since 2.18, GTK+ supports markup for clickable hyperlinks in addition
223 * to regular Pango markup. The markup for links is borrowed from HTML, using the
224 * <tag>a</tag> with href and title attributes. GTK+ renders links similar to the
225 * way they appear in web browsers, with colored, underlined text. The title
226 * attribute is displayed as a tooltip on the link. An example looks like this:
228 * <informalexample><programlisting>
229 * gtk_label_set_markup (label, "Go to the <a href="http://www.gtk.org" title="<i>Our</i> website">GTK+ website</a> for more...");
230 * </programlisting></informalexample>
232 * It is possible to implement custom handling for links and their tooltips with
233 * the #GtkLabel::activate-link signal and the gtk_label_get_current_uri() function.
238 struct _GtkLabelPrivate
240 GtkLabelSelectionInfo *select_info;
241 GtkWidget *mnemonic_widget;
242 GtkWindow *mnemonic_window;
244 PangoAttrList *attrs;
245 PangoAttrList *markup_attrs;
253 guint mnemonics_visible : 1;
256 guint use_underline : 1;
257 guint use_markup : 1;
259 guint single_line_mode : 1;
260 guint have_transform : 1;
263 guint pattern_set : 1;
264 guint track_links : 1;
266 guint mnemonic_keyval;
269 gint max_width_chars;
272 /* Notes about the handling of links:
274 * Links share the GtkLabelSelectionInfo struct with selectable labels.
275 * There are some new fields for links. The links field contains the list
276 * of GtkLabelLink structs that describe the links which are embedded in
277 * the label. The active_link field points to the link under the mouse
278 * pointer. For keyboard navigation, the 'focus' link is determined by
279 * finding the link which contains the selection_anchor position.
280 * The link_clicked field is used with button press and release events
281 * to ensure that pressing inside a link and releasing outside of it
282 * does not activate the link.
284 * Links are rendered with the link-color/visited-link-color colors
285 * that are determined by the style and with an underline. When the mouse
286 * pointer is over a link, the pointer is changed to indicate the link,
287 * and the background behind the link is rendered with the base[PRELIGHT]
288 * color. While a button is pressed over a link, the background is rendered
289 * with the base[ACTIVE] color.
291 * Labels with links accept keyboard focus, and it is possible to move
292 * the focus between the embedded links using Tab/Shift-Tab. The focus
293 * is indicated by a focus rectangle that is drawn around the link text.
294 * Pressing Enter activates the focussed link, and there is a suitable
295 * context menu for links that can be opened with the Menu key. Pressing
296 * Control-C copies the link URI to the clipboard.
298 * In selectable labels with links, link functionality is only available
299 * when the selection is empty.
304 gchar *title; /* the title attribute, used as tooltip */
305 gboolean visited; /* get set when the link is activated; this flag
306 * gets preserved over later set_markup() calls
308 gint start; /* position of the link in the PangoLayout */
312 struct _GtkLabelSelectionInfo
315 gint selection_anchor;
317 GtkWidget *popup_menu;
320 GtkLabelLink *active_link;
326 guint select_words : 1;
327 guint selectable : 1;
328 guint link_clicked : 1;
336 ACTIVATE_CURRENT_LINK,
351 PROP_MNEMONIC_KEYVAL,
352 PROP_MNEMONIC_WIDGET,
353 PROP_CURSOR_POSITION,
354 PROP_SELECTION_BOUND,
357 PROP_SINGLE_LINE_MODE,
359 PROP_MAX_WIDTH_CHARS,
360 PROP_TRACK_VISITED_LINKS
363 /* When rotating ellipsizable text we want the natural size to request
364 * more to ensure the label wont ever ellipsize in an allocation of full natural size.
366 #define ROTATION_ELLIPSIZE_PADDING 2
368 static guint signals[LAST_SIGNAL] = { 0 };
370 static const GdkColor default_link_color = { 0, 0, 0, 0xeeee };
371 static const GdkColor default_visited_link_color = { 0, 0x5555, 0x1a1a, 0x8b8b };
373 static void gtk_label_set_property (GObject *object,
377 static void gtk_label_get_property (GObject *object,
381 static void gtk_label_finalize (GObject *object);
382 static void gtk_label_destroy (GtkWidget *widget);
383 static void gtk_label_size_allocate (GtkWidget *widget,
384 GtkAllocation *allocation);
385 static void gtk_label_state_flags_changed (GtkWidget *widget,
386 GtkStateFlags prev_state);
387 static void gtk_label_style_updated (GtkWidget *widget);
388 static void gtk_label_direction_changed (GtkWidget *widget,
389 GtkTextDirection previous_dir);
390 static gint gtk_label_draw (GtkWidget *widget,
392 static gboolean gtk_label_focus (GtkWidget *widget,
393 GtkDirectionType direction);
395 static void gtk_label_realize (GtkWidget *widget);
396 static void gtk_label_unrealize (GtkWidget *widget);
397 static void gtk_label_map (GtkWidget *widget);
398 static void gtk_label_unmap (GtkWidget *widget);
400 static gboolean gtk_label_button_press (GtkWidget *widget,
401 GdkEventButton *event);
402 static gboolean gtk_label_button_release (GtkWidget *widget,
403 GdkEventButton *event);
404 static gboolean gtk_label_motion (GtkWidget *widget,
405 GdkEventMotion *event);
406 static gboolean gtk_label_leave_notify (GtkWidget *widget,
407 GdkEventCrossing *event);
409 static void gtk_label_grab_focus (GtkWidget *widget);
411 static gboolean gtk_label_query_tooltip (GtkWidget *widget,
414 gboolean keyboard_tip,
415 GtkTooltip *tooltip);
417 static void gtk_label_set_text_internal (GtkLabel *label,
419 static void gtk_label_set_label_internal (GtkLabel *label,
421 static void gtk_label_set_use_markup_internal (GtkLabel *label,
423 static void gtk_label_set_use_underline_internal (GtkLabel *label,
425 static void gtk_label_set_uline_text_internal (GtkLabel *label,
427 static void gtk_label_set_pattern_internal (GtkLabel *label,
428 const gchar *pattern,
429 gboolean is_mnemonic);
430 static void gtk_label_set_markup_internal (GtkLabel *label,
432 gboolean with_uline);
433 static void gtk_label_recalculate (GtkLabel *label);
434 static void gtk_label_hierarchy_changed (GtkWidget *widget,
435 GtkWidget *old_toplevel);
436 static void gtk_label_screen_changed (GtkWidget *widget,
437 GdkScreen *old_screen);
438 static gboolean gtk_label_popup_menu (GtkWidget *widget);
440 static void gtk_label_create_window (GtkLabel *label);
441 static void gtk_label_destroy_window (GtkLabel *label);
442 static void gtk_label_ensure_select_info (GtkLabel *label);
443 static void gtk_label_clear_select_info (GtkLabel *label);
444 static void gtk_label_update_cursor (GtkLabel *label);
445 static void gtk_label_clear_layout (GtkLabel *label);
446 static void gtk_label_ensure_layout (GtkLabel *label);
447 static void gtk_label_select_region_index (GtkLabel *label,
452 static gboolean gtk_label_mnemonic_activate (GtkWidget *widget,
453 gboolean group_cycling);
454 static void gtk_label_setup_mnemonic (GtkLabel *label,
456 static void gtk_label_drag_data_get (GtkWidget *widget,
457 GdkDragContext *context,
458 GtkSelectionData *selection_data,
462 static void gtk_label_buildable_interface_init (GtkBuildableIface *iface);
463 static gboolean gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
466 const gchar *tagname,
467 GMarkupParser *parser,
470 static void gtk_label_buildable_custom_finished (GtkBuildable *buildable,
473 const gchar *tagname,
477 static void connect_mnemonics_visible_notify (GtkLabel *label);
478 static gboolean separate_uline_pattern (const gchar *str,
484 /* For selectable labels: */
485 static void gtk_label_move_cursor (GtkLabel *label,
486 GtkMovementStep step,
488 gboolean extend_selection);
489 static void gtk_label_copy_clipboard (GtkLabel *label);
490 static void gtk_label_select_all (GtkLabel *label);
491 static void gtk_label_do_popup (GtkLabel *label,
492 GdkEventButton *event);
493 static gint gtk_label_move_forward_word (GtkLabel *label,
495 static gint gtk_label_move_backward_word (GtkLabel *label,
499 static void gtk_label_clear_links (GtkLabel *label);
500 static gboolean gtk_label_activate_link (GtkLabel *label,
502 static void gtk_label_activate_current_link (GtkLabel *label);
503 static GtkLabelLink *gtk_label_get_current_link (GtkLabel *label);
504 static void gtk_label_get_link_colors (GtkWidget *widget,
505 GdkColor *link_color,
506 GdkColor *visited_link_color);
507 static void emit_activate_link (GtkLabel *label,
510 static GtkSizeRequestMode gtk_label_get_request_mode (GtkWidget *widget);
511 static void gtk_label_get_preferred_width (GtkWidget *widget,
514 static void gtk_label_get_preferred_height (GtkWidget *widget,
517 static void gtk_label_get_preferred_width_for_height (GtkWidget *widget,
520 gint *natural_width);
521 static void gtk_label_get_preferred_height_for_width (GtkWidget *widget,
523 gint *minimum_height,
524 gint *natural_height);
526 static GtkBuildableIface *buildable_parent_iface = NULL;
528 G_DEFINE_TYPE_WITH_CODE (GtkLabel, gtk_label, GTK_TYPE_MISC,
529 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
530 gtk_label_buildable_interface_init))
533 add_move_binding (GtkBindingSet *binding_set,
536 GtkMovementStep step,
539 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
541 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
545 G_TYPE_BOOLEAN, FALSE);
547 /* Selection-extending version */
548 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
552 G_TYPE_BOOLEAN, TRUE);
556 gtk_label_class_init (GtkLabelClass *class)
558 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
559 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
560 GtkBindingSet *binding_set;
562 gobject_class->set_property = gtk_label_set_property;
563 gobject_class->get_property = gtk_label_get_property;
564 gobject_class->finalize = gtk_label_finalize;
566 widget_class->destroy = gtk_label_destroy;
567 widget_class->size_allocate = gtk_label_size_allocate;
568 widget_class->state_flags_changed = gtk_label_state_flags_changed;
569 widget_class->style_updated = gtk_label_style_updated;
570 widget_class->query_tooltip = gtk_label_query_tooltip;
571 widget_class->direction_changed = gtk_label_direction_changed;
572 widget_class->draw = gtk_label_draw;
573 widget_class->realize = gtk_label_realize;
574 widget_class->unrealize = gtk_label_unrealize;
575 widget_class->map = gtk_label_map;
576 widget_class->unmap = gtk_label_unmap;
577 widget_class->button_press_event = gtk_label_button_press;
578 widget_class->button_release_event = gtk_label_button_release;
579 widget_class->motion_notify_event = gtk_label_motion;
580 widget_class->leave_notify_event = gtk_label_leave_notify;
581 widget_class->hierarchy_changed = gtk_label_hierarchy_changed;
582 widget_class->screen_changed = gtk_label_screen_changed;
583 widget_class->mnemonic_activate = gtk_label_mnemonic_activate;
584 widget_class->drag_data_get = gtk_label_drag_data_get;
585 widget_class->grab_focus = gtk_label_grab_focus;
586 widget_class->popup_menu = gtk_label_popup_menu;
587 widget_class->focus = gtk_label_focus;
588 widget_class->get_request_mode = gtk_label_get_request_mode;
589 widget_class->get_preferred_width = gtk_label_get_preferred_width;
590 widget_class->get_preferred_height = gtk_label_get_preferred_height;
591 widget_class->get_preferred_width_for_height = gtk_label_get_preferred_width_for_height;
592 widget_class->get_preferred_height_for_width = gtk_label_get_preferred_height_for_width;
594 class->move_cursor = gtk_label_move_cursor;
595 class->copy_clipboard = gtk_label_copy_clipboard;
596 class->activate_link = gtk_label_activate_link;
599 * GtkLabel::move-cursor:
600 * @entry: the object which received the signal
601 * @step: the granularity of the move, as a #GtkMovementStep
602 * @count: the number of @step units to move
603 * @extend_selection: %TRUE if the move should extend the selection
605 * The ::move-cursor signal is a
606 * <link linkend="keybinding-signals">keybinding signal</link>
607 * which gets emitted when the user initiates a cursor movement.
608 * If the cursor is not visible in @entry, this signal causes
609 * the viewport to be moved instead.
611 * Applications should not connect to it, but may emit it with
612 * g_signal_emit_by_name() if they need to control the cursor
615 * The default bindings for this signal come in two variants,
616 * the variant with the Shift modifier extends the selection,
617 * the variant without the Shift modifer does not.
618 * There are too many key combinations to list them all here.
620 * <listitem>Arrow keys move by individual characters/lines</listitem>
621 * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
622 * <listitem>Home/End keys move to the ends of the buffer</listitem>
625 signals[MOVE_CURSOR] =
626 g_signal_new (I_("move-cursor"),
627 G_OBJECT_CLASS_TYPE (gobject_class),
628 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
629 G_STRUCT_OFFSET (GtkLabelClass, move_cursor),
631 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
633 GTK_TYPE_MOVEMENT_STEP,
638 * GtkLabel::copy-clipboard:
639 * @label: the object which received the signal
641 * The ::copy-clipboard signal is a
642 * <link linkend="keybinding-signals">keybinding signal</link>
643 * which gets emitted to copy the selection to the clipboard.
645 * The default binding for this signal is Ctrl-c.
647 signals[COPY_CLIPBOARD] =
648 g_signal_new (I_("copy-clipboard"),
649 G_OBJECT_CLASS_TYPE (gobject_class),
650 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
651 G_STRUCT_OFFSET (GtkLabelClass, copy_clipboard),
653 _gtk_marshal_VOID__VOID,
657 * GtkLabel::populate-popup:
658 * @label: The label on which the signal is emitted
659 * @menu: the menu that is being populated
661 * The ::populate-popup signal gets emitted before showing the
662 * context menu of the label. Note that only selectable labels
663 * have context menus.
665 * If you need to add items to the context menu, connect
666 * to this signal and append your menuitems to the @menu.
668 signals[POPULATE_POPUP] =
669 g_signal_new (I_("populate-popup"),
670 G_OBJECT_CLASS_TYPE (gobject_class),
672 G_STRUCT_OFFSET (GtkLabelClass, populate_popup),
674 _gtk_marshal_VOID__OBJECT,
679 * GtkLabel::activate-current-link:
680 * @label: The label on which the signal was emitted
682 * A <link linkend="keybinding-signals">keybinding signal</link>
683 * which gets emitted when the user activates a link in the label.
685 * Applications may also emit the signal with g_signal_emit_by_name()
686 * if they need to control activation of URIs programmatically.
688 * The default bindings for this signal are all forms of the Enter key.
692 signals[ACTIVATE_CURRENT_LINK] =
693 g_signal_new_class_handler ("activate-current-link",
694 G_TYPE_FROM_CLASS (gobject_class),
695 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
696 G_CALLBACK (gtk_label_activate_current_link),
698 _gtk_marshal_VOID__VOID,
702 * GtkLabel::activate-link:
703 * @label: The label on which the signal was emitted
704 * @uri: the URI that is activated
706 * The signal which gets emitted to activate a URI.
707 * Applications may connect to it to override the default behaviour,
708 * which is to call gtk_show_uri().
710 * Returns: %TRUE if the link has been activated
714 signals[ACTIVATE_LINK] =
715 g_signal_new ("activate-link",
716 G_TYPE_FROM_CLASS (gobject_class),
718 G_STRUCT_OFFSET (GtkLabelClass, activate_link),
719 _gtk_boolean_handled_accumulator, NULL,
720 _gtk_marshal_BOOLEAN__STRING,
721 G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
723 g_object_class_install_property (gobject_class,
725 g_param_spec_string ("label",
727 P_("The text of the label"),
729 GTK_PARAM_READWRITE));
730 g_object_class_install_property (gobject_class,
732 g_param_spec_boxed ("attributes",
734 P_("A list of style attributes to apply to the text of the label"),
735 PANGO_TYPE_ATTR_LIST,
736 GTK_PARAM_READWRITE));
737 g_object_class_install_property (gobject_class,
739 g_param_spec_boolean ("use-markup",
741 P_("The text of the label includes XML markup. See pango_parse_markup()"),
743 GTK_PARAM_READWRITE));
744 g_object_class_install_property (gobject_class,
746 g_param_spec_boolean ("use-underline",
748 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
750 GTK_PARAM_READWRITE));
752 g_object_class_install_property (gobject_class,
754 g_param_spec_enum ("justify",
756 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"),
757 GTK_TYPE_JUSTIFICATION,
759 GTK_PARAM_READWRITE));
761 g_object_class_install_property (gobject_class,
763 g_param_spec_string ("pattern",
765 P_("A string with _ characters in positions correspond to characters in the text to underline"),
767 GTK_PARAM_WRITABLE));
769 g_object_class_install_property (gobject_class,
771 g_param_spec_boolean ("wrap",
773 P_("If set, wrap lines if the text becomes too wide"),
775 GTK_PARAM_READWRITE));
777 * GtkLabel:wrap-mode:
779 * If line wrapping is on (see the #GtkLabel:wrap property) this controls
780 * how the line wrapping is done. The default is %PANGO_WRAP_WORD, which
781 * means wrap on word boundaries.
785 g_object_class_install_property (gobject_class,
787 g_param_spec_enum ("wrap-mode",
788 P_("Line wrap mode"),
789 P_("If wrap is set, controls how linewrapping is done"),
790 PANGO_TYPE_WRAP_MODE,
792 GTK_PARAM_READWRITE));
793 g_object_class_install_property (gobject_class,
795 g_param_spec_boolean ("selectable",
797 P_("Whether the label text can be selected with the mouse"),
799 GTK_PARAM_READWRITE));
800 g_object_class_install_property (gobject_class,
801 PROP_MNEMONIC_KEYVAL,
802 g_param_spec_uint ("mnemonic-keyval",
804 P_("The mnemonic accelerator key for this label"),
808 GTK_PARAM_READABLE));
809 g_object_class_install_property (gobject_class,
810 PROP_MNEMONIC_WIDGET,
811 g_param_spec_object ("mnemonic-widget",
812 P_("Mnemonic widget"),
813 P_("The widget to be activated when the label's mnemonic "
816 GTK_PARAM_READWRITE));
818 g_object_class_install_property (gobject_class,
819 PROP_CURSOR_POSITION,
820 g_param_spec_int ("cursor-position",
821 P_("Cursor Position"),
822 P_("The current position of the insertion cursor in chars"),
826 GTK_PARAM_READABLE));
828 g_object_class_install_property (gobject_class,
829 PROP_SELECTION_BOUND,
830 g_param_spec_int ("selection-bound",
831 P_("Selection Bound"),
832 P_("The position of the opposite end of the selection from the cursor in chars"),
836 GTK_PARAM_READABLE));
839 * GtkLabel:ellipsize:
841 * The preferred place to ellipsize the string, if the label does
842 * not have enough room to display the entire string, specified as a
843 * #PangoEllisizeMode.
845 * Note that setting this property to a value other than
846 * %PANGO_ELLIPSIZE_NONE has the side-effect that the label requests
847 * only enough space to display the ellipsis "...". In particular, this
848 * means that ellipsizing labels do not work well in notebook tabs, unless
849 * the tab's #GtkNotebook:tab-expand property is set to %TRUE. Other ways
850 * to set a label's width are gtk_widget_set_size_request() and
851 * gtk_label_set_width_chars().
855 g_object_class_install_property (gobject_class,
857 g_param_spec_enum ("ellipsize",
859 P_("The preferred place to ellipsize the string, if the label does not have enough room to display the entire string"),
860 PANGO_TYPE_ELLIPSIZE_MODE,
861 PANGO_ELLIPSIZE_NONE,
862 GTK_PARAM_READWRITE));
865 * GtkLabel:width-chars:
867 * The desired width of the label, in characters. If this property is set to
868 * -1, the width will be calculated automatically.
870 * See the section on <link linkend="label-text-layout">text layout</link>
871 * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
872 * determine the width of ellipsized and wrapped labels.
876 g_object_class_install_property (gobject_class,
878 g_param_spec_int ("width-chars",
879 P_("Width In Characters"),
880 P_("The desired width of the label, in characters"),
884 GTK_PARAM_READWRITE));
887 * GtkLabel:single-line-mode:
889 * Whether the label is in single line mode. In single line mode,
890 * the height of the label does not depend on the actual text, it
891 * is always set to ascent + descent of the font. This can be an
892 * advantage in situations where resizing the label because of text
893 * changes would be distracting, e.g. in a statusbar.
897 g_object_class_install_property (gobject_class,
898 PROP_SINGLE_LINE_MODE,
899 g_param_spec_boolean ("single-line-mode",
900 P_("Single Line Mode"),
901 P_("Whether the label is in single line mode"),
903 GTK_PARAM_READWRITE));
908 * The angle that the baseline of the label makes with the horizontal,
909 * in degrees, measured counterclockwise. An angle of 90 reads from
910 * from bottom to top, an angle of 270, from top to bottom. Ignored
911 * if the label is selectable, wrapped, or ellipsized.
915 g_object_class_install_property (gobject_class,
917 g_param_spec_double ("angle",
919 P_("Angle at which the label is rotated"),
923 GTK_PARAM_READWRITE));
926 * GtkLabel:max-width-chars:
928 * The desired maximum width of the label, in characters. If this property
929 * is set to -1, the width will be calculated automatically.
931 * See the section on <link linkend="label-text-layout">text layout</link>
932 * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
933 * determine the width of ellipsized and wrapped labels.
937 g_object_class_install_property (gobject_class,
938 PROP_MAX_WIDTH_CHARS,
939 g_param_spec_int ("max-width-chars",
940 P_("Maximum Width In Characters"),
941 P_("The desired maximum width of the label, in characters"),
945 GTK_PARAM_READWRITE));
948 * GtkLabel:track-visited-links:
950 * Set this property to %TRUE to make the label track which links
951 * have been clicked. It will then apply the ::visited-link-color
952 * color, instead of ::link-color.
956 g_object_class_install_property (gobject_class,
957 PROP_TRACK_VISITED_LINKS,
958 g_param_spec_boolean ("track-visited-links",
959 P_("Track visited links"),
960 P_("Whether visited links should be tracked"),
962 GTK_PARAM_READWRITE));
967 binding_set = gtk_binding_set_by_class (class);
969 /* Moving the insertion point */
970 add_move_binding (binding_set, GDK_KEY_Right, 0,
971 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
973 add_move_binding (binding_set, GDK_KEY_Left, 0,
974 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
976 add_move_binding (binding_set, GDK_KEY_KP_Right, 0,
977 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
979 add_move_binding (binding_set, GDK_KEY_KP_Left, 0,
980 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
982 add_move_binding (binding_set, GDK_KEY_f, GDK_CONTROL_MASK,
983 GTK_MOVEMENT_LOGICAL_POSITIONS, 1);
985 add_move_binding (binding_set, GDK_KEY_b, GDK_CONTROL_MASK,
986 GTK_MOVEMENT_LOGICAL_POSITIONS, -1);
988 add_move_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK,
989 GTK_MOVEMENT_WORDS, 1);
991 add_move_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK,
992 GTK_MOVEMENT_WORDS, -1);
994 add_move_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK,
995 GTK_MOVEMENT_WORDS, 1);
997 add_move_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK,
998 GTK_MOVEMENT_WORDS, -1);
1001 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
1003 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1005 G_TYPE_BOOLEAN, FALSE);
1007 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
1009 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1011 G_TYPE_BOOLEAN, TRUE);
1013 gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
1015 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1017 G_TYPE_BOOLEAN, FALSE);
1019 gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
1021 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1023 G_TYPE_BOOLEAN, TRUE);
1026 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1028 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1030 G_TYPE_BOOLEAN, FALSE);
1032 gtk_binding_entry_add_signal (binding_set, GDK_KEY_backslash, GDK_CONTROL_MASK,
1034 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1036 G_TYPE_BOOLEAN, FALSE);
1038 add_move_binding (binding_set, GDK_KEY_f, GDK_MOD1_MASK,
1039 GTK_MOVEMENT_WORDS, 1);
1041 add_move_binding (binding_set, GDK_KEY_b, GDK_MOD1_MASK,
1042 GTK_MOVEMENT_WORDS, -1);
1044 add_move_binding (binding_set, GDK_KEY_Home, 0,
1045 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1047 add_move_binding (binding_set, GDK_KEY_End, 0,
1048 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1050 add_move_binding (binding_set, GDK_KEY_KP_Home, 0,
1051 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1053 add_move_binding (binding_set, GDK_KEY_KP_End, 0,
1054 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1056 add_move_binding (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK,
1057 GTK_MOVEMENT_BUFFER_ENDS, -1);
1059 add_move_binding (binding_set, GDK_KEY_End, GDK_CONTROL_MASK,
1060 GTK_MOVEMENT_BUFFER_ENDS, 1);
1062 add_move_binding (binding_set, GDK_KEY_KP_Home, GDK_CONTROL_MASK,
1063 GTK_MOVEMENT_BUFFER_ENDS, -1);
1065 add_move_binding (binding_set, GDK_KEY_KP_End, GDK_CONTROL_MASK,
1066 GTK_MOVEMENT_BUFFER_ENDS, 1);
1069 gtk_binding_entry_add_signal (binding_set, GDK_KEY_c, GDK_CONTROL_MASK,
1070 "copy-clipboard", 0);
1072 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Return, 0,
1073 "activate-current-link", 0);
1074 gtk_binding_entry_add_signal (binding_set, GDK_KEY_ISO_Enter, 0,
1075 "activate-current-link", 0);
1076 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Enter, 0,
1077 "activate-current-link", 0);
1079 g_type_class_add_private (class, sizeof (GtkLabelPrivate));
1081 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_LABEL_ACCESSIBLE);
1085 gtk_label_set_property (GObject *object,
1087 const GValue *value,
1090 GtkLabel *label = GTK_LABEL (object);
1095 gtk_label_set_label (label, g_value_get_string (value));
1097 case PROP_ATTRIBUTES:
1098 gtk_label_set_attributes (label, g_value_get_boxed (value));
1100 case PROP_USE_MARKUP:
1101 gtk_label_set_use_markup (label, g_value_get_boolean (value));
1103 case PROP_USE_UNDERLINE:
1104 gtk_label_set_use_underline (label, g_value_get_boolean (value));
1107 gtk_label_set_justify (label, g_value_get_enum (value));
1110 gtk_label_set_pattern (label, g_value_get_string (value));
1113 gtk_label_set_line_wrap (label, g_value_get_boolean (value));
1115 case PROP_WRAP_MODE:
1116 gtk_label_set_line_wrap_mode (label, g_value_get_enum (value));
1118 case PROP_SELECTABLE:
1119 gtk_label_set_selectable (label, g_value_get_boolean (value));
1121 case PROP_MNEMONIC_WIDGET:
1122 gtk_label_set_mnemonic_widget (label, (GtkWidget*) g_value_get_object (value));
1124 case PROP_ELLIPSIZE:
1125 gtk_label_set_ellipsize (label, g_value_get_enum (value));
1127 case PROP_WIDTH_CHARS:
1128 gtk_label_set_width_chars (label, g_value_get_int (value));
1130 case PROP_SINGLE_LINE_MODE:
1131 gtk_label_set_single_line_mode (label, g_value_get_boolean (value));
1134 gtk_label_set_angle (label, g_value_get_double (value));
1136 case PROP_MAX_WIDTH_CHARS:
1137 gtk_label_set_max_width_chars (label, g_value_get_int (value));
1139 case PROP_TRACK_VISITED_LINKS:
1140 gtk_label_set_track_visited_links (label, g_value_get_boolean (value));
1143 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1149 gtk_label_get_property (GObject *object,
1154 GtkLabel *label = GTK_LABEL (object);
1155 GtkLabelPrivate *priv = label->priv;
1160 g_value_set_string (value, priv->label);
1162 case PROP_ATTRIBUTES:
1163 g_value_set_boxed (value, priv->attrs);
1165 case PROP_USE_MARKUP:
1166 g_value_set_boolean (value, priv->use_markup);
1168 case PROP_USE_UNDERLINE:
1169 g_value_set_boolean (value, priv->use_underline);
1172 g_value_set_enum (value, priv->jtype);
1175 g_value_set_boolean (value, priv->wrap);
1177 case PROP_WRAP_MODE:
1178 g_value_set_enum (value, priv->wrap_mode);
1180 case PROP_SELECTABLE:
1181 g_value_set_boolean (value, gtk_label_get_selectable (label));
1183 case PROP_MNEMONIC_KEYVAL:
1184 g_value_set_uint (value, priv->mnemonic_keyval);
1186 case PROP_MNEMONIC_WIDGET:
1187 g_value_set_object (value, (GObject*) priv->mnemonic_widget);
1189 case PROP_CURSOR_POSITION:
1190 g_value_set_int (value, _gtk_label_get_cursor_position (label));
1192 case PROP_SELECTION_BOUND:
1193 g_value_set_int (value, _gtk_label_get_selection_bound (label));
1195 case PROP_ELLIPSIZE:
1196 g_value_set_enum (value, priv->ellipsize);
1198 case PROP_WIDTH_CHARS:
1199 g_value_set_int (value, gtk_label_get_width_chars (label));
1201 case PROP_SINGLE_LINE_MODE:
1202 g_value_set_boolean (value, gtk_label_get_single_line_mode (label));
1205 g_value_set_double (value, gtk_label_get_angle (label));
1207 case PROP_MAX_WIDTH_CHARS:
1208 g_value_set_int (value, gtk_label_get_max_width_chars (label));
1210 case PROP_TRACK_VISITED_LINKS:
1211 g_value_set_boolean (value, gtk_label_get_track_visited_links (label));
1214 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1220 gtk_label_init (GtkLabel *label)
1222 GtkLabelPrivate *priv;
1224 label->priv = G_TYPE_INSTANCE_GET_PRIVATE (label,
1229 gtk_widget_set_has_window (GTK_WIDGET (label), FALSE);
1231 priv->width_chars = -1;
1232 priv->max_width_chars = -1;
1235 priv->jtype = GTK_JUSTIFY_LEFT;
1237 priv->wrap_mode = PANGO_WRAP_WORD;
1238 priv->ellipsize = PANGO_ELLIPSIZE_NONE;
1240 priv->use_underline = FALSE;
1241 priv->use_markup = FALSE;
1242 priv->pattern_set = FALSE;
1243 priv->track_links = TRUE;
1245 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
1246 priv->layout = NULL;
1250 priv->mnemonic_widget = NULL;
1251 priv->mnemonic_window = NULL;
1253 priv->mnemonics_visible = TRUE;
1255 gtk_label_set_text (label, "");
1260 gtk_label_buildable_interface_init (GtkBuildableIface *iface)
1262 buildable_parent_iface = g_type_interface_peek_parent (iface);
1264 iface->custom_tag_start = gtk_label_buildable_custom_tag_start;
1265 iface->custom_finished = gtk_label_buildable_custom_finished;
1269 GtkBuilder *builder;
1271 PangoAttrList *attrs;
1274 static PangoAttribute *
1275 attribute_from_text (GtkBuilder *builder,
1280 PangoAttribute *attribute = NULL;
1282 PangoLanguage *language;
1283 PangoFontDescription *font_desc;
1285 GValue val = G_VALUE_INIT;
1287 if (!gtk_builder_value_from_string_type (builder, PANGO_TYPE_ATTR_TYPE, name, &val, error))
1290 type = g_value_get_enum (&val);
1291 g_value_unset (&val);
1295 /* PangoAttrLanguage */
1296 case PANGO_ATTR_LANGUAGE:
1297 if ((language = pango_language_from_string (value)))
1299 attribute = pango_attr_language_new (language);
1300 g_value_init (&val, G_TYPE_INT);
1304 case PANGO_ATTR_STYLE:
1305 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STYLE, value, &val, error))
1306 attribute = pango_attr_style_new (g_value_get_enum (&val));
1308 case PANGO_ATTR_WEIGHT:
1309 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_WEIGHT, value, &val, error))
1310 attribute = pango_attr_weight_new (g_value_get_enum (&val));
1312 case PANGO_ATTR_VARIANT:
1313 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_VARIANT, value, &val, error))
1314 attribute = pango_attr_variant_new (g_value_get_enum (&val));
1316 case PANGO_ATTR_STRETCH:
1317 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STRETCH, value, &val, error))
1318 attribute = pango_attr_stretch_new (g_value_get_enum (&val));
1320 case PANGO_ATTR_UNDERLINE:
1321 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_UNDERLINE, value, &val, NULL))
1322 attribute = pango_attr_underline_new (g_value_get_enum (&val));
1325 /* XXX: allow boolean for backwards compat, so ignore error */
1326 /* Deprecate this somehow */
1327 g_value_unset (&val);
1328 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1329 attribute = pango_attr_underline_new (g_value_get_boolean (&val));
1332 case PANGO_ATTR_STRIKETHROUGH:
1333 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1334 attribute = pango_attr_strikethrough_new (g_value_get_boolean (&val));
1336 case PANGO_ATTR_GRAVITY:
1337 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY, value, &val, error))
1338 attribute = pango_attr_gravity_new (g_value_get_enum (&val));
1340 case PANGO_ATTR_GRAVITY_HINT:
1341 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY_HINT,
1342 value, &val, error))
1343 attribute = pango_attr_gravity_hint_new (g_value_get_enum (&val));
1345 /* PangoAttrString */
1346 case PANGO_ATTR_FAMILY:
1347 attribute = pango_attr_family_new (value);
1348 g_value_init (&val, G_TYPE_INT);
1352 case PANGO_ATTR_SIZE:
1353 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1354 value, &val, error))
1355 attribute = pango_attr_size_new (g_value_get_int (&val));
1357 case PANGO_ATTR_ABSOLUTE_SIZE:
1358 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1359 value, &val, error))
1360 attribute = pango_attr_size_new_absolute (g_value_get_int (&val));
1363 /* PangoAttrFontDesc */
1364 case PANGO_ATTR_FONT_DESC:
1365 if ((font_desc = pango_font_description_from_string (value)))
1367 attribute = pango_attr_font_desc_new (font_desc);
1368 pango_font_description_free (font_desc);
1369 g_value_init (&val, G_TYPE_INT);
1373 /* PangoAttrColor */
1374 case PANGO_ATTR_FOREGROUND:
1375 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1376 value, &val, error))
1378 color = g_value_get_boxed (&val);
1379 attribute = pango_attr_foreground_new (color->red, color->green, color->blue);
1382 case PANGO_ATTR_BACKGROUND:
1383 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1384 value, &val, error))
1386 color = g_value_get_boxed (&val);
1387 attribute = pango_attr_background_new (color->red, color->green, color->blue);
1390 case PANGO_ATTR_UNDERLINE_COLOR:
1391 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1392 value, &val, error))
1394 color = g_value_get_boxed (&val);
1395 attribute = pango_attr_underline_color_new (color->red, color->green, color->blue);
1398 case PANGO_ATTR_STRIKETHROUGH_COLOR:
1399 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1400 value, &val, error))
1402 color = g_value_get_boxed (&val);
1403 attribute = pango_attr_strikethrough_color_new (color->red, color->green, color->blue);
1407 /* PangoAttrShape */
1408 case PANGO_ATTR_SHAPE:
1409 /* Unsupported for now */
1411 /* PangoAttrFloat */
1412 case PANGO_ATTR_SCALE:
1413 if (gtk_builder_value_from_string_type (builder, G_TYPE_DOUBLE,
1414 value, &val, error))
1415 attribute = pango_attr_scale_new (g_value_get_double (&val));
1418 case PANGO_ATTR_INVALID:
1419 case PANGO_ATTR_LETTER_SPACING:
1420 case PANGO_ATTR_RISE:
1421 case PANGO_ATTR_FALLBACK:
1426 g_value_unset (&val);
1433 pango_start_element (GMarkupParseContext *context,
1434 const gchar *element_name,
1435 const gchar **names,
1436 const gchar **values,
1440 PangoParserData *data = (PangoParserData*)user_data;
1441 GValue val = G_VALUE_INIT;
1443 gint line_number, char_number;
1445 if (strcmp (element_name, "attribute") == 0)
1447 PangoAttribute *attr = NULL;
1448 const gchar *name = NULL;
1449 const gchar *value = NULL;
1450 const gchar *start = NULL;
1451 const gchar *end = NULL;
1452 guint start_val = 0;
1453 guint end_val = G_MAXUINT;
1455 for (i = 0; names[i]; i++)
1457 if (strcmp (names[i], "name") == 0)
1459 else if (strcmp (names[i], "value") == 0)
1461 else if (strcmp (names[i], "start") == 0)
1463 else if (strcmp (names[i], "end") == 0)
1467 g_markup_parse_context_get_position (context,
1472 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1473 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1475 line_number, char_number, names[i], "attribute");
1480 if (!name || !value)
1482 g_markup_parse_context_get_position (context,
1487 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1488 "%s:%d:%d <%s> requires attribute \"%s\"",
1490 line_number, char_number, "attribute",
1491 name ? "value" : "name");
1497 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1498 start, &val, error))
1500 start_val = g_value_get_uint (&val);
1501 g_value_unset (&val);
1506 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1509 end_val = g_value_get_uint (&val);
1510 g_value_unset (&val);
1513 attr = attribute_from_text (data->builder, name, value, error);
1517 attr->start_index = start_val;
1518 attr->end_index = end_val;
1521 data->attrs = pango_attr_list_new ();
1523 pango_attr_list_insert (data->attrs, attr);
1526 else if (strcmp (element_name, "attributes") == 0)
1529 g_warning ("Unsupported tag for GtkLabel: %s\n", element_name);
1532 static const GMarkupParser pango_parser =
1534 pango_start_element,
1538 gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
1539 GtkBuilder *builder,
1541 const gchar *tagname,
1542 GMarkupParser *parser,
1545 if (buildable_parent_iface->custom_tag_start (buildable, builder, child,
1546 tagname, parser, data))
1549 if (strcmp (tagname, "attributes") == 0)
1551 PangoParserData *parser_data;
1553 parser_data = g_slice_new0 (PangoParserData);
1554 parser_data->builder = g_object_ref (builder);
1555 parser_data->object = g_object_ref (buildable);
1556 *parser = pango_parser;
1557 *data = parser_data;
1564 gtk_label_buildable_custom_finished (GtkBuildable *buildable,
1565 GtkBuilder *builder,
1567 const gchar *tagname,
1570 PangoParserData *data;
1572 buildable_parent_iface->custom_finished (buildable, builder, child,
1573 tagname, user_data);
1575 if (strcmp (tagname, "attributes") == 0)
1577 data = (PangoParserData*)user_data;
1581 gtk_label_set_attributes (GTK_LABEL (buildable), data->attrs);
1582 pango_attr_list_unref (data->attrs);
1585 g_object_unref (data->object);
1586 g_object_unref (data->builder);
1587 g_slice_free (PangoParserData, data);
1594 * @str: The text of the label
1596 * Creates a new label with the given text inside it. You can
1597 * pass %NULL to get an empty label widget.
1599 * Return value: the new #GtkLabel
1602 gtk_label_new (const gchar *str)
1606 label = g_object_new (GTK_TYPE_LABEL, NULL);
1609 gtk_label_set_text (label, str);
1611 return GTK_WIDGET (label);
1615 * gtk_label_new_with_mnemonic:
1616 * @str: The text of the label, with an underscore in front of the
1617 * mnemonic character
1619 * Creates a new #GtkLabel, containing the text in @str.
1621 * If characters in @str are preceded by an underscore, they are
1622 * underlined. If you need a literal underscore character in a label, use
1623 * '__' (two underscores). The first underlined character represents a
1624 * keyboard accelerator called a mnemonic. The mnemonic key can be used
1625 * to activate another widget, chosen automatically, or explicitly using
1626 * gtk_label_set_mnemonic_widget().
1628 * If gtk_label_set_mnemonic_widget() is not called, then the first
1629 * activatable ancestor of the #GtkLabel will be chosen as the mnemonic
1630 * widget. For instance, if the label is inside a button or menu item,
1631 * the button or menu item will automatically become the mnemonic widget
1632 * and be activated by the mnemonic.
1634 * Return value: the new #GtkLabel
1637 gtk_label_new_with_mnemonic (const gchar *str)
1641 label = g_object_new (GTK_TYPE_LABEL, NULL);
1644 gtk_label_set_text_with_mnemonic (label, str);
1646 return GTK_WIDGET (label);
1650 gtk_label_mnemonic_activate (GtkWidget *widget,
1651 gboolean group_cycling)
1653 GtkLabel *label = GTK_LABEL (widget);
1654 GtkLabelPrivate *priv = label->priv;
1657 if (priv->mnemonic_widget)
1658 return gtk_widget_mnemonic_activate (priv->mnemonic_widget, group_cycling);
1660 /* Try to find the widget to activate by traversing the
1661 * widget's ancestry.
1663 parent = gtk_widget_get_parent (widget);
1665 if (GTK_IS_NOTEBOOK (parent))
1670 if (gtk_widget_get_can_focus (parent) ||
1671 (!group_cycling && GTK_WIDGET_GET_CLASS (parent)->activate_signal) ||
1672 GTK_IS_NOTEBOOK (gtk_widget_get_parent (parent)) ||
1673 GTK_IS_MENU_ITEM (parent))
1674 return gtk_widget_mnemonic_activate (parent, group_cycling);
1675 parent = gtk_widget_get_parent (parent);
1678 /* barf if there was nothing to activate */
1679 g_warning ("Couldn't find a target for a mnemonic activation.");
1680 gtk_widget_error_bell (widget);
1686 gtk_label_setup_mnemonic (GtkLabel *label,
1689 GtkLabelPrivate *priv = label->priv;
1690 GtkWidget *widget = GTK_WIDGET (label);
1691 GtkWidget *toplevel;
1692 GtkWidget *mnemonic_menu;
1694 mnemonic_menu = g_object_get_data (G_OBJECT (label), "gtk-mnemonic-menu");
1696 if (last_key != GDK_KEY_VoidSymbol)
1698 if (priv->mnemonic_window)
1700 gtk_window_remove_mnemonic (priv->mnemonic_window,
1703 priv->mnemonic_window = NULL;
1707 _gtk_menu_shell_remove_mnemonic (GTK_MENU_SHELL (mnemonic_menu),
1710 mnemonic_menu = NULL;
1714 if (priv->mnemonic_keyval == GDK_KEY_VoidSymbol)
1717 connect_mnemonics_visible_notify (GTK_LABEL (widget));
1719 toplevel = gtk_widget_get_toplevel (widget);
1720 if (gtk_widget_is_toplevel (toplevel))
1722 GtkWidget *menu_shell;
1724 menu_shell = gtk_widget_get_ancestor (widget,
1725 GTK_TYPE_MENU_SHELL);
1729 _gtk_menu_shell_add_mnemonic (GTK_MENU_SHELL (menu_shell),
1730 priv->mnemonic_keyval,
1732 mnemonic_menu = menu_shell;
1735 if (!GTK_IS_MENU (menu_shell))
1737 gtk_window_add_mnemonic (GTK_WINDOW (toplevel),
1738 priv->mnemonic_keyval,
1740 priv->mnemonic_window = GTK_WINDOW (toplevel);
1745 g_object_set_data (G_OBJECT (label), I_("gtk-mnemonic-menu"), mnemonic_menu);
1749 gtk_label_hierarchy_changed (GtkWidget *widget,
1750 GtkWidget *old_toplevel)
1752 GtkLabel *label = GTK_LABEL (widget);
1753 GtkLabelPrivate *priv = label->priv;
1755 gtk_label_setup_mnemonic (label, priv->mnemonic_keyval);
1759 label_shortcut_setting_apply (GtkLabel *label)
1761 gtk_label_recalculate (label);
1762 if (GTK_IS_ACCEL_LABEL (label))
1763 gtk_accel_label_refetch (GTK_ACCEL_LABEL (label));
1767 label_shortcut_setting_traverse_container (GtkWidget *widget,
1770 if (GTK_IS_LABEL (widget))
1771 label_shortcut_setting_apply (GTK_LABEL (widget));
1772 else if (GTK_IS_CONTAINER (widget))
1773 gtk_container_forall (GTK_CONTAINER (widget),
1774 label_shortcut_setting_traverse_container, data);
1778 label_shortcut_setting_changed (GtkSettings *settings)
1782 list = gtk_window_list_toplevels ();
1784 for (l = list; l ; l = l->next)
1786 GtkWidget *widget = l->data;
1788 if (gtk_widget_get_settings (widget) == settings)
1789 gtk_container_forall (GTK_CONTAINER (widget),
1790 label_shortcut_setting_traverse_container, NULL);
1797 mnemonics_visible_apply (GtkWidget *widget,
1798 gboolean mnemonics_visible)
1800 GtkLabel *label = GTK_LABEL (widget);
1801 GtkLabelPrivate *priv = label->priv;
1803 mnemonics_visible = mnemonics_visible != FALSE;
1805 if (priv->mnemonics_visible != mnemonics_visible)
1807 priv->mnemonics_visible = mnemonics_visible;
1809 gtk_label_recalculate (label);
1814 label_mnemonics_visible_traverse_container (GtkWidget *widget,
1817 gboolean mnemonics_visible = GPOINTER_TO_INT (data);
1819 _gtk_label_mnemonics_visible_apply_recursively (widget, mnemonics_visible);
1823 _gtk_label_mnemonics_visible_apply_recursively (GtkWidget *widget,
1824 gboolean mnemonics_visible)
1826 if (GTK_IS_LABEL (widget))
1827 mnemonics_visible_apply (widget, mnemonics_visible);
1828 else if (GTK_IS_CONTAINER (widget))
1829 gtk_container_forall (GTK_CONTAINER (widget),
1830 label_mnemonics_visible_traverse_container,
1831 GINT_TO_POINTER (mnemonics_visible));
1835 label_mnemonics_visible_changed (GtkWindow *window,
1839 gboolean mnemonics_visible;
1841 g_object_get (window, "mnemonics-visible", &mnemonics_visible, NULL);
1843 gtk_container_forall (GTK_CONTAINER (window),
1844 label_mnemonics_visible_traverse_container,
1845 GINT_TO_POINTER (mnemonics_visible));
1849 gtk_label_screen_changed (GtkWidget *widget,
1850 GdkScreen *old_screen)
1852 GtkSettings *settings;
1853 gboolean shortcuts_connected;
1855 if (!gtk_widget_has_screen (widget))
1858 settings = gtk_widget_get_settings (widget);
1860 shortcuts_connected =
1861 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (settings),
1862 "gtk-label-shortcuts-connected"));
1864 if (! shortcuts_connected)
1866 g_signal_connect (settings, "notify::gtk-enable-mnemonics",
1867 G_CALLBACK (label_shortcut_setting_changed),
1869 g_signal_connect (settings, "notify::gtk-enable-accels",
1870 G_CALLBACK (label_shortcut_setting_changed),
1873 g_object_set_data (G_OBJECT (settings), "gtk-label-shortcuts-connected",
1874 GINT_TO_POINTER (TRUE));
1877 label_shortcut_setting_apply (GTK_LABEL (widget));
1882 label_mnemonic_widget_weak_notify (gpointer data,
1883 GObject *where_the_object_was)
1885 GtkLabel *label = data;
1886 GtkLabelPrivate *priv = label->priv;
1888 priv->mnemonic_widget = NULL;
1889 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1893 * gtk_label_set_mnemonic_widget:
1894 * @label: a #GtkLabel
1895 * @widget: (allow-none): the target #GtkWidget
1897 * If the label has been set so that it has an mnemonic key (using
1898 * i.e. gtk_label_set_markup_with_mnemonic(),
1899 * gtk_label_set_text_with_mnemonic(), gtk_label_new_with_mnemonic()
1900 * or the "use_underline" property) the label can be associated with a
1901 * widget that is the target of the mnemonic. When the label is inside
1902 * a widget (like a #GtkButton or a #GtkNotebook tab) it is
1903 * automatically associated with the correct widget, but sometimes
1904 * (i.e. when the target is a #GtkEntry next to the label) you need to
1905 * set it explicitly using this function.
1907 * The target widget will be accelerated by emitting the
1908 * GtkWidget::mnemonic-activate signal on it. The default handler for
1909 * this signal will activate the widget if there are no mnemonic collisions
1910 * and toggle focus between the colliding widgets otherwise.
1913 gtk_label_set_mnemonic_widget (GtkLabel *label,
1916 GtkLabelPrivate *priv;
1918 g_return_if_fail (GTK_IS_LABEL (label));
1923 g_return_if_fail (GTK_IS_WIDGET (widget));
1925 if (priv->mnemonic_widget)
1927 gtk_widget_remove_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1928 g_object_weak_unref (G_OBJECT (priv->mnemonic_widget),
1929 label_mnemonic_widget_weak_notify,
1932 priv->mnemonic_widget = widget;
1933 if (priv->mnemonic_widget)
1935 g_object_weak_ref (G_OBJECT (priv->mnemonic_widget),
1936 label_mnemonic_widget_weak_notify,
1938 gtk_widget_add_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1941 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1945 * gtk_label_get_mnemonic_widget:
1946 * @label: a #GtkLabel
1948 * Retrieves the target of the mnemonic (keyboard shortcut) of this
1949 * label. See gtk_label_set_mnemonic_widget().
1951 * Return value: (transfer none): the target of the label's mnemonic,
1952 * or %NULL if none has been set and the default algorithm will be used.
1955 gtk_label_get_mnemonic_widget (GtkLabel *label)
1957 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1959 return label->priv->mnemonic_widget;
1963 * gtk_label_get_mnemonic_keyval:
1964 * @label: a #GtkLabel
1966 * If the label has been set so that it has an mnemonic key this function
1967 * returns the keyval used for the mnemonic accelerator. If there is no
1968 * mnemonic set up it returns #GDK_VoidSymbol.
1970 * Returns: GDK keyval usable for accelerators, or #GDK_VoidSymbol
1973 gtk_label_get_mnemonic_keyval (GtkLabel *label)
1975 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_KEY_VoidSymbol);
1977 return label->priv->mnemonic_keyval;
1981 gtk_label_set_text_internal (GtkLabel *label,
1984 GtkLabelPrivate *priv = label->priv;
1985 gboolean text_changed;
1987 text_changed = g_strcmp0 (priv->text, str) != 0;
1989 g_free (priv->text);
1993 gtk_label_select_region_index (label, 0, 0);
1997 gtk_label_set_label_internal (GtkLabel *label,
2000 GtkLabelPrivate *priv = label->priv;
2002 g_free (priv->label);
2006 g_object_notify (G_OBJECT (label), "label");
2010 gtk_label_set_use_markup_internal (GtkLabel *label,
2013 GtkLabelPrivate *priv = label->priv;
2016 if (priv->use_markup != val)
2018 priv->use_markup = val;
2020 g_object_notify (G_OBJECT (label), "use-markup");
2025 gtk_label_set_use_underline_internal (GtkLabel *label,
2028 GtkLabelPrivate *priv = label->priv;
2031 if (priv->use_underline != val)
2033 priv->use_underline = val;
2035 g_object_notify (G_OBJECT (label), "use-underline");
2040 my_pango_attr_list_merge_filter (PangoAttribute *attribute,
2043 pango_attr_list_change (list, pango_attribute_copy (attribute));
2048 my_pango_attr_list_merge (PangoAttrList *into,
2049 PangoAttrList *from)
2051 pango_attr_list_filter (from, my_pango_attr_list_merge_filter, into);
2054 /* Calculates text, attrs and mnemonic_keyval from
2055 * label, use_underline and use_markup
2058 gtk_label_recalculate (GtkLabel *label)
2060 GtkLabelPrivate *priv = label->priv;
2061 guint keyval = priv->mnemonic_keyval;
2063 gtk_label_clear_links (label);
2065 if (priv->use_markup)
2066 gtk_label_set_markup_internal (label, priv->label, priv->use_underline);
2067 else if (priv->use_underline)
2068 gtk_label_set_uline_text_internal (label, priv->label);
2071 if (!priv->pattern_set)
2073 if (priv->markup_attrs)
2074 pango_attr_list_unref (priv->markup_attrs);
2075 priv->markup_attrs = NULL;
2077 gtk_label_set_text_internal (label, g_strdup (priv->label));
2080 if (!priv->use_underline)
2081 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
2083 if (keyval != priv->mnemonic_keyval)
2085 gtk_label_setup_mnemonic (label, keyval);
2086 g_object_notify (G_OBJECT (label), "mnemonic-keyval");
2089 gtk_label_clear_layout (label);
2090 gtk_label_clear_select_info (label);
2091 gtk_widget_queue_resize (GTK_WIDGET (label));
2095 * gtk_label_set_text:
2096 * @label: a #GtkLabel
2097 * @str: The text you want to set
2099 * Sets the text within the #GtkLabel widget. It overwrites any text that
2102 * This will also clear any previously set mnemonic accelerators.
2105 gtk_label_set_text (GtkLabel *label,
2108 g_return_if_fail (GTK_IS_LABEL (label));
2110 g_object_freeze_notify (G_OBJECT (label));
2112 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2113 gtk_label_set_use_markup_internal (label, FALSE);
2114 gtk_label_set_use_underline_internal (label, FALSE);
2116 gtk_label_recalculate (label);
2118 g_object_thaw_notify (G_OBJECT (label));
2122 * gtk_label_set_attributes:
2123 * @label: a #GtkLabel
2124 * @attrs: a #PangoAttrList
2126 * Sets a #PangoAttrList; the attributes in the list are applied to the
2129 * <note><para>The attributes set with this function will be applied
2130 * and merged with any other attributes previously effected by way
2131 * of the #GtkLabel:use-underline or #GtkLabel:use-markup properties.
2132 * While it is not recommended to mix markup strings with manually set
2133 * attributes, if you must; know that the attributes will be applied
2134 * to the label after the markup string is parsed.</para></note>
2137 gtk_label_set_attributes (GtkLabel *label,
2138 PangoAttrList *attrs)
2140 GtkLabelPrivate *priv = label->priv;
2142 g_return_if_fail (GTK_IS_LABEL (label));
2145 pango_attr_list_ref (attrs);
2148 pango_attr_list_unref (priv->attrs);
2149 priv->attrs = attrs;
2151 g_object_notify (G_OBJECT (label), "attributes");
2153 gtk_label_clear_layout (label);
2154 gtk_widget_queue_resize (GTK_WIDGET (label));
2158 * gtk_label_get_attributes:
2159 * @label: a #GtkLabel
2161 * Gets the attribute list that was set on the label using
2162 * gtk_label_set_attributes(), if any. This function does
2163 * not reflect attributes that come from the labels markup
2164 * (see gtk_label_set_markup()). If you want to get the
2165 * effective attributes for the label, use
2166 * pango_layout_get_attribute (gtk_label_get_layout (label)).
2168 * Return value: (transfer none): the attribute list, or %NULL
2172 gtk_label_get_attributes (GtkLabel *label)
2174 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2176 return label->priv->attrs;
2180 * gtk_label_set_label:
2181 * @label: a #GtkLabel
2182 * @str: the new text to set for the label
2184 * Sets the text of the label. The label is interpreted as
2185 * including embedded underlines and/or Pango markup depending
2186 * on the values of the #GtkLabel:use-underline" and
2187 * #GtkLabel:use-markup properties.
2190 gtk_label_set_label (GtkLabel *label,
2193 g_return_if_fail (GTK_IS_LABEL (label));
2195 g_object_freeze_notify (G_OBJECT (label));
2197 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2198 gtk_label_recalculate (label);
2200 g_object_thaw_notify (G_OBJECT (label));
2204 * gtk_label_get_label:
2205 * @label: a #GtkLabel
2207 * Fetches the text from a label widget including any embedded
2208 * underlines indicating mnemonics and Pango markup. (See
2209 * gtk_label_get_text()).
2211 * Return value: the text of the label widget. This string is
2212 * owned by the widget and must not be modified or freed.
2215 gtk_label_get_label (GtkLabel *label)
2217 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2219 return label->priv->label;
2231 start_element_handler (GMarkupParseContext *context,
2232 const gchar *element_name,
2233 const gchar **attribute_names,
2234 const gchar **attribute_values,
2238 GtkLabelPrivate *priv;
2239 UriParserData *pdata = user_data;
2241 if (strcmp (element_name, "a") == 0)
2244 const gchar *uri = NULL;
2245 const gchar *title = NULL;
2246 gboolean visited = FALSE;
2251 g_markup_parse_context_get_position (context, &line_number, &char_number);
2253 for (i = 0; attribute_names[i] != NULL; i++)
2255 const gchar *attr = attribute_names[i];
2257 if (strcmp (attr, "href") == 0)
2258 uri = attribute_values[i];
2259 else if (strcmp (attr, "title") == 0)
2260 title = attribute_values[i];
2265 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
2266 "Attribute '%s' is not allowed on the <a> tag "
2267 "on line %d char %d",
2268 attr, line_number, char_number);
2277 G_MARKUP_ERROR_INVALID_CONTENT,
2278 "Attribute 'href' was missing on the <a> tag "
2279 "on line %d char %d",
2280 line_number, char_number);
2285 priv = pdata->label->priv;
2286 if (priv->track_links && priv->select_info)
2289 for (l = priv->select_info->links; l; l = l->next)
2292 if (strcmp (uri, link->uri) == 0)
2294 visited = link->visited;
2300 link = g_new0 (GtkLabelLink, 1);
2301 link->uri = g_strdup (uri);
2302 link->title = g_strdup (title);
2303 link->visited = visited;
2304 link->start = pdata->text_len;
2305 pdata->links = g_list_prepend (pdata->links, link);
2311 g_string_append_c (pdata->new_str, '<');
2312 g_string_append (pdata->new_str, element_name);
2314 for (i = 0; attribute_names[i] != NULL; i++)
2316 const gchar *attr = attribute_names[i];
2317 const gchar *value = attribute_values[i];
2320 newvalue = g_markup_escape_text (value, -1);
2322 g_string_append_c (pdata->new_str, ' ');
2323 g_string_append (pdata->new_str, attr);
2324 g_string_append (pdata->new_str, "=\"");
2325 g_string_append (pdata->new_str, newvalue);
2326 g_string_append_c (pdata->new_str, '\"');
2330 g_string_append_c (pdata->new_str, '>');
2335 end_element_handler (GMarkupParseContext *context,
2336 const gchar *element_name,
2340 UriParserData *pdata = user_data;
2342 if (!strcmp (element_name, "a"))
2344 GtkLabelLink *link = pdata->links->data;
2345 link->end = pdata->text_len;
2349 g_string_append (pdata->new_str, "</");
2350 g_string_append (pdata->new_str, element_name);
2351 g_string_append_c (pdata->new_str, '>');
2356 text_handler (GMarkupParseContext *context,
2362 UriParserData *pdata = user_data;
2365 newtext = g_markup_escape_text (text, text_len);
2366 g_string_append (pdata->new_str, newtext);
2367 pdata->text_len += text_len;
2371 static const GMarkupParser markup_parser =
2373 start_element_handler,
2374 end_element_handler,
2381 xml_isspace (gchar c)
2383 return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
2387 link_free (GtkLabelLink *link)
2390 g_free (link->title);
2395 gtk_label_get_link_colors (GtkWidget *widget,
2396 GdkColor *link_color,
2397 GdkColor *visited_link_color)
2399 GtkStyleContext *context;
2400 GdkColor *link, *visited;
2402 context = gtk_widget_get_style_context (widget);
2403 gtk_style_context_get_style (context,
2404 "link-color", &link,
2405 "visited-link-color", &visited,
2409 *link_color = *link;
2410 gdk_color_free (link);
2413 *link_color = default_link_color;
2417 *visited_link_color = *visited;
2418 gdk_color_free (visited);
2421 *visited_link_color = default_visited_link_color;
2425 parse_uri_markup (GtkLabel *label,
2431 GMarkupParseContext *context = NULL;
2432 const gchar *p, *end;
2433 gboolean needs_root = TRUE;
2435 UriParserData pdata;
2437 length = strlen (str);
2441 pdata.label = label;
2443 pdata.new_str = g_string_sized_new (length);
2446 while (p != end && xml_isspace (*p))
2449 if (end - p >= 8 && strncmp (p, "<markup>", 8) == 0)
2452 context = g_markup_parse_context_new (&markup_parser, 0, &pdata, NULL);
2456 if (!g_markup_parse_context_parse (context, "<markup>", -1, error))
2460 if (!g_markup_parse_context_parse (context, str, length, error))
2465 if (!g_markup_parse_context_parse (context, "</markup>", -1, error))
2469 if (!g_markup_parse_context_end_parse (context, error))
2472 g_markup_parse_context_free (context);
2474 *new_str = g_string_free (pdata.new_str, FALSE);
2475 *links = pdata.links;
2480 g_markup_parse_context_free (context);
2481 g_string_free (pdata.new_str, TRUE);
2482 g_list_free_full (pdata.links, (GDestroyNotify) link_free);
2488 gtk_label_ensure_has_tooltip (GtkLabel *label)
2490 GtkLabelPrivate *priv = label->priv;
2492 gboolean has_tooltip = FALSE;
2494 for (l = priv->select_info->links; l; l = l->next)
2496 GtkLabelLink *link = l->data;
2504 gtk_widget_set_has_tooltip (GTK_WIDGET (label), has_tooltip);
2508 gtk_label_set_markup_internal (GtkLabel *label,
2510 gboolean with_uline)
2512 GtkLabelPrivate *priv = label->priv;
2514 GError *error = NULL;
2515 PangoAttrList *attrs = NULL;
2516 gunichar accel_char = 0;
2518 GList *links = NULL;
2520 if (!parse_uri_markup (label, str, &new_str, &links, &error))
2522 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2524 g_error_free (error);
2530 gtk_label_ensure_select_info (label);
2531 priv->select_info->links = g_list_reverse (links);
2532 gtk_label_ensure_has_tooltip (label);
2537 gboolean enable_mnemonics;
2538 gboolean auto_mnemonics;
2540 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2541 "gtk-enable-mnemonics", &enable_mnemonics,
2542 "gtk-auto-mnemonics", &auto_mnemonics,
2545 if (!(enable_mnemonics && priv->mnemonics_visible &&
2547 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2548 (!priv->mnemonic_widget ||
2549 gtk_widget_is_sensitive (priv->mnemonic_widget))))))
2555 if (separate_uline_pattern (new_str, &key, &tmp, &pattern))
2564 if (!pango_parse_markup (new_str,
2566 with_uline ? '_' : 0,
2569 with_uline ? &accel_char : NULL,
2572 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2575 g_error_free (error);
2582 gtk_label_set_text_internal (label, text);
2586 if (priv->markup_attrs)
2587 pango_attr_list_unref (priv->markup_attrs);
2588 priv->markup_attrs = attrs;
2591 if (accel_char != 0)
2592 priv->mnemonic_keyval = gdk_keyval_to_lower (gdk_unicode_to_keyval (accel_char));
2594 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
2598 * gtk_label_set_markup:
2599 * @label: a #GtkLabel
2600 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
2602 * Parses @str which is marked up with the <link
2603 * linkend="PangoMarkupFormat">Pango text markup language</link>, setting the
2604 * label's text and attribute list based on the parse results. If the @str is
2605 * external data, you may need to escape it with g_markup_escape_text() or
2606 * g_markup_printf_escaped()<!-- -->:
2610 * markup = g_markup_printf_escaped ("<span style=\"italic\">%s</span>", str);
2611 * gtk_label_set_markup (GTK_LABEL (label), markup);
2616 gtk_label_set_markup (GtkLabel *label,
2619 g_return_if_fail (GTK_IS_LABEL (label));
2621 g_object_freeze_notify (G_OBJECT (label));
2623 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2624 gtk_label_set_use_markup_internal (label, TRUE);
2625 gtk_label_set_use_underline_internal (label, FALSE);
2627 gtk_label_recalculate (label);
2629 g_object_thaw_notify (G_OBJECT (label));
2633 * gtk_label_set_markup_with_mnemonic:
2634 * @label: a #GtkLabel
2635 * @str: a markup string (see
2636 * <link linkend="PangoMarkupFormat">Pango markup format</link>)
2638 * Parses @str which is marked up with the
2639 * <link linkend="PangoMarkupFormat">Pango text markup language</link>,
2640 * setting the label's text and attribute list based on the parse results.
2641 * If characters in @str are preceded by an underscore, they are underlined
2642 * indicating that they represent a keyboard accelerator called a mnemonic.
2644 * The mnemonic key can be used to activate another widget, chosen
2645 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
2648 gtk_label_set_markup_with_mnemonic (GtkLabel *label,
2651 g_return_if_fail (GTK_IS_LABEL (label));
2653 g_object_freeze_notify (G_OBJECT (label));
2655 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2656 gtk_label_set_use_markup_internal (label, TRUE);
2657 gtk_label_set_use_underline_internal (label, TRUE);
2659 gtk_label_recalculate (label);
2661 g_object_thaw_notify (G_OBJECT (label));
2665 * gtk_label_get_text:
2666 * @label: a #GtkLabel
2668 * Fetches the text from a label widget, as displayed on the
2669 * screen. This does not include any embedded underlines
2670 * indicating mnemonics or Pango markup. (See gtk_label_get_label())
2672 * Return value: the text in the label widget. This is the internal
2673 * string used by the label, and must not be modified.
2676 gtk_label_get_text (GtkLabel *label)
2678 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2680 return label->priv->text;
2683 static PangoAttrList *
2684 gtk_label_pattern_to_attrs (GtkLabel *label,
2685 const gchar *pattern)
2687 GtkLabelPrivate *priv = label->priv;
2689 const char *p = priv->text;
2690 const char *q = pattern;
2691 PangoAttrList *attrs;
2693 attrs = pango_attr_list_new ();
2697 while (*p && *q && *q != '_')
2699 p = g_utf8_next_char (p);
2703 while (*p && *q && *q == '_')
2705 p = g_utf8_next_char (p);
2711 PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_LOW);
2712 attr->start_index = start - priv->text;
2713 attr->end_index = p - priv->text;
2715 pango_attr_list_insert (attrs, attr);
2725 gtk_label_set_pattern_internal (GtkLabel *label,
2726 const gchar *pattern,
2727 gboolean is_mnemonic)
2729 GtkLabelPrivate *priv = label->priv;
2730 PangoAttrList *attrs;
2731 gboolean enable_mnemonics;
2732 gboolean auto_mnemonics;
2734 if (priv->pattern_set)
2739 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2740 "gtk-enable-mnemonics", &enable_mnemonics,
2741 "gtk-auto-mnemonics", &auto_mnemonics,
2744 if (enable_mnemonics && priv->mnemonics_visible && pattern &&
2746 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2747 (!priv->mnemonic_widget ||
2748 gtk_widget_is_sensitive (priv->mnemonic_widget)))))
2749 attrs = gtk_label_pattern_to_attrs (label, pattern);
2754 attrs = gtk_label_pattern_to_attrs (label, pattern);
2756 if (priv->markup_attrs)
2757 pango_attr_list_unref (priv->markup_attrs);
2758 priv->markup_attrs = attrs;
2762 * gtk_label_set_pattern:
2763 * @label: The #GtkLabel you want to set the pattern to.
2764 * @pattern: The pattern as described above.
2766 * The pattern of underlines you want under the existing text within the
2767 * #GtkLabel widget. For example if the current text of the label says
2768 * "FooBarBaz" passing a pattern of "___ ___" will underline
2769 * "Foo" and "Baz" but not "Bar".
2772 gtk_label_set_pattern (GtkLabel *label,
2773 const gchar *pattern)
2775 GtkLabelPrivate *priv;
2777 g_return_if_fail (GTK_IS_LABEL (label));
2781 priv->pattern_set = FALSE;
2785 gtk_label_set_pattern_internal (label, pattern, FALSE);
2786 priv->pattern_set = TRUE;
2789 gtk_label_recalculate (label);
2791 gtk_label_clear_layout (label);
2792 gtk_widget_queue_resize (GTK_WIDGET (label));
2797 * gtk_label_set_justify:
2798 * @label: a #GtkLabel
2799 * @jtype: a #GtkJustification
2801 * Sets the alignment of the lines in the text of the label relative to
2802 * each other. %GTK_JUSTIFY_LEFT is the default value when the
2803 * widget is first created with gtk_label_new(). If you instead want
2804 * to set the alignment of the label as a whole, use
2805 * gtk_misc_set_alignment() instead. gtk_label_set_justify() has no
2806 * effect on labels containing only a single line.
2809 gtk_label_set_justify (GtkLabel *label,
2810 GtkJustification jtype)
2812 GtkLabelPrivate *priv;
2814 g_return_if_fail (GTK_IS_LABEL (label));
2815 g_return_if_fail (jtype >= GTK_JUSTIFY_LEFT && jtype <= GTK_JUSTIFY_FILL);
2819 if ((GtkJustification) priv->jtype != jtype)
2821 priv->jtype = jtype;
2823 /* No real need to be this drastic, but easier than duplicating the code */
2824 gtk_label_clear_layout (label);
2826 g_object_notify (G_OBJECT (label), "justify");
2827 gtk_widget_queue_resize (GTK_WIDGET (label));
2832 * gtk_label_get_justify:
2833 * @label: a #GtkLabel
2835 * Returns the justification of the label. See gtk_label_set_justify().
2837 * Return value: #GtkJustification
2840 gtk_label_get_justify (GtkLabel *label)
2842 g_return_val_if_fail (GTK_IS_LABEL (label), 0);
2844 return label->priv->jtype;
2848 * gtk_label_set_ellipsize:
2849 * @label: a #GtkLabel
2850 * @mode: a #PangoEllipsizeMode
2852 * Sets the mode used to ellipsize (add an ellipsis: "...") to the text
2853 * if there is not enough space to render the entire string.
2858 gtk_label_set_ellipsize (GtkLabel *label,
2859 PangoEllipsizeMode mode)
2861 GtkLabelPrivate *priv;
2863 g_return_if_fail (GTK_IS_LABEL (label));
2864 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE && mode <= PANGO_ELLIPSIZE_END);
2868 if ((PangoEllipsizeMode) priv->ellipsize != mode)
2870 priv->ellipsize = mode;
2872 /* No real need to be this drastic, but easier than duplicating the code */
2873 gtk_label_clear_layout (label);
2875 g_object_notify (G_OBJECT (label), "ellipsize");
2876 gtk_widget_queue_resize (GTK_WIDGET (label));
2881 * gtk_label_get_ellipsize:
2882 * @label: a #GtkLabel
2884 * Returns the ellipsizing position of the label. See gtk_label_set_ellipsize().
2886 * Return value: #PangoEllipsizeMode
2891 gtk_label_get_ellipsize (GtkLabel *label)
2893 g_return_val_if_fail (GTK_IS_LABEL (label), PANGO_ELLIPSIZE_NONE);
2895 return label->priv->ellipsize;
2899 * gtk_label_set_width_chars:
2900 * @label: a #GtkLabel
2901 * @n_chars: the new desired width, in characters.
2903 * Sets the desired width in characters of @label to @n_chars.
2908 gtk_label_set_width_chars (GtkLabel *label,
2911 GtkLabelPrivate *priv;
2913 g_return_if_fail (GTK_IS_LABEL (label));
2917 if (priv->width_chars != n_chars)
2919 priv->width_chars = n_chars;
2920 g_object_notify (G_OBJECT (label), "width-chars");
2921 gtk_widget_queue_resize (GTK_WIDGET (label));
2926 * gtk_label_get_width_chars:
2927 * @label: a #GtkLabel
2929 * Retrieves the desired width of @label, in characters. See
2930 * gtk_label_set_width_chars().
2932 * Return value: the width of the label in characters.
2937 gtk_label_get_width_chars (GtkLabel *label)
2939 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2941 return label->priv->width_chars;
2945 * gtk_label_set_max_width_chars:
2946 * @label: a #GtkLabel
2947 * @n_chars: the new desired maximum width, in characters.
2949 * Sets the desired maximum width in characters of @label to @n_chars.
2954 gtk_label_set_max_width_chars (GtkLabel *label,
2957 GtkLabelPrivate *priv;
2959 g_return_if_fail (GTK_IS_LABEL (label));
2963 if (priv->max_width_chars != n_chars)
2965 priv->max_width_chars = n_chars;
2967 g_object_notify (G_OBJECT (label), "max-width-chars");
2968 gtk_widget_queue_resize (GTK_WIDGET (label));
2973 * gtk_label_get_max_width_chars:
2974 * @label: a #GtkLabel
2976 * Retrieves the desired maximum width of @label, in characters. See
2977 * gtk_label_set_width_chars().
2979 * Return value: the maximum width of the label in characters.
2984 gtk_label_get_max_width_chars (GtkLabel *label)
2986 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2988 return label->priv->max_width_chars;
2992 * gtk_label_set_line_wrap:
2993 * @label: a #GtkLabel
2994 * @wrap: the setting
2996 * Toggles line wrapping within the #GtkLabel widget. %TRUE makes it break
2997 * lines if text exceeds the widget's size. %FALSE lets the text get cut off
2998 * by the edge of the widget if it exceeds the widget size.
3000 * Note that setting line wrapping to %TRUE does not make the label
3001 * wrap at its parent container's width, because GTK+ widgets
3002 * conceptually can't make their requisition depend on the parent
3003 * container's size. For a label that wraps at a specific position,
3004 * set the label's width using gtk_widget_set_size_request().
3007 gtk_label_set_line_wrap (GtkLabel *label,
3010 GtkLabelPrivate *priv;
3012 g_return_if_fail (GTK_IS_LABEL (label));
3016 wrap = wrap != FALSE;
3018 if (priv->wrap != wrap)
3022 gtk_label_clear_layout (label);
3023 gtk_widget_queue_resize (GTK_WIDGET (label));
3024 g_object_notify (G_OBJECT (label), "wrap");
3029 * gtk_label_get_line_wrap:
3030 * @label: a #GtkLabel
3032 * Returns whether lines in the label are automatically wrapped.
3033 * See gtk_label_set_line_wrap().
3035 * Return value: %TRUE if the lines of the label are automatically wrapped.
3038 gtk_label_get_line_wrap (GtkLabel *label)
3040 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
3042 return label->priv->wrap;
3046 * gtk_label_set_line_wrap_mode:
3047 * @label: a #GtkLabel
3048 * @wrap_mode: the line wrapping mode
3050 * If line wrapping is on (see gtk_label_set_line_wrap()) this controls how
3051 * the line wrapping is done. The default is %PANGO_WRAP_WORD which means
3052 * wrap on word boundaries.
3057 gtk_label_set_line_wrap_mode (GtkLabel *label,
3058 PangoWrapMode wrap_mode)
3060 GtkLabelPrivate *priv;
3062 g_return_if_fail (GTK_IS_LABEL (label));
3066 if (priv->wrap_mode != wrap_mode)
3068 priv->wrap_mode = wrap_mode;
3069 g_object_notify (G_OBJECT (label), "wrap-mode");
3071 gtk_widget_queue_resize (GTK_WIDGET (label));
3076 * gtk_label_get_line_wrap_mode:
3077 * @label: a #GtkLabel
3079 * Returns line wrap mode used by the label. See gtk_label_set_line_wrap_mode().
3081 * Return value: %TRUE if the lines of the label are automatically wrapped.
3086 gtk_label_get_line_wrap_mode (GtkLabel *label)
3088 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
3090 return label->priv->wrap_mode;
3094 gtk_label_destroy (GtkWidget *widget)
3096 GtkLabel *label = GTK_LABEL (widget);
3098 gtk_label_set_mnemonic_widget (label, NULL);
3100 GTK_WIDGET_CLASS (gtk_label_parent_class)->destroy (widget);
3104 gtk_label_finalize (GObject *object)
3106 GtkLabel *label = GTK_LABEL (object);
3107 GtkLabelPrivate *priv = label->priv;
3109 g_free (priv->label);
3110 g_free (priv->text);
3113 g_object_unref (priv->layout);
3116 pango_attr_list_unref (priv->attrs);
3118 if (priv->markup_attrs)
3119 pango_attr_list_unref (priv->markup_attrs);
3121 gtk_label_clear_links (label);
3122 g_free (priv->select_info);
3124 G_OBJECT_CLASS (gtk_label_parent_class)->finalize (object);
3128 gtk_label_clear_layout (GtkLabel *label)
3130 GtkLabelPrivate *priv = label->priv;
3134 g_object_unref (priv->layout);
3135 priv->layout = NULL;
3139 static PangoFontMetrics *
3140 get_font_metrics (PangoContext *context, GtkWidget *widget)
3142 GtkStyleContext *style_context;
3143 const PangoFontDescription *font;
3144 PangoFontMetrics *retval;
3146 style_context = gtk_widget_get_style_context (widget);
3147 font = gtk_style_context_get_font (style_context, GTK_STATE_FLAG_NORMAL);
3149 retval = pango_context_get_metrics (context,
3151 pango_context_get_language (context));
3157 * gtk_label_get_measuring_layout:
3159 * @existing_layout: %NULL or an existing layout already in use.
3160 * @width: the width to measure with in pango units, or -1 for infinite
3161 * @height: the height to measure with in pango units, or -1 for infinite
3163 * Gets a layout that can be used for measuring sizes. The returned
3164 * layout will be identical to the label's layout except for the
3165 * layout's width, which will be set to @width. Do not modify the returned
3168 * Returns: a new reference to a pango layout
3170 static PangoLayout *
3171 gtk_label_get_measuring_layout (GtkLabel * label,
3172 PangoLayout *existing_layout,
3176 GtkLabelPrivate *priv = label->priv;
3177 PangoRectangle rect;
3180 if (existing_layout != NULL)
3182 if (existing_layout != priv->layout)
3184 pango_layout_set_width (existing_layout, width);
3185 pango_layout_set_height (existing_layout, height);
3186 return existing_layout;
3189 g_object_unref (existing_layout);
3192 gtk_label_ensure_layout (label);
3194 if (pango_layout_get_width (priv->layout) == width &&
3195 pango_layout_get_height (priv->layout) == height)
3197 g_object_ref (priv->layout);
3198 return priv->layout;
3201 /* We can use the label's own layout if we're not allocated a size yet,
3202 * because we don't need it to be properly setup at that point.
3203 * This way we can make use of caching upon the label's creation.
3205 if (gtk_widget_get_allocated_width (GTK_WIDGET (label)) <= 1)
3207 g_object_ref (priv->layout);
3208 pango_layout_set_width (priv->layout, width);
3209 pango_layout_set_height (priv->layout, height);
3210 return priv->layout;
3213 /* oftentimes we want to measure a width that is far wider than the current width,
3214 * even though the layout would not change if we made it wider. In that case, we
3215 * can just return the current layout, because for measuring purposes, it will be
3218 pango_layout_get_extents (priv->layout, NULL, &rect);
3219 if ((width == -1 || rect.width <= width) &&
3220 (height == -1 || rect.height <= height) &&
3221 !pango_layout_is_wrapped (priv->layout) &&
3222 !pango_layout_is_ellipsized (priv->layout))
3224 g_object_ref (priv->layout);
3225 return priv->layout;
3228 copy = pango_layout_copy (priv->layout);
3229 pango_layout_set_width (copy, width);
3230 pango_layout_set_height (copy, height);
3235 gtk_label_update_layout_width (GtkLabel *label)
3237 GtkLabelPrivate *priv = label->priv;
3238 GtkWidget *widget = GTK_WIDGET (label);
3240 g_assert (priv->layout);
3242 if (priv->ellipsize || priv->wrap)
3245 PangoRectangle logical;
3248 _gtk_misc_get_padding_and_border (GTK_MISC (label), &border);
3250 width = gtk_widget_get_allocated_width (GTK_WIDGET (label)) - border.left - border.right;
3251 height = gtk_widget_get_allocated_height (GTK_WIDGET (label)) - border.top - border.bottom;
3253 if (priv->have_transform)
3255 PangoContext *context = gtk_widget_get_pango_context (widget);
3256 const PangoMatrix *matrix = pango_context_get_matrix (context);
3257 const gdouble dx = matrix->xx; /* cos (M_PI * angle / 180) */
3258 const gdouble dy = matrix->xy; /* sin (M_PI * angle / 180) */
3260 pango_layout_set_width (priv->layout, -1);
3261 pango_layout_set_height (priv->layout, -1);
3262 pango_layout_get_pixel_extents (priv->layout, NULL, &logical);
3264 if (fabs (dy) < 0.01)
3266 if (logical.width > width)
3267 pango_layout_set_width (priv->layout, width * PANGO_SCALE);
3269 else if (fabs (dx) < 0.01)
3271 if (logical.width > height)
3272 pango_layout_set_width (priv->layout, height * PANGO_SCALE);
3276 gdouble x0, y0, x1, y1, length;
3281 y0 = dx ? x0 * dy / dx : G_MAXDOUBLE;
3282 vertical = fabs (y0) > height / 2;
3287 x0 = dy ? y0 * dx / dy : G_MAXDOUBLE;
3290 length = 2 * sqrt (x0 * x0 + y0 * y0);
3291 pango_layout_set_width (priv->layout, rint (length * PANGO_SCALE));
3292 pango_layout_get_pixel_size (priv->layout, NULL, &cy);
3299 y0 = height/2 + y1 - y0;
3304 x0 = width/2 + x1 - x0;
3308 length = length - sqrt (x0 * x0 + y0 * y0) * 2;
3309 pango_layout_set_width (priv->layout, rint (length * PANGO_SCALE));
3314 pango_layout_set_width (priv->layout, width * PANGO_SCALE);
3315 pango_layout_set_height (priv->layout, priv->ellipsize ? height * PANGO_SCALE : -1);
3320 pango_layout_set_width (priv->layout, -1);
3321 pango_layout_set_height (priv->layout, -1);
3326 gtk_label_ensure_layout (GtkLabel *label)
3328 GtkLabelPrivate *priv = label->priv;
3332 widget = GTK_WIDGET (label);
3334 rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
3338 PangoAlignment align = PANGO_ALIGN_LEFT; /* Quiet gcc */
3339 PangoAttrList *attrs;
3340 gdouble angle = gtk_label_get_angle (label);
3342 if (angle != 0.0 && !priv->select_info)
3344 PangoMatrix matrix = PANGO_MATRIX_INIT;
3346 /* We rotate the standard singleton PangoContext for the widget,
3347 * depending on the fact that it's meant pretty much exclusively
3350 pango_matrix_rotate (&matrix, angle);
3352 pango_context_set_matrix (gtk_widget_get_pango_context (widget), &matrix);
3354 priv->have_transform = TRUE;
3358 if (priv->have_transform)
3359 pango_context_set_matrix (gtk_widget_get_pango_context (widget), NULL);
3361 priv->have_transform = FALSE;
3364 priv->layout = gtk_widget_create_pango_layout (widget, priv->text);
3366 if (priv->select_info && priv->select_info->links)
3368 GdkColor link_color, visited_color;
3369 PangoAttribute *attribute;
3372 gtk_label_get_link_colors (widget, &link_color, &visited_color);
3373 attrs = pango_attr_list_new ();
3375 for (list = priv->select_info->links; list; list = list->next)
3377 GtkLabelLink *link = list->data;
3379 attribute = pango_attr_underline_new (TRUE);
3380 attribute->start_index = link->start;
3381 attribute->end_index = link->end;
3382 pango_attr_list_insert (attrs, attribute);
3385 attribute = pango_attr_foreground_new (visited_color.red,
3386 visited_color.green,
3387 visited_color.blue);
3389 attribute = pango_attr_foreground_new (link_color.red,
3392 attribute->start_index = link->start;
3393 attribute->end_index = link->end;
3394 pango_attr_list_insert (attrs, attribute);
3397 else if (priv->markup_attrs && priv->markup_attrs)
3398 attrs = pango_attr_list_new ();
3402 if (priv->markup_attrs)
3405 my_pango_attr_list_merge (attrs, priv->markup_attrs);
3407 attrs = pango_attr_list_ref (priv->markup_attrs);
3413 my_pango_attr_list_merge (attrs, priv->attrs);
3415 attrs = pango_attr_list_ref (priv->attrs);
3420 pango_layout_set_attributes (priv->layout, attrs);
3421 pango_attr_list_unref (attrs);
3424 switch (priv->jtype)
3426 case GTK_JUSTIFY_LEFT:
3427 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3429 case GTK_JUSTIFY_RIGHT:
3430 align = rtl ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
3432 case GTK_JUSTIFY_CENTER:
3433 align = PANGO_ALIGN_CENTER;
3435 case GTK_JUSTIFY_FILL:
3436 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3437 pango_layout_set_justify (priv->layout, TRUE);
3440 g_assert_not_reached();
3443 pango_layout_set_alignment (priv->layout, align);
3444 pango_layout_set_ellipsize (priv->layout, priv->ellipsize);
3445 pango_layout_set_wrap (priv->layout, priv->wrap_mode);
3446 pango_layout_set_single_paragraph_mode (priv->layout, priv->single_line_mode);
3448 gtk_label_update_layout_width (label);
3453 get_single_line_height (GtkWidget *widget,
3454 PangoLayout *layout)
3456 PangoContext *context;
3457 PangoFontMetrics *metrics;
3458 gint ascent, descent;
3460 context = pango_layout_get_context (layout);
3461 metrics = get_font_metrics (context, widget);
3462 ascent = pango_font_metrics_get_ascent (metrics);
3463 descent = pango_font_metrics_get_descent (metrics);
3464 pango_font_metrics_unref (metrics);
3466 return ascent + descent;
3469 static GtkSizeRequestMode
3470 gtk_label_get_request_mode (GtkWidget *widget)
3472 GtkLabel *label = GTK_LABEL (widget);
3473 gdouble angle = gtk_label_get_angle (label);
3475 if (label->priv->wrap)
3476 return (angle == 90 || angle == 270) ?
3477 GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT :
3478 GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
3480 return GTK_SIZE_REQUEST_CONSTANT_SIZE;
3484 get_size_for_allocation (GtkLabel *label,
3485 GtkOrientation orientation,
3490 GtkLabelPrivate *priv = label->priv;
3491 PangoLayout *layout;
3494 layout = gtk_label_get_measuring_layout (label, NULL, allocation * PANGO_SCALE, -1);
3496 pango_layout_get_pixel_size (layout, NULL, &text_height);
3499 *minimum_size = text_height;
3503 if (priv->ellipsize && priv->wrap)
3505 layout = gtk_label_get_measuring_layout (label, layout, allocation * PANGO_SCALE, G_MAXINT);
3506 pango_layout_get_pixel_size (layout, NULL, &text_height);
3509 *natural_size = text_height;
3512 g_object_unref (layout);
3516 get_char_pixels (GtkWidget *label,
3517 PangoLayout *layout)
3519 PangoContext *context;
3520 PangoFontMetrics *metrics;
3521 gint char_width, digit_width;
3523 context = pango_layout_get_context (layout);
3524 metrics = get_font_metrics (context, GTK_WIDGET (label));
3525 char_width = pango_font_metrics_get_approximate_char_width (metrics);
3526 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
3527 pango_font_metrics_unref (metrics);
3529 return MAX (char_width, digit_width);;
3533 gtk_label_get_preferred_layout_size (GtkLabel *label,
3534 PangoRectangle *required,
3535 PangoRectangle *natural)
3537 GtkLabelPrivate *priv = label->priv;
3538 PangoLayout *layout;
3540 /* "width-chars" Hard-coded minimum width:
3541 * - minimum size should be MAX (width-chars, strlen ("..."));
3542 * - natural size should be MAX (width-chars, strlen (priv->text));
3544 * "max-width-chars" User specified maximum size requisition
3545 * - minimum size should be MAX (width-chars, 0)
3546 * - natural size should be MIN (max-width-chars, strlen (priv->text))
3548 * For ellipsizing labels; if max-width-chars is specified: either it is used as
3549 * a minimum size or the label text as a minimum size (natural size still overflows).
3551 * For wrapping labels; A reasonable minimum size is useful to naturally layout
3552 * interfaces automatically. In this case if no "width-chars" is specified, the minimum
3553 * width will default to the wrap guess that gtk_label_ensure_layout() does.
3556 /* Start off with the pixel extents of an as-wide-as-possible layout */
3557 layout = gtk_label_get_measuring_layout (label, NULL, -1, -1);
3559 pango_layout_get_extents (layout, NULL, natural);
3560 natural->x = natural->y = 0;
3563 natural->height = get_single_line_height (GTK_WIDGET (label), layout);
3565 if (priv->ellipsize || priv->wrap)
3567 /* a layout with width 0 will be as small as humanly possible */
3568 layout = gtk_label_get_measuring_layout (label, layout, 0, -1);
3570 pango_layout_get_extents (layout, NULL, required);
3572 /* can happen when Pango decides to ellipsize text */
3573 if (required->width > natural->width)
3574 required->width = natural->width;
3576 required->x = required->y = 0;
3577 required->height = natural->height;
3581 *required = *natural;
3584 if (priv->width_chars > -1 || priv->max_width_chars > -1)
3588 char_pixels = get_char_pixels (GTK_WIDGET (label), layout);
3590 if (priv->width_chars > -1)
3591 required->width = MAX (required->width, char_pixels * priv->width_chars);
3593 if (priv->max_width_chars > -1)
3594 natural->width = MIN (natural->width, priv->max_width_chars * char_pixels);
3595 natural->width = MAX (natural->width, required->width);
3598 g_object_unref (layout);
3602 gtk_label_get_preferred_size (GtkWidget *widget,
3603 GtkOrientation orientation,
3607 GtkLabel *label = GTK_LABEL (widget);
3608 GtkLabelPrivate *priv = label->priv;
3609 PangoRectangle required_rect;
3610 PangoRectangle natural_rect;
3613 gtk_label_get_preferred_layout_size (label, &required_rect, &natural_rect);
3615 /* Now that we have minimum and natural sizes in pango extents, apply a possible transform */
3616 if (priv->have_transform)
3619 PangoContext *context;
3620 const PangoMatrix *matrix;
3622 copy = pango_layout_copy (priv->layout);
3623 context = pango_layout_get_context (copy);
3624 matrix = pango_context_get_matrix (context);
3626 pango_layout_set_width (copy, -1);
3627 pango_layout_set_ellipsize (copy, PANGO_ELLIPSIZE_NONE);
3629 pango_layout_get_extents (copy, NULL, &natural_rect);
3630 g_object_unref (copy);
3632 pango_matrix_transform_rectangle (matrix, &required_rect);
3633 pango_matrix_transform_rectangle (matrix, &natural_rect);
3635 /* Bump the natural size in case of ellipsize to ensure pango has
3636 * enough space in the angles (note, we could alternatively set the
3637 * layout to not ellipsize when we know we have been allocated our
3638 * full natural size, or it may be that pango needs a fix here).
3640 if (priv->ellipsize && priv->angle != 0 && priv->angle != 90 &&
3641 priv->angle != 180 && priv->angle != 270 && priv->angle != 360)
3643 /* For some reason we only need this at about 110 degrees, and only
3644 * when gaining in height
3646 natural_rect.height += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3647 natural_rect.width += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3651 required_rect.width = PANGO_PIXELS_CEIL (required_rect.width);
3652 required_rect.height = PANGO_PIXELS_CEIL (required_rect.height);
3654 natural_rect.width = PANGO_PIXELS_CEIL (natural_rect.width);
3655 natural_rect.height = PANGO_PIXELS_CEIL (natural_rect.height);
3657 _gtk_misc_get_padding_and_border (GTK_MISC (label), &border);
3659 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3661 /* Note, we cant use get_size_for_allocation() when rotating
3662 * ellipsized labels.
3664 if (!(priv->ellipsize && priv->have_transform) &&
3665 (priv->angle == 90 || priv->angle == 270))
3667 /* Doing a h4w request on a rotated label here, return the
3668 * required width for the minimum height.
3670 get_size_for_allocation (label,
3671 GTK_ORIENTATION_VERTICAL,
3672 required_rect.height,
3673 minimum_size, natural_size);
3678 /* Normal desired width */
3679 *minimum_size = required_rect.width;
3680 *natural_size = natural_rect.width;
3683 *minimum_size += border.left + border.right;
3684 *natural_size += border.left + border.right;
3686 else /* GTK_ORIENTATION_VERTICAL */
3688 /* Note, we cant use get_size_for_allocation() when rotating
3689 * ellipsized labels.
3691 if (!(priv->ellipsize && priv->have_transform) &&
3692 (priv->angle == 0 || priv->angle == 180 || priv->angle == 360))
3694 /* Doing a w4h request on a label here, return the required
3695 * height for the minimum width.
3697 get_size_for_allocation (label,
3698 GTK_ORIENTATION_HORIZONTAL,
3699 required_rect.width,
3700 minimum_size, natural_size);
3704 /* A vertically rotated label does w4h, so return the base
3705 * desired height (text length)
3707 *minimum_size = required_rect.height;
3708 *natural_size = natural_rect.height;
3711 *minimum_size += border.top + border.bottom;
3712 *natural_size += border.top + border.bottom;
3717 gtk_label_get_preferred_width (GtkWidget *widget,
3721 gtk_label_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
3725 gtk_label_get_preferred_height (GtkWidget *widget,
3729 gtk_label_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
3733 gtk_label_get_preferred_width_for_height (GtkWidget *widget,
3735 gint *minimum_width,
3736 gint *natural_width)
3738 GtkLabel *label = GTK_LABEL (widget);
3739 GtkLabelPrivate *priv = label->priv;
3741 if (priv->wrap && (priv->angle == 90 || priv->angle == 270))
3745 _gtk_misc_get_padding_and_border (GTK_MISC (label), &border);
3748 gtk_label_clear_layout (label);
3750 get_size_for_allocation (label, GTK_ORIENTATION_VERTICAL,
3751 MAX (1, height - border.top - border.bottom),
3752 minimum_width, natural_width);
3755 *minimum_width += border.right + border.left;
3758 *natural_width += border.right + border.left;
3761 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_width, natural_width);
3765 gtk_label_get_preferred_height_for_width (GtkWidget *widget,
3767 gint *minimum_height,
3768 gint *natural_height)
3770 GtkLabel *label = GTK_LABEL (widget);
3771 GtkLabelPrivate *priv = label->priv;
3773 if (priv->wrap && (priv->angle == 0 || priv->angle == 180 || priv->angle == 360))
3777 _gtk_misc_get_padding_and_border (GTK_MISC (label), &border);
3780 gtk_label_clear_layout (label);
3782 get_size_for_allocation (label, GTK_ORIENTATION_HORIZONTAL,
3783 MAX (1, width - border.left - border.right),
3784 minimum_height, natural_height);
3787 *minimum_height += border.top + border.bottom;
3790 *natural_height += border.top + border.bottom;
3793 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, minimum_height, natural_height);
3797 gtk_label_size_allocate (GtkWidget *widget,
3798 GtkAllocation *allocation)
3800 GtkLabel *label = GTK_LABEL (widget);
3801 GtkLabelPrivate *priv = label->priv;
3803 GTK_WIDGET_CLASS (gtk_label_parent_class)->size_allocate (widget, allocation);
3806 gtk_label_update_layout_width (label);
3808 if (priv->select_info && priv->select_info->window)
3810 gdk_window_move_resize (priv->select_info->window,
3814 allocation->height);
3819 gtk_label_update_cursor (GtkLabel *label)
3821 GtkLabelPrivate *priv = label->priv;
3824 if (!priv->select_info)
3827 widget = GTK_WIDGET (label);
3829 if (gtk_widget_get_realized (widget))
3831 GdkDisplay *display;
3834 if (gtk_widget_is_sensitive (widget))
3836 display = gtk_widget_get_display (widget);
3838 if (priv->select_info->active_link)
3839 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
3840 else if (priv->select_info->selectable)
3841 cursor = gdk_cursor_new_for_display (display, GDK_XTERM);
3848 gdk_window_set_cursor (priv->select_info->window, cursor);
3851 g_object_unref (cursor);
3856 gtk_label_state_flags_changed (GtkWidget *widget,
3857 GtkStateFlags prev_state)
3859 GtkLabel *label = GTK_LABEL (widget);
3860 GtkLabelPrivate *priv = label->priv;
3862 if (priv->select_info)
3864 if (!gtk_widget_is_sensitive (widget))
3865 gtk_label_select_region (label, 0, 0);
3867 gtk_label_update_cursor (label);
3870 /* We have to clear the layout, fonts etc. may have changed */
3871 gtk_label_clear_layout (label);
3873 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->state_flags_changed)
3874 GTK_WIDGET_CLASS (gtk_label_parent_class)->state_flags_changed (widget, prev_state);
3878 gtk_label_style_updated (GtkWidget *widget)
3880 GtkLabel *label = GTK_LABEL (widget);
3882 GTK_WIDGET_CLASS (gtk_label_parent_class)->style_updated (widget);
3884 /* We have to clear the layout, fonts etc. may have changed */
3885 gtk_label_clear_layout (label);
3889 gtk_label_direction_changed (GtkWidget *widget,
3890 GtkTextDirection previous_dir)
3892 GtkLabel *label = GTK_LABEL (widget);
3893 GtkLabelPrivate *priv = label->priv;
3896 pango_layout_context_changed (priv->layout);
3898 GTK_WIDGET_CLASS (gtk_label_parent_class)->direction_changed (widget, previous_dir);
3902 get_layout_location (GtkLabel *label,
3906 GtkAllocation allocation;
3909 GtkLabelPrivate *priv;
3911 gint req_width, x, y;
3913 gfloat xalign, yalign;
3914 PangoRectangle logical;
3916 misc = GTK_MISC (label);
3917 widget = GTK_WIDGET (label);
3920 gtk_misc_get_alignment (misc, &xalign, &yalign);
3921 _gtk_misc_get_padding_and_border (GTK_MISC (label), &border);
3923 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
3924 xalign = 1.0 - xalign;
3926 pango_layout_get_extents (priv->layout, NULL, &logical);
3928 if (priv->have_transform)
3930 PangoContext *context = gtk_widget_get_pango_context (widget);
3931 const PangoMatrix *matrix = pango_context_get_matrix (context);
3932 pango_matrix_transform_rectangle (matrix, &logical);
3935 pango_extents_to_pixels (&logical, NULL);
3937 req_width = logical.width;
3938 req_height = logical.height;
3940 req_width += border.left + border.right;
3941 req_height += border.top + border.bottom;
3943 gtk_widget_get_allocation (widget, &allocation);
3945 x = floor (allocation.x + border.left + xalign * (allocation.width - req_width) - logical.x);
3947 /* bgo#315462 - For single-line labels, *do* align the requisition with
3948 * respect to the allocation, even if we are under-allocated. For multi-line
3949 * labels, always show the top of the text when they are under-allocated. The
3950 * rationale is this:
3952 * - Single-line labels appear in GtkButtons, and it is very easy to get them
3953 * to be smaller than their requisition. The button may clip the label, but
3954 * the label will still be able to show most of itself and the focus
3955 * rectangle. Also, it is fairly easy to read a single line of clipped text.
3957 * - Multi-line labels should not be clipped to showing "something in the
3958 * middle". You want to read the first line, at least, to get some context.
3960 if (pango_layout_get_line_count (priv->layout) == 1)
3961 y = floor (allocation.y + border.top + (allocation.height - req_height) * yalign) - logical.y;
3963 y = floor (allocation.y + border.top + MAX ((allocation.height - req_height) * yalign, 0)) - logical.y;
3972 static PangoDirection
3973 get_cursor_direction (GtkLabel *label)
3975 GtkLabelPrivate *priv = label->priv;
3978 g_assert (priv->select_info);
3980 gtk_label_ensure_layout (label);
3982 for (l = pango_layout_get_lines_readonly (priv->layout); l; l = l->next)
3984 PangoLayoutLine *line = l->data;
3986 /* If priv->select_info->selection_end is at the very end of
3987 * the line, we don't know if the cursor is on this line or
3988 * the next without looking ahead at the next line. (End
3989 * of paragraph is different from line break.) But it's
3990 * definitely in this paragraph, which is good enough
3991 * to figure out the resolved direction.
3993 if (line->start_index + line->length >= priv->select_info->selection_end)
3994 return line->resolved_dir;
3997 return PANGO_DIRECTION_LTR;
4000 static GtkLabelLink *
4001 gtk_label_get_focus_link (GtkLabel *label)
4003 GtkLabelPrivate *priv = label->priv;
4004 GtkLabelSelectionInfo *info = priv->select_info;
4010 if (info->selection_anchor != info->selection_end)
4013 for (l = info->links; l; l = l->next)
4015 GtkLabelLink *link = l->data;
4016 if (link->start <= info->selection_anchor &&
4017 info->selection_anchor <= link->end)
4025 gtk_label_draw (GtkWidget *widget,
4028 GtkLabel *label = GTK_LABEL (widget);
4029 GtkLabelPrivate *priv = label->priv;
4030 GtkLabelSelectionInfo *info = priv->select_info;
4031 GtkAllocation allocation;
4032 GtkStyleContext *context;
4033 GtkStateFlags state;
4036 gtk_label_ensure_layout (label);
4038 context = gtk_widget_get_style_context (widget);
4039 gtk_widget_get_allocation (widget, &allocation);
4041 gtk_render_background (context, cr,
4043 allocation.width, allocation.height);
4044 gtk_render_frame (context, cr,
4046 allocation.width, allocation.height);
4048 if (priv->text && (*priv->text != '\0'))
4050 get_layout_location (label, &x, &y);
4052 cairo_translate (cr, -allocation.x, -allocation.y);
4054 gtk_render_layout (context, cr,
4058 state = gtk_widget_get_state_flags (widget);
4061 (info->selection_anchor != info->selection_end))
4064 cairo_region_t *clip;
4065 GdkRGBA bg_color, fg_color;
4067 range[0] = info->selection_anchor;
4068 range[1] = info->selection_end;
4070 if (range[0] > range[1])
4072 gint tmp = range[0];
4073 range[0] = range[1];
4077 clip = gdk_pango_layout_get_clip_region (priv->layout,
4082 /* FIXME should use gtk_paint, but it can't use a clip region */
4085 gdk_cairo_region (cr, clip);
4088 state |= GTK_STATE_FLAG_SELECTED;
4090 gtk_style_context_get_color (context, state, &fg_color);
4091 gtk_style_context_get_background_color (context, state, &bg_color);
4093 gdk_cairo_set_source_rgba (cr, &bg_color);
4096 gdk_cairo_set_source_rgba (cr, &fg_color);
4097 cairo_move_to (cr, x, y);
4098 _gtk_pango_fill_layout (cr, priv->layout);
4101 cairo_region_destroy (clip);
4105 GtkLabelLink *focus_link;
4106 GtkLabelLink *active_link;
4108 cairo_region_t *clip;
4110 GdkColor *text_color;
4111 GdkColor link_color;
4112 GdkColor visited_link_color;
4114 if (info->selectable &&
4115 gtk_widget_has_focus (widget) &&
4116 gtk_widget_is_drawable (widget))
4118 PangoDirection cursor_direction;
4120 cursor_direction = get_cursor_direction (label);
4121 gtk_render_insertion_cursor (context, cr,
4123 priv->layout, priv->select_info->selection_end,
4127 focus_link = gtk_label_get_focus_link (label);
4128 active_link = info->active_link;
4134 range[0] = active_link->start;
4135 range[1] = active_link->end;
4139 clip = gdk_pango_layout_get_clip_region (priv->layout,
4143 gdk_cairo_region (cr, clip);
4145 cairo_region_destroy (clip);
4147 gtk_label_get_link_colors (widget, &link_color, &visited_link_color);
4148 if (active_link->visited)
4149 text_color = &visited_link_color;
4151 text_color = &link_color;
4153 if (info->link_clicked)
4154 state |= GTK_STATE_FLAG_ACTIVE;
4156 state |= GTK_STATE_FLAG_PRELIGHT;
4158 gtk_style_context_get_background_color (context, state, &bg_color);
4160 gdk_cairo_set_source_rgba (cr, &bg_color);
4163 cairo_set_source_rgb (cr, text_color->red / 65535.,
4164 text_color->green / 65535.,
4165 text_color->blue / 65535.);
4166 cairo_move_to (cr, x, y);
4167 _gtk_pango_fill_layout (cr, priv->layout);
4172 if (focus_link && gtk_widget_has_visible_focus (widget))
4174 range[0] = focus_link->start;
4175 range[1] = focus_link->end;
4177 clip = gdk_pango_layout_get_clip_region (priv->layout,
4181 cairo_region_get_extents (clip, &rect);
4183 gtk_render_focus (context, cr,
4185 rect.width, rect.height);
4187 cairo_region_destroy (clip);
4196 separate_uline_pattern (const gchar *str,
4201 gboolean underscore;
4204 gchar *pattern_dest;
4206 *accel_key = GDK_KEY_VoidSymbol;
4207 *new_str = g_new (gchar, strlen (str) + 1);
4208 *pattern = g_new (gchar, g_utf8_strlen (str, -1) + 1);
4214 pattern_dest = *pattern;
4219 const gchar *next_src;
4221 c = g_utf8_get_char (src);
4222 if (c == (gunichar)-1)
4224 g_warning ("Invalid input string");
4230 next_src = g_utf8_next_char (src);
4235 *pattern_dest++ = ' ';
4238 *pattern_dest++ = '_';
4239 if (*accel_key == GDK_KEY_VoidSymbol)
4240 *accel_key = gdk_keyval_to_lower (gdk_unicode_to_keyval (c));
4243 while (src < next_src)
4257 while (src < next_src)
4260 *pattern_dest++ = ' ';
4272 gtk_label_set_uline_text_internal (GtkLabel *label,
4275 GtkLabelPrivate *priv = label->priv;
4276 guint accel_key = GDK_KEY_VoidSymbol;
4280 g_return_if_fail (GTK_IS_LABEL (label));
4281 g_return_if_fail (str != NULL);
4283 /* Split text into the base text and a separate pattern
4286 if (!separate_uline_pattern (str, &accel_key, &new_str, &pattern))
4289 gtk_label_set_text_internal (label, new_str);
4290 gtk_label_set_pattern_internal (label, pattern, TRUE);
4291 priv->mnemonic_keyval = accel_key;
4297 * gtk_label_set_text_with_mnemonic:
4298 * @label: a #GtkLabel
4301 * Sets the label's text from the string @str.
4302 * If characters in @str are preceded by an underscore, they are underlined
4303 * indicating that they represent a keyboard accelerator called a mnemonic.
4304 * The mnemonic key can be used to activate another widget, chosen
4305 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
4308 gtk_label_set_text_with_mnemonic (GtkLabel *label,
4311 g_return_if_fail (GTK_IS_LABEL (label));
4312 g_return_if_fail (str != NULL);
4314 g_object_freeze_notify (G_OBJECT (label));
4316 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
4317 gtk_label_set_use_markup_internal (label, FALSE);
4318 gtk_label_set_use_underline_internal (label, TRUE);
4320 gtk_label_recalculate (label);
4322 g_object_thaw_notify (G_OBJECT (label));
4326 gtk_label_realize (GtkWidget *widget)
4328 GtkLabel *label = GTK_LABEL (widget);
4329 GtkLabelPrivate *priv = label->priv;
4331 GTK_WIDGET_CLASS (gtk_label_parent_class)->realize (widget);
4333 if (priv->select_info)
4334 gtk_label_create_window (label);
4338 gtk_label_unrealize (GtkWidget *widget)
4340 GtkLabel *label = GTK_LABEL (widget);
4341 GtkLabelPrivate *priv = label->priv;
4343 if (priv->select_info)
4344 gtk_label_destroy_window (label);
4346 GTK_WIDGET_CLASS (gtk_label_parent_class)->unrealize (widget);
4350 gtk_label_map (GtkWidget *widget)
4352 GtkLabel *label = GTK_LABEL (widget);
4353 GtkLabelPrivate *priv = label->priv;
4355 GTK_WIDGET_CLASS (gtk_label_parent_class)->map (widget);
4357 if (priv->select_info)
4358 gdk_window_show (priv->select_info->window);
4362 gtk_label_unmap (GtkWidget *widget)
4364 GtkLabel *label = GTK_LABEL (widget);
4365 GtkLabelPrivate *priv = label->priv;
4367 if (priv->select_info)
4368 gdk_window_hide (priv->select_info->window);
4370 GTK_WIDGET_CLASS (gtk_label_parent_class)->unmap (widget);
4374 window_to_layout_coords (GtkLabel *label,
4378 GtkAllocation allocation;
4382 widget = GTK_WIDGET (label);
4384 /* get layout location in widget->window coords */
4385 get_layout_location (label, &lx, &ly);
4387 gtk_widget_get_allocation (widget, &allocation);
4391 *x += allocation.x; /* go to widget->window */
4392 *x -= lx; /* go to layout */
4397 *y += allocation.y; /* go to widget->window */
4398 *y -= ly; /* go to layout */
4404 layout_to_window_coords (GtkLabel *label,
4411 widget = GTK_WIDGET (label);
4413 /* get layout location in widget->window coords */
4414 get_layout_location (label, &lx, &ly);
4418 *x += lx; /* go to widget->window */
4419 *x -= widget->allocation.x; /* go to selection window */
4424 *y += ly; /* go to widget->window */
4425 *y -= widget->allocation.y; /* go to selection window */
4431 get_layout_index (GtkLabel *label,
4436 GtkLabelPrivate *priv = label->priv;
4438 const gchar *cluster;
4439 const gchar *cluster_end;
4444 gtk_label_ensure_layout (label);
4446 window_to_layout_coords (label, &x, &y);
4451 inside = pango_layout_xy_to_index (priv->layout,
4455 cluster = priv->text + *index;
4456 cluster_end = cluster;
4459 cluster_end = g_utf8_next_char (cluster_end);
4463 *index += (cluster_end - cluster);
4469 gtk_label_select_word (GtkLabel *label)
4471 GtkLabelPrivate *priv = label->priv;
4474 gint start_index = gtk_label_move_backward_word (label, priv->select_info->selection_end);
4475 gint end_index = gtk_label_move_forward_word (label, priv->select_info->selection_end);
4477 min = MIN (priv->select_info->selection_anchor,
4478 priv->select_info->selection_end);
4479 max = MAX (priv->select_info->selection_anchor,
4480 priv->select_info->selection_end);
4482 min = MIN (min, start_index);
4483 max = MAX (max, end_index);
4485 gtk_label_select_region_index (label, min, max);
4489 gtk_label_grab_focus (GtkWidget *widget)
4491 GtkLabel *label = GTK_LABEL (widget);
4492 GtkLabelPrivate *priv = label->priv;
4493 gboolean select_on_focus;
4496 if (priv->select_info == NULL)
4499 GTK_WIDGET_CLASS (gtk_label_parent_class)->grab_focus (widget);
4501 if (priv->select_info->selectable)
4503 g_object_get (gtk_widget_get_settings (widget),
4504 "gtk-label-select-on-focus",
4508 if (select_on_focus && !priv->in_click)
4509 gtk_label_select_region (label, 0, -1);
4513 if (priv->select_info->links && !priv->in_click)
4515 link = priv->select_info->links->data;
4516 priv->select_info->selection_anchor = link->start;
4517 priv->select_info->selection_end = link->start;
4523 gtk_label_focus (GtkWidget *widget,
4524 GtkDirectionType direction)
4526 GtkLabel *label = GTK_LABEL (widget);
4527 GtkLabelPrivate *priv = label->priv;
4528 GtkLabelSelectionInfo *info = priv->select_info;
4529 GtkLabelLink *focus_link;
4532 if (!gtk_widget_is_focus (widget))
4534 gtk_widget_grab_focus (widget);
4537 focus_link = gtk_label_get_focus_link (label);
4538 if (focus_link && direction == GTK_DIR_TAB_BACKWARD)
4540 l = g_list_last (info->links);
4541 focus_link = l->data;
4542 info->selection_anchor = focus_link->start;
4543 info->selection_end = focus_link->start;
4553 if (info->selectable)
4557 if (info->selection_anchor != info->selection_end)
4560 index = info->selection_anchor;
4562 if (direction == GTK_DIR_TAB_FORWARD)
4563 for (l = info->links; l; l = l->next)
4565 GtkLabelLink *link = l->data;
4567 if (link->start > index)
4569 gtk_label_select_region_index (label, link->start, link->start);
4573 else if (direction == GTK_DIR_TAB_BACKWARD)
4574 for (l = g_list_last (info->links); l; l = l->prev)
4576 GtkLabelLink *link = l->data;
4578 if (link->end < index)
4580 gtk_label_select_region_index (label, link->start, link->start);
4589 focus_link = gtk_label_get_focus_link (label);
4592 case GTK_DIR_TAB_FORWARD:
4595 l = g_list_find (info->links, focus_link);
4602 case GTK_DIR_TAB_BACKWARD:
4605 l = g_list_find (info->links, focus_link);
4609 l = g_list_last (info->links);
4618 focus_link = l->data;
4619 info->selection_anchor = focus_link->start;
4620 info->selection_end = focus_link->start;
4621 gtk_widget_queue_draw (widget);
4633 gtk_label_button_press (GtkWidget *widget,
4634 GdkEventButton *event)
4636 GtkLabel *label = GTK_LABEL (widget);
4637 GtkLabelPrivate *priv = label->priv;
4638 GtkLabelSelectionInfo *info = priv->select_info;
4645 if (info->active_link)
4647 if (gdk_event_triggers_context_menu ((GdkEvent *) event))
4649 info->link_clicked = 1;
4650 gtk_label_do_popup (label, event);
4653 else if (event->button == GDK_BUTTON_PRIMARY)
4655 info->link_clicked = 1;
4656 gtk_widget_queue_draw (widget);
4660 if (!info->selectable)
4663 info->in_drag = FALSE;
4664 info->select_words = FALSE;
4666 if (gdk_event_triggers_context_menu ((GdkEvent *) event))
4668 gtk_label_do_popup (label, event);
4672 else if (event->button == GDK_BUTTON_PRIMARY)
4674 if (!gtk_widget_has_focus (widget))
4676 priv->in_click = TRUE;
4677 gtk_widget_grab_focus (widget);
4678 priv->in_click = FALSE;
4681 if (event->type == GDK_3BUTTON_PRESS)
4683 gtk_label_select_region_index (label, 0, strlen (priv->text));
4687 if (event->type == GDK_2BUTTON_PRESS)
4689 info->select_words = TRUE;
4690 gtk_label_select_word (label);
4694 get_layout_index (label, event->x, event->y, &index);
4696 min = MIN (info->selection_anchor, info->selection_end);
4697 max = MAX (info->selection_anchor, info->selection_end);
4699 if ((info->selection_anchor != info->selection_end) &&
4700 (event->state & GDK_SHIFT_MASK))
4702 if (index > min && index < max)
4704 /* truncate selection, but keep it as big as possible */
4705 if (index - min > max - index)
4712 /* extend (same as motion) */
4713 min = MIN (min, index);
4714 max = MAX (max, index);
4717 /* ensure the anchor is opposite index */
4725 gtk_label_select_region_index (label, min, max);
4729 if (event->type == GDK_3BUTTON_PRESS)
4730 gtk_label_select_region_index (label, 0, strlen (priv->text));
4731 else if (event->type == GDK_2BUTTON_PRESS)
4732 gtk_label_select_word (label);
4733 else if (min < max && min <= index && index <= max)
4735 info->in_drag = TRUE;
4736 info->drag_start_x = event->x;
4737 info->drag_start_y = event->y;
4740 /* start a replacement */
4741 gtk_label_select_region_index (label, index, index);
4751 gtk_label_button_release (GtkWidget *widget,
4752 GdkEventButton *event)
4755 GtkLabel *label = GTK_LABEL (widget);
4756 GtkLabelPrivate *priv = label->priv;
4757 GtkLabelSelectionInfo *info = priv->select_info;
4767 get_layout_index (label, event->x, event->y, &index);
4768 gtk_label_select_region_index (label, index, index);
4773 if (event->button != GDK_BUTTON_PRIMARY)
4776 if (info->active_link &&
4777 info->selection_anchor == info->selection_end &&
4780 emit_activate_link (label, info->active_link);
4781 info->link_clicked = 0;
4786 /* The goal here is to return TRUE iff we ate the
4787 * button press to start selecting.
4793 connect_mnemonics_visible_notify (GtkLabel *label)
4795 GtkLabelPrivate *priv = label->priv;
4796 GtkWidget *toplevel;
4799 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
4801 if (!GTK_IS_WINDOW (toplevel))
4804 /* always set up this widgets initial value */
4805 priv->mnemonics_visible =
4806 gtk_window_get_mnemonics_visible (GTK_WINDOW (toplevel));
4809 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (toplevel),
4810 "gtk-label-mnemonics-visible-connected"));
4814 g_signal_connect (toplevel,
4815 "notify::mnemonics-visible",
4816 G_CALLBACK (label_mnemonics_visible_changed),
4818 g_object_set_data (G_OBJECT (toplevel),
4819 "gtk-label-mnemonics-visible-connected",
4820 GINT_TO_POINTER (1));
4825 drag_begin_cb (GtkWidget *widget,
4826 GdkDragContext *context,
4829 GtkLabel *label = GTK_LABEL (widget);
4830 GtkLabelPrivate *priv = label->priv;
4831 cairo_surface_t *surface = NULL;
4833 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
4835 if ((priv->select_info->selection_anchor !=
4836 priv->select_info->selection_end) &&
4842 start = MIN (priv->select_info->selection_anchor,
4843 priv->select_info->selection_end);
4844 end = MAX (priv->select_info->selection_anchor,
4845 priv->select_info->selection_end);
4847 len = strlen (priv->text);
4855 surface = _gtk_text_util_create_drag_icon (widget,
4862 gtk_drag_set_icon_surface (context, surface);
4863 cairo_surface_destroy (surface);
4867 gtk_drag_set_icon_default (context);
4872 gtk_label_motion (GtkWidget *widget,
4873 GdkEventMotion *event)
4875 GtkLabel *label = GTK_LABEL (widget);
4876 GtkLabelPrivate *priv = label->priv;
4877 GtkLabelSelectionInfo *info = priv->select_info;
4883 if (info->links && !info->in_drag)
4887 gboolean found = FALSE;
4889 if (info->selection_anchor == info->selection_end)
4891 if (get_layout_index (label, event->x, event->y, &index))
4893 for (l = info->links; l != NULL; l = l->next)
4896 if (index >= link->start && index <= link->end)
4907 if (info->active_link != link)
4909 info->link_clicked = 0;
4910 info->active_link = link;
4911 gtk_label_update_cursor (label);
4912 gtk_widget_queue_draw (widget);
4917 if (info->active_link != NULL)
4919 info->link_clicked = 0;
4920 info->active_link = NULL;
4921 gtk_label_update_cursor (label);
4922 gtk_widget_queue_draw (widget);
4927 if (!info->selectable)
4930 if ((event->state & GDK_BUTTON1_MASK) == 0)
4935 if (gtk_drag_check_threshold (widget,
4938 event->x, event->y))
4940 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
4942 gtk_target_list_add_text_targets (target_list, 0);
4944 g_signal_connect (widget, "drag-begin",
4945 G_CALLBACK (drag_begin_cb), NULL);
4946 gtk_drag_begin (widget, target_list,
4948 1, (GdkEvent *)event);
4950 info->in_drag = FALSE;
4952 gtk_target_list_unref (target_list);
4959 gdk_window_get_device_position (info->window, event->device, &x, &y, NULL);
4960 get_layout_index (label, x, y, &index);
4962 if (info->select_words)
4965 gint old_min, old_max;
4968 min = gtk_label_move_backward_word (label, index);
4969 max = gtk_label_move_forward_word (label, index);
4971 anchor = info->selection_anchor;
4972 end = info->selection_end;
4974 old_min = MIN (anchor, end);
4975 old_max = MAX (anchor, end);
4982 else if (old_max < max)
4987 else if (anchor == old_min)
4998 gtk_label_select_region_index (label, anchor, end);
5001 gtk_label_select_region_index (label, info->selection_anchor, index);
5008 gtk_label_leave_notify (GtkWidget *widget,
5009 GdkEventCrossing *event)
5011 GtkLabel *label = GTK_LABEL (widget);
5012 GtkLabelPrivate *priv = label->priv;
5014 if (priv->select_info)
5016 priv->select_info->active_link = NULL;
5017 gtk_label_update_cursor (label);
5018 gtk_widget_queue_draw (widget);
5021 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event)
5022 return GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event (widget, event);
5028 gtk_label_create_window (GtkLabel *label)
5030 GtkLabelPrivate *priv = label->priv;
5031 GtkAllocation allocation;
5033 GdkWindowAttr attributes;
5034 gint attributes_mask;
5036 g_assert (priv->select_info);
5037 widget = GTK_WIDGET (label);
5038 g_assert (gtk_widget_get_realized (widget));
5040 if (priv->select_info->window)
5043 gtk_widget_get_allocation (widget, &allocation);
5045 attributes.x = allocation.x;
5046 attributes.y = allocation.y;
5047 attributes.width = allocation.width;
5048 attributes.height = allocation.height;
5049 attributes.window_type = GDK_WINDOW_CHILD;
5050 attributes.wclass = GDK_INPUT_ONLY;
5051 attributes.override_redirect = TRUE;
5052 attributes.event_mask = gtk_widget_get_events (widget) |
5053 GDK_BUTTON_PRESS_MASK |
5054 GDK_BUTTON_RELEASE_MASK |
5055 GDK_LEAVE_NOTIFY_MASK |
5056 GDK_BUTTON_MOTION_MASK |
5057 GDK_POINTER_MOTION_MASK |
5058 GDK_POINTER_MOTION_HINT_MASK;
5059 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
5060 if (gtk_widget_is_sensitive (widget))
5062 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
5064 attributes_mask |= GDK_WA_CURSOR;
5068 priv->select_info->window = gdk_window_new (gtk_widget_get_window (widget),
5069 &attributes, attributes_mask);
5070 gdk_window_set_user_data (priv->select_info->window, widget);
5072 if (attributes_mask & GDK_WA_CURSOR)
5073 g_object_unref (attributes.cursor);
5077 gtk_label_destroy_window (GtkLabel *label)
5079 GtkLabelPrivate *priv = label->priv;
5081 g_assert (priv->select_info);
5083 if (priv->select_info->window == NULL)
5086 gdk_window_set_user_data (priv->select_info->window, NULL);
5087 gdk_window_destroy (priv->select_info->window);
5088 priv->select_info->window = NULL;
5092 gtk_label_ensure_select_info (GtkLabel *label)
5094 GtkLabelPrivate *priv = label->priv;
5096 if (priv->select_info == NULL)
5098 priv->select_info = g_new0 (GtkLabelSelectionInfo, 1);
5100 gtk_widget_set_can_focus (GTK_WIDGET (label), TRUE);
5102 if (gtk_widget_get_realized (GTK_WIDGET (label)))
5103 gtk_label_create_window (label);
5105 if (gtk_widget_get_mapped (GTK_WIDGET (label)))
5106 gdk_window_show (priv->select_info->window);
5111 gtk_label_clear_select_info (GtkLabel *label)
5113 GtkLabelPrivate *priv = label->priv;
5115 if (priv->select_info == NULL)
5118 if (!priv->select_info->selectable && !priv->select_info->links)
5120 gtk_label_destroy_window (label);
5122 g_free (priv->select_info);
5123 priv->select_info = NULL;
5125 gtk_widget_set_can_focus (GTK_WIDGET (label), FALSE);
5130 * gtk_label_set_selectable:
5131 * @label: a #GtkLabel
5132 * @setting: %TRUE to allow selecting text in the label
5134 * Selectable labels allow the user to select text from the label, for
5138 gtk_label_set_selectable (GtkLabel *label,
5141 GtkLabelPrivate *priv;
5142 gboolean old_setting;
5144 g_return_if_fail (GTK_IS_LABEL (label));
5148 setting = setting != FALSE;
5149 old_setting = priv->select_info && priv->select_info->selectable;
5153 gtk_label_ensure_select_info (label);
5154 priv->select_info->selectable = TRUE;
5155 gtk_label_update_cursor (label);
5161 /* unselect, to give up the selection */
5162 gtk_label_select_region (label, 0, 0);
5164 priv->select_info->selectable = FALSE;
5165 gtk_label_clear_select_info (label);
5166 gtk_label_update_cursor (label);
5169 if (setting != old_setting)
5171 g_object_freeze_notify (G_OBJECT (label));
5172 g_object_notify (G_OBJECT (label), "selectable");
5173 g_object_notify (G_OBJECT (label), "cursor-position");
5174 g_object_notify (G_OBJECT (label), "selection-bound");
5175 g_object_thaw_notify (G_OBJECT (label));
5176 gtk_widget_queue_draw (GTK_WIDGET (label));
5181 * gtk_label_get_selectable:
5182 * @label: a #GtkLabel
5184 * Gets the value set by gtk_label_set_selectable().
5186 * Return value: %TRUE if the user can copy text from the label
5189 gtk_label_get_selectable (GtkLabel *label)
5191 GtkLabelPrivate *priv;
5193 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5197 return priv->select_info && priv->select_info->selectable;
5201 * gtk_label_set_angle:
5202 * @label: a #GtkLabel
5203 * @angle: the angle that the baseline of the label makes with
5204 * the horizontal, in degrees, measured counterclockwise
5206 * Sets the angle of rotation for the label. An angle of 90 reads from
5207 * from bottom to top, an angle of 270, from top to bottom. The angle
5208 * setting for the label is ignored if the label is selectable,
5209 * wrapped, or ellipsized.
5214 gtk_label_set_angle (GtkLabel *label,
5217 GtkLabelPrivate *priv;
5219 g_return_if_fail (GTK_IS_LABEL (label));
5223 /* Canonicalize to [0,360]. We don't canonicalize 360 to 0, because
5224 * double property ranges are inclusive, and changing 360 to 0 would
5225 * make a property editor behave strangely.
5227 if (angle < 0 || angle > 360.0)
5228 angle = angle - 360. * floor (angle / 360.);
5230 if (priv->angle != angle)
5232 priv->angle = angle;
5234 gtk_label_clear_layout (label);
5235 gtk_widget_queue_resize (GTK_WIDGET (label));
5237 g_object_notify (G_OBJECT (label), "angle");
5242 * gtk_label_get_angle:
5243 * @label: a #GtkLabel
5245 * Gets the angle of rotation for the label. See
5246 * gtk_label_set_angle().
5248 * Return value: the angle of rotation for the label
5253 gtk_label_get_angle (GtkLabel *label)
5255 g_return_val_if_fail (GTK_IS_LABEL (label), 0.0);
5257 return label->priv->angle;
5261 gtk_label_set_selection_text (GtkLabel *label,
5262 GtkSelectionData *selection_data)
5264 GtkLabelPrivate *priv = label->priv;
5266 if (priv->select_info &&
5267 (priv->select_info->selection_anchor !=
5268 priv->select_info->selection_end) &&
5274 start = MIN (priv->select_info->selection_anchor,
5275 priv->select_info->selection_end);
5276 end = MAX (priv->select_info->selection_anchor,
5277 priv->select_info->selection_end);
5279 len = strlen (priv->text);
5287 gtk_selection_data_set_text (selection_data,
5294 gtk_label_drag_data_get (GtkWidget *widget,
5295 GdkDragContext *context,
5296 GtkSelectionData *selection_data,
5300 gtk_label_set_selection_text (GTK_LABEL (widget), selection_data);
5304 get_text_callback (GtkClipboard *clipboard,
5305 GtkSelectionData *selection_data,
5307 gpointer user_data_or_owner)
5309 gtk_label_set_selection_text (GTK_LABEL (user_data_or_owner), selection_data);
5313 clear_text_callback (GtkClipboard *clipboard,
5314 gpointer user_data_or_owner)
5317 GtkLabelPrivate *priv;
5319 label = GTK_LABEL (user_data_or_owner);
5322 if (priv->select_info)
5324 priv->select_info->selection_anchor = priv->select_info->selection_end;
5326 gtk_widget_queue_draw (GTK_WIDGET (label));
5331 gtk_label_select_region_index (GtkLabel *label,
5335 GtkLabelPrivate *priv;
5337 g_return_if_fail (GTK_IS_LABEL (label));
5341 if (priv->select_info && priv->select_info->selectable)
5343 GtkClipboard *clipboard;
5345 if (priv->select_info->selection_anchor == anchor_index &&
5346 priv->select_info->selection_end == end_index)
5349 g_object_freeze_notify (G_OBJECT (label));
5351 if (priv->select_info->selection_anchor != anchor_index)
5352 g_object_notify (G_OBJECT (label), "selection-bound");
5353 if (priv->select_info->selection_end != end_index)
5354 g_object_notify (G_OBJECT (label), "cursor-position");
5356 priv->select_info->selection_anchor = anchor_index;
5357 priv->select_info->selection_end = end_index;
5359 if (gtk_widget_has_screen (GTK_WIDGET (label)))
5360 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label),
5361 GDK_SELECTION_PRIMARY);
5365 if (anchor_index != end_index)
5367 GtkTargetList *list;
5368 GtkTargetEntry *targets;
5371 list = gtk_target_list_new (NULL, 0);
5372 gtk_target_list_add_text_targets (list, 0);
5373 targets = gtk_target_table_new_from_list (list, &n_targets);
5376 gtk_clipboard_set_with_owner (clipboard,
5379 clear_text_callback,
5382 gtk_target_table_free (targets, n_targets);
5383 gtk_target_list_unref (list);
5388 gtk_clipboard_get_owner (clipboard) == G_OBJECT (label))
5389 gtk_clipboard_clear (clipboard);
5392 gtk_widget_queue_draw (GTK_WIDGET (label));
5394 g_object_thaw_notify (G_OBJECT (label));
5399 * gtk_label_select_region:
5400 * @label: a #GtkLabel
5401 * @start_offset: start offset (in characters not bytes)
5402 * @end_offset: end offset (in characters not bytes)
5404 * Selects a range of characters in the label, if the label is selectable.
5405 * See gtk_label_set_selectable(). If the label is not selectable,
5406 * this function has no effect. If @start_offset or
5407 * @end_offset are -1, then the end of the label will be substituted.
5410 gtk_label_select_region (GtkLabel *label,
5414 GtkLabelPrivate *priv;
5416 g_return_if_fail (GTK_IS_LABEL (label));
5420 if (priv->text && priv->select_info)
5422 if (start_offset < 0)
5423 start_offset = g_utf8_strlen (priv->text, -1);
5426 end_offset = g_utf8_strlen (priv->text, -1);
5428 gtk_label_select_region_index (label,
5429 g_utf8_offset_to_pointer (priv->text, start_offset) - priv->text,
5430 g_utf8_offset_to_pointer (priv->text, end_offset) - priv->text);
5435 * gtk_label_get_selection_bounds:
5436 * @label: a #GtkLabel
5437 * @start: (out): return location for start of selection, as a character offset
5438 * @end: (out): return location for end of selection, as a character offset
5440 * Gets the selected range of characters in the label, returning %TRUE
5441 * if there's a selection.
5443 * Return value: %TRUE if selection is non-empty
5446 gtk_label_get_selection_bounds (GtkLabel *label,
5450 GtkLabelPrivate *priv;
5452 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5456 if (priv->select_info == NULL)
5458 /* not a selectable label */
5468 gint start_index, end_index;
5469 gint start_offset, end_offset;
5472 start_index = MIN (priv->select_info->selection_anchor,
5473 priv->select_info->selection_end);
5474 end_index = MAX (priv->select_info->selection_anchor,
5475 priv->select_info->selection_end);
5477 len = strlen (priv->text);
5479 if (end_index > len)
5482 if (start_index > len)
5485 start_offset = g_utf8_strlen (priv->text, start_index);
5486 end_offset = g_utf8_strlen (priv->text, end_index);
5488 if (start_offset > end_offset)
5490 gint tmp = start_offset;
5491 start_offset = end_offset;
5496 *start = start_offset;
5501 return start_offset != end_offset;
5507 * gtk_label_get_layout:
5508 * @label: a #GtkLabel
5510 * Gets the #PangoLayout used to display the label.
5511 * The layout is useful to e.g. convert text positions to
5512 * pixel positions, in combination with gtk_label_get_layout_offsets().
5513 * The returned layout is owned by the @label so need not be
5514 * freed by the caller. The @label is free to recreate its layout at
5515 * any time, so it should be considered read-only.
5517 * Return value: (transfer none): the #PangoLayout for this label
5520 gtk_label_get_layout (GtkLabel *label)
5522 GtkLabelPrivate *priv;
5524 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
5528 gtk_label_ensure_layout (label);
5530 return priv->layout;
5534 * gtk_label_get_layout_offsets:
5535 * @label: a #GtkLabel
5536 * @x: (out) (allow-none): location to store X offset of layout, or %NULL
5537 * @y: (out) (allow-none): location to store Y offset of layout, or %NULL
5539 * Obtains the coordinates where the label will draw the #PangoLayout
5540 * representing the text in the label; useful to convert mouse events
5541 * into coordinates inside the #PangoLayout, e.g. to take some action
5542 * if some part of the label is clicked. Of course you will need to
5543 * create a #GtkEventBox to receive the events, and pack the label
5544 * inside it, since labels are a #GTK_NO_WINDOW widget. Remember
5545 * when using the #PangoLayout functions you need to convert to
5546 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
5549 gtk_label_get_layout_offsets (GtkLabel *label,
5553 g_return_if_fail (GTK_IS_LABEL (label));
5555 gtk_label_ensure_layout (label);
5557 get_layout_location (label, x, y);
5561 * gtk_label_set_use_markup:
5562 * @label: a #GtkLabel
5563 * @setting: %TRUE if the label's text should be parsed for markup.
5565 * Sets whether the text of the label contains markup in <link
5566 * linkend="PangoMarkupFormat">Pango's text markup
5567 * language</link>. See gtk_label_set_markup().
5570 gtk_label_set_use_markup (GtkLabel *label,
5573 g_return_if_fail (GTK_IS_LABEL (label));
5575 g_object_freeze_notify (G_OBJECT (label));
5577 gtk_label_set_use_markup_internal (label, setting);
5578 gtk_label_recalculate (label);
5580 g_object_thaw_notify (G_OBJECT (label));
5584 * gtk_label_get_use_markup:
5585 * @label: a #GtkLabel
5587 * Returns whether the label's text is interpreted as marked up with
5588 * the <link linkend="PangoMarkupFormat">Pango text markup
5589 * language</link>. See gtk_label_set_use_markup ().
5591 * Return value: %TRUE if the label's text will be parsed for markup.
5594 gtk_label_get_use_markup (GtkLabel *label)
5596 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5598 return label->priv->use_markup;
5602 * gtk_label_set_use_underline:
5603 * @label: a #GtkLabel
5604 * @setting: %TRUE if underlines in the text indicate mnemonics
5606 * If true, an underline in the text indicates the next character should be
5607 * used for the mnemonic accelerator key.
5610 gtk_label_set_use_underline (GtkLabel *label,
5613 g_return_if_fail (GTK_IS_LABEL (label));
5615 g_object_freeze_notify (G_OBJECT (label));
5617 gtk_label_set_use_underline_internal (label, setting);
5618 gtk_label_recalculate (label);
5620 g_object_thaw_notify (G_OBJECT (label));
5624 * gtk_label_get_use_underline:
5625 * @label: a #GtkLabel
5627 * Returns whether an embedded underline in the label indicates a
5628 * mnemonic. See gtk_label_set_use_underline().
5630 * Return value: %TRUE whether an embedded underline in the label indicates
5631 * the mnemonic accelerator keys.
5634 gtk_label_get_use_underline (GtkLabel *label)
5636 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5638 return label->priv->use_underline;
5642 * gtk_label_set_single_line_mode:
5643 * @label: a #GtkLabel
5644 * @single_line_mode: %TRUE if the label should be in single line mode
5646 * Sets whether the label is in single line mode.
5651 gtk_label_set_single_line_mode (GtkLabel *label,
5652 gboolean single_line_mode)
5654 GtkLabelPrivate *priv;
5656 g_return_if_fail (GTK_IS_LABEL (label));
5660 single_line_mode = single_line_mode != FALSE;
5662 if (priv->single_line_mode != single_line_mode)
5664 priv->single_line_mode = single_line_mode;
5666 gtk_label_clear_layout (label);
5667 gtk_widget_queue_resize (GTK_WIDGET (label));
5669 g_object_notify (G_OBJECT (label), "single-line-mode");
5674 * gtk_label_get_single_line_mode:
5675 * @label: a #GtkLabel
5677 * Returns whether the label is in single line mode.
5679 * Return value: %TRUE when the label is in single line mode.
5684 gtk_label_get_single_line_mode (GtkLabel *label)
5686 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5688 return label->priv->single_line_mode;
5691 /* Compute the X position for an offset that corresponds to the "more important
5692 * cursor position for that offset. We use this when trying to guess to which
5693 * end of the selection we should go to when the user hits the left or
5697 get_better_cursor (GtkLabel *label,
5702 GtkLabelPrivate *priv = label->priv;
5703 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5704 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5705 PangoDirection cursor_direction = get_cursor_direction (label);
5706 gboolean split_cursor;
5707 PangoRectangle strong_pos, weak_pos;
5709 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5710 "gtk-split-cursor", &split_cursor,
5713 gtk_label_ensure_layout (label);
5715 pango_layout_get_cursor_pos (priv->layout, index,
5716 &strong_pos, &weak_pos);
5720 *x = strong_pos.x / PANGO_SCALE;
5721 *y = strong_pos.y / PANGO_SCALE;
5725 if (keymap_direction == cursor_direction)
5727 *x = strong_pos.x / PANGO_SCALE;
5728 *y = strong_pos.y / PANGO_SCALE;
5732 *x = weak_pos.x / PANGO_SCALE;
5733 *y = weak_pos.y / PANGO_SCALE;
5740 gtk_label_move_logically (GtkLabel *label,
5744 GtkLabelPrivate *priv = label->priv;
5745 gint offset = g_utf8_pointer_to_offset (priv->text,
5746 priv->text + start);
5750 PangoLogAttr *log_attrs;
5754 gtk_label_ensure_layout (label);
5756 length = g_utf8_strlen (priv->text, -1);
5758 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5760 while (count > 0 && offset < length)
5764 while (offset < length && !log_attrs[offset].is_cursor_position);
5768 while (count < 0 && offset > 0)
5772 while (offset > 0 && !log_attrs[offset].is_cursor_position);
5780 return g_utf8_offset_to_pointer (priv->text, offset) - priv->text;
5784 gtk_label_move_visually (GtkLabel *label,
5788 GtkLabelPrivate *priv = label->priv;
5795 int new_index, new_trailing;
5796 gboolean split_cursor;
5799 gtk_label_ensure_layout (label);
5801 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5802 "gtk-split-cursor", &split_cursor,
5809 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5810 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5812 strong = keymap_direction == get_cursor_direction (label);
5817 pango_layout_move_cursor_visually (priv->layout, strong, index, 0, 1, &new_index, &new_trailing);
5822 pango_layout_move_cursor_visually (priv->layout, strong, index, 0, -1, &new_index, &new_trailing);
5826 if (new_index < 0 || new_index == G_MAXINT)
5831 while (new_trailing--)
5832 index = g_utf8_next_char (priv->text + new_index) - priv->text;
5839 gtk_label_move_forward_word (GtkLabel *label,
5842 GtkLabelPrivate *priv = label->priv;
5843 gint new_pos = g_utf8_pointer_to_offset (priv->text,
5844 priv->text + start);
5847 length = g_utf8_strlen (priv->text, -1);
5848 if (new_pos < length)
5850 PangoLogAttr *log_attrs;
5853 gtk_label_ensure_layout (label);
5855 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5857 /* Find the next word end */
5859 while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
5865 return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
5870 gtk_label_move_backward_word (GtkLabel *label,
5873 GtkLabelPrivate *priv = label->priv;
5874 gint new_pos = g_utf8_pointer_to_offset (priv->text,
5875 priv->text + start);
5879 PangoLogAttr *log_attrs;
5882 gtk_label_ensure_layout (label);
5884 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5888 /* Find the previous word beginning */
5889 while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
5895 return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
5899 gtk_label_move_cursor (GtkLabel *label,
5900 GtkMovementStep step,
5902 gboolean extend_selection)
5904 GtkLabelPrivate *priv = label->priv;
5908 if (priv->select_info == NULL)
5911 old_pos = new_pos = priv->select_info->selection_end;
5913 if (priv->select_info->selection_end != priv->select_info->selection_anchor &&
5916 /* If we have a current selection and aren't extending it, move to the
5917 * start/or end of the selection as appropriate
5921 case GTK_MOVEMENT_VISUAL_POSITIONS:
5924 gint anchor_x, anchor_y;
5925 gboolean end_is_left;
5927 get_better_cursor (label, priv->select_info->selection_end, &end_x, &end_y);
5928 get_better_cursor (label, priv->select_info->selection_anchor, &anchor_x, &anchor_y);
5930 end_is_left = (end_y < anchor_y) || (end_y == anchor_y && end_x < anchor_x);
5933 new_pos = end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
5935 new_pos = !end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
5938 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5939 case GTK_MOVEMENT_WORDS:
5941 new_pos = MIN (priv->select_info->selection_end, priv->select_info->selection_anchor);
5943 new_pos = MAX (priv->select_info->selection_end, priv->select_info->selection_anchor);
5945 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5946 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5947 case GTK_MOVEMENT_BUFFER_ENDS:
5948 /* FIXME: Can do better here */
5949 new_pos = count < 0 ? 0 : strlen (priv->text);
5951 case GTK_MOVEMENT_DISPLAY_LINES:
5952 case GTK_MOVEMENT_PARAGRAPHS:
5953 case GTK_MOVEMENT_PAGES:
5954 case GTK_MOVEMENT_HORIZONTAL_PAGES:
5962 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5963 new_pos = gtk_label_move_logically (label, new_pos, count);
5965 case GTK_MOVEMENT_VISUAL_POSITIONS:
5966 new_pos = gtk_label_move_visually (label, new_pos, count);
5967 if (new_pos == old_pos)
5969 if (!extend_selection)
5971 if (!gtk_widget_keynav_failed (GTK_WIDGET (label),
5973 GTK_DIR_RIGHT : GTK_DIR_LEFT))
5975 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
5978 gtk_widget_child_focus (toplevel,
5980 GTK_DIR_RIGHT : GTK_DIR_LEFT);
5985 gtk_widget_error_bell (GTK_WIDGET (label));
5989 case GTK_MOVEMENT_WORDS:
5992 new_pos = gtk_label_move_forward_word (label, new_pos);
5997 new_pos = gtk_label_move_backward_word (label, new_pos);
6000 if (new_pos == old_pos)
6001 gtk_widget_error_bell (GTK_WIDGET (label));
6003 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
6004 case GTK_MOVEMENT_PARAGRAPH_ENDS:
6005 case GTK_MOVEMENT_BUFFER_ENDS:
6006 /* FIXME: Can do better here */
6007 new_pos = count < 0 ? 0 : strlen (priv->text);
6008 if (new_pos == old_pos)
6009 gtk_widget_error_bell (GTK_WIDGET (label));
6011 case GTK_MOVEMENT_DISPLAY_LINES:
6012 case GTK_MOVEMENT_PARAGRAPHS:
6013 case GTK_MOVEMENT_PAGES:
6014 case GTK_MOVEMENT_HORIZONTAL_PAGES:
6019 if (extend_selection)
6020 gtk_label_select_region_index (label,
6021 priv->select_info->selection_anchor,
6024 gtk_label_select_region_index (label, new_pos, new_pos);
6028 gtk_label_copy_clipboard (GtkLabel *label)
6030 GtkLabelPrivate *priv = label->priv;
6032 if (priv->text && priv->select_info)
6036 GtkClipboard *clipboard;
6038 start = MIN (priv->select_info->selection_anchor,
6039 priv->select_info->selection_end);
6040 end = MAX (priv->select_info->selection_anchor,
6041 priv->select_info->selection_end);
6043 len = strlen (priv->text);
6051 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6054 gtk_clipboard_set_text (clipboard, priv->text + start, end - start);
6059 link = gtk_label_get_focus_link (label);
6061 gtk_clipboard_set_text (clipboard, link->uri, -1);
6067 gtk_label_select_all (GtkLabel *label)
6069 GtkLabelPrivate *priv = label->priv;
6071 gtk_label_select_region_index (label, 0, strlen (priv->text));
6074 /* Quick hack of a popup menu
6077 activate_cb (GtkWidget *menuitem,
6080 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6081 g_signal_emit_by_name (label, signal);
6085 append_action_signal (GtkLabel *label,
6087 const gchar *stock_id,
6088 const gchar *signal,
6091 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6093 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
6094 g_signal_connect (menuitem, "activate",
6095 G_CALLBACK (activate_cb), label);
6097 gtk_widget_set_sensitive (menuitem, sensitive);
6099 gtk_widget_show (menuitem);
6100 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6104 popup_menu_detach (GtkWidget *attach_widget,
6107 GtkLabel *label = GTK_LABEL (attach_widget);
6108 GtkLabelPrivate *priv = label->priv;
6110 if (priv->select_info)
6111 priv->select_info->popup_menu = NULL;
6115 popup_position_func (GtkMenu *menu,
6123 GtkAllocation allocation;
6127 label = GTK_LABEL (user_data);
6128 widget = GTK_WIDGET (label);
6130 g_return_if_fail (gtk_widget_get_realized (widget));
6132 screen = gtk_widget_get_screen (widget);
6133 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
6135 gtk_widget_get_allocation (widget, &allocation);
6140 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
6143 gtk_widget_get_allocation (widget, &allocation);
6145 *x += allocation.width / 2;
6146 *y += allocation.height;
6148 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
6149 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
6153 open_link_activate_cb (GtkMenuItem *menu_item,
6158 link = gtk_label_get_current_link (label);
6161 emit_activate_link (label, link);
6165 copy_link_activate_cb (GtkMenuItem *menu_item,
6168 GtkClipboard *clipboard;
6171 uri = gtk_label_get_current_uri (label);
6174 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6175 gtk_clipboard_set_text (clipboard, uri, -1);
6180 gtk_label_popup_menu (GtkWidget *widget)
6182 gtk_label_do_popup (GTK_LABEL (widget), NULL);
6188 gtk_label_do_popup (GtkLabel *label,
6189 GdkEventButton *event)
6191 GtkLabelPrivate *priv = label->priv;
6192 GtkWidget *menuitem;
6195 gboolean have_selection;
6198 if (!priv->select_info)
6201 if (priv->select_info->popup_menu)
6202 gtk_widget_destroy (priv->select_info->popup_menu);
6204 priv->select_info->popup_menu = menu = gtk_menu_new ();
6206 gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (label), popup_menu_detach);
6209 priv->select_info->selection_anchor != priv->select_info->selection_end;
6213 if (priv->select_info->link_clicked)
6214 link = priv->select_info->active_link;
6219 link = gtk_label_get_focus_link (label);
6221 if (!have_selection && link)
6224 menuitem = gtk_image_menu_item_new_with_mnemonic (_("_Open Link"));
6225 gtk_widget_show (menuitem);
6226 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6228 g_signal_connect (G_OBJECT (menuitem), "activate",
6229 G_CALLBACK (open_link_activate_cb), label);
6231 image = gtk_image_new_from_stock (GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_MENU);
6232 gtk_widget_show (image);
6233 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6235 /* Copy Link Address */
6236 menuitem = gtk_image_menu_item_new_with_mnemonic (_("Copy _Link Address"));
6237 gtk_widget_show (menuitem);
6238 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6240 g_signal_connect (G_OBJECT (menuitem), "activate",
6241 G_CALLBACK (copy_link_activate_cb), label);
6243 image = gtk_image_new_from_stock (GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
6244 gtk_widget_show (image);
6245 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6249 append_action_signal (label, menu, GTK_STOCK_CUT, "cut-clipboard", FALSE);
6250 append_action_signal (label, menu, GTK_STOCK_COPY, "copy-clipboard", have_selection);
6251 append_action_signal (label, menu, GTK_STOCK_PASTE, "paste-clipboard", FALSE);
6253 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
6254 gtk_widget_set_sensitive (menuitem, FALSE);
6255 gtk_widget_show (menuitem);
6256 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6258 menuitem = gtk_separator_menu_item_new ();
6259 gtk_widget_show (menuitem);
6260 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6262 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
6263 g_signal_connect_swapped (menuitem, "activate",
6264 G_CALLBACK (gtk_label_select_all), label);
6265 gtk_widget_show (menuitem);
6266 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6269 g_signal_emit (label, signals[POPULATE_POPUP], 0, menu);
6272 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6274 event->button, event->time);
6277 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6278 popup_position_func, label,
6279 0, gtk_get_current_event_time ());
6280 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
6285 gtk_label_clear_links (GtkLabel *label)
6287 GtkLabelPrivate *priv = label->priv;
6289 if (!priv->select_info)
6292 g_list_free_full (priv->select_info->links, (GDestroyNotify) link_free);
6293 priv->select_info->links = NULL;
6294 priv->select_info->active_link = NULL;
6298 gtk_label_activate_link (GtkLabel *label,
6301 GtkWidget *widget = GTK_WIDGET (label);
6302 GError *error = NULL;
6304 if (!gtk_show_uri (gtk_widget_get_screen (widget),
6305 uri, gtk_get_current_event_time (), &error))
6307 g_warning ("Unable to show '%s': %s", uri, error->message);
6308 g_error_free (error);
6315 emit_activate_link (GtkLabel *label,
6318 GtkLabelPrivate *priv = label->priv;
6321 g_signal_emit (label, signals[ACTIVATE_LINK], 0, link->uri, &handled);
6322 if (handled && priv->track_links && !link->visited)
6324 link->visited = TRUE;
6325 /* FIXME: shouldn't have to redo everything here */
6326 gtk_label_clear_layout (label);
6331 gtk_label_activate_current_link (GtkLabel *label)
6334 GtkWidget *widget = GTK_WIDGET (label);
6336 link = gtk_label_get_focus_link (label);
6340 emit_activate_link (label, link);
6344 GtkWidget *toplevel;
6346 GtkWidget *default_widget, *focus_widget;
6348 toplevel = gtk_widget_get_toplevel (widget);
6349 if (GTK_IS_WINDOW (toplevel))
6351 window = GTK_WINDOW (toplevel);
6355 default_widget = gtk_window_get_default_widget (window);
6356 focus_widget = gtk_window_get_focus (window);
6358 if (default_widget != widget &&
6359 !(widget == focus_widget && (!default_widget || !gtk_widget_is_sensitive (default_widget))))
6360 gtk_window_activate_default (window);
6366 static GtkLabelLink *
6367 gtk_label_get_current_link (GtkLabel *label)
6369 GtkLabelPrivate *priv = label->priv;
6372 if (!priv->select_info)
6375 if (priv->select_info->link_clicked)
6376 link = priv->select_info->active_link;
6378 link = gtk_label_get_focus_link (label);
6384 * gtk_label_get_current_uri:
6385 * @label: a #GtkLabel
6387 * Returns the URI for the currently active link in the label.
6388 * The active link is the one under the mouse pointer or, in a
6389 * selectable label, the link in which the text cursor is currently
6392 * This function is intended for use in a #GtkLabel::activate-link handler
6393 * or for use in a #GtkWidget::query-tooltip handler.
6395 * Returns: the currently active URI. The string is owned by GTK+ and must
6396 * not be freed or modified.
6401 gtk_label_get_current_uri (GtkLabel *label)
6405 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
6407 link = gtk_label_get_current_link (label);
6416 * gtk_label_set_track_visited_links:
6417 * @label: a #GtkLabel
6418 * @track_links: %TRUE to track visited links
6420 * Sets whether the label should keep track of clicked
6421 * links (and use a different color for them).
6426 gtk_label_set_track_visited_links (GtkLabel *label,
6427 gboolean track_links)
6429 GtkLabelPrivate *priv;
6431 g_return_if_fail (GTK_IS_LABEL (label));
6435 track_links = track_links != FALSE;
6437 if (priv->track_links != track_links)
6439 priv->track_links = track_links;
6441 /* FIXME: shouldn't have to redo everything here */
6442 gtk_label_recalculate (label);
6444 g_object_notify (G_OBJECT (label), "track-visited-links");
6449 * gtk_label_get_track_visited_links:
6450 * @label: a #GtkLabel
6452 * Returns whether the label is currently keeping track
6455 * Returns: %TRUE if clicked links are remembered
6460 gtk_label_get_track_visited_links (GtkLabel *label)
6462 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
6464 return label->priv->track_links;
6468 gtk_label_query_tooltip (GtkWidget *widget,
6471 gboolean keyboard_tip,
6472 GtkTooltip *tooltip)
6474 GtkLabel *label = GTK_LABEL (widget);
6475 GtkLabelPrivate *priv = label->priv;
6476 GtkLabelSelectionInfo *info = priv->select_info;
6480 if (info && info->links)
6484 if (info->selection_anchor == info->selection_end)
6485 index = info->selection_anchor;
6489 if (!get_layout_index (label, x, y, &index))
6495 for (l = info->links; l != NULL; l = l->next)
6497 GtkLabelLink *link = l->data;
6498 if (index >= link->start && index <= link->end)
6502 gtk_tooltip_set_markup (tooltip, link->title);
6511 return GTK_WIDGET_CLASS (gtk_label_parent_class)->query_tooltip (widget,
6518 _gtk_label_get_cursor_position (GtkLabel *label)
6520 GtkLabelPrivate *priv = label->priv;
6522 if (priv->select_info && priv->select_info->selectable)
6523 return g_utf8_pointer_to_offset (priv->text,
6524 priv->text + priv->select_info->selection_end);
6530 _gtk_label_get_selection_bound (GtkLabel *label)
6532 GtkLabelPrivate *priv = label->priv;
6534 if (priv->select_info && priv->select_info->selectable)
6535 return g_utf8_pointer_to_offset (priv->text,
6536 priv->text + priv->select_info->selection_anchor);