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"
35 #include "gtkmarshalers.h"
37 #include "gtkwindow.h"
38 #include "gdk/gdkkeysyms.h"
39 #include "gtkclipboard.h"
40 #include "gtkimagemenuitem.h"
42 #include "gtkseparatormenuitem.h"
43 #include "gtktextutil.h"
44 #include "gtkmenuitem.h"
45 #include "gtknotebook.h"
47 #include "gtkbindings.h"
48 #include "gtkbuildable.h"
51 #include "gtktooltip.h"
52 #include "gtkprivate.h"
55 struct _GtkLabelPrivate
57 GtkLabelSelectionInfo *select_info;
58 GtkWidget *mnemonic_widget;
59 GtkWindow *mnemonic_window;
62 PangoAttrList *effective_attrs;
65 gboolean mnemonics_visible;
72 guint use_underline : 1;
75 guint single_line_mode : 1;
76 guint have_transform : 1;
79 guint pattern_set : 1;
80 guint track_links : 1;
82 guint mnemonic_keyval;
91 /* Notes about the handling of links:
93 * Links share the GtkLabelSelectionInfo struct with selectable labels.
94 * There are some new fields for links. The links field contains the list
95 * of GtkLabelLink structs that describe the links which are embedded in
96 * the label. The active_link field points to the link under the mouse
97 * pointer. For keyboard navigation, the 'focus' link is determined by
98 * finding the link which contains the selection_anchor position.
99 * The link_clicked field is used with button press and release events
100 * to ensure that pressing inside a link and releasing outside of it
101 * does not activate the link.
103 * Links are rendered with the link-color/visited-link-color colors
104 * that are determined by the style and with an underline. When the mouse
105 * pointer is over a link, the pointer is changed to indicate the link,
106 * and the background behind the link is rendered with the base[PRELIGHT]
107 * color. While a button is pressed over a link, the background is rendered
108 * with the base[ACTIVE] color.
110 * Labels with links accept keyboard focus, and it is possible to move
111 * the focus between the embedded links using Tab/Shift-Tab. The focus
112 * is indicated by a focus rectangle that is drawn around the link text.
113 * Pressing Enter activates the focussed link, and there is a suitable
114 * context menu for links that can be opened with the Menu key. Pressing
115 * Control-C copies the link URI to the clipboard.
117 * In selectable labels with links, link functionality is only available
118 * when the selection is empty.
123 gchar *title; /* the title attribute, used as tooltip */
124 gboolean visited; /* get set when the link is activated; this flag
125 * gets preserved over later set_markup() calls
127 gint start; /* position of the link in the PangoLayout */
131 struct _GtkLabelSelectionInfo
134 gint selection_anchor;
136 GtkWidget *popup_menu;
139 GtkLabelLink *active_link;
145 guint select_words : 1;
146 guint selectable : 1;
147 guint link_clicked : 1;
155 ACTIVATE_CURRENT_LINK,
170 PROP_MNEMONIC_KEYVAL,
171 PROP_MNEMONIC_WIDGET,
172 PROP_CURSOR_POSITION,
173 PROP_SELECTION_BOUND,
176 PROP_SINGLE_LINE_MODE,
178 PROP_MAX_WIDTH_CHARS,
179 PROP_TRACK_VISITED_LINKS
182 /* When rotating ellipsizable text we want the natural size to request
183 * more to ensure the label wont ever ellipsize in an allocation of full natural size.
185 #define ROTATION_ELLIPSIZE_PADDING 2
187 static guint signals[LAST_SIGNAL] = { 0 };
189 static const GdkColor default_link_color = { 0, 0, 0, 0xeeee };
190 static const GdkColor default_visited_link_color = { 0, 0x5555, 0x1a1a, 0x8b8b };
192 static void gtk_label_set_property (GObject *object,
196 static void gtk_label_get_property (GObject *object,
200 static void gtk_label_finalize (GObject *object);
201 static void gtk_label_destroy (GtkWidget *widget);
202 static void gtk_label_size_allocate (GtkWidget *widget,
203 GtkAllocation *allocation);
204 static void gtk_label_state_changed (GtkWidget *widget,
206 static void gtk_label_style_set (GtkWidget *widget,
207 GtkStyle *previous_style);
208 static void gtk_label_direction_changed (GtkWidget *widget,
209 GtkTextDirection previous_dir);
210 static gint gtk_label_draw (GtkWidget *widget,
212 static gboolean gtk_label_focus (GtkWidget *widget,
213 GtkDirectionType direction);
215 static void gtk_label_realize (GtkWidget *widget);
216 static void gtk_label_unrealize (GtkWidget *widget);
217 static void gtk_label_map (GtkWidget *widget);
218 static void gtk_label_unmap (GtkWidget *widget);
220 static gboolean gtk_label_button_press (GtkWidget *widget,
221 GdkEventButton *event);
222 static gboolean gtk_label_button_release (GtkWidget *widget,
223 GdkEventButton *event);
224 static gboolean gtk_label_motion (GtkWidget *widget,
225 GdkEventMotion *event);
226 static gboolean gtk_label_leave_notify (GtkWidget *widget,
227 GdkEventCrossing *event);
229 static void gtk_label_grab_focus (GtkWidget *widget);
231 static gboolean gtk_label_query_tooltip (GtkWidget *widget,
234 gboolean keyboard_tip,
235 GtkTooltip *tooltip);
237 static void gtk_label_set_text_internal (GtkLabel *label,
239 static void gtk_label_set_label_internal (GtkLabel *label,
241 static void gtk_label_set_use_markup_internal (GtkLabel *label,
243 static void gtk_label_set_use_underline_internal (GtkLabel *label,
245 static void gtk_label_set_attributes_internal (GtkLabel *label,
246 PangoAttrList *attrs);
247 static void gtk_label_set_uline_text_internal (GtkLabel *label,
249 static void gtk_label_set_pattern_internal (GtkLabel *label,
250 const gchar *pattern,
251 gboolean is_mnemonic);
252 static void gtk_label_set_markup_internal (GtkLabel *label,
254 gboolean with_uline);
255 static void gtk_label_recalculate (GtkLabel *label);
256 static void gtk_label_hierarchy_changed (GtkWidget *widget,
257 GtkWidget *old_toplevel);
258 static void gtk_label_screen_changed (GtkWidget *widget,
259 GdkScreen *old_screen);
260 static gboolean gtk_label_popup_menu (GtkWidget *widget);
262 static void gtk_label_create_window (GtkLabel *label);
263 static void gtk_label_destroy_window (GtkLabel *label);
264 static void gtk_label_ensure_select_info (GtkLabel *label);
265 static void gtk_label_clear_select_info (GtkLabel *label);
266 static void gtk_label_update_cursor (GtkLabel *label);
267 static void gtk_label_clear_layout (GtkLabel *label);
268 static void gtk_label_ensure_layout (GtkLabel *label, gboolean guess_wrap_width);
269 static void gtk_label_invalidate_wrap_width (GtkLabel *label);
270 static void gtk_label_select_region_index (GtkLabel *label,
274 static gboolean gtk_label_mnemonic_activate (GtkWidget *widget,
275 gboolean group_cycling);
276 static void gtk_label_setup_mnemonic (GtkLabel *label,
278 static void gtk_label_drag_data_get (GtkWidget *widget,
279 GdkDragContext *context,
280 GtkSelectionData *selection_data,
284 static void gtk_label_buildable_interface_init (GtkBuildableIface *iface);
285 static gboolean gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
288 const gchar *tagname,
289 GMarkupParser *parser,
292 static void gtk_label_buildable_custom_finished (GtkBuildable *buildable,
295 const gchar *tagname,
299 static void connect_mnemonics_visible_notify (GtkLabel *label);
300 static gboolean separate_uline_pattern (const gchar *str,
306 /* For selectable labels: */
307 static void gtk_label_move_cursor (GtkLabel *label,
308 GtkMovementStep step,
310 gboolean extend_selection);
311 static void gtk_label_copy_clipboard (GtkLabel *label);
312 static void gtk_label_select_all (GtkLabel *label);
313 static void gtk_label_do_popup (GtkLabel *label,
314 GdkEventButton *event);
315 static gint gtk_label_move_forward_word (GtkLabel *label,
317 static gint gtk_label_move_backward_word (GtkLabel *label,
321 static void gtk_label_rescan_links (GtkLabel *label);
322 static void gtk_label_clear_links (GtkLabel *label);
323 static gboolean gtk_label_activate_link (GtkLabel *label,
325 static void gtk_label_activate_current_link (GtkLabel *label);
326 static GtkLabelLink *gtk_label_get_current_link (GtkLabel *label);
327 static void gtk_label_get_link_colors (GtkWidget *widget,
328 GdkColor **link_color,
329 GdkColor **visited_link_color);
330 static void emit_activate_link (GtkLabel *label,
333 static GtkSizeRequestMode gtk_label_get_request_mode (GtkWidget *widget);
334 static void gtk_label_get_preferred_width (GtkWidget *widget,
337 static void gtk_label_get_preferred_height (GtkWidget *widget,
340 static void gtk_label_get_preferred_width_for_height (GtkWidget *widget,
343 gint *natural_width);
344 static void gtk_label_get_preferred_height_for_width (GtkWidget *widget,
346 gint *minimum_height,
347 gint *natural_height);
349 static GtkBuildableIface *buildable_parent_iface = NULL;
351 G_DEFINE_TYPE_WITH_CODE (GtkLabel, gtk_label, GTK_TYPE_MISC,
352 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
353 gtk_label_buildable_interface_init))
356 add_move_binding (GtkBindingSet *binding_set,
359 GtkMovementStep step,
362 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
364 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
368 G_TYPE_BOOLEAN, FALSE);
370 /* Selection-extending version */
371 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
375 G_TYPE_BOOLEAN, TRUE);
379 gtk_label_class_init (GtkLabelClass *class)
381 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
382 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
383 GtkBindingSet *binding_set;
385 gobject_class->set_property = gtk_label_set_property;
386 gobject_class->get_property = gtk_label_get_property;
387 gobject_class->finalize = gtk_label_finalize;
389 widget_class->destroy = gtk_label_destroy;
390 widget_class->size_allocate = gtk_label_size_allocate;
391 widget_class->state_changed = gtk_label_state_changed;
392 widget_class->style_set = gtk_label_style_set;
393 widget_class->query_tooltip = gtk_label_query_tooltip;
394 widget_class->direction_changed = gtk_label_direction_changed;
395 widget_class->draw = gtk_label_draw;
396 widget_class->realize = gtk_label_realize;
397 widget_class->unrealize = gtk_label_unrealize;
398 widget_class->map = gtk_label_map;
399 widget_class->unmap = gtk_label_unmap;
400 widget_class->button_press_event = gtk_label_button_press;
401 widget_class->button_release_event = gtk_label_button_release;
402 widget_class->motion_notify_event = gtk_label_motion;
403 widget_class->leave_notify_event = gtk_label_leave_notify;
404 widget_class->hierarchy_changed = gtk_label_hierarchy_changed;
405 widget_class->screen_changed = gtk_label_screen_changed;
406 widget_class->mnemonic_activate = gtk_label_mnemonic_activate;
407 widget_class->drag_data_get = gtk_label_drag_data_get;
408 widget_class->grab_focus = gtk_label_grab_focus;
409 widget_class->popup_menu = gtk_label_popup_menu;
410 widget_class->focus = gtk_label_focus;
411 widget_class->get_request_mode = gtk_label_get_request_mode;
412 widget_class->get_preferred_width = gtk_label_get_preferred_width;
413 widget_class->get_preferred_height = gtk_label_get_preferred_height;
414 widget_class->get_preferred_width_for_height = gtk_label_get_preferred_width_for_height;
415 widget_class->get_preferred_height_for_width = gtk_label_get_preferred_height_for_width;
417 class->move_cursor = gtk_label_move_cursor;
418 class->copy_clipboard = gtk_label_copy_clipboard;
419 class->activate_link = gtk_label_activate_link;
422 * GtkLabel::move-cursor:
423 * @entry: the object which received the signal
424 * @step: the granularity of the move, as a #GtkMovementStep
425 * @count: the number of @step units to move
426 * @extend_selection: %TRUE if the move should extend the selection
428 * The ::move-cursor signal is a
429 * <link linkend="keybinding-signals">keybinding signal</link>
430 * which gets emitted when the user initiates a cursor movement.
431 * If the cursor is not visible in @entry, this signal causes
432 * the viewport to be moved instead.
434 * Applications should not connect to it, but may emit it with
435 * g_signal_emit_by_name() if they need to control the cursor
438 * The default bindings for this signal come in two variants,
439 * the variant with the Shift modifier extends the selection,
440 * the variant without the Shift modifer does not.
441 * There are too many key combinations to list them all here.
443 * <listitem>Arrow keys move by individual characters/lines</listitem>
444 * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
445 * <listitem>Home/End keys move to the ends of the buffer</listitem>
448 signals[MOVE_CURSOR] =
449 g_signal_new (I_("move-cursor"),
450 G_OBJECT_CLASS_TYPE (gobject_class),
451 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
452 G_STRUCT_OFFSET (GtkLabelClass, move_cursor),
454 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
456 GTK_TYPE_MOVEMENT_STEP,
461 * GtkLabel::copy-clipboard:
462 * @label: the object which received the signal
464 * The ::copy-clipboard signal is a
465 * <link linkend="keybinding-signals">keybinding signal</link>
466 * which gets emitted to copy the selection to the clipboard.
468 * The default binding for this signal is Ctrl-c.
470 signals[COPY_CLIPBOARD] =
471 g_signal_new (I_("copy-clipboard"),
472 G_OBJECT_CLASS_TYPE (gobject_class),
473 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
474 G_STRUCT_OFFSET (GtkLabelClass, copy_clipboard),
476 _gtk_marshal_VOID__VOID,
480 * GtkLabel::populate-popup:
481 * @label: The label on which the signal is emitted
482 * @menu: the menu that is being populated
484 * The ::populate-popup signal gets emitted before showing the
485 * context menu of the label. Note that only selectable labels
486 * have context menus.
488 * If you need to add items to the context menu, connect
489 * to this signal and append your menuitems to the @menu.
491 signals[POPULATE_POPUP] =
492 g_signal_new (I_("populate-popup"),
493 G_OBJECT_CLASS_TYPE (gobject_class),
495 G_STRUCT_OFFSET (GtkLabelClass, populate_popup),
497 _gtk_marshal_VOID__OBJECT,
502 * GtkLabel::activate-current-link:
503 * @label: The label on which the signal was emitted
505 * A <link linkend="keybinding-signals">keybinding signal</link>
506 * which gets emitted when the user activates a link in the label.
508 * Applications may also emit the signal with g_signal_emit_by_name()
509 * if they need to control activation of URIs programmatically.
511 * The default bindings for this signal are all forms of the Enter key.
515 signals[ACTIVATE_CURRENT_LINK] =
516 g_signal_new_class_handler ("activate-current-link",
517 G_TYPE_FROM_CLASS (gobject_class),
518 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
519 G_CALLBACK (gtk_label_activate_current_link),
521 _gtk_marshal_VOID__VOID,
525 * GtkLabel::activate-link:
526 * @label: The label on which the signal was emitted
527 * @uri: the URI that is activated
529 * The signal which gets emitted to activate a URI.
530 * Applications may connect to it to override the default behaviour,
531 * which is to call gtk_show_uri().
533 * Returns: %TRUE if the link has been activated
537 signals[ACTIVATE_LINK] =
538 g_signal_new ("activate-link",
539 G_TYPE_FROM_CLASS (gobject_class),
541 G_STRUCT_OFFSET (GtkLabelClass, activate_link),
542 _gtk_boolean_handled_accumulator, NULL,
543 _gtk_marshal_BOOLEAN__STRING,
544 G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
546 g_object_class_install_property (gobject_class,
548 g_param_spec_string ("label",
550 P_("The text of the label"),
552 GTK_PARAM_READWRITE));
553 g_object_class_install_property (gobject_class,
555 g_param_spec_boxed ("attributes",
557 P_("A list of style attributes to apply to the text of the label"),
558 PANGO_TYPE_ATTR_LIST,
559 GTK_PARAM_READWRITE));
560 g_object_class_install_property (gobject_class,
562 g_param_spec_boolean ("use-markup",
564 P_("The text of the label includes XML markup. See pango_parse_markup()"),
566 GTK_PARAM_READWRITE));
567 g_object_class_install_property (gobject_class,
569 g_param_spec_boolean ("use-underline",
571 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
573 GTK_PARAM_READWRITE));
575 g_object_class_install_property (gobject_class,
577 g_param_spec_enum ("justify",
579 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"),
580 GTK_TYPE_JUSTIFICATION,
582 GTK_PARAM_READWRITE));
584 g_object_class_install_property (gobject_class,
586 g_param_spec_string ("pattern",
588 P_("A string with _ characters in positions correspond to characters in the text to underline"),
590 GTK_PARAM_WRITABLE));
592 g_object_class_install_property (gobject_class,
594 g_param_spec_boolean ("wrap",
596 P_("If set, wrap lines if the text becomes too wide"),
598 GTK_PARAM_READWRITE));
600 * GtkLabel:wrap-mode:
602 * If line wrapping is on (see the #GtkLabel:wrap property) this controls
603 * how the line wrapping is done. The default is %PANGO_WRAP_WORD, which
604 * means wrap on word boundaries.
608 g_object_class_install_property (gobject_class,
610 g_param_spec_enum ("wrap-mode",
611 P_("Line wrap mode"),
612 P_("If wrap is set, controls how linewrapping is done"),
613 PANGO_TYPE_WRAP_MODE,
615 GTK_PARAM_READWRITE));
616 g_object_class_install_property (gobject_class,
618 g_param_spec_boolean ("selectable",
620 P_("Whether the label text can be selected with the mouse"),
622 GTK_PARAM_READWRITE));
623 g_object_class_install_property (gobject_class,
624 PROP_MNEMONIC_KEYVAL,
625 g_param_spec_uint ("mnemonic-keyval",
627 P_("The mnemonic accelerator key for this label"),
631 GTK_PARAM_READABLE));
632 g_object_class_install_property (gobject_class,
633 PROP_MNEMONIC_WIDGET,
634 g_param_spec_object ("mnemonic-widget",
635 P_("Mnemonic widget"),
636 P_("The widget to be activated when the label's mnemonic "
639 GTK_PARAM_READWRITE));
641 g_object_class_install_property (gobject_class,
642 PROP_CURSOR_POSITION,
643 g_param_spec_int ("cursor-position",
644 P_("Cursor Position"),
645 P_("The current position of the insertion cursor in chars"),
649 GTK_PARAM_READABLE));
651 g_object_class_install_property (gobject_class,
652 PROP_SELECTION_BOUND,
653 g_param_spec_int ("selection-bound",
654 P_("Selection Bound"),
655 P_("The position of the opposite end of the selection from the cursor in chars"),
659 GTK_PARAM_READABLE));
662 * GtkLabel:ellipsize:
664 * The preferred place to ellipsize the string, if the label does
665 * not have enough room to display the entire string, specified as a
666 * #PangoEllisizeMode.
668 * Note that setting this property to a value other than
669 * %PANGO_ELLIPSIZE_NONE has the side-effect that the label requests
670 * only enough space to display the ellipsis "...". In particular, this
671 * means that ellipsizing labels do not work well in notebook tabs, unless
672 * the tab's #GtkNotebook:tab-expand property is set to %TRUE. Other ways
673 * to set a label's width are gtk_widget_set_size_request() and
674 * gtk_label_set_width_chars().
678 g_object_class_install_property (gobject_class,
680 g_param_spec_enum ("ellipsize",
682 P_("The preferred place to ellipsize the string, if the label does not have enough room to display the entire string"),
683 PANGO_TYPE_ELLIPSIZE_MODE,
684 PANGO_ELLIPSIZE_NONE,
685 GTK_PARAM_READWRITE));
688 * GtkLabel:width-chars:
690 * The desired width of the label, in characters. If this property is set to
691 * -1, the width will be calculated automatically.
693 * See the section on <link linkend="label-text-layout">text layout</link>
694 * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
695 * determine the width of ellipsized and wrapped labels.
699 g_object_class_install_property (gobject_class,
701 g_param_spec_int ("width-chars",
702 P_("Width In Characters"),
703 P_("The desired width of the label, in characters"),
707 GTK_PARAM_READWRITE));
710 * GtkLabel:single-line-mode:
712 * Whether the label is in single line mode. In single line mode,
713 * the height of the label does not depend on the actual text, it
714 * is always set to ascent + descent of the font. This can be an
715 * advantage in situations where resizing the label because of text
716 * changes would be distracting, e.g. in a statusbar.
720 g_object_class_install_property (gobject_class,
721 PROP_SINGLE_LINE_MODE,
722 g_param_spec_boolean ("single-line-mode",
723 P_("Single Line Mode"),
724 P_("Whether the label is in single line mode"),
726 GTK_PARAM_READWRITE));
731 * The angle that the baseline of the label makes with the horizontal,
732 * in degrees, measured counterclockwise. An angle of 90 reads from
733 * from bottom to top, an angle of 270, from top to bottom. Ignored
734 * if the label is selectable, wrapped, or ellipsized.
738 g_object_class_install_property (gobject_class,
740 g_param_spec_double ("angle",
742 P_("Angle at which the label is rotated"),
746 GTK_PARAM_READWRITE));
749 * GtkLabel:max-width-chars:
751 * The desired maximum width of the label, in characters. If this property
752 * is set to -1, the width will be calculated automatically.
754 * See the section on <link linkend="label-text-layout">text layout</link>
755 * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
756 * determine the width of ellipsized and wrapped labels.
760 g_object_class_install_property (gobject_class,
761 PROP_MAX_WIDTH_CHARS,
762 g_param_spec_int ("max-width-chars",
763 P_("Maximum Width In Characters"),
764 P_("The desired maximum width of the label, in characters"),
768 GTK_PARAM_READWRITE));
771 * GtkLabel:track-visited-links:
773 * Set this property to %TRUE to make the label track which links
774 * have been clicked. It will then apply the ::visited-link-color
775 * color, instead of ::link-color.
779 g_object_class_install_property (gobject_class,
780 PROP_TRACK_VISITED_LINKS,
781 g_param_spec_boolean ("track-visited-links",
782 P_("Track visited links"),
783 P_("Whether visited links should be tracked"),
785 GTK_PARAM_READWRITE));
790 binding_set = gtk_binding_set_by_class (class);
792 /* Moving the insertion point */
793 add_move_binding (binding_set, GDK_KEY_Right, 0,
794 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
796 add_move_binding (binding_set, GDK_KEY_Left, 0,
797 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
799 add_move_binding (binding_set, GDK_KEY_KP_Right, 0,
800 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
802 add_move_binding (binding_set, GDK_KEY_KP_Left, 0,
803 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
805 add_move_binding (binding_set, GDK_KEY_f, GDK_CONTROL_MASK,
806 GTK_MOVEMENT_LOGICAL_POSITIONS, 1);
808 add_move_binding (binding_set, GDK_KEY_b, GDK_CONTROL_MASK,
809 GTK_MOVEMENT_LOGICAL_POSITIONS, -1);
811 add_move_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK,
812 GTK_MOVEMENT_WORDS, 1);
814 add_move_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK,
815 GTK_MOVEMENT_WORDS, -1);
817 add_move_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK,
818 GTK_MOVEMENT_WORDS, 1);
820 add_move_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK,
821 GTK_MOVEMENT_WORDS, -1);
824 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
826 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
828 G_TYPE_BOOLEAN, FALSE);
830 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
832 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
834 G_TYPE_BOOLEAN, TRUE);
836 gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
838 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
840 G_TYPE_BOOLEAN, FALSE);
842 gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
844 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
846 G_TYPE_BOOLEAN, TRUE);
849 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
851 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
853 G_TYPE_BOOLEAN, FALSE);
855 gtk_binding_entry_add_signal (binding_set, GDK_KEY_backslash, GDK_CONTROL_MASK,
857 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
859 G_TYPE_BOOLEAN, FALSE);
861 add_move_binding (binding_set, GDK_KEY_f, GDK_MOD1_MASK,
862 GTK_MOVEMENT_WORDS, 1);
864 add_move_binding (binding_set, GDK_KEY_b, GDK_MOD1_MASK,
865 GTK_MOVEMENT_WORDS, -1);
867 add_move_binding (binding_set, GDK_KEY_Home, 0,
868 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
870 add_move_binding (binding_set, GDK_KEY_End, 0,
871 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
873 add_move_binding (binding_set, GDK_KEY_KP_Home, 0,
874 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
876 add_move_binding (binding_set, GDK_KEY_KP_End, 0,
877 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
879 add_move_binding (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK,
880 GTK_MOVEMENT_BUFFER_ENDS, -1);
882 add_move_binding (binding_set, GDK_KEY_End, GDK_CONTROL_MASK,
883 GTK_MOVEMENT_BUFFER_ENDS, 1);
885 add_move_binding (binding_set, GDK_KEY_KP_Home, GDK_CONTROL_MASK,
886 GTK_MOVEMENT_BUFFER_ENDS, -1);
888 add_move_binding (binding_set, GDK_KEY_KP_End, GDK_CONTROL_MASK,
889 GTK_MOVEMENT_BUFFER_ENDS, 1);
892 gtk_binding_entry_add_signal (binding_set, GDK_KEY_c, GDK_CONTROL_MASK,
893 "copy-clipboard", 0);
895 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Return, 0,
896 "activate-current-link", 0);
897 gtk_binding_entry_add_signal (binding_set, GDK_KEY_ISO_Enter, 0,
898 "activate-current-link", 0);
899 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Enter, 0,
900 "activate-current-link", 0);
902 g_type_class_add_private (class, sizeof (GtkLabelPrivate));
906 gtk_label_set_property (GObject *object,
911 GtkLabel *label = GTK_LABEL (object);
916 gtk_label_set_label (label, g_value_get_string (value));
918 case PROP_ATTRIBUTES:
919 gtk_label_set_attributes (label, g_value_get_boxed (value));
921 case PROP_USE_MARKUP:
922 gtk_label_set_use_markup (label, g_value_get_boolean (value));
924 case PROP_USE_UNDERLINE:
925 gtk_label_set_use_underline (label, g_value_get_boolean (value));
928 gtk_label_set_justify (label, g_value_get_enum (value));
931 gtk_label_set_pattern (label, g_value_get_string (value));
934 gtk_label_set_line_wrap (label, g_value_get_boolean (value));
937 gtk_label_set_line_wrap_mode (label, g_value_get_enum (value));
939 case PROP_SELECTABLE:
940 gtk_label_set_selectable (label, g_value_get_boolean (value));
942 case PROP_MNEMONIC_WIDGET:
943 gtk_label_set_mnemonic_widget (label, (GtkWidget*) g_value_get_object (value));
946 gtk_label_set_ellipsize (label, g_value_get_enum (value));
948 case PROP_WIDTH_CHARS:
949 gtk_label_set_width_chars (label, g_value_get_int (value));
951 case PROP_SINGLE_LINE_MODE:
952 gtk_label_set_single_line_mode (label, g_value_get_boolean (value));
955 gtk_label_set_angle (label, g_value_get_double (value));
957 case PROP_MAX_WIDTH_CHARS:
958 gtk_label_set_max_width_chars (label, g_value_get_int (value));
960 case PROP_TRACK_VISITED_LINKS:
961 gtk_label_set_track_visited_links (label, g_value_get_boolean (value));
964 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
970 gtk_label_get_property (GObject *object,
975 GtkLabel *label = GTK_LABEL (object);
976 GtkLabelPrivate *priv = label->priv;
981 g_value_set_string (value, priv->label);
983 case PROP_ATTRIBUTES:
984 g_value_set_boxed (value, priv->attrs);
986 case PROP_USE_MARKUP:
987 g_value_set_boolean (value, priv->use_markup);
989 case PROP_USE_UNDERLINE:
990 g_value_set_boolean (value, priv->use_underline);
993 g_value_set_enum (value, priv->jtype);
996 g_value_set_boolean (value, priv->wrap);
999 g_value_set_enum (value, priv->wrap_mode);
1001 case PROP_SELECTABLE:
1002 g_value_set_boolean (value, gtk_label_get_selectable (label));
1004 case PROP_MNEMONIC_KEYVAL:
1005 g_value_set_uint (value, priv->mnemonic_keyval);
1007 case PROP_MNEMONIC_WIDGET:
1008 g_value_set_object (value, (GObject*) priv->mnemonic_widget);
1010 case PROP_CURSOR_POSITION:
1011 if (priv->select_info && priv->select_info->selectable)
1013 gint offset = g_utf8_pointer_to_offset (priv->text,
1014 priv->text + priv->select_info->selection_end);
1015 g_value_set_int (value, offset);
1018 g_value_set_int (value, 0);
1020 case PROP_SELECTION_BOUND:
1021 if (priv->select_info && priv->select_info->selectable)
1023 gint offset = g_utf8_pointer_to_offset (priv->text,
1024 priv->text + priv->select_info->selection_anchor);
1025 g_value_set_int (value, offset);
1028 g_value_set_int (value, 0);
1030 case PROP_ELLIPSIZE:
1031 g_value_set_enum (value, priv->ellipsize);
1033 case PROP_WIDTH_CHARS:
1034 g_value_set_int (value, gtk_label_get_width_chars (label));
1036 case PROP_SINGLE_LINE_MODE:
1037 g_value_set_boolean (value, gtk_label_get_single_line_mode (label));
1040 g_value_set_double (value, gtk_label_get_angle (label));
1042 case PROP_MAX_WIDTH_CHARS:
1043 g_value_set_int (value, gtk_label_get_max_width_chars (label));
1045 case PROP_TRACK_VISITED_LINKS:
1046 g_value_set_boolean (value, gtk_label_get_track_visited_links (label));
1049 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1055 gtk_label_init (GtkLabel *label)
1057 GtkLabelPrivate *priv;
1059 label->priv = G_TYPE_INSTANCE_GET_PRIVATE (label,
1064 gtk_widget_set_has_window (GTK_WIDGET (label), FALSE);
1066 priv->width_chars = -1;
1067 priv->max_width_chars = -1;
1068 priv->wrap_width = -1;
1071 priv->jtype = GTK_JUSTIFY_LEFT;
1073 priv->wrap_mode = PANGO_WRAP_WORD;
1074 priv->ellipsize = PANGO_ELLIPSIZE_NONE;
1076 priv->use_underline = FALSE;
1077 priv->use_markup = FALSE;
1078 priv->pattern_set = FALSE;
1079 priv->track_links = TRUE;
1081 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
1082 priv->layout = NULL;
1086 priv->mnemonic_widget = NULL;
1087 priv->mnemonic_window = NULL;
1089 priv->mnemonics_visible = TRUE;
1091 gtk_label_set_text (label, "");
1096 gtk_label_buildable_interface_init (GtkBuildableIface *iface)
1098 buildable_parent_iface = g_type_interface_peek_parent (iface);
1100 iface->custom_tag_start = gtk_label_buildable_custom_tag_start;
1101 iface->custom_finished = gtk_label_buildable_custom_finished;
1105 GtkBuilder *builder;
1107 PangoAttrList *attrs;
1110 static PangoAttribute *
1111 attribute_from_text (GtkBuilder *builder,
1116 PangoAttribute *attribute = NULL;
1118 PangoLanguage *language;
1119 PangoFontDescription *font_desc;
1121 GValue val = { 0, };
1123 if (!gtk_builder_value_from_string_type (builder, PANGO_TYPE_ATTR_TYPE, name, &val, error))
1126 type = g_value_get_enum (&val);
1127 g_value_unset (&val);
1131 /* PangoAttrLanguage */
1132 case PANGO_ATTR_LANGUAGE:
1133 if ((language = pango_language_from_string (value)))
1135 attribute = pango_attr_language_new (language);
1136 g_value_init (&val, G_TYPE_INT);
1140 case PANGO_ATTR_STYLE:
1141 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STYLE, value, &val, error))
1142 attribute = pango_attr_style_new (g_value_get_enum (&val));
1144 case PANGO_ATTR_WEIGHT:
1145 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_WEIGHT, value, &val, error))
1146 attribute = pango_attr_weight_new (g_value_get_enum (&val));
1148 case PANGO_ATTR_VARIANT:
1149 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_VARIANT, value, &val, error))
1150 attribute = pango_attr_variant_new (g_value_get_enum (&val));
1152 case PANGO_ATTR_STRETCH:
1153 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STRETCH, value, &val, error))
1154 attribute = pango_attr_stretch_new (g_value_get_enum (&val));
1156 case PANGO_ATTR_UNDERLINE:
1157 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1158 attribute = pango_attr_underline_new (g_value_get_boolean (&val));
1160 case PANGO_ATTR_STRIKETHROUGH:
1161 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1162 attribute = pango_attr_strikethrough_new (g_value_get_boolean (&val));
1164 case PANGO_ATTR_GRAVITY:
1165 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY, value, &val, error))
1166 attribute = pango_attr_gravity_new (g_value_get_enum (&val));
1168 case PANGO_ATTR_GRAVITY_HINT:
1169 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY_HINT,
1170 value, &val, error))
1171 attribute = pango_attr_gravity_hint_new (g_value_get_enum (&val));
1173 /* PangoAttrString */
1174 case PANGO_ATTR_FAMILY:
1175 attribute = pango_attr_family_new (value);
1176 g_value_init (&val, G_TYPE_INT);
1180 case PANGO_ATTR_SIZE:
1181 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1182 value, &val, error))
1183 attribute = pango_attr_size_new (g_value_get_int (&val));
1185 case PANGO_ATTR_ABSOLUTE_SIZE:
1186 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1187 value, &val, error))
1188 attribute = pango_attr_size_new_absolute (g_value_get_int (&val));
1191 /* PangoAttrFontDesc */
1192 case PANGO_ATTR_FONT_DESC:
1193 if ((font_desc = pango_font_description_from_string (value)))
1195 attribute = pango_attr_font_desc_new (font_desc);
1196 pango_font_description_free (font_desc);
1197 g_value_init (&val, G_TYPE_INT);
1201 /* PangoAttrColor */
1202 case PANGO_ATTR_FOREGROUND:
1203 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1204 value, &val, error))
1206 color = g_value_get_boxed (&val);
1207 attribute = pango_attr_foreground_new (color->red, color->green, color->blue);
1210 case PANGO_ATTR_BACKGROUND:
1211 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1212 value, &val, error))
1214 color = g_value_get_boxed (&val);
1215 attribute = pango_attr_background_new (color->red, color->green, color->blue);
1218 case PANGO_ATTR_UNDERLINE_COLOR:
1219 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1220 value, &val, error))
1222 color = g_value_get_boxed (&val);
1223 attribute = pango_attr_underline_color_new (color->red, color->green, color->blue);
1226 case PANGO_ATTR_STRIKETHROUGH_COLOR:
1227 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1228 value, &val, error))
1230 color = g_value_get_boxed (&val);
1231 attribute = pango_attr_strikethrough_color_new (color->red, color->green, color->blue);
1235 /* PangoAttrShape */
1236 case PANGO_ATTR_SHAPE:
1237 /* Unsupported for now */
1239 /* PangoAttrFloat */
1240 case PANGO_ATTR_SCALE:
1241 if (gtk_builder_value_from_string_type (builder, G_TYPE_DOUBLE,
1242 value, &val, error))
1243 attribute = pango_attr_scale_new (g_value_get_double (&val));
1246 case PANGO_ATTR_INVALID:
1247 case PANGO_ATTR_LETTER_SPACING:
1248 case PANGO_ATTR_RISE:
1249 case PANGO_ATTR_FALLBACK:
1254 g_value_unset (&val);
1261 pango_start_element (GMarkupParseContext *context,
1262 const gchar *element_name,
1263 const gchar **names,
1264 const gchar **values,
1268 PangoParserData *data = (PangoParserData*)user_data;
1269 GValue val = { 0, };
1271 gint line_number, char_number;
1273 if (strcmp (element_name, "attribute") == 0)
1275 PangoAttribute *attr = NULL;
1276 const gchar *name = NULL;
1277 const gchar *value = NULL;
1278 const gchar *start = NULL;
1279 const gchar *end = NULL;
1280 guint start_val = 0;
1281 guint end_val = G_MAXUINT;
1283 for (i = 0; names[i]; i++)
1285 if (strcmp (names[i], "name") == 0)
1287 else if (strcmp (names[i], "value") == 0)
1289 else if (strcmp (names[i], "start") == 0)
1291 else if (strcmp (names[i], "end") == 0)
1295 g_markup_parse_context_get_position (context,
1300 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1301 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1303 line_number, char_number, names[i], "attribute");
1308 if (!name || !value)
1310 g_markup_parse_context_get_position (context,
1315 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1316 "%s:%d:%d <%s> requires attribute \"%s\"",
1318 line_number, char_number, "attribute",
1319 name ? "value" : "name");
1325 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1326 start, &val, error))
1328 start_val = g_value_get_uint (&val);
1329 g_value_unset (&val);
1334 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1337 end_val = g_value_get_uint (&val);
1338 g_value_unset (&val);
1341 attr = attribute_from_text (data->builder, name, value, error);
1342 attr->start_index = start_val;
1343 attr->end_index = end_val;
1348 data->attrs = pango_attr_list_new ();
1350 pango_attr_list_insert (data->attrs, attr);
1353 else if (strcmp (element_name, "attributes") == 0)
1356 g_warning ("Unsupported tag for GtkLabel: %s\n", element_name);
1359 static const GMarkupParser pango_parser =
1361 pango_start_element,
1365 gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
1366 GtkBuilder *builder,
1368 const gchar *tagname,
1369 GMarkupParser *parser,
1372 if (buildable_parent_iface->custom_tag_start (buildable, builder, child,
1373 tagname, parser, data))
1376 if (strcmp (tagname, "attributes") == 0)
1378 PangoParserData *parser_data;
1380 parser_data = g_slice_new0 (PangoParserData);
1381 parser_data->builder = g_object_ref (builder);
1382 parser_data->object = g_object_ref (buildable);
1383 *parser = pango_parser;
1384 *data = parser_data;
1391 gtk_label_buildable_custom_finished (GtkBuildable *buildable,
1392 GtkBuilder *builder,
1394 const gchar *tagname,
1397 PangoParserData *data;
1399 buildable_parent_iface->custom_finished (buildable, builder, child,
1400 tagname, user_data);
1402 if (strcmp (tagname, "attributes") == 0)
1404 data = (PangoParserData*)user_data;
1408 gtk_label_set_attributes (GTK_LABEL (buildable), data->attrs);
1409 pango_attr_list_unref (data->attrs);
1412 g_object_unref (data->object);
1413 g_object_unref (data->builder);
1414 g_slice_free (PangoParserData, data);
1421 * @str: The text of the label
1423 * Creates a new label with the given text inside it. You can
1424 * pass %NULL to get an empty label widget.
1426 * Return value: the new #GtkLabel
1429 gtk_label_new (const gchar *str)
1433 label = g_object_new (GTK_TYPE_LABEL, NULL);
1436 gtk_label_set_text (label, str);
1438 return GTK_WIDGET (label);
1442 * gtk_label_new_with_mnemonic:
1443 * @str: The text of the label, with an underscore in front of the
1444 * mnemonic character
1446 * Creates a new #GtkLabel, containing the text in @str.
1448 * If characters in @str are preceded by an underscore, they are
1449 * underlined. If you need a literal underscore character in a label, use
1450 * '__' (two underscores). The first underlined character represents a
1451 * keyboard accelerator called a mnemonic. The mnemonic key can be used
1452 * to activate another widget, chosen automatically, or explicitly using
1453 * gtk_label_set_mnemonic_widget().
1455 * If gtk_label_set_mnemonic_widget() is not called, then the first
1456 * activatable ancestor of the #GtkLabel will be chosen as the mnemonic
1457 * widget. For instance, if the label is inside a button or menu item,
1458 * the button or menu item will automatically become the mnemonic widget
1459 * and be activated by the mnemonic.
1461 * Return value: the new #GtkLabel
1464 gtk_label_new_with_mnemonic (const gchar *str)
1468 label = g_object_new (GTK_TYPE_LABEL, NULL);
1471 gtk_label_set_text_with_mnemonic (label, str);
1473 return GTK_WIDGET (label);
1477 gtk_label_mnemonic_activate (GtkWidget *widget,
1478 gboolean group_cycling)
1480 GtkLabel *label = GTK_LABEL (widget);
1481 GtkLabelPrivate *priv = label->priv;
1484 if (priv->mnemonic_widget)
1485 return gtk_widget_mnemonic_activate (priv->mnemonic_widget, group_cycling);
1487 /* Try to find the widget to activate by traversing the
1488 * widget's ancestry.
1490 parent = gtk_widget_get_parent (widget);
1492 if (GTK_IS_NOTEBOOK (parent))
1497 if (gtk_widget_get_can_focus (parent) ||
1498 (!group_cycling && GTK_WIDGET_GET_CLASS (parent)->activate_signal) ||
1499 GTK_IS_NOTEBOOK (gtk_widget_get_parent (parent)) ||
1500 GTK_IS_MENU_ITEM (parent))
1501 return gtk_widget_mnemonic_activate (parent, group_cycling);
1502 parent = gtk_widget_get_parent (parent);
1505 /* barf if there was nothing to activate */
1506 g_warning ("Couldn't find a target for a mnemonic activation.");
1507 gtk_widget_error_bell (widget);
1513 gtk_label_setup_mnemonic (GtkLabel *label,
1516 GtkLabelPrivate *priv = label->priv;
1517 GtkWidget *widget = GTK_WIDGET (label);
1518 GtkWidget *toplevel;
1519 GtkWidget *mnemonic_menu;
1521 mnemonic_menu = g_object_get_data (G_OBJECT (label), "gtk-mnemonic-menu");
1523 if (last_key != GDK_KEY_VoidSymbol)
1525 if (priv->mnemonic_window)
1527 gtk_window_remove_mnemonic (priv->mnemonic_window,
1530 priv->mnemonic_window = NULL;
1534 _gtk_menu_shell_remove_mnemonic (GTK_MENU_SHELL (mnemonic_menu),
1537 mnemonic_menu = NULL;
1541 if (priv->mnemonic_keyval == GDK_KEY_VoidSymbol)
1544 connect_mnemonics_visible_notify (GTK_LABEL (widget));
1546 toplevel = gtk_widget_get_toplevel (widget);
1547 if (gtk_widget_is_toplevel (toplevel))
1549 GtkWidget *menu_shell;
1551 menu_shell = gtk_widget_get_ancestor (widget,
1552 GTK_TYPE_MENU_SHELL);
1556 _gtk_menu_shell_add_mnemonic (GTK_MENU_SHELL (menu_shell),
1557 priv->mnemonic_keyval,
1559 mnemonic_menu = menu_shell;
1562 if (!GTK_IS_MENU (menu_shell))
1564 gtk_window_add_mnemonic (GTK_WINDOW (toplevel),
1565 priv->mnemonic_keyval,
1567 priv->mnemonic_window = GTK_WINDOW (toplevel);
1572 g_object_set_data (G_OBJECT (label), I_("gtk-mnemonic-menu"), mnemonic_menu);
1576 gtk_label_hierarchy_changed (GtkWidget *widget,
1577 GtkWidget *old_toplevel)
1579 GtkLabel *label = GTK_LABEL (widget);
1580 GtkLabelPrivate *priv = label->priv;
1582 gtk_label_setup_mnemonic (label, priv->mnemonic_keyval);
1586 label_shortcut_setting_apply (GtkLabel *label)
1588 gtk_label_recalculate (label);
1589 if (GTK_IS_ACCEL_LABEL (label))
1590 gtk_accel_label_refetch (GTK_ACCEL_LABEL (label));
1594 label_shortcut_setting_traverse_container (GtkWidget *widget,
1597 if (GTK_IS_LABEL (widget))
1598 label_shortcut_setting_apply (GTK_LABEL (widget));
1599 else if (GTK_IS_CONTAINER (widget))
1600 gtk_container_forall (GTK_CONTAINER (widget),
1601 label_shortcut_setting_traverse_container, data);
1605 label_shortcut_setting_changed (GtkSettings *settings)
1609 list = gtk_window_list_toplevels ();
1611 for (l = list; l ; l = l->next)
1613 GtkWidget *widget = l->data;
1615 if (gtk_widget_get_settings (widget) == settings)
1616 gtk_container_forall (GTK_CONTAINER (widget),
1617 label_shortcut_setting_traverse_container, NULL);
1624 mnemonics_visible_apply (GtkWidget *widget,
1625 gboolean mnemonics_visible)
1627 GtkLabel *label = GTK_LABEL (widget);
1628 GtkLabelPrivate *priv = label->priv;
1630 mnemonics_visible = mnemonics_visible != FALSE;
1632 if (priv->mnemonics_visible != mnemonics_visible)
1634 priv->mnemonics_visible = mnemonics_visible;
1636 gtk_label_recalculate (label);
1641 label_mnemonics_visible_traverse_container (GtkWidget *widget,
1644 gboolean mnemonics_visible = GPOINTER_TO_INT (data);
1646 _gtk_label_mnemonics_visible_apply_recursively (widget, mnemonics_visible);
1650 _gtk_label_mnemonics_visible_apply_recursively (GtkWidget *widget,
1651 gboolean mnemonics_visible)
1653 if (GTK_IS_LABEL (widget))
1654 mnemonics_visible_apply (widget, mnemonics_visible);
1655 else if (GTK_IS_CONTAINER (widget))
1656 gtk_container_forall (GTK_CONTAINER (widget),
1657 label_mnemonics_visible_traverse_container,
1658 GINT_TO_POINTER (mnemonics_visible));
1662 label_mnemonics_visible_changed (GtkWindow *window,
1666 gboolean mnemonics_visible;
1668 g_object_get (window, "mnemonics-visible", &mnemonics_visible, NULL);
1670 gtk_container_forall (GTK_CONTAINER (window),
1671 label_mnemonics_visible_traverse_container,
1672 GINT_TO_POINTER (mnemonics_visible));
1676 gtk_label_screen_changed (GtkWidget *widget,
1677 GdkScreen *old_screen)
1679 GtkSettings *settings;
1680 gboolean shortcuts_connected;
1682 if (!gtk_widget_has_screen (widget))
1685 settings = gtk_widget_get_settings (widget);
1687 shortcuts_connected =
1688 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (settings),
1689 "gtk-label-shortcuts-connected"));
1691 if (! shortcuts_connected)
1693 g_signal_connect (settings, "notify::gtk-enable-mnemonics",
1694 G_CALLBACK (label_shortcut_setting_changed),
1696 g_signal_connect (settings, "notify::gtk-enable-accels",
1697 G_CALLBACK (label_shortcut_setting_changed),
1700 g_object_set_data (G_OBJECT (settings), "gtk-label-shortcuts-connected",
1701 GINT_TO_POINTER (TRUE));
1704 label_shortcut_setting_apply (GTK_LABEL (widget));
1709 label_mnemonic_widget_weak_notify (gpointer data,
1710 GObject *where_the_object_was)
1712 GtkLabel *label = data;
1713 GtkLabelPrivate *priv = label->priv;
1715 priv->mnemonic_widget = NULL;
1716 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1720 * gtk_label_set_mnemonic_widget:
1721 * @label: a #GtkLabel
1722 * @widget: (allow-none): the target #GtkWidget
1724 * If the label has been set so that it has an mnemonic key (using
1725 * i.e. gtk_label_set_markup_with_mnemonic(),
1726 * gtk_label_set_text_with_mnemonic(), gtk_label_new_with_mnemonic()
1727 * or the "use_underline" property) the label can be associated with a
1728 * widget that is the target of the mnemonic. When the label is inside
1729 * a widget (like a #GtkButton or a #GtkNotebook tab) it is
1730 * automatically associated with the correct widget, but sometimes
1731 * (i.e. when the target is a #GtkEntry next to the label) you need to
1732 * set it explicitly using this function.
1734 * The target widget will be accelerated by emitting the
1735 * GtkWidget::mnemonic-activate signal on it. The default handler for
1736 * this signal will activate the widget if there are no mnemonic collisions
1737 * and toggle focus between the colliding widgets otherwise.
1740 gtk_label_set_mnemonic_widget (GtkLabel *label,
1743 GtkLabelPrivate *priv;
1745 g_return_if_fail (GTK_IS_LABEL (label));
1750 g_return_if_fail (GTK_IS_WIDGET (widget));
1752 if (priv->mnemonic_widget)
1754 gtk_widget_remove_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1755 g_object_weak_unref (G_OBJECT (priv->mnemonic_widget),
1756 label_mnemonic_widget_weak_notify,
1759 priv->mnemonic_widget = widget;
1760 if (priv->mnemonic_widget)
1762 g_object_weak_ref (G_OBJECT (priv->mnemonic_widget),
1763 label_mnemonic_widget_weak_notify,
1765 gtk_widget_add_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1768 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1772 * gtk_label_get_mnemonic_widget:
1773 * @label: a #GtkLabel
1775 * Retrieves the target of the mnemonic (keyboard shortcut) of this
1776 * label. See gtk_label_set_mnemonic_widget().
1778 * Return value: (transfer none): the target of the label's mnemonic,
1779 * or %NULL if none has been set and the default algorithm will be used.
1782 gtk_label_get_mnemonic_widget (GtkLabel *label)
1784 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1786 return label->priv->mnemonic_widget;
1790 * gtk_label_get_mnemonic_keyval:
1791 * @label: a #GtkLabel
1793 * If the label has been set so that it has an mnemonic key this function
1794 * returns the keyval used for the mnemonic accelerator. If there is no
1795 * mnemonic set up it returns #GDK_VoidSymbol.
1797 * Returns: GDK keyval usable for accelerators, or #GDK_VoidSymbol
1800 gtk_label_get_mnemonic_keyval (GtkLabel *label)
1802 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_KEY_VoidSymbol);
1804 return label->priv->mnemonic_keyval;
1808 gtk_label_set_text_internal (GtkLabel *label,
1811 GtkLabelPrivate *priv = label->priv;
1813 g_free (priv->text);
1817 gtk_label_select_region_index (label, 0, 0);
1821 gtk_label_set_label_internal (GtkLabel *label,
1824 GtkLabelPrivate *priv = label->priv;
1826 g_free (priv->label);
1830 g_object_notify (G_OBJECT (label), "label");
1834 gtk_label_set_use_markup_internal (GtkLabel *label,
1837 GtkLabelPrivate *priv = label->priv;
1840 if (priv->use_markup != val)
1842 priv->use_markup = val;
1844 g_object_notify (G_OBJECT (label), "use-markup");
1849 gtk_label_set_use_underline_internal (GtkLabel *label,
1852 GtkLabelPrivate *priv = label->priv;
1855 if (priv->use_underline != val)
1857 priv->use_underline = val;
1859 g_object_notify (G_OBJECT (label), "use-underline");
1864 gtk_label_compose_effective_attrs (GtkLabel *label)
1866 GtkLabelPrivate *priv = label->priv;
1867 PangoAttrIterator *iter;
1868 PangoAttribute *attr;
1869 GSList *iter_attrs, *l;
1873 if (priv->effective_attrs)
1875 if ((iter = pango_attr_list_get_iterator (priv->attrs)))
1879 iter_attrs = pango_attr_iterator_get_attrs (iter);
1880 for (l = iter_attrs; l; l = l->next)
1883 pango_attr_list_insert (priv->effective_attrs, attr);
1885 g_slist_free (iter_attrs);
1887 while (pango_attr_iterator_next (iter));
1888 pango_attr_iterator_destroy (iter);
1892 priv->effective_attrs =
1893 pango_attr_list_ref (priv->attrs);
1898 gtk_label_set_attributes_internal (GtkLabel *label,
1899 PangoAttrList *attrs)
1901 GtkLabelPrivate *priv = label->priv;
1904 pango_attr_list_ref (attrs);
1907 pango_attr_list_unref (priv->attrs);
1908 priv->attrs = attrs;
1910 g_object_notify (G_OBJECT (label), "attributes");
1914 /* Calculates text, attrs and mnemonic_keyval from
1915 * label, use_underline and use_markup
1918 gtk_label_recalculate (GtkLabel *label)
1920 GtkLabelPrivate *priv = label->priv;
1921 guint keyval = priv->mnemonic_keyval;
1923 if (priv->use_markup)
1924 gtk_label_set_markup_internal (label, priv->label, priv->use_underline);
1927 if (priv->use_underline)
1928 gtk_label_set_uline_text_internal (label, priv->label);
1931 if (priv->effective_attrs)
1932 pango_attr_list_unref (priv->effective_attrs);
1933 priv->effective_attrs = NULL;
1934 gtk_label_set_text_internal (label, g_strdup (priv->label));
1938 gtk_label_compose_effective_attrs (label);
1940 if (!priv->use_underline)
1941 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
1943 if (keyval != priv->mnemonic_keyval)
1945 gtk_label_setup_mnemonic (label, keyval);
1946 g_object_notify (G_OBJECT (label), "mnemonic-keyval");
1949 gtk_label_clear_layout (label);
1950 gtk_label_clear_select_info (label);
1951 gtk_widget_queue_resize (GTK_WIDGET (label));
1955 * gtk_label_set_text:
1956 * @label: a #GtkLabel
1957 * @str: The text you want to set
1959 * Sets the text within the #GtkLabel widget. It overwrites any text that
1962 * This will also clear any previously set mnemonic accelerators.
1965 gtk_label_set_text (GtkLabel *label,
1968 g_return_if_fail (GTK_IS_LABEL (label));
1970 g_object_freeze_notify (G_OBJECT (label));
1972 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
1973 gtk_label_set_use_markup_internal (label, FALSE);
1974 gtk_label_set_use_underline_internal (label, FALSE);
1976 gtk_label_recalculate (label);
1978 g_object_thaw_notify (G_OBJECT (label));
1982 * gtk_label_set_attributes:
1983 * @label: a #GtkLabel
1984 * @attrs: a #PangoAttrList
1986 * Sets a #PangoAttrList; the attributes in the list are applied to the
1989 * <note><para>The attributes set with this function will be applied
1990 * and merged with any other attributes previously effected by way
1991 * of the #GtkLabel:use-underline or #GtkLabel:use-markup properties.
1992 * While it is not recommended to mix markup strings with manually set
1993 * attributes, if you must; know that the attributes will be applied
1994 * to the label after the markup string is parsed.</para></note>
1997 gtk_label_set_attributes (GtkLabel *label,
1998 PangoAttrList *attrs)
2000 g_return_if_fail (GTK_IS_LABEL (label));
2002 gtk_label_set_attributes_internal (label, attrs);
2004 gtk_label_recalculate (label);
2006 gtk_label_clear_layout (label);
2007 gtk_widget_queue_resize (GTK_WIDGET (label));
2011 * gtk_label_get_attributes:
2012 * @label: a #GtkLabel
2014 * Gets the attribute list that was set on the label using
2015 * gtk_label_set_attributes(), if any. This function does
2016 * not reflect attributes that come from the labels markup
2017 * (see gtk_label_set_markup()). If you want to get the
2018 * effective attributes for the label, use
2019 * pango_layout_get_attribute (gtk_label_get_layout (label)).
2021 * Return value: the attribute list, or %NULL if none was set.
2024 gtk_label_get_attributes (GtkLabel *label)
2026 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2028 return label->priv->attrs;
2032 * gtk_label_set_label:
2033 * @label: a #GtkLabel
2034 * @str: the new text to set for the label
2036 * Sets the text of the label. The label is interpreted as
2037 * including embedded underlines and/or Pango markup depending
2038 * on the values of the #GtkLabel:use-underline" and
2039 * #GtkLabel:use-markup properties.
2042 gtk_label_set_label (GtkLabel *label,
2045 g_return_if_fail (GTK_IS_LABEL (label));
2047 g_object_freeze_notify (G_OBJECT (label));
2049 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2050 gtk_label_recalculate (label);
2052 g_object_thaw_notify (G_OBJECT (label));
2056 * gtk_label_get_label:
2057 * @label: a #GtkLabel
2059 * Fetches the text from a label widget including any embedded
2060 * underlines indicating mnemonics and Pango markup. (See
2061 * gtk_label_get_text()).
2063 * Return value: the text of the label widget. This string is
2064 * owned by the widget and must not be modified or freed.
2066 G_CONST_RETURN gchar *
2067 gtk_label_get_label (GtkLabel *label)
2069 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2071 return label->priv->label;
2079 GdkColor *link_color;
2080 GdkColor *visited_link_color;
2084 start_element_handler (GMarkupParseContext *context,
2085 const gchar *element_name,
2086 const gchar **attribute_names,
2087 const gchar **attribute_values,
2091 GtkLabelPrivate *priv;
2092 UriParserData *pdata = user_data;
2094 if (strcmp (element_name, "a") == 0)
2097 const gchar *uri = NULL;
2098 const gchar *title = NULL;
2099 gboolean visited = FALSE;
2103 GdkColor *color = NULL;
2105 g_markup_parse_context_get_position (context, &line_number, &char_number);
2107 for (i = 0; attribute_names[i] != NULL; i++)
2109 const gchar *attr = attribute_names[i];
2111 if (strcmp (attr, "href") == 0)
2112 uri = attribute_values[i];
2113 else if (strcmp (attr, "title") == 0)
2114 title = attribute_values[i];
2119 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
2120 "Attribute '%s' is not allowed on the <a> tag "
2121 "on line %d char %d",
2122 attr, line_number, char_number);
2131 G_MARKUP_ERROR_INVALID_CONTENT,
2132 "Attribute 'href' was missing on the <a> tag "
2133 "on line %d char %d",
2134 line_number, char_number);
2139 priv = pdata->label->priv;
2140 if (priv->track_links && priv->select_info)
2143 for (l = priv->select_info->links; l; l = l->next)
2146 if (strcmp (uri, link->uri) == 0)
2148 visited = link->visited;
2155 color = pdata->visited_link_color;
2157 color = pdata->link_color;
2159 g_string_append_printf (pdata->new_str,
2160 "<span color=\"#%04x%04x%04x\" underline=\"single\">",
2165 link = g_new0 (GtkLabelLink, 1);
2166 link->uri = g_strdup (uri);
2167 link->title = g_strdup (title);
2168 link->visited = visited;
2169 pdata->links = g_list_append (pdata->links, link);
2175 g_string_append_c (pdata->new_str, '<');
2176 g_string_append (pdata->new_str, element_name);
2178 for (i = 0; attribute_names[i] != NULL; i++)
2180 const gchar *attr = attribute_names[i];
2181 const gchar *value = attribute_values[i];
2184 newvalue = g_markup_escape_text (value, -1);
2186 g_string_append_c (pdata->new_str, ' ');
2187 g_string_append (pdata->new_str, attr);
2188 g_string_append (pdata->new_str, "=\"");
2189 g_string_append (pdata->new_str, newvalue);
2190 g_string_append_c (pdata->new_str, '\"');
2194 g_string_append_c (pdata->new_str, '>');
2199 end_element_handler (GMarkupParseContext *context,
2200 const gchar *element_name,
2204 UriParserData *pdata = user_data;
2206 if (!strcmp (element_name, "a"))
2207 g_string_append (pdata->new_str, "</span>");
2210 g_string_append (pdata->new_str, "</");
2211 g_string_append (pdata->new_str, element_name);
2212 g_string_append_c (pdata->new_str, '>');
2217 text_handler (GMarkupParseContext *context,
2223 UriParserData *pdata = user_data;
2226 newtext = g_markup_escape_text (text, text_len);
2227 g_string_append (pdata->new_str, newtext);
2231 static const GMarkupParser markup_parser =
2233 start_element_handler,
2234 end_element_handler,
2241 xml_isspace (gchar c)
2243 return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
2247 link_free (GtkLabelLink *link)
2250 g_free (link->title);
2255 gtk_label_get_link_colors (GtkWidget *widget,
2256 GdkColor **link_color,
2257 GdkColor **visited_link_color)
2259 gtk_widget_ensure_style (widget);
2260 gtk_widget_style_get (widget,
2261 "link-color", link_color,
2262 "visited-link-color", visited_link_color,
2265 *link_color = gdk_color_copy (&default_link_color);
2266 if (!*visited_link_color)
2267 *visited_link_color = gdk_color_copy (&default_visited_link_color);
2271 parse_uri_markup (GtkLabel *label,
2277 GMarkupParseContext *context = NULL;
2278 const gchar *p, *end;
2279 gboolean needs_root = TRUE;
2281 UriParserData pdata;
2283 length = strlen (str);
2287 pdata.label = label;
2289 pdata.new_str = g_string_sized_new (length);
2291 gtk_label_get_link_colors (GTK_WIDGET (label), &pdata.link_color, &pdata.visited_link_color);
2293 while (p != end && xml_isspace (*p))
2296 if (end - p >= 8 && strncmp (p, "<markup>", 8) == 0)
2299 context = g_markup_parse_context_new (&markup_parser, 0, &pdata, NULL);
2303 if (!g_markup_parse_context_parse (context, "<markup>", -1, error))
2307 if (!g_markup_parse_context_parse (context, str, length, error))
2312 if (!g_markup_parse_context_parse (context, "</markup>", -1, error))
2316 if (!g_markup_parse_context_end_parse (context, error))
2319 g_markup_parse_context_free (context);
2321 *new_str = g_string_free (pdata.new_str, FALSE);
2322 *links = pdata.links;
2324 gdk_color_free (pdata.link_color);
2325 gdk_color_free (pdata.visited_link_color);
2330 g_markup_parse_context_free (context);
2331 g_string_free (pdata.new_str, TRUE);
2332 g_list_foreach (pdata.links, (GFunc)link_free, NULL);
2333 g_list_free (pdata.links);
2334 gdk_color_free (pdata.link_color);
2335 gdk_color_free (pdata.visited_link_color);
2341 gtk_label_ensure_has_tooltip (GtkLabel *label)
2343 GtkLabelPrivate *priv = label->priv;
2345 gboolean has_tooltip = FALSE;
2347 for (l = priv->select_info->links; l; l = l->next)
2349 GtkLabelLink *link = l->data;
2357 gtk_widget_set_has_tooltip (GTK_WIDGET (label), has_tooltip);
2361 gtk_label_set_markup_internal (GtkLabel *label,
2363 gboolean with_uline)
2365 GtkLabelPrivate *priv = label->priv;
2367 GError *error = NULL;
2368 PangoAttrList *attrs = NULL;
2369 gunichar accel_char = 0;
2371 GList *links = NULL;
2373 if (!parse_uri_markup (label, str, &new_str, &links, &error))
2375 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2377 g_error_free (error);
2381 gtk_label_clear_links (label);
2384 gtk_label_ensure_select_info (label);
2385 priv->select_info->links = links;
2386 gtk_label_ensure_has_tooltip (label);
2391 gboolean enable_mnemonics;
2392 gboolean auto_mnemonics;
2394 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2395 "gtk-enable-mnemonics", &enable_mnemonics,
2396 "gtk-auto-mnemonics", &auto_mnemonics,
2399 if (!(enable_mnemonics && priv->mnemonics_visible &&
2401 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2402 (!priv->mnemonic_widget ||
2403 gtk_widget_is_sensitive (priv->mnemonic_widget))))))
2409 if (separate_uline_pattern (new_str, &key, &tmp, &pattern))
2418 if (!pango_parse_markup (new_str,
2420 with_uline ? '_' : 0,
2423 with_uline ? &accel_char : NULL,
2426 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2429 g_error_free (error);
2436 gtk_label_set_text_internal (label, text);
2440 if (priv->effective_attrs)
2441 pango_attr_list_unref (priv->effective_attrs);
2442 priv->effective_attrs = attrs;
2445 if (accel_char != 0)
2446 priv->mnemonic_keyval = gdk_keyval_to_lower (gdk_unicode_to_keyval (accel_char));
2448 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
2452 * gtk_label_set_markup:
2453 * @label: a #GtkLabel
2454 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
2456 * Parses @str which is marked up with the <link
2457 * linkend="PangoMarkupFormat">Pango text markup language</link>, setting the
2458 * label's text and attribute list based on the parse results. If the @str is
2459 * external data, you may need to escape it with g_markup_escape_text() or
2460 * g_markup_printf_escaped()<!-- -->:
2464 * markup = g_markup_printf_escaped ("<span style=\"italic\">%s</span>", str);
2465 * gtk_label_set_markup (GTK_LABEL (label), markup);
2470 gtk_label_set_markup (GtkLabel *label,
2473 GtkLabelPrivate *priv;
2475 g_return_if_fail (GTK_IS_LABEL (label));
2479 g_object_freeze_notify (G_OBJECT (label));
2481 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2482 gtk_label_set_use_markup_internal (label, TRUE);
2483 gtk_label_set_use_underline_internal (label, FALSE);
2485 gtk_label_recalculate (label);
2487 g_object_thaw_notify (G_OBJECT (label));
2491 * gtk_label_set_markup_with_mnemonic:
2492 * @label: a #GtkLabel
2493 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
2495 * Parses @str which is marked up with the <link linkend="PangoMarkupFormat">Pango text markup language</link>,
2496 * setting the label's text and attribute list based on the parse results.
2497 * If characters in @str are preceded by an underscore, they are underlined
2498 * indicating that they represent a keyboard accelerator called a mnemonic.
2500 * The mnemonic key can be used to activate another widget, chosen
2501 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
2504 gtk_label_set_markup_with_mnemonic (GtkLabel *label,
2507 GtkLabelPrivate *priv;
2509 g_return_if_fail (GTK_IS_LABEL (label));
2513 g_object_freeze_notify (G_OBJECT (label));
2515 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2516 gtk_label_set_use_markup_internal (label, TRUE);
2517 gtk_label_set_use_underline_internal (label, TRUE);
2519 gtk_label_recalculate (label);
2521 g_object_thaw_notify (G_OBJECT (label));
2525 * gtk_label_get_text:
2526 * @label: a #GtkLabel
2528 * Fetches the text from a label widget, as displayed on the
2529 * screen. This does not include any embedded underlines
2530 * indicating mnemonics or Pango markup. (See gtk_label_get_label())
2532 * Return value: the text in the label widget. This is the internal
2533 * string used by the label, and must not be modified.
2535 G_CONST_RETURN gchar *
2536 gtk_label_get_text (GtkLabel *label)
2538 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2540 return label->priv->text;
2543 static PangoAttrList *
2544 gtk_label_pattern_to_attrs (GtkLabel *label,
2545 const gchar *pattern)
2547 GtkLabelPrivate *priv = label->priv;
2549 const char *p = priv->text;
2550 const char *q = pattern;
2551 PangoAttrList *attrs;
2553 attrs = pango_attr_list_new ();
2557 while (*p && *q && *q != '_')
2559 p = g_utf8_next_char (p);
2563 while (*p && *q && *q == '_')
2565 p = g_utf8_next_char (p);
2571 PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_LOW);
2572 attr->start_index = start - priv->text;
2573 attr->end_index = p - priv->text;
2575 pango_attr_list_insert (attrs, attr);
2585 gtk_label_set_pattern_internal (GtkLabel *label,
2586 const gchar *pattern,
2587 gboolean is_mnemonic)
2589 GtkLabelPrivate *priv = label->priv;
2590 PangoAttrList *attrs;
2591 gboolean enable_mnemonics;
2592 gboolean auto_mnemonics;
2594 if (priv->pattern_set)
2599 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2600 "gtk-enable-mnemonics", &enable_mnemonics,
2601 "gtk-auto-mnemonics", &auto_mnemonics,
2604 if (enable_mnemonics && priv->mnemonics_visible && pattern &&
2606 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2607 (!priv->mnemonic_widget ||
2608 gtk_widget_is_sensitive (priv->mnemonic_widget)))))
2609 attrs = gtk_label_pattern_to_attrs (label, pattern);
2614 attrs = gtk_label_pattern_to_attrs (label, pattern);
2616 if (priv->effective_attrs)
2617 pango_attr_list_unref (priv->effective_attrs);
2618 priv->effective_attrs = attrs;
2622 gtk_label_set_pattern (GtkLabel *label,
2623 const gchar *pattern)
2625 GtkLabelPrivate *priv = label->priv;
2627 g_return_if_fail (GTK_IS_LABEL (label));
2631 priv->pattern_set = FALSE;
2635 gtk_label_set_pattern_internal (label, pattern, FALSE);
2636 priv->pattern_set = TRUE;
2639 gtk_label_recalculate (label);
2641 gtk_label_clear_layout (label);
2642 gtk_widget_queue_resize (GTK_WIDGET (label));
2647 * gtk_label_set_justify:
2648 * @label: a #GtkLabel
2649 * @jtype: a #GtkJustification
2651 * Sets the alignment of the lines in the text of the label relative to
2652 * each other. %GTK_JUSTIFY_LEFT is the default value when the
2653 * widget is first created with gtk_label_new(). If you instead want
2654 * to set the alignment of the label as a whole, use
2655 * gtk_misc_set_alignment() instead. gtk_label_set_justify() has no
2656 * effect on labels containing only a single line.
2659 gtk_label_set_justify (GtkLabel *label,
2660 GtkJustification jtype)
2662 GtkLabelPrivate *priv;
2664 g_return_if_fail (GTK_IS_LABEL (label));
2665 g_return_if_fail (jtype >= GTK_JUSTIFY_LEFT && jtype <= GTK_JUSTIFY_FILL);
2669 if ((GtkJustification) priv->jtype != jtype)
2671 priv->jtype = jtype;
2673 /* No real need to be this drastic, but easier than duplicating the code */
2674 gtk_label_clear_layout (label);
2676 g_object_notify (G_OBJECT (label), "justify");
2677 gtk_widget_queue_resize (GTK_WIDGET (label));
2682 * gtk_label_get_justify:
2683 * @label: a #GtkLabel
2685 * Returns the justification of the label. See gtk_label_set_justify().
2687 * Return value: #GtkJustification
2690 gtk_label_get_justify (GtkLabel *label)
2692 g_return_val_if_fail (GTK_IS_LABEL (label), 0);
2694 return label->priv->jtype;
2698 * gtk_label_set_ellipsize:
2699 * @label: a #GtkLabel
2700 * @mode: a #PangoEllipsizeMode
2702 * Sets the mode used to ellipsize (add an ellipsis: "...") to the text
2703 * if there is not enough space to render the entire string.
2708 gtk_label_set_ellipsize (GtkLabel *label,
2709 PangoEllipsizeMode mode)
2711 GtkLabelPrivate *priv;
2713 g_return_if_fail (GTK_IS_LABEL (label));
2714 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE && mode <= PANGO_ELLIPSIZE_END);
2718 if ((PangoEllipsizeMode) priv->ellipsize != mode)
2720 priv->ellipsize = mode;
2722 /* No real need to be this drastic, but easier than duplicating the code */
2723 gtk_label_clear_layout (label);
2725 g_object_notify (G_OBJECT (label), "ellipsize");
2726 gtk_widget_queue_resize (GTK_WIDGET (label));
2731 * gtk_label_get_ellipsize:
2732 * @label: a #GtkLabel
2734 * Returns the ellipsizing position of the label. See gtk_label_set_ellipsize().
2736 * Return value: #PangoEllipsizeMode
2741 gtk_label_get_ellipsize (GtkLabel *label)
2743 g_return_val_if_fail (GTK_IS_LABEL (label), PANGO_ELLIPSIZE_NONE);
2745 return label->priv->ellipsize;
2749 * gtk_label_set_width_chars:
2750 * @label: a #GtkLabel
2751 * @n_chars: the new desired width, in characters.
2753 * Sets the desired width in characters of @label to @n_chars.
2758 gtk_label_set_width_chars (GtkLabel *label,
2761 GtkLabelPrivate *priv;
2763 g_return_if_fail (GTK_IS_LABEL (label));
2767 if (priv->width_chars != n_chars)
2769 priv->width_chars = n_chars;
2770 g_object_notify (G_OBJECT (label), "width-chars");
2771 gtk_label_invalidate_wrap_width (label);
2772 gtk_widget_queue_resize (GTK_WIDGET (label));
2777 * gtk_label_get_width_chars:
2778 * @label: a #GtkLabel
2780 * Retrieves the desired width of @label, in characters. See
2781 * gtk_label_set_width_chars().
2783 * Return value: the width of the label in characters.
2788 gtk_label_get_width_chars (GtkLabel *label)
2790 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2792 return label->priv->width_chars;
2796 * gtk_label_set_max_width_chars:
2797 * @label: a #GtkLabel
2798 * @n_chars: the new desired maximum width, in characters.
2800 * Sets the desired maximum width in characters of @label to @n_chars.
2805 gtk_label_set_max_width_chars (GtkLabel *label,
2808 GtkLabelPrivate *priv;
2810 g_return_if_fail (GTK_IS_LABEL (label));
2814 if (priv->max_width_chars != n_chars)
2816 priv->max_width_chars = n_chars;
2818 g_object_notify (G_OBJECT (label), "max-width-chars");
2819 gtk_label_invalidate_wrap_width (label);
2820 gtk_widget_queue_resize (GTK_WIDGET (label));
2825 * gtk_label_get_max_width_chars:
2826 * @label: a #GtkLabel
2828 * Retrieves the desired maximum width of @label, in characters. See
2829 * gtk_label_set_width_chars().
2831 * Return value: the maximum width of the label in characters.
2836 gtk_label_get_max_width_chars (GtkLabel *label)
2838 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2840 return label->priv->max_width_chars;
2844 * gtk_label_set_line_wrap:
2845 * @label: a #GtkLabel
2846 * @wrap: the setting
2848 * Toggles line wrapping within the #GtkLabel widget. %TRUE makes it break
2849 * lines if text exceeds the widget's size. %FALSE lets the text get cut off
2850 * by the edge of the widget if it exceeds the widget size.
2852 * Note that setting line wrapping to %TRUE does not make the label
2853 * wrap at its parent container's width, because GTK+ widgets
2854 * conceptually can't make their requisition depend on the parent
2855 * container's size. For a label that wraps at a specific position,
2856 * set the label's width using gtk_widget_set_size_request().
2859 gtk_label_set_line_wrap (GtkLabel *label,
2862 GtkLabelPrivate *priv;
2864 g_return_if_fail (GTK_IS_LABEL (label));
2868 wrap = wrap != FALSE;
2870 if (priv->wrap != wrap)
2874 gtk_label_clear_layout (label);
2875 gtk_widget_queue_resize (GTK_WIDGET (label));
2876 g_object_notify (G_OBJECT (label), "wrap");
2881 * gtk_label_get_line_wrap:
2882 * @label: a #GtkLabel
2884 * Returns whether lines in the label are automatically wrapped.
2885 * See gtk_label_set_line_wrap().
2887 * Return value: %TRUE if the lines of the label are automatically wrapped.
2890 gtk_label_get_line_wrap (GtkLabel *label)
2892 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
2894 return label->priv->wrap;
2898 * gtk_label_set_line_wrap_mode:
2899 * @label: a #GtkLabel
2900 * @wrap_mode: the line wrapping mode
2902 * If line wrapping is on (see gtk_label_set_line_wrap()) this controls how
2903 * the line wrapping is done. The default is %PANGO_WRAP_WORD which means
2904 * wrap on word boundaries.
2909 gtk_label_set_line_wrap_mode (GtkLabel *label,
2910 PangoWrapMode wrap_mode)
2912 GtkLabelPrivate *priv;
2914 g_return_if_fail (GTK_IS_LABEL (label));
2918 if (priv->wrap_mode != wrap_mode)
2920 priv->wrap_mode = wrap_mode;
2921 g_object_notify (G_OBJECT (label), "wrap-mode");
2923 gtk_widget_queue_resize (GTK_WIDGET (label));
2928 * gtk_label_get_line_wrap_mode:
2929 * @label: a #GtkLabel
2931 * Returns line wrap mode used by the label. See gtk_label_set_line_wrap_mode().
2933 * Return value: %TRUE if the lines of the label are automatically wrapped.
2938 gtk_label_get_line_wrap_mode (GtkLabel *label)
2940 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
2942 return label->priv->wrap_mode;
2946 gtk_label_destroy (GtkWidget *widget)
2948 GtkLabel *label = GTK_LABEL (widget);
2950 gtk_label_set_mnemonic_widget (label, NULL);
2952 GTK_WIDGET_CLASS (gtk_label_parent_class)->destroy (widget);
2956 gtk_label_finalize (GObject *object)
2958 GtkLabel *label = GTK_LABEL (object);
2959 GtkLabelPrivate *priv = label->priv;
2961 g_free (priv->label);
2962 g_free (priv->text);
2965 g_object_unref (priv->layout);
2968 pango_attr_list_unref (priv->attrs);
2970 if (priv->effective_attrs)
2971 pango_attr_list_unref (priv->effective_attrs);
2973 gtk_label_clear_links (label);
2974 g_free (priv->select_info);
2976 G_OBJECT_CLASS (gtk_label_parent_class)->finalize (object);
2980 gtk_label_clear_layout (GtkLabel *label)
2982 GtkLabelPrivate *priv = label->priv;
2986 g_object_unref (priv->layout);
2987 priv->layout = NULL;
2989 //gtk_label_clear_links (label);
2995 get_label_width (GtkLabel *label,
2999 GtkWidgetAuxInfo *aux_info;
3000 GtkLabelPrivate *priv;
3001 PangoLayout *layout;
3002 PangoContext *context;
3003 PangoFontMetrics *metrics;
3004 PangoRectangle rect;
3005 gint char_width, digit_width, char_pixels, text_width, ellipsize_chars, guess_width;
3008 aux_info = _gtk_widget_get_aux_info (GTK_WIDGET (label), FALSE);
3010 layout = pango_layout_copy (priv->layout);
3011 context = pango_layout_get_context (layout);
3012 metrics = pango_context_get_metrics (context,
3013 gtk_widget_get_style (GTK_WIDGET (label))->font_desc,
3014 pango_context_get_language (context));
3016 char_width = pango_font_metrics_get_approximate_char_width (metrics);
3017 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
3018 char_pixels = MAX (char_width, digit_width);
3019 pango_font_metrics_unref (metrics);
3021 /* Fetch the length of the complete unwrapped text */
3022 pango_layout_set_width (layout, -1);
3023 pango_layout_get_extents (layout, NULL, &rect);
3024 text_width = rect.width;
3026 /* Fetch the width that was guessed by gtk_label_ensure_layout() */
3027 pango_layout_get_extents (priv->layout, NULL, &rect);
3028 guess_width = rect.width;
3030 /* enforce minimum width for ellipsized labels at ~3 chars */
3031 if (priv->ellipsize)
3032 ellipsize_chars = 3;
3034 ellipsize_chars = 0;
3036 /* "width-chars" Hard-coded minimum width:
3037 * - minimum size should be MAX (width-chars, strlen ("..."));
3038 * - natural size should be MAX (width-chars, strlen (priv->text));
3040 * "max-width-chars" User specified maximum size requisition
3041 * - minimum size should be MAX (width-chars, 0)
3042 * - natural size should be MIN (max-width-chars, strlen (priv->text))
3044 * For ellipsizing labels; if max-width-chars is specified: either it is used as
3045 * a minimum size or the label text as a minimum size (natural size still overflows).
3047 * For wrapping labels; A reasonable minimum size is useful to naturally layout
3048 * interfaces automatically. In this case if no "width-chars" is specified, the minimum
3049 * width will default to the wrap guess that gtk_label_ensure_layout() does.
3051 * In *any* case the minimum width is completely overridden if an explicit width
3052 * request was provided.
3055 if (priv->ellipsize || priv->wrap)
3057 *minimum = char_pixels * MAX (priv->width_chars, ellipsize_chars);
3059 /* Default to the minimum width regularly guessed by GTK+ if no minimum
3060 * width was specified, only allow unwrapping of these labels.
3062 * Note that when specifying a small width_chars for a long text;
3063 * an accordingly large size will be required for the label height.
3065 if (priv->wrap && priv->width_chars <= 0)
3066 *minimum = guess_width;
3068 if (priv->max_width_chars < 0)
3070 *natural = MAX (*minimum, text_width);
3074 gint max_char_width = char_pixels * priv->max_width_chars;
3075 gint max_width = MIN (text_width, max_char_width);
3077 /* With max-char-width specified, we let the minimum widths of
3078 * ellipsized text crawl up to the max-char-width
3079 * (note that we dont want to limit the minimum width for wrapping text).
3081 if (priv->ellipsize)
3082 *minimum = MIN (text_width, max_width);
3084 *natural = MAX (*minimum, max_width);
3089 *minimum = text_width;
3090 *natural = *minimum;
3093 /* if a width-request is set, use that as the requested label width */
3094 if ((priv->wrap || priv->ellipsize || priv->width_chars > 0 || priv->max_width_chars > 0) &&
3095 aux_info && aux_info->width > 0)
3097 *minimum = aux_info->width * PANGO_SCALE;
3098 *natural = MAX (*natural, *minimum);
3101 g_object_unref (layout);
3105 gtk_label_invalidate_wrap_width (GtkLabel *label)
3107 GtkLabelPrivate *priv = label->priv;
3109 priv->wrap_width = -1;
3113 get_label_wrap_width (GtkLabel *label)
3115 GtkLabelPrivate *priv = label->priv;
3117 if (priv->wrap_width < 0)
3119 if (priv->width_chars > 0)
3121 PangoLayout *layout;
3122 PangoContext *context;
3123 PangoFontMetrics *metrics;
3124 PangoRectangle rect;
3125 gint char_width, digit_width, char_pixels, text_width;
3127 layout = pango_layout_copy (priv->layout);
3128 context = pango_layout_get_context (layout);
3129 metrics = pango_context_get_metrics (context,
3130 gtk_widget_get_style (GTK_WIDGET (label))->font_desc,
3131 pango_context_get_language (context));
3133 char_width = pango_font_metrics_get_approximate_char_width (metrics);
3134 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
3135 char_pixels = MAX (char_width, digit_width);
3136 pango_font_metrics_unref (metrics);
3138 pango_layout_set_width (layout, -1);
3139 pango_layout_get_extents (layout, NULL, &rect);
3140 g_object_unref (layout);
3142 text_width = rect.width;
3144 priv->wrap_width = PANGO_PIXELS (MAX (text_width, char_pixels * priv->width_chars));
3148 PangoLayout *layout;
3150 layout = gtk_widget_create_pango_layout (GTK_WIDGET (label),
3151 "This string is just about long enough.");
3152 pango_layout_get_size (layout, &priv->wrap_width, NULL);
3153 g_object_unref (layout);
3157 return priv->wrap_width;
3161 gtk_label_ensure_layout (GtkLabel *label, gboolean guess_wrap_width)
3163 GtkLabelPrivate *priv = label->priv;
3165 PangoRectangle logical_rect;
3168 widget = GTK_WIDGET (label);
3170 rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
3174 GtkAllocation allocation;
3175 PangoAlignment align = PANGO_ALIGN_LEFT; /* Quiet gcc */
3176 gdouble angle = gtk_label_get_angle (label);
3178 if (angle != 0.0 && !priv->select_info)
3180 PangoMatrix matrix = PANGO_MATRIX_INIT;
3182 /* We rotate the standard singleton PangoContext for the widget,
3183 * depending on the fact that it's meant pretty much exclusively
3186 pango_matrix_rotate (&matrix, angle);
3188 pango_context_set_matrix (gtk_widget_get_pango_context (widget), &matrix);
3190 priv->have_transform = TRUE;
3194 if (priv->have_transform)
3195 pango_context_set_matrix (gtk_widget_get_pango_context (widget), NULL);
3197 priv->have_transform = FALSE;
3200 priv->layout = gtk_widget_create_pango_layout (widget, priv->text);
3202 if (priv->effective_attrs)
3203 pango_layout_set_attributes (priv->layout, priv->effective_attrs);
3205 gtk_label_rescan_links (label);
3207 switch (priv->jtype)
3209 case GTK_JUSTIFY_LEFT:
3210 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3212 case GTK_JUSTIFY_RIGHT:
3213 align = rtl ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
3215 case GTK_JUSTIFY_CENTER:
3216 align = PANGO_ALIGN_CENTER;
3218 case GTK_JUSTIFY_FILL:
3219 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3220 pango_layout_set_justify (priv->layout, TRUE);
3223 g_assert_not_reached();
3226 pango_layout_set_alignment (priv->layout, align);
3227 pango_layout_set_ellipsize (priv->layout, priv->ellipsize);
3228 pango_layout_set_single_paragraph_mode (priv->layout, priv->single_line_mode);
3230 gtk_widget_get_allocation (widget, &allocation);
3232 if (priv->ellipsize)
3233 pango_layout_set_width (priv->layout, allocation.width * PANGO_SCALE);
3234 else if (priv->wrap)
3236 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (widget, FALSE);
3237 gint longest_paragraph;
3241 if ((angle == 90 || angle == 270) && aux_info && aux_info->height > 0)
3242 aux_width = aux_info->height;
3243 else if (aux_info && aux_info->width > 0)
3244 aux_width = aux_info->width;
3247 pango_layout_set_width (priv->layout, aux_width * PANGO_SCALE);
3248 else if (guess_wrap_width == FALSE && allocation.width > 1 && allocation.height > 1)
3250 PangoRectangle rect;
3251 gint xpad, ypad, natural_width;
3252 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3254 if (angle == 90 || angle == 270)
3255 width = allocation.height - ypad * 2;
3257 width = allocation.width - xpad * 2;
3259 /* dont set a wrap width wider than the label's natural width
3260 * incase we're allocated more space than needed */
3261 pango_layout_get_extents (priv->layout, NULL, &rect);
3262 natural_width = PANGO_PIXELS (rect.width);
3263 width = MIN (natural_width, width);
3265 pango_layout_set_wrap (priv->layout, priv->wrap_mode);
3266 pango_layout_set_width (priv->layout, MAX (width, 1) * PANGO_SCALE);
3270 GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (label));
3273 pango_layout_set_width (priv->layout, -1);
3274 pango_layout_get_extents (priv->layout, NULL, &logical_rect);
3276 width = logical_rect.width;
3277 /* Try to guess a reasonable maximum width */
3278 longest_paragraph = width;
3280 wrap_width = get_label_wrap_width (label);
3281 width = MIN (width, wrap_width);
3283 PANGO_SCALE * (gdk_screen_get_width (screen) + 1) / 2);
3285 pango_layout_set_width (priv->layout, width);
3286 pango_layout_get_extents (priv->layout, NULL, &logical_rect);
3287 width = logical_rect.width;
3288 height = logical_rect.height;
3290 /* Unfortunately, the above may leave us with a very unbalanced looking paragraph,
3291 * so we try short search for a narrower width that leaves us with the same height
3293 if (longest_paragraph > 0)
3295 gint nlines, perfect_width;
3297 nlines = pango_layout_get_line_count (priv->layout);
3298 perfect_width = (longest_paragraph + nlines - 1) / nlines;
3300 if (perfect_width < width)
3302 pango_layout_set_width (priv->layout, perfect_width);
3303 pango_layout_get_extents (priv->layout, NULL, &logical_rect);
3305 if (logical_rect.height <= height)
3306 width = logical_rect.width;
3309 gint mid_width = (perfect_width + width) / 2;
3311 if (mid_width > perfect_width)
3313 pango_layout_set_width (priv->layout, mid_width);
3314 pango_layout_get_extents (priv->layout, NULL, &logical_rect);
3316 if (logical_rect.height <= height)
3317 width = logical_rect.width;
3322 pango_layout_set_width (priv->layout, width);
3325 else /* !priv->wrap */
3326 pango_layout_set_width (priv->layout, -1);
3331 get_single_line_height (GtkWidget *widget,
3332 PangoLayout *layout)
3334 PangoContext *context;
3335 PangoFontMetrics *metrics;
3336 gint ascent, descent;
3338 context = pango_layout_get_context (layout);
3339 metrics = pango_context_get_metrics (context, gtk_widget_get_style (widget)->font_desc,
3340 pango_context_get_language (context));
3342 ascent = pango_font_metrics_get_ascent (metrics);
3343 descent = pango_font_metrics_get_descent (metrics);
3344 pango_font_metrics_unref (metrics);
3346 return ascent + descent;
3349 static GtkSizeRequestMode
3350 gtk_label_get_request_mode (GtkWidget *widget)
3352 GtkLabel *label = GTK_LABEL (widget);
3353 gdouble angle = gtk_label_get_angle (label);
3355 if (angle == 90 || angle == 270)
3356 return GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
3358 return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
3362 get_size_for_allocation (GtkLabel *label,
3363 GtkOrientation orientation,
3368 GtkLabelPrivate *priv = label->priv;
3369 PangoLayout *layout;
3370 GtkWidgetAuxInfo *aux_info =
3371 _gtk_widget_get_aux_info (GTK_WIDGET (label), FALSE);
3375 gtk_label_ensure_layout (label, FALSE);
3376 layout = pango_layout_copy (priv->layout);
3380 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3381 aux_size = aux_info->width;
3383 aux_size = aux_info->height;
3389 pango_layout_set_width (layout, aux_size * PANGO_SCALE);
3391 pango_layout_set_width (layout, allocation * PANGO_SCALE);
3393 pango_layout_get_pixel_size (layout, NULL, &text_height);
3396 *minimum_size = text_height;
3399 *natural_size = text_height;
3401 g_object_unref (layout);
3405 gtk_label_get_preferred_size (GtkWidget *widget,
3406 GtkOrientation orientation,
3410 GtkLabel *label = GTK_LABEL (widget);
3411 GtkLabelPrivate *priv = label->priv;
3412 PangoRectangle required_rect;
3413 PangoRectangle natural_rect;
3416 /* "width-chars" Hard-coded minimum width:
3417 * - minimum size should be MAX (width-chars, strlen ("..."));
3418 * - natural size should be MAX (width-chars, strlen (priv->text));
3420 * "max-width-chars" User specified maximum size requisition
3421 * - minimum size should be MAX (width-chars, 0)
3422 * - natural size should be MIN (max-width-chars, strlen (priv->text))
3426 /* When calculating ->wrap sometimes we need to invent a size; Ideally we should be doing
3427 * that stuff here instead of inside gtk_label_ensure_layout() */
3429 gtk_label_clear_layout (label);
3430 gtk_label_ensure_layout (label, TRUE);
3432 /* Start off with the pixel extents of the rendered layout */
3433 pango_layout_get_extents (priv->layout, NULL, &required_rect);
3434 required_rect.x = required_rect.y = 0;
3436 if (priv->single_line_mode || priv->wrap)
3437 required_rect.height = get_single_line_height (GTK_WIDGET (label), priv->layout);
3439 natural_rect = required_rect;
3441 /* Calculate text width itself based on GtkLabel property rules */
3442 get_label_width (label, &required_rect.width, &natural_rect.width);
3444 /* Now that we have minimum and natural sizes in pango extents, apply a possible transform */
3445 if (priv->have_transform)
3447 PangoLayout *layout = pango_layout_copy (priv->layout);
3448 PangoContext *context = pango_layout_get_context (priv->layout);
3449 const PangoMatrix *matrix = pango_context_get_matrix (context);
3451 pango_layout_set_width (layout, -1);
3452 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_NONE);
3454 pango_layout_get_extents (layout, NULL, &natural_rect);
3455 g_object_unref (layout);
3457 pango_matrix_transform_rectangle (matrix, &required_rect);
3458 pango_matrix_transform_rectangle (matrix, &natural_rect);
3460 /* Bump the natural size in case of ellipsize to ensure pango has
3461 * enough space in the angles (note, we could alternatively set the
3462 * layout to not ellipsize when we know we have been allocated our
3463 * full natural size, or it may be that pango needs a fix here).
3465 if (priv->ellipsize && priv->angle != 0 && priv->angle != 90 &&
3466 priv->angle != 180 && priv->angle != 270 && priv->angle != 360)
3468 /* For some reason we only need this at about 110 degrees, and only
3469 * when gaining in height
3471 natural_rect.height += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3472 natural_rect.width += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3476 required_rect.width = PANGO_PIXELS_CEIL (required_rect.width);
3477 required_rect.height = PANGO_PIXELS_CEIL (required_rect.height);
3479 natural_rect.width = PANGO_PIXELS_CEIL (natural_rect.width);
3480 natural_rect.height = PANGO_PIXELS_CEIL (natural_rect.height);
3482 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3484 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3486 /* Note, we cant use get_size_for_allocation() when rotating
3487 * ellipsized labels.
3489 if (!(priv->ellipsize && priv->have_transform) &&
3490 (priv->angle == 90 || priv->angle == 270))
3492 /* Doing a h4w request on a rotated label here, return the
3493 * required width for the minimum height.
3495 get_size_for_allocation (label,
3496 GTK_ORIENTATION_VERTICAL,
3497 required_rect.height,
3498 minimum_size, natural_size);
3503 /* Normal desired width */
3504 *minimum_size = required_rect.width;
3505 *natural_size = natural_rect.width;
3508 *minimum_size += xpad * 2;
3509 *natural_size += xpad * 2;
3511 else /* GTK_ORIENTATION_VERTICAL */
3513 /* Note, we cant use get_size_for_allocation() when rotating
3514 * ellipsized labels.
3516 if (!(priv->ellipsize && priv->have_transform) &&
3517 (priv->angle == 0 || priv->angle == 180 || priv->angle == 360))
3519 /* Doing a w4h request on a label here, return the required
3520 * height for the minimum width.
3522 get_size_for_allocation (label,
3523 GTK_ORIENTATION_HORIZONTAL,
3524 required_rect.width,
3525 minimum_size, natural_size);
3529 /* A vertically rotated label does w4h, so return the base
3530 * desired height (text length)
3532 *minimum_size = required_rect.height;
3533 *natural_size = natural_rect.height;
3536 *minimum_size += ypad * 2;
3537 *natural_size += ypad * 2;
3540 /* Restore real allocated size of layout; sometimes size-requests
3541 * are randomly called without a following allocation; for this case
3542 * we need to make sure we dont have a mucked up layout because we
3543 * went and guessed the wrap-size.
3546 gtk_label_clear_layout (label);
3547 gtk_label_ensure_layout (label, FALSE);
3553 gtk_label_get_preferred_width (GtkWidget *widget,
3557 gtk_label_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
3561 gtk_label_get_preferred_height (GtkWidget *widget,
3565 gtk_label_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
3569 gtk_label_get_preferred_width_for_height (GtkWidget *widget,
3571 gint *minimum_width,
3572 gint *natural_width)
3574 GtkLabel *label = GTK_LABEL (widget);
3575 GtkLabelPrivate *priv = label->priv;
3577 if (priv->wrap && (priv->angle == 90 || priv->angle == 270))
3581 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3584 gtk_label_clear_layout (label);
3586 get_size_for_allocation (label, GTK_ORIENTATION_VERTICAL,
3587 MAX (1, height - (ypad * 2)),
3588 minimum_width, natural_width);
3591 *minimum_width += xpad * 2;
3594 *natural_width += xpad * 2;
3597 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_width, natural_width);
3601 gtk_label_get_preferred_height_for_width (GtkWidget *widget,
3603 gint *minimum_height,
3604 gint *natural_height)
3606 GtkLabel *label = GTK_LABEL (widget);
3607 GtkLabelPrivate *priv = label->priv;
3609 if (priv->wrap && (priv->angle == 0 || priv->angle == 180 || priv->angle == 360))
3613 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3616 gtk_label_clear_layout (label);
3618 get_size_for_allocation (label, GTK_ORIENTATION_HORIZONTAL,
3619 MAX (1, width - xpad * 2),
3620 minimum_height, natural_height);
3623 *minimum_height += ypad * 2;
3626 *natural_height += ypad * 2;
3629 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, minimum_height, natural_height);
3633 gtk_label_size_allocate (GtkWidget *widget,
3634 GtkAllocation *allocation)
3636 GtkLabel *label = GTK_LABEL (widget);
3637 GtkLabelPrivate *priv = label->priv;
3639 GTK_WIDGET_CLASS (gtk_label_parent_class)->size_allocate (widget, allocation);
3641 /* The layout may have been recently cleared in get_size_for_orientation(),
3642 * but the width at that point may not be the same as the allocated width
3645 gtk_label_clear_layout (label);
3647 gtk_label_ensure_layout (label, FALSE);
3649 if (priv->ellipsize)
3653 PangoRectangle logical;
3654 PangoRectangle bounds;
3657 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3659 bounds.x = bounds.y = 0;
3660 bounds.width = allocation->width - xpad * 2;
3661 bounds.height = allocation->height - ypad * 2;
3663 pango_layout_set_width (priv->layout, -1);
3664 pango_layout_get_pixel_extents (priv->layout, NULL, &logical);
3666 if (priv->have_transform)
3668 PangoContext *context = gtk_widget_get_pango_context (widget);
3669 const PangoMatrix *matrix = pango_context_get_matrix (context);
3671 const gdouble dx = matrix->xx; /* cos (M_PI * angle / 180) */
3672 const gdouble dy = matrix->xy; /* sin (M_PI * angle / 180) */
3673 if (fabs (dy) < 0.01)
3675 if (logical.width > bounds.width)
3676 pango_layout_set_width (priv->layout, bounds.width * PANGO_SCALE);
3678 else if (fabs (dx) < 0.01)
3680 if (logical.width > bounds.height)
3681 pango_layout_set_width (priv->layout, bounds.height * PANGO_SCALE);
3685 gdouble x0, y0, x1, y1, length;
3689 x0 = bounds.width / 2;
3690 y0 = dx ? x0 * dy / dx : G_MAXDOUBLE;
3691 vertical = fabs (y0) > bounds.height / 2;
3695 y0 = bounds.height/2;
3696 x0 = dy ? y0 * dx / dy : G_MAXDOUBLE;
3699 length = 2 * sqrt (x0 * x0 + y0 * y0);
3700 pango_layout_set_width (priv->layout, rint (length * PANGO_SCALE));
3701 pango_layout_get_pixel_size (priv->layout, NULL, &cy);
3708 y0 = bounds.height/2 + y1 - y0;
3713 x0 = bounds.width/2 + x1 - x0;
3717 length = length - sqrt (x0 * x0 + y0 * y0) * 2;
3718 pango_layout_set_width (priv->layout, rint (length * PANGO_SCALE));
3721 else if (logical.width > bounds.width)
3722 pango_layout_set_width (priv->layout, bounds.width * PANGO_SCALE);
3726 if (priv->select_info && priv->select_info->window)
3728 gdk_window_move_resize (priv->select_info->window,
3732 allocation->height);
3737 gtk_label_update_cursor (GtkLabel *label)
3739 GtkLabelPrivate *priv = label->priv;
3742 if (!priv->select_info)
3745 widget = GTK_WIDGET (label);
3747 if (gtk_widget_get_realized (widget))
3749 GdkDisplay *display;
3752 if (gtk_widget_is_sensitive (widget))
3754 display = gtk_widget_get_display (widget);
3756 if (priv->select_info->active_link)
3757 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
3758 else if (priv->select_info->selectable)
3759 cursor = gdk_cursor_new_for_display (display, GDK_XTERM);
3766 gdk_window_set_cursor (priv->select_info->window, cursor);
3769 gdk_cursor_unref (cursor);
3774 gtk_label_state_changed (GtkWidget *widget,
3775 GtkStateType prev_state)
3777 GtkLabel *label = GTK_LABEL (widget);
3778 GtkLabelPrivate *priv = label->priv;
3780 if (priv->select_info)
3782 gtk_label_select_region (label, 0, 0);
3783 gtk_label_update_cursor (label);
3786 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed)
3787 GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed (widget, prev_state);
3791 gtk_label_style_set (GtkWidget *widget,
3792 GtkStyle *previous_style)
3794 GtkLabel *label = GTK_LABEL (widget);
3796 /* We have to clear the layout, fonts etc. may have changed */
3797 gtk_label_clear_layout (label);
3798 gtk_label_invalidate_wrap_width (label);
3802 gtk_label_direction_changed (GtkWidget *widget,
3803 GtkTextDirection previous_dir)
3805 GtkLabel *label = GTK_LABEL (widget);
3806 GtkLabelPrivate *priv = label->priv;
3809 pango_layout_context_changed (priv->layout);
3811 GTK_WIDGET_CLASS (gtk_label_parent_class)->direction_changed (widget, previous_dir);
3815 get_layout_location (GtkLabel *label,
3819 GtkAllocation allocation;
3822 GtkLabelPrivate *priv;
3823 gint req_width, x, y;
3826 gfloat xalign, yalign;
3827 PangoRectangle logical;
3829 misc = GTK_MISC (label);
3830 widget = GTK_WIDGET (label);
3833 gtk_misc_get_alignment (misc, &xalign, &yalign);
3834 gtk_misc_get_padding (misc, &xpad, &ypad);
3836 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
3837 xalign = 1.0 - xalign;
3839 pango_layout_get_extents (priv->layout, NULL, &logical);
3841 if (priv->have_transform)
3843 PangoContext *context = gtk_widget_get_pango_context (widget);
3844 const PangoMatrix *matrix = pango_context_get_matrix (context);
3845 pango_matrix_transform_rectangle (matrix, &logical);
3848 pango_extents_to_pixels (&logical, NULL);
3850 req_width = logical.width;
3851 req_height = logical.height;
3853 req_width += 2 * xpad;
3854 req_height += 2 * ypad;
3856 gtk_widget_get_allocation (widget, &allocation);
3858 x = floor (allocation.x + xpad + xalign * (allocation.width - req_width));
3860 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
3861 x = MAX (x, allocation.x + xpad);
3863 x = MIN (x, allocation.x + allocation.width - xpad);
3866 /* bgo#315462 - For single-line labels, *do* align the requisition with
3867 * respect to the allocation, even if we are under-allocated. For multi-line
3868 * labels, always show the top of the text when they are under-allocated. The
3869 * rationale is this:
3871 * - Single-line labels appear in GtkButtons, and it is very easy to get them
3872 * to be smaller than their requisition. The button may clip the label, but
3873 * the label will still be able to show most of itself and the focus
3874 * rectangle. Also, it is fairly easy to read a single line of clipped text.
3876 * - Multi-line labels should not be clipped to showing "something in the
3877 * middle". You want to read the first line, at least, to get some context.
3879 if (pango_layout_get_line_count (priv->layout) == 1)
3880 y = floor (allocation.y + ypad + (allocation.height - req_height) * yalign);
3882 y = floor (allocation.y + ypad + MAX ((allocation.height - req_height) * yalign, 0));
3892 draw_insertion_cursor (GtkLabel *label,
3894 GdkRectangle *cursor_location,
3895 gboolean is_primary,
3896 PangoDirection direction,
3897 gboolean draw_arrow)
3899 GtkWidget *widget = GTK_WIDGET (label);
3900 GtkTextDirection text_dir;
3902 if (direction == PANGO_DIRECTION_LTR)
3903 text_dir = GTK_TEXT_DIR_LTR;
3905 text_dir = GTK_TEXT_DIR_RTL;
3907 gtk_draw_insertion_cursor (widget, cr, cursor_location,
3908 is_primary, text_dir, draw_arrow);
3911 static PangoDirection
3912 get_cursor_direction (GtkLabel *label)
3914 GtkLabelPrivate *priv = label->priv;
3917 g_assert (priv->select_info);
3919 gtk_label_ensure_layout (label, FALSE);
3921 for (l = pango_layout_get_lines_readonly (priv->layout); l; l = l->next)
3923 PangoLayoutLine *line = l->data;
3925 /* If priv->select_info->selection_end is at the very end of
3926 * the line, we don't know if the cursor is on this line or
3927 * the next without looking ahead at the next line. (End
3928 * of paragraph is different from line break.) But it's
3929 * definitely in this paragraph, which is good enough
3930 * to figure out the resolved direction.
3932 if (line->start_index + line->length >= priv->select_info->selection_end)
3933 return line->resolved_dir;
3936 return PANGO_DIRECTION_LTR;
3940 gtk_label_draw_cursor (GtkLabel *label, cairo_t *cr, gint xoffset, gint yoffset)
3942 GtkLabelPrivate *priv = label->priv;
3945 if (priv->select_info == NULL)
3948 widget = GTK_WIDGET (label);
3950 if (gtk_widget_is_drawable (widget))
3952 PangoDirection keymap_direction;
3953 PangoDirection cursor_direction;
3954 PangoRectangle strong_pos, weak_pos;
3955 gboolean split_cursor;
3956 PangoRectangle *cursor1 = NULL;
3957 PangoRectangle *cursor2 = NULL;
3958 GdkRectangle cursor_location;
3959 PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
3960 PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
3962 keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gtk_widget_get_display (widget)));
3963 cursor_direction = get_cursor_direction (label);
3965 gtk_label_ensure_layout (label, FALSE);
3967 pango_layout_get_cursor_pos (priv->layout, priv->select_info->selection_end,
3968 &strong_pos, &weak_pos);
3970 g_object_get (gtk_widget_get_settings (widget),
3971 "gtk-split-cursor", &split_cursor,
3974 dir1 = cursor_direction;
3978 cursor1 = &strong_pos;
3980 if (strong_pos.x != weak_pos.x ||
3981 strong_pos.y != weak_pos.y)
3983 dir2 = (cursor_direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
3984 cursor2 = &weak_pos;
3989 if (keymap_direction == cursor_direction)
3990 cursor1 = &strong_pos;
3992 cursor1 = &weak_pos;
3995 cursor_location.x = xoffset + PANGO_PIXELS (cursor1->x);
3996 cursor_location.y = yoffset + PANGO_PIXELS (cursor1->y);
3997 cursor_location.width = 0;
3998 cursor_location.height = PANGO_PIXELS (cursor1->height);
4000 draw_insertion_cursor (label, cr,
4001 &cursor_location, TRUE, dir1,
4002 dir2 != PANGO_DIRECTION_NEUTRAL);
4004 if (dir2 != PANGO_DIRECTION_NEUTRAL)
4006 cursor_location.x = xoffset + PANGO_PIXELS (cursor2->x);
4007 cursor_location.y = yoffset + PANGO_PIXELS (cursor2->y);
4008 cursor_location.width = 0;
4009 cursor_location.height = PANGO_PIXELS (cursor2->height);
4011 draw_insertion_cursor (label, cr,
4012 &cursor_location, FALSE, dir2,
4018 static GtkLabelLink *
4019 gtk_label_get_focus_link (GtkLabel *label)
4021 GtkLabelPrivate *priv = label->priv;
4022 GtkLabelSelectionInfo *info = priv->select_info;
4028 if (info->selection_anchor != info->selection_end)
4031 for (l = info->links; l; l = l->next)
4033 GtkLabelLink *link = l->data;
4034 if (link->start <= info->selection_anchor &&
4035 info->selection_anchor <= link->end)
4043 gtk_label_draw (GtkWidget *widget,
4046 GtkLabel *label = GTK_LABEL (widget);
4047 GtkLabelPrivate *priv = label->priv;
4048 GtkLabelSelectionInfo *info = priv->select_info;
4049 GtkAllocation allocation;
4054 gtk_label_ensure_layout (label, FALSE);
4056 if (priv->text && (*priv->text != '\0'))
4058 get_layout_location (label, &x, &y);
4060 style = gtk_widget_get_style (widget);
4061 window = gtk_widget_get_window (widget);
4062 gtk_widget_get_allocation (widget, &allocation);
4064 cairo_translate (cr, -allocation.x, -allocation.y);
4066 gtk_paint_layout (style,
4068 gtk_widget_get_state (widget),
4076 (info->selection_anchor != info->selection_end))
4079 cairo_region_t *clip;
4082 range[0] = info->selection_anchor;
4083 range[1] = info->selection_end;
4085 if (range[0] > range[1])
4087 gint tmp = range[0];
4088 range[0] = range[1];
4092 clip = gdk_pango_layout_get_clip_region (priv->layout,
4097 /* FIXME should use gtk_paint, but it can't use a clip
4103 gdk_cairo_region (cr, clip);
4106 state = GTK_STATE_SELECTED;
4107 if (!gtk_widget_has_focus (widget))
4108 state = GTK_STATE_ACTIVE;
4110 gdk_cairo_set_source_color (cr, &style->base[state]);
4113 gdk_cairo_set_source_color (cr, &style->text[state]);
4114 cairo_move_to (cr, x, y);
4115 _gtk_pango_fill_layout (cr, priv->layout);
4118 cairo_region_destroy (clip);
4122 GtkLabelLink *focus_link;
4123 GtkLabelLink *active_link;
4125 cairo_region_t *clip;
4127 GdkColor *text_color;
4128 GdkColor *base_color;
4129 GdkColor *link_color;
4130 GdkColor *visited_link_color;
4132 if (info->selectable && gtk_widget_has_focus (widget))
4133 gtk_label_draw_cursor (label, cr, x, y);
4135 focus_link = gtk_label_get_focus_link (label);
4136 active_link = info->active_link;
4141 range[0] = active_link->start;
4142 range[1] = active_link->end;
4146 clip = gdk_pango_layout_get_clip_region (priv->layout,
4150 gdk_cairo_region (cr, clip);
4152 cairo_region_destroy (clip);
4154 gtk_label_get_link_colors (widget, &link_color, &visited_link_color);
4155 if (active_link->visited)
4156 text_color = visited_link_color;
4158 text_color = link_color;
4159 if (info->link_clicked)
4160 base_color = &style->base[GTK_STATE_ACTIVE];
4162 base_color = &style->base[GTK_STATE_PRELIGHT];
4164 gdk_cairo_set_source_color (cr, base_color);
4167 gdk_cairo_set_source_color (cr, text_color);
4168 cairo_move_to (cr, x, y);
4169 _gtk_pango_fill_layout (cr, priv->layout);
4171 gdk_color_free (link_color);
4172 gdk_color_free (visited_link_color);
4177 if (focus_link && gtk_widget_has_focus (widget))
4179 range[0] = focus_link->start;
4180 range[1] = focus_link->end;
4182 clip = gdk_pango_layout_get_clip_region (priv->layout,
4186 cairo_region_get_extents (clip, &rect);
4188 gtk_paint_focus (style, cr, gtk_widget_get_state (widget),
4190 rect.x, rect.y, rect.width, rect.height);
4192 cairo_region_destroy (clip);
4201 separate_uline_pattern (const gchar *str,
4206 gboolean underscore;
4209 gchar *pattern_dest;
4211 *accel_key = GDK_KEY_VoidSymbol;
4212 *new_str = g_new (gchar, strlen (str) + 1);
4213 *pattern = g_new (gchar, g_utf8_strlen (str, -1) + 1);
4219 pattern_dest = *pattern;
4224 const gchar *next_src;
4226 c = g_utf8_get_char (src);
4227 if (c == (gunichar)-1)
4229 g_warning ("Invalid input string");
4235 next_src = g_utf8_next_char (src);
4240 *pattern_dest++ = ' ';
4243 *pattern_dest++ = '_';
4244 if (*accel_key == GDK_KEY_VoidSymbol)
4245 *accel_key = gdk_keyval_to_lower (gdk_unicode_to_keyval (c));
4248 while (src < next_src)
4262 while (src < next_src)
4265 *pattern_dest++ = ' ';
4277 gtk_label_set_uline_text_internal (GtkLabel *label,
4280 GtkLabelPrivate *priv = label->priv;
4281 guint accel_key = GDK_KEY_VoidSymbol;
4285 g_return_if_fail (GTK_IS_LABEL (label));
4286 g_return_if_fail (str != NULL);
4288 /* Split text into the base text and a separate pattern
4291 if (!separate_uline_pattern (str, &accel_key, &new_str, &pattern))
4294 gtk_label_set_text_internal (label, new_str);
4295 gtk_label_set_pattern_internal (label, pattern, TRUE);
4296 priv->mnemonic_keyval = accel_key;
4302 * gtk_label_set_text_with_mnemonic:
4303 * @label: a #GtkLabel
4306 * Sets the label's text from the string @str.
4307 * If characters in @str are preceded by an underscore, they are underlined
4308 * indicating that they represent a keyboard accelerator called a mnemonic.
4309 * The mnemonic key can be used to activate another widget, chosen
4310 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
4313 gtk_label_set_text_with_mnemonic (GtkLabel *label,
4316 g_return_if_fail (GTK_IS_LABEL (label));
4317 g_return_if_fail (str != NULL);
4319 g_object_freeze_notify (G_OBJECT (label));
4321 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
4322 gtk_label_set_use_markup_internal (label, FALSE);
4323 gtk_label_set_use_underline_internal (label, TRUE);
4325 gtk_label_recalculate (label);
4327 g_object_thaw_notify (G_OBJECT (label));
4331 gtk_label_realize (GtkWidget *widget)
4333 GtkLabel *label = GTK_LABEL (widget);
4334 GtkLabelPrivate *priv = label->priv;
4336 GTK_WIDGET_CLASS (gtk_label_parent_class)->realize (widget);
4338 if (priv->select_info)
4339 gtk_label_create_window (label);
4343 gtk_label_unrealize (GtkWidget *widget)
4345 GtkLabel *label = GTK_LABEL (widget);
4346 GtkLabelPrivate *priv = label->priv;
4348 if (priv->select_info)
4349 gtk_label_destroy_window (label);
4351 GTK_WIDGET_CLASS (gtk_label_parent_class)->unrealize (widget);
4355 gtk_label_map (GtkWidget *widget)
4357 GtkLabel *label = GTK_LABEL (widget);
4358 GtkLabelPrivate *priv = label->priv;
4360 GTK_WIDGET_CLASS (gtk_label_parent_class)->map (widget);
4362 if (priv->select_info)
4363 gdk_window_show (priv->select_info->window);
4367 gtk_label_unmap (GtkWidget *widget)
4369 GtkLabel *label = GTK_LABEL (widget);
4370 GtkLabelPrivate *priv = label->priv;
4372 if (priv->select_info)
4373 gdk_window_hide (priv->select_info->window);
4375 GTK_WIDGET_CLASS (gtk_label_parent_class)->unmap (widget);
4379 window_to_layout_coords (GtkLabel *label,
4383 GtkAllocation allocation;
4387 widget = GTK_WIDGET (label);
4389 /* get layout location in widget->window coords */
4390 get_layout_location (label, &lx, &ly);
4392 gtk_widget_get_allocation (widget, &allocation);
4396 *x += allocation.x; /* go to widget->window */
4397 *x -= lx; /* go to layout */
4402 *y += allocation.y; /* go to widget->window */
4403 *y -= ly; /* go to layout */
4409 layout_to_window_coords (GtkLabel *label,
4416 widget = GTK_WIDGET (label);
4418 /* get layout location in widget->window coords */
4419 get_layout_location (label, &lx, &ly);
4423 *x += lx; /* go to widget->window */
4424 *x -= widget->allocation.x; /* go to selection window */
4429 *y += ly; /* go to widget->window */
4430 *y -= widget->allocation.y; /* go to selection window */
4436 get_layout_index (GtkLabel *label,
4441 GtkLabelPrivate *priv = label->priv;
4443 const gchar *cluster;
4444 const gchar *cluster_end;
4449 gtk_label_ensure_layout (label, FALSE);
4451 window_to_layout_coords (label, &x, &y);
4456 inside = pango_layout_xy_to_index (priv->layout,
4460 cluster = priv->text + *index;
4461 cluster_end = cluster;
4464 cluster_end = g_utf8_next_char (cluster_end);
4468 *index += (cluster_end - cluster);
4474 gtk_label_select_word (GtkLabel *label)
4476 GtkLabelPrivate *priv = label->priv;
4479 gint start_index = gtk_label_move_backward_word (label, priv->select_info->selection_end);
4480 gint end_index = gtk_label_move_forward_word (label, priv->select_info->selection_end);
4482 min = MIN (priv->select_info->selection_anchor,
4483 priv->select_info->selection_end);
4484 max = MAX (priv->select_info->selection_anchor,
4485 priv->select_info->selection_end);
4487 min = MIN (min, start_index);
4488 max = MAX (max, end_index);
4490 gtk_label_select_region_index (label, min, max);
4494 gtk_label_grab_focus (GtkWidget *widget)
4496 GtkLabel *label = GTK_LABEL (widget);
4497 GtkLabelPrivate *priv = label->priv;
4498 gboolean select_on_focus;
4501 if (priv->select_info == NULL)
4504 GTK_WIDGET_CLASS (gtk_label_parent_class)->grab_focus (widget);
4506 if (priv->select_info->selectable)
4508 g_object_get (gtk_widget_get_settings (widget),
4509 "gtk-label-select-on-focus",
4513 if (select_on_focus && !priv->in_click)
4514 gtk_label_select_region (label, 0, -1);
4518 if (priv->select_info->links && !priv->in_click)
4520 link = priv->select_info->links->data;
4521 priv->select_info->selection_anchor = link->start;
4522 priv->select_info->selection_end = link->start;
4528 gtk_label_focus (GtkWidget *widget,
4529 GtkDirectionType direction)
4531 GtkLabel *label = GTK_LABEL (widget);
4532 GtkLabelPrivate *priv = label->priv;
4533 GtkLabelSelectionInfo *info = priv->select_info;
4534 GtkLabelLink *focus_link;
4537 if (!gtk_widget_is_focus (widget))
4539 gtk_widget_grab_focus (widget);
4542 focus_link = gtk_label_get_focus_link (label);
4543 if (focus_link && direction == GTK_DIR_TAB_BACKWARD)
4545 l = g_list_last (info->links);
4546 focus_link = l->data;
4547 info->selection_anchor = focus_link->start;
4548 info->selection_end = focus_link->start;
4558 if (info->selectable)
4562 if (info->selection_anchor != info->selection_end)
4565 index = info->selection_anchor;
4567 if (direction == GTK_DIR_TAB_FORWARD)
4568 for (l = info->links; l; l = l->next)
4570 GtkLabelLink *link = l->data;
4572 if (link->start > index)
4574 gtk_label_select_region_index (label, link->start, link->start);
4578 else if (direction == GTK_DIR_TAB_BACKWARD)
4579 for (l = g_list_last (info->links); l; l = l->prev)
4581 GtkLabelLink *link = l->data;
4583 if (link->end < index)
4585 gtk_label_select_region_index (label, link->start, link->start);
4594 focus_link = gtk_label_get_focus_link (label);
4597 case GTK_DIR_TAB_FORWARD:
4600 l = g_list_find (info->links, focus_link);
4607 case GTK_DIR_TAB_BACKWARD:
4610 l = g_list_find (info->links, focus_link);
4614 l = g_list_last (info->links);
4623 focus_link = l->data;
4624 info->selection_anchor = focus_link->start;
4625 info->selection_end = focus_link->start;
4626 gtk_widget_queue_draw (widget);
4638 gtk_label_button_press (GtkWidget *widget,
4639 GdkEventButton *event)
4641 GtkLabel *label = GTK_LABEL (widget);
4642 GtkLabelPrivate *priv = label->priv;
4643 GtkLabelSelectionInfo *info = priv->select_info;
4650 if (info->active_link)
4652 if (event->button == 1)
4654 info->link_clicked = 1;
4655 gtk_widget_queue_draw (widget);
4657 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4659 info->link_clicked = 1;
4660 gtk_label_do_popup (label, event);
4665 if (!info->selectable)
4668 info->in_drag = FALSE;
4669 info->select_words = FALSE;
4671 if (event->button == 1)
4673 if (!gtk_widget_has_focus (widget))
4675 priv->in_click = TRUE;
4676 gtk_widget_grab_focus (widget);
4677 priv->in_click = FALSE;
4680 if (event->type == GDK_3BUTTON_PRESS)
4682 gtk_label_select_region_index (label, 0, strlen (priv->text));
4686 if (event->type == GDK_2BUTTON_PRESS)
4688 info->select_words = TRUE;
4689 gtk_label_select_word (label);
4693 get_layout_index (label, event->x, event->y, &index);
4695 min = MIN (info->selection_anchor, info->selection_end);
4696 max = MAX (info->selection_anchor, info->selection_end);
4698 if ((info->selection_anchor != info->selection_end) &&
4699 (event->state & GDK_SHIFT_MASK))
4701 /* extend (same as motion) */
4702 min = MIN (min, index);
4703 max = MAX (max, index);
4705 /* ensure the anchor is opposite index */
4713 gtk_label_select_region_index (label, min, max);
4717 if (event->type == GDK_3BUTTON_PRESS)
4718 gtk_label_select_region_index (label, 0, strlen (priv->text));
4719 else if (event->type == GDK_2BUTTON_PRESS)
4720 gtk_label_select_word (label);
4721 else if (min < max && min <= index && index <= max)
4723 info->in_drag = TRUE;
4724 info->drag_start_x = event->x;
4725 info->drag_start_y = event->y;
4728 /* start a replacement */
4729 gtk_label_select_region_index (label, index, index);
4734 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4736 gtk_label_do_popup (label, event);
4744 gtk_label_button_release (GtkWidget *widget,
4745 GdkEventButton *event)
4748 GtkLabel *label = GTK_LABEL (widget);
4749 GtkLabelPrivate *priv = label->priv;
4750 GtkLabelSelectionInfo *info = priv->select_info;
4760 get_layout_index (label, event->x, event->y, &index);
4761 gtk_label_select_region_index (label, index, index);
4766 if (event->button != 1)
4769 if (info->active_link &&
4770 info->selection_anchor == info->selection_end &&
4773 emit_activate_link (label, info->active_link);
4774 info->link_clicked = 0;
4779 /* The goal here is to return TRUE iff we ate the
4780 * button press to start selecting.
4786 connect_mnemonics_visible_notify (GtkLabel *label)
4788 GtkLabelPrivate *priv = label->priv;
4789 GtkWidget *toplevel;
4792 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
4794 if (!GTK_IS_WINDOW (toplevel))
4797 /* always set up this widgets initial value */
4798 priv->mnemonics_visible =
4799 gtk_window_get_mnemonics_visible (GTK_WINDOW (toplevel));
4802 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (toplevel),
4803 "gtk-label-mnemonics-visible-connected"));
4807 g_signal_connect (toplevel,
4808 "notify::mnemonics-visible",
4809 G_CALLBACK (label_mnemonics_visible_changed),
4811 g_object_set_data (G_OBJECT (toplevel),
4812 "gtk-label-mnemonics-visible-connected",
4813 GINT_TO_POINTER (1));
4818 drag_begin_cb (GtkWidget *widget,
4819 GdkDragContext *context,
4822 GtkLabel *label = GTK_LABEL (widget);
4823 GtkLabelPrivate *priv = label->priv;
4824 cairo_surface_t *surface = NULL;
4826 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
4828 if ((priv->select_info->selection_anchor !=
4829 priv->select_info->selection_end) &&
4835 start = MIN (priv->select_info->selection_anchor,
4836 priv->select_info->selection_end);
4837 end = MAX (priv->select_info->selection_anchor,
4838 priv->select_info->selection_end);
4840 len = strlen (priv->text);
4848 surface = _gtk_text_util_create_drag_icon (widget,
4854 gtk_drag_set_icon_surface (context, surface);
4856 gtk_drag_set_icon_default (context);
4859 g_object_unref (surface);
4863 gtk_label_motion (GtkWidget *widget,
4864 GdkEventMotion *event)
4866 GtkLabel *label = GTK_LABEL (widget);
4867 GtkLabelPrivate *priv = label->priv;
4868 GtkLabelSelectionInfo *info = priv->select_info;
4874 if (info->links && !info->in_drag)
4878 gboolean found = FALSE;
4880 if (info->selection_anchor == info->selection_end)
4882 if (get_layout_index (label, event->x, event->y, &index))
4884 for (l = info->links; l != NULL; l = l->next)
4887 if (index >= link->start && index <= link->end)
4898 if (info->active_link != link)
4900 info->link_clicked = 0;
4901 info->active_link = link;
4902 gtk_label_update_cursor (label);
4903 gtk_widget_queue_draw (widget);
4908 if (info->active_link != NULL)
4910 info->link_clicked = 0;
4911 info->active_link = NULL;
4912 gtk_label_update_cursor (label);
4913 gtk_widget_queue_draw (widget);
4918 if (!info->selectable)
4921 if ((event->state & GDK_BUTTON1_MASK) == 0)
4926 if (gtk_drag_check_threshold (widget,
4929 event->x, event->y))
4931 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
4933 gtk_target_list_add_text_targets (target_list, 0);
4935 g_signal_connect (widget, "drag-begin",
4936 G_CALLBACK (drag_begin_cb), NULL);
4937 gtk_drag_begin (widget, target_list,
4939 1, (GdkEvent *)event);
4941 info->in_drag = FALSE;
4943 gtk_target_list_unref (target_list);
4950 gdk_window_get_device_position (info->window, event->device, &x, &y, NULL);
4951 get_layout_index (label, x, y, &index);
4953 if (info->select_words)
4956 gint old_min, old_max;
4959 min = gtk_label_move_backward_word (label, index);
4960 max = gtk_label_move_forward_word (label, index);
4962 anchor = info->selection_anchor;
4963 end = info->selection_end;
4965 old_min = MIN (anchor, end);
4966 old_max = MAX (anchor, end);
4973 else if (old_max < max)
4978 else if (anchor == old_min)
4989 gtk_label_select_region_index (label, anchor, end);
4992 gtk_label_select_region_index (label, info->selection_anchor, index);
4999 gtk_label_leave_notify (GtkWidget *widget,
5000 GdkEventCrossing *event)
5002 GtkLabel *label = GTK_LABEL (widget);
5003 GtkLabelPrivate *priv = label->priv;
5005 if (priv->select_info)
5007 priv->select_info->active_link = NULL;
5008 gtk_label_update_cursor (label);
5009 gtk_widget_queue_draw (widget);
5012 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event)
5013 return GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event (widget, event);
5019 gtk_label_create_window (GtkLabel *label)
5021 GtkLabelPrivate *priv = label->priv;
5022 GtkAllocation allocation;
5024 GdkWindowAttr attributes;
5025 gint attributes_mask;
5027 g_assert (priv->select_info);
5028 widget = GTK_WIDGET (label);
5029 g_assert (gtk_widget_get_realized (widget));
5031 if (priv->select_info->window)
5034 gtk_widget_get_allocation (widget, &allocation);
5036 attributes.x = allocation.x;
5037 attributes.y = allocation.y;
5038 attributes.width = allocation.width;
5039 attributes.height = allocation.height;
5040 attributes.window_type = GDK_WINDOW_CHILD;
5041 attributes.wclass = GDK_INPUT_ONLY;
5042 attributes.override_redirect = TRUE;
5043 attributes.event_mask = gtk_widget_get_events (widget) |
5044 GDK_BUTTON_PRESS_MASK |
5045 GDK_BUTTON_RELEASE_MASK |
5046 GDK_LEAVE_NOTIFY_MASK |
5047 GDK_BUTTON_MOTION_MASK |
5048 GDK_POINTER_MOTION_MASK |
5049 GDK_POINTER_MOTION_HINT_MASK;
5050 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
5051 if (gtk_widget_is_sensitive (widget))
5053 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
5055 attributes_mask |= GDK_WA_CURSOR;
5059 priv->select_info->window = gdk_window_new (gtk_widget_get_window (widget),
5060 &attributes, attributes_mask);
5061 gdk_window_set_user_data (priv->select_info->window, widget);
5063 if (attributes_mask & GDK_WA_CURSOR)
5064 gdk_cursor_unref (attributes.cursor);
5068 gtk_label_destroy_window (GtkLabel *label)
5070 GtkLabelPrivate *priv = label->priv;
5072 g_assert (priv->select_info);
5074 if (priv->select_info->window == NULL)
5077 gdk_window_set_user_data (priv->select_info->window, NULL);
5078 gdk_window_destroy (priv->select_info->window);
5079 priv->select_info->window = NULL;
5083 gtk_label_ensure_select_info (GtkLabel *label)
5085 GtkLabelPrivate *priv = label->priv;
5087 if (priv->select_info == NULL)
5089 priv->select_info = g_new0 (GtkLabelSelectionInfo, 1);
5091 gtk_widget_set_can_focus (GTK_WIDGET (label), TRUE);
5093 if (gtk_widget_get_realized (GTK_WIDGET (label)))
5094 gtk_label_create_window (label);
5096 if (gtk_widget_get_mapped (GTK_WIDGET (label)))
5097 gdk_window_show (priv->select_info->window);
5102 gtk_label_clear_select_info (GtkLabel *label)
5104 GtkLabelPrivate *priv = label->priv;
5106 if (priv->select_info == NULL)
5109 if (!priv->select_info->selectable && !priv->select_info->links)
5111 gtk_label_destroy_window (label);
5113 g_free (priv->select_info);
5114 priv->select_info = NULL;
5116 gtk_widget_set_can_focus (GTK_WIDGET (label), FALSE);
5121 * gtk_label_set_selectable:
5122 * @label: a #GtkLabel
5123 * @setting: %TRUE to allow selecting text in the label
5125 * Selectable labels allow the user to select text from the label, for
5129 gtk_label_set_selectable (GtkLabel *label,
5132 GtkLabelPrivate *priv;
5133 gboolean old_setting;
5135 g_return_if_fail (GTK_IS_LABEL (label));
5139 setting = setting != FALSE;
5140 old_setting = priv->select_info && priv->select_info->selectable;
5144 gtk_label_ensure_select_info (label);
5145 priv->select_info->selectable = TRUE;
5146 gtk_label_update_cursor (label);
5152 /* unselect, to give up the selection */
5153 gtk_label_select_region (label, 0, 0);
5155 priv->select_info->selectable = FALSE;
5156 gtk_label_clear_select_info (label);
5157 gtk_label_update_cursor (label);
5160 if (setting != old_setting)
5162 g_object_freeze_notify (G_OBJECT (label));
5163 g_object_notify (G_OBJECT (label), "selectable");
5164 g_object_notify (G_OBJECT (label), "cursor-position");
5165 g_object_notify (G_OBJECT (label), "selection-bound");
5166 g_object_thaw_notify (G_OBJECT (label));
5167 gtk_widget_queue_draw (GTK_WIDGET (label));
5172 * gtk_label_get_selectable:
5173 * @label: a #GtkLabel
5175 * Gets the value set by gtk_label_set_selectable().
5177 * Return value: %TRUE if the user can copy text from the label
5180 gtk_label_get_selectable (GtkLabel *label)
5182 GtkLabelPrivate *priv;
5184 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5188 return priv->select_info && priv->select_info->selectable;
5192 * gtk_label_set_angle:
5193 * @label: a #GtkLabel
5194 * @angle: the angle that the baseline of the label makes with
5195 * the horizontal, in degrees, measured counterclockwise
5197 * Sets the angle of rotation for the label. An angle of 90 reads from
5198 * from bottom to top, an angle of 270, from top to bottom. The angle
5199 * setting for the label is ignored if the label is selectable,
5200 * wrapped, or ellipsized.
5205 gtk_label_set_angle (GtkLabel *label,
5208 GtkLabelPrivate *priv;
5210 g_return_if_fail (GTK_IS_LABEL (label));
5214 /* Canonicalize to [0,360]. We don't canonicalize 360 to 0, because
5215 * double property ranges are inclusive, and changing 360 to 0 would
5216 * make a property editor behave strangely.
5218 if (angle < 0 || angle > 360.0)
5219 angle = angle - 360. * floor (angle / 360.);
5221 if (priv->angle != angle)
5223 priv->angle = angle;
5225 gtk_label_clear_layout (label);
5226 gtk_widget_queue_resize (GTK_WIDGET (label));
5228 g_object_notify (G_OBJECT (label), "angle");
5233 * gtk_label_get_angle:
5234 * @label: a #GtkLabel
5236 * Gets the angle of rotation for the label. See
5237 * gtk_label_set_angle().
5239 * Return value: the angle of rotation for the label
5244 gtk_label_get_angle (GtkLabel *label)
5246 g_return_val_if_fail (GTK_IS_LABEL (label), 0.0);
5248 return label->priv->angle;
5252 gtk_label_set_selection_text (GtkLabel *label,
5253 GtkSelectionData *selection_data)
5255 GtkLabelPrivate *priv = label->priv;
5257 if ((priv->select_info->selection_anchor !=
5258 priv->select_info->selection_end) &&
5264 start = MIN (priv->select_info->selection_anchor,
5265 priv->select_info->selection_end);
5266 end = MAX (priv->select_info->selection_anchor,
5267 priv->select_info->selection_end);
5269 len = strlen (priv->text);
5277 gtk_selection_data_set_text (selection_data,
5284 gtk_label_drag_data_get (GtkWidget *widget,
5285 GdkDragContext *context,
5286 GtkSelectionData *selection_data,
5290 gtk_label_set_selection_text (GTK_LABEL (widget), selection_data);
5294 get_text_callback (GtkClipboard *clipboard,
5295 GtkSelectionData *selection_data,
5297 gpointer user_data_or_owner)
5299 gtk_label_set_selection_text (GTK_LABEL (user_data_or_owner), selection_data);
5303 clear_text_callback (GtkClipboard *clipboard,
5304 gpointer user_data_or_owner)
5307 GtkLabelPrivate *priv;
5309 label = GTK_LABEL (user_data_or_owner);
5312 if (priv->select_info)
5314 priv->select_info->selection_anchor = priv->select_info->selection_end;
5316 gtk_widget_queue_draw (GTK_WIDGET (label));
5321 gtk_label_select_region_index (GtkLabel *label,
5325 GtkLabelPrivate *priv;
5327 g_return_if_fail (GTK_IS_LABEL (label));
5331 if (priv->select_info && priv->select_info->selectable)
5333 GtkClipboard *clipboard;
5335 if (priv->select_info->selection_anchor == anchor_index &&
5336 priv->select_info->selection_end == end_index)
5339 priv->select_info->selection_anchor = anchor_index;
5340 priv->select_info->selection_end = end_index;
5342 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label),
5343 GDK_SELECTION_PRIMARY);
5345 if (anchor_index != end_index)
5347 GtkTargetList *list;
5348 GtkTargetEntry *targets;
5351 list = gtk_target_list_new (NULL, 0);
5352 gtk_target_list_add_text_targets (list, 0);
5353 targets = gtk_target_table_new_from_list (list, &n_targets);
5355 gtk_clipboard_set_with_owner (clipboard,
5358 clear_text_callback,
5361 gtk_target_table_free (targets, n_targets);
5362 gtk_target_list_unref (list);
5366 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (label))
5367 gtk_clipboard_clear (clipboard);
5370 gtk_widget_queue_draw (GTK_WIDGET (label));
5372 g_object_freeze_notify (G_OBJECT (label));
5373 g_object_notify (G_OBJECT (label), "cursor-position");
5374 g_object_notify (G_OBJECT (label), "selection-bound");
5375 g_object_thaw_notify (G_OBJECT (label));
5380 * gtk_label_select_region:
5381 * @label: a #GtkLabel
5382 * @start_offset: start offset (in characters not bytes)
5383 * @end_offset: end offset (in characters not bytes)
5385 * Selects a range of characters in the label, if the label is selectable.
5386 * See gtk_label_set_selectable(). If the label is not selectable,
5387 * this function has no effect. If @start_offset or
5388 * @end_offset are -1, then the end of the label will be substituted.
5391 gtk_label_select_region (GtkLabel *label,
5395 GtkLabelPrivate *priv;
5397 g_return_if_fail (GTK_IS_LABEL (label));
5401 if (priv->text && priv->select_info)
5403 if (start_offset < 0)
5404 start_offset = g_utf8_strlen (priv->text, -1);
5407 end_offset = g_utf8_strlen (priv->text, -1);
5409 gtk_label_select_region_index (label,
5410 g_utf8_offset_to_pointer (priv->text, start_offset) - priv->text,
5411 g_utf8_offset_to_pointer (priv->text, end_offset) - priv->text);
5416 * gtk_label_get_selection_bounds:
5417 * @label: a #GtkLabel
5418 * @start: return location for start of selection, as a character offset
5419 * @end: return location for end of selection, as a character offset
5421 * Gets the selected range of characters in the label, returning %TRUE
5422 * if there's a selection.
5424 * Return value: %TRUE if selection is non-empty
5427 gtk_label_get_selection_bounds (GtkLabel *label,
5431 GtkLabelPrivate *priv;
5433 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5437 if (priv->select_info == NULL)
5439 /* not a selectable label */
5449 gint start_index, end_index;
5450 gint start_offset, end_offset;
5453 start_index = MIN (priv->select_info->selection_anchor,
5454 priv->select_info->selection_end);
5455 end_index = MAX (priv->select_info->selection_anchor,
5456 priv->select_info->selection_end);
5458 len = strlen (priv->text);
5460 if (end_index > len)
5463 if (start_index > len)
5466 start_offset = g_utf8_strlen (priv->text, start_index);
5467 end_offset = g_utf8_strlen (priv->text, end_index);
5469 if (start_offset > end_offset)
5471 gint tmp = start_offset;
5472 start_offset = end_offset;
5477 *start = start_offset;
5482 return start_offset != end_offset;
5488 * gtk_label_get_layout:
5489 * @label: a #GtkLabel
5491 * Gets the #PangoLayout used to display the label.
5492 * The layout is useful to e.g. convert text positions to
5493 * pixel positions, in combination with gtk_label_get_layout_offsets().
5494 * The returned layout is owned by the label so need not be
5495 * freed by the caller.
5497 * Return value: (transfer none): the #PangoLayout for this label
5500 gtk_label_get_layout (GtkLabel *label)
5502 GtkLabelPrivate *priv;
5504 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
5508 gtk_label_ensure_layout (label, FALSE);
5510 return priv->layout;
5514 * gtk_label_get_layout_offsets:
5515 * @label: a #GtkLabel
5516 * @x: (allow-none): location to store X offset of layout, or %NULL
5517 * @y: (allow-none): location to store Y offset of layout, or %NULL
5519 * Obtains the coordinates where the label will draw the #PangoLayout
5520 * representing the text in the label; useful to convert mouse events
5521 * into coordinates inside the #PangoLayout, e.g. to take some action
5522 * if some part of the label is clicked. Of course you will need to
5523 * create a #GtkEventBox to receive the events, and pack the label
5524 * inside it, since labels are a #GTK_NO_WINDOW widget. Remember
5525 * when using the #PangoLayout functions you need to convert to
5526 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
5529 gtk_label_get_layout_offsets (GtkLabel *label,
5533 g_return_if_fail (GTK_IS_LABEL (label));
5535 gtk_label_ensure_layout (label, FALSE);
5537 get_layout_location (label, x, y);
5541 * gtk_label_set_use_markup:
5542 * @label: a #GtkLabel
5543 * @setting: %TRUE if the label's text should be parsed for markup.
5545 * Sets whether the text of the label contains markup in <link
5546 * linkend="PangoMarkupFormat">Pango's text markup
5547 * language</link>. See gtk_label_set_markup().
5550 gtk_label_set_use_markup (GtkLabel *label,
5553 g_return_if_fail (GTK_IS_LABEL (label));
5555 gtk_label_set_use_markup_internal (label, setting);
5556 gtk_label_recalculate (label);
5560 * gtk_label_get_use_markup:
5561 * @label: a #GtkLabel
5563 * Returns whether the label's text is interpreted as marked up with
5564 * the <link linkend="PangoMarkupFormat">Pango text markup
5565 * language</link>. See gtk_label_set_use_markup ().
5567 * Return value: %TRUE if the label's text will be parsed for markup.
5570 gtk_label_get_use_markup (GtkLabel *label)
5572 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5574 return label->priv->use_markup;
5578 * gtk_label_set_use_underline:
5579 * @label: a #GtkLabel
5580 * @setting: %TRUE if underlines in the text indicate mnemonics
5582 * If true, an underline in the text indicates the next character should be
5583 * used for the mnemonic accelerator key.
5586 gtk_label_set_use_underline (GtkLabel *label,
5589 g_return_if_fail (GTK_IS_LABEL (label));
5591 gtk_label_set_use_underline_internal (label, setting);
5592 gtk_label_recalculate (label);
5596 * gtk_label_get_use_underline:
5597 * @label: a #GtkLabel
5599 * Returns whether an embedded underline in the label indicates a
5600 * mnemonic. See gtk_label_set_use_underline().
5602 * Return value: %TRUE whether an embedded underline in the label indicates
5603 * the mnemonic accelerator keys.
5606 gtk_label_get_use_underline (GtkLabel *label)
5608 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5610 return label->priv->use_underline;
5614 * gtk_label_set_single_line_mode:
5615 * @label: a #GtkLabel
5616 * @single_line_mode: %TRUE if the label should be in single line mode
5618 * Sets whether the label is in single line mode.
5623 gtk_label_set_single_line_mode (GtkLabel *label,
5624 gboolean single_line_mode)
5626 GtkLabelPrivate *priv;
5628 g_return_if_fail (GTK_IS_LABEL (label));
5632 single_line_mode = single_line_mode != FALSE;
5634 if (priv->single_line_mode != single_line_mode)
5636 priv->single_line_mode = single_line_mode;
5638 gtk_label_clear_layout (label);
5639 gtk_widget_queue_resize (GTK_WIDGET (label));
5641 g_object_notify (G_OBJECT (label), "single-line-mode");
5646 * gtk_label_get_single_line_mode:
5647 * @label: a #GtkLabel
5649 * Returns whether the label is in single line mode.
5651 * Return value: %TRUE when the label is in single line mode.
5656 gtk_label_get_single_line_mode (GtkLabel *label)
5658 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5660 return label->priv->single_line_mode;
5663 /* Compute the X position for an offset that corresponds to the "more important
5664 * cursor position for that offset. We use this when trying to guess to which
5665 * end of the selection we should go to when the user hits the left or
5669 get_better_cursor (GtkLabel *label,
5674 GtkLabelPrivate *priv = label->priv;
5675 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5676 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5677 PangoDirection cursor_direction = get_cursor_direction (label);
5678 gboolean split_cursor;
5679 PangoRectangle strong_pos, weak_pos;
5681 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5682 "gtk-split-cursor", &split_cursor,
5685 gtk_label_ensure_layout (label, FALSE);
5687 pango_layout_get_cursor_pos (priv->layout, index,
5688 &strong_pos, &weak_pos);
5692 *x = strong_pos.x / PANGO_SCALE;
5693 *y = strong_pos.y / PANGO_SCALE;
5697 if (keymap_direction == cursor_direction)
5699 *x = strong_pos.x / PANGO_SCALE;
5700 *y = strong_pos.y / PANGO_SCALE;
5704 *x = weak_pos.x / PANGO_SCALE;
5705 *y = weak_pos.y / PANGO_SCALE;
5712 gtk_label_move_logically (GtkLabel *label,
5716 GtkLabelPrivate *priv = label->priv;
5717 gint offset = g_utf8_pointer_to_offset (priv->text,
5718 priv->text + start);
5722 PangoLogAttr *log_attrs;
5726 gtk_label_ensure_layout (label, FALSE);
5728 length = g_utf8_strlen (priv->text, -1);
5730 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5732 while (count > 0 && offset < length)
5736 while (offset < length && !log_attrs[offset].is_cursor_position);
5740 while (count < 0 && offset > 0)
5744 while (offset > 0 && !log_attrs[offset].is_cursor_position);
5752 return g_utf8_offset_to_pointer (priv->text, offset) - priv->text;
5756 gtk_label_move_visually (GtkLabel *label,
5760 GtkLabelPrivate *priv = label->priv;
5767 int new_index, new_trailing;
5768 gboolean split_cursor;
5771 gtk_label_ensure_layout (label, FALSE);
5773 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5774 "gtk-split-cursor", &split_cursor,
5781 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5782 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5784 strong = keymap_direction == get_cursor_direction (label);
5789 pango_layout_move_cursor_visually (priv->layout, strong, index, 0, 1, &new_index, &new_trailing);
5794 pango_layout_move_cursor_visually (priv->layout, strong, index, 0, -1, &new_index, &new_trailing);
5798 if (new_index < 0 || new_index == G_MAXINT)
5803 while (new_trailing--)
5804 index = g_utf8_next_char (priv->text + new_index) - priv->text;
5811 gtk_label_move_forward_word (GtkLabel *label,
5814 GtkLabelPrivate *priv = label->priv;
5815 gint new_pos = g_utf8_pointer_to_offset (priv->text,
5816 priv->text + start);
5819 length = g_utf8_strlen (priv->text, -1);
5820 if (new_pos < length)
5822 PangoLogAttr *log_attrs;
5825 gtk_label_ensure_layout (label, FALSE);
5827 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5829 /* Find the next word end */
5831 while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
5837 return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
5842 gtk_label_move_backward_word (GtkLabel *label,
5845 GtkLabelPrivate *priv = label->priv;
5846 gint new_pos = g_utf8_pointer_to_offset (priv->text,
5847 priv->text + start);
5851 PangoLogAttr *log_attrs;
5854 gtk_label_ensure_layout (label, FALSE);
5856 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5860 /* Find the previous word beginning */
5861 while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
5867 return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
5871 gtk_label_move_cursor (GtkLabel *label,
5872 GtkMovementStep step,
5874 gboolean extend_selection)
5876 GtkLabelPrivate *priv = label->priv;
5880 if (priv->select_info == NULL)
5883 old_pos = new_pos = priv->select_info->selection_end;
5885 if (priv->select_info->selection_end != priv->select_info->selection_anchor &&
5888 /* If we have a current selection and aren't extending it, move to the
5889 * start/or end of the selection as appropriate
5893 case GTK_MOVEMENT_VISUAL_POSITIONS:
5896 gint anchor_x, anchor_y;
5897 gboolean end_is_left;
5899 get_better_cursor (label, priv->select_info->selection_end, &end_x, &end_y);
5900 get_better_cursor (label, priv->select_info->selection_anchor, &anchor_x, &anchor_y);
5902 end_is_left = (end_y < anchor_y) || (end_y == anchor_y && end_x < anchor_x);
5905 new_pos = end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
5907 new_pos = !end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
5910 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5911 case GTK_MOVEMENT_WORDS:
5913 new_pos = MIN (priv->select_info->selection_end, priv->select_info->selection_anchor);
5915 new_pos = MAX (priv->select_info->selection_end, priv->select_info->selection_anchor);
5917 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5918 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5919 case GTK_MOVEMENT_BUFFER_ENDS:
5920 /* FIXME: Can do better here */
5921 new_pos = count < 0 ? 0 : strlen (priv->text);
5923 case GTK_MOVEMENT_DISPLAY_LINES:
5924 case GTK_MOVEMENT_PARAGRAPHS:
5925 case GTK_MOVEMENT_PAGES:
5926 case GTK_MOVEMENT_HORIZONTAL_PAGES:
5934 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5935 new_pos = gtk_label_move_logically (label, new_pos, count);
5937 case GTK_MOVEMENT_VISUAL_POSITIONS:
5938 new_pos = gtk_label_move_visually (label, new_pos, count);
5939 if (new_pos == old_pos)
5941 if (!extend_selection)
5943 if (!gtk_widget_keynav_failed (GTK_WIDGET (label),
5945 GTK_DIR_RIGHT : GTK_DIR_LEFT))
5947 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
5950 gtk_widget_child_focus (toplevel,
5952 GTK_DIR_RIGHT : GTK_DIR_LEFT);
5957 gtk_widget_error_bell (GTK_WIDGET (label));
5961 case GTK_MOVEMENT_WORDS:
5964 new_pos = gtk_label_move_forward_word (label, new_pos);
5969 new_pos = gtk_label_move_backward_word (label, new_pos);
5972 if (new_pos == old_pos)
5973 gtk_widget_error_bell (GTK_WIDGET (label));
5975 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5976 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5977 case GTK_MOVEMENT_BUFFER_ENDS:
5978 /* FIXME: Can do better here */
5979 new_pos = count < 0 ? 0 : strlen (priv->text);
5980 if (new_pos == old_pos)
5981 gtk_widget_error_bell (GTK_WIDGET (label));
5983 case GTK_MOVEMENT_DISPLAY_LINES:
5984 case GTK_MOVEMENT_PARAGRAPHS:
5985 case GTK_MOVEMENT_PAGES:
5986 case GTK_MOVEMENT_HORIZONTAL_PAGES:
5991 if (extend_selection)
5992 gtk_label_select_region_index (label,
5993 priv->select_info->selection_anchor,
5996 gtk_label_select_region_index (label, new_pos, new_pos);
6000 gtk_label_copy_clipboard (GtkLabel *label)
6002 GtkLabelPrivate *priv = label->priv;
6004 if (priv->text && priv->select_info)
6008 GtkClipboard *clipboard;
6010 start = MIN (priv->select_info->selection_anchor,
6011 priv->select_info->selection_end);
6012 end = MAX (priv->select_info->selection_anchor,
6013 priv->select_info->selection_end);
6015 len = strlen (priv->text);
6023 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6026 gtk_clipboard_set_text (clipboard, priv->text + start, end - start);
6031 link = gtk_label_get_focus_link (label);
6033 gtk_clipboard_set_text (clipboard, link->uri, -1);
6039 gtk_label_select_all (GtkLabel *label)
6041 GtkLabelPrivate *priv = label->priv;
6043 gtk_label_select_region_index (label, 0, strlen (priv->text));
6046 /* Quick hack of a popup menu
6049 activate_cb (GtkWidget *menuitem,
6052 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6053 g_signal_emit_by_name (label, signal);
6057 append_action_signal (GtkLabel *label,
6059 const gchar *stock_id,
6060 const gchar *signal,
6063 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6065 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
6066 g_signal_connect (menuitem, "activate",
6067 G_CALLBACK (activate_cb), label);
6069 gtk_widget_set_sensitive (menuitem, sensitive);
6071 gtk_widget_show (menuitem);
6072 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6076 popup_menu_detach (GtkWidget *attach_widget,
6079 GtkLabel *label = GTK_LABEL (attach_widget);
6080 GtkLabelPrivate *priv = label->priv;
6082 if (priv->select_info)
6083 priv->select_info->popup_menu = NULL;
6087 popup_position_func (GtkMenu *menu,
6095 GtkAllocation allocation;
6099 label = GTK_LABEL (user_data);
6100 widget = GTK_WIDGET (label);
6102 g_return_if_fail (gtk_widget_get_realized (widget));
6104 screen = gtk_widget_get_screen (widget);
6105 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
6107 gtk_widget_get_allocation (widget, &allocation);
6112 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
6115 gtk_widget_get_allocation (widget, &allocation);
6117 *x += allocation.width / 2;
6118 *y += allocation.height;
6120 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
6121 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
6125 open_link_activate_cb (GtkMenuItem *menu_item,
6130 link = gtk_label_get_current_link (label);
6133 emit_activate_link (label, link);
6137 copy_link_activate_cb (GtkMenuItem *menu_item,
6140 GtkClipboard *clipboard;
6143 uri = gtk_label_get_current_uri (label);
6146 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6147 gtk_clipboard_set_text (clipboard, uri, -1);
6152 gtk_label_popup_menu (GtkWidget *widget)
6154 gtk_label_do_popup (GTK_LABEL (widget), NULL);
6160 gtk_label_do_popup (GtkLabel *label,
6161 GdkEventButton *event)
6163 GtkLabelPrivate *priv = label->priv;
6164 GtkWidget *menuitem;
6167 gboolean have_selection;
6170 if (!priv->select_info)
6173 if (priv->select_info->popup_menu)
6174 gtk_widget_destroy (priv->select_info->popup_menu);
6176 priv->select_info->popup_menu = menu = gtk_menu_new ();
6178 gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (label), popup_menu_detach);
6181 priv->select_info->selection_anchor != priv->select_info->selection_end;
6185 if (priv->select_info->link_clicked)
6186 link = priv->select_info->active_link;
6191 link = gtk_label_get_focus_link (label);
6193 if (!have_selection && link)
6196 menuitem = gtk_image_menu_item_new_with_mnemonic (_("_Open Link"));
6197 gtk_widget_show (menuitem);
6198 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6200 g_signal_connect (G_OBJECT (menuitem), "activate",
6201 G_CALLBACK (open_link_activate_cb), label);
6203 image = gtk_image_new_from_stock (GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_MENU);
6204 gtk_widget_show (image);
6205 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6207 /* Copy Link Address */
6208 menuitem = gtk_image_menu_item_new_with_mnemonic (_("Copy _Link Address"));
6209 gtk_widget_show (menuitem);
6210 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6212 g_signal_connect (G_OBJECT (menuitem), "activate",
6213 G_CALLBACK (copy_link_activate_cb), label);
6215 image = gtk_image_new_from_stock (GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
6216 gtk_widget_show (image);
6217 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6221 append_action_signal (label, menu, GTK_STOCK_CUT, "cut-clipboard", FALSE);
6222 append_action_signal (label, menu, GTK_STOCK_COPY, "copy-clipboard", have_selection);
6223 append_action_signal (label, menu, GTK_STOCK_PASTE, "paste-clipboard", FALSE);
6225 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
6226 gtk_widget_set_sensitive (menuitem, FALSE);
6227 gtk_widget_show (menuitem);
6228 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6230 menuitem = gtk_separator_menu_item_new ();
6231 gtk_widget_show (menuitem);
6232 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6234 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
6235 g_signal_connect_swapped (menuitem, "activate",
6236 G_CALLBACK (gtk_label_select_all), label);
6237 gtk_widget_show (menuitem);
6238 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6241 g_signal_emit (label, signals[POPULATE_POPUP], 0, menu);
6244 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6246 event->button, event->time);
6249 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6250 popup_position_func, label,
6251 0, gtk_get_current_event_time ());
6252 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
6257 gtk_label_clear_links (GtkLabel *label)
6259 GtkLabelPrivate *priv = label->priv;
6261 if (!priv->select_info)
6264 g_list_foreach (priv->select_info->links, (GFunc)link_free, NULL);
6265 g_list_free (priv->select_info->links);
6266 priv->select_info->links = NULL;
6267 priv->select_info->active_link = NULL;
6271 gtk_label_rescan_links (GtkLabel *label)
6273 GtkLabelPrivate *priv = label->priv;
6274 PangoLayout *layout = priv->layout;
6275 PangoAttrList *attlist;
6276 PangoAttrIterator *iter;
6279 if (!priv->select_info || !priv->select_info->links)
6282 attlist = pango_layout_get_attributes (layout);
6284 if (attlist == NULL)
6287 iter = pango_attr_list_get_iterator (attlist);
6289 links = priv->select_info->links;
6293 PangoAttribute *underline;
6294 PangoAttribute *color;
6296 underline = pango_attr_iterator_get (iter, PANGO_ATTR_UNDERLINE);
6297 color = pango_attr_iterator_get (iter, PANGO_ATTR_FOREGROUND);
6299 if (underline != NULL && color != NULL)
6302 PangoRectangle start_pos;
6303 PangoRectangle end_pos;
6306 pango_attr_iterator_range (iter, &start, &end);
6307 pango_layout_index_to_pos (layout, start, &start_pos);
6308 pango_layout_index_to_pos (layout, end, &end_pos);
6312 g_warning ("Ran out of links");
6316 links = links->next;
6317 link->start = start;
6320 } while (pango_attr_iterator_next (iter));
6322 pango_attr_iterator_destroy (iter);
6326 gtk_label_activate_link (GtkLabel *label,
6329 GtkWidget *widget = GTK_WIDGET (label);
6330 GError *error = NULL;
6332 if (!gtk_show_uri (gtk_widget_get_screen (widget),
6333 uri, gtk_get_current_event_time (), &error))
6335 g_warning ("Unable to show '%s': %s", uri, error->message);
6336 g_error_free (error);
6343 emit_activate_link (GtkLabel *label,
6346 GtkLabelPrivate *priv = label->priv;
6349 g_signal_emit (label, signals[ACTIVATE_LINK], 0, link->uri, &handled);
6350 if (handled && priv->track_links && !link->visited)
6352 link->visited = TRUE;
6353 /* FIXME: shouldn't have to redo everything here */
6354 gtk_label_recalculate (label);
6359 gtk_label_activate_current_link (GtkLabel *label)
6362 GtkWidget *widget = GTK_WIDGET (label);
6364 link = gtk_label_get_focus_link (label);
6368 emit_activate_link (label, link);
6372 GtkWidget *toplevel;
6374 GtkWidget *default_widget, *focus_widget;
6376 toplevel = gtk_widget_get_toplevel (widget);
6377 if (GTK_IS_WINDOW (toplevel))
6379 window = GTK_WINDOW (toplevel);
6383 default_widget = gtk_window_get_default_widget (window);
6384 focus_widget = gtk_window_get_focus (window);
6386 if (default_widget != widget &&
6387 !(widget == focus_widget && (!default_widget || !gtk_widget_is_sensitive (default_widget))))
6388 gtk_window_activate_default (window);
6394 static GtkLabelLink *
6395 gtk_label_get_current_link (GtkLabel *label)
6397 GtkLabelPrivate *priv = label->priv;
6400 if (!priv->select_info)
6403 if (priv->select_info->link_clicked)
6404 link = priv->select_info->active_link;
6406 link = gtk_label_get_focus_link (label);
6412 * gtk_label_get_current_uri:
6413 * @label: a #GtkLabel
6415 * Returns the URI for the currently active link in the label.
6416 * The active link is the one under the mouse pointer or, in a
6417 * selectable label, the link in which the text cursor is currently
6420 * This function is intended for use in a #GtkLabel::activate-link handler
6421 * or for use in a #GtkWidget::query-tooltip handler.
6423 * Returns: the currently active URI. The string is owned by GTK+ and must
6424 * not be freed or modified.
6428 G_CONST_RETURN gchar *
6429 gtk_label_get_current_uri (GtkLabel *label)
6433 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
6435 link = gtk_label_get_current_link (label);
6444 * gtk_label_set_track_visited_links:
6445 * @label: a #GtkLabel
6446 * @track_links: %TRUE to track visited links
6448 * Sets whether the label should keep track of clicked
6449 * links (and use a different color for them).
6454 gtk_label_set_track_visited_links (GtkLabel *label,
6455 gboolean track_links)
6457 GtkLabelPrivate *priv;
6459 g_return_if_fail (GTK_IS_LABEL (label));
6463 track_links = track_links != FALSE;
6465 if (priv->track_links != track_links)
6467 priv->track_links = track_links;
6469 /* FIXME: shouldn't have to redo everything here */
6470 gtk_label_recalculate (label);
6472 g_object_notify (G_OBJECT (label), "track-visited-links");
6477 * gtk_label_get_track_visited_links:
6478 * @label: a #GtkLabel
6480 * Returns whether the label is currently keeping track
6483 * Returns: %TRUE if clicked links are remembered
6488 gtk_label_get_track_visited_links (GtkLabel *label)
6490 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
6492 return label->priv->track_links;
6496 gtk_label_query_tooltip (GtkWidget *widget,
6499 gboolean keyboard_tip,
6500 GtkTooltip *tooltip)
6502 GtkLabel *label = GTK_LABEL (widget);
6503 GtkLabelPrivate *priv = label->priv;
6504 GtkLabelSelectionInfo *info = priv->select_info;
6508 if (info && info->links)
6512 if (info->selection_anchor == info->selection_end)
6513 index = info->selection_anchor;
6517 if (!get_layout_index (label, x, y, &index))
6523 for (l = info->links; l != NULL; l = l->next)
6525 GtkLabelLink *link = l->data;
6526 if (index >= link->start && index <= link->end)
6530 gtk_tooltip_set_markup (tooltip, link->title);
6539 return GTK_WIDGET_CLASS (gtk_label_parent_class)->query_tooltip (widget,