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"
55 #include "a11y/gtklabelaccessible.h"
59 * @Short_description: A widget that displays a small to medium amount of text
62 * The #GtkLabel widget displays a small amount of text. As the name
63 * implies, most labels are used to label another widget such as a
64 * #GtkButton, a #GtkMenuItem, or a #GtkOptionMenu.
66 * <refsect2 id="GtkLabel-BUILDER-UI">
67 * <title>GtkLabel as GtkBuildable</title>
69 * The GtkLabel implementation of the GtkBuildable interface supports a
70 * custom <attributes> element, which supports any number of <attribute>
71 * elements. the <attribute> element has attributes named name, value,
72 * start and end and allows you to specify #PangoAttribute values for this label.
75 * <title>A UI definition fragment specifying Pango attributes</title>
76 * <programlisting><![CDATA[
77 * <object class="GtkLabel">
79 * <attribute name="weight" value="PANGO_WEIGHT_BOLD"/>
80 * <attribute name="background" value="red" start="5" end="10"/>"
83 * ]]></programlisting>
85 * The start and end attributes specify the range of characters to which the
86 * Pango attribute applies. If start and end are not specified, the attribute is
87 * applied to the whole text. Note that specifying ranges does not make much
88 * sense with translatable attributes. Use markup embedded in the translatable
93 * <title>Mnemonics</title>
95 * Labels may contain <firstterm>mnemonics</firstterm>. Mnemonics are
96 * underlined characters in the label, used for keyboard navigation.
97 * Mnemonics are created by providing a string with an underscore before
98 * the mnemonic character, such as <literal>"_File"</literal>, to the
99 * functions gtk_label_new_with_mnemonic() or
100 * gtk_label_set_text_with_mnemonic().
102 * Mnemonics automatically activate any activatable widget the label is
103 * inside, such as a #GtkButton; if the label is not inside the
104 * mnemonic's target widget, you have to tell the label about the target
105 * using gtk_label_set_mnemonic_widget(). Here's a simple example where
106 * the label is inside a button:
110 * // Pressing Alt+H will activate this button
111 * button = gtk_button_new (<!-- -->);
112 * label = gtk_label_new_with_mnemonic ("_Hello");
113 * gtk_container_add (GTK_CONTAINER (button), label);
117 * There's a convenience function to create buttons with a mnemonic label
122 * // Pressing Alt+H will activate this button
123 * button = gtk_button_new_with_mnemonic ("_Hello");
127 * To create a mnemonic for a widget alongside the label, such as a
128 * #GtkEntry, you have to point the label at the entry with
129 * gtk_label_set_mnemonic_widget():
133 * // Pressing Alt+H will focus the entry
134 * entry = gtk_entry_new (<!-- -->);
135 * label = gtk_label_new_with_mnemonic ("_Hello");
136 * gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
142 * <title>Markup (styled text)</title>
144 * To make it easy to format text in a label (changing colors, fonts,
145 * etc.), label text can be provided in a simple <link
146 * linkend="PangoMarkupFormat">markup format</link>.
147 * Here's how to create a label with a small font:
151 * label = gtk_label_new (NULL);
152 * gtk_label_set_markup (GTK_LABEL (label), "<small>Small text</small>");
157 * linkend="PangoMarkupFormat">complete documentation</link> of available
158 * tags in the Pango manual.)
160 * The markup passed to gtk_label_set_markup() must be valid; for example,
161 * literal <, > and & characters must be escaped as \<,
162 * \gt;, and \&. If you pass text obtained from the user, file,
163 * or a network to gtk_label_set_markup(), you'll want to escape it with
164 * g_markup_escape_text() or g_markup_printf_escaped().
166 * Markup strings are just a convenient way to set the #PangoAttrList on
167 * a label; gtk_label_set_attributes() may be a simpler way to set
168 * attributes in some cases. Be careful though; #PangoAttrList tends to
169 * cause internationalization problems, unless you're applying attributes
170 * to the entire string (i.e. unless you set the range of each attribute
171 * to [0, %G_MAXINT)). The reason is that specifying the start_index and
172 * end_index for a #PangoAttribute requires knowledge of the exact string
173 * being displayed, so translations will cause problems.
177 * <title>Selectable labels</title>
178 * Labels can be made selectable with gtk_label_set_selectable().
179 * Selectable labels allow the user to copy the label contents to
180 * the clipboard. Only labels that contain useful-to-copy information
181 * — such as error messages — should be made selectable.
183 * <refsect2 id="label-text-layout">
184 * <title>Text layout</title>
186 * A label can contain any number of paragraphs, but will have
187 * performance problems if it contains more than a small number.
188 * Paragraphs are separated by newlines or other paragraph separators
189 * understood by Pango.
191 * Labels can automatically wrap text if you call
192 * gtk_label_set_line_wrap().
194 * gtk_label_set_justify() sets how the lines in a label align
195 * with one another. If you want to set how the label as a whole
196 * aligns in its available space, see gtk_misc_set_alignment().
198 * The #GtkLabel:width-chars and #GtkLabel:max-width-chars properties
199 * can be used to control the size allocation of ellipsized or wrapped
200 * labels. For ellipsizing labels, if either is specified (and less
201 * than the actual text size), it is used as the minimum width, and the actual
202 * text size is used as the natural width of the label. For wrapping labels,
203 * width-chars is used as the minimum width, if specified, and max-width-chars
204 * is used as the natural width. Even if max-width-chars specified, wrapping
205 * labels will be rewrapped to use all of the available width.
208 * Note that the interpretation of #GtkLabel:width-chars and
209 * #GtkLabel:max-width-chars has changed a bit with the introduction of
210 * <link linkend="geometry-management">width-for-height geometry management.</link>
215 * <title>Links</title>
217 * Since 2.18, GTK+ supports markup for clickable hyperlinks in addition
218 * to regular Pango markup. The markup for links is borrowed from HTML, using the
219 * <tag>a</tag> with href and title attributes. GTK+ renders links similar to the
220 * way they appear in web browsers, with colored, underlined text. The title
221 * attribute is displayed as a tooltip on the link. An example looks like this:
223 * <informalexample><programlisting>
224 * gtk_label_set_markup (label, "Go to the <a href="http://www.gtk.org" title="<i>Our</i> website">GTK+ website</a> for more...");
225 * </programlisting></informalexample>
227 * It is possible to implement custom handling for links and their tooltips with
228 * the #GtkLabel::activate-link signal and the gtk_label_get_current_uri() function.
234 /*rint() is only available in GCC and/or C99*/
235 #if (__STDC_VERSION__ < 199901L && !defined __GNUC__)
236 double rint(double x)
238 if (ceil(x+0.5) == floor(x+0.5))
240 int a = (int)ceil(x);
253 struct _GtkLabelPrivate
255 GtkLabelSelectionInfo *select_info;
256 GtkWidget *mnemonic_widget;
257 GtkWindow *mnemonic_window;
259 PangoAttrList *attrs;
260 PangoAttrList *effective_attrs;
268 guint mnemonics_visible : 1;
271 guint use_underline : 1;
272 guint use_markup : 1;
274 guint single_line_mode : 1;
275 guint have_transform : 1;
278 guint pattern_set : 1;
279 guint track_links : 1;
281 guint mnemonic_keyval;
284 gint max_width_chars;
287 /* Notes about the handling of links:
289 * Links share the GtkLabelSelectionInfo struct with selectable labels.
290 * There are some new fields for links. The links field contains the list
291 * of GtkLabelLink structs that describe the links which are embedded in
292 * the label. The active_link field points to the link under the mouse
293 * pointer. For keyboard navigation, the 'focus' link is determined by
294 * finding the link which contains the selection_anchor position.
295 * The link_clicked field is used with button press and release events
296 * to ensure that pressing inside a link and releasing outside of it
297 * does not activate the link.
299 * Links are rendered with the link-color/visited-link-color colors
300 * that are determined by the style and with an underline. When the mouse
301 * pointer is over a link, the pointer is changed to indicate the link,
302 * and the background behind the link is rendered with the base[PRELIGHT]
303 * color. While a button is pressed over a link, the background is rendered
304 * with the base[ACTIVE] color.
306 * Labels with links accept keyboard focus, and it is possible to move
307 * the focus between the embedded links using Tab/Shift-Tab. The focus
308 * is indicated by a focus rectangle that is drawn around the link text.
309 * Pressing Enter activates the focussed link, and there is a suitable
310 * context menu for links that can be opened with the Menu key. Pressing
311 * Control-C copies the link URI to the clipboard.
313 * In selectable labels with links, link functionality is only available
314 * when the selection is empty.
319 gchar *title; /* the title attribute, used as tooltip */
320 gboolean visited; /* get set when the link is activated; this flag
321 * gets preserved over later set_markup() calls
323 gint start; /* position of the link in the PangoLayout */
327 struct _GtkLabelSelectionInfo
330 gint selection_anchor;
332 GtkWidget *popup_menu;
335 GtkLabelLink *active_link;
341 guint select_words : 1;
342 guint selectable : 1;
343 guint link_clicked : 1;
351 ACTIVATE_CURRENT_LINK,
366 PROP_MNEMONIC_KEYVAL,
367 PROP_MNEMONIC_WIDGET,
368 PROP_CURSOR_POSITION,
369 PROP_SELECTION_BOUND,
372 PROP_SINGLE_LINE_MODE,
374 PROP_MAX_WIDTH_CHARS,
375 PROP_TRACK_VISITED_LINKS
378 /* When rotating ellipsizable text we want the natural size to request
379 * more to ensure the label wont ever ellipsize in an allocation of full natural size.
381 #define ROTATION_ELLIPSIZE_PADDING 2
383 static guint signals[LAST_SIGNAL] = { 0 };
385 static const GdkColor default_link_color = { 0, 0, 0, 0xeeee };
386 static const GdkColor default_visited_link_color = { 0, 0x5555, 0x1a1a, 0x8b8b };
388 static void gtk_label_set_property (GObject *object,
392 static void gtk_label_get_property (GObject *object,
396 static void gtk_label_finalize (GObject *object);
397 static void gtk_label_destroy (GtkWidget *widget);
398 static void gtk_label_size_allocate (GtkWidget *widget,
399 GtkAllocation *allocation);
400 static void gtk_label_state_changed (GtkWidget *widget,
402 static void gtk_label_style_updated (GtkWidget *widget);
403 static void gtk_label_direction_changed (GtkWidget *widget,
404 GtkTextDirection previous_dir);
405 static gint gtk_label_draw (GtkWidget *widget,
407 static gboolean gtk_label_focus (GtkWidget *widget,
408 GtkDirectionType direction);
410 static void gtk_label_realize (GtkWidget *widget);
411 static void gtk_label_unrealize (GtkWidget *widget);
412 static void gtk_label_map (GtkWidget *widget);
413 static void gtk_label_unmap (GtkWidget *widget);
415 static gboolean gtk_label_button_press (GtkWidget *widget,
416 GdkEventButton *event);
417 static gboolean gtk_label_button_release (GtkWidget *widget,
418 GdkEventButton *event);
419 static gboolean gtk_label_motion (GtkWidget *widget,
420 GdkEventMotion *event);
421 static gboolean gtk_label_leave_notify (GtkWidget *widget,
422 GdkEventCrossing *event);
424 static void gtk_label_grab_focus (GtkWidget *widget);
426 static gboolean gtk_label_query_tooltip (GtkWidget *widget,
429 gboolean keyboard_tip,
430 GtkTooltip *tooltip);
432 static void gtk_label_set_text_internal (GtkLabel *label,
434 static void gtk_label_set_label_internal (GtkLabel *label,
436 static void gtk_label_set_use_markup_internal (GtkLabel *label,
438 static void gtk_label_set_use_underline_internal (GtkLabel *label,
440 static void gtk_label_set_attributes_internal (GtkLabel *label,
441 PangoAttrList *attrs);
442 static void gtk_label_set_uline_text_internal (GtkLabel *label,
444 static void gtk_label_set_pattern_internal (GtkLabel *label,
445 const gchar *pattern,
446 gboolean is_mnemonic);
447 static void gtk_label_set_markup_internal (GtkLabel *label,
449 gboolean with_uline);
450 static void gtk_label_recalculate (GtkLabel *label);
451 static void gtk_label_hierarchy_changed (GtkWidget *widget,
452 GtkWidget *old_toplevel);
453 static void gtk_label_screen_changed (GtkWidget *widget,
454 GdkScreen *old_screen);
455 static gboolean gtk_label_popup_menu (GtkWidget *widget);
457 static void gtk_label_create_window (GtkLabel *label);
458 static void gtk_label_destroy_window (GtkLabel *label);
459 static void gtk_label_ensure_select_info (GtkLabel *label);
460 static void gtk_label_clear_select_info (GtkLabel *label);
461 static void gtk_label_update_cursor (GtkLabel *label);
462 static void gtk_label_clear_layout (GtkLabel *label);
463 static void gtk_label_ensure_layout (GtkLabel *label);
464 static void gtk_label_select_region_index (GtkLabel *label,
468 static gboolean gtk_label_mnemonic_activate (GtkWidget *widget,
469 gboolean group_cycling);
470 static void gtk_label_setup_mnemonic (GtkLabel *label,
472 static void gtk_label_drag_data_get (GtkWidget *widget,
473 GdkDragContext *context,
474 GtkSelectionData *selection_data,
478 static void gtk_label_buildable_interface_init (GtkBuildableIface *iface);
479 static gboolean gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
482 const gchar *tagname,
483 GMarkupParser *parser,
486 static void gtk_label_buildable_custom_finished (GtkBuildable *buildable,
489 const gchar *tagname,
493 static void connect_mnemonics_visible_notify (GtkLabel *label);
494 static gboolean separate_uline_pattern (const gchar *str,
500 /* For selectable labels: */
501 static void gtk_label_move_cursor (GtkLabel *label,
502 GtkMovementStep step,
504 gboolean extend_selection);
505 static void gtk_label_copy_clipboard (GtkLabel *label);
506 static void gtk_label_select_all (GtkLabel *label);
507 static void gtk_label_do_popup (GtkLabel *label,
508 GdkEventButton *event);
509 static gint gtk_label_move_forward_word (GtkLabel *label,
511 static gint gtk_label_move_backward_word (GtkLabel *label,
515 static void gtk_label_rescan_links (GtkLabel *label);
516 static void gtk_label_clear_links (GtkLabel *label);
517 static gboolean gtk_label_activate_link (GtkLabel *label,
519 static void gtk_label_activate_current_link (GtkLabel *label);
520 static GtkLabelLink *gtk_label_get_current_link (GtkLabel *label);
521 static void gtk_label_get_link_colors (GtkWidget *widget,
522 GdkColor **link_color,
523 GdkColor **visited_link_color);
524 static void emit_activate_link (GtkLabel *label,
527 static GtkSizeRequestMode gtk_label_get_request_mode (GtkWidget *widget);
528 static void gtk_label_get_preferred_width (GtkWidget *widget,
531 static void gtk_label_get_preferred_height (GtkWidget *widget,
534 static void gtk_label_get_preferred_width_for_height (GtkWidget *widget,
537 gint *natural_width);
538 static void gtk_label_get_preferred_height_for_width (GtkWidget *widget,
540 gint *minimum_height,
541 gint *natural_height);
543 static GtkBuildableIface *buildable_parent_iface = NULL;
545 G_DEFINE_TYPE_WITH_CODE (GtkLabel, gtk_label, GTK_TYPE_MISC,
546 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
547 gtk_label_buildable_interface_init))
550 add_move_binding (GtkBindingSet *binding_set,
553 GtkMovementStep step,
556 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
558 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
562 G_TYPE_BOOLEAN, FALSE);
564 /* Selection-extending version */
565 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
569 G_TYPE_BOOLEAN, TRUE);
573 gtk_label_class_init (GtkLabelClass *class)
575 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
576 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
577 GtkBindingSet *binding_set;
579 gobject_class->set_property = gtk_label_set_property;
580 gobject_class->get_property = gtk_label_get_property;
581 gobject_class->finalize = gtk_label_finalize;
583 widget_class->destroy = gtk_label_destroy;
584 widget_class->size_allocate = gtk_label_size_allocate;
585 widget_class->state_changed = gtk_label_state_changed;
586 widget_class->style_updated = gtk_label_style_updated;
587 widget_class->query_tooltip = gtk_label_query_tooltip;
588 widget_class->direction_changed = gtk_label_direction_changed;
589 widget_class->draw = gtk_label_draw;
590 widget_class->realize = gtk_label_realize;
591 widget_class->unrealize = gtk_label_unrealize;
592 widget_class->map = gtk_label_map;
593 widget_class->unmap = gtk_label_unmap;
594 widget_class->button_press_event = gtk_label_button_press;
595 widget_class->button_release_event = gtk_label_button_release;
596 widget_class->motion_notify_event = gtk_label_motion;
597 widget_class->leave_notify_event = gtk_label_leave_notify;
598 widget_class->hierarchy_changed = gtk_label_hierarchy_changed;
599 widget_class->screen_changed = gtk_label_screen_changed;
600 widget_class->mnemonic_activate = gtk_label_mnemonic_activate;
601 widget_class->drag_data_get = gtk_label_drag_data_get;
602 widget_class->grab_focus = gtk_label_grab_focus;
603 widget_class->popup_menu = gtk_label_popup_menu;
604 widget_class->focus = gtk_label_focus;
605 widget_class->get_request_mode = gtk_label_get_request_mode;
606 widget_class->get_preferred_width = gtk_label_get_preferred_width;
607 widget_class->get_preferred_height = gtk_label_get_preferred_height;
608 widget_class->get_preferred_width_for_height = gtk_label_get_preferred_width_for_height;
609 widget_class->get_preferred_height_for_width = gtk_label_get_preferred_height_for_width;
611 class->move_cursor = gtk_label_move_cursor;
612 class->copy_clipboard = gtk_label_copy_clipboard;
613 class->activate_link = gtk_label_activate_link;
616 * GtkLabel::move-cursor:
617 * @entry: the object which received the signal
618 * @step: the granularity of the move, as a #GtkMovementStep
619 * @count: the number of @step units to move
620 * @extend_selection: %TRUE if the move should extend the selection
622 * The ::move-cursor signal is a
623 * <link linkend="keybinding-signals">keybinding signal</link>
624 * which gets emitted when the user initiates a cursor movement.
625 * If the cursor is not visible in @entry, this signal causes
626 * the viewport to be moved instead.
628 * Applications should not connect to it, but may emit it with
629 * g_signal_emit_by_name() if they need to control the cursor
632 * The default bindings for this signal come in two variants,
633 * the variant with the Shift modifier extends the selection,
634 * the variant without the Shift modifer does not.
635 * There are too many key combinations to list them all here.
637 * <listitem>Arrow keys move by individual characters/lines</listitem>
638 * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
639 * <listitem>Home/End keys move to the ends of the buffer</listitem>
642 signals[MOVE_CURSOR] =
643 g_signal_new (I_("move-cursor"),
644 G_OBJECT_CLASS_TYPE (gobject_class),
645 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
646 G_STRUCT_OFFSET (GtkLabelClass, move_cursor),
648 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
650 GTK_TYPE_MOVEMENT_STEP,
655 * GtkLabel::copy-clipboard:
656 * @label: the object which received the signal
658 * The ::copy-clipboard signal is a
659 * <link linkend="keybinding-signals">keybinding signal</link>
660 * which gets emitted to copy the selection to the clipboard.
662 * The default binding for this signal is Ctrl-c.
664 signals[COPY_CLIPBOARD] =
665 g_signal_new (I_("copy-clipboard"),
666 G_OBJECT_CLASS_TYPE (gobject_class),
667 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
668 G_STRUCT_OFFSET (GtkLabelClass, copy_clipboard),
670 _gtk_marshal_VOID__VOID,
674 * GtkLabel::populate-popup:
675 * @label: The label on which the signal is emitted
676 * @menu: the menu that is being populated
678 * The ::populate-popup signal gets emitted before showing the
679 * context menu of the label. Note that only selectable labels
680 * have context menus.
682 * If you need to add items to the context menu, connect
683 * to this signal and append your menuitems to the @menu.
685 signals[POPULATE_POPUP] =
686 g_signal_new (I_("populate-popup"),
687 G_OBJECT_CLASS_TYPE (gobject_class),
689 G_STRUCT_OFFSET (GtkLabelClass, populate_popup),
691 _gtk_marshal_VOID__OBJECT,
696 * GtkLabel::activate-current-link:
697 * @label: The label on which the signal was emitted
699 * A <link linkend="keybinding-signals">keybinding signal</link>
700 * which gets emitted when the user activates a link in the label.
702 * Applications may also emit the signal with g_signal_emit_by_name()
703 * if they need to control activation of URIs programmatically.
705 * The default bindings for this signal are all forms of the Enter key.
709 signals[ACTIVATE_CURRENT_LINK] =
710 g_signal_new_class_handler ("activate-current-link",
711 G_TYPE_FROM_CLASS (gobject_class),
712 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
713 G_CALLBACK (gtk_label_activate_current_link),
715 _gtk_marshal_VOID__VOID,
719 * GtkLabel::activate-link:
720 * @label: The label on which the signal was emitted
721 * @uri: the URI that is activated
723 * The signal which gets emitted to activate a URI.
724 * Applications may connect to it to override the default behaviour,
725 * which is to call gtk_show_uri().
727 * Returns: %TRUE if the link has been activated
731 signals[ACTIVATE_LINK] =
732 g_signal_new ("activate-link",
733 G_TYPE_FROM_CLASS (gobject_class),
735 G_STRUCT_OFFSET (GtkLabelClass, activate_link),
736 _gtk_boolean_handled_accumulator, NULL,
737 _gtk_marshal_BOOLEAN__STRING,
738 G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
740 g_object_class_install_property (gobject_class,
742 g_param_spec_string ("label",
744 P_("The text of the label"),
746 GTK_PARAM_READWRITE));
747 g_object_class_install_property (gobject_class,
749 g_param_spec_boxed ("attributes",
751 P_("A list of style attributes to apply to the text of the label"),
752 PANGO_TYPE_ATTR_LIST,
753 GTK_PARAM_READWRITE));
754 g_object_class_install_property (gobject_class,
756 g_param_spec_boolean ("use-markup",
758 P_("The text of the label includes XML markup. See pango_parse_markup()"),
760 GTK_PARAM_READWRITE));
761 g_object_class_install_property (gobject_class,
763 g_param_spec_boolean ("use-underline",
765 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
767 GTK_PARAM_READWRITE));
769 g_object_class_install_property (gobject_class,
771 g_param_spec_enum ("justify",
773 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"),
774 GTK_TYPE_JUSTIFICATION,
776 GTK_PARAM_READWRITE));
778 g_object_class_install_property (gobject_class,
780 g_param_spec_string ("pattern",
782 P_("A string with _ characters in positions correspond to characters in the text to underline"),
784 GTK_PARAM_WRITABLE));
786 g_object_class_install_property (gobject_class,
788 g_param_spec_boolean ("wrap",
790 P_("If set, wrap lines if the text becomes too wide"),
792 GTK_PARAM_READWRITE));
794 * GtkLabel:wrap-mode:
796 * If line wrapping is on (see the #GtkLabel:wrap property) this controls
797 * how the line wrapping is done. The default is %PANGO_WRAP_WORD, which
798 * means wrap on word boundaries.
802 g_object_class_install_property (gobject_class,
804 g_param_spec_enum ("wrap-mode",
805 P_("Line wrap mode"),
806 P_("If wrap is set, controls how linewrapping is done"),
807 PANGO_TYPE_WRAP_MODE,
809 GTK_PARAM_READWRITE));
810 g_object_class_install_property (gobject_class,
812 g_param_spec_boolean ("selectable",
814 P_("Whether the label text can be selected with the mouse"),
816 GTK_PARAM_READWRITE));
817 g_object_class_install_property (gobject_class,
818 PROP_MNEMONIC_KEYVAL,
819 g_param_spec_uint ("mnemonic-keyval",
821 P_("The mnemonic accelerator key for this label"),
825 GTK_PARAM_READABLE));
826 g_object_class_install_property (gobject_class,
827 PROP_MNEMONIC_WIDGET,
828 g_param_spec_object ("mnemonic-widget",
829 P_("Mnemonic widget"),
830 P_("The widget to be activated when the label's mnemonic "
833 GTK_PARAM_READWRITE));
835 g_object_class_install_property (gobject_class,
836 PROP_CURSOR_POSITION,
837 g_param_spec_int ("cursor-position",
838 P_("Cursor Position"),
839 P_("The current position of the insertion cursor in chars"),
843 GTK_PARAM_READABLE));
845 g_object_class_install_property (gobject_class,
846 PROP_SELECTION_BOUND,
847 g_param_spec_int ("selection-bound",
848 P_("Selection Bound"),
849 P_("The position of the opposite end of the selection from the cursor in chars"),
853 GTK_PARAM_READABLE));
856 * GtkLabel:ellipsize:
858 * The preferred place to ellipsize the string, if the label does
859 * not have enough room to display the entire string, specified as a
860 * #PangoEllisizeMode.
862 * Note that setting this property to a value other than
863 * %PANGO_ELLIPSIZE_NONE has the side-effect that the label requests
864 * only enough space to display the ellipsis "...". In particular, this
865 * means that ellipsizing labels do not work well in notebook tabs, unless
866 * the tab's #GtkNotebook:tab-expand property is set to %TRUE. Other ways
867 * to set a label's width are gtk_widget_set_size_request() and
868 * gtk_label_set_width_chars().
872 g_object_class_install_property (gobject_class,
874 g_param_spec_enum ("ellipsize",
876 P_("The preferred place to ellipsize the string, if the label does not have enough room to display the entire string"),
877 PANGO_TYPE_ELLIPSIZE_MODE,
878 PANGO_ELLIPSIZE_NONE,
879 GTK_PARAM_READWRITE));
882 * GtkLabel:width-chars:
884 * The desired width of the label, in characters. If this property is set to
885 * -1, the width will be calculated automatically.
887 * See the section on <link linkend="label-text-layout">text layout</link>
888 * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
889 * determine the width of ellipsized and wrapped labels.
893 g_object_class_install_property (gobject_class,
895 g_param_spec_int ("width-chars",
896 P_("Width In Characters"),
897 P_("The desired width of the label, in characters"),
901 GTK_PARAM_READWRITE));
904 * GtkLabel:single-line-mode:
906 * Whether the label is in single line mode. In single line mode,
907 * the height of the label does not depend on the actual text, it
908 * is always set to ascent + descent of the font. This can be an
909 * advantage in situations where resizing the label because of text
910 * changes would be distracting, e.g. in a statusbar.
914 g_object_class_install_property (gobject_class,
915 PROP_SINGLE_LINE_MODE,
916 g_param_spec_boolean ("single-line-mode",
917 P_("Single Line Mode"),
918 P_("Whether the label is in single line mode"),
920 GTK_PARAM_READWRITE));
925 * The angle that the baseline of the label makes with the horizontal,
926 * in degrees, measured counterclockwise. An angle of 90 reads from
927 * from bottom to top, an angle of 270, from top to bottom. Ignored
928 * if the label is selectable, wrapped, or ellipsized.
932 g_object_class_install_property (gobject_class,
934 g_param_spec_double ("angle",
936 P_("Angle at which the label is rotated"),
940 GTK_PARAM_READWRITE));
943 * GtkLabel:max-width-chars:
945 * The desired maximum width of the label, in characters. If this property
946 * is set to -1, the width will be calculated automatically.
948 * See the section on <link linkend="label-text-layout">text layout</link>
949 * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
950 * determine the width of ellipsized and wrapped labels.
954 g_object_class_install_property (gobject_class,
955 PROP_MAX_WIDTH_CHARS,
956 g_param_spec_int ("max-width-chars",
957 P_("Maximum Width In Characters"),
958 P_("The desired maximum width of the label, in characters"),
962 GTK_PARAM_READWRITE));
965 * GtkLabel:track-visited-links:
967 * Set this property to %TRUE to make the label track which links
968 * have been clicked. It will then apply the ::visited-link-color
969 * color, instead of ::link-color.
973 g_object_class_install_property (gobject_class,
974 PROP_TRACK_VISITED_LINKS,
975 g_param_spec_boolean ("track-visited-links",
976 P_("Track visited links"),
977 P_("Whether visited links should be tracked"),
979 GTK_PARAM_READWRITE));
984 binding_set = gtk_binding_set_by_class (class);
986 /* Moving the insertion point */
987 add_move_binding (binding_set, GDK_KEY_Right, 0,
988 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
990 add_move_binding (binding_set, GDK_KEY_Left, 0,
991 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
993 add_move_binding (binding_set, GDK_KEY_KP_Right, 0,
994 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
996 add_move_binding (binding_set, GDK_KEY_KP_Left, 0,
997 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
999 add_move_binding (binding_set, GDK_KEY_f, GDK_CONTROL_MASK,
1000 GTK_MOVEMENT_LOGICAL_POSITIONS, 1);
1002 add_move_binding (binding_set, GDK_KEY_b, GDK_CONTROL_MASK,
1003 GTK_MOVEMENT_LOGICAL_POSITIONS, -1);
1005 add_move_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK,
1006 GTK_MOVEMENT_WORDS, 1);
1008 add_move_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK,
1009 GTK_MOVEMENT_WORDS, -1);
1011 add_move_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK,
1012 GTK_MOVEMENT_WORDS, 1);
1014 add_move_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK,
1015 GTK_MOVEMENT_WORDS, -1);
1018 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
1020 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1022 G_TYPE_BOOLEAN, FALSE);
1024 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
1026 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1028 G_TYPE_BOOLEAN, TRUE);
1030 gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
1032 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1034 G_TYPE_BOOLEAN, FALSE);
1036 gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
1038 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1040 G_TYPE_BOOLEAN, TRUE);
1043 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1045 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1047 G_TYPE_BOOLEAN, FALSE);
1049 gtk_binding_entry_add_signal (binding_set, GDK_KEY_backslash, GDK_CONTROL_MASK,
1051 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1053 G_TYPE_BOOLEAN, FALSE);
1055 add_move_binding (binding_set, GDK_KEY_f, GDK_MOD1_MASK,
1056 GTK_MOVEMENT_WORDS, 1);
1058 add_move_binding (binding_set, GDK_KEY_b, GDK_MOD1_MASK,
1059 GTK_MOVEMENT_WORDS, -1);
1061 add_move_binding (binding_set, GDK_KEY_Home, 0,
1062 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1064 add_move_binding (binding_set, GDK_KEY_End, 0,
1065 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1067 add_move_binding (binding_set, GDK_KEY_KP_Home, 0,
1068 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1070 add_move_binding (binding_set, GDK_KEY_KP_End, 0,
1071 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1073 add_move_binding (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK,
1074 GTK_MOVEMENT_BUFFER_ENDS, -1);
1076 add_move_binding (binding_set, GDK_KEY_End, GDK_CONTROL_MASK,
1077 GTK_MOVEMENT_BUFFER_ENDS, 1);
1079 add_move_binding (binding_set, GDK_KEY_KP_Home, GDK_CONTROL_MASK,
1080 GTK_MOVEMENT_BUFFER_ENDS, -1);
1082 add_move_binding (binding_set, GDK_KEY_KP_End, GDK_CONTROL_MASK,
1083 GTK_MOVEMENT_BUFFER_ENDS, 1);
1086 gtk_binding_entry_add_signal (binding_set, GDK_KEY_c, GDK_CONTROL_MASK,
1087 "copy-clipboard", 0);
1089 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Return, 0,
1090 "activate-current-link", 0);
1091 gtk_binding_entry_add_signal (binding_set, GDK_KEY_ISO_Enter, 0,
1092 "activate-current-link", 0);
1093 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Enter, 0,
1094 "activate-current-link", 0);
1096 g_type_class_add_private (class, sizeof (GtkLabelPrivate));
1098 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_LABEL_ACCESSIBLE);
1102 gtk_label_set_property (GObject *object,
1104 const GValue *value,
1107 GtkLabel *label = GTK_LABEL (object);
1112 gtk_label_set_label (label, g_value_get_string (value));
1114 case PROP_ATTRIBUTES:
1115 gtk_label_set_attributes (label, g_value_get_boxed (value));
1117 case PROP_USE_MARKUP:
1118 gtk_label_set_use_markup (label, g_value_get_boolean (value));
1120 case PROP_USE_UNDERLINE:
1121 gtk_label_set_use_underline (label, g_value_get_boolean (value));
1124 gtk_label_set_justify (label, g_value_get_enum (value));
1127 gtk_label_set_pattern (label, g_value_get_string (value));
1130 gtk_label_set_line_wrap (label, g_value_get_boolean (value));
1132 case PROP_WRAP_MODE:
1133 gtk_label_set_line_wrap_mode (label, g_value_get_enum (value));
1135 case PROP_SELECTABLE:
1136 gtk_label_set_selectable (label, g_value_get_boolean (value));
1138 case PROP_MNEMONIC_WIDGET:
1139 gtk_label_set_mnemonic_widget (label, (GtkWidget*) g_value_get_object (value));
1141 case PROP_ELLIPSIZE:
1142 gtk_label_set_ellipsize (label, g_value_get_enum (value));
1144 case PROP_WIDTH_CHARS:
1145 gtk_label_set_width_chars (label, g_value_get_int (value));
1147 case PROP_SINGLE_LINE_MODE:
1148 gtk_label_set_single_line_mode (label, g_value_get_boolean (value));
1151 gtk_label_set_angle (label, g_value_get_double (value));
1153 case PROP_MAX_WIDTH_CHARS:
1154 gtk_label_set_max_width_chars (label, g_value_get_int (value));
1156 case PROP_TRACK_VISITED_LINKS:
1157 gtk_label_set_track_visited_links (label, g_value_get_boolean (value));
1160 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1166 gtk_label_get_property (GObject *object,
1171 GtkLabel *label = GTK_LABEL (object);
1172 GtkLabelPrivate *priv = label->priv;
1177 g_value_set_string (value, priv->label);
1179 case PROP_ATTRIBUTES:
1180 g_value_set_boxed (value, priv->attrs);
1182 case PROP_USE_MARKUP:
1183 g_value_set_boolean (value, priv->use_markup);
1185 case PROP_USE_UNDERLINE:
1186 g_value_set_boolean (value, priv->use_underline);
1189 g_value_set_enum (value, priv->jtype);
1192 g_value_set_boolean (value, priv->wrap);
1194 case PROP_WRAP_MODE:
1195 g_value_set_enum (value, priv->wrap_mode);
1197 case PROP_SELECTABLE:
1198 g_value_set_boolean (value, gtk_label_get_selectable (label));
1200 case PROP_MNEMONIC_KEYVAL:
1201 g_value_set_uint (value, priv->mnemonic_keyval);
1203 case PROP_MNEMONIC_WIDGET:
1204 g_value_set_object (value, (GObject*) priv->mnemonic_widget);
1206 case PROP_CURSOR_POSITION:
1207 if (priv->select_info && priv->select_info->selectable)
1209 gint offset = g_utf8_pointer_to_offset (priv->text,
1210 priv->text + priv->select_info->selection_end);
1211 g_value_set_int (value, offset);
1214 g_value_set_int (value, 0);
1216 case PROP_SELECTION_BOUND:
1217 if (priv->select_info && priv->select_info->selectable)
1219 gint offset = g_utf8_pointer_to_offset (priv->text,
1220 priv->text + priv->select_info->selection_anchor);
1221 g_value_set_int (value, offset);
1224 g_value_set_int (value, 0);
1226 case PROP_ELLIPSIZE:
1227 g_value_set_enum (value, priv->ellipsize);
1229 case PROP_WIDTH_CHARS:
1230 g_value_set_int (value, gtk_label_get_width_chars (label));
1232 case PROP_SINGLE_LINE_MODE:
1233 g_value_set_boolean (value, gtk_label_get_single_line_mode (label));
1236 g_value_set_double (value, gtk_label_get_angle (label));
1238 case PROP_MAX_WIDTH_CHARS:
1239 g_value_set_int (value, gtk_label_get_max_width_chars (label));
1241 case PROP_TRACK_VISITED_LINKS:
1242 g_value_set_boolean (value, gtk_label_get_track_visited_links (label));
1245 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1251 gtk_label_init (GtkLabel *label)
1253 GtkLabelPrivate *priv;
1255 label->priv = G_TYPE_INSTANCE_GET_PRIVATE (label,
1260 gtk_widget_set_has_window (GTK_WIDGET (label), FALSE);
1262 priv->width_chars = -1;
1263 priv->max_width_chars = -1;
1266 priv->jtype = GTK_JUSTIFY_LEFT;
1268 priv->wrap_mode = PANGO_WRAP_WORD;
1269 priv->ellipsize = PANGO_ELLIPSIZE_NONE;
1271 priv->use_underline = FALSE;
1272 priv->use_markup = FALSE;
1273 priv->pattern_set = FALSE;
1274 priv->track_links = TRUE;
1276 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
1277 priv->layout = NULL;
1281 priv->mnemonic_widget = NULL;
1282 priv->mnemonic_window = NULL;
1284 priv->mnemonics_visible = TRUE;
1286 gtk_label_set_text (label, "");
1291 gtk_label_buildable_interface_init (GtkBuildableIface *iface)
1293 buildable_parent_iface = g_type_interface_peek_parent (iface);
1295 iface->custom_tag_start = gtk_label_buildable_custom_tag_start;
1296 iface->custom_finished = gtk_label_buildable_custom_finished;
1300 GtkBuilder *builder;
1302 PangoAttrList *attrs;
1305 static PangoAttribute *
1306 attribute_from_text (GtkBuilder *builder,
1311 PangoAttribute *attribute = NULL;
1313 PangoLanguage *language;
1314 PangoFontDescription *font_desc;
1316 GValue val = { 0, };
1318 if (!gtk_builder_value_from_string_type (builder, PANGO_TYPE_ATTR_TYPE, name, &val, error))
1321 type = g_value_get_enum (&val);
1322 g_value_unset (&val);
1326 /* PangoAttrLanguage */
1327 case PANGO_ATTR_LANGUAGE:
1328 if ((language = pango_language_from_string (value)))
1330 attribute = pango_attr_language_new (language);
1331 g_value_init (&val, G_TYPE_INT);
1335 case PANGO_ATTR_STYLE:
1336 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STYLE, value, &val, error))
1337 attribute = pango_attr_style_new (g_value_get_enum (&val));
1339 case PANGO_ATTR_WEIGHT:
1340 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_WEIGHT, value, &val, error))
1341 attribute = pango_attr_weight_new (g_value_get_enum (&val));
1343 case PANGO_ATTR_VARIANT:
1344 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_VARIANT, value, &val, error))
1345 attribute = pango_attr_variant_new (g_value_get_enum (&val));
1347 case PANGO_ATTR_STRETCH:
1348 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STRETCH, value, &val, error))
1349 attribute = pango_attr_stretch_new (g_value_get_enum (&val));
1351 case PANGO_ATTR_UNDERLINE:
1352 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_UNDERLINE, value, &val, NULL))
1353 attribute = pango_attr_underline_new (g_value_get_enum (&val));
1356 /* XXX: allow boolean for backwards compat, so ignore error */
1357 /* Deprecate this somehow */
1358 g_value_unset (&val);
1359 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1360 attribute = pango_attr_underline_new (g_value_get_boolean (&val));
1363 case PANGO_ATTR_STRIKETHROUGH:
1364 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1365 attribute = pango_attr_strikethrough_new (g_value_get_boolean (&val));
1367 case PANGO_ATTR_GRAVITY:
1368 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY, value, &val, error))
1369 attribute = pango_attr_gravity_new (g_value_get_enum (&val));
1371 case PANGO_ATTR_GRAVITY_HINT:
1372 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY_HINT,
1373 value, &val, error))
1374 attribute = pango_attr_gravity_hint_new (g_value_get_enum (&val));
1376 /* PangoAttrString */
1377 case PANGO_ATTR_FAMILY:
1378 attribute = pango_attr_family_new (value);
1379 g_value_init (&val, G_TYPE_INT);
1383 case PANGO_ATTR_SIZE:
1384 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1385 value, &val, error))
1386 attribute = pango_attr_size_new (g_value_get_int (&val));
1388 case PANGO_ATTR_ABSOLUTE_SIZE:
1389 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1390 value, &val, error))
1391 attribute = pango_attr_size_new_absolute (g_value_get_int (&val));
1394 /* PangoAttrFontDesc */
1395 case PANGO_ATTR_FONT_DESC:
1396 if ((font_desc = pango_font_description_from_string (value)))
1398 attribute = pango_attr_font_desc_new (font_desc);
1399 pango_font_description_free (font_desc);
1400 g_value_init (&val, G_TYPE_INT);
1404 /* PangoAttrColor */
1405 case PANGO_ATTR_FOREGROUND:
1406 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1407 value, &val, error))
1409 color = g_value_get_boxed (&val);
1410 attribute = pango_attr_foreground_new (color->red, color->green, color->blue);
1413 case PANGO_ATTR_BACKGROUND:
1414 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1415 value, &val, error))
1417 color = g_value_get_boxed (&val);
1418 attribute = pango_attr_background_new (color->red, color->green, color->blue);
1421 case PANGO_ATTR_UNDERLINE_COLOR:
1422 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1423 value, &val, error))
1425 color = g_value_get_boxed (&val);
1426 attribute = pango_attr_underline_color_new (color->red, color->green, color->blue);
1429 case PANGO_ATTR_STRIKETHROUGH_COLOR:
1430 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1431 value, &val, error))
1433 color = g_value_get_boxed (&val);
1434 attribute = pango_attr_strikethrough_color_new (color->red, color->green, color->blue);
1438 /* PangoAttrShape */
1439 case PANGO_ATTR_SHAPE:
1440 /* Unsupported for now */
1442 /* PangoAttrFloat */
1443 case PANGO_ATTR_SCALE:
1444 if (gtk_builder_value_from_string_type (builder, G_TYPE_DOUBLE,
1445 value, &val, error))
1446 attribute = pango_attr_scale_new (g_value_get_double (&val));
1449 case PANGO_ATTR_INVALID:
1450 case PANGO_ATTR_LETTER_SPACING:
1451 case PANGO_ATTR_RISE:
1452 case PANGO_ATTR_FALLBACK:
1457 g_value_unset (&val);
1464 pango_start_element (GMarkupParseContext *context,
1465 const gchar *element_name,
1466 const gchar **names,
1467 const gchar **values,
1471 PangoParserData *data = (PangoParserData*)user_data;
1472 GValue val = { 0, };
1474 gint line_number, char_number;
1476 if (strcmp (element_name, "attribute") == 0)
1478 PangoAttribute *attr = NULL;
1479 const gchar *name = NULL;
1480 const gchar *value = NULL;
1481 const gchar *start = NULL;
1482 const gchar *end = NULL;
1483 guint start_val = 0;
1484 guint end_val = G_MAXUINT;
1486 for (i = 0; names[i]; i++)
1488 if (strcmp (names[i], "name") == 0)
1490 else if (strcmp (names[i], "value") == 0)
1492 else if (strcmp (names[i], "start") == 0)
1494 else if (strcmp (names[i], "end") == 0)
1498 g_markup_parse_context_get_position (context,
1503 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1504 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1506 line_number, char_number, names[i], "attribute");
1511 if (!name || !value)
1513 g_markup_parse_context_get_position (context,
1518 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1519 "%s:%d:%d <%s> requires attribute \"%s\"",
1521 line_number, char_number, "attribute",
1522 name ? "value" : "name");
1528 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1529 start, &val, error))
1531 start_val = g_value_get_uint (&val);
1532 g_value_unset (&val);
1537 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1540 end_val = g_value_get_uint (&val);
1541 g_value_unset (&val);
1544 attr = attribute_from_text (data->builder, name, value, error);
1548 attr->start_index = start_val;
1549 attr->end_index = end_val;
1552 data->attrs = pango_attr_list_new ();
1554 pango_attr_list_insert (data->attrs, attr);
1557 else if (strcmp (element_name, "attributes") == 0)
1560 g_warning ("Unsupported tag for GtkLabel: %s\n", element_name);
1563 static const GMarkupParser pango_parser =
1565 pango_start_element,
1569 gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
1570 GtkBuilder *builder,
1572 const gchar *tagname,
1573 GMarkupParser *parser,
1576 if (buildable_parent_iface->custom_tag_start (buildable, builder, child,
1577 tagname, parser, data))
1580 if (strcmp (tagname, "attributes") == 0)
1582 PangoParserData *parser_data;
1584 parser_data = g_slice_new0 (PangoParserData);
1585 parser_data->builder = g_object_ref (builder);
1586 parser_data->object = g_object_ref (buildable);
1587 *parser = pango_parser;
1588 *data = parser_data;
1595 gtk_label_buildable_custom_finished (GtkBuildable *buildable,
1596 GtkBuilder *builder,
1598 const gchar *tagname,
1601 PangoParserData *data;
1603 buildable_parent_iface->custom_finished (buildable, builder, child,
1604 tagname, user_data);
1606 if (strcmp (tagname, "attributes") == 0)
1608 data = (PangoParserData*)user_data;
1612 gtk_label_set_attributes (GTK_LABEL (buildable), data->attrs);
1613 pango_attr_list_unref (data->attrs);
1616 g_object_unref (data->object);
1617 g_object_unref (data->builder);
1618 g_slice_free (PangoParserData, data);
1625 * @str: The text of the label
1627 * Creates a new label with the given text inside it. You can
1628 * pass %NULL to get an empty label widget.
1630 * Return value: the new #GtkLabel
1633 gtk_label_new (const gchar *str)
1637 label = g_object_new (GTK_TYPE_LABEL, NULL);
1640 gtk_label_set_text (label, str);
1642 return GTK_WIDGET (label);
1646 * gtk_label_new_with_mnemonic:
1647 * @str: The text of the label, with an underscore in front of the
1648 * mnemonic character
1650 * Creates a new #GtkLabel, containing the text in @str.
1652 * If characters in @str are preceded by an underscore, they are
1653 * underlined. If you need a literal underscore character in a label, use
1654 * '__' (two underscores). The first underlined character represents a
1655 * keyboard accelerator called a mnemonic. The mnemonic key can be used
1656 * to activate another widget, chosen automatically, or explicitly using
1657 * gtk_label_set_mnemonic_widget().
1659 * If gtk_label_set_mnemonic_widget() is not called, then the first
1660 * activatable ancestor of the #GtkLabel will be chosen as the mnemonic
1661 * widget. For instance, if the label is inside a button or menu item,
1662 * the button or menu item will automatically become the mnemonic widget
1663 * and be activated by the mnemonic.
1665 * Return value: the new #GtkLabel
1668 gtk_label_new_with_mnemonic (const gchar *str)
1672 label = g_object_new (GTK_TYPE_LABEL, NULL);
1675 gtk_label_set_text_with_mnemonic (label, str);
1677 return GTK_WIDGET (label);
1681 gtk_label_mnemonic_activate (GtkWidget *widget,
1682 gboolean group_cycling)
1684 GtkLabel *label = GTK_LABEL (widget);
1685 GtkLabelPrivate *priv = label->priv;
1688 if (priv->mnemonic_widget)
1689 return gtk_widget_mnemonic_activate (priv->mnemonic_widget, group_cycling);
1691 /* Try to find the widget to activate by traversing the
1692 * widget's ancestry.
1694 parent = gtk_widget_get_parent (widget);
1696 if (GTK_IS_NOTEBOOK (parent))
1701 if (gtk_widget_get_can_focus (parent) ||
1702 (!group_cycling && GTK_WIDGET_GET_CLASS (parent)->activate_signal) ||
1703 GTK_IS_NOTEBOOK (gtk_widget_get_parent (parent)) ||
1704 GTK_IS_MENU_ITEM (parent))
1705 return gtk_widget_mnemonic_activate (parent, group_cycling);
1706 parent = gtk_widget_get_parent (parent);
1709 /* barf if there was nothing to activate */
1710 g_warning ("Couldn't find a target for a mnemonic activation.");
1711 gtk_widget_error_bell (widget);
1717 gtk_label_setup_mnemonic (GtkLabel *label,
1720 GtkLabelPrivate *priv = label->priv;
1721 GtkWidget *widget = GTK_WIDGET (label);
1722 GtkWidget *toplevel;
1723 GtkWidget *mnemonic_menu;
1725 mnemonic_menu = g_object_get_data (G_OBJECT (label), "gtk-mnemonic-menu");
1727 if (last_key != GDK_KEY_VoidSymbol)
1729 if (priv->mnemonic_window)
1731 gtk_window_remove_mnemonic (priv->mnemonic_window,
1734 priv->mnemonic_window = NULL;
1738 _gtk_menu_shell_remove_mnemonic (GTK_MENU_SHELL (mnemonic_menu),
1741 mnemonic_menu = NULL;
1745 if (priv->mnemonic_keyval == GDK_KEY_VoidSymbol)
1748 connect_mnemonics_visible_notify (GTK_LABEL (widget));
1750 toplevel = gtk_widget_get_toplevel (widget);
1751 if (gtk_widget_is_toplevel (toplevel))
1753 GtkWidget *menu_shell;
1755 menu_shell = gtk_widget_get_ancestor (widget,
1756 GTK_TYPE_MENU_SHELL);
1760 _gtk_menu_shell_add_mnemonic (GTK_MENU_SHELL (menu_shell),
1761 priv->mnemonic_keyval,
1763 mnemonic_menu = menu_shell;
1766 if (!GTK_IS_MENU (menu_shell))
1768 gtk_window_add_mnemonic (GTK_WINDOW (toplevel),
1769 priv->mnemonic_keyval,
1771 priv->mnemonic_window = GTK_WINDOW (toplevel);
1776 g_object_set_data (G_OBJECT (label), I_("gtk-mnemonic-menu"), mnemonic_menu);
1780 gtk_label_hierarchy_changed (GtkWidget *widget,
1781 GtkWidget *old_toplevel)
1783 GtkLabel *label = GTK_LABEL (widget);
1784 GtkLabelPrivate *priv = label->priv;
1786 gtk_label_setup_mnemonic (label, priv->mnemonic_keyval);
1790 label_shortcut_setting_apply (GtkLabel *label)
1792 gtk_label_recalculate (label);
1793 if (GTK_IS_ACCEL_LABEL (label))
1794 gtk_accel_label_refetch (GTK_ACCEL_LABEL (label));
1798 label_shortcut_setting_traverse_container (GtkWidget *widget,
1801 if (GTK_IS_LABEL (widget))
1802 label_shortcut_setting_apply (GTK_LABEL (widget));
1803 else if (GTK_IS_CONTAINER (widget))
1804 gtk_container_forall (GTK_CONTAINER (widget),
1805 label_shortcut_setting_traverse_container, data);
1809 label_shortcut_setting_changed (GtkSettings *settings)
1813 list = gtk_window_list_toplevels ();
1815 for (l = list; l ; l = l->next)
1817 GtkWidget *widget = l->data;
1819 if (gtk_widget_get_settings (widget) == settings)
1820 gtk_container_forall (GTK_CONTAINER (widget),
1821 label_shortcut_setting_traverse_container, NULL);
1828 mnemonics_visible_apply (GtkWidget *widget,
1829 gboolean mnemonics_visible)
1831 GtkLabel *label = GTK_LABEL (widget);
1832 GtkLabelPrivate *priv = label->priv;
1834 mnemonics_visible = mnemonics_visible != FALSE;
1836 if (priv->mnemonics_visible != mnemonics_visible)
1838 priv->mnemonics_visible = mnemonics_visible;
1840 gtk_label_recalculate (label);
1845 label_mnemonics_visible_traverse_container (GtkWidget *widget,
1848 gboolean mnemonics_visible = GPOINTER_TO_INT (data);
1850 _gtk_label_mnemonics_visible_apply_recursively (widget, mnemonics_visible);
1854 _gtk_label_mnemonics_visible_apply_recursively (GtkWidget *widget,
1855 gboolean mnemonics_visible)
1857 if (GTK_IS_LABEL (widget))
1858 mnemonics_visible_apply (widget, mnemonics_visible);
1859 else if (GTK_IS_CONTAINER (widget))
1860 gtk_container_forall (GTK_CONTAINER (widget),
1861 label_mnemonics_visible_traverse_container,
1862 GINT_TO_POINTER (mnemonics_visible));
1866 label_mnemonics_visible_changed (GtkWindow *window,
1870 gboolean mnemonics_visible;
1872 g_object_get (window, "mnemonics-visible", &mnemonics_visible, NULL);
1874 gtk_container_forall (GTK_CONTAINER (window),
1875 label_mnemonics_visible_traverse_container,
1876 GINT_TO_POINTER (mnemonics_visible));
1880 gtk_label_screen_changed (GtkWidget *widget,
1881 GdkScreen *old_screen)
1883 GtkSettings *settings;
1884 gboolean shortcuts_connected;
1886 if (!gtk_widget_has_screen (widget))
1889 settings = gtk_widget_get_settings (widget);
1891 shortcuts_connected =
1892 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (settings),
1893 "gtk-label-shortcuts-connected"));
1895 if (! shortcuts_connected)
1897 g_signal_connect (settings, "notify::gtk-enable-mnemonics",
1898 G_CALLBACK (label_shortcut_setting_changed),
1900 g_signal_connect (settings, "notify::gtk-enable-accels",
1901 G_CALLBACK (label_shortcut_setting_changed),
1904 g_object_set_data (G_OBJECT (settings), "gtk-label-shortcuts-connected",
1905 GINT_TO_POINTER (TRUE));
1908 label_shortcut_setting_apply (GTK_LABEL (widget));
1913 label_mnemonic_widget_weak_notify (gpointer data,
1914 GObject *where_the_object_was)
1916 GtkLabel *label = data;
1917 GtkLabelPrivate *priv = label->priv;
1919 priv->mnemonic_widget = NULL;
1920 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1924 * gtk_label_set_mnemonic_widget:
1925 * @label: a #GtkLabel
1926 * @widget: (allow-none): the target #GtkWidget
1928 * If the label has been set so that it has an mnemonic key (using
1929 * i.e. gtk_label_set_markup_with_mnemonic(),
1930 * gtk_label_set_text_with_mnemonic(), gtk_label_new_with_mnemonic()
1931 * or the "use_underline" property) the label can be associated with a
1932 * widget that is the target of the mnemonic. When the label is inside
1933 * a widget (like a #GtkButton or a #GtkNotebook tab) it is
1934 * automatically associated with the correct widget, but sometimes
1935 * (i.e. when the target is a #GtkEntry next to the label) you need to
1936 * set it explicitly using this function.
1938 * The target widget will be accelerated by emitting the
1939 * GtkWidget::mnemonic-activate signal on it. The default handler for
1940 * this signal will activate the widget if there are no mnemonic collisions
1941 * and toggle focus between the colliding widgets otherwise.
1944 gtk_label_set_mnemonic_widget (GtkLabel *label,
1947 GtkLabelPrivate *priv;
1949 g_return_if_fail (GTK_IS_LABEL (label));
1954 g_return_if_fail (GTK_IS_WIDGET (widget));
1956 if (priv->mnemonic_widget)
1958 gtk_widget_remove_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1959 g_object_weak_unref (G_OBJECT (priv->mnemonic_widget),
1960 label_mnemonic_widget_weak_notify,
1963 priv->mnemonic_widget = widget;
1964 if (priv->mnemonic_widget)
1966 g_object_weak_ref (G_OBJECT (priv->mnemonic_widget),
1967 label_mnemonic_widget_weak_notify,
1969 gtk_widget_add_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1972 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1976 * gtk_label_get_mnemonic_widget:
1977 * @label: a #GtkLabel
1979 * Retrieves the target of the mnemonic (keyboard shortcut) of this
1980 * label. See gtk_label_set_mnemonic_widget().
1982 * Return value: (transfer none): the target of the label's mnemonic,
1983 * or %NULL if none has been set and the default algorithm will be used.
1986 gtk_label_get_mnemonic_widget (GtkLabel *label)
1988 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1990 return label->priv->mnemonic_widget;
1994 * gtk_label_get_mnemonic_keyval:
1995 * @label: a #GtkLabel
1997 * If the label has been set so that it has an mnemonic key this function
1998 * returns the keyval used for the mnemonic accelerator. If there is no
1999 * mnemonic set up it returns #GDK_VoidSymbol.
2001 * Returns: GDK keyval usable for accelerators, or #GDK_VoidSymbol
2004 gtk_label_get_mnemonic_keyval (GtkLabel *label)
2006 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_KEY_VoidSymbol);
2008 return label->priv->mnemonic_keyval;
2012 gtk_label_set_text_internal (GtkLabel *label,
2015 GtkLabelPrivate *priv = label->priv;
2017 g_free (priv->text);
2021 gtk_label_select_region_index (label, 0, 0);
2025 gtk_label_set_label_internal (GtkLabel *label,
2028 GtkLabelPrivate *priv = label->priv;
2030 g_free (priv->label);
2034 g_object_notify (G_OBJECT (label), "label");
2038 gtk_label_set_use_markup_internal (GtkLabel *label,
2041 GtkLabelPrivate *priv = label->priv;
2044 if (priv->use_markup != val)
2046 priv->use_markup = val;
2048 g_object_notify (G_OBJECT (label), "use-markup");
2053 gtk_label_set_use_underline_internal (GtkLabel *label,
2056 GtkLabelPrivate *priv = label->priv;
2059 if (priv->use_underline != val)
2061 priv->use_underline = val;
2063 g_object_notify (G_OBJECT (label), "use-underline");
2068 gtk_label_compose_effective_attrs (GtkLabel *label)
2070 GtkLabelPrivate *priv = label->priv;
2071 PangoAttrIterator *iter;
2072 PangoAttribute *attr;
2073 GSList *iter_attrs, *l;
2077 if (priv->effective_attrs)
2079 if ((iter = pango_attr_list_get_iterator (priv->attrs)))
2083 iter_attrs = pango_attr_iterator_get_attrs (iter);
2084 for (l = iter_attrs; l; l = l->next)
2087 pango_attr_list_insert (priv->effective_attrs, attr);
2089 g_slist_free (iter_attrs);
2091 while (pango_attr_iterator_next (iter));
2092 pango_attr_iterator_destroy (iter);
2096 priv->effective_attrs =
2097 pango_attr_list_ref (priv->attrs);
2102 gtk_label_set_attributes_internal (GtkLabel *label,
2103 PangoAttrList *attrs)
2105 GtkLabelPrivate *priv = label->priv;
2108 pango_attr_list_ref (attrs);
2111 pango_attr_list_unref (priv->attrs);
2112 priv->attrs = attrs;
2114 g_object_notify (G_OBJECT (label), "attributes");
2118 /* Calculates text, attrs and mnemonic_keyval from
2119 * label, use_underline and use_markup
2122 gtk_label_recalculate (GtkLabel *label)
2124 GtkLabelPrivate *priv = label->priv;
2125 guint keyval = priv->mnemonic_keyval;
2127 if (priv->use_markup)
2128 gtk_label_set_markup_internal (label, priv->label, priv->use_underline);
2131 if (priv->use_underline)
2132 gtk_label_set_uline_text_internal (label, priv->label);
2135 if (priv->effective_attrs)
2136 pango_attr_list_unref (priv->effective_attrs);
2137 priv->effective_attrs = NULL;
2138 gtk_label_set_text_internal (label, g_strdup (priv->label));
2142 gtk_label_compose_effective_attrs (label);
2144 if (!priv->use_underline)
2145 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
2147 if (keyval != priv->mnemonic_keyval)
2149 gtk_label_setup_mnemonic (label, keyval);
2150 g_object_notify (G_OBJECT (label), "mnemonic-keyval");
2153 gtk_label_clear_layout (label);
2154 gtk_label_clear_select_info (label);
2155 gtk_widget_queue_resize (GTK_WIDGET (label));
2159 * gtk_label_set_text:
2160 * @label: a #GtkLabel
2161 * @str: The text you want to set
2163 * Sets the text within the #GtkLabel widget. It overwrites any text that
2166 * This will also clear any previously set mnemonic accelerators.
2169 gtk_label_set_text (GtkLabel *label,
2172 g_return_if_fail (GTK_IS_LABEL (label));
2174 g_object_freeze_notify (G_OBJECT (label));
2176 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2177 gtk_label_set_use_markup_internal (label, FALSE);
2178 gtk_label_set_use_underline_internal (label, FALSE);
2180 gtk_label_recalculate (label);
2182 g_object_thaw_notify (G_OBJECT (label));
2186 * gtk_label_set_attributes:
2187 * @label: a #GtkLabel
2188 * @attrs: a #PangoAttrList
2190 * Sets a #PangoAttrList; the attributes in the list are applied to the
2193 * <note><para>The attributes set with this function will be applied
2194 * and merged with any other attributes previously effected by way
2195 * of the #GtkLabel:use-underline or #GtkLabel:use-markup properties.
2196 * While it is not recommended to mix markup strings with manually set
2197 * attributes, if you must; know that the attributes will be applied
2198 * to the label after the markup string is parsed.</para></note>
2201 gtk_label_set_attributes (GtkLabel *label,
2202 PangoAttrList *attrs)
2204 g_return_if_fail (GTK_IS_LABEL (label));
2206 gtk_label_set_attributes_internal (label, attrs);
2208 gtk_label_recalculate (label);
2210 gtk_label_clear_layout (label);
2211 gtk_widget_queue_resize (GTK_WIDGET (label));
2215 * gtk_label_get_attributes:
2216 * @label: a #GtkLabel
2218 * Gets the attribute list that was set on the label using
2219 * gtk_label_set_attributes(), if any. This function does
2220 * not reflect attributes that come from the labels markup
2221 * (see gtk_label_set_markup()). If you want to get the
2222 * effective attributes for the label, use
2223 * pango_layout_get_attribute (gtk_label_get_layout (label)).
2225 * Return value: (transfer none): the attribute list, or %NULL
2229 gtk_label_get_attributes (GtkLabel *label)
2231 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2233 return label->priv->attrs;
2237 * gtk_label_set_label:
2238 * @label: a #GtkLabel
2239 * @str: the new text to set for the label
2241 * Sets the text of the label. The label is interpreted as
2242 * including embedded underlines and/or Pango markup depending
2243 * on the values of the #GtkLabel:use-underline" and
2244 * #GtkLabel:use-markup properties.
2247 gtk_label_set_label (GtkLabel *label,
2250 g_return_if_fail (GTK_IS_LABEL (label));
2252 g_object_freeze_notify (G_OBJECT (label));
2254 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2255 gtk_label_recalculate (label);
2257 g_object_thaw_notify (G_OBJECT (label));
2261 * gtk_label_get_label:
2262 * @label: a #GtkLabel
2264 * Fetches the text from a label widget including any embedded
2265 * underlines indicating mnemonics and Pango markup. (See
2266 * gtk_label_get_text()).
2268 * Return value: the text of the label widget. This string is
2269 * owned by the widget and must not be modified or freed.
2272 gtk_label_get_label (GtkLabel *label)
2274 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2276 return label->priv->label;
2284 GdkColor *link_color;
2285 GdkColor *visited_link_color;
2289 start_element_handler (GMarkupParseContext *context,
2290 const gchar *element_name,
2291 const gchar **attribute_names,
2292 const gchar **attribute_values,
2296 GtkLabelPrivate *priv;
2297 UriParserData *pdata = user_data;
2299 if (strcmp (element_name, "a") == 0)
2302 const gchar *uri = NULL;
2303 const gchar *title = NULL;
2304 gboolean visited = FALSE;
2308 GdkColor *color = NULL;
2310 g_markup_parse_context_get_position (context, &line_number, &char_number);
2312 for (i = 0; attribute_names[i] != NULL; i++)
2314 const gchar *attr = attribute_names[i];
2316 if (strcmp (attr, "href") == 0)
2317 uri = attribute_values[i];
2318 else if (strcmp (attr, "title") == 0)
2319 title = attribute_values[i];
2324 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
2325 "Attribute '%s' is not allowed on the <a> tag "
2326 "on line %d char %d",
2327 attr, line_number, char_number);
2336 G_MARKUP_ERROR_INVALID_CONTENT,
2337 "Attribute 'href' was missing on the <a> tag "
2338 "on line %d char %d",
2339 line_number, char_number);
2344 priv = pdata->label->priv;
2345 if (priv->track_links && priv->select_info)
2348 for (l = priv->select_info->links; l; l = l->next)
2351 if (strcmp (uri, link->uri) == 0)
2353 visited = link->visited;
2360 color = pdata->visited_link_color;
2362 color = pdata->link_color;
2364 g_string_append_printf (pdata->new_str,
2365 "<span color=\"#%04x%04x%04x\" underline=\"single\">",
2370 link = g_new0 (GtkLabelLink, 1);
2371 link->uri = g_strdup (uri);
2372 link->title = g_strdup (title);
2373 link->visited = visited;
2374 pdata->links = g_list_append (pdata->links, link);
2380 g_string_append_c (pdata->new_str, '<');
2381 g_string_append (pdata->new_str, element_name);
2383 for (i = 0; attribute_names[i] != NULL; i++)
2385 const gchar *attr = attribute_names[i];
2386 const gchar *value = attribute_values[i];
2389 newvalue = g_markup_escape_text (value, -1);
2391 g_string_append_c (pdata->new_str, ' ');
2392 g_string_append (pdata->new_str, attr);
2393 g_string_append (pdata->new_str, "=\"");
2394 g_string_append (pdata->new_str, newvalue);
2395 g_string_append_c (pdata->new_str, '\"');
2399 g_string_append_c (pdata->new_str, '>');
2404 end_element_handler (GMarkupParseContext *context,
2405 const gchar *element_name,
2409 UriParserData *pdata = user_data;
2411 if (!strcmp (element_name, "a"))
2412 g_string_append (pdata->new_str, "</span>");
2415 g_string_append (pdata->new_str, "</");
2416 g_string_append (pdata->new_str, element_name);
2417 g_string_append_c (pdata->new_str, '>');
2422 text_handler (GMarkupParseContext *context,
2428 UriParserData *pdata = user_data;
2431 newtext = g_markup_escape_text (text, text_len);
2432 g_string_append (pdata->new_str, newtext);
2436 static const GMarkupParser markup_parser =
2438 start_element_handler,
2439 end_element_handler,
2446 xml_isspace (gchar c)
2448 return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
2452 link_free (GtkLabelLink *link)
2455 g_free (link->title);
2460 gtk_label_get_link_colors (GtkWidget *widget,
2461 GdkColor **link_color,
2462 GdkColor **visited_link_color)
2464 GtkStyleContext *context;
2466 context = gtk_widget_get_style_context (widget);
2467 gtk_style_context_get_style (context,
2468 "link-color", link_color,
2469 "visited-link-color", visited_link_color,
2472 *link_color = gdk_color_copy (&default_link_color);
2473 if (!*visited_link_color)
2474 *visited_link_color = gdk_color_copy (&default_visited_link_color);
2478 parse_uri_markup (GtkLabel *label,
2484 GMarkupParseContext *context = NULL;
2485 const gchar *p, *end;
2486 gboolean needs_root = TRUE;
2488 UriParserData pdata;
2490 length = strlen (str);
2494 pdata.label = label;
2496 pdata.new_str = g_string_sized_new (length);
2498 gtk_label_get_link_colors (GTK_WIDGET (label), &pdata.link_color, &pdata.visited_link_color);
2500 while (p != end && xml_isspace (*p))
2503 if (end - p >= 8 && strncmp (p, "<markup>", 8) == 0)
2506 context = g_markup_parse_context_new (&markup_parser, 0, &pdata, NULL);
2510 if (!g_markup_parse_context_parse (context, "<markup>", -1, error))
2514 if (!g_markup_parse_context_parse (context, str, length, error))
2519 if (!g_markup_parse_context_parse (context, "</markup>", -1, error))
2523 if (!g_markup_parse_context_end_parse (context, error))
2526 g_markup_parse_context_free (context);
2528 *new_str = g_string_free (pdata.new_str, FALSE);
2529 *links = pdata.links;
2531 gdk_color_free (pdata.link_color);
2532 gdk_color_free (pdata.visited_link_color);
2537 g_markup_parse_context_free (context);
2538 g_string_free (pdata.new_str, TRUE);
2539 g_list_foreach (pdata.links, (GFunc)link_free, NULL);
2540 g_list_free (pdata.links);
2541 gdk_color_free (pdata.link_color);
2542 gdk_color_free (pdata.visited_link_color);
2548 gtk_label_ensure_has_tooltip (GtkLabel *label)
2550 GtkLabelPrivate *priv = label->priv;
2552 gboolean has_tooltip = FALSE;
2554 for (l = priv->select_info->links; l; l = l->next)
2556 GtkLabelLink *link = l->data;
2564 gtk_widget_set_has_tooltip (GTK_WIDGET (label), has_tooltip);
2568 gtk_label_set_markup_internal (GtkLabel *label,
2570 gboolean with_uline)
2572 GtkLabelPrivate *priv = label->priv;
2574 GError *error = NULL;
2575 PangoAttrList *attrs = NULL;
2576 gunichar accel_char = 0;
2578 GList *links = NULL;
2580 if (!parse_uri_markup (label, str, &new_str, &links, &error))
2582 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2584 g_error_free (error);
2588 gtk_label_clear_links (label);
2591 gtk_label_ensure_select_info (label);
2592 priv->select_info->links = links;
2593 gtk_label_ensure_has_tooltip (label);
2598 gboolean enable_mnemonics;
2599 gboolean auto_mnemonics;
2601 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2602 "gtk-enable-mnemonics", &enable_mnemonics,
2603 "gtk-auto-mnemonics", &auto_mnemonics,
2606 if (!(enable_mnemonics && priv->mnemonics_visible &&
2608 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2609 (!priv->mnemonic_widget ||
2610 gtk_widget_is_sensitive (priv->mnemonic_widget))))))
2616 if (separate_uline_pattern (new_str, &key, &tmp, &pattern))
2625 if (!pango_parse_markup (new_str,
2627 with_uline ? '_' : 0,
2630 with_uline ? &accel_char : NULL,
2633 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2636 g_error_free (error);
2643 gtk_label_set_text_internal (label, text);
2647 if (priv->effective_attrs)
2648 pango_attr_list_unref (priv->effective_attrs);
2649 priv->effective_attrs = attrs;
2652 if (accel_char != 0)
2653 priv->mnemonic_keyval = gdk_keyval_to_lower (gdk_unicode_to_keyval (accel_char));
2655 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
2659 * gtk_label_set_markup:
2660 * @label: a #GtkLabel
2661 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
2663 * Parses @str which is marked up with the <link
2664 * linkend="PangoMarkupFormat">Pango text markup language</link>, setting the
2665 * label's text and attribute list based on the parse results. If the @str is
2666 * external data, you may need to escape it with g_markup_escape_text() or
2667 * g_markup_printf_escaped()<!-- -->:
2671 * markup = g_markup_printf_escaped ("<span style=\"italic\">%s</span>", str);
2672 * gtk_label_set_markup (GTK_LABEL (label), markup);
2677 gtk_label_set_markup (GtkLabel *label,
2680 g_return_if_fail (GTK_IS_LABEL (label));
2682 g_object_freeze_notify (G_OBJECT (label));
2684 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2685 gtk_label_set_use_markup_internal (label, TRUE);
2686 gtk_label_set_use_underline_internal (label, FALSE);
2688 gtk_label_recalculate (label);
2690 g_object_thaw_notify (G_OBJECT (label));
2694 * gtk_label_set_markup_with_mnemonic:
2695 * @label: a #GtkLabel
2696 * @str: a markup string (see
2697 * <link linkend="PangoMarkupFormat">Pango markup format</link>)
2699 * Parses @str which is marked up with the
2700 * <link linkend="PangoMarkupFormat">Pango text markup language</link>,
2701 * setting the label's text and attribute list based on the parse results.
2702 * If characters in @str are preceded by an underscore, they are underlined
2703 * indicating that they represent a keyboard accelerator called a mnemonic.
2705 * The mnemonic key can be used to activate another widget, chosen
2706 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
2709 gtk_label_set_markup_with_mnemonic (GtkLabel *label,
2712 g_return_if_fail (GTK_IS_LABEL (label));
2714 g_object_freeze_notify (G_OBJECT (label));
2716 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2717 gtk_label_set_use_markup_internal (label, TRUE);
2718 gtk_label_set_use_underline_internal (label, TRUE);
2720 gtk_label_recalculate (label);
2722 g_object_thaw_notify (G_OBJECT (label));
2726 * gtk_label_get_text:
2727 * @label: a #GtkLabel
2729 * Fetches the text from a label widget, as displayed on the
2730 * screen. This does not include any embedded underlines
2731 * indicating mnemonics or Pango markup. (See gtk_label_get_label())
2733 * Return value: the text in the label widget. This is the internal
2734 * string used by the label, and must not be modified.
2737 gtk_label_get_text (GtkLabel *label)
2739 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2741 return label->priv->text;
2744 static PangoAttrList *
2745 gtk_label_pattern_to_attrs (GtkLabel *label,
2746 const gchar *pattern)
2748 GtkLabelPrivate *priv = label->priv;
2750 const char *p = priv->text;
2751 const char *q = pattern;
2752 PangoAttrList *attrs;
2754 attrs = pango_attr_list_new ();
2758 while (*p && *q && *q != '_')
2760 p = g_utf8_next_char (p);
2764 while (*p && *q && *q == '_')
2766 p = g_utf8_next_char (p);
2772 PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_LOW);
2773 attr->start_index = start - priv->text;
2774 attr->end_index = p - priv->text;
2776 pango_attr_list_insert (attrs, attr);
2786 gtk_label_set_pattern_internal (GtkLabel *label,
2787 const gchar *pattern,
2788 gboolean is_mnemonic)
2790 GtkLabelPrivate *priv = label->priv;
2791 PangoAttrList *attrs;
2792 gboolean enable_mnemonics;
2793 gboolean auto_mnemonics;
2795 if (priv->pattern_set)
2800 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2801 "gtk-enable-mnemonics", &enable_mnemonics,
2802 "gtk-auto-mnemonics", &auto_mnemonics,
2805 if (enable_mnemonics && priv->mnemonics_visible && pattern &&
2807 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2808 (!priv->mnemonic_widget ||
2809 gtk_widget_is_sensitive (priv->mnemonic_widget)))))
2810 attrs = gtk_label_pattern_to_attrs (label, pattern);
2815 attrs = gtk_label_pattern_to_attrs (label, pattern);
2817 if (priv->effective_attrs)
2818 pango_attr_list_unref (priv->effective_attrs);
2819 priv->effective_attrs = attrs;
2823 * gtk_label_set_pattern:
2824 * @label: The #GtkLabel you want to set the pattern to.
2825 * @pattern: The pattern as described above.
2827 * The pattern of underlines you want under the existing text within the
2828 * #GtkLabel widget. For example if the current text of the label says
2829 * "FooBarBaz" passing a pattern of "___ ___" will underline
2830 * "Foo" and "Baz" but not "Bar".
2833 gtk_label_set_pattern (GtkLabel *label,
2834 const gchar *pattern)
2836 GtkLabelPrivate *priv = label->priv;
2838 g_return_if_fail (GTK_IS_LABEL (label));
2842 priv->pattern_set = FALSE;
2846 gtk_label_set_pattern_internal (label, pattern, FALSE);
2847 priv->pattern_set = TRUE;
2850 gtk_label_recalculate (label);
2852 gtk_label_clear_layout (label);
2853 gtk_widget_queue_resize (GTK_WIDGET (label));
2858 * gtk_label_set_justify:
2859 * @label: a #GtkLabel
2860 * @jtype: a #GtkJustification
2862 * Sets the alignment of the lines in the text of the label relative to
2863 * each other. %GTK_JUSTIFY_LEFT is the default value when the
2864 * widget is first created with gtk_label_new(). If you instead want
2865 * to set the alignment of the label as a whole, use
2866 * gtk_misc_set_alignment() instead. gtk_label_set_justify() has no
2867 * effect on labels containing only a single line.
2870 gtk_label_set_justify (GtkLabel *label,
2871 GtkJustification jtype)
2873 GtkLabelPrivate *priv;
2875 g_return_if_fail (GTK_IS_LABEL (label));
2876 g_return_if_fail (jtype >= GTK_JUSTIFY_LEFT && jtype <= GTK_JUSTIFY_FILL);
2880 if ((GtkJustification) priv->jtype != jtype)
2882 priv->jtype = jtype;
2884 /* No real need to be this drastic, but easier than duplicating the code */
2885 gtk_label_clear_layout (label);
2887 g_object_notify (G_OBJECT (label), "justify");
2888 gtk_widget_queue_resize (GTK_WIDGET (label));
2893 * gtk_label_get_justify:
2894 * @label: a #GtkLabel
2896 * Returns the justification of the label. See gtk_label_set_justify().
2898 * Return value: #GtkJustification
2901 gtk_label_get_justify (GtkLabel *label)
2903 g_return_val_if_fail (GTK_IS_LABEL (label), 0);
2905 return label->priv->jtype;
2909 * gtk_label_set_ellipsize:
2910 * @label: a #GtkLabel
2911 * @mode: a #PangoEllipsizeMode
2913 * Sets the mode used to ellipsize (add an ellipsis: "...") to the text
2914 * if there is not enough space to render the entire string.
2919 gtk_label_set_ellipsize (GtkLabel *label,
2920 PangoEllipsizeMode mode)
2922 GtkLabelPrivate *priv;
2924 g_return_if_fail (GTK_IS_LABEL (label));
2925 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE && mode <= PANGO_ELLIPSIZE_END);
2929 if ((PangoEllipsizeMode) priv->ellipsize != mode)
2931 priv->ellipsize = mode;
2933 /* No real need to be this drastic, but easier than duplicating the code */
2934 gtk_label_clear_layout (label);
2936 g_object_notify (G_OBJECT (label), "ellipsize");
2937 gtk_widget_queue_resize (GTK_WIDGET (label));
2942 * gtk_label_get_ellipsize:
2943 * @label: a #GtkLabel
2945 * Returns the ellipsizing position of the label. See gtk_label_set_ellipsize().
2947 * Return value: #PangoEllipsizeMode
2952 gtk_label_get_ellipsize (GtkLabel *label)
2954 g_return_val_if_fail (GTK_IS_LABEL (label), PANGO_ELLIPSIZE_NONE);
2956 return label->priv->ellipsize;
2960 * gtk_label_set_width_chars:
2961 * @label: a #GtkLabel
2962 * @n_chars: the new desired width, in characters.
2964 * Sets the desired width in characters of @label to @n_chars.
2969 gtk_label_set_width_chars (GtkLabel *label,
2972 GtkLabelPrivate *priv;
2974 g_return_if_fail (GTK_IS_LABEL (label));
2978 if (priv->width_chars != n_chars)
2980 priv->width_chars = n_chars;
2981 g_object_notify (G_OBJECT (label), "width-chars");
2982 gtk_widget_queue_resize (GTK_WIDGET (label));
2987 * gtk_label_get_width_chars:
2988 * @label: a #GtkLabel
2990 * Retrieves the desired width of @label, in characters. See
2991 * gtk_label_set_width_chars().
2993 * Return value: the width of the label in characters.
2998 gtk_label_get_width_chars (GtkLabel *label)
3000 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
3002 return label->priv->width_chars;
3006 * gtk_label_set_max_width_chars:
3007 * @label: a #GtkLabel
3008 * @n_chars: the new desired maximum width, in characters.
3010 * Sets the desired maximum width in characters of @label to @n_chars.
3015 gtk_label_set_max_width_chars (GtkLabel *label,
3018 GtkLabelPrivate *priv;
3020 g_return_if_fail (GTK_IS_LABEL (label));
3024 if (priv->max_width_chars != n_chars)
3026 priv->max_width_chars = n_chars;
3028 g_object_notify (G_OBJECT (label), "max-width-chars");
3029 gtk_widget_queue_resize (GTK_WIDGET (label));
3034 * gtk_label_get_max_width_chars:
3035 * @label: a #GtkLabel
3037 * Retrieves the desired maximum width of @label, in characters. See
3038 * gtk_label_set_width_chars().
3040 * Return value: the maximum width of the label in characters.
3045 gtk_label_get_max_width_chars (GtkLabel *label)
3047 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
3049 return label->priv->max_width_chars;
3053 * gtk_label_set_line_wrap:
3054 * @label: a #GtkLabel
3055 * @wrap: the setting
3057 * Toggles line wrapping within the #GtkLabel widget. %TRUE makes it break
3058 * lines if text exceeds the widget's size. %FALSE lets the text get cut off
3059 * by the edge of the widget if it exceeds the widget size.
3061 * Note that setting line wrapping to %TRUE does not make the label
3062 * wrap at its parent container's width, because GTK+ widgets
3063 * conceptually can't make their requisition depend on the parent
3064 * container's size. For a label that wraps at a specific position,
3065 * set the label's width using gtk_widget_set_size_request().
3068 gtk_label_set_line_wrap (GtkLabel *label,
3071 GtkLabelPrivate *priv;
3073 g_return_if_fail (GTK_IS_LABEL (label));
3077 wrap = wrap != FALSE;
3079 if (priv->wrap != wrap)
3083 gtk_label_clear_layout (label);
3084 gtk_widget_queue_resize (GTK_WIDGET (label));
3085 g_object_notify (G_OBJECT (label), "wrap");
3090 * gtk_label_get_line_wrap:
3091 * @label: a #GtkLabel
3093 * Returns whether lines in the label are automatically wrapped.
3094 * See gtk_label_set_line_wrap().
3096 * Return value: %TRUE if the lines of the label are automatically wrapped.
3099 gtk_label_get_line_wrap (GtkLabel *label)
3101 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
3103 return label->priv->wrap;
3107 * gtk_label_set_line_wrap_mode:
3108 * @label: a #GtkLabel
3109 * @wrap_mode: the line wrapping mode
3111 * If line wrapping is on (see gtk_label_set_line_wrap()) this controls how
3112 * the line wrapping is done. The default is %PANGO_WRAP_WORD which means
3113 * wrap on word boundaries.
3118 gtk_label_set_line_wrap_mode (GtkLabel *label,
3119 PangoWrapMode wrap_mode)
3121 GtkLabelPrivate *priv;
3123 g_return_if_fail (GTK_IS_LABEL (label));
3127 if (priv->wrap_mode != wrap_mode)
3129 priv->wrap_mode = wrap_mode;
3130 g_object_notify (G_OBJECT (label), "wrap-mode");
3132 gtk_widget_queue_resize (GTK_WIDGET (label));
3137 * gtk_label_get_line_wrap_mode:
3138 * @label: a #GtkLabel
3140 * Returns line wrap mode used by the label. See gtk_label_set_line_wrap_mode().
3142 * Return value: %TRUE if the lines of the label are automatically wrapped.
3147 gtk_label_get_line_wrap_mode (GtkLabel *label)
3149 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
3151 return label->priv->wrap_mode;
3155 gtk_label_destroy (GtkWidget *widget)
3157 GtkLabel *label = GTK_LABEL (widget);
3159 gtk_label_set_mnemonic_widget (label, NULL);
3161 GTK_WIDGET_CLASS (gtk_label_parent_class)->destroy (widget);
3165 gtk_label_finalize (GObject *object)
3167 GtkLabel *label = GTK_LABEL (object);
3168 GtkLabelPrivate *priv = label->priv;
3170 g_free (priv->label);
3171 g_free (priv->text);
3174 g_object_unref (priv->layout);
3177 pango_attr_list_unref (priv->attrs);
3179 if (priv->effective_attrs)
3180 pango_attr_list_unref (priv->effective_attrs);
3182 gtk_label_clear_links (label);
3183 g_free (priv->select_info);
3185 G_OBJECT_CLASS (gtk_label_parent_class)->finalize (object);
3189 gtk_label_clear_layout (GtkLabel *label)
3191 GtkLabelPrivate *priv = label->priv;
3195 g_object_unref (priv->layout);
3196 priv->layout = NULL;
3198 //gtk_label_clear_links (label);
3202 static PangoFontMetrics *
3203 get_font_metrics (PangoContext *context, GtkWidget *widget)
3205 GtkStyleContext *style_context;
3206 const PangoFontDescription *font;
3207 PangoFontMetrics *retval;
3209 style_context = gtk_widget_get_style_context (widget);
3210 font = gtk_style_context_get_font (style_context, GTK_STATE_FLAG_NORMAL);
3212 retval = pango_context_get_metrics (context,
3214 pango_context_get_language (context));
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) - logical.y;
3971 y = floor (allocation.y + ypad + MAX ((allocation.height - req_height) * yalign, 0)) - logical.y;
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_color (context, state, &fg_color);
4196 gtk_style_context_get_background_color (context, state, &bg_color);
4198 gdk_cairo_set_source_rgba (cr, &bg_color);
4201 gdk_cairo_set_source_rgba (cr, &fg_color);
4202 cairo_move_to (cr, x, y);
4203 _gtk_pango_fill_layout (cr, priv->layout);
4206 cairo_region_destroy (clip);
4210 GtkLabelLink *focus_link;
4211 GtkLabelLink *active_link;
4213 cairo_region_t *clip;
4215 GdkColor *text_color;
4216 GdkColor *link_color;
4217 GdkColor *visited_link_color;
4219 if (info->selectable && gtk_widget_has_focus (widget))
4220 gtk_label_draw_cursor (label, cr, x, y);
4222 focus_link = gtk_label_get_focus_link (label);
4223 active_link = info->active_link;
4230 range[0] = active_link->start;
4231 range[1] = active_link->end;
4235 clip = gdk_pango_layout_get_clip_region (priv->layout,
4239 gdk_cairo_region (cr, clip);
4241 cairo_region_destroy (clip);
4243 gtk_label_get_link_colors (widget, &link_color, &visited_link_color);
4244 if (active_link->visited)
4245 text_color = visited_link_color;
4247 text_color = link_color;
4249 if (info->link_clicked)
4250 state = GTK_STATE_FLAG_ACTIVE;
4252 state = GTK_STATE_FLAG_PRELIGHT;
4254 gtk_style_context_get_background_color (context, state, &bg_color);
4256 gdk_cairo_set_source_rgba (cr, &bg_color);
4259 gdk_cairo_set_source_color (cr, text_color);
4260 cairo_move_to (cr, x, y);
4261 _gtk_pango_fill_layout (cr, priv->layout);
4263 gdk_color_free (link_color);
4264 gdk_color_free (visited_link_color);
4269 if (focus_link && gtk_widget_has_focus (widget))
4271 range[0] = focus_link->start;
4272 range[1] = focus_link->end;
4274 clip = gdk_pango_layout_get_clip_region (priv->layout,
4278 cairo_region_get_extents (clip, &rect);
4280 state = gtk_widget_get_state_flags (widget);
4281 gtk_style_context_set_state (context, state);
4283 gtk_render_focus (context, cr,
4285 rect.width, rect.height);
4287 cairo_region_destroy (clip);
4296 separate_uline_pattern (const gchar *str,
4301 gboolean underscore;
4304 gchar *pattern_dest;
4306 *accel_key = GDK_KEY_VoidSymbol;
4307 *new_str = g_new (gchar, strlen (str) + 1);
4308 *pattern = g_new (gchar, g_utf8_strlen (str, -1) + 1);
4314 pattern_dest = *pattern;
4319 const gchar *next_src;
4321 c = g_utf8_get_char (src);
4322 if (c == (gunichar)-1)
4324 g_warning ("Invalid input string");
4330 next_src = g_utf8_next_char (src);
4335 *pattern_dest++ = ' ';
4338 *pattern_dest++ = '_';
4339 if (*accel_key == GDK_KEY_VoidSymbol)
4340 *accel_key = gdk_keyval_to_lower (gdk_unicode_to_keyval (c));
4343 while (src < next_src)
4357 while (src < next_src)
4360 *pattern_dest++ = ' ';
4372 gtk_label_set_uline_text_internal (GtkLabel *label,
4375 GtkLabelPrivate *priv = label->priv;
4376 guint accel_key = GDK_KEY_VoidSymbol;
4380 g_return_if_fail (GTK_IS_LABEL (label));
4381 g_return_if_fail (str != NULL);
4383 /* Split text into the base text and a separate pattern
4386 if (!separate_uline_pattern (str, &accel_key, &new_str, &pattern))
4389 gtk_label_set_text_internal (label, new_str);
4390 gtk_label_set_pattern_internal (label, pattern, TRUE);
4391 priv->mnemonic_keyval = accel_key;
4397 * gtk_label_set_text_with_mnemonic:
4398 * @label: a #GtkLabel
4401 * Sets the label's text from the string @str.
4402 * If characters in @str are preceded by an underscore, they are underlined
4403 * indicating that they represent a keyboard accelerator called a mnemonic.
4404 * The mnemonic key can be used to activate another widget, chosen
4405 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
4408 gtk_label_set_text_with_mnemonic (GtkLabel *label,
4411 g_return_if_fail (GTK_IS_LABEL (label));
4412 g_return_if_fail (str != NULL);
4414 g_object_freeze_notify (G_OBJECT (label));
4416 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
4417 gtk_label_set_use_markup_internal (label, FALSE);
4418 gtk_label_set_use_underline_internal (label, TRUE);
4420 gtk_label_recalculate (label);
4422 g_object_thaw_notify (G_OBJECT (label));
4426 gtk_label_realize (GtkWidget *widget)
4428 GtkLabel *label = GTK_LABEL (widget);
4429 GtkLabelPrivate *priv = label->priv;
4431 GTK_WIDGET_CLASS (gtk_label_parent_class)->realize (widget);
4433 if (priv->select_info)
4434 gtk_label_create_window (label);
4438 gtk_label_unrealize (GtkWidget *widget)
4440 GtkLabel *label = GTK_LABEL (widget);
4441 GtkLabelPrivate *priv = label->priv;
4443 if (priv->select_info)
4444 gtk_label_destroy_window (label);
4446 GTK_WIDGET_CLASS (gtk_label_parent_class)->unrealize (widget);
4450 gtk_label_map (GtkWidget *widget)
4452 GtkLabel *label = GTK_LABEL (widget);
4453 GtkLabelPrivate *priv = label->priv;
4455 GTK_WIDGET_CLASS (gtk_label_parent_class)->map (widget);
4457 if (priv->select_info)
4458 gdk_window_show (priv->select_info->window);
4462 gtk_label_unmap (GtkWidget *widget)
4464 GtkLabel *label = GTK_LABEL (widget);
4465 GtkLabelPrivate *priv = label->priv;
4467 if (priv->select_info)
4468 gdk_window_hide (priv->select_info->window);
4470 GTK_WIDGET_CLASS (gtk_label_parent_class)->unmap (widget);
4474 window_to_layout_coords (GtkLabel *label,
4478 GtkAllocation allocation;
4482 widget = GTK_WIDGET (label);
4484 /* get layout location in widget->window coords */
4485 get_layout_location (label, &lx, &ly);
4487 gtk_widget_get_allocation (widget, &allocation);
4491 *x += allocation.x; /* go to widget->window */
4492 *x -= lx; /* go to layout */
4497 *y += allocation.y; /* go to widget->window */
4498 *y -= ly; /* go to layout */
4504 layout_to_window_coords (GtkLabel *label,
4511 widget = GTK_WIDGET (label);
4513 /* get layout location in widget->window coords */
4514 get_layout_location (label, &lx, &ly);
4518 *x += lx; /* go to widget->window */
4519 *x -= widget->allocation.x; /* go to selection window */
4524 *y += ly; /* go to widget->window */
4525 *y -= widget->allocation.y; /* go to selection window */
4531 get_layout_index (GtkLabel *label,
4536 GtkLabelPrivate *priv = label->priv;
4538 const gchar *cluster;
4539 const gchar *cluster_end;
4544 gtk_label_ensure_layout (label);
4546 window_to_layout_coords (label, &x, &y);
4551 inside = pango_layout_xy_to_index (priv->layout,
4555 cluster = priv->text + *index;
4556 cluster_end = cluster;
4559 cluster_end = g_utf8_next_char (cluster_end);
4563 *index += (cluster_end - cluster);
4569 gtk_label_select_word (GtkLabel *label)
4571 GtkLabelPrivate *priv = label->priv;
4574 gint start_index = gtk_label_move_backward_word (label, priv->select_info->selection_end);
4575 gint end_index = gtk_label_move_forward_word (label, priv->select_info->selection_end);
4577 min = MIN (priv->select_info->selection_anchor,
4578 priv->select_info->selection_end);
4579 max = MAX (priv->select_info->selection_anchor,
4580 priv->select_info->selection_end);
4582 min = MIN (min, start_index);
4583 max = MAX (max, end_index);
4585 gtk_label_select_region_index (label, min, max);
4589 gtk_label_grab_focus (GtkWidget *widget)
4591 GtkLabel *label = GTK_LABEL (widget);
4592 GtkLabelPrivate *priv = label->priv;
4593 gboolean select_on_focus;
4596 if (priv->select_info == NULL)
4599 GTK_WIDGET_CLASS (gtk_label_parent_class)->grab_focus (widget);
4601 if (priv->select_info->selectable)
4603 g_object_get (gtk_widget_get_settings (widget),
4604 "gtk-label-select-on-focus",
4608 if (select_on_focus && !priv->in_click)
4609 gtk_label_select_region (label, 0, -1);
4613 if (priv->select_info->links && !priv->in_click)
4615 link = priv->select_info->links->data;
4616 priv->select_info->selection_anchor = link->start;
4617 priv->select_info->selection_end = link->start;
4623 gtk_label_focus (GtkWidget *widget,
4624 GtkDirectionType direction)
4626 GtkLabel *label = GTK_LABEL (widget);
4627 GtkLabelPrivate *priv = label->priv;
4628 GtkLabelSelectionInfo *info = priv->select_info;
4629 GtkLabelLink *focus_link;
4632 if (!gtk_widget_is_focus (widget))
4634 gtk_widget_grab_focus (widget);
4637 focus_link = gtk_label_get_focus_link (label);
4638 if (focus_link && direction == GTK_DIR_TAB_BACKWARD)
4640 l = g_list_last (info->links);
4641 focus_link = l->data;
4642 info->selection_anchor = focus_link->start;
4643 info->selection_end = focus_link->start;
4653 if (info->selectable)
4657 if (info->selection_anchor != info->selection_end)
4660 index = info->selection_anchor;
4662 if (direction == GTK_DIR_TAB_FORWARD)
4663 for (l = info->links; l; l = l->next)
4665 GtkLabelLink *link = l->data;
4667 if (link->start > index)
4669 gtk_label_select_region_index (label, link->start, link->start);
4673 else if (direction == GTK_DIR_TAB_BACKWARD)
4674 for (l = g_list_last (info->links); l; l = l->prev)
4676 GtkLabelLink *link = l->data;
4678 if (link->end < index)
4680 gtk_label_select_region_index (label, link->start, link->start);
4689 focus_link = gtk_label_get_focus_link (label);
4692 case GTK_DIR_TAB_FORWARD:
4695 l = g_list_find (info->links, focus_link);
4702 case GTK_DIR_TAB_BACKWARD:
4705 l = g_list_find (info->links, focus_link);
4709 l = g_list_last (info->links);
4718 focus_link = l->data;
4719 info->selection_anchor = focus_link->start;
4720 info->selection_end = focus_link->start;
4721 gtk_widget_queue_draw (widget);
4733 gtk_label_button_press (GtkWidget *widget,
4734 GdkEventButton *event)
4736 GtkLabel *label = GTK_LABEL (widget);
4737 GtkLabelPrivate *priv = label->priv;
4738 GtkLabelSelectionInfo *info = priv->select_info;
4745 if (info->active_link)
4747 if (event->button == 1)
4749 info->link_clicked = 1;
4750 gtk_widget_queue_draw (widget);
4752 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4754 info->link_clicked = 1;
4755 gtk_label_do_popup (label, event);
4760 if (!info->selectable)
4763 info->in_drag = FALSE;
4764 info->select_words = FALSE;
4766 if (event->button == 1)
4768 if (!gtk_widget_has_focus (widget))
4770 priv->in_click = TRUE;
4771 gtk_widget_grab_focus (widget);
4772 priv->in_click = FALSE;
4775 if (event->type == GDK_3BUTTON_PRESS)
4777 gtk_label_select_region_index (label, 0, strlen (priv->text));
4781 if (event->type == GDK_2BUTTON_PRESS)
4783 info->select_words = TRUE;
4784 gtk_label_select_word (label);
4788 get_layout_index (label, event->x, event->y, &index);
4790 min = MIN (info->selection_anchor, info->selection_end);
4791 max = MAX (info->selection_anchor, info->selection_end);
4793 if ((info->selection_anchor != info->selection_end) &&
4794 (event->state & GDK_SHIFT_MASK))
4796 if (index > min && index < max)
4798 /* truncate selection, but keep it as big as possible */
4799 if (index - min > max - index)
4806 /* extend (same as motion) */
4807 min = MIN (min, index);
4808 max = MAX (max, index);
4811 /* ensure the anchor is opposite index */
4819 gtk_label_select_region_index (label, min, max);
4823 if (event->type == GDK_3BUTTON_PRESS)
4824 gtk_label_select_region_index (label, 0, strlen (priv->text));
4825 else if (event->type == GDK_2BUTTON_PRESS)
4826 gtk_label_select_word (label);
4827 else if (min < max && min <= index && index <= max)
4829 info->in_drag = TRUE;
4830 info->drag_start_x = event->x;
4831 info->drag_start_y = event->y;
4834 /* start a replacement */
4835 gtk_label_select_region_index (label, index, index);
4840 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4842 gtk_label_do_popup (label, event);
4850 gtk_label_button_release (GtkWidget *widget,
4851 GdkEventButton *event)
4854 GtkLabel *label = GTK_LABEL (widget);
4855 GtkLabelPrivate *priv = label->priv;
4856 GtkLabelSelectionInfo *info = priv->select_info;
4866 get_layout_index (label, event->x, event->y, &index);
4867 gtk_label_select_region_index (label, index, index);
4872 if (event->button != 1)
4875 if (info->active_link &&
4876 info->selection_anchor == info->selection_end &&
4879 emit_activate_link (label, info->active_link);
4880 info->link_clicked = 0;
4885 /* The goal here is to return TRUE iff we ate the
4886 * button press to start selecting.
4892 connect_mnemonics_visible_notify (GtkLabel *label)
4894 GtkLabelPrivate *priv = label->priv;
4895 GtkWidget *toplevel;
4898 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
4900 if (!GTK_IS_WINDOW (toplevel))
4903 /* always set up this widgets initial value */
4904 priv->mnemonics_visible =
4905 gtk_window_get_mnemonics_visible (GTK_WINDOW (toplevel));
4908 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (toplevel),
4909 "gtk-label-mnemonics-visible-connected"));
4913 g_signal_connect (toplevel,
4914 "notify::mnemonics-visible",
4915 G_CALLBACK (label_mnemonics_visible_changed),
4917 g_object_set_data (G_OBJECT (toplevel),
4918 "gtk-label-mnemonics-visible-connected",
4919 GINT_TO_POINTER (1));
4924 drag_begin_cb (GtkWidget *widget,
4925 GdkDragContext *context,
4928 GtkLabel *label = GTK_LABEL (widget);
4929 GtkLabelPrivate *priv = label->priv;
4930 cairo_surface_t *surface = NULL;
4932 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
4934 if ((priv->select_info->selection_anchor !=
4935 priv->select_info->selection_end) &&
4941 start = MIN (priv->select_info->selection_anchor,
4942 priv->select_info->selection_end);
4943 end = MAX (priv->select_info->selection_anchor,
4944 priv->select_info->selection_end);
4946 len = strlen (priv->text);
4954 surface = _gtk_text_util_create_drag_icon (widget,
4961 gtk_drag_set_icon_surface (context, surface);
4962 cairo_surface_destroy (surface);
4966 gtk_drag_set_icon_default (context);
4971 gtk_label_motion (GtkWidget *widget,
4972 GdkEventMotion *event)
4974 GtkLabel *label = GTK_LABEL (widget);
4975 GtkLabelPrivate *priv = label->priv;
4976 GtkLabelSelectionInfo *info = priv->select_info;
4982 if (info->links && !info->in_drag)
4986 gboolean found = FALSE;
4988 if (info->selection_anchor == info->selection_end)
4990 if (get_layout_index (label, event->x, event->y, &index))
4992 for (l = info->links; l != NULL; l = l->next)
4995 if (index >= link->start && index <= link->end)
5006 if (info->active_link != link)
5008 info->link_clicked = 0;
5009 info->active_link = link;
5010 gtk_label_update_cursor (label);
5011 gtk_widget_queue_draw (widget);
5016 if (info->active_link != NULL)
5018 info->link_clicked = 0;
5019 info->active_link = NULL;
5020 gtk_label_update_cursor (label);
5021 gtk_widget_queue_draw (widget);
5026 if (!info->selectable)
5029 if ((event->state & GDK_BUTTON1_MASK) == 0)
5034 if (gtk_drag_check_threshold (widget,
5037 event->x, event->y))
5039 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
5041 gtk_target_list_add_text_targets (target_list, 0);
5043 g_signal_connect (widget, "drag-begin",
5044 G_CALLBACK (drag_begin_cb), NULL);
5045 gtk_drag_begin (widget, target_list,
5047 1, (GdkEvent *)event);
5049 info->in_drag = FALSE;
5051 gtk_target_list_unref (target_list);
5058 gdk_window_get_device_position (info->window, event->device, &x, &y, NULL);
5059 get_layout_index (label, x, y, &index);
5061 if (info->select_words)
5064 gint old_min, old_max;
5067 min = gtk_label_move_backward_word (label, index);
5068 max = gtk_label_move_forward_word (label, index);
5070 anchor = info->selection_anchor;
5071 end = info->selection_end;
5073 old_min = MIN (anchor, end);
5074 old_max = MAX (anchor, end);
5081 else if (old_max < max)
5086 else if (anchor == old_min)
5097 gtk_label_select_region_index (label, anchor, end);
5100 gtk_label_select_region_index (label, info->selection_anchor, index);
5107 gtk_label_leave_notify (GtkWidget *widget,
5108 GdkEventCrossing *event)
5110 GtkLabel *label = GTK_LABEL (widget);
5111 GtkLabelPrivate *priv = label->priv;
5113 if (priv->select_info)
5115 priv->select_info->active_link = NULL;
5116 gtk_label_update_cursor (label);
5117 gtk_widget_queue_draw (widget);
5120 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event)
5121 return GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event (widget, event);
5127 gtk_label_create_window (GtkLabel *label)
5129 GtkLabelPrivate *priv = label->priv;
5130 GtkAllocation allocation;
5132 GdkWindowAttr attributes;
5133 gint attributes_mask;
5135 g_assert (priv->select_info);
5136 widget = GTK_WIDGET (label);
5137 g_assert (gtk_widget_get_realized (widget));
5139 if (priv->select_info->window)
5142 gtk_widget_get_allocation (widget, &allocation);
5144 attributes.x = allocation.x;
5145 attributes.y = allocation.y;
5146 attributes.width = allocation.width;
5147 attributes.height = allocation.height;
5148 attributes.window_type = GDK_WINDOW_CHILD;
5149 attributes.wclass = GDK_INPUT_ONLY;
5150 attributes.override_redirect = TRUE;
5151 attributes.event_mask = gtk_widget_get_events (widget) |
5152 GDK_BUTTON_PRESS_MASK |
5153 GDK_BUTTON_RELEASE_MASK |
5154 GDK_LEAVE_NOTIFY_MASK |
5155 GDK_BUTTON_MOTION_MASK |
5156 GDK_POINTER_MOTION_MASK |
5157 GDK_POINTER_MOTION_HINT_MASK;
5158 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
5159 if (gtk_widget_is_sensitive (widget))
5161 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
5163 attributes_mask |= GDK_WA_CURSOR;
5167 priv->select_info->window = gdk_window_new (gtk_widget_get_window (widget),
5168 &attributes, attributes_mask);
5169 gdk_window_set_user_data (priv->select_info->window, widget);
5171 if (attributes_mask & GDK_WA_CURSOR)
5172 g_object_unref (attributes.cursor);
5176 gtk_label_destroy_window (GtkLabel *label)
5178 GtkLabelPrivate *priv = label->priv;
5180 g_assert (priv->select_info);
5182 if (priv->select_info->window == NULL)
5185 gdk_window_set_user_data (priv->select_info->window, NULL);
5186 gdk_window_destroy (priv->select_info->window);
5187 priv->select_info->window = NULL;
5191 gtk_label_ensure_select_info (GtkLabel *label)
5193 GtkLabelPrivate *priv = label->priv;
5195 if (priv->select_info == NULL)
5197 priv->select_info = g_new0 (GtkLabelSelectionInfo, 1);
5199 gtk_widget_set_can_focus (GTK_WIDGET (label), TRUE);
5201 if (gtk_widget_get_realized (GTK_WIDGET (label)))
5202 gtk_label_create_window (label);
5204 if (gtk_widget_get_mapped (GTK_WIDGET (label)))
5205 gdk_window_show (priv->select_info->window);
5210 gtk_label_clear_select_info (GtkLabel *label)
5212 GtkLabelPrivate *priv = label->priv;
5214 if (priv->select_info == NULL)
5217 if (!priv->select_info->selectable && !priv->select_info->links)
5219 gtk_label_destroy_window (label);
5221 g_free (priv->select_info);
5222 priv->select_info = NULL;
5224 gtk_widget_set_can_focus (GTK_WIDGET (label), FALSE);
5229 * gtk_label_set_selectable:
5230 * @label: a #GtkLabel
5231 * @setting: %TRUE to allow selecting text in the label
5233 * Selectable labels allow the user to select text from the label, for
5237 gtk_label_set_selectable (GtkLabel *label,
5240 GtkLabelPrivate *priv;
5241 gboolean old_setting;
5243 g_return_if_fail (GTK_IS_LABEL (label));
5247 setting = setting != FALSE;
5248 old_setting = priv->select_info && priv->select_info->selectable;
5252 gtk_label_ensure_select_info (label);
5253 priv->select_info->selectable = TRUE;
5254 gtk_label_update_cursor (label);
5260 /* unselect, to give up the selection */
5261 gtk_label_select_region (label, 0, 0);
5263 priv->select_info->selectable = FALSE;
5264 gtk_label_clear_select_info (label);
5265 gtk_label_update_cursor (label);
5268 if (setting != old_setting)
5270 g_object_freeze_notify (G_OBJECT (label));
5271 g_object_notify (G_OBJECT (label), "selectable");
5272 g_object_notify (G_OBJECT (label), "cursor-position");
5273 g_object_notify (G_OBJECT (label), "selection-bound");
5274 g_object_thaw_notify (G_OBJECT (label));
5275 gtk_widget_queue_draw (GTK_WIDGET (label));
5280 * gtk_label_get_selectable:
5281 * @label: a #GtkLabel
5283 * Gets the value set by gtk_label_set_selectable().
5285 * Return value: %TRUE if the user can copy text from the label
5288 gtk_label_get_selectable (GtkLabel *label)
5290 GtkLabelPrivate *priv;
5292 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5296 return priv->select_info && priv->select_info->selectable;
5300 * gtk_label_set_angle:
5301 * @label: a #GtkLabel
5302 * @angle: the angle that the baseline of the label makes with
5303 * the horizontal, in degrees, measured counterclockwise
5305 * Sets the angle of rotation for the label. An angle of 90 reads from
5306 * from bottom to top, an angle of 270, from top to bottom. The angle
5307 * setting for the label is ignored if the label is selectable,
5308 * wrapped, or ellipsized.
5313 gtk_label_set_angle (GtkLabel *label,
5316 GtkLabelPrivate *priv;
5318 g_return_if_fail (GTK_IS_LABEL (label));
5322 /* Canonicalize to [0,360]. We don't canonicalize 360 to 0, because
5323 * double property ranges are inclusive, and changing 360 to 0 would
5324 * make a property editor behave strangely.
5326 if (angle < 0 || angle > 360.0)
5327 angle = angle - 360. * floor (angle / 360.);
5329 if (priv->angle != angle)
5331 priv->angle = angle;
5333 gtk_label_clear_layout (label);
5334 gtk_widget_queue_resize (GTK_WIDGET (label));
5336 g_object_notify (G_OBJECT (label), "angle");
5341 * gtk_label_get_angle:
5342 * @label: a #GtkLabel
5344 * Gets the angle of rotation for the label. See
5345 * gtk_label_set_angle().
5347 * Return value: the angle of rotation for the label
5352 gtk_label_get_angle (GtkLabel *label)
5354 g_return_val_if_fail (GTK_IS_LABEL (label), 0.0);
5356 return label->priv->angle;
5360 gtk_label_set_selection_text (GtkLabel *label,
5361 GtkSelectionData *selection_data)
5363 GtkLabelPrivate *priv = label->priv;
5365 if ((priv->select_info->selection_anchor !=
5366 priv->select_info->selection_end) &&
5372 start = MIN (priv->select_info->selection_anchor,
5373 priv->select_info->selection_end);
5374 end = MAX (priv->select_info->selection_anchor,
5375 priv->select_info->selection_end);
5377 len = strlen (priv->text);
5385 gtk_selection_data_set_text (selection_data,
5392 gtk_label_drag_data_get (GtkWidget *widget,
5393 GdkDragContext *context,
5394 GtkSelectionData *selection_data,
5398 gtk_label_set_selection_text (GTK_LABEL (widget), selection_data);
5402 get_text_callback (GtkClipboard *clipboard,
5403 GtkSelectionData *selection_data,
5405 gpointer user_data_or_owner)
5407 gtk_label_set_selection_text (GTK_LABEL (user_data_or_owner), selection_data);
5411 clear_text_callback (GtkClipboard *clipboard,
5412 gpointer user_data_or_owner)
5415 GtkLabelPrivate *priv;
5417 label = GTK_LABEL (user_data_or_owner);
5420 if (priv->select_info)
5422 priv->select_info->selection_anchor = priv->select_info->selection_end;
5424 gtk_widget_queue_draw (GTK_WIDGET (label));
5429 gtk_label_select_region_index (GtkLabel *label,
5433 GtkLabelPrivate *priv;
5435 g_return_if_fail (GTK_IS_LABEL (label));
5439 if (priv->select_info && priv->select_info->selectable)
5441 GtkClipboard *clipboard;
5443 if (priv->select_info->selection_anchor == anchor_index &&
5444 priv->select_info->selection_end == end_index)
5447 priv->select_info->selection_anchor = anchor_index;
5448 priv->select_info->selection_end = end_index;
5450 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label),
5451 GDK_SELECTION_PRIMARY);
5453 if (anchor_index != end_index)
5455 GtkTargetList *list;
5456 GtkTargetEntry *targets;
5459 list = gtk_target_list_new (NULL, 0);
5460 gtk_target_list_add_text_targets (list, 0);
5461 targets = gtk_target_table_new_from_list (list, &n_targets);
5463 gtk_clipboard_set_with_owner (clipboard,
5466 clear_text_callback,
5469 gtk_target_table_free (targets, n_targets);
5470 gtk_target_list_unref (list);
5474 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (label))
5475 gtk_clipboard_clear (clipboard);
5478 gtk_widget_queue_draw (GTK_WIDGET (label));
5480 g_object_freeze_notify (G_OBJECT (label));
5481 g_object_notify (G_OBJECT (label), "cursor-position");
5482 g_object_notify (G_OBJECT (label), "selection-bound");
5483 g_object_thaw_notify (G_OBJECT (label));
5488 * gtk_label_select_region:
5489 * @label: a #GtkLabel
5490 * @start_offset: start offset (in characters not bytes)
5491 * @end_offset: end offset (in characters not bytes)
5493 * Selects a range of characters in the label, if the label is selectable.
5494 * See gtk_label_set_selectable(). If the label is not selectable,
5495 * this function has no effect. If @start_offset or
5496 * @end_offset are -1, then the end of the label will be substituted.
5499 gtk_label_select_region (GtkLabel *label,
5503 GtkLabelPrivate *priv;
5505 g_return_if_fail (GTK_IS_LABEL (label));
5509 if (priv->text && priv->select_info)
5511 if (start_offset < 0)
5512 start_offset = g_utf8_strlen (priv->text, -1);
5515 end_offset = g_utf8_strlen (priv->text, -1);
5517 gtk_label_select_region_index (label,
5518 g_utf8_offset_to_pointer (priv->text, start_offset) - priv->text,
5519 g_utf8_offset_to_pointer (priv->text, end_offset) - priv->text);
5524 * gtk_label_get_selection_bounds:
5525 * @label: a #GtkLabel
5526 * @start: (out): return location for start of selection, as a character offset
5527 * @end: (out): return location for end of selection, as a character offset
5529 * Gets the selected range of characters in the label, returning %TRUE
5530 * if there's a selection.
5532 * Return value: %TRUE if selection is non-empty
5535 gtk_label_get_selection_bounds (GtkLabel *label,
5539 GtkLabelPrivate *priv;
5541 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5545 if (priv->select_info == NULL)
5547 /* not a selectable label */
5557 gint start_index, end_index;
5558 gint start_offset, end_offset;
5561 start_index = MIN (priv->select_info->selection_anchor,
5562 priv->select_info->selection_end);
5563 end_index = MAX (priv->select_info->selection_anchor,
5564 priv->select_info->selection_end);
5566 len = strlen (priv->text);
5568 if (end_index > len)
5571 if (start_index > len)
5574 start_offset = g_utf8_strlen (priv->text, start_index);
5575 end_offset = g_utf8_strlen (priv->text, end_index);
5577 if (start_offset > end_offset)
5579 gint tmp = start_offset;
5580 start_offset = end_offset;
5585 *start = start_offset;
5590 return start_offset != end_offset;
5596 * gtk_label_get_layout:
5597 * @label: a #GtkLabel
5599 * Gets the #PangoLayout used to display the label.
5600 * The layout is useful to e.g. convert text positions to
5601 * pixel positions, in combination with gtk_label_get_layout_offsets().
5602 * The returned layout is owned by the @label so need not be
5603 * freed by the caller. The @label is free to recreate its layout at
5604 * any time, so it should be considered read-only.
5606 * Return value: (transfer none): the #PangoLayout for this label
5609 gtk_label_get_layout (GtkLabel *label)
5611 GtkLabelPrivate *priv;
5613 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
5617 gtk_label_ensure_layout (label);
5619 return priv->layout;
5623 * gtk_label_get_layout_offsets:
5624 * @label: a #GtkLabel
5625 * @x: (out) (allow-none): location to store X offset of layout, or %NULL
5626 * @y: (out) (allow-none): location to store Y offset of layout, or %NULL
5628 * Obtains the coordinates where the label will draw the #PangoLayout
5629 * representing the text in the label; useful to convert mouse events
5630 * into coordinates inside the #PangoLayout, e.g. to take some action
5631 * if some part of the label is clicked. Of course you will need to
5632 * create a #GtkEventBox to receive the events, and pack the label
5633 * inside it, since labels are a #GTK_NO_WINDOW widget. Remember
5634 * when using the #PangoLayout functions you need to convert to
5635 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
5638 gtk_label_get_layout_offsets (GtkLabel *label,
5642 g_return_if_fail (GTK_IS_LABEL (label));
5644 gtk_label_ensure_layout (label);
5646 get_layout_location (label, x, y);
5650 * gtk_label_set_use_markup:
5651 * @label: a #GtkLabel
5652 * @setting: %TRUE if the label's text should be parsed for markup.
5654 * Sets whether the text of the label contains markup in <link
5655 * linkend="PangoMarkupFormat">Pango's text markup
5656 * language</link>. See gtk_label_set_markup().
5659 gtk_label_set_use_markup (GtkLabel *label,
5662 g_return_if_fail (GTK_IS_LABEL (label));
5664 g_object_freeze_notify (G_OBJECT (label));
5666 gtk_label_set_use_markup_internal (label, setting);
5667 gtk_label_recalculate (label);
5669 g_object_thaw_notify (G_OBJECT (label));
5673 * gtk_label_get_use_markup:
5674 * @label: a #GtkLabel
5676 * Returns whether the label's text is interpreted as marked up with
5677 * the <link linkend="PangoMarkupFormat">Pango text markup
5678 * language</link>. See gtk_label_set_use_markup ().
5680 * Return value: %TRUE if the label's text will be parsed for markup.
5683 gtk_label_get_use_markup (GtkLabel *label)
5685 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5687 return label->priv->use_markup;
5691 * gtk_label_set_use_underline:
5692 * @label: a #GtkLabel
5693 * @setting: %TRUE if underlines in the text indicate mnemonics
5695 * If true, an underline in the text indicates the next character should be
5696 * used for the mnemonic accelerator key.
5699 gtk_label_set_use_underline (GtkLabel *label,
5702 g_return_if_fail (GTK_IS_LABEL (label));
5704 g_object_freeze_notify (G_OBJECT (label));
5706 gtk_label_set_use_underline_internal (label, setting);
5707 gtk_label_recalculate (label);
5709 g_object_thaw_notify (G_OBJECT (label));
5713 * gtk_label_get_use_underline:
5714 * @label: a #GtkLabel
5716 * Returns whether an embedded underline in the label indicates a
5717 * mnemonic. See gtk_label_set_use_underline().
5719 * Return value: %TRUE whether an embedded underline in the label indicates
5720 * the mnemonic accelerator keys.
5723 gtk_label_get_use_underline (GtkLabel *label)
5725 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5727 return label->priv->use_underline;
5731 * gtk_label_set_single_line_mode:
5732 * @label: a #GtkLabel
5733 * @single_line_mode: %TRUE if the label should be in single line mode
5735 * Sets whether the label is in single line mode.
5740 gtk_label_set_single_line_mode (GtkLabel *label,
5741 gboolean single_line_mode)
5743 GtkLabelPrivate *priv;
5745 g_return_if_fail (GTK_IS_LABEL (label));
5749 single_line_mode = single_line_mode != FALSE;
5751 if (priv->single_line_mode != single_line_mode)
5753 priv->single_line_mode = single_line_mode;
5755 gtk_label_clear_layout (label);
5756 gtk_widget_queue_resize (GTK_WIDGET (label));
5758 g_object_notify (G_OBJECT (label), "single-line-mode");
5763 * gtk_label_get_single_line_mode:
5764 * @label: a #GtkLabel
5766 * Returns whether the label is in single line mode.
5768 * Return value: %TRUE when the label is in single line mode.
5773 gtk_label_get_single_line_mode (GtkLabel *label)
5775 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5777 return label->priv->single_line_mode;
5780 /* Compute the X position for an offset that corresponds to the "more important
5781 * cursor position for that offset. We use this when trying to guess to which
5782 * end of the selection we should go to when the user hits the left or
5786 get_better_cursor (GtkLabel *label,
5791 GtkLabelPrivate *priv = label->priv;
5792 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5793 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5794 PangoDirection cursor_direction = get_cursor_direction (label);
5795 gboolean split_cursor;
5796 PangoRectangle strong_pos, weak_pos;
5798 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5799 "gtk-split-cursor", &split_cursor,
5802 gtk_label_ensure_layout (label);
5804 pango_layout_get_cursor_pos (priv->layout, index,
5805 &strong_pos, &weak_pos);
5809 *x = strong_pos.x / PANGO_SCALE;
5810 *y = strong_pos.y / PANGO_SCALE;
5814 if (keymap_direction == cursor_direction)
5816 *x = strong_pos.x / PANGO_SCALE;
5817 *y = strong_pos.y / PANGO_SCALE;
5821 *x = weak_pos.x / PANGO_SCALE;
5822 *y = weak_pos.y / PANGO_SCALE;
5829 gtk_label_move_logically (GtkLabel *label,
5833 GtkLabelPrivate *priv = label->priv;
5834 gint offset = g_utf8_pointer_to_offset (priv->text,
5835 priv->text + start);
5839 PangoLogAttr *log_attrs;
5843 gtk_label_ensure_layout (label);
5845 length = g_utf8_strlen (priv->text, -1);
5847 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5849 while (count > 0 && offset < length)
5853 while (offset < length && !log_attrs[offset].is_cursor_position);
5857 while (count < 0 && offset > 0)
5861 while (offset > 0 && !log_attrs[offset].is_cursor_position);
5869 return g_utf8_offset_to_pointer (priv->text, offset) - priv->text;
5873 gtk_label_move_visually (GtkLabel *label,
5877 GtkLabelPrivate *priv = label->priv;
5884 int new_index, new_trailing;
5885 gboolean split_cursor;
5888 gtk_label_ensure_layout (label);
5890 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5891 "gtk-split-cursor", &split_cursor,
5898 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5899 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5901 strong = keymap_direction == get_cursor_direction (label);
5906 pango_layout_move_cursor_visually (priv->layout, strong, index, 0, 1, &new_index, &new_trailing);
5911 pango_layout_move_cursor_visually (priv->layout, strong, index, 0, -1, &new_index, &new_trailing);
5915 if (new_index < 0 || new_index == G_MAXINT)
5920 while (new_trailing--)
5921 index = g_utf8_next_char (priv->text + new_index) - priv->text;
5928 gtk_label_move_forward_word (GtkLabel *label,
5931 GtkLabelPrivate *priv = label->priv;
5932 gint new_pos = g_utf8_pointer_to_offset (priv->text,
5933 priv->text + start);
5936 length = g_utf8_strlen (priv->text, -1);
5937 if (new_pos < length)
5939 PangoLogAttr *log_attrs;
5942 gtk_label_ensure_layout (label);
5944 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5946 /* Find the next word end */
5948 while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
5954 return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
5959 gtk_label_move_backward_word (GtkLabel *label,
5962 GtkLabelPrivate *priv = label->priv;
5963 gint new_pos = g_utf8_pointer_to_offset (priv->text,
5964 priv->text + start);
5968 PangoLogAttr *log_attrs;
5971 gtk_label_ensure_layout (label);
5973 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5977 /* Find the previous word beginning */
5978 while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
5984 return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
5988 gtk_label_move_cursor (GtkLabel *label,
5989 GtkMovementStep step,
5991 gboolean extend_selection)
5993 GtkLabelPrivate *priv = label->priv;
5997 if (priv->select_info == NULL)
6000 old_pos = new_pos = priv->select_info->selection_end;
6002 if (priv->select_info->selection_end != priv->select_info->selection_anchor &&
6005 /* If we have a current selection and aren't extending it, move to the
6006 * start/or end of the selection as appropriate
6010 case GTK_MOVEMENT_VISUAL_POSITIONS:
6013 gint anchor_x, anchor_y;
6014 gboolean end_is_left;
6016 get_better_cursor (label, priv->select_info->selection_end, &end_x, &end_y);
6017 get_better_cursor (label, priv->select_info->selection_anchor, &anchor_x, &anchor_y);
6019 end_is_left = (end_y < anchor_y) || (end_y == anchor_y && end_x < anchor_x);
6022 new_pos = end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
6024 new_pos = !end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
6027 case GTK_MOVEMENT_LOGICAL_POSITIONS:
6028 case GTK_MOVEMENT_WORDS:
6030 new_pos = MIN (priv->select_info->selection_end, priv->select_info->selection_anchor);
6032 new_pos = MAX (priv->select_info->selection_end, priv->select_info->selection_anchor);
6034 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
6035 case GTK_MOVEMENT_PARAGRAPH_ENDS:
6036 case GTK_MOVEMENT_BUFFER_ENDS:
6037 /* FIXME: Can do better here */
6038 new_pos = count < 0 ? 0 : strlen (priv->text);
6040 case GTK_MOVEMENT_DISPLAY_LINES:
6041 case GTK_MOVEMENT_PARAGRAPHS:
6042 case GTK_MOVEMENT_PAGES:
6043 case GTK_MOVEMENT_HORIZONTAL_PAGES:
6051 case GTK_MOVEMENT_LOGICAL_POSITIONS:
6052 new_pos = gtk_label_move_logically (label, new_pos, count);
6054 case GTK_MOVEMENT_VISUAL_POSITIONS:
6055 new_pos = gtk_label_move_visually (label, new_pos, count);
6056 if (new_pos == old_pos)
6058 if (!extend_selection)
6060 if (!gtk_widget_keynav_failed (GTK_WIDGET (label),
6062 GTK_DIR_RIGHT : GTK_DIR_LEFT))
6064 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
6067 gtk_widget_child_focus (toplevel,
6069 GTK_DIR_RIGHT : GTK_DIR_LEFT);
6074 gtk_widget_error_bell (GTK_WIDGET (label));
6078 case GTK_MOVEMENT_WORDS:
6081 new_pos = gtk_label_move_forward_word (label, new_pos);
6086 new_pos = gtk_label_move_backward_word (label, new_pos);
6089 if (new_pos == old_pos)
6090 gtk_widget_error_bell (GTK_WIDGET (label));
6092 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
6093 case GTK_MOVEMENT_PARAGRAPH_ENDS:
6094 case GTK_MOVEMENT_BUFFER_ENDS:
6095 /* FIXME: Can do better here */
6096 new_pos = count < 0 ? 0 : strlen (priv->text);
6097 if (new_pos == old_pos)
6098 gtk_widget_error_bell (GTK_WIDGET (label));
6100 case GTK_MOVEMENT_DISPLAY_LINES:
6101 case GTK_MOVEMENT_PARAGRAPHS:
6102 case GTK_MOVEMENT_PAGES:
6103 case GTK_MOVEMENT_HORIZONTAL_PAGES:
6108 if (extend_selection)
6109 gtk_label_select_region_index (label,
6110 priv->select_info->selection_anchor,
6113 gtk_label_select_region_index (label, new_pos, new_pos);
6117 gtk_label_copy_clipboard (GtkLabel *label)
6119 GtkLabelPrivate *priv = label->priv;
6121 if (priv->text && priv->select_info)
6125 GtkClipboard *clipboard;
6127 start = MIN (priv->select_info->selection_anchor,
6128 priv->select_info->selection_end);
6129 end = MAX (priv->select_info->selection_anchor,
6130 priv->select_info->selection_end);
6132 len = strlen (priv->text);
6140 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6143 gtk_clipboard_set_text (clipboard, priv->text + start, end - start);
6148 link = gtk_label_get_focus_link (label);
6150 gtk_clipboard_set_text (clipboard, link->uri, -1);
6156 gtk_label_select_all (GtkLabel *label)
6158 GtkLabelPrivate *priv = label->priv;
6160 gtk_label_select_region_index (label, 0, strlen (priv->text));
6163 /* Quick hack of a popup menu
6166 activate_cb (GtkWidget *menuitem,
6169 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6170 g_signal_emit_by_name (label, signal);
6174 append_action_signal (GtkLabel *label,
6176 const gchar *stock_id,
6177 const gchar *signal,
6180 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6182 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
6183 g_signal_connect (menuitem, "activate",
6184 G_CALLBACK (activate_cb), label);
6186 gtk_widget_set_sensitive (menuitem, sensitive);
6188 gtk_widget_show (menuitem);
6189 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6193 popup_menu_detach (GtkWidget *attach_widget,
6196 GtkLabel *label = GTK_LABEL (attach_widget);
6197 GtkLabelPrivate *priv = label->priv;
6199 if (priv->select_info)
6200 priv->select_info->popup_menu = NULL;
6204 popup_position_func (GtkMenu *menu,
6212 GtkAllocation allocation;
6216 label = GTK_LABEL (user_data);
6217 widget = GTK_WIDGET (label);
6219 g_return_if_fail (gtk_widget_get_realized (widget));
6221 screen = gtk_widget_get_screen (widget);
6222 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
6224 gtk_widget_get_allocation (widget, &allocation);
6229 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
6232 gtk_widget_get_allocation (widget, &allocation);
6234 *x += allocation.width / 2;
6235 *y += allocation.height;
6237 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
6238 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
6242 open_link_activate_cb (GtkMenuItem *menu_item,
6247 link = gtk_label_get_current_link (label);
6250 emit_activate_link (label, link);
6254 copy_link_activate_cb (GtkMenuItem *menu_item,
6257 GtkClipboard *clipboard;
6260 uri = gtk_label_get_current_uri (label);
6263 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6264 gtk_clipboard_set_text (clipboard, uri, -1);
6269 gtk_label_popup_menu (GtkWidget *widget)
6271 gtk_label_do_popup (GTK_LABEL (widget), NULL);
6277 gtk_label_do_popup (GtkLabel *label,
6278 GdkEventButton *event)
6280 GtkLabelPrivate *priv = label->priv;
6281 GtkWidget *menuitem;
6284 gboolean have_selection;
6287 if (!priv->select_info)
6290 if (priv->select_info->popup_menu)
6291 gtk_widget_destroy (priv->select_info->popup_menu);
6293 priv->select_info->popup_menu = menu = gtk_menu_new ();
6295 gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (label), popup_menu_detach);
6298 priv->select_info->selection_anchor != priv->select_info->selection_end;
6302 if (priv->select_info->link_clicked)
6303 link = priv->select_info->active_link;
6308 link = gtk_label_get_focus_link (label);
6310 if (!have_selection && link)
6313 menuitem = gtk_image_menu_item_new_with_mnemonic (_("_Open Link"));
6314 gtk_widget_show (menuitem);
6315 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6317 g_signal_connect (G_OBJECT (menuitem), "activate",
6318 G_CALLBACK (open_link_activate_cb), label);
6320 image = gtk_image_new_from_stock (GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_MENU);
6321 gtk_widget_show (image);
6322 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6324 /* Copy Link Address */
6325 menuitem = gtk_image_menu_item_new_with_mnemonic (_("Copy _Link Address"));
6326 gtk_widget_show (menuitem);
6327 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6329 g_signal_connect (G_OBJECT (menuitem), "activate",
6330 G_CALLBACK (copy_link_activate_cb), label);
6332 image = gtk_image_new_from_stock (GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
6333 gtk_widget_show (image);
6334 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6338 append_action_signal (label, menu, GTK_STOCK_CUT, "cut-clipboard", FALSE);
6339 append_action_signal (label, menu, GTK_STOCK_COPY, "copy-clipboard", have_selection);
6340 append_action_signal (label, menu, GTK_STOCK_PASTE, "paste-clipboard", FALSE);
6342 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
6343 gtk_widget_set_sensitive (menuitem, FALSE);
6344 gtk_widget_show (menuitem);
6345 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6347 menuitem = gtk_separator_menu_item_new ();
6348 gtk_widget_show (menuitem);
6349 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6351 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
6352 g_signal_connect_swapped (menuitem, "activate",
6353 G_CALLBACK (gtk_label_select_all), label);
6354 gtk_widget_show (menuitem);
6355 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6358 g_signal_emit (label, signals[POPULATE_POPUP], 0, menu);
6361 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6363 event->button, event->time);
6366 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6367 popup_position_func, label,
6368 0, gtk_get_current_event_time ());
6369 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
6374 gtk_label_clear_links (GtkLabel *label)
6376 GtkLabelPrivate *priv = label->priv;
6378 if (!priv->select_info)
6381 g_list_foreach (priv->select_info->links, (GFunc)link_free, NULL);
6382 g_list_free (priv->select_info->links);
6383 priv->select_info->links = NULL;
6384 priv->select_info->active_link = NULL;
6388 gtk_label_rescan_links (GtkLabel *label)
6390 GtkLabelPrivate *priv = label->priv;
6391 PangoLayout *layout = priv->layout;
6392 PangoAttrList *attlist;
6393 PangoAttrIterator *iter;
6396 if (!priv->select_info || !priv->select_info->links)
6399 attlist = pango_layout_get_attributes (layout);
6401 if (attlist == NULL)
6404 iter = pango_attr_list_get_iterator (attlist);
6406 links = priv->select_info->links;
6410 PangoAttribute *underline;
6411 PangoAttribute *color;
6413 underline = pango_attr_iterator_get (iter, PANGO_ATTR_UNDERLINE);
6414 color = pango_attr_iterator_get (iter, PANGO_ATTR_FOREGROUND);
6416 if (underline != NULL && color != NULL)
6419 PangoRectangle start_pos;
6420 PangoRectangle end_pos;
6423 pango_attr_iterator_range (iter, &start, &end);
6424 pango_layout_index_to_pos (layout, start, &start_pos);
6425 pango_layout_index_to_pos (layout, end, &end_pos);
6429 g_warning ("Ran out of links");
6433 links = links->next;
6434 link->start = start;
6437 } while (pango_attr_iterator_next (iter));
6439 pango_attr_iterator_destroy (iter);
6443 gtk_label_activate_link (GtkLabel *label,
6446 GtkWidget *widget = GTK_WIDGET (label);
6447 GError *error = NULL;
6449 if (!gtk_show_uri (gtk_widget_get_screen (widget),
6450 uri, gtk_get_current_event_time (), &error))
6452 g_warning ("Unable to show '%s': %s", uri, error->message);
6453 g_error_free (error);
6460 emit_activate_link (GtkLabel *label,
6463 GtkLabelPrivate *priv = label->priv;
6466 g_signal_emit (label, signals[ACTIVATE_LINK], 0, link->uri, &handled);
6467 if (handled && priv->track_links && !link->visited)
6469 link->visited = TRUE;
6470 /* FIXME: shouldn't have to redo everything here */
6471 gtk_label_recalculate (label);
6476 gtk_label_activate_current_link (GtkLabel *label)
6479 GtkWidget *widget = GTK_WIDGET (label);
6481 link = gtk_label_get_focus_link (label);
6485 emit_activate_link (label, link);
6489 GtkWidget *toplevel;
6491 GtkWidget *default_widget, *focus_widget;
6493 toplevel = gtk_widget_get_toplevel (widget);
6494 if (GTK_IS_WINDOW (toplevel))
6496 window = GTK_WINDOW (toplevel);
6500 default_widget = gtk_window_get_default_widget (window);
6501 focus_widget = gtk_window_get_focus (window);
6503 if (default_widget != widget &&
6504 !(widget == focus_widget && (!default_widget || !gtk_widget_is_sensitive (default_widget))))
6505 gtk_window_activate_default (window);
6511 static GtkLabelLink *
6512 gtk_label_get_current_link (GtkLabel *label)
6514 GtkLabelPrivate *priv = label->priv;
6517 if (!priv->select_info)
6520 if (priv->select_info->link_clicked)
6521 link = priv->select_info->active_link;
6523 link = gtk_label_get_focus_link (label);
6529 * gtk_label_get_current_uri:
6530 * @label: a #GtkLabel
6532 * Returns the URI for the currently active link in the label.
6533 * The active link is the one under the mouse pointer or, in a
6534 * selectable label, the link in which the text cursor is currently
6537 * This function is intended for use in a #GtkLabel::activate-link handler
6538 * or for use in a #GtkWidget::query-tooltip handler.
6540 * Returns: the currently active URI. The string is owned by GTK+ and must
6541 * not be freed or modified.
6546 gtk_label_get_current_uri (GtkLabel *label)
6550 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
6552 link = gtk_label_get_current_link (label);
6561 * gtk_label_set_track_visited_links:
6562 * @label: a #GtkLabel
6563 * @track_links: %TRUE to track visited links
6565 * Sets whether the label should keep track of clicked
6566 * links (and use a different color for them).
6571 gtk_label_set_track_visited_links (GtkLabel *label,
6572 gboolean track_links)
6574 GtkLabelPrivate *priv;
6576 g_return_if_fail (GTK_IS_LABEL (label));
6580 track_links = track_links != FALSE;
6582 if (priv->track_links != track_links)
6584 priv->track_links = track_links;
6586 /* FIXME: shouldn't have to redo everything here */
6587 gtk_label_recalculate (label);
6589 g_object_notify (G_OBJECT (label), "track-visited-links");
6594 * gtk_label_get_track_visited_links:
6595 * @label: a #GtkLabel
6597 * Returns whether the label is currently keeping track
6600 * Returns: %TRUE if clicked links are remembered
6605 gtk_label_get_track_visited_links (GtkLabel *label)
6607 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
6609 return label->priv->track_links;
6613 gtk_label_query_tooltip (GtkWidget *widget,
6616 gboolean keyboard_tip,
6617 GtkTooltip *tooltip)
6619 GtkLabel *label = GTK_LABEL (widget);
6620 GtkLabelPrivate *priv = label->priv;
6621 GtkLabelSelectionInfo *info = priv->select_info;
6625 if (info && info->links)
6629 if (info->selection_anchor == info->selection_end)
6630 index = info->selection_anchor;
6634 if (!get_layout_index (label, x, y, &index))
6640 for (l = info->links; l != NULL; l = l->next)
6642 GtkLabelLink *link = l->data;
6643 if (index >= link->start && index <= link->end)
6647 gtk_tooltip_set_markup (tooltip, link->title);
6656 return GTK_WIDGET_CLASS (gtk_label_parent_class)->query_tooltip (widget,