1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
21 * file for a list of people on the GTK+ Team. See the ChangeLog
22 * files for a list of changes. These files are distributed with
23 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
32 #include "gtkaccellabel.h"
34 #include "gtkmainprivate.h"
35 #include "gtkmarshalers.h"
37 #include "gtkwindow.h"
38 #include "gtkclipboard.h"
39 #include "gtkimagemenuitem.h"
41 #include "gtkseparatormenuitem.h"
42 #include "gtktextutil.h"
43 #include "gtkmenuitem.h"
44 #include "gtkmenushellprivate.h"
45 #include "gtknotebook.h"
47 #include "gtkbindings.h"
48 #include "gtkbuildable.h"
51 #include "gtktooltip.h"
52 #include "gtkprivate.h"
53 #include "gtktypebuiltins.h"
55 /*rint() is only available in GCC and/or C99*/
56 #if (__STDC_VERSION__ < 199901L && !defined __GNUC__)
59 if (ceil(x+0.5) == floor(x+0.5))
74 struct _GtkLabelPrivate
76 GtkLabelSelectionInfo *select_info;
77 GtkWidget *mnemonic_widget;
78 GtkWindow *mnemonic_window;
81 PangoAttrList *effective_attrs;
84 gboolean mnemonics_visible;
91 guint use_underline : 1;
94 guint single_line_mode : 1;
95 guint have_transform : 1;
98 guint pattern_set : 1;
99 guint track_links : 1;
101 guint mnemonic_keyval;
105 gint max_width_chars;
110 /* Notes about the handling of links:
112 * Links share the GtkLabelSelectionInfo struct with selectable labels.
113 * There are some new fields for links. The links field contains the list
114 * of GtkLabelLink structs that describe the links which are embedded in
115 * the label. The active_link field points to the link under the mouse
116 * pointer. For keyboard navigation, the 'focus' link is determined by
117 * finding the link which contains the selection_anchor position.
118 * The link_clicked field is used with button press and release events
119 * to ensure that pressing inside a link and releasing outside of it
120 * does not activate the link.
122 * Links are rendered with the link-color/visited-link-color colors
123 * that are determined by the style and with an underline. When the mouse
124 * pointer is over a link, the pointer is changed to indicate the link,
125 * and the background behind the link is rendered with the base[PRELIGHT]
126 * color. While a button is pressed over a link, the background is rendered
127 * with the base[ACTIVE] color.
129 * Labels with links accept keyboard focus, and it is possible to move
130 * the focus between the embedded links using Tab/Shift-Tab. The focus
131 * is indicated by a focus rectangle that is drawn around the link text.
132 * Pressing Enter activates the focussed link, and there is a suitable
133 * context menu for links that can be opened with the Menu key. Pressing
134 * Control-C copies the link URI to the clipboard.
136 * In selectable labels with links, link functionality is only available
137 * when the selection is empty.
142 gchar *title; /* the title attribute, used as tooltip */
143 gboolean visited; /* get set when the link is activated; this flag
144 * gets preserved over later set_markup() calls
146 gint start; /* position of the link in the PangoLayout */
150 struct _GtkLabelSelectionInfo
153 gint selection_anchor;
155 GtkWidget *popup_menu;
158 GtkLabelLink *active_link;
164 guint select_words : 1;
165 guint selectable : 1;
166 guint link_clicked : 1;
174 ACTIVATE_CURRENT_LINK,
189 PROP_MNEMONIC_KEYVAL,
190 PROP_MNEMONIC_WIDGET,
191 PROP_CURSOR_POSITION,
192 PROP_SELECTION_BOUND,
195 PROP_SINGLE_LINE_MODE,
197 PROP_MAX_WIDTH_CHARS,
198 PROP_TRACK_VISITED_LINKS
201 /* When rotating ellipsizable text we want the natural size to request
202 * more to ensure the label wont ever ellipsize in an allocation of full natural size.
204 #define ROTATION_ELLIPSIZE_PADDING 2
206 static guint signals[LAST_SIGNAL] = { 0 };
208 static const GdkColor default_link_color = { 0, 0, 0, 0xeeee };
209 static const GdkColor default_visited_link_color = { 0, 0x5555, 0x1a1a, 0x8b8b };
211 static void gtk_label_set_property (GObject *object,
215 static void gtk_label_get_property (GObject *object,
219 static void gtk_label_finalize (GObject *object);
220 static void gtk_label_destroy (GtkWidget *widget);
221 static void gtk_label_size_allocate (GtkWidget *widget,
222 GtkAllocation *allocation);
223 static void gtk_label_state_changed (GtkWidget *widget,
225 static void gtk_label_style_updated (GtkWidget *widget);
226 static void gtk_label_direction_changed (GtkWidget *widget,
227 GtkTextDirection previous_dir);
228 static gint gtk_label_draw (GtkWidget *widget,
230 static gboolean gtk_label_focus (GtkWidget *widget,
231 GtkDirectionType direction);
233 static void gtk_label_realize (GtkWidget *widget);
234 static void gtk_label_unrealize (GtkWidget *widget);
235 static void gtk_label_map (GtkWidget *widget);
236 static void gtk_label_unmap (GtkWidget *widget);
238 static gboolean gtk_label_button_press (GtkWidget *widget,
239 GdkEventButton *event);
240 static gboolean gtk_label_button_release (GtkWidget *widget,
241 GdkEventButton *event);
242 static gboolean gtk_label_motion (GtkWidget *widget,
243 GdkEventMotion *event);
244 static gboolean gtk_label_leave_notify (GtkWidget *widget,
245 GdkEventCrossing *event);
247 static void gtk_label_grab_focus (GtkWidget *widget);
249 static gboolean gtk_label_query_tooltip (GtkWidget *widget,
252 gboolean keyboard_tip,
253 GtkTooltip *tooltip);
255 static void gtk_label_set_text_internal (GtkLabel *label,
257 static void gtk_label_set_label_internal (GtkLabel *label,
259 static void gtk_label_set_use_markup_internal (GtkLabel *label,
261 static void gtk_label_set_use_underline_internal (GtkLabel *label,
263 static void gtk_label_set_attributes_internal (GtkLabel *label,
264 PangoAttrList *attrs);
265 static void gtk_label_set_uline_text_internal (GtkLabel *label,
267 static void gtk_label_set_pattern_internal (GtkLabel *label,
268 const gchar *pattern,
269 gboolean is_mnemonic);
270 static void gtk_label_set_markup_internal (GtkLabel *label,
272 gboolean with_uline);
273 static void gtk_label_recalculate (GtkLabel *label);
274 static void gtk_label_hierarchy_changed (GtkWidget *widget,
275 GtkWidget *old_toplevel);
276 static void gtk_label_screen_changed (GtkWidget *widget,
277 GdkScreen *old_screen);
278 static gboolean gtk_label_popup_menu (GtkWidget *widget);
280 static void gtk_label_create_window (GtkLabel *label);
281 static void gtk_label_destroy_window (GtkLabel *label);
282 static void gtk_label_ensure_select_info (GtkLabel *label);
283 static void gtk_label_clear_select_info (GtkLabel *label);
284 static void gtk_label_update_cursor (GtkLabel *label);
285 static void gtk_label_clear_layout (GtkLabel *label);
286 static void gtk_label_ensure_layout (GtkLabel *label, gboolean guess_wrap_width);
287 static void gtk_label_invalidate_wrap_width (GtkLabel *label);
288 static void gtk_label_select_region_index (GtkLabel *label,
292 static gboolean gtk_label_mnemonic_activate (GtkWidget *widget,
293 gboolean group_cycling);
294 static void gtk_label_setup_mnemonic (GtkLabel *label,
296 static void gtk_label_drag_data_get (GtkWidget *widget,
297 GdkDragContext *context,
298 GtkSelectionData *selection_data,
302 static void gtk_label_buildable_interface_init (GtkBuildableIface *iface);
303 static gboolean gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
306 const gchar *tagname,
307 GMarkupParser *parser,
310 static void gtk_label_buildable_custom_finished (GtkBuildable *buildable,
313 const gchar *tagname,
317 static void connect_mnemonics_visible_notify (GtkLabel *label);
318 static gboolean separate_uline_pattern (const gchar *str,
324 /* For selectable labels: */
325 static void gtk_label_move_cursor (GtkLabel *label,
326 GtkMovementStep step,
328 gboolean extend_selection);
329 static void gtk_label_copy_clipboard (GtkLabel *label);
330 static void gtk_label_select_all (GtkLabel *label);
331 static void gtk_label_do_popup (GtkLabel *label,
332 GdkEventButton *event);
333 static gint gtk_label_move_forward_word (GtkLabel *label,
335 static gint gtk_label_move_backward_word (GtkLabel *label,
339 static void gtk_label_rescan_links (GtkLabel *label);
340 static void gtk_label_clear_links (GtkLabel *label);
341 static gboolean gtk_label_activate_link (GtkLabel *label,
343 static void gtk_label_activate_current_link (GtkLabel *label);
344 static GtkLabelLink *gtk_label_get_current_link (GtkLabel *label);
345 static void gtk_label_get_link_colors (GtkWidget *widget,
346 GdkColor **link_color,
347 GdkColor **visited_link_color);
348 static void emit_activate_link (GtkLabel *label,
351 static GtkSizeRequestMode gtk_label_get_request_mode (GtkWidget *widget);
352 static void gtk_label_get_preferred_width (GtkWidget *widget,
355 static void gtk_label_get_preferred_height (GtkWidget *widget,
358 static void gtk_label_get_preferred_width_for_height (GtkWidget *widget,
361 gint *natural_width);
362 static void gtk_label_get_preferred_height_for_width (GtkWidget *widget,
364 gint *minimum_height,
365 gint *natural_height);
367 static GtkBuildableIface *buildable_parent_iface = NULL;
369 G_DEFINE_TYPE_WITH_CODE (GtkLabel, gtk_label, GTK_TYPE_MISC,
370 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
371 gtk_label_buildable_interface_init))
374 add_move_binding (GtkBindingSet *binding_set,
377 GtkMovementStep step,
380 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
382 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
386 G_TYPE_BOOLEAN, FALSE);
388 /* Selection-extending version */
389 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
393 G_TYPE_BOOLEAN, TRUE);
397 gtk_label_class_init (GtkLabelClass *class)
399 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
400 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
401 GtkBindingSet *binding_set;
403 gobject_class->set_property = gtk_label_set_property;
404 gobject_class->get_property = gtk_label_get_property;
405 gobject_class->finalize = gtk_label_finalize;
407 widget_class->destroy = gtk_label_destroy;
408 widget_class->size_allocate = gtk_label_size_allocate;
409 widget_class->state_changed = gtk_label_state_changed;
410 widget_class->style_updated = gtk_label_style_updated;
411 widget_class->query_tooltip = gtk_label_query_tooltip;
412 widget_class->direction_changed = gtk_label_direction_changed;
413 widget_class->draw = gtk_label_draw;
414 widget_class->realize = gtk_label_realize;
415 widget_class->unrealize = gtk_label_unrealize;
416 widget_class->map = gtk_label_map;
417 widget_class->unmap = gtk_label_unmap;
418 widget_class->button_press_event = gtk_label_button_press;
419 widget_class->button_release_event = gtk_label_button_release;
420 widget_class->motion_notify_event = gtk_label_motion;
421 widget_class->leave_notify_event = gtk_label_leave_notify;
422 widget_class->hierarchy_changed = gtk_label_hierarchy_changed;
423 widget_class->screen_changed = gtk_label_screen_changed;
424 widget_class->mnemonic_activate = gtk_label_mnemonic_activate;
425 widget_class->drag_data_get = gtk_label_drag_data_get;
426 widget_class->grab_focus = gtk_label_grab_focus;
427 widget_class->popup_menu = gtk_label_popup_menu;
428 widget_class->focus = gtk_label_focus;
429 widget_class->get_request_mode = gtk_label_get_request_mode;
430 widget_class->get_preferred_width = gtk_label_get_preferred_width;
431 widget_class->get_preferred_height = gtk_label_get_preferred_height;
432 widget_class->get_preferred_width_for_height = gtk_label_get_preferred_width_for_height;
433 widget_class->get_preferred_height_for_width = gtk_label_get_preferred_height_for_width;
435 class->move_cursor = gtk_label_move_cursor;
436 class->copy_clipboard = gtk_label_copy_clipboard;
437 class->activate_link = gtk_label_activate_link;
440 * GtkLabel::move-cursor:
441 * @entry: the object which received the signal
442 * @step: the granularity of the move, as a #GtkMovementStep
443 * @count: the number of @step units to move
444 * @extend_selection: %TRUE if the move should extend the selection
446 * The ::move-cursor signal is a
447 * <link linkend="keybinding-signals">keybinding signal</link>
448 * which gets emitted when the user initiates a cursor movement.
449 * If the cursor is not visible in @entry, this signal causes
450 * the viewport to be moved instead.
452 * Applications should not connect to it, but may emit it with
453 * g_signal_emit_by_name() if they need to control the cursor
456 * The default bindings for this signal come in two variants,
457 * the variant with the Shift modifier extends the selection,
458 * the variant without the Shift modifer does not.
459 * There are too many key combinations to list them all here.
461 * <listitem>Arrow keys move by individual characters/lines</listitem>
462 * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
463 * <listitem>Home/End keys move to the ends of the buffer</listitem>
466 signals[MOVE_CURSOR] =
467 g_signal_new (I_("move-cursor"),
468 G_OBJECT_CLASS_TYPE (gobject_class),
469 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
470 G_STRUCT_OFFSET (GtkLabelClass, move_cursor),
472 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
474 GTK_TYPE_MOVEMENT_STEP,
479 * GtkLabel::copy-clipboard:
480 * @label: the object which received the signal
482 * The ::copy-clipboard signal is a
483 * <link linkend="keybinding-signals">keybinding signal</link>
484 * which gets emitted to copy the selection to the clipboard.
486 * The default binding for this signal is Ctrl-c.
488 signals[COPY_CLIPBOARD] =
489 g_signal_new (I_("copy-clipboard"),
490 G_OBJECT_CLASS_TYPE (gobject_class),
491 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
492 G_STRUCT_OFFSET (GtkLabelClass, copy_clipboard),
494 _gtk_marshal_VOID__VOID,
498 * GtkLabel::populate-popup:
499 * @label: The label on which the signal is emitted
500 * @menu: the menu that is being populated
502 * The ::populate-popup signal gets emitted before showing the
503 * context menu of the label. Note that only selectable labels
504 * have context menus.
506 * If you need to add items to the context menu, connect
507 * to this signal and append your menuitems to the @menu.
509 signals[POPULATE_POPUP] =
510 g_signal_new (I_("populate-popup"),
511 G_OBJECT_CLASS_TYPE (gobject_class),
513 G_STRUCT_OFFSET (GtkLabelClass, populate_popup),
515 _gtk_marshal_VOID__OBJECT,
520 * GtkLabel::activate-current-link:
521 * @label: The label on which the signal was emitted
523 * A <link linkend="keybinding-signals">keybinding signal</link>
524 * which gets emitted when the user activates a link in the label.
526 * Applications may also emit the signal with g_signal_emit_by_name()
527 * if they need to control activation of URIs programmatically.
529 * The default bindings for this signal are all forms of the Enter key.
533 signals[ACTIVATE_CURRENT_LINK] =
534 g_signal_new_class_handler ("activate-current-link",
535 G_TYPE_FROM_CLASS (gobject_class),
536 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
537 G_CALLBACK (gtk_label_activate_current_link),
539 _gtk_marshal_VOID__VOID,
543 * GtkLabel::activate-link:
544 * @label: The label on which the signal was emitted
545 * @uri: the URI that is activated
547 * The signal which gets emitted to activate a URI.
548 * Applications may connect to it to override the default behaviour,
549 * which is to call gtk_show_uri().
551 * Returns: %TRUE if the link has been activated
555 signals[ACTIVATE_LINK] =
556 g_signal_new ("activate-link",
557 G_TYPE_FROM_CLASS (gobject_class),
559 G_STRUCT_OFFSET (GtkLabelClass, activate_link),
560 _gtk_boolean_handled_accumulator, NULL,
561 _gtk_marshal_BOOLEAN__STRING,
562 G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
564 g_object_class_install_property (gobject_class,
566 g_param_spec_string ("label",
568 P_("The text of the label"),
570 GTK_PARAM_READWRITE));
571 g_object_class_install_property (gobject_class,
573 g_param_spec_boxed ("attributes",
575 P_("A list of style attributes to apply to the text of the label"),
576 PANGO_TYPE_ATTR_LIST,
577 GTK_PARAM_READWRITE));
578 g_object_class_install_property (gobject_class,
580 g_param_spec_boolean ("use-markup",
582 P_("The text of the label includes XML markup. See pango_parse_markup()"),
584 GTK_PARAM_READWRITE));
585 g_object_class_install_property (gobject_class,
587 g_param_spec_boolean ("use-underline",
589 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
591 GTK_PARAM_READWRITE));
593 g_object_class_install_property (gobject_class,
595 g_param_spec_enum ("justify",
597 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"),
598 GTK_TYPE_JUSTIFICATION,
600 GTK_PARAM_READWRITE));
602 g_object_class_install_property (gobject_class,
604 g_param_spec_string ("pattern",
606 P_("A string with _ characters in positions correspond to characters in the text to underline"),
608 GTK_PARAM_WRITABLE));
610 g_object_class_install_property (gobject_class,
612 g_param_spec_boolean ("wrap",
614 P_("If set, wrap lines if the text becomes too wide"),
616 GTK_PARAM_READWRITE));
618 * GtkLabel:wrap-mode:
620 * If line wrapping is on (see the #GtkLabel:wrap property) this controls
621 * how the line wrapping is done. The default is %PANGO_WRAP_WORD, which
622 * means wrap on word boundaries.
626 g_object_class_install_property (gobject_class,
628 g_param_spec_enum ("wrap-mode",
629 P_("Line wrap mode"),
630 P_("If wrap is set, controls how linewrapping is done"),
631 PANGO_TYPE_WRAP_MODE,
633 GTK_PARAM_READWRITE));
634 g_object_class_install_property (gobject_class,
636 g_param_spec_boolean ("selectable",
638 P_("Whether the label text can be selected with the mouse"),
640 GTK_PARAM_READWRITE));
641 g_object_class_install_property (gobject_class,
642 PROP_MNEMONIC_KEYVAL,
643 g_param_spec_uint ("mnemonic-keyval",
645 P_("The mnemonic accelerator key for this label"),
649 GTK_PARAM_READABLE));
650 g_object_class_install_property (gobject_class,
651 PROP_MNEMONIC_WIDGET,
652 g_param_spec_object ("mnemonic-widget",
653 P_("Mnemonic widget"),
654 P_("The widget to be activated when the label's mnemonic "
657 GTK_PARAM_READWRITE));
659 g_object_class_install_property (gobject_class,
660 PROP_CURSOR_POSITION,
661 g_param_spec_int ("cursor-position",
662 P_("Cursor Position"),
663 P_("The current position of the insertion cursor in chars"),
667 GTK_PARAM_READABLE));
669 g_object_class_install_property (gobject_class,
670 PROP_SELECTION_BOUND,
671 g_param_spec_int ("selection-bound",
672 P_("Selection Bound"),
673 P_("The position of the opposite end of the selection from the cursor in chars"),
677 GTK_PARAM_READABLE));
680 * GtkLabel:ellipsize:
682 * The preferred place to ellipsize the string, if the label does
683 * not have enough room to display the entire string, specified as a
684 * #PangoEllisizeMode.
686 * Note that setting this property to a value other than
687 * %PANGO_ELLIPSIZE_NONE has the side-effect that the label requests
688 * only enough space to display the ellipsis "...". In particular, this
689 * means that ellipsizing labels do not work well in notebook tabs, unless
690 * the tab's #GtkNotebook:tab-expand property is set to %TRUE. Other ways
691 * to set a label's width are gtk_widget_set_size_request() and
692 * gtk_label_set_width_chars().
696 g_object_class_install_property (gobject_class,
698 g_param_spec_enum ("ellipsize",
700 P_("The preferred place to ellipsize the string, if the label does not have enough room to display the entire string"),
701 PANGO_TYPE_ELLIPSIZE_MODE,
702 PANGO_ELLIPSIZE_NONE,
703 GTK_PARAM_READWRITE));
706 * GtkLabel:width-chars:
708 * The desired width of the label, in characters. If this property is set to
709 * -1, the width will be calculated automatically.
711 * See the section on <link linkend="label-text-layout">text layout</link>
712 * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
713 * determine the width of ellipsized and wrapped labels.
717 g_object_class_install_property (gobject_class,
719 g_param_spec_int ("width-chars",
720 P_("Width In Characters"),
721 P_("The desired width of the label, in characters"),
725 GTK_PARAM_READWRITE));
728 * GtkLabel:single-line-mode:
730 * Whether the label is in single line mode. In single line mode,
731 * the height of the label does not depend on the actual text, it
732 * is always set to ascent + descent of the font. This can be an
733 * advantage in situations where resizing the label because of text
734 * changes would be distracting, e.g. in a statusbar.
738 g_object_class_install_property (gobject_class,
739 PROP_SINGLE_LINE_MODE,
740 g_param_spec_boolean ("single-line-mode",
741 P_("Single Line Mode"),
742 P_("Whether the label is in single line mode"),
744 GTK_PARAM_READWRITE));
749 * The angle that the baseline of the label makes with the horizontal,
750 * in degrees, measured counterclockwise. An angle of 90 reads from
751 * from bottom to top, an angle of 270, from top to bottom. Ignored
752 * if the label is selectable, wrapped, or ellipsized.
756 g_object_class_install_property (gobject_class,
758 g_param_spec_double ("angle",
760 P_("Angle at which the label is rotated"),
764 GTK_PARAM_READWRITE));
767 * GtkLabel:max-width-chars:
769 * The desired maximum width of the label, in characters. If this property
770 * is set to -1, the width will be calculated automatically.
772 * See the section on <link linkend="label-text-layout">text layout</link>
773 * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
774 * determine the width of ellipsized and wrapped labels.
778 g_object_class_install_property (gobject_class,
779 PROP_MAX_WIDTH_CHARS,
780 g_param_spec_int ("max-width-chars",
781 P_("Maximum Width In Characters"),
782 P_("The desired maximum width of the label, in characters"),
786 GTK_PARAM_READWRITE));
789 * GtkLabel:track-visited-links:
791 * Set this property to %TRUE to make the label track which links
792 * have been clicked. It will then apply the ::visited-link-color
793 * color, instead of ::link-color.
797 g_object_class_install_property (gobject_class,
798 PROP_TRACK_VISITED_LINKS,
799 g_param_spec_boolean ("track-visited-links",
800 P_("Track visited links"),
801 P_("Whether visited links should be tracked"),
803 GTK_PARAM_READWRITE));
808 binding_set = gtk_binding_set_by_class (class);
810 /* Moving the insertion point */
811 add_move_binding (binding_set, GDK_KEY_Right, 0,
812 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
814 add_move_binding (binding_set, GDK_KEY_Left, 0,
815 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
817 add_move_binding (binding_set, GDK_KEY_KP_Right, 0,
818 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
820 add_move_binding (binding_set, GDK_KEY_KP_Left, 0,
821 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
823 add_move_binding (binding_set, GDK_KEY_f, GDK_CONTROL_MASK,
824 GTK_MOVEMENT_LOGICAL_POSITIONS, 1);
826 add_move_binding (binding_set, GDK_KEY_b, GDK_CONTROL_MASK,
827 GTK_MOVEMENT_LOGICAL_POSITIONS, -1);
829 add_move_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK,
830 GTK_MOVEMENT_WORDS, 1);
832 add_move_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK,
833 GTK_MOVEMENT_WORDS, -1);
835 add_move_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK,
836 GTK_MOVEMENT_WORDS, 1);
838 add_move_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK,
839 GTK_MOVEMENT_WORDS, -1);
842 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
844 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
846 G_TYPE_BOOLEAN, FALSE);
848 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
850 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
852 G_TYPE_BOOLEAN, TRUE);
854 gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
856 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
858 G_TYPE_BOOLEAN, FALSE);
860 gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
862 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
864 G_TYPE_BOOLEAN, TRUE);
867 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
869 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
871 G_TYPE_BOOLEAN, FALSE);
873 gtk_binding_entry_add_signal (binding_set, GDK_KEY_backslash, GDK_CONTROL_MASK,
875 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
877 G_TYPE_BOOLEAN, FALSE);
879 add_move_binding (binding_set, GDK_KEY_f, GDK_MOD1_MASK,
880 GTK_MOVEMENT_WORDS, 1);
882 add_move_binding (binding_set, GDK_KEY_b, GDK_MOD1_MASK,
883 GTK_MOVEMENT_WORDS, -1);
885 add_move_binding (binding_set, GDK_KEY_Home, 0,
886 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
888 add_move_binding (binding_set, GDK_KEY_End, 0,
889 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
891 add_move_binding (binding_set, GDK_KEY_KP_Home, 0,
892 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
894 add_move_binding (binding_set, GDK_KEY_KP_End, 0,
895 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
897 add_move_binding (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK,
898 GTK_MOVEMENT_BUFFER_ENDS, -1);
900 add_move_binding (binding_set, GDK_KEY_End, GDK_CONTROL_MASK,
901 GTK_MOVEMENT_BUFFER_ENDS, 1);
903 add_move_binding (binding_set, GDK_KEY_KP_Home, GDK_CONTROL_MASK,
904 GTK_MOVEMENT_BUFFER_ENDS, -1);
906 add_move_binding (binding_set, GDK_KEY_KP_End, GDK_CONTROL_MASK,
907 GTK_MOVEMENT_BUFFER_ENDS, 1);
910 gtk_binding_entry_add_signal (binding_set, GDK_KEY_c, GDK_CONTROL_MASK,
911 "copy-clipboard", 0);
913 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Return, 0,
914 "activate-current-link", 0);
915 gtk_binding_entry_add_signal (binding_set, GDK_KEY_ISO_Enter, 0,
916 "activate-current-link", 0);
917 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Enter, 0,
918 "activate-current-link", 0);
920 g_type_class_add_private (class, sizeof (GtkLabelPrivate));
924 gtk_label_set_property (GObject *object,
929 GtkLabel *label = GTK_LABEL (object);
934 gtk_label_set_label (label, g_value_get_string (value));
936 case PROP_ATTRIBUTES:
937 gtk_label_set_attributes (label, g_value_get_boxed (value));
939 case PROP_USE_MARKUP:
940 gtk_label_set_use_markup (label, g_value_get_boolean (value));
942 case PROP_USE_UNDERLINE:
943 gtk_label_set_use_underline (label, g_value_get_boolean (value));
946 gtk_label_set_justify (label, g_value_get_enum (value));
949 gtk_label_set_pattern (label, g_value_get_string (value));
952 gtk_label_set_line_wrap (label, g_value_get_boolean (value));
955 gtk_label_set_line_wrap_mode (label, g_value_get_enum (value));
957 case PROP_SELECTABLE:
958 gtk_label_set_selectable (label, g_value_get_boolean (value));
960 case PROP_MNEMONIC_WIDGET:
961 gtk_label_set_mnemonic_widget (label, (GtkWidget*) g_value_get_object (value));
964 gtk_label_set_ellipsize (label, g_value_get_enum (value));
966 case PROP_WIDTH_CHARS:
967 gtk_label_set_width_chars (label, g_value_get_int (value));
969 case PROP_SINGLE_LINE_MODE:
970 gtk_label_set_single_line_mode (label, g_value_get_boolean (value));
973 gtk_label_set_angle (label, g_value_get_double (value));
975 case PROP_MAX_WIDTH_CHARS:
976 gtk_label_set_max_width_chars (label, g_value_get_int (value));
978 case PROP_TRACK_VISITED_LINKS:
979 gtk_label_set_track_visited_links (label, g_value_get_boolean (value));
982 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
988 gtk_label_get_property (GObject *object,
993 GtkLabel *label = GTK_LABEL (object);
994 GtkLabelPrivate *priv = label->priv;
999 g_value_set_string (value, priv->label);
1001 case PROP_ATTRIBUTES:
1002 g_value_set_boxed (value, priv->attrs);
1004 case PROP_USE_MARKUP:
1005 g_value_set_boolean (value, priv->use_markup);
1007 case PROP_USE_UNDERLINE:
1008 g_value_set_boolean (value, priv->use_underline);
1011 g_value_set_enum (value, priv->jtype);
1014 g_value_set_boolean (value, priv->wrap);
1016 case PROP_WRAP_MODE:
1017 g_value_set_enum (value, priv->wrap_mode);
1019 case PROP_SELECTABLE:
1020 g_value_set_boolean (value, gtk_label_get_selectable (label));
1022 case PROP_MNEMONIC_KEYVAL:
1023 g_value_set_uint (value, priv->mnemonic_keyval);
1025 case PROP_MNEMONIC_WIDGET:
1026 g_value_set_object (value, (GObject*) priv->mnemonic_widget);
1028 case PROP_CURSOR_POSITION:
1029 if (priv->select_info && priv->select_info->selectable)
1031 gint offset = g_utf8_pointer_to_offset (priv->text,
1032 priv->text + priv->select_info->selection_end);
1033 g_value_set_int (value, offset);
1036 g_value_set_int (value, 0);
1038 case PROP_SELECTION_BOUND:
1039 if (priv->select_info && priv->select_info->selectable)
1041 gint offset = g_utf8_pointer_to_offset (priv->text,
1042 priv->text + priv->select_info->selection_anchor);
1043 g_value_set_int (value, offset);
1046 g_value_set_int (value, 0);
1048 case PROP_ELLIPSIZE:
1049 g_value_set_enum (value, priv->ellipsize);
1051 case PROP_WIDTH_CHARS:
1052 g_value_set_int (value, gtk_label_get_width_chars (label));
1054 case PROP_SINGLE_LINE_MODE:
1055 g_value_set_boolean (value, gtk_label_get_single_line_mode (label));
1058 g_value_set_double (value, gtk_label_get_angle (label));
1060 case PROP_MAX_WIDTH_CHARS:
1061 g_value_set_int (value, gtk_label_get_max_width_chars (label));
1063 case PROP_TRACK_VISITED_LINKS:
1064 g_value_set_boolean (value, gtk_label_get_track_visited_links (label));
1067 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1073 gtk_label_init (GtkLabel *label)
1075 GtkLabelPrivate *priv;
1077 label->priv = G_TYPE_INSTANCE_GET_PRIVATE (label,
1082 gtk_widget_set_has_window (GTK_WIDGET (label), FALSE);
1084 priv->width_chars = -1;
1085 priv->max_width_chars = -1;
1086 priv->wrap_width = -1;
1089 priv->jtype = GTK_JUSTIFY_LEFT;
1091 priv->wrap_mode = PANGO_WRAP_WORD;
1092 priv->ellipsize = PANGO_ELLIPSIZE_NONE;
1094 priv->use_underline = FALSE;
1095 priv->use_markup = FALSE;
1096 priv->pattern_set = FALSE;
1097 priv->track_links = TRUE;
1099 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
1100 priv->layout = NULL;
1104 priv->mnemonic_widget = NULL;
1105 priv->mnemonic_window = NULL;
1107 priv->mnemonics_visible = TRUE;
1109 gtk_label_set_text (label, "");
1114 gtk_label_buildable_interface_init (GtkBuildableIface *iface)
1116 buildable_parent_iface = g_type_interface_peek_parent (iface);
1118 iface->custom_tag_start = gtk_label_buildable_custom_tag_start;
1119 iface->custom_finished = gtk_label_buildable_custom_finished;
1123 GtkBuilder *builder;
1125 PangoAttrList *attrs;
1128 static PangoAttribute *
1129 attribute_from_text (GtkBuilder *builder,
1134 PangoAttribute *attribute = NULL;
1136 PangoLanguage *language;
1137 PangoFontDescription *font_desc;
1139 GValue val = { 0, };
1141 if (!gtk_builder_value_from_string_type (builder, PANGO_TYPE_ATTR_TYPE, name, &val, error))
1144 type = g_value_get_enum (&val);
1145 g_value_unset (&val);
1149 /* PangoAttrLanguage */
1150 case PANGO_ATTR_LANGUAGE:
1151 if ((language = pango_language_from_string (value)))
1153 attribute = pango_attr_language_new (language);
1154 g_value_init (&val, G_TYPE_INT);
1158 case PANGO_ATTR_STYLE:
1159 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STYLE, value, &val, error))
1160 attribute = pango_attr_style_new (g_value_get_enum (&val));
1162 case PANGO_ATTR_WEIGHT:
1163 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_WEIGHT, value, &val, error))
1164 attribute = pango_attr_weight_new (g_value_get_enum (&val));
1166 case PANGO_ATTR_VARIANT:
1167 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_VARIANT, value, &val, error))
1168 attribute = pango_attr_variant_new (g_value_get_enum (&val));
1170 case PANGO_ATTR_STRETCH:
1171 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STRETCH, value, &val, error))
1172 attribute = pango_attr_stretch_new (g_value_get_enum (&val));
1174 case PANGO_ATTR_UNDERLINE:
1175 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1176 attribute = pango_attr_underline_new (g_value_get_boolean (&val));
1178 case PANGO_ATTR_STRIKETHROUGH:
1179 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1180 attribute = pango_attr_strikethrough_new (g_value_get_boolean (&val));
1182 case PANGO_ATTR_GRAVITY:
1183 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY, value, &val, error))
1184 attribute = pango_attr_gravity_new (g_value_get_enum (&val));
1186 case PANGO_ATTR_GRAVITY_HINT:
1187 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY_HINT,
1188 value, &val, error))
1189 attribute = pango_attr_gravity_hint_new (g_value_get_enum (&val));
1191 /* PangoAttrString */
1192 case PANGO_ATTR_FAMILY:
1193 attribute = pango_attr_family_new (value);
1194 g_value_init (&val, G_TYPE_INT);
1198 case PANGO_ATTR_SIZE:
1199 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1200 value, &val, error))
1201 attribute = pango_attr_size_new (g_value_get_int (&val));
1203 case PANGO_ATTR_ABSOLUTE_SIZE:
1204 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1205 value, &val, error))
1206 attribute = pango_attr_size_new_absolute (g_value_get_int (&val));
1209 /* PangoAttrFontDesc */
1210 case PANGO_ATTR_FONT_DESC:
1211 if ((font_desc = pango_font_description_from_string (value)))
1213 attribute = pango_attr_font_desc_new (font_desc);
1214 pango_font_description_free (font_desc);
1215 g_value_init (&val, G_TYPE_INT);
1219 /* PangoAttrColor */
1220 case PANGO_ATTR_FOREGROUND:
1221 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1222 value, &val, error))
1224 color = g_value_get_boxed (&val);
1225 attribute = pango_attr_foreground_new (color->red, color->green, color->blue);
1228 case PANGO_ATTR_BACKGROUND:
1229 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1230 value, &val, error))
1232 color = g_value_get_boxed (&val);
1233 attribute = pango_attr_background_new (color->red, color->green, color->blue);
1236 case PANGO_ATTR_UNDERLINE_COLOR:
1237 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1238 value, &val, error))
1240 color = g_value_get_boxed (&val);
1241 attribute = pango_attr_underline_color_new (color->red, color->green, color->blue);
1244 case PANGO_ATTR_STRIKETHROUGH_COLOR:
1245 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1246 value, &val, error))
1248 color = g_value_get_boxed (&val);
1249 attribute = pango_attr_strikethrough_color_new (color->red, color->green, color->blue);
1253 /* PangoAttrShape */
1254 case PANGO_ATTR_SHAPE:
1255 /* Unsupported for now */
1257 /* PangoAttrFloat */
1258 case PANGO_ATTR_SCALE:
1259 if (gtk_builder_value_from_string_type (builder, G_TYPE_DOUBLE,
1260 value, &val, error))
1261 attribute = pango_attr_scale_new (g_value_get_double (&val));
1264 case PANGO_ATTR_INVALID:
1265 case PANGO_ATTR_LETTER_SPACING:
1266 case PANGO_ATTR_RISE:
1267 case PANGO_ATTR_FALLBACK:
1272 g_value_unset (&val);
1279 pango_start_element (GMarkupParseContext *context,
1280 const gchar *element_name,
1281 const gchar **names,
1282 const gchar **values,
1286 PangoParserData *data = (PangoParserData*)user_data;
1287 GValue val = { 0, };
1289 gint line_number, char_number;
1291 if (strcmp (element_name, "attribute") == 0)
1293 PangoAttribute *attr = NULL;
1294 const gchar *name = NULL;
1295 const gchar *value = NULL;
1296 const gchar *start = NULL;
1297 const gchar *end = NULL;
1298 guint start_val = 0;
1299 guint end_val = G_MAXUINT;
1301 for (i = 0; names[i]; i++)
1303 if (strcmp (names[i], "name") == 0)
1305 else if (strcmp (names[i], "value") == 0)
1307 else if (strcmp (names[i], "start") == 0)
1309 else if (strcmp (names[i], "end") == 0)
1313 g_markup_parse_context_get_position (context,
1318 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1319 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1321 line_number, char_number, names[i], "attribute");
1326 if (!name || !value)
1328 g_markup_parse_context_get_position (context,
1333 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1334 "%s:%d:%d <%s> requires attribute \"%s\"",
1336 line_number, char_number, "attribute",
1337 name ? "value" : "name");
1343 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1344 start, &val, error))
1346 start_val = g_value_get_uint (&val);
1347 g_value_unset (&val);
1352 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1355 end_val = g_value_get_uint (&val);
1356 g_value_unset (&val);
1359 attr = attribute_from_text (data->builder, name, value, error);
1360 attr->start_index = start_val;
1361 attr->end_index = end_val;
1366 data->attrs = pango_attr_list_new ();
1368 pango_attr_list_insert (data->attrs, attr);
1371 else if (strcmp (element_name, "attributes") == 0)
1374 g_warning ("Unsupported tag for GtkLabel: %s\n", element_name);
1377 static const GMarkupParser pango_parser =
1379 pango_start_element,
1383 gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
1384 GtkBuilder *builder,
1386 const gchar *tagname,
1387 GMarkupParser *parser,
1390 if (buildable_parent_iface->custom_tag_start (buildable, builder, child,
1391 tagname, parser, data))
1394 if (strcmp (tagname, "attributes") == 0)
1396 PangoParserData *parser_data;
1398 parser_data = g_slice_new0 (PangoParserData);
1399 parser_data->builder = g_object_ref (builder);
1400 parser_data->object = g_object_ref (buildable);
1401 *parser = pango_parser;
1402 *data = parser_data;
1409 gtk_label_buildable_custom_finished (GtkBuildable *buildable,
1410 GtkBuilder *builder,
1412 const gchar *tagname,
1415 PangoParserData *data;
1417 buildable_parent_iface->custom_finished (buildable, builder, child,
1418 tagname, user_data);
1420 if (strcmp (tagname, "attributes") == 0)
1422 data = (PangoParserData*)user_data;
1426 gtk_label_set_attributes (GTK_LABEL (buildable), data->attrs);
1427 pango_attr_list_unref (data->attrs);
1430 g_object_unref (data->object);
1431 g_object_unref (data->builder);
1432 g_slice_free (PangoParserData, data);
1439 * @str: The text of the label
1441 * Creates a new label with the given text inside it. You can
1442 * pass %NULL to get an empty label widget.
1444 * Return value: the new #GtkLabel
1447 gtk_label_new (const gchar *str)
1451 label = g_object_new (GTK_TYPE_LABEL, NULL);
1454 gtk_label_set_text (label, str);
1456 return GTK_WIDGET (label);
1460 * gtk_label_new_with_mnemonic:
1461 * @str: The text of the label, with an underscore in front of the
1462 * mnemonic character
1464 * Creates a new #GtkLabel, containing the text in @str.
1466 * If characters in @str are preceded by an underscore, they are
1467 * underlined. If you need a literal underscore character in a label, use
1468 * '__' (two underscores). The first underlined character represents a
1469 * keyboard accelerator called a mnemonic. The mnemonic key can be used
1470 * to activate another widget, chosen automatically, or explicitly using
1471 * gtk_label_set_mnemonic_widget().
1473 * If gtk_label_set_mnemonic_widget() is not called, then the first
1474 * activatable ancestor of the #GtkLabel will be chosen as the mnemonic
1475 * widget. For instance, if the label is inside a button or menu item,
1476 * the button or menu item will automatically become the mnemonic widget
1477 * and be activated by the mnemonic.
1479 * Return value: the new #GtkLabel
1482 gtk_label_new_with_mnemonic (const gchar *str)
1486 label = g_object_new (GTK_TYPE_LABEL, NULL);
1489 gtk_label_set_text_with_mnemonic (label, str);
1491 return GTK_WIDGET (label);
1495 gtk_label_mnemonic_activate (GtkWidget *widget,
1496 gboolean group_cycling)
1498 GtkLabel *label = GTK_LABEL (widget);
1499 GtkLabelPrivate *priv = label->priv;
1502 if (priv->mnemonic_widget)
1503 return gtk_widget_mnemonic_activate (priv->mnemonic_widget, group_cycling);
1505 /* Try to find the widget to activate by traversing the
1506 * widget's ancestry.
1508 parent = gtk_widget_get_parent (widget);
1510 if (GTK_IS_NOTEBOOK (parent))
1515 if (gtk_widget_get_can_focus (parent) ||
1516 (!group_cycling && GTK_WIDGET_GET_CLASS (parent)->activate_signal) ||
1517 GTK_IS_NOTEBOOK (gtk_widget_get_parent (parent)) ||
1518 GTK_IS_MENU_ITEM (parent))
1519 return gtk_widget_mnemonic_activate (parent, group_cycling);
1520 parent = gtk_widget_get_parent (parent);
1523 /* barf if there was nothing to activate */
1524 g_warning ("Couldn't find a target for a mnemonic activation.");
1525 gtk_widget_error_bell (widget);
1531 gtk_label_setup_mnemonic (GtkLabel *label,
1534 GtkLabelPrivate *priv = label->priv;
1535 GtkWidget *widget = GTK_WIDGET (label);
1536 GtkWidget *toplevel;
1537 GtkWidget *mnemonic_menu;
1539 mnemonic_menu = g_object_get_data (G_OBJECT (label), "gtk-mnemonic-menu");
1541 if (last_key != GDK_KEY_VoidSymbol)
1543 if (priv->mnemonic_window)
1545 gtk_window_remove_mnemonic (priv->mnemonic_window,
1548 priv->mnemonic_window = NULL;
1552 _gtk_menu_shell_remove_mnemonic (GTK_MENU_SHELL (mnemonic_menu),
1555 mnemonic_menu = NULL;
1559 if (priv->mnemonic_keyval == GDK_KEY_VoidSymbol)
1562 connect_mnemonics_visible_notify (GTK_LABEL (widget));
1564 toplevel = gtk_widget_get_toplevel (widget);
1565 if (gtk_widget_is_toplevel (toplevel))
1567 GtkWidget *menu_shell;
1569 menu_shell = gtk_widget_get_ancestor (widget,
1570 GTK_TYPE_MENU_SHELL);
1574 _gtk_menu_shell_add_mnemonic (GTK_MENU_SHELL (menu_shell),
1575 priv->mnemonic_keyval,
1577 mnemonic_menu = menu_shell;
1580 if (!GTK_IS_MENU (menu_shell))
1582 gtk_window_add_mnemonic (GTK_WINDOW (toplevel),
1583 priv->mnemonic_keyval,
1585 priv->mnemonic_window = GTK_WINDOW (toplevel);
1590 g_object_set_data (G_OBJECT (label), I_("gtk-mnemonic-menu"), mnemonic_menu);
1594 gtk_label_hierarchy_changed (GtkWidget *widget,
1595 GtkWidget *old_toplevel)
1597 GtkLabel *label = GTK_LABEL (widget);
1598 GtkLabelPrivate *priv = label->priv;
1600 gtk_label_setup_mnemonic (label, priv->mnemonic_keyval);
1604 label_shortcut_setting_apply (GtkLabel *label)
1606 gtk_label_recalculate (label);
1607 if (GTK_IS_ACCEL_LABEL (label))
1608 gtk_accel_label_refetch (GTK_ACCEL_LABEL (label));
1612 label_shortcut_setting_traverse_container (GtkWidget *widget,
1615 if (GTK_IS_LABEL (widget))
1616 label_shortcut_setting_apply (GTK_LABEL (widget));
1617 else if (GTK_IS_CONTAINER (widget))
1618 gtk_container_forall (GTK_CONTAINER (widget),
1619 label_shortcut_setting_traverse_container, data);
1623 label_shortcut_setting_changed (GtkSettings *settings)
1627 list = gtk_window_list_toplevels ();
1629 for (l = list; l ; l = l->next)
1631 GtkWidget *widget = l->data;
1633 if (gtk_widget_get_settings (widget) == settings)
1634 gtk_container_forall (GTK_CONTAINER (widget),
1635 label_shortcut_setting_traverse_container, NULL);
1642 mnemonics_visible_apply (GtkWidget *widget,
1643 gboolean mnemonics_visible)
1645 GtkLabel *label = GTK_LABEL (widget);
1646 GtkLabelPrivate *priv = label->priv;
1648 mnemonics_visible = mnemonics_visible != FALSE;
1650 if (priv->mnemonics_visible != mnemonics_visible)
1652 priv->mnemonics_visible = mnemonics_visible;
1654 gtk_label_recalculate (label);
1659 label_mnemonics_visible_traverse_container (GtkWidget *widget,
1662 gboolean mnemonics_visible = GPOINTER_TO_INT (data);
1664 _gtk_label_mnemonics_visible_apply_recursively (widget, mnemonics_visible);
1668 _gtk_label_mnemonics_visible_apply_recursively (GtkWidget *widget,
1669 gboolean mnemonics_visible)
1671 if (GTK_IS_LABEL (widget))
1672 mnemonics_visible_apply (widget, mnemonics_visible);
1673 else if (GTK_IS_CONTAINER (widget))
1674 gtk_container_forall (GTK_CONTAINER (widget),
1675 label_mnemonics_visible_traverse_container,
1676 GINT_TO_POINTER (mnemonics_visible));
1680 label_mnemonics_visible_changed (GtkWindow *window,
1684 gboolean mnemonics_visible;
1686 g_object_get (window, "mnemonics-visible", &mnemonics_visible, NULL);
1688 gtk_container_forall (GTK_CONTAINER (window),
1689 label_mnemonics_visible_traverse_container,
1690 GINT_TO_POINTER (mnemonics_visible));
1694 gtk_label_screen_changed (GtkWidget *widget,
1695 GdkScreen *old_screen)
1697 GtkSettings *settings;
1698 gboolean shortcuts_connected;
1700 if (!gtk_widget_has_screen (widget))
1703 settings = gtk_widget_get_settings (widget);
1705 shortcuts_connected =
1706 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (settings),
1707 "gtk-label-shortcuts-connected"));
1709 if (! shortcuts_connected)
1711 g_signal_connect (settings, "notify::gtk-enable-mnemonics",
1712 G_CALLBACK (label_shortcut_setting_changed),
1714 g_signal_connect (settings, "notify::gtk-enable-accels",
1715 G_CALLBACK (label_shortcut_setting_changed),
1718 g_object_set_data (G_OBJECT (settings), "gtk-label-shortcuts-connected",
1719 GINT_TO_POINTER (TRUE));
1722 label_shortcut_setting_apply (GTK_LABEL (widget));
1727 label_mnemonic_widget_weak_notify (gpointer data,
1728 GObject *where_the_object_was)
1730 GtkLabel *label = data;
1731 GtkLabelPrivate *priv = label->priv;
1733 priv->mnemonic_widget = NULL;
1734 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1738 * gtk_label_set_mnemonic_widget:
1739 * @label: a #GtkLabel
1740 * @widget: (allow-none): the target #GtkWidget
1742 * If the label has been set so that it has an mnemonic key (using
1743 * i.e. gtk_label_set_markup_with_mnemonic(),
1744 * gtk_label_set_text_with_mnemonic(), gtk_label_new_with_mnemonic()
1745 * or the "use_underline" property) the label can be associated with a
1746 * widget that is the target of the mnemonic. When the label is inside
1747 * a widget (like a #GtkButton or a #GtkNotebook tab) it is
1748 * automatically associated with the correct widget, but sometimes
1749 * (i.e. when the target is a #GtkEntry next to the label) you need to
1750 * set it explicitly using this function.
1752 * The target widget will be accelerated by emitting the
1753 * GtkWidget::mnemonic-activate signal on it. The default handler for
1754 * this signal will activate the widget if there are no mnemonic collisions
1755 * and toggle focus between the colliding widgets otherwise.
1758 gtk_label_set_mnemonic_widget (GtkLabel *label,
1761 GtkLabelPrivate *priv;
1763 g_return_if_fail (GTK_IS_LABEL (label));
1768 g_return_if_fail (GTK_IS_WIDGET (widget));
1770 if (priv->mnemonic_widget)
1772 gtk_widget_remove_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1773 g_object_weak_unref (G_OBJECT (priv->mnemonic_widget),
1774 label_mnemonic_widget_weak_notify,
1777 priv->mnemonic_widget = widget;
1778 if (priv->mnemonic_widget)
1780 g_object_weak_ref (G_OBJECT (priv->mnemonic_widget),
1781 label_mnemonic_widget_weak_notify,
1783 gtk_widget_add_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1786 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1790 * gtk_label_get_mnemonic_widget:
1791 * @label: a #GtkLabel
1793 * Retrieves the target of the mnemonic (keyboard shortcut) of this
1794 * label. See gtk_label_set_mnemonic_widget().
1796 * Return value: (transfer none): the target of the label's mnemonic,
1797 * or %NULL if none has been set and the default algorithm will be used.
1800 gtk_label_get_mnemonic_widget (GtkLabel *label)
1802 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1804 return label->priv->mnemonic_widget;
1808 * gtk_label_get_mnemonic_keyval:
1809 * @label: a #GtkLabel
1811 * If the label has been set so that it has an mnemonic key this function
1812 * returns the keyval used for the mnemonic accelerator. If there is no
1813 * mnemonic set up it returns #GDK_VoidSymbol.
1815 * Returns: GDK keyval usable for accelerators, or #GDK_VoidSymbol
1818 gtk_label_get_mnemonic_keyval (GtkLabel *label)
1820 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_KEY_VoidSymbol);
1822 return label->priv->mnemonic_keyval;
1826 gtk_label_set_text_internal (GtkLabel *label,
1829 GtkLabelPrivate *priv = label->priv;
1831 g_free (priv->text);
1835 gtk_label_select_region_index (label, 0, 0);
1839 gtk_label_set_label_internal (GtkLabel *label,
1842 GtkLabelPrivate *priv = label->priv;
1844 g_free (priv->label);
1848 g_object_notify (G_OBJECT (label), "label");
1852 gtk_label_set_use_markup_internal (GtkLabel *label,
1855 GtkLabelPrivate *priv = label->priv;
1858 if (priv->use_markup != val)
1860 priv->use_markup = val;
1862 g_object_notify (G_OBJECT (label), "use-markup");
1867 gtk_label_set_use_underline_internal (GtkLabel *label,
1870 GtkLabelPrivate *priv = label->priv;
1873 if (priv->use_underline != val)
1875 priv->use_underline = val;
1877 g_object_notify (G_OBJECT (label), "use-underline");
1882 gtk_label_compose_effective_attrs (GtkLabel *label)
1884 GtkLabelPrivate *priv = label->priv;
1885 PangoAttrIterator *iter;
1886 PangoAttribute *attr;
1887 GSList *iter_attrs, *l;
1891 if (priv->effective_attrs)
1893 if ((iter = pango_attr_list_get_iterator (priv->attrs)))
1897 iter_attrs = pango_attr_iterator_get_attrs (iter);
1898 for (l = iter_attrs; l; l = l->next)
1901 pango_attr_list_insert (priv->effective_attrs, attr);
1903 g_slist_free (iter_attrs);
1905 while (pango_attr_iterator_next (iter));
1906 pango_attr_iterator_destroy (iter);
1910 priv->effective_attrs =
1911 pango_attr_list_ref (priv->attrs);
1916 gtk_label_set_attributes_internal (GtkLabel *label,
1917 PangoAttrList *attrs)
1919 GtkLabelPrivate *priv = label->priv;
1922 pango_attr_list_ref (attrs);
1925 pango_attr_list_unref (priv->attrs);
1926 priv->attrs = attrs;
1928 g_object_notify (G_OBJECT (label), "attributes");
1932 /* Calculates text, attrs and mnemonic_keyval from
1933 * label, use_underline and use_markup
1936 gtk_label_recalculate (GtkLabel *label)
1938 GtkLabelPrivate *priv = label->priv;
1939 guint keyval = priv->mnemonic_keyval;
1941 if (priv->use_markup)
1942 gtk_label_set_markup_internal (label, priv->label, priv->use_underline);
1945 if (priv->use_underline)
1946 gtk_label_set_uline_text_internal (label, priv->label);
1949 if (priv->effective_attrs)
1950 pango_attr_list_unref (priv->effective_attrs);
1951 priv->effective_attrs = NULL;
1952 gtk_label_set_text_internal (label, g_strdup (priv->label));
1956 gtk_label_compose_effective_attrs (label);
1958 if (!priv->use_underline)
1959 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
1961 if (keyval != priv->mnemonic_keyval)
1963 gtk_label_setup_mnemonic (label, keyval);
1964 g_object_notify (G_OBJECT (label), "mnemonic-keyval");
1967 gtk_label_clear_layout (label);
1968 gtk_label_clear_select_info (label);
1969 gtk_widget_queue_resize (GTK_WIDGET (label));
1973 * gtk_label_set_text:
1974 * @label: a #GtkLabel
1975 * @str: The text you want to set
1977 * Sets the text within the #GtkLabel widget. It overwrites any text that
1980 * This will also clear any previously set mnemonic accelerators.
1983 gtk_label_set_text (GtkLabel *label,
1986 g_return_if_fail (GTK_IS_LABEL (label));
1988 g_object_freeze_notify (G_OBJECT (label));
1990 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
1991 gtk_label_set_use_markup_internal (label, FALSE);
1992 gtk_label_set_use_underline_internal (label, FALSE);
1994 gtk_label_recalculate (label);
1996 g_object_thaw_notify (G_OBJECT (label));
2000 * gtk_label_set_attributes:
2001 * @label: a #GtkLabel
2002 * @attrs: a #PangoAttrList
2004 * Sets a #PangoAttrList; the attributes in the list are applied to the
2007 * <note><para>The attributes set with this function will be applied
2008 * and merged with any other attributes previously effected by way
2009 * of the #GtkLabel:use-underline or #GtkLabel:use-markup properties.
2010 * While it is not recommended to mix markup strings with manually set
2011 * attributes, if you must; know that the attributes will be applied
2012 * to the label after the markup string is parsed.</para></note>
2015 gtk_label_set_attributes (GtkLabel *label,
2016 PangoAttrList *attrs)
2018 g_return_if_fail (GTK_IS_LABEL (label));
2020 gtk_label_set_attributes_internal (label, attrs);
2022 gtk_label_recalculate (label);
2024 gtk_label_clear_layout (label);
2025 gtk_widget_queue_resize (GTK_WIDGET (label));
2029 * gtk_label_get_attributes:
2030 * @label: a #GtkLabel
2032 * Gets the attribute list that was set on the label using
2033 * gtk_label_set_attributes(), if any. This function does
2034 * not reflect attributes that come from the labels markup
2035 * (see gtk_label_set_markup()). If you want to get the
2036 * effective attributes for the label, use
2037 * pango_layout_get_attribute (gtk_label_get_layout (label)).
2039 * Return value: (transfer none): the attribute list, or %NULL
2043 gtk_label_get_attributes (GtkLabel *label)
2045 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2047 return label->priv->attrs;
2051 * gtk_label_set_label:
2052 * @label: a #GtkLabel
2053 * @str: the new text to set for the label
2055 * Sets the text of the label. The label is interpreted as
2056 * including embedded underlines and/or Pango markup depending
2057 * on the values of the #GtkLabel:use-underline" and
2058 * #GtkLabel:use-markup properties.
2061 gtk_label_set_label (GtkLabel *label,
2064 g_return_if_fail (GTK_IS_LABEL (label));
2066 g_object_freeze_notify (G_OBJECT (label));
2068 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2069 gtk_label_recalculate (label);
2071 g_object_thaw_notify (G_OBJECT (label));
2075 * gtk_label_get_label:
2076 * @label: a #GtkLabel
2078 * Fetches the text from a label widget including any embedded
2079 * underlines indicating mnemonics and Pango markup. (See
2080 * gtk_label_get_text()).
2082 * Return value: the text of the label widget. This string is
2083 * owned by the widget and must not be modified or freed.
2085 G_CONST_RETURN gchar *
2086 gtk_label_get_label (GtkLabel *label)
2088 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2090 return label->priv->label;
2098 GdkColor *link_color;
2099 GdkColor *visited_link_color;
2103 start_element_handler (GMarkupParseContext *context,
2104 const gchar *element_name,
2105 const gchar **attribute_names,
2106 const gchar **attribute_values,
2110 GtkLabelPrivate *priv;
2111 UriParserData *pdata = user_data;
2113 if (strcmp (element_name, "a") == 0)
2116 const gchar *uri = NULL;
2117 const gchar *title = NULL;
2118 gboolean visited = FALSE;
2122 GdkColor *color = NULL;
2124 g_markup_parse_context_get_position (context, &line_number, &char_number);
2126 for (i = 0; attribute_names[i] != NULL; i++)
2128 const gchar *attr = attribute_names[i];
2130 if (strcmp (attr, "href") == 0)
2131 uri = attribute_values[i];
2132 else if (strcmp (attr, "title") == 0)
2133 title = attribute_values[i];
2138 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
2139 "Attribute '%s' is not allowed on the <a> tag "
2140 "on line %d char %d",
2141 attr, line_number, char_number);
2150 G_MARKUP_ERROR_INVALID_CONTENT,
2151 "Attribute 'href' was missing on the <a> tag "
2152 "on line %d char %d",
2153 line_number, char_number);
2158 priv = pdata->label->priv;
2159 if (priv->track_links && priv->select_info)
2162 for (l = priv->select_info->links; l; l = l->next)
2165 if (strcmp (uri, link->uri) == 0)
2167 visited = link->visited;
2174 color = pdata->visited_link_color;
2176 color = pdata->link_color;
2178 g_string_append_printf (pdata->new_str,
2179 "<span color=\"#%04x%04x%04x\" underline=\"single\">",
2184 link = g_new0 (GtkLabelLink, 1);
2185 link->uri = g_strdup (uri);
2186 link->title = g_strdup (title);
2187 link->visited = visited;
2188 pdata->links = g_list_append (pdata->links, link);
2194 g_string_append_c (pdata->new_str, '<');
2195 g_string_append (pdata->new_str, element_name);
2197 for (i = 0; attribute_names[i] != NULL; i++)
2199 const gchar *attr = attribute_names[i];
2200 const gchar *value = attribute_values[i];
2203 newvalue = g_markup_escape_text (value, -1);
2205 g_string_append_c (pdata->new_str, ' ');
2206 g_string_append (pdata->new_str, attr);
2207 g_string_append (pdata->new_str, "=\"");
2208 g_string_append (pdata->new_str, newvalue);
2209 g_string_append_c (pdata->new_str, '\"');
2213 g_string_append_c (pdata->new_str, '>');
2218 end_element_handler (GMarkupParseContext *context,
2219 const gchar *element_name,
2223 UriParserData *pdata = user_data;
2225 if (!strcmp (element_name, "a"))
2226 g_string_append (pdata->new_str, "</span>");
2229 g_string_append (pdata->new_str, "</");
2230 g_string_append (pdata->new_str, element_name);
2231 g_string_append_c (pdata->new_str, '>');
2236 text_handler (GMarkupParseContext *context,
2242 UriParserData *pdata = user_data;
2245 newtext = g_markup_escape_text (text, text_len);
2246 g_string_append (pdata->new_str, newtext);
2250 static const GMarkupParser markup_parser =
2252 start_element_handler,
2253 end_element_handler,
2260 xml_isspace (gchar c)
2262 return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
2266 link_free (GtkLabelLink *link)
2269 g_free (link->title);
2274 gtk_label_get_link_colors (GtkWidget *widget,
2275 GdkColor **link_color,
2276 GdkColor **visited_link_color)
2278 GtkStyleContext *context;
2280 context = gtk_widget_get_style_context (widget);
2281 gtk_style_context_get_style (context,
2282 "link-color", link_color,
2283 "visited-link-color", visited_link_color,
2286 *link_color = gdk_color_copy (&default_link_color);
2287 if (!*visited_link_color)
2288 *visited_link_color = gdk_color_copy (&default_visited_link_color);
2292 parse_uri_markup (GtkLabel *label,
2298 GMarkupParseContext *context = NULL;
2299 const gchar *p, *end;
2300 gboolean needs_root = TRUE;
2302 UriParserData pdata;
2304 length = strlen (str);
2308 pdata.label = label;
2310 pdata.new_str = g_string_sized_new (length);
2312 gtk_label_get_link_colors (GTK_WIDGET (label), &pdata.link_color, &pdata.visited_link_color);
2314 while (p != end && xml_isspace (*p))
2317 if (end - p >= 8 && strncmp (p, "<markup>", 8) == 0)
2320 context = g_markup_parse_context_new (&markup_parser, 0, &pdata, NULL);
2324 if (!g_markup_parse_context_parse (context, "<markup>", -1, error))
2328 if (!g_markup_parse_context_parse (context, str, length, error))
2333 if (!g_markup_parse_context_parse (context, "</markup>", -1, error))
2337 if (!g_markup_parse_context_end_parse (context, error))
2340 g_markup_parse_context_free (context);
2342 *new_str = g_string_free (pdata.new_str, FALSE);
2343 *links = pdata.links;
2345 gdk_color_free (pdata.link_color);
2346 gdk_color_free (pdata.visited_link_color);
2351 g_markup_parse_context_free (context);
2352 g_string_free (pdata.new_str, TRUE);
2353 g_list_foreach (pdata.links, (GFunc)link_free, NULL);
2354 g_list_free (pdata.links);
2355 gdk_color_free (pdata.link_color);
2356 gdk_color_free (pdata.visited_link_color);
2362 gtk_label_ensure_has_tooltip (GtkLabel *label)
2364 GtkLabelPrivate *priv = label->priv;
2366 gboolean has_tooltip = FALSE;
2368 for (l = priv->select_info->links; l; l = l->next)
2370 GtkLabelLink *link = l->data;
2378 gtk_widget_set_has_tooltip (GTK_WIDGET (label), has_tooltip);
2382 gtk_label_set_markup_internal (GtkLabel *label,
2384 gboolean with_uline)
2386 GtkLabelPrivate *priv = label->priv;
2388 GError *error = NULL;
2389 PangoAttrList *attrs = NULL;
2390 gunichar accel_char = 0;
2392 GList *links = NULL;
2394 if (!parse_uri_markup (label, str, &new_str, &links, &error))
2396 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2398 g_error_free (error);
2402 gtk_label_clear_links (label);
2405 gtk_label_ensure_select_info (label);
2406 priv->select_info->links = links;
2407 gtk_label_ensure_has_tooltip (label);
2412 gboolean enable_mnemonics;
2413 gboolean auto_mnemonics;
2415 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2416 "gtk-enable-mnemonics", &enable_mnemonics,
2417 "gtk-auto-mnemonics", &auto_mnemonics,
2420 if (!(enable_mnemonics && priv->mnemonics_visible &&
2422 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2423 (!priv->mnemonic_widget ||
2424 gtk_widget_is_sensitive (priv->mnemonic_widget))))))
2430 if (separate_uline_pattern (new_str, &key, &tmp, &pattern))
2439 if (!pango_parse_markup (new_str,
2441 with_uline ? '_' : 0,
2444 with_uline ? &accel_char : NULL,
2447 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2450 g_error_free (error);
2457 gtk_label_set_text_internal (label, text);
2461 if (priv->effective_attrs)
2462 pango_attr_list_unref (priv->effective_attrs);
2463 priv->effective_attrs = attrs;
2466 if (accel_char != 0)
2467 priv->mnemonic_keyval = gdk_keyval_to_lower (gdk_unicode_to_keyval (accel_char));
2469 priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
2473 * gtk_label_set_markup:
2474 * @label: a #GtkLabel
2475 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
2477 * Parses @str which is marked up with the <link
2478 * linkend="PangoMarkupFormat">Pango text markup language</link>, setting the
2479 * label's text and attribute list based on the parse results. If the @str is
2480 * external data, you may need to escape it with g_markup_escape_text() or
2481 * g_markup_printf_escaped()<!-- -->:
2485 * markup = g_markup_printf_escaped ("<span style=\"italic\">%s</span>", str);
2486 * gtk_label_set_markup (GTK_LABEL (label), markup);
2491 gtk_label_set_markup (GtkLabel *label,
2494 g_return_if_fail (GTK_IS_LABEL (label));
2496 g_object_freeze_notify (G_OBJECT (label));
2498 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2499 gtk_label_set_use_markup_internal (label, TRUE);
2500 gtk_label_set_use_underline_internal (label, FALSE);
2502 gtk_label_recalculate (label);
2504 g_object_thaw_notify (G_OBJECT (label));
2508 * gtk_label_set_markup_with_mnemonic:
2509 * @label: a #GtkLabel
2510 * @str: a markup string (see
2511 * <link linkend="PangoMarkupFormat">Pango markup format</link>)
2513 * Parses @str which is marked up with the
2514 * <link linkend="PangoMarkupFormat">Pango text markup language</link>,
2515 * setting the label's text and attribute list based on the parse results.
2516 * If characters in @str are preceded by an underscore, they are underlined
2517 * indicating that they represent a keyboard accelerator called a mnemonic.
2519 * The mnemonic key can be used to activate another widget, chosen
2520 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
2523 gtk_label_set_markup_with_mnemonic (GtkLabel *label,
2526 g_return_if_fail (GTK_IS_LABEL (label));
2528 g_object_freeze_notify (G_OBJECT (label));
2530 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2531 gtk_label_set_use_markup_internal (label, TRUE);
2532 gtk_label_set_use_underline_internal (label, TRUE);
2534 gtk_label_recalculate (label);
2536 g_object_thaw_notify (G_OBJECT (label));
2540 * gtk_label_get_text:
2541 * @label: a #GtkLabel
2543 * Fetches the text from a label widget, as displayed on the
2544 * screen. This does not include any embedded underlines
2545 * indicating mnemonics or Pango markup. (See gtk_label_get_label())
2547 * Return value: the text in the label widget. This is the internal
2548 * string used by the label, and must not be modified.
2550 G_CONST_RETURN gchar *
2551 gtk_label_get_text (GtkLabel *label)
2553 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2555 return label->priv->text;
2558 static PangoAttrList *
2559 gtk_label_pattern_to_attrs (GtkLabel *label,
2560 const gchar *pattern)
2562 GtkLabelPrivate *priv = label->priv;
2564 const char *p = priv->text;
2565 const char *q = pattern;
2566 PangoAttrList *attrs;
2568 attrs = pango_attr_list_new ();
2572 while (*p && *q && *q != '_')
2574 p = g_utf8_next_char (p);
2578 while (*p && *q && *q == '_')
2580 p = g_utf8_next_char (p);
2586 PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_LOW);
2587 attr->start_index = start - priv->text;
2588 attr->end_index = p - priv->text;
2590 pango_attr_list_insert (attrs, attr);
2600 gtk_label_set_pattern_internal (GtkLabel *label,
2601 const gchar *pattern,
2602 gboolean is_mnemonic)
2604 GtkLabelPrivate *priv = label->priv;
2605 PangoAttrList *attrs;
2606 gboolean enable_mnemonics;
2607 gboolean auto_mnemonics;
2609 if (priv->pattern_set)
2614 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2615 "gtk-enable-mnemonics", &enable_mnemonics,
2616 "gtk-auto-mnemonics", &auto_mnemonics,
2619 if (enable_mnemonics && priv->mnemonics_visible && pattern &&
2621 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2622 (!priv->mnemonic_widget ||
2623 gtk_widget_is_sensitive (priv->mnemonic_widget)))))
2624 attrs = gtk_label_pattern_to_attrs (label, pattern);
2629 attrs = gtk_label_pattern_to_attrs (label, pattern);
2631 if (priv->effective_attrs)
2632 pango_attr_list_unref (priv->effective_attrs);
2633 priv->effective_attrs = attrs;
2637 gtk_label_set_pattern (GtkLabel *label,
2638 const gchar *pattern)
2640 GtkLabelPrivate *priv = label->priv;
2642 g_return_if_fail (GTK_IS_LABEL (label));
2646 priv->pattern_set = FALSE;
2650 gtk_label_set_pattern_internal (label, pattern, FALSE);
2651 priv->pattern_set = TRUE;
2654 gtk_label_recalculate (label);
2656 gtk_label_clear_layout (label);
2657 gtk_widget_queue_resize (GTK_WIDGET (label));
2662 * gtk_label_set_justify:
2663 * @label: a #GtkLabel
2664 * @jtype: a #GtkJustification
2666 * Sets the alignment of the lines in the text of the label relative to
2667 * each other. %GTK_JUSTIFY_LEFT is the default value when the
2668 * widget is first created with gtk_label_new(). If you instead want
2669 * to set the alignment of the label as a whole, use
2670 * gtk_misc_set_alignment() instead. gtk_label_set_justify() has no
2671 * effect on labels containing only a single line.
2674 gtk_label_set_justify (GtkLabel *label,
2675 GtkJustification jtype)
2677 GtkLabelPrivate *priv;
2679 g_return_if_fail (GTK_IS_LABEL (label));
2680 g_return_if_fail (jtype >= GTK_JUSTIFY_LEFT && jtype <= GTK_JUSTIFY_FILL);
2684 if ((GtkJustification) priv->jtype != jtype)
2686 priv->jtype = jtype;
2688 /* No real need to be this drastic, but easier than duplicating the code */
2689 gtk_label_clear_layout (label);
2691 g_object_notify (G_OBJECT (label), "justify");
2692 gtk_widget_queue_resize (GTK_WIDGET (label));
2697 * gtk_label_get_justify:
2698 * @label: a #GtkLabel
2700 * Returns the justification of the label. See gtk_label_set_justify().
2702 * Return value: #GtkJustification
2705 gtk_label_get_justify (GtkLabel *label)
2707 g_return_val_if_fail (GTK_IS_LABEL (label), 0);
2709 return label->priv->jtype;
2713 * gtk_label_set_ellipsize:
2714 * @label: a #GtkLabel
2715 * @mode: a #PangoEllipsizeMode
2717 * Sets the mode used to ellipsize (add an ellipsis: "...") to the text
2718 * if there is not enough space to render the entire string.
2723 gtk_label_set_ellipsize (GtkLabel *label,
2724 PangoEllipsizeMode mode)
2726 GtkLabelPrivate *priv;
2728 g_return_if_fail (GTK_IS_LABEL (label));
2729 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE && mode <= PANGO_ELLIPSIZE_END);
2733 if ((PangoEllipsizeMode) priv->ellipsize != mode)
2735 priv->ellipsize = mode;
2737 /* No real need to be this drastic, but easier than duplicating the code */
2738 gtk_label_clear_layout (label);
2740 g_object_notify (G_OBJECT (label), "ellipsize");
2741 gtk_widget_queue_resize (GTK_WIDGET (label));
2746 * gtk_label_get_ellipsize:
2747 * @label: a #GtkLabel
2749 * Returns the ellipsizing position of the label. See gtk_label_set_ellipsize().
2751 * Return value: #PangoEllipsizeMode
2756 gtk_label_get_ellipsize (GtkLabel *label)
2758 g_return_val_if_fail (GTK_IS_LABEL (label), PANGO_ELLIPSIZE_NONE);
2760 return label->priv->ellipsize;
2764 * gtk_label_set_width_chars:
2765 * @label: a #GtkLabel
2766 * @n_chars: the new desired width, in characters.
2768 * Sets the desired width in characters of @label to @n_chars.
2773 gtk_label_set_width_chars (GtkLabel *label,
2776 GtkLabelPrivate *priv;
2778 g_return_if_fail (GTK_IS_LABEL (label));
2782 if (priv->width_chars != n_chars)
2784 priv->width_chars = n_chars;
2785 g_object_notify (G_OBJECT (label), "width-chars");
2786 gtk_label_invalidate_wrap_width (label);
2787 gtk_widget_queue_resize (GTK_WIDGET (label));
2792 * gtk_label_get_width_chars:
2793 * @label: a #GtkLabel
2795 * Retrieves the desired width of @label, in characters. See
2796 * gtk_label_set_width_chars().
2798 * Return value: the width of the label in characters.
2803 gtk_label_get_width_chars (GtkLabel *label)
2805 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2807 return label->priv->width_chars;
2811 * gtk_label_set_max_width_chars:
2812 * @label: a #GtkLabel
2813 * @n_chars: the new desired maximum width, in characters.
2815 * Sets the desired maximum width in characters of @label to @n_chars.
2820 gtk_label_set_max_width_chars (GtkLabel *label,
2823 GtkLabelPrivate *priv;
2825 g_return_if_fail (GTK_IS_LABEL (label));
2829 if (priv->max_width_chars != n_chars)
2831 priv->max_width_chars = n_chars;
2833 g_object_notify (G_OBJECT (label), "max-width-chars");
2834 gtk_label_invalidate_wrap_width (label);
2835 gtk_widget_queue_resize (GTK_WIDGET (label));
2840 * gtk_label_get_max_width_chars:
2841 * @label: a #GtkLabel
2843 * Retrieves the desired maximum width of @label, in characters. See
2844 * gtk_label_set_width_chars().
2846 * Return value: the maximum width of the label in characters.
2851 gtk_label_get_max_width_chars (GtkLabel *label)
2853 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2855 return label->priv->max_width_chars;
2859 * gtk_label_set_line_wrap:
2860 * @label: a #GtkLabel
2861 * @wrap: the setting
2863 * Toggles line wrapping within the #GtkLabel widget. %TRUE makes it break
2864 * lines if text exceeds the widget's size. %FALSE lets the text get cut off
2865 * by the edge of the widget if it exceeds the widget size.
2867 * Note that setting line wrapping to %TRUE does not make the label
2868 * wrap at its parent container's width, because GTK+ widgets
2869 * conceptually can't make their requisition depend on the parent
2870 * container's size. For a label that wraps at a specific position,
2871 * set the label's width using gtk_widget_set_size_request().
2874 gtk_label_set_line_wrap (GtkLabel *label,
2877 GtkLabelPrivate *priv;
2879 g_return_if_fail (GTK_IS_LABEL (label));
2883 wrap = wrap != FALSE;
2885 if (priv->wrap != wrap)
2889 gtk_label_clear_layout (label);
2890 gtk_widget_queue_resize (GTK_WIDGET (label));
2891 g_object_notify (G_OBJECT (label), "wrap");
2896 * gtk_label_get_line_wrap:
2897 * @label: a #GtkLabel
2899 * Returns whether lines in the label are automatically wrapped.
2900 * See gtk_label_set_line_wrap().
2902 * Return value: %TRUE if the lines of the label are automatically wrapped.
2905 gtk_label_get_line_wrap (GtkLabel *label)
2907 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
2909 return label->priv->wrap;
2913 * gtk_label_set_line_wrap_mode:
2914 * @label: a #GtkLabel
2915 * @wrap_mode: the line wrapping mode
2917 * If line wrapping is on (see gtk_label_set_line_wrap()) this controls how
2918 * the line wrapping is done. The default is %PANGO_WRAP_WORD which means
2919 * wrap on word boundaries.
2924 gtk_label_set_line_wrap_mode (GtkLabel *label,
2925 PangoWrapMode wrap_mode)
2927 GtkLabelPrivate *priv;
2929 g_return_if_fail (GTK_IS_LABEL (label));
2933 if (priv->wrap_mode != wrap_mode)
2935 priv->wrap_mode = wrap_mode;
2936 g_object_notify (G_OBJECT (label), "wrap-mode");
2938 gtk_widget_queue_resize (GTK_WIDGET (label));
2943 * gtk_label_get_line_wrap_mode:
2944 * @label: a #GtkLabel
2946 * Returns line wrap mode used by the label. See gtk_label_set_line_wrap_mode().
2948 * Return value: %TRUE if the lines of the label are automatically wrapped.
2953 gtk_label_get_line_wrap_mode (GtkLabel *label)
2955 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
2957 return label->priv->wrap_mode;
2961 gtk_label_destroy (GtkWidget *widget)
2963 GtkLabel *label = GTK_LABEL (widget);
2965 gtk_label_set_mnemonic_widget (label, NULL);
2967 GTK_WIDGET_CLASS (gtk_label_parent_class)->destroy (widget);
2971 gtk_label_finalize (GObject *object)
2973 GtkLabel *label = GTK_LABEL (object);
2974 GtkLabelPrivate *priv = label->priv;
2976 g_free (priv->label);
2977 g_free (priv->text);
2980 g_object_unref (priv->layout);
2983 pango_attr_list_unref (priv->attrs);
2985 if (priv->effective_attrs)
2986 pango_attr_list_unref (priv->effective_attrs);
2988 gtk_label_clear_links (label);
2989 g_free (priv->select_info);
2991 G_OBJECT_CLASS (gtk_label_parent_class)->finalize (object);
2995 gtk_label_clear_layout (GtkLabel *label)
2997 GtkLabelPrivate *priv = label->priv;
3001 g_object_unref (priv->layout);
3002 priv->layout = NULL;
3004 //gtk_label_clear_links (label);
3008 static PangoFontMetrics *
3009 get_font_metrics (PangoContext *context, GtkWidget *widget)
3011 GtkStyleContext *style_context;
3012 PangoFontDescription *font;
3013 PangoFontMetrics *retval;
3015 style_context = gtk_widget_get_style_context (widget);
3016 gtk_style_context_get (style_context, 0, "font", &font, NULL);
3018 retval = pango_context_get_metrics (context,
3020 pango_context_get_language (context));
3023 pango_font_description_free (font);
3029 * gtk_label_get_measuring_layout:
3031 * @existing_layout: %NULL or an existing layout already in use.
3032 * @width: the width to measure with in pango units, or -1 for infinite
3034 * Gets a layout that can be used for measuring sizes. The returned
3035 * layout will be identical to the label's layout except for the
3036 * layout's width, which will be set to @width. Do not modify the returned
3039 * Returns: a new reference to a pango layout
3041 static PangoLayout *
3042 gtk_label_get_measuring_layout (GtkLabel * label,
3043 PangoLayout *existing_layout,
3046 GtkLabelPrivate *priv = label->priv;
3049 if (existing_layout != NULL)
3051 if (existing_layout != priv->layout)
3053 pango_layout_set_width (existing_layout, width);
3054 return existing_layout;
3057 g_object_unref (existing_layout);
3060 gtk_label_ensure_layout (label, FALSE);
3062 if (pango_layout_get_width (priv->layout) == width)
3064 g_object_ref (priv->layout);
3065 return priv->layout;
3068 copy = pango_layout_copy (priv->layout);
3069 pango_layout_set_width (copy, width);
3074 get_label_width (GtkLabel *label,
3075 PangoLayout *guess_layout,
3079 GtkLabelPrivate *priv;
3080 PangoLayout *layout;
3081 PangoContext *context;
3082 PangoFontMetrics *metrics;
3083 PangoRectangle rect;
3084 gint char_width, digit_width, char_pixels, text_width, ellipsize_chars, guess_width;
3088 layout = gtk_label_get_measuring_layout (label, NULL, -1);
3089 context = pango_layout_get_context (layout);
3090 metrics = get_font_metrics (context, GTK_WIDGET (label));
3091 char_width = pango_font_metrics_get_approximate_char_width (metrics);
3092 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
3093 char_pixels = MAX (char_width, digit_width);
3094 pango_font_metrics_unref (metrics);
3096 /* Fetch the length of the complete unwrapped text */
3097 pango_layout_get_extents (layout, NULL, &rect);
3098 text_width = rect.width;
3100 /* Fetch the width that was guessed */
3101 pango_layout_get_extents (guess_layout, NULL, &rect);
3102 guess_width = rect.width;
3104 /* enforce minimum width for ellipsized labels at ~3 chars */
3105 if (priv->ellipsize)
3106 ellipsize_chars = 3;
3108 ellipsize_chars = 0;
3110 /* "width-chars" Hard-coded minimum width:
3111 * - minimum size should be MAX (width-chars, strlen ("..."));
3112 * - natural size should be MAX (width-chars, strlen (priv->text));
3114 * "max-width-chars" User specified maximum size requisition
3115 * - minimum size should be MAX (width-chars, 0)
3116 * - natural size should be MIN (max-width-chars, strlen (priv->text))
3118 * For ellipsizing labels; if max-width-chars is specified: either it is used as
3119 * a minimum size or the label text as a minimum size (natural size still overflows).
3121 * For wrapping labels; A reasonable minimum size is useful to naturally layout
3122 * interfaces automatically. In this case if no "width-chars" is specified, the minimum
3123 * width will default to the wrap guess that gtk_label_ensure_layout() does.
3125 * In *any* case the minimum width is completely overridden if an explicit width
3126 * request was provided.
3129 if (priv->ellipsize || priv->wrap)
3131 *minimum = char_pixels * MAX (priv->width_chars, ellipsize_chars);
3133 /* Default to the minimum width regularly guessed by GTK+ if no minimum
3134 * width was specified, only allow unwrapping of these labels.
3136 * Note that when specifying a small width_chars for a long text;
3137 * an accordingly large size will be required for the label height.
3139 if (priv->wrap && priv->width_chars <= 0)
3140 *minimum = guess_width;
3142 if (priv->max_width_chars < 0)
3144 *natural = MAX (*minimum, text_width);
3148 gint max_char_width = char_pixels * priv->max_width_chars;
3149 gint max_width = MIN (text_width, max_char_width);
3151 /* With max-char-width specified, we let the minimum widths of
3152 * ellipsized text crawl up to the max-char-width
3153 * (note that we dont want to limit the minimum width for wrapping text).
3155 if (priv->ellipsize)
3156 *minimum = MIN (text_width, max_width);
3158 *natural = MAX (*minimum, max_width);
3163 *minimum = text_width;
3164 *natural = *minimum;
3167 /* if a width-request is set, use that as the requested label width */
3168 if (priv->wrap || priv->ellipsize || priv->width_chars > 0 || priv->max_width_chars > 0)
3170 GtkWidgetAuxInfo *aux_info;
3172 aux_info = _gtk_widget_get_aux_info (GTK_WIDGET (label), FALSE);
3173 if (aux_info && aux_info->width > 0)
3175 *minimum = aux_info->width * PANGO_SCALE;
3176 *natural = MAX (*natural, *minimum);
3180 g_object_unref (layout);
3184 gtk_label_invalidate_wrap_width (GtkLabel *label)
3186 GtkLabelPrivate *priv = label->priv;
3188 priv->wrap_width = -1;
3192 get_label_wrap_width (GtkLabel *label)
3194 GtkLabelPrivate *priv = label->priv;
3196 if (priv->wrap_width < 0)
3198 if (priv->width_chars > 0)
3200 PangoLayout *layout;
3201 PangoContext *context;
3202 PangoFontMetrics *metrics;
3203 PangoRectangle rect;
3204 gint char_width, digit_width, char_pixels, text_width;
3206 layout = gtk_label_get_measuring_layout (label, NULL, -1);
3207 context = pango_layout_get_context (layout);
3208 metrics = get_font_metrics (context, GTK_WIDGET (label));
3209 char_width = pango_font_metrics_get_approximate_char_width (metrics);
3210 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
3211 char_pixels = MAX (char_width, digit_width);
3212 pango_font_metrics_unref (metrics);
3214 pango_layout_get_extents (layout, NULL, &rect);
3215 g_object_unref (layout);
3217 text_width = rect.width;
3219 priv->wrap_width = PANGO_PIXELS (MAX (text_width, char_pixels * priv->width_chars));
3223 PangoLayout *layout;
3225 layout = gtk_widget_create_pango_layout (GTK_WIDGET (label),
3226 "This string is just about long enough.");
3227 pango_layout_get_size (layout, &priv->wrap_width, NULL);
3228 g_object_unref (layout);
3232 return priv->wrap_width;
3235 static PangoLayout *
3236 gtk_label_get_layout_with_guessed_wrap_width (GtkLabel *label)
3239 PangoRectangle logical_rect;
3240 gint wrap_width, width, height, longest_paragraph;
3241 PangoLayout *layout;
3243 if (!label->priv->wrap)
3244 return gtk_label_get_measuring_layout (label, NULL, -1);
3246 screen = gtk_widget_get_screen (GTK_WIDGET (label));
3248 layout = gtk_label_get_measuring_layout (label, NULL, -1);
3249 pango_layout_get_extents (layout, NULL, &logical_rect);
3251 width = logical_rect.width;
3252 /* Try to guess a reasonable maximum width */
3253 longest_paragraph = width;
3255 wrap_width = get_label_wrap_width (label);
3256 width = MIN (width, wrap_width);
3258 PANGO_SCALE * (gdk_screen_get_width (screen) + 1) / 2);
3260 layout = gtk_label_get_measuring_layout (label, layout, width);
3262 pango_layout_get_extents (layout, NULL, &logical_rect);
3263 width = logical_rect.width;
3264 height = logical_rect.height;
3266 /* Unfortunately, the above may leave us with a very unbalanced looking paragraph,
3267 * so we try short search for a narrower width that leaves us with the same height
3269 if (longest_paragraph > 0)
3271 gint nlines, perfect_width;
3273 nlines = pango_layout_get_line_count (layout);
3274 perfect_width = (longest_paragraph + nlines - 1) / nlines;
3276 if (perfect_width < width)
3278 layout = gtk_label_get_measuring_layout (label, layout, perfect_width);
3279 pango_layout_get_extents (layout, NULL, &logical_rect);
3281 if (logical_rect.height <= height)
3282 width = logical_rect.width;
3285 gint mid_width = (perfect_width + width) / 2;
3287 if (mid_width > perfect_width)
3289 layout = gtk_label_get_measuring_layout (label, layout, mid_width);
3290 pango_layout_get_extents (layout, NULL, &logical_rect);
3292 if (logical_rect.height <= height)
3293 width = logical_rect.width;
3299 return gtk_label_get_measuring_layout (label, layout, width);
3303 gtk_label_ensure_layout (GtkLabel *label, gboolean guess_wrap_width)
3305 GtkLabelPrivate *priv = label->priv;
3306 PangoRectangle logical_rect;
3310 widget = GTK_WIDGET (label);
3312 rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
3316 GtkAllocation allocation;
3317 PangoAlignment align = PANGO_ALIGN_LEFT; /* Quiet gcc */
3318 gdouble angle = gtk_label_get_angle (label);
3320 if (angle != 0.0 && !priv->select_info)
3322 PangoMatrix matrix = PANGO_MATRIX_INIT;
3324 /* We rotate the standard singleton PangoContext for the widget,
3325 * depending on the fact that it's meant pretty much exclusively
3328 pango_matrix_rotate (&matrix, angle);
3330 pango_context_set_matrix (gtk_widget_get_pango_context (widget), &matrix);
3332 priv->have_transform = TRUE;
3336 if (priv->have_transform)
3337 pango_context_set_matrix (gtk_widget_get_pango_context (widget), NULL);
3339 priv->have_transform = FALSE;
3342 priv->layout = gtk_widget_create_pango_layout (widget, priv->text);
3344 if (priv->effective_attrs)
3345 pango_layout_set_attributes (priv->layout, priv->effective_attrs);
3347 gtk_label_rescan_links (label);
3349 switch (priv->jtype)
3351 case GTK_JUSTIFY_LEFT:
3352 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3354 case GTK_JUSTIFY_RIGHT:
3355 align = rtl ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
3357 case GTK_JUSTIFY_CENTER:
3358 align = PANGO_ALIGN_CENTER;
3360 case GTK_JUSTIFY_FILL:
3361 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3362 pango_layout_set_justify (priv->layout, TRUE);
3365 g_assert_not_reached();
3368 pango_layout_set_alignment (priv->layout, align);
3369 pango_layout_set_ellipsize (priv->layout, priv->ellipsize);
3370 pango_layout_set_single_paragraph_mode (priv->layout, priv->single_line_mode);
3372 gtk_widget_get_allocation (widget, &allocation);
3374 if (priv->ellipsize)
3375 pango_layout_set_width (priv->layout, allocation.width * PANGO_SCALE);
3376 else if (priv->wrap)
3378 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (widget, FALSE);
3379 gint longest_paragraph;
3383 if ((angle == 90 || angle == 270) && aux_info && aux_info->height > 0)
3384 aux_width = aux_info->height;
3385 else if (aux_info && aux_info->width > 0)
3386 aux_width = aux_info->width;
3389 pango_layout_set_width (priv->layout, aux_width * PANGO_SCALE);
3390 else if (guess_wrap_width == FALSE && allocation.width > 1 && allocation.height > 1)
3392 PangoRectangle rect;
3393 gint xpad, ypad, natural_width;
3394 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3396 if (angle == 90 || angle == 270)
3397 width = allocation.height - ypad * 2;
3399 width = allocation.width - xpad * 2;
3401 /* dont set a wrap width wider than the label's natural width
3402 * incase we're allocated more space than needed */
3403 pango_layout_get_extents (priv->layout, NULL, &rect);
3404 natural_width = PANGO_PIXELS (rect.width);
3405 width = MIN (natural_width, width);
3407 pango_layout_set_wrap (priv->layout, priv->wrap_mode);
3408 pango_layout_set_width (priv->layout, MAX (width, 1) * PANGO_SCALE);
3412 GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (label));
3415 pango_layout_set_width (priv->layout, -1);
3416 pango_layout_get_extents (priv->layout, NULL, &logical_rect);
3418 width = logical_rect.width;
3419 /* Try to guess a reasonable maximum width */
3420 longest_paragraph = width;
3422 wrap_width = get_label_wrap_width (label);
3423 width = MIN (width, wrap_width);
3425 PANGO_SCALE * (gdk_screen_get_width (screen) + 1) / 2);
3427 pango_layout_set_width (priv->layout, width);
3428 pango_layout_get_extents (priv->layout, NULL, &logical_rect);
3429 width = logical_rect.width;
3430 height = logical_rect.height;
3432 /* Unfortunately, the above may leave us with a very unbalanced looking paragraph,
3433 * so we try short search for a narrower width that leaves us with the same height
3435 if (longest_paragraph > 0)
3437 gint nlines, perfect_width;
3439 nlines = pango_layout_get_line_count (priv->layout);
3440 perfect_width = (longest_paragraph + nlines - 1) / nlines;
3442 if (perfect_width < width)
3444 pango_layout_set_width (priv->layout, perfect_width);
3445 pango_layout_get_extents (priv->layout, NULL, &logical_rect);
3447 if (logical_rect.height <= height)
3448 width = logical_rect.width;
3451 gint mid_width = (perfect_width + width) / 2;
3453 if (mid_width > perfect_width)
3455 pango_layout_set_width (priv->layout, mid_width);
3456 pango_layout_get_extents (priv->layout, NULL, &logical_rect);
3458 if (logical_rect.height <= height)
3459 width = logical_rect.width;
3464 pango_layout_set_width (priv->layout, width);
3467 else /* !priv->wrap */
3468 pango_layout_set_width (priv->layout, -1);
3473 get_single_line_height (GtkWidget *widget,
3474 PangoLayout *layout)
3476 PangoContext *context;
3477 PangoFontMetrics *metrics;
3478 gint ascent, descent;
3480 context = pango_layout_get_context (layout);
3481 metrics = get_font_metrics (context, widget);
3482 ascent = pango_font_metrics_get_ascent (metrics);
3483 descent = pango_font_metrics_get_descent (metrics);
3484 pango_font_metrics_unref (metrics);
3486 return ascent + descent;
3489 static GtkSizeRequestMode
3490 gtk_label_get_request_mode (GtkWidget *widget)
3492 GtkLabel *label = GTK_LABEL (widget);
3493 gdouble angle = gtk_label_get_angle (label);
3495 if (label->priv->wrap)
3496 return (angle == 90 || angle == 270) ?
3497 GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT :
3498 GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
3500 return GTK_SIZE_REQUEST_CONSTANT_SIZE;
3504 get_size_for_allocation (GtkLabel *label,
3505 GtkOrientation orientation,
3510 PangoLayout *layout;
3511 GtkWidgetAuxInfo *aux_info =
3512 _gtk_widget_get_aux_info (GTK_WIDGET (label), FALSE);
3518 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3519 aux_size = aux_info->width;
3521 aux_size = aux_info->height;
3527 layout = gtk_label_get_measuring_layout (label, NULL, aux_size * PANGO_SCALE);
3529 layout = gtk_label_get_measuring_layout (label, NULL, allocation * PANGO_SCALE);
3531 pango_layout_get_pixel_size (layout, NULL, &text_height);
3534 *minimum_size = text_height;
3537 *natural_size = text_height;
3539 g_object_unref (layout);
3543 gtk_label_get_preferred_size (GtkWidget *widget,
3544 GtkOrientation orientation,
3548 GtkLabel *label = GTK_LABEL (widget);
3549 GtkLabelPrivate *priv = label->priv;
3550 PangoRectangle required_rect;
3551 PangoRectangle natural_rect;
3553 PangoLayout *layout;
3555 /* "width-chars" Hard-coded minimum width:
3556 * - minimum size should be MAX (width-chars, strlen ("..."));
3557 * - natural size should be MAX (width-chars, strlen (priv->text));
3559 * "max-width-chars" User specified maximum size requisition
3560 * - minimum size should be MAX (width-chars, 0)
3561 * - natural size should be MIN (max-width-chars, strlen (priv->text))
3565 /* When calculating ->wrap sometimes we need to invent a size; Ideally we should be doing
3566 * that stuff here instead of inside gtk_label_ensure_layout() */
3567 layout = gtk_label_get_layout_with_guessed_wrap_width (label);
3569 /* Start off with the pixel extents of the rendered layout */
3570 pango_layout_get_extents (layout, NULL, &required_rect);
3571 required_rect.x = required_rect.y = 0;
3573 if (priv->single_line_mode || priv->wrap)
3574 required_rect.height = get_single_line_height (GTK_WIDGET (label), layout);
3576 natural_rect = required_rect;
3578 /* Calculate text width itself based on GtkLabel property rules */
3579 get_label_width (label, layout, &required_rect.width, &natural_rect.width);
3581 /* Now that we have minimum and natural sizes in pango extents, apply a possible transform */
3582 if (priv->have_transform)
3585 PangoContext *context;
3586 const PangoMatrix *matrix;
3588 copy = pango_layout_copy (priv->layout);
3589 context = pango_layout_get_context (copy);
3590 matrix = pango_context_get_matrix (context);
3592 pango_layout_set_width (copy, -1);
3593 pango_layout_set_ellipsize (copy, PANGO_ELLIPSIZE_NONE);
3595 pango_layout_get_extents (copy, NULL, &natural_rect);
3596 g_object_unref (copy);
3598 pango_matrix_transform_rectangle (matrix, &required_rect);
3599 pango_matrix_transform_rectangle (matrix, &natural_rect);
3601 /* Bump the natural size in case of ellipsize to ensure pango has
3602 * enough space in the angles (note, we could alternatively set the
3603 * layout to not ellipsize when we know we have been allocated our
3604 * full natural size, or it may be that pango needs a fix here).
3606 if (priv->ellipsize && priv->angle != 0 && priv->angle != 90 &&
3607 priv->angle != 180 && priv->angle != 270 && priv->angle != 360)
3609 /* For some reason we only need this at about 110 degrees, and only
3610 * when gaining in height
3612 natural_rect.height += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3613 natural_rect.width += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3617 required_rect.width = PANGO_PIXELS_CEIL (required_rect.width);
3618 required_rect.height = PANGO_PIXELS_CEIL (required_rect.height);
3620 natural_rect.width = PANGO_PIXELS_CEIL (natural_rect.width);
3621 natural_rect.height = PANGO_PIXELS_CEIL (natural_rect.height);
3623 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3625 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3627 /* Note, we cant use get_size_for_allocation() when rotating
3628 * ellipsized labels.
3630 if (!(priv->ellipsize && priv->have_transform) &&
3631 (priv->angle == 90 || priv->angle == 270))
3633 /* Doing a h4w request on a rotated label here, return the
3634 * required width for the minimum height.
3636 get_size_for_allocation (label,
3637 GTK_ORIENTATION_VERTICAL,
3638 required_rect.height,
3639 minimum_size, natural_size);
3644 /* Normal desired width */
3645 *minimum_size = required_rect.width;
3646 *natural_size = natural_rect.width;
3649 *minimum_size += xpad * 2;
3650 *natural_size += xpad * 2;
3652 else /* GTK_ORIENTATION_VERTICAL */
3654 /* Note, we cant use get_size_for_allocation() when rotating
3655 * ellipsized labels.
3657 if (!(priv->ellipsize && priv->have_transform) &&
3658 (priv->angle == 0 || priv->angle == 180 || priv->angle == 360))
3660 /* Doing a w4h request on a label here, return the required
3661 * height for the minimum width.
3663 get_size_for_allocation (label,
3664 GTK_ORIENTATION_HORIZONTAL,
3665 required_rect.width,
3666 minimum_size, natural_size);
3670 /* A vertically rotated label does w4h, so return the base
3671 * desired height (text length)
3673 *minimum_size = required_rect.height;
3674 *natural_size = natural_rect.height;
3677 *minimum_size += ypad * 2;
3678 *natural_size += ypad * 2;
3681 g_object_unref (layout);
3686 gtk_label_get_preferred_width (GtkWidget *widget,
3690 gtk_label_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
3694 gtk_label_get_preferred_height (GtkWidget *widget,
3698 gtk_label_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
3702 gtk_label_get_preferred_width_for_height (GtkWidget *widget,
3704 gint *minimum_width,
3705 gint *natural_width)
3707 GtkLabel *label = GTK_LABEL (widget);
3708 GtkLabelPrivate *priv = label->priv;
3710 if (priv->wrap && (priv->angle == 90 || priv->angle == 270))
3714 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3717 gtk_label_clear_layout (label);
3719 get_size_for_allocation (label, GTK_ORIENTATION_VERTICAL,
3720 MAX (1, height - (ypad * 2)),
3721 minimum_width, natural_width);
3724 *minimum_width += xpad * 2;
3727 *natural_width += xpad * 2;
3730 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_width, natural_width);
3734 gtk_label_get_preferred_height_for_width (GtkWidget *widget,
3736 gint *minimum_height,
3737 gint *natural_height)
3739 GtkLabel *label = GTK_LABEL (widget);
3740 GtkLabelPrivate *priv = label->priv;
3742 if (priv->wrap && (priv->angle == 0 || priv->angle == 180 || priv->angle == 360))
3746 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3749 gtk_label_clear_layout (label);
3751 get_size_for_allocation (label, GTK_ORIENTATION_HORIZONTAL,
3752 MAX (1, width - xpad * 2),
3753 minimum_height, natural_height);
3756 *minimum_height += ypad * 2;
3759 *natural_height += ypad * 2;
3762 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, minimum_height, natural_height);
3766 gtk_label_size_allocate (GtkWidget *widget,
3767 GtkAllocation *allocation)
3769 GtkLabel *label = GTK_LABEL (widget);
3770 GtkLabelPrivate *priv = label->priv;
3772 GTK_WIDGET_CLASS (gtk_label_parent_class)->size_allocate (widget, allocation);
3774 /* The layout may have been recently cleared in get_size_for_orientation(),
3775 * but the width at that point may not be the same as the allocated width
3778 gtk_label_clear_layout (label);
3780 gtk_label_ensure_layout (label, FALSE);
3782 if (priv->ellipsize)
3786 PangoRectangle logical;
3787 PangoRectangle bounds;
3790 gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3792 bounds.x = bounds.y = 0;
3793 bounds.width = allocation->width - xpad * 2;
3794 bounds.height = allocation->height - ypad * 2;
3796 pango_layout_set_width (priv->layout, -1);
3797 pango_layout_get_pixel_extents (priv->layout, NULL, &logical);
3799 if (priv->have_transform)
3801 PangoContext *context = gtk_widget_get_pango_context (widget);
3802 const PangoMatrix *matrix = pango_context_get_matrix (context);
3804 const gdouble dx = matrix->xx; /* cos (M_PI * angle / 180) */
3805 const gdouble dy = matrix->xy; /* sin (M_PI * angle / 180) */
3806 if (fabs (dy) < 0.01)
3808 if (logical.width > bounds.width)
3809 pango_layout_set_width (priv->layout, bounds.width * PANGO_SCALE);
3811 else if (fabs (dx) < 0.01)
3813 if (logical.width > bounds.height)
3814 pango_layout_set_width (priv->layout, bounds.height * PANGO_SCALE);
3818 gdouble x0, y0, x1, y1, length;
3822 x0 = bounds.width / 2;
3823 y0 = dx ? x0 * dy / dx : G_MAXDOUBLE;
3824 vertical = fabs (y0) > bounds.height / 2;
3828 y0 = bounds.height/2;
3829 x0 = dy ? y0 * dx / dy : G_MAXDOUBLE;
3832 length = 2 * sqrt (x0 * x0 + y0 * y0);
3833 pango_layout_set_width (priv->layout, rint (length * PANGO_SCALE));
3834 pango_layout_get_pixel_size (priv->layout, NULL, &cy);
3841 y0 = bounds.height/2 + y1 - y0;
3846 x0 = bounds.width/2 + x1 - x0;
3850 length = length - sqrt (x0 * x0 + y0 * y0) * 2;
3851 pango_layout_set_width (priv->layout, rint (length * PANGO_SCALE));
3854 else if (logical.width > bounds.width)
3855 pango_layout_set_width (priv->layout, bounds.width * PANGO_SCALE);
3859 if (priv->select_info && priv->select_info->window)
3861 gdk_window_move_resize (priv->select_info->window,
3865 allocation->height);
3870 gtk_label_update_cursor (GtkLabel *label)
3872 GtkLabelPrivate *priv = label->priv;
3875 if (!priv->select_info)
3878 widget = GTK_WIDGET (label);
3880 if (gtk_widget_get_realized (widget))
3882 GdkDisplay *display;
3885 if (gtk_widget_is_sensitive (widget))
3887 display = gtk_widget_get_display (widget);
3889 if (priv->select_info->active_link)
3890 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
3891 else if (priv->select_info->selectable)
3892 cursor = gdk_cursor_new_for_display (display, GDK_XTERM);
3899 gdk_window_set_cursor (priv->select_info->window, cursor);
3902 g_object_unref (cursor);
3907 gtk_label_state_changed (GtkWidget *widget,
3908 GtkStateType prev_state)
3910 GtkLabel *label = GTK_LABEL (widget);
3911 GtkLabelPrivate *priv = label->priv;
3913 if (priv->select_info)
3915 if (!gtk_widget_is_sensitive (widget))
3916 gtk_label_select_region (label, 0, 0);
3918 gtk_label_update_cursor (label);
3921 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed)
3922 GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed (widget, prev_state);
3926 gtk_label_style_updated (GtkWidget *widget)
3928 GtkLabel *label = GTK_LABEL (widget);
3930 GTK_WIDGET_CLASS (gtk_label_parent_class)->style_updated (widget);
3932 /* We have to clear the layout, fonts etc. may have changed */
3933 gtk_label_clear_layout (label);
3934 gtk_label_invalidate_wrap_width (label);
3938 gtk_label_direction_changed (GtkWidget *widget,
3939 GtkTextDirection previous_dir)
3941 GtkLabel *label = GTK_LABEL (widget);
3942 GtkLabelPrivate *priv = label->priv;
3945 pango_layout_context_changed (priv->layout);
3947 GTK_WIDGET_CLASS (gtk_label_parent_class)->direction_changed (widget, previous_dir);
3951 get_layout_location (GtkLabel *label,
3955 GtkAllocation allocation;
3958 GtkLabelPrivate *priv;
3959 gint req_width, x, y;
3962 gfloat xalign, yalign;
3963 PangoRectangle logical;
3965 misc = GTK_MISC (label);
3966 widget = GTK_WIDGET (label);
3969 gtk_misc_get_alignment (misc, &xalign, &yalign);
3970 gtk_misc_get_padding (misc, &xpad, &ypad);
3972 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
3973 xalign = 1.0 - xalign;
3975 pango_layout_get_extents (priv->layout, NULL, &logical);
3977 if (priv->have_transform)
3979 PangoContext *context = gtk_widget_get_pango_context (widget);
3980 const PangoMatrix *matrix = pango_context_get_matrix (context);
3981 pango_matrix_transform_rectangle (matrix, &logical);
3984 pango_extents_to_pixels (&logical, NULL);
3986 req_width = logical.width;
3987 req_height = logical.height;
3989 req_width += 2 * xpad;
3990 req_height += 2 * ypad;
3992 gtk_widget_get_allocation (widget, &allocation);
3994 x = floor (allocation.x + xpad + xalign * (allocation.width - req_width));
3996 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
3997 x = MAX (x, allocation.x + xpad);
3999 x = MIN (x, allocation.x + allocation.width - xpad);
4002 /* bgo#315462 - For single-line labels, *do* align the requisition with
4003 * respect to the allocation, even if we are under-allocated. For multi-line
4004 * labels, always show the top of the text when they are under-allocated. The
4005 * rationale is this:
4007 * - Single-line labels appear in GtkButtons, and it is very easy to get them
4008 * to be smaller than their requisition. The button may clip the label, but
4009 * the label will still be able to show most of itself and the focus
4010 * rectangle. Also, it is fairly easy to read a single line of clipped text.
4012 * - Multi-line labels should not be clipped to showing "something in the
4013 * middle". You want to read the first line, at least, to get some context.
4015 if (pango_layout_get_line_count (priv->layout) == 1)
4016 y = floor (allocation.y + ypad + (allocation.height - req_height) * yalign);
4018 y = floor (allocation.y + ypad + MAX ((allocation.height - req_height) * yalign, 0));
4028 draw_insertion_cursor (GtkLabel *label,
4030 GdkRectangle *cursor_location,
4031 gboolean is_primary,
4032 PangoDirection direction,
4033 gboolean draw_arrow)
4035 GtkWidget *widget = GTK_WIDGET (label);
4036 GtkTextDirection text_dir;
4038 if (direction == PANGO_DIRECTION_LTR)
4039 text_dir = GTK_TEXT_DIR_LTR;
4041 text_dir = GTK_TEXT_DIR_RTL;
4043 gtk_draw_insertion_cursor (widget, cr, cursor_location,
4044 is_primary, text_dir, draw_arrow);
4047 static PangoDirection
4048 get_cursor_direction (GtkLabel *label)
4050 GtkLabelPrivate *priv = label->priv;
4053 g_assert (priv->select_info);
4055 gtk_label_ensure_layout (label, FALSE);
4057 for (l = pango_layout_get_lines_readonly (priv->layout); l; l = l->next)
4059 PangoLayoutLine *line = l->data;
4061 /* If priv->select_info->selection_end is at the very end of
4062 * the line, we don't know if the cursor is on this line or
4063 * the next without looking ahead at the next line. (End
4064 * of paragraph is different from line break.) But it's
4065 * definitely in this paragraph, which is good enough
4066 * to figure out the resolved direction.
4068 if (line->start_index + line->length >= priv->select_info->selection_end)
4069 return line->resolved_dir;
4072 return PANGO_DIRECTION_LTR;
4076 gtk_label_draw_cursor (GtkLabel *label, cairo_t *cr, gint xoffset, gint yoffset)
4078 GtkLabelPrivate *priv = label->priv;
4081 if (priv->select_info == NULL)
4084 widget = GTK_WIDGET (label);
4086 if (gtk_widget_is_drawable (widget))
4088 PangoDirection keymap_direction;
4089 PangoDirection cursor_direction;
4090 PangoRectangle strong_pos, weak_pos;
4091 gboolean split_cursor;
4092 PangoRectangle *cursor1 = NULL;
4093 PangoRectangle *cursor2 = NULL;
4094 GdkRectangle cursor_location;
4095 PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
4096 PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
4098 keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gtk_widget_get_display (widget)));
4099 cursor_direction = get_cursor_direction (label);
4101 gtk_label_ensure_layout (label, FALSE);
4103 pango_layout_get_cursor_pos (priv->layout, priv->select_info->selection_end,
4104 &strong_pos, &weak_pos);
4106 g_object_get (gtk_widget_get_settings (widget),
4107 "gtk-split-cursor", &split_cursor,
4110 dir1 = cursor_direction;
4114 cursor1 = &strong_pos;
4116 if (strong_pos.x != weak_pos.x ||
4117 strong_pos.y != weak_pos.y)
4119 dir2 = (cursor_direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
4120 cursor2 = &weak_pos;
4125 if (keymap_direction == cursor_direction)
4126 cursor1 = &strong_pos;
4128 cursor1 = &weak_pos;
4131 cursor_location.x = xoffset + PANGO_PIXELS (cursor1->x);
4132 cursor_location.y = yoffset + PANGO_PIXELS (cursor1->y);
4133 cursor_location.width = 0;
4134 cursor_location.height = PANGO_PIXELS (cursor1->height);
4136 draw_insertion_cursor (label, cr,
4137 &cursor_location, TRUE, dir1,
4138 dir2 != PANGO_DIRECTION_NEUTRAL);
4140 if (dir2 != PANGO_DIRECTION_NEUTRAL)
4142 cursor_location.x = xoffset + PANGO_PIXELS (cursor2->x);
4143 cursor_location.y = yoffset + PANGO_PIXELS (cursor2->y);
4144 cursor_location.width = 0;
4145 cursor_location.height = PANGO_PIXELS (cursor2->height);
4147 draw_insertion_cursor (label, cr,
4148 &cursor_location, FALSE, dir2,
4154 static GtkLabelLink *
4155 gtk_label_get_focus_link (GtkLabel *label)
4157 GtkLabelPrivate *priv = label->priv;
4158 GtkLabelSelectionInfo *info = priv->select_info;
4164 if (info->selection_anchor != info->selection_end)
4167 for (l = info->links; l; l = l->next)
4169 GtkLabelLink *link = l->data;
4170 if (link->start <= info->selection_anchor &&
4171 info->selection_anchor <= link->end)
4179 gtk_label_draw (GtkWidget *widget,
4182 GtkLabel *label = GTK_LABEL (widget);
4183 GtkLabelPrivate *priv = label->priv;
4184 GtkLabelSelectionInfo *info = priv->select_info;
4185 GtkAllocation allocation;
4186 GtkStyleContext *context;
4187 GtkStateFlags state;
4190 gtk_label_ensure_layout (label, FALSE);
4192 if (priv->text && (*priv->text != '\0'))
4194 GdkRGBA *bg_color, *fg_color;
4196 get_layout_location (label, &x, &y);
4198 context = gtk_widget_get_style_context (widget);
4199 gtk_widget_get_allocation (widget, &allocation);
4201 cairo_translate (cr, -allocation.x, -allocation.y);
4203 state = gtk_widget_get_state_flags (widget);
4204 gtk_style_context_set_state (context, state);
4206 gtk_render_layout (context, cr,
4211 (info->selection_anchor != info->selection_end))
4214 cairo_region_t *clip;
4216 range[0] = info->selection_anchor;
4217 range[1] = info->selection_end;
4219 if (range[0] > range[1])
4221 gint tmp = range[0];
4222 range[0] = range[1];
4226 clip = gdk_pango_layout_get_clip_region (priv->layout,
4231 /* FIXME should use gtk_paint, but it can't use a clip region */
4234 gdk_cairo_region (cr, clip);
4237 state = GTK_STATE_FLAG_SELECTED;
4239 if (gtk_widget_has_focus (widget))
4240 state |= GTK_STATE_FLAG_FOCUSED;
4242 gtk_style_context_get (context, state,
4243 "background-color", &bg_color,
4247 gdk_cairo_set_source_rgba (cr, bg_color);
4250 gdk_cairo_set_source_rgba (cr, fg_color);
4251 cairo_move_to (cr, x, y);
4252 _gtk_pango_fill_layout (cr, priv->layout);
4255 cairo_region_destroy (clip);
4257 gdk_rgba_free (bg_color);
4258 gdk_rgba_free (fg_color);
4262 GtkLabelLink *focus_link;
4263 GtkLabelLink *active_link;
4265 cairo_region_t *clip;
4267 GdkColor *text_color;
4268 GdkColor *link_color;
4269 GdkColor *visited_link_color;
4271 if (info->selectable && gtk_widget_has_focus (widget))
4272 gtk_label_draw_cursor (label, cr, x, y);
4274 focus_link = gtk_label_get_focus_link (label);
4275 active_link = info->active_link;
4282 range[0] = active_link->start;
4283 range[1] = active_link->end;
4287 clip = gdk_pango_layout_get_clip_region (priv->layout,
4291 gdk_cairo_region (cr, clip);
4293 cairo_region_destroy (clip);
4295 gtk_label_get_link_colors (widget, &link_color, &visited_link_color);
4296 if (active_link->visited)
4297 text_color = visited_link_color;
4299 text_color = link_color;
4301 if (info->link_clicked)
4302 state = GTK_STATE_FLAG_ACTIVE;
4304 state = GTK_STATE_FLAG_PRELIGHT;
4306 gtk_style_context_get (context, state,
4307 "background-color", &bg_color,
4310 gdk_cairo_set_source_rgba (cr, bg_color);
4313 gdk_cairo_set_source_color (cr, text_color);
4314 cairo_move_to (cr, x, y);
4315 _gtk_pango_fill_layout (cr, priv->layout);
4317 gdk_color_free (link_color);
4318 gdk_color_free (visited_link_color);
4319 gdk_rgba_free (bg_color);
4324 if (focus_link && gtk_widget_has_focus (widget))
4326 range[0] = focus_link->start;
4327 range[1] = focus_link->end;
4329 clip = gdk_pango_layout_get_clip_region (priv->layout,
4333 cairo_region_get_extents (clip, &rect);
4335 state = gtk_widget_get_state_flags (widget);
4336 gtk_style_context_set_state (context, state);
4338 gtk_render_focus (context, cr,
4340 rect.width, rect.height);
4342 cairo_region_destroy (clip);
4351 separate_uline_pattern (const gchar *str,
4356 gboolean underscore;
4359 gchar *pattern_dest;
4361 *accel_key = GDK_KEY_VoidSymbol;
4362 *new_str = g_new (gchar, strlen (str) + 1);
4363 *pattern = g_new (gchar, g_utf8_strlen (str, -1) + 1);
4369 pattern_dest = *pattern;
4374 const gchar *next_src;
4376 c = g_utf8_get_char (src);
4377 if (c == (gunichar)-1)
4379 g_warning ("Invalid input string");
4385 next_src = g_utf8_next_char (src);
4390 *pattern_dest++ = ' ';
4393 *pattern_dest++ = '_';
4394 if (*accel_key == GDK_KEY_VoidSymbol)
4395 *accel_key = gdk_keyval_to_lower (gdk_unicode_to_keyval (c));
4398 while (src < next_src)
4412 while (src < next_src)
4415 *pattern_dest++ = ' ';
4427 gtk_label_set_uline_text_internal (GtkLabel *label,
4430 GtkLabelPrivate *priv = label->priv;
4431 guint accel_key = GDK_KEY_VoidSymbol;
4435 g_return_if_fail (GTK_IS_LABEL (label));
4436 g_return_if_fail (str != NULL);
4438 /* Split text into the base text and a separate pattern
4441 if (!separate_uline_pattern (str, &accel_key, &new_str, &pattern))
4444 gtk_label_set_text_internal (label, new_str);
4445 gtk_label_set_pattern_internal (label, pattern, TRUE);
4446 priv->mnemonic_keyval = accel_key;
4452 * gtk_label_set_text_with_mnemonic:
4453 * @label: a #GtkLabel
4456 * Sets the label's text from the string @str.
4457 * If characters in @str are preceded by an underscore, they are underlined
4458 * indicating that they represent a keyboard accelerator called a mnemonic.
4459 * The mnemonic key can be used to activate another widget, chosen
4460 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
4463 gtk_label_set_text_with_mnemonic (GtkLabel *label,
4466 g_return_if_fail (GTK_IS_LABEL (label));
4467 g_return_if_fail (str != NULL);
4469 g_object_freeze_notify (G_OBJECT (label));
4471 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
4472 gtk_label_set_use_markup_internal (label, FALSE);
4473 gtk_label_set_use_underline_internal (label, TRUE);
4475 gtk_label_recalculate (label);
4477 g_object_thaw_notify (G_OBJECT (label));
4481 gtk_label_realize (GtkWidget *widget)
4483 GtkLabel *label = GTK_LABEL (widget);
4484 GtkLabelPrivate *priv = label->priv;
4486 GTK_WIDGET_CLASS (gtk_label_parent_class)->realize (widget);
4488 if (priv->select_info)
4489 gtk_label_create_window (label);
4493 gtk_label_unrealize (GtkWidget *widget)
4495 GtkLabel *label = GTK_LABEL (widget);
4496 GtkLabelPrivate *priv = label->priv;
4498 if (priv->select_info)
4499 gtk_label_destroy_window (label);
4501 GTK_WIDGET_CLASS (gtk_label_parent_class)->unrealize (widget);
4505 gtk_label_map (GtkWidget *widget)
4507 GtkLabel *label = GTK_LABEL (widget);
4508 GtkLabelPrivate *priv = label->priv;
4510 GTK_WIDGET_CLASS (gtk_label_parent_class)->map (widget);
4512 if (priv->select_info)
4513 gdk_window_show (priv->select_info->window);
4517 gtk_label_unmap (GtkWidget *widget)
4519 GtkLabel *label = GTK_LABEL (widget);
4520 GtkLabelPrivate *priv = label->priv;
4522 if (priv->select_info)
4523 gdk_window_hide (priv->select_info->window);
4525 GTK_WIDGET_CLASS (gtk_label_parent_class)->unmap (widget);
4529 window_to_layout_coords (GtkLabel *label,
4533 GtkAllocation allocation;
4537 widget = GTK_WIDGET (label);
4539 /* get layout location in widget->window coords */
4540 get_layout_location (label, &lx, &ly);
4542 gtk_widget_get_allocation (widget, &allocation);
4546 *x += allocation.x; /* go to widget->window */
4547 *x -= lx; /* go to layout */
4552 *y += allocation.y; /* go to widget->window */
4553 *y -= ly; /* go to layout */
4559 layout_to_window_coords (GtkLabel *label,
4566 widget = GTK_WIDGET (label);
4568 /* get layout location in widget->window coords */
4569 get_layout_location (label, &lx, &ly);
4573 *x += lx; /* go to widget->window */
4574 *x -= widget->allocation.x; /* go to selection window */
4579 *y += ly; /* go to widget->window */
4580 *y -= widget->allocation.y; /* go to selection window */
4586 get_layout_index (GtkLabel *label,
4591 GtkLabelPrivate *priv = label->priv;
4593 const gchar *cluster;
4594 const gchar *cluster_end;
4599 gtk_label_ensure_layout (label, FALSE);
4601 window_to_layout_coords (label, &x, &y);
4606 inside = pango_layout_xy_to_index (priv->layout,
4610 cluster = priv->text + *index;
4611 cluster_end = cluster;
4614 cluster_end = g_utf8_next_char (cluster_end);
4618 *index += (cluster_end - cluster);
4624 gtk_label_select_word (GtkLabel *label)
4626 GtkLabelPrivate *priv = label->priv;
4629 gint start_index = gtk_label_move_backward_word (label, priv->select_info->selection_end);
4630 gint end_index = gtk_label_move_forward_word (label, priv->select_info->selection_end);
4632 min = MIN (priv->select_info->selection_anchor,
4633 priv->select_info->selection_end);
4634 max = MAX (priv->select_info->selection_anchor,
4635 priv->select_info->selection_end);
4637 min = MIN (min, start_index);
4638 max = MAX (max, end_index);
4640 gtk_label_select_region_index (label, min, max);
4644 gtk_label_grab_focus (GtkWidget *widget)
4646 GtkLabel *label = GTK_LABEL (widget);
4647 GtkLabelPrivate *priv = label->priv;
4648 gboolean select_on_focus;
4651 if (priv->select_info == NULL)
4654 GTK_WIDGET_CLASS (gtk_label_parent_class)->grab_focus (widget);
4656 if (priv->select_info->selectable)
4658 g_object_get (gtk_widget_get_settings (widget),
4659 "gtk-label-select-on-focus",
4663 if (select_on_focus && !priv->in_click)
4664 gtk_label_select_region (label, 0, -1);
4668 if (priv->select_info->links && !priv->in_click)
4670 link = priv->select_info->links->data;
4671 priv->select_info->selection_anchor = link->start;
4672 priv->select_info->selection_end = link->start;
4678 gtk_label_focus (GtkWidget *widget,
4679 GtkDirectionType direction)
4681 GtkLabel *label = GTK_LABEL (widget);
4682 GtkLabelPrivate *priv = label->priv;
4683 GtkLabelSelectionInfo *info = priv->select_info;
4684 GtkLabelLink *focus_link;
4687 if (!gtk_widget_is_focus (widget))
4689 gtk_widget_grab_focus (widget);
4692 focus_link = gtk_label_get_focus_link (label);
4693 if (focus_link && direction == GTK_DIR_TAB_BACKWARD)
4695 l = g_list_last (info->links);
4696 focus_link = l->data;
4697 info->selection_anchor = focus_link->start;
4698 info->selection_end = focus_link->start;
4708 if (info->selectable)
4712 if (info->selection_anchor != info->selection_end)
4715 index = info->selection_anchor;
4717 if (direction == GTK_DIR_TAB_FORWARD)
4718 for (l = info->links; l; l = l->next)
4720 GtkLabelLink *link = l->data;
4722 if (link->start > index)
4724 gtk_label_select_region_index (label, link->start, link->start);
4728 else if (direction == GTK_DIR_TAB_BACKWARD)
4729 for (l = g_list_last (info->links); l; l = l->prev)
4731 GtkLabelLink *link = l->data;
4733 if (link->end < index)
4735 gtk_label_select_region_index (label, link->start, link->start);
4744 focus_link = gtk_label_get_focus_link (label);
4747 case GTK_DIR_TAB_FORWARD:
4750 l = g_list_find (info->links, focus_link);
4757 case GTK_DIR_TAB_BACKWARD:
4760 l = g_list_find (info->links, focus_link);
4764 l = g_list_last (info->links);
4773 focus_link = l->data;
4774 info->selection_anchor = focus_link->start;
4775 info->selection_end = focus_link->start;
4776 gtk_widget_queue_draw (widget);
4788 gtk_label_button_press (GtkWidget *widget,
4789 GdkEventButton *event)
4791 GtkLabel *label = GTK_LABEL (widget);
4792 GtkLabelPrivate *priv = label->priv;
4793 GtkLabelSelectionInfo *info = priv->select_info;
4800 if (info->active_link)
4802 if (event->button == 1)
4804 info->link_clicked = 1;
4805 gtk_widget_queue_draw (widget);
4807 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4809 info->link_clicked = 1;
4810 gtk_label_do_popup (label, event);
4815 if (!info->selectable)
4818 info->in_drag = FALSE;
4819 info->select_words = FALSE;
4821 if (event->button == 1)
4823 if (!gtk_widget_has_focus (widget))
4825 priv->in_click = TRUE;
4826 gtk_widget_grab_focus (widget);
4827 priv->in_click = FALSE;
4830 if (event->type == GDK_3BUTTON_PRESS)
4832 gtk_label_select_region_index (label, 0, strlen (priv->text));
4836 if (event->type == GDK_2BUTTON_PRESS)
4838 info->select_words = TRUE;
4839 gtk_label_select_word (label);
4843 get_layout_index (label, event->x, event->y, &index);
4845 min = MIN (info->selection_anchor, info->selection_end);
4846 max = MAX (info->selection_anchor, info->selection_end);
4848 if ((info->selection_anchor != info->selection_end) &&
4849 (event->state & GDK_SHIFT_MASK))
4851 /* extend (same as motion) */
4852 min = MIN (min, index);
4853 max = MAX (max, index);
4855 /* ensure the anchor is opposite index */
4863 gtk_label_select_region_index (label, min, max);
4867 if (event->type == GDK_3BUTTON_PRESS)
4868 gtk_label_select_region_index (label, 0, strlen (priv->text));
4869 else if (event->type == GDK_2BUTTON_PRESS)
4870 gtk_label_select_word (label);
4871 else if (min < max && min <= index && index <= max)
4873 info->in_drag = TRUE;
4874 info->drag_start_x = event->x;
4875 info->drag_start_y = event->y;
4878 /* start a replacement */
4879 gtk_label_select_region_index (label, index, index);
4884 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4886 gtk_label_do_popup (label, event);
4894 gtk_label_button_release (GtkWidget *widget,
4895 GdkEventButton *event)
4898 GtkLabel *label = GTK_LABEL (widget);
4899 GtkLabelPrivate *priv = label->priv;
4900 GtkLabelSelectionInfo *info = priv->select_info;
4910 get_layout_index (label, event->x, event->y, &index);
4911 gtk_label_select_region_index (label, index, index);
4916 if (event->button != 1)
4919 if (info->active_link &&
4920 info->selection_anchor == info->selection_end &&
4923 emit_activate_link (label, info->active_link);
4924 info->link_clicked = 0;
4929 /* The goal here is to return TRUE iff we ate the
4930 * button press to start selecting.
4936 connect_mnemonics_visible_notify (GtkLabel *label)
4938 GtkLabelPrivate *priv = label->priv;
4939 GtkWidget *toplevel;
4942 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
4944 if (!GTK_IS_WINDOW (toplevel))
4947 /* always set up this widgets initial value */
4948 priv->mnemonics_visible =
4949 gtk_window_get_mnemonics_visible (GTK_WINDOW (toplevel));
4952 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (toplevel),
4953 "gtk-label-mnemonics-visible-connected"));
4957 g_signal_connect (toplevel,
4958 "notify::mnemonics-visible",
4959 G_CALLBACK (label_mnemonics_visible_changed),
4961 g_object_set_data (G_OBJECT (toplevel),
4962 "gtk-label-mnemonics-visible-connected",
4963 GINT_TO_POINTER (1));
4968 drag_begin_cb (GtkWidget *widget,
4969 GdkDragContext *context,
4972 GtkLabel *label = GTK_LABEL (widget);
4973 GtkLabelPrivate *priv = label->priv;
4974 cairo_surface_t *surface = NULL;
4976 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
4978 if ((priv->select_info->selection_anchor !=
4979 priv->select_info->selection_end) &&
4985 start = MIN (priv->select_info->selection_anchor,
4986 priv->select_info->selection_end);
4987 end = MAX (priv->select_info->selection_anchor,
4988 priv->select_info->selection_end);
4990 len = strlen (priv->text);
4998 surface = _gtk_text_util_create_drag_icon (widget,
5005 gtk_drag_set_icon_surface (context, surface);
5006 cairo_surface_destroy (surface);
5010 gtk_drag_set_icon_default (context);
5015 gtk_label_motion (GtkWidget *widget,
5016 GdkEventMotion *event)
5018 GtkLabel *label = GTK_LABEL (widget);
5019 GtkLabelPrivate *priv = label->priv;
5020 GtkLabelSelectionInfo *info = priv->select_info;
5026 if (info->links && !info->in_drag)
5030 gboolean found = FALSE;
5032 if (info->selection_anchor == info->selection_end)
5034 if (get_layout_index (label, event->x, event->y, &index))
5036 for (l = info->links; l != NULL; l = l->next)
5039 if (index >= link->start && index <= link->end)
5050 if (info->active_link != link)
5052 info->link_clicked = 0;
5053 info->active_link = link;
5054 gtk_label_update_cursor (label);
5055 gtk_widget_queue_draw (widget);
5060 if (info->active_link != NULL)
5062 info->link_clicked = 0;
5063 info->active_link = NULL;
5064 gtk_label_update_cursor (label);
5065 gtk_widget_queue_draw (widget);
5070 if (!info->selectable)
5073 if ((event->state & GDK_BUTTON1_MASK) == 0)
5078 if (gtk_drag_check_threshold (widget,
5081 event->x, event->y))
5083 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
5085 gtk_target_list_add_text_targets (target_list, 0);
5087 g_signal_connect (widget, "drag-begin",
5088 G_CALLBACK (drag_begin_cb), NULL);
5089 gtk_drag_begin (widget, target_list,
5091 1, (GdkEvent *)event);
5093 info->in_drag = FALSE;
5095 gtk_target_list_unref (target_list);
5102 gdk_window_get_device_position (info->window, event->device, &x, &y, NULL);
5103 get_layout_index (label, x, y, &index);
5105 if (info->select_words)
5108 gint old_min, old_max;
5111 min = gtk_label_move_backward_word (label, index);
5112 max = gtk_label_move_forward_word (label, index);
5114 anchor = info->selection_anchor;
5115 end = info->selection_end;
5117 old_min = MIN (anchor, end);
5118 old_max = MAX (anchor, end);
5125 else if (old_max < max)
5130 else if (anchor == old_min)
5141 gtk_label_select_region_index (label, anchor, end);
5144 gtk_label_select_region_index (label, info->selection_anchor, index);
5151 gtk_label_leave_notify (GtkWidget *widget,
5152 GdkEventCrossing *event)
5154 GtkLabel *label = GTK_LABEL (widget);
5155 GtkLabelPrivate *priv = label->priv;
5157 if (priv->select_info)
5159 priv->select_info->active_link = NULL;
5160 gtk_label_update_cursor (label);
5161 gtk_widget_queue_draw (widget);
5164 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event)
5165 return GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event (widget, event);
5171 gtk_label_create_window (GtkLabel *label)
5173 GtkLabelPrivate *priv = label->priv;
5174 GtkAllocation allocation;
5176 GdkWindowAttr attributes;
5177 gint attributes_mask;
5179 g_assert (priv->select_info);
5180 widget = GTK_WIDGET (label);
5181 g_assert (gtk_widget_get_realized (widget));
5183 if (priv->select_info->window)
5186 gtk_widget_get_allocation (widget, &allocation);
5188 attributes.x = allocation.x;
5189 attributes.y = allocation.y;
5190 attributes.width = allocation.width;
5191 attributes.height = allocation.height;
5192 attributes.window_type = GDK_WINDOW_CHILD;
5193 attributes.wclass = GDK_INPUT_ONLY;
5194 attributes.override_redirect = TRUE;
5195 attributes.event_mask = gtk_widget_get_events (widget) |
5196 GDK_BUTTON_PRESS_MASK |
5197 GDK_BUTTON_RELEASE_MASK |
5198 GDK_LEAVE_NOTIFY_MASK |
5199 GDK_BUTTON_MOTION_MASK |
5200 GDK_POINTER_MOTION_MASK |
5201 GDK_POINTER_MOTION_HINT_MASK;
5202 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
5203 if (gtk_widget_is_sensitive (widget))
5205 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
5207 attributes_mask |= GDK_WA_CURSOR;
5211 priv->select_info->window = gdk_window_new (gtk_widget_get_window (widget),
5212 &attributes, attributes_mask);
5213 gdk_window_set_user_data (priv->select_info->window, widget);
5215 if (attributes_mask & GDK_WA_CURSOR)
5216 g_object_unref (attributes.cursor);
5220 gtk_label_destroy_window (GtkLabel *label)
5222 GtkLabelPrivate *priv = label->priv;
5224 g_assert (priv->select_info);
5226 if (priv->select_info->window == NULL)
5229 gdk_window_set_user_data (priv->select_info->window, NULL);
5230 gdk_window_destroy (priv->select_info->window);
5231 priv->select_info->window = NULL;
5235 gtk_label_ensure_select_info (GtkLabel *label)
5237 GtkLabelPrivate *priv = label->priv;
5239 if (priv->select_info == NULL)
5241 priv->select_info = g_new0 (GtkLabelSelectionInfo, 1);
5243 gtk_widget_set_can_focus (GTK_WIDGET (label), TRUE);
5245 if (gtk_widget_get_realized (GTK_WIDGET (label)))
5246 gtk_label_create_window (label);
5248 if (gtk_widget_get_mapped (GTK_WIDGET (label)))
5249 gdk_window_show (priv->select_info->window);
5254 gtk_label_clear_select_info (GtkLabel *label)
5256 GtkLabelPrivate *priv = label->priv;
5258 if (priv->select_info == NULL)
5261 if (!priv->select_info->selectable && !priv->select_info->links)
5263 gtk_label_destroy_window (label);
5265 g_free (priv->select_info);
5266 priv->select_info = NULL;
5268 gtk_widget_set_can_focus (GTK_WIDGET (label), FALSE);
5273 * gtk_label_set_selectable:
5274 * @label: a #GtkLabel
5275 * @setting: %TRUE to allow selecting text in the label
5277 * Selectable labels allow the user to select text from the label, for
5281 gtk_label_set_selectable (GtkLabel *label,
5284 GtkLabelPrivate *priv;
5285 gboolean old_setting;
5287 g_return_if_fail (GTK_IS_LABEL (label));
5291 setting = setting != FALSE;
5292 old_setting = priv->select_info && priv->select_info->selectable;
5296 gtk_label_ensure_select_info (label);
5297 priv->select_info->selectable = TRUE;
5298 gtk_label_update_cursor (label);
5304 /* unselect, to give up the selection */
5305 gtk_label_select_region (label, 0, 0);
5307 priv->select_info->selectable = FALSE;
5308 gtk_label_clear_select_info (label);
5309 gtk_label_update_cursor (label);
5312 if (setting != old_setting)
5314 g_object_freeze_notify (G_OBJECT (label));
5315 g_object_notify (G_OBJECT (label), "selectable");
5316 g_object_notify (G_OBJECT (label), "cursor-position");
5317 g_object_notify (G_OBJECT (label), "selection-bound");
5318 g_object_thaw_notify (G_OBJECT (label));
5319 gtk_widget_queue_draw (GTK_WIDGET (label));
5324 * gtk_label_get_selectable:
5325 * @label: a #GtkLabel
5327 * Gets the value set by gtk_label_set_selectable().
5329 * Return value: %TRUE if the user can copy text from the label
5332 gtk_label_get_selectable (GtkLabel *label)
5334 GtkLabelPrivate *priv;
5336 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5340 return priv->select_info && priv->select_info->selectable;
5344 * gtk_label_set_angle:
5345 * @label: a #GtkLabel
5346 * @angle: the angle that the baseline of the label makes with
5347 * the horizontal, in degrees, measured counterclockwise
5349 * Sets the angle of rotation for the label. An angle of 90 reads from
5350 * from bottom to top, an angle of 270, from top to bottom. The angle
5351 * setting for the label is ignored if the label is selectable,
5352 * wrapped, or ellipsized.
5357 gtk_label_set_angle (GtkLabel *label,
5360 GtkLabelPrivate *priv;
5362 g_return_if_fail (GTK_IS_LABEL (label));
5366 /* Canonicalize to [0,360]. We don't canonicalize 360 to 0, because
5367 * double property ranges are inclusive, and changing 360 to 0 would
5368 * make a property editor behave strangely.
5370 if (angle < 0 || angle > 360.0)
5371 angle = angle - 360. * floor (angle / 360.);
5373 if (priv->angle != angle)
5375 priv->angle = angle;
5377 gtk_label_clear_layout (label);
5378 gtk_widget_queue_resize (GTK_WIDGET (label));
5380 g_object_notify (G_OBJECT (label), "angle");
5385 * gtk_label_get_angle:
5386 * @label: a #GtkLabel
5388 * Gets the angle of rotation for the label. See
5389 * gtk_label_set_angle().
5391 * Return value: the angle of rotation for the label
5396 gtk_label_get_angle (GtkLabel *label)
5398 g_return_val_if_fail (GTK_IS_LABEL (label), 0.0);
5400 return label->priv->angle;
5404 gtk_label_set_selection_text (GtkLabel *label,
5405 GtkSelectionData *selection_data)
5407 GtkLabelPrivate *priv = label->priv;
5409 if ((priv->select_info->selection_anchor !=
5410 priv->select_info->selection_end) &&
5416 start = MIN (priv->select_info->selection_anchor,
5417 priv->select_info->selection_end);
5418 end = MAX (priv->select_info->selection_anchor,
5419 priv->select_info->selection_end);
5421 len = strlen (priv->text);
5429 gtk_selection_data_set_text (selection_data,
5436 gtk_label_drag_data_get (GtkWidget *widget,
5437 GdkDragContext *context,
5438 GtkSelectionData *selection_data,
5442 gtk_label_set_selection_text (GTK_LABEL (widget), selection_data);
5446 get_text_callback (GtkClipboard *clipboard,
5447 GtkSelectionData *selection_data,
5449 gpointer user_data_or_owner)
5451 gtk_label_set_selection_text (GTK_LABEL (user_data_or_owner), selection_data);
5455 clear_text_callback (GtkClipboard *clipboard,
5456 gpointer user_data_or_owner)
5459 GtkLabelPrivate *priv;
5461 label = GTK_LABEL (user_data_or_owner);
5464 if (priv->select_info)
5466 priv->select_info->selection_anchor = priv->select_info->selection_end;
5468 gtk_widget_queue_draw (GTK_WIDGET (label));
5473 gtk_label_select_region_index (GtkLabel *label,
5477 GtkLabelPrivate *priv;
5479 g_return_if_fail (GTK_IS_LABEL (label));
5483 if (priv->select_info && priv->select_info->selectable)
5485 GtkClipboard *clipboard;
5487 if (priv->select_info->selection_anchor == anchor_index &&
5488 priv->select_info->selection_end == end_index)
5491 priv->select_info->selection_anchor = anchor_index;
5492 priv->select_info->selection_end = end_index;
5494 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label),
5495 GDK_SELECTION_PRIMARY);
5497 if (anchor_index != end_index)
5499 GtkTargetList *list;
5500 GtkTargetEntry *targets;
5503 list = gtk_target_list_new (NULL, 0);
5504 gtk_target_list_add_text_targets (list, 0);
5505 targets = gtk_target_table_new_from_list (list, &n_targets);
5507 gtk_clipboard_set_with_owner (clipboard,
5510 clear_text_callback,
5513 gtk_target_table_free (targets, n_targets);
5514 gtk_target_list_unref (list);
5518 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (label))
5519 gtk_clipboard_clear (clipboard);
5522 gtk_widget_queue_draw (GTK_WIDGET (label));
5524 g_object_freeze_notify (G_OBJECT (label));
5525 g_object_notify (G_OBJECT (label), "cursor-position");
5526 g_object_notify (G_OBJECT (label), "selection-bound");
5527 g_object_thaw_notify (G_OBJECT (label));
5532 * gtk_label_select_region:
5533 * @label: a #GtkLabel
5534 * @start_offset: start offset (in characters not bytes)
5535 * @end_offset: end offset (in characters not bytes)
5537 * Selects a range of characters in the label, if the label is selectable.
5538 * See gtk_label_set_selectable(). If the label is not selectable,
5539 * this function has no effect. If @start_offset or
5540 * @end_offset are -1, then the end of the label will be substituted.
5543 gtk_label_select_region (GtkLabel *label,
5547 GtkLabelPrivate *priv;
5549 g_return_if_fail (GTK_IS_LABEL (label));
5553 if (priv->text && priv->select_info)
5555 if (start_offset < 0)
5556 start_offset = g_utf8_strlen (priv->text, -1);
5559 end_offset = g_utf8_strlen (priv->text, -1);
5561 gtk_label_select_region_index (label,
5562 g_utf8_offset_to_pointer (priv->text, start_offset) - priv->text,
5563 g_utf8_offset_to_pointer (priv->text, end_offset) - priv->text);
5568 * gtk_label_get_selection_bounds:
5569 * @label: a #GtkLabel
5570 * @start: (out): return location for start of selection, as a character offset
5571 * @end: (out): return location for end of selection, as a character offset
5573 * Gets the selected range of characters in the label, returning %TRUE
5574 * if there's a selection.
5576 * Return value: %TRUE if selection is non-empty
5579 gtk_label_get_selection_bounds (GtkLabel *label,
5583 GtkLabelPrivate *priv;
5585 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5589 if (priv->select_info == NULL)
5591 /* not a selectable label */
5601 gint start_index, end_index;
5602 gint start_offset, end_offset;
5605 start_index = MIN (priv->select_info->selection_anchor,
5606 priv->select_info->selection_end);
5607 end_index = MAX (priv->select_info->selection_anchor,
5608 priv->select_info->selection_end);
5610 len = strlen (priv->text);
5612 if (end_index > len)
5615 if (start_index > len)
5618 start_offset = g_utf8_strlen (priv->text, start_index);
5619 end_offset = g_utf8_strlen (priv->text, end_index);
5621 if (start_offset > end_offset)
5623 gint tmp = start_offset;
5624 start_offset = end_offset;
5629 *start = start_offset;
5634 return start_offset != end_offset;
5640 * gtk_label_get_layout:
5641 * @label: a #GtkLabel
5643 * Gets the #PangoLayout used to display the label.
5644 * The layout is useful to e.g. convert text positions to
5645 * pixel positions, in combination with gtk_label_get_layout_offsets().
5646 * The returned layout is owned by the @label so need not be
5647 * freed by the caller. The @label is free to recreate its layout at
5648 * any time, so it should be considered read-only.
5650 * Return value: (transfer none): the #PangoLayout for this label
5653 gtk_label_get_layout (GtkLabel *label)
5655 GtkLabelPrivate *priv;
5657 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
5661 gtk_label_ensure_layout (label, FALSE);
5663 return priv->layout;
5667 * gtk_label_get_layout_offsets:
5668 * @label: a #GtkLabel
5669 * @x: (out) (allow-none): location to store X offset of layout, or %NULL
5670 * @y: (out) (allow-none): location to store Y offset of layout, or %NULL
5672 * Obtains the coordinates where the label will draw the #PangoLayout
5673 * representing the text in the label; useful to convert mouse events
5674 * into coordinates inside the #PangoLayout, e.g. to take some action
5675 * if some part of the label is clicked. Of course you will need to
5676 * create a #GtkEventBox to receive the events, and pack the label
5677 * inside it, since labels are a #GTK_NO_WINDOW widget. Remember
5678 * when using the #PangoLayout functions you need to convert to
5679 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
5682 gtk_label_get_layout_offsets (GtkLabel *label,
5686 g_return_if_fail (GTK_IS_LABEL (label));
5688 gtk_label_ensure_layout (label, FALSE);
5690 get_layout_location (label, x, y);
5694 * gtk_label_set_use_markup:
5695 * @label: a #GtkLabel
5696 * @setting: %TRUE if the label's text should be parsed for markup.
5698 * Sets whether the text of the label contains markup in <link
5699 * linkend="PangoMarkupFormat">Pango's text markup
5700 * language</link>. See gtk_label_set_markup().
5703 gtk_label_set_use_markup (GtkLabel *label,
5706 g_return_if_fail (GTK_IS_LABEL (label));
5708 gtk_label_set_use_markup_internal (label, setting);
5709 gtk_label_recalculate (label);
5713 * gtk_label_get_use_markup:
5714 * @label: a #GtkLabel
5716 * Returns whether the label's text is interpreted as marked up with
5717 * the <link linkend="PangoMarkupFormat">Pango text markup
5718 * language</link>. See gtk_label_set_use_markup ().
5720 * Return value: %TRUE if the label's text will be parsed for markup.
5723 gtk_label_get_use_markup (GtkLabel *label)
5725 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5727 return label->priv->use_markup;
5731 * gtk_label_set_use_underline:
5732 * @label: a #GtkLabel
5733 * @setting: %TRUE if underlines in the text indicate mnemonics
5735 * If true, an underline in the text indicates the next character should be
5736 * used for the mnemonic accelerator key.
5739 gtk_label_set_use_underline (GtkLabel *label,
5742 g_return_if_fail (GTK_IS_LABEL (label));
5744 gtk_label_set_use_underline_internal (label, setting);
5745 gtk_label_recalculate (label);
5749 * gtk_label_get_use_underline:
5750 * @label: a #GtkLabel
5752 * Returns whether an embedded underline in the label indicates a
5753 * mnemonic. See gtk_label_set_use_underline().
5755 * Return value: %TRUE whether an embedded underline in the label indicates
5756 * the mnemonic accelerator keys.
5759 gtk_label_get_use_underline (GtkLabel *label)
5761 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5763 return label->priv->use_underline;
5767 * gtk_label_set_single_line_mode:
5768 * @label: a #GtkLabel
5769 * @single_line_mode: %TRUE if the label should be in single line mode
5771 * Sets whether the label is in single line mode.
5776 gtk_label_set_single_line_mode (GtkLabel *label,
5777 gboolean single_line_mode)
5779 GtkLabelPrivate *priv;
5781 g_return_if_fail (GTK_IS_LABEL (label));
5785 single_line_mode = single_line_mode != FALSE;
5787 if (priv->single_line_mode != single_line_mode)
5789 priv->single_line_mode = single_line_mode;
5791 gtk_label_clear_layout (label);
5792 gtk_widget_queue_resize (GTK_WIDGET (label));
5794 g_object_notify (G_OBJECT (label), "single-line-mode");
5799 * gtk_label_get_single_line_mode:
5800 * @label: a #GtkLabel
5802 * Returns whether the label is in single line mode.
5804 * Return value: %TRUE when the label is in single line mode.
5809 gtk_label_get_single_line_mode (GtkLabel *label)
5811 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5813 return label->priv->single_line_mode;
5816 /* Compute the X position for an offset that corresponds to the "more important
5817 * cursor position for that offset. We use this when trying to guess to which
5818 * end of the selection we should go to when the user hits the left or
5822 get_better_cursor (GtkLabel *label,
5827 GtkLabelPrivate *priv = label->priv;
5828 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5829 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5830 PangoDirection cursor_direction = get_cursor_direction (label);
5831 gboolean split_cursor;
5832 PangoRectangle strong_pos, weak_pos;
5834 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5835 "gtk-split-cursor", &split_cursor,
5838 gtk_label_ensure_layout (label, FALSE);
5840 pango_layout_get_cursor_pos (priv->layout, index,
5841 &strong_pos, &weak_pos);
5845 *x = strong_pos.x / PANGO_SCALE;
5846 *y = strong_pos.y / PANGO_SCALE;
5850 if (keymap_direction == cursor_direction)
5852 *x = strong_pos.x / PANGO_SCALE;
5853 *y = strong_pos.y / PANGO_SCALE;
5857 *x = weak_pos.x / PANGO_SCALE;
5858 *y = weak_pos.y / PANGO_SCALE;
5865 gtk_label_move_logically (GtkLabel *label,
5869 GtkLabelPrivate *priv = label->priv;
5870 gint offset = g_utf8_pointer_to_offset (priv->text,
5871 priv->text + start);
5875 PangoLogAttr *log_attrs;
5879 gtk_label_ensure_layout (label, FALSE);
5881 length = g_utf8_strlen (priv->text, -1);
5883 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5885 while (count > 0 && offset < length)
5889 while (offset < length && !log_attrs[offset].is_cursor_position);
5893 while (count < 0 && offset > 0)
5897 while (offset > 0 && !log_attrs[offset].is_cursor_position);
5905 return g_utf8_offset_to_pointer (priv->text, offset) - priv->text;
5909 gtk_label_move_visually (GtkLabel *label,
5913 GtkLabelPrivate *priv = label->priv;
5920 int new_index, new_trailing;
5921 gboolean split_cursor;
5924 gtk_label_ensure_layout (label, FALSE);
5926 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5927 "gtk-split-cursor", &split_cursor,
5934 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5935 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5937 strong = keymap_direction == get_cursor_direction (label);
5942 pango_layout_move_cursor_visually (priv->layout, strong, index, 0, 1, &new_index, &new_trailing);
5947 pango_layout_move_cursor_visually (priv->layout, strong, index, 0, -1, &new_index, &new_trailing);
5951 if (new_index < 0 || new_index == G_MAXINT)
5956 while (new_trailing--)
5957 index = g_utf8_next_char (priv->text + new_index) - priv->text;
5964 gtk_label_move_forward_word (GtkLabel *label,
5967 GtkLabelPrivate *priv = label->priv;
5968 gint new_pos = g_utf8_pointer_to_offset (priv->text,
5969 priv->text + start);
5972 length = g_utf8_strlen (priv->text, -1);
5973 if (new_pos < length)
5975 PangoLogAttr *log_attrs;
5978 gtk_label_ensure_layout (label, FALSE);
5980 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5982 /* Find the next word end */
5984 while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
5990 return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
5995 gtk_label_move_backward_word (GtkLabel *label,
5998 GtkLabelPrivate *priv = label->priv;
5999 gint new_pos = g_utf8_pointer_to_offset (priv->text,
6000 priv->text + start);
6004 PangoLogAttr *log_attrs;
6007 gtk_label_ensure_layout (label, FALSE);
6009 pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
6013 /* Find the previous word beginning */
6014 while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
6020 return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
6024 gtk_label_move_cursor (GtkLabel *label,
6025 GtkMovementStep step,
6027 gboolean extend_selection)
6029 GtkLabelPrivate *priv = label->priv;
6033 if (priv->select_info == NULL)
6036 old_pos = new_pos = priv->select_info->selection_end;
6038 if (priv->select_info->selection_end != priv->select_info->selection_anchor &&
6041 /* If we have a current selection and aren't extending it, move to the
6042 * start/or end of the selection as appropriate
6046 case GTK_MOVEMENT_VISUAL_POSITIONS:
6049 gint anchor_x, anchor_y;
6050 gboolean end_is_left;
6052 get_better_cursor (label, priv->select_info->selection_end, &end_x, &end_y);
6053 get_better_cursor (label, priv->select_info->selection_anchor, &anchor_x, &anchor_y);
6055 end_is_left = (end_y < anchor_y) || (end_y == anchor_y && end_x < anchor_x);
6058 new_pos = end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
6060 new_pos = !end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
6063 case GTK_MOVEMENT_LOGICAL_POSITIONS:
6064 case GTK_MOVEMENT_WORDS:
6066 new_pos = MIN (priv->select_info->selection_end, priv->select_info->selection_anchor);
6068 new_pos = MAX (priv->select_info->selection_end, priv->select_info->selection_anchor);
6070 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
6071 case GTK_MOVEMENT_PARAGRAPH_ENDS:
6072 case GTK_MOVEMENT_BUFFER_ENDS:
6073 /* FIXME: Can do better here */
6074 new_pos = count < 0 ? 0 : strlen (priv->text);
6076 case GTK_MOVEMENT_DISPLAY_LINES:
6077 case GTK_MOVEMENT_PARAGRAPHS:
6078 case GTK_MOVEMENT_PAGES:
6079 case GTK_MOVEMENT_HORIZONTAL_PAGES:
6087 case GTK_MOVEMENT_LOGICAL_POSITIONS:
6088 new_pos = gtk_label_move_logically (label, new_pos, count);
6090 case GTK_MOVEMENT_VISUAL_POSITIONS:
6091 new_pos = gtk_label_move_visually (label, new_pos, count);
6092 if (new_pos == old_pos)
6094 if (!extend_selection)
6096 if (!gtk_widget_keynav_failed (GTK_WIDGET (label),
6098 GTK_DIR_RIGHT : GTK_DIR_LEFT))
6100 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
6103 gtk_widget_child_focus (toplevel,
6105 GTK_DIR_RIGHT : GTK_DIR_LEFT);
6110 gtk_widget_error_bell (GTK_WIDGET (label));
6114 case GTK_MOVEMENT_WORDS:
6117 new_pos = gtk_label_move_forward_word (label, new_pos);
6122 new_pos = gtk_label_move_backward_word (label, new_pos);
6125 if (new_pos == old_pos)
6126 gtk_widget_error_bell (GTK_WIDGET (label));
6128 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
6129 case GTK_MOVEMENT_PARAGRAPH_ENDS:
6130 case GTK_MOVEMENT_BUFFER_ENDS:
6131 /* FIXME: Can do better here */
6132 new_pos = count < 0 ? 0 : strlen (priv->text);
6133 if (new_pos == old_pos)
6134 gtk_widget_error_bell (GTK_WIDGET (label));
6136 case GTK_MOVEMENT_DISPLAY_LINES:
6137 case GTK_MOVEMENT_PARAGRAPHS:
6138 case GTK_MOVEMENT_PAGES:
6139 case GTK_MOVEMENT_HORIZONTAL_PAGES:
6144 if (extend_selection)
6145 gtk_label_select_region_index (label,
6146 priv->select_info->selection_anchor,
6149 gtk_label_select_region_index (label, new_pos, new_pos);
6153 gtk_label_copy_clipboard (GtkLabel *label)
6155 GtkLabelPrivate *priv = label->priv;
6157 if (priv->text && priv->select_info)
6161 GtkClipboard *clipboard;
6163 start = MIN (priv->select_info->selection_anchor,
6164 priv->select_info->selection_end);
6165 end = MAX (priv->select_info->selection_anchor,
6166 priv->select_info->selection_end);
6168 len = strlen (priv->text);
6176 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6179 gtk_clipboard_set_text (clipboard, priv->text + start, end - start);
6184 link = gtk_label_get_focus_link (label);
6186 gtk_clipboard_set_text (clipboard, link->uri, -1);
6192 gtk_label_select_all (GtkLabel *label)
6194 GtkLabelPrivate *priv = label->priv;
6196 gtk_label_select_region_index (label, 0, strlen (priv->text));
6199 /* Quick hack of a popup menu
6202 activate_cb (GtkWidget *menuitem,
6205 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6206 g_signal_emit_by_name (label, signal);
6210 append_action_signal (GtkLabel *label,
6212 const gchar *stock_id,
6213 const gchar *signal,
6216 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6218 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
6219 g_signal_connect (menuitem, "activate",
6220 G_CALLBACK (activate_cb), label);
6222 gtk_widget_set_sensitive (menuitem, sensitive);
6224 gtk_widget_show (menuitem);
6225 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6229 popup_menu_detach (GtkWidget *attach_widget,
6232 GtkLabel *label = GTK_LABEL (attach_widget);
6233 GtkLabelPrivate *priv = label->priv;
6235 if (priv->select_info)
6236 priv->select_info->popup_menu = NULL;
6240 popup_position_func (GtkMenu *menu,
6248 GtkAllocation allocation;
6252 label = GTK_LABEL (user_data);
6253 widget = GTK_WIDGET (label);
6255 g_return_if_fail (gtk_widget_get_realized (widget));
6257 screen = gtk_widget_get_screen (widget);
6258 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
6260 gtk_widget_get_allocation (widget, &allocation);
6265 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
6268 gtk_widget_get_allocation (widget, &allocation);
6270 *x += allocation.width / 2;
6271 *y += allocation.height;
6273 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
6274 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
6278 open_link_activate_cb (GtkMenuItem *menu_item,
6283 link = gtk_label_get_current_link (label);
6286 emit_activate_link (label, link);
6290 copy_link_activate_cb (GtkMenuItem *menu_item,
6293 GtkClipboard *clipboard;
6296 uri = gtk_label_get_current_uri (label);
6299 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6300 gtk_clipboard_set_text (clipboard, uri, -1);
6305 gtk_label_popup_menu (GtkWidget *widget)
6307 gtk_label_do_popup (GTK_LABEL (widget), NULL);
6313 gtk_label_do_popup (GtkLabel *label,
6314 GdkEventButton *event)
6316 GtkLabelPrivate *priv = label->priv;
6317 GtkWidget *menuitem;
6320 gboolean have_selection;
6323 if (!priv->select_info)
6326 if (priv->select_info->popup_menu)
6327 gtk_widget_destroy (priv->select_info->popup_menu);
6329 priv->select_info->popup_menu = menu = gtk_menu_new ();
6331 gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (label), popup_menu_detach);
6334 priv->select_info->selection_anchor != priv->select_info->selection_end;
6338 if (priv->select_info->link_clicked)
6339 link = priv->select_info->active_link;
6344 link = gtk_label_get_focus_link (label);
6346 if (!have_selection && link)
6349 menuitem = gtk_image_menu_item_new_with_mnemonic (_("_Open Link"));
6350 gtk_widget_show (menuitem);
6351 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6353 g_signal_connect (G_OBJECT (menuitem), "activate",
6354 G_CALLBACK (open_link_activate_cb), label);
6356 image = gtk_image_new_from_stock (GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_MENU);
6357 gtk_widget_show (image);
6358 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6360 /* Copy Link Address */
6361 menuitem = gtk_image_menu_item_new_with_mnemonic (_("Copy _Link Address"));
6362 gtk_widget_show (menuitem);
6363 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6365 g_signal_connect (G_OBJECT (menuitem), "activate",
6366 G_CALLBACK (copy_link_activate_cb), label);
6368 image = gtk_image_new_from_stock (GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
6369 gtk_widget_show (image);
6370 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6374 append_action_signal (label, menu, GTK_STOCK_CUT, "cut-clipboard", FALSE);
6375 append_action_signal (label, menu, GTK_STOCK_COPY, "copy-clipboard", have_selection);
6376 append_action_signal (label, menu, GTK_STOCK_PASTE, "paste-clipboard", FALSE);
6378 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
6379 gtk_widget_set_sensitive (menuitem, FALSE);
6380 gtk_widget_show (menuitem);
6381 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6383 menuitem = gtk_separator_menu_item_new ();
6384 gtk_widget_show (menuitem);
6385 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6387 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
6388 g_signal_connect_swapped (menuitem, "activate",
6389 G_CALLBACK (gtk_label_select_all), label);
6390 gtk_widget_show (menuitem);
6391 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6394 g_signal_emit (label, signals[POPULATE_POPUP], 0, menu);
6397 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6399 event->button, event->time);
6402 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6403 popup_position_func, label,
6404 0, gtk_get_current_event_time ());
6405 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
6410 gtk_label_clear_links (GtkLabel *label)
6412 GtkLabelPrivate *priv = label->priv;
6414 if (!priv->select_info)
6417 g_list_foreach (priv->select_info->links, (GFunc)link_free, NULL);
6418 g_list_free (priv->select_info->links);
6419 priv->select_info->links = NULL;
6420 priv->select_info->active_link = NULL;
6424 gtk_label_rescan_links (GtkLabel *label)
6426 GtkLabelPrivate *priv = label->priv;
6427 PangoLayout *layout = priv->layout;
6428 PangoAttrList *attlist;
6429 PangoAttrIterator *iter;
6432 if (!priv->select_info || !priv->select_info->links)
6435 attlist = pango_layout_get_attributes (layout);
6437 if (attlist == NULL)
6440 iter = pango_attr_list_get_iterator (attlist);
6442 links = priv->select_info->links;
6446 PangoAttribute *underline;
6447 PangoAttribute *color;
6449 underline = pango_attr_iterator_get (iter, PANGO_ATTR_UNDERLINE);
6450 color = pango_attr_iterator_get (iter, PANGO_ATTR_FOREGROUND);
6452 if (underline != NULL && color != NULL)
6455 PangoRectangle start_pos;
6456 PangoRectangle end_pos;
6459 pango_attr_iterator_range (iter, &start, &end);
6460 pango_layout_index_to_pos (layout, start, &start_pos);
6461 pango_layout_index_to_pos (layout, end, &end_pos);
6465 g_warning ("Ran out of links");
6469 links = links->next;
6470 link->start = start;
6473 } while (pango_attr_iterator_next (iter));
6475 pango_attr_iterator_destroy (iter);
6479 gtk_label_activate_link (GtkLabel *label,
6482 GtkWidget *widget = GTK_WIDGET (label);
6483 GError *error = NULL;
6485 if (!gtk_show_uri (gtk_widget_get_screen (widget),
6486 uri, gtk_get_current_event_time (), &error))
6488 g_warning ("Unable to show '%s': %s", uri, error->message);
6489 g_error_free (error);
6496 emit_activate_link (GtkLabel *label,
6499 GtkLabelPrivate *priv = label->priv;
6502 g_signal_emit (label, signals[ACTIVATE_LINK], 0, link->uri, &handled);
6503 if (handled && priv->track_links && !link->visited)
6505 link->visited = TRUE;
6506 /* FIXME: shouldn't have to redo everything here */
6507 gtk_label_recalculate (label);
6512 gtk_label_activate_current_link (GtkLabel *label)
6515 GtkWidget *widget = GTK_WIDGET (label);
6517 link = gtk_label_get_focus_link (label);
6521 emit_activate_link (label, link);
6525 GtkWidget *toplevel;
6527 GtkWidget *default_widget, *focus_widget;
6529 toplevel = gtk_widget_get_toplevel (widget);
6530 if (GTK_IS_WINDOW (toplevel))
6532 window = GTK_WINDOW (toplevel);
6536 default_widget = gtk_window_get_default_widget (window);
6537 focus_widget = gtk_window_get_focus (window);
6539 if (default_widget != widget &&
6540 !(widget == focus_widget && (!default_widget || !gtk_widget_is_sensitive (default_widget))))
6541 gtk_window_activate_default (window);
6547 static GtkLabelLink *
6548 gtk_label_get_current_link (GtkLabel *label)
6550 GtkLabelPrivate *priv = label->priv;
6553 if (!priv->select_info)
6556 if (priv->select_info->link_clicked)
6557 link = priv->select_info->active_link;
6559 link = gtk_label_get_focus_link (label);
6565 * gtk_label_get_current_uri:
6566 * @label: a #GtkLabel
6568 * Returns the URI for the currently active link in the label.
6569 * The active link is the one under the mouse pointer or, in a
6570 * selectable label, the link in which the text cursor is currently
6573 * This function is intended for use in a #GtkLabel::activate-link handler
6574 * or for use in a #GtkWidget::query-tooltip handler.
6576 * Returns: the currently active URI. The string is owned by GTK+ and must
6577 * not be freed or modified.
6581 G_CONST_RETURN gchar *
6582 gtk_label_get_current_uri (GtkLabel *label)
6586 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
6588 link = gtk_label_get_current_link (label);
6597 * gtk_label_set_track_visited_links:
6598 * @label: a #GtkLabel
6599 * @track_links: %TRUE to track visited links
6601 * Sets whether the label should keep track of clicked
6602 * links (and use a different color for them).
6607 gtk_label_set_track_visited_links (GtkLabel *label,
6608 gboolean track_links)
6610 GtkLabelPrivate *priv;
6612 g_return_if_fail (GTK_IS_LABEL (label));
6616 track_links = track_links != FALSE;
6618 if (priv->track_links != track_links)
6620 priv->track_links = track_links;
6622 /* FIXME: shouldn't have to redo everything here */
6623 gtk_label_recalculate (label);
6625 g_object_notify (G_OBJECT (label), "track-visited-links");
6630 * gtk_label_get_track_visited_links:
6631 * @label: a #GtkLabel
6633 * Returns whether the label is currently keeping track
6636 * Returns: %TRUE if clicked links are remembered
6641 gtk_label_get_track_visited_links (GtkLabel *label)
6643 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
6645 return label->priv->track_links;
6649 gtk_label_query_tooltip (GtkWidget *widget,
6652 gboolean keyboard_tip,
6653 GtkTooltip *tooltip)
6655 GtkLabel *label = GTK_LABEL (widget);
6656 GtkLabelPrivate *priv = label->priv;
6657 GtkLabelSelectionInfo *info = priv->select_info;
6661 if (info && info->links)
6665 if (info->selection_anchor == info->selection_end)
6666 index = info->selection_anchor;
6670 if (!get_layout_index (label, x, y, &index))
6676 for (l = info->links; l != NULL; l = l->next)
6678 GtkLabelLink *link = l->data;
6679 if (index >= link->start && index <= link->end)
6683 gtk_tooltip_set_markup (tooltip, link->title);
6692 return GTK_WIDGET_CLASS (gtk_label_parent_class)->query_tooltip (widget,