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"
42 #include "gtkmenuitem.h"
43 #include "gtkmenushellprivate.h"
44 #include "gtknotebook.h"
46 #include "gtkbindings.h"
47 #include "gtkbuildable.h"
50 #include "gtktooltip.h"
51 #include "gtkprivate.h"
52 #include "gtktypebuiltins.h"
54 #include "a11y/gtklabelaccessible.h"
56 /* this is in case rint() is not provided by the compiler,
57 * such as in the case of C89 compilers, like MSVC
59 #include "fallback-c89.c"
63 * @Short_description: A widget that displays a small to medium amount of text
66 * The #GtkLabel widget displays a small amount of text. As the name
67 * implies, most labels are used to label another widget such as a
68 * #GtkButton, a #GtkMenuItem, or a #GtkOptionMenu.
70 * <refsect2 id="GtkLabel-BUILDER-UI">
71 * <title>GtkLabel as GtkBuildable</title>
73 * The GtkLabel implementation of the GtkBuildable interface supports a
74 * custom <attributes> element, which supports any number of <attribute>
75 * elements. the <attribute> element has attributes named name, value,
76 * start and end and allows you to specify #PangoAttribute values for this label.
79 * <title>A UI definition fragment specifying Pango attributes</title>
80 * <programlisting><![CDATA[
81 * <object class="GtkLabel">
83 * <attribute name="weight" value="PANGO_WEIGHT_BOLD"/>
84 * <attribute name="background" value="red" start="5" end="10"/>"
87 * ]]></programlisting>
89 * The start and end attributes specify the range of characters to which the
90 * Pango attribute applies. If start and end are not specified, the attribute is
91 * applied to the whole text. Note that specifying ranges does not make much
92 * sense with translatable attributes. Use markup embedded in the translatable
97 * <title>Mnemonics</title>
99 * Labels may contain <firstterm>mnemonics</firstterm>. Mnemonics are
100 * underlined characters in the label, used for keyboard navigation.
101 * Mnemonics are created by providing a string with an underscore before
102 * the mnemonic character, such as <literal>"_File"</literal>, to the
103 * functions gtk_label_new_with_mnemonic() or
104 * gtk_label_set_text_with_mnemonic().
106 * Mnemonics automatically activate any activatable widget the label is
107 * inside, such as a #GtkButton; if the label is not inside the
108 * mnemonic's target widget, you have to tell the label about the target
109 * using gtk_label_set_mnemonic_widget(). Here's a simple example where
110 * the label is inside a button:
114 * // Pressing Alt+H will activate this button
115 * button = gtk_button_new (<!-- -->);
116 * label = gtk_label_new_with_mnemonic ("_Hello");
117 * gtk_container_add (GTK_CONTAINER (button), label);
121 * There's a convenience function to create buttons with a mnemonic label
126 * // Pressing Alt+H will activate this button
127 * button = gtk_button_new_with_mnemonic ("_Hello");
131 * To create a mnemonic for a widget alongside the label, such as a
132 * #GtkEntry, you have to point the label at the entry with
133 * gtk_label_set_mnemonic_widget():
137 * // Pressing Alt+H will focus the entry
138 * entry = gtk_entry_new (<!-- -->);
139 * label = gtk_label_new_with_mnemonic ("_Hello");
140 * gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
146 * <title>Markup (styled text)</title>
148 * To make it easy to format text in a label (changing colors, fonts,
149 * etc.), label text can be provided in a simple <link
150 * linkend="PangoMarkupFormat">markup format</link>.
151 * Here's how to create a label with a small font:
155 * label = gtk_label_new (NULL);
156 * gtk_label_set_markup (GTK_LABEL (label), "<small>Small text</small>");
161 * linkend="PangoMarkupFormat">complete documentation</link> of available
162 * tags in the Pango manual.)
164 * The markup passed to gtk_label_set_markup() must be valid; for example,
165 * literal <, > and & characters must be escaped as \<,
166 * \gt;, and \&. If you pass text obtained from the user, file,
167 * or a network to gtk_label_set_markup(), you'll want to escape it with
168 * g_markup_escape_text() or g_markup_printf_escaped().
170 * Markup strings are just a convenient way to set the #PangoAttrList on
171 * a label; gtk_label_set_attributes() may be a simpler way to set
172 * attributes in some cases. Be careful though; #PangoAttrList tends to
173 * cause internationalization problems, unless you're applying attributes
174 * to the entire string (i.e. unless you set the range of each attribute
175 * to [0, %G_MAXINT)). The reason is that specifying the start_index and
176 * end_index for a #PangoAttribute requires knowledge of the exact string
177 * being displayed, so translations will cause problems.
181 * <title>Selectable labels</title>
182 * Labels can be made selectable with gtk_label_set_selectable().
183 * Selectable labels allow the user to copy the label contents to
184 * the clipboard. Only labels that contain useful-to-copy information
185 * — such as error messages — should be made selectable.
187 * <refsect2 id="label-text-layout">
188 * <title>Text layout</title>
190 * A label can contain any number of paragraphs, but will have
191 * performance problems if it contains more than a small number.
192 * Paragraphs are separated by newlines or other paragraph separators
193 * understood by Pango.
195 * Labels can automatically wrap text if you call
196 * gtk_label_set_line_wrap().
198 * gtk_label_set_justify() sets how the lines in a label align
199 * with one another. If you want to set how the label as a whole
200 * aligns in its available space, see gtk_misc_set_alignment().
202 * The #GtkLabel:width-chars and #GtkLabel:max-width-chars properties
203 * can be used to control the size allocation of ellipsized or wrapped
204 * labels. For ellipsizing labels, if either is specified (and less
205 * than the actual text size), it is used as the minimum width, and the actual
206 * text size is used as the natural width of the label. For wrapping labels,
207 * width-chars is used as the minimum width, if specified, and max-width-chars
208 * is used as the natural width. Even if max-width-chars specified, wrapping
209 * labels will be rewrapped to use all of the available width.
212 * Note that the interpretation of #GtkLabel:width-chars and
213 * #GtkLabel:max-width-chars has changed a bit with the introduction of
214 * <link linkend="geometry-management">width-for-height geometry management.</link>
219 * <title>Links</title>
221 * Since 2.18, GTK+ supports markup for clickable hyperlinks in addition
222 * to regular Pango markup. The markup for links is borrowed from HTML, using the
223 * <tag>a</tag> with href and title attributes. GTK+ renders links similar to the
224 * way they appear in web browsers, with colored, underlined text. The title
225 * attribute is displayed as a tooltip on the link. An example looks like this:
227 * <informalexample><programlisting>
228 * gtk_label_set_markup (label, "Go to the <a href="http://www.gtk.org" title="<i>Our</i> website">GTK+ website</a> for more...");
229 * </programlisting></informalexample>
231 * It is possible to implement custom handling for links and their tooltips with
232 * the #GtkLabel::activate-link signal and the gtk_label_get_current_uri() function.
237 struct _GtkLabelPrivate
239 GtkLabelSelectionInfo *select_info;
240 GtkWidget *mnemonic_widget;
241 GtkWindow *mnemonic_window;
243 PangoAttrList *attrs;
244 PangoAttrList *effective_attrs;
252 guint mnemonics_visible : 1;
255 guint use_underline : 1;
256 guint use_markup : 1;
258 guint single_line_mode : 1;
259 guint have_transform : 1;
262 guint pattern_set : 1;
263 guint track_links : 1;
265 guint mnemonic_keyval;
268 gint max_width_chars;
271 /* Notes about the handling of links:
273 * Links share the GtkLabelSelectionInfo struct with selectable labels.
274 * There are some new fields for links. The links field contains the list
275 * of GtkLabelLink structs that describe the links which are embedded in
276 * the label. The active_link field points to the link under the mouse
277 * pointer. For keyboard navigation, the 'focus' link is determined by
278 * finding the link which contains the selection_anchor position.
279 * The link_clicked field is used with button press and release events
280 * to ensure that pressing inside a link and releasing outside of it
281 * does not activate the link.
283 * Links are rendered with the link-color/visited-link-color colors
284 * that are determined by the style and with an underline. When the mouse
285 * pointer is over a link, the pointer is changed to indicate the link,
286 * and the background behind the link is rendered with the base[PRELIGHT]
287 * color. While a button is pressed over a link, the background is rendered
288 * with the base[ACTIVE] color.
290 * Labels with links accept keyboard focus, and it is possible to move
291 * the focus between the embedded links using Tab/Shift-Tab. The focus
292 * is indicated by a focus rectangle that is drawn around the link text.
293 * Pressing Enter activates the focussed link, and there is a suitable
294 * context menu for links that can be opened with the Menu key. Pressing
295 * Control-C copies the link URI to the clipboard.
297 * In selectable labels with links, link functionality is only available
298 * when the selection is empty.
303 gchar *title; /* the title attribute, used as tooltip */
304 gboolean visited; /* get set when the link is activated; this flag
305 * gets preserved over later set_markup() calls
307 gint start; /* position of the link in the PangoLayout */
311 struct _GtkLabelSelectionInfo
314 gint selection_anchor;
316 GtkWidget *popup_menu;
319 GtkLabelLink *active_link;
325 guint select_words : 1;
326 guint selectable : 1;
327 guint link_clicked : 1;
335 ACTIVATE_CURRENT_LINK,
350 PROP_MNEMONIC_KEYVAL,
351 PROP_MNEMONIC_WIDGET,
352 PROP_CURSOR_POSITION,
353 PROP_SELECTION_BOUND,
356 PROP_SINGLE_LINE_MODE,
358 PROP_MAX_WIDTH_CHARS,
359 PROP_TRACK_VISITED_LINKS
362 /* When rotating ellipsizable text we want the natural size to request
363 * more to ensure the label wont ever ellipsize in an allocation of full natural size.
365 #define ROTATION_ELLIPSIZE_PADDING 2
367 static guint signals[LAST_SIGNAL] = { 0 };
369 static const GdkColor default_link_color = { 0, 0, 0, 0xeeee };
370 static const GdkColor default_visited_link_color = { 0, 0x5555, 0x1a1a, 0x8b8b };
372 static void gtk_label_set_property (GObject *object,
376 static void gtk_label_get_property (GObject *object,
380 static void gtk_label_finalize (GObject *object);
381 static void gtk_label_destroy (GtkWidget *widget);
382 static void gtk_label_size_allocate (GtkWidget *widget,
383 GtkAllocation *allocation);
384 static void gtk_label_state_changed (GtkWidget *widget,
386 static void gtk_label_style_updated (GtkWidget *widget);
387 static void gtk_label_direction_changed (GtkWidget *widget,
388 GtkTextDirection previous_dir);
389 static gint gtk_label_draw (GtkWidget *widget,
391 static gboolean gtk_label_focus (GtkWidget *widget,
392 GtkDirectionType direction);
394 static void gtk_label_realize (GtkWidget *widget);
395 static void gtk_label_unrealize (GtkWidget *widget);
396 static void gtk_label_map (GtkWidget *widget);
397 static void gtk_label_unmap (GtkWidget *widget);
399 static gboolean gtk_label_button_press (GtkWidget *widget,
400 GdkEventButton *event);
401 static gboolean gtk_label_button_release (GtkWidget *widget,
402 GdkEventButton *event);
403 static gboolean gtk_label_motion (GtkWidget *widget,
404 GdkEventMotion *event);
405 static gboolean gtk_label_leave_notify (GtkWidget *widget,
406 GdkEventCrossing *event);
408 static void gtk_label_grab_focus (GtkWidget *widget);
410 static gboolean gtk_label_query_tooltip (GtkWidget *widget,
413 gboolean keyboard_tip,
414 GtkTooltip *tooltip);
416 static void gtk_label_set_text_internal (GtkLabel *label,
418 static void gtk_label_set_label_internal (GtkLabel *label,
420 static void gtk_label_set_use_markup_internal (GtkLabel *label,
422 static void gtk_label_set_use_underline_internal (GtkLabel *label,
424 static void gtk_label_set_attributes_internal (GtkLabel *label,
425 PangoAttrList *attrs);
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_rescan_links (GtkLabel *label);
501 static void gtk_label_clear_links (GtkLabel *label);
502 static gboolean gtk_label_activate_link (GtkLabel *label,
504 static void gtk_label_activate_current_link (GtkLabel *label);
505 static GtkLabelLink *gtk_label_get_current_link (GtkLabel *label);
506 static void gtk_label_get_link_colors (GtkWidget *widget,
507 GdkColor **link_color,
508 GdkColor **visited_link_color);
509 static void emit_activate_link (GtkLabel *label,
512 static GtkSizeRequestMode gtk_label_get_request_mode (GtkWidget *widget);
513 static void gtk_label_get_preferred_width (GtkWidget *widget,
516 static void gtk_label_get_preferred_height (GtkWidget *widget,
519 static void gtk_label_get_preferred_width_for_height (GtkWidget *widget,
522 gint *natural_width);
523 static void gtk_label_get_preferred_height_for_width (GtkWidget *widget,
525 gint *minimum_height,
526 gint *natural_height);
528 static GtkBuildableIface *buildable_parent_iface = NULL;
530 G_DEFINE_TYPE_WITH_CODE (GtkLabel, gtk_label, GTK_TYPE_MISC,
531 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
532 gtk_label_buildable_interface_init))
535 add_move_binding (GtkBindingSet *binding_set,
538 GtkMovementStep step,
541 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
543 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
547 G_TYPE_BOOLEAN, FALSE);
549 /* Selection-extending version */
550 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
554 G_TYPE_BOOLEAN, TRUE);
558 gtk_label_class_init (GtkLabelClass *class)
560 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
561 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
562 GtkBindingSet *binding_set;
564 gobject_class->set_property = gtk_label_set_property;
565 gobject_class->get_property = gtk_label_get_property;
566 gobject_class->finalize = gtk_label_finalize;
568 widget_class->destroy = gtk_label_destroy;
569 widget_class->size_allocate = gtk_label_size_allocate;
570 widget_class->state_changed = gtk_label_state_changed;
571 widget_class->style_updated = gtk_label_style_updated;
572 widget_class->query_tooltip = gtk_label_query_tooltip;
573 widget_class->direction_changed = gtk_label_direction_changed;
574 widget_class->draw = gtk_label_draw;
575 widget_class->realize = gtk_label_realize;
576 widget_class->unrealize = gtk_label_unrealize;
577 widget_class->map = gtk_label_map;
578 widget_class->unmap = gtk_label_unmap;
579 widget_class->button_press_event = gtk_label_button_press;
580 widget_class->button_release_event = gtk_label_button_release;
581 widget_class->motion_notify_event = gtk_label_motion;
582 widget_class->leave_notify_event = gtk_label_leave_notify;
583 widget_class->hierarchy_changed = gtk_label_hierarchy_changed;
584 widget_class->screen_changed = gtk_label_screen_changed;
585 widget_class->mnemonic_activate = gtk_label_mnemonic_activate;
586 widget_class->drag_data_get = gtk_label_drag_data_get;
587 widget_class->grab_focus = gtk_label_grab_focus;
588 widget_class->popup_menu = gtk_label_popup_menu;
589 widget_class->focus = gtk_label_focus;
590 widget_class->get_request_mode = gtk_label_get_request_mode;
591 widget_class->get_preferred_width = gtk_label_get_preferred_width;
592 widget_class->get_preferred_height = gtk_label_get_preferred_height;
593 widget_class->get_preferred_width_for_height = gtk_label_get_preferred_width_for_height;
594 widget_class->get_preferred_height_for_width = gtk_label_get_preferred_height_for_width;
596 class->move_cursor = gtk_label_move_cursor;
597 class->copy_clipboard = gtk_label_copy_clipboard;
598 class->activate_link = gtk_label_activate_link;
601 * GtkLabel::move-cursor:
602 * @entry: the object which received the signal
603 * @step: the granularity of the move, as a #GtkMovementStep
604 * @count: the number of @step units to move
605 * @extend_selection: %TRUE if the move should extend the selection
607 * The ::move-cursor signal is a
608 * <link linkend="keybinding-signals">keybinding signal</link>
609 * which gets emitted when the user initiates a cursor movement.
610 * If the cursor is not visible in @entry, this signal causes
611 * the viewport to be moved instead.
613 * Applications should not connect to it, but may emit it with
614 * g_signal_emit_by_name() if they need to control the cursor
617 * The default bindings for this signal come in two variants,
618 * the variant with the Shift modifier extends the selection,
619 * the variant without the Shift modifer does not.
620 * There are too many key combinations to list them all here.
622 * <listitem>Arrow keys move by individual characters/lines</listitem>
623 * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
624 * <listitem>Home/End keys move to the ends of the buffer</listitem>
627 signals[MOVE_CURSOR] =
628 g_signal_new (I_("move-cursor"),
629 G_OBJECT_CLASS_TYPE (gobject_class),
630 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
631 G_STRUCT_OFFSET (GtkLabelClass, move_cursor),
633 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
635 GTK_TYPE_MOVEMENT_STEP,
640 * GtkLabel::copy-clipboard:
641 * @label: the object which received the signal
643 * The ::copy-clipboard signal is a
644 * <link linkend="keybinding-signals">keybinding signal</link>
645 * which gets emitted to copy the selection to the clipboard.
647 * The default binding for this signal is Ctrl-c.
649 signals[COPY_CLIPBOARD] =
650 g_signal_new (I_("copy-clipboard"),
651 G_OBJECT_CLASS_TYPE (gobject_class),
652 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
653 G_STRUCT_OFFSET (GtkLabelClass, copy_clipboard),
655 _gtk_marshal_VOID__VOID,
659 * GtkLabel::populate-popup:
660 * @label: The label on which the signal is emitted
661 * @menu: the menu that is being populated
663 * The ::populate-popup signal gets emitted before showing the
664 * context menu of the label. Note that only selectable labels
665 * have context menus.
667 * If you need to add items to the context menu, connect
668 * to this signal and append your menuitems to the @menu.
670 signals[POPULATE_POPUP] =
671 g_signal_new (I_("populate-popup"),
672 G_OBJECT_CLASS_TYPE (gobject_class),
674 G_STRUCT_OFFSET (GtkLabelClass, populate_popup),
676 _gtk_marshal_VOID__OBJECT,
681 * GtkLabel::activate-current-link:
682 * @label: The label on which the signal was emitted
684 * A <link linkend="keybinding-signals">keybinding signal</link>
685 * which gets emitted when the user activates a link in the label.
687 * Applications may also emit the signal with g_signal_emit_by_name()
688 * if they need to control activation of URIs programmatically.
690 * The default bindings for this signal are all forms of the Enter key.
694 signals[ACTIVATE_CURRENT_LINK] =
695 g_signal_new_class_handler ("activate-current-link",
696 G_TYPE_FROM_CLASS (gobject_class),
697 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
698 G_CALLBACK (gtk_label_activate_current_link),
700 _gtk_marshal_VOID__VOID,
704 * GtkLabel::activate-link:
705 * @label: The label on which the signal was emitted
706 * @uri: the URI that is activated
708 * The signal which gets emitted to activate a URI.
709 * Applications may connect to it to override the default behaviour,
710 * which is to call gtk_show_uri().
712 * Returns: %TRUE if the link has been activated
716 signals[ACTIVATE_LINK] =
717 g_signal_new ("activate-link",
718 G_TYPE_FROM_CLASS (gobject_class),
720 G_STRUCT_OFFSET (GtkLabelClass, activate_link),
721 _gtk_boolean_handled_accumulator, NULL,
722 _gtk_marshal_BOOLEAN__STRING,
723 G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
725 g_object_class_install_property (gobject_class,
727 g_param_spec_string ("label",
729 P_("The text of the label"),
731 GTK_PARAM_READWRITE));
732 g_object_class_install_property (gobject_class,
734 g_param_spec_boxed ("attributes",
736 P_("A list of style attributes to apply to the text of the label"),
737 PANGO_TYPE_ATTR_LIST,
738 GTK_PARAM_READWRITE));
739 g_object_class_install_property (gobject_class,
741 g_param_spec_boolean ("use-markup",
743 P_("The text of the label includes XML markup. See pango_parse_markup()"),
745 GTK_PARAM_READWRITE));
746 g_object_class_install_property (gobject_class,
748 g_param_spec_boolean ("use-underline",
750 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
752 GTK_PARAM_READWRITE));
754 g_object_class_install_property (gobject_class,
756 g_param_spec_enum ("justify",
758 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"),
759 GTK_TYPE_JUSTIFICATION,
761 GTK_PARAM_READWRITE));
763 g_object_class_install_property (gobject_class,
765 g_param_spec_string ("pattern",
767 P_("A string with _ characters in positions correspond to characters in the text to underline"),
769 GTK_PARAM_WRITABLE));
771 g_object_class_install_property (gobject_class,
773 g_param_spec_boolean ("wrap",
775 P_("If set, wrap lines if the text becomes too wide"),
777 GTK_PARAM_READWRITE));
779 * GtkLabel:wrap-mode:
781 * If line wrapping is on (see the #GtkLabel:wrap property) this controls
782 * how the line wrapping is done. The default is %PANGO_WRAP_WORD, which
783 * means wrap on word boundaries.
787 g_object_class_install_property (gobject_class,
789 g_param_spec_enum ("wrap-mode",
790 P_("Line wrap mode"),
791 P_("If wrap is set, controls how linewrapping is done"),
792 PANGO_TYPE_WRAP_MODE,
794 GTK_PARAM_READWRITE));
795 g_object_class_install_property (gobject_class,
797 g_param_spec_boolean ("selectable",
799 P_("Whether the label text can be selected with the mouse"),
801 GTK_PARAM_READWRITE));
802 g_object_class_install_property (gobject_class,
803 PROP_MNEMONIC_KEYVAL,
804 g_param_spec_uint ("mnemonic-keyval",
806 P_("The mnemonic accelerator key for this label"),
810 GTK_PARAM_READABLE));
811 g_object_class_install_property (gobject_class,
812 PROP_MNEMONIC_WIDGET,
813 g_param_spec_object ("mnemonic-widget",
814 P_("Mnemonic widget"),
815 P_("The widget to be activated when the label's mnemonic "
818 GTK_PARAM_READWRITE));
820 g_object_class_install_property (gobject_class,
821 PROP_CURSOR_POSITION,
822 g_param_spec_int ("cursor-position",
823 P_("Cursor Position"),
824 P_("The current position of the insertion cursor in chars"),
828 GTK_PARAM_READABLE));
830 g_object_class_install_property (gobject_class,
831 PROP_SELECTION_BOUND,
832 g_param_spec_int ("selection-bound",
833 P_("Selection Bound"),
834 P_("The position of the opposite end of the selection from the cursor in chars"),
838 GTK_PARAM_READABLE));
841 * GtkLabel:ellipsize:
843 * The preferred place to ellipsize the string, if the label does
844 * not have enough room to display the entire string, specified as a
845 * #PangoEllisizeMode.
847 * Note that setting this property to a value other than
848 * %PANGO_ELLIPSIZE_NONE has the side-effect that the label requests
849 * only enough space to display the ellipsis "...". In particular, this
850 * means that ellipsizing labels do not work well in notebook tabs, unless
851 * the tab's #GtkNotebook:tab-expand property is set to %TRUE. Other ways
852 * to set a label's width are gtk_widget_set_size_request() and
853 * gtk_label_set_width_chars().
857 g_object_class_install_property (gobject_class,
859 g_param_spec_enum ("ellipsize",
861 P_("The preferred place to ellipsize the string, if the label does not have enough room to display the entire string"),
862 PANGO_TYPE_ELLIPSIZE_MODE,
863 PANGO_ELLIPSIZE_NONE,
864 GTK_PARAM_READWRITE));
867 * GtkLabel:width-chars:
869 * The desired width of the label, in characters. If this property is set to
870 * -1, the width will be calculated automatically.
872 * See the section on <link linkend="label-text-layout">text layout</link>
873 * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
874 * determine the width of ellipsized and wrapped labels.
878 g_object_class_install_property (gobject_class,
880 g_param_spec_int ("width-chars",
881 P_("Width In Characters"),
882 P_("The desired width of the label, in characters"),
886 GTK_PARAM_READWRITE));
889 * GtkLabel:single-line-mode:
891 * Whether the label is in single line mode. In single line mode,
892 * the height of the label does not depend on the actual text, it
893 * is always set to ascent + descent of the font. This can be an
894 * advantage in situations where resizing the label because of text
895 * changes would be distracting, e.g. in a statusbar.
899 g_object_class_install_property (gobject_class,
900 PROP_SINGLE_LINE_MODE,
901 g_param_spec_boolean ("single-line-mode",
902 P_("Single Line Mode"),
903 P_("Whether the label is in single line mode"),
905 GTK_PARAM_READWRITE));
910 * The angle that the baseline of the label makes with the horizontal,
911 * in degrees, measured counterclockwise. An angle of 90 reads from
912 * from bottom to top, an angle of 270, from top to bottom. Ignored
913 * if the label is selectable, wrapped, or ellipsized.
917 g_object_class_install_property (gobject_class,
919 g_param_spec_double ("angle",
921 P_("Angle at which the label is rotated"),
925 GTK_PARAM_READWRITE));
928 * GtkLabel:max-width-chars:
930 * The desired maximum width of the label, in characters. If this property
931 * is set to -1, the width will be calculated automatically.
933 * See the section on <link linkend="label-text-layout">text layout</link>
934 * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
935 * determine the width of ellipsized and wrapped labels.
939 g_object_class_install_property (gobject_class,
940 PROP_MAX_WIDTH_CHARS,
941 g_param_spec_int ("max-width-chars",
942 P_("Maximum Width In Characters"),
943 P_("The desired maximum width of the label, in characters"),
947 GTK_PARAM_READWRITE));
950 * GtkLabel:track-visited-links:
952 * Set this property to %TRUE to make the label track which links
953 * have been clicked. It will then apply the ::visited-link-color
954 * color, instead of ::link-color.
958 g_object_class_install_property (gobject_class,
959 PROP_TRACK_VISITED_LINKS,
960 g_param_spec_boolean ("track-visited-links",
961 P_("Track visited links"),
962 P_("Whether visited links should be tracked"),
964 GTK_PARAM_READWRITE));
969 binding_set = gtk_binding_set_by_class (class);
971 /* Moving the insertion point */
972 add_move_binding (binding_set, GDK_KEY_Right, 0,
973 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
975 add_move_binding (binding_set, GDK_KEY_Left, 0,
976 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
978 add_move_binding (binding_set, GDK_KEY_KP_Right, 0,
979 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
981 add_move_binding (binding_set, GDK_KEY_KP_Left, 0,
982 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
984 add_move_binding (binding_set, GDK_KEY_f, GDK_CONTROL_MASK,
985 GTK_MOVEMENT_LOGICAL_POSITIONS, 1);
987 add_move_binding (binding_set, GDK_KEY_b, GDK_CONTROL_MASK,
988 GTK_MOVEMENT_LOGICAL_POSITIONS, -1);
990 add_move_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK,
991 GTK_MOVEMENT_WORDS, 1);
993 add_move_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK,
994 GTK_MOVEMENT_WORDS, -1);
996 add_move_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK,
997 GTK_MOVEMENT_WORDS, 1);
999 add_move_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK,
1000 GTK_MOVEMENT_WORDS, -1);
1003 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
1005 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1007 G_TYPE_BOOLEAN, FALSE);
1009 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
1011 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1013 G_TYPE_BOOLEAN, TRUE);
1015 gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
1017 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1019 G_TYPE_BOOLEAN, FALSE);
1021 gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
1023 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1025 G_TYPE_BOOLEAN, TRUE);
1028 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1030 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1032 G_TYPE_BOOLEAN, FALSE);
1034 gtk_binding_entry_add_signal (binding_set, GDK_KEY_backslash, GDK_CONTROL_MASK,
1036 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1038 G_TYPE_BOOLEAN, FALSE);
1040 add_move_binding (binding_set, GDK_KEY_f, GDK_MOD1_MASK,
1041 GTK_MOVEMENT_WORDS, 1);
1043 add_move_binding (binding_set, GDK_KEY_b, GDK_MOD1_MASK,
1044 GTK_MOVEMENT_WORDS, -1);
1046 add_move_binding (binding_set, GDK_KEY_Home, 0,
1047 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1049 add_move_binding (binding_set, GDK_KEY_End, 0,
1050 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1052 add_move_binding (binding_set, GDK_KEY_KP_Home, 0,
1053 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1055 add_move_binding (binding_set, GDK_KEY_KP_End, 0,
1056 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1058 add_move_binding (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK,
1059 GTK_MOVEMENT_BUFFER_ENDS, -1);
1061 add_move_binding (binding_set, GDK_KEY_End, GDK_CONTROL_MASK,
1062 GTK_MOVEMENT_BUFFER_ENDS, 1);
1064 add_move_binding (binding_set, GDK_KEY_KP_Home, GDK_CONTROL_MASK,
1065 GTK_MOVEMENT_BUFFER_ENDS, -1);
1067 add_move_binding (binding_set, GDK_KEY_KP_End, GDK_CONTROL_MASK,
1068 GTK_MOVEMENT_BUFFER_ENDS, 1);
1071 gtk_binding_entry_add_signal (binding_set, GDK_KEY_c, GDK_CONTROL_MASK,
1072 "copy-clipboard", 0);
1074 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Return, 0,
1075 "activate-current-link", 0);
1076 gtk_binding_entry_add_signal (binding_set, GDK_KEY_ISO_Enter, 0,
1077 "activate-current-link", 0);
1078 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Enter, 0,
1079 "activate-current-link", 0);
1081 g_type_class_add_private (class, sizeof (GtkLabelPrivate));
1083 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_LABEL_ACCESSIBLE);
1087 gtk_label_set_property (GObject *object,
1089 const GValue *value,
1092 GtkLabel *label = GTK_LABEL (object);
1097 gtk_label_set_label (label, g_value_get_string (value));
1099 case PROP_ATTRIBUTES:
1100 gtk_label_set_attributes (label, g_value_get_boxed (value));
1102 case PROP_USE_MARKUP:
1103 gtk_label_set_use_markup (label, g_value_get_boolean (value));
1105 case PROP_USE_UNDERLINE:
1106 gtk_label_set_use_underline (label, g_value_get_boolean (value));
1109 gtk_label_set_justify (label, g_value_get_enum (value));
1112 gtk_label_set_pattern (label, g_value_get_string (value));
1115 gtk_label_set_line_wrap (label, g_value_get_boolean (value));
1117 case PROP_WRAP_MODE:
1118 gtk_label_set_line_wrap_mode (label, g_value_get_enum (value));
1120 case PROP_SELECTABLE:
1121 gtk_label_set_selectable (label, g_value_get_boolean (value));
1123 case PROP_MNEMONIC_WIDGET:
1124 gtk_label_set_mnemonic_widget (label, (GtkWidget*) g_value_get_object (value));
1126 case PROP_ELLIPSIZE:
1127 gtk_label_set_ellipsize (label, g_value_get_enum (value));
1129 case PROP_WIDTH_CHARS:
1130 gtk_label_set_width_chars (label, g_value_get_int (value));
1132 case PROP_SINGLE_LINE_MODE:
1133 gtk_label_set_single_line_mode (label, g_value_get_boolean (value));
1136 gtk_label_set_angle (label, g_value_get_double (value));
1138 case PROP_MAX_WIDTH_CHARS:
1139 gtk_label_set_max_width_chars (label, g_value_get_int (value));
1141 case PROP_TRACK_VISITED_LINKS:
1142 gtk_label_set_track_visited_links (label, g_value_get_boolean (value));
1145 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1151 gtk_label_get_property (GObject *object,
1156 GtkLabel *label = GTK_LABEL (object);
1157 GtkLabelPrivate *priv = label->priv;
1162 g_value_set_string (value, priv->label);
1164 case PROP_ATTRIBUTES:
1165 g_value_set_boxed (value, priv->attrs);
1167 case PROP_USE_MARKUP:
1168 g_value_set_boolean (value, priv->use_markup);
1170 case PROP_USE_UNDERLINE:
1171 g_value_set_boolean (value, priv->use_underline);
1174 g_value_set_enum (value, priv->jtype);
1177 g_value_set_boolean (value, priv->wrap);
1179 case PROP_WRAP_MODE:
1180 g_value_set_enum (value, priv->wrap_mode);
1182 case PROP_SELECTABLE:
1183 g_value_set_boolean (value, gtk_label_get_selectable (label));
1185 case PROP_MNEMONIC_KEYVAL:
1186 g_value_set_uint (value, priv->mnemonic_keyval);
1188 case PROP_MNEMONIC_WIDGET:
1189 g_value_set_object (value, (GObject*) priv->mnemonic_widget);
1191 case PROP_CURSOR_POSITION:
1192 g_value_set_int (value, _gtk_label_get_cursor_position (label));
1194 case PROP_SELECTION_BOUND:
1195 g_value_set_int (value, _gtk_label_get_selection_bound (label));
1197 case PROP_ELLIPSIZE:
1198 g_value_set_enum (value, priv->ellipsize);
1200 case PROP_WIDTH_CHARS:
1201 g_value_set_int (value, gtk_label_get_width_chars (label));
1203 case PROP_SINGLE_LINE_MODE:
1204 g_value_set_boolean (value, gtk_label_get_single_line_mode (label));
1207 g_value_set_double (value, gtk_label_get_angle (label));
1209 case PROP_MAX_WIDTH_CHARS:
1210 g_value_set_int (value, gtk_label_get_max_width_chars (label));
1212 case PROP_TRACK_VISITED_LINKS:
1213 g_value_set_boolean (value, gtk_label_get_track_visited_links (label));
1216 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1222 gtk_label_init (GtkLabel *label)
1224 GtkLabelPrivate *priv;
1226 label->priv = G_TYPE_INSTANCE_GET_PRIVATE (label,
1231 gtk_widget_set_has_window (GTK_WIDGET (label), FALSE);
1233 priv->width_chars = -1;
1234 priv->max_width_chars = -1;
1237 priv->jtype = GTK_JUSTIFY_LEFT;
1239 priv->wrap_mode = PANGO_WRAP_WORD;
1240 priv->ellipsize = PANGO_ELLIPSIZE_NONE;
1242 priv->use_underline = FALSE;
1243 priv->use_markup = FALSE;
1244 priv->pattern_set = FALSE;
1245 priv->track_links = TRUE;
1247 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
1248 priv->layout = NULL;
1252 priv->mnemonic_widget = NULL;
1253 priv->mnemonic_window = NULL;
1255 priv->mnemonics_visible = TRUE;
1257 gtk_label_set_text (label, "");
1262 gtk_label_buildable_interface_init (GtkBuildableIface *iface)
1264 buildable_parent_iface = g_type_interface_peek_parent (iface);
1266 iface->custom_tag_start = gtk_label_buildable_custom_tag_start;
1267 iface->custom_finished = gtk_label_buildable_custom_finished;
1271 GtkBuilder *builder;
1273 PangoAttrList *attrs;
1276 static PangoAttribute *
1277 attribute_from_text (GtkBuilder *builder,
1282 PangoAttribute *attribute = NULL;
1284 PangoLanguage *language;
1285 PangoFontDescription *font_desc;
1287 GValue val = G_VALUE_INIT;
1289 if (!gtk_builder_value_from_string_type (builder, PANGO_TYPE_ATTR_TYPE, name, &val, error))
1292 type = g_value_get_enum (&val);
1293 g_value_unset (&val);
1297 /* PangoAttrLanguage */
1298 case PANGO_ATTR_LANGUAGE:
1299 if ((language = pango_language_from_string (value)))
1301 attribute = pango_attr_language_new (language);
1302 g_value_init (&val, G_TYPE_INT);
1306 case PANGO_ATTR_STYLE:
1307 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STYLE, value, &val, error))
1308 attribute = pango_attr_style_new (g_value_get_enum (&val));
1310 case PANGO_ATTR_WEIGHT:
1311 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_WEIGHT, value, &val, error))
1312 attribute = pango_attr_weight_new (g_value_get_enum (&val));
1314 case PANGO_ATTR_VARIANT:
1315 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_VARIANT, value, &val, error))
1316 attribute = pango_attr_variant_new (g_value_get_enum (&val));
1318 case PANGO_ATTR_STRETCH:
1319 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STRETCH, value, &val, error))
1320 attribute = pango_attr_stretch_new (g_value_get_enum (&val));
1322 case PANGO_ATTR_UNDERLINE:
1323 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_UNDERLINE, value, &val, NULL))
1324 attribute = pango_attr_underline_new (g_value_get_enum (&val));
1327 /* XXX: allow boolean for backwards compat, so ignore error */
1328 /* Deprecate this somehow */
1329 g_value_unset (&val);
1330 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1331 attribute = pango_attr_underline_new (g_value_get_boolean (&val));
1334 case PANGO_ATTR_STRIKETHROUGH:
1335 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1336 attribute = pango_attr_strikethrough_new (g_value_get_boolean (&val));
1338 case PANGO_ATTR_GRAVITY:
1339 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY, value, &val, error))
1340 attribute = pango_attr_gravity_new (g_value_get_enum (&val));
1342 case PANGO_ATTR_GRAVITY_HINT:
1343 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY_HINT,
1344 value, &val, error))
1345 attribute = pango_attr_gravity_hint_new (g_value_get_enum (&val));
1347 /* PangoAttrString */
1348 case PANGO_ATTR_FAMILY:
1349 attribute = pango_attr_family_new (value);
1350 g_value_init (&val, G_TYPE_INT);
1354 case PANGO_ATTR_SIZE:
1355 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1356 value, &val, error))
1357 attribute = pango_attr_size_new (g_value_get_int (&val));
1359 case PANGO_ATTR_ABSOLUTE_SIZE:
1360 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1361 value, &val, error))
1362 attribute = pango_attr_size_new_absolute (g_value_get_int (&val));
1365 /* PangoAttrFontDesc */
1366 case PANGO_ATTR_FONT_DESC:
1367 if ((font_desc = pango_font_description_from_string (value)))
1369 attribute = pango_attr_font_desc_new (font_desc);
1370 pango_font_description_free (font_desc);
1371 g_value_init (&val, G_TYPE_INT);
1375 /* PangoAttrColor */
1376 case PANGO_ATTR_FOREGROUND:
1377 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1378 value, &val, error))
1380 color = g_value_get_boxed (&val);
1381 attribute = pango_attr_foreground_new (color->red, color->green, color->blue);
1384 case PANGO_ATTR_BACKGROUND:
1385 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1386 value, &val, error))
1388 color = g_value_get_boxed (&val);
1389 attribute = pango_attr_background_new (color->red, color->green, color->blue);
1392 case PANGO_ATTR_UNDERLINE_COLOR:
1393 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1394 value, &val, error))
1396 color = g_value_get_boxed (&val);
1397 attribute = pango_attr_underline_color_new (color->red, color->green, color->blue);
1400 case PANGO_ATTR_STRIKETHROUGH_COLOR:
1401 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1402 value, &val, error))
1404 color = g_value_get_boxed (&val);
1405 attribute = pango_attr_strikethrough_color_new (color->red, color->green, color->blue);
1409 /* PangoAttrShape */
1410 case PANGO_ATTR_SHAPE:
1411 /* Unsupported for now */
1413 /* PangoAttrFloat */
1414 case PANGO_ATTR_SCALE:
1415 if (gtk_builder_value_from_string_type (builder, G_TYPE_DOUBLE,
1416 value, &val, error))
1417 attribute = pango_attr_scale_new (g_value_get_double (&val));
1420 case PANGO_ATTR_INVALID:
1421 case PANGO_ATTR_LETTER_SPACING:
1422 case PANGO_ATTR_RISE:
1423 case PANGO_ATTR_FALLBACK:
1428 g_value_unset (&val);
1435 pango_start_element (GMarkupParseContext *context,
1436 const gchar *element_name,
1437 const gchar **names,
1438 const gchar **values,
1442 PangoParserData *data = (PangoParserData*)user_data;
1443 GValue val = G_VALUE_INIT;
1445 gint line_number, char_number;
1447 if (strcmp (element_name, "attribute") == 0)
1449 PangoAttribute *attr = NULL;
1450 const gchar *name = NULL;
1451 const gchar *value = NULL;
1452 const gchar *start = NULL;
1453 const gchar *end = NULL;
1454 guint start_val = 0;
1455 guint end_val = G_MAXUINT;
1457 for (i = 0; names[i]; i++)
1459 if (strcmp (names[i], "name") == 0)
1461 else if (strcmp (names[i], "value") == 0)
1463 else if (strcmp (names[i], "start") == 0)
1465 else if (strcmp (names[i], "end") == 0)
1469 g_markup_parse_context_get_position (context,
1474 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1475 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1477 line_number, char_number, names[i], "attribute");
1482 if (!name || !value)
1484 g_markup_parse_context_get_position (context,
1489 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1490 "%s:%d:%d <%s> requires attribute \"%s\"",
1492 line_number, char_number, "attribute",
1493 name ? "value" : "name");
1499 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1500 start, &val, error))
1502 start_val = g_value_get_uint (&val);
1503 g_value_unset (&val);
1508 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1511 end_val = g_value_get_uint (&val);
1512 g_value_unset (&val);
1515 attr = attribute_from_text (data->builder, name, value, error);
1519 attr->start_index = start_val;
1520 attr->end_index = end_val;
1523 data->attrs = pango_attr_list_new ();
1525 pango_attr_list_insert (data->attrs, attr);
1528 else if (strcmp (element_name, "attributes") == 0)
1531 g_warning ("Unsupported tag for GtkLabel: %s\n", element_name);
1534 static const GMarkupParser pango_parser =
1536 pango_start_element,
1540 gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
1541 GtkBuilder *builder,
1543 const gchar *tagname,
1544 GMarkupParser *parser,
1547 if (buildable_parent_iface->custom_tag_start (buildable, builder, child,
1548 tagname, parser, data))
1551 if (strcmp (tagname, "attributes") == 0)
1553 PangoParserData *parser_data;
1555 parser_data = g_slice_new0 (PangoParserData);
1556 parser_data->builder = g_object_ref (builder);
1557 parser_data->object = g_object_ref (buildable);
1558 *parser = pango_parser;
1559 *data = parser_data;
1566 gtk_label_buildable_custom_finished (GtkBuildable *buildable,
1567 GtkBuilder *builder,
1569 const gchar *tagname,
1572 PangoParserData *data;
1574 buildable_parent_iface->custom_finished (buildable, builder, child,
1575 tagname, user_data);
1577 if (strcmp (tagname, "attributes") == 0)
1579 data = (PangoParserData*)user_data;
1583 gtk_label_set_attributes (GTK_LABEL (buildable), data->attrs);
1584 pango_attr_list_unref (data->attrs);
1587 g_object_unref (data->object);
1588 g_object_unref (data->builder);
1589 g_slice_free (PangoParserData, data);
1596 * @str: The text of the label
1598 * Creates a new label with the given text inside it. You can
1599 * pass %NULL to get an empty label widget.
1601 * Return value: the new #GtkLabel
1604 gtk_label_new (const gchar *str)
1608 label = g_object_new (GTK_TYPE_LABEL, NULL);
1611 gtk_label_set_text (label, str);
1613 return GTK_WIDGET (label);
1617 * gtk_label_new_with_mnemonic:
1618 * @str: The text of the label, with an underscore in front of the
1619 * mnemonic character
1621 * Creates a new #GtkLabel, containing the text in @str.
1623 * If characters in @str are preceded by an underscore, they are
1624 * underlined. If you need a literal underscore character in a label, use
1625 * '__' (two underscores). The first underlined character represents a
1626 * keyboard accelerator called a mnemonic. The mnemonic key can be used
1627 * to activate another widget, chosen automatically, or explicitly using
1628 * gtk_label_set_mnemonic_widget().
1630 * If gtk_label_set_mnemonic_widget() is not called, then the first
1631 * activatable ancestor of the #GtkLabel will be chosen as the mnemonic
1632 * widget. For instance, if the label is inside a button or menu item,
1633 * the button or menu item will automatically become the mnemonic widget
1634 * and be activated by the mnemonic.
1636 * Return value: the new #GtkLabel
1639 gtk_label_new_with_mnemonic (const gchar *str)
1643 label = g_object_new (GTK_TYPE_LABEL, NULL);
1646 gtk_label_set_text_with_mnemonic (label, str);
1648 return GTK_WIDGET (label);
1652 gtk_label_mnemonic_activate (GtkWidget *widget,
1653 gboolean group_cycling)
1655 GtkLabel *label = GTK_LABEL (widget);
1656 GtkLabelPrivate *priv = label->priv;
1659 if (priv->mnemonic_widget)
1660 return gtk_widget_mnemonic_activate (priv->mnemonic_widget, group_cycling);
1662 /* Try to find the widget to activate by traversing the
1663 * widget's ancestry.
1665 parent = gtk_widget_get_parent (widget);
1667 if (GTK_IS_NOTEBOOK (parent))
1672 if (gtk_widget_get_can_focus (parent) ||
1673 (!group_cycling && GTK_WIDGET_GET_CLASS (parent)->activate_signal) ||
1674 GTK_IS_NOTEBOOK (gtk_widget_get_parent (parent)) ||
1675 GTK_IS_MENU_ITEM (parent))
1676 return gtk_widget_mnemonic_activate (parent, group_cycling);
1677 parent = gtk_widget_get_parent (parent);
1680 /* barf if there was nothing to activate */
1681 g_warning ("Couldn't find a target for a mnemonic activation.");
1682 gtk_widget_error_bell (widget);
1688 gtk_label_setup_mnemonic (GtkLabel *label,
1691 GtkLabelPrivate *priv = label->priv;
1692 GtkWidget *widget = GTK_WIDGET (label);
1693 GtkWidget *toplevel;
1694 GtkWidget *mnemonic_menu;
1696 mnemonic_menu = g_object_get_data (G_OBJECT (label), "gtk-mnemonic-menu");
1698 if (last_key != GDK_KEY_VoidSymbol)
1700 if (priv->mnemonic_window)
1702 gtk_window_remove_mnemonic (priv->mnemonic_window,
1705 priv->mnemonic_window = NULL;
1709 _gtk_menu_shell_remove_mnemonic (GTK_MENU_SHELL (mnemonic_menu),
1712 mnemonic_menu = NULL;
1716 if (priv->mnemonic_keyval == GDK_KEY_VoidSymbol)
1719 connect_mnemonics_visible_notify (GTK_LABEL (widget));
1721 toplevel = gtk_widget_get_toplevel (widget);
1722 if (gtk_widget_is_toplevel (toplevel))
1724 GtkWidget *menu_shell;
1726 menu_shell = gtk_widget_get_ancestor (widget,
1727 GTK_TYPE_MENU_SHELL);
1731 _gtk_menu_shell_add_mnemonic (GTK_MENU_SHELL (menu_shell),
1732 priv->mnemonic_keyval,
1734 mnemonic_menu = menu_shell;
1737 if (!GTK_IS_MENU (menu_shell))
1739 gtk_window_add_mnemonic (GTK_WINDOW (toplevel),
1740 priv->mnemonic_keyval,
1742 priv->mnemonic_window = GTK_WINDOW (toplevel);
1747 g_object_set_data (G_OBJECT (label), I_("gtk-mnemonic-menu"), mnemonic_menu);
1751 gtk_label_hierarchy_changed (GtkWidget *widget,
1752 GtkWidget *old_toplevel)
1754 GtkLabel *label = GTK_LABEL (widget);
1755 GtkLabelPrivate *priv = label->priv;
1757 gtk_label_setup_mnemonic (label, priv->mnemonic_keyval);
1761 label_shortcut_setting_apply (GtkLabel *label)
1763 gtk_label_recalculate (label);
1764 if (GTK_IS_ACCEL_LABEL (label))
1765 gtk_accel_label_refetch (GTK_ACCEL_LABEL (label));
1769 label_shortcut_setting_traverse_container (GtkWidget *widget,
1772 if (GTK_IS_LABEL (widget))
1773 label_shortcut_setting_apply (GTK_LABEL (widget));
1774 else if (GTK_IS_CONTAINER (widget))
1775 gtk_container_forall (GTK_CONTAINER (widget),
1776 label_shortcut_setting_traverse_container, data);
1780 label_shortcut_setting_changed (GtkSettings *settings)
1784 list = gtk_window_list_toplevels ();
1786 for (l = list; l ; l = l->next)
1788 GtkWidget *widget = l->data;
1790 if (gtk_widget_get_settings (widget) == settings)
1791 gtk_container_forall (GTK_CONTAINER (widget),
1792 label_shortcut_setting_traverse_container, NULL);
1799 mnemonics_visible_apply (GtkWidget *widget,
1800 gboolean mnemonics_visible)
1802 GtkLabel *label = GTK_LABEL (widget);
1803 GtkLabelPrivate *priv = label->priv;
1805 mnemonics_visible = mnemonics_visible != FALSE;
1807 if (priv->mnemonics_visible != mnemonics_visible)
1809 priv->mnemonics_visible = mnemonics_visible;
1811 gtk_label_recalculate (label);
1816 label_mnemonics_visible_traverse_container (GtkWidget *widget,
1819 gboolean mnemonics_visible = GPOINTER_TO_INT (data);
1821 _gtk_label_mnemonics_visible_apply_recursively (widget, mnemonics_visible);
1825 _gtk_label_mnemonics_visible_apply_recursively (GtkWidget *widget,
1826 gboolean mnemonics_visible)
1828 if (GTK_IS_LABEL (widget))
1829 mnemonics_visible_apply (widget, mnemonics_visible);
1830 else if (GTK_IS_CONTAINER (widget))
1831 gtk_container_forall (GTK_CONTAINER (widget),
1832 label_mnemonics_visible_traverse_container,
1833 GINT_TO_POINTER (mnemonics_visible));
1837 label_mnemonics_visible_changed (GtkWindow *window,
1841 gboolean mnemonics_visible;
1843 g_object_get (window, "mnemonics-visible", &mnemonics_visible, NULL);
1845 gtk_container_forall (GTK_CONTAINER (window),
1846 label_mnemonics_visible_traverse_container,
1847 GINT_TO_POINTER (mnemonics_visible));
1851 gtk_label_screen_changed (GtkWidget *widget,
1852 GdkScreen *old_screen)
1854 GtkSettings *settings;
1855 gboolean shortcuts_connected;
1857 if (!gtk_widget_has_screen (widget))
1860 settings = gtk_widget_get_settings (widget);
1862 shortcuts_connected =
1863 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (settings),
1864 "gtk-label-shortcuts-connected"));
1866 if (! shortcuts_connected)
1868 g_signal_connect (settings, "notify::gtk-enable-mnemonics",
1869 G_CALLBACK (label_shortcut_setting_changed),
1871 g_signal_connect (settings, "notify::gtk-enable-accels",
1872 G_CALLBACK (label_shortcut_setting_changed),
1875 g_object_set_data (G_OBJECT (settings), "gtk-label-shortcuts-connected",
1876 GINT_TO_POINTER (TRUE));
1879 label_shortcut_setting_apply (GTK_LABEL (widget));
1884 label_mnemonic_widget_weak_notify (gpointer data,
1885 GObject *where_the_object_was)
1887 GtkLabel *label = data;
1888 GtkLabelPrivate *priv = label->priv;
1890 priv->mnemonic_widget = NULL;
1891 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1895 * gtk_label_set_mnemonic_widget:
1896 * @label: a #GtkLabel
1897 * @widget: (allow-none): the target #GtkWidget
1899 * If the label has been set so that it has an mnemonic key (using
1900 * i.e. gtk_label_set_markup_with_mnemonic(),
1901 * gtk_label_set_text_with_mnemonic(), gtk_label_new_with_mnemonic()
1902 * or the "use_underline" property) the label can be associated with a
1903 * widget that is the target of the mnemonic. When the label is inside
1904 * a widget (like a #GtkButton or a #GtkNotebook tab) it is
1905 * automatically associated with the correct widget, but sometimes
1906 * (i.e. when the target is a #GtkEntry next to the label) you need to
1907 * set it explicitly using this function.
1909 * The target widget will be accelerated by emitting the
1910 * GtkWidget::mnemonic-activate signal on it. The default handler for
1911 * this signal will activate the widget if there are no mnemonic collisions
1912 * and toggle focus between the colliding widgets otherwise.
1915 gtk_label_set_mnemonic_widget (GtkLabel *label,
1918 GtkLabelPrivate *priv;
1920 g_return_if_fail (GTK_IS_LABEL (label));
1925 g_return_if_fail (GTK_IS_WIDGET (widget));
1927 if (priv->mnemonic_widget)
1929 gtk_widget_remove_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1930 g_object_weak_unref (G_OBJECT (priv->mnemonic_widget),
1931 label_mnemonic_widget_weak_notify,
1934 priv->mnemonic_widget = widget;
1935 if (priv->mnemonic_widget)
1937 g_object_weak_ref (G_OBJECT (priv->mnemonic_widget),
1938 label_mnemonic_widget_weak_notify,
1940 gtk_widget_add_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1943 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1947 * gtk_label_get_mnemonic_widget:
1948 * @label: a #GtkLabel
1950 * Retrieves the target of the mnemonic (keyboard shortcut) of this
1951 * label. See gtk_label_set_mnemonic_widget().
1953 * Return value: (transfer none): the target of the label's mnemonic,
1954 * or %NULL if none has been set and the default algorithm will be used.
1957 gtk_label_get_mnemonic_widget (GtkLabel *label)
1959 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1961 return label->priv->mnemonic_widget;
1965 * gtk_label_get_mnemonic_keyval:
1966 * @label: a #GtkLabel
1968 * If the label has been set so that it has an mnemonic key this function
1969 * returns the keyval used for the mnemonic accelerator. If there is no
1970 * mnemonic set up it returns #GDK_VoidSymbol.
1972 * Returns: GDK keyval usable for accelerators, or #GDK_VoidSymbol
1975 gtk_label_get_mnemonic_keyval (GtkLabel *label)
1977 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_KEY_VoidSymbol);
1979 return label->priv->mnemonic_keyval;
1983 gtk_label_set_text_internal (GtkLabel *label,
1986 GtkLabelPrivate *priv = label->priv;
1988 g_free (priv->text);
1992 gtk_label_select_region_index (label, 0, 0);
1996 gtk_label_set_label_internal (GtkLabel *label,
1999 GtkLabelPrivate *priv = label->priv;
2001 g_free (priv->label);
2005 g_object_notify (G_OBJECT (label), "label");
2009 gtk_label_set_use_markup_internal (GtkLabel *label,
2012 GtkLabelPrivate *priv = label->priv;
2015 if (priv->use_markup != val)
2017 priv->use_markup = val;
2019 g_object_notify (G_OBJECT (label), "use-markup");
2024 gtk_label_set_use_underline_internal (GtkLabel *label,
2027 GtkLabelPrivate *priv = label->priv;
2030 if (priv->use_underline != val)
2032 priv->use_underline = val;
2034 g_object_notify (G_OBJECT (label), "use-underline");
2039 gtk_label_compose_effective_attrs (GtkLabel *label)
2041 GtkLabelPrivate *priv = label->priv;
2042 PangoAttrIterator *iter;
2043 PangoAttribute *attr;
2044 GSList *iter_attrs, *l;
2048 if (priv->effective_attrs)
2050 if ((iter = pango_attr_list_get_iterator (priv->attrs)))
2054 iter_attrs = pango_attr_iterator_get_attrs (iter);
2055 for (l = iter_attrs; l; l = l->next)
2058 pango_attr_list_insert (priv->effective_attrs, attr);
2060 g_slist_free (iter_attrs);
2062 while (pango_attr_iterator_next (iter));
2063 pango_attr_iterator_destroy (iter);
2067 priv->effective_attrs =
2068 pango_attr_list_ref (priv->attrs);
2073 gtk_label_set_attributes_internal (GtkLabel *label,
2074 PangoAttrList *attrs)
2076 GtkLabelPrivate *priv = label->priv;
2079 pango_attr_list_ref (attrs);
2082 pango_attr_list_unref (priv->attrs);
2083 priv->attrs = attrs;
2085 g_object_notify (G_OBJECT (label), "attributes");
2089 /* Calculates text, attrs and mnemonic_keyval from
2090 * label, use_underline and use_markup
2093 gtk_label_recalculate (GtkLabel *label)
2095 GtkLabelPrivate *priv = label->priv;
2096 guint keyval = priv->mnemonic_keyval;
2098 if (priv->use_markup)
2099 gtk_label_set_markup_internal (label, priv->label, priv->use_underline);
2100 else if (priv->use_underline)
2101 gtk_label_set_uline_text_internal (label, priv->label);
2104 if (!priv->pattern_set)
2106 if (priv->effective_attrs)
2107 pango_attr_list_unref (priv->effective_attrs);
2108 priv->effective_attrs = NULL;
2110 gtk_label_set_text_internal (label, g_strdup (priv->label));
2113 gtk_label_compose_effective_attrs (label);
2115 if (!priv->use_underline)
2116 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
2118 if (keyval != priv->mnemonic_keyval)
2120 gtk_label_setup_mnemonic (label, keyval);
2121 g_object_notify (G_OBJECT (label), "mnemonic-keyval");
2124 gtk_label_clear_layout (label);
2125 gtk_label_clear_select_info (label);
2126 gtk_widget_queue_resize (GTK_WIDGET (label));
2130 * gtk_label_set_text:
2131 * @label: a #GtkLabel
2132 * @str: The text you want to set
2134 * Sets the text within the #GtkLabel widget. It overwrites any text that
2137 * This will also clear any previously set mnemonic accelerators.
2140 gtk_label_set_text (GtkLabel *label,
2143 g_return_if_fail (GTK_IS_LABEL (label));
2145 g_object_freeze_notify (G_OBJECT (label));
2147 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2148 gtk_label_set_use_markup_internal (label, FALSE);
2149 gtk_label_set_use_underline_internal (label, FALSE);
2151 gtk_label_recalculate (label);
2153 g_object_thaw_notify (G_OBJECT (label));
2157 * gtk_label_set_attributes:
2158 * @label: a #GtkLabel
2159 * @attrs: a #PangoAttrList
2161 * Sets a #PangoAttrList; the attributes in the list are applied to the
2164 * <note><para>The attributes set with this function will be applied
2165 * and merged with any other attributes previously effected by way
2166 * of the #GtkLabel:use-underline or #GtkLabel:use-markup properties.
2167 * While it is not recommended to mix markup strings with manually set
2168 * attributes, if you must; know that the attributes will be applied
2169 * to the label after the markup string is parsed.</para></note>
2172 gtk_label_set_attributes (GtkLabel *label,
2173 PangoAttrList *attrs)
2175 g_return_if_fail (GTK_IS_LABEL (label));
2177 gtk_label_set_attributes_internal (label, attrs);
2179 gtk_label_recalculate (label);
2181 gtk_label_clear_layout (label);
2182 gtk_widget_queue_resize (GTK_WIDGET (label));
2186 * gtk_label_get_attributes:
2187 * @label: a #GtkLabel
2189 * Gets the attribute list that was set on the label using
2190 * gtk_label_set_attributes(), if any. This function does
2191 * not reflect attributes that come from the labels markup
2192 * (see gtk_label_set_markup()). If you want to get the
2193 * effective attributes for the label, use
2194 * pango_layout_get_attribute (gtk_label_get_layout (label)).
2196 * Return value: (transfer none): the attribute list, or %NULL
2200 gtk_label_get_attributes (GtkLabel *label)
2202 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2204 return label->priv->attrs;
2208 * gtk_label_set_label:
2209 * @label: a #GtkLabel
2210 * @str: the new text to set for the label
2212 * Sets the text of the label. The label is interpreted as
2213 * including embedded underlines and/or Pango markup depending
2214 * on the values of the #GtkLabel:use-underline" and
2215 * #GtkLabel:use-markup properties.
2218 gtk_label_set_label (GtkLabel *label,
2221 g_return_if_fail (GTK_IS_LABEL (label));
2223 g_object_freeze_notify (G_OBJECT (label));
2225 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2226 gtk_label_recalculate (label);
2228 g_object_thaw_notify (G_OBJECT (label));
2232 * gtk_label_get_label:
2233 * @label: a #GtkLabel
2235 * Fetches the text from a label widget including any embedded
2236 * underlines indicating mnemonics and Pango markup. (See
2237 * gtk_label_get_text()).
2239 * Return value: the text of the label widget. This string is
2240 * owned by the widget and must not be modified or freed.
2243 gtk_label_get_label (GtkLabel *label)
2245 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2247 return label->priv->label;
2255 GdkColor *link_color;
2256 GdkColor *visited_link_color;
2260 start_element_handler (GMarkupParseContext *context,
2261 const gchar *element_name,
2262 const gchar **attribute_names,
2263 const gchar **attribute_values,
2267 GtkLabelPrivate *priv;
2268 UriParserData *pdata = user_data;
2270 if (strcmp (element_name, "a") == 0)
2273 const gchar *uri = NULL;
2274 const gchar *title = NULL;
2275 gboolean visited = FALSE;
2279 GdkColor *color = NULL;
2281 g_markup_parse_context_get_position (context, &line_number, &char_number);
2283 for (i = 0; attribute_names[i] != NULL; i++)
2285 const gchar *attr = attribute_names[i];
2287 if (strcmp (attr, "href") == 0)
2288 uri = attribute_values[i];
2289 else if (strcmp (attr, "title") == 0)
2290 title = attribute_values[i];
2295 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
2296 "Attribute '%s' is not allowed on the <a> tag "
2297 "on line %d char %d",
2298 attr, line_number, char_number);
2307 G_MARKUP_ERROR_INVALID_CONTENT,
2308 "Attribute 'href' was missing on the <a> tag "
2309 "on line %d char %d",
2310 line_number, char_number);
2315 priv = pdata->label->priv;
2316 if (priv->track_links && priv->select_info)
2319 for (l = priv->select_info->links; l; l = l->next)
2322 if (strcmp (uri, link->uri) == 0)
2324 visited = link->visited;
2331 color = pdata->visited_link_color;
2333 color = pdata->link_color;
2335 g_string_append_printf (pdata->new_str,
2336 "<span color=\"#%04x%04x%04x\" underline=\"single\">",
2341 link = g_new0 (GtkLabelLink, 1);
2342 link->uri = g_strdup (uri);
2343 link->title = g_strdup (title);
2344 link->visited = visited;
2345 pdata->links = g_list_append (pdata->links, link);
2351 g_string_append_c (pdata->new_str, '<');
2352 g_string_append (pdata->new_str, element_name);
2354 for (i = 0; attribute_names[i] != NULL; i++)
2356 const gchar *attr = attribute_names[i];
2357 const gchar *value = attribute_values[i];
2360 newvalue = g_markup_escape_text (value, -1);
2362 g_string_append_c (pdata->new_str, ' ');
2363 g_string_append (pdata->new_str, attr);
2364 g_string_append (pdata->new_str, "=\"");
2365 g_string_append (pdata->new_str, newvalue);
2366 g_string_append_c (pdata->new_str, '\"');
2370 g_string_append_c (pdata->new_str, '>');
2375 end_element_handler (GMarkupParseContext *context,
2376 const gchar *element_name,
2380 UriParserData *pdata = user_data;
2382 if (!strcmp (element_name, "a"))
2383 g_string_append (pdata->new_str, "</span>");
2386 g_string_append (pdata->new_str, "</");
2387 g_string_append (pdata->new_str, element_name);
2388 g_string_append_c (pdata->new_str, '>');
2393 text_handler (GMarkupParseContext *context,
2399 UriParserData *pdata = user_data;
2402 newtext = g_markup_escape_text (text, text_len);
2403 g_string_append (pdata->new_str, newtext);
2407 static const GMarkupParser markup_parser =
2409 start_element_handler,
2410 end_element_handler,
2417 xml_isspace (gchar c)
2419 return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
2423 link_free (GtkLabelLink *link)
2426 g_free (link->title);
2431 gtk_label_get_link_colors (GtkWidget *widget,
2432 GdkColor **link_color,
2433 GdkColor **visited_link_color)
2435 GtkStyleContext *context;
2437 context = gtk_widget_get_style_context (widget);
2438 gtk_style_context_get_style (context,
2439 "link-color", link_color,
2440 "visited-link-color", visited_link_color,
2443 *link_color = gdk_color_copy (&default_link_color);
2444 if (!*visited_link_color)
2445 *visited_link_color = gdk_color_copy (&default_visited_link_color);
2449 parse_uri_markup (GtkLabel *label,
2455 GMarkupParseContext *context = NULL;
2456 const gchar *p, *end;
2457 gboolean needs_root = TRUE;
2459 UriParserData pdata;
2461 length = strlen (str);
2465 pdata.label = label;
2467 pdata.new_str = g_string_sized_new (length);
2469 gtk_label_get_link_colors (GTK_WIDGET (label), &pdata.link_color, &pdata.visited_link_color);
2471 while (p != end && xml_isspace (*p))
2474 if (end - p >= 8 && strncmp (p, "<markup>", 8) == 0)
2477 context = g_markup_parse_context_new (&markup_parser, 0, &pdata, NULL);
2481 if (!g_markup_parse_context_parse (context, "<markup>", -1, error))
2485 if (!g_markup_parse_context_parse (context, str, length, error))
2490 if (!g_markup_parse_context_parse (context, "</markup>", -1, error))
2494 if (!g_markup_parse_context_end_parse (context, error))
2497 g_markup_parse_context_free (context);
2499 *new_str = g_string_free (pdata.new_str, FALSE);
2500 *links = pdata.links;
2502 gdk_color_free (pdata.link_color);
2503 gdk_color_free (pdata.visited_link_color);
2508 g_markup_parse_context_free (context);
2509 g_string_free (pdata.new_str, TRUE);
2510 g_list_foreach (pdata.links, (GFunc)link_free, NULL);
2511 g_list_free (pdata.links);
2512 gdk_color_free (pdata.link_color);
2513 gdk_color_free (pdata.visited_link_color);
2519 gtk_label_ensure_has_tooltip (GtkLabel *label)
2521 GtkLabelPrivate *priv = label->priv;
2523 gboolean has_tooltip = FALSE;
2525 for (l = priv->select_info->links; l; l = l->next)
2527 GtkLabelLink *link = l->data;
2535 gtk_widget_set_has_tooltip (GTK_WIDGET (label), has_tooltip);
2539 gtk_label_set_markup_internal (GtkLabel *label,
2541 gboolean with_uline)
2543 GtkLabelPrivate *priv = label->priv;
2545 GError *error = NULL;
2546 PangoAttrList *attrs = NULL;
2547 gunichar accel_char = 0;
2549 GList *links = NULL;
2551 if (!parse_uri_markup (label, str, &new_str, &links, &error))
2553 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2555 g_error_free (error);
2559 gtk_label_clear_links (label);
2562 gtk_label_ensure_select_info (label);
2563 priv->select_info->links = links;
2564 gtk_label_ensure_has_tooltip (label);
2569 gboolean enable_mnemonics;
2570 gboolean auto_mnemonics;
2572 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2573 "gtk-enable-mnemonics", &enable_mnemonics,
2574 "gtk-auto-mnemonics", &auto_mnemonics,
2577 if (!(enable_mnemonics && priv->mnemonics_visible &&
2579 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2580 (!priv->mnemonic_widget ||
2581 gtk_widget_is_sensitive (priv->mnemonic_widget))))))
2587 if (separate_uline_pattern (new_str, &key, &tmp, &pattern))
2596 if (!pango_parse_markup (new_str,
2598 with_uline ? '_' : 0,
2601 with_uline ? &accel_char : NULL,
2604 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2607 g_error_free (error);
2614 gtk_label_set_text_internal (label, text);
2618 if (priv->effective_attrs)
2619 pango_attr_list_unref (priv->effective_attrs);
2620 priv->effective_attrs = attrs;
2623 if (accel_char != 0)
2624 priv->mnemonic_keyval = gdk_keyval_to_lower (gdk_unicode_to_keyval (accel_char));
2626 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
2630 * gtk_label_set_markup:
2631 * @label: a #GtkLabel
2632 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
2634 * Parses @str which is marked up with the <link
2635 * linkend="PangoMarkupFormat">Pango text markup language</link>, setting the
2636 * label's text and attribute list based on the parse results. If the @str is
2637 * external data, you may need to escape it with g_markup_escape_text() or
2638 * g_markup_printf_escaped()<!-- -->:
2642 * markup = g_markup_printf_escaped ("<span style=\"italic\">%s</span>", str);
2643 * gtk_label_set_markup (GTK_LABEL (label), markup);
2648 gtk_label_set_markup (GtkLabel *label,
2651 g_return_if_fail (GTK_IS_LABEL (label));
2653 g_object_freeze_notify (G_OBJECT (label));
2655 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2656 gtk_label_set_use_markup_internal (label, TRUE);
2657 gtk_label_set_use_underline_internal (label, FALSE);
2659 gtk_label_recalculate (label);
2661 g_object_thaw_notify (G_OBJECT (label));
2665 * gtk_label_set_markup_with_mnemonic:
2666 * @label: a #GtkLabel
2667 * @str: a markup string (see
2668 * <link linkend="PangoMarkupFormat">Pango markup format</link>)
2670 * Parses @str which is marked up with the
2671 * <link linkend="PangoMarkupFormat">Pango text markup language</link>,
2672 * setting the label's text and attribute list based on the parse results.
2673 * If characters in @str are preceded by an underscore, they are underlined
2674 * indicating that they represent a keyboard accelerator called a mnemonic.
2676 * The mnemonic key can be used to activate another widget, chosen
2677 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
2680 gtk_label_set_markup_with_mnemonic (GtkLabel *label,
2683 g_return_if_fail (GTK_IS_LABEL (label));
2685 g_object_freeze_notify (G_OBJECT (label));
2687 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2688 gtk_label_set_use_markup_internal (label, TRUE);
2689 gtk_label_set_use_underline_internal (label, TRUE);
2691 gtk_label_recalculate (label);
2693 g_object_thaw_notify (G_OBJECT (label));
2697 * gtk_label_get_text:
2698 * @label: a #GtkLabel
2700 * Fetches the text from a label widget, as displayed on the
2701 * screen. This does not include any embedded underlines
2702 * indicating mnemonics or Pango markup. (See gtk_label_get_label())
2704 * Return value: the text in the label widget. This is the internal
2705 * string used by the label, and must not be modified.
2708 gtk_label_get_text (GtkLabel *label)
2710 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2712 return label->priv->text;
2715 static PangoAttrList *
2716 gtk_label_pattern_to_attrs (GtkLabel *label,
2717 const gchar *pattern)
2719 GtkLabelPrivate *priv = label->priv;
2721 const char *p = priv->text;
2722 const char *q = pattern;
2723 PangoAttrList *attrs;
2725 attrs = pango_attr_list_new ();
2729 while (*p && *q && *q != '_')
2731 p = g_utf8_next_char (p);
2735 while (*p && *q && *q == '_')
2737 p = g_utf8_next_char (p);
2743 PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_LOW);
2744 attr->start_index = start - priv->text;
2745 attr->end_index = p - priv->text;
2747 pango_attr_list_insert (attrs, attr);
2757 gtk_label_set_pattern_internal (GtkLabel *label,
2758 const gchar *pattern,
2759 gboolean is_mnemonic)
2761 GtkLabelPrivate *priv = label->priv;
2762 PangoAttrList *attrs;
2763 gboolean enable_mnemonics;
2764 gboolean auto_mnemonics;
2766 if (priv->pattern_set)
2771 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2772 "gtk-enable-mnemonics", &enable_mnemonics,
2773 "gtk-auto-mnemonics", &auto_mnemonics,
2776 if (enable_mnemonics && priv->mnemonics_visible && pattern &&
2778 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2779 (!priv->mnemonic_widget ||
2780 gtk_widget_is_sensitive (priv->mnemonic_widget)))))
2781 attrs = gtk_label_pattern_to_attrs (label, pattern);
2786 attrs = gtk_label_pattern_to_attrs (label, pattern);
2788 if (priv->effective_attrs)
2789 pango_attr_list_unref (priv->effective_attrs);
2790 priv->effective_attrs = attrs;
2794 * gtk_label_set_pattern:
2795 * @label: The #GtkLabel you want to set the pattern to.
2796 * @pattern: The pattern as described above.
2798 * The pattern of underlines you want under the existing text within the
2799 * #GtkLabel widget. For example if the current text of the label says
2800 * "FooBarBaz" passing a pattern of "___ ___" will underline
2801 * "Foo" and "Baz" but not "Bar".
2804 gtk_label_set_pattern (GtkLabel *label,
2805 const gchar *pattern)
2807 GtkLabelPrivate *priv;
2809 g_return_if_fail (GTK_IS_LABEL (label));
2813 priv->pattern_set = FALSE;
2817 gtk_label_set_pattern_internal (label, pattern, FALSE);
2818 priv->pattern_set = TRUE;
2821 gtk_label_recalculate (label);
2823 gtk_label_clear_layout (label);
2824 gtk_widget_queue_resize (GTK_WIDGET (label));
2829 * gtk_label_set_justify:
2830 * @label: a #GtkLabel
2831 * @jtype: a #GtkJustification
2833 * Sets the alignment of the lines in the text of the label relative to
2834 * each other. %GTK_JUSTIFY_LEFT is the default value when the
2835 * widget is first created with gtk_label_new(). If you instead want
2836 * to set the alignment of the label as a whole, use
2837 * gtk_misc_set_alignment() instead. gtk_label_set_justify() has no
2838 * effect on labels containing only a single line.
2841 gtk_label_set_justify (GtkLabel *label,
2842 GtkJustification jtype)
2844 GtkLabelPrivate *priv;
2846 g_return_if_fail (GTK_IS_LABEL (label));
2847 g_return_if_fail (jtype >= GTK_JUSTIFY_LEFT && jtype <= GTK_JUSTIFY_FILL);
2851 if ((GtkJustification) priv->jtype != jtype)
2853 priv->jtype = jtype;
2855 /* No real need to be this drastic, but easier than duplicating the code */
2856 gtk_label_clear_layout (label);
2858 g_object_notify (G_OBJECT (label), "justify");
2859 gtk_widget_queue_resize (GTK_WIDGET (label));
2864 * gtk_label_get_justify:
2865 * @label: a #GtkLabel
2867 * Returns the justification of the label. See gtk_label_set_justify().
2869 * Return value: #GtkJustification
2872 gtk_label_get_justify (GtkLabel *label)
2874 g_return_val_if_fail (GTK_IS_LABEL (label), 0);
2876 return label->priv->jtype;
2880 * gtk_label_set_ellipsize:
2881 * @label: a #GtkLabel
2882 * @mode: a #PangoEllipsizeMode
2884 * Sets the mode used to ellipsize (add an ellipsis: "...") to the text
2885 * if there is not enough space to render the entire string.
2890 gtk_label_set_ellipsize (GtkLabel *label,
2891 PangoEllipsizeMode mode)
2893 GtkLabelPrivate *priv;
2895 g_return_if_fail (GTK_IS_LABEL (label));
2896 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE && mode <= PANGO_ELLIPSIZE_END);
2900 if ((PangoEllipsizeMode) priv->ellipsize != mode)
2902 priv->ellipsize = mode;
2904 /* No real need to be this drastic, but easier than duplicating the code */
2905 gtk_label_clear_layout (label);
2907 g_object_notify (G_OBJECT (label), "ellipsize");
2908 gtk_widget_queue_resize (GTK_WIDGET (label));
2913 * gtk_label_get_ellipsize:
2914 * @label: a #GtkLabel
2916 * Returns the ellipsizing position of the label. See gtk_label_set_ellipsize().
2918 * Return value: #PangoEllipsizeMode
2923 gtk_label_get_ellipsize (GtkLabel *label)
2925 g_return_val_if_fail (GTK_IS_LABEL (label), PANGO_ELLIPSIZE_NONE);
2927 return label->priv->ellipsize;
2931 * gtk_label_set_width_chars:
2932 * @label: a #GtkLabel
2933 * @n_chars: the new desired width, in characters.
2935 * Sets the desired width in characters of @label to @n_chars.
2940 gtk_label_set_width_chars (GtkLabel *label,
2943 GtkLabelPrivate *priv;
2945 g_return_if_fail (GTK_IS_LABEL (label));
2949 if (priv->width_chars != n_chars)
2951 priv->width_chars = n_chars;
2952 g_object_notify (G_OBJECT (label), "width-chars");
2953 gtk_widget_queue_resize (GTK_WIDGET (label));
2958 * gtk_label_get_width_chars:
2959 * @label: a #GtkLabel
2961 * Retrieves the desired width of @label, in characters. See
2962 * gtk_label_set_width_chars().
2964 * Return value: the width of the label in characters.
2969 gtk_label_get_width_chars (GtkLabel *label)
2971 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2973 return label->priv->width_chars;
2977 * gtk_label_set_max_width_chars:
2978 * @label: a #GtkLabel
2979 * @n_chars: the new desired maximum width, in characters.
2981 * Sets the desired maximum width in characters of @label to @n_chars.
2986 gtk_label_set_max_width_chars (GtkLabel *label,
2989 GtkLabelPrivate *priv;
2991 g_return_if_fail (GTK_IS_LABEL (label));
2995 if (priv->max_width_chars != n_chars)
2997 priv->max_width_chars = n_chars;
2999 g_object_notify (G_OBJECT (label), "max-width-chars");
3000 gtk_widget_queue_resize (GTK_WIDGET (label));
3005 * gtk_label_get_max_width_chars:
3006 * @label: a #GtkLabel
3008 * Retrieves the desired maximum width of @label, in characters. See
3009 * gtk_label_set_width_chars().
3011 * Return value: the maximum width of the label in characters.
3016 gtk_label_get_max_width_chars (GtkLabel *label)
3018 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
3020 return label->priv->max_width_chars;
3024 * gtk_label_set_line_wrap:
3025 * @label: a #GtkLabel
3026 * @wrap: the setting
3028 * Toggles line wrapping within the #GtkLabel widget. %TRUE makes it break
3029 * lines if text exceeds the widget's size. %FALSE lets the text get cut off
3030 * by the edge of the widget if it exceeds the widget size.
3032 * Note that setting line wrapping to %TRUE does not make the label
3033 * wrap at its parent container's width, because GTK+ widgets
3034 * conceptually can't make their requisition depend on the parent
3035 * container's size. For a label that wraps at a specific position,
3036 * set the label's width using gtk_widget_set_size_request().
3039 gtk_label_set_line_wrap (GtkLabel *label,
3042 GtkLabelPrivate *priv;
3044 g_return_if_fail (GTK_IS_LABEL (label));
3048 wrap = wrap != FALSE;
3050 if (priv->wrap != wrap)
3054 gtk_label_clear_layout (label);
3055 gtk_widget_queue_resize (GTK_WIDGET (label));
3056 g_object_notify (G_OBJECT (label), "wrap");
3061 * gtk_label_get_line_wrap:
3062 * @label: a #GtkLabel
3064 * Returns whether lines in the label are automatically wrapped.
3065 * See gtk_label_set_line_wrap().
3067 * Return value: %TRUE if the lines of the label are automatically wrapped.
3070 gtk_label_get_line_wrap (GtkLabel *label)
3072 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
3074 return label->priv->wrap;
3078 * gtk_label_set_line_wrap_mode:
3079 * @label: a #GtkLabel
3080 * @wrap_mode: the line wrapping mode
3082 * If line wrapping is on (see gtk_label_set_line_wrap()) this controls how
3083 * the line wrapping is done. The default is %PANGO_WRAP_WORD which means
3084 * wrap on word boundaries.
3089 gtk_label_set_line_wrap_mode (GtkLabel *label,
3090 PangoWrapMode wrap_mode)
3092 GtkLabelPrivate *priv;
3094 g_return_if_fail (GTK_IS_LABEL (label));
3098 if (priv->wrap_mode != wrap_mode)
3100 priv->wrap_mode = wrap_mode;
3101 g_object_notify (G_OBJECT (label), "wrap-mode");
3103 gtk_widget_queue_resize (GTK_WIDGET (label));
3108 * gtk_label_get_line_wrap_mode:
3109 * @label: a #GtkLabel
3111 * Returns line wrap mode used by the label. See gtk_label_set_line_wrap_mode().
3113 * Return value: %TRUE if the lines of the label are automatically wrapped.
3118 gtk_label_get_line_wrap_mode (GtkLabel *label)
3120 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
3122 return label->priv->wrap_mode;
3126 gtk_label_destroy (GtkWidget *widget)
3128 GtkLabel *label = GTK_LABEL (widget);
3130 gtk_label_set_mnemonic_widget (label, NULL);
3132 GTK_WIDGET_CLASS (gtk_label_parent_class)->destroy (widget);
3136 gtk_label_finalize (GObject *object)
3138 GtkLabel *label = GTK_LABEL (object);
3139 GtkLabelPrivate *priv = label->priv;
3141 g_free (priv->label);
3142 g_free (priv->text);
3145 g_object_unref (priv->layout);
3148 pango_attr_list_unref (priv->attrs);
3150 if (priv->effective_attrs)
3151 pango_attr_list_unref (priv->effective_attrs);
3153 gtk_label_clear_links (label);
3154 g_free (priv->select_info);
3156 G_OBJECT_CLASS (gtk_label_parent_class)->finalize (object);
3160 gtk_label_clear_layout (GtkLabel *label)
3162 GtkLabelPrivate *priv = label->priv;
3166 g_object_unref (priv->layout);
3167 priv->layout = NULL;
3169 //gtk_label_clear_links (label);
3173 static PangoFontMetrics *
3174 get_font_metrics (PangoContext *context, GtkWidget *widget)
3176 GtkStyleContext *style_context;
3177 const PangoFontDescription *font;
3178 PangoFontMetrics *retval;
3180 style_context = gtk_widget_get_style_context (widget);
3181 font = gtk_style_context_get_font (style_context, GTK_STATE_FLAG_NORMAL);
3183 retval = pango_context_get_metrics (context,
3185 pango_context_get_language (context));
3191 * gtk_label_get_measuring_layout:
3193 * @existing_layout: %NULL or an existing layout already in use.
3194 * @width: the width to measure with in pango units, or -1 for infinite
3195 * @height: the height to measure with in pango units, or -1 for infinite
3197 * Gets a layout that can be used for measuring sizes. The returned
3198 * layout will be identical to the label's layout except for the
3199 * layout's width, which will be set to @width. Do not modify the returned
3202 * Returns: a new reference to a pango layout
3204 static PangoLayout *
3205 gtk_label_get_measuring_layout (GtkLabel * label,
3206 PangoLayout *existing_layout,
3210 GtkLabelPrivate *priv = label->priv;
3211 PangoRectangle rect;
3214 if (existing_layout != NULL)
3216 if (existing_layout != priv->layout)
3218 pango_layout_set_width (existing_layout, width);
3219 pango_layout_set_height (existing_layout, height);
3220 return existing_layout;
3223 g_object_unref (existing_layout);
3226 gtk_label_ensure_layout (label);
3228 if (pango_layout_get_width (priv->layout) == width &&
3229 pango_layout_get_height (priv->layout) == height)
3231 g_object_ref (priv->layout);
3232 return priv->layout;
3235 /* We can use the label's own layout if we're not allocated a size yet,
3236 * because we don't need it to be properly setup at that point.
3237 * This way we can make use of caching upon the label's creation.
3239 if (gtk_widget_get_allocated_width (GTK_WIDGET (label)) <= 1)
3241 g_object_ref (priv->layout);
3242 pango_layout_set_width (priv->layout, width);
3243 pango_layout_set_height (priv->layout, height);
3244 return priv->layout;
3247 /* oftentimes we want to measure a width that is far wider than the current width,
3248 * even though the layout would not change if we made it wider. In that case, we
3249 * can just return the current layout, because for measuring purposes, it will be
3252 pango_layout_get_extents (priv->layout, NULL, &rect);
3253 if ((width == -1 || rect.width <= width) &&
3254 (height == -1 || rect.height <= height) &&
3255 !pango_layout_is_wrapped (priv->layout) &&
3256 !pango_layout_is_ellipsized (priv->layout))
3258 g_object_ref (priv->layout);
3259 return priv->layout;
3262 copy = pango_layout_copy (priv->layout);
3263 pango_layout_set_width (copy, width);
3264 pango_layout_set_height (copy, height);
3269 gtk_label_update_layout_width (GtkLabel *label)
3271 GtkLabelPrivate *priv = label->priv;
3272 GtkWidget *widget = GTK_WIDGET (label);
3274 g_assert (priv->layout);
3276 if (priv->ellipsize || priv->wrap)
3278 PangoRectangle logical;
3282 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3284 width = gtk_widget_get_allocated_width (GTK_WIDGET (label)) - xpad * 2;
3285 height = gtk_widget_get_allocated_height (GTK_WIDGET (label)) - ypad * 2;
3287 if (priv->have_transform)
3289 PangoContext *context = gtk_widget_get_pango_context (widget);
3290 const PangoMatrix *matrix = pango_context_get_matrix (context);
3291 const gdouble dx = matrix->xx; /* cos (M_PI * angle / 180) */
3292 const gdouble dy = matrix->xy; /* sin (M_PI * angle / 180) */
3294 pango_layout_set_width (priv->layout, -1);
3295 pango_layout_set_height (priv->layout, -1);
3296 pango_layout_get_pixel_extents (priv->layout, NULL, &logical);
3298 if (fabs (dy) < 0.01)
3300 if (logical.width > width)
3301 pango_layout_set_width (priv->layout, width * PANGO_SCALE);
3303 else if (fabs (dx) < 0.01)
3305 if (logical.width > height)
3306 pango_layout_set_width (priv->layout, height * PANGO_SCALE);
3310 gdouble x0, y0, x1, y1, length;
3315 y0 = dx ? x0 * dy / dx : G_MAXDOUBLE;
3316 vertical = fabs (y0) > height / 2;
3321 x0 = dy ? y0 * dx / dy : G_MAXDOUBLE;
3324 length = 2 * sqrt (x0 * x0 + y0 * y0);
3325 pango_layout_set_width (priv->layout, rint (length * PANGO_SCALE));
3326 pango_layout_get_pixel_size (priv->layout, NULL, &cy);
3333 y0 = height/2 + y1 - y0;
3338 x0 = width/2 + x1 - x0;
3342 length = length - sqrt (x0 * x0 + y0 * y0) * 2;
3343 pango_layout_set_width (priv->layout, rint (length * PANGO_SCALE));
3348 pango_layout_set_width (priv->layout, width * PANGO_SCALE);
3349 pango_layout_set_height (priv->layout, priv->ellipsize ? height * PANGO_SCALE : -1);
3354 pango_layout_set_width (priv->layout, -1);
3355 pango_layout_set_height (priv->layout, -1);
3360 gtk_label_ensure_layout (GtkLabel *label)
3362 GtkLabelPrivate *priv = label->priv;
3366 widget = GTK_WIDGET (label);
3368 rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
3372 PangoAlignment align = PANGO_ALIGN_LEFT; /* Quiet gcc */
3373 gdouble angle = gtk_label_get_angle (label);
3375 if (angle != 0.0 && !priv->select_info)
3377 PangoMatrix matrix = PANGO_MATRIX_INIT;
3379 /* We rotate the standard singleton PangoContext for the widget,
3380 * depending on the fact that it's meant pretty much exclusively
3383 pango_matrix_rotate (&matrix, angle);
3385 pango_context_set_matrix (gtk_widget_get_pango_context (widget), &matrix);
3387 priv->have_transform = TRUE;
3391 if (priv->have_transform)
3392 pango_context_set_matrix (gtk_widget_get_pango_context (widget), NULL);
3394 priv->have_transform = FALSE;
3397 priv->layout = gtk_widget_create_pango_layout (widget, priv->text);
3399 if (priv->effective_attrs)
3400 pango_layout_set_attributes (priv->layout, priv->effective_attrs);
3402 gtk_label_rescan_links (label);
3404 switch (priv->jtype)
3406 case GTK_JUSTIFY_LEFT:
3407 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3409 case GTK_JUSTIFY_RIGHT:
3410 align = rtl ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
3412 case GTK_JUSTIFY_CENTER:
3413 align = PANGO_ALIGN_CENTER;
3415 case GTK_JUSTIFY_FILL:
3416 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3417 pango_layout_set_justify (priv->layout, TRUE);
3420 g_assert_not_reached();
3423 pango_layout_set_alignment (priv->layout, align);
3424 pango_layout_set_ellipsize (priv->layout, priv->ellipsize);
3425 pango_layout_set_wrap (priv->layout, priv->wrap_mode);
3426 pango_layout_set_single_paragraph_mode (priv->layout, priv->single_line_mode);
3428 gtk_label_update_layout_width (label);
3433 get_single_line_height (GtkWidget *widget,
3434 PangoLayout *layout)
3436 PangoContext *context;
3437 PangoFontMetrics *metrics;
3438 gint ascent, descent;
3440 context = pango_layout_get_context (layout);
3441 metrics = get_font_metrics (context, widget);
3442 ascent = pango_font_metrics_get_ascent (metrics);
3443 descent = pango_font_metrics_get_descent (metrics);
3444 pango_font_metrics_unref (metrics);
3446 return ascent + descent;
3449 static GtkSizeRequestMode
3450 gtk_label_get_request_mode (GtkWidget *widget)
3452 GtkLabel *label = GTK_LABEL (widget);
3453 gdouble angle = gtk_label_get_angle (label);
3455 if (label->priv->wrap)
3456 return (angle == 90 || angle == 270) ?
3457 GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT :
3458 GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
3460 return GTK_SIZE_REQUEST_CONSTANT_SIZE;
3464 get_size_for_allocation (GtkLabel *label,
3465 GtkOrientation orientation,
3470 GtkLabelPrivate *priv = label->priv;
3471 PangoLayout *layout;
3474 layout = gtk_label_get_measuring_layout (label, NULL, allocation * PANGO_SCALE, -1);
3476 pango_layout_get_pixel_size (layout, NULL, &text_height);
3479 *minimum_size = text_height;
3483 if (priv->ellipsize && priv->wrap)
3485 layout = gtk_label_get_measuring_layout (label, layout, allocation * PANGO_SCALE, G_MAXINT);
3486 pango_layout_get_pixel_size (layout, NULL, &text_height);
3489 *natural_size = text_height;
3492 g_object_unref (layout);
3496 get_char_pixels (GtkWidget *label,
3497 PangoLayout *layout)
3499 PangoContext *context;
3500 PangoFontMetrics *metrics;
3501 gint char_width, digit_width;
3503 context = pango_layout_get_context (layout);
3504 metrics = get_font_metrics (context, GTK_WIDGET (label));
3505 char_width = pango_font_metrics_get_approximate_char_width (metrics);
3506 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
3507 pango_font_metrics_unref (metrics);
3509 return MAX (char_width, digit_width);;
3513 gtk_label_get_preferred_layout_size (GtkLabel *label,
3514 PangoRectangle *required,
3515 PangoRectangle *natural)
3517 GtkLabelPrivate *priv = label->priv;
3518 PangoLayout *layout;
3520 /* "width-chars" Hard-coded minimum width:
3521 * - minimum size should be MAX (width-chars, strlen ("..."));
3522 * - natural size should be MAX (width-chars, strlen (priv->text));
3524 * "max-width-chars" User specified maximum size requisition
3525 * - minimum size should be MAX (width-chars, 0)
3526 * - natural size should be MIN (max-width-chars, strlen (priv->text))
3528 * For ellipsizing labels; if max-width-chars is specified: either it is used as
3529 * a minimum size or the label text as a minimum size (natural size still overflows).
3531 * For wrapping labels; A reasonable minimum size is useful to naturally layout
3532 * interfaces automatically. In this case if no "width-chars" is specified, the minimum
3533 * width will default to the wrap guess that gtk_label_ensure_layout() does.
3536 /* Start off with the pixel extents of an as-wide-as-possible layout */
3537 layout = gtk_label_get_measuring_layout (label, NULL, -1, -1);
3539 pango_layout_get_extents (layout, NULL, natural);
3540 natural->x = natural->y = 0;
3543 natural->height = get_single_line_height (GTK_WIDGET (label), layout);
3545 if (priv->ellipsize || priv->wrap)
3547 /* a layout with width 0 will be as small as humanly possible */
3548 layout = gtk_label_get_measuring_layout (label, layout, 0, -1);
3550 pango_layout_get_extents (layout, NULL, required);
3552 /* can happen when Pango decides to ellipsize text */
3553 if (required->width > natural->width)
3554 required->width = natural->width;
3556 required->x = required->y = 0;
3557 required->height = natural->height;
3561 *required = *natural;
3564 if (priv->width_chars > -1 || priv->max_width_chars > -1)
3568 char_pixels = get_char_pixels (GTK_WIDGET (label), layout);
3570 if (priv->width_chars > -1)
3571 required->width = MAX (required->width, char_pixels * priv->width_chars);
3573 if (priv->max_width_chars > -1)
3574 natural->width = MIN (natural->width, priv->max_width_chars * char_pixels);
3575 natural->width = MAX (natural->width, required->width);
3578 g_object_unref (layout);
3582 gtk_label_get_preferred_size (GtkWidget *widget,
3583 GtkOrientation orientation,
3587 GtkLabel *label = GTK_LABEL (widget);
3588 GtkLabelPrivate *priv = label->priv;
3589 PangoRectangle required_rect;
3590 PangoRectangle natural_rect;
3593 gtk_label_get_preferred_layout_size (label, &required_rect, &natural_rect);
3595 /* Now that we have minimum and natural sizes in pango extents, apply a possible transform */
3596 if (priv->have_transform)
3599 PangoContext *context;
3600 const PangoMatrix *matrix;
3602 copy = pango_layout_copy (priv->layout);
3603 context = pango_layout_get_context (copy);
3604 matrix = pango_context_get_matrix (context);
3606 pango_layout_set_width (copy, -1);
3607 pango_layout_set_ellipsize (copy, PANGO_ELLIPSIZE_NONE);
3609 pango_layout_get_extents (copy, NULL, &natural_rect);
3610 g_object_unref (copy);
3612 pango_matrix_transform_rectangle (matrix, &required_rect);
3613 pango_matrix_transform_rectangle (matrix, &natural_rect);
3615 /* Bump the natural size in case of ellipsize to ensure pango has
3616 * enough space in the angles (note, we could alternatively set the
3617 * layout to not ellipsize when we know we have been allocated our
3618 * full natural size, or it may be that pango needs a fix here).
3620 if (priv->ellipsize && priv->angle != 0 && priv->angle != 90 &&
3621 priv->angle != 180 && priv->angle != 270 && priv->angle != 360)
3623 /* For some reason we only need this at about 110 degrees, and only
3624 * when gaining in height
3626 natural_rect.height += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3627 natural_rect.width += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3631 required_rect.width = PANGO_PIXELS_CEIL (required_rect.width);
3632 required_rect.height = PANGO_PIXELS_CEIL (required_rect.height);
3634 natural_rect.width = PANGO_PIXELS_CEIL (natural_rect.width);
3635 natural_rect.height = PANGO_PIXELS_CEIL (natural_rect.height);
3637 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3639 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3641 /* Note, we cant use get_size_for_allocation() when rotating
3642 * ellipsized labels.
3644 if (!(priv->ellipsize && priv->have_transform) &&
3645 (priv->angle == 90 || priv->angle == 270))
3647 /* Doing a h4w request on a rotated label here, return the
3648 * required width for the minimum height.
3650 get_size_for_allocation (label,
3651 GTK_ORIENTATION_VERTICAL,
3652 required_rect.height,
3653 minimum_size, natural_size);
3658 /* Normal desired width */
3659 *minimum_size = required_rect.width;
3660 *natural_size = natural_rect.width;
3663 *minimum_size += xpad * 2;
3664 *natural_size += xpad * 2;
3666 else /* GTK_ORIENTATION_VERTICAL */
3668 /* Note, we cant use get_size_for_allocation() when rotating
3669 * ellipsized labels.
3671 if (!(priv->ellipsize && priv->have_transform) &&
3672 (priv->angle == 0 || priv->angle == 180 || priv->angle == 360))
3674 /* Doing a w4h request on a label here, return the required
3675 * height for the minimum width.
3677 get_size_for_allocation (label,
3678 GTK_ORIENTATION_HORIZONTAL,
3679 required_rect.width,
3680 minimum_size, natural_size);
3684 /* A vertically rotated label does w4h, so return the base
3685 * desired height (text length)
3687 *minimum_size = required_rect.height;
3688 *natural_size = natural_rect.height;
3691 *minimum_size += ypad * 2;
3692 *natural_size += ypad * 2;
3698 gtk_label_get_preferred_width (GtkWidget *widget,
3702 gtk_label_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
3706 gtk_label_get_preferred_height (GtkWidget *widget,
3710 gtk_label_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
3714 gtk_label_get_preferred_width_for_height (GtkWidget *widget,
3716 gint *minimum_width,
3717 gint *natural_width)
3719 GtkLabel *label = GTK_LABEL (widget);
3720 GtkLabelPrivate *priv = label->priv;
3722 if (priv->wrap && (priv->angle == 90 || priv->angle == 270))
3726 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3729 gtk_label_clear_layout (label);
3731 get_size_for_allocation (label, GTK_ORIENTATION_VERTICAL,
3732 MAX (1, height - (ypad * 2)),
3733 minimum_width, natural_width);
3736 *minimum_width += xpad * 2;
3739 *natural_width += xpad * 2;
3742 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_width, natural_width);
3746 gtk_label_get_preferred_height_for_width (GtkWidget *widget,
3748 gint *minimum_height,
3749 gint *natural_height)
3751 GtkLabel *label = GTK_LABEL (widget);
3752 GtkLabelPrivate *priv = label->priv;
3754 if (priv->wrap && (priv->angle == 0 || priv->angle == 180 || priv->angle == 360))
3758 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3761 gtk_label_clear_layout (label);
3763 get_size_for_allocation (label, GTK_ORIENTATION_HORIZONTAL,
3764 MAX (1, width - xpad * 2),
3765 minimum_height, natural_height);
3768 *minimum_height += ypad * 2;
3771 *natural_height += ypad * 2;
3774 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, minimum_height, natural_height);
3778 gtk_label_size_allocate (GtkWidget *widget,
3779 GtkAllocation *allocation)
3781 GtkLabel *label = GTK_LABEL (widget);
3782 GtkLabelPrivate *priv = label->priv;
3784 GTK_WIDGET_CLASS (gtk_label_parent_class)->size_allocate (widget, allocation);
3787 gtk_label_update_layout_width (label);
3789 if (priv->select_info && priv->select_info->window)
3791 gdk_window_move_resize (priv->select_info->window,
3795 allocation->height);
3800 gtk_label_update_cursor (GtkLabel *label)
3802 GtkLabelPrivate *priv = label->priv;
3805 if (!priv->select_info)
3808 widget = GTK_WIDGET (label);
3810 if (gtk_widget_get_realized (widget))
3812 GdkDisplay *display;
3815 if (gtk_widget_is_sensitive (widget))
3817 display = gtk_widget_get_display (widget);
3819 if (priv->select_info->active_link)
3820 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
3821 else if (priv->select_info->selectable)
3822 cursor = gdk_cursor_new_for_display (display, GDK_XTERM);
3829 gdk_window_set_cursor (priv->select_info->window, cursor);
3832 g_object_unref (cursor);
3837 gtk_label_state_changed (GtkWidget *widget,
3838 GtkStateType prev_state)
3840 GtkLabel *label = GTK_LABEL (widget);
3841 GtkLabelPrivate *priv = label->priv;
3843 if (priv->select_info)
3845 if (!gtk_widget_is_sensitive (widget))
3846 gtk_label_select_region (label, 0, 0);
3848 gtk_label_update_cursor (label);
3851 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed)
3852 GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed (widget, prev_state);
3856 gtk_label_style_updated (GtkWidget *widget)
3858 GtkLabel *label = GTK_LABEL (widget);
3860 GTK_WIDGET_CLASS (gtk_label_parent_class)->style_updated (widget);
3862 /* We have to clear the layout, fonts etc. may have changed */
3863 gtk_label_clear_layout (label);
3867 gtk_label_direction_changed (GtkWidget *widget,
3868 GtkTextDirection previous_dir)
3870 GtkLabel *label = GTK_LABEL (widget);
3871 GtkLabelPrivate *priv = label->priv;
3874 pango_layout_context_changed (priv->layout);
3876 GTK_WIDGET_CLASS (gtk_label_parent_class)->direction_changed (widget, previous_dir);
3880 get_layout_location (GtkLabel *label,
3884 GtkAllocation allocation;
3887 GtkLabelPrivate *priv;
3888 gint req_width, x, y;
3891 gfloat xalign, yalign;
3892 PangoRectangle logical;
3894 misc = GTK_MISC (label);
3895 widget = GTK_WIDGET (label);
3898 gtk_misc_get_alignment (misc, &xalign, &yalign);
3899 gtk_misc_get_padding (misc, &xpad, &ypad);
3901 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
3902 xalign = 1.0 - xalign;
3904 pango_layout_get_extents (priv->layout, NULL, &logical);
3906 if (priv->have_transform)
3908 PangoContext *context = gtk_widget_get_pango_context (widget);
3909 const PangoMatrix *matrix = pango_context_get_matrix (context);
3910 pango_matrix_transform_rectangle (matrix, &logical);
3913 pango_extents_to_pixels (&logical, NULL);
3915 req_width = logical.width;
3916 req_height = logical.height;
3918 req_width += 2 * xpad;
3919 req_height += 2 * ypad;
3921 gtk_widget_get_allocation (widget, &allocation);
3923 x = floor (allocation.x + xpad + xalign * (allocation.width - req_width) - logical.x);
3926 /* bgo#315462 - For single-line labels, *do* align the requisition with
3927 * respect to the allocation, even if we are under-allocated. For multi-line
3928 * labels, always show the top of the text when they are under-allocated. The
3929 * rationale is this:
3931 * - Single-line labels appear in GtkButtons, and it is very easy to get them
3932 * to be smaller than their requisition. The button may clip the label, but
3933 * the label will still be able to show most of itself and the focus
3934 * rectangle. Also, it is fairly easy to read a single line of clipped text.
3936 * - Multi-line labels should not be clipped to showing "something in the
3937 * middle". You want to read the first line, at least, to get some context.
3939 if (pango_layout_get_line_count (priv->layout) == 1)
3940 y = floor (allocation.y + ypad + (allocation.height - req_height) * yalign) - logical.y;
3942 y = floor (allocation.y + ypad + MAX ((allocation.height - req_height) * yalign, 0)) - logical.y;
3952 draw_insertion_cursor (GtkLabel *label,
3954 GdkRectangle *cursor_location,
3955 gboolean is_primary,
3956 PangoDirection direction,
3957 gboolean draw_arrow)
3959 GtkWidget *widget = GTK_WIDGET (label);
3960 GtkTextDirection text_dir;
3962 if (direction == PANGO_DIRECTION_LTR)
3963 text_dir = GTK_TEXT_DIR_LTR;
3965 text_dir = GTK_TEXT_DIR_RTL;
3967 gtk_draw_insertion_cursor (widget, cr, cursor_location,
3968 is_primary, text_dir, draw_arrow);
3971 static PangoDirection
3972 get_cursor_direction (GtkLabel *label)
3974 GtkLabelPrivate *priv = label->priv;
3977 g_assert (priv->select_info);
3979 gtk_label_ensure_layout (label);
3981 for (l = pango_layout_get_lines_readonly (priv->layout); l; l = l->next)
3983 PangoLayoutLine *line = l->data;
3985 /* If priv->select_info->selection_end is at the very end of
3986 * the line, we don't know if the cursor is on this line or
3987 * the next without looking ahead at the next line. (End
3988 * of paragraph is different from line break.) But it's
3989 * definitely in this paragraph, which is good enough
3990 * to figure out the resolved direction.
3992 if (line->start_index + line->length >= priv->select_info->selection_end)
3993 return line->resolved_dir;
3996 return PANGO_DIRECTION_LTR;
4000 gtk_label_draw_cursor (GtkLabel *label, cairo_t *cr, gint xoffset, gint yoffset)
4002 GtkLabelPrivate *priv = label->priv;
4005 if (priv->select_info == NULL)
4008 widget = GTK_WIDGET (label);
4010 if (gtk_widget_is_drawable (widget))
4012 PangoDirection keymap_direction;
4013 PangoDirection cursor_direction;
4014 PangoRectangle strong_pos, weak_pos;
4015 gboolean split_cursor;
4016 PangoRectangle *cursor1 = NULL;
4017 PangoRectangle *cursor2 = NULL;
4018 GdkRectangle cursor_location;
4019 PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
4020 PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
4022 keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gtk_widget_get_display (widget)));
4023 cursor_direction = get_cursor_direction (label);
4025 gtk_label_ensure_layout (label);
4027 pango_layout_get_cursor_pos (priv->layout, priv->select_info->selection_end,
4028 &strong_pos, &weak_pos);
4030 g_object_get (gtk_widget_get_settings (widget),
4031 "gtk-split-cursor", &split_cursor,
4034 dir1 = cursor_direction;
4038 cursor1 = &strong_pos;
4040 if (strong_pos.x != weak_pos.x ||
4041 strong_pos.y != weak_pos.y)
4043 dir2 = (cursor_direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
4044 cursor2 = &weak_pos;
4049 if (keymap_direction == cursor_direction)
4050 cursor1 = &strong_pos;
4052 cursor1 = &weak_pos;
4055 cursor_location.x = xoffset + PANGO_PIXELS (cursor1->x);
4056 cursor_location.y = yoffset + PANGO_PIXELS (cursor1->y);
4057 cursor_location.width = 0;
4058 cursor_location.height = PANGO_PIXELS (cursor1->height);
4060 draw_insertion_cursor (label, cr,
4061 &cursor_location, TRUE, dir1,
4062 dir2 != PANGO_DIRECTION_NEUTRAL);
4064 if (dir2 != PANGO_DIRECTION_NEUTRAL)
4066 cursor_location.x = xoffset + PANGO_PIXELS (cursor2->x);
4067 cursor_location.y = yoffset + PANGO_PIXELS (cursor2->y);
4068 cursor_location.width = 0;
4069 cursor_location.height = PANGO_PIXELS (cursor2->height);
4071 draw_insertion_cursor (label, cr,
4072 &cursor_location, FALSE, dir2,
4078 static GtkLabelLink *
4079 gtk_label_get_focus_link (GtkLabel *label)
4081 GtkLabelPrivate *priv = label->priv;
4082 GtkLabelSelectionInfo *info = priv->select_info;
4088 if (info->selection_anchor != info->selection_end)
4091 for (l = info->links; l; l = l->next)
4093 GtkLabelLink *link = l->data;
4094 if (link->start <= info->selection_anchor &&
4095 info->selection_anchor <= link->end)
4103 gtk_label_draw (GtkWidget *widget,
4106 GtkLabel *label = GTK_LABEL (widget);
4107 GtkLabelPrivate *priv = label->priv;
4108 GtkLabelSelectionInfo *info = priv->select_info;
4109 GtkAllocation allocation;
4110 GtkStyleContext *context;
4111 GtkStateFlags state;
4114 gtk_label_ensure_layout (label);
4116 if (priv->text && (*priv->text != '\0'))
4118 GdkRGBA bg_color, fg_color;
4120 get_layout_location (label, &x, &y);
4122 context = gtk_widget_get_style_context (widget);
4123 gtk_widget_get_allocation (widget, &allocation);
4125 cairo_translate (cr, -allocation.x, -allocation.y);
4127 state = gtk_widget_get_state_flags (widget);
4128 gtk_style_context_set_state (context, state);
4130 gtk_render_layout (context, cr,
4135 (info->selection_anchor != info->selection_end))
4138 cairo_region_t *clip;
4140 range[0] = info->selection_anchor;
4141 range[1] = info->selection_end;
4143 if (range[0] > range[1])
4145 gint tmp = range[0];
4146 range[0] = range[1];
4150 clip = gdk_pango_layout_get_clip_region (priv->layout,
4155 /* FIXME should use gtk_paint, but it can't use a clip region */
4158 gdk_cairo_region (cr, clip);
4161 state = GTK_STATE_FLAG_SELECTED;
4163 if (gtk_widget_has_focus (widget))
4164 state |= GTK_STATE_FLAG_FOCUSED;
4166 gtk_style_context_get_color (context, state, &fg_color);
4167 gtk_style_context_get_background_color (context, state, &bg_color);
4169 gdk_cairo_set_source_rgba (cr, &bg_color);
4172 gdk_cairo_set_source_rgba (cr, &fg_color);
4173 cairo_move_to (cr, x, y);
4174 _gtk_pango_fill_layout (cr, priv->layout);
4177 cairo_region_destroy (clip);
4181 GtkLabelLink *focus_link;
4182 GtkLabelLink *active_link;
4184 cairo_region_t *clip;
4186 GdkColor *text_color;
4187 GdkColor *link_color;
4188 GdkColor *visited_link_color;
4190 if (info->selectable && gtk_widget_has_focus (widget))
4191 gtk_label_draw_cursor (label, cr, x, y);
4193 focus_link = gtk_label_get_focus_link (label);
4194 active_link = info->active_link;
4200 range[0] = active_link->start;
4201 range[1] = active_link->end;
4205 clip = gdk_pango_layout_get_clip_region (priv->layout,
4209 gdk_cairo_region (cr, clip);
4211 cairo_region_destroy (clip);
4213 gtk_label_get_link_colors (widget, &link_color, &visited_link_color);
4214 if (active_link->visited)
4215 text_color = visited_link_color;
4217 text_color = link_color;
4219 if (info->link_clicked)
4220 state = GTK_STATE_FLAG_ACTIVE;
4222 state = GTK_STATE_FLAG_PRELIGHT;
4224 gtk_style_context_get_background_color (context, state, &bg_color);
4226 gdk_cairo_set_source_rgba (cr, &bg_color);
4229 gdk_cairo_set_source_color (cr, text_color);
4230 cairo_move_to (cr, x, y);
4231 _gtk_pango_fill_layout (cr, priv->layout);
4233 gdk_color_free (link_color);
4234 gdk_color_free (visited_link_color);
4239 if (focus_link && gtk_widget_has_visible_focus (widget))
4241 range[0] = focus_link->start;
4242 range[1] = focus_link->end;
4244 clip = gdk_pango_layout_get_clip_region (priv->layout,
4248 cairo_region_get_extents (clip, &rect);
4250 state = gtk_widget_get_state_flags (widget);
4251 gtk_style_context_set_state (context, state);
4253 gtk_render_focus (context, cr,
4255 rect.width, rect.height);
4257 cairo_region_destroy (clip);
4266 separate_uline_pattern (const gchar *str,
4271 gboolean underscore;
4274 gchar *pattern_dest;
4276 *accel_key = GDK_KEY_VoidSymbol;
4277 *new_str = g_new (gchar, strlen (str) + 1);
4278 *pattern = g_new (gchar, g_utf8_strlen (str, -1) + 1);
4284 pattern_dest = *pattern;
4289 const gchar *next_src;
4291 c = g_utf8_get_char (src);
4292 if (c == (gunichar)-1)
4294 g_warning ("Invalid input string");
4300 next_src = g_utf8_next_char (src);
4305 *pattern_dest++ = ' ';
4308 *pattern_dest++ = '_';
4309 if (*accel_key == GDK_KEY_VoidSymbol)
4310 *accel_key = gdk_keyval_to_lower (gdk_unicode_to_keyval (c));
4313 while (src < next_src)
4327 while (src < next_src)
4330 *pattern_dest++ = ' ';
4342 gtk_label_set_uline_text_internal (GtkLabel *label,
4345 GtkLabelPrivate *priv = label->priv;
4346 guint accel_key = GDK_KEY_VoidSymbol;
4350 g_return_if_fail (GTK_IS_LABEL (label));
4351 g_return_if_fail (str != NULL);
4353 /* Split text into the base text and a separate pattern
4356 if (!separate_uline_pattern (str, &accel_key, &new_str, &pattern))
4359 gtk_label_set_text_internal (label, new_str);
4360 gtk_label_set_pattern_internal (label, pattern, TRUE);
4361 priv->mnemonic_keyval = accel_key;
4367 * gtk_label_set_text_with_mnemonic:
4368 * @label: a #GtkLabel
4371 * Sets the label's text from the string @str.
4372 * If characters in @str are preceded by an underscore, they are underlined
4373 * indicating that they represent a keyboard accelerator called a mnemonic.
4374 * The mnemonic key can be used to activate another widget, chosen
4375 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
4378 gtk_label_set_text_with_mnemonic (GtkLabel *label,
4381 g_return_if_fail (GTK_IS_LABEL (label));
4382 g_return_if_fail (str != NULL);
4384 g_object_freeze_notify (G_OBJECT (label));
4386 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
4387 gtk_label_set_use_markup_internal (label, FALSE);
4388 gtk_label_set_use_underline_internal (label, TRUE);
4390 gtk_label_recalculate (label);
4392 g_object_thaw_notify (G_OBJECT (label));
4396 gtk_label_realize (GtkWidget *widget)
4398 GtkLabel *label = GTK_LABEL (widget);
4399 GtkLabelPrivate *priv = label->priv;
4401 GTK_WIDGET_CLASS (gtk_label_parent_class)->realize (widget);
4403 if (priv->select_info)
4404 gtk_label_create_window (label);
4408 gtk_label_unrealize (GtkWidget *widget)
4410 GtkLabel *label = GTK_LABEL (widget);
4411 GtkLabelPrivate *priv = label->priv;
4413 if (priv->select_info)
4414 gtk_label_destroy_window (label);
4416 GTK_WIDGET_CLASS (gtk_label_parent_class)->unrealize (widget);
4420 gtk_label_map (GtkWidget *widget)
4422 GtkLabel *label = GTK_LABEL (widget);
4423 GtkLabelPrivate *priv = label->priv;
4425 GTK_WIDGET_CLASS (gtk_label_parent_class)->map (widget);
4427 if (priv->select_info)
4428 gdk_window_show (priv->select_info->window);
4432 gtk_label_unmap (GtkWidget *widget)
4434 GtkLabel *label = GTK_LABEL (widget);
4435 GtkLabelPrivate *priv = label->priv;
4437 if (priv->select_info)
4438 gdk_window_hide (priv->select_info->window);
4440 GTK_WIDGET_CLASS (gtk_label_parent_class)->unmap (widget);
4444 window_to_layout_coords (GtkLabel *label,
4448 GtkAllocation allocation;
4452 widget = GTK_WIDGET (label);
4454 /* get layout location in widget->window coords */
4455 get_layout_location (label, &lx, &ly);
4457 gtk_widget_get_allocation (widget, &allocation);
4461 *x += allocation.x; /* go to widget->window */
4462 *x -= lx; /* go to layout */
4467 *y += allocation.y; /* go to widget->window */
4468 *y -= ly; /* go to layout */
4474 layout_to_window_coords (GtkLabel *label,
4481 widget = GTK_WIDGET (label);
4483 /* get layout location in widget->window coords */
4484 get_layout_location (label, &lx, &ly);
4488 *x += lx; /* go to widget->window */
4489 *x -= widget->allocation.x; /* go to selection window */
4494 *y += ly; /* go to widget->window */
4495 *y -= widget->allocation.y; /* go to selection window */
4501 get_layout_index (GtkLabel *label,
4506 GtkLabelPrivate *priv = label->priv;
4508 const gchar *cluster;
4509 const gchar *cluster_end;
4514 gtk_label_ensure_layout (label);
4516 window_to_layout_coords (label, &x, &y);
4521 inside = pango_layout_xy_to_index (priv->layout,
4525 cluster = priv->text + *index;
4526 cluster_end = cluster;
4529 cluster_end = g_utf8_next_char (cluster_end);
4533 *index += (cluster_end - cluster);
4539 gtk_label_select_word (GtkLabel *label)
4541 GtkLabelPrivate *priv = label->priv;
4544 gint start_index = gtk_label_move_backward_word (label, priv->select_info->selection_end);
4545 gint end_index = gtk_label_move_forward_word (label, priv->select_info->selection_end);
4547 min = MIN (priv->select_info->selection_anchor,
4548 priv->select_info->selection_end);
4549 max = MAX (priv->select_info->selection_anchor,
4550 priv->select_info->selection_end);
4552 min = MIN (min, start_index);
4553 max = MAX (max, end_index);
4555 gtk_label_select_region_index (label, min, max);
4559 gtk_label_grab_focus (GtkWidget *widget)
4561 GtkLabel *label = GTK_LABEL (widget);
4562 GtkLabelPrivate *priv = label->priv;
4563 gboolean select_on_focus;
4566 if (priv->select_info == NULL)
4569 GTK_WIDGET_CLASS (gtk_label_parent_class)->grab_focus (widget);
4571 if (priv->select_info->selectable)
4573 g_object_get (gtk_widget_get_settings (widget),
4574 "gtk-label-select-on-focus",
4578 if (select_on_focus && !priv->in_click)
4579 gtk_label_select_region (label, 0, -1);
4583 if (priv->select_info->links && !priv->in_click)
4585 link = priv->select_info->links->data;
4586 priv->select_info->selection_anchor = link->start;
4587 priv->select_info->selection_end = link->start;
4593 gtk_label_focus (GtkWidget *widget,
4594 GtkDirectionType direction)
4596 GtkLabel *label = GTK_LABEL (widget);
4597 GtkLabelPrivate *priv = label->priv;
4598 GtkLabelSelectionInfo *info = priv->select_info;
4599 GtkLabelLink *focus_link;
4602 if (!gtk_widget_is_focus (widget))
4604 gtk_widget_grab_focus (widget);
4607 focus_link = gtk_label_get_focus_link (label);
4608 if (focus_link && direction == GTK_DIR_TAB_BACKWARD)
4610 l = g_list_last (info->links);
4611 focus_link = l->data;
4612 info->selection_anchor = focus_link->start;
4613 info->selection_end = focus_link->start;
4623 if (info->selectable)
4627 if (info->selection_anchor != info->selection_end)
4630 index = info->selection_anchor;
4632 if (direction == GTK_DIR_TAB_FORWARD)
4633 for (l = info->links; l; l = l->next)
4635 GtkLabelLink *link = l->data;
4637 if (link->start > index)
4639 gtk_label_select_region_index (label, link->start, link->start);
4643 else if (direction == GTK_DIR_TAB_BACKWARD)
4644 for (l = g_list_last (info->links); l; l = l->prev)
4646 GtkLabelLink *link = l->data;
4648 if (link->end < index)
4650 gtk_label_select_region_index (label, link->start, link->start);
4659 focus_link = gtk_label_get_focus_link (label);
4662 case GTK_DIR_TAB_FORWARD:
4665 l = g_list_find (info->links, focus_link);
4672 case GTK_DIR_TAB_BACKWARD:
4675 l = g_list_find (info->links, focus_link);
4679 l = g_list_last (info->links);
4688 focus_link = l->data;
4689 info->selection_anchor = focus_link->start;
4690 info->selection_end = focus_link->start;
4691 gtk_widget_queue_draw (widget);
4703 gtk_label_button_press (GtkWidget *widget,
4704 GdkEventButton *event)
4706 GtkLabel *label = GTK_LABEL (widget);
4707 GtkLabelPrivate *priv = label->priv;
4708 GtkLabelSelectionInfo *info = priv->select_info;
4715 if (info->active_link)
4717 if (gdk_event_triggers_context_menu ((GdkEvent *) event))
4719 info->link_clicked = 1;
4720 gtk_label_do_popup (label, event);
4723 else if (event->button == 1)
4725 info->link_clicked = 1;
4726 gtk_widget_queue_draw (widget);
4730 if (!info->selectable)
4733 info->in_drag = FALSE;
4734 info->select_words = FALSE;
4736 if (gdk_event_triggers_context_menu ((GdkEvent *) event))
4738 gtk_label_do_popup (label, event);
4742 else if (event->button == 1)
4744 if (!gtk_widget_has_focus (widget))
4746 priv->in_click = TRUE;
4747 gtk_widget_grab_focus (widget);
4748 priv->in_click = FALSE;
4751 if (event->type == GDK_3BUTTON_PRESS)
4753 gtk_label_select_region_index (label, 0, strlen (priv->text));
4757 if (event->type == GDK_2BUTTON_PRESS)
4759 info->select_words = TRUE;
4760 gtk_label_select_word (label);
4764 get_layout_index (label, event->x, event->y, &index);
4766 min = MIN (info->selection_anchor, info->selection_end);
4767 max = MAX (info->selection_anchor, info->selection_end);
4769 if ((info->selection_anchor != info->selection_end) &&
4770 (event->state & GDK_SHIFT_MASK))
4772 if (index > min && index < max)
4774 /* truncate selection, but keep it as big as possible */
4775 if (index - min > max - index)
4782 /* extend (same as motion) */
4783 min = MIN (min, index);
4784 max = MAX (max, index);
4787 /* ensure the anchor is opposite index */
4795 gtk_label_select_region_index (label, min, max);
4799 if (event->type == GDK_3BUTTON_PRESS)
4800 gtk_label_select_region_index (label, 0, strlen (priv->text));
4801 else if (event->type == GDK_2BUTTON_PRESS)
4802 gtk_label_select_word (label);
4803 else if (min < max && min <= index && index <= max)
4805 info->in_drag = TRUE;
4806 info->drag_start_x = event->x;
4807 info->drag_start_y = event->y;
4810 /* start a replacement */
4811 gtk_label_select_region_index (label, index, index);
4821 gtk_label_button_release (GtkWidget *widget,
4822 GdkEventButton *event)
4825 GtkLabel *label = GTK_LABEL (widget);
4826 GtkLabelPrivate *priv = label->priv;
4827 GtkLabelSelectionInfo *info = priv->select_info;
4837 get_layout_index (label, event->x, event->y, &index);
4838 gtk_label_select_region_index (label, index, index);
4843 if (event->button != 1)
4846 if (info->active_link &&
4847 info->selection_anchor == info->selection_end &&
4850 emit_activate_link (label, info->active_link);
4851 info->link_clicked = 0;
4856 /* The goal here is to return TRUE iff we ate the
4857 * button press to start selecting.
4863 connect_mnemonics_visible_notify (GtkLabel *label)
4865 GtkLabelPrivate *priv = label->priv;
4866 GtkWidget *toplevel;
4869 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
4871 if (!GTK_IS_WINDOW (toplevel))
4874 /* always set up this widgets initial value */
4875 priv->mnemonics_visible =
4876 gtk_window_get_mnemonics_visible (GTK_WINDOW (toplevel));
4879 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (toplevel),
4880 "gtk-label-mnemonics-visible-connected"));
4884 g_signal_connect (toplevel,
4885 "notify::mnemonics-visible",
4886 G_CALLBACK (label_mnemonics_visible_changed),
4888 g_object_set_data (G_OBJECT (toplevel),
4889 "gtk-label-mnemonics-visible-connected",
4890 GINT_TO_POINTER (1));
4895 drag_begin_cb (GtkWidget *widget,
4896 GdkDragContext *context,
4899 GtkLabel *label = GTK_LABEL (widget);
4900 GtkLabelPrivate *priv = label->priv;
4901 cairo_surface_t *surface = NULL;
4903 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
4905 if ((priv->select_info->selection_anchor !=
4906 priv->select_info->selection_end) &&
4912 start = MIN (priv->select_info->selection_anchor,
4913 priv->select_info->selection_end);
4914 end = MAX (priv->select_info->selection_anchor,
4915 priv->select_info->selection_end);
4917 len = strlen (priv->text);
4925 surface = _gtk_text_util_create_drag_icon (widget,
4932 gtk_drag_set_icon_surface (context, surface);
4933 cairo_surface_destroy (surface);
4937 gtk_drag_set_icon_default (context);
4942 gtk_label_motion (GtkWidget *widget,
4943 GdkEventMotion *event)
4945 GtkLabel *label = GTK_LABEL (widget);
4946 GtkLabelPrivate *priv = label->priv;
4947 GtkLabelSelectionInfo *info = priv->select_info;
4953 if (info->links && !info->in_drag)
4957 gboolean found = FALSE;
4959 if (info->selection_anchor == info->selection_end)
4961 if (get_layout_index (label, event->x, event->y, &index))
4963 for (l = info->links; l != NULL; l = l->next)
4966 if (index >= link->start && index <= link->end)
4977 if (info->active_link != link)
4979 info->link_clicked = 0;
4980 info->active_link = link;
4981 gtk_label_update_cursor (label);
4982 gtk_widget_queue_draw (widget);
4987 if (info->active_link != NULL)
4989 info->link_clicked = 0;
4990 info->active_link = NULL;
4991 gtk_label_update_cursor (label);
4992 gtk_widget_queue_draw (widget);
4997 if (!info->selectable)
5000 if ((event->state & GDK_BUTTON1_MASK) == 0)
5005 if (gtk_drag_check_threshold (widget,
5008 event->x, event->y))
5010 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
5012 gtk_target_list_add_text_targets (target_list, 0);
5014 g_signal_connect (widget, "drag-begin",
5015 G_CALLBACK (drag_begin_cb), NULL);
5016 gtk_drag_begin (widget, target_list,
5018 1, (GdkEvent *)event);
5020 info->in_drag = FALSE;
5022 gtk_target_list_unref (target_list);
5029 gdk_window_get_device_position (info->window, event->device, &x, &y, NULL);
5030 get_layout_index (label, x, y, &index);
5032 if (info->select_words)
5035 gint old_min, old_max;
5038 min = gtk_label_move_backward_word (label, index);
5039 max = gtk_label_move_forward_word (label, index);
5041 anchor = info->selection_anchor;
5042 end = info->selection_end;
5044 old_min = MIN (anchor, end);
5045 old_max = MAX (anchor, end);
5052 else if (old_max < max)
5057 else if (anchor == old_min)
5068 gtk_label_select_region_index (label, anchor, end);
5071 gtk_label_select_region_index (label, info->selection_anchor, index);
5078 gtk_label_leave_notify (GtkWidget *widget,
5079 GdkEventCrossing *event)
5081 GtkLabel *label = GTK_LABEL (widget);
5082 GtkLabelPrivate *priv = label->priv;
5084 if (priv->select_info)
5086 priv->select_info->active_link = NULL;
5087 gtk_label_update_cursor (label);
5088 gtk_widget_queue_draw (widget);
5091 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event)
5092 return GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event (widget, event);
5098 gtk_label_create_window (GtkLabel *label)
5100 GtkLabelPrivate *priv = label->priv;
5101 GtkAllocation allocation;
5103 GdkWindowAttr attributes;
5104 gint attributes_mask;
5106 g_assert (priv->select_info);
5107 widget = GTK_WIDGET (label);
5108 g_assert (gtk_widget_get_realized (widget));
5110 if (priv->select_info->window)
5113 gtk_widget_get_allocation (widget, &allocation);
5115 attributes.x = allocation.x;
5116 attributes.y = allocation.y;
5117 attributes.width = allocation.width;
5118 attributes.height = allocation.height;
5119 attributes.window_type = GDK_WINDOW_CHILD;
5120 attributes.wclass = GDK_INPUT_ONLY;
5121 attributes.override_redirect = TRUE;
5122 attributes.event_mask = gtk_widget_get_events (widget) |
5123 GDK_BUTTON_PRESS_MASK |
5124 GDK_BUTTON_RELEASE_MASK |
5125 GDK_LEAVE_NOTIFY_MASK |
5126 GDK_BUTTON_MOTION_MASK |
5127 GDK_POINTER_MOTION_MASK |
5128 GDK_POINTER_MOTION_HINT_MASK;
5129 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
5130 if (gtk_widget_is_sensitive (widget))
5132 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
5134 attributes_mask |= GDK_WA_CURSOR;
5138 priv->select_info->window = gdk_window_new (gtk_widget_get_window (widget),
5139 &attributes, attributes_mask);
5140 gdk_window_set_user_data (priv->select_info->window, widget);
5142 if (attributes_mask & GDK_WA_CURSOR)
5143 g_object_unref (attributes.cursor);
5147 gtk_label_destroy_window (GtkLabel *label)
5149 GtkLabelPrivate *priv = label->priv;
5151 g_assert (priv->select_info);
5153 if (priv->select_info->window == NULL)
5156 gdk_window_set_user_data (priv->select_info->window, NULL);
5157 gdk_window_destroy (priv->select_info->window);
5158 priv->select_info->window = NULL;
5162 gtk_label_ensure_select_info (GtkLabel *label)
5164 GtkLabelPrivate *priv = label->priv;
5166 if (priv->select_info == NULL)
5168 priv->select_info = g_new0 (GtkLabelSelectionInfo, 1);
5170 gtk_widget_set_can_focus (GTK_WIDGET (label), TRUE);
5172 if (gtk_widget_get_realized (GTK_WIDGET (label)))
5173 gtk_label_create_window (label);
5175 if (gtk_widget_get_mapped (GTK_WIDGET (label)))
5176 gdk_window_show (priv->select_info->window);
5181 gtk_label_clear_select_info (GtkLabel *label)
5183 GtkLabelPrivate *priv = label->priv;
5185 if (priv->select_info == NULL)
5188 if (!priv->select_info->selectable && !priv->select_info->links)
5190 gtk_label_destroy_window (label);
5192 g_free (priv->select_info);
5193 priv->select_info = NULL;
5195 gtk_widget_set_can_focus (GTK_WIDGET (label), FALSE);
5200 * gtk_label_set_selectable:
5201 * @label: a #GtkLabel
5202 * @setting: %TRUE to allow selecting text in the label
5204 * Selectable labels allow the user to select text from the label, for
5208 gtk_label_set_selectable (GtkLabel *label,
5211 GtkLabelPrivate *priv;
5212 gboolean old_setting;
5214 g_return_if_fail (GTK_IS_LABEL (label));
5218 setting = setting != FALSE;
5219 old_setting = priv->select_info && priv->select_info->selectable;
5223 gtk_label_ensure_select_info (label);
5224 priv->select_info->selectable = TRUE;
5225 gtk_label_update_cursor (label);
5231 /* unselect, to give up the selection */
5232 gtk_label_select_region (label, 0, 0);
5234 priv->select_info->selectable = FALSE;
5235 gtk_label_clear_select_info (label);
5236 gtk_label_update_cursor (label);
5239 if (setting != old_setting)
5241 g_object_freeze_notify (G_OBJECT (label));
5242 g_object_notify (G_OBJECT (label), "selectable");
5243 g_object_notify (G_OBJECT (label), "cursor-position");
5244 g_object_notify (G_OBJECT (label), "selection-bound");
5245 g_object_thaw_notify (G_OBJECT (label));
5246 gtk_widget_queue_draw (GTK_WIDGET (label));
5251 * gtk_label_get_selectable:
5252 * @label: a #GtkLabel
5254 * Gets the value set by gtk_label_set_selectable().
5256 * Return value: %TRUE if the user can copy text from the label
5259 gtk_label_get_selectable (GtkLabel *label)
5261 GtkLabelPrivate *priv;
5263 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5267 return priv->select_info && priv->select_info->selectable;
5271 * gtk_label_set_angle:
5272 * @label: a #GtkLabel
5273 * @angle: the angle that the baseline of the label makes with
5274 * the horizontal, in degrees, measured counterclockwise
5276 * Sets the angle of rotation for the label. An angle of 90 reads from
5277 * from bottom to top, an angle of 270, from top to bottom. The angle
5278 * setting for the label is ignored if the label is selectable,
5279 * wrapped, or ellipsized.
5284 gtk_label_set_angle (GtkLabel *label,
5287 GtkLabelPrivate *priv;
5289 g_return_if_fail (GTK_IS_LABEL (label));
5293 /* Canonicalize to [0,360]. We don't canonicalize 360 to 0, because
5294 * double property ranges are inclusive, and changing 360 to 0 would
5295 * make a property editor behave strangely.
5297 if (angle < 0 || angle > 360.0)
5298 angle = angle - 360. * floor (angle / 360.);
5300 if (priv->angle != angle)
5302 priv->angle = angle;
5304 gtk_label_clear_layout (label);
5305 gtk_widget_queue_resize (GTK_WIDGET (label));
5307 g_object_notify (G_OBJECT (label), "angle");
5312 * gtk_label_get_angle:
5313 * @label: a #GtkLabel
5315 * Gets the angle of rotation for the label. See
5316 * gtk_label_set_angle().
5318 * Return value: the angle of rotation for the label
5323 gtk_label_get_angle (GtkLabel *label)
5325 g_return_val_if_fail (GTK_IS_LABEL (label), 0.0);
5327 return label->priv->angle;
5331 gtk_label_set_selection_text (GtkLabel *label,
5332 GtkSelectionData *selection_data)
5334 GtkLabelPrivate *priv = label->priv;
5336 if (priv->select_info &&
5337 (priv->select_info->selection_anchor !=
5338 priv->select_info->selection_end) &&
5344 start = MIN (priv->select_info->selection_anchor,
5345 priv->select_info->selection_end);
5346 end = MAX (priv->select_info->selection_anchor,
5347 priv->select_info->selection_end);
5349 len = strlen (priv->text);
5357 gtk_selection_data_set_text (selection_data,
5364 gtk_label_drag_data_get (GtkWidget *widget,
5365 GdkDragContext *context,
5366 GtkSelectionData *selection_data,
5370 gtk_label_set_selection_text (GTK_LABEL (widget), selection_data);
5374 get_text_callback (GtkClipboard *clipboard,
5375 GtkSelectionData *selection_data,
5377 gpointer user_data_or_owner)
5379 gtk_label_set_selection_text (GTK_LABEL (user_data_or_owner), selection_data);
5383 clear_text_callback (GtkClipboard *clipboard,
5384 gpointer user_data_or_owner)
5387 GtkLabelPrivate *priv;
5389 label = GTK_LABEL (user_data_or_owner);
5392 if (priv->select_info)
5394 priv->select_info->selection_anchor = priv->select_info->selection_end;
5396 gtk_widget_queue_draw (GTK_WIDGET (label));
5401 gtk_label_select_region_index (GtkLabel *label,
5405 GtkLabelPrivate *priv;
5407 g_return_if_fail (GTK_IS_LABEL (label));
5411 if (priv->select_info && priv->select_info->selectable)
5413 GtkClipboard *clipboard;
5415 if (priv->select_info->selection_anchor == anchor_index &&
5416 priv->select_info->selection_end == end_index)
5419 g_object_freeze_notify (G_OBJECT (label));
5421 if (priv->select_info->selection_anchor != anchor_index)
5422 g_object_notify (G_OBJECT (label), "selection-bound");
5423 if (priv->select_info->selection_end != end_index)
5424 g_object_notify (G_OBJECT (label), "cursor-position");
5426 priv->select_info->selection_anchor = anchor_index;
5427 priv->select_info->selection_end = end_index;
5429 if (gtk_widget_has_screen (GTK_WIDGET (label)))
5430 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label),
5431 GDK_SELECTION_PRIMARY);
5435 if (anchor_index != end_index)
5437 GtkTargetList *list;
5438 GtkTargetEntry *targets;
5441 list = gtk_target_list_new (NULL, 0);
5442 gtk_target_list_add_text_targets (list, 0);
5443 targets = gtk_target_table_new_from_list (list, &n_targets);
5446 gtk_clipboard_set_with_owner (clipboard,
5449 clear_text_callback,
5452 gtk_target_table_free (targets, n_targets);
5453 gtk_target_list_unref (list);
5458 gtk_clipboard_get_owner (clipboard) == G_OBJECT (label))
5459 gtk_clipboard_clear (clipboard);
5462 gtk_widget_queue_draw (GTK_WIDGET (label));
5464 g_object_thaw_notify (G_OBJECT (label));
5469 * gtk_label_select_region:
5470 * @label: a #GtkLabel
5471 * @start_offset: start offset (in characters not bytes)
5472 * @end_offset: end offset (in characters not bytes)
5474 * Selects a range of characters in the label, if the label is selectable.
5475 * See gtk_label_set_selectable(). If the label is not selectable,
5476 * this function has no effect. If @start_offset or
5477 * @end_offset are -1, then the end of the label will be substituted.
5480 gtk_label_select_region (GtkLabel *label,
5484 GtkLabelPrivate *priv;
5486 g_return_if_fail (GTK_IS_LABEL (label));
5490 if (priv->text && priv->select_info)
5492 if (start_offset < 0)
5493 start_offset = g_utf8_strlen (priv->text, -1);
5496 end_offset = g_utf8_strlen (priv->text, -1);
5498 gtk_label_select_region_index (label,
5499 g_utf8_offset_to_pointer (priv->text, start_offset) - priv->text,
5500 g_utf8_offset_to_pointer (priv->text, end_offset) - priv->text);
5505 * gtk_label_get_selection_bounds:
5506 * @label: a #GtkLabel
5507 * @start: (out): return location for start of selection, as a character offset
5508 * @end: (out): return location for end of selection, as a character offset
5510 * Gets the selected range of characters in the label, returning %TRUE
5511 * if there's a selection.
5513 * Return value: %TRUE if selection is non-empty
5516 gtk_label_get_selection_bounds (GtkLabel *label,
5520 GtkLabelPrivate *priv;
5522 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5526 if (priv->select_info == NULL)
5528 /* not a selectable label */
5538 gint start_index, end_index;
5539 gint start_offset, end_offset;
5542 start_index = MIN (priv->select_info->selection_anchor,
5543 priv->select_info->selection_end);
5544 end_index = MAX (priv->select_info->selection_anchor,
5545 priv->select_info->selection_end);
5547 len = strlen (priv->text);
5549 if (end_index > len)
5552 if (start_index > len)
5555 start_offset = g_utf8_strlen (priv->text, start_index);
5556 end_offset = g_utf8_strlen (priv->text, end_index);
5558 if (start_offset > end_offset)
5560 gint tmp = start_offset;
5561 start_offset = end_offset;
5566 *start = start_offset;
5571 return start_offset != end_offset;
5577 * gtk_label_get_layout:
5578 * @label: a #GtkLabel
5580 * Gets the #PangoLayout used to display the label.
5581 * The layout is useful to e.g. convert text positions to
5582 * pixel positions, in combination with gtk_label_get_layout_offsets().
5583 * The returned layout is owned by the @label so need not be
5584 * freed by the caller. The @label is free to recreate its layout at
5585 * any time, so it should be considered read-only.
5587 * Return value: (transfer none): the #PangoLayout for this label
5590 gtk_label_get_layout (GtkLabel *label)
5592 GtkLabelPrivate *priv;
5594 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
5598 gtk_label_ensure_layout (label);
5600 return priv->layout;
5604 * gtk_label_get_layout_offsets:
5605 * @label: a #GtkLabel
5606 * @x: (out) (allow-none): location to store X offset of layout, or %NULL
5607 * @y: (out) (allow-none): location to store Y offset of layout, or %NULL
5609 * Obtains the coordinates where the label will draw the #PangoLayout
5610 * representing the text in the label; useful to convert mouse events
5611 * into coordinates inside the #PangoLayout, e.g. to take some action
5612 * if some part of the label is clicked. Of course you will need to
5613 * create a #GtkEventBox to receive the events, and pack the label
5614 * inside it, since labels are a #GTK_NO_WINDOW widget. Remember
5615 * when using the #PangoLayout functions you need to convert to
5616 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
5619 gtk_label_get_layout_offsets (GtkLabel *label,
5623 g_return_if_fail (GTK_IS_LABEL (label));
5625 gtk_label_ensure_layout (label);
5627 get_layout_location (label, x, y);
5631 * gtk_label_set_use_markup:
5632 * @label: a #GtkLabel
5633 * @setting: %TRUE if the label's text should be parsed for markup.
5635 * Sets whether the text of the label contains markup in <link
5636 * linkend="PangoMarkupFormat">Pango's text markup
5637 * language</link>. See gtk_label_set_markup().
5640 gtk_label_set_use_markup (GtkLabel *label,
5643 g_return_if_fail (GTK_IS_LABEL (label));
5645 g_object_freeze_notify (G_OBJECT (label));
5647 gtk_label_set_use_markup_internal (label, setting);
5648 gtk_label_recalculate (label);
5650 g_object_thaw_notify (G_OBJECT (label));
5654 * gtk_label_get_use_markup:
5655 * @label: a #GtkLabel
5657 * Returns whether the label's text is interpreted as marked up with
5658 * the <link linkend="PangoMarkupFormat">Pango text markup
5659 * language</link>. See gtk_label_set_use_markup ().
5661 * Return value: %TRUE if the label's text will be parsed for markup.
5664 gtk_label_get_use_markup (GtkLabel *label)
5666 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5668 return label->priv->use_markup;
5672 * gtk_label_set_use_underline:
5673 * @label: a #GtkLabel
5674 * @setting: %TRUE if underlines in the text indicate mnemonics
5676 * If true, an underline in the text indicates the next character should be
5677 * used for the mnemonic accelerator key.
5680 gtk_label_set_use_underline (GtkLabel *label,
5683 g_return_if_fail (GTK_IS_LABEL (label));
5685 g_object_freeze_notify (G_OBJECT (label));
5687 gtk_label_set_use_underline_internal (label, setting);
5688 gtk_label_recalculate (label);
5690 g_object_thaw_notify (G_OBJECT (label));
5694 * gtk_label_get_use_underline:
5695 * @label: a #GtkLabel
5697 * Returns whether an embedded underline in the label indicates a
5698 * mnemonic. See gtk_label_set_use_underline().
5700 * Return value: %TRUE whether an embedded underline in the label indicates
5701 * the mnemonic accelerator keys.
5704 gtk_label_get_use_underline (GtkLabel *label)
5706 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5708 return label->priv->use_underline;
5712 * gtk_label_set_single_line_mode:
5713 * @label: a #GtkLabel
5714 * @single_line_mode: %TRUE if the label should be in single line mode
5716 * Sets whether the label is in single line mode.
5721 gtk_label_set_single_line_mode (GtkLabel *label,
5722 gboolean single_line_mode)
5724 GtkLabelPrivate *priv;
5726 g_return_if_fail (GTK_IS_LABEL (label));
5730 single_line_mode = single_line_mode != FALSE;
5732 if (priv->single_line_mode != single_line_mode)
5734 priv->single_line_mode = single_line_mode;
5736 gtk_label_clear_layout (label);
5737 gtk_widget_queue_resize (GTK_WIDGET (label));
5739 g_object_notify (G_OBJECT (label), "single-line-mode");
5744 * gtk_label_get_single_line_mode:
5745 * @label: a #GtkLabel
5747 * Returns whether the label is in single line mode.
5749 * Return value: %TRUE when the label is in single line mode.
5754 gtk_label_get_single_line_mode (GtkLabel *label)
5756 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5758 return label->priv->single_line_mode;
5761 /* Compute the X position for an offset that corresponds to the "more important
5762 * cursor position for that offset. We use this when trying to guess to which
5763 * end of the selection we should go to when the user hits the left or
5767 get_better_cursor (GtkLabel *label,
5772 GtkLabelPrivate *priv = label->priv;
5773 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5774 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5775 PangoDirection cursor_direction = get_cursor_direction (label);
5776 gboolean split_cursor;
5777 PangoRectangle strong_pos, weak_pos;
5779 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5780 "gtk-split-cursor", &split_cursor,
5783 gtk_label_ensure_layout (label);
5785 pango_layout_get_cursor_pos (priv->layout, index,
5786 &strong_pos, &weak_pos);
5790 *x = strong_pos.x / PANGO_SCALE;
5791 *y = strong_pos.y / PANGO_SCALE;
5795 if (keymap_direction == cursor_direction)
5797 *x = strong_pos.x / PANGO_SCALE;
5798 *y = strong_pos.y / PANGO_SCALE;
5802 *x = weak_pos.x / PANGO_SCALE;
5803 *y = weak_pos.y / PANGO_SCALE;
5810 gtk_label_move_logically (GtkLabel *label,
5814 GtkLabelPrivate *priv = label->priv;
5815 gint offset = g_utf8_pointer_to_offset (priv->text,
5816 priv->text + start);
5820 PangoLogAttr *log_attrs;
5824 gtk_label_ensure_layout (label);
5826 length = g_utf8_strlen (priv->text, -1);
5828 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5830 while (count > 0 && offset < length)
5834 while (offset < length && !log_attrs[offset].is_cursor_position);
5838 while (count < 0 && offset > 0)
5842 while (offset > 0 && !log_attrs[offset].is_cursor_position);
5850 return g_utf8_offset_to_pointer (priv->text, offset) - priv->text;
5854 gtk_label_move_visually (GtkLabel *label,
5858 GtkLabelPrivate *priv = label->priv;
5865 int new_index, new_trailing;
5866 gboolean split_cursor;
5869 gtk_label_ensure_layout (label);
5871 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5872 "gtk-split-cursor", &split_cursor,
5879 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5880 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5882 strong = keymap_direction == get_cursor_direction (label);
5887 pango_layout_move_cursor_visually (priv->layout, strong, index, 0, 1, &new_index, &new_trailing);
5892 pango_layout_move_cursor_visually (priv->layout, strong, index, 0, -1, &new_index, &new_trailing);
5896 if (new_index < 0 || new_index == G_MAXINT)
5901 while (new_trailing--)
5902 index = g_utf8_next_char (priv->text + new_index) - priv->text;
5909 gtk_label_move_forward_word (GtkLabel *label,
5912 GtkLabelPrivate *priv = label->priv;
5913 gint new_pos = g_utf8_pointer_to_offset (priv->text,
5914 priv->text + start);
5917 length = g_utf8_strlen (priv->text, -1);
5918 if (new_pos < length)
5920 PangoLogAttr *log_attrs;
5923 gtk_label_ensure_layout (label);
5925 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5927 /* Find the next word end */
5929 while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
5935 return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
5940 gtk_label_move_backward_word (GtkLabel *label,
5943 GtkLabelPrivate *priv = label->priv;
5944 gint new_pos = g_utf8_pointer_to_offset (priv->text,
5945 priv->text + start);
5949 PangoLogAttr *log_attrs;
5952 gtk_label_ensure_layout (label);
5954 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5958 /* Find the previous word beginning */
5959 while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
5965 return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
5969 gtk_label_move_cursor (GtkLabel *label,
5970 GtkMovementStep step,
5972 gboolean extend_selection)
5974 GtkLabelPrivate *priv = label->priv;
5978 if (priv->select_info == NULL)
5981 old_pos = new_pos = priv->select_info->selection_end;
5983 if (priv->select_info->selection_end != priv->select_info->selection_anchor &&
5986 /* If we have a current selection and aren't extending it, move to the
5987 * start/or end of the selection as appropriate
5991 case GTK_MOVEMENT_VISUAL_POSITIONS:
5994 gint anchor_x, anchor_y;
5995 gboolean end_is_left;
5997 get_better_cursor (label, priv->select_info->selection_end, &end_x, &end_y);
5998 get_better_cursor (label, priv->select_info->selection_anchor, &anchor_x, &anchor_y);
6000 end_is_left = (end_y < anchor_y) || (end_y == anchor_y && end_x < anchor_x);
6003 new_pos = end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
6005 new_pos = !end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
6008 case GTK_MOVEMENT_LOGICAL_POSITIONS:
6009 case GTK_MOVEMENT_WORDS:
6011 new_pos = MIN (priv->select_info->selection_end, priv->select_info->selection_anchor);
6013 new_pos = MAX (priv->select_info->selection_end, priv->select_info->selection_anchor);
6015 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
6016 case GTK_MOVEMENT_PARAGRAPH_ENDS:
6017 case GTK_MOVEMENT_BUFFER_ENDS:
6018 /* FIXME: Can do better here */
6019 new_pos = count < 0 ? 0 : strlen (priv->text);
6021 case GTK_MOVEMENT_DISPLAY_LINES:
6022 case GTK_MOVEMENT_PARAGRAPHS:
6023 case GTK_MOVEMENT_PAGES:
6024 case GTK_MOVEMENT_HORIZONTAL_PAGES:
6032 case GTK_MOVEMENT_LOGICAL_POSITIONS:
6033 new_pos = gtk_label_move_logically (label, new_pos, count);
6035 case GTK_MOVEMENT_VISUAL_POSITIONS:
6036 new_pos = gtk_label_move_visually (label, new_pos, count);
6037 if (new_pos == old_pos)
6039 if (!extend_selection)
6041 if (!gtk_widget_keynav_failed (GTK_WIDGET (label),
6043 GTK_DIR_RIGHT : GTK_DIR_LEFT))
6045 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
6048 gtk_widget_child_focus (toplevel,
6050 GTK_DIR_RIGHT : GTK_DIR_LEFT);
6055 gtk_widget_error_bell (GTK_WIDGET (label));
6059 case GTK_MOVEMENT_WORDS:
6062 new_pos = gtk_label_move_forward_word (label, new_pos);
6067 new_pos = gtk_label_move_backward_word (label, new_pos);
6070 if (new_pos == old_pos)
6071 gtk_widget_error_bell (GTK_WIDGET (label));
6073 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
6074 case GTK_MOVEMENT_PARAGRAPH_ENDS:
6075 case GTK_MOVEMENT_BUFFER_ENDS:
6076 /* FIXME: Can do better here */
6077 new_pos = count < 0 ? 0 : strlen (priv->text);
6078 if (new_pos == old_pos)
6079 gtk_widget_error_bell (GTK_WIDGET (label));
6081 case GTK_MOVEMENT_DISPLAY_LINES:
6082 case GTK_MOVEMENT_PARAGRAPHS:
6083 case GTK_MOVEMENT_PAGES:
6084 case GTK_MOVEMENT_HORIZONTAL_PAGES:
6089 if (extend_selection)
6090 gtk_label_select_region_index (label,
6091 priv->select_info->selection_anchor,
6094 gtk_label_select_region_index (label, new_pos, new_pos);
6098 gtk_label_copy_clipboard (GtkLabel *label)
6100 GtkLabelPrivate *priv = label->priv;
6102 if (priv->text && priv->select_info)
6106 GtkClipboard *clipboard;
6108 start = MIN (priv->select_info->selection_anchor,
6109 priv->select_info->selection_end);
6110 end = MAX (priv->select_info->selection_anchor,
6111 priv->select_info->selection_end);
6113 len = strlen (priv->text);
6121 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6124 gtk_clipboard_set_text (clipboard, priv->text + start, end - start);
6129 link = gtk_label_get_focus_link (label);
6131 gtk_clipboard_set_text (clipboard, link->uri, -1);
6137 gtk_label_select_all (GtkLabel *label)
6139 GtkLabelPrivate *priv = label->priv;
6141 gtk_label_select_region_index (label, 0, strlen (priv->text));
6144 /* Quick hack of a popup menu
6147 activate_cb (GtkWidget *menuitem,
6150 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6151 g_signal_emit_by_name (label, signal);
6155 append_action_signal (GtkLabel *label,
6157 const gchar *stock_id,
6158 const gchar *signal,
6161 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6163 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
6164 g_signal_connect (menuitem, "activate",
6165 G_CALLBACK (activate_cb), label);
6167 gtk_widget_set_sensitive (menuitem, sensitive);
6169 gtk_widget_show (menuitem);
6170 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6174 popup_menu_detach (GtkWidget *attach_widget,
6177 GtkLabel *label = GTK_LABEL (attach_widget);
6178 GtkLabelPrivate *priv = label->priv;
6180 if (priv->select_info)
6181 priv->select_info->popup_menu = NULL;
6185 popup_position_func (GtkMenu *menu,
6193 GtkAllocation allocation;
6197 label = GTK_LABEL (user_data);
6198 widget = GTK_WIDGET (label);
6200 g_return_if_fail (gtk_widget_get_realized (widget));
6202 screen = gtk_widget_get_screen (widget);
6203 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
6205 gtk_widget_get_allocation (widget, &allocation);
6210 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
6213 gtk_widget_get_allocation (widget, &allocation);
6215 *x += allocation.width / 2;
6216 *y += allocation.height;
6218 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
6219 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
6223 open_link_activate_cb (GtkMenuItem *menu_item,
6228 link = gtk_label_get_current_link (label);
6231 emit_activate_link (label, link);
6235 copy_link_activate_cb (GtkMenuItem *menu_item,
6238 GtkClipboard *clipboard;
6241 uri = gtk_label_get_current_uri (label);
6244 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6245 gtk_clipboard_set_text (clipboard, uri, -1);
6250 gtk_label_popup_menu (GtkWidget *widget)
6252 gtk_label_do_popup (GTK_LABEL (widget), NULL);
6258 gtk_label_do_popup (GtkLabel *label,
6259 GdkEventButton *event)
6261 GtkLabelPrivate *priv = label->priv;
6262 GtkWidget *menuitem;
6265 gboolean have_selection;
6268 if (!priv->select_info)
6271 if (priv->select_info->popup_menu)
6272 gtk_widget_destroy (priv->select_info->popup_menu);
6274 priv->select_info->popup_menu = menu = gtk_menu_new ();
6276 gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (label), popup_menu_detach);
6279 priv->select_info->selection_anchor != priv->select_info->selection_end;
6283 if (priv->select_info->link_clicked)
6284 link = priv->select_info->active_link;
6289 link = gtk_label_get_focus_link (label);
6291 if (!have_selection && link)
6294 menuitem = gtk_image_menu_item_new_with_mnemonic (_("_Open Link"));
6295 gtk_widget_show (menuitem);
6296 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6298 g_signal_connect (G_OBJECT (menuitem), "activate",
6299 G_CALLBACK (open_link_activate_cb), label);
6301 image = gtk_image_new_from_stock (GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_MENU);
6302 gtk_widget_show (image);
6303 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6305 /* Copy Link Address */
6306 menuitem = gtk_image_menu_item_new_with_mnemonic (_("Copy _Link Address"));
6307 gtk_widget_show (menuitem);
6308 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6310 g_signal_connect (G_OBJECT (menuitem), "activate",
6311 G_CALLBACK (copy_link_activate_cb), label);
6313 image = gtk_image_new_from_stock (GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
6314 gtk_widget_show (image);
6315 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6319 append_action_signal (label, menu, GTK_STOCK_CUT, "cut-clipboard", FALSE);
6320 append_action_signal (label, menu, GTK_STOCK_COPY, "copy-clipboard", have_selection);
6321 append_action_signal (label, menu, GTK_STOCK_PASTE, "paste-clipboard", FALSE);
6323 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
6324 gtk_widget_set_sensitive (menuitem, FALSE);
6325 gtk_widget_show (menuitem);
6326 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6328 menuitem = gtk_separator_menu_item_new ();
6329 gtk_widget_show (menuitem);
6330 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6332 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
6333 g_signal_connect_swapped (menuitem, "activate",
6334 G_CALLBACK (gtk_label_select_all), label);
6335 gtk_widget_show (menuitem);
6336 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6339 g_signal_emit (label, signals[POPULATE_POPUP], 0, menu);
6342 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6344 event->button, event->time);
6347 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6348 popup_position_func, label,
6349 0, gtk_get_current_event_time ());
6350 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
6355 gtk_label_clear_links (GtkLabel *label)
6357 GtkLabelPrivate *priv = label->priv;
6359 if (!priv->select_info)
6362 g_list_foreach (priv->select_info->links, (GFunc)link_free, NULL);
6363 g_list_free (priv->select_info->links);
6364 priv->select_info->links = NULL;
6365 priv->select_info->active_link = NULL;
6369 gtk_label_rescan_links (GtkLabel *label)
6371 GtkLabelPrivate *priv = label->priv;
6372 PangoLayout *layout = priv->layout;
6373 PangoAttrList *attlist;
6374 PangoAttrIterator *iter;
6377 if (!priv->select_info || !priv->select_info->links)
6380 attlist = pango_layout_get_attributes (layout);
6382 if (attlist == NULL)
6385 iter = pango_attr_list_get_iterator (attlist);
6387 links = priv->select_info->links;
6391 PangoAttribute *underline;
6392 PangoAttribute *color;
6394 underline = pango_attr_iterator_get (iter, PANGO_ATTR_UNDERLINE);
6395 color = pango_attr_iterator_get (iter, PANGO_ATTR_FOREGROUND);
6397 if (underline != NULL && color != NULL)
6400 PangoRectangle start_pos;
6401 PangoRectangle end_pos;
6404 pango_attr_iterator_range (iter, &start, &end);
6405 pango_layout_index_to_pos (layout, start, &start_pos);
6406 pango_layout_index_to_pos (layout, end, &end_pos);
6410 g_warning ("Ran out of links");
6414 links = links->next;
6415 link->start = start;
6418 } while (pango_attr_iterator_next (iter));
6420 pango_attr_iterator_destroy (iter);
6424 gtk_label_activate_link (GtkLabel *label,
6427 GtkWidget *widget = GTK_WIDGET (label);
6428 GError *error = NULL;
6430 if (!gtk_show_uri (gtk_widget_get_screen (widget),
6431 uri, gtk_get_current_event_time (), &error))
6433 g_warning ("Unable to show '%s': %s", uri, error->message);
6434 g_error_free (error);
6441 emit_activate_link (GtkLabel *label,
6444 GtkLabelPrivate *priv = label->priv;
6447 g_signal_emit (label, signals[ACTIVATE_LINK], 0, link->uri, &handled);
6448 if (handled && priv->track_links && !link->visited)
6450 link->visited = TRUE;
6451 /* FIXME: shouldn't have to redo everything here */
6452 gtk_label_recalculate (label);
6457 gtk_label_activate_current_link (GtkLabel *label)
6460 GtkWidget *widget = GTK_WIDGET (label);
6462 link = gtk_label_get_focus_link (label);
6466 emit_activate_link (label, link);
6470 GtkWidget *toplevel;
6472 GtkWidget *default_widget, *focus_widget;
6474 toplevel = gtk_widget_get_toplevel (widget);
6475 if (GTK_IS_WINDOW (toplevel))
6477 window = GTK_WINDOW (toplevel);
6481 default_widget = gtk_window_get_default_widget (window);
6482 focus_widget = gtk_window_get_focus (window);
6484 if (default_widget != widget &&
6485 !(widget == focus_widget && (!default_widget || !gtk_widget_is_sensitive (default_widget))))
6486 gtk_window_activate_default (window);
6492 static GtkLabelLink *
6493 gtk_label_get_current_link (GtkLabel *label)
6495 GtkLabelPrivate *priv = label->priv;
6498 if (!priv->select_info)
6501 if (priv->select_info->link_clicked)
6502 link = priv->select_info->active_link;
6504 link = gtk_label_get_focus_link (label);
6510 * gtk_label_get_current_uri:
6511 * @label: a #GtkLabel
6513 * Returns the URI for the currently active link in the label.
6514 * The active link is the one under the mouse pointer or, in a
6515 * selectable label, the link in which the text cursor is currently
6518 * This function is intended for use in a #GtkLabel::activate-link handler
6519 * or for use in a #GtkWidget::query-tooltip handler.
6521 * Returns: the currently active URI. The string is owned by GTK+ and must
6522 * not be freed or modified.
6527 gtk_label_get_current_uri (GtkLabel *label)
6531 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
6533 link = gtk_label_get_current_link (label);
6542 * gtk_label_set_track_visited_links:
6543 * @label: a #GtkLabel
6544 * @track_links: %TRUE to track visited links
6546 * Sets whether the label should keep track of clicked
6547 * links (and use a different color for them).
6552 gtk_label_set_track_visited_links (GtkLabel *label,
6553 gboolean track_links)
6555 GtkLabelPrivate *priv;
6557 g_return_if_fail (GTK_IS_LABEL (label));
6561 track_links = track_links != FALSE;
6563 if (priv->track_links != track_links)
6565 priv->track_links = track_links;
6567 /* FIXME: shouldn't have to redo everything here */
6568 gtk_label_recalculate (label);
6570 g_object_notify (G_OBJECT (label), "track-visited-links");
6575 * gtk_label_get_track_visited_links:
6576 * @label: a #GtkLabel
6578 * Returns whether the label is currently keeping track
6581 * Returns: %TRUE if clicked links are remembered
6586 gtk_label_get_track_visited_links (GtkLabel *label)
6588 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
6590 return label->priv->track_links;
6594 gtk_label_query_tooltip (GtkWidget *widget,
6597 gboolean keyboard_tip,
6598 GtkTooltip *tooltip)
6600 GtkLabel *label = GTK_LABEL (widget);
6601 GtkLabelPrivate *priv = label->priv;
6602 GtkLabelSelectionInfo *info = priv->select_info;
6606 if (info && info->links)
6610 if (info->selection_anchor == info->selection_end)
6611 index = info->selection_anchor;
6615 if (!get_layout_index (label, x, y, &index))
6621 for (l = info->links; l != NULL; l = l->next)
6623 GtkLabelLink *link = l->data;
6624 if (index >= link->start && index <= link->end)
6628 gtk_tooltip_set_markup (tooltip, link->title);
6637 return GTK_WIDGET_CLASS (gtk_label_parent_class)->query_tooltip (widget,
6644 _gtk_label_get_cursor_position (GtkLabel *label)
6646 GtkLabelPrivate *priv = label->priv;
6648 if (priv->select_info && priv->select_info->selectable)
6649 return g_utf8_pointer_to_offset (priv->text,
6650 priv->text + priv->select_info->selection_end);
6656 _gtk_label_get_selection_bound (GtkLabel *label)
6658 GtkLabelPrivate *priv = label->priv;
6660 if (priv->select_info && priv->select_info->selectable)
6661 return g_utf8_pointer_to_offset (priv->text,
6662 priv->text + priv->select_info->selection_anchor);