1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
21 * file for a list of people on the GTK+ Team. See the ChangeLog
22 * files for a list of changes. These files are distributed with
23 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
32 #include "gtkaccellabel.h"
35 #include "gtkmarshalers.h"
36 #include "gtkwindow.h"
37 #include "gdk/gdkkeysyms.h"
38 #include "gtkclipboard.h"
39 #include "gtkimagemenuitem.h"
41 #include "gtkseparatormenuitem.h"
42 #include "gtktextutil.h"
43 #include "gtkmenuitem.h"
44 #include "gtknotebook.h"
46 #include "gtkbindings.h"
47 #include "gtkbuildable.h"
50 #include "gtktooltip.h"
51 #include "gtkprivate.h"
54 #define GTK_LABEL_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_LABEL, GtkLabelPrivate))
63 /* Notes about the handling of links:
65 * Links share the GtkLabelSelectionInfo struct with selectable labels.
66 * There are some new fields for links. The links field contains the list
67 * of GtkLabelLink structs that describe the links which are embedded in
68 * the label. The active_link field points to the link under the mouse
69 * pointer. For keyboard navigation, the 'focus' link is determined by
70 * finding the link which contains the selection_anchor position.
71 * The link_clicked field is used with button press and release events
72 * to ensure that pressing inside a link and releasing outside of it
73 * does not activate the link.
75 * Links are rendered with the link-color/visited-link-color colors
76 * that are determined by the style and with an underline. When the mouse
77 * pointer is over a link, the pointer is changed to indicate the link,
78 * and the background behind the link is rendered with the base[PRELIGHT]
79 * color. While a button is pressed over a link, the background is rendered
80 * with the base[ACTIVE] color.
82 * Labels with links accept keyboard focus, and it is possible to move
83 * the focus between the embedded links using Tab/Shift-Tab. The focus
84 * is indicated by a focus rectangle that is drawn around the link text.
85 * Pressing Enter activates the focussed link, and there is a suitable
86 * context menu for links that can be opened with the Menu key. Pressing
87 * Control-C copies the link URI to the clipboard.
89 * In selectable labels with links, link functionality is only available
90 * when the selection is empty.
95 gchar *title; /* the title attribute, used as tooltip */
96 gboolean visited; /* get set when the link is activated; this flag
97 * gets preserved over later set_markup() calls
99 gint start; /* position of the link in the PangoLayout */
103 struct _GtkLabelSelectionInfo
106 gint selection_anchor;
108 GtkWidget *popup_menu;
111 GtkLabelLink *active_link;
117 guint select_words : 1;
118 guint selectable : 1;
119 guint link_clicked : 1;
127 ACTIVATE_CURRENT_LINK,
142 PROP_MNEMONIC_KEYVAL,
143 PROP_MNEMONIC_WIDGET,
144 PROP_CURSOR_POSITION,
145 PROP_SELECTION_BOUND,
148 PROP_SINGLE_LINE_MODE,
153 static guint signals[LAST_SIGNAL] = { 0 };
155 static const GdkColor default_link_color = { 0, 0, 0, 0xeeee };
156 static const GdkColor default_visited_link_color = { 0, 0x5555, 0x1a1a, 0x8b8b };
158 static void gtk_label_set_property (GObject *object,
162 static void gtk_label_get_property (GObject *object,
166 static void gtk_label_destroy (GtkObject *object);
167 static void gtk_label_finalize (GObject *object);
168 static void gtk_label_size_request (GtkWidget *widget,
169 GtkRequisition *requisition);
170 static void gtk_label_size_allocate (GtkWidget *widget,
171 GtkAllocation *allocation);
172 static void gtk_label_state_changed (GtkWidget *widget,
174 static void gtk_label_style_set (GtkWidget *widget,
175 GtkStyle *previous_style);
176 static void gtk_label_direction_changed (GtkWidget *widget,
177 GtkTextDirection previous_dir);
178 static gint gtk_label_expose (GtkWidget *widget,
179 GdkEventExpose *event);
180 static gboolean gtk_label_focus (GtkWidget *widget,
181 GtkDirectionType direction);
183 static void gtk_label_realize (GtkWidget *widget);
184 static void gtk_label_unrealize (GtkWidget *widget);
185 static void gtk_label_map (GtkWidget *widget);
186 static void gtk_label_unmap (GtkWidget *widget);
188 static gboolean gtk_label_button_press (GtkWidget *widget,
189 GdkEventButton *event);
190 static gboolean gtk_label_button_release (GtkWidget *widget,
191 GdkEventButton *event);
192 static gboolean gtk_label_motion (GtkWidget *widget,
193 GdkEventMotion *event);
194 static gboolean gtk_label_leave_notify (GtkWidget *widget,
195 GdkEventCrossing *event);
197 static void gtk_label_grab_focus (GtkWidget *widget);
199 static gboolean gtk_label_query_tooltip (GtkWidget *widget,
202 gboolean keyboard_tip,
203 GtkTooltip *tooltip);
205 static void gtk_label_set_text_internal (GtkLabel *label,
207 static void gtk_label_set_label_internal (GtkLabel *label,
209 static void gtk_label_set_use_markup_internal (GtkLabel *label,
211 static void gtk_label_set_use_underline_internal (GtkLabel *label,
213 static void gtk_label_set_attributes_internal (GtkLabel *label,
214 PangoAttrList *attrs);
215 static void gtk_label_set_uline_text_internal (GtkLabel *label,
217 static void gtk_label_set_pattern_internal (GtkLabel *label,
218 const gchar *pattern);
219 static void gtk_label_set_markup_internal (GtkLabel *label,
221 gboolean with_uline);
222 static void gtk_label_recalculate (GtkLabel *label);
223 static void gtk_label_hierarchy_changed (GtkWidget *widget,
224 GtkWidget *old_toplevel);
225 static void gtk_label_screen_changed (GtkWidget *widget,
226 GdkScreen *old_screen);
227 static gboolean gtk_label_popup_menu (GtkWidget *widget);
229 static void gtk_label_create_window (GtkLabel *label);
230 static void gtk_label_destroy_window (GtkLabel *label);
231 static void gtk_label_ensure_select_info (GtkLabel *label);
232 static void gtk_label_clear_select_info (GtkLabel *label);
233 static void gtk_label_update_cursor (GtkLabel *label);
234 static void gtk_label_clear_layout (GtkLabel *label);
235 static void gtk_label_ensure_layout (GtkLabel *label);
236 static void gtk_label_invalidate_wrap_width (GtkLabel *label);
237 static void gtk_label_select_region_index (GtkLabel *label,
241 static gboolean gtk_label_mnemonic_activate (GtkWidget *widget,
242 gboolean group_cycling);
243 static void gtk_label_setup_mnemonic (GtkLabel *label,
245 static void gtk_label_drag_data_get (GtkWidget *widget,
246 GdkDragContext *context,
247 GtkSelectionData *selection_data,
251 static void gtk_label_buildable_interface_init (GtkBuildableIface *iface);
252 static gboolean gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
255 const gchar *tagname,
256 GMarkupParser *parser,
259 static void gtk_label_buildable_custom_finished (GtkBuildable *buildable,
262 const gchar *tagname,
266 /* For selectable labels: */
267 static void gtk_label_move_cursor (GtkLabel *label,
268 GtkMovementStep step,
270 gboolean extend_selection);
271 static void gtk_label_copy_clipboard (GtkLabel *label);
272 static void gtk_label_select_all (GtkLabel *label);
273 static void gtk_label_do_popup (GtkLabel *label,
274 GdkEventButton *event);
275 static gint gtk_label_move_forward_word (GtkLabel *label,
277 static gint gtk_label_move_backward_word (GtkLabel *label,
281 static void gtk_label_rescan_links (GtkLabel *label);
282 static void gtk_label_clear_links (GtkLabel *label);
283 static gboolean gtk_label_activate_link (GtkLabel *label,
285 static void gtk_label_activate_current_link (GtkLabel *label);
286 static GtkLabelLink *gtk_label_get_current_link (GtkLabel *label);
287 static void gtk_label_get_link_colors (GtkWidget *widget,
288 GdkColor **link_color,
289 GdkColor **visited_link_color);
290 static void emit_activate_link (GtkLabel *label,
293 static GQuark quark_angle = 0;
295 static GtkBuildableIface *buildable_parent_iface = NULL;
297 G_DEFINE_TYPE_WITH_CODE (GtkLabel, gtk_label, GTK_TYPE_MISC,
298 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
299 gtk_label_buildable_interface_init));
302 add_move_binding (GtkBindingSet *binding_set,
305 GtkMovementStep step,
308 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
310 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
314 G_TYPE_BOOLEAN, FALSE);
316 /* Selection-extending version */
317 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
321 G_TYPE_BOOLEAN, TRUE);
325 gtk_label_class_init (GtkLabelClass *class)
327 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
328 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
329 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
330 GtkBindingSet *binding_set;
332 quark_angle = g_quark_from_static_string ("angle");
334 gobject_class->set_property = gtk_label_set_property;
335 gobject_class->get_property = gtk_label_get_property;
336 gobject_class->finalize = gtk_label_finalize;
338 object_class->destroy = gtk_label_destroy;
340 widget_class->size_request = gtk_label_size_request;
341 widget_class->size_allocate = gtk_label_size_allocate;
342 widget_class->state_changed = gtk_label_state_changed;
343 widget_class->style_set = gtk_label_style_set;
344 widget_class->query_tooltip = gtk_label_query_tooltip;
345 widget_class->direction_changed = gtk_label_direction_changed;
346 widget_class->expose_event = gtk_label_expose;
347 widget_class->realize = gtk_label_realize;
348 widget_class->unrealize = gtk_label_unrealize;
349 widget_class->map = gtk_label_map;
350 widget_class->unmap = gtk_label_unmap;
351 widget_class->button_press_event = gtk_label_button_press;
352 widget_class->button_release_event = gtk_label_button_release;
353 widget_class->motion_notify_event = gtk_label_motion;
354 widget_class->leave_notify_event = gtk_label_leave_notify;
355 widget_class->hierarchy_changed = gtk_label_hierarchy_changed;
356 widget_class->screen_changed = gtk_label_screen_changed;
357 widget_class->mnemonic_activate = gtk_label_mnemonic_activate;
358 widget_class->drag_data_get = gtk_label_drag_data_get;
359 widget_class->grab_focus = gtk_label_grab_focus;
360 widget_class->popup_menu = gtk_label_popup_menu;
361 widget_class->focus = gtk_label_focus;
363 class->move_cursor = gtk_label_move_cursor;
364 class->copy_clipboard = gtk_label_copy_clipboard;
365 class->activate_link = gtk_label_activate_link;
368 * GtkLabel::move-cursor:
369 * @entry: the object which received the signal
370 * @step: the granularity of the move, as a #GtkMovementStep
371 * @count: the number of @step units to move
372 * @extend_selection: %TRUE if the move should extend the selection
374 * The ::move-cursor signal is a
375 * <link linkend="keybinding-signals">keybinding signal</link>
376 * which gets emitted when the user initiates a cursor movement.
377 * If the cursor is not visible in @entry, this signal causes
378 * the viewport to be moved instead.
380 * Applications should not connect to it, but may emit it with
381 * g_signal_emit_by_name() if they need to control the cursor
384 * The default bindings for this signal come in two variants,
385 * the variant with the Shift modifier extends the selection,
386 * the variant without the Shift modifer does not.
387 * There are too many key combinations to list them all here.
389 * <listitem>Arrow keys move by individual characters/lines</listitem>
390 * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
391 * <listitem>Home/End keys move to the ends of the buffer</listitem>
394 signals[MOVE_CURSOR] =
395 g_signal_new (I_("move-cursor"),
396 G_OBJECT_CLASS_TYPE (gobject_class),
397 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
398 G_STRUCT_OFFSET (GtkLabelClass, move_cursor),
400 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
402 GTK_TYPE_MOVEMENT_STEP,
407 * GtkLabel::copy-clipboard:
408 * @label: the object which received the signal
410 * The ::copy-clipboard signal is a
411 * <link linkend="keybinding-signals">keybinding signal</link>
412 * which gets emitted to copy the selection to the clipboard.
414 * The default binding for this signal is Ctrl-c.
416 signals[COPY_CLIPBOARD] =
417 g_signal_new (I_("copy-clipboard"),
418 G_OBJECT_CLASS_TYPE (gobject_class),
419 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
420 G_STRUCT_OFFSET (GtkLabelClass, copy_clipboard),
422 _gtk_marshal_VOID__VOID,
426 * GtkLabel::populate-popup:
427 * @label: The label on which the signal is emitted
428 * @menu: the menu that is being populated
430 * The ::populate-popup signal gets emitted before showing the
431 * context menu of the label. Note that only selectable labels
432 * have context menus.
434 * If you need to add items to the context menu, connect
435 * to this signal and append your menuitems to the @menu.
437 signals[POPULATE_POPUP] =
438 g_signal_new (I_("populate-popup"),
439 G_OBJECT_CLASS_TYPE (gobject_class),
441 G_STRUCT_OFFSET (GtkLabelClass, populate_popup),
443 _gtk_marshal_VOID__OBJECT,
448 * GtkLabel::activate-current-link:
449 * @label: The label on which the signal was emitted
451 * A <link linkend="keybinding-signals">keybinding signal</link>
452 * which gets emitted when the user activates a link in the label.
454 * Applications may also emit the signal with g_signal_emit_by_name()
455 * if they need to control activation of URIs programmatically.
457 * The default bindings for this signal are all forms of the Enter key.
461 signals[ACTIVATE_CURRENT_LINK] =
462 g_signal_new_class_handler ("activate-current-link",
463 G_TYPE_FROM_CLASS (object_class),
464 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
465 G_CALLBACK (gtk_label_activate_current_link),
467 _gtk_marshal_VOID__VOID,
471 * GtkLabel::activate-link:
472 * @label: The label on which the signal was emitted
473 * @uri: the URI that is activated
475 * The signal which gets emitted to activate a URI.
476 * Applications may connect to it to override the default behaviour,
477 * which is to call gtk_show_uri().
479 * Returns: %TRUE if the link has been activated
483 signals[ACTIVATE_LINK] =
484 g_signal_new ("activate-link",
485 G_TYPE_FROM_CLASS (object_class),
487 G_STRUCT_OFFSET (GtkLabelClass, activate_link),
488 _gtk_boolean_handled_accumulator, NULL,
489 _gtk_marshal_BOOLEAN__STRING,
490 G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
492 g_object_class_install_property (gobject_class,
494 g_param_spec_string ("label",
496 P_("The text of the label"),
498 GTK_PARAM_READWRITE));
499 g_object_class_install_property (gobject_class,
501 g_param_spec_boxed ("attributes",
503 P_("A list of style attributes to apply to the text of the label"),
504 PANGO_TYPE_ATTR_LIST,
505 GTK_PARAM_READWRITE));
506 g_object_class_install_property (gobject_class,
508 g_param_spec_boolean ("use-markup",
510 P_("The text of the label includes XML markup. See pango_parse_markup()"),
512 GTK_PARAM_READWRITE));
513 g_object_class_install_property (gobject_class,
515 g_param_spec_boolean ("use-underline",
517 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
519 GTK_PARAM_READWRITE));
521 g_object_class_install_property (gobject_class,
523 g_param_spec_enum ("justify",
525 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"),
526 GTK_TYPE_JUSTIFICATION,
528 GTK_PARAM_READWRITE));
530 g_object_class_install_property (gobject_class,
532 g_param_spec_string ("pattern",
534 P_("A string with _ characters in positions correspond to characters in the text to underline"),
536 GTK_PARAM_WRITABLE));
538 g_object_class_install_property (gobject_class,
540 g_param_spec_boolean ("wrap",
542 P_("If set, wrap lines if the text becomes too wide"),
544 GTK_PARAM_READWRITE));
546 * GtkLabel:wrap-mode:
548 * If line wrapping is on (see the #GtkLabel:wrap property) this controls
549 * how the line wrapping is done. The default is %PANGO_WRAP_WORD, which
550 * means wrap on word boundaries.
554 g_object_class_install_property (gobject_class,
556 g_param_spec_enum ("wrap-mode",
557 P_("Line wrap mode"),
558 P_("If wrap is set, controls how linewrapping is done"),
559 PANGO_TYPE_WRAP_MODE,
561 GTK_PARAM_READWRITE));
562 g_object_class_install_property (gobject_class,
564 g_param_spec_boolean ("selectable",
566 P_("Whether the label text can be selected with the mouse"),
568 GTK_PARAM_READWRITE));
569 g_object_class_install_property (gobject_class,
570 PROP_MNEMONIC_KEYVAL,
571 g_param_spec_uint ("mnemonic-keyval",
573 P_("The mnemonic accelerator key for this label"),
577 GTK_PARAM_READABLE));
578 g_object_class_install_property (gobject_class,
579 PROP_MNEMONIC_WIDGET,
580 g_param_spec_object ("mnemonic-widget",
581 P_("Mnemonic widget"),
582 P_("The widget to be activated when the label's mnemonic "
585 GTK_PARAM_READWRITE));
587 g_object_class_install_property (gobject_class,
588 PROP_CURSOR_POSITION,
589 g_param_spec_int ("cursor-position",
590 P_("Cursor Position"),
591 P_("The current position of the insertion cursor in chars"),
595 GTK_PARAM_READABLE));
597 g_object_class_install_property (gobject_class,
598 PROP_SELECTION_BOUND,
599 g_param_spec_int ("selection-bound",
600 P_("Selection Bound"),
601 P_("The position of the opposite end of the selection from the cursor in chars"),
605 GTK_PARAM_READABLE));
608 * GtkLabel:ellipsize:
610 * The preferred place to ellipsize the string, if the label does
611 * not have enough room to display the entire string, specified as a
612 * #PangoEllisizeMode.
614 * Note that setting this property to a value other than
615 * %PANGO_ELLIPSIZE_NONE has the side-effect that the label requests
616 * only enough space to display the ellipsis "...". In particular, this
617 * means that ellipsizing labels do not work well in notebook tabs, unless
618 * the tab's #GtkNotebook:tab-expand property is set to %TRUE. Other ways
619 * to set a label's width are gtk_widget_set_size_request() and
620 * gtk_label_set_width_chars().
624 g_object_class_install_property (gobject_class,
626 g_param_spec_enum ("ellipsize",
628 P_("The preferred place to ellipsize the string, if the label does not have enough room to display the entire string"),
629 PANGO_TYPE_ELLIPSIZE_MODE,
630 PANGO_ELLIPSIZE_NONE,
631 GTK_PARAM_READWRITE));
634 * GtkLabel:width-chars:
636 * The desired width of the label, in characters. If this property is set to
637 * -1, the width will be calculated automatically, otherwise the label will
638 * request either 3 characters or the property value, whichever is greater.
639 * If the "width-chars" property is set to a positive value, then the
640 * #GtkLabel:max-width-chars property is ignored.
644 g_object_class_install_property (gobject_class,
646 g_param_spec_int ("width-chars",
647 P_("Width In Characters"),
648 P_("The desired width of the label, in characters"),
652 GTK_PARAM_READWRITE));
655 * GtkLabel:single-line-mode:
657 * Whether the label is in single line mode. In single line mode,
658 * the height of the label does not depend on the actual text, it
659 * is always set to ascent + descent of the font. This can be an
660 * advantage in situations where resizing the label because of text
661 * changes would be distracting, e.g. in a statusbar.
665 g_object_class_install_property (gobject_class,
666 PROP_SINGLE_LINE_MODE,
667 g_param_spec_boolean ("single-line-mode",
668 P_("Single Line Mode"),
669 P_("Whether the label is in single line mode"),
671 GTK_PARAM_READWRITE));
676 * The angle that the baseline of the label makes with the horizontal,
677 * in degrees, measured counterclockwise. An angle of 90 reads from
678 * from bottom to top, an angle of 270, from top to bottom. Ignored
679 * if the label is selectable, wrapped, or ellipsized.
683 g_object_class_install_property (gobject_class,
685 g_param_spec_double ("angle",
687 P_("Angle at which the label is rotated"),
691 GTK_PARAM_READWRITE));
694 * GtkLabel:max-width-chars:
696 * The desired maximum width of the label, in characters. If this property
697 * is set to -1, the width will be calculated automatically, otherwise the
698 * label will request space for no more than the requested number of
699 * characters. If the #GtkLabel:width-chars property is set to a positive
700 * value, then the "max-width-chars" property is ignored.
704 g_object_class_install_property (gobject_class,
705 PROP_MAX_WIDTH_CHARS,
706 g_param_spec_int ("max-width-chars",
707 P_("Maximum Width In Characters"),
708 P_("The desired maximum width of the label, in characters"),
712 GTK_PARAM_READWRITE));
717 binding_set = gtk_binding_set_by_class (class);
719 /* Moving the insertion point */
720 add_move_binding (binding_set, GDK_Right, 0,
721 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
723 add_move_binding (binding_set, GDK_Left, 0,
724 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
726 add_move_binding (binding_set, GDK_KP_Right, 0,
727 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
729 add_move_binding (binding_set, GDK_KP_Left, 0,
730 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
732 add_move_binding (binding_set, GDK_f, GDK_CONTROL_MASK,
733 GTK_MOVEMENT_LOGICAL_POSITIONS, 1);
735 add_move_binding (binding_set, GDK_b, GDK_CONTROL_MASK,
736 GTK_MOVEMENT_LOGICAL_POSITIONS, -1);
738 add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
739 GTK_MOVEMENT_WORDS, 1);
741 add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
742 GTK_MOVEMENT_WORDS, -1);
744 add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
745 GTK_MOVEMENT_WORDS, 1);
747 add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
748 GTK_MOVEMENT_WORDS, -1);
751 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
753 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
755 G_TYPE_BOOLEAN, FALSE);
757 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
759 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
761 G_TYPE_BOOLEAN, TRUE);
763 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
765 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
767 G_TYPE_BOOLEAN, FALSE);
769 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
771 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
773 G_TYPE_BOOLEAN, TRUE);
776 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
778 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
780 G_TYPE_BOOLEAN, FALSE);
782 gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_CONTROL_MASK,
784 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
786 G_TYPE_BOOLEAN, FALSE);
788 add_move_binding (binding_set, GDK_f, GDK_MOD1_MASK,
789 GTK_MOVEMENT_WORDS, 1);
791 add_move_binding (binding_set, GDK_b, GDK_MOD1_MASK,
792 GTK_MOVEMENT_WORDS, -1);
794 add_move_binding (binding_set, GDK_Home, 0,
795 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
797 add_move_binding (binding_set, GDK_End, 0,
798 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
800 add_move_binding (binding_set, GDK_KP_Home, 0,
801 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
803 add_move_binding (binding_set, GDK_KP_End, 0,
804 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
806 add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
807 GTK_MOVEMENT_BUFFER_ENDS, -1);
809 add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
810 GTK_MOVEMENT_BUFFER_ENDS, 1);
812 add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
813 GTK_MOVEMENT_BUFFER_ENDS, -1);
815 add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
816 GTK_MOVEMENT_BUFFER_ENDS, 1);
819 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
820 "copy-clipboard", 0);
822 gtk_binding_entry_add_signal (binding_set, GDK_Return, 0,
823 "activate-current-link", 0);
824 gtk_binding_entry_add_signal (binding_set, GDK_ISO_Enter, 0,
825 "activate-current-link", 0);
826 gtk_binding_entry_add_signal (binding_set, GDK_KP_Enter, 0,
827 "activate-current-link", 0);
829 gtk_settings_install_property (g_param_spec_boolean ("gtk-label-select-on-focus",
830 P_("Select on focus"),
831 P_("Whether to select the contents of a selectable label when it is focused"),
833 GTK_PARAM_READWRITE));
836 g_type_class_add_private (class, sizeof (GtkLabelPrivate));
840 gtk_label_set_property (GObject *object,
847 label = GTK_LABEL (object);
852 gtk_label_set_label (label, g_value_get_string (value));
854 case PROP_ATTRIBUTES:
855 gtk_label_set_attributes (label, g_value_get_boxed (value));
857 case PROP_USE_MARKUP:
858 gtk_label_set_use_markup (label, g_value_get_boolean (value));
860 case PROP_USE_UNDERLINE:
861 gtk_label_set_use_underline (label, g_value_get_boolean (value));
864 gtk_label_set_justify (label, g_value_get_enum (value));
867 gtk_label_set_pattern (label, g_value_get_string (value));
870 gtk_label_set_line_wrap (label, g_value_get_boolean (value));
873 gtk_label_set_line_wrap_mode (label, g_value_get_enum (value));
875 case PROP_SELECTABLE:
876 gtk_label_set_selectable (label, g_value_get_boolean (value));
878 case PROP_MNEMONIC_WIDGET:
879 gtk_label_set_mnemonic_widget (label, (GtkWidget*) g_value_get_object (value));
882 gtk_label_set_ellipsize (label, g_value_get_enum (value));
884 case PROP_WIDTH_CHARS:
885 gtk_label_set_width_chars (label, g_value_get_int (value));
887 case PROP_SINGLE_LINE_MODE:
888 gtk_label_set_single_line_mode (label, g_value_get_boolean (value));
891 gtk_label_set_angle (label, g_value_get_double (value));
893 case PROP_MAX_WIDTH_CHARS:
894 gtk_label_set_max_width_chars (label, g_value_get_int (value));
897 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
903 gtk_label_get_property (GObject *object,
910 label = GTK_LABEL (object);
915 g_value_set_string (value, label->label);
917 case PROP_ATTRIBUTES:
918 g_value_set_boxed (value, label->attrs);
920 case PROP_USE_MARKUP:
921 g_value_set_boolean (value, label->use_markup);
923 case PROP_USE_UNDERLINE:
924 g_value_set_boolean (value, label->use_underline);
927 g_value_set_enum (value, label->jtype);
930 g_value_set_boolean (value, label->wrap);
933 g_value_set_enum (value, label->wrap_mode);
935 case PROP_SELECTABLE:
936 g_value_set_boolean (value, gtk_label_get_selectable (label));
938 case PROP_MNEMONIC_KEYVAL:
939 g_value_set_uint (value, label->mnemonic_keyval);
941 case PROP_MNEMONIC_WIDGET:
942 g_value_set_object (value, (GObject*) label->mnemonic_widget);
944 case PROP_CURSOR_POSITION:
945 if (label->select_info && label->select_info->selectable)
947 gint offset = g_utf8_pointer_to_offset (label->text,
948 label->text + label->select_info->selection_end);
949 g_value_set_int (value, offset);
952 g_value_set_int (value, 0);
954 case PROP_SELECTION_BOUND:
955 if (label->select_info && label->select_info->selectable)
957 gint offset = g_utf8_pointer_to_offset (label->text,
958 label->text + label->select_info->selection_anchor);
959 g_value_set_int (value, offset);
962 g_value_set_int (value, 0);
965 g_value_set_enum (value, label->ellipsize);
967 case PROP_WIDTH_CHARS:
968 g_value_set_int (value, gtk_label_get_width_chars (label));
970 case PROP_SINGLE_LINE_MODE:
971 g_value_set_boolean (value, gtk_label_get_single_line_mode (label));
974 g_value_set_double (value, gtk_label_get_angle (label));
976 case PROP_MAX_WIDTH_CHARS:
977 g_value_set_int (value, gtk_label_get_max_width_chars (label));
981 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
987 gtk_label_init (GtkLabel *label)
989 GtkLabelPrivate *priv;
991 GTK_WIDGET_SET_FLAGS (label, GTK_NO_WINDOW);
993 priv = GTK_LABEL_GET_PRIVATE (label);
994 priv->width_chars = -1;
995 priv->max_width_chars = -1;
996 priv->wrap_width = -1;
999 label->jtype = GTK_JUSTIFY_LEFT;
1000 label->wrap = FALSE;
1001 label->wrap_mode = PANGO_WRAP_WORD;
1002 label->ellipsize = PANGO_ELLIPSIZE_NONE;
1004 label->use_underline = FALSE;
1005 label->use_markup = FALSE;
1006 label->pattern_set = FALSE;
1008 label->mnemonic_keyval = GDK_VoidSymbol;
1009 label->layout = NULL;
1011 label->attrs = NULL;
1013 label->mnemonic_widget = NULL;
1014 label->mnemonic_window = NULL;
1016 gtk_label_set_text (label, "");
1021 gtk_label_buildable_interface_init (GtkBuildableIface *iface)
1023 buildable_parent_iface = g_type_interface_peek_parent (iface);
1025 iface->custom_tag_start = gtk_label_buildable_custom_tag_start;
1026 iface->custom_finished = gtk_label_buildable_custom_finished;
1030 GtkBuilder *builder;
1032 PangoAttrList *attrs;
1035 static PangoAttribute *
1036 attribute_from_text (GtkBuilder *builder,
1041 PangoAttribute *attribute = NULL;
1043 PangoLanguage *language;
1044 PangoFontDescription *font_desc;
1046 GValue val = { 0, };
1048 if (!gtk_builder_value_from_string_type (builder, PANGO_TYPE_ATTR_TYPE, name, &val, error))
1051 type = g_value_get_enum (&val);
1052 g_value_unset (&val);
1056 /* PangoAttrLanguage */
1057 case PANGO_ATTR_LANGUAGE:
1058 if ((language = pango_language_from_string (value)))
1060 attribute = pango_attr_language_new (language);
1061 g_value_init (&val, G_TYPE_INT);
1065 case PANGO_ATTR_STYLE:
1066 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STYLE, value, &val, error))
1067 attribute = pango_attr_style_new (g_value_get_enum (&val));
1069 case PANGO_ATTR_WEIGHT:
1070 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_WEIGHT, value, &val, error))
1071 attribute = pango_attr_weight_new (g_value_get_enum (&val));
1073 case PANGO_ATTR_VARIANT:
1074 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_VARIANT, value, &val, error))
1075 attribute = pango_attr_variant_new (g_value_get_enum (&val));
1077 case PANGO_ATTR_STRETCH:
1078 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STRETCH, value, &val, error))
1079 attribute = pango_attr_stretch_new (g_value_get_enum (&val));
1081 case PANGO_ATTR_UNDERLINE:
1082 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1083 attribute = pango_attr_underline_new (g_value_get_boolean (&val));
1085 case PANGO_ATTR_STRIKETHROUGH:
1086 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1087 attribute = pango_attr_strikethrough_new (g_value_get_boolean (&val));
1089 case PANGO_ATTR_GRAVITY:
1090 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY, value, &val, error))
1091 attribute = pango_attr_gravity_new (g_value_get_enum (&val));
1093 case PANGO_ATTR_GRAVITY_HINT:
1094 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY_HINT,
1095 value, &val, error))
1096 attribute = pango_attr_gravity_hint_new (g_value_get_enum (&val));
1099 /* PangoAttrString */
1100 case PANGO_ATTR_FAMILY:
1101 attribute = pango_attr_family_new (value);
1102 g_value_init (&val, G_TYPE_INT);
1106 case PANGO_ATTR_SIZE:
1107 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1108 value, &val, error))
1109 attribute = pango_attr_size_new (g_value_get_int (&val));
1111 case PANGO_ATTR_ABSOLUTE_SIZE:
1112 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1113 value, &val, error))
1114 attribute = pango_attr_size_new_absolute (g_value_get_int (&val));
1117 /* PangoAttrFontDesc */
1118 case PANGO_ATTR_FONT_DESC:
1119 if ((font_desc = pango_font_description_from_string (value)))
1121 attribute = pango_attr_font_desc_new (font_desc);
1122 pango_font_description_free (font_desc);
1123 g_value_init (&val, G_TYPE_INT);
1127 /* PangoAttrColor */
1128 case PANGO_ATTR_FOREGROUND:
1129 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1130 value, &val, error))
1132 color = g_value_get_boxed (&val);
1133 attribute = pango_attr_foreground_new (color->red, color->green, color->blue);
1136 case PANGO_ATTR_BACKGROUND:
1137 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1138 value, &val, error))
1140 color = g_value_get_boxed (&val);
1141 attribute = pango_attr_background_new (color->red, color->green, color->blue);
1144 case PANGO_ATTR_UNDERLINE_COLOR:
1145 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1146 value, &val, error))
1148 color = g_value_get_boxed (&val);
1149 attribute = pango_attr_underline_color_new (color->red, color->green, color->blue);
1152 case PANGO_ATTR_STRIKETHROUGH_COLOR:
1153 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1154 value, &val, error))
1156 color = g_value_get_boxed (&val);
1157 attribute = pango_attr_strikethrough_color_new (color->red, color->green, color->blue);
1161 /* PangoAttrShape */
1162 case PANGO_ATTR_SHAPE:
1163 /* Unsupported for now */
1165 /* PangoAttrFloat */
1166 case PANGO_ATTR_SCALE:
1167 if (gtk_builder_value_from_string_type (builder, G_TYPE_DOUBLE,
1168 value, &val, error))
1169 attribute = pango_attr_scale_new (g_value_get_double (&val));
1172 case PANGO_ATTR_INVALID:
1173 case PANGO_ATTR_LETTER_SPACING:
1174 case PANGO_ATTR_RISE:
1175 case PANGO_ATTR_FALLBACK:
1180 g_value_unset (&val);
1187 pango_start_element (GMarkupParseContext *context,
1188 const gchar *element_name,
1189 const gchar **names,
1190 const gchar **values,
1194 PangoParserData *data = (PangoParserData*)user_data;
1195 GValue val = { 0, };
1197 gint line_number, char_number;
1199 if (strcmp (element_name, "attribute") == 0)
1201 PangoAttribute *attr = NULL;
1202 const gchar *name = NULL;
1203 const gchar *value = NULL;
1204 const gchar *start = NULL;
1205 const gchar *end = NULL;
1206 guint start_val = 0;
1207 guint end_val = G_MAXUINT;
1209 for (i = 0; names[i]; i++)
1211 if (strcmp (names[i], "name") == 0)
1213 else if (strcmp (names[i], "value") == 0)
1215 else if (strcmp (names[i], "start") == 0)
1217 else if (strcmp (names[i], "end") == 0)
1221 g_markup_parse_context_get_position (context,
1226 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1227 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1229 line_number, char_number, names[i], "attribute");
1234 if (!name || !value)
1236 g_markup_parse_context_get_position (context,
1241 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1242 "%s:%d:%d <%s> requires attribute \"%s\"",
1244 line_number, char_number, "attribute",
1245 name ? "value" : "name");
1251 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1252 start, &val, error))
1254 start_val = g_value_get_uint (&val);
1255 g_value_unset (&val);
1260 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1263 end_val = g_value_get_uint (&val);
1264 g_value_unset (&val);
1267 attr = attribute_from_text (data->builder, name, value, error);
1268 attr->start_index = start_val;
1269 attr->end_index = end_val;
1274 data->attrs = pango_attr_list_new ();
1276 pango_attr_list_insert (data->attrs, attr);
1279 else if (strcmp (element_name, "attributes") == 0)
1282 g_warning ("Unsupported tag for GtkLabel: %s\n", element_name);
1285 static const GMarkupParser pango_parser =
1287 pango_start_element,
1291 gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
1292 GtkBuilder *builder,
1294 const gchar *tagname,
1295 GMarkupParser *parser,
1298 if (buildable_parent_iface->custom_tag_start (buildable, builder, child,
1299 tagname, parser, data))
1302 if (strcmp (tagname, "attributes") == 0)
1304 PangoParserData *parser_data;
1306 parser_data = g_slice_new0 (PangoParserData);
1307 parser_data->builder = g_object_ref (builder);
1308 parser_data->object = g_object_ref (buildable);
1309 *parser = pango_parser;
1310 *data = parser_data;
1317 gtk_label_buildable_custom_finished (GtkBuildable *buildable,
1318 GtkBuilder *builder,
1320 const gchar *tagname,
1323 PangoParserData *data;
1325 buildable_parent_iface->custom_finished (buildable, builder, child,
1326 tagname, user_data);
1328 if (strcmp (tagname, "attributes") == 0)
1330 data = (PangoParserData*)user_data;
1334 gtk_label_set_attributes (GTK_LABEL (buildable), data->attrs);
1335 pango_attr_list_unref (data->attrs);
1338 g_object_unref (data->object);
1339 g_object_unref (data->builder);
1340 g_slice_free (PangoParserData, data);
1347 * @str: The text of the label
1349 * Creates a new label with the given text inside it. You can
1350 * pass %NULL to get an empty label widget.
1352 * Return value: the new #GtkLabel
1355 gtk_label_new (const gchar *str)
1359 label = g_object_new (GTK_TYPE_LABEL, NULL);
1362 gtk_label_set_text (label, str);
1364 return GTK_WIDGET (label);
1368 * gtk_label_new_with_mnemonic:
1369 * @str: The text of the label, with an underscore in front of the
1370 * mnemonic character
1372 * Creates a new #GtkLabel, containing the text in @str.
1374 * If characters in @str are preceded by an underscore, they are
1375 * underlined. If you need a literal underscore character in a label, use
1376 * '__' (two underscores). The first underlined character represents a
1377 * keyboard accelerator called a mnemonic. The mnemonic key can be used
1378 * to activate another widget, chosen automatically, or explicitly using
1379 * gtk_label_set_mnemonic_widget().
1381 * If gtk_label_set_mnemonic_widget() is not called, then the first
1382 * activatable ancestor of the #GtkLabel will be chosen as the mnemonic
1383 * widget. For instance, if the label is inside a button or menu item,
1384 * the button or menu item will automatically become the mnemonic widget
1385 * and be activated by the mnemonic.
1387 * Return value: the new #GtkLabel
1390 gtk_label_new_with_mnemonic (const gchar *str)
1394 label = g_object_new (GTK_TYPE_LABEL, NULL);
1397 gtk_label_set_text_with_mnemonic (label, str);
1399 return GTK_WIDGET (label);
1403 gtk_label_mnemonic_activate (GtkWidget *widget,
1404 gboolean group_cycling)
1408 if (GTK_LABEL (widget)->mnemonic_widget)
1409 return gtk_widget_mnemonic_activate (GTK_LABEL (widget)->mnemonic_widget, group_cycling);
1411 /* Try to find the widget to activate by traversing the
1412 * widget's ancestry.
1414 parent = widget->parent;
1416 if (GTK_IS_NOTEBOOK (parent))
1421 if (GTK_WIDGET_CAN_FOCUS (parent) ||
1422 (!group_cycling && GTK_WIDGET_GET_CLASS (parent)->activate_signal) ||
1423 GTK_IS_NOTEBOOK (parent->parent) ||
1424 GTK_IS_MENU_ITEM (parent))
1425 return gtk_widget_mnemonic_activate (parent, group_cycling);
1426 parent = parent->parent;
1429 /* barf if there was nothing to activate */
1430 g_warning ("Couldn't find a target for a mnemonic activation.");
1431 gtk_widget_error_bell (widget);
1437 gtk_label_setup_mnemonic (GtkLabel *label,
1440 GtkWidget *widget = GTK_WIDGET (label);
1441 GtkWidget *toplevel;
1442 GtkWidget *mnemonic_menu;
1444 mnemonic_menu = g_object_get_data (G_OBJECT (label), "gtk-mnemonic-menu");
1446 if (last_key != GDK_VoidSymbol)
1448 if (label->mnemonic_window)
1450 gtk_window_remove_mnemonic (label->mnemonic_window,
1453 label->mnemonic_window = NULL;
1457 _gtk_menu_shell_remove_mnemonic (GTK_MENU_SHELL (mnemonic_menu),
1460 mnemonic_menu = NULL;
1464 if (label->mnemonic_keyval == GDK_VoidSymbol)
1467 toplevel = gtk_widget_get_toplevel (widget);
1468 if (GTK_WIDGET_TOPLEVEL (toplevel))
1470 GtkWidget *menu_shell;
1472 menu_shell = gtk_widget_get_ancestor (widget,
1473 GTK_TYPE_MENU_SHELL);
1477 _gtk_menu_shell_add_mnemonic (GTK_MENU_SHELL (menu_shell),
1478 label->mnemonic_keyval,
1480 mnemonic_menu = menu_shell;
1483 if (!GTK_IS_MENU (menu_shell))
1485 gtk_window_add_mnemonic (GTK_WINDOW (toplevel),
1486 label->mnemonic_keyval,
1488 label->mnemonic_window = GTK_WINDOW (toplevel);
1493 g_object_set_data (G_OBJECT (label), I_("gtk-mnemonic-menu"), mnemonic_menu);
1497 gtk_label_hierarchy_changed (GtkWidget *widget,
1498 GtkWidget *old_toplevel)
1500 GtkLabel *label = GTK_LABEL (widget);
1502 gtk_label_setup_mnemonic (label, label->mnemonic_keyval);
1506 label_shortcut_setting_apply (GtkLabel *label)
1508 gtk_label_recalculate (label);
1509 if (GTK_IS_ACCEL_LABEL (label))
1510 gtk_accel_label_refetch (GTK_ACCEL_LABEL (label));
1514 label_shortcut_setting_traverse_container (GtkWidget *widget,
1517 if (GTK_IS_LABEL (widget))
1518 label_shortcut_setting_apply (GTK_LABEL (widget));
1519 else if (GTK_IS_CONTAINER (widget))
1520 gtk_container_forall (GTK_CONTAINER (widget),
1521 label_shortcut_setting_traverse_container, data);
1525 label_shortcut_setting_changed (GtkSettings *settings)
1529 list = gtk_window_list_toplevels ();
1531 for (l = list; l ; l = l->next)
1533 GtkWidget *widget = l->data;
1535 if (gtk_widget_get_settings (widget) == settings)
1536 gtk_container_forall (GTK_CONTAINER (widget),
1537 label_shortcut_setting_traverse_container, NULL);
1544 gtk_label_screen_changed (GtkWidget *widget,
1545 GdkScreen *old_screen)
1547 GtkSettings *settings;
1548 gboolean shortcuts_connected;
1550 if (!gtk_widget_has_screen (widget))
1553 settings = gtk_widget_get_settings (widget);
1555 shortcuts_connected =
1556 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (settings),
1557 "gtk-label-shortcuts-connected"));
1559 if (! shortcuts_connected)
1561 g_signal_connect (settings, "notify::gtk-enable-mnemonics",
1562 G_CALLBACK (label_shortcut_setting_changed),
1564 g_signal_connect (settings, "notify::gtk-enable-accels",
1565 G_CALLBACK (label_shortcut_setting_changed),
1568 g_object_set_data (G_OBJECT (settings), "gtk-label-shortcuts-connected",
1569 GINT_TO_POINTER (TRUE));
1572 label_shortcut_setting_apply (GTK_LABEL (widget));
1577 label_mnemonic_widget_weak_notify (gpointer data,
1578 GObject *where_the_object_was)
1580 GtkLabel *label = data;
1582 label->mnemonic_widget = NULL;
1583 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1587 * gtk_label_set_mnemonic_widget:
1588 * @label: a #GtkLabel
1589 * @widget: the target #GtkWidget
1591 * If the label has been set so that it has an mnemonic key (using
1592 * i.e. gtk_label_set_markup_with_mnemonic(),
1593 * gtk_label_set_text_with_mnemonic(), gtk_label_new_with_mnemonic()
1594 * or the "use_underline" property) the label can be associated with a
1595 * widget that is the target of the mnemonic. When the label is inside
1596 * a widget (like a #GtkButton or a #GtkNotebook tab) it is
1597 * automatically associated with the correct widget, but sometimes
1598 * (i.e. when the target is a #GtkEntry next to the label) you need to
1599 * set it explicitly using this function.
1601 * The target widget will be accelerated by emitting the
1602 * GtkWidget::mnemonic-activate signal on it. The default handler for
1603 * this signal will activate the widget if there are no mnemonic collisions
1604 * and toggle focus between the colliding widgets otherwise.
1607 gtk_label_set_mnemonic_widget (GtkLabel *label,
1610 g_return_if_fail (GTK_IS_LABEL (label));
1612 g_return_if_fail (GTK_IS_WIDGET (widget));
1614 if (label->mnemonic_widget)
1616 gtk_widget_remove_mnemonic_label (label->mnemonic_widget, GTK_WIDGET (label));
1617 g_object_weak_unref (G_OBJECT (label->mnemonic_widget),
1618 label_mnemonic_widget_weak_notify,
1621 label->mnemonic_widget = widget;
1622 if (label->mnemonic_widget)
1624 g_object_weak_ref (G_OBJECT (label->mnemonic_widget),
1625 label_mnemonic_widget_weak_notify,
1627 gtk_widget_add_mnemonic_label (label->mnemonic_widget, GTK_WIDGET (label));
1630 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1634 * gtk_label_get_mnemonic_widget:
1635 * @label: a #GtkLabel
1637 * Retrieves the target of the mnemonic (keyboard shortcut) of this
1638 * label. See gtk_label_set_mnemonic_widget().
1640 * Return value: the target of the label's mnemonic, or %NULL if none
1641 * has been set and the default algorithm will be used.
1644 gtk_label_get_mnemonic_widget (GtkLabel *label)
1646 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1648 return label->mnemonic_widget;
1652 * gtk_label_get_mnemonic_keyval:
1653 * @label: a #GtkLabel
1655 * If the label has been set so that it has an mnemonic key this function
1656 * returns the keyval used for the mnemonic accelerator. If there is no
1657 * mnemonic set up it returns #GDK_VoidSymbol.
1659 * Returns: GDK keyval usable for accelerators, or #GDK_VoidSymbol
1662 gtk_label_get_mnemonic_keyval (GtkLabel *label)
1664 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_VoidSymbol);
1666 return label->mnemonic_keyval;
1670 gtk_label_set_text_internal (GtkLabel *label,
1673 g_free (label->text);
1677 gtk_label_select_region_index (label, 0, 0);
1681 gtk_label_set_label_internal (GtkLabel *label,
1684 g_free (label->label);
1688 g_object_notify (G_OBJECT (label), "label");
1692 gtk_label_set_use_markup_internal (GtkLabel *label,
1696 if (label->use_markup != val)
1698 label->use_markup = val;
1700 g_object_notify (G_OBJECT (label), "use-markup");
1705 gtk_label_set_use_underline_internal (GtkLabel *label,
1709 if (label->use_underline != val)
1711 label->use_underline = val;
1713 g_object_notify (G_OBJECT (label), "use-underline");
1718 gtk_label_compose_effective_attrs (GtkLabel *label)
1720 PangoAttrIterator *iter;
1721 PangoAttribute *attr;
1722 GSList *iter_attrs, *l;
1726 if (label->effective_attrs)
1728 if ((iter = pango_attr_list_get_iterator (label->attrs)))
1731 iter_attrs = pango_attr_iterator_get_attrs (iter);
1732 for (l = iter_attrs; l; l = l->next)
1735 pango_attr_list_insert (label->effective_attrs, attr);
1737 g_slist_free (iter_attrs);
1739 while (pango_attr_iterator_next (iter));
1742 label->effective_attrs =
1743 pango_attr_list_ref (label->attrs);
1748 gtk_label_set_attributes_internal (GtkLabel *label,
1749 PangoAttrList *attrs)
1752 pango_attr_list_ref (attrs);
1755 pango_attr_list_unref (label->attrs);
1756 label->attrs = attrs;
1758 g_object_notify (G_OBJECT (label), "attributes");
1762 /* Calculates text, attrs and mnemonic_keyval from
1763 * label, use_underline and use_markup
1766 gtk_label_recalculate (GtkLabel *label)
1768 guint keyval = label->mnemonic_keyval;
1770 if (label->use_markup)
1772 gtk_label_set_markup_internal (label, label->label, label->use_underline);
1773 gtk_label_compose_effective_attrs (label);
1777 if (label->use_underline)
1779 gtk_label_set_uline_text_internal (label, label->label);
1780 gtk_label_compose_effective_attrs (label);
1784 gtk_label_set_text_internal (label, g_strdup (label->label));
1786 pango_attr_list_ref (label->attrs);
1787 if (label->effective_attrs)
1788 pango_attr_list_unref (label->effective_attrs);
1789 label->effective_attrs = label->attrs;
1793 if (!label->use_underline)
1794 label->mnemonic_keyval = GDK_VoidSymbol;
1796 if (keyval != label->mnemonic_keyval)
1798 gtk_label_setup_mnemonic (label, keyval);
1799 g_object_notify (G_OBJECT (label), "mnemonic-keyval");
1802 gtk_label_clear_layout (label);
1803 gtk_label_clear_select_info (label);
1804 gtk_widget_queue_resize (GTK_WIDGET (label));
1808 * gtk_label_set_text:
1809 * @label: a #GtkLabel
1810 * @str: The text you want to set
1812 * Sets the text within the #GtkLabel widget. It overwrites any text that
1815 * This will also clear any previously set mnemonic accelerators.
1818 gtk_label_set_text (GtkLabel *label,
1821 g_return_if_fail (GTK_IS_LABEL (label));
1823 g_object_freeze_notify (G_OBJECT (label));
1825 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
1826 gtk_label_set_use_markup_internal (label, FALSE);
1827 gtk_label_set_use_underline_internal (label, FALSE);
1829 gtk_label_recalculate (label);
1831 g_object_thaw_notify (G_OBJECT (label));
1835 * gtk_label_set_attributes:
1836 * @label: a #GtkLabel
1837 * @attrs: a #PangoAttrList
1839 * Sets a #PangoAttrList; the attributes in the list are applied to the
1842 * <note><para>The attributes set with this function will be applied
1843 * and merged with any other attributes previously effected by way
1844 * of the #GtkLabel:use-underline or #GtkLabel:use-markup properties.
1845 * While it is not recommended to mix markup strings with manually set
1846 * attributes, if you must; know that the attributes will be applied
1847 * to the label after the markup string is parsed.</para></note>
1850 gtk_label_set_attributes (GtkLabel *label,
1851 PangoAttrList *attrs)
1853 g_return_if_fail (GTK_IS_LABEL (label));
1855 gtk_label_set_attributes_internal (label, attrs);
1857 gtk_label_recalculate (label);
1859 gtk_label_clear_layout (label);
1860 gtk_widget_queue_resize (GTK_WIDGET (label));
1864 * gtk_label_get_attributes:
1865 * @label: a #GtkLabel
1867 * Gets the attribute list that was set on the label using
1868 * gtk_label_set_attributes(), if any. This function does
1869 * not reflect attributes that come from the labels markup
1870 * (see gtk_label_set_markup()). If you want to get the
1871 * effective attributes for the label, use
1872 * pango_layout_get_attribute (gtk_label_get_layout (label)).
1874 * Return value: the attribute list, or %NULL if none was set.
1877 gtk_label_get_attributes (GtkLabel *label)
1879 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1881 return label->attrs;
1885 * gtk_label_set_label:
1886 * @label: a #GtkLabel
1887 * @str: the new text to set for the label
1889 * Sets the text of the label. The label is interpreted as
1890 * including embedded underlines and/or Pango markup depending
1891 * on the values of the #GtkLabel:use-underline" and
1892 * #GtkLabel:use-markup properties.
1895 gtk_label_set_label (GtkLabel *label,
1898 g_return_if_fail (GTK_IS_LABEL (label));
1900 g_object_freeze_notify (G_OBJECT (label));
1902 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
1903 gtk_label_recalculate (label);
1905 g_object_thaw_notify (G_OBJECT (label));
1909 * gtk_label_get_label:
1910 * @label: a #GtkLabel
1912 * Fetches the text from a label widget including any embedded
1913 * underlines indicating mnemonics and Pango markup. (See
1914 * gtk_label_get_text()).
1916 * Return value: the text of the label widget. This string is
1917 * owned by the widget and must not be modified or freed.
1919 G_CONST_RETURN gchar *
1920 gtk_label_get_label (GtkLabel *label)
1922 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1924 return label->label;
1932 GdkColor *link_color;
1933 GdkColor *visited_link_color;
1937 start_element_handler (GMarkupParseContext *context,
1938 const gchar *element_name,
1939 const gchar **attribute_names,
1940 const gchar **attribute_values,
1944 UriParserData *pdata = user_data;
1946 if (strcmp (element_name, "a") == 0)
1949 const gchar *uri = NULL;
1950 const gchar *title = NULL;
1951 gboolean visited = FALSE;
1955 GdkColor *color = NULL;
1957 g_markup_parse_context_get_position (context, &line_number, &char_number);
1959 for (i = 0; attribute_names[i] != NULL; i++)
1961 const gchar *attr = attribute_names[i];
1963 if (strcmp (attr, "href") == 0)
1964 uri = attribute_values[i];
1965 else if (strcmp (attr, "title") == 0)
1966 title = attribute_values[i];
1971 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
1972 "Attribute '%s' is not allowed on the <a> tag "
1973 "on line %d char %d",
1974 attr, line_number, char_number);
1983 G_MARKUP_ERROR_INVALID_CONTENT,
1984 "Attribute 'href' was missing on the <a> tag "
1985 "on line %d char %d",
1986 line_number, char_number);
1990 if (pdata->label->select_info)
1993 for (l = pdata->label->select_info->links; l; l = l->next)
1996 if (strcmp (uri, link->uri) == 0)
1998 visited = link->visited;
2005 color = pdata->visited_link_color;
2007 color = pdata->link_color;
2009 g_string_append_printf (pdata->new_str,
2010 "<span color=\"#%04x%04x%04x\" underline=\"single\">",
2015 link = g_new0 (GtkLabelLink, 1);
2016 link->uri = g_strdup (uri);
2017 link->title = g_strdup (title);
2018 link->visited = visited;
2019 pdata->links = g_list_append (pdata->links, link);
2025 g_string_append_c (pdata->new_str, '<');
2026 g_string_append (pdata->new_str, element_name);
2028 for (i = 0; attribute_names[i] != NULL; i++)
2030 const gchar *attr = attribute_names[i];
2031 const gchar *value = attribute_values[i];
2034 newvalue = g_markup_escape_text (value, -1);
2036 g_string_append_c (pdata->new_str, ' ');
2037 g_string_append (pdata->new_str, attr);
2038 g_string_append (pdata->new_str, "=\"");
2039 g_string_append (pdata->new_str, newvalue);
2040 g_string_append_c (pdata->new_str, '\"');
2044 g_string_append_c (pdata->new_str, '>');
2049 end_element_handler (GMarkupParseContext *context,
2050 const gchar *element_name,
2054 UriParserData *pdata = user_data;
2056 if (!strcmp (element_name, "a"))
2057 g_string_append (pdata->new_str, "</span>");
2060 g_string_append (pdata->new_str, "</");
2061 g_string_append (pdata->new_str, element_name);
2062 g_string_append_c (pdata->new_str, '>');
2067 text_handler (GMarkupParseContext *context,
2073 UriParserData *pdata = user_data;
2076 newtext = g_markup_escape_text (text, text_len);
2077 g_string_append (pdata->new_str, newtext);
2081 static const GMarkupParser markup_parser =
2083 start_element_handler,
2084 end_element_handler,
2091 xml_isspace (gchar c)
2093 return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
2097 link_free (GtkLabelLink *link)
2100 g_free (link->title);
2105 gtk_label_get_link_colors (GtkWidget *widget,
2106 GdkColor **link_color,
2107 GdkColor **visited_link_color)
2109 gtk_widget_ensure_style (widget);
2110 gtk_widget_style_get (widget,
2111 "link-color", link_color,
2112 "visited-link-color", visited_link_color,
2115 *link_color = gdk_color_copy (&default_link_color);
2116 if (!*visited_link_color)
2117 *visited_link_color = gdk_color_copy (&default_visited_link_color);
2121 parse_uri_markup (GtkLabel *label,
2127 GMarkupParseContext *context = NULL;
2128 const gchar *p, *end;
2129 gboolean needs_root = TRUE;
2131 UriParserData pdata;
2133 length = strlen (str);
2137 pdata.label = label;
2139 pdata.new_str = g_string_sized_new (length);
2141 gtk_label_get_link_colors (GTK_WIDGET (label), &pdata.link_color, &pdata.visited_link_color);
2143 while (p != end && xml_isspace (*p))
2146 if (end - p >= 8 && strncmp (p, "<markup>", 8) == 0)
2149 context = g_markup_parse_context_new (&markup_parser, 0, &pdata, NULL);
2153 if (!g_markup_parse_context_parse (context, "<markup>", -1, error))
2157 if (!g_markup_parse_context_parse (context, str, length, error))
2162 if (!g_markup_parse_context_parse (context, "</markup>", -1, error))
2166 if (!g_markup_parse_context_end_parse (context, error))
2169 g_markup_parse_context_free (context);
2171 *new_str = g_string_free (pdata.new_str, FALSE);
2172 *links = pdata.links;
2174 gdk_color_free (pdata.link_color);
2175 gdk_color_free (pdata.visited_link_color);
2180 g_markup_parse_context_free (context);
2181 g_string_free (pdata.new_str, TRUE);
2182 g_list_foreach (pdata.links, (GFunc)link_free, NULL);
2183 g_list_free (pdata.links);
2184 gdk_color_free (pdata.link_color);
2185 gdk_color_free (pdata.visited_link_color);
2191 gtk_label_ensure_has_tooltip (GtkLabel *label)
2194 gboolean has_tooltip = FALSE;
2196 for (l = label->select_info->links; l; l = l->next)
2198 GtkLabelLink *link = l->data;
2206 gtk_widget_set_has_tooltip (GTK_WIDGET (label), has_tooltip);
2210 gtk_label_set_markup_internal (GtkLabel *label,
2212 gboolean with_uline)
2215 GError *error = NULL;
2216 PangoAttrList *attrs = NULL;
2217 gunichar accel_char = 0;
2219 GList *links = NULL;
2221 if (!parse_uri_markup (label, str, &new_str, &links, &error))
2223 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2225 g_error_free (error);
2229 gtk_label_clear_links (label);
2232 gtk_label_ensure_select_info (label);
2233 label->select_info->links = links;
2234 gtk_label_ensure_has_tooltip (label);
2237 if (!pango_parse_markup (new_str,
2239 with_uline ? '_' : 0,
2242 with_uline ? &accel_char : NULL,
2245 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2248 g_error_free (error);
2255 gtk_label_set_text_internal (label, text);
2259 if (label->effective_attrs)
2260 pango_attr_list_unref (label->effective_attrs);
2261 label->effective_attrs = attrs;
2264 if (accel_char != 0)
2265 label->mnemonic_keyval = gdk_keyval_to_lower (gdk_unicode_to_keyval (accel_char));
2267 label->mnemonic_keyval = GDK_VoidSymbol;
2271 * gtk_label_set_markup:
2272 * @label: a #GtkLabel
2273 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
2275 * Parses @str which is marked up with the <link
2276 * linkend="PangoMarkupFormat">Pango text markup language</link>, setting the
2277 * label's text and attribute list based on the parse results. If the @str is
2278 * external data, you may need to escape it with g_markup_escape_text() or
2279 * g_markup_printf_escaped()<!-- -->:
2283 * markup = g_markup_printf_escaped ("<span style=\"italic\">%s</span>", str);
2284 * gtk_label_set_markup (GTK_LABEL (label), markup);
2289 gtk_label_set_markup (GtkLabel *label,
2292 g_return_if_fail (GTK_IS_LABEL (label));
2294 g_object_freeze_notify (G_OBJECT (label));
2296 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2297 gtk_label_set_use_markup_internal (label, TRUE);
2298 gtk_label_set_use_underline_internal (label, FALSE);
2300 gtk_label_recalculate (label);
2302 g_object_thaw_notify (G_OBJECT (label));
2306 * gtk_label_set_markup_with_mnemonic:
2307 * @label: a #GtkLabel
2308 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
2310 * Parses @str which is marked up with the <link linkend="PangoMarkupFormat">Pango text markup language</link>,
2311 * setting the label's text and attribute list based on the parse results.
2312 * If characters in @str are preceded by an underscore, they are underlined
2313 * indicating that they represent a keyboard accelerator called a mnemonic.
2315 * The mnemonic key can be used to activate another widget, chosen
2316 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
2319 gtk_label_set_markup_with_mnemonic (GtkLabel *label,
2322 g_return_if_fail (GTK_IS_LABEL (label));
2324 g_object_freeze_notify (G_OBJECT (label));
2326 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2327 gtk_label_set_use_markup_internal (label, TRUE);
2328 gtk_label_set_use_underline_internal (label, TRUE);
2330 gtk_label_recalculate (label);
2332 g_object_thaw_notify (G_OBJECT (label));
2336 * gtk_label_get_text:
2337 * @label: a #GtkLabel
2339 * Fetches the text from a label widget, as displayed on the
2340 * screen. This does not include any embedded underlines
2341 * indicating mnemonics or Pango markup. (See gtk_label_get_label())
2343 * Return value: the text in the label widget. This is the internal
2344 * string used by the label, and must not be modified.
2346 G_CONST_RETURN gchar *
2347 gtk_label_get_text (GtkLabel *label)
2349 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2354 static PangoAttrList *
2355 gtk_label_pattern_to_attrs (GtkLabel *label,
2356 const gchar *pattern)
2359 const char *p = label->text;
2360 const char *q = pattern;
2361 PangoAttrList *attrs;
2363 attrs = pango_attr_list_new ();
2367 while (*p && *q && *q != '_')
2369 p = g_utf8_next_char (p);
2373 while (*p && *q && *q == '_')
2375 p = g_utf8_next_char (p);
2381 PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_LOW);
2382 attr->start_index = start - label->text;
2383 attr->end_index = p - label->text;
2385 pango_attr_list_insert (attrs, attr);
2395 gtk_label_set_pattern_internal (GtkLabel *label,
2396 const gchar *pattern)
2398 PangoAttrList *attrs;
2399 gboolean enable_mnemonics;
2401 g_return_if_fail (GTK_IS_LABEL (label));
2403 if (label->pattern_set)
2406 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2407 "gtk-enable-mnemonics", &enable_mnemonics,
2410 if (enable_mnemonics && pattern)
2411 attrs = gtk_label_pattern_to_attrs (label, pattern);
2415 if (label->effective_attrs)
2416 pango_attr_list_unref (label->effective_attrs);
2417 label->effective_attrs = attrs;
2421 gtk_label_set_pattern (GtkLabel *label,
2422 const gchar *pattern)
2424 g_return_if_fail (GTK_IS_LABEL (label));
2426 label->pattern_set = FALSE;
2430 gtk_label_set_pattern_internal (label, pattern);
2431 label->pattern_set = TRUE;
2434 gtk_label_recalculate (label);
2436 gtk_label_clear_layout (label);
2437 gtk_widget_queue_resize (GTK_WIDGET (label));
2442 * gtk_label_set_justify:
2443 * @label: a #GtkLabel
2444 * @jtype: a #GtkJustification
2446 * Sets the alignment of the lines in the text of the label relative to
2447 * each other. %GTK_JUSTIFY_LEFT is the default value when the
2448 * widget is first created with gtk_label_new(). If you instead want
2449 * to set the alignment of the label as a whole, use
2450 * gtk_misc_set_alignment() instead. gtk_label_set_justify() has no
2451 * effect on labels containing only a single line.
2454 gtk_label_set_justify (GtkLabel *label,
2455 GtkJustification jtype)
2457 g_return_if_fail (GTK_IS_LABEL (label));
2458 g_return_if_fail (jtype >= GTK_JUSTIFY_LEFT && jtype <= GTK_JUSTIFY_FILL);
2460 if ((GtkJustification) label->jtype != jtype)
2462 label->jtype = jtype;
2464 /* No real need to be this drastic, but easier than duplicating the code */
2465 gtk_label_clear_layout (label);
2467 g_object_notify (G_OBJECT (label), "justify");
2468 gtk_widget_queue_resize (GTK_WIDGET (label));
2473 * gtk_label_get_justify:
2474 * @label: a #GtkLabel
2476 * Returns the justification of the label. See gtk_label_set_justify().
2478 * Return value: #GtkJustification
2481 gtk_label_get_justify (GtkLabel *label)
2483 g_return_val_if_fail (GTK_IS_LABEL (label), 0);
2485 return label->jtype;
2489 * gtk_label_set_ellipsize:
2490 * @label: a #GtkLabel
2491 * @mode: a #PangoEllipsizeMode
2493 * Sets the mode used to ellipsize (add an ellipsis: "...") to the text
2494 * if there is not enough space to render the entire string.
2499 gtk_label_set_ellipsize (GtkLabel *label,
2500 PangoEllipsizeMode mode)
2502 g_return_if_fail (GTK_IS_LABEL (label));
2503 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE && mode <= PANGO_ELLIPSIZE_END);
2505 if ((PangoEllipsizeMode) label->ellipsize != mode)
2507 label->ellipsize = mode;
2509 /* No real need to be this drastic, but easier than duplicating the code */
2510 gtk_label_clear_layout (label);
2512 g_object_notify (G_OBJECT (label), "ellipsize");
2513 gtk_widget_queue_resize (GTK_WIDGET (label));
2518 * gtk_label_get_ellipsize:
2519 * @label: a #GtkLabel
2521 * Returns the ellipsizing position of the label. See gtk_label_set_ellipsize().
2523 * Return value: #PangoEllipsizeMode
2528 gtk_label_get_ellipsize (GtkLabel *label)
2530 g_return_val_if_fail (GTK_IS_LABEL (label), PANGO_ELLIPSIZE_NONE);
2532 return label->ellipsize;
2536 * gtk_label_set_width_chars:
2537 * @label: a #GtkLabel
2538 * @n_chars: the new desired width, in characters.
2540 * Sets the desired width in characters of @label to @n_chars.
2545 gtk_label_set_width_chars (GtkLabel *label,
2548 GtkLabelPrivate *priv;
2550 g_return_if_fail (GTK_IS_LABEL (label));
2552 priv = GTK_LABEL_GET_PRIVATE (label);
2554 if (priv->width_chars != n_chars)
2556 priv->width_chars = n_chars;
2557 g_object_notify (G_OBJECT (label), "width-chars");
2558 gtk_label_invalidate_wrap_width (label);
2559 gtk_widget_queue_resize (GTK_WIDGET (label));
2564 * gtk_label_get_width_chars:
2565 * @label: a #GtkLabel
2567 * Retrieves the desired width of @label, in characters. See
2568 * gtk_label_set_width_chars().
2570 * Return value: the width of the label in characters.
2575 gtk_label_get_width_chars (GtkLabel *label)
2577 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2579 return GTK_LABEL_GET_PRIVATE (label)->width_chars;
2583 * gtk_label_set_max_width_chars:
2584 * @label: a #GtkLabel
2585 * @n_chars: the new desired maximum width, in characters.
2587 * Sets the desired maximum width in characters of @label to @n_chars.
2592 gtk_label_set_max_width_chars (GtkLabel *label,
2595 GtkLabelPrivate *priv;
2597 g_return_if_fail (GTK_IS_LABEL (label));
2599 priv = GTK_LABEL_GET_PRIVATE (label);
2601 if (priv->max_width_chars != n_chars)
2603 priv->max_width_chars = n_chars;
2605 g_object_notify (G_OBJECT (label), "max-width-chars");
2606 gtk_label_invalidate_wrap_width (label);
2607 gtk_widget_queue_resize (GTK_WIDGET (label));
2612 * gtk_label_get_max_width_chars:
2613 * @label: a #GtkLabel
2615 * Retrieves the desired maximum width of @label, in characters. See
2616 * gtk_label_set_width_chars().
2618 * Return value: the maximum width of the label in characters.
2623 gtk_label_get_max_width_chars (GtkLabel *label)
2625 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2627 return GTK_LABEL_GET_PRIVATE (label)->max_width_chars;
2631 * gtk_label_set_line_wrap:
2632 * @label: a #GtkLabel
2633 * @wrap: the setting
2635 * Toggles line wrapping within the #GtkLabel widget. %TRUE makes it break
2636 * lines if text exceeds the widget's size. %FALSE lets the text get cut off
2637 * by the edge of the widget if it exceeds the widget size.
2639 * Note that setting line wrapping to %TRUE does not make the label
2640 * wrap at its parent container's width, because GTK+ widgets
2641 * conceptually can't make their requisition depend on the parent
2642 * container's size. For a label that wraps at a specific position,
2643 * set the label's width using gtk_widget_set_size_request().
2646 gtk_label_set_line_wrap (GtkLabel *label,
2649 g_return_if_fail (GTK_IS_LABEL (label));
2651 wrap = wrap != FALSE;
2653 if (label->wrap != wrap)
2657 gtk_label_clear_layout (label);
2658 gtk_widget_queue_resize (GTK_WIDGET (label));
2659 g_object_notify (G_OBJECT (label), "wrap");
2664 * gtk_label_get_line_wrap:
2665 * @label: a #GtkLabel
2667 * Returns whether lines in the label are automatically wrapped.
2668 * See gtk_label_set_line_wrap().
2670 * Return value: %TRUE if the lines of the label are automatically wrapped.
2673 gtk_label_get_line_wrap (GtkLabel *label)
2675 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
2681 * gtk_label_set_line_wrap_mode:
2682 * @label: a #GtkLabel
2683 * @wrap_mode: the line wrapping mode
2685 * If line wrapping is on (see gtk_label_set_line_wrap()) this controls how
2686 * the line wrapping is done. The default is %PANGO_WRAP_WORD which means
2687 * wrap on word boundaries.
2692 gtk_label_set_line_wrap_mode (GtkLabel *label,
2693 PangoWrapMode wrap_mode)
2695 g_return_if_fail (GTK_IS_LABEL (label));
2697 if (label->wrap_mode != wrap_mode)
2699 label->wrap_mode = wrap_mode;
2700 g_object_notify (G_OBJECT (label), "wrap-mode");
2702 gtk_widget_queue_resize (GTK_WIDGET (label));
2707 * gtk_label_get_line_wrap_mode:
2708 * @label: a #GtkLabel
2710 * Returns line wrap mode used by the label. See gtk_label_set_line_wrap_mode().
2712 * Return value: %TRUE if the lines of the label are automatically wrapped.
2717 gtk_label_get_line_wrap_mode (GtkLabel *label)
2719 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
2721 return label->wrap_mode;
2726 gtk_label_get (GtkLabel *label,
2729 g_return_if_fail (GTK_IS_LABEL (label));
2730 g_return_if_fail (str != NULL);
2736 gtk_label_destroy (GtkObject *object)
2738 GtkLabel *label = GTK_LABEL (object);
2740 gtk_label_set_mnemonic_widget (label, NULL);
2742 GTK_OBJECT_CLASS (gtk_label_parent_class)->destroy (object);
2746 gtk_label_finalize (GObject *object)
2748 GtkLabel *label = GTK_LABEL (object);
2750 g_free (label->label);
2751 g_free (label->text);
2754 g_object_unref (label->layout);
2757 pango_attr_list_unref (label->attrs);
2759 if (label->effective_attrs)
2760 pango_attr_list_unref (label->effective_attrs);
2762 gtk_label_clear_links (label);
2763 g_free (label->select_info);
2765 G_OBJECT_CLASS (gtk_label_parent_class)->finalize (object);
2769 gtk_label_clear_layout (GtkLabel *label)
2773 g_object_unref (label->layout);
2774 label->layout = NULL;
2776 //gtk_label_clear_links (label);
2781 get_label_char_width (GtkLabel *label)
2783 GtkLabelPrivate *priv;
2784 PangoContext *context;
2785 PangoFontMetrics *metrics;
2786 gint char_width, digit_width, char_pixels, w;
2788 priv = GTK_LABEL_GET_PRIVATE (label);
2790 context = pango_layout_get_context (label->layout);
2791 metrics = pango_context_get_metrics (context, GTK_WIDGET (label)->style->font_desc,
2792 pango_context_get_language (context));
2794 char_width = pango_font_metrics_get_approximate_char_width (metrics);
2795 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
2796 char_pixels = MAX (char_width, digit_width);
2797 pango_font_metrics_unref (metrics);
2799 if (priv->width_chars < 0)
2801 PangoRectangle rect;
2803 pango_layout_set_width (label->layout, -1);
2804 pango_layout_get_extents (label->layout, NULL, &rect);
2806 w = char_pixels * MAX (priv->max_width_chars, 3);
2807 w = MIN (rect.width, w);
2811 /* enforce minimum width for ellipsized labels at ~3 chars */
2812 w = char_pixels * MAX (priv->width_chars, 3);
2819 gtk_label_invalidate_wrap_width (GtkLabel *label)
2821 GtkLabelPrivate *priv;
2823 priv = GTK_LABEL_GET_PRIVATE (label);
2825 priv->wrap_width = -1;
2829 get_label_wrap_width (GtkLabel *label)
2831 GtkLabelPrivate *priv;
2833 priv = GTK_LABEL_GET_PRIVATE (label);
2835 if (priv->wrap_width < 0)
2837 if (priv->width_chars > 0 || priv->max_width_chars > 0)
2838 priv->wrap_width = get_label_char_width (label);
2841 PangoLayout *layout;
2843 layout = gtk_widget_create_pango_layout (GTK_WIDGET (label),
2844 "This long string gives a good enough length for any line to have.");
2845 pango_layout_get_size (layout, &priv->wrap_width, NULL);
2846 g_object_unref (layout);
2850 return priv->wrap_width;
2854 gtk_label_ensure_layout (GtkLabel *label)
2857 PangoRectangle logical_rect;
2860 widget = GTK_WIDGET (label);
2862 rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2866 PangoAlignment align = PANGO_ALIGN_LEFT; /* Quiet gcc */
2867 gdouble angle = gtk_label_get_angle (label);
2869 if (angle != 0.0 && !label->wrap && !label->ellipsize && !label->select_info)
2871 /* We rotate the standard singleton PangoContext for the widget,
2872 * depending on the fact that it's meant pretty much exclusively
2875 PangoMatrix matrix = PANGO_MATRIX_INIT;
2877 pango_matrix_rotate (&matrix, angle);
2879 pango_context_set_matrix (gtk_widget_get_pango_context (widget), &matrix);
2881 label->have_transform = TRUE;
2885 if (label->have_transform)
2886 pango_context_set_matrix (gtk_widget_get_pango_context (widget), NULL);
2888 label->have_transform = FALSE;
2891 label->layout = gtk_widget_create_pango_layout (widget, label->text);
2893 if (label->effective_attrs)
2894 pango_layout_set_attributes (label->layout, label->effective_attrs);
2896 gtk_label_rescan_links (label);
2898 switch (label->jtype)
2900 case GTK_JUSTIFY_LEFT:
2901 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
2903 case GTK_JUSTIFY_RIGHT:
2904 align = rtl ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
2906 case GTK_JUSTIFY_CENTER:
2907 align = PANGO_ALIGN_CENTER;
2909 case GTK_JUSTIFY_FILL:
2910 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
2911 pango_layout_set_justify (label->layout, TRUE);
2914 g_assert_not_reached();
2917 pango_layout_set_alignment (label->layout, align);
2918 pango_layout_set_ellipsize (label->layout, label->ellipsize);
2919 pango_layout_set_single_paragraph_mode (label->layout, label->single_line_mode);
2921 if (label->ellipsize)
2922 pango_layout_set_width (label->layout,
2923 widget->allocation.width * PANGO_SCALE);
2924 else if (label->wrap)
2926 GtkWidgetAuxInfo *aux_info;
2927 gint longest_paragraph;
2930 pango_layout_set_wrap (label->layout, label->wrap_mode);
2932 aux_info = _gtk_widget_get_aux_info (widget, FALSE);
2933 if (aux_info && aux_info->width > 0)
2934 pango_layout_set_width (label->layout, aux_info->width * PANGO_SCALE);
2937 GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (label));
2940 pango_layout_set_width (label->layout, -1);
2941 pango_layout_get_extents (label->layout, NULL, &logical_rect);
2943 width = logical_rect.width;
2945 /* Try to guess a reasonable maximum width */
2946 longest_paragraph = width;
2948 wrap_width = get_label_wrap_width (label);
2949 width = MIN (width, wrap_width);
2951 PANGO_SCALE * (gdk_screen_get_width (screen) + 1) / 2);
2953 pango_layout_set_width (label->layout, width);
2954 pango_layout_get_extents (label->layout, NULL, &logical_rect);
2955 width = logical_rect.width;
2956 height = logical_rect.height;
2958 /* Unfortunately, the above may leave us with a very unbalanced looking paragraph,
2959 * so we try short search for a narrower width that leaves us with the same height
2961 if (longest_paragraph > 0)
2963 gint nlines, perfect_width;
2965 nlines = pango_layout_get_line_count (label->layout);
2966 perfect_width = (longest_paragraph + nlines - 1) / nlines;
2968 if (perfect_width < width)
2970 pango_layout_set_width (label->layout, perfect_width);
2971 pango_layout_get_extents (label->layout, NULL, &logical_rect);
2973 if (logical_rect.height <= height)
2974 width = logical_rect.width;
2977 gint mid_width = (perfect_width + width) / 2;
2979 if (mid_width > perfect_width)
2981 pango_layout_set_width (label->layout, mid_width);
2982 pango_layout_get_extents (label->layout, NULL, &logical_rect);
2984 if (logical_rect.height <= height)
2985 width = logical_rect.width;
2990 pango_layout_set_width (label->layout, width);
2993 else /* !label->wrap */
2994 pango_layout_set_width (label->layout, -1);
2999 gtk_label_size_request (GtkWidget *widget,
3000 GtkRequisition *requisition)
3002 GtkLabel *label = GTK_LABEL (widget);
3003 GtkLabelPrivate *priv;
3005 PangoRectangle logical_rect;
3006 GtkWidgetAuxInfo *aux_info;
3008 priv = GTK_LABEL_GET_PRIVATE (widget);
3011 * If word wrapping is on, then the height requisition can depend
3014 * - Any width set on the widget via gtk_widget_set_size_request().
3015 * - The padding of the widget (xpad, set by gtk_misc_set_padding)
3017 * Instead of trying to detect changes to these quantities, if we
3018 * are wrapping, we just rewrap for each size request. Since
3019 * size requisitions are cached by the GTK+ core, this is not
3024 gtk_label_clear_layout (label);
3026 gtk_label_ensure_layout (label);
3028 width = label->misc.xpad * 2;
3029 height = label->misc.ypad * 2;
3031 aux_info = _gtk_widget_get_aux_info (widget, FALSE);
3033 if (label->have_transform)
3035 PangoRectangle rect;
3036 PangoContext *context = pango_layout_get_context (label->layout);
3037 const PangoMatrix *matrix = pango_context_get_matrix (context);
3039 pango_layout_get_extents (label->layout, NULL, &rect);
3040 pango_matrix_transform_rectangle (matrix, &rect);
3041 pango_extents_to_pixels (&rect, NULL);
3043 requisition->width = width + rect.width;
3044 requisition->height = height + rect.height;
3049 pango_layout_get_extents (label->layout, NULL, &logical_rect);
3051 if ((label->wrap || label->ellipsize ||
3052 priv->width_chars > 0 || priv->max_width_chars > 0) &&
3053 aux_info && aux_info->width > 0)
3054 width += aux_info->width;
3055 else if (label->ellipsize || priv->width_chars > 0 || priv->max_width_chars > 0)
3057 width += PANGO_PIXELS (get_label_char_width (label));
3060 width += PANGO_PIXELS (logical_rect.width);
3062 if (label->single_line_mode)
3064 PangoContext *context;
3065 PangoFontMetrics *metrics;
3066 gint ascent, descent;
3068 context = pango_layout_get_context (label->layout);
3069 metrics = pango_context_get_metrics (context, widget->style->font_desc,
3070 pango_context_get_language (context));
3072 ascent = pango_font_metrics_get_ascent (metrics);
3073 descent = pango_font_metrics_get_descent (metrics);
3074 pango_font_metrics_unref (metrics);
3076 height += PANGO_PIXELS (ascent + descent);
3079 height += PANGO_PIXELS (logical_rect.height);
3081 requisition->width = width;
3082 requisition->height = height;
3086 gtk_label_size_allocate (GtkWidget *widget,
3087 GtkAllocation *allocation)
3091 label = GTK_LABEL (widget);
3093 GTK_WIDGET_CLASS (gtk_label_parent_class)->size_allocate (widget, allocation);
3095 if (label->ellipsize)
3100 PangoRectangle logical;
3102 width = (allocation->width - label->misc.xpad * 2) * PANGO_SCALE;
3104 pango_layout_set_width (label->layout, -1);
3105 pango_layout_get_extents (label->layout, NULL, &logical);
3107 if (logical.width > width)
3108 pango_layout_set_width (label->layout, width);
3112 if (label->select_info && label->select_info->window)
3114 gdk_window_move_resize (label->select_info->window,
3118 allocation->height);
3123 gtk_label_update_cursor (GtkLabel *label)
3125 if (!label->select_info)
3128 if (GTK_WIDGET_REALIZED (label))
3130 GdkDisplay *display;
3133 if (GTK_WIDGET_IS_SENSITIVE (label))
3135 display = gtk_widget_get_display (GTK_WIDGET (label));
3137 if (label->select_info->active_link)
3138 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
3139 else if (label->select_info->selectable)
3140 cursor = gdk_cursor_new_for_display (display, GDK_XTERM);
3147 gdk_window_set_cursor (label->select_info->window, cursor);
3150 gdk_cursor_unref (cursor);
3155 gtk_label_state_changed (GtkWidget *widget,
3156 GtkStateType prev_state)
3158 GtkLabel *label = GTK_LABEL (widget);
3160 if (label->select_info)
3162 gtk_label_select_region (label, 0, 0);
3163 gtk_label_update_cursor (label);
3166 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed)
3167 GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed (widget, prev_state);
3171 gtk_label_style_set (GtkWidget *widget,
3172 GtkStyle *previous_style)
3174 GtkLabel *label = GTK_LABEL (widget);
3176 /* We have to clear the layout, fonts etc. may have changed */
3177 gtk_label_clear_layout (label);
3178 gtk_label_invalidate_wrap_width (label);
3182 gtk_label_direction_changed (GtkWidget *widget,
3183 GtkTextDirection previous_dir)
3185 GtkLabel *label = GTK_LABEL (widget);
3188 pango_layout_context_changed (label->layout);
3190 GTK_WIDGET_CLASS (gtk_label_parent_class)->direction_changed (widget, previous_dir);
3194 get_layout_location (GtkLabel *label,
3200 GtkLabelPrivate *priv;
3202 gint req_width, x, y;
3203 PangoRectangle logical;
3205 misc = GTK_MISC (label);
3206 widget = GTK_WIDGET (label);
3207 priv = GTK_LABEL_GET_PRIVATE (label);
3209 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
3210 xalign = misc->xalign;
3212 xalign = 1.0 - misc->xalign;
3214 pango_layout_get_pixel_extents (label->layout, NULL, &logical);
3216 if (label->ellipsize || priv->width_chars > 0)
3220 width = pango_layout_get_width (label->layout);
3222 req_width = logical.width;
3224 req_width = MIN(PANGO_PIXELS (width), req_width);
3225 req_width += 2 * misc->xpad;
3228 req_width = widget->requisition.width;
3230 x = floor (widget->allocation.x + (gint)misc->xpad +
3231 xalign * (widget->allocation.width - req_width));
3233 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
3234 x = MAX (x, widget->allocation.x + misc->xpad);
3236 x = MIN (x, widget->allocation.x + widget->allocation.width - misc->xpad);
3239 /* bgo#315462 - For single-line labels, *do* align the requisition with
3240 * respect to the allocation, even if we are under-allocated. For multi-line
3241 * labels, always show the top of the text when they are under-allocated. The
3242 * rationale is this:
3244 * - Single-line labels appear in GtkButtons, and it is very easy to get them
3245 * to be smaller than their requisition. The button may clip the label, but
3246 * the label will still be able to show most of itself and the focus
3247 * rectangle. Also, it is fairly easy to read a single line of clipped text.
3249 * - Multi-line labels should not be clipped to showing "something in the
3250 * middle". You want to read the first line, at least, to get some context.
3252 if (pango_layout_get_line_count (label->layout) == 1)
3253 y = floor (widget->allocation.y + (gint)misc->ypad
3254 + (widget->allocation.height - widget->requisition.height) * misc->yalign);
3256 y = floor (widget->allocation.y + (gint)misc->ypad
3257 + MAX (((widget->allocation.height - widget->requisition.height) * misc->yalign),
3268 draw_insertion_cursor (GtkLabel *label,
3269 GdkRectangle *cursor_location,
3270 gboolean is_primary,
3271 PangoDirection direction,
3272 gboolean draw_arrow)
3274 GtkWidget *widget = GTK_WIDGET (label);
3275 GtkTextDirection text_dir;
3277 if (direction == PANGO_DIRECTION_LTR)
3278 text_dir = GTK_TEXT_DIR_LTR;
3280 text_dir = GTK_TEXT_DIR_RTL;
3282 gtk_draw_insertion_cursor (widget, widget->window, &(widget->allocation),
3284 is_primary, text_dir, draw_arrow);
3287 static PangoDirection
3288 get_cursor_direction (GtkLabel *label)
3292 g_assert (label->select_info);
3294 gtk_label_ensure_layout (label);
3296 for (l = pango_layout_get_lines_readonly (label->layout); l; l = l->next)
3298 PangoLayoutLine *line = l->data;
3300 /* If label->select_info->selection_end is at the very end of
3301 * the line, we don't know if the cursor is on this line or
3302 * the next without looking ahead at the next line. (End
3303 * of paragraph is different from line break.) But it's
3304 * definitely in this paragraph, which is good enough
3305 * to figure out the resolved direction.
3307 if (line->start_index + line->length >= label->select_info->selection_end)
3308 return line->resolved_dir;
3311 return PANGO_DIRECTION_LTR;
3315 gtk_label_draw_cursor (GtkLabel *label, gint xoffset, gint yoffset)
3317 if (label->select_info == NULL)
3320 if (GTK_WIDGET_DRAWABLE (label))
3322 GtkWidget *widget = GTK_WIDGET (label);
3324 PangoDirection keymap_direction;
3325 PangoDirection cursor_direction;
3326 PangoRectangle strong_pos, weak_pos;
3327 gboolean split_cursor;
3328 PangoRectangle *cursor1 = NULL;
3329 PangoRectangle *cursor2 = NULL;
3330 GdkRectangle cursor_location;
3331 PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
3332 PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
3334 keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gtk_widget_get_display (widget)));
3335 cursor_direction = get_cursor_direction (label);
3337 gtk_label_ensure_layout (label);
3339 pango_layout_get_cursor_pos (label->layout, label->select_info->selection_end,
3340 &strong_pos, &weak_pos);
3342 g_object_get (gtk_widget_get_settings (widget),
3343 "gtk-split-cursor", &split_cursor,
3346 dir1 = cursor_direction;
3350 cursor1 = &strong_pos;
3352 if (strong_pos.x != weak_pos.x ||
3353 strong_pos.y != weak_pos.y)
3355 dir2 = (cursor_direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
3356 cursor2 = &weak_pos;
3361 if (keymap_direction == cursor_direction)
3362 cursor1 = &strong_pos;
3364 cursor1 = &weak_pos;
3367 cursor_location.x = xoffset + PANGO_PIXELS (cursor1->x);
3368 cursor_location.y = yoffset + PANGO_PIXELS (cursor1->y);
3369 cursor_location.width = 0;
3370 cursor_location.height = PANGO_PIXELS (cursor1->height);
3372 draw_insertion_cursor (label,
3373 &cursor_location, TRUE, dir1,
3374 dir2 != PANGO_DIRECTION_NEUTRAL);
3376 if (dir2 != PANGO_DIRECTION_NEUTRAL)
3378 cursor_location.x = xoffset + PANGO_PIXELS (cursor2->x);
3379 cursor_location.y = yoffset + PANGO_PIXELS (cursor2->y);
3380 cursor_location.width = 0;
3381 cursor_location.height = PANGO_PIXELS (cursor2->height);
3383 draw_insertion_cursor (label,
3384 &cursor_location, FALSE, dir2,
3390 static GtkLabelLink *
3391 gtk_label_get_focus_link (GtkLabel *label)
3393 GtkLabelSelectionInfo *info = label->select_info;
3399 if (info->selection_anchor != info->selection_end)
3402 for (l = info->links; l; l = l->next)
3404 GtkLabelLink *link = l->data;
3405 if (link->start <= info->selection_anchor &&
3406 info->selection_anchor <= link->end)
3414 gtk_label_expose (GtkWidget *widget,
3415 GdkEventExpose *event)
3417 GtkLabel *label = GTK_LABEL (widget);
3418 GtkLabelSelectionInfo *info = label->select_info;
3421 gtk_label_ensure_layout (label);
3423 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget) &&
3424 label->text && (*label->text != '\0'))
3426 get_layout_location (label, &x, &y);
3428 gtk_paint_layout (widget->style,
3430 GTK_WIDGET_STATE (widget),
3439 (info->selection_anchor != info->selection_end))
3445 range[0] = info->selection_anchor;
3446 range[1] = info->selection_end;
3448 if (range[0] > range[1])
3450 gint tmp = range[0];
3451 range[0] = range[1];
3455 clip = gdk_pango_layout_get_clip_region (label->layout,
3459 gdk_region_intersect (clip, event->region);
3461 /* FIXME should use gtk_paint, but it can't use a clip
3465 gdk_gc_set_clip_region (widget->style->black_gc, clip);
3468 state = GTK_STATE_SELECTED;
3469 if (!GTK_WIDGET_HAS_FOCUS (widget))
3470 state = GTK_STATE_ACTIVE;
3472 gdk_draw_layout_with_colors (widget->window,
3473 widget->style->black_gc,
3476 &widget->style->text[state],
3477 &widget->style->base[state]);
3479 gdk_gc_set_clip_region (widget->style->black_gc, NULL);
3480 gdk_region_destroy (clip);
3484 GtkLabelLink *focus_link;
3485 GtkLabelLink *active_link;
3489 GdkColor *text_color;
3490 GdkColor *base_color;
3491 GdkColor *link_color;
3492 GdkColor *visited_link_color;
3494 if (info->selectable && GTK_WIDGET_HAS_FOCUS (widget))
3495 gtk_label_draw_cursor (label, x, y);
3497 focus_link = gtk_label_get_focus_link (label);
3498 active_link = info->active_link;
3502 range[0] = active_link->start;
3503 range[1] = active_link->end;
3505 clip = gdk_pango_layout_get_clip_region (label->layout,
3509 gdk_gc_set_clip_region (widget->style->black_gc, clip);
3511 gtk_label_get_link_colors (widget, &link_color, &visited_link_color);
3512 if (active_link->visited)
3513 text_color = visited_link_color;
3515 text_color = link_color;
3516 if (info->link_clicked)
3517 base_color = &widget->style->base[GTK_STATE_ACTIVE];
3519 base_color = &widget->style->base[GTK_STATE_PRELIGHT];
3520 gdk_draw_layout_with_colors (widget->window,
3521 widget->style->black_gc,
3526 gdk_color_free (link_color);
3527 gdk_color_free (visited_link_color);
3529 gdk_gc_set_clip_region (widget->style->black_gc, NULL);
3530 gdk_region_destroy (clip);
3533 if (focus_link && GTK_WIDGET_HAS_FOCUS (widget))
3535 range[0] = focus_link->start;
3536 range[1] = focus_link->end;
3538 clip = gdk_pango_layout_get_clip_region (label->layout,
3542 gdk_region_get_clipbox (clip, &rect);
3544 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
3545 &event->area, widget, "label",
3546 rect.x, rect.y, rect.width, rect.height);
3548 gdk_region_destroy (clip);
3557 gtk_label_set_uline_text_internal (GtkLabel *label,
3560 guint accel_key = GDK_VoidSymbol;
3565 gchar *dest, *pattern_dest;
3566 gboolean underscore;
3568 g_return_if_fail (GTK_IS_LABEL (label));
3569 g_return_if_fail (str != NULL);
3571 /* Split text into the base text and a separate pattern
3575 new_str = g_new (gchar, strlen (str) + 1);
3576 pattern = g_new (gchar, g_utf8_strlen (str, -1) + 1);
3585 pattern_dest = pattern;
3592 c = g_utf8_get_char (src);
3593 if (c == (gunichar)-1)
3595 g_warning ("Invalid input string");
3600 next_src = g_utf8_next_char (src);
3605 *pattern_dest++ = ' ';
3608 *pattern_dest++ = '_';
3609 if (accel_key == GDK_VoidSymbol)
3610 accel_key = gdk_keyval_to_lower (gdk_unicode_to_keyval (c));
3613 while (src < next_src)
3627 while (src < next_src)
3630 *pattern_dest++ = ' ';
3637 gtk_label_set_text_internal (label, new_str);
3638 gtk_label_set_pattern_internal (label, pattern);
3642 label->mnemonic_keyval = accel_key;
3646 gtk_label_parse_uline (GtkLabel *label,
3651 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_VoidSymbol);
3652 g_return_val_if_fail (str != NULL, GDK_VoidSymbol);
3654 g_object_freeze_notify (G_OBJECT (label));
3656 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
3657 gtk_label_set_use_markup_internal (label, FALSE);
3658 gtk_label_set_use_underline_internal (label, TRUE);
3660 gtk_label_recalculate (label);
3662 keyval = label->mnemonic_keyval;
3663 if (keyval != GDK_VoidSymbol)
3665 label->mnemonic_keyval = GDK_VoidSymbol;
3666 gtk_label_setup_mnemonic (label, keyval);
3667 g_object_notify (G_OBJECT (label), "mnemonic-keyval");
3670 g_object_thaw_notify (G_OBJECT (label));
3676 * gtk_label_set_text_with_mnemonic:
3677 * @label: a #GtkLabel
3680 * Sets the label's text from the string @str.
3681 * If characters in @str are preceded by an underscore, they are underlined
3682 * indicating that they represent a keyboard accelerator called a mnemonic.
3683 * The mnemonic key can be used to activate another widget, chosen
3684 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
3687 gtk_label_set_text_with_mnemonic (GtkLabel *label,
3690 g_return_if_fail (GTK_IS_LABEL (label));
3691 g_return_if_fail (str != NULL);
3693 g_object_freeze_notify (G_OBJECT (label));
3695 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
3696 gtk_label_set_use_markup_internal (label, FALSE);
3697 gtk_label_set_use_underline_internal (label, TRUE);
3699 gtk_label_recalculate (label);
3701 g_object_thaw_notify (G_OBJECT (label));
3705 gtk_label_realize (GtkWidget *widget)
3709 label = GTK_LABEL (widget);
3711 GTK_WIDGET_CLASS (gtk_label_parent_class)->realize (widget);
3713 if (label->select_info)
3714 gtk_label_create_window (label);
3718 gtk_label_unrealize (GtkWidget *widget)
3722 label = GTK_LABEL (widget);
3724 if (label->select_info)
3725 gtk_label_destroy_window (label);
3727 GTK_WIDGET_CLASS (gtk_label_parent_class)->unrealize (widget);
3731 gtk_label_map (GtkWidget *widget)
3735 label = GTK_LABEL (widget);
3737 GTK_WIDGET_CLASS (gtk_label_parent_class)->map (widget);
3739 if (label->select_info)
3740 gdk_window_show (label->select_info->window);
3744 gtk_label_unmap (GtkWidget *widget)
3748 label = GTK_LABEL (widget);
3750 if (label->select_info)
3751 gdk_window_hide (label->select_info->window);
3753 GTK_WIDGET_CLASS (gtk_label_parent_class)->unmap (widget);
3757 window_to_layout_coords (GtkLabel *label,
3764 widget = GTK_WIDGET (label);
3766 /* get layout location in widget->window coords */
3767 get_layout_location (label, &lx, &ly);
3771 *x += widget->allocation.x; /* go to widget->window */
3772 *x -= lx; /* go to layout */
3777 *y += widget->allocation.y; /* go to widget->window */
3778 *y -= ly; /* go to layout */
3784 layout_to_window_coords (GtkLabel *label,
3791 widget = GTK_WIDGET (label);
3793 /* get layout location in widget->window coords */
3794 get_layout_location (label, &lx, &ly);
3798 *x += lx; /* go to widget->window */
3799 *x -= widget->allocation.x; /* go to selection window */
3804 *y += ly; /* go to widget->window */
3805 *y -= widget->allocation.y; /* go to selection window */
3811 get_layout_index (GtkLabel *label,
3817 const gchar *cluster;
3818 const gchar *cluster_end;
3822 gtk_label_ensure_layout (label);
3824 window_to_layout_coords (label, &x, &y);
3829 if (pango_layout_xy_to_index (label->layout,
3833 cluster = label->text + *index;
3834 cluster_end = cluster;
3837 cluster_end = g_utf8_next_char (cluster_end);
3841 *index += (cluster_end - cluster);
3850 gtk_label_select_word (GtkLabel *label)
3854 gint start_index = gtk_label_move_backward_word (label, label->select_info->selection_end);
3855 gint end_index = gtk_label_move_forward_word (label, label->select_info->selection_end);
3857 min = MIN (label->select_info->selection_anchor,
3858 label->select_info->selection_end);
3859 max = MAX (label->select_info->selection_anchor,
3860 label->select_info->selection_end);
3862 min = MIN (min, start_index);
3863 max = MAX (max, end_index);
3865 gtk_label_select_region_index (label, min, max);
3869 gtk_label_grab_focus (GtkWidget *widget)
3872 gboolean select_on_focus;
3875 label = GTK_LABEL (widget);
3877 if (label->select_info == NULL)
3880 GTK_WIDGET_CLASS (gtk_label_parent_class)->grab_focus (widget);
3882 if (label->select_info->selectable)
3884 g_object_get (gtk_widget_get_settings (widget),
3885 "gtk-label-select-on-focus",
3889 if (select_on_focus && !label->in_click)
3890 gtk_label_select_region (label, 0, -1);
3894 if (label->select_info->links && !label->in_click)
3896 link = label->select_info->links->data;
3897 label->select_info->selection_anchor = link->start;
3898 label->select_info->selection_end = link->start;
3904 gtk_label_focus (GtkWidget *widget,
3905 GtkDirectionType direction)
3907 GtkLabel *label = GTK_LABEL (widget);
3908 GtkLabelSelectionInfo *info = label->select_info;
3909 GtkLabelLink *focus_link;
3912 if (!gtk_widget_is_focus (widget))
3914 gtk_widget_grab_focus (widget);
3917 focus_link = gtk_label_get_focus_link (label);
3918 if (focus_link && direction == GTK_DIR_TAB_BACKWARD)
3920 l = g_list_last (info->links);
3921 focus_link = l->data;
3922 info->selection_anchor = focus_link->start;
3923 info->selection_end = focus_link->start;
3933 if (info->selectable)
3937 if (info->selection_anchor != info->selection_end)
3940 index = info->selection_anchor;
3942 if (direction == GTK_DIR_TAB_FORWARD)
3943 for (l = info->links; l; l = l->next)
3945 GtkLabelLink *link = l->data;
3947 if (link->start > index)
3949 gtk_label_select_region_index (label, link->start, link->start);
3953 else if (direction == GTK_DIR_TAB_BACKWARD)
3954 for (l = g_list_last (info->links); l; l = l->prev)
3956 GtkLabelLink *link = l->data;
3958 if (link->end < index)
3960 gtk_label_select_region_index (label, link->start, link->start);
3969 focus_link = gtk_label_get_focus_link (label);
3972 case GTK_DIR_TAB_FORWARD:
3975 l = g_list_find (info->links, focus_link);
3982 case GTK_DIR_TAB_BACKWARD:
3985 l = g_list_find (info->links, focus_link);
3989 l = g_list_last (info->links);
3998 focus_link = l->data;
3999 info->selection_anchor = focus_link->start;
4000 info->selection_end = focus_link->start;
4001 gtk_widget_queue_draw (widget);
4013 gtk_label_button_press (GtkWidget *widget,
4014 GdkEventButton *event)
4016 GtkLabel *label = GTK_LABEL (widget);
4017 GtkLabelSelectionInfo *info = label->select_info;
4024 if (info->active_link)
4026 if (event->button == 1)
4028 info->link_clicked = 1;
4029 gtk_widget_queue_draw (widget);
4031 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4033 info->link_clicked = 1;
4034 gtk_label_do_popup (label, event);
4039 if (!info->selectable)
4042 info->in_drag = FALSE;
4043 info->select_words = FALSE;
4045 if (event->button == 1)
4047 if (!GTK_WIDGET_HAS_FOCUS (widget))
4049 label->in_click = TRUE;
4050 gtk_widget_grab_focus (widget);
4051 label->in_click = FALSE;
4054 if (event->type == GDK_3BUTTON_PRESS)
4056 gtk_label_select_region_index (label, 0, strlen (label->text));
4060 if (event->type == GDK_2BUTTON_PRESS)
4062 info->select_words = TRUE;
4063 gtk_label_select_word (label);
4067 get_layout_index (label, event->x, event->y, &index);
4069 min = MIN (info->selection_anchor, info->selection_end);
4070 max = MAX (info->selection_anchor, info->selection_end);
4072 if ((info->selection_anchor != info->selection_end) &&
4073 (event->state & GDK_SHIFT_MASK))
4075 /* extend (same as motion) */
4076 min = MIN (min, index);
4077 max = MAX (max, index);
4079 /* ensure the anchor is opposite index */
4087 gtk_label_select_region_index (label, min, max);
4091 if (event->type == GDK_3BUTTON_PRESS)
4092 gtk_label_select_region_index (label, 0, strlen (label->text));
4093 else if (event->type == GDK_2BUTTON_PRESS)
4094 gtk_label_select_word (label);
4095 else if (min < max && min <= index && index <= max)
4097 info->in_drag = TRUE;
4098 info->drag_start_x = event->x;
4099 info->drag_start_y = event->y;
4102 /* start a replacement */
4103 gtk_label_select_region_index (label, index, index);
4108 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4110 gtk_label_do_popup (label, event);
4118 gtk_label_button_release (GtkWidget *widget,
4119 GdkEventButton *event)
4122 GtkLabel *label = GTK_LABEL (widget);
4123 GtkLabelSelectionInfo *info = label->select_info;
4133 get_layout_index (label, event->x, event->y, &index);
4134 gtk_label_select_region_index (label, index, index);
4139 if (event->button != 1)
4142 if (info->active_link &&
4143 info->selection_anchor == info->selection_end &&
4146 emit_activate_link (label, info->active_link);
4147 info->link_clicked = 0;
4152 /* The goal here is to return TRUE iff we ate the
4153 * button press to start selecting.
4159 drag_begin_cb (GtkWidget *widget,
4160 GdkDragContext *context,
4164 GdkPixmap *pixmap = NULL;
4166 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
4168 label = GTK_LABEL (widget);
4170 if ((label->select_info->selection_anchor !=
4171 label->select_info->selection_end) &&
4177 start = MIN (label->select_info->selection_anchor,
4178 label->select_info->selection_end);
4179 end = MAX (label->select_info->selection_anchor,
4180 label->select_info->selection_end);
4182 len = strlen (label->text);
4190 pixmap = _gtk_text_util_create_drag_icon (widget,
4191 label->text + start,
4196 gtk_drag_set_icon_pixmap (context,
4197 gdk_drawable_get_colormap (pixmap),
4202 gtk_drag_set_icon_default (context);
4205 g_object_unref (pixmap);
4209 gtk_label_motion (GtkWidget *widget,
4210 GdkEventMotion *event)
4212 GtkLabel *label = GTK_LABEL (widget);
4213 GtkLabelSelectionInfo *info = label->select_info;
4220 if (info->links && !info->in_drag)
4224 gboolean found = FALSE;
4226 if (info->selection_anchor == info->selection_end)
4228 gdk_window_get_pointer (event->window, &x, &y, NULL);
4229 if (get_layout_index (label, x, y, &index))
4231 for (l = info->links; l != NULL; l = l->next)
4234 if (index >= link->start && index <= link->end)
4245 if (info->active_link != link)
4247 info->link_clicked = 0;
4248 info->active_link = link;
4249 gtk_label_update_cursor (label);
4250 gtk_widget_queue_draw (widget);
4255 if (info->active_link != NULL)
4257 info->link_clicked = 0;
4258 info->active_link = NULL;
4259 gtk_label_update_cursor (label);
4260 gtk_widget_queue_draw (widget);
4265 if (!info->selectable)
4268 if ((event->state & GDK_BUTTON1_MASK) == 0)
4271 gdk_window_get_pointer (info->window, &x, &y, NULL);
4275 if (gtk_drag_check_threshold (widget,
4278 event->x, event->y))
4280 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
4282 gtk_target_list_add_text_targets (target_list, 0);
4284 g_signal_connect (widget, "drag-begin",
4285 G_CALLBACK (drag_begin_cb), NULL);
4286 gtk_drag_begin (widget, target_list,
4288 1, (GdkEvent *)event);
4290 info->in_drag = FALSE;
4292 gtk_target_list_unref (target_list);
4297 get_layout_index (label, x, y, &index);
4299 if (info->select_words)
4302 gint old_min, old_max;
4305 min = gtk_label_move_backward_word (label, index);
4306 max = gtk_label_move_forward_word (label, index);
4308 anchor = info->selection_anchor;
4309 end = info->selection_end;
4311 old_min = MIN (anchor, end);
4312 old_max = MAX (anchor, end);
4319 else if (old_max < max)
4324 else if (anchor == old_min)
4335 gtk_label_select_region_index (label, anchor, end);
4338 gtk_label_select_region_index (label, info->selection_anchor, index);
4345 gtk_label_leave_notify (GtkWidget *widget,
4346 GdkEventCrossing *event)
4348 GtkLabel *label = GTK_LABEL (widget);
4350 if (label->select_info)
4352 label->select_info->active_link = NULL;
4353 gtk_label_update_cursor (label);
4354 gtk_widget_queue_draw (widget);
4357 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event)
4358 return GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event (widget, event);
4364 gtk_label_create_window (GtkLabel *label)
4367 GdkWindowAttr attributes;
4368 gint attributes_mask;
4370 g_assert (label->select_info);
4371 g_assert (GTK_WIDGET_REALIZED (label));
4373 if (label->select_info->window)
4376 widget = GTK_WIDGET (label);
4378 attributes.x = widget->allocation.x;
4379 attributes.y = widget->allocation.y;
4380 attributes.width = widget->allocation.width;
4381 attributes.height = widget->allocation.height;
4382 attributes.window_type = GDK_WINDOW_CHILD;
4383 attributes.wclass = GDK_INPUT_ONLY;
4384 attributes.override_redirect = TRUE;
4385 attributes.event_mask = gtk_widget_get_events (widget) |
4386 GDK_BUTTON_PRESS_MASK |
4387 GDK_BUTTON_RELEASE_MASK |
4388 GDK_LEAVE_NOTIFY_MASK |
4389 GDK_BUTTON_MOTION_MASK |
4390 GDK_POINTER_MOTION_MASK |
4391 GDK_POINTER_MOTION_HINT_MASK;
4392 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
4393 if (GTK_WIDGET_IS_SENSITIVE (widget))
4395 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
4397 attributes_mask |= GDK_WA_CURSOR;
4401 label->select_info->window = gdk_window_new (widget->window,
4402 &attributes, attributes_mask);
4403 gdk_window_set_user_data (label->select_info->window, widget);
4405 if (attributes_mask & GDK_WA_CURSOR)
4406 gdk_cursor_unref (attributes.cursor);
4410 gtk_label_destroy_window (GtkLabel *label)
4412 g_assert (label->select_info);
4414 if (label->select_info->window == NULL)
4417 gdk_window_set_user_data (label->select_info->window, NULL);
4418 gdk_window_destroy (label->select_info->window);
4419 label->select_info->window = NULL;
4423 gtk_label_ensure_select_info (GtkLabel *label)
4425 if (label->select_info == NULL)
4427 label->select_info = g_new0 (GtkLabelSelectionInfo, 1);
4429 GTK_WIDGET_SET_FLAGS (label, GTK_CAN_FOCUS);
4431 if (GTK_WIDGET_REALIZED (label))
4432 gtk_label_create_window (label);
4434 if (GTK_WIDGET_MAPPED (label))
4435 gdk_window_show (label->select_info->window);
4440 gtk_label_clear_select_info (GtkLabel *label)
4442 if (label->select_info == NULL)
4445 if (!label->select_info->selectable && !label->select_info->links)
4447 gtk_label_destroy_window (label);
4449 g_free (label->select_info);
4450 label->select_info = NULL;
4452 GTK_WIDGET_UNSET_FLAGS (label, GTK_CAN_FOCUS);
4457 * gtk_label_set_selectable:
4458 * @label: a #GtkLabel
4459 * @setting: %TRUE to allow selecting text in the label
4461 * Selectable labels allow the user to select text from the label, for
4465 gtk_label_set_selectable (GtkLabel *label,
4468 gboolean old_setting;
4470 g_return_if_fail (GTK_IS_LABEL (label));
4472 setting = setting != FALSE;
4473 old_setting = label->select_info && label->select_info->selectable;
4477 gtk_label_ensure_select_info (label);
4478 label->select_info->selectable = TRUE;
4479 gtk_label_update_cursor (label);
4485 /* unselect, to give up the selection */
4486 gtk_label_select_region (label, 0, 0);
4488 label->select_info->selectable = FALSE;
4489 gtk_label_clear_select_info (label);
4490 gtk_label_update_cursor (label);
4493 if (setting != old_setting)
4495 g_object_freeze_notify (G_OBJECT (label));
4496 g_object_notify (G_OBJECT (label), "selectable");
4497 g_object_notify (G_OBJECT (label), "cursor-position");
4498 g_object_notify (G_OBJECT (label), "selection-bound");
4499 g_object_thaw_notify (G_OBJECT (label));
4500 gtk_widget_queue_draw (GTK_WIDGET (label));
4505 * gtk_label_get_selectable:
4506 * @label: a #GtkLabel
4508 * Gets the value set by gtk_label_set_selectable().
4510 * Return value: %TRUE if the user can copy text from the label
4513 gtk_label_get_selectable (GtkLabel *label)
4515 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
4517 return label->select_info && label->select_info->selectable;
4521 free_angle (gpointer angle)
4523 g_slice_free (gdouble, angle);
4527 * gtk_label_set_angle:
4528 * @label: a #GtkLabel
4529 * @angle: the angle that the baseline of the label makes with
4530 * the horizontal, in degrees, measured counterclockwise
4532 * Sets the angle of rotation for the label. An angle of 90 reads from
4533 * from bottom to top, an angle of 270, from top to bottom. The angle
4534 * setting for the label is ignored if the label is selectable,
4535 * wrapped, or ellipsized.
4540 gtk_label_set_angle (GtkLabel *label,
4543 gdouble *label_angle;
4545 g_return_if_fail (GTK_IS_LABEL (label));
4547 label_angle = (gdouble *)g_object_get_qdata (G_OBJECT (label), quark_angle);
4551 label_angle = g_slice_new (gdouble);
4553 g_object_set_qdata_full (G_OBJECT (label), quark_angle,
4554 label_angle, free_angle);
4557 /* Canonicalize to [0,360]. We don't canonicalize 360 to 0, because
4558 * double property ranges are inclusive, and changing 360 to 0 would
4559 * make a property editor behave strangely.
4561 if (angle < 0 || angle > 360.0)
4562 angle = angle - 360. * floor (angle / 360.);
4564 if (*label_angle != angle)
4566 *label_angle = angle;
4568 gtk_label_clear_layout (label);
4569 gtk_widget_queue_resize (GTK_WIDGET (label));
4571 g_object_notify (G_OBJECT (label), "angle");
4576 * gtk_label_get_angle:
4577 * @label: a #GtkLabel
4579 * Gets the angle of rotation for the label. See
4580 * gtk_label_set_angle().
4582 * Return value: the angle of rotation for the label
4587 gtk_label_get_angle (GtkLabel *label)
4591 g_return_val_if_fail (GTK_IS_LABEL (label), 0.0);
4593 angle = (gdouble *)g_object_get_qdata (G_OBJECT (label), quark_angle);
4602 gtk_label_set_selection_text (GtkLabel *label,
4603 GtkSelectionData *selection_data)
4605 if ((label->select_info->selection_anchor !=
4606 label->select_info->selection_end) &&
4612 start = MIN (label->select_info->selection_anchor,
4613 label->select_info->selection_end);
4614 end = MAX (label->select_info->selection_anchor,
4615 label->select_info->selection_end);
4617 len = strlen (label->text);
4625 gtk_selection_data_set_text (selection_data,
4626 label->text + start,
4632 gtk_label_drag_data_get (GtkWidget *widget,
4633 GdkDragContext *context,
4634 GtkSelectionData *selection_data,
4638 gtk_label_set_selection_text (GTK_LABEL (widget), selection_data);
4642 get_text_callback (GtkClipboard *clipboard,
4643 GtkSelectionData *selection_data,
4645 gpointer user_data_or_owner)
4647 gtk_label_set_selection_text (GTK_LABEL (user_data_or_owner), selection_data);
4651 clear_text_callback (GtkClipboard *clipboard,
4652 gpointer user_data_or_owner)
4656 label = GTK_LABEL (user_data_or_owner);
4658 if (label->select_info)
4660 label->select_info->selection_anchor = label->select_info->selection_end;
4662 gtk_widget_queue_draw (GTK_WIDGET (label));
4667 gtk_label_select_region_index (GtkLabel *label,
4671 g_return_if_fail (GTK_IS_LABEL (label));
4673 if (label->select_info && label->select_info->selectable)
4675 GtkClipboard *clipboard;
4677 if (label->select_info->selection_anchor == anchor_index &&
4678 label->select_info->selection_end == end_index)
4681 label->select_info->selection_anchor = anchor_index;
4682 label->select_info->selection_end = end_index;
4684 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label),
4685 GDK_SELECTION_PRIMARY);
4687 if (anchor_index != end_index)
4689 GtkTargetList *list;
4690 GtkTargetEntry *targets;
4693 list = gtk_target_list_new (NULL, 0);
4694 gtk_target_list_add_text_targets (list, 0);
4695 targets = gtk_target_table_new_from_list (list, &n_targets);
4697 gtk_clipboard_set_with_owner (clipboard,
4700 clear_text_callback,
4703 gtk_target_table_free (targets, n_targets);
4704 gtk_target_list_unref (list);
4708 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (label))
4709 gtk_clipboard_clear (clipboard);
4712 gtk_widget_queue_draw (GTK_WIDGET (label));
4714 g_object_freeze_notify (G_OBJECT (label));
4715 g_object_notify (G_OBJECT (label), "cursor-position");
4716 g_object_notify (G_OBJECT (label), "selection-bound");
4717 g_object_thaw_notify (G_OBJECT (label));
4722 * gtk_label_select_region:
4723 * @label: a #GtkLabel
4724 * @start_offset: start offset (in characters not bytes)
4725 * @end_offset: end offset (in characters not bytes)
4727 * Selects a range of characters in the label, if the label is selectable.
4728 * See gtk_label_set_selectable(). If the label is not selectable,
4729 * this function has no effect. If @start_offset or
4730 * @end_offset are -1, then the end of the label will be substituted.
4733 gtk_label_select_region (GtkLabel *label,
4737 g_return_if_fail (GTK_IS_LABEL (label));
4739 if (label->text && label->select_info)
4741 if (start_offset < 0)
4742 start_offset = g_utf8_strlen (label->text, -1);
4745 end_offset = g_utf8_strlen (label->text, -1);
4747 gtk_label_select_region_index (label,
4748 g_utf8_offset_to_pointer (label->text, start_offset) - label->text,
4749 g_utf8_offset_to_pointer (label->text, end_offset) - label->text);
4754 * gtk_label_get_selection_bounds:
4755 * @label: a #GtkLabel
4756 * @start: return location for start of selection, as a character offset
4757 * @end: return location for end of selection, as a character offset
4759 * Gets the selected range of characters in the label, returning %TRUE
4760 * if there's a selection.
4762 * Return value: %TRUE if selection is non-empty
4765 gtk_label_get_selection_bounds (GtkLabel *label,
4769 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
4771 if (label->select_info == NULL)
4773 /* not a selectable label */
4783 gint start_index, end_index;
4784 gint start_offset, end_offset;
4787 start_index = MIN (label->select_info->selection_anchor,
4788 label->select_info->selection_end);
4789 end_index = MAX (label->select_info->selection_anchor,
4790 label->select_info->selection_end);
4792 len = strlen (label->text);
4794 if (end_index > len)
4797 if (start_index > len)
4800 start_offset = g_utf8_strlen (label->text, start_index);
4801 end_offset = g_utf8_strlen (label->text, end_index);
4803 if (start_offset > end_offset)
4805 gint tmp = start_offset;
4806 start_offset = end_offset;
4811 *start = start_offset;
4816 return start_offset != end_offset;
4822 * gtk_label_get_layout:
4823 * @label: a #GtkLabel
4825 * Gets the #PangoLayout used to display the label.
4826 * The layout is useful to e.g. convert text positions to
4827 * pixel positions, in combination with gtk_label_get_layout_offsets().
4828 * The returned layout is owned by the label so need not be
4829 * freed by the caller.
4831 * Return value: the #PangoLayout for this label
4834 gtk_label_get_layout (GtkLabel *label)
4836 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
4838 gtk_label_ensure_layout (label);
4840 return label->layout;
4844 * gtk_label_get_layout_offsets:
4845 * @label: a #GtkLabel
4846 * @x: location to store X offset of layout, or %NULL
4847 * @y: location to store Y offset of layout, or %NULL
4849 * Obtains the coordinates where the label will draw the #PangoLayout
4850 * representing the text in the label; useful to convert mouse events
4851 * into coordinates inside the #PangoLayout, e.g. to take some action
4852 * if some part of the label is clicked. Of course you will need to
4853 * create a #GtkEventBox to receive the events, and pack the label
4854 * inside it, since labels are a #GTK_NO_WINDOW widget. Remember
4855 * when using the #PangoLayout functions you need to convert to
4856 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
4859 gtk_label_get_layout_offsets (GtkLabel *label,
4863 g_return_if_fail (GTK_IS_LABEL (label));
4865 gtk_label_ensure_layout (label);
4867 get_layout_location (label, x, y);
4871 * gtk_label_set_use_markup:
4872 * @label: a #GtkLabel
4873 * @setting: %TRUE if the label's text should be parsed for markup.
4875 * Sets whether the text of the label contains markup in <link
4876 * linkend="PangoMarkupFormat">Pango's text markup
4877 * language</link>. See gtk_label_set_markup().
4880 gtk_label_set_use_markup (GtkLabel *label,
4883 g_return_if_fail (GTK_IS_LABEL (label));
4885 gtk_label_set_use_markup_internal (label, setting);
4886 gtk_label_recalculate (label);
4890 * gtk_label_get_use_markup:
4891 * @label: a #GtkLabel
4893 * Returns whether the label's text is interpreted as marked up with
4894 * the <link linkend="PangoMarkupFormat">Pango text markup
4895 * language</link>. See gtk_label_set_use_markup ().
4897 * Return value: %TRUE if the label's text will be parsed for markup.
4900 gtk_label_get_use_markup (GtkLabel *label)
4902 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
4904 return label->use_markup;
4908 * gtk_label_set_use_underline:
4909 * @label: a #GtkLabel
4910 * @setting: %TRUE if underlines in the text indicate mnemonics
4912 * If true, an underline in the text indicates the next character should be
4913 * used for the mnemonic accelerator key.
4916 gtk_label_set_use_underline (GtkLabel *label,
4919 g_return_if_fail (GTK_IS_LABEL (label));
4921 gtk_label_set_use_underline_internal (label, setting);
4922 gtk_label_recalculate (label);
4926 * gtk_label_get_use_underline:
4927 * @label: a #GtkLabel
4929 * Returns whether an embedded underline in the label indicates a
4930 * mnemonic. See gtk_label_set_use_underline().
4932 * Return value: %TRUE whether an embedded underline in the label indicates
4933 * the mnemonic accelerator keys.
4936 gtk_label_get_use_underline (GtkLabel *label)
4938 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
4940 return label->use_underline;
4944 * gtk_label_set_single_line_mode:
4945 * @label: a #GtkLabel
4946 * @single_line_mode: %TRUE if the label should be in single line mode
4948 * Sets whether the label is in single line mode.
4953 gtk_label_set_single_line_mode (GtkLabel *label,
4954 gboolean single_line_mode)
4956 g_return_if_fail (GTK_IS_LABEL (label));
4958 single_line_mode = single_line_mode != FALSE;
4960 if (label->single_line_mode != single_line_mode)
4962 label->single_line_mode = single_line_mode;
4964 gtk_label_clear_layout (label);
4965 gtk_widget_queue_resize (GTK_WIDGET (label));
4967 g_object_notify (G_OBJECT (label), "single-line-mode");
4972 * gtk_label_get_single_line_mode:
4973 * @label: a #GtkLabel
4975 * Returns whether the label is in single line mode.
4977 * Return value: %TRUE when the label is in single line mode.
4982 gtk_label_get_single_line_mode (GtkLabel *label)
4984 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
4986 return label->single_line_mode;
4989 /* Compute the X position for an offset that corresponds to the "more important
4990 * cursor position for that offset. We use this when trying to guess to which
4991 * end of the selection we should go to when the user hits the left or
4995 get_better_cursor (GtkLabel *label,
5000 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5001 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5002 PangoDirection cursor_direction = get_cursor_direction (label);
5003 gboolean split_cursor;
5004 PangoRectangle strong_pos, weak_pos;
5006 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5007 "gtk-split-cursor", &split_cursor,
5010 gtk_label_ensure_layout (label);
5012 pango_layout_get_cursor_pos (label->layout, index,
5013 &strong_pos, &weak_pos);
5017 *x = strong_pos.x / PANGO_SCALE;
5018 *y = strong_pos.y / PANGO_SCALE;
5022 if (keymap_direction == cursor_direction)
5024 *x = strong_pos.x / PANGO_SCALE;
5025 *y = strong_pos.y / PANGO_SCALE;
5029 *x = weak_pos.x / PANGO_SCALE;
5030 *y = weak_pos.y / PANGO_SCALE;
5037 gtk_label_move_logically (GtkLabel *label,
5041 gint offset = g_utf8_pointer_to_offset (label->text,
5042 label->text + start);
5046 PangoLogAttr *log_attrs;
5050 gtk_label_ensure_layout (label);
5052 length = g_utf8_strlen (label->text, -1);
5054 pango_layout_get_log_attrs (label->layout, &log_attrs, &n_attrs);
5056 while (count > 0 && offset < length)
5060 while (offset < length && !log_attrs[offset].is_cursor_position);
5064 while (count < 0 && offset > 0)
5068 while (offset > 0 && !log_attrs[offset].is_cursor_position);
5076 return g_utf8_offset_to_pointer (label->text, offset) - label->text;
5080 gtk_label_move_visually (GtkLabel *label,
5090 int new_index, new_trailing;
5091 gboolean split_cursor;
5094 gtk_label_ensure_layout (label);
5096 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5097 "gtk-split-cursor", &split_cursor,
5104 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5105 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5107 strong = keymap_direction == get_cursor_direction (label);
5112 pango_layout_move_cursor_visually (label->layout, strong, index, 0, 1, &new_index, &new_trailing);
5117 pango_layout_move_cursor_visually (label->layout, strong, index, 0, -1, &new_index, &new_trailing);
5121 if (new_index < 0 || new_index == G_MAXINT)
5126 while (new_trailing--)
5127 index = g_utf8_next_char (label->text + new_index) - label->text;
5134 gtk_label_move_forward_word (GtkLabel *label,
5137 gint new_pos = g_utf8_pointer_to_offset (label->text,
5138 label->text + start);
5141 length = g_utf8_strlen (label->text, -1);
5142 if (new_pos < length)
5144 PangoLogAttr *log_attrs;
5147 gtk_label_ensure_layout (label);
5149 pango_layout_get_log_attrs (label->layout, &log_attrs, &n_attrs);
5151 /* Find the next word end */
5153 while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
5159 return g_utf8_offset_to_pointer (label->text, new_pos) - label->text;
5164 gtk_label_move_backward_word (GtkLabel *label,
5167 gint new_pos = g_utf8_pointer_to_offset (label->text,
5168 label->text + start);
5172 PangoLogAttr *log_attrs;
5175 gtk_label_ensure_layout (label);
5177 pango_layout_get_log_attrs (label->layout, &log_attrs, &n_attrs);
5181 /* Find the previous word beginning */
5182 while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
5188 return g_utf8_offset_to_pointer (label->text, new_pos) - label->text;
5192 gtk_label_move_cursor (GtkLabel *label,
5193 GtkMovementStep step,
5195 gboolean extend_selection)
5200 if (label->select_info == NULL)
5203 old_pos = new_pos = label->select_info->selection_end;
5205 if (label->select_info->selection_end != label->select_info->selection_anchor &&
5208 /* If we have a current selection and aren't extending it, move to the
5209 * start/or end of the selection as appropriate
5213 case GTK_MOVEMENT_VISUAL_POSITIONS:
5216 gint anchor_x, anchor_y;
5217 gboolean end_is_left;
5219 get_better_cursor (label, label->select_info->selection_end, &end_x, &end_y);
5220 get_better_cursor (label, label->select_info->selection_anchor, &anchor_x, &anchor_y);
5222 end_is_left = (end_y < anchor_y) || (end_y == anchor_y && end_x < anchor_x);
5225 new_pos = end_is_left ? label->select_info->selection_end : label->select_info->selection_anchor;
5227 new_pos = !end_is_left ? label->select_info->selection_end : label->select_info->selection_anchor;
5230 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5231 case GTK_MOVEMENT_WORDS:
5233 new_pos = MIN (label->select_info->selection_end, label->select_info->selection_anchor);
5235 new_pos = MAX (label->select_info->selection_end, label->select_info->selection_anchor);
5237 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5238 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5239 case GTK_MOVEMENT_BUFFER_ENDS:
5240 /* FIXME: Can do better here */
5241 new_pos = count < 0 ? 0 : strlen (label->text);
5243 case GTK_MOVEMENT_DISPLAY_LINES:
5244 case GTK_MOVEMENT_PARAGRAPHS:
5245 case GTK_MOVEMENT_PAGES:
5246 case GTK_MOVEMENT_HORIZONTAL_PAGES:
5254 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5255 new_pos = gtk_label_move_logically (label, new_pos, count);
5257 case GTK_MOVEMENT_VISUAL_POSITIONS:
5258 new_pos = gtk_label_move_visually (label, new_pos, count);
5259 if (new_pos == old_pos)
5261 if (!extend_selection)
5263 if (!gtk_widget_keynav_failed (GTK_WIDGET (label),
5265 GTK_DIR_RIGHT : GTK_DIR_LEFT))
5267 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
5270 gtk_widget_child_focus (toplevel,
5272 GTK_DIR_RIGHT : GTK_DIR_LEFT);
5277 gtk_widget_error_bell (GTK_WIDGET (label));
5281 case GTK_MOVEMENT_WORDS:
5284 new_pos = gtk_label_move_forward_word (label, new_pos);
5289 new_pos = gtk_label_move_backward_word (label, new_pos);
5292 if (new_pos == old_pos)
5293 gtk_widget_error_bell (GTK_WIDGET (label));
5295 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5296 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5297 case GTK_MOVEMENT_BUFFER_ENDS:
5298 /* FIXME: Can do better here */
5299 new_pos = count < 0 ? 0 : strlen (label->text);
5300 if (new_pos == old_pos)
5301 gtk_widget_error_bell (GTK_WIDGET (label));
5303 case GTK_MOVEMENT_DISPLAY_LINES:
5304 case GTK_MOVEMENT_PARAGRAPHS:
5305 case GTK_MOVEMENT_PAGES:
5306 case GTK_MOVEMENT_HORIZONTAL_PAGES:
5311 if (extend_selection)
5312 gtk_label_select_region_index (label,
5313 label->select_info->selection_anchor,
5316 gtk_label_select_region_index (label, new_pos, new_pos);
5320 gtk_label_copy_clipboard (GtkLabel *label)
5322 if (label->text && label->select_info)
5326 GtkClipboard *clipboard;
5328 start = MIN (label->select_info->selection_anchor,
5329 label->select_info->selection_end);
5330 end = MAX (label->select_info->selection_anchor,
5331 label->select_info->selection_end);
5333 len = strlen (label->text);
5341 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
5344 gtk_clipboard_set_text (clipboard, label->text + start, end - start);
5349 link = gtk_label_get_focus_link (label);
5351 gtk_clipboard_set_text (clipboard, link->uri, -1);
5357 gtk_label_select_all (GtkLabel *label)
5359 gtk_label_select_region_index (label, 0, strlen (label->text));
5362 /* Quick hack of a popup menu
5365 activate_cb (GtkWidget *menuitem,
5368 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
5369 g_signal_emit_by_name (label, signal);
5373 append_action_signal (GtkLabel *label,
5375 const gchar *stock_id,
5376 const gchar *signal,
5379 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
5381 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
5382 g_signal_connect (menuitem, "activate",
5383 G_CALLBACK (activate_cb), label);
5385 gtk_widget_set_sensitive (menuitem, sensitive);
5387 gtk_widget_show (menuitem);
5388 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5392 popup_menu_detach (GtkWidget *attach_widget,
5395 GtkLabel *label = GTK_LABEL (attach_widget);
5397 if (label->select_info)
5398 label->select_info->popup_menu = NULL;
5402 popup_position_func (GtkMenu *menu,
5413 label = GTK_LABEL (user_data);
5414 widget = GTK_WIDGET (label);
5416 g_return_if_fail (GTK_WIDGET_REALIZED (label));
5418 screen = gtk_widget_get_screen (widget);
5419 gdk_window_get_origin (widget->window, x, y);
5421 *x += widget->allocation.x;
5422 *y += widget->allocation.y;
5424 gtk_widget_size_request (GTK_WIDGET (menu), &req);
5426 *x += widget->allocation.width / 2;
5427 *y += widget->allocation.height;
5429 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
5430 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
5434 open_link_activate_cb (GtkMenuItem *menu_item,
5439 link = gtk_label_get_current_link (label);
5442 emit_activate_link (label, link);
5446 copy_link_activate_cb (GtkMenuItem *menu_item,
5449 GtkClipboard *clipboard;
5452 uri = gtk_label_get_current_uri (label);
5455 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
5456 gtk_clipboard_set_text (clipboard, uri, -1);
5461 gtk_label_popup_menu (GtkWidget *widget)
5463 gtk_label_do_popup (GTK_LABEL (widget), NULL);
5469 gtk_label_do_popup (GtkLabel *label,
5470 GdkEventButton *event)
5472 GtkWidget *menuitem;
5475 gboolean have_selection;
5478 if (!label->select_info)
5481 if (label->select_info->popup_menu)
5482 gtk_widget_destroy (label->select_info->popup_menu);
5484 label->select_info->popup_menu = menu = gtk_menu_new ();
5486 gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (label), popup_menu_detach);
5489 label->select_info->selection_anchor != label->select_info->selection_end;
5493 if (label->select_info->link_clicked)
5494 link = label->select_info->active_link;
5499 link = gtk_label_get_focus_link (label);
5501 if (!have_selection && link)
5504 menuitem = gtk_image_menu_item_new_with_mnemonic (_("_Open Link"));
5505 gtk_widget_show (menuitem);
5506 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5508 g_signal_connect (G_OBJECT (menuitem), "activate",
5509 G_CALLBACK (open_link_activate_cb), label);
5511 image = gtk_image_new_from_stock (GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_MENU);
5512 gtk_widget_show (image);
5513 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
5515 /* Copy Link Address */
5516 menuitem = gtk_image_menu_item_new_with_mnemonic (_("Copy _Link Address"));
5517 gtk_widget_show (menuitem);
5518 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5520 g_signal_connect (G_OBJECT (menuitem), "activate",
5521 G_CALLBACK (copy_link_activate_cb), label);
5523 image = gtk_image_new_from_stock (GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
5524 gtk_widget_show (image);
5525 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
5529 append_action_signal (label, menu, GTK_STOCK_CUT, "cut-clipboard", FALSE);
5530 append_action_signal (label, menu, GTK_STOCK_COPY, "copy-clipboard", have_selection);
5531 append_action_signal (label, menu, GTK_STOCK_PASTE, "paste-clipboard", FALSE);
5533 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
5534 gtk_widget_set_sensitive (menuitem, FALSE);
5535 gtk_widget_show (menuitem);
5536 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5538 menuitem = gtk_separator_menu_item_new ();
5539 gtk_widget_show (menuitem);
5540 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5542 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
5543 g_signal_connect_swapped (menuitem, "activate",
5544 G_CALLBACK (gtk_label_select_all), label);
5545 gtk_widget_show (menuitem);
5546 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5549 g_signal_emit (label, signals[POPULATE_POPUP], 0, menu);
5552 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
5554 event->button, event->time);
5557 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
5558 popup_position_func, label,
5559 0, gtk_get_current_event_time ());
5560 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
5565 gtk_label_clear_links (GtkLabel *label)
5567 if (!label->select_info)
5570 g_list_foreach (label->select_info->links, (GFunc)link_free, NULL);
5571 g_list_free (label->select_info->links);
5572 label->select_info->links = NULL;
5573 label->select_info->active_link = NULL;
5577 gtk_label_rescan_links (GtkLabel *label)
5579 PangoLayout *layout = label->layout;
5580 PangoAttrList *attlist;
5581 PangoAttrIterator *iter;
5584 if (!label->select_info)
5587 attlist = pango_layout_get_attributes (layout);
5589 if (attlist == NULL)
5592 iter = pango_attr_list_get_iterator (attlist);
5594 links = label->select_info->links;
5598 PangoAttribute *underline;
5599 PangoAttribute *color;
5601 underline = pango_attr_iterator_get (iter, PANGO_ATTR_UNDERLINE);
5602 color = pango_attr_iterator_get (iter, PANGO_ATTR_FOREGROUND);
5604 if (underline != NULL && color != NULL)
5607 PangoRectangle start_pos;
5608 PangoRectangle end_pos;
5611 pango_attr_iterator_range (iter, &start, &end);
5612 pango_layout_index_to_pos (layout, start, &start_pos);
5613 pango_layout_index_to_pos (layout, end, &end_pos);
5617 g_warning ("Ran out of links");
5621 links = links->next;
5622 link->start = start;
5625 } while (pango_attr_iterator_next (iter));
5627 pango_attr_iterator_destroy (iter);
5631 gtk_label_activate_link (GtkLabel *label,
5634 GtkWidget *widget = GTK_WIDGET (label);
5635 GError *error = NULL;
5637 if (!gtk_show_uri (gtk_widget_get_screen (widget),
5638 uri, gtk_get_current_event_time (), &error))
5640 g_warning ("Unable to show '%s': %s", uri, error->message);
5641 g_error_free (error);
5648 emit_activate_link (GtkLabel *label,
5653 g_signal_emit (label, signals[ACTIVATE_LINK], 0, link->uri, &handled);
5654 if (handled && !link->visited)
5656 link->visited = TRUE;
5657 /* FIXME: shouldn't have to redo everything here */
5658 gtk_label_recalculate (label);
5663 gtk_label_activate_current_link (GtkLabel *label)
5666 GtkWidget *widget = GTK_WIDGET (label);
5668 link = gtk_label_get_focus_link (label);
5672 emit_activate_link (label, link);
5676 GtkWidget *toplevel;
5679 toplevel = gtk_widget_get_toplevel (widget);
5680 if (GTK_IS_WINDOW (toplevel))
5682 window = GTK_WINDOW (toplevel);
5685 window->default_widget != widget &&
5686 !(widget == window->focus_widget &&
5687 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
5688 gtk_window_activate_default (window);
5693 static GtkLabelLink *
5694 gtk_label_get_current_link (GtkLabel *label)
5698 if (!label->select_info)
5701 if (label->select_info->link_clicked)
5702 link = label->select_info->active_link;
5704 link = gtk_label_get_focus_link (label);
5710 * gtk_label_get_current_uri:
5711 * @label: a #GtkLabel
5713 * Returns the URI for the currently active link in the label.
5714 * The active link is the one under the mouse pointer or, in a
5715 * selectable label, the link in which the text cursor is currently
5718 * This function is intended for use in a #GtkLabel::link-activate handler
5719 * or for use in a #GtkWidget::query-tooltip handler.
5721 * Returns: the currently active URI. The string is owned by GTK+ and must
5722 * not be freed or modified.
5726 G_CONST_RETURN gchar *
5727 gtk_label_get_current_uri (GtkLabel *label)
5730 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
5732 link = gtk_label_get_current_link (label);
5742 gtk_label_query_tooltip (GtkWidget *widget,
5745 gboolean keyboard_tip,
5746 GtkTooltip *tooltip)
5748 GtkLabel *label = GTK_LABEL (widget);
5749 GtkLabelSelectionInfo *info = label->select_info;
5753 if (info && info->links)
5757 if (info->selection_anchor == info->selection_end)
5758 index = info->selection_anchor;
5762 if (!get_layout_index (label, x, y, &index))
5768 for (l = info->links; l != NULL; l = l->next)
5770 GtkLabelLink *link = l->data;
5771 if (index >= link->start && index <= link->end)
5775 gtk_tooltip_set_markup (tooltip, link->title);
5784 return GTK_WIDGET_CLASS (gtk_label_parent_class)->query_tooltip (widget,
5791 #define __GTK_LABEL_C__
5792 #include "gtkaliasdef.c"