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"
36 #include "gtkwindow.h"
37 #include "gdk/gdkkeysyms.h"
38 #include "gtkclipboard.h"
39 #include "gtkimagemenuitem.h"
41 #include "gtkseparatormenuitem.h"
42 #include "gtktextutil.h"
43 #include "gtkmenuitem.h"
44 #include "gtknotebook.h"
46 #include "gtkbindings.h"
47 #include "gtkbuildable.h"
50 #include "gtktooltip.h"
51 #include "gtksizerequest.h"
52 #include "gtkprivate.h"
57 GtkLabelSelectionInfo *select_info;
58 GtkWidget *mnemonic_widget;
59 GtkWindow *mnemonic_window;
62 PangoAttrList *effective_attrs;
65 gboolean mnemonics_visible;
72 guint use_underline : 1;
75 guint single_line_mode : 1;
76 guint have_transform : 1;
79 guint pattern_set : 1;
80 guint track_links : 1;
82 guint mnemonic_keyval;
89 /* Notes about the handling of links:
91 * Links share the GtkLabelSelectionInfo struct with selectable labels.
92 * There are some new fields for links. The links field contains the list
93 * of GtkLabelLink structs that describe the links which are embedded in
94 * the label. The active_link field points to the link under the mouse
95 * pointer. For keyboard navigation, the 'focus' link is determined by
96 * finding the link which contains the selection_anchor position.
97 * The link_clicked field is used with button press and release events
98 * to ensure that pressing inside a link and releasing outside of it
99 * does not activate the link.
101 * Links are rendered with the link-color/visited-link-color colors
102 * that are determined by the style and with an underline. When the mouse
103 * pointer is over a link, the pointer is changed to indicate the link,
104 * and the background behind the link is rendered with the base[PRELIGHT]
105 * color. While a button is pressed over a link, the background is rendered
106 * with the base[ACTIVE] color.
108 * Labels with links accept keyboard focus, and it is possible to move
109 * the focus between the embedded links using Tab/Shift-Tab. The focus
110 * is indicated by a focus rectangle that is drawn around the link text.
111 * Pressing Enter activates the focussed link, and there is a suitable
112 * context menu for links that can be opened with the Menu key. Pressing
113 * Control-C copies the link URI to the clipboard.
115 * In selectable labels with links, link functionality is only available
116 * when the selection is empty.
121 gchar *title; /* the title attribute, used as tooltip */
122 gboolean visited; /* get set when the link is activated; this flag
123 * gets preserved over later set_markup() calls
125 gint start; /* position of the link in the PangoLayout */
129 struct _GtkLabelSelectionInfo
132 gint selection_anchor;
134 GtkWidget *popup_menu;
137 GtkLabelLink *active_link;
143 guint select_words : 1;
144 guint selectable : 1;
145 guint link_clicked : 1;
153 ACTIVATE_CURRENT_LINK,
168 PROP_MNEMONIC_KEYVAL,
169 PROP_MNEMONIC_WIDGET,
170 PROP_CURSOR_POSITION,
171 PROP_SELECTION_BOUND,
174 PROP_SINGLE_LINE_MODE,
176 PROP_MAX_WIDTH_CHARS,
177 PROP_TRACK_VISITED_LINKS
180 /* When rotating ellipsizable text we want the natural size to request
181 * more to ensure the label wont ever ellipsize in an allocation of full natural size.
183 #define ROTATION_ELLIPSIZE_PADDING 2
185 static guint signals[LAST_SIGNAL] = { 0 };
187 static const GdkColor default_link_color = { 0, 0, 0, 0xeeee };
188 static const GdkColor default_visited_link_color = { 0, 0x5555, 0x1a1a, 0x8b8b };
190 static void gtk_label_set_property (GObject *object,
194 static void gtk_label_get_property (GObject *object,
198 static void gtk_label_destroy (GtkObject *object);
199 static void gtk_label_finalize (GObject *object);
200 static void gtk_label_size_allocate (GtkWidget *widget,
201 GtkAllocation *allocation);
202 static void gtk_label_state_changed (GtkWidget *widget,
204 static void gtk_label_style_set (GtkWidget *widget,
205 GtkStyle *previous_style);
206 static void gtk_label_direction_changed (GtkWidget *widget,
207 GtkTextDirection previous_dir);
208 static gint gtk_label_expose (GtkWidget *widget,
209 GdkEventExpose *event);
210 static gboolean gtk_label_focus (GtkWidget *widget,
211 GtkDirectionType direction);
213 static void gtk_label_realize (GtkWidget *widget);
214 static void gtk_label_unrealize (GtkWidget *widget);
215 static void gtk_label_map (GtkWidget *widget);
216 static void gtk_label_unmap (GtkWidget *widget);
218 static gboolean gtk_label_button_press (GtkWidget *widget,
219 GdkEventButton *event);
220 static gboolean gtk_label_button_release (GtkWidget *widget,
221 GdkEventButton *event);
222 static gboolean gtk_label_motion (GtkWidget *widget,
223 GdkEventMotion *event);
224 static gboolean gtk_label_leave_notify (GtkWidget *widget,
225 GdkEventCrossing *event);
227 static void gtk_label_grab_focus (GtkWidget *widget);
229 static gboolean gtk_label_query_tooltip (GtkWidget *widget,
232 gboolean keyboard_tip,
233 GtkTooltip *tooltip);
235 static void gtk_label_set_text_internal (GtkLabel *label,
237 static void gtk_label_set_label_internal (GtkLabel *label,
239 static void gtk_label_set_use_markup_internal (GtkLabel *label,
241 static void gtk_label_set_use_underline_internal (GtkLabel *label,
243 static void gtk_label_set_attributes_internal (GtkLabel *label,
244 PangoAttrList *attrs);
245 static void gtk_label_set_uline_text_internal (GtkLabel *label,
247 static void gtk_label_set_pattern_internal (GtkLabel *label,
248 const gchar *pattern,
249 gboolean is_mnemonic);
250 static void gtk_label_set_markup_internal (GtkLabel *label,
252 gboolean with_uline);
253 static void gtk_label_recalculate (GtkLabel *label);
254 static void gtk_label_hierarchy_changed (GtkWidget *widget,
255 GtkWidget *old_toplevel);
256 static void gtk_label_screen_changed (GtkWidget *widget,
257 GdkScreen *old_screen);
258 static gboolean gtk_label_popup_menu (GtkWidget *widget);
260 static void gtk_label_create_window (GtkLabel *label);
261 static void gtk_label_destroy_window (GtkLabel *label);
262 static void gtk_label_ensure_select_info (GtkLabel *label);
263 static void gtk_label_clear_select_info (GtkLabel *label);
264 static void gtk_label_update_cursor (GtkLabel *label);
265 static void gtk_label_clear_layout (GtkLabel *label);
266 static void gtk_label_ensure_layout (GtkLabel *label, gboolean guess_wrap_width);
267 static void gtk_label_invalidate_wrap_width (GtkLabel *label);
268 static void gtk_label_select_region_index (GtkLabel *label,
272 static gboolean gtk_label_mnemonic_activate (GtkWidget *widget,
273 gboolean group_cycling);
274 static void gtk_label_setup_mnemonic (GtkLabel *label,
276 static void gtk_label_drag_data_get (GtkWidget *widget,
277 GdkDragContext *context,
278 GtkSelectionData *selection_data,
282 static void gtk_label_buildable_interface_init (GtkBuildableIface *iface);
283 static gboolean gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
286 const gchar *tagname,
287 GMarkupParser *parser,
290 static void gtk_label_buildable_custom_finished (GtkBuildable *buildable,
293 const gchar *tagname,
297 static void connect_mnemonics_visible_notify (GtkLabel *label);
298 static gboolean separate_uline_pattern (const gchar *str,
304 /* For selectable labels: */
305 static void gtk_label_move_cursor (GtkLabel *label,
306 GtkMovementStep step,
308 gboolean extend_selection);
309 static void gtk_label_copy_clipboard (GtkLabel *label);
310 static void gtk_label_select_all (GtkLabel *label);
311 static void gtk_label_do_popup (GtkLabel *label,
312 GdkEventButton *event);
313 static gint gtk_label_move_forward_word (GtkLabel *label,
315 static gint gtk_label_move_backward_word (GtkLabel *label,
319 static void gtk_label_rescan_links (GtkLabel *label);
320 static void gtk_label_clear_links (GtkLabel *label);
321 static gboolean gtk_label_activate_link (GtkLabel *label,
323 static void gtk_label_activate_current_link (GtkLabel *label);
324 static GtkLabelLink *gtk_label_get_current_link (GtkLabel *label);
325 static void gtk_label_get_link_colors (GtkWidget *widget,
326 GdkColor **link_color,
327 GdkColor **visited_link_color);
328 static void emit_activate_link (GtkLabel *label,
331 static void gtk_label_size_request_init (GtkSizeRequestIface *iface);
332 static GtkSizeRequestMode gtk_label_get_request_mode (GtkSizeRequest *widget);
333 static void gtk_label_get_width (GtkSizeRequest *widget,
336 static void gtk_label_get_height (GtkSizeRequest *widget,
339 static void gtk_label_get_width_for_height (GtkSizeRequest *widget,
342 gint *natural_width);
343 static void gtk_label_get_height_for_width (GtkSizeRequest *widget,
345 gint *minimum_height,
346 gint *natural_height);
348 static GQuark quark_angle = 0;
350 static GtkBuildableIface *buildable_parent_iface = NULL;
352 G_DEFINE_TYPE_WITH_CODE (GtkLabel, gtk_label, GTK_TYPE_MISC,
353 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
354 gtk_label_buildable_interface_init)
355 G_IMPLEMENT_INTERFACE (GTK_TYPE_SIZE_REQUEST,
356 gtk_label_size_request_init));
359 add_move_binding (GtkBindingSet *binding_set,
362 GtkMovementStep step,
365 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
367 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
371 G_TYPE_BOOLEAN, FALSE);
373 /* Selection-extending version */
374 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
378 G_TYPE_BOOLEAN, TRUE);
382 gtk_label_class_init (GtkLabelClass *class)
384 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
385 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
386 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
387 GtkBindingSet *binding_set;
389 quark_angle = g_quark_from_static_string ("angle");
391 gobject_class->set_property = gtk_label_set_property;
392 gobject_class->get_property = gtk_label_get_property;
393 gobject_class->finalize = gtk_label_finalize;
395 object_class->destroy = gtk_label_destroy;
397 widget_class->size_allocate = gtk_label_size_allocate;
398 widget_class->state_changed = gtk_label_state_changed;
399 widget_class->style_set = gtk_label_style_set;
400 widget_class->query_tooltip = gtk_label_query_tooltip;
401 widget_class->direction_changed = gtk_label_direction_changed;
402 widget_class->expose_event = gtk_label_expose;
403 widget_class->realize = gtk_label_realize;
404 widget_class->unrealize = gtk_label_unrealize;
405 widget_class->map = gtk_label_map;
406 widget_class->unmap = gtk_label_unmap;
407 widget_class->button_press_event = gtk_label_button_press;
408 widget_class->button_release_event = gtk_label_button_release;
409 widget_class->motion_notify_event = gtk_label_motion;
410 widget_class->leave_notify_event = gtk_label_leave_notify;
411 widget_class->hierarchy_changed = gtk_label_hierarchy_changed;
412 widget_class->screen_changed = gtk_label_screen_changed;
413 widget_class->mnemonic_activate = gtk_label_mnemonic_activate;
414 widget_class->drag_data_get = gtk_label_drag_data_get;
415 widget_class->grab_focus = gtk_label_grab_focus;
416 widget_class->popup_menu = gtk_label_popup_menu;
417 widget_class->focus = gtk_label_focus;
419 class->move_cursor = gtk_label_move_cursor;
420 class->copy_clipboard = gtk_label_copy_clipboard;
421 class->activate_link = gtk_label_activate_link;
424 * GtkLabel::move-cursor:
425 * @entry: the object which received the signal
426 * @step: the granularity of the move, as a #GtkMovementStep
427 * @count: the number of @step units to move
428 * @extend_selection: %TRUE if the move should extend the selection
430 * The ::move-cursor signal is a
431 * <link linkend="keybinding-signals">keybinding signal</link>
432 * which gets emitted when the user initiates a cursor movement.
433 * If the cursor is not visible in @entry, this signal causes
434 * the viewport to be moved instead.
436 * Applications should not connect to it, but may emit it with
437 * g_signal_emit_by_name() if they need to control the cursor
440 * The default bindings for this signal come in two variants,
441 * the variant with the Shift modifier extends the selection,
442 * the variant without the Shift modifer does not.
443 * There are too many key combinations to list them all here.
445 * <listitem>Arrow keys move by individual characters/lines</listitem>
446 * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
447 * <listitem>Home/End keys move to the ends of the buffer</listitem>
450 signals[MOVE_CURSOR] =
451 g_signal_new (I_("move-cursor"),
452 G_OBJECT_CLASS_TYPE (gobject_class),
453 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
454 G_STRUCT_OFFSET (GtkLabelClass, move_cursor),
456 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
458 GTK_TYPE_MOVEMENT_STEP,
463 * GtkLabel::copy-clipboard:
464 * @label: the object which received the signal
466 * The ::copy-clipboard signal is a
467 * <link linkend="keybinding-signals">keybinding signal</link>
468 * which gets emitted to copy the selection to the clipboard.
470 * The default binding for this signal is Ctrl-c.
472 signals[COPY_CLIPBOARD] =
473 g_signal_new (I_("copy-clipboard"),
474 G_OBJECT_CLASS_TYPE (gobject_class),
475 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
476 G_STRUCT_OFFSET (GtkLabelClass, copy_clipboard),
478 _gtk_marshal_VOID__VOID,
482 * GtkLabel::populate-popup:
483 * @label: The label on which the signal is emitted
484 * @menu: the menu that is being populated
486 * The ::populate-popup signal gets emitted before showing the
487 * context menu of the label. Note that only selectable labels
488 * have context menus.
490 * If you need to add items to the context menu, connect
491 * to this signal and append your menuitems to the @menu.
493 signals[POPULATE_POPUP] =
494 g_signal_new (I_("populate-popup"),
495 G_OBJECT_CLASS_TYPE (gobject_class),
497 G_STRUCT_OFFSET (GtkLabelClass, populate_popup),
499 _gtk_marshal_VOID__OBJECT,
504 * GtkLabel::activate-current-link:
505 * @label: The label on which the signal was emitted
507 * A <link linkend="keybinding-signals">keybinding signal</link>
508 * which gets emitted when the user activates a link in the label.
510 * Applications may also emit the signal with g_signal_emit_by_name()
511 * if they need to control activation of URIs programmatically.
513 * The default bindings for this signal are all forms of the Enter key.
517 signals[ACTIVATE_CURRENT_LINK] =
518 g_signal_new_class_handler ("activate-current-link",
519 G_TYPE_FROM_CLASS (object_class),
520 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
521 G_CALLBACK (gtk_label_activate_current_link),
523 _gtk_marshal_VOID__VOID,
527 * GtkLabel::activate-link:
528 * @label: The label on which the signal was emitted
529 * @uri: the URI that is activated
531 * The signal which gets emitted to activate a URI.
532 * Applications may connect to it to override the default behaviour,
533 * which is to call gtk_show_uri().
535 * Returns: %TRUE if the link has been activated
539 signals[ACTIVATE_LINK] =
540 g_signal_new ("activate-link",
541 G_TYPE_FROM_CLASS (object_class),
543 G_STRUCT_OFFSET (GtkLabelClass, activate_link),
544 _gtk_boolean_handled_accumulator, NULL,
545 _gtk_marshal_BOOLEAN__STRING,
546 G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
548 g_object_class_install_property (gobject_class,
550 g_param_spec_string ("label",
552 P_("The text of the label"),
554 GTK_PARAM_READWRITE));
555 g_object_class_install_property (gobject_class,
557 g_param_spec_boxed ("attributes",
559 P_("A list of style attributes to apply to the text of the label"),
560 PANGO_TYPE_ATTR_LIST,
561 GTK_PARAM_READWRITE));
562 g_object_class_install_property (gobject_class,
564 g_param_spec_boolean ("use-markup",
566 P_("The text of the label includes XML markup. See pango_parse_markup()"),
568 GTK_PARAM_READWRITE));
569 g_object_class_install_property (gobject_class,
571 g_param_spec_boolean ("use-underline",
573 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
575 GTK_PARAM_READWRITE));
577 g_object_class_install_property (gobject_class,
579 g_param_spec_enum ("justify",
581 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"),
582 GTK_TYPE_JUSTIFICATION,
584 GTK_PARAM_READWRITE));
586 g_object_class_install_property (gobject_class,
588 g_param_spec_string ("pattern",
590 P_("A string with _ characters in positions correspond to characters in the text to underline"),
592 GTK_PARAM_WRITABLE));
594 g_object_class_install_property (gobject_class,
596 g_param_spec_boolean ("wrap",
598 P_("If set, wrap lines if the text becomes too wide"),
600 GTK_PARAM_READWRITE));
602 * GtkLabel:wrap-mode:
604 * If line wrapping is on (see the #GtkLabel:wrap property) this controls
605 * how the line wrapping is done. The default is %PANGO_WRAP_WORD, which
606 * means wrap on word boundaries.
610 g_object_class_install_property (gobject_class,
612 g_param_spec_enum ("wrap-mode",
613 P_("Line wrap mode"),
614 P_("If wrap is set, controls how linewrapping is done"),
615 PANGO_TYPE_WRAP_MODE,
617 GTK_PARAM_READWRITE));
618 g_object_class_install_property (gobject_class,
620 g_param_spec_boolean ("selectable",
622 P_("Whether the label text can be selected with the mouse"),
624 GTK_PARAM_READWRITE));
625 g_object_class_install_property (gobject_class,
626 PROP_MNEMONIC_KEYVAL,
627 g_param_spec_uint ("mnemonic-keyval",
629 P_("The mnemonic accelerator key for this label"),
633 GTK_PARAM_READABLE));
634 g_object_class_install_property (gobject_class,
635 PROP_MNEMONIC_WIDGET,
636 g_param_spec_object ("mnemonic-widget",
637 P_("Mnemonic widget"),
638 P_("The widget to be activated when the label's mnemonic "
641 GTK_PARAM_READWRITE));
643 g_object_class_install_property (gobject_class,
644 PROP_CURSOR_POSITION,
645 g_param_spec_int ("cursor-position",
646 P_("Cursor Position"),
647 P_("The current position of the insertion cursor in chars"),
651 GTK_PARAM_READABLE));
653 g_object_class_install_property (gobject_class,
654 PROP_SELECTION_BOUND,
655 g_param_spec_int ("selection-bound",
656 P_("Selection Bound"),
657 P_("The position of the opposite end of the selection from the cursor in chars"),
661 GTK_PARAM_READABLE));
664 * GtkLabel:ellipsize:
666 * The preferred place to ellipsize the string, if the label does
667 * not have enough room to display the entire string, specified as a
668 * #PangoEllisizeMode.
670 * Note that setting this property to a value other than
671 * %PANGO_ELLIPSIZE_NONE has the side-effect that the label requests
672 * only enough space to display the ellipsis "...". In particular, this
673 * means that ellipsizing labels do not work well in notebook tabs, unless
674 * the tab's #GtkNotebook:tab-expand property is set to %TRUE. Other ways
675 * to set a label's width are gtk_widget_set_size_request() and
676 * gtk_label_set_width_chars().
680 g_object_class_install_property (gobject_class,
682 g_param_spec_enum ("ellipsize",
684 P_("The preferred place to ellipsize the string, if the label does not have enough room to display the entire string"),
685 PANGO_TYPE_ELLIPSIZE_MODE,
686 PANGO_ELLIPSIZE_NONE,
687 GTK_PARAM_READWRITE));
690 * GtkLabel:width-chars:
692 * The desired width of the label, in characters. If this property is set to
693 * -1, the width will be calculated automatically.
695 * See the section on <link linkend="label-text-layout">text layout</link>
696 * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
697 * determine the width of ellipsized and wrapped labels.
701 g_object_class_install_property (gobject_class,
703 g_param_spec_int ("width-chars",
704 P_("Width In Characters"),
705 P_("The desired width of the label, in characters"),
709 GTK_PARAM_READWRITE));
712 * GtkLabel:single-line-mode:
714 * Whether the label is in single line mode. In single line mode,
715 * the height of the label does not depend on the actual text, it
716 * is always set to ascent + descent of the font. This can be an
717 * advantage in situations where resizing the label because of text
718 * changes would be distracting, e.g. in a statusbar.
722 g_object_class_install_property (gobject_class,
723 PROP_SINGLE_LINE_MODE,
724 g_param_spec_boolean ("single-line-mode",
725 P_("Single Line Mode"),
726 P_("Whether the label is in single line mode"),
728 GTK_PARAM_READWRITE));
733 * The angle that the baseline of the label makes with the horizontal,
734 * in degrees, measured counterclockwise. An angle of 90 reads from
735 * from bottom to top, an angle of 270, from top to bottom. Ignored
736 * if the label is selectable, wrapped, or ellipsized.
740 g_object_class_install_property (gobject_class,
742 g_param_spec_double ("angle",
744 P_("Angle at which the label is rotated"),
748 GTK_PARAM_READWRITE));
751 * GtkLabel:max-width-chars:
753 * The desired maximum width of the label, in characters. If this property
754 * is set to -1, the width will be calculated automatically.
756 * See the section on <link linkend="label-text-layout">text layout</link>
757 * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
758 * determine the width of ellipsized and wrapped labels.
762 g_object_class_install_property (gobject_class,
763 PROP_MAX_WIDTH_CHARS,
764 g_param_spec_int ("max-width-chars",
765 P_("Maximum Width In Characters"),
766 P_("The desired maximum width of the label, in characters"),
770 GTK_PARAM_READWRITE));
773 * GtkLabel:track-visited-links:
775 * Set this property to %TRUE to make the label track which links
776 * have been clicked. It will then apply the ::visited-link-color
777 * color, instead of ::link-color.
781 g_object_class_install_property (gobject_class,
782 PROP_TRACK_VISITED_LINKS,
783 g_param_spec_boolean ("track-visited-links",
784 P_("Track visited links"),
785 P_("Whether visited links should be tracked"),
787 GTK_PARAM_READWRITE));
792 binding_set = gtk_binding_set_by_class (class);
794 /* Moving the insertion point */
795 add_move_binding (binding_set, GDK_Right, 0,
796 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
798 add_move_binding (binding_set, GDK_Left, 0,
799 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
801 add_move_binding (binding_set, GDK_KP_Right, 0,
802 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
804 add_move_binding (binding_set, GDK_KP_Left, 0,
805 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
807 add_move_binding (binding_set, GDK_f, GDK_CONTROL_MASK,
808 GTK_MOVEMENT_LOGICAL_POSITIONS, 1);
810 add_move_binding (binding_set, GDK_b, GDK_CONTROL_MASK,
811 GTK_MOVEMENT_LOGICAL_POSITIONS, -1);
813 add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
814 GTK_MOVEMENT_WORDS, 1);
816 add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
817 GTK_MOVEMENT_WORDS, -1);
819 add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
820 GTK_MOVEMENT_WORDS, 1);
822 add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
823 GTK_MOVEMENT_WORDS, -1);
826 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
828 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
830 G_TYPE_BOOLEAN, FALSE);
832 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
834 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
836 G_TYPE_BOOLEAN, TRUE);
838 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
840 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
842 G_TYPE_BOOLEAN, FALSE);
844 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
846 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
848 G_TYPE_BOOLEAN, TRUE);
851 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
853 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
855 G_TYPE_BOOLEAN, FALSE);
857 gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_CONTROL_MASK,
859 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
861 G_TYPE_BOOLEAN, FALSE);
863 add_move_binding (binding_set, GDK_f, GDK_MOD1_MASK,
864 GTK_MOVEMENT_WORDS, 1);
866 add_move_binding (binding_set, GDK_b, GDK_MOD1_MASK,
867 GTK_MOVEMENT_WORDS, -1);
869 add_move_binding (binding_set, GDK_Home, 0,
870 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
872 add_move_binding (binding_set, GDK_End, 0,
873 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
875 add_move_binding (binding_set, GDK_KP_Home, 0,
876 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
878 add_move_binding (binding_set, GDK_KP_End, 0,
879 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
881 add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
882 GTK_MOVEMENT_BUFFER_ENDS, -1);
884 add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
885 GTK_MOVEMENT_BUFFER_ENDS, 1);
887 add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
888 GTK_MOVEMENT_BUFFER_ENDS, -1);
890 add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
891 GTK_MOVEMENT_BUFFER_ENDS, 1);
894 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
895 "copy-clipboard", 0);
897 gtk_binding_entry_add_signal (binding_set, GDK_Return, 0,
898 "activate-current-link", 0);
899 gtk_binding_entry_add_signal (binding_set, GDK_ISO_Enter, 0,
900 "activate-current-link", 0);
901 gtk_binding_entry_add_signal (binding_set, GDK_KP_Enter, 0,
902 "activate-current-link", 0);
904 gtk_settings_install_property (g_param_spec_boolean ("gtk-label-select-on-focus",
905 P_("Select on focus"),
906 P_("Whether to select the contents of a selectable label when it is focused"),
908 GTK_PARAM_READWRITE));
910 g_type_class_add_private (class, sizeof (GtkLabelPriv));
914 gtk_label_set_property (GObject *object,
919 GtkLabel *label = GTK_LABEL (object);
924 gtk_label_set_label (label, g_value_get_string (value));
926 case PROP_ATTRIBUTES:
927 gtk_label_set_attributes (label, g_value_get_boxed (value));
929 case PROP_USE_MARKUP:
930 gtk_label_set_use_markup (label, g_value_get_boolean (value));
932 case PROP_USE_UNDERLINE:
933 gtk_label_set_use_underline (label, g_value_get_boolean (value));
936 gtk_label_set_justify (label, g_value_get_enum (value));
939 gtk_label_set_pattern (label, g_value_get_string (value));
942 gtk_label_set_line_wrap (label, g_value_get_boolean (value));
945 gtk_label_set_line_wrap_mode (label, g_value_get_enum (value));
947 case PROP_SELECTABLE:
948 gtk_label_set_selectable (label, g_value_get_boolean (value));
950 case PROP_MNEMONIC_WIDGET:
951 gtk_label_set_mnemonic_widget (label, (GtkWidget*) g_value_get_object (value));
954 gtk_label_set_ellipsize (label, g_value_get_enum (value));
956 case PROP_WIDTH_CHARS:
957 gtk_label_set_width_chars (label, g_value_get_int (value));
959 case PROP_SINGLE_LINE_MODE:
960 gtk_label_set_single_line_mode (label, g_value_get_boolean (value));
963 gtk_label_set_angle (label, g_value_get_double (value));
965 case PROP_MAX_WIDTH_CHARS:
966 gtk_label_set_max_width_chars (label, g_value_get_int (value));
968 case PROP_TRACK_VISITED_LINKS:
969 gtk_label_set_track_visited_links (label, g_value_get_boolean (value));
972 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
978 gtk_label_get_property (GObject *object,
983 GtkLabel *label = GTK_LABEL (object);
984 GtkLabelPriv *priv = label->priv;
989 g_value_set_string (value, priv->label);
991 case PROP_ATTRIBUTES:
992 g_value_set_boxed (value, priv->attrs);
994 case PROP_USE_MARKUP:
995 g_value_set_boolean (value, priv->use_markup);
997 case PROP_USE_UNDERLINE:
998 g_value_set_boolean (value, priv->use_underline);
1001 g_value_set_enum (value, priv->jtype);
1004 g_value_set_boolean (value, priv->wrap);
1006 case PROP_WRAP_MODE:
1007 g_value_set_enum (value, priv->wrap_mode);
1009 case PROP_SELECTABLE:
1010 g_value_set_boolean (value, gtk_label_get_selectable (label));
1012 case PROP_MNEMONIC_KEYVAL:
1013 g_value_set_uint (value, priv->mnemonic_keyval);
1015 case PROP_MNEMONIC_WIDGET:
1016 g_value_set_object (value, (GObject*) priv->mnemonic_widget);
1018 case PROP_CURSOR_POSITION:
1019 if (priv->select_info && priv->select_info->selectable)
1021 gint offset = g_utf8_pointer_to_offset (priv->text,
1022 priv->text + priv->select_info->selection_end);
1023 g_value_set_int (value, offset);
1026 g_value_set_int (value, 0);
1028 case PROP_SELECTION_BOUND:
1029 if (priv->select_info && priv->select_info->selectable)
1031 gint offset = g_utf8_pointer_to_offset (priv->text,
1032 priv->text + priv->select_info->selection_anchor);
1033 g_value_set_int (value, offset);
1036 g_value_set_int (value, 0);
1038 case PROP_ELLIPSIZE:
1039 g_value_set_enum (value, priv->ellipsize);
1041 case PROP_WIDTH_CHARS:
1042 g_value_set_int (value, gtk_label_get_width_chars (label));
1044 case PROP_SINGLE_LINE_MODE:
1045 g_value_set_boolean (value, gtk_label_get_single_line_mode (label));
1048 g_value_set_double (value, gtk_label_get_angle (label));
1050 case PROP_MAX_WIDTH_CHARS:
1051 g_value_set_int (value, gtk_label_get_max_width_chars (label));
1053 case PROP_TRACK_VISITED_LINKS:
1054 g_value_set_boolean (value, gtk_label_get_track_visited_links (label));
1057 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1063 gtk_label_init (GtkLabel *label)
1067 label->priv = G_TYPE_INSTANCE_GET_PRIVATE (label,
1072 gtk_widget_set_has_window (GTK_WIDGET (label), FALSE);
1074 priv->width_chars = -1;
1075 priv->max_width_chars = -1;
1076 priv->wrap_width = -1;
1079 priv->jtype = GTK_JUSTIFY_LEFT;
1081 priv->wrap_mode = PANGO_WRAP_WORD;
1082 priv->ellipsize = PANGO_ELLIPSIZE_NONE;
1084 priv->use_underline = FALSE;
1085 priv->use_markup = FALSE;
1086 priv->pattern_set = FALSE;
1087 priv->track_links = TRUE;
1089 priv->mnemonic_keyval = GDK_VoidSymbol;
1090 priv->layout = NULL;
1094 priv->mnemonic_widget = NULL;
1095 priv->mnemonic_window = NULL;
1097 priv->mnemonics_visible = TRUE;
1099 gtk_label_set_text (label, "");
1104 gtk_label_buildable_interface_init (GtkBuildableIface *iface)
1106 buildable_parent_iface = g_type_interface_peek_parent (iface);
1108 iface->custom_tag_start = gtk_label_buildable_custom_tag_start;
1109 iface->custom_finished = gtk_label_buildable_custom_finished;
1113 GtkBuilder *builder;
1115 PangoAttrList *attrs;
1118 static PangoAttribute *
1119 attribute_from_text (GtkBuilder *builder,
1124 PangoAttribute *attribute = NULL;
1126 PangoLanguage *language;
1127 PangoFontDescription *font_desc;
1129 GValue val = { 0, };
1131 if (!gtk_builder_value_from_string_type (builder, PANGO_TYPE_ATTR_TYPE, name, &val, error))
1134 type = g_value_get_enum (&val);
1135 g_value_unset (&val);
1139 /* PangoAttrLanguage */
1140 case PANGO_ATTR_LANGUAGE:
1141 if ((language = pango_language_from_string (value)))
1143 attribute = pango_attr_language_new (language);
1144 g_value_init (&val, G_TYPE_INT);
1148 case PANGO_ATTR_STYLE:
1149 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STYLE, value, &val, error))
1150 attribute = pango_attr_style_new (g_value_get_enum (&val));
1152 case PANGO_ATTR_WEIGHT:
1153 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_WEIGHT, value, &val, error))
1154 attribute = pango_attr_weight_new (g_value_get_enum (&val));
1156 case PANGO_ATTR_VARIANT:
1157 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_VARIANT, value, &val, error))
1158 attribute = pango_attr_variant_new (g_value_get_enum (&val));
1160 case PANGO_ATTR_STRETCH:
1161 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STRETCH, value, &val, error))
1162 attribute = pango_attr_stretch_new (g_value_get_enum (&val));
1164 case PANGO_ATTR_UNDERLINE:
1165 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1166 attribute = pango_attr_underline_new (g_value_get_boolean (&val));
1168 case PANGO_ATTR_STRIKETHROUGH:
1169 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1170 attribute = pango_attr_strikethrough_new (g_value_get_boolean (&val));
1172 case PANGO_ATTR_GRAVITY:
1173 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY, value, &val, error))
1174 attribute = pango_attr_gravity_new (g_value_get_enum (&val));
1176 case PANGO_ATTR_GRAVITY_HINT:
1177 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY_HINT,
1178 value, &val, error))
1179 attribute = pango_attr_gravity_hint_new (g_value_get_enum (&val));
1181 /* PangoAttrString */
1182 case PANGO_ATTR_FAMILY:
1183 attribute = pango_attr_family_new (value);
1184 g_value_init (&val, G_TYPE_INT);
1188 case PANGO_ATTR_SIZE:
1189 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1190 value, &val, error))
1191 attribute = pango_attr_size_new (g_value_get_int (&val));
1193 case PANGO_ATTR_ABSOLUTE_SIZE:
1194 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1195 value, &val, error))
1196 attribute = pango_attr_size_new_absolute (g_value_get_int (&val));
1199 /* PangoAttrFontDesc */
1200 case PANGO_ATTR_FONT_DESC:
1201 if ((font_desc = pango_font_description_from_string (value)))
1203 attribute = pango_attr_font_desc_new (font_desc);
1204 pango_font_description_free (font_desc);
1205 g_value_init (&val, G_TYPE_INT);
1209 /* PangoAttrColor */
1210 case PANGO_ATTR_FOREGROUND:
1211 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1212 value, &val, error))
1214 color = g_value_get_boxed (&val);
1215 attribute = pango_attr_foreground_new (color->red, color->green, color->blue);
1218 case PANGO_ATTR_BACKGROUND:
1219 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1220 value, &val, error))
1222 color = g_value_get_boxed (&val);
1223 attribute = pango_attr_background_new (color->red, color->green, color->blue);
1226 case PANGO_ATTR_UNDERLINE_COLOR:
1227 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1228 value, &val, error))
1230 color = g_value_get_boxed (&val);
1231 attribute = pango_attr_underline_color_new (color->red, color->green, color->blue);
1234 case PANGO_ATTR_STRIKETHROUGH_COLOR:
1235 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1236 value, &val, error))
1238 color = g_value_get_boxed (&val);
1239 attribute = pango_attr_strikethrough_color_new (color->red, color->green, color->blue);
1243 /* PangoAttrShape */
1244 case PANGO_ATTR_SHAPE:
1245 /* Unsupported for now */
1247 /* PangoAttrFloat */
1248 case PANGO_ATTR_SCALE:
1249 if (gtk_builder_value_from_string_type (builder, G_TYPE_DOUBLE,
1250 value, &val, error))
1251 attribute = pango_attr_scale_new (g_value_get_double (&val));
1254 case PANGO_ATTR_INVALID:
1255 case PANGO_ATTR_LETTER_SPACING:
1256 case PANGO_ATTR_RISE:
1257 case PANGO_ATTR_FALLBACK:
1262 g_value_unset (&val);
1269 pango_start_element (GMarkupParseContext *context,
1270 const gchar *element_name,
1271 const gchar **names,
1272 const gchar **values,
1276 PangoParserData *data = (PangoParserData*)user_data;
1277 GValue val = { 0, };
1279 gint line_number, char_number;
1281 if (strcmp (element_name, "attribute") == 0)
1283 PangoAttribute *attr = NULL;
1284 const gchar *name = NULL;
1285 const gchar *value = NULL;
1286 const gchar *start = NULL;
1287 const gchar *end = NULL;
1288 guint start_val = 0;
1289 guint end_val = G_MAXUINT;
1291 for (i = 0; names[i]; i++)
1293 if (strcmp (names[i], "name") == 0)
1295 else if (strcmp (names[i], "value") == 0)
1297 else if (strcmp (names[i], "start") == 0)
1299 else if (strcmp (names[i], "end") == 0)
1303 g_markup_parse_context_get_position (context,
1308 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1309 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1311 line_number, char_number, names[i], "attribute");
1316 if (!name || !value)
1318 g_markup_parse_context_get_position (context,
1323 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1324 "%s:%d:%d <%s> requires attribute \"%s\"",
1326 line_number, char_number, "attribute",
1327 name ? "value" : "name");
1333 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1334 start, &val, error))
1336 start_val = g_value_get_uint (&val);
1337 g_value_unset (&val);
1342 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1345 end_val = g_value_get_uint (&val);
1346 g_value_unset (&val);
1349 attr = attribute_from_text (data->builder, name, value, error);
1350 attr->start_index = start_val;
1351 attr->end_index = end_val;
1356 data->attrs = pango_attr_list_new ();
1358 pango_attr_list_insert (data->attrs, attr);
1361 else if (strcmp (element_name, "attributes") == 0)
1364 g_warning ("Unsupported tag for GtkLabel: %s\n", element_name);
1367 static const GMarkupParser pango_parser =
1369 pango_start_element,
1373 gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
1374 GtkBuilder *builder,
1376 const gchar *tagname,
1377 GMarkupParser *parser,
1380 if (buildable_parent_iface->custom_tag_start (buildable, builder, child,
1381 tagname, parser, data))
1384 if (strcmp (tagname, "attributes") == 0)
1386 PangoParserData *parser_data;
1388 parser_data = g_slice_new0 (PangoParserData);
1389 parser_data->builder = g_object_ref (builder);
1390 parser_data->object = g_object_ref (buildable);
1391 *parser = pango_parser;
1392 *data = parser_data;
1399 gtk_label_buildable_custom_finished (GtkBuildable *buildable,
1400 GtkBuilder *builder,
1402 const gchar *tagname,
1405 PangoParserData *data;
1407 buildable_parent_iface->custom_finished (buildable, builder, child,
1408 tagname, user_data);
1410 if (strcmp (tagname, "attributes") == 0)
1412 data = (PangoParserData*)user_data;
1416 gtk_label_set_attributes (GTK_LABEL (buildable), data->attrs);
1417 pango_attr_list_unref (data->attrs);
1420 g_object_unref (data->object);
1421 g_object_unref (data->builder);
1422 g_slice_free (PangoParserData, data);
1429 * @str: The text of the label
1431 * Creates a new label with the given text inside it. You can
1432 * pass %NULL to get an empty label widget.
1434 * Return value: the new #GtkLabel
1437 gtk_label_new (const gchar *str)
1441 label = g_object_new (GTK_TYPE_LABEL, NULL);
1444 gtk_label_set_text (label, str);
1446 return GTK_WIDGET (label);
1450 * gtk_label_new_with_mnemonic:
1451 * @str: The text of the label, with an underscore in front of the
1452 * mnemonic character
1454 * Creates a new #GtkLabel, containing the text in @str.
1456 * If characters in @str are preceded by an underscore, they are
1457 * underlined. If you need a literal underscore character in a label, use
1458 * '__' (two underscores). The first underlined character represents a
1459 * keyboard accelerator called a mnemonic. The mnemonic key can be used
1460 * to activate another widget, chosen automatically, or explicitly using
1461 * gtk_label_set_mnemonic_widget().
1463 * If gtk_label_set_mnemonic_widget() is not called, then the first
1464 * activatable ancestor of the #GtkLabel will be chosen as the mnemonic
1465 * widget. For instance, if the label is inside a button or menu item,
1466 * the button or menu item will automatically become the mnemonic widget
1467 * and be activated by the mnemonic.
1469 * Return value: the new #GtkLabel
1472 gtk_label_new_with_mnemonic (const gchar *str)
1476 label = g_object_new (GTK_TYPE_LABEL, NULL);
1479 gtk_label_set_text_with_mnemonic (label, str);
1481 return GTK_WIDGET (label);
1485 gtk_label_mnemonic_activate (GtkWidget *widget,
1486 gboolean group_cycling)
1488 GtkLabel *label = GTK_LABEL (widget);
1489 GtkLabelPriv *priv = label->priv;
1492 if (priv->mnemonic_widget)
1493 return gtk_widget_mnemonic_activate (priv->mnemonic_widget, group_cycling);
1495 /* Try to find the widget to activate by traversing the
1496 * widget's ancestry.
1498 parent = widget->parent;
1500 if (GTK_IS_NOTEBOOK (parent))
1505 if (gtk_widget_get_can_focus (parent) ||
1506 (!group_cycling && GTK_WIDGET_GET_CLASS (parent)->activate_signal) ||
1507 GTK_IS_NOTEBOOK (parent->parent) ||
1508 GTK_IS_MENU_ITEM (parent))
1509 return gtk_widget_mnemonic_activate (parent, group_cycling);
1510 parent = parent->parent;
1513 /* barf if there was nothing to activate */
1514 g_warning ("Couldn't find a target for a mnemonic activation.");
1515 gtk_widget_error_bell (widget);
1521 gtk_label_setup_mnemonic (GtkLabel *label,
1524 GtkLabelPriv *priv = label->priv;
1525 GtkWidget *widget = GTK_WIDGET (label);
1526 GtkWidget *toplevel;
1527 GtkWidget *mnemonic_menu;
1529 mnemonic_menu = g_object_get_data (G_OBJECT (label), "gtk-mnemonic-menu");
1531 if (last_key != GDK_VoidSymbol)
1533 if (priv->mnemonic_window)
1535 gtk_window_remove_mnemonic (priv->mnemonic_window,
1538 priv->mnemonic_window = NULL;
1542 _gtk_menu_shell_remove_mnemonic (GTK_MENU_SHELL (mnemonic_menu),
1545 mnemonic_menu = NULL;
1549 if (priv->mnemonic_keyval == GDK_VoidSymbol)
1552 connect_mnemonics_visible_notify (GTK_LABEL (widget));
1554 toplevel = gtk_widget_get_toplevel (widget);
1555 if (gtk_widget_is_toplevel (toplevel))
1557 GtkWidget *menu_shell;
1559 menu_shell = gtk_widget_get_ancestor (widget,
1560 GTK_TYPE_MENU_SHELL);
1564 _gtk_menu_shell_add_mnemonic (GTK_MENU_SHELL (menu_shell),
1565 priv->mnemonic_keyval,
1567 mnemonic_menu = menu_shell;
1570 if (!GTK_IS_MENU (menu_shell))
1572 gtk_window_add_mnemonic (GTK_WINDOW (toplevel),
1573 priv->mnemonic_keyval,
1575 priv->mnemonic_window = GTK_WINDOW (toplevel);
1580 g_object_set_data (G_OBJECT (label), I_("gtk-mnemonic-menu"), mnemonic_menu);
1584 gtk_label_hierarchy_changed (GtkWidget *widget,
1585 GtkWidget *old_toplevel)
1587 GtkLabel *label = GTK_LABEL (widget);
1588 GtkLabelPriv *priv = label->priv;
1590 gtk_label_setup_mnemonic (label, priv->mnemonic_keyval);
1594 label_shortcut_setting_apply (GtkLabel *label)
1596 gtk_label_recalculate (label);
1597 if (GTK_IS_ACCEL_LABEL (label))
1598 gtk_accel_label_refetch (GTK_ACCEL_LABEL (label));
1602 label_shortcut_setting_traverse_container (GtkWidget *widget,
1605 if (GTK_IS_LABEL (widget))
1606 label_shortcut_setting_apply (GTK_LABEL (widget));
1607 else if (GTK_IS_CONTAINER (widget))
1608 gtk_container_forall (GTK_CONTAINER (widget),
1609 label_shortcut_setting_traverse_container, data);
1613 label_shortcut_setting_changed (GtkSettings *settings)
1617 list = gtk_window_list_toplevels ();
1619 for (l = list; l ; l = l->next)
1621 GtkWidget *widget = l->data;
1623 if (gtk_widget_get_settings (widget) == settings)
1624 gtk_container_forall (GTK_CONTAINER (widget),
1625 label_shortcut_setting_traverse_container, NULL);
1632 mnemonics_visible_apply (GtkWidget *widget,
1633 gboolean mnemonics_visible)
1635 GtkLabel *label = GTK_LABEL (widget);
1636 GtkLabelPriv *priv = label->priv;
1638 mnemonics_visible = mnemonics_visible != FALSE;
1640 if (priv->mnemonics_visible != mnemonics_visible)
1642 priv->mnemonics_visible = mnemonics_visible;
1644 gtk_label_recalculate (label);
1649 label_mnemonics_visible_traverse_container (GtkWidget *widget,
1652 gboolean mnemonics_visible = GPOINTER_TO_INT (data);
1654 _gtk_label_mnemonics_visible_apply_recursively (widget, mnemonics_visible);
1658 _gtk_label_mnemonics_visible_apply_recursively (GtkWidget *widget,
1659 gboolean mnemonics_visible)
1661 if (GTK_IS_LABEL (widget))
1662 mnemonics_visible_apply (widget, mnemonics_visible);
1663 else if (GTK_IS_CONTAINER (widget))
1664 gtk_container_forall (GTK_CONTAINER (widget),
1665 label_mnemonics_visible_traverse_container,
1666 GINT_TO_POINTER (mnemonics_visible));
1670 label_mnemonics_visible_changed (GtkWindow *window,
1674 gboolean mnemonics_visible;
1676 g_object_get (window, "mnemonics-visible", &mnemonics_visible, NULL);
1678 gtk_container_forall (GTK_CONTAINER (window),
1679 label_mnemonics_visible_traverse_container,
1680 GINT_TO_POINTER (mnemonics_visible));
1684 gtk_label_screen_changed (GtkWidget *widget,
1685 GdkScreen *old_screen)
1687 GtkSettings *settings;
1688 gboolean shortcuts_connected;
1690 if (!gtk_widget_has_screen (widget))
1693 settings = gtk_widget_get_settings (widget);
1695 shortcuts_connected =
1696 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (settings),
1697 "gtk-label-shortcuts-connected"));
1699 if (! shortcuts_connected)
1701 g_signal_connect (settings, "notify::gtk-enable-mnemonics",
1702 G_CALLBACK (label_shortcut_setting_changed),
1704 g_signal_connect (settings, "notify::gtk-enable-accels",
1705 G_CALLBACK (label_shortcut_setting_changed),
1708 g_object_set_data (G_OBJECT (settings), "gtk-label-shortcuts-connected",
1709 GINT_TO_POINTER (TRUE));
1712 label_shortcut_setting_apply (GTK_LABEL (widget));
1717 label_mnemonic_widget_weak_notify (gpointer data,
1718 GObject *where_the_object_was)
1720 GtkLabel *label = data;
1721 GtkLabelPriv *priv = label->priv;
1723 priv->mnemonic_widget = NULL;
1724 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1728 * gtk_label_set_mnemonic_widget:
1729 * @label: a #GtkLabel
1730 * @widget: (allow-none): the target #GtkWidget
1732 * If the label has been set so that it has an mnemonic key (using
1733 * i.e. gtk_label_set_markup_with_mnemonic(),
1734 * gtk_label_set_text_with_mnemonic(), gtk_label_new_with_mnemonic()
1735 * or the "use_underline" property) the label can be associated with a
1736 * widget that is the target of the mnemonic. When the label is inside
1737 * a widget (like a #GtkButton or a #GtkNotebook tab) it is
1738 * automatically associated with the correct widget, but sometimes
1739 * (i.e. when the target is a #GtkEntry next to the label) you need to
1740 * set it explicitly using this function.
1742 * The target widget will be accelerated by emitting the
1743 * GtkWidget::mnemonic-activate signal on it. The default handler for
1744 * this signal will activate the widget if there are no mnemonic collisions
1745 * and toggle focus between the colliding widgets otherwise.
1748 gtk_label_set_mnemonic_widget (GtkLabel *label,
1753 g_return_if_fail (GTK_IS_LABEL (label));
1758 g_return_if_fail (GTK_IS_WIDGET (widget));
1760 if (priv->mnemonic_widget)
1762 gtk_widget_remove_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1763 g_object_weak_unref (G_OBJECT (priv->mnemonic_widget),
1764 label_mnemonic_widget_weak_notify,
1767 priv->mnemonic_widget = widget;
1768 if (priv->mnemonic_widget)
1770 g_object_weak_ref (G_OBJECT (priv->mnemonic_widget),
1771 label_mnemonic_widget_weak_notify,
1773 gtk_widget_add_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1776 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1780 * gtk_label_get_mnemonic_widget:
1781 * @label: a #GtkLabel
1783 * Retrieves the target of the mnemonic (keyboard shortcut) of this
1784 * label. See gtk_label_set_mnemonic_widget().
1786 * Return value: the target of the label's mnemonic, or %NULL if none
1787 * has been set and the default algorithm will be used.
1790 gtk_label_get_mnemonic_widget (GtkLabel *label)
1792 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1794 return label->priv->mnemonic_widget;
1798 * gtk_label_get_mnemonic_keyval:
1799 * @label: a #GtkLabel
1801 * If the label has been set so that it has an mnemonic key this function
1802 * returns the keyval used for the mnemonic accelerator. If there is no
1803 * mnemonic set up it returns #GDK_VoidSymbol.
1805 * Returns: GDK keyval usable for accelerators, or #GDK_VoidSymbol
1808 gtk_label_get_mnemonic_keyval (GtkLabel *label)
1810 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_VoidSymbol);
1812 return label->priv->mnemonic_keyval;
1816 gtk_label_set_text_internal (GtkLabel *label,
1819 GtkLabelPriv *priv = label->priv;
1821 g_free (priv->text);
1825 gtk_label_select_region_index (label, 0, 0);
1829 gtk_label_set_label_internal (GtkLabel *label,
1832 GtkLabelPriv *priv = label->priv;
1834 g_free (priv->label);
1838 g_object_notify (G_OBJECT (label), "label");
1842 gtk_label_set_use_markup_internal (GtkLabel *label,
1845 GtkLabelPriv *priv = label->priv;
1848 if (priv->use_markup != val)
1850 priv->use_markup = val;
1852 g_object_notify (G_OBJECT (label), "use-markup");
1857 gtk_label_set_use_underline_internal (GtkLabel *label,
1860 GtkLabelPriv *priv = label->priv;
1863 if (priv->use_underline != val)
1865 priv->use_underline = val;
1867 g_object_notify (G_OBJECT (label), "use-underline");
1872 gtk_label_compose_effective_attrs (GtkLabel *label)
1874 GtkLabelPriv *priv = label->priv;
1875 PangoAttrIterator *iter;
1876 PangoAttribute *attr;
1877 GSList *iter_attrs, *l;
1881 if (priv->effective_attrs)
1883 if ((iter = pango_attr_list_get_iterator (priv->attrs)))
1887 iter_attrs = pango_attr_iterator_get_attrs (iter);
1888 for (l = iter_attrs; l; l = l->next)
1891 pango_attr_list_insert (priv->effective_attrs, attr);
1893 g_slist_free (iter_attrs);
1895 while (pango_attr_iterator_next (iter));
1896 pango_attr_iterator_destroy (iter);
1900 priv->effective_attrs =
1901 pango_attr_list_ref (priv->attrs);
1906 gtk_label_set_attributes_internal (GtkLabel *label,
1907 PangoAttrList *attrs)
1909 GtkLabelPriv *priv = label->priv;
1912 pango_attr_list_ref (attrs);
1915 pango_attr_list_unref (priv->attrs);
1916 priv->attrs = attrs;
1918 g_object_notify (G_OBJECT (label), "attributes");
1922 /* Calculates text, attrs and mnemonic_keyval from
1923 * label, use_underline and use_markup
1926 gtk_label_recalculate (GtkLabel *label)
1928 GtkLabelPriv *priv = label->priv;
1929 guint keyval = priv->mnemonic_keyval;
1931 if (priv->use_markup)
1932 gtk_label_set_markup_internal (label, priv->label, priv->use_underline);
1935 if (priv->use_underline)
1936 gtk_label_set_uline_text_internal (label, priv->label);
1939 if (priv->effective_attrs)
1940 pango_attr_list_unref (priv->effective_attrs);
1941 priv->effective_attrs = NULL;
1942 gtk_label_set_text_internal (label, g_strdup (priv->label));
1946 gtk_label_compose_effective_attrs (label);
1948 if (!priv->use_underline)
1949 priv->mnemonic_keyval = GDK_VoidSymbol;
1951 if (keyval != priv->mnemonic_keyval)
1953 gtk_label_setup_mnemonic (label, keyval);
1954 g_object_notify (G_OBJECT (label), "mnemonic-keyval");
1957 gtk_label_clear_layout (label);
1958 gtk_label_clear_select_info (label);
1959 gtk_widget_queue_resize (GTK_WIDGET (label));
1963 * gtk_label_set_text:
1964 * @label: a #GtkLabel
1965 * @str: The text you want to set
1967 * Sets the text within the #GtkLabel widget. It overwrites any text that
1970 * This will also clear any previously set mnemonic accelerators.
1973 gtk_label_set_text (GtkLabel *label,
1976 g_return_if_fail (GTK_IS_LABEL (label));
1978 g_object_freeze_notify (G_OBJECT (label));
1980 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
1981 gtk_label_set_use_markup_internal (label, FALSE);
1982 gtk_label_set_use_underline_internal (label, FALSE);
1984 gtk_label_recalculate (label);
1986 g_object_thaw_notify (G_OBJECT (label));
1990 * gtk_label_set_attributes:
1991 * @label: a #GtkLabel
1992 * @attrs: a #PangoAttrList
1994 * Sets a #PangoAttrList; the attributes in the list are applied to the
1997 * <note><para>The attributes set with this function will be applied
1998 * and merged with any other attributes previously effected by way
1999 * of the #GtkLabel:use-underline or #GtkLabel:use-markup properties.
2000 * While it is not recommended to mix markup strings with manually set
2001 * attributes, if you must; know that the attributes will be applied
2002 * to the label after the markup string is parsed.</para></note>
2005 gtk_label_set_attributes (GtkLabel *label,
2006 PangoAttrList *attrs)
2008 g_return_if_fail (GTK_IS_LABEL (label));
2010 gtk_label_set_attributes_internal (label, attrs);
2012 gtk_label_recalculate (label);
2014 gtk_label_clear_layout (label);
2015 gtk_widget_queue_resize (GTK_WIDGET (label));
2019 * gtk_label_get_attributes:
2020 * @label: a #GtkLabel
2022 * Gets the attribute list that was set on the label using
2023 * gtk_label_set_attributes(), if any. This function does
2024 * not reflect attributes that come from the labels markup
2025 * (see gtk_label_set_markup()). If you want to get the
2026 * effective attributes for the label, use
2027 * pango_layout_get_attribute (gtk_label_get_layout (label)).
2029 * Return value: the attribute list, or %NULL if none was set.
2032 gtk_label_get_attributes (GtkLabel *label)
2034 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2036 return label->priv->attrs;
2040 * gtk_label_set_label:
2041 * @label: a #GtkLabel
2042 * @str: the new text to set for the label
2044 * Sets the text of the label. The label is interpreted as
2045 * including embedded underlines and/or Pango markup depending
2046 * on the values of the #GtkLabel:use-underline" and
2047 * #GtkLabel:use-markup properties.
2050 gtk_label_set_label (GtkLabel *label,
2053 g_return_if_fail (GTK_IS_LABEL (label));
2055 g_object_freeze_notify (G_OBJECT (label));
2057 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2058 gtk_label_recalculate (label);
2060 g_object_thaw_notify (G_OBJECT (label));
2064 * gtk_label_get_label:
2065 * @label: a #GtkLabel
2067 * Fetches the text from a label widget including any embedded
2068 * underlines indicating mnemonics and Pango markup. (See
2069 * gtk_label_get_text()).
2071 * Return value: the text of the label widget. This string is
2072 * owned by the widget and must not be modified or freed.
2074 G_CONST_RETURN gchar *
2075 gtk_label_get_label (GtkLabel *label)
2077 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2079 return label->priv->label;
2087 GdkColor *link_color;
2088 GdkColor *visited_link_color;
2092 start_element_handler (GMarkupParseContext *context,
2093 const gchar *element_name,
2094 const gchar **attribute_names,
2095 const gchar **attribute_values,
2100 UriParserData *pdata = user_data;
2102 if (strcmp (element_name, "a") == 0)
2105 const gchar *uri = NULL;
2106 const gchar *title = NULL;
2107 gboolean visited = FALSE;
2111 GdkColor *color = NULL;
2113 g_markup_parse_context_get_position (context, &line_number, &char_number);
2115 for (i = 0; attribute_names[i] != NULL; i++)
2117 const gchar *attr = attribute_names[i];
2119 if (strcmp (attr, "href") == 0)
2120 uri = attribute_values[i];
2121 else if (strcmp (attr, "title") == 0)
2122 title = attribute_values[i];
2127 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
2128 "Attribute '%s' is not allowed on the <a> tag "
2129 "on line %d char %d",
2130 attr, line_number, char_number);
2139 G_MARKUP_ERROR_INVALID_CONTENT,
2140 "Attribute 'href' was missing on the <a> tag "
2141 "on line %d char %d",
2142 line_number, char_number);
2147 priv = pdata->label->priv;
2148 if (priv->track_links && priv->select_info)
2151 for (l = priv->select_info->links; l; l = l->next)
2154 if (strcmp (uri, link->uri) == 0)
2156 visited = link->visited;
2163 color = pdata->visited_link_color;
2165 color = pdata->link_color;
2167 g_string_append_printf (pdata->new_str,
2168 "<span color=\"#%04x%04x%04x\" underline=\"single\">",
2173 link = g_new0 (GtkLabelLink, 1);
2174 link->uri = g_strdup (uri);
2175 link->title = g_strdup (title);
2176 link->visited = visited;
2177 pdata->links = g_list_append (pdata->links, link);
2183 g_string_append_c (pdata->new_str, '<');
2184 g_string_append (pdata->new_str, element_name);
2186 for (i = 0; attribute_names[i] != NULL; i++)
2188 const gchar *attr = attribute_names[i];
2189 const gchar *value = attribute_values[i];
2192 newvalue = g_markup_escape_text (value, -1);
2194 g_string_append_c (pdata->new_str, ' ');
2195 g_string_append (pdata->new_str, attr);
2196 g_string_append (pdata->new_str, "=\"");
2197 g_string_append (pdata->new_str, newvalue);
2198 g_string_append_c (pdata->new_str, '\"');
2202 g_string_append_c (pdata->new_str, '>');
2207 end_element_handler (GMarkupParseContext *context,
2208 const gchar *element_name,
2212 UriParserData *pdata = user_data;
2214 if (!strcmp (element_name, "a"))
2215 g_string_append (pdata->new_str, "</span>");
2218 g_string_append (pdata->new_str, "</");
2219 g_string_append (pdata->new_str, element_name);
2220 g_string_append_c (pdata->new_str, '>');
2225 text_handler (GMarkupParseContext *context,
2231 UriParserData *pdata = user_data;
2234 newtext = g_markup_escape_text (text, text_len);
2235 g_string_append (pdata->new_str, newtext);
2239 static const GMarkupParser markup_parser =
2241 start_element_handler,
2242 end_element_handler,
2249 xml_isspace (gchar c)
2251 return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
2255 link_free (GtkLabelLink *link)
2258 g_free (link->title);
2263 gtk_label_get_link_colors (GtkWidget *widget,
2264 GdkColor **link_color,
2265 GdkColor **visited_link_color)
2267 gtk_widget_ensure_style (widget);
2268 gtk_widget_style_get (widget,
2269 "link-color", link_color,
2270 "visited-link-color", visited_link_color,
2273 *link_color = gdk_color_copy (&default_link_color);
2274 if (!*visited_link_color)
2275 *visited_link_color = gdk_color_copy (&default_visited_link_color);
2279 parse_uri_markup (GtkLabel *label,
2285 GMarkupParseContext *context = NULL;
2286 const gchar *p, *end;
2287 gboolean needs_root = TRUE;
2289 UriParserData pdata;
2291 length = strlen (str);
2295 pdata.label = label;
2297 pdata.new_str = g_string_sized_new (length);
2299 gtk_label_get_link_colors (GTK_WIDGET (label), &pdata.link_color, &pdata.visited_link_color);
2301 while (p != end && xml_isspace (*p))
2304 if (end - p >= 8 && strncmp (p, "<markup>", 8) == 0)
2307 context = g_markup_parse_context_new (&markup_parser, 0, &pdata, NULL);
2311 if (!g_markup_parse_context_parse (context, "<markup>", -1, error))
2315 if (!g_markup_parse_context_parse (context, str, length, error))
2320 if (!g_markup_parse_context_parse (context, "</markup>", -1, error))
2324 if (!g_markup_parse_context_end_parse (context, error))
2327 g_markup_parse_context_free (context);
2329 *new_str = g_string_free (pdata.new_str, FALSE);
2330 *links = pdata.links;
2332 gdk_color_free (pdata.link_color);
2333 gdk_color_free (pdata.visited_link_color);
2338 g_markup_parse_context_free (context);
2339 g_string_free (pdata.new_str, TRUE);
2340 g_list_foreach (pdata.links, (GFunc)link_free, NULL);
2341 g_list_free (pdata.links);
2342 gdk_color_free (pdata.link_color);
2343 gdk_color_free (pdata.visited_link_color);
2349 gtk_label_ensure_has_tooltip (GtkLabel *label)
2351 GtkLabelPriv *priv = label->priv;
2353 gboolean has_tooltip = FALSE;
2355 for (l = priv->select_info->links; l; l = l->next)
2357 GtkLabelLink *link = l->data;
2365 gtk_widget_set_has_tooltip (GTK_WIDGET (label), has_tooltip);
2369 gtk_label_set_markup_internal (GtkLabel *label,
2371 gboolean with_uline)
2373 GtkLabelPriv *priv = label->priv;
2375 GError *error = NULL;
2376 PangoAttrList *attrs = NULL;
2377 gunichar accel_char = 0;
2379 GList *links = NULL;
2381 if (!parse_uri_markup (label, str, &new_str, &links, &error))
2383 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2385 g_error_free (error);
2389 gtk_label_clear_links (label);
2392 gtk_label_ensure_select_info (label);
2393 priv->select_info->links = links;
2394 gtk_label_ensure_has_tooltip (label);
2399 gboolean enable_mnemonics;
2400 gboolean auto_mnemonics;
2402 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2403 "gtk-enable-mnemonics", &enable_mnemonics,
2404 "gtk-auto-mnemonics", &auto_mnemonics,
2407 if (!(enable_mnemonics && priv->mnemonics_visible &&
2409 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2410 (!priv->mnemonic_widget ||
2411 gtk_widget_is_sensitive (priv->mnemonic_widget))))))
2417 if (separate_uline_pattern (new_str, &key, &tmp, &pattern))
2426 if (!pango_parse_markup (new_str,
2428 with_uline ? '_' : 0,
2431 with_uline ? &accel_char : NULL,
2434 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2437 g_error_free (error);
2444 gtk_label_set_text_internal (label, text);
2448 if (priv->effective_attrs)
2449 pango_attr_list_unref (priv->effective_attrs);
2450 priv->effective_attrs = attrs;
2453 if (accel_char != 0)
2454 priv->mnemonic_keyval = gdk_keyval_to_lower (gdk_unicode_to_keyval (accel_char));
2456 priv->mnemonic_keyval = GDK_VoidSymbol;
2460 * gtk_label_set_markup:
2461 * @label: a #GtkLabel
2462 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
2464 * Parses @str which is marked up with the <link
2465 * linkend="PangoMarkupFormat">Pango text markup language</link>, setting the
2466 * label's text and attribute list based on the parse results. If the @str is
2467 * external data, you may need to escape it with g_markup_escape_text() or
2468 * g_markup_printf_escaped()<!-- -->:
2472 * markup = g_markup_printf_escaped ("<span style=\"italic\">%s</span>", str);
2473 * gtk_label_set_markup (GTK_LABEL (label), markup);
2478 gtk_label_set_markup (GtkLabel *label,
2483 g_return_if_fail (GTK_IS_LABEL (label));
2487 g_object_freeze_notify (G_OBJECT (label));
2489 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2490 gtk_label_set_use_markup_internal (label, TRUE);
2491 gtk_label_set_use_underline_internal (label, FALSE);
2493 gtk_label_recalculate (label);
2495 g_object_thaw_notify (G_OBJECT (label));
2499 * gtk_label_set_markup_with_mnemonic:
2500 * @label: a #GtkLabel
2501 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
2503 * Parses @str which is marked up with the <link linkend="PangoMarkupFormat">Pango text markup language</link>,
2504 * setting the label's text and attribute list based on the parse results.
2505 * If characters in @str are preceded by an underscore, they are underlined
2506 * indicating that they represent a keyboard accelerator called a mnemonic.
2508 * The mnemonic key can be used to activate another widget, chosen
2509 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
2512 gtk_label_set_markup_with_mnemonic (GtkLabel *label,
2517 g_return_if_fail (GTK_IS_LABEL (label));
2521 g_object_freeze_notify (G_OBJECT (label));
2523 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2524 gtk_label_set_use_markup_internal (label, TRUE);
2525 gtk_label_set_use_underline_internal (label, TRUE);
2527 gtk_label_recalculate (label);
2529 g_object_thaw_notify (G_OBJECT (label));
2533 * gtk_label_get_text:
2534 * @label: a #GtkLabel
2536 * Fetches the text from a label widget, as displayed on the
2537 * screen. This does not include any embedded underlines
2538 * indicating mnemonics or Pango markup. (See gtk_label_get_label())
2540 * Return value: the text in the label widget. This is the internal
2541 * string used by the label, and must not be modified.
2543 G_CONST_RETURN gchar *
2544 gtk_label_get_text (GtkLabel *label)
2546 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2548 return label->priv->text;
2551 static PangoAttrList *
2552 gtk_label_pattern_to_attrs (GtkLabel *label,
2553 const gchar *pattern)
2555 GtkLabelPriv *priv = label->priv;
2557 const char *p = priv->text;
2558 const char *q = pattern;
2559 PangoAttrList *attrs;
2561 attrs = pango_attr_list_new ();
2565 while (*p && *q && *q != '_')
2567 p = g_utf8_next_char (p);
2571 while (*p && *q && *q == '_')
2573 p = g_utf8_next_char (p);
2579 PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_LOW);
2580 attr->start_index = start - priv->text;
2581 attr->end_index = p - priv->text;
2583 pango_attr_list_insert (attrs, attr);
2593 gtk_label_set_pattern_internal (GtkLabel *label,
2594 const gchar *pattern,
2595 gboolean is_mnemonic)
2597 GtkLabelPriv *priv = label->priv;
2598 PangoAttrList *attrs;
2599 gboolean enable_mnemonics;
2600 gboolean auto_mnemonics;
2602 if (priv->pattern_set)
2607 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2608 "gtk-enable-mnemonics", &enable_mnemonics,
2609 "gtk-auto-mnemonics", &auto_mnemonics,
2612 if (enable_mnemonics && priv->mnemonics_visible && pattern &&
2614 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2615 (!priv->mnemonic_widget ||
2616 gtk_widget_is_sensitive (priv->mnemonic_widget)))))
2617 attrs = gtk_label_pattern_to_attrs (label, pattern);
2622 attrs = gtk_label_pattern_to_attrs (label, pattern);
2624 if (priv->effective_attrs)
2625 pango_attr_list_unref (priv->effective_attrs);
2626 priv->effective_attrs = attrs;
2630 gtk_label_set_pattern (GtkLabel *label,
2631 const gchar *pattern)
2633 GtkLabelPriv *priv = label->priv;
2635 g_return_if_fail (GTK_IS_LABEL (label));
2639 priv->pattern_set = FALSE;
2643 gtk_label_set_pattern_internal (label, pattern, FALSE);
2644 priv->pattern_set = TRUE;
2647 gtk_label_recalculate (label);
2649 gtk_label_clear_layout (label);
2650 gtk_widget_queue_resize (GTK_WIDGET (label));
2655 * gtk_label_set_justify:
2656 * @label: a #GtkLabel
2657 * @jtype: a #GtkJustification
2659 * Sets the alignment of the lines in the text of the label relative to
2660 * each other. %GTK_JUSTIFY_LEFT is the default value when the
2661 * widget is first created with gtk_label_new(). If you instead want
2662 * to set the alignment of the label as a whole, use
2663 * gtk_misc_set_alignment() instead. gtk_label_set_justify() has no
2664 * effect on labels containing only a single line.
2667 gtk_label_set_justify (GtkLabel *label,
2668 GtkJustification jtype)
2672 g_return_if_fail (GTK_IS_LABEL (label));
2673 g_return_if_fail (jtype >= GTK_JUSTIFY_LEFT && jtype <= GTK_JUSTIFY_FILL);
2677 if ((GtkJustification) priv->jtype != jtype)
2679 priv->jtype = jtype;
2681 /* No real need to be this drastic, but easier than duplicating the code */
2682 gtk_label_clear_layout (label);
2684 g_object_notify (G_OBJECT (label), "justify");
2685 gtk_widget_queue_resize (GTK_WIDGET (label));
2690 * gtk_label_get_justify:
2691 * @label: a #GtkLabel
2693 * Returns the justification of the label. See gtk_label_set_justify().
2695 * Return value: #GtkJustification
2698 gtk_label_get_justify (GtkLabel *label)
2700 g_return_val_if_fail (GTK_IS_LABEL (label), 0);
2702 return label->priv->jtype;
2706 * gtk_label_set_ellipsize:
2707 * @label: a #GtkLabel
2708 * @mode: a #PangoEllipsizeMode
2710 * Sets the mode used to ellipsize (add an ellipsis: "...") to the text
2711 * if there is not enough space to render the entire string.
2716 gtk_label_set_ellipsize (GtkLabel *label,
2717 PangoEllipsizeMode mode)
2721 g_return_if_fail (GTK_IS_LABEL (label));
2722 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE && mode <= PANGO_ELLIPSIZE_END);
2726 if ((PangoEllipsizeMode) priv->ellipsize != mode)
2728 priv->ellipsize = mode;
2730 /* No real need to be this drastic, but easier than duplicating the code */
2731 gtk_label_clear_layout (label);
2733 g_object_notify (G_OBJECT (label), "ellipsize");
2734 gtk_widget_queue_resize (GTK_WIDGET (label));
2739 * gtk_label_get_ellipsize:
2740 * @label: a #GtkLabel
2742 * Returns the ellipsizing position of the label. See gtk_label_set_ellipsize().
2744 * Return value: #PangoEllipsizeMode
2749 gtk_label_get_ellipsize (GtkLabel *label)
2751 g_return_val_if_fail (GTK_IS_LABEL (label), PANGO_ELLIPSIZE_NONE);
2753 return label->priv->ellipsize;
2757 * gtk_label_set_width_chars:
2758 * @label: a #GtkLabel
2759 * @n_chars: the new desired width, in characters.
2761 * Sets the desired width in characters of @label to @n_chars.
2766 gtk_label_set_width_chars (GtkLabel *label,
2771 g_return_if_fail (GTK_IS_LABEL (label));
2775 if (priv->width_chars != n_chars)
2777 priv->width_chars = n_chars;
2778 g_object_notify (G_OBJECT (label), "width-chars");
2779 gtk_label_invalidate_wrap_width (label);
2780 gtk_widget_queue_resize (GTK_WIDGET (label));
2785 * gtk_label_get_width_chars:
2786 * @label: a #GtkLabel
2788 * Retrieves the desired width of @label, in characters. See
2789 * gtk_label_set_width_chars().
2791 * Return value: the width of the label in characters.
2796 gtk_label_get_width_chars (GtkLabel *label)
2798 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2800 return label->priv->width_chars;
2804 * gtk_label_set_max_width_chars:
2805 * @label: a #GtkLabel
2806 * @n_chars: the new desired maximum width, in characters.
2808 * Sets the desired maximum width in characters of @label to @n_chars.
2813 gtk_label_set_max_width_chars (GtkLabel *label,
2818 g_return_if_fail (GTK_IS_LABEL (label));
2822 if (priv->max_width_chars != n_chars)
2824 priv->max_width_chars = n_chars;
2826 g_object_notify (G_OBJECT (label), "max-width-chars");
2827 gtk_label_invalidate_wrap_width (label);
2828 gtk_widget_queue_resize (GTK_WIDGET (label));
2833 * gtk_label_get_max_width_chars:
2834 * @label: a #GtkLabel
2836 * Retrieves the desired maximum width of @label, in characters. See
2837 * gtk_label_set_width_chars().
2839 * Return value: the maximum width of the label in characters.
2844 gtk_label_get_max_width_chars (GtkLabel *label)
2846 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2848 return label->priv->max_width_chars;
2852 * gtk_label_set_line_wrap:
2853 * @label: a #GtkLabel
2854 * @wrap: the setting
2856 * Toggles line wrapping within the #GtkLabel widget. %TRUE makes it break
2857 * lines if text exceeds the widget's size. %FALSE lets the text get cut off
2858 * by the edge of the widget if it exceeds the widget size.
2860 * Note that setting line wrapping to %TRUE does not make the label
2861 * wrap at its parent container's width, because GTK+ widgets
2862 * conceptually can't make their requisition depend on the parent
2863 * container's size. For a label that wraps at a specific position,
2864 * set the label's width using gtk_widget_set_size_request().
2867 gtk_label_set_line_wrap (GtkLabel *label,
2872 g_return_if_fail (GTK_IS_LABEL (label));
2876 wrap = wrap != FALSE;
2878 if (priv->wrap != wrap)
2882 gtk_label_clear_layout (label);
2883 gtk_widget_queue_resize (GTK_WIDGET (label));
2884 g_object_notify (G_OBJECT (label), "wrap");
2889 * gtk_label_get_line_wrap:
2890 * @label: a #GtkLabel
2892 * Returns whether lines in the label are automatically wrapped.
2893 * See gtk_label_set_line_wrap().
2895 * Return value: %TRUE if the lines of the label are automatically wrapped.
2898 gtk_label_get_line_wrap (GtkLabel *label)
2900 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
2902 return label->priv->wrap;
2906 * gtk_label_set_line_wrap_mode:
2907 * @label: a #GtkLabel
2908 * @wrap_mode: the line wrapping mode
2910 * If line wrapping is on (see gtk_label_set_line_wrap()) this controls how
2911 * the line wrapping is done. The default is %PANGO_WRAP_WORD which means
2912 * wrap on word boundaries.
2917 gtk_label_set_line_wrap_mode (GtkLabel *label,
2918 PangoWrapMode wrap_mode)
2922 g_return_if_fail (GTK_IS_LABEL (label));
2926 if (priv->wrap_mode != wrap_mode)
2928 priv->wrap_mode = wrap_mode;
2929 g_object_notify (G_OBJECT (label), "wrap-mode");
2931 gtk_widget_queue_resize (GTK_WIDGET (label));
2936 * gtk_label_get_line_wrap_mode:
2937 * @label: a #GtkLabel
2939 * Returns line wrap mode used by the label. See gtk_label_set_line_wrap_mode().
2941 * Return value: %TRUE if the lines of the label are automatically wrapped.
2946 gtk_label_get_line_wrap_mode (GtkLabel *label)
2948 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
2950 return label->priv->wrap_mode;
2954 gtk_label_destroy (GtkObject *object)
2956 GtkLabel *label = GTK_LABEL (object);
2958 gtk_label_set_mnemonic_widget (label, NULL);
2960 GTK_OBJECT_CLASS (gtk_label_parent_class)->destroy (object);
2964 gtk_label_finalize (GObject *object)
2966 GtkLabel *label = GTK_LABEL (object);
2967 GtkLabelPriv *priv = label->priv;
2969 g_free (priv->label);
2970 g_free (priv->text);
2973 g_object_unref (priv->layout);
2976 pango_attr_list_unref (priv->attrs);
2978 if (priv->effective_attrs)
2979 pango_attr_list_unref (priv->effective_attrs);
2981 gtk_label_clear_links (label);
2982 g_free (priv->select_info);
2984 G_OBJECT_CLASS (gtk_label_parent_class)->finalize (object);
2988 gtk_label_clear_layout (GtkLabel *label)
2990 GtkLabelPriv *priv = label->priv;
2994 g_object_unref (priv->layout);
2995 priv->layout = NULL;
2997 //gtk_label_clear_links (label);
3003 get_label_width (GtkLabel *label,
3007 GtkWidgetAuxInfo *aux_info;
3009 PangoLayout *layout;
3010 PangoContext *context;
3011 PangoFontMetrics *metrics;
3012 PangoRectangle rect;
3013 gint char_width, digit_width, char_pixels, text_width, ellipsize_chars, guess_width;
3016 aux_info = _gtk_widget_get_aux_info (GTK_WIDGET (label), FALSE);
3018 layout = pango_layout_copy (priv->layout);
3019 context = pango_layout_get_context (layout);
3020 metrics = pango_context_get_metrics (context, GTK_WIDGET (label)->style->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, GTK_WIDGET (label)->style->font_desc,
3137 pango_context_get_language (context));
3139 char_width = pango_font_metrics_get_approximate_char_width (metrics);
3140 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
3141 char_pixels = MAX (char_width, digit_width);
3142 pango_font_metrics_unref (metrics);
3144 pango_layout_set_width (layout, -1);
3145 pango_layout_get_extents (layout, NULL, &rect);
3146 g_object_unref (layout);
3148 text_width = rect.width;
3150 priv->wrap_width = PANGO_PIXELS (MAX (text_width, char_pixels * priv->width_chars));
3154 PangoLayout *layout;
3156 layout = gtk_widget_create_pango_layout (GTK_WIDGET (label),
3157 "This string is just about long enough.");
3158 pango_layout_get_size (layout, &priv->wrap_width, NULL);
3159 g_object_unref (layout);
3163 return priv->wrap_width;
3167 gtk_label_ensure_layout (GtkLabel *label, gboolean guess_wrap_width)
3169 GtkLabelPriv *priv = label->priv;
3171 PangoRectangle logical_rect;
3174 widget = GTK_WIDGET (label);
3176 rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
3180 PangoAlignment align = PANGO_ALIGN_LEFT; /* Quiet gcc */
3181 gdouble angle = gtk_label_get_angle (label);
3183 if (angle != 0.0 && !priv->select_info)
3185 PangoMatrix matrix = PANGO_MATRIX_INIT;
3187 /* We rotate the standard singleton PangoContext for the widget,
3188 * depending on the fact that it's meant pretty much exclusively
3191 pango_matrix_rotate (&matrix, angle);
3193 pango_context_set_matrix (gtk_widget_get_pango_context (widget), &matrix);
3195 priv->have_transform = TRUE;
3199 if (priv->have_transform)
3200 pango_context_set_matrix (gtk_widget_get_pango_context (widget), NULL);
3202 priv->have_transform = FALSE;
3205 priv->layout = gtk_widget_create_pango_layout (widget, priv->text);
3207 if (priv->effective_attrs)
3208 pango_layout_set_attributes (priv->layout, priv->effective_attrs);
3210 gtk_label_rescan_links (label);
3212 switch (priv->jtype)
3214 case GTK_JUSTIFY_LEFT:
3215 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3217 case GTK_JUSTIFY_RIGHT:
3218 align = rtl ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
3220 case GTK_JUSTIFY_CENTER:
3221 align = PANGO_ALIGN_CENTER;
3223 case GTK_JUSTIFY_FILL:
3224 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3225 pango_layout_set_justify (priv->layout, TRUE);
3228 g_assert_not_reached();
3231 pango_layout_set_alignment (priv->layout, align);
3232 pango_layout_set_ellipsize (priv->layout, priv->ellipsize);
3233 pango_layout_set_single_paragraph_mode (priv->layout, priv->single_line_mode);
3235 if (priv->ellipsize)
3236 pango_layout_set_width (priv->layout,
3237 widget->allocation.width * PANGO_SCALE);
3238 else if (priv->wrap)
3240 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (widget, FALSE);
3241 gint longest_paragraph;
3245 if ((angle == 90 || angle == 270) && aux_info && aux_info->height > 0)
3246 aux_width = aux_info->height;
3247 else if (aux_info && aux_info->width > 0)
3248 aux_width = aux_info->width;
3251 pango_layout_set_width (priv->layout, aux_width * PANGO_SCALE);
3252 else if (guess_wrap_width == FALSE &&
3253 widget->allocation.width > 1 && widget->allocation.height > 1)
3255 if (angle == 90 || angle == 270)
3256 width = widget->allocation.height - label->misc.ypad * 2;
3258 width = widget->allocation.width - label->misc.xpad * 2;
3260 pango_layout_set_wrap (priv->layout, priv->wrap_mode);
3261 pango_layout_set_width (priv->layout, MAX (width, 1) * PANGO_SCALE);
3265 GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (label));
3268 pango_layout_set_width (priv->layout, -1);
3269 pango_layout_get_extents (priv->layout, NULL, &logical_rect);
3271 width = logical_rect.width;
3272 /* Try to guess a reasonable maximum width */
3273 longest_paragraph = width;
3275 wrap_width = get_label_wrap_width (label);
3276 width = MIN (width, wrap_width);
3278 PANGO_SCALE * (gdk_screen_get_width (screen) + 1) / 2);
3280 pango_layout_set_width (priv->layout, width);
3281 pango_layout_get_extents (priv->layout, NULL, &logical_rect);
3282 width = logical_rect.width;
3283 height = logical_rect.height;
3285 /* Unfortunately, the above may leave us with a very unbalanced looking paragraph,
3286 * so we try short search for a narrower width that leaves us with the same height
3288 if (longest_paragraph > 0)
3290 gint nlines, perfect_width;
3292 nlines = pango_layout_get_line_count (priv->layout);
3293 perfect_width = (longest_paragraph + nlines - 1) / nlines;
3295 if (perfect_width < width)
3297 pango_layout_set_width (priv->layout, perfect_width);
3298 pango_layout_get_extents (priv->layout, NULL, &logical_rect);
3300 if (logical_rect.height <= height)
3301 width = logical_rect.width;
3304 gint mid_width = (perfect_width + width) / 2;
3306 if (mid_width > perfect_width)
3308 pango_layout_set_width (priv->layout, mid_width);
3309 pango_layout_get_extents (priv->layout, NULL, &logical_rect);
3311 if (logical_rect.height <= height)
3312 width = logical_rect.width;
3317 pango_layout_set_width (priv->layout, width);
3320 else /* !priv->wrap */
3321 pango_layout_set_width (priv->layout, -1);
3326 get_single_line_height (GtkWidget *widget,
3327 PangoLayout *layout)
3329 PangoContext *context;
3330 PangoFontMetrics *metrics;
3331 gint ascent, descent;
3333 context = pango_layout_get_context (layout);
3334 metrics = pango_context_get_metrics (context, widget->style->font_desc,
3335 pango_context_get_language (context));
3337 ascent = pango_font_metrics_get_ascent (metrics);
3338 descent = pango_font_metrics_get_descent (metrics);
3339 pango_font_metrics_unref (metrics);
3341 return ascent + descent;
3345 gtk_label_size_request_init (GtkSizeRequestIface *iface)
3347 iface->get_request_mode = gtk_label_get_request_mode;
3348 iface->get_width = gtk_label_get_width;
3349 iface->get_height = gtk_label_get_height;
3350 iface->get_width_for_height = gtk_label_get_width_for_height;
3351 iface->get_height_for_width = gtk_label_get_height_for_width;
3354 static GtkSizeRequestMode
3355 gtk_label_get_request_mode (GtkSizeRequest *layout)
3357 GtkLabel *label = GTK_LABEL (layout);
3358 gdouble angle = gtk_label_get_angle (label);
3360 if (angle == 90 || angle == 270)
3361 return GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
3363 return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
3367 get_size_for_allocation (GtkLabel *label,
3368 GtkOrientation orientation,
3373 GtkLabelPriv *priv = label->priv;
3374 PangoLayout *layout;
3375 GtkWidgetAuxInfo *aux_info =
3376 _gtk_widget_get_aux_info (GTK_WIDGET (label), FALSE);
3380 gtk_label_ensure_layout (label, FALSE);
3381 layout = pango_layout_copy (priv->layout);
3385 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3386 aux_size = aux_info->width;
3388 aux_size = aux_info->height;
3394 pango_layout_set_width (layout, aux_size * PANGO_SCALE);
3396 pango_layout_set_width (layout, allocation * PANGO_SCALE);
3398 pango_layout_get_pixel_size (layout, NULL, &text_height);
3401 *minimum_size = text_height;
3404 *natural_size = text_height;
3406 g_object_unref (layout);
3410 gtk_label_get_size (GtkSizeRequest *widget,
3411 GtkOrientation orientation,
3415 GtkLabel *label = GTK_LABEL (widget);
3416 GtkLabelPriv *priv = label->priv;
3417 PangoRectangle required_rect;
3418 PangoRectangle natural_rect;
3421 /* "width-chars" Hard-coded minimum width:
3422 * - minimum size should be MAX (width-chars, strlen ("..."));
3423 * - natural size should be MAX (width-chars, strlen (priv->text));
3425 * "max-width-chars" User specified maximum size requisition
3426 * - minimum size should be MAX (width-chars, 0)
3427 * - natural size should be MIN (max-width-chars, strlen (priv->text))
3431 /* When calculating ->wrap sometimes we need to invent a size; Ideally we should be doing
3432 * that stuff here instead of inside gtk_label_ensure_layout() */
3434 gtk_label_clear_layout (label);
3435 gtk_label_ensure_layout (label, TRUE);
3437 angle = gtk_label_get_angle (label);
3439 /* Start off with the pixel extents of the rendered layout */
3440 pango_layout_get_extents (priv->layout, NULL, &required_rect);
3441 required_rect.x = required_rect.y = 0;
3443 if (priv->single_line_mode || priv->wrap)
3444 required_rect.height = get_single_line_height (GTK_WIDGET (label), priv->layout);
3446 natural_rect = required_rect;
3448 /* Calculate text width itself based on GtkLabel property rules */
3449 get_label_width (label, &required_rect.width, &natural_rect.width);
3451 /* Now that we have minimum and natural sizes in pango extents, apply a possible transform */
3452 if (priv->have_transform)
3454 PangoLayout *layout = pango_layout_copy (priv->layout);
3455 PangoContext *context = pango_layout_get_context (priv->layout);
3456 const PangoMatrix *matrix = pango_context_get_matrix (context);
3458 pango_layout_set_width (layout, -1);
3459 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_NONE);
3461 pango_layout_get_extents (layout, NULL, &natural_rect);
3462 g_object_unref (layout);
3464 pango_matrix_transform_rectangle (matrix, &required_rect);
3465 pango_matrix_transform_rectangle (matrix, &natural_rect);
3467 /* Bump the natural size in case of ellipsize to ensure pango has
3468 * enough space in the angles (note, we could alternatively set the
3469 * layout to not ellipsize when we know we have been allocated our
3470 * full natural size, or it may be that pango needs a fix here).
3472 if (priv->ellipsize && angle != 0 && angle != 90 && angle != 180 && angle != 270 && angle != 360)
3474 /* For some reason we only need this at about 110 degrees, and only
3475 * when gaining in height
3477 natural_rect.height += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3478 natural_rect.width += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3482 required_rect.width = PANGO_PIXELS_CEIL (required_rect.width);
3483 required_rect.height = PANGO_PIXELS_CEIL (required_rect.height);
3485 natural_rect.width = PANGO_PIXELS_CEIL (natural_rect.width);
3486 natural_rect.height = PANGO_PIXELS_CEIL (natural_rect.height);
3488 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3490 /* Note, we cant use get_size_for_allocation() when rotating
3491 * ellipsized labels.
3493 if (!(priv->ellipsize && priv->have_transform) &&
3494 (angle == 90 || angle == 270))
3496 /* Doing a h4w request on a rotated label here, return the
3497 * required width for the minimum height.
3499 get_size_for_allocation (label,
3500 GTK_ORIENTATION_VERTICAL,
3501 required_rect.height,
3502 minimum_size, natural_size);
3507 /* Normal desired width */
3508 *minimum_size = required_rect.width;
3509 *natural_size = natural_rect.width;
3512 *minimum_size += label->misc.xpad * 2;
3513 *natural_size += label->misc.xpad * 2;
3515 else /* GTK_ORIENTATION_VERTICAL */
3517 /* Note, we cant use get_size_for_allocation() when rotating
3518 * ellipsized labels.
3520 if (!(priv->ellipsize && priv->have_transform) &&
3521 (angle == 0 || angle == 180))
3523 /* Doing a w4h request on a label here, return the required
3524 * height for the minimum width.
3526 get_size_for_allocation (label,
3527 GTK_ORIENTATION_HORIZONTAL,
3528 required_rect.width,
3529 minimum_size, natural_size);
3533 /* A vertically rotated label does w4h, so return the base
3534 * desired height (text length)
3536 *minimum_size = required_rect.height;
3537 *natural_size = natural_rect.height;
3540 *minimum_size += label->misc.ypad * 2;
3541 *natural_size += label->misc.ypad * 2;
3544 /* Restore real allocated size of layout; sometimes size-requests
3545 * are randomly called without a following allocation; for this case
3546 * we need to make sure we dont have a mucked up layout because we
3547 * went and guessed the wrap-size.
3550 gtk_label_clear_layout (label);
3551 gtk_label_ensure_layout (label, FALSE);
3557 gtk_label_get_width (GtkSizeRequest *widget,
3561 gtk_label_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
3565 gtk_label_get_height (GtkSizeRequest *widget,
3569 gtk_label_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
3573 gtk_label_get_width_for_height (GtkSizeRequest *widget,
3575 gint *minimum_width,
3576 gint *natural_width)
3578 GtkLabel *label = GTK_LABEL (widget);
3579 GtkLabelPriv *priv = label->priv;
3580 gdouble angle = gtk_label_get_angle (label);
3582 if (priv->wrap && (angle == 90 || angle == 270))
3585 gtk_label_clear_layout (label);
3587 get_size_for_allocation (label, GTK_ORIENTATION_VERTICAL,
3588 MAX (1, height - (label->misc.ypad * 2)),
3589 minimum_width, natural_width);
3592 *minimum_width += label->misc.xpad * 2;
3595 *natural_width += label->misc.xpad * 2;
3598 GTK_SIZE_REQUEST_GET_IFACE (widget)->get_width (widget, minimum_width, natural_width);
3602 gtk_label_get_height_for_width (GtkSizeRequest *widget,
3604 gint *minimum_height,
3605 gint *natural_height)
3607 GtkLabel *label = GTK_LABEL (widget);
3608 GtkLabelPriv *priv = label->priv;
3609 gdouble angle = gtk_label_get_angle (label);
3611 if (priv->wrap && (angle == 0 || angle == 180 || angle == 360))
3614 gtk_label_clear_layout (label);
3616 get_size_for_allocation (label, GTK_ORIENTATION_HORIZONTAL,
3617 MAX (1, width - label->misc.xpad * 2),
3618 minimum_height, natural_height);
3621 *minimum_height += label->misc.ypad * 2;
3624 *natural_height += label->misc.ypad * 2;
3627 GTK_SIZE_REQUEST_GET_IFACE (widget)->get_height (widget, minimum_height, natural_height);
3631 gtk_label_size_allocate (GtkWidget *widget,
3632 GtkAllocation *allocation)
3634 GtkLabel *label = GTK_LABEL (widget);
3635 GtkLabelPriv *priv = label->priv;
3637 GTK_WIDGET_CLASS (gtk_label_parent_class)->size_allocate (widget, allocation);
3639 /* The layout may have been recently cleared in get_size_for_orientation(),
3640 * but the width at that point may not be the same as the allocated width
3643 gtk_label_clear_layout (label);
3645 gtk_label_ensure_layout (label, FALSE);
3647 if (priv->ellipsize)
3651 PangoRectangle logical;
3652 PangoRectangle bounds;
3654 bounds.x = bounds.y = 0;
3655 bounds.width = allocation->width - label->misc.xpad * 2;
3656 bounds.height = allocation->height - label->misc.ypad * 2;
3658 pango_layout_set_width (priv->layout, -1);
3659 pango_layout_get_pixel_extents (priv->layout, NULL, &logical);
3661 if (priv->have_transform)
3663 PangoContext *context = gtk_widget_get_pango_context (widget);
3664 const PangoMatrix *matrix = pango_context_get_matrix (context);
3666 const gdouble dx = matrix->xx; /* cos (M_PI * angle / 180) */
3667 const gdouble dy = matrix->xy; /* sin (M_PI * angle / 180) */
3668 if (fabs (dy) < 0.01)
3670 if (logical.width > bounds.width)
3671 pango_layout_set_width (priv->layout, bounds.width * PANGO_SCALE);
3673 else if (fabs (dx) < 0.01)
3675 if (logical.width > bounds.height)
3676 pango_layout_set_width (priv->layout, bounds.height * PANGO_SCALE);
3680 gdouble x0, y0, x1, y1, length;
3684 x0 = bounds.width / 2;
3685 y0 = dx ? x0 * dy / dx : G_MAXDOUBLE;
3686 vertical = fabs (y0) > bounds.height / 2;
3690 y0 = bounds.height/2;
3691 x0 = dy ? y0 * dx / dy : G_MAXDOUBLE;
3694 length = 2 * sqrt (x0 * x0 + y0 * y0);
3695 pango_layout_set_width (priv->layout, rint (length * PANGO_SCALE));
3696 pango_layout_get_pixel_size (priv->layout, NULL, &cy);
3703 y0 = bounds.height/2 + y1 - y0;
3708 x0 = bounds.width/2 + x1 - x0;
3712 length = length - sqrt (x0 * x0 + y0 * y0) * 2;
3713 pango_layout_set_width (priv->layout, rint (length * PANGO_SCALE));
3716 else if (logical.width > bounds.width)
3717 pango_layout_set_width (priv->layout, bounds.width * PANGO_SCALE);
3721 if (priv->select_info && priv->select_info->window)
3723 gdk_window_move_resize (priv->select_info->window,
3727 allocation->height);
3732 gtk_label_update_cursor (GtkLabel *label)
3734 GtkLabelPriv *priv = label->priv;
3737 if (!priv->select_info)
3740 widget = GTK_WIDGET (label);
3742 if (gtk_widget_get_realized (widget))
3744 GdkDisplay *display;
3747 if (gtk_widget_is_sensitive (widget))
3749 display = gtk_widget_get_display (widget);
3751 if (priv->select_info->active_link)
3752 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
3753 else if (priv->select_info->selectable)
3754 cursor = gdk_cursor_new_for_display (display, GDK_XTERM);
3761 gdk_window_set_cursor (priv->select_info->window, cursor);
3764 gdk_cursor_unref (cursor);
3769 gtk_label_state_changed (GtkWidget *widget,
3770 GtkStateType prev_state)
3772 GtkLabel *label = GTK_LABEL (widget);
3773 GtkLabelPriv *priv = label->priv;
3775 if (priv->select_info)
3777 gtk_label_select_region (label, 0, 0);
3778 gtk_label_update_cursor (label);
3781 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed)
3782 GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed (widget, prev_state);
3786 gtk_label_style_set (GtkWidget *widget,
3787 GtkStyle *previous_style)
3789 GtkLabel *label = GTK_LABEL (widget);
3791 /* We have to clear the layout, fonts etc. may have changed */
3792 gtk_label_clear_layout (label);
3793 gtk_label_invalidate_wrap_width (label);
3797 gtk_label_direction_changed (GtkWidget *widget,
3798 GtkTextDirection previous_dir)
3800 GtkLabel *label = GTK_LABEL (widget);
3801 GtkLabelPriv *priv = label->priv;
3804 pango_layout_context_changed (priv->layout);
3806 GTK_WIDGET_CLASS (gtk_label_parent_class)->direction_changed (widget, previous_dir);
3810 get_layout_location (GtkLabel *label,
3818 gint req_width, x, y;
3820 PangoRectangle logical;
3823 misc = GTK_MISC (label);
3824 widget = GTK_WIDGET (label);
3826 angle = gtk_label_get_angle (label);
3828 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
3829 xalign = misc->xalign;
3831 xalign = 1.0 - misc->xalign;
3833 pango_layout_get_extents (priv->layout, NULL, &logical);
3835 /* Do the wrap width delimiting before the transform
3837 if (priv->wrap || priv->ellipsize || priv->width_chars > 0)
3841 width = pango_layout_get_width (priv->layout);
3844 logical.width = MIN (width, logical.width);
3847 if (priv->have_transform)
3849 PangoContext *context = gtk_widget_get_pango_context (widget);
3850 const PangoMatrix *matrix = pango_context_get_matrix (context);
3851 pango_matrix_transform_rectangle (matrix, &logical);
3854 pango_extents_to_pixels (&logical, NULL);
3856 req_width = logical.width;
3857 req_height = logical.height;
3859 req_width += 2 * misc->xpad;
3860 req_height += 2 * misc->ypad;
3862 x = floor (widget->allocation.x + (gint)misc->xpad +
3863 xalign * (widget->allocation.width - req_width));
3865 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
3866 x = MAX (x, widget->allocation.x + misc->xpad);
3868 x = MIN (x, widget->allocation.x + widget->allocation.width - misc->xpad);
3873 /* bgo#315462 - For single-line labels, *do* align the requisition with
3874 * respect to the allocation, even if we are under-allocated. For multi-line
3875 * labels, always show the top of the text when they are under-allocated. The
3876 * rationale is this:
3878 * - Single-line labels appear in GtkButtons, and it is very easy to get them
3879 * to be smaller than their requisition. The button may clip the label, but
3880 * the label will still be able to show most of itself and the focus
3881 * rectangle. Also, it is fairly easy to read a single line of clipped text.
3883 * - Multi-line labels should not be clipped to showing "something in the
3884 * middle". You want to read the first line, at least, to get some context.
3886 if (pango_layout_get_line_count (priv->layout) == 1)
3887 y = floor (widget->allocation.y + (gint)misc->ypad
3888 + (widget->allocation.height - req_height) * misc->yalign);
3890 y = floor (widget->allocation.y + (gint)misc->ypad
3891 + MAX (((widget->allocation.height - req_height) * misc->yalign),
3902 draw_insertion_cursor (GtkLabel *label,
3903 GdkRectangle *cursor_location,
3904 gboolean is_primary,
3905 PangoDirection direction,
3906 gboolean draw_arrow)
3908 GtkWidget *widget = GTK_WIDGET (label);
3909 GtkTextDirection text_dir;
3911 if (direction == PANGO_DIRECTION_LTR)
3912 text_dir = GTK_TEXT_DIR_LTR;
3914 text_dir = GTK_TEXT_DIR_RTL;
3916 gtk_draw_insertion_cursor (widget, widget->window, &(widget->allocation),
3918 is_primary, text_dir, draw_arrow);
3921 static PangoDirection
3922 get_cursor_direction (GtkLabel *label)
3924 GtkLabelPriv *priv = label->priv;
3927 g_assert (priv->select_info);
3929 gtk_label_ensure_layout (label, FALSE);
3931 for (l = pango_layout_get_lines_readonly (priv->layout); l; l = l->next)
3933 PangoLayoutLine *line = l->data;
3935 /* If priv->select_info->selection_end is at the very end of
3936 * the line, we don't know if the cursor is on this line or
3937 * the next without looking ahead at the next line. (End
3938 * of paragraph is different from line break.) But it's
3939 * definitely in this paragraph, which is good enough
3940 * to figure out the resolved direction.
3942 if (line->start_index + line->length >= priv->select_info->selection_end)
3943 return line->resolved_dir;
3946 return PANGO_DIRECTION_LTR;
3950 gtk_label_draw_cursor (GtkLabel *label, gint xoffset, gint yoffset)
3952 GtkLabelPriv *priv = label->priv;
3955 if (priv->select_info == NULL)
3958 widget = GTK_WIDGET (label);
3960 if (gtk_widget_is_drawable (widget))
3962 PangoDirection keymap_direction;
3963 PangoDirection cursor_direction;
3964 PangoRectangle strong_pos, weak_pos;
3965 gboolean split_cursor;
3966 PangoRectangle *cursor1 = NULL;
3967 PangoRectangle *cursor2 = NULL;
3968 GdkRectangle cursor_location;
3969 PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
3970 PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
3972 keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gtk_widget_get_display (widget)));
3973 cursor_direction = get_cursor_direction (label);
3975 gtk_label_ensure_layout (label, FALSE);
3977 pango_layout_get_cursor_pos (priv->layout, priv->select_info->selection_end,
3978 &strong_pos, &weak_pos);
3980 g_object_get (gtk_widget_get_settings (widget),
3981 "gtk-split-cursor", &split_cursor,
3984 dir1 = cursor_direction;
3988 cursor1 = &strong_pos;
3990 if (strong_pos.x != weak_pos.x ||
3991 strong_pos.y != weak_pos.y)
3993 dir2 = (cursor_direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
3994 cursor2 = &weak_pos;
3999 if (keymap_direction == cursor_direction)
4000 cursor1 = &strong_pos;
4002 cursor1 = &weak_pos;
4005 cursor_location.x = xoffset + PANGO_PIXELS (cursor1->x);
4006 cursor_location.y = yoffset + PANGO_PIXELS (cursor1->y);
4007 cursor_location.width = 0;
4008 cursor_location.height = PANGO_PIXELS (cursor1->height);
4010 draw_insertion_cursor (label,
4011 &cursor_location, TRUE, dir1,
4012 dir2 != PANGO_DIRECTION_NEUTRAL);
4014 if (dir2 != PANGO_DIRECTION_NEUTRAL)
4016 cursor_location.x = xoffset + PANGO_PIXELS (cursor2->x);
4017 cursor_location.y = yoffset + PANGO_PIXELS (cursor2->y);
4018 cursor_location.width = 0;
4019 cursor_location.height = PANGO_PIXELS (cursor2->height);
4021 draw_insertion_cursor (label,
4022 &cursor_location, FALSE, dir2,
4028 static GtkLabelLink *
4029 gtk_label_get_focus_link (GtkLabel *label)
4031 GtkLabelPriv *priv = label->priv;
4032 GtkLabelSelectionInfo *info = priv->select_info;
4038 if (info->selection_anchor != info->selection_end)
4041 for (l = info->links; l; l = l->next)
4043 GtkLabelLink *link = l->data;
4044 if (link->start <= info->selection_anchor &&
4045 info->selection_anchor <= link->end)
4053 gtk_label_expose (GtkWidget *widget,
4054 GdkEventExpose *event)
4056 GtkLabel *label = GTK_LABEL (widget);
4057 GtkLabelPriv *priv = label->priv;
4058 GtkLabelSelectionInfo *info = priv->select_info;
4061 gtk_label_ensure_layout (label, FALSE);
4063 if (gtk_widget_get_visible (widget) && gtk_widget_get_mapped (widget) &&
4064 priv->text && (*priv->text != '\0'))
4066 get_layout_location (label, &x, &y);
4068 gtk_paint_layout (widget->style,
4070 gtk_widget_get_state (widget),
4079 (info->selection_anchor != info->selection_end))
4082 cairo_region_t *clip;
4085 range[0] = info->selection_anchor;
4086 range[1] = info->selection_end;
4088 if (range[0] > range[1])
4090 gint tmp = range[0];
4091 range[0] = range[1];
4095 clip = gdk_pango_layout_get_clip_region (priv->layout,
4099 cairo_region_intersect (clip, event->region);
4101 /* FIXME should use gtk_paint, but it can't use a clip
4105 gdk_gc_set_clip_region (widget->style->black_gc, clip);
4108 state = GTK_STATE_SELECTED;
4109 if (!gtk_widget_has_focus (widget))
4110 state = GTK_STATE_ACTIVE;
4112 gdk_draw_layout_with_colors (widget->window,
4113 widget->style->black_gc,
4116 &widget->style->text[state],
4117 &widget->style->base[state]);
4119 gdk_gc_set_clip_region (widget->style->black_gc, NULL);
4120 cairo_region_destroy (clip);
4124 GtkLabelLink *focus_link;
4125 GtkLabelLink *active_link;
4127 cairo_region_t *clip;
4129 GdkColor *text_color;
4130 GdkColor *base_color;
4131 GdkColor *link_color;
4132 GdkColor *visited_link_color;
4134 if (info->selectable && gtk_widget_has_focus (widget))
4135 gtk_label_draw_cursor (label, x, y);
4137 focus_link = gtk_label_get_focus_link (label);
4138 active_link = info->active_link;
4142 range[0] = active_link->start;
4143 range[1] = active_link->end;
4145 clip = gdk_pango_layout_get_clip_region (priv->layout,
4149 gdk_gc_set_clip_region (widget->style->black_gc, clip);
4151 gtk_label_get_link_colors (widget, &link_color, &visited_link_color);
4152 if (active_link->visited)
4153 text_color = visited_link_color;
4155 text_color = link_color;
4156 if (info->link_clicked)
4157 base_color = &widget->style->base[GTK_STATE_ACTIVE];
4159 base_color = &widget->style->base[GTK_STATE_PRELIGHT];
4160 gdk_draw_layout_with_colors (widget->window,
4161 widget->style->black_gc,
4166 gdk_color_free (link_color);
4167 gdk_color_free (visited_link_color);
4169 gdk_gc_set_clip_region (widget->style->black_gc, NULL);
4170 cairo_region_destroy (clip);
4173 if (focus_link && gtk_widget_has_focus (widget))
4175 range[0] = focus_link->start;
4176 range[1] = focus_link->end;
4178 clip = gdk_pango_layout_get_clip_region (priv->layout,
4182 cairo_region_get_extents (clip, &rect);
4184 gtk_paint_focus (widget->style, widget->window, gtk_widget_get_state (widget),
4185 &event->area, widget, "label",
4186 rect.x, rect.y, rect.width, rect.height);
4188 cairo_region_destroy (clip);
4197 separate_uline_pattern (const gchar *str,
4202 gboolean underscore;
4205 gchar *pattern_dest;
4207 *accel_key = GDK_VoidSymbol;
4208 *new_str = g_new (gchar, strlen (str) + 1);
4209 *pattern = g_new (gchar, g_utf8_strlen (str, -1) + 1);
4215 pattern_dest = *pattern;
4220 const gchar *next_src;
4222 c = g_utf8_get_char (src);
4223 if (c == (gunichar)-1)
4225 g_warning ("Invalid input string");
4231 next_src = g_utf8_next_char (src);
4236 *pattern_dest++ = ' ';
4239 *pattern_dest++ = '_';
4240 if (*accel_key == GDK_VoidSymbol)
4241 *accel_key = gdk_keyval_to_lower (gdk_unicode_to_keyval (c));
4244 while (src < next_src)
4258 while (src < next_src)
4261 *pattern_dest++ = ' ';
4273 gtk_label_set_uline_text_internal (GtkLabel *label,
4276 GtkLabelPriv *priv = label->priv;
4277 guint accel_key = GDK_VoidSymbol;
4281 g_return_if_fail (GTK_IS_LABEL (label));
4282 g_return_if_fail (str != NULL);
4284 /* Split text into the base text and a separate pattern
4287 if (!separate_uline_pattern (str, &accel_key, &new_str, &pattern))
4290 gtk_label_set_text_internal (label, new_str);
4291 gtk_label_set_pattern_internal (label, pattern, TRUE);
4292 priv->mnemonic_keyval = accel_key;
4298 * gtk_label_set_text_with_mnemonic:
4299 * @label: a #GtkLabel
4302 * Sets the label's text from the string @str.
4303 * If characters in @str are preceded by an underscore, they are underlined
4304 * indicating that they represent a keyboard accelerator called a mnemonic.
4305 * The mnemonic key can be used to activate another widget, chosen
4306 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
4309 gtk_label_set_text_with_mnemonic (GtkLabel *label,
4312 g_return_if_fail (GTK_IS_LABEL (label));
4313 g_return_if_fail (str != NULL);
4315 g_object_freeze_notify (G_OBJECT (label));
4317 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
4318 gtk_label_set_use_markup_internal (label, FALSE);
4319 gtk_label_set_use_underline_internal (label, TRUE);
4321 gtk_label_recalculate (label);
4323 g_object_thaw_notify (G_OBJECT (label));
4327 gtk_label_realize (GtkWidget *widget)
4329 GtkLabel *label = GTK_LABEL (widget);
4330 GtkLabelPriv *priv = label->priv;
4332 GTK_WIDGET_CLASS (gtk_label_parent_class)->realize (widget);
4334 if (priv->select_info)
4335 gtk_label_create_window (label);
4339 gtk_label_unrealize (GtkWidget *widget)
4341 GtkLabel *label = GTK_LABEL (widget);
4342 GtkLabelPriv *priv = label->priv;
4344 if (priv->select_info)
4345 gtk_label_destroy_window (label);
4347 GTK_WIDGET_CLASS (gtk_label_parent_class)->unrealize (widget);
4351 gtk_label_map (GtkWidget *widget)
4353 GtkLabel *label = GTK_LABEL (widget);
4354 GtkLabelPriv *priv = label->priv;
4356 GTK_WIDGET_CLASS (gtk_label_parent_class)->map (widget);
4358 if (priv->select_info)
4359 gdk_window_show (priv->select_info->window);
4363 gtk_label_unmap (GtkWidget *widget)
4365 GtkLabel *label = GTK_LABEL (widget);
4366 GtkLabelPriv *priv = label->priv;
4368 if (priv->select_info)
4369 gdk_window_hide (priv->select_info->window);
4371 GTK_WIDGET_CLASS (gtk_label_parent_class)->unmap (widget);
4375 window_to_layout_coords (GtkLabel *label,
4382 widget = GTK_WIDGET (label);
4384 /* get layout location in widget->window coords */
4385 get_layout_location (label, &lx, &ly);
4389 *x += widget->allocation.x; /* go to widget->window */
4390 *x -= lx; /* go to layout */
4395 *y += widget->allocation.y; /* go to widget->window */
4396 *y -= ly; /* go to layout */
4402 layout_to_window_coords (GtkLabel *label,
4409 widget = GTK_WIDGET (label);
4411 /* get layout location in widget->window coords */
4412 get_layout_location (label, &lx, &ly);
4416 *x += lx; /* go to widget->window */
4417 *x -= widget->allocation.x; /* go to selection window */
4422 *y += ly; /* go to widget->window */
4423 *y -= widget->allocation.y; /* go to selection window */
4429 get_layout_index (GtkLabel *label,
4434 GtkLabelPriv *priv = label->priv;
4436 const gchar *cluster;
4437 const gchar *cluster_end;
4442 gtk_label_ensure_layout (label, FALSE);
4444 window_to_layout_coords (label, &x, &y);
4449 inside = pango_layout_xy_to_index (priv->layout,
4453 cluster = priv->text + *index;
4454 cluster_end = cluster;
4457 cluster_end = g_utf8_next_char (cluster_end);
4461 *index += (cluster_end - cluster);
4467 gtk_label_select_word (GtkLabel *label)
4469 GtkLabelPriv *priv = label->priv;
4472 gint start_index = gtk_label_move_backward_word (label, priv->select_info->selection_end);
4473 gint end_index = gtk_label_move_forward_word (label, priv->select_info->selection_end);
4475 min = MIN (priv->select_info->selection_anchor,
4476 priv->select_info->selection_end);
4477 max = MAX (priv->select_info->selection_anchor,
4478 priv->select_info->selection_end);
4480 min = MIN (min, start_index);
4481 max = MAX (max, end_index);
4483 gtk_label_select_region_index (label, min, max);
4487 gtk_label_grab_focus (GtkWidget *widget)
4489 GtkLabel *label = GTK_LABEL (widget);
4490 GtkLabelPriv *priv = label->priv;
4491 gboolean select_on_focus;
4494 if (priv->select_info == NULL)
4497 GTK_WIDGET_CLASS (gtk_label_parent_class)->grab_focus (widget);
4499 if (priv->select_info->selectable)
4501 g_object_get (gtk_widget_get_settings (widget),
4502 "gtk-label-select-on-focus",
4506 if (select_on_focus && !priv->in_click)
4507 gtk_label_select_region (label, 0, -1);
4511 if (priv->select_info->links && !priv->in_click)
4513 link = priv->select_info->links->data;
4514 priv->select_info->selection_anchor = link->start;
4515 priv->select_info->selection_end = link->start;
4521 gtk_label_focus (GtkWidget *widget,
4522 GtkDirectionType direction)
4524 GtkLabel *label = GTK_LABEL (widget);
4525 GtkLabelPriv *priv = label->priv;
4526 GtkLabelSelectionInfo *info = priv->select_info;
4527 GtkLabelLink *focus_link;
4530 if (!gtk_widget_is_focus (widget))
4532 gtk_widget_grab_focus (widget);
4535 focus_link = gtk_label_get_focus_link (label);
4536 if (focus_link && direction == GTK_DIR_TAB_BACKWARD)
4538 l = g_list_last (info->links);
4539 focus_link = l->data;
4540 info->selection_anchor = focus_link->start;
4541 info->selection_end = focus_link->start;
4551 if (info->selectable)
4555 if (info->selection_anchor != info->selection_end)
4558 index = info->selection_anchor;
4560 if (direction == GTK_DIR_TAB_FORWARD)
4561 for (l = info->links; l; l = l->next)
4563 GtkLabelLink *link = l->data;
4565 if (link->start > index)
4567 gtk_label_select_region_index (label, link->start, link->start);
4571 else if (direction == GTK_DIR_TAB_BACKWARD)
4572 for (l = g_list_last (info->links); l; l = l->prev)
4574 GtkLabelLink *link = l->data;
4576 if (link->end < index)
4578 gtk_label_select_region_index (label, link->start, link->start);
4587 focus_link = gtk_label_get_focus_link (label);
4590 case GTK_DIR_TAB_FORWARD:
4593 l = g_list_find (info->links, focus_link);
4600 case GTK_DIR_TAB_BACKWARD:
4603 l = g_list_find (info->links, focus_link);
4607 l = g_list_last (info->links);
4616 focus_link = l->data;
4617 info->selection_anchor = focus_link->start;
4618 info->selection_end = focus_link->start;
4619 gtk_widget_queue_draw (widget);
4631 gtk_label_button_press (GtkWidget *widget,
4632 GdkEventButton *event)
4634 GtkLabel *label = GTK_LABEL (widget);
4635 GtkLabelPriv *priv = label->priv;
4636 GtkLabelSelectionInfo *info = priv->select_info;
4643 if (info->active_link)
4645 if (event->button == 1)
4647 info->link_clicked = 1;
4648 gtk_widget_queue_draw (widget);
4650 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4652 info->link_clicked = 1;
4653 gtk_label_do_popup (label, event);
4658 if (!info->selectable)
4661 info->in_drag = FALSE;
4662 info->select_words = FALSE;
4664 if (event->button == 1)
4666 if (!gtk_widget_has_focus (widget))
4668 priv->in_click = TRUE;
4669 gtk_widget_grab_focus (widget);
4670 priv->in_click = FALSE;
4673 if (event->type == GDK_3BUTTON_PRESS)
4675 gtk_label_select_region_index (label, 0, strlen (priv->text));
4679 if (event->type == GDK_2BUTTON_PRESS)
4681 info->select_words = TRUE;
4682 gtk_label_select_word (label);
4686 get_layout_index (label, event->x, event->y, &index);
4688 min = MIN (info->selection_anchor, info->selection_end);
4689 max = MAX (info->selection_anchor, info->selection_end);
4691 if ((info->selection_anchor != info->selection_end) &&
4692 (event->state & GDK_SHIFT_MASK))
4694 /* extend (same as motion) */
4695 min = MIN (min, index);
4696 max = MAX (max, index);
4698 /* ensure the anchor is opposite index */
4706 gtk_label_select_region_index (label, min, max);
4710 if (event->type == GDK_3BUTTON_PRESS)
4711 gtk_label_select_region_index (label, 0, strlen (priv->text));
4712 else if (event->type == GDK_2BUTTON_PRESS)
4713 gtk_label_select_word (label);
4714 else if (min < max && min <= index && index <= max)
4716 info->in_drag = TRUE;
4717 info->drag_start_x = event->x;
4718 info->drag_start_y = event->y;
4721 /* start a replacement */
4722 gtk_label_select_region_index (label, index, index);
4727 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4729 gtk_label_do_popup (label, event);
4737 gtk_label_button_release (GtkWidget *widget,
4738 GdkEventButton *event)
4741 GtkLabel *label = GTK_LABEL (widget);
4742 GtkLabelPriv *priv = label->priv;
4743 GtkLabelSelectionInfo *info = priv->select_info;
4753 get_layout_index (label, event->x, event->y, &index);
4754 gtk_label_select_region_index (label, index, index);
4759 if (event->button != 1)
4762 if (info->active_link &&
4763 info->selection_anchor == info->selection_end &&
4766 emit_activate_link (label, info->active_link);
4767 info->link_clicked = 0;
4772 /* The goal here is to return TRUE iff we ate the
4773 * button press to start selecting.
4779 connect_mnemonics_visible_notify (GtkLabel *label)
4781 GtkLabelPriv *priv = label->priv;
4782 GtkWidget *toplevel;
4785 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
4787 if (!GTK_IS_WINDOW (toplevel))
4790 /* always set up this widgets initial value */
4791 priv->mnemonics_visible =
4792 gtk_window_get_mnemonics_visible (GTK_WINDOW (toplevel));
4795 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (toplevel),
4796 "gtk-label-mnemonics-visible-connected"));
4800 g_signal_connect (toplevel,
4801 "notify::mnemonics-visible",
4802 G_CALLBACK (label_mnemonics_visible_changed),
4804 g_object_set_data (G_OBJECT (toplevel),
4805 "gtk-label-mnemonics-visible-connected",
4806 GINT_TO_POINTER (1));
4811 drag_begin_cb (GtkWidget *widget,
4812 GdkDragContext *context,
4815 GtkLabel *label = GTK_LABEL (widget);
4816 GtkLabelPriv *priv = label->priv;
4817 GdkPixmap *pixmap = NULL;
4819 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
4821 if ((priv->select_info->selection_anchor !=
4822 priv->select_info->selection_end) &&
4828 start = MIN (priv->select_info->selection_anchor,
4829 priv->select_info->selection_end);
4830 end = MAX (priv->select_info->selection_anchor,
4831 priv->select_info->selection_end);
4833 len = strlen (priv->text);
4841 pixmap = _gtk_text_util_create_drag_icon (widget,
4847 gtk_drag_set_icon_pixmap (context,
4848 gdk_drawable_get_colormap (pixmap),
4853 gtk_drag_set_icon_default (context);
4856 g_object_unref (pixmap);
4860 gtk_label_motion (GtkWidget *widget,
4861 GdkEventMotion *event)
4863 GtkLabel *label = GTK_LABEL (widget);
4864 GtkLabelPriv *priv = label->priv;
4865 GtkLabelSelectionInfo *info = priv->select_info;
4871 if (info->links && !info->in_drag)
4875 gboolean found = FALSE;
4877 if (info->selection_anchor == info->selection_end)
4879 if (get_layout_index (label, event->x, event->y, &index))
4881 for (l = info->links; l != NULL; l = l->next)
4884 if (index >= link->start && index <= link->end)
4895 if (info->active_link != link)
4897 info->link_clicked = 0;
4898 info->active_link = link;
4899 gtk_label_update_cursor (label);
4900 gtk_widget_queue_draw (widget);
4905 if (info->active_link != NULL)
4907 info->link_clicked = 0;
4908 info->active_link = NULL;
4909 gtk_label_update_cursor (label);
4910 gtk_widget_queue_draw (widget);
4915 if (!info->selectable)
4918 if ((event->state & GDK_BUTTON1_MASK) == 0)
4923 if (gtk_drag_check_threshold (widget,
4926 event->x, event->y))
4928 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
4930 gtk_target_list_add_text_targets (target_list, 0);
4932 g_signal_connect (widget, "drag-begin",
4933 G_CALLBACK (drag_begin_cb), NULL);
4934 gtk_drag_begin (widget, target_list,
4936 1, (GdkEvent *)event);
4938 info->in_drag = FALSE;
4940 gtk_target_list_unref (target_list);
4947 gdk_window_get_device_position (info->window, event->device, &x, &y, NULL);
4948 get_layout_index (label, x, y, &index);
4950 if (info->select_words)
4953 gint old_min, old_max;
4956 min = gtk_label_move_backward_word (label, index);
4957 max = gtk_label_move_forward_word (label, index);
4959 anchor = info->selection_anchor;
4960 end = info->selection_end;
4962 old_min = MIN (anchor, end);
4963 old_max = MAX (anchor, end);
4970 else if (old_max < max)
4975 else if (anchor == old_min)
4986 gtk_label_select_region_index (label, anchor, end);
4989 gtk_label_select_region_index (label, info->selection_anchor, index);
4996 gtk_label_leave_notify (GtkWidget *widget,
4997 GdkEventCrossing *event)
4999 GtkLabel *label = GTK_LABEL (widget);
5000 GtkLabelPriv *priv = label->priv;
5002 if (priv->select_info)
5004 priv->select_info->active_link = NULL;
5005 gtk_label_update_cursor (label);
5006 gtk_widget_queue_draw (widget);
5009 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event)
5010 return GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event (widget, event);
5016 gtk_label_create_window (GtkLabel *label)
5018 GtkLabelPriv *priv = label->priv;
5020 GdkWindowAttr attributes;
5021 gint attributes_mask;
5023 g_assert (priv->select_info);
5024 widget = GTK_WIDGET (label);
5025 g_assert (gtk_widget_get_realized (widget));
5027 if (priv->select_info->window)
5030 attributes.x = widget->allocation.x;
5031 attributes.y = widget->allocation.y;
5032 attributes.width = widget->allocation.width;
5033 attributes.height = widget->allocation.height;
5034 attributes.window_type = GDK_WINDOW_CHILD;
5035 attributes.wclass = GDK_INPUT_ONLY;
5036 attributes.override_redirect = TRUE;
5037 attributes.event_mask = gtk_widget_get_events (widget) |
5038 GDK_BUTTON_PRESS_MASK |
5039 GDK_BUTTON_RELEASE_MASK |
5040 GDK_LEAVE_NOTIFY_MASK |
5041 GDK_BUTTON_MOTION_MASK |
5042 GDK_POINTER_MOTION_MASK |
5043 GDK_POINTER_MOTION_HINT_MASK;
5044 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
5045 if (gtk_widget_is_sensitive (widget))
5047 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
5049 attributes_mask |= GDK_WA_CURSOR;
5053 priv->select_info->window = gdk_window_new (widget->window,
5054 &attributes, attributes_mask);
5055 gdk_window_set_user_data (priv->select_info->window, widget);
5057 if (attributes_mask & GDK_WA_CURSOR)
5058 gdk_cursor_unref (attributes.cursor);
5062 gtk_label_destroy_window (GtkLabel *label)
5064 GtkLabelPriv *priv = label->priv;
5066 g_assert (priv->select_info);
5068 if (priv->select_info->window == NULL)
5071 gdk_window_set_user_data (priv->select_info->window, NULL);
5072 gdk_window_destroy (priv->select_info->window);
5073 priv->select_info->window = NULL;
5077 gtk_label_ensure_select_info (GtkLabel *label)
5079 GtkLabelPriv *priv = label->priv;
5081 if (priv->select_info == NULL)
5083 priv->select_info = g_new0 (GtkLabelSelectionInfo, 1);
5085 gtk_widget_set_can_focus (GTK_WIDGET (label), TRUE);
5087 if (gtk_widget_get_realized (GTK_WIDGET (label)))
5088 gtk_label_create_window (label);
5090 if (gtk_widget_get_mapped (GTK_WIDGET (label)))
5091 gdk_window_show (priv->select_info->window);
5096 gtk_label_clear_select_info (GtkLabel *label)
5098 GtkLabelPriv *priv = label->priv;
5100 if (priv->select_info == NULL)
5103 if (!priv->select_info->selectable && !priv->select_info->links)
5105 gtk_label_destroy_window (label);
5107 g_free (priv->select_info);
5108 priv->select_info = NULL;
5110 gtk_widget_set_can_focus (GTK_WIDGET (label), FALSE);
5115 * gtk_label_set_selectable:
5116 * @label: a #GtkLabel
5117 * @setting: %TRUE to allow selecting text in the label
5119 * Selectable labels allow the user to select text from the label, for
5123 gtk_label_set_selectable (GtkLabel *label,
5127 gboolean old_setting;
5129 g_return_if_fail (GTK_IS_LABEL (label));
5133 setting = setting != FALSE;
5134 old_setting = priv->select_info && priv->select_info->selectable;
5138 gtk_label_ensure_select_info (label);
5139 priv->select_info->selectable = TRUE;
5140 gtk_label_update_cursor (label);
5146 /* unselect, to give up the selection */
5147 gtk_label_select_region (label, 0, 0);
5149 priv->select_info->selectable = FALSE;
5150 gtk_label_clear_select_info (label);
5151 gtk_label_update_cursor (label);
5154 if (setting != old_setting)
5156 g_object_freeze_notify (G_OBJECT (label));
5157 g_object_notify (G_OBJECT (label), "selectable");
5158 g_object_notify (G_OBJECT (label), "cursor-position");
5159 g_object_notify (G_OBJECT (label), "selection-bound");
5160 g_object_thaw_notify (G_OBJECT (label));
5161 gtk_widget_queue_draw (GTK_WIDGET (label));
5166 * gtk_label_get_selectable:
5167 * @label: a #GtkLabel
5169 * Gets the value set by gtk_label_set_selectable().
5171 * Return value: %TRUE if the user can copy text from the label
5174 gtk_label_get_selectable (GtkLabel *label)
5178 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5182 return priv->select_info && priv->select_info->selectable;
5186 free_angle (gpointer angle)
5188 g_slice_free (gdouble, angle);
5192 * gtk_label_set_angle:
5193 * @label: a #GtkLabel
5194 * @angle: the angle that the baseline of the label makes with
5195 * the horizontal, in degrees, measured counterclockwise
5197 * Sets the angle of rotation for the label. An angle of 90 reads from
5198 * from bottom to top, an angle of 270, from top to bottom. The angle
5199 * setting for the label is ignored if the label is selectable,
5200 * wrapped, or ellipsized.
5205 gtk_label_set_angle (GtkLabel *label,
5208 gdouble *label_angle;
5210 g_return_if_fail (GTK_IS_LABEL (label));
5212 label_angle = (gdouble *)g_object_get_qdata (G_OBJECT (label), quark_angle);
5216 label_angle = g_slice_new (gdouble);
5218 g_object_set_qdata_full (G_OBJECT (label), quark_angle,
5219 label_angle, free_angle);
5222 /* Canonicalize to [0,360]. We don't canonicalize 360 to 0, because
5223 * double property ranges are inclusive, and changing 360 to 0 would
5224 * make a property editor behave strangely.
5226 if (angle < 0 || angle > 360.0)
5227 angle = angle - 360. * floor (angle / 360.);
5229 if (*label_angle != angle)
5231 *label_angle = angle;
5233 gtk_label_clear_layout (label);
5234 gtk_widget_queue_resize (GTK_WIDGET (label));
5236 g_object_notify (G_OBJECT (label), "angle");
5241 * gtk_label_get_angle:
5242 * @label: a #GtkLabel
5244 * Gets the angle of rotation for the label. See
5245 * gtk_label_set_angle().
5247 * Return value: the angle of rotation for the label
5252 gtk_label_get_angle (GtkLabel *label)
5256 g_return_val_if_fail (GTK_IS_LABEL (label), 0.0);
5258 angle = (gdouble *)g_object_get_qdata (G_OBJECT (label), quark_angle);
5267 gtk_label_set_selection_text (GtkLabel *label,
5268 GtkSelectionData *selection_data)
5270 GtkLabelPriv *priv = label->priv;
5272 if ((priv->select_info->selection_anchor !=
5273 priv->select_info->selection_end) &&
5279 start = MIN (priv->select_info->selection_anchor,
5280 priv->select_info->selection_end);
5281 end = MAX (priv->select_info->selection_anchor,
5282 priv->select_info->selection_end);
5284 len = strlen (priv->text);
5292 gtk_selection_data_set_text (selection_data,
5299 gtk_label_drag_data_get (GtkWidget *widget,
5300 GdkDragContext *context,
5301 GtkSelectionData *selection_data,
5305 gtk_label_set_selection_text (GTK_LABEL (widget), selection_data);
5309 get_text_callback (GtkClipboard *clipboard,
5310 GtkSelectionData *selection_data,
5312 gpointer user_data_or_owner)
5314 gtk_label_set_selection_text (GTK_LABEL (user_data_or_owner), selection_data);
5318 clear_text_callback (GtkClipboard *clipboard,
5319 gpointer user_data_or_owner)
5324 label = GTK_LABEL (user_data_or_owner);
5327 if (priv->select_info)
5329 priv->select_info->selection_anchor = priv->select_info->selection_end;
5331 gtk_widget_queue_draw (GTK_WIDGET (label));
5336 gtk_label_select_region_index (GtkLabel *label,
5342 g_return_if_fail (GTK_IS_LABEL (label));
5346 if (priv->select_info && priv->select_info->selectable)
5348 GtkClipboard *clipboard;
5350 if (priv->select_info->selection_anchor == anchor_index &&
5351 priv->select_info->selection_end == end_index)
5354 priv->select_info->selection_anchor = anchor_index;
5355 priv->select_info->selection_end = end_index;
5357 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label),
5358 GDK_SELECTION_PRIMARY);
5360 if (anchor_index != end_index)
5362 GtkTargetList *list;
5363 GtkTargetEntry *targets;
5366 list = gtk_target_list_new (NULL, 0);
5367 gtk_target_list_add_text_targets (list, 0);
5368 targets = gtk_target_table_new_from_list (list, &n_targets);
5370 gtk_clipboard_set_with_owner (clipboard,
5373 clear_text_callback,
5376 gtk_target_table_free (targets, n_targets);
5377 gtk_target_list_unref (list);
5381 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (label))
5382 gtk_clipboard_clear (clipboard);
5385 gtk_widget_queue_draw (GTK_WIDGET (label));
5387 g_object_freeze_notify (G_OBJECT (label));
5388 g_object_notify (G_OBJECT (label), "cursor-position");
5389 g_object_notify (G_OBJECT (label), "selection-bound");
5390 g_object_thaw_notify (G_OBJECT (label));
5395 * gtk_label_select_region:
5396 * @label: a #GtkLabel
5397 * @start_offset: start offset (in characters not bytes)
5398 * @end_offset: end offset (in characters not bytes)
5400 * Selects a range of characters in the label, if the label is selectable.
5401 * See gtk_label_set_selectable(). If the label is not selectable,
5402 * this function has no effect. If @start_offset or
5403 * @end_offset are -1, then the end of the label will be substituted.
5406 gtk_label_select_region (GtkLabel *label,
5412 g_return_if_fail (GTK_IS_LABEL (label));
5416 if (priv->text && priv->select_info)
5418 if (start_offset < 0)
5419 start_offset = g_utf8_strlen (priv->text, -1);
5422 end_offset = g_utf8_strlen (priv->text, -1);
5424 gtk_label_select_region_index (label,
5425 g_utf8_offset_to_pointer (priv->text, start_offset) - priv->text,
5426 g_utf8_offset_to_pointer (priv->text, end_offset) - priv->text);
5431 * gtk_label_get_selection_bounds:
5432 * @label: a #GtkLabel
5433 * @start: return location for start of selection, as a character offset
5434 * @end: return location for end of selection, as a character offset
5436 * Gets the selected range of characters in the label, returning %TRUE
5437 * if there's a selection.
5439 * Return value: %TRUE if selection is non-empty
5442 gtk_label_get_selection_bounds (GtkLabel *label,
5448 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5452 if (priv->select_info == NULL)
5454 /* not a selectable label */
5464 gint start_index, end_index;
5465 gint start_offset, end_offset;
5468 start_index = MIN (priv->select_info->selection_anchor,
5469 priv->select_info->selection_end);
5470 end_index = MAX (priv->select_info->selection_anchor,
5471 priv->select_info->selection_end);
5473 len = strlen (priv->text);
5475 if (end_index > len)
5478 if (start_index > len)
5481 start_offset = g_utf8_strlen (priv->text, start_index);
5482 end_offset = g_utf8_strlen (priv->text, end_index);
5484 if (start_offset > end_offset)
5486 gint tmp = start_offset;
5487 start_offset = end_offset;
5492 *start = start_offset;
5497 return start_offset != end_offset;
5503 * gtk_label_get_layout:
5504 * @label: a #GtkLabel
5506 * Gets the #PangoLayout used to display the label.
5507 * The layout is useful to e.g. convert text positions to
5508 * pixel positions, in combination with gtk_label_get_layout_offsets().
5509 * The returned layout is owned by the label so need not be
5510 * freed by the caller.
5512 * Return value: (transfer none): the #PangoLayout for this label
5515 gtk_label_get_layout (GtkLabel *label)
5519 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
5523 gtk_label_ensure_layout (label, FALSE);
5525 return priv->layout;
5529 * gtk_label_get_layout_offsets:
5530 * @label: a #GtkLabel
5531 * @x: (allow-none): location to store X offset of layout, or %NULL
5532 * @y: (allow-none): location to store Y offset of layout, or %NULL
5534 * Obtains the coordinates where the label will draw the #PangoLayout
5535 * representing the text in the label; useful to convert mouse events
5536 * into coordinates inside the #PangoLayout, e.g. to take some action
5537 * if some part of the label is clicked. Of course you will need to
5538 * create a #GtkEventBox to receive the events, and pack the label
5539 * inside it, since labels are a #GTK_NO_WINDOW widget. Remember
5540 * when using the #PangoLayout functions you need to convert to
5541 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
5544 gtk_label_get_layout_offsets (GtkLabel *label,
5548 g_return_if_fail (GTK_IS_LABEL (label));
5550 gtk_label_ensure_layout (label, FALSE);
5552 get_layout_location (label, x, y);
5556 * gtk_label_set_use_markup:
5557 * @label: a #GtkLabel
5558 * @setting: %TRUE if the label's text should be parsed for markup.
5560 * Sets whether the text of the label contains markup in <link
5561 * linkend="PangoMarkupFormat">Pango's text markup
5562 * language</link>. See gtk_label_set_markup().
5565 gtk_label_set_use_markup (GtkLabel *label,
5568 g_return_if_fail (GTK_IS_LABEL (label));
5570 gtk_label_set_use_markup_internal (label, setting);
5571 gtk_label_recalculate (label);
5575 * gtk_label_get_use_markup:
5576 * @label: a #GtkLabel
5578 * Returns whether the label's text is interpreted as marked up with
5579 * the <link linkend="PangoMarkupFormat">Pango text markup
5580 * language</link>. See gtk_label_set_use_markup ().
5582 * Return value: %TRUE if the label's text will be parsed for markup.
5585 gtk_label_get_use_markup (GtkLabel *label)
5587 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5589 return label->priv->use_markup;
5593 * gtk_label_set_use_underline:
5594 * @label: a #GtkLabel
5595 * @setting: %TRUE if underlines in the text indicate mnemonics
5597 * If true, an underline in the text indicates the next character should be
5598 * used for the mnemonic accelerator key.
5601 gtk_label_set_use_underline (GtkLabel *label,
5604 g_return_if_fail (GTK_IS_LABEL (label));
5606 gtk_label_set_use_underline_internal (label, setting);
5607 gtk_label_recalculate (label);
5611 * gtk_label_get_use_underline:
5612 * @label: a #GtkLabel
5614 * Returns whether an embedded underline in the label indicates a
5615 * mnemonic. See gtk_label_set_use_underline().
5617 * Return value: %TRUE whether an embedded underline in the label indicates
5618 * the mnemonic accelerator keys.
5621 gtk_label_get_use_underline (GtkLabel *label)
5623 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5625 return label->priv->use_underline;
5629 * gtk_label_set_single_line_mode:
5630 * @label: a #GtkLabel
5631 * @single_line_mode: %TRUE if the label should be in single line mode
5633 * Sets whether the label is in single line mode.
5638 gtk_label_set_single_line_mode (GtkLabel *label,
5639 gboolean single_line_mode)
5643 g_return_if_fail (GTK_IS_LABEL (label));
5647 single_line_mode = single_line_mode != FALSE;
5649 if (priv->single_line_mode != single_line_mode)
5651 priv->single_line_mode = single_line_mode;
5653 gtk_label_clear_layout (label);
5654 gtk_widget_queue_resize (GTK_WIDGET (label));
5656 g_object_notify (G_OBJECT (label), "single-line-mode");
5661 * gtk_label_get_single_line_mode:
5662 * @label: a #GtkLabel
5664 * Returns whether the label is in single line mode.
5666 * Return value: %TRUE when the label is in single line mode.
5671 gtk_label_get_single_line_mode (GtkLabel *label)
5673 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5675 return label->priv->single_line_mode;
5678 /* Compute the X position for an offset that corresponds to the "more important
5679 * cursor position for that offset. We use this when trying to guess to which
5680 * end of the selection we should go to when the user hits the left or
5684 get_better_cursor (GtkLabel *label,
5689 GtkLabelPriv *priv = label->priv;
5690 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5691 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5692 PangoDirection cursor_direction = get_cursor_direction (label);
5693 gboolean split_cursor;
5694 PangoRectangle strong_pos, weak_pos;
5696 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5697 "gtk-split-cursor", &split_cursor,
5700 gtk_label_ensure_layout (label, FALSE);
5702 pango_layout_get_cursor_pos (priv->layout, index,
5703 &strong_pos, &weak_pos);
5707 *x = strong_pos.x / PANGO_SCALE;
5708 *y = strong_pos.y / PANGO_SCALE;
5712 if (keymap_direction == cursor_direction)
5714 *x = strong_pos.x / PANGO_SCALE;
5715 *y = strong_pos.y / PANGO_SCALE;
5719 *x = weak_pos.x / PANGO_SCALE;
5720 *y = weak_pos.y / PANGO_SCALE;
5727 gtk_label_move_logically (GtkLabel *label,
5731 GtkLabelPriv *priv = label->priv;
5732 gint offset = g_utf8_pointer_to_offset (priv->text,
5733 priv->text + start);
5737 PangoLogAttr *log_attrs;
5741 gtk_label_ensure_layout (label, FALSE);
5743 length = g_utf8_strlen (priv->text, -1);
5745 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5747 while (count > 0 && offset < length)
5751 while (offset < length && !log_attrs[offset].is_cursor_position);
5755 while (count < 0 && offset > 0)
5759 while (offset > 0 && !log_attrs[offset].is_cursor_position);
5767 return g_utf8_offset_to_pointer (priv->text, offset) - priv->text;
5771 gtk_label_move_visually (GtkLabel *label,
5775 GtkLabelPriv *priv = label->priv;
5782 int new_index, new_trailing;
5783 gboolean split_cursor;
5786 gtk_label_ensure_layout (label, FALSE);
5788 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5789 "gtk-split-cursor", &split_cursor,
5796 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5797 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5799 strong = keymap_direction == get_cursor_direction (label);
5804 pango_layout_move_cursor_visually (priv->layout, strong, index, 0, 1, &new_index, &new_trailing);
5809 pango_layout_move_cursor_visually (priv->layout, strong, index, 0, -1, &new_index, &new_trailing);
5813 if (new_index < 0 || new_index == G_MAXINT)
5818 while (new_trailing--)
5819 index = g_utf8_next_char (priv->text + new_index) - priv->text;
5826 gtk_label_move_forward_word (GtkLabel *label,
5829 GtkLabelPriv *priv = label->priv;
5830 gint new_pos = g_utf8_pointer_to_offset (priv->text,
5831 priv->text + start);
5834 length = g_utf8_strlen (priv->text, -1);
5835 if (new_pos < length)
5837 PangoLogAttr *log_attrs;
5840 gtk_label_ensure_layout (label, FALSE);
5842 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5844 /* Find the next word end */
5846 while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
5852 return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
5857 gtk_label_move_backward_word (GtkLabel *label,
5860 GtkLabelPriv *priv = label->priv;
5861 gint new_pos = g_utf8_pointer_to_offset (priv->text,
5862 priv->text + start);
5866 PangoLogAttr *log_attrs;
5869 gtk_label_ensure_layout (label, FALSE);
5871 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5875 /* Find the previous word beginning */
5876 while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
5882 return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
5886 gtk_label_move_cursor (GtkLabel *label,
5887 GtkMovementStep step,
5889 gboolean extend_selection)
5891 GtkLabelPriv *priv = label->priv;
5895 if (priv->select_info == NULL)
5898 old_pos = new_pos = priv->select_info->selection_end;
5900 if (priv->select_info->selection_end != priv->select_info->selection_anchor &&
5903 /* If we have a current selection and aren't extending it, move to the
5904 * start/or end of the selection as appropriate
5908 case GTK_MOVEMENT_VISUAL_POSITIONS:
5911 gint anchor_x, anchor_y;
5912 gboolean end_is_left;
5914 get_better_cursor (label, priv->select_info->selection_end, &end_x, &end_y);
5915 get_better_cursor (label, priv->select_info->selection_anchor, &anchor_x, &anchor_y);
5917 end_is_left = (end_y < anchor_y) || (end_y == anchor_y && end_x < anchor_x);
5920 new_pos = end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
5922 new_pos = !end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
5925 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5926 case GTK_MOVEMENT_WORDS:
5928 new_pos = MIN (priv->select_info->selection_end, priv->select_info->selection_anchor);
5930 new_pos = MAX (priv->select_info->selection_end, priv->select_info->selection_anchor);
5932 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5933 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5934 case GTK_MOVEMENT_BUFFER_ENDS:
5935 /* FIXME: Can do better here */
5936 new_pos = count < 0 ? 0 : strlen (priv->text);
5938 case GTK_MOVEMENT_DISPLAY_LINES:
5939 case GTK_MOVEMENT_PARAGRAPHS:
5940 case GTK_MOVEMENT_PAGES:
5941 case GTK_MOVEMENT_HORIZONTAL_PAGES:
5949 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5950 new_pos = gtk_label_move_logically (label, new_pos, count);
5952 case GTK_MOVEMENT_VISUAL_POSITIONS:
5953 new_pos = gtk_label_move_visually (label, new_pos, count);
5954 if (new_pos == old_pos)
5956 if (!extend_selection)
5958 if (!gtk_widget_keynav_failed (GTK_WIDGET (label),
5960 GTK_DIR_RIGHT : GTK_DIR_LEFT))
5962 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
5965 gtk_widget_child_focus (toplevel,
5967 GTK_DIR_RIGHT : GTK_DIR_LEFT);
5972 gtk_widget_error_bell (GTK_WIDGET (label));
5976 case GTK_MOVEMENT_WORDS:
5979 new_pos = gtk_label_move_forward_word (label, new_pos);
5984 new_pos = gtk_label_move_backward_word (label, new_pos);
5987 if (new_pos == old_pos)
5988 gtk_widget_error_bell (GTK_WIDGET (label));
5990 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5991 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5992 case GTK_MOVEMENT_BUFFER_ENDS:
5993 /* FIXME: Can do better here */
5994 new_pos = count < 0 ? 0 : strlen (priv->text);
5995 if (new_pos == old_pos)
5996 gtk_widget_error_bell (GTK_WIDGET (label));
5998 case GTK_MOVEMENT_DISPLAY_LINES:
5999 case GTK_MOVEMENT_PARAGRAPHS:
6000 case GTK_MOVEMENT_PAGES:
6001 case GTK_MOVEMENT_HORIZONTAL_PAGES:
6006 if (extend_selection)
6007 gtk_label_select_region_index (label,
6008 priv->select_info->selection_anchor,
6011 gtk_label_select_region_index (label, new_pos, new_pos);
6015 gtk_label_copy_clipboard (GtkLabel *label)
6017 GtkLabelPriv *priv = label->priv;
6019 if (priv->text && priv->select_info)
6023 GtkClipboard *clipboard;
6025 start = MIN (priv->select_info->selection_anchor,
6026 priv->select_info->selection_end);
6027 end = MAX (priv->select_info->selection_anchor,
6028 priv->select_info->selection_end);
6030 len = strlen (priv->text);
6038 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6041 gtk_clipboard_set_text (clipboard, priv->text + start, end - start);
6046 link = gtk_label_get_focus_link (label);
6048 gtk_clipboard_set_text (clipboard, link->uri, -1);
6054 gtk_label_select_all (GtkLabel *label)
6056 GtkLabelPriv *priv = label->priv;
6058 gtk_label_select_region_index (label, 0, strlen (priv->text));
6061 /* Quick hack of a popup menu
6064 activate_cb (GtkWidget *menuitem,
6067 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6068 g_signal_emit_by_name (label, signal);
6072 append_action_signal (GtkLabel *label,
6074 const gchar *stock_id,
6075 const gchar *signal,
6078 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6080 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
6081 g_signal_connect (menuitem, "activate",
6082 G_CALLBACK (activate_cb), label);
6084 gtk_widget_set_sensitive (menuitem, sensitive);
6086 gtk_widget_show (menuitem);
6087 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6091 popup_menu_detach (GtkWidget *attach_widget,
6094 GtkLabel *label = GTK_LABEL (attach_widget);
6095 GtkLabelPriv *priv = label->priv;
6097 if (priv->select_info)
6098 priv->select_info->popup_menu = NULL;
6102 popup_position_func (GtkMenu *menu,
6113 label = GTK_LABEL (user_data);
6114 widget = GTK_WIDGET (label);
6116 g_return_if_fail (gtk_widget_get_realized (widget));
6118 screen = gtk_widget_get_screen (widget);
6119 gdk_window_get_origin (widget->window, x, y);
6121 *x += widget->allocation.x;
6122 *y += widget->allocation.y;
6124 gtk_widget_size_request (GTK_WIDGET (menu), &req);
6126 *x += widget->allocation.width / 2;
6127 *y += widget->allocation.height;
6129 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
6130 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
6134 open_link_activate_cb (GtkMenuItem *menu_item,
6139 link = gtk_label_get_current_link (label);
6142 emit_activate_link (label, link);
6146 copy_link_activate_cb (GtkMenuItem *menu_item,
6149 GtkClipboard *clipboard;
6152 uri = gtk_label_get_current_uri (label);
6155 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6156 gtk_clipboard_set_text (clipboard, uri, -1);
6161 gtk_label_popup_menu (GtkWidget *widget)
6163 gtk_label_do_popup (GTK_LABEL (widget), NULL);
6169 gtk_label_do_popup (GtkLabel *label,
6170 GdkEventButton *event)
6172 GtkLabelPriv *priv = label->priv;
6173 GtkWidget *menuitem;
6176 gboolean have_selection;
6179 if (!priv->select_info)
6182 if (priv->select_info->popup_menu)
6183 gtk_widget_destroy (priv->select_info->popup_menu);
6185 priv->select_info->popup_menu = menu = gtk_menu_new ();
6187 gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (label), popup_menu_detach);
6190 priv->select_info->selection_anchor != priv->select_info->selection_end;
6194 if (priv->select_info->link_clicked)
6195 link = priv->select_info->active_link;
6200 link = gtk_label_get_focus_link (label);
6202 if (!have_selection && link)
6205 menuitem = gtk_image_menu_item_new_with_mnemonic (_("_Open Link"));
6206 gtk_widget_show (menuitem);
6207 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6209 g_signal_connect (G_OBJECT (menuitem), "activate",
6210 G_CALLBACK (open_link_activate_cb), label);
6212 image = gtk_image_new_from_stock (GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_MENU);
6213 gtk_widget_show (image);
6214 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6216 /* Copy Link Address */
6217 menuitem = gtk_image_menu_item_new_with_mnemonic (_("Copy _Link Address"));
6218 gtk_widget_show (menuitem);
6219 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6221 g_signal_connect (G_OBJECT (menuitem), "activate",
6222 G_CALLBACK (copy_link_activate_cb), label);
6224 image = gtk_image_new_from_stock (GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
6225 gtk_widget_show (image);
6226 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6230 append_action_signal (label, menu, GTK_STOCK_CUT, "cut-clipboard", FALSE);
6231 append_action_signal (label, menu, GTK_STOCK_COPY, "copy-clipboard", have_selection);
6232 append_action_signal (label, menu, GTK_STOCK_PASTE, "paste-clipboard", FALSE);
6234 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
6235 gtk_widget_set_sensitive (menuitem, FALSE);
6236 gtk_widget_show (menuitem);
6237 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6239 menuitem = gtk_separator_menu_item_new ();
6240 gtk_widget_show (menuitem);
6241 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6243 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
6244 g_signal_connect_swapped (menuitem, "activate",
6245 G_CALLBACK (gtk_label_select_all), label);
6246 gtk_widget_show (menuitem);
6247 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6250 g_signal_emit (label, signals[POPULATE_POPUP], 0, menu);
6253 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6255 event->button, event->time);
6258 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6259 popup_position_func, label,
6260 0, gtk_get_current_event_time ());
6261 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
6266 gtk_label_clear_links (GtkLabel *label)
6268 GtkLabelPriv *priv = label->priv;
6270 if (!priv->select_info)
6273 g_list_foreach (priv->select_info->links, (GFunc)link_free, NULL);
6274 g_list_free (priv->select_info->links);
6275 priv->select_info->links = NULL;
6276 priv->select_info->active_link = NULL;
6280 gtk_label_rescan_links (GtkLabel *label)
6282 GtkLabelPriv *priv = label->priv;
6283 PangoLayout *layout = priv->layout;
6284 PangoAttrList *attlist;
6285 PangoAttrIterator *iter;
6288 if (!priv->select_info || !priv->select_info->links)
6291 attlist = pango_layout_get_attributes (layout);
6293 if (attlist == NULL)
6296 iter = pango_attr_list_get_iterator (attlist);
6298 links = priv->select_info->links;
6302 PangoAttribute *underline;
6303 PangoAttribute *color;
6305 underline = pango_attr_iterator_get (iter, PANGO_ATTR_UNDERLINE);
6306 color = pango_attr_iterator_get (iter, PANGO_ATTR_FOREGROUND);
6308 if (underline != NULL && color != NULL)
6311 PangoRectangle start_pos;
6312 PangoRectangle end_pos;
6315 pango_attr_iterator_range (iter, &start, &end);
6316 pango_layout_index_to_pos (layout, start, &start_pos);
6317 pango_layout_index_to_pos (layout, end, &end_pos);
6321 g_warning ("Ran out of links");
6325 links = links->next;
6326 link->start = start;
6329 } while (pango_attr_iterator_next (iter));
6331 pango_attr_iterator_destroy (iter);
6335 gtk_label_activate_link (GtkLabel *label,
6338 GtkWidget *widget = GTK_WIDGET (label);
6339 GError *error = NULL;
6341 if (!gtk_show_uri (gtk_widget_get_screen (widget),
6342 uri, gtk_get_current_event_time (), &error))
6344 g_warning ("Unable to show '%s': %s", uri, error->message);
6345 g_error_free (error);
6352 emit_activate_link (GtkLabel *label,
6355 GtkLabelPriv *priv = label->priv;
6358 g_signal_emit (label, signals[ACTIVATE_LINK], 0, link->uri, &handled);
6359 if (handled && priv->track_links && !link->visited)
6361 link->visited = TRUE;
6362 /* FIXME: shouldn't have to redo everything here */
6363 gtk_label_recalculate (label);
6368 gtk_label_activate_current_link (GtkLabel *label)
6371 GtkWidget *widget = GTK_WIDGET (label);
6373 link = gtk_label_get_focus_link (label);
6377 emit_activate_link (label, link);
6381 GtkWidget *toplevel;
6384 toplevel = gtk_widget_get_toplevel (widget);
6385 if (GTK_IS_WINDOW (toplevel))
6387 window = GTK_WINDOW (toplevel);
6390 window->default_widget != widget &&
6391 !(widget == window->focus_widget &&
6392 (!window->default_widget || !gtk_widget_is_sensitive (window->default_widget))))
6393 gtk_window_activate_default (window);
6398 static GtkLabelLink *
6399 gtk_label_get_current_link (GtkLabel *label)
6401 GtkLabelPriv *priv = label->priv;
6404 if (!priv->select_info)
6407 if (priv->select_info->link_clicked)
6408 link = priv->select_info->active_link;
6410 link = gtk_label_get_focus_link (label);
6416 * gtk_label_get_current_uri:
6417 * @label: a #GtkLabel
6419 * Returns the URI for the currently active link in the label.
6420 * The active link is the one under the mouse pointer or, in a
6421 * selectable label, the link in which the text cursor is currently
6424 * This function is intended for use in a #GtkLabel::activate-link handler
6425 * or for use in a #GtkWidget::query-tooltip handler.
6427 * Returns: the currently active URI. The string is owned by GTK+ and must
6428 * not be freed or modified.
6432 G_CONST_RETURN gchar *
6433 gtk_label_get_current_uri (GtkLabel *label)
6437 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
6439 link = gtk_label_get_current_link (label);
6448 * gtk_label_set_track_visited_links:
6449 * @label: a #GtkLabel
6450 * @track_links: %TRUE to track visited links
6452 * Sets whether the label should keep track of clicked
6453 * links (and use a different color for them).
6458 gtk_label_set_track_visited_links (GtkLabel *label,
6459 gboolean track_links)
6463 g_return_if_fail (GTK_IS_LABEL (label));
6467 track_links = track_links != FALSE;
6469 if (priv->track_links != track_links)
6471 priv->track_links = track_links;
6473 /* FIXME: shouldn't have to redo everything here */
6474 gtk_label_recalculate (label);
6476 g_object_notify (G_OBJECT (label), "track-visited-links");
6481 * gtk_label_get_track_visited_links:
6482 * @label: a #GtkLabel
6484 * Returns whether the label is currently keeping track
6487 * Returns: %TRUE if clicked links are remembered
6492 gtk_label_get_track_visited_links (GtkLabel *label)
6494 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
6496 return label->priv->track_links;
6500 gtk_label_query_tooltip (GtkWidget *widget,
6503 gboolean keyboard_tip,
6504 GtkTooltip *tooltip)
6506 GtkLabel *label = GTK_LABEL (widget);
6507 GtkLabelPriv *priv = label->priv;
6508 GtkLabelSelectionInfo *info = priv->select_info;
6512 if (info && info->links)
6516 if (info->selection_anchor == info->selection_end)
6517 index = info->selection_anchor;
6521 if (!get_layout_index (label, x, y, &index))
6527 for (l = info->links; l != NULL; l = l->next)
6529 GtkLabelLink *link = l->data;
6530 if (index >= link->start && index <= link->end)
6534 gtk_tooltip_set_markup (tooltip, link->title);
6543 return GTK_WIDGET_CLASS (gtk_label_parent_class)->query_tooltip (widget,