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 "gtksizerequest.h"
53 #include "gtkprivate.h"
58 GtkLabelSelectionInfo *select_info;
59 GtkWidget *mnemonic_widget;
60 GtkWindow *mnemonic_window;
63 PangoAttrList *effective_attrs;
66 gboolean mnemonics_visible;
73 guint use_underline : 1;
76 guint single_line_mode : 1;
77 guint have_transform : 1;
80 guint pattern_set : 1;
81 guint track_links : 1;
83 guint mnemonic_keyval;
92 /* Notes about the handling of links:
94 * Links share the GtkLabelSelectionInfo struct with selectable labels.
95 * There are some new fields for links. The links field contains the list
96 * of GtkLabelLink structs that describe the links which are embedded in
97 * the label. The active_link field points to the link under the mouse
98 * pointer. For keyboard navigation, the 'focus' link is determined by
99 * finding the link which contains the selection_anchor position.
100 * The link_clicked field is used with button press and release events
101 * to ensure that pressing inside a link and releasing outside of it
102 * does not activate the link.
104 * Links are rendered with the link-color/visited-link-color colors
105 * that are determined by the style and with an underline. When the mouse
106 * pointer is over a link, the pointer is changed to indicate the link,
107 * and the background behind the link is rendered with the base[PRELIGHT]
108 * color. While a button is pressed over a link, the background is rendered
109 * with the base[ACTIVE] color.
111 * Labels with links accept keyboard focus, and it is possible to move
112 * the focus between the embedded links using Tab/Shift-Tab. The focus
113 * is indicated by a focus rectangle that is drawn around the link text.
114 * Pressing Enter activates the focussed link, and there is a suitable
115 * context menu for links that can be opened with the Menu key. Pressing
116 * Control-C copies the link URI to the clipboard.
118 * In selectable labels with links, link functionality is only available
119 * when the selection is empty.
124 gchar *title; /* the title attribute, used as tooltip */
125 gboolean visited; /* get set when the link is activated; this flag
126 * gets preserved over later set_markup() calls
128 gint start; /* position of the link in the PangoLayout */
132 struct _GtkLabelSelectionInfo
135 gint selection_anchor;
137 GtkWidget *popup_menu;
140 GtkLabelLink *active_link;
146 guint select_words : 1;
147 guint selectable : 1;
148 guint link_clicked : 1;
156 ACTIVATE_CURRENT_LINK,
171 PROP_MNEMONIC_KEYVAL,
172 PROP_MNEMONIC_WIDGET,
173 PROP_CURSOR_POSITION,
174 PROP_SELECTION_BOUND,
177 PROP_SINGLE_LINE_MODE,
179 PROP_MAX_WIDTH_CHARS,
180 PROP_TRACK_VISITED_LINKS
183 /* When rotating ellipsizable text we want the natural size to request
184 * more to ensure the label wont ever ellipsize in an allocation of full natural size.
186 #define ROTATION_ELLIPSIZE_PADDING 2
188 static guint signals[LAST_SIGNAL] = { 0 };
190 static const GdkColor default_link_color = { 0, 0, 0, 0xeeee };
191 static const GdkColor default_visited_link_color = { 0, 0x5555, 0x1a1a, 0x8b8b };
193 static void gtk_label_set_property (GObject *object,
197 static void gtk_label_get_property (GObject *object,
201 static void gtk_label_destroy (GtkObject *object);
202 static void gtk_label_finalize (GObject *object);
203 static void gtk_label_size_allocate (GtkWidget *widget,
204 GtkAllocation *allocation);
205 static void gtk_label_state_changed (GtkWidget *widget,
207 static void gtk_label_style_set (GtkWidget *widget,
208 GtkStyle *previous_style);
209 static void gtk_label_direction_changed (GtkWidget *widget,
210 GtkTextDirection previous_dir);
211 static gint gtk_label_expose (GtkWidget *widget,
212 GdkEventExpose *event);
213 static gboolean gtk_label_focus (GtkWidget *widget,
214 GtkDirectionType direction);
216 static void gtk_label_realize (GtkWidget *widget);
217 static void gtk_label_unrealize (GtkWidget *widget);
218 static void gtk_label_map (GtkWidget *widget);
219 static void gtk_label_unmap (GtkWidget *widget);
221 static gboolean gtk_label_button_press (GtkWidget *widget,
222 GdkEventButton *event);
223 static gboolean gtk_label_button_release (GtkWidget *widget,
224 GdkEventButton *event);
225 static gboolean gtk_label_motion (GtkWidget *widget,
226 GdkEventMotion *event);
227 static gboolean gtk_label_leave_notify (GtkWidget *widget,
228 GdkEventCrossing *event);
230 static void gtk_label_grab_focus (GtkWidget *widget);
232 static gboolean gtk_label_query_tooltip (GtkWidget *widget,
235 gboolean keyboard_tip,
236 GtkTooltip *tooltip);
238 static void gtk_label_set_text_internal (GtkLabel *label,
240 static void gtk_label_set_label_internal (GtkLabel *label,
242 static void gtk_label_set_use_markup_internal (GtkLabel *label,
244 static void gtk_label_set_use_underline_internal (GtkLabel *label,
246 static void gtk_label_set_attributes_internal (GtkLabel *label,
247 PangoAttrList *attrs);
248 static void gtk_label_set_uline_text_internal (GtkLabel *label,
250 static void gtk_label_set_pattern_internal (GtkLabel *label,
251 const gchar *pattern,
252 gboolean is_mnemonic);
253 static void gtk_label_set_markup_internal (GtkLabel *label,
255 gboolean with_uline);
256 static void gtk_label_recalculate (GtkLabel *label);
257 static void gtk_label_hierarchy_changed (GtkWidget *widget,
258 GtkWidget *old_toplevel);
259 static void gtk_label_screen_changed (GtkWidget *widget,
260 GdkScreen *old_screen);
261 static gboolean gtk_label_popup_menu (GtkWidget *widget);
263 static void gtk_label_create_window (GtkLabel *label);
264 static void gtk_label_destroy_window (GtkLabel *label);
265 static void gtk_label_ensure_select_info (GtkLabel *label);
266 static void gtk_label_clear_select_info (GtkLabel *label);
267 static void gtk_label_update_cursor (GtkLabel *label);
268 static void gtk_label_clear_layout (GtkLabel *label);
269 static void gtk_label_ensure_layout (GtkLabel *label, gboolean guess_wrap_width);
270 static void gtk_label_invalidate_wrap_width (GtkLabel *label);
271 static void gtk_label_select_region_index (GtkLabel *label,
275 static gboolean gtk_label_mnemonic_activate (GtkWidget *widget,
276 gboolean group_cycling);
277 static void gtk_label_setup_mnemonic (GtkLabel *label,
279 static void gtk_label_drag_data_get (GtkWidget *widget,
280 GdkDragContext *context,
281 GtkSelectionData *selection_data,
285 static void gtk_label_buildable_interface_init (GtkBuildableIface *iface);
286 static gboolean gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
289 const gchar *tagname,
290 GMarkupParser *parser,
293 static void gtk_label_buildable_custom_finished (GtkBuildable *buildable,
296 const gchar *tagname,
300 static void connect_mnemonics_visible_notify (GtkLabel *label);
301 static gboolean separate_uline_pattern (const gchar *str,
307 /* For selectable labels: */
308 static void gtk_label_move_cursor (GtkLabel *label,
309 GtkMovementStep step,
311 gboolean extend_selection);
312 static void gtk_label_copy_clipboard (GtkLabel *label);
313 static void gtk_label_select_all (GtkLabel *label);
314 static void gtk_label_do_popup (GtkLabel *label,
315 GdkEventButton *event);
316 static gint gtk_label_move_forward_word (GtkLabel *label,
318 static gint gtk_label_move_backward_word (GtkLabel *label,
322 static void gtk_label_rescan_links (GtkLabel *label);
323 static void gtk_label_clear_links (GtkLabel *label);
324 static gboolean gtk_label_activate_link (GtkLabel *label,
326 static void gtk_label_activate_current_link (GtkLabel *label);
327 static GtkLabelLink *gtk_label_get_current_link (GtkLabel *label);
328 static void gtk_label_get_link_colors (GtkWidget *widget,
329 GdkColor **link_color,
330 GdkColor **visited_link_color);
331 static void emit_activate_link (GtkLabel *label,
334 static void gtk_label_size_request_init (GtkSizeRequestIface *iface);
335 static GtkSizeRequestMode gtk_label_get_request_mode (GtkSizeRequest *widget);
336 static void gtk_label_get_width (GtkSizeRequest *widget,
339 static void gtk_label_get_height (GtkSizeRequest *widget,
342 static void gtk_label_get_width_for_height (GtkSizeRequest *widget,
345 gint *natural_width);
346 static void gtk_label_get_height_for_width (GtkSizeRequest *widget,
348 gint *minimum_height,
349 gint *natural_height);
351 static GtkBuildableIface *buildable_parent_iface = NULL;
353 G_DEFINE_TYPE_WITH_CODE (GtkLabel, gtk_label, GTK_TYPE_MISC,
354 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
355 gtk_label_buildable_interface_init)
356 G_IMPLEMENT_INTERFACE (GTK_TYPE_SIZE_REQUEST,
357 gtk_label_size_request_init));
360 add_move_binding (GtkBindingSet *binding_set,
363 GtkMovementStep step,
366 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
368 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
372 G_TYPE_BOOLEAN, FALSE);
374 /* Selection-extending version */
375 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
379 G_TYPE_BOOLEAN, TRUE);
383 gtk_label_class_init (GtkLabelClass *class)
385 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
386 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
387 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
388 GtkBindingSet *binding_set;
390 gobject_class->set_property = gtk_label_set_property;
391 gobject_class->get_property = gtk_label_get_property;
392 gobject_class->finalize = gtk_label_finalize;
394 object_class->destroy = gtk_label_destroy;
396 widget_class->size_allocate = gtk_label_size_allocate;
397 widget_class->state_changed = gtk_label_state_changed;
398 widget_class->style_set = gtk_label_style_set;
399 widget_class->query_tooltip = gtk_label_query_tooltip;
400 widget_class->direction_changed = gtk_label_direction_changed;
401 widget_class->expose_event = gtk_label_expose;
402 widget_class->realize = gtk_label_realize;
403 widget_class->unrealize = gtk_label_unrealize;
404 widget_class->map = gtk_label_map;
405 widget_class->unmap = gtk_label_unmap;
406 widget_class->button_press_event = gtk_label_button_press;
407 widget_class->button_release_event = gtk_label_button_release;
408 widget_class->motion_notify_event = gtk_label_motion;
409 widget_class->leave_notify_event = gtk_label_leave_notify;
410 widget_class->hierarchy_changed = gtk_label_hierarchy_changed;
411 widget_class->screen_changed = gtk_label_screen_changed;
412 widget_class->mnemonic_activate = gtk_label_mnemonic_activate;
413 widget_class->drag_data_get = gtk_label_drag_data_get;
414 widget_class->grab_focus = gtk_label_grab_focus;
415 widget_class->popup_menu = gtk_label_popup_menu;
416 widget_class->focus = gtk_label_focus;
418 class->move_cursor = gtk_label_move_cursor;
419 class->copy_clipboard = gtk_label_copy_clipboard;
420 class->activate_link = gtk_label_activate_link;
423 * GtkLabel::move-cursor:
424 * @entry: the object which received the signal
425 * @step: the granularity of the move, as a #GtkMovementStep
426 * @count: the number of @step units to move
427 * @extend_selection: %TRUE if the move should extend the selection
429 * The ::move-cursor signal is a
430 * <link linkend="keybinding-signals">keybinding signal</link>
431 * which gets emitted when the user initiates a cursor movement.
432 * If the cursor is not visible in @entry, this signal causes
433 * the viewport to be moved instead.
435 * Applications should not connect to it, but may emit it with
436 * g_signal_emit_by_name() if they need to control the cursor
439 * The default bindings for this signal come in two variants,
440 * the variant with the Shift modifier extends the selection,
441 * the variant without the Shift modifer does not.
442 * There are too many key combinations to list them all here.
444 * <listitem>Arrow keys move by individual characters/lines</listitem>
445 * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
446 * <listitem>Home/End keys move to the ends of the buffer</listitem>
449 signals[MOVE_CURSOR] =
450 g_signal_new (I_("move-cursor"),
451 G_OBJECT_CLASS_TYPE (gobject_class),
452 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
453 G_STRUCT_OFFSET (GtkLabelClass, move_cursor),
455 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
457 GTK_TYPE_MOVEMENT_STEP,
462 * GtkLabel::copy-clipboard:
463 * @label: the object which received the signal
465 * The ::copy-clipboard signal is a
466 * <link linkend="keybinding-signals">keybinding signal</link>
467 * which gets emitted to copy the selection to the clipboard.
469 * The default binding for this signal is Ctrl-c.
471 signals[COPY_CLIPBOARD] =
472 g_signal_new (I_("copy-clipboard"),
473 G_OBJECT_CLASS_TYPE (gobject_class),
474 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
475 G_STRUCT_OFFSET (GtkLabelClass, copy_clipboard),
477 _gtk_marshal_VOID__VOID,
481 * GtkLabel::populate-popup:
482 * @label: The label on which the signal is emitted
483 * @menu: the menu that is being populated
485 * The ::populate-popup signal gets emitted before showing the
486 * context menu of the label. Note that only selectable labels
487 * have context menus.
489 * If you need to add items to the context menu, connect
490 * to this signal and append your menuitems to the @menu.
492 signals[POPULATE_POPUP] =
493 g_signal_new (I_("populate-popup"),
494 G_OBJECT_CLASS_TYPE (gobject_class),
496 G_STRUCT_OFFSET (GtkLabelClass, populate_popup),
498 _gtk_marshal_VOID__OBJECT,
503 * GtkLabel::activate-current-link:
504 * @label: The label on which the signal was emitted
506 * A <link linkend="keybinding-signals">keybinding signal</link>
507 * which gets emitted when the user activates a link in the label.
509 * Applications may also emit the signal with g_signal_emit_by_name()
510 * if they need to control activation of URIs programmatically.
512 * The default bindings for this signal are all forms of the Enter key.
516 signals[ACTIVATE_CURRENT_LINK] =
517 g_signal_new_class_handler ("activate-current-link",
518 G_TYPE_FROM_CLASS (object_class),
519 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
520 G_CALLBACK (gtk_label_activate_current_link),
522 _gtk_marshal_VOID__VOID,
526 * GtkLabel::activate-link:
527 * @label: The label on which the signal was emitted
528 * @uri: the URI that is activated
530 * The signal which gets emitted to activate a URI.
531 * Applications may connect to it to override the default behaviour,
532 * which is to call gtk_show_uri().
534 * Returns: %TRUE if the link has been activated
538 signals[ACTIVATE_LINK] =
539 g_signal_new ("activate-link",
540 G_TYPE_FROM_CLASS (object_class),
542 G_STRUCT_OFFSET (GtkLabelClass, activate_link),
543 _gtk_boolean_handled_accumulator, NULL,
544 _gtk_marshal_BOOLEAN__STRING,
545 G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
547 g_object_class_install_property (gobject_class,
549 g_param_spec_string ("label",
551 P_("The text of the label"),
553 GTK_PARAM_READWRITE));
554 g_object_class_install_property (gobject_class,
556 g_param_spec_boxed ("attributes",
558 P_("A list of style attributes to apply to the text of the label"),
559 PANGO_TYPE_ATTR_LIST,
560 GTK_PARAM_READWRITE));
561 g_object_class_install_property (gobject_class,
563 g_param_spec_boolean ("use-markup",
565 P_("The text of the label includes XML markup. See pango_parse_markup()"),
567 GTK_PARAM_READWRITE));
568 g_object_class_install_property (gobject_class,
570 g_param_spec_boolean ("use-underline",
572 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
574 GTK_PARAM_READWRITE));
576 g_object_class_install_property (gobject_class,
578 g_param_spec_enum ("justify",
580 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"),
581 GTK_TYPE_JUSTIFICATION,
583 GTK_PARAM_READWRITE));
585 g_object_class_install_property (gobject_class,
587 g_param_spec_string ("pattern",
589 P_("A string with _ characters in positions correspond to characters in the text to underline"),
591 GTK_PARAM_WRITABLE));
593 g_object_class_install_property (gobject_class,
595 g_param_spec_boolean ("wrap",
597 P_("If set, wrap lines if the text becomes too wide"),
599 GTK_PARAM_READWRITE));
601 * GtkLabel:wrap-mode:
603 * If line wrapping is on (see the #GtkLabel:wrap property) this controls
604 * how the line wrapping is done. The default is %PANGO_WRAP_WORD, which
605 * means wrap on word boundaries.
609 g_object_class_install_property (gobject_class,
611 g_param_spec_enum ("wrap-mode",
612 P_("Line wrap mode"),
613 P_("If wrap is set, controls how linewrapping is done"),
614 PANGO_TYPE_WRAP_MODE,
616 GTK_PARAM_READWRITE));
617 g_object_class_install_property (gobject_class,
619 g_param_spec_boolean ("selectable",
621 P_("Whether the label text can be selected with the mouse"),
623 GTK_PARAM_READWRITE));
624 g_object_class_install_property (gobject_class,
625 PROP_MNEMONIC_KEYVAL,
626 g_param_spec_uint ("mnemonic-keyval",
628 P_("The mnemonic accelerator key for this label"),
632 GTK_PARAM_READABLE));
633 g_object_class_install_property (gobject_class,
634 PROP_MNEMONIC_WIDGET,
635 g_param_spec_object ("mnemonic-widget",
636 P_("Mnemonic widget"),
637 P_("The widget to be activated when the label's mnemonic "
640 GTK_PARAM_READWRITE));
642 g_object_class_install_property (gobject_class,
643 PROP_CURSOR_POSITION,
644 g_param_spec_int ("cursor-position",
645 P_("Cursor Position"),
646 P_("The current position of the insertion cursor in chars"),
650 GTK_PARAM_READABLE));
652 g_object_class_install_property (gobject_class,
653 PROP_SELECTION_BOUND,
654 g_param_spec_int ("selection-bound",
655 P_("Selection Bound"),
656 P_("The position of the opposite end of the selection from the cursor in chars"),
660 GTK_PARAM_READABLE));
663 * GtkLabel:ellipsize:
665 * The preferred place to ellipsize the string, if the label does
666 * not have enough room to display the entire string, specified as a
667 * #PangoEllisizeMode.
669 * Note that setting this property to a value other than
670 * %PANGO_ELLIPSIZE_NONE has the side-effect that the label requests
671 * only enough space to display the ellipsis "...". In particular, this
672 * means that ellipsizing labels do not work well in notebook tabs, unless
673 * the tab's #GtkNotebook:tab-expand property is set to %TRUE. Other ways
674 * to set a label's width are gtk_widget_set_size_request() and
675 * gtk_label_set_width_chars().
679 g_object_class_install_property (gobject_class,
681 g_param_spec_enum ("ellipsize",
683 P_("The preferred place to ellipsize the string, if the label does not have enough room to display the entire string"),
684 PANGO_TYPE_ELLIPSIZE_MODE,
685 PANGO_ELLIPSIZE_NONE,
686 GTK_PARAM_READWRITE));
689 * GtkLabel:width-chars:
691 * The desired width of the label, in characters. If this property is set to
692 * -1, the width will be calculated automatically.
694 * See the section on <link linkend="label-text-layout">text layout</link>
695 * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
696 * determine the width of ellipsized and wrapped labels.
700 g_object_class_install_property (gobject_class,
702 g_param_spec_int ("width-chars",
703 P_("Width In Characters"),
704 P_("The desired width of the label, in characters"),
708 GTK_PARAM_READWRITE));
711 * GtkLabel:single-line-mode:
713 * Whether the label is in single line mode. In single line mode,
714 * the height of the label does not depend on the actual text, it
715 * is always set to ascent + descent of the font. This can be an
716 * advantage in situations where resizing the label because of text
717 * changes would be distracting, e.g. in a statusbar.
721 g_object_class_install_property (gobject_class,
722 PROP_SINGLE_LINE_MODE,
723 g_param_spec_boolean ("single-line-mode",
724 P_("Single Line Mode"),
725 P_("Whether the label is in single line mode"),
727 GTK_PARAM_READWRITE));
732 * The angle that the baseline of the label makes with the horizontal,
733 * in degrees, measured counterclockwise. An angle of 90 reads from
734 * from bottom to top, an angle of 270, from top to bottom. Ignored
735 * if the label is selectable, wrapped, or ellipsized.
739 g_object_class_install_property (gobject_class,
741 g_param_spec_double ("angle",
743 P_("Angle at which the label is rotated"),
747 GTK_PARAM_READWRITE));
750 * GtkLabel:max-width-chars:
752 * The desired maximum width of the label, in characters. If this property
753 * is set to -1, the width will be calculated automatically.
755 * See the section on <link linkend="label-text-layout">text layout</link>
756 * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
757 * determine the width of ellipsized and wrapped labels.
761 g_object_class_install_property (gobject_class,
762 PROP_MAX_WIDTH_CHARS,
763 g_param_spec_int ("max-width-chars",
764 P_("Maximum Width In Characters"),
765 P_("The desired maximum width of the label, in characters"),
769 GTK_PARAM_READWRITE));
772 * GtkLabel:track-visited-links:
774 * Set this property to %TRUE to make the label track which links
775 * have been clicked. It will then apply the ::visited-link-color
776 * color, instead of ::link-color.
780 g_object_class_install_property (gobject_class,
781 PROP_TRACK_VISITED_LINKS,
782 g_param_spec_boolean ("track-visited-links",
783 P_("Track visited links"),
784 P_("Whether visited links should be tracked"),
786 GTK_PARAM_READWRITE));
791 binding_set = gtk_binding_set_by_class (class);
793 /* Moving the insertion point */
794 add_move_binding (binding_set, GDK_Right, 0,
795 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
797 add_move_binding (binding_set, GDK_Left, 0,
798 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
800 add_move_binding (binding_set, GDK_KP_Right, 0,
801 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
803 add_move_binding (binding_set, GDK_KP_Left, 0,
804 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
806 add_move_binding (binding_set, GDK_f, GDK_CONTROL_MASK,
807 GTK_MOVEMENT_LOGICAL_POSITIONS, 1);
809 add_move_binding (binding_set, GDK_b, GDK_CONTROL_MASK,
810 GTK_MOVEMENT_LOGICAL_POSITIONS, -1);
812 add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
813 GTK_MOVEMENT_WORDS, 1);
815 add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
816 GTK_MOVEMENT_WORDS, -1);
818 add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
819 GTK_MOVEMENT_WORDS, 1);
821 add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
822 GTK_MOVEMENT_WORDS, -1);
825 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
827 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
829 G_TYPE_BOOLEAN, FALSE);
831 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
833 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
835 G_TYPE_BOOLEAN, TRUE);
837 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
839 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
841 G_TYPE_BOOLEAN, FALSE);
843 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
845 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
847 G_TYPE_BOOLEAN, TRUE);
850 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
852 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
854 G_TYPE_BOOLEAN, FALSE);
856 gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_CONTROL_MASK,
858 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
860 G_TYPE_BOOLEAN, FALSE);
862 add_move_binding (binding_set, GDK_f, GDK_MOD1_MASK,
863 GTK_MOVEMENT_WORDS, 1);
865 add_move_binding (binding_set, GDK_b, GDK_MOD1_MASK,
866 GTK_MOVEMENT_WORDS, -1);
868 add_move_binding (binding_set, GDK_Home, 0,
869 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
871 add_move_binding (binding_set, GDK_End, 0,
872 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
874 add_move_binding (binding_set, GDK_KP_Home, 0,
875 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
877 add_move_binding (binding_set, GDK_KP_End, 0,
878 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
880 add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
881 GTK_MOVEMENT_BUFFER_ENDS, -1);
883 add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
884 GTK_MOVEMENT_BUFFER_ENDS, 1);
886 add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
887 GTK_MOVEMENT_BUFFER_ENDS, -1);
889 add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
890 GTK_MOVEMENT_BUFFER_ENDS, 1);
893 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
894 "copy-clipboard", 0);
896 gtk_binding_entry_add_signal (binding_set, GDK_Return, 0,
897 "activate-current-link", 0);
898 gtk_binding_entry_add_signal (binding_set, GDK_ISO_Enter, 0,
899 "activate-current-link", 0);
900 gtk_binding_entry_add_signal (binding_set, GDK_KP_Enter, 0,
901 "activate-current-link", 0);
903 gtk_settings_install_property (g_param_spec_boolean ("gtk-label-select-on-focus",
904 P_("Select on focus"),
905 P_("Whether to select the contents of a selectable label when it is focused"),
907 GTK_PARAM_READWRITE));
909 g_type_class_add_private (class, sizeof (GtkLabelPriv));
913 gtk_label_set_property (GObject *object,
918 GtkLabel *label = GTK_LABEL (object);
923 gtk_label_set_label (label, g_value_get_string (value));
925 case PROP_ATTRIBUTES:
926 gtk_label_set_attributes (label, g_value_get_boxed (value));
928 case PROP_USE_MARKUP:
929 gtk_label_set_use_markup (label, g_value_get_boolean (value));
931 case PROP_USE_UNDERLINE:
932 gtk_label_set_use_underline (label, g_value_get_boolean (value));
935 gtk_label_set_justify (label, g_value_get_enum (value));
938 gtk_label_set_pattern (label, g_value_get_string (value));
941 gtk_label_set_line_wrap (label, g_value_get_boolean (value));
944 gtk_label_set_line_wrap_mode (label, g_value_get_enum (value));
946 case PROP_SELECTABLE:
947 gtk_label_set_selectable (label, g_value_get_boolean (value));
949 case PROP_MNEMONIC_WIDGET:
950 gtk_label_set_mnemonic_widget (label, (GtkWidget*) g_value_get_object (value));
953 gtk_label_set_ellipsize (label, g_value_get_enum (value));
955 case PROP_WIDTH_CHARS:
956 gtk_label_set_width_chars (label, g_value_get_int (value));
958 case PROP_SINGLE_LINE_MODE:
959 gtk_label_set_single_line_mode (label, g_value_get_boolean (value));
962 gtk_label_set_angle (label, g_value_get_double (value));
964 case PROP_MAX_WIDTH_CHARS:
965 gtk_label_set_max_width_chars (label, g_value_get_int (value));
967 case PROP_TRACK_VISITED_LINKS:
968 gtk_label_set_track_visited_links (label, g_value_get_boolean (value));
971 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
977 gtk_label_get_property (GObject *object,
982 GtkLabel *label = GTK_LABEL (object);
983 GtkLabelPriv *priv = label->priv;
988 g_value_set_string (value, priv->label);
990 case PROP_ATTRIBUTES:
991 g_value_set_boxed (value, priv->attrs);
993 case PROP_USE_MARKUP:
994 g_value_set_boolean (value, priv->use_markup);
996 case PROP_USE_UNDERLINE:
997 g_value_set_boolean (value, priv->use_underline);
1000 g_value_set_enum (value, priv->jtype);
1003 g_value_set_boolean (value, priv->wrap);
1005 case PROP_WRAP_MODE:
1006 g_value_set_enum (value, priv->wrap_mode);
1008 case PROP_SELECTABLE:
1009 g_value_set_boolean (value, gtk_label_get_selectable (label));
1011 case PROP_MNEMONIC_KEYVAL:
1012 g_value_set_uint (value, priv->mnemonic_keyval);
1014 case PROP_MNEMONIC_WIDGET:
1015 g_value_set_object (value, (GObject*) priv->mnemonic_widget);
1017 case PROP_CURSOR_POSITION:
1018 if (priv->select_info && priv->select_info->selectable)
1020 gint offset = g_utf8_pointer_to_offset (priv->text,
1021 priv->text + priv->select_info->selection_end);
1022 g_value_set_int (value, offset);
1025 g_value_set_int (value, 0);
1027 case PROP_SELECTION_BOUND:
1028 if (priv->select_info && priv->select_info->selectable)
1030 gint offset = g_utf8_pointer_to_offset (priv->text,
1031 priv->text + priv->select_info->selection_anchor);
1032 g_value_set_int (value, offset);
1035 g_value_set_int (value, 0);
1037 case PROP_ELLIPSIZE:
1038 g_value_set_enum (value, priv->ellipsize);
1040 case PROP_WIDTH_CHARS:
1041 g_value_set_int (value, gtk_label_get_width_chars (label));
1043 case PROP_SINGLE_LINE_MODE:
1044 g_value_set_boolean (value, gtk_label_get_single_line_mode (label));
1047 g_value_set_double (value, gtk_label_get_angle (label));
1049 case PROP_MAX_WIDTH_CHARS:
1050 g_value_set_int (value, gtk_label_get_max_width_chars (label));
1052 case PROP_TRACK_VISITED_LINKS:
1053 g_value_set_boolean (value, gtk_label_get_track_visited_links (label));
1056 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1062 gtk_label_init (GtkLabel *label)
1066 label->priv = G_TYPE_INSTANCE_GET_PRIVATE (label,
1071 gtk_widget_set_has_window (GTK_WIDGET (label), FALSE);
1073 priv->width_chars = -1;
1074 priv->max_width_chars = -1;
1075 priv->wrap_width = -1;
1078 priv->jtype = GTK_JUSTIFY_LEFT;
1080 priv->wrap_mode = PANGO_WRAP_WORD;
1081 priv->ellipsize = PANGO_ELLIPSIZE_NONE;
1083 priv->use_underline = FALSE;
1084 priv->use_markup = FALSE;
1085 priv->pattern_set = FALSE;
1086 priv->track_links = TRUE;
1088 priv->mnemonic_keyval = GDK_VoidSymbol;
1089 priv->layout = NULL;
1093 priv->mnemonic_widget = NULL;
1094 priv->mnemonic_window = NULL;
1096 priv->mnemonics_visible = TRUE;
1098 gtk_label_set_text (label, "");
1103 gtk_label_buildable_interface_init (GtkBuildableIface *iface)
1105 buildable_parent_iface = g_type_interface_peek_parent (iface);
1107 iface->custom_tag_start = gtk_label_buildable_custom_tag_start;
1108 iface->custom_finished = gtk_label_buildable_custom_finished;
1112 GtkBuilder *builder;
1114 PangoAttrList *attrs;
1117 static PangoAttribute *
1118 attribute_from_text (GtkBuilder *builder,
1123 PangoAttribute *attribute = NULL;
1125 PangoLanguage *language;
1126 PangoFontDescription *font_desc;
1128 GValue val = { 0, };
1130 if (!gtk_builder_value_from_string_type (builder, PANGO_TYPE_ATTR_TYPE, name, &val, error))
1133 type = g_value_get_enum (&val);
1134 g_value_unset (&val);
1138 /* PangoAttrLanguage */
1139 case PANGO_ATTR_LANGUAGE:
1140 if ((language = pango_language_from_string (value)))
1142 attribute = pango_attr_language_new (language);
1143 g_value_init (&val, G_TYPE_INT);
1147 case PANGO_ATTR_STYLE:
1148 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STYLE, value, &val, error))
1149 attribute = pango_attr_style_new (g_value_get_enum (&val));
1151 case PANGO_ATTR_WEIGHT:
1152 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_WEIGHT, value, &val, error))
1153 attribute = pango_attr_weight_new (g_value_get_enum (&val));
1155 case PANGO_ATTR_VARIANT:
1156 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_VARIANT, value, &val, error))
1157 attribute = pango_attr_variant_new (g_value_get_enum (&val));
1159 case PANGO_ATTR_STRETCH:
1160 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STRETCH, value, &val, error))
1161 attribute = pango_attr_stretch_new (g_value_get_enum (&val));
1163 case PANGO_ATTR_UNDERLINE:
1164 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1165 attribute = pango_attr_underline_new (g_value_get_boolean (&val));
1167 case PANGO_ATTR_STRIKETHROUGH:
1168 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1169 attribute = pango_attr_strikethrough_new (g_value_get_boolean (&val));
1171 case PANGO_ATTR_GRAVITY:
1172 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY, value, &val, error))
1173 attribute = pango_attr_gravity_new (g_value_get_enum (&val));
1175 case PANGO_ATTR_GRAVITY_HINT:
1176 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY_HINT,
1177 value, &val, error))
1178 attribute = pango_attr_gravity_hint_new (g_value_get_enum (&val));
1180 /* PangoAttrString */
1181 case PANGO_ATTR_FAMILY:
1182 attribute = pango_attr_family_new (value);
1183 g_value_init (&val, G_TYPE_INT);
1187 case PANGO_ATTR_SIZE:
1188 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1189 value, &val, error))
1190 attribute = pango_attr_size_new (g_value_get_int (&val));
1192 case PANGO_ATTR_ABSOLUTE_SIZE:
1193 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1194 value, &val, error))
1195 attribute = pango_attr_size_new_absolute (g_value_get_int (&val));
1198 /* PangoAttrFontDesc */
1199 case PANGO_ATTR_FONT_DESC:
1200 if ((font_desc = pango_font_description_from_string (value)))
1202 attribute = pango_attr_font_desc_new (font_desc);
1203 pango_font_description_free (font_desc);
1204 g_value_init (&val, G_TYPE_INT);
1208 /* PangoAttrColor */
1209 case PANGO_ATTR_FOREGROUND:
1210 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1211 value, &val, error))
1213 color = g_value_get_boxed (&val);
1214 attribute = pango_attr_foreground_new (color->red, color->green, color->blue);
1217 case PANGO_ATTR_BACKGROUND:
1218 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1219 value, &val, error))
1221 color = g_value_get_boxed (&val);
1222 attribute = pango_attr_background_new (color->red, color->green, color->blue);
1225 case PANGO_ATTR_UNDERLINE_COLOR:
1226 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1227 value, &val, error))
1229 color = g_value_get_boxed (&val);
1230 attribute = pango_attr_underline_color_new (color->red, color->green, color->blue);
1233 case PANGO_ATTR_STRIKETHROUGH_COLOR:
1234 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1235 value, &val, error))
1237 color = g_value_get_boxed (&val);
1238 attribute = pango_attr_strikethrough_color_new (color->red, color->green, color->blue);
1242 /* PangoAttrShape */
1243 case PANGO_ATTR_SHAPE:
1244 /* Unsupported for now */
1246 /* PangoAttrFloat */
1247 case PANGO_ATTR_SCALE:
1248 if (gtk_builder_value_from_string_type (builder, G_TYPE_DOUBLE,
1249 value, &val, error))
1250 attribute = pango_attr_scale_new (g_value_get_double (&val));
1253 case PANGO_ATTR_INVALID:
1254 case PANGO_ATTR_LETTER_SPACING:
1255 case PANGO_ATTR_RISE:
1256 case PANGO_ATTR_FALLBACK:
1261 g_value_unset (&val);
1268 pango_start_element (GMarkupParseContext *context,
1269 const gchar *element_name,
1270 const gchar **names,
1271 const gchar **values,
1275 PangoParserData *data = (PangoParserData*)user_data;
1276 GValue val = { 0, };
1278 gint line_number, char_number;
1280 if (strcmp (element_name, "attribute") == 0)
1282 PangoAttribute *attr = NULL;
1283 const gchar *name = NULL;
1284 const gchar *value = NULL;
1285 const gchar *start = NULL;
1286 const gchar *end = NULL;
1287 guint start_val = 0;
1288 guint end_val = G_MAXUINT;
1290 for (i = 0; names[i]; i++)
1292 if (strcmp (names[i], "name") == 0)
1294 else if (strcmp (names[i], "value") == 0)
1296 else if (strcmp (names[i], "start") == 0)
1298 else if (strcmp (names[i], "end") == 0)
1302 g_markup_parse_context_get_position (context,
1307 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1308 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1310 line_number, char_number, names[i], "attribute");
1315 if (!name || !value)
1317 g_markup_parse_context_get_position (context,
1322 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1323 "%s:%d:%d <%s> requires attribute \"%s\"",
1325 line_number, char_number, "attribute",
1326 name ? "value" : "name");
1332 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1333 start, &val, error))
1335 start_val = g_value_get_uint (&val);
1336 g_value_unset (&val);
1341 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1344 end_val = g_value_get_uint (&val);
1345 g_value_unset (&val);
1348 attr = attribute_from_text (data->builder, name, value, error);
1349 attr->start_index = start_val;
1350 attr->end_index = end_val;
1355 data->attrs = pango_attr_list_new ();
1357 pango_attr_list_insert (data->attrs, attr);
1360 else if (strcmp (element_name, "attributes") == 0)
1363 g_warning ("Unsupported tag for GtkLabel: %s\n", element_name);
1366 static const GMarkupParser pango_parser =
1368 pango_start_element,
1372 gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
1373 GtkBuilder *builder,
1375 const gchar *tagname,
1376 GMarkupParser *parser,
1379 if (buildable_parent_iface->custom_tag_start (buildable, builder, child,
1380 tagname, parser, data))
1383 if (strcmp (tagname, "attributes") == 0)
1385 PangoParserData *parser_data;
1387 parser_data = g_slice_new0 (PangoParserData);
1388 parser_data->builder = g_object_ref (builder);
1389 parser_data->object = g_object_ref (buildable);
1390 *parser = pango_parser;
1391 *data = parser_data;
1398 gtk_label_buildable_custom_finished (GtkBuildable *buildable,
1399 GtkBuilder *builder,
1401 const gchar *tagname,
1404 PangoParserData *data;
1406 buildable_parent_iface->custom_finished (buildable, builder, child,
1407 tagname, user_data);
1409 if (strcmp (tagname, "attributes") == 0)
1411 data = (PangoParserData*)user_data;
1415 gtk_label_set_attributes (GTK_LABEL (buildable), data->attrs);
1416 pango_attr_list_unref (data->attrs);
1419 g_object_unref (data->object);
1420 g_object_unref (data->builder);
1421 g_slice_free (PangoParserData, data);
1428 * @str: The text of the label
1430 * Creates a new label with the given text inside it. You can
1431 * pass %NULL to get an empty label widget.
1433 * Return value: the new #GtkLabel
1436 gtk_label_new (const gchar *str)
1440 label = g_object_new (GTK_TYPE_LABEL, NULL);
1443 gtk_label_set_text (label, str);
1445 return GTK_WIDGET (label);
1449 * gtk_label_new_with_mnemonic:
1450 * @str: The text of the label, with an underscore in front of the
1451 * mnemonic character
1453 * Creates a new #GtkLabel, containing the text in @str.
1455 * If characters in @str are preceded by an underscore, they are
1456 * underlined. If you need a literal underscore character in a label, use
1457 * '__' (two underscores). The first underlined character represents a
1458 * keyboard accelerator called a mnemonic. The mnemonic key can be used
1459 * to activate another widget, chosen automatically, or explicitly using
1460 * gtk_label_set_mnemonic_widget().
1462 * If gtk_label_set_mnemonic_widget() is not called, then the first
1463 * activatable ancestor of the #GtkLabel will be chosen as the mnemonic
1464 * widget. For instance, if the label is inside a button or menu item,
1465 * the button or menu item will automatically become the mnemonic widget
1466 * and be activated by the mnemonic.
1468 * Return value: the new #GtkLabel
1471 gtk_label_new_with_mnemonic (const gchar *str)
1475 label = g_object_new (GTK_TYPE_LABEL, NULL);
1478 gtk_label_set_text_with_mnemonic (label, str);
1480 return GTK_WIDGET (label);
1484 gtk_label_mnemonic_activate (GtkWidget *widget,
1485 gboolean group_cycling)
1487 GtkLabel *label = GTK_LABEL (widget);
1488 GtkLabelPriv *priv = label->priv;
1491 if (priv->mnemonic_widget)
1492 return gtk_widget_mnemonic_activate (priv->mnemonic_widget, group_cycling);
1494 /* Try to find the widget to activate by traversing the
1495 * widget's ancestry.
1497 parent = gtk_widget_get_parent (widget);
1499 if (GTK_IS_NOTEBOOK (parent))
1504 if (gtk_widget_get_can_focus (parent) ||
1505 (!group_cycling && GTK_WIDGET_GET_CLASS (parent)->activate_signal) ||
1506 GTK_IS_NOTEBOOK (gtk_widget_get_parent (parent)) ||
1507 GTK_IS_MENU_ITEM (parent))
1508 return gtk_widget_mnemonic_activate (parent, group_cycling);
1509 parent = gtk_widget_get_parent (parent);
1512 /* barf if there was nothing to activate */
1513 g_warning ("Couldn't find a target for a mnemonic activation.");
1514 gtk_widget_error_bell (widget);
1520 gtk_label_setup_mnemonic (GtkLabel *label,
1523 GtkLabelPriv *priv = label->priv;
1524 GtkWidget *widget = GTK_WIDGET (label);
1525 GtkWidget *toplevel;
1526 GtkWidget *mnemonic_menu;
1528 mnemonic_menu = g_object_get_data (G_OBJECT (label), "gtk-mnemonic-menu");
1530 if (last_key != GDK_VoidSymbol)
1532 if (priv->mnemonic_window)
1534 gtk_window_remove_mnemonic (priv->mnemonic_window,
1537 priv->mnemonic_window = NULL;
1541 _gtk_menu_shell_remove_mnemonic (GTK_MENU_SHELL (mnemonic_menu),
1544 mnemonic_menu = NULL;
1548 if (priv->mnemonic_keyval == GDK_VoidSymbol)
1551 connect_mnemonics_visible_notify (GTK_LABEL (widget));
1553 toplevel = gtk_widget_get_toplevel (widget);
1554 if (gtk_widget_is_toplevel (toplevel))
1556 GtkWidget *menu_shell;
1558 menu_shell = gtk_widget_get_ancestor (widget,
1559 GTK_TYPE_MENU_SHELL);
1563 _gtk_menu_shell_add_mnemonic (GTK_MENU_SHELL (menu_shell),
1564 priv->mnemonic_keyval,
1566 mnemonic_menu = menu_shell;
1569 if (!GTK_IS_MENU (menu_shell))
1571 gtk_window_add_mnemonic (GTK_WINDOW (toplevel),
1572 priv->mnemonic_keyval,
1574 priv->mnemonic_window = GTK_WINDOW (toplevel);
1579 g_object_set_data (G_OBJECT (label), I_("gtk-mnemonic-menu"), mnemonic_menu);
1583 gtk_label_hierarchy_changed (GtkWidget *widget,
1584 GtkWidget *old_toplevel)
1586 GtkLabel *label = GTK_LABEL (widget);
1587 GtkLabelPriv *priv = label->priv;
1589 gtk_label_setup_mnemonic (label, priv->mnemonic_keyval);
1593 label_shortcut_setting_apply (GtkLabel *label)
1595 gtk_label_recalculate (label);
1596 if (GTK_IS_ACCEL_LABEL (label))
1597 gtk_accel_label_refetch (GTK_ACCEL_LABEL (label));
1601 label_shortcut_setting_traverse_container (GtkWidget *widget,
1604 if (GTK_IS_LABEL (widget))
1605 label_shortcut_setting_apply (GTK_LABEL (widget));
1606 else if (GTK_IS_CONTAINER (widget))
1607 gtk_container_forall (GTK_CONTAINER (widget),
1608 label_shortcut_setting_traverse_container, data);
1612 label_shortcut_setting_changed (GtkSettings *settings)
1616 list = gtk_window_list_toplevels ();
1618 for (l = list; l ; l = l->next)
1620 GtkWidget *widget = l->data;
1622 if (gtk_widget_get_settings (widget) == settings)
1623 gtk_container_forall (GTK_CONTAINER (widget),
1624 label_shortcut_setting_traverse_container, NULL);
1631 mnemonics_visible_apply (GtkWidget *widget,
1632 gboolean mnemonics_visible)
1634 GtkLabel *label = GTK_LABEL (widget);
1635 GtkLabelPriv *priv = label->priv;
1637 mnemonics_visible = mnemonics_visible != FALSE;
1639 if (priv->mnemonics_visible != mnemonics_visible)
1641 priv->mnemonics_visible = mnemonics_visible;
1643 gtk_label_recalculate (label);
1648 label_mnemonics_visible_traverse_container (GtkWidget *widget,
1651 gboolean mnemonics_visible = GPOINTER_TO_INT (data);
1653 _gtk_label_mnemonics_visible_apply_recursively (widget, mnemonics_visible);
1657 _gtk_label_mnemonics_visible_apply_recursively (GtkWidget *widget,
1658 gboolean mnemonics_visible)
1660 if (GTK_IS_LABEL (widget))
1661 mnemonics_visible_apply (widget, mnemonics_visible);
1662 else if (GTK_IS_CONTAINER (widget))
1663 gtk_container_forall (GTK_CONTAINER (widget),
1664 label_mnemonics_visible_traverse_container,
1665 GINT_TO_POINTER (mnemonics_visible));
1669 label_mnemonics_visible_changed (GtkWindow *window,
1673 gboolean mnemonics_visible;
1675 g_object_get (window, "mnemonics-visible", &mnemonics_visible, NULL);
1677 gtk_container_forall (GTK_CONTAINER (window),
1678 label_mnemonics_visible_traverse_container,
1679 GINT_TO_POINTER (mnemonics_visible));
1683 gtk_label_screen_changed (GtkWidget *widget,
1684 GdkScreen *old_screen)
1686 GtkSettings *settings;
1687 gboolean shortcuts_connected;
1689 if (!gtk_widget_has_screen (widget))
1692 settings = gtk_widget_get_settings (widget);
1694 shortcuts_connected =
1695 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (settings),
1696 "gtk-label-shortcuts-connected"));
1698 if (! shortcuts_connected)
1700 g_signal_connect (settings, "notify::gtk-enable-mnemonics",
1701 G_CALLBACK (label_shortcut_setting_changed),
1703 g_signal_connect (settings, "notify::gtk-enable-accels",
1704 G_CALLBACK (label_shortcut_setting_changed),
1707 g_object_set_data (G_OBJECT (settings), "gtk-label-shortcuts-connected",
1708 GINT_TO_POINTER (TRUE));
1711 label_shortcut_setting_apply (GTK_LABEL (widget));
1716 label_mnemonic_widget_weak_notify (gpointer data,
1717 GObject *where_the_object_was)
1719 GtkLabel *label = data;
1720 GtkLabelPriv *priv = label->priv;
1722 priv->mnemonic_widget = NULL;
1723 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1727 * gtk_label_set_mnemonic_widget:
1728 * @label: a #GtkLabel
1729 * @widget: (allow-none): the target #GtkWidget
1731 * If the label has been set so that it has an mnemonic key (using
1732 * i.e. gtk_label_set_markup_with_mnemonic(),
1733 * gtk_label_set_text_with_mnemonic(), gtk_label_new_with_mnemonic()
1734 * or the "use_underline" property) the label can be associated with a
1735 * widget that is the target of the mnemonic. When the label is inside
1736 * a widget (like a #GtkButton or a #GtkNotebook tab) it is
1737 * automatically associated with the correct widget, but sometimes
1738 * (i.e. when the target is a #GtkEntry next to the label) you need to
1739 * set it explicitly using this function.
1741 * The target widget will be accelerated by emitting the
1742 * GtkWidget::mnemonic-activate signal on it. The default handler for
1743 * this signal will activate the widget if there are no mnemonic collisions
1744 * and toggle focus between the colliding widgets otherwise.
1747 gtk_label_set_mnemonic_widget (GtkLabel *label,
1752 g_return_if_fail (GTK_IS_LABEL (label));
1757 g_return_if_fail (GTK_IS_WIDGET (widget));
1759 if (priv->mnemonic_widget)
1761 gtk_widget_remove_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1762 g_object_weak_unref (G_OBJECT (priv->mnemonic_widget),
1763 label_mnemonic_widget_weak_notify,
1766 priv->mnemonic_widget = widget;
1767 if (priv->mnemonic_widget)
1769 g_object_weak_ref (G_OBJECT (priv->mnemonic_widget),
1770 label_mnemonic_widget_weak_notify,
1772 gtk_widget_add_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1775 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1779 * gtk_label_get_mnemonic_widget:
1780 * @label: a #GtkLabel
1782 * Retrieves the target of the mnemonic (keyboard shortcut) of this
1783 * label. See gtk_label_set_mnemonic_widget().
1785 * Return value: the target of the label's mnemonic, or %NULL if none
1786 * has been set and the default algorithm will be used.
1789 gtk_label_get_mnemonic_widget (GtkLabel *label)
1791 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1793 return label->priv->mnemonic_widget;
1797 * gtk_label_get_mnemonic_keyval:
1798 * @label: a #GtkLabel
1800 * If the label has been set so that it has an mnemonic key this function
1801 * returns the keyval used for the mnemonic accelerator. If there is no
1802 * mnemonic set up it returns #GDK_VoidSymbol.
1804 * Returns: GDK keyval usable for accelerators, or #GDK_VoidSymbol
1807 gtk_label_get_mnemonic_keyval (GtkLabel *label)
1809 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_VoidSymbol);
1811 return label->priv->mnemonic_keyval;
1815 gtk_label_set_text_internal (GtkLabel *label,
1818 GtkLabelPriv *priv = label->priv;
1820 g_free (priv->text);
1824 gtk_label_select_region_index (label, 0, 0);
1828 gtk_label_set_label_internal (GtkLabel *label,
1831 GtkLabelPriv *priv = label->priv;
1833 g_free (priv->label);
1837 g_object_notify (G_OBJECT (label), "label");
1841 gtk_label_set_use_markup_internal (GtkLabel *label,
1844 GtkLabelPriv *priv = label->priv;
1847 if (priv->use_markup != val)
1849 priv->use_markup = val;
1851 g_object_notify (G_OBJECT (label), "use-markup");
1856 gtk_label_set_use_underline_internal (GtkLabel *label,
1859 GtkLabelPriv *priv = label->priv;
1862 if (priv->use_underline != val)
1864 priv->use_underline = val;
1866 g_object_notify (G_OBJECT (label), "use-underline");
1871 gtk_label_compose_effective_attrs (GtkLabel *label)
1873 GtkLabelPriv *priv = label->priv;
1874 PangoAttrIterator *iter;
1875 PangoAttribute *attr;
1876 GSList *iter_attrs, *l;
1880 if (priv->effective_attrs)
1882 if ((iter = pango_attr_list_get_iterator (priv->attrs)))
1886 iter_attrs = pango_attr_iterator_get_attrs (iter);
1887 for (l = iter_attrs; l; l = l->next)
1890 pango_attr_list_insert (priv->effective_attrs, attr);
1892 g_slist_free (iter_attrs);
1894 while (pango_attr_iterator_next (iter));
1895 pango_attr_iterator_destroy (iter);
1899 priv->effective_attrs =
1900 pango_attr_list_ref (priv->attrs);
1905 gtk_label_set_attributes_internal (GtkLabel *label,
1906 PangoAttrList *attrs)
1908 GtkLabelPriv *priv = label->priv;
1911 pango_attr_list_ref (attrs);
1914 pango_attr_list_unref (priv->attrs);
1915 priv->attrs = attrs;
1917 g_object_notify (G_OBJECT (label), "attributes");
1921 /* Calculates text, attrs and mnemonic_keyval from
1922 * label, use_underline and use_markup
1925 gtk_label_recalculate (GtkLabel *label)
1927 GtkLabelPriv *priv = label->priv;
1928 guint keyval = priv->mnemonic_keyval;
1930 if (priv->use_markup)
1931 gtk_label_set_markup_internal (label, priv->label, priv->use_underline);
1934 if (priv->use_underline)
1935 gtk_label_set_uline_text_internal (label, priv->label);
1938 if (priv->effective_attrs)
1939 pango_attr_list_unref (priv->effective_attrs);
1940 priv->effective_attrs = NULL;
1941 gtk_label_set_text_internal (label, g_strdup (priv->label));
1945 gtk_label_compose_effective_attrs (label);
1947 if (!priv->use_underline)
1948 priv->mnemonic_keyval = GDK_VoidSymbol;
1950 if (keyval != priv->mnemonic_keyval)
1952 gtk_label_setup_mnemonic (label, keyval);
1953 g_object_notify (G_OBJECT (label), "mnemonic-keyval");
1956 gtk_label_clear_layout (label);
1957 gtk_label_clear_select_info (label);
1958 gtk_widget_queue_resize (GTK_WIDGET (label));
1962 * gtk_label_set_text:
1963 * @label: a #GtkLabel
1964 * @str: The text you want to set
1966 * Sets the text within the #GtkLabel widget. It overwrites any text that
1969 * This will also clear any previously set mnemonic accelerators.
1972 gtk_label_set_text (GtkLabel *label,
1975 g_return_if_fail (GTK_IS_LABEL (label));
1977 g_object_freeze_notify (G_OBJECT (label));
1979 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
1980 gtk_label_set_use_markup_internal (label, FALSE);
1981 gtk_label_set_use_underline_internal (label, FALSE);
1983 gtk_label_recalculate (label);
1985 g_object_thaw_notify (G_OBJECT (label));
1989 * gtk_label_set_attributes:
1990 * @label: a #GtkLabel
1991 * @attrs: a #PangoAttrList
1993 * Sets a #PangoAttrList; the attributes in the list are applied to the
1996 * <note><para>The attributes set with this function will be applied
1997 * and merged with any other attributes previously effected by way
1998 * of the #GtkLabel:use-underline or #GtkLabel:use-markup properties.
1999 * While it is not recommended to mix markup strings with manually set
2000 * attributes, if you must; know that the attributes will be applied
2001 * to the label after the markup string is parsed.</para></note>
2004 gtk_label_set_attributes (GtkLabel *label,
2005 PangoAttrList *attrs)
2007 g_return_if_fail (GTK_IS_LABEL (label));
2009 gtk_label_set_attributes_internal (label, attrs);
2011 gtk_label_recalculate (label);
2013 gtk_label_clear_layout (label);
2014 gtk_widget_queue_resize (GTK_WIDGET (label));
2018 * gtk_label_get_attributes:
2019 * @label: a #GtkLabel
2021 * Gets the attribute list that was set on the label using
2022 * gtk_label_set_attributes(), if any. This function does
2023 * not reflect attributes that come from the labels markup
2024 * (see gtk_label_set_markup()). If you want to get the
2025 * effective attributes for the label, use
2026 * pango_layout_get_attribute (gtk_label_get_layout (label)).
2028 * Return value: the attribute list, or %NULL if none was set.
2031 gtk_label_get_attributes (GtkLabel *label)
2033 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2035 return label->priv->attrs;
2039 * gtk_label_set_label:
2040 * @label: a #GtkLabel
2041 * @str: the new text to set for the label
2043 * Sets the text of the label. The label is interpreted as
2044 * including embedded underlines and/or Pango markup depending
2045 * on the values of the #GtkLabel:use-underline" and
2046 * #GtkLabel:use-markup properties.
2049 gtk_label_set_label (GtkLabel *label,
2052 g_return_if_fail (GTK_IS_LABEL (label));
2054 g_object_freeze_notify (G_OBJECT (label));
2056 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2057 gtk_label_recalculate (label);
2059 g_object_thaw_notify (G_OBJECT (label));
2063 * gtk_label_get_label:
2064 * @label: a #GtkLabel
2066 * Fetches the text from a label widget including any embedded
2067 * underlines indicating mnemonics and Pango markup. (See
2068 * gtk_label_get_text()).
2070 * Return value: the text of the label widget. This string is
2071 * owned by the widget and must not be modified or freed.
2073 G_CONST_RETURN gchar *
2074 gtk_label_get_label (GtkLabel *label)
2076 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2078 return label->priv->label;
2086 GdkColor *link_color;
2087 GdkColor *visited_link_color;
2091 start_element_handler (GMarkupParseContext *context,
2092 const gchar *element_name,
2093 const gchar **attribute_names,
2094 const gchar **attribute_values,
2099 UriParserData *pdata = user_data;
2101 if (strcmp (element_name, "a") == 0)
2104 const gchar *uri = NULL;
2105 const gchar *title = NULL;
2106 gboolean visited = FALSE;
2110 GdkColor *color = NULL;
2112 g_markup_parse_context_get_position (context, &line_number, &char_number);
2114 for (i = 0; attribute_names[i] != NULL; i++)
2116 const gchar *attr = attribute_names[i];
2118 if (strcmp (attr, "href") == 0)
2119 uri = attribute_values[i];
2120 else if (strcmp (attr, "title") == 0)
2121 title = attribute_values[i];
2126 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
2127 "Attribute '%s' is not allowed on the <a> tag "
2128 "on line %d char %d",
2129 attr, line_number, char_number);
2138 G_MARKUP_ERROR_INVALID_CONTENT,
2139 "Attribute 'href' was missing on the <a> tag "
2140 "on line %d char %d",
2141 line_number, char_number);
2146 priv = pdata->label->priv;
2147 if (priv->track_links && priv->select_info)
2150 for (l = priv->select_info->links; l; l = l->next)
2153 if (strcmp (uri, link->uri) == 0)
2155 visited = link->visited;
2162 color = pdata->visited_link_color;
2164 color = pdata->link_color;
2166 g_string_append_printf (pdata->new_str,
2167 "<span color=\"#%04x%04x%04x\" underline=\"single\">",
2172 link = g_new0 (GtkLabelLink, 1);
2173 link->uri = g_strdup (uri);
2174 link->title = g_strdup (title);
2175 link->visited = visited;
2176 pdata->links = g_list_append (pdata->links, link);
2182 g_string_append_c (pdata->new_str, '<');
2183 g_string_append (pdata->new_str, element_name);
2185 for (i = 0; attribute_names[i] != NULL; i++)
2187 const gchar *attr = attribute_names[i];
2188 const gchar *value = attribute_values[i];
2191 newvalue = g_markup_escape_text (value, -1);
2193 g_string_append_c (pdata->new_str, ' ');
2194 g_string_append (pdata->new_str, attr);
2195 g_string_append (pdata->new_str, "=\"");
2196 g_string_append (pdata->new_str, newvalue);
2197 g_string_append_c (pdata->new_str, '\"');
2201 g_string_append_c (pdata->new_str, '>');
2206 end_element_handler (GMarkupParseContext *context,
2207 const gchar *element_name,
2211 UriParserData *pdata = user_data;
2213 if (!strcmp (element_name, "a"))
2214 g_string_append (pdata->new_str, "</span>");
2217 g_string_append (pdata->new_str, "</");
2218 g_string_append (pdata->new_str, element_name);
2219 g_string_append_c (pdata->new_str, '>');
2224 text_handler (GMarkupParseContext *context,
2230 UriParserData *pdata = user_data;
2233 newtext = g_markup_escape_text (text, text_len);
2234 g_string_append (pdata->new_str, newtext);
2238 static const GMarkupParser markup_parser =
2240 start_element_handler,
2241 end_element_handler,
2248 xml_isspace (gchar c)
2250 return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
2254 link_free (GtkLabelLink *link)
2257 g_free (link->title);
2262 gtk_label_get_link_colors (GtkWidget *widget,
2263 GdkColor **link_color,
2264 GdkColor **visited_link_color)
2266 gtk_widget_ensure_style (widget);
2267 gtk_widget_style_get (widget,
2268 "link-color", link_color,
2269 "visited-link-color", visited_link_color,
2272 *link_color = gdk_color_copy (&default_link_color);
2273 if (!*visited_link_color)
2274 *visited_link_color = gdk_color_copy (&default_visited_link_color);
2278 parse_uri_markup (GtkLabel *label,
2284 GMarkupParseContext *context = NULL;
2285 const gchar *p, *end;
2286 gboolean needs_root = TRUE;
2288 UriParserData pdata;
2290 length = strlen (str);
2294 pdata.label = label;
2296 pdata.new_str = g_string_sized_new (length);
2298 gtk_label_get_link_colors (GTK_WIDGET (label), &pdata.link_color, &pdata.visited_link_color);
2300 while (p != end && xml_isspace (*p))
2303 if (end - p >= 8 && strncmp (p, "<markup>", 8) == 0)
2306 context = g_markup_parse_context_new (&markup_parser, 0, &pdata, NULL);
2310 if (!g_markup_parse_context_parse (context, "<markup>", -1, error))
2314 if (!g_markup_parse_context_parse (context, str, length, error))
2319 if (!g_markup_parse_context_parse (context, "</markup>", -1, error))
2323 if (!g_markup_parse_context_end_parse (context, error))
2326 g_markup_parse_context_free (context);
2328 *new_str = g_string_free (pdata.new_str, FALSE);
2329 *links = pdata.links;
2331 gdk_color_free (pdata.link_color);
2332 gdk_color_free (pdata.visited_link_color);
2337 g_markup_parse_context_free (context);
2338 g_string_free (pdata.new_str, TRUE);
2339 g_list_foreach (pdata.links, (GFunc)link_free, NULL);
2340 g_list_free (pdata.links);
2341 gdk_color_free (pdata.link_color);
2342 gdk_color_free (pdata.visited_link_color);
2348 gtk_label_ensure_has_tooltip (GtkLabel *label)
2350 GtkLabelPriv *priv = label->priv;
2352 gboolean has_tooltip = FALSE;
2354 for (l = priv->select_info->links; l; l = l->next)
2356 GtkLabelLink *link = l->data;
2364 gtk_widget_set_has_tooltip (GTK_WIDGET (label), has_tooltip);
2368 gtk_label_set_markup_internal (GtkLabel *label,
2370 gboolean with_uline)
2372 GtkLabelPriv *priv = label->priv;
2374 GError *error = NULL;
2375 PangoAttrList *attrs = NULL;
2376 gunichar accel_char = 0;
2378 GList *links = NULL;
2380 if (!parse_uri_markup (label, str, &new_str, &links, &error))
2382 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2384 g_error_free (error);
2388 gtk_label_clear_links (label);
2391 gtk_label_ensure_select_info (label);
2392 priv->select_info->links = links;
2393 gtk_label_ensure_has_tooltip (label);
2398 gboolean enable_mnemonics;
2399 gboolean auto_mnemonics;
2401 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2402 "gtk-enable-mnemonics", &enable_mnemonics,
2403 "gtk-auto-mnemonics", &auto_mnemonics,
2406 if (!(enable_mnemonics && priv->mnemonics_visible &&
2408 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2409 (!priv->mnemonic_widget ||
2410 gtk_widget_is_sensitive (priv->mnemonic_widget))))))
2416 if (separate_uline_pattern (new_str, &key, &tmp, &pattern))
2425 if (!pango_parse_markup (new_str,
2427 with_uline ? '_' : 0,
2430 with_uline ? &accel_char : NULL,
2433 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2436 g_error_free (error);
2443 gtk_label_set_text_internal (label, text);
2447 if (priv->effective_attrs)
2448 pango_attr_list_unref (priv->effective_attrs);
2449 priv->effective_attrs = attrs;
2452 if (accel_char != 0)
2453 priv->mnemonic_keyval = gdk_keyval_to_lower (gdk_unicode_to_keyval (accel_char));
2455 priv->mnemonic_keyval = GDK_VoidSymbol;
2459 * gtk_label_set_markup:
2460 * @label: a #GtkLabel
2461 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
2463 * Parses @str which is marked up with the <link
2464 * linkend="PangoMarkupFormat">Pango text markup language</link>, setting the
2465 * label's text and attribute list based on the parse results. If the @str is
2466 * external data, you may need to escape it with g_markup_escape_text() or
2467 * g_markup_printf_escaped()<!-- -->:
2471 * markup = g_markup_printf_escaped ("<span style=\"italic\">%s</span>", str);
2472 * gtk_label_set_markup (GTK_LABEL (label), markup);
2477 gtk_label_set_markup (GtkLabel *label,
2482 g_return_if_fail (GTK_IS_LABEL (label));
2486 g_object_freeze_notify (G_OBJECT (label));
2488 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2489 gtk_label_set_use_markup_internal (label, TRUE);
2490 gtk_label_set_use_underline_internal (label, FALSE);
2492 gtk_label_recalculate (label);
2494 g_object_thaw_notify (G_OBJECT (label));
2498 * gtk_label_set_markup_with_mnemonic:
2499 * @label: a #GtkLabel
2500 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
2502 * Parses @str which is marked up with the <link linkend="PangoMarkupFormat">Pango text markup language</link>,
2503 * setting the label's text and attribute list based on the parse results.
2504 * If characters in @str are preceded by an underscore, they are underlined
2505 * indicating that they represent a keyboard accelerator called a mnemonic.
2507 * The mnemonic key can be used to activate another widget, chosen
2508 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
2511 gtk_label_set_markup_with_mnemonic (GtkLabel *label,
2516 g_return_if_fail (GTK_IS_LABEL (label));
2520 g_object_freeze_notify (G_OBJECT (label));
2522 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2523 gtk_label_set_use_markup_internal (label, TRUE);
2524 gtk_label_set_use_underline_internal (label, TRUE);
2526 gtk_label_recalculate (label);
2528 g_object_thaw_notify (G_OBJECT (label));
2532 * gtk_label_get_text:
2533 * @label: a #GtkLabel
2535 * Fetches the text from a label widget, as displayed on the
2536 * screen. This does not include any embedded underlines
2537 * indicating mnemonics or Pango markup. (See gtk_label_get_label())
2539 * Return value: the text in the label widget. This is the internal
2540 * string used by the label, and must not be modified.
2542 G_CONST_RETURN gchar *
2543 gtk_label_get_text (GtkLabel *label)
2545 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2547 return label->priv->text;
2550 static PangoAttrList *
2551 gtk_label_pattern_to_attrs (GtkLabel *label,
2552 const gchar *pattern)
2554 GtkLabelPriv *priv = label->priv;
2556 const char *p = priv->text;
2557 const char *q = pattern;
2558 PangoAttrList *attrs;
2560 attrs = pango_attr_list_new ();
2564 while (*p && *q && *q != '_')
2566 p = g_utf8_next_char (p);
2570 while (*p && *q && *q == '_')
2572 p = g_utf8_next_char (p);
2578 PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_LOW);
2579 attr->start_index = start - priv->text;
2580 attr->end_index = p - priv->text;
2582 pango_attr_list_insert (attrs, attr);
2592 gtk_label_set_pattern_internal (GtkLabel *label,
2593 const gchar *pattern,
2594 gboolean is_mnemonic)
2596 GtkLabelPriv *priv = label->priv;
2597 PangoAttrList *attrs;
2598 gboolean enable_mnemonics;
2599 gboolean auto_mnemonics;
2601 if (priv->pattern_set)
2606 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2607 "gtk-enable-mnemonics", &enable_mnemonics,
2608 "gtk-auto-mnemonics", &auto_mnemonics,
2611 if (enable_mnemonics && priv->mnemonics_visible && pattern &&
2613 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2614 (!priv->mnemonic_widget ||
2615 gtk_widget_is_sensitive (priv->mnemonic_widget)))))
2616 attrs = gtk_label_pattern_to_attrs (label, pattern);
2621 attrs = gtk_label_pattern_to_attrs (label, pattern);
2623 if (priv->effective_attrs)
2624 pango_attr_list_unref (priv->effective_attrs);
2625 priv->effective_attrs = attrs;
2629 gtk_label_set_pattern (GtkLabel *label,
2630 const gchar *pattern)
2632 GtkLabelPriv *priv = label->priv;
2634 g_return_if_fail (GTK_IS_LABEL (label));
2638 priv->pattern_set = FALSE;
2642 gtk_label_set_pattern_internal (label, pattern, FALSE);
2643 priv->pattern_set = TRUE;
2646 gtk_label_recalculate (label);
2648 gtk_label_clear_layout (label);
2649 gtk_widget_queue_resize (GTK_WIDGET (label));
2654 * gtk_label_set_justify:
2655 * @label: a #GtkLabel
2656 * @jtype: a #GtkJustification
2658 * Sets the alignment of the lines in the text of the label relative to
2659 * each other. %GTK_JUSTIFY_LEFT is the default value when the
2660 * widget is first created with gtk_label_new(). If you instead want
2661 * to set the alignment of the label as a whole, use
2662 * gtk_misc_set_alignment() instead. gtk_label_set_justify() has no
2663 * effect on labels containing only a single line.
2666 gtk_label_set_justify (GtkLabel *label,
2667 GtkJustification jtype)
2671 g_return_if_fail (GTK_IS_LABEL (label));
2672 g_return_if_fail (jtype >= GTK_JUSTIFY_LEFT && jtype <= GTK_JUSTIFY_FILL);
2676 if ((GtkJustification) priv->jtype != jtype)
2678 priv->jtype = jtype;
2680 /* No real need to be this drastic, but easier than duplicating the code */
2681 gtk_label_clear_layout (label);
2683 g_object_notify (G_OBJECT (label), "justify");
2684 gtk_widget_queue_resize (GTK_WIDGET (label));
2689 * gtk_label_get_justify:
2690 * @label: a #GtkLabel
2692 * Returns the justification of the label. See gtk_label_set_justify().
2694 * Return value: #GtkJustification
2697 gtk_label_get_justify (GtkLabel *label)
2699 g_return_val_if_fail (GTK_IS_LABEL (label), 0);
2701 return label->priv->jtype;
2705 * gtk_label_set_ellipsize:
2706 * @label: a #GtkLabel
2707 * @mode: a #PangoEllipsizeMode
2709 * Sets the mode used to ellipsize (add an ellipsis: "...") to the text
2710 * if there is not enough space to render the entire string.
2715 gtk_label_set_ellipsize (GtkLabel *label,
2716 PangoEllipsizeMode mode)
2720 g_return_if_fail (GTK_IS_LABEL (label));
2721 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE && mode <= PANGO_ELLIPSIZE_END);
2725 if ((PangoEllipsizeMode) priv->ellipsize != mode)
2727 priv->ellipsize = mode;
2729 /* No real need to be this drastic, but easier than duplicating the code */
2730 gtk_label_clear_layout (label);
2732 g_object_notify (G_OBJECT (label), "ellipsize");
2733 gtk_widget_queue_resize (GTK_WIDGET (label));
2738 * gtk_label_get_ellipsize:
2739 * @label: a #GtkLabel
2741 * Returns the ellipsizing position of the label. See gtk_label_set_ellipsize().
2743 * Return value: #PangoEllipsizeMode
2748 gtk_label_get_ellipsize (GtkLabel *label)
2750 g_return_val_if_fail (GTK_IS_LABEL (label), PANGO_ELLIPSIZE_NONE);
2752 return label->priv->ellipsize;
2756 * gtk_label_set_width_chars:
2757 * @label: a #GtkLabel
2758 * @n_chars: the new desired width, in characters.
2760 * Sets the desired width in characters of @label to @n_chars.
2765 gtk_label_set_width_chars (GtkLabel *label,
2770 g_return_if_fail (GTK_IS_LABEL (label));
2774 if (priv->width_chars != n_chars)
2776 priv->width_chars = n_chars;
2777 g_object_notify (G_OBJECT (label), "width-chars");
2778 gtk_label_invalidate_wrap_width (label);
2779 gtk_widget_queue_resize (GTK_WIDGET (label));
2784 * gtk_label_get_width_chars:
2785 * @label: a #GtkLabel
2787 * Retrieves the desired width of @label, in characters. See
2788 * gtk_label_set_width_chars().
2790 * Return value: the width of the label in characters.
2795 gtk_label_get_width_chars (GtkLabel *label)
2797 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2799 return label->priv->width_chars;
2803 * gtk_label_set_max_width_chars:
2804 * @label: a #GtkLabel
2805 * @n_chars: the new desired maximum width, in characters.
2807 * Sets the desired maximum width in characters of @label to @n_chars.
2812 gtk_label_set_max_width_chars (GtkLabel *label,
2817 g_return_if_fail (GTK_IS_LABEL (label));
2821 if (priv->max_width_chars != n_chars)
2823 priv->max_width_chars = n_chars;
2825 g_object_notify (G_OBJECT (label), "max-width-chars");
2826 gtk_label_invalidate_wrap_width (label);
2827 gtk_widget_queue_resize (GTK_WIDGET (label));
2832 * gtk_label_get_max_width_chars:
2833 * @label: a #GtkLabel
2835 * Retrieves the desired maximum width of @label, in characters. See
2836 * gtk_label_set_width_chars().
2838 * Return value: the maximum width of the label in characters.
2843 gtk_label_get_max_width_chars (GtkLabel *label)
2845 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2847 return label->priv->max_width_chars;
2851 * gtk_label_set_line_wrap:
2852 * @label: a #GtkLabel
2853 * @wrap: the setting
2855 * Toggles line wrapping within the #GtkLabel widget. %TRUE makes it break
2856 * lines if text exceeds the widget's size. %FALSE lets the text get cut off
2857 * by the edge of the widget if it exceeds the widget size.
2859 * Note that setting line wrapping to %TRUE does not make the label
2860 * wrap at its parent container's width, because GTK+ widgets
2861 * conceptually can't make their requisition depend on the parent
2862 * container's size. For a label that wraps at a specific position,
2863 * set the label's width using gtk_widget_set_size_request().
2866 gtk_label_set_line_wrap (GtkLabel *label,
2871 g_return_if_fail (GTK_IS_LABEL (label));
2875 wrap = wrap != FALSE;
2877 if (priv->wrap != wrap)
2881 gtk_label_clear_layout (label);
2882 gtk_widget_queue_resize (GTK_WIDGET (label));
2883 g_object_notify (G_OBJECT (label), "wrap");
2888 * gtk_label_get_line_wrap:
2889 * @label: a #GtkLabel
2891 * Returns whether lines in the label are automatically wrapped.
2892 * See gtk_label_set_line_wrap().
2894 * Return value: %TRUE if the lines of the label are automatically wrapped.
2897 gtk_label_get_line_wrap (GtkLabel *label)
2899 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
2901 return label->priv->wrap;
2905 * gtk_label_set_line_wrap_mode:
2906 * @label: a #GtkLabel
2907 * @wrap_mode: the line wrapping mode
2909 * If line wrapping is on (see gtk_label_set_line_wrap()) this controls how
2910 * the line wrapping is done. The default is %PANGO_WRAP_WORD which means
2911 * wrap on word boundaries.
2916 gtk_label_set_line_wrap_mode (GtkLabel *label,
2917 PangoWrapMode wrap_mode)
2921 g_return_if_fail (GTK_IS_LABEL (label));
2925 if (priv->wrap_mode != wrap_mode)
2927 priv->wrap_mode = wrap_mode;
2928 g_object_notify (G_OBJECT (label), "wrap-mode");
2930 gtk_widget_queue_resize (GTK_WIDGET (label));
2935 * gtk_label_get_line_wrap_mode:
2936 * @label: a #GtkLabel
2938 * Returns line wrap mode used by the label. See gtk_label_set_line_wrap_mode().
2940 * Return value: %TRUE if the lines of the label are automatically wrapped.
2945 gtk_label_get_line_wrap_mode (GtkLabel *label)
2947 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
2949 return label->priv->wrap_mode;
2953 gtk_label_destroy (GtkObject *object)
2955 GtkLabel *label = GTK_LABEL (object);
2957 gtk_label_set_mnemonic_widget (label, NULL);
2959 GTK_OBJECT_CLASS (gtk_label_parent_class)->destroy (object);
2963 gtk_label_finalize (GObject *object)
2965 GtkLabel *label = GTK_LABEL (object);
2966 GtkLabelPriv *priv = label->priv;
2968 g_free (priv->label);
2969 g_free (priv->text);
2972 g_object_unref (priv->layout);
2975 pango_attr_list_unref (priv->attrs);
2977 if (priv->effective_attrs)
2978 pango_attr_list_unref (priv->effective_attrs);
2980 gtk_label_clear_links (label);
2981 g_free (priv->select_info);
2983 G_OBJECT_CLASS (gtk_label_parent_class)->finalize (object);
2987 gtk_label_clear_layout (GtkLabel *label)
2989 GtkLabelPriv *priv = label->priv;
2993 g_object_unref (priv->layout);
2994 priv->layout = NULL;
2996 //gtk_label_clear_links (label);
3002 get_label_width (GtkLabel *label,
3006 GtkWidgetAuxInfo *aux_info;
3008 PangoLayout *layout;
3009 PangoContext *context;
3010 PangoFontMetrics *metrics;
3011 PangoRectangle rect;
3012 gint char_width, digit_width, char_pixels, text_width, ellipsize_chars, guess_width;
3015 aux_info = _gtk_widget_get_aux_info (GTK_WIDGET (label), FALSE);
3017 layout = pango_layout_copy (priv->layout);
3018 context = pango_layout_get_context (layout);
3019 metrics = pango_context_get_metrics (context,
3020 gtk_widget_get_style (GTK_WIDGET (label))->font_desc,
3021 pango_context_get_language (context));
3023 char_width = pango_font_metrics_get_approximate_char_width (metrics);
3024 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
3025 char_pixels = MAX (char_width, digit_width);
3026 pango_font_metrics_unref (metrics);
3028 /* Fetch the length of the complete unwrapped text */
3029 pango_layout_set_width (layout, -1);
3030 pango_layout_get_extents (layout, NULL, &rect);
3031 text_width = rect.width;
3033 /* Fetch the width that was guessed by gtk_label_ensure_layout() */
3034 pango_layout_get_extents (priv->layout, NULL, &rect);
3035 guess_width = rect.width;
3037 /* enforce minimum width for ellipsized labels at ~3 chars */
3038 if (priv->ellipsize)
3039 ellipsize_chars = 3;
3041 ellipsize_chars = 0;
3043 /* "width-chars" Hard-coded minimum width:
3044 * - minimum size should be MAX (width-chars, strlen ("..."));
3045 * - natural size should be MAX (width-chars, strlen (priv->text));
3047 * "max-width-chars" User specified maximum size requisition
3048 * - minimum size should be MAX (width-chars, 0)
3049 * - natural size should be MIN (max-width-chars, strlen (priv->text))
3051 * For ellipsizing labels; if max-width-chars is specified: either it is used as
3052 * a minimum size or the label text as a minimum size (natural size still overflows).
3054 * For wrapping labels; A reasonable minimum size is useful to naturally layout
3055 * interfaces automatically. In this case if no "width-chars" is specified, the minimum
3056 * width will default to the wrap guess that gtk_label_ensure_layout() does.
3058 * In *any* case the minimum width is completely overridden if an explicit width
3059 * request was provided.
3062 if (priv->ellipsize || priv->wrap)
3064 *minimum = char_pixels * MAX (priv->width_chars, ellipsize_chars);
3066 /* Default to the minimum width regularly guessed by GTK+ if no minimum
3067 * width was specified, only allow unwrapping of these labels.
3069 * Note that when specifying a small width_chars for a long text;
3070 * an accordingly large size will be required for the label height.
3072 if (priv->wrap && priv->width_chars <= 0)
3073 *minimum = guess_width;
3075 if (priv->max_width_chars < 0)
3077 *natural = MAX (*minimum, text_width);
3081 gint max_char_width = char_pixels * priv->max_width_chars;
3082 gint max_width = MIN (text_width, max_char_width);
3084 /* With max-char-width specified, we let the minimum widths of
3085 * ellipsized text crawl up to the max-char-width
3086 * (note that we dont want to limit the minimum width for wrapping text).
3088 if (priv->ellipsize)
3089 *minimum = MIN (text_width, max_width);
3091 *natural = MAX (*minimum, max_width);
3096 *minimum = text_width;
3097 *natural = *minimum;
3100 /* if a width-request is set, use that as the requested label width */
3101 if ((priv->wrap || priv->ellipsize || priv->width_chars > 0 || priv->max_width_chars > 0) &&
3102 aux_info && aux_info->width > 0)
3104 *minimum = aux_info->width * PANGO_SCALE;
3105 *natural = MAX (*natural, *minimum);
3108 g_object_unref (layout);
3112 gtk_label_invalidate_wrap_width (GtkLabel *label)
3114 GtkLabelPriv *priv = label->priv;
3116 priv->wrap_width = -1;
3120 get_label_wrap_width (GtkLabel *label)
3122 GtkLabelPriv *priv = label->priv;
3124 if (priv->wrap_width < 0)
3126 if (priv->width_chars > 0)
3128 PangoLayout *layout;
3129 PangoContext *context;
3130 PangoFontMetrics *metrics;
3131 PangoRectangle rect;
3132 gint char_width, digit_width, char_pixels, text_width;
3134 layout = pango_layout_copy (priv->layout);
3135 context = pango_layout_get_context (layout);
3136 metrics = pango_context_get_metrics (context,
3137 gtk_widget_get_style (GTK_WIDGET (label))->font_desc,
3138 pango_context_get_language (context));
3140 char_width = pango_font_metrics_get_approximate_char_width (metrics);
3141 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
3142 char_pixels = MAX (char_width, digit_width);
3143 pango_font_metrics_unref (metrics);
3145 pango_layout_set_width (layout, -1);
3146 pango_layout_get_extents (layout, NULL, &rect);
3147 g_object_unref (layout);
3149 text_width = rect.width;
3151 priv->wrap_width = PANGO_PIXELS (MAX (text_width, char_pixels * priv->width_chars));
3155 PangoLayout *layout;
3157 layout = gtk_widget_create_pango_layout (GTK_WIDGET (label),
3158 "This string is just about long enough.");
3159 pango_layout_get_size (layout, &priv->wrap_width, NULL);
3160 g_object_unref (layout);
3164 return priv->wrap_width;
3168 gtk_label_ensure_layout (GtkLabel *label, gboolean guess_wrap_width)
3170 GtkLabelPriv *priv = label->priv;
3172 PangoRectangle logical_rect;
3175 widget = GTK_WIDGET (label);
3177 rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
3181 GtkAllocation allocation;
3182 PangoAlignment align = PANGO_ALIGN_LEFT; /* Quiet gcc */
3183 gdouble angle = gtk_label_get_angle (label);
3185 if (angle != 0.0 && !priv->select_info)
3187 PangoMatrix matrix = PANGO_MATRIX_INIT;
3189 /* We rotate the standard singleton PangoContext for the widget,
3190 * depending on the fact that it's meant pretty much exclusively
3193 pango_matrix_rotate (&matrix, angle);
3195 pango_context_set_matrix (gtk_widget_get_pango_context (widget), &matrix);
3197 priv->have_transform = TRUE;
3201 if (priv->have_transform)
3202 pango_context_set_matrix (gtk_widget_get_pango_context (widget), NULL);
3204 priv->have_transform = FALSE;
3207 priv->layout = gtk_widget_create_pango_layout (widget, priv->text);
3209 if (priv->effective_attrs)
3210 pango_layout_set_attributes (priv->layout, priv->effective_attrs);
3212 gtk_label_rescan_links (label);
3214 switch (priv->jtype)
3216 case GTK_JUSTIFY_LEFT:
3217 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3219 case GTK_JUSTIFY_RIGHT:
3220 align = rtl ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
3222 case GTK_JUSTIFY_CENTER:
3223 align = PANGO_ALIGN_CENTER;
3225 case GTK_JUSTIFY_FILL:
3226 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3227 pango_layout_set_justify (priv->layout, TRUE);
3230 g_assert_not_reached();
3233 pango_layout_set_alignment (priv->layout, align);
3234 pango_layout_set_ellipsize (priv->layout, priv->ellipsize);
3235 pango_layout_set_single_paragraph_mode (priv->layout, priv->single_line_mode);
3237 gtk_widget_get_allocation (widget, &allocation);
3239 if (priv->ellipsize)
3240 pango_layout_set_width (priv->layout, allocation.width * PANGO_SCALE);
3241 else if (priv->wrap)
3243 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (widget, FALSE);
3244 gint longest_paragraph;
3248 if ((angle == 90 || angle == 270) && aux_info && aux_info->height > 0)
3249 aux_width = aux_info->height;
3250 else if (aux_info && aux_info->width > 0)
3251 aux_width = aux_info->width;
3254 pango_layout_set_width (priv->layout, aux_width * PANGO_SCALE);
3255 else if (guess_wrap_width == FALSE && allocation.width > 1 && allocation.height > 1)
3257 PangoRectangle rect;
3258 gint xpad, ypad, natural_width;
3259 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3261 if (angle == 90 || angle == 270)
3262 width = allocation.height - ypad * 2;
3264 width = allocation.width - xpad * 2;
3266 /* dont set a wrap width wider than the label's natural width
3267 * incase we're allocated more space than needed */
3268 pango_layout_get_extents (priv->layout, NULL, &rect);
3269 natural_width = PANGO_PIXELS (rect.width);
3270 width = MIN (natural_width, width);
3272 pango_layout_set_wrap (priv->layout, priv->wrap_mode);
3273 pango_layout_set_width (priv->layout, MAX (width, 1) * PANGO_SCALE);
3277 GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (label));
3280 pango_layout_set_width (priv->layout, -1);
3281 pango_layout_get_extents (priv->layout, NULL, &logical_rect);
3283 width = logical_rect.width;
3284 /* Try to guess a reasonable maximum width */
3285 longest_paragraph = width;
3287 wrap_width = get_label_wrap_width (label);
3288 width = MIN (width, wrap_width);
3290 PANGO_SCALE * (gdk_screen_get_width (screen) + 1) / 2);
3292 pango_layout_set_width (priv->layout, width);
3293 pango_layout_get_extents (priv->layout, NULL, &logical_rect);
3294 width = logical_rect.width;
3295 height = logical_rect.height;
3297 /* Unfortunately, the above may leave us with a very unbalanced looking paragraph,
3298 * so we try short search for a narrower width that leaves us with the same height
3300 if (longest_paragraph > 0)
3302 gint nlines, perfect_width;
3304 nlines = pango_layout_get_line_count (priv->layout);
3305 perfect_width = (longest_paragraph + nlines - 1) / nlines;
3307 if (perfect_width < width)
3309 pango_layout_set_width (priv->layout, perfect_width);
3310 pango_layout_get_extents (priv->layout, NULL, &logical_rect);
3312 if (logical_rect.height <= height)
3313 width = logical_rect.width;
3316 gint mid_width = (perfect_width + width) / 2;
3318 if (mid_width > perfect_width)
3320 pango_layout_set_width (priv->layout, mid_width);
3321 pango_layout_get_extents (priv->layout, NULL, &logical_rect);
3323 if (logical_rect.height <= height)
3324 width = logical_rect.width;
3329 pango_layout_set_width (priv->layout, width);
3332 else /* !priv->wrap */
3333 pango_layout_set_width (priv->layout, -1);
3338 get_single_line_height (GtkWidget *widget,
3339 PangoLayout *layout)
3341 PangoContext *context;
3342 PangoFontMetrics *metrics;
3343 gint ascent, descent;
3345 context = pango_layout_get_context (layout);
3346 metrics = pango_context_get_metrics (context, gtk_widget_get_style (widget)->font_desc,
3347 pango_context_get_language (context));
3349 ascent = pango_font_metrics_get_ascent (metrics);
3350 descent = pango_font_metrics_get_descent (metrics);
3351 pango_font_metrics_unref (metrics);
3353 return ascent + descent;
3357 gtk_label_size_request_init (GtkSizeRequestIface *iface)
3359 iface->get_request_mode = gtk_label_get_request_mode;
3360 iface->get_width = gtk_label_get_width;
3361 iface->get_height = gtk_label_get_height;
3362 iface->get_width_for_height = gtk_label_get_width_for_height;
3363 iface->get_height_for_width = gtk_label_get_height_for_width;
3366 static GtkSizeRequestMode
3367 gtk_label_get_request_mode (GtkSizeRequest *layout)
3369 GtkLabel *label = GTK_LABEL (layout);
3370 gdouble angle = gtk_label_get_angle (label);
3372 if (angle == 90 || angle == 270)
3373 return GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
3375 return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
3379 get_size_for_allocation (GtkLabel *label,
3380 GtkOrientation orientation,
3385 GtkLabelPriv *priv = label->priv;
3386 PangoLayout *layout;
3387 GtkWidgetAuxInfo *aux_info =
3388 _gtk_widget_get_aux_info (GTK_WIDGET (label), FALSE);
3392 gtk_label_ensure_layout (label, FALSE);
3393 layout = pango_layout_copy (priv->layout);
3397 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3398 aux_size = aux_info->width;
3400 aux_size = aux_info->height;
3406 pango_layout_set_width (layout, aux_size * PANGO_SCALE);
3408 pango_layout_set_width (layout, allocation * PANGO_SCALE);
3410 pango_layout_get_pixel_size (layout, NULL, &text_height);
3413 *minimum_size = text_height;
3416 *natural_size = text_height;
3418 g_object_unref (layout);
3422 gtk_label_get_size (GtkSizeRequest *widget,
3423 GtkOrientation orientation,
3427 GtkLabel *label = GTK_LABEL (widget);
3428 GtkLabelPriv *priv = label->priv;
3429 PangoRectangle required_rect;
3430 PangoRectangle natural_rect;
3433 /* "width-chars" Hard-coded minimum width:
3434 * - minimum size should be MAX (width-chars, strlen ("..."));
3435 * - natural size should be MAX (width-chars, strlen (priv->text));
3437 * "max-width-chars" User specified maximum size requisition
3438 * - minimum size should be MAX (width-chars, 0)
3439 * - natural size should be MIN (max-width-chars, strlen (priv->text))
3443 /* When calculating ->wrap sometimes we need to invent a size; Ideally we should be doing
3444 * that stuff here instead of inside gtk_label_ensure_layout() */
3446 gtk_label_clear_layout (label);
3447 gtk_label_ensure_layout (label, TRUE);
3449 /* Start off with the pixel extents of the rendered layout */
3450 pango_layout_get_extents (priv->layout, NULL, &required_rect);
3451 required_rect.x = required_rect.y = 0;
3453 if (priv->single_line_mode || priv->wrap)
3454 required_rect.height = get_single_line_height (GTK_WIDGET (label), priv->layout);
3456 natural_rect = required_rect;
3458 /* Calculate text width itself based on GtkLabel property rules */
3459 get_label_width (label, &required_rect.width, &natural_rect.width);
3461 /* Now that we have minimum and natural sizes in pango extents, apply a possible transform */
3462 if (priv->have_transform)
3464 PangoLayout *layout = pango_layout_copy (priv->layout);
3465 PangoContext *context = pango_layout_get_context (priv->layout);
3466 const PangoMatrix *matrix = pango_context_get_matrix (context);
3468 pango_layout_set_width (layout, -1);
3469 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_NONE);
3471 pango_layout_get_extents (layout, NULL, &natural_rect);
3472 g_object_unref (layout);
3474 pango_matrix_transform_rectangle (matrix, &required_rect);
3475 pango_matrix_transform_rectangle (matrix, &natural_rect);
3477 /* Bump the natural size in case of ellipsize to ensure pango has
3478 * enough space in the angles (note, we could alternatively set the
3479 * layout to not ellipsize when we know we have been allocated our
3480 * full natural size, or it may be that pango needs a fix here).
3482 if (priv->ellipsize && priv->angle != 0 && priv->angle != 90 &&
3483 priv->angle != 180 && priv->angle != 270 && priv->angle != 360)
3485 /* For some reason we only need this at about 110 degrees, and only
3486 * when gaining in height
3488 natural_rect.height += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3489 natural_rect.width += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3493 required_rect.width = PANGO_PIXELS_CEIL (required_rect.width);
3494 required_rect.height = PANGO_PIXELS_CEIL (required_rect.height);
3496 natural_rect.width = PANGO_PIXELS_CEIL (natural_rect.width);
3497 natural_rect.height = PANGO_PIXELS_CEIL (natural_rect.height);
3499 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3501 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3503 /* Note, we cant use get_size_for_allocation() when rotating
3504 * ellipsized labels.
3506 if (!(priv->ellipsize && priv->have_transform) &&
3507 (priv->angle == 90 || priv->angle == 270))
3509 /* Doing a h4w request on a rotated label here, return the
3510 * required width for the minimum height.
3512 get_size_for_allocation (label,
3513 GTK_ORIENTATION_VERTICAL,
3514 required_rect.height,
3515 minimum_size, natural_size);
3520 /* Normal desired width */
3521 *minimum_size = required_rect.width;
3522 *natural_size = natural_rect.width;
3525 *minimum_size += xpad * 2;
3526 *natural_size += xpad * 2;
3528 else /* GTK_ORIENTATION_VERTICAL */
3530 /* Note, we cant use get_size_for_allocation() when rotating
3531 * ellipsized labels.
3533 if (!(priv->ellipsize && priv->have_transform) &&
3534 (priv->angle == 0 || priv->angle == 180 || priv->angle == 360))
3536 /* Doing a w4h request on a label here, return the required
3537 * height for the minimum width.
3539 get_size_for_allocation (label,
3540 GTK_ORIENTATION_HORIZONTAL,
3541 required_rect.width,
3542 minimum_size, natural_size);
3546 /* A vertically rotated label does w4h, so return the base
3547 * desired height (text length)
3549 *minimum_size = required_rect.height;
3550 *natural_size = natural_rect.height;
3553 *minimum_size += ypad * 2;
3554 *natural_size += ypad * 2;
3557 /* Restore real allocated size of layout; sometimes size-requests
3558 * are randomly called without a following allocation; for this case
3559 * we need to make sure we dont have a mucked up layout because we
3560 * went and guessed the wrap-size.
3563 gtk_label_clear_layout (label);
3564 gtk_label_ensure_layout (label, FALSE);
3570 gtk_label_get_width (GtkSizeRequest *widget,
3574 gtk_label_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
3578 gtk_label_get_height (GtkSizeRequest *widget,
3582 gtk_label_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
3586 gtk_label_get_width_for_height (GtkSizeRequest *widget,
3588 gint *minimum_width,
3589 gint *natural_width)
3591 GtkLabel *label = GTK_LABEL (widget);
3592 GtkLabelPriv *priv = label->priv;
3594 if (priv->wrap && (priv->angle == 90 || priv->angle == 270))
3598 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3601 gtk_label_clear_layout (label);
3603 get_size_for_allocation (label, GTK_ORIENTATION_VERTICAL,
3604 MAX (1, height - (ypad * 2)),
3605 minimum_width, natural_width);
3608 *minimum_width += xpad * 2;
3611 *natural_width += xpad * 2;
3614 GTK_SIZE_REQUEST_GET_IFACE (widget)->get_width (widget, minimum_width, natural_width);
3618 gtk_label_get_height_for_width (GtkSizeRequest *widget,
3620 gint *minimum_height,
3621 gint *natural_height)
3623 GtkLabel *label = GTK_LABEL (widget);
3624 GtkLabelPriv *priv = label->priv;
3626 if (priv->wrap && (priv->angle == 0 || priv->angle == 180 || priv->angle == 360))
3630 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3633 gtk_label_clear_layout (label);
3635 get_size_for_allocation (label, GTK_ORIENTATION_HORIZONTAL,
3636 MAX (1, width - xpad * 2),
3637 minimum_height, natural_height);
3640 *minimum_height += ypad * 2;
3643 *natural_height += ypad * 2;
3646 GTK_SIZE_REQUEST_GET_IFACE (widget)->get_height (widget, minimum_height, natural_height);
3650 gtk_label_size_allocate (GtkWidget *widget,
3651 GtkAllocation *allocation)
3653 GtkLabel *label = GTK_LABEL (widget);
3654 GtkLabelPriv *priv = label->priv;
3656 GTK_WIDGET_CLASS (gtk_label_parent_class)->size_allocate (widget, allocation);
3658 /* The layout may have been recently cleared in get_size_for_orientation(),
3659 * but the width at that point may not be the same as the allocated width
3662 gtk_label_clear_layout (label);
3664 gtk_label_ensure_layout (label, FALSE);
3666 if (priv->ellipsize)
3670 PangoRectangle logical;
3671 PangoRectangle bounds;
3674 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3676 bounds.x = bounds.y = 0;
3677 bounds.width = allocation->width - xpad * 2;
3678 bounds.height = allocation->height - ypad * 2;
3680 pango_layout_set_width (priv->layout, -1);
3681 pango_layout_get_pixel_extents (priv->layout, NULL, &logical);
3683 if (priv->have_transform)
3685 PangoContext *context = gtk_widget_get_pango_context (widget);
3686 const PangoMatrix *matrix = pango_context_get_matrix (context);
3688 const gdouble dx = matrix->xx; /* cos (M_PI * angle / 180) */
3689 const gdouble dy = matrix->xy; /* sin (M_PI * angle / 180) */
3690 if (fabs (dy) < 0.01)
3692 if (logical.width > bounds.width)
3693 pango_layout_set_width (priv->layout, bounds.width * PANGO_SCALE);
3695 else if (fabs (dx) < 0.01)
3697 if (logical.width > bounds.height)
3698 pango_layout_set_width (priv->layout, bounds.height * PANGO_SCALE);
3702 gdouble x0, y0, x1, y1, length;
3706 x0 = bounds.width / 2;
3707 y0 = dx ? x0 * dy / dx : G_MAXDOUBLE;
3708 vertical = fabs (y0) > bounds.height / 2;
3712 y0 = bounds.height/2;
3713 x0 = dy ? y0 * dx / dy : G_MAXDOUBLE;
3716 length = 2 * sqrt (x0 * x0 + y0 * y0);
3717 pango_layout_set_width (priv->layout, rint (length * PANGO_SCALE));
3718 pango_layout_get_pixel_size (priv->layout, NULL, &cy);
3725 y0 = bounds.height/2 + y1 - y0;
3730 x0 = bounds.width/2 + x1 - x0;
3734 length = length - sqrt (x0 * x0 + y0 * y0) * 2;
3735 pango_layout_set_width (priv->layout, rint (length * PANGO_SCALE));
3738 else if (logical.width > bounds.width)
3739 pango_layout_set_width (priv->layout, bounds.width * PANGO_SCALE);
3743 if (priv->select_info && priv->select_info->window)
3745 gdk_window_move_resize (priv->select_info->window,
3749 allocation->height);
3754 gtk_label_update_cursor (GtkLabel *label)
3756 GtkLabelPriv *priv = label->priv;
3759 if (!priv->select_info)
3762 widget = GTK_WIDGET (label);
3764 if (gtk_widget_get_realized (widget))
3766 GdkDisplay *display;
3769 if (gtk_widget_is_sensitive (widget))
3771 display = gtk_widget_get_display (widget);
3773 if (priv->select_info->active_link)
3774 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
3775 else if (priv->select_info->selectable)
3776 cursor = gdk_cursor_new_for_display (display, GDK_XTERM);
3783 gdk_window_set_cursor (priv->select_info->window, cursor);
3786 gdk_cursor_unref (cursor);
3791 gtk_label_state_changed (GtkWidget *widget,
3792 GtkStateType prev_state)
3794 GtkLabel *label = GTK_LABEL (widget);
3795 GtkLabelPriv *priv = label->priv;
3797 if (priv->select_info)
3799 gtk_label_select_region (label, 0, 0);
3800 gtk_label_update_cursor (label);
3803 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed)
3804 GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed (widget, prev_state);
3808 gtk_label_style_set (GtkWidget *widget,
3809 GtkStyle *previous_style)
3811 GtkLabel *label = GTK_LABEL (widget);
3813 /* We have to clear the layout, fonts etc. may have changed */
3814 gtk_label_clear_layout (label);
3815 gtk_label_invalidate_wrap_width (label);
3819 gtk_label_direction_changed (GtkWidget *widget,
3820 GtkTextDirection previous_dir)
3822 GtkLabel *label = GTK_LABEL (widget);
3823 GtkLabelPriv *priv = label->priv;
3826 pango_layout_context_changed (priv->layout);
3828 GTK_WIDGET_CLASS (gtk_label_parent_class)->direction_changed (widget, previous_dir);
3832 get_layout_location (GtkLabel *label,
3836 GtkAllocation allocation;
3840 gint req_width, x, y;
3843 gfloat xalign, yalign;
3844 PangoRectangle logical;
3846 misc = GTK_MISC (label);
3847 widget = GTK_WIDGET (label);
3850 gtk_misc_get_alignment (misc, &xalign, &yalign);
3851 gtk_misc_get_padding (misc, &xpad, &ypad);
3853 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
3854 xalign = 1.0 - xalign;
3856 pango_layout_get_extents (priv->layout, NULL, &logical);
3858 if (priv->have_transform)
3860 PangoContext *context = gtk_widget_get_pango_context (widget);
3861 const PangoMatrix *matrix = pango_context_get_matrix (context);
3862 pango_matrix_transform_rectangle (matrix, &logical);
3865 pango_extents_to_pixels (&logical, NULL);
3867 req_width = logical.width;
3868 req_height = logical.height;
3870 req_width += 2 * xpad;
3871 req_height += 2 * ypad;
3873 gtk_widget_get_allocation (widget, &allocation);
3875 x = floor (allocation.x + xpad + xalign * (allocation.width - req_width));
3877 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
3878 x = MAX (x, allocation.x + xpad);
3880 x = MIN (x, allocation.x + allocation.width - xpad);
3883 /* bgo#315462 - For single-line labels, *do* align the requisition with
3884 * respect to the allocation, even if we are under-allocated. For multi-line
3885 * labels, always show the top of the text when they are under-allocated. The
3886 * rationale is this:
3888 * - Single-line labels appear in GtkButtons, and it is very easy to get them
3889 * to be smaller than their requisition. The button may clip the label, but
3890 * the label will still be able to show most of itself and the focus
3891 * rectangle. Also, it is fairly easy to read a single line of clipped text.
3893 * - Multi-line labels should not be clipped to showing "something in the
3894 * middle". You want to read the first line, at least, to get some context.
3896 if (pango_layout_get_line_count (priv->layout) == 1)
3897 y = floor (allocation.y + ypad + (allocation.height - req_height) * yalign);
3899 y = floor (allocation.y + ypad + MAX ((allocation.height - req_height) * yalign, 0));
3909 draw_insertion_cursor (GtkLabel *label,
3910 GdkRectangle *cursor_location,
3911 gboolean is_primary,
3912 PangoDirection direction,
3913 gboolean draw_arrow)
3915 GtkWidget *widget = GTK_WIDGET (label);
3916 GtkAllocation allocation;
3917 GtkTextDirection text_dir;
3919 if (direction == PANGO_DIRECTION_LTR)
3920 text_dir = GTK_TEXT_DIR_LTR;
3922 text_dir = GTK_TEXT_DIR_RTL;
3924 gtk_widget_get_allocation (widget, &allocation);
3925 gtk_draw_insertion_cursor (widget, gtk_widget_get_window (widget),
3926 &allocation, cursor_location,
3927 is_primary, text_dir, draw_arrow);
3930 static PangoDirection
3931 get_cursor_direction (GtkLabel *label)
3933 GtkLabelPriv *priv = label->priv;
3936 g_assert (priv->select_info);
3938 gtk_label_ensure_layout (label, FALSE);
3940 for (l = pango_layout_get_lines_readonly (priv->layout); l; l = l->next)
3942 PangoLayoutLine *line = l->data;
3944 /* If priv->select_info->selection_end is at the very end of
3945 * the line, we don't know if the cursor is on this line or
3946 * the next without looking ahead at the next line. (End
3947 * of paragraph is different from line break.) But it's
3948 * definitely in this paragraph, which is good enough
3949 * to figure out the resolved direction.
3951 if (line->start_index + line->length >= priv->select_info->selection_end)
3952 return line->resolved_dir;
3955 return PANGO_DIRECTION_LTR;
3959 gtk_label_draw_cursor (GtkLabel *label, gint xoffset, gint yoffset)
3961 GtkLabelPriv *priv = label->priv;
3964 if (priv->select_info == NULL)
3967 widget = GTK_WIDGET (label);
3969 if (gtk_widget_is_drawable (widget))
3971 PangoDirection keymap_direction;
3972 PangoDirection cursor_direction;
3973 PangoRectangle strong_pos, weak_pos;
3974 gboolean split_cursor;
3975 PangoRectangle *cursor1 = NULL;
3976 PangoRectangle *cursor2 = NULL;
3977 GdkRectangle cursor_location;
3978 PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
3979 PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
3981 keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gtk_widget_get_display (widget)));
3982 cursor_direction = get_cursor_direction (label);
3984 gtk_label_ensure_layout (label, FALSE);
3986 pango_layout_get_cursor_pos (priv->layout, priv->select_info->selection_end,
3987 &strong_pos, &weak_pos);
3989 g_object_get (gtk_widget_get_settings (widget),
3990 "gtk-split-cursor", &split_cursor,
3993 dir1 = cursor_direction;
3997 cursor1 = &strong_pos;
3999 if (strong_pos.x != weak_pos.x ||
4000 strong_pos.y != weak_pos.y)
4002 dir2 = (cursor_direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
4003 cursor2 = &weak_pos;
4008 if (keymap_direction == cursor_direction)
4009 cursor1 = &strong_pos;
4011 cursor1 = &weak_pos;
4014 cursor_location.x = xoffset + PANGO_PIXELS (cursor1->x);
4015 cursor_location.y = yoffset + PANGO_PIXELS (cursor1->y);
4016 cursor_location.width = 0;
4017 cursor_location.height = PANGO_PIXELS (cursor1->height);
4019 draw_insertion_cursor (label,
4020 &cursor_location, TRUE, dir1,
4021 dir2 != PANGO_DIRECTION_NEUTRAL);
4023 if (dir2 != PANGO_DIRECTION_NEUTRAL)
4025 cursor_location.x = xoffset + PANGO_PIXELS (cursor2->x);
4026 cursor_location.y = yoffset + PANGO_PIXELS (cursor2->y);
4027 cursor_location.width = 0;
4028 cursor_location.height = PANGO_PIXELS (cursor2->height);
4030 draw_insertion_cursor (label,
4031 &cursor_location, FALSE, dir2,
4037 static GtkLabelLink *
4038 gtk_label_get_focus_link (GtkLabel *label)
4040 GtkLabelPriv *priv = label->priv;
4041 GtkLabelSelectionInfo *info = priv->select_info;
4047 if (info->selection_anchor != info->selection_end)
4050 for (l = info->links; l; l = l->next)
4052 GtkLabelLink *link = l->data;
4053 if (link->start <= info->selection_anchor &&
4054 info->selection_anchor <= link->end)
4062 gtk_label_expose (GtkWidget *widget,
4063 GdkEventExpose *event)
4065 GtkLabel *label = GTK_LABEL (widget);
4066 GtkLabelPriv *priv = label->priv;
4067 GtkLabelSelectionInfo *info = priv->select_info;
4072 gtk_label_ensure_layout (label, FALSE);
4074 if (gtk_widget_get_visible (widget) && gtk_widget_get_mapped (widget) &&
4075 priv->text && (*priv->text != '\0'))
4077 get_layout_location (label, &x, &y);
4079 style = gtk_widget_get_style (widget);
4080 window = gtk_widget_get_window (widget);
4082 gtk_paint_layout (style,
4084 gtk_widget_get_state (widget),
4093 (info->selection_anchor != info->selection_end))
4096 cairo_region_t *clip;
4100 range[0] = info->selection_anchor;
4101 range[1] = info->selection_end;
4103 if (range[0] > range[1])
4105 gint tmp = range[0];
4106 range[0] = range[1];
4110 clip = gdk_pango_layout_get_clip_region (priv->layout,
4114 cairo_region_intersect (clip, event->region);
4116 /* FIXME should use gtk_paint, but it can't use a clip
4120 cr = gdk_cairo_create (event->window);
4122 gdk_cairo_region (cr, clip);
4125 state = GTK_STATE_SELECTED;
4126 if (!gtk_widget_has_focus (widget))
4127 state = GTK_STATE_ACTIVE;
4129 gdk_cairo_set_source_color (cr, &style->base[state]);
4132 gdk_cairo_set_source_color (cr, &style->text[state]);
4133 cairo_move_to (cr, x, y);
4134 _gtk_pango_fill_layout (cr, priv->layout);
4137 cairo_region_destroy (clip);
4141 GtkLabelLink *focus_link;
4142 GtkLabelLink *active_link;
4144 cairo_region_t *clip;
4146 GdkColor *text_color;
4147 GdkColor *base_color;
4148 GdkColor *link_color;
4149 GdkColor *visited_link_color;
4151 if (info->selectable && gtk_widget_has_focus (widget))
4152 gtk_label_draw_cursor (label, x, y);
4154 focus_link = gtk_label_get_focus_link (label);
4155 active_link = info->active_link;
4162 range[0] = active_link->start;
4163 range[1] = active_link->end;
4165 cr = gdk_cairo_create (event->window);
4167 gdk_cairo_region (cr, event->region);
4170 clip = gdk_pango_layout_get_clip_region (priv->layout,
4174 gdk_cairo_region (cr, clip);
4176 cairo_region_destroy (clip);
4178 gtk_label_get_link_colors (widget, &link_color, &visited_link_color);
4179 if (active_link->visited)
4180 text_color = visited_link_color;
4182 text_color = link_color;
4183 if (info->link_clicked)
4184 base_color = &style->base[GTK_STATE_ACTIVE];
4186 base_color = &style->base[GTK_STATE_PRELIGHT];
4188 gdk_cairo_set_source_color (cr, base_color);
4191 gdk_cairo_set_source_color (cr, text_color);
4192 cairo_move_to (cr, x, y);
4193 _gtk_pango_fill_layout (cr, priv->layout);
4195 gdk_color_free (link_color);
4196 gdk_color_free (visited_link_color);
4201 if (focus_link && gtk_widget_has_focus (widget))
4203 range[0] = focus_link->start;
4204 range[1] = focus_link->end;
4206 clip = gdk_pango_layout_get_clip_region (priv->layout,
4210 cairo_region_get_extents (clip, &rect);
4212 gtk_paint_focus (style, window, gtk_widget_get_state (widget),
4213 &event->area, widget, "label",
4214 rect.x, rect.y, rect.width, rect.height);
4216 cairo_region_destroy (clip);
4225 separate_uline_pattern (const gchar *str,
4230 gboolean underscore;
4233 gchar *pattern_dest;
4235 *accel_key = GDK_VoidSymbol;
4236 *new_str = g_new (gchar, strlen (str) + 1);
4237 *pattern = g_new (gchar, g_utf8_strlen (str, -1) + 1);
4243 pattern_dest = *pattern;
4248 const gchar *next_src;
4250 c = g_utf8_get_char (src);
4251 if (c == (gunichar)-1)
4253 g_warning ("Invalid input string");
4259 next_src = g_utf8_next_char (src);
4264 *pattern_dest++ = ' ';
4267 *pattern_dest++ = '_';
4268 if (*accel_key == GDK_VoidSymbol)
4269 *accel_key = gdk_keyval_to_lower (gdk_unicode_to_keyval (c));
4272 while (src < next_src)
4286 while (src < next_src)
4289 *pattern_dest++ = ' ';
4301 gtk_label_set_uline_text_internal (GtkLabel *label,
4304 GtkLabelPriv *priv = label->priv;
4305 guint accel_key = GDK_VoidSymbol;
4309 g_return_if_fail (GTK_IS_LABEL (label));
4310 g_return_if_fail (str != NULL);
4312 /* Split text into the base text and a separate pattern
4315 if (!separate_uline_pattern (str, &accel_key, &new_str, &pattern))
4318 gtk_label_set_text_internal (label, new_str);
4319 gtk_label_set_pattern_internal (label, pattern, TRUE);
4320 priv->mnemonic_keyval = accel_key;
4326 * gtk_label_set_text_with_mnemonic:
4327 * @label: a #GtkLabel
4330 * Sets the label's text from the string @str.
4331 * If characters in @str are preceded by an underscore, they are underlined
4332 * indicating that they represent a keyboard accelerator called a mnemonic.
4333 * The mnemonic key can be used to activate another widget, chosen
4334 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
4337 gtk_label_set_text_with_mnemonic (GtkLabel *label,
4340 g_return_if_fail (GTK_IS_LABEL (label));
4341 g_return_if_fail (str != NULL);
4343 g_object_freeze_notify (G_OBJECT (label));
4345 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
4346 gtk_label_set_use_markup_internal (label, FALSE);
4347 gtk_label_set_use_underline_internal (label, TRUE);
4349 gtk_label_recalculate (label);
4351 g_object_thaw_notify (G_OBJECT (label));
4355 gtk_label_realize (GtkWidget *widget)
4357 GtkLabel *label = GTK_LABEL (widget);
4358 GtkLabelPriv *priv = label->priv;
4360 GTK_WIDGET_CLASS (gtk_label_parent_class)->realize (widget);
4362 if (priv->select_info)
4363 gtk_label_create_window (label);
4367 gtk_label_unrealize (GtkWidget *widget)
4369 GtkLabel *label = GTK_LABEL (widget);
4370 GtkLabelPriv *priv = label->priv;
4372 if (priv->select_info)
4373 gtk_label_destroy_window (label);
4375 GTK_WIDGET_CLASS (gtk_label_parent_class)->unrealize (widget);
4379 gtk_label_map (GtkWidget *widget)
4381 GtkLabel *label = GTK_LABEL (widget);
4382 GtkLabelPriv *priv = label->priv;
4384 GTK_WIDGET_CLASS (gtk_label_parent_class)->map (widget);
4386 if (priv->select_info)
4387 gdk_window_show (priv->select_info->window);
4391 gtk_label_unmap (GtkWidget *widget)
4393 GtkLabel *label = GTK_LABEL (widget);
4394 GtkLabelPriv *priv = label->priv;
4396 if (priv->select_info)
4397 gdk_window_hide (priv->select_info->window);
4399 GTK_WIDGET_CLASS (gtk_label_parent_class)->unmap (widget);
4403 window_to_layout_coords (GtkLabel *label,
4407 GtkAllocation allocation;
4411 widget = GTK_WIDGET (label);
4413 /* get layout location in widget->window coords */
4414 get_layout_location (label, &lx, &ly);
4416 gtk_widget_get_allocation (widget, &allocation);
4420 *x += allocation.x; /* go to widget->window */
4421 *x -= lx; /* go to layout */
4426 *y += allocation.y; /* go to widget->window */
4427 *y -= ly; /* go to layout */
4433 layout_to_window_coords (GtkLabel *label,
4440 widget = GTK_WIDGET (label);
4442 /* get layout location in widget->window coords */
4443 get_layout_location (label, &lx, &ly);
4447 *x += lx; /* go to widget->window */
4448 *x -= widget->allocation.x; /* go to selection window */
4453 *y += ly; /* go to widget->window */
4454 *y -= widget->allocation.y; /* go to selection window */
4460 get_layout_index (GtkLabel *label,
4465 GtkLabelPriv *priv = label->priv;
4467 const gchar *cluster;
4468 const gchar *cluster_end;
4473 gtk_label_ensure_layout (label, FALSE);
4475 window_to_layout_coords (label, &x, &y);
4480 inside = pango_layout_xy_to_index (priv->layout,
4484 cluster = priv->text + *index;
4485 cluster_end = cluster;
4488 cluster_end = g_utf8_next_char (cluster_end);
4492 *index += (cluster_end - cluster);
4498 gtk_label_select_word (GtkLabel *label)
4500 GtkLabelPriv *priv = label->priv;
4503 gint start_index = gtk_label_move_backward_word (label, priv->select_info->selection_end);
4504 gint end_index = gtk_label_move_forward_word (label, priv->select_info->selection_end);
4506 min = MIN (priv->select_info->selection_anchor,
4507 priv->select_info->selection_end);
4508 max = MAX (priv->select_info->selection_anchor,
4509 priv->select_info->selection_end);
4511 min = MIN (min, start_index);
4512 max = MAX (max, end_index);
4514 gtk_label_select_region_index (label, min, max);
4518 gtk_label_grab_focus (GtkWidget *widget)
4520 GtkLabel *label = GTK_LABEL (widget);
4521 GtkLabelPriv *priv = label->priv;
4522 gboolean select_on_focus;
4525 if (priv->select_info == NULL)
4528 GTK_WIDGET_CLASS (gtk_label_parent_class)->grab_focus (widget);
4530 if (priv->select_info->selectable)
4532 g_object_get (gtk_widget_get_settings (widget),
4533 "gtk-label-select-on-focus",
4537 if (select_on_focus && !priv->in_click)
4538 gtk_label_select_region (label, 0, -1);
4542 if (priv->select_info->links && !priv->in_click)
4544 link = priv->select_info->links->data;
4545 priv->select_info->selection_anchor = link->start;
4546 priv->select_info->selection_end = link->start;
4552 gtk_label_focus (GtkWidget *widget,
4553 GtkDirectionType direction)
4555 GtkLabel *label = GTK_LABEL (widget);
4556 GtkLabelPriv *priv = label->priv;
4557 GtkLabelSelectionInfo *info = priv->select_info;
4558 GtkLabelLink *focus_link;
4561 if (!gtk_widget_is_focus (widget))
4563 gtk_widget_grab_focus (widget);
4566 focus_link = gtk_label_get_focus_link (label);
4567 if (focus_link && direction == GTK_DIR_TAB_BACKWARD)
4569 l = g_list_last (info->links);
4570 focus_link = l->data;
4571 info->selection_anchor = focus_link->start;
4572 info->selection_end = focus_link->start;
4582 if (info->selectable)
4586 if (info->selection_anchor != info->selection_end)
4589 index = info->selection_anchor;
4591 if (direction == GTK_DIR_TAB_FORWARD)
4592 for (l = info->links; l; l = l->next)
4594 GtkLabelLink *link = l->data;
4596 if (link->start > index)
4598 gtk_label_select_region_index (label, link->start, link->start);
4602 else if (direction == GTK_DIR_TAB_BACKWARD)
4603 for (l = g_list_last (info->links); l; l = l->prev)
4605 GtkLabelLink *link = l->data;
4607 if (link->end < index)
4609 gtk_label_select_region_index (label, link->start, link->start);
4618 focus_link = gtk_label_get_focus_link (label);
4621 case GTK_DIR_TAB_FORWARD:
4624 l = g_list_find (info->links, focus_link);
4631 case GTK_DIR_TAB_BACKWARD:
4634 l = g_list_find (info->links, focus_link);
4638 l = g_list_last (info->links);
4647 focus_link = l->data;
4648 info->selection_anchor = focus_link->start;
4649 info->selection_end = focus_link->start;
4650 gtk_widget_queue_draw (widget);
4662 gtk_label_button_press (GtkWidget *widget,
4663 GdkEventButton *event)
4665 GtkLabel *label = GTK_LABEL (widget);
4666 GtkLabelPriv *priv = label->priv;
4667 GtkLabelSelectionInfo *info = priv->select_info;
4674 if (info->active_link)
4676 if (event->button == 1)
4678 info->link_clicked = 1;
4679 gtk_widget_queue_draw (widget);
4681 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4683 info->link_clicked = 1;
4684 gtk_label_do_popup (label, event);
4689 if (!info->selectable)
4692 info->in_drag = FALSE;
4693 info->select_words = FALSE;
4695 if (event->button == 1)
4697 if (!gtk_widget_has_focus (widget))
4699 priv->in_click = TRUE;
4700 gtk_widget_grab_focus (widget);
4701 priv->in_click = FALSE;
4704 if (event->type == GDK_3BUTTON_PRESS)
4706 gtk_label_select_region_index (label, 0, strlen (priv->text));
4710 if (event->type == GDK_2BUTTON_PRESS)
4712 info->select_words = TRUE;
4713 gtk_label_select_word (label);
4717 get_layout_index (label, event->x, event->y, &index);
4719 min = MIN (info->selection_anchor, info->selection_end);
4720 max = MAX (info->selection_anchor, info->selection_end);
4722 if ((info->selection_anchor != info->selection_end) &&
4723 (event->state & GDK_SHIFT_MASK))
4725 /* extend (same as motion) */
4726 min = MIN (min, index);
4727 max = MAX (max, index);
4729 /* ensure the anchor is opposite index */
4737 gtk_label_select_region_index (label, min, max);
4741 if (event->type == GDK_3BUTTON_PRESS)
4742 gtk_label_select_region_index (label, 0, strlen (priv->text));
4743 else if (event->type == GDK_2BUTTON_PRESS)
4744 gtk_label_select_word (label);
4745 else if (min < max && min <= index && index <= max)
4747 info->in_drag = TRUE;
4748 info->drag_start_x = event->x;
4749 info->drag_start_y = event->y;
4752 /* start a replacement */
4753 gtk_label_select_region_index (label, index, index);
4758 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4760 gtk_label_do_popup (label, event);
4768 gtk_label_button_release (GtkWidget *widget,
4769 GdkEventButton *event)
4772 GtkLabel *label = GTK_LABEL (widget);
4773 GtkLabelPriv *priv = label->priv;
4774 GtkLabelSelectionInfo *info = priv->select_info;
4784 get_layout_index (label, event->x, event->y, &index);
4785 gtk_label_select_region_index (label, index, index);
4790 if (event->button != 1)
4793 if (info->active_link &&
4794 info->selection_anchor == info->selection_end &&
4797 emit_activate_link (label, info->active_link);
4798 info->link_clicked = 0;
4803 /* The goal here is to return TRUE iff we ate the
4804 * button press to start selecting.
4810 connect_mnemonics_visible_notify (GtkLabel *label)
4812 GtkLabelPriv *priv = label->priv;
4813 GtkWidget *toplevel;
4816 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
4818 if (!GTK_IS_WINDOW (toplevel))
4821 /* always set up this widgets initial value */
4822 priv->mnemonics_visible =
4823 gtk_window_get_mnemonics_visible (GTK_WINDOW (toplevel));
4826 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (toplevel),
4827 "gtk-label-mnemonics-visible-connected"));
4831 g_signal_connect (toplevel,
4832 "notify::mnemonics-visible",
4833 G_CALLBACK (label_mnemonics_visible_changed),
4835 g_object_set_data (G_OBJECT (toplevel),
4836 "gtk-label-mnemonics-visible-connected",
4837 GINT_TO_POINTER (1));
4842 drag_begin_cb (GtkWidget *widget,
4843 GdkDragContext *context,
4846 GtkLabel *label = GTK_LABEL (widget);
4847 GtkLabelPriv *priv = label->priv;
4848 GdkPixmap *pixmap = NULL;
4850 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
4852 if ((priv->select_info->selection_anchor !=
4853 priv->select_info->selection_end) &&
4859 start = MIN (priv->select_info->selection_anchor,
4860 priv->select_info->selection_end);
4861 end = MAX (priv->select_info->selection_anchor,
4862 priv->select_info->selection_end);
4864 len = strlen (priv->text);
4872 pixmap = _gtk_text_util_create_drag_icon (widget,
4878 gtk_drag_set_icon_pixmap (context,
4879 gdk_drawable_get_colormap (pixmap),
4884 gtk_drag_set_icon_default (context);
4887 g_object_unref (pixmap);
4891 gtk_label_motion (GtkWidget *widget,
4892 GdkEventMotion *event)
4894 GtkLabel *label = GTK_LABEL (widget);
4895 GtkLabelPriv *priv = label->priv;
4896 GtkLabelSelectionInfo *info = priv->select_info;
4902 if (info->links && !info->in_drag)
4906 gboolean found = FALSE;
4908 if (info->selection_anchor == info->selection_end)
4910 if (get_layout_index (label, event->x, event->y, &index))
4912 for (l = info->links; l != NULL; l = l->next)
4915 if (index >= link->start && index <= link->end)
4926 if (info->active_link != link)
4928 info->link_clicked = 0;
4929 info->active_link = link;
4930 gtk_label_update_cursor (label);
4931 gtk_widget_queue_draw (widget);
4936 if (info->active_link != NULL)
4938 info->link_clicked = 0;
4939 info->active_link = NULL;
4940 gtk_label_update_cursor (label);
4941 gtk_widget_queue_draw (widget);
4946 if (!info->selectable)
4949 if ((event->state & GDK_BUTTON1_MASK) == 0)
4954 if (gtk_drag_check_threshold (widget,
4957 event->x, event->y))
4959 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
4961 gtk_target_list_add_text_targets (target_list, 0);
4963 g_signal_connect (widget, "drag-begin",
4964 G_CALLBACK (drag_begin_cb), NULL);
4965 gtk_drag_begin (widget, target_list,
4967 1, (GdkEvent *)event);
4969 info->in_drag = FALSE;
4971 gtk_target_list_unref (target_list);
4978 gdk_window_get_device_position (info->window, event->device, &x, &y, NULL);
4979 get_layout_index (label, x, y, &index);
4981 if (info->select_words)
4984 gint old_min, old_max;
4987 min = gtk_label_move_backward_word (label, index);
4988 max = gtk_label_move_forward_word (label, index);
4990 anchor = info->selection_anchor;
4991 end = info->selection_end;
4993 old_min = MIN (anchor, end);
4994 old_max = MAX (anchor, end);
5001 else if (old_max < max)
5006 else if (anchor == old_min)
5017 gtk_label_select_region_index (label, anchor, end);
5020 gtk_label_select_region_index (label, info->selection_anchor, index);
5027 gtk_label_leave_notify (GtkWidget *widget,
5028 GdkEventCrossing *event)
5030 GtkLabel *label = GTK_LABEL (widget);
5031 GtkLabelPriv *priv = label->priv;
5033 if (priv->select_info)
5035 priv->select_info->active_link = NULL;
5036 gtk_label_update_cursor (label);
5037 gtk_widget_queue_draw (widget);
5040 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event)
5041 return GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event (widget, event);
5047 gtk_label_create_window (GtkLabel *label)
5049 GtkLabelPriv *priv = label->priv;
5050 GtkAllocation allocation;
5052 GdkWindowAttr attributes;
5053 gint attributes_mask;
5055 g_assert (priv->select_info);
5056 widget = GTK_WIDGET (label);
5057 g_assert (gtk_widget_get_realized (widget));
5059 if (priv->select_info->window)
5062 gtk_widget_get_allocation (widget, &allocation);
5064 attributes.x = allocation.x;
5065 attributes.y = allocation.y;
5066 attributes.width = allocation.width;
5067 attributes.height = allocation.height;
5068 attributes.window_type = GDK_WINDOW_CHILD;
5069 attributes.wclass = GDK_INPUT_ONLY;
5070 attributes.override_redirect = TRUE;
5071 attributes.event_mask = gtk_widget_get_events (widget) |
5072 GDK_BUTTON_PRESS_MASK |
5073 GDK_BUTTON_RELEASE_MASK |
5074 GDK_LEAVE_NOTIFY_MASK |
5075 GDK_BUTTON_MOTION_MASK |
5076 GDK_POINTER_MOTION_MASK |
5077 GDK_POINTER_MOTION_HINT_MASK;
5078 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
5079 if (gtk_widget_is_sensitive (widget))
5081 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
5083 attributes_mask |= GDK_WA_CURSOR;
5087 priv->select_info->window = gdk_window_new (gtk_widget_get_window (widget),
5088 &attributes, attributes_mask);
5089 gdk_window_set_user_data (priv->select_info->window, widget);
5091 if (attributes_mask & GDK_WA_CURSOR)
5092 gdk_cursor_unref (attributes.cursor);
5096 gtk_label_destroy_window (GtkLabel *label)
5098 GtkLabelPriv *priv = label->priv;
5100 g_assert (priv->select_info);
5102 if (priv->select_info->window == NULL)
5105 gdk_window_set_user_data (priv->select_info->window, NULL);
5106 gdk_window_destroy (priv->select_info->window);
5107 priv->select_info->window = NULL;
5111 gtk_label_ensure_select_info (GtkLabel *label)
5113 GtkLabelPriv *priv = label->priv;
5115 if (priv->select_info == NULL)
5117 priv->select_info = g_new0 (GtkLabelSelectionInfo, 1);
5119 gtk_widget_set_can_focus (GTK_WIDGET (label), TRUE);
5121 if (gtk_widget_get_realized (GTK_WIDGET (label)))
5122 gtk_label_create_window (label);
5124 if (gtk_widget_get_mapped (GTK_WIDGET (label)))
5125 gdk_window_show (priv->select_info->window);
5130 gtk_label_clear_select_info (GtkLabel *label)
5132 GtkLabelPriv *priv = label->priv;
5134 if (priv->select_info == NULL)
5137 if (!priv->select_info->selectable && !priv->select_info->links)
5139 gtk_label_destroy_window (label);
5141 g_free (priv->select_info);
5142 priv->select_info = NULL;
5144 gtk_widget_set_can_focus (GTK_WIDGET (label), FALSE);
5149 * gtk_label_set_selectable:
5150 * @label: a #GtkLabel
5151 * @setting: %TRUE to allow selecting text in the label
5153 * Selectable labels allow the user to select text from the label, for
5157 gtk_label_set_selectable (GtkLabel *label,
5161 gboolean old_setting;
5163 g_return_if_fail (GTK_IS_LABEL (label));
5167 setting = setting != FALSE;
5168 old_setting = priv->select_info && priv->select_info->selectable;
5172 gtk_label_ensure_select_info (label);
5173 priv->select_info->selectable = TRUE;
5174 gtk_label_update_cursor (label);
5180 /* unselect, to give up the selection */
5181 gtk_label_select_region (label, 0, 0);
5183 priv->select_info->selectable = FALSE;
5184 gtk_label_clear_select_info (label);
5185 gtk_label_update_cursor (label);
5188 if (setting != old_setting)
5190 g_object_freeze_notify (G_OBJECT (label));
5191 g_object_notify (G_OBJECT (label), "selectable");
5192 g_object_notify (G_OBJECT (label), "cursor-position");
5193 g_object_notify (G_OBJECT (label), "selection-bound");
5194 g_object_thaw_notify (G_OBJECT (label));
5195 gtk_widget_queue_draw (GTK_WIDGET (label));
5200 * gtk_label_get_selectable:
5201 * @label: a #GtkLabel
5203 * Gets the value set by gtk_label_set_selectable().
5205 * Return value: %TRUE if the user can copy text from the label
5208 gtk_label_get_selectable (GtkLabel *label)
5212 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5216 return priv->select_info && priv->select_info->selectable;
5220 * gtk_label_set_angle:
5221 * @label: a #GtkLabel
5222 * @angle: the angle that the baseline of the label makes with
5223 * the horizontal, in degrees, measured counterclockwise
5225 * Sets the angle of rotation for the label. An angle of 90 reads from
5226 * from bottom to top, an angle of 270, from top to bottom. The angle
5227 * setting for the label is ignored if the label is selectable,
5228 * wrapped, or ellipsized.
5233 gtk_label_set_angle (GtkLabel *label,
5238 g_return_if_fail (GTK_IS_LABEL (label));
5242 /* Canonicalize to [0,360]. We don't canonicalize 360 to 0, because
5243 * double property ranges are inclusive, and changing 360 to 0 would
5244 * make a property editor behave strangely.
5246 if (angle < 0 || angle > 360.0)
5247 angle = angle - 360. * floor (angle / 360.);
5249 if (priv->angle != angle)
5251 priv->angle = angle;
5253 gtk_label_clear_layout (label);
5254 gtk_widget_queue_resize (GTK_WIDGET (label));
5256 g_object_notify (G_OBJECT (label), "angle");
5261 * gtk_label_get_angle:
5262 * @label: a #GtkLabel
5264 * Gets the angle of rotation for the label. See
5265 * gtk_label_set_angle().
5267 * Return value: the angle of rotation for the label
5272 gtk_label_get_angle (GtkLabel *label)
5274 g_return_val_if_fail (GTK_IS_LABEL (label), 0.0);
5276 return label->priv->angle;
5280 gtk_label_set_selection_text (GtkLabel *label,
5281 GtkSelectionData *selection_data)
5283 GtkLabelPriv *priv = label->priv;
5285 if ((priv->select_info->selection_anchor !=
5286 priv->select_info->selection_end) &&
5292 start = MIN (priv->select_info->selection_anchor,
5293 priv->select_info->selection_end);
5294 end = MAX (priv->select_info->selection_anchor,
5295 priv->select_info->selection_end);
5297 len = strlen (priv->text);
5305 gtk_selection_data_set_text (selection_data,
5312 gtk_label_drag_data_get (GtkWidget *widget,
5313 GdkDragContext *context,
5314 GtkSelectionData *selection_data,
5318 gtk_label_set_selection_text (GTK_LABEL (widget), selection_data);
5322 get_text_callback (GtkClipboard *clipboard,
5323 GtkSelectionData *selection_data,
5325 gpointer user_data_or_owner)
5327 gtk_label_set_selection_text (GTK_LABEL (user_data_or_owner), selection_data);
5331 clear_text_callback (GtkClipboard *clipboard,
5332 gpointer user_data_or_owner)
5337 label = GTK_LABEL (user_data_or_owner);
5340 if (priv->select_info)
5342 priv->select_info->selection_anchor = priv->select_info->selection_end;
5344 gtk_widget_queue_draw (GTK_WIDGET (label));
5349 gtk_label_select_region_index (GtkLabel *label,
5355 g_return_if_fail (GTK_IS_LABEL (label));
5359 if (priv->select_info && priv->select_info->selectable)
5361 GtkClipboard *clipboard;
5363 if (priv->select_info->selection_anchor == anchor_index &&
5364 priv->select_info->selection_end == end_index)
5367 priv->select_info->selection_anchor = anchor_index;
5368 priv->select_info->selection_end = end_index;
5370 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label),
5371 GDK_SELECTION_PRIMARY);
5373 if (anchor_index != end_index)
5375 GtkTargetList *list;
5376 GtkTargetEntry *targets;
5379 list = gtk_target_list_new (NULL, 0);
5380 gtk_target_list_add_text_targets (list, 0);
5381 targets = gtk_target_table_new_from_list (list, &n_targets);
5383 gtk_clipboard_set_with_owner (clipboard,
5386 clear_text_callback,
5389 gtk_target_table_free (targets, n_targets);
5390 gtk_target_list_unref (list);
5394 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (label))
5395 gtk_clipboard_clear (clipboard);
5398 gtk_widget_queue_draw (GTK_WIDGET (label));
5400 g_object_freeze_notify (G_OBJECT (label));
5401 g_object_notify (G_OBJECT (label), "cursor-position");
5402 g_object_notify (G_OBJECT (label), "selection-bound");
5403 g_object_thaw_notify (G_OBJECT (label));
5408 * gtk_label_select_region:
5409 * @label: a #GtkLabel
5410 * @start_offset: start offset (in characters not bytes)
5411 * @end_offset: end offset (in characters not bytes)
5413 * Selects a range of characters in the label, if the label is selectable.
5414 * See gtk_label_set_selectable(). If the label is not selectable,
5415 * this function has no effect. If @start_offset or
5416 * @end_offset are -1, then the end of the label will be substituted.
5419 gtk_label_select_region (GtkLabel *label,
5425 g_return_if_fail (GTK_IS_LABEL (label));
5429 if (priv->text && priv->select_info)
5431 if (start_offset < 0)
5432 start_offset = g_utf8_strlen (priv->text, -1);
5435 end_offset = g_utf8_strlen (priv->text, -1);
5437 gtk_label_select_region_index (label,
5438 g_utf8_offset_to_pointer (priv->text, start_offset) - priv->text,
5439 g_utf8_offset_to_pointer (priv->text, end_offset) - priv->text);
5444 * gtk_label_get_selection_bounds:
5445 * @label: a #GtkLabel
5446 * @start: return location for start of selection, as a character offset
5447 * @end: return location for end of selection, as a character offset
5449 * Gets the selected range of characters in the label, returning %TRUE
5450 * if there's a selection.
5452 * Return value: %TRUE if selection is non-empty
5455 gtk_label_get_selection_bounds (GtkLabel *label,
5461 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5465 if (priv->select_info == NULL)
5467 /* not a selectable label */
5477 gint start_index, end_index;
5478 gint start_offset, end_offset;
5481 start_index = MIN (priv->select_info->selection_anchor,
5482 priv->select_info->selection_end);
5483 end_index = MAX (priv->select_info->selection_anchor,
5484 priv->select_info->selection_end);
5486 len = strlen (priv->text);
5488 if (end_index > len)
5491 if (start_index > len)
5494 start_offset = g_utf8_strlen (priv->text, start_index);
5495 end_offset = g_utf8_strlen (priv->text, end_index);
5497 if (start_offset > end_offset)
5499 gint tmp = start_offset;
5500 start_offset = end_offset;
5505 *start = start_offset;
5510 return start_offset != end_offset;
5516 * gtk_label_get_layout:
5517 * @label: a #GtkLabel
5519 * Gets the #PangoLayout used to display the label.
5520 * The layout is useful to e.g. convert text positions to
5521 * pixel positions, in combination with gtk_label_get_layout_offsets().
5522 * The returned layout is owned by the label so need not be
5523 * freed by the caller.
5525 * Return value: (transfer none): the #PangoLayout for this label
5528 gtk_label_get_layout (GtkLabel *label)
5532 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
5536 gtk_label_ensure_layout (label, FALSE);
5538 return priv->layout;
5542 * gtk_label_get_layout_offsets:
5543 * @label: a #GtkLabel
5544 * @x: (allow-none): location to store X offset of layout, or %NULL
5545 * @y: (allow-none): location to store Y offset of layout, or %NULL
5547 * Obtains the coordinates where the label will draw the #PangoLayout
5548 * representing the text in the label; useful to convert mouse events
5549 * into coordinates inside the #PangoLayout, e.g. to take some action
5550 * if some part of the label is clicked. Of course you will need to
5551 * create a #GtkEventBox to receive the events, and pack the label
5552 * inside it, since labels are a #GTK_NO_WINDOW widget. Remember
5553 * when using the #PangoLayout functions you need to convert to
5554 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
5557 gtk_label_get_layout_offsets (GtkLabel *label,
5561 g_return_if_fail (GTK_IS_LABEL (label));
5563 gtk_label_ensure_layout (label, FALSE);
5565 get_layout_location (label, x, y);
5569 * gtk_label_set_use_markup:
5570 * @label: a #GtkLabel
5571 * @setting: %TRUE if the label's text should be parsed for markup.
5573 * Sets whether the text of the label contains markup in <link
5574 * linkend="PangoMarkupFormat">Pango's text markup
5575 * language</link>. See gtk_label_set_markup().
5578 gtk_label_set_use_markup (GtkLabel *label,
5581 g_return_if_fail (GTK_IS_LABEL (label));
5583 gtk_label_set_use_markup_internal (label, setting);
5584 gtk_label_recalculate (label);
5588 * gtk_label_get_use_markup:
5589 * @label: a #GtkLabel
5591 * Returns whether the label's text is interpreted as marked up with
5592 * the <link linkend="PangoMarkupFormat">Pango text markup
5593 * language</link>. See gtk_label_set_use_markup ().
5595 * Return value: %TRUE if the label's text will be parsed for markup.
5598 gtk_label_get_use_markup (GtkLabel *label)
5600 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5602 return label->priv->use_markup;
5606 * gtk_label_set_use_underline:
5607 * @label: a #GtkLabel
5608 * @setting: %TRUE if underlines in the text indicate mnemonics
5610 * If true, an underline in the text indicates the next character should be
5611 * used for the mnemonic accelerator key.
5614 gtk_label_set_use_underline (GtkLabel *label,
5617 g_return_if_fail (GTK_IS_LABEL (label));
5619 gtk_label_set_use_underline_internal (label, setting);
5620 gtk_label_recalculate (label);
5624 * gtk_label_get_use_underline:
5625 * @label: a #GtkLabel
5627 * Returns whether an embedded underline in the label indicates a
5628 * mnemonic. See gtk_label_set_use_underline().
5630 * Return value: %TRUE whether an embedded underline in the label indicates
5631 * the mnemonic accelerator keys.
5634 gtk_label_get_use_underline (GtkLabel *label)
5636 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5638 return label->priv->use_underline;
5642 * gtk_label_set_single_line_mode:
5643 * @label: a #GtkLabel
5644 * @single_line_mode: %TRUE if the label should be in single line mode
5646 * Sets whether the label is in single line mode.
5651 gtk_label_set_single_line_mode (GtkLabel *label,
5652 gboolean single_line_mode)
5656 g_return_if_fail (GTK_IS_LABEL (label));
5660 single_line_mode = single_line_mode != FALSE;
5662 if (priv->single_line_mode != single_line_mode)
5664 priv->single_line_mode = single_line_mode;
5666 gtk_label_clear_layout (label);
5667 gtk_widget_queue_resize (GTK_WIDGET (label));
5669 g_object_notify (G_OBJECT (label), "single-line-mode");
5674 * gtk_label_get_single_line_mode:
5675 * @label: a #GtkLabel
5677 * Returns whether the label is in single line mode.
5679 * Return value: %TRUE when the label is in single line mode.
5684 gtk_label_get_single_line_mode (GtkLabel *label)
5686 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5688 return label->priv->single_line_mode;
5691 /* Compute the X position for an offset that corresponds to the "more important
5692 * cursor position for that offset. We use this when trying to guess to which
5693 * end of the selection we should go to when the user hits the left or
5697 get_better_cursor (GtkLabel *label,
5702 GtkLabelPriv *priv = label->priv;
5703 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5704 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5705 PangoDirection cursor_direction = get_cursor_direction (label);
5706 gboolean split_cursor;
5707 PangoRectangle strong_pos, weak_pos;
5709 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5710 "gtk-split-cursor", &split_cursor,
5713 gtk_label_ensure_layout (label, FALSE);
5715 pango_layout_get_cursor_pos (priv->layout, index,
5716 &strong_pos, &weak_pos);
5720 *x = strong_pos.x / PANGO_SCALE;
5721 *y = strong_pos.y / PANGO_SCALE;
5725 if (keymap_direction == cursor_direction)
5727 *x = strong_pos.x / PANGO_SCALE;
5728 *y = strong_pos.y / PANGO_SCALE;
5732 *x = weak_pos.x / PANGO_SCALE;
5733 *y = weak_pos.y / PANGO_SCALE;
5740 gtk_label_move_logically (GtkLabel *label,
5744 GtkLabelPriv *priv = label->priv;
5745 gint offset = g_utf8_pointer_to_offset (priv->text,
5746 priv->text + start);
5750 PangoLogAttr *log_attrs;
5754 gtk_label_ensure_layout (label, FALSE);
5756 length = g_utf8_strlen (priv->text, -1);
5758 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5760 while (count > 0 && offset < length)
5764 while (offset < length && !log_attrs[offset].is_cursor_position);
5768 while (count < 0 && offset > 0)
5772 while (offset > 0 && !log_attrs[offset].is_cursor_position);
5780 return g_utf8_offset_to_pointer (priv->text, offset) - priv->text;
5784 gtk_label_move_visually (GtkLabel *label,
5788 GtkLabelPriv *priv = label->priv;
5795 int new_index, new_trailing;
5796 gboolean split_cursor;
5799 gtk_label_ensure_layout (label, FALSE);
5801 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5802 "gtk-split-cursor", &split_cursor,
5809 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5810 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5812 strong = keymap_direction == get_cursor_direction (label);
5817 pango_layout_move_cursor_visually (priv->layout, strong, index, 0, 1, &new_index, &new_trailing);
5822 pango_layout_move_cursor_visually (priv->layout, strong, index, 0, -1, &new_index, &new_trailing);
5826 if (new_index < 0 || new_index == G_MAXINT)
5831 while (new_trailing--)
5832 index = g_utf8_next_char (priv->text + new_index) - priv->text;
5839 gtk_label_move_forward_word (GtkLabel *label,
5842 GtkLabelPriv *priv = label->priv;
5843 gint new_pos = g_utf8_pointer_to_offset (priv->text,
5844 priv->text + start);
5847 length = g_utf8_strlen (priv->text, -1);
5848 if (new_pos < length)
5850 PangoLogAttr *log_attrs;
5853 gtk_label_ensure_layout (label, FALSE);
5855 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5857 /* Find the next word end */
5859 while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
5865 return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
5870 gtk_label_move_backward_word (GtkLabel *label,
5873 GtkLabelPriv *priv = label->priv;
5874 gint new_pos = g_utf8_pointer_to_offset (priv->text,
5875 priv->text + start);
5879 PangoLogAttr *log_attrs;
5882 gtk_label_ensure_layout (label, FALSE);
5884 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5888 /* Find the previous word beginning */
5889 while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
5895 return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
5899 gtk_label_move_cursor (GtkLabel *label,
5900 GtkMovementStep step,
5902 gboolean extend_selection)
5904 GtkLabelPriv *priv = label->priv;
5908 if (priv->select_info == NULL)
5911 old_pos = new_pos = priv->select_info->selection_end;
5913 if (priv->select_info->selection_end != priv->select_info->selection_anchor &&
5916 /* If we have a current selection and aren't extending it, move to the
5917 * start/or end of the selection as appropriate
5921 case GTK_MOVEMENT_VISUAL_POSITIONS:
5924 gint anchor_x, anchor_y;
5925 gboolean end_is_left;
5927 get_better_cursor (label, priv->select_info->selection_end, &end_x, &end_y);
5928 get_better_cursor (label, priv->select_info->selection_anchor, &anchor_x, &anchor_y);
5930 end_is_left = (end_y < anchor_y) || (end_y == anchor_y && end_x < anchor_x);
5933 new_pos = end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
5935 new_pos = !end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
5938 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5939 case GTK_MOVEMENT_WORDS:
5941 new_pos = MIN (priv->select_info->selection_end, priv->select_info->selection_anchor);
5943 new_pos = MAX (priv->select_info->selection_end, priv->select_info->selection_anchor);
5945 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5946 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5947 case GTK_MOVEMENT_BUFFER_ENDS:
5948 /* FIXME: Can do better here */
5949 new_pos = count < 0 ? 0 : strlen (priv->text);
5951 case GTK_MOVEMENT_DISPLAY_LINES:
5952 case GTK_MOVEMENT_PARAGRAPHS:
5953 case GTK_MOVEMENT_PAGES:
5954 case GTK_MOVEMENT_HORIZONTAL_PAGES:
5962 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5963 new_pos = gtk_label_move_logically (label, new_pos, count);
5965 case GTK_MOVEMENT_VISUAL_POSITIONS:
5966 new_pos = gtk_label_move_visually (label, new_pos, count);
5967 if (new_pos == old_pos)
5969 if (!extend_selection)
5971 if (!gtk_widget_keynav_failed (GTK_WIDGET (label),
5973 GTK_DIR_RIGHT : GTK_DIR_LEFT))
5975 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
5978 gtk_widget_child_focus (toplevel,
5980 GTK_DIR_RIGHT : GTK_DIR_LEFT);
5985 gtk_widget_error_bell (GTK_WIDGET (label));
5989 case GTK_MOVEMENT_WORDS:
5992 new_pos = gtk_label_move_forward_word (label, new_pos);
5997 new_pos = gtk_label_move_backward_word (label, new_pos);
6000 if (new_pos == old_pos)
6001 gtk_widget_error_bell (GTK_WIDGET (label));
6003 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
6004 case GTK_MOVEMENT_PARAGRAPH_ENDS:
6005 case GTK_MOVEMENT_BUFFER_ENDS:
6006 /* FIXME: Can do better here */
6007 new_pos = count < 0 ? 0 : strlen (priv->text);
6008 if (new_pos == old_pos)
6009 gtk_widget_error_bell (GTK_WIDGET (label));
6011 case GTK_MOVEMENT_DISPLAY_LINES:
6012 case GTK_MOVEMENT_PARAGRAPHS:
6013 case GTK_MOVEMENT_PAGES:
6014 case GTK_MOVEMENT_HORIZONTAL_PAGES:
6019 if (extend_selection)
6020 gtk_label_select_region_index (label,
6021 priv->select_info->selection_anchor,
6024 gtk_label_select_region_index (label, new_pos, new_pos);
6028 gtk_label_copy_clipboard (GtkLabel *label)
6030 GtkLabelPriv *priv = label->priv;
6032 if (priv->text && priv->select_info)
6036 GtkClipboard *clipboard;
6038 start = MIN (priv->select_info->selection_anchor,
6039 priv->select_info->selection_end);
6040 end = MAX (priv->select_info->selection_anchor,
6041 priv->select_info->selection_end);
6043 len = strlen (priv->text);
6051 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6054 gtk_clipboard_set_text (clipboard, priv->text + start, end - start);
6059 link = gtk_label_get_focus_link (label);
6061 gtk_clipboard_set_text (clipboard, link->uri, -1);
6067 gtk_label_select_all (GtkLabel *label)
6069 GtkLabelPriv *priv = label->priv;
6071 gtk_label_select_region_index (label, 0, strlen (priv->text));
6074 /* Quick hack of a popup menu
6077 activate_cb (GtkWidget *menuitem,
6080 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6081 g_signal_emit_by_name (label, signal);
6085 append_action_signal (GtkLabel *label,
6087 const gchar *stock_id,
6088 const gchar *signal,
6091 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6093 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
6094 g_signal_connect (menuitem, "activate",
6095 G_CALLBACK (activate_cb), label);
6097 gtk_widget_set_sensitive (menuitem, sensitive);
6099 gtk_widget_show (menuitem);
6100 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6104 popup_menu_detach (GtkWidget *attach_widget,
6107 GtkLabel *label = GTK_LABEL (attach_widget);
6108 GtkLabelPriv *priv = label->priv;
6110 if (priv->select_info)
6111 priv->select_info->popup_menu = NULL;
6115 popup_position_func (GtkMenu *menu,
6123 GtkAllocation allocation;
6127 label = GTK_LABEL (user_data);
6128 widget = GTK_WIDGET (label);
6130 g_return_if_fail (gtk_widget_get_realized (widget));
6132 screen = gtk_widget_get_screen (widget);
6133 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
6135 gtk_widget_get_allocation (widget, &allocation);
6140 gtk_widget_size_request (GTK_WIDGET (menu), &req);
6142 gtk_widget_get_allocation (widget, &allocation);
6144 *x += allocation.width / 2;
6145 *y += allocation.height;
6147 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
6148 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
6152 open_link_activate_cb (GtkMenuItem *menu_item,
6157 link = gtk_label_get_current_link (label);
6160 emit_activate_link (label, link);
6164 copy_link_activate_cb (GtkMenuItem *menu_item,
6167 GtkClipboard *clipboard;
6170 uri = gtk_label_get_current_uri (label);
6173 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6174 gtk_clipboard_set_text (clipboard, uri, -1);
6179 gtk_label_popup_menu (GtkWidget *widget)
6181 gtk_label_do_popup (GTK_LABEL (widget), NULL);
6187 gtk_label_do_popup (GtkLabel *label,
6188 GdkEventButton *event)
6190 GtkLabelPriv *priv = label->priv;
6191 GtkWidget *menuitem;
6194 gboolean have_selection;
6197 if (!priv->select_info)
6200 if (priv->select_info->popup_menu)
6201 gtk_widget_destroy (priv->select_info->popup_menu);
6203 priv->select_info->popup_menu = menu = gtk_menu_new ();
6205 gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (label), popup_menu_detach);
6208 priv->select_info->selection_anchor != priv->select_info->selection_end;
6212 if (priv->select_info->link_clicked)
6213 link = priv->select_info->active_link;
6218 link = gtk_label_get_focus_link (label);
6220 if (!have_selection && link)
6223 menuitem = gtk_image_menu_item_new_with_mnemonic (_("_Open Link"));
6224 gtk_widget_show (menuitem);
6225 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6227 g_signal_connect (G_OBJECT (menuitem), "activate",
6228 G_CALLBACK (open_link_activate_cb), label);
6230 image = gtk_image_new_from_stock (GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_MENU);
6231 gtk_widget_show (image);
6232 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6234 /* Copy Link Address */
6235 menuitem = gtk_image_menu_item_new_with_mnemonic (_("Copy _Link Address"));
6236 gtk_widget_show (menuitem);
6237 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6239 g_signal_connect (G_OBJECT (menuitem), "activate",
6240 G_CALLBACK (copy_link_activate_cb), label);
6242 image = gtk_image_new_from_stock (GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
6243 gtk_widget_show (image);
6244 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6248 append_action_signal (label, menu, GTK_STOCK_CUT, "cut-clipboard", FALSE);
6249 append_action_signal (label, menu, GTK_STOCK_COPY, "copy-clipboard", have_selection);
6250 append_action_signal (label, menu, GTK_STOCK_PASTE, "paste-clipboard", FALSE);
6252 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
6253 gtk_widget_set_sensitive (menuitem, FALSE);
6254 gtk_widget_show (menuitem);
6255 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6257 menuitem = gtk_separator_menu_item_new ();
6258 gtk_widget_show (menuitem);
6259 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6261 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
6262 g_signal_connect_swapped (menuitem, "activate",
6263 G_CALLBACK (gtk_label_select_all), label);
6264 gtk_widget_show (menuitem);
6265 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6268 g_signal_emit (label, signals[POPULATE_POPUP], 0, menu);
6271 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6273 event->button, event->time);
6276 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6277 popup_position_func, label,
6278 0, gtk_get_current_event_time ());
6279 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
6284 gtk_label_clear_links (GtkLabel *label)
6286 GtkLabelPriv *priv = label->priv;
6288 if (!priv->select_info)
6291 g_list_foreach (priv->select_info->links, (GFunc)link_free, NULL);
6292 g_list_free (priv->select_info->links);
6293 priv->select_info->links = NULL;
6294 priv->select_info->active_link = NULL;
6298 gtk_label_rescan_links (GtkLabel *label)
6300 GtkLabelPriv *priv = label->priv;
6301 PangoLayout *layout = priv->layout;
6302 PangoAttrList *attlist;
6303 PangoAttrIterator *iter;
6306 if (!priv->select_info || !priv->select_info->links)
6309 attlist = pango_layout_get_attributes (layout);
6311 if (attlist == NULL)
6314 iter = pango_attr_list_get_iterator (attlist);
6316 links = priv->select_info->links;
6320 PangoAttribute *underline;
6321 PangoAttribute *color;
6323 underline = pango_attr_iterator_get (iter, PANGO_ATTR_UNDERLINE);
6324 color = pango_attr_iterator_get (iter, PANGO_ATTR_FOREGROUND);
6326 if (underline != NULL && color != NULL)
6329 PangoRectangle start_pos;
6330 PangoRectangle end_pos;
6333 pango_attr_iterator_range (iter, &start, &end);
6334 pango_layout_index_to_pos (layout, start, &start_pos);
6335 pango_layout_index_to_pos (layout, end, &end_pos);
6339 g_warning ("Ran out of links");
6343 links = links->next;
6344 link->start = start;
6347 } while (pango_attr_iterator_next (iter));
6349 pango_attr_iterator_destroy (iter);
6353 gtk_label_activate_link (GtkLabel *label,
6356 GtkWidget *widget = GTK_WIDGET (label);
6357 GError *error = NULL;
6359 if (!gtk_show_uri (gtk_widget_get_screen (widget),
6360 uri, gtk_get_current_event_time (), &error))
6362 g_warning ("Unable to show '%s': %s", uri, error->message);
6363 g_error_free (error);
6370 emit_activate_link (GtkLabel *label,
6373 GtkLabelPriv *priv = label->priv;
6376 g_signal_emit (label, signals[ACTIVATE_LINK], 0, link->uri, &handled);
6377 if (handled && priv->track_links && !link->visited)
6379 link->visited = TRUE;
6380 /* FIXME: shouldn't have to redo everything here */
6381 gtk_label_recalculate (label);
6386 gtk_label_activate_current_link (GtkLabel *label)
6389 GtkWidget *widget = GTK_WIDGET (label);
6391 link = gtk_label_get_focus_link (label);
6395 emit_activate_link (label, link);
6399 GtkWidget *toplevel;
6402 toplevel = gtk_widget_get_toplevel (widget);
6403 if (GTK_IS_WINDOW (toplevel))
6405 window = GTK_WINDOW (toplevel);
6408 window->default_widget != widget &&
6409 !(widget == window->focus_widget &&
6410 (!window->default_widget || !gtk_widget_is_sensitive (window->default_widget))))
6411 gtk_window_activate_default (window);
6416 static GtkLabelLink *
6417 gtk_label_get_current_link (GtkLabel *label)
6419 GtkLabelPriv *priv = label->priv;
6422 if (!priv->select_info)
6425 if (priv->select_info->link_clicked)
6426 link = priv->select_info->active_link;
6428 link = gtk_label_get_focus_link (label);
6434 * gtk_label_get_current_uri:
6435 * @label: a #GtkLabel
6437 * Returns the URI for the currently active link in the label.
6438 * The active link is the one under the mouse pointer or, in a
6439 * selectable label, the link in which the text cursor is currently
6442 * This function is intended for use in a #GtkLabel::activate-link handler
6443 * or for use in a #GtkWidget::query-tooltip handler.
6445 * Returns: the currently active URI. The string is owned by GTK+ and must
6446 * not be freed or modified.
6450 G_CONST_RETURN gchar *
6451 gtk_label_get_current_uri (GtkLabel *label)
6455 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
6457 link = gtk_label_get_current_link (label);
6466 * gtk_label_set_track_visited_links:
6467 * @label: a #GtkLabel
6468 * @track_links: %TRUE to track visited links
6470 * Sets whether the label should keep track of clicked
6471 * links (and use a different color for them).
6476 gtk_label_set_track_visited_links (GtkLabel *label,
6477 gboolean track_links)
6481 g_return_if_fail (GTK_IS_LABEL (label));
6485 track_links = track_links != FALSE;
6487 if (priv->track_links != track_links)
6489 priv->track_links = track_links;
6491 /* FIXME: shouldn't have to redo everything here */
6492 gtk_label_recalculate (label);
6494 g_object_notify (G_OBJECT (label), "track-visited-links");
6499 * gtk_label_get_track_visited_links:
6500 * @label: a #GtkLabel
6502 * Returns whether the label is currently keeping track
6505 * Returns: %TRUE if clicked links are remembered
6510 gtk_label_get_track_visited_links (GtkLabel *label)
6512 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
6514 return label->priv->track_links;
6518 gtk_label_query_tooltip (GtkWidget *widget,
6521 gboolean keyboard_tip,
6522 GtkTooltip *tooltip)
6524 GtkLabel *label = GTK_LABEL (widget);
6525 GtkLabelPriv *priv = label->priv;
6526 GtkLabelSelectionInfo *info = priv->select_info;
6530 if (info && info->links)
6534 if (info->selection_anchor == info->selection_end)
6535 index = info->selection_anchor;
6539 if (!get_layout_index (label, x, y, &index))
6545 for (l = info->links; l != NULL; l = l->next)
6547 GtkLabelLink *link = l->data;
6548 if (index >= link->start && index <= link->end)
6552 gtk_tooltip_set_markup (tooltip, link->title);
6561 return GTK_WIDGET_CLASS (gtk_label_parent_class)->query_tooltip (widget,