1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
21 * file for a list of people on the GTK+ Team. See the ChangeLog
22 * files for a list of changes. These files are distributed with
23 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
32 #include "gtkaccellabel.h"
34 #include "gtkmainprivate.h"
35 #include "gtkmarshalers.h"
37 #include "gtkwindow.h"
38 #include "gtkclipboard.h"
39 #include "gtkimagemenuitem.h"
41 #include "gtkseparatormenuitem.h"
42 #include "gtktextutil.h"
43 #include "gtkmenuitem.h"
44 #include "gtkmenushellprivate.h"
45 #include "gtknotebook.h"
47 #include "gtkbindings.h"
48 #include "gtkbuildable.h"
51 #include "gtktooltip.h"
52 #include "gtkprivate.h"
53 #include "gtktypebuiltins.h"
58 * @Short_description: A widget that displays a small to medium amount of text
61 * The #GtkLabel widget displays a small amount of text. As the name
62 * implies, most labels are used to label another widget such as a
63 * #GtkButton, a #GtkMenuItem, or a #GtkOptionMenu.
65 * <refsect2 id="GtkLabel-BUILDER-UI">
66 * <title>GtkLabel as GtkBuildable</title>
68 * The GtkLabel implementation of the GtkBuildable interface supports a
69 * custom <attributes> element, which supports any number of <attribute>
70 * elements. the <attribute> element has attributes named name, value,
71 * start and end and allows you to specify #PangoAttribute values for this label.
74 * <title>A UI definition fragment specifying Pango attributes</title>
75 * <programlisting><![CDATA[
76 * <object class="GtkLabel">
78 * <attribute name="weight" value="PANGO_WEIGHT_BOLD"/>
79 * <attribute name="background" value="red" start="5" end="10"/>"
82 * ]]></programlisting>
84 * The start and end attributes specify the range of characters to which the
85 * Pango attribute applies. If start and end are not specified, the attribute is
86 * applied to the whole text. Note that specifying ranges does not make much
87 * sense with translatable attributes. Use markup embedded in the translatable
92 * <title>Mnemonics</title>
94 * Labels may contain <firstterm>mnemonics</firstterm>. Mnemonics are
95 * underlined characters in the label, used for keyboard navigation.
96 * Mnemonics are created by providing a string with an underscore before
97 * the mnemonic character, such as <literal>"_File"</literal>, to the
98 * functions gtk_label_new_with_mnemonic() or
99 * gtk_label_set_text_with_mnemonic().
101 * Mnemonics automatically activate any activatable widget the label is
102 * inside, such as a #GtkButton; if the label is not inside the
103 * mnemonic's target widget, you have to tell the label about the target
104 * using gtk_label_set_mnemonic_widget(). Here's a simple example where
105 * the label is inside a button:
109 * // Pressing Alt+H will activate this button
110 * button = gtk_button_new (<!-- -->);
111 * label = gtk_label_new_with_mnemonic ("_Hello");
112 * gtk_container_add (GTK_CONTAINER (button), label);
116 * There's a convenience function to create buttons with a mnemonic label
121 * // Pressing Alt+H will activate this button
122 * button = gtk_button_new_with_mnemonic ("_Hello");
126 * To create a mnemonic for a widget alongside the label, such as a
127 * #GtkEntry, you have to point the label at the entry with
128 * gtk_label_set_mnemonic_widget():
132 * // Pressing Alt+H will focus the entry
133 * entry = gtk_entry_new (<!-- -->);
134 * label = gtk_label_new_with_mnemonic ("_Hello");
135 * gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
141 * <title>Markup (styled text)</title>
143 * To make it easy to format text in a label (changing colors, fonts,
144 * etc.), label text can be provided in a simple <link
145 * linkend="PangoMarkupFormat">markup format</link>.
146 * Here's how to create a label with a small font:
150 * label = gtk_label_new (NULL);
151 * gtk_label_set_markup (GTK_LABEL (label), "<small>Small text</small>");
156 * linkend="PangoMarkupFormat">complete documentation</link> of available
157 * tags in the Pango manual.)
159 * The markup passed to gtk_label_set_markup() must be valid; for example,
160 * literal <, > and & characters must be escaped as \<,
161 * \gt;, and \&. If you pass text obtained from the user, file,
162 * or a network to gtk_label_set_markup(), you'll want to escape it with
163 * g_markup_escape_text() or g_markup_printf_escaped().
165 * Markup strings are just a convenient way to set the #PangoAttrList on
166 * a label; gtk_label_set_attributes() may be a simpler way to set
167 * attributes in some cases. Be careful though; #PangoAttrList tends to
168 * cause internationalization problems, unless you're applying attributes
169 * to the entire string (i.e. unless you set the range of each attribute
170 * to [0, %G_MAXINT)). The reason is that specifying the start_index and
171 * end_index for a #PangoAttribute requires knowledge of the exact string
172 * being displayed, so translations will cause problems.
176 * <title>Selectable labels</title>
177 * Labels can be made selectable with gtk_label_set_selectable().
178 * Selectable labels allow the user to copy the label contents to
179 * the clipboard. Only labels that contain useful-to-copy information
180 * — such as error messages — should be made selectable.
182 * <refsect2 id="label-text-layout">
183 * <title>Text layout</title>
185 * A label can contain any number of paragraphs, but will have
186 * performance problems if it contains more than a small number.
187 * Paragraphs are separated by newlines or other paragraph separators
188 * understood by Pango.
190 * Labels can automatically wrap text if you call
191 * gtk_label_set_line_wrap().
193 * gtk_label_set_justify() sets how the lines in a label align
194 * with one another. If you want to set how the label as a whole
195 * aligns in its available space, see gtk_misc_set_alignment().
197 * The #GtkLabel:width-chars and #GtkLabel:max-width-chars properties
198 * can be used to control the size allocation of ellipsized or wrapped
199 * labels. For ellipsizing labels, if either is specified (and less
200 * than the actual text size), it is used as the minimum width, and the actual
201 * text size is used as the natural width of the label. For wrapping labels,
202 * width-chars is used as the minimum width, if specified, and max-width-chars
203 * is used as the natural width. Even if max-width-chars specified, wrapping
204 * labels will be rewrapped to use all of the available width.
207 * Note that the interpretation of #GtkLabel:width-chars and
208 * #GtkLabel:max-width-chars has changed a bit with the introduction of
209 * <link linkend="geometry-management">width-for-height geometry management.</link>
214 * <title>Links</title>
216 * Since 2.18, GTK+ supports markup for clickable hyperlinks in addition
217 * to regular Pango markup. The markup for links is borrowed from HTML, using the
218 * <tag>a</tag> with href and title attributes. GTK+ renders links similar to the
219 * way they appear in web browsers, with colored, underlined text. The title
220 * attribute is displayed as a tooltip on the link. An example looks like this:
222 * <informalexample><programlisting>
223 * gtk_label_set_markup (label, "Go to the <a href="http://www.gtk.org" title="<i>Our</i> website">GTK+ website</a> for more...");
224 * </programlisting></informalexample>
226 * It is possible to implement custom handling for links and their tooltips with
227 * the #GtkLabel::activate-link signal and the gtk_label_get_current_uri() function.
233 /*rint() is only available in GCC and/or C99*/
234 #if (__STDC_VERSION__ < 199901L && !defined __GNUC__)
235 double rint(double x)
237 if (ceil(x+0.5) == floor(x+0.5))
239 int a = (int)ceil(x);
252 struct _GtkLabelPrivate
254 GtkLabelSelectionInfo *select_info;
255 GtkWidget *mnemonic_widget;
256 GtkWindow *mnemonic_window;
258 PangoAttrList *attrs;
259 PangoAttrList *effective_attrs;
267 guint mnemonics_visible : 1;
270 guint use_underline : 1;
271 guint use_markup : 1;
273 guint single_line_mode : 1;
274 guint have_transform : 1;
277 guint pattern_set : 1;
278 guint track_links : 1;
280 guint mnemonic_keyval;
284 gint max_width_chars;
287 /* Notes about the handling of links:
289 * Links share the GtkLabelSelectionInfo struct with selectable labels.
290 * There are some new fields for links. The links field contains the list
291 * of GtkLabelLink structs that describe the links which are embedded in
292 * the label. The active_link field points to the link under the mouse
293 * pointer. For keyboard navigation, the 'focus' link is determined by
294 * finding the link which contains the selection_anchor position.
295 * The link_clicked field is used with button press and release events
296 * to ensure that pressing inside a link and releasing outside of it
297 * does not activate the link.
299 * Links are rendered with the link-color/visited-link-color colors
300 * that are determined by the style and with an underline. When the mouse
301 * pointer is over a link, the pointer is changed to indicate the link,
302 * and the background behind the link is rendered with the base[PRELIGHT]
303 * color. While a button is pressed over a link, the background is rendered
304 * with the base[ACTIVE] color.
306 * Labels with links accept keyboard focus, and it is possible to move
307 * the focus between the embedded links using Tab/Shift-Tab. The focus
308 * is indicated by a focus rectangle that is drawn around the link text.
309 * Pressing Enter activates the focussed link, and there is a suitable
310 * context menu for links that can be opened with the Menu key. Pressing
311 * Control-C copies the link URI to the clipboard.
313 * In selectable labels with links, link functionality is only available
314 * when the selection is empty.
319 gchar *title; /* the title attribute, used as tooltip */
320 gboolean visited; /* get set when the link is activated; this flag
321 * gets preserved over later set_markup() calls
323 gint start; /* position of the link in the PangoLayout */
327 struct _GtkLabelSelectionInfo
330 gint selection_anchor;
332 GtkWidget *popup_menu;
335 GtkLabelLink *active_link;
341 guint select_words : 1;
342 guint selectable : 1;
343 guint link_clicked : 1;
351 ACTIVATE_CURRENT_LINK,
366 PROP_MNEMONIC_KEYVAL,
367 PROP_MNEMONIC_WIDGET,
368 PROP_CURSOR_POSITION,
369 PROP_SELECTION_BOUND,
372 PROP_SINGLE_LINE_MODE,
374 PROP_MAX_WIDTH_CHARS,
375 PROP_TRACK_VISITED_LINKS
378 /* When rotating ellipsizable text we want the natural size to request
379 * more to ensure the label wont ever ellipsize in an allocation of full natural size.
381 #define ROTATION_ELLIPSIZE_PADDING 2
383 static guint signals[LAST_SIGNAL] = { 0 };
385 static const GdkColor default_link_color = { 0, 0, 0, 0xeeee };
386 static const GdkColor default_visited_link_color = { 0, 0x5555, 0x1a1a, 0x8b8b };
388 static void gtk_label_set_property (GObject *object,
392 static void gtk_label_get_property (GObject *object,
396 static void gtk_label_finalize (GObject *object);
397 static void gtk_label_destroy (GtkWidget *widget);
398 static void gtk_label_size_allocate (GtkWidget *widget,
399 GtkAllocation *allocation);
400 static void gtk_label_state_changed (GtkWidget *widget,
402 static void gtk_label_style_updated (GtkWidget *widget);
403 static void gtk_label_direction_changed (GtkWidget *widget,
404 GtkTextDirection previous_dir);
405 static gint gtk_label_draw (GtkWidget *widget,
407 static gboolean gtk_label_focus (GtkWidget *widget,
408 GtkDirectionType direction);
410 static void gtk_label_realize (GtkWidget *widget);
411 static void gtk_label_unrealize (GtkWidget *widget);
412 static void gtk_label_map (GtkWidget *widget);
413 static void gtk_label_unmap (GtkWidget *widget);
415 static gboolean gtk_label_button_press (GtkWidget *widget,
416 GdkEventButton *event);
417 static gboolean gtk_label_button_release (GtkWidget *widget,
418 GdkEventButton *event);
419 static gboolean gtk_label_motion (GtkWidget *widget,
420 GdkEventMotion *event);
421 static gboolean gtk_label_leave_notify (GtkWidget *widget,
422 GdkEventCrossing *event);
424 static void gtk_label_grab_focus (GtkWidget *widget);
426 static gboolean gtk_label_query_tooltip (GtkWidget *widget,
429 gboolean keyboard_tip,
430 GtkTooltip *tooltip);
432 static void gtk_label_set_text_internal (GtkLabel *label,
434 static void gtk_label_set_label_internal (GtkLabel *label,
436 static void gtk_label_set_use_markup_internal (GtkLabel *label,
438 static void gtk_label_set_use_underline_internal (GtkLabel *label,
440 static void gtk_label_set_attributes_internal (GtkLabel *label,
441 PangoAttrList *attrs);
442 static void gtk_label_set_uline_text_internal (GtkLabel *label,
444 static void gtk_label_set_pattern_internal (GtkLabel *label,
445 const gchar *pattern,
446 gboolean is_mnemonic);
447 static void gtk_label_set_markup_internal (GtkLabel *label,
449 gboolean with_uline);
450 static void gtk_label_recalculate (GtkLabel *label);
451 static void gtk_label_hierarchy_changed (GtkWidget *widget,
452 GtkWidget *old_toplevel);
453 static void gtk_label_screen_changed (GtkWidget *widget,
454 GdkScreen *old_screen);
455 static gboolean gtk_label_popup_menu (GtkWidget *widget);
457 static void gtk_label_create_window (GtkLabel *label);
458 static void gtk_label_destroy_window (GtkLabel *label);
459 static void gtk_label_ensure_select_info (GtkLabel *label);
460 static void gtk_label_clear_select_info (GtkLabel *label);
461 static void gtk_label_update_cursor (GtkLabel *label);
462 static void gtk_label_clear_layout (GtkLabel *label);
463 static void gtk_label_ensure_layout (GtkLabel *label);
464 static void gtk_label_invalidate_wrap_width (GtkLabel *label);
465 static void gtk_label_select_region_index (GtkLabel *label,
469 static gboolean gtk_label_mnemonic_activate (GtkWidget *widget,
470 gboolean group_cycling);
471 static void gtk_label_setup_mnemonic (GtkLabel *label,
473 static void gtk_label_drag_data_get (GtkWidget *widget,
474 GdkDragContext *context,
475 GtkSelectionData *selection_data,
479 static void gtk_label_buildable_interface_init (GtkBuildableIface *iface);
480 static gboolean gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
483 const gchar *tagname,
484 GMarkupParser *parser,
487 static void gtk_label_buildable_custom_finished (GtkBuildable *buildable,
490 const gchar *tagname,
494 static void connect_mnemonics_visible_notify (GtkLabel *label);
495 static gboolean separate_uline_pattern (const gchar *str,
501 /* For selectable labels: */
502 static void gtk_label_move_cursor (GtkLabel *label,
503 GtkMovementStep step,
505 gboolean extend_selection);
506 static void gtk_label_copy_clipboard (GtkLabel *label);
507 static void gtk_label_select_all (GtkLabel *label);
508 static void gtk_label_do_popup (GtkLabel *label,
509 GdkEventButton *event);
510 static gint gtk_label_move_forward_word (GtkLabel *label,
512 static gint gtk_label_move_backward_word (GtkLabel *label,
516 static void gtk_label_rescan_links (GtkLabel *label);
517 static void gtk_label_clear_links (GtkLabel *label);
518 static gboolean gtk_label_activate_link (GtkLabel *label,
520 static void gtk_label_activate_current_link (GtkLabel *label);
521 static GtkLabelLink *gtk_label_get_current_link (GtkLabel *label);
522 static void gtk_label_get_link_colors (GtkWidget *widget,
523 GdkColor **link_color,
524 GdkColor **visited_link_color);
525 static void emit_activate_link (GtkLabel *label,
528 static GtkSizeRequestMode gtk_label_get_request_mode (GtkWidget *widget);
529 static void gtk_label_get_preferred_width (GtkWidget *widget,
532 static void gtk_label_get_preferred_height (GtkWidget *widget,
535 static void gtk_label_get_preferred_width_for_height (GtkWidget *widget,
538 gint *natural_width);
539 static void gtk_label_get_preferred_height_for_width (GtkWidget *widget,
541 gint *minimum_height,
542 gint *natural_height);
544 static GtkBuildableIface *buildable_parent_iface = NULL;
546 G_DEFINE_TYPE_WITH_CODE (GtkLabel, gtk_label, GTK_TYPE_MISC,
547 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
548 gtk_label_buildable_interface_init))
551 add_move_binding (GtkBindingSet *binding_set,
554 GtkMovementStep step,
557 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
559 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
563 G_TYPE_BOOLEAN, FALSE);
565 /* Selection-extending version */
566 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
570 G_TYPE_BOOLEAN, TRUE);
574 gtk_label_class_init (GtkLabelClass *class)
576 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
577 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
578 GtkBindingSet *binding_set;
580 gobject_class->set_property = gtk_label_set_property;
581 gobject_class->get_property = gtk_label_get_property;
582 gobject_class->finalize = gtk_label_finalize;
584 widget_class->destroy = gtk_label_destroy;
585 widget_class->size_allocate = gtk_label_size_allocate;
586 widget_class->state_changed = gtk_label_state_changed;
587 widget_class->style_updated = gtk_label_style_updated;
588 widget_class->query_tooltip = gtk_label_query_tooltip;
589 widget_class->direction_changed = gtk_label_direction_changed;
590 widget_class->draw = gtk_label_draw;
591 widget_class->realize = gtk_label_realize;
592 widget_class->unrealize = gtk_label_unrealize;
593 widget_class->map = gtk_label_map;
594 widget_class->unmap = gtk_label_unmap;
595 widget_class->button_press_event = gtk_label_button_press;
596 widget_class->button_release_event = gtk_label_button_release;
597 widget_class->motion_notify_event = gtk_label_motion;
598 widget_class->leave_notify_event = gtk_label_leave_notify;
599 widget_class->hierarchy_changed = gtk_label_hierarchy_changed;
600 widget_class->screen_changed = gtk_label_screen_changed;
601 widget_class->mnemonic_activate = gtk_label_mnemonic_activate;
602 widget_class->drag_data_get = gtk_label_drag_data_get;
603 widget_class->grab_focus = gtk_label_grab_focus;
604 widget_class->popup_menu = gtk_label_popup_menu;
605 widget_class->focus = gtk_label_focus;
606 widget_class->get_request_mode = gtk_label_get_request_mode;
607 widget_class->get_preferred_width = gtk_label_get_preferred_width;
608 widget_class->get_preferred_height = gtk_label_get_preferred_height;
609 widget_class->get_preferred_width_for_height = gtk_label_get_preferred_width_for_height;
610 widget_class->get_preferred_height_for_width = gtk_label_get_preferred_height_for_width;
612 class->move_cursor = gtk_label_move_cursor;
613 class->copy_clipboard = gtk_label_copy_clipboard;
614 class->activate_link = gtk_label_activate_link;
617 * GtkLabel::move-cursor:
618 * @entry: the object which received the signal
619 * @step: the granularity of the move, as a #GtkMovementStep
620 * @count: the number of @step units to move
621 * @extend_selection: %TRUE if the move should extend the selection
623 * The ::move-cursor signal is a
624 * <link linkend="keybinding-signals">keybinding signal</link>
625 * which gets emitted when the user initiates a cursor movement.
626 * If the cursor is not visible in @entry, this signal causes
627 * the viewport to be moved instead.
629 * Applications should not connect to it, but may emit it with
630 * g_signal_emit_by_name() if they need to control the cursor
633 * The default bindings for this signal come in two variants,
634 * the variant with the Shift modifier extends the selection,
635 * the variant without the Shift modifer does not.
636 * There are too many key combinations to list them all here.
638 * <listitem>Arrow keys move by individual characters/lines</listitem>
639 * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
640 * <listitem>Home/End keys move to the ends of the buffer</listitem>
643 signals[MOVE_CURSOR] =
644 g_signal_new (I_("move-cursor"),
645 G_OBJECT_CLASS_TYPE (gobject_class),
646 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
647 G_STRUCT_OFFSET (GtkLabelClass, move_cursor),
649 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
651 GTK_TYPE_MOVEMENT_STEP,
656 * GtkLabel::copy-clipboard:
657 * @label: the object which received the signal
659 * The ::copy-clipboard signal is a
660 * <link linkend="keybinding-signals">keybinding signal</link>
661 * which gets emitted to copy the selection to the clipboard.
663 * The default binding for this signal is Ctrl-c.
665 signals[COPY_CLIPBOARD] =
666 g_signal_new (I_("copy-clipboard"),
667 G_OBJECT_CLASS_TYPE (gobject_class),
668 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
669 G_STRUCT_OFFSET (GtkLabelClass, copy_clipboard),
671 _gtk_marshal_VOID__VOID,
675 * GtkLabel::populate-popup:
676 * @label: The label on which the signal is emitted
677 * @menu: the menu that is being populated
679 * The ::populate-popup signal gets emitted before showing the
680 * context menu of the label. Note that only selectable labels
681 * have context menus.
683 * If you need to add items to the context menu, connect
684 * to this signal and append your menuitems to the @menu.
686 signals[POPULATE_POPUP] =
687 g_signal_new (I_("populate-popup"),
688 G_OBJECT_CLASS_TYPE (gobject_class),
690 G_STRUCT_OFFSET (GtkLabelClass, populate_popup),
692 _gtk_marshal_VOID__OBJECT,
697 * GtkLabel::activate-current-link:
698 * @label: The label on which the signal was emitted
700 * A <link linkend="keybinding-signals">keybinding signal</link>
701 * which gets emitted when the user activates a link in the label.
703 * Applications may also emit the signal with g_signal_emit_by_name()
704 * if they need to control activation of URIs programmatically.
706 * The default bindings for this signal are all forms of the Enter key.
710 signals[ACTIVATE_CURRENT_LINK] =
711 g_signal_new_class_handler ("activate-current-link",
712 G_TYPE_FROM_CLASS (gobject_class),
713 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
714 G_CALLBACK (gtk_label_activate_current_link),
716 _gtk_marshal_VOID__VOID,
720 * GtkLabel::activate-link:
721 * @label: The label on which the signal was emitted
722 * @uri: the URI that is activated
724 * The signal which gets emitted to activate a URI.
725 * Applications may connect to it to override the default behaviour,
726 * which is to call gtk_show_uri().
728 * Returns: %TRUE if the link has been activated
732 signals[ACTIVATE_LINK] =
733 g_signal_new ("activate-link",
734 G_TYPE_FROM_CLASS (gobject_class),
736 G_STRUCT_OFFSET (GtkLabelClass, activate_link),
737 _gtk_boolean_handled_accumulator, NULL,
738 _gtk_marshal_BOOLEAN__STRING,
739 G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
741 g_object_class_install_property (gobject_class,
743 g_param_spec_string ("label",
745 P_("The text of the label"),
747 GTK_PARAM_READWRITE));
748 g_object_class_install_property (gobject_class,
750 g_param_spec_boxed ("attributes",
752 P_("A list of style attributes to apply to the text of the label"),
753 PANGO_TYPE_ATTR_LIST,
754 GTK_PARAM_READWRITE));
755 g_object_class_install_property (gobject_class,
757 g_param_spec_boolean ("use-markup",
759 P_("The text of the label includes XML markup. See pango_parse_markup()"),
761 GTK_PARAM_READWRITE));
762 g_object_class_install_property (gobject_class,
764 g_param_spec_boolean ("use-underline",
766 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
768 GTK_PARAM_READWRITE));
770 g_object_class_install_property (gobject_class,
772 g_param_spec_enum ("justify",
774 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"),
775 GTK_TYPE_JUSTIFICATION,
777 GTK_PARAM_READWRITE));
779 g_object_class_install_property (gobject_class,
781 g_param_spec_string ("pattern",
783 P_("A string with _ characters in positions correspond to characters in the text to underline"),
785 GTK_PARAM_WRITABLE));
787 g_object_class_install_property (gobject_class,
789 g_param_spec_boolean ("wrap",
791 P_("If set, wrap lines if the text becomes too wide"),
793 GTK_PARAM_READWRITE));
795 * GtkLabel:wrap-mode:
797 * If line wrapping is on (see the #GtkLabel:wrap property) this controls
798 * how the line wrapping is done. The default is %PANGO_WRAP_WORD, which
799 * means wrap on word boundaries.
803 g_object_class_install_property (gobject_class,
805 g_param_spec_enum ("wrap-mode",
806 P_("Line wrap mode"),
807 P_("If wrap is set, controls how linewrapping is done"),
808 PANGO_TYPE_WRAP_MODE,
810 GTK_PARAM_READWRITE));
811 g_object_class_install_property (gobject_class,
813 g_param_spec_boolean ("selectable",
815 P_("Whether the label text can be selected with the mouse"),
817 GTK_PARAM_READWRITE));
818 g_object_class_install_property (gobject_class,
819 PROP_MNEMONIC_KEYVAL,
820 g_param_spec_uint ("mnemonic-keyval",
822 P_("The mnemonic accelerator key for this label"),
826 GTK_PARAM_READABLE));
827 g_object_class_install_property (gobject_class,
828 PROP_MNEMONIC_WIDGET,
829 g_param_spec_object ("mnemonic-widget",
830 P_("Mnemonic widget"),
831 P_("The widget to be activated when the label's mnemonic "
834 GTK_PARAM_READWRITE));
836 g_object_class_install_property (gobject_class,
837 PROP_CURSOR_POSITION,
838 g_param_spec_int ("cursor-position",
839 P_("Cursor Position"),
840 P_("The current position of the insertion cursor in chars"),
844 GTK_PARAM_READABLE));
846 g_object_class_install_property (gobject_class,
847 PROP_SELECTION_BOUND,
848 g_param_spec_int ("selection-bound",
849 P_("Selection Bound"),
850 P_("The position of the opposite end of the selection from the cursor in chars"),
854 GTK_PARAM_READABLE));
857 * GtkLabel:ellipsize:
859 * The preferred place to ellipsize the string, if the label does
860 * not have enough room to display the entire string, specified as a
861 * #PangoEllisizeMode.
863 * Note that setting this property to a value other than
864 * %PANGO_ELLIPSIZE_NONE has the side-effect that the label requests
865 * only enough space to display the ellipsis "...". In particular, this
866 * means that ellipsizing labels do not work well in notebook tabs, unless
867 * the tab's #GtkNotebook:tab-expand property is set to %TRUE. Other ways
868 * to set a label's width are gtk_widget_set_size_request() and
869 * gtk_label_set_width_chars().
873 g_object_class_install_property (gobject_class,
875 g_param_spec_enum ("ellipsize",
877 P_("The preferred place to ellipsize the string, if the label does not have enough room to display the entire string"),
878 PANGO_TYPE_ELLIPSIZE_MODE,
879 PANGO_ELLIPSIZE_NONE,
880 GTK_PARAM_READWRITE));
883 * GtkLabel:width-chars:
885 * The desired width of the label, in characters. If this property is set to
886 * -1, the width will be calculated automatically.
888 * See the section on <link linkend="label-text-layout">text layout</link>
889 * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
890 * determine the width of ellipsized and wrapped labels.
894 g_object_class_install_property (gobject_class,
896 g_param_spec_int ("width-chars",
897 P_("Width In Characters"),
898 P_("The desired width of the label, in characters"),
902 GTK_PARAM_READWRITE));
905 * GtkLabel:single-line-mode:
907 * Whether the label is in single line mode. In single line mode,
908 * the height of the label does not depend on the actual text, it
909 * is always set to ascent + descent of the font. This can be an
910 * advantage in situations where resizing the label because of text
911 * changes would be distracting, e.g. in a statusbar.
915 g_object_class_install_property (gobject_class,
916 PROP_SINGLE_LINE_MODE,
917 g_param_spec_boolean ("single-line-mode",
918 P_("Single Line Mode"),
919 P_("Whether the label is in single line mode"),
921 GTK_PARAM_READWRITE));
926 * The angle that the baseline of the label makes with the horizontal,
927 * in degrees, measured counterclockwise. An angle of 90 reads from
928 * from bottom to top, an angle of 270, from top to bottom. Ignored
929 * if the label is selectable, wrapped, or ellipsized.
933 g_object_class_install_property (gobject_class,
935 g_param_spec_double ("angle",
937 P_("Angle at which the label is rotated"),
941 GTK_PARAM_READWRITE));
944 * GtkLabel:max-width-chars:
946 * The desired maximum width of the label, in characters. If this property
947 * is set to -1, the width will be calculated automatically.
949 * See the section on <link linkend="label-text-layout">text layout</link>
950 * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
951 * determine the width of ellipsized and wrapped labels.
955 g_object_class_install_property (gobject_class,
956 PROP_MAX_WIDTH_CHARS,
957 g_param_spec_int ("max-width-chars",
958 P_("Maximum Width In Characters"),
959 P_("The desired maximum width of the label, in characters"),
963 GTK_PARAM_READWRITE));
966 * GtkLabel:track-visited-links:
968 * Set this property to %TRUE to make the label track which links
969 * have been clicked. It will then apply the ::visited-link-color
970 * color, instead of ::link-color.
974 g_object_class_install_property (gobject_class,
975 PROP_TRACK_VISITED_LINKS,
976 g_param_spec_boolean ("track-visited-links",
977 P_("Track visited links"),
978 P_("Whether visited links should be tracked"),
980 GTK_PARAM_READWRITE));
985 binding_set = gtk_binding_set_by_class (class);
987 /* Moving the insertion point */
988 add_move_binding (binding_set, GDK_KEY_Right, 0,
989 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
991 add_move_binding (binding_set, GDK_KEY_Left, 0,
992 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
994 add_move_binding (binding_set, GDK_KEY_KP_Right, 0,
995 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
997 add_move_binding (binding_set, GDK_KEY_KP_Left, 0,
998 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
1000 add_move_binding (binding_set, GDK_KEY_f, GDK_CONTROL_MASK,
1001 GTK_MOVEMENT_LOGICAL_POSITIONS, 1);
1003 add_move_binding (binding_set, GDK_KEY_b, GDK_CONTROL_MASK,
1004 GTK_MOVEMENT_LOGICAL_POSITIONS, -1);
1006 add_move_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK,
1007 GTK_MOVEMENT_WORDS, 1);
1009 add_move_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK,
1010 GTK_MOVEMENT_WORDS, -1);
1012 add_move_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK,
1013 GTK_MOVEMENT_WORDS, 1);
1015 add_move_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK,
1016 GTK_MOVEMENT_WORDS, -1);
1019 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
1021 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1023 G_TYPE_BOOLEAN, FALSE);
1025 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
1027 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1029 G_TYPE_BOOLEAN, TRUE);
1031 gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
1033 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1035 G_TYPE_BOOLEAN, FALSE);
1037 gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
1039 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1041 G_TYPE_BOOLEAN, TRUE);
1044 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1046 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1048 G_TYPE_BOOLEAN, FALSE);
1050 gtk_binding_entry_add_signal (binding_set, GDK_KEY_backslash, GDK_CONTROL_MASK,
1052 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
1054 G_TYPE_BOOLEAN, FALSE);
1056 add_move_binding (binding_set, GDK_KEY_f, GDK_MOD1_MASK,
1057 GTK_MOVEMENT_WORDS, 1);
1059 add_move_binding (binding_set, GDK_KEY_b, GDK_MOD1_MASK,
1060 GTK_MOVEMENT_WORDS, -1);
1062 add_move_binding (binding_set, GDK_KEY_Home, 0,
1063 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1065 add_move_binding (binding_set, GDK_KEY_End, 0,
1066 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1068 add_move_binding (binding_set, GDK_KEY_KP_Home, 0,
1069 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1071 add_move_binding (binding_set, GDK_KEY_KP_End, 0,
1072 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1074 add_move_binding (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK,
1075 GTK_MOVEMENT_BUFFER_ENDS, -1);
1077 add_move_binding (binding_set, GDK_KEY_End, GDK_CONTROL_MASK,
1078 GTK_MOVEMENT_BUFFER_ENDS, 1);
1080 add_move_binding (binding_set, GDK_KEY_KP_Home, GDK_CONTROL_MASK,
1081 GTK_MOVEMENT_BUFFER_ENDS, -1);
1083 add_move_binding (binding_set, GDK_KEY_KP_End, GDK_CONTROL_MASK,
1084 GTK_MOVEMENT_BUFFER_ENDS, 1);
1087 gtk_binding_entry_add_signal (binding_set, GDK_KEY_c, GDK_CONTROL_MASK,
1088 "copy-clipboard", 0);
1090 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Return, 0,
1091 "activate-current-link", 0);
1092 gtk_binding_entry_add_signal (binding_set, GDK_KEY_ISO_Enter, 0,
1093 "activate-current-link", 0);
1094 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Enter, 0,
1095 "activate-current-link", 0);
1097 g_type_class_add_private (class, sizeof (GtkLabelPrivate));
1101 gtk_label_set_property (GObject *object,
1103 const GValue *value,
1106 GtkLabel *label = GTK_LABEL (object);
1111 gtk_label_set_label (label, g_value_get_string (value));
1113 case PROP_ATTRIBUTES:
1114 gtk_label_set_attributes (label, g_value_get_boxed (value));
1116 case PROP_USE_MARKUP:
1117 gtk_label_set_use_markup (label, g_value_get_boolean (value));
1119 case PROP_USE_UNDERLINE:
1120 gtk_label_set_use_underline (label, g_value_get_boolean (value));
1123 gtk_label_set_justify (label, g_value_get_enum (value));
1126 gtk_label_set_pattern (label, g_value_get_string (value));
1129 gtk_label_set_line_wrap (label, g_value_get_boolean (value));
1131 case PROP_WRAP_MODE:
1132 gtk_label_set_line_wrap_mode (label, g_value_get_enum (value));
1134 case PROP_SELECTABLE:
1135 gtk_label_set_selectable (label, g_value_get_boolean (value));
1137 case PROP_MNEMONIC_WIDGET:
1138 gtk_label_set_mnemonic_widget (label, (GtkWidget*) g_value_get_object (value));
1140 case PROP_ELLIPSIZE:
1141 gtk_label_set_ellipsize (label, g_value_get_enum (value));
1143 case PROP_WIDTH_CHARS:
1144 gtk_label_set_width_chars (label, g_value_get_int (value));
1146 case PROP_SINGLE_LINE_MODE:
1147 gtk_label_set_single_line_mode (label, g_value_get_boolean (value));
1150 gtk_label_set_angle (label, g_value_get_double (value));
1152 case PROP_MAX_WIDTH_CHARS:
1153 gtk_label_set_max_width_chars (label, g_value_get_int (value));
1155 case PROP_TRACK_VISITED_LINKS:
1156 gtk_label_set_track_visited_links (label, g_value_get_boolean (value));
1159 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1165 gtk_label_get_property (GObject *object,
1170 GtkLabel *label = GTK_LABEL (object);
1171 GtkLabelPrivate *priv = label->priv;
1176 g_value_set_string (value, priv->label);
1178 case PROP_ATTRIBUTES:
1179 g_value_set_boxed (value, priv->attrs);
1181 case PROP_USE_MARKUP:
1182 g_value_set_boolean (value, priv->use_markup);
1184 case PROP_USE_UNDERLINE:
1185 g_value_set_boolean (value, priv->use_underline);
1188 g_value_set_enum (value, priv->jtype);
1191 g_value_set_boolean (value, priv->wrap);
1193 case PROP_WRAP_MODE:
1194 g_value_set_enum (value, priv->wrap_mode);
1196 case PROP_SELECTABLE:
1197 g_value_set_boolean (value, gtk_label_get_selectable (label));
1199 case PROP_MNEMONIC_KEYVAL:
1200 g_value_set_uint (value, priv->mnemonic_keyval);
1202 case PROP_MNEMONIC_WIDGET:
1203 g_value_set_object (value, (GObject*) priv->mnemonic_widget);
1205 case PROP_CURSOR_POSITION:
1206 if (priv->select_info && priv->select_info->selectable)
1208 gint offset = g_utf8_pointer_to_offset (priv->text,
1209 priv->text + priv->select_info->selection_end);
1210 g_value_set_int (value, offset);
1213 g_value_set_int (value, 0);
1215 case PROP_SELECTION_BOUND:
1216 if (priv->select_info && priv->select_info->selectable)
1218 gint offset = g_utf8_pointer_to_offset (priv->text,
1219 priv->text + priv->select_info->selection_anchor);
1220 g_value_set_int (value, offset);
1223 g_value_set_int (value, 0);
1225 case PROP_ELLIPSIZE:
1226 g_value_set_enum (value, priv->ellipsize);
1228 case PROP_WIDTH_CHARS:
1229 g_value_set_int (value, gtk_label_get_width_chars (label));
1231 case PROP_SINGLE_LINE_MODE:
1232 g_value_set_boolean (value, gtk_label_get_single_line_mode (label));
1235 g_value_set_double (value, gtk_label_get_angle (label));
1237 case PROP_MAX_WIDTH_CHARS:
1238 g_value_set_int (value, gtk_label_get_max_width_chars (label));
1240 case PROP_TRACK_VISITED_LINKS:
1241 g_value_set_boolean (value, gtk_label_get_track_visited_links (label));
1244 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1250 gtk_label_init (GtkLabel *label)
1252 GtkLabelPrivate *priv;
1254 label->priv = G_TYPE_INSTANCE_GET_PRIVATE (label,
1259 gtk_widget_set_has_window (GTK_WIDGET (label), FALSE);
1261 priv->width_chars = -1;
1262 priv->max_width_chars = -1;
1263 priv->wrap_width = -1;
1266 priv->jtype = GTK_JUSTIFY_LEFT;
1268 priv->wrap_mode = PANGO_WRAP_WORD;
1269 priv->ellipsize = PANGO_ELLIPSIZE_NONE;
1271 priv->use_underline = FALSE;
1272 priv->use_markup = FALSE;
1273 priv->pattern_set = FALSE;
1274 priv->track_links = TRUE;
1276 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
1277 priv->layout = NULL;
1281 priv->mnemonic_widget = NULL;
1282 priv->mnemonic_window = NULL;
1284 priv->mnemonics_visible = TRUE;
1286 gtk_label_set_text (label, "");
1291 gtk_label_buildable_interface_init (GtkBuildableIface *iface)
1293 buildable_parent_iface = g_type_interface_peek_parent (iface);
1295 iface->custom_tag_start = gtk_label_buildable_custom_tag_start;
1296 iface->custom_finished = gtk_label_buildable_custom_finished;
1300 GtkBuilder *builder;
1302 PangoAttrList *attrs;
1305 static PangoAttribute *
1306 attribute_from_text (GtkBuilder *builder,
1311 PangoAttribute *attribute = NULL;
1313 PangoLanguage *language;
1314 PangoFontDescription *font_desc;
1316 GValue val = { 0, };
1318 if (!gtk_builder_value_from_string_type (builder, PANGO_TYPE_ATTR_TYPE, name, &val, error))
1321 type = g_value_get_enum (&val);
1322 g_value_unset (&val);
1326 /* PangoAttrLanguage */
1327 case PANGO_ATTR_LANGUAGE:
1328 if ((language = pango_language_from_string (value)))
1330 attribute = pango_attr_language_new (language);
1331 g_value_init (&val, G_TYPE_INT);
1335 case PANGO_ATTR_STYLE:
1336 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STYLE, value, &val, error))
1337 attribute = pango_attr_style_new (g_value_get_enum (&val));
1339 case PANGO_ATTR_WEIGHT:
1340 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_WEIGHT, value, &val, error))
1341 attribute = pango_attr_weight_new (g_value_get_enum (&val));
1343 case PANGO_ATTR_VARIANT:
1344 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_VARIANT, value, &val, error))
1345 attribute = pango_attr_variant_new (g_value_get_enum (&val));
1347 case PANGO_ATTR_STRETCH:
1348 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STRETCH, value, &val, error))
1349 attribute = pango_attr_stretch_new (g_value_get_enum (&val));
1351 case PANGO_ATTR_UNDERLINE:
1352 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1353 attribute = pango_attr_underline_new (g_value_get_boolean (&val));
1355 case PANGO_ATTR_STRIKETHROUGH:
1356 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1357 attribute = pango_attr_strikethrough_new (g_value_get_boolean (&val));
1359 case PANGO_ATTR_GRAVITY:
1360 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY, value, &val, error))
1361 attribute = pango_attr_gravity_new (g_value_get_enum (&val));
1363 case PANGO_ATTR_GRAVITY_HINT:
1364 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY_HINT,
1365 value, &val, error))
1366 attribute = pango_attr_gravity_hint_new (g_value_get_enum (&val));
1368 /* PangoAttrString */
1369 case PANGO_ATTR_FAMILY:
1370 attribute = pango_attr_family_new (value);
1371 g_value_init (&val, G_TYPE_INT);
1375 case PANGO_ATTR_SIZE:
1376 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1377 value, &val, error))
1378 attribute = pango_attr_size_new (g_value_get_int (&val));
1380 case PANGO_ATTR_ABSOLUTE_SIZE:
1381 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1382 value, &val, error))
1383 attribute = pango_attr_size_new_absolute (g_value_get_int (&val));
1386 /* PangoAttrFontDesc */
1387 case PANGO_ATTR_FONT_DESC:
1388 if ((font_desc = pango_font_description_from_string (value)))
1390 attribute = pango_attr_font_desc_new (font_desc);
1391 pango_font_description_free (font_desc);
1392 g_value_init (&val, G_TYPE_INT);
1396 /* PangoAttrColor */
1397 case PANGO_ATTR_FOREGROUND:
1398 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1399 value, &val, error))
1401 color = g_value_get_boxed (&val);
1402 attribute = pango_attr_foreground_new (color->red, color->green, color->blue);
1405 case PANGO_ATTR_BACKGROUND:
1406 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1407 value, &val, error))
1409 color = g_value_get_boxed (&val);
1410 attribute = pango_attr_background_new (color->red, color->green, color->blue);
1413 case PANGO_ATTR_UNDERLINE_COLOR:
1414 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1415 value, &val, error))
1417 color = g_value_get_boxed (&val);
1418 attribute = pango_attr_underline_color_new (color->red, color->green, color->blue);
1421 case PANGO_ATTR_STRIKETHROUGH_COLOR:
1422 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1423 value, &val, error))
1425 color = g_value_get_boxed (&val);
1426 attribute = pango_attr_strikethrough_color_new (color->red, color->green, color->blue);
1430 /* PangoAttrShape */
1431 case PANGO_ATTR_SHAPE:
1432 /* Unsupported for now */
1434 /* PangoAttrFloat */
1435 case PANGO_ATTR_SCALE:
1436 if (gtk_builder_value_from_string_type (builder, G_TYPE_DOUBLE,
1437 value, &val, error))
1438 attribute = pango_attr_scale_new (g_value_get_double (&val));
1441 case PANGO_ATTR_INVALID:
1442 case PANGO_ATTR_LETTER_SPACING:
1443 case PANGO_ATTR_RISE:
1444 case PANGO_ATTR_FALLBACK:
1449 g_value_unset (&val);
1456 pango_start_element (GMarkupParseContext *context,
1457 const gchar *element_name,
1458 const gchar **names,
1459 const gchar **values,
1463 PangoParserData *data = (PangoParserData*)user_data;
1464 GValue val = { 0, };
1466 gint line_number, char_number;
1468 if (strcmp (element_name, "attribute") == 0)
1470 PangoAttribute *attr = NULL;
1471 const gchar *name = NULL;
1472 const gchar *value = NULL;
1473 const gchar *start = NULL;
1474 const gchar *end = NULL;
1475 guint start_val = 0;
1476 guint end_val = G_MAXUINT;
1478 for (i = 0; names[i]; i++)
1480 if (strcmp (names[i], "name") == 0)
1482 else if (strcmp (names[i], "value") == 0)
1484 else if (strcmp (names[i], "start") == 0)
1486 else if (strcmp (names[i], "end") == 0)
1490 g_markup_parse_context_get_position (context,
1495 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1496 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1498 line_number, char_number, names[i], "attribute");
1503 if (!name || !value)
1505 g_markup_parse_context_get_position (context,
1510 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1511 "%s:%d:%d <%s> requires attribute \"%s\"",
1513 line_number, char_number, "attribute",
1514 name ? "value" : "name");
1520 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1521 start, &val, error))
1523 start_val = g_value_get_uint (&val);
1524 g_value_unset (&val);
1529 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1532 end_val = g_value_get_uint (&val);
1533 g_value_unset (&val);
1536 attr = attribute_from_text (data->builder, name, value, error);
1537 attr->start_index = start_val;
1538 attr->end_index = end_val;
1543 data->attrs = pango_attr_list_new ();
1545 pango_attr_list_insert (data->attrs, attr);
1548 else if (strcmp (element_name, "attributes") == 0)
1551 g_warning ("Unsupported tag for GtkLabel: %s\n", element_name);
1554 static const GMarkupParser pango_parser =
1556 pango_start_element,
1560 gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
1561 GtkBuilder *builder,
1563 const gchar *tagname,
1564 GMarkupParser *parser,
1567 if (buildable_parent_iface->custom_tag_start (buildable, builder, child,
1568 tagname, parser, data))
1571 if (strcmp (tagname, "attributes") == 0)
1573 PangoParserData *parser_data;
1575 parser_data = g_slice_new0 (PangoParserData);
1576 parser_data->builder = g_object_ref (builder);
1577 parser_data->object = g_object_ref (buildable);
1578 *parser = pango_parser;
1579 *data = parser_data;
1586 gtk_label_buildable_custom_finished (GtkBuildable *buildable,
1587 GtkBuilder *builder,
1589 const gchar *tagname,
1592 PangoParserData *data;
1594 buildable_parent_iface->custom_finished (buildable, builder, child,
1595 tagname, user_data);
1597 if (strcmp (tagname, "attributes") == 0)
1599 data = (PangoParserData*)user_data;
1603 gtk_label_set_attributes (GTK_LABEL (buildable), data->attrs);
1604 pango_attr_list_unref (data->attrs);
1607 g_object_unref (data->object);
1608 g_object_unref (data->builder);
1609 g_slice_free (PangoParserData, data);
1616 * @str: The text of the label
1618 * Creates a new label with the given text inside it. You can
1619 * pass %NULL to get an empty label widget.
1621 * Return value: the new #GtkLabel
1624 gtk_label_new (const gchar *str)
1628 label = g_object_new (GTK_TYPE_LABEL, NULL);
1631 gtk_label_set_text (label, str);
1633 return GTK_WIDGET (label);
1637 * gtk_label_new_with_mnemonic:
1638 * @str: The text of the label, with an underscore in front of the
1639 * mnemonic character
1641 * Creates a new #GtkLabel, containing the text in @str.
1643 * If characters in @str are preceded by an underscore, they are
1644 * underlined. If you need a literal underscore character in a label, use
1645 * '__' (two underscores). The first underlined character represents a
1646 * keyboard accelerator called a mnemonic. The mnemonic key can be used
1647 * to activate another widget, chosen automatically, or explicitly using
1648 * gtk_label_set_mnemonic_widget().
1650 * If gtk_label_set_mnemonic_widget() is not called, then the first
1651 * activatable ancestor of the #GtkLabel will be chosen as the mnemonic
1652 * widget. For instance, if the label is inside a button or menu item,
1653 * the button or menu item will automatically become the mnemonic widget
1654 * and be activated by the mnemonic.
1656 * Return value: the new #GtkLabel
1659 gtk_label_new_with_mnemonic (const gchar *str)
1663 label = g_object_new (GTK_TYPE_LABEL, NULL);
1666 gtk_label_set_text_with_mnemonic (label, str);
1668 return GTK_WIDGET (label);
1672 gtk_label_mnemonic_activate (GtkWidget *widget,
1673 gboolean group_cycling)
1675 GtkLabel *label = GTK_LABEL (widget);
1676 GtkLabelPrivate *priv = label->priv;
1679 if (priv->mnemonic_widget)
1680 return gtk_widget_mnemonic_activate (priv->mnemonic_widget, group_cycling);
1682 /* Try to find the widget to activate by traversing the
1683 * widget's ancestry.
1685 parent = gtk_widget_get_parent (widget);
1687 if (GTK_IS_NOTEBOOK (parent))
1692 if (gtk_widget_get_can_focus (parent) ||
1693 (!group_cycling && GTK_WIDGET_GET_CLASS (parent)->activate_signal) ||
1694 GTK_IS_NOTEBOOK (gtk_widget_get_parent (parent)) ||
1695 GTK_IS_MENU_ITEM (parent))
1696 return gtk_widget_mnemonic_activate (parent, group_cycling);
1697 parent = gtk_widget_get_parent (parent);
1700 /* barf if there was nothing to activate */
1701 g_warning ("Couldn't find a target for a mnemonic activation.");
1702 gtk_widget_error_bell (widget);
1708 gtk_label_setup_mnemonic (GtkLabel *label,
1711 GtkLabelPrivate *priv = label->priv;
1712 GtkWidget *widget = GTK_WIDGET (label);
1713 GtkWidget *toplevel;
1714 GtkWidget *mnemonic_menu;
1716 mnemonic_menu = g_object_get_data (G_OBJECT (label), "gtk-mnemonic-menu");
1718 if (last_key != GDK_KEY_VoidSymbol)
1720 if (priv->mnemonic_window)
1722 gtk_window_remove_mnemonic (priv->mnemonic_window,
1725 priv->mnemonic_window = NULL;
1729 _gtk_menu_shell_remove_mnemonic (GTK_MENU_SHELL (mnemonic_menu),
1732 mnemonic_menu = NULL;
1736 if (priv->mnemonic_keyval == GDK_KEY_VoidSymbol)
1739 connect_mnemonics_visible_notify (GTK_LABEL (widget));
1741 toplevel = gtk_widget_get_toplevel (widget);
1742 if (gtk_widget_is_toplevel (toplevel))
1744 GtkWidget *menu_shell;
1746 menu_shell = gtk_widget_get_ancestor (widget,
1747 GTK_TYPE_MENU_SHELL);
1751 _gtk_menu_shell_add_mnemonic (GTK_MENU_SHELL (menu_shell),
1752 priv->mnemonic_keyval,
1754 mnemonic_menu = menu_shell;
1757 if (!GTK_IS_MENU (menu_shell))
1759 gtk_window_add_mnemonic (GTK_WINDOW (toplevel),
1760 priv->mnemonic_keyval,
1762 priv->mnemonic_window = GTK_WINDOW (toplevel);
1767 g_object_set_data (G_OBJECT (label), I_("gtk-mnemonic-menu"), mnemonic_menu);
1771 gtk_label_hierarchy_changed (GtkWidget *widget,
1772 GtkWidget *old_toplevel)
1774 GtkLabel *label = GTK_LABEL (widget);
1775 GtkLabelPrivate *priv = label->priv;
1777 gtk_label_setup_mnemonic (label, priv->mnemonic_keyval);
1781 label_shortcut_setting_apply (GtkLabel *label)
1783 gtk_label_recalculate (label);
1784 if (GTK_IS_ACCEL_LABEL (label))
1785 gtk_accel_label_refetch (GTK_ACCEL_LABEL (label));
1789 label_shortcut_setting_traverse_container (GtkWidget *widget,
1792 if (GTK_IS_LABEL (widget))
1793 label_shortcut_setting_apply (GTK_LABEL (widget));
1794 else if (GTK_IS_CONTAINER (widget))
1795 gtk_container_forall (GTK_CONTAINER (widget),
1796 label_shortcut_setting_traverse_container, data);
1800 label_shortcut_setting_changed (GtkSettings *settings)
1804 list = gtk_window_list_toplevels ();
1806 for (l = list; l ; l = l->next)
1808 GtkWidget *widget = l->data;
1810 if (gtk_widget_get_settings (widget) == settings)
1811 gtk_container_forall (GTK_CONTAINER (widget),
1812 label_shortcut_setting_traverse_container, NULL);
1819 mnemonics_visible_apply (GtkWidget *widget,
1820 gboolean mnemonics_visible)
1822 GtkLabel *label = GTK_LABEL (widget);
1823 GtkLabelPrivate *priv = label->priv;
1825 mnemonics_visible = mnemonics_visible != FALSE;
1827 if (priv->mnemonics_visible != mnemonics_visible)
1829 priv->mnemonics_visible = mnemonics_visible;
1831 gtk_label_recalculate (label);
1836 label_mnemonics_visible_traverse_container (GtkWidget *widget,
1839 gboolean mnemonics_visible = GPOINTER_TO_INT (data);
1841 _gtk_label_mnemonics_visible_apply_recursively (widget, mnemonics_visible);
1845 _gtk_label_mnemonics_visible_apply_recursively (GtkWidget *widget,
1846 gboolean mnemonics_visible)
1848 if (GTK_IS_LABEL (widget))
1849 mnemonics_visible_apply (widget, mnemonics_visible);
1850 else if (GTK_IS_CONTAINER (widget))
1851 gtk_container_forall (GTK_CONTAINER (widget),
1852 label_mnemonics_visible_traverse_container,
1853 GINT_TO_POINTER (mnemonics_visible));
1857 label_mnemonics_visible_changed (GtkWindow *window,
1861 gboolean mnemonics_visible;
1863 g_object_get (window, "mnemonics-visible", &mnemonics_visible, NULL);
1865 gtk_container_forall (GTK_CONTAINER (window),
1866 label_mnemonics_visible_traverse_container,
1867 GINT_TO_POINTER (mnemonics_visible));
1871 gtk_label_screen_changed (GtkWidget *widget,
1872 GdkScreen *old_screen)
1874 GtkSettings *settings;
1875 gboolean shortcuts_connected;
1877 if (!gtk_widget_has_screen (widget))
1880 settings = gtk_widget_get_settings (widget);
1882 shortcuts_connected =
1883 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (settings),
1884 "gtk-label-shortcuts-connected"));
1886 if (! shortcuts_connected)
1888 g_signal_connect (settings, "notify::gtk-enable-mnemonics",
1889 G_CALLBACK (label_shortcut_setting_changed),
1891 g_signal_connect (settings, "notify::gtk-enable-accels",
1892 G_CALLBACK (label_shortcut_setting_changed),
1895 g_object_set_data (G_OBJECT (settings), "gtk-label-shortcuts-connected",
1896 GINT_TO_POINTER (TRUE));
1899 label_shortcut_setting_apply (GTK_LABEL (widget));
1904 label_mnemonic_widget_weak_notify (gpointer data,
1905 GObject *where_the_object_was)
1907 GtkLabel *label = data;
1908 GtkLabelPrivate *priv = label->priv;
1910 priv->mnemonic_widget = NULL;
1911 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1915 * gtk_label_set_mnemonic_widget:
1916 * @label: a #GtkLabel
1917 * @widget: (allow-none): the target #GtkWidget
1919 * If the label has been set so that it has an mnemonic key (using
1920 * i.e. gtk_label_set_markup_with_mnemonic(),
1921 * gtk_label_set_text_with_mnemonic(), gtk_label_new_with_mnemonic()
1922 * or the "use_underline" property) the label can be associated with a
1923 * widget that is the target of the mnemonic. When the label is inside
1924 * a widget (like a #GtkButton or a #GtkNotebook tab) it is
1925 * automatically associated with the correct widget, but sometimes
1926 * (i.e. when the target is a #GtkEntry next to the label) you need to
1927 * set it explicitly using this function.
1929 * The target widget will be accelerated by emitting the
1930 * GtkWidget::mnemonic-activate signal on it. The default handler for
1931 * this signal will activate the widget if there are no mnemonic collisions
1932 * and toggle focus between the colliding widgets otherwise.
1935 gtk_label_set_mnemonic_widget (GtkLabel *label,
1938 GtkLabelPrivate *priv;
1940 g_return_if_fail (GTK_IS_LABEL (label));
1945 g_return_if_fail (GTK_IS_WIDGET (widget));
1947 if (priv->mnemonic_widget)
1949 gtk_widget_remove_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1950 g_object_weak_unref (G_OBJECT (priv->mnemonic_widget),
1951 label_mnemonic_widget_weak_notify,
1954 priv->mnemonic_widget = widget;
1955 if (priv->mnemonic_widget)
1957 g_object_weak_ref (G_OBJECT (priv->mnemonic_widget),
1958 label_mnemonic_widget_weak_notify,
1960 gtk_widget_add_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1963 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1967 * gtk_label_get_mnemonic_widget:
1968 * @label: a #GtkLabel
1970 * Retrieves the target of the mnemonic (keyboard shortcut) of this
1971 * label. See gtk_label_set_mnemonic_widget().
1973 * Return value: (transfer none): the target of the label's mnemonic,
1974 * or %NULL if none has been set and the default algorithm will be used.
1977 gtk_label_get_mnemonic_widget (GtkLabel *label)
1979 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1981 return label->priv->mnemonic_widget;
1985 * gtk_label_get_mnemonic_keyval:
1986 * @label: a #GtkLabel
1988 * If the label has been set so that it has an mnemonic key this function
1989 * returns the keyval used for the mnemonic accelerator. If there is no
1990 * mnemonic set up it returns #GDK_VoidSymbol.
1992 * Returns: GDK keyval usable for accelerators, or #GDK_VoidSymbol
1995 gtk_label_get_mnemonic_keyval (GtkLabel *label)
1997 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_KEY_VoidSymbol);
1999 return label->priv->mnemonic_keyval;
2003 gtk_label_set_text_internal (GtkLabel *label,
2006 GtkLabelPrivate *priv = label->priv;
2008 g_free (priv->text);
2012 gtk_label_select_region_index (label, 0, 0);
2016 gtk_label_set_label_internal (GtkLabel *label,
2019 GtkLabelPrivate *priv = label->priv;
2021 g_free (priv->label);
2025 g_object_notify (G_OBJECT (label), "label");
2029 gtk_label_set_use_markup_internal (GtkLabel *label,
2032 GtkLabelPrivate *priv = label->priv;
2035 if (priv->use_markup != val)
2037 priv->use_markup = val;
2039 g_object_notify (G_OBJECT (label), "use-markup");
2044 gtk_label_set_use_underline_internal (GtkLabel *label,
2047 GtkLabelPrivate *priv = label->priv;
2050 if (priv->use_underline != val)
2052 priv->use_underline = val;
2054 g_object_notify (G_OBJECT (label), "use-underline");
2059 gtk_label_compose_effective_attrs (GtkLabel *label)
2061 GtkLabelPrivate *priv = label->priv;
2062 PangoAttrIterator *iter;
2063 PangoAttribute *attr;
2064 GSList *iter_attrs, *l;
2068 if (priv->effective_attrs)
2070 if ((iter = pango_attr_list_get_iterator (priv->attrs)))
2074 iter_attrs = pango_attr_iterator_get_attrs (iter);
2075 for (l = iter_attrs; l; l = l->next)
2078 pango_attr_list_insert (priv->effective_attrs, attr);
2080 g_slist_free (iter_attrs);
2082 while (pango_attr_iterator_next (iter));
2083 pango_attr_iterator_destroy (iter);
2087 priv->effective_attrs =
2088 pango_attr_list_ref (priv->attrs);
2093 gtk_label_set_attributes_internal (GtkLabel *label,
2094 PangoAttrList *attrs)
2096 GtkLabelPrivate *priv = label->priv;
2099 pango_attr_list_ref (attrs);
2102 pango_attr_list_unref (priv->attrs);
2103 priv->attrs = attrs;
2105 g_object_notify (G_OBJECT (label), "attributes");
2109 /* Calculates text, attrs and mnemonic_keyval from
2110 * label, use_underline and use_markup
2113 gtk_label_recalculate (GtkLabel *label)
2115 GtkLabelPrivate *priv = label->priv;
2116 guint keyval = priv->mnemonic_keyval;
2118 if (priv->use_markup)
2119 gtk_label_set_markup_internal (label, priv->label, priv->use_underline);
2122 if (priv->use_underline)
2123 gtk_label_set_uline_text_internal (label, priv->label);
2126 if (priv->effective_attrs)
2127 pango_attr_list_unref (priv->effective_attrs);
2128 priv->effective_attrs = NULL;
2129 gtk_label_set_text_internal (label, g_strdup (priv->label));
2133 gtk_label_compose_effective_attrs (label);
2135 if (!priv->use_underline)
2136 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
2138 if (keyval != priv->mnemonic_keyval)
2140 gtk_label_setup_mnemonic (label, keyval);
2141 g_object_notify (G_OBJECT (label), "mnemonic-keyval");
2144 gtk_label_clear_layout (label);
2145 gtk_label_clear_select_info (label);
2146 gtk_widget_queue_resize (GTK_WIDGET (label));
2150 * gtk_label_set_text:
2151 * @label: a #GtkLabel
2152 * @str: The text you want to set
2154 * Sets the text within the #GtkLabel widget. It overwrites any text that
2157 * This will also clear any previously set mnemonic accelerators.
2160 gtk_label_set_text (GtkLabel *label,
2163 g_return_if_fail (GTK_IS_LABEL (label));
2165 g_object_freeze_notify (G_OBJECT (label));
2167 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2168 gtk_label_set_use_markup_internal (label, FALSE);
2169 gtk_label_set_use_underline_internal (label, FALSE);
2171 gtk_label_recalculate (label);
2173 g_object_thaw_notify (G_OBJECT (label));
2177 * gtk_label_set_attributes:
2178 * @label: a #GtkLabel
2179 * @attrs: a #PangoAttrList
2181 * Sets a #PangoAttrList; the attributes in the list are applied to the
2184 * <note><para>The attributes set with this function will be applied
2185 * and merged with any other attributes previously effected by way
2186 * of the #GtkLabel:use-underline or #GtkLabel:use-markup properties.
2187 * While it is not recommended to mix markup strings with manually set
2188 * attributes, if you must; know that the attributes will be applied
2189 * to the label after the markup string is parsed.</para></note>
2192 gtk_label_set_attributes (GtkLabel *label,
2193 PangoAttrList *attrs)
2195 g_return_if_fail (GTK_IS_LABEL (label));
2197 gtk_label_set_attributes_internal (label, attrs);
2199 gtk_label_recalculate (label);
2201 gtk_label_clear_layout (label);
2202 gtk_widget_queue_resize (GTK_WIDGET (label));
2206 * gtk_label_get_attributes:
2207 * @label: a #GtkLabel
2209 * Gets the attribute list that was set on the label using
2210 * gtk_label_set_attributes(), if any. This function does
2211 * not reflect attributes that come from the labels markup
2212 * (see gtk_label_set_markup()). If you want to get the
2213 * effective attributes for the label, use
2214 * pango_layout_get_attribute (gtk_label_get_layout (label)).
2216 * Return value: (transfer none): the attribute list, or %NULL
2220 gtk_label_get_attributes (GtkLabel *label)
2222 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2224 return label->priv->attrs;
2228 * gtk_label_set_label:
2229 * @label: a #GtkLabel
2230 * @str: the new text to set for the label
2232 * Sets the text of the label. The label is interpreted as
2233 * including embedded underlines and/or Pango markup depending
2234 * on the values of the #GtkLabel:use-underline" and
2235 * #GtkLabel:use-markup properties.
2238 gtk_label_set_label (GtkLabel *label,
2241 g_return_if_fail (GTK_IS_LABEL (label));
2243 g_object_freeze_notify (G_OBJECT (label));
2245 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2246 gtk_label_recalculate (label);
2248 g_object_thaw_notify (G_OBJECT (label));
2252 * gtk_label_get_label:
2253 * @label: a #GtkLabel
2255 * Fetches the text from a label widget including any embedded
2256 * underlines indicating mnemonics and Pango markup. (See
2257 * gtk_label_get_text()).
2259 * Return value: the text of the label widget. This string is
2260 * owned by the widget and must not be modified or freed.
2262 G_CONST_RETURN gchar *
2263 gtk_label_get_label (GtkLabel *label)
2265 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2267 return label->priv->label;
2275 GdkColor *link_color;
2276 GdkColor *visited_link_color;
2280 start_element_handler (GMarkupParseContext *context,
2281 const gchar *element_name,
2282 const gchar **attribute_names,
2283 const gchar **attribute_values,
2287 GtkLabelPrivate *priv;
2288 UriParserData *pdata = user_data;
2290 if (strcmp (element_name, "a") == 0)
2293 const gchar *uri = NULL;
2294 const gchar *title = NULL;
2295 gboolean visited = FALSE;
2299 GdkColor *color = NULL;
2301 g_markup_parse_context_get_position (context, &line_number, &char_number);
2303 for (i = 0; attribute_names[i] != NULL; i++)
2305 const gchar *attr = attribute_names[i];
2307 if (strcmp (attr, "href") == 0)
2308 uri = attribute_values[i];
2309 else if (strcmp (attr, "title") == 0)
2310 title = attribute_values[i];
2315 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
2316 "Attribute '%s' is not allowed on the <a> tag "
2317 "on line %d char %d",
2318 attr, line_number, char_number);
2327 G_MARKUP_ERROR_INVALID_CONTENT,
2328 "Attribute 'href' was missing on the <a> tag "
2329 "on line %d char %d",
2330 line_number, char_number);
2335 priv = pdata->label->priv;
2336 if (priv->track_links && priv->select_info)
2339 for (l = priv->select_info->links; l; l = l->next)
2342 if (strcmp (uri, link->uri) == 0)
2344 visited = link->visited;
2351 color = pdata->visited_link_color;
2353 color = pdata->link_color;
2355 g_string_append_printf (pdata->new_str,
2356 "<span color=\"#%04x%04x%04x\" underline=\"single\">",
2361 link = g_new0 (GtkLabelLink, 1);
2362 link->uri = g_strdup (uri);
2363 link->title = g_strdup (title);
2364 link->visited = visited;
2365 pdata->links = g_list_append (pdata->links, link);
2371 g_string_append_c (pdata->new_str, '<');
2372 g_string_append (pdata->new_str, element_name);
2374 for (i = 0; attribute_names[i] != NULL; i++)
2376 const gchar *attr = attribute_names[i];
2377 const gchar *value = attribute_values[i];
2380 newvalue = g_markup_escape_text (value, -1);
2382 g_string_append_c (pdata->new_str, ' ');
2383 g_string_append (pdata->new_str, attr);
2384 g_string_append (pdata->new_str, "=\"");
2385 g_string_append (pdata->new_str, newvalue);
2386 g_string_append_c (pdata->new_str, '\"');
2390 g_string_append_c (pdata->new_str, '>');
2395 end_element_handler (GMarkupParseContext *context,
2396 const gchar *element_name,
2400 UriParserData *pdata = user_data;
2402 if (!strcmp (element_name, "a"))
2403 g_string_append (pdata->new_str, "</span>");
2406 g_string_append (pdata->new_str, "</");
2407 g_string_append (pdata->new_str, element_name);
2408 g_string_append_c (pdata->new_str, '>');
2413 text_handler (GMarkupParseContext *context,
2419 UriParserData *pdata = user_data;
2422 newtext = g_markup_escape_text (text, text_len);
2423 g_string_append (pdata->new_str, newtext);
2427 static const GMarkupParser markup_parser =
2429 start_element_handler,
2430 end_element_handler,
2437 xml_isspace (gchar c)
2439 return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
2443 link_free (GtkLabelLink *link)
2446 g_free (link->title);
2451 gtk_label_get_link_colors (GtkWidget *widget,
2452 GdkColor **link_color,
2453 GdkColor **visited_link_color)
2455 GtkStyleContext *context;
2457 context = gtk_widget_get_style_context (widget);
2458 gtk_style_context_get_style (context,
2459 "link-color", link_color,
2460 "visited-link-color", visited_link_color,
2463 *link_color = gdk_color_copy (&default_link_color);
2464 if (!*visited_link_color)
2465 *visited_link_color = gdk_color_copy (&default_visited_link_color);
2469 parse_uri_markup (GtkLabel *label,
2475 GMarkupParseContext *context = NULL;
2476 const gchar *p, *end;
2477 gboolean needs_root = TRUE;
2479 UriParserData pdata;
2481 length = strlen (str);
2485 pdata.label = label;
2487 pdata.new_str = g_string_sized_new (length);
2489 gtk_label_get_link_colors (GTK_WIDGET (label), &pdata.link_color, &pdata.visited_link_color);
2491 while (p != end && xml_isspace (*p))
2494 if (end - p >= 8 && strncmp (p, "<markup>", 8) == 0)
2497 context = g_markup_parse_context_new (&markup_parser, 0, &pdata, NULL);
2501 if (!g_markup_parse_context_parse (context, "<markup>", -1, error))
2505 if (!g_markup_parse_context_parse (context, str, length, error))
2510 if (!g_markup_parse_context_parse (context, "</markup>", -1, error))
2514 if (!g_markup_parse_context_end_parse (context, error))
2517 g_markup_parse_context_free (context);
2519 *new_str = g_string_free (pdata.new_str, FALSE);
2520 *links = pdata.links;
2522 gdk_color_free (pdata.link_color);
2523 gdk_color_free (pdata.visited_link_color);
2528 g_markup_parse_context_free (context);
2529 g_string_free (pdata.new_str, TRUE);
2530 g_list_foreach (pdata.links, (GFunc)link_free, NULL);
2531 g_list_free (pdata.links);
2532 gdk_color_free (pdata.link_color);
2533 gdk_color_free (pdata.visited_link_color);
2539 gtk_label_ensure_has_tooltip (GtkLabel *label)
2541 GtkLabelPrivate *priv = label->priv;
2543 gboolean has_tooltip = FALSE;
2545 for (l = priv->select_info->links; l; l = l->next)
2547 GtkLabelLink *link = l->data;
2555 gtk_widget_set_has_tooltip (GTK_WIDGET (label), has_tooltip);
2559 gtk_label_set_markup_internal (GtkLabel *label,
2561 gboolean with_uline)
2563 GtkLabelPrivate *priv = label->priv;
2565 GError *error = NULL;
2566 PangoAttrList *attrs = NULL;
2567 gunichar accel_char = 0;
2569 GList *links = NULL;
2571 if (!parse_uri_markup (label, str, &new_str, &links, &error))
2573 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2575 g_error_free (error);
2579 gtk_label_clear_links (label);
2582 gtk_label_ensure_select_info (label);
2583 priv->select_info->links = links;
2584 gtk_label_ensure_has_tooltip (label);
2589 gboolean enable_mnemonics;
2590 gboolean auto_mnemonics;
2592 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2593 "gtk-enable-mnemonics", &enable_mnemonics,
2594 "gtk-auto-mnemonics", &auto_mnemonics,
2597 if (!(enable_mnemonics && priv->mnemonics_visible &&
2599 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2600 (!priv->mnemonic_widget ||
2601 gtk_widget_is_sensitive (priv->mnemonic_widget))))))
2607 if (separate_uline_pattern (new_str, &key, &tmp, &pattern))
2616 if (!pango_parse_markup (new_str,
2618 with_uline ? '_' : 0,
2621 with_uline ? &accel_char : NULL,
2624 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2627 g_error_free (error);
2634 gtk_label_set_text_internal (label, text);
2638 if (priv->effective_attrs)
2639 pango_attr_list_unref (priv->effective_attrs);
2640 priv->effective_attrs = attrs;
2643 if (accel_char != 0)
2644 priv->mnemonic_keyval = gdk_keyval_to_lower (gdk_unicode_to_keyval (accel_char));
2646 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
2650 * gtk_label_set_markup:
2651 * @label: a #GtkLabel
2652 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
2654 * Parses @str which is marked up with the <link
2655 * linkend="PangoMarkupFormat">Pango text markup language</link>, setting the
2656 * label's text and attribute list based on the parse results. If the @str is
2657 * external data, you may need to escape it with g_markup_escape_text() or
2658 * g_markup_printf_escaped()<!-- -->:
2662 * markup = g_markup_printf_escaped ("<span style=\"italic\">%s</span>", str);
2663 * gtk_label_set_markup (GTK_LABEL (label), markup);
2668 gtk_label_set_markup (GtkLabel *label,
2671 g_return_if_fail (GTK_IS_LABEL (label));
2673 g_object_freeze_notify (G_OBJECT (label));
2675 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2676 gtk_label_set_use_markup_internal (label, TRUE);
2677 gtk_label_set_use_underline_internal (label, FALSE);
2679 gtk_label_recalculate (label);
2681 g_object_thaw_notify (G_OBJECT (label));
2685 * gtk_label_set_markup_with_mnemonic:
2686 * @label: a #GtkLabel
2687 * @str: a markup string (see
2688 * <link linkend="PangoMarkupFormat">Pango markup format</link>)
2690 * Parses @str which is marked up with the
2691 * <link linkend="PangoMarkupFormat">Pango text markup language</link>,
2692 * setting the label's text and attribute list based on the parse results.
2693 * If characters in @str are preceded by an underscore, they are underlined
2694 * indicating that they represent a keyboard accelerator called a mnemonic.
2696 * The mnemonic key can be used to activate another widget, chosen
2697 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
2700 gtk_label_set_markup_with_mnemonic (GtkLabel *label,
2703 g_return_if_fail (GTK_IS_LABEL (label));
2705 g_object_freeze_notify (G_OBJECT (label));
2707 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2708 gtk_label_set_use_markup_internal (label, TRUE);
2709 gtk_label_set_use_underline_internal (label, TRUE);
2711 gtk_label_recalculate (label);
2713 g_object_thaw_notify (G_OBJECT (label));
2717 * gtk_label_get_text:
2718 * @label: a #GtkLabel
2720 * Fetches the text from a label widget, as displayed on the
2721 * screen. This does not include any embedded underlines
2722 * indicating mnemonics or Pango markup. (See gtk_label_get_label())
2724 * Return value: the text in the label widget. This is the internal
2725 * string used by the label, and must not be modified.
2727 G_CONST_RETURN gchar *
2728 gtk_label_get_text (GtkLabel *label)
2730 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2732 return label->priv->text;
2735 static PangoAttrList *
2736 gtk_label_pattern_to_attrs (GtkLabel *label,
2737 const gchar *pattern)
2739 GtkLabelPrivate *priv = label->priv;
2741 const char *p = priv->text;
2742 const char *q = pattern;
2743 PangoAttrList *attrs;
2745 attrs = pango_attr_list_new ();
2749 while (*p && *q && *q != '_')
2751 p = g_utf8_next_char (p);
2755 while (*p && *q && *q == '_')
2757 p = g_utf8_next_char (p);
2763 PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_LOW);
2764 attr->start_index = start - priv->text;
2765 attr->end_index = p - priv->text;
2767 pango_attr_list_insert (attrs, attr);
2777 gtk_label_set_pattern_internal (GtkLabel *label,
2778 const gchar *pattern,
2779 gboolean is_mnemonic)
2781 GtkLabelPrivate *priv = label->priv;
2782 PangoAttrList *attrs;
2783 gboolean enable_mnemonics;
2784 gboolean auto_mnemonics;
2786 if (priv->pattern_set)
2791 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2792 "gtk-enable-mnemonics", &enable_mnemonics,
2793 "gtk-auto-mnemonics", &auto_mnemonics,
2796 if (enable_mnemonics && priv->mnemonics_visible && pattern &&
2798 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2799 (!priv->mnemonic_widget ||
2800 gtk_widget_is_sensitive (priv->mnemonic_widget)))))
2801 attrs = gtk_label_pattern_to_attrs (label, pattern);
2806 attrs = gtk_label_pattern_to_attrs (label, pattern);
2808 if (priv->effective_attrs)
2809 pango_attr_list_unref (priv->effective_attrs);
2810 priv->effective_attrs = attrs;
2814 * gtk_label_set_pattern:
2815 * @label: The #GtkLabel you want to set the pattern to.
2816 * @pattern: The pattern as described above.
2818 * The pattern of underlines you want under the existing text within the
2819 * #GtkLabel widget. For example if the current text of the label says
2820 * "FooBarBaz" passing a pattern of "___ ___" will underline
2821 * "Foo" and "Baz" but not "Bar".
2824 gtk_label_set_pattern (GtkLabel *label,
2825 const gchar *pattern)
2827 GtkLabelPrivate *priv = label->priv;
2829 g_return_if_fail (GTK_IS_LABEL (label));
2833 priv->pattern_set = FALSE;
2837 gtk_label_set_pattern_internal (label, pattern, FALSE);
2838 priv->pattern_set = TRUE;
2841 gtk_label_recalculate (label);
2843 gtk_label_clear_layout (label);
2844 gtk_widget_queue_resize (GTK_WIDGET (label));
2849 * gtk_label_set_justify:
2850 * @label: a #GtkLabel
2851 * @jtype: a #GtkJustification
2853 * Sets the alignment of the lines in the text of the label relative to
2854 * each other. %GTK_JUSTIFY_LEFT is the default value when the
2855 * widget is first created with gtk_label_new(). If you instead want
2856 * to set the alignment of the label as a whole, use
2857 * gtk_misc_set_alignment() instead. gtk_label_set_justify() has no
2858 * effect on labels containing only a single line.
2861 gtk_label_set_justify (GtkLabel *label,
2862 GtkJustification jtype)
2864 GtkLabelPrivate *priv;
2866 g_return_if_fail (GTK_IS_LABEL (label));
2867 g_return_if_fail (jtype >= GTK_JUSTIFY_LEFT && jtype <= GTK_JUSTIFY_FILL);
2871 if ((GtkJustification) priv->jtype != jtype)
2873 priv->jtype = jtype;
2875 /* No real need to be this drastic, but easier than duplicating the code */
2876 gtk_label_clear_layout (label);
2878 g_object_notify (G_OBJECT (label), "justify");
2879 gtk_widget_queue_resize (GTK_WIDGET (label));
2884 * gtk_label_get_justify:
2885 * @label: a #GtkLabel
2887 * Returns the justification of the label. See gtk_label_set_justify().
2889 * Return value: #GtkJustification
2892 gtk_label_get_justify (GtkLabel *label)
2894 g_return_val_if_fail (GTK_IS_LABEL (label), 0);
2896 return label->priv->jtype;
2900 * gtk_label_set_ellipsize:
2901 * @label: a #GtkLabel
2902 * @mode: a #PangoEllipsizeMode
2904 * Sets the mode used to ellipsize (add an ellipsis: "...") to the text
2905 * if there is not enough space to render the entire string.
2910 gtk_label_set_ellipsize (GtkLabel *label,
2911 PangoEllipsizeMode mode)
2913 GtkLabelPrivate *priv;
2915 g_return_if_fail (GTK_IS_LABEL (label));
2916 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE && mode <= PANGO_ELLIPSIZE_END);
2920 if ((PangoEllipsizeMode) priv->ellipsize != mode)
2922 priv->ellipsize = mode;
2924 /* No real need to be this drastic, but easier than duplicating the code */
2925 gtk_label_clear_layout (label);
2927 g_object_notify (G_OBJECT (label), "ellipsize");
2928 gtk_widget_queue_resize (GTK_WIDGET (label));
2933 * gtk_label_get_ellipsize:
2934 * @label: a #GtkLabel
2936 * Returns the ellipsizing position of the label. See gtk_label_set_ellipsize().
2938 * Return value: #PangoEllipsizeMode
2943 gtk_label_get_ellipsize (GtkLabel *label)
2945 g_return_val_if_fail (GTK_IS_LABEL (label), PANGO_ELLIPSIZE_NONE);
2947 return label->priv->ellipsize;
2951 * gtk_label_set_width_chars:
2952 * @label: a #GtkLabel
2953 * @n_chars: the new desired width, in characters.
2955 * Sets the desired width in characters of @label to @n_chars.
2960 gtk_label_set_width_chars (GtkLabel *label,
2963 GtkLabelPrivate *priv;
2965 g_return_if_fail (GTK_IS_LABEL (label));
2969 if (priv->width_chars != n_chars)
2971 priv->width_chars = n_chars;
2972 g_object_notify (G_OBJECT (label), "width-chars");
2973 gtk_label_invalidate_wrap_width (label);
2974 gtk_widget_queue_resize (GTK_WIDGET (label));
2979 * gtk_label_get_width_chars:
2980 * @label: a #GtkLabel
2982 * Retrieves the desired width of @label, in characters. See
2983 * gtk_label_set_width_chars().
2985 * Return value: the width of the label in characters.
2990 gtk_label_get_width_chars (GtkLabel *label)
2992 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2994 return label->priv->width_chars;
2998 * gtk_label_set_max_width_chars:
2999 * @label: a #GtkLabel
3000 * @n_chars: the new desired maximum width, in characters.
3002 * Sets the desired maximum width in characters of @label to @n_chars.
3007 gtk_label_set_max_width_chars (GtkLabel *label,
3010 GtkLabelPrivate *priv;
3012 g_return_if_fail (GTK_IS_LABEL (label));
3016 if (priv->max_width_chars != n_chars)
3018 priv->max_width_chars = n_chars;
3020 g_object_notify (G_OBJECT (label), "max-width-chars");
3021 gtk_label_invalidate_wrap_width (label);
3022 gtk_widget_queue_resize (GTK_WIDGET (label));
3027 * gtk_label_get_max_width_chars:
3028 * @label: a #GtkLabel
3030 * Retrieves the desired maximum width of @label, in characters. See
3031 * gtk_label_set_width_chars().
3033 * Return value: the maximum width of the label in characters.
3038 gtk_label_get_max_width_chars (GtkLabel *label)
3040 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
3042 return label->priv->max_width_chars;
3046 * gtk_label_set_line_wrap:
3047 * @label: a #GtkLabel
3048 * @wrap: the setting
3050 * Toggles line wrapping within the #GtkLabel widget. %TRUE makes it break
3051 * lines if text exceeds the widget's size. %FALSE lets the text get cut off
3052 * by the edge of the widget if it exceeds the widget size.
3054 * Note that setting line wrapping to %TRUE does not make the label
3055 * wrap at its parent container's width, because GTK+ widgets
3056 * conceptually can't make their requisition depend on the parent
3057 * container's size. For a label that wraps at a specific position,
3058 * set the label's width using gtk_widget_set_size_request().
3061 gtk_label_set_line_wrap (GtkLabel *label,
3064 GtkLabelPrivate *priv;
3066 g_return_if_fail (GTK_IS_LABEL (label));
3070 wrap = wrap != FALSE;
3072 if (priv->wrap != wrap)
3076 gtk_label_clear_layout (label);
3077 gtk_widget_queue_resize (GTK_WIDGET (label));
3078 g_object_notify (G_OBJECT (label), "wrap");
3083 * gtk_label_get_line_wrap:
3084 * @label: a #GtkLabel
3086 * Returns whether lines in the label are automatically wrapped.
3087 * See gtk_label_set_line_wrap().
3089 * Return value: %TRUE if the lines of the label are automatically wrapped.
3092 gtk_label_get_line_wrap (GtkLabel *label)
3094 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
3096 return label->priv->wrap;
3100 * gtk_label_set_line_wrap_mode:
3101 * @label: a #GtkLabel
3102 * @wrap_mode: the line wrapping mode
3104 * If line wrapping is on (see gtk_label_set_line_wrap()) this controls how
3105 * the line wrapping is done. The default is %PANGO_WRAP_WORD which means
3106 * wrap on word boundaries.
3111 gtk_label_set_line_wrap_mode (GtkLabel *label,
3112 PangoWrapMode wrap_mode)
3114 GtkLabelPrivate *priv;
3116 g_return_if_fail (GTK_IS_LABEL (label));
3120 if (priv->wrap_mode != wrap_mode)
3122 priv->wrap_mode = wrap_mode;
3123 g_object_notify (G_OBJECT (label), "wrap-mode");
3125 gtk_widget_queue_resize (GTK_WIDGET (label));
3130 * gtk_label_get_line_wrap_mode:
3131 * @label: a #GtkLabel
3133 * Returns line wrap mode used by the label. See gtk_label_set_line_wrap_mode().
3135 * Return value: %TRUE if the lines of the label are automatically wrapped.
3140 gtk_label_get_line_wrap_mode (GtkLabel *label)
3142 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
3144 return label->priv->wrap_mode;
3148 gtk_label_destroy (GtkWidget *widget)
3150 GtkLabel *label = GTK_LABEL (widget);
3152 gtk_label_set_mnemonic_widget (label, NULL);
3154 GTK_WIDGET_CLASS (gtk_label_parent_class)->destroy (widget);
3158 gtk_label_finalize (GObject *object)
3160 GtkLabel *label = GTK_LABEL (object);
3161 GtkLabelPrivate *priv = label->priv;
3163 g_free (priv->label);
3164 g_free (priv->text);
3167 g_object_unref (priv->layout);
3170 pango_attr_list_unref (priv->attrs);
3172 if (priv->effective_attrs)
3173 pango_attr_list_unref (priv->effective_attrs);
3175 gtk_label_clear_links (label);
3176 g_free (priv->select_info);
3178 G_OBJECT_CLASS (gtk_label_parent_class)->finalize (object);
3182 gtk_label_clear_layout (GtkLabel *label)
3184 GtkLabelPrivate *priv = label->priv;
3188 g_object_unref (priv->layout);
3189 priv->layout = NULL;
3191 //gtk_label_clear_links (label);
3195 static PangoFontMetrics *
3196 get_font_metrics (PangoContext *context, GtkWidget *widget)
3198 GtkStyleContext *style_context;
3199 PangoFontDescription *font;
3200 PangoFontMetrics *retval;
3202 style_context = gtk_widget_get_style_context (widget);
3203 gtk_style_context_get (style_context, 0, "font", &font, NULL);
3205 retval = pango_context_get_metrics (context,
3207 pango_context_get_language (context));
3210 pango_font_description_free (font);
3216 * gtk_label_get_measuring_layout:
3218 * @existing_layout: %NULL or an existing layout already in use.
3219 * @width: the width to measure with in pango units, or -1 for infinite
3221 * Gets a layout that can be used for measuring sizes. The returned
3222 * layout will be identical to the label's layout except for the
3223 * layout's width, which will be set to @width. Do not modify the returned
3226 * Returns: a new reference to a pango layout
3228 static PangoLayout *
3229 gtk_label_get_measuring_layout (GtkLabel * label,
3230 PangoLayout *existing_layout,
3233 GtkLabelPrivate *priv = label->priv;
3234 PangoRectangle rect;
3237 if (existing_layout != NULL)
3239 if (existing_layout != priv->layout)
3241 pango_layout_set_width (existing_layout, width);
3242 return existing_layout;
3245 g_object_unref (existing_layout);
3248 gtk_label_ensure_layout (label);
3250 if (pango_layout_get_width (priv->layout) == width)
3252 g_object_ref (priv->layout);
3253 return priv->layout;
3256 /* We can use the label's own layout if we're not allocated a size yet,
3257 * because we don't need it to be properly setup at that point.
3258 * This way we can make use of caching upon the label's creation.
3260 if (gtk_widget_get_allocated_width (GTK_WIDGET (label)) <= 1)
3262 g_object_ref (priv->layout);
3263 pango_layout_set_width (priv->layout, width);
3264 return priv->layout;
3267 /* oftentimes we want to measure a width that is far wider than the current width,
3268 * even though the layout would not change if we made it wider. In that case, we
3269 * can just return the current layout, because for measuring purposes, it will be
3272 pango_layout_get_extents (priv->layout, NULL, &rect);
3273 if ((width == -1 || rect.width <= width) &&
3274 !pango_layout_is_wrapped (priv->layout) &&
3275 !pango_layout_is_ellipsized (priv->layout))
3277 g_object_ref (priv->layout);
3278 return priv->layout;
3281 copy = pango_layout_copy (priv->layout);
3282 pango_layout_set_width (copy, width);
3287 gtk_label_invalidate_wrap_width (GtkLabel *label)
3289 GtkLabelPrivate *priv = label->priv;
3291 priv->wrap_width = -1;
3295 get_label_wrap_width (GtkLabel *label)
3297 GtkLabelPrivate *priv = label->priv;
3299 if (priv->wrap_width < 0)
3301 if (priv->width_chars > 0)
3303 PangoLayout *layout;
3304 PangoContext *context;
3305 PangoFontMetrics *metrics;
3306 PangoRectangle rect;
3307 gint char_width, digit_width, char_pixels, text_width;
3309 layout = gtk_label_get_measuring_layout (label, NULL, -1);
3310 context = pango_layout_get_context (layout);
3311 metrics = get_font_metrics (context, GTK_WIDGET (label));
3312 char_width = pango_font_metrics_get_approximate_char_width (metrics);
3313 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
3314 char_pixels = MAX (char_width, digit_width);
3315 pango_font_metrics_unref (metrics);
3317 pango_layout_get_extents (layout, NULL, &rect);
3318 g_object_unref (layout);
3320 text_width = rect.width;
3322 priv->wrap_width = PANGO_PIXELS (MAX (text_width, char_pixels * priv->width_chars));
3326 PangoLayout *layout;
3328 layout = gtk_widget_create_pango_layout (GTK_WIDGET (label),
3329 "This string is just about long enough.");
3330 pango_layout_get_size (layout, &priv->wrap_width, NULL);
3331 g_object_unref (layout);
3335 return priv->wrap_width;
3338 static PangoLayout *
3339 gtk_label_get_layout_with_guessed_wrap_width (GtkLabel *label)
3342 PangoRectangle logical_rect;
3343 gint wrap_width, width, height, longest_paragraph;
3344 PangoLayout *layout;
3346 if (!label->priv->wrap)
3347 return gtk_label_get_measuring_layout (label, NULL, -1);
3349 screen = gtk_widget_get_screen (GTK_WIDGET (label));
3351 layout = gtk_label_get_measuring_layout (label, NULL, -1);
3352 pango_layout_get_extents (layout, NULL, &logical_rect);
3354 width = logical_rect.width;
3355 /* Try to guess a reasonable maximum width */
3356 longest_paragraph = width;
3358 wrap_width = get_label_wrap_width (label);
3359 width = MIN (width, wrap_width);
3361 PANGO_SCALE * (gdk_screen_get_width (screen) + 1) / 2);
3363 layout = gtk_label_get_measuring_layout (label, layout, width);
3365 pango_layout_get_extents (layout, NULL, &logical_rect);
3366 width = logical_rect.width;
3367 height = logical_rect.height;
3369 /* Unfortunately, the above may leave us with a very unbalanced looking paragraph,
3370 * so we try short search for a narrower width that leaves us with the same height
3372 if (longest_paragraph > 0)
3374 gint nlines, perfect_width;
3376 nlines = pango_layout_get_line_count (layout);
3377 perfect_width = (longest_paragraph + nlines - 1) / nlines;
3379 if (perfect_width < width)
3381 layout = gtk_label_get_measuring_layout (label, layout, perfect_width);
3382 pango_layout_get_extents (layout, NULL, &logical_rect);
3384 if (logical_rect.height <= height)
3385 width = logical_rect.width;
3388 gint mid_width = (perfect_width + width) / 2;
3390 if (mid_width > perfect_width)
3392 layout = gtk_label_get_measuring_layout (label, layout, mid_width);
3393 pango_layout_get_extents (layout, NULL, &logical_rect);
3395 if (logical_rect.height <= height)
3396 width = logical_rect.width;
3402 return gtk_label_get_measuring_layout (label, layout, width);
3406 gtk_label_update_layout_width (GtkLabel *label)
3408 GtkLabelPrivate *priv = label->priv;
3409 GtkWidget *widget = GTK_WIDGET (label);
3410 GtkAllocation allocation;
3412 g_assert (priv->layout);
3414 gtk_widget_get_allocation (widget, &allocation);
3416 if (priv->ellipsize)
3418 PangoRectangle logical;
3419 PangoRectangle bounds;
3422 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3424 bounds.x = bounds.y = 0;
3425 bounds.width = allocation.width - xpad * 2;
3426 bounds.height = allocation.height - ypad * 2;
3428 pango_layout_set_width (priv->layout, -1);
3429 pango_layout_get_pixel_extents (priv->layout, NULL, &logical);
3431 if (priv->have_transform)
3433 PangoContext *context = gtk_widget_get_pango_context (widget);
3434 const PangoMatrix *matrix = pango_context_get_matrix (context);
3436 const gdouble dx = matrix->xx; /* cos (M_PI * angle / 180) */
3437 const gdouble dy = matrix->xy; /* sin (M_PI * angle / 180) */
3438 if (fabs (dy) < 0.01)
3440 if (logical.width > bounds.width)
3441 pango_layout_set_width (priv->layout, bounds.width * PANGO_SCALE);
3443 else if (fabs (dx) < 0.01)
3445 if (logical.width > bounds.height)
3446 pango_layout_set_width (priv->layout, bounds.height * PANGO_SCALE);
3450 gdouble x0, y0, x1, y1, length;
3454 x0 = bounds.width / 2;
3455 y0 = dx ? x0 * dy / dx : G_MAXDOUBLE;
3456 vertical = fabs (y0) > bounds.height / 2;
3460 y0 = bounds.height/2;
3461 x0 = dy ? y0 * dx / dy : G_MAXDOUBLE;
3464 length = 2 * sqrt (x0 * x0 + y0 * y0);
3465 pango_layout_set_width (priv->layout, rint (length * PANGO_SCALE));
3466 pango_layout_get_pixel_size (priv->layout, NULL, &cy);
3473 y0 = bounds.height/2 + y1 - y0;
3478 x0 = bounds.width/2 + x1 - x0;
3482 length = length - sqrt (x0 * x0 + y0 * y0) * 2;
3483 pango_layout_set_width (priv->layout, rint (length * PANGO_SCALE));
3486 else if (logical.width > bounds.width)
3487 pango_layout_set_width (priv->layout, bounds.width * PANGO_SCALE);
3489 else if (priv->wrap)
3491 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (widget, FALSE);
3492 gdouble angle = gtk_label_get_angle (label);
3496 if ((angle == 90 || angle == 270) && aux_info && aux_info->height > 0)
3497 aux_width = aux_info->height;
3498 else if (aux_info && aux_info->width > 0)
3499 aux_width = aux_info->width;
3502 pango_layout_set_width (priv->layout, aux_width * PANGO_SCALE);
3506 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3508 if (angle == 90 || angle == 270)
3509 width = allocation.height - ypad * 2;
3511 width = allocation.width - xpad * 2;
3513 pango_layout_set_wrap (priv->layout, priv->wrap_mode);
3514 pango_layout_set_width (priv->layout, MAX (width, 1) * PANGO_SCALE);
3517 else /* !priv->wrap */
3518 pango_layout_set_width (priv->layout, -1);
3522 gtk_label_ensure_layout (GtkLabel *label)
3524 GtkLabelPrivate *priv = label->priv;
3528 widget = GTK_WIDGET (label);
3530 rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
3534 PangoAlignment align = PANGO_ALIGN_LEFT; /* Quiet gcc */
3535 gdouble angle = gtk_label_get_angle (label);
3537 if (angle != 0.0 && !priv->select_info)
3539 PangoMatrix matrix = PANGO_MATRIX_INIT;
3541 /* We rotate the standard singleton PangoContext for the widget,
3542 * depending on the fact that it's meant pretty much exclusively
3545 pango_matrix_rotate (&matrix, angle);
3547 pango_context_set_matrix (gtk_widget_get_pango_context (widget), &matrix);
3549 priv->have_transform = TRUE;
3553 if (priv->have_transform)
3554 pango_context_set_matrix (gtk_widget_get_pango_context (widget), NULL);
3556 priv->have_transform = FALSE;
3559 priv->layout = gtk_widget_create_pango_layout (widget, priv->text);
3561 if (priv->effective_attrs)
3562 pango_layout_set_attributes (priv->layout, priv->effective_attrs);
3564 gtk_label_rescan_links (label);
3566 switch (priv->jtype)
3568 case GTK_JUSTIFY_LEFT:
3569 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3571 case GTK_JUSTIFY_RIGHT:
3572 align = rtl ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
3574 case GTK_JUSTIFY_CENTER:
3575 align = PANGO_ALIGN_CENTER;
3577 case GTK_JUSTIFY_FILL:
3578 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3579 pango_layout_set_justify (priv->layout, TRUE);
3582 g_assert_not_reached();
3585 pango_layout_set_alignment (priv->layout, align);
3586 pango_layout_set_ellipsize (priv->layout, priv->ellipsize);
3587 pango_layout_set_single_paragraph_mode (priv->layout, priv->single_line_mode);
3589 gtk_label_update_layout_width (label);
3594 get_single_line_height (GtkWidget *widget,
3595 PangoLayout *layout)
3597 PangoContext *context;
3598 PangoFontMetrics *metrics;
3599 gint ascent, descent;
3601 context = pango_layout_get_context (layout);
3602 metrics = get_font_metrics (context, widget);
3603 ascent = pango_font_metrics_get_ascent (metrics);
3604 descent = pango_font_metrics_get_descent (metrics);
3605 pango_font_metrics_unref (metrics);
3607 return ascent + descent;
3610 static GtkSizeRequestMode
3611 gtk_label_get_request_mode (GtkWidget *widget)
3613 GtkLabel *label = GTK_LABEL (widget);
3614 gdouble angle = gtk_label_get_angle (label);
3616 if (label->priv->wrap)
3617 return (angle == 90 || angle == 270) ?
3618 GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT :
3619 GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
3621 return GTK_SIZE_REQUEST_CONSTANT_SIZE;
3625 get_size_for_allocation (GtkLabel *label,
3626 GtkOrientation orientation,
3631 PangoLayout *layout;
3632 GtkWidgetAuxInfo *aux_info =
3633 _gtk_widget_get_aux_info (GTK_WIDGET (label), FALSE);
3639 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3640 aux_size = aux_info->width;
3642 aux_size = aux_info->height;
3648 layout = gtk_label_get_measuring_layout (label, NULL, aux_size * PANGO_SCALE);
3650 layout = gtk_label_get_measuring_layout (label, NULL, allocation * PANGO_SCALE);
3652 pango_layout_get_pixel_size (layout, NULL, &text_height);
3655 *minimum_size = text_height;
3658 *natural_size = text_height;
3660 g_object_unref (layout);
3664 get_char_pixels (GtkWidget *label,
3665 PangoLayout *layout)
3667 PangoContext *context;
3668 PangoFontMetrics *metrics;
3669 gint char_width, digit_width;
3671 context = pango_layout_get_context (layout);
3672 metrics = get_font_metrics (context, GTK_WIDGET (label));
3673 char_width = pango_font_metrics_get_approximate_char_width (metrics);
3674 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
3675 pango_font_metrics_unref (metrics);
3677 return MAX (char_width, digit_width);;
3681 gtk_label_get_preferred_layout_size (GtkLabel *label,
3682 PangoRectangle *required,
3683 PangoRectangle *natural)
3685 GtkLabelPrivate *priv = label->priv;
3686 PangoLayout *layout;
3687 PangoRectangle rect;
3688 gint text_width, ellipsize_chars, guess_width;
3690 /* "width-chars" Hard-coded minimum width:
3691 * - minimum size should be MAX (width-chars, strlen ("..."));
3692 * - natural size should be MAX (width-chars, strlen (priv->text));
3694 * "max-width-chars" User specified maximum size requisition
3695 * - minimum size should be MAX (width-chars, 0)
3696 * - natural size should be MIN (max-width-chars, strlen (priv->text))
3700 /* When calculating ->wrap sometimes we need to invent a size; Ideally we should be doing
3701 * that stuff here instead of inside gtk_label_ensure_layout() */
3702 layout = gtk_label_get_layout_with_guessed_wrap_width (label);
3704 /* Start off with the pixel extents of the rendered layout */
3705 pango_layout_get_extents (layout, NULL, required);
3706 required->x = required->y = 0;
3708 if (priv->single_line_mode || priv->wrap)
3709 required->height = get_single_line_height (GTK_WIDGET (label), layout);
3711 *natural = *required;
3713 guess_width = required->width;
3715 layout = gtk_label_get_measuring_layout (label, layout, -1);
3717 /* Fetch the length of the complete unwrapped text */
3718 pango_layout_get_extents (layout, NULL, &rect);
3719 text_width = rect.width;
3721 /* enforce minimum width for ellipsized labels at ~3 chars */
3722 if (priv->ellipsize)
3723 ellipsize_chars = 3;
3725 ellipsize_chars = 0;
3727 /* "width-chars" Hard-coded minimum width:
3728 * - minimum size should be MAX (width-chars, strlen ("..."));
3729 * - natural size should be MAX (width-chars, strlen (priv->text));
3731 * "max-width-chars" User specified maximum size requisition
3732 * - minimum size should be MAX (width-chars, 0)
3733 * - natural size should be MIN (max-width-chars, strlen (priv->text))
3735 * For ellipsizing labels; if max-width-chars is specified: either it is used as
3736 * a minimum size or the label text as a minimum size (natural size still overflows).
3738 * For wrapping labels; A reasonable minimum size is useful to naturally layout
3739 * interfaces automatically. In this case if no "width-chars" is specified, the minimum
3740 * width will default to the wrap guess that gtk_label_ensure_layout() does.
3742 * In *any* case the minimum width is completely overridden if an explicit width
3743 * request was provided.
3746 if (priv->ellipsize || priv->wrap)
3750 char_pixels = get_char_pixels (GTK_WIDGET (label), layout);
3752 required->width = char_pixels * MAX (priv->width_chars, ellipsize_chars);
3754 /* Default to the minimum width regularly guessed by GTK+ if no minimum
3755 * width was specified, only allow unwrapping of these labels.
3757 * Note that when specifying a small width_chars for a long text;
3758 * an accordingly large size will be required for the label height.
3760 if (priv->wrap && priv->width_chars <= 0)
3761 required->width = guess_width;
3763 if (priv->max_width_chars < 0)
3765 natural->width = MAX (required->width, text_width);
3769 gint max_char_width = char_pixels * priv->max_width_chars;
3770 gint max_width = MIN (text_width, max_char_width);
3772 /* With max-char-width specified, we let the minimum widths of
3773 * ellipsized text crawl up to the max-char-width
3774 * (note that we dont want to limit the minimum width for wrapping text).
3776 if (priv->ellipsize)
3777 required->width = MIN (text_width, max_width);
3779 natural->width = MAX (required->width, max_width);
3784 required->width = text_width;
3785 natural->width = required->width;
3788 /* if a width-request is set, use that as the requested label width */
3789 if (priv->wrap || priv->ellipsize || priv->width_chars > 0 || priv->max_width_chars > 0)
3791 GtkWidgetAuxInfo *aux_info;
3793 aux_info = _gtk_widget_get_aux_info (GTK_WIDGET (label), FALSE);
3794 if (aux_info && aux_info->width > 0)
3796 required->width = aux_info->width * PANGO_SCALE;
3797 natural->width = MAX (natural->width, required->width);
3801 g_object_unref (layout);
3805 gtk_label_get_preferred_size (GtkWidget *widget,
3806 GtkOrientation orientation,
3810 GtkLabel *label = GTK_LABEL (widget);
3811 GtkLabelPrivate *priv = label->priv;
3812 PangoRectangle required_rect;
3813 PangoRectangle natural_rect;
3816 gtk_label_get_preferred_layout_size (label, &required_rect, &natural_rect);
3818 /* Now that we have minimum and natural sizes in pango extents, apply a possible transform */
3819 if (priv->have_transform)
3822 PangoContext *context;
3823 const PangoMatrix *matrix;
3825 copy = pango_layout_copy (priv->layout);
3826 context = pango_layout_get_context (copy);
3827 matrix = pango_context_get_matrix (context);
3829 pango_layout_set_width (copy, -1);
3830 pango_layout_set_ellipsize (copy, PANGO_ELLIPSIZE_NONE);
3832 pango_layout_get_extents (copy, NULL, &natural_rect);
3833 g_object_unref (copy);
3835 pango_matrix_transform_rectangle (matrix, &required_rect);
3836 pango_matrix_transform_rectangle (matrix, &natural_rect);
3838 /* Bump the natural size in case of ellipsize to ensure pango has
3839 * enough space in the angles (note, we could alternatively set the
3840 * layout to not ellipsize when we know we have been allocated our
3841 * full natural size, or it may be that pango needs a fix here).
3843 if (priv->ellipsize && priv->angle != 0 && priv->angle != 90 &&
3844 priv->angle != 180 && priv->angle != 270 && priv->angle != 360)
3846 /* For some reason we only need this at about 110 degrees, and only
3847 * when gaining in height
3849 natural_rect.height += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3850 natural_rect.width += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3854 required_rect.width = PANGO_PIXELS_CEIL (required_rect.width);
3855 required_rect.height = PANGO_PIXELS_CEIL (required_rect.height);
3857 natural_rect.width = PANGO_PIXELS_CEIL (natural_rect.width);
3858 natural_rect.height = PANGO_PIXELS_CEIL (natural_rect.height);
3860 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3862 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3864 /* Note, we cant use get_size_for_allocation() when rotating
3865 * ellipsized labels.
3867 if (!(priv->ellipsize && priv->have_transform) &&
3868 (priv->angle == 90 || priv->angle == 270))
3870 /* Doing a h4w request on a rotated label here, return the
3871 * required width for the minimum height.
3873 get_size_for_allocation (label,
3874 GTK_ORIENTATION_VERTICAL,
3875 required_rect.height,
3876 minimum_size, natural_size);
3881 /* Normal desired width */
3882 *minimum_size = required_rect.width;
3883 *natural_size = natural_rect.width;
3886 *minimum_size += xpad * 2;
3887 *natural_size += xpad * 2;
3889 else /* GTK_ORIENTATION_VERTICAL */
3891 /* Note, we cant use get_size_for_allocation() when rotating
3892 * ellipsized labels.
3894 if (!(priv->ellipsize && priv->have_transform) &&
3895 (priv->angle == 0 || priv->angle == 180 || priv->angle == 360))
3897 /* Doing a w4h request on a label here, return the required
3898 * height for the minimum width.
3900 get_size_for_allocation (label,
3901 GTK_ORIENTATION_HORIZONTAL,
3902 required_rect.width,
3903 minimum_size, natural_size);
3907 /* A vertically rotated label does w4h, so return the base
3908 * desired height (text length)
3910 *minimum_size = required_rect.height;
3911 *natural_size = natural_rect.height;
3914 *minimum_size += ypad * 2;
3915 *natural_size += ypad * 2;
3921 gtk_label_get_preferred_width (GtkWidget *widget,
3925 gtk_label_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
3929 gtk_label_get_preferred_height (GtkWidget *widget,
3933 gtk_label_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
3937 gtk_label_get_preferred_width_for_height (GtkWidget *widget,
3939 gint *minimum_width,
3940 gint *natural_width)
3942 GtkLabel *label = GTK_LABEL (widget);
3943 GtkLabelPrivate *priv = label->priv;
3945 if (priv->wrap && (priv->angle == 90 || priv->angle == 270))
3949 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3952 gtk_label_clear_layout (label);
3954 get_size_for_allocation (label, GTK_ORIENTATION_VERTICAL,
3955 MAX (1, height - (ypad * 2)),
3956 minimum_width, natural_width);
3959 *minimum_width += xpad * 2;
3962 *natural_width += xpad * 2;
3965 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_width, natural_width);
3969 gtk_label_get_preferred_height_for_width (GtkWidget *widget,
3971 gint *minimum_height,
3972 gint *natural_height)
3974 GtkLabel *label = GTK_LABEL (widget);
3975 GtkLabelPrivate *priv = label->priv;
3977 if (priv->wrap && (priv->angle == 0 || priv->angle == 180 || priv->angle == 360))
3981 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3984 gtk_label_clear_layout (label);
3986 get_size_for_allocation (label, GTK_ORIENTATION_HORIZONTAL,
3987 MAX (1, width - xpad * 2),
3988 minimum_height, natural_height);
3991 *minimum_height += ypad * 2;
3994 *natural_height += ypad * 2;
3997 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, minimum_height, natural_height);
4001 gtk_label_size_allocate (GtkWidget *widget,
4002 GtkAllocation *allocation)
4004 GtkLabel *label = GTK_LABEL (widget);
4005 GtkLabelPrivate *priv = label->priv;
4007 GTK_WIDGET_CLASS (gtk_label_parent_class)->size_allocate (widget, allocation);
4010 gtk_label_update_layout_width (label);
4012 if (priv->select_info && priv->select_info->window)
4014 gdk_window_move_resize (priv->select_info->window,
4018 allocation->height);
4023 gtk_label_update_cursor (GtkLabel *label)
4025 GtkLabelPrivate *priv = label->priv;
4028 if (!priv->select_info)
4031 widget = GTK_WIDGET (label);
4033 if (gtk_widget_get_realized (widget))
4035 GdkDisplay *display;
4038 if (gtk_widget_is_sensitive (widget))
4040 display = gtk_widget_get_display (widget);
4042 if (priv->select_info->active_link)
4043 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
4044 else if (priv->select_info->selectable)
4045 cursor = gdk_cursor_new_for_display (display, GDK_XTERM);
4052 gdk_window_set_cursor (priv->select_info->window, cursor);
4055 g_object_unref (cursor);
4060 gtk_label_state_changed (GtkWidget *widget,
4061 GtkStateType prev_state)
4063 GtkLabel *label = GTK_LABEL (widget);
4064 GtkLabelPrivate *priv = label->priv;
4066 if (priv->select_info)
4068 if (!gtk_widget_is_sensitive (widget))
4069 gtk_label_select_region (label, 0, 0);
4071 gtk_label_update_cursor (label);
4074 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed)
4075 GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed (widget, prev_state);
4079 gtk_label_style_updated (GtkWidget *widget)
4081 GtkLabel *label = GTK_LABEL (widget);
4083 GTK_WIDGET_CLASS (gtk_label_parent_class)->style_updated (widget);
4085 /* We have to clear the layout, fonts etc. may have changed */
4086 gtk_label_clear_layout (label);
4087 gtk_label_invalidate_wrap_width (label);
4091 gtk_label_direction_changed (GtkWidget *widget,
4092 GtkTextDirection previous_dir)
4094 GtkLabel *label = GTK_LABEL (widget);
4095 GtkLabelPrivate *priv = label->priv;
4098 pango_layout_context_changed (priv->layout);
4100 GTK_WIDGET_CLASS (gtk_label_parent_class)->direction_changed (widget, previous_dir);
4104 get_layout_location (GtkLabel *label,
4108 GtkAllocation allocation;
4111 GtkLabelPrivate *priv;
4112 gint req_width, x, y;
4115 gfloat xalign, yalign;
4116 PangoRectangle logical;
4118 misc = GTK_MISC (label);
4119 widget = GTK_WIDGET (label);
4122 gtk_misc_get_alignment (misc, &xalign, &yalign);
4123 gtk_misc_get_padding (misc, &xpad, &ypad);
4125 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
4126 xalign = 1.0 - xalign;
4128 pango_layout_get_extents (priv->layout, NULL, &logical);
4130 if (priv->have_transform)
4132 PangoContext *context = gtk_widget_get_pango_context (widget);
4133 const PangoMatrix *matrix = pango_context_get_matrix (context);
4134 pango_matrix_transform_rectangle (matrix, &logical);
4137 pango_extents_to_pixels (&logical, NULL);
4139 req_width = logical.width;
4140 req_height = logical.height;
4142 req_width += 2 * xpad;
4143 req_height += 2 * ypad;
4145 gtk_widget_get_allocation (widget, &allocation);
4147 x = floor (allocation.x + xpad + xalign * (allocation.width - req_width));
4149 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
4150 x = MAX (x, allocation.x + xpad);
4152 x = MIN (x, allocation.x + allocation.width - xpad);
4155 /* bgo#315462 - For single-line labels, *do* align the requisition with
4156 * respect to the allocation, even if we are under-allocated. For multi-line
4157 * labels, always show the top of the text when they are under-allocated. The
4158 * rationale is this:
4160 * - Single-line labels appear in GtkButtons, and it is very easy to get them
4161 * to be smaller than their requisition. The button may clip the label, but
4162 * the label will still be able to show most of itself and the focus
4163 * rectangle. Also, it is fairly easy to read a single line of clipped text.
4165 * - Multi-line labels should not be clipped to showing "something in the
4166 * middle". You want to read the first line, at least, to get some context.
4168 if (pango_layout_get_line_count (priv->layout) == 1)
4169 y = floor (allocation.y + ypad + (allocation.height - req_height) * yalign);
4171 y = floor (allocation.y + ypad + MAX ((allocation.height - req_height) * yalign, 0));
4181 draw_insertion_cursor (GtkLabel *label,
4183 GdkRectangle *cursor_location,
4184 gboolean is_primary,
4185 PangoDirection direction,
4186 gboolean draw_arrow)
4188 GtkWidget *widget = GTK_WIDGET (label);
4189 GtkTextDirection text_dir;
4191 if (direction == PANGO_DIRECTION_LTR)
4192 text_dir = GTK_TEXT_DIR_LTR;
4194 text_dir = GTK_TEXT_DIR_RTL;
4196 gtk_draw_insertion_cursor (widget, cr, cursor_location,
4197 is_primary, text_dir, draw_arrow);
4200 static PangoDirection
4201 get_cursor_direction (GtkLabel *label)
4203 GtkLabelPrivate *priv = label->priv;
4206 g_assert (priv->select_info);
4208 gtk_label_ensure_layout (label);
4210 for (l = pango_layout_get_lines_readonly (priv->layout); l; l = l->next)
4212 PangoLayoutLine *line = l->data;
4214 /* If priv->select_info->selection_end is at the very end of
4215 * the line, we don't know if the cursor is on this line or
4216 * the next without looking ahead at the next line. (End
4217 * of paragraph is different from line break.) But it's
4218 * definitely in this paragraph, which is good enough
4219 * to figure out the resolved direction.
4221 if (line->start_index + line->length >= priv->select_info->selection_end)
4222 return line->resolved_dir;
4225 return PANGO_DIRECTION_LTR;
4229 gtk_label_draw_cursor (GtkLabel *label, cairo_t *cr, gint xoffset, gint yoffset)
4231 GtkLabelPrivate *priv = label->priv;
4234 if (priv->select_info == NULL)
4237 widget = GTK_WIDGET (label);
4239 if (gtk_widget_is_drawable (widget))
4241 PangoDirection keymap_direction;
4242 PangoDirection cursor_direction;
4243 PangoRectangle strong_pos, weak_pos;
4244 gboolean split_cursor;
4245 PangoRectangle *cursor1 = NULL;
4246 PangoRectangle *cursor2 = NULL;
4247 GdkRectangle cursor_location;
4248 PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
4249 PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
4251 keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gtk_widget_get_display (widget)));
4252 cursor_direction = get_cursor_direction (label);
4254 gtk_label_ensure_layout (label);
4256 pango_layout_get_cursor_pos (priv->layout, priv->select_info->selection_end,
4257 &strong_pos, &weak_pos);
4259 g_object_get (gtk_widget_get_settings (widget),
4260 "gtk-split-cursor", &split_cursor,
4263 dir1 = cursor_direction;
4267 cursor1 = &strong_pos;
4269 if (strong_pos.x != weak_pos.x ||
4270 strong_pos.y != weak_pos.y)
4272 dir2 = (cursor_direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
4273 cursor2 = &weak_pos;
4278 if (keymap_direction == cursor_direction)
4279 cursor1 = &strong_pos;
4281 cursor1 = &weak_pos;
4284 cursor_location.x = xoffset + PANGO_PIXELS (cursor1->x);
4285 cursor_location.y = yoffset + PANGO_PIXELS (cursor1->y);
4286 cursor_location.width = 0;
4287 cursor_location.height = PANGO_PIXELS (cursor1->height);
4289 draw_insertion_cursor (label, cr,
4290 &cursor_location, TRUE, dir1,
4291 dir2 != PANGO_DIRECTION_NEUTRAL);
4293 if (dir2 != PANGO_DIRECTION_NEUTRAL)
4295 cursor_location.x = xoffset + PANGO_PIXELS (cursor2->x);
4296 cursor_location.y = yoffset + PANGO_PIXELS (cursor2->y);
4297 cursor_location.width = 0;
4298 cursor_location.height = PANGO_PIXELS (cursor2->height);
4300 draw_insertion_cursor (label, cr,
4301 &cursor_location, FALSE, dir2,
4307 static GtkLabelLink *
4308 gtk_label_get_focus_link (GtkLabel *label)
4310 GtkLabelPrivate *priv = label->priv;
4311 GtkLabelSelectionInfo *info = priv->select_info;
4317 if (info->selection_anchor != info->selection_end)
4320 for (l = info->links; l; l = l->next)
4322 GtkLabelLink *link = l->data;
4323 if (link->start <= info->selection_anchor &&
4324 info->selection_anchor <= link->end)
4332 gtk_label_draw (GtkWidget *widget,
4335 GtkLabel *label = GTK_LABEL (widget);
4336 GtkLabelPrivate *priv = label->priv;
4337 GtkLabelSelectionInfo *info = priv->select_info;
4338 GtkAllocation allocation;
4339 GtkStyleContext *context;
4340 GtkStateFlags state;
4343 gtk_label_ensure_layout (label);
4345 if (priv->text && (*priv->text != '\0'))
4347 GdkRGBA *bg_color, *fg_color;
4349 get_layout_location (label, &x, &y);
4351 context = gtk_widget_get_style_context (widget);
4352 gtk_widget_get_allocation (widget, &allocation);
4354 cairo_translate (cr, -allocation.x, -allocation.y);
4356 state = gtk_widget_get_state_flags (widget);
4357 gtk_style_context_set_state (context, state);
4359 gtk_render_layout (context, cr,
4364 (info->selection_anchor != info->selection_end))
4367 cairo_region_t *clip;
4369 range[0] = info->selection_anchor;
4370 range[1] = info->selection_end;
4372 if (range[0] > range[1])
4374 gint tmp = range[0];
4375 range[0] = range[1];
4379 clip = gdk_pango_layout_get_clip_region (priv->layout,
4384 /* FIXME should use gtk_paint, but it can't use a clip region */
4387 gdk_cairo_region (cr, clip);
4390 state = GTK_STATE_FLAG_SELECTED;
4392 if (gtk_widget_has_focus (widget))
4393 state |= GTK_STATE_FLAG_FOCUSED;
4395 gtk_style_context_get (context, state,
4396 "background-color", &bg_color,
4400 gdk_cairo_set_source_rgba (cr, bg_color);
4403 gdk_cairo_set_source_rgba (cr, fg_color);
4404 cairo_move_to (cr, x, y);
4405 _gtk_pango_fill_layout (cr, priv->layout);
4408 cairo_region_destroy (clip);
4410 gdk_rgba_free (bg_color);
4411 gdk_rgba_free (fg_color);
4415 GtkLabelLink *focus_link;
4416 GtkLabelLink *active_link;
4418 cairo_region_t *clip;
4420 GdkColor *text_color;
4421 GdkColor *link_color;
4422 GdkColor *visited_link_color;
4424 if (info->selectable && gtk_widget_has_focus (widget))
4425 gtk_label_draw_cursor (label, cr, x, y);
4427 focus_link = gtk_label_get_focus_link (label);
4428 active_link = info->active_link;
4435 range[0] = active_link->start;
4436 range[1] = active_link->end;
4440 clip = gdk_pango_layout_get_clip_region (priv->layout,
4444 gdk_cairo_region (cr, clip);
4446 cairo_region_destroy (clip);
4448 gtk_label_get_link_colors (widget, &link_color, &visited_link_color);
4449 if (active_link->visited)
4450 text_color = visited_link_color;
4452 text_color = link_color;
4454 if (info->link_clicked)
4455 state = GTK_STATE_FLAG_ACTIVE;
4457 state = GTK_STATE_FLAG_PRELIGHT;
4459 gtk_style_context_get (context, state,
4460 "background-color", &bg_color,
4463 gdk_cairo_set_source_rgba (cr, bg_color);
4466 gdk_cairo_set_source_color (cr, text_color);
4467 cairo_move_to (cr, x, y);
4468 _gtk_pango_fill_layout (cr, priv->layout);
4470 gdk_color_free (link_color);
4471 gdk_color_free (visited_link_color);
4472 gdk_rgba_free (bg_color);
4477 if (focus_link && gtk_widget_has_focus (widget))
4479 range[0] = focus_link->start;
4480 range[1] = focus_link->end;
4482 clip = gdk_pango_layout_get_clip_region (priv->layout,
4486 cairo_region_get_extents (clip, &rect);
4488 state = gtk_widget_get_state_flags (widget);
4489 gtk_style_context_set_state (context, state);
4491 gtk_render_focus (context, cr,
4493 rect.width, rect.height);
4495 cairo_region_destroy (clip);
4504 separate_uline_pattern (const gchar *str,
4509 gboolean underscore;
4512 gchar *pattern_dest;
4514 *accel_key = GDK_KEY_VoidSymbol;
4515 *new_str = g_new (gchar, strlen (str) + 1);
4516 *pattern = g_new (gchar, g_utf8_strlen (str, -1) + 1);
4522 pattern_dest = *pattern;
4527 const gchar *next_src;
4529 c = g_utf8_get_char (src);
4530 if (c == (gunichar)-1)
4532 g_warning ("Invalid input string");
4538 next_src = g_utf8_next_char (src);
4543 *pattern_dest++ = ' ';
4546 *pattern_dest++ = '_';
4547 if (*accel_key == GDK_KEY_VoidSymbol)
4548 *accel_key = gdk_keyval_to_lower (gdk_unicode_to_keyval (c));
4551 while (src < next_src)
4565 while (src < next_src)
4568 *pattern_dest++ = ' ';
4580 gtk_label_set_uline_text_internal (GtkLabel *label,
4583 GtkLabelPrivate *priv = label->priv;
4584 guint accel_key = GDK_KEY_VoidSymbol;
4588 g_return_if_fail (GTK_IS_LABEL (label));
4589 g_return_if_fail (str != NULL);
4591 /* Split text into the base text and a separate pattern
4594 if (!separate_uline_pattern (str, &accel_key, &new_str, &pattern))
4597 gtk_label_set_text_internal (label, new_str);
4598 gtk_label_set_pattern_internal (label, pattern, TRUE);
4599 priv->mnemonic_keyval = accel_key;
4605 * gtk_label_set_text_with_mnemonic:
4606 * @label: a #GtkLabel
4609 * Sets the label's text from the string @str.
4610 * If characters in @str are preceded by an underscore, they are underlined
4611 * indicating that they represent a keyboard accelerator called a mnemonic.
4612 * The mnemonic key can be used to activate another widget, chosen
4613 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
4616 gtk_label_set_text_with_mnemonic (GtkLabel *label,
4619 g_return_if_fail (GTK_IS_LABEL (label));
4620 g_return_if_fail (str != NULL);
4622 g_object_freeze_notify (G_OBJECT (label));
4624 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
4625 gtk_label_set_use_markup_internal (label, FALSE);
4626 gtk_label_set_use_underline_internal (label, TRUE);
4628 gtk_label_recalculate (label);
4630 g_object_thaw_notify (G_OBJECT (label));
4634 gtk_label_realize (GtkWidget *widget)
4636 GtkLabel *label = GTK_LABEL (widget);
4637 GtkLabelPrivate *priv = label->priv;
4639 GTK_WIDGET_CLASS (gtk_label_parent_class)->realize (widget);
4641 if (priv->select_info)
4642 gtk_label_create_window (label);
4646 gtk_label_unrealize (GtkWidget *widget)
4648 GtkLabel *label = GTK_LABEL (widget);
4649 GtkLabelPrivate *priv = label->priv;
4651 if (priv->select_info)
4652 gtk_label_destroy_window (label);
4654 GTK_WIDGET_CLASS (gtk_label_parent_class)->unrealize (widget);
4658 gtk_label_map (GtkWidget *widget)
4660 GtkLabel *label = GTK_LABEL (widget);
4661 GtkLabelPrivate *priv = label->priv;
4663 GTK_WIDGET_CLASS (gtk_label_parent_class)->map (widget);
4665 if (priv->select_info)
4666 gdk_window_show (priv->select_info->window);
4670 gtk_label_unmap (GtkWidget *widget)
4672 GtkLabel *label = GTK_LABEL (widget);
4673 GtkLabelPrivate *priv = label->priv;
4675 if (priv->select_info)
4676 gdk_window_hide (priv->select_info->window);
4678 GTK_WIDGET_CLASS (gtk_label_parent_class)->unmap (widget);
4682 window_to_layout_coords (GtkLabel *label,
4686 GtkAllocation allocation;
4690 widget = GTK_WIDGET (label);
4692 /* get layout location in widget->window coords */
4693 get_layout_location (label, &lx, &ly);
4695 gtk_widget_get_allocation (widget, &allocation);
4699 *x += allocation.x; /* go to widget->window */
4700 *x -= lx; /* go to layout */
4705 *y += allocation.y; /* go to widget->window */
4706 *y -= ly; /* go to layout */
4712 layout_to_window_coords (GtkLabel *label,
4719 widget = GTK_WIDGET (label);
4721 /* get layout location in widget->window coords */
4722 get_layout_location (label, &lx, &ly);
4726 *x += lx; /* go to widget->window */
4727 *x -= widget->allocation.x; /* go to selection window */
4732 *y += ly; /* go to widget->window */
4733 *y -= widget->allocation.y; /* go to selection window */
4739 get_layout_index (GtkLabel *label,
4744 GtkLabelPrivate *priv = label->priv;
4746 const gchar *cluster;
4747 const gchar *cluster_end;
4752 gtk_label_ensure_layout (label);
4754 window_to_layout_coords (label, &x, &y);
4759 inside = pango_layout_xy_to_index (priv->layout,
4763 cluster = priv->text + *index;
4764 cluster_end = cluster;
4767 cluster_end = g_utf8_next_char (cluster_end);
4771 *index += (cluster_end - cluster);
4777 gtk_label_select_word (GtkLabel *label)
4779 GtkLabelPrivate *priv = label->priv;
4782 gint start_index = gtk_label_move_backward_word (label, priv->select_info->selection_end);
4783 gint end_index = gtk_label_move_forward_word (label, priv->select_info->selection_end);
4785 min = MIN (priv->select_info->selection_anchor,
4786 priv->select_info->selection_end);
4787 max = MAX (priv->select_info->selection_anchor,
4788 priv->select_info->selection_end);
4790 min = MIN (min, start_index);
4791 max = MAX (max, end_index);
4793 gtk_label_select_region_index (label, min, max);
4797 gtk_label_grab_focus (GtkWidget *widget)
4799 GtkLabel *label = GTK_LABEL (widget);
4800 GtkLabelPrivate *priv = label->priv;
4801 gboolean select_on_focus;
4804 if (priv->select_info == NULL)
4807 GTK_WIDGET_CLASS (gtk_label_parent_class)->grab_focus (widget);
4809 if (priv->select_info->selectable)
4811 g_object_get (gtk_widget_get_settings (widget),
4812 "gtk-label-select-on-focus",
4816 if (select_on_focus && !priv->in_click)
4817 gtk_label_select_region (label, 0, -1);
4821 if (priv->select_info->links && !priv->in_click)
4823 link = priv->select_info->links->data;
4824 priv->select_info->selection_anchor = link->start;
4825 priv->select_info->selection_end = link->start;
4831 gtk_label_focus (GtkWidget *widget,
4832 GtkDirectionType direction)
4834 GtkLabel *label = GTK_LABEL (widget);
4835 GtkLabelPrivate *priv = label->priv;
4836 GtkLabelSelectionInfo *info = priv->select_info;
4837 GtkLabelLink *focus_link;
4840 if (!gtk_widget_is_focus (widget))
4842 gtk_widget_grab_focus (widget);
4845 focus_link = gtk_label_get_focus_link (label);
4846 if (focus_link && direction == GTK_DIR_TAB_BACKWARD)
4848 l = g_list_last (info->links);
4849 focus_link = l->data;
4850 info->selection_anchor = focus_link->start;
4851 info->selection_end = focus_link->start;
4861 if (info->selectable)
4865 if (info->selection_anchor != info->selection_end)
4868 index = info->selection_anchor;
4870 if (direction == GTK_DIR_TAB_FORWARD)
4871 for (l = info->links; l; l = l->next)
4873 GtkLabelLink *link = l->data;
4875 if (link->start > index)
4877 gtk_label_select_region_index (label, link->start, link->start);
4881 else if (direction == GTK_DIR_TAB_BACKWARD)
4882 for (l = g_list_last (info->links); l; l = l->prev)
4884 GtkLabelLink *link = l->data;
4886 if (link->end < index)
4888 gtk_label_select_region_index (label, link->start, link->start);
4897 focus_link = gtk_label_get_focus_link (label);
4900 case GTK_DIR_TAB_FORWARD:
4903 l = g_list_find (info->links, focus_link);
4910 case GTK_DIR_TAB_BACKWARD:
4913 l = g_list_find (info->links, focus_link);
4917 l = g_list_last (info->links);
4926 focus_link = l->data;
4927 info->selection_anchor = focus_link->start;
4928 info->selection_end = focus_link->start;
4929 gtk_widget_queue_draw (widget);
4941 gtk_label_button_press (GtkWidget *widget,
4942 GdkEventButton *event)
4944 GtkLabel *label = GTK_LABEL (widget);
4945 GtkLabelPrivate *priv = label->priv;
4946 GtkLabelSelectionInfo *info = priv->select_info;
4953 if (info->active_link)
4955 if (event->button == 1)
4957 info->link_clicked = 1;
4958 gtk_widget_queue_draw (widget);
4960 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4962 info->link_clicked = 1;
4963 gtk_label_do_popup (label, event);
4968 if (!info->selectable)
4971 info->in_drag = FALSE;
4972 info->select_words = FALSE;
4974 if (event->button == 1)
4976 if (!gtk_widget_has_focus (widget))
4978 priv->in_click = TRUE;
4979 gtk_widget_grab_focus (widget);
4980 priv->in_click = FALSE;
4983 if (event->type == GDK_3BUTTON_PRESS)
4985 gtk_label_select_region_index (label, 0, strlen (priv->text));
4989 if (event->type == GDK_2BUTTON_PRESS)
4991 info->select_words = TRUE;
4992 gtk_label_select_word (label);
4996 get_layout_index (label, event->x, event->y, &index);
4998 min = MIN (info->selection_anchor, info->selection_end);
4999 max = MAX (info->selection_anchor, info->selection_end);
5001 if ((info->selection_anchor != info->selection_end) &&
5002 (event->state & GDK_SHIFT_MASK))
5004 /* extend (same as motion) */
5005 min = MIN (min, index);
5006 max = MAX (max, index);
5008 /* ensure the anchor is opposite index */
5016 gtk_label_select_region_index (label, min, max);
5020 if (event->type == GDK_3BUTTON_PRESS)
5021 gtk_label_select_region_index (label, 0, strlen (priv->text));
5022 else if (event->type == GDK_2BUTTON_PRESS)
5023 gtk_label_select_word (label);
5024 else if (min < max && min <= index && index <= max)
5026 info->in_drag = TRUE;
5027 info->drag_start_x = event->x;
5028 info->drag_start_y = event->y;
5031 /* start a replacement */
5032 gtk_label_select_region_index (label, index, index);
5037 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
5039 gtk_label_do_popup (label, event);
5047 gtk_label_button_release (GtkWidget *widget,
5048 GdkEventButton *event)
5051 GtkLabel *label = GTK_LABEL (widget);
5052 GtkLabelPrivate *priv = label->priv;
5053 GtkLabelSelectionInfo *info = priv->select_info;
5063 get_layout_index (label, event->x, event->y, &index);
5064 gtk_label_select_region_index (label, index, index);
5069 if (event->button != 1)
5072 if (info->active_link &&
5073 info->selection_anchor == info->selection_end &&
5076 emit_activate_link (label, info->active_link);
5077 info->link_clicked = 0;
5082 /* The goal here is to return TRUE iff we ate the
5083 * button press to start selecting.
5089 connect_mnemonics_visible_notify (GtkLabel *label)
5091 GtkLabelPrivate *priv = label->priv;
5092 GtkWidget *toplevel;
5095 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
5097 if (!GTK_IS_WINDOW (toplevel))
5100 /* always set up this widgets initial value */
5101 priv->mnemonics_visible =
5102 gtk_window_get_mnemonics_visible (GTK_WINDOW (toplevel));
5105 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (toplevel),
5106 "gtk-label-mnemonics-visible-connected"));
5110 g_signal_connect (toplevel,
5111 "notify::mnemonics-visible",
5112 G_CALLBACK (label_mnemonics_visible_changed),
5114 g_object_set_data (G_OBJECT (toplevel),
5115 "gtk-label-mnemonics-visible-connected",
5116 GINT_TO_POINTER (1));
5121 drag_begin_cb (GtkWidget *widget,
5122 GdkDragContext *context,
5125 GtkLabel *label = GTK_LABEL (widget);
5126 GtkLabelPrivate *priv = label->priv;
5127 cairo_surface_t *surface = NULL;
5129 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
5131 if ((priv->select_info->selection_anchor !=
5132 priv->select_info->selection_end) &&
5138 start = MIN (priv->select_info->selection_anchor,
5139 priv->select_info->selection_end);
5140 end = MAX (priv->select_info->selection_anchor,
5141 priv->select_info->selection_end);
5143 len = strlen (priv->text);
5151 surface = _gtk_text_util_create_drag_icon (widget,
5158 gtk_drag_set_icon_surface (context, surface);
5159 cairo_surface_destroy (surface);
5163 gtk_drag_set_icon_default (context);
5168 gtk_label_motion (GtkWidget *widget,
5169 GdkEventMotion *event)
5171 GtkLabel *label = GTK_LABEL (widget);
5172 GtkLabelPrivate *priv = label->priv;
5173 GtkLabelSelectionInfo *info = priv->select_info;
5179 if (info->links && !info->in_drag)
5183 gboolean found = FALSE;
5185 if (info->selection_anchor == info->selection_end)
5187 if (get_layout_index (label, event->x, event->y, &index))
5189 for (l = info->links; l != NULL; l = l->next)
5192 if (index >= link->start && index <= link->end)
5203 if (info->active_link != link)
5205 info->link_clicked = 0;
5206 info->active_link = link;
5207 gtk_label_update_cursor (label);
5208 gtk_widget_queue_draw (widget);
5213 if (info->active_link != NULL)
5215 info->link_clicked = 0;
5216 info->active_link = NULL;
5217 gtk_label_update_cursor (label);
5218 gtk_widget_queue_draw (widget);
5223 if (!info->selectable)
5226 if ((event->state & GDK_BUTTON1_MASK) == 0)
5231 if (gtk_drag_check_threshold (widget,
5234 event->x, event->y))
5236 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
5238 gtk_target_list_add_text_targets (target_list, 0);
5240 g_signal_connect (widget, "drag-begin",
5241 G_CALLBACK (drag_begin_cb), NULL);
5242 gtk_drag_begin (widget, target_list,
5244 1, (GdkEvent *)event);
5246 info->in_drag = FALSE;
5248 gtk_target_list_unref (target_list);
5255 gdk_window_get_device_position (info->window, event->device, &x, &y, NULL);
5256 get_layout_index (label, x, y, &index);
5258 if (info->select_words)
5261 gint old_min, old_max;
5264 min = gtk_label_move_backward_word (label, index);
5265 max = gtk_label_move_forward_word (label, index);
5267 anchor = info->selection_anchor;
5268 end = info->selection_end;
5270 old_min = MIN (anchor, end);
5271 old_max = MAX (anchor, end);
5278 else if (old_max < max)
5283 else if (anchor == old_min)
5294 gtk_label_select_region_index (label, anchor, end);
5297 gtk_label_select_region_index (label, info->selection_anchor, index);
5304 gtk_label_leave_notify (GtkWidget *widget,
5305 GdkEventCrossing *event)
5307 GtkLabel *label = GTK_LABEL (widget);
5308 GtkLabelPrivate *priv = label->priv;
5310 if (priv->select_info)
5312 priv->select_info->active_link = NULL;
5313 gtk_label_update_cursor (label);
5314 gtk_widget_queue_draw (widget);
5317 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event)
5318 return GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event (widget, event);
5324 gtk_label_create_window (GtkLabel *label)
5326 GtkLabelPrivate *priv = label->priv;
5327 GtkAllocation allocation;
5329 GdkWindowAttr attributes;
5330 gint attributes_mask;
5332 g_assert (priv->select_info);
5333 widget = GTK_WIDGET (label);
5334 g_assert (gtk_widget_get_realized (widget));
5336 if (priv->select_info->window)
5339 gtk_widget_get_allocation (widget, &allocation);
5341 attributes.x = allocation.x;
5342 attributes.y = allocation.y;
5343 attributes.width = allocation.width;
5344 attributes.height = allocation.height;
5345 attributes.window_type = GDK_WINDOW_CHILD;
5346 attributes.wclass = GDK_INPUT_ONLY;
5347 attributes.override_redirect = TRUE;
5348 attributes.event_mask = gtk_widget_get_events (widget) |
5349 GDK_BUTTON_PRESS_MASK |
5350 GDK_BUTTON_RELEASE_MASK |
5351 GDK_LEAVE_NOTIFY_MASK |
5352 GDK_BUTTON_MOTION_MASK |
5353 GDK_POINTER_MOTION_MASK |
5354 GDK_POINTER_MOTION_HINT_MASK;
5355 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
5356 if (gtk_widget_is_sensitive (widget))
5358 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
5360 attributes_mask |= GDK_WA_CURSOR;
5364 priv->select_info->window = gdk_window_new (gtk_widget_get_window (widget),
5365 &attributes, attributes_mask);
5366 gdk_window_set_user_data (priv->select_info->window, widget);
5368 if (attributes_mask & GDK_WA_CURSOR)
5369 g_object_unref (attributes.cursor);
5373 gtk_label_destroy_window (GtkLabel *label)
5375 GtkLabelPrivate *priv = label->priv;
5377 g_assert (priv->select_info);
5379 if (priv->select_info->window == NULL)
5382 gdk_window_set_user_data (priv->select_info->window, NULL);
5383 gdk_window_destroy (priv->select_info->window);
5384 priv->select_info->window = NULL;
5388 gtk_label_ensure_select_info (GtkLabel *label)
5390 GtkLabelPrivate *priv = label->priv;
5392 if (priv->select_info == NULL)
5394 priv->select_info = g_new0 (GtkLabelSelectionInfo, 1);
5396 gtk_widget_set_can_focus (GTK_WIDGET (label), TRUE);
5398 if (gtk_widget_get_realized (GTK_WIDGET (label)))
5399 gtk_label_create_window (label);
5401 if (gtk_widget_get_mapped (GTK_WIDGET (label)))
5402 gdk_window_show (priv->select_info->window);
5407 gtk_label_clear_select_info (GtkLabel *label)
5409 GtkLabelPrivate *priv = label->priv;
5411 if (priv->select_info == NULL)
5414 if (!priv->select_info->selectable && !priv->select_info->links)
5416 gtk_label_destroy_window (label);
5418 g_free (priv->select_info);
5419 priv->select_info = NULL;
5421 gtk_widget_set_can_focus (GTK_WIDGET (label), FALSE);
5426 * gtk_label_set_selectable:
5427 * @label: a #GtkLabel
5428 * @setting: %TRUE to allow selecting text in the label
5430 * Selectable labels allow the user to select text from the label, for
5434 gtk_label_set_selectable (GtkLabel *label,
5437 GtkLabelPrivate *priv;
5438 gboolean old_setting;
5440 g_return_if_fail (GTK_IS_LABEL (label));
5444 setting = setting != FALSE;
5445 old_setting = priv->select_info && priv->select_info->selectable;
5449 gtk_label_ensure_select_info (label);
5450 priv->select_info->selectable = TRUE;
5451 gtk_label_update_cursor (label);
5457 /* unselect, to give up the selection */
5458 gtk_label_select_region (label, 0, 0);
5460 priv->select_info->selectable = FALSE;
5461 gtk_label_clear_select_info (label);
5462 gtk_label_update_cursor (label);
5465 if (setting != old_setting)
5467 g_object_freeze_notify (G_OBJECT (label));
5468 g_object_notify (G_OBJECT (label), "selectable");
5469 g_object_notify (G_OBJECT (label), "cursor-position");
5470 g_object_notify (G_OBJECT (label), "selection-bound");
5471 g_object_thaw_notify (G_OBJECT (label));
5472 gtk_widget_queue_draw (GTK_WIDGET (label));
5477 * gtk_label_get_selectable:
5478 * @label: a #GtkLabel
5480 * Gets the value set by gtk_label_set_selectable().
5482 * Return value: %TRUE if the user can copy text from the label
5485 gtk_label_get_selectable (GtkLabel *label)
5487 GtkLabelPrivate *priv;
5489 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5493 return priv->select_info && priv->select_info->selectable;
5497 * gtk_label_set_angle:
5498 * @label: a #GtkLabel
5499 * @angle: the angle that the baseline of the label makes with
5500 * the horizontal, in degrees, measured counterclockwise
5502 * Sets the angle of rotation for the label. An angle of 90 reads from
5503 * from bottom to top, an angle of 270, from top to bottom. The angle
5504 * setting for the label is ignored if the label is selectable,
5505 * wrapped, or ellipsized.
5510 gtk_label_set_angle (GtkLabel *label,
5513 GtkLabelPrivate *priv;
5515 g_return_if_fail (GTK_IS_LABEL (label));
5519 /* Canonicalize to [0,360]. We don't canonicalize 360 to 0, because
5520 * double property ranges are inclusive, and changing 360 to 0 would
5521 * make a property editor behave strangely.
5523 if (angle < 0 || angle > 360.0)
5524 angle = angle - 360. * floor (angle / 360.);
5526 if (priv->angle != angle)
5528 priv->angle = angle;
5530 gtk_label_clear_layout (label);
5531 gtk_widget_queue_resize (GTK_WIDGET (label));
5533 g_object_notify (G_OBJECT (label), "angle");
5538 * gtk_label_get_angle:
5539 * @label: a #GtkLabel
5541 * Gets the angle of rotation for the label. See
5542 * gtk_label_set_angle().
5544 * Return value: the angle of rotation for the label
5549 gtk_label_get_angle (GtkLabel *label)
5551 g_return_val_if_fail (GTK_IS_LABEL (label), 0.0);
5553 return label->priv->angle;
5557 gtk_label_set_selection_text (GtkLabel *label,
5558 GtkSelectionData *selection_data)
5560 GtkLabelPrivate *priv = label->priv;
5562 if ((priv->select_info->selection_anchor !=
5563 priv->select_info->selection_end) &&
5569 start = MIN (priv->select_info->selection_anchor,
5570 priv->select_info->selection_end);
5571 end = MAX (priv->select_info->selection_anchor,
5572 priv->select_info->selection_end);
5574 len = strlen (priv->text);
5582 gtk_selection_data_set_text (selection_data,
5589 gtk_label_drag_data_get (GtkWidget *widget,
5590 GdkDragContext *context,
5591 GtkSelectionData *selection_data,
5595 gtk_label_set_selection_text (GTK_LABEL (widget), selection_data);
5599 get_text_callback (GtkClipboard *clipboard,
5600 GtkSelectionData *selection_data,
5602 gpointer user_data_or_owner)
5604 gtk_label_set_selection_text (GTK_LABEL (user_data_or_owner), selection_data);
5608 clear_text_callback (GtkClipboard *clipboard,
5609 gpointer user_data_or_owner)
5612 GtkLabelPrivate *priv;
5614 label = GTK_LABEL (user_data_or_owner);
5617 if (priv->select_info)
5619 priv->select_info->selection_anchor = priv->select_info->selection_end;
5621 gtk_widget_queue_draw (GTK_WIDGET (label));
5626 gtk_label_select_region_index (GtkLabel *label,
5630 GtkLabelPrivate *priv;
5632 g_return_if_fail (GTK_IS_LABEL (label));
5636 if (priv->select_info && priv->select_info->selectable)
5638 GtkClipboard *clipboard;
5640 if (priv->select_info->selection_anchor == anchor_index &&
5641 priv->select_info->selection_end == end_index)
5644 priv->select_info->selection_anchor = anchor_index;
5645 priv->select_info->selection_end = end_index;
5647 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label),
5648 GDK_SELECTION_PRIMARY);
5650 if (anchor_index != end_index)
5652 GtkTargetList *list;
5653 GtkTargetEntry *targets;
5656 list = gtk_target_list_new (NULL, 0);
5657 gtk_target_list_add_text_targets (list, 0);
5658 targets = gtk_target_table_new_from_list (list, &n_targets);
5660 gtk_clipboard_set_with_owner (clipboard,
5663 clear_text_callback,
5666 gtk_target_table_free (targets, n_targets);
5667 gtk_target_list_unref (list);
5671 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (label))
5672 gtk_clipboard_clear (clipboard);
5675 gtk_widget_queue_draw (GTK_WIDGET (label));
5677 g_object_freeze_notify (G_OBJECT (label));
5678 g_object_notify (G_OBJECT (label), "cursor-position");
5679 g_object_notify (G_OBJECT (label), "selection-bound");
5680 g_object_thaw_notify (G_OBJECT (label));
5685 * gtk_label_select_region:
5686 * @label: a #GtkLabel
5687 * @start_offset: start offset (in characters not bytes)
5688 * @end_offset: end offset (in characters not bytes)
5690 * Selects a range of characters in the label, if the label is selectable.
5691 * See gtk_label_set_selectable(). If the label is not selectable,
5692 * this function has no effect. If @start_offset or
5693 * @end_offset are -1, then the end of the label will be substituted.
5696 gtk_label_select_region (GtkLabel *label,
5700 GtkLabelPrivate *priv;
5702 g_return_if_fail (GTK_IS_LABEL (label));
5706 if (priv->text && priv->select_info)
5708 if (start_offset < 0)
5709 start_offset = g_utf8_strlen (priv->text, -1);
5712 end_offset = g_utf8_strlen (priv->text, -1);
5714 gtk_label_select_region_index (label,
5715 g_utf8_offset_to_pointer (priv->text, start_offset) - priv->text,
5716 g_utf8_offset_to_pointer (priv->text, end_offset) - priv->text);
5721 * gtk_label_get_selection_bounds:
5722 * @label: a #GtkLabel
5723 * @start: (out): return location for start of selection, as a character offset
5724 * @end: (out): return location for end of selection, as a character offset
5726 * Gets the selected range of characters in the label, returning %TRUE
5727 * if there's a selection.
5729 * Return value: %TRUE if selection is non-empty
5732 gtk_label_get_selection_bounds (GtkLabel *label,
5736 GtkLabelPrivate *priv;
5738 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5742 if (priv->select_info == NULL)
5744 /* not a selectable label */
5754 gint start_index, end_index;
5755 gint start_offset, end_offset;
5758 start_index = MIN (priv->select_info->selection_anchor,
5759 priv->select_info->selection_end);
5760 end_index = MAX (priv->select_info->selection_anchor,
5761 priv->select_info->selection_end);
5763 len = strlen (priv->text);
5765 if (end_index > len)
5768 if (start_index > len)
5771 start_offset = g_utf8_strlen (priv->text, start_index);
5772 end_offset = g_utf8_strlen (priv->text, end_index);
5774 if (start_offset > end_offset)
5776 gint tmp = start_offset;
5777 start_offset = end_offset;
5782 *start = start_offset;
5787 return start_offset != end_offset;
5793 * gtk_label_get_layout:
5794 * @label: a #GtkLabel
5796 * Gets the #PangoLayout used to display the label.
5797 * The layout is useful to e.g. convert text positions to
5798 * pixel positions, in combination with gtk_label_get_layout_offsets().
5799 * The returned layout is owned by the @label so need not be
5800 * freed by the caller. The @label is free to recreate its layout at
5801 * any time, so it should be considered read-only.
5803 * Return value: (transfer none): the #PangoLayout for this label
5806 gtk_label_get_layout (GtkLabel *label)
5808 GtkLabelPrivate *priv;
5810 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
5814 gtk_label_ensure_layout (label);
5816 return priv->layout;
5820 * gtk_label_get_layout_offsets:
5821 * @label: a #GtkLabel
5822 * @x: (out) (allow-none): location to store X offset of layout, or %NULL
5823 * @y: (out) (allow-none): location to store Y offset of layout, or %NULL
5825 * Obtains the coordinates where the label will draw the #PangoLayout
5826 * representing the text in the label; useful to convert mouse events
5827 * into coordinates inside the #PangoLayout, e.g. to take some action
5828 * if some part of the label is clicked. Of course you will need to
5829 * create a #GtkEventBox to receive the events, and pack the label
5830 * inside it, since labels are a #GTK_NO_WINDOW widget. Remember
5831 * when using the #PangoLayout functions you need to convert to
5832 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
5835 gtk_label_get_layout_offsets (GtkLabel *label,
5839 g_return_if_fail (GTK_IS_LABEL (label));
5841 gtk_label_ensure_layout (label);
5843 get_layout_location (label, x, y);
5847 * gtk_label_set_use_markup:
5848 * @label: a #GtkLabel
5849 * @setting: %TRUE if the label's text should be parsed for markup.
5851 * Sets whether the text of the label contains markup in <link
5852 * linkend="PangoMarkupFormat">Pango's text markup
5853 * language</link>. See gtk_label_set_markup().
5856 gtk_label_set_use_markup (GtkLabel *label,
5859 g_return_if_fail (GTK_IS_LABEL (label));
5861 gtk_label_set_use_markup_internal (label, setting);
5862 gtk_label_recalculate (label);
5866 * gtk_label_get_use_markup:
5867 * @label: a #GtkLabel
5869 * Returns whether the label's text is interpreted as marked up with
5870 * the <link linkend="PangoMarkupFormat">Pango text markup
5871 * language</link>. See gtk_label_set_use_markup ().
5873 * Return value: %TRUE if the label's text will be parsed for markup.
5876 gtk_label_get_use_markup (GtkLabel *label)
5878 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5880 return label->priv->use_markup;
5884 * gtk_label_set_use_underline:
5885 * @label: a #GtkLabel
5886 * @setting: %TRUE if underlines in the text indicate mnemonics
5888 * If true, an underline in the text indicates the next character should be
5889 * used for the mnemonic accelerator key.
5892 gtk_label_set_use_underline (GtkLabel *label,
5895 g_return_if_fail (GTK_IS_LABEL (label));
5897 gtk_label_set_use_underline_internal (label, setting);
5898 gtk_label_recalculate (label);
5902 * gtk_label_get_use_underline:
5903 * @label: a #GtkLabel
5905 * Returns whether an embedded underline in the label indicates a
5906 * mnemonic. See gtk_label_set_use_underline().
5908 * Return value: %TRUE whether an embedded underline in the label indicates
5909 * the mnemonic accelerator keys.
5912 gtk_label_get_use_underline (GtkLabel *label)
5914 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5916 return label->priv->use_underline;
5920 * gtk_label_set_single_line_mode:
5921 * @label: a #GtkLabel
5922 * @single_line_mode: %TRUE if the label should be in single line mode
5924 * Sets whether the label is in single line mode.
5929 gtk_label_set_single_line_mode (GtkLabel *label,
5930 gboolean single_line_mode)
5932 GtkLabelPrivate *priv;
5934 g_return_if_fail (GTK_IS_LABEL (label));
5938 single_line_mode = single_line_mode != FALSE;
5940 if (priv->single_line_mode != single_line_mode)
5942 priv->single_line_mode = single_line_mode;
5944 gtk_label_clear_layout (label);
5945 gtk_widget_queue_resize (GTK_WIDGET (label));
5947 g_object_notify (G_OBJECT (label), "single-line-mode");
5952 * gtk_label_get_single_line_mode:
5953 * @label: a #GtkLabel
5955 * Returns whether the label is in single line mode.
5957 * Return value: %TRUE when the label is in single line mode.
5962 gtk_label_get_single_line_mode (GtkLabel *label)
5964 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5966 return label->priv->single_line_mode;
5969 /* Compute the X position for an offset that corresponds to the "more important
5970 * cursor position for that offset. We use this when trying to guess to which
5971 * end of the selection we should go to when the user hits the left or
5975 get_better_cursor (GtkLabel *label,
5980 GtkLabelPrivate *priv = label->priv;
5981 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5982 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5983 PangoDirection cursor_direction = get_cursor_direction (label);
5984 gboolean split_cursor;
5985 PangoRectangle strong_pos, weak_pos;
5987 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5988 "gtk-split-cursor", &split_cursor,
5991 gtk_label_ensure_layout (label);
5993 pango_layout_get_cursor_pos (priv->layout, index,
5994 &strong_pos, &weak_pos);
5998 *x = strong_pos.x / PANGO_SCALE;
5999 *y = strong_pos.y / PANGO_SCALE;
6003 if (keymap_direction == cursor_direction)
6005 *x = strong_pos.x / PANGO_SCALE;
6006 *y = strong_pos.y / PANGO_SCALE;
6010 *x = weak_pos.x / PANGO_SCALE;
6011 *y = weak_pos.y / PANGO_SCALE;
6018 gtk_label_move_logically (GtkLabel *label,
6022 GtkLabelPrivate *priv = label->priv;
6023 gint offset = g_utf8_pointer_to_offset (priv->text,
6024 priv->text + start);
6028 PangoLogAttr *log_attrs;
6032 gtk_label_ensure_layout (label);
6034 length = g_utf8_strlen (priv->text, -1);
6036 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
6038 while (count > 0 && offset < length)
6042 while (offset < length && !log_attrs[offset].is_cursor_position);
6046 while (count < 0 && offset > 0)
6050 while (offset > 0 && !log_attrs[offset].is_cursor_position);
6058 return g_utf8_offset_to_pointer (priv->text, offset) - priv->text;
6062 gtk_label_move_visually (GtkLabel *label,
6066 GtkLabelPrivate *priv = label->priv;
6073 int new_index, new_trailing;
6074 gboolean split_cursor;
6077 gtk_label_ensure_layout (label);
6079 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
6080 "gtk-split-cursor", &split_cursor,
6087 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
6088 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
6090 strong = keymap_direction == get_cursor_direction (label);
6095 pango_layout_move_cursor_visually (priv->layout, strong, index, 0, 1, &new_index, &new_trailing);
6100 pango_layout_move_cursor_visually (priv->layout, strong, index, 0, -1, &new_index, &new_trailing);
6104 if (new_index < 0 || new_index == G_MAXINT)
6109 while (new_trailing--)
6110 index = g_utf8_next_char (priv->text + new_index) - priv->text;
6117 gtk_label_move_forward_word (GtkLabel *label,
6120 GtkLabelPrivate *priv = label->priv;
6121 gint new_pos = g_utf8_pointer_to_offset (priv->text,
6122 priv->text + start);
6125 length = g_utf8_strlen (priv->text, -1);
6126 if (new_pos < length)
6128 PangoLogAttr *log_attrs;
6131 gtk_label_ensure_layout (label);
6133 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
6135 /* Find the next word end */
6137 while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
6143 return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
6148 gtk_label_move_backward_word (GtkLabel *label,
6151 GtkLabelPrivate *priv = label->priv;
6152 gint new_pos = g_utf8_pointer_to_offset (priv->text,
6153 priv->text + start);
6157 PangoLogAttr *log_attrs;
6160 gtk_label_ensure_layout (label);
6162 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
6166 /* Find the previous word beginning */
6167 while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
6173 return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
6177 gtk_label_move_cursor (GtkLabel *label,
6178 GtkMovementStep step,
6180 gboolean extend_selection)
6182 GtkLabelPrivate *priv = label->priv;
6186 if (priv->select_info == NULL)
6189 old_pos = new_pos = priv->select_info->selection_end;
6191 if (priv->select_info->selection_end != priv->select_info->selection_anchor &&
6194 /* If we have a current selection and aren't extending it, move to the
6195 * start/or end of the selection as appropriate
6199 case GTK_MOVEMENT_VISUAL_POSITIONS:
6202 gint anchor_x, anchor_y;
6203 gboolean end_is_left;
6205 get_better_cursor (label, priv->select_info->selection_end, &end_x, &end_y);
6206 get_better_cursor (label, priv->select_info->selection_anchor, &anchor_x, &anchor_y);
6208 end_is_left = (end_y < anchor_y) || (end_y == anchor_y && end_x < anchor_x);
6211 new_pos = end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
6213 new_pos = !end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
6216 case GTK_MOVEMENT_LOGICAL_POSITIONS:
6217 case GTK_MOVEMENT_WORDS:
6219 new_pos = MIN (priv->select_info->selection_end, priv->select_info->selection_anchor);
6221 new_pos = MAX (priv->select_info->selection_end, priv->select_info->selection_anchor);
6223 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
6224 case GTK_MOVEMENT_PARAGRAPH_ENDS:
6225 case GTK_MOVEMENT_BUFFER_ENDS:
6226 /* FIXME: Can do better here */
6227 new_pos = count < 0 ? 0 : strlen (priv->text);
6229 case GTK_MOVEMENT_DISPLAY_LINES:
6230 case GTK_MOVEMENT_PARAGRAPHS:
6231 case GTK_MOVEMENT_PAGES:
6232 case GTK_MOVEMENT_HORIZONTAL_PAGES:
6240 case GTK_MOVEMENT_LOGICAL_POSITIONS:
6241 new_pos = gtk_label_move_logically (label, new_pos, count);
6243 case GTK_MOVEMENT_VISUAL_POSITIONS:
6244 new_pos = gtk_label_move_visually (label, new_pos, count);
6245 if (new_pos == old_pos)
6247 if (!extend_selection)
6249 if (!gtk_widget_keynav_failed (GTK_WIDGET (label),
6251 GTK_DIR_RIGHT : GTK_DIR_LEFT))
6253 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
6256 gtk_widget_child_focus (toplevel,
6258 GTK_DIR_RIGHT : GTK_DIR_LEFT);
6263 gtk_widget_error_bell (GTK_WIDGET (label));
6267 case GTK_MOVEMENT_WORDS:
6270 new_pos = gtk_label_move_forward_word (label, new_pos);
6275 new_pos = gtk_label_move_backward_word (label, new_pos);
6278 if (new_pos == old_pos)
6279 gtk_widget_error_bell (GTK_WIDGET (label));
6281 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
6282 case GTK_MOVEMENT_PARAGRAPH_ENDS:
6283 case GTK_MOVEMENT_BUFFER_ENDS:
6284 /* FIXME: Can do better here */
6285 new_pos = count < 0 ? 0 : strlen (priv->text);
6286 if (new_pos == old_pos)
6287 gtk_widget_error_bell (GTK_WIDGET (label));
6289 case GTK_MOVEMENT_DISPLAY_LINES:
6290 case GTK_MOVEMENT_PARAGRAPHS:
6291 case GTK_MOVEMENT_PAGES:
6292 case GTK_MOVEMENT_HORIZONTAL_PAGES:
6297 if (extend_selection)
6298 gtk_label_select_region_index (label,
6299 priv->select_info->selection_anchor,
6302 gtk_label_select_region_index (label, new_pos, new_pos);
6306 gtk_label_copy_clipboard (GtkLabel *label)
6308 GtkLabelPrivate *priv = label->priv;
6310 if (priv->text && priv->select_info)
6314 GtkClipboard *clipboard;
6316 start = MIN (priv->select_info->selection_anchor,
6317 priv->select_info->selection_end);
6318 end = MAX (priv->select_info->selection_anchor,
6319 priv->select_info->selection_end);
6321 len = strlen (priv->text);
6329 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6332 gtk_clipboard_set_text (clipboard, priv->text + start, end - start);
6337 link = gtk_label_get_focus_link (label);
6339 gtk_clipboard_set_text (clipboard, link->uri, -1);
6345 gtk_label_select_all (GtkLabel *label)
6347 GtkLabelPrivate *priv = label->priv;
6349 gtk_label_select_region_index (label, 0, strlen (priv->text));
6352 /* Quick hack of a popup menu
6355 activate_cb (GtkWidget *menuitem,
6358 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6359 g_signal_emit_by_name (label, signal);
6363 append_action_signal (GtkLabel *label,
6365 const gchar *stock_id,
6366 const gchar *signal,
6369 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6371 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
6372 g_signal_connect (menuitem, "activate",
6373 G_CALLBACK (activate_cb), label);
6375 gtk_widget_set_sensitive (menuitem, sensitive);
6377 gtk_widget_show (menuitem);
6378 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6382 popup_menu_detach (GtkWidget *attach_widget,
6385 GtkLabel *label = GTK_LABEL (attach_widget);
6386 GtkLabelPrivate *priv = label->priv;
6388 if (priv->select_info)
6389 priv->select_info->popup_menu = NULL;
6393 popup_position_func (GtkMenu *menu,
6401 GtkAllocation allocation;
6405 label = GTK_LABEL (user_data);
6406 widget = GTK_WIDGET (label);
6408 g_return_if_fail (gtk_widget_get_realized (widget));
6410 screen = gtk_widget_get_screen (widget);
6411 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
6413 gtk_widget_get_allocation (widget, &allocation);
6418 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
6421 gtk_widget_get_allocation (widget, &allocation);
6423 *x += allocation.width / 2;
6424 *y += allocation.height;
6426 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
6427 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
6431 open_link_activate_cb (GtkMenuItem *menu_item,
6436 link = gtk_label_get_current_link (label);
6439 emit_activate_link (label, link);
6443 copy_link_activate_cb (GtkMenuItem *menu_item,
6446 GtkClipboard *clipboard;
6449 uri = gtk_label_get_current_uri (label);
6452 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6453 gtk_clipboard_set_text (clipboard, uri, -1);
6458 gtk_label_popup_menu (GtkWidget *widget)
6460 gtk_label_do_popup (GTK_LABEL (widget), NULL);
6466 gtk_label_do_popup (GtkLabel *label,
6467 GdkEventButton *event)
6469 GtkLabelPrivate *priv = label->priv;
6470 GtkWidget *menuitem;
6473 gboolean have_selection;
6476 if (!priv->select_info)
6479 if (priv->select_info->popup_menu)
6480 gtk_widget_destroy (priv->select_info->popup_menu);
6482 priv->select_info->popup_menu = menu = gtk_menu_new ();
6484 gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (label), popup_menu_detach);
6487 priv->select_info->selection_anchor != priv->select_info->selection_end;
6491 if (priv->select_info->link_clicked)
6492 link = priv->select_info->active_link;
6497 link = gtk_label_get_focus_link (label);
6499 if (!have_selection && link)
6502 menuitem = gtk_image_menu_item_new_with_mnemonic (_("_Open Link"));
6503 gtk_widget_show (menuitem);
6504 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6506 g_signal_connect (G_OBJECT (menuitem), "activate",
6507 G_CALLBACK (open_link_activate_cb), label);
6509 image = gtk_image_new_from_stock (GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_MENU);
6510 gtk_widget_show (image);
6511 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6513 /* Copy Link Address */
6514 menuitem = gtk_image_menu_item_new_with_mnemonic (_("Copy _Link Address"));
6515 gtk_widget_show (menuitem);
6516 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6518 g_signal_connect (G_OBJECT (menuitem), "activate",
6519 G_CALLBACK (copy_link_activate_cb), label);
6521 image = gtk_image_new_from_stock (GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
6522 gtk_widget_show (image);
6523 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6527 append_action_signal (label, menu, GTK_STOCK_CUT, "cut-clipboard", FALSE);
6528 append_action_signal (label, menu, GTK_STOCK_COPY, "copy-clipboard", have_selection);
6529 append_action_signal (label, menu, GTK_STOCK_PASTE, "paste-clipboard", FALSE);
6531 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
6532 gtk_widget_set_sensitive (menuitem, FALSE);
6533 gtk_widget_show (menuitem);
6534 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6536 menuitem = gtk_separator_menu_item_new ();
6537 gtk_widget_show (menuitem);
6538 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6540 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
6541 g_signal_connect_swapped (menuitem, "activate",
6542 G_CALLBACK (gtk_label_select_all), label);
6543 gtk_widget_show (menuitem);
6544 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6547 g_signal_emit (label, signals[POPULATE_POPUP], 0, menu);
6550 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6552 event->button, event->time);
6555 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6556 popup_position_func, label,
6557 0, gtk_get_current_event_time ());
6558 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
6563 gtk_label_clear_links (GtkLabel *label)
6565 GtkLabelPrivate *priv = label->priv;
6567 if (!priv->select_info)
6570 g_list_foreach (priv->select_info->links, (GFunc)link_free, NULL);
6571 g_list_free (priv->select_info->links);
6572 priv->select_info->links = NULL;
6573 priv->select_info->active_link = NULL;
6577 gtk_label_rescan_links (GtkLabel *label)
6579 GtkLabelPrivate *priv = label->priv;
6580 PangoLayout *layout = priv->layout;
6581 PangoAttrList *attlist;
6582 PangoAttrIterator *iter;
6585 if (!priv->select_info || !priv->select_info->links)
6588 attlist = pango_layout_get_attributes (layout);
6590 if (attlist == NULL)
6593 iter = pango_attr_list_get_iterator (attlist);
6595 links = priv->select_info->links;
6599 PangoAttribute *underline;
6600 PangoAttribute *color;
6602 underline = pango_attr_iterator_get (iter, PANGO_ATTR_UNDERLINE);
6603 color = pango_attr_iterator_get (iter, PANGO_ATTR_FOREGROUND);
6605 if (underline != NULL && color != NULL)
6608 PangoRectangle start_pos;
6609 PangoRectangle end_pos;
6612 pango_attr_iterator_range (iter, &start, &end);
6613 pango_layout_index_to_pos (layout, start, &start_pos);
6614 pango_layout_index_to_pos (layout, end, &end_pos);
6618 g_warning ("Ran out of links");
6622 links = links->next;
6623 link->start = start;
6626 } while (pango_attr_iterator_next (iter));
6628 pango_attr_iterator_destroy (iter);
6632 gtk_label_activate_link (GtkLabel *label,
6635 GtkWidget *widget = GTK_WIDGET (label);
6636 GError *error = NULL;
6638 if (!gtk_show_uri (gtk_widget_get_screen (widget),
6639 uri, gtk_get_current_event_time (), &error))
6641 g_warning ("Unable to show '%s': %s", uri, error->message);
6642 g_error_free (error);
6649 emit_activate_link (GtkLabel *label,
6652 GtkLabelPrivate *priv = label->priv;
6655 g_signal_emit (label, signals[ACTIVATE_LINK], 0, link->uri, &handled);
6656 if (handled && priv->track_links && !link->visited)
6658 link->visited = TRUE;
6659 /* FIXME: shouldn't have to redo everything here */
6660 gtk_label_recalculate (label);
6665 gtk_label_activate_current_link (GtkLabel *label)
6668 GtkWidget *widget = GTK_WIDGET (label);
6670 link = gtk_label_get_focus_link (label);
6674 emit_activate_link (label, link);
6678 GtkWidget *toplevel;
6680 GtkWidget *default_widget, *focus_widget;
6682 toplevel = gtk_widget_get_toplevel (widget);
6683 if (GTK_IS_WINDOW (toplevel))
6685 window = GTK_WINDOW (toplevel);
6689 default_widget = gtk_window_get_default_widget (window);
6690 focus_widget = gtk_window_get_focus (window);
6692 if (default_widget != widget &&
6693 !(widget == focus_widget && (!default_widget || !gtk_widget_is_sensitive (default_widget))))
6694 gtk_window_activate_default (window);
6700 static GtkLabelLink *
6701 gtk_label_get_current_link (GtkLabel *label)
6703 GtkLabelPrivate *priv = label->priv;
6706 if (!priv->select_info)
6709 if (priv->select_info->link_clicked)
6710 link = priv->select_info->active_link;
6712 link = gtk_label_get_focus_link (label);
6718 * gtk_label_get_current_uri:
6719 * @label: a #GtkLabel
6721 * Returns the URI for the currently active link in the label.
6722 * The active link is the one under the mouse pointer or, in a
6723 * selectable label, the link in which the text cursor is currently
6726 * This function is intended for use in a #GtkLabel::activate-link handler
6727 * or for use in a #GtkWidget::query-tooltip handler.
6729 * Returns: the currently active URI. The string is owned by GTK+ and must
6730 * not be freed or modified.
6734 G_CONST_RETURN gchar *
6735 gtk_label_get_current_uri (GtkLabel *label)
6739 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
6741 link = gtk_label_get_current_link (label);
6750 * gtk_label_set_track_visited_links:
6751 * @label: a #GtkLabel
6752 * @track_links: %TRUE to track visited links
6754 * Sets whether the label should keep track of clicked
6755 * links (and use a different color for them).
6760 gtk_label_set_track_visited_links (GtkLabel *label,
6761 gboolean track_links)
6763 GtkLabelPrivate *priv;
6765 g_return_if_fail (GTK_IS_LABEL (label));
6769 track_links = track_links != FALSE;
6771 if (priv->track_links != track_links)
6773 priv->track_links = track_links;
6775 /* FIXME: shouldn't have to redo everything here */
6776 gtk_label_recalculate (label);
6778 g_object_notify (G_OBJECT (label), "track-visited-links");
6783 * gtk_label_get_track_visited_links:
6784 * @label: a #GtkLabel
6786 * Returns whether the label is currently keeping track
6789 * Returns: %TRUE if clicked links are remembered
6794 gtk_label_get_track_visited_links (GtkLabel *label)
6796 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
6798 return label->priv->track_links;
6802 gtk_label_query_tooltip (GtkWidget *widget,
6805 gboolean keyboard_tip,
6806 GtkTooltip *tooltip)
6808 GtkLabel *label = GTK_LABEL (widget);
6809 GtkLabelPrivate *priv = label->priv;
6810 GtkLabelSelectionInfo *info = priv->select_info;
6814 if (info && info->links)
6818 if (info->selection_anchor == info->selection_end)
6819 index = info->selection_anchor;
6823 if (!get_layout_index (label, x, y, &index))
6829 for (l = info->links; l != NULL; l = l->next)
6831 GtkLabelLink *link = l->data;
6832 if (index >= link->start && index <= link->end)
6836 gtk_tooltip_set_markup (tooltip, link->title);
6845 return GTK_WIDGET_CLASS (gtk_label_parent_class)->query_tooltip (widget,