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, see <http://www.gnu.org/licenses/>.Free
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
31 #include "gtkaccellabel.h"
33 #include "gtkmarshalers.h"
35 #include "gtkwindow.h"
36 #include "gtkclipboard.h"
37 #include "gtkimagemenuitem.h"
39 #include "gtkseparatormenuitem.h"
40 #include "gtktextutil.h"
42 #include "gtkmenuitem.h"
43 #include "gtkmenushellprivate.h"
44 #include "gtknotebook.h"
46 #include "gtkbindings.h"
47 #include "gtkbuildable.h"
50 #include "gtktooltip.h"
51 #include "gtkprivate.h"
52 #include "gtktypebuiltins.h"
55 #include "a11y/gtklabelaccessible.h"
57 /* this is in case rint() is not provided by the compiler,
58 * such as in the case of C89 compilers, like MSVC
60 #include "fallback-c89.c"
64 * @Short_description: A widget that displays a small to medium amount of text
67 * The #GtkLabel widget displays a small amount of text. As the name
68 * implies, most labels are used to label another widget such as a
69 * #GtkButton, a #GtkMenuItem, or a #GtkComboBox.
71 * <refsect2 id="GtkLabel-BUILDER-UI">
72 * <title>GtkLabel as GtkBuildable</title>
74 * The GtkLabel implementation of the GtkBuildable interface supports a
75 * custom <attributes> element, which supports any number of <attribute>
76 * elements. the <attribute> element has attributes named name, value,
77 * start and end and allows you to specify #PangoAttribute values for this label.
80 * <title>A UI definition fragment specifying Pango attributes</title>
81 * <programlisting><![CDATA[
82 * <object class="GtkLabel">
84 * <attribute name="weight" value="PANGO_WEIGHT_BOLD"/>
85 * <attribute name="background" value="red" start="5" end="10"/>"
88 * ]]></programlisting>
90 * The start and end attributes specify the range of characters to which the
91 * Pango attribute applies. If start and end are not specified, the attribute is
92 * applied to the whole text. Note that specifying ranges does not make much
93 * sense with translatable attributes. Use markup embedded in the translatable
98 * <title>Mnemonics</title>
100 * Labels may contain <firstterm>mnemonics</firstterm>. Mnemonics are
101 * underlined characters in the label, used for keyboard navigation.
102 * Mnemonics are created by providing a string with an underscore before
103 * the mnemonic character, such as <literal>"_File"</literal>, to the
104 * functions gtk_label_new_with_mnemonic() or
105 * gtk_label_set_text_with_mnemonic().
107 * Mnemonics automatically activate any activatable widget the label is
108 * inside, such as a #GtkButton; if the label is not inside the
109 * mnemonic's target widget, you have to tell the label about the target
110 * using gtk_label_set_mnemonic_widget(). Here's a simple example where
111 * the label is inside a button:
115 * // Pressing Alt+H will activate this button
116 * button = gtk_button_new (<!-- -->);
117 * label = gtk_label_new_with_mnemonic ("_Hello");
118 * gtk_container_add (GTK_CONTAINER (button), label);
122 * There's a convenience function to create buttons with a mnemonic label
127 * // Pressing Alt+H will activate this button
128 * button = gtk_button_new_with_mnemonic ("_Hello");
132 * To create a mnemonic for a widget alongside the label, such as a
133 * #GtkEntry, you have to point the label at the entry with
134 * gtk_label_set_mnemonic_widget():
138 * // Pressing Alt+H will focus the entry
139 * entry = gtk_entry_new (<!-- -->);
140 * label = gtk_label_new_with_mnemonic ("_Hello");
141 * gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
147 * <title>Markup (styled text)</title>
149 * To make it easy to format text in a label (changing colors, fonts,
150 * etc.), label text can be provided in a simple <link
151 * linkend="PangoMarkupFormat">markup format</link>.
152 * Here's how to create a label with a small font:
156 * label = gtk_label_new (NULL);
157 * gtk_label_set_markup (GTK_LABEL (label), "<small>Small text</small>");
162 * linkend="PangoMarkupFormat">complete documentation</link> of available
163 * tags in the Pango manual.)
165 * The markup passed to gtk_label_set_markup() must be valid; for example,
166 * literal <, > and & characters must be escaped as \<,
167 * \gt;, and \&. If you pass text obtained from the user, file,
168 * or a network to gtk_label_set_markup(), you'll want to escape it with
169 * g_markup_escape_text() or g_markup_printf_escaped().
171 * Markup strings are just a convenient way to set the #PangoAttrList on
172 * a label; gtk_label_set_attributes() may be a simpler way to set
173 * attributes in some cases. Be careful though; #PangoAttrList tends to
174 * cause internationalization problems, unless you're applying attributes
175 * to the entire string (i.e. unless you set the range of each attribute
176 * to [0, %G_MAXINT)). The reason is that specifying the start_index and
177 * end_index for a #PangoAttribute requires knowledge of the exact string
178 * being displayed, so translations will cause problems.
182 * <title>Selectable labels</title>
183 * Labels can be made selectable with gtk_label_set_selectable().
184 * Selectable labels allow the user to copy the label contents to
185 * the clipboard. Only labels that contain useful-to-copy information
186 * — such as error messages — should be made selectable.
188 * <refsect2 id="label-text-layout">
189 * <title>Text layout</title>
191 * A label can contain any number of paragraphs, but will have
192 * performance problems if it contains more than a small number.
193 * Paragraphs are separated by newlines or other paragraph separators
194 * understood by Pango.
196 * Labels can automatically wrap text if you call
197 * gtk_label_set_line_wrap().
199 * gtk_label_set_justify() sets how the lines in a label align
200 * with one another. If you want to set how the label as a whole
201 * aligns in its available space, see gtk_misc_set_alignment().
203 * The #GtkLabel:width-chars and #GtkLabel:max-width-chars properties
204 * can be used to control the size allocation of ellipsized or wrapped
205 * labels. For ellipsizing labels, if either is specified (and less
206 * than the actual text size), it is used as the minimum width, and the actual
207 * text size is used as the natural width of the label. For wrapping labels,
208 * width-chars is used as the minimum width, if specified, and max-width-chars
209 * is used as the natural width. Even if max-width-chars specified, wrapping
210 * labels will be rewrapped to use all of the available width.
213 * Note that the interpretation of #GtkLabel:width-chars and
214 * #GtkLabel:max-width-chars has changed a bit with the introduction of
215 * <link linkend="geometry-management">width-for-height geometry management.</link>
220 * <title>Links</title>
222 * Since 2.18, GTK+ supports markup for clickable hyperlinks in addition
223 * to regular Pango markup. The markup for links is borrowed from HTML, using the
224 * <tag>a</tag> with href and title attributes. GTK+ renders links similar to the
225 * way they appear in web browsers, with colored, underlined text. The title
226 * attribute is displayed as a tooltip on the link. An example looks like this:
228 * <informalexample><programlisting>
229 * gtk_label_set_markup (label, "Go to the <a href="http://www.gtk.org" title="<i>Our</i> website">GTK+ website</a> for more...");
230 * </programlisting></informalexample>
232 * It is possible to implement custom handling for links and their tooltips with
233 * the #GtkLabel::activate-link signal and the gtk_label_get_current_uri() function.
238 struct _GtkLabelPrivate
240 GtkLabelSelectionInfo *select_info;
241 GtkWidget *mnemonic_widget;
242 GtkWindow *mnemonic_window;
244 PangoAttrList *attrs;
245 PangoAttrList *markup_attrs;
253 guint mnemonics_visible : 1;
256 guint use_underline : 1;
257 guint use_markup : 1;
259 guint single_line_mode : 1;
260 guint have_transform : 1;
263 guint pattern_set : 1;
264 guint track_links : 1;
266 guint mnemonic_keyval;
269 gint max_width_chars;
272 /* Notes about the handling of links:
274 * Links share the GtkLabelSelectionInfo struct with selectable labels.
275 * There are some new fields for links. The links field contains the list
276 * of GtkLabelLink structs that describe the links which are embedded in
277 * the label. The active_link field points to the link under the mouse
278 * pointer. For keyboard navigation, the 'focus' link is determined by
279 * finding the link which contains the selection_anchor position.
280 * The link_clicked field is used with button press and release events
281 * to ensure that pressing inside a link and releasing outside of it
282 * does not activate the link.
284 * Links are rendered with the link-color/visited-link-color colors
285 * that are determined by the style and with an underline. When the mouse
286 * pointer is over a link, the pointer is changed to indicate the link,
287 * and the background behind the link is rendered with the base[PRELIGHT]
288 * color. While a button is pressed over a link, the background is rendered
289 * with the base[ACTIVE] color.
291 * Labels with links accept keyboard focus, and it is possible to move
292 * the focus between the embedded links using Tab/Shift-Tab. The focus
293 * is indicated by a focus rectangle that is drawn around the link text.
294 * Pressing Enter activates the focussed link, and there is a suitable
295 * context menu for links that can be opened with the Menu key. Pressing
296 * Control-C copies the link URI to the clipboard.
298 * In selectable labels with links, link functionality is only available
299 * when the selection is empty.
304 gchar *title; /* the title attribute, used as tooltip */
305 gboolean visited; /* get set when the link is activated; this flag
306 * gets preserved over later set_markup() calls
308 gint start; /* position of the link in the PangoLayout */
312 struct _GtkLabelSelectionInfo
315 gint selection_anchor;
317 GtkWidget *popup_menu;
320 GtkLabelLink *active_link;
326 guint select_words : 1;
327 guint selectable : 1;
328 guint link_clicked : 1;
336 ACTIVATE_CURRENT_LINK,
351 PROP_MNEMONIC_KEYVAL,
352 PROP_MNEMONIC_WIDGET,
353 PROP_CURSOR_POSITION,
354 PROP_SELECTION_BOUND,
357 PROP_SINGLE_LINE_MODE,
359 PROP_MAX_WIDTH_CHARS,
360 PROP_TRACK_VISITED_LINKS
363 /* When rotating ellipsizable text we want the natural size to request
364 * more to ensure the label wont ever ellipsize in an allocation of full natural size.
366 #define ROTATION_ELLIPSIZE_PADDING 2
368 static guint signals[LAST_SIGNAL] = { 0 };
370 static const GdkColor default_link_color = { 0, 0, 0, 0xeeee };
371 static const GdkColor default_visited_link_color = { 0, 0x5555, 0x1a1a, 0x8b8b };
373 static void gtk_label_set_property (GObject *object,
377 static void gtk_label_get_property (GObject *object,
381 static void gtk_label_finalize (GObject *object);
382 static void gtk_label_destroy (GtkWidget *widget);
383 static void gtk_label_size_allocate (GtkWidget *widget,
384 GtkAllocation *allocation);
385 static void gtk_label_state_flags_changed (GtkWidget *widget,
386 GtkStateFlags prev_state);
387 static gint gtk_label_draw (GtkWidget *widget,
389 static gboolean gtk_label_focus (GtkWidget *widget,
390 GtkDirectionType direction);
392 static void gtk_label_realize (GtkWidget *widget);
393 static void gtk_label_unrealize (GtkWidget *widget);
394 static void gtk_label_map (GtkWidget *widget);
395 static void gtk_label_unmap (GtkWidget *widget);
397 static gboolean gtk_label_button_press (GtkWidget *widget,
398 GdkEventButton *event);
399 static gboolean gtk_label_button_release (GtkWidget *widget,
400 GdkEventButton *event);
401 static gboolean gtk_label_motion (GtkWidget *widget,
402 GdkEventMotion *event);
403 static gboolean gtk_label_leave_notify (GtkWidget *widget,
404 GdkEventCrossing *event);
406 static void gtk_label_grab_focus (GtkWidget *widget);
408 static gboolean gtk_label_query_tooltip (GtkWidget *widget,
411 gboolean keyboard_tip,
412 GtkTooltip *tooltip);
414 static void gtk_label_set_text_internal (GtkLabel *label,
416 static void gtk_label_set_label_internal (GtkLabel *label,
418 static void gtk_label_set_use_markup_internal (GtkLabel *label,
420 static void gtk_label_set_use_underline_internal (GtkLabel *label,
422 static void gtk_label_set_uline_text_internal (GtkLabel *label,
424 static void gtk_label_set_pattern_internal (GtkLabel *label,
425 const gchar *pattern,
426 gboolean is_mnemonic);
427 static void gtk_label_set_markup_internal (GtkLabel *label,
429 gboolean with_uline);
430 static void gtk_label_recalculate (GtkLabel *label);
431 static void gtk_label_hierarchy_changed (GtkWidget *widget,
432 GtkWidget *old_toplevel);
433 static void gtk_label_screen_changed (GtkWidget *widget,
434 GdkScreen *old_screen);
435 static gboolean gtk_label_popup_menu (GtkWidget *widget);
437 static void gtk_label_create_window (GtkLabel *label);
438 static void gtk_label_destroy_window (GtkLabel *label);
439 static void gtk_label_ensure_select_info (GtkLabel *label);
440 static void gtk_label_clear_select_info (GtkLabel *label);
441 static void gtk_label_update_cursor (GtkLabel *label);
442 static void gtk_label_clear_layout (GtkLabel *label);
443 static void gtk_label_ensure_layout (GtkLabel *label);
444 static void gtk_label_select_region_index (GtkLabel *label,
449 static gboolean gtk_label_mnemonic_activate (GtkWidget *widget,
450 gboolean group_cycling);
451 static void gtk_label_setup_mnemonic (GtkLabel *label,
453 static void gtk_label_drag_data_get (GtkWidget *widget,
454 GdkDragContext *context,
455 GtkSelectionData *selection_data,
459 static void gtk_label_buildable_interface_init (GtkBuildableIface *iface);
460 static gboolean gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
463 const gchar *tagname,
464 GMarkupParser *parser,
467 static void gtk_label_buildable_custom_finished (GtkBuildable *buildable,
470 const gchar *tagname,
474 static void connect_mnemonics_visible_notify (GtkLabel *label);
475 static gboolean separate_uline_pattern (const gchar *str,
481 /* For selectable labels: */
482 static void gtk_label_move_cursor (GtkLabel *label,
483 GtkMovementStep step,
485 gboolean extend_selection);
486 static void gtk_label_copy_clipboard (GtkLabel *label);
487 static void gtk_label_select_all (GtkLabel *label);
488 static void gtk_label_do_popup (GtkLabel *label,
489 GdkEventButton *event);
490 static gint gtk_label_move_forward_word (GtkLabel *label,
492 static gint gtk_label_move_backward_word (GtkLabel *label,
496 static void gtk_label_clear_links (GtkLabel *label);
497 static gboolean gtk_label_activate_link (GtkLabel *label,
499 static void gtk_label_activate_current_link (GtkLabel *label);
500 static GtkLabelLink *gtk_label_get_current_link (GtkLabel *label);
501 static void gtk_label_get_link_colors (GtkWidget *widget,
502 GdkColor *link_color,
503 GdkColor *visited_link_color);
504 static void emit_activate_link (GtkLabel *label,
507 static GtkSizeRequestMode gtk_label_get_request_mode (GtkWidget *widget);
508 static void gtk_label_get_preferred_width (GtkWidget *widget,
511 static void gtk_label_get_preferred_height (GtkWidget *widget,
514 static void gtk_label_get_preferred_width_for_height (GtkWidget *widget,
517 gint *natural_width);
518 static void gtk_label_get_preferred_height_for_width (GtkWidget *widget,
520 gint *minimum_height,
521 gint *natural_height);
523 static GtkBuildableIface *buildable_parent_iface = NULL;
525 G_DEFINE_TYPE_WITH_CODE (GtkLabel, gtk_label, GTK_TYPE_MISC,
526 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
527 gtk_label_buildable_interface_init))
530 add_move_binding (GtkBindingSet *binding_set,
533 GtkMovementStep step,
536 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
538 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
542 G_TYPE_BOOLEAN, FALSE);
544 /* Selection-extending version */
545 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
549 G_TYPE_BOOLEAN, TRUE);
553 gtk_label_class_init (GtkLabelClass *class)
555 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
556 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
557 GtkBindingSet *binding_set;
559 gobject_class->set_property = gtk_label_set_property;
560 gobject_class->get_property = gtk_label_get_property;
561 gobject_class->finalize = gtk_label_finalize;
563 widget_class->destroy = gtk_label_destroy;
564 widget_class->size_allocate = gtk_label_size_allocate;
565 widget_class->state_flags_changed = gtk_label_state_flags_changed;
566 widget_class->query_tooltip = gtk_label_query_tooltip;
567 widget_class->draw = gtk_label_draw;
568 widget_class->realize = gtk_label_realize;
569 widget_class->unrealize = gtk_label_unrealize;
570 widget_class->map = gtk_label_map;
571 widget_class->unmap = gtk_label_unmap;
572 widget_class->button_press_event = gtk_label_button_press;
573 widget_class->button_release_event = gtk_label_button_release;
574 widget_class->motion_notify_event = gtk_label_motion;
575 widget_class->leave_notify_event = gtk_label_leave_notify;
576 widget_class->hierarchy_changed = gtk_label_hierarchy_changed;
577 widget_class->screen_changed = gtk_label_screen_changed;
578 widget_class->mnemonic_activate = gtk_label_mnemonic_activate;
579 widget_class->drag_data_get = gtk_label_drag_data_get;
580 widget_class->grab_focus = gtk_label_grab_focus;
581 widget_class->popup_menu = gtk_label_popup_menu;
582 widget_class->focus = gtk_label_focus;
583 widget_class->get_request_mode = gtk_label_get_request_mode;
584 widget_class->get_preferred_width = gtk_label_get_preferred_width;
585 widget_class->get_preferred_height = gtk_label_get_preferred_height;
586 widget_class->get_preferred_width_for_height = gtk_label_get_preferred_width_for_height;
587 widget_class->get_preferred_height_for_width = gtk_label_get_preferred_height_for_width;
589 class->move_cursor = gtk_label_move_cursor;
590 class->copy_clipboard = gtk_label_copy_clipboard;
591 class->activate_link = gtk_label_activate_link;
594 * GtkLabel::move-cursor:
595 * @entry: the object which received the signal
596 * @step: the granularity of the move, as a #GtkMovementStep
597 * @count: the number of @step units to move
598 * @extend_selection: %TRUE if the move should extend the selection
600 * The ::move-cursor signal is a
601 * <link linkend="keybinding-signals">keybinding signal</link>
602 * which gets emitted when the user initiates a cursor movement.
603 * If the cursor is not visible in @entry, this signal causes
604 * the viewport to be moved instead.
606 * Applications should not connect to it, but may emit it with
607 * g_signal_emit_by_name() if they need to control the cursor
610 * The default bindings for this signal come in two variants,
611 * the variant with the Shift modifier extends the selection,
612 * the variant without the Shift modifer does not.
613 * There are too many key combinations to list them all here.
615 * <listitem>Arrow keys move by individual characters/lines</listitem>
616 * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
617 * <listitem>Home/End keys move to the ends of the buffer</listitem>
620 signals[MOVE_CURSOR] =
621 g_signal_new (I_("move-cursor"),
622 G_OBJECT_CLASS_TYPE (gobject_class),
623 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
624 G_STRUCT_OFFSET (GtkLabelClass, move_cursor),
626 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
628 GTK_TYPE_MOVEMENT_STEP,
633 * GtkLabel::copy-clipboard:
634 * @label: the object which received the signal
636 * The ::copy-clipboard signal is a
637 * <link linkend="keybinding-signals">keybinding signal</link>
638 * which gets emitted to copy the selection to the clipboard.
640 * The default binding for this signal is Ctrl-c.
642 signals[COPY_CLIPBOARD] =
643 g_signal_new (I_("copy-clipboard"),
644 G_OBJECT_CLASS_TYPE (gobject_class),
645 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
646 G_STRUCT_OFFSET (GtkLabelClass, copy_clipboard),
648 _gtk_marshal_VOID__VOID,
652 * GtkLabel::populate-popup:
653 * @label: The label on which the signal is emitted
654 * @menu: the menu that is being populated
656 * The ::populate-popup signal gets emitted before showing the
657 * context menu of the label. Note that only selectable labels
658 * have context menus.
660 * If you need to add items to the context menu, connect
661 * to this signal and append your menuitems to the @menu.
663 signals[POPULATE_POPUP] =
664 g_signal_new (I_("populate-popup"),
665 G_OBJECT_CLASS_TYPE (gobject_class),
667 G_STRUCT_OFFSET (GtkLabelClass, populate_popup),
669 _gtk_marshal_VOID__OBJECT,
674 * GtkLabel::activate-current-link:
675 * @label: The label on which the signal was emitted
677 * A <link linkend="keybinding-signals">keybinding signal</link>
678 * which gets emitted when the user activates a link in the label.
680 * Applications may also emit the signal with g_signal_emit_by_name()
681 * if they need to control activation of URIs programmatically.
683 * The default bindings for this signal are all forms of the Enter key.
687 signals[ACTIVATE_CURRENT_LINK] =
688 g_signal_new_class_handler ("activate-current-link",
689 G_TYPE_FROM_CLASS (gobject_class),
690 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
691 G_CALLBACK (gtk_label_activate_current_link),
693 _gtk_marshal_VOID__VOID,
697 * GtkLabel::activate-link:
698 * @label: The label on which the signal was emitted
699 * @uri: the URI that is activated
701 * The signal which gets emitted to activate a URI.
702 * Applications may connect to it to override the default behaviour,
703 * which is to call gtk_show_uri().
705 * Returns: %TRUE if the link has been activated
709 signals[ACTIVATE_LINK] =
710 g_signal_new ("activate-link",
711 G_TYPE_FROM_CLASS (gobject_class),
713 G_STRUCT_OFFSET (GtkLabelClass, activate_link),
714 _gtk_boolean_handled_accumulator, NULL,
715 _gtk_marshal_BOOLEAN__STRING,
716 G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
718 g_object_class_install_property (gobject_class,
720 g_param_spec_string ("label",
722 P_("The text of the label"),
724 GTK_PARAM_READWRITE));
725 g_object_class_install_property (gobject_class,
727 g_param_spec_boxed ("attributes",
729 P_("A list of style attributes to apply to the text of the label"),
730 PANGO_TYPE_ATTR_LIST,
731 GTK_PARAM_READWRITE));
732 g_object_class_install_property (gobject_class,
734 g_param_spec_boolean ("use-markup",
736 P_("The text of the label includes XML markup. See pango_parse_markup()"),
738 GTK_PARAM_READWRITE));
739 g_object_class_install_property (gobject_class,
741 g_param_spec_boolean ("use-underline",
743 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
745 GTK_PARAM_READWRITE));
747 g_object_class_install_property (gobject_class,
749 g_param_spec_enum ("justify",
751 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"),
752 GTK_TYPE_JUSTIFICATION,
754 GTK_PARAM_READWRITE));
756 g_object_class_install_property (gobject_class,
758 g_param_spec_string ("pattern",
760 P_("A string with _ characters in positions correspond to characters in the text to underline"),
762 GTK_PARAM_WRITABLE));
764 g_object_class_install_property (gobject_class,
766 g_param_spec_boolean ("wrap",
768 P_("If set, wrap lines if the text becomes too wide"),
770 GTK_PARAM_READWRITE));
772 * GtkLabel:wrap-mode:
774 * If line wrapping is on (see the #GtkLabel:wrap property) this controls
775 * how the line wrapping is done. The default is %PANGO_WRAP_WORD, which
776 * means wrap on word boundaries.
780 g_object_class_install_property (gobject_class,
782 g_param_spec_enum ("wrap-mode",
783 P_("Line wrap mode"),
784 P_("If wrap is set, controls how linewrapping is done"),
785 PANGO_TYPE_WRAP_MODE,
787 GTK_PARAM_READWRITE));
788 g_object_class_install_property (gobject_class,
790 g_param_spec_boolean ("selectable",
792 P_("Whether the label text can be selected with the mouse"),
794 GTK_PARAM_READWRITE));
795 g_object_class_install_property (gobject_class,
796 PROP_MNEMONIC_KEYVAL,
797 g_param_spec_uint ("mnemonic-keyval",
799 P_("The mnemonic accelerator key for this label"),
803 GTK_PARAM_READABLE));
804 g_object_class_install_property (gobject_class,
805 PROP_MNEMONIC_WIDGET,
806 g_param_spec_object ("mnemonic-widget",
807 P_("Mnemonic widget"),
808 P_("The widget to be activated when the label's mnemonic "
811 GTK_PARAM_READWRITE));
813 g_object_class_install_property (gobject_class,
814 PROP_CURSOR_POSITION,
815 g_param_spec_int ("cursor-position",
816 P_("Cursor Position"),
817 P_("The current position of the insertion cursor in chars"),
821 GTK_PARAM_READABLE));
823 g_object_class_install_property (gobject_class,
824 PROP_SELECTION_BOUND,
825 g_param_spec_int ("selection-bound",
826 P_("Selection Bound"),
827 P_("The position of the opposite end of the selection from the cursor in chars"),
831 GTK_PARAM_READABLE));
834 * GtkLabel:ellipsize:
836 * The preferred place to ellipsize the string, if the label does
837 * not have enough room to display the entire string, specified as a
838 * #PangoEllipsizeMode.
840 * Note that setting this property to a value other than
841 * %PANGO_ELLIPSIZE_NONE has the side-effect that the label requests
842 * only enough space to display the ellipsis "...". In particular, this
843 * means that ellipsizing labels do not work well in notebook tabs, unless
844 * the tab's #GtkNotebook:tab-expand property is set to %TRUE. Other ways
845 * to set a label's width are gtk_widget_set_size_request() and
846 * gtk_label_set_width_chars().
850 g_object_class_install_property (gobject_class,
852 g_param_spec_enum ("ellipsize",
854 P_("The preferred place to ellipsize the string, if the label does not have enough room to display the entire string"),
855 PANGO_TYPE_ELLIPSIZE_MODE,
856 PANGO_ELLIPSIZE_NONE,
857 GTK_PARAM_READWRITE));
860 * GtkLabel:width-chars:
862 * The desired width of the label, in characters. If this property is set to
863 * -1, the width will be calculated automatically.
865 * See the section on <link linkend="label-text-layout">text layout</link>
866 * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
867 * determine the width of ellipsized and wrapped labels.
871 g_object_class_install_property (gobject_class,
873 g_param_spec_int ("width-chars",
874 P_("Width In Characters"),
875 P_("The desired width of the label, in characters"),
879 GTK_PARAM_READWRITE));
882 * GtkLabel:single-line-mode:
884 * Whether the label is in single line mode. In single line mode,
885 * the height of the label does not depend on the actual text, it
886 * is always set to ascent + descent of the font. This can be an
887 * advantage in situations where resizing the label because of text
888 * changes would be distracting, e.g. in a statusbar.
892 g_object_class_install_property (gobject_class,
893 PROP_SINGLE_LINE_MODE,
894 g_param_spec_boolean ("single-line-mode",
895 P_("Single Line Mode"),
896 P_("Whether the label is in single line mode"),
898 GTK_PARAM_READWRITE));
903 * The angle that the baseline of the label makes with the horizontal,
904 * in degrees, measured counterclockwise. An angle of 90 reads from
905 * from bottom to top, an angle of 270, from top to bottom. Ignored
906 * if the label is selectable, wrapped, or ellipsized.
910 g_object_class_install_property (gobject_class,
912 g_param_spec_double ("angle",
914 P_("Angle at which the label is rotated"),
918 GTK_PARAM_READWRITE));
921 * GtkLabel:max-width-chars:
923 * The desired maximum width of the label, in characters. If this property
924 * is set to -1, the width will be calculated automatically.
926 * See the section on <link linkend="label-text-layout">text layout</link>
927 * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
928 * determine the width of ellipsized and wrapped labels.
932 g_object_class_install_property (gobject_class,
933 PROP_MAX_WIDTH_CHARS,
934 g_param_spec_int ("max-width-chars",
935 P_("Maximum Width In Characters"),
936 P_("The desired maximum width of the label, in characters"),
940 GTK_PARAM_READWRITE));
943 * GtkLabel:track-visited-links:
945 * Set this property to %TRUE to make the label track which links
946 * have been clicked. It will then apply the ::visited-link-color
947 * color, instead of ::link-color.
951 g_object_class_install_property (gobject_class,
952 PROP_TRACK_VISITED_LINKS,
953 g_param_spec_boolean ("track-visited-links",
954 P_("Track visited links"),
955 P_("Whether visited links should be tracked"),
957 GTK_PARAM_READWRITE));
962 binding_set = gtk_binding_set_by_class (class);
964 /* Moving the insertion point */
965 add_move_binding (binding_set, GDK_KEY_Right, 0,
966 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
968 add_move_binding (binding_set, GDK_KEY_Left, 0,
969 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
971 add_move_binding (binding_set, GDK_KEY_KP_Right, 0,
972 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
974 add_move_binding (binding_set, GDK_KEY_KP_Left, 0,
975 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
977 add_move_binding (binding_set, GDK_KEY_f, GDK_CONTROL_MASK,
978 GTK_MOVEMENT_LOGICAL_POSITIONS, 1);
980 add_move_binding (binding_set, GDK_KEY_b, GDK_CONTROL_MASK,
981 GTK_MOVEMENT_LOGICAL_POSITIONS, -1);
983 add_move_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK,
984 GTK_MOVEMENT_WORDS, 1);
986 add_move_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK,
987 GTK_MOVEMENT_WORDS, -1);
989 add_move_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK,
990 GTK_MOVEMENT_WORDS, 1);
992 add_move_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK,
993 GTK_MOVEMENT_WORDS, -1);
996 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
998 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1000 G_TYPE_BOOLEAN, FALSE);
1002 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
1004 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1006 G_TYPE_BOOLEAN, TRUE);
1008 gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
1010 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1012 G_TYPE_BOOLEAN, FALSE);
1014 gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
1016 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1018 G_TYPE_BOOLEAN, TRUE);
1021 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1023 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1025 G_TYPE_BOOLEAN, FALSE);
1027 gtk_binding_entry_add_signal (binding_set, GDK_KEY_backslash, GDK_CONTROL_MASK,
1029 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1031 G_TYPE_BOOLEAN, FALSE);
1033 add_move_binding (binding_set, GDK_KEY_f, GDK_MOD1_MASK,
1034 GTK_MOVEMENT_WORDS, 1);
1036 add_move_binding (binding_set, GDK_KEY_b, GDK_MOD1_MASK,
1037 GTK_MOVEMENT_WORDS, -1);
1039 add_move_binding (binding_set, GDK_KEY_Home, 0,
1040 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1042 add_move_binding (binding_set, GDK_KEY_End, 0,
1043 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1045 add_move_binding (binding_set, GDK_KEY_KP_Home, 0,
1046 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1048 add_move_binding (binding_set, GDK_KEY_KP_End, 0,
1049 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1051 add_move_binding (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK,
1052 GTK_MOVEMENT_BUFFER_ENDS, -1);
1054 add_move_binding (binding_set, GDK_KEY_End, GDK_CONTROL_MASK,
1055 GTK_MOVEMENT_BUFFER_ENDS, 1);
1057 add_move_binding (binding_set, GDK_KEY_KP_Home, GDK_CONTROL_MASK,
1058 GTK_MOVEMENT_BUFFER_ENDS, -1);
1060 add_move_binding (binding_set, GDK_KEY_KP_End, GDK_CONTROL_MASK,
1061 GTK_MOVEMENT_BUFFER_ENDS, 1);
1064 gtk_binding_entry_add_signal (binding_set, GDK_KEY_c, GDK_CONTROL_MASK,
1065 "copy-clipboard", 0);
1067 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Return, 0,
1068 "activate-current-link", 0);
1069 gtk_binding_entry_add_signal (binding_set, GDK_KEY_ISO_Enter, 0,
1070 "activate-current-link", 0);
1071 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Enter, 0,
1072 "activate-current-link", 0);
1074 g_type_class_add_private (class, sizeof (GtkLabelPrivate));
1076 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_LABEL_ACCESSIBLE);
1080 gtk_label_set_property (GObject *object,
1082 const GValue *value,
1085 GtkLabel *label = GTK_LABEL (object);
1090 gtk_label_set_label (label, g_value_get_string (value));
1092 case PROP_ATTRIBUTES:
1093 gtk_label_set_attributes (label, g_value_get_boxed (value));
1095 case PROP_USE_MARKUP:
1096 gtk_label_set_use_markup (label, g_value_get_boolean (value));
1098 case PROP_USE_UNDERLINE:
1099 gtk_label_set_use_underline (label, g_value_get_boolean (value));
1102 gtk_label_set_justify (label, g_value_get_enum (value));
1105 gtk_label_set_pattern (label, g_value_get_string (value));
1108 gtk_label_set_line_wrap (label, g_value_get_boolean (value));
1110 case PROP_WRAP_MODE:
1111 gtk_label_set_line_wrap_mode (label, g_value_get_enum (value));
1113 case PROP_SELECTABLE:
1114 gtk_label_set_selectable (label, g_value_get_boolean (value));
1116 case PROP_MNEMONIC_WIDGET:
1117 gtk_label_set_mnemonic_widget (label, (GtkWidget*) g_value_get_object (value));
1119 case PROP_ELLIPSIZE:
1120 gtk_label_set_ellipsize (label, g_value_get_enum (value));
1122 case PROP_WIDTH_CHARS:
1123 gtk_label_set_width_chars (label, g_value_get_int (value));
1125 case PROP_SINGLE_LINE_MODE:
1126 gtk_label_set_single_line_mode (label, g_value_get_boolean (value));
1129 gtk_label_set_angle (label, g_value_get_double (value));
1131 case PROP_MAX_WIDTH_CHARS:
1132 gtk_label_set_max_width_chars (label, g_value_get_int (value));
1134 case PROP_TRACK_VISITED_LINKS:
1135 gtk_label_set_track_visited_links (label, g_value_get_boolean (value));
1138 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1144 gtk_label_get_property (GObject *object,
1149 GtkLabel *label = GTK_LABEL (object);
1150 GtkLabelPrivate *priv = label->priv;
1155 g_value_set_string (value, priv->label);
1157 case PROP_ATTRIBUTES:
1158 g_value_set_boxed (value, priv->attrs);
1160 case PROP_USE_MARKUP:
1161 g_value_set_boolean (value, priv->use_markup);
1163 case PROP_USE_UNDERLINE:
1164 g_value_set_boolean (value, priv->use_underline);
1167 g_value_set_enum (value, priv->jtype);
1170 g_value_set_boolean (value, priv->wrap);
1172 case PROP_WRAP_MODE:
1173 g_value_set_enum (value, priv->wrap_mode);
1175 case PROP_SELECTABLE:
1176 g_value_set_boolean (value, gtk_label_get_selectable (label));
1178 case PROP_MNEMONIC_KEYVAL:
1179 g_value_set_uint (value, priv->mnemonic_keyval);
1181 case PROP_MNEMONIC_WIDGET:
1182 g_value_set_object (value, (GObject*) priv->mnemonic_widget);
1184 case PROP_CURSOR_POSITION:
1185 g_value_set_int (value, _gtk_label_get_cursor_position (label));
1187 case PROP_SELECTION_BOUND:
1188 g_value_set_int (value, _gtk_label_get_selection_bound (label));
1190 case PROP_ELLIPSIZE:
1191 g_value_set_enum (value, priv->ellipsize);
1193 case PROP_WIDTH_CHARS:
1194 g_value_set_int (value, gtk_label_get_width_chars (label));
1196 case PROP_SINGLE_LINE_MODE:
1197 g_value_set_boolean (value, gtk_label_get_single_line_mode (label));
1200 g_value_set_double (value, gtk_label_get_angle (label));
1202 case PROP_MAX_WIDTH_CHARS:
1203 g_value_set_int (value, gtk_label_get_max_width_chars (label));
1205 case PROP_TRACK_VISITED_LINKS:
1206 g_value_set_boolean (value, gtk_label_get_track_visited_links (label));
1209 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1215 gtk_label_init (GtkLabel *label)
1217 GtkLabelPrivate *priv;
1219 label->priv = G_TYPE_INSTANCE_GET_PRIVATE (label,
1224 gtk_widget_set_has_window (GTK_WIDGET (label), FALSE);
1226 priv->width_chars = -1;
1227 priv->max_width_chars = -1;
1230 priv->jtype = GTK_JUSTIFY_LEFT;
1232 priv->wrap_mode = PANGO_WRAP_WORD;
1233 priv->ellipsize = PANGO_ELLIPSIZE_NONE;
1235 priv->use_underline = FALSE;
1236 priv->use_markup = FALSE;
1237 priv->pattern_set = FALSE;
1238 priv->track_links = TRUE;
1240 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
1241 priv->layout = NULL;
1245 priv->mnemonic_widget = NULL;
1246 priv->mnemonic_window = NULL;
1248 priv->mnemonics_visible = TRUE;
1250 gtk_label_set_text (label, "");
1255 gtk_label_buildable_interface_init (GtkBuildableIface *iface)
1257 buildable_parent_iface = g_type_interface_peek_parent (iface);
1259 iface->custom_tag_start = gtk_label_buildable_custom_tag_start;
1260 iface->custom_finished = gtk_label_buildable_custom_finished;
1264 GtkBuilder *builder;
1266 PangoAttrList *attrs;
1269 static PangoAttribute *
1270 attribute_from_text (GtkBuilder *builder,
1275 PangoAttribute *attribute = NULL;
1277 PangoLanguage *language;
1278 PangoFontDescription *font_desc;
1280 GValue val = G_VALUE_INIT;
1282 if (!gtk_builder_value_from_string_type (builder, PANGO_TYPE_ATTR_TYPE, name, &val, error))
1285 type = g_value_get_enum (&val);
1286 g_value_unset (&val);
1290 /* PangoAttrLanguage */
1291 case PANGO_ATTR_LANGUAGE:
1292 if ((language = pango_language_from_string (value)))
1294 attribute = pango_attr_language_new (language);
1295 g_value_init (&val, G_TYPE_INT);
1299 case PANGO_ATTR_STYLE:
1300 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STYLE, value, &val, error))
1301 attribute = pango_attr_style_new (g_value_get_enum (&val));
1303 case PANGO_ATTR_WEIGHT:
1304 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_WEIGHT, value, &val, error))
1305 attribute = pango_attr_weight_new (g_value_get_enum (&val));
1307 case PANGO_ATTR_VARIANT:
1308 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_VARIANT, value, &val, error))
1309 attribute = pango_attr_variant_new (g_value_get_enum (&val));
1311 case PANGO_ATTR_STRETCH:
1312 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STRETCH, value, &val, error))
1313 attribute = pango_attr_stretch_new (g_value_get_enum (&val));
1315 case PANGO_ATTR_UNDERLINE:
1316 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_UNDERLINE, value, &val, NULL))
1317 attribute = pango_attr_underline_new (g_value_get_enum (&val));
1320 /* XXX: allow boolean for backwards compat, so ignore error */
1321 /* Deprecate this somehow */
1322 g_value_unset (&val);
1323 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1324 attribute = pango_attr_underline_new (g_value_get_boolean (&val));
1327 case PANGO_ATTR_STRIKETHROUGH:
1328 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1329 attribute = pango_attr_strikethrough_new (g_value_get_boolean (&val));
1331 case PANGO_ATTR_GRAVITY:
1332 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY, value, &val, error))
1333 attribute = pango_attr_gravity_new (g_value_get_enum (&val));
1335 case PANGO_ATTR_GRAVITY_HINT:
1336 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY_HINT,
1337 value, &val, error))
1338 attribute = pango_attr_gravity_hint_new (g_value_get_enum (&val));
1340 /* PangoAttrString */
1341 case PANGO_ATTR_FAMILY:
1342 attribute = pango_attr_family_new (value);
1343 g_value_init (&val, G_TYPE_INT);
1347 case PANGO_ATTR_SIZE:
1348 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1349 value, &val, error))
1350 attribute = pango_attr_size_new (g_value_get_int (&val));
1352 case PANGO_ATTR_ABSOLUTE_SIZE:
1353 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1354 value, &val, error))
1355 attribute = pango_attr_size_new_absolute (g_value_get_int (&val));
1358 /* PangoAttrFontDesc */
1359 case PANGO_ATTR_FONT_DESC:
1360 if ((font_desc = pango_font_description_from_string (value)))
1362 attribute = pango_attr_font_desc_new (font_desc);
1363 pango_font_description_free (font_desc);
1364 g_value_init (&val, G_TYPE_INT);
1368 /* PangoAttrColor */
1369 case PANGO_ATTR_FOREGROUND:
1370 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1371 value, &val, error))
1373 color = g_value_get_boxed (&val);
1374 attribute = pango_attr_foreground_new (color->red, color->green, color->blue);
1377 case PANGO_ATTR_BACKGROUND:
1378 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1379 value, &val, error))
1381 color = g_value_get_boxed (&val);
1382 attribute = pango_attr_background_new (color->red, color->green, color->blue);
1385 case PANGO_ATTR_UNDERLINE_COLOR:
1386 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1387 value, &val, error))
1389 color = g_value_get_boxed (&val);
1390 attribute = pango_attr_underline_color_new (color->red, color->green, color->blue);
1393 case PANGO_ATTR_STRIKETHROUGH_COLOR:
1394 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1395 value, &val, error))
1397 color = g_value_get_boxed (&val);
1398 attribute = pango_attr_strikethrough_color_new (color->red, color->green, color->blue);
1402 /* PangoAttrShape */
1403 case PANGO_ATTR_SHAPE:
1404 /* Unsupported for now */
1406 /* PangoAttrFloat */
1407 case PANGO_ATTR_SCALE:
1408 if (gtk_builder_value_from_string_type (builder, G_TYPE_DOUBLE,
1409 value, &val, error))
1410 attribute = pango_attr_scale_new (g_value_get_double (&val));
1413 case PANGO_ATTR_INVALID:
1414 case PANGO_ATTR_LETTER_SPACING:
1415 case PANGO_ATTR_RISE:
1416 case PANGO_ATTR_FALLBACK:
1421 g_value_unset (&val);
1428 pango_start_element (GMarkupParseContext *context,
1429 const gchar *element_name,
1430 const gchar **names,
1431 const gchar **values,
1435 PangoParserData *data = (PangoParserData*)user_data;
1436 GValue val = G_VALUE_INIT;
1438 gint line_number, char_number;
1440 if (strcmp (element_name, "attribute") == 0)
1442 PangoAttribute *attr = NULL;
1443 const gchar *name = NULL;
1444 const gchar *value = NULL;
1445 const gchar *start = NULL;
1446 const gchar *end = NULL;
1447 guint start_val = 0;
1448 guint end_val = G_MAXUINT;
1450 for (i = 0; names[i]; i++)
1452 if (strcmp (names[i], "name") == 0)
1454 else if (strcmp (names[i], "value") == 0)
1456 else if (strcmp (names[i], "start") == 0)
1458 else if (strcmp (names[i], "end") == 0)
1462 g_markup_parse_context_get_position (context,
1467 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1468 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1470 line_number, char_number, names[i], "attribute");
1475 if (!name || !value)
1477 g_markup_parse_context_get_position (context,
1482 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1483 "%s:%d:%d <%s> requires attribute \"%s\"",
1485 line_number, char_number, "attribute",
1486 name ? "value" : "name");
1492 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1493 start, &val, error))
1495 start_val = g_value_get_uint (&val);
1496 g_value_unset (&val);
1501 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1504 end_val = g_value_get_uint (&val);
1505 g_value_unset (&val);
1508 attr = attribute_from_text (data->builder, name, value, error);
1512 attr->start_index = start_val;
1513 attr->end_index = end_val;
1516 data->attrs = pango_attr_list_new ();
1518 pango_attr_list_insert (data->attrs, attr);
1521 else if (strcmp (element_name, "attributes") == 0)
1524 g_warning ("Unsupported tag for GtkLabel: %s\n", element_name);
1527 static const GMarkupParser pango_parser =
1529 pango_start_element,
1533 gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
1534 GtkBuilder *builder,
1536 const gchar *tagname,
1537 GMarkupParser *parser,
1540 if (buildable_parent_iface->custom_tag_start (buildable, builder, child,
1541 tagname, parser, data))
1544 if (strcmp (tagname, "attributes") == 0)
1546 PangoParserData *parser_data;
1548 parser_data = g_slice_new0 (PangoParserData);
1549 parser_data->builder = g_object_ref (builder);
1550 parser_data->object = g_object_ref (buildable);
1551 *parser = pango_parser;
1552 *data = parser_data;
1559 gtk_label_buildable_custom_finished (GtkBuildable *buildable,
1560 GtkBuilder *builder,
1562 const gchar *tagname,
1565 PangoParserData *data;
1567 buildable_parent_iface->custom_finished (buildable, builder, child,
1568 tagname, user_data);
1570 if (strcmp (tagname, "attributes") == 0)
1572 data = (PangoParserData*)user_data;
1576 gtk_label_set_attributes (GTK_LABEL (buildable), data->attrs);
1577 pango_attr_list_unref (data->attrs);
1580 g_object_unref (data->object);
1581 g_object_unref (data->builder);
1582 g_slice_free (PangoParserData, data);
1589 * @str: The text of the label
1591 * Creates a new label with the given text inside it. You can
1592 * pass %NULL to get an empty label widget.
1594 * Return value: the new #GtkLabel
1597 gtk_label_new (const gchar *str)
1601 label = g_object_new (GTK_TYPE_LABEL, NULL);
1604 gtk_label_set_text (label, str);
1606 return GTK_WIDGET (label);
1610 * gtk_label_new_with_mnemonic:
1611 * @str: The text of the label, with an underscore in front of the
1612 * mnemonic character
1614 * Creates a new #GtkLabel, containing the text in @str.
1616 * If characters in @str are preceded by an underscore, they are
1617 * underlined. If you need a literal underscore character in a label, use
1618 * '__' (two underscores). The first underlined character represents a
1619 * keyboard accelerator called a mnemonic. The mnemonic key can be used
1620 * to activate another widget, chosen automatically, or explicitly using
1621 * gtk_label_set_mnemonic_widget().
1623 * If gtk_label_set_mnemonic_widget() is not called, then the first
1624 * activatable ancestor of the #GtkLabel will be chosen as the mnemonic
1625 * widget. For instance, if the label is inside a button or menu item,
1626 * the button or menu item will automatically become the mnemonic widget
1627 * and be activated by the mnemonic.
1629 * Return value: the new #GtkLabel
1632 gtk_label_new_with_mnemonic (const gchar *str)
1636 label = g_object_new (GTK_TYPE_LABEL, NULL);
1639 gtk_label_set_text_with_mnemonic (label, str);
1641 return GTK_WIDGET (label);
1645 gtk_label_mnemonic_activate (GtkWidget *widget,
1646 gboolean group_cycling)
1648 GtkLabel *label = GTK_LABEL (widget);
1649 GtkLabelPrivate *priv = label->priv;
1652 if (priv->mnemonic_widget)
1653 return gtk_widget_mnemonic_activate (priv->mnemonic_widget, group_cycling);
1655 /* Try to find the widget to activate by traversing the
1656 * widget's ancestry.
1658 parent = gtk_widget_get_parent (widget);
1660 if (GTK_IS_NOTEBOOK (parent))
1665 if (gtk_widget_get_can_focus (parent) ||
1666 (!group_cycling && GTK_WIDGET_GET_CLASS (parent)->activate_signal) ||
1667 GTK_IS_NOTEBOOK (gtk_widget_get_parent (parent)) ||
1668 GTK_IS_MENU_ITEM (parent))
1669 return gtk_widget_mnemonic_activate (parent, group_cycling);
1670 parent = gtk_widget_get_parent (parent);
1673 /* barf if there was nothing to activate */
1674 g_warning ("Couldn't find a target for a mnemonic activation.");
1675 gtk_widget_error_bell (widget);
1681 gtk_label_setup_mnemonic (GtkLabel *label,
1684 GtkLabelPrivate *priv = label->priv;
1685 GtkWidget *widget = GTK_WIDGET (label);
1686 GtkWidget *toplevel;
1687 GtkWidget *mnemonic_menu;
1689 mnemonic_menu = g_object_get_data (G_OBJECT (label), "gtk-mnemonic-menu");
1691 if (last_key != GDK_KEY_VoidSymbol)
1693 if (priv->mnemonic_window)
1695 gtk_window_remove_mnemonic (priv->mnemonic_window,
1698 priv->mnemonic_window = NULL;
1702 _gtk_menu_shell_remove_mnemonic (GTK_MENU_SHELL (mnemonic_menu),
1705 mnemonic_menu = NULL;
1709 if (priv->mnemonic_keyval == GDK_KEY_VoidSymbol)
1712 connect_mnemonics_visible_notify (GTK_LABEL (widget));
1714 toplevel = gtk_widget_get_toplevel (widget);
1715 if (gtk_widget_is_toplevel (toplevel))
1717 GtkWidget *menu_shell;
1719 menu_shell = gtk_widget_get_ancestor (widget,
1720 GTK_TYPE_MENU_SHELL);
1724 _gtk_menu_shell_add_mnemonic (GTK_MENU_SHELL (menu_shell),
1725 priv->mnemonic_keyval,
1727 mnemonic_menu = menu_shell;
1730 if (!GTK_IS_MENU (menu_shell))
1732 gtk_window_add_mnemonic (GTK_WINDOW (toplevel),
1733 priv->mnemonic_keyval,
1735 priv->mnemonic_window = GTK_WINDOW (toplevel);
1740 g_object_set_data (G_OBJECT (label), I_("gtk-mnemonic-menu"), mnemonic_menu);
1744 gtk_label_hierarchy_changed (GtkWidget *widget,
1745 GtkWidget *old_toplevel)
1747 GtkLabel *label = GTK_LABEL (widget);
1748 GtkLabelPrivate *priv = label->priv;
1750 gtk_label_setup_mnemonic (label, priv->mnemonic_keyval);
1754 label_shortcut_setting_apply (GtkLabel *label)
1756 gtk_label_recalculate (label);
1757 if (GTK_IS_ACCEL_LABEL (label))
1758 gtk_accel_label_refetch (GTK_ACCEL_LABEL (label));
1762 label_shortcut_setting_traverse_container (GtkWidget *widget,
1765 if (GTK_IS_LABEL (widget))
1766 label_shortcut_setting_apply (GTK_LABEL (widget));
1767 else if (GTK_IS_CONTAINER (widget))
1768 gtk_container_forall (GTK_CONTAINER (widget),
1769 label_shortcut_setting_traverse_container, data);
1773 label_shortcut_setting_changed (GtkSettings *settings)
1777 list = gtk_window_list_toplevels ();
1779 for (l = list; l ; l = l->next)
1781 GtkWidget *widget = l->data;
1783 if (gtk_widget_get_settings (widget) == settings)
1784 gtk_container_forall (GTK_CONTAINER (widget),
1785 label_shortcut_setting_traverse_container, NULL);
1792 mnemonics_visible_apply (GtkWidget *widget,
1793 gboolean mnemonics_visible)
1795 GtkLabel *label = GTK_LABEL (widget);
1796 GtkLabelPrivate *priv = label->priv;
1798 mnemonics_visible = mnemonics_visible != FALSE;
1800 if (priv->mnemonics_visible != mnemonics_visible)
1802 priv->mnemonics_visible = mnemonics_visible;
1804 gtk_label_recalculate (label);
1809 label_mnemonics_visible_traverse_container (GtkWidget *widget,
1812 gboolean mnemonics_visible = GPOINTER_TO_INT (data);
1814 _gtk_label_mnemonics_visible_apply_recursively (widget, mnemonics_visible);
1818 _gtk_label_mnemonics_visible_apply_recursively (GtkWidget *widget,
1819 gboolean mnemonics_visible)
1821 if (GTK_IS_LABEL (widget))
1822 mnemonics_visible_apply (widget, mnemonics_visible);
1823 else if (GTK_IS_CONTAINER (widget))
1824 gtk_container_forall (GTK_CONTAINER (widget),
1825 label_mnemonics_visible_traverse_container,
1826 GINT_TO_POINTER (mnemonics_visible));
1830 label_mnemonics_visible_changed (GtkWindow *window,
1834 gboolean mnemonics_visible;
1836 g_object_get (window, "mnemonics-visible", &mnemonics_visible, NULL);
1838 gtk_container_forall (GTK_CONTAINER (window),
1839 label_mnemonics_visible_traverse_container,
1840 GINT_TO_POINTER (mnemonics_visible));
1844 gtk_label_screen_changed (GtkWidget *widget,
1845 GdkScreen *old_screen)
1847 GtkSettings *settings;
1848 gboolean shortcuts_connected;
1850 /* The PangoContext is replaced when the screen changes, so clear the layouts */
1851 gtk_label_clear_layout (GTK_LABEL (widget));
1853 if (!gtk_widget_has_screen (widget))
1856 settings = gtk_widget_get_settings (widget);
1858 shortcuts_connected =
1859 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (settings),
1860 "gtk-label-shortcuts-connected"));
1862 if (! shortcuts_connected)
1864 g_signal_connect (settings, "notify::gtk-enable-mnemonics",
1865 G_CALLBACK (label_shortcut_setting_changed),
1867 g_signal_connect (settings, "notify::gtk-enable-accels",
1868 G_CALLBACK (label_shortcut_setting_changed),
1871 g_object_set_data (G_OBJECT (settings), "gtk-label-shortcuts-connected",
1872 GINT_TO_POINTER (TRUE));
1875 label_shortcut_setting_apply (GTK_LABEL (widget));
1880 label_mnemonic_widget_weak_notify (gpointer data,
1881 GObject *where_the_object_was)
1883 GtkLabel *label = data;
1884 GtkLabelPrivate *priv = label->priv;
1886 priv->mnemonic_widget = NULL;
1887 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1891 * gtk_label_set_mnemonic_widget:
1892 * @label: a #GtkLabel
1893 * @widget: (allow-none): the target #GtkWidget
1895 * If the label has been set so that it has an mnemonic key (using
1896 * i.e. gtk_label_set_markup_with_mnemonic(),
1897 * gtk_label_set_text_with_mnemonic(), gtk_label_new_with_mnemonic()
1898 * or the "use_underline" property) the label can be associated with a
1899 * widget that is the target of the mnemonic. When the label is inside
1900 * a widget (like a #GtkButton or a #GtkNotebook tab) it is
1901 * automatically associated with the correct widget, but sometimes
1902 * (i.e. when the target is a #GtkEntry next to the label) you need to
1903 * set it explicitly using this function.
1905 * The target widget will be accelerated by emitting the
1906 * GtkWidget::mnemonic-activate signal on it. The default handler for
1907 * this signal will activate the widget if there are no mnemonic collisions
1908 * and toggle focus between the colliding widgets otherwise.
1911 gtk_label_set_mnemonic_widget (GtkLabel *label,
1914 GtkLabelPrivate *priv;
1916 g_return_if_fail (GTK_IS_LABEL (label));
1921 g_return_if_fail (GTK_IS_WIDGET (widget));
1923 if (priv->mnemonic_widget)
1925 gtk_widget_remove_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1926 g_object_weak_unref (G_OBJECT (priv->mnemonic_widget),
1927 label_mnemonic_widget_weak_notify,
1930 priv->mnemonic_widget = widget;
1931 if (priv->mnemonic_widget)
1933 g_object_weak_ref (G_OBJECT (priv->mnemonic_widget),
1934 label_mnemonic_widget_weak_notify,
1936 gtk_widget_add_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1939 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1943 * gtk_label_get_mnemonic_widget:
1944 * @label: a #GtkLabel
1946 * Retrieves the target of the mnemonic (keyboard shortcut) of this
1947 * label. See gtk_label_set_mnemonic_widget().
1949 * Return value: (transfer none): the target of the label's mnemonic,
1950 * or %NULL if none has been set and the default algorithm will be used.
1953 gtk_label_get_mnemonic_widget (GtkLabel *label)
1955 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1957 return label->priv->mnemonic_widget;
1961 * gtk_label_get_mnemonic_keyval:
1962 * @label: a #GtkLabel
1964 * If the label has been set so that it has an mnemonic key this function
1965 * returns the keyval used for the mnemonic accelerator. If there is no
1966 * mnemonic set up it returns #GDK_KEY_VoidSymbol.
1968 * Returns: GDK keyval usable for accelerators, or #GDK_KEY_VoidSymbol
1971 gtk_label_get_mnemonic_keyval (GtkLabel *label)
1973 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_KEY_VoidSymbol);
1975 return label->priv->mnemonic_keyval;
1979 gtk_label_set_text_internal (GtkLabel *label,
1982 GtkLabelPrivate *priv = label->priv;
1983 gboolean text_changed;
1985 text_changed = g_strcmp0 (priv->text, str) != 0;
1987 g_free (priv->text);
1991 gtk_label_select_region_index (label, 0, 0);
1995 gtk_label_set_label_internal (GtkLabel *label,
1998 GtkLabelPrivate *priv = label->priv;
2000 g_free (priv->label);
2004 g_object_notify (G_OBJECT (label), "label");
2008 gtk_label_set_use_markup_internal (GtkLabel *label,
2011 GtkLabelPrivate *priv = label->priv;
2014 if (priv->use_markup != val)
2016 priv->use_markup = val;
2018 g_object_notify (G_OBJECT (label), "use-markup");
2023 gtk_label_set_use_underline_internal (GtkLabel *label,
2026 GtkLabelPrivate *priv = label->priv;
2029 if (priv->use_underline != val)
2031 priv->use_underline = val;
2033 g_object_notify (G_OBJECT (label), "use-underline");
2038 my_pango_attr_list_merge_filter (PangoAttribute *attribute,
2041 pango_attr_list_change (list, pango_attribute_copy (attribute));
2046 my_pango_attr_list_merge (PangoAttrList *into,
2047 PangoAttrList *from)
2049 pango_attr_list_filter (from, my_pango_attr_list_merge_filter, into);
2052 /* Calculates text, attrs and mnemonic_keyval from
2053 * label, use_underline and use_markup
2056 gtk_label_recalculate (GtkLabel *label)
2058 GtkLabelPrivate *priv = label->priv;
2059 guint keyval = priv->mnemonic_keyval;
2061 gtk_label_clear_links (label);
2063 if (priv->use_markup)
2064 gtk_label_set_markup_internal (label, priv->label, priv->use_underline);
2065 else if (priv->use_underline)
2066 gtk_label_set_uline_text_internal (label, priv->label);
2069 if (!priv->pattern_set)
2071 if (priv->markup_attrs)
2072 pango_attr_list_unref (priv->markup_attrs);
2073 priv->markup_attrs = NULL;
2075 gtk_label_set_text_internal (label, g_strdup (priv->label));
2078 if (!priv->use_underline)
2079 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
2081 if (keyval != priv->mnemonic_keyval)
2083 gtk_label_setup_mnemonic (label, keyval);
2084 g_object_notify (G_OBJECT (label), "mnemonic-keyval");
2087 gtk_label_clear_layout (label);
2088 gtk_label_clear_select_info (label);
2089 gtk_widget_queue_resize (GTK_WIDGET (label));
2093 * gtk_label_set_text:
2094 * @label: a #GtkLabel
2095 * @str: The text you want to set
2097 * Sets the text within the #GtkLabel widget. It overwrites any text that
2100 * This will also clear any previously set mnemonic accelerators.
2103 gtk_label_set_text (GtkLabel *label,
2106 g_return_if_fail (GTK_IS_LABEL (label));
2108 g_object_freeze_notify (G_OBJECT (label));
2110 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2111 gtk_label_set_use_markup_internal (label, FALSE);
2112 gtk_label_set_use_underline_internal (label, FALSE);
2114 gtk_label_recalculate (label);
2116 g_object_thaw_notify (G_OBJECT (label));
2120 * gtk_label_set_attributes:
2121 * @label: a #GtkLabel
2122 * @attrs: a #PangoAttrList
2124 * Sets a #PangoAttrList; the attributes in the list are applied to the
2127 * <note><para>The attributes set with this function will be applied
2128 * and merged with any other attributes previously effected by way
2129 * of the #GtkLabel:use-underline or #GtkLabel:use-markup properties.
2130 * While it is not recommended to mix markup strings with manually set
2131 * attributes, if you must; know that the attributes will be applied
2132 * to the label after the markup string is parsed.</para></note>
2135 gtk_label_set_attributes (GtkLabel *label,
2136 PangoAttrList *attrs)
2138 GtkLabelPrivate *priv = label->priv;
2140 g_return_if_fail (GTK_IS_LABEL (label));
2143 pango_attr_list_ref (attrs);
2146 pango_attr_list_unref (priv->attrs);
2147 priv->attrs = attrs;
2149 g_object_notify (G_OBJECT (label), "attributes");
2151 gtk_label_clear_layout (label);
2152 gtk_widget_queue_resize (GTK_WIDGET (label));
2156 * gtk_label_get_attributes:
2157 * @label: a #GtkLabel
2159 * Gets the attribute list that was set on the label using
2160 * gtk_label_set_attributes(), if any. This function does
2161 * not reflect attributes that come from the labels markup
2162 * (see gtk_label_set_markup()). If you want to get the
2163 * effective attributes for the label, use
2164 * pango_layout_get_attribute (gtk_label_get_layout (label)).
2166 * Return value: (transfer none): the attribute list, or %NULL
2170 gtk_label_get_attributes (GtkLabel *label)
2172 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2174 return label->priv->attrs;
2178 * gtk_label_set_label:
2179 * @label: a #GtkLabel
2180 * @str: the new text to set for the label
2182 * Sets the text of the label. The label is interpreted as
2183 * including embedded underlines and/or Pango markup depending
2184 * on the values of the #GtkLabel:use-underline" and
2185 * #GtkLabel:use-markup properties.
2188 gtk_label_set_label (GtkLabel *label,
2191 g_return_if_fail (GTK_IS_LABEL (label));
2193 g_object_freeze_notify (G_OBJECT (label));
2195 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2196 gtk_label_recalculate (label);
2198 g_object_thaw_notify (G_OBJECT (label));
2202 * gtk_label_get_label:
2203 * @label: a #GtkLabel
2205 * Fetches the text from a label widget including any embedded
2206 * underlines indicating mnemonics and Pango markup. (See
2207 * gtk_label_get_text()).
2209 * Return value: the text of the label widget. This string is
2210 * owned by the widget and must not be modified or freed.
2213 gtk_label_get_label (GtkLabel *label)
2215 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2217 return label->priv->label;
2229 start_element_handler (GMarkupParseContext *context,
2230 const gchar *element_name,
2231 const gchar **attribute_names,
2232 const gchar **attribute_values,
2236 GtkLabelPrivate *priv;
2237 UriParserData *pdata = user_data;
2239 if (strcmp (element_name, "a") == 0)
2242 const gchar *uri = NULL;
2243 const gchar *title = NULL;
2244 gboolean visited = FALSE;
2249 g_markup_parse_context_get_position (context, &line_number, &char_number);
2251 for (i = 0; attribute_names[i] != NULL; i++)
2253 const gchar *attr = attribute_names[i];
2255 if (strcmp (attr, "href") == 0)
2256 uri = attribute_values[i];
2257 else if (strcmp (attr, "title") == 0)
2258 title = attribute_values[i];
2263 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
2264 "Attribute '%s' is not allowed on the <a> tag "
2265 "on line %d char %d",
2266 attr, line_number, char_number);
2275 G_MARKUP_ERROR_INVALID_CONTENT,
2276 "Attribute 'href' was missing on the <a> tag "
2277 "on line %d char %d",
2278 line_number, char_number);
2283 priv = pdata->label->priv;
2284 if (priv->track_links && priv->select_info)
2287 for (l = priv->select_info->links; l; l = l->next)
2290 if (strcmp (uri, link->uri) == 0)
2292 visited = link->visited;
2298 link = g_new0 (GtkLabelLink, 1);
2299 link->uri = g_strdup (uri);
2300 link->title = g_strdup (title);
2301 link->visited = visited;
2302 link->start = pdata->text_len;
2303 pdata->links = g_list_prepend (pdata->links, link);
2309 g_string_append_c (pdata->new_str, '<');
2310 g_string_append (pdata->new_str, element_name);
2312 for (i = 0; attribute_names[i] != NULL; i++)
2314 const gchar *attr = attribute_names[i];
2315 const gchar *value = attribute_values[i];
2318 newvalue = g_markup_escape_text (value, -1);
2320 g_string_append_c (pdata->new_str, ' ');
2321 g_string_append (pdata->new_str, attr);
2322 g_string_append (pdata->new_str, "=\"");
2323 g_string_append (pdata->new_str, newvalue);
2324 g_string_append_c (pdata->new_str, '\"');
2328 g_string_append_c (pdata->new_str, '>');
2333 end_element_handler (GMarkupParseContext *context,
2334 const gchar *element_name,
2338 UriParserData *pdata = user_data;
2340 if (!strcmp (element_name, "a"))
2342 GtkLabelLink *link = pdata->links->data;
2343 link->end = pdata->text_len;
2347 g_string_append (pdata->new_str, "</");
2348 g_string_append (pdata->new_str, element_name);
2349 g_string_append_c (pdata->new_str, '>');
2354 text_handler (GMarkupParseContext *context,
2360 UriParserData *pdata = user_data;
2363 newtext = g_markup_escape_text (text, text_len);
2364 g_string_append (pdata->new_str, newtext);
2365 pdata->text_len += text_len;
2369 static const GMarkupParser markup_parser =
2371 start_element_handler,
2372 end_element_handler,
2379 xml_isspace (gchar c)
2381 return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
2385 link_free (GtkLabelLink *link)
2388 g_free (link->title);
2393 gtk_label_get_link_colors (GtkWidget *widget,
2394 GdkColor *link_color,
2395 GdkColor *visited_link_color)
2397 GtkStyleContext *context;
2398 GdkColor *link, *visited;
2400 context = gtk_widget_get_style_context (widget);
2401 gtk_style_context_get_style (context,
2402 "link-color", &link,
2403 "visited-link-color", &visited,
2407 *link_color = *link;
2408 gdk_color_free (link);
2411 *link_color = default_link_color;
2415 *visited_link_color = *visited;
2416 gdk_color_free (visited);
2419 *visited_link_color = default_visited_link_color;
2423 parse_uri_markup (GtkLabel *label,
2429 GMarkupParseContext *context = NULL;
2430 const gchar *p, *end;
2431 gboolean needs_root = TRUE;
2433 UriParserData pdata;
2435 length = strlen (str);
2439 pdata.label = label;
2441 pdata.new_str = g_string_sized_new (length);
2444 while (p != end && xml_isspace (*p))
2447 if (end - p >= 8 && strncmp (p, "<markup>", 8) == 0)
2450 context = g_markup_parse_context_new (&markup_parser, 0, &pdata, NULL);
2454 if (!g_markup_parse_context_parse (context, "<markup>", -1, error))
2458 if (!g_markup_parse_context_parse (context, str, length, error))
2463 if (!g_markup_parse_context_parse (context, "</markup>", -1, error))
2467 if (!g_markup_parse_context_end_parse (context, error))
2470 g_markup_parse_context_free (context);
2472 *new_str = g_string_free (pdata.new_str, FALSE);
2473 *links = pdata.links;
2478 g_markup_parse_context_free (context);
2479 g_string_free (pdata.new_str, TRUE);
2480 g_list_free_full (pdata.links, (GDestroyNotify) link_free);
2486 gtk_label_ensure_has_tooltip (GtkLabel *label)
2488 GtkLabelPrivate *priv = label->priv;
2490 gboolean has_tooltip = FALSE;
2492 for (l = priv->select_info->links; l; l = l->next)
2494 GtkLabelLink *link = l->data;
2502 gtk_widget_set_has_tooltip (GTK_WIDGET (label), has_tooltip);
2506 gtk_label_set_markup_internal (GtkLabel *label,
2508 gboolean with_uline)
2510 GtkLabelPrivate *priv = label->priv;
2512 GError *error = NULL;
2513 PangoAttrList *attrs = NULL;
2514 gunichar accel_char = 0;
2516 GList *links = NULL;
2518 if (!parse_uri_markup (label, str, &new_str, &links, &error))
2520 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2522 g_error_free (error);
2528 gtk_label_ensure_select_info (label);
2529 priv->select_info->links = g_list_reverse (links);
2530 gtk_label_ensure_has_tooltip (label);
2535 gboolean enable_mnemonics;
2536 gboolean auto_mnemonics;
2538 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2539 "gtk-enable-mnemonics", &enable_mnemonics,
2540 "gtk-auto-mnemonics", &auto_mnemonics,
2543 if (!(enable_mnemonics && priv->mnemonics_visible &&
2545 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2546 (!priv->mnemonic_widget ||
2547 gtk_widget_is_sensitive (priv->mnemonic_widget))))))
2553 if (separate_uline_pattern (new_str, &key, &tmp, &pattern))
2562 if (!pango_parse_markup (new_str,
2564 with_uline ? '_' : 0,
2567 with_uline ? &accel_char : NULL,
2570 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2573 g_error_free (error);
2580 gtk_label_set_text_internal (label, text);
2584 if (priv->markup_attrs)
2585 pango_attr_list_unref (priv->markup_attrs);
2586 priv->markup_attrs = attrs;
2589 if (accel_char != 0)
2590 priv->mnemonic_keyval = gdk_keyval_to_lower (gdk_unicode_to_keyval (accel_char));
2592 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
2596 * gtk_label_set_markup:
2597 * @label: a #GtkLabel
2598 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
2600 * Parses @str which is marked up with the <link
2601 * linkend="PangoMarkupFormat">Pango text markup language</link>, setting the
2602 * label's text and attribute list based on the parse results. If the @str is
2603 * external data, you may need to escape it with g_markup_escape_text() or
2604 * g_markup_printf_escaped()<!-- -->:
2608 * markup = g_markup_printf_escaped ("<span style=\"italic\">%s</span>", str);
2609 * gtk_label_set_markup (GTK_LABEL (label), markup);
2614 gtk_label_set_markup (GtkLabel *label,
2617 g_return_if_fail (GTK_IS_LABEL (label));
2619 g_object_freeze_notify (G_OBJECT (label));
2621 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2622 gtk_label_set_use_markup_internal (label, TRUE);
2623 gtk_label_set_use_underline_internal (label, FALSE);
2625 gtk_label_recalculate (label);
2627 g_object_thaw_notify (G_OBJECT (label));
2631 * gtk_label_set_markup_with_mnemonic:
2632 * @label: a #GtkLabel
2633 * @str: a markup string (see
2634 * <link linkend="PangoMarkupFormat">Pango markup format</link>)
2636 * Parses @str which is marked up with the
2637 * <link linkend="PangoMarkupFormat">Pango text markup language</link>,
2638 * setting the label's text and attribute list based on the parse results.
2639 * If characters in @str are preceded by an underscore, they are underlined
2640 * indicating that they represent a keyboard accelerator called a mnemonic.
2642 * The mnemonic key can be used to activate another widget, chosen
2643 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
2646 gtk_label_set_markup_with_mnemonic (GtkLabel *label,
2649 g_return_if_fail (GTK_IS_LABEL (label));
2651 g_object_freeze_notify (G_OBJECT (label));
2653 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2654 gtk_label_set_use_markup_internal (label, TRUE);
2655 gtk_label_set_use_underline_internal (label, TRUE);
2657 gtk_label_recalculate (label);
2659 g_object_thaw_notify (G_OBJECT (label));
2663 * gtk_label_get_text:
2664 * @label: a #GtkLabel
2666 * Fetches the text from a label widget, as displayed on the
2667 * screen. This does not include any embedded underlines
2668 * indicating mnemonics or Pango markup. (See gtk_label_get_label())
2670 * Return value: the text in the label widget. This is the internal
2671 * string used by the label, and must not be modified.
2674 gtk_label_get_text (GtkLabel *label)
2676 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2678 return label->priv->text;
2681 static PangoAttrList *
2682 gtk_label_pattern_to_attrs (GtkLabel *label,
2683 const gchar *pattern)
2685 GtkLabelPrivate *priv = label->priv;
2687 const char *p = priv->text;
2688 const char *q = pattern;
2689 PangoAttrList *attrs;
2691 attrs = pango_attr_list_new ();
2695 while (*p && *q && *q != '_')
2697 p = g_utf8_next_char (p);
2701 while (*p && *q && *q == '_')
2703 p = g_utf8_next_char (p);
2709 PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_LOW);
2710 attr->start_index = start - priv->text;
2711 attr->end_index = p - priv->text;
2713 pango_attr_list_insert (attrs, attr);
2723 gtk_label_set_pattern_internal (GtkLabel *label,
2724 const gchar *pattern,
2725 gboolean is_mnemonic)
2727 GtkLabelPrivate *priv = label->priv;
2728 PangoAttrList *attrs;
2729 gboolean enable_mnemonics;
2730 gboolean auto_mnemonics;
2732 if (priv->pattern_set)
2737 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2738 "gtk-enable-mnemonics", &enable_mnemonics,
2739 "gtk-auto-mnemonics", &auto_mnemonics,
2742 if (enable_mnemonics && priv->mnemonics_visible && pattern &&
2744 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2745 (!priv->mnemonic_widget ||
2746 gtk_widget_is_sensitive (priv->mnemonic_widget)))))
2747 attrs = gtk_label_pattern_to_attrs (label, pattern);
2752 attrs = gtk_label_pattern_to_attrs (label, pattern);
2754 if (priv->markup_attrs)
2755 pango_attr_list_unref (priv->markup_attrs);
2756 priv->markup_attrs = attrs;
2760 * gtk_label_set_pattern:
2761 * @label: The #GtkLabel you want to set the pattern to.
2762 * @pattern: The pattern as described above.
2764 * The pattern of underlines you want under the existing text within the
2765 * #GtkLabel widget. For example if the current text of the label says
2766 * "FooBarBaz" passing a pattern of "___ ___" will underline
2767 * "Foo" and "Baz" but not "Bar".
2770 gtk_label_set_pattern (GtkLabel *label,
2771 const gchar *pattern)
2773 GtkLabelPrivate *priv;
2775 g_return_if_fail (GTK_IS_LABEL (label));
2779 priv->pattern_set = FALSE;
2783 gtk_label_set_pattern_internal (label, pattern, FALSE);
2784 priv->pattern_set = TRUE;
2787 gtk_label_recalculate (label);
2789 gtk_label_clear_layout (label);
2790 gtk_widget_queue_resize (GTK_WIDGET (label));
2795 * gtk_label_set_justify:
2796 * @label: a #GtkLabel
2797 * @jtype: a #GtkJustification
2799 * Sets the alignment of the lines in the text of the label relative to
2800 * each other. %GTK_JUSTIFY_LEFT is the default value when the
2801 * widget is first created with gtk_label_new(). If you instead want
2802 * to set the alignment of the label as a whole, use
2803 * gtk_misc_set_alignment() instead. gtk_label_set_justify() has no
2804 * effect on labels containing only a single line.
2807 gtk_label_set_justify (GtkLabel *label,
2808 GtkJustification jtype)
2810 GtkLabelPrivate *priv;
2812 g_return_if_fail (GTK_IS_LABEL (label));
2813 g_return_if_fail (jtype >= GTK_JUSTIFY_LEFT && jtype <= GTK_JUSTIFY_FILL);
2817 if ((GtkJustification) priv->jtype != jtype)
2819 priv->jtype = jtype;
2821 /* No real need to be this drastic, but easier than duplicating the code */
2822 gtk_label_clear_layout (label);
2824 g_object_notify (G_OBJECT (label), "justify");
2825 gtk_widget_queue_resize (GTK_WIDGET (label));
2830 * gtk_label_get_justify:
2831 * @label: a #GtkLabel
2833 * Returns the justification of the label. See gtk_label_set_justify().
2835 * Return value: #GtkJustification
2838 gtk_label_get_justify (GtkLabel *label)
2840 g_return_val_if_fail (GTK_IS_LABEL (label), 0);
2842 return label->priv->jtype;
2846 * gtk_label_set_ellipsize:
2847 * @label: a #GtkLabel
2848 * @mode: a #PangoEllipsizeMode
2850 * Sets the mode used to ellipsize (add an ellipsis: "...") to the text
2851 * if there is not enough space to render the entire string.
2856 gtk_label_set_ellipsize (GtkLabel *label,
2857 PangoEllipsizeMode mode)
2859 GtkLabelPrivate *priv;
2861 g_return_if_fail (GTK_IS_LABEL (label));
2862 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE && mode <= PANGO_ELLIPSIZE_END);
2866 if ((PangoEllipsizeMode) priv->ellipsize != mode)
2868 priv->ellipsize = mode;
2870 /* No real need to be this drastic, but easier than duplicating the code */
2871 gtk_label_clear_layout (label);
2873 g_object_notify (G_OBJECT (label), "ellipsize");
2874 gtk_widget_queue_resize (GTK_WIDGET (label));
2879 * gtk_label_get_ellipsize:
2880 * @label: a #GtkLabel
2882 * Returns the ellipsizing position of the label. See gtk_label_set_ellipsize().
2884 * Return value: #PangoEllipsizeMode
2889 gtk_label_get_ellipsize (GtkLabel *label)
2891 g_return_val_if_fail (GTK_IS_LABEL (label), PANGO_ELLIPSIZE_NONE);
2893 return label->priv->ellipsize;
2897 * gtk_label_set_width_chars:
2898 * @label: a #GtkLabel
2899 * @n_chars: the new desired width, in characters.
2901 * Sets the desired width in characters of @label to @n_chars.
2906 gtk_label_set_width_chars (GtkLabel *label,
2909 GtkLabelPrivate *priv;
2911 g_return_if_fail (GTK_IS_LABEL (label));
2915 if (priv->width_chars != n_chars)
2917 priv->width_chars = n_chars;
2918 g_object_notify (G_OBJECT (label), "width-chars");
2919 gtk_widget_queue_resize (GTK_WIDGET (label));
2924 * gtk_label_get_width_chars:
2925 * @label: a #GtkLabel
2927 * Retrieves the desired width of @label, in characters. See
2928 * gtk_label_set_width_chars().
2930 * Return value: the width of the label in characters.
2935 gtk_label_get_width_chars (GtkLabel *label)
2937 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2939 return label->priv->width_chars;
2943 * gtk_label_set_max_width_chars:
2944 * @label: a #GtkLabel
2945 * @n_chars: the new desired maximum width, in characters.
2947 * Sets the desired maximum width in characters of @label to @n_chars.
2952 gtk_label_set_max_width_chars (GtkLabel *label,
2955 GtkLabelPrivate *priv;
2957 g_return_if_fail (GTK_IS_LABEL (label));
2961 if (priv->max_width_chars != n_chars)
2963 priv->max_width_chars = n_chars;
2965 g_object_notify (G_OBJECT (label), "max-width-chars");
2966 gtk_widget_queue_resize (GTK_WIDGET (label));
2971 * gtk_label_get_max_width_chars:
2972 * @label: a #GtkLabel
2974 * Retrieves the desired maximum width of @label, in characters. See
2975 * gtk_label_set_width_chars().
2977 * Return value: the maximum width of the label in characters.
2982 gtk_label_get_max_width_chars (GtkLabel *label)
2984 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2986 return label->priv->max_width_chars;
2990 * gtk_label_set_line_wrap:
2991 * @label: a #GtkLabel
2992 * @wrap: the setting
2994 * Toggles line wrapping within the #GtkLabel widget. %TRUE makes it break
2995 * lines if text exceeds the widget's size. %FALSE lets the text get cut off
2996 * by the edge of the widget if it exceeds the widget size.
2998 * Note that setting line wrapping to %TRUE does not make the label
2999 * wrap at its parent container's width, because GTK+ widgets
3000 * conceptually can't make their requisition depend on the parent
3001 * container's size. For a label that wraps at a specific position,
3002 * set the label's width using gtk_widget_set_size_request().
3005 gtk_label_set_line_wrap (GtkLabel *label,
3008 GtkLabelPrivate *priv;
3010 g_return_if_fail (GTK_IS_LABEL (label));
3014 wrap = wrap != FALSE;
3016 if (priv->wrap != wrap)
3020 gtk_label_clear_layout (label);
3021 gtk_widget_queue_resize (GTK_WIDGET (label));
3022 g_object_notify (G_OBJECT (label), "wrap");
3027 * gtk_label_get_line_wrap:
3028 * @label: a #GtkLabel
3030 * Returns whether lines in the label are automatically wrapped.
3031 * See gtk_label_set_line_wrap().
3033 * Return value: %TRUE if the lines of the label are automatically wrapped.
3036 gtk_label_get_line_wrap (GtkLabel *label)
3038 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
3040 return label->priv->wrap;
3044 * gtk_label_set_line_wrap_mode:
3045 * @label: a #GtkLabel
3046 * @wrap_mode: the line wrapping mode
3048 * If line wrapping is on (see gtk_label_set_line_wrap()) this controls how
3049 * the line wrapping is done. The default is %PANGO_WRAP_WORD which means
3050 * wrap on word boundaries.
3055 gtk_label_set_line_wrap_mode (GtkLabel *label,
3056 PangoWrapMode wrap_mode)
3058 GtkLabelPrivate *priv;
3060 g_return_if_fail (GTK_IS_LABEL (label));
3064 if (priv->wrap_mode != wrap_mode)
3066 priv->wrap_mode = wrap_mode;
3067 g_object_notify (G_OBJECT (label), "wrap-mode");
3069 gtk_widget_queue_resize (GTK_WIDGET (label));
3074 * gtk_label_get_line_wrap_mode:
3075 * @label: a #GtkLabel
3077 * Returns line wrap mode used by the label. See gtk_label_set_line_wrap_mode().
3079 * Return value: %TRUE if the lines of the label are automatically wrapped.
3084 gtk_label_get_line_wrap_mode (GtkLabel *label)
3086 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
3088 return label->priv->wrap_mode;
3092 gtk_label_destroy (GtkWidget *widget)
3094 GtkLabel *label = GTK_LABEL (widget);
3096 gtk_label_set_mnemonic_widget (label, NULL);
3098 GTK_WIDGET_CLASS (gtk_label_parent_class)->destroy (widget);
3102 gtk_label_finalize (GObject *object)
3104 GtkLabel *label = GTK_LABEL (object);
3105 GtkLabelPrivate *priv = label->priv;
3107 g_free (priv->label);
3108 g_free (priv->text);
3111 g_object_unref (priv->layout);
3114 pango_attr_list_unref (priv->attrs);
3116 if (priv->markup_attrs)
3117 pango_attr_list_unref (priv->markup_attrs);
3119 gtk_label_clear_links (label);
3120 g_free (priv->select_info);
3122 G_OBJECT_CLASS (gtk_label_parent_class)->finalize (object);
3126 gtk_label_clear_layout (GtkLabel *label)
3128 GtkLabelPrivate *priv = label->priv;
3132 g_object_unref (priv->layout);
3133 priv->layout = NULL;
3138 * gtk_label_get_measuring_layout:
3140 * @existing_layout: %NULL or an existing layout already in use.
3141 * @width: the width to measure with in pango units, or -1 for infinite
3143 * Gets a layout that can be used for measuring sizes. The returned
3144 * layout will be identical to the label's layout except for the
3145 * layout's width, which will be set to @width. Do not modify the returned
3148 * Returns: a new reference to a pango layout
3150 static PangoLayout *
3151 gtk_label_get_measuring_layout (GtkLabel * label,
3152 PangoLayout *existing_layout,
3155 GtkLabelPrivate *priv = label->priv;
3156 PangoRectangle rect;
3159 if (existing_layout != NULL)
3161 if (existing_layout != priv->layout)
3163 pango_layout_set_width (existing_layout, width);
3164 return existing_layout;
3167 g_object_unref (existing_layout);
3170 gtk_label_ensure_layout (label);
3172 if (pango_layout_get_width (priv->layout) == width)
3174 g_object_ref (priv->layout);
3175 return priv->layout;
3178 /* We can use the label's own layout if we're not allocated a size yet,
3179 * because we don't need it to be properly setup at that point.
3180 * This way we can make use of caching upon the label's creation.
3182 if (gtk_widget_get_allocated_width (GTK_WIDGET (label)) <= 1)
3184 g_object_ref (priv->layout);
3185 pango_layout_set_width (priv->layout, width);
3186 return priv->layout;
3189 /* oftentimes we want to measure a width that is far wider than the current width,
3190 * even though the layout would not change if we made it wider. In that case, we
3191 * can just return the current layout, because for measuring purposes, it will be
3194 pango_layout_get_extents (priv->layout, NULL, &rect);
3195 if ((width == -1 || rect.width <= width) &&
3196 !pango_layout_is_wrapped (priv->layout) &&
3197 !pango_layout_is_ellipsized (priv->layout))
3199 g_object_ref (priv->layout);
3200 return priv->layout;
3203 copy = pango_layout_copy (priv->layout);
3204 pango_layout_set_width (copy, width);
3209 gtk_label_update_layout_width (GtkLabel *label)
3211 GtkLabelPrivate *priv = label->priv;
3212 GtkWidget *widget = GTK_WIDGET (label);
3214 g_assert (priv->layout);
3216 if (priv->ellipsize || priv->wrap)
3219 PangoRectangle logical;
3222 _gtk_misc_get_padding_and_border (GTK_MISC (label), &border);
3224 width = gtk_widget_get_allocated_width (GTK_WIDGET (label)) - border.left - border.right;
3225 height = gtk_widget_get_allocated_height (GTK_WIDGET (label)) - border.top - border.bottom;
3227 if (priv->have_transform)
3229 PangoContext *context = gtk_widget_get_pango_context (widget);
3230 const PangoMatrix *matrix = pango_context_get_matrix (context);
3231 const gdouble dx = matrix->xx; /* cos (M_PI * angle / 180) */
3232 const gdouble dy = matrix->xy; /* sin (M_PI * angle / 180) */
3234 pango_layout_set_width (priv->layout, -1);
3235 pango_layout_get_pixel_extents (priv->layout, NULL, &logical);
3237 if (fabs (dy) < 0.01)
3239 if (logical.width > width)
3240 pango_layout_set_width (priv->layout, width * PANGO_SCALE);
3242 else if (fabs (dx) < 0.01)
3244 if (logical.width > height)
3245 pango_layout_set_width (priv->layout, height * PANGO_SCALE);
3249 gdouble x0, y0, x1, y1, length;
3254 y0 = dx ? x0 * dy / dx : G_MAXDOUBLE;
3255 vertical = fabs (y0) > height / 2;
3260 x0 = dy ? y0 * dx / dy : G_MAXDOUBLE;
3263 length = 2 * sqrt (x0 * x0 + y0 * y0);
3264 pango_layout_set_width (priv->layout, rint (length * PANGO_SCALE));
3265 pango_layout_get_pixel_size (priv->layout, NULL, &cy);
3272 y0 = height/2 + y1 - y0;
3277 x0 = width/2 + x1 - x0;
3281 length = length - sqrt (x0 * x0 + y0 * y0) * 2;
3282 pango_layout_set_width (priv->layout, rint (length * PANGO_SCALE));
3287 pango_layout_set_width (priv->layout, width * PANGO_SCALE);
3292 pango_layout_set_width (priv->layout, -1);
3297 gtk_label_ensure_layout (GtkLabel *label)
3299 GtkLabelPrivate *priv = label->priv;
3303 widget = GTK_WIDGET (label);
3305 rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
3309 PangoAlignment align = PANGO_ALIGN_LEFT; /* Quiet gcc */
3310 PangoAttrList *attrs;
3311 gdouble angle = gtk_label_get_angle (label);
3313 if (angle != 0.0 && !priv->select_info)
3315 PangoMatrix matrix = PANGO_MATRIX_INIT;
3317 /* We rotate the standard singleton PangoContext for the widget,
3318 * depending on the fact that it's meant pretty much exclusively
3321 pango_matrix_rotate (&matrix, angle);
3323 pango_context_set_matrix (gtk_widget_get_pango_context (widget), &matrix);
3325 priv->have_transform = TRUE;
3329 if (priv->have_transform)
3330 pango_context_set_matrix (gtk_widget_get_pango_context (widget), NULL);
3332 priv->have_transform = FALSE;
3335 priv->layout = gtk_widget_create_pango_layout (widget, priv->text);
3337 if (priv->select_info && priv->select_info->links)
3339 GdkColor link_color, visited_color;
3340 PangoAttribute *attribute;
3343 gtk_label_get_link_colors (widget, &link_color, &visited_color);
3344 attrs = pango_attr_list_new ();
3346 for (list = priv->select_info->links; list; list = list->next)
3348 GtkLabelLink *link = list->data;
3350 attribute = pango_attr_underline_new (TRUE);
3351 attribute->start_index = link->start;
3352 attribute->end_index = link->end;
3353 pango_attr_list_insert (attrs, attribute);
3356 attribute = pango_attr_foreground_new (visited_color.red,
3357 visited_color.green,
3358 visited_color.blue);
3360 attribute = pango_attr_foreground_new (link_color.red,
3363 attribute->start_index = link->start;
3364 attribute->end_index = link->end;
3365 pango_attr_list_insert (attrs, attribute);
3368 else if (priv->markup_attrs || priv->attrs)
3369 attrs = pango_attr_list_new ();
3373 if (priv->markup_attrs)
3376 my_pango_attr_list_merge (attrs, priv->markup_attrs);
3378 attrs = pango_attr_list_ref (priv->markup_attrs);
3384 my_pango_attr_list_merge (attrs, priv->attrs);
3386 attrs = pango_attr_list_ref (priv->attrs);
3391 pango_layout_set_attributes (priv->layout, attrs);
3392 pango_attr_list_unref (attrs);
3395 switch (priv->jtype)
3397 case GTK_JUSTIFY_LEFT:
3398 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3400 case GTK_JUSTIFY_RIGHT:
3401 align = rtl ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
3403 case GTK_JUSTIFY_CENTER:
3404 align = PANGO_ALIGN_CENTER;
3406 case GTK_JUSTIFY_FILL:
3407 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3408 pango_layout_set_justify (priv->layout, TRUE);
3411 g_assert_not_reached();
3414 pango_layout_set_alignment (priv->layout, align);
3415 pango_layout_set_ellipsize (priv->layout, priv->ellipsize);
3416 pango_layout_set_wrap (priv->layout, priv->wrap_mode);
3417 pango_layout_set_single_paragraph_mode (priv->layout, priv->single_line_mode);
3419 gtk_label_update_layout_width (label);
3423 static GtkSizeRequestMode
3424 gtk_label_get_request_mode (GtkWidget *widget)
3426 GtkLabel *label = GTK_LABEL (widget);
3427 gdouble angle = gtk_label_get_angle (label);
3429 if (label->priv->wrap)
3430 return (angle == 90 || angle == 270) ?
3431 GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT :
3432 GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
3434 return GTK_SIZE_REQUEST_CONSTANT_SIZE;
3438 get_size_for_allocation (GtkLabel *label,
3439 GtkOrientation orientation,
3444 PangoLayout *layout;
3447 layout = gtk_label_get_measuring_layout (label, NULL, allocation * PANGO_SCALE);
3449 pango_layout_get_pixel_size (layout, NULL, &text_height);
3452 *minimum_size = text_height;
3455 *natural_size = text_height;
3457 g_object_unref (layout);
3461 get_char_pixels (GtkWidget *label,
3462 PangoLayout *layout)
3464 PangoContext *context;
3465 PangoFontMetrics *metrics;
3466 gint char_width, digit_width;
3468 context = pango_layout_get_context (layout);
3469 metrics = pango_context_get_metrics (context,
3470 pango_context_get_font_description (context),
3471 pango_context_get_language (context));
3472 char_width = pango_font_metrics_get_approximate_char_width (metrics);
3473 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
3474 pango_font_metrics_unref (metrics);
3476 return MAX (char_width, digit_width);;
3480 gtk_label_get_preferred_layout_size (GtkLabel *label,
3481 PangoRectangle *smallest,
3482 PangoRectangle *widest)
3484 GtkLabelPrivate *priv = label->priv;
3485 PangoLayout *layout;
3488 /* "width-chars" Hard-coded minimum width:
3489 * - minimum size should be MAX (width-chars, strlen ("..."));
3490 * - natural size should be MAX (width-chars, strlen (priv->text));
3492 * "max-width-chars" User specified maximum size requisition
3493 * - minimum size should be MAX (width-chars, 0)
3494 * - natural size should be MIN (max-width-chars, strlen (priv->text))
3496 * For ellipsizing labels; if max-width-chars is specified: either it is used as
3497 * a minimum size or the label text as a minimum size (natural size still overflows).
3499 * For wrapping labels; A reasonable minimum size is useful to naturally layout
3500 * interfaces automatically. In this case if no "width-chars" is specified, the minimum
3501 * width will default to the wrap guess that gtk_label_ensure_layout() does.
3504 /* Start off with the pixel extents of an as-wide-as-possible layout */
3505 layout = gtk_label_get_measuring_layout (label, NULL, -1);
3507 if (priv->width_chars > -1 || priv->max_width_chars > -1)
3508 char_pixels = get_char_pixels (GTK_WIDGET (label), layout);
3512 pango_layout_get_extents (layout, NULL, widest);
3513 widest->width = MAX (widest->width, char_pixels * priv->width_chars);
3514 widest->x = widest->y = 0;
3516 if (priv->ellipsize || priv->wrap)
3518 /* a layout with width 0 will be as small as humanly possible */
3519 layout = gtk_label_get_measuring_layout (label,
3521 priv->width_chars > -1 ? char_pixels * priv->width_chars
3524 pango_layout_get_extents (layout, NULL, smallest);
3525 smallest->width = MAX (smallest->width, char_pixels * priv->width_chars);
3526 smallest->x = smallest->y = 0;
3528 if (priv->max_width_chars > -1 && widest->width > char_pixels * priv->max_width_chars)
3530 layout = gtk_label_get_measuring_layout (label,
3532 MAX (smallest->width, char_pixels * priv->max_width_chars));
3533 pango_layout_get_extents (layout, NULL, widest);
3534 widest->width = MAX (widest->width, char_pixels * priv->width_chars);
3535 widest->x = widest->y = 0;
3540 *smallest = *widest;
3543 if (widest->width < smallest->width)
3544 *smallest = *widest;
3546 g_object_unref (layout);
3550 gtk_label_get_preferred_size (GtkWidget *widget,
3551 GtkOrientation orientation,
3555 GtkLabel *label = GTK_LABEL (widget);
3556 GtkLabelPrivate *priv = label->priv;
3557 PangoRectangle widest_rect;
3558 PangoRectangle smallest_rect;
3561 gtk_label_get_preferred_layout_size (label, &smallest_rect, &widest_rect);
3563 /* Now that we have minimum and natural sizes in pango extents, apply a possible transform */
3564 if (priv->have_transform)
3566 PangoContext *context;
3567 const PangoMatrix *matrix;
3569 context = pango_layout_get_context (priv->layout);
3570 matrix = pango_context_get_matrix (context);
3572 pango_matrix_transform_rectangle (matrix, &widest_rect);
3573 pango_matrix_transform_rectangle (matrix, &smallest_rect);
3575 /* Bump the size in case of ellipsize to ensure pango has
3576 * enough space in the angles (note, we could alternatively set the
3577 * layout to not ellipsize when we know we have been allocated our
3578 * full size, or it may be that pango needs a fix here).
3580 if (priv->ellipsize && priv->angle != 0 && priv->angle != 90 &&
3581 priv->angle != 180 && priv->angle != 270 && priv->angle != 360)
3583 /* For some reason we only need this at about 110 degrees, and only
3584 * when gaining in height
3586 widest_rect.height += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3587 widest_rect.width += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3588 smallest_rect.height += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3589 smallest_rect.width += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3593 widest_rect.width = PANGO_PIXELS_CEIL (widest_rect.width);
3594 widest_rect.height = PANGO_PIXELS_CEIL (widest_rect.height);
3596 smallest_rect.width = PANGO_PIXELS_CEIL (smallest_rect.width);
3597 smallest_rect.height = PANGO_PIXELS_CEIL (smallest_rect.height);
3599 _gtk_misc_get_padding_and_border (GTK_MISC (label), &border);
3601 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3603 /* Note, we cant use get_size_for_allocation() when rotating
3604 * ellipsized labels.
3606 if (!(priv->ellipsize && priv->have_transform) &&
3607 (priv->angle == 90 || priv->angle == 270))
3609 /* Doing a h4w request on a rotated label here, return the
3610 * required width for the minimum height.
3612 get_size_for_allocation (label,
3613 GTK_ORIENTATION_VERTICAL,
3614 smallest_rect.height,
3615 minimum_size, natural_size);
3620 /* Normal desired width */
3621 *minimum_size = smallest_rect.width;
3622 *natural_size = widest_rect.width;
3625 *minimum_size += border.left + border.right;
3626 *natural_size += border.left + border.right;
3628 else /* GTK_ORIENTATION_VERTICAL */
3630 /* Note, we cant use get_size_for_allocation() when rotating
3631 * ellipsized labels.
3633 if (!(priv->ellipsize && priv->have_transform) &&
3634 (priv->angle == 0 || priv->angle == 180 || priv->angle == 360))
3636 /* Doing a w4h request on a label here, return the required
3637 * height for the minimum width.
3639 get_size_for_allocation (label,
3640 GTK_ORIENTATION_HORIZONTAL,
3642 minimum_size, natural_size);
3646 /* A vertically rotated label does w4h, so return the base
3647 * desired height (text length)
3649 *minimum_size = MIN (smallest_rect.height, widest_rect.height);
3650 *natural_size = MAX (smallest_rect.height, widest_rect.height);
3653 *minimum_size += border.top + border.bottom;
3654 *natural_size += border.top + border.bottom;
3659 gtk_label_get_preferred_width (GtkWidget *widget,
3663 gtk_label_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
3667 gtk_label_get_preferred_height (GtkWidget *widget,
3671 gtk_label_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
3675 gtk_label_get_preferred_width_for_height (GtkWidget *widget,
3677 gint *minimum_width,
3678 gint *natural_width)
3680 GtkLabel *label = GTK_LABEL (widget);
3681 GtkLabelPrivate *priv = label->priv;
3683 if (priv->wrap && (priv->angle == 90 || priv->angle == 270))
3687 _gtk_misc_get_padding_and_border (GTK_MISC (label), &border);
3690 gtk_label_clear_layout (label);
3692 get_size_for_allocation (label, GTK_ORIENTATION_VERTICAL,
3693 MAX (1, height - border.top - border.bottom),
3694 minimum_width, natural_width);
3697 *minimum_width += border.right + border.left;
3700 *natural_width += border.right + border.left;
3703 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_width, natural_width);
3707 gtk_label_get_preferred_height_for_width (GtkWidget *widget,
3709 gint *minimum_height,
3710 gint *natural_height)
3712 GtkLabel *label = GTK_LABEL (widget);
3713 GtkLabelPrivate *priv = label->priv;
3715 if (priv->wrap && (priv->angle == 0 || priv->angle == 180 || priv->angle == 360))
3719 _gtk_misc_get_padding_and_border (GTK_MISC (label), &border);
3722 gtk_label_clear_layout (label);
3724 get_size_for_allocation (label, GTK_ORIENTATION_HORIZONTAL,
3725 MAX (1, width - border.left - border.right),
3726 minimum_height, natural_height);
3729 *minimum_height += border.top + border.bottom;
3732 *natural_height += border.top + border.bottom;
3735 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, minimum_height, natural_height);
3739 gtk_label_size_allocate (GtkWidget *widget,
3740 GtkAllocation *allocation)
3742 GtkLabel *label = GTK_LABEL (widget);
3743 GtkLabelPrivate *priv = label->priv;
3745 GTK_WIDGET_CLASS (gtk_label_parent_class)->size_allocate (widget, allocation);
3748 gtk_label_update_layout_width (label);
3750 if (priv->select_info && priv->select_info->window)
3752 gdk_window_move_resize (priv->select_info->window,
3756 allocation->height);
3761 gtk_label_update_cursor (GtkLabel *label)
3763 GtkLabelPrivate *priv = label->priv;
3766 if (!priv->select_info)
3769 widget = GTK_WIDGET (label);
3771 if (gtk_widget_get_realized (widget))
3773 GdkDisplay *display;
3776 if (gtk_widget_is_sensitive (widget))
3778 display = gtk_widget_get_display (widget);
3780 if (priv->select_info->active_link)
3781 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
3782 else if (priv->select_info->selectable)
3783 cursor = gdk_cursor_new_for_display (display, GDK_XTERM);
3790 gdk_window_set_cursor (priv->select_info->window, cursor);
3793 g_object_unref (cursor);
3798 gtk_label_state_flags_changed (GtkWidget *widget,
3799 GtkStateFlags prev_state)
3801 GtkLabel *label = GTK_LABEL (widget);
3802 GtkLabelPrivate *priv = label->priv;
3804 if (priv->select_info)
3806 if (!gtk_widget_is_sensitive (widget))
3807 gtk_label_select_region (label, 0, 0);
3809 gtk_label_update_cursor (label);
3812 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->state_flags_changed)
3813 GTK_WIDGET_CLASS (gtk_label_parent_class)->state_flags_changed (widget, prev_state);
3817 get_layout_location (GtkLabel *label,
3821 GtkAllocation allocation;
3824 GtkLabelPrivate *priv;
3826 gint req_width, x, y;
3828 gfloat xalign, yalign;
3829 PangoRectangle logical;
3831 misc = GTK_MISC (label);
3832 widget = GTK_WIDGET (label);
3835 gtk_misc_get_alignment (misc, &xalign, &yalign);
3836 _gtk_misc_get_padding_and_border (GTK_MISC (label), &border);
3838 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
3839 xalign = 1.0 - xalign;
3841 pango_layout_get_extents (priv->layout, NULL, &logical);
3843 if (priv->have_transform)
3845 PangoContext *context = gtk_widget_get_pango_context (widget);
3846 const PangoMatrix *matrix = pango_context_get_matrix (context);
3847 pango_matrix_transform_rectangle (matrix, &logical);
3850 pango_extents_to_pixels (&logical, NULL);
3852 req_width = logical.width;
3853 req_height = logical.height;
3855 req_width += border.left + border.right;
3856 req_height += border.top + border.bottom;
3858 gtk_widget_get_allocation (widget, &allocation);
3860 x = floor (allocation.x + border.left + xalign * (allocation.width - req_width) - logical.x);
3862 /* bgo#315462 - For single-line labels, *do* align the requisition with
3863 * respect to the allocation, even if we are under-allocated. For multi-line
3864 * labels, always show the top of the text when they are under-allocated. The
3865 * rationale is this:
3867 * - Single-line labels appear in GtkButtons, and it is very easy to get them
3868 * to be smaller than their requisition. The button may clip the label, but
3869 * the label will still be able to show most of itself and the focus
3870 * rectangle. Also, it is fairly easy to read a single line of clipped text.
3872 * - Multi-line labels should not be clipped to showing "something in the
3873 * middle". You want to read the first line, at least, to get some context.
3875 if (pango_layout_get_line_count (priv->layout) == 1)
3876 y = floor (allocation.y + border.top + (allocation.height - req_height) * yalign) - logical.y;
3878 y = floor (allocation.y + border.top + MAX ((allocation.height - req_height) * yalign, 0)) - logical.y;
3887 static PangoDirection
3888 get_cursor_direction (GtkLabel *label)
3890 GtkLabelPrivate *priv = label->priv;
3893 g_assert (priv->select_info);
3895 gtk_label_ensure_layout (label);
3897 for (l = pango_layout_get_lines_readonly (priv->layout); l; l = l->next)
3899 PangoLayoutLine *line = l->data;
3901 /* If priv->select_info->selection_end is at the very end of
3902 * the line, we don't know if the cursor is on this line or
3903 * the next without looking ahead at the next line. (End
3904 * of paragraph is different from line break.) But it's
3905 * definitely in this paragraph, which is good enough
3906 * to figure out the resolved direction.
3908 if (line->start_index + line->length >= priv->select_info->selection_end)
3909 return line->resolved_dir;
3912 return PANGO_DIRECTION_LTR;
3915 static GtkLabelLink *
3916 gtk_label_get_focus_link (GtkLabel *label)
3918 GtkLabelPrivate *priv = label->priv;
3919 GtkLabelSelectionInfo *info = priv->select_info;
3925 if (info->selection_anchor != info->selection_end)
3928 for (l = info->links; l; l = l->next)
3930 GtkLabelLink *link = l->data;
3931 if (link->start <= info->selection_anchor &&
3932 info->selection_anchor <= link->end)
3940 gtk_label_draw (GtkWidget *widget,
3943 GtkLabel *label = GTK_LABEL (widget);
3944 GtkLabelPrivate *priv = label->priv;
3945 GtkLabelSelectionInfo *info = priv->select_info;
3946 GtkAllocation allocation;
3947 GtkStyleContext *context;
3948 GtkStateFlags state;
3951 gtk_label_ensure_layout (label);
3953 context = gtk_widget_get_style_context (widget);
3954 gtk_widget_get_allocation (widget, &allocation);
3956 gtk_render_background (context, cr,
3958 allocation.width, allocation.height);
3959 gtk_render_frame (context, cr,
3961 allocation.width, allocation.height);
3963 if (priv->text && (*priv->text != '\0'))
3965 get_layout_location (label, &x, &y);
3967 cairo_translate (cr, -allocation.x, -allocation.y);
3969 gtk_render_layout (context, cr,
3973 state = gtk_widget_get_state_flags (widget);
3976 (info->selection_anchor != info->selection_end))
3979 cairo_region_t *clip;
3980 GdkRGBA bg_color, fg_color;
3982 range[0] = info->selection_anchor;
3983 range[1] = info->selection_end;
3985 if (range[0] > range[1])
3987 gint tmp = range[0];
3988 range[0] = range[1];
3992 clip = gdk_pango_layout_get_clip_region (priv->layout,
3997 /* FIXME should use gtk_paint, but it can't use a clip region */
4000 gdk_cairo_region (cr, clip);
4003 state |= GTK_STATE_FLAG_SELECTED;
4005 gtk_style_context_get_color (context, state, &fg_color);
4006 gtk_style_context_get_background_color (context, state, &bg_color);
4008 gdk_cairo_set_source_rgba (cr, &bg_color);
4011 gdk_cairo_set_source_rgba (cr, &fg_color);
4012 cairo_move_to (cr, x, y);
4013 _gtk_pango_fill_layout (cr, priv->layout);
4016 cairo_region_destroy (clip);
4020 GtkLabelLink *focus_link;
4021 GtkLabelLink *active_link;
4023 cairo_region_t *clip;
4025 GdkColor *text_color;
4026 GdkColor link_color;
4027 GdkColor visited_link_color;
4029 if (info->selectable &&
4030 gtk_widget_has_focus (widget) &&
4031 gtk_widget_is_drawable (widget))
4033 PangoDirection cursor_direction;
4035 cursor_direction = get_cursor_direction (label);
4036 gtk_render_insertion_cursor (context, cr,
4038 priv->layout, priv->select_info->selection_end,
4042 focus_link = gtk_label_get_focus_link (label);
4043 active_link = info->active_link;
4049 range[0] = active_link->start;
4050 range[1] = active_link->end;
4054 clip = gdk_pango_layout_get_clip_region (priv->layout,
4058 gdk_cairo_region (cr, clip);
4060 cairo_region_destroy (clip);
4062 gtk_label_get_link_colors (widget, &link_color, &visited_link_color);
4063 if (active_link->visited)
4064 text_color = &visited_link_color;
4066 text_color = &link_color;
4068 if (info->link_clicked)
4069 state |= GTK_STATE_FLAG_ACTIVE;
4071 state |= GTK_STATE_FLAG_PRELIGHT;
4073 gtk_style_context_get_background_color (context, state, &bg_color);
4075 gdk_cairo_set_source_rgba (cr, &bg_color);
4078 cairo_set_source_rgb (cr, text_color->red / 65535.,
4079 text_color->green / 65535.,
4080 text_color->blue / 65535.);
4081 cairo_move_to (cr, x, y);
4082 _gtk_pango_fill_layout (cr, priv->layout);
4087 if (focus_link && gtk_widget_has_visible_focus (widget))
4089 range[0] = focus_link->start;
4090 range[1] = focus_link->end;
4092 clip = gdk_pango_layout_get_clip_region (priv->layout,
4096 cairo_region_get_extents (clip, &rect);
4098 gtk_render_focus (context, cr,
4100 rect.width, rect.height);
4102 cairo_region_destroy (clip);
4111 separate_uline_pattern (const gchar *str,
4116 gboolean underscore;
4119 gchar *pattern_dest;
4121 *accel_key = GDK_KEY_VoidSymbol;
4122 *new_str = g_new (gchar, strlen (str) + 1);
4123 *pattern = g_new (gchar, g_utf8_strlen (str, -1) + 1);
4129 pattern_dest = *pattern;
4134 const gchar *next_src;
4136 c = g_utf8_get_char (src);
4137 if (c == (gunichar)-1)
4139 g_warning ("Invalid input string");
4145 next_src = g_utf8_next_char (src);
4150 *pattern_dest++ = ' ';
4153 *pattern_dest++ = '_';
4154 if (*accel_key == GDK_KEY_VoidSymbol)
4155 *accel_key = gdk_keyval_to_lower (gdk_unicode_to_keyval (c));
4158 while (src < next_src)
4172 while (src < next_src)
4175 *pattern_dest++ = ' ';
4187 gtk_label_set_uline_text_internal (GtkLabel *label,
4190 GtkLabelPrivate *priv = label->priv;
4191 guint accel_key = GDK_KEY_VoidSymbol;
4195 g_return_if_fail (GTK_IS_LABEL (label));
4196 g_return_if_fail (str != NULL);
4198 /* Split text into the base text and a separate pattern
4201 if (!separate_uline_pattern (str, &accel_key, &new_str, &pattern))
4204 gtk_label_set_text_internal (label, new_str);
4205 gtk_label_set_pattern_internal (label, pattern, TRUE);
4206 priv->mnemonic_keyval = accel_key;
4212 * gtk_label_set_text_with_mnemonic:
4213 * @label: a #GtkLabel
4216 * Sets the label's text from the string @str.
4217 * If characters in @str are preceded by an underscore, they are underlined
4218 * indicating that they represent a keyboard accelerator called a mnemonic.
4219 * The mnemonic key can be used to activate another widget, chosen
4220 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
4223 gtk_label_set_text_with_mnemonic (GtkLabel *label,
4226 g_return_if_fail (GTK_IS_LABEL (label));
4227 g_return_if_fail (str != NULL);
4229 g_object_freeze_notify (G_OBJECT (label));
4231 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
4232 gtk_label_set_use_markup_internal (label, FALSE);
4233 gtk_label_set_use_underline_internal (label, TRUE);
4235 gtk_label_recalculate (label);
4237 g_object_thaw_notify (G_OBJECT (label));
4241 gtk_label_realize (GtkWidget *widget)
4243 GtkLabel *label = GTK_LABEL (widget);
4244 GtkLabelPrivate *priv = label->priv;
4246 GTK_WIDGET_CLASS (gtk_label_parent_class)->realize (widget);
4248 if (priv->select_info)
4249 gtk_label_create_window (label);
4253 gtk_label_unrealize (GtkWidget *widget)
4255 GtkLabel *label = GTK_LABEL (widget);
4256 GtkLabelPrivate *priv = label->priv;
4258 if (priv->select_info)
4259 gtk_label_destroy_window (label);
4261 GTK_WIDGET_CLASS (gtk_label_parent_class)->unrealize (widget);
4265 gtk_label_map (GtkWidget *widget)
4267 GtkLabel *label = GTK_LABEL (widget);
4268 GtkLabelPrivate *priv = label->priv;
4270 GTK_WIDGET_CLASS (gtk_label_parent_class)->map (widget);
4272 if (priv->select_info)
4273 gdk_window_show (priv->select_info->window);
4277 gtk_label_unmap (GtkWidget *widget)
4279 GtkLabel *label = GTK_LABEL (widget);
4280 GtkLabelPrivate *priv = label->priv;
4282 if (priv->select_info)
4283 gdk_window_hide (priv->select_info->window);
4285 GTK_WIDGET_CLASS (gtk_label_parent_class)->unmap (widget);
4289 window_to_layout_coords (GtkLabel *label,
4293 GtkAllocation allocation;
4297 widget = GTK_WIDGET (label);
4299 /* get layout location in widget->window coords */
4300 get_layout_location (label, &lx, &ly);
4302 gtk_widget_get_allocation (widget, &allocation);
4306 *x += allocation.x; /* go to widget->window */
4307 *x -= lx; /* go to layout */
4312 *y += allocation.y; /* go to widget->window */
4313 *y -= ly; /* go to layout */
4319 layout_to_window_coords (GtkLabel *label,
4326 widget = GTK_WIDGET (label);
4328 /* get layout location in widget->window coords */
4329 get_layout_location (label, &lx, &ly);
4333 *x += lx; /* go to widget->window */
4334 *x -= widget->allocation.x; /* go to selection window */
4339 *y += ly; /* go to widget->window */
4340 *y -= widget->allocation.y; /* go to selection window */
4346 get_layout_index (GtkLabel *label,
4351 GtkLabelPrivate *priv = label->priv;
4353 const gchar *cluster;
4354 const gchar *cluster_end;
4359 gtk_label_ensure_layout (label);
4361 window_to_layout_coords (label, &x, &y);
4366 inside = pango_layout_xy_to_index (priv->layout,
4370 cluster = priv->text + *index;
4371 cluster_end = cluster;
4374 cluster_end = g_utf8_next_char (cluster_end);
4378 *index += (cluster_end - cluster);
4384 gtk_label_select_word (GtkLabel *label)
4386 GtkLabelPrivate *priv = label->priv;
4389 gint start_index = gtk_label_move_backward_word (label, priv->select_info->selection_end);
4390 gint end_index = gtk_label_move_forward_word (label, priv->select_info->selection_end);
4392 min = MIN (priv->select_info->selection_anchor,
4393 priv->select_info->selection_end);
4394 max = MAX (priv->select_info->selection_anchor,
4395 priv->select_info->selection_end);
4397 min = MIN (min, start_index);
4398 max = MAX (max, end_index);
4400 gtk_label_select_region_index (label, min, max);
4404 gtk_label_grab_focus (GtkWidget *widget)
4406 GtkLabel *label = GTK_LABEL (widget);
4407 GtkLabelPrivate *priv = label->priv;
4408 gboolean select_on_focus;
4411 if (priv->select_info == NULL)
4414 GTK_WIDGET_CLASS (gtk_label_parent_class)->grab_focus (widget);
4416 if (priv->select_info->selectable)
4418 g_object_get (gtk_widget_get_settings (widget),
4419 "gtk-label-select-on-focus",
4423 if (select_on_focus && !priv->in_click)
4424 gtk_label_select_region (label, 0, -1);
4428 if (priv->select_info->links && !priv->in_click)
4430 link = priv->select_info->links->data;
4431 priv->select_info->selection_anchor = link->start;
4432 priv->select_info->selection_end = link->start;
4438 gtk_label_focus (GtkWidget *widget,
4439 GtkDirectionType direction)
4441 GtkLabel *label = GTK_LABEL (widget);
4442 GtkLabelPrivate *priv = label->priv;
4443 GtkLabelSelectionInfo *info = priv->select_info;
4444 GtkLabelLink *focus_link;
4447 if (!gtk_widget_is_focus (widget))
4449 gtk_widget_grab_focus (widget);
4452 focus_link = gtk_label_get_focus_link (label);
4453 if (focus_link && direction == GTK_DIR_TAB_BACKWARD)
4455 l = g_list_last (info->links);
4456 focus_link = l->data;
4457 info->selection_anchor = focus_link->start;
4458 info->selection_end = focus_link->start;
4468 if (info->selectable)
4472 if (info->selection_anchor != info->selection_end)
4475 index = info->selection_anchor;
4477 if (direction == GTK_DIR_TAB_FORWARD)
4478 for (l = info->links; l; l = l->next)
4480 GtkLabelLink *link = l->data;
4482 if (link->start > index)
4484 gtk_label_select_region_index (label, link->start, link->start);
4488 else if (direction == GTK_DIR_TAB_BACKWARD)
4489 for (l = g_list_last (info->links); l; l = l->prev)
4491 GtkLabelLink *link = l->data;
4493 if (link->end < index)
4495 gtk_label_select_region_index (label, link->start, link->start);
4504 focus_link = gtk_label_get_focus_link (label);
4507 case GTK_DIR_TAB_FORWARD:
4510 l = g_list_find (info->links, focus_link);
4517 case GTK_DIR_TAB_BACKWARD:
4520 l = g_list_find (info->links, focus_link);
4524 l = g_list_last (info->links);
4533 focus_link = l->data;
4534 info->selection_anchor = focus_link->start;
4535 info->selection_end = focus_link->start;
4536 gtk_widget_queue_draw (widget);
4548 gtk_label_button_press (GtkWidget *widget,
4549 GdkEventButton *event)
4551 GtkLabel *label = GTK_LABEL (widget);
4552 GtkLabelPrivate *priv = label->priv;
4553 GtkLabelSelectionInfo *info = priv->select_info;
4560 if (info->active_link)
4562 if (gdk_event_triggers_context_menu ((GdkEvent *) event))
4564 info->link_clicked = 1;
4565 gtk_label_do_popup (label, event);
4568 else if (event->button == GDK_BUTTON_PRIMARY)
4570 info->link_clicked = 1;
4571 gtk_widget_queue_draw (widget);
4575 if (!info->selectable)
4578 info->in_drag = FALSE;
4579 info->select_words = FALSE;
4581 if (gdk_event_triggers_context_menu ((GdkEvent *) event))
4583 gtk_label_do_popup (label, event);
4587 else if (event->button == GDK_BUTTON_PRIMARY)
4589 if (!gtk_widget_has_focus (widget))
4591 priv->in_click = TRUE;
4592 gtk_widget_grab_focus (widget);
4593 priv->in_click = FALSE;
4596 if (event->type == GDK_3BUTTON_PRESS)
4598 gtk_label_select_region_index (label, 0, strlen (priv->text));
4602 if (event->type == GDK_2BUTTON_PRESS)
4604 info->select_words = TRUE;
4605 gtk_label_select_word (label);
4609 get_layout_index (label, event->x, event->y, &index);
4611 min = MIN (info->selection_anchor, info->selection_end);
4612 max = MAX (info->selection_anchor, info->selection_end);
4614 if ((info->selection_anchor != info->selection_end) &&
4615 (event->state & GDK_SHIFT_MASK))
4617 if (index > min && index < max)
4619 /* truncate selection, but keep it as big as possible */
4620 if (index - min > max - index)
4627 /* extend (same as motion) */
4628 min = MIN (min, index);
4629 max = MAX (max, index);
4632 /* ensure the anchor is opposite index */
4640 gtk_label_select_region_index (label, min, max);
4644 if (event->type == GDK_3BUTTON_PRESS)
4645 gtk_label_select_region_index (label, 0, strlen (priv->text));
4646 else if (event->type == GDK_2BUTTON_PRESS)
4647 gtk_label_select_word (label);
4648 else if (min < max && min <= index && index <= max)
4650 info->in_drag = TRUE;
4651 info->drag_start_x = event->x;
4652 info->drag_start_y = event->y;
4655 /* start a replacement */
4656 gtk_label_select_region_index (label, index, index);
4666 gtk_label_button_release (GtkWidget *widget,
4667 GdkEventButton *event)
4670 GtkLabel *label = GTK_LABEL (widget);
4671 GtkLabelPrivate *priv = label->priv;
4672 GtkLabelSelectionInfo *info = priv->select_info;
4682 get_layout_index (label, event->x, event->y, &index);
4683 gtk_label_select_region_index (label, index, index);
4688 if (event->button != GDK_BUTTON_PRIMARY)
4691 if (info->active_link &&
4692 info->selection_anchor == info->selection_end &&
4695 emit_activate_link (label, info->active_link);
4696 info->link_clicked = 0;
4701 /* The goal here is to return TRUE iff we ate the
4702 * button press to start selecting.
4708 connect_mnemonics_visible_notify (GtkLabel *label)
4710 GtkLabelPrivate *priv = label->priv;
4711 GtkWidget *toplevel;
4714 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
4716 if (!GTK_IS_WINDOW (toplevel))
4719 /* always set up this widgets initial value */
4720 priv->mnemonics_visible =
4721 gtk_window_get_mnemonics_visible (GTK_WINDOW (toplevel));
4724 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (toplevel),
4725 "gtk-label-mnemonics-visible-connected"));
4729 g_signal_connect (toplevel,
4730 "notify::mnemonics-visible",
4731 G_CALLBACK (label_mnemonics_visible_changed),
4733 g_object_set_data (G_OBJECT (toplevel),
4734 "gtk-label-mnemonics-visible-connected",
4735 GINT_TO_POINTER (1));
4740 drag_begin_cb (GtkWidget *widget,
4741 GdkDragContext *context,
4744 GtkLabel *label = GTK_LABEL (widget);
4745 GtkLabelPrivate *priv = label->priv;
4746 cairo_surface_t *surface = NULL;
4748 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
4750 if ((priv->select_info->selection_anchor !=
4751 priv->select_info->selection_end) &&
4757 start = MIN (priv->select_info->selection_anchor,
4758 priv->select_info->selection_end);
4759 end = MAX (priv->select_info->selection_anchor,
4760 priv->select_info->selection_end);
4762 len = strlen (priv->text);
4770 surface = _gtk_text_util_create_drag_icon (widget,
4777 gtk_drag_set_icon_surface (context, surface);
4778 cairo_surface_destroy (surface);
4782 gtk_drag_set_icon_default (context);
4787 gtk_label_motion (GtkWidget *widget,
4788 GdkEventMotion *event)
4790 GtkLabel *label = GTK_LABEL (widget);
4791 GtkLabelPrivate *priv = label->priv;
4792 GtkLabelSelectionInfo *info = priv->select_info;
4798 if (info->links && !info->in_drag)
4802 gboolean found = FALSE;
4804 if (info->selection_anchor == info->selection_end)
4806 if (get_layout_index (label, event->x, event->y, &index))
4808 for (l = info->links; l != NULL; l = l->next)
4811 if (index >= link->start && index <= link->end)
4822 if (info->active_link != link)
4824 info->link_clicked = 0;
4825 info->active_link = link;
4826 gtk_label_update_cursor (label);
4827 gtk_widget_queue_draw (widget);
4832 if (info->active_link != NULL)
4834 info->link_clicked = 0;
4835 info->active_link = NULL;
4836 gtk_label_update_cursor (label);
4837 gtk_widget_queue_draw (widget);
4842 if (!info->selectable)
4845 if ((event->state & GDK_BUTTON1_MASK) == 0)
4850 if (gtk_drag_check_threshold (widget,
4853 event->x, event->y))
4855 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
4857 gtk_target_list_add_text_targets (target_list, 0);
4859 g_signal_connect (widget, "drag-begin",
4860 G_CALLBACK (drag_begin_cb), NULL);
4861 gtk_drag_begin (widget, target_list,
4863 1, (GdkEvent *)event);
4865 info->in_drag = FALSE;
4867 gtk_target_list_unref (target_list);
4874 gdk_window_get_device_position (info->window, event->device, &x, &y, NULL);
4875 get_layout_index (label, x, y, &index);
4877 if (info->select_words)
4880 gint old_min, old_max;
4883 min = gtk_label_move_backward_word (label, index);
4884 max = gtk_label_move_forward_word (label, index);
4886 anchor = info->selection_anchor;
4887 end = info->selection_end;
4889 old_min = MIN (anchor, end);
4890 old_max = MAX (anchor, end);
4897 else if (old_max < max)
4902 else if (anchor == old_min)
4913 gtk_label_select_region_index (label, anchor, end);
4916 gtk_label_select_region_index (label, info->selection_anchor, index);
4923 gtk_label_leave_notify (GtkWidget *widget,
4924 GdkEventCrossing *event)
4926 GtkLabel *label = GTK_LABEL (widget);
4927 GtkLabelPrivate *priv = label->priv;
4929 if (priv->select_info)
4931 priv->select_info->active_link = NULL;
4932 gtk_label_update_cursor (label);
4933 gtk_widget_queue_draw (widget);
4936 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event)
4937 return GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event (widget, event);
4943 gtk_label_create_window (GtkLabel *label)
4945 GtkLabelPrivate *priv = label->priv;
4946 GtkAllocation allocation;
4948 GdkWindowAttr attributes;
4949 gint attributes_mask;
4951 g_assert (priv->select_info);
4952 widget = GTK_WIDGET (label);
4953 g_assert (gtk_widget_get_realized (widget));
4955 if (priv->select_info->window)
4958 gtk_widget_get_allocation (widget, &allocation);
4960 attributes.x = allocation.x;
4961 attributes.y = allocation.y;
4962 attributes.width = allocation.width;
4963 attributes.height = allocation.height;
4964 attributes.window_type = GDK_WINDOW_CHILD;
4965 attributes.wclass = GDK_INPUT_ONLY;
4966 attributes.override_redirect = TRUE;
4967 attributes.event_mask = gtk_widget_get_events (widget) |
4968 GDK_BUTTON_PRESS_MASK |
4969 GDK_BUTTON_RELEASE_MASK |
4970 GDK_LEAVE_NOTIFY_MASK |
4971 GDK_BUTTON_MOTION_MASK |
4972 GDK_POINTER_MOTION_MASK |
4973 GDK_POINTER_MOTION_HINT_MASK;
4974 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
4975 if (gtk_widget_is_sensitive (widget))
4977 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
4979 attributes_mask |= GDK_WA_CURSOR;
4983 priv->select_info->window = gdk_window_new (gtk_widget_get_window (widget),
4984 &attributes, attributes_mask);
4985 gdk_window_set_user_data (priv->select_info->window, widget);
4987 if (attributes_mask & GDK_WA_CURSOR)
4988 g_object_unref (attributes.cursor);
4992 gtk_label_destroy_window (GtkLabel *label)
4994 GtkLabelPrivate *priv = label->priv;
4996 g_assert (priv->select_info);
4998 if (priv->select_info->window == NULL)
5001 gdk_window_set_user_data (priv->select_info->window, NULL);
5002 gdk_window_destroy (priv->select_info->window);
5003 priv->select_info->window = NULL;
5007 gtk_label_ensure_select_info (GtkLabel *label)
5009 GtkLabelPrivate *priv = label->priv;
5011 if (priv->select_info == NULL)
5013 priv->select_info = g_new0 (GtkLabelSelectionInfo, 1);
5015 gtk_widget_set_can_focus (GTK_WIDGET (label), TRUE);
5017 if (gtk_widget_get_realized (GTK_WIDGET (label)))
5018 gtk_label_create_window (label);
5020 if (gtk_widget_get_mapped (GTK_WIDGET (label)))
5021 gdk_window_show (priv->select_info->window);
5026 gtk_label_clear_select_info (GtkLabel *label)
5028 GtkLabelPrivate *priv = label->priv;
5030 if (priv->select_info == NULL)
5033 if (!priv->select_info->selectable && !priv->select_info->links)
5035 gtk_label_destroy_window (label);
5037 g_free (priv->select_info);
5038 priv->select_info = NULL;
5040 gtk_widget_set_can_focus (GTK_WIDGET (label), FALSE);
5045 * gtk_label_set_selectable:
5046 * @label: a #GtkLabel
5047 * @setting: %TRUE to allow selecting text in the label
5049 * Selectable labels allow the user to select text from the label, for
5053 gtk_label_set_selectable (GtkLabel *label,
5056 GtkLabelPrivate *priv;
5057 gboolean old_setting;
5059 g_return_if_fail (GTK_IS_LABEL (label));
5063 setting = setting != FALSE;
5064 old_setting = priv->select_info && priv->select_info->selectable;
5068 gtk_label_ensure_select_info (label);
5069 priv->select_info->selectable = TRUE;
5070 gtk_label_update_cursor (label);
5076 /* unselect, to give up the selection */
5077 gtk_label_select_region (label, 0, 0);
5079 priv->select_info->selectable = FALSE;
5080 gtk_label_clear_select_info (label);
5081 gtk_label_update_cursor (label);
5084 if (setting != old_setting)
5086 g_object_freeze_notify (G_OBJECT (label));
5087 g_object_notify (G_OBJECT (label), "selectable");
5088 g_object_notify (G_OBJECT (label), "cursor-position");
5089 g_object_notify (G_OBJECT (label), "selection-bound");
5090 g_object_thaw_notify (G_OBJECT (label));
5091 gtk_widget_queue_draw (GTK_WIDGET (label));
5096 * gtk_label_get_selectable:
5097 * @label: a #GtkLabel
5099 * Gets the value set by gtk_label_set_selectable().
5101 * Return value: %TRUE if the user can copy text from the label
5104 gtk_label_get_selectable (GtkLabel *label)
5106 GtkLabelPrivate *priv;
5108 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5112 return priv->select_info && priv->select_info->selectable;
5116 * gtk_label_set_angle:
5117 * @label: a #GtkLabel
5118 * @angle: the angle that the baseline of the label makes with
5119 * the horizontal, in degrees, measured counterclockwise
5121 * Sets the angle of rotation for the label. An angle of 90 reads from
5122 * from bottom to top, an angle of 270, from top to bottom. The angle
5123 * setting for the label is ignored if the label is selectable,
5124 * wrapped, or ellipsized.
5129 gtk_label_set_angle (GtkLabel *label,
5132 GtkLabelPrivate *priv;
5134 g_return_if_fail (GTK_IS_LABEL (label));
5138 /* Canonicalize to [0,360]. We don't canonicalize 360 to 0, because
5139 * double property ranges are inclusive, and changing 360 to 0 would
5140 * make a property editor behave strangely.
5142 if (angle < 0 || angle > 360.0)
5143 angle = angle - 360. * floor (angle / 360.);
5145 if (priv->angle != angle)
5147 priv->angle = angle;
5149 gtk_label_clear_layout (label);
5150 gtk_widget_queue_resize (GTK_WIDGET (label));
5152 g_object_notify (G_OBJECT (label), "angle");
5157 * gtk_label_get_angle:
5158 * @label: a #GtkLabel
5160 * Gets the angle of rotation for the label. See
5161 * gtk_label_set_angle().
5163 * Return value: the angle of rotation for the label
5168 gtk_label_get_angle (GtkLabel *label)
5170 g_return_val_if_fail (GTK_IS_LABEL (label), 0.0);
5172 return label->priv->angle;
5176 gtk_label_set_selection_text (GtkLabel *label,
5177 GtkSelectionData *selection_data)
5179 GtkLabelPrivate *priv = label->priv;
5181 if (priv->select_info &&
5182 (priv->select_info->selection_anchor !=
5183 priv->select_info->selection_end) &&
5189 start = MIN (priv->select_info->selection_anchor,
5190 priv->select_info->selection_end);
5191 end = MAX (priv->select_info->selection_anchor,
5192 priv->select_info->selection_end);
5194 len = strlen (priv->text);
5202 gtk_selection_data_set_text (selection_data,
5209 gtk_label_drag_data_get (GtkWidget *widget,
5210 GdkDragContext *context,
5211 GtkSelectionData *selection_data,
5215 gtk_label_set_selection_text (GTK_LABEL (widget), selection_data);
5219 get_text_callback (GtkClipboard *clipboard,
5220 GtkSelectionData *selection_data,
5222 gpointer user_data_or_owner)
5224 gtk_label_set_selection_text (GTK_LABEL (user_data_or_owner), selection_data);
5228 clear_text_callback (GtkClipboard *clipboard,
5229 gpointer user_data_or_owner)
5232 GtkLabelPrivate *priv;
5234 label = GTK_LABEL (user_data_or_owner);
5237 if (priv->select_info)
5239 priv->select_info->selection_anchor = priv->select_info->selection_end;
5241 gtk_widget_queue_draw (GTK_WIDGET (label));
5246 gtk_label_select_region_index (GtkLabel *label,
5250 GtkLabelPrivate *priv;
5252 g_return_if_fail (GTK_IS_LABEL (label));
5256 if (priv->select_info && priv->select_info->selectable)
5258 GtkClipboard *clipboard;
5260 if (priv->select_info->selection_anchor == anchor_index &&
5261 priv->select_info->selection_end == end_index)
5264 g_object_freeze_notify (G_OBJECT (label));
5266 if (priv->select_info->selection_anchor != anchor_index)
5267 g_object_notify (G_OBJECT (label), "selection-bound");
5268 if (priv->select_info->selection_end != end_index)
5269 g_object_notify (G_OBJECT (label), "cursor-position");
5271 priv->select_info->selection_anchor = anchor_index;
5272 priv->select_info->selection_end = end_index;
5274 if (gtk_widget_has_screen (GTK_WIDGET (label)))
5275 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label),
5276 GDK_SELECTION_PRIMARY);
5280 if (anchor_index != end_index)
5282 GtkTargetList *list;
5283 GtkTargetEntry *targets;
5286 list = gtk_target_list_new (NULL, 0);
5287 gtk_target_list_add_text_targets (list, 0);
5288 targets = gtk_target_table_new_from_list (list, &n_targets);
5291 gtk_clipboard_set_with_owner (clipboard,
5294 clear_text_callback,
5297 gtk_target_table_free (targets, n_targets);
5298 gtk_target_list_unref (list);
5303 gtk_clipboard_get_owner (clipboard) == G_OBJECT (label))
5304 gtk_clipboard_clear (clipboard);
5307 gtk_widget_queue_draw (GTK_WIDGET (label));
5309 g_object_thaw_notify (G_OBJECT (label));
5314 * gtk_label_select_region:
5315 * @label: a #GtkLabel
5316 * @start_offset: start offset (in characters not bytes)
5317 * @end_offset: end offset (in characters not bytes)
5319 * Selects a range of characters in the label, if the label is selectable.
5320 * See gtk_label_set_selectable(). If the label is not selectable,
5321 * this function has no effect. If @start_offset or
5322 * @end_offset are -1, then the end of the label will be substituted.
5325 gtk_label_select_region (GtkLabel *label,
5329 GtkLabelPrivate *priv;
5331 g_return_if_fail (GTK_IS_LABEL (label));
5335 if (priv->text && priv->select_info)
5337 if (start_offset < 0)
5338 start_offset = g_utf8_strlen (priv->text, -1);
5341 end_offset = g_utf8_strlen (priv->text, -1);
5343 gtk_label_select_region_index (label,
5344 g_utf8_offset_to_pointer (priv->text, start_offset) - priv->text,
5345 g_utf8_offset_to_pointer (priv->text, end_offset) - priv->text);
5350 * gtk_label_get_selection_bounds:
5351 * @label: a #GtkLabel
5352 * @start: (out): return location for start of selection, as a character offset
5353 * @end: (out): return location for end of selection, as a character offset
5355 * Gets the selected range of characters in the label, returning %TRUE
5356 * if there's a selection.
5358 * Return value: %TRUE if selection is non-empty
5361 gtk_label_get_selection_bounds (GtkLabel *label,
5365 GtkLabelPrivate *priv;
5367 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5371 if (priv->select_info == NULL)
5373 /* not a selectable label */
5383 gint start_index, end_index;
5384 gint start_offset, end_offset;
5387 start_index = MIN (priv->select_info->selection_anchor,
5388 priv->select_info->selection_end);
5389 end_index = MAX (priv->select_info->selection_anchor,
5390 priv->select_info->selection_end);
5392 len = strlen (priv->text);
5394 if (end_index > len)
5397 if (start_index > len)
5400 start_offset = g_utf8_strlen (priv->text, start_index);
5401 end_offset = g_utf8_strlen (priv->text, end_index);
5403 if (start_offset > end_offset)
5405 gint tmp = start_offset;
5406 start_offset = end_offset;
5411 *start = start_offset;
5416 return start_offset != end_offset;
5422 * gtk_label_get_layout:
5423 * @label: a #GtkLabel
5425 * Gets the #PangoLayout used to display the label.
5426 * The layout is useful to e.g. convert text positions to
5427 * pixel positions, in combination with gtk_label_get_layout_offsets().
5428 * The returned layout is owned by the @label so need not be
5429 * freed by the caller. The @label is free to recreate its layout at
5430 * any time, so it should be considered read-only.
5432 * Return value: (transfer none): the #PangoLayout for this label
5435 gtk_label_get_layout (GtkLabel *label)
5437 GtkLabelPrivate *priv;
5439 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
5443 gtk_label_ensure_layout (label);
5445 return priv->layout;
5449 * gtk_label_get_layout_offsets:
5450 * @label: a #GtkLabel
5451 * @x: (out) (allow-none): location to store X offset of layout, or %NULL
5452 * @y: (out) (allow-none): location to store Y offset of layout, or %NULL
5454 * Obtains the coordinates where the label will draw the #PangoLayout
5455 * representing the text in the label; useful to convert mouse events
5456 * into coordinates inside the #PangoLayout, e.g. to take some action
5457 * if some part of the label is clicked. Of course you will need to
5458 * create a #GtkEventBox to receive the events, and pack the label
5459 * inside it, since labels are a #GTK_NO_WINDOW widget. Remember
5460 * when using the #PangoLayout functions you need to convert to
5461 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
5464 gtk_label_get_layout_offsets (GtkLabel *label,
5468 g_return_if_fail (GTK_IS_LABEL (label));
5470 gtk_label_ensure_layout (label);
5472 get_layout_location (label, x, y);
5476 * gtk_label_set_use_markup:
5477 * @label: a #GtkLabel
5478 * @setting: %TRUE if the label's text should be parsed for markup.
5480 * Sets whether the text of the label contains markup in <link
5481 * linkend="PangoMarkupFormat">Pango's text markup
5482 * language</link>. See gtk_label_set_markup().
5485 gtk_label_set_use_markup (GtkLabel *label,
5488 g_return_if_fail (GTK_IS_LABEL (label));
5490 g_object_freeze_notify (G_OBJECT (label));
5492 gtk_label_set_use_markup_internal (label, setting);
5493 gtk_label_recalculate (label);
5495 g_object_thaw_notify (G_OBJECT (label));
5499 * gtk_label_get_use_markup:
5500 * @label: a #GtkLabel
5502 * Returns whether the label's text is interpreted as marked up with
5503 * the <link linkend="PangoMarkupFormat">Pango text markup
5504 * language</link>. See gtk_label_set_use_markup ().
5506 * Return value: %TRUE if the label's text will be parsed for markup.
5509 gtk_label_get_use_markup (GtkLabel *label)
5511 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5513 return label->priv->use_markup;
5517 * gtk_label_set_use_underline:
5518 * @label: a #GtkLabel
5519 * @setting: %TRUE if underlines in the text indicate mnemonics
5521 * If true, an underline in the text indicates the next character should be
5522 * used for the mnemonic accelerator key.
5525 gtk_label_set_use_underline (GtkLabel *label,
5528 g_return_if_fail (GTK_IS_LABEL (label));
5530 g_object_freeze_notify (G_OBJECT (label));
5532 gtk_label_set_use_underline_internal (label, setting);
5533 gtk_label_recalculate (label);
5535 g_object_thaw_notify (G_OBJECT (label));
5539 * gtk_label_get_use_underline:
5540 * @label: a #GtkLabel
5542 * Returns whether an embedded underline in the label indicates a
5543 * mnemonic. See gtk_label_set_use_underline().
5545 * Return value: %TRUE whether an embedded underline in the label indicates
5546 * the mnemonic accelerator keys.
5549 gtk_label_get_use_underline (GtkLabel *label)
5551 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5553 return label->priv->use_underline;
5557 * gtk_label_set_single_line_mode:
5558 * @label: a #GtkLabel
5559 * @single_line_mode: %TRUE if the label should be in single line mode
5561 * Sets whether the label is in single line mode.
5566 gtk_label_set_single_line_mode (GtkLabel *label,
5567 gboolean single_line_mode)
5569 GtkLabelPrivate *priv;
5571 g_return_if_fail (GTK_IS_LABEL (label));
5575 single_line_mode = single_line_mode != FALSE;
5577 if (priv->single_line_mode != single_line_mode)
5579 priv->single_line_mode = single_line_mode;
5581 gtk_label_clear_layout (label);
5582 gtk_widget_queue_resize (GTK_WIDGET (label));
5584 g_object_notify (G_OBJECT (label), "single-line-mode");
5589 * gtk_label_get_single_line_mode:
5590 * @label: a #GtkLabel
5592 * Returns whether the label is in single line mode.
5594 * Return value: %TRUE when the label is in single line mode.
5599 gtk_label_get_single_line_mode (GtkLabel *label)
5601 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5603 return label->priv->single_line_mode;
5606 /* Compute the X position for an offset that corresponds to the "more important
5607 * cursor position for that offset. We use this when trying to guess to which
5608 * end of the selection we should go to when the user hits the left or
5612 get_better_cursor (GtkLabel *label,
5617 GtkLabelPrivate *priv = label->priv;
5618 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5619 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5620 PangoDirection cursor_direction = get_cursor_direction (label);
5621 gboolean split_cursor;
5622 PangoRectangle strong_pos, weak_pos;
5624 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5625 "gtk-split-cursor", &split_cursor,
5628 gtk_label_ensure_layout (label);
5630 pango_layout_get_cursor_pos (priv->layout, index,
5631 &strong_pos, &weak_pos);
5635 *x = strong_pos.x / PANGO_SCALE;
5636 *y = strong_pos.y / PANGO_SCALE;
5640 if (keymap_direction == cursor_direction)
5642 *x = strong_pos.x / PANGO_SCALE;
5643 *y = strong_pos.y / PANGO_SCALE;
5647 *x = weak_pos.x / PANGO_SCALE;
5648 *y = weak_pos.y / PANGO_SCALE;
5655 gtk_label_move_logically (GtkLabel *label,
5659 GtkLabelPrivate *priv = label->priv;
5660 gint offset = g_utf8_pointer_to_offset (priv->text,
5661 priv->text + start);
5665 PangoLogAttr *log_attrs;
5669 gtk_label_ensure_layout (label);
5671 length = g_utf8_strlen (priv->text, -1);
5673 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5675 while (count > 0 && offset < length)
5679 while (offset < length && !log_attrs[offset].is_cursor_position);
5683 while (count < 0 && offset > 0)
5687 while (offset > 0 && !log_attrs[offset].is_cursor_position);
5695 return g_utf8_offset_to_pointer (priv->text, offset) - priv->text;
5699 gtk_label_move_visually (GtkLabel *label,
5703 GtkLabelPrivate *priv = label->priv;
5710 int new_index, new_trailing;
5711 gboolean split_cursor;
5714 gtk_label_ensure_layout (label);
5716 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5717 "gtk-split-cursor", &split_cursor,
5724 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5725 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5727 strong = keymap_direction == get_cursor_direction (label);
5732 pango_layout_move_cursor_visually (priv->layout, strong, index, 0, 1, &new_index, &new_trailing);
5737 pango_layout_move_cursor_visually (priv->layout, strong, index, 0, -1, &new_index, &new_trailing);
5741 if (new_index < 0 || new_index == G_MAXINT)
5746 while (new_trailing--)
5747 index = g_utf8_next_char (priv->text + new_index) - priv->text;
5754 gtk_label_move_forward_word (GtkLabel *label,
5757 GtkLabelPrivate *priv = label->priv;
5758 gint new_pos = g_utf8_pointer_to_offset (priv->text,
5759 priv->text + start);
5762 length = g_utf8_strlen (priv->text, -1);
5763 if (new_pos < length)
5765 PangoLogAttr *log_attrs;
5768 gtk_label_ensure_layout (label);
5770 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5772 /* Find the next word end */
5774 while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
5780 return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
5785 gtk_label_move_backward_word (GtkLabel *label,
5788 GtkLabelPrivate *priv = label->priv;
5789 gint new_pos = g_utf8_pointer_to_offset (priv->text,
5790 priv->text + start);
5794 PangoLogAttr *log_attrs;
5797 gtk_label_ensure_layout (label);
5799 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5803 /* Find the previous word beginning */
5804 while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
5810 return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
5814 gtk_label_move_cursor (GtkLabel *label,
5815 GtkMovementStep step,
5817 gboolean extend_selection)
5819 GtkLabelPrivate *priv = label->priv;
5823 if (priv->select_info == NULL)
5826 old_pos = new_pos = priv->select_info->selection_end;
5828 if (priv->select_info->selection_end != priv->select_info->selection_anchor &&
5831 /* If we have a current selection and aren't extending it, move to the
5832 * start/or end of the selection as appropriate
5836 case GTK_MOVEMENT_VISUAL_POSITIONS:
5839 gint anchor_x, anchor_y;
5840 gboolean end_is_left;
5842 get_better_cursor (label, priv->select_info->selection_end, &end_x, &end_y);
5843 get_better_cursor (label, priv->select_info->selection_anchor, &anchor_x, &anchor_y);
5845 end_is_left = (end_y < anchor_y) || (end_y == anchor_y && end_x < anchor_x);
5848 new_pos = end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
5850 new_pos = !end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
5853 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5854 case GTK_MOVEMENT_WORDS:
5856 new_pos = MIN (priv->select_info->selection_end, priv->select_info->selection_anchor);
5858 new_pos = MAX (priv->select_info->selection_end, priv->select_info->selection_anchor);
5860 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5861 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5862 case GTK_MOVEMENT_BUFFER_ENDS:
5863 /* FIXME: Can do better here */
5864 new_pos = count < 0 ? 0 : strlen (priv->text);
5866 case GTK_MOVEMENT_DISPLAY_LINES:
5867 case GTK_MOVEMENT_PARAGRAPHS:
5868 case GTK_MOVEMENT_PAGES:
5869 case GTK_MOVEMENT_HORIZONTAL_PAGES:
5877 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5878 new_pos = gtk_label_move_logically (label, new_pos, count);
5880 case GTK_MOVEMENT_VISUAL_POSITIONS:
5881 new_pos = gtk_label_move_visually (label, new_pos, count);
5882 if (new_pos == old_pos)
5884 if (!extend_selection)
5886 if (!gtk_widget_keynav_failed (GTK_WIDGET (label),
5888 GTK_DIR_RIGHT : GTK_DIR_LEFT))
5890 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
5893 gtk_widget_child_focus (toplevel,
5895 GTK_DIR_RIGHT : GTK_DIR_LEFT);
5900 gtk_widget_error_bell (GTK_WIDGET (label));
5904 case GTK_MOVEMENT_WORDS:
5907 new_pos = gtk_label_move_forward_word (label, new_pos);
5912 new_pos = gtk_label_move_backward_word (label, new_pos);
5915 if (new_pos == old_pos)
5916 gtk_widget_error_bell (GTK_WIDGET (label));
5918 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5919 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5920 case GTK_MOVEMENT_BUFFER_ENDS:
5921 /* FIXME: Can do better here */
5922 new_pos = count < 0 ? 0 : strlen (priv->text);
5923 if (new_pos == old_pos)
5924 gtk_widget_error_bell (GTK_WIDGET (label));
5926 case GTK_MOVEMENT_DISPLAY_LINES:
5927 case GTK_MOVEMENT_PARAGRAPHS:
5928 case GTK_MOVEMENT_PAGES:
5929 case GTK_MOVEMENT_HORIZONTAL_PAGES:
5934 if (extend_selection)
5935 gtk_label_select_region_index (label,
5936 priv->select_info->selection_anchor,
5939 gtk_label_select_region_index (label, new_pos, new_pos);
5943 gtk_label_copy_clipboard (GtkLabel *label)
5945 GtkLabelPrivate *priv = label->priv;
5947 if (priv->text && priv->select_info)
5951 GtkClipboard *clipboard;
5953 start = MIN (priv->select_info->selection_anchor,
5954 priv->select_info->selection_end);
5955 end = MAX (priv->select_info->selection_anchor,
5956 priv->select_info->selection_end);
5958 len = strlen (priv->text);
5966 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
5969 gtk_clipboard_set_text (clipboard, priv->text + start, end - start);
5974 link = gtk_label_get_focus_link (label);
5976 gtk_clipboard_set_text (clipboard, link->uri, -1);
5982 gtk_label_select_all (GtkLabel *label)
5984 GtkLabelPrivate *priv = label->priv;
5986 gtk_label_select_region_index (label, 0, strlen (priv->text));
5989 /* Quick hack of a popup menu
5992 activate_cb (GtkWidget *menuitem,
5995 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
5996 g_signal_emit_by_name (label, signal);
6000 append_action_signal (GtkLabel *label,
6002 const gchar *stock_id,
6003 const gchar *signal,
6006 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6008 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
6009 g_signal_connect (menuitem, "activate",
6010 G_CALLBACK (activate_cb), label);
6012 gtk_widget_set_sensitive (menuitem, sensitive);
6014 gtk_widget_show (menuitem);
6015 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6019 popup_menu_detach (GtkWidget *attach_widget,
6022 GtkLabel *label = GTK_LABEL (attach_widget);
6023 GtkLabelPrivate *priv = label->priv;
6025 if (priv->select_info)
6026 priv->select_info->popup_menu = NULL;
6030 popup_position_func (GtkMenu *menu,
6038 GtkAllocation allocation;
6042 label = GTK_LABEL (user_data);
6043 widget = GTK_WIDGET (label);
6045 g_return_if_fail (gtk_widget_get_realized (widget));
6047 screen = gtk_widget_get_screen (widget);
6048 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
6050 gtk_widget_get_allocation (widget, &allocation);
6055 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
6058 gtk_widget_get_allocation (widget, &allocation);
6060 *x += allocation.width / 2;
6061 *y += allocation.height;
6063 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
6064 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
6068 open_link_activate_cb (GtkMenuItem *menu_item,
6073 link = gtk_label_get_current_link (label);
6076 emit_activate_link (label, link);
6080 copy_link_activate_cb (GtkMenuItem *menu_item,
6083 GtkClipboard *clipboard;
6086 uri = gtk_label_get_current_uri (label);
6089 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6090 gtk_clipboard_set_text (clipboard, uri, -1);
6095 gtk_label_popup_menu (GtkWidget *widget)
6097 gtk_label_do_popup (GTK_LABEL (widget), NULL);
6103 gtk_label_do_popup (GtkLabel *label,
6104 GdkEventButton *event)
6106 GtkLabelPrivate *priv = label->priv;
6107 GtkWidget *menuitem;
6110 gboolean have_selection;
6113 if (!priv->select_info)
6116 if (priv->select_info->popup_menu)
6117 gtk_widget_destroy (priv->select_info->popup_menu);
6119 priv->select_info->popup_menu = menu = gtk_menu_new ();
6121 gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (label), popup_menu_detach);
6124 priv->select_info->selection_anchor != priv->select_info->selection_end;
6128 if (priv->select_info->link_clicked)
6129 link = priv->select_info->active_link;
6134 link = gtk_label_get_focus_link (label);
6136 if (!have_selection && link)
6139 menuitem = gtk_image_menu_item_new_with_mnemonic (_("_Open Link"));
6140 gtk_widget_show (menuitem);
6141 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6143 g_signal_connect (G_OBJECT (menuitem), "activate",
6144 G_CALLBACK (open_link_activate_cb), label);
6146 image = gtk_image_new_from_stock (GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_MENU);
6147 gtk_widget_show (image);
6148 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6150 /* Copy Link Address */
6151 menuitem = gtk_image_menu_item_new_with_mnemonic (_("Copy _Link Address"));
6152 gtk_widget_show (menuitem);
6153 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6155 g_signal_connect (G_OBJECT (menuitem), "activate",
6156 G_CALLBACK (copy_link_activate_cb), label);
6158 image = gtk_image_new_from_stock (GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
6159 gtk_widget_show (image);
6160 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6164 append_action_signal (label, menu, GTK_STOCK_CUT, "cut-clipboard", FALSE);
6165 append_action_signal (label, menu, GTK_STOCK_COPY, "copy-clipboard", have_selection);
6166 append_action_signal (label, menu, GTK_STOCK_PASTE, "paste-clipboard", FALSE);
6168 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
6169 gtk_widget_set_sensitive (menuitem, FALSE);
6170 gtk_widget_show (menuitem);
6171 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6173 menuitem = gtk_separator_menu_item_new ();
6174 gtk_widget_show (menuitem);
6175 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6177 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
6178 g_signal_connect_swapped (menuitem, "activate",
6179 G_CALLBACK (gtk_label_select_all), label);
6180 gtk_widget_show (menuitem);
6181 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6184 g_signal_emit (label, signals[POPULATE_POPUP], 0, menu);
6187 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6189 event->button, event->time);
6192 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6193 popup_position_func, label,
6194 0, gtk_get_current_event_time ());
6195 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
6200 gtk_label_clear_links (GtkLabel *label)
6202 GtkLabelPrivate *priv = label->priv;
6204 if (!priv->select_info)
6207 g_list_free_full (priv->select_info->links, (GDestroyNotify) link_free);
6208 priv->select_info->links = NULL;
6209 priv->select_info->active_link = NULL;
6213 gtk_label_activate_link (GtkLabel *label,
6216 GtkWidget *widget = GTK_WIDGET (label);
6217 GError *error = NULL;
6219 if (!gtk_show_uri (gtk_widget_get_screen (widget),
6220 uri, gtk_get_current_event_time (), &error))
6222 g_warning ("Unable to show '%s': %s", uri, error->message);
6223 g_error_free (error);
6230 emit_activate_link (GtkLabel *label,
6233 GtkLabelPrivate *priv = label->priv;
6236 g_signal_emit (label, signals[ACTIVATE_LINK], 0, link->uri, &handled);
6237 if (handled && priv->track_links && !link->visited)
6239 link->visited = TRUE;
6240 /* FIXME: shouldn't have to redo everything here */
6241 gtk_label_clear_layout (label);
6246 gtk_label_activate_current_link (GtkLabel *label)
6249 GtkWidget *widget = GTK_WIDGET (label);
6251 link = gtk_label_get_focus_link (label);
6255 emit_activate_link (label, link);
6259 GtkWidget *toplevel;
6261 GtkWidget *default_widget, *focus_widget;
6263 toplevel = gtk_widget_get_toplevel (widget);
6264 if (GTK_IS_WINDOW (toplevel))
6266 window = GTK_WINDOW (toplevel);
6270 default_widget = gtk_window_get_default_widget (window);
6271 focus_widget = gtk_window_get_focus (window);
6273 if (default_widget != widget &&
6274 !(widget == focus_widget && (!default_widget || !gtk_widget_is_sensitive (default_widget))))
6275 gtk_window_activate_default (window);
6281 static GtkLabelLink *
6282 gtk_label_get_current_link (GtkLabel *label)
6284 GtkLabelPrivate *priv = label->priv;
6287 if (!priv->select_info)
6290 if (priv->select_info->link_clicked)
6291 link = priv->select_info->active_link;
6293 link = gtk_label_get_focus_link (label);
6299 * gtk_label_get_current_uri:
6300 * @label: a #GtkLabel
6302 * Returns the URI for the currently active link in the label.
6303 * The active link is the one under the mouse pointer or, in a
6304 * selectable label, the link in which the text cursor is currently
6307 * This function is intended for use in a #GtkLabel::activate-link handler
6308 * or for use in a #GtkWidget::query-tooltip handler.
6310 * Returns: the currently active URI. The string is owned by GTK+ and must
6311 * not be freed or modified.
6316 gtk_label_get_current_uri (GtkLabel *label)
6320 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
6322 link = gtk_label_get_current_link (label);
6331 * gtk_label_set_track_visited_links:
6332 * @label: a #GtkLabel
6333 * @track_links: %TRUE to track visited links
6335 * Sets whether the label should keep track of clicked
6336 * links (and use a different color for them).
6341 gtk_label_set_track_visited_links (GtkLabel *label,
6342 gboolean track_links)
6344 GtkLabelPrivate *priv;
6346 g_return_if_fail (GTK_IS_LABEL (label));
6350 track_links = track_links != FALSE;
6352 if (priv->track_links != track_links)
6354 priv->track_links = track_links;
6356 /* FIXME: shouldn't have to redo everything here */
6357 gtk_label_recalculate (label);
6359 g_object_notify (G_OBJECT (label), "track-visited-links");
6364 * gtk_label_get_track_visited_links:
6365 * @label: a #GtkLabel
6367 * Returns whether the label is currently keeping track
6370 * Returns: %TRUE if clicked links are remembered
6375 gtk_label_get_track_visited_links (GtkLabel *label)
6377 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
6379 return label->priv->track_links;
6383 gtk_label_query_tooltip (GtkWidget *widget,
6386 gboolean keyboard_tip,
6387 GtkTooltip *tooltip)
6389 GtkLabel *label = GTK_LABEL (widget);
6390 GtkLabelPrivate *priv = label->priv;
6391 GtkLabelSelectionInfo *info = priv->select_info;
6395 if (info && info->links)
6399 if (info->selection_anchor == info->selection_end)
6400 index = info->selection_anchor;
6404 if (!get_layout_index (label, x, y, &index))
6410 for (l = info->links; l != NULL; l = l->next)
6412 GtkLabelLink *link = l->data;
6413 if (index >= link->start && index <= link->end)
6417 gtk_tooltip_set_markup (tooltip, link->title);
6426 return GTK_WIDGET_CLASS (gtk_label_parent_class)->query_tooltip (widget,
6433 _gtk_label_get_cursor_position (GtkLabel *label)
6435 GtkLabelPrivate *priv = label->priv;
6437 if (priv->select_info && priv->select_info->selectable)
6438 return g_utf8_pointer_to_offset (priv->text,
6439 priv->text + priv->select_info->selection_end);
6445 _gtk_label_get_selection_bound (GtkLabel *label)
6447 GtkLabelPrivate *priv = label->priv;
6449 if (priv->select_info && priv->select_info->selectable)
6450 return g_utf8_pointer_to_offset (priv->text,
6451 priv->text + priv->select_info->selection_anchor);