1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
21 * file for a list of people on the GTK+ Team. See the ChangeLog
22 * files for a list of changes. These files are distributed with
23 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
32 #include "gtkaccellabel.h"
34 #include "gtkmainprivate.h"
35 #include "gtkmarshalers.h"
37 #include "gtkwindow.h"
38 #include "gtkclipboard.h"
39 #include "gtkimagemenuitem.h"
41 #include "gtkseparatormenuitem.h"
42 #include "gtktextutil.h"
43 #include "gtkmenuitem.h"
44 #include "gtkmenushellprivate.h"
45 #include "gtknotebook.h"
47 #include "gtkbindings.h"
48 #include "gtkbuildable.h"
51 #include "gtktooltip.h"
52 #include "gtkprivate.h"
53 #include "gtktypebuiltins.h"
58 * @Short_description: A widget that displays a small to medium amount of text
61 * The #GtkLabel widget displays a small amount of text. As the name
62 * implies, most labels are used to label another widget such as a
63 * #GtkButton, a #GtkMenuItem, or a #GtkOptionMenu.
65 * <refsect2 id="GtkLabel-BUILDER-UI">
66 * <title>GtkLabel as GtkBuildable</title>
68 * The GtkLabel implementation of the GtkBuildable interface supports a
69 * custom <attributes> element, which supports any number of <attribute>
70 * elements. the <attribute> element has attributes named name, value,
71 * start and end and allows you to specify #PangoAttribute values for this label.
74 * <title>A UI definition fragment specifying Pango attributes</title>
75 * <programlisting><![CDATA[
76 * <object class="GtkLabel">
78 * <attribute name="weight" value="PANGO_WEIGHT_BOLD"/>
79 * <attribute name="background" value="red" start="5" end="10"/>"
82 * ]]></programlisting>
84 * The start and end attributes specify the range of characters to which the
85 * Pango attribute applies. If start and end are not specified, the attribute is
86 * applied to the whole text. Note that specifying ranges does not make much
87 * sense with translatable attributes. Use markup embedded in the translatable
92 * <title>Mnemonics</title>
94 * Labels may contain <firstterm>mnemonics</firstterm>. Mnemonics are
95 * underlined characters in the label, used for keyboard navigation.
96 * Mnemonics are created by providing a string with an underscore before
97 * the mnemonic character, such as <literal>"_File"</literal>, to the
98 * functions gtk_label_new_with_mnemonic() or
99 * gtk_label_set_text_with_mnemonic().
101 * Mnemonics automatically activate any activatable widget the label is
102 * inside, such as a #GtkButton; if the label is not inside the
103 * mnemonic's target widget, you have to tell the label about the target
104 * using gtk_label_set_mnemonic_widget(). Here's a simple example where
105 * the label is inside a button:
109 * // Pressing Alt+H will activate this button
110 * button = gtk_button_new (<!-- -->);
111 * label = gtk_label_new_with_mnemonic ("_Hello");
112 * gtk_container_add (GTK_CONTAINER (button), label);
116 * There's a convenience function to create buttons with a mnemonic label
121 * // Pressing Alt+H will activate this button
122 * button = gtk_button_new_with_mnemonic ("_Hello");
126 * To create a mnemonic for a widget alongside the label, such as a
127 * #GtkEntry, you have to point the label at the entry with
128 * gtk_label_set_mnemonic_widget():
132 * // Pressing Alt+H will focus the entry
133 * entry = gtk_entry_new (<!-- -->);
134 * label = gtk_label_new_with_mnemonic ("_Hello");
135 * gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
141 * <title>Markup (styled text)</title>
143 * To make it easy to format text in a label (changing colors, fonts,
144 * etc.), label text can be provided in a simple <link
145 * linkend="PangoMarkupFormat">markup format</link>.
146 * Here's how to create a label with a small font:
150 * label = gtk_label_new (NULL);
151 * gtk_label_set_markup (GTK_LABEL (label), "<small>Small text</small>");
156 * linkend="PangoMarkupFormat">complete documentation</link> of available
157 * tags in the Pango manual.)
159 * The markup passed to gtk_label_set_markup() must be valid; for example,
160 * literal <, > and & characters must be escaped as \<,
161 * \gt;, and \&. If you pass text obtained from the user, file,
162 * or a network to gtk_label_set_markup(), you'll want to escape it with
163 * g_markup_escape_text() or g_markup_printf_escaped().
165 * Markup strings are just a convenient way to set the #PangoAttrList on
166 * a label; gtk_label_set_attributes() may be a simpler way to set
167 * attributes in some cases. Be careful though; #PangoAttrList tends to
168 * cause internationalization problems, unless you're applying attributes
169 * to the entire string (i.e. unless you set the range of each attribute
170 * to [0, %G_MAXINT)). The reason is that specifying the start_index and
171 * end_index for a #PangoAttribute requires knowledge of the exact string
172 * being displayed, so translations will cause problems.
176 * <title>Selectable labels</title>
177 * Labels can be made selectable with gtk_label_set_selectable().
178 * Selectable labels allow the user to copy the label contents to
179 * the clipboard. Only labels that contain useful-to-copy information
180 * — such as error messages — should be made selectable.
182 * <refsect2 id="label-text-layout">
183 * <title>Text layout</title>
185 * A label can contain any number of paragraphs, but will have
186 * performance problems if it contains more than a small number.
187 * Paragraphs are separated by newlines or other paragraph separators
188 * understood by Pango.
190 * Labels can automatically wrap text if you call
191 * gtk_label_set_line_wrap().
193 * gtk_label_set_justify() sets how the lines in a label align
194 * with one another. If you want to set how the label as a whole
195 * aligns in its available space, see gtk_misc_set_alignment().
197 * The #GtkLabel:width-chars and #GtkLabel:max-width-chars properties
198 * can be used to control the size allocation of ellipsized or wrapped
199 * labels. For ellipsizing labels, if either is specified (and less
200 * than the actual text size), it is used as the minimum width, and the actual
201 * text size is used as the natural width of the label. For wrapping labels,
202 * width-chars is used as the minimum width, if specified, and max-width-chars
203 * is used as the natural width. Even if max-width-chars specified, wrapping
204 * labels will be rewrapped to use all of the available width.
207 * Note that the interpretation of #GtkLabel:width-chars and
208 * #GtkLabel:max-width-chars has changed a bit with the introduction of
209 * <link linkend="geometry-management">width-for-height geometry management.</link>
214 * <title>Links</title>
216 * Since 2.18, GTK+ supports markup for clickable hyperlinks in addition
217 * to regular Pango markup. The markup for links is borrowed from HTML, using the
218 * <tag>a</tag> with href and title attributes. GTK+ renders links similar to the
219 * way they appear in web browsers, with colored, underlined text. The title
220 * attribute is displayed as a tooltip on the link. An example looks like this:
222 * <informalexample><programlisting>
223 * gtk_label_set_markup (label, "Go to the <a href="http://www.gtk.org" title="<i>Our</i> website">GTK+ website</a> for more...");
224 * </programlisting></informalexample>
226 * It is possible to implement custom handling for links and their tooltips with
227 * the #GtkLabel::activate-link signal and the gtk_label_get_current_uri() function.
233 /*rint() is only available in GCC and/or C99*/
234 #if (__STDC_VERSION__ < 199901L && !defined __GNUC__)
235 double rint(double x)
237 if (ceil(x+0.5) == floor(x+0.5))
239 int a = (int)ceil(x);
252 struct _GtkLabelPrivate
254 GtkLabelSelectionInfo *select_info;
255 GtkWidget *mnemonic_widget;
256 GtkWindow *mnemonic_window;
258 PangoAttrList *attrs;
259 PangoAttrList *effective_attrs;
267 guint mnemonics_visible : 1;
270 guint use_underline : 1;
271 guint use_markup : 1;
273 guint single_line_mode : 1;
274 guint have_transform : 1;
277 guint pattern_set : 1;
278 guint track_links : 1;
280 guint mnemonic_keyval;
283 gint max_width_chars;
286 /* Notes about the handling of links:
288 * Links share the GtkLabelSelectionInfo struct with selectable labels.
289 * There are some new fields for links. The links field contains the list
290 * of GtkLabelLink structs that describe the links which are embedded in
291 * the label. The active_link field points to the link under the mouse
292 * pointer. For keyboard navigation, the 'focus' link is determined by
293 * finding the link which contains the selection_anchor position.
294 * The link_clicked field is used with button press and release events
295 * to ensure that pressing inside a link and releasing outside of it
296 * does not activate the link.
298 * Links are rendered with the link-color/visited-link-color colors
299 * that are determined by the style and with an underline. When the mouse
300 * pointer is over a link, the pointer is changed to indicate the link,
301 * and the background behind the link is rendered with the base[PRELIGHT]
302 * color. While a button is pressed over a link, the background is rendered
303 * with the base[ACTIVE] color.
305 * Labels with links accept keyboard focus, and it is possible to move
306 * the focus between the embedded links using Tab/Shift-Tab. The focus
307 * is indicated by a focus rectangle that is drawn around the link text.
308 * Pressing Enter activates the focussed link, and there is a suitable
309 * context menu for links that can be opened with the Menu key. Pressing
310 * Control-C copies the link URI to the clipboard.
312 * In selectable labels with links, link functionality is only available
313 * when the selection is empty.
318 gchar *title; /* the title attribute, used as tooltip */
319 gboolean visited; /* get set when the link is activated; this flag
320 * gets preserved over later set_markup() calls
322 gint start; /* position of the link in the PangoLayout */
326 struct _GtkLabelSelectionInfo
329 gint selection_anchor;
331 GtkWidget *popup_menu;
334 GtkLabelLink *active_link;
340 guint select_words : 1;
341 guint selectable : 1;
342 guint link_clicked : 1;
350 ACTIVATE_CURRENT_LINK,
365 PROP_MNEMONIC_KEYVAL,
366 PROP_MNEMONIC_WIDGET,
367 PROP_CURSOR_POSITION,
368 PROP_SELECTION_BOUND,
371 PROP_SINGLE_LINE_MODE,
373 PROP_MAX_WIDTH_CHARS,
374 PROP_TRACK_VISITED_LINKS
377 /* When rotating ellipsizable text we want the natural size to request
378 * more to ensure the label wont ever ellipsize in an allocation of full natural size.
380 #define ROTATION_ELLIPSIZE_PADDING 2
382 static guint signals[LAST_SIGNAL] = { 0 };
384 static const GdkColor default_link_color = { 0, 0, 0, 0xeeee };
385 static const GdkColor default_visited_link_color = { 0, 0x5555, 0x1a1a, 0x8b8b };
387 static void gtk_label_set_property (GObject *object,
391 static void gtk_label_get_property (GObject *object,
395 static void gtk_label_finalize (GObject *object);
396 static void gtk_label_destroy (GtkWidget *widget);
397 static void gtk_label_size_allocate (GtkWidget *widget,
398 GtkAllocation *allocation);
399 static void gtk_label_state_changed (GtkWidget *widget,
401 static void gtk_label_style_updated (GtkWidget *widget);
402 static void gtk_label_direction_changed (GtkWidget *widget,
403 GtkTextDirection previous_dir);
404 static gint gtk_label_draw (GtkWidget *widget,
406 static gboolean gtk_label_focus (GtkWidget *widget,
407 GtkDirectionType direction);
409 static void gtk_label_realize (GtkWidget *widget);
410 static void gtk_label_unrealize (GtkWidget *widget);
411 static void gtk_label_map (GtkWidget *widget);
412 static void gtk_label_unmap (GtkWidget *widget);
414 static gboolean gtk_label_button_press (GtkWidget *widget,
415 GdkEventButton *event);
416 static gboolean gtk_label_button_release (GtkWidget *widget,
417 GdkEventButton *event);
418 static gboolean gtk_label_motion (GtkWidget *widget,
419 GdkEventMotion *event);
420 static gboolean gtk_label_leave_notify (GtkWidget *widget,
421 GdkEventCrossing *event);
423 static void gtk_label_grab_focus (GtkWidget *widget);
425 static gboolean gtk_label_query_tooltip (GtkWidget *widget,
428 gboolean keyboard_tip,
429 GtkTooltip *tooltip);
431 static void gtk_label_set_text_internal (GtkLabel *label,
433 static void gtk_label_set_label_internal (GtkLabel *label,
435 static void gtk_label_set_use_markup_internal (GtkLabel *label,
437 static void gtk_label_set_use_underline_internal (GtkLabel *label,
439 static void gtk_label_set_attributes_internal (GtkLabel *label,
440 PangoAttrList *attrs);
441 static void gtk_label_set_uline_text_internal (GtkLabel *label,
443 static void gtk_label_set_pattern_internal (GtkLabel *label,
444 const gchar *pattern,
445 gboolean is_mnemonic);
446 static void gtk_label_set_markup_internal (GtkLabel *label,
448 gboolean with_uline);
449 static void gtk_label_recalculate (GtkLabel *label);
450 static void gtk_label_hierarchy_changed (GtkWidget *widget,
451 GtkWidget *old_toplevel);
452 static void gtk_label_screen_changed (GtkWidget *widget,
453 GdkScreen *old_screen);
454 static gboolean gtk_label_popup_menu (GtkWidget *widget);
456 static void gtk_label_create_window (GtkLabel *label);
457 static void gtk_label_destroy_window (GtkLabel *label);
458 static void gtk_label_ensure_select_info (GtkLabel *label);
459 static void gtk_label_clear_select_info (GtkLabel *label);
460 static void gtk_label_update_cursor (GtkLabel *label);
461 static void gtk_label_clear_layout (GtkLabel *label);
462 static void gtk_label_ensure_layout (GtkLabel *label);
463 static void gtk_label_select_region_index (GtkLabel *label,
467 static gboolean gtk_label_mnemonic_activate (GtkWidget *widget,
468 gboolean group_cycling);
469 static void gtk_label_setup_mnemonic (GtkLabel *label,
471 static void gtk_label_drag_data_get (GtkWidget *widget,
472 GdkDragContext *context,
473 GtkSelectionData *selection_data,
477 static void gtk_label_buildable_interface_init (GtkBuildableIface *iface);
478 static gboolean gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
481 const gchar *tagname,
482 GMarkupParser *parser,
485 static void gtk_label_buildable_custom_finished (GtkBuildable *buildable,
488 const gchar *tagname,
492 static void connect_mnemonics_visible_notify (GtkLabel *label);
493 static gboolean separate_uline_pattern (const gchar *str,
499 /* For selectable labels: */
500 static void gtk_label_move_cursor (GtkLabel *label,
501 GtkMovementStep step,
503 gboolean extend_selection);
504 static void gtk_label_copy_clipboard (GtkLabel *label);
505 static void gtk_label_select_all (GtkLabel *label);
506 static void gtk_label_do_popup (GtkLabel *label,
507 GdkEventButton *event);
508 static gint gtk_label_move_forward_word (GtkLabel *label,
510 static gint gtk_label_move_backward_word (GtkLabel *label,
514 static void gtk_label_rescan_links (GtkLabel *label);
515 static void gtk_label_clear_links (GtkLabel *label);
516 static gboolean gtk_label_activate_link (GtkLabel *label,
518 static void gtk_label_activate_current_link (GtkLabel *label);
519 static GtkLabelLink *gtk_label_get_current_link (GtkLabel *label);
520 static void gtk_label_get_link_colors (GtkWidget *widget,
521 GdkColor **link_color,
522 GdkColor **visited_link_color);
523 static void emit_activate_link (GtkLabel *label,
526 static GtkSizeRequestMode gtk_label_get_request_mode (GtkWidget *widget);
527 static void gtk_label_get_preferred_width (GtkWidget *widget,
530 static void gtk_label_get_preferred_height (GtkWidget *widget,
533 static void gtk_label_get_preferred_width_for_height (GtkWidget *widget,
536 gint *natural_width);
537 static void gtk_label_get_preferred_height_for_width (GtkWidget *widget,
539 gint *minimum_height,
540 gint *natural_height);
542 static GtkBuildableIface *buildable_parent_iface = NULL;
544 G_DEFINE_TYPE_WITH_CODE (GtkLabel, gtk_label, GTK_TYPE_MISC,
545 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
546 gtk_label_buildable_interface_init))
549 add_move_binding (GtkBindingSet *binding_set,
552 GtkMovementStep step,
555 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
557 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
561 G_TYPE_BOOLEAN, FALSE);
563 /* Selection-extending version */
564 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
568 G_TYPE_BOOLEAN, TRUE);
572 gtk_label_class_init (GtkLabelClass *class)
574 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
575 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
576 GtkBindingSet *binding_set;
578 gobject_class->set_property = gtk_label_set_property;
579 gobject_class->get_property = gtk_label_get_property;
580 gobject_class->finalize = gtk_label_finalize;
582 widget_class->destroy = gtk_label_destroy;
583 widget_class->size_allocate = gtk_label_size_allocate;
584 widget_class->state_changed = gtk_label_state_changed;
585 widget_class->style_updated = gtk_label_style_updated;
586 widget_class->query_tooltip = gtk_label_query_tooltip;
587 widget_class->direction_changed = gtk_label_direction_changed;
588 widget_class->draw = gtk_label_draw;
589 widget_class->realize = gtk_label_realize;
590 widget_class->unrealize = gtk_label_unrealize;
591 widget_class->map = gtk_label_map;
592 widget_class->unmap = gtk_label_unmap;
593 widget_class->button_press_event = gtk_label_button_press;
594 widget_class->button_release_event = gtk_label_button_release;
595 widget_class->motion_notify_event = gtk_label_motion;
596 widget_class->leave_notify_event = gtk_label_leave_notify;
597 widget_class->hierarchy_changed = gtk_label_hierarchy_changed;
598 widget_class->screen_changed = gtk_label_screen_changed;
599 widget_class->mnemonic_activate = gtk_label_mnemonic_activate;
600 widget_class->drag_data_get = gtk_label_drag_data_get;
601 widget_class->grab_focus = gtk_label_grab_focus;
602 widget_class->popup_menu = gtk_label_popup_menu;
603 widget_class->focus = gtk_label_focus;
604 widget_class->get_request_mode = gtk_label_get_request_mode;
605 widget_class->get_preferred_width = gtk_label_get_preferred_width;
606 widget_class->get_preferred_height = gtk_label_get_preferred_height;
607 widget_class->get_preferred_width_for_height = gtk_label_get_preferred_width_for_height;
608 widget_class->get_preferred_height_for_width = gtk_label_get_preferred_height_for_width;
610 class->move_cursor = gtk_label_move_cursor;
611 class->copy_clipboard = gtk_label_copy_clipboard;
612 class->activate_link = gtk_label_activate_link;
615 * GtkLabel::move-cursor:
616 * @entry: the object which received the signal
617 * @step: the granularity of the move, as a #GtkMovementStep
618 * @count: the number of @step units to move
619 * @extend_selection: %TRUE if the move should extend the selection
621 * The ::move-cursor signal is a
622 * <link linkend="keybinding-signals">keybinding signal</link>
623 * which gets emitted when the user initiates a cursor movement.
624 * If the cursor is not visible in @entry, this signal causes
625 * the viewport to be moved instead.
627 * Applications should not connect to it, but may emit it with
628 * g_signal_emit_by_name() if they need to control the cursor
631 * The default bindings for this signal come in two variants,
632 * the variant with the Shift modifier extends the selection,
633 * the variant without the Shift modifer does not.
634 * There are too many key combinations to list them all here.
636 * <listitem>Arrow keys move by individual characters/lines</listitem>
637 * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
638 * <listitem>Home/End keys move to the ends of the buffer</listitem>
641 signals[MOVE_CURSOR] =
642 g_signal_new (I_("move-cursor"),
643 G_OBJECT_CLASS_TYPE (gobject_class),
644 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
645 G_STRUCT_OFFSET (GtkLabelClass, move_cursor),
647 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
649 GTK_TYPE_MOVEMENT_STEP,
654 * GtkLabel::copy-clipboard:
655 * @label: the object which received the signal
657 * The ::copy-clipboard signal is a
658 * <link linkend="keybinding-signals">keybinding signal</link>
659 * which gets emitted to copy the selection to the clipboard.
661 * The default binding for this signal is Ctrl-c.
663 signals[COPY_CLIPBOARD] =
664 g_signal_new (I_("copy-clipboard"),
665 G_OBJECT_CLASS_TYPE (gobject_class),
666 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
667 G_STRUCT_OFFSET (GtkLabelClass, copy_clipboard),
669 _gtk_marshal_VOID__VOID,
673 * GtkLabel::populate-popup:
674 * @label: The label on which the signal is emitted
675 * @menu: the menu that is being populated
677 * The ::populate-popup signal gets emitted before showing the
678 * context menu of the label. Note that only selectable labels
679 * have context menus.
681 * If you need to add items to the context menu, connect
682 * to this signal and append your menuitems to the @menu.
684 signals[POPULATE_POPUP] =
685 g_signal_new (I_("populate-popup"),
686 G_OBJECT_CLASS_TYPE (gobject_class),
688 G_STRUCT_OFFSET (GtkLabelClass, populate_popup),
690 _gtk_marshal_VOID__OBJECT,
695 * GtkLabel::activate-current-link:
696 * @label: The label on which the signal was emitted
698 * A <link linkend="keybinding-signals">keybinding signal</link>
699 * which gets emitted when the user activates a link in the label.
701 * Applications may also emit the signal with g_signal_emit_by_name()
702 * if they need to control activation of URIs programmatically.
704 * The default bindings for this signal are all forms of the Enter key.
708 signals[ACTIVATE_CURRENT_LINK] =
709 g_signal_new_class_handler ("activate-current-link",
710 G_TYPE_FROM_CLASS (gobject_class),
711 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
712 G_CALLBACK (gtk_label_activate_current_link),
714 _gtk_marshal_VOID__VOID,
718 * GtkLabel::activate-link:
719 * @label: The label on which the signal was emitted
720 * @uri: the URI that is activated
722 * The signal which gets emitted to activate a URI.
723 * Applications may connect to it to override the default behaviour,
724 * which is to call gtk_show_uri().
726 * Returns: %TRUE if the link has been activated
730 signals[ACTIVATE_LINK] =
731 g_signal_new ("activate-link",
732 G_TYPE_FROM_CLASS (gobject_class),
734 G_STRUCT_OFFSET (GtkLabelClass, activate_link),
735 _gtk_boolean_handled_accumulator, NULL,
736 _gtk_marshal_BOOLEAN__STRING,
737 G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
739 g_object_class_install_property (gobject_class,
741 g_param_spec_string ("label",
743 P_("The text of the label"),
745 GTK_PARAM_READWRITE));
746 g_object_class_install_property (gobject_class,
748 g_param_spec_boxed ("attributes",
750 P_("A list of style attributes to apply to the text of the label"),
751 PANGO_TYPE_ATTR_LIST,
752 GTK_PARAM_READWRITE));
753 g_object_class_install_property (gobject_class,
755 g_param_spec_boolean ("use-markup",
757 P_("The text of the label includes XML markup. See pango_parse_markup()"),
759 GTK_PARAM_READWRITE));
760 g_object_class_install_property (gobject_class,
762 g_param_spec_boolean ("use-underline",
764 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
766 GTK_PARAM_READWRITE));
768 g_object_class_install_property (gobject_class,
770 g_param_spec_enum ("justify",
772 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"),
773 GTK_TYPE_JUSTIFICATION,
775 GTK_PARAM_READWRITE));
777 g_object_class_install_property (gobject_class,
779 g_param_spec_string ("pattern",
781 P_("A string with _ characters in positions correspond to characters in the text to underline"),
783 GTK_PARAM_WRITABLE));
785 g_object_class_install_property (gobject_class,
787 g_param_spec_boolean ("wrap",
789 P_("If set, wrap lines if the text becomes too wide"),
791 GTK_PARAM_READWRITE));
793 * GtkLabel:wrap-mode:
795 * If line wrapping is on (see the #GtkLabel:wrap property) this controls
796 * how the line wrapping is done. The default is %PANGO_WRAP_WORD, which
797 * means wrap on word boundaries.
801 g_object_class_install_property (gobject_class,
803 g_param_spec_enum ("wrap-mode",
804 P_("Line wrap mode"),
805 P_("If wrap is set, controls how linewrapping is done"),
806 PANGO_TYPE_WRAP_MODE,
808 GTK_PARAM_READWRITE));
809 g_object_class_install_property (gobject_class,
811 g_param_spec_boolean ("selectable",
813 P_("Whether the label text can be selected with the mouse"),
815 GTK_PARAM_READWRITE));
816 g_object_class_install_property (gobject_class,
817 PROP_MNEMONIC_KEYVAL,
818 g_param_spec_uint ("mnemonic-keyval",
820 P_("The mnemonic accelerator key for this label"),
824 GTK_PARAM_READABLE));
825 g_object_class_install_property (gobject_class,
826 PROP_MNEMONIC_WIDGET,
827 g_param_spec_object ("mnemonic-widget",
828 P_("Mnemonic widget"),
829 P_("The widget to be activated when the label's mnemonic "
832 GTK_PARAM_READWRITE));
834 g_object_class_install_property (gobject_class,
835 PROP_CURSOR_POSITION,
836 g_param_spec_int ("cursor-position",
837 P_("Cursor Position"),
838 P_("The current position of the insertion cursor in chars"),
842 GTK_PARAM_READABLE));
844 g_object_class_install_property (gobject_class,
845 PROP_SELECTION_BOUND,
846 g_param_spec_int ("selection-bound",
847 P_("Selection Bound"),
848 P_("The position of the opposite end of the selection from the cursor in chars"),
852 GTK_PARAM_READABLE));
855 * GtkLabel:ellipsize:
857 * The preferred place to ellipsize the string, if the label does
858 * not have enough room to display the entire string, specified as a
859 * #PangoEllisizeMode.
861 * Note that setting this property to a value other than
862 * %PANGO_ELLIPSIZE_NONE has the side-effect that the label requests
863 * only enough space to display the ellipsis "...". In particular, this
864 * means that ellipsizing labels do not work well in notebook tabs, unless
865 * the tab's #GtkNotebook:tab-expand property is set to %TRUE. Other ways
866 * to set a label's width are gtk_widget_set_size_request() and
867 * gtk_label_set_width_chars().
871 g_object_class_install_property (gobject_class,
873 g_param_spec_enum ("ellipsize",
875 P_("The preferred place to ellipsize the string, if the label does not have enough room to display the entire string"),
876 PANGO_TYPE_ELLIPSIZE_MODE,
877 PANGO_ELLIPSIZE_NONE,
878 GTK_PARAM_READWRITE));
881 * GtkLabel:width-chars:
883 * The desired width of the label, in characters. If this property is set to
884 * -1, the width will be calculated automatically.
886 * See the section on <link linkend="label-text-layout">text layout</link>
887 * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
888 * determine the width of ellipsized and wrapped labels.
892 g_object_class_install_property (gobject_class,
894 g_param_spec_int ("width-chars",
895 P_("Width In Characters"),
896 P_("The desired width of the label, in characters"),
900 GTK_PARAM_READWRITE));
903 * GtkLabel:single-line-mode:
905 * Whether the label is in single line mode. In single line mode,
906 * the height of the label does not depend on the actual text, it
907 * is always set to ascent + descent of the font. This can be an
908 * advantage in situations where resizing the label because of text
909 * changes would be distracting, e.g. in a statusbar.
913 g_object_class_install_property (gobject_class,
914 PROP_SINGLE_LINE_MODE,
915 g_param_spec_boolean ("single-line-mode",
916 P_("Single Line Mode"),
917 P_("Whether the label is in single line mode"),
919 GTK_PARAM_READWRITE));
924 * The angle that the baseline of the label makes with the horizontal,
925 * in degrees, measured counterclockwise. An angle of 90 reads from
926 * from bottom to top, an angle of 270, from top to bottom. Ignored
927 * if the label is selectable, wrapped, or ellipsized.
931 g_object_class_install_property (gobject_class,
933 g_param_spec_double ("angle",
935 P_("Angle at which the label is rotated"),
939 GTK_PARAM_READWRITE));
942 * GtkLabel:max-width-chars:
944 * The desired maximum width of the label, in characters. If this property
945 * is set to -1, the width will be calculated automatically.
947 * See the section on <link linkend="label-text-layout">text layout</link>
948 * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
949 * determine the width of ellipsized and wrapped labels.
953 g_object_class_install_property (gobject_class,
954 PROP_MAX_WIDTH_CHARS,
955 g_param_spec_int ("max-width-chars",
956 P_("Maximum Width In Characters"),
957 P_("The desired maximum width of the label, in characters"),
961 GTK_PARAM_READWRITE));
964 * GtkLabel:track-visited-links:
966 * Set this property to %TRUE to make the label track which links
967 * have been clicked. It will then apply the ::visited-link-color
968 * color, instead of ::link-color.
972 g_object_class_install_property (gobject_class,
973 PROP_TRACK_VISITED_LINKS,
974 g_param_spec_boolean ("track-visited-links",
975 P_("Track visited links"),
976 P_("Whether visited links should be tracked"),
978 GTK_PARAM_READWRITE));
983 binding_set = gtk_binding_set_by_class (class);
985 /* Moving the insertion point */
986 add_move_binding (binding_set, GDK_KEY_Right, 0,
987 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
989 add_move_binding (binding_set, GDK_KEY_Left, 0,
990 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
992 add_move_binding (binding_set, GDK_KEY_KP_Right, 0,
993 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
995 add_move_binding (binding_set, GDK_KEY_KP_Left, 0,
996 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
998 add_move_binding (binding_set, GDK_KEY_f, GDK_CONTROL_MASK,
999 GTK_MOVEMENT_LOGICAL_POSITIONS, 1);
1001 add_move_binding (binding_set, GDK_KEY_b, GDK_CONTROL_MASK,
1002 GTK_MOVEMENT_LOGICAL_POSITIONS, -1);
1004 add_move_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK,
1005 GTK_MOVEMENT_WORDS, 1);
1007 add_move_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK,
1008 GTK_MOVEMENT_WORDS, -1);
1010 add_move_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK,
1011 GTK_MOVEMENT_WORDS, 1);
1013 add_move_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK,
1014 GTK_MOVEMENT_WORDS, -1);
1017 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
1019 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1021 G_TYPE_BOOLEAN, FALSE);
1023 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
1025 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1027 G_TYPE_BOOLEAN, TRUE);
1029 gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
1031 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1033 G_TYPE_BOOLEAN, FALSE);
1035 gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
1037 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1039 G_TYPE_BOOLEAN, TRUE);
1042 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1044 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1046 G_TYPE_BOOLEAN, FALSE);
1048 gtk_binding_entry_add_signal (binding_set, GDK_KEY_backslash, GDK_CONTROL_MASK,
1050 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1052 G_TYPE_BOOLEAN, FALSE);
1054 add_move_binding (binding_set, GDK_KEY_f, GDK_MOD1_MASK,
1055 GTK_MOVEMENT_WORDS, 1);
1057 add_move_binding (binding_set, GDK_KEY_b, GDK_MOD1_MASK,
1058 GTK_MOVEMENT_WORDS, -1);
1060 add_move_binding (binding_set, GDK_KEY_Home, 0,
1061 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1063 add_move_binding (binding_set, GDK_KEY_End, 0,
1064 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1066 add_move_binding (binding_set, GDK_KEY_KP_Home, 0,
1067 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1069 add_move_binding (binding_set, GDK_KEY_KP_End, 0,
1070 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1072 add_move_binding (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK,
1073 GTK_MOVEMENT_BUFFER_ENDS, -1);
1075 add_move_binding (binding_set, GDK_KEY_End, GDK_CONTROL_MASK,
1076 GTK_MOVEMENT_BUFFER_ENDS, 1);
1078 add_move_binding (binding_set, GDK_KEY_KP_Home, GDK_CONTROL_MASK,
1079 GTK_MOVEMENT_BUFFER_ENDS, -1);
1081 add_move_binding (binding_set, GDK_KEY_KP_End, GDK_CONTROL_MASK,
1082 GTK_MOVEMENT_BUFFER_ENDS, 1);
1085 gtk_binding_entry_add_signal (binding_set, GDK_KEY_c, GDK_CONTROL_MASK,
1086 "copy-clipboard", 0);
1088 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Return, 0,
1089 "activate-current-link", 0);
1090 gtk_binding_entry_add_signal (binding_set, GDK_KEY_ISO_Enter, 0,
1091 "activate-current-link", 0);
1092 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Enter, 0,
1093 "activate-current-link", 0);
1095 g_type_class_add_private (class, sizeof (GtkLabelPrivate));
1099 gtk_label_set_property (GObject *object,
1101 const GValue *value,
1104 GtkLabel *label = GTK_LABEL (object);
1109 gtk_label_set_label (label, g_value_get_string (value));
1111 case PROP_ATTRIBUTES:
1112 gtk_label_set_attributes (label, g_value_get_boxed (value));
1114 case PROP_USE_MARKUP:
1115 gtk_label_set_use_markup (label, g_value_get_boolean (value));
1117 case PROP_USE_UNDERLINE:
1118 gtk_label_set_use_underline (label, g_value_get_boolean (value));
1121 gtk_label_set_justify (label, g_value_get_enum (value));
1124 gtk_label_set_pattern (label, g_value_get_string (value));
1127 gtk_label_set_line_wrap (label, g_value_get_boolean (value));
1129 case PROP_WRAP_MODE:
1130 gtk_label_set_line_wrap_mode (label, g_value_get_enum (value));
1132 case PROP_SELECTABLE:
1133 gtk_label_set_selectable (label, g_value_get_boolean (value));
1135 case PROP_MNEMONIC_WIDGET:
1136 gtk_label_set_mnemonic_widget (label, (GtkWidget*) g_value_get_object (value));
1138 case PROP_ELLIPSIZE:
1139 gtk_label_set_ellipsize (label, g_value_get_enum (value));
1141 case PROP_WIDTH_CHARS:
1142 gtk_label_set_width_chars (label, g_value_get_int (value));
1144 case PROP_SINGLE_LINE_MODE:
1145 gtk_label_set_single_line_mode (label, g_value_get_boolean (value));
1148 gtk_label_set_angle (label, g_value_get_double (value));
1150 case PROP_MAX_WIDTH_CHARS:
1151 gtk_label_set_max_width_chars (label, g_value_get_int (value));
1153 case PROP_TRACK_VISITED_LINKS:
1154 gtk_label_set_track_visited_links (label, g_value_get_boolean (value));
1157 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1163 gtk_label_get_property (GObject *object,
1168 GtkLabel *label = GTK_LABEL (object);
1169 GtkLabelPrivate *priv = label->priv;
1174 g_value_set_string (value, priv->label);
1176 case PROP_ATTRIBUTES:
1177 g_value_set_boxed (value, priv->attrs);
1179 case PROP_USE_MARKUP:
1180 g_value_set_boolean (value, priv->use_markup);
1182 case PROP_USE_UNDERLINE:
1183 g_value_set_boolean (value, priv->use_underline);
1186 g_value_set_enum (value, priv->jtype);
1189 g_value_set_boolean (value, priv->wrap);
1191 case PROP_WRAP_MODE:
1192 g_value_set_enum (value, priv->wrap_mode);
1194 case PROP_SELECTABLE:
1195 g_value_set_boolean (value, gtk_label_get_selectable (label));
1197 case PROP_MNEMONIC_KEYVAL:
1198 g_value_set_uint (value, priv->mnemonic_keyval);
1200 case PROP_MNEMONIC_WIDGET:
1201 g_value_set_object (value, (GObject*) priv->mnemonic_widget);
1203 case PROP_CURSOR_POSITION:
1204 if (priv->select_info && priv->select_info->selectable)
1206 gint offset = g_utf8_pointer_to_offset (priv->text,
1207 priv->text + priv->select_info->selection_end);
1208 g_value_set_int (value, offset);
1211 g_value_set_int (value, 0);
1213 case PROP_SELECTION_BOUND:
1214 if (priv->select_info && priv->select_info->selectable)
1216 gint offset = g_utf8_pointer_to_offset (priv->text,
1217 priv->text + priv->select_info->selection_anchor);
1218 g_value_set_int (value, offset);
1221 g_value_set_int (value, 0);
1223 case PROP_ELLIPSIZE:
1224 g_value_set_enum (value, priv->ellipsize);
1226 case PROP_WIDTH_CHARS:
1227 g_value_set_int (value, gtk_label_get_width_chars (label));
1229 case PROP_SINGLE_LINE_MODE:
1230 g_value_set_boolean (value, gtk_label_get_single_line_mode (label));
1233 g_value_set_double (value, gtk_label_get_angle (label));
1235 case PROP_MAX_WIDTH_CHARS:
1236 g_value_set_int (value, gtk_label_get_max_width_chars (label));
1238 case PROP_TRACK_VISITED_LINKS:
1239 g_value_set_boolean (value, gtk_label_get_track_visited_links (label));
1242 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1248 gtk_label_init (GtkLabel *label)
1250 GtkLabelPrivate *priv;
1252 label->priv = G_TYPE_INSTANCE_GET_PRIVATE (label,
1257 gtk_widget_set_has_window (GTK_WIDGET (label), FALSE);
1259 priv->width_chars = -1;
1260 priv->max_width_chars = -1;
1263 priv->jtype = GTK_JUSTIFY_LEFT;
1265 priv->wrap_mode = PANGO_WRAP_WORD;
1266 priv->ellipsize = PANGO_ELLIPSIZE_NONE;
1268 priv->use_underline = FALSE;
1269 priv->use_markup = FALSE;
1270 priv->pattern_set = FALSE;
1271 priv->track_links = TRUE;
1273 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
1274 priv->layout = NULL;
1278 priv->mnemonic_widget = NULL;
1279 priv->mnemonic_window = NULL;
1281 priv->mnemonics_visible = TRUE;
1283 gtk_label_set_text (label, "");
1288 gtk_label_buildable_interface_init (GtkBuildableIface *iface)
1290 buildable_parent_iface = g_type_interface_peek_parent (iface);
1292 iface->custom_tag_start = gtk_label_buildable_custom_tag_start;
1293 iface->custom_finished = gtk_label_buildable_custom_finished;
1297 GtkBuilder *builder;
1299 PangoAttrList *attrs;
1302 static PangoAttribute *
1303 attribute_from_text (GtkBuilder *builder,
1308 PangoAttribute *attribute = NULL;
1310 PangoLanguage *language;
1311 PangoFontDescription *font_desc;
1313 GValue val = { 0, };
1315 if (!gtk_builder_value_from_string_type (builder, PANGO_TYPE_ATTR_TYPE, name, &val, error))
1318 type = g_value_get_enum (&val);
1319 g_value_unset (&val);
1323 /* PangoAttrLanguage */
1324 case PANGO_ATTR_LANGUAGE:
1325 if ((language = pango_language_from_string (value)))
1327 attribute = pango_attr_language_new (language);
1328 g_value_init (&val, G_TYPE_INT);
1332 case PANGO_ATTR_STYLE:
1333 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STYLE, value, &val, error))
1334 attribute = pango_attr_style_new (g_value_get_enum (&val));
1336 case PANGO_ATTR_WEIGHT:
1337 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_WEIGHT, value, &val, error))
1338 attribute = pango_attr_weight_new (g_value_get_enum (&val));
1340 case PANGO_ATTR_VARIANT:
1341 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_VARIANT, value, &val, error))
1342 attribute = pango_attr_variant_new (g_value_get_enum (&val));
1344 case PANGO_ATTR_STRETCH:
1345 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STRETCH, value, &val, error))
1346 attribute = pango_attr_stretch_new (g_value_get_enum (&val));
1348 case PANGO_ATTR_UNDERLINE:
1349 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1350 attribute = pango_attr_underline_new (g_value_get_boolean (&val));
1352 case PANGO_ATTR_STRIKETHROUGH:
1353 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1354 attribute = pango_attr_strikethrough_new (g_value_get_boolean (&val));
1356 case PANGO_ATTR_GRAVITY:
1357 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY, value, &val, error))
1358 attribute = pango_attr_gravity_new (g_value_get_enum (&val));
1360 case PANGO_ATTR_GRAVITY_HINT:
1361 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY_HINT,
1362 value, &val, error))
1363 attribute = pango_attr_gravity_hint_new (g_value_get_enum (&val));
1365 /* PangoAttrString */
1366 case PANGO_ATTR_FAMILY:
1367 attribute = pango_attr_family_new (value);
1368 g_value_init (&val, G_TYPE_INT);
1372 case PANGO_ATTR_SIZE:
1373 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1374 value, &val, error))
1375 attribute = pango_attr_size_new (g_value_get_int (&val));
1377 case PANGO_ATTR_ABSOLUTE_SIZE:
1378 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1379 value, &val, error))
1380 attribute = pango_attr_size_new_absolute (g_value_get_int (&val));
1383 /* PangoAttrFontDesc */
1384 case PANGO_ATTR_FONT_DESC:
1385 if ((font_desc = pango_font_description_from_string (value)))
1387 attribute = pango_attr_font_desc_new (font_desc);
1388 pango_font_description_free (font_desc);
1389 g_value_init (&val, G_TYPE_INT);
1393 /* PangoAttrColor */
1394 case PANGO_ATTR_FOREGROUND:
1395 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1396 value, &val, error))
1398 color = g_value_get_boxed (&val);
1399 attribute = pango_attr_foreground_new (color->red, color->green, color->blue);
1402 case PANGO_ATTR_BACKGROUND:
1403 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1404 value, &val, error))
1406 color = g_value_get_boxed (&val);
1407 attribute = pango_attr_background_new (color->red, color->green, color->blue);
1410 case PANGO_ATTR_UNDERLINE_COLOR:
1411 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1412 value, &val, error))
1414 color = g_value_get_boxed (&val);
1415 attribute = pango_attr_underline_color_new (color->red, color->green, color->blue);
1418 case PANGO_ATTR_STRIKETHROUGH_COLOR:
1419 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1420 value, &val, error))
1422 color = g_value_get_boxed (&val);
1423 attribute = pango_attr_strikethrough_color_new (color->red, color->green, color->blue);
1427 /* PangoAttrShape */
1428 case PANGO_ATTR_SHAPE:
1429 /* Unsupported for now */
1431 /* PangoAttrFloat */
1432 case PANGO_ATTR_SCALE:
1433 if (gtk_builder_value_from_string_type (builder, G_TYPE_DOUBLE,
1434 value, &val, error))
1435 attribute = pango_attr_scale_new (g_value_get_double (&val));
1438 case PANGO_ATTR_INVALID:
1439 case PANGO_ATTR_LETTER_SPACING:
1440 case PANGO_ATTR_RISE:
1441 case PANGO_ATTR_FALLBACK:
1446 g_value_unset (&val);
1453 pango_start_element (GMarkupParseContext *context,
1454 const gchar *element_name,
1455 const gchar **names,
1456 const gchar **values,
1460 PangoParserData *data = (PangoParserData*)user_data;
1461 GValue val = { 0, };
1463 gint line_number, char_number;
1465 if (strcmp (element_name, "attribute") == 0)
1467 PangoAttribute *attr = NULL;
1468 const gchar *name = NULL;
1469 const gchar *value = NULL;
1470 const gchar *start = NULL;
1471 const gchar *end = NULL;
1472 guint start_val = 0;
1473 guint end_val = G_MAXUINT;
1475 for (i = 0; names[i]; i++)
1477 if (strcmp (names[i], "name") == 0)
1479 else if (strcmp (names[i], "value") == 0)
1481 else if (strcmp (names[i], "start") == 0)
1483 else if (strcmp (names[i], "end") == 0)
1487 g_markup_parse_context_get_position (context,
1492 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1493 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1495 line_number, char_number, names[i], "attribute");
1500 if (!name || !value)
1502 g_markup_parse_context_get_position (context,
1507 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1508 "%s:%d:%d <%s> requires attribute \"%s\"",
1510 line_number, char_number, "attribute",
1511 name ? "value" : "name");
1517 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1518 start, &val, error))
1520 start_val = g_value_get_uint (&val);
1521 g_value_unset (&val);
1526 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1529 end_val = g_value_get_uint (&val);
1530 g_value_unset (&val);
1533 attr = attribute_from_text (data->builder, name, value, error);
1534 attr->start_index = start_val;
1535 attr->end_index = end_val;
1540 data->attrs = pango_attr_list_new ();
1542 pango_attr_list_insert (data->attrs, attr);
1545 else if (strcmp (element_name, "attributes") == 0)
1548 g_warning ("Unsupported tag for GtkLabel: %s\n", element_name);
1551 static const GMarkupParser pango_parser =
1553 pango_start_element,
1557 gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
1558 GtkBuilder *builder,
1560 const gchar *tagname,
1561 GMarkupParser *parser,
1564 if (buildable_parent_iface->custom_tag_start (buildable, builder, child,
1565 tagname, parser, data))
1568 if (strcmp (tagname, "attributes") == 0)
1570 PangoParserData *parser_data;
1572 parser_data = g_slice_new0 (PangoParserData);
1573 parser_data->builder = g_object_ref (builder);
1574 parser_data->object = g_object_ref (buildable);
1575 *parser = pango_parser;
1576 *data = parser_data;
1583 gtk_label_buildable_custom_finished (GtkBuildable *buildable,
1584 GtkBuilder *builder,
1586 const gchar *tagname,
1589 PangoParserData *data;
1591 buildable_parent_iface->custom_finished (buildable, builder, child,
1592 tagname, user_data);
1594 if (strcmp (tagname, "attributes") == 0)
1596 data = (PangoParserData*)user_data;
1600 gtk_label_set_attributes (GTK_LABEL (buildable), data->attrs);
1601 pango_attr_list_unref (data->attrs);
1604 g_object_unref (data->object);
1605 g_object_unref (data->builder);
1606 g_slice_free (PangoParserData, data);
1613 * @str: The text of the label
1615 * Creates a new label with the given text inside it. You can
1616 * pass %NULL to get an empty label widget.
1618 * Return value: the new #GtkLabel
1621 gtk_label_new (const gchar *str)
1625 label = g_object_new (GTK_TYPE_LABEL, NULL);
1628 gtk_label_set_text (label, str);
1630 return GTK_WIDGET (label);
1634 * gtk_label_new_with_mnemonic:
1635 * @str: The text of the label, with an underscore in front of the
1636 * mnemonic character
1638 * Creates a new #GtkLabel, containing the text in @str.
1640 * If characters in @str are preceded by an underscore, they are
1641 * underlined. If you need a literal underscore character in a label, use
1642 * '__' (two underscores). The first underlined character represents a
1643 * keyboard accelerator called a mnemonic. The mnemonic key can be used
1644 * to activate another widget, chosen automatically, or explicitly using
1645 * gtk_label_set_mnemonic_widget().
1647 * If gtk_label_set_mnemonic_widget() is not called, then the first
1648 * activatable ancestor of the #GtkLabel will be chosen as the mnemonic
1649 * widget. For instance, if the label is inside a button or menu item,
1650 * the button or menu item will automatically become the mnemonic widget
1651 * and be activated by the mnemonic.
1653 * Return value: the new #GtkLabel
1656 gtk_label_new_with_mnemonic (const gchar *str)
1660 label = g_object_new (GTK_TYPE_LABEL, NULL);
1663 gtk_label_set_text_with_mnemonic (label, str);
1665 return GTK_WIDGET (label);
1669 gtk_label_mnemonic_activate (GtkWidget *widget,
1670 gboolean group_cycling)
1672 GtkLabel *label = GTK_LABEL (widget);
1673 GtkLabelPrivate *priv = label->priv;
1676 if (priv->mnemonic_widget)
1677 return gtk_widget_mnemonic_activate (priv->mnemonic_widget, group_cycling);
1679 /* Try to find the widget to activate by traversing the
1680 * widget's ancestry.
1682 parent = gtk_widget_get_parent (widget);
1684 if (GTK_IS_NOTEBOOK (parent))
1689 if (gtk_widget_get_can_focus (parent) ||
1690 (!group_cycling && GTK_WIDGET_GET_CLASS (parent)->activate_signal) ||
1691 GTK_IS_NOTEBOOK (gtk_widget_get_parent (parent)) ||
1692 GTK_IS_MENU_ITEM (parent))
1693 return gtk_widget_mnemonic_activate (parent, group_cycling);
1694 parent = gtk_widget_get_parent (parent);
1697 /* barf if there was nothing to activate */
1698 g_warning ("Couldn't find a target for a mnemonic activation.");
1699 gtk_widget_error_bell (widget);
1705 gtk_label_setup_mnemonic (GtkLabel *label,
1708 GtkLabelPrivate *priv = label->priv;
1709 GtkWidget *widget = GTK_WIDGET (label);
1710 GtkWidget *toplevel;
1711 GtkWidget *mnemonic_menu;
1713 mnemonic_menu = g_object_get_data (G_OBJECT (label), "gtk-mnemonic-menu");
1715 if (last_key != GDK_KEY_VoidSymbol)
1717 if (priv->mnemonic_window)
1719 gtk_window_remove_mnemonic (priv->mnemonic_window,
1722 priv->mnemonic_window = NULL;
1726 _gtk_menu_shell_remove_mnemonic (GTK_MENU_SHELL (mnemonic_menu),
1729 mnemonic_menu = NULL;
1733 if (priv->mnemonic_keyval == GDK_KEY_VoidSymbol)
1736 connect_mnemonics_visible_notify (GTK_LABEL (widget));
1738 toplevel = gtk_widget_get_toplevel (widget);
1739 if (gtk_widget_is_toplevel (toplevel))
1741 GtkWidget *menu_shell;
1743 menu_shell = gtk_widget_get_ancestor (widget,
1744 GTK_TYPE_MENU_SHELL);
1748 _gtk_menu_shell_add_mnemonic (GTK_MENU_SHELL (menu_shell),
1749 priv->mnemonic_keyval,
1751 mnemonic_menu = menu_shell;
1754 if (!GTK_IS_MENU (menu_shell))
1756 gtk_window_add_mnemonic (GTK_WINDOW (toplevel),
1757 priv->mnemonic_keyval,
1759 priv->mnemonic_window = GTK_WINDOW (toplevel);
1764 g_object_set_data (G_OBJECT (label), I_("gtk-mnemonic-menu"), mnemonic_menu);
1768 gtk_label_hierarchy_changed (GtkWidget *widget,
1769 GtkWidget *old_toplevel)
1771 GtkLabel *label = GTK_LABEL (widget);
1772 GtkLabelPrivate *priv = label->priv;
1774 gtk_label_setup_mnemonic (label, priv->mnemonic_keyval);
1778 label_shortcut_setting_apply (GtkLabel *label)
1780 gtk_label_recalculate (label);
1781 if (GTK_IS_ACCEL_LABEL (label))
1782 gtk_accel_label_refetch (GTK_ACCEL_LABEL (label));
1786 label_shortcut_setting_traverse_container (GtkWidget *widget,
1789 if (GTK_IS_LABEL (widget))
1790 label_shortcut_setting_apply (GTK_LABEL (widget));
1791 else if (GTK_IS_CONTAINER (widget))
1792 gtk_container_forall (GTK_CONTAINER (widget),
1793 label_shortcut_setting_traverse_container, data);
1797 label_shortcut_setting_changed (GtkSettings *settings)
1801 list = gtk_window_list_toplevels ();
1803 for (l = list; l ; l = l->next)
1805 GtkWidget *widget = l->data;
1807 if (gtk_widget_get_settings (widget) == settings)
1808 gtk_container_forall (GTK_CONTAINER (widget),
1809 label_shortcut_setting_traverse_container, NULL);
1816 mnemonics_visible_apply (GtkWidget *widget,
1817 gboolean mnemonics_visible)
1819 GtkLabel *label = GTK_LABEL (widget);
1820 GtkLabelPrivate *priv = label->priv;
1822 mnemonics_visible = mnemonics_visible != FALSE;
1824 if (priv->mnemonics_visible != mnemonics_visible)
1826 priv->mnemonics_visible = mnemonics_visible;
1828 gtk_label_recalculate (label);
1833 label_mnemonics_visible_traverse_container (GtkWidget *widget,
1836 gboolean mnemonics_visible = GPOINTER_TO_INT (data);
1838 _gtk_label_mnemonics_visible_apply_recursively (widget, mnemonics_visible);
1842 _gtk_label_mnemonics_visible_apply_recursively (GtkWidget *widget,
1843 gboolean mnemonics_visible)
1845 if (GTK_IS_LABEL (widget))
1846 mnemonics_visible_apply (widget, mnemonics_visible);
1847 else if (GTK_IS_CONTAINER (widget))
1848 gtk_container_forall (GTK_CONTAINER (widget),
1849 label_mnemonics_visible_traverse_container,
1850 GINT_TO_POINTER (mnemonics_visible));
1854 label_mnemonics_visible_changed (GtkWindow *window,
1858 gboolean mnemonics_visible;
1860 g_object_get (window, "mnemonics-visible", &mnemonics_visible, NULL);
1862 gtk_container_forall (GTK_CONTAINER (window),
1863 label_mnemonics_visible_traverse_container,
1864 GINT_TO_POINTER (mnemonics_visible));
1868 gtk_label_screen_changed (GtkWidget *widget,
1869 GdkScreen *old_screen)
1871 GtkSettings *settings;
1872 gboolean shortcuts_connected;
1874 if (!gtk_widget_has_screen (widget))
1877 settings = gtk_widget_get_settings (widget);
1879 shortcuts_connected =
1880 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (settings),
1881 "gtk-label-shortcuts-connected"));
1883 if (! shortcuts_connected)
1885 g_signal_connect (settings, "notify::gtk-enable-mnemonics",
1886 G_CALLBACK (label_shortcut_setting_changed),
1888 g_signal_connect (settings, "notify::gtk-enable-accels",
1889 G_CALLBACK (label_shortcut_setting_changed),
1892 g_object_set_data (G_OBJECT (settings), "gtk-label-shortcuts-connected",
1893 GINT_TO_POINTER (TRUE));
1896 label_shortcut_setting_apply (GTK_LABEL (widget));
1901 label_mnemonic_widget_weak_notify (gpointer data,
1902 GObject *where_the_object_was)
1904 GtkLabel *label = data;
1905 GtkLabelPrivate *priv = label->priv;
1907 priv->mnemonic_widget = NULL;
1908 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1912 * gtk_label_set_mnemonic_widget:
1913 * @label: a #GtkLabel
1914 * @widget: (allow-none): the target #GtkWidget
1916 * If the label has been set so that it has an mnemonic key (using
1917 * i.e. gtk_label_set_markup_with_mnemonic(),
1918 * gtk_label_set_text_with_mnemonic(), gtk_label_new_with_mnemonic()
1919 * or the "use_underline" property) the label can be associated with a
1920 * widget that is the target of the mnemonic. When the label is inside
1921 * a widget (like a #GtkButton or a #GtkNotebook tab) it is
1922 * automatically associated with the correct widget, but sometimes
1923 * (i.e. when the target is a #GtkEntry next to the label) you need to
1924 * set it explicitly using this function.
1926 * The target widget will be accelerated by emitting the
1927 * GtkWidget::mnemonic-activate signal on it. The default handler for
1928 * this signal will activate the widget if there are no mnemonic collisions
1929 * and toggle focus between the colliding widgets otherwise.
1932 gtk_label_set_mnemonic_widget (GtkLabel *label,
1935 GtkLabelPrivate *priv;
1937 g_return_if_fail (GTK_IS_LABEL (label));
1942 g_return_if_fail (GTK_IS_WIDGET (widget));
1944 if (priv->mnemonic_widget)
1946 gtk_widget_remove_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1947 g_object_weak_unref (G_OBJECT (priv->mnemonic_widget),
1948 label_mnemonic_widget_weak_notify,
1951 priv->mnemonic_widget = widget;
1952 if (priv->mnemonic_widget)
1954 g_object_weak_ref (G_OBJECT (priv->mnemonic_widget),
1955 label_mnemonic_widget_weak_notify,
1957 gtk_widget_add_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1960 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1964 * gtk_label_get_mnemonic_widget:
1965 * @label: a #GtkLabel
1967 * Retrieves the target of the mnemonic (keyboard shortcut) of this
1968 * label. See gtk_label_set_mnemonic_widget().
1970 * Return value: (transfer none): the target of the label's mnemonic,
1971 * or %NULL if none has been set and the default algorithm will be used.
1974 gtk_label_get_mnemonic_widget (GtkLabel *label)
1976 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1978 return label->priv->mnemonic_widget;
1982 * gtk_label_get_mnemonic_keyval:
1983 * @label: a #GtkLabel
1985 * If the label has been set so that it has an mnemonic key this function
1986 * returns the keyval used for the mnemonic accelerator. If there is no
1987 * mnemonic set up it returns #GDK_VoidSymbol.
1989 * Returns: GDK keyval usable for accelerators, or #GDK_VoidSymbol
1992 gtk_label_get_mnemonic_keyval (GtkLabel *label)
1994 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_KEY_VoidSymbol);
1996 return label->priv->mnemonic_keyval;
2000 gtk_label_set_text_internal (GtkLabel *label,
2003 GtkLabelPrivate *priv = label->priv;
2005 g_free (priv->text);
2009 gtk_label_select_region_index (label, 0, 0);
2013 gtk_label_set_label_internal (GtkLabel *label,
2016 GtkLabelPrivate *priv = label->priv;
2018 g_free (priv->label);
2022 g_object_notify (G_OBJECT (label), "label");
2026 gtk_label_set_use_markup_internal (GtkLabel *label,
2029 GtkLabelPrivate *priv = label->priv;
2032 if (priv->use_markup != val)
2034 priv->use_markup = val;
2036 g_object_notify (G_OBJECT (label), "use-markup");
2041 gtk_label_set_use_underline_internal (GtkLabel *label,
2044 GtkLabelPrivate *priv = label->priv;
2047 if (priv->use_underline != val)
2049 priv->use_underline = val;
2051 g_object_notify (G_OBJECT (label), "use-underline");
2056 gtk_label_compose_effective_attrs (GtkLabel *label)
2058 GtkLabelPrivate *priv = label->priv;
2059 PangoAttrIterator *iter;
2060 PangoAttribute *attr;
2061 GSList *iter_attrs, *l;
2065 if (priv->effective_attrs)
2067 if ((iter = pango_attr_list_get_iterator (priv->attrs)))
2071 iter_attrs = pango_attr_iterator_get_attrs (iter);
2072 for (l = iter_attrs; l; l = l->next)
2075 pango_attr_list_insert (priv->effective_attrs, attr);
2077 g_slist_free (iter_attrs);
2079 while (pango_attr_iterator_next (iter));
2080 pango_attr_iterator_destroy (iter);
2084 priv->effective_attrs =
2085 pango_attr_list_ref (priv->attrs);
2090 gtk_label_set_attributes_internal (GtkLabel *label,
2091 PangoAttrList *attrs)
2093 GtkLabelPrivate *priv = label->priv;
2096 pango_attr_list_ref (attrs);
2099 pango_attr_list_unref (priv->attrs);
2100 priv->attrs = attrs;
2102 g_object_notify (G_OBJECT (label), "attributes");
2106 /* Calculates text, attrs and mnemonic_keyval from
2107 * label, use_underline and use_markup
2110 gtk_label_recalculate (GtkLabel *label)
2112 GtkLabelPrivate *priv = label->priv;
2113 guint keyval = priv->mnemonic_keyval;
2115 if (priv->use_markup)
2116 gtk_label_set_markup_internal (label, priv->label, priv->use_underline);
2119 if (priv->use_underline)
2120 gtk_label_set_uline_text_internal (label, priv->label);
2123 if (priv->effective_attrs)
2124 pango_attr_list_unref (priv->effective_attrs);
2125 priv->effective_attrs = NULL;
2126 gtk_label_set_text_internal (label, g_strdup (priv->label));
2130 gtk_label_compose_effective_attrs (label);
2132 if (!priv->use_underline)
2133 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
2135 if (keyval != priv->mnemonic_keyval)
2137 gtk_label_setup_mnemonic (label, keyval);
2138 g_object_notify (G_OBJECT (label), "mnemonic-keyval");
2141 gtk_label_clear_layout (label);
2142 gtk_label_clear_select_info (label);
2143 gtk_widget_queue_resize (GTK_WIDGET (label));
2147 * gtk_label_set_text:
2148 * @label: a #GtkLabel
2149 * @str: The text you want to set
2151 * Sets the text within the #GtkLabel widget. It overwrites any text that
2154 * This will also clear any previously set mnemonic accelerators.
2157 gtk_label_set_text (GtkLabel *label,
2160 g_return_if_fail (GTK_IS_LABEL (label));
2162 g_object_freeze_notify (G_OBJECT (label));
2164 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2165 gtk_label_set_use_markup_internal (label, FALSE);
2166 gtk_label_set_use_underline_internal (label, FALSE);
2168 gtk_label_recalculate (label);
2170 g_object_thaw_notify (G_OBJECT (label));
2174 * gtk_label_set_attributes:
2175 * @label: a #GtkLabel
2176 * @attrs: a #PangoAttrList
2178 * Sets a #PangoAttrList; the attributes in the list are applied to the
2181 * <note><para>The attributes set with this function will be applied
2182 * and merged with any other attributes previously effected by way
2183 * of the #GtkLabel:use-underline or #GtkLabel:use-markup properties.
2184 * While it is not recommended to mix markup strings with manually set
2185 * attributes, if you must; know that the attributes will be applied
2186 * to the label after the markup string is parsed.</para></note>
2189 gtk_label_set_attributes (GtkLabel *label,
2190 PangoAttrList *attrs)
2192 g_return_if_fail (GTK_IS_LABEL (label));
2194 gtk_label_set_attributes_internal (label, attrs);
2196 gtk_label_recalculate (label);
2198 gtk_label_clear_layout (label);
2199 gtk_widget_queue_resize (GTK_WIDGET (label));
2203 * gtk_label_get_attributes:
2204 * @label: a #GtkLabel
2206 * Gets the attribute list that was set on the label using
2207 * gtk_label_set_attributes(), if any. This function does
2208 * not reflect attributes that come from the labels markup
2209 * (see gtk_label_set_markup()). If you want to get the
2210 * effective attributes for the label, use
2211 * pango_layout_get_attribute (gtk_label_get_layout (label)).
2213 * Return value: (transfer none): the attribute list, or %NULL
2217 gtk_label_get_attributes (GtkLabel *label)
2219 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2221 return label->priv->attrs;
2225 * gtk_label_set_label:
2226 * @label: a #GtkLabel
2227 * @str: the new text to set for the label
2229 * Sets the text of the label. The label is interpreted as
2230 * including embedded underlines and/or Pango markup depending
2231 * on the values of the #GtkLabel:use-underline" and
2232 * #GtkLabel:use-markup properties.
2235 gtk_label_set_label (GtkLabel *label,
2238 g_return_if_fail (GTK_IS_LABEL (label));
2240 g_object_freeze_notify (G_OBJECT (label));
2242 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2243 gtk_label_recalculate (label);
2245 g_object_thaw_notify (G_OBJECT (label));
2249 * gtk_label_get_label:
2250 * @label: a #GtkLabel
2252 * Fetches the text from a label widget including any embedded
2253 * underlines indicating mnemonics and Pango markup. (See
2254 * gtk_label_get_text()).
2256 * Return value: the text of the label widget. This string is
2257 * owned by the widget and must not be modified or freed.
2259 G_CONST_RETURN gchar *
2260 gtk_label_get_label (GtkLabel *label)
2262 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2264 return label->priv->label;
2272 GdkColor *link_color;
2273 GdkColor *visited_link_color;
2277 start_element_handler (GMarkupParseContext *context,
2278 const gchar *element_name,
2279 const gchar **attribute_names,
2280 const gchar **attribute_values,
2284 GtkLabelPrivate *priv;
2285 UriParserData *pdata = user_data;
2287 if (strcmp (element_name, "a") == 0)
2290 const gchar *uri = NULL;
2291 const gchar *title = NULL;
2292 gboolean visited = FALSE;
2296 GdkColor *color = NULL;
2298 g_markup_parse_context_get_position (context, &line_number, &char_number);
2300 for (i = 0; attribute_names[i] != NULL; i++)
2302 const gchar *attr = attribute_names[i];
2304 if (strcmp (attr, "href") == 0)
2305 uri = attribute_values[i];
2306 else if (strcmp (attr, "title") == 0)
2307 title = attribute_values[i];
2312 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
2313 "Attribute '%s' is not allowed on the <a> tag "
2314 "on line %d char %d",
2315 attr, line_number, char_number);
2324 G_MARKUP_ERROR_INVALID_CONTENT,
2325 "Attribute 'href' was missing on the <a> tag "
2326 "on line %d char %d",
2327 line_number, char_number);
2332 priv = pdata->label->priv;
2333 if (priv->track_links && priv->select_info)
2336 for (l = priv->select_info->links; l; l = l->next)
2339 if (strcmp (uri, link->uri) == 0)
2341 visited = link->visited;
2348 color = pdata->visited_link_color;
2350 color = pdata->link_color;
2352 g_string_append_printf (pdata->new_str,
2353 "<span color=\"#%04x%04x%04x\" underline=\"single\">",
2358 link = g_new0 (GtkLabelLink, 1);
2359 link->uri = g_strdup (uri);
2360 link->title = g_strdup (title);
2361 link->visited = visited;
2362 pdata->links = g_list_append (pdata->links, link);
2368 g_string_append_c (pdata->new_str, '<');
2369 g_string_append (pdata->new_str, element_name);
2371 for (i = 0; attribute_names[i] != NULL; i++)
2373 const gchar *attr = attribute_names[i];
2374 const gchar *value = attribute_values[i];
2377 newvalue = g_markup_escape_text (value, -1);
2379 g_string_append_c (pdata->new_str, ' ');
2380 g_string_append (pdata->new_str, attr);
2381 g_string_append (pdata->new_str, "=\"");
2382 g_string_append (pdata->new_str, newvalue);
2383 g_string_append_c (pdata->new_str, '\"');
2387 g_string_append_c (pdata->new_str, '>');
2392 end_element_handler (GMarkupParseContext *context,
2393 const gchar *element_name,
2397 UriParserData *pdata = user_data;
2399 if (!strcmp (element_name, "a"))
2400 g_string_append (pdata->new_str, "</span>");
2403 g_string_append (pdata->new_str, "</");
2404 g_string_append (pdata->new_str, element_name);
2405 g_string_append_c (pdata->new_str, '>');
2410 text_handler (GMarkupParseContext *context,
2416 UriParserData *pdata = user_data;
2419 newtext = g_markup_escape_text (text, text_len);
2420 g_string_append (pdata->new_str, newtext);
2424 static const GMarkupParser markup_parser =
2426 start_element_handler,
2427 end_element_handler,
2434 xml_isspace (gchar c)
2436 return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
2440 link_free (GtkLabelLink *link)
2443 g_free (link->title);
2448 gtk_label_get_link_colors (GtkWidget *widget,
2449 GdkColor **link_color,
2450 GdkColor **visited_link_color)
2452 GtkStyleContext *context;
2454 context = gtk_widget_get_style_context (widget);
2455 gtk_style_context_get_style (context,
2456 "link-color", link_color,
2457 "visited-link-color", visited_link_color,
2460 *link_color = gdk_color_copy (&default_link_color);
2461 if (!*visited_link_color)
2462 *visited_link_color = gdk_color_copy (&default_visited_link_color);
2466 parse_uri_markup (GtkLabel *label,
2472 GMarkupParseContext *context = NULL;
2473 const gchar *p, *end;
2474 gboolean needs_root = TRUE;
2476 UriParserData pdata;
2478 length = strlen (str);
2482 pdata.label = label;
2484 pdata.new_str = g_string_sized_new (length);
2486 gtk_label_get_link_colors (GTK_WIDGET (label), &pdata.link_color, &pdata.visited_link_color);
2488 while (p != end && xml_isspace (*p))
2491 if (end - p >= 8 && strncmp (p, "<markup>", 8) == 0)
2494 context = g_markup_parse_context_new (&markup_parser, 0, &pdata, NULL);
2498 if (!g_markup_parse_context_parse (context, "<markup>", -1, error))
2502 if (!g_markup_parse_context_parse (context, str, length, error))
2507 if (!g_markup_parse_context_parse (context, "</markup>", -1, error))
2511 if (!g_markup_parse_context_end_parse (context, error))
2514 g_markup_parse_context_free (context);
2516 *new_str = g_string_free (pdata.new_str, FALSE);
2517 *links = pdata.links;
2519 gdk_color_free (pdata.link_color);
2520 gdk_color_free (pdata.visited_link_color);
2525 g_markup_parse_context_free (context);
2526 g_string_free (pdata.new_str, TRUE);
2527 g_list_foreach (pdata.links, (GFunc)link_free, NULL);
2528 g_list_free (pdata.links);
2529 gdk_color_free (pdata.link_color);
2530 gdk_color_free (pdata.visited_link_color);
2536 gtk_label_ensure_has_tooltip (GtkLabel *label)
2538 GtkLabelPrivate *priv = label->priv;
2540 gboolean has_tooltip = FALSE;
2542 for (l = priv->select_info->links; l; l = l->next)
2544 GtkLabelLink *link = l->data;
2552 gtk_widget_set_has_tooltip (GTK_WIDGET (label), has_tooltip);
2556 gtk_label_set_markup_internal (GtkLabel *label,
2558 gboolean with_uline)
2560 GtkLabelPrivate *priv = label->priv;
2562 GError *error = NULL;
2563 PangoAttrList *attrs = NULL;
2564 gunichar accel_char = 0;
2566 GList *links = NULL;
2568 if (!parse_uri_markup (label, str, &new_str, &links, &error))
2570 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2572 g_error_free (error);
2576 gtk_label_clear_links (label);
2579 gtk_label_ensure_select_info (label);
2580 priv->select_info->links = links;
2581 gtk_label_ensure_has_tooltip (label);
2586 gboolean enable_mnemonics;
2587 gboolean auto_mnemonics;
2589 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2590 "gtk-enable-mnemonics", &enable_mnemonics,
2591 "gtk-auto-mnemonics", &auto_mnemonics,
2594 if (!(enable_mnemonics && priv->mnemonics_visible &&
2596 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2597 (!priv->mnemonic_widget ||
2598 gtk_widget_is_sensitive (priv->mnemonic_widget))))))
2604 if (separate_uline_pattern (new_str, &key, &tmp, &pattern))
2613 if (!pango_parse_markup (new_str,
2615 with_uline ? '_' : 0,
2618 with_uline ? &accel_char : NULL,
2621 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2624 g_error_free (error);
2631 gtk_label_set_text_internal (label, text);
2635 if (priv->effective_attrs)
2636 pango_attr_list_unref (priv->effective_attrs);
2637 priv->effective_attrs = attrs;
2640 if (accel_char != 0)
2641 priv->mnemonic_keyval = gdk_keyval_to_lower (gdk_unicode_to_keyval (accel_char));
2643 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
2647 * gtk_label_set_markup:
2648 * @label: a #GtkLabel
2649 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
2651 * Parses @str which is marked up with the <link
2652 * linkend="PangoMarkupFormat">Pango text markup language</link>, setting the
2653 * label's text and attribute list based on the parse results. If the @str is
2654 * external data, you may need to escape it with g_markup_escape_text() or
2655 * g_markup_printf_escaped()<!-- -->:
2659 * markup = g_markup_printf_escaped ("<span style=\"italic\">%s</span>", str);
2660 * gtk_label_set_markup (GTK_LABEL (label), markup);
2665 gtk_label_set_markup (GtkLabel *label,
2668 g_return_if_fail (GTK_IS_LABEL (label));
2670 g_object_freeze_notify (G_OBJECT (label));
2672 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2673 gtk_label_set_use_markup_internal (label, TRUE);
2674 gtk_label_set_use_underline_internal (label, FALSE);
2676 gtk_label_recalculate (label);
2678 g_object_thaw_notify (G_OBJECT (label));
2682 * gtk_label_set_markup_with_mnemonic:
2683 * @label: a #GtkLabel
2684 * @str: a markup string (see
2685 * <link linkend="PangoMarkupFormat">Pango markup format</link>)
2687 * Parses @str which is marked up with the
2688 * <link linkend="PangoMarkupFormat">Pango text markup language</link>,
2689 * setting the label's text and attribute list based on the parse results.
2690 * If characters in @str are preceded by an underscore, they are underlined
2691 * indicating that they represent a keyboard accelerator called a mnemonic.
2693 * The mnemonic key can be used to activate another widget, chosen
2694 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
2697 gtk_label_set_markup_with_mnemonic (GtkLabel *label,
2700 g_return_if_fail (GTK_IS_LABEL (label));
2702 g_object_freeze_notify (G_OBJECT (label));
2704 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2705 gtk_label_set_use_markup_internal (label, TRUE);
2706 gtk_label_set_use_underline_internal (label, TRUE);
2708 gtk_label_recalculate (label);
2710 g_object_thaw_notify (G_OBJECT (label));
2714 * gtk_label_get_text:
2715 * @label: a #GtkLabel
2717 * Fetches the text from a label widget, as displayed on the
2718 * screen. This does not include any embedded underlines
2719 * indicating mnemonics or Pango markup. (See gtk_label_get_label())
2721 * Return value: the text in the label widget. This is the internal
2722 * string used by the label, and must not be modified.
2724 G_CONST_RETURN gchar *
2725 gtk_label_get_text (GtkLabel *label)
2727 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2729 return label->priv->text;
2732 static PangoAttrList *
2733 gtk_label_pattern_to_attrs (GtkLabel *label,
2734 const gchar *pattern)
2736 GtkLabelPrivate *priv = label->priv;
2738 const char *p = priv->text;
2739 const char *q = pattern;
2740 PangoAttrList *attrs;
2742 attrs = pango_attr_list_new ();
2746 while (*p && *q && *q != '_')
2748 p = g_utf8_next_char (p);
2752 while (*p && *q && *q == '_')
2754 p = g_utf8_next_char (p);
2760 PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_LOW);
2761 attr->start_index = start - priv->text;
2762 attr->end_index = p - priv->text;
2764 pango_attr_list_insert (attrs, attr);
2774 gtk_label_set_pattern_internal (GtkLabel *label,
2775 const gchar *pattern,
2776 gboolean is_mnemonic)
2778 GtkLabelPrivate *priv = label->priv;
2779 PangoAttrList *attrs;
2780 gboolean enable_mnemonics;
2781 gboolean auto_mnemonics;
2783 if (priv->pattern_set)
2788 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2789 "gtk-enable-mnemonics", &enable_mnemonics,
2790 "gtk-auto-mnemonics", &auto_mnemonics,
2793 if (enable_mnemonics && priv->mnemonics_visible && pattern &&
2795 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2796 (!priv->mnemonic_widget ||
2797 gtk_widget_is_sensitive (priv->mnemonic_widget)))))
2798 attrs = gtk_label_pattern_to_attrs (label, pattern);
2803 attrs = gtk_label_pattern_to_attrs (label, pattern);
2805 if (priv->effective_attrs)
2806 pango_attr_list_unref (priv->effective_attrs);
2807 priv->effective_attrs = attrs;
2811 * gtk_label_set_pattern:
2812 * @label: The #GtkLabel you want to set the pattern to.
2813 * @pattern: The pattern as described above.
2815 * The pattern of underlines you want under the existing text within the
2816 * #GtkLabel widget. For example if the current text of the label says
2817 * "FooBarBaz" passing a pattern of "___ ___" will underline
2818 * "Foo" and "Baz" but not "Bar".
2821 gtk_label_set_pattern (GtkLabel *label,
2822 const gchar *pattern)
2824 GtkLabelPrivate *priv = label->priv;
2826 g_return_if_fail (GTK_IS_LABEL (label));
2830 priv->pattern_set = FALSE;
2834 gtk_label_set_pattern_internal (label, pattern, FALSE);
2835 priv->pattern_set = TRUE;
2838 gtk_label_recalculate (label);
2840 gtk_label_clear_layout (label);
2841 gtk_widget_queue_resize (GTK_WIDGET (label));
2846 * gtk_label_set_justify:
2847 * @label: a #GtkLabel
2848 * @jtype: a #GtkJustification
2850 * Sets the alignment of the lines in the text of the label relative to
2851 * each other. %GTK_JUSTIFY_LEFT is the default value when the
2852 * widget is first created with gtk_label_new(). If you instead want
2853 * to set the alignment of the label as a whole, use
2854 * gtk_misc_set_alignment() instead. gtk_label_set_justify() has no
2855 * effect on labels containing only a single line.
2858 gtk_label_set_justify (GtkLabel *label,
2859 GtkJustification jtype)
2861 GtkLabelPrivate *priv;
2863 g_return_if_fail (GTK_IS_LABEL (label));
2864 g_return_if_fail (jtype >= GTK_JUSTIFY_LEFT && jtype <= GTK_JUSTIFY_FILL);
2868 if ((GtkJustification) priv->jtype != jtype)
2870 priv->jtype = jtype;
2872 /* No real need to be this drastic, but easier than duplicating the code */
2873 gtk_label_clear_layout (label);
2875 g_object_notify (G_OBJECT (label), "justify");
2876 gtk_widget_queue_resize (GTK_WIDGET (label));
2881 * gtk_label_get_justify:
2882 * @label: a #GtkLabel
2884 * Returns the justification of the label. See gtk_label_set_justify().
2886 * Return value: #GtkJustification
2889 gtk_label_get_justify (GtkLabel *label)
2891 g_return_val_if_fail (GTK_IS_LABEL (label), 0);
2893 return label->priv->jtype;
2897 * gtk_label_set_ellipsize:
2898 * @label: a #GtkLabel
2899 * @mode: a #PangoEllipsizeMode
2901 * Sets the mode used to ellipsize (add an ellipsis: "...") to the text
2902 * if there is not enough space to render the entire string.
2907 gtk_label_set_ellipsize (GtkLabel *label,
2908 PangoEllipsizeMode mode)
2910 GtkLabelPrivate *priv;
2912 g_return_if_fail (GTK_IS_LABEL (label));
2913 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE && mode <= PANGO_ELLIPSIZE_END);
2917 if ((PangoEllipsizeMode) priv->ellipsize != mode)
2919 priv->ellipsize = mode;
2921 /* No real need to be this drastic, but easier than duplicating the code */
2922 gtk_label_clear_layout (label);
2924 g_object_notify (G_OBJECT (label), "ellipsize");
2925 gtk_widget_queue_resize (GTK_WIDGET (label));
2930 * gtk_label_get_ellipsize:
2931 * @label: a #GtkLabel
2933 * Returns the ellipsizing position of the label. See gtk_label_set_ellipsize().
2935 * Return value: #PangoEllipsizeMode
2940 gtk_label_get_ellipsize (GtkLabel *label)
2942 g_return_val_if_fail (GTK_IS_LABEL (label), PANGO_ELLIPSIZE_NONE);
2944 return label->priv->ellipsize;
2948 * gtk_label_set_width_chars:
2949 * @label: a #GtkLabel
2950 * @n_chars: the new desired width, in characters.
2952 * Sets the desired width in characters of @label to @n_chars.
2957 gtk_label_set_width_chars (GtkLabel *label,
2960 GtkLabelPrivate *priv;
2962 g_return_if_fail (GTK_IS_LABEL (label));
2966 if (priv->width_chars != n_chars)
2968 priv->width_chars = n_chars;
2969 g_object_notify (G_OBJECT (label), "width-chars");
2970 gtk_widget_queue_resize (GTK_WIDGET (label));
2975 * gtk_label_get_width_chars:
2976 * @label: a #GtkLabel
2978 * Retrieves the desired width of @label, in characters. See
2979 * gtk_label_set_width_chars().
2981 * Return value: the width of the label in characters.
2986 gtk_label_get_width_chars (GtkLabel *label)
2988 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2990 return label->priv->width_chars;
2994 * gtk_label_set_max_width_chars:
2995 * @label: a #GtkLabel
2996 * @n_chars: the new desired maximum width, in characters.
2998 * Sets the desired maximum width in characters of @label to @n_chars.
3003 gtk_label_set_max_width_chars (GtkLabel *label,
3006 GtkLabelPrivate *priv;
3008 g_return_if_fail (GTK_IS_LABEL (label));
3012 if (priv->max_width_chars != n_chars)
3014 priv->max_width_chars = n_chars;
3016 g_object_notify (G_OBJECT (label), "max-width-chars");
3017 gtk_widget_queue_resize (GTK_WIDGET (label));
3022 * gtk_label_get_max_width_chars:
3023 * @label: a #GtkLabel
3025 * Retrieves the desired maximum width of @label, in characters. See
3026 * gtk_label_set_width_chars().
3028 * Return value: the maximum width of the label in characters.
3033 gtk_label_get_max_width_chars (GtkLabel *label)
3035 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
3037 return label->priv->max_width_chars;
3041 * gtk_label_set_line_wrap:
3042 * @label: a #GtkLabel
3043 * @wrap: the setting
3045 * Toggles line wrapping within the #GtkLabel widget. %TRUE makes it break
3046 * lines if text exceeds the widget's size. %FALSE lets the text get cut off
3047 * by the edge of the widget if it exceeds the widget size.
3049 * Note that setting line wrapping to %TRUE does not make the label
3050 * wrap at its parent container's width, because GTK+ widgets
3051 * conceptually can't make their requisition depend on the parent
3052 * container's size. For a label that wraps at a specific position,
3053 * set the label's width using gtk_widget_set_size_request().
3056 gtk_label_set_line_wrap (GtkLabel *label,
3059 GtkLabelPrivate *priv;
3061 g_return_if_fail (GTK_IS_LABEL (label));
3065 wrap = wrap != FALSE;
3067 if (priv->wrap != wrap)
3071 gtk_label_clear_layout (label);
3072 gtk_widget_queue_resize (GTK_WIDGET (label));
3073 g_object_notify (G_OBJECT (label), "wrap");
3078 * gtk_label_get_line_wrap:
3079 * @label: a #GtkLabel
3081 * Returns whether lines in the label are automatically wrapped.
3082 * See gtk_label_set_line_wrap().
3084 * Return value: %TRUE if the lines of the label are automatically wrapped.
3087 gtk_label_get_line_wrap (GtkLabel *label)
3089 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
3091 return label->priv->wrap;
3095 * gtk_label_set_line_wrap_mode:
3096 * @label: a #GtkLabel
3097 * @wrap_mode: the line wrapping mode
3099 * If line wrapping is on (see gtk_label_set_line_wrap()) this controls how
3100 * the line wrapping is done. The default is %PANGO_WRAP_WORD which means
3101 * wrap on word boundaries.
3106 gtk_label_set_line_wrap_mode (GtkLabel *label,
3107 PangoWrapMode wrap_mode)
3109 GtkLabelPrivate *priv;
3111 g_return_if_fail (GTK_IS_LABEL (label));
3115 if (priv->wrap_mode != wrap_mode)
3117 priv->wrap_mode = wrap_mode;
3118 g_object_notify (G_OBJECT (label), "wrap-mode");
3120 gtk_widget_queue_resize (GTK_WIDGET (label));
3125 * gtk_label_get_line_wrap_mode:
3126 * @label: a #GtkLabel
3128 * Returns line wrap mode used by the label. See gtk_label_set_line_wrap_mode().
3130 * Return value: %TRUE if the lines of the label are automatically wrapped.
3135 gtk_label_get_line_wrap_mode (GtkLabel *label)
3137 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
3139 return label->priv->wrap_mode;
3143 gtk_label_destroy (GtkWidget *widget)
3145 GtkLabel *label = GTK_LABEL (widget);
3147 gtk_label_set_mnemonic_widget (label, NULL);
3149 GTK_WIDGET_CLASS (gtk_label_parent_class)->destroy (widget);
3153 gtk_label_finalize (GObject *object)
3155 GtkLabel *label = GTK_LABEL (object);
3156 GtkLabelPrivate *priv = label->priv;
3158 g_free (priv->label);
3159 g_free (priv->text);
3162 g_object_unref (priv->layout);
3165 pango_attr_list_unref (priv->attrs);
3167 if (priv->effective_attrs)
3168 pango_attr_list_unref (priv->effective_attrs);
3170 gtk_label_clear_links (label);
3171 g_free (priv->select_info);
3173 G_OBJECT_CLASS (gtk_label_parent_class)->finalize (object);
3177 gtk_label_clear_layout (GtkLabel *label)
3179 GtkLabelPrivate *priv = label->priv;
3183 g_object_unref (priv->layout);
3184 priv->layout = NULL;
3186 //gtk_label_clear_links (label);
3190 static PangoFontMetrics *
3191 get_font_metrics (PangoContext *context, GtkWidget *widget)
3193 GtkStyleContext *style_context;
3194 PangoFontDescription *font;
3195 PangoFontMetrics *retval;
3197 style_context = gtk_widget_get_style_context (widget);
3198 gtk_style_context_get (style_context, 0, "font", &font, NULL);
3200 retval = pango_context_get_metrics (context,
3202 pango_context_get_language (context));
3205 pango_font_description_free (font);
3211 * gtk_label_get_measuring_layout:
3213 * @existing_layout: %NULL or an existing layout already in use.
3214 * @width: the width to measure with in pango units, or -1 for infinite
3215 * @height: the height to measure with in pango units, or -1 for infinite
3217 * Gets a layout that can be used for measuring sizes. The returned
3218 * layout will be identical to the label's layout except for the
3219 * layout's width, which will be set to @width. Do not modify the returned
3222 * Returns: a new reference to a pango layout
3224 static PangoLayout *
3225 gtk_label_get_measuring_layout (GtkLabel * label,
3226 PangoLayout *existing_layout,
3230 GtkLabelPrivate *priv = label->priv;
3231 PangoRectangle rect;
3234 if (existing_layout != NULL)
3236 if (existing_layout != priv->layout)
3238 pango_layout_set_width (existing_layout, width);
3239 pango_layout_set_height (existing_layout, height);
3240 return existing_layout;
3243 g_object_unref (existing_layout);
3246 gtk_label_ensure_layout (label);
3248 if (pango_layout_get_width (priv->layout) == width &&
3249 pango_layout_get_height (priv->layout) == height)
3251 g_object_ref (priv->layout);
3252 return priv->layout;
3255 /* We can use the label's own layout if we're not allocated a size yet,
3256 * because we don't need it to be properly setup at that point.
3257 * This way we can make use of caching upon the label's creation.
3259 if (gtk_widget_get_allocated_width (GTK_WIDGET (label)) <= 1)
3261 g_object_ref (priv->layout);
3262 pango_layout_set_width (priv->layout, width);
3263 pango_layout_set_height (priv->layout, height);
3264 return priv->layout;
3267 /* oftentimes we want to measure a width that is far wider than the current width,
3268 * even though the layout would not change if we made it wider. In that case, we
3269 * can just return the current layout, because for measuring purposes, it will be
3272 pango_layout_get_extents (priv->layout, NULL, &rect);
3273 if ((width == -1 || rect.width <= width) &&
3274 (height == -1 || rect.height <= height) &&
3275 !pango_layout_is_wrapped (priv->layout) &&
3276 !pango_layout_is_ellipsized (priv->layout))
3278 g_object_ref (priv->layout);
3279 return priv->layout;
3282 copy = pango_layout_copy (priv->layout);
3283 pango_layout_set_width (copy, width);
3284 pango_layout_set_height (copy, height);
3289 gtk_label_update_layout_width (GtkLabel *label)
3291 GtkLabelPrivate *priv = label->priv;
3292 GtkWidget *widget = GTK_WIDGET (label);
3294 g_assert (priv->layout);
3296 if (priv->ellipsize || priv->wrap)
3298 PangoRectangle logical;
3302 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3304 width = gtk_widget_get_allocated_width (GTK_WIDGET (label)) - xpad * 2;
3305 height = gtk_widget_get_allocated_height (GTK_WIDGET (label)) - ypad * 2;
3307 if (priv->have_transform)
3309 PangoContext *context = gtk_widget_get_pango_context (widget);
3310 const PangoMatrix *matrix = pango_context_get_matrix (context);
3311 const gdouble dx = matrix->xx; /* cos (M_PI * angle / 180) */
3312 const gdouble dy = matrix->xy; /* sin (M_PI * angle / 180) */
3314 pango_layout_set_width (priv->layout, -1);
3315 pango_layout_set_height (priv->layout, -1);
3316 pango_layout_get_pixel_extents (priv->layout, NULL, &logical);
3318 if (fabs (dy) < 0.01)
3320 if (logical.width > width)
3321 pango_layout_set_width (priv->layout, width * PANGO_SCALE);
3323 else if (fabs (dx) < 0.01)
3325 if (logical.width > height)
3326 pango_layout_set_width (priv->layout, height * PANGO_SCALE);
3330 gdouble x0, y0, x1, y1, length;
3335 y0 = dx ? x0 * dy / dx : G_MAXDOUBLE;
3336 vertical = fabs (y0) > height / 2;
3341 x0 = dy ? y0 * dx / dy : G_MAXDOUBLE;
3344 length = 2 * sqrt (x0 * x0 + y0 * y0);
3345 pango_layout_set_width (priv->layout, rint (length * PANGO_SCALE));
3346 pango_layout_get_pixel_size (priv->layout, NULL, &cy);
3353 y0 = height/2 + y1 - y0;
3358 x0 = width/2 + x1 - x0;
3362 length = length - sqrt (x0 * x0 + y0 * y0) * 2;
3363 pango_layout_set_width (priv->layout, rint (length * PANGO_SCALE));
3368 pango_layout_set_width (priv->layout, width * PANGO_SCALE);
3369 pango_layout_set_height (priv->layout, priv->ellipsize ? height * PANGO_SCALE : -1);
3374 pango_layout_set_width (priv->layout, -1);
3375 pango_layout_set_height (priv->layout, -1);
3380 gtk_label_ensure_layout (GtkLabel *label)
3382 GtkLabelPrivate *priv = label->priv;
3386 widget = GTK_WIDGET (label);
3388 rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
3392 PangoAlignment align = PANGO_ALIGN_LEFT; /* Quiet gcc */
3393 gdouble angle = gtk_label_get_angle (label);
3395 if (angle != 0.0 && !priv->select_info)
3397 PangoMatrix matrix = PANGO_MATRIX_INIT;
3399 /* We rotate the standard singleton PangoContext for the widget,
3400 * depending on the fact that it's meant pretty much exclusively
3403 pango_matrix_rotate (&matrix, angle);
3405 pango_context_set_matrix (gtk_widget_get_pango_context (widget), &matrix);
3407 priv->have_transform = TRUE;
3411 if (priv->have_transform)
3412 pango_context_set_matrix (gtk_widget_get_pango_context (widget), NULL);
3414 priv->have_transform = FALSE;
3417 priv->layout = gtk_widget_create_pango_layout (widget, priv->text);
3419 if (priv->effective_attrs)
3420 pango_layout_set_attributes (priv->layout, priv->effective_attrs);
3422 gtk_label_rescan_links (label);
3424 switch (priv->jtype)
3426 case GTK_JUSTIFY_LEFT:
3427 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3429 case GTK_JUSTIFY_RIGHT:
3430 align = rtl ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
3432 case GTK_JUSTIFY_CENTER:
3433 align = PANGO_ALIGN_CENTER;
3435 case GTK_JUSTIFY_FILL:
3436 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3437 pango_layout_set_justify (priv->layout, TRUE);
3440 g_assert_not_reached();
3443 pango_layout_set_alignment (priv->layout, align);
3444 pango_layout_set_ellipsize (priv->layout, priv->ellipsize);
3445 pango_layout_set_wrap (priv->layout, priv->wrap_mode);
3446 pango_layout_set_single_paragraph_mode (priv->layout, priv->single_line_mode);
3448 gtk_label_update_layout_width (label);
3453 get_single_line_height (GtkWidget *widget,
3454 PangoLayout *layout)
3456 PangoContext *context;
3457 PangoFontMetrics *metrics;
3458 gint ascent, descent;
3460 context = pango_layout_get_context (layout);
3461 metrics = get_font_metrics (context, widget);
3462 ascent = pango_font_metrics_get_ascent (metrics);
3463 descent = pango_font_metrics_get_descent (metrics);
3464 pango_font_metrics_unref (metrics);
3466 return ascent + descent;
3469 static GtkSizeRequestMode
3470 gtk_label_get_request_mode (GtkWidget *widget)
3472 GtkLabel *label = GTK_LABEL (widget);
3473 gdouble angle = gtk_label_get_angle (label);
3475 if (label->priv->wrap)
3476 return (angle == 90 || angle == 270) ?
3477 GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT :
3478 GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
3480 return GTK_SIZE_REQUEST_CONSTANT_SIZE;
3484 get_size_for_allocation (GtkLabel *label,
3485 GtkOrientation orientation,
3490 PangoLayout *layout;
3493 layout = gtk_label_get_measuring_layout (label, NULL, allocation * PANGO_SCALE, -1);
3495 pango_layout_get_pixel_size (layout, NULL, &text_height);
3498 *minimum_size = text_height;
3501 *natural_size = text_height;
3503 g_object_unref (layout);
3507 get_char_pixels (GtkWidget *label,
3508 PangoLayout *layout)
3510 PangoContext *context;
3511 PangoFontMetrics *metrics;
3512 gint char_width, digit_width;
3514 context = pango_layout_get_context (layout);
3515 metrics = get_font_metrics (context, GTK_WIDGET (label));
3516 char_width = pango_font_metrics_get_approximate_char_width (metrics);
3517 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
3518 pango_font_metrics_unref (metrics);
3520 return MAX (char_width, digit_width);;
3524 gtk_label_get_preferred_layout_size (GtkLabel *label,
3525 PangoRectangle *required,
3526 PangoRectangle *natural)
3528 GtkLabelPrivate *priv = label->priv;
3529 PangoLayout *layout;
3531 /* "width-chars" Hard-coded minimum width:
3532 * - minimum size should be MAX (width-chars, strlen ("..."));
3533 * - natural size should be MAX (width-chars, strlen (priv->text));
3535 * "max-width-chars" User specified maximum size requisition
3536 * - minimum size should be MAX (width-chars, 0)
3537 * - natural size should be MIN (max-width-chars, strlen (priv->text))
3539 * For ellipsizing labels; if max-width-chars is specified: either it is used as
3540 * a minimum size or the label text as a minimum size (natural size still overflows).
3542 * For wrapping labels; A reasonable minimum size is useful to naturally layout
3543 * interfaces automatically. In this case if no "width-chars" is specified, the minimum
3544 * width will default to the wrap guess that gtk_label_ensure_layout() does.
3547 /* Start off with the pixel extents of an as-wide-as-possible layout */
3548 layout = gtk_label_get_measuring_layout (label, NULL, -1, -1);
3550 pango_layout_get_extents (layout, NULL, natural);
3551 natural->x = natural->y = 0;
3553 if (priv->single_line_mode || priv->wrap)
3554 natural->height = get_single_line_height (GTK_WIDGET (label), layout);
3556 if (priv->ellipsize || priv->wrap)
3558 /* a layout with width 0 will be as small as humanly possible */
3559 layout = gtk_label_get_measuring_layout (label, layout, 0, -1);
3561 pango_layout_get_extents (layout, NULL, required);
3562 required->x = required->y = 0;
3563 required->height = natural->height;
3567 *required = *natural;
3570 if (priv->width_chars > -1 || priv->max_width_chars > -1)
3574 char_pixels = get_char_pixels (GTK_WIDGET (label), layout);
3576 if (priv->width_chars > -1)
3577 required->width = MAX (required->width, char_pixels * priv->width_chars);
3579 if (priv->max_width_chars > -1)
3580 natural->width = MIN (natural->width, priv->max_width_chars * char_pixels);
3581 natural->width = MAX (natural->width, required->width);
3584 g_object_unref (layout);
3588 gtk_label_get_preferred_size (GtkWidget *widget,
3589 GtkOrientation orientation,
3593 GtkLabel *label = GTK_LABEL (widget);
3594 GtkLabelPrivate *priv = label->priv;
3595 PangoRectangle required_rect;
3596 PangoRectangle natural_rect;
3599 gtk_label_get_preferred_layout_size (label, &required_rect, &natural_rect);
3601 /* Now that we have minimum and natural sizes in pango extents, apply a possible transform */
3602 if (priv->have_transform)
3605 PangoContext *context;
3606 const PangoMatrix *matrix;
3608 copy = pango_layout_copy (priv->layout);
3609 context = pango_layout_get_context (copy);
3610 matrix = pango_context_get_matrix (context);
3612 pango_layout_set_width (copy, -1);
3613 pango_layout_set_ellipsize (copy, PANGO_ELLIPSIZE_NONE);
3615 pango_layout_get_extents (copy, NULL, &natural_rect);
3616 g_object_unref (copy);
3618 pango_matrix_transform_rectangle (matrix, &required_rect);
3619 pango_matrix_transform_rectangle (matrix, &natural_rect);
3621 /* Bump the natural size in case of ellipsize to ensure pango has
3622 * enough space in the angles (note, we could alternatively set the
3623 * layout to not ellipsize when we know we have been allocated our
3624 * full natural size, or it may be that pango needs a fix here).
3626 if (priv->ellipsize && priv->angle != 0 && priv->angle != 90 &&
3627 priv->angle != 180 && priv->angle != 270 && priv->angle != 360)
3629 /* For some reason we only need this at about 110 degrees, and only
3630 * when gaining in height
3632 natural_rect.height += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3633 natural_rect.width += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3637 required_rect.width = PANGO_PIXELS_CEIL (required_rect.width);
3638 required_rect.height = PANGO_PIXELS_CEIL (required_rect.height);
3640 natural_rect.width = PANGO_PIXELS_CEIL (natural_rect.width);
3641 natural_rect.height = PANGO_PIXELS_CEIL (natural_rect.height);
3643 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3645 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3647 /* Note, we cant use get_size_for_allocation() when rotating
3648 * ellipsized labels.
3650 if (!(priv->ellipsize && priv->have_transform) &&
3651 (priv->angle == 90 || priv->angle == 270))
3653 /* Doing a h4w request on a rotated label here, return the
3654 * required width for the minimum height.
3656 get_size_for_allocation (label,
3657 GTK_ORIENTATION_VERTICAL,
3658 required_rect.height,
3659 minimum_size, natural_size);
3664 /* Normal desired width */
3665 *minimum_size = required_rect.width;
3666 *natural_size = natural_rect.width;
3669 *minimum_size += xpad * 2;
3670 *natural_size += xpad * 2;
3672 else /* GTK_ORIENTATION_VERTICAL */
3674 /* Note, we cant use get_size_for_allocation() when rotating
3675 * ellipsized labels.
3677 if (!(priv->ellipsize && priv->have_transform) &&
3678 (priv->angle == 0 || priv->angle == 180 || priv->angle == 360))
3680 /* Doing a w4h request on a label here, return the required
3681 * height for the minimum width.
3683 get_size_for_allocation (label,
3684 GTK_ORIENTATION_HORIZONTAL,
3685 required_rect.width,
3686 minimum_size, natural_size);
3690 /* A vertically rotated label does w4h, so return the base
3691 * desired height (text length)
3693 *minimum_size = required_rect.height;
3694 *natural_size = natural_rect.height;
3697 *minimum_size += ypad * 2;
3698 *natural_size += ypad * 2;
3704 gtk_label_get_preferred_width (GtkWidget *widget,
3708 gtk_label_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
3712 gtk_label_get_preferred_height (GtkWidget *widget,
3716 gtk_label_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
3720 gtk_label_get_preferred_width_for_height (GtkWidget *widget,
3722 gint *minimum_width,
3723 gint *natural_width)
3725 GtkLabel *label = GTK_LABEL (widget);
3726 GtkLabelPrivate *priv = label->priv;
3728 if (priv->wrap && (priv->angle == 90 || priv->angle == 270))
3732 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3735 gtk_label_clear_layout (label);
3737 get_size_for_allocation (label, GTK_ORIENTATION_VERTICAL,
3738 MAX (1, height - (ypad * 2)),
3739 minimum_width, natural_width);
3742 *minimum_width += xpad * 2;
3745 *natural_width += xpad * 2;
3748 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_width, natural_width);
3752 gtk_label_get_preferred_height_for_width (GtkWidget *widget,
3754 gint *minimum_height,
3755 gint *natural_height)
3757 GtkLabel *label = GTK_LABEL (widget);
3758 GtkLabelPrivate *priv = label->priv;
3760 if (priv->wrap && (priv->angle == 0 || priv->angle == 180 || priv->angle == 360))
3764 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3767 gtk_label_clear_layout (label);
3769 get_size_for_allocation (label, GTK_ORIENTATION_HORIZONTAL,
3770 MAX (1, width - xpad * 2),
3771 minimum_height, natural_height);
3774 *minimum_height += ypad * 2;
3777 *natural_height += ypad * 2;
3780 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, minimum_height, natural_height);
3784 gtk_label_size_allocate (GtkWidget *widget,
3785 GtkAllocation *allocation)
3787 GtkLabel *label = GTK_LABEL (widget);
3788 GtkLabelPrivate *priv = label->priv;
3790 GTK_WIDGET_CLASS (gtk_label_parent_class)->size_allocate (widget, allocation);
3793 gtk_label_update_layout_width (label);
3795 if (priv->select_info && priv->select_info->window)
3797 gdk_window_move_resize (priv->select_info->window,
3801 allocation->height);
3806 gtk_label_update_cursor (GtkLabel *label)
3808 GtkLabelPrivate *priv = label->priv;
3811 if (!priv->select_info)
3814 widget = GTK_WIDGET (label);
3816 if (gtk_widget_get_realized (widget))
3818 GdkDisplay *display;
3821 if (gtk_widget_is_sensitive (widget))
3823 display = gtk_widget_get_display (widget);
3825 if (priv->select_info->active_link)
3826 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
3827 else if (priv->select_info->selectable)
3828 cursor = gdk_cursor_new_for_display (display, GDK_XTERM);
3835 gdk_window_set_cursor (priv->select_info->window, cursor);
3838 g_object_unref (cursor);
3843 gtk_label_state_changed (GtkWidget *widget,
3844 GtkStateType prev_state)
3846 GtkLabel *label = GTK_LABEL (widget);
3847 GtkLabelPrivate *priv = label->priv;
3849 if (priv->select_info)
3851 if (!gtk_widget_is_sensitive (widget))
3852 gtk_label_select_region (label, 0, 0);
3854 gtk_label_update_cursor (label);
3857 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed)
3858 GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed (widget, prev_state);
3862 gtk_label_style_updated (GtkWidget *widget)
3864 GtkLabel *label = GTK_LABEL (widget);
3866 GTK_WIDGET_CLASS (gtk_label_parent_class)->style_updated (widget);
3868 /* We have to clear the layout, fonts etc. may have changed */
3869 gtk_label_clear_layout (label);
3873 gtk_label_direction_changed (GtkWidget *widget,
3874 GtkTextDirection previous_dir)
3876 GtkLabel *label = GTK_LABEL (widget);
3877 GtkLabelPrivate *priv = label->priv;
3880 pango_layout_context_changed (priv->layout);
3882 GTK_WIDGET_CLASS (gtk_label_parent_class)->direction_changed (widget, previous_dir);
3886 get_layout_location (GtkLabel *label,
3890 GtkAllocation allocation;
3893 GtkLabelPrivate *priv;
3894 gint req_width, x, y;
3897 gfloat xalign, yalign;
3898 PangoRectangle logical;
3900 misc = GTK_MISC (label);
3901 widget = GTK_WIDGET (label);
3904 gtk_misc_get_alignment (misc, &xalign, &yalign);
3905 gtk_misc_get_padding (misc, &xpad, &ypad);
3907 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
3908 xalign = 1.0 - xalign;
3910 pango_layout_get_extents (priv->layout, NULL, &logical);
3912 if (priv->have_transform)
3914 PangoContext *context = gtk_widget_get_pango_context (widget);
3915 const PangoMatrix *matrix = pango_context_get_matrix (context);
3916 pango_matrix_transform_rectangle (matrix, &logical);
3919 pango_extents_to_pixels (&logical, NULL);
3921 req_width = logical.width;
3922 req_height = logical.height;
3924 req_width += 2 * xpad;
3925 req_height += 2 * ypad;
3927 gtk_widget_get_allocation (widget, &allocation);
3929 x = floor (allocation.x + xpad + xalign * (allocation.width - req_width));
3931 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
3932 x = MAX (x, allocation.x + xpad);
3934 x = MIN (x, allocation.x + allocation.width - xpad);
3937 /* bgo#315462 - For single-line labels, *do* align the requisition with
3938 * respect to the allocation, even if we are under-allocated. For multi-line
3939 * labels, always show the top of the text when they are under-allocated. The
3940 * rationale is this:
3942 * - Single-line labels appear in GtkButtons, and it is very easy to get them
3943 * to be smaller than their requisition. The button may clip the label, but
3944 * the label will still be able to show most of itself and the focus
3945 * rectangle. Also, it is fairly easy to read a single line of clipped text.
3947 * - Multi-line labels should not be clipped to showing "something in the
3948 * middle". You want to read the first line, at least, to get some context.
3950 if (pango_layout_get_line_count (priv->layout) == 1)
3951 y = floor (allocation.y + ypad + (allocation.height - req_height) * yalign);
3953 y = floor (allocation.y + ypad + MAX ((allocation.height - req_height) * yalign, 0));
3963 draw_insertion_cursor (GtkLabel *label,
3965 GdkRectangle *cursor_location,
3966 gboolean is_primary,
3967 PangoDirection direction,
3968 gboolean draw_arrow)
3970 GtkWidget *widget = GTK_WIDGET (label);
3971 GtkTextDirection text_dir;
3973 if (direction == PANGO_DIRECTION_LTR)
3974 text_dir = GTK_TEXT_DIR_LTR;
3976 text_dir = GTK_TEXT_DIR_RTL;
3978 gtk_draw_insertion_cursor (widget, cr, cursor_location,
3979 is_primary, text_dir, draw_arrow);
3982 static PangoDirection
3983 get_cursor_direction (GtkLabel *label)
3985 GtkLabelPrivate *priv = label->priv;
3988 g_assert (priv->select_info);
3990 gtk_label_ensure_layout (label);
3992 for (l = pango_layout_get_lines_readonly (priv->layout); l; l = l->next)
3994 PangoLayoutLine *line = l->data;
3996 /* If priv->select_info->selection_end is at the very end of
3997 * the line, we don't know if the cursor is on this line or
3998 * the next without looking ahead at the next line. (End
3999 * of paragraph is different from line break.) But it's
4000 * definitely in this paragraph, which is good enough
4001 * to figure out the resolved direction.
4003 if (line->start_index + line->length >= priv->select_info->selection_end)
4004 return line->resolved_dir;
4007 return PANGO_DIRECTION_LTR;
4011 gtk_label_draw_cursor (GtkLabel *label, cairo_t *cr, gint xoffset, gint yoffset)
4013 GtkLabelPrivate *priv = label->priv;
4016 if (priv->select_info == NULL)
4019 widget = GTK_WIDGET (label);
4021 if (gtk_widget_is_drawable (widget))
4023 PangoDirection keymap_direction;
4024 PangoDirection cursor_direction;
4025 PangoRectangle strong_pos, weak_pos;
4026 gboolean split_cursor;
4027 PangoRectangle *cursor1 = NULL;
4028 PangoRectangle *cursor2 = NULL;
4029 GdkRectangle cursor_location;
4030 PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
4031 PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
4033 keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gtk_widget_get_display (widget)));
4034 cursor_direction = get_cursor_direction (label);
4036 gtk_label_ensure_layout (label);
4038 pango_layout_get_cursor_pos (priv->layout, priv->select_info->selection_end,
4039 &strong_pos, &weak_pos);
4041 g_object_get (gtk_widget_get_settings (widget),
4042 "gtk-split-cursor", &split_cursor,
4045 dir1 = cursor_direction;
4049 cursor1 = &strong_pos;
4051 if (strong_pos.x != weak_pos.x ||
4052 strong_pos.y != weak_pos.y)
4054 dir2 = (cursor_direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
4055 cursor2 = &weak_pos;
4060 if (keymap_direction == cursor_direction)
4061 cursor1 = &strong_pos;
4063 cursor1 = &weak_pos;
4066 cursor_location.x = xoffset + PANGO_PIXELS (cursor1->x);
4067 cursor_location.y = yoffset + PANGO_PIXELS (cursor1->y);
4068 cursor_location.width = 0;
4069 cursor_location.height = PANGO_PIXELS (cursor1->height);
4071 draw_insertion_cursor (label, cr,
4072 &cursor_location, TRUE, dir1,
4073 dir2 != PANGO_DIRECTION_NEUTRAL);
4075 if (dir2 != PANGO_DIRECTION_NEUTRAL)
4077 cursor_location.x = xoffset + PANGO_PIXELS (cursor2->x);
4078 cursor_location.y = yoffset + PANGO_PIXELS (cursor2->y);
4079 cursor_location.width = 0;
4080 cursor_location.height = PANGO_PIXELS (cursor2->height);
4082 draw_insertion_cursor (label, cr,
4083 &cursor_location, FALSE, dir2,
4089 static GtkLabelLink *
4090 gtk_label_get_focus_link (GtkLabel *label)
4092 GtkLabelPrivate *priv = label->priv;
4093 GtkLabelSelectionInfo *info = priv->select_info;
4099 if (info->selection_anchor != info->selection_end)
4102 for (l = info->links; l; l = l->next)
4104 GtkLabelLink *link = l->data;
4105 if (link->start <= info->selection_anchor &&
4106 info->selection_anchor <= link->end)
4114 gtk_label_draw (GtkWidget *widget,
4117 GtkLabel *label = GTK_LABEL (widget);
4118 GtkLabelPrivate *priv = label->priv;
4119 GtkLabelSelectionInfo *info = priv->select_info;
4120 GtkAllocation allocation;
4121 GtkStyleContext *context;
4122 GtkStateFlags state;
4125 gtk_label_ensure_layout (label);
4127 if (priv->text && (*priv->text != '\0'))
4129 GdkRGBA *bg_color, *fg_color;
4131 get_layout_location (label, &x, &y);
4133 context = gtk_widget_get_style_context (widget);
4134 gtk_widget_get_allocation (widget, &allocation);
4136 cairo_translate (cr, -allocation.x, -allocation.y);
4138 state = gtk_widget_get_state_flags (widget);
4139 gtk_style_context_set_state (context, state);
4141 gtk_render_layout (context, cr,
4146 (info->selection_anchor != info->selection_end))
4149 cairo_region_t *clip;
4151 range[0] = info->selection_anchor;
4152 range[1] = info->selection_end;
4154 if (range[0] > range[1])
4156 gint tmp = range[0];
4157 range[0] = range[1];
4161 clip = gdk_pango_layout_get_clip_region (priv->layout,
4166 /* FIXME should use gtk_paint, but it can't use a clip region */
4169 gdk_cairo_region (cr, clip);
4172 state = GTK_STATE_FLAG_SELECTED;
4174 if (gtk_widget_has_focus (widget))
4175 state |= GTK_STATE_FLAG_FOCUSED;
4177 gtk_style_context_get (context, state,
4178 "background-color", &bg_color,
4182 gdk_cairo_set_source_rgba (cr, bg_color);
4185 gdk_cairo_set_source_rgba (cr, fg_color);
4186 cairo_move_to (cr, x, y);
4187 _gtk_pango_fill_layout (cr, priv->layout);
4190 cairo_region_destroy (clip);
4192 gdk_rgba_free (bg_color);
4193 gdk_rgba_free (fg_color);
4197 GtkLabelLink *focus_link;
4198 GtkLabelLink *active_link;
4200 cairo_region_t *clip;
4202 GdkColor *text_color;
4203 GdkColor *link_color;
4204 GdkColor *visited_link_color;
4206 if (info->selectable && gtk_widget_has_focus (widget))
4207 gtk_label_draw_cursor (label, cr, x, y);
4209 focus_link = gtk_label_get_focus_link (label);
4210 active_link = info->active_link;
4217 range[0] = active_link->start;
4218 range[1] = active_link->end;
4222 clip = gdk_pango_layout_get_clip_region (priv->layout,
4226 gdk_cairo_region (cr, clip);
4228 cairo_region_destroy (clip);
4230 gtk_label_get_link_colors (widget, &link_color, &visited_link_color);
4231 if (active_link->visited)
4232 text_color = visited_link_color;
4234 text_color = link_color;
4236 if (info->link_clicked)
4237 state = GTK_STATE_FLAG_ACTIVE;
4239 state = GTK_STATE_FLAG_PRELIGHT;
4241 gtk_style_context_get (context, state,
4242 "background-color", &bg_color,
4245 gdk_cairo_set_source_rgba (cr, bg_color);
4248 gdk_cairo_set_source_color (cr, text_color);
4249 cairo_move_to (cr, x, y);
4250 _gtk_pango_fill_layout (cr, priv->layout);
4252 gdk_color_free (link_color);
4253 gdk_color_free (visited_link_color);
4254 gdk_rgba_free (bg_color);
4259 if (focus_link && gtk_widget_has_focus (widget))
4261 range[0] = focus_link->start;
4262 range[1] = focus_link->end;
4264 clip = gdk_pango_layout_get_clip_region (priv->layout,
4268 cairo_region_get_extents (clip, &rect);
4270 state = gtk_widget_get_state_flags (widget);
4271 gtk_style_context_set_state (context, state);
4273 gtk_render_focus (context, cr,
4275 rect.width, rect.height);
4277 cairo_region_destroy (clip);
4286 separate_uline_pattern (const gchar *str,
4291 gboolean underscore;
4294 gchar *pattern_dest;
4296 *accel_key = GDK_KEY_VoidSymbol;
4297 *new_str = g_new (gchar, strlen (str) + 1);
4298 *pattern = g_new (gchar, g_utf8_strlen (str, -1) + 1);
4304 pattern_dest = *pattern;
4309 const gchar *next_src;
4311 c = g_utf8_get_char (src);
4312 if (c == (gunichar)-1)
4314 g_warning ("Invalid input string");
4320 next_src = g_utf8_next_char (src);
4325 *pattern_dest++ = ' ';
4328 *pattern_dest++ = '_';
4329 if (*accel_key == GDK_KEY_VoidSymbol)
4330 *accel_key = gdk_keyval_to_lower (gdk_unicode_to_keyval (c));
4333 while (src < next_src)
4347 while (src < next_src)
4350 *pattern_dest++ = ' ';
4362 gtk_label_set_uline_text_internal (GtkLabel *label,
4365 GtkLabelPrivate *priv = label->priv;
4366 guint accel_key = GDK_KEY_VoidSymbol;
4370 g_return_if_fail (GTK_IS_LABEL (label));
4371 g_return_if_fail (str != NULL);
4373 /* Split text into the base text and a separate pattern
4376 if (!separate_uline_pattern (str, &accel_key, &new_str, &pattern))
4379 gtk_label_set_text_internal (label, new_str);
4380 gtk_label_set_pattern_internal (label, pattern, TRUE);
4381 priv->mnemonic_keyval = accel_key;
4387 * gtk_label_set_text_with_mnemonic:
4388 * @label: a #GtkLabel
4391 * Sets the label's text from the string @str.
4392 * If characters in @str are preceded by an underscore, they are underlined
4393 * indicating that they represent a keyboard accelerator called a mnemonic.
4394 * The mnemonic key can be used to activate another widget, chosen
4395 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
4398 gtk_label_set_text_with_mnemonic (GtkLabel *label,
4401 g_return_if_fail (GTK_IS_LABEL (label));
4402 g_return_if_fail (str != NULL);
4404 g_object_freeze_notify (G_OBJECT (label));
4406 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
4407 gtk_label_set_use_markup_internal (label, FALSE);
4408 gtk_label_set_use_underline_internal (label, TRUE);
4410 gtk_label_recalculate (label);
4412 g_object_thaw_notify (G_OBJECT (label));
4416 gtk_label_realize (GtkWidget *widget)
4418 GtkLabel *label = GTK_LABEL (widget);
4419 GtkLabelPrivate *priv = label->priv;
4421 GTK_WIDGET_CLASS (gtk_label_parent_class)->realize (widget);
4423 if (priv->select_info)
4424 gtk_label_create_window (label);
4428 gtk_label_unrealize (GtkWidget *widget)
4430 GtkLabel *label = GTK_LABEL (widget);
4431 GtkLabelPrivate *priv = label->priv;
4433 if (priv->select_info)
4434 gtk_label_destroy_window (label);
4436 GTK_WIDGET_CLASS (gtk_label_parent_class)->unrealize (widget);
4440 gtk_label_map (GtkWidget *widget)
4442 GtkLabel *label = GTK_LABEL (widget);
4443 GtkLabelPrivate *priv = label->priv;
4445 GTK_WIDGET_CLASS (gtk_label_parent_class)->map (widget);
4447 if (priv->select_info)
4448 gdk_window_show (priv->select_info->window);
4452 gtk_label_unmap (GtkWidget *widget)
4454 GtkLabel *label = GTK_LABEL (widget);
4455 GtkLabelPrivate *priv = label->priv;
4457 if (priv->select_info)
4458 gdk_window_hide (priv->select_info->window);
4460 GTK_WIDGET_CLASS (gtk_label_parent_class)->unmap (widget);
4464 window_to_layout_coords (GtkLabel *label,
4468 GtkAllocation allocation;
4472 widget = GTK_WIDGET (label);
4474 /* get layout location in widget->window coords */
4475 get_layout_location (label, &lx, &ly);
4477 gtk_widget_get_allocation (widget, &allocation);
4481 *x += allocation.x; /* go to widget->window */
4482 *x -= lx; /* go to layout */
4487 *y += allocation.y; /* go to widget->window */
4488 *y -= ly; /* go to layout */
4494 layout_to_window_coords (GtkLabel *label,
4501 widget = GTK_WIDGET (label);
4503 /* get layout location in widget->window coords */
4504 get_layout_location (label, &lx, &ly);
4508 *x += lx; /* go to widget->window */
4509 *x -= widget->allocation.x; /* go to selection window */
4514 *y += ly; /* go to widget->window */
4515 *y -= widget->allocation.y; /* go to selection window */
4521 get_layout_index (GtkLabel *label,
4526 GtkLabelPrivate *priv = label->priv;
4528 const gchar *cluster;
4529 const gchar *cluster_end;
4534 gtk_label_ensure_layout (label);
4536 window_to_layout_coords (label, &x, &y);
4541 inside = pango_layout_xy_to_index (priv->layout,
4545 cluster = priv->text + *index;
4546 cluster_end = cluster;
4549 cluster_end = g_utf8_next_char (cluster_end);
4553 *index += (cluster_end - cluster);
4559 gtk_label_select_word (GtkLabel *label)
4561 GtkLabelPrivate *priv = label->priv;
4564 gint start_index = gtk_label_move_backward_word (label, priv->select_info->selection_end);
4565 gint end_index = gtk_label_move_forward_word (label, priv->select_info->selection_end);
4567 min = MIN (priv->select_info->selection_anchor,
4568 priv->select_info->selection_end);
4569 max = MAX (priv->select_info->selection_anchor,
4570 priv->select_info->selection_end);
4572 min = MIN (min, start_index);
4573 max = MAX (max, end_index);
4575 gtk_label_select_region_index (label, min, max);
4579 gtk_label_grab_focus (GtkWidget *widget)
4581 GtkLabel *label = GTK_LABEL (widget);
4582 GtkLabelPrivate *priv = label->priv;
4583 gboolean select_on_focus;
4586 if (priv->select_info == NULL)
4589 GTK_WIDGET_CLASS (gtk_label_parent_class)->grab_focus (widget);
4591 if (priv->select_info->selectable)
4593 g_object_get (gtk_widget_get_settings (widget),
4594 "gtk-label-select-on-focus",
4598 if (select_on_focus && !priv->in_click)
4599 gtk_label_select_region (label, 0, -1);
4603 if (priv->select_info->links && !priv->in_click)
4605 link = priv->select_info->links->data;
4606 priv->select_info->selection_anchor = link->start;
4607 priv->select_info->selection_end = link->start;
4613 gtk_label_focus (GtkWidget *widget,
4614 GtkDirectionType direction)
4616 GtkLabel *label = GTK_LABEL (widget);
4617 GtkLabelPrivate *priv = label->priv;
4618 GtkLabelSelectionInfo *info = priv->select_info;
4619 GtkLabelLink *focus_link;
4622 if (!gtk_widget_is_focus (widget))
4624 gtk_widget_grab_focus (widget);
4627 focus_link = gtk_label_get_focus_link (label);
4628 if (focus_link && direction == GTK_DIR_TAB_BACKWARD)
4630 l = g_list_last (info->links);
4631 focus_link = l->data;
4632 info->selection_anchor = focus_link->start;
4633 info->selection_end = focus_link->start;
4643 if (info->selectable)
4647 if (info->selection_anchor != info->selection_end)
4650 index = info->selection_anchor;
4652 if (direction == GTK_DIR_TAB_FORWARD)
4653 for (l = info->links; l; l = l->next)
4655 GtkLabelLink *link = l->data;
4657 if (link->start > index)
4659 gtk_label_select_region_index (label, link->start, link->start);
4663 else if (direction == GTK_DIR_TAB_BACKWARD)
4664 for (l = g_list_last (info->links); l; l = l->prev)
4666 GtkLabelLink *link = l->data;
4668 if (link->end < index)
4670 gtk_label_select_region_index (label, link->start, link->start);
4679 focus_link = gtk_label_get_focus_link (label);
4682 case GTK_DIR_TAB_FORWARD:
4685 l = g_list_find (info->links, focus_link);
4692 case GTK_DIR_TAB_BACKWARD:
4695 l = g_list_find (info->links, focus_link);
4699 l = g_list_last (info->links);
4708 focus_link = l->data;
4709 info->selection_anchor = focus_link->start;
4710 info->selection_end = focus_link->start;
4711 gtk_widget_queue_draw (widget);
4723 gtk_label_button_press (GtkWidget *widget,
4724 GdkEventButton *event)
4726 GtkLabel *label = GTK_LABEL (widget);
4727 GtkLabelPrivate *priv = label->priv;
4728 GtkLabelSelectionInfo *info = priv->select_info;
4735 if (info->active_link)
4737 if (event->button == 1)
4739 info->link_clicked = 1;
4740 gtk_widget_queue_draw (widget);
4742 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4744 info->link_clicked = 1;
4745 gtk_label_do_popup (label, event);
4750 if (!info->selectable)
4753 info->in_drag = FALSE;
4754 info->select_words = FALSE;
4756 if (event->button == 1)
4758 if (!gtk_widget_has_focus (widget))
4760 priv->in_click = TRUE;
4761 gtk_widget_grab_focus (widget);
4762 priv->in_click = FALSE;
4765 if (event->type == GDK_3BUTTON_PRESS)
4767 gtk_label_select_region_index (label, 0, strlen (priv->text));
4771 if (event->type == GDK_2BUTTON_PRESS)
4773 info->select_words = TRUE;
4774 gtk_label_select_word (label);
4778 get_layout_index (label, event->x, event->y, &index);
4780 min = MIN (info->selection_anchor, info->selection_end);
4781 max = MAX (info->selection_anchor, info->selection_end);
4783 if ((info->selection_anchor != info->selection_end) &&
4784 (event->state & GDK_SHIFT_MASK))
4786 /* extend (same as motion) */
4787 min = MIN (min, index);
4788 max = MAX (max, index);
4790 /* ensure the anchor is opposite index */
4798 gtk_label_select_region_index (label, min, max);
4802 if (event->type == GDK_3BUTTON_PRESS)
4803 gtk_label_select_region_index (label, 0, strlen (priv->text));
4804 else if (event->type == GDK_2BUTTON_PRESS)
4805 gtk_label_select_word (label);
4806 else if (min < max && min <= index && index <= max)
4808 info->in_drag = TRUE;
4809 info->drag_start_x = event->x;
4810 info->drag_start_y = event->y;
4813 /* start a replacement */
4814 gtk_label_select_region_index (label, index, index);
4819 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4821 gtk_label_do_popup (label, event);
4829 gtk_label_button_release (GtkWidget *widget,
4830 GdkEventButton *event)
4833 GtkLabel *label = GTK_LABEL (widget);
4834 GtkLabelPrivate *priv = label->priv;
4835 GtkLabelSelectionInfo *info = priv->select_info;
4845 get_layout_index (label, event->x, event->y, &index);
4846 gtk_label_select_region_index (label, index, index);
4851 if (event->button != 1)
4854 if (info->active_link &&
4855 info->selection_anchor == info->selection_end &&
4858 emit_activate_link (label, info->active_link);
4859 info->link_clicked = 0;
4864 /* The goal here is to return TRUE iff we ate the
4865 * button press to start selecting.
4871 connect_mnemonics_visible_notify (GtkLabel *label)
4873 GtkLabelPrivate *priv = label->priv;
4874 GtkWidget *toplevel;
4877 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
4879 if (!GTK_IS_WINDOW (toplevel))
4882 /* always set up this widgets initial value */
4883 priv->mnemonics_visible =
4884 gtk_window_get_mnemonics_visible (GTK_WINDOW (toplevel));
4887 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (toplevel),
4888 "gtk-label-mnemonics-visible-connected"));
4892 g_signal_connect (toplevel,
4893 "notify::mnemonics-visible",
4894 G_CALLBACK (label_mnemonics_visible_changed),
4896 g_object_set_data (G_OBJECT (toplevel),
4897 "gtk-label-mnemonics-visible-connected",
4898 GINT_TO_POINTER (1));
4903 drag_begin_cb (GtkWidget *widget,
4904 GdkDragContext *context,
4907 GtkLabel *label = GTK_LABEL (widget);
4908 GtkLabelPrivate *priv = label->priv;
4909 cairo_surface_t *surface = NULL;
4911 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
4913 if ((priv->select_info->selection_anchor !=
4914 priv->select_info->selection_end) &&
4920 start = MIN (priv->select_info->selection_anchor,
4921 priv->select_info->selection_end);
4922 end = MAX (priv->select_info->selection_anchor,
4923 priv->select_info->selection_end);
4925 len = strlen (priv->text);
4933 surface = _gtk_text_util_create_drag_icon (widget,
4940 gtk_drag_set_icon_surface (context, surface);
4941 cairo_surface_destroy (surface);
4945 gtk_drag_set_icon_default (context);
4950 gtk_label_motion (GtkWidget *widget,
4951 GdkEventMotion *event)
4953 GtkLabel *label = GTK_LABEL (widget);
4954 GtkLabelPrivate *priv = label->priv;
4955 GtkLabelSelectionInfo *info = priv->select_info;
4961 if (info->links && !info->in_drag)
4965 gboolean found = FALSE;
4967 if (info->selection_anchor == info->selection_end)
4969 if (get_layout_index (label, event->x, event->y, &index))
4971 for (l = info->links; l != NULL; l = l->next)
4974 if (index >= link->start && index <= link->end)
4985 if (info->active_link != link)
4987 info->link_clicked = 0;
4988 info->active_link = link;
4989 gtk_label_update_cursor (label);
4990 gtk_widget_queue_draw (widget);
4995 if (info->active_link != NULL)
4997 info->link_clicked = 0;
4998 info->active_link = NULL;
4999 gtk_label_update_cursor (label);
5000 gtk_widget_queue_draw (widget);
5005 if (!info->selectable)
5008 if ((event->state & GDK_BUTTON1_MASK) == 0)
5013 if (gtk_drag_check_threshold (widget,
5016 event->x, event->y))
5018 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
5020 gtk_target_list_add_text_targets (target_list, 0);
5022 g_signal_connect (widget, "drag-begin",
5023 G_CALLBACK (drag_begin_cb), NULL);
5024 gtk_drag_begin (widget, target_list,
5026 1, (GdkEvent *)event);
5028 info->in_drag = FALSE;
5030 gtk_target_list_unref (target_list);
5037 gdk_window_get_device_position (info->window, event->device, &x, &y, NULL);
5038 get_layout_index (label, x, y, &index);
5040 if (info->select_words)
5043 gint old_min, old_max;
5046 min = gtk_label_move_backward_word (label, index);
5047 max = gtk_label_move_forward_word (label, index);
5049 anchor = info->selection_anchor;
5050 end = info->selection_end;
5052 old_min = MIN (anchor, end);
5053 old_max = MAX (anchor, end);
5060 else if (old_max < max)
5065 else if (anchor == old_min)
5076 gtk_label_select_region_index (label, anchor, end);
5079 gtk_label_select_region_index (label, info->selection_anchor, index);
5086 gtk_label_leave_notify (GtkWidget *widget,
5087 GdkEventCrossing *event)
5089 GtkLabel *label = GTK_LABEL (widget);
5090 GtkLabelPrivate *priv = label->priv;
5092 if (priv->select_info)
5094 priv->select_info->active_link = NULL;
5095 gtk_label_update_cursor (label);
5096 gtk_widget_queue_draw (widget);
5099 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event)
5100 return GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event (widget, event);
5106 gtk_label_create_window (GtkLabel *label)
5108 GtkLabelPrivate *priv = label->priv;
5109 GtkAllocation allocation;
5111 GdkWindowAttr attributes;
5112 gint attributes_mask;
5114 g_assert (priv->select_info);
5115 widget = GTK_WIDGET (label);
5116 g_assert (gtk_widget_get_realized (widget));
5118 if (priv->select_info->window)
5121 gtk_widget_get_allocation (widget, &allocation);
5123 attributes.x = allocation.x;
5124 attributes.y = allocation.y;
5125 attributes.width = allocation.width;
5126 attributes.height = allocation.height;
5127 attributes.window_type = GDK_WINDOW_CHILD;
5128 attributes.wclass = GDK_INPUT_ONLY;
5129 attributes.override_redirect = TRUE;
5130 attributes.event_mask = gtk_widget_get_events (widget) |
5131 GDK_BUTTON_PRESS_MASK |
5132 GDK_BUTTON_RELEASE_MASK |
5133 GDK_LEAVE_NOTIFY_MASK |
5134 GDK_BUTTON_MOTION_MASK |
5135 GDK_POINTER_MOTION_MASK |
5136 GDK_POINTER_MOTION_HINT_MASK;
5137 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
5138 if (gtk_widget_is_sensitive (widget))
5140 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
5142 attributes_mask |= GDK_WA_CURSOR;
5146 priv->select_info->window = gdk_window_new (gtk_widget_get_window (widget),
5147 &attributes, attributes_mask);
5148 gdk_window_set_user_data (priv->select_info->window, widget);
5150 if (attributes_mask & GDK_WA_CURSOR)
5151 g_object_unref (attributes.cursor);
5155 gtk_label_destroy_window (GtkLabel *label)
5157 GtkLabelPrivate *priv = label->priv;
5159 g_assert (priv->select_info);
5161 if (priv->select_info->window == NULL)
5164 gdk_window_set_user_data (priv->select_info->window, NULL);
5165 gdk_window_destroy (priv->select_info->window);
5166 priv->select_info->window = NULL;
5170 gtk_label_ensure_select_info (GtkLabel *label)
5172 GtkLabelPrivate *priv = label->priv;
5174 if (priv->select_info == NULL)
5176 priv->select_info = g_new0 (GtkLabelSelectionInfo, 1);
5178 gtk_widget_set_can_focus (GTK_WIDGET (label), TRUE);
5180 if (gtk_widget_get_realized (GTK_WIDGET (label)))
5181 gtk_label_create_window (label);
5183 if (gtk_widget_get_mapped (GTK_WIDGET (label)))
5184 gdk_window_show (priv->select_info->window);
5189 gtk_label_clear_select_info (GtkLabel *label)
5191 GtkLabelPrivate *priv = label->priv;
5193 if (priv->select_info == NULL)
5196 if (!priv->select_info->selectable && !priv->select_info->links)
5198 gtk_label_destroy_window (label);
5200 g_free (priv->select_info);
5201 priv->select_info = NULL;
5203 gtk_widget_set_can_focus (GTK_WIDGET (label), FALSE);
5208 * gtk_label_set_selectable:
5209 * @label: a #GtkLabel
5210 * @setting: %TRUE to allow selecting text in the label
5212 * Selectable labels allow the user to select text from the label, for
5216 gtk_label_set_selectable (GtkLabel *label,
5219 GtkLabelPrivate *priv;
5220 gboolean old_setting;
5222 g_return_if_fail (GTK_IS_LABEL (label));
5226 setting = setting != FALSE;
5227 old_setting = priv->select_info && priv->select_info->selectable;
5231 gtk_label_ensure_select_info (label);
5232 priv->select_info->selectable = TRUE;
5233 gtk_label_update_cursor (label);
5239 /* unselect, to give up the selection */
5240 gtk_label_select_region (label, 0, 0);
5242 priv->select_info->selectable = FALSE;
5243 gtk_label_clear_select_info (label);
5244 gtk_label_update_cursor (label);
5247 if (setting != old_setting)
5249 g_object_freeze_notify (G_OBJECT (label));
5250 g_object_notify (G_OBJECT (label), "selectable");
5251 g_object_notify (G_OBJECT (label), "cursor-position");
5252 g_object_notify (G_OBJECT (label), "selection-bound");
5253 g_object_thaw_notify (G_OBJECT (label));
5254 gtk_widget_queue_draw (GTK_WIDGET (label));
5259 * gtk_label_get_selectable:
5260 * @label: a #GtkLabel
5262 * Gets the value set by gtk_label_set_selectable().
5264 * Return value: %TRUE if the user can copy text from the label
5267 gtk_label_get_selectable (GtkLabel *label)
5269 GtkLabelPrivate *priv;
5271 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5275 return priv->select_info && priv->select_info->selectable;
5279 * gtk_label_set_angle:
5280 * @label: a #GtkLabel
5281 * @angle: the angle that the baseline of the label makes with
5282 * the horizontal, in degrees, measured counterclockwise
5284 * Sets the angle of rotation for the label. An angle of 90 reads from
5285 * from bottom to top, an angle of 270, from top to bottom. The angle
5286 * setting for the label is ignored if the label is selectable,
5287 * wrapped, or ellipsized.
5292 gtk_label_set_angle (GtkLabel *label,
5295 GtkLabelPrivate *priv;
5297 g_return_if_fail (GTK_IS_LABEL (label));
5301 /* Canonicalize to [0,360]. We don't canonicalize 360 to 0, because
5302 * double property ranges are inclusive, and changing 360 to 0 would
5303 * make a property editor behave strangely.
5305 if (angle < 0 || angle > 360.0)
5306 angle = angle - 360. * floor (angle / 360.);
5308 if (priv->angle != angle)
5310 priv->angle = angle;
5312 gtk_label_clear_layout (label);
5313 gtk_widget_queue_resize (GTK_WIDGET (label));
5315 g_object_notify (G_OBJECT (label), "angle");
5320 * gtk_label_get_angle:
5321 * @label: a #GtkLabel
5323 * Gets the angle of rotation for the label. See
5324 * gtk_label_set_angle().
5326 * Return value: the angle of rotation for the label
5331 gtk_label_get_angle (GtkLabel *label)
5333 g_return_val_if_fail (GTK_IS_LABEL (label), 0.0);
5335 return label->priv->angle;
5339 gtk_label_set_selection_text (GtkLabel *label,
5340 GtkSelectionData *selection_data)
5342 GtkLabelPrivate *priv = label->priv;
5344 if ((priv->select_info->selection_anchor !=
5345 priv->select_info->selection_end) &&
5351 start = MIN (priv->select_info->selection_anchor,
5352 priv->select_info->selection_end);
5353 end = MAX (priv->select_info->selection_anchor,
5354 priv->select_info->selection_end);
5356 len = strlen (priv->text);
5364 gtk_selection_data_set_text (selection_data,
5371 gtk_label_drag_data_get (GtkWidget *widget,
5372 GdkDragContext *context,
5373 GtkSelectionData *selection_data,
5377 gtk_label_set_selection_text (GTK_LABEL (widget), selection_data);
5381 get_text_callback (GtkClipboard *clipboard,
5382 GtkSelectionData *selection_data,
5384 gpointer user_data_or_owner)
5386 gtk_label_set_selection_text (GTK_LABEL (user_data_or_owner), selection_data);
5390 clear_text_callback (GtkClipboard *clipboard,
5391 gpointer user_data_or_owner)
5394 GtkLabelPrivate *priv;
5396 label = GTK_LABEL (user_data_or_owner);
5399 if (priv->select_info)
5401 priv->select_info->selection_anchor = priv->select_info->selection_end;
5403 gtk_widget_queue_draw (GTK_WIDGET (label));
5408 gtk_label_select_region_index (GtkLabel *label,
5412 GtkLabelPrivate *priv;
5414 g_return_if_fail (GTK_IS_LABEL (label));
5418 if (priv->select_info && priv->select_info->selectable)
5420 GtkClipboard *clipboard;
5422 if (priv->select_info->selection_anchor == anchor_index &&
5423 priv->select_info->selection_end == end_index)
5426 priv->select_info->selection_anchor = anchor_index;
5427 priv->select_info->selection_end = end_index;
5429 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label),
5430 GDK_SELECTION_PRIMARY);
5432 if (anchor_index != end_index)
5434 GtkTargetList *list;
5435 GtkTargetEntry *targets;
5438 list = gtk_target_list_new (NULL, 0);
5439 gtk_target_list_add_text_targets (list, 0);
5440 targets = gtk_target_table_new_from_list (list, &n_targets);
5442 gtk_clipboard_set_with_owner (clipboard,
5445 clear_text_callback,
5448 gtk_target_table_free (targets, n_targets);
5449 gtk_target_list_unref (list);
5453 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (label))
5454 gtk_clipboard_clear (clipboard);
5457 gtk_widget_queue_draw (GTK_WIDGET (label));
5459 g_object_freeze_notify (G_OBJECT (label));
5460 g_object_notify (G_OBJECT (label), "cursor-position");
5461 g_object_notify (G_OBJECT (label), "selection-bound");
5462 g_object_thaw_notify (G_OBJECT (label));
5467 * gtk_label_select_region:
5468 * @label: a #GtkLabel
5469 * @start_offset: start offset (in characters not bytes)
5470 * @end_offset: end offset (in characters not bytes)
5472 * Selects a range of characters in the label, if the label is selectable.
5473 * See gtk_label_set_selectable(). If the label is not selectable,
5474 * this function has no effect. If @start_offset or
5475 * @end_offset are -1, then the end of the label will be substituted.
5478 gtk_label_select_region (GtkLabel *label,
5482 GtkLabelPrivate *priv;
5484 g_return_if_fail (GTK_IS_LABEL (label));
5488 if (priv->text && priv->select_info)
5490 if (start_offset < 0)
5491 start_offset = g_utf8_strlen (priv->text, -1);
5494 end_offset = g_utf8_strlen (priv->text, -1);
5496 gtk_label_select_region_index (label,
5497 g_utf8_offset_to_pointer (priv->text, start_offset) - priv->text,
5498 g_utf8_offset_to_pointer (priv->text, end_offset) - priv->text);
5503 * gtk_label_get_selection_bounds:
5504 * @label: a #GtkLabel
5505 * @start: (out): return location for start of selection, as a character offset
5506 * @end: (out): return location for end of selection, as a character offset
5508 * Gets the selected range of characters in the label, returning %TRUE
5509 * if there's a selection.
5511 * Return value: %TRUE if selection is non-empty
5514 gtk_label_get_selection_bounds (GtkLabel *label,
5518 GtkLabelPrivate *priv;
5520 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5524 if (priv->select_info == NULL)
5526 /* not a selectable label */
5536 gint start_index, end_index;
5537 gint start_offset, end_offset;
5540 start_index = MIN (priv->select_info->selection_anchor,
5541 priv->select_info->selection_end);
5542 end_index = MAX (priv->select_info->selection_anchor,
5543 priv->select_info->selection_end);
5545 len = strlen (priv->text);
5547 if (end_index > len)
5550 if (start_index > len)
5553 start_offset = g_utf8_strlen (priv->text, start_index);
5554 end_offset = g_utf8_strlen (priv->text, end_index);
5556 if (start_offset > end_offset)
5558 gint tmp = start_offset;
5559 start_offset = end_offset;
5564 *start = start_offset;
5569 return start_offset != end_offset;
5575 * gtk_label_get_layout:
5576 * @label: a #GtkLabel
5578 * Gets the #PangoLayout used to display the label.
5579 * The layout is useful to e.g. convert text positions to
5580 * pixel positions, in combination with gtk_label_get_layout_offsets().
5581 * The returned layout is owned by the @label so need not be
5582 * freed by the caller. The @label is free to recreate its layout at
5583 * any time, so it should be considered read-only.
5585 * Return value: (transfer none): the #PangoLayout for this label
5588 gtk_label_get_layout (GtkLabel *label)
5590 GtkLabelPrivate *priv;
5592 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
5596 gtk_label_ensure_layout (label);
5598 return priv->layout;
5602 * gtk_label_get_layout_offsets:
5603 * @label: a #GtkLabel
5604 * @x: (out) (allow-none): location to store X offset of layout, or %NULL
5605 * @y: (out) (allow-none): location to store Y offset of layout, or %NULL
5607 * Obtains the coordinates where the label will draw the #PangoLayout
5608 * representing the text in the label; useful to convert mouse events
5609 * into coordinates inside the #PangoLayout, e.g. to take some action
5610 * if some part of the label is clicked. Of course you will need to
5611 * create a #GtkEventBox to receive the events, and pack the label
5612 * inside it, since labels are a #GTK_NO_WINDOW widget. Remember
5613 * when using the #PangoLayout functions you need to convert to
5614 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
5617 gtk_label_get_layout_offsets (GtkLabel *label,
5621 g_return_if_fail (GTK_IS_LABEL (label));
5623 gtk_label_ensure_layout (label);
5625 get_layout_location (label, x, y);
5629 * gtk_label_set_use_markup:
5630 * @label: a #GtkLabel
5631 * @setting: %TRUE if the label's text should be parsed for markup.
5633 * Sets whether the text of the label contains markup in <link
5634 * linkend="PangoMarkupFormat">Pango's text markup
5635 * language</link>. See gtk_label_set_markup().
5638 gtk_label_set_use_markup (GtkLabel *label,
5641 g_return_if_fail (GTK_IS_LABEL (label));
5643 gtk_label_set_use_markup_internal (label, setting);
5644 gtk_label_recalculate (label);
5648 * gtk_label_get_use_markup:
5649 * @label: a #GtkLabel
5651 * Returns whether the label's text is interpreted as marked up with
5652 * the <link linkend="PangoMarkupFormat">Pango text markup
5653 * language</link>. See gtk_label_set_use_markup ().
5655 * Return value: %TRUE if the label's text will be parsed for markup.
5658 gtk_label_get_use_markup (GtkLabel *label)
5660 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5662 return label->priv->use_markup;
5666 * gtk_label_set_use_underline:
5667 * @label: a #GtkLabel
5668 * @setting: %TRUE if underlines in the text indicate mnemonics
5670 * If true, an underline in the text indicates the next character should be
5671 * used for the mnemonic accelerator key.
5674 gtk_label_set_use_underline (GtkLabel *label,
5677 g_return_if_fail (GTK_IS_LABEL (label));
5679 gtk_label_set_use_underline_internal (label, setting);
5680 gtk_label_recalculate (label);
5684 * gtk_label_get_use_underline:
5685 * @label: a #GtkLabel
5687 * Returns whether an embedded underline in the label indicates a
5688 * mnemonic. See gtk_label_set_use_underline().
5690 * Return value: %TRUE whether an embedded underline in the label indicates
5691 * the mnemonic accelerator keys.
5694 gtk_label_get_use_underline (GtkLabel *label)
5696 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5698 return label->priv->use_underline;
5702 * gtk_label_set_single_line_mode:
5703 * @label: a #GtkLabel
5704 * @single_line_mode: %TRUE if the label should be in single line mode
5706 * Sets whether the label is in single line mode.
5711 gtk_label_set_single_line_mode (GtkLabel *label,
5712 gboolean single_line_mode)
5714 GtkLabelPrivate *priv;
5716 g_return_if_fail (GTK_IS_LABEL (label));
5720 single_line_mode = single_line_mode != FALSE;
5722 if (priv->single_line_mode != single_line_mode)
5724 priv->single_line_mode = single_line_mode;
5726 gtk_label_clear_layout (label);
5727 gtk_widget_queue_resize (GTK_WIDGET (label));
5729 g_object_notify (G_OBJECT (label), "single-line-mode");
5734 * gtk_label_get_single_line_mode:
5735 * @label: a #GtkLabel
5737 * Returns whether the label is in single line mode.
5739 * Return value: %TRUE when the label is in single line mode.
5744 gtk_label_get_single_line_mode (GtkLabel *label)
5746 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5748 return label->priv->single_line_mode;
5751 /* Compute the X position for an offset that corresponds to the "more important
5752 * cursor position for that offset. We use this when trying to guess to which
5753 * end of the selection we should go to when the user hits the left or
5757 get_better_cursor (GtkLabel *label,
5762 GtkLabelPrivate *priv = label->priv;
5763 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5764 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5765 PangoDirection cursor_direction = get_cursor_direction (label);
5766 gboolean split_cursor;
5767 PangoRectangle strong_pos, weak_pos;
5769 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5770 "gtk-split-cursor", &split_cursor,
5773 gtk_label_ensure_layout (label);
5775 pango_layout_get_cursor_pos (priv->layout, index,
5776 &strong_pos, &weak_pos);
5780 *x = strong_pos.x / PANGO_SCALE;
5781 *y = strong_pos.y / PANGO_SCALE;
5785 if (keymap_direction == cursor_direction)
5787 *x = strong_pos.x / PANGO_SCALE;
5788 *y = strong_pos.y / PANGO_SCALE;
5792 *x = weak_pos.x / PANGO_SCALE;
5793 *y = weak_pos.y / PANGO_SCALE;
5800 gtk_label_move_logically (GtkLabel *label,
5804 GtkLabelPrivate *priv = label->priv;
5805 gint offset = g_utf8_pointer_to_offset (priv->text,
5806 priv->text + start);
5810 PangoLogAttr *log_attrs;
5814 gtk_label_ensure_layout (label);
5816 length = g_utf8_strlen (priv->text, -1);
5818 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5820 while (count > 0 && offset < length)
5824 while (offset < length && !log_attrs[offset].is_cursor_position);
5828 while (count < 0 && offset > 0)
5832 while (offset > 0 && !log_attrs[offset].is_cursor_position);
5840 return g_utf8_offset_to_pointer (priv->text, offset) - priv->text;
5844 gtk_label_move_visually (GtkLabel *label,
5848 GtkLabelPrivate *priv = label->priv;
5855 int new_index, new_trailing;
5856 gboolean split_cursor;
5859 gtk_label_ensure_layout (label);
5861 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5862 "gtk-split-cursor", &split_cursor,
5869 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5870 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5872 strong = keymap_direction == get_cursor_direction (label);
5877 pango_layout_move_cursor_visually (priv->layout, strong, index, 0, 1, &new_index, &new_trailing);
5882 pango_layout_move_cursor_visually (priv->layout, strong, index, 0, -1, &new_index, &new_trailing);
5886 if (new_index < 0 || new_index == G_MAXINT)
5891 while (new_trailing--)
5892 index = g_utf8_next_char (priv->text + new_index) - priv->text;
5899 gtk_label_move_forward_word (GtkLabel *label,
5902 GtkLabelPrivate *priv = label->priv;
5903 gint new_pos = g_utf8_pointer_to_offset (priv->text,
5904 priv->text + start);
5907 length = g_utf8_strlen (priv->text, -1);
5908 if (new_pos < length)
5910 PangoLogAttr *log_attrs;
5913 gtk_label_ensure_layout (label);
5915 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5917 /* Find the next word end */
5919 while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
5925 return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
5930 gtk_label_move_backward_word (GtkLabel *label,
5933 GtkLabelPrivate *priv = label->priv;
5934 gint new_pos = g_utf8_pointer_to_offset (priv->text,
5935 priv->text + start);
5939 PangoLogAttr *log_attrs;
5942 gtk_label_ensure_layout (label);
5944 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5948 /* Find the previous word beginning */
5949 while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
5955 return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
5959 gtk_label_move_cursor (GtkLabel *label,
5960 GtkMovementStep step,
5962 gboolean extend_selection)
5964 GtkLabelPrivate *priv = label->priv;
5968 if (priv->select_info == NULL)
5971 old_pos = new_pos = priv->select_info->selection_end;
5973 if (priv->select_info->selection_end != priv->select_info->selection_anchor &&
5976 /* If we have a current selection and aren't extending it, move to the
5977 * start/or end of the selection as appropriate
5981 case GTK_MOVEMENT_VISUAL_POSITIONS:
5984 gint anchor_x, anchor_y;
5985 gboolean end_is_left;
5987 get_better_cursor (label, priv->select_info->selection_end, &end_x, &end_y);
5988 get_better_cursor (label, priv->select_info->selection_anchor, &anchor_x, &anchor_y);
5990 end_is_left = (end_y < anchor_y) || (end_y == anchor_y && end_x < anchor_x);
5993 new_pos = end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
5995 new_pos = !end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
5998 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5999 case GTK_MOVEMENT_WORDS:
6001 new_pos = MIN (priv->select_info->selection_end, priv->select_info->selection_anchor);
6003 new_pos = MAX (priv->select_info->selection_end, priv->select_info->selection_anchor);
6005 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
6006 case GTK_MOVEMENT_PARAGRAPH_ENDS:
6007 case GTK_MOVEMENT_BUFFER_ENDS:
6008 /* FIXME: Can do better here */
6009 new_pos = count < 0 ? 0 : strlen (priv->text);
6011 case GTK_MOVEMENT_DISPLAY_LINES:
6012 case GTK_MOVEMENT_PARAGRAPHS:
6013 case GTK_MOVEMENT_PAGES:
6014 case GTK_MOVEMENT_HORIZONTAL_PAGES:
6022 case GTK_MOVEMENT_LOGICAL_POSITIONS:
6023 new_pos = gtk_label_move_logically (label, new_pos, count);
6025 case GTK_MOVEMENT_VISUAL_POSITIONS:
6026 new_pos = gtk_label_move_visually (label, new_pos, count);
6027 if (new_pos == old_pos)
6029 if (!extend_selection)
6031 if (!gtk_widget_keynav_failed (GTK_WIDGET (label),
6033 GTK_DIR_RIGHT : GTK_DIR_LEFT))
6035 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
6038 gtk_widget_child_focus (toplevel,
6040 GTK_DIR_RIGHT : GTK_DIR_LEFT);
6045 gtk_widget_error_bell (GTK_WIDGET (label));
6049 case GTK_MOVEMENT_WORDS:
6052 new_pos = gtk_label_move_forward_word (label, new_pos);
6057 new_pos = gtk_label_move_backward_word (label, new_pos);
6060 if (new_pos == old_pos)
6061 gtk_widget_error_bell (GTK_WIDGET (label));
6063 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
6064 case GTK_MOVEMENT_PARAGRAPH_ENDS:
6065 case GTK_MOVEMENT_BUFFER_ENDS:
6066 /* FIXME: Can do better here */
6067 new_pos = count < 0 ? 0 : strlen (priv->text);
6068 if (new_pos == old_pos)
6069 gtk_widget_error_bell (GTK_WIDGET (label));
6071 case GTK_MOVEMENT_DISPLAY_LINES:
6072 case GTK_MOVEMENT_PARAGRAPHS:
6073 case GTK_MOVEMENT_PAGES:
6074 case GTK_MOVEMENT_HORIZONTAL_PAGES:
6079 if (extend_selection)
6080 gtk_label_select_region_index (label,
6081 priv->select_info->selection_anchor,
6084 gtk_label_select_region_index (label, new_pos, new_pos);
6088 gtk_label_copy_clipboard (GtkLabel *label)
6090 GtkLabelPrivate *priv = label->priv;
6092 if (priv->text && priv->select_info)
6096 GtkClipboard *clipboard;
6098 start = MIN (priv->select_info->selection_anchor,
6099 priv->select_info->selection_end);
6100 end = MAX (priv->select_info->selection_anchor,
6101 priv->select_info->selection_end);
6103 len = strlen (priv->text);
6111 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6114 gtk_clipboard_set_text (clipboard, priv->text + start, end - start);
6119 link = gtk_label_get_focus_link (label);
6121 gtk_clipboard_set_text (clipboard, link->uri, -1);
6127 gtk_label_select_all (GtkLabel *label)
6129 GtkLabelPrivate *priv = label->priv;
6131 gtk_label_select_region_index (label, 0, strlen (priv->text));
6134 /* Quick hack of a popup menu
6137 activate_cb (GtkWidget *menuitem,
6140 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6141 g_signal_emit_by_name (label, signal);
6145 append_action_signal (GtkLabel *label,
6147 const gchar *stock_id,
6148 const gchar *signal,
6151 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6153 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
6154 g_signal_connect (menuitem, "activate",
6155 G_CALLBACK (activate_cb), label);
6157 gtk_widget_set_sensitive (menuitem, sensitive);
6159 gtk_widget_show (menuitem);
6160 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6164 popup_menu_detach (GtkWidget *attach_widget,
6167 GtkLabel *label = GTK_LABEL (attach_widget);
6168 GtkLabelPrivate *priv = label->priv;
6170 if (priv->select_info)
6171 priv->select_info->popup_menu = NULL;
6175 popup_position_func (GtkMenu *menu,
6183 GtkAllocation allocation;
6187 label = GTK_LABEL (user_data);
6188 widget = GTK_WIDGET (label);
6190 g_return_if_fail (gtk_widget_get_realized (widget));
6192 screen = gtk_widget_get_screen (widget);
6193 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
6195 gtk_widget_get_allocation (widget, &allocation);
6200 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
6203 gtk_widget_get_allocation (widget, &allocation);
6205 *x += allocation.width / 2;
6206 *y += allocation.height;
6208 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
6209 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
6213 open_link_activate_cb (GtkMenuItem *menu_item,
6218 link = gtk_label_get_current_link (label);
6221 emit_activate_link (label, link);
6225 copy_link_activate_cb (GtkMenuItem *menu_item,
6228 GtkClipboard *clipboard;
6231 uri = gtk_label_get_current_uri (label);
6234 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6235 gtk_clipboard_set_text (clipboard, uri, -1);
6240 gtk_label_popup_menu (GtkWidget *widget)
6242 gtk_label_do_popup (GTK_LABEL (widget), NULL);
6248 gtk_label_do_popup (GtkLabel *label,
6249 GdkEventButton *event)
6251 GtkLabelPrivate *priv = label->priv;
6252 GtkWidget *menuitem;
6255 gboolean have_selection;
6258 if (!priv->select_info)
6261 if (priv->select_info->popup_menu)
6262 gtk_widget_destroy (priv->select_info->popup_menu);
6264 priv->select_info->popup_menu = menu = gtk_menu_new ();
6266 gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (label), popup_menu_detach);
6269 priv->select_info->selection_anchor != priv->select_info->selection_end;
6273 if (priv->select_info->link_clicked)
6274 link = priv->select_info->active_link;
6279 link = gtk_label_get_focus_link (label);
6281 if (!have_selection && link)
6284 menuitem = gtk_image_menu_item_new_with_mnemonic (_("_Open Link"));
6285 gtk_widget_show (menuitem);
6286 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6288 g_signal_connect (G_OBJECT (menuitem), "activate",
6289 G_CALLBACK (open_link_activate_cb), label);
6291 image = gtk_image_new_from_stock (GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_MENU);
6292 gtk_widget_show (image);
6293 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6295 /* Copy Link Address */
6296 menuitem = gtk_image_menu_item_new_with_mnemonic (_("Copy _Link Address"));
6297 gtk_widget_show (menuitem);
6298 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6300 g_signal_connect (G_OBJECT (menuitem), "activate",
6301 G_CALLBACK (copy_link_activate_cb), label);
6303 image = gtk_image_new_from_stock (GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
6304 gtk_widget_show (image);
6305 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6309 append_action_signal (label, menu, GTK_STOCK_CUT, "cut-clipboard", FALSE);
6310 append_action_signal (label, menu, GTK_STOCK_COPY, "copy-clipboard", have_selection);
6311 append_action_signal (label, menu, GTK_STOCK_PASTE, "paste-clipboard", FALSE);
6313 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
6314 gtk_widget_set_sensitive (menuitem, FALSE);
6315 gtk_widget_show (menuitem);
6316 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6318 menuitem = gtk_separator_menu_item_new ();
6319 gtk_widget_show (menuitem);
6320 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6322 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
6323 g_signal_connect_swapped (menuitem, "activate",
6324 G_CALLBACK (gtk_label_select_all), label);
6325 gtk_widget_show (menuitem);
6326 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6329 g_signal_emit (label, signals[POPULATE_POPUP], 0, menu);
6332 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6334 event->button, event->time);
6337 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6338 popup_position_func, label,
6339 0, gtk_get_current_event_time ());
6340 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
6345 gtk_label_clear_links (GtkLabel *label)
6347 GtkLabelPrivate *priv = label->priv;
6349 if (!priv->select_info)
6352 g_list_foreach (priv->select_info->links, (GFunc)link_free, NULL);
6353 g_list_free (priv->select_info->links);
6354 priv->select_info->links = NULL;
6355 priv->select_info->active_link = NULL;
6359 gtk_label_rescan_links (GtkLabel *label)
6361 GtkLabelPrivate *priv = label->priv;
6362 PangoLayout *layout = priv->layout;
6363 PangoAttrList *attlist;
6364 PangoAttrIterator *iter;
6367 if (!priv->select_info || !priv->select_info->links)
6370 attlist = pango_layout_get_attributes (layout);
6372 if (attlist == NULL)
6375 iter = pango_attr_list_get_iterator (attlist);
6377 links = priv->select_info->links;
6381 PangoAttribute *underline;
6382 PangoAttribute *color;
6384 underline = pango_attr_iterator_get (iter, PANGO_ATTR_UNDERLINE);
6385 color = pango_attr_iterator_get (iter, PANGO_ATTR_FOREGROUND);
6387 if (underline != NULL && color != NULL)
6390 PangoRectangle start_pos;
6391 PangoRectangle end_pos;
6394 pango_attr_iterator_range (iter, &start, &end);
6395 pango_layout_index_to_pos (layout, start, &start_pos);
6396 pango_layout_index_to_pos (layout, end, &end_pos);
6400 g_warning ("Ran out of links");
6404 links = links->next;
6405 link->start = start;
6408 } while (pango_attr_iterator_next (iter));
6410 pango_attr_iterator_destroy (iter);
6414 gtk_label_activate_link (GtkLabel *label,
6417 GtkWidget *widget = GTK_WIDGET (label);
6418 GError *error = NULL;
6420 if (!gtk_show_uri (gtk_widget_get_screen (widget),
6421 uri, gtk_get_current_event_time (), &error))
6423 g_warning ("Unable to show '%s': %s", uri, error->message);
6424 g_error_free (error);
6431 emit_activate_link (GtkLabel *label,
6434 GtkLabelPrivate *priv = label->priv;
6437 g_signal_emit (label, signals[ACTIVATE_LINK], 0, link->uri, &handled);
6438 if (handled && priv->track_links && !link->visited)
6440 link->visited = TRUE;
6441 /* FIXME: shouldn't have to redo everything here */
6442 gtk_label_recalculate (label);
6447 gtk_label_activate_current_link (GtkLabel *label)
6450 GtkWidget *widget = GTK_WIDGET (label);
6452 link = gtk_label_get_focus_link (label);
6456 emit_activate_link (label, link);
6460 GtkWidget *toplevel;
6462 GtkWidget *default_widget, *focus_widget;
6464 toplevel = gtk_widget_get_toplevel (widget);
6465 if (GTK_IS_WINDOW (toplevel))
6467 window = GTK_WINDOW (toplevel);
6471 default_widget = gtk_window_get_default_widget (window);
6472 focus_widget = gtk_window_get_focus (window);
6474 if (default_widget != widget &&
6475 !(widget == focus_widget && (!default_widget || !gtk_widget_is_sensitive (default_widget))))
6476 gtk_window_activate_default (window);
6482 static GtkLabelLink *
6483 gtk_label_get_current_link (GtkLabel *label)
6485 GtkLabelPrivate *priv = label->priv;
6488 if (!priv->select_info)
6491 if (priv->select_info->link_clicked)
6492 link = priv->select_info->active_link;
6494 link = gtk_label_get_focus_link (label);
6500 * gtk_label_get_current_uri:
6501 * @label: a #GtkLabel
6503 * Returns the URI for the currently active link in the label.
6504 * The active link is the one under the mouse pointer or, in a
6505 * selectable label, the link in which the text cursor is currently
6508 * This function is intended for use in a #GtkLabel::activate-link handler
6509 * or for use in a #GtkWidget::query-tooltip handler.
6511 * Returns: the currently active URI. The string is owned by GTK+ and must
6512 * not be freed or modified.
6516 G_CONST_RETURN gchar *
6517 gtk_label_get_current_uri (GtkLabel *label)
6521 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
6523 link = gtk_label_get_current_link (label);
6532 * gtk_label_set_track_visited_links:
6533 * @label: a #GtkLabel
6534 * @track_links: %TRUE to track visited links
6536 * Sets whether the label should keep track of clicked
6537 * links (and use a different color for them).
6542 gtk_label_set_track_visited_links (GtkLabel *label,
6543 gboolean track_links)
6545 GtkLabelPrivate *priv;
6547 g_return_if_fail (GTK_IS_LABEL (label));
6551 track_links = track_links != FALSE;
6553 if (priv->track_links != track_links)
6555 priv->track_links = track_links;
6557 /* FIXME: shouldn't have to redo everything here */
6558 gtk_label_recalculate (label);
6560 g_object_notify (G_OBJECT (label), "track-visited-links");
6565 * gtk_label_get_track_visited_links:
6566 * @label: a #GtkLabel
6568 * Returns whether the label is currently keeping track
6571 * Returns: %TRUE if clicked links are remembered
6576 gtk_label_get_track_visited_links (GtkLabel *label)
6578 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
6580 return label->priv->track_links;
6584 gtk_label_query_tooltip (GtkWidget *widget,
6587 gboolean keyboard_tip,
6588 GtkTooltip *tooltip)
6590 GtkLabel *label = GTK_LABEL (widget);
6591 GtkLabelPrivate *priv = label->priv;
6592 GtkLabelSelectionInfo *info = priv->select_info;
6596 if (info && info->links)
6600 if (info->selection_anchor == info->selection_end)
6601 index = info->selection_anchor;
6605 if (!get_layout_index (label, x, y, &index))
6611 for (l = info->links; l != NULL; l = l->next)
6613 GtkLabelLink *link = l->data;
6614 if (index >= link->start && index <= link->end)
6618 gtk_tooltip_set_markup (tooltip, link->title);
6627 return GTK_WIDGET_CLASS (gtk_label_parent_class)->query_tooltip (widget,