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, PANGO_TYPE_UNDERLINE, value, &val, NULL))
1350 attribute = pango_attr_underline_new (g_value_get_enum (&val));
1353 /* XXX: allow boolean for backwards compat, so ignore error */
1354 /* Deprecate this somehow */
1355 g_value_unset (&val);
1356 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1357 attribute = pango_attr_underline_new (g_value_get_boolean (&val));
1360 case PANGO_ATTR_STRIKETHROUGH:
1361 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1362 attribute = pango_attr_strikethrough_new (g_value_get_boolean (&val));
1364 case PANGO_ATTR_GRAVITY:
1365 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY, value, &val, error))
1366 attribute = pango_attr_gravity_new (g_value_get_enum (&val));
1368 case PANGO_ATTR_GRAVITY_HINT:
1369 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY_HINT,
1370 value, &val, error))
1371 attribute = pango_attr_gravity_hint_new (g_value_get_enum (&val));
1373 /* PangoAttrString */
1374 case PANGO_ATTR_FAMILY:
1375 attribute = pango_attr_family_new (value);
1376 g_value_init (&val, G_TYPE_INT);
1380 case PANGO_ATTR_SIZE:
1381 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1382 value, &val, error))
1383 attribute = pango_attr_size_new (g_value_get_int (&val));
1385 case PANGO_ATTR_ABSOLUTE_SIZE:
1386 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1387 value, &val, error))
1388 attribute = pango_attr_size_new_absolute (g_value_get_int (&val));
1391 /* PangoAttrFontDesc */
1392 case PANGO_ATTR_FONT_DESC:
1393 if ((font_desc = pango_font_description_from_string (value)))
1395 attribute = pango_attr_font_desc_new (font_desc);
1396 pango_font_description_free (font_desc);
1397 g_value_init (&val, G_TYPE_INT);
1401 /* PangoAttrColor */
1402 case PANGO_ATTR_FOREGROUND:
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_foreground_new (color->red, color->green, color->blue);
1410 case PANGO_ATTR_BACKGROUND:
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_background_new (color->red, color->green, color->blue);
1418 case PANGO_ATTR_UNDERLINE_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_underline_color_new (color->red, color->green, color->blue);
1426 case PANGO_ATTR_STRIKETHROUGH_COLOR:
1427 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1428 value, &val, error))
1430 color = g_value_get_boxed (&val);
1431 attribute = pango_attr_strikethrough_color_new (color->red, color->green, color->blue);
1435 /* PangoAttrShape */
1436 case PANGO_ATTR_SHAPE:
1437 /* Unsupported for now */
1439 /* PangoAttrFloat */
1440 case PANGO_ATTR_SCALE:
1441 if (gtk_builder_value_from_string_type (builder, G_TYPE_DOUBLE,
1442 value, &val, error))
1443 attribute = pango_attr_scale_new (g_value_get_double (&val));
1446 case PANGO_ATTR_INVALID:
1447 case PANGO_ATTR_LETTER_SPACING:
1448 case PANGO_ATTR_RISE:
1449 case PANGO_ATTR_FALLBACK:
1454 g_value_unset (&val);
1461 pango_start_element (GMarkupParseContext *context,
1462 const gchar *element_name,
1463 const gchar **names,
1464 const gchar **values,
1468 PangoParserData *data = (PangoParserData*)user_data;
1469 GValue val = { 0, };
1471 gint line_number, char_number;
1473 if (strcmp (element_name, "attribute") == 0)
1475 PangoAttribute *attr = NULL;
1476 const gchar *name = NULL;
1477 const gchar *value = NULL;
1478 const gchar *start = NULL;
1479 const gchar *end = NULL;
1480 guint start_val = 0;
1481 guint end_val = G_MAXUINT;
1483 for (i = 0; names[i]; i++)
1485 if (strcmp (names[i], "name") == 0)
1487 else if (strcmp (names[i], "value") == 0)
1489 else if (strcmp (names[i], "start") == 0)
1491 else if (strcmp (names[i], "end") == 0)
1495 g_markup_parse_context_get_position (context,
1500 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1501 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1503 line_number, char_number, names[i], "attribute");
1508 if (!name || !value)
1510 g_markup_parse_context_get_position (context,
1515 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1516 "%s:%d:%d <%s> requires attribute \"%s\"",
1518 line_number, char_number, "attribute",
1519 name ? "value" : "name");
1525 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1526 start, &val, error))
1528 start_val = g_value_get_uint (&val);
1529 g_value_unset (&val);
1534 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1537 end_val = g_value_get_uint (&val);
1538 g_value_unset (&val);
1541 attr = attribute_from_text (data->builder, name, value, error);
1545 attr->start_index = start_val;
1546 attr->end_index = end_val;
1549 data->attrs = pango_attr_list_new ();
1551 pango_attr_list_insert (data->attrs, attr);
1554 else if (strcmp (element_name, "attributes") == 0)
1557 g_warning ("Unsupported tag for GtkLabel: %s\n", element_name);
1560 static const GMarkupParser pango_parser =
1562 pango_start_element,
1566 gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
1567 GtkBuilder *builder,
1569 const gchar *tagname,
1570 GMarkupParser *parser,
1573 if (buildable_parent_iface->custom_tag_start (buildable, builder, child,
1574 tagname, parser, data))
1577 if (strcmp (tagname, "attributes") == 0)
1579 PangoParserData *parser_data;
1581 parser_data = g_slice_new0 (PangoParserData);
1582 parser_data->builder = g_object_ref (builder);
1583 parser_data->object = g_object_ref (buildable);
1584 *parser = pango_parser;
1585 *data = parser_data;
1592 gtk_label_buildable_custom_finished (GtkBuildable *buildable,
1593 GtkBuilder *builder,
1595 const gchar *tagname,
1598 PangoParserData *data;
1600 buildable_parent_iface->custom_finished (buildable, builder, child,
1601 tagname, user_data);
1603 if (strcmp (tagname, "attributes") == 0)
1605 data = (PangoParserData*)user_data;
1609 gtk_label_set_attributes (GTK_LABEL (buildable), data->attrs);
1610 pango_attr_list_unref (data->attrs);
1613 g_object_unref (data->object);
1614 g_object_unref (data->builder);
1615 g_slice_free (PangoParserData, data);
1622 * @str: The text of the label
1624 * Creates a new label with the given text inside it. You can
1625 * pass %NULL to get an empty label widget.
1627 * Return value: the new #GtkLabel
1630 gtk_label_new (const gchar *str)
1634 label = g_object_new (GTK_TYPE_LABEL, NULL);
1637 gtk_label_set_text (label, str);
1639 return GTK_WIDGET (label);
1643 * gtk_label_new_with_mnemonic:
1644 * @str: The text of the label, with an underscore in front of the
1645 * mnemonic character
1647 * Creates a new #GtkLabel, containing the text in @str.
1649 * If characters in @str are preceded by an underscore, they are
1650 * underlined. If you need a literal underscore character in a label, use
1651 * '__' (two underscores). The first underlined character represents a
1652 * keyboard accelerator called a mnemonic. The mnemonic key can be used
1653 * to activate another widget, chosen automatically, or explicitly using
1654 * gtk_label_set_mnemonic_widget().
1656 * If gtk_label_set_mnemonic_widget() is not called, then the first
1657 * activatable ancestor of the #GtkLabel will be chosen as the mnemonic
1658 * widget. For instance, if the label is inside a button or menu item,
1659 * the button or menu item will automatically become the mnemonic widget
1660 * and be activated by the mnemonic.
1662 * Return value: the new #GtkLabel
1665 gtk_label_new_with_mnemonic (const gchar *str)
1669 label = g_object_new (GTK_TYPE_LABEL, NULL);
1672 gtk_label_set_text_with_mnemonic (label, str);
1674 return GTK_WIDGET (label);
1678 gtk_label_mnemonic_activate (GtkWidget *widget,
1679 gboolean group_cycling)
1681 GtkLabel *label = GTK_LABEL (widget);
1682 GtkLabelPrivate *priv = label->priv;
1685 if (priv->mnemonic_widget)
1686 return gtk_widget_mnemonic_activate (priv->mnemonic_widget, group_cycling);
1688 /* Try to find the widget to activate by traversing the
1689 * widget's ancestry.
1691 parent = gtk_widget_get_parent (widget);
1693 if (GTK_IS_NOTEBOOK (parent))
1698 if (gtk_widget_get_can_focus (parent) ||
1699 (!group_cycling && GTK_WIDGET_GET_CLASS (parent)->activate_signal) ||
1700 GTK_IS_NOTEBOOK (gtk_widget_get_parent (parent)) ||
1701 GTK_IS_MENU_ITEM (parent))
1702 return gtk_widget_mnemonic_activate (parent, group_cycling);
1703 parent = gtk_widget_get_parent (parent);
1706 /* barf if there was nothing to activate */
1707 g_warning ("Couldn't find a target for a mnemonic activation.");
1708 gtk_widget_error_bell (widget);
1714 gtk_label_setup_mnemonic (GtkLabel *label,
1717 GtkLabelPrivate *priv = label->priv;
1718 GtkWidget *widget = GTK_WIDGET (label);
1719 GtkWidget *toplevel;
1720 GtkWidget *mnemonic_menu;
1722 mnemonic_menu = g_object_get_data (G_OBJECT (label), "gtk-mnemonic-menu");
1724 if (last_key != GDK_KEY_VoidSymbol)
1726 if (priv->mnemonic_window)
1728 gtk_window_remove_mnemonic (priv->mnemonic_window,
1731 priv->mnemonic_window = NULL;
1735 _gtk_menu_shell_remove_mnemonic (GTK_MENU_SHELL (mnemonic_menu),
1738 mnemonic_menu = NULL;
1742 if (priv->mnemonic_keyval == GDK_KEY_VoidSymbol)
1745 connect_mnemonics_visible_notify (GTK_LABEL (widget));
1747 toplevel = gtk_widget_get_toplevel (widget);
1748 if (gtk_widget_is_toplevel (toplevel))
1750 GtkWidget *menu_shell;
1752 menu_shell = gtk_widget_get_ancestor (widget,
1753 GTK_TYPE_MENU_SHELL);
1757 _gtk_menu_shell_add_mnemonic (GTK_MENU_SHELL (menu_shell),
1758 priv->mnemonic_keyval,
1760 mnemonic_menu = menu_shell;
1763 if (!GTK_IS_MENU (menu_shell))
1765 gtk_window_add_mnemonic (GTK_WINDOW (toplevel),
1766 priv->mnemonic_keyval,
1768 priv->mnemonic_window = GTK_WINDOW (toplevel);
1773 g_object_set_data (G_OBJECT (label), I_("gtk-mnemonic-menu"), mnemonic_menu);
1777 gtk_label_hierarchy_changed (GtkWidget *widget,
1778 GtkWidget *old_toplevel)
1780 GtkLabel *label = GTK_LABEL (widget);
1781 GtkLabelPrivate *priv = label->priv;
1783 gtk_label_setup_mnemonic (label, priv->mnemonic_keyval);
1787 label_shortcut_setting_apply (GtkLabel *label)
1789 gtk_label_recalculate (label);
1790 if (GTK_IS_ACCEL_LABEL (label))
1791 gtk_accel_label_refetch (GTK_ACCEL_LABEL (label));
1795 label_shortcut_setting_traverse_container (GtkWidget *widget,
1798 if (GTK_IS_LABEL (widget))
1799 label_shortcut_setting_apply (GTK_LABEL (widget));
1800 else if (GTK_IS_CONTAINER (widget))
1801 gtk_container_forall (GTK_CONTAINER (widget),
1802 label_shortcut_setting_traverse_container, data);
1806 label_shortcut_setting_changed (GtkSettings *settings)
1810 list = gtk_window_list_toplevels ();
1812 for (l = list; l ; l = l->next)
1814 GtkWidget *widget = l->data;
1816 if (gtk_widget_get_settings (widget) == settings)
1817 gtk_container_forall (GTK_CONTAINER (widget),
1818 label_shortcut_setting_traverse_container, NULL);
1825 mnemonics_visible_apply (GtkWidget *widget,
1826 gboolean mnemonics_visible)
1828 GtkLabel *label = GTK_LABEL (widget);
1829 GtkLabelPrivate *priv = label->priv;
1831 mnemonics_visible = mnemonics_visible != FALSE;
1833 if (priv->mnemonics_visible != mnemonics_visible)
1835 priv->mnemonics_visible = mnemonics_visible;
1837 gtk_label_recalculate (label);
1842 label_mnemonics_visible_traverse_container (GtkWidget *widget,
1845 gboolean mnemonics_visible = GPOINTER_TO_INT (data);
1847 _gtk_label_mnemonics_visible_apply_recursively (widget, mnemonics_visible);
1851 _gtk_label_mnemonics_visible_apply_recursively (GtkWidget *widget,
1852 gboolean mnemonics_visible)
1854 if (GTK_IS_LABEL (widget))
1855 mnemonics_visible_apply (widget, mnemonics_visible);
1856 else if (GTK_IS_CONTAINER (widget))
1857 gtk_container_forall (GTK_CONTAINER (widget),
1858 label_mnemonics_visible_traverse_container,
1859 GINT_TO_POINTER (mnemonics_visible));
1863 label_mnemonics_visible_changed (GtkWindow *window,
1867 gboolean mnemonics_visible;
1869 g_object_get (window, "mnemonics-visible", &mnemonics_visible, NULL);
1871 gtk_container_forall (GTK_CONTAINER (window),
1872 label_mnemonics_visible_traverse_container,
1873 GINT_TO_POINTER (mnemonics_visible));
1877 gtk_label_screen_changed (GtkWidget *widget,
1878 GdkScreen *old_screen)
1880 GtkSettings *settings;
1881 gboolean shortcuts_connected;
1883 if (!gtk_widget_has_screen (widget))
1886 settings = gtk_widget_get_settings (widget);
1888 shortcuts_connected =
1889 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (settings),
1890 "gtk-label-shortcuts-connected"));
1892 if (! shortcuts_connected)
1894 g_signal_connect (settings, "notify::gtk-enable-mnemonics",
1895 G_CALLBACK (label_shortcut_setting_changed),
1897 g_signal_connect (settings, "notify::gtk-enable-accels",
1898 G_CALLBACK (label_shortcut_setting_changed),
1901 g_object_set_data (G_OBJECT (settings), "gtk-label-shortcuts-connected",
1902 GINT_TO_POINTER (TRUE));
1905 label_shortcut_setting_apply (GTK_LABEL (widget));
1910 label_mnemonic_widget_weak_notify (gpointer data,
1911 GObject *where_the_object_was)
1913 GtkLabel *label = data;
1914 GtkLabelPrivate *priv = label->priv;
1916 priv->mnemonic_widget = NULL;
1917 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1921 * gtk_label_set_mnemonic_widget:
1922 * @label: a #GtkLabel
1923 * @widget: (allow-none): the target #GtkWidget
1925 * If the label has been set so that it has an mnemonic key (using
1926 * i.e. gtk_label_set_markup_with_mnemonic(),
1927 * gtk_label_set_text_with_mnemonic(), gtk_label_new_with_mnemonic()
1928 * or the "use_underline" property) the label can be associated with a
1929 * widget that is the target of the mnemonic. When the label is inside
1930 * a widget (like a #GtkButton or a #GtkNotebook tab) it is
1931 * automatically associated with the correct widget, but sometimes
1932 * (i.e. when the target is a #GtkEntry next to the label) you need to
1933 * set it explicitly using this function.
1935 * The target widget will be accelerated by emitting the
1936 * GtkWidget::mnemonic-activate signal on it. The default handler for
1937 * this signal will activate the widget if there are no mnemonic collisions
1938 * and toggle focus between the colliding widgets otherwise.
1941 gtk_label_set_mnemonic_widget (GtkLabel *label,
1944 GtkLabelPrivate *priv;
1946 g_return_if_fail (GTK_IS_LABEL (label));
1951 g_return_if_fail (GTK_IS_WIDGET (widget));
1953 if (priv->mnemonic_widget)
1955 gtk_widget_remove_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1956 g_object_weak_unref (G_OBJECT (priv->mnemonic_widget),
1957 label_mnemonic_widget_weak_notify,
1960 priv->mnemonic_widget = widget;
1961 if (priv->mnemonic_widget)
1963 g_object_weak_ref (G_OBJECT (priv->mnemonic_widget),
1964 label_mnemonic_widget_weak_notify,
1966 gtk_widget_add_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1969 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1973 * gtk_label_get_mnemonic_widget:
1974 * @label: a #GtkLabel
1976 * Retrieves the target of the mnemonic (keyboard shortcut) of this
1977 * label. See gtk_label_set_mnemonic_widget().
1979 * Return value: (transfer none): the target of the label's mnemonic,
1980 * or %NULL if none has been set and the default algorithm will be used.
1983 gtk_label_get_mnemonic_widget (GtkLabel *label)
1985 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1987 return label->priv->mnemonic_widget;
1991 * gtk_label_get_mnemonic_keyval:
1992 * @label: a #GtkLabel
1994 * If the label has been set so that it has an mnemonic key this function
1995 * returns the keyval used for the mnemonic accelerator. If there is no
1996 * mnemonic set up it returns #GDK_VoidSymbol.
1998 * Returns: GDK keyval usable for accelerators, or #GDK_VoidSymbol
2001 gtk_label_get_mnemonic_keyval (GtkLabel *label)
2003 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_KEY_VoidSymbol);
2005 return label->priv->mnemonic_keyval;
2009 gtk_label_set_text_internal (GtkLabel *label,
2012 GtkLabelPrivate *priv = label->priv;
2014 g_free (priv->text);
2018 gtk_label_select_region_index (label, 0, 0);
2022 gtk_label_set_label_internal (GtkLabel *label,
2025 GtkLabelPrivate *priv = label->priv;
2027 g_free (priv->label);
2031 g_object_notify (G_OBJECT (label), "label");
2035 gtk_label_set_use_markup_internal (GtkLabel *label,
2038 GtkLabelPrivate *priv = label->priv;
2041 if (priv->use_markup != val)
2043 priv->use_markup = val;
2045 g_object_notify (G_OBJECT (label), "use-markup");
2050 gtk_label_set_use_underline_internal (GtkLabel *label,
2053 GtkLabelPrivate *priv = label->priv;
2056 if (priv->use_underline != val)
2058 priv->use_underline = val;
2060 g_object_notify (G_OBJECT (label), "use-underline");
2065 gtk_label_compose_effective_attrs (GtkLabel *label)
2067 GtkLabelPrivate *priv = label->priv;
2068 PangoAttrIterator *iter;
2069 PangoAttribute *attr;
2070 GSList *iter_attrs, *l;
2074 if (priv->effective_attrs)
2076 if ((iter = pango_attr_list_get_iterator (priv->attrs)))
2080 iter_attrs = pango_attr_iterator_get_attrs (iter);
2081 for (l = iter_attrs; l; l = l->next)
2084 pango_attr_list_insert (priv->effective_attrs, attr);
2086 g_slist_free (iter_attrs);
2088 while (pango_attr_iterator_next (iter));
2089 pango_attr_iterator_destroy (iter);
2093 priv->effective_attrs =
2094 pango_attr_list_ref (priv->attrs);
2099 gtk_label_set_attributes_internal (GtkLabel *label,
2100 PangoAttrList *attrs)
2102 GtkLabelPrivate *priv = label->priv;
2105 pango_attr_list_ref (attrs);
2108 pango_attr_list_unref (priv->attrs);
2109 priv->attrs = attrs;
2111 g_object_notify (G_OBJECT (label), "attributes");
2115 /* Calculates text, attrs and mnemonic_keyval from
2116 * label, use_underline and use_markup
2119 gtk_label_recalculate (GtkLabel *label)
2121 GtkLabelPrivate *priv = label->priv;
2122 guint keyval = priv->mnemonic_keyval;
2124 if (priv->use_markup)
2125 gtk_label_set_markup_internal (label, priv->label, priv->use_underline);
2128 if (priv->use_underline)
2129 gtk_label_set_uline_text_internal (label, priv->label);
2132 if (priv->effective_attrs)
2133 pango_attr_list_unref (priv->effective_attrs);
2134 priv->effective_attrs = NULL;
2135 gtk_label_set_text_internal (label, g_strdup (priv->label));
2139 gtk_label_compose_effective_attrs (label);
2141 if (!priv->use_underline)
2142 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
2144 if (keyval != priv->mnemonic_keyval)
2146 gtk_label_setup_mnemonic (label, keyval);
2147 g_object_notify (G_OBJECT (label), "mnemonic-keyval");
2150 gtk_label_clear_layout (label);
2151 gtk_label_clear_select_info (label);
2152 gtk_widget_queue_resize (GTK_WIDGET (label));
2156 * gtk_label_set_text:
2157 * @label: a #GtkLabel
2158 * @str: The text you want to set
2160 * Sets the text within the #GtkLabel widget. It overwrites any text that
2163 * This will also clear any previously set mnemonic accelerators.
2166 gtk_label_set_text (GtkLabel *label,
2169 g_return_if_fail (GTK_IS_LABEL (label));
2171 g_object_freeze_notify (G_OBJECT (label));
2173 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2174 gtk_label_set_use_markup_internal (label, FALSE);
2175 gtk_label_set_use_underline_internal (label, FALSE);
2177 gtk_label_recalculate (label);
2179 g_object_thaw_notify (G_OBJECT (label));
2183 * gtk_label_set_attributes:
2184 * @label: a #GtkLabel
2185 * @attrs: a #PangoAttrList
2187 * Sets a #PangoAttrList; the attributes in the list are applied to the
2190 * <note><para>The attributes set with this function will be applied
2191 * and merged with any other attributes previously effected by way
2192 * of the #GtkLabel:use-underline or #GtkLabel:use-markup properties.
2193 * While it is not recommended to mix markup strings with manually set
2194 * attributes, if you must; know that the attributes will be applied
2195 * to the label after the markup string is parsed.</para></note>
2198 gtk_label_set_attributes (GtkLabel *label,
2199 PangoAttrList *attrs)
2201 g_return_if_fail (GTK_IS_LABEL (label));
2203 gtk_label_set_attributes_internal (label, attrs);
2205 gtk_label_recalculate (label);
2207 gtk_label_clear_layout (label);
2208 gtk_widget_queue_resize (GTK_WIDGET (label));
2212 * gtk_label_get_attributes:
2213 * @label: a #GtkLabel
2215 * Gets the attribute list that was set on the label using
2216 * gtk_label_set_attributes(), if any. This function does
2217 * not reflect attributes that come from the labels markup
2218 * (see gtk_label_set_markup()). If you want to get the
2219 * effective attributes for the label, use
2220 * pango_layout_get_attribute (gtk_label_get_layout (label)).
2222 * Return value: (transfer none): the attribute list, or %NULL
2226 gtk_label_get_attributes (GtkLabel *label)
2228 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2230 return label->priv->attrs;
2234 * gtk_label_set_label:
2235 * @label: a #GtkLabel
2236 * @str: the new text to set for the label
2238 * Sets the text of the label. The label is interpreted as
2239 * including embedded underlines and/or Pango markup depending
2240 * on the values of the #GtkLabel:use-underline" and
2241 * #GtkLabel:use-markup properties.
2244 gtk_label_set_label (GtkLabel *label,
2247 g_return_if_fail (GTK_IS_LABEL (label));
2249 g_object_freeze_notify (G_OBJECT (label));
2251 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2252 gtk_label_recalculate (label);
2254 g_object_thaw_notify (G_OBJECT (label));
2258 * gtk_label_get_label:
2259 * @label: a #GtkLabel
2261 * Fetches the text from a label widget including any embedded
2262 * underlines indicating mnemonics and Pango markup. (See
2263 * gtk_label_get_text()).
2265 * Return value: the text of the label widget. This string is
2266 * owned by the widget and must not be modified or freed.
2268 G_CONST_RETURN gchar *
2269 gtk_label_get_label (GtkLabel *label)
2271 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2273 return label->priv->label;
2281 GdkColor *link_color;
2282 GdkColor *visited_link_color;
2286 start_element_handler (GMarkupParseContext *context,
2287 const gchar *element_name,
2288 const gchar **attribute_names,
2289 const gchar **attribute_values,
2293 GtkLabelPrivate *priv;
2294 UriParserData *pdata = user_data;
2296 if (strcmp (element_name, "a") == 0)
2299 const gchar *uri = NULL;
2300 const gchar *title = NULL;
2301 gboolean visited = FALSE;
2305 GdkColor *color = NULL;
2307 g_markup_parse_context_get_position (context, &line_number, &char_number);
2309 for (i = 0; attribute_names[i] != NULL; i++)
2311 const gchar *attr = attribute_names[i];
2313 if (strcmp (attr, "href") == 0)
2314 uri = attribute_values[i];
2315 else if (strcmp (attr, "title") == 0)
2316 title = attribute_values[i];
2321 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
2322 "Attribute '%s' is not allowed on the <a> tag "
2323 "on line %d char %d",
2324 attr, line_number, char_number);
2333 G_MARKUP_ERROR_INVALID_CONTENT,
2334 "Attribute 'href' was missing on the <a> tag "
2335 "on line %d char %d",
2336 line_number, char_number);
2341 priv = pdata->label->priv;
2342 if (priv->track_links && priv->select_info)
2345 for (l = priv->select_info->links; l; l = l->next)
2348 if (strcmp (uri, link->uri) == 0)
2350 visited = link->visited;
2357 color = pdata->visited_link_color;
2359 color = pdata->link_color;
2361 g_string_append_printf (pdata->new_str,
2362 "<span color=\"#%04x%04x%04x\" underline=\"single\">",
2367 link = g_new0 (GtkLabelLink, 1);
2368 link->uri = g_strdup (uri);
2369 link->title = g_strdup (title);
2370 link->visited = visited;
2371 pdata->links = g_list_append (pdata->links, link);
2377 g_string_append_c (pdata->new_str, '<');
2378 g_string_append (pdata->new_str, element_name);
2380 for (i = 0; attribute_names[i] != NULL; i++)
2382 const gchar *attr = attribute_names[i];
2383 const gchar *value = attribute_values[i];
2386 newvalue = g_markup_escape_text (value, -1);
2388 g_string_append_c (pdata->new_str, ' ');
2389 g_string_append (pdata->new_str, attr);
2390 g_string_append (pdata->new_str, "=\"");
2391 g_string_append (pdata->new_str, newvalue);
2392 g_string_append_c (pdata->new_str, '\"');
2396 g_string_append_c (pdata->new_str, '>');
2401 end_element_handler (GMarkupParseContext *context,
2402 const gchar *element_name,
2406 UriParserData *pdata = user_data;
2408 if (!strcmp (element_name, "a"))
2409 g_string_append (pdata->new_str, "</span>");
2412 g_string_append (pdata->new_str, "</");
2413 g_string_append (pdata->new_str, element_name);
2414 g_string_append_c (pdata->new_str, '>');
2419 text_handler (GMarkupParseContext *context,
2425 UriParserData *pdata = user_data;
2428 newtext = g_markup_escape_text (text, text_len);
2429 g_string_append (pdata->new_str, newtext);
2433 static const GMarkupParser markup_parser =
2435 start_element_handler,
2436 end_element_handler,
2443 xml_isspace (gchar c)
2445 return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
2449 link_free (GtkLabelLink *link)
2452 g_free (link->title);
2457 gtk_label_get_link_colors (GtkWidget *widget,
2458 GdkColor **link_color,
2459 GdkColor **visited_link_color)
2461 GtkStyleContext *context;
2463 context = gtk_widget_get_style_context (widget);
2464 gtk_style_context_get_style (context,
2465 "link-color", link_color,
2466 "visited-link-color", visited_link_color,
2469 *link_color = gdk_color_copy (&default_link_color);
2470 if (!*visited_link_color)
2471 *visited_link_color = gdk_color_copy (&default_visited_link_color);
2475 parse_uri_markup (GtkLabel *label,
2481 GMarkupParseContext *context = NULL;
2482 const gchar *p, *end;
2483 gboolean needs_root = TRUE;
2485 UriParserData pdata;
2487 length = strlen (str);
2491 pdata.label = label;
2493 pdata.new_str = g_string_sized_new (length);
2495 gtk_label_get_link_colors (GTK_WIDGET (label), &pdata.link_color, &pdata.visited_link_color);
2497 while (p != end && xml_isspace (*p))
2500 if (end - p >= 8 && strncmp (p, "<markup>", 8) == 0)
2503 context = g_markup_parse_context_new (&markup_parser, 0, &pdata, NULL);
2507 if (!g_markup_parse_context_parse (context, "<markup>", -1, error))
2511 if (!g_markup_parse_context_parse (context, str, length, error))
2516 if (!g_markup_parse_context_parse (context, "</markup>", -1, error))
2520 if (!g_markup_parse_context_end_parse (context, error))
2523 g_markup_parse_context_free (context);
2525 *new_str = g_string_free (pdata.new_str, FALSE);
2526 *links = pdata.links;
2528 gdk_color_free (pdata.link_color);
2529 gdk_color_free (pdata.visited_link_color);
2534 g_markup_parse_context_free (context);
2535 g_string_free (pdata.new_str, TRUE);
2536 g_list_foreach (pdata.links, (GFunc)link_free, NULL);
2537 g_list_free (pdata.links);
2538 gdk_color_free (pdata.link_color);
2539 gdk_color_free (pdata.visited_link_color);
2545 gtk_label_ensure_has_tooltip (GtkLabel *label)
2547 GtkLabelPrivate *priv = label->priv;
2549 gboolean has_tooltip = FALSE;
2551 for (l = priv->select_info->links; l; l = l->next)
2553 GtkLabelLink *link = l->data;
2561 gtk_widget_set_has_tooltip (GTK_WIDGET (label), has_tooltip);
2565 gtk_label_set_markup_internal (GtkLabel *label,
2567 gboolean with_uline)
2569 GtkLabelPrivate *priv = label->priv;
2571 GError *error = NULL;
2572 PangoAttrList *attrs = NULL;
2573 gunichar accel_char = 0;
2575 GList *links = NULL;
2577 if (!parse_uri_markup (label, str, &new_str, &links, &error))
2579 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2581 g_error_free (error);
2585 gtk_label_clear_links (label);
2588 gtk_label_ensure_select_info (label);
2589 priv->select_info->links = links;
2590 gtk_label_ensure_has_tooltip (label);
2595 gboolean enable_mnemonics;
2596 gboolean auto_mnemonics;
2598 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2599 "gtk-enable-mnemonics", &enable_mnemonics,
2600 "gtk-auto-mnemonics", &auto_mnemonics,
2603 if (!(enable_mnemonics && priv->mnemonics_visible &&
2605 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2606 (!priv->mnemonic_widget ||
2607 gtk_widget_is_sensitive (priv->mnemonic_widget))))))
2613 if (separate_uline_pattern (new_str, &key, &tmp, &pattern))
2622 if (!pango_parse_markup (new_str,
2624 with_uline ? '_' : 0,
2627 with_uline ? &accel_char : NULL,
2630 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2633 g_error_free (error);
2640 gtk_label_set_text_internal (label, text);
2644 if (priv->effective_attrs)
2645 pango_attr_list_unref (priv->effective_attrs);
2646 priv->effective_attrs = attrs;
2649 if (accel_char != 0)
2650 priv->mnemonic_keyval = gdk_keyval_to_lower (gdk_unicode_to_keyval (accel_char));
2652 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
2656 * gtk_label_set_markup:
2657 * @label: a #GtkLabel
2658 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
2660 * Parses @str which is marked up with the <link
2661 * linkend="PangoMarkupFormat">Pango text markup language</link>, setting the
2662 * label's text and attribute list based on the parse results. If the @str is
2663 * external data, you may need to escape it with g_markup_escape_text() or
2664 * g_markup_printf_escaped()<!-- -->:
2668 * markup = g_markup_printf_escaped ("<span style=\"italic\">%s</span>", str);
2669 * gtk_label_set_markup (GTK_LABEL (label), markup);
2674 gtk_label_set_markup (GtkLabel *label,
2677 g_return_if_fail (GTK_IS_LABEL (label));
2679 g_object_freeze_notify (G_OBJECT (label));
2681 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2682 gtk_label_set_use_markup_internal (label, TRUE);
2683 gtk_label_set_use_underline_internal (label, FALSE);
2685 gtk_label_recalculate (label);
2687 g_object_thaw_notify (G_OBJECT (label));
2691 * gtk_label_set_markup_with_mnemonic:
2692 * @label: a #GtkLabel
2693 * @str: a markup string (see
2694 * <link linkend="PangoMarkupFormat">Pango markup format</link>)
2696 * Parses @str which is marked up with the
2697 * <link linkend="PangoMarkupFormat">Pango text markup language</link>,
2698 * setting the label's text and attribute list based on the parse results.
2699 * If characters in @str are preceded by an underscore, they are underlined
2700 * indicating that they represent a keyboard accelerator called a mnemonic.
2702 * The mnemonic key can be used to activate another widget, chosen
2703 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
2706 gtk_label_set_markup_with_mnemonic (GtkLabel *label,
2709 g_return_if_fail (GTK_IS_LABEL (label));
2711 g_object_freeze_notify (G_OBJECT (label));
2713 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2714 gtk_label_set_use_markup_internal (label, TRUE);
2715 gtk_label_set_use_underline_internal (label, TRUE);
2717 gtk_label_recalculate (label);
2719 g_object_thaw_notify (G_OBJECT (label));
2723 * gtk_label_get_text:
2724 * @label: a #GtkLabel
2726 * Fetches the text from a label widget, as displayed on the
2727 * screen. This does not include any embedded underlines
2728 * indicating mnemonics or Pango markup. (See gtk_label_get_label())
2730 * Return value: the text in the label widget. This is the internal
2731 * string used by the label, and must not be modified.
2733 G_CONST_RETURN gchar *
2734 gtk_label_get_text (GtkLabel *label)
2736 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2738 return label->priv->text;
2741 static PangoAttrList *
2742 gtk_label_pattern_to_attrs (GtkLabel *label,
2743 const gchar *pattern)
2745 GtkLabelPrivate *priv = label->priv;
2747 const char *p = priv->text;
2748 const char *q = pattern;
2749 PangoAttrList *attrs;
2751 attrs = pango_attr_list_new ();
2755 while (*p && *q && *q != '_')
2757 p = g_utf8_next_char (p);
2761 while (*p && *q && *q == '_')
2763 p = g_utf8_next_char (p);
2769 PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_LOW);
2770 attr->start_index = start - priv->text;
2771 attr->end_index = p - priv->text;
2773 pango_attr_list_insert (attrs, attr);
2783 gtk_label_set_pattern_internal (GtkLabel *label,
2784 const gchar *pattern,
2785 gboolean is_mnemonic)
2787 GtkLabelPrivate *priv = label->priv;
2788 PangoAttrList *attrs;
2789 gboolean enable_mnemonics;
2790 gboolean auto_mnemonics;
2792 if (priv->pattern_set)
2797 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2798 "gtk-enable-mnemonics", &enable_mnemonics,
2799 "gtk-auto-mnemonics", &auto_mnemonics,
2802 if (enable_mnemonics && priv->mnemonics_visible && pattern &&
2804 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2805 (!priv->mnemonic_widget ||
2806 gtk_widget_is_sensitive (priv->mnemonic_widget)))))
2807 attrs = gtk_label_pattern_to_attrs (label, pattern);
2812 attrs = gtk_label_pattern_to_attrs (label, pattern);
2814 if (priv->effective_attrs)
2815 pango_attr_list_unref (priv->effective_attrs);
2816 priv->effective_attrs = attrs;
2820 * gtk_label_set_pattern:
2821 * @label: The #GtkLabel you want to set the pattern to.
2822 * @pattern: The pattern as described above.
2824 * The pattern of underlines you want under the existing text within the
2825 * #GtkLabel widget. For example if the current text of the label says
2826 * "FooBarBaz" passing a pattern of "___ ___" will underline
2827 * "Foo" and "Baz" but not "Bar".
2830 gtk_label_set_pattern (GtkLabel *label,
2831 const gchar *pattern)
2833 GtkLabelPrivate *priv = label->priv;
2835 g_return_if_fail (GTK_IS_LABEL (label));
2839 priv->pattern_set = FALSE;
2843 gtk_label_set_pattern_internal (label, pattern, FALSE);
2844 priv->pattern_set = TRUE;
2847 gtk_label_recalculate (label);
2849 gtk_label_clear_layout (label);
2850 gtk_widget_queue_resize (GTK_WIDGET (label));
2855 * gtk_label_set_justify:
2856 * @label: a #GtkLabel
2857 * @jtype: a #GtkJustification
2859 * Sets the alignment of the lines in the text of the label relative to
2860 * each other. %GTK_JUSTIFY_LEFT is the default value when the
2861 * widget is first created with gtk_label_new(). If you instead want
2862 * to set the alignment of the label as a whole, use
2863 * gtk_misc_set_alignment() instead. gtk_label_set_justify() has no
2864 * effect on labels containing only a single line.
2867 gtk_label_set_justify (GtkLabel *label,
2868 GtkJustification jtype)
2870 GtkLabelPrivate *priv;
2872 g_return_if_fail (GTK_IS_LABEL (label));
2873 g_return_if_fail (jtype >= GTK_JUSTIFY_LEFT && jtype <= GTK_JUSTIFY_FILL);
2877 if ((GtkJustification) priv->jtype != jtype)
2879 priv->jtype = jtype;
2881 /* No real need to be this drastic, but easier than duplicating the code */
2882 gtk_label_clear_layout (label);
2884 g_object_notify (G_OBJECT (label), "justify");
2885 gtk_widget_queue_resize (GTK_WIDGET (label));
2890 * gtk_label_get_justify:
2891 * @label: a #GtkLabel
2893 * Returns the justification of the label. See gtk_label_set_justify().
2895 * Return value: #GtkJustification
2898 gtk_label_get_justify (GtkLabel *label)
2900 g_return_val_if_fail (GTK_IS_LABEL (label), 0);
2902 return label->priv->jtype;
2906 * gtk_label_set_ellipsize:
2907 * @label: a #GtkLabel
2908 * @mode: a #PangoEllipsizeMode
2910 * Sets the mode used to ellipsize (add an ellipsis: "...") to the text
2911 * if there is not enough space to render the entire string.
2916 gtk_label_set_ellipsize (GtkLabel *label,
2917 PangoEllipsizeMode mode)
2919 GtkLabelPrivate *priv;
2921 g_return_if_fail (GTK_IS_LABEL (label));
2922 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE && mode <= PANGO_ELLIPSIZE_END);
2926 if ((PangoEllipsizeMode) priv->ellipsize != mode)
2928 priv->ellipsize = mode;
2930 /* No real need to be this drastic, but easier than duplicating the code */
2931 gtk_label_clear_layout (label);
2933 g_object_notify (G_OBJECT (label), "ellipsize");
2934 gtk_widget_queue_resize (GTK_WIDGET (label));
2939 * gtk_label_get_ellipsize:
2940 * @label: a #GtkLabel
2942 * Returns the ellipsizing position of the label. See gtk_label_set_ellipsize().
2944 * Return value: #PangoEllipsizeMode
2949 gtk_label_get_ellipsize (GtkLabel *label)
2951 g_return_val_if_fail (GTK_IS_LABEL (label), PANGO_ELLIPSIZE_NONE);
2953 return label->priv->ellipsize;
2957 * gtk_label_set_width_chars:
2958 * @label: a #GtkLabel
2959 * @n_chars: the new desired width, in characters.
2961 * Sets the desired width in characters of @label to @n_chars.
2966 gtk_label_set_width_chars (GtkLabel *label,
2969 GtkLabelPrivate *priv;
2971 g_return_if_fail (GTK_IS_LABEL (label));
2975 if (priv->width_chars != n_chars)
2977 priv->width_chars = n_chars;
2978 g_object_notify (G_OBJECT (label), "width-chars");
2979 gtk_widget_queue_resize (GTK_WIDGET (label));
2984 * gtk_label_get_width_chars:
2985 * @label: a #GtkLabel
2987 * Retrieves the desired width of @label, in characters. See
2988 * gtk_label_set_width_chars().
2990 * Return value: the width of the label in characters.
2995 gtk_label_get_width_chars (GtkLabel *label)
2997 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2999 return label->priv->width_chars;
3003 * gtk_label_set_max_width_chars:
3004 * @label: a #GtkLabel
3005 * @n_chars: the new desired maximum width, in characters.
3007 * Sets the desired maximum width in characters of @label to @n_chars.
3012 gtk_label_set_max_width_chars (GtkLabel *label,
3015 GtkLabelPrivate *priv;
3017 g_return_if_fail (GTK_IS_LABEL (label));
3021 if (priv->max_width_chars != n_chars)
3023 priv->max_width_chars = n_chars;
3025 g_object_notify (G_OBJECT (label), "max-width-chars");
3026 gtk_widget_queue_resize (GTK_WIDGET (label));
3031 * gtk_label_get_max_width_chars:
3032 * @label: a #GtkLabel
3034 * Retrieves the desired maximum width of @label, in characters. See
3035 * gtk_label_set_width_chars().
3037 * Return value: the maximum width of the label in characters.
3042 gtk_label_get_max_width_chars (GtkLabel *label)
3044 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
3046 return label->priv->max_width_chars;
3050 * gtk_label_set_line_wrap:
3051 * @label: a #GtkLabel
3052 * @wrap: the setting
3054 * Toggles line wrapping within the #GtkLabel widget. %TRUE makes it break
3055 * lines if text exceeds the widget's size. %FALSE lets the text get cut off
3056 * by the edge of the widget if it exceeds the widget size.
3058 * Note that setting line wrapping to %TRUE does not make the label
3059 * wrap at its parent container's width, because GTK+ widgets
3060 * conceptually can't make their requisition depend on the parent
3061 * container's size. For a label that wraps at a specific position,
3062 * set the label's width using gtk_widget_set_size_request().
3065 gtk_label_set_line_wrap (GtkLabel *label,
3068 GtkLabelPrivate *priv;
3070 g_return_if_fail (GTK_IS_LABEL (label));
3074 wrap = wrap != FALSE;
3076 if (priv->wrap != wrap)
3080 gtk_label_clear_layout (label);
3081 gtk_widget_queue_resize (GTK_WIDGET (label));
3082 g_object_notify (G_OBJECT (label), "wrap");
3087 * gtk_label_get_line_wrap:
3088 * @label: a #GtkLabel
3090 * Returns whether lines in the label are automatically wrapped.
3091 * See gtk_label_set_line_wrap().
3093 * Return value: %TRUE if the lines of the label are automatically wrapped.
3096 gtk_label_get_line_wrap (GtkLabel *label)
3098 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
3100 return label->priv->wrap;
3104 * gtk_label_set_line_wrap_mode:
3105 * @label: a #GtkLabel
3106 * @wrap_mode: the line wrapping mode
3108 * If line wrapping is on (see gtk_label_set_line_wrap()) this controls how
3109 * the line wrapping is done. The default is %PANGO_WRAP_WORD which means
3110 * wrap on word boundaries.
3115 gtk_label_set_line_wrap_mode (GtkLabel *label,
3116 PangoWrapMode wrap_mode)
3118 GtkLabelPrivate *priv;
3120 g_return_if_fail (GTK_IS_LABEL (label));
3124 if (priv->wrap_mode != wrap_mode)
3126 priv->wrap_mode = wrap_mode;
3127 g_object_notify (G_OBJECT (label), "wrap-mode");
3129 gtk_widget_queue_resize (GTK_WIDGET (label));
3134 * gtk_label_get_line_wrap_mode:
3135 * @label: a #GtkLabel
3137 * Returns line wrap mode used by the label. See gtk_label_set_line_wrap_mode().
3139 * Return value: %TRUE if the lines of the label are automatically wrapped.
3144 gtk_label_get_line_wrap_mode (GtkLabel *label)
3146 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
3148 return label->priv->wrap_mode;
3152 gtk_label_destroy (GtkWidget *widget)
3154 GtkLabel *label = GTK_LABEL (widget);
3156 gtk_label_set_mnemonic_widget (label, NULL);
3158 GTK_WIDGET_CLASS (gtk_label_parent_class)->destroy (widget);
3162 gtk_label_finalize (GObject *object)
3164 GtkLabel *label = GTK_LABEL (object);
3165 GtkLabelPrivate *priv = label->priv;
3167 g_free (priv->label);
3168 g_free (priv->text);
3171 g_object_unref (priv->layout);
3174 pango_attr_list_unref (priv->attrs);
3176 if (priv->effective_attrs)
3177 pango_attr_list_unref (priv->effective_attrs);
3179 gtk_label_clear_links (label);
3180 g_free (priv->select_info);
3182 G_OBJECT_CLASS (gtk_label_parent_class)->finalize (object);
3186 gtk_label_clear_layout (GtkLabel *label)
3188 GtkLabelPrivate *priv = label->priv;
3192 g_object_unref (priv->layout);
3193 priv->layout = NULL;
3195 //gtk_label_clear_links (label);
3199 static PangoFontMetrics *
3200 get_font_metrics (PangoContext *context, GtkWidget *widget)
3202 GtkStyleContext *style_context;
3203 PangoFontDescription *font;
3204 PangoFontMetrics *retval;
3206 style_context = gtk_widget_get_style_context (widget);
3207 gtk_style_context_get (style_context, 0, "font", &font, NULL);
3209 retval = pango_context_get_metrics (context,
3211 pango_context_get_language (context));
3214 pango_font_description_free (font);
3220 * gtk_label_get_measuring_layout:
3222 * @existing_layout: %NULL or an existing layout already in use.
3223 * @width: the width to measure with in pango units, or -1 for infinite
3224 * @height: the height to measure with in pango units, or -1 for infinite
3226 * Gets a layout that can be used for measuring sizes. The returned
3227 * layout will be identical to the label's layout except for the
3228 * layout's width, which will be set to @width. Do not modify the returned
3231 * Returns: a new reference to a pango layout
3233 static PangoLayout *
3234 gtk_label_get_measuring_layout (GtkLabel * label,
3235 PangoLayout *existing_layout,
3239 GtkLabelPrivate *priv = label->priv;
3240 PangoRectangle rect;
3243 if (existing_layout != NULL)
3245 if (existing_layout != priv->layout)
3247 pango_layout_set_width (existing_layout, width);
3248 pango_layout_set_height (existing_layout, height);
3249 return existing_layout;
3252 g_object_unref (existing_layout);
3255 gtk_label_ensure_layout (label);
3257 if (pango_layout_get_width (priv->layout) == width &&
3258 pango_layout_get_height (priv->layout) == height)
3260 g_object_ref (priv->layout);
3261 return priv->layout;
3264 /* We can use the label's own layout if we're not allocated a size yet,
3265 * because we don't need it to be properly setup at that point.
3266 * This way we can make use of caching upon the label's creation.
3268 if (gtk_widget_get_allocated_width (GTK_WIDGET (label)) <= 1)
3270 g_object_ref (priv->layout);
3271 pango_layout_set_width (priv->layout, width);
3272 pango_layout_set_height (priv->layout, height);
3273 return priv->layout;
3276 /* oftentimes we want to measure a width that is far wider than the current width,
3277 * even though the layout would not change if we made it wider. In that case, we
3278 * can just return the current layout, because for measuring purposes, it will be
3281 pango_layout_get_extents (priv->layout, NULL, &rect);
3282 if ((width == -1 || rect.width <= width) &&
3283 (height == -1 || rect.height <= height) &&
3284 !pango_layout_is_wrapped (priv->layout) &&
3285 !pango_layout_is_ellipsized (priv->layout))
3287 g_object_ref (priv->layout);
3288 return priv->layout;
3291 copy = pango_layout_copy (priv->layout);
3292 pango_layout_set_width (copy, width);
3293 pango_layout_set_height (copy, height);
3298 gtk_label_update_layout_width (GtkLabel *label)
3300 GtkLabelPrivate *priv = label->priv;
3301 GtkWidget *widget = GTK_WIDGET (label);
3303 g_assert (priv->layout);
3305 if (priv->ellipsize || priv->wrap)
3307 PangoRectangle logical;
3311 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3313 width = gtk_widget_get_allocated_width (GTK_WIDGET (label)) - xpad * 2;
3314 height = gtk_widget_get_allocated_height (GTK_WIDGET (label)) - ypad * 2;
3316 if (priv->have_transform)
3318 PangoContext *context = gtk_widget_get_pango_context (widget);
3319 const PangoMatrix *matrix = pango_context_get_matrix (context);
3320 const gdouble dx = matrix->xx; /* cos (M_PI * angle / 180) */
3321 const gdouble dy = matrix->xy; /* sin (M_PI * angle / 180) */
3323 pango_layout_set_width (priv->layout, -1);
3324 pango_layout_set_height (priv->layout, -1);
3325 pango_layout_get_pixel_extents (priv->layout, NULL, &logical);
3327 if (fabs (dy) < 0.01)
3329 if (logical.width > width)
3330 pango_layout_set_width (priv->layout, width * PANGO_SCALE);
3332 else if (fabs (dx) < 0.01)
3334 if (logical.width > height)
3335 pango_layout_set_width (priv->layout, height * PANGO_SCALE);
3339 gdouble x0, y0, x1, y1, length;
3344 y0 = dx ? x0 * dy / dx : G_MAXDOUBLE;
3345 vertical = fabs (y0) > height / 2;
3350 x0 = dy ? y0 * dx / dy : G_MAXDOUBLE;
3353 length = 2 * sqrt (x0 * x0 + y0 * y0);
3354 pango_layout_set_width (priv->layout, rint (length * PANGO_SCALE));
3355 pango_layout_get_pixel_size (priv->layout, NULL, &cy);
3362 y0 = height/2 + y1 - y0;
3367 x0 = width/2 + x1 - x0;
3371 length = length - sqrt (x0 * x0 + y0 * y0) * 2;
3372 pango_layout_set_width (priv->layout, rint (length * PANGO_SCALE));
3377 pango_layout_set_width (priv->layout, width * PANGO_SCALE);
3378 pango_layout_set_height (priv->layout, priv->ellipsize ? height * PANGO_SCALE : -1);
3383 pango_layout_set_width (priv->layout, -1);
3384 pango_layout_set_height (priv->layout, -1);
3389 gtk_label_ensure_layout (GtkLabel *label)
3391 GtkLabelPrivate *priv = label->priv;
3395 widget = GTK_WIDGET (label);
3397 rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
3401 PangoAlignment align = PANGO_ALIGN_LEFT; /* Quiet gcc */
3402 gdouble angle = gtk_label_get_angle (label);
3404 if (angle != 0.0 && !priv->select_info)
3406 PangoMatrix matrix = PANGO_MATRIX_INIT;
3408 /* We rotate the standard singleton PangoContext for the widget,
3409 * depending on the fact that it's meant pretty much exclusively
3412 pango_matrix_rotate (&matrix, angle);
3414 pango_context_set_matrix (gtk_widget_get_pango_context (widget), &matrix);
3416 priv->have_transform = TRUE;
3420 if (priv->have_transform)
3421 pango_context_set_matrix (gtk_widget_get_pango_context (widget), NULL);
3423 priv->have_transform = FALSE;
3426 priv->layout = gtk_widget_create_pango_layout (widget, priv->text);
3428 if (priv->effective_attrs)
3429 pango_layout_set_attributes (priv->layout, priv->effective_attrs);
3431 gtk_label_rescan_links (label);
3433 switch (priv->jtype)
3435 case GTK_JUSTIFY_LEFT:
3436 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3438 case GTK_JUSTIFY_RIGHT:
3439 align = rtl ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
3441 case GTK_JUSTIFY_CENTER:
3442 align = PANGO_ALIGN_CENTER;
3444 case GTK_JUSTIFY_FILL:
3445 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3446 pango_layout_set_justify (priv->layout, TRUE);
3449 g_assert_not_reached();
3452 pango_layout_set_alignment (priv->layout, align);
3453 pango_layout_set_ellipsize (priv->layout, priv->ellipsize);
3454 pango_layout_set_wrap (priv->layout, priv->wrap_mode);
3455 pango_layout_set_single_paragraph_mode (priv->layout, priv->single_line_mode);
3457 gtk_label_update_layout_width (label);
3462 get_single_line_height (GtkWidget *widget,
3463 PangoLayout *layout)
3465 PangoContext *context;
3466 PangoFontMetrics *metrics;
3467 gint ascent, descent;
3469 context = pango_layout_get_context (layout);
3470 metrics = get_font_metrics (context, widget);
3471 ascent = pango_font_metrics_get_ascent (metrics);
3472 descent = pango_font_metrics_get_descent (metrics);
3473 pango_font_metrics_unref (metrics);
3475 return ascent + descent;
3478 static GtkSizeRequestMode
3479 gtk_label_get_request_mode (GtkWidget *widget)
3481 GtkLabel *label = GTK_LABEL (widget);
3482 gdouble angle = gtk_label_get_angle (label);
3484 if (label->priv->wrap)
3485 return (angle == 90 || angle == 270) ?
3486 GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT :
3487 GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
3489 return GTK_SIZE_REQUEST_CONSTANT_SIZE;
3493 get_size_for_allocation (GtkLabel *label,
3494 GtkOrientation orientation,
3499 GtkLabelPrivate *priv = label->priv;
3500 PangoLayout *layout;
3503 layout = gtk_label_get_measuring_layout (label, NULL, allocation * PANGO_SCALE, -1);
3505 pango_layout_get_pixel_size (layout, NULL, &text_height);
3508 *minimum_size = text_height;
3512 if (priv->ellipsize && priv->wrap)
3514 layout = gtk_label_get_measuring_layout (label, layout, allocation * PANGO_SCALE, G_MAXINT);
3515 pango_layout_get_pixel_size (layout, NULL, &text_height);
3518 *natural_size = text_height;
3521 g_object_unref (layout);
3525 get_char_pixels (GtkWidget *label,
3526 PangoLayout *layout)
3528 PangoContext *context;
3529 PangoFontMetrics *metrics;
3530 gint char_width, digit_width;
3532 context = pango_layout_get_context (layout);
3533 metrics = get_font_metrics (context, GTK_WIDGET (label));
3534 char_width = pango_font_metrics_get_approximate_char_width (metrics);
3535 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
3536 pango_font_metrics_unref (metrics);
3538 return MAX (char_width, digit_width);;
3542 gtk_label_get_preferred_layout_size (GtkLabel *label,
3543 PangoRectangle *required,
3544 PangoRectangle *natural)
3546 GtkLabelPrivate *priv = label->priv;
3547 PangoLayout *layout;
3549 /* "width-chars" Hard-coded minimum width:
3550 * - minimum size should be MAX (width-chars, strlen ("..."));
3551 * - natural size should be MAX (width-chars, strlen (priv->text));
3553 * "max-width-chars" User specified maximum size requisition
3554 * - minimum size should be MAX (width-chars, 0)
3555 * - natural size should be MIN (max-width-chars, strlen (priv->text))
3557 * For ellipsizing labels; if max-width-chars is specified: either it is used as
3558 * a minimum size or the label text as a minimum size (natural size still overflows).
3560 * For wrapping labels; A reasonable minimum size is useful to naturally layout
3561 * interfaces automatically. In this case if no "width-chars" is specified, the minimum
3562 * width will default to the wrap guess that gtk_label_ensure_layout() does.
3565 /* Start off with the pixel extents of an as-wide-as-possible layout */
3566 layout = gtk_label_get_measuring_layout (label, NULL, -1, -1);
3568 pango_layout_get_extents (layout, NULL, natural);
3569 natural->x = natural->y = 0;
3572 natural->height = get_single_line_height (GTK_WIDGET (label), layout);
3574 if (priv->ellipsize || priv->wrap)
3576 /* a layout with width 0 will be as small as humanly possible */
3577 layout = gtk_label_get_measuring_layout (label, layout, 0, -1);
3579 pango_layout_get_extents (layout, NULL, required);
3581 /* can happen when Pango decides to ellipsize text */
3582 if (required->width > natural->width)
3583 required->width = natural->width;
3585 required->x = required->y = 0;
3586 required->height = natural->height;
3590 *required = *natural;
3593 if (priv->width_chars > -1 || priv->max_width_chars > -1)
3597 char_pixels = get_char_pixels (GTK_WIDGET (label), layout);
3599 if (priv->width_chars > -1)
3600 required->width = MAX (required->width, char_pixels * priv->width_chars);
3602 if (priv->max_width_chars > -1)
3603 natural->width = MIN (natural->width, priv->max_width_chars * char_pixels);
3604 natural->width = MAX (natural->width, required->width);
3607 g_object_unref (layout);
3611 gtk_label_get_preferred_size (GtkWidget *widget,
3612 GtkOrientation orientation,
3616 GtkLabel *label = GTK_LABEL (widget);
3617 GtkLabelPrivate *priv = label->priv;
3618 PangoRectangle required_rect;
3619 PangoRectangle natural_rect;
3622 gtk_label_get_preferred_layout_size (label, &required_rect, &natural_rect);
3624 /* Now that we have minimum and natural sizes in pango extents, apply a possible transform */
3625 if (priv->have_transform)
3628 PangoContext *context;
3629 const PangoMatrix *matrix;
3631 copy = pango_layout_copy (priv->layout);
3632 context = pango_layout_get_context (copy);
3633 matrix = pango_context_get_matrix (context);
3635 pango_layout_set_width (copy, -1);
3636 pango_layout_set_ellipsize (copy, PANGO_ELLIPSIZE_NONE);
3638 pango_layout_get_extents (copy, NULL, &natural_rect);
3639 g_object_unref (copy);
3641 pango_matrix_transform_rectangle (matrix, &required_rect);
3642 pango_matrix_transform_rectangle (matrix, &natural_rect);
3644 /* Bump the natural size in case of ellipsize to ensure pango has
3645 * enough space in the angles (note, we could alternatively set the
3646 * layout to not ellipsize when we know we have been allocated our
3647 * full natural size, or it may be that pango needs a fix here).
3649 if (priv->ellipsize && priv->angle != 0 && priv->angle != 90 &&
3650 priv->angle != 180 && priv->angle != 270 && priv->angle != 360)
3652 /* For some reason we only need this at about 110 degrees, and only
3653 * when gaining in height
3655 natural_rect.height += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3656 natural_rect.width += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3660 required_rect.width = PANGO_PIXELS_CEIL (required_rect.width);
3661 required_rect.height = PANGO_PIXELS_CEIL (required_rect.height);
3663 natural_rect.width = PANGO_PIXELS_CEIL (natural_rect.width);
3664 natural_rect.height = PANGO_PIXELS_CEIL (natural_rect.height);
3666 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3668 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3670 /* Note, we cant use get_size_for_allocation() when rotating
3671 * ellipsized labels.
3673 if (!(priv->ellipsize && priv->have_transform) &&
3674 (priv->angle == 90 || priv->angle == 270))
3676 /* Doing a h4w request on a rotated label here, return the
3677 * required width for the minimum height.
3679 get_size_for_allocation (label,
3680 GTK_ORIENTATION_VERTICAL,
3681 required_rect.height,
3682 minimum_size, natural_size);
3687 /* Normal desired width */
3688 *minimum_size = required_rect.width;
3689 *natural_size = natural_rect.width;
3692 *minimum_size += xpad * 2;
3693 *natural_size += xpad * 2;
3695 else /* GTK_ORIENTATION_VERTICAL */
3697 /* Note, we cant use get_size_for_allocation() when rotating
3698 * ellipsized labels.
3700 if (!(priv->ellipsize && priv->have_transform) &&
3701 (priv->angle == 0 || priv->angle == 180 || priv->angle == 360))
3703 /* Doing a w4h request on a label here, return the required
3704 * height for the minimum width.
3706 get_size_for_allocation (label,
3707 GTK_ORIENTATION_HORIZONTAL,
3708 required_rect.width,
3709 minimum_size, natural_size);
3713 /* A vertically rotated label does w4h, so return the base
3714 * desired height (text length)
3716 *minimum_size = required_rect.height;
3717 *natural_size = natural_rect.height;
3720 *minimum_size += ypad * 2;
3721 *natural_size += ypad * 2;
3727 gtk_label_get_preferred_width (GtkWidget *widget,
3731 gtk_label_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
3735 gtk_label_get_preferred_height (GtkWidget *widget,
3739 gtk_label_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
3743 gtk_label_get_preferred_width_for_height (GtkWidget *widget,
3745 gint *minimum_width,
3746 gint *natural_width)
3748 GtkLabel *label = GTK_LABEL (widget);
3749 GtkLabelPrivate *priv = label->priv;
3751 if (priv->wrap && (priv->angle == 90 || priv->angle == 270))
3755 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3758 gtk_label_clear_layout (label);
3760 get_size_for_allocation (label, GTK_ORIENTATION_VERTICAL,
3761 MAX (1, height - (ypad * 2)),
3762 minimum_width, natural_width);
3765 *minimum_width += xpad * 2;
3768 *natural_width += xpad * 2;
3771 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_width, natural_width);
3775 gtk_label_get_preferred_height_for_width (GtkWidget *widget,
3777 gint *minimum_height,
3778 gint *natural_height)
3780 GtkLabel *label = GTK_LABEL (widget);
3781 GtkLabelPrivate *priv = label->priv;
3783 if (priv->wrap && (priv->angle == 0 || priv->angle == 180 || priv->angle == 360))
3787 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3790 gtk_label_clear_layout (label);
3792 get_size_for_allocation (label, GTK_ORIENTATION_HORIZONTAL,
3793 MAX (1, width - xpad * 2),
3794 minimum_height, natural_height);
3797 *minimum_height += ypad * 2;
3800 *natural_height += ypad * 2;
3803 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, minimum_height, natural_height);
3807 gtk_label_size_allocate (GtkWidget *widget,
3808 GtkAllocation *allocation)
3810 GtkLabel *label = GTK_LABEL (widget);
3811 GtkLabelPrivate *priv = label->priv;
3813 GTK_WIDGET_CLASS (gtk_label_parent_class)->size_allocate (widget, allocation);
3816 gtk_label_update_layout_width (label);
3818 if (priv->select_info && priv->select_info->window)
3820 gdk_window_move_resize (priv->select_info->window,
3824 allocation->height);
3829 gtk_label_update_cursor (GtkLabel *label)
3831 GtkLabelPrivate *priv = label->priv;
3834 if (!priv->select_info)
3837 widget = GTK_WIDGET (label);
3839 if (gtk_widget_get_realized (widget))
3841 GdkDisplay *display;
3844 if (gtk_widget_is_sensitive (widget))
3846 display = gtk_widget_get_display (widget);
3848 if (priv->select_info->active_link)
3849 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
3850 else if (priv->select_info->selectable)
3851 cursor = gdk_cursor_new_for_display (display, GDK_XTERM);
3858 gdk_window_set_cursor (priv->select_info->window, cursor);
3861 g_object_unref (cursor);
3866 gtk_label_state_changed (GtkWidget *widget,
3867 GtkStateType prev_state)
3869 GtkLabel *label = GTK_LABEL (widget);
3870 GtkLabelPrivate *priv = label->priv;
3872 if (priv->select_info)
3874 if (!gtk_widget_is_sensitive (widget))
3875 gtk_label_select_region (label, 0, 0);
3877 gtk_label_update_cursor (label);
3880 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed)
3881 GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed (widget, prev_state);
3885 gtk_label_style_updated (GtkWidget *widget)
3887 GtkLabel *label = GTK_LABEL (widget);
3889 GTK_WIDGET_CLASS (gtk_label_parent_class)->style_updated (widget);
3891 /* We have to clear the layout, fonts etc. may have changed */
3892 gtk_label_clear_layout (label);
3896 gtk_label_direction_changed (GtkWidget *widget,
3897 GtkTextDirection previous_dir)
3899 GtkLabel *label = GTK_LABEL (widget);
3900 GtkLabelPrivate *priv = label->priv;
3903 pango_layout_context_changed (priv->layout);
3905 GTK_WIDGET_CLASS (gtk_label_parent_class)->direction_changed (widget, previous_dir);
3909 get_layout_location (GtkLabel *label,
3913 GtkAllocation allocation;
3916 GtkLabelPrivate *priv;
3917 gint req_width, x, y;
3920 gfloat xalign, yalign;
3921 PangoRectangle logical;
3923 misc = GTK_MISC (label);
3924 widget = GTK_WIDGET (label);
3927 gtk_misc_get_alignment (misc, &xalign, &yalign);
3928 gtk_misc_get_padding (misc, &xpad, &ypad);
3930 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
3931 xalign = 1.0 - xalign;
3933 pango_layout_get_extents (priv->layout, NULL, &logical);
3935 if (priv->have_transform)
3937 PangoContext *context = gtk_widget_get_pango_context (widget);
3938 const PangoMatrix *matrix = pango_context_get_matrix (context);
3939 pango_matrix_transform_rectangle (matrix, &logical);
3942 pango_extents_to_pixels (&logical, NULL);
3944 req_width = logical.width;
3945 req_height = logical.height;
3947 req_width += 2 * xpad;
3948 req_height += 2 * ypad;
3950 gtk_widget_get_allocation (widget, &allocation);
3952 x = floor (allocation.x + xpad + xalign * (allocation.width - req_width) - logical.x);
3955 /* bgo#315462 - For single-line labels, *do* align the requisition with
3956 * respect to the allocation, even if we are under-allocated. For multi-line
3957 * labels, always show the top of the text when they are under-allocated. The
3958 * rationale is this:
3960 * - Single-line labels appear in GtkButtons, and it is very easy to get them
3961 * to be smaller than their requisition. The button may clip the label, but
3962 * the label will still be able to show most of itself and the focus
3963 * rectangle. Also, it is fairly easy to read a single line of clipped text.
3965 * - Multi-line labels should not be clipped to showing "something in the
3966 * middle". You want to read the first line, at least, to get some context.
3968 if (pango_layout_get_line_count (priv->layout) == 1)
3969 y = floor (allocation.y + ypad + (allocation.height - req_height) * yalign);
3971 y = floor (allocation.y + ypad + MAX ((allocation.height - req_height) * yalign, 0));
3981 draw_insertion_cursor (GtkLabel *label,
3983 GdkRectangle *cursor_location,
3984 gboolean is_primary,
3985 PangoDirection direction,
3986 gboolean draw_arrow)
3988 GtkWidget *widget = GTK_WIDGET (label);
3989 GtkTextDirection text_dir;
3991 if (direction == PANGO_DIRECTION_LTR)
3992 text_dir = GTK_TEXT_DIR_LTR;
3994 text_dir = GTK_TEXT_DIR_RTL;
3996 gtk_draw_insertion_cursor (widget, cr, cursor_location,
3997 is_primary, text_dir, draw_arrow);
4000 static PangoDirection
4001 get_cursor_direction (GtkLabel *label)
4003 GtkLabelPrivate *priv = label->priv;
4006 g_assert (priv->select_info);
4008 gtk_label_ensure_layout (label);
4010 for (l = pango_layout_get_lines_readonly (priv->layout); l; l = l->next)
4012 PangoLayoutLine *line = l->data;
4014 /* If priv->select_info->selection_end is at the very end of
4015 * the line, we don't know if the cursor is on this line or
4016 * the next without looking ahead at the next line. (End
4017 * of paragraph is different from line break.) But it's
4018 * definitely in this paragraph, which is good enough
4019 * to figure out the resolved direction.
4021 if (line->start_index + line->length >= priv->select_info->selection_end)
4022 return line->resolved_dir;
4025 return PANGO_DIRECTION_LTR;
4029 gtk_label_draw_cursor (GtkLabel *label, cairo_t *cr, gint xoffset, gint yoffset)
4031 GtkLabelPrivate *priv = label->priv;
4034 if (priv->select_info == NULL)
4037 widget = GTK_WIDGET (label);
4039 if (gtk_widget_is_drawable (widget))
4041 PangoDirection keymap_direction;
4042 PangoDirection cursor_direction;
4043 PangoRectangle strong_pos, weak_pos;
4044 gboolean split_cursor;
4045 PangoRectangle *cursor1 = NULL;
4046 PangoRectangle *cursor2 = NULL;
4047 GdkRectangle cursor_location;
4048 PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
4049 PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
4051 keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gtk_widget_get_display (widget)));
4052 cursor_direction = get_cursor_direction (label);
4054 gtk_label_ensure_layout (label);
4056 pango_layout_get_cursor_pos (priv->layout, priv->select_info->selection_end,
4057 &strong_pos, &weak_pos);
4059 g_object_get (gtk_widget_get_settings (widget),
4060 "gtk-split-cursor", &split_cursor,
4063 dir1 = cursor_direction;
4067 cursor1 = &strong_pos;
4069 if (strong_pos.x != weak_pos.x ||
4070 strong_pos.y != weak_pos.y)
4072 dir2 = (cursor_direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
4073 cursor2 = &weak_pos;
4078 if (keymap_direction == cursor_direction)
4079 cursor1 = &strong_pos;
4081 cursor1 = &weak_pos;
4084 cursor_location.x = xoffset + PANGO_PIXELS (cursor1->x);
4085 cursor_location.y = yoffset + PANGO_PIXELS (cursor1->y);
4086 cursor_location.width = 0;
4087 cursor_location.height = PANGO_PIXELS (cursor1->height);
4089 draw_insertion_cursor (label, cr,
4090 &cursor_location, TRUE, dir1,
4091 dir2 != PANGO_DIRECTION_NEUTRAL);
4093 if (dir2 != PANGO_DIRECTION_NEUTRAL)
4095 cursor_location.x = xoffset + PANGO_PIXELS (cursor2->x);
4096 cursor_location.y = yoffset + PANGO_PIXELS (cursor2->y);
4097 cursor_location.width = 0;
4098 cursor_location.height = PANGO_PIXELS (cursor2->height);
4100 draw_insertion_cursor (label, cr,
4101 &cursor_location, FALSE, dir2,
4107 static GtkLabelLink *
4108 gtk_label_get_focus_link (GtkLabel *label)
4110 GtkLabelPrivate *priv = label->priv;
4111 GtkLabelSelectionInfo *info = priv->select_info;
4117 if (info->selection_anchor != info->selection_end)
4120 for (l = info->links; l; l = l->next)
4122 GtkLabelLink *link = l->data;
4123 if (link->start <= info->selection_anchor &&
4124 info->selection_anchor <= link->end)
4132 gtk_label_draw (GtkWidget *widget,
4135 GtkLabel *label = GTK_LABEL (widget);
4136 GtkLabelPrivate *priv = label->priv;
4137 GtkLabelSelectionInfo *info = priv->select_info;
4138 GtkAllocation allocation;
4139 GtkStyleContext *context;
4140 GtkStateFlags state;
4143 gtk_label_ensure_layout (label);
4145 if (priv->text && (*priv->text != '\0'))
4147 GdkRGBA *bg_color, *fg_color;
4149 get_layout_location (label, &x, &y);
4151 context = gtk_widget_get_style_context (widget);
4152 gtk_widget_get_allocation (widget, &allocation);
4154 cairo_translate (cr, -allocation.x, -allocation.y);
4156 state = gtk_widget_get_state_flags (widget);
4157 gtk_style_context_set_state (context, state);
4159 gtk_render_layout (context, cr,
4164 (info->selection_anchor != info->selection_end))
4167 cairo_region_t *clip;
4169 range[0] = info->selection_anchor;
4170 range[1] = info->selection_end;
4172 if (range[0] > range[1])
4174 gint tmp = range[0];
4175 range[0] = range[1];
4179 clip = gdk_pango_layout_get_clip_region (priv->layout,
4184 /* FIXME should use gtk_paint, but it can't use a clip region */
4187 gdk_cairo_region (cr, clip);
4190 state = GTK_STATE_FLAG_SELECTED;
4192 if (gtk_widget_has_focus (widget))
4193 state |= GTK_STATE_FLAG_FOCUSED;
4195 gtk_style_context_get (context, state,
4196 "background-color", &bg_color,
4200 gdk_cairo_set_source_rgba (cr, bg_color);
4203 gdk_cairo_set_source_rgba (cr, fg_color);
4204 cairo_move_to (cr, x, y);
4205 _gtk_pango_fill_layout (cr, priv->layout);
4208 cairo_region_destroy (clip);
4210 gdk_rgba_free (bg_color);
4211 gdk_rgba_free (fg_color);
4215 GtkLabelLink *focus_link;
4216 GtkLabelLink *active_link;
4218 cairo_region_t *clip;
4220 GdkColor *text_color;
4221 GdkColor *link_color;
4222 GdkColor *visited_link_color;
4224 if (info->selectable && gtk_widget_has_focus (widget))
4225 gtk_label_draw_cursor (label, cr, x, y);
4227 focus_link = gtk_label_get_focus_link (label);
4228 active_link = info->active_link;
4235 range[0] = active_link->start;
4236 range[1] = active_link->end;
4240 clip = gdk_pango_layout_get_clip_region (priv->layout,
4244 gdk_cairo_region (cr, clip);
4246 cairo_region_destroy (clip);
4248 gtk_label_get_link_colors (widget, &link_color, &visited_link_color);
4249 if (active_link->visited)
4250 text_color = visited_link_color;
4252 text_color = link_color;
4254 if (info->link_clicked)
4255 state = GTK_STATE_FLAG_ACTIVE;
4257 state = GTK_STATE_FLAG_PRELIGHT;
4259 gtk_style_context_get (context, state,
4260 "background-color", &bg_color,
4263 gdk_cairo_set_source_rgba (cr, bg_color);
4266 gdk_cairo_set_source_color (cr, text_color);
4267 cairo_move_to (cr, x, y);
4268 _gtk_pango_fill_layout (cr, priv->layout);
4270 gdk_color_free (link_color);
4271 gdk_color_free (visited_link_color);
4272 gdk_rgba_free (bg_color);
4277 if (focus_link && gtk_widget_has_focus (widget))
4279 range[0] = focus_link->start;
4280 range[1] = focus_link->end;
4282 clip = gdk_pango_layout_get_clip_region (priv->layout,
4286 cairo_region_get_extents (clip, &rect);
4288 state = gtk_widget_get_state_flags (widget);
4289 gtk_style_context_set_state (context, state);
4291 gtk_render_focus (context, cr,
4293 rect.width, rect.height);
4295 cairo_region_destroy (clip);
4304 separate_uline_pattern (const gchar *str,
4309 gboolean underscore;
4312 gchar *pattern_dest;
4314 *accel_key = GDK_KEY_VoidSymbol;
4315 *new_str = g_new (gchar, strlen (str) + 1);
4316 *pattern = g_new (gchar, g_utf8_strlen (str, -1) + 1);
4322 pattern_dest = *pattern;
4327 const gchar *next_src;
4329 c = g_utf8_get_char (src);
4330 if (c == (gunichar)-1)
4332 g_warning ("Invalid input string");
4338 next_src = g_utf8_next_char (src);
4343 *pattern_dest++ = ' ';
4346 *pattern_dest++ = '_';
4347 if (*accel_key == GDK_KEY_VoidSymbol)
4348 *accel_key = gdk_keyval_to_lower (gdk_unicode_to_keyval (c));
4351 while (src < next_src)
4365 while (src < next_src)
4368 *pattern_dest++ = ' ';
4380 gtk_label_set_uline_text_internal (GtkLabel *label,
4383 GtkLabelPrivate *priv = label->priv;
4384 guint accel_key = GDK_KEY_VoidSymbol;
4388 g_return_if_fail (GTK_IS_LABEL (label));
4389 g_return_if_fail (str != NULL);
4391 /* Split text into the base text and a separate pattern
4394 if (!separate_uline_pattern (str, &accel_key, &new_str, &pattern))
4397 gtk_label_set_text_internal (label, new_str);
4398 gtk_label_set_pattern_internal (label, pattern, TRUE);
4399 priv->mnemonic_keyval = accel_key;
4405 * gtk_label_set_text_with_mnemonic:
4406 * @label: a #GtkLabel
4409 * Sets the label's text from the string @str.
4410 * If characters in @str are preceded by an underscore, they are underlined
4411 * indicating that they represent a keyboard accelerator called a mnemonic.
4412 * The mnemonic key can be used to activate another widget, chosen
4413 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
4416 gtk_label_set_text_with_mnemonic (GtkLabel *label,
4419 g_return_if_fail (GTK_IS_LABEL (label));
4420 g_return_if_fail (str != NULL);
4422 g_object_freeze_notify (G_OBJECT (label));
4424 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
4425 gtk_label_set_use_markup_internal (label, FALSE);
4426 gtk_label_set_use_underline_internal (label, TRUE);
4428 gtk_label_recalculate (label);
4430 g_object_thaw_notify (G_OBJECT (label));
4434 gtk_label_realize (GtkWidget *widget)
4436 GtkLabel *label = GTK_LABEL (widget);
4437 GtkLabelPrivate *priv = label->priv;
4439 GTK_WIDGET_CLASS (gtk_label_parent_class)->realize (widget);
4441 if (priv->select_info)
4442 gtk_label_create_window (label);
4446 gtk_label_unrealize (GtkWidget *widget)
4448 GtkLabel *label = GTK_LABEL (widget);
4449 GtkLabelPrivate *priv = label->priv;
4451 if (priv->select_info)
4452 gtk_label_destroy_window (label);
4454 GTK_WIDGET_CLASS (gtk_label_parent_class)->unrealize (widget);
4458 gtk_label_map (GtkWidget *widget)
4460 GtkLabel *label = GTK_LABEL (widget);
4461 GtkLabelPrivate *priv = label->priv;
4463 GTK_WIDGET_CLASS (gtk_label_parent_class)->map (widget);
4465 if (priv->select_info)
4466 gdk_window_show (priv->select_info->window);
4470 gtk_label_unmap (GtkWidget *widget)
4472 GtkLabel *label = GTK_LABEL (widget);
4473 GtkLabelPrivate *priv = label->priv;
4475 if (priv->select_info)
4476 gdk_window_hide (priv->select_info->window);
4478 GTK_WIDGET_CLASS (gtk_label_parent_class)->unmap (widget);
4482 window_to_layout_coords (GtkLabel *label,
4486 GtkAllocation allocation;
4490 widget = GTK_WIDGET (label);
4492 /* get layout location in widget->window coords */
4493 get_layout_location (label, &lx, &ly);
4495 gtk_widget_get_allocation (widget, &allocation);
4499 *x += allocation.x; /* go to widget->window */
4500 *x -= lx; /* go to layout */
4505 *y += allocation.y; /* go to widget->window */
4506 *y -= ly; /* go to layout */
4512 layout_to_window_coords (GtkLabel *label,
4519 widget = GTK_WIDGET (label);
4521 /* get layout location in widget->window coords */
4522 get_layout_location (label, &lx, &ly);
4526 *x += lx; /* go to widget->window */
4527 *x -= widget->allocation.x; /* go to selection window */
4532 *y += ly; /* go to widget->window */
4533 *y -= widget->allocation.y; /* go to selection window */
4539 get_layout_index (GtkLabel *label,
4544 GtkLabelPrivate *priv = label->priv;
4546 const gchar *cluster;
4547 const gchar *cluster_end;
4552 gtk_label_ensure_layout (label);
4554 window_to_layout_coords (label, &x, &y);
4559 inside = pango_layout_xy_to_index (priv->layout,
4563 cluster = priv->text + *index;
4564 cluster_end = cluster;
4567 cluster_end = g_utf8_next_char (cluster_end);
4571 *index += (cluster_end - cluster);
4577 gtk_label_select_word (GtkLabel *label)
4579 GtkLabelPrivate *priv = label->priv;
4582 gint start_index = gtk_label_move_backward_word (label, priv->select_info->selection_end);
4583 gint end_index = gtk_label_move_forward_word (label, priv->select_info->selection_end);
4585 min = MIN (priv->select_info->selection_anchor,
4586 priv->select_info->selection_end);
4587 max = MAX (priv->select_info->selection_anchor,
4588 priv->select_info->selection_end);
4590 min = MIN (min, start_index);
4591 max = MAX (max, end_index);
4593 gtk_label_select_region_index (label, min, max);
4597 gtk_label_grab_focus (GtkWidget *widget)
4599 GtkLabel *label = GTK_LABEL (widget);
4600 GtkLabelPrivate *priv = label->priv;
4601 gboolean select_on_focus;
4604 if (priv->select_info == NULL)
4607 GTK_WIDGET_CLASS (gtk_label_parent_class)->grab_focus (widget);
4609 if (priv->select_info->selectable)
4611 g_object_get (gtk_widget_get_settings (widget),
4612 "gtk-label-select-on-focus",
4616 if (select_on_focus && !priv->in_click)
4617 gtk_label_select_region (label, 0, -1);
4621 if (priv->select_info->links && !priv->in_click)
4623 link = priv->select_info->links->data;
4624 priv->select_info->selection_anchor = link->start;
4625 priv->select_info->selection_end = link->start;
4631 gtk_label_focus (GtkWidget *widget,
4632 GtkDirectionType direction)
4634 GtkLabel *label = GTK_LABEL (widget);
4635 GtkLabelPrivate *priv = label->priv;
4636 GtkLabelSelectionInfo *info = priv->select_info;
4637 GtkLabelLink *focus_link;
4640 if (!gtk_widget_is_focus (widget))
4642 gtk_widget_grab_focus (widget);
4645 focus_link = gtk_label_get_focus_link (label);
4646 if (focus_link && direction == GTK_DIR_TAB_BACKWARD)
4648 l = g_list_last (info->links);
4649 focus_link = l->data;
4650 info->selection_anchor = focus_link->start;
4651 info->selection_end = focus_link->start;
4661 if (info->selectable)
4665 if (info->selection_anchor != info->selection_end)
4668 index = info->selection_anchor;
4670 if (direction == GTK_DIR_TAB_FORWARD)
4671 for (l = info->links; l; l = l->next)
4673 GtkLabelLink *link = l->data;
4675 if (link->start > index)
4677 gtk_label_select_region_index (label, link->start, link->start);
4681 else if (direction == GTK_DIR_TAB_BACKWARD)
4682 for (l = g_list_last (info->links); l; l = l->prev)
4684 GtkLabelLink *link = l->data;
4686 if (link->end < index)
4688 gtk_label_select_region_index (label, link->start, link->start);
4697 focus_link = gtk_label_get_focus_link (label);
4700 case GTK_DIR_TAB_FORWARD:
4703 l = g_list_find (info->links, focus_link);
4710 case GTK_DIR_TAB_BACKWARD:
4713 l = g_list_find (info->links, focus_link);
4717 l = g_list_last (info->links);
4726 focus_link = l->data;
4727 info->selection_anchor = focus_link->start;
4728 info->selection_end = focus_link->start;
4729 gtk_widget_queue_draw (widget);
4741 gtk_label_button_press (GtkWidget *widget,
4742 GdkEventButton *event)
4744 GtkLabel *label = GTK_LABEL (widget);
4745 GtkLabelPrivate *priv = label->priv;
4746 GtkLabelSelectionInfo *info = priv->select_info;
4753 if (info->active_link)
4755 if (event->button == 1)
4757 info->link_clicked = 1;
4758 gtk_widget_queue_draw (widget);
4760 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4762 info->link_clicked = 1;
4763 gtk_label_do_popup (label, event);
4768 if (!info->selectable)
4771 info->in_drag = FALSE;
4772 info->select_words = FALSE;
4774 if (event->button == 1)
4776 if (!gtk_widget_has_focus (widget))
4778 priv->in_click = TRUE;
4779 gtk_widget_grab_focus (widget);
4780 priv->in_click = FALSE;
4783 if (event->type == GDK_3BUTTON_PRESS)
4785 gtk_label_select_region_index (label, 0, strlen (priv->text));
4789 if (event->type == GDK_2BUTTON_PRESS)
4791 info->select_words = TRUE;
4792 gtk_label_select_word (label);
4796 get_layout_index (label, event->x, event->y, &index);
4798 min = MIN (info->selection_anchor, info->selection_end);
4799 max = MAX (info->selection_anchor, info->selection_end);
4801 if ((info->selection_anchor != info->selection_end) &&
4802 (event->state & GDK_SHIFT_MASK))
4804 /* extend (same as motion) */
4805 min = MIN (min, index);
4806 max = MAX (max, index);
4808 /* ensure the anchor is opposite index */
4816 gtk_label_select_region_index (label, min, max);
4820 if (event->type == GDK_3BUTTON_PRESS)
4821 gtk_label_select_region_index (label, 0, strlen (priv->text));
4822 else if (event->type == GDK_2BUTTON_PRESS)
4823 gtk_label_select_word (label);
4824 else if (min < max && min <= index && index <= max)
4826 info->in_drag = TRUE;
4827 info->drag_start_x = event->x;
4828 info->drag_start_y = event->y;
4831 /* start a replacement */
4832 gtk_label_select_region_index (label, index, index);
4837 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4839 gtk_label_do_popup (label, event);
4847 gtk_label_button_release (GtkWidget *widget,
4848 GdkEventButton *event)
4851 GtkLabel *label = GTK_LABEL (widget);
4852 GtkLabelPrivate *priv = label->priv;
4853 GtkLabelSelectionInfo *info = priv->select_info;
4863 get_layout_index (label, event->x, event->y, &index);
4864 gtk_label_select_region_index (label, index, index);
4869 if (event->button != 1)
4872 if (info->active_link &&
4873 info->selection_anchor == info->selection_end &&
4876 emit_activate_link (label, info->active_link);
4877 info->link_clicked = 0;
4882 /* The goal here is to return TRUE iff we ate the
4883 * button press to start selecting.
4889 connect_mnemonics_visible_notify (GtkLabel *label)
4891 GtkLabelPrivate *priv = label->priv;
4892 GtkWidget *toplevel;
4895 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
4897 if (!GTK_IS_WINDOW (toplevel))
4900 /* always set up this widgets initial value */
4901 priv->mnemonics_visible =
4902 gtk_window_get_mnemonics_visible (GTK_WINDOW (toplevel));
4905 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (toplevel),
4906 "gtk-label-mnemonics-visible-connected"));
4910 g_signal_connect (toplevel,
4911 "notify::mnemonics-visible",
4912 G_CALLBACK (label_mnemonics_visible_changed),
4914 g_object_set_data (G_OBJECT (toplevel),
4915 "gtk-label-mnemonics-visible-connected",
4916 GINT_TO_POINTER (1));
4921 drag_begin_cb (GtkWidget *widget,
4922 GdkDragContext *context,
4925 GtkLabel *label = GTK_LABEL (widget);
4926 GtkLabelPrivate *priv = label->priv;
4927 cairo_surface_t *surface = NULL;
4929 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
4931 if ((priv->select_info->selection_anchor !=
4932 priv->select_info->selection_end) &&
4938 start = MIN (priv->select_info->selection_anchor,
4939 priv->select_info->selection_end);
4940 end = MAX (priv->select_info->selection_anchor,
4941 priv->select_info->selection_end);
4943 len = strlen (priv->text);
4951 surface = _gtk_text_util_create_drag_icon (widget,
4958 gtk_drag_set_icon_surface (context, surface);
4959 cairo_surface_destroy (surface);
4963 gtk_drag_set_icon_default (context);
4968 gtk_label_motion (GtkWidget *widget,
4969 GdkEventMotion *event)
4971 GtkLabel *label = GTK_LABEL (widget);
4972 GtkLabelPrivate *priv = label->priv;
4973 GtkLabelSelectionInfo *info = priv->select_info;
4979 if (info->links && !info->in_drag)
4983 gboolean found = FALSE;
4985 if (info->selection_anchor == info->selection_end)
4987 if (get_layout_index (label, event->x, event->y, &index))
4989 for (l = info->links; l != NULL; l = l->next)
4992 if (index >= link->start && index <= link->end)
5003 if (info->active_link != link)
5005 info->link_clicked = 0;
5006 info->active_link = link;
5007 gtk_label_update_cursor (label);
5008 gtk_widget_queue_draw (widget);
5013 if (info->active_link != NULL)
5015 info->link_clicked = 0;
5016 info->active_link = NULL;
5017 gtk_label_update_cursor (label);
5018 gtk_widget_queue_draw (widget);
5023 if (!info->selectable)
5026 if ((event->state & GDK_BUTTON1_MASK) == 0)
5031 if (gtk_drag_check_threshold (widget,
5034 event->x, event->y))
5036 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
5038 gtk_target_list_add_text_targets (target_list, 0);
5040 g_signal_connect (widget, "drag-begin",
5041 G_CALLBACK (drag_begin_cb), NULL);
5042 gtk_drag_begin (widget, target_list,
5044 1, (GdkEvent *)event);
5046 info->in_drag = FALSE;
5048 gtk_target_list_unref (target_list);
5055 gdk_window_get_device_position (info->window, event->device, &x, &y, NULL);
5056 get_layout_index (label, x, y, &index);
5058 if (info->select_words)
5061 gint old_min, old_max;
5064 min = gtk_label_move_backward_word (label, index);
5065 max = gtk_label_move_forward_word (label, index);
5067 anchor = info->selection_anchor;
5068 end = info->selection_end;
5070 old_min = MIN (anchor, end);
5071 old_max = MAX (anchor, end);
5078 else if (old_max < max)
5083 else if (anchor == old_min)
5094 gtk_label_select_region_index (label, anchor, end);
5097 gtk_label_select_region_index (label, info->selection_anchor, index);
5104 gtk_label_leave_notify (GtkWidget *widget,
5105 GdkEventCrossing *event)
5107 GtkLabel *label = GTK_LABEL (widget);
5108 GtkLabelPrivate *priv = label->priv;
5110 if (priv->select_info)
5112 priv->select_info->active_link = NULL;
5113 gtk_label_update_cursor (label);
5114 gtk_widget_queue_draw (widget);
5117 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event)
5118 return GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event (widget, event);
5124 gtk_label_create_window (GtkLabel *label)
5126 GtkLabelPrivate *priv = label->priv;
5127 GtkAllocation allocation;
5129 GdkWindowAttr attributes;
5130 gint attributes_mask;
5132 g_assert (priv->select_info);
5133 widget = GTK_WIDGET (label);
5134 g_assert (gtk_widget_get_realized (widget));
5136 if (priv->select_info->window)
5139 gtk_widget_get_allocation (widget, &allocation);
5141 attributes.x = allocation.x;
5142 attributes.y = allocation.y;
5143 attributes.width = allocation.width;
5144 attributes.height = allocation.height;
5145 attributes.window_type = GDK_WINDOW_CHILD;
5146 attributes.wclass = GDK_INPUT_ONLY;
5147 attributes.override_redirect = TRUE;
5148 attributes.event_mask = gtk_widget_get_events (widget) |
5149 GDK_BUTTON_PRESS_MASK |
5150 GDK_BUTTON_RELEASE_MASK |
5151 GDK_LEAVE_NOTIFY_MASK |
5152 GDK_BUTTON_MOTION_MASK |
5153 GDK_POINTER_MOTION_MASK |
5154 GDK_POINTER_MOTION_HINT_MASK;
5155 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
5156 if (gtk_widget_is_sensitive (widget))
5158 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
5160 attributes_mask |= GDK_WA_CURSOR;
5164 priv->select_info->window = gdk_window_new (gtk_widget_get_window (widget),
5165 &attributes, attributes_mask);
5166 gdk_window_set_user_data (priv->select_info->window, widget);
5168 if (attributes_mask & GDK_WA_CURSOR)
5169 g_object_unref (attributes.cursor);
5173 gtk_label_destroy_window (GtkLabel *label)
5175 GtkLabelPrivate *priv = label->priv;
5177 g_assert (priv->select_info);
5179 if (priv->select_info->window == NULL)
5182 gdk_window_set_user_data (priv->select_info->window, NULL);
5183 gdk_window_destroy (priv->select_info->window);
5184 priv->select_info->window = NULL;
5188 gtk_label_ensure_select_info (GtkLabel *label)
5190 GtkLabelPrivate *priv = label->priv;
5192 if (priv->select_info == NULL)
5194 priv->select_info = g_new0 (GtkLabelSelectionInfo, 1);
5196 gtk_widget_set_can_focus (GTK_WIDGET (label), TRUE);
5198 if (gtk_widget_get_realized (GTK_WIDGET (label)))
5199 gtk_label_create_window (label);
5201 if (gtk_widget_get_mapped (GTK_WIDGET (label)))
5202 gdk_window_show (priv->select_info->window);
5207 gtk_label_clear_select_info (GtkLabel *label)
5209 GtkLabelPrivate *priv = label->priv;
5211 if (priv->select_info == NULL)
5214 if (!priv->select_info->selectable && !priv->select_info->links)
5216 gtk_label_destroy_window (label);
5218 g_free (priv->select_info);
5219 priv->select_info = NULL;
5221 gtk_widget_set_can_focus (GTK_WIDGET (label), FALSE);
5226 * gtk_label_set_selectable:
5227 * @label: a #GtkLabel
5228 * @setting: %TRUE to allow selecting text in the label
5230 * Selectable labels allow the user to select text from the label, for
5234 gtk_label_set_selectable (GtkLabel *label,
5237 GtkLabelPrivate *priv;
5238 gboolean old_setting;
5240 g_return_if_fail (GTK_IS_LABEL (label));
5244 setting = setting != FALSE;
5245 old_setting = priv->select_info && priv->select_info->selectable;
5249 gtk_label_ensure_select_info (label);
5250 priv->select_info->selectable = TRUE;
5251 gtk_label_update_cursor (label);
5257 /* unselect, to give up the selection */
5258 gtk_label_select_region (label, 0, 0);
5260 priv->select_info->selectable = FALSE;
5261 gtk_label_clear_select_info (label);
5262 gtk_label_update_cursor (label);
5265 if (setting != old_setting)
5267 g_object_freeze_notify (G_OBJECT (label));
5268 g_object_notify (G_OBJECT (label), "selectable");
5269 g_object_notify (G_OBJECT (label), "cursor-position");
5270 g_object_notify (G_OBJECT (label), "selection-bound");
5271 g_object_thaw_notify (G_OBJECT (label));
5272 gtk_widget_queue_draw (GTK_WIDGET (label));
5277 * gtk_label_get_selectable:
5278 * @label: a #GtkLabel
5280 * Gets the value set by gtk_label_set_selectable().
5282 * Return value: %TRUE if the user can copy text from the label
5285 gtk_label_get_selectable (GtkLabel *label)
5287 GtkLabelPrivate *priv;
5289 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5293 return priv->select_info && priv->select_info->selectable;
5297 * gtk_label_set_angle:
5298 * @label: a #GtkLabel
5299 * @angle: the angle that the baseline of the label makes with
5300 * the horizontal, in degrees, measured counterclockwise
5302 * Sets the angle of rotation for the label. An angle of 90 reads from
5303 * from bottom to top, an angle of 270, from top to bottom. The angle
5304 * setting for the label is ignored if the label is selectable,
5305 * wrapped, or ellipsized.
5310 gtk_label_set_angle (GtkLabel *label,
5313 GtkLabelPrivate *priv;
5315 g_return_if_fail (GTK_IS_LABEL (label));
5319 /* Canonicalize to [0,360]. We don't canonicalize 360 to 0, because
5320 * double property ranges are inclusive, and changing 360 to 0 would
5321 * make a property editor behave strangely.
5323 if (angle < 0 || angle > 360.0)
5324 angle = angle - 360. * floor (angle / 360.);
5326 if (priv->angle != angle)
5328 priv->angle = angle;
5330 gtk_label_clear_layout (label);
5331 gtk_widget_queue_resize (GTK_WIDGET (label));
5333 g_object_notify (G_OBJECT (label), "angle");
5338 * gtk_label_get_angle:
5339 * @label: a #GtkLabel
5341 * Gets the angle of rotation for the label. See
5342 * gtk_label_set_angle().
5344 * Return value: the angle of rotation for the label
5349 gtk_label_get_angle (GtkLabel *label)
5351 g_return_val_if_fail (GTK_IS_LABEL (label), 0.0);
5353 return label->priv->angle;
5357 gtk_label_set_selection_text (GtkLabel *label,
5358 GtkSelectionData *selection_data)
5360 GtkLabelPrivate *priv = label->priv;
5362 if ((priv->select_info->selection_anchor !=
5363 priv->select_info->selection_end) &&
5369 start = MIN (priv->select_info->selection_anchor,
5370 priv->select_info->selection_end);
5371 end = MAX (priv->select_info->selection_anchor,
5372 priv->select_info->selection_end);
5374 len = strlen (priv->text);
5382 gtk_selection_data_set_text (selection_data,
5389 gtk_label_drag_data_get (GtkWidget *widget,
5390 GdkDragContext *context,
5391 GtkSelectionData *selection_data,
5395 gtk_label_set_selection_text (GTK_LABEL (widget), selection_data);
5399 get_text_callback (GtkClipboard *clipboard,
5400 GtkSelectionData *selection_data,
5402 gpointer user_data_or_owner)
5404 gtk_label_set_selection_text (GTK_LABEL (user_data_or_owner), selection_data);
5408 clear_text_callback (GtkClipboard *clipboard,
5409 gpointer user_data_or_owner)
5412 GtkLabelPrivate *priv;
5414 label = GTK_LABEL (user_data_or_owner);
5417 if (priv->select_info)
5419 priv->select_info->selection_anchor = priv->select_info->selection_end;
5421 gtk_widget_queue_draw (GTK_WIDGET (label));
5426 gtk_label_select_region_index (GtkLabel *label,
5430 GtkLabelPrivate *priv;
5432 g_return_if_fail (GTK_IS_LABEL (label));
5436 if (priv->select_info && priv->select_info->selectable)
5438 GtkClipboard *clipboard;
5440 if (priv->select_info->selection_anchor == anchor_index &&
5441 priv->select_info->selection_end == end_index)
5444 priv->select_info->selection_anchor = anchor_index;
5445 priv->select_info->selection_end = end_index;
5447 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label),
5448 GDK_SELECTION_PRIMARY);
5450 if (anchor_index != end_index)
5452 GtkTargetList *list;
5453 GtkTargetEntry *targets;
5456 list = gtk_target_list_new (NULL, 0);
5457 gtk_target_list_add_text_targets (list, 0);
5458 targets = gtk_target_table_new_from_list (list, &n_targets);
5460 gtk_clipboard_set_with_owner (clipboard,
5463 clear_text_callback,
5466 gtk_target_table_free (targets, n_targets);
5467 gtk_target_list_unref (list);
5471 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (label))
5472 gtk_clipboard_clear (clipboard);
5475 gtk_widget_queue_draw (GTK_WIDGET (label));
5477 g_object_freeze_notify (G_OBJECT (label));
5478 g_object_notify (G_OBJECT (label), "cursor-position");
5479 g_object_notify (G_OBJECT (label), "selection-bound");
5480 g_object_thaw_notify (G_OBJECT (label));
5485 * gtk_label_select_region:
5486 * @label: a #GtkLabel
5487 * @start_offset: start offset (in characters not bytes)
5488 * @end_offset: end offset (in characters not bytes)
5490 * Selects a range of characters in the label, if the label is selectable.
5491 * See gtk_label_set_selectable(). If the label is not selectable,
5492 * this function has no effect. If @start_offset or
5493 * @end_offset are -1, then the end of the label will be substituted.
5496 gtk_label_select_region (GtkLabel *label,
5500 GtkLabelPrivate *priv;
5502 g_return_if_fail (GTK_IS_LABEL (label));
5506 if (priv->text && priv->select_info)
5508 if (start_offset < 0)
5509 start_offset = g_utf8_strlen (priv->text, -1);
5512 end_offset = g_utf8_strlen (priv->text, -1);
5514 gtk_label_select_region_index (label,
5515 g_utf8_offset_to_pointer (priv->text, start_offset) - priv->text,
5516 g_utf8_offset_to_pointer (priv->text, end_offset) - priv->text);
5521 * gtk_label_get_selection_bounds:
5522 * @label: a #GtkLabel
5523 * @start: (out): return location for start of selection, as a character offset
5524 * @end: (out): return location for end of selection, as a character offset
5526 * Gets the selected range of characters in the label, returning %TRUE
5527 * if there's a selection.
5529 * Return value: %TRUE if selection is non-empty
5532 gtk_label_get_selection_bounds (GtkLabel *label,
5536 GtkLabelPrivate *priv;
5538 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5542 if (priv->select_info == NULL)
5544 /* not a selectable label */
5554 gint start_index, end_index;
5555 gint start_offset, end_offset;
5558 start_index = MIN (priv->select_info->selection_anchor,
5559 priv->select_info->selection_end);
5560 end_index = MAX (priv->select_info->selection_anchor,
5561 priv->select_info->selection_end);
5563 len = strlen (priv->text);
5565 if (end_index > len)
5568 if (start_index > len)
5571 start_offset = g_utf8_strlen (priv->text, start_index);
5572 end_offset = g_utf8_strlen (priv->text, end_index);
5574 if (start_offset > end_offset)
5576 gint tmp = start_offset;
5577 start_offset = end_offset;
5582 *start = start_offset;
5587 return start_offset != end_offset;
5593 * gtk_label_get_layout:
5594 * @label: a #GtkLabel
5596 * Gets the #PangoLayout used to display the label.
5597 * The layout is useful to e.g. convert text positions to
5598 * pixel positions, in combination with gtk_label_get_layout_offsets().
5599 * The returned layout is owned by the @label so need not be
5600 * freed by the caller. The @label is free to recreate its layout at
5601 * any time, so it should be considered read-only.
5603 * Return value: (transfer none): the #PangoLayout for this label
5606 gtk_label_get_layout (GtkLabel *label)
5608 GtkLabelPrivate *priv;
5610 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
5614 gtk_label_ensure_layout (label);
5616 return priv->layout;
5620 * gtk_label_get_layout_offsets:
5621 * @label: a #GtkLabel
5622 * @x: (out) (allow-none): location to store X offset of layout, or %NULL
5623 * @y: (out) (allow-none): location to store Y offset of layout, or %NULL
5625 * Obtains the coordinates where the label will draw the #PangoLayout
5626 * representing the text in the label; useful to convert mouse events
5627 * into coordinates inside the #PangoLayout, e.g. to take some action
5628 * if some part of the label is clicked. Of course you will need to
5629 * create a #GtkEventBox to receive the events, and pack the label
5630 * inside it, since labels are a #GTK_NO_WINDOW widget. Remember
5631 * when using the #PangoLayout functions you need to convert to
5632 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
5635 gtk_label_get_layout_offsets (GtkLabel *label,
5639 g_return_if_fail (GTK_IS_LABEL (label));
5641 gtk_label_ensure_layout (label);
5643 get_layout_location (label, x, y);
5647 * gtk_label_set_use_markup:
5648 * @label: a #GtkLabel
5649 * @setting: %TRUE if the label's text should be parsed for markup.
5651 * Sets whether the text of the label contains markup in <link
5652 * linkend="PangoMarkupFormat">Pango's text markup
5653 * language</link>. See gtk_label_set_markup().
5656 gtk_label_set_use_markup (GtkLabel *label,
5659 g_return_if_fail (GTK_IS_LABEL (label));
5661 gtk_label_set_use_markup_internal (label, setting);
5662 gtk_label_recalculate (label);
5666 * gtk_label_get_use_markup:
5667 * @label: a #GtkLabel
5669 * Returns whether the label's text is interpreted as marked up with
5670 * the <link linkend="PangoMarkupFormat">Pango text markup
5671 * language</link>. See gtk_label_set_use_markup ().
5673 * Return value: %TRUE if the label's text will be parsed for markup.
5676 gtk_label_get_use_markup (GtkLabel *label)
5678 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5680 return label->priv->use_markup;
5684 * gtk_label_set_use_underline:
5685 * @label: a #GtkLabel
5686 * @setting: %TRUE if underlines in the text indicate mnemonics
5688 * If true, an underline in the text indicates the next character should be
5689 * used for the mnemonic accelerator key.
5692 gtk_label_set_use_underline (GtkLabel *label,
5695 g_return_if_fail (GTK_IS_LABEL (label));
5697 gtk_label_set_use_underline_internal (label, setting);
5698 gtk_label_recalculate (label);
5702 * gtk_label_get_use_underline:
5703 * @label: a #GtkLabel
5705 * Returns whether an embedded underline in the label indicates a
5706 * mnemonic. See gtk_label_set_use_underline().
5708 * Return value: %TRUE whether an embedded underline in the label indicates
5709 * the mnemonic accelerator keys.
5712 gtk_label_get_use_underline (GtkLabel *label)
5714 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5716 return label->priv->use_underline;
5720 * gtk_label_set_single_line_mode:
5721 * @label: a #GtkLabel
5722 * @single_line_mode: %TRUE if the label should be in single line mode
5724 * Sets whether the label is in single line mode.
5729 gtk_label_set_single_line_mode (GtkLabel *label,
5730 gboolean single_line_mode)
5732 GtkLabelPrivate *priv;
5734 g_return_if_fail (GTK_IS_LABEL (label));
5738 single_line_mode = single_line_mode != FALSE;
5740 if (priv->single_line_mode != single_line_mode)
5742 priv->single_line_mode = single_line_mode;
5744 gtk_label_clear_layout (label);
5745 gtk_widget_queue_resize (GTK_WIDGET (label));
5747 g_object_notify (G_OBJECT (label), "single-line-mode");
5752 * gtk_label_get_single_line_mode:
5753 * @label: a #GtkLabel
5755 * Returns whether the label is in single line mode.
5757 * Return value: %TRUE when the label is in single line mode.
5762 gtk_label_get_single_line_mode (GtkLabel *label)
5764 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5766 return label->priv->single_line_mode;
5769 /* Compute the X position for an offset that corresponds to the "more important
5770 * cursor position for that offset. We use this when trying to guess to which
5771 * end of the selection we should go to when the user hits the left or
5775 get_better_cursor (GtkLabel *label,
5780 GtkLabelPrivate *priv = label->priv;
5781 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5782 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5783 PangoDirection cursor_direction = get_cursor_direction (label);
5784 gboolean split_cursor;
5785 PangoRectangle strong_pos, weak_pos;
5787 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5788 "gtk-split-cursor", &split_cursor,
5791 gtk_label_ensure_layout (label);
5793 pango_layout_get_cursor_pos (priv->layout, index,
5794 &strong_pos, &weak_pos);
5798 *x = strong_pos.x / PANGO_SCALE;
5799 *y = strong_pos.y / PANGO_SCALE;
5803 if (keymap_direction == cursor_direction)
5805 *x = strong_pos.x / PANGO_SCALE;
5806 *y = strong_pos.y / PANGO_SCALE;
5810 *x = weak_pos.x / PANGO_SCALE;
5811 *y = weak_pos.y / PANGO_SCALE;
5818 gtk_label_move_logically (GtkLabel *label,
5822 GtkLabelPrivate *priv = label->priv;
5823 gint offset = g_utf8_pointer_to_offset (priv->text,
5824 priv->text + start);
5828 PangoLogAttr *log_attrs;
5832 gtk_label_ensure_layout (label);
5834 length = g_utf8_strlen (priv->text, -1);
5836 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5838 while (count > 0 && offset < length)
5842 while (offset < length && !log_attrs[offset].is_cursor_position);
5846 while (count < 0 && offset > 0)
5850 while (offset > 0 && !log_attrs[offset].is_cursor_position);
5858 return g_utf8_offset_to_pointer (priv->text, offset) - priv->text;
5862 gtk_label_move_visually (GtkLabel *label,
5866 GtkLabelPrivate *priv = label->priv;
5873 int new_index, new_trailing;
5874 gboolean split_cursor;
5877 gtk_label_ensure_layout (label);
5879 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5880 "gtk-split-cursor", &split_cursor,
5887 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5888 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5890 strong = keymap_direction == get_cursor_direction (label);
5895 pango_layout_move_cursor_visually (priv->layout, strong, index, 0, 1, &new_index, &new_trailing);
5900 pango_layout_move_cursor_visually (priv->layout, strong, index, 0, -1, &new_index, &new_trailing);
5904 if (new_index < 0 || new_index == G_MAXINT)
5909 while (new_trailing--)
5910 index = g_utf8_next_char (priv->text + new_index) - priv->text;
5917 gtk_label_move_forward_word (GtkLabel *label,
5920 GtkLabelPrivate *priv = label->priv;
5921 gint new_pos = g_utf8_pointer_to_offset (priv->text,
5922 priv->text + start);
5925 length = g_utf8_strlen (priv->text, -1);
5926 if (new_pos < length)
5928 PangoLogAttr *log_attrs;
5931 gtk_label_ensure_layout (label);
5933 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5935 /* Find the next word end */
5937 while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
5943 return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
5948 gtk_label_move_backward_word (GtkLabel *label,
5951 GtkLabelPrivate *priv = label->priv;
5952 gint new_pos = g_utf8_pointer_to_offset (priv->text,
5953 priv->text + start);
5957 PangoLogAttr *log_attrs;
5960 gtk_label_ensure_layout (label);
5962 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5966 /* Find the previous word beginning */
5967 while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
5973 return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
5977 gtk_label_move_cursor (GtkLabel *label,
5978 GtkMovementStep step,
5980 gboolean extend_selection)
5982 GtkLabelPrivate *priv = label->priv;
5986 if (priv->select_info == NULL)
5989 old_pos = new_pos = priv->select_info->selection_end;
5991 if (priv->select_info->selection_end != priv->select_info->selection_anchor &&
5994 /* If we have a current selection and aren't extending it, move to the
5995 * start/or end of the selection as appropriate
5999 case GTK_MOVEMENT_VISUAL_POSITIONS:
6002 gint anchor_x, anchor_y;
6003 gboolean end_is_left;
6005 get_better_cursor (label, priv->select_info->selection_end, &end_x, &end_y);
6006 get_better_cursor (label, priv->select_info->selection_anchor, &anchor_x, &anchor_y);
6008 end_is_left = (end_y < anchor_y) || (end_y == anchor_y && end_x < anchor_x);
6011 new_pos = end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
6013 new_pos = !end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
6016 case GTK_MOVEMENT_LOGICAL_POSITIONS:
6017 case GTK_MOVEMENT_WORDS:
6019 new_pos = MIN (priv->select_info->selection_end, priv->select_info->selection_anchor);
6021 new_pos = MAX (priv->select_info->selection_end, priv->select_info->selection_anchor);
6023 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
6024 case GTK_MOVEMENT_PARAGRAPH_ENDS:
6025 case GTK_MOVEMENT_BUFFER_ENDS:
6026 /* FIXME: Can do better here */
6027 new_pos = count < 0 ? 0 : strlen (priv->text);
6029 case GTK_MOVEMENT_DISPLAY_LINES:
6030 case GTK_MOVEMENT_PARAGRAPHS:
6031 case GTK_MOVEMENT_PAGES:
6032 case GTK_MOVEMENT_HORIZONTAL_PAGES:
6040 case GTK_MOVEMENT_LOGICAL_POSITIONS:
6041 new_pos = gtk_label_move_logically (label, new_pos, count);
6043 case GTK_MOVEMENT_VISUAL_POSITIONS:
6044 new_pos = gtk_label_move_visually (label, new_pos, count);
6045 if (new_pos == old_pos)
6047 if (!extend_selection)
6049 if (!gtk_widget_keynav_failed (GTK_WIDGET (label),
6051 GTK_DIR_RIGHT : GTK_DIR_LEFT))
6053 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
6056 gtk_widget_child_focus (toplevel,
6058 GTK_DIR_RIGHT : GTK_DIR_LEFT);
6063 gtk_widget_error_bell (GTK_WIDGET (label));
6067 case GTK_MOVEMENT_WORDS:
6070 new_pos = gtk_label_move_forward_word (label, new_pos);
6075 new_pos = gtk_label_move_backward_word (label, new_pos);
6078 if (new_pos == old_pos)
6079 gtk_widget_error_bell (GTK_WIDGET (label));
6081 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
6082 case GTK_MOVEMENT_PARAGRAPH_ENDS:
6083 case GTK_MOVEMENT_BUFFER_ENDS:
6084 /* FIXME: Can do better here */
6085 new_pos = count < 0 ? 0 : strlen (priv->text);
6086 if (new_pos == old_pos)
6087 gtk_widget_error_bell (GTK_WIDGET (label));
6089 case GTK_MOVEMENT_DISPLAY_LINES:
6090 case GTK_MOVEMENT_PARAGRAPHS:
6091 case GTK_MOVEMENT_PAGES:
6092 case GTK_MOVEMENT_HORIZONTAL_PAGES:
6097 if (extend_selection)
6098 gtk_label_select_region_index (label,
6099 priv->select_info->selection_anchor,
6102 gtk_label_select_region_index (label, new_pos, new_pos);
6106 gtk_label_copy_clipboard (GtkLabel *label)
6108 GtkLabelPrivate *priv = label->priv;
6110 if (priv->text && priv->select_info)
6114 GtkClipboard *clipboard;
6116 start = MIN (priv->select_info->selection_anchor,
6117 priv->select_info->selection_end);
6118 end = MAX (priv->select_info->selection_anchor,
6119 priv->select_info->selection_end);
6121 len = strlen (priv->text);
6129 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6132 gtk_clipboard_set_text (clipboard, priv->text + start, end - start);
6137 link = gtk_label_get_focus_link (label);
6139 gtk_clipboard_set_text (clipboard, link->uri, -1);
6145 gtk_label_select_all (GtkLabel *label)
6147 GtkLabelPrivate *priv = label->priv;
6149 gtk_label_select_region_index (label, 0, strlen (priv->text));
6152 /* Quick hack of a popup menu
6155 activate_cb (GtkWidget *menuitem,
6158 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6159 g_signal_emit_by_name (label, signal);
6163 append_action_signal (GtkLabel *label,
6165 const gchar *stock_id,
6166 const gchar *signal,
6169 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6171 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
6172 g_signal_connect (menuitem, "activate",
6173 G_CALLBACK (activate_cb), label);
6175 gtk_widget_set_sensitive (menuitem, sensitive);
6177 gtk_widget_show (menuitem);
6178 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6182 popup_menu_detach (GtkWidget *attach_widget,
6185 GtkLabel *label = GTK_LABEL (attach_widget);
6186 GtkLabelPrivate *priv = label->priv;
6188 if (priv->select_info)
6189 priv->select_info->popup_menu = NULL;
6193 popup_position_func (GtkMenu *menu,
6201 GtkAllocation allocation;
6205 label = GTK_LABEL (user_data);
6206 widget = GTK_WIDGET (label);
6208 g_return_if_fail (gtk_widget_get_realized (widget));
6210 screen = gtk_widget_get_screen (widget);
6211 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
6213 gtk_widget_get_allocation (widget, &allocation);
6218 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
6221 gtk_widget_get_allocation (widget, &allocation);
6223 *x += allocation.width / 2;
6224 *y += allocation.height;
6226 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
6227 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
6231 open_link_activate_cb (GtkMenuItem *menu_item,
6236 link = gtk_label_get_current_link (label);
6239 emit_activate_link (label, link);
6243 copy_link_activate_cb (GtkMenuItem *menu_item,
6246 GtkClipboard *clipboard;
6249 uri = gtk_label_get_current_uri (label);
6252 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6253 gtk_clipboard_set_text (clipboard, uri, -1);
6258 gtk_label_popup_menu (GtkWidget *widget)
6260 gtk_label_do_popup (GTK_LABEL (widget), NULL);
6266 gtk_label_do_popup (GtkLabel *label,
6267 GdkEventButton *event)
6269 GtkLabelPrivate *priv = label->priv;
6270 GtkWidget *menuitem;
6273 gboolean have_selection;
6276 if (!priv->select_info)
6279 if (priv->select_info->popup_menu)
6280 gtk_widget_destroy (priv->select_info->popup_menu);
6282 priv->select_info->popup_menu = menu = gtk_menu_new ();
6284 gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (label), popup_menu_detach);
6287 priv->select_info->selection_anchor != priv->select_info->selection_end;
6291 if (priv->select_info->link_clicked)
6292 link = priv->select_info->active_link;
6297 link = gtk_label_get_focus_link (label);
6299 if (!have_selection && link)
6302 menuitem = gtk_image_menu_item_new_with_mnemonic (_("_Open Link"));
6303 gtk_widget_show (menuitem);
6304 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6306 g_signal_connect (G_OBJECT (menuitem), "activate",
6307 G_CALLBACK (open_link_activate_cb), label);
6309 image = gtk_image_new_from_stock (GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_MENU);
6310 gtk_widget_show (image);
6311 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6313 /* Copy Link Address */
6314 menuitem = gtk_image_menu_item_new_with_mnemonic (_("Copy _Link Address"));
6315 gtk_widget_show (menuitem);
6316 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6318 g_signal_connect (G_OBJECT (menuitem), "activate",
6319 G_CALLBACK (copy_link_activate_cb), label);
6321 image = gtk_image_new_from_stock (GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
6322 gtk_widget_show (image);
6323 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6327 append_action_signal (label, menu, GTK_STOCK_CUT, "cut-clipboard", FALSE);
6328 append_action_signal (label, menu, GTK_STOCK_COPY, "copy-clipboard", have_selection);
6329 append_action_signal (label, menu, GTK_STOCK_PASTE, "paste-clipboard", FALSE);
6331 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
6332 gtk_widget_set_sensitive (menuitem, FALSE);
6333 gtk_widget_show (menuitem);
6334 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6336 menuitem = gtk_separator_menu_item_new ();
6337 gtk_widget_show (menuitem);
6338 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6340 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
6341 g_signal_connect_swapped (menuitem, "activate",
6342 G_CALLBACK (gtk_label_select_all), label);
6343 gtk_widget_show (menuitem);
6344 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6347 g_signal_emit (label, signals[POPULATE_POPUP], 0, menu);
6350 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6352 event->button, event->time);
6355 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6356 popup_position_func, label,
6357 0, gtk_get_current_event_time ());
6358 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
6363 gtk_label_clear_links (GtkLabel *label)
6365 GtkLabelPrivate *priv = label->priv;
6367 if (!priv->select_info)
6370 g_list_foreach (priv->select_info->links, (GFunc)link_free, NULL);
6371 g_list_free (priv->select_info->links);
6372 priv->select_info->links = NULL;
6373 priv->select_info->active_link = NULL;
6377 gtk_label_rescan_links (GtkLabel *label)
6379 GtkLabelPrivate *priv = label->priv;
6380 PangoLayout *layout = priv->layout;
6381 PangoAttrList *attlist;
6382 PangoAttrIterator *iter;
6385 if (!priv->select_info || !priv->select_info->links)
6388 attlist = pango_layout_get_attributes (layout);
6390 if (attlist == NULL)
6393 iter = pango_attr_list_get_iterator (attlist);
6395 links = priv->select_info->links;
6399 PangoAttribute *underline;
6400 PangoAttribute *color;
6402 underline = pango_attr_iterator_get (iter, PANGO_ATTR_UNDERLINE);
6403 color = pango_attr_iterator_get (iter, PANGO_ATTR_FOREGROUND);
6405 if (underline != NULL && color != NULL)
6408 PangoRectangle start_pos;
6409 PangoRectangle end_pos;
6412 pango_attr_iterator_range (iter, &start, &end);
6413 pango_layout_index_to_pos (layout, start, &start_pos);
6414 pango_layout_index_to_pos (layout, end, &end_pos);
6418 g_warning ("Ran out of links");
6422 links = links->next;
6423 link->start = start;
6426 } while (pango_attr_iterator_next (iter));
6428 pango_attr_iterator_destroy (iter);
6432 gtk_label_activate_link (GtkLabel *label,
6435 GtkWidget *widget = GTK_WIDGET (label);
6436 GError *error = NULL;
6438 if (!gtk_show_uri (gtk_widget_get_screen (widget),
6439 uri, gtk_get_current_event_time (), &error))
6441 g_warning ("Unable to show '%s': %s", uri, error->message);
6442 g_error_free (error);
6449 emit_activate_link (GtkLabel *label,
6452 GtkLabelPrivate *priv = label->priv;
6455 g_signal_emit (label, signals[ACTIVATE_LINK], 0, link->uri, &handled);
6456 if (handled && priv->track_links && !link->visited)
6458 link->visited = TRUE;
6459 /* FIXME: shouldn't have to redo everything here */
6460 gtk_label_recalculate (label);
6465 gtk_label_activate_current_link (GtkLabel *label)
6468 GtkWidget *widget = GTK_WIDGET (label);
6470 link = gtk_label_get_focus_link (label);
6474 emit_activate_link (label, link);
6478 GtkWidget *toplevel;
6480 GtkWidget *default_widget, *focus_widget;
6482 toplevel = gtk_widget_get_toplevel (widget);
6483 if (GTK_IS_WINDOW (toplevel))
6485 window = GTK_WINDOW (toplevel);
6489 default_widget = gtk_window_get_default_widget (window);
6490 focus_widget = gtk_window_get_focus (window);
6492 if (default_widget != widget &&
6493 !(widget == focus_widget && (!default_widget || !gtk_widget_is_sensitive (default_widget))))
6494 gtk_window_activate_default (window);
6500 static GtkLabelLink *
6501 gtk_label_get_current_link (GtkLabel *label)
6503 GtkLabelPrivate *priv = label->priv;
6506 if (!priv->select_info)
6509 if (priv->select_info->link_clicked)
6510 link = priv->select_info->active_link;
6512 link = gtk_label_get_focus_link (label);
6518 * gtk_label_get_current_uri:
6519 * @label: a #GtkLabel
6521 * Returns the URI for the currently active link in the label.
6522 * The active link is the one under the mouse pointer or, in a
6523 * selectable label, the link in which the text cursor is currently
6526 * This function is intended for use in a #GtkLabel::activate-link handler
6527 * or for use in a #GtkWidget::query-tooltip handler.
6529 * Returns: the currently active URI. The string is owned by GTK+ and must
6530 * not be freed or modified.
6534 G_CONST_RETURN gchar *
6535 gtk_label_get_current_uri (GtkLabel *label)
6539 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
6541 link = gtk_label_get_current_link (label);
6550 * gtk_label_set_track_visited_links:
6551 * @label: a #GtkLabel
6552 * @track_links: %TRUE to track visited links
6554 * Sets whether the label should keep track of clicked
6555 * links (and use a different color for them).
6560 gtk_label_set_track_visited_links (GtkLabel *label,
6561 gboolean track_links)
6563 GtkLabelPrivate *priv;
6565 g_return_if_fail (GTK_IS_LABEL (label));
6569 track_links = track_links != FALSE;
6571 if (priv->track_links != track_links)
6573 priv->track_links = track_links;
6575 /* FIXME: shouldn't have to redo everything here */
6576 gtk_label_recalculate (label);
6578 g_object_notify (G_OBJECT (label), "track-visited-links");
6583 * gtk_label_get_track_visited_links:
6584 * @label: a #GtkLabel
6586 * Returns whether the label is currently keeping track
6589 * Returns: %TRUE if clicked links are remembered
6594 gtk_label_get_track_visited_links (GtkLabel *label)
6596 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
6598 return label->priv->track_links;
6602 gtk_label_query_tooltip (GtkWidget *widget,
6605 gboolean keyboard_tip,
6606 GtkTooltip *tooltip)
6608 GtkLabel *label = GTK_LABEL (widget);
6609 GtkLabelPrivate *priv = label->priv;
6610 GtkLabelSelectionInfo *info = priv->select_info;
6614 if (info && info->links)
6618 if (info->selection_anchor == info->selection_end)
6619 index = info->selection_anchor;
6623 if (!get_layout_index (label, x, y, &index))
6629 for (l = info->links; l != NULL; l = l->next)
6631 GtkLabelLink *link = l->data;
6632 if (index >= link->start && index <= link->end)
6636 gtk_tooltip_set_markup (tooltip, link->title);
6645 return GTK_WIDGET_CLASS (gtk_label_parent_class)->query_tooltip (widget,