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 y = floor (widget->allocation.y + (gint)misc->ypad
3240 + MAX (((widget->allocation.height - widget->requisition.height) * misc->yalign),
3251 draw_insertion_cursor (GtkLabel *label,
3252 GdkRectangle *cursor_location,
3253 gboolean is_primary,
3254 PangoDirection direction,
3255 gboolean draw_arrow)
3257 GtkWidget *widget = GTK_WIDGET (label);
3258 GtkTextDirection text_dir;
3260 if (direction == PANGO_DIRECTION_LTR)
3261 text_dir = GTK_TEXT_DIR_LTR;
3263 text_dir = GTK_TEXT_DIR_RTL;
3265 gtk_draw_insertion_cursor (widget, widget->window, &(widget->allocation),
3267 is_primary, text_dir, draw_arrow);
3270 static PangoDirection
3271 get_cursor_direction (GtkLabel *label)
3275 g_assert (label->select_info);
3277 gtk_label_ensure_layout (label);
3279 for (l = pango_layout_get_lines_readonly (label->layout); l; l = l->next)
3281 PangoLayoutLine *line = l->data;
3283 /* If label->select_info->selection_end is at the very end of
3284 * the line, we don't know if the cursor is on this line or
3285 * the next without looking ahead at the next line. (End
3286 * of paragraph is different from line break.) But it's
3287 * definitely in this paragraph, which is good enough
3288 * to figure out the resolved direction.
3290 if (line->start_index + line->length >= label->select_info->selection_end)
3291 return line->resolved_dir;
3294 return PANGO_DIRECTION_LTR;
3298 gtk_label_draw_cursor (GtkLabel *label, gint xoffset, gint yoffset)
3300 if (label->select_info == NULL)
3303 if (GTK_WIDGET_DRAWABLE (label))
3305 GtkWidget *widget = GTK_WIDGET (label);
3307 PangoDirection keymap_direction;
3308 PangoDirection cursor_direction;
3309 PangoRectangle strong_pos, weak_pos;
3310 gboolean split_cursor;
3311 PangoRectangle *cursor1 = NULL;
3312 PangoRectangle *cursor2 = NULL;
3313 GdkRectangle cursor_location;
3314 PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
3315 PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
3317 keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gtk_widget_get_display (widget)));
3318 cursor_direction = get_cursor_direction (label);
3320 gtk_label_ensure_layout (label);
3322 pango_layout_get_cursor_pos (label->layout, label->select_info->selection_end,
3323 &strong_pos, &weak_pos);
3325 g_object_get (gtk_widget_get_settings (widget),
3326 "gtk-split-cursor", &split_cursor,
3329 dir1 = cursor_direction;
3333 cursor1 = &strong_pos;
3335 if (strong_pos.x != weak_pos.x ||
3336 strong_pos.y != weak_pos.y)
3338 dir2 = (cursor_direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
3339 cursor2 = &weak_pos;
3344 if (keymap_direction == cursor_direction)
3345 cursor1 = &strong_pos;
3347 cursor1 = &weak_pos;
3350 cursor_location.x = xoffset + PANGO_PIXELS (cursor1->x);
3351 cursor_location.y = yoffset + PANGO_PIXELS (cursor1->y);
3352 cursor_location.width = 0;
3353 cursor_location.height = PANGO_PIXELS (cursor1->height);
3355 draw_insertion_cursor (label,
3356 &cursor_location, TRUE, dir1,
3357 dir2 != PANGO_DIRECTION_NEUTRAL);
3359 if (dir2 != PANGO_DIRECTION_NEUTRAL)
3361 cursor_location.x = xoffset + PANGO_PIXELS (cursor2->x);
3362 cursor_location.y = yoffset + PANGO_PIXELS (cursor2->y);
3363 cursor_location.width = 0;
3364 cursor_location.height = PANGO_PIXELS (cursor2->height);
3366 draw_insertion_cursor (label,
3367 &cursor_location, FALSE, dir2,
3373 static GtkLabelLink *
3374 gtk_label_get_focus_link (GtkLabel *label)
3376 GtkLabelSelectionInfo *info = label->select_info;
3382 if (info->selection_anchor != info->selection_end)
3385 for (l = info->links; l; l = l->next)
3387 GtkLabelLink *link = l->data;
3388 if (link->start <= info->selection_anchor &&
3389 info->selection_anchor <= link->end)
3397 gtk_label_expose (GtkWidget *widget,
3398 GdkEventExpose *event)
3400 GtkLabel *label = GTK_LABEL (widget);
3401 GtkLabelSelectionInfo *info = label->select_info;
3404 gtk_label_ensure_layout (label);
3406 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget) &&
3407 label->text && (*label->text != '\0'))
3409 get_layout_location (label, &x, &y);
3411 gtk_paint_layout (widget->style,
3413 GTK_WIDGET_STATE (widget),
3422 (info->selection_anchor != info->selection_end))
3428 range[0] = info->selection_anchor;
3429 range[1] = info->selection_end;
3431 if (range[0] > range[1])
3433 gint tmp = range[0];
3434 range[0] = range[1];
3438 clip = gdk_pango_layout_get_clip_region (label->layout,
3442 gdk_region_intersect (clip, event->region);
3444 /* FIXME should use gtk_paint, but it can't use a clip
3448 gdk_gc_set_clip_region (widget->style->black_gc, clip);
3451 state = GTK_STATE_SELECTED;
3452 if (!GTK_WIDGET_HAS_FOCUS (widget))
3453 state = GTK_STATE_ACTIVE;
3455 gdk_draw_layout_with_colors (widget->window,
3456 widget->style->black_gc,
3459 &widget->style->text[state],
3460 &widget->style->base[state]);
3462 gdk_gc_set_clip_region (widget->style->black_gc, NULL);
3463 gdk_region_destroy (clip);
3467 GtkLabelLink *focus_link;
3468 GtkLabelLink *active_link;
3472 GdkColor *text_color;
3473 GdkColor *base_color;
3474 GdkColor *link_color;
3475 GdkColor *visited_link_color;
3477 if (info->selectable && GTK_WIDGET_HAS_FOCUS (widget))
3478 gtk_label_draw_cursor (label, x, y);
3480 focus_link = gtk_label_get_focus_link (label);
3481 active_link = info->active_link;
3485 range[0] = active_link->start;
3486 range[1] = active_link->end;
3488 clip = gdk_pango_layout_get_clip_region (label->layout,
3492 gdk_gc_set_clip_region (widget->style->black_gc, clip);
3494 gtk_label_get_link_colors (widget, &link_color, &visited_link_color);
3495 if (active_link->visited)
3496 text_color = visited_link_color;
3498 text_color = link_color;
3499 if (info->link_clicked)
3500 base_color = &widget->style->base[GTK_STATE_ACTIVE];
3502 base_color = &widget->style->base[GTK_STATE_PRELIGHT];
3503 gdk_draw_layout_with_colors (widget->window,
3504 widget->style->black_gc,
3509 gdk_color_free (link_color);
3510 gdk_color_free (visited_link_color);
3512 gdk_gc_set_clip_region (widget->style->black_gc, NULL);
3513 gdk_region_destroy (clip);
3516 if (focus_link && GTK_WIDGET_HAS_FOCUS (widget))
3518 range[0] = focus_link->start;
3519 range[1] = focus_link->end;
3521 clip = gdk_pango_layout_get_clip_region (label->layout,
3525 gdk_region_get_clipbox (clip, &rect);
3527 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
3528 &event->area, widget, "label",
3529 rect.x, rect.y, rect.width, rect.height);
3531 gdk_region_destroy (clip);
3540 gtk_label_set_uline_text_internal (GtkLabel *label,
3543 guint accel_key = GDK_VoidSymbol;
3548 gchar *dest, *pattern_dest;
3549 gboolean underscore;
3551 g_return_if_fail (GTK_IS_LABEL (label));
3552 g_return_if_fail (str != NULL);
3554 /* Split text into the base text and a separate pattern
3558 new_str = g_new (gchar, strlen (str) + 1);
3559 pattern = g_new (gchar, g_utf8_strlen (str, -1) + 1);
3568 pattern_dest = pattern;
3575 c = g_utf8_get_char (src);
3576 if (c == (gunichar)-1)
3578 g_warning ("Invalid input string");
3583 next_src = g_utf8_next_char (src);
3588 *pattern_dest++ = ' ';
3591 *pattern_dest++ = '_';
3592 if (accel_key == GDK_VoidSymbol)
3593 accel_key = gdk_keyval_to_lower (gdk_unicode_to_keyval (c));
3596 while (src < next_src)
3610 while (src < next_src)
3613 *pattern_dest++ = ' ';
3620 gtk_label_set_text_internal (label, new_str);
3621 gtk_label_set_pattern_internal (label, pattern);
3625 label->mnemonic_keyval = accel_key;
3629 gtk_label_parse_uline (GtkLabel *label,
3634 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_VoidSymbol);
3635 g_return_val_if_fail (str != NULL, GDK_VoidSymbol);
3637 g_object_freeze_notify (G_OBJECT (label));
3639 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
3640 gtk_label_set_use_markup_internal (label, FALSE);
3641 gtk_label_set_use_underline_internal (label, TRUE);
3643 gtk_label_recalculate (label);
3645 keyval = label->mnemonic_keyval;
3646 if (keyval != GDK_VoidSymbol)
3648 label->mnemonic_keyval = GDK_VoidSymbol;
3649 gtk_label_setup_mnemonic (label, keyval);
3650 g_object_notify (G_OBJECT (label), "mnemonic-keyval");
3653 g_object_thaw_notify (G_OBJECT (label));
3659 * gtk_label_set_text_with_mnemonic:
3660 * @label: a #GtkLabel
3663 * Sets the label's text from the string @str.
3664 * If characters in @str are preceded by an underscore, they are underlined
3665 * indicating that they represent a keyboard accelerator called a mnemonic.
3666 * The mnemonic key can be used to activate another widget, chosen
3667 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
3670 gtk_label_set_text_with_mnemonic (GtkLabel *label,
3673 g_return_if_fail (GTK_IS_LABEL (label));
3674 g_return_if_fail (str != NULL);
3676 g_object_freeze_notify (G_OBJECT (label));
3678 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
3679 gtk_label_set_use_markup_internal (label, FALSE);
3680 gtk_label_set_use_underline_internal (label, TRUE);
3682 gtk_label_recalculate (label);
3684 g_object_thaw_notify (G_OBJECT (label));
3688 gtk_label_realize (GtkWidget *widget)
3692 label = GTK_LABEL (widget);
3694 GTK_WIDGET_CLASS (gtk_label_parent_class)->realize (widget);
3696 if (label->select_info)
3697 gtk_label_create_window (label);
3701 gtk_label_unrealize (GtkWidget *widget)
3705 label = GTK_LABEL (widget);
3707 if (label->select_info)
3708 gtk_label_destroy_window (label);
3710 GTK_WIDGET_CLASS (gtk_label_parent_class)->unrealize (widget);
3714 gtk_label_map (GtkWidget *widget)
3718 label = GTK_LABEL (widget);
3720 GTK_WIDGET_CLASS (gtk_label_parent_class)->map (widget);
3722 if (label->select_info)
3723 gdk_window_show (label->select_info->window);
3727 gtk_label_unmap (GtkWidget *widget)
3731 label = GTK_LABEL (widget);
3733 if (label->select_info)
3734 gdk_window_hide (label->select_info->window);
3736 GTK_WIDGET_CLASS (gtk_label_parent_class)->unmap (widget);
3740 window_to_layout_coords (GtkLabel *label,
3747 widget = GTK_WIDGET (label);
3749 /* get layout location in widget->window coords */
3750 get_layout_location (label, &lx, &ly);
3754 *x += widget->allocation.x; /* go to widget->window */
3755 *x -= lx; /* go to layout */
3760 *y += widget->allocation.y; /* go to widget->window */
3761 *y -= ly; /* go to layout */
3767 layout_to_window_coords (GtkLabel *label,
3774 widget = GTK_WIDGET (label);
3776 /* get layout location in widget->window coords */
3777 get_layout_location (label, &lx, &ly);
3781 *x += lx; /* go to widget->window */
3782 *x -= widget->allocation.x; /* go to selection window */
3787 *y += ly; /* go to widget->window */
3788 *y -= widget->allocation.y; /* go to selection window */
3794 get_layout_index (GtkLabel *label,
3800 const gchar *cluster;
3801 const gchar *cluster_end;
3805 gtk_label_ensure_layout (label);
3807 window_to_layout_coords (label, &x, &y);
3812 if (pango_layout_xy_to_index (label->layout,
3816 cluster = label->text + *index;
3817 cluster_end = cluster;
3820 cluster_end = g_utf8_next_char (cluster_end);
3824 *index += (cluster_end - cluster);
3833 gtk_label_select_word (GtkLabel *label)
3837 gint start_index = gtk_label_move_backward_word (label, label->select_info->selection_end);
3838 gint end_index = gtk_label_move_forward_word (label, label->select_info->selection_end);
3840 min = MIN (label->select_info->selection_anchor,
3841 label->select_info->selection_end);
3842 max = MAX (label->select_info->selection_anchor,
3843 label->select_info->selection_end);
3845 min = MIN (min, start_index);
3846 max = MAX (max, end_index);
3848 gtk_label_select_region_index (label, min, max);
3852 gtk_label_grab_focus (GtkWidget *widget)
3855 gboolean select_on_focus;
3858 label = GTK_LABEL (widget);
3860 if (label->select_info == NULL)
3863 GTK_WIDGET_CLASS (gtk_label_parent_class)->grab_focus (widget);
3865 if (label->select_info->selectable)
3867 g_object_get (gtk_widget_get_settings (widget),
3868 "gtk-label-select-on-focus",
3872 if (select_on_focus && !label->in_click)
3873 gtk_label_select_region (label, 0, -1);
3877 if (label->select_info->links && !label->in_click)
3879 link = label->select_info->links->data;
3880 label->select_info->selection_anchor = link->start;
3881 label->select_info->selection_end = link->start;
3887 gtk_label_focus (GtkWidget *widget,
3888 GtkDirectionType direction)
3890 GtkLabel *label = GTK_LABEL (widget);
3891 GtkLabelSelectionInfo *info = label->select_info;
3892 GtkLabelLink *focus_link;
3895 if (!gtk_widget_is_focus (widget))
3897 gtk_widget_grab_focus (widget);
3900 focus_link = gtk_label_get_focus_link (label);
3901 if (focus_link && direction == GTK_DIR_TAB_BACKWARD)
3903 l = g_list_last (info->links);
3904 focus_link = l->data;
3905 info->selection_anchor = focus_link->start;
3906 info->selection_end = focus_link->start;
3916 if (info->selectable)
3920 if (info->selection_anchor != info->selection_end)
3923 index = info->selection_anchor;
3925 if (direction == GTK_DIR_TAB_FORWARD)
3926 for (l = info->links; l; l = l->next)
3928 GtkLabelLink *link = l->data;
3930 if (link->start > index)
3932 gtk_label_select_region_index (label, link->start, link->start);
3936 else if (direction == GTK_DIR_TAB_BACKWARD)
3937 for (l = g_list_last (info->links); l; l = l->prev)
3939 GtkLabelLink *link = l->data;
3941 if (link->end < index)
3943 gtk_label_select_region_index (label, link->start, link->start);
3952 focus_link = gtk_label_get_focus_link (label);
3955 case GTK_DIR_TAB_FORWARD:
3958 l = g_list_find (info->links, focus_link);
3965 case GTK_DIR_TAB_BACKWARD:
3968 l = g_list_find (info->links, focus_link);
3972 l = g_list_last (info->links);
3981 focus_link = l->data;
3982 info->selection_anchor = focus_link->start;
3983 info->selection_end = focus_link->start;
3984 gtk_widget_queue_draw (widget);
3996 gtk_label_button_press (GtkWidget *widget,
3997 GdkEventButton *event)
3999 GtkLabel *label = GTK_LABEL (widget);
4000 GtkLabelSelectionInfo *info = label->select_info;
4007 if (info->active_link)
4009 if (event->button == 1)
4011 info->link_clicked = 1;
4012 gtk_widget_queue_draw (widget);
4014 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4016 info->link_clicked = 1;
4017 gtk_label_do_popup (label, event);
4022 if (!info->selectable)
4025 info->in_drag = FALSE;
4026 info->select_words = FALSE;
4028 if (event->button == 1)
4030 if (!GTK_WIDGET_HAS_FOCUS (widget))
4032 label->in_click = TRUE;
4033 gtk_widget_grab_focus (widget);
4034 label->in_click = FALSE;
4037 if (event->type == GDK_3BUTTON_PRESS)
4039 gtk_label_select_region_index (label, 0, strlen (label->text));
4043 if (event->type == GDK_2BUTTON_PRESS)
4045 info->select_words = TRUE;
4046 gtk_label_select_word (label);
4050 get_layout_index (label, event->x, event->y, &index);
4052 min = MIN (info->selection_anchor, info->selection_end);
4053 max = MAX (info->selection_anchor, info->selection_end);
4055 if ((info->selection_anchor != info->selection_end) &&
4056 (event->state & GDK_SHIFT_MASK))
4058 /* extend (same as motion) */
4059 min = MIN (min, index);
4060 max = MAX (max, index);
4062 /* ensure the anchor is opposite index */
4070 gtk_label_select_region_index (label, min, max);
4074 if (event->type == GDK_3BUTTON_PRESS)
4075 gtk_label_select_region_index (label, 0, strlen (label->text));
4076 else if (event->type == GDK_2BUTTON_PRESS)
4077 gtk_label_select_word (label);
4078 else if (min < max && min <= index && index <= max)
4080 info->in_drag = TRUE;
4081 info->drag_start_x = event->x;
4082 info->drag_start_y = event->y;
4085 /* start a replacement */
4086 gtk_label_select_region_index (label, index, index);
4091 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4093 gtk_label_do_popup (label, event);
4101 gtk_label_button_release (GtkWidget *widget,
4102 GdkEventButton *event)
4105 GtkLabel *label = GTK_LABEL (widget);
4106 GtkLabelSelectionInfo *info = label->select_info;
4116 get_layout_index (label, event->x, event->y, &index);
4117 gtk_label_select_region_index (label, index, index);
4122 if (event->button != 1)
4125 if (info->active_link &&
4126 info->selection_anchor == info->selection_end &&
4129 emit_activate_link (label, info->active_link);
4130 info->link_clicked = 0;
4135 /* The goal here is to return TRUE iff we ate the
4136 * button press to start selecting.
4142 drag_begin_cb (GtkWidget *widget,
4143 GdkDragContext *context,
4147 GdkPixmap *pixmap = NULL;
4149 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
4151 label = GTK_LABEL (widget);
4153 if ((label->select_info->selection_anchor !=
4154 label->select_info->selection_end) &&
4160 start = MIN (label->select_info->selection_anchor,
4161 label->select_info->selection_end);
4162 end = MAX (label->select_info->selection_anchor,
4163 label->select_info->selection_end);
4165 len = strlen (label->text);
4173 pixmap = _gtk_text_util_create_drag_icon (widget,
4174 label->text + start,
4179 gtk_drag_set_icon_pixmap (context,
4180 gdk_drawable_get_colormap (pixmap),
4185 gtk_drag_set_icon_default (context);
4188 g_object_unref (pixmap);
4192 gtk_label_motion (GtkWidget *widget,
4193 GdkEventMotion *event)
4195 GtkLabel *label = GTK_LABEL (widget);
4196 GtkLabelSelectionInfo *info = label->select_info;
4203 if (info->links && !info->in_drag)
4207 gboolean found = FALSE;
4209 if (info->selection_anchor == info->selection_end)
4211 gdk_window_get_pointer (event->window, &x, &y, NULL);
4212 if (get_layout_index (label, x, y, &index))
4214 for (l = info->links; l != NULL; l = l->next)
4217 if (index >= link->start && index <= link->end)
4228 if (info->active_link != link)
4230 info->link_clicked = 0;
4231 info->active_link = link;
4232 gtk_label_update_cursor (label);
4233 gtk_widget_queue_draw (widget);
4238 if (info->active_link != NULL)
4240 info->link_clicked = 0;
4241 info->active_link = NULL;
4242 gtk_label_update_cursor (label);
4243 gtk_widget_queue_draw (widget);
4248 if (!info->selectable)
4251 if ((event->state & GDK_BUTTON1_MASK) == 0)
4254 gdk_window_get_pointer (info->window, &x, &y, NULL);
4258 if (gtk_drag_check_threshold (widget,
4261 event->x, event->y))
4263 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
4265 gtk_target_list_add_text_targets (target_list, 0);
4267 g_signal_connect (widget, "drag-begin",
4268 G_CALLBACK (drag_begin_cb), NULL);
4269 gtk_drag_begin (widget, target_list,
4271 1, (GdkEvent *)event);
4273 info->in_drag = FALSE;
4275 gtk_target_list_unref (target_list);
4280 get_layout_index (label, x, y, &index);
4282 if (info->select_words)
4285 gint old_min, old_max;
4288 min = gtk_label_move_backward_word (label, index);
4289 max = gtk_label_move_forward_word (label, index);
4291 anchor = info->selection_anchor;
4292 end = info->selection_end;
4294 old_min = MIN (anchor, end);
4295 old_max = MAX (anchor, end);
4302 else if (old_max < max)
4307 else if (anchor == old_min)
4318 gtk_label_select_region_index (label, anchor, end);
4321 gtk_label_select_region_index (label, info->selection_anchor, index);
4328 gtk_label_leave_notify (GtkWidget *widget,
4329 GdkEventCrossing *event)
4331 GtkLabel *label = GTK_LABEL (widget);
4333 if (label->select_info)
4335 label->select_info->active_link = NULL;
4336 gtk_label_update_cursor (label);
4337 gtk_widget_queue_draw (widget);
4340 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event)
4341 return GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event (widget, event);
4347 gtk_label_create_window (GtkLabel *label)
4350 GdkWindowAttr attributes;
4351 gint attributes_mask;
4353 g_assert (label->select_info);
4354 g_assert (GTK_WIDGET_REALIZED (label));
4356 if (label->select_info->window)
4359 widget = GTK_WIDGET (label);
4361 attributes.x = widget->allocation.x;
4362 attributes.y = widget->allocation.y;
4363 attributes.width = widget->allocation.width;
4364 attributes.height = widget->allocation.height;
4365 attributes.window_type = GDK_WINDOW_CHILD;
4366 attributes.wclass = GDK_INPUT_ONLY;
4367 attributes.override_redirect = TRUE;
4368 attributes.event_mask = gtk_widget_get_events (widget) |
4369 GDK_BUTTON_PRESS_MASK |
4370 GDK_BUTTON_RELEASE_MASK |
4371 GDK_LEAVE_NOTIFY_MASK |
4372 GDK_BUTTON_MOTION_MASK |
4373 GDK_POINTER_MOTION_MASK |
4374 GDK_POINTER_MOTION_HINT_MASK;
4375 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
4376 if (GTK_WIDGET_IS_SENSITIVE (widget))
4378 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
4380 attributes_mask |= GDK_WA_CURSOR;
4384 label->select_info->window = gdk_window_new (widget->window,
4385 &attributes, attributes_mask);
4386 gdk_window_set_user_data (label->select_info->window, widget);
4388 if (attributes_mask & GDK_WA_CURSOR)
4389 gdk_cursor_unref (attributes.cursor);
4393 gtk_label_destroy_window (GtkLabel *label)
4395 g_assert (label->select_info);
4397 if (label->select_info->window == NULL)
4400 gdk_window_set_user_data (label->select_info->window, NULL);
4401 gdk_window_destroy (label->select_info->window);
4402 label->select_info->window = NULL;
4406 gtk_label_ensure_select_info (GtkLabel *label)
4408 if (label->select_info == NULL)
4410 label->select_info = g_new0 (GtkLabelSelectionInfo, 1);
4412 GTK_WIDGET_SET_FLAGS (label, GTK_CAN_FOCUS);
4414 if (GTK_WIDGET_REALIZED (label))
4415 gtk_label_create_window (label);
4417 if (GTK_WIDGET_MAPPED (label))
4418 gdk_window_show (label->select_info->window);
4423 gtk_label_clear_select_info (GtkLabel *label)
4425 if (label->select_info == NULL)
4428 if (!label->select_info->selectable && !label->select_info->links)
4430 gtk_label_destroy_window (label);
4432 g_free (label->select_info);
4433 label->select_info = NULL;
4435 GTK_WIDGET_UNSET_FLAGS (label, GTK_CAN_FOCUS);
4440 * gtk_label_set_selectable:
4441 * @label: a #GtkLabel
4442 * @setting: %TRUE to allow selecting text in the label
4444 * Selectable labels allow the user to select text from the label, for
4448 gtk_label_set_selectable (GtkLabel *label,
4451 gboolean old_setting;
4453 g_return_if_fail (GTK_IS_LABEL (label));
4455 setting = setting != FALSE;
4456 old_setting = label->select_info && label->select_info->selectable;
4460 gtk_label_ensure_select_info (label);
4461 label->select_info->selectable = TRUE;
4462 gtk_label_update_cursor (label);
4468 /* unselect, to give up the selection */
4469 gtk_label_select_region (label, 0, 0);
4471 label->select_info->selectable = FALSE;
4472 gtk_label_clear_select_info (label);
4473 gtk_label_update_cursor (label);
4476 if (setting != old_setting)
4478 g_object_freeze_notify (G_OBJECT (label));
4479 g_object_notify (G_OBJECT (label), "selectable");
4480 g_object_notify (G_OBJECT (label), "cursor-position");
4481 g_object_notify (G_OBJECT (label), "selection-bound");
4482 g_object_thaw_notify (G_OBJECT (label));
4483 gtk_widget_queue_draw (GTK_WIDGET (label));
4488 * gtk_label_get_selectable:
4489 * @label: a #GtkLabel
4491 * Gets the value set by gtk_label_set_selectable().
4493 * Return value: %TRUE if the user can copy text from the label
4496 gtk_label_get_selectable (GtkLabel *label)
4498 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
4500 return label->select_info && label->select_info->selectable;
4504 free_angle (gpointer angle)
4506 g_slice_free (gdouble, angle);
4510 * gtk_label_set_angle:
4511 * @label: a #GtkLabel
4512 * @angle: the angle that the baseline of the label makes with
4513 * the horizontal, in degrees, measured counterclockwise
4515 * Sets the angle of rotation for the label. An angle of 90 reads from
4516 * from bottom to top, an angle of 270, from top to bottom. The angle
4517 * setting for the label is ignored if the label is selectable,
4518 * wrapped, or ellipsized.
4523 gtk_label_set_angle (GtkLabel *label,
4526 gdouble *label_angle;
4528 g_return_if_fail (GTK_IS_LABEL (label));
4530 label_angle = (gdouble *)g_object_get_qdata (G_OBJECT (label), quark_angle);
4534 label_angle = g_slice_new (gdouble);
4536 g_object_set_qdata_full (G_OBJECT (label), quark_angle,
4537 label_angle, free_angle);
4540 /* Canonicalize to [0,360]. We don't canonicalize 360 to 0, because
4541 * double property ranges are inclusive, and changing 360 to 0 would
4542 * make a property editor behave strangely.
4544 if (angle < 0 || angle > 360.0)
4545 angle = angle - 360. * floor (angle / 360.);
4547 if (*label_angle != angle)
4549 *label_angle = angle;
4551 gtk_label_clear_layout (label);
4552 gtk_widget_queue_resize (GTK_WIDGET (label));
4554 g_object_notify (G_OBJECT (label), "angle");
4559 * gtk_label_get_angle:
4560 * @label: a #GtkLabel
4562 * Gets the angle of rotation for the label. See
4563 * gtk_label_set_angle().
4565 * Return value: the angle of rotation for the label
4570 gtk_label_get_angle (GtkLabel *label)
4574 g_return_val_if_fail (GTK_IS_LABEL (label), 0.0);
4576 angle = (gdouble *)g_object_get_qdata (G_OBJECT (label), quark_angle);
4585 gtk_label_set_selection_text (GtkLabel *label,
4586 GtkSelectionData *selection_data)
4588 if ((label->select_info->selection_anchor !=
4589 label->select_info->selection_end) &&
4595 start = MIN (label->select_info->selection_anchor,
4596 label->select_info->selection_end);
4597 end = MAX (label->select_info->selection_anchor,
4598 label->select_info->selection_end);
4600 len = strlen (label->text);
4608 gtk_selection_data_set_text (selection_data,
4609 label->text + start,
4615 gtk_label_drag_data_get (GtkWidget *widget,
4616 GdkDragContext *context,
4617 GtkSelectionData *selection_data,
4621 gtk_label_set_selection_text (GTK_LABEL (widget), selection_data);
4625 get_text_callback (GtkClipboard *clipboard,
4626 GtkSelectionData *selection_data,
4628 gpointer user_data_or_owner)
4630 gtk_label_set_selection_text (GTK_LABEL (user_data_or_owner), selection_data);
4634 clear_text_callback (GtkClipboard *clipboard,
4635 gpointer user_data_or_owner)
4639 label = GTK_LABEL (user_data_or_owner);
4641 if (label->select_info)
4643 label->select_info->selection_anchor = label->select_info->selection_end;
4645 gtk_widget_queue_draw (GTK_WIDGET (label));
4650 gtk_label_select_region_index (GtkLabel *label,
4654 g_return_if_fail (GTK_IS_LABEL (label));
4656 if (label->select_info && label->select_info->selectable)
4658 GtkClipboard *clipboard;
4660 if (label->select_info->selection_anchor == anchor_index &&
4661 label->select_info->selection_end == end_index)
4664 label->select_info->selection_anchor = anchor_index;
4665 label->select_info->selection_end = end_index;
4667 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label),
4668 GDK_SELECTION_PRIMARY);
4670 if (anchor_index != end_index)
4672 GtkTargetList *list;
4673 GtkTargetEntry *targets;
4676 list = gtk_target_list_new (NULL, 0);
4677 gtk_target_list_add_text_targets (list, 0);
4678 targets = gtk_target_table_new_from_list (list, &n_targets);
4680 gtk_clipboard_set_with_owner (clipboard,
4683 clear_text_callback,
4686 gtk_target_table_free (targets, n_targets);
4687 gtk_target_list_unref (list);
4691 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (label))
4692 gtk_clipboard_clear (clipboard);
4695 gtk_widget_queue_draw (GTK_WIDGET (label));
4697 g_object_freeze_notify (G_OBJECT (label));
4698 g_object_notify (G_OBJECT (label), "cursor-position");
4699 g_object_notify (G_OBJECT (label), "selection-bound");
4700 g_object_thaw_notify (G_OBJECT (label));
4705 * gtk_label_select_region:
4706 * @label: a #GtkLabel
4707 * @start_offset: start offset (in characters not bytes)
4708 * @end_offset: end offset (in characters not bytes)
4710 * Selects a range of characters in the label, if the label is selectable.
4711 * See gtk_label_set_selectable(). If the label is not selectable,
4712 * this function has no effect. If @start_offset or
4713 * @end_offset are -1, then the end of the label will be substituted.
4716 gtk_label_select_region (GtkLabel *label,
4720 g_return_if_fail (GTK_IS_LABEL (label));
4722 if (label->text && label->select_info)
4724 if (start_offset < 0)
4725 start_offset = g_utf8_strlen (label->text, -1);
4728 end_offset = g_utf8_strlen (label->text, -1);
4730 gtk_label_select_region_index (label,
4731 g_utf8_offset_to_pointer (label->text, start_offset) - label->text,
4732 g_utf8_offset_to_pointer (label->text, end_offset) - label->text);
4737 * gtk_label_get_selection_bounds:
4738 * @label: a #GtkLabel
4739 * @start: return location for start of selection, as a character offset
4740 * @end: return location for end of selection, as a character offset
4742 * Gets the selected range of characters in the label, returning %TRUE
4743 * if there's a selection.
4745 * Return value: %TRUE if selection is non-empty
4748 gtk_label_get_selection_bounds (GtkLabel *label,
4752 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
4754 if (label->select_info == NULL)
4756 /* not a selectable label */
4766 gint start_index, end_index;
4767 gint start_offset, end_offset;
4770 start_index = MIN (label->select_info->selection_anchor,
4771 label->select_info->selection_end);
4772 end_index = MAX (label->select_info->selection_anchor,
4773 label->select_info->selection_end);
4775 len = strlen (label->text);
4777 if (end_index > len)
4780 if (start_index > len)
4783 start_offset = g_utf8_strlen (label->text, start_index);
4784 end_offset = g_utf8_strlen (label->text, end_index);
4786 if (start_offset > end_offset)
4788 gint tmp = start_offset;
4789 start_offset = end_offset;
4794 *start = start_offset;
4799 return start_offset != end_offset;
4805 * gtk_label_get_layout:
4806 * @label: a #GtkLabel
4808 * Gets the #PangoLayout used to display the label.
4809 * The layout is useful to e.g. convert text positions to
4810 * pixel positions, in combination with gtk_label_get_layout_offsets().
4811 * The returned layout is owned by the label so need not be
4812 * freed by the caller.
4814 * Return value: the #PangoLayout for this label
4817 gtk_label_get_layout (GtkLabel *label)
4819 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
4821 gtk_label_ensure_layout (label);
4823 return label->layout;
4827 * gtk_label_get_layout_offsets:
4828 * @label: a #GtkLabel
4829 * @x: location to store X offset of layout, or %NULL
4830 * @y: location to store Y offset of layout, or %NULL
4832 * Obtains the coordinates where the label will draw the #PangoLayout
4833 * representing the text in the label; useful to convert mouse events
4834 * into coordinates inside the #PangoLayout, e.g. to take some action
4835 * if some part of the label is clicked. Of course you will need to
4836 * create a #GtkEventBox to receive the events, and pack the label
4837 * inside it, since labels are a #GTK_NO_WINDOW widget. Remember
4838 * when using the #PangoLayout functions you need to convert to
4839 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
4842 gtk_label_get_layout_offsets (GtkLabel *label,
4846 g_return_if_fail (GTK_IS_LABEL (label));
4848 gtk_label_ensure_layout (label);
4850 get_layout_location (label, x, y);
4854 * gtk_label_set_use_markup:
4855 * @label: a #GtkLabel
4856 * @setting: %TRUE if the label's text should be parsed for markup.
4858 * Sets whether the text of the label contains markup in <link
4859 * linkend="PangoMarkupFormat">Pango's text markup
4860 * language</link>. See gtk_label_set_markup().
4863 gtk_label_set_use_markup (GtkLabel *label,
4866 g_return_if_fail (GTK_IS_LABEL (label));
4868 gtk_label_set_use_markup_internal (label, setting);
4869 gtk_label_recalculate (label);
4873 * gtk_label_get_use_markup:
4874 * @label: a #GtkLabel
4876 * Returns whether the label's text is interpreted as marked up with
4877 * the <link linkend="PangoMarkupFormat">Pango text markup
4878 * language</link>. See gtk_label_set_use_markup ().
4880 * Return value: %TRUE if the label's text will be parsed for markup.
4883 gtk_label_get_use_markup (GtkLabel *label)
4885 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
4887 return label->use_markup;
4891 * gtk_label_set_use_underline:
4892 * @label: a #GtkLabel
4893 * @setting: %TRUE if underlines in the text indicate mnemonics
4895 * If true, an underline in the text indicates the next character should be
4896 * used for the mnemonic accelerator key.
4899 gtk_label_set_use_underline (GtkLabel *label,
4902 g_return_if_fail (GTK_IS_LABEL (label));
4904 gtk_label_set_use_underline_internal (label, setting);
4905 gtk_label_recalculate (label);
4909 * gtk_label_get_use_underline:
4910 * @label: a #GtkLabel
4912 * Returns whether an embedded underline in the label indicates a
4913 * mnemonic. See gtk_label_set_use_underline().
4915 * Return value: %TRUE whether an embedded underline in the label indicates
4916 * the mnemonic accelerator keys.
4919 gtk_label_get_use_underline (GtkLabel *label)
4921 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
4923 return label->use_underline;
4927 * gtk_label_set_single_line_mode:
4928 * @label: a #GtkLabel
4929 * @single_line_mode: %TRUE if the label should be in single line mode
4931 * Sets whether the label is in single line mode.
4936 gtk_label_set_single_line_mode (GtkLabel *label,
4937 gboolean single_line_mode)
4939 g_return_if_fail (GTK_IS_LABEL (label));
4941 single_line_mode = single_line_mode != FALSE;
4943 if (label->single_line_mode != single_line_mode)
4945 label->single_line_mode = single_line_mode;
4947 gtk_label_clear_layout (label);
4948 gtk_widget_queue_resize (GTK_WIDGET (label));
4950 g_object_notify (G_OBJECT (label), "single-line-mode");
4955 * gtk_label_get_single_line_mode:
4956 * @label: a #GtkLabel
4958 * Returns whether the label is in single line mode.
4960 * Return value: %TRUE when the label is in single line mode.
4965 gtk_label_get_single_line_mode (GtkLabel *label)
4967 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
4969 return label->single_line_mode;
4972 /* Compute the X position for an offset that corresponds to the "more important
4973 * cursor position for that offset. We use this when trying to guess to which
4974 * end of the selection we should go to when the user hits the left or
4978 get_better_cursor (GtkLabel *label,
4983 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
4984 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
4985 PangoDirection cursor_direction = get_cursor_direction (label);
4986 gboolean split_cursor;
4987 PangoRectangle strong_pos, weak_pos;
4989 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
4990 "gtk-split-cursor", &split_cursor,
4993 gtk_label_ensure_layout (label);
4995 pango_layout_get_cursor_pos (label->layout, index,
4996 &strong_pos, &weak_pos);
5000 *x = strong_pos.x / PANGO_SCALE;
5001 *y = strong_pos.y / PANGO_SCALE;
5005 if (keymap_direction == cursor_direction)
5007 *x = strong_pos.x / PANGO_SCALE;
5008 *y = strong_pos.y / PANGO_SCALE;
5012 *x = weak_pos.x / PANGO_SCALE;
5013 *y = weak_pos.y / PANGO_SCALE;
5020 gtk_label_move_logically (GtkLabel *label,
5024 gint offset = g_utf8_pointer_to_offset (label->text,
5025 label->text + start);
5029 PangoLogAttr *log_attrs;
5033 gtk_label_ensure_layout (label);
5035 length = g_utf8_strlen (label->text, -1);
5037 pango_layout_get_log_attrs (label->layout, &log_attrs, &n_attrs);
5039 while (count > 0 && offset < length)
5043 while (offset < length && !log_attrs[offset].is_cursor_position);
5047 while (count < 0 && offset > 0)
5051 while (offset > 0 && !log_attrs[offset].is_cursor_position);
5059 return g_utf8_offset_to_pointer (label->text, offset) - label->text;
5063 gtk_label_move_visually (GtkLabel *label,
5073 int new_index, new_trailing;
5074 gboolean split_cursor;
5077 gtk_label_ensure_layout (label);
5079 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5080 "gtk-split-cursor", &split_cursor,
5087 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5088 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5090 strong = keymap_direction == get_cursor_direction (label);
5095 pango_layout_move_cursor_visually (label->layout, strong, index, 0, 1, &new_index, &new_trailing);
5100 pango_layout_move_cursor_visually (label->layout, strong, index, 0, -1, &new_index, &new_trailing);
5104 if (new_index < 0 || new_index == G_MAXINT)
5109 while (new_trailing--)
5110 index = g_utf8_next_char (label->text + new_index) - label->text;
5117 gtk_label_move_forward_word (GtkLabel *label,
5120 gint new_pos = g_utf8_pointer_to_offset (label->text,
5121 label->text + start);
5124 length = g_utf8_strlen (label->text, -1);
5125 if (new_pos < length)
5127 PangoLogAttr *log_attrs;
5130 gtk_label_ensure_layout (label);
5132 pango_layout_get_log_attrs (label->layout, &log_attrs, &n_attrs);
5134 /* Find the next word end */
5136 while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
5142 return g_utf8_offset_to_pointer (label->text, new_pos) - label->text;
5147 gtk_label_move_backward_word (GtkLabel *label,
5150 gint new_pos = g_utf8_pointer_to_offset (label->text,
5151 label->text + start);
5155 PangoLogAttr *log_attrs;
5158 gtk_label_ensure_layout (label);
5160 pango_layout_get_log_attrs (label->layout, &log_attrs, &n_attrs);
5164 /* Find the previous word beginning */
5165 while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
5171 return g_utf8_offset_to_pointer (label->text, new_pos) - label->text;
5175 gtk_label_move_cursor (GtkLabel *label,
5176 GtkMovementStep step,
5178 gboolean extend_selection)
5183 if (label->select_info == NULL)
5186 old_pos = new_pos = label->select_info->selection_end;
5188 if (label->select_info->selection_end != label->select_info->selection_anchor &&
5191 /* If we have a current selection and aren't extending it, move to the
5192 * start/or end of the selection as appropriate
5196 case GTK_MOVEMENT_VISUAL_POSITIONS:
5199 gint anchor_x, anchor_y;
5200 gboolean end_is_left;
5202 get_better_cursor (label, label->select_info->selection_end, &end_x, &end_y);
5203 get_better_cursor (label, label->select_info->selection_anchor, &anchor_x, &anchor_y);
5205 end_is_left = (end_y < anchor_y) || (end_y == anchor_y && end_x < anchor_x);
5208 new_pos = end_is_left ? label->select_info->selection_end : label->select_info->selection_anchor;
5210 new_pos = !end_is_left ? label->select_info->selection_end : label->select_info->selection_anchor;
5213 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5214 case GTK_MOVEMENT_WORDS:
5216 new_pos = MIN (label->select_info->selection_end, label->select_info->selection_anchor);
5218 new_pos = MAX (label->select_info->selection_end, label->select_info->selection_anchor);
5220 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5221 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5222 case GTK_MOVEMENT_BUFFER_ENDS:
5223 /* FIXME: Can do better here */
5224 new_pos = count < 0 ? 0 : strlen (label->text);
5226 case GTK_MOVEMENT_DISPLAY_LINES:
5227 case GTK_MOVEMENT_PARAGRAPHS:
5228 case GTK_MOVEMENT_PAGES:
5229 case GTK_MOVEMENT_HORIZONTAL_PAGES:
5237 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5238 new_pos = gtk_label_move_logically (label, new_pos, count);
5240 case GTK_MOVEMENT_VISUAL_POSITIONS:
5241 new_pos = gtk_label_move_visually (label, new_pos, count);
5242 if (new_pos == old_pos)
5244 if (!extend_selection)
5246 if (!gtk_widget_keynav_failed (GTK_WIDGET (label),
5248 GTK_DIR_RIGHT : GTK_DIR_LEFT))
5250 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
5253 gtk_widget_child_focus (toplevel,
5255 GTK_DIR_RIGHT : GTK_DIR_LEFT);
5260 gtk_widget_error_bell (GTK_WIDGET (label));
5264 case GTK_MOVEMENT_WORDS:
5267 new_pos = gtk_label_move_forward_word (label, new_pos);
5272 new_pos = gtk_label_move_backward_word (label, new_pos);
5275 if (new_pos == old_pos)
5276 gtk_widget_error_bell (GTK_WIDGET (label));
5278 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5279 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5280 case GTK_MOVEMENT_BUFFER_ENDS:
5281 /* FIXME: Can do better here */
5282 new_pos = count < 0 ? 0 : strlen (label->text);
5283 if (new_pos == old_pos)
5284 gtk_widget_error_bell (GTK_WIDGET (label));
5286 case GTK_MOVEMENT_DISPLAY_LINES:
5287 case GTK_MOVEMENT_PARAGRAPHS:
5288 case GTK_MOVEMENT_PAGES:
5289 case GTK_MOVEMENT_HORIZONTAL_PAGES:
5294 if (extend_selection)
5295 gtk_label_select_region_index (label,
5296 label->select_info->selection_anchor,
5299 gtk_label_select_region_index (label, new_pos, new_pos);
5303 gtk_label_copy_clipboard (GtkLabel *label)
5305 if (label->text && label->select_info)
5309 GtkClipboard *clipboard;
5311 start = MIN (label->select_info->selection_anchor,
5312 label->select_info->selection_end);
5313 end = MAX (label->select_info->selection_anchor,
5314 label->select_info->selection_end);
5316 len = strlen (label->text);
5324 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
5327 gtk_clipboard_set_text (clipboard, label->text + start, end - start);
5332 link = gtk_label_get_focus_link (label);
5334 gtk_clipboard_set_text (clipboard, link->uri, -1);
5340 gtk_label_select_all (GtkLabel *label)
5342 gtk_label_select_region_index (label, 0, strlen (label->text));
5345 /* Quick hack of a popup menu
5348 activate_cb (GtkWidget *menuitem,
5351 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
5352 g_signal_emit_by_name (label, signal);
5356 append_action_signal (GtkLabel *label,
5358 const gchar *stock_id,
5359 const gchar *signal,
5362 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
5364 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
5365 g_signal_connect (menuitem, "activate",
5366 G_CALLBACK (activate_cb), label);
5368 gtk_widget_set_sensitive (menuitem, sensitive);
5370 gtk_widget_show (menuitem);
5371 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5375 popup_menu_detach (GtkWidget *attach_widget,
5378 GtkLabel *label = GTK_LABEL (attach_widget);
5380 if (label->select_info)
5381 label->select_info->popup_menu = NULL;
5385 popup_position_func (GtkMenu *menu,
5396 label = GTK_LABEL (user_data);
5397 widget = GTK_WIDGET (label);
5399 g_return_if_fail (GTK_WIDGET_REALIZED (label));
5401 screen = gtk_widget_get_screen (widget);
5402 gdk_window_get_origin (widget->window, x, y);
5404 *x += widget->allocation.x;
5405 *y += widget->allocation.y;
5407 gtk_widget_size_request (GTK_WIDGET (menu), &req);
5409 *x += widget->allocation.width / 2;
5410 *y += widget->allocation.height;
5412 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
5413 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
5417 open_link_activate_cb (GtkMenuItem *menu_item,
5422 link = gtk_label_get_current_link (label);
5425 emit_activate_link (label, link);
5429 copy_link_activate_cb (GtkMenuItem *menu_item,
5432 GtkClipboard *clipboard;
5435 uri = gtk_label_get_current_uri (label);
5438 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
5439 gtk_clipboard_set_text (clipboard, uri, -1);
5444 gtk_label_popup_menu (GtkWidget *widget)
5446 gtk_label_do_popup (GTK_LABEL (widget), NULL);
5452 gtk_label_do_popup (GtkLabel *label,
5453 GdkEventButton *event)
5455 GtkWidget *menuitem;
5458 gboolean have_selection;
5461 if (!label->select_info)
5464 if (label->select_info->popup_menu)
5465 gtk_widget_destroy (label->select_info->popup_menu);
5467 label->select_info->popup_menu = menu = gtk_menu_new ();
5469 gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (label), popup_menu_detach);
5472 label->select_info->selection_anchor != label->select_info->selection_end;
5476 if (label->select_info->link_clicked)
5477 link = label->select_info->active_link;
5482 link = gtk_label_get_focus_link (label);
5484 if (!have_selection && link)
5487 menuitem = gtk_image_menu_item_new_with_mnemonic (_("_Open Link"));
5488 gtk_widget_show (menuitem);
5489 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5491 g_signal_connect (G_OBJECT (menuitem), "activate",
5492 G_CALLBACK (open_link_activate_cb), label);
5494 image = gtk_image_new_from_stock (GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_MENU);
5495 gtk_widget_show (image);
5496 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
5498 /* Copy Link Address */
5499 menuitem = gtk_image_menu_item_new_with_mnemonic (_("Copy _Link Address"));
5500 gtk_widget_show (menuitem);
5501 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5503 g_signal_connect (G_OBJECT (menuitem), "activate",
5504 G_CALLBACK (copy_link_activate_cb), label);
5506 image = gtk_image_new_from_stock (GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
5507 gtk_widget_show (image);
5508 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
5512 append_action_signal (label, menu, GTK_STOCK_CUT, "cut-clipboard", FALSE);
5513 append_action_signal (label, menu, GTK_STOCK_COPY, "copy-clipboard", have_selection);
5514 append_action_signal (label, menu, GTK_STOCK_PASTE, "paste-clipboard", FALSE);
5516 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
5517 gtk_widget_set_sensitive (menuitem, FALSE);
5518 gtk_widget_show (menuitem);
5519 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5521 menuitem = gtk_separator_menu_item_new ();
5522 gtk_widget_show (menuitem);
5523 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5525 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
5526 g_signal_connect_swapped (menuitem, "activate",
5527 G_CALLBACK (gtk_label_select_all), label);
5528 gtk_widget_show (menuitem);
5529 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5532 g_signal_emit (label, signals[POPULATE_POPUP], 0, menu);
5535 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
5537 event->button, event->time);
5540 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
5541 popup_position_func, label,
5542 0, gtk_get_current_event_time ());
5543 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
5548 gtk_label_clear_links (GtkLabel *label)
5550 if (!label->select_info)
5553 g_list_foreach (label->select_info->links, (GFunc)link_free, NULL);
5554 g_list_free (label->select_info->links);
5555 label->select_info->links = NULL;
5556 label->select_info->active_link = NULL;
5560 gtk_label_rescan_links (GtkLabel *label)
5562 PangoLayout *layout = label->layout;
5563 PangoAttrList *attlist;
5564 PangoAttrIterator *iter;
5567 if (!label->select_info)
5570 attlist = pango_layout_get_attributes (layout);
5572 if (attlist == NULL)
5575 iter = pango_attr_list_get_iterator (attlist);
5577 links = label->select_info->links;
5581 PangoAttribute *underline;
5582 PangoAttribute *color;
5584 underline = pango_attr_iterator_get (iter, PANGO_ATTR_UNDERLINE);
5585 color = pango_attr_iterator_get (iter, PANGO_ATTR_FOREGROUND);
5587 if (underline != NULL && color != NULL)
5590 PangoRectangle start_pos;
5591 PangoRectangle end_pos;
5594 pango_attr_iterator_range (iter, &start, &end);
5595 pango_layout_index_to_pos (layout, start, &start_pos);
5596 pango_layout_index_to_pos (layout, end, &end_pos);
5600 g_warning ("Ran out of links");
5604 links = links->next;
5605 link->start = start;
5608 } while (pango_attr_iterator_next (iter));
5610 pango_attr_iterator_destroy (iter);
5614 gtk_label_activate_link (GtkLabel *label,
5617 GtkWidget *widget = GTK_WIDGET (label);
5618 GError *error = NULL;
5620 if (!gtk_show_uri (gtk_widget_get_screen (widget),
5621 uri, gtk_get_current_event_time (), &error))
5623 g_warning ("Unable to show '%s': %s", uri, error->message);
5624 g_error_free (error);
5631 emit_activate_link (GtkLabel *label,
5636 g_signal_emit (label, signals[ACTIVATE_LINK], 0, link->uri, &handled);
5637 if (handled && !link->visited)
5639 link->visited = TRUE;
5640 /* FIXME: shouldn't have to redo everything here */
5641 gtk_label_recalculate (label);
5646 gtk_label_activate_current_link (GtkLabel *label)
5649 GtkWidget *widget = GTK_WIDGET (label);
5651 link = gtk_label_get_focus_link (label);
5655 emit_activate_link (label, link);
5659 GtkWidget *toplevel;
5662 toplevel = gtk_widget_get_toplevel (widget);
5663 if (GTK_IS_WINDOW (toplevel))
5665 window = GTK_WINDOW (toplevel);
5668 window->default_widget != widget &&
5669 !(widget == window->focus_widget &&
5670 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
5671 gtk_window_activate_default (window);
5676 static GtkLabelLink *
5677 gtk_label_get_current_link (GtkLabel *label)
5681 if (!label->select_info)
5684 if (label->select_info->link_clicked)
5685 link = label->select_info->active_link;
5687 link = gtk_label_get_focus_link (label);
5693 * gtk_label_get_current_uri:
5694 * @label: a #GtkLabel
5696 * Returns the URI for the currently active link in the label.
5697 * The active link is the one under the mouse pointer or, in a
5698 * selectable label, the link in which the text cursor is currently
5701 * This function is intended for use in a #GtkLabel::link-activate handler
5702 * or for use in a #GtkWidget::query-tooltip handler.
5704 * Returns: the currently active URI. The string is owned by GTK+ and must
5705 * not be freed or modified.
5709 G_CONST_RETURN gchar *
5710 gtk_label_get_current_uri (GtkLabel *label)
5713 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
5715 link = gtk_label_get_current_link (label);
5725 gtk_label_query_tooltip (GtkWidget *widget,
5728 gboolean keyboard_tip,
5729 GtkTooltip *tooltip)
5731 GtkLabel *label = GTK_LABEL (widget);
5732 GtkLabelSelectionInfo *info = label->select_info;
5736 if (info && info->links)
5740 if (info->selection_anchor == info->selection_end)
5741 index = info->selection_anchor;
5745 if (!get_layout_index (label, x, y, &index))
5751 for (l = info->links; l != NULL; l = l->next)
5753 GtkLabelLink *link = l->data;
5754 if (index >= link->start && index <= link->end)
5758 gtk_tooltip_set_markup (tooltip, link->title);
5767 return GTK_WIDGET_CLASS (gtk_label_parent_class)->query_tooltip (widget,
5774 #define __GTK_LABEL_C__
5775 #include "gtkaliasdef.c"