1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
21 * file for a list of people on the GTK+ Team. See the ChangeLog
22 * files for a list of changes. These files are distributed with
23 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
32 #include "gtkaccellabel.h"
34 #include "gtkmarshalers.h"
36 #include "gtkwindow.h"
37 #include "gtkclipboard.h"
38 #include "gtkimagemenuitem.h"
40 #include "gtkseparatormenuitem.h"
41 #include "gtktextutil.h"
43 #include "gtkmenuitem.h"
44 #include "gtkmenushellprivate.h"
45 #include "gtknotebook.h"
47 #include "gtkbindings.h"
48 #include "gtkbuildable.h"
51 #include "gtktooltip.h"
52 #include "gtkprivate.h"
53 #include "gtktypebuiltins.h"
56 #include "a11y/gtklabelaccessible.h"
58 /* this is in case rint() is not provided by the compiler,
59 * such as in the case of C89 compilers, like MSVC
61 #include "fallback-c89.c"
65 * @Short_description: A widget that displays a small to medium amount of text
68 * The #GtkLabel widget displays a small amount of text. As the name
69 * implies, most labels are used to label another widget such as a
70 * #GtkButton, a #GtkMenuItem, or a #GtkOptionMenu.
72 * <refsect2 id="GtkLabel-BUILDER-UI">
73 * <title>GtkLabel as GtkBuildable</title>
75 * The GtkLabel implementation of the GtkBuildable interface supports a
76 * custom <attributes> element, which supports any number of <attribute>
77 * elements. the <attribute> element has attributes named name, value,
78 * start and end and allows you to specify #PangoAttribute values for this label.
81 * <title>A UI definition fragment specifying Pango attributes</title>
82 * <programlisting><![CDATA[
83 * <object class="GtkLabel">
85 * <attribute name="weight" value="PANGO_WEIGHT_BOLD"/>
86 * <attribute name="background" value="red" start="5" end="10"/>"
89 * ]]></programlisting>
91 * The start and end attributes specify the range of characters to which the
92 * Pango attribute applies. If start and end are not specified, the attribute is
93 * applied to the whole text. Note that specifying ranges does not make much
94 * sense with translatable attributes. Use markup embedded in the translatable
99 * <title>Mnemonics</title>
101 * Labels may contain <firstterm>mnemonics</firstterm>. Mnemonics are
102 * underlined characters in the label, used for keyboard navigation.
103 * Mnemonics are created by providing a string with an underscore before
104 * the mnemonic character, such as <literal>"_File"</literal>, to the
105 * functions gtk_label_new_with_mnemonic() or
106 * gtk_label_set_text_with_mnemonic().
108 * Mnemonics automatically activate any activatable widget the label is
109 * inside, such as a #GtkButton; if the label is not inside the
110 * mnemonic's target widget, you have to tell the label about the target
111 * using gtk_label_set_mnemonic_widget(). Here's a simple example where
112 * the label is inside a button:
116 * // Pressing Alt+H will activate this button
117 * button = gtk_button_new (<!-- -->);
118 * label = gtk_label_new_with_mnemonic ("_Hello");
119 * gtk_container_add (GTK_CONTAINER (button), label);
123 * There's a convenience function to create buttons with a mnemonic label
128 * // Pressing Alt+H will activate this button
129 * button = gtk_button_new_with_mnemonic ("_Hello");
133 * To create a mnemonic for a widget alongside the label, such as a
134 * #GtkEntry, you have to point the label at the entry with
135 * gtk_label_set_mnemonic_widget():
139 * // Pressing Alt+H will focus the entry
140 * entry = gtk_entry_new (<!-- -->);
141 * label = gtk_label_new_with_mnemonic ("_Hello");
142 * gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
148 * <title>Markup (styled text)</title>
150 * To make it easy to format text in a label (changing colors, fonts,
151 * etc.), label text can be provided in a simple <link
152 * linkend="PangoMarkupFormat">markup format</link>.
153 * Here's how to create a label with a small font:
157 * label = gtk_label_new (NULL);
158 * gtk_label_set_markup (GTK_LABEL (label), "<small>Small text</small>");
163 * linkend="PangoMarkupFormat">complete documentation</link> of available
164 * tags in the Pango manual.)
166 * The markup passed to gtk_label_set_markup() must be valid; for example,
167 * literal <, > and & characters must be escaped as \<,
168 * \gt;, and \&. If you pass text obtained from the user, file,
169 * or a network to gtk_label_set_markup(), you'll want to escape it with
170 * g_markup_escape_text() or g_markup_printf_escaped().
172 * Markup strings are just a convenient way to set the #PangoAttrList on
173 * a label; gtk_label_set_attributes() may be a simpler way to set
174 * attributes in some cases. Be careful though; #PangoAttrList tends to
175 * cause internationalization problems, unless you're applying attributes
176 * to the entire string (i.e. unless you set the range of each attribute
177 * to [0, %G_MAXINT)). The reason is that specifying the start_index and
178 * end_index for a #PangoAttribute requires knowledge of the exact string
179 * being displayed, so translations will cause problems.
183 * <title>Selectable labels</title>
184 * Labels can be made selectable with gtk_label_set_selectable().
185 * Selectable labels allow the user to copy the label contents to
186 * the clipboard. Only labels that contain useful-to-copy information
187 * — such as error messages — should be made selectable.
189 * <refsect2 id="label-text-layout">
190 * <title>Text layout</title>
192 * A label can contain any number of paragraphs, but will have
193 * performance problems if it contains more than a small number.
194 * Paragraphs are separated by newlines or other paragraph separators
195 * understood by Pango.
197 * Labels can automatically wrap text if you call
198 * gtk_label_set_line_wrap().
200 * gtk_label_set_justify() sets how the lines in a label align
201 * with one another. If you want to set how the label as a whole
202 * aligns in its available space, see gtk_misc_set_alignment().
204 * The #GtkLabel:width-chars and #GtkLabel:max-width-chars properties
205 * can be used to control the size allocation of ellipsized or wrapped
206 * labels. For ellipsizing labels, if either is specified (and less
207 * than the actual text size), it is used as the minimum width, and the actual
208 * text size is used as the natural width of the label. For wrapping labels,
209 * width-chars is used as the minimum width, if specified, and max-width-chars
210 * is used as the natural width. Even if max-width-chars specified, wrapping
211 * labels will be rewrapped to use all of the available width.
214 * Note that the interpretation of #GtkLabel:width-chars and
215 * #GtkLabel:max-width-chars has changed a bit with the introduction of
216 * <link linkend="geometry-management">width-for-height geometry management.</link>
221 * <title>Links</title>
223 * Since 2.18, GTK+ supports markup for clickable hyperlinks in addition
224 * to regular Pango markup. The markup for links is borrowed from HTML, using the
225 * <tag>a</tag> with href and title attributes. GTK+ renders links similar to the
226 * way they appear in web browsers, with colored, underlined text. The title
227 * attribute is displayed as a tooltip on the link. An example looks like this:
229 * <informalexample><programlisting>
230 * gtk_label_set_markup (label, "Go to the <a href="http://www.gtk.org" title="<i>Our</i> website">GTK+ website</a> for more...");
231 * </programlisting></informalexample>
233 * It is possible to implement custom handling for links and their tooltips with
234 * the #GtkLabel::activate-link signal and the gtk_label_get_current_uri() function.
239 struct _GtkLabelPrivate
241 GtkLabelSelectionInfo *select_info;
242 GtkWidget *mnemonic_widget;
243 GtkWindow *mnemonic_window;
245 PangoAttrList *attrs;
246 PangoAttrList *markup_attrs;
254 guint mnemonics_visible : 1;
257 guint use_underline : 1;
258 guint use_markup : 1;
260 guint single_line_mode : 1;
261 guint have_transform : 1;
264 guint pattern_set : 1;
265 guint track_links : 1;
267 guint mnemonic_keyval;
270 gint max_width_chars;
273 /* Notes about the handling of links:
275 * Links share the GtkLabelSelectionInfo struct with selectable labels.
276 * There are some new fields for links. The links field contains the list
277 * of GtkLabelLink structs that describe the links which are embedded in
278 * the label. The active_link field points to the link under the mouse
279 * pointer. For keyboard navigation, the 'focus' link is determined by
280 * finding the link which contains the selection_anchor position.
281 * The link_clicked field is used with button press and release events
282 * to ensure that pressing inside a link and releasing outside of it
283 * does not activate the link.
285 * Links are rendered with the link-color/visited-link-color colors
286 * that are determined by the style and with an underline. When the mouse
287 * pointer is over a link, the pointer is changed to indicate the link,
288 * and the background behind the link is rendered with the base[PRELIGHT]
289 * color. While a button is pressed over a link, the background is rendered
290 * with the base[ACTIVE] color.
292 * Labels with links accept keyboard focus, and it is possible to move
293 * the focus between the embedded links using Tab/Shift-Tab. The focus
294 * is indicated by a focus rectangle that is drawn around the link text.
295 * Pressing Enter activates the focussed link, and there is a suitable
296 * context menu for links that can be opened with the Menu key. Pressing
297 * Control-C copies the link URI to the clipboard.
299 * In selectable labels with links, link functionality is only available
300 * when the selection is empty.
305 gchar *title; /* the title attribute, used as tooltip */
306 gboolean visited; /* get set when the link is activated; this flag
307 * gets preserved over later set_markup() calls
309 gint start; /* position of the link in the PangoLayout */
313 struct _GtkLabelSelectionInfo
316 gint selection_anchor;
318 GtkWidget *popup_menu;
321 GtkLabelLink *active_link;
327 guint select_words : 1;
328 guint selectable : 1;
329 guint link_clicked : 1;
337 ACTIVATE_CURRENT_LINK,
352 PROP_MNEMONIC_KEYVAL,
353 PROP_MNEMONIC_WIDGET,
354 PROP_CURSOR_POSITION,
355 PROP_SELECTION_BOUND,
358 PROP_SINGLE_LINE_MODE,
360 PROP_MAX_WIDTH_CHARS,
361 PROP_TRACK_VISITED_LINKS
364 /* When rotating ellipsizable text we want the natural size to request
365 * more to ensure the label wont ever ellipsize in an allocation of full natural size.
367 #define ROTATION_ELLIPSIZE_PADDING 2
369 static guint signals[LAST_SIGNAL] = { 0 };
371 static const GdkColor default_link_color = { 0, 0, 0, 0xeeee };
372 static const GdkColor default_visited_link_color = { 0, 0x5555, 0x1a1a, 0x8b8b };
374 static void gtk_label_set_property (GObject *object,
378 static void gtk_label_get_property (GObject *object,
382 static void gtk_label_finalize (GObject *object);
383 static void gtk_label_destroy (GtkWidget *widget);
384 static void gtk_label_size_allocate (GtkWidget *widget,
385 GtkAllocation *allocation);
386 static void gtk_label_state_flags_changed (GtkWidget *widget,
387 GtkStateFlags prev_state);
388 static void gtk_label_style_updated (GtkWidget *widget);
389 static void gtk_label_direction_changed (GtkWidget *widget,
390 GtkTextDirection previous_dir);
391 static gint gtk_label_draw (GtkWidget *widget,
393 static gboolean gtk_label_focus (GtkWidget *widget,
394 GtkDirectionType direction);
396 static void gtk_label_realize (GtkWidget *widget);
397 static void gtk_label_unrealize (GtkWidget *widget);
398 static void gtk_label_map (GtkWidget *widget);
399 static void gtk_label_unmap (GtkWidget *widget);
401 static gboolean gtk_label_button_press (GtkWidget *widget,
402 GdkEventButton *event);
403 static gboolean gtk_label_button_release (GtkWidget *widget,
404 GdkEventButton *event);
405 static gboolean gtk_label_motion (GtkWidget *widget,
406 GdkEventMotion *event);
407 static gboolean gtk_label_leave_notify (GtkWidget *widget,
408 GdkEventCrossing *event);
410 static void gtk_label_grab_focus (GtkWidget *widget);
412 static gboolean gtk_label_query_tooltip (GtkWidget *widget,
415 gboolean keyboard_tip,
416 GtkTooltip *tooltip);
418 static void gtk_label_set_text_internal (GtkLabel *label,
420 static void gtk_label_set_label_internal (GtkLabel *label,
422 static void gtk_label_set_use_markup_internal (GtkLabel *label,
424 static void gtk_label_set_use_underline_internal (GtkLabel *label,
426 static void gtk_label_set_uline_text_internal (GtkLabel *label,
428 static void gtk_label_set_pattern_internal (GtkLabel *label,
429 const gchar *pattern,
430 gboolean is_mnemonic);
431 static void gtk_label_set_markup_internal (GtkLabel *label,
433 gboolean with_uline);
434 static void gtk_label_recalculate (GtkLabel *label);
435 static void gtk_label_hierarchy_changed (GtkWidget *widget,
436 GtkWidget *old_toplevel);
437 static void gtk_label_screen_changed (GtkWidget *widget,
438 GdkScreen *old_screen);
439 static gboolean gtk_label_popup_menu (GtkWidget *widget);
441 static void gtk_label_create_window (GtkLabel *label);
442 static void gtk_label_destroy_window (GtkLabel *label);
443 static void gtk_label_ensure_select_info (GtkLabel *label);
444 static void gtk_label_clear_select_info (GtkLabel *label);
445 static void gtk_label_update_cursor (GtkLabel *label);
446 static void gtk_label_clear_layout (GtkLabel *label);
447 static void gtk_label_ensure_layout (GtkLabel *label);
448 static void gtk_label_select_region_index (GtkLabel *label,
453 static gboolean gtk_label_mnemonic_activate (GtkWidget *widget,
454 gboolean group_cycling);
455 static void gtk_label_setup_mnemonic (GtkLabel *label,
457 static void gtk_label_drag_data_get (GtkWidget *widget,
458 GdkDragContext *context,
459 GtkSelectionData *selection_data,
463 static void gtk_label_buildable_interface_init (GtkBuildableIface *iface);
464 static gboolean gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
467 const gchar *tagname,
468 GMarkupParser *parser,
471 static void gtk_label_buildable_custom_finished (GtkBuildable *buildable,
474 const gchar *tagname,
478 static void connect_mnemonics_visible_notify (GtkLabel *label);
479 static gboolean separate_uline_pattern (const gchar *str,
485 /* For selectable labels: */
486 static void gtk_label_move_cursor (GtkLabel *label,
487 GtkMovementStep step,
489 gboolean extend_selection);
490 static void gtk_label_copy_clipboard (GtkLabel *label);
491 static void gtk_label_select_all (GtkLabel *label);
492 static void gtk_label_do_popup (GtkLabel *label,
493 GdkEventButton *event);
494 static gint gtk_label_move_forward_word (GtkLabel *label,
496 static gint gtk_label_move_backward_word (GtkLabel *label,
500 static void gtk_label_clear_links (GtkLabel *label);
501 static gboolean gtk_label_activate_link (GtkLabel *label,
503 static void gtk_label_activate_current_link (GtkLabel *label);
504 static GtkLabelLink *gtk_label_get_current_link (GtkLabel *label);
505 static void gtk_label_get_link_colors (GtkWidget *widget,
506 GdkColor *link_color,
507 GdkColor *visited_link_color);
508 static void emit_activate_link (GtkLabel *label,
511 static GtkSizeRequestMode gtk_label_get_request_mode (GtkWidget *widget);
512 static void gtk_label_get_preferred_width (GtkWidget *widget,
515 static void gtk_label_get_preferred_height (GtkWidget *widget,
518 static void gtk_label_get_preferred_width_for_height (GtkWidget *widget,
521 gint *natural_width);
522 static void gtk_label_get_preferred_height_for_width (GtkWidget *widget,
524 gint *minimum_height,
525 gint *natural_height);
527 static GtkBuildableIface *buildable_parent_iface = NULL;
529 G_DEFINE_TYPE_WITH_CODE (GtkLabel, gtk_label, GTK_TYPE_MISC,
530 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
531 gtk_label_buildable_interface_init))
534 add_move_binding (GtkBindingSet *binding_set,
537 GtkMovementStep step,
540 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
542 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
546 G_TYPE_BOOLEAN, FALSE);
548 /* Selection-extending version */
549 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
553 G_TYPE_BOOLEAN, TRUE);
557 gtk_label_class_init (GtkLabelClass *class)
559 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
560 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
561 GtkBindingSet *binding_set;
563 gobject_class->set_property = gtk_label_set_property;
564 gobject_class->get_property = gtk_label_get_property;
565 gobject_class->finalize = gtk_label_finalize;
567 widget_class->destroy = gtk_label_destroy;
568 widget_class->size_allocate = gtk_label_size_allocate;
569 widget_class->state_flags_changed = gtk_label_state_flags_changed;
570 widget_class->style_updated = gtk_label_style_updated;
571 widget_class->query_tooltip = gtk_label_query_tooltip;
572 widget_class->direction_changed = gtk_label_direction_changed;
573 widget_class->draw = gtk_label_draw;
574 widget_class->realize = gtk_label_realize;
575 widget_class->unrealize = gtk_label_unrealize;
576 widget_class->map = gtk_label_map;
577 widget_class->unmap = gtk_label_unmap;
578 widget_class->button_press_event = gtk_label_button_press;
579 widget_class->button_release_event = gtk_label_button_release;
580 widget_class->motion_notify_event = gtk_label_motion;
581 widget_class->leave_notify_event = gtk_label_leave_notify;
582 widget_class->hierarchy_changed = gtk_label_hierarchy_changed;
583 widget_class->screen_changed = gtk_label_screen_changed;
584 widget_class->mnemonic_activate = gtk_label_mnemonic_activate;
585 widget_class->drag_data_get = gtk_label_drag_data_get;
586 widget_class->grab_focus = gtk_label_grab_focus;
587 widget_class->popup_menu = gtk_label_popup_menu;
588 widget_class->focus = gtk_label_focus;
589 widget_class->get_request_mode = gtk_label_get_request_mode;
590 widget_class->get_preferred_width = gtk_label_get_preferred_width;
591 widget_class->get_preferred_height = gtk_label_get_preferred_height;
592 widget_class->get_preferred_width_for_height = gtk_label_get_preferred_width_for_height;
593 widget_class->get_preferred_height_for_width = gtk_label_get_preferred_height_for_width;
595 class->move_cursor = gtk_label_move_cursor;
596 class->copy_clipboard = gtk_label_copy_clipboard;
597 class->activate_link = gtk_label_activate_link;
600 * GtkLabel::move-cursor:
601 * @entry: the object which received the signal
602 * @step: the granularity of the move, as a #GtkMovementStep
603 * @count: the number of @step units to move
604 * @extend_selection: %TRUE if the move should extend the selection
606 * The ::move-cursor signal is a
607 * <link linkend="keybinding-signals">keybinding signal</link>
608 * which gets emitted when the user initiates a cursor movement.
609 * If the cursor is not visible in @entry, this signal causes
610 * the viewport to be moved instead.
612 * Applications should not connect to it, but may emit it with
613 * g_signal_emit_by_name() if they need to control the cursor
616 * The default bindings for this signal come in two variants,
617 * the variant with the Shift modifier extends the selection,
618 * the variant without the Shift modifer does not.
619 * There are too many key combinations to list them all here.
621 * <listitem>Arrow keys move by individual characters/lines</listitem>
622 * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
623 * <listitem>Home/End keys move to the ends of the buffer</listitem>
626 signals[MOVE_CURSOR] =
627 g_signal_new (I_("move-cursor"),
628 G_OBJECT_CLASS_TYPE (gobject_class),
629 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
630 G_STRUCT_OFFSET (GtkLabelClass, move_cursor),
632 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
634 GTK_TYPE_MOVEMENT_STEP,
639 * GtkLabel::copy-clipboard:
640 * @label: the object which received the signal
642 * The ::copy-clipboard signal is a
643 * <link linkend="keybinding-signals">keybinding signal</link>
644 * which gets emitted to copy the selection to the clipboard.
646 * The default binding for this signal is Ctrl-c.
648 signals[COPY_CLIPBOARD] =
649 g_signal_new (I_("copy-clipboard"),
650 G_OBJECT_CLASS_TYPE (gobject_class),
651 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
652 G_STRUCT_OFFSET (GtkLabelClass, copy_clipboard),
654 _gtk_marshal_VOID__VOID,
658 * GtkLabel::populate-popup:
659 * @label: The label on which the signal is emitted
660 * @menu: the menu that is being populated
662 * The ::populate-popup signal gets emitted before showing the
663 * context menu of the label. Note that only selectable labels
664 * have context menus.
666 * If you need to add items to the context menu, connect
667 * to this signal and append your menuitems to the @menu.
669 signals[POPULATE_POPUP] =
670 g_signal_new (I_("populate-popup"),
671 G_OBJECT_CLASS_TYPE (gobject_class),
673 G_STRUCT_OFFSET (GtkLabelClass, populate_popup),
675 _gtk_marshal_VOID__OBJECT,
680 * GtkLabel::activate-current-link:
681 * @label: The label on which the signal was emitted
683 * A <link linkend="keybinding-signals">keybinding signal</link>
684 * which gets emitted when the user activates a link in the label.
686 * Applications may also emit the signal with g_signal_emit_by_name()
687 * if they need to control activation of URIs programmatically.
689 * The default bindings for this signal are all forms of the Enter key.
693 signals[ACTIVATE_CURRENT_LINK] =
694 g_signal_new_class_handler ("activate-current-link",
695 G_TYPE_FROM_CLASS (gobject_class),
696 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
697 G_CALLBACK (gtk_label_activate_current_link),
699 _gtk_marshal_VOID__VOID,
703 * GtkLabel::activate-link:
704 * @label: The label on which the signal was emitted
705 * @uri: the URI that is activated
707 * The signal which gets emitted to activate a URI.
708 * Applications may connect to it to override the default behaviour,
709 * which is to call gtk_show_uri().
711 * Returns: %TRUE if the link has been activated
715 signals[ACTIVATE_LINK] =
716 g_signal_new ("activate-link",
717 G_TYPE_FROM_CLASS (gobject_class),
719 G_STRUCT_OFFSET (GtkLabelClass, activate_link),
720 _gtk_boolean_handled_accumulator, NULL,
721 _gtk_marshal_BOOLEAN__STRING,
722 G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
724 g_object_class_install_property (gobject_class,
726 g_param_spec_string ("label",
728 P_("The text of the label"),
730 GTK_PARAM_READWRITE));
731 g_object_class_install_property (gobject_class,
733 g_param_spec_boxed ("attributes",
735 P_("A list of style attributes to apply to the text of the label"),
736 PANGO_TYPE_ATTR_LIST,
737 GTK_PARAM_READWRITE));
738 g_object_class_install_property (gobject_class,
740 g_param_spec_boolean ("use-markup",
742 P_("The text of the label includes XML markup. See pango_parse_markup()"),
744 GTK_PARAM_READWRITE));
745 g_object_class_install_property (gobject_class,
747 g_param_spec_boolean ("use-underline",
749 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
751 GTK_PARAM_READWRITE));
753 g_object_class_install_property (gobject_class,
755 g_param_spec_enum ("justify",
757 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"),
758 GTK_TYPE_JUSTIFICATION,
760 GTK_PARAM_READWRITE));
762 g_object_class_install_property (gobject_class,
764 g_param_spec_string ("pattern",
766 P_("A string with _ characters in positions correspond to characters in the text to underline"),
768 GTK_PARAM_WRITABLE));
770 g_object_class_install_property (gobject_class,
772 g_param_spec_boolean ("wrap",
774 P_("If set, wrap lines if the text becomes too wide"),
776 GTK_PARAM_READWRITE));
778 * GtkLabel:wrap-mode:
780 * If line wrapping is on (see the #GtkLabel:wrap property) this controls
781 * how the line wrapping is done. The default is %PANGO_WRAP_WORD, which
782 * means wrap on word boundaries.
786 g_object_class_install_property (gobject_class,
788 g_param_spec_enum ("wrap-mode",
789 P_("Line wrap mode"),
790 P_("If wrap is set, controls how linewrapping is done"),
791 PANGO_TYPE_WRAP_MODE,
793 GTK_PARAM_READWRITE));
794 g_object_class_install_property (gobject_class,
796 g_param_spec_boolean ("selectable",
798 P_("Whether the label text can be selected with the mouse"),
800 GTK_PARAM_READWRITE));
801 g_object_class_install_property (gobject_class,
802 PROP_MNEMONIC_KEYVAL,
803 g_param_spec_uint ("mnemonic-keyval",
805 P_("The mnemonic accelerator key for this label"),
809 GTK_PARAM_READABLE));
810 g_object_class_install_property (gobject_class,
811 PROP_MNEMONIC_WIDGET,
812 g_param_spec_object ("mnemonic-widget",
813 P_("Mnemonic widget"),
814 P_("The widget to be activated when the label's mnemonic "
817 GTK_PARAM_READWRITE));
819 g_object_class_install_property (gobject_class,
820 PROP_CURSOR_POSITION,
821 g_param_spec_int ("cursor-position",
822 P_("Cursor Position"),
823 P_("The current position of the insertion cursor in chars"),
827 GTK_PARAM_READABLE));
829 g_object_class_install_property (gobject_class,
830 PROP_SELECTION_BOUND,
831 g_param_spec_int ("selection-bound",
832 P_("Selection Bound"),
833 P_("The position of the opposite end of the selection from the cursor in chars"),
837 GTK_PARAM_READABLE));
840 * GtkLabel:ellipsize:
842 * The preferred place to ellipsize the string, if the label does
843 * not have enough room to display the entire string, specified as a
844 * #PangoEllisizeMode.
846 * Note that setting this property to a value other than
847 * %PANGO_ELLIPSIZE_NONE has the side-effect that the label requests
848 * only enough space to display the ellipsis "...". In particular, this
849 * means that ellipsizing labels do not work well in notebook tabs, unless
850 * the tab's #GtkNotebook:tab-expand property is set to %TRUE. Other ways
851 * to set a label's width are gtk_widget_set_size_request() and
852 * gtk_label_set_width_chars().
856 g_object_class_install_property (gobject_class,
858 g_param_spec_enum ("ellipsize",
860 P_("The preferred place to ellipsize the string, if the label does not have enough room to display the entire string"),
861 PANGO_TYPE_ELLIPSIZE_MODE,
862 PANGO_ELLIPSIZE_NONE,
863 GTK_PARAM_READWRITE));
866 * GtkLabel:width-chars:
868 * The desired width of the label, in characters. If this property is set to
869 * -1, the width will be calculated automatically.
871 * See the section on <link linkend="label-text-layout">text layout</link>
872 * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
873 * determine the width of ellipsized and wrapped labels.
877 g_object_class_install_property (gobject_class,
879 g_param_spec_int ("width-chars",
880 P_("Width In Characters"),
881 P_("The desired width of the label, in characters"),
885 GTK_PARAM_READWRITE));
888 * GtkLabel:single-line-mode:
890 * Whether the label is in single line mode. In single line mode,
891 * the height of the label does not depend on the actual text, it
892 * is always set to ascent + descent of the font. This can be an
893 * advantage in situations where resizing the label because of text
894 * changes would be distracting, e.g. in a statusbar.
898 g_object_class_install_property (gobject_class,
899 PROP_SINGLE_LINE_MODE,
900 g_param_spec_boolean ("single-line-mode",
901 P_("Single Line Mode"),
902 P_("Whether the label is in single line mode"),
904 GTK_PARAM_READWRITE));
909 * The angle that the baseline of the label makes with the horizontal,
910 * in degrees, measured counterclockwise. An angle of 90 reads from
911 * from bottom to top, an angle of 270, from top to bottom. Ignored
912 * if the label is selectable, wrapped, or ellipsized.
916 g_object_class_install_property (gobject_class,
918 g_param_spec_double ("angle",
920 P_("Angle at which the label is rotated"),
924 GTK_PARAM_READWRITE));
927 * GtkLabel:max-width-chars:
929 * The desired maximum width of the label, in characters. If this property
930 * is set to -1, the width will be calculated automatically.
932 * See the section on <link linkend="label-text-layout">text layout</link>
933 * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
934 * determine the width of ellipsized and wrapped labels.
938 g_object_class_install_property (gobject_class,
939 PROP_MAX_WIDTH_CHARS,
940 g_param_spec_int ("max-width-chars",
941 P_("Maximum Width In Characters"),
942 P_("The desired maximum width of the label, in characters"),
946 GTK_PARAM_READWRITE));
949 * GtkLabel:track-visited-links:
951 * Set this property to %TRUE to make the label track which links
952 * have been clicked. It will then apply the ::visited-link-color
953 * color, instead of ::link-color.
957 g_object_class_install_property (gobject_class,
958 PROP_TRACK_VISITED_LINKS,
959 g_param_spec_boolean ("track-visited-links",
960 P_("Track visited links"),
961 P_("Whether visited links should be tracked"),
963 GTK_PARAM_READWRITE));
968 binding_set = gtk_binding_set_by_class (class);
970 /* Moving the insertion point */
971 add_move_binding (binding_set, GDK_KEY_Right, 0,
972 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
974 add_move_binding (binding_set, GDK_KEY_Left, 0,
975 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
977 add_move_binding (binding_set, GDK_KEY_KP_Right, 0,
978 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
980 add_move_binding (binding_set, GDK_KEY_KP_Left, 0,
981 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
983 add_move_binding (binding_set, GDK_KEY_f, GDK_CONTROL_MASK,
984 GTK_MOVEMENT_LOGICAL_POSITIONS, 1);
986 add_move_binding (binding_set, GDK_KEY_b, GDK_CONTROL_MASK,
987 GTK_MOVEMENT_LOGICAL_POSITIONS, -1);
989 add_move_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK,
990 GTK_MOVEMENT_WORDS, 1);
992 add_move_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK,
993 GTK_MOVEMENT_WORDS, -1);
995 add_move_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK,
996 GTK_MOVEMENT_WORDS, 1);
998 add_move_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK,
999 GTK_MOVEMENT_WORDS, -1);
1002 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
1004 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1006 G_TYPE_BOOLEAN, FALSE);
1008 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
1010 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1012 G_TYPE_BOOLEAN, TRUE);
1014 gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
1016 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1018 G_TYPE_BOOLEAN, FALSE);
1020 gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
1022 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1024 G_TYPE_BOOLEAN, TRUE);
1027 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1029 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1031 G_TYPE_BOOLEAN, FALSE);
1033 gtk_binding_entry_add_signal (binding_set, GDK_KEY_backslash, GDK_CONTROL_MASK,
1035 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1037 G_TYPE_BOOLEAN, FALSE);
1039 add_move_binding (binding_set, GDK_KEY_f, GDK_MOD1_MASK,
1040 GTK_MOVEMENT_WORDS, 1);
1042 add_move_binding (binding_set, GDK_KEY_b, GDK_MOD1_MASK,
1043 GTK_MOVEMENT_WORDS, -1);
1045 add_move_binding (binding_set, GDK_KEY_Home, 0,
1046 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1048 add_move_binding (binding_set, GDK_KEY_End, 0,
1049 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1051 add_move_binding (binding_set, GDK_KEY_KP_Home, 0,
1052 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1054 add_move_binding (binding_set, GDK_KEY_KP_End, 0,
1055 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1057 add_move_binding (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK,
1058 GTK_MOVEMENT_BUFFER_ENDS, -1);
1060 add_move_binding (binding_set, GDK_KEY_End, GDK_CONTROL_MASK,
1061 GTK_MOVEMENT_BUFFER_ENDS, 1);
1063 add_move_binding (binding_set, GDK_KEY_KP_Home, GDK_CONTROL_MASK,
1064 GTK_MOVEMENT_BUFFER_ENDS, -1);
1066 add_move_binding (binding_set, GDK_KEY_KP_End, GDK_CONTROL_MASK,
1067 GTK_MOVEMENT_BUFFER_ENDS, 1);
1070 gtk_binding_entry_add_signal (binding_set, GDK_KEY_c, GDK_CONTROL_MASK,
1071 "copy-clipboard", 0);
1073 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Return, 0,
1074 "activate-current-link", 0);
1075 gtk_binding_entry_add_signal (binding_set, GDK_KEY_ISO_Enter, 0,
1076 "activate-current-link", 0);
1077 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Enter, 0,
1078 "activate-current-link", 0);
1080 g_type_class_add_private (class, sizeof (GtkLabelPrivate));
1082 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_LABEL_ACCESSIBLE);
1086 gtk_label_set_property (GObject *object,
1088 const GValue *value,
1091 GtkLabel *label = GTK_LABEL (object);
1096 gtk_label_set_label (label, g_value_get_string (value));
1098 case PROP_ATTRIBUTES:
1099 gtk_label_set_attributes (label, g_value_get_boxed (value));
1101 case PROP_USE_MARKUP:
1102 gtk_label_set_use_markup (label, g_value_get_boolean (value));
1104 case PROP_USE_UNDERLINE:
1105 gtk_label_set_use_underline (label, g_value_get_boolean (value));
1108 gtk_label_set_justify (label, g_value_get_enum (value));
1111 gtk_label_set_pattern (label, g_value_get_string (value));
1114 gtk_label_set_line_wrap (label, g_value_get_boolean (value));
1116 case PROP_WRAP_MODE:
1117 gtk_label_set_line_wrap_mode (label, g_value_get_enum (value));
1119 case PROP_SELECTABLE:
1120 gtk_label_set_selectable (label, g_value_get_boolean (value));
1122 case PROP_MNEMONIC_WIDGET:
1123 gtk_label_set_mnemonic_widget (label, (GtkWidget*) g_value_get_object (value));
1125 case PROP_ELLIPSIZE:
1126 gtk_label_set_ellipsize (label, g_value_get_enum (value));
1128 case PROP_WIDTH_CHARS:
1129 gtk_label_set_width_chars (label, g_value_get_int (value));
1131 case PROP_SINGLE_LINE_MODE:
1132 gtk_label_set_single_line_mode (label, g_value_get_boolean (value));
1135 gtk_label_set_angle (label, g_value_get_double (value));
1137 case PROP_MAX_WIDTH_CHARS:
1138 gtk_label_set_max_width_chars (label, g_value_get_int (value));
1140 case PROP_TRACK_VISITED_LINKS:
1141 gtk_label_set_track_visited_links (label, g_value_get_boolean (value));
1144 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1150 gtk_label_get_property (GObject *object,
1155 GtkLabel *label = GTK_LABEL (object);
1156 GtkLabelPrivate *priv = label->priv;
1161 g_value_set_string (value, priv->label);
1163 case PROP_ATTRIBUTES:
1164 g_value_set_boxed (value, priv->attrs);
1166 case PROP_USE_MARKUP:
1167 g_value_set_boolean (value, priv->use_markup);
1169 case PROP_USE_UNDERLINE:
1170 g_value_set_boolean (value, priv->use_underline);
1173 g_value_set_enum (value, priv->jtype);
1176 g_value_set_boolean (value, priv->wrap);
1178 case PROP_WRAP_MODE:
1179 g_value_set_enum (value, priv->wrap_mode);
1181 case PROP_SELECTABLE:
1182 g_value_set_boolean (value, gtk_label_get_selectable (label));
1184 case PROP_MNEMONIC_KEYVAL:
1185 g_value_set_uint (value, priv->mnemonic_keyval);
1187 case PROP_MNEMONIC_WIDGET:
1188 g_value_set_object (value, (GObject*) priv->mnemonic_widget);
1190 case PROP_CURSOR_POSITION:
1191 g_value_set_int (value, _gtk_label_get_cursor_position (label));
1193 case PROP_SELECTION_BOUND:
1194 g_value_set_int (value, _gtk_label_get_selection_bound (label));
1196 case PROP_ELLIPSIZE:
1197 g_value_set_enum (value, priv->ellipsize);
1199 case PROP_WIDTH_CHARS:
1200 g_value_set_int (value, gtk_label_get_width_chars (label));
1202 case PROP_SINGLE_LINE_MODE:
1203 g_value_set_boolean (value, gtk_label_get_single_line_mode (label));
1206 g_value_set_double (value, gtk_label_get_angle (label));
1208 case PROP_MAX_WIDTH_CHARS:
1209 g_value_set_int (value, gtk_label_get_max_width_chars (label));
1211 case PROP_TRACK_VISITED_LINKS:
1212 g_value_set_boolean (value, gtk_label_get_track_visited_links (label));
1215 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1221 gtk_label_init (GtkLabel *label)
1223 GtkLabelPrivate *priv;
1225 label->priv = G_TYPE_INSTANCE_GET_PRIVATE (label,
1230 gtk_widget_set_has_window (GTK_WIDGET (label), FALSE);
1232 priv->width_chars = -1;
1233 priv->max_width_chars = -1;
1236 priv->jtype = GTK_JUSTIFY_LEFT;
1238 priv->wrap_mode = PANGO_WRAP_WORD;
1239 priv->ellipsize = PANGO_ELLIPSIZE_NONE;
1241 priv->use_underline = FALSE;
1242 priv->use_markup = FALSE;
1243 priv->pattern_set = FALSE;
1244 priv->track_links = TRUE;
1246 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
1247 priv->layout = NULL;
1251 priv->mnemonic_widget = NULL;
1252 priv->mnemonic_window = NULL;
1254 priv->mnemonics_visible = TRUE;
1256 gtk_label_set_text (label, "");
1261 gtk_label_buildable_interface_init (GtkBuildableIface *iface)
1263 buildable_parent_iface = g_type_interface_peek_parent (iface);
1265 iface->custom_tag_start = gtk_label_buildable_custom_tag_start;
1266 iface->custom_finished = gtk_label_buildable_custom_finished;
1270 GtkBuilder *builder;
1272 PangoAttrList *attrs;
1275 static PangoAttribute *
1276 attribute_from_text (GtkBuilder *builder,
1281 PangoAttribute *attribute = NULL;
1283 PangoLanguage *language;
1284 PangoFontDescription *font_desc;
1286 GValue val = G_VALUE_INIT;
1288 if (!gtk_builder_value_from_string_type (builder, PANGO_TYPE_ATTR_TYPE, name, &val, error))
1291 type = g_value_get_enum (&val);
1292 g_value_unset (&val);
1296 /* PangoAttrLanguage */
1297 case PANGO_ATTR_LANGUAGE:
1298 if ((language = pango_language_from_string (value)))
1300 attribute = pango_attr_language_new (language);
1301 g_value_init (&val, G_TYPE_INT);
1305 case PANGO_ATTR_STYLE:
1306 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STYLE, value, &val, error))
1307 attribute = pango_attr_style_new (g_value_get_enum (&val));
1309 case PANGO_ATTR_WEIGHT:
1310 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_WEIGHT, value, &val, error))
1311 attribute = pango_attr_weight_new (g_value_get_enum (&val));
1313 case PANGO_ATTR_VARIANT:
1314 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_VARIANT, value, &val, error))
1315 attribute = pango_attr_variant_new (g_value_get_enum (&val));
1317 case PANGO_ATTR_STRETCH:
1318 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STRETCH, value, &val, error))
1319 attribute = pango_attr_stretch_new (g_value_get_enum (&val));
1321 case PANGO_ATTR_UNDERLINE:
1322 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_UNDERLINE, value, &val, NULL))
1323 attribute = pango_attr_underline_new (g_value_get_enum (&val));
1326 /* XXX: allow boolean for backwards compat, so ignore error */
1327 /* Deprecate this somehow */
1328 g_value_unset (&val);
1329 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1330 attribute = pango_attr_underline_new (g_value_get_boolean (&val));
1333 case PANGO_ATTR_STRIKETHROUGH:
1334 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1335 attribute = pango_attr_strikethrough_new (g_value_get_boolean (&val));
1337 case PANGO_ATTR_GRAVITY:
1338 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY, value, &val, error))
1339 attribute = pango_attr_gravity_new (g_value_get_enum (&val));
1341 case PANGO_ATTR_GRAVITY_HINT:
1342 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY_HINT,
1343 value, &val, error))
1344 attribute = pango_attr_gravity_hint_new (g_value_get_enum (&val));
1346 /* PangoAttrString */
1347 case PANGO_ATTR_FAMILY:
1348 attribute = pango_attr_family_new (value);
1349 g_value_init (&val, G_TYPE_INT);
1353 case PANGO_ATTR_SIZE:
1354 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1355 value, &val, error))
1356 attribute = pango_attr_size_new (g_value_get_int (&val));
1358 case PANGO_ATTR_ABSOLUTE_SIZE:
1359 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1360 value, &val, error))
1361 attribute = pango_attr_size_new_absolute (g_value_get_int (&val));
1364 /* PangoAttrFontDesc */
1365 case PANGO_ATTR_FONT_DESC:
1366 if ((font_desc = pango_font_description_from_string (value)))
1368 attribute = pango_attr_font_desc_new (font_desc);
1369 pango_font_description_free (font_desc);
1370 g_value_init (&val, G_TYPE_INT);
1374 /* PangoAttrColor */
1375 case PANGO_ATTR_FOREGROUND:
1376 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1377 value, &val, error))
1379 color = g_value_get_boxed (&val);
1380 attribute = pango_attr_foreground_new (color->red, color->green, color->blue);
1383 case PANGO_ATTR_BACKGROUND:
1384 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1385 value, &val, error))
1387 color = g_value_get_boxed (&val);
1388 attribute = pango_attr_background_new (color->red, color->green, color->blue);
1391 case PANGO_ATTR_UNDERLINE_COLOR:
1392 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1393 value, &val, error))
1395 color = g_value_get_boxed (&val);
1396 attribute = pango_attr_underline_color_new (color->red, color->green, color->blue);
1399 case PANGO_ATTR_STRIKETHROUGH_COLOR:
1400 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1401 value, &val, error))
1403 color = g_value_get_boxed (&val);
1404 attribute = pango_attr_strikethrough_color_new (color->red, color->green, color->blue);
1408 /* PangoAttrShape */
1409 case PANGO_ATTR_SHAPE:
1410 /* Unsupported for now */
1412 /* PangoAttrFloat */
1413 case PANGO_ATTR_SCALE:
1414 if (gtk_builder_value_from_string_type (builder, G_TYPE_DOUBLE,
1415 value, &val, error))
1416 attribute = pango_attr_scale_new (g_value_get_double (&val));
1419 case PANGO_ATTR_INVALID:
1420 case PANGO_ATTR_LETTER_SPACING:
1421 case PANGO_ATTR_RISE:
1422 case PANGO_ATTR_FALLBACK:
1427 g_value_unset (&val);
1434 pango_start_element (GMarkupParseContext *context,
1435 const gchar *element_name,
1436 const gchar **names,
1437 const gchar **values,
1441 PangoParserData *data = (PangoParserData*)user_data;
1442 GValue val = G_VALUE_INIT;
1444 gint line_number, char_number;
1446 if (strcmp (element_name, "attribute") == 0)
1448 PangoAttribute *attr = NULL;
1449 const gchar *name = NULL;
1450 const gchar *value = NULL;
1451 const gchar *start = NULL;
1452 const gchar *end = NULL;
1453 guint start_val = 0;
1454 guint end_val = G_MAXUINT;
1456 for (i = 0; names[i]; i++)
1458 if (strcmp (names[i], "name") == 0)
1460 else if (strcmp (names[i], "value") == 0)
1462 else if (strcmp (names[i], "start") == 0)
1464 else if (strcmp (names[i], "end") == 0)
1468 g_markup_parse_context_get_position (context,
1473 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1474 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1476 line_number, char_number, names[i], "attribute");
1481 if (!name || !value)
1483 g_markup_parse_context_get_position (context,
1488 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1489 "%s:%d:%d <%s> requires attribute \"%s\"",
1491 line_number, char_number, "attribute",
1492 name ? "value" : "name");
1498 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1499 start, &val, error))
1501 start_val = g_value_get_uint (&val);
1502 g_value_unset (&val);
1507 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1510 end_val = g_value_get_uint (&val);
1511 g_value_unset (&val);
1514 attr = attribute_from_text (data->builder, name, value, error);
1518 attr->start_index = start_val;
1519 attr->end_index = end_val;
1522 data->attrs = pango_attr_list_new ();
1524 pango_attr_list_insert (data->attrs, attr);
1527 else if (strcmp (element_name, "attributes") == 0)
1530 g_warning ("Unsupported tag for GtkLabel: %s\n", element_name);
1533 static const GMarkupParser pango_parser =
1535 pango_start_element,
1539 gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
1540 GtkBuilder *builder,
1542 const gchar *tagname,
1543 GMarkupParser *parser,
1546 if (buildable_parent_iface->custom_tag_start (buildable, builder, child,
1547 tagname, parser, data))
1550 if (strcmp (tagname, "attributes") == 0)
1552 PangoParserData *parser_data;
1554 parser_data = g_slice_new0 (PangoParserData);
1555 parser_data->builder = g_object_ref (builder);
1556 parser_data->object = g_object_ref (buildable);
1557 *parser = pango_parser;
1558 *data = parser_data;
1565 gtk_label_buildable_custom_finished (GtkBuildable *buildable,
1566 GtkBuilder *builder,
1568 const gchar *tagname,
1571 PangoParserData *data;
1573 buildable_parent_iface->custom_finished (buildable, builder, child,
1574 tagname, user_data);
1576 if (strcmp (tagname, "attributes") == 0)
1578 data = (PangoParserData*)user_data;
1582 gtk_label_set_attributes (GTK_LABEL (buildable), data->attrs);
1583 pango_attr_list_unref (data->attrs);
1586 g_object_unref (data->object);
1587 g_object_unref (data->builder);
1588 g_slice_free (PangoParserData, data);
1595 * @str: The text of the label
1597 * Creates a new label with the given text inside it. You can
1598 * pass %NULL to get an empty label widget.
1600 * Return value: the new #GtkLabel
1603 gtk_label_new (const gchar *str)
1607 label = g_object_new (GTK_TYPE_LABEL, NULL);
1610 gtk_label_set_text (label, str);
1612 return GTK_WIDGET (label);
1616 * gtk_label_new_with_mnemonic:
1617 * @str: The text of the label, with an underscore in front of the
1618 * mnemonic character
1620 * Creates a new #GtkLabel, containing the text in @str.
1622 * If characters in @str are preceded by an underscore, they are
1623 * underlined. If you need a literal underscore character in a label, use
1624 * '__' (two underscores). The first underlined character represents a
1625 * keyboard accelerator called a mnemonic. The mnemonic key can be used
1626 * to activate another widget, chosen automatically, or explicitly using
1627 * gtk_label_set_mnemonic_widget().
1629 * If gtk_label_set_mnemonic_widget() is not called, then the first
1630 * activatable ancestor of the #GtkLabel will be chosen as the mnemonic
1631 * widget. For instance, if the label is inside a button or menu item,
1632 * the button or menu item will automatically become the mnemonic widget
1633 * and be activated by the mnemonic.
1635 * Return value: the new #GtkLabel
1638 gtk_label_new_with_mnemonic (const gchar *str)
1642 label = g_object_new (GTK_TYPE_LABEL, NULL);
1645 gtk_label_set_text_with_mnemonic (label, str);
1647 return GTK_WIDGET (label);
1651 gtk_label_mnemonic_activate (GtkWidget *widget,
1652 gboolean group_cycling)
1654 GtkLabel *label = GTK_LABEL (widget);
1655 GtkLabelPrivate *priv = label->priv;
1658 if (priv->mnemonic_widget)
1659 return gtk_widget_mnemonic_activate (priv->mnemonic_widget, group_cycling);
1661 /* Try to find the widget to activate by traversing the
1662 * widget's ancestry.
1664 parent = gtk_widget_get_parent (widget);
1666 if (GTK_IS_NOTEBOOK (parent))
1671 if (gtk_widget_get_can_focus (parent) ||
1672 (!group_cycling && GTK_WIDGET_GET_CLASS (parent)->activate_signal) ||
1673 GTK_IS_NOTEBOOK (gtk_widget_get_parent (parent)) ||
1674 GTK_IS_MENU_ITEM (parent))
1675 return gtk_widget_mnemonic_activate (parent, group_cycling);
1676 parent = gtk_widget_get_parent (parent);
1679 /* barf if there was nothing to activate */
1680 g_warning ("Couldn't find a target for a mnemonic activation.");
1681 gtk_widget_error_bell (widget);
1687 gtk_label_setup_mnemonic (GtkLabel *label,
1690 GtkLabelPrivate *priv = label->priv;
1691 GtkWidget *widget = GTK_WIDGET (label);
1692 GtkWidget *toplevel;
1693 GtkWidget *mnemonic_menu;
1695 mnemonic_menu = g_object_get_data (G_OBJECT (label), "gtk-mnemonic-menu");
1697 if (last_key != GDK_KEY_VoidSymbol)
1699 if (priv->mnemonic_window)
1701 gtk_window_remove_mnemonic (priv->mnemonic_window,
1704 priv->mnemonic_window = NULL;
1708 _gtk_menu_shell_remove_mnemonic (GTK_MENU_SHELL (mnemonic_menu),
1711 mnemonic_menu = NULL;
1715 if (priv->mnemonic_keyval == GDK_KEY_VoidSymbol)
1718 connect_mnemonics_visible_notify (GTK_LABEL (widget));
1720 toplevel = gtk_widget_get_toplevel (widget);
1721 if (gtk_widget_is_toplevel (toplevel))
1723 GtkWidget *menu_shell;
1725 menu_shell = gtk_widget_get_ancestor (widget,
1726 GTK_TYPE_MENU_SHELL);
1730 _gtk_menu_shell_add_mnemonic (GTK_MENU_SHELL (menu_shell),
1731 priv->mnemonic_keyval,
1733 mnemonic_menu = menu_shell;
1736 if (!GTK_IS_MENU (menu_shell))
1738 gtk_window_add_mnemonic (GTK_WINDOW (toplevel),
1739 priv->mnemonic_keyval,
1741 priv->mnemonic_window = GTK_WINDOW (toplevel);
1746 g_object_set_data (G_OBJECT (label), I_("gtk-mnemonic-menu"), mnemonic_menu);
1750 gtk_label_hierarchy_changed (GtkWidget *widget,
1751 GtkWidget *old_toplevel)
1753 GtkLabel *label = GTK_LABEL (widget);
1754 GtkLabelPrivate *priv = label->priv;
1756 gtk_label_setup_mnemonic (label, priv->mnemonic_keyval);
1760 label_shortcut_setting_apply (GtkLabel *label)
1762 gtk_label_recalculate (label);
1763 if (GTK_IS_ACCEL_LABEL (label))
1764 gtk_accel_label_refetch (GTK_ACCEL_LABEL (label));
1768 label_shortcut_setting_traverse_container (GtkWidget *widget,
1771 if (GTK_IS_LABEL (widget))
1772 label_shortcut_setting_apply (GTK_LABEL (widget));
1773 else if (GTK_IS_CONTAINER (widget))
1774 gtk_container_forall (GTK_CONTAINER (widget),
1775 label_shortcut_setting_traverse_container, data);
1779 label_shortcut_setting_changed (GtkSettings *settings)
1783 list = gtk_window_list_toplevels ();
1785 for (l = list; l ; l = l->next)
1787 GtkWidget *widget = l->data;
1789 if (gtk_widget_get_settings (widget) == settings)
1790 gtk_container_forall (GTK_CONTAINER (widget),
1791 label_shortcut_setting_traverse_container, NULL);
1798 mnemonics_visible_apply (GtkWidget *widget,
1799 gboolean mnemonics_visible)
1801 GtkLabel *label = GTK_LABEL (widget);
1802 GtkLabelPrivate *priv = label->priv;
1804 mnemonics_visible = mnemonics_visible != FALSE;
1806 if (priv->mnemonics_visible != mnemonics_visible)
1808 priv->mnemonics_visible = mnemonics_visible;
1810 gtk_label_recalculate (label);
1815 label_mnemonics_visible_traverse_container (GtkWidget *widget,
1818 gboolean mnemonics_visible = GPOINTER_TO_INT (data);
1820 _gtk_label_mnemonics_visible_apply_recursively (widget, mnemonics_visible);
1824 _gtk_label_mnemonics_visible_apply_recursively (GtkWidget *widget,
1825 gboolean mnemonics_visible)
1827 if (GTK_IS_LABEL (widget))
1828 mnemonics_visible_apply (widget, mnemonics_visible);
1829 else if (GTK_IS_CONTAINER (widget))
1830 gtk_container_forall (GTK_CONTAINER (widget),
1831 label_mnemonics_visible_traverse_container,
1832 GINT_TO_POINTER (mnemonics_visible));
1836 label_mnemonics_visible_changed (GtkWindow *window,
1840 gboolean mnemonics_visible;
1842 g_object_get (window, "mnemonics-visible", &mnemonics_visible, NULL);
1844 gtk_container_forall (GTK_CONTAINER (window),
1845 label_mnemonics_visible_traverse_container,
1846 GINT_TO_POINTER (mnemonics_visible));
1850 gtk_label_screen_changed (GtkWidget *widget,
1851 GdkScreen *old_screen)
1853 GtkSettings *settings;
1854 gboolean shortcuts_connected;
1856 if (!gtk_widget_has_screen (widget))
1859 settings = gtk_widget_get_settings (widget);
1861 shortcuts_connected =
1862 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (settings),
1863 "gtk-label-shortcuts-connected"));
1865 if (! shortcuts_connected)
1867 g_signal_connect (settings, "notify::gtk-enable-mnemonics",
1868 G_CALLBACK (label_shortcut_setting_changed),
1870 g_signal_connect (settings, "notify::gtk-enable-accels",
1871 G_CALLBACK (label_shortcut_setting_changed),
1874 g_object_set_data (G_OBJECT (settings), "gtk-label-shortcuts-connected",
1875 GINT_TO_POINTER (TRUE));
1878 label_shortcut_setting_apply (GTK_LABEL (widget));
1883 label_mnemonic_widget_weak_notify (gpointer data,
1884 GObject *where_the_object_was)
1886 GtkLabel *label = data;
1887 GtkLabelPrivate *priv = label->priv;
1889 priv->mnemonic_widget = NULL;
1890 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1894 * gtk_label_set_mnemonic_widget:
1895 * @label: a #GtkLabel
1896 * @widget: (allow-none): the target #GtkWidget
1898 * If the label has been set so that it has an mnemonic key (using
1899 * i.e. gtk_label_set_markup_with_mnemonic(),
1900 * gtk_label_set_text_with_mnemonic(), gtk_label_new_with_mnemonic()
1901 * or the "use_underline" property) the label can be associated with a
1902 * widget that is the target of the mnemonic. When the label is inside
1903 * a widget (like a #GtkButton or a #GtkNotebook tab) it is
1904 * automatically associated with the correct widget, but sometimes
1905 * (i.e. when the target is a #GtkEntry next to the label) you need to
1906 * set it explicitly using this function.
1908 * The target widget will be accelerated by emitting the
1909 * GtkWidget::mnemonic-activate signal on it. The default handler for
1910 * this signal will activate the widget if there are no mnemonic collisions
1911 * and toggle focus between the colliding widgets otherwise.
1914 gtk_label_set_mnemonic_widget (GtkLabel *label,
1917 GtkLabelPrivate *priv;
1919 g_return_if_fail (GTK_IS_LABEL (label));
1924 g_return_if_fail (GTK_IS_WIDGET (widget));
1926 if (priv->mnemonic_widget)
1928 gtk_widget_remove_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1929 g_object_weak_unref (G_OBJECT (priv->mnemonic_widget),
1930 label_mnemonic_widget_weak_notify,
1933 priv->mnemonic_widget = widget;
1934 if (priv->mnemonic_widget)
1936 g_object_weak_ref (G_OBJECT (priv->mnemonic_widget),
1937 label_mnemonic_widget_weak_notify,
1939 gtk_widget_add_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1942 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1946 * gtk_label_get_mnemonic_widget:
1947 * @label: a #GtkLabel
1949 * Retrieves the target of the mnemonic (keyboard shortcut) of this
1950 * label. See gtk_label_set_mnemonic_widget().
1952 * Return value: (transfer none): the target of the label's mnemonic,
1953 * or %NULL if none has been set and the default algorithm will be used.
1956 gtk_label_get_mnemonic_widget (GtkLabel *label)
1958 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1960 return label->priv->mnemonic_widget;
1964 * gtk_label_get_mnemonic_keyval:
1965 * @label: a #GtkLabel
1967 * If the label has been set so that it has an mnemonic key this function
1968 * returns the keyval used for the mnemonic accelerator. If there is no
1969 * mnemonic set up it returns #GDK_VoidSymbol.
1971 * Returns: GDK keyval usable for accelerators, or #GDK_VoidSymbol
1974 gtk_label_get_mnemonic_keyval (GtkLabel *label)
1976 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_KEY_VoidSymbol);
1978 return label->priv->mnemonic_keyval;
1982 gtk_label_set_text_internal (GtkLabel *label,
1985 GtkLabelPrivate *priv = label->priv;
1987 g_free (priv->text);
1991 gtk_label_select_region_index (label, 0, 0);
1995 gtk_label_set_label_internal (GtkLabel *label,
1998 GtkLabelPrivate *priv = label->priv;
2000 g_free (priv->label);
2004 g_object_notify (G_OBJECT (label), "label");
2008 gtk_label_set_use_markup_internal (GtkLabel *label,
2011 GtkLabelPrivate *priv = label->priv;
2014 if (priv->use_markup != val)
2016 priv->use_markup = val;
2018 g_object_notify (G_OBJECT (label), "use-markup");
2023 gtk_label_set_use_underline_internal (GtkLabel *label,
2026 GtkLabelPrivate *priv = label->priv;
2029 if (priv->use_underline != val)
2031 priv->use_underline = val;
2033 g_object_notify (G_OBJECT (label), "use-underline");
2038 my_pango_attr_list_merge_filter (PangoAttribute *attribute,
2041 pango_attr_list_change (list, pango_attribute_copy (attribute));
2046 my_pango_attr_list_merge (PangoAttrList *into,
2047 PangoAttrList *from)
2049 pango_attr_list_filter (from, my_pango_attr_list_merge_filter, into);
2052 /* Calculates text, attrs and mnemonic_keyval from
2053 * label, use_underline and use_markup
2056 gtk_label_recalculate (GtkLabel *label)
2058 GtkLabelPrivate *priv = label->priv;
2059 guint keyval = priv->mnemonic_keyval;
2061 gtk_label_clear_links (label);
2063 if (priv->use_markup)
2064 gtk_label_set_markup_internal (label, priv->label, priv->use_underline);
2065 else if (priv->use_underline)
2066 gtk_label_set_uline_text_internal (label, priv->label);
2069 if (!priv->pattern_set)
2071 if (priv->markup_attrs)
2072 pango_attr_list_unref (priv->markup_attrs);
2073 priv->markup_attrs = NULL;
2075 gtk_label_set_text_internal (label, g_strdup (priv->label));
2078 if (!priv->use_underline)
2079 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
2081 if (keyval != priv->mnemonic_keyval)
2083 gtk_label_setup_mnemonic (label, keyval);
2084 g_object_notify (G_OBJECT (label), "mnemonic-keyval");
2087 gtk_label_clear_layout (label);
2088 gtk_label_clear_select_info (label);
2089 gtk_widget_queue_resize (GTK_WIDGET (label));
2093 * gtk_label_set_text:
2094 * @label: a #GtkLabel
2095 * @str: The text you want to set
2097 * Sets the text within the #GtkLabel widget. It overwrites any text that
2100 * This will also clear any previously set mnemonic accelerators.
2103 gtk_label_set_text (GtkLabel *label,
2106 g_return_if_fail (GTK_IS_LABEL (label));
2108 g_object_freeze_notify (G_OBJECT (label));
2110 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2111 gtk_label_set_use_markup_internal (label, FALSE);
2112 gtk_label_set_use_underline_internal (label, FALSE);
2114 gtk_label_recalculate (label);
2116 g_object_thaw_notify (G_OBJECT (label));
2120 * gtk_label_set_attributes:
2121 * @label: a #GtkLabel
2122 * @attrs: a #PangoAttrList
2124 * Sets a #PangoAttrList; the attributes in the list are applied to the
2127 * <note><para>The attributes set with this function will be applied
2128 * and merged with any other attributes previously effected by way
2129 * of the #GtkLabel:use-underline or #GtkLabel:use-markup properties.
2130 * While it is not recommended to mix markup strings with manually set
2131 * attributes, if you must; know that the attributes will be applied
2132 * to the label after the markup string is parsed.</para></note>
2135 gtk_label_set_attributes (GtkLabel *label,
2136 PangoAttrList *attrs)
2138 GtkLabelPrivate *priv = label->priv;
2140 g_return_if_fail (GTK_IS_LABEL (label));
2143 pango_attr_list_ref (attrs);
2146 pango_attr_list_unref (priv->attrs);
2147 priv->attrs = attrs;
2149 g_object_notify (G_OBJECT (label), "attributes");
2151 gtk_label_clear_layout (label);
2152 gtk_widget_queue_resize (GTK_WIDGET (label));
2156 * gtk_label_get_attributes:
2157 * @label: a #GtkLabel
2159 * Gets the attribute list that was set on the label using
2160 * gtk_label_set_attributes(), if any. This function does
2161 * not reflect attributes that come from the labels markup
2162 * (see gtk_label_set_markup()). If you want to get the
2163 * effective attributes for the label, use
2164 * pango_layout_get_attribute (gtk_label_get_layout (label)).
2166 * Return value: (transfer none): the attribute list, or %NULL
2170 gtk_label_get_attributes (GtkLabel *label)
2172 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2174 return label->priv->attrs;
2178 * gtk_label_set_label:
2179 * @label: a #GtkLabel
2180 * @str: the new text to set for the label
2182 * Sets the text of the label. The label is interpreted as
2183 * including embedded underlines and/or Pango markup depending
2184 * on the values of the #GtkLabel:use-underline" and
2185 * #GtkLabel:use-markup properties.
2188 gtk_label_set_label (GtkLabel *label,
2191 g_return_if_fail (GTK_IS_LABEL (label));
2193 g_object_freeze_notify (G_OBJECT (label));
2195 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2196 gtk_label_recalculate (label);
2198 g_object_thaw_notify (G_OBJECT (label));
2202 * gtk_label_get_label:
2203 * @label: a #GtkLabel
2205 * Fetches the text from a label widget including any embedded
2206 * underlines indicating mnemonics and Pango markup. (See
2207 * gtk_label_get_text()).
2209 * Return value: the text of the label widget. This string is
2210 * owned by the widget and must not be modified or freed.
2213 gtk_label_get_label (GtkLabel *label)
2215 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2217 return label->priv->label;
2229 start_element_handler (GMarkupParseContext *context,
2230 const gchar *element_name,
2231 const gchar **attribute_names,
2232 const gchar **attribute_values,
2236 GtkLabelPrivate *priv;
2237 UriParserData *pdata = user_data;
2239 if (strcmp (element_name, "a") == 0)
2242 const gchar *uri = NULL;
2243 const gchar *title = NULL;
2244 gboolean visited = FALSE;
2249 g_markup_parse_context_get_position (context, &line_number, &char_number);
2251 for (i = 0; attribute_names[i] != NULL; i++)
2253 const gchar *attr = attribute_names[i];
2255 if (strcmp (attr, "href") == 0)
2256 uri = attribute_values[i];
2257 else if (strcmp (attr, "title") == 0)
2258 title = attribute_values[i];
2263 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
2264 "Attribute '%s' is not allowed on the <a> tag "
2265 "on line %d char %d",
2266 attr, line_number, char_number);
2275 G_MARKUP_ERROR_INVALID_CONTENT,
2276 "Attribute 'href' was missing on the <a> tag "
2277 "on line %d char %d",
2278 line_number, char_number);
2283 priv = pdata->label->priv;
2284 if (priv->track_links && priv->select_info)
2287 for (l = priv->select_info->links; l; l = l->next)
2290 if (strcmp (uri, link->uri) == 0)
2292 visited = link->visited;
2298 link = g_new0 (GtkLabelLink, 1);
2299 link->uri = g_strdup (uri);
2300 link->title = g_strdup (title);
2301 link->visited = visited;
2302 link->start = pdata->text_len;
2303 pdata->links = g_list_prepend (pdata->links, link);
2309 g_string_append_c (pdata->new_str, '<');
2310 g_string_append (pdata->new_str, element_name);
2312 for (i = 0; attribute_names[i] != NULL; i++)
2314 const gchar *attr = attribute_names[i];
2315 const gchar *value = attribute_values[i];
2318 newvalue = g_markup_escape_text (value, -1);
2320 g_string_append_c (pdata->new_str, ' ');
2321 g_string_append (pdata->new_str, attr);
2322 g_string_append (pdata->new_str, "=\"");
2323 g_string_append (pdata->new_str, newvalue);
2324 g_string_append_c (pdata->new_str, '\"');
2328 g_string_append_c (pdata->new_str, '>');
2333 end_element_handler (GMarkupParseContext *context,
2334 const gchar *element_name,
2338 UriParserData *pdata = user_data;
2340 if (!strcmp (element_name, "a"))
2342 GtkLabelLink *link = pdata->links->data;
2343 link->end = pdata->text_len;
2347 g_string_append (pdata->new_str, "</");
2348 g_string_append (pdata->new_str, element_name);
2349 g_string_append_c (pdata->new_str, '>');
2354 text_handler (GMarkupParseContext *context,
2360 UriParserData *pdata = user_data;
2363 newtext = g_markup_escape_text (text, text_len);
2364 g_string_append (pdata->new_str, newtext);
2365 pdata->text_len += text_len;
2369 static const GMarkupParser markup_parser =
2371 start_element_handler,
2372 end_element_handler,
2379 xml_isspace (gchar c)
2381 return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
2385 link_free (GtkLabelLink *link)
2388 g_free (link->title);
2393 gtk_label_get_link_colors (GtkWidget *widget,
2394 GdkColor *link_color,
2395 GdkColor *visited_link_color)
2397 GtkStyleContext *context;
2398 GdkColor *link, *visited;
2400 context = gtk_widget_get_style_context (widget);
2401 gtk_style_context_get_style (context,
2402 "link-color", &link,
2403 "visited-link-color", &visited,
2407 *link_color = *link;
2408 gdk_color_free (link);
2411 *link_color = default_link_color;
2415 *visited_link_color = *visited;
2416 gdk_color_free (visited);
2419 *visited_link_color = default_visited_link_color;
2423 parse_uri_markup (GtkLabel *label,
2429 GMarkupParseContext *context = NULL;
2430 const gchar *p, *end;
2431 gboolean needs_root = TRUE;
2433 UriParserData pdata;
2435 length = strlen (str);
2439 pdata.label = label;
2441 pdata.new_str = g_string_sized_new (length);
2444 while (p != end && xml_isspace (*p))
2447 if (end - p >= 8 && strncmp (p, "<markup>", 8) == 0)
2450 context = g_markup_parse_context_new (&markup_parser, 0, &pdata, NULL);
2454 if (!g_markup_parse_context_parse (context, "<markup>", -1, error))
2458 if (!g_markup_parse_context_parse (context, str, length, error))
2463 if (!g_markup_parse_context_parse (context, "</markup>", -1, error))
2467 if (!g_markup_parse_context_end_parse (context, error))
2470 g_markup_parse_context_free (context);
2472 *new_str = g_string_free (pdata.new_str, FALSE);
2473 *links = pdata.links;
2478 g_markup_parse_context_free (context);
2479 g_string_free (pdata.new_str, TRUE);
2480 g_list_free_full (pdata.links, (GDestroyNotify) link_free);
2486 gtk_label_ensure_has_tooltip (GtkLabel *label)
2488 GtkLabelPrivate *priv = label->priv;
2490 gboolean has_tooltip = FALSE;
2492 for (l = priv->select_info->links; l; l = l->next)
2494 GtkLabelLink *link = l->data;
2502 gtk_widget_set_has_tooltip (GTK_WIDGET (label), has_tooltip);
2506 gtk_label_set_markup_internal (GtkLabel *label,
2508 gboolean with_uline)
2510 GtkLabelPrivate *priv = label->priv;
2512 GError *error = NULL;
2513 PangoAttrList *attrs = NULL;
2514 gunichar accel_char = 0;
2516 GList *links = NULL;
2518 if (!parse_uri_markup (label, str, &new_str, &links, &error))
2520 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2522 g_error_free (error);
2528 gtk_label_ensure_select_info (label);
2529 priv->select_info->links = g_list_reverse (links);
2530 gtk_label_ensure_has_tooltip (label);
2535 gboolean enable_mnemonics;
2536 gboolean auto_mnemonics;
2538 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2539 "gtk-enable-mnemonics", &enable_mnemonics,
2540 "gtk-auto-mnemonics", &auto_mnemonics,
2543 if (!(enable_mnemonics && priv->mnemonics_visible &&
2545 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2546 (!priv->mnemonic_widget ||
2547 gtk_widget_is_sensitive (priv->mnemonic_widget))))))
2553 if (separate_uline_pattern (new_str, &key, &tmp, &pattern))
2562 if (!pango_parse_markup (new_str,
2564 with_uline ? '_' : 0,
2567 with_uline ? &accel_char : NULL,
2570 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2573 g_error_free (error);
2580 gtk_label_set_text_internal (label, text);
2584 if (priv->markup_attrs)
2585 pango_attr_list_unref (priv->markup_attrs);
2586 priv->markup_attrs = attrs;
2589 if (accel_char != 0)
2590 priv->mnemonic_keyval = gdk_keyval_to_lower (gdk_unicode_to_keyval (accel_char));
2592 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
2596 * gtk_label_set_markup:
2597 * @label: a #GtkLabel
2598 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
2600 * Parses @str which is marked up with the <link
2601 * linkend="PangoMarkupFormat">Pango text markup language</link>, setting the
2602 * label's text and attribute list based on the parse results. If the @str is
2603 * external data, you may need to escape it with g_markup_escape_text() or
2604 * g_markup_printf_escaped()<!-- -->:
2608 * markup = g_markup_printf_escaped ("<span style=\"italic\">%s</span>", str);
2609 * gtk_label_set_markup (GTK_LABEL (label), markup);
2614 gtk_label_set_markup (GtkLabel *label,
2617 g_return_if_fail (GTK_IS_LABEL (label));
2619 g_object_freeze_notify (G_OBJECT (label));
2621 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2622 gtk_label_set_use_markup_internal (label, TRUE);
2623 gtk_label_set_use_underline_internal (label, FALSE);
2625 gtk_label_recalculate (label);
2627 g_object_thaw_notify (G_OBJECT (label));
2631 * gtk_label_set_markup_with_mnemonic:
2632 * @label: a #GtkLabel
2633 * @str: a markup string (see
2634 * <link linkend="PangoMarkupFormat">Pango markup format</link>)
2636 * Parses @str which is marked up with the
2637 * <link linkend="PangoMarkupFormat">Pango text markup language</link>,
2638 * setting the label's text and attribute list based on the parse results.
2639 * If characters in @str are preceded by an underscore, they are underlined
2640 * indicating that they represent a keyboard accelerator called a mnemonic.
2642 * The mnemonic key can be used to activate another widget, chosen
2643 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
2646 gtk_label_set_markup_with_mnemonic (GtkLabel *label,
2649 g_return_if_fail (GTK_IS_LABEL (label));
2651 g_object_freeze_notify (G_OBJECT (label));
2653 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2654 gtk_label_set_use_markup_internal (label, TRUE);
2655 gtk_label_set_use_underline_internal (label, TRUE);
2657 gtk_label_recalculate (label);
2659 g_object_thaw_notify (G_OBJECT (label));
2663 * gtk_label_get_text:
2664 * @label: a #GtkLabel
2666 * Fetches the text from a label widget, as displayed on the
2667 * screen. This does not include any embedded underlines
2668 * indicating mnemonics or Pango markup. (See gtk_label_get_label())
2670 * Return value: the text in the label widget. This is the internal
2671 * string used by the label, and must not be modified.
2674 gtk_label_get_text (GtkLabel *label)
2676 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2678 return label->priv->text;
2681 static PangoAttrList *
2682 gtk_label_pattern_to_attrs (GtkLabel *label,
2683 const gchar *pattern)
2685 GtkLabelPrivate *priv = label->priv;
2687 const char *p = priv->text;
2688 const char *q = pattern;
2689 PangoAttrList *attrs;
2691 attrs = pango_attr_list_new ();
2695 while (*p && *q && *q != '_')
2697 p = g_utf8_next_char (p);
2701 while (*p && *q && *q == '_')
2703 p = g_utf8_next_char (p);
2709 PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_LOW);
2710 attr->start_index = start - priv->text;
2711 attr->end_index = p - priv->text;
2713 pango_attr_list_insert (attrs, attr);
2723 gtk_label_set_pattern_internal (GtkLabel *label,
2724 const gchar *pattern,
2725 gboolean is_mnemonic)
2727 GtkLabelPrivate *priv = label->priv;
2728 PangoAttrList *attrs;
2729 gboolean enable_mnemonics;
2730 gboolean auto_mnemonics;
2732 if (priv->pattern_set)
2737 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2738 "gtk-enable-mnemonics", &enable_mnemonics,
2739 "gtk-auto-mnemonics", &auto_mnemonics,
2742 if (enable_mnemonics && priv->mnemonics_visible && pattern &&
2744 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2745 (!priv->mnemonic_widget ||
2746 gtk_widget_is_sensitive (priv->mnemonic_widget)))))
2747 attrs = gtk_label_pattern_to_attrs (label, pattern);
2752 attrs = gtk_label_pattern_to_attrs (label, pattern);
2754 if (priv->markup_attrs)
2755 pango_attr_list_unref (priv->markup_attrs);
2756 priv->markup_attrs = attrs;
2760 * gtk_label_set_pattern:
2761 * @label: The #GtkLabel you want to set the pattern to.
2762 * @pattern: The pattern as described above.
2764 * The pattern of underlines you want under the existing text within the
2765 * #GtkLabel widget. For example if the current text of the label says
2766 * "FooBarBaz" passing a pattern of "___ ___" will underline
2767 * "Foo" and "Baz" but not "Bar".
2770 gtk_label_set_pattern (GtkLabel *label,
2771 const gchar *pattern)
2773 GtkLabelPrivate *priv;
2775 g_return_if_fail (GTK_IS_LABEL (label));
2779 priv->pattern_set = FALSE;
2783 gtk_label_set_pattern_internal (label, pattern, FALSE);
2784 priv->pattern_set = TRUE;
2787 gtk_label_recalculate (label);
2789 gtk_label_clear_layout (label);
2790 gtk_widget_queue_resize (GTK_WIDGET (label));
2795 * gtk_label_set_justify:
2796 * @label: a #GtkLabel
2797 * @jtype: a #GtkJustification
2799 * Sets the alignment of the lines in the text of the label relative to
2800 * each other. %GTK_JUSTIFY_LEFT is the default value when the
2801 * widget is first created with gtk_label_new(). If you instead want
2802 * to set the alignment of the label as a whole, use
2803 * gtk_misc_set_alignment() instead. gtk_label_set_justify() has no
2804 * effect on labels containing only a single line.
2807 gtk_label_set_justify (GtkLabel *label,
2808 GtkJustification jtype)
2810 GtkLabelPrivate *priv;
2812 g_return_if_fail (GTK_IS_LABEL (label));
2813 g_return_if_fail (jtype >= GTK_JUSTIFY_LEFT && jtype <= GTK_JUSTIFY_FILL);
2817 if ((GtkJustification) priv->jtype != jtype)
2819 priv->jtype = jtype;
2821 /* No real need to be this drastic, but easier than duplicating the code */
2822 gtk_label_clear_layout (label);
2824 g_object_notify (G_OBJECT (label), "justify");
2825 gtk_widget_queue_resize (GTK_WIDGET (label));
2830 * gtk_label_get_justify:
2831 * @label: a #GtkLabel
2833 * Returns the justification of the label. See gtk_label_set_justify().
2835 * Return value: #GtkJustification
2838 gtk_label_get_justify (GtkLabel *label)
2840 g_return_val_if_fail (GTK_IS_LABEL (label), 0);
2842 return label->priv->jtype;
2846 * gtk_label_set_ellipsize:
2847 * @label: a #GtkLabel
2848 * @mode: a #PangoEllipsizeMode
2850 * Sets the mode used to ellipsize (add an ellipsis: "...") to the text
2851 * if there is not enough space to render the entire string.
2856 gtk_label_set_ellipsize (GtkLabel *label,
2857 PangoEllipsizeMode mode)
2859 GtkLabelPrivate *priv;
2861 g_return_if_fail (GTK_IS_LABEL (label));
2862 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE && mode <= PANGO_ELLIPSIZE_END);
2866 if ((PangoEllipsizeMode) priv->ellipsize != mode)
2868 priv->ellipsize = mode;
2870 /* No real need to be this drastic, but easier than duplicating the code */
2871 gtk_label_clear_layout (label);
2873 g_object_notify (G_OBJECT (label), "ellipsize");
2874 gtk_widget_queue_resize (GTK_WIDGET (label));
2879 * gtk_label_get_ellipsize:
2880 * @label: a #GtkLabel
2882 * Returns the ellipsizing position of the label. See gtk_label_set_ellipsize().
2884 * Return value: #PangoEllipsizeMode
2889 gtk_label_get_ellipsize (GtkLabel *label)
2891 g_return_val_if_fail (GTK_IS_LABEL (label), PANGO_ELLIPSIZE_NONE);
2893 return label->priv->ellipsize;
2897 * gtk_label_set_width_chars:
2898 * @label: a #GtkLabel
2899 * @n_chars: the new desired width, in characters.
2901 * Sets the desired width in characters of @label to @n_chars.
2906 gtk_label_set_width_chars (GtkLabel *label,
2909 GtkLabelPrivate *priv;
2911 g_return_if_fail (GTK_IS_LABEL (label));
2915 if (priv->width_chars != n_chars)
2917 priv->width_chars = n_chars;
2918 g_object_notify (G_OBJECT (label), "width-chars");
2919 gtk_widget_queue_resize (GTK_WIDGET (label));
2924 * gtk_label_get_width_chars:
2925 * @label: a #GtkLabel
2927 * Retrieves the desired width of @label, in characters. See
2928 * gtk_label_set_width_chars().
2930 * Return value: the width of the label in characters.
2935 gtk_label_get_width_chars (GtkLabel *label)
2937 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2939 return label->priv->width_chars;
2943 * gtk_label_set_max_width_chars:
2944 * @label: a #GtkLabel
2945 * @n_chars: the new desired maximum width, in characters.
2947 * Sets the desired maximum width in characters of @label to @n_chars.
2952 gtk_label_set_max_width_chars (GtkLabel *label,
2955 GtkLabelPrivate *priv;
2957 g_return_if_fail (GTK_IS_LABEL (label));
2961 if (priv->max_width_chars != n_chars)
2963 priv->max_width_chars = n_chars;
2965 g_object_notify (G_OBJECT (label), "max-width-chars");
2966 gtk_widget_queue_resize (GTK_WIDGET (label));
2971 * gtk_label_get_max_width_chars:
2972 * @label: a #GtkLabel
2974 * Retrieves the desired maximum width of @label, in characters. See
2975 * gtk_label_set_width_chars().
2977 * Return value: the maximum width of the label in characters.
2982 gtk_label_get_max_width_chars (GtkLabel *label)
2984 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2986 return label->priv->max_width_chars;
2990 * gtk_label_set_line_wrap:
2991 * @label: a #GtkLabel
2992 * @wrap: the setting
2994 * Toggles line wrapping within the #GtkLabel widget. %TRUE makes it break
2995 * lines if text exceeds the widget's size. %FALSE lets the text get cut off
2996 * by the edge of the widget if it exceeds the widget size.
2998 * Note that setting line wrapping to %TRUE does not make the label
2999 * wrap at its parent container's width, because GTK+ widgets
3000 * conceptually can't make their requisition depend on the parent
3001 * container's size. For a label that wraps at a specific position,
3002 * set the label's width using gtk_widget_set_size_request().
3005 gtk_label_set_line_wrap (GtkLabel *label,
3008 GtkLabelPrivate *priv;
3010 g_return_if_fail (GTK_IS_LABEL (label));
3014 wrap = wrap != FALSE;
3016 if (priv->wrap != wrap)
3020 gtk_label_clear_layout (label);
3021 gtk_widget_queue_resize (GTK_WIDGET (label));
3022 g_object_notify (G_OBJECT (label), "wrap");
3027 * gtk_label_get_line_wrap:
3028 * @label: a #GtkLabel
3030 * Returns whether lines in the label are automatically wrapped.
3031 * See gtk_label_set_line_wrap().
3033 * Return value: %TRUE if the lines of the label are automatically wrapped.
3036 gtk_label_get_line_wrap (GtkLabel *label)
3038 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
3040 return label->priv->wrap;
3044 * gtk_label_set_line_wrap_mode:
3045 * @label: a #GtkLabel
3046 * @wrap_mode: the line wrapping mode
3048 * If line wrapping is on (see gtk_label_set_line_wrap()) this controls how
3049 * the line wrapping is done. The default is %PANGO_WRAP_WORD which means
3050 * wrap on word boundaries.
3055 gtk_label_set_line_wrap_mode (GtkLabel *label,
3056 PangoWrapMode wrap_mode)
3058 GtkLabelPrivate *priv;
3060 g_return_if_fail (GTK_IS_LABEL (label));
3064 if (priv->wrap_mode != wrap_mode)
3066 priv->wrap_mode = wrap_mode;
3067 g_object_notify (G_OBJECT (label), "wrap-mode");
3069 gtk_widget_queue_resize (GTK_WIDGET (label));
3074 * gtk_label_get_line_wrap_mode:
3075 * @label: a #GtkLabel
3077 * Returns line wrap mode used by the label. See gtk_label_set_line_wrap_mode().
3079 * Return value: %TRUE if the lines of the label are automatically wrapped.
3084 gtk_label_get_line_wrap_mode (GtkLabel *label)
3086 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
3088 return label->priv->wrap_mode;
3092 gtk_label_destroy (GtkWidget *widget)
3094 GtkLabel *label = GTK_LABEL (widget);
3096 gtk_label_set_mnemonic_widget (label, NULL);
3098 GTK_WIDGET_CLASS (gtk_label_parent_class)->destroy (widget);
3102 gtk_label_finalize (GObject *object)
3104 GtkLabel *label = GTK_LABEL (object);
3105 GtkLabelPrivate *priv = label->priv;
3107 g_free (priv->label);
3108 g_free (priv->text);
3111 g_object_unref (priv->layout);
3114 pango_attr_list_unref (priv->attrs);
3116 if (priv->markup_attrs)
3117 pango_attr_list_unref (priv->markup_attrs);
3119 gtk_label_clear_links (label);
3120 g_free (priv->select_info);
3122 G_OBJECT_CLASS (gtk_label_parent_class)->finalize (object);
3126 gtk_label_clear_layout (GtkLabel *label)
3128 GtkLabelPrivate *priv = label->priv;
3132 g_object_unref (priv->layout);
3133 priv->layout = NULL;
3137 static PangoFontMetrics *
3138 get_font_metrics (PangoContext *context, GtkWidget *widget)
3140 GtkStyleContext *style_context;
3141 const PangoFontDescription *font;
3142 PangoFontMetrics *retval;
3144 style_context = gtk_widget_get_style_context (widget);
3145 font = gtk_style_context_get_font (style_context, GTK_STATE_FLAG_NORMAL);
3147 retval = pango_context_get_metrics (context,
3149 pango_context_get_language (context));
3155 * gtk_label_get_measuring_layout:
3157 * @existing_layout: %NULL or an existing layout already in use.
3158 * @width: the width to measure with in pango units, or -1 for infinite
3159 * @height: the height to measure with in pango units, or -1 for infinite
3161 * Gets a layout that can be used for measuring sizes. The returned
3162 * layout will be identical to the label's layout except for the
3163 * layout's width, which will be set to @width. Do not modify the returned
3166 * Returns: a new reference to a pango layout
3168 static PangoLayout *
3169 gtk_label_get_measuring_layout (GtkLabel * label,
3170 PangoLayout *existing_layout,
3174 GtkLabelPrivate *priv = label->priv;
3175 PangoRectangle rect;
3178 if (existing_layout != NULL)
3180 if (existing_layout != priv->layout)
3182 pango_layout_set_width (existing_layout, width);
3183 pango_layout_set_height (existing_layout, height);
3184 return existing_layout;
3187 g_object_unref (existing_layout);
3190 gtk_label_ensure_layout (label);
3192 if (pango_layout_get_width (priv->layout) == width &&
3193 pango_layout_get_height (priv->layout) == height)
3195 g_object_ref (priv->layout);
3196 return priv->layout;
3199 /* We can use the label's own layout if we're not allocated a size yet,
3200 * because we don't need it to be properly setup at that point.
3201 * This way we can make use of caching upon the label's creation.
3203 if (gtk_widget_get_allocated_width (GTK_WIDGET (label)) <= 1)
3205 g_object_ref (priv->layout);
3206 pango_layout_set_width (priv->layout, width);
3207 pango_layout_set_height (priv->layout, height);
3208 return priv->layout;
3211 /* oftentimes we want to measure a width that is far wider than the current width,
3212 * even though the layout would not change if we made it wider. In that case, we
3213 * can just return the current layout, because for measuring purposes, it will be
3216 pango_layout_get_extents (priv->layout, NULL, &rect);
3217 if ((width == -1 || rect.width <= width) &&
3218 (height == -1 || rect.height <= height) &&
3219 !pango_layout_is_wrapped (priv->layout) &&
3220 !pango_layout_is_ellipsized (priv->layout))
3222 g_object_ref (priv->layout);
3223 return priv->layout;
3226 copy = pango_layout_copy (priv->layout);
3227 pango_layout_set_width (copy, width);
3228 pango_layout_set_height (copy, height);
3233 gtk_label_update_layout_width (GtkLabel *label)
3235 GtkLabelPrivate *priv = label->priv;
3236 GtkWidget *widget = GTK_WIDGET (label);
3238 g_assert (priv->layout);
3240 if (priv->ellipsize || priv->wrap)
3243 PangoRectangle logical;
3246 _gtk_misc_get_padding_and_border (GTK_MISC (label), &border);
3248 width = gtk_widget_get_allocated_width (GTK_WIDGET (label)) - border.left - border.right;
3249 height = gtk_widget_get_allocated_height (GTK_WIDGET (label)) - border.top - border.bottom;
3251 if (priv->have_transform)
3253 PangoContext *context = gtk_widget_get_pango_context (widget);
3254 const PangoMatrix *matrix = pango_context_get_matrix (context);
3255 const gdouble dx = matrix->xx; /* cos (M_PI * angle / 180) */
3256 const gdouble dy = matrix->xy; /* sin (M_PI * angle / 180) */
3258 pango_layout_set_width (priv->layout, -1);
3259 pango_layout_set_height (priv->layout, -1);
3260 pango_layout_get_pixel_extents (priv->layout, NULL, &logical);
3262 if (fabs (dy) < 0.01)
3264 if (logical.width > width)
3265 pango_layout_set_width (priv->layout, width * PANGO_SCALE);
3267 else if (fabs (dx) < 0.01)
3269 if (logical.width > height)
3270 pango_layout_set_width (priv->layout, height * PANGO_SCALE);
3274 gdouble x0, y0, x1, y1, length;
3279 y0 = dx ? x0 * dy / dx : G_MAXDOUBLE;
3280 vertical = fabs (y0) > height / 2;
3285 x0 = dy ? y0 * dx / dy : G_MAXDOUBLE;
3288 length = 2 * sqrt (x0 * x0 + y0 * y0);
3289 pango_layout_set_width (priv->layout, rint (length * PANGO_SCALE));
3290 pango_layout_get_pixel_size (priv->layout, NULL, &cy);
3297 y0 = height/2 + y1 - y0;
3302 x0 = width/2 + x1 - x0;
3306 length = length - sqrt (x0 * x0 + y0 * y0) * 2;
3307 pango_layout_set_width (priv->layout, rint (length * PANGO_SCALE));
3312 pango_layout_set_width (priv->layout, width * PANGO_SCALE);
3313 pango_layout_set_height (priv->layout, priv->ellipsize ? height * PANGO_SCALE : -1);
3318 pango_layout_set_width (priv->layout, -1);
3319 pango_layout_set_height (priv->layout, -1);
3324 gtk_label_ensure_layout (GtkLabel *label)
3326 GtkLabelPrivate *priv = label->priv;
3330 widget = GTK_WIDGET (label);
3332 rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
3336 PangoAlignment align = PANGO_ALIGN_LEFT; /* Quiet gcc */
3337 PangoAttrList *attrs;
3338 gdouble angle = gtk_label_get_angle (label);
3340 if (angle != 0.0 && !priv->select_info)
3342 PangoMatrix matrix = PANGO_MATRIX_INIT;
3344 /* We rotate the standard singleton PangoContext for the widget,
3345 * depending on the fact that it's meant pretty much exclusively
3348 pango_matrix_rotate (&matrix, angle);
3350 pango_context_set_matrix (gtk_widget_get_pango_context (widget), &matrix);
3352 priv->have_transform = TRUE;
3356 if (priv->have_transform)
3357 pango_context_set_matrix (gtk_widget_get_pango_context (widget), NULL);
3359 priv->have_transform = FALSE;
3362 priv->layout = gtk_widget_create_pango_layout (widget, priv->text);
3364 if (priv->select_info && priv->select_info->links)
3366 GdkColor link_color, visited_color;
3367 PangoAttribute *attribute;
3370 gtk_label_get_link_colors (widget, &link_color, &visited_color);
3371 attrs = pango_attr_list_new ();
3373 for (list = priv->select_info->links; list; list = list->next)
3375 GtkLabelLink *link = list->data;
3377 attribute = pango_attr_underline_new (TRUE);
3378 attribute->start_index = link->start;
3379 attribute->end_index = link->end;
3380 pango_attr_list_insert (attrs, attribute);
3383 attribute = pango_attr_foreground_new (visited_color.red,
3384 visited_color.green,
3385 visited_color.blue);
3387 attribute = pango_attr_foreground_new (link_color.red,
3390 attribute->start_index = link->start;
3391 attribute->end_index = link->end;
3392 pango_attr_list_insert (attrs, attribute);
3395 else if (priv->markup_attrs && priv->markup_attrs)
3396 attrs = pango_attr_list_new ();
3400 if (priv->markup_attrs)
3403 my_pango_attr_list_merge (attrs, priv->markup_attrs);
3405 attrs = pango_attr_list_ref (priv->markup_attrs);
3411 my_pango_attr_list_merge (attrs, priv->attrs);
3413 attrs = pango_attr_list_ref (priv->attrs);
3418 pango_layout_set_attributes (priv->layout, attrs);
3419 pango_attr_list_unref (attrs);
3422 switch (priv->jtype)
3424 case GTK_JUSTIFY_LEFT:
3425 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3427 case GTK_JUSTIFY_RIGHT:
3428 align = rtl ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
3430 case GTK_JUSTIFY_CENTER:
3431 align = PANGO_ALIGN_CENTER;
3433 case GTK_JUSTIFY_FILL:
3434 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3435 pango_layout_set_justify (priv->layout, TRUE);
3438 g_assert_not_reached();
3441 pango_layout_set_alignment (priv->layout, align);
3442 pango_layout_set_ellipsize (priv->layout, priv->ellipsize);
3443 pango_layout_set_wrap (priv->layout, priv->wrap_mode);
3444 pango_layout_set_single_paragraph_mode (priv->layout, priv->single_line_mode);
3446 gtk_label_update_layout_width (label);
3451 get_single_line_height (GtkWidget *widget,
3452 PangoLayout *layout)
3454 PangoContext *context;
3455 PangoFontMetrics *metrics;
3456 gint ascent, descent;
3458 context = pango_layout_get_context (layout);
3459 metrics = get_font_metrics (context, widget);
3460 ascent = pango_font_metrics_get_ascent (metrics);
3461 descent = pango_font_metrics_get_descent (metrics);
3462 pango_font_metrics_unref (metrics);
3464 return ascent + descent;
3467 static GtkSizeRequestMode
3468 gtk_label_get_request_mode (GtkWidget *widget)
3470 GtkLabel *label = GTK_LABEL (widget);
3471 gdouble angle = gtk_label_get_angle (label);
3473 if (label->priv->wrap)
3474 return (angle == 90 || angle == 270) ?
3475 GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT :
3476 GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
3478 return GTK_SIZE_REQUEST_CONSTANT_SIZE;
3482 get_size_for_allocation (GtkLabel *label,
3483 GtkOrientation orientation,
3488 GtkLabelPrivate *priv = label->priv;
3489 PangoLayout *layout;
3492 layout = gtk_label_get_measuring_layout (label, NULL, allocation * PANGO_SCALE, -1);
3494 pango_layout_get_pixel_size (layout, NULL, &text_height);
3497 *minimum_size = text_height;
3501 if (priv->ellipsize && priv->wrap)
3503 layout = gtk_label_get_measuring_layout (label, layout, allocation * PANGO_SCALE, G_MAXINT);
3504 pango_layout_get_pixel_size (layout, NULL, &text_height);
3507 *natural_size = text_height;
3510 g_object_unref (layout);
3514 get_char_pixels (GtkWidget *label,
3515 PangoLayout *layout)
3517 PangoContext *context;
3518 PangoFontMetrics *metrics;
3519 gint char_width, digit_width;
3521 context = pango_layout_get_context (layout);
3522 metrics = get_font_metrics (context, GTK_WIDGET (label));
3523 char_width = pango_font_metrics_get_approximate_char_width (metrics);
3524 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
3525 pango_font_metrics_unref (metrics);
3527 return MAX (char_width, digit_width);;
3531 gtk_label_get_preferred_layout_size (GtkLabel *label,
3532 PangoRectangle *required,
3533 PangoRectangle *natural)
3535 GtkLabelPrivate *priv = label->priv;
3536 PangoLayout *layout;
3538 /* "width-chars" Hard-coded minimum width:
3539 * - minimum size should be MAX (width-chars, strlen ("..."));
3540 * - natural size should be MAX (width-chars, strlen (priv->text));
3542 * "max-width-chars" User specified maximum size requisition
3543 * - minimum size should be MAX (width-chars, 0)
3544 * - natural size should be MIN (max-width-chars, strlen (priv->text))
3546 * For ellipsizing labels; if max-width-chars is specified: either it is used as
3547 * a minimum size or the label text as a minimum size (natural size still overflows).
3549 * For wrapping labels; A reasonable minimum size is useful to naturally layout
3550 * interfaces automatically. In this case if no "width-chars" is specified, the minimum
3551 * width will default to the wrap guess that gtk_label_ensure_layout() does.
3554 /* Start off with the pixel extents of an as-wide-as-possible layout */
3555 layout = gtk_label_get_measuring_layout (label, NULL, -1, -1);
3557 pango_layout_get_extents (layout, NULL, natural);
3558 natural->x = natural->y = 0;
3561 natural->height = get_single_line_height (GTK_WIDGET (label), layout);
3563 if (priv->ellipsize || priv->wrap)
3565 /* a layout with width 0 will be as small as humanly possible */
3566 layout = gtk_label_get_measuring_layout (label, layout, 0, -1);
3568 pango_layout_get_extents (layout, NULL, required);
3570 /* can happen when Pango decides to ellipsize text */
3571 if (required->width > natural->width)
3572 required->width = natural->width;
3574 required->x = required->y = 0;
3575 required->height = natural->height;
3579 *required = *natural;
3582 if (priv->width_chars > -1 || priv->max_width_chars > -1)
3586 char_pixels = get_char_pixels (GTK_WIDGET (label), layout);
3588 if (priv->width_chars > -1)
3589 required->width = MAX (required->width, char_pixels * priv->width_chars);
3591 if (priv->max_width_chars > -1)
3592 natural->width = MIN (natural->width, priv->max_width_chars * char_pixels);
3593 natural->width = MAX (natural->width, required->width);
3596 g_object_unref (layout);
3600 gtk_label_get_preferred_size (GtkWidget *widget,
3601 GtkOrientation orientation,
3605 GtkLabel *label = GTK_LABEL (widget);
3606 GtkLabelPrivate *priv = label->priv;
3607 PangoRectangle required_rect;
3608 PangoRectangle natural_rect;
3611 gtk_label_get_preferred_layout_size (label, &required_rect, &natural_rect);
3613 /* Now that we have minimum and natural sizes in pango extents, apply a possible transform */
3614 if (priv->have_transform)
3617 PangoContext *context;
3618 const PangoMatrix *matrix;
3620 copy = pango_layout_copy (priv->layout);
3621 context = pango_layout_get_context (copy);
3622 matrix = pango_context_get_matrix (context);
3624 pango_layout_set_width (copy, -1);
3625 pango_layout_set_ellipsize (copy, PANGO_ELLIPSIZE_NONE);
3627 pango_layout_get_extents (copy, NULL, &natural_rect);
3628 g_object_unref (copy);
3630 pango_matrix_transform_rectangle (matrix, &required_rect);
3631 pango_matrix_transform_rectangle (matrix, &natural_rect);
3633 /* Bump the natural size in case of ellipsize to ensure pango has
3634 * enough space in the angles (note, we could alternatively set the
3635 * layout to not ellipsize when we know we have been allocated our
3636 * full natural size, or it may be that pango needs a fix here).
3638 if (priv->ellipsize && priv->angle != 0 && priv->angle != 90 &&
3639 priv->angle != 180 && priv->angle != 270 && priv->angle != 360)
3641 /* For some reason we only need this at about 110 degrees, and only
3642 * when gaining in height
3644 natural_rect.height += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3645 natural_rect.width += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3649 required_rect.width = PANGO_PIXELS_CEIL (required_rect.width);
3650 required_rect.height = PANGO_PIXELS_CEIL (required_rect.height);
3652 natural_rect.width = PANGO_PIXELS_CEIL (natural_rect.width);
3653 natural_rect.height = PANGO_PIXELS_CEIL (natural_rect.height);
3655 _gtk_misc_get_padding_and_border (GTK_MISC (label), &border);
3657 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3659 /* Note, we cant use get_size_for_allocation() when rotating
3660 * ellipsized labels.
3662 if (!(priv->ellipsize && priv->have_transform) &&
3663 (priv->angle == 90 || priv->angle == 270))
3665 /* Doing a h4w request on a rotated label here, return the
3666 * required width for the minimum height.
3668 get_size_for_allocation (label,
3669 GTK_ORIENTATION_VERTICAL,
3670 required_rect.height,
3671 minimum_size, natural_size);
3676 /* Normal desired width */
3677 *minimum_size = required_rect.width;
3678 *natural_size = natural_rect.width;
3681 *minimum_size += border.left + border.right;
3682 *natural_size += border.left + border.right;
3684 else /* GTK_ORIENTATION_VERTICAL */
3686 /* Note, we cant use get_size_for_allocation() when rotating
3687 * ellipsized labels.
3689 if (!(priv->ellipsize && priv->have_transform) &&
3690 (priv->angle == 0 || priv->angle == 180 || priv->angle == 360))
3692 /* Doing a w4h request on a label here, return the required
3693 * height for the minimum width.
3695 get_size_for_allocation (label,
3696 GTK_ORIENTATION_HORIZONTAL,
3697 required_rect.width,
3698 minimum_size, natural_size);
3702 /* A vertically rotated label does w4h, so return the base
3703 * desired height (text length)
3705 *minimum_size = required_rect.height;
3706 *natural_size = natural_rect.height;
3709 *minimum_size += border.top + border.bottom;
3710 *natural_size += border.top + border.bottom;
3715 gtk_label_get_preferred_width (GtkWidget *widget,
3719 gtk_label_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
3723 gtk_label_get_preferred_height (GtkWidget *widget,
3727 gtk_label_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
3731 gtk_label_get_preferred_width_for_height (GtkWidget *widget,
3733 gint *minimum_width,
3734 gint *natural_width)
3736 GtkLabel *label = GTK_LABEL (widget);
3737 GtkLabelPrivate *priv = label->priv;
3739 if (priv->wrap && (priv->angle == 90 || priv->angle == 270))
3743 _gtk_misc_get_padding_and_border (GTK_MISC (label), &border);
3746 gtk_label_clear_layout (label);
3748 get_size_for_allocation (label, GTK_ORIENTATION_VERTICAL,
3749 MAX (1, height - border.top - border.bottom),
3750 minimum_width, natural_width);
3753 *minimum_width += border.right + border.left;
3756 *natural_width += border.right + border.left;
3759 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_width, natural_width);
3763 gtk_label_get_preferred_height_for_width (GtkWidget *widget,
3765 gint *minimum_height,
3766 gint *natural_height)
3768 GtkLabel *label = GTK_LABEL (widget);
3769 GtkLabelPrivate *priv = label->priv;
3771 if (priv->wrap && (priv->angle == 0 || priv->angle == 180 || priv->angle == 360))
3775 _gtk_misc_get_padding_and_border (GTK_MISC (label), &border);
3778 gtk_label_clear_layout (label);
3780 get_size_for_allocation (label, GTK_ORIENTATION_HORIZONTAL,
3781 MAX (1, width - border.left - border.right),
3782 minimum_height, natural_height);
3785 *minimum_height += border.top + border.bottom;
3788 *natural_height += border.top + border.bottom;
3791 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, minimum_height, natural_height);
3795 gtk_label_size_allocate (GtkWidget *widget,
3796 GtkAllocation *allocation)
3798 GtkLabel *label = GTK_LABEL (widget);
3799 GtkLabelPrivate *priv = label->priv;
3801 GTK_WIDGET_CLASS (gtk_label_parent_class)->size_allocate (widget, allocation);
3804 gtk_label_update_layout_width (label);
3806 if (priv->select_info && priv->select_info->window)
3808 gdk_window_move_resize (priv->select_info->window,
3812 allocation->height);
3817 gtk_label_update_cursor (GtkLabel *label)
3819 GtkLabelPrivate *priv = label->priv;
3822 if (!priv->select_info)
3825 widget = GTK_WIDGET (label);
3827 if (gtk_widget_get_realized (widget))
3829 GdkDisplay *display;
3832 if (gtk_widget_is_sensitive (widget))
3834 display = gtk_widget_get_display (widget);
3836 if (priv->select_info->active_link)
3837 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
3838 else if (priv->select_info->selectable)
3839 cursor = gdk_cursor_new_for_display (display, GDK_XTERM);
3846 gdk_window_set_cursor (priv->select_info->window, cursor);
3849 g_object_unref (cursor);
3854 gtk_label_state_flags_changed (GtkWidget *widget,
3855 GtkStateFlags prev_state)
3857 GtkLabel *label = GTK_LABEL (widget);
3858 GtkLabelPrivate *priv = label->priv;
3860 if (priv->select_info)
3862 if (!gtk_widget_is_sensitive (widget))
3863 gtk_label_select_region (label, 0, 0);
3865 gtk_label_update_cursor (label);
3868 /* We have to clear the layout, fonts etc. may have changed */
3869 gtk_label_clear_layout (label);
3871 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->state_flags_changed)
3872 GTK_WIDGET_CLASS (gtk_label_parent_class)->state_flags_changed (widget, prev_state);
3876 gtk_label_style_updated (GtkWidget *widget)
3878 GtkLabel *label = GTK_LABEL (widget);
3880 GTK_WIDGET_CLASS (gtk_label_parent_class)->style_updated (widget);
3882 /* We have to clear the layout, fonts etc. may have changed */
3883 gtk_label_clear_layout (label);
3887 gtk_label_direction_changed (GtkWidget *widget,
3888 GtkTextDirection previous_dir)
3890 GtkLabel *label = GTK_LABEL (widget);
3891 GtkLabelPrivate *priv = label->priv;
3894 pango_layout_context_changed (priv->layout);
3896 GTK_WIDGET_CLASS (gtk_label_parent_class)->direction_changed (widget, previous_dir);
3900 get_layout_location (GtkLabel *label,
3904 GtkAllocation allocation;
3907 GtkLabelPrivate *priv;
3909 gint req_width, x, y;
3911 gfloat xalign, yalign;
3912 PangoRectangle logical;
3914 misc = GTK_MISC (label);
3915 widget = GTK_WIDGET (label);
3918 gtk_misc_get_alignment (misc, &xalign, &yalign);
3919 _gtk_misc_get_padding_and_border (GTK_MISC (label), &border);
3921 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
3922 xalign = 1.0 - xalign;
3924 pango_layout_get_extents (priv->layout, NULL, &logical);
3926 if (priv->have_transform)
3928 PangoContext *context = gtk_widget_get_pango_context (widget);
3929 const PangoMatrix *matrix = pango_context_get_matrix (context);
3930 pango_matrix_transform_rectangle (matrix, &logical);
3933 pango_extents_to_pixels (&logical, NULL);
3935 req_width = logical.width;
3936 req_height = logical.height;
3938 req_width += border.left + border.right;
3939 req_height += border.top + border.bottom;
3941 gtk_widget_get_allocation (widget, &allocation);
3943 x = floor (allocation.x + border.left + xalign * (allocation.width - req_width) - logical.x);
3945 /* bgo#315462 - For single-line labels, *do* align the requisition with
3946 * respect to the allocation, even if we are under-allocated. For multi-line
3947 * labels, always show the top of the text when they are under-allocated. The
3948 * rationale is this:
3950 * - Single-line labels appear in GtkButtons, and it is very easy to get them
3951 * to be smaller than their requisition. The button may clip the label, but
3952 * the label will still be able to show most of itself and the focus
3953 * rectangle. Also, it is fairly easy to read a single line of clipped text.
3955 * - Multi-line labels should not be clipped to showing "something in the
3956 * middle". You want to read the first line, at least, to get some context.
3958 if (pango_layout_get_line_count (priv->layout) == 1)
3959 y = floor (allocation.y + border.top + (allocation.height - req_height) * yalign) - logical.y;
3961 y = floor (allocation.y + border.top + MAX ((allocation.height - req_height) * yalign, 0)) - logical.y;
3970 static PangoDirection
3971 get_cursor_direction (GtkLabel *label)
3973 GtkLabelPrivate *priv = label->priv;
3976 g_assert (priv->select_info);
3978 gtk_label_ensure_layout (label);
3980 for (l = pango_layout_get_lines_readonly (priv->layout); l; l = l->next)
3982 PangoLayoutLine *line = l->data;
3984 /* If priv->select_info->selection_end is at the very end of
3985 * the line, we don't know if the cursor is on this line or
3986 * the next without looking ahead at the next line. (End
3987 * of paragraph is different from line break.) But it's
3988 * definitely in this paragraph, which is good enough
3989 * to figure out the resolved direction.
3991 if (line->start_index + line->length >= priv->select_info->selection_end)
3992 return line->resolved_dir;
3995 return PANGO_DIRECTION_LTR;
3998 static GtkLabelLink *
3999 gtk_label_get_focus_link (GtkLabel *label)
4001 GtkLabelPrivate *priv = label->priv;
4002 GtkLabelSelectionInfo *info = priv->select_info;
4008 if (info->selection_anchor != info->selection_end)
4011 for (l = info->links; l; l = l->next)
4013 GtkLabelLink *link = l->data;
4014 if (link->start <= info->selection_anchor &&
4015 info->selection_anchor <= link->end)
4023 gtk_label_draw (GtkWidget *widget,
4026 GtkLabel *label = GTK_LABEL (widget);
4027 GtkLabelPrivate *priv = label->priv;
4028 GtkLabelSelectionInfo *info = priv->select_info;
4029 GtkAllocation allocation;
4030 GtkStyleContext *context;
4031 GtkStateFlags state;
4034 gtk_label_ensure_layout (label);
4036 if (priv->text && (*priv->text != '\0'))
4038 get_layout_location (label, &x, &y);
4040 context = gtk_widget_get_style_context (widget);
4041 gtk_widget_get_allocation (widget, &allocation);
4043 cairo_translate (cr, -allocation.x, -allocation.y);
4045 gtk_render_layout (context, cr,
4049 state = gtk_widget_get_state_flags (widget);
4052 (info->selection_anchor != info->selection_end))
4055 cairo_region_t *clip;
4056 GdkRGBA bg_color, fg_color;
4058 range[0] = info->selection_anchor;
4059 range[1] = info->selection_end;
4061 if (range[0] > range[1])
4063 gint tmp = range[0];
4064 range[0] = range[1];
4068 clip = gdk_pango_layout_get_clip_region (priv->layout,
4073 /* FIXME should use gtk_paint, but it can't use a clip region */
4076 gdk_cairo_region (cr, clip);
4079 state |= GTK_STATE_FLAG_SELECTED;
4081 gtk_style_context_get_color (context, state, &fg_color);
4082 gtk_style_context_get_background_color (context, state, &bg_color);
4084 gdk_cairo_set_source_rgba (cr, &bg_color);
4087 gdk_cairo_set_source_rgba (cr, &fg_color);
4088 cairo_move_to (cr, x, y);
4089 _gtk_pango_fill_layout (cr, priv->layout);
4092 cairo_region_destroy (clip);
4096 GtkLabelLink *focus_link;
4097 GtkLabelLink *active_link;
4099 cairo_region_t *clip;
4101 GdkColor *text_color;
4102 GdkColor link_color;
4103 GdkColor visited_link_color;
4105 if (info->selectable &&
4106 gtk_widget_has_focus (widget) &&
4107 gtk_widget_is_drawable (widget))
4109 PangoDirection cursor_direction;
4111 cursor_direction = get_cursor_direction (label);
4112 gtk_render_insertion_cursor (context, cr,
4114 priv->layout, priv->select_info->selection_end,
4118 focus_link = gtk_label_get_focus_link (label);
4119 active_link = info->active_link;
4125 range[0] = active_link->start;
4126 range[1] = active_link->end;
4130 clip = gdk_pango_layout_get_clip_region (priv->layout,
4134 gdk_cairo_region (cr, clip);
4136 cairo_region_destroy (clip);
4138 gtk_label_get_link_colors (widget, &link_color, &visited_link_color);
4139 if (active_link->visited)
4140 text_color = &visited_link_color;
4142 text_color = &link_color;
4144 if (info->link_clicked)
4145 state |= GTK_STATE_FLAG_ACTIVE;
4147 state |= GTK_STATE_FLAG_PRELIGHT;
4149 gtk_style_context_get_background_color (context, state, &bg_color);
4151 gdk_cairo_set_source_rgba (cr, &bg_color);
4154 cairo_set_source_rgb (cr, text_color->red / 65535.,
4155 text_color->green / 65535.,
4156 text_color->blue / 65535.);
4157 cairo_move_to (cr, x, y);
4158 _gtk_pango_fill_layout (cr, priv->layout);
4163 if (focus_link && gtk_widget_has_visible_focus (widget))
4165 range[0] = focus_link->start;
4166 range[1] = focus_link->end;
4168 clip = gdk_pango_layout_get_clip_region (priv->layout,
4172 cairo_region_get_extents (clip, &rect);
4174 gtk_render_focus (context, cr,
4176 rect.width, rect.height);
4178 cairo_region_destroy (clip);
4187 separate_uline_pattern (const gchar *str,
4192 gboolean underscore;
4195 gchar *pattern_dest;
4197 *accel_key = GDK_KEY_VoidSymbol;
4198 *new_str = g_new (gchar, strlen (str) + 1);
4199 *pattern = g_new (gchar, g_utf8_strlen (str, -1) + 1);
4205 pattern_dest = *pattern;
4210 const gchar *next_src;
4212 c = g_utf8_get_char (src);
4213 if (c == (gunichar)-1)
4215 g_warning ("Invalid input string");
4221 next_src = g_utf8_next_char (src);
4226 *pattern_dest++ = ' ';
4229 *pattern_dest++ = '_';
4230 if (*accel_key == GDK_KEY_VoidSymbol)
4231 *accel_key = gdk_keyval_to_lower (gdk_unicode_to_keyval (c));
4234 while (src < next_src)
4248 while (src < next_src)
4251 *pattern_dest++ = ' ';
4263 gtk_label_set_uline_text_internal (GtkLabel *label,
4266 GtkLabelPrivate *priv = label->priv;
4267 guint accel_key = GDK_KEY_VoidSymbol;
4271 g_return_if_fail (GTK_IS_LABEL (label));
4272 g_return_if_fail (str != NULL);
4274 /* Split text into the base text and a separate pattern
4277 if (!separate_uline_pattern (str, &accel_key, &new_str, &pattern))
4280 gtk_label_set_text_internal (label, new_str);
4281 gtk_label_set_pattern_internal (label, pattern, TRUE);
4282 priv->mnemonic_keyval = accel_key;
4288 * gtk_label_set_text_with_mnemonic:
4289 * @label: a #GtkLabel
4292 * Sets the label's text from the string @str.
4293 * If characters in @str are preceded by an underscore, they are underlined
4294 * indicating that they represent a keyboard accelerator called a mnemonic.
4295 * The mnemonic key can be used to activate another widget, chosen
4296 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
4299 gtk_label_set_text_with_mnemonic (GtkLabel *label,
4302 g_return_if_fail (GTK_IS_LABEL (label));
4303 g_return_if_fail (str != NULL);
4305 g_object_freeze_notify (G_OBJECT (label));
4307 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
4308 gtk_label_set_use_markup_internal (label, FALSE);
4309 gtk_label_set_use_underline_internal (label, TRUE);
4311 gtk_label_recalculate (label);
4313 g_object_thaw_notify (G_OBJECT (label));
4317 gtk_label_realize (GtkWidget *widget)
4319 GtkLabel *label = GTK_LABEL (widget);
4320 GtkLabelPrivate *priv = label->priv;
4322 GTK_WIDGET_CLASS (gtk_label_parent_class)->realize (widget);
4324 if (priv->select_info)
4325 gtk_label_create_window (label);
4329 gtk_label_unrealize (GtkWidget *widget)
4331 GtkLabel *label = GTK_LABEL (widget);
4332 GtkLabelPrivate *priv = label->priv;
4334 if (priv->select_info)
4335 gtk_label_destroy_window (label);
4337 GTK_WIDGET_CLASS (gtk_label_parent_class)->unrealize (widget);
4341 gtk_label_map (GtkWidget *widget)
4343 GtkLabel *label = GTK_LABEL (widget);
4344 GtkLabelPrivate *priv = label->priv;
4346 GTK_WIDGET_CLASS (gtk_label_parent_class)->map (widget);
4348 if (priv->select_info)
4349 gdk_window_show (priv->select_info->window);
4353 gtk_label_unmap (GtkWidget *widget)
4355 GtkLabel *label = GTK_LABEL (widget);
4356 GtkLabelPrivate *priv = label->priv;
4358 if (priv->select_info)
4359 gdk_window_hide (priv->select_info->window);
4361 GTK_WIDGET_CLASS (gtk_label_parent_class)->unmap (widget);
4365 window_to_layout_coords (GtkLabel *label,
4369 GtkAllocation allocation;
4373 widget = GTK_WIDGET (label);
4375 /* get layout location in widget->window coords */
4376 get_layout_location (label, &lx, &ly);
4378 gtk_widget_get_allocation (widget, &allocation);
4382 *x += allocation.x; /* go to widget->window */
4383 *x -= lx; /* go to layout */
4388 *y += allocation.y; /* go to widget->window */
4389 *y -= ly; /* go to layout */
4395 layout_to_window_coords (GtkLabel *label,
4402 widget = GTK_WIDGET (label);
4404 /* get layout location in widget->window coords */
4405 get_layout_location (label, &lx, &ly);
4409 *x += lx; /* go to widget->window */
4410 *x -= widget->allocation.x; /* go to selection window */
4415 *y += ly; /* go to widget->window */
4416 *y -= widget->allocation.y; /* go to selection window */
4422 get_layout_index (GtkLabel *label,
4427 GtkLabelPrivate *priv = label->priv;
4429 const gchar *cluster;
4430 const gchar *cluster_end;
4435 gtk_label_ensure_layout (label);
4437 window_to_layout_coords (label, &x, &y);
4442 inside = pango_layout_xy_to_index (priv->layout,
4446 cluster = priv->text + *index;
4447 cluster_end = cluster;
4450 cluster_end = g_utf8_next_char (cluster_end);
4454 *index += (cluster_end - cluster);
4460 gtk_label_select_word (GtkLabel *label)
4462 GtkLabelPrivate *priv = label->priv;
4465 gint start_index = gtk_label_move_backward_word (label, priv->select_info->selection_end);
4466 gint end_index = gtk_label_move_forward_word (label, priv->select_info->selection_end);
4468 min = MIN (priv->select_info->selection_anchor,
4469 priv->select_info->selection_end);
4470 max = MAX (priv->select_info->selection_anchor,
4471 priv->select_info->selection_end);
4473 min = MIN (min, start_index);
4474 max = MAX (max, end_index);
4476 gtk_label_select_region_index (label, min, max);
4480 gtk_label_grab_focus (GtkWidget *widget)
4482 GtkLabel *label = GTK_LABEL (widget);
4483 GtkLabelPrivate *priv = label->priv;
4484 gboolean select_on_focus;
4487 if (priv->select_info == NULL)
4490 GTK_WIDGET_CLASS (gtk_label_parent_class)->grab_focus (widget);
4492 if (priv->select_info->selectable)
4494 g_object_get (gtk_widget_get_settings (widget),
4495 "gtk-label-select-on-focus",
4499 if (select_on_focus && !priv->in_click)
4500 gtk_label_select_region (label, 0, -1);
4504 if (priv->select_info->links && !priv->in_click)
4506 link = priv->select_info->links->data;
4507 priv->select_info->selection_anchor = link->start;
4508 priv->select_info->selection_end = link->start;
4514 gtk_label_focus (GtkWidget *widget,
4515 GtkDirectionType direction)
4517 GtkLabel *label = GTK_LABEL (widget);
4518 GtkLabelPrivate *priv = label->priv;
4519 GtkLabelSelectionInfo *info = priv->select_info;
4520 GtkLabelLink *focus_link;
4523 if (!gtk_widget_is_focus (widget))
4525 gtk_widget_grab_focus (widget);
4528 focus_link = gtk_label_get_focus_link (label);
4529 if (focus_link && direction == GTK_DIR_TAB_BACKWARD)
4531 l = g_list_last (info->links);
4532 focus_link = l->data;
4533 info->selection_anchor = focus_link->start;
4534 info->selection_end = focus_link->start;
4544 if (info->selectable)
4548 if (info->selection_anchor != info->selection_end)
4551 index = info->selection_anchor;
4553 if (direction == GTK_DIR_TAB_FORWARD)
4554 for (l = info->links; l; l = l->next)
4556 GtkLabelLink *link = l->data;
4558 if (link->start > index)
4560 gtk_label_select_region_index (label, link->start, link->start);
4564 else if (direction == GTK_DIR_TAB_BACKWARD)
4565 for (l = g_list_last (info->links); l; l = l->prev)
4567 GtkLabelLink *link = l->data;
4569 if (link->end < index)
4571 gtk_label_select_region_index (label, link->start, link->start);
4580 focus_link = gtk_label_get_focus_link (label);
4583 case GTK_DIR_TAB_FORWARD:
4586 l = g_list_find (info->links, focus_link);
4593 case GTK_DIR_TAB_BACKWARD:
4596 l = g_list_find (info->links, focus_link);
4600 l = g_list_last (info->links);
4609 focus_link = l->data;
4610 info->selection_anchor = focus_link->start;
4611 info->selection_end = focus_link->start;
4612 gtk_widget_queue_draw (widget);
4624 gtk_label_button_press (GtkWidget *widget,
4625 GdkEventButton *event)
4627 GtkLabel *label = GTK_LABEL (widget);
4628 GtkLabelPrivate *priv = label->priv;
4629 GtkLabelSelectionInfo *info = priv->select_info;
4636 if (info->active_link)
4638 if (gdk_event_triggers_context_menu ((GdkEvent *) event))
4640 info->link_clicked = 1;
4641 gtk_label_do_popup (label, event);
4644 else if (event->button == GDK_BUTTON_PRIMARY)
4646 info->link_clicked = 1;
4647 gtk_widget_queue_draw (widget);
4651 if (!info->selectable)
4654 info->in_drag = FALSE;
4655 info->select_words = FALSE;
4657 if (gdk_event_triggers_context_menu ((GdkEvent *) event))
4659 gtk_label_do_popup (label, event);
4663 else if (event->button == GDK_BUTTON_PRIMARY)
4665 if (!gtk_widget_has_focus (widget))
4667 priv->in_click = TRUE;
4668 gtk_widget_grab_focus (widget);
4669 priv->in_click = FALSE;
4672 if (event->type == GDK_3BUTTON_PRESS)
4674 gtk_label_select_region_index (label, 0, strlen (priv->text));
4678 if (event->type == GDK_2BUTTON_PRESS)
4680 info->select_words = TRUE;
4681 gtk_label_select_word (label);
4685 get_layout_index (label, event->x, event->y, &index);
4687 min = MIN (info->selection_anchor, info->selection_end);
4688 max = MAX (info->selection_anchor, info->selection_end);
4690 if ((info->selection_anchor != info->selection_end) &&
4691 (event->state & GDK_SHIFT_MASK))
4693 if (index > min && index < max)
4695 /* truncate selection, but keep it as big as possible */
4696 if (index - min > max - index)
4703 /* extend (same as motion) */
4704 min = MIN (min, index);
4705 max = MAX (max, index);
4708 /* ensure the anchor is opposite index */
4716 gtk_label_select_region_index (label, min, max);
4720 if (event->type == GDK_3BUTTON_PRESS)
4721 gtk_label_select_region_index (label, 0, strlen (priv->text));
4722 else if (event->type == GDK_2BUTTON_PRESS)
4723 gtk_label_select_word (label);
4724 else if (min < max && min <= index && index <= max)
4726 info->in_drag = TRUE;
4727 info->drag_start_x = event->x;
4728 info->drag_start_y = event->y;
4731 /* start a replacement */
4732 gtk_label_select_region_index (label, index, index);
4742 gtk_label_button_release (GtkWidget *widget,
4743 GdkEventButton *event)
4746 GtkLabel *label = GTK_LABEL (widget);
4747 GtkLabelPrivate *priv = label->priv;
4748 GtkLabelSelectionInfo *info = priv->select_info;
4758 get_layout_index (label, event->x, event->y, &index);
4759 gtk_label_select_region_index (label, index, index);
4764 if (event->button != GDK_BUTTON_PRIMARY)
4767 if (info->active_link &&
4768 info->selection_anchor == info->selection_end &&
4771 emit_activate_link (label, info->active_link);
4772 info->link_clicked = 0;
4777 /* The goal here is to return TRUE iff we ate the
4778 * button press to start selecting.
4784 connect_mnemonics_visible_notify (GtkLabel *label)
4786 GtkLabelPrivate *priv = label->priv;
4787 GtkWidget *toplevel;
4790 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
4792 if (!GTK_IS_WINDOW (toplevel))
4795 /* always set up this widgets initial value */
4796 priv->mnemonics_visible =
4797 gtk_window_get_mnemonics_visible (GTK_WINDOW (toplevel));
4800 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (toplevel),
4801 "gtk-label-mnemonics-visible-connected"));
4805 g_signal_connect (toplevel,
4806 "notify::mnemonics-visible",
4807 G_CALLBACK (label_mnemonics_visible_changed),
4809 g_object_set_data (G_OBJECT (toplevel),
4810 "gtk-label-mnemonics-visible-connected",
4811 GINT_TO_POINTER (1));
4816 drag_begin_cb (GtkWidget *widget,
4817 GdkDragContext *context,
4820 GtkLabel *label = GTK_LABEL (widget);
4821 GtkLabelPrivate *priv = label->priv;
4822 cairo_surface_t *surface = NULL;
4824 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
4826 if ((priv->select_info->selection_anchor !=
4827 priv->select_info->selection_end) &&
4833 start = MIN (priv->select_info->selection_anchor,
4834 priv->select_info->selection_end);
4835 end = MAX (priv->select_info->selection_anchor,
4836 priv->select_info->selection_end);
4838 len = strlen (priv->text);
4846 surface = _gtk_text_util_create_drag_icon (widget,
4853 gtk_drag_set_icon_surface (context, surface);
4854 cairo_surface_destroy (surface);
4858 gtk_drag_set_icon_default (context);
4863 gtk_label_motion (GtkWidget *widget,
4864 GdkEventMotion *event)
4866 GtkLabel *label = GTK_LABEL (widget);
4867 GtkLabelPrivate *priv = label->priv;
4868 GtkLabelSelectionInfo *info = priv->select_info;
4874 if (info->links && !info->in_drag)
4878 gboolean found = FALSE;
4880 if (info->selection_anchor == info->selection_end)
4882 if (get_layout_index (label, event->x, event->y, &index))
4884 for (l = info->links; l != NULL; l = l->next)
4887 if (index >= link->start && index <= link->end)
4898 if (info->active_link != link)
4900 info->link_clicked = 0;
4901 info->active_link = link;
4902 gtk_label_update_cursor (label);
4903 gtk_widget_queue_draw (widget);
4908 if (info->active_link != NULL)
4910 info->link_clicked = 0;
4911 info->active_link = NULL;
4912 gtk_label_update_cursor (label);
4913 gtk_widget_queue_draw (widget);
4918 if (!info->selectable)
4921 if ((event->state & GDK_BUTTON1_MASK) == 0)
4926 if (gtk_drag_check_threshold (widget,
4929 event->x, event->y))
4931 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
4933 gtk_target_list_add_text_targets (target_list, 0);
4935 g_signal_connect (widget, "drag-begin",
4936 G_CALLBACK (drag_begin_cb), NULL);
4937 gtk_drag_begin (widget, target_list,
4939 1, (GdkEvent *)event);
4941 info->in_drag = FALSE;
4943 gtk_target_list_unref (target_list);
4950 gdk_window_get_device_position (info->window, event->device, &x, &y, NULL);
4951 get_layout_index (label, x, y, &index);
4953 if (info->select_words)
4956 gint old_min, old_max;
4959 min = gtk_label_move_backward_word (label, index);
4960 max = gtk_label_move_forward_word (label, index);
4962 anchor = info->selection_anchor;
4963 end = info->selection_end;
4965 old_min = MIN (anchor, end);
4966 old_max = MAX (anchor, end);
4973 else if (old_max < max)
4978 else if (anchor == old_min)
4989 gtk_label_select_region_index (label, anchor, end);
4992 gtk_label_select_region_index (label, info->selection_anchor, index);
4999 gtk_label_leave_notify (GtkWidget *widget,
5000 GdkEventCrossing *event)
5002 GtkLabel *label = GTK_LABEL (widget);
5003 GtkLabelPrivate *priv = label->priv;
5005 if (priv->select_info)
5007 priv->select_info->active_link = NULL;
5008 gtk_label_update_cursor (label);
5009 gtk_widget_queue_draw (widget);
5012 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event)
5013 return GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event (widget, event);
5019 gtk_label_create_window (GtkLabel *label)
5021 GtkLabelPrivate *priv = label->priv;
5022 GtkAllocation allocation;
5024 GdkWindowAttr attributes;
5025 gint attributes_mask;
5027 g_assert (priv->select_info);
5028 widget = GTK_WIDGET (label);
5029 g_assert (gtk_widget_get_realized (widget));
5031 if (priv->select_info->window)
5034 gtk_widget_get_allocation (widget, &allocation);
5036 attributes.x = allocation.x;
5037 attributes.y = allocation.y;
5038 attributes.width = allocation.width;
5039 attributes.height = allocation.height;
5040 attributes.window_type = GDK_WINDOW_CHILD;
5041 attributes.wclass = GDK_INPUT_ONLY;
5042 attributes.override_redirect = TRUE;
5043 attributes.event_mask = gtk_widget_get_events (widget) |
5044 GDK_BUTTON_PRESS_MASK |
5045 GDK_BUTTON_RELEASE_MASK |
5046 GDK_LEAVE_NOTIFY_MASK |
5047 GDK_BUTTON_MOTION_MASK |
5048 GDK_POINTER_MOTION_MASK |
5049 GDK_POINTER_MOTION_HINT_MASK;
5050 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
5051 if (gtk_widget_is_sensitive (widget))
5053 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
5055 attributes_mask |= GDK_WA_CURSOR;
5059 priv->select_info->window = gdk_window_new (gtk_widget_get_window (widget),
5060 &attributes, attributes_mask);
5061 gdk_window_set_user_data (priv->select_info->window, widget);
5063 if (attributes_mask & GDK_WA_CURSOR)
5064 g_object_unref (attributes.cursor);
5068 gtk_label_destroy_window (GtkLabel *label)
5070 GtkLabelPrivate *priv = label->priv;
5072 g_assert (priv->select_info);
5074 if (priv->select_info->window == NULL)
5077 gdk_window_set_user_data (priv->select_info->window, NULL);
5078 gdk_window_destroy (priv->select_info->window);
5079 priv->select_info->window = NULL;
5083 gtk_label_ensure_select_info (GtkLabel *label)
5085 GtkLabelPrivate *priv = label->priv;
5087 if (priv->select_info == NULL)
5089 priv->select_info = g_new0 (GtkLabelSelectionInfo, 1);
5091 gtk_widget_set_can_focus (GTK_WIDGET (label), TRUE);
5093 if (gtk_widget_get_realized (GTK_WIDGET (label)))
5094 gtk_label_create_window (label);
5096 if (gtk_widget_get_mapped (GTK_WIDGET (label)))
5097 gdk_window_show (priv->select_info->window);
5102 gtk_label_clear_select_info (GtkLabel *label)
5104 GtkLabelPrivate *priv = label->priv;
5106 if (priv->select_info == NULL)
5109 if (!priv->select_info->selectable && !priv->select_info->links)
5111 gtk_label_destroy_window (label);
5113 g_free (priv->select_info);
5114 priv->select_info = NULL;
5116 gtk_widget_set_can_focus (GTK_WIDGET (label), FALSE);
5121 * gtk_label_set_selectable:
5122 * @label: a #GtkLabel
5123 * @setting: %TRUE to allow selecting text in the label
5125 * Selectable labels allow the user to select text from the label, for
5129 gtk_label_set_selectable (GtkLabel *label,
5132 GtkLabelPrivate *priv;
5133 gboolean old_setting;
5135 g_return_if_fail (GTK_IS_LABEL (label));
5139 setting = setting != FALSE;
5140 old_setting = priv->select_info && priv->select_info->selectable;
5144 gtk_label_ensure_select_info (label);
5145 priv->select_info->selectable = TRUE;
5146 gtk_label_update_cursor (label);
5152 /* unselect, to give up the selection */
5153 gtk_label_select_region (label, 0, 0);
5155 priv->select_info->selectable = FALSE;
5156 gtk_label_clear_select_info (label);
5157 gtk_label_update_cursor (label);
5160 if (setting != old_setting)
5162 g_object_freeze_notify (G_OBJECT (label));
5163 g_object_notify (G_OBJECT (label), "selectable");
5164 g_object_notify (G_OBJECT (label), "cursor-position");
5165 g_object_notify (G_OBJECT (label), "selection-bound");
5166 g_object_thaw_notify (G_OBJECT (label));
5167 gtk_widget_queue_draw (GTK_WIDGET (label));
5172 * gtk_label_get_selectable:
5173 * @label: a #GtkLabel
5175 * Gets the value set by gtk_label_set_selectable().
5177 * Return value: %TRUE if the user can copy text from the label
5180 gtk_label_get_selectable (GtkLabel *label)
5182 GtkLabelPrivate *priv;
5184 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5188 return priv->select_info && priv->select_info->selectable;
5192 * gtk_label_set_angle:
5193 * @label: a #GtkLabel
5194 * @angle: the angle that the baseline of the label makes with
5195 * the horizontal, in degrees, measured counterclockwise
5197 * Sets the angle of rotation for the label. An angle of 90 reads from
5198 * from bottom to top, an angle of 270, from top to bottom. The angle
5199 * setting for the label is ignored if the label is selectable,
5200 * wrapped, or ellipsized.
5205 gtk_label_set_angle (GtkLabel *label,
5208 GtkLabelPrivate *priv;
5210 g_return_if_fail (GTK_IS_LABEL (label));
5214 /* Canonicalize to [0,360]. We don't canonicalize 360 to 0, because
5215 * double property ranges are inclusive, and changing 360 to 0 would
5216 * make a property editor behave strangely.
5218 if (angle < 0 || angle > 360.0)
5219 angle = angle - 360. * floor (angle / 360.);
5221 if (priv->angle != angle)
5223 priv->angle = angle;
5225 gtk_label_clear_layout (label);
5226 gtk_widget_queue_resize (GTK_WIDGET (label));
5228 g_object_notify (G_OBJECT (label), "angle");
5233 * gtk_label_get_angle:
5234 * @label: a #GtkLabel
5236 * Gets the angle of rotation for the label. See
5237 * gtk_label_set_angle().
5239 * Return value: the angle of rotation for the label
5244 gtk_label_get_angle (GtkLabel *label)
5246 g_return_val_if_fail (GTK_IS_LABEL (label), 0.0);
5248 return label->priv->angle;
5252 gtk_label_set_selection_text (GtkLabel *label,
5253 GtkSelectionData *selection_data)
5255 GtkLabelPrivate *priv = label->priv;
5257 if (priv->select_info &&
5258 (priv->select_info->selection_anchor !=
5259 priv->select_info->selection_end) &&
5265 start = MIN (priv->select_info->selection_anchor,
5266 priv->select_info->selection_end);
5267 end = MAX (priv->select_info->selection_anchor,
5268 priv->select_info->selection_end);
5270 len = strlen (priv->text);
5278 gtk_selection_data_set_text (selection_data,
5285 gtk_label_drag_data_get (GtkWidget *widget,
5286 GdkDragContext *context,
5287 GtkSelectionData *selection_data,
5291 gtk_label_set_selection_text (GTK_LABEL (widget), selection_data);
5295 get_text_callback (GtkClipboard *clipboard,
5296 GtkSelectionData *selection_data,
5298 gpointer user_data_or_owner)
5300 gtk_label_set_selection_text (GTK_LABEL (user_data_or_owner), selection_data);
5304 clear_text_callback (GtkClipboard *clipboard,
5305 gpointer user_data_or_owner)
5308 GtkLabelPrivate *priv;
5310 label = GTK_LABEL (user_data_or_owner);
5313 if (priv->select_info)
5315 priv->select_info->selection_anchor = priv->select_info->selection_end;
5317 gtk_widget_queue_draw (GTK_WIDGET (label));
5322 gtk_label_select_region_index (GtkLabel *label,
5326 GtkLabelPrivate *priv;
5328 g_return_if_fail (GTK_IS_LABEL (label));
5332 if (priv->select_info && priv->select_info->selectable)
5334 GtkClipboard *clipboard;
5336 if (priv->select_info->selection_anchor == anchor_index &&
5337 priv->select_info->selection_end == end_index)
5340 g_object_freeze_notify (G_OBJECT (label));
5342 if (priv->select_info->selection_anchor != anchor_index)
5343 g_object_notify (G_OBJECT (label), "selection-bound");
5344 if (priv->select_info->selection_end != end_index)
5345 g_object_notify (G_OBJECT (label), "cursor-position");
5347 priv->select_info->selection_anchor = anchor_index;
5348 priv->select_info->selection_end = end_index;
5350 if (gtk_widget_has_screen (GTK_WIDGET (label)))
5351 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label),
5352 GDK_SELECTION_PRIMARY);
5356 if (anchor_index != end_index)
5358 GtkTargetList *list;
5359 GtkTargetEntry *targets;
5362 list = gtk_target_list_new (NULL, 0);
5363 gtk_target_list_add_text_targets (list, 0);
5364 targets = gtk_target_table_new_from_list (list, &n_targets);
5367 gtk_clipboard_set_with_owner (clipboard,
5370 clear_text_callback,
5373 gtk_target_table_free (targets, n_targets);
5374 gtk_target_list_unref (list);
5379 gtk_clipboard_get_owner (clipboard) == G_OBJECT (label))
5380 gtk_clipboard_clear (clipboard);
5383 gtk_widget_queue_draw (GTK_WIDGET (label));
5385 g_object_thaw_notify (G_OBJECT (label));
5390 * gtk_label_select_region:
5391 * @label: a #GtkLabel
5392 * @start_offset: start offset (in characters not bytes)
5393 * @end_offset: end offset (in characters not bytes)
5395 * Selects a range of characters in the label, if the label is selectable.
5396 * See gtk_label_set_selectable(). If the label is not selectable,
5397 * this function has no effect. If @start_offset or
5398 * @end_offset are -1, then the end of the label will be substituted.
5401 gtk_label_select_region (GtkLabel *label,
5405 GtkLabelPrivate *priv;
5407 g_return_if_fail (GTK_IS_LABEL (label));
5411 if (priv->text && priv->select_info)
5413 if (start_offset < 0)
5414 start_offset = g_utf8_strlen (priv->text, -1);
5417 end_offset = g_utf8_strlen (priv->text, -1);
5419 gtk_label_select_region_index (label,
5420 g_utf8_offset_to_pointer (priv->text, start_offset) - priv->text,
5421 g_utf8_offset_to_pointer (priv->text, end_offset) - priv->text);
5426 * gtk_label_get_selection_bounds:
5427 * @label: a #GtkLabel
5428 * @start: (out): return location for start of selection, as a character offset
5429 * @end: (out): return location for end of selection, as a character offset
5431 * Gets the selected range of characters in the label, returning %TRUE
5432 * if there's a selection.
5434 * Return value: %TRUE if selection is non-empty
5437 gtk_label_get_selection_bounds (GtkLabel *label,
5441 GtkLabelPrivate *priv;
5443 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5447 if (priv->select_info == NULL)
5449 /* not a selectable label */
5459 gint start_index, end_index;
5460 gint start_offset, end_offset;
5463 start_index = MIN (priv->select_info->selection_anchor,
5464 priv->select_info->selection_end);
5465 end_index = MAX (priv->select_info->selection_anchor,
5466 priv->select_info->selection_end);
5468 len = strlen (priv->text);
5470 if (end_index > len)
5473 if (start_index > len)
5476 start_offset = g_utf8_strlen (priv->text, start_index);
5477 end_offset = g_utf8_strlen (priv->text, end_index);
5479 if (start_offset > end_offset)
5481 gint tmp = start_offset;
5482 start_offset = end_offset;
5487 *start = start_offset;
5492 return start_offset != end_offset;
5498 * gtk_label_get_layout:
5499 * @label: a #GtkLabel
5501 * Gets the #PangoLayout used to display the label.
5502 * The layout is useful to e.g. convert text positions to
5503 * pixel positions, in combination with gtk_label_get_layout_offsets().
5504 * The returned layout is owned by the @label so need not be
5505 * freed by the caller. The @label is free to recreate its layout at
5506 * any time, so it should be considered read-only.
5508 * Return value: (transfer none): the #PangoLayout for this label
5511 gtk_label_get_layout (GtkLabel *label)
5513 GtkLabelPrivate *priv;
5515 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
5519 gtk_label_ensure_layout (label);
5521 return priv->layout;
5525 * gtk_label_get_layout_offsets:
5526 * @label: a #GtkLabel
5527 * @x: (out) (allow-none): location to store X offset of layout, or %NULL
5528 * @y: (out) (allow-none): location to store Y offset of layout, or %NULL
5530 * Obtains the coordinates where the label will draw the #PangoLayout
5531 * representing the text in the label; useful to convert mouse events
5532 * into coordinates inside the #PangoLayout, e.g. to take some action
5533 * if some part of the label is clicked. Of course you will need to
5534 * create a #GtkEventBox to receive the events, and pack the label
5535 * inside it, since labels are a #GTK_NO_WINDOW widget. Remember
5536 * when using the #PangoLayout functions you need to convert to
5537 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
5540 gtk_label_get_layout_offsets (GtkLabel *label,
5544 g_return_if_fail (GTK_IS_LABEL (label));
5546 gtk_label_ensure_layout (label);
5548 get_layout_location (label, x, y);
5552 * gtk_label_set_use_markup:
5553 * @label: a #GtkLabel
5554 * @setting: %TRUE if the label's text should be parsed for markup.
5556 * Sets whether the text of the label contains markup in <link
5557 * linkend="PangoMarkupFormat">Pango's text markup
5558 * language</link>. See gtk_label_set_markup().
5561 gtk_label_set_use_markup (GtkLabel *label,
5564 g_return_if_fail (GTK_IS_LABEL (label));
5566 g_object_freeze_notify (G_OBJECT (label));
5568 gtk_label_set_use_markup_internal (label, setting);
5569 gtk_label_recalculate (label);
5571 g_object_thaw_notify (G_OBJECT (label));
5575 * gtk_label_get_use_markup:
5576 * @label: a #GtkLabel
5578 * Returns whether the label's text is interpreted as marked up with
5579 * the <link linkend="PangoMarkupFormat">Pango text markup
5580 * language</link>. See gtk_label_set_use_markup ().
5582 * Return value: %TRUE if the label's text will be parsed for markup.
5585 gtk_label_get_use_markup (GtkLabel *label)
5587 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5589 return label->priv->use_markup;
5593 * gtk_label_set_use_underline:
5594 * @label: a #GtkLabel
5595 * @setting: %TRUE if underlines in the text indicate mnemonics
5597 * If true, an underline in the text indicates the next character should be
5598 * used for the mnemonic accelerator key.
5601 gtk_label_set_use_underline (GtkLabel *label,
5604 g_return_if_fail (GTK_IS_LABEL (label));
5606 g_object_freeze_notify (G_OBJECT (label));
5608 gtk_label_set_use_underline_internal (label, setting);
5609 gtk_label_recalculate (label);
5611 g_object_thaw_notify (G_OBJECT (label));
5615 * gtk_label_get_use_underline:
5616 * @label: a #GtkLabel
5618 * Returns whether an embedded underline in the label indicates a
5619 * mnemonic. See gtk_label_set_use_underline().
5621 * Return value: %TRUE whether an embedded underline in the label indicates
5622 * the mnemonic accelerator keys.
5625 gtk_label_get_use_underline (GtkLabel *label)
5627 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5629 return label->priv->use_underline;
5633 * gtk_label_set_single_line_mode:
5634 * @label: a #GtkLabel
5635 * @single_line_mode: %TRUE if the label should be in single line mode
5637 * Sets whether the label is in single line mode.
5642 gtk_label_set_single_line_mode (GtkLabel *label,
5643 gboolean single_line_mode)
5645 GtkLabelPrivate *priv;
5647 g_return_if_fail (GTK_IS_LABEL (label));
5651 single_line_mode = single_line_mode != FALSE;
5653 if (priv->single_line_mode != single_line_mode)
5655 priv->single_line_mode = single_line_mode;
5657 gtk_label_clear_layout (label);
5658 gtk_widget_queue_resize (GTK_WIDGET (label));
5660 g_object_notify (G_OBJECT (label), "single-line-mode");
5665 * gtk_label_get_single_line_mode:
5666 * @label: a #GtkLabel
5668 * Returns whether the label is in single line mode.
5670 * Return value: %TRUE when the label is in single line mode.
5675 gtk_label_get_single_line_mode (GtkLabel *label)
5677 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5679 return label->priv->single_line_mode;
5682 /* Compute the X position for an offset that corresponds to the "more important
5683 * cursor position for that offset. We use this when trying to guess to which
5684 * end of the selection we should go to when the user hits the left or
5688 get_better_cursor (GtkLabel *label,
5693 GtkLabelPrivate *priv = label->priv;
5694 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5695 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5696 PangoDirection cursor_direction = get_cursor_direction (label);
5697 gboolean split_cursor;
5698 PangoRectangle strong_pos, weak_pos;
5700 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5701 "gtk-split-cursor", &split_cursor,
5704 gtk_label_ensure_layout (label);
5706 pango_layout_get_cursor_pos (priv->layout, index,
5707 &strong_pos, &weak_pos);
5711 *x = strong_pos.x / PANGO_SCALE;
5712 *y = strong_pos.y / PANGO_SCALE;
5716 if (keymap_direction == cursor_direction)
5718 *x = strong_pos.x / PANGO_SCALE;
5719 *y = strong_pos.y / PANGO_SCALE;
5723 *x = weak_pos.x / PANGO_SCALE;
5724 *y = weak_pos.y / PANGO_SCALE;
5731 gtk_label_move_logically (GtkLabel *label,
5735 GtkLabelPrivate *priv = label->priv;
5736 gint offset = g_utf8_pointer_to_offset (priv->text,
5737 priv->text + start);
5741 PangoLogAttr *log_attrs;
5745 gtk_label_ensure_layout (label);
5747 length = g_utf8_strlen (priv->text, -1);
5749 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5751 while (count > 0 && offset < length)
5755 while (offset < length && !log_attrs[offset].is_cursor_position);
5759 while (count < 0 && offset > 0)
5763 while (offset > 0 && !log_attrs[offset].is_cursor_position);
5771 return g_utf8_offset_to_pointer (priv->text, offset) - priv->text;
5775 gtk_label_move_visually (GtkLabel *label,
5779 GtkLabelPrivate *priv = label->priv;
5786 int new_index, new_trailing;
5787 gboolean split_cursor;
5790 gtk_label_ensure_layout (label);
5792 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5793 "gtk-split-cursor", &split_cursor,
5800 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5801 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5803 strong = keymap_direction == get_cursor_direction (label);
5808 pango_layout_move_cursor_visually (priv->layout, strong, index, 0, 1, &new_index, &new_trailing);
5813 pango_layout_move_cursor_visually (priv->layout, strong, index, 0, -1, &new_index, &new_trailing);
5817 if (new_index < 0 || new_index == G_MAXINT)
5822 while (new_trailing--)
5823 index = g_utf8_next_char (priv->text + new_index) - priv->text;
5830 gtk_label_move_forward_word (GtkLabel *label,
5833 GtkLabelPrivate *priv = label->priv;
5834 gint new_pos = g_utf8_pointer_to_offset (priv->text,
5835 priv->text + start);
5838 length = g_utf8_strlen (priv->text, -1);
5839 if (new_pos < length)
5841 PangoLogAttr *log_attrs;
5844 gtk_label_ensure_layout (label);
5846 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5848 /* Find the next word end */
5850 while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
5856 return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
5861 gtk_label_move_backward_word (GtkLabel *label,
5864 GtkLabelPrivate *priv = label->priv;
5865 gint new_pos = g_utf8_pointer_to_offset (priv->text,
5866 priv->text + start);
5870 PangoLogAttr *log_attrs;
5873 gtk_label_ensure_layout (label);
5875 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5879 /* Find the previous word beginning */
5880 while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
5886 return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
5890 gtk_label_move_cursor (GtkLabel *label,
5891 GtkMovementStep step,
5893 gboolean extend_selection)
5895 GtkLabelPrivate *priv = label->priv;
5899 if (priv->select_info == NULL)
5902 old_pos = new_pos = priv->select_info->selection_end;
5904 if (priv->select_info->selection_end != priv->select_info->selection_anchor &&
5907 /* If we have a current selection and aren't extending it, move to the
5908 * start/or end of the selection as appropriate
5912 case GTK_MOVEMENT_VISUAL_POSITIONS:
5915 gint anchor_x, anchor_y;
5916 gboolean end_is_left;
5918 get_better_cursor (label, priv->select_info->selection_end, &end_x, &end_y);
5919 get_better_cursor (label, priv->select_info->selection_anchor, &anchor_x, &anchor_y);
5921 end_is_left = (end_y < anchor_y) || (end_y == anchor_y && end_x < anchor_x);
5924 new_pos = end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
5926 new_pos = !end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
5929 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5930 case GTK_MOVEMENT_WORDS:
5932 new_pos = MIN (priv->select_info->selection_end, priv->select_info->selection_anchor);
5934 new_pos = MAX (priv->select_info->selection_end, priv->select_info->selection_anchor);
5936 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5937 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5938 case GTK_MOVEMENT_BUFFER_ENDS:
5939 /* FIXME: Can do better here */
5940 new_pos = count < 0 ? 0 : strlen (priv->text);
5942 case GTK_MOVEMENT_DISPLAY_LINES:
5943 case GTK_MOVEMENT_PARAGRAPHS:
5944 case GTK_MOVEMENT_PAGES:
5945 case GTK_MOVEMENT_HORIZONTAL_PAGES:
5953 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5954 new_pos = gtk_label_move_logically (label, new_pos, count);
5956 case GTK_MOVEMENT_VISUAL_POSITIONS:
5957 new_pos = gtk_label_move_visually (label, new_pos, count);
5958 if (new_pos == old_pos)
5960 if (!extend_selection)
5962 if (!gtk_widget_keynav_failed (GTK_WIDGET (label),
5964 GTK_DIR_RIGHT : GTK_DIR_LEFT))
5966 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
5969 gtk_widget_child_focus (toplevel,
5971 GTK_DIR_RIGHT : GTK_DIR_LEFT);
5976 gtk_widget_error_bell (GTK_WIDGET (label));
5980 case GTK_MOVEMENT_WORDS:
5983 new_pos = gtk_label_move_forward_word (label, new_pos);
5988 new_pos = gtk_label_move_backward_word (label, new_pos);
5991 if (new_pos == old_pos)
5992 gtk_widget_error_bell (GTK_WIDGET (label));
5994 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5995 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5996 case GTK_MOVEMENT_BUFFER_ENDS:
5997 /* FIXME: Can do better here */
5998 new_pos = count < 0 ? 0 : strlen (priv->text);
5999 if (new_pos == old_pos)
6000 gtk_widget_error_bell (GTK_WIDGET (label));
6002 case GTK_MOVEMENT_DISPLAY_LINES:
6003 case GTK_MOVEMENT_PARAGRAPHS:
6004 case GTK_MOVEMENT_PAGES:
6005 case GTK_MOVEMENT_HORIZONTAL_PAGES:
6010 if (extend_selection)
6011 gtk_label_select_region_index (label,
6012 priv->select_info->selection_anchor,
6015 gtk_label_select_region_index (label, new_pos, new_pos);
6019 gtk_label_copy_clipboard (GtkLabel *label)
6021 GtkLabelPrivate *priv = label->priv;
6023 if (priv->text && priv->select_info)
6027 GtkClipboard *clipboard;
6029 start = MIN (priv->select_info->selection_anchor,
6030 priv->select_info->selection_end);
6031 end = MAX (priv->select_info->selection_anchor,
6032 priv->select_info->selection_end);
6034 len = strlen (priv->text);
6042 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6045 gtk_clipboard_set_text (clipboard, priv->text + start, end - start);
6050 link = gtk_label_get_focus_link (label);
6052 gtk_clipboard_set_text (clipboard, link->uri, -1);
6058 gtk_label_select_all (GtkLabel *label)
6060 GtkLabelPrivate *priv = label->priv;
6062 gtk_label_select_region_index (label, 0, strlen (priv->text));
6065 /* Quick hack of a popup menu
6068 activate_cb (GtkWidget *menuitem,
6071 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6072 g_signal_emit_by_name (label, signal);
6076 append_action_signal (GtkLabel *label,
6078 const gchar *stock_id,
6079 const gchar *signal,
6082 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6084 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
6085 g_signal_connect (menuitem, "activate",
6086 G_CALLBACK (activate_cb), label);
6088 gtk_widget_set_sensitive (menuitem, sensitive);
6090 gtk_widget_show (menuitem);
6091 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6095 popup_menu_detach (GtkWidget *attach_widget,
6098 GtkLabel *label = GTK_LABEL (attach_widget);
6099 GtkLabelPrivate *priv = label->priv;
6101 if (priv->select_info)
6102 priv->select_info->popup_menu = NULL;
6106 popup_position_func (GtkMenu *menu,
6114 GtkAllocation allocation;
6118 label = GTK_LABEL (user_data);
6119 widget = GTK_WIDGET (label);
6121 g_return_if_fail (gtk_widget_get_realized (widget));
6123 screen = gtk_widget_get_screen (widget);
6124 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
6126 gtk_widget_get_allocation (widget, &allocation);
6131 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
6134 gtk_widget_get_allocation (widget, &allocation);
6136 *x += allocation.width / 2;
6137 *y += allocation.height;
6139 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
6140 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
6144 open_link_activate_cb (GtkMenuItem *menu_item,
6149 link = gtk_label_get_current_link (label);
6152 emit_activate_link (label, link);
6156 copy_link_activate_cb (GtkMenuItem *menu_item,
6159 GtkClipboard *clipboard;
6162 uri = gtk_label_get_current_uri (label);
6165 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6166 gtk_clipboard_set_text (clipboard, uri, -1);
6171 gtk_label_popup_menu (GtkWidget *widget)
6173 gtk_label_do_popup (GTK_LABEL (widget), NULL);
6179 gtk_label_do_popup (GtkLabel *label,
6180 GdkEventButton *event)
6182 GtkLabelPrivate *priv = label->priv;
6183 GtkWidget *menuitem;
6186 gboolean have_selection;
6189 if (!priv->select_info)
6192 if (priv->select_info->popup_menu)
6193 gtk_widget_destroy (priv->select_info->popup_menu);
6195 priv->select_info->popup_menu = menu = gtk_menu_new ();
6197 gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (label), popup_menu_detach);
6200 priv->select_info->selection_anchor != priv->select_info->selection_end;
6204 if (priv->select_info->link_clicked)
6205 link = priv->select_info->active_link;
6210 link = gtk_label_get_focus_link (label);
6212 if (!have_selection && link)
6215 menuitem = gtk_image_menu_item_new_with_mnemonic (_("_Open Link"));
6216 gtk_widget_show (menuitem);
6217 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6219 g_signal_connect (G_OBJECT (menuitem), "activate",
6220 G_CALLBACK (open_link_activate_cb), label);
6222 image = gtk_image_new_from_stock (GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_MENU);
6223 gtk_widget_show (image);
6224 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6226 /* Copy Link Address */
6227 menuitem = gtk_image_menu_item_new_with_mnemonic (_("Copy _Link Address"));
6228 gtk_widget_show (menuitem);
6229 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6231 g_signal_connect (G_OBJECT (menuitem), "activate",
6232 G_CALLBACK (copy_link_activate_cb), label);
6234 image = gtk_image_new_from_stock (GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
6235 gtk_widget_show (image);
6236 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6240 append_action_signal (label, menu, GTK_STOCK_CUT, "cut-clipboard", FALSE);
6241 append_action_signal (label, menu, GTK_STOCK_COPY, "copy-clipboard", have_selection);
6242 append_action_signal (label, menu, GTK_STOCK_PASTE, "paste-clipboard", FALSE);
6244 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
6245 gtk_widget_set_sensitive (menuitem, FALSE);
6246 gtk_widget_show (menuitem);
6247 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6249 menuitem = gtk_separator_menu_item_new ();
6250 gtk_widget_show (menuitem);
6251 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6253 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
6254 g_signal_connect_swapped (menuitem, "activate",
6255 G_CALLBACK (gtk_label_select_all), label);
6256 gtk_widget_show (menuitem);
6257 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6260 g_signal_emit (label, signals[POPULATE_POPUP], 0, menu);
6263 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6265 event->button, event->time);
6268 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6269 popup_position_func, label,
6270 0, gtk_get_current_event_time ());
6271 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
6276 gtk_label_clear_links (GtkLabel *label)
6278 GtkLabelPrivate *priv = label->priv;
6280 if (!priv->select_info)
6283 g_list_free_full (priv->select_info->links, (GDestroyNotify) link_free);
6284 priv->select_info->links = NULL;
6285 priv->select_info->active_link = NULL;
6289 gtk_label_activate_link (GtkLabel *label,
6292 GtkWidget *widget = GTK_WIDGET (label);
6293 GError *error = NULL;
6295 if (!gtk_show_uri (gtk_widget_get_screen (widget),
6296 uri, gtk_get_current_event_time (), &error))
6298 g_warning ("Unable to show '%s': %s", uri, error->message);
6299 g_error_free (error);
6306 emit_activate_link (GtkLabel *label,
6309 GtkLabelPrivate *priv = label->priv;
6312 g_signal_emit (label, signals[ACTIVATE_LINK], 0, link->uri, &handled);
6313 if (handled && priv->track_links && !link->visited)
6315 link->visited = TRUE;
6316 /* FIXME: shouldn't have to redo everything here */
6317 gtk_label_clear_layout (label);
6322 gtk_label_activate_current_link (GtkLabel *label)
6325 GtkWidget *widget = GTK_WIDGET (label);
6327 link = gtk_label_get_focus_link (label);
6331 emit_activate_link (label, link);
6335 GtkWidget *toplevel;
6337 GtkWidget *default_widget, *focus_widget;
6339 toplevel = gtk_widget_get_toplevel (widget);
6340 if (GTK_IS_WINDOW (toplevel))
6342 window = GTK_WINDOW (toplevel);
6346 default_widget = gtk_window_get_default_widget (window);
6347 focus_widget = gtk_window_get_focus (window);
6349 if (default_widget != widget &&
6350 !(widget == focus_widget && (!default_widget || !gtk_widget_is_sensitive (default_widget))))
6351 gtk_window_activate_default (window);
6357 static GtkLabelLink *
6358 gtk_label_get_current_link (GtkLabel *label)
6360 GtkLabelPrivate *priv = label->priv;
6363 if (!priv->select_info)
6366 if (priv->select_info->link_clicked)
6367 link = priv->select_info->active_link;
6369 link = gtk_label_get_focus_link (label);
6375 * gtk_label_get_current_uri:
6376 * @label: a #GtkLabel
6378 * Returns the URI for the currently active link in the label.
6379 * The active link is the one under the mouse pointer or, in a
6380 * selectable label, the link in which the text cursor is currently
6383 * This function is intended for use in a #GtkLabel::activate-link handler
6384 * or for use in a #GtkWidget::query-tooltip handler.
6386 * Returns: the currently active URI. The string is owned by GTK+ and must
6387 * not be freed or modified.
6392 gtk_label_get_current_uri (GtkLabel *label)
6396 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
6398 link = gtk_label_get_current_link (label);
6407 * gtk_label_set_track_visited_links:
6408 * @label: a #GtkLabel
6409 * @track_links: %TRUE to track visited links
6411 * Sets whether the label should keep track of clicked
6412 * links (and use a different color for them).
6417 gtk_label_set_track_visited_links (GtkLabel *label,
6418 gboolean track_links)
6420 GtkLabelPrivate *priv;
6422 g_return_if_fail (GTK_IS_LABEL (label));
6426 track_links = track_links != FALSE;
6428 if (priv->track_links != track_links)
6430 priv->track_links = track_links;
6432 /* FIXME: shouldn't have to redo everything here */
6433 gtk_label_recalculate (label);
6435 g_object_notify (G_OBJECT (label), "track-visited-links");
6440 * gtk_label_get_track_visited_links:
6441 * @label: a #GtkLabel
6443 * Returns whether the label is currently keeping track
6446 * Returns: %TRUE if clicked links are remembered
6451 gtk_label_get_track_visited_links (GtkLabel *label)
6453 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
6455 return label->priv->track_links;
6459 gtk_label_query_tooltip (GtkWidget *widget,
6462 gboolean keyboard_tip,
6463 GtkTooltip *tooltip)
6465 GtkLabel *label = GTK_LABEL (widget);
6466 GtkLabelPrivate *priv = label->priv;
6467 GtkLabelSelectionInfo *info = priv->select_info;
6471 if (info && info->links)
6475 if (info->selection_anchor == info->selection_end)
6476 index = info->selection_anchor;
6480 if (!get_layout_index (label, x, y, &index))
6486 for (l = info->links; l != NULL; l = l->next)
6488 GtkLabelLink *link = l->data;
6489 if (index >= link->start && index <= link->end)
6493 gtk_tooltip_set_markup (tooltip, link->title);
6502 return GTK_WIDGET_CLASS (gtk_label_parent_class)->query_tooltip (widget,
6509 _gtk_label_get_cursor_position (GtkLabel *label)
6511 GtkLabelPrivate *priv = label->priv;
6513 if (priv->select_info && priv->select_info->selectable)
6514 return g_utf8_pointer_to_offset (priv->text,
6515 priv->text + priv->select_info->selection_end);
6521 _gtk_label_get_selection_bound (GtkLabel *label)
6523 GtkLabelPrivate *priv = label->priv;
6525 if (priv->select_info && priv->select_info->selectable)
6526 return g_utf8_pointer_to_offset (priv->text,
6527 priv->text + priv->select_info->selection_anchor);