1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
21 * file for a list of people on the GTK+ Team. See the ChangeLog
22 * files for a list of changes. These files are distributed with
23 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
32 #include "gtkaccellabel.h"
35 #include "gtkmarshalers.h"
36 #include "gtkwindow.h"
37 #include "gdk/gdkkeysyms.h"
38 #include "gtkclipboard.h"
39 #include "gtkimagemenuitem.h"
41 #include "gtkseparatormenuitem.h"
42 #include "gtktextutil.h"
43 #include "gtkmenuitem.h"
44 #include "gtknotebook.h"
46 #include "gtkbindings.h"
47 #include "gtkbuildable.h"
50 #include "gtktooltip.h"
51 #include "gtkextendedlayout.h"
52 #include "gtkprivate.h"
55 #define GTK_LABEL_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_LABEL, GtkLabelPrivate))
63 gboolean mnemonics_visible;
66 /* Notes about the handling of links:
68 * Links share the GtkLabelSelectionInfo struct with selectable labels.
69 * There are some new fields for links. The links field contains the list
70 * of GtkLabelLink structs that describe the links which are embedded in
71 * the label. The active_link field points to the link under the mouse
72 * pointer. For keyboard navigation, the 'focus' link is determined by
73 * finding the link which contains the selection_anchor position.
74 * The link_clicked field is used with button press and release events
75 * to ensure that pressing inside a link and releasing outside of it
76 * does not activate the link.
78 * Links are rendered with the link-color/visited-link-color colors
79 * that are determined by the style and with an underline. When the mouse
80 * pointer is over a link, the pointer is changed to indicate the link,
81 * and the background behind the link is rendered with the base[PRELIGHT]
82 * color. While a button is pressed over a link, the background is rendered
83 * with the base[ACTIVE] color.
85 * Labels with links accept keyboard focus, and it is possible to move
86 * the focus between the embedded links using Tab/Shift-Tab. The focus
87 * is indicated by a focus rectangle that is drawn around the link text.
88 * Pressing Enter activates the focussed link, and there is a suitable
89 * context menu for links that can be opened with the Menu key. Pressing
90 * Control-C copies the link URI to the clipboard.
92 * In selectable labels with links, link functionality is only available
93 * when the selection is empty.
98 gchar *title; /* the title attribute, used as tooltip */
99 gboolean visited; /* get set when the link is activated; this flag
100 * gets preserved over later set_markup() calls
102 gint start; /* position of the link in the PangoLayout */
106 struct _GtkLabelSelectionInfo
109 gint selection_anchor;
111 GtkWidget *popup_menu;
114 GtkLabelLink *active_link;
120 guint select_words : 1;
121 guint selectable : 1;
122 guint link_clicked : 1;
130 ACTIVATE_CURRENT_LINK,
145 PROP_MNEMONIC_KEYVAL,
146 PROP_MNEMONIC_WIDGET,
147 PROP_CURSOR_POSITION,
148 PROP_SELECTION_BOUND,
151 PROP_SINGLE_LINE_MODE,
153 PROP_MAX_WIDTH_CHARS,
154 PROP_TRACK_VISITED_LINKS
157 /* When rotating ellipsizable text we want the natural size to request
158 * more to ensure the label wont ever ellipsize in an allocation of full natural size.
160 #define ROTATION_ELLIPSIZE_PADDING 2
162 static guint signals[LAST_SIGNAL] = { 0 };
164 static const GdkColor default_link_color = { 0, 0, 0, 0xeeee };
165 static const GdkColor default_visited_link_color = { 0, 0x5555, 0x1a1a, 0x8b8b };
167 static void gtk_label_set_property (GObject *object,
171 static void gtk_label_get_property (GObject *object,
175 static void gtk_label_destroy (GtkObject *object);
176 static void gtk_label_finalize (GObject *object);
177 static void gtk_label_size_allocate (GtkWidget *widget,
178 GtkAllocation *allocation);
179 static void gtk_label_state_changed (GtkWidget *widget,
181 static void gtk_label_style_set (GtkWidget *widget,
182 GtkStyle *previous_style);
183 static void gtk_label_direction_changed (GtkWidget *widget,
184 GtkTextDirection previous_dir);
185 static gint gtk_label_expose (GtkWidget *widget,
186 GdkEventExpose *event);
187 static gboolean gtk_label_focus (GtkWidget *widget,
188 GtkDirectionType direction);
190 static void gtk_label_realize (GtkWidget *widget);
191 static void gtk_label_unrealize (GtkWidget *widget);
192 static void gtk_label_map (GtkWidget *widget);
193 static void gtk_label_unmap (GtkWidget *widget);
195 static gboolean gtk_label_button_press (GtkWidget *widget,
196 GdkEventButton *event);
197 static gboolean gtk_label_button_release (GtkWidget *widget,
198 GdkEventButton *event);
199 static gboolean gtk_label_motion (GtkWidget *widget,
200 GdkEventMotion *event);
201 static gboolean gtk_label_leave_notify (GtkWidget *widget,
202 GdkEventCrossing *event);
204 static void gtk_label_grab_focus (GtkWidget *widget);
206 static gboolean gtk_label_query_tooltip (GtkWidget *widget,
209 gboolean keyboard_tip,
210 GtkTooltip *tooltip);
212 static void gtk_label_set_text_internal (GtkLabel *label,
214 static void gtk_label_set_label_internal (GtkLabel *label,
216 static void gtk_label_set_use_markup_internal (GtkLabel *label,
218 static void gtk_label_set_use_underline_internal (GtkLabel *label,
220 static void gtk_label_set_attributes_internal (GtkLabel *label,
221 PangoAttrList *attrs);
222 static void gtk_label_set_uline_text_internal (GtkLabel *label,
224 static void gtk_label_set_pattern_internal (GtkLabel *label,
225 const gchar *pattern,
226 gboolean is_mnemonic);
227 static void gtk_label_set_markup_internal (GtkLabel *label,
229 gboolean with_uline);
230 static void gtk_label_recalculate (GtkLabel *label);
231 static void gtk_label_hierarchy_changed (GtkWidget *widget,
232 GtkWidget *old_toplevel);
233 static void gtk_label_screen_changed (GtkWidget *widget,
234 GdkScreen *old_screen);
235 static gboolean gtk_label_popup_menu (GtkWidget *widget);
237 static void gtk_label_create_window (GtkLabel *label);
238 static void gtk_label_destroy_window (GtkLabel *label);
239 static void gtk_label_ensure_select_info (GtkLabel *label);
240 static void gtk_label_clear_select_info (GtkLabel *label);
241 static void gtk_label_update_cursor (GtkLabel *label);
242 static void gtk_label_clear_layout (GtkLabel *label);
243 static void gtk_label_ensure_layout (GtkLabel *label, gboolean guess_wrap_width);
244 static void gtk_label_invalidate_wrap_width (GtkLabel *label);
245 static void gtk_label_select_region_index (GtkLabel *label,
249 static gboolean gtk_label_mnemonic_activate (GtkWidget *widget,
250 gboolean group_cycling);
251 static void gtk_label_setup_mnemonic (GtkLabel *label,
253 static void gtk_label_drag_data_get (GtkWidget *widget,
254 GdkDragContext *context,
255 GtkSelectionData *selection_data,
259 static void gtk_label_buildable_interface_init (GtkBuildableIface *iface);
260 static gboolean gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
263 const gchar *tagname,
264 GMarkupParser *parser,
267 static void gtk_label_buildable_custom_finished (GtkBuildable *buildable,
270 const gchar *tagname,
274 static void connect_mnemonics_visible_notify (GtkLabel *label);
275 static gboolean separate_uline_pattern (const gchar *str,
281 /* For selectable labels: */
282 static void gtk_label_move_cursor (GtkLabel *label,
283 GtkMovementStep step,
285 gboolean extend_selection);
286 static void gtk_label_copy_clipboard (GtkLabel *label);
287 static void gtk_label_select_all (GtkLabel *label);
288 static void gtk_label_do_popup (GtkLabel *label,
289 GdkEventButton *event);
290 static gint gtk_label_move_forward_word (GtkLabel *label,
292 static gint gtk_label_move_backward_word (GtkLabel *label,
296 static void gtk_label_rescan_links (GtkLabel *label);
297 static void gtk_label_clear_links (GtkLabel *label);
298 static gboolean gtk_label_activate_link (GtkLabel *label,
300 static void gtk_label_activate_current_link (GtkLabel *label);
301 static GtkLabelLink *gtk_label_get_current_link (GtkLabel *label);
302 static void gtk_label_get_link_colors (GtkWidget *widget,
303 GdkColor **link_color,
304 GdkColor **visited_link_color);
305 static void emit_activate_link (GtkLabel *label,
308 static void gtk_label_extended_layout_init (GtkExtendedLayoutIface *iface);
309 static gboolean gtk_label_is_height_for_width (GtkExtendedLayout *layout);
310 static void gtk_label_get_desired_width (GtkExtendedLayout *layout,
313 static void gtk_label_get_desired_height (GtkExtendedLayout *layout,
316 static void gtk_label_get_width_for_height (GtkExtendedLayout *layout,
319 gint *natural_width);
320 static void gtk_label_get_height_for_width (GtkExtendedLayout *layout,
322 gint *minimum_height,
323 gint *natural_height);
325 static GQuark quark_angle = 0;
327 static GtkBuildableIface *buildable_parent_iface = NULL;
329 G_DEFINE_TYPE_WITH_CODE (GtkLabel, gtk_label, GTK_TYPE_MISC,
330 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
331 gtk_label_buildable_interface_init)
332 G_IMPLEMENT_INTERFACE (GTK_TYPE_EXTENDED_LAYOUT,
333 gtk_label_extended_layout_init));
336 add_move_binding (GtkBindingSet *binding_set,
339 GtkMovementStep step,
342 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
344 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
348 G_TYPE_BOOLEAN, FALSE);
350 /* Selection-extending version */
351 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
355 G_TYPE_BOOLEAN, TRUE);
359 gtk_label_class_init (GtkLabelClass *class)
361 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
362 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
363 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
364 GtkBindingSet *binding_set;
366 quark_angle = g_quark_from_static_string ("angle");
368 gobject_class->set_property = gtk_label_set_property;
369 gobject_class->get_property = gtk_label_get_property;
370 gobject_class->finalize = gtk_label_finalize;
372 object_class->destroy = gtk_label_destroy;
374 widget_class->size_allocate = gtk_label_size_allocate;
375 widget_class->state_changed = gtk_label_state_changed;
376 widget_class->style_set = gtk_label_style_set;
377 widget_class->query_tooltip = gtk_label_query_tooltip;
378 widget_class->direction_changed = gtk_label_direction_changed;
379 widget_class->expose_event = gtk_label_expose;
380 widget_class->realize = gtk_label_realize;
381 widget_class->unrealize = gtk_label_unrealize;
382 widget_class->map = gtk_label_map;
383 widget_class->unmap = gtk_label_unmap;
384 widget_class->button_press_event = gtk_label_button_press;
385 widget_class->button_release_event = gtk_label_button_release;
386 widget_class->motion_notify_event = gtk_label_motion;
387 widget_class->leave_notify_event = gtk_label_leave_notify;
388 widget_class->hierarchy_changed = gtk_label_hierarchy_changed;
389 widget_class->screen_changed = gtk_label_screen_changed;
390 widget_class->mnemonic_activate = gtk_label_mnemonic_activate;
391 widget_class->drag_data_get = gtk_label_drag_data_get;
392 widget_class->grab_focus = gtk_label_grab_focus;
393 widget_class->popup_menu = gtk_label_popup_menu;
394 widget_class->focus = gtk_label_focus;
396 class->move_cursor = gtk_label_move_cursor;
397 class->copy_clipboard = gtk_label_copy_clipboard;
398 class->activate_link = gtk_label_activate_link;
401 * GtkLabel::move-cursor:
402 * @entry: the object which received the signal
403 * @step: the granularity of the move, as a #GtkMovementStep
404 * @count: the number of @step units to move
405 * @extend_selection: %TRUE if the move should extend the selection
407 * The ::move-cursor signal is a
408 * <link linkend="keybinding-signals">keybinding signal</link>
409 * which gets emitted when the user initiates a cursor movement.
410 * If the cursor is not visible in @entry, this signal causes
411 * the viewport to be moved instead.
413 * Applications should not connect to it, but may emit it with
414 * g_signal_emit_by_name() if they need to control the cursor
417 * The default bindings for this signal come in two variants,
418 * the variant with the Shift modifier extends the selection,
419 * the variant without the Shift modifer does not.
420 * There are too many key combinations to list them all here.
422 * <listitem>Arrow keys move by individual characters/lines</listitem>
423 * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
424 * <listitem>Home/End keys move to the ends of the buffer</listitem>
427 signals[MOVE_CURSOR] =
428 g_signal_new (I_("move-cursor"),
429 G_OBJECT_CLASS_TYPE (gobject_class),
430 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
431 G_STRUCT_OFFSET (GtkLabelClass, move_cursor),
433 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
435 GTK_TYPE_MOVEMENT_STEP,
440 * GtkLabel::copy-clipboard:
441 * @label: the object which received the signal
443 * The ::copy-clipboard signal is a
444 * <link linkend="keybinding-signals">keybinding signal</link>
445 * which gets emitted to copy the selection to the clipboard.
447 * The default binding for this signal is Ctrl-c.
449 signals[COPY_CLIPBOARD] =
450 g_signal_new (I_("copy-clipboard"),
451 G_OBJECT_CLASS_TYPE (gobject_class),
452 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
453 G_STRUCT_OFFSET (GtkLabelClass, copy_clipboard),
455 _gtk_marshal_VOID__VOID,
459 * GtkLabel::populate-popup:
460 * @label: The label on which the signal is emitted
461 * @menu: the menu that is being populated
463 * The ::populate-popup signal gets emitted before showing the
464 * context menu of the label. Note that only selectable labels
465 * have context menus.
467 * If you need to add items to the context menu, connect
468 * to this signal and append your menuitems to the @menu.
470 signals[POPULATE_POPUP] =
471 g_signal_new (I_("populate-popup"),
472 G_OBJECT_CLASS_TYPE (gobject_class),
474 G_STRUCT_OFFSET (GtkLabelClass, populate_popup),
476 _gtk_marshal_VOID__OBJECT,
481 * GtkLabel::activate-current-link:
482 * @label: The label on which the signal was emitted
484 * A <link linkend="keybinding-signals">keybinding signal</link>
485 * which gets emitted when the user activates a link in the label.
487 * Applications may also emit the signal with g_signal_emit_by_name()
488 * if they need to control activation of URIs programmatically.
490 * The default bindings for this signal are all forms of the Enter key.
494 signals[ACTIVATE_CURRENT_LINK] =
495 g_signal_new_class_handler ("activate-current-link",
496 G_TYPE_FROM_CLASS (object_class),
497 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
498 G_CALLBACK (gtk_label_activate_current_link),
500 _gtk_marshal_VOID__VOID,
504 * GtkLabel::activate-link:
505 * @label: The label on which the signal was emitted
506 * @uri: the URI that is activated
508 * The signal which gets emitted to activate a URI.
509 * Applications may connect to it to override the default behaviour,
510 * which is to call gtk_show_uri().
512 * Returns: %TRUE if the link has been activated
516 signals[ACTIVATE_LINK] =
517 g_signal_new ("activate-link",
518 G_TYPE_FROM_CLASS (object_class),
520 G_STRUCT_OFFSET (GtkLabelClass, activate_link),
521 _gtk_boolean_handled_accumulator, NULL,
522 _gtk_marshal_BOOLEAN__STRING,
523 G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
525 g_object_class_install_property (gobject_class,
527 g_param_spec_string ("label",
529 P_("The text of the label"),
531 GTK_PARAM_READWRITE));
532 g_object_class_install_property (gobject_class,
534 g_param_spec_boxed ("attributes",
536 P_("A list of style attributes to apply to the text of the label"),
537 PANGO_TYPE_ATTR_LIST,
538 GTK_PARAM_READWRITE));
539 g_object_class_install_property (gobject_class,
541 g_param_spec_boolean ("use-markup",
543 P_("The text of the label includes XML markup. See pango_parse_markup()"),
545 GTK_PARAM_READWRITE));
546 g_object_class_install_property (gobject_class,
548 g_param_spec_boolean ("use-underline",
550 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
552 GTK_PARAM_READWRITE));
554 g_object_class_install_property (gobject_class,
556 g_param_spec_enum ("justify",
558 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"),
559 GTK_TYPE_JUSTIFICATION,
561 GTK_PARAM_READWRITE));
563 g_object_class_install_property (gobject_class,
565 g_param_spec_string ("pattern",
567 P_("A string with _ characters in positions correspond to characters in the text to underline"),
569 GTK_PARAM_WRITABLE));
571 g_object_class_install_property (gobject_class,
573 g_param_spec_boolean ("wrap",
575 P_("If set, wrap lines if the text becomes too wide"),
577 GTK_PARAM_READWRITE));
579 * GtkLabel:wrap-mode:
581 * If line wrapping is on (see the #GtkLabel:wrap property) this controls
582 * how the line wrapping is done. The default is %PANGO_WRAP_WORD, which
583 * means wrap on word boundaries.
587 g_object_class_install_property (gobject_class,
589 g_param_spec_enum ("wrap-mode",
590 P_("Line wrap mode"),
591 P_("If wrap is set, controls how linewrapping is done"),
592 PANGO_TYPE_WRAP_MODE,
594 GTK_PARAM_READWRITE));
595 g_object_class_install_property (gobject_class,
597 g_param_spec_boolean ("selectable",
599 P_("Whether the label text can be selected with the mouse"),
601 GTK_PARAM_READWRITE));
602 g_object_class_install_property (gobject_class,
603 PROP_MNEMONIC_KEYVAL,
604 g_param_spec_uint ("mnemonic-keyval",
606 P_("The mnemonic accelerator key for this label"),
610 GTK_PARAM_READABLE));
611 g_object_class_install_property (gobject_class,
612 PROP_MNEMONIC_WIDGET,
613 g_param_spec_object ("mnemonic-widget",
614 P_("Mnemonic widget"),
615 P_("The widget to be activated when the label's mnemonic "
618 GTK_PARAM_READWRITE));
620 g_object_class_install_property (gobject_class,
621 PROP_CURSOR_POSITION,
622 g_param_spec_int ("cursor-position",
623 P_("Cursor Position"),
624 P_("The current position of the insertion cursor in chars"),
628 GTK_PARAM_READABLE));
630 g_object_class_install_property (gobject_class,
631 PROP_SELECTION_BOUND,
632 g_param_spec_int ("selection-bound",
633 P_("Selection Bound"),
634 P_("The position of the opposite end of the selection from the cursor in chars"),
638 GTK_PARAM_READABLE));
641 * GtkLabel:ellipsize:
643 * The preferred place to ellipsize the string, if the label does
644 * not have enough room to display the entire string, specified as a
645 * #PangoEllisizeMode.
647 * Note that setting this property to a value other than
648 * %PANGO_ELLIPSIZE_NONE has the side-effect that the label requests
649 * only enough space to display the ellipsis "...". In particular, this
650 * means that ellipsizing labels do not work well in notebook tabs, unless
651 * the tab's #GtkNotebook:tab-expand property is set to %TRUE. Other ways
652 * to set a label's width are gtk_widget_set_size_request() and
653 * gtk_label_set_width_chars().
657 g_object_class_install_property (gobject_class,
659 g_param_spec_enum ("ellipsize",
661 P_("The preferred place to ellipsize the string, if the label does not have enough room to display the entire string"),
662 PANGO_TYPE_ELLIPSIZE_MODE,
663 PANGO_ELLIPSIZE_NONE,
664 GTK_PARAM_READWRITE));
667 * GtkLabel:width-chars:
669 * The desired width of the label, in characters. If this property is set to
670 * -1, the width will be calculated automatically.
672 * See the section on <link linkend="label-text-layout">text layout</link>
673 * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
674 * determine the width of ellipsized and wrapped labels.
678 g_object_class_install_property (gobject_class,
680 g_param_spec_int ("width-chars",
681 P_("Width In Characters"),
682 P_("The desired width of the label, in characters"),
686 GTK_PARAM_READWRITE));
689 * GtkLabel:single-line-mode:
691 * Whether the label is in single line mode. In single line mode,
692 * the height of the label does not depend on the actual text, it
693 * is always set to ascent + descent of the font. This can be an
694 * advantage in situations where resizing the label because of text
695 * changes would be distracting, e.g. in a statusbar.
699 g_object_class_install_property (gobject_class,
700 PROP_SINGLE_LINE_MODE,
701 g_param_spec_boolean ("single-line-mode",
702 P_("Single Line Mode"),
703 P_("Whether the label is in single line mode"),
705 GTK_PARAM_READWRITE));
710 * The angle that the baseline of the label makes with the horizontal,
711 * in degrees, measured counterclockwise. An angle of 90 reads from
712 * from bottom to top, an angle of 270, from top to bottom. Ignored
713 * if the label is selectable, wrapped, or ellipsized.
717 g_object_class_install_property (gobject_class,
719 g_param_spec_double ("angle",
721 P_("Angle at which the label is rotated"),
725 GTK_PARAM_READWRITE));
728 * GtkLabel:max-width-chars:
730 * The desired maximum width of the label, in characters. If this property
731 * is set to -1, the width will be calculated automatically.
733 * See the section on <link linkend="label-text-layout">text layout</link>
734 * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
735 * determine the width of ellipsized and wrapped labels.
739 g_object_class_install_property (gobject_class,
740 PROP_MAX_WIDTH_CHARS,
741 g_param_spec_int ("max-width-chars",
742 P_("Maximum Width In Characters"),
743 P_("The desired maximum width of the label, in characters"),
747 GTK_PARAM_READWRITE));
750 * GtkLabel:track-visited-links:
752 * Set this property to %TRUE to make the label track which links
753 * have been clicked. It will then apply the ::visited-link-color
754 * color, instead of ::link-color.
758 g_object_class_install_property (gobject_class,
759 PROP_TRACK_VISITED_LINKS,
760 g_param_spec_boolean ("track-visited-links",
761 P_("Track visited links"),
762 P_("Whether visited links should be tracked"),
764 GTK_PARAM_READWRITE));
769 binding_set = gtk_binding_set_by_class (class);
771 /* Moving the insertion point */
772 add_move_binding (binding_set, GDK_Right, 0,
773 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
775 add_move_binding (binding_set, GDK_Left, 0,
776 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
778 add_move_binding (binding_set, GDK_KP_Right, 0,
779 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
781 add_move_binding (binding_set, GDK_KP_Left, 0,
782 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
784 add_move_binding (binding_set, GDK_f, GDK_CONTROL_MASK,
785 GTK_MOVEMENT_LOGICAL_POSITIONS, 1);
787 add_move_binding (binding_set, GDK_b, GDK_CONTROL_MASK,
788 GTK_MOVEMENT_LOGICAL_POSITIONS, -1);
790 add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
791 GTK_MOVEMENT_WORDS, 1);
793 add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
794 GTK_MOVEMENT_WORDS, -1);
796 add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
797 GTK_MOVEMENT_WORDS, 1);
799 add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
800 GTK_MOVEMENT_WORDS, -1);
803 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
805 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
807 G_TYPE_BOOLEAN, FALSE);
809 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
811 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
813 G_TYPE_BOOLEAN, TRUE);
815 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
817 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
819 G_TYPE_BOOLEAN, FALSE);
821 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
823 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
825 G_TYPE_BOOLEAN, TRUE);
828 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
830 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
832 G_TYPE_BOOLEAN, FALSE);
834 gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_CONTROL_MASK,
836 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
838 G_TYPE_BOOLEAN, FALSE);
840 add_move_binding (binding_set, GDK_f, GDK_MOD1_MASK,
841 GTK_MOVEMENT_WORDS, 1);
843 add_move_binding (binding_set, GDK_b, GDK_MOD1_MASK,
844 GTK_MOVEMENT_WORDS, -1);
846 add_move_binding (binding_set, GDK_Home, 0,
847 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
849 add_move_binding (binding_set, GDK_End, 0,
850 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
852 add_move_binding (binding_set, GDK_KP_Home, 0,
853 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
855 add_move_binding (binding_set, GDK_KP_End, 0,
856 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
858 add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
859 GTK_MOVEMENT_BUFFER_ENDS, -1);
861 add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
862 GTK_MOVEMENT_BUFFER_ENDS, 1);
864 add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
865 GTK_MOVEMENT_BUFFER_ENDS, -1);
867 add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
868 GTK_MOVEMENT_BUFFER_ENDS, 1);
871 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
872 "copy-clipboard", 0);
874 gtk_binding_entry_add_signal (binding_set, GDK_Return, 0,
875 "activate-current-link", 0);
876 gtk_binding_entry_add_signal (binding_set, GDK_ISO_Enter, 0,
877 "activate-current-link", 0);
878 gtk_binding_entry_add_signal (binding_set, GDK_KP_Enter, 0,
879 "activate-current-link", 0);
881 gtk_settings_install_property (g_param_spec_boolean ("gtk-label-select-on-focus",
882 P_("Select on focus"),
883 P_("Whether to select the contents of a selectable label when it is focused"),
885 GTK_PARAM_READWRITE));
888 g_type_class_add_private (class, sizeof (GtkLabelPrivate));
892 gtk_label_set_property (GObject *object,
899 label = GTK_LABEL (object);
904 gtk_label_set_label (label, g_value_get_string (value));
906 case PROP_ATTRIBUTES:
907 gtk_label_set_attributes (label, g_value_get_boxed (value));
909 case PROP_USE_MARKUP:
910 gtk_label_set_use_markup (label, g_value_get_boolean (value));
912 case PROP_USE_UNDERLINE:
913 gtk_label_set_use_underline (label, g_value_get_boolean (value));
916 gtk_label_set_justify (label, g_value_get_enum (value));
919 gtk_label_set_pattern (label, g_value_get_string (value));
922 gtk_label_set_line_wrap (label, g_value_get_boolean (value));
925 gtk_label_set_line_wrap_mode (label, g_value_get_enum (value));
927 case PROP_SELECTABLE:
928 gtk_label_set_selectable (label, g_value_get_boolean (value));
930 case PROP_MNEMONIC_WIDGET:
931 gtk_label_set_mnemonic_widget (label, (GtkWidget*) g_value_get_object (value));
934 gtk_label_set_ellipsize (label, g_value_get_enum (value));
936 case PROP_WIDTH_CHARS:
937 gtk_label_set_width_chars (label, g_value_get_int (value));
939 case PROP_SINGLE_LINE_MODE:
940 gtk_label_set_single_line_mode (label, g_value_get_boolean (value));
943 gtk_label_set_angle (label, g_value_get_double (value));
945 case PROP_MAX_WIDTH_CHARS:
946 gtk_label_set_max_width_chars (label, g_value_get_int (value));
948 case PROP_TRACK_VISITED_LINKS:
949 gtk_label_set_track_visited_links (label, g_value_get_boolean (value));
952 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
958 gtk_label_get_property (GObject *object,
965 label = GTK_LABEL (object);
970 g_value_set_string (value, label->label);
972 case PROP_ATTRIBUTES:
973 g_value_set_boxed (value, label->attrs);
975 case PROP_USE_MARKUP:
976 g_value_set_boolean (value, label->use_markup);
978 case PROP_USE_UNDERLINE:
979 g_value_set_boolean (value, label->use_underline);
982 g_value_set_enum (value, label->jtype);
985 g_value_set_boolean (value, label->wrap);
988 g_value_set_enum (value, label->wrap_mode);
990 case PROP_SELECTABLE:
991 g_value_set_boolean (value, gtk_label_get_selectable (label));
993 case PROP_MNEMONIC_KEYVAL:
994 g_value_set_uint (value, label->mnemonic_keyval);
996 case PROP_MNEMONIC_WIDGET:
997 g_value_set_object (value, (GObject*) label->mnemonic_widget);
999 case PROP_CURSOR_POSITION:
1000 if (label->select_info && label->select_info->selectable)
1002 gint offset = g_utf8_pointer_to_offset (label->text,
1003 label->text + label->select_info->selection_end);
1004 g_value_set_int (value, offset);
1007 g_value_set_int (value, 0);
1009 case PROP_SELECTION_BOUND:
1010 if (label->select_info && label->select_info->selectable)
1012 gint offset = g_utf8_pointer_to_offset (label->text,
1013 label->text + label->select_info->selection_anchor);
1014 g_value_set_int (value, offset);
1017 g_value_set_int (value, 0);
1019 case PROP_ELLIPSIZE:
1020 g_value_set_enum (value, label->ellipsize);
1022 case PROP_WIDTH_CHARS:
1023 g_value_set_int (value, gtk_label_get_width_chars (label));
1025 case PROP_SINGLE_LINE_MODE:
1026 g_value_set_boolean (value, gtk_label_get_single_line_mode (label));
1029 g_value_set_double (value, gtk_label_get_angle (label));
1031 case PROP_MAX_WIDTH_CHARS:
1032 g_value_set_int (value, gtk_label_get_max_width_chars (label));
1034 case PROP_TRACK_VISITED_LINKS:
1035 g_value_set_boolean (value, gtk_label_get_track_visited_links (label));
1038 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1044 gtk_label_init (GtkLabel *label)
1046 GtkLabelPrivate *priv;
1048 gtk_widget_set_has_window (GTK_WIDGET (label), FALSE);
1050 priv = GTK_LABEL_GET_PRIVATE (label);
1051 priv->width_chars = -1;
1052 priv->max_width_chars = -1;
1053 priv->wrap_width = -1;
1054 label->label = NULL;
1056 label->jtype = GTK_JUSTIFY_LEFT;
1057 label->wrap = FALSE;
1058 label->wrap_mode = PANGO_WRAP_WORD;
1059 label->ellipsize = PANGO_ELLIPSIZE_NONE;
1061 label->use_underline = FALSE;
1062 label->use_markup = FALSE;
1063 label->pattern_set = FALSE;
1064 label->track_links = TRUE;
1066 label->mnemonic_keyval = GDK_VoidSymbol;
1067 label->layout = NULL;
1069 label->attrs = NULL;
1071 label->mnemonic_widget = NULL;
1072 label->mnemonic_window = NULL;
1074 priv->mnemonics_visible = TRUE;
1076 gtk_label_set_text (label, "");
1081 gtk_label_buildable_interface_init (GtkBuildableIface *iface)
1083 buildable_parent_iface = g_type_interface_peek_parent (iface);
1085 iface->custom_tag_start = gtk_label_buildable_custom_tag_start;
1086 iface->custom_finished = gtk_label_buildable_custom_finished;
1090 GtkBuilder *builder;
1092 PangoAttrList *attrs;
1095 static PangoAttribute *
1096 attribute_from_text (GtkBuilder *builder,
1101 PangoAttribute *attribute = NULL;
1103 PangoLanguage *language;
1104 PangoFontDescription *font_desc;
1106 GValue val = { 0, };
1108 if (!gtk_builder_value_from_string_type (builder, PANGO_TYPE_ATTR_TYPE, name, &val, error))
1111 type = g_value_get_enum (&val);
1112 g_value_unset (&val);
1116 /* PangoAttrLanguage */
1117 case PANGO_ATTR_LANGUAGE:
1118 if ((language = pango_language_from_string (value)))
1120 attribute = pango_attr_language_new (language);
1121 g_value_init (&val, G_TYPE_INT);
1125 case PANGO_ATTR_STYLE:
1126 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STYLE, value, &val, error))
1127 attribute = pango_attr_style_new (g_value_get_enum (&val));
1129 case PANGO_ATTR_WEIGHT:
1130 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_WEIGHT, value, &val, error))
1131 attribute = pango_attr_weight_new (g_value_get_enum (&val));
1133 case PANGO_ATTR_VARIANT:
1134 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_VARIANT, value, &val, error))
1135 attribute = pango_attr_variant_new (g_value_get_enum (&val));
1137 case PANGO_ATTR_STRETCH:
1138 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STRETCH, value, &val, error))
1139 attribute = pango_attr_stretch_new (g_value_get_enum (&val));
1141 case PANGO_ATTR_UNDERLINE:
1142 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1143 attribute = pango_attr_underline_new (g_value_get_boolean (&val));
1145 case PANGO_ATTR_STRIKETHROUGH:
1146 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1147 attribute = pango_attr_strikethrough_new (g_value_get_boolean (&val));
1149 case PANGO_ATTR_GRAVITY:
1150 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY, value, &val, error))
1151 attribute = pango_attr_gravity_new (g_value_get_enum (&val));
1153 case PANGO_ATTR_GRAVITY_HINT:
1154 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY_HINT,
1155 value, &val, error))
1156 attribute = pango_attr_gravity_hint_new (g_value_get_enum (&val));
1158 /* PangoAttrString */
1159 case PANGO_ATTR_FAMILY:
1160 attribute = pango_attr_family_new (value);
1161 g_value_init (&val, G_TYPE_INT);
1165 case PANGO_ATTR_SIZE:
1166 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1167 value, &val, error))
1168 attribute = pango_attr_size_new (g_value_get_int (&val));
1170 case PANGO_ATTR_ABSOLUTE_SIZE:
1171 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1172 value, &val, error))
1173 attribute = pango_attr_size_new_absolute (g_value_get_int (&val));
1176 /* PangoAttrFontDesc */
1177 case PANGO_ATTR_FONT_DESC:
1178 if ((font_desc = pango_font_description_from_string (value)))
1180 attribute = pango_attr_font_desc_new (font_desc);
1181 pango_font_description_free (font_desc);
1182 g_value_init (&val, G_TYPE_INT);
1186 /* PangoAttrColor */
1187 case PANGO_ATTR_FOREGROUND:
1188 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1189 value, &val, error))
1191 color = g_value_get_boxed (&val);
1192 attribute = pango_attr_foreground_new (color->red, color->green, color->blue);
1195 case PANGO_ATTR_BACKGROUND:
1196 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1197 value, &val, error))
1199 color = g_value_get_boxed (&val);
1200 attribute = pango_attr_background_new (color->red, color->green, color->blue);
1203 case PANGO_ATTR_UNDERLINE_COLOR:
1204 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1205 value, &val, error))
1207 color = g_value_get_boxed (&val);
1208 attribute = pango_attr_underline_color_new (color->red, color->green, color->blue);
1211 case PANGO_ATTR_STRIKETHROUGH_COLOR:
1212 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1213 value, &val, error))
1215 color = g_value_get_boxed (&val);
1216 attribute = pango_attr_strikethrough_color_new (color->red, color->green, color->blue);
1220 /* PangoAttrShape */
1221 case PANGO_ATTR_SHAPE:
1222 /* Unsupported for now */
1224 /* PangoAttrFloat */
1225 case PANGO_ATTR_SCALE:
1226 if (gtk_builder_value_from_string_type (builder, G_TYPE_DOUBLE,
1227 value, &val, error))
1228 attribute = pango_attr_scale_new (g_value_get_double (&val));
1231 case PANGO_ATTR_INVALID:
1232 case PANGO_ATTR_LETTER_SPACING:
1233 case PANGO_ATTR_RISE:
1234 case PANGO_ATTR_FALLBACK:
1239 g_value_unset (&val);
1246 pango_start_element (GMarkupParseContext *context,
1247 const gchar *element_name,
1248 const gchar **names,
1249 const gchar **values,
1253 PangoParserData *data = (PangoParserData*)user_data;
1254 GValue val = { 0, };
1256 gint line_number, char_number;
1258 if (strcmp (element_name, "attribute") == 0)
1260 PangoAttribute *attr = NULL;
1261 const gchar *name = NULL;
1262 const gchar *value = NULL;
1263 const gchar *start = NULL;
1264 const gchar *end = NULL;
1265 guint start_val = 0;
1266 guint end_val = G_MAXUINT;
1268 for (i = 0; names[i]; i++)
1270 if (strcmp (names[i], "name") == 0)
1272 else if (strcmp (names[i], "value") == 0)
1274 else if (strcmp (names[i], "start") == 0)
1276 else if (strcmp (names[i], "end") == 0)
1280 g_markup_parse_context_get_position (context,
1285 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1286 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1288 line_number, char_number, names[i], "attribute");
1293 if (!name || !value)
1295 g_markup_parse_context_get_position (context,
1300 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1301 "%s:%d:%d <%s> requires attribute \"%s\"",
1303 line_number, char_number, "attribute",
1304 name ? "value" : "name");
1310 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1311 start, &val, error))
1313 start_val = g_value_get_uint (&val);
1314 g_value_unset (&val);
1319 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1322 end_val = g_value_get_uint (&val);
1323 g_value_unset (&val);
1326 attr = attribute_from_text (data->builder, name, value, error);
1327 attr->start_index = start_val;
1328 attr->end_index = end_val;
1333 data->attrs = pango_attr_list_new ();
1335 pango_attr_list_insert (data->attrs, attr);
1338 else if (strcmp (element_name, "attributes") == 0)
1341 g_warning ("Unsupported tag for GtkLabel: %s\n", element_name);
1344 static const GMarkupParser pango_parser =
1346 pango_start_element,
1350 gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
1351 GtkBuilder *builder,
1353 const gchar *tagname,
1354 GMarkupParser *parser,
1357 if (buildable_parent_iface->custom_tag_start (buildable, builder, child,
1358 tagname, parser, data))
1361 if (strcmp (tagname, "attributes") == 0)
1363 PangoParserData *parser_data;
1365 parser_data = g_slice_new0 (PangoParserData);
1366 parser_data->builder = g_object_ref (builder);
1367 parser_data->object = g_object_ref (buildable);
1368 *parser = pango_parser;
1369 *data = parser_data;
1376 gtk_label_buildable_custom_finished (GtkBuildable *buildable,
1377 GtkBuilder *builder,
1379 const gchar *tagname,
1382 PangoParserData *data;
1384 buildable_parent_iface->custom_finished (buildable, builder, child,
1385 tagname, user_data);
1387 if (strcmp (tagname, "attributes") == 0)
1389 data = (PangoParserData*)user_data;
1393 gtk_label_set_attributes (GTK_LABEL (buildable), data->attrs);
1394 pango_attr_list_unref (data->attrs);
1397 g_object_unref (data->object);
1398 g_object_unref (data->builder);
1399 g_slice_free (PangoParserData, data);
1406 * @str: The text of the label
1408 * Creates a new label with the given text inside it. You can
1409 * pass %NULL to get an empty label widget.
1411 * Return value: the new #GtkLabel
1414 gtk_label_new (const gchar *str)
1418 label = g_object_new (GTK_TYPE_LABEL, NULL);
1421 gtk_label_set_text (label, str);
1423 return GTK_WIDGET (label);
1427 * gtk_label_new_with_mnemonic:
1428 * @str: The text of the label, with an underscore in front of the
1429 * mnemonic character
1431 * Creates a new #GtkLabel, containing the text in @str.
1433 * If characters in @str are preceded by an underscore, they are
1434 * underlined. If you need a literal underscore character in a label, use
1435 * '__' (two underscores). The first underlined character represents a
1436 * keyboard accelerator called a mnemonic. The mnemonic key can be used
1437 * to activate another widget, chosen automatically, or explicitly using
1438 * gtk_label_set_mnemonic_widget().
1440 * If gtk_label_set_mnemonic_widget() is not called, then the first
1441 * activatable ancestor of the #GtkLabel will be chosen as the mnemonic
1442 * widget. For instance, if the label is inside a button or menu item,
1443 * the button or menu item will automatically become the mnemonic widget
1444 * and be activated by the mnemonic.
1446 * Return value: the new #GtkLabel
1449 gtk_label_new_with_mnemonic (const gchar *str)
1453 label = g_object_new (GTK_TYPE_LABEL, NULL);
1456 gtk_label_set_text_with_mnemonic (label, str);
1458 return GTK_WIDGET (label);
1462 gtk_label_mnemonic_activate (GtkWidget *widget,
1463 gboolean group_cycling)
1467 if (GTK_LABEL (widget)->mnemonic_widget)
1468 return gtk_widget_mnemonic_activate (GTK_LABEL (widget)->mnemonic_widget, group_cycling);
1470 /* Try to find the widget to activate by traversing the
1471 * widget's ancestry.
1473 parent = widget->parent;
1475 if (GTK_IS_NOTEBOOK (parent))
1480 if (gtk_widget_get_can_focus (parent) ||
1481 (!group_cycling && GTK_WIDGET_GET_CLASS (parent)->activate_signal) ||
1482 GTK_IS_NOTEBOOK (parent->parent) ||
1483 GTK_IS_MENU_ITEM (parent))
1484 return gtk_widget_mnemonic_activate (parent, group_cycling);
1485 parent = parent->parent;
1488 /* barf if there was nothing to activate */
1489 g_warning ("Couldn't find a target for a mnemonic activation.");
1490 gtk_widget_error_bell (widget);
1496 gtk_label_setup_mnemonic (GtkLabel *label,
1499 GtkWidget *widget = GTK_WIDGET (label);
1500 GtkWidget *toplevel;
1501 GtkWidget *mnemonic_menu;
1503 mnemonic_menu = g_object_get_data (G_OBJECT (label), "gtk-mnemonic-menu");
1505 if (last_key != GDK_VoidSymbol)
1507 if (label->mnemonic_window)
1509 gtk_window_remove_mnemonic (label->mnemonic_window,
1512 label->mnemonic_window = NULL;
1516 _gtk_menu_shell_remove_mnemonic (GTK_MENU_SHELL (mnemonic_menu),
1519 mnemonic_menu = NULL;
1523 if (label->mnemonic_keyval == GDK_VoidSymbol)
1526 connect_mnemonics_visible_notify (GTK_LABEL (widget));
1528 toplevel = gtk_widget_get_toplevel (widget);
1529 if (gtk_widget_is_toplevel (toplevel))
1531 GtkWidget *menu_shell;
1533 menu_shell = gtk_widget_get_ancestor (widget,
1534 GTK_TYPE_MENU_SHELL);
1538 _gtk_menu_shell_add_mnemonic (GTK_MENU_SHELL (menu_shell),
1539 label->mnemonic_keyval,
1541 mnemonic_menu = menu_shell;
1544 if (!GTK_IS_MENU (menu_shell))
1546 gtk_window_add_mnemonic (GTK_WINDOW (toplevel),
1547 label->mnemonic_keyval,
1549 label->mnemonic_window = GTK_WINDOW (toplevel);
1554 g_object_set_data (G_OBJECT (label), I_("gtk-mnemonic-menu"), mnemonic_menu);
1558 gtk_label_hierarchy_changed (GtkWidget *widget,
1559 GtkWidget *old_toplevel)
1561 GtkLabel *label = GTK_LABEL (widget);
1563 gtk_label_setup_mnemonic (label, label->mnemonic_keyval);
1567 label_shortcut_setting_apply (GtkLabel *label)
1569 gtk_label_recalculate (label);
1570 if (GTK_IS_ACCEL_LABEL (label))
1571 gtk_accel_label_refetch (GTK_ACCEL_LABEL (label));
1575 label_shortcut_setting_traverse_container (GtkWidget *widget,
1578 if (GTK_IS_LABEL (widget))
1579 label_shortcut_setting_apply (GTK_LABEL (widget));
1580 else if (GTK_IS_CONTAINER (widget))
1581 gtk_container_forall (GTK_CONTAINER (widget),
1582 label_shortcut_setting_traverse_container, data);
1586 label_shortcut_setting_changed (GtkSettings *settings)
1590 list = gtk_window_list_toplevels ();
1592 for (l = list; l ; l = l->next)
1594 GtkWidget *widget = l->data;
1596 if (gtk_widget_get_settings (widget) == settings)
1597 gtk_container_forall (GTK_CONTAINER (widget),
1598 label_shortcut_setting_traverse_container, NULL);
1605 mnemonics_visible_apply (GtkWidget *widget,
1606 gboolean mnemonics_visible)
1609 GtkLabelPrivate *priv;
1611 label = GTK_LABEL (widget);
1613 priv = GTK_LABEL_GET_PRIVATE (label);
1615 mnemonics_visible = mnemonics_visible != FALSE;
1617 if (priv->mnemonics_visible != mnemonics_visible)
1619 priv->mnemonics_visible = mnemonics_visible;
1621 gtk_label_recalculate (label);
1626 label_mnemonics_visible_traverse_container (GtkWidget *widget,
1629 gboolean mnemonics_visible = GPOINTER_TO_INT (data);
1631 _gtk_label_mnemonics_visible_apply_recursively (widget, mnemonics_visible);
1635 _gtk_label_mnemonics_visible_apply_recursively (GtkWidget *widget,
1636 gboolean mnemonics_visible)
1638 if (GTK_IS_LABEL (widget))
1639 mnemonics_visible_apply (widget, mnemonics_visible);
1640 else if (GTK_IS_CONTAINER (widget))
1641 gtk_container_forall (GTK_CONTAINER (widget),
1642 label_mnemonics_visible_traverse_container,
1643 GINT_TO_POINTER (mnemonics_visible));
1647 label_mnemonics_visible_changed (GtkWindow *window,
1651 gboolean mnemonics_visible;
1653 g_object_get (window, "mnemonics-visible", &mnemonics_visible, NULL);
1655 gtk_container_forall (GTK_CONTAINER (window),
1656 label_mnemonics_visible_traverse_container,
1657 GINT_TO_POINTER (mnemonics_visible));
1661 gtk_label_screen_changed (GtkWidget *widget,
1662 GdkScreen *old_screen)
1664 GtkSettings *settings;
1665 gboolean shortcuts_connected;
1667 if (!gtk_widget_has_screen (widget))
1670 settings = gtk_widget_get_settings (widget);
1672 shortcuts_connected =
1673 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (settings),
1674 "gtk-label-shortcuts-connected"));
1676 if (! shortcuts_connected)
1678 g_signal_connect (settings, "notify::gtk-enable-mnemonics",
1679 G_CALLBACK (label_shortcut_setting_changed),
1681 g_signal_connect (settings, "notify::gtk-enable-accels",
1682 G_CALLBACK (label_shortcut_setting_changed),
1685 g_object_set_data (G_OBJECT (settings), "gtk-label-shortcuts-connected",
1686 GINT_TO_POINTER (TRUE));
1689 label_shortcut_setting_apply (GTK_LABEL (widget));
1694 label_mnemonic_widget_weak_notify (gpointer data,
1695 GObject *where_the_object_was)
1697 GtkLabel *label = data;
1699 label->mnemonic_widget = NULL;
1700 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1704 * gtk_label_set_mnemonic_widget:
1705 * @label: a #GtkLabel
1706 * @widget: (allow-none): the target #GtkWidget
1708 * If the label has been set so that it has an mnemonic key (using
1709 * i.e. gtk_label_set_markup_with_mnemonic(),
1710 * gtk_label_set_text_with_mnemonic(), gtk_label_new_with_mnemonic()
1711 * or the "use_underline" property) the label can be associated with a
1712 * widget that is the target of the mnemonic. When the label is inside
1713 * a widget (like a #GtkButton or a #GtkNotebook tab) it is
1714 * automatically associated with the correct widget, but sometimes
1715 * (i.e. when the target is a #GtkEntry next to the label) you need to
1716 * set it explicitly using this function.
1718 * The target widget will be accelerated by emitting the
1719 * GtkWidget::mnemonic-activate signal on it. The default handler for
1720 * this signal will activate the widget if there are no mnemonic collisions
1721 * and toggle focus between the colliding widgets otherwise.
1724 gtk_label_set_mnemonic_widget (GtkLabel *label,
1727 g_return_if_fail (GTK_IS_LABEL (label));
1729 g_return_if_fail (GTK_IS_WIDGET (widget));
1731 if (label->mnemonic_widget)
1733 gtk_widget_remove_mnemonic_label (label->mnemonic_widget, GTK_WIDGET (label));
1734 g_object_weak_unref (G_OBJECT (label->mnemonic_widget),
1735 label_mnemonic_widget_weak_notify,
1738 label->mnemonic_widget = widget;
1739 if (label->mnemonic_widget)
1741 g_object_weak_ref (G_OBJECT (label->mnemonic_widget),
1742 label_mnemonic_widget_weak_notify,
1744 gtk_widget_add_mnemonic_label (label->mnemonic_widget, GTK_WIDGET (label));
1747 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1751 * gtk_label_get_mnemonic_widget:
1752 * @label: a #GtkLabel
1754 * Retrieves the target of the mnemonic (keyboard shortcut) of this
1755 * label. See gtk_label_set_mnemonic_widget().
1757 * Return value: the target of the label's mnemonic, or %NULL if none
1758 * has been set and the default algorithm will be used.
1761 gtk_label_get_mnemonic_widget (GtkLabel *label)
1763 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1765 return label->mnemonic_widget;
1769 * gtk_label_get_mnemonic_keyval:
1770 * @label: a #GtkLabel
1772 * If the label has been set so that it has an mnemonic key this function
1773 * returns the keyval used for the mnemonic accelerator. If there is no
1774 * mnemonic set up it returns #GDK_VoidSymbol.
1776 * Returns: GDK keyval usable for accelerators, or #GDK_VoidSymbol
1779 gtk_label_get_mnemonic_keyval (GtkLabel *label)
1781 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_VoidSymbol);
1783 return label->mnemonic_keyval;
1787 gtk_label_set_text_internal (GtkLabel *label,
1790 g_free (label->text);
1794 gtk_label_select_region_index (label, 0, 0);
1798 gtk_label_set_label_internal (GtkLabel *label,
1801 g_free (label->label);
1805 g_object_notify (G_OBJECT (label), "label");
1809 gtk_label_set_use_markup_internal (GtkLabel *label,
1813 if (label->use_markup != val)
1815 label->use_markup = val;
1817 g_object_notify (G_OBJECT (label), "use-markup");
1822 gtk_label_set_use_underline_internal (GtkLabel *label,
1826 if (label->use_underline != val)
1828 label->use_underline = val;
1830 g_object_notify (G_OBJECT (label), "use-underline");
1835 gtk_label_compose_effective_attrs (GtkLabel *label)
1837 PangoAttrIterator *iter;
1838 PangoAttribute *attr;
1839 GSList *iter_attrs, *l;
1843 if (label->effective_attrs)
1845 if ((iter = pango_attr_list_get_iterator (label->attrs)))
1849 iter_attrs = pango_attr_iterator_get_attrs (iter);
1850 for (l = iter_attrs; l; l = l->next)
1853 pango_attr_list_insert (label->effective_attrs, attr);
1855 g_slist_free (iter_attrs);
1857 while (pango_attr_iterator_next (iter));
1858 pango_attr_iterator_destroy (iter);
1862 label->effective_attrs =
1863 pango_attr_list_ref (label->attrs);
1868 gtk_label_set_attributes_internal (GtkLabel *label,
1869 PangoAttrList *attrs)
1872 pango_attr_list_ref (attrs);
1875 pango_attr_list_unref (label->attrs);
1876 label->attrs = attrs;
1878 g_object_notify (G_OBJECT (label), "attributes");
1882 /* Calculates text, attrs and mnemonic_keyval from
1883 * label, use_underline and use_markup
1886 gtk_label_recalculate (GtkLabel *label)
1888 guint keyval = label->mnemonic_keyval;
1890 if (label->use_markup)
1891 gtk_label_set_markup_internal (label, label->label, label->use_underline);
1894 if (label->use_underline)
1895 gtk_label_set_uline_text_internal (label, label->label);
1898 if (label->effective_attrs)
1899 pango_attr_list_unref (label->effective_attrs);
1900 label->effective_attrs = NULL;
1901 gtk_label_set_text_internal (label, g_strdup (label->label));
1905 gtk_label_compose_effective_attrs (label);
1907 if (!label->use_underline)
1908 label->mnemonic_keyval = GDK_VoidSymbol;
1910 if (keyval != label->mnemonic_keyval)
1912 gtk_label_setup_mnemonic (label, keyval);
1913 g_object_notify (G_OBJECT (label), "mnemonic-keyval");
1916 gtk_label_clear_layout (label);
1917 gtk_label_clear_select_info (label);
1918 gtk_widget_queue_resize (GTK_WIDGET (label));
1922 * gtk_label_set_text:
1923 * @label: a #GtkLabel
1924 * @str: The text you want to set
1926 * Sets the text within the #GtkLabel widget. It overwrites any text that
1929 * This will also clear any previously set mnemonic accelerators.
1932 gtk_label_set_text (GtkLabel *label,
1935 g_return_if_fail (GTK_IS_LABEL (label));
1937 g_object_freeze_notify (G_OBJECT (label));
1939 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
1940 gtk_label_set_use_markup_internal (label, FALSE);
1941 gtk_label_set_use_underline_internal (label, FALSE);
1943 gtk_label_recalculate (label);
1945 g_object_thaw_notify (G_OBJECT (label));
1949 * gtk_label_set_attributes:
1950 * @label: a #GtkLabel
1951 * @attrs: a #PangoAttrList
1953 * Sets a #PangoAttrList; the attributes in the list are applied to the
1956 * <note><para>The attributes set with this function will be applied
1957 * and merged with any other attributes previously effected by way
1958 * of the #GtkLabel:use-underline or #GtkLabel:use-markup properties.
1959 * While it is not recommended to mix markup strings with manually set
1960 * attributes, if you must; know that the attributes will be applied
1961 * to the label after the markup string is parsed.</para></note>
1964 gtk_label_set_attributes (GtkLabel *label,
1965 PangoAttrList *attrs)
1967 g_return_if_fail (GTK_IS_LABEL (label));
1969 gtk_label_set_attributes_internal (label, attrs);
1971 gtk_label_recalculate (label);
1973 gtk_label_clear_layout (label);
1974 gtk_widget_queue_resize (GTK_WIDGET (label));
1978 * gtk_label_get_attributes:
1979 * @label: a #GtkLabel
1981 * Gets the attribute list that was set on the label using
1982 * gtk_label_set_attributes(), if any. This function does
1983 * not reflect attributes that come from the labels markup
1984 * (see gtk_label_set_markup()). If you want to get the
1985 * effective attributes for the label, use
1986 * pango_layout_get_attribute (gtk_label_get_layout (label)).
1988 * Return value: the attribute list, or %NULL if none was set.
1991 gtk_label_get_attributes (GtkLabel *label)
1993 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1995 return label->attrs;
1999 * gtk_label_set_label:
2000 * @label: a #GtkLabel
2001 * @str: the new text to set for the label
2003 * Sets the text of the label. The label is interpreted as
2004 * including embedded underlines and/or Pango markup depending
2005 * on the values of the #GtkLabel:use-underline" and
2006 * #GtkLabel:use-markup properties.
2009 gtk_label_set_label (GtkLabel *label,
2012 g_return_if_fail (GTK_IS_LABEL (label));
2014 g_object_freeze_notify (G_OBJECT (label));
2016 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2017 gtk_label_recalculate (label);
2019 g_object_thaw_notify (G_OBJECT (label));
2023 * gtk_label_get_label:
2024 * @label: a #GtkLabel
2026 * Fetches the text from a label widget including any embedded
2027 * underlines indicating mnemonics and Pango markup. (See
2028 * gtk_label_get_text()).
2030 * Return value: the text of the label widget. This string is
2031 * owned by the widget and must not be modified or freed.
2033 G_CONST_RETURN gchar *
2034 gtk_label_get_label (GtkLabel *label)
2036 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2038 return label->label;
2046 GdkColor *link_color;
2047 GdkColor *visited_link_color;
2051 start_element_handler (GMarkupParseContext *context,
2052 const gchar *element_name,
2053 const gchar **attribute_names,
2054 const gchar **attribute_values,
2058 UriParserData *pdata = user_data;
2060 if (strcmp (element_name, "a") == 0)
2063 const gchar *uri = NULL;
2064 const gchar *title = NULL;
2065 gboolean visited = FALSE;
2069 GdkColor *color = NULL;
2071 g_markup_parse_context_get_position (context, &line_number, &char_number);
2073 for (i = 0; attribute_names[i] != NULL; i++)
2075 const gchar *attr = attribute_names[i];
2077 if (strcmp (attr, "href") == 0)
2078 uri = attribute_values[i];
2079 else if (strcmp (attr, "title") == 0)
2080 title = attribute_values[i];
2085 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
2086 "Attribute '%s' is not allowed on the <a> tag "
2087 "on line %d char %d",
2088 attr, line_number, char_number);
2097 G_MARKUP_ERROR_INVALID_CONTENT,
2098 "Attribute 'href' was missing on the <a> tag "
2099 "on line %d char %d",
2100 line_number, char_number);
2105 if (pdata->label->track_links && pdata->label->select_info)
2108 for (l = pdata->label->select_info->links; l; l = l->next)
2111 if (strcmp (uri, link->uri) == 0)
2113 visited = link->visited;
2120 color = pdata->visited_link_color;
2122 color = pdata->link_color;
2124 g_string_append_printf (pdata->new_str,
2125 "<span color=\"#%04x%04x%04x\" underline=\"single\">",
2130 link = g_new0 (GtkLabelLink, 1);
2131 link->uri = g_strdup (uri);
2132 link->title = g_strdup (title);
2133 link->visited = visited;
2134 pdata->links = g_list_append (pdata->links, link);
2140 g_string_append_c (pdata->new_str, '<');
2141 g_string_append (pdata->new_str, element_name);
2143 for (i = 0; attribute_names[i] != NULL; i++)
2145 const gchar *attr = attribute_names[i];
2146 const gchar *value = attribute_values[i];
2149 newvalue = g_markup_escape_text (value, -1);
2151 g_string_append_c (pdata->new_str, ' ');
2152 g_string_append (pdata->new_str, attr);
2153 g_string_append (pdata->new_str, "=\"");
2154 g_string_append (pdata->new_str, newvalue);
2155 g_string_append_c (pdata->new_str, '\"');
2159 g_string_append_c (pdata->new_str, '>');
2164 end_element_handler (GMarkupParseContext *context,
2165 const gchar *element_name,
2169 UriParserData *pdata = user_data;
2171 if (!strcmp (element_name, "a"))
2172 g_string_append (pdata->new_str, "</span>");
2175 g_string_append (pdata->new_str, "</");
2176 g_string_append (pdata->new_str, element_name);
2177 g_string_append_c (pdata->new_str, '>');
2182 text_handler (GMarkupParseContext *context,
2188 UriParserData *pdata = user_data;
2191 newtext = g_markup_escape_text (text, text_len);
2192 g_string_append (pdata->new_str, newtext);
2196 static const GMarkupParser markup_parser =
2198 start_element_handler,
2199 end_element_handler,
2206 xml_isspace (gchar c)
2208 return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
2212 link_free (GtkLabelLink *link)
2215 g_free (link->title);
2220 gtk_label_get_link_colors (GtkWidget *widget,
2221 GdkColor **link_color,
2222 GdkColor **visited_link_color)
2224 gtk_widget_ensure_style (widget);
2225 gtk_widget_style_get (widget,
2226 "link-color", link_color,
2227 "visited-link-color", visited_link_color,
2230 *link_color = gdk_color_copy (&default_link_color);
2231 if (!*visited_link_color)
2232 *visited_link_color = gdk_color_copy (&default_visited_link_color);
2236 parse_uri_markup (GtkLabel *label,
2242 GMarkupParseContext *context = NULL;
2243 const gchar *p, *end;
2244 gboolean needs_root = TRUE;
2246 UriParserData pdata;
2248 length = strlen (str);
2252 pdata.label = label;
2254 pdata.new_str = g_string_sized_new (length);
2256 gtk_label_get_link_colors (GTK_WIDGET (label), &pdata.link_color, &pdata.visited_link_color);
2258 while (p != end && xml_isspace (*p))
2261 if (end - p >= 8 && strncmp (p, "<markup>", 8) == 0)
2264 context = g_markup_parse_context_new (&markup_parser, 0, &pdata, NULL);
2268 if (!g_markup_parse_context_parse (context, "<markup>", -1, error))
2272 if (!g_markup_parse_context_parse (context, str, length, error))
2277 if (!g_markup_parse_context_parse (context, "</markup>", -1, error))
2281 if (!g_markup_parse_context_end_parse (context, error))
2284 g_markup_parse_context_free (context);
2286 *new_str = g_string_free (pdata.new_str, FALSE);
2287 *links = pdata.links;
2289 gdk_color_free (pdata.link_color);
2290 gdk_color_free (pdata.visited_link_color);
2295 g_markup_parse_context_free (context);
2296 g_string_free (pdata.new_str, TRUE);
2297 g_list_foreach (pdata.links, (GFunc)link_free, NULL);
2298 g_list_free (pdata.links);
2299 gdk_color_free (pdata.link_color);
2300 gdk_color_free (pdata.visited_link_color);
2306 gtk_label_ensure_has_tooltip (GtkLabel *label)
2309 gboolean has_tooltip = FALSE;
2311 for (l = label->select_info->links; l; l = l->next)
2313 GtkLabelLink *link = l->data;
2321 gtk_widget_set_has_tooltip (GTK_WIDGET (label), has_tooltip);
2325 gtk_label_set_markup_internal (GtkLabel *label,
2327 gboolean with_uline)
2329 GtkLabelPrivate *priv = GTK_LABEL_GET_PRIVATE (label);
2331 GError *error = NULL;
2332 PangoAttrList *attrs = NULL;
2333 gunichar accel_char = 0;
2335 GList *links = NULL;
2337 if (!parse_uri_markup (label, str, &new_str, &links, &error))
2339 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2341 g_error_free (error);
2345 gtk_label_clear_links (label);
2348 gtk_label_ensure_select_info (label);
2349 label->select_info->links = links;
2350 gtk_label_ensure_has_tooltip (label);
2355 gboolean enable_mnemonics;
2356 gboolean auto_mnemonics;
2358 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2359 "gtk-enable-mnemonics", &enable_mnemonics,
2360 "gtk-auto-mnemonics", &auto_mnemonics,
2363 if (!(enable_mnemonics && priv->mnemonics_visible &&
2365 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2366 (!label->mnemonic_widget ||
2367 gtk_widget_is_sensitive (label->mnemonic_widget))))))
2373 if (separate_uline_pattern (new_str, &key, &tmp, &pattern))
2382 if (!pango_parse_markup (new_str,
2384 with_uline ? '_' : 0,
2387 with_uline ? &accel_char : NULL,
2390 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2393 g_error_free (error);
2400 gtk_label_set_text_internal (label, text);
2404 if (label->effective_attrs)
2405 pango_attr_list_unref (label->effective_attrs);
2406 label->effective_attrs = attrs;
2409 if (accel_char != 0)
2410 label->mnemonic_keyval = gdk_keyval_to_lower (gdk_unicode_to_keyval (accel_char));
2412 label->mnemonic_keyval = GDK_VoidSymbol;
2416 * gtk_label_set_markup:
2417 * @label: a #GtkLabel
2418 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
2420 * Parses @str which is marked up with the <link
2421 * linkend="PangoMarkupFormat">Pango text markup language</link>, setting the
2422 * label's text and attribute list based on the parse results. If the @str is
2423 * external data, you may need to escape it with g_markup_escape_text() or
2424 * g_markup_printf_escaped()<!-- -->:
2428 * markup = g_markup_printf_escaped ("<span style=\"italic\">%s</span>", str);
2429 * gtk_label_set_markup (GTK_LABEL (label), markup);
2434 gtk_label_set_markup (GtkLabel *label,
2437 g_return_if_fail (GTK_IS_LABEL (label));
2439 g_object_freeze_notify (G_OBJECT (label));
2441 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2442 gtk_label_set_use_markup_internal (label, TRUE);
2443 gtk_label_set_use_underline_internal (label, FALSE);
2445 gtk_label_recalculate (label);
2447 g_object_thaw_notify (G_OBJECT (label));
2451 * gtk_label_set_markup_with_mnemonic:
2452 * @label: a #GtkLabel
2453 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
2455 * Parses @str which is marked up with the <link linkend="PangoMarkupFormat">Pango text markup language</link>,
2456 * setting the label's text and attribute list based on the parse results.
2457 * If characters in @str are preceded by an underscore, they are underlined
2458 * indicating that they represent a keyboard accelerator called a mnemonic.
2460 * The mnemonic key can be used to activate another widget, chosen
2461 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
2464 gtk_label_set_markup_with_mnemonic (GtkLabel *label,
2467 g_return_if_fail (GTK_IS_LABEL (label));
2469 g_object_freeze_notify (G_OBJECT (label));
2471 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2472 gtk_label_set_use_markup_internal (label, TRUE);
2473 gtk_label_set_use_underline_internal (label, TRUE);
2475 gtk_label_recalculate (label);
2477 g_object_thaw_notify (G_OBJECT (label));
2481 * gtk_label_get_text:
2482 * @label: a #GtkLabel
2484 * Fetches the text from a label widget, as displayed on the
2485 * screen. This does not include any embedded underlines
2486 * indicating mnemonics or Pango markup. (See gtk_label_get_label())
2488 * Return value: the text in the label widget. This is the internal
2489 * string used by the label, and must not be modified.
2491 G_CONST_RETURN gchar *
2492 gtk_label_get_text (GtkLabel *label)
2494 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2499 static PangoAttrList *
2500 gtk_label_pattern_to_attrs (GtkLabel *label,
2501 const gchar *pattern)
2504 const char *p = label->text;
2505 const char *q = pattern;
2506 PangoAttrList *attrs;
2508 attrs = pango_attr_list_new ();
2512 while (*p && *q && *q != '_')
2514 p = g_utf8_next_char (p);
2518 while (*p && *q && *q == '_')
2520 p = g_utf8_next_char (p);
2526 PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_LOW);
2527 attr->start_index = start - label->text;
2528 attr->end_index = p - label->text;
2530 pango_attr_list_insert (attrs, attr);
2540 gtk_label_set_pattern_internal (GtkLabel *label,
2541 const gchar *pattern,
2542 gboolean is_mnemonic)
2544 GtkLabelPrivate *priv = GTK_LABEL_GET_PRIVATE (label);
2545 PangoAttrList *attrs;
2546 gboolean enable_mnemonics;
2547 gboolean auto_mnemonics;
2549 g_return_if_fail (GTK_IS_LABEL (label));
2551 if (label->pattern_set)
2556 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2557 "gtk-enable-mnemonics", &enable_mnemonics,
2558 "gtk-auto-mnemonics", &auto_mnemonics,
2561 if (enable_mnemonics && priv->mnemonics_visible && pattern &&
2563 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2564 (!label->mnemonic_widget ||
2565 gtk_widget_is_sensitive (label->mnemonic_widget)))))
2566 attrs = gtk_label_pattern_to_attrs (label, pattern);
2571 attrs = gtk_label_pattern_to_attrs (label, pattern);
2573 if (label->effective_attrs)
2574 pango_attr_list_unref (label->effective_attrs);
2575 label->effective_attrs = attrs;
2579 gtk_label_set_pattern (GtkLabel *label,
2580 const gchar *pattern)
2582 g_return_if_fail (GTK_IS_LABEL (label));
2584 label->pattern_set = FALSE;
2588 gtk_label_set_pattern_internal (label, pattern, FALSE);
2589 label->pattern_set = TRUE;
2592 gtk_label_recalculate (label);
2594 gtk_label_clear_layout (label);
2595 gtk_widget_queue_resize (GTK_WIDGET (label));
2600 * gtk_label_set_justify:
2601 * @label: a #GtkLabel
2602 * @jtype: a #GtkJustification
2604 * Sets the alignment of the lines in the text of the label relative to
2605 * each other. %GTK_JUSTIFY_LEFT is the default value when the
2606 * widget is first created with gtk_label_new(). If you instead want
2607 * to set the alignment of the label as a whole, use
2608 * gtk_misc_set_alignment() instead. gtk_label_set_justify() has no
2609 * effect on labels containing only a single line.
2612 gtk_label_set_justify (GtkLabel *label,
2613 GtkJustification jtype)
2615 g_return_if_fail (GTK_IS_LABEL (label));
2616 g_return_if_fail (jtype >= GTK_JUSTIFY_LEFT && jtype <= GTK_JUSTIFY_FILL);
2618 if ((GtkJustification) label->jtype != jtype)
2620 label->jtype = jtype;
2622 /* No real need to be this drastic, but easier than duplicating the code */
2623 gtk_label_clear_layout (label);
2625 g_object_notify (G_OBJECT (label), "justify");
2626 gtk_widget_queue_resize (GTK_WIDGET (label));
2631 * gtk_label_get_justify:
2632 * @label: a #GtkLabel
2634 * Returns the justification of the label. See gtk_label_set_justify().
2636 * Return value: #GtkJustification
2639 gtk_label_get_justify (GtkLabel *label)
2641 g_return_val_if_fail (GTK_IS_LABEL (label), 0);
2643 return label->jtype;
2647 * gtk_label_set_ellipsize:
2648 * @label: a #GtkLabel
2649 * @mode: a #PangoEllipsizeMode
2651 * Sets the mode used to ellipsize (add an ellipsis: "...") to the text
2652 * if there is not enough space to render the entire string.
2657 gtk_label_set_ellipsize (GtkLabel *label,
2658 PangoEllipsizeMode mode)
2660 g_return_if_fail (GTK_IS_LABEL (label));
2661 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE && mode <= PANGO_ELLIPSIZE_END);
2663 if ((PangoEllipsizeMode) label->ellipsize != mode)
2665 label->ellipsize = mode;
2667 /* No real need to be this drastic, but easier than duplicating the code */
2668 gtk_label_clear_layout (label);
2670 g_object_notify (G_OBJECT (label), "ellipsize");
2671 gtk_widget_queue_resize (GTK_WIDGET (label));
2676 * gtk_label_get_ellipsize:
2677 * @label: a #GtkLabel
2679 * Returns the ellipsizing position of the label. See gtk_label_set_ellipsize().
2681 * Return value: #PangoEllipsizeMode
2686 gtk_label_get_ellipsize (GtkLabel *label)
2688 g_return_val_if_fail (GTK_IS_LABEL (label), PANGO_ELLIPSIZE_NONE);
2690 return label->ellipsize;
2694 * gtk_label_set_width_chars:
2695 * @label: a #GtkLabel
2696 * @n_chars: the new desired width, in characters.
2698 * Sets the desired width in characters of @label to @n_chars.
2703 gtk_label_set_width_chars (GtkLabel *label,
2706 GtkLabelPrivate *priv;
2708 g_return_if_fail (GTK_IS_LABEL (label));
2710 priv = GTK_LABEL_GET_PRIVATE (label);
2712 if (priv->width_chars != n_chars)
2714 priv->width_chars = n_chars;
2715 g_object_notify (G_OBJECT (label), "width-chars");
2716 gtk_label_invalidate_wrap_width (label);
2717 gtk_widget_queue_resize (GTK_WIDGET (label));
2722 * gtk_label_get_width_chars:
2723 * @label: a #GtkLabel
2725 * Retrieves the desired width of @label, in characters. See
2726 * gtk_label_set_width_chars().
2728 * Return value: the width of the label in characters.
2733 gtk_label_get_width_chars (GtkLabel *label)
2735 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2737 return GTK_LABEL_GET_PRIVATE (label)->width_chars;
2741 * gtk_label_set_max_width_chars:
2742 * @label: a #GtkLabel
2743 * @n_chars: the new desired maximum width, in characters.
2745 * Sets the desired maximum width in characters of @label to @n_chars.
2750 gtk_label_set_max_width_chars (GtkLabel *label,
2753 GtkLabelPrivate *priv;
2755 g_return_if_fail (GTK_IS_LABEL (label));
2757 priv = GTK_LABEL_GET_PRIVATE (label);
2759 if (priv->max_width_chars != n_chars)
2761 priv->max_width_chars = n_chars;
2763 g_object_notify (G_OBJECT (label), "max-width-chars");
2764 gtk_label_invalidate_wrap_width (label);
2765 gtk_widget_queue_resize (GTK_WIDGET (label));
2770 * gtk_label_get_max_width_chars:
2771 * @label: a #GtkLabel
2773 * Retrieves the desired maximum width of @label, in characters. See
2774 * gtk_label_set_width_chars().
2776 * Return value: the maximum width of the label in characters.
2781 gtk_label_get_max_width_chars (GtkLabel *label)
2783 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2785 return GTK_LABEL_GET_PRIVATE (label)->max_width_chars;
2789 * gtk_label_set_line_wrap:
2790 * @label: a #GtkLabel
2791 * @wrap: the setting
2793 * Toggles line wrapping within the #GtkLabel widget. %TRUE makes it break
2794 * lines if text exceeds the widget's size. %FALSE lets the text get cut off
2795 * by the edge of the widget if it exceeds the widget size.
2797 * Note that setting line wrapping to %TRUE does not make the label
2798 * wrap at its parent container's width, because GTK+ widgets
2799 * conceptually can't make their requisition depend on the parent
2800 * container's size. For a label that wraps at a specific position,
2801 * set the label's width using gtk_widget_set_size_request().
2804 gtk_label_set_line_wrap (GtkLabel *label,
2807 g_return_if_fail (GTK_IS_LABEL (label));
2809 wrap = wrap != FALSE;
2811 if (label->wrap != wrap)
2815 gtk_label_clear_layout (label);
2816 gtk_widget_queue_resize (GTK_WIDGET (label));
2817 g_object_notify (G_OBJECT (label), "wrap");
2822 * gtk_label_get_line_wrap:
2823 * @label: a #GtkLabel
2825 * Returns whether lines in the label are automatically wrapped.
2826 * See gtk_label_set_line_wrap().
2828 * Return value: %TRUE if the lines of the label are automatically wrapped.
2831 gtk_label_get_line_wrap (GtkLabel *label)
2833 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
2839 * gtk_label_set_line_wrap_mode:
2840 * @label: a #GtkLabel
2841 * @wrap_mode: the line wrapping mode
2843 * If line wrapping is on (see gtk_label_set_line_wrap()) this controls how
2844 * the line wrapping is done. The default is %PANGO_WRAP_WORD which means
2845 * wrap on word boundaries.
2850 gtk_label_set_line_wrap_mode (GtkLabel *label,
2851 PangoWrapMode wrap_mode)
2853 g_return_if_fail (GTK_IS_LABEL (label));
2855 if (label->wrap_mode != wrap_mode)
2857 label->wrap_mode = wrap_mode;
2858 g_object_notify (G_OBJECT (label), "wrap-mode");
2860 gtk_widget_queue_resize (GTK_WIDGET (label));
2865 * gtk_label_get_line_wrap_mode:
2866 * @label: a #GtkLabel
2868 * Returns line wrap mode used by the label. See gtk_label_set_line_wrap_mode().
2870 * Return value: %TRUE if the lines of the label are automatically wrapped.
2875 gtk_label_get_line_wrap_mode (GtkLabel *label)
2877 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
2879 return label->wrap_mode;
2884 gtk_label_get (GtkLabel *label,
2887 g_return_if_fail (GTK_IS_LABEL (label));
2888 g_return_if_fail (str != NULL);
2894 gtk_label_destroy (GtkObject *object)
2896 GtkLabel *label = GTK_LABEL (object);
2898 gtk_label_set_mnemonic_widget (label, NULL);
2900 GTK_OBJECT_CLASS (gtk_label_parent_class)->destroy (object);
2904 gtk_label_finalize (GObject *object)
2906 GtkLabel *label = GTK_LABEL (object);
2908 g_free (label->label);
2909 g_free (label->text);
2912 g_object_unref (label->layout);
2915 pango_attr_list_unref (label->attrs);
2917 if (label->effective_attrs)
2918 pango_attr_list_unref (label->effective_attrs);
2920 gtk_label_clear_links (label);
2921 g_free (label->select_info);
2923 G_OBJECT_CLASS (gtk_label_parent_class)->finalize (object);
2927 gtk_label_clear_layout (GtkLabel *label)
2931 g_object_unref (label->layout);
2932 label->layout = NULL;
2934 //gtk_label_clear_links (label);
2940 get_label_width (GtkLabel *label,
2944 GtkWidgetAuxInfo *aux_info;
2945 GtkLabelPrivate *priv;
2946 PangoLayout *layout;
2947 PangoContext *context;
2948 PangoFontMetrics *metrics;
2949 PangoRectangle rect;
2950 gint char_width, digit_width, char_pixels, text_width, ellipsize_chars, guess_width;
2952 priv = GTK_LABEL_GET_PRIVATE (label);
2953 aux_info = _gtk_widget_get_aux_info (GTK_WIDGET (label), FALSE);
2955 layout = pango_layout_copy (label->layout);
2956 context = pango_layout_get_context (layout);
2957 metrics = pango_context_get_metrics (context, GTK_WIDGET (label)->style->font_desc,
2958 pango_context_get_language (context));
2960 char_width = pango_font_metrics_get_approximate_char_width (metrics);
2961 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
2962 char_pixels = MAX (char_width, digit_width);
2963 pango_font_metrics_unref (metrics);
2965 /* Fetch the length of the complete unwrapped text */
2966 pango_layout_set_width (layout, -1);
2967 pango_layout_get_extents (layout, NULL, &rect);
2968 text_width = rect.width;
2970 /* Fetch the width that was guessed by gtk_label_ensure_layout() */
2971 pango_layout_get_extents (label->layout, NULL, &rect);
2972 guess_width = rect.width;
2974 /* enforce minimum width for ellipsized labels at ~3 chars */
2975 if (label->ellipsize)
2976 ellipsize_chars = 3;
2978 ellipsize_chars = 0;
2980 /* "width-chars" Hard-coded minimum width:
2981 * - minimum size should be MAX (width-chars, strlen ("..."));
2982 * - natural size should be MAX (width-chars, strlen (label->text));
2984 * "max-width-chars" User specified maximum size requisition
2985 * - minimum size should be MAX (width-chars, 0)
2986 * - natural size should be MIN (max-width-chars, strlen (label->text))
2988 * For ellipsizing labels; if max-width-chars is specified: either it is used as
2989 * a minimum size or the label text as a minimum size (natural size still overflows).
2991 * For wrapping labels; A reasonable minimum size is useful to naturally layout
2992 * interfaces automatically. In this case if no "width-chars" is specified, the minimum
2993 * width will default to the wrap guess that gtk_label_ensure_layout() does.
2995 * In *any* case the minimum width is completely overridden if an explicit width
2996 * request was provided.
2999 if (label->ellipsize || label->wrap)
3001 *minimum = char_pixels * MAX (priv->width_chars, ellipsize_chars);
3003 /* Default to the minimum width regularly guessed by GTK+ if no minimum
3004 * width was specified, only allow unwrapping of these labels.
3006 * Note that when specifying a small width_chars for a long text;
3007 * an accordingly large size will be required for the label height.
3009 if (label->wrap && priv->width_chars <= 0)
3010 *minimum = guess_width;
3012 if (priv->max_width_chars < 0)
3014 *natural = MAX (*minimum, text_width);
3018 gint max_char_width = char_pixels * priv->max_width_chars;
3019 gint max_width = MIN (text_width, max_char_width);
3021 /* With max-char-width specified, we let the minimum widths of
3022 * ellipsized text crawl up to the max-char-width
3023 * (note that we dont want to limit the minimum width for wrapping text).
3025 if (label->ellipsize)
3026 *minimum = MIN (text_width, max_width);
3028 *natural = MAX (*minimum, max_width);
3033 *minimum = text_width;
3034 *natural = *minimum;
3037 /* if a width-request is set, use that as the requested label width */
3038 if ((label->wrap || label->ellipsize || priv->width_chars > 0 || priv->max_width_chars > 0) &&
3039 aux_info && aux_info->width > 0)
3041 *minimum = aux_info->width * PANGO_SCALE;
3042 *natural = MAX (*natural, *minimum);
3045 g_object_unref (layout);
3049 gtk_label_invalidate_wrap_width (GtkLabel *label)
3051 GtkLabelPrivate *priv;
3053 priv = GTK_LABEL_GET_PRIVATE (label);
3055 priv->wrap_width = -1;
3059 get_label_wrap_width (GtkLabel *label)
3061 GtkLabelPrivate *priv;
3063 priv = GTK_LABEL_GET_PRIVATE (label);
3065 if (priv->wrap_width < 0)
3067 if (priv->width_chars > 0)
3069 PangoLayout *layout;
3070 PangoContext *context;
3071 PangoFontMetrics *metrics;
3072 PangoRectangle rect;
3073 gint char_width, digit_width, char_pixels, text_width;
3075 layout = pango_layout_copy (label->layout);
3076 context = pango_layout_get_context (layout);
3077 metrics = pango_context_get_metrics (context, GTK_WIDGET (label)->style->font_desc,
3078 pango_context_get_language (context));
3080 char_width = pango_font_metrics_get_approximate_char_width (metrics);
3081 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
3082 char_pixels = MAX (char_width, digit_width);
3083 pango_font_metrics_unref (metrics);
3085 pango_layout_set_width (layout, -1);
3086 pango_layout_get_extents (layout, NULL, &rect);
3087 g_object_unref (layout);
3089 text_width = rect.width;
3091 priv->wrap_width = PANGO_PIXELS (MAX (text_width, char_pixels * priv->width_chars));
3095 PangoLayout *layout;
3097 layout = gtk_widget_create_pango_layout (GTK_WIDGET (label),
3098 "This string is just about long enough.");
3099 pango_layout_get_size (layout, &priv->wrap_width, NULL);
3100 g_object_unref (layout);
3104 return priv->wrap_width;
3108 gtk_label_ensure_layout (GtkLabel *label, gboolean guess_wrap_width)
3111 PangoRectangle logical_rect;
3114 widget = GTK_WIDGET (label);
3116 rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
3120 PangoAlignment align = PANGO_ALIGN_LEFT; /* Quiet gcc */
3121 gdouble angle = gtk_label_get_angle (label);
3123 if (angle != 0.0 && !label->select_info)
3125 PangoMatrix matrix = PANGO_MATRIX_INIT;
3127 /* We rotate the standard singleton PangoContext for the widget,
3128 * depending on the fact that it's meant pretty much exclusively
3131 pango_matrix_rotate (&matrix, angle);
3133 pango_context_set_matrix (gtk_widget_get_pango_context (widget), &matrix);
3135 label->have_transform = TRUE;
3139 if (label->have_transform)
3140 pango_context_set_matrix (gtk_widget_get_pango_context (widget), NULL);
3142 label->have_transform = FALSE;
3145 label->layout = gtk_widget_create_pango_layout (widget, label->text);
3147 if (label->effective_attrs)
3148 pango_layout_set_attributes (label->layout, label->effective_attrs);
3150 gtk_label_rescan_links (label);
3152 switch (label->jtype)
3154 case GTK_JUSTIFY_LEFT:
3155 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3157 case GTK_JUSTIFY_RIGHT:
3158 align = rtl ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
3160 case GTK_JUSTIFY_CENTER:
3161 align = PANGO_ALIGN_CENTER;
3163 case GTK_JUSTIFY_FILL:
3164 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3165 pango_layout_set_justify (label->layout, TRUE);
3168 g_assert_not_reached();
3171 pango_layout_set_alignment (label->layout, align);
3172 pango_layout_set_ellipsize (label->layout, label->ellipsize);
3173 pango_layout_set_single_paragraph_mode (label->layout, label->single_line_mode);
3175 if (label->ellipsize)
3176 pango_layout_set_width (label->layout,
3177 widget->allocation.width * PANGO_SCALE);
3178 else if (label->wrap)
3180 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (widget, FALSE);
3181 gint longest_paragraph;
3185 if ((angle == 90 || angle == 270) && aux_info && aux_info->height > 0)
3186 aux_width = aux_info->height;
3187 else if (aux_info && aux_info->width > 0)
3188 aux_width = aux_info->width;
3191 pango_layout_set_width (label->layout, aux_width * PANGO_SCALE);
3192 else if (guess_wrap_width == FALSE &&
3193 widget->allocation.width > 1 && widget->allocation.height > 1)
3195 if (angle == 90 || angle == 270)
3196 width = widget->allocation.height - label->misc.ypad * 2;
3198 width = widget->allocation.width - label->misc.xpad * 2;
3200 pango_layout_set_wrap (label->layout, label->wrap_mode);
3201 pango_layout_set_width (label->layout, MAX (width, 1) * PANGO_SCALE);
3205 GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (label));
3208 pango_layout_set_width (label->layout, -1);
3209 pango_layout_get_extents (label->layout, NULL, &logical_rect);
3211 width = logical_rect.width;
3212 /* Try to guess a reasonable maximum width */
3213 longest_paragraph = width;
3215 wrap_width = get_label_wrap_width (label);
3216 width = MIN (width, wrap_width);
3218 PANGO_SCALE * (gdk_screen_get_width (screen) + 1) / 2);
3220 pango_layout_set_width (label->layout, width);
3221 pango_layout_get_extents (label->layout, NULL, &logical_rect);
3222 width = logical_rect.width;
3223 height = logical_rect.height;
3225 /* Unfortunately, the above may leave us with a very unbalanced looking paragraph,
3226 * so we try short search for a narrower width that leaves us with the same height
3228 if (longest_paragraph > 0)
3230 gint nlines, perfect_width;
3232 nlines = pango_layout_get_line_count (label->layout);
3233 perfect_width = (longest_paragraph + nlines - 1) / nlines;
3235 if (perfect_width < width)
3237 pango_layout_set_width (label->layout, perfect_width);
3238 pango_layout_get_extents (label->layout, NULL, &logical_rect);
3240 if (logical_rect.height <= height)
3241 width = logical_rect.width;
3244 gint mid_width = (perfect_width + width) / 2;
3246 if (mid_width > perfect_width)
3248 pango_layout_set_width (label->layout, mid_width);
3249 pango_layout_get_extents (label->layout, NULL, &logical_rect);
3251 if (logical_rect.height <= height)
3252 width = logical_rect.width;
3257 pango_layout_set_width (label->layout, width);
3260 else /* !label->wrap */
3261 pango_layout_set_width (label->layout, -1);
3266 get_single_line_height (GtkWidget *widget,
3267 PangoLayout *layout)
3269 PangoContext *context;
3270 PangoFontMetrics *metrics;
3271 gint ascent, descent;
3273 context = pango_layout_get_context (layout);
3274 metrics = pango_context_get_metrics (context, widget->style->font_desc,
3275 pango_context_get_language (context));
3277 ascent = pango_font_metrics_get_ascent (metrics);
3278 descent = pango_font_metrics_get_descent (metrics);
3279 pango_font_metrics_unref (metrics);
3281 return ascent + descent;
3285 gtk_label_extended_layout_init (GtkExtendedLayoutIface *iface)
3287 iface->is_height_for_width = gtk_label_is_height_for_width;
3288 iface->get_desired_width = gtk_label_get_desired_width;
3289 iface->get_desired_height = gtk_label_get_desired_height;
3290 iface->get_width_for_height = gtk_label_get_width_for_height;
3291 iface->get_height_for_width = gtk_label_get_height_for_width;
3295 gtk_label_is_height_for_width (GtkExtendedLayout *layout)
3297 GtkLabel *label = GTK_LABEL (layout);
3298 gdouble angle = gtk_label_get_angle (label);
3300 if (angle == 90 || angle == 270)
3307 get_size_for_allocation (GtkLabel *label,
3308 GtkOrientation orientation,
3313 PangoLayout *layout;
3314 GtkWidgetAuxInfo *aux_info =
3315 _gtk_widget_get_aux_info (GTK_WIDGET (label), FALSE);
3319 gtk_label_ensure_layout (label, FALSE);
3320 layout = pango_layout_copy (label->layout);
3324 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3325 aux_size = aux_info->width;
3327 aux_size = aux_info->height;
3333 pango_layout_set_width (layout, aux_size * PANGO_SCALE);
3335 pango_layout_set_width (layout, allocation * PANGO_SCALE);
3337 pango_layout_get_pixel_size (layout, NULL, &text_height);
3340 *minimum_size = text_height;
3343 *natural_size = text_height;
3345 g_object_unref (layout);
3349 gtk_label_get_desired_size (GtkExtendedLayout *layout,
3350 GtkOrientation orientation,
3354 GtkLabel *label = GTK_LABEL (layout);
3355 PangoRectangle required_rect;
3356 PangoRectangle natural_rect;
3359 /* "width-chars" Hard-coded minimum width:
3360 * - minimum size should be MAX (width-chars, strlen ("..."));
3361 * - natural size should be MAX (width-chars, strlen (label->text));
3363 * "max-width-chars" User specified maximum size requisition
3364 * - minimum size should be MAX (width-chars, 0)
3365 * - natural size should be MIN (max-width-chars, strlen (label->text))
3369 /* When calculating ->wrap sometimes we need to invent a size; Ideally we should be doing
3370 * that stuff here instead of inside gtk_label_ensure_layout() */
3372 gtk_label_clear_layout (label);
3373 gtk_label_ensure_layout (label, TRUE);
3375 angle = gtk_label_get_angle (label);
3377 /* Start off with the pixel extents of the rendered layout */
3378 pango_layout_get_extents (label->layout, NULL, &required_rect);
3379 required_rect.x = required_rect.y = 0;
3381 if (label->single_line_mode || label->wrap)
3382 required_rect.height = get_single_line_height (GTK_WIDGET (label), label->layout);
3384 natural_rect = required_rect;
3386 /* Calculate text width itself based on GtkLabel property rules */
3387 get_label_width (label, &required_rect.width, &natural_rect.width);
3389 /* Now that we have minimum and natural sizes in pango extents, apply a possible transform */
3390 if (label->have_transform)
3392 PangoLayout *layout = pango_layout_copy (label->layout);
3393 PangoContext *context = pango_layout_get_context (label->layout);
3394 const PangoMatrix *matrix = pango_context_get_matrix (context);
3396 pango_layout_set_width (layout, -1);
3397 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_NONE);
3399 pango_layout_get_extents (layout, NULL, &natural_rect);
3400 g_object_unref (layout);
3402 pango_matrix_transform_rectangle (matrix, &required_rect);
3403 pango_matrix_transform_rectangle (matrix, &natural_rect);
3405 /* Bump the natural size in case of ellipsize to ensure pango has
3406 * enough space in the angles (note, we could alternatively set the
3407 * layout to not ellipsize when we know we have been allocated our
3408 * full natural size, or it may be that pango needs a fix here).
3410 if (label->ellipsize && angle != 0 && angle != 90 && angle != 180 && angle != 270 && angle != 360)
3412 /* For some reason we only need this at about 110 degrees, and only
3413 * when gaining in height
3415 natural_rect.height += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3416 natural_rect.width += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3420 required_rect.width = PANGO_PIXELS_CEIL (required_rect.width);
3421 required_rect.height = PANGO_PIXELS_CEIL (required_rect.height);
3423 natural_rect.width = PANGO_PIXELS_CEIL (natural_rect.width);
3424 natural_rect.height = PANGO_PIXELS_CEIL (natural_rect.height);
3426 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3428 /* Note, we cant use get_size_for_allocation() when rotating
3429 * ellipsized labels.
3431 if (!(label->ellipsize && label->have_transform) &&
3432 (angle == 90 || angle == 270))
3434 /* Doing a h4w request on a rotated label here, return the
3435 * required width for the minimum height.
3437 get_size_for_allocation (label,
3438 GTK_ORIENTATION_VERTICAL,
3439 required_rect.height,
3440 minimum_size, natural_size);
3445 /* Normal desired width */
3446 *minimum_size = required_rect.width;
3447 *natural_size = natural_rect.width;
3450 *minimum_size += label->misc.xpad * 2;
3451 *natural_size += label->misc.xpad * 2;
3453 else /* GTK_ORIENTATION_VERTICAL */
3455 /* Note, we cant use get_size_for_allocation() when rotating
3456 * ellipsized labels.
3458 if (!(label->ellipsize && label->have_transform) &&
3459 (angle == 0 || angle == 180))
3461 /* Doing a w4h request on a label here, return the required
3462 * height for the minimum width.
3464 get_size_for_allocation (label,
3465 GTK_ORIENTATION_HORIZONTAL,
3466 required_rect.width,
3467 minimum_size, natural_size);
3471 /* A vertically rotated label does w4h, so return the base
3472 * desired height (text length)
3474 *minimum_size = required_rect.height;
3475 *natural_size = natural_rect.height;
3478 *minimum_size += label->misc.ypad * 2;
3479 *natural_size += label->misc.ypad * 2;
3482 /* Restore real allocated size of layout; sometimes size-requests
3483 * are randomly called without a following allocation; for this case
3484 * we need to make sure we dont have a mucked up layout because we
3485 * went and guessed the wrap-size.
3488 gtk_label_clear_layout (label);
3489 gtk_label_ensure_layout (label, FALSE);
3495 gtk_label_get_desired_width (GtkExtendedLayout *layout,
3499 gtk_label_get_desired_size (layout,
3500 GTK_ORIENTATION_HORIZONTAL,
3501 minimum_size, natural_size);
3505 gtk_label_get_desired_height (GtkExtendedLayout *layout,
3509 gtk_label_get_desired_size (layout,
3510 GTK_ORIENTATION_VERTICAL,
3511 minimum_size, natural_size);
3515 gtk_label_get_width_for_height (GtkExtendedLayout *layout,
3517 gint *minimum_width,
3518 gint *natural_width)
3520 GtkLabel *label = GTK_LABEL (layout);
3521 gdouble angle = gtk_label_get_angle (label);
3523 if (label->wrap && (angle == 90 || angle == 270))
3526 gtk_label_clear_layout (label);
3528 get_size_for_allocation (label, GTK_ORIENTATION_VERTICAL,
3529 MAX (1, height - (label->misc.ypad * 2)),
3530 minimum_width, natural_width);
3533 *minimum_width += label->misc.xpad * 2;
3536 *natural_width += label->misc.xpad * 2;
3539 GTK_EXTENDED_LAYOUT_GET_IFACE (layout)->get_desired_width (layout, minimum_width, natural_width);
3543 gtk_label_get_height_for_width (GtkExtendedLayout *layout,
3545 gint *minimum_height,
3546 gint *natural_height)
3548 GtkLabel *label = GTK_LABEL (layout);
3549 gdouble angle = gtk_label_get_angle (label);
3551 if (label->wrap && (angle == 0 || angle == 180 || angle == 360))
3554 gtk_label_clear_layout (label);
3556 get_size_for_allocation (label, GTK_ORIENTATION_HORIZONTAL,
3557 MAX (1, width - label->misc.xpad * 2),
3558 minimum_height, natural_height);
3561 *minimum_height += label->misc.ypad * 2;
3564 *natural_height += label->misc.ypad * 2;
3567 GTK_EXTENDED_LAYOUT_GET_IFACE (layout)->get_desired_height (layout, minimum_height, natural_height);
3571 gtk_label_size_allocate (GtkWidget *widget,
3572 GtkAllocation *allocation)
3576 label = GTK_LABEL (widget);
3578 GTK_WIDGET_CLASS (gtk_label_parent_class)->size_allocate (widget, allocation);
3580 /* The layout may have been recently cleared in get_size_for_orientation(),
3581 * but the width at that point may not be the same as the allocated width
3584 gtk_label_clear_layout (label);
3586 gtk_label_ensure_layout (label, FALSE);
3588 if (label->ellipsize)
3592 PangoRectangle logical;
3593 PangoRectangle bounds;
3595 bounds.x = bounds.y = 0;
3596 bounds.width = allocation->width - label->misc.xpad * 2;
3597 bounds.height = allocation->height - label->misc.ypad * 2;
3599 pango_layout_set_width (label->layout, -1);
3600 pango_layout_get_pixel_extents (label->layout, NULL, &logical);
3602 if (label->have_transform)
3604 PangoContext *context = gtk_widget_get_pango_context (widget);
3605 const PangoMatrix *matrix = pango_context_get_matrix (context);
3607 const gdouble dx = matrix->xx; /* cos (M_PI * angle / 180) */
3608 const gdouble dy = matrix->xy; /* sin (M_PI * angle / 180) */
3609 if (fabs (dy) < 0.01)
3611 if (logical.width > bounds.width)
3612 pango_layout_set_width (label->layout, bounds.width * PANGO_SCALE);
3614 else if (fabs (dx) < 0.01)
3616 if (logical.width > bounds.height)
3617 pango_layout_set_width (label->layout, bounds.height * PANGO_SCALE);
3621 gdouble x0, y0, x1, y1, length;
3625 x0 = bounds.width / 2;
3626 y0 = dx ? x0 * dy / dx : dy * INFINITY;
3627 vertical = fabs (y0) > bounds.height / 2;
3631 y0 = bounds.height/2;
3632 x0 = dy ? y0 * dx / dy : dx * INFINITY;
3635 length = 2 * sqrt (x0 * x0 + y0 * y0);
3636 pango_layout_set_width (label->layout, rint (length * PANGO_SCALE));
3637 pango_layout_get_pixel_size (label->layout, NULL, &cy);
3644 y0 = bounds.height/2 + y1 - y0;
3649 x0 = bounds.width/2 + x1 - x0;
3653 length = length - sqrt (x0 * x0 + y0 * y0) * 2;
3654 pango_layout_set_width (label->layout, rint (length * PANGO_SCALE));
3657 else if (logical.width > bounds.width)
3658 pango_layout_set_width (label->layout, bounds.width * PANGO_SCALE);
3662 if (label->select_info && label->select_info->window)
3664 gdk_window_move_resize (label->select_info->window,
3668 allocation->height);
3673 gtk_label_update_cursor (GtkLabel *label)
3677 if (!label->select_info)
3680 widget = GTK_WIDGET (label);
3682 if (gtk_widget_get_realized (widget))
3684 GdkDisplay *display;
3687 if (gtk_widget_is_sensitive (widget))
3689 display = gtk_widget_get_display (widget);
3691 if (label->select_info->active_link)
3692 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
3693 else if (label->select_info->selectable)
3694 cursor = gdk_cursor_new_for_display (display, GDK_XTERM);
3701 gdk_window_set_cursor (label->select_info->window, cursor);
3704 gdk_cursor_unref (cursor);
3709 gtk_label_state_changed (GtkWidget *widget,
3710 GtkStateType prev_state)
3712 GtkLabel *label = GTK_LABEL (widget);
3714 if (label->select_info)
3716 gtk_label_select_region (label, 0, 0);
3717 gtk_label_update_cursor (label);
3720 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed)
3721 GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed (widget, prev_state);
3725 gtk_label_style_set (GtkWidget *widget,
3726 GtkStyle *previous_style)
3728 GtkLabel *label = GTK_LABEL (widget);
3730 /* We have to clear the layout, fonts etc. may have changed */
3731 gtk_label_clear_layout (label);
3732 gtk_label_invalidate_wrap_width (label);
3736 gtk_label_direction_changed (GtkWidget *widget,
3737 GtkTextDirection previous_dir)
3739 GtkLabel *label = GTK_LABEL (widget);
3742 pango_layout_context_changed (label->layout);
3744 GTK_WIDGET_CLASS (gtk_label_parent_class)->direction_changed (widget, previous_dir);
3748 get_layout_location (GtkLabel *label,
3754 GtkLabelPrivate *priv;
3756 gint req_width, x, y;
3758 PangoRectangle logical;
3761 misc = GTK_MISC (label);
3762 widget = GTK_WIDGET (label);
3763 priv = GTK_LABEL_GET_PRIVATE (label);
3764 angle = gtk_label_get_angle (label);
3766 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
3767 xalign = misc->xalign;
3769 xalign = 1.0 - misc->xalign;
3771 pango_layout_get_extents (label->layout, NULL, &logical);
3773 /* Do the wrap width delimiting before the transform
3775 if (label->wrap || label->ellipsize || priv->width_chars > 0)
3779 width = pango_layout_get_width (label->layout);
3782 logical.width = MIN (width, logical.width);
3785 if (label->have_transform)
3787 PangoContext *context = gtk_widget_get_pango_context (widget);
3788 const PangoMatrix *matrix = pango_context_get_matrix (context);
3789 pango_matrix_transform_rectangle (matrix, &logical);
3792 pango_extents_to_pixels (&logical, NULL);
3794 req_width = logical.width;
3795 req_height = logical.height;
3797 req_width += 2 * misc->xpad;
3798 req_height += 2 * misc->ypad;
3800 x = floor (widget->allocation.x + (gint)misc->xpad +
3801 xalign * (widget->allocation.width - req_width));
3803 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
3804 x = MAX (x, widget->allocation.x + misc->xpad);
3806 x = MIN (x, widget->allocation.x + widget->allocation.width - misc->xpad);
3811 /* bgo#315462 - For single-line labels, *do* align the requisition with
3812 * respect to the allocation, even if we are under-allocated. For multi-line
3813 * labels, always show the top of the text when they are under-allocated. The
3814 * rationale is this:
3816 * - Single-line labels appear in GtkButtons, and it is very easy to get them
3817 * to be smaller than their requisition. The button may clip the label, but
3818 * the label will still be able to show most of itself and the focus
3819 * rectangle. Also, it is fairly easy to read a single line of clipped text.
3821 * - Multi-line labels should not be clipped to showing "something in the
3822 * middle". You want to read the first line, at least, to get some context.
3824 if (pango_layout_get_line_count (label->layout) == 1)
3825 y = floor (widget->allocation.y + (gint)misc->ypad
3826 + (widget->allocation.height - req_height) * misc->yalign);
3828 y = floor (widget->allocation.y + (gint)misc->ypad
3829 + MAX (((widget->allocation.height - req_height) * misc->yalign),
3840 draw_insertion_cursor (GtkLabel *label,
3841 GdkRectangle *cursor_location,
3842 gboolean is_primary,
3843 PangoDirection direction,
3844 gboolean draw_arrow)
3846 GtkWidget *widget = GTK_WIDGET (label);
3847 GtkTextDirection text_dir;
3849 if (direction == PANGO_DIRECTION_LTR)
3850 text_dir = GTK_TEXT_DIR_LTR;
3852 text_dir = GTK_TEXT_DIR_RTL;
3854 gtk_draw_insertion_cursor (widget, widget->window, &(widget->allocation),
3856 is_primary, text_dir, draw_arrow);
3859 static PangoDirection
3860 get_cursor_direction (GtkLabel *label)
3864 g_assert (label->select_info);
3866 gtk_label_ensure_layout (label, FALSE);
3868 for (l = pango_layout_get_lines_readonly (label->layout); l; l = l->next)
3870 PangoLayoutLine *line = l->data;
3872 /* If label->select_info->selection_end is at the very end of
3873 * the line, we don't know if the cursor is on this line or
3874 * the next without looking ahead at the next line. (End
3875 * of paragraph is different from line break.) But it's
3876 * definitely in this paragraph, which is good enough
3877 * to figure out the resolved direction.
3879 if (line->start_index + line->length >= label->select_info->selection_end)
3880 return line->resolved_dir;
3883 return PANGO_DIRECTION_LTR;
3887 gtk_label_draw_cursor (GtkLabel *label, gint xoffset, gint yoffset)
3891 if (label->select_info == NULL)
3894 widget = GTK_WIDGET (label);
3896 if (gtk_widget_is_drawable (widget))
3898 PangoDirection keymap_direction;
3899 PangoDirection cursor_direction;
3900 PangoRectangle strong_pos, weak_pos;
3901 gboolean split_cursor;
3902 PangoRectangle *cursor1 = NULL;
3903 PangoRectangle *cursor2 = NULL;
3904 GdkRectangle cursor_location;
3905 PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
3906 PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
3908 keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gtk_widget_get_display (widget)));
3909 cursor_direction = get_cursor_direction (label);
3911 gtk_label_ensure_layout (label, FALSE);
3913 pango_layout_get_cursor_pos (label->layout, label->select_info->selection_end,
3914 &strong_pos, &weak_pos);
3916 g_object_get (gtk_widget_get_settings (widget),
3917 "gtk-split-cursor", &split_cursor,
3920 dir1 = cursor_direction;
3924 cursor1 = &strong_pos;
3926 if (strong_pos.x != weak_pos.x ||
3927 strong_pos.y != weak_pos.y)
3929 dir2 = (cursor_direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
3930 cursor2 = &weak_pos;
3935 if (keymap_direction == cursor_direction)
3936 cursor1 = &strong_pos;
3938 cursor1 = &weak_pos;
3941 cursor_location.x = xoffset + PANGO_PIXELS (cursor1->x);
3942 cursor_location.y = yoffset + PANGO_PIXELS (cursor1->y);
3943 cursor_location.width = 0;
3944 cursor_location.height = PANGO_PIXELS (cursor1->height);
3946 draw_insertion_cursor (label,
3947 &cursor_location, TRUE, dir1,
3948 dir2 != PANGO_DIRECTION_NEUTRAL);
3950 if (dir2 != PANGO_DIRECTION_NEUTRAL)
3952 cursor_location.x = xoffset + PANGO_PIXELS (cursor2->x);
3953 cursor_location.y = yoffset + PANGO_PIXELS (cursor2->y);
3954 cursor_location.width = 0;
3955 cursor_location.height = PANGO_PIXELS (cursor2->height);
3957 draw_insertion_cursor (label,
3958 &cursor_location, FALSE, dir2,
3964 static GtkLabelLink *
3965 gtk_label_get_focus_link (GtkLabel *label)
3967 GtkLabelSelectionInfo *info = label->select_info;
3973 if (info->selection_anchor != info->selection_end)
3976 for (l = info->links; l; l = l->next)
3978 GtkLabelLink *link = l->data;
3979 if (link->start <= info->selection_anchor &&
3980 info->selection_anchor <= link->end)
3988 gtk_label_expose (GtkWidget *widget,
3989 GdkEventExpose *event)
3991 GtkLabel *label = GTK_LABEL (widget);
3992 GtkLabelSelectionInfo *info = label->select_info;
3995 gtk_label_ensure_layout (label, FALSE);
3997 if (gtk_widget_get_visible (widget) && gtk_widget_get_mapped (widget) &&
3998 label->text && (*label->text != '\0'))
4000 get_layout_location (label, &x, &y);
4002 gtk_paint_layout (widget->style,
4004 gtk_widget_get_state (widget),
4013 (info->selection_anchor != info->selection_end))
4019 range[0] = info->selection_anchor;
4020 range[1] = info->selection_end;
4022 if (range[0] > range[1])
4024 gint tmp = range[0];
4025 range[0] = range[1];
4029 clip = gdk_pango_layout_get_clip_region (label->layout,
4033 gdk_region_intersect (clip, event->region);
4035 /* FIXME should use gtk_paint, but it can't use a clip
4039 gdk_gc_set_clip_region (widget->style->black_gc, clip);
4042 state = GTK_STATE_SELECTED;
4043 if (!gtk_widget_has_focus (widget))
4044 state = GTK_STATE_ACTIVE;
4046 gdk_draw_layout_with_colors (widget->window,
4047 widget->style->black_gc,
4050 &widget->style->text[state],
4051 &widget->style->base[state]);
4053 gdk_gc_set_clip_region (widget->style->black_gc, NULL);
4054 gdk_region_destroy (clip);
4058 GtkLabelLink *focus_link;
4059 GtkLabelLink *active_link;
4063 GdkColor *text_color;
4064 GdkColor *base_color;
4065 GdkColor *link_color;
4066 GdkColor *visited_link_color;
4068 if (info->selectable && gtk_widget_has_focus (widget))
4069 gtk_label_draw_cursor (label, x, y);
4071 focus_link = gtk_label_get_focus_link (label);
4072 active_link = info->active_link;
4076 range[0] = active_link->start;
4077 range[1] = active_link->end;
4079 clip = gdk_pango_layout_get_clip_region (label->layout,
4083 gdk_gc_set_clip_region (widget->style->black_gc, clip);
4085 gtk_label_get_link_colors (widget, &link_color, &visited_link_color);
4086 if (active_link->visited)
4087 text_color = visited_link_color;
4089 text_color = link_color;
4090 if (info->link_clicked)
4091 base_color = &widget->style->base[GTK_STATE_ACTIVE];
4093 base_color = &widget->style->base[GTK_STATE_PRELIGHT];
4094 gdk_draw_layout_with_colors (widget->window,
4095 widget->style->black_gc,
4100 gdk_color_free (link_color);
4101 gdk_color_free (visited_link_color);
4103 gdk_gc_set_clip_region (widget->style->black_gc, NULL);
4104 gdk_region_destroy (clip);
4107 if (focus_link && gtk_widget_has_focus (widget))
4109 range[0] = focus_link->start;
4110 range[1] = focus_link->end;
4112 clip = gdk_pango_layout_get_clip_region (label->layout,
4116 gdk_region_get_clipbox (clip, &rect);
4118 gtk_paint_focus (widget->style, widget->window, gtk_widget_get_state (widget),
4119 &event->area, widget, "label",
4120 rect.x, rect.y, rect.width, rect.height);
4122 gdk_region_destroy (clip);
4131 separate_uline_pattern (const gchar *str,
4136 gboolean underscore;
4139 gchar *pattern_dest;
4141 *accel_key = GDK_VoidSymbol;
4142 *new_str = g_new (gchar, strlen (str) + 1);
4143 *pattern = g_new (gchar, g_utf8_strlen (str, -1) + 1);
4149 pattern_dest = *pattern;
4154 const gchar *next_src;
4156 c = g_utf8_get_char (src);
4157 if (c == (gunichar)-1)
4159 g_warning ("Invalid input string");
4165 next_src = g_utf8_next_char (src);
4170 *pattern_dest++ = ' ';
4173 *pattern_dest++ = '_';
4174 if (*accel_key == GDK_VoidSymbol)
4175 *accel_key = gdk_keyval_to_lower (gdk_unicode_to_keyval (c));
4178 while (src < next_src)
4192 while (src < next_src)
4195 *pattern_dest++ = ' ';
4207 gtk_label_set_uline_text_internal (GtkLabel *label,
4210 guint accel_key = GDK_VoidSymbol;
4214 g_return_if_fail (GTK_IS_LABEL (label));
4215 g_return_if_fail (str != NULL);
4217 /* Split text into the base text and a separate pattern
4220 if (!separate_uline_pattern (str, &accel_key, &new_str, &pattern))
4223 gtk_label_set_text_internal (label, new_str);
4224 gtk_label_set_pattern_internal (label, pattern, TRUE);
4225 label->mnemonic_keyval = accel_key;
4231 gtk_label_parse_uline (GtkLabel *label,
4236 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_VoidSymbol);
4237 g_return_val_if_fail (str != NULL, GDK_VoidSymbol);
4239 g_object_freeze_notify (G_OBJECT (label));
4241 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
4242 gtk_label_set_use_markup_internal (label, FALSE);
4243 gtk_label_set_use_underline_internal (label, TRUE);
4245 gtk_label_recalculate (label);
4247 keyval = label->mnemonic_keyval;
4248 if (keyval != GDK_VoidSymbol)
4250 label->mnemonic_keyval = GDK_VoidSymbol;
4251 gtk_label_setup_mnemonic (label, keyval);
4252 g_object_notify (G_OBJECT (label), "mnemonic-keyval");
4255 g_object_thaw_notify (G_OBJECT (label));
4261 * gtk_label_set_text_with_mnemonic:
4262 * @label: a #GtkLabel
4265 * Sets the label's text from the string @str.
4266 * If characters in @str are preceded by an underscore, they are underlined
4267 * indicating that they represent a keyboard accelerator called a mnemonic.
4268 * The mnemonic key can be used to activate another widget, chosen
4269 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
4272 gtk_label_set_text_with_mnemonic (GtkLabel *label,
4275 g_return_if_fail (GTK_IS_LABEL (label));
4276 g_return_if_fail (str != NULL);
4278 g_object_freeze_notify (G_OBJECT (label));
4280 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
4281 gtk_label_set_use_markup_internal (label, FALSE);
4282 gtk_label_set_use_underline_internal (label, TRUE);
4284 gtk_label_recalculate (label);
4286 g_object_thaw_notify (G_OBJECT (label));
4290 gtk_label_realize (GtkWidget *widget)
4294 label = GTK_LABEL (widget);
4296 GTK_WIDGET_CLASS (gtk_label_parent_class)->realize (widget);
4298 if (label->select_info)
4299 gtk_label_create_window (label);
4303 gtk_label_unrealize (GtkWidget *widget)
4307 label = GTK_LABEL (widget);
4309 if (label->select_info)
4310 gtk_label_destroy_window (label);
4312 GTK_WIDGET_CLASS (gtk_label_parent_class)->unrealize (widget);
4316 gtk_label_map (GtkWidget *widget)
4320 label = GTK_LABEL (widget);
4322 GTK_WIDGET_CLASS (gtk_label_parent_class)->map (widget);
4324 if (label->select_info)
4325 gdk_window_show (label->select_info->window);
4329 gtk_label_unmap (GtkWidget *widget)
4333 label = GTK_LABEL (widget);
4335 if (label->select_info)
4336 gdk_window_hide (label->select_info->window);
4338 GTK_WIDGET_CLASS (gtk_label_parent_class)->unmap (widget);
4342 window_to_layout_coords (GtkLabel *label,
4349 widget = GTK_WIDGET (label);
4351 /* get layout location in widget->window coords */
4352 get_layout_location (label, &lx, &ly);
4356 *x += widget->allocation.x; /* go to widget->window */
4357 *x -= lx; /* go to layout */
4362 *y += widget->allocation.y; /* go to widget->window */
4363 *y -= ly; /* go to layout */
4369 layout_to_window_coords (GtkLabel *label,
4376 widget = GTK_WIDGET (label);
4378 /* get layout location in widget->window coords */
4379 get_layout_location (label, &lx, &ly);
4383 *x += lx; /* go to widget->window */
4384 *x -= widget->allocation.x; /* go to selection window */
4389 *y += ly; /* go to widget->window */
4390 *y -= widget->allocation.y; /* go to selection window */
4396 get_layout_index (GtkLabel *label,
4402 const gchar *cluster;
4403 const gchar *cluster_end;
4408 gtk_label_ensure_layout (label, FALSE);
4410 window_to_layout_coords (label, &x, &y);
4415 inside = pango_layout_xy_to_index (label->layout,
4419 cluster = label->text + *index;
4420 cluster_end = cluster;
4423 cluster_end = g_utf8_next_char (cluster_end);
4427 *index += (cluster_end - cluster);
4433 gtk_label_select_word (GtkLabel *label)
4437 gint start_index = gtk_label_move_backward_word (label, label->select_info->selection_end);
4438 gint end_index = gtk_label_move_forward_word (label, label->select_info->selection_end);
4440 min = MIN (label->select_info->selection_anchor,
4441 label->select_info->selection_end);
4442 max = MAX (label->select_info->selection_anchor,
4443 label->select_info->selection_end);
4445 min = MIN (min, start_index);
4446 max = MAX (max, end_index);
4448 gtk_label_select_region_index (label, min, max);
4452 gtk_label_grab_focus (GtkWidget *widget)
4455 gboolean select_on_focus;
4458 label = GTK_LABEL (widget);
4460 if (label->select_info == NULL)
4463 GTK_WIDGET_CLASS (gtk_label_parent_class)->grab_focus (widget);
4465 if (label->select_info->selectable)
4467 g_object_get (gtk_widget_get_settings (widget),
4468 "gtk-label-select-on-focus",
4472 if (select_on_focus && !label->in_click)
4473 gtk_label_select_region (label, 0, -1);
4477 if (label->select_info->links && !label->in_click)
4479 link = label->select_info->links->data;
4480 label->select_info->selection_anchor = link->start;
4481 label->select_info->selection_end = link->start;
4487 gtk_label_focus (GtkWidget *widget,
4488 GtkDirectionType direction)
4490 GtkLabel *label = GTK_LABEL (widget);
4491 GtkLabelSelectionInfo *info = label->select_info;
4492 GtkLabelLink *focus_link;
4495 if (!gtk_widget_is_focus (widget))
4497 gtk_widget_grab_focus (widget);
4500 focus_link = gtk_label_get_focus_link (label);
4501 if (focus_link && direction == GTK_DIR_TAB_BACKWARD)
4503 l = g_list_last (info->links);
4504 focus_link = l->data;
4505 info->selection_anchor = focus_link->start;
4506 info->selection_end = focus_link->start;
4516 if (info->selectable)
4520 if (info->selection_anchor != info->selection_end)
4523 index = info->selection_anchor;
4525 if (direction == GTK_DIR_TAB_FORWARD)
4526 for (l = info->links; l; l = l->next)
4528 GtkLabelLink *link = l->data;
4530 if (link->start > index)
4532 gtk_label_select_region_index (label, link->start, link->start);
4536 else if (direction == GTK_DIR_TAB_BACKWARD)
4537 for (l = g_list_last (info->links); l; l = l->prev)
4539 GtkLabelLink *link = l->data;
4541 if (link->end < index)
4543 gtk_label_select_region_index (label, link->start, link->start);
4552 focus_link = gtk_label_get_focus_link (label);
4555 case GTK_DIR_TAB_FORWARD:
4558 l = g_list_find (info->links, focus_link);
4565 case GTK_DIR_TAB_BACKWARD:
4568 l = g_list_find (info->links, focus_link);
4572 l = g_list_last (info->links);
4581 focus_link = l->data;
4582 info->selection_anchor = focus_link->start;
4583 info->selection_end = focus_link->start;
4584 gtk_widget_queue_draw (widget);
4596 gtk_label_button_press (GtkWidget *widget,
4597 GdkEventButton *event)
4599 GtkLabel *label = GTK_LABEL (widget);
4600 GtkLabelSelectionInfo *info = label->select_info;
4607 if (info->active_link)
4609 if (event->button == 1)
4611 info->link_clicked = 1;
4612 gtk_widget_queue_draw (widget);
4614 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4616 info->link_clicked = 1;
4617 gtk_label_do_popup (label, event);
4622 if (!info->selectable)
4625 info->in_drag = FALSE;
4626 info->select_words = FALSE;
4628 if (event->button == 1)
4630 if (!gtk_widget_has_focus (widget))
4632 label->in_click = TRUE;
4633 gtk_widget_grab_focus (widget);
4634 label->in_click = FALSE;
4637 if (event->type == GDK_3BUTTON_PRESS)
4639 gtk_label_select_region_index (label, 0, strlen (label->text));
4643 if (event->type == GDK_2BUTTON_PRESS)
4645 info->select_words = TRUE;
4646 gtk_label_select_word (label);
4650 get_layout_index (label, event->x, event->y, &index);
4652 min = MIN (info->selection_anchor, info->selection_end);
4653 max = MAX (info->selection_anchor, info->selection_end);
4655 if ((info->selection_anchor != info->selection_end) &&
4656 (event->state & GDK_SHIFT_MASK))
4658 /* extend (same as motion) */
4659 min = MIN (min, index);
4660 max = MAX (max, index);
4662 /* ensure the anchor is opposite index */
4670 gtk_label_select_region_index (label, min, max);
4674 if (event->type == GDK_3BUTTON_PRESS)
4675 gtk_label_select_region_index (label, 0, strlen (label->text));
4676 else if (event->type == GDK_2BUTTON_PRESS)
4677 gtk_label_select_word (label);
4678 else if (min < max && min <= index && index <= max)
4680 info->in_drag = TRUE;
4681 info->drag_start_x = event->x;
4682 info->drag_start_y = event->y;
4685 /* start a replacement */
4686 gtk_label_select_region_index (label, index, index);
4691 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4693 gtk_label_do_popup (label, event);
4701 gtk_label_button_release (GtkWidget *widget,
4702 GdkEventButton *event)
4705 GtkLabel *label = GTK_LABEL (widget);
4706 GtkLabelSelectionInfo *info = label->select_info;
4716 get_layout_index (label, event->x, event->y, &index);
4717 gtk_label_select_region_index (label, index, index);
4722 if (event->button != 1)
4725 if (info->active_link &&
4726 info->selection_anchor == info->selection_end &&
4729 emit_activate_link (label, info->active_link);
4730 info->link_clicked = 0;
4735 /* The goal here is to return TRUE iff we ate the
4736 * button press to start selecting.
4742 connect_mnemonics_visible_notify (GtkLabel *label)
4744 GtkLabelPrivate *priv = GTK_LABEL_GET_PRIVATE (label);
4745 GtkWidget *toplevel;
4748 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
4750 if (!GTK_IS_WINDOW (toplevel))
4753 /* always set up this widgets initial value */
4754 priv->mnemonics_visible =
4755 gtk_window_get_mnemonics_visible (GTK_WINDOW (toplevel));
4758 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (toplevel),
4759 "gtk-label-mnemonics-visible-connected"));
4763 g_signal_connect (toplevel,
4764 "notify::mnemonics-visible",
4765 G_CALLBACK (label_mnemonics_visible_changed),
4767 g_object_set_data (G_OBJECT (toplevel),
4768 "gtk-label-mnemonics-visible-connected",
4769 GINT_TO_POINTER (1));
4774 drag_begin_cb (GtkWidget *widget,
4775 GdkDragContext *context,
4779 GdkPixmap *pixmap = NULL;
4781 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
4783 label = GTK_LABEL (widget);
4785 if ((label->select_info->selection_anchor !=
4786 label->select_info->selection_end) &&
4792 start = MIN (label->select_info->selection_anchor,
4793 label->select_info->selection_end);
4794 end = MAX (label->select_info->selection_anchor,
4795 label->select_info->selection_end);
4797 len = strlen (label->text);
4805 pixmap = _gtk_text_util_create_drag_icon (widget,
4806 label->text + start,
4811 gtk_drag_set_icon_pixmap (context,
4812 gdk_drawable_get_colormap (pixmap),
4817 gtk_drag_set_icon_default (context);
4820 g_object_unref (pixmap);
4824 gtk_label_motion (GtkWidget *widget,
4825 GdkEventMotion *event)
4827 GtkLabel *label = GTK_LABEL (widget);
4828 GtkLabelSelectionInfo *info = label->select_info;
4835 if (info->links && !info->in_drag)
4839 gboolean found = FALSE;
4841 if (info->selection_anchor == info->selection_end)
4843 gdk_window_get_pointer (event->window, &x, &y, NULL);
4844 if (get_layout_index (label, x, y, &index))
4846 for (l = info->links; l != NULL; l = l->next)
4849 if (index >= link->start && index <= link->end)
4860 if (info->active_link != link)
4862 info->link_clicked = 0;
4863 info->active_link = link;
4864 gtk_label_update_cursor (label);
4865 gtk_widget_queue_draw (widget);
4870 if (info->active_link != NULL)
4872 info->link_clicked = 0;
4873 info->active_link = NULL;
4874 gtk_label_update_cursor (label);
4875 gtk_widget_queue_draw (widget);
4880 if (!info->selectable)
4883 if ((event->state & GDK_BUTTON1_MASK) == 0)
4886 gdk_window_get_pointer (info->window, &x, &y, NULL);
4890 if (gtk_drag_check_threshold (widget,
4893 event->x, event->y))
4895 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
4897 gtk_target_list_add_text_targets (target_list, 0);
4899 g_signal_connect (widget, "drag-begin",
4900 G_CALLBACK (drag_begin_cb), NULL);
4901 gtk_drag_begin (widget, target_list,
4903 1, (GdkEvent *)event);
4905 info->in_drag = FALSE;
4907 gtk_target_list_unref (target_list);
4912 get_layout_index (label, x, y, &index);
4914 if (info->select_words)
4917 gint old_min, old_max;
4920 min = gtk_label_move_backward_word (label, index);
4921 max = gtk_label_move_forward_word (label, index);
4923 anchor = info->selection_anchor;
4924 end = info->selection_end;
4926 old_min = MIN (anchor, end);
4927 old_max = MAX (anchor, end);
4934 else if (old_max < max)
4939 else if (anchor == old_min)
4950 gtk_label_select_region_index (label, anchor, end);
4953 gtk_label_select_region_index (label, info->selection_anchor, index);
4960 gtk_label_leave_notify (GtkWidget *widget,
4961 GdkEventCrossing *event)
4963 GtkLabel *label = GTK_LABEL (widget);
4965 if (label->select_info)
4967 label->select_info->active_link = NULL;
4968 gtk_label_update_cursor (label);
4969 gtk_widget_queue_draw (widget);
4972 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event)
4973 return GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event (widget, event);
4979 gtk_label_create_window (GtkLabel *label)
4982 GdkWindowAttr attributes;
4983 gint attributes_mask;
4985 g_assert (label->select_info);
4986 widget = GTK_WIDGET (label);
4987 g_assert (gtk_widget_get_realized (widget));
4989 if (label->select_info->window)
4992 attributes.x = widget->allocation.x;
4993 attributes.y = widget->allocation.y;
4994 attributes.width = widget->allocation.width;
4995 attributes.height = widget->allocation.height;
4996 attributes.window_type = GDK_WINDOW_CHILD;
4997 attributes.wclass = GDK_INPUT_ONLY;
4998 attributes.override_redirect = TRUE;
4999 attributes.event_mask = gtk_widget_get_events (widget) |
5000 GDK_BUTTON_PRESS_MASK |
5001 GDK_BUTTON_RELEASE_MASK |
5002 GDK_LEAVE_NOTIFY_MASK |
5003 GDK_BUTTON_MOTION_MASK |
5004 GDK_POINTER_MOTION_MASK |
5005 GDK_POINTER_MOTION_HINT_MASK;
5006 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
5007 if (gtk_widget_is_sensitive (widget))
5009 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
5011 attributes_mask |= GDK_WA_CURSOR;
5015 label->select_info->window = gdk_window_new (widget->window,
5016 &attributes, attributes_mask);
5017 gdk_window_set_user_data (label->select_info->window, widget);
5019 if (attributes_mask & GDK_WA_CURSOR)
5020 gdk_cursor_unref (attributes.cursor);
5024 gtk_label_destroy_window (GtkLabel *label)
5026 g_assert (label->select_info);
5028 if (label->select_info->window == NULL)
5031 gdk_window_set_user_data (label->select_info->window, NULL);
5032 gdk_window_destroy (label->select_info->window);
5033 label->select_info->window = NULL;
5037 gtk_label_ensure_select_info (GtkLabel *label)
5039 if (label->select_info == NULL)
5041 label->select_info = g_new0 (GtkLabelSelectionInfo, 1);
5043 gtk_widget_set_can_focus (GTK_WIDGET (label), TRUE);
5045 if (gtk_widget_get_realized (GTK_WIDGET (label)))
5046 gtk_label_create_window (label);
5048 if (gtk_widget_get_mapped (GTK_WIDGET (label)))
5049 gdk_window_show (label->select_info->window);
5054 gtk_label_clear_select_info (GtkLabel *label)
5056 if (label->select_info == NULL)
5059 if (!label->select_info->selectable && !label->select_info->links)
5061 gtk_label_destroy_window (label);
5063 g_free (label->select_info);
5064 label->select_info = NULL;
5066 gtk_widget_set_can_focus (GTK_WIDGET (label), FALSE);
5071 * gtk_label_set_selectable:
5072 * @label: a #GtkLabel
5073 * @setting: %TRUE to allow selecting text in the label
5075 * Selectable labels allow the user to select text from the label, for
5079 gtk_label_set_selectable (GtkLabel *label,
5082 gboolean old_setting;
5084 g_return_if_fail (GTK_IS_LABEL (label));
5086 setting = setting != FALSE;
5087 old_setting = label->select_info && label->select_info->selectable;
5091 gtk_label_ensure_select_info (label);
5092 label->select_info->selectable = TRUE;
5093 gtk_label_update_cursor (label);
5099 /* unselect, to give up the selection */
5100 gtk_label_select_region (label, 0, 0);
5102 label->select_info->selectable = FALSE;
5103 gtk_label_clear_select_info (label);
5104 gtk_label_update_cursor (label);
5107 if (setting != old_setting)
5109 g_object_freeze_notify (G_OBJECT (label));
5110 g_object_notify (G_OBJECT (label), "selectable");
5111 g_object_notify (G_OBJECT (label), "cursor-position");
5112 g_object_notify (G_OBJECT (label), "selection-bound");
5113 g_object_thaw_notify (G_OBJECT (label));
5114 gtk_widget_queue_draw (GTK_WIDGET (label));
5119 * gtk_label_get_selectable:
5120 * @label: a #GtkLabel
5122 * Gets the value set by gtk_label_set_selectable().
5124 * Return value: %TRUE if the user can copy text from the label
5127 gtk_label_get_selectable (GtkLabel *label)
5129 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5131 return label->select_info && label->select_info->selectable;
5135 free_angle (gpointer angle)
5137 g_slice_free (gdouble, angle);
5141 * gtk_label_set_angle:
5142 * @label: a #GtkLabel
5143 * @angle: the angle that the baseline of the label makes with
5144 * the horizontal, in degrees, measured counterclockwise
5146 * Sets the angle of rotation for the label. An angle of 90 reads from
5147 * from bottom to top, an angle of 270, from top to bottom. The angle
5148 * setting for the label is ignored if the label is selectable,
5149 * wrapped, or ellipsized.
5154 gtk_label_set_angle (GtkLabel *label,
5157 gdouble *label_angle;
5159 g_return_if_fail (GTK_IS_LABEL (label));
5161 label_angle = (gdouble *)g_object_get_qdata (G_OBJECT (label), quark_angle);
5165 label_angle = g_slice_new (gdouble);
5167 g_object_set_qdata_full (G_OBJECT (label), quark_angle,
5168 label_angle, free_angle);
5171 /* Canonicalize to [0,360]. We don't canonicalize 360 to 0, because
5172 * double property ranges are inclusive, and changing 360 to 0 would
5173 * make a property editor behave strangely.
5175 if (angle < 0 || angle > 360.0)
5176 angle = angle - 360. * floor (angle / 360.);
5178 if (*label_angle != angle)
5180 *label_angle = angle;
5182 gtk_label_clear_layout (label);
5183 gtk_widget_queue_resize (GTK_WIDGET (label));
5185 g_object_notify (G_OBJECT (label), "angle");
5190 * gtk_label_get_angle:
5191 * @label: a #GtkLabel
5193 * Gets the angle of rotation for the label. See
5194 * gtk_label_set_angle().
5196 * Return value: the angle of rotation for the label
5201 gtk_label_get_angle (GtkLabel *label)
5205 g_return_val_if_fail (GTK_IS_LABEL (label), 0.0);
5207 angle = (gdouble *)g_object_get_qdata (G_OBJECT (label), quark_angle);
5216 gtk_label_set_selection_text (GtkLabel *label,
5217 GtkSelectionData *selection_data)
5219 if ((label->select_info->selection_anchor !=
5220 label->select_info->selection_end) &&
5226 start = MIN (label->select_info->selection_anchor,
5227 label->select_info->selection_end);
5228 end = MAX (label->select_info->selection_anchor,
5229 label->select_info->selection_end);
5231 len = strlen (label->text);
5239 gtk_selection_data_set_text (selection_data,
5240 label->text + start,
5246 gtk_label_drag_data_get (GtkWidget *widget,
5247 GdkDragContext *context,
5248 GtkSelectionData *selection_data,
5252 gtk_label_set_selection_text (GTK_LABEL (widget), selection_data);
5256 get_text_callback (GtkClipboard *clipboard,
5257 GtkSelectionData *selection_data,
5259 gpointer user_data_or_owner)
5261 gtk_label_set_selection_text (GTK_LABEL (user_data_or_owner), selection_data);
5265 clear_text_callback (GtkClipboard *clipboard,
5266 gpointer user_data_or_owner)
5270 label = GTK_LABEL (user_data_or_owner);
5272 if (label->select_info)
5274 label->select_info->selection_anchor = label->select_info->selection_end;
5276 gtk_widget_queue_draw (GTK_WIDGET (label));
5281 gtk_label_select_region_index (GtkLabel *label,
5285 g_return_if_fail (GTK_IS_LABEL (label));
5287 if (label->select_info && label->select_info->selectable)
5289 GtkClipboard *clipboard;
5291 if (label->select_info->selection_anchor == anchor_index &&
5292 label->select_info->selection_end == end_index)
5295 label->select_info->selection_anchor = anchor_index;
5296 label->select_info->selection_end = end_index;
5298 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label),
5299 GDK_SELECTION_PRIMARY);
5301 if (anchor_index != end_index)
5303 GtkTargetList *list;
5304 GtkTargetEntry *targets;
5307 list = gtk_target_list_new (NULL, 0);
5308 gtk_target_list_add_text_targets (list, 0);
5309 targets = gtk_target_table_new_from_list (list, &n_targets);
5311 gtk_clipboard_set_with_owner (clipboard,
5314 clear_text_callback,
5317 gtk_target_table_free (targets, n_targets);
5318 gtk_target_list_unref (list);
5322 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (label))
5323 gtk_clipboard_clear (clipboard);
5326 gtk_widget_queue_draw (GTK_WIDGET (label));
5328 g_object_freeze_notify (G_OBJECT (label));
5329 g_object_notify (G_OBJECT (label), "cursor-position");
5330 g_object_notify (G_OBJECT (label), "selection-bound");
5331 g_object_thaw_notify (G_OBJECT (label));
5336 * gtk_label_select_region:
5337 * @label: a #GtkLabel
5338 * @start_offset: start offset (in characters not bytes)
5339 * @end_offset: end offset (in characters not bytes)
5341 * Selects a range of characters in the label, if the label is selectable.
5342 * See gtk_label_set_selectable(). If the label is not selectable,
5343 * this function has no effect. If @start_offset or
5344 * @end_offset are -1, then the end of the label will be substituted.
5347 gtk_label_select_region (GtkLabel *label,
5351 g_return_if_fail (GTK_IS_LABEL (label));
5353 if (label->text && label->select_info)
5355 if (start_offset < 0)
5356 start_offset = g_utf8_strlen (label->text, -1);
5359 end_offset = g_utf8_strlen (label->text, -1);
5361 gtk_label_select_region_index (label,
5362 g_utf8_offset_to_pointer (label->text, start_offset) - label->text,
5363 g_utf8_offset_to_pointer (label->text, end_offset) - label->text);
5368 * gtk_label_get_selection_bounds:
5369 * @label: a #GtkLabel
5370 * @start: return location for start of selection, as a character offset
5371 * @end: return location for end of selection, as a character offset
5373 * Gets the selected range of characters in the label, returning %TRUE
5374 * if there's a selection.
5376 * Return value: %TRUE if selection is non-empty
5379 gtk_label_get_selection_bounds (GtkLabel *label,
5383 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5385 if (label->select_info == NULL)
5387 /* not a selectable label */
5397 gint start_index, end_index;
5398 gint start_offset, end_offset;
5401 start_index = MIN (label->select_info->selection_anchor,
5402 label->select_info->selection_end);
5403 end_index = MAX (label->select_info->selection_anchor,
5404 label->select_info->selection_end);
5406 len = strlen (label->text);
5408 if (end_index > len)
5411 if (start_index > len)
5414 start_offset = g_utf8_strlen (label->text, start_index);
5415 end_offset = g_utf8_strlen (label->text, end_index);
5417 if (start_offset > end_offset)
5419 gint tmp = start_offset;
5420 start_offset = end_offset;
5425 *start = start_offset;
5430 return start_offset != end_offset;
5436 * gtk_label_get_layout:
5437 * @label: a #GtkLabel
5439 * Gets the #PangoLayout used to display the label.
5440 * The layout is useful to e.g. convert text positions to
5441 * pixel positions, in combination with gtk_label_get_layout_offsets().
5442 * The returned layout is owned by the label so need not be
5443 * freed by the caller.
5445 * Return value: (transfer none): the #PangoLayout for this label
5448 gtk_label_get_layout (GtkLabel *label)
5450 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
5452 gtk_label_ensure_layout (label, FALSE);
5454 return label->layout;
5458 * gtk_label_get_layout_offsets:
5459 * @label: a #GtkLabel
5460 * @x: (allow-none): location to store X offset of layout, or %NULL
5461 * @y: (allow-none): location to store Y offset of layout, or %NULL
5463 * Obtains the coordinates where the label will draw the #PangoLayout
5464 * representing the text in the label; useful to convert mouse events
5465 * into coordinates inside the #PangoLayout, e.g. to take some action
5466 * if some part of the label is clicked. Of course you will need to
5467 * create a #GtkEventBox to receive the events, and pack the label
5468 * inside it, since labels are a #GTK_NO_WINDOW widget. Remember
5469 * when using the #PangoLayout functions you need to convert to
5470 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
5473 gtk_label_get_layout_offsets (GtkLabel *label,
5477 g_return_if_fail (GTK_IS_LABEL (label));
5479 gtk_label_ensure_layout (label, FALSE);
5481 get_layout_location (label, x, y);
5485 * gtk_label_set_use_markup:
5486 * @label: a #GtkLabel
5487 * @setting: %TRUE if the label's text should be parsed for markup.
5489 * Sets whether the text of the label contains markup in <link
5490 * linkend="PangoMarkupFormat">Pango's text markup
5491 * language</link>. See gtk_label_set_markup().
5494 gtk_label_set_use_markup (GtkLabel *label,
5497 g_return_if_fail (GTK_IS_LABEL (label));
5499 gtk_label_set_use_markup_internal (label, setting);
5500 gtk_label_recalculate (label);
5504 * gtk_label_get_use_markup:
5505 * @label: a #GtkLabel
5507 * Returns whether the label's text is interpreted as marked up with
5508 * the <link linkend="PangoMarkupFormat">Pango text markup
5509 * language</link>. See gtk_label_set_use_markup ().
5511 * Return value: %TRUE if the label's text will be parsed for markup.
5514 gtk_label_get_use_markup (GtkLabel *label)
5516 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5518 return label->use_markup;
5522 * gtk_label_set_use_underline:
5523 * @label: a #GtkLabel
5524 * @setting: %TRUE if underlines in the text indicate mnemonics
5526 * If true, an underline in the text indicates the next character should be
5527 * used for the mnemonic accelerator key.
5530 gtk_label_set_use_underline (GtkLabel *label,
5533 g_return_if_fail (GTK_IS_LABEL (label));
5535 gtk_label_set_use_underline_internal (label, setting);
5536 gtk_label_recalculate (label);
5540 * gtk_label_get_use_underline:
5541 * @label: a #GtkLabel
5543 * Returns whether an embedded underline in the label indicates a
5544 * mnemonic. See gtk_label_set_use_underline().
5546 * Return value: %TRUE whether an embedded underline in the label indicates
5547 * the mnemonic accelerator keys.
5550 gtk_label_get_use_underline (GtkLabel *label)
5552 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5554 return label->use_underline;
5558 * gtk_label_set_single_line_mode:
5559 * @label: a #GtkLabel
5560 * @single_line_mode: %TRUE if the label should be in single line mode
5562 * Sets whether the label is in single line mode.
5567 gtk_label_set_single_line_mode (GtkLabel *label,
5568 gboolean single_line_mode)
5570 g_return_if_fail (GTK_IS_LABEL (label));
5572 single_line_mode = single_line_mode != FALSE;
5574 if (label->single_line_mode != single_line_mode)
5576 label->single_line_mode = single_line_mode;
5578 gtk_label_clear_layout (label);
5579 gtk_widget_queue_resize (GTK_WIDGET (label));
5581 g_object_notify (G_OBJECT (label), "single-line-mode");
5586 * gtk_label_get_single_line_mode:
5587 * @label: a #GtkLabel
5589 * Returns whether the label is in single line mode.
5591 * Return value: %TRUE when the label is in single line mode.
5596 gtk_label_get_single_line_mode (GtkLabel *label)
5598 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5600 return label->single_line_mode;
5603 /* Compute the X position for an offset that corresponds to the "more important
5604 * cursor position for that offset. We use this when trying to guess to which
5605 * end of the selection we should go to when the user hits the left or
5609 get_better_cursor (GtkLabel *label,
5614 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5615 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5616 PangoDirection cursor_direction = get_cursor_direction (label);
5617 gboolean split_cursor;
5618 PangoRectangle strong_pos, weak_pos;
5620 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5621 "gtk-split-cursor", &split_cursor,
5624 gtk_label_ensure_layout (label, FALSE);
5626 pango_layout_get_cursor_pos (label->layout, index,
5627 &strong_pos, &weak_pos);
5631 *x = strong_pos.x / PANGO_SCALE;
5632 *y = strong_pos.y / PANGO_SCALE;
5636 if (keymap_direction == cursor_direction)
5638 *x = strong_pos.x / PANGO_SCALE;
5639 *y = strong_pos.y / PANGO_SCALE;
5643 *x = weak_pos.x / PANGO_SCALE;
5644 *y = weak_pos.y / PANGO_SCALE;
5651 gtk_label_move_logically (GtkLabel *label,
5655 gint offset = g_utf8_pointer_to_offset (label->text,
5656 label->text + start);
5660 PangoLogAttr *log_attrs;
5664 gtk_label_ensure_layout (label, FALSE);
5666 length = g_utf8_strlen (label->text, -1);
5668 pango_layout_get_log_attrs (label->layout, &log_attrs, &n_attrs);
5670 while (count > 0 && offset < length)
5674 while (offset < length && !log_attrs[offset].is_cursor_position);
5678 while (count < 0 && offset > 0)
5682 while (offset > 0 && !log_attrs[offset].is_cursor_position);
5690 return g_utf8_offset_to_pointer (label->text, offset) - label->text;
5694 gtk_label_move_visually (GtkLabel *label,
5704 int new_index, new_trailing;
5705 gboolean split_cursor;
5708 gtk_label_ensure_layout (label, FALSE);
5710 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5711 "gtk-split-cursor", &split_cursor,
5718 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5719 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5721 strong = keymap_direction == get_cursor_direction (label);
5726 pango_layout_move_cursor_visually (label->layout, strong, index, 0, 1, &new_index, &new_trailing);
5731 pango_layout_move_cursor_visually (label->layout, strong, index, 0, -1, &new_index, &new_trailing);
5735 if (new_index < 0 || new_index == G_MAXINT)
5740 while (new_trailing--)
5741 index = g_utf8_next_char (label->text + new_index) - label->text;
5748 gtk_label_move_forward_word (GtkLabel *label,
5751 gint new_pos = g_utf8_pointer_to_offset (label->text,
5752 label->text + start);
5755 length = g_utf8_strlen (label->text, -1);
5756 if (new_pos < length)
5758 PangoLogAttr *log_attrs;
5761 gtk_label_ensure_layout (label, FALSE);
5763 pango_layout_get_log_attrs (label->layout, &log_attrs, &n_attrs);
5765 /* Find the next word end */
5767 while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
5773 return g_utf8_offset_to_pointer (label->text, new_pos) - label->text;
5778 gtk_label_move_backward_word (GtkLabel *label,
5781 gint new_pos = g_utf8_pointer_to_offset (label->text,
5782 label->text + start);
5786 PangoLogAttr *log_attrs;
5789 gtk_label_ensure_layout (label, FALSE);
5791 pango_layout_get_log_attrs (label->layout, &log_attrs, &n_attrs);
5795 /* Find the previous word beginning */
5796 while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
5802 return g_utf8_offset_to_pointer (label->text, new_pos) - label->text;
5806 gtk_label_move_cursor (GtkLabel *label,
5807 GtkMovementStep step,
5809 gboolean extend_selection)
5814 if (label->select_info == NULL)
5817 old_pos = new_pos = label->select_info->selection_end;
5819 if (label->select_info->selection_end != label->select_info->selection_anchor &&
5822 /* If we have a current selection and aren't extending it, move to the
5823 * start/or end of the selection as appropriate
5827 case GTK_MOVEMENT_VISUAL_POSITIONS:
5830 gint anchor_x, anchor_y;
5831 gboolean end_is_left;
5833 get_better_cursor (label, label->select_info->selection_end, &end_x, &end_y);
5834 get_better_cursor (label, label->select_info->selection_anchor, &anchor_x, &anchor_y);
5836 end_is_left = (end_y < anchor_y) || (end_y == anchor_y && end_x < anchor_x);
5839 new_pos = end_is_left ? label->select_info->selection_end : label->select_info->selection_anchor;
5841 new_pos = !end_is_left ? label->select_info->selection_end : label->select_info->selection_anchor;
5844 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5845 case GTK_MOVEMENT_WORDS:
5847 new_pos = MIN (label->select_info->selection_end, label->select_info->selection_anchor);
5849 new_pos = MAX (label->select_info->selection_end, label->select_info->selection_anchor);
5851 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5852 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5853 case GTK_MOVEMENT_BUFFER_ENDS:
5854 /* FIXME: Can do better here */
5855 new_pos = count < 0 ? 0 : strlen (label->text);
5857 case GTK_MOVEMENT_DISPLAY_LINES:
5858 case GTK_MOVEMENT_PARAGRAPHS:
5859 case GTK_MOVEMENT_PAGES:
5860 case GTK_MOVEMENT_HORIZONTAL_PAGES:
5868 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5869 new_pos = gtk_label_move_logically (label, new_pos, count);
5871 case GTK_MOVEMENT_VISUAL_POSITIONS:
5872 new_pos = gtk_label_move_visually (label, new_pos, count);
5873 if (new_pos == old_pos)
5875 if (!extend_selection)
5877 if (!gtk_widget_keynav_failed (GTK_WIDGET (label),
5879 GTK_DIR_RIGHT : GTK_DIR_LEFT))
5881 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
5884 gtk_widget_child_focus (toplevel,
5886 GTK_DIR_RIGHT : GTK_DIR_LEFT);
5891 gtk_widget_error_bell (GTK_WIDGET (label));
5895 case GTK_MOVEMENT_WORDS:
5898 new_pos = gtk_label_move_forward_word (label, new_pos);
5903 new_pos = gtk_label_move_backward_word (label, new_pos);
5906 if (new_pos == old_pos)
5907 gtk_widget_error_bell (GTK_WIDGET (label));
5909 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5910 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5911 case GTK_MOVEMENT_BUFFER_ENDS:
5912 /* FIXME: Can do better here */
5913 new_pos = count < 0 ? 0 : strlen (label->text);
5914 if (new_pos == old_pos)
5915 gtk_widget_error_bell (GTK_WIDGET (label));
5917 case GTK_MOVEMENT_DISPLAY_LINES:
5918 case GTK_MOVEMENT_PARAGRAPHS:
5919 case GTK_MOVEMENT_PAGES:
5920 case GTK_MOVEMENT_HORIZONTAL_PAGES:
5925 if (extend_selection)
5926 gtk_label_select_region_index (label,
5927 label->select_info->selection_anchor,
5930 gtk_label_select_region_index (label, new_pos, new_pos);
5934 gtk_label_copy_clipboard (GtkLabel *label)
5936 if (label->text && label->select_info)
5940 GtkClipboard *clipboard;
5942 start = MIN (label->select_info->selection_anchor,
5943 label->select_info->selection_end);
5944 end = MAX (label->select_info->selection_anchor,
5945 label->select_info->selection_end);
5947 len = strlen (label->text);
5955 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
5958 gtk_clipboard_set_text (clipboard, label->text + start, end - start);
5963 link = gtk_label_get_focus_link (label);
5965 gtk_clipboard_set_text (clipboard, link->uri, -1);
5971 gtk_label_select_all (GtkLabel *label)
5973 gtk_label_select_region_index (label, 0, strlen (label->text));
5976 /* Quick hack of a popup menu
5979 activate_cb (GtkWidget *menuitem,
5982 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
5983 g_signal_emit_by_name (label, signal);
5987 append_action_signal (GtkLabel *label,
5989 const gchar *stock_id,
5990 const gchar *signal,
5993 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
5995 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
5996 g_signal_connect (menuitem, "activate",
5997 G_CALLBACK (activate_cb), label);
5999 gtk_widget_set_sensitive (menuitem, sensitive);
6001 gtk_widget_show (menuitem);
6002 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6006 popup_menu_detach (GtkWidget *attach_widget,
6009 GtkLabel *label = GTK_LABEL (attach_widget);
6011 if (label->select_info)
6012 label->select_info->popup_menu = NULL;
6016 popup_position_func (GtkMenu *menu,
6027 label = GTK_LABEL (user_data);
6028 widget = GTK_WIDGET (label);
6030 g_return_if_fail (gtk_widget_get_realized (widget));
6032 screen = gtk_widget_get_screen (widget);
6033 gdk_window_get_origin (widget->window, x, y);
6035 *x += widget->allocation.x;
6036 *y += widget->allocation.y;
6038 gtk_widget_size_request (GTK_WIDGET (menu), &req);
6040 *x += widget->allocation.width / 2;
6041 *y += widget->allocation.height;
6043 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
6044 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
6048 open_link_activate_cb (GtkMenuItem *menu_item,
6053 link = gtk_label_get_current_link (label);
6056 emit_activate_link (label, link);
6060 copy_link_activate_cb (GtkMenuItem *menu_item,
6063 GtkClipboard *clipboard;
6066 uri = gtk_label_get_current_uri (label);
6069 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6070 gtk_clipboard_set_text (clipboard, uri, -1);
6075 gtk_label_popup_menu (GtkWidget *widget)
6077 gtk_label_do_popup (GTK_LABEL (widget), NULL);
6083 gtk_label_do_popup (GtkLabel *label,
6084 GdkEventButton *event)
6086 GtkWidget *menuitem;
6089 gboolean have_selection;
6092 if (!label->select_info)
6095 if (label->select_info->popup_menu)
6096 gtk_widget_destroy (label->select_info->popup_menu);
6098 label->select_info->popup_menu = menu = gtk_menu_new ();
6100 gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (label), popup_menu_detach);
6103 label->select_info->selection_anchor != label->select_info->selection_end;
6107 if (label->select_info->link_clicked)
6108 link = label->select_info->active_link;
6113 link = gtk_label_get_focus_link (label);
6115 if (!have_selection && link)
6118 menuitem = gtk_image_menu_item_new_with_mnemonic (_("_Open Link"));
6119 gtk_widget_show (menuitem);
6120 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6122 g_signal_connect (G_OBJECT (menuitem), "activate",
6123 G_CALLBACK (open_link_activate_cb), label);
6125 image = gtk_image_new_from_stock (GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_MENU);
6126 gtk_widget_show (image);
6127 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6129 /* Copy Link Address */
6130 menuitem = gtk_image_menu_item_new_with_mnemonic (_("Copy _Link Address"));
6131 gtk_widget_show (menuitem);
6132 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6134 g_signal_connect (G_OBJECT (menuitem), "activate",
6135 G_CALLBACK (copy_link_activate_cb), label);
6137 image = gtk_image_new_from_stock (GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
6138 gtk_widget_show (image);
6139 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6143 append_action_signal (label, menu, GTK_STOCK_CUT, "cut-clipboard", FALSE);
6144 append_action_signal (label, menu, GTK_STOCK_COPY, "copy-clipboard", have_selection);
6145 append_action_signal (label, menu, GTK_STOCK_PASTE, "paste-clipboard", FALSE);
6147 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
6148 gtk_widget_set_sensitive (menuitem, FALSE);
6149 gtk_widget_show (menuitem);
6150 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6152 menuitem = gtk_separator_menu_item_new ();
6153 gtk_widget_show (menuitem);
6154 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6156 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
6157 g_signal_connect_swapped (menuitem, "activate",
6158 G_CALLBACK (gtk_label_select_all), label);
6159 gtk_widget_show (menuitem);
6160 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6163 g_signal_emit (label, signals[POPULATE_POPUP], 0, menu);
6166 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6168 event->button, event->time);
6171 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6172 popup_position_func, label,
6173 0, gtk_get_current_event_time ());
6174 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
6179 gtk_label_clear_links (GtkLabel *label)
6181 if (!label->select_info)
6184 g_list_foreach (label->select_info->links, (GFunc)link_free, NULL);
6185 g_list_free (label->select_info->links);
6186 label->select_info->links = NULL;
6187 label->select_info->active_link = NULL;
6191 gtk_label_rescan_links (GtkLabel *label)
6193 PangoLayout *layout = label->layout;
6194 PangoAttrList *attlist;
6195 PangoAttrIterator *iter;
6198 if (!label->select_info || !label->select_info->links)
6201 attlist = pango_layout_get_attributes (layout);
6203 if (attlist == NULL)
6206 iter = pango_attr_list_get_iterator (attlist);
6208 links = label->select_info->links;
6212 PangoAttribute *underline;
6213 PangoAttribute *color;
6215 underline = pango_attr_iterator_get (iter, PANGO_ATTR_UNDERLINE);
6216 color = pango_attr_iterator_get (iter, PANGO_ATTR_FOREGROUND);
6218 if (underline != NULL && color != NULL)
6221 PangoRectangle start_pos;
6222 PangoRectangle end_pos;
6225 pango_attr_iterator_range (iter, &start, &end);
6226 pango_layout_index_to_pos (layout, start, &start_pos);
6227 pango_layout_index_to_pos (layout, end, &end_pos);
6231 g_warning ("Ran out of links");
6235 links = links->next;
6236 link->start = start;
6239 } while (pango_attr_iterator_next (iter));
6241 pango_attr_iterator_destroy (iter);
6245 gtk_label_activate_link (GtkLabel *label,
6248 GtkWidget *widget = GTK_WIDGET (label);
6249 GError *error = NULL;
6251 if (!gtk_show_uri (gtk_widget_get_screen (widget),
6252 uri, gtk_get_current_event_time (), &error))
6254 g_warning ("Unable to show '%s': %s", uri, error->message);
6255 g_error_free (error);
6262 emit_activate_link (GtkLabel *label,
6267 g_signal_emit (label, signals[ACTIVATE_LINK], 0, link->uri, &handled);
6268 if (handled && label->track_links && !link->visited)
6270 link->visited = TRUE;
6271 /* FIXME: shouldn't have to redo everything here */
6272 gtk_label_recalculate (label);
6277 gtk_label_activate_current_link (GtkLabel *label)
6280 GtkWidget *widget = GTK_WIDGET (label);
6282 link = gtk_label_get_focus_link (label);
6286 emit_activate_link (label, link);
6290 GtkWidget *toplevel;
6293 toplevel = gtk_widget_get_toplevel (widget);
6294 if (GTK_IS_WINDOW (toplevel))
6296 window = GTK_WINDOW (toplevel);
6299 window->default_widget != widget &&
6300 !(widget == window->focus_widget &&
6301 (!window->default_widget || !gtk_widget_is_sensitive (window->default_widget))))
6302 gtk_window_activate_default (window);
6307 static GtkLabelLink *
6308 gtk_label_get_current_link (GtkLabel *label)
6312 if (!label->select_info)
6315 if (label->select_info->link_clicked)
6316 link = label->select_info->active_link;
6318 link = gtk_label_get_focus_link (label);
6324 * gtk_label_get_current_uri:
6325 * @label: a #GtkLabel
6327 * Returns the URI for the currently active link in the label.
6328 * The active link is the one under the mouse pointer or, in a
6329 * selectable label, the link in which the text cursor is currently
6332 * This function is intended for use in a #GtkLabel::activate-link handler
6333 * or for use in a #GtkWidget::query-tooltip handler.
6335 * Returns: the currently active URI. The string is owned by GTK+ and must
6336 * not be freed or modified.
6340 G_CONST_RETURN gchar *
6341 gtk_label_get_current_uri (GtkLabel *label)
6344 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
6346 link = gtk_label_get_current_link (label);
6355 * gtk_label_set_track_visited_links:
6356 * @label: a #GtkLabel
6357 * @track_links: %TRUE to track visited links
6359 * Sets whether the label should keep track of clicked
6360 * links (and use a different color for them).
6365 gtk_label_set_track_visited_links (GtkLabel *label,
6366 gboolean track_links)
6368 g_return_if_fail (GTK_IS_LABEL (label));
6370 track_links = track_links != FALSE;
6372 if (label->track_links != track_links)
6374 label->track_links = track_links;
6376 /* FIXME: shouldn't have to redo everything here */
6377 gtk_label_recalculate (label);
6379 g_object_notify (G_OBJECT (label), "track-visited-links");
6384 * gtk_label_get_track_visited_links:
6385 * @label: a #GtkLabel
6387 * Returns whether the label is currently keeping track
6390 * Returns: %TRUE if clicked links are remembered
6395 gtk_label_get_track_visited_links (GtkLabel *label)
6397 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
6399 return label->track_links;
6403 gtk_label_query_tooltip (GtkWidget *widget,
6406 gboolean keyboard_tip,
6407 GtkTooltip *tooltip)
6409 GtkLabel *label = GTK_LABEL (widget);
6410 GtkLabelSelectionInfo *info = label->select_info;
6414 if (info && info->links)
6418 if (info->selection_anchor == info->selection_end)
6419 index = info->selection_anchor;
6423 if (!get_layout_index (label, x, y, &index))
6429 for (l = info->links; l != NULL; l = l->next)
6431 GtkLabelLink *link = l->data;
6432 if (index >= link->start && index <= link->end)
6436 gtk_tooltip_set_markup (tooltip, link->title);
6445 return GTK_WIDGET_CLASS (gtk_label_parent_class)->query_tooltip (widget,
6452 #define __GTK_LABEL_C__
6453 #include "gtkaliasdef.c"