1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
21 * file for a list of people on the GTK+ Team. See the ChangeLog
22 * files for a list of changes. These files are distributed with
23 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
32 #include "gtkaccellabel.h"
34 #include "gtkmainprivate.h"
35 #include "gtkmarshalers.h"
37 #include "gtkwindow.h"
38 #include "gtkclipboard.h"
39 #include "gtkimagemenuitem.h"
41 #include "gtkseparatormenuitem.h"
42 #include "gtktextutil.h"
43 #include "gtkmenuitem.h"
44 #include "gtkmenushellprivate.h"
45 #include "gtknotebook.h"
47 #include "gtkbindings.h"
48 #include "gtkbuildable.h"
51 #include "gtktooltip.h"
52 #include "gtkprivate.h"
53 #include "gtktypebuiltins.h"
55 /*rint() is only available in GCC and/or C99*/
56 #if (__STDC_VERSION__ < 199901L && !defined __GNUC__)
59 if (ceil(x+0.5) == floor(x+0.5))
74 struct _GtkLabelPrivate
76 GtkLabelSelectionInfo *select_info;
77 GtkWidget *mnemonic_widget;
78 GtkWindow *mnemonic_window;
81 PangoAttrList *effective_attrs;
89 guint mnemonics_visible : 1;
92 guint use_underline : 1;
95 guint single_line_mode : 1;
96 guint have_transform : 1;
99 guint pattern_set : 1;
100 guint track_links : 1;
102 guint mnemonic_keyval;
106 gint max_width_chars;
109 /* Notes about the handling of links:
111 * Links share the GtkLabelSelectionInfo struct with selectable labels.
112 * There are some new fields for links. The links field contains the list
113 * of GtkLabelLink structs that describe the links which are embedded in
114 * the label. The active_link field points to the link under the mouse
115 * pointer. For keyboard navigation, the 'focus' link is determined by
116 * finding the link which contains the selection_anchor position.
117 * The link_clicked field is used with button press and release events
118 * to ensure that pressing inside a link and releasing outside of it
119 * does not activate the link.
121 * Links are rendered with the link-color/visited-link-color colors
122 * that are determined by the style and with an underline. When the mouse
123 * pointer is over a link, the pointer is changed to indicate the link,
124 * and the background behind the link is rendered with the base[PRELIGHT]
125 * color. While a button is pressed over a link, the background is rendered
126 * with the base[ACTIVE] color.
128 * Labels with links accept keyboard focus, and it is possible to move
129 * the focus between the embedded links using Tab/Shift-Tab. The focus
130 * is indicated by a focus rectangle that is drawn around the link text.
131 * Pressing Enter activates the focussed link, and there is a suitable
132 * context menu for links that can be opened with the Menu key. Pressing
133 * Control-C copies the link URI to the clipboard.
135 * In selectable labels with links, link functionality is only available
136 * when the selection is empty.
141 gchar *title; /* the title attribute, used as tooltip */
142 gboolean visited; /* get set when the link is activated; this flag
143 * gets preserved over later set_markup() calls
145 gint start; /* position of the link in the PangoLayout */
149 struct _GtkLabelSelectionInfo
152 gint selection_anchor;
154 GtkWidget *popup_menu;
157 GtkLabelLink *active_link;
163 guint select_words : 1;
164 guint selectable : 1;
165 guint link_clicked : 1;
173 ACTIVATE_CURRENT_LINK,
188 PROP_MNEMONIC_KEYVAL,
189 PROP_MNEMONIC_WIDGET,
190 PROP_CURSOR_POSITION,
191 PROP_SELECTION_BOUND,
194 PROP_SINGLE_LINE_MODE,
196 PROP_MAX_WIDTH_CHARS,
197 PROP_TRACK_VISITED_LINKS
200 /* When rotating ellipsizable text we want the natural size to request
201 * more to ensure the label wont ever ellipsize in an allocation of full natural size.
203 #define ROTATION_ELLIPSIZE_PADDING 2
205 static guint signals[LAST_SIGNAL] = { 0 };
207 static const GdkColor default_link_color = { 0, 0, 0, 0xeeee };
208 static const GdkColor default_visited_link_color = { 0, 0x5555, 0x1a1a, 0x8b8b };
210 static void gtk_label_set_property (GObject *object,
214 static void gtk_label_get_property (GObject *object,
218 static void gtk_label_finalize (GObject *object);
219 static void gtk_label_destroy (GtkWidget *widget);
220 static void gtk_label_size_allocate (GtkWidget *widget,
221 GtkAllocation *allocation);
222 static void gtk_label_state_changed (GtkWidget *widget,
224 static void gtk_label_style_updated (GtkWidget *widget);
225 static void gtk_label_direction_changed (GtkWidget *widget,
226 GtkTextDirection previous_dir);
227 static gint gtk_label_draw (GtkWidget *widget,
229 static gboolean gtk_label_focus (GtkWidget *widget,
230 GtkDirectionType direction);
232 static void gtk_label_realize (GtkWidget *widget);
233 static void gtk_label_unrealize (GtkWidget *widget);
234 static void gtk_label_map (GtkWidget *widget);
235 static void gtk_label_unmap (GtkWidget *widget);
237 static gboolean gtk_label_button_press (GtkWidget *widget,
238 GdkEventButton *event);
239 static gboolean gtk_label_button_release (GtkWidget *widget,
240 GdkEventButton *event);
241 static gboolean gtk_label_motion (GtkWidget *widget,
242 GdkEventMotion *event);
243 static gboolean gtk_label_leave_notify (GtkWidget *widget,
244 GdkEventCrossing *event);
246 static void gtk_label_grab_focus (GtkWidget *widget);
248 static gboolean gtk_label_query_tooltip (GtkWidget *widget,
251 gboolean keyboard_tip,
252 GtkTooltip *tooltip);
254 static void gtk_label_set_text_internal (GtkLabel *label,
256 static void gtk_label_set_label_internal (GtkLabel *label,
258 static void gtk_label_set_use_markup_internal (GtkLabel *label,
260 static void gtk_label_set_use_underline_internal (GtkLabel *label,
262 static void gtk_label_set_attributes_internal (GtkLabel *label,
263 PangoAttrList *attrs);
264 static void gtk_label_set_uline_text_internal (GtkLabel *label,
266 static void gtk_label_set_pattern_internal (GtkLabel *label,
267 const gchar *pattern,
268 gboolean is_mnemonic);
269 static void gtk_label_set_markup_internal (GtkLabel *label,
271 gboolean with_uline);
272 static void gtk_label_recalculate (GtkLabel *label);
273 static void gtk_label_hierarchy_changed (GtkWidget *widget,
274 GtkWidget *old_toplevel);
275 static void gtk_label_screen_changed (GtkWidget *widget,
276 GdkScreen *old_screen);
277 static gboolean gtk_label_popup_menu (GtkWidget *widget);
279 static void gtk_label_create_window (GtkLabel *label);
280 static void gtk_label_destroy_window (GtkLabel *label);
281 static void gtk_label_ensure_select_info (GtkLabel *label);
282 static void gtk_label_clear_select_info (GtkLabel *label);
283 static void gtk_label_update_cursor (GtkLabel *label);
284 static void gtk_label_clear_layout (GtkLabel *label);
285 static void gtk_label_ensure_layout (GtkLabel *label);
286 static void gtk_label_invalidate_wrap_width (GtkLabel *label);
287 static void gtk_label_select_region_index (GtkLabel *label,
291 static gboolean gtk_label_mnemonic_activate (GtkWidget *widget,
292 gboolean group_cycling);
293 static void gtk_label_setup_mnemonic (GtkLabel *label,
295 static void gtk_label_drag_data_get (GtkWidget *widget,
296 GdkDragContext *context,
297 GtkSelectionData *selection_data,
301 static void gtk_label_buildable_interface_init (GtkBuildableIface *iface);
302 static gboolean gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
305 const gchar *tagname,
306 GMarkupParser *parser,
309 static void gtk_label_buildable_custom_finished (GtkBuildable *buildable,
312 const gchar *tagname,
316 static void connect_mnemonics_visible_notify (GtkLabel *label);
317 static gboolean separate_uline_pattern (const gchar *str,
323 /* For selectable labels: */
324 static void gtk_label_move_cursor (GtkLabel *label,
325 GtkMovementStep step,
327 gboolean extend_selection);
328 static void gtk_label_copy_clipboard (GtkLabel *label);
329 static void gtk_label_select_all (GtkLabel *label);
330 static void gtk_label_do_popup (GtkLabel *label,
331 GdkEventButton *event);
332 static gint gtk_label_move_forward_word (GtkLabel *label,
334 static gint gtk_label_move_backward_word (GtkLabel *label,
338 static void gtk_label_rescan_links (GtkLabel *label);
339 static void gtk_label_clear_links (GtkLabel *label);
340 static gboolean gtk_label_activate_link (GtkLabel *label,
342 static void gtk_label_activate_current_link (GtkLabel *label);
343 static GtkLabelLink *gtk_label_get_current_link (GtkLabel *label);
344 static void gtk_label_get_link_colors (GtkWidget *widget,
345 GdkColor **link_color,
346 GdkColor **visited_link_color);
347 static void emit_activate_link (GtkLabel *label,
350 static GtkSizeRequestMode gtk_label_get_request_mode (GtkWidget *widget);
351 static void gtk_label_get_preferred_width (GtkWidget *widget,
354 static void gtk_label_get_preferred_height (GtkWidget *widget,
357 static void gtk_label_get_preferred_width_for_height (GtkWidget *widget,
360 gint *natural_width);
361 static void gtk_label_get_preferred_height_for_width (GtkWidget *widget,
363 gint *minimum_height,
364 gint *natural_height);
366 static GtkBuildableIface *buildable_parent_iface = NULL;
368 G_DEFINE_TYPE_WITH_CODE (GtkLabel, gtk_label, GTK_TYPE_MISC,
369 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
370 gtk_label_buildable_interface_init))
373 add_move_binding (GtkBindingSet *binding_set,
376 GtkMovementStep step,
379 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
381 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
385 G_TYPE_BOOLEAN, FALSE);
387 /* Selection-extending version */
388 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
392 G_TYPE_BOOLEAN, TRUE);
396 gtk_label_class_init (GtkLabelClass *class)
398 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
399 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
400 GtkBindingSet *binding_set;
402 gobject_class->set_property = gtk_label_set_property;
403 gobject_class->get_property = gtk_label_get_property;
404 gobject_class->finalize = gtk_label_finalize;
406 widget_class->destroy = gtk_label_destroy;
407 widget_class->size_allocate = gtk_label_size_allocate;
408 widget_class->state_changed = gtk_label_state_changed;
409 widget_class->style_updated = gtk_label_style_updated;
410 widget_class->query_tooltip = gtk_label_query_tooltip;
411 widget_class->direction_changed = gtk_label_direction_changed;
412 widget_class->draw = gtk_label_draw;
413 widget_class->realize = gtk_label_realize;
414 widget_class->unrealize = gtk_label_unrealize;
415 widget_class->map = gtk_label_map;
416 widget_class->unmap = gtk_label_unmap;
417 widget_class->button_press_event = gtk_label_button_press;
418 widget_class->button_release_event = gtk_label_button_release;
419 widget_class->motion_notify_event = gtk_label_motion;
420 widget_class->leave_notify_event = gtk_label_leave_notify;
421 widget_class->hierarchy_changed = gtk_label_hierarchy_changed;
422 widget_class->screen_changed = gtk_label_screen_changed;
423 widget_class->mnemonic_activate = gtk_label_mnemonic_activate;
424 widget_class->drag_data_get = gtk_label_drag_data_get;
425 widget_class->grab_focus = gtk_label_grab_focus;
426 widget_class->popup_menu = gtk_label_popup_menu;
427 widget_class->focus = gtk_label_focus;
428 widget_class->get_request_mode = gtk_label_get_request_mode;
429 widget_class->get_preferred_width = gtk_label_get_preferred_width;
430 widget_class->get_preferred_height = gtk_label_get_preferred_height;
431 widget_class->get_preferred_width_for_height = gtk_label_get_preferred_width_for_height;
432 widget_class->get_preferred_height_for_width = gtk_label_get_preferred_height_for_width;
434 class->move_cursor = gtk_label_move_cursor;
435 class->copy_clipboard = gtk_label_copy_clipboard;
436 class->activate_link = gtk_label_activate_link;
439 * GtkLabel::move-cursor:
440 * @entry: the object which received the signal
441 * @step: the granularity of the move, as a #GtkMovementStep
442 * @count: the number of @step units to move
443 * @extend_selection: %TRUE if the move should extend the selection
445 * The ::move-cursor signal is a
446 * <link linkend="keybinding-signals">keybinding signal</link>
447 * which gets emitted when the user initiates a cursor movement.
448 * If the cursor is not visible in @entry, this signal causes
449 * the viewport to be moved instead.
451 * Applications should not connect to it, but may emit it with
452 * g_signal_emit_by_name() if they need to control the cursor
455 * The default bindings for this signal come in two variants,
456 * the variant with the Shift modifier extends the selection,
457 * the variant without the Shift modifer does not.
458 * There are too many key combinations to list them all here.
460 * <listitem>Arrow keys move by individual characters/lines</listitem>
461 * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
462 * <listitem>Home/End keys move to the ends of the buffer</listitem>
465 signals[MOVE_CURSOR] =
466 g_signal_new (I_("move-cursor"),
467 G_OBJECT_CLASS_TYPE (gobject_class),
468 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
469 G_STRUCT_OFFSET (GtkLabelClass, move_cursor),
471 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
473 GTK_TYPE_MOVEMENT_STEP,
478 * GtkLabel::copy-clipboard:
479 * @label: the object which received the signal
481 * The ::copy-clipboard signal is a
482 * <link linkend="keybinding-signals">keybinding signal</link>
483 * which gets emitted to copy the selection to the clipboard.
485 * The default binding for this signal is Ctrl-c.
487 signals[COPY_CLIPBOARD] =
488 g_signal_new (I_("copy-clipboard"),
489 G_OBJECT_CLASS_TYPE (gobject_class),
490 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
491 G_STRUCT_OFFSET (GtkLabelClass, copy_clipboard),
493 _gtk_marshal_VOID__VOID,
497 * GtkLabel::populate-popup:
498 * @label: The label on which the signal is emitted
499 * @menu: the menu that is being populated
501 * The ::populate-popup signal gets emitted before showing the
502 * context menu of the label. Note that only selectable labels
503 * have context menus.
505 * If you need to add items to the context menu, connect
506 * to this signal and append your menuitems to the @menu.
508 signals[POPULATE_POPUP] =
509 g_signal_new (I_("populate-popup"),
510 G_OBJECT_CLASS_TYPE (gobject_class),
512 G_STRUCT_OFFSET (GtkLabelClass, populate_popup),
514 _gtk_marshal_VOID__OBJECT,
519 * GtkLabel::activate-current-link:
520 * @label: The label on which the signal was emitted
522 * A <link linkend="keybinding-signals">keybinding signal</link>
523 * which gets emitted when the user activates a link in the label.
525 * Applications may also emit the signal with g_signal_emit_by_name()
526 * if they need to control activation of URIs programmatically.
528 * The default bindings for this signal are all forms of the Enter key.
532 signals[ACTIVATE_CURRENT_LINK] =
533 g_signal_new_class_handler ("activate-current-link",
534 G_TYPE_FROM_CLASS (gobject_class),
535 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
536 G_CALLBACK (gtk_label_activate_current_link),
538 _gtk_marshal_VOID__VOID,
542 * GtkLabel::activate-link:
543 * @label: The label on which the signal was emitted
544 * @uri: the URI that is activated
546 * The signal which gets emitted to activate a URI.
547 * Applications may connect to it to override the default behaviour,
548 * which is to call gtk_show_uri().
550 * Returns: %TRUE if the link has been activated
554 signals[ACTIVATE_LINK] =
555 g_signal_new ("activate-link",
556 G_TYPE_FROM_CLASS (gobject_class),
558 G_STRUCT_OFFSET (GtkLabelClass, activate_link),
559 _gtk_boolean_handled_accumulator, NULL,
560 _gtk_marshal_BOOLEAN__STRING,
561 G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
563 g_object_class_install_property (gobject_class,
565 g_param_spec_string ("label",
567 P_("The text of the label"),
569 GTK_PARAM_READWRITE));
570 g_object_class_install_property (gobject_class,
572 g_param_spec_boxed ("attributes",
574 P_("A list of style attributes to apply to the text of the label"),
575 PANGO_TYPE_ATTR_LIST,
576 GTK_PARAM_READWRITE));
577 g_object_class_install_property (gobject_class,
579 g_param_spec_boolean ("use-markup",
581 P_("The text of the label includes XML markup. See pango_parse_markup()"),
583 GTK_PARAM_READWRITE));
584 g_object_class_install_property (gobject_class,
586 g_param_spec_boolean ("use-underline",
588 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
590 GTK_PARAM_READWRITE));
592 g_object_class_install_property (gobject_class,
594 g_param_spec_enum ("justify",
596 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"),
597 GTK_TYPE_JUSTIFICATION,
599 GTK_PARAM_READWRITE));
601 g_object_class_install_property (gobject_class,
603 g_param_spec_string ("pattern",
605 P_("A string with _ characters in positions correspond to characters in the text to underline"),
607 GTK_PARAM_WRITABLE));
609 g_object_class_install_property (gobject_class,
611 g_param_spec_boolean ("wrap",
613 P_("If set, wrap lines if the text becomes too wide"),
615 GTK_PARAM_READWRITE));
617 * GtkLabel:wrap-mode:
619 * If line wrapping is on (see the #GtkLabel:wrap property) this controls
620 * how the line wrapping is done. The default is %PANGO_WRAP_WORD, which
621 * means wrap on word boundaries.
625 g_object_class_install_property (gobject_class,
627 g_param_spec_enum ("wrap-mode",
628 P_("Line wrap mode"),
629 P_("If wrap is set, controls how linewrapping is done"),
630 PANGO_TYPE_WRAP_MODE,
632 GTK_PARAM_READWRITE));
633 g_object_class_install_property (gobject_class,
635 g_param_spec_boolean ("selectable",
637 P_("Whether the label text can be selected with the mouse"),
639 GTK_PARAM_READWRITE));
640 g_object_class_install_property (gobject_class,
641 PROP_MNEMONIC_KEYVAL,
642 g_param_spec_uint ("mnemonic-keyval",
644 P_("The mnemonic accelerator key for this label"),
648 GTK_PARAM_READABLE));
649 g_object_class_install_property (gobject_class,
650 PROP_MNEMONIC_WIDGET,
651 g_param_spec_object ("mnemonic-widget",
652 P_("Mnemonic widget"),
653 P_("The widget to be activated when the label's mnemonic "
656 GTK_PARAM_READWRITE));
658 g_object_class_install_property (gobject_class,
659 PROP_CURSOR_POSITION,
660 g_param_spec_int ("cursor-position",
661 P_("Cursor Position"),
662 P_("The current position of the insertion cursor in chars"),
666 GTK_PARAM_READABLE));
668 g_object_class_install_property (gobject_class,
669 PROP_SELECTION_BOUND,
670 g_param_spec_int ("selection-bound",
671 P_("Selection Bound"),
672 P_("The position of the opposite end of the selection from the cursor in chars"),
676 GTK_PARAM_READABLE));
679 * GtkLabel:ellipsize:
681 * The preferred place to ellipsize the string, if the label does
682 * not have enough room to display the entire string, specified as a
683 * #PangoEllisizeMode.
685 * Note that setting this property to a value other than
686 * %PANGO_ELLIPSIZE_NONE has the side-effect that the label requests
687 * only enough space to display the ellipsis "...". In particular, this
688 * means that ellipsizing labels do not work well in notebook tabs, unless
689 * the tab's #GtkNotebook:tab-expand property is set to %TRUE. Other ways
690 * to set a label's width are gtk_widget_set_size_request() and
691 * gtk_label_set_width_chars().
695 g_object_class_install_property (gobject_class,
697 g_param_spec_enum ("ellipsize",
699 P_("The preferred place to ellipsize the string, if the label does not have enough room to display the entire string"),
700 PANGO_TYPE_ELLIPSIZE_MODE,
701 PANGO_ELLIPSIZE_NONE,
702 GTK_PARAM_READWRITE));
705 * GtkLabel:width-chars:
707 * The desired width of the label, in characters. If this property is set to
708 * -1, the width will be calculated automatically.
710 * See the section on <link linkend="label-text-layout">text layout</link>
711 * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
712 * determine the width of ellipsized and wrapped labels.
716 g_object_class_install_property (gobject_class,
718 g_param_spec_int ("width-chars",
719 P_("Width In Characters"),
720 P_("The desired width of the label, in characters"),
724 GTK_PARAM_READWRITE));
727 * GtkLabel:single-line-mode:
729 * Whether the label is in single line mode. In single line mode,
730 * the height of the label does not depend on the actual text, it
731 * is always set to ascent + descent of the font. This can be an
732 * advantage in situations where resizing the label because of text
733 * changes would be distracting, e.g. in a statusbar.
737 g_object_class_install_property (gobject_class,
738 PROP_SINGLE_LINE_MODE,
739 g_param_spec_boolean ("single-line-mode",
740 P_("Single Line Mode"),
741 P_("Whether the label is in single line mode"),
743 GTK_PARAM_READWRITE));
748 * The angle that the baseline of the label makes with the horizontal,
749 * in degrees, measured counterclockwise. An angle of 90 reads from
750 * from bottom to top, an angle of 270, from top to bottom. Ignored
751 * if the label is selectable, wrapped, or ellipsized.
755 g_object_class_install_property (gobject_class,
757 g_param_spec_double ("angle",
759 P_("Angle at which the label is rotated"),
763 GTK_PARAM_READWRITE));
766 * GtkLabel:max-width-chars:
768 * The desired maximum width of the label, in characters. If this property
769 * is set to -1, the width will be calculated automatically.
771 * See the section on <link linkend="label-text-layout">text layout</link>
772 * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
773 * determine the width of ellipsized and wrapped labels.
777 g_object_class_install_property (gobject_class,
778 PROP_MAX_WIDTH_CHARS,
779 g_param_spec_int ("max-width-chars",
780 P_("Maximum Width In Characters"),
781 P_("The desired maximum width of the label, in characters"),
785 GTK_PARAM_READWRITE));
788 * GtkLabel:track-visited-links:
790 * Set this property to %TRUE to make the label track which links
791 * have been clicked. It will then apply the ::visited-link-color
792 * color, instead of ::link-color.
796 g_object_class_install_property (gobject_class,
797 PROP_TRACK_VISITED_LINKS,
798 g_param_spec_boolean ("track-visited-links",
799 P_("Track visited links"),
800 P_("Whether visited links should be tracked"),
802 GTK_PARAM_READWRITE));
807 binding_set = gtk_binding_set_by_class (class);
809 /* Moving the insertion point */
810 add_move_binding (binding_set, GDK_KEY_Right, 0,
811 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
813 add_move_binding (binding_set, GDK_KEY_Left, 0,
814 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
816 add_move_binding (binding_set, GDK_KEY_KP_Right, 0,
817 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
819 add_move_binding (binding_set, GDK_KEY_KP_Left, 0,
820 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
822 add_move_binding (binding_set, GDK_KEY_f, GDK_CONTROL_MASK,
823 GTK_MOVEMENT_LOGICAL_POSITIONS, 1);
825 add_move_binding (binding_set, GDK_KEY_b, GDK_CONTROL_MASK,
826 GTK_MOVEMENT_LOGICAL_POSITIONS, -1);
828 add_move_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK,
829 GTK_MOVEMENT_WORDS, 1);
831 add_move_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK,
832 GTK_MOVEMENT_WORDS, -1);
834 add_move_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK,
835 GTK_MOVEMENT_WORDS, 1);
837 add_move_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK,
838 GTK_MOVEMENT_WORDS, -1);
841 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
843 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
845 G_TYPE_BOOLEAN, FALSE);
847 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
849 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
851 G_TYPE_BOOLEAN, TRUE);
853 gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
855 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
857 G_TYPE_BOOLEAN, FALSE);
859 gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
861 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
863 G_TYPE_BOOLEAN, TRUE);
866 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
868 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
870 G_TYPE_BOOLEAN, FALSE);
872 gtk_binding_entry_add_signal (binding_set, GDK_KEY_backslash, GDK_CONTROL_MASK,
874 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
876 G_TYPE_BOOLEAN, FALSE);
878 add_move_binding (binding_set, GDK_KEY_f, GDK_MOD1_MASK,
879 GTK_MOVEMENT_WORDS, 1);
881 add_move_binding (binding_set, GDK_KEY_b, GDK_MOD1_MASK,
882 GTK_MOVEMENT_WORDS, -1);
884 add_move_binding (binding_set, GDK_KEY_Home, 0,
885 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
887 add_move_binding (binding_set, GDK_KEY_End, 0,
888 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
890 add_move_binding (binding_set, GDK_KEY_KP_Home, 0,
891 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
893 add_move_binding (binding_set, GDK_KEY_KP_End, 0,
894 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
896 add_move_binding (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK,
897 GTK_MOVEMENT_BUFFER_ENDS, -1);
899 add_move_binding (binding_set, GDK_KEY_End, GDK_CONTROL_MASK,
900 GTK_MOVEMENT_BUFFER_ENDS, 1);
902 add_move_binding (binding_set, GDK_KEY_KP_Home, GDK_CONTROL_MASK,
903 GTK_MOVEMENT_BUFFER_ENDS, -1);
905 add_move_binding (binding_set, GDK_KEY_KP_End, GDK_CONTROL_MASK,
906 GTK_MOVEMENT_BUFFER_ENDS, 1);
909 gtk_binding_entry_add_signal (binding_set, GDK_KEY_c, GDK_CONTROL_MASK,
910 "copy-clipboard", 0);
912 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Return, 0,
913 "activate-current-link", 0);
914 gtk_binding_entry_add_signal (binding_set, GDK_KEY_ISO_Enter, 0,
915 "activate-current-link", 0);
916 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Enter, 0,
917 "activate-current-link", 0);
919 g_type_class_add_private (class, sizeof (GtkLabelPrivate));
923 gtk_label_set_property (GObject *object,
928 GtkLabel *label = GTK_LABEL (object);
933 gtk_label_set_label (label, g_value_get_string (value));
935 case PROP_ATTRIBUTES:
936 gtk_label_set_attributes (label, g_value_get_boxed (value));
938 case PROP_USE_MARKUP:
939 gtk_label_set_use_markup (label, g_value_get_boolean (value));
941 case PROP_USE_UNDERLINE:
942 gtk_label_set_use_underline (label, g_value_get_boolean (value));
945 gtk_label_set_justify (label, g_value_get_enum (value));
948 gtk_label_set_pattern (label, g_value_get_string (value));
951 gtk_label_set_line_wrap (label, g_value_get_boolean (value));
954 gtk_label_set_line_wrap_mode (label, g_value_get_enum (value));
956 case PROP_SELECTABLE:
957 gtk_label_set_selectable (label, g_value_get_boolean (value));
959 case PROP_MNEMONIC_WIDGET:
960 gtk_label_set_mnemonic_widget (label, (GtkWidget*) g_value_get_object (value));
963 gtk_label_set_ellipsize (label, g_value_get_enum (value));
965 case PROP_WIDTH_CHARS:
966 gtk_label_set_width_chars (label, g_value_get_int (value));
968 case PROP_SINGLE_LINE_MODE:
969 gtk_label_set_single_line_mode (label, g_value_get_boolean (value));
972 gtk_label_set_angle (label, g_value_get_double (value));
974 case PROP_MAX_WIDTH_CHARS:
975 gtk_label_set_max_width_chars (label, g_value_get_int (value));
977 case PROP_TRACK_VISITED_LINKS:
978 gtk_label_set_track_visited_links (label, g_value_get_boolean (value));
981 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
987 gtk_label_get_property (GObject *object,
992 GtkLabel *label = GTK_LABEL (object);
993 GtkLabelPrivate *priv = label->priv;
998 g_value_set_string (value, priv->label);
1000 case PROP_ATTRIBUTES:
1001 g_value_set_boxed (value, priv->attrs);
1003 case PROP_USE_MARKUP:
1004 g_value_set_boolean (value, priv->use_markup);
1006 case PROP_USE_UNDERLINE:
1007 g_value_set_boolean (value, priv->use_underline);
1010 g_value_set_enum (value, priv->jtype);
1013 g_value_set_boolean (value, priv->wrap);
1015 case PROP_WRAP_MODE:
1016 g_value_set_enum (value, priv->wrap_mode);
1018 case PROP_SELECTABLE:
1019 g_value_set_boolean (value, gtk_label_get_selectable (label));
1021 case PROP_MNEMONIC_KEYVAL:
1022 g_value_set_uint (value, priv->mnemonic_keyval);
1024 case PROP_MNEMONIC_WIDGET:
1025 g_value_set_object (value, (GObject*) priv->mnemonic_widget);
1027 case PROP_CURSOR_POSITION:
1028 if (priv->select_info && priv->select_info->selectable)
1030 gint offset = g_utf8_pointer_to_offset (priv->text,
1031 priv->text + priv->select_info->selection_end);
1032 g_value_set_int (value, offset);
1035 g_value_set_int (value, 0);
1037 case PROP_SELECTION_BOUND:
1038 if (priv->select_info && priv->select_info->selectable)
1040 gint offset = g_utf8_pointer_to_offset (priv->text,
1041 priv->text + priv->select_info->selection_anchor);
1042 g_value_set_int (value, offset);
1045 g_value_set_int (value, 0);
1047 case PROP_ELLIPSIZE:
1048 g_value_set_enum (value, priv->ellipsize);
1050 case PROP_WIDTH_CHARS:
1051 g_value_set_int (value, gtk_label_get_width_chars (label));
1053 case PROP_SINGLE_LINE_MODE:
1054 g_value_set_boolean (value, gtk_label_get_single_line_mode (label));
1057 g_value_set_double (value, gtk_label_get_angle (label));
1059 case PROP_MAX_WIDTH_CHARS:
1060 g_value_set_int (value, gtk_label_get_max_width_chars (label));
1062 case PROP_TRACK_VISITED_LINKS:
1063 g_value_set_boolean (value, gtk_label_get_track_visited_links (label));
1066 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1072 gtk_label_init (GtkLabel *label)
1074 GtkLabelPrivate *priv;
1076 label->priv = G_TYPE_INSTANCE_GET_PRIVATE (label,
1081 gtk_widget_set_has_window (GTK_WIDGET (label), FALSE);
1083 priv->width_chars = -1;
1084 priv->max_width_chars = -1;
1085 priv->wrap_width = -1;
1088 priv->jtype = GTK_JUSTIFY_LEFT;
1090 priv->wrap_mode = PANGO_WRAP_WORD;
1091 priv->ellipsize = PANGO_ELLIPSIZE_NONE;
1093 priv->use_underline = FALSE;
1094 priv->use_markup = FALSE;
1095 priv->pattern_set = FALSE;
1096 priv->track_links = TRUE;
1098 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
1099 priv->layout = NULL;
1103 priv->mnemonic_widget = NULL;
1104 priv->mnemonic_window = NULL;
1106 priv->mnemonics_visible = TRUE;
1108 gtk_label_set_text (label, "");
1113 gtk_label_buildable_interface_init (GtkBuildableIface *iface)
1115 buildable_parent_iface = g_type_interface_peek_parent (iface);
1117 iface->custom_tag_start = gtk_label_buildable_custom_tag_start;
1118 iface->custom_finished = gtk_label_buildable_custom_finished;
1122 GtkBuilder *builder;
1124 PangoAttrList *attrs;
1127 static PangoAttribute *
1128 attribute_from_text (GtkBuilder *builder,
1133 PangoAttribute *attribute = NULL;
1135 PangoLanguage *language;
1136 PangoFontDescription *font_desc;
1138 GValue val = { 0, };
1140 if (!gtk_builder_value_from_string_type (builder, PANGO_TYPE_ATTR_TYPE, name, &val, error))
1143 type = g_value_get_enum (&val);
1144 g_value_unset (&val);
1148 /* PangoAttrLanguage */
1149 case PANGO_ATTR_LANGUAGE:
1150 if ((language = pango_language_from_string (value)))
1152 attribute = pango_attr_language_new (language);
1153 g_value_init (&val, G_TYPE_INT);
1157 case PANGO_ATTR_STYLE:
1158 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STYLE, value, &val, error))
1159 attribute = pango_attr_style_new (g_value_get_enum (&val));
1161 case PANGO_ATTR_WEIGHT:
1162 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_WEIGHT, value, &val, error))
1163 attribute = pango_attr_weight_new (g_value_get_enum (&val));
1165 case PANGO_ATTR_VARIANT:
1166 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_VARIANT, value, &val, error))
1167 attribute = pango_attr_variant_new (g_value_get_enum (&val));
1169 case PANGO_ATTR_STRETCH:
1170 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STRETCH, value, &val, error))
1171 attribute = pango_attr_stretch_new (g_value_get_enum (&val));
1173 case PANGO_ATTR_UNDERLINE:
1174 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1175 attribute = pango_attr_underline_new (g_value_get_boolean (&val));
1177 case PANGO_ATTR_STRIKETHROUGH:
1178 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1179 attribute = pango_attr_strikethrough_new (g_value_get_boolean (&val));
1181 case PANGO_ATTR_GRAVITY:
1182 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY, value, &val, error))
1183 attribute = pango_attr_gravity_new (g_value_get_enum (&val));
1185 case PANGO_ATTR_GRAVITY_HINT:
1186 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY_HINT,
1187 value, &val, error))
1188 attribute = pango_attr_gravity_hint_new (g_value_get_enum (&val));
1190 /* PangoAttrString */
1191 case PANGO_ATTR_FAMILY:
1192 attribute = pango_attr_family_new (value);
1193 g_value_init (&val, G_TYPE_INT);
1197 case PANGO_ATTR_SIZE:
1198 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1199 value, &val, error))
1200 attribute = pango_attr_size_new (g_value_get_int (&val));
1202 case PANGO_ATTR_ABSOLUTE_SIZE:
1203 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1204 value, &val, error))
1205 attribute = pango_attr_size_new_absolute (g_value_get_int (&val));
1208 /* PangoAttrFontDesc */
1209 case PANGO_ATTR_FONT_DESC:
1210 if ((font_desc = pango_font_description_from_string (value)))
1212 attribute = pango_attr_font_desc_new (font_desc);
1213 pango_font_description_free (font_desc);
1214 g_value_init (&val, G_TYPE_INT);
1218 /* PangoAttrColor */
1219 case PANGO_ATTR_FOREGROUND:
1220 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1221 value, &val, error))
1223 color = g_value_get_boxed (&val);
1224 attribute = pango_attr_foreground_new (color->red, color->green, color->blue);
1227 case PANGO_ATTR_BACKGROUND:
1228 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1229 value, &val, error))
1231 color = g_value_get_boxed (&val);
1232 attribute = pango_attr_background_new (color->red, color->green, color->blue);
1235 case PANGO_ATTR_UNDERLINE_COLOR:
1236 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1237 value, &val, error))
1239 color = g_value_get_boxed (&val);
1240 attribute = pango_attr_underline_color_new (color->red, color->green, color->blue);
1243 case PANGO_ATTR_STRIKETHROUGH_COLOR:
1244 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1245 value, &val, error))
1247 color = g_value_get_boxed (&val);
1248 attribute = pango_attr_strikethrough_color_new (color->red, color->green, color->blue);
1252 /* PangoAttrShape */
1253 case PANGO_ATTR_SHAPE:
1254 /* Unsupported for now */
1256 /* PangoAttrFloat */
1257 case PANGO_ATTR_SCALE:
1258 if (gtk_builder_value_from_string_type (builder, G_TYPE_DOUBLE,
1259 value, &val, error))
1260 attribute = pango_attr_scale_new (g_value_get_double (&val));
1263 case PANGO_ATTR_INVALID:
1264 case PANGO_ATTR_LETTER_SPACING:
1265 case PANGO_ATTR_RISE:
1266 case PANGO_ATTR_FALLBACK:
1271 g_value_unset (&val);
1278 pango_start_element (GMarkupParseContext *context,
1279 const gchar *element_name,
1280 const gchar **names,
1281 const gchar **values,
1285 PangoParserData *data = (PangoParserData*)user_data;
1286 GValue val = { 0, };
1288 gint line_number, char_number;
1290 if (strcmp (element_name, "attribute") == 0)
1292 PangoAttribute *attr = NULL;
1293 const gchar *name = NULL;
1294 const gchar *value = NULL;
1295 const gchar *start = NULL;
1296 const gchar *end = NULL;
1297 guint start_val = 0;
1298 guint end_val = G_MAXUINT;
1300 for (i = 0; names[i]; i++)
1302 if (strcmp (names[i], "name") == 0)
1304 else if (strcmp (names[i], "value") == 0)
1306 else if (strcmp (names[i], "start") == 0)
1308 else if (strcmp (names[i], "end") == 0)
1312 g_markup_parse_context_get_position (context,
1317 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1318 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1320 line_number, char_number, names[i], "attribute");
1325 if (!name || !value)
1327 g_markup_parse_context_get_position (context,
1332 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1333 "%s:%d:%d <%s> requires attribute \"%s\"",
1335 line_number, char_number, "attribute",
1336 name ? "value" : "name");
1342 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1343 start, &val, error))
1345 start_val = g_value_get_uint (&val);
1346 g_value_unset (&val);
1351 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1354 end_val = g_value_get_uint (&val);
1355 g_value_unset (&val);
1358 attr = attribute_from_text (data->builder, name, value, error);
1359 attr->start_index = start_val;
1360 attr->end_index = end_val;
1365 data->attrs = pango_attr_list_new ();
1367 pango_attr_list_insert (data->attrs, attr);
1370 else if (strcmp (element_name, "attributes") == 0)
1373 g_warning ("Unsupported tag for GtkLabel: %s\n", element_name);
1376 static const GMarkupParser pango_parser =
1378 pango_start_element,
1382 gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
1383 GtkBuilder *builder,
1385 const gchar *tagname,
1386 GMarkupParser *parser,
1389 if (buildable_parent_iface->custom_tag_start (buildable, builder, child,
1390 tagname, parser, data))
1393 if (strcmp (tagname, "attributes") == 0)
1395 PangoParserData *parser_data;
1397 parser_data = g_slice_new0 (PangoParserData);
1398 parser_data->builder = g_object_ref (builder);
1399 parser_data->object = g_object_ref (buildable);
1400 *parser = pango_parser;
1401 *data = parser_data;
1408 gtk_label_buildable_custom_finished (GtkBuildable *buildable,
1409 GtkBuilder *builder,
1411 const gchar *tagname,
1414 PangoParserData *data;
1416 buildable_parent_iface->custom_finished (buildable, builder, child,
1417 tagname, user_data);
1419 if (strcmp (tagname, "attributes") == 0)
1421 data = (PangoParserData*)user_data;
1425 gtk_label_set_attributes (GTK_LABEL (buildable), data->attrs);
1426 pango_attr_list_unref (data->attrs);
1429 g_object_unref (data->object);
1430 g_object_unref (data->builder);
1431 g_slice_free (PangoParserData, data);
1438 * @str: The text of the label
1440 * Creates a new label with the given text inside it. You can
1441 * pass %NULL to get an empty label widget.
1443 * Return value: the new #GtkLabel
1446 gtk_label_new (const gchar *str)
1450 label = g_object_new (GTK_TYPE_LABEL, NULL);
1453 gtk_label_set_text (label, str);
1455 return GTK_WIDGET (label);
1459 * gtk_label_new_with_mnemonic:
1460 * @str: The text of the label, with an underscore in front of the
1461 * mnemonic character
1463 * Creates a new #GtkLabel, containing the text in @str.
1465 * If characters in @str are preceded by an underscore, they are
1466 * underlined. If you need a literal underscore character in a label, use
1467 * '__' (two underscores). The first underlined character represents a
1468 * keyboard accelerator called a mnemonic. The mnemonic key can be used
1469 * to activate another widget, chosen automatically, or explicitly using
1470 * gtk_label_set_mnemonic_widget().
1472 * If gtk_label_set_mnemonic_widget() is not called, then the first
1473 * activatable ancestor of the #GtkLabel will be chosen as the mnemonic
1474 * widget. For instance, if the label is inside a button or menu item,
1475 * the button or menu item will automatically become the mnemonic widget
1476 * and be activated by the mnemonic.
1478 * Return value: the new #GtkLabel
1481 gtk_label_new_with_mnemonic (const gchar *str)
1485 label = g_object_new (GTK_TYPE_LABEL, NULL);
1488 gtk_label_set_text_with_mnemonic (label, str);
1490 return GTK_WIDGET (label);
1494 gtk_label_mnemonic_activate (GtkWidget *widget,
1495 gboolean group_cycling)
1497 GtkLabel *label = GTK_LABEL (widget);
1498 GtkLabelPrivate *priv = label->priv;
1501 if (priv->mnemonic_widget)
1502 return gtk_widget_mnemonic_activate (priv->mnemonic_widget, group_cycling);
1504 /* Try to find the widget to activate by traversing the
1505 * widget's ancestry.
1507 parent = gtk_widget_get_parent (widget);
1509 if (GTK_IS_NOTEBOOK (parent))
1514 if (gtk_widget_get_can_focus (parent) ||
1515 (!group_cycling && GTK_WIDGET_GET_CLASS (parent)->activate_signal) ||
1516 GTK_IS_NOTEBOOK (gtk_widget_get_parent (parent)) ||
1517 GTK_IS_MENU_ITEM (parent))
1518 return gtk_widget_mnemonic_activate (parent, group_cycling);
1519 parent = gtk_widget_get_parent (parent);
1522 /* barf if there was nothing to activate */
1523 g_warning ("Couldn't find a target for a mnemonic activation.");
1524 gtk_widget_error_bell (widget);
1530 gtk_label_setup_mnemonic (GtkLabel *label,
1533 GtkLabelPrivate *priv = label->priv;
1534 GtkWidget *widget = GTK_WIDGET (label);
1535 GtkWidget *toplevel;
1536 GtkWidget *mnemonic_menu;
1538 mnemonic_menu = g_object_get_data (G_OBJECT (label), "gtk-mnemonic-menu");
1540 if (last_key != GDK_KEY_VoidSymbol)
1542 if (priv->mnemonic_window)
1544 gtk_window_remove_mnemonic (priv->mnemonic_window,
1547 priv->mnemonic_window = NULL;
1551 _gtk_menu_shell_remove_mnemonic (GTK_MENU_SHELL (mnemonic_menu),
1554 mnemonic_menu = NULL;
1558 if (priv->mnemonic_keyval == GDK_KEY_VoidSymbol)
1561 connect_mnemonics_visible_notify (GTK_LABEL (widget));
1563 toplevel = gtk_widget_get_toplevel (widget);
1564 if (gtk_widget_is_toplevel (toplevel))
1566 GtkWidget *menu_shell;
1568 menu_shell = gtk_widget_get_ancestor (widget,
1569 GTK_TYPE_MENU_SHELL);
1573 _gtk_menu_shell_add_mnemonic (GTK_MENU_SHELL (menu_shell),
1574 priv->mnemonic_keyval,
1576 mnemonic_menu = menu_shell;
1579 if (!GTK_IS_MENU (menu_shell))
1581 gtk_window_add_mnemonic (GTK_WINDOW (toplevel),
1582 priv->mnemonic_keyval,
1584 priv->mnemonic_window = GTK_WINDOW (toplevel);
1589 g_object_set_data (G_OBJECT (label), I_("gtk-mnemonic-menu"), mnemonic_menu);
1593 gtk_label_hierarchy_changed (GtkWidget *widget,
1594 GtkWidget *old_toplevel)
1596 GtkLabel *label = GTK_LABEL (widget);
1597 GtkLabelPrivate *priv = label->priv;
1599 gtk_label_setup_mnemonic (label, priv->mnemonic_keyval);
1603 label_shortcut_setting_apply (GtkLabel *label)
1605 gtk_label_recalculate (label);
1606 if (GTK_IS_ACCEL_LABEL (label))
1607 gtk_accel_label_refetch (GTK_ACCEL_LABEL (label));
1611 label_shortcut_setting_traverse_container (GtkWidget *widget,
1614 if (GTK_IS_LABEL (widget))
1615 label_shortcut_setting_apply (GTK_LABEL (widget));
1616 else if (GTK_IS_CONTAINER (widget))
1617 gtk_container_forall (GTK_CONTAINER (widget),
1618 label_shortcut_setting_traverse_container, data);
1622 label_shortcut_setting_changed (GtkSettings *settings)
1626 list = gtk_window_list_toplevels ();
1628 for (l = list; l ; l = l->next)
1630 GtkWidget *widget = l->data;
1632 if (gtk_widget_get_settings (widget) == settings)
1633 gtk_container_forall (GTK_CONTAINER (widget),
1634 label_shortcut_setting_traverse_container, NULL);
1641 mnemonics_visible_apply (GtkWidget *widget,
1642 gboolean mnemonics_visible)
1644 GtkLabel *label = GTK_LABEL (widget);
1645 GtkLabelPrivate *priv = label->priv;
1647 mnemonics_visible = mnemonics_visible != FALSE;
1649 if (priv->mnemonics_visible != mnemonics_visible)
1651 priv->mnemonics_visible = mnemonics_visible;
1653 gtk_label_recalculate (label);
1658 label_mnemonics_visible_traverse_container (GtkWidget *widget,
1661 gboolean mnemonics_visible = GPOINTER_TO_INT (data);
1663 _gtk_label_mnemonics_visible_apply_recursively (widget, mnemonics_visible);
1667 _gtk_label_mnemonics_visible_apply_recursively (GtkWidget *widget,
1668 gboolean mnemonics_visible)
1670 if (GTK_IS_LABEL (widget))
1671 mnemonics_visible_apply (widget, mnemonics_visible);
1672 else if (GTK_IS_CONTAINER (widget))
1673 gtk_container_forall (GTK_CONTAINER (widget),
1674 label_mnemonics_visible_traverse_container,
1675 GINT_TO_POINTER (mnemonics_visible));
1679 label_mnemonics_visible_changed (GtkWindow *window,
1683 gboolean mnemonics_visible;
1685 g_object_get (window, "mnemonics-visible", &mnemonics_visible, NULL);
1687 gtk_container_forall (GTK_CONTAINER (window),
1688 label_mnemonics_visible_traverse_container,
1689 GINT_TO_POINTER (mnemonics_visible));
1693 gtk_label_screen_changed (GtkWidget *widget,
1694 GdkScreen *old_screen)
1696 GtkSettings *settings;
1697 gboolean shortcuts_connected;
1699 if (!gtk_widget_has_screen (widget))
1702 settings = gtk_widget_get_settings (widget);
1704 shortcuts_connected =
1705 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (settings),
1706 "gtk-label-shortcuts-connected"));
1708 if (! shortcuts_connected)
1710 g_signal_connect (settings, "notify::gtk-enable-mnemonics",
1711 G_CALLBACK (label_shortcut_setting_changed),
1713 g_signal_connect (settings, "notify::gtk-enable-accels",
1714 G_CALLBACK (label_shortcut_setting_changed),
1717 g_object_set_data (G_OBJECT (settings), "gtk-label-shortcuts-connected",
1718 GINT_TO_POINTER (TRUE));
1721 label_shortcut_setting_apply (GTK_LABEL (widget));
1726 label_mnemonic_widget_weak_notify (gpointer data,
1727 GObject *where_the_object_was)
1729 GtkLabel *label = data;
1730 GtkLabelPrivate *priv = label->priv;
1732 priv->mnemonic_widget = NULL;
1733 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1737 * gtk_label_set_mnemonic_widget:
1738 * @label: a #GtkLabel
1739 * @widget: (allow-none): the target #GtkWidget
1741 * If the label has been set so that it has an mnemonic key (using
1742 * i.e. gtk_label_set_markup_with_mnemonic(),
1743 * gtk_label_set_text_with_mnemonic(), gtk_label_new_with_mnemonic()
1744 * or the "use_underline" property) the label can be associated with a
1745 * widget that is the target of the mnemonic. When the label is inside
1746 * a widget (like a #GtkButton or a #GtkNotebook tab) it is
1747 * automatically associated with the correct widget, but sometimes
1748 * (i.e. when the target is a #GtkEntry next to the label) you need to
1749 * set it explicitly using this function.
1751 * The target widget will be accelerated by emitting the
1752 * GtkWidget::mnemonic-activate signal on it. The default handler for
1753 * this signal will activate the widget if there are no mnemonic collisions
1754 * and toggle focus between the colliding widgets otherwise.
1757 gtk_label_set_mnemonic_widget (GtkLabel *label,
1760 GtkLabelPrivate *priv;
1762 g_return_if_fail (GTK_IS_LABEL (label));
1767 g_return_if_fail (GTK_IS_WIDGET (widget));
1769 if (priv->mnemonic_widget)
1771 gtk_widget_remove_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1772 g_object_weak_unref (G_OBJECT (priv->mnemonic_widget),
1773 label_mnemonic_widget_weak_notify,
1776 priv->mnemonic_widget = widget;
1777 if (priv->mnemonic_widget)
1779 g_object_weak_ref (G_OBJECT (priv->mnemonic_widget),
1780 label_mnemonic_widget_weak_notify,
1782 gtk_widget_add_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1785 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1789 * gtk_label_get_mnemonic_widget:
1790 * @label: a #GtkLabel
1792 * Retrieves the target of the mnemonic (keyboard shortcut) of this
1793 * label. See gtk_label_set_mnemonic_widget().
1795 * Return value: (transfer none): the target of the label's mnemonic,
1796 * or %NULL if none has been set and the default algorithm will be used.
1799 gtk_label_get_mnemonic_widget (GtkLabel *label)
1801 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1803 return label->priv->mnemonic_widget;
1807 * gtk_label_get_mnemonic_keyval:
1808 * @label: a #GtkLabel
1810 * If the label has been set so that it has an mnemonic key this function
1811 * returns the keyval used for the mnemonic accelerator. If there is no
1812 * mnemonic set up it returns #GDK_VoidSymbol.
1814 * Returns: GDK keyval usable for accelerators, or #GDK_VoidSymbol
1817 gtk_label_get_mnemonic_keyval (GtkLabel *label)
1819 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_KEY_VoidSymbol);
1821 return label->priv->mnemonic_keyval;
1825 gtk_label_set_text_internal (GtkLabel *label,
1828 GtkLabelPrivate *priv = label->priv;
1830 g_free (priv->text);
1834 gtk_label_select_region_index (label, 0, 0);
1838 gtk_label_set_label_internal (GtkLabel *label,
1841 GtkLabelPrivate *priv = label->priv;
1843 g_free (priv->label);
1847 g_object_notify (G_OBJECT (label), "label");
1851 gtk_label_set_use_markup_internal (GtkLabel *label,
1854 GtkLabelPrivate *priv = label->priv;
1857 if (priv->use_markup != val)
1859 priv->use_markup = val;
1861 g_object_notify (G_OBJECT (label), "use-markup");
1866 gtk_label_set_use_underline_internal (GtkLabel *label,
1869 GtkLabelPrivate *priv = label->priv;
1872 if (priv->use_underline != val)
1874 priv->use_underline = val;
1876 g_object_notify (G_OBJECT (label), "use-underline");
1881 gtk_label_compose_effective_attrs (GtkLabel *label)
1883 GtkLabelPrivate *priv = label->priv;
1884 PangoAttrIterator *iter;
1885 PangoAttribute *attr;
1886 GSList *iter_attrs, *l;
1890 if (priv->effective_attrs)
1892 if ((iter = pango_attr_list_get_iterator (priv->attrs)))
1896 iter_attrs = pango_attr_iterator_get_attrs (iter);
1897 for (l = iter_attrs; l; l = l->next)
1900 pango_attr_list_insert (priv->effective_attrs, attr);
1902 g_slist_free (iter_attrs);
1904 while (pango_attr_iterator_next (iter));
1905 pango_attr_iterator_destroy (iter);
1909 priv->effective_attrs =
1910 pango_attr_list_ref (priv->attrs);
1915 gtk_label_set_attributes_internal (GtkLabel *label,
1916 PangoAttrList *attrs)
1918 GtkLabelPrivate *priv = label->priv;
1921 pango_attr_list_ref (attrs);
1924 pango_attr_list_unref (priv->attrs);
1925 priv->attrs = attrs;
1927 g_object_notify (G_OBJECT (label), "attributes");
1931 /* Calculates text, attrs and mnemonic_keyval from
1932 * label, use_underline and use_markup
1935 gtk_label_recalculate (GtkLabel *label)
1937 GtkLabelPrivate *priv = label->priv;
1938 guint keyval = priv->mnemonic_keyval;
1940 if (priv->use_markup)
1941 gtk_label_set_markup_internal (label, priv->label, priv->use_underline);
1944 if (priv->use_underline)
1945 gtk_label_set_uline_text_internal (label, priv->label);
1948 if (priv->effective_attrs)
1949 pango_attr_list_unref (priv->effective_attrs);
1950 priv->effective_attrs = NULL;
1951 gtk_label_set_text_internal (label, g_strdup (priv->label));
1955 gtk_label_compose_effective_attrs (label);
1957 if (!priv->use_underline)
1958 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
1960 if (keyval != priv->mnemonic_keyval)
1962 gtk_label_setup_mnemonic (label, keyval);
1963 g_object_notify (G_OBJECT (label), "mnemonic-keyval");
1966 gtk_label_clear_layout (label);
1967 gtk_label_clear_select_info (label);
1968 gtk_widget_queue_resize (GTK_WIDGET (label));
1972 * gtk_label_set_text:
1973 * @label: a #GtkLabel
1974 * @str: The text you want to set
1976 * Sets the text within the #GtkLabel widget. It overwrites any text that
1979 * This will also clear any previously set mnemonic accelerators.
1982 gtk_label_set_text (GtkLabel *label,
1985 g_return_if_fail (GTK_IS_LABEL (label));
1987 g_object_freeze_notify (G_OBJECT (label));
1989 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
1990 gtk_label_set_use_markup_internal (label, FALSE);
1991 gtk_label_set_use_underline_internal (label, FALSE);
1993 gtk_label_recalculate (label);
1995 g_object_thaw_notify (G_OBJECT (label));
1999 * gtk_label_set_attributes:
2000 * @label: a #GtkLabel
2001 * @attrs: a #PangoAttrList
2003 * Sets a #PangoAttrList; the attributes in the list are applied to the
2006 * <note><para>The attributes set with this function will be applied
2007 * and merged with any other attributes previously effected by way
2008 * of the #GtkLabel:use-underline or #GtkLabel:use-markup properties.
2009 * While it is not recommended to mix markup strings with manually set
2010 * attributes, if you must; know that the attributes will be applied
2011 * to the label after the markup string is parsed.</para></note>
2014 gtk_label_set_attributes (GtkLabel *label,
2015 PangoAttrList *attrs)
2017 g_return_if_fail (GTK_IS_LABEL (label));
2019 gtk_label_set_attributes_internal (label, attrs);
2021 gtk_label_recalculate (label);
2023 gtk_label_clear_layout (label);
2024 gtk_widget_queue_resize (GTK_WIDGET (label));
2028 * gtk_label_get_attributes:
2029 * @label: a #GtkLabel
2031 * Gets the attribute list that was set on the label using
2032 * gtk_label_set_attributes(), if any. This function does
2033 * not reflect attributes that come from the labels markup
2034 * (see gtk_label_set_markup()). If you want to get the
2035 * effective attributes for the label, use
2036 * pango_layout_get_attribute (gtk_label_get_layout (label)).
2038 * Return value: (transfer none): the attribute list, or %NULL
2042 gtk_label_get_attributes (GtkLabel *label)
2044 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2046 return label->priv->attrs;
2050 * gtk_label_set_label:
2051 * @label: a #GtkLabel
2052 * @str: the new text to set for the label
2054 * Sets the text of the label. The label is interpreted as
2055 * including embedded underlines and/or Pango markup depending
2056 * on the values of the #GtkLabel:use-underline" and
2057 * #GtkLabel:use-markup properties.
2060 gtk_label_set_label (GtkLabel *label,
2063 g_return_if_fail (GTK_IS_LABEL (label));
2065 g_object_freeze_notify (G_OBJECT (label));
2067 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2068 gtk_label_recalculate (label);
2070 g_object_thaw_notify (G_OBJECT (label));
2074 * gtk_label_get_label:
2075 * @label: a #GtkLabel
2077 * Fetches the text from a label widget including any embedded
2078 * underlines indicating mnemonics and Pango markup. (See
2079 * gtk_label_get_text()).
2081 * Return value: the text of the label widget. This string is
2082 * owned by the widget and must not be modified or freed.
2084 G_CONST_RETURN gchar *
2085 gtk_label_get_label (GtkLabel *label)
2087 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2089 return label->priv->label;
2097 GdkColor *link_color;
2098 GdkColor *visited_link_color;
2102 start_element_handler (GMarkupParseContext *context,
2103 const gchar *element_name,
2104 const gchar **attribute_names,
2105 const gchar **attribute_values,
2109 GtkLabelPrivate *priv;
2110 UriParserData *pdata = user_data;
2112 if (strcmp (element_name, "a") == 0)
2115 const gchar *uri = NULL;
2116 const gchar *title = NULL;
2117 gboolean visited = FALSE;
2121 GdkColor *color = NULL;
2123 g_markup_parse_context_get_position (context, &line_number, &char_number);
2125 for (i = 0; attribute_names[i] != NULL; i++)
2127 const gchar *attr = attribute_names[i];
2129 if (strcmp (attr, "href") == 0)
2130 uri = attribute_values[i];
2131 else if (strcmp (attr, "title") == 0)
2132 title = attribute_values[i];
2137 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
2138 "Attribute '%s' is not allowed on the <a> tag "
2139 "on line %d char %d",
2140 attr, line_number, char_number);
2149 G_MARKUP_ERROR_INVALID_CONTENT,
2150 "Attribute 'href' was missing on the <a> tag "
2151 "on line %d char %d",
2152 line_number, char_number);
2157 priv = pdata->label->priv;
2158 if (priv->track_links && priv->select_info)
2161 for (l = priv->select_info->links; l; l = l->next)
2164 if (strcmp (uri, link->uri) == 0)
2166 visited = link->visited;
2173 color = pdata->visited_link_color;
2175 color = pdata->link_color;
2177 g_string_append_printf (pdata->new_str,
2178 "<span color=\"#%04x%04x%04x\" underline=\"single\">",
2183 link = g_new0 (GtkLabelLink, 1);
2184 link->uri = g_strdup (uri);
2185 link->title = g_strdup (title);
2186 link->visited = visited;
2187 pdata->links = g_list_append (pdata->links, link);
2193 g_string_append_c (pdata->new_str, '<');
2194 g_string_append (pdata->new_str, element_name);
2196 for (i = 0; attribute_names[i] != NULL; i++)
2198 const gchar *attr = attribute_names[i];
2199 const gchar *value = attribute_values[i];
2202 newvalue = g_markup_escape_text (value, -1);
2204 g_string_append_c (pdata->new_str, ' ');
2205 g_string_append (pdata->new_str, attr);
2206 g_string_append (pdata->new_str, "=\"");
2207 g_string_append (pdata->new_str, newvalue);
2208 g_string_append_c (pdata->new_str, '\"');
2212 g_string_append_c (pdata->new_str, '>');
2217 end_element_handler (GMarkupParseContext *context,
2218 const gchar *element_name,
2222 UriParserData *pdata = user_data;
2224 if (!strcmp (element_name, "a"))
2225 g_string_append (pdata->new_str, "</span>");
2228 g_string_append (pdata->new_str, "</");
2229 g_string_append (pdata->new_str, element_name);
2230 g_string_append_c (pdata->new_str, '>');
2235 text_handler (GMarkupParseContext *context,
2241 UriParserData *pdata = user_data;
2244 newtext = g_markup_escape_text (text, text_len);
2245 g_string_append (pdata->new_str, newtext);
2249 static const GMarkupParser markup_parser =
2251 start_element_handler,
2252 end_element_handler,
2259 xml_isspace (gchar c)
2261 return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
2265 link_free (GtkLabelLink *link)
2268 g_free (link->title);
2273 gtk_label_get_link_colors (GtkWidget *widget,
2274 GdkColor **link_color,
2275 GdkColor **visited_link_color)
2277 GtkStyleContext *context;
2279 context = gtk_widget_get_style_context (widget);
2280 gtk_style_context_get_style (context,
2281 "link-color", link_color,
2282 "visited-link-color", visited_link_color,
2285 *link_color = gdk_color_copy (&default_link_color);
2286 if (!*visited_link_color)
2287 *visited_link_color = gdk_color_copy (&default_visited_link_color);
2291 parse_uri_markup (GtkLabel *label,
2297 GMarkupParseContext *context = NULL;
2298 const gchar *p, *end;
2299 gboolean needs_root = TRUE;
2301 UriParserData pdata;
2303 length = strlen (str);
2307 pdata.label = label;
2309 pdata.new_str = g_string_sized_new (length);
2311 gtk_label_get_link_colors (GTK_WIDGET (label), &pdata.link_color, &pdata.visited_link_color);
2313 while (p != end && xml_isspace (*p))
2316 if (end - p >= 8 && strncmp (p, "<markup>", 8) == 0)
2319 context = g_markup_parse_context_new (&markup_parser, 0, &pdata, NULL);
2323 if (!g_markup_parse_context_parse (context, "<markup>", -1, error))
2327 if (!g_markup_parse_context_parse (context, str, length, error))
2332 if (!g_markup_parse_context_parse (context, "</markup>", -1, error))
2336 if (!g_markup_parse_context_end_parse (context, error))
2339 g_markup_parse_context_free (context);
2341 *new_str = g_string_free (pdata.new_str, FALSE);
2342 *links = pdata.links;
2344 gdk_color_free (pdata.link_color);
2345 gdk_color_free (pdata.visited_link_color);
2350 g_markup_parse_context_free (context);
2351 g_string_free (pdata.new_str, TRUE);
2352 g_list_foreach (pdata.links, (GFunc)link_free, NULL);
2353 g_list_free (pdata.links);
2354 gdk_color_free (pdata.link_color);
2355 gdk_color_free (pdata.visited_link_color);
2361 gtk_label_ensure_has_tooltip (GtkLabel *label)
2363 GtkLabelPrivate *priv = label->priv;
2365 gboolean has_tooltip = FALSE;
2367 for (l = priv->select_info->links; l; l = l->next)
2369 GtkLabelLink *link = l->data;
2377 gtk_widget_set_has_tooltip (GTK_WIDGET (label), has_tooltip);
2381 gtk_label_set_markup_internal (GtkLabel *label,
2383 gboolean with_uline)
2385 GtkLabelPrivate *priv = label->priv;
2387 GError *error = NULL;
2388 PangoAttrList *attrs = NULL;
2389 gunichar accel_char = 0;
2391 GList *links = NULL;
2393 if (!parse_uri_markup (label, str, &new_str, &links, &error))
2395 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2397 g_error_free (error);
2401 gtk_label_clear_links (label);
2404 gtk_label_ensure_select_info (label);
2405 priv->select_info->links = links;
2406 gtk_label_ensure_has_tooltip (label);
2411 gboolean enable_mnemonics;
2412 gboolean auto_mnemonics;
2414 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2415 "gtk-enable-mnemonics", &enable_mnemonics,
2416 "gtk-auto-mnemonics", &auto_mnemonics,
2419 if (!(enable_mnemonics && priv->mnemonics_visible &&
2421 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2422 (!priv->mnemonic_widget ||
2423 gtk_widget_is_sensitive (priv->mnemonic_widget))))))
2429 if (separate_uline_pattern (new_str, &key, &tmp, &pattern))
2438 if (!pango_parse_markup (new_str,
2440 with_uline ? '_' : 0,
2443 with_uline ? &accel_char : NULL,
2446 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2449 g_error_free (error);
2456 gtk_label_set_text_internal (label, text);
2460 if (priv->effective_attrs)
2461 pango_attr_list_unref (priv->effective_attrs);
2462 priv->effective_attrs = attrs;
2465 if (accel_char != 0)
2466 priv->mnemonic_keyval = gdk_keyval_to_lower (gdk_unicode_to_keyval (accel_char));
2468 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
2472 * gtk_label_set_markup:
2473 * @label: a #GtkLabel
2474 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
2476 * Parses @str which is marked up with the <link
2477 * linkend="PangoMarkupFormat">Pango text markup language</link>, setting the
2478 * label's text and attribute list based on the parse results. If the @str is
2479 * external data, you may need to escape it with g_markup_escape_text() or
2480 * g_markup_printf_escaped()<!-- -->:
2484 * markup = g_markup_printf_escaped ("<span style=\"italic\">%s</span>", str);
2485 * gtk_label_set_markup (GTK_LABEL (label), markup);
2490 gtk_label_set_markup (GtkLabel *label,
2493 g_return_if_fail (GTK_IS_LABEL (label));
2495 g_object_freeze_notify (G_OBJECT (label));
2497 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2498 gtk_label_set_use_markup_internal (label, TRUE);
2499 gtk_label_set_use_underline_internal (label, FALSE);
2501 gtk_label_recalculate (label);
2503 g_object_thaw_notify (G_OBJECT (label));
2507 * gtk_label_set_markup_with_mnemonic:
2508 * @label: a #GtkLabel
2509 * @str: a markup string (see
2510 * <link linkend="PangoMarkupFormat">Pango markup format</link>)
2512 * Parses @str which is marked up with the
2513 * <link linkend="PangoMarkupFormat">Pango text markup language</link>,
2514 * setting the label's text and attribute list based on the parse results.
2515 * If characters in @str are preceded by an underscore, they are underlined
2516 * indicating that they represent a keyboard accelerator called a mnemonic.
2518 * The mnemonic key can be used to activate another widget, chosen
2519 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
2522 gtk_label_set_markup_with_mnemonic (GtkLabel *label,
2525 g_return_if_fail (GTK_IS_LABEL (label));
2527 g_object_freeze_notify (G_OBJECT (label));
2529 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2530 gtk_label_set_use_markup_internal (label, TRUE);
2531 gtk_label_set_use_underline_internal (label, TRUE);
2533 gtk_label_recalculate (label);
2535 g_object_thaw_notify (G_OBJECT (label));
2539 * gtk_label_get_text:
2540 * @label: a #GtkLabel
2542 * Fetches the text from a label widget, as displayed on the
2543 * screen. This does not include any embedded underlines
2544 * indicating mnemonics or Pango markup. (See gtk_label_get_label())
2546 * Return value: the text in the label widget. This is the internal
2547 * string used by the label, and must not be modified.
2549 G_CONST_RETURN gchar *
2550 gtk_label_get_text (GtkLabel *label)
2552 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2554 return label->priv->text;
2557 static PangoAttrList *
2558 gtk_label_pattern_to_attrs (GtkLabel *label,
2559 const gchar *pattern)
2561 GtkLabelPrivate *priv = label->priv;
2563 const char *p = priv->text;
2564 const char *q = pattern;
2565 PangoAttrList *attrs;
2567 attrs = pango_attr_list_new ();
2571 while (*p && *q && *q != '_')
2573 p = g_utf8_next_char (p);
2577 while (*p && *q && *q == '_')
2579 p = g_utf8_next_char (p);
2585 PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_LOW);
2586 attr->start_index = start - priv->text;
2587 attr->end_index = p - priv->text;
2589 pango_attr_list_insert (attrs, attr);
2599 gtk_label_set_pattern_internal (GtkLabel *label,
2600 const gchar *pattern,
2601 gboolean is_mnemonic)
2603 GtkLabelPrivate *priv = label->priv;
2604 PangoAttrList *attrs;
2605 gboolean enable_mnemonics;
2606 gboolean auto_mnemonics;
2608 if (priv->pattern_set)
2613 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2614 "gtk-enable-mnemonics", &enable_mnemonics,
2615 "gtk-auto-mnemonics", &auto_mnemonics,
2618 if (enable_mnemonics && priv->mnemonics_visible && pattern &&
2620 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2621 (!priv->mnemonic_widget ||
2622 gtk_widget_is_sensitive (priv->mnemonic_widget)))))
2623 attrs = gtk_label_pattern_to_attrs (label, pattern);
2628 attrs = gtk_label_pattern_to_attrs (label, pattern);
2630 if (priv->effective_attrs)
2631 pango_attr_list_unref (priv->effective_attrs);
2632 priv->effective_attrs = attrs;
2636 gtk_label_set_pattern (GtkLabel *label,
2637 const gchar *pattern)
2639 GtkLabelPrivate *priv = label->priv;
2641 g_return_if_fail (GTK_IS_LABEL (label));
2645 priv->pattern_set = FALSE;
2649 gtk_label_set_pattern_internal (label, pattern, FALSE);
2650 priv->pattern_set = TRUE;
2653 gtk_label_recalculate (label);
2655 gtk_label_clear_layout (label);
2656 gtk_widget_queue_resize (GTK_WIDGET (label));
2661 * gtk_label_set_justify:
2662 * @label: a #GtkLabel
2663 * @jtype: a #GtkJustification
2665 * Sets the alignment of the lines in the text of the label relative to
2666 * each other. %GTK_JUSTIFY_LEFT is the default value when the
2667 * widget is first created with gtk_label_new(). If you instead want
2668 * to set the alignment of the label as a whole, use
2669 * gtk_misc_set_alignment() instead. gtk_label_set_justify() has no
2670 * effect on labels containing only a single line.
2673 gtk_label_set_justify (GtkLabel *label,
2674 GtkJustification jtype)
2676 GtkLabelPrivate *priv;
2678 g_return_if_fail (GTK_IS_LABEL (label));
2679 g_return_if_fail (jtype >= GTK_JUSTIFY_LEFT && jtype <= GTK_JUSTIFY_FILL);
2683 if ((GtkJustification) priv->jtype != jtype)
2685 priv->jtype = jtype;
2687 /* No real need to be this drastic, but easier than duplicating the code */
2688 gtk_label_clear_layout (label);
2690 g_object_notify (G_OBJECT (label), "justify");
2691 gtk_widget_queue_resize (GTK_WIDGET (label));
2696 * gtk_label_get_justify:
2697 * @label: a #GtkLabel
2699 * Returns the justification of the label. See gtk_label_set_justify().
2701 * Return value: #GtkJustification
2704 gtk_label_get_justify (GtkLabel *label)
2706 g_return_val_if_fail (GTK_IS_LABEL (label), 0);
2708 return label->priv->jtype;
2712 * gtk_label_set_ellipsize:
2713 * @label: a #GtkLabel
2714 * @mode: a #PangoEllipsizeMode
2716 * Sets the mode used to ellipsize (add an ellipsis: "...") to the text
2717 * if there is not enough space to render the entire string.
2722 gtk_label_set_ellipsize (GtkLabel *label,
2723 PangoEllipsizeMode mode)
2725 GtkLabelPrivate *priv;
2727 g_return_if_fail (GTK_IS_LABEL (label));
2728 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE && mode <= PANGO_ELLIPSIZE_END);
2732 if ((PangoEllipsizeMode) priv->ellipsize != mode)
2734 priv->ellipsize = mode;
2736 /* No real need to be this drastic, but easier than duplicating the code */
2737 gtk_label_clear_layout (label);
2739 g_object_notify (G_OBJECT (label), "ellipsize");
2740 gtk_widget_queue_resize (GTK_WIDGET (label));
2745 * gtk_label_get_ellipsize:
2746 * @label: a #GtkLabel
2748 * Returns the ellipsizing position of the label. See gtk_label_set_ellipsize().
2750 * Return value: #PangoEllipsizeMode
2755 gtk_label_get_ellipsize (GtkLabel *label)
2757 g_return_val_if_fail (GTK_IS_LABEL (label), PANGO_ELLIPSIZE_NONE);
2759 return label->priv->ellipsize;
2763 * gtk_label_set_width_chars:
2764 * @label: a #GtkLabel
2765 * @n_chars: the new desired width, in characters.
2767 * Sets the desired width in characters of @label to @n_chars.
2772 gtk_label_set_width_chars (GtkLabel *label,
2775 GtkLabelPrivate *priv;
2777 g_return_if_fail (GTK_IS_LABEL (label));
2781 if (priv->width_chars != n_chars)
2783 priv->width_chars = n_chars;
2784 g_object_notify (G_OBJECT (label), "width-chars");
2785 gtk_label_invalidate_wrap_width (label);
2786 gtk_widget_queue_resize (GTK_WIDGET (label));
2791 * gtk_label_get_width_chars:
2792 * @label: a #GtkLabel
2794 * Retrieves the desired width of @label, in characters. See
2795 * gtk_label_set_width_chars().
2797 * Return value: the width of the label in characters.
2802 gtk_label_get_width_chars (GtkLabel *label)
2804 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2806 return label->priv->width_chars;
2810 * gtk_label_set_max_width_chars:
2811 * @label: a #GtkLabel
2812 * @n_chars: the new desired maximum width, in characters.
2814 * Sets the desired maximum width in characters of @label to @n_chars.
2819 gtk_label_set_max_width_chars (GtkLabel *label,
2822 GtkLabelPrivate *priv;
2824 g_return_if_fail (GTK_IS_LABEL (label));
2828 if (priv->max_width_chars != n_chars)
2830 priv->max_width_chars = n_chars;
2832 g_object_notify (G_OBJECT (label), "max-width-chars");
2833 gtk_label_invalidate_wrap_width (label);
2834 gtk_widget_queue_resize (GTK_WIDGET (label));
2839 * gtk_label_get_max_width_chars:
2840 * @label: a #GtkLabel
2842 * Retrieves the desired maximum width of @label, in characters. See
2843 * gtk_label_set_width_chars().
2845 * Return value: the maximum width of the label in characters.
2850 gtk_label_get_max_width_chars (GtkLabel *label)
2852 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2854 return label->priv->max_width_chars;
2858 * gtk_label_set_line_wrap:
2859 * @label: a #GtkLabel
2860 * @wrap: the setting
2862 * Toggles line wrapping within the #GtkLabel widget. %TRUE makes it break
2863 * lines if text exceeds the widget's size. %FALSE lets the text get cut off
2864 * by the edge of the widget if it exceeds the widget size.
2866 * Note that setting line wrapping to %TRUE does not make the label
2867 * wrap at its parent container's width, because GTK+ widgets
2868 * conceptually can't make their requisition depend on the parent
2869 * container's size. For a label that wraps at a specific position,
2870 * set the label's width using gtk_widget_set_size_request().
2873 gtk_label_set_line_wrap (GtkLabel *label,
2876 GtkLabelPrivate *priv;
2878 g_return_if_fail (GTK_IS_LABEL (label));
2882 wrap = wrap != FALSE;
2884 if (priv->wrap != wrap)
2888 gtk_label_clear_layout (label);
2889 gtk_widget_queue_resize (GTK_WIDGET (label));
2890 g_object_notify (G_OBJECT (label), "wrap");
2895 * gtk_label_get_line_wrap:
2896 * @label: a #GtkLabel
2898 * Returns whether lines in the label are automatically wrapped.
2899 * See gtk_label_set_line_wrap().
2901 * Return value: %TRUE if the lines of the label are automatically wrapped.
2904 gtk_label_get_line_wrap (GtkLabel *label)
2906 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
2908 return label->priv->wrap;
2912 * gtk_label_set_line_wrap_mode:
2913 * @label: a #GtkLabel
2914 * @wrap_mode: the line wrapping mode
2916 * If line wrapping is on (see gtk_label_set_line_wrap()) this controls how
2917 * the line wrapping is done. The default is %PANGO_WRAP_WORD which means
2918 * wrap on word boundaries.
2923 gtk_label_set_line_wrap_mode (GtkLabel *label,
2924 PangoWrapMode wrap_mode)
2926 GtkLabelPrivate *priv;
2928 g_return_if_fail (GTK_IS_LABEL (label));
2932 if (priv->wrap_mode != wrap_mode)
2934 priv->wrap_mode = wrap_mode;
2935 g_object_notify (G_OBJECT (label), "wrap-mode");
2937 gtk_widget_queue_resize (GTK_WIDGET (label));
2942 * gtk_label_get_line_wrap_mode:
2943 * @label: a #GtkLabel
2945 * Returns line wrap mode used by the label. See gtk_label_set_line_wrap_mode().
2947 * Return value: %TRUE if the lines of the label are automatically wrapped.
2952 gtk_label_get_line_wrap_mode (GtkLabel *label)
2954 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
2956 return label->priv->wrap_mode;
2960 gtk_label_destroy (GtkWidget *widget)
2962 GtkLabel *label = GTK_LABEL (widget);
2964 gtk_label_set_mnemonic_widget (label, NULL);
2966 GTK_WIDGET_CLASS (gtk_label_parent_class)->destroy (widget);
2970 gtk_label_finalize (GObject *object)
2972 GtkLabel *label = GTK_LABEL (object);
2973 GtkLabelPrivate *priv = label->priv;
2975 g_free (priv->label);
2976 g_free (priv->text);
2979 g_object_unref (priv->layout);
2982 pango_attr_list_unref (priv->attrs);
2984 if (priv->effective_attrs)
2985 pango_attr_list_unref (priv->effective_attrs);
2987 gtk_label_clear_links (label);
2988 g_free (priv->select_info);
2990 G_OBJECT_CLASS (gtk_label_parent_class)->finalize (object);
2994 gtk_label_clear_layout (GtkLabel *label)
2996 GtkLabelPrivate *priv = label->priv;
3000 g_object_unref (priv->layout);
3001 priv->layout = NULL;
3003 //gtk_label_clear_links (label);
3007 static PangoFontMetrics *
3008 get_font_metrics (PangoContext *context, GtkWidget *widget)
3010 GtkStyleContext *style_context;
3011 PangoFontDescription *font;
3012 PangoFontMetrics *retval;
3014 style_context = gtk_widget_get_style_context (widget);
3015 gtk_style_context_get (style_context, 0, "font", &font, NULL);
3017 retval = pango_context_get_metrics (context,
3019 pango_context_get_language (context));
3022 pango_font_description_free (font);
3028 * gtk_label_get_measuring_layout:
3030 * @existing_layout: %NULL or an existing layout already in use.
3031 * @width: the width to measure with in pango units, or -1 for infinite
3033 * Gets a layout that can be used for measuring sizes. The returned
3034 * layout will be identical to the label's layout except for the
3035 * layout's width, which will be set to @width. Do not modify the returned
3038 * Returns: a new reference to a pango layout
3040 static PangoLayout *
3041 gtk_label_get_measuring_layout (GtkLabel * label,
3042 PangoLayout *existing_layout,
3045 GtkLabelPrivate *priv = label->priv;
3046 PangoRectangle rect;
3049 if (existing_layout != NULL)
3051 if (existing_layout != priv->layout)
3053 pango_layout_set_width (existing_layout, width);
3054 return existing_layout;
3057 g_object_unref (existing_layout);
3060 gtk_label_ensure_layout (label);
3062 if (pango_layout_get_width (priv->layout) == width)
3064 g_object_ref (priv->layout);
3065 return priv->layout;
3068 /* We can use the label's own layout if we're not allocated a size yet,
3069 * because we don't need it to be properly setup at that point.
3070 * This way we can make use of caching upon the label's creation.
3072 if (gtk_widget_get_allocated_width (GTK_WIDGET (label)) <= 1)
3074 g_object_ref (priv->layout);
3075 pango_layout_set_width (priv->layout, width);
3076 return priv->layout;
3079 /* oftentimes we want to measure a width that is far wider than the current width,
3080 * even though the layout is not wrapped. In that case, we can just return the
3081 * current layout, because for measuring purposes, it will be identical.
3083 pango_layout_get_extents (priv->layout, NULL, &rect);
3084 if ((width == -1 || rect.width <= width) &&
3085 !pango_layout_is_wrapped (priv->layout))
3087 g_object_ref (priv->layout);
3088 return priv->layout;
3091 copy = pango_layout_copy (priv->layout);
3092 pango_layout_set_width (copy, width);
3097 gtk_label_invalidate_wrap_width (GtkLabel *label)
3099 GtkLabelPrivate *priv = label->priv;
3101 priv->wrap_width = -1;
3105 get_label_wrap_width (GtkLabel *label)
3107 GtkLabelPrivate *priv = label->priv;
3109 if (priv->wrap_width < 0)
3111 if (priv->width_chars > 0)
3113 PangoLayout *layout;
3114 PangoContext *context;
3115 PangoFontMetrics *metrics;
3116 PangoRectangle rect;
3117 gint char_width, digit_width, char_pixels, text_width;
3119 layout = gtk_label_get_measuring_layout (label, NULL, -1);
3120 context = pango_layout_get_context (layout);
3121 metrics = get_font_metrics (context, GTK_WIDGET (label));
3122 char_width = pango_font_metrics_get_approximate_char_width (metrics);
3123 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
3124 char_pixels = MAX (char_width, digit_width);
3125 pango_font_metrics_unref (metrics);
3127 pango_layout_get_extents (layout, NULL, &rect);
3128 g_object_unref (layout);
3130 text_width = rect.width;
3132 priv->wrap_width = PANGO_PIXELS (MAX (text_width, char_pixels * priv->width_chars));
3136 PangoLayout *layout;
3138 layout = gtk_widget_create_pango_layout (GTK_WIDGET (label),
3139 "This string is just about long enough.");
3140 pango_layout_get_size (layout, &priv->wrap_width, NULL);
3141 g_object_unref (layout);
3145 return priv->wrap_width;
3148 static PangoLayout *
3149 gtk_label_get_layout_with_guessed_wrap_width (GtkLabel *label)
3152 PangoRectangle logical_rect;
3153 gint wrap_width, width, height, longest_paragraph;
3154 PangoLayout *layout;
3156 if (!label->priv->wrap)
3157 return gtk_label_get_measuring_layout (label, NULL, -1);
3159 screen = gtk_widget_get_screen (GTK_WIDGET (label));
3161 layout = gtk_label_get_measuring_layout (label, NULL, -1);
3162 pango_layout_get_extents (layout, NULL, &logical_rect);
3164 width = logical_rect.width;
3165 /* Try to guess a reasonable maximum width */
3166 longest_paragraph = width;
3168 wrap_width = get_label_wrap_width (label);
3169 width = MIN (width, wrap_width);
3171 PANGO_SCALE * (gdk_screen_get_width (screen) + 1) / 2);
3173 layout = gtk_label_get_measuring_layout (label, layout, width);
3175 pango_layout_get_extents (layout, NULL, &logical_rect);
3176 width = logical_rect.width;
3177 height = logical_rect.height;
3179 /* Unfortunately, the above may leave us with a very unbalanced looking paragraph,
3180 * so we try short search for a narrower width that leaves us with the same height
3182 if (longest_paragraph > 0)
3184 gint nlines, perfect_width;
3186 nlines = pango_layout_get_line_count (layout);
3187 perfect_width = (longest_paragraph + nlines - 1) / nlines;
3189 if (perfect_width < width)
3191 layout = gtk_label_get_measuring_layout (label, layout, perfect_width);
3192 pango_layout_get_extents (layout, NULL, &logical_rect);
3194 if (logical_rect.height <= height)
3195 width = logical_rect.width;
3198 gint mid_width = (perfect_width + width) / 2;
3200 if (mid_width > perfect_width)
3202 layout = gtk_label_get_measuring_layout (label, layout, mid_width);
3203 pango_layout_get_extents (layout, NULL, &logical_rect);
3205 if (logical_rect.height <= height)
3206 width = logical_rect.width;
3212 return gtk_label_get_measuring_layout (label, layout, width);
3216 gtk_label_update_layout_width (GtkLabel *label)
3218 GtkLabelPrivate *priv = label->priv;
3219 GtkWidget *widget = GTK_WIDGET (label);
3220 GtkAllocation allocation;
3222 g_assert (priv->layout);
3224 gtk_widget_get_allocation (widget, &allocation);
3226 if (priv->ellipsize)
3228 PangoRectangle logical;
3229 PangoRectangle bounds;
3232 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3234 bounds.x = bounds.y = 0;
3235 bounds.width = allocation.width - xpad * 2;
3236 bounds.height = allocation.height - ypad * 2;
3238 pango_layout_set_width (priv->layout, -1);
3239 pango_layout_get_pixel_extents (priv->layout, NULL, &logical);
3241 if (priv->have_transform)
3243 PangoContext *context = gtk_widget_get_pango_context (widget);
3244 const PangoMatrix *matrix = pango_context_get_matrix (context);
3246 const gdouble dx = matrix->xx; /* cos (M_PI * angle / 180) */
3247 const gdouble dy = matrix->xy; /* sin (M_PI * angle / 180) */
3248 if (fabs (dy) < 0.01)
3250 if (logical.width > bounds.width)
3251 pango_layout_set_width (priv->layout, bounds.width * PANGO_SCALE);
3253 else if (fabs (dx) < 0.01)
3255 if (logical.width > bounds.height)
3256 pango_layout_set_width (priv->layout, bounds.height * PANGO_SCALE);
3260 gdouble x0, y0, x1, y1, length;
3264 x0 = bounds.width / 2;
3265 y0 = dx ? x0 * dy / dx : G_MAXDOUBLE;
3266 vertical = fabs (y0) > bounds.height / 2;
3270 y0 = bounds.height/2;
3271 x0 = dy ? y0 * dx / dy : G_MAXDOUBLE;
3274 length = 2 * sqrt (x0 * x0 + y0 * y0);
3275 pango_layout_set_width (priv->layout, rint (length * PANGO_SCALE));
3276 pango_layout_get_pixel_size (priv->layout, NULL, &cy);
3283 y0 = bounds.height/2 + y1 - y0;
3288 x0 = bounds.width/2 + x1 - x0;
3292 length = length - sqrt (x0 * x0 + y0 * y0) * 2;
3293 pango_layout_set_width (priv->layout, rint (length * PANGO_SCALE));
3296 else if (logical.width > bounds.width)
3297 pango_layout_set_width (priv->layout, bounds.width * PANGO_SCALE);
3299 else if (priv->wrap)
3301 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (widget, FALSE);
3302 gdouble angle = gtk_label_get_angle (label);
3306 if ((angle == 90 || angle == 270) && aux_info && aux_info->height > 0)
3307 aux_width = aux_info->height;
3308 else if (aux_info && aux_info->width > 0)
3309 aux_width = aux_info->width;
3312 pango_layout_set_width (priv->layout, aux_width * PANGO_SCALE);
3316 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3318 if (angle == 90 || angle == 270)
3319 width = allocation.height - ypad * 2;
3321 width = allocation.width - xpad * 2;
3323 pango_layout_set_wrap (priv->layout, priv->wrap_mode);
3324 pango_layout_set_width (priv->layout, MAX (width, 1) * PANGO_SCALE);
3327 else /* !priv->wrap */
3328 pango_layout_set_width (priv->layout, -1);
3332 gtk_label_ensure_layout (GtkLabel *label)
3334 GtkLabelPrivate *priv = label->priv;
3338 widget = GTK_WIDGET (label);
3340 rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
3344 PangoAlignment align = PANGO_ALIGN_LEFT; /* Quiet gcc */
3345 gdouble angle = gtk_label_get_angle (label);
3347 if (angle != 0.0 && !priv->select_info)
3349 PangoMatrix matrix = PANGO_MATRIX_INIT;
3351 /* We rotate the standard singleton PangoContext for the widget,
3352 * depending on the fact that it's meant pretty much exclusively
3355 pango_matrix_rotate (&matrix, angle);
3357 pango_context_set_matrix (gtk_widget_get_pango_context (widget), &matrix);
3359 priv->have_transform = TRUE;
3363 if (priv->have_transform)
3364 pango_context_set_matrix (gtk_widget_get_pango_context (widget), NULL);
3366 priv->have_transform = FALSE;
3369 priv->layout = gtk_widget_create_pango_layout (widget, priv->text);
3371 if (priv->effective_attrs)
3372 pango_layout_set_attributes (priv->layout, priv->effective_attrs);
3374 gtk_label_rescan_links (label);
3376 switch (priv->jtype)
3378 case GTK_JUSTIFY_LEFT:
3379 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3381 case GTK_JUSTIFY_RIGHT:
3382 align = rtl ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
3384 case GTK_JUSTIFY_CENTER:
3385 align = PANGO_ALIGN_CENTER;
3387 case GTK_JUSTIFY_FILL:
3388 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3389 pango_layout_set_justify (priv->layout, TRUE);
3392 g_assert_not_reached();
3395 pango_layout_set_alignment (priv->layout, align);
3396 pango_layout_set_ellipsize (priv->layout, priv->ellipsize);
3397 pango_layout_set_single_paragraph_mode (priv->layout, priv->single_line_mode);
3399 gtk_label_update_layout_width (label);
3404 get_single_line_height (GtkWidget *widget,
3405 PangoLayout *layout)
3407 PangoContext *context;
3408 PangoFontMetrics *metrics;
3409 gint ascent, descent;
3411 context = pango_layout_get_context (layout);
3412 metrics = get_font_metrics (context, widget);
3413 ascent = pango_font_metrics_get_ascent (metrics);
3414 descent = pango_font_metrics_get_descent (metrics);
3415 pango_font_metrics_unref (metrics);
3417 return ascent + descent;
3420 static GtkSizeRequestMode
3421 gtk_label_get_request_mode (GtkWidget *widget)
3423 GtkLabel *label = GTK_LABEL (widget);
3424 gdouble angle = gtk_label_get_angle (label);
3426 if (label->priv->wrap)
3427 return (angle == 90 || angle == 270) ?
3428 GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT :
3429 GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
3431 return GTK_SIZE_REQUEST_CONSTANT_SIZE;
3435 get_size_for_allocation (GtkLabel *label,
3436 GtkOrientation orientation,
3441 PangoLayout *layout;
3442 GtkWidgetAuxInfo *aux_info =
3443 _gtk_widget_get_aux_info (GTK_WIDGET (label), FALSE);
3449 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3450 aux_size = aux_info->width;
3452 aux_size = aux_info->height;
3458 layout = gtk_label_get_measuring_layout (label, NULL, aux_size * PANGO_SCALE);
3460 layout = gtk_label_get_measuring_layout (label, NULL, allocation * PANGO_SCALE);
3462 pango_layout_get_pixel_size (layout, NULL, &text_height);
3465 *minimum_size = text_height;
3468 *natural_size = text_height;
3470 g_object_unref (layout);
3474 gtk_label_get_preferred_layout_size (GtkLabel *label,
3475 PangoRectangle *required,
3476 PangoRectangle *natural)
3478 GtkLabelPrivate *priv = label->priv;
3479 PangoLayout *layout;
3480 PangoRectangle rect;
3481 gint text_width, ellipsize_chars, guess_width;
3483 /* "width-chars" Hard-coded minimum width:
3484 * - minimum size should be MAX (width-chars, strlen ("..."));
3485 * - natural size should be MAX (width-chars, strlen (priv->text));
3487 * "max-width-chars" User specified maximum size requisition
3488 * - minimum size should be MAX (width-chars, 0)
3489 * - natural size should be MIN (max-width-chars, strlen (priv->text))
3493 /* When calculating ->wrap sometimes we need to invent a size; Ideally we should be doing
3494 * that stuff here instead of inside gtk_label_ensure_layout() */
3495 layout = gtk_label_get_layout_with_guessed_wrap_width (label);
3497 /* Start off with the pixel extents of the rendered layout */
3498 pango_layout_get_extents (layout, NULL, required);
3499 required->x = required->y = 0;
3501 if (priv->single_line_mode || priv->wrap)
3502 required->height = get_single_line_height (GTK_WIDGET (label), layout);
3504 *natural = *required;
3506 guess_width = required->width;
3508 layout = gtk_label_get_measuring_layout (label, layout, -1);
3510 /* Fetch the length of the complete unwrapped text */
3511 pango_layout_get_extents (layout, NULL, &rect);
3512 text_width = rect.width;
3514 /* enforce minimum width for ellipsized labels at ~3 chars */
3515 if (priv->ellipsize)
3516 ellipsize_chars = 3;
3518 ellipsize_chars = 0;
3520 /* "width-chars" Hard-coded minimum width:
3521 * - minimum size should be MAX (width-chars, strlen ("..."));
3522 * - natural size should be MAX (width-chars, strlen (priv->text));
3524 * "max-width-chars" User specified maximum size requisition
3525 * - minimum size should be MAX (width-chars, 0)
3526 * - natural size should be MIN (max-width-chars, strlen (priv->text))
3528 * For ellipsizing labels; if max-width-chars is specified: either it is used as
3529 * a minimum size or the label text as a minimum size (natural size still overflows).
3531 * For wrapping labels; A reasonable minimum size is useful to naturally layout
3532 * interfaces automatically. In this case if no "width-chars" is specified, the minimum
3533 * width will default to the wrap guess that gtk_label_ensure_layout() does.
3535 * In *any* case the minimum width is completely overridden if an explicit width
3536 * request was provided.
3539 if (priv->ellipsize || priv->wrap)
3541 PangoContext *context;
3542 PangoFontMetrics *metrics;
3543 gint char_width, digit_width, char_pixels;
3545 context = pango_layout_get_context (layout);
3546 metrics = get_font_metrics (context, GTK_WIDGET (label));
3547 char_width = pango_font_metrics_get_approximate_char_width (metrics);
3548 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
3549 char_pixels = MAX (char_width, digit_width);
3550 pango_font_metrics_unref (metrics);
3552 required->width = char_pixels * MAX (priv->width_chars, ellipsize_chars);
3554 /* Default to the minimum width regularly guessed by GTK+ if no minimum
3555 * width was specified, only allow unwrapping of these labels.
3557 * Note that when specifying a small width_chars for a long text;
3558 * an accordingly large size will be required for the label height.
3560 if (priv->wrap && priv->width_chars <= 0)
3561 required->width = guess_width;
3563 if (priv->max_width_chars < 0)
3565 natural->width = MAX (required->width, text_width);
3569 gint max_char_width = char_pixels * priv->max_width_chars;
3570 gint max_width = MIN (text_width, max_char_width);
3572 /* With max-char-width specified, we let the minimum widths of
3573 * ellipsized text crawl up to the max-char-width
3574 * (note that we dont want to limit the minimum width for wrapping text).
3576 if (priv->ellipsize)
3577 required->width = MIN (text_width, max_width);
3579 natural->width = MAX (required->width, max_width);
3584 required->width = text_width;
3585 natural->width = required->width;
3588 /* if a width-request is set, use that as the requested label width */
3589 if (priv->wrap || priv->ellipsize || priv->width_chars > 0 || priv->max_width_chars > 0)
3591 GtkWidgetAuxInfo *aux_info;
3593 aux_info = _gtk_widget_get_aux_info (GTK_WIDGET (label), FALSE);
3594 if (aux_info && aux_info->width > 0)
3596 required->width = aux_info->width * PANGO_SCALE;
3597 natural->width = MAX (natural->width, required->width);
3601 g_object_unref (layout);
3605 gtk_label_get_preferred_size (GtkWidget *widget,
3606 GtkOrientation orientation,
3610 GtkLabel *label = GTK_LABEL (widget);
3611 GtkLabelPrivate *priv = label->priv;
3612 PangoRectangle required_rect;
3613 PangoRectangle natural_rect;
3616 gtk_label_get_preferred_layout_size (label, &required_rect, &natural_rect);
3618 /* Now that we have minimum and natural sizes in pango extents, apply a possible transform */
3619 if (priv->have_transform)
3622 PangoContext *context;
3623 const PangoMatrix *matrix;
3625 copy = pango_layout_copy (priv->layout);
3626 context = pango_layout_get_context (copy);
3627 matrix = pango_context_get_matrix (context);
3629 pango_layout_set_width (copy, -1);
3630 pango_layout_set_ellipsize (copy, PANGO_ELLIPSIZE_NONE);
3632 pango_layout_get_extents (copy, NULL, &natural_rect);
3633 g_object_unref (copy);
3635 pango_matrix_transform_rectangle (matrix, &required_rect);
3636 pango_matrix_transform_rectangle (matrix, &natural_rect);
3638 /* Bump the natural size in case of ellipsize to ensure pango has
3639 * enough space in the angles (note, we could alternatively set the
3640 * layout to not ellipsize when we know we have been allocated our
3641 * full natural size, or it may be that pango needs a fix here).
3643 if (priv->ellipsize && priv->angle != 0 && priv->angle != 90 &&
3644 priv->angle != 180 && priv->angle != 270 && priv->angle != 360)
3646 /* For some reason we only need this at about 110 degrees, and only
3647 * when gaining in height
3649 natural_rect.height += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3650 natural_rect.width += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3654 required_rect.width = PANGO_PIXELS_CEIL (required_rect.width);
3655 required_rect.height = PANGO_PIXELS_CEIL (required_rect.height);
3657 natural_rect.width = PANGO_PIXELS_CEIL (natural_rect.width);
3658 natural_rect.height = PANGO_PIXELS_CEIL (natural_rect.height);
3660 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3662 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3664 /* Note, we cant use get_size_for_allocation() when rotating
3665 * ellipsized labels.
3667 if (!(priv->ellipsize && priv->have_transform) &&
3668 (priv->angle == 90 || priv->angle == 270))
3670 /* Doing a h4w request on a rotated label here, return the
3671 * required width for the minimum height.
3673 get_size_for_allocation (label,
3674 GTK_ORIENTATION_VERTICAL,
3675 required_rect.height,
3676 minimum_size, natural_size);
3681 /* Normal desired width */
3682 *minimum_size = required_rect.width;
3683 *natural_size = natural_rect.width;
3686 *minimum_size += xpad * 2;
3687 *natural_size += xpad * 2;
3689 else /* GTK_ORIENTATION_VERTICAL */
3691 /* Note, we cant use get_size_for_allocation() when rotating
3692 * ellipsized labels.
3694 if (!(priv->ellipsize && priv->have_transform) &&
3695 (priv->angle == 0 || priv->angle == 180 || priv->angle == 360))
3697 /* Doing a w4h request on a label here, return the required
3698 * height for the minimum width.
3700 get_size_for_allocation (label,
3701 GTK_ORIENTATION_HORIZONTAL,
3702 required_rect.width,
3703 minimum_size, natural_size);
3707 /* A vertically rotated label does w4h, so return the base
3708 * desired height (text length)
3710 *minimum_size = required_rect.height;
3711 *natural_size = natural_rect.height;
3714 *minimum_size += ypad * 2;
3715 *natural_size += ypad * 2;
3721 gtk_label_get_preferred_width (GtkWidget *widget,
3725 gtk_label_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
3729 gtk_label_get_preferred_height (GtkWidget *widget,
3733 gtk_label_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
3737 gtk_label_get_preferred_width_for_height (GtkWidget *widget,
3739 gint *minimum_width,
3740 gint *natural_width)
3742 GtkLabel *label = GTK_LABEL (widget);
3743 GtkLabelPrivate *priv = label->priv;
3745 if (priv->wrap && (priv->angle == 90 || priv->angle == 270))
3749 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3752 gtk_label_clear_layout (label);
3754 get_size_for_allocation (label, GTK_ORIENTATION_VERTICAL,
3755 MAX (1, height - (ypad * 2)),
3756 minimum_width, natural_width);
3759 *minimum_width += xpad * 2;
3762 *natural_width += xpad * 2;
3765 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_width, natural_width);
3769 gtk_label_get_preferred_height_for_width (GtkWidget *widget,
3771 gint *minimum_height,
3772 gint *natural_height)
3774 GtkLabel *label = GTK_LABEL (widget);
3775 GtkLabelPrivate *priv = label->priv;
3777 if (priv->wrap && (priv->angle == 0 || priv->angle == 180 || priv->angle == 360))
3781 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3784 gtk_label_clear_layout (label);
3786 get_size_for_allocation (label, GTK_ORIENTATION_HORIZONTAL,
3787 MAX (1, width - xpad * 2),
3788 minimum_height, natural_height);
3791 *minimum_height += ypad * 2;
3794 *natural_height += ypad * 2;
3797 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, minimum_height, natural_height);
3801 gtk_label_size_allocate (GtkWidget *widget,
3802 GtkAllocation *allocation)
3804 GtkLabel *label = GTK_LABEL (widget);
3805 GtkLabelPrivate *priv = label->priv;
3807 GTK_WIDGET_CLASS (gtk_label_parent_class)->size_allocate (widget, allocation);
3810 gtk_label_update_layout_width (label);
3812 if (priv->select_info && priv->select_info->window)
3814 gdk_window_move_resize (priv->select_info->window,
3818 allocation->height);
3823 gtk_label_update_cursor (GtkLabel *label)
3825 GtkLabelPrivate *priv = label->priv;
3828 if (!priv->select_info)
3831 widget = GTK_WIDGET (label);
3833 if (gtk_widget_get_realized (widget))
3835 GdkDisplay *display;
3838 if (gtk_widget_is_sensitive (widget))
3840 display = gtk_widget_get_display (widget);
3842 if (priv->select_info->active_link)
3843 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
3844 else if (priv->select_info->selectable)
3845 cursor = gdk_cursor_new_for_display (display, GDK_XTERM);
3852 gdk_window_set_cursor (priv->select_info->window, cursor);
3855 g_object_unref (cursor);
3860 gtk_label_state_changed (GtkWidget *widget,
3861 GtkStateType prev_state)
3863 GtkLabel *label = GTK_LABEL (widget);
3864 GtkLabelPrivate *priv = label->priv;
3866 if (priv->select_info)
3868 if (!gtk_widget_is_sensitive (widget))
3869 gtk_label_select_region (label, 0, 0);
3871 gtk_label_update_cursor (label);
3874 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed)
3875 GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed (widget, prev_state);
3879 gtk_label_style_updated (GtkWidget *widget)
3881 GtkLabel *label = GTK_LABEL (widget);
3883 GTK_WIDGET_CLASS (gtk_label_parent_class)->style_updated (widget);
3885 /* We have to clear the layout, fonts etc. may have changed */
3886 gtk_label_clear_layout (label);
3887 gtk_label_invalidate_wrap_width (label);
3891 gtk_label_direction_changed (GtkWidget *widget,
3892 GtkTextDirection previous_dir)
3894 GtkLabel *label = GTK_LABEL (widget);
3895 GtkLabelPrivate *priv = label->priv;
3898 pango_layout_context_changed (priv->layout);
3900 GTK_WIDGET_CLASS (gtk_label_parent_class)->direction_changed (widget, previous_dir);
3904 get_layout_location (GtkLabel *label,
3908 GtkAllocation allocation;
3911 GtkLabelPrivate *priv;
3912 gint req_width, x, y;
3915 gfloat xalign, yalign;
3916 PangoRectangle logical;
3918 misc = GTK_MISC (label);
3919 widget = GTK_WIDGET (label);
3922 gtk_misc_get_alignment (misc, &xalign, &yalign);
3923 gtk_misc_get_padding (misc, &xpad, &ypad);
3925 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
3926 xalign = 1.0 - xalign;
3928 pango_layout_get_extents (priv->layout, NULL, &logical);
3930 if (priv->have_transform)
3932 PangoContext *context = gtk_widget_get_pango_context (widget);
3933 const PangoMatrix *matrix = pango_context_get_matrix (context);
3934 pango_matrix_transform_rectangle (matrix, &logical);
3937 pango_extents_to_pixels (&logical, NULL);
3939 req_width = logical.width;
3940 req_height = logical.height;
3942 req_width += 2 * xpad;
3943 req_height += 2 * ypad;
3945 gtk_widget_get_allocation (widget, &allocation);
3947 x = floor (allocation.x + xpad + xalign * (allocation.width - req_width));
3949 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
3950 x = MAX (x, allocation.x + xpad);
3952 x = MIN (x, allocation.x + allocation.width - xpad);
3955 /* bgo#315462 - For single-line labels, *do* align the requisition with
3956 * respect to the allocation, even if we are under-allocated. For multi-line
3957 * labels, always show the top of the text when they are under-allocated. The
3958 * rationale is this:
3960 * - Single-line labels appear in GtkButtons, and it is very easy to get them
3961 * to be smaller than their requisition. The button may clip the label, but
3962 * the label will still be able to show most of itself and the focus
3963 * rectangle. Also, it is fairly easy to read a single line of clipped text.
3965 * - Multi-line labels should not be clipped to showing "something in the
3966 * middle". You want to read the first line, at least, to get some context.
3968 if (pango_layout_get_line_count (priv->layout) == 1)
3969 y = floor (allocation.y + ypad + (allocation.height - req_height) * yalign);
3971 y = floor (allocation.y + ypad + MAX ((allocation.height - req_height) * yalign, 0));
3981 draw_insertion_cursor (GtkLabel *label,
3983 GdkRectangle *cursor_location,
3984 gboolean is_primary,
3985 PangoDirection direction,
3986 gboolean draw_arrow)
3988 GtkWidget *widget = GTK_WIDGET (label);
3989 GtkTextDirection text_dir;
3991 if (direction == PANGO_DIRECTION_LTR)
3992 text_dir = GTK_TEXT_DIR_LTR;
3994 text_dir = GTK_TEXT_DIR_RTL;
3996 gtk_draw_insertion_cursor (widget, cr, cursor_location,
3997 is_primary, text_dir, draw_arrow);
4000 static PangoDirection
4001 get_cursor_direction (GtkLabel *label)
4003 GtkLabelPrivate *priv = label->priv;
4006 g_assert (priv->select_info);
4008 gtk_label_ensure_layout (label);
4010 for (l = pango_layout_get_lines_readonly (priv->layout); l; l = l->next)
4012 PangoLayoutLine *line = l->data;
4014 /* If priv->select_info->selection_end is at the very end of
4015 * the line, we don't know if the cursor is on this line or
4016 * the next without looking ahead at the next line. (End
4017 * of paragraph is different from line break.) But it's
4018 * definitely in this paragraph, which is good enough
4019 * to figure out the resolved direction.
4021 if (line->start_index + line->length >= priv->select_info->selection_end)
4022 return line->resolved_dir;
4025 return PANGO_DIRECTION_LTR;
4029 gtk_label_draw_cursor (GtkLabel *label, cairo_t *cr, gint xoffset, gint yoffset)
4031 GtkLabelPrivate *priv = label->priv;
4034 if (priv->select_info == NULL)
4037 widget = GTK_WIDGET (label);
4039 if (gtk_widget_is_drawable (widget))
4041 PangoDirection keymap_direction;
4042 PangoDirection cursor_direction;
4043 PangoRectangle strong_pos, weak_pos;
4044 gboolean split_cursor;
4045 PangoRectangle *cursor1 = NULL;
4046 PangoRectangle *cursor2 = NULL;
4047 GdkRectangle cursor_location;
4048 PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
4049 PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
4051 keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gtk_widget_get_display (widget)));
4052 cursor_direction = get_cursor_direction (label);
4054 gtk_label_ensure_layout (label);
4056 pango_layout_get_cursor_pos (priv->layout, priv->select_info->selection_end,
4057 &strong_pos, &weak_pos);
4059 g_object_get (gtk_widget_get_settings (widget),
4060 "gtk-split-cursor", &split_cursor,
4063 dir1 = cursor_direction;
4067 cursor1 = &strong_pos;
4069 if (strong_pos.x != weak_pos.x ||
4070 strong_pos.y != weak_pos.y)
4072 dir2 = (cursor_direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
4073 cursor2 = &weak_pos;
4078 if (keymap_direction == cursor_direction)
4079 cursor1 = &strong_pos;
4081 cursor1 = &weak_pos;
4084 cursor_location.x = xoffset + PANGO_PIXELS (cursor1->x);
4085 cursor_location.y = yoffset + PANGO_PIXELS (cursor1->y);
4086 cursor_location.width = 0;
4087 cursor_location.height = PANGO_PIXELS (cursor1->height);
4089 draw_insertion_cursor (label, cr,
4090 &cursor_location, TRUE, dir1,
4091 dir2 != PANGO_DIRECTION_NEUTRAL);
4093 if (dir2 != PANGO_DIRECTION_NEUTRAL)
4095 cursor_location.x = xoffset + PANGO_PIXELS (cursor2->x);
4096 cursor_location.y = yoffset + PANGO_PIXELS (cursor2->y);
4097 cursor_location.width = 0;
4098 cursor_location.height = PANGO_PIXELS (cursor2->height);
4100 draw_insertion_cursor (label, cr,
4101 &cursor_location, FALSE, dir2,
4107 static GtkLabelLink *
4108 gtk_label_get_focus_link (GtkLabel *label)
4110 GtkLabelPrivate *priv = label->priv;
4111 GtkLabelSelectionInfo *info = priv->select_info;
4117 if (info->selection_anchor != info->selection_end)
4120 for (l = info->links; l; l = l->next)
4122 GtkLabelLink *link = l->data;
4123 if (link->start <= info->selection_anchor &&
4124 info->selection_anchor <= link->end)
4132 gtk_label_draw (GtkWidget *widget,
4135 GtkLabel *label = GTK_LABEL (widget);
4136 GtkLabelPrivate *priv = label->priv;
4137 GtkLabelSelectionInfo *info = priv->select_info;
4138 GtkAllocation allocation;
4139 GtkStyleContext *context;
4140 GtkStateFlags state;
4143 gtk_label_ensure_layout (label);
4145 if (priv->text && (*priv->text != '\0'))
4147 GdkRGBA *bg_color, *fg_color;
4149 get_layout_location (label, &x, &y);
4151 context = gtk_widget_get_style_context (widget);
4152 gtk_widget_get_allocation (widget, &allocation);
4154 cairo_translate (cr, -allocation.x, -allocation.y);
4156 state = gtk_widget_get_state_flags (widget);
4157 gtk_style_context_set_state (context, state);
4159 gtk_render_layout (context, cr,
4164 (info->selection_anchor != info->selection_end))
4167 cairo_region_t *clip;
4169 range[0] = info->selection_anchor;
4170 range[1] = info->selection_end;
4172 if (range[0] > range[1])
4174 gint tmp = range[0];
4175 range[0] = range[1];
4179 clip = gdk_pango_layout_get_clip_region (priv->layout,
4184 /* FIXME should use gtk_paint, but it can't use a clip region */
4187 gdk_cairo_region (cr, clip);
4190 state = GTK_STATE_FLAG_SELECTED;
4192 if (gtk_widget_has_focus (widget))
4193 state |= GTK_STATE_FLAG_FOCUSED;
4195 gtk_style_context_get (context, state,
4196 "background-color", &bg_color,
4200 gdk_cairo_set_source_rgba (cr, bg_color);
4203 gdk_cairo_set_source_rgba (cr, fg_color);
4204 cairo_move_to (cr, x, y);
4205 _gtk_pango_fill_layout (cr, priv->layout);
4208 cairo_region_destroy (clip);
4210 gdk_rgba_free (bg_color);
4211 gdk_rgba_free (fg_color);
4215 GtkLabelLink *focus_link;
4216 GtkLabelLink *active_link;
4218 cairo_region_t *clip;
4220 GdkColor *text_color;
4221 GdkColor *link_color;
4222 GdkColor *visited_link_color;
4224 if (info->selectable && gtk_widget_has_focus (widget))
4225 gtk_label_draw_cursor (label, cr, x, y);
4227 focus_link = gtk_label_get_focus_link (label);
4228 active_link = info->active_link;
4235 range[0] = active_link->start;
4236 range[1] = active_link->end;
4240 clip = gdk_pango_layout_get_clip_region (priv->layout,
4244 gdk_cairo_region (cr, clip);
4246 cairo_region_destroy (clip);
4248 gtk_label_get_link_colors (widget, &link_color, &visited_link_color);
4249 if (active_link->visited)
4250 text_color = visited_link_color;
4252 text_color = link_color;
4254 if (info->link_clicked)
4255 state = GTK_STATE_FLAG_ACTIVE;
4257 state = GTK_STATE_FLAG_PRELIGHT;
4259 gtk_style_context_get (context, state,
4260 "background-color", &bg_color,
4263 gdk_cairo_set_source_rgba (cr, bg_color);
4266 gdk_cairo_set_source_color (cr, text_color);
4267 cairo_move_to (cr, x, y);
4268 _gtk_pango_fill_layout (cr, priv->layout);
4270 gdk_color_free (link_color);
4271 gdk_color_free (visited_link_color);
4272 gdk_rgba_free (bg_color);
4277 if (focus_link && gtk_widget_has_focus (widget))
4279 range[0] = focus_link->start;
4280 range[1] = focus_link->end;
4282 clip = gdk_pango_layout_get_clip_region (priv->layout,
4286 cairo_region_get_extents (clip, &rect);
4288 state = gtk_widget_get_state_flags (widget);
4289 gtk_style_context_set_state (context, state);
4291 gtk_render_focus (context, cr,
4293 rect.width, rect.height);
4295 cairo_region_destroy (clip);
4304 separate_uline_pattern (const gchar *str,
4309 gboolean underscore;
4312 gchar *pattern_dest;
4314 *accel_key = GDK_KEY_VoidSymbol;
4315 *new_str = g_new (gchar, strlen (str) + 1);
4316 *pattern = g_new (gchar, g_utf8_strlen (str, -1) + 1);
4322 pattern_dest = *pattern;
4327 const gchar *next_src;
4329 c = g_utf8_get_char (src);
4330 if (c == (gunichar)-1)
4332 g_warning ("Invalid input string");
4338 next_src = g_utf8_next_char (src);
4343 *pattern_dest++ = ' ';
4346 *pattern_dest++ = '_';
4347 if (*accel_key == GDK_KEY_VoidSymbol)
4348 *accel_key = gdk_keyval_to_lower (gdk_unicode_to_keyval (c));
4351 while (src < next_src)
4365 while (src < next_src)
4368 *pattern_dest++ = ' ';
4380 gtk_label_set_uline_text_internal (GtkLabel *label,
4383 GtkLabelPrivate *priv = label->priv;
4384 guint accel_key = GDK_KEY_VoidSymbol;
4388 g_return_if_fail (GTK_IS_LABEL (label));
4389 g_return_if_fail (str != NULL);
4391 /* Split text into the base text and a separate pattern
4394 if (!separate_uline_pattern (str, &accel_key, &new_str, &pattern))
4397 gtk_label_set_text_internal (label, new_str);
4398 gtk_label_set_pattern_internal (label, pattern, TRUE);
4399 priv->mnemonic_keyval = accel_key;
4405 * gtk_label_set_text_with_mnemonic:
4406 * @label: a #GtkLabel
4409 * Sets the label's text from the string @str.
4410 * If characters in @str are preceded by an underscore, they are underlined
4411 * indicating that they represent a keyboard accelerator called a mnemonic.
4412 * The mnemonic key can be used to activate another widget, chosen
4413 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
4416 gtk_label_set_text_with_mnemonic (GtkLabel *label,
4419 g_return_if_fail (GTK_IS_LABEL (label));
4420 g_return_if_fail (str != NULL);
4422 g_object_freeze_notify (G_OBJECT (label));
4424 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
4425 gtk_label_set_use_markup_internal (label, FALSE);
4426 gtk_label_set_use_underline_internal (label, TRUE);
4428 gtk_label_recalculate (label);
4430 g_object_thaw_notify (G_OBJECT (label));
4434 gtk_label_realize (GtkWidget *widget)
4436 GtkLabel *label = GTK_LABEL (widget);
4437 GtkLabelPrivate *priv = label->priv;
4439 GTK_WIDGET_CLASS (gtk_label_parent_class)->realize (widget);
4441 if (priv->select_info)
4442 gtk_label_create_window (label);
4446 gtk_label_unrealize (GtkWidget *widget)
4448 GtkLabel *label = GTK_LABEL (widget);
4449 GtkLabelPrivate *priv = label->priv;
4451 if (priv->select_info)
4452 gtk_label_destroy_window (label);
4454 GTK_WIDGET_CLASS (gtk_label_parent_class)->unrealize (widget);
4458 gtk_label_map (GtkWidget *widget)
4460 GtkLabel *label = GTK_LABEL (widget);
4461 GtkLabelPrivate *priv = label->priv;
4463 GTK_WIDGET_CLASS (gtk_label_parent_class)->map (widget);
4465 if (priv->select_info)
4466 gdk_window_show (priv->select_info->window);
4470 gtk_label_unmap (GtkWidget *widget)
4472 GtkLabel *label = GTK_LABEL (widget);
4473 GtkLabelPrivate *priv = label->priv;
4475 if (priv->select_info)
4476 gdk_window_hide (priv->select_info->window);
4478 GTK_WIDGET_CLASS (gtk_label_parent_class)->unmap (widget);
4482 window_to_layout_coords (GtkLabel *label,
4486 GtkAllocation allocation;
4490 widget = GTK_WIDGET (label);
4492 /* get layout location in widget->window coords */
4493 get_layout_location (label, &lx, &ly);
4495 gtk_widget_get_allocation (widget, &allocation);
4499 *x += allocation.x; /* go to widget->window */
4500 *x -= lx; /* go to layout */
4505 *y += allocation.y; /* go to widget->window */
4506 *y -= ly; /* go to layout */
4512 layout_to_window_coords (GtkLabel *label,
4519 widget = GTK_WIDGET (label);
4521 /* get layout location in widget->window coords */
4522 get_layout_location (label, &lx, &ly);
4526 *x += lx; /* go to widget->window */
4527 *x -= widget->allocation.x; /* go to selection window */
4532 *y += ly; /* go to widget->window */
4533 *y -= widget->allocation.y; /* go to selection window */
4539 get_layout_index (GtkLabel *label,
4544 GtkLabelPrivate *priv = label->priv;
4546 const gchar *cluster;
4547 const gchar *cluster_end;
4552 gtk_label_ensure_layout (label);
4554 window_to_layout_coords (label, &x, &y);
4559 inside = pango_layout_xy_to_index (priv->layout,
4563 cluster = priv->text + *index;
4564 cluster_end = cluster;
4567 cluster_end = g_utf8_next_char (cluster_end);
4571 *index += (cluster_end - cluster);
4577 gtk_label_select_word (GtkLabel *label)
4579 GtkLabelPrivate *priv = label->priv;
4582 gint start_index = gtk_label_move_backward_word (label, priv->select_info->selection_end);
4583 gint end_index = gtk_label_move_forward_word (label, priv->select_info->selection_end);
4585 min = MIN (priv->select_info->selection_anchor,
4586 priv->select_info->selection_end);
4587 max = MAX (priv->select_info->selection_anchor,
4588 priv->select_info->selection_end);
4590 min = MIN (min, start_index);
4591 max = MAX (max, end_index);
4593 gtk_label_select_region_index (label, min, max);
4597 gtk_label_grab_focus (GtkWidget *widget)
4599 GtkLabel *label = GTK_LABEL (widget);
4600 GtkLabelPrivate *priv = label->priv;
4601 gboolean select_on_focus;
4604 if (priv->select_info == NULL)
4607 GTK_WIDGET_CLASS (gtk_label_parent_class)->grab_focus (widget);
4609 if (priv->select_info->selectable)
4611 g_object_get (gtk_widget_get_settings (widget),
4612 "gtk-label-select-on-focus",
4616 if (select_on_focus && !priv->in_click)
4617 gtk_label_select_region (label, 0, -1);
4621 if (priv->select_info->links && !priv->in_click)
4623 link = priv->select_info->links->data;
4624 priv->select_info->selection_anchor = link->start;
4625 priv->select_info->selection_end = link->start;
4631 gtk_label_focus (GtkWidget *widget,
4632 GtkDirectionType direction)
4634 GtkLabel *label = GTK_LABEL (widget);
4635 GtkLabelPrivate *priv = label->priv;
4636 GtkLabelSelectionInfo *info = priv->select_info;
4637 GtkLabelLink *focus_link;
4640 if (!gtk_widget_is_focus (widget))
4642 gtk_widget_grab_focus (widget);
4645 focus_link = gtk_label_get_focus_link (label);
4646 if (focus_link && direction == GTK_DIR_TAB_BACKWARD)
4648 l = g_list_last (info->links);
4649 focus_link = l->data;
4650 info->selection_anchor = focus_link->start;
4651 info->selection_end = focus_link->start;
4661 if (info->selectable)
4665 if (info->selection_anchor != info->selection_end)
4668 index = info->selection_anchor;
4670 if (direction == GTK_DIR_TAB_FORWARD)
4671 for (l = info->links; l; l = l->next)
4673 GtkLabelLink *link = l->data;
4675 if (link->start > index)
4677 gtk_label_select_region_index (label, link->start, link->start);
4681 else if (direction == GTK_DIR_TAB_BACKWARD)
4682 for (l = g_list_last (info->links); l; l = l->prev)
4684 GtkLabelLink *link = l->data;
4686 if (link->end < index)
4688 gtk_label_select_region_index (label, link->start, link->start);
4697 focus_link = gtk_label_get_focus_link (label);
4700 case GTK_DIR_TAB_FORWARD:
4703 l = g_list_find (info->links, focus_link);
4710 case GTK_DIR_TAB_BACKWARD:
4713 l = g_list_find (info->links, focus_link);
4717 l = g_list_last (info->links);
4726 focus_link = l->data;
4727 info->selection_anchor = focus_link->start;
4728 info->selection_end = focus_link->start;
4729 gtk_widget_queue_draw (widget);
4741 gtk_label_button_press (GtkWidget *widget,
4742 GdkEventButton *event)
4744 GtkLabel *label = GTK_LABEL (widget);
4745 GtkLabelPrivate *priv = label->priv;
4746 GtkLabelSelectionInfo *info = priv->select_info;
4753 if (info->active_link)
4755 if (event->button == 1)
4757 info->link_clicked = 1;
4758 gtk_widget_queue_draw (widget);
4760 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4762 info->link_clicked = 1;
4763 gtk_label_do_popup (label, event);
4768 if (!info->selectable)
4771 info->in_drag = FALSE;
4772 info->select_words = FALSE;
4774 if (event->button == 1)
4776 if (!gtk_widget_has_focus (widget))
4778 priv->in_click = TRUE;
4779 gtk_widget_grab_focus (widget);
4780 priv->in_click = FALSE;
4783 if (event->type == GDK_3BUTTON_PRESS)
4785 gtk_label_select_region_index (label, 0, strlen (priv->text));
4789 if (event->type == GDK_2BUTTON_PRESS)
4791 info->select_words = TRUE;
4792 gtk_label_select_word (label);
4796 get_layout_index (label, event->x, event->y, &index);
4798 min = MIN (info->selection_anchor, info->selection_end);
4799 max = MAX (info->selection_anchor, info->selection_end);
4801 if ((info->selection_anchor != info->selection_end) &&
4802 (event->state & GDK_SHIFT_MASK))
4804 /* extend (same as motion) */
4805 min = MIN (min, index);
4806 max = MAX (max, index);
4808 /* ensure the anchor is opposite index */
4816 gtk_label_select_region_index (label, min, max);
4820 if (event->type == GDK_3BUTTON_PRESS)
4821 gtk_label_select_region_index (label, 0, strlen (priv->text));
4822 else if (event->type == GDK_2BUTTON_PRESS)
4823 gtk_label_select_word (label);
4824 else if (min < max && min <= index && index <= max)
4826 info->in_drag = TRUE;
4827 info->drag_start_x = event->x;
4828 info->drag_start_y = event->y;
4831 /* start a replacement */
4832 gtk_label_select_region_index (label, index, index);
4837 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4839 gtk_label_do_popup (label, event);
4847 gtk_label_button_release (GtkWidget *widget,
4848 GdkEventButton *event)
4851 GtkLabel *label = GTK_LABEL (widget);
4852 GtkLabelPrivate *priv = label->priv;
4853 GtkLabelSelectionInfo *info = priv->select_info;
4863 get_layout_index (label, event->x, event->y, &index);
4864 gtk_label_select_region_index (label, index, index);
4869 if (event->button != 1)
4872 if (info->active_link &&
4873 info->selection_anchor == info->selection_end &&
4876 emit_activate_link (label, info->active_link);
4877 info->link_clicked = 0;
4882 /* The goal here is to return TRUE iff we ate the
4883 * button press to start selecting.
4889 connect_mnemonics_visible_notify (GtkLabel *label)
4891 GtkLabelPrivate *priv = label->priv;
4892 GtkWidget *toplevel;
4895 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
4897 if (!GTK_IS_WINDOW (toplevel))
4900 /* always set up this widgets initial value */
4901 priv->mnemonics_visible =
4902 gtk_window_get_mnemonics_visible (GTK_WINDOW (toplevel));
4905 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (toplevel),
4906 "gtk-label-mnemonics-visible-connected"));
4910 g_signal_connect (toplevel,
4911 "notify::mnemonics-visible",
4912 G_CALLBACK (label_mnemonics_visible_changed),
4914 g_object_set_data (G_OBJECT (toplevel),
4915 "gtk-label-mnemonics-visible-connected",
4916 GINT_TO_POINTER (1));
4921 drag_begin_cb (GtkWidget *widget,
4922 GdkDragContext *context,
4925 GtkLabel *label = GTK_LABEL (widget);
4926 GtkLabelPrivate *priv = label->priv;
4927 cairo_surface_t *surface = NULL;
4929 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
4931 if ((priv->select_info->selection_anchor !=
4932 priv->select_info->selection_end) &&
4938 start = MIN (priv->select_info->selection_anchor,
4939 priv->select_info->selection_end);
4940 end = MAX (priv->select_info->selection_anchor,
4941 priv->select_info->selection_end);
4943 len = strlen (priv->text);
4951 surface = _gtk_text_util_create_drag_icon (widget,
4958 gtk_drag_set_icon_surface (context, surface);
4959 cairo_surface_destroy (surface);
4963 gtk_drag_set_icon_default (context);
4968 gtk_label_motion (GtkWidget *widget,
4969 GdkEventMotion *event)
4971 GtkLabel *label = GTK_LABEL (widget);
4972 GtkLabelPrivate *priv = label->priv;
4973 GtkLabelSelectionInfo *info = priv->select_info;
4979 if (info->links && !info->in_drag)
4983 gboolean found = FALSE;
4985 if (info->selection_anchor == info->selection_end)
4987 if (get_layout_index (label, event->x, event->y, &index))
4989 for (l = info->links; l != NULL; l = l->next)
4992 if (index >= link->start && index <= link->end)
5003 if (info->active_link != link)
5005 info->link_clicked = 0;
5006 info->active_link = link;
5007 gtk_label_update_cursor (label);
5008 gtk_widget_queue_draw (widget);
5013 if (info->active_link != NULL)
5015 info->link_clicked = 0;
5016 info->active_link = NULL;
5017 gtk_label_update_cursor (label);
5018 gtk_widget_queue_draw (widget);
5023 if (!info->selectable)
5026 if ((event->state & GDK_BUTTON1_MASK) == 0)
5031 if (gtk_drag_check_threshold (widget,
5034 event->x, event->y))
5036 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
5038 gtk_target_list_add_text_targets (target_list, 0);
5040 g_signal_connect (widget, "drag-begin",
5041 G_CALLBACK (drag_begin_cb), NULL);
5042 gtk_drag_begin (widget, target_list,
5044 1, (GdkEvent *)event);
5046 info->in_drag = FALSE;
5048 gtk_target_list_unref (target_list);
5055 gdk_window_get_device_position (info->window, event->device, &x, &y, NULL);
5056 get_layout_index (label, x, y, &index);
5058 if (info->select_words)
5061 gint old_min, old_max;
5064 min = gtk_label_move_backward_word (label, index);
5065 max = gtk_label_move_forward_word (label, index);
5067 anchor = info->selection_anchor;
5068 end = info->selection_end;
5070 old_min = MIN (anchor, end);
5071 old_max = MAX (anchor, end);
5078 else if (old_max < max)
5083 else if (anchor == old_min)
5094 gtk_label_select_region_index (label, anchor, end);
5097 gtk_label_select_region_index (label, info->selection_anchor, index);
5104 gtk_label_leave_notify (GtkWidget *widget,
5105 GdkEventCrossing *event)
5107 GtkLabel *label = GTK_LABEL (widget);
5108 GtkLabelPrivate *priv = label->priv;
5110 if (priv->select_info)
5112 priv->select_info->active_link = NULL;
5113 gtk_label_update_cursor (label);
5114 gtk_widget_queue_draw (widget);
5117 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event)
5118 return GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event (widget, event);
5124 gtk_label_create_window (GtkLabel *label)
5126 GtkLabelPrivate *priv = label->priv;
5127 GtkAllocation allocation;
5129 GdkWindowAttr attributes;
5130 gint attributes_mask;
5132 g_assert (priv->select_info);
5133 widget = GTK_WIDGET (label);
5134 g_assert (gtk_widget_get_realized (widget));
5136 if (priv->select_info->window)
5139 gtk_widget_get_allocation (widget, &allocation);
5141 attributes.x = allocation.x;
5142 attributes.y = allocation.y;
5143 attributes.width = allocation.width;
5144 attributes.height = allocation.height;
5145 attributes.window_type = GDK_WINDOW_CHILD;
5146 attributes.wclass = GDK_INPUT_ONLY;
5147 attributes.override_redirect = TRUE;
5148 attributes.event_mask = gtk_widget_get_events (widget) |
5149 GDK_BUTTON_PRESS_MASK |
5150 GDK_BUTTON_RELEASE_MASK |
5151 GDK_LEAVE_NOTIFY_MASK |
5152 GDK_BUTTON_MOTION_MASK |
5153 GDK_POINTER_MOTION_MASK |
5154 GDK_POINTER_MOTION_HINT_MASK;
5155 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
5156 if (gtk_widget_is_sensitive (widget))
5158 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
5160 attributes_mask |= GDK_WA_CURSOR;
5164 priv->select_info->window = gdk_window_new (gtk_widget_get_window (widget),
5165 &attributes, attributes_mask);
5166 gdk_window_set_user_data (priv->select_info->window, widget);
5168 if (attributes_mask & GDK_WA_CURSOR)
5169 g_object_unref (attributes.cursor);
5173 gtk_label_destroy_window (GtkLabel *label)
5175 GtkLabelPrivate *priv = label->priv;
5177 g_assert (priv->select_info);
5179 if (priv->select_info->window == NULL)
5182 gdk_window_set_user_data (priv->select_info->window, NULL);
5183 gdk_window_destroy (priv->select_info->window);
5184 priv->select_info->window = NULL;
5188 gtk_label_ensure_select_info (GtkLabel *label)
5190 GtkLabelPrivate *priv = label->priv;
5192 if (priv->select_info == NULL)
5194 priv->select_info = g_new0 (GtkLabelSelectionInfo, 1);
5196 gtk_widget_set_can_focus (GTK_WIDGET (label), TRUE);
5198 if (gtk_widget_get_realized (GTK_WIDGET (label)))
5199 gtk_label_create_window (label);
5201 if (gtk_widget_get_mapped (GTK_WIDGET (label)))
5202 gdk_window_show (priv->select_info->window);
5207 gtk_label_clear_select_info (GtkLabel *label)
5209 GtkLabelPrivate *priv = label->priv;
5211 if (priv->select_info == NULL)
5214 if (!priv->select_info->selectable && !priv->select_info->links)
5216 gtk_label_destroy_window (label);
5218 g_free (priv->select_info);
5219 priv->select_info = NULL;
5221 gtk_widget_set_can_focus (GTK_WIDGET (label), FALSE);
5226 * gtk_label_set_selectable:
5227 * @label: a #GtkLabel
5228 * @setting: %TRUE to allow selecting text in the label
5230 * Selectable labels allow the user to select text from the label, for
5234 gtk_label_set_selectable (GtkLabel *label,
5237 GtkLabelPrivate *priv;
5238 gboolean old_setting;
5240 g_return_if_fail (GTK_IS_LABEL (label));
5244 setting = setting != FALSE;
5245 old_setting = priv->select_info && priv->select_info->selectable;
5249 gtk_label_ensure_select_info (label);
5250 priv->select_info->selectable = TRUE;
5251 gtk_label_update_cursor (label);
5257 /* unselect, to give up the selection */
5258 gtk_label_select_region (label, 0, 0);
5260 priv->select_info->selectable = FALSE;
5261 gtk_label_clear_select_info (label);
5262 gtk_label_update_cursor (label);
5265 if (setting != old_setting)
5267 g_object_freeze_notify (G_OBJECT (label));
5268 g_object_notify (G_OBJECT (label), "selectable");
5269 g_object_notify (G_OBJECT (label), "cursor-position");
5270 g_object_notify (G_OBJECT (label), "selection-bound");
5271 g_object_thaw_notify (G_OBJECT (label));
5272 gtk_widget_queue_draw (GTK_WIDGET (label));
5277 * gtk_label_get_selectable:
5278 * @label: a #GtkLabel
5280 * Gets the value set by gtk_label_set_selectable().
5282 * Return value: %TRUE if the user can copy text from the label
5285 gtk_label_get_selectable (GtkLabel *label)
5287 GtkLabelPrivate *priv;
5289 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5293 return priv->select_info && priv->select_info->selectable;
5297 * gtk_label_set_angle:
5298 * @label: a #GtkLabel
5299 * @angle: the angle that the baseline of the label makes with
5300 * the horizontal, in degrees, measured counterclockwise
5302 * Sets the angle of rotation for the label. An angle of 90 reads from
5303 * from bottom to top, an angle of 270, from top to bottom. The angle
5304 * setting for the label is ignored if the label is selectable,
5305 * wrapped, or ellipsized.
5310 gtk_label_set_angle (GtkLabel *label,
5313 GtkLabelPrivate *priv;
5315 g_return_if_fail (GTK_IS_LABEL (label));
5319 /* Canonicalize to [0,360]. We don't canonicalize 360 to 0, because
5320 * double property ranges are inclusive, and changing 360 to 0 would
5321 * make a property editor behave strangely.
5323 if (angle < 0 || angle > 360.0)
5324 angle = angle - 360. * floor (angle / 360.);
5326 if (priv->angle != angle)
5328 priv->angle = angle;
5330 gtk_label_clear_layout (label);
5331 gtk_widget_queue_resize (GTK_WIDGET (label));
5333 g_object_notify (G_OBJECT (label), "angle");
5338 * gtk_label_get_angle:
5339 * @label: a #GtkLabel
5341 * Gets the angle of rotation for the label. See
5342 * gtk_label_set_angle().
5344 * Return value: the angle of rotation for the label
5349 gtk_label_get_angle (GtkLabel *label)
5351 g_return_val_if_fail (GTK_IS_LABEL (label), 0.0);
5353 return label->priv->angle;
5357 gtk_label_set_selection_text (GtkLabel *label,
5358 GtkSelectionData *selection_data)
5360 GtkLabelPrivate *priv = label->priv;
5362 if ((priv->select_info->selection_anchor !=
5363 priv->select_info->selection_end) &&
5369 start = MIN (priv->select_info->selection_anchor,
5370 priv->select_info->selection_end);
5371 end = MAX (priv->select_info->selection_anchor,
5372 priv->select_info->selection_end);
5374 len = strlen (priv->text);
5382 gtk_selection_data_set_text (selection_data,
5389 gtk_label_drag_data_get (GtkWidget *widget,
5390 GdkDragContext *context,
5391 GtkSelectionData *selection_data,
5395 gtk_label_set_selection_text (GTK_LABEL (widget), selection_data);
5399 get_text_callback (GtkClipboard *clipboard,
5400 GtkSelectionData *selection_data,
5402 gpointer user_data_or_owner)
5404 gtk_label_set_selection_text (GTK_LABEL (user_data_or_owner), selection_data);
5408 clear_text_callback (GtkClipboard *clipboard,
5409 gpointer user_data_or_owner)
5412 GtkLabelPrivate *priv;
5414 label = GTK_LABEL (user_data_or_owner);
5417 if (priv->select_info)
5419 priv->select_info->selection_anchor = priv->select_info->selection_end;
5421 gtk_widget_queue_draw (GTK_WIDGET (label));
5426 gtk_label_select_region_index (GtkLabel *label,
5430 GtkLabelPrivate *priv;
5432 g_return_if_fail (GTK_IS_LABEL (label));
5436 if (priv->select_info && priv->select_info->selectable)
5438 GtkClipboard *clipboard;
5440 if (priv->select_info->selection_anchor == anchor_index &&
5441 priv->select_info->selection_end == end_index)
5444 priv->select_info->selection_anchor = anchor_index;
5445 priv->select_info->selection_end = end_index;
5447 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label),
5448 GDK_SELECTION_PRIMARY);
5450 if (anchor_index != end_index)
5452 GtkTargetList *list;
5453 GtkTargetEntry *targets;
5456 list = gtk_target_list_new (NULL, 0);
5457 gtk_target_list_add_text_targets (list, 0);
5458 targets = gtk_target_table_new_from_list (list, &n_targets);
5460 gtk_clipboard_set_with_owner (clipboard,
5463 clear_text_callback,
5466 gtk_target_table_free (targets, n_targets);
5467 gtk_target_list_unref (list);
5471 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (label))
5472 gtk_clipboard_clear (clipboard);
5475 gtk_widget_queue_draw (GTK_WIDGET (label));
5477 g_object_freeze_notify (G_OBJECT (label));
5478 g_object_notify (G_OBJECT (label), "cursor-position");
5479 g_object_notify (G_OBJECT (label), "selection-bound");
5480 g_object_thaw_notify (G_OBJECT (label));
5485 * gtk_label_select_region:
5486 * @label: a #GtkLabel
5487 * @start_offset: start offset (in characters not bytes)
5488 * @end_offset: end offset (in characters not bytes)
5490 * Selects a range of characters in the label, if the label is selectable.
5491 * See gtk_label_set_selectable(). If the label is not selectable,
5492 * this function has no effect. If @start_offset or
5493 * @end_offset are -1, then the end of the label will be substituted.
5496 gtk_label_select_region (GtkLabel *label,
5500 GtkLabelPrivate *priv;
5502 g_return_if_fail (GTK_IS_LABEL (label));
5506 if (priv->text && priv->select_info)
5508 if (start_offset < 0)
5509 start_offset = g_utf8_strlen (priv->text, -1);
5512 end_offset = g_utf8_strlen (priv->text, -1);
5514 gtk_label_select_region_index (label,
5515 g_utf8_offset_to_pointer (priv->text, start_offset) - priv->text,
5516 g_utf8_offset_to_pointer (priv->text, end_offset) - priv->text);
5521 * gtk_label_get_selection_bounds:
5522 * @label: a #GtkLabel
5523 * @start: (out): return location for start of selection, as a character offset
5524 * @end: (out): return location for end of selection, as a character offset
5526 * Gets the selected range of characters in the label, returning %TRUE
5527 * if there's a selection.
5529 * Return value: %TRUE if selection is non-empty
5532 gtk_label_get_selection_bounds (GtkLabel *label,
5536 GtkLabelPrivate *priv;
5538 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5542 if (priv->select_info == NULL)
5544 /* not a selectable label */
5554 gint start_index, end_index;
5555 gint start_offset, end_offset;
5558 start_index = MIN (priv->select_info->selection_anchor,
5559 priv->select_info->selection_end);
5560 end_index = MAX (priv->select_info->selection_anchor,
5561 priv->select_info->selection_end);
5563 len = strlen (priv->text);
5565 if (end_index > len)
5568 if (start_index > len)
5571 start_offset = g_utf8_strlen (priv->text, start_index);
5572 end_offset = g_utf8_strlen (priv->text, end_index);
5574 if (start_offset > end_offset)
5576 gint tmp = start_offset;
5577 start_offset = end_offset;
5582 *start = start_offset;
5587 return start_offset != end_offset;
5593 * gtk_label_get_layout:
5594 * @label: a #GtkLabel
5596 * Gets the #PangoLayout used to display the label.
5597 * The layout is useful to e.g. convert text positions to
5598 * pixel positions, in combination with gtk_label_get_layout_offsets().
5599 * The returned layout is owned by the @label so need not be
5600 * freed by the caller. The @label is free to recreate its layout at
5601 * any time, so it should be considered read-only.
5603 * Return value: (transfer none): the #PangoLayout for this label
5606 gtk_label_get_layout (GtkLabel *label)
5608 GtkLabelPrivate *priv;
5610 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
5614 gtk_label_ensure_layout (label);
5616 return priv->layout;
5620 * gtk_label_get_layout_offsets:
5621 * @label: a #GtkLabel
5622 * @x: (out) (allow-none): location to store X offset of layout, or %NULL
5623 * @y: (out) (allow-none): location to store Y offset of layout, or %NULL
5625 * Obtains the coordinates where the label will draw the #PangoLayout
5626 * representing the text in the label; useful to convert mouse events
5627 * into coordinates inside the #PangoLayout, e.g. to take some action
5628 * if some part of the label is clicked. Of course you will need to
5629 * create a #GtkEventBox to receive the events, and pack the label
5630 * inside it, since labels are a #GTK_NO_WINDOW widget. Remember
5631 * when using the #PangoLayout functions you need to convert to
5632 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
5635 gtk_label_get_layout_offsets (GtkLabel *label,
5639 g_return_if_fail (GTK_IS_LABEL (label));
5641 gtk_label_ensure_layout (label);
5643 get_layout_location (label, x, y);
5647 * gtk_label_set_use_markup:
5648 * @label: a #GtkLabel
5649 * @setting: %TRUE if the label's text should be parsed for markup.
5651 * Sets whether the text of the label contains markup in <link
5652 * linkend="PangoMarkupFormat">Pango's text markup
5653 * language</link>. See gtk_label_set_markup().
5656 gtk_label_set_use_markup (GtkLabel *label,
5659 g_return_if_fail (GTK_IS_LABEL (label));
5661 gtk_label_set_use_markup_internal (label, setting);
5662 gtk_label_recalculate (label);
5666 * gtk_label_get_use_markup:
5667 * @label: a #GtkLabel
5669 * Returns whether the label's text is interpreted as marked up with
5670 * the <link linkend="PangoMarkupFormat">Pango text markup
5671 * language</link>. See gtk_label_set_use_markup ().
5673 * Return value: %TRUE if the label's text will be parsed for markup.
5676 gtk_label_get_use_markup (GtkLabel *label)
5678 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5680 return label->priv->use_markup;
5684 * gtk_label_set_use_underline:
5685 * @label: a #GtkLabel
5686 * @setting: %TRUE if underlines in the text indicate mnemonics
5688 * If true, an underline in the text indicates the next character should be
5689 * used for the mnemonic accelerator key.
5692 gtk_label_set_use_underline (GtkLabel *label,
5695 g_return_if_fail (GTK_IS_LABEL (label));
5697 gtk_label_set_use_underline_internal (label, setting);
5698 gtk_label_recalculate (label);
5702 * gtk_label_get_use_underline:
5703 * @label: a #GtkLabel
5705 * Returns whether an embedded underline in the label indicates a
5706 * mnemonic. See gtk_label_set_use_underline().
5708 * Return value: %TRUE whether an embedded underline in the label indicates
5709 * the mnemonic accelerator keys.
5712 gtk_label_get_use_underline (GtkLabel *label)
5714 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5716 return label->priv->use_underline;
5720 * gtk_label_set_single_line_mode:
5721 * @label: a #GtkLabel
5722 * @single_line_mode: %TRUE if the label should be in single line mode
5724 * Sets whether the label is in single line mode.
5729 gtk_label_set_single_line_mode (GtkLabel *label,
5730 gboolean single_line_mode)
5732 GtkLabelPrivate *priv;
5734 g_return_if_fail (GTK_IS_LABEL (label));
5738 single_line_mode = single_line_mode != FALSE;
5740 if (priv->single_line_mode != single_line_mode)
5742 priv->single_line_mode = single_line_mode;
5744 gtk_label_clear_layout (label);
5745 gtk_widget_queue_resize (GTK_WIDGET (label));
5747 g_object_notify (G_OBJECT (label), "single-line-mode");
5752 * gtk_label_get_single_line_mode:
5753 * @label: a #GtkLabel
5755 * Returns whether the label is in single line mode.
5757 * Return value: %TRUE when the label is in single line mode.
5762 gtk_label_get_single_line_mode (GtkLabel *label)
5764 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5766 return label->priv->single_line_mode;
5769 /* Compute the X position for an offset that corresponds to the "more important
5770 * cursor position for that offset. We use this when trying to guess to which
5771 * end of the selection we should go to when the user hits the left or
5775 get_better_cursor (GtkLabel *label,
5780 GtkLabelPrivate *priv = label->priv;
5781 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5782 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5783 PangoDirection cursor_direction = get_cursor_direction (label);
5784 gboolean split_cursor;
5785 PangoRectangle strong_pos, weak_pos;
5787 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5788 "gtk-split-cursor", &split_cursor,
5791 gtk_label_ensure_layout (label);
5793 pango_layout_get_cursor_pos (priv->layout, index,
5794 &strong_pos, &weak_pos);
5798 *x = strong_pos.x / PANGO_SCALE;
5799 *y = strong_pos.y / PANGO_SCALE;
5803 if (keymap_direction == cursor_direction)
5805 *x = strong_pos.x / PANGO_SCALE;
5806 *y = strong_pos.y / PANGO_SCALE;
5810 *x = weak_pos.x / PANGO_SCALE;
5811 *y = weak_pos.y / PANGO_SCALE;
5818 gtk_label_move_logically (GtkLabel *label,
5822 GtkLabelPrivate *priv = label->priv;
5823 gint offset = g_utf8_pointer_to_offset (priv->text,
5824 priv->text + start);
5828 PangoLogAttr *log_attrs;
5832 gtk_label_ensure_layout (label);
5834 length = g_utf8_strlen (priv->text, -1);
5836 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5838 while (count > 0 && offset < length)
5842 while (offset < length && !log_attrs[offset].is_cursor_position);
5846 while (count < 0 && offset > 0)
5850 while (offset > 0 && !log_attrs[offset].is_cursor_position);
5858 return g_utf8_offset_to_pointer (priv->text, offset) - priv->text;
5862 gtk_label_move_visually (GtkLabel *label,
5866 GtkLabelPrivate *priv = label->priv;
5873 int new_index, new_trailing;
5874 gboolean split_cursor;
5877 gtk_label_ensure_layout (label);
5879 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5880 "gtk-split-cursor", &split_cursor,
5887 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5888 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5890 strong = keymap_direction == get_cursor_direction (label);
5895 pango_layout_move_cursor_visually (priv->layout, strong, index, 0, 1, &new_index, &new_trailing);
5900 pango_layout_move_cursor_visually (priv->layout, strong, index, 0, -1, &new_index, &new_trailing);
5904 if (new_index < 0 || new_index == G_MAXINT)
5909 while (new_trailing--)
5910 index = g_utf8_next_char (priv->text + new_index) - priv->text;
5917 gtk_label_move_forward_word (GtkLabel *label,
5920 GtkLabelPrivate *priv = label->priv;
5921 gint new_pos = g_utf8_pointer_to_offset (priv->text,
5922 priv->text + start);
5925 length = g_utf8_strlen (priv->text, -1);
5926 if (new_pos < length)
5928 PangoLogAttr *log_attrs;
5931 gtk_label_ensure_layout (label);
5933 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5935 /* Find the next word end */
5937 while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
5943 return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
5948 gtk_label_move_backward_word (GtkLabel *label,
5951 GtkLabelPrivate *priv = label->priv;
5952 gint new_pos = g_utf8_pointer_to_offset (priv->text,
5953 priv->text + start);
5957 PangoLogAttr *log_attrs;
5960 gtk_label_ensure_layout (label);
5962 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5966 /* Find the previous word beginning */
5967 while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
5973 return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
5977 gtk_label_move_cursor (GtkLabel *label,
5978 GtkMovementStep step,
5980 gboolean extend_selection)
5982 GtkLabelPrivate *priv = label->priv;
5986 if (priv->select_info == NULL)
5989 old_pos = new_pos = priv->select_info->selection_end;
5991 if (priv->select_info->selection_end != priv->select_info->selection_anchor &&
5994 /* If we have a current selection and aren't extending it, move to the
5995 * start/or end of the selection as appropriate
5999 case GTK_MOVEMENT_VISUAL_POSITIONS:
6002 gint anchor_x, anchor_y;
6003 gboolean end_is_left;
6005 get_better_cursor (label, priv->select_info->selection_end, &end_x, &end_y);
6006 get_better_cursor (label, priv->select_info->selection_anchor, &anchor_x, &anchor_y);
6008 end_is_left = (end_y < anchor_y) || (end_y == anchor_y && end_x < anchor_x);
6011 new_pos = end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
6013 new_pos = !end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
6016 case GTK_MOVEMENT_LOGICAL_POSITIONS:
6017 case GTK_MOVEMENT_WORDS:
6019 new_pos = MIN (priv->select_info->selection_end, priv->select_info->selection_anchor);
6021 new_pos = MAX (priv->select_info->selection_end, priv->select_info->selection_anchor);
6023 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
6024 case GTK_MOVEMENT_PARAGRAPH_ENDS:
6025 case GTK_MOVEMENT_BUFFER_ENDS:
6026 /* FIXME: Can do better here */
6027 new_pos = count < 0 ? 0 : strlen (priv->text);
6029 case GTK_MOVEMENT_DISPLAY_LINES:
6030 case GTK_MOVEMENT_PARAGRAPHS:
6031 case GTK_MOVEMENT_PAGES:
6032 case GTK_MOVEMENT_HORIZONTAL_PAGES:
6040 case GTK_MOVEMENT_LOGICAL_POSITIONS:
6041 new_pos = gtk_label_move_logically (label, new_pos, count);
6043 case GTK_MOVEMENT_VISUAL_POSITIONS:
6044 new_pos = gtk_label_move_visually (label, new_pos, count);
6045 if (new_pos == old_pos)
6047 if (!extend_selection)
6049 if (!gtk_widget_keynav_failed (GTK_WIDGET (label),
6051 GTK_DIR_RIGHT : GTK_DIR_LEFT))
6053 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
6056 gtk_widget_child_focus (toplevel,
6058 GTK_DIR_RIGHT : GTK_DIR_LEFT);
6063 gtk_widget_error_bell (GTK_WIDGET (label));
6067 case GTK_MOVEMENT_WORDS:
6070 new_pos = gtk_label_move_forward_word (label, new_pos);
6075 new_pos = gtk_label_move_backward_word (label, new_pos);
6078 if (new_pos == old_pos)
6079 gtk_widget_error_bell (GTK_WIDGET (label));
6081 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
6082 case GTK_MOVEMENT_PARAGRAPH_ENDS:
6083 case GTK_MOVEMENT_BUFFER_ENDS:
6084 /* FIXME: Can do better here */
6085 new_pos = count < 0 ? 0 : strlen (priv->text);
6086 if (new_pos == old_pos)
6087 gtk_widget_error_bell (GTK_WIDGET (label));
6089 case GTK_MOVEMENT_DISPLAY_LINES:
6090 case GTK_MOVEMENT_PARAGRAPHS:
6091 case GTK_MOVEMENT_PAGES:
6092 case GTK_MOVEMENT_HORIZONTAL_PAGES:
6097 if (extend_selection)
6098 gtk_label_select_region_index (label,
6099 priv->select_info->selection_anchor,
6102 gtk_label_select_region_index (label, new_pos, new_pos);
6106 gtk_label_copy_clipboard (GtkLabel *label)
6108 GtkLabelPrivate *priv = label->priv;
6110 if (priv->text && priv->select_info)
6114 GtkClipboard *clipboard;
6116 start = MIN (priv->select_info->selection_anchor,
6117 priv->select_info->selection_end);
6118 end = MAX (priv->select_info->selection_anchor,
6119 priv->select_info->selection_end);
6121 len = strlen (priv->text);
6129 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6132 gtk_clipboard_set_text (clipboard, priv->text + start, end - start);
6137 link = gtk_label_get_focus_link (label);
6139 gtk_clipboard_set_text (clipboard, link->uri, -1);
6145 gtk_label_select_all (GtkLabel *label)
6147 GtkLabelPrivate *priv = label->priv;
6149 gtk_label_select_region_index (label, 0, strlen (priv->text));
6152 /* Quick hack of a popup menu
6155 activate_cb (GtkWidget *menuitem,
6158 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6159 g_signal_emit_by_name (label, signal);
6163 append_action_signal (GtkLabel *label,
6165 const gchar *stock_id,
6166 const gchar *signal,
6169 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6171 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
6172 g_signal_connect (menuitem, "activate",
6173 G_CALLBACK (activate_cb), label);
6175 gtk_widget_set_sensitive (menuitem, sensitive);
6177 gtk_widget_show (menuitem);
6178 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6182 popup_menu_detach (GtkWidget *attach_widget,
6185 GtkLabel *label = GTK_LABEL (attach_widget);
6186 GtkLabelPrivate *priv = label->priv;
6188 if (priv->select_info)
6189 priv->select_info->popup_menu = NULL;
6193 popup_position_func (GtkMenu *menu,
6201 GtkAllocation allocation;
6205 label = GTK_LABEL (user_data);
6206 widget = GTK_WIDGET (label);
6208 g_return_if_fail (gtk_widget_get_realized (widget));
6210 screen = gtk_widget_get_screen (widget);
6211 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
6213 gtk_widget_get_allocation (widget, &allocation);
6218 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
6221 gtk_widget_get_allocation (widget, &allocation);
6223 *x += allocation.width / 2;
6224 *y += allocation.height;
6226 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
6227 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
6231 open_link_activate_cb (GtkMenuItem *menu_item,
6236 link = gtk_label_get_current_link (label);
6239 emit_activate_link (label, link);
6243 copy_link_activate_cb (GtkMenuItem *menu_item,
6246 GtkClipboard *clipboard;
6249 uri = gtk_label_get_current_uri (label);
6252 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6253 gtk_clipboard_set_text (clipboard, uri, -1);
6258 gtk_label_popup_menu (GtkWidget *widget)
6260 gtk_label_do_popup (GTK_LABEL (widget), NULL);
6266 gtk_label_do_popup (GtkLabel *label,
6267 GdkEventButton *event)
6269 GtkLabelPrivate *priv = label->priv;
6270 GtkWidget *menuitem;
6273 gboolean have_selection;
6276 if (!priv->select_info)
6279 if (priv->select_info->popup_menu)
6280 gtk_widget_destroy (priv->select_info->popup_menu);
6282 priv->select_info->popup_menu = menu = gtk_menu_new ();
6284 gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (label), popup_menu_detach);
6287 priv->select_info->selection_anchor != priv->select_info->selection_end;
6291 if (priv->select_info->link_clicked)
6292 link = priv->select_info->active_link;
6297 link = gtk_label_get_focus_link (label);
6299 if (!have_selection && link)
6302 menuitem = gtk_image_menu_item_new_with_mnemonic (_("_Open Link"));
6303 gtk_widget_show (menuitem);
6304 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6306 g_signal_connect (G_OBJECT (menuitem), "activate",
6307 G_CALLBACK (open_link_activate_cb), label);
6309 image = gtk_image_new_from_stock (GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_MENU);
6310 gtk_widget_show (image);
6311 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6313 /* Copy Link Address */
6314 menuitem = gtk_image_menu_item_new_with_mnemonic (_("Copy _Link Address"));
6315 gtk_widget_show (menuitem);
6316 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6318 g_signal_connect (G_OBJECT (menuitem), "activate",
6319 G_CALLBACK (copy_link_activate_cb), label);
6321 image = gtk_image_new_from_stock (GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
6322 gtk_widget_show (image);
6323 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6327 append_action_signal (label, menu, GTK_STOCK_CUT, "cut-clipboard", FALSE);
6328 append_action_signal (label, menu, GTK_STOCK_COPY, "copy-clipboard", have_selection);
6329 append_action_signal (label, menu, GTK_STOCK_PASTE, "paste-clipboard", FALSE);
6331 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
6332 gtk_widget_set_sensitive (menuitem, FALSE);
6333 gtk_widget_show (menuitem);
6334 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6336 menuitem = gtk_separator_menu_item_new ();
6337 gtk_widget_show (menuitem);
6338 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6340 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
6341 g_signal_connect_swapped (menuitem, "activate",
6342 G_CALLBACK (gtk_label_select_all), label);
6343 gtk_widget_show (menuitem);
6344 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6347 g_signal_emit (label, signals[POPULATE_POPUP], 0, menu);
6350 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6352 event->button, event->time);
6355 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6356 popup_position_func, label,
6357 0, gtk_get_current_event_time ());
6358 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
6363 gtk_label_clear_links (GtkLabel *label)
6365 GtkLabelPrivate *priv = label->priv;
6367 if (!priv->select_info)
6370 g_list_foreach (priv->select_info->links, (GFunc)link_free, NULL);
6371 g_list_free (priv->select_info->links);
6372 priv->select_info->links = NULL;
6373 priv->select_info->active_link = NULL;
6377 gtk_label_rescan_links (GtkLabel *label)
6379 GtkLabelPrivate *priv = label->priv;
6380 PangoLayout *layout = priv->layout;
6381 PangoAttrList *attlist;
6382 PangoAttrIterator *iter;
6385 if (!priv->select_info || !priv->select_info->links)
6388 attlist = pango_layout_get_attributes (layout);
6390 if (attlist == NULL)
6393 iter = pango_attr_list_get_iterator (attlist);
6395 links = priv->select_info->links;
6399 PangoAttribute *underline;
6400 PangoAttribute *color;
6402 underline = pango_attr_iterator_get (iter, PANGO_ATTR_UNDERLINE);
6403 color = pango_attr_iterator_get (iter, PANGO_ATTR_FOREGROUND);
6405 if (underline != NULL && color != NULL)
6408 PangoRectangle start_pos;
6409 PangoRectangle end_pos;
6412 pango_attr_iterator_range (iter, &start, &end);
6413 pango_layout_index_to_pos (layout, start, &start_pos);
6414 pango_layout_index_to_pos (layout, end, &end_pos);
6418 g_warning ("Ran out of links");
6422 links = links->next;
6423 link->start = start;
6426 } while (pango_attr_iterator_next (iter));
6428 pango_attr_iterator_destroy (iter);
6432 gtk_label_activate_link (GtkLabel *label,
6435 GtkWidget *widget = GTK_WIDGET (label);
6436 GError *error = NULL;
6438 if (!gtk_show_uri (gtk_widget_get_screen (widget),
6439 uri, gtk_get_current_event_time (), &error))
6441 g_warning ("Unable to show '%s': %s", uri, error->message);
6442 g_error_free (error);
6449 emit_activate_link (GtkLabel *label,
6452 GtkLabelPrivate *priv = label->priv;
6455 g_signal_emit (label, signals[ACTIVATE_LINK], 0, link->uri, &handled);
6456 if (handled && priv->track_links && !link->visited)
6458 link->visited = TRUE;
6459 /* FIXME: shouldn't have to redo everything here */
6460 gtk_label_recalculate (label);
6465 gtk_label_activate_current_link (GtkLabel *label)
6468 GtkWidget *widget = GTK_WIDGET (label);
6470 link = gtk_label_get_focus_link (label);
6474 emit_activate_link (label, link);
6478 GtkWidget *toplevel;
6480 GtkWidget *default_widget, *focus_widget;
6482 toplevel = gtk_widget_get_toplevel (widget);
6483 if (GTK_IS_WINDOW (toplevel))
6485 window = GTK_WINDOW (toplevel);
6489 default_widget = gtk_window_get_default_widget (window);
6490 focus_widget = gtk_window_get_focus (window);
6492 if (default_widget != widget &&
6493 !(widget == focus_widget && (!default_widget || !gtk_widget_is_sensitive (default_widget))))
6494 gtk_window_activate_default (window);
6500 static GtkLabelLink *
6501 gtk_label_get_current_link (GtkLabel *label)
6503 GtkLabelPrivate *priv = label->priv;
6506 if (!priv->select_info)
6509 if (priv->select_info->link_clicked)
6510 link = priv->select_info->active_link;
6512 link = gtk_label_get_focus_link (label);
6518 * gtk_label_get_current_uri:
6519 * @label: a #GtkLabel
6521 * Returns the URI for the currently active link in the label.
6522 * The active link is the one under the mouse pointer or, in a
6523 * selectable label, the link in which the text cursor is currently
6526 * This function is intended for use in a #GtkLabel::activate-link handler
6527 * or for use in a #GtkWidget::query-tooltip handler.
6529 * Returns: the currently active URI. The string is owned by GTK+ and must
6530 * not be freed or modified.
6534 G_CONST_RETURN gchar *
6535 gtk_label_get_current_uri (GtkLabel *label)
6539 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
6541 link = gtk_label_get_current_link (label);
6550 * gtk_label_set_track_visited_links:
6551 * @label: a #GtkLabel
6552 * @track_links: %TRUE to track visited links
6554 * Sets whether the label should keep track of clicked
6555 * links (and use a different color for them).
6560 gtk_label_set_track_visited_links (GtkLabel *label,
6561 gboolean track_links)
6563 GtkLabelPrivate *priv;
6565 g_return_if_fail (GTK_IS_LABEL (label));
6569 track_links = track_links != FALSE;
6571 if (priv->track_links != track_links)
6573 priv->track_links = track_links;
6575 /* FIXME: shouldn't have to redo everything here */
6576 gtk_label_recalculate (label);
6578 g_object_notify (G_OBJECT (label), "track-visited-links");
6583 * gtk_label_get_track_visited_links:
6584 * @label: a #GtkLabel
6586 * Returns whether the label is currently keeping track
6589 * Returns: %TRUE if clicked links are remembered
6594 gtk_label_get_track_visited_links (GtkLabel *label)
6596 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
6598 return label->priv->track_links;
6602 gtk_label_query_tooltip (GtkWidget *widget,
6605 gboolean keyboard_tip,
6606 GtkTooltip *tooltip)
6608 GtkLabel *label = GTK_LABEL (widget);
6609 GtkLabelPrivate *priv = label->priv;
6610 GtkLabelSelectionInfo *info = priv->select_info;
6614 if (info && info->links)
6618 if (info->selection_anchor == info->selection_end)
6619 index = info->selection_anchor;
6623 if (!get_layout_index (label, x, y, &index))
6629 for (l = info->links; l != NULL; l = l->next)
6631 GtkLabelLink *link = l->data;
6632 if (index >= link->start && index <= link->end)
6636 gtk_tooltip_set_markup (tooltip, link->title);
6645 return GTK_WIDGET_CLASS (gtk_label_parent_class)->query_tooltip (widget,