1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
21 * file for a list of people on the GTK+ Team. See the ChangeLog
22 * files for a list of changes. These files are distributed with
23 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
32 #include "gtkaccellabel.h"
35 #include "gtkmarshalers.h"
36 #include "gtkwindow.h"
37 #include "gdk/gdkkeysyms.h"
38 #include "gtkclipboard.h"
39 #include "gtkimagemenuitem.h"
41 #include "gtkseparatormenuitem.h"
42 #include "gtktextutil.h"
43 #include "gtkmenuitem.h"
44 #include "gtknotebook.h"
46 #include "gtkbindings.h"
47 #include "gtkbuildable.h"
50 #include "gtktooltip.h"
51 #include "gtkextendedlayout.h"
52 #include "gtkprivate.h"
55 #define GTK_LABEL_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_LABEL, GtkLabelPrivate))
63 gboolean mnemonics_visible;
66 /* Notes about the handling of links:
68 * Links share the GtkLabelSelectionInfo struct with selectable labels.
69 * There are some new fields for links. The links field contains the list
70 * of GtkLabelLink structs that describe the links which are embedded in
71 * the label. The active_link field points to the link under the mouse
72 * pointer. For keyboard navigation, the 'focus' link is determined by
73 * finding the link which contains the selection_anchor position.
74 * The link_clicked field is used with button press and release events
75 * to ensure that pressing inside a link and releasing outside of it
76 * does not activate the link.
78 * Links are rendered with the link-color/visited-link-color colors
79 * that are determined by the style and with an underline. When the mouse
80 * pointer is over a link, the pointer is changed to indicate the link,
81 * and the background behind the link is rendered with the base[PRELIGHT]
82 * color. While a button is pressed over a link, the background is rendered
83 * with the base[ACTIVE] color.
85 * Labels with links accept keyboard focus, and it is possible to move
86 * the focus between the embedded links using Tab/Shift-Tab. The focus
87 * is indicated by a focus rectangle that is drawn around the link text.
88 * Pressing Enter activates the focussed link, and there is a suitable
89 * context menu for links that can be opened with the Menu key. Pressing
90 * Control-C copies the link URI to the clipboard.
92 * In selectable labels with links, link functionality is only available
93 * when the selection is empty.
98 gchar *title; /* the title attribute, used as tooltip */
99 gboolean visited; /* get set when the link is activated; this flag
100 * gets preserved over later set_markup() calls
102 gint start; /* position of the link in the PangoLayout */
106 struct _GtkLabelSelectionInfo
109 gint selection_anchor;
111 GtkWidget *popup_menu;
114 GtkLabelLink *active_link;
120 guint select_words : 1;
121 guint selectable : 1;
122 guint link_clicked : 1;
130 ACTIVATE_CURRENT_LINK,
145 PROP_MNEMONIC_KEYVAL,
146 PROP_MNEMONIC_WIDGET,
147 PROP_CURSOR_POSITION,
148 PROP_SELECTION_BOUND,
151 PROP_SINGLE_LINE_MODE,
153 PROP_MAX_WIDTH_CHARS,
154 PROP_TRACK_VISITED_LINKS,
158 static guint signals[LAST_SIGNAL] = { 0 };
160 static const GdkColor default_link_color = { 0, 0, 0, 0xeeee };
161 static const GdkColor default_visited_link_color = { 0, 0x5555, 0x1a1a, 0x8b8b };
163 static void gtk_label_set_property (GObject *object,
167 static void gtk_label_get_property (GObject *object,
171 static void gtk_label_destroy (GtkObject *object);
172 static void gtk_label_finalize (GObject *object);
173 static void gtk_label_size_allocate (GtkWidget *widget,
174 GtkAllocation *allocation);
175 static void gtk_label_state_changed (GtkWidget *widget,
177 static void gtk_label_style_set (GtkWidget *widget,
178 GtkStyle *previous_style);
179 static void gtk_label_direction_changed (GtkWidget *widget,
180 GtkTextDirection previous_dir);
181 static gint gtk_label_expose (GtkWidget *widget,
182 GdkEventExpose *event);
183 static gboolean gtk_label_focus (GtkWidget *widget,
184 GtkDirectionType direction);
186 static void gtk_label_realize (GtkWidget *widget);
187 static void gtk_label_unrealize (GtkWidget *widget);
188 static void gtk_label_map (GtkWidget *widget);
189 static void gtk_label_unmap (GtkWidget *widget);
191 static gboolean gtk_label_button_press (GtkWidget *widget,
192 GdkEventButton *event);
193 static gboolean gtk_label_button_release (GtkWidget *widget,
194 GdkEventButton *event);
195 static gboolean gtk_label_motion (GtkWidget *widget,
196 GdkEventMotion *event);
197 static gboolean gtk_label_leave_notify (GtkWidget *widget,
198 GdkEventCrossing *event);
200 static void gtk_label_grab_focus (GtkWidget *widget);
202 static gboolean gtk_label_query_tooltip (GtkWidget *widget,
205 gboolean keyboard_tip,
206 GtkTooltip *tooltip);
208 static void gtk_label_set_text_internal (GtkLabel *label,
210 static void gtk_label_set_label_internal (GtkLabel *label,
212 static void gtk_label_set_use_markup_internal (GtkLabel *label,
214 static void gtk_label_set_use_underline_internal (GtkLabel *label,
216 static void gtk_label_set_attributes_internal (GtkLabel *label,
217 PangoAttrList *attrs);
218 static void gtk_label_set_uline_text_internal (GtkLabel *label,
220 static void gtk_label_set_pattern_internal (GtkLabel *label,
221 const gchar *pattern,
222 gboolean is_mnemonic);
223 static void gtk_label_set_markup_internal (GtkLabel *label,
225 gboolean with_uline);
226 static void gtk_label_recalculate (GtkLabel *label);
227 static void gtk_label_hierarchy_changed (GtkWidget *widget,
228 GtkWidget *old_toplevel);
229 static void gtk_label_screen_changed (GtkWidget *widget,
230 GdkScreen *old_screen);
231 static gboolean gtk_label_popup_menu (GtkWidget *widget);
233 static void gtk_label_create_window (GtkLabel *label);
234 static void gtk_label_destroy_window (GtkLabel *label);
235 static void gtk_label_ensure_select_info (GtkLabel *label);
236 static void gtk_label_clear_select_info (GtkLabel *label);
237 static void gtk_label_update_cursor (GtkLabel *label);
238 static void gtk_label_clear_layout (GtkLabel *label);
239 static void gtk_label_ensure_layout (GtkLabel *label);
240 static void gtk_label_invalidate_wrap_width (GtkLabel *label);
241 static void gtk_label_select_region_index (GtkLabel *label,
245 static gboolean gtk_label_mnemonic_activate (GtkWidget *widget,
246 gboolean group_cycling);
247 static void gtk_label_setup_mnemonic (GtkLabel *label,
249 static void gtk_label_drag_data_get (GtkWidget *widget,
250 GdkDragContext *context,
251 GtkSelectionData *selection_data,
255 static void gtk_label_buildable_interface_init (GtkBuildableIface *iface);
256 static gboolean gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
259 const gchar *tagname,
260 GMarkupParser *parser,
263 static void gtk_label_buildable_custom_finished (GtkBuildable *buildable,
266 const gchar *tagname,
270 static void connect_mnemonics_visible_notify (GtkLabel *label);
271 static gboolean separate_uline_pattern (const gchar *str,
277 /* For selectable labels: */
278 static void gtk_label_move_cursor (GtkLabel *label,
279 GtkMovementStep step,
281 gboolean extend_selection);
282 static void gtk_label_copy_clipboard (GtkLabel *label);
283 static void gtk_label_select_all (GtkLabel *label);
284 static void gtk_label_do_popup (GtkLabel *label,
285 GdkEventButton *event);
286 static gint gtk_label_move_forward_word (GtkLabel *label,
288 static gint gtk_label_move_backward_word (GtkLabel *label,
292 static void gtk_label_rescan_links (GtkLabel *label);
293 static void gtk_label_clear_links (GtkLabel *label);
294 static gboolean gtk_label_activate_link (GtkLabel *label,
296 static void gtk_label_activate_current_link (GtkLabel *label);
297 static GtkLabelLink *gtk_label_get_current_link (GtkLabel *label);
298 static void gtk_label_get_link_colors (GtkWidget *widget,
299 GdkColor **link_color,
300 GdkColor **visited_link_color);
301 static void emit_activate_link (GtkLabel *label,
304 static void gtk_label_layout_interface_init (GtkExtendedLayoutIface *iface);
306 static void gtk_label_get_desired_size (GtkExtendedLayout *layout,
307 GtkRequisition *minimum_size,
308 GtkRequisition *natural_size);
309 static void gtk_label_get_width_for_height (GtkExtendedLayout *layout,
312 gint *natural_width);
313 static void gtk_label_get_height_for_width (GtkExtendedLayout *layout,
315 gint *minimum_height,
316 gint *natural_height);
318 static GQuark quark_angle = 0;
320 static GtkBuildableIface *buildable_parent_iface = NULL;
322 G_DEFINE_TYPE_WITH_CODE (GtkLabel, gtk_label, GTK_TYPE_MISC,
323 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
324 gtk_label_buildable_interface_init)
325 G_IMPLEMENT_INTERFACE (GTK_TYPE_EXTENDED_LAYOUT,
326 gtk_label_layout_interface_init));
329 add_move_binding (GtkBindingSet *binding_set,
332 GtkMovementStep step,
335 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
337 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
341 G_TYPE_BOOLEAN, FALSE);
343 /* Selection-extending version */
344 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
348 G_TYPE_BOOLEAN, TRUE);
352 gtk_label_layout_interface_init (GtkExtendedLayoutIface *iface)
354 iface->get_desired_size = gtk_label_get_desired_size;
355 iface->get_width_for_height = gtk_label_get_width_for_height;
356 iface->get_height_for_width = gtk_label_get_height_for_width;
360 gtk_label_class_init (GtkLabelClass *class)
362 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
363 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
364 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
365 GtkBindingSet *binding_set;
367 quark_angle = g_quark_from_static_string ("angle");
369 gobject_class->set_property = gtk_label_set_property;
370 gobject_class->get_property = gtk_label_get_property;
371 gobject_class->finalize = gtk_label_finalize;
373 object_class->destroy = gtk_label_destroy;
375 widget_class->size_allocate = gtk_label_size_allocate;
376 widget_class->state_changed = gtk_label_state_changed;
377 widget_class->style_set = gtk_label_style_set;
378 widget_class->query_tooltip = gtk_label_query_tooltip;
379 widget_class->direction_changed = gtk_label_direction_changed;
380 widget_class->expose_event = gtk_label_expose;
381 widget_class->realize = gtk_label_realize;
382 widget_class->unrealize = gtk_label_unrealize;
383 widget_class->map = gtk_label_map;
384 widget_class->unmap = gtk_label_unmap;
385 widget_class->button_press_event = gtk_label_button_press;
386 widget_class->button_release_event = gtk_label_button_release;
387 widget_class->motion_notify_event = gtk_label_motion;
388 widget_class->leave_notify_event = gtk_label_leave_notify;
389 widget_class->hierarchy_changed = gtk_label_hierarchy_changed;
390 widget_class->screen_changed = gtk_label_screen_changed;
391 widget_class->mnemonic_activate = gtk_label_mnemonic_activate;
392 widget_class->drag_data_get = gtk_label_drag_data_get;
393 widget_class->grab_focus = gtk_label_grab_focus;
394 widget_class->popup_menu = gtk_label_popup_menu;
395 widget_class->focus = gtk_label_focus;
397 class->move_cursor = gtk_label_move_cursor;
398 class->copy_clipboard = gtk_label_copy_clipboard;
399 class->activate_link = gtk_label_activate_link;
402 * GtkLabel::move-cursor:
403 * @entry: the object which received the signal
404 * @step: the granularity of the move, as a #GtkMovementStep
405 * @count: the number of @step units to move
406 * @extend_selection: %TRUE if the move should extend the selection
408 * The ::move-cursor signal is a
409 * <link linkend="keybinding-signals">keybinding signal</link>
410 * which gets emitted when the user initiates a cursor movement.
411 * If the cursor is not visible in @entry, this signal causes
412 * the viewport to be moved instead.
414 * Applications should not connect to it, but may emit it with
415 * g_signal_emit_by_name() if they need to control the cursor
418 * The default bindings for this signal come in two variants,
419 * the variant with the Shift modifier extends the selection,
420 * the variant without the Shift modifer does not.
421 * There are too many key combinations to list them all here.
423 * <listitem>Arrow keys move by individual characters/lines</listitem>
424 * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
425 * <listitem>Home/End keys move to the ends of the buffer</listitem>
428 signals[MOVE_CURSOR] =
429 g_signal_new (I_("move-cursor"),
430 G_OBJECT_CLASS_TYPE (gobject_class),
431 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
432 G_STRUCT_OFFSET (GtkLabelClass, move_cursor),
434 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
436 GTK_TYPE_MOVEMENT_STEP,
441 * GtkLabel::copy-clipboard:
442 * @label: the object which received the signal
444 * The ::copy-clipboard signal is a
445 * <link linkend="keybinding-signals">keybinding signal</link>
446 * which gets emitted to copy the selection to the clipboard.
448 * The default binding for this signal is Ctrl-c.
450 signals[COPY_CLIPBOARD] =
451 g_signal_new (I_("copy-clipboard"),
452 G_OBJECT_CLASS_TYPE (gobject_class),
453 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
454 G_STRUCT_OFFSET (GtkLabelClass, copy_clipboard),
456 _gtk_marshal_VOID__VOID,
460 * GtkLabel::populate-popup:
461 * @label: The label on which the signal is emitted
462 * @menu: the menu that is being populated
464 * The ::populate-popup signal gets emitted before showing the
465 * context menu of the label. Note that only selectable labels
466 * have context menus.
468 * If you need to add items to the context menu, connect
469 * to this signal and append your menuitems to the @menu.
471 signals[POPULATE_POPUP] =
472 g_signal_new (I_("populate-popup"),
473 G_OBJECT_CLASS_TYPE (gobject_class),
475 G_STRUCT_OFFSET (GtkLabelClass, populate_popup),
477 _gtk_marshal_VOID__OBJECT,
482 * GtkLabel::activate-current-link:
483 * @label: The label on which the signal was emitted
485 * A <link linkend="keybinding-signals">keybinding signal</link>
486 * which gets emitted when the user activates a link in the label.
488 * Applications may also emit the signal with g_signal_emit_by_name()
489 * if they need to control activation of URIs programmatically.
491 * The default bindings for this signal are all forms of the Enter key.
495 signals[ACTIVATE_CURRENT_LINK] =
496 g_signal_new_class_handler ("activate-current-link",
497 G_TYPE_FROM_CLASS (object_class),
498 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
499 G_CALLBACK (gtk_label_activate_current_link),
501 _gtk_marshal_VOID__VOID,
505 * GtkLabel::activate-link:
506 * @label: The label on which the signal was emitted
507 * @uri: the URI that is activated
509 * The signal which gets emitted to activate a URI.
510 * Applications may connect to it to override the default behaviour,
511 * which is to call gtk_show_uri().
513 * Returns: %TRUE if the link has been activated
517 signals[ACTIVATE_LINK] =
518 g_signal_new ("activate-link",
519 G_TYPE_FROM_CLASS (object_class),
521 G_STRUCT_OFFSET (GtkLabelClass, activate_link),
522 _gtk_boolean_handled_accumulator, NULL,
523 _gtk_marshal_BOOLEAN__STRING,
524 G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
526 g_object_class_install_property (gobject_class,
528 g_param_spec_string ("label",
530 P_("The text of the label"),
532 GTK_PARAM_READWRITE));
533 g_object_class_install_property (gobject_class,
535 g_param_spec_boxed ("attributes",
537 P_("A list of style attributes to apply to the text of the label"),
538 PANGO_TYPE_ATTR_LIST,
539 GTK_PARAM_READWRITE));
540 g_object_class_install_property (gobject_class,
542 g_param_spec_boolean ("use-markup",
544 P_("The text of the label includes XML markup. See pango_parse_markup()"),
546 GTK_PARAM_READWRITE));
547 g_object_class_install_property (gobject_class,
549 g_param_spec_boolean ("use-underline",
551 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
553 GTK_PARAM_READWRITE));
555 g_object_class_install_property (gobject_class,
557 g_param_spec_enum ("justify",
559 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"),
560 GTK_TYPE_JUSTIFICATION,
562 GTK_PARAM_READWRITE));
564 g_object_class_install_property (gobject_class,
566 g_param_spec_string ("pattern",
568 P_("A string with _ characters in positions correspond to characters in the text to underline"),
570 GTK_PARAM_WRITABLE));
572 g_object_class_install_property (gobject_class,
574 g_param_spec_boolean ("wrap",
576 P_("If set, wrap lines if the text becomes too wide"),
578 GTK_PARAM_READWRITE));
580 * GtkLabel:wrap-mode:
582 * If line wrapping is on (see the #GtkLabel:wrap property) this controls
583 * how the line wrapping is done. The default is %PANGO_WRAP_WORD, which
584 * means wrap on word boundaries.
588 g_object_class_install_property (gobject_class,
590 g_param_spec_enum ("wrap-mode",
591 P_("Line wrap mode"),
592 P_("If wrap is set, controls how linewrapping is done"),
593 PANGO_TYPE_WRAP_MODE,
595 GTK_PARAM_READWRITE));
596 g_object_class_install_property (gobject_class,
598 g_param_spec_boolean ("selectable",
600 P_("Whether the label text can be selected with the mouse"),
602 GTK_PARAM_READWRITE));
603 g_object_class_install_property (gobject_class,
604 PROP_MNEMONIC_KEYVAL,
605 g_param_spec_uint ("mnemonic-keyval",
607 P_("The mnemonic accelerator key for this label"),
611 GTK_PARAM_READABLE));
612 g_object_class_install_property (gobject_class,
613 PROP_MNEMONIC_WIDGET,
614 g_param_spec_object ("mnemonic-widget",
615 P_("Mnemonic widget"),
616 P_("The widget to be activated when the label's mnemonic "
619 GTK_PARAM_READWRITE));
621 g_object_class_install_property (gobject_class,
622 PROP_CURSOR_POSITION,
623 g_param_spec_int ("cursor-position",
624 P_("Cursor Position"),
625 P_("The current position of the insertion cursor in chars"),
629 GTK_PARAM_READABLE));
631 g_object_class_install_property (gobject_class,
632 PROP_SELECTION_BOUND,
633 g_param_spec_int ("selection-bound",
634 P_("Selection Bound"),
635 P_("The position of the opposite end of the selection from the cursor in chars"),
639 GTK_PARAM_READABLE));
642 * GtkLabel:ellipsize:
644 * The preferred place to ellipsize the string, if the label does
645 * not have enough room to display the entire string, specified as a
646 * #PangoEllisizeMode.
648 * Note that setting this property to a value other than
649 * %PANGO_ELLIPSIZE_NONE has the side-effect that the label requests
650 * only enough space to display the ellipsis "...". In particular, this
651 * means that ellipsizing labels do not work well in notebook tabs, unless
652 * the tab's #GtkNotebook:tab-expand property is set to %TRUE. Other ways
653 * to set a label's width are gtk_widget_set_size_request() and
654 * gtk_label_set_width_chars().
658 g_object_class_install_property (gobject_class,
660 g_param_spec_enum ("ellipsize",
662 P_("The preferred place to ellipsize the string, if the label does not have enough room to display the entire string"),
663 PANGO_TYPE_ELLIPSIZE_MODE,
664 PANGO_ELLIPSIZE_NONE,
665 GTK_PARAM_READWRITE));
668 * GtkLabel:width-chars:
670 * The desired width of the label, in characters. If this property is set to
671 * -1, the width will be calculated automatically, otherwise the label will
672 * request either 3 characters or the property value, whichever is greater.
673 * If the "width-chars" property is set to a positive value, then the
674 * #GtkLabel:max-width-chars property is ignored.
678 g_object_class_install_property (gobject_class,
680 g_param_spec_int ("width-chars",
681 P_("Width In Characters"),
682 P_("The desired width of the label, in characters"),
686 GTK_PARAM_READWRITE));
689 * GtkLabel:single-line-mode:
691 * Whether the label is in single line mode. In single line mode,
692 * the height of the label does not depend on the actual text, it
693 * is always set to ascent + descent of the font. This can be an
694 * advantage in situations where resizing the label because of text
695 * changes would be distracting, e.g. in a statusbar.
699 g_object_class_install_property (gobject_class,
700 PROP_SINGLE_LINE_MODE,
701 g_param_spec_boolean ("single-line-mode",
702 P_("Single Line Mode"),
703 P_("Whether the label is in single line mode"),
705 GTK_PARAM_READWRITE));
710 * The angle that the baseline of the label makes with the horizontal,
711 * in degrees, measured counterclockwise. An angle of 90 reads from
712 * from bottom to top, an angle of 270, from top to bottom. Ignored
713 * if the label is selectable, wrapped, or ellipsized.
717 g_object_class_install_property (gobject_class,
719 g_param_spec_double ("angle",
721 P_("Angle at which the label is rotated"),
725 GTK_PARAM_READWRITE));
728 * GtkLabel:max-width-chars:
730 * The desired maximum width of the label, in characters. If this property
731 * is set to -1, the width will be calculated automatically, otherwise the
732 * label will request space for no more than the requested number of
733 * characters. If the #GtkLabel:width-chars property is set to a positive
734 * value, then the "max-width-chars" property is ignored.
738 g_object_class_install_property (gobject_class,
739 PROP_MAX_WIDTH_CHARS,
740 g_param_spec_int ("max-width-chars",
741 P_("Maximum Width In Characters"),
742 P_("The desired maximum width of the label, in characters"),
746 GTK_PARAM_READWRITE));
749 * GtkLabel:track-visited-links:
751 * Set this property to %TRUE to make the label track which links
752 * have been clicked. It will then apply the ::visited-link-color
753 * color, instead of ::link-color.
757 g_object_class_install_property (gobject_class,
758 PROP_TRACK_VISITED_LINKS,
759 g_param_spec_boolean ("track-visited-links",
760 P_("Track visited links"),
761 P_("Whether visited links should be tracked"),
763 GTK_PARAM_READWRITE));
766 * GtkLabel:full-size:
768 * Use the entire space the widget got assigned for text wrapping. Overrides
769 * any #GtkLabel:width-chars, #GtkLabel:max-width-chars and screen size based
770 * constraints. Requires #GtkLabel:angle to be 0°, 90°, 180° or 270°.
774 g_object_class_install_property (gobject_class,
776 g_param_spec_boolean ("full-size",
778 P_("Use the entire size of the widget to wrap text"),
780 GTK_PARAM_READWRITE));
786 binding_set = gtk_binding_set_by_class (class);
788 /* Moving the insertion point */
789 add_move_binding (binding_set, GDK_Right, 0,
790 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
792 add_move_binding (binding_set, GDK_Left, 0,
793 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
795 add_move_binding (binding_set, GDK_KP_Right, 0,
796 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
798 add_move_binding (binding_set, GDK_KP_Left, 0,
799 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
801 add_move_binding (binding_set, GDK_f, GDK_CONTROL_MASK,
802 GTK_MOVEMENT_LOGICAL_POSITIONS, 1);
804 add_move_binding (binding_set, GDK_b, GDK_CONTROL_MASK,
805 GTK_MOVEMENT_LOGICAL_POSITIONS, -1);
807 add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
808 GTK_MOVEMENT_WORDS, 1);
810 add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
811 GTK_MOVEMENT_WORDS, -1);
813 add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
814 GTK_MOVEMENT_WORDS, 1);
816 add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
817 GTK_MOVEMENT_WORDS, -1);
820 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
822 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
824 G_TYPE_BOOLEAN, FALSE);
826 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
828 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
830 G_TYPE_BOOLEAN, TRUE);
832 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
834 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
836 G_TYPE_BOOLEAN, FALSE);
838 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
840 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
842 G_TYPE_BOOLEAN, TRUE);
845 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
847 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
849 G_TYPE_BOOLEAN, FALSE);
851 gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_CONTROL_MASK,
853 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
855 G_TYPE_BOOLEAN, FALSE);
857 add_move_binding (binding_set, GDK_f, GDK_MOD1_MASK,
858 GTK_MOVEMENT_WORDS, 1);
860 add_move_binding (binding_set, GDK_b, GDK_MOD1_MASK,
861 GTK_MOVEMENT_WORDS, -1);
863 add_move_binding (binding_set, GDK_Home, 0,
864 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
866 add_move_binding (binding_set, GDK_End, 0,
867 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
869 add_move_binding (binding_set, GDK_KP_Home, 0,
870 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
872 add_move_binding (binding_set, GDK_KP_End, 0,
873 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
875 add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
876 GTK_MOVEMENT_BUFFER_ENDS, -1);
878 add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
879 GTK_MOVEMENT_BUFFER_ENDS, 1);
881 add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
882 GTK_MOVEMENT_BUFFER_ENDS, -1);
884 add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
885 GTK_MOVEMENT_BUFFER_ENDS, 1);
888 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
889 "copy-clipboard", 0);
891 gtk_binding_entry_add_signal (binding_set, GDK_Return, 0,
892 "activate-current-link", 0);
893 gtk_binding_entry_add_signal (binding_set, GDK_ISO_Enter, 0,
894 "activate-current-link", 0);
895 gtk_binding_entry_add_signal (binding_set, GDK_KP_Enter, 0,
896 "activate-current-link", 0);
898 gtk_settings_install_property (g_param_spec_boolean ("gtk-label-select-on-focus",
899 P_("Select on focus"),
900 P_("Whether to select the contents of a selectable label when it is focused"),
902 GTK_PARAM_READWRITE));
905 g_type_class_add_private (class, sizeof (GtkLabelPrivate));
909 gtk_label_set_property (GObject *object,
916 label = GTK_LABEL (object);
921 gtk_label_set_label (label, g_value_get_string (value));
923 case PROP_ATTRIBUTES:
924 gtk_label_set_attributes (label, g_value_get_boxed (value));
926 case PROP_USE_MARKUP:
927 gtk_label_set_use_markup (label, g_value_get_boolean (value));
929 case PROP_USE_UNDERLINE:
930 gtk_label_set_use_underline (label, g_value_get_boolean (value));
933 gtk_label_set_justify (label, g_value_get_enum (value));
936 gtk_label_set_pattern (label, g_value_get_string (value));
939 gtk_label_set_line_wrap (label, g_value_get_boolean (value));
942 gtk_label_set_line_wrap_mode (label, g_value_get_enum (value));
944 case PROP_SELECTABLE:
945 gtk_label_set_selectable (label, g_value_get_boolean (value));
947 case PROP_MNEMONIC_WIDGET:
948 gtk_label_set_mnemonic_widget (label, (GtkWidget*) g_value_get_object (value));
951 gtk_label_set_ellipsize (label, g_value_get_enum (value));
953 case PROP_WIDTH_CHARS:
954 gtk_label_set_width_chars (label, g_value_get_int (value));
956 case PROP_SINGLE_LINE_MODE:
957 gtk_label_set_single_line_mode (label, g_value_get_boolean (value));
960 gtk_label_set_angle (label, g_value_get_double (value));
962 case PROP_MAX_WIDTH_CHARS:
963 gtk_label_set_max_width_chars (label, g_value_get_int (value));
965 case PROP_TRACK_VISITED_LINKS:
966 gtk_label_set_track_visited_links (label, g_value_get_boolean (value));
969 gtk_label_set_full_size (label, g_value_get_boolean (value));
972 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
978 gtk_label_get_property (GObject *object,
985 label = GTK_LABEL (object);
990 g_value_set_string (value, label->label);
992 case PROP_ATTRIBUTES:
993 g_value_set_boxed (value, label->attrs);
995 case PROP_USE_MARKUP:
996 g_value_set_boolean (value, label->use_markup);
998 case PROP_USE_UNDERLINE:
999 g_value_set_boolean (value, label->use_underline);
1002 g_value_set_enum (value, label->jtype);
1005 g_value_set_boolean (value, label->wrap);
1007 case PROP_WRAP_MODE:
1008 g_value_set_enum (value, label->wrap_mode);
1010 case PROP_SELECTABLE:
1011 g_value_set_boolean (value, gtk_label_get_selectable (label));
1013 case PROP_MNEMONIC_KEYVAL:
1014 g_value_set_uint (value, label->mnemonic_keyval);
1016 case PROP_MNEMONIC_WIDGET:
1017 g_value_set_object (value, (GObject*) label->mnemonic_widget);
1019 case PROP_CURSOR_POSITION:
1020 if (label->select_info && label->select_info->selectable)
1022 gint offset = g_utf8_pointer_to_offset (label->text,
1023 label->text + label->select_info->selection_end);
1024 g_value_set_int (value, offset);
1027 g_value_set_int (value, 0);
1029 case PROP_SELECTION_BOUND:
1030 if (label->select_info && label->select_info->selectable)
1032 gint offset = g_utf8_pointer_to_offset (label->text,
1033 label->text + label->select_info->selection_anchor);
1034 g_value_set_int (value, offset);
1037 g_value_set_int (value, 0);
1039 case PROP_ELLIPSIZE:
1040 g_value_set_enum (value, label->ellipsize);
1042 case PROP_WIDTH_CHARS:
1043 g_value_set_int (value, gtk_label_get_width_chars (label));
1045 case PROP_SINGLE_LINE_MODE:
1046 g_value_set_boolean (value, gtk_label_get_single_line_mode (label));
1049 g_value_set_double (value, gtk_label_get_angle (label));
1051 case PROP_MAX_WIDTH_CHARS:
1052 g_value_set_int (value, gtk_label_get_max_width_chars (label));
1054 case PROP_TRACK_VISITED_LINKS:
1055 g_value_set_boolean (value, gtk_label_get_track_visited_links (label));
1057 case PROP_FULL_SIZE:
1058 g_value_set_int (value, gtk_label_get_full_size (label));
1061 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1067 gtk_label_init (GtkLabel *label)
1069 GtkLabelPrivate *priv;
1071 GTK_WIDGET_SET_FLAGS (label, GTK_NO_WINDOW);
1073 priv = GTK_LABEL_GET_PRIVATE (label);
1074 priv->width_chars = -1;
1075 priv->max_width_chars = -1;
1076 priv->wrap_width = -1;
1077 label->label = NULL;
1079 label->jtype = GTK_JUSTIFY_LEFT;
1080 label->wrap = FALSE;
1081 label->wrap_mode = PANGO_WRAP_WORD;
1082 label->ellipsize = PANGO_ELLIPSIZE_NONE;
1084 label->use_underline = FALSE;
1085 label->use_markup = FALSE;
1086 label->pattern_set = FALSE;
1087 label->track_links = TRUE;
1089 label->mnemonic_keyval = GDK_VoidSymbol;
1090 label->layout = NULL;
1092 label->attrs = NULL;
1094 label->mnemonic_widget = NULL;
1095 label->mnemonic_window = NULL;
1097 priv->mnemonics_visible = TRUE;
1099 gtk_label_set_text (label, "");
1104 gtk_label_buildable_interface_init (GtkBuildableIface *iface)
1106 buildable_parent_iface = g_type_interface_peek_parent (iface);
1108 iface->custom_tag_start = gtk_label_buildable_custom_tag_start;
1109 iface->custom_finished = gtk_label_buildable_custom_finished;
1113 GtkBuilder *builder;
1115 PangoAttrList *attrs;
1118 static PangoAttribute *
1119 attribute_from_text (GtkBuilder *builder,
1124 PangoAttribute *attribute = NULL;
1126 PangoLanguage *language;
1127 PangoFontDescription *font_desc;
1129 GValue val = { 0, };
1131 if (!gtk_builder_value_from_string_type (builder, PANGO_TYPE_ATTR_TYPE, name, &val, error))
1134 type = g_value_get_enum (&val);
1135 g_value_unset (&val);
1139 /* PangoAttrLanguage */
1140 case PANGO_ATTR_LANGUAGE:
1141 if ((language = pango_language_from_string (value)))
1143 attribute = pango_attr_language_new (language);
1144 g_value_init (&val, G_TYPE_INT);
1148 case PANGO_ATTR_STYLE:
1149 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STYLE, value, &val, error))
1150 attribute = pango_attr_style_new (g_value_get_enum (&val));
1152 case PANGO_ATTR_WEIGHT:
1153 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_WEIGHT, value, &val, error))
1154 attribute = pango_attr_weight_new (g_value_get_enum (&val));
1156 case PANGO_ATTR_VARIANT:
1157 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_VARIANT, value, &val, error))
1158 attribute = pango_attr_variant_new (g_value_get_enum (&val));
1160 case PANGO_ATTR_STRETCH:
1161 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STRETCH, value, &val, error))
1162 attribute = pango_attr_stretch_new (g_value_get_enum (&val));
1164 case PANGO_ATTR_UNDERLINE:
1165 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1166 attribute = pango_attr_underline_new (g_value_get_boolean (&val));
1168 case PANGO_ATTR_STRIKETHROUGH:
1169 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1170 attribute = pango_attr_strikethrough_new (g_value_get_boolean (&val));
1172 case PANGO_ATTR_GRAVITY:
1173 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY, value, &val, error))
1174 attribute = pango_attr_gravity_new (g_value_get_enum (&val));
1176 case PANGO_ATTR_GRAVITY_HINT:
1177 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY_HINT,
1178 value, &val, error))
1179 attribute = pango_attr_gravity_hint_new (g_value_get_enum (&val));
1181 /* PangoAttrString */
1182 case PANGO_ATTR_FAMILY:
1183 attribute = pango_attr_family_new (value);
1184 g_value_init (&val, G_TYPE_INT);
1188 case PANGO_ATTR_SIZE:
1189 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1190 value, &val, error))
1191 attribute = pango_attr_size_new (g_value_get_int (&val));
1193 case PANGO_ATTR_ABSOLUTE_SIZE:
1194 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1195 value, &val, error))
1196 attribute = pango_attr_size_new_absolute (g_value_get_int (&val));
1199 /* PangoAttrFontDesc */
1200 case PANGO_ATTR_FONT_DESC:
1201 if ((font_desc = pango_font_description_from_string (value)))
1203 attribute = pango_attr_font_desc_new (font_desc);
1204 pango_font_description_free (font_desc);
1205 g_value_init (&val, G_TYPE_INT);
1209 /* PangoAttrColor */
1210 case PANGO_ATTR_FOREGROUND:
1211 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1212 value, &val, error))
1214 color = g_value_get_boxed (&val);
1215 attribute = pango_attr_foreground_new (color->red, color->green, color->blue);
1218 case PANGO_ATTR_BACKGROUND:
1219 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1220 value, &val, error))
1222 color = g_value_get_boxed (&val);
1223 attribute = pango_attr_background_new (color->red, color->green, color->blue);
1226 case PANGO_ATTR_UNDERLINE_COLOR:
1227 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1228 value, &val, error))
1230 color = g_value_get_boxed (&val);
1231 attribute = pango_attr_underline_color_new (color->red, color->green, color->blue);
1234 case PANGO_ATTR_STRIKETHROUGH_COLOR:
1235 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1236 value, &val, error))
1238 color = g_value_get_boxed (&val);
1239 attribute = pango_attr_strikethrough_color_new (color->red, color->green, color->blue);
1243 /* PangoAttrShape */
1244 case PANGO_ATTR_SHAPE:
1245 /* Unsupported for now */
1247 /* PangoAttrFloat */
1248 case PANGO_ATTR_SCALE:
1249 if (gtk_builder_value_from_string_type (builder, G_TYPE_DOUBLE,
1250 value, &val, error))
1251 attribute = pango_attr_scale_new (g_value_get_double (&val));
1254 case PANGO_ATTR_INVALID:
1255 case PANGO_ATTR_LETTER_SPACING:
1256 case PANGO_ATTR_RISE:
1257 case PANGO_ATTR_FALLBACK:
1262 g_value_unset (&val);
1269 pango_start_element (GMarkupParseContext *context,
1270 const gchar *element_name,
1271 const gchar **names,
1272 const gchar **values,
1276 PangoParserData *data = (PangoParserData*)user_data;
1277 GValue val = { 0, };
1279 gint line_number, char_number;
1281 if (strcmp (element_name, "attribute") == 0)
1283 PangoAttribute *attr = NULL;
1284 const gchar *name = NULL;
1285 const gchar *value = NULL;
1286 const gchar *start = NULL;
1287 const gchar *end = NULL;
1288 guint start_val = 0;
1289 guint end_val = G_MAXUINT;
1291 for (i = 0; names[i]; i++)
1293 if (strcmp (names[i], "name") == 0)
1295 else if (strcmp (names[i], "value") == 0)
1297 else if (strcmp (names[i], "start") == 0)
1299 else if (strcmp (names[i], "end") == 0)
1303 g_markup_parse_context_get_position (context,
1308 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1309 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1311 line_number, char_number, names[i], "attribute");
1316 if (!name || !value)
1318 g_markup_parse_context_get_position (context,
1323 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1324 "%s:%d:%d <%s> requires attribute \"%s\"",
1326 line_number, char_number, "attribute",
1327 name ? "value" : "name");
1333 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1334 start, &val, error))
1336 start_val = g_value_get_uint (&val);
1337 g_value_unset (&val);
1342 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1345 end_val = g_value_get_uint (&val);
1346 g_value_unset (&val);
1349 attr = attribute_from_text (data->builder, name, value, error);
1350 attr->start_index = start_val;
1351 attr->end_index = end_val;
1356 data->attrs = pango_attr_list_new ();
1358 pango_attr_list_insert (data->attrs, attr);
1361 else if (strcmp (element_name, "attributes") == 0)
1364 g_warning ("Unsupported tag for GtkLabel: %s\n", element_name);
1367 static const GMarkupParser pango_parser =
1369 pango_start_element,
1373 gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
1374 GtkBuilder *builder,
1376 const gchar *tagname,
1377 GMarkupParser *parser,
1380 if (buildable_parent_iface->custom_tag_start (buildable, builder, child,
1381 tagname, parser, data))
1384 if (strcmp (tagname, "attributes") == 0)
1386 PangoParserData *parser_data;
1388 parser_data = g_slice_new0 (PangoParserData);
1389 parser_data->builder = g_object_ref (builder);
1390 parser_data->object = g_object_ref (buildable);
1391 *parser = pango_parser;
1392 *data = parser_data;
1399 gtk_label_buildable_custom_finished (GtkBuildable *buildable,
1400 GtkBuilder *builder,
1402 const gchar *tagname,
1405 PangoParserData *data;
1407 buildable_parent_iface->custom_finished (buildable, builder, child,
1408 tagname, user_data);
1410 if (strcmp (tagname, "attributes") == 0)
1412 data = (PangoParserData*)user_data;
1416 gtk_label_set_attributes (GTK_LABEL (buildable), data->attrs);
1417 pango_attr_list_unref (data->attrs);
1420 g_object_unref (data->object);
1421 g_object_unref (data->builder);
1422 g_slice_free (PangoParserData, data);
1429 * @str: The text of the label
1431 * Creates a new label with the given text inside it. You can
1432 * pass %NULL to get an empty label widget.
1434 * Return value: the new #GtkLabel
1437 gtk_label_new (const gchar *str)
1441 label = g_object_new (GTK_TYPE_LABEL, NULL);
1444 gtk_label_set_text (label, str);
1446 return GTK_WIDGET (label);
1450 * gtk_label_new_with_mnemonic:
1451 * @str: The text of the label, with an underscore in front of the
1452 * mnemonic character
1454 * Creates a new #GtkLabel, containing the text in @str.
1456 * If characters in @str are preceded by an underscore, they are
1457 * underlined. If you need a literal underscore character in a label, use
1458 * '__' (two underscores). The first underlined character represents a
1459 * keyboard accelerator called a mnemonic. The mnemonic key can be used
1460 * to activate another widget, chosen automatically, or explicitly using
1461 * gtk_label_set_mnemonic_widget().
1463 * If gtk_label_set_mnemonic_widget() is not called, then the first
1464 * activatable ancestor of the #GtkLabel will be chosen as the mnemonic
1465 * widget. For instance, if the label is inside a button or menu item,
1466 * the button or menu item will automatically become the mnemonic widget
1467 * and be activated by the mnemonic.
1469 * Return value: the new #GtkLabel
1472 gtk_label_new_with_mnemonic (const gchar *str)
1476 label = g_object_new (GTK_TYPE_LABEL, NULL);
1479 gtk_label_set_text_with_mnemonic (label, str);
1481 return GTK_WIDGET (label);
1485 gtk_label_mnemonic_activate (GtkWidget *widget,
1486 gboolean group_cycling)
1490 if (GTK_LABEL (widget)->mnemonic_widget)
1491 return gtk_widget_mnemonic_activate (GTK_LABEL (widget)->mnemonic_widget, group_cycling);
1493 /* Try to find the widget to activate by traversing the
1494 * widget's ancestry.
1496 parent = widget->parent;
1498 if (GTK_IS_NOTEBOOK (parent))
1503 if (gtk_widget_get_can_focus (parent) ||
1504 (!group_cycling && GTK_WIDGET_GET_CLASS (parent)->activate_signal) ||
1505 GTK_IS_NOTEBOOK (parent->parent) ||
1506 GTK_IS_MENU_ITEM (parent))
1507 return gtk_widget_mnemonic_activate (parent, group_cycling);
1508 parent = parent->parent;
1511 /* barf if there was nothing to activate */
1512 g_warning ("Couldn't find a target for a mnemonic activation.");
1513 gtk_widget_error_bell (widget);
1519 gtk_label_setup_mnemonic (GtkLabel *label,
1522 GtkWidget *widget = GTK_WIDGET (label);
1523 GtkWidget *toplevel;
1524 GtkWidget *mnemonic_menu;
1526 mnemonic_menu = g_object_get_data (G_OBJECT (label), "gtk-mnemonic-menu");
1528 if (last_key != GDK_VoidSymbol)
1530 if (label->mnemonic_window)
1532 gtk_window_remove_mnemonic (label->mnemonic_window,
1535 label->mnemonic_window = NULL;
1539 _gtk_menu_shell_remove_mnemonic (GTK_MENU_SHELL (mnemonic_menu),
1542 mnemonic_menu = NULL;
1546 if (label->mnemonic_keyval == GDK_VoidSymbol)
1549 connect_mnemonics_visible_notify (GTK_LABEL (widget));
1551 toplevel = gtk_widget_get_toplevel (widget);
1552 if (gtk_widget_is_toplevel (toplevel))
1554 GtkWidget *menu_shell;
1556 menu_shell = gtk_widget_get_ancestor (widget,
1557 GTK_TYPE_MENU_SHELL);
1561 _gtk_menu_shell_add_mnemonic (GTK_MENU_SHELL (menu_shell),
1562 label->mnemonic_keyval,
1564 mnemonic_menu = menu_shell;
1567 if (!GTK_IS_MENU (menu_shell))
1569 gtk_window_add_mnemonic (GTK_WINDOW (toplevel),
1570 label->mnemonic_keyval,
1572 label->mnemonic_window = GTK_WINDOW (toplevel);
1577 g_object_set_data (G_OBJECT (label), I_("gtk-mnemonic-menu"), mnemonic_menu);
1581 gtk_label_hierarchy_changed (GtkWidget *widget,
1582 GtkWidget *old_toplevel)
1584 GtkLabel *label = GTK_LABEL (widget);
1586 gtk_label_setup_mnemonic (label, label->mnemonic_keyval);
1590 label_shortcut_setting_apply (GtkLabel *label)
1592 gtk_label_recalculate (label);
1593 if (GTK_IS_ACCEL_LABEL (label))
1594 gtk_accel_label_refetch (GTK_ACCEL_LABEL (label));
1598 label_shortcut_setting_traverse_container (GtkWidget *widget,
1601 if (GTK_IS_LABEL (widget))
1602 label_shortcut_setting_apply (GTK_LABEL (widget));
1603 else if (GTK_IS_CONTAINER (widget))
1604 gtk_container_forall (GTK_CONTAINER (widget),
1605 label_shortcut_setting_traverse_container, data);
1609 label_shortcut_setting_changed (GtkSettings *settings)
1613 list = gtk_window_list_toplevels ();
1615 for (l = list; l ; l = l->next)
1617 GtkWidget *widget = l->data;
1619 if (gtk_widget_get_settings (widget) == settings)
1620 gtk_container_forall (GTK_CONTAINER (widget),
1621 label_shortcut_setting_traverse_container, NULL);
1628 mnemonics_visible_apply (GtkWidget *widget,
1629 gboolean mnemonics_visible)
1632 GtkLabelPrivate *priv;
1634 label = GTK_LABEL (widget);
1636 priv = GTK_LABEL_GET_PRIVATE (label);
1638 mnemonics_visible = mnemonics_visible != FALSE;
1640 if (priv->mnemonics_visible != mnemonics_visible)
1642 priv->mnemonics_visible = mnemonics_visible;
1644 gtk_label_recalculate (label);
1649 label_mnemonics_visible_traverse_container (GtkWidget *widget,
1652 gboolean mnemonics_visible = GPOINTER_TO_INT (data);
1654 _gtk_label_mnemonics_visible_apply_recursively (widget, mnemonics_visible);
1658 _gtk_label_mnemonics_visible_apply_recursively (GtkWidget *widget,
1659 gboolean mnemonics_visible)
1661 if (GTK_IS_LABEL (widget))
1662 mnemonics_visible_apply (widget, mnemonics_visible);
1663 else if (GTK_IS_CONTAINER (widget))
1664 gtk_container_forall (GTK_CONTAINER (widget),
1665 label_mnemonics_visible_traverse_container,
1666 GINT_TO_POINTER (mnemonics_visible));
1670 label_mnemonics_visible_changed (GtkWindow *window,
1674 gboolean mnemonics_visible;
1676 g_object_get (window, "mnemonics-visible", &mnemonics_visible, NULL);
1678 gtk_container_forall (GTK_CONTAINER (window),
1679 label_mnemonics_visible_traverse_container,
1680 GINT_TO_POINTER (mnemonics_visible));
1684 gtk_label_screen_changed (GtkWidget *widget,
1685 GdkScreen *old_screen)
1687 GtkSettings *settings;
1688 gboolean shortcuts_connected;
1690 if (!gtk_widget_has_screen (widget))
1693 settings = gtk_widget_get_settings (widget);
1695 shortcuts_connected =
1696 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (settings),
1697 "gtk-label-shortcuts-connected"));
1699 if (! shortcuts_connected)
1701 g_signal_connect (settings, "notify::gtk-enable-mnemonics",
1702 G_CALLBACK (label_shortcut_setting_changed),
1704 g_signal_connect (settings, "notify::gtk-enable-accels",
1705 G_CALLBACK (label_shortcut_setting_changed),
1708 g_object_set_data (G_OBJECT (settings), "gtk-label-shortcuts-connected",
1709 GINT_TO_POINTER (TRUE));
1712 label_shortcut_setting_apply (GTK_LABEL (widget));
1717 label_mnemonic_widget_weak_notify (gpointer data,
1718 GObject *where_the_object_was)
1720 GtkLabel *label = data;
1722 label->mnemonic_widget = NULL;
1723 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1727 * gtk_label_set_mnemonic_widget:
1728 * @label: a #GtkLabel
1729 * @widget: (allow-none): the target #GtkWidget
1731 * If the label has been set so that it has an mnemonic key (using
1732 * i.e. gtk_label_set_markup_with_mnemonic(),
1733 * gtk_label_set_text_with_mnemonic(), gtk_label_new_with_mnemonic()
1734 * or the "use_underline" property) the label can be associated with a
1735 * widget that is the target of the mnemonic. When the label is inside
1736 * a widget (like a #GtkButton or a #GtkNotebook tab) it is
1737 * automatically associated with the correct widget, but sometimes
1738 * (i.e. when the target is a #GtkEntry next to the label) you need to
1739 * set it explicitly using this function.
1741 * The target widget will be accelerated by emitting the
1742 * GtkWidget::mnemonic-activate signal on it. The default handler for
1743 * this signal will activate the widget if there are no mnemonic collisions
1744 * and toggle focus between the colliding widgets otherwise.
1747 gtk_label_set_mnemonic_widget (GtkLabel *label,
1750 g_return_if_fail (GTK_IS_LABEL (label));
1752 g_return_if_fail (GTK_IS_WIDGET (widget));
1754 if (label->mnemonic_widget)
1756 gtk_widget_remove_mnemonic_label (label->mnemonic_widget, GTK_WIDGET (label));
1757 g_object_weak_unref (G_OBJECT (label->mnemonic_widget),
1758 label_mnemonic_widget_weak_notify,
1761 label->mnemonic_widget = widget;
1762 if (label->mnemonic_widget)
1764 g_object_weak_ref (G_OBJECT (label->mnemonic_widget),
1765 label_mnemonic_widget_weak_notify,
1767 gtk_widget_add_mnemonic_label (label->mnemonic_widget, GTK_WIDGET (label));
1770 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1774 * gtk_label_get_mnemonic_widget:
1775 * @label: a #GtkLabel
1777 * Retrieves the target of the mnemonic (keyboard shortcut) of this
1778 * label. See gtk_label_set_mnemonic_widget().
1780 * Return value: the target of the label's mnemonic, or %NULL if none
1781 * has been set and the default algorithm will be used.
1784 gtk_label_get_mnemonic_widget (GtkLabel *label)
1786 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1788 return label->mnemonic_widget;
1792 * gtk_label_get_mnemonic_keyval:
1793 * @label: a #GtkLabel
1795 * If the label has been set so that it has an mnemonic key this function
1796 * returns the keyval used for the mnemonic accelerator. If there is no
1797 * mnemonic set up it returns #GDK_VoidSymbol.
1799 * Returns: GDK keyval usable for accelerators, or #GDK_VoidSymbol
1802 gtk_label_get_mnemonic_keyval (GtkLabel *label)
1804 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_VoidSymbol);
1806 return label->mnemonic_keyval;
1810 gtk_label_set_text_internal (GtkLabel *label,
1813 g_free (label->text);
1817 gtk_label_select_region_index (label, 0, 0);
1821 gtk_label_set_label_internal (GtkLabel *label,
1824 g_free (label->label);
1828 g_object_notify (G_OBJECT (label), "label");
1832 gtk_label_set_use_markup_internal (GtkLabel *label,
1836 if (label->use_markup != val)
1838 label->use_markup = val;
1840 g_object_notify (G_OBJECT (label), "use-markup");
1845 gtk_label_set_use_underline_internal (GtkLabel *label,
1849 if (label->use_underline != val)
1851 label->use_underline = val;
1853 g_object_notify (G_OBJECT (label), "use-underline");
1858 gtk_label_compose_effective_attrs (GtkLabel *label)
1860 PangoAttrIterator *iter;
1861 PangoAttribute *attr;
1862 GSList *iter_attrs, *l;
1866 if (label->effective_attrs)
1868 if ((iter = pango_attr_list_get_iterator (label->attrs)))
1872 iter_attrs = pango_attr_iterator_get_attrs (iter);
1873 for (l = iter_attrs; l; l = l->next)
1876 pango_attr_list_insert (label->effective_attrs, attr);
1878 g_slist_free (iter_attrs);
1880 while (pango_attr_iterator_next (iter));
1881 pango_attr_iterator_destroy (iter);
1885 label->effective_attrs =
1886 pango_attr_list_ref (label->attrs);
1891 gtk_label_set_attributes_internal (GtkLabel *label,
1892 PangoAttrList *attrs)
1895 pango_attr_list_ref (attrs);
1898 pango_attr_list_unref (label->attrs);
1899 label->attrs = attrs;
1901 g_object_notify (G_OBJECT (label), "attributes");
1905 /* Calculates text, attrs and mnemonic_keyval from
1906 * label, use_underline and use_markup
1909 gtk_label_recalculate (GtkLabel *label)
1911 guint keyval = label->mnemonic_keyval;
1913 if (label->use_markup)
1914 gtk_label_set_markup_internal (label, label->label, label->use_underline);
1917 if (label->use_underline)
1918 gtk_label_set_uline_text_internal (label, label->label);
1921 if (label->effective_attrs)
1922 pango_attr_list_unref (label->effective_attrs);
1923 label->effective_attrs = NULL;
1924 gtk_label_set_text_internal (label, g_strdup (label->label));
1928 gtk_label_compose_effective_attrs (label);
1930 if (!label->use_underline)
1931 label->mnemonic_keyval = GDK_VoidSymbol;
1933 if (keyval != label->mnemonic_keyval)
1935 gtk_label_setup_mnemonic (label, keyval);
1936 g_object_notify (G_OBJECT (label), "mnemonic-keyval");
1939 gtk_label_clear_layout (label);
1940 gtk_label_clear_select_info (label);
1941 gtk_widget_queue_resize (GTK_WIDGET (label));
1945 * gtk_label_set_text:
1946 * @label: a #GtkLabel
1947 * @str: The text you want to set
1949 * Sets the text within the #GtkLabel widget. It overwrites any text that
1952 * This will also clear any previously set mnemonic accelerators.
1955 gtk_label_set_text (GtkLabel *label,
1958 g_return_if_fail (GTK_IS_LABEL (label));
1960 g_object_freeze_notify (G_OBJECT (label));
1962 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
1963 gtk_label_set_use_markup_internal (label, FALSE);
1964 gtk_label_set_use_underline_internal (label, FALSE);
1966 gtk_label_recalculate (label);
1968 g_object_thaw_notify (G_OBJECT (label));
1972 * gtk_label_set_attributes:
1973 * @label: a #GtkLabel
1974 * @attrs: a #PangoAttrList
1976 * Sets a #PangoAttrList; the attributes in the list are applied to the
1979 * <note><para>The attributes set with this function will be applied
1980 * and merged with any other attributes previously effected by way
1981 * of the #GtkLabel:use-underline or #GtkLabel:use-markup properties.
1982 * While it is not recommended to mix markup strings with manually set
1983 * attributes, if you must; know that the attributes will be applied
1984 * to the label after the markup string is parsed.</para></note>
1987 gtk_label_set_attributes (GtkLabel *label,
1988 PangoAttrList *attrs)
1990 g_return_if_fail (GTK_IS_LABEL (label));
1992 gtk_label_set_attributes_internal (label, attrs);
1994 gtk_label_recalculate (label);
1996 gtk_label_clear_layout (label);
1997 gtk_widget_queue_resize (GTK_WIDGET (label));
2001 * gtk_label_get_attributes:
2002 * @label: a #GtkLabel
2004 * Gets the attribute list that was set on the label using
2005 * gtk_label_set_attributes(), if any. This function does
2006 * not reflect attributes that come from the labels markup
2007 * (see gtk_label_set_markup()). If you want to get the
2008 * effective attributes for the label, use
2009 * pango_layout_get_attribute (gtk_label_get_layout (label)).
2011 * Return value: the attribute list, or %NULL if none was set.
2014 gtk_label_get_attributes (GtkLabel *label)
2016 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2018 return label->attrs;
2022 * gtk_label_set_label:
2023 * @label: a #GtkLabel
2024 * @str: the new text to set for the label
2026 * Sets the text of the label. The label is interpreted as
2027 * including embedded underlines and/or Pango markup depending
2028 * on the values of the #GtkLabel:use-underline" and
2029 * #GtkLabel:use-markup properties.
2032 gtk_label_set_label (GtkLabel *label,
2035 g_return_if_fail (GTK_IS_LABEL (label));
2037 g_object_freeze_notify (G_OBJECT (label));
2039 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2040 gtk_label_recalculate (label);
2042 g_object_thaw_notify (G_OBJECT (label));
2046 * gtk_label_get_label:
2047 * @label: a #GtkLabel
2049 * Fetches the text from a label widget including any embedded
2050 * underlines indicating mnemonics and Pango markup. (See
2051 * gtk_label_get_text()).
2053 * Return value: the text of the label widget. This string is
2054 * owned by the widget and must not be modified or freed.
2056 G_CONST_RETURN gchar *
2057 gtk_label_get_label (GtkLabel *label)
2059 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2061 return label->label;
2069 GdkColor *link_color;
2070 GdkColor *visited_link_color;
2074 start_element_handler (GMarkupParseContext *context,
2075 const gchar *element_name,
2076 const gchar **attribute_names,
2077 const gchar **attribute_values,
2081 UriParserData *pdata = user_data;
2083 if (strcmp (element_name, "a") == 0)
2086 const gchar *uri = NULL;
2087 const gchar *title = NULL;
2088 gboolean visited = FALSE;
2092 GdkColor *color = NULL;
2094 g_markup_parse_context_get_position (context, &line_number, &char_number);
2096 for (i = 0; attribute_names[i] != NULL; i++)
2098 const gchar *attr = attribute_names[i];
2100 if (strcmp (attr, "href") == 0)
2101 uri = attribute_values[i];
2102 else if (strcmp (attr, "title") == 0)
2103 title = attribute_values[i];
2108 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
2109 "Attribute '%s' is not allowed on the <a> tag "
2110 "on line %d char %d",
2111 attr, line_number, char_number);
2120 G_MARKUP_ERROR_INVALID_CONTENT,
2121 "Attribute 'href' was missing on the <a> tag "
2122 "on line %d char %d",
2123 line_number, char_number);
2128 if (pdata->label->track_links && pdata->label->select_info)
2131 for (l = pdata->label->select_info->links; l; l = l->next)
2134 if (strcmp (uri, link->uri) == 0)
2136 visited = link->visited;
2143 color = pdata->visited_link_color;
2145 color = pdata->link_color;
2147 g_string_append_printf (pdata->new_str,
2148 "<span color=\"#%04x%04x%04x\" underline=\"single\">",
2153 link = g_new0 (GtkLabelLink, 1);
2154 link->uri = g_strdup (uri);
2155 link->title = g_strdup (title);
2156 link->visited = visited;
2157 pdata->links = g_list_append (pdata->links, link);
2163 g_string_append_c (pdata->new_str, '<');
2164 g_string_append (pdata->new_str, element_name);
2166 for (i = 0; attribute_names[i] != NULL; i++)
2168 const gchar *attr = attribute_names[i];
2169 const gchar *value = attribute_values[i];
2172 newvalue = g_markup_escape_text (value, -1);
2174 g_string_append_c (pdata->new_str, ' ');
2175 g_string_append (pdata->new_str, attr);
2176 g_string_append (pdata->new_str, "=\"");
2177 g_string_append (pdata->new_str, newvalue);
2178 g_string_append_c (pdata->new_str, '\"');
2182 g_string_append_c (pdata->new_str, '>');
2187 end_element_handler (GMarkupParseContext *context,
2188 const gchar *element_name,
2192 UriParserData *pdata = user_data;
2194 if (!strcmp (element_name, "a"))
2195 g_string_append (pdata->new_str, "</span>");
2198 g_string_append (pdata->new_str, "</");
2199 g_string_append (pdata->new_str, element_name);
2200 g_string_append_c (pdata->new_str, '>');
2205 text_handler (GMarkupParseContext *context,
2211 UriParserData *pdata = user_data;
2214 newtext = g_markup_escape_text (text, text_len);
2215 g_string_append (pdata->new_str, newtext);
2219 static const GMarkupParser markup_parser =
2221 start_element_handler,
2222 end_element_handler,
2229 xml_isspace (gchar c)
2231 return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
2235 link_free (GtkLabelLink *link)
2238 g_free (link->title);
2243 gtk_label_get_link_colors (GtkWidget *widget,
2244 GdkColor **link_color,
2245 GdkColor **visited_link_color)
2247 gtk_widget_ensure_style (widget);
2248 gtk_widget_style_get (widget,
2249 "link-color", link_color,
2250 "visited-link-color", visited_link_color,
2253 *link_color = gdk_color_copy (&default_link_color);
2254 if (!*visited_link_color)
2255 *visited_link_color = gdk_color_copy (&default_visited_link_color);
2259 parse_uri_markup (GtkLabel *label,
2265 GMarkupParseContext *context = NULL;
2266 const gchar *p, *end;
2267 gboolean needs_root = TRUE;
2269 UriParserData pdata;
2271 length = strlen (str);
2275 pdata.label = label;
2277 pdata.new_str = g_string_sized_new (length);
2279 gtk_label_get_link_colors (GTK_WIDGET (label), &pdata.link_color, &pdata.visited_link_color);
2281 while (p != end && xml_isspace (*p))
2284 if (end - p >= 8 && strncmp (p, "<markup>", 8) == 0)
2287 context = g_markup_parse_context_new (&markup_parser, 0, &pdata, NULL);
2291 if (!g_markup_parse_context_parse (context, "<markup>", -1, error))
2295 if (!g_markup_parse_context_parse (context, str, length, error))
2300 if (!g_markup_parse_context_parse (context, "</markup>", -1, error))
2304 if (!g_markup_parse_context_end_parse (context, error))
2307 g_markup_parse_context_free (context);
2309 *new_str = g_string_free (pdata.new_str, FALSE);
2310 *links = pdata.links;
2312 gdk_color_free (pdata.link_color);
2313 gdk_color_free (pdata.visited_link_color);
2318 g_markup_parse_context_free (context);
2319 g_string_free (pdata.new_str, TRUE);
2320 g_list_foreach (pdata.links, (GFunc)link_free, NULL);
2321 g_list_free (pdata.links);
2322 gdk_color_free (pdata.link_color);
2323 gdk_color_free (pdata.visited_link_color);
2329 gtk_label_ensure_has_tooltip (GtkLabel *label)
2332 gboolean has_tooltip = FALSE;
2334 for (l = label->select_info->links; l; l = l->next)
2336 GtkLabelLink *link = l->data;
2344 gtk_widget_set_has_tooltip (GTK_WIDGET (label), has_tooltip);
2348 gtk_label_set_markup_internal (GtkLabel *label,
2350 gboolean with_uline)
2352 GtkLabelPrivate *priv = GTK_LABEL_GET_PRIVATE (label);
2354 GError *error = NULL;
2355 PangoAttrList *attrs = NULL;
2356 gunichar accel_char = 0;
2358 GList *links = NULL;
2360 if (!parse_uri_markup (label, str, &new_str, &links, &error))
2362 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2364 g_error_free (error);
2368 gtk_label_clear_links (label);
2371 gtk_label_ensure_select_info (label);
2372 label->select_info->links = links;
2373 gtk_label_ensure_has_tooltip (label);
2378 gboolean enable_mnemonics;
2379 gboolean auto_mnemonics;
2381 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2382 "gtk-enable-mnemonics", &enable_mnemonics,
2383 "gtk-auto-mnemonics", &auto_mnemonics,
2386 if (!(enable_mnemonics && priv->mnemonics_visible &&
2388 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2389 (!label->mnemonic_widget ||
2390 gtk_widget_is_sensitive (label->mnemonic_widget))))))
2396 if (separate_uline_pattern (new_str, &key, &tmp, &pattern))
2405 if (!pango_parse_markup (new_str,
2407 with_uline ? '_' : 0,
2410 with_uline ? &accel_char : NULL,
2413 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2416 g_error_free (error);
2423 gtk_label_set_text_internal (label, text);
2427 if (label->effective_attrs)
2428 pango_attr_list_unref (label->effective_attrs);
2429 label->effective_attrs = attrs;
2432 if (accel_char != 0)
2433 label->mnemonic_keyval = gdk_keyval_to_lower (gdk_unicode_to_keyval (accel_char));
2435 label->mnemonic_keyval = GDK_VoidSymbol;
2439 * gtk_label_set_markup:
2440 * @label: a #GtkLabel
2441 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
2443 * Parses @str which is marked up with the <link
2444 * linkend="PangoMarkupFormat">Pango text markup language</link>, setting the
2445 * label's text and attribute list based on the parse results. If the @str is
2446 * external data, you may need to escape it with g_markup_escape_text() or
2447 * g_markup_printf_escaped()<!-- -->:
2451 * markup = g_markup_printf_escaped ("<span style=\"italic\">%s</span>", str);
2452 * gtk_label_set_markup (GTK_LABEL (label), markup);
2457 gtk_label_set_markup (GtkLabel *label,
2460 g_return_if_fail (GTK_IS_LABEL (label));
2462 g_object_freeze_notify (G_OBJECT (label));
2464 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2465 gtk_label_set_use_markup_internal (label, TRUE);
2466 gtk_label_set_use_underline_internal (label, FALSE);
2468 gtk_label_recalculate (label);
2470 g_object_thaw_notify (G_OBJECT (label));
2474 * gtk_label_set_markup_with_mnemonic:
2475 * @label: a #GtkLabel
2476 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
2478 * Parses @str which is marked up with the <link linkend="PangoMarkupFormat">Pango text markup language</link>,
2479 * setting the label's text and attribute list based on the parse results.
2480 * If characters in @str are preceded by an underscore, they are underlined
2481 * indicating that they represent a keyboard accelerator called a mnemonic.
2483 * The mnemonic key can be used to activate another widget, chosen
2484 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
2487 gtk_label_set_markup_with_mnemonic (GtkLabel *label,
2490 g_return_if_fail (GTK_IS_LABEL (label));
2492 g_object_freeze_notify (G_OBJECT (label));
2494 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2495 gtk_label_set_use_markup_internal (label, TRUE);
2496 gtk_label_set_use_underline_internal (label, TRUE);
2498 gtk_label_recalculate (label);
2500 g_object_thaw_notify (G_OBJECT (label));
2504 * gtk_label_get_text:
2505 * @label: a #GtkLabel
2507 * Fetches the text from a label widget, as displayed on the
2508 * screen. This does not include any embedded underlines
2509 * indicating mnemonics or Pango markup. (See gtk_label_get_label())
2511 * Return value: the text in the label widget. This is the internal
2512 * string used by the label, and must not be modified.
2514 G_CONST_RETURN gchar *
2515 gtk_label_get_text (GtkLabel *label)
2517 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2522 static PangoAttrList *
2523 gtk_label_pattern_to_attrs (GtkLabel *label,
2524 const gchar *pattern)
2527 const char *p = label->text;
2528 const char *q = pattern;
2529 PangoAttrList *attrs;
2531 attrs = pango_attr_list_new ();
2535 while (*p && *q && *q != '_')
2537 p = g_utf8_next_char (p);
2541 while (*p && *q && *q == '_')
2543 p = g_utf8_next_char (p);
2549 PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_LOW);
2550 attr->start_index = start - label->text;
2551 attr->end_index = p - label->text;
2553 pango_attr_list_insert (attrs, attr);
2563 gtk_label_set_pattern_internal (GtkLabel *label,
2564 const gchar *pattern,
2565 gboolean is_mnemonic)
2567 GtkLabelPrivate *priv = GTK_LABEL_GET_PRIVATE (label);
2568 PangoAttrList *attrs;
2569 gboolean enable_mnemonics;
2570 gboolean auto_mnemonics;
2572 g_return_if_fail (GTK_IS_LABEL (label));
2574 if (label->pattern_set)
2579 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2580 "gtk-enable-mnemonics", &enable_mnemonics,
2581 "gtk-auto-mnemonics", &auto_mnemonics,
2584 if (enable_mnemonics && priv->mnemonics_visible && pattern &&
2586 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2587 (!label->mnemonic_widget ||
2588 gtk_widget_is_sensitive (label->mnemonic_widget)))))
2589 attrs = gtk_label_pattern_to_attrs (label, pattern);
2594 attrs = gtk_label_pattern_to_attrs (label, pattern);
2596 if (label->effective_attrs)
2597 pango_attr_list_unref (label->effective_attrs);
2598 label->effective_attrs = attrs;
2602 gtk_label_set_pattern (GtkLabel *label,
2603 const gchar *pattern)
2605 g_return_if_fail (GTK_IS_LABEL (label));
2607 label->pattern_set = FALSE;
2611 gtk_label_set_pattern_internal (label, pattern, FALSE);
2612 label->pattern_set = TRUE;
2615 gtk_label_recalculate (label);
2617 gtk_label_clear_layout (label);
2618 gtk_widget_queue_resize (GTK_WIDGET (label));
2623 * gtk_label_set_justify:
2624 * @label: a #GtkLabel
2625 * @jtype: a #GtkJustification
2627 * Sets the alignment of the lines in the text of the label relative to
2628 * each other. %GTK_JUSTIFY_LEFT is the default value when the
2629 * widget is first created with gtk_label_new(). If you instead want
2630 * to set the alignment of the label as a whole, use
2631 * gtk_misc_set_alignment() instead. gtk_label_set_justify() has no
2632 * effect on labels containing only a single line.
2635 gtk_label_set_justify (GtkLabel *label,
2636 GtkJustification jtype)
2638 g_return_if_fail (GTK_IS_LABEL (label));
2639 g_return_if_fail (jtype >= GTK_JUSTIFY_LEFT && jtype <= GTK_JUSTIFY_FILL);
2641 if ((GtkJustification) label->jtype != jtype)
2643 label->jtype = jtype;
2645 /* No real need to be this drastic, but easier than duplicating the code */
2646 gtk_label_clear_layout (label);
2648 g_object_notify (G_OBJECT (label), "justify");
2649 gtk_widget_queue_resize (GTK_WIDGET (label));
2654 * gtk_label_get_justify:
2655 * @label: a #GtkLabel
2657 * Returns the justification of the label. See gtk_label_set_justify().
2659 * Return value: #GtkJustification
2662 gtk_label_get_justify (GtkLabel *label)
2664 g_return_val_if_fail (GTK_IS_LABEL (label), 0);
2666 return label->jtype;
2670 * gtk_label_set_ellipsize:
2671 * @label: a #GtkLabel
2672 * @mode: a #PangoEllipsizeMode
2674 * Sets the mode used to ellipsize (add an ellipsis: "...") to the text
2675 * if there is not enough space to render the entire string.
2680 gtk_label_set_ellipsize (GtkLabel *label,
2681 PangoEllipsizeMode mode)
2683 g_return_if_fail (GTK_IS_LABEL (label));
2684 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE && mode <= PANGO_ELLIPSIZE_END);
2686 if ((PangoEllipsizeMode) label->ellipsize != mode)
2688 label->ellipsize = mode;
2690 /* No real need to be this drastic, but easier than duplicating the code */
2691 gtk_label_clear_layout (label);
2693 g_object_notify (G_OBJECT (label), "ellipsize");
2694 gtk_widget_queue_resize (GTK_WIDGET (label));
2699 * gtk_label_get_ellipsize:
2700 * @label: a #GtkLabel
2702 * Returns the ellipsizing position of the label. See gtk_label_set_ellipsize().
2704 * Return value: #PangoEllipsizeMode
2709 gtk_label_get_ellipsize (GtkLabel *label)
2711 g_return_val_if_fail (GTK_IS_LABEL (label), PANGO_ELLIPSIZE_NONE);
2713 return label->ellipsize;
2717 * gtk_label_set_width_chars:
2718 * @label: a #GtkLabel
2719 * @n_chars: the new desired width, in characters.
2721 * Sets the desired width in characters of @label to @n_chars.
2726 gtk_label_set_width_chars (GtkLabel *label,
2729 GtkLabelPrivate *priv;
2731 g_return_if_fail (GTK_IS_LABEL (label));
2733 priv = GTK_LABEL_GET_PRIVATE (label);
2735 if (priv->width_chars != n_chars)
2737 priv->width_chars = n_chars;
2738 g_object_notify (G_OBJECT (label), "width-chars");
2739 gtk_label_invalidate_wrap_width (label);
2740 gtk_widget_queue_resize (GTK_WIDGET (label));
2745 * gtk_label_get_width_chars:
2746 * @label: a #GtkLabel
2748 * Retrieves the desired width of @label, in characters. See
2749 * gtk_label_set_width_chars().
2751 * Return value: the width of the label in characters.
2756 gtk_label_get_width_chars (GtkLabel *label)
2758 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2760 return GTK_LABEL_GET_PRIVATE (label)->width_chars;
2764 * gtk_label_set_max_width_chars:
2765 * @label: a #GtkLabel
2766 * @n_chars: the new desired maximum width, in characters.
2768 * Sets the desired maximum width in characters of @label to @n_chars.
2773 gtk_label_set_max_width_chars (GtkLabel *label,
2776 GtkLabelPrivate *priv;
2778 g_return_if_fail (GTK_IS_LABEL (label));
2780 priv = GTK_LABEL_GET_PRIVATE (label);
2782 if (priv->max_width_chars != n_chars)
2784 priv->max_width_chars = n_chars;
2786 g_object_notify (G_OBJECT (label), "max-width-chars");
2787 gtk_label_invalidate_wrap_width (label);
2788 gtk_widget_queue_resize (GTK_WIDGET (label));
2793 * gtk_label_get_max_width_chars:
2794 * @label: a #GtkLabel
2796 * Retrieves the desired maximum width of @label, in characters. See
2797 * gtk_label_set_width_chars().
2799 * Return value: the maximum width of the label in characters.
2804 gtk_label_get_max_width_chars (GtkLabel *label)
2806 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2808 return GTK_LABEL_GET_PRIVATE (label)->max_width_chars;
2812 * gtk_label_set_line_wrap:
2813 * @label: a #GtkLabel
2814 * @wrap: the setting
2816 * Toggles line wrapping within the #GtkLabel widget. %TRUE makes it break
2817 * lines if text exceeds the widget's size. %FALSE lets the text get cut off
2818 * by the edge of the widget if it exceeds the widget size.
2820 * Note that setting line wrapping to %TRUE does not make the label
2821 * wrap at its parent container's width, because GTK+ widgets
2822 * conceptually can't make their requisition depend on the parent
2823 * container's size. For a label that wraps at a specific position,
2824 * set the label's width using gtk_widget_set_size_request().
2827 gtk_label_set_line_wrap (GtkLabel *label,
2830 g_return_if_fail (GTK_IS_LABEL (label));
2832 wrap = wrap != FALSE;
2834 if (label->wrap != wrap)
2838 gtk_label_clear_layout (label);
2839 gtk_widget_queue_resize (GTK_WIDGET (label));
2840 g_object_notify (G_OBJECT (label), "wrap");
2845 * gtk_label_get_line_wrap:
2846 * @label: a #GtkLabel
2848 * Returns whether lines in the label are automatically wrapped.
2849 * See gtk_label_set_line_wrap().
2851 * Return value: %TRUE if the lines of the label are automatically wrapped.
2854 gtk_label_get_line_wrap (GtkLabel *label)
2856 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
2862 * gtk_label_set_line_wrap_mode:
2863 * @label: a #GtkLabel
2864 * @wrap_mode: the line wrapping mode
2866 * If line wrapping is on (see gtk_label_set_line_wrap()) this controls how
2867 * the line wrapping is done. The default is %PANGO_WRAP_WORD which means
2868 * wrap on word boundaries.
2873 gtk_label_set_line_wrap_mode (GtkLabel *label,
2874 PangoWrapMode wrap_mode)
2876 g_return_if_fail (GTK_IS_LABEL (label));
2878 if (label->wrap_mode != wrap_mode)
2880 label->wrap_mode = wrap_mode;
2881 g_object_notify (G_OBJECT (label), "wrap-mode");
2883 gtk_widget_queue_resize (GTK_WIDGET (label));
2888 * gtk_label_get_line_wrap_mode:
2889 * @label: a #GtkLabel
2891 * Returns line wrap mode used by the label. See gtk_label_set_line_wrap_mode().
2893 * Return value: %TRUE if the lines of the label are automatically wrapped.
2898 gtk_label_get_line_wrap_mode (GtkLabel *label)
2900 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
2902 return label->wrap_mode;
2907 gtk_label_get (GtkLabel *label,
2910 g_return_if_fail (GTK_IS_LABEL (label));
2911 g_return_if_fail (str != NULL);
2917 gtk_label_destroy (GtkObject *object)
2919 GtkLabel *label = GTK_LABEL (object);
2921 gtk_label_set_mnemonic_widget (label, NULL);
2923 GTK_OBJECT_CLASS (gtk_label_parent_class)->destroy (object);
2927 gtk_label_finalize (GObject *object)
2929 GtkLabel *label = GTK_LABEL (object);
2931 g_free (label->label);
2932 g_free (label->text);
2935 g_object_unref (label->layout);
2938 pango_attr_list_unref (label->attrs);
2940 if (label->effective_attrs)
2941 pango_attr_list_unref (label->effective_attrs);
2943 gtk_label_clear_links (label);
2944 g_free (label->select_info);
2946 G_OBJECT_CLASS (gtk_label_parent_class)->finalize (object);
2950 gtk_label_clear_layout (GtkLabel *label)
2954 g_object_unref (label->layout);
2955 label->layout = NULL;
2957 //gtk_label_clear_links (label);
2962 get_label_char_width (GtkLabel *label)
2964 GtkLabelPrivate *priv;
2965 PangoContext *context;
2966 PangoFontMetrics *metrics;
2967 gint char_width, digit_width, char_pixels, w;
2969 priv = GTK_LABEL_GET_PRIVATE (label);
2971 context = pango_layout_get_context (label->layout);
2972 metrics = pango_context_get_metrics (context, GTK_WIDGET (label)->style->font_desc,
2973 pango_context_get_language (context));
2975 char_width = pango_font_metrics_get_approximate_char_width (metrics);
2976 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
2977 char_pixels = MAX (char_width, digit_width);
2978 pango_font_metrics_unref (metrics);
2980 if (priv->width_chars < 0)
2982 PangoRectangle rect;
2984 pango_layout_set_width (label->layout, -1);
2985 pango_layout_get_extents (label->layout, NULL, &rect);
2987 w = char_pixels * MAX (priv->max_width_chars, 3);
2988 w = MIN (rect.width, w);
2992 /* enforce minimum width for ellipsized labels at ~3 chars */
2993 w = char_pixels * MAX (priv->width_chars, 3);
3000 gtk_label_invalidate_wrap_width (GtkLabel *label)
3002 GtkLabelPrivate *priv;
3004 priv = GTK_LABEL_GET_PRIVATE (label);
3006 priv->wrap_width = -1;
3010 get_label_wrap_width (GtkLabel *label)
3012 GtkLabelPrivate *priv;
3014 priv = GTK_LABEL_GET_PRIVATE (label);
3016 if (priv->wrap_width < 0)
3018 if (priv->width_chars > 0 || priv->max_width_chars > 0)
3019 priv->wrap_width = get_label_char_width (label);
3022 PangoLayout *layout;
3024 layout = gtk_widget_create_pango_layout (GTK_WIDGET (label),
3025 "This long string gives a good enough length for any line to have.");
3026 pango_layout_get_size (layout, &priv->wrap_width, NULL);
3027 g_object_unref (layout);
3031 return priv->wrap_width;
3035 gtk_label_ensure_layout (GtkLabel *label)
3038 PangoRectangle logical_rect;
3041 widget = GTK_WIDGET (label);
3043 rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
3047 PangoAlignment align = PANGO_ALIGN_LEFT; /* Quiet gcc */
3048 gdouble angle = gtk_label_get_angle (label);
3050 if (angle != 0.0 && !label->select_info)
3052 PangoMatrix matrix = PANGO_MATRIX_INIT;
3054 /* We rotate the standard singleton PangoContext for the widget,
3055 * depending on the fact that it's meant pretty much exclusively
3058 pango_matrix_rotate (&matrix, angle);
3060 pango_context_set_matrix (gtk_widget_get_pango_context (widget), &matrix);
3062 label->have_transform = TRUE;
3066 if (label->have_transform)
3067 pango_context_set_matrix (gtk_widget_get_pango_context (widget), NULL);
3069 label->have_transform = FALSE;
3072 label->layout = gtk_widget_create_pango_layout (widget, label->text);
3074 if (label->effective_attrs)
3075 pango_layout_set_attributes (label->layout, label->effective_attrs);
3077 gtk_label_rescan_links (label);
3079 switch (label->jtype)
3081 case GTK_JUSTIFY_LEFT:
3082 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3084 case GTK_JUSTIFY_RIGHT:
3085 align = rtl ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
3087 case GTK_JUSTIFY_CENTER:
3088 align = PANGO_ALIGN_CENTER;
3090 case GTK_JUSTIFY_FILL:
3091 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3092 pango_layout_set_justify (label->layout, TRUE);
3095 g_assert_not_reached();
3098 pango_layout_set_alignment (label->layout, align);
3099 pango_layout_set_ellipsize (label->layout, label->ellipsize);
3100 pango_layout_set_single_paragraph_mode (label->layout, label->single_line_mode);
3102 if (label->ellipsize)
3103 pango_layout_set_width (label->layout,
3104 widget->allocation.width * PANGO_SCALE);
3105 else if (label->wrap)
3107 GtkWidgetAuxInfo *aux_info;
3108 gint longest_paragraph;
3111 pango_layout_set_wrap (label->layout, label->wrap_mode);
3113 aux_info = _gtk_widget_get_aux_info (widget, FALSE);
3114 if (aux_info && aux_info->width > 0)
3115 pango_layout_set_width (label->layout, aux_info->width * PANGO_SCALE);
3118 GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (label));
3121 pango_layout_set_width (label->layout, -1);
3122 pango_layout_get_extents (label->layout, NULL, &logical_rect);
3124 width = logical_rect.width;
3126 /* Try to guess a reasonable maximum width */
3127 longest_paragraph = width;
3129 wrap_width = get_label_wrap_width (label);
3130 width = MIN (width, wrap_width);
3132 PANGO_SCALE * (gdk_screen_get_width (screen) + 1) / 2);
3134 pango_layout_set_width (label->layout, width);
3135 pango_layout_get_extents (label->layout, NULL, &logical_rect);
3136 width = logical_rect.width;
3137 height = logical_rect.height;
3139 /* Unfortunately, the above may leave us with a very unbalanced looking paragraph,
3140 * so we try short search for a narrower width that leaves us with the same height
3142 if (longest_paragraph > 0)
3144 gint nlines, perfect_width;
3146 nlines = pango_layout_get_line_count (label->layout);
3147 perfect_width = (longest_paragraph + nlines - 1) / nlines;
3149 if (perfect_width < width)
3151 pango_layout_set_width (label->layout, perfect_width);
3152 pango_layout_get_extents (label->layout, NULL, &logical_rect);
3154 if (logical_rect.height <= height)
3155 width = logical_rect.width;
3158 gint mid_width = (perfect_width + width) / 2;
3160 if (mid_width > perfect_width)
3162 pango_layout_set_width (label->layout, mid_width);
3163 pango_layout_get_extents (label->layout, NULL, &logical_rect);
3165 if (logical_rect.height <= height)
3166 width = logical_rect.width;
3171 pango_layout_set_width (label->layout, width);
3174 else /* !label->wrap */
3175 pango_layout_set_width (label->layout, -1);
3180 get_single_line_height (GtkWidget *widget,
3181 PangoLayout *layout)
3183 PangoContext *context;
3184 PangoFontMetrics *metrics;
3185 gint ascent, descent;
3187 context = pango_layout_get_context (layout);
3188 metrics = pango_context_get_metrics (context, widget->style->font_desc,
3189 pango_context_get_language (context));
3191 ascent = pango_font_metrics_get_ascent (metrics);
3192 descent = pango_font_metrics_get_descent (metrics);
3193 pango_font_metrics_unref (metrics);
3195 return PANGO_PIXELS (ascent + descent);
3199 gtk_label_get_desired_size (GtkExtendedLayout *layout,
3200 GtkRequisition *minimum_size,
3201 GtkRequisition *natural_size)
3203 GtkLabelPrivate *priv = GTK_LABEL_GET_PRIVATE (layout);
3204 GtkLabel *label = GTK_LABEL (layout);
3205 PangoRectangle required_rect;
3208 * If word wrapping is on, then the height requisition can depend
3211 * - Any width set on the widget via gtk_widget_set_size_request().
3212 * - The padding of the widget (xpad, set by gtk_misc_set_padding)
3214 * Instead of trying to detect changes to these quantities, if we
3215 * are wrapping, we just rewrap for each size request. Since
3216 * size requisitions are cached by the GTK+ core, this is not
3221 gtk_label_clear_layout (label);
3223 gtk_label_ensure_layout (label);
3227 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (GTK_WIDGET (label), FALSE);
3229 pango_layout_get_extents (label->layout, NULL, &required_rect);
3230 required_rect.x = required_rect.y = 0;
3232 if (label->ellipsize || priv->width_chars > 0 || priv->max_width_chars > 0)
3234 /* backup the Pango layout, as get_label_char_width() scrambles it */
3236 PangoLayout *backup = label->layout;
3237 label->layout = pango_layout_copy (label->layout);
3239 required_rect.width = get_label_char_width (label);
3241 g_object_unref (label->layout);
3242 label->layout = backup;
3245 if (label->single_line_mode)
3246 required_rect.height = get_single_line_height (GTK_WIDGET (label), label->layout);
3248 if (label->have_transform)
3250 PangoContext *context = pango_layout_get_context (label->layout);
3251 const PangoMatrix *matrix = pango_context_get_matrix (context);
3252 pango_matrix_transform_rectangle (matrix, &required_rect);
3255 required_rect.width = PANGO_PIXELS_CEIL (required_rect.width);
3256 required_rect.height = PANGO_PIXELS_CEIL (required_rect.height);
3258 if ((label->wrap || label->ellipsize ||
3259 priv->width_chars > 0 || priv->max_width_chars > 0) &&
3260 aux_info && aux_info->width > 0)
3261 required_rect.width = aux_info->width;
3263 minimum_size->width = required_rect.width + label->misc.xpad * 2;
3264 minimum_size->height = required_rect.height + label->misc.ypad * 2;
3269 PangoLayout *natural_layout = pango_layout_copy (label->layout);
3271 pango_layout_set_width (natural_layout, -1);
3272 pango_layout_set_ellipsize (natural_layout, PANGO_ELLIPSIZE_NONE);
3274 pango_layout_get_extents (natural_layout, NULL, &required_rect);
3275 required_rect.x = required_rect.y = 0;
3277 if (label->single_line_mode)
3278 required_rect.height = get_single_line_height (GTK_WIDGET (label), label->layout);
3280 if (label->have_transform)
3282 PangoContext *context = pango_layout_get_context (natural_layout);
3283 const PangoMatrix *matrix = pango_context_get_matrix (context);
3284 pango_matrix_transform_rectangle (matrix, &required_rect);
3287 required_rect.width = PANGO_PIXELS_CEIL (required_rect.width);
3288 required_rect.height = PANGO_PIXELS_CEIL (required_rect.height);
3290 natural_size->width = required_rect.width + label->misc.xpad * 2;
3291 natural_size->height = required_rect.height + label->misc.ypad * 2;
3293 g_object_unref (natural_layout);
3298 get_size_for_allocation (GtkLabel *label,
3303 PangoLayout *layout;
3305 gtk_label_ensure_layout (label);
3306 layout = pango_layout_copy (label->layout);
3307 pango_layout_set_width (layout, PANGO_SCALE * allocation);
3310 pango_layout_get_pixel_size (layout, NULL, minimum_size);
3314 // pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_NONE);
3315 pango_layout_get_pixel_size (layout, NULL, natural_size);
3318 g_object_unref (layout);
3322 gtk_label_get_width_for_height (GtkExtendedLayout *layout,
3324 gint *minimum_width,
3325 gint *natural_width)
3327 GtkLabel *label = GTK_LABEL (layout);
3328 gdouble angle = gtk_label_get_angle (label);
3330 if (90 == angle || 270 == angle)
3331 get_size_for_allocation (label, height, minimum_width, natural_width);
3334 GtkRequisition minimum_size, natural_size;
3336 gtk_extended_layout_get_desired_size (layout,
3337 minimum_width ? &minimum_size : NULL,
3338 natural_width ? &natural_size : NULL);
3341 *minimum_width = minimum_size.width;
3343 *natural_width = natural_size.width;
3348 gtk_label_get_height_for_width (GtkExtendedLayout *layout,
3350 gint *minimum_height,
3351 gint *natural_height)
3353 GtkLabel *label = GTK_LABEL (layout);
3354 gdouble angle = gtk_label_get_angle (label);
3356 if (0 == angle || 180 == angle)
3357 get_size_for_allocation (label, width, minimum_height, natural_height);
3360 GtkRequisition minimum_size, natural_size;
3362 gtk_extended_layout_get_desired_size (layout,
3363 minimum_height ? &minimum_size : NULL,
3364 natural_height ? &natural_size : NULL);
3367 *minimum_height = minimum_size.height;
3369 *natural_height = natural_size.height;
3374 gtk_label_size_allocate (GtkWidget *widget,
3375 GtkAllocation *allocation)
3379 label = GTK_LABEL (widget);
3381 GTK_WIDGET_CLASS (gtk_label_parent_class)->size_allocate (widget, allocation);
3383 if (label->ellipsize)
3388 PangoRectangle logical;
3390 width = (allocation->width - label->misc.xpad * 2) * PANGO_SCALE;
3392 pango_layout_set_width (label->layout, -1);
3393 pango_layout_get_extents (label->layout, NULL, &logical);
3395 if (logical.width > width)
3396 pango_layout_set_width (label->layout, width);
3400 if (label->select_info && label->select_info->window)
3402 gdk_window_move_resize (label->select_info->window,
3406 allocation->height);
3411 gtk_label_update_cursor (GtkLabel *label)
3413 if (!label->select_info)
3416 if (GTK_WIDGET_REALIZED (label))
3418 GdkDisplay *display;
3421 if (gtk_widget_is_sensitive (GTK_WIDGET (label)))
3423 display = gtk_widget_get_display (GTK_WIDGET (label));
3425 if (label->select_info->active_link)
3426 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
3427 else if (label->select_info->selectable)
3428 cursor = gdk_cursor_new_for_display (display, GDK_XTERM);
3435 gdk_window_set_cursor (label->select_info->window, cursor);
3438 gdk_cursor_unref (cursor);
3443 gtk_label_state_changed (GtkWidget *widget,
3444 GtkStateType prev_state)
3446 GtkLabel *label = GTK_LABEL (widget);
3448 if (label->select_info)
3450 gtk_label_select_region (label, 0, 0);
3451 gtk_label_update_cursor (label);
3454 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed)
3455 GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed (widget, prev_state);
3459 gtk_label_style_set (GtkWidget *widget,
3460 GtkStyle *previous_style)
3462 GtkLabel *label = GTK_LABEL (widget);
3464 /* We have to clear the layout, fonts etc. may have changed */
3465 gtk_label_clear_layout (label);
3466 gtk_label_invalidate_wrap_width (label);
3470 gtk_label_direction_changed (GtkWidget *widget,
3471 GtkTextDirection previous_dir)
3473 GtkLabel *label = GTK_LABEL (widget);
3476 pango_layout_context_changed (label->layout);
3478 GTK_WIDGET_CLASS (gtk_label_parent_class)->direction_changed (widget, previous_dir);
3482 get_layout_location (GtkLabel *label,
3488 GtkLabelPrivate *priv;
3490 gint req_width, x, y;
3491 PangoRectangle logical;
3493 misc = GTK_MISC (label);
3494 widget = GTK_WIDGET (label);
3495 priv = GTK_LABEL_GET_PRIVATE (label);
3497 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
3498 xalign = misc->xalign;
3500 xalign = 1.0 - misc->xalign;
3502 pango_layout_get_pixel_extents (label->layout, NULL, &logical);
3504 if (label->ellipsize || priv->width_chars > 0)
3508 width = pango_layout_get_width (label->layout);
3510 req_width = logical.width;
3512 req_width = MIN(PANGO_PIXELS (width), req_width);
3513 req_width += 2 * misc->xpad;
3516 req_width = widget->requisition.width;
3518 x = floor (widget->allocation.x + (gint)misc->xpad +
3519 xalign * (widget->allocation.width - req_width));
3521 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
3522 x = MAX (x, widget->allocation.x + misc->xpad);
3524 x = MIN (x, widget->allocation.x + widget->allocation.width - misc->xpad);
3527 /* bgo#315462 - For single-line labels, *do* align the requisition with
3528 * respect to the allocation, even if we are under-allocated. For multi-line
3529 * labels, always show the top of the text when they are under-allocated. The
3530 * rationale is this:
3532 * - Single-line labels appear in GtkButtons, and it is very easy to get them
3533 * to be smaller than their requisition. The button may clip the label, but
3534 * the label will still be able to show most of itself and the focus
3535 * rectangle. Also, it is fairly easy to read a single line of clipped text.
3537 * - Multi-line labels should not be clipped to showing "something in the
3538 * middle". You want to read the first line, at least, to get some context.
3540 if (pango_layout_get_line_count (label->layout) == 1)
3541 y = floor (widget->allocation.y + (gint)misc->ypad
3542 + (widget->allocation.height - widget->requisition.height) * misc->yalign);
3544 y = floor (widget->allocation.y + (gint)misc->ypad
3545 + MAX (((widget->allocation.height - widget->requisition.height) * misc->yalign),
3556 draw_insertion_cursor (GtkLabel *label,
3557 GdkRectangle *cursor_location,
3558 gboolean is_primary,
3559 PangoDirection direction,
3560 gboolean draw_arrow)
3562 GtkWidget *widget = GTK_WIDGET (label);
3563 GtkTextDirection text_dir;
3565 if (direction == PANGO_DIRECTION_LTR)
3566 text_dir = GTK_TEXT_DIR_LTR;
3568 text_dir = GTK_TEXT_DIR_RTL;
3570 gtk_draw_insertion_cursor (widget, widget->window, &(widget->allocation),
3572 is_primary, text_dir, draw_arrow);
3575 static PangoDirection
3576 get_cursor_direction (GtkLabel *label)
3580 g_assert (label->select_info);
3582 gtk_label_ensure_layout (label);
3584 for (l = pango_layout_get_lines_readonly (label->layout); l; l = l->next)
3586 PangoLayoutLine *line = l->data;
3588 /* If label->select_info->selection_end is at the very end of
3589 * the line, we don't know if the cursor is on this line or
3590 * the next without looking ahead at the next line. (End
3591 * of paragraph is different from line break.) But it's
3592 * definitely in this paragraph, which is good enough
3593 * to figure out the resolved direction.
3595 if (line->start_index + line->length >= label->select_info->selection_end)
3596 return line->resolved_dir;
3599 return PANGO_DIRECTION_LTR;
3603 gtk_label_draw_cursor (GtkLabel *label, gint xoffset, gint yoffset)
3607 if (label->select_info == NULL)
3610 widget = GTK_WIDGET (label);
3612 if (gtk_widget_is_drawable (widget))
3614 PangoDirection keymap_direction;
3615 PangoDirection cursor_direction;
3616 PangoRectangle strong_pos, weak_pos;
3617 gboolean split_cursor;
3618 PangoRectangle *cursor1 = NULL;
3619 PangoRectangle *cursor2 = NULL;
3620 GdkRectangle cursor_location;
3621 PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
3622 PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
3624 keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gtk_widget_get_display (widget)));
3625 cursor_direction = get_cursor_direction (label);
3627 gtk_label_ensure_layout (label);
3629 pango_layout_get_cursor_pos (label->layout, label->select_info->selection_end,
3630 &strong_pos, &weak_pos);
3632 g_object_get (gtk_widget_get_settings (widget),
3633 "gtk-split-cursor", &split_cursor,
3636 dir1 = cursor_direction;
3640 cursor1 = &strong_pos;
3642 if (strong_pos.x != weak_pos.x ||
3643 strong_pos.y != weak_pos.y)
3645 dir2 = (cursor_direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
3646 cursor2 = &weak_pos;
3651 if (keymap_direction == cursor_direction)
3652 cursor1 = &strong_pos;
3654 cursor1 = &weak_pos;
3657 cursor_location.x = xoffset + PANGO_PIXELS (cursor1->x);
3658 cursor_location.y = yoffset + PANGO_PIXELS (cursor1->y);
3659 cursor_location.width = 0;
3660 cursor_location.height = PANGO_PIXELS (cursor1->height);
3662 draw_insertion_cursor (label,
3663 &cursor_location, TRUE, dir1,
3664 dir2 != PANGO_DIRECTION_NEUTRAL);
3666 if (dir2 != PANGO_DIRECTION_NEUTRAL)
3668 cursor_location.x = xoffset + PANGO_PIXELS (cursor2->x);
3669 cursor_location.y = yoffset + PANGO_PIXELS (cursor2->y);
3670 cursor_location.width = 0;
3671 cursor_location.height = PANGO_PIXELS (cursor2->height);
3673 draw_insertion_cursor (label,
3674 &cursor_location, FALSE, dir2,
3680 static GtkLabelLink *
3681 gtk_label_get_focus_link (GtkLabel *label)
3683 GtkLabelSelectionInfo *info = label->select_info;
3689 if (info->selection_anchor != info->selection_end)
3692 for (l = info->links; l; l = l->next)
3694 GtkLabelLink *link = l->data;
3695 if (link->start <= info->selection_anchor &&
3696 info->selection_anchor <= link->end)
3704 gtk_label_expose (GtkWidget *widget,
3705 GdkEventExpose *event)
3707 GtkLabel *label = GTK_LABEL (widget);
3708 GtkLabelSelectionInfo *info = label->select_info;
3711 gtk_label_ensure_layout (label);
3713 if (gtk_widget_get_visible (widget) && gtk_widget_get_mapped (widget) &&
3714 label->text && (*label->text != '\0'))
3716 get_layout_location (label, &x, &y);
3718 gtk_paint_layout (widget->style,
3720 GTK_WIDGET_STATE (widget),
3729 (info->selection_anchor != info->selection_end))
3735 range[0] = info->selection_anchor;
3736 range[1] = info->selection_end;
3738 if (range[0] > range[1])
3740 gint tmp = range[0];
3741 range[0] = range[1];
3745 clip = gdk_pango_layout_get_clip_region (label->layout,
3749 gdk_region_intersect (clip, event->region);
3751 /* FIXME should use gtk_paint, but it can't use a clip
3755 gdk_gc_set_clip_region (widget->style->black_gc, clip);
3758 state = GTK_STATE_SELECTED;
3759 if (!gtk_widget_has_focus (widget))
3760 state = GTK_STATE_ACTIVE;
3762 gdk_draw_layout_with_colors (widget->window,
3763 widget->style->black_gc,
3766 &widget->style->text[state],
3767 &widget->style->base[state]);
3769 gdk_gc_set_clip_region (widget->style->black_gc, NULL);
3770 gdk_region_destroy (clip);
3774 GtkLabelLink *focus_link;
3775 GtkLabelLink *active_link;
3779 GdkColor *text_color;
3780 GdkColor *base_color;
3781 GdkColor *link_color;
3782 GdkColor *visited_link_color;
3784 if (info->selectable && gtk_widget_has_focus (widget))
3785 gtk_label_draw_cursor (label, x, y);
3787 focus_link = gtk_label_get_focus_link (label);
3788 active_link = info->active_link;
3792 range[0] = active_link->start;
3793 range[1] = active_link->end;
3795 clip = gdk_pango_layout_get_clip_region (label->layout,
3799 gdk_gc_set_clip_region (widget->style->black_gc, clip);
3801 gtk_label_get_link_colors (widget, &link_color, &visited_link_color);
3802 if (active_link->visited)
3803 text_color = visited_link_color;
3805 text_color = link_color;
3806 if (info->link_clicked)
3807 base_color = &widget->style->base[GTK_STATE_ACTIVE];
3809 base_color = &widget->style->base[GTK_STATE_PRELIGHT];
3810 gdk_draw_layout_with_colors (widget->window,
3811 widget->style->black_gc,
3816 gdk_color_free (link_color);
3817 gdk_color_free (visited_link_color);
3819 gdk_gc_set_clip_region (widget->style->black_gc, NULL);
3820 gdk_region_destroy (clip);
3823 if (focus_link && gtk_widget_has_focus (widget))
3825 range[0] = focus_link->start;
3826 range[1] = focus_link->end;
3828 clip = gdk_pango_layout_get_clip_region (label->layout,
3832 gdk_region_get_clipbox (clip, &rect);
3834 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
3835 &event->area, widget, "label",
3836 rect.x, rect.y, rect.width, rect.height);
3838 gdk_region_destroy (clip);
3847 separate_uline_pattern (const gchar *str,
3852 gboolean underscore;
3855 gchar *pattern_dest;
3857 *accel_key = GDK_VoidSymbol;
3858 *new_str = g_new (gchar, strlen (str) + 1);
3859 *pattern = g_new (gchar, g_utf8_strlen (str, -1) + 1);
3865 pattern_dest = *pattern;
3870 const gchar *next_src;
3872 c = g_utf8_get_char (src);
3873 if (c == (gunichar)-1)
3875 g_warning ("Invalid input string");
3881 next_src = g_utf8_next_char (src);
3886 *pattern_dest++ = ' ';
3889 *pattern_dest++ = '_';
3890 if (*accel_key == GDK_VoidSymbol)
3891 *accel_key = gdk_keyval_to_lower (gdk_unicode_to_keyval (c));
3894 while (src < next_src)
3908 while (src < next_src)
3911 *pattern_dest++ = ' ';
3923 gtk_label_set_uline_text_internal (GtkLabel *label,
3926 guint accel_key = GDK_VoidSymbol;
3930 g_return_if_fail (GTK_IS_LABEL (label));
3931 g_return_if_fail (str != NULL);
3933 /* Split text into the base text and a separate pattern
3936 if (!separate_uline_pattern (str, &accel_key, &new_str, &pattern))
3939 gtk_label_set_text_internal (label, new_str);
3940 gtk_label_set_pattern_internal (label, pattern, TRUE);
3941 label->mnemonic_keyval = accel_key;
3947 gtk_label_parse_uline (GtkLabel *label,
3952 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_VoidSymbol);
3953 g_return_val_if_fail (str != NULL, GDK_VoidSymbol);
3955 g_object_freeze_notify (G_OBJECT (label));
3957 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
3958 gtk_label_set_use_markup_internal (label, FALSE);
3959 gtk_label_set_use_underline_internal (label, TRUE);
3961 gtk_label_recalculate (label);
3963 keyval = label->mnemonic_keyval;
3964 if (keyval != GDK_VoidSymbol)
3966 label->mnemonic_keyval = GDK_VoidSymbol;
3967 gtk_label_setup_mnemonic (label, keyval);
3968 g_object_notify (G_OBJECT (label), "mnemonic-keyval");
3971 g_object_thaw_notify (G_OBJECT (label));
3977 * gtk_label_set_text_with_mnemonic:
3978 * @label: a #GtkLabel
3981 * Sets the label's text from the string @str.
3982 * If characters in @str are preceded by an underscore, they are underlined
3983 * indicating that they represent a keyboard accelerator called a mnemonic.
3984 * The mnemonic key can be used to activate another widget, chosen
3985 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
3988 gtk_label_set_text_with_mnemonic (GtkLabel *label,
3991 g_return_if_fail (GTK_IS_LABEL (label));
3992 g_return_if_fail (str != NULL);
3994 g_object_freeze_notify (G_OBJECT (label));
3996 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
3997 gtk_label_set_use_markup_internal (label, FALSE);
3998 gtk_label_set_use_underline_internal (label, TRUE);
4000 gtk_label_recalculate (label);
4002 g_object_thaw_notify (G_OBJECT (label));
4006 gtk_label_realize (GtkWidget *widget)
4010 label = GTK_LABEL (widget);
4012 GTK_WIDGET_CLASS (gtk_label_parent_class)->realize (widget);
4014 if (label->select_info)
4015 gtk_label_create_window (label);
4019 gtk_label_unrealize (GtkWidget *widget)
4023 label = GTK_LABEL (widget);
4025 if (label->select_info)
4026 gtk_label_destroy_window (label);
4028 GTK_WIDGET_CLASS (gtk_label_parent_class)->unrealize (widget);
4032 gtk_label_map (GtkWidget *widget)
4036 label = GTK_LABEL (widget);
4038 GTK_WIDGET_CLASS (gtk_label_parent_class)->map (widget);
4040 if (label->select_info)
4041 gdk_window_show (label->select_info->window);
4045 gtk_label_unmap (GtkWidget *widget)
4049 label = GTK_LABEL (widget);
4051 if (label->select_info)
4052 gdk_window_hide (label->select_info->window);
4054 GTK_WIDGET_CLASS (gtk_label_parent_class)->unmap (widget);
4058 window_to_layout_coords (GtkLabel *label,
4065 widget = GTK_WIDGET (label);
4067 /* get layout location in widget->window coords */
4068 get_layout_location (label, &lx, &ly);
4072 *x += widget->allocation.x; /* go to widget->window */
4073 *x -= lx; /* go to layout */
4078 *y += widget->allocation.y; /* go to widget->window */
4079 *y -= ly; /* go to layout */
4085 layout_to_window_coords (GtkLabel *label,
4092 widget = GTK_WIDGET (label);
4094 /* get layout location in widget->window coords */
4095 get_layout_location (label, &lx, &ly);
4099 *x += lx; /* go to widget->window */
4100 *x -= widget->allocation.x; /* go to selection window */
4105 *y += ly; /* go to widget->window */
4106 *y -= widget->allocation.y; /* go to selection window */
4112 get_layout_index (GtkLabel *label,
4118 const gchar *cluster;
4119 const gchar *cluster_end;
4124 gtk_label_ensure_layout (label);
4126 window_to_layout_coords (label, &x, &y);
4131 inside = pango_layout_xy_to_index (label->layout,
4135 cluster = label->text + *index;
4136 cluster_end = cluster;
4139 cluster_end = g_utf8_next_char (cluster_end);
4143 *index += (cluster_end - cluster);
4149 gtk_label_select_word (GtkLabel *label)
4153 gint start_index = gtk_label_move_backward_word (label, label->select_info->selection_end);
4154 gint end_index = gtk_label_move_forward_word (label, label->select_info->selection_end);
4156 min = MIN (label->select_info->selection_anchor,
4157 label->select_info->selection_end);
4158 max = MAX (label->select_info->selection_anchor,
4159 label->select_info->selection_end);
4161 min = MIN (min, start_index);
4162 max = MAX (max, end_index);
4164 gtk_label_select_region_index (label, min, max);
4168 gtk_label_grab_focus (GtkWidget *widget)
4171 gboolean select_on_focus;
4174 label = GTK_LABEL (widget);
4176 if (label->select_info == NULL)
4179 GTK_WIDGET_CLASS (gtk_label_parent_class)->grab_focus (widget);
4181 if (label->select_info->selectable)
4183 g_object_get (gtk_widget_get_settings (widget),
4184 "gtk-label-select-on-focus",
4188 if (select_on_focus && !label->in_click)
4189 gtk_label_select_region (label, 0, -1);
4193 if (label->select_info->links && !label->in_click)
4195 link = label->select_info->links->data;
4196 label->select_info->selection_anchor = link->start;
4197 label->select_info->selection_end = link->start;
4203 gtk_label_focus (GtkWidget *widget,
4204 GtkDirectionType direction)
4206 GtkLabel *label = GTK_LABEL (widget);
4207 GtkLabelSelectionInfo *info = label->select_info;
4208 GtkLabelLink *focus_link;
4211 if (!gtk_widget_is_focus (widget))
4213 gtk_widget_grab_focus (widget);
4216 focus_link = gtk_label_get_focus_link (label);
4217 if (focus_link && direction == GTK_DIR_TAB_BACKWARD)
4219 l = g_list_last (info->links);
4220 focus_link = l->data;
4221 info->selection_anchor = focus_link->start;
4222 info->selection_end = focus_link->start;
4232 if (info->selectable)
4236 if (info->selection_anchor != info->selection_end)
4239 index = info->selection_anchor;
4241 if (direction == GTK_DIR_TAB_FORWARD)
4242 for (l = info->links; l; l = l->next)
4244 GtkLabelLink *link = l->data;
4246 if (link->start > index)
4248 gtk_label_select_region_index (label, link->start, link->start);
4252 else if (direction == GTK_DIR_TAB_BACKWARD)
4253 for (l = g_list_last (info->links); l; l = l->prev)
4255 GtkLabelLink *link = l->data;
4257 if (link->end < index)
4259 gtk_label_select_region_index (label, link->start, link->start);
4268 focus_link = gtk_label_get_focus_link (label);
4271 case GTK_DIR_TAB_FORWARD:
4274 l = g_list_find (info->links, focus_link);
4281 case GTK_DIR_TAB_BACKWARD:
4284 l = g_list_find (info->links, focus_link);
4288 l = g_list_last (info->links);
4297 focus_link = l->data;
4298 info->selection_anchor = focus_link->start;
4299 info->selection_end = focus_link->start;
4300 gtk_widget_queue_draw (widget);
4312 gtk_label_button_press (GtkWidget *widget,
4313 GdkEventButton *event)
4315 GtkLabel *label = GTK_LABEL (widget);
4316 GtkLabelSelectionInfo *info = label->select_info;
4323 if (info->active_link)
4325 if (event->button == 1)
4327 info->link_clicked = 1;
4328 gtk_widget_queue_draw (widget);
4330 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4332 info->link_clicked = 1;
4333 gtk_label_do_popup (label, event);
4338 if (!info->selectable)
4341 info->in_drag = FALSE;
4342 info->select_words = FALSE;
4344 if (event->button == 1)
4346 if (!gtk_widget_has_focus (widget))
4348 label->in_click = TRUE;
4349 gtk_widget_grab_focus (widget);
4350 label->in_click = FALSE;
4353 if (event->type == GDK_3BUTTON_PRESS)
4355 gtk_label_select_region_index (label, 0, strlen (label->text));
4359 if (event->type == GDK_2BUTTON_PRESS)
4361 info->select_words = TRUE;
4362 gtk_label_select_word (label);
4366 get_layout_index (label, event->x, event->y, &index);
4368 min = MIN (info->selection_anchor, info->selection_end);
4369 max = MAX (info->selection_anchor, info->selection_end);
4371 if ((info->selection_anchor != info->selection_end) &&
4372 (event->state & GDK_SHIFT_MASK))
4374 /* extend (same as motion) */
4375 min = MIN (min, index);
4376 max = MAX (max, index);
4378 /* ensure the anchor is opposite index */
4386 gtk_label_select_region_index (label, min, max);
4390 if (event->type == GDK_3BUTTON_PRESS)
4391 gtk_label_select_region_index (label, 0, strlen (label->text));
4392 else if (event->type == GDK_2BUTTON_PRESS)
4393 gtk_label_select_word (label);
4394 else if (min < max && min <= index && index <= max)
4396 info->in_drag = TRUE;
4397 info->drag_start_x = event->x;
4398 info->drag_start_y = event->y;
4401 /* start a replacement */
4402 gtk_label_select_region_index (label, index, index);
4407 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4409 gtk_label_do_popup (label, event);
4417 gtk_label_button_release (GtkWidget *widget,
4418 GdkEventButton *event)
4421 GtkLabel *label = GTK_LABEL (widget);
4422 GtkLabelSelectionInfo *info = label->select_info;
4432 get_layout_index (label, event->x, event->y, &index);
4433 gtk_label_select_region_index (label, index, index);
4438 if (event->button != 1)
4441 if (info->active_link &&
4442 info->selection_anchor == info->selection_end &&
4445 emit_activate_link (label, info->active_link);
4446 info->link_clicked = 0;
4451 /* The goal here is to return TRUE iff we ate the
4452 * button press to start selecting.
4458 connect_mnemonics_visible_notify (GtkLabel *label)
4460 GtkLabelPrivate *priv = GTK_LABEL_GET_PRIVATE (label);
4461 GtkWidget *toplevel;
4464 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
4466 if (!GTK_IS_WINDOW (toplevel))
4469 /* always set up this widgets initial value */
4470 priv->mnemonics_visible =
4471 gtk_window_get_mnemonics_visible (GTK_WINDOW (toplevel));
4474 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (toplevel),
4475 "gtk-label-mnemonics-visible-connected"));
4479 g_signal_connect (toplevel,
4480 "notify::mnemonics-visible",
4481 G_CALLBACK (label_mnemonics_visible_changed),
4483 g_object_set_data (G_OBJECT (toplevel),
4484 "gtk-label-mnemonics-visible-connected",
4485 GINT_TO_POINTER (1));
4490 drag_begin_cb (GtkWidget *widget,
4491 GdkDragContext *context,
4495 GdkPixmap *pixmap = NULL;
4497 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
4499 label = GTK_LABEL (widget);
4501 if ((label->select_info->selection_anchor !=
4502 label->select_info->selection_end) &&
4508 start = MIN (label->select_info->selection_anchor,
4509 label->select_info->selection_end);
4510 end = MAX (label->select_info->selection_anchor,
4511 label->select_info->selection_end);
4513 len = strlen (label->text);
4521 pixmap = _gtk_text_util_create_drag_icon (widget,
4522 label->text + start,
4527 gtk_drag_set_icon_pixmap (context,
4528 gdk_drawable_get_colormap (pixmap),
4533 gtk_drag_set_icon_default (context);
4536 g_object_unref (pixmap);
4540 gtk_label_motion (GtkWidget *widget,
4541 GdkEventMotion *event)
4543 GtkLabel *label = GTK_LABEL (widget);
4544 GtkLabelSelectionInfo *info = label->select_info;
4551 if (info->links && !info->in_drag)
4555 gboolean found = FALSE;
4557 if (info->selection_anchor == info->selection_end)
4559 gdk_window_get_pointer (event->window, &x, &y, NULL);
4560 if (get_layout_index (label, x, y, &index))
4562 for (l = info->links; l != NULL; l = l->next)
4565 if (index >= link->start && index <= link->end)
4576 if (info->active_link != link)
4578 info->link_clicked = 0;
4579 info->active_link = link;
4580 gtk_label_update_cursor (label);
4581 gtk_widget_queue_draw (widget);
4586 if (info->active_link != NULL)
4588 info->link_clicked = 0;
4589 info->active_link = NULL;
4590 gtk_label_update_cursor (label);
4591 gtk_widget_queue_draw (widget);
4596 if (!info->selectable)
4599 if ((event->state & GDK_BUTTON1_MASK) == 0)
4602 gdk_window_get_pointer (info->window, &x, &y, NULL);
4606 if (gtk_drag_check_threshold (widget,
4609 event->x, event->y))
4611 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
4613 gtk_target_list_add_text_targets (target_list, 0);
4615 g_signal_connect (widget, "drag-begin",
4616 G_CALLBACK (drag_begin_cb), NULL);
4617 gtk_drag_begin (widget, target_list,
4619 1, (GdkEvent *)event);
4621 info->in_drag = FALSE;
4623 gtk_target_list_unref (target_list);
4628 get_layout_index (label, x, y, &index);
4630 if (info->select_words)
4633 gint old_min, old_max;
4636 min = gtk_label_move_backward_word (label, index);
4637 max = gtk_label_move_forward_word (label, index);
4639 anchor = info->selection_anchor;
4640 end = info->selection_end;
4642 old_min = MIN (anchor, end);
4643 old_max = MAX (anchor, end);
4650 else if (old_max < max)
4655 else if (anchor == old_min)
4666 gtk_label_select_region_index (label, anchor, end);
4669 gtk_label_select_region_index (label, info->selection_anchor, index);
4676 gtk_label_leave_notify (GtkWidget *widget,
4677 GdkEventCrossing *event)
4679 GtkLabel *label = GTK_LABEL (widget);
4681 if (label->select_info)
4683 label->select_info->active_link = NULL;
4684 gtk_label_update_cursor (label);
4685 gtk_widget_queue_draw (widget);
4688 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event)
4689 return GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event (widget, event);
4695 gtk_label_create_window (GtkLabel *label)
4698 GdkWindowAttr attributes;
4699 gint attributes_mask;
4701 g_assert (label->select_info);
4702 g_assert (GTK_WIDGET_REALIZED (label));
4704 if (label->select_info->window)
4707 widget = GTK_WIDGET (label);
4709 attributes.x = widget->allocation.x;
4710 attributes.y = widget->allocation.y;
4711 attributes.width = widget->allocation.width;
4712 attributes.height = widget->allocation.height;
4713 attributes.window_type = GDK_WINDOW_CHILD;
4714 attributes.wclass = GDK_INPUT_ONLY;
4715 attributes.override_redirect = TRUE;
4716 attributes.event_mask = gtk_widget_get_events (widget) |
4717 GDK_BUTTON_PRESS_MASK |
4718 GDK_BUTTON_RELEASE_MASK |
4719 GDK_LEAVE_NOTIFY_MASK |
4720 GDK_BUTTON_MOTION_MASK |
4721 GDK_POINTER_MOTION_MASK |
4722 GDK_POINTER_MOTION_HINT_MASK;
4723 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
4724 if (gtk_widget_is_sensitive (widget))
4726 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
4728 attributes_mask |= GDK_WA_CURSOR;
4732 label->select_info->window = gdk_window_new (widget->window,
4733 &attributes, attributes_mask);
4734 gdk_window_set_user_data (label->select_info->window, widget);
4736 if (attributes_mask & GDK_WA_CURSOR)
4737 gdk_cursor_unref (attributes.cursor);
4741 gtk_label_destroy_window (GtkLabel *label)
4743 g_assert (label->select_info);
4745 if (label->select_info->window == NULL)
4748 gdk_window_set_user_data (label->select_info->window, NULL);
4749 gdk_window_destroy (label->select_info->window);
4750 label->select_info->window = NULL;
4754 gtk_label_ensure_select_info (GtkLabel *label)
4756 if (label->select_info == NULL)
4758 label->select_info = g_new0 (GtkLabelSelectionInfo, 1);
4760 gtk_widget_set_can_focus (GTK_WIDGET (label), TRUE);
4762 if (GTK_WIDGET_REALIZED (label))
4763 gtk_label_create_window (label);
4765 if (gtk_widget_get_mapped (GTK_WIDGET (label)))
4766 gdk_window_show (label->select_info->window);
4771 gtk_label_clear_select_info (GtkLabel *label)
4773 if (label->select_info == NULL)
4776 if (!label->select_info->selectable && !label->select_info->links)
4778 gtk_label_destroy_window (label);
4780 g_free (label->select_info);
4781 label->select_info = NULL;
4783 gtk_widget_set_can_focus (GTK_WIDGET (label), FALSE);
4788 * gtk_label_set_selectable:
4789 * @label: a #GtkLabel
4790 * @setting: %TRUE to allow selecting text in the label
4792 * Selectable labels allow the user to select text from the label, for
4796 gtk_label_set_selectable (GtkLabel *label,
4799 gboolean old_setting;
4801 g_return_if_fail (GTK_IS_LABEL (label));
4803 setting = setting != FALSE;
4804 old_setting = label->select_info && label->select_info->selectable;
4808 gtk_label_ensure_select_info (label);
4809 label->select_info->selectable = TRUE;
4810 gtk_label_update_cursor (label);
4816 /* unselect, to give up the selection */
4817 gtk_label_select_region (label, 0, 0);
4819 label->select_info->selectable = FALSE;
4820 gtk_label_clear_select_info (label);
4821 gtk_label_update_cursor (label);
4824 if (setting != old_setting)
4826 g_object_freeze_notify (G_OBJECT (label));
4827 g_object_notify (G_OBJECT (label), "selectable");
4828 g_object_notify (G_OBJECT (label), "cursor-position");
4829 g_object_notify (G_OBJECT (label), "selection-bound");
4830 g_object_thaw_notify (G_OBJECT (label));
4831 gtk_widget_queue_draw (GTK_WIDGET (label));
4836 * gtk_label_get_selectable:
4837 * @label: a #GtkLabel
4839 * Gets the value set by gtk_label_set_selectable().
4841 * Return value: %TRUE if the user can copy text from the label
4844 gtk_label_get_selectable (GtkLabel *label)
4846 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
4848 return label->select_info && label->select_info->selectable;
4852 free_angle (gpointer angle)
4854 g_slice_free (gdouble, angle);
4858 * gtk_label_set_angle:
4859 * @label: a #GtkLabel
4860 * @angle: the angle that the baseline of the label makes with
4861 * the horizontal, in degrees, measured counterclockwise
4863 * Sets the angle of rotation for the label. An angle of 90 reads from
4864 * from bottom to top, an angle of 270, from top to bottom. The angle
4865 * setting for the label is ignored if the label is selectable,
4866 * wrapped, or ellipsized.
4871 gtk_label_set_angle (GtkLabel *label,
4874 gdouble *label_angle;
4876 g_return_if_fail (GTK_IS_LABEL (label));
4878 label_angle = (gdouble *)g_object_get_qdata (G_OBJECT (label), quark_angle);
4882 label_angle = g_slice_new (gdouble);
4884 g_object_set_qdata_full (G_OBJECT (label), quark_angle,
4885 label_angle, free_angle);
4888 /* Canonicalize to [0,360]. We don't canonicalize 360 to 0, because
4889 * double property ranges are inclusive, and changing 360 to 0 would
4890 * make a property editor behave strangely.
4892 if (angle < 0 || angle > 360.0)
4893 angle = angle - 360. * floor (angle / 360.);
4895 if (*label_angle != angle)
4897 *label_angle = angle;
4899 gtk_label_clear_layout (label);
4900 gtk_widget_queue_resize (GTK_WIDGET (label));
4902 g_object_notify (G_OBJECT (label), "angle");
4907 * gtk_label_get_angle:
4908 * @label: a #GtkLabel
4910 * Gets the angle of rotation for the label. See
4911 * gtk_label_set_angle().
4913 * Return value: the angle of rotation for the label
4918 gtk_label_get_angle (GtkLabel *label)
4922 g_return_val_if_fail (GTK_IS_LABEL (label), 0.0);
4924 angle = (gdouble *)g_object_get_qdata (G_OBJECT (label), quark_angle);
4933 gtk_label_set_selection_text (GtkLabel *label,
4934 GtkSelectionData *selection_data)
4936 if ((label->select_info->selection_anchor !=
4937 label->select_info->selection_end) &&
4943 start = MIN (label->select_info->selection_anchor,
4944 label->select_info->selection_end);
4945 end = MAX (label->select_info->selection_anchor,
4946 label->select_info->selection_end);
4948 len = strlen (label->text);
4956 gtk_selection_data_set_text (selection_data,
4957 label->text + start,
4963 gtk_label_drag_data_get (GtkWidget *widget,
4964 GdkDragContext *context,
4965 GtkSelectionData *selection_data,
4969 gtk_label_set_selection_text (GTK_LABEL (widget), selection_data);
4973 get_text_callback (GtkClipboard *clipboard,
4974 GtkSelectionData *selection_data,
4976 gpointer user_data_or_owner)
4978 gtk_label_set_selection_text (GTK_LABEL (user_data_or_owner), selection_data);
4982 clear_text_callback (GtkClipboard *clipboard,
4983 gpointer user_data_or_owner)
4987 label = GTK_LABEL (user_data_or_owner);
4989 if (label->select_info)
4991 label->select_info->selection_anchor = label->select_info->selection_end;
4993 gtk_widget_queue_draw (GTK_WIDGET (label));
4998 gtk_label_select_region_index (GtkLabel *label,
5002 g_return_if_fail (GTK_IS_LABEL (label));
5004 if (label->select_info && label->select_info->selectable)
5006 GtkClipboard *clipboard;
5008 if (label->select_info->selection_anchor == anchor_index &&
5009 label->select_info->selection_end == end_index)
5012 label->select_info->selection_anchor = anchor_index;
5013 label->select_info->selection_end = end_index;
5015 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label),
5016 GDK_SELECTION_PRIMARY);
5018 if (anchor_index != end_index)
5020 GtkTargetList *list;
5021 GtkTargetEntry *targets;
5024 list = gtk_target_list_new (NULL, 0);
5025 gtk_target_list_add_text_targets (list, 0);
5026 targets = gtk_target_table_new_from_list (list, &n_targets);
5028 gtk_clipboard_set_with_owner (clipboard,
5031 clear_text_callback,
5034 gtk_target_table_free (targets, n_targets);
5035 gtk_target_list_unref (list);
5039 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (label))
5040 gtk_clipboard_clear (clipboard);
5043 gtk_widget_queue_draw (GTK_WIDGET (label));
5045 g_object_freeze_notify (G_OBJECT (label));
5046 g_object_notify (G_OBJECT (label), "cursor-position");
5047 g_object_notify (G_OBJECT (label), "selection-bound");
5048 g_object_thaw_notify (G_OBJECT (label));
5053 * gtk_label_select_region:
5054 * @label: a #GtkLabel
5055 * @start_offset: start offset (in characters not bytes)
5056 * @end_offset: end offset (in characters not bytes)
5058 * Selects a range of characters in the label, if the label is selectable.
5059 * See gtk_label_set_selectable(). If the label is not selectable,
5060 * this function has no effect. If @start_offset or
5061 * @end_offset are -1, then the end of the label will be substituted.
5064 gtk_label_select_region (GtkLabel *label,
5068 g_return_if_fail (GTK_IS_LABEL (label));
5070 if (label->text && label->select_info)
5072 if (start_offset < 0)
5073 start_offset = g_utf8_strlen (label->text, -1);
5076 end_offset = g_utf8_strlen (label->text, -1);
5078 gtk_label_select_region_index (label,
5079 g_utf8_offset_to_pointer (label->text, start_offset) - label->text,
5080 g_utf8_offset_to_pointer (label->text, end_offset) - label->text);
5085 * gtk_label_get_selection_bounds:
5086 * @label: a #GtkLabel
5087 * @start: return location for start of selection, as a character offset
5088 * @end: return location for end of selection, as a character offset
5090 * Gets the selected range of characters in the label, returning %TRUE
5091 * if there's a selection.
5093 * Return value: %TRUE if selection is non-empty
5096 gtk_label_get_selection_bounds (GtkLabel *label,
5100 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5102 if (label->select_info == NULL)
5104 /* not a selectable label */
5114 gint start_index, end_index;
5115 gint start_offset, end_offset;
5118 start_index = MIN (label->select_info->selection_anchor,
5119 label->select_info->selection_end);
5120 end_index = MAX (label->select_info->selection_anchor,
5121 label->select_info->selection_end);
5123 len = strlen (label->text);
5125 if (end_index > len)
5128 if (start_index > len)
5131 start_offset = g_utf8_strlen (label->text, start_index);
5132 end_offset = g_utf8_strlen (label->text, end_index);
5134 if (start_offset > end_offset)
5136 gint tmp = start_offset;
5137 start_offset = end_offset;
5142 *start = start_offset;
5147 return start_offset != end_offset;
5153 * gtk_label_get_layout:
5154 * @label: a #GtkLabel
5156 * Gets the #PangoLayout used to display the label.
5157 * The layout is useful to e.g. convert text positions to
5158 * pixel positions, in combination with gtk_label_get_layout_offsets().
5159 * The returned layout is owned by the label so need not be
5160 * freed by the caller.
5162 * Return value: (transfer none): the #PangoLayout for this label
5165 gtk_label_get_layout (GtkLabel *label)
5167 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
5169 gtk_label_ensure_layout (label);
5171 return label->layout;
5175 * gtk_label_get_layout_offsets:
5176 * @label: a #GtkLabel
5177 * @x: (allow-none): location to store X offset of layout, or %NULL
5178 * @y: (allow-none): location to store Y offset of layout, or %NULL
5180 * Obtains the coordinates where the label will draw the #PangoLayout
5181 * representing the text in the label; useful to convert mouse events
5182 * into coordinates inside the #PangoLayout, e.g. to take some action
5183 * if some part of the label is clicked. Of course you will need to
5184 * create a #GtkEventBox to receive the events, and pack the label
5185 * inside it, since labels are a #GTK_NO_WINDOW widget. Remember
5186 * when using the #PangoLayout functions you need to convert to
5187 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
5190 gtk_label_get_layout_offsets (GtkLabel *label,
5194 g_return_if_fail (GTK_IS_LABEL (label));
5196 gtk_label_ensure_layout (label);
5198 get_layout_location (label, x, y);
5202 * gtk_label_set_use_markup:
5203 * @label: a #GtkLabel
5204 * @setting: %TRUE if the label's text should be parsed for markup.
5206 * Sets whether the text of the label contains markup in <link
5207 * linkend="PangoMarkupFormat">Pango's text markup
5208 * language</link>. See gtk_label_set_markup().
5211 gtk_label_set_use_markup (GtkLabel *label,
5214 g_return_if_fail (GTK_IS_LABEL (label));
5216 gtk_label_set_use_markup_internal (label, setting);
5217 gtk_label_recalculate (label);
5221 * gtk_label_get_use_markup:
5222 * @label: a #GtkLabel
5224 * Returns whether the label's text is interpreted as marked up with
5225 * the <link linkend="PangoMarkupFormat">Pango text markup
5226 * language</link>. See gtk_label_set_use_markup ().
5228 * Return value: %TRUE if the label's text will be parsed for markup.
5231 gtk_label_get_use_markup (GtkLabel *label)
5233 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5235 return label->use_markup;
5239 * gtk_label_set_use_underline:
5240 * @label: a #GtkLabel
5241 * @setting: %TRUE if underlines in the text indicate mnemonics
5243 * If true, an underline in the text indicates the next character should be
5244 * used for the mnemonic accelerator key.
5247 gtk_label_set_use_underline (GtkLabel *label,
5250 g_return_if_fail (GTK_IS_LABEL (label));
5252 gtk_label_set_use_underline_internal (label, setting);
5253 gtk_label_recalculate (label);
5257 gtk_label_get_full_size (GtkLabel *label)
5259 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5260 return GTK_LABEL_GET_PRIVATE (label)->full_size;
5264 gtk_label_set_full_size (GtkLabel *label,
5267 GtkLabelPrivate *priv;
5269 g_return_if_fail (GTK_IS_LABEL (label));
5270 priv = GTK_LABEL_GET_PRIVATE (label);
5272 if (priv->full_size != setting)
5274 priv->full_size = setting;
5276 g_object_notify (G_OBJECT (label), "full-size");
5277 gtk_label_invalidate_wrap_width (label);
5278 gtk_widget_queue_resize (GTK_WIDGET (label));
5283 * gtk_label_get_use_underline:
5284 * @label: a #GtkLabel
5286 * Returns whether an embedded underline in the label indicates a
5287 * mnemonic. See gtk_label_set_use_underline().
5289 * Return value: %TRUE whether an embedded underline in the label indicates
5290 * the mnemonic accelerator keys.
5293 gtk_label_get_use_underline (GtkLabel *label)
5295 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5297 return label->use_underline;
5301 * gtk_label_set_single_line_mode:
5302 * @label: a #GtkLabel
5303 * @single_line_mode: %TRUE if the label should be in single line mode
5305 * Sets whether the label is in single line mode.
5310 gtk_label_set_single_line_mode (GtkLabel *label,
5311 gboolean single_line_mode)
5313 g_return_if_fail (GTK_IS_LABEL (label));
5315 single_line_mode = single_line_mode != FALSE;
5317 if (label->single_line_mode != single_line_mode)
5319 label->single_line_mode = single_line_mode;
5321 gtk_label_clear_layout (label);
5322 gtk_widget_queue_resize (GTK_WIDGET (label));
5324 g_object_notify (G_OBJECT (label), "single-line-mode");
5329 * gtk_label_get_single_line_mode:
5330 * @label: a #GtkLabel
5332 * Returns whether the label is in single line mode.
5334 * Return value: %TRUE when the label is in single line mode.
5339 gtk_label_get_single_line_mode (GtkLabel *label)
5341 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5343 return label->single_line_mode;
5346 /* Compute the X position for an offset that corresponds to the "more important
5347 * cursor position for that offset. We use this when trying to guess to which
5348 * end of the selection we should go to when the user hits the left or
5352 get_better_cursor (GtkLabel *label,
5357 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5358 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5359 PangoDirection cursor_direction = get_cursor_direction (label);
5360 gboolean split_cursor;
5361 PangoRectangle strong_pos, weak_pos;
5363 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5364 "gtk-split-cursor", &split_cursor,
5367 gtk_label_ensure_layout (label);
5369 pango_layout_get_cursor_pos (label->layout, index,
5370 &strong_pos, &weak_pos);
5374 *x = strong_pos.x / PANGO_SCALE;
5375 *y = strong_pos.y / PANGO_SCALE;
5379 if (keymap_direction == cursor_direction)
5381 *x = strong_pos.x / PANGO_SCALE;
5382 *y = strong_pos.y / PANGO_SCALE;
5386 *x = weak_pos.x / PANGO_SCALE;
5387 *y = weak_pos.y / PANGO_SCALE;
5394 gtk_label_move_logically (GtkLabel *label,
5398 gint offset = g_utf8_pointer_to_offset (label->text,
5399 label->text + start);
5403 PangoLogAttr *log_attrs;
5407 gtk_label_ensure_layout (label);
5409 length = g_utf8_strlen (label->text, -1);
5411 pango_layout_get_log_attrs (label->layout, &log_attrs, &n_attrs);
5413 while (count > 0 && offset < length)
5417 while (offset < length && !log_attrs[offset].is_cursor_position);
5421 while (count < 0 && offset > 0)
5425 while (offset > 0 && !log_attrs[offset].is_cursor_position);
5433 return g_utf8_offset_to_pointer (label->text, offset) - label->text;
5437 gtk_label_move_visually (GtkLabel *label,
5447 int new_index, new_trailing;
5448 gboolean split_cursor;
5451 gtk_label_ensure_layout (label);
5453 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5454 "gtk-split-cursor", &split_cursor,
5461 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5462 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5464 strong = keymap_direction == get_cursor_direction (label);
5469 pango_layout_move_cursor_visually (label->layout, strong, index, 0, 1, &new_index, &new_trailing);
5474 pango_layout_move_cursor_visually (label->layout, strong, index, 0, -1, &new_index, &new_trailing);
5478 if (new_index < 0 || new_index == G_MAXINT)
5483 while (new_trailing--)
5484 index = g_utf8_next_char (label->text + new_index) - label->text;
5491 gtk_label_move_forward_word (GtkLabel *label,
5494 gint new_pos = g_utf8_pointer_to_offset (label->text,
5495 label->text + start);
5498 length = g_utf8_strlen (label->text, -1);
5499 if (new_pos < length)
5501 PangoLogAttr *log_attrs;
5504 gtk_label_ensure_layout (label);
5506 pango_layout_get_log_attrs (label->layout, &log_attrs, &n_attrs);
5508 /* Find the next word end */
5510 while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
5516 return g_utf8_offset_to_pointer (label->text, new_pos) - label->text;
5521 gtk_label_move_backward_word (GtkLabel *label,
5524 gint new_pos = g_utf8_pointer_to_offset (label->text,
5525 label->text + start);
5529 PangoLogAttr *log_attrs;
5532 gtk_label_ensure_layout (label);
5534 pango_layout_get_log_attrs (label->layout, &log_attrs, &n_attrs);
5538 /* Find the previous word beginning */
5539 while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
5545 return g_utf8_offset_to_pointer (label->text, new_pos) - label->text;
5549 gtk_label_move_cursor (GtkLabel *label,
5550 GtkMovementStep step,
5552 gboolean extend_selection)
5557 if (label->select_info == NULL)
5560 old_pos = new_pos = label->select_info->selection_end;
5562 if (label->select_info->selection_end != label->select_info->selection_anchor &&
5565 /* If we have a current selection and aren't extending it, move to the
5566 * start/or end of the selection as appropriate
5570 case GTK_MOVEMENT_VISUAL_POSITIONS:
5573 gint anchor_x, anchor_y;
5574 gboolean end_is_left;
5576 get_better_cursor (label, label->select_info->selection_end, &end_x, &end_y);
5577 get_better_cursor (label, label->select_info->selection_anchor, &anchor_x, &anchor_y);
5579 end_is_left = (end_y < anchor_y) || (end_y == anchor_y && end_x < anchor_x);
5582 new_pos = end_is_left ? label->select_info->selection_end : label->select_info->selection_anchor;
5584 new_pos = !end_is_left ? label->select_info->selection_end : label->select_info->selection_anchor;
5587 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5588 case GTK_MOVEMENT_WORDS:
5590 new_pos = MIN (label->select_info->selection_end, label->select_info->selection_anchor);
5592 new_pos = MAX (label->select_info->selection_end, label->select_info->selection_anchor);
5594 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5595 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5596 case GTK_MOVEMENT_BUFFER_ENDS:
5597 /* FIXME: Can do better here */
5598 new_pos = count < 0 ? 0 : strlen (label->text);
5600 case GTK_MOVEMENT_DISPLAY_LINES:
5601 case GTK_MOVEMENT_PARAGRAPHS:
5602 case GTK_MOVEMENT_PAGES:
5603 case GTK_MOVEMENT_HORIZONTAL_PAGES:
5611 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5612 new_pos = gtk_label_move_logically (label, new_pos, count);
5614 case GTK_MOVEMENT_VISUAL_POSITIONS:
5615 new_pos = gtk_label_move_visually (label, new_pos, count);
5616 if (new_pos == old_pos)
5618 if (!extend_selection)
5620 if (!gtk_widget_keynav_failed (GTK_WIDGET (label),
5622 GTK_DIR_RIGHT : GTK_DIR_LEFT))
5624 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
5627 gtk_widget_child_focus (toplevel,
5629 GTK_DIR_RIGHT : GTK_DIR_LEFT);
5634 gtk_widget_error_bell (GTK_WIDGET (label));
5638 case GTK_MOVEMENT_WORDS:
5641 new_pos = gtk_label_move_forward_word (label, new_pos);
5646 new_pos = gtk_label_move_backward_word (label, new_pos);
5649 if (new_pos == old_pos)
5650 gtk_widget_error_bell (GTK_WIDGET (label));
5652 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5653 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5654 case GTK_MOVEMENT_BUFFER_ENDS:
5655 /* FIXME: Can do better here */
5656 new_pos = count < 0 ? 0 : strlen (label->text);
5657 if (new_pos == old_pos)
5658 gtk_widget_error_bell (GTK_WIDGET (label));
5660 case GTK_MOVEMENT_DISPLAY_LINES:
5661 case GTK_MOVEMENT_PARAGRAPHS:
5662 case GTK_MOVEMENT_PAGES:
5663 case GTK_MOVEMENT_HORIZONTAL_PAGES:
5668 if (extend_selection)
5669 gtk_label_select_region_index (label,
5670 label->select_info->selection_anchor,
5673 gtk_label_select_region_index (label, new_pos, new_pos);
5677 gtk_label_copy_clipboard (GtkLabel *label)
5679 if (label->text && label->select_info)
5683 GtkClipboard *clipboard;
5685 start = MIN (label->select_info->selection_anchor,
5686 label->select_info->selection_end);
5687 end = MAX (label->select_info->selection_anchor,
5688 label->select_info->selection_end);
5690 len = strlen (label->text);
5698 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
5701 gtk_clipboard_set_text (clipboard, label->text + start, end - start);
5706 link = gtk_label_get_focus_link (label);
5708 gtk_clipboard_set_text (clipboard, link->uri, -1);
5714 gtk_label_select_all (GtkLabel *label)
5716 gtk_label_select_region_index (label, 0, strlen (label->text));
5719 /* Quick hack of a popup menu
5722 activate_cb (GtkWidget *menuitem,
5725 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
5726 g_signal_emit_by_name (label, signal);
5730 append_action_signal (GtkLabel *label,
5732 const gchar *stock_id,
5733 const gchar *signal,
5736 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
5738 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
5739 g_signal_connect (menuitem, "activate",
5740 G_CALLBACK (activate_cb), label);
5742 gtk_widget_set_sensitive (menuitem, sensitive);
5744 gtk_widget_show (menuitem);
5745 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5749 popup_menu_detach (GtkWidget *attach_widget,
5752 GtkLabel *label = GTK_LABEL (attach_widget);
5754 if (label->select_info)
5755 label->select_info->popup_menu = NULL;
5759 popup_position_func (GtkMenu *menu,
5770 label = GTK_LABEL (user_data);
5771 widget = GTK_WIDGET (label);
5773 g_return_if_fail (GTK_WIDGET_REALIZED (label));
5775 screen = gtk_widget_get_screen (widget);
5776 gdk_window_get_origin (widget->window, x, y);
5778 *x += widget->allocation.x;
5779 *y += widget->allocation.y;
5781 gtk_widget_size_request (GTK_WIDGET (menu), &req);
5783 *x += widget->allocation.width / 2;
5784 *y += widget->allocation.height;
5786 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
5787 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
5791 open_link_activate_cb (GtkMenuItem *menu_item,
5796 link = gtk_label_get_current_link (label);
5799 emit_activate_link (label, link);
5803 copy_link_activate_cb (GtkMenuItem *menu_item,
5806 GtkClipboard *clipboard;
5809 uri = gtk_label_get_current_uri (label);
5812 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
5813 gtk_clipboard_set_text (clipboard, uri, -1);
5818 gtk_label_popup_menu (GtkWidget *widget)
5820 gtk_label_do_popup (GTK_LABEL (widget), NULL);
5826 gtk_label_do_popup (GtkLabel *label,
5827 GdkEventButton *event)
5829 GtkWidget *menuitem;
5832 gboolean have_selection;
5835 if (!label->select_info)
5838 if (label->select_info->popup_menu)
5839 gtk_widget_destroy (label->select_info->popup_menu);
5841 label->select_info->popup_menu = menu = gtk_menu_new ();
5843 gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (label), popup_menu_detach);
5846 label->select_info->selection_anchor != label->select_info->selection_end;
5850 if (label->select_info->link_clicked)
5851 link = label->select_info->active_link;
5856 link = gtk_label_get_focus_link (label);
5858 if (!have_selection && link)
5861 menuitem = gtk_image_menu_item_new_with_mnemonic (_("_Open Link"));
5862 gtk_widget_show (menuitem);
5863 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5865 g_signal_connect (G_OBJECT (menuitem), "activate",
5866 G_CALLBACK (open_link_activate_cb), label);
5868 image = gtk_image_new_from_stock (GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_MENU);
5869 gtk_widget_show (image);
5870 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
5872 /* Copy Link Address */
5873 menuitem = gtk_image_menu_item_new_with_mnemonic (_("Copy _Link Address"));
5874 gtk_widget_show (menuitem);
5875 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5877 g_signal_connect (G_OBJECT (menuitem), "activate",
5878 G_CALLBACK (copy_link_activate_cb), label);
5880 image = gtk_image_new_from_stock (GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
5881 gtk_widget_show (image);
5882 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
5886 append_action_signal (label, menu, GTK_STOCK_CUT, "cut-clipboard", FALSE);
5887 append_action_signal (label, menu, GTK_STOCK_COPY, "copy-clipboard", have_selection);
5888 append_action_signal (label, menu, GTK_STOCK_PASTE, "paste-clipboard", FALSE);
5890 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
5891 gtk_widget_set_sensitive (menuitem, FALSE);
5892 gtk_widget_show (menuitem);
5893 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5895 menuitem = gtk_separator_menu_item_new ();
5896 gtk_widget_show (menuitem);
5897 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5899 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
5900 g_signal_connect_swapped (menuitem, "activate",
5901 G_CALLBACK (gtk_label_select_all), label);
5902 gtk_widget_show (menuitem);
5903 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5906 g_signal_emit (label, signals[POPULATE_POPUP], 0, menu);
5909 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
5911 event->button, event->time);
5914 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
5915 popup_position_func, label,
5916 0, gtk_get_current_event_time ());
5917 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
5922 gtk_label_clear_links (GtkLabel *label)
5924 if (!label->select_info)
5927 g_list_foreach (label->select_info->links, (GFunc)link_free, NULL);
5928 g_list_free (label->select_info->links);
5929 label->select_info->links = NULL;
5930 label->select_info->active_link = NULL;
5934 gtk_label_rescan_links (GtkLabel *label)
5936 PangoLayout *layout = label->layout;
5937 PangoAttrList *attlist;
5938 PangoAttrIterator *iter;
5941 if (!label->select_info || !label->select_info->links)
5944 attlist = pango_layout_get_attributes (layout);
5946 if (attlist == NULL)
5949 iter = pango_attr_list_get_iterator (attlist);
5951 links = label->select_info->links;
5955 PangoAttribute *underline;
5956 PangoAttribute *color;
5958 underline = pango_attr_iterator_get (iter, PANGO_ATTR_UNDERLINE);
5959 color = pango_attr_iterator_get (iter, PANGO_ATTR_FOREGROUND);
5961 if (underline != NULL && color != NULL)
5964 PangoRectangle start_pos;
5965 PangoRectangle end_pos;
5968 pango_attr_iterator_range (iter, &start, &end);
5969 pango_layout_index_to_pos (layout, start, &start_pos);
5970 pango_layout_index_to_pos (layout, end, &end_pos);
5974 g_warning ("Ran out of links");
5978 links = links->next;
5979 link->start = start;
5982 } while (pango_attr_iterator_next (iter));
5984 pango_attr_iterator_destroy (iter);
5988 gtk_label_activate_link (GtkLabel *label,
5991 GtkWidget *widget = GTK_WIDGET (label);
5992 GError *error = NULL;
5994 if (!gtk_show_uri (gtk_widget_get_screen (widget),
5995 uri, gtk_get_current_event_time (), &error))
5997 g_warning ("Unable to show '%s': %s", uri, error->message);
5998 g_error_free (error);
6005 emit_activate_link (GtkLabel *label,
6010 g_signal_emit (label, signals[ACTIVATE_LINK], 0, link->uri, &handled);
6011 if (handled && label->track_links && !link->visited)
6013 link->visited = TRUE;
6014 /* FIXME: shouldn't have to redo everything here */
6015 gtk_label_recalculate (label);
6020 gtk_label_activate_current_link (GtkLabel *label)
6023 GtkWidget *widget = GTK_WIDGET (label);
6025 link = gtk_label_get_focus_link (label);
6029 emit_activate_link (label, link);
6033 GtkWidget *toplevel;
6036 toplevel = gtk_widget_get_toplevel (widget);
6037 if (GTK_IS_WINDOW (toplevel))
6039 window = GTK_WINDOW (toplevel);
6042 window->default_widget != widget &&
6043 !(widget == window->focus_widget &&
6044 (!window->default_widget || !gtk_widget_is_sensitive (window->default_widget))))
6045 gtk_window_activate_default (window);
6050 static GtkLabelLink *
6051 gtk_label_get_current_link (GtkLabel *label)
6055 if (!label->select_info)
6058 if (label->select_info->link_clicked)
6059 link = label->select_info->active_link;
6061 link = gtk_label_get_focus_link (label);
6067 * gtk_label_get_current_uri:
6068 * @label: a #GtkLabel
6070 * Returns the URI for the currently active link in the label.
6071 * The active link is the one under the mouse pointer or, in a
6072 * selectable label, the link in which the text cursor is currently
6075 * This function is intended for use in a #GtkLabel::activate-link handler
6076 * or for use in a #GtkWidget::query-tooltip handler.
6078 * Returns: the currently active URI. The string is owned by GTK+ and must
6079 * not be freed or modified.
6083 G_CONST_RETURN gchar *
6084 gtk_label_get_current_uri (GtkLabel *label)
6087 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
6089 link = gtk_label_get_current_link (label);
6098 * gtk_label_set_track_visited_links:
6099 * @label: a #GtkLabel
6100 * @track_links: %TRUE to track visited links
6102 * Sets whether the label should keep track of clicked
6103 * links (and use a different color for them).
6108 gtk_label_set_track_visited_links (GtkLabel *label,
6109 gboolean track_links)
6111 g_return_if_fail (GTK_IS_LABEL (label));
6113 track_links = track_links != FALSE;
6115 if (label->track_links != track_links)
6117 label->track_links = track_links;
6119 /* FIXME: shouldn't have to redo everything here */
6120 gtk_label_recalculate (label);
6122 g_object_notify (G_OBJECT (label), "track-visited-links");
6127 * gtk_label_get_track_visited_links:
6128 * @label: a #GtkLabel
6130 * Returns whether the label is currently keeping track
6133 * Returns: %TRUE if clicked links are remembered
6138 gtk_label_get_track_visited_links (GtkLabel *label)
6140 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
6142 return label->track_links;
6146 gtk_label_query_tooltip (GtkWidget *widget,
6149 gboolean keyboard_tip,
6150 GtkTooltip *tooltip)
6152 GtkLabel *label = GTK_LABEL (widget);
6153 GtkLabelSelectionInfo *info = label->select_info;
6157 if (info && info->links)
6161 if (info->selection_anchor == info->selection_end)
6162 index = info->selection_anchor;
6166 if (!get_layout_index (label, x, y, &index))
6172 for (l = info->links; l != NULL; l = l->next)
6174 GtkLabelLink *link = l->data;
6175 if (index >= link->start && index <= link->end)
6179 gtk_tooltip_set_markup (tooltip, link->title);
6188 return GTK_WIDGET_CLASS (gtk_label_parent_class)->query_tooltip (widget,
6195 #define __GTK_LABEL_C__
6196 #include "gtkaliasdef.c"