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_has_window (GTK_WIDGET (label), FALSE);
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)
3415 if (!label->select_info)
3418 widget = GTK_WIDGET (label);
3420 if (gtk_widget_get_realized (widget))
3422 GdkDisplay *display;
3425 if (gtk_widget_is_sensitive (widget))
3427 display = gtk_widget_get_display (widget);
3429 if (label->select_info->active_link)
3430 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
3431 else if (label->select_info->selectable)
3432 cursor = gdk_cursor_new_for_display (display, GDK_XTERM);
3439 gdk_window_set_cursor (label->select_info->window, cursor);
3442 gdk_cursor_unref (cursor);
3447 gtk_label_state_changed (GtkWidget *widget,
3448 GtkStateType prev_state)
3450 GtkLabel *label = GTK_LABEL (widget);
3452 if (label->select_info)
3454 gtk_label_select_region (label, 0, 0);
3455 gtk_label_update_cursor (label);
3458 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed)
3459 GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed (widget, prev_state);
3463 gtk_label_style_set (GtkWidget *widget,
3464 GtkStyle *previous_style)
3466 GtkLabel *label = GTK_LABEL (widget);
3468 /* We have to clear the layout, fonts etc. may have changed */
3469 gtk_label_clear_layout (label);
3470 gtk_label_invalidate_wrap_width (label);
3474 gtk_label_direction_changed (GtkWidget *widget,
3475 GtkTextDirection previous_dir)
3477 GtkLabel *label = GTK_LABEL (widget);
3480 pango_layout_context_changed (label->layout);
3482 GTK_WIDGET_CLASS (gtk_label_parent_class)->direction_changed (widget, previous_dir);
3486 get_layout_location (GtkLabel *label,
3492 GtkLabelPrivate *priv;
3494 gint req_width, x, y;
3495 PangoRectangle logical;
3497 misc = GTK_MISC (label);
3498 widget = GTK_WIDGET (label);
3499 priv = GTK_LABEL_GET_PRIVATE (label);
3501 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
3502 xalign = misc->xalign;
3504 xalign = 1.0 - misc->xalign;
3506 pango_layout_get_pixel_extents (label->layout, NULL, &logical);
3508 if (label->ellipsize || priv->width_chars > 0)
3512 width = pango_layout_get_width (label->layout);
3514 req_width = logical.width;
3516 req_width = MIN(PANGO_PIXELS (width), req_width);
3517 req_width += 2 * misc->xpad;
3520 req_width = widget->requisition.width;
3522 x = floor (widget->allocation.x + (gint)misc->xpad +
3523 xalign * (widget->allocation.width - req_width));
3525 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
3526 x = MAX (x, widget->allocation.x + misc->xpad);
3528 x = MIN (x, widget->allocation.x + widget->allocation.width - misc->xpad);
3531 /* bgo#315462 - For single-line labels, *do* align the requisition with
3532 * respect to the allocation, even if we are under-allocated. For multi-line
3533 * labels, always show the top of the text when they are under-allocated. The
3534 * rationale is this:
3536 * - Single-line labels appear in GtkButtons, and it is very easy to get them
3537 * to be smaller than their requisition. The button may clip the label, but
3538 * the label will still be able to show most of itself and the focus
3539 * rectangle. Also, it is fairly easy to read a single line of clipped text.
3541 * - Multi-line labels should not be clipped to showing "something in the
3542 * middle". You want to read the first line, at least, to get some context.
3544 if (pango_layout_get_line_count (label->layout) == 1)
3545 y = floor (widget->allocation.y + (gint)misc->ypad
3546 + (widget->allocation.height - widget->requisition.height) * misc->yalign);
3548 y = floor (widget->allocation.y + (gint)misc->ypad
3549 + MAX (((widget->allocation.height - widget->requisition.height) * misc->yalign),
3560 draw_insertion_cursor (GtkLabel *label,
3561 GdkRectangle *cursor_location,
3562 gboolean is_primary,
3563 PangoDirection direction,
3564 gboolean draw_arrow)
3566 GtkWidget *widget = GTK_WIDGET (label);
3567 GtkTextDirection text_dir;
3569 if (direction == PANGO_DIRECTION_LTR)
3570 text_dir = GTK_TEXT_DIR_LTR;
3572 text_dir = GTK_TEXT_DIR_RTL;
3574 gtk_draw_insertion_cursor (widget, widget->window, &(widget->allocation),
3576 is_primary, text_dir, draw_arrow);
3579 static PangoDirection
3580 get_cursor_direction (GtkLabel *label)
3584 g_assert (label->select_info);
3586 gtk_label_ensure_layout (label);
3588 for (l = pango_layout_get_lines_readonly (label->layout); l; l = l->next)
3590 PangoLayoutLine *line = l->data;
3592 /* If label->select_info->selection_end is at the very end of
3593 * the line, we don't know if the cursor is on this line or
3594 * the next without looking ahead at the next line. (End
3595 * of paragraph is different from line break.) But it's
3596 * definitely in this paragraph, which is good enough
3597 * to figure out the resolved direction.
3599 if (line->start_index + line->length >= label->select_info->selection_end)
3600 return line->resolved_dir;
3603 return PANGO_DIRECTION_LTR;
3607 gtk_label_draw_cursor (GtkLabel *label, gint xoffset, gint yoffset)
3611 if (label->select_info == NULL)
3614 widget = GTK_WIDGET (label);
3616 if (gtk_widget_is_drawable (widget))
3618 PangoDirection keymap_direction;
3619 PangoDirection cursor_direction;
3620 PangoRectangle strong_pos, weak_pos;
3621 gboolean split_cursor;
3622 PangoRectangle *cursor1 = NULL;
3623 PangoRectangle *cursor2 = NULL;
3624 GdkRectangle cursor_location;
3625 PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
3626 PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
3628 keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gtk_widget_get_display (widget)));
3629 cursor_direction = get_cursor_direction (label);
3631 gtk_label_ensure_layout (label);
3633 pango_layout_get_cursor_pos (label->layout, label->select_info->selection_end,
3634 &strong_pos, &weak_pos);
3636 g_object_get (gtk_widget_get_settings (widget),
3637 "gtk-split-cursor", &split_cursor,
3640 dir1 = cursor_direction;
3644 cursor1 = &strong_pos;
3646 if (strong_pos.x != weak_pos.x ||
3647 strong_pos.y != weak_pos.y)
3649 dir2 = (cursor_direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
3650 cursor2 = &weak_pos;
3655 if (keymap_direction == cursor_direction)
3656 cursor1 = &strong_pos;
3658 cursor1 = &weak_pos;
3661 cursor_location.x = xoffset + PANGO_PIXELS (cursor1->x);
3662 cursor_location.y = yoffset + PANGO_PIXELS (cursor1->y);
3663 cursor_location.width = 0;
3664 cursor_location.height = PANGO_PIXELS (cursor1->height);
3666 draw_insertion_cursor (label,
3667 &cursor_location, TRUE, dir1,
3668 dir2 != PANGO_DIRECTION_NEUTRAL);
3670 if (dir2 != PANGO_DIRECTION_NEUTRAL)
3672 cursor_location.x = xoffset + PANGO_PIXELS (cursor2->x);
3673 cursor_location.y = yoffset + PANGO_PIXELS (cursor2->y);
3674 cursor_location.width = 0;
3675 cursor_location.height = PANGO_PIXELS (cursor2->height);
3677 draw_insertion_cursor (label,
3678 &cursor_location, FALSE, dir2,
3684 static GtkLabelLink *
3685 gtk_label_get_focus_link (GtkLabel *label)
3687 GtkLabelSelectionInfo *info = label->select_info;
3693 if (info->selection_anchor != info->selection_end)
3696 for (l = info->links; l; l = l->next)
3698 GtkLabelLink *link = l->data;
3699 if (link->start <= info->selection_anchor &&
3700 info->selection_anchor <= link->end)
3708 gtk_label_expose (GtkWidget *widget,
3709 GdkEventExpose *event)
3711 GtkLabel *label = GTK_LABEL (widget);
3712 GtkLabelSelectionInfo *info = label->select_info;
3715 gtk_label_ensure_layout (label);
3717 if (gtk_widget_get_visible (widget) && gtk_widget_get_mapped (widget) &&
3718 label->text && (*label->text != '\0'))
3720 get_layout_location (label, &x, &y);
3722 gtk_paint_layout (widget->style,
3724 GTK_WIDGET_STATE (widget),
3733 (info->selection_anchor != info->selection_end))
3739 range[0] = info->selection_anchor;
3740 range[1] = info->selection_end;
3742 if (range[0] > range[1])
3744 gint tmp = range[0];
3745 range[0] = range[1];
3749 clip = gdk_pango_layout_get_clip_region (label->layout,
3753 gdk_region_intersect (clip, event->region);
3755 /* FIXME should use gtk_paint, but it can't use a clip
3759 gdk_gc_set_clip_region (widget->style->black_gc, clip);
3762 state = GTK_STATE_SELECTED;
3763 if (!gtk_widget_has_focus (widget))
3764 state = GTK_STATE_ACTIVE;
3766 gdk_draw_layout_with_colors (widget->window,
3767 widget->style->black_gc,
3770 &widget->style->text[state],
3771 &widget->style->base[state]);
3773 gdk_gc_set_clip_region (widget->style->black_gc, NULL);
3774 gdk_region_destroy (clip);
3778 GtkLabelLink *focus_link;
3779 GtkLabelLink *active_link;
3783 GdkColor *text_color;
3784 GdkColor *base_color;
3785 GdkColor *link_color;
3786 GdkColor *visited_link_color;
3788 if (info->selectable && gtk_widget_has_focus (widget))
3789 gtk_label_draw_cursor (label, x, y);
3791 focus_link = gtk_label_get_focus_link (label);
3792 active_link = info->active_link;
3796 range[0] = active_link->start;
3797 range[1] = active_link->end;
3799 clip = gdk_pango_layout_get_clip_region (label->layout,
3803 gdk_gc_set_clip_region (widget->style->black_gc, clip);
3805 gtk_label_get_link_colors (widget, &link_color, &visited_link_color);
3806 if (active_link->visited)
3807 text_color = visited_link_color;
3809 text_color = link_color;
3810 if (info->link_clicked)
3811 base_color = &widget->style->base[GTK_STATE_ACTIVE];
3813 base_color = &widget->style->base[GTK_STATE_PRELIGHT];
3814 gdk_draw_layout_with_colors (widget->window,
3815 widget->style->black_gc,
3820 gdk_color_free (link_color);
3821 gdk_color_free (visited_link_color);
3823 gdk_gc_set_clip_region (widget->style->black_gc, NULL);
3824 gdk_region_destroy (clip);
3827 if (focus_link && gtk_widget_has_focus (widget))
3829 range[0] = focus_link->start;
3830 range[1] = focus_link->end;
3832 clip = gdk_pango_layout_get_clip_region (label->layout,
3836 gdk_region_get_clipbox (clip, &rect);
3838 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
3839 &event->area, widget, "label",
3840 rect.x, rect.y, rect.width, rect.height);
3842 gdk_region_destroy (clip);
3851 separate_uline_pattern (const gchar *str,
3856 gboolean underscore;
3859 gchar *pattern_dest;
3861 *accel_key = GDK_VoidSymbol;
3862 *new_str = g_new (gchar, strlen (str) + 1);
3863 *pattern = g_new (gchar, g_utf8_strlen (str, -1) + 1);
3869 pattern_dest = *pattern;
3874 const gchar *next_src;
3876 c = g_utf8_get_char (src);
3877 if (c == (gunichar)-1)
3879 g_warning ("Invalid input string");
3885 next_src = g_utf8_next_char (src);
3890 *pattern_dest++ = ' ';
3893 *pattern_dest++ = '_';
3894 if (*accel_key == GDK_VoidSymbol)
3895 *accel_key = gdk_keyval_to_lower (gdk_unicode_to_keyval (c));
3898 while (src < next_src)
3912 while (src < next_src)
3915 *pattern_dest++ = ' ';
3927 gtk_label_set_uline_text_internal (GtkLabel *label,
3930 guint accel_key = GDK_VoidSymbol;
3934 g_return_if_fail (GTK_IS_LABEL (label));
3935 g_return_if_fail (str != NULL);
3937 /* Split text into the base text and a separate pattern
3940 if (!separate_uline_pattern (str, &accel_key, &new_str, &pattern))
3943 gtk_label_set_text_internal (label, new_str);
3944 gtk_label_set_pattern_internal (label, pattern, TRUE);
3945 label->mnemonic_keyval = accel_key;
3951 gtk_label_parse_uline (GtkLabel *label,
3956 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_VoidSymbol);
3957 g_return_val_if_fail (str != NULL, GDK_VoidSymbol);
3959 g_object_freeze_notify (G_OBJECT (label));
3961 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
3962 gtk_label_set_use_markup_internal (label, FALSE);
3963 gtk_label_set_use_underline_internal (label, TRUE);
3965 gtk_label_recalculate (label);
3967 keyval = label->mnemonic_keyval;
3968 if (keyval != GDK_VoidSymbol)
3970 label->mnemonic_keyval = GDK_VoidSymbol;
3971 gtk_label_setup_mnemonic (label, keyval);
3972 g_object_notify (G_OBJECT (label), "mnemonic-keyval");
3975 g_object_thaw_notify (G_OBJECT (label));
3981 * gtk_label_set_text_with_mnemonic:
3982 * @label: a #GtkLabel
3985 * Sets the label's text from the string @str.
3986 * If characters in @str are preceded by an underscore, they are underlined
3987 * indicating that they represent a keyboard accelerator called a mnemonic.
3988 * The mnemonic key can be used to activate another widget, chosen
3989 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
3992 gtk_label_set_text_with_mnemonic (GtkLabel *label,
3995 g_return_if_fail (GTK_IS_LABEL (label));
3996 g_return_if_fail (str != NULL);
3998 g_object_freeze_notify (G_OBJECT (label));
4000 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
4001 gtk_label_set_use_markup_internal (label, FALSE);
4002 gtk_label_set_use_underline_internal (label, TRUE);
4004 gtk_label_recalculate (label);
4006 g_object_thaw_notify (G_OBJECT (label));
4010 gtk_label_realize (GtkWidget *widget)
4014 label = GTK_LABEL (widget);
4016 GTK_WIDGET_CLASS (gtk_label_parent_class)->realize (widget);
4018 if (label->select_info)
4019 gtk_label_create_window (label);
4023 gtk_label_unrealize (GtkWidget *widget)
4027 label = GTK_LABEL (widget);
4029 if (label->select_info)
4030 gtk_label_destroy_window (label);
4032 GTK_WIDGET_CLASS (gtk_label_parent_class)->unrealize (widget);
4036 gtk_label_map (GtkWidget *widget)
4040 label = GTK_LABEL (widget);
4042 GTK_WIDGET_CLASS (gtk_label_parent_class)->map (widget);
4044 if (label->select_info)
4045 gdk_window_show (label->select_info->window);
4049 gtk_label_unmap (GtkWidget *widget)
4053 label = GTK_LABEL (widget);
4055 if (label->select_info)
4056 gdk_window_hide (label->select_info->window);
4058 GTK_WIDGET_CLASS (gtk_label_parent_class)->unmap (widget);
4062 window_to_layout_coords (GtkLabel *label,
4069 widget = GTK_WIDGET (label);
4071 /* get layout location in widget->window coords */
4072 get_layout_location (label, &lx, &ly);
4076 *x += widget->allocation.x; /* go to widget->window */
4077 *x -= lx; /* go to layout */
4082 *y += widget->allocation.y; /* go to widget->window */
4083 *y -= ly; /* go to layout */
4089 layout_to_window_coords (GtkLabel *label,
4096 widget = GTK_WIDGET (label);
4098 /* get layout location in widget->window coords */
4099 get_layout_location (label, &lx, &ly);
4103 *x += lx; /* go to widget->window */
4104 *x -= widget->allocation.x; /* go to selection window */
4109 *y += ly; /* go to widget->window */
4110 *y -= widget->allocation.y; /* go to selection window */
4116 get_layout_index (GtkLabel *label,
4122 const gchar *cluster;
4123 const gchar *cluster_end;
4128 gtk_label_ensure_layout (label);
4130 window_to_layout_coords (label, &x, &y);
4135 inside = pango_layout_xy_to_index (label->layout,
4139 cluster = label->text + *index;
4140 cluster_end = cluster;
4143 cluster_end = g_utf8_next_char (cluster_end);
4147 *index += (cluster_end - cluster);
4153 gtk_label_select_word (GtkLabel *label)
4157 gint start_index = gtk_label_move_backward_word (label, label->select_info->selection_end);
4158 gint end_index = gtk_label_move_forward_word (label, label->select_info->selection_end);
4160 min = MIN (label->select_info->selection_anchor,
4161 label->select_info->selection_end);
4162 max = MAX (label->select_info->selection_anchor,
4163 label->select_info->selection_end);
4165 min = MIN (min, start_index);
4166 max = MAX (max, end_index);
4168 gtk_label_select_region_index (label, min, max);
4172 gtk_label_grab_focus (GtkWidget *widget)
4175 gboolean select_on_focus;
4178 label = GTK_LABEL (widget);
4180 if (label->select_info == NULL)
4183 GTK_WIDGET_CLASS (gtk_label_parent_class)->grab_focus (widget);
4185 if (label->select_info->selectable)
4187 g_object_get (gtk_widget_get_settings (widget),
4188 "gtk-label-select-on-focus",
4192 if (select_on_focus && !label->in_click)
4193 gtk_label_select_region (label, 0, -1);
4197 if (label->select_info->links && !label->in_click)
4199 link = label->select_info->links->data;
4200 label->select_info->selection_anchor = link->start;
4201 label->select_info->selection_end = link->start;
4207 gtk_label_focus (GtkWidget *widget,
4208 GtkDirectionType direction)
4210 GtkLabel *label = GTK_LABEL (widget);
4211 GtkLabelSelectionInfo *info = label->select_info;
4212 GtkLabelLink *focus_link;
4215 if (!gtk_widget_is_focus (widget))
4217 gtk_widget_grab_focus (widget);
4220 focus_link = gtk_label_get_focus_link (label);
4221 if (focus_link && direction == GTK_DIR_TAB_BACKWARD)
4223 l = g_list_last (info->links);
4224 focus_link = l->data;
4225 info->selection_anchor = focus_link->start;
4226 info->selection_end = focus_link->start;
4236 if (info->selectable)
4240 if (info->selection_anchor != info->selection_end)
4243 index = info->selection_anchor;
4245 if (direction == GTK_DIR_TAB_FORWARD)
4246 for (l = info->links; l; l = l->next)
4248 GtkLabelLink *link = l->data;
4250 if (link->start > index)
4252 gtk_label_select_region_index (label, link->start, link->start);
4256 else if (direction == GTK_DIR_TAB_BACKWARD)
4257 for (l = g_list_last (info->links); l; l = l->prev)
4259 GtkLabelLink *link = l->data;
4261 if (link->end < index)
4263 gtk_label_select_region_index (label, link->start, link->start);
4272 focus_link = gtk_label_get_focus_link (label);
4275 case GTK_DIR_TAB_FORWARD:
4278 l = g_list_find (info->links, focus_link);
4285 case GTK_DIR_TAB_BACKWARD:
4288 l = g_list_find (info->links, focus_link);
4292 l = g_list_last (info->links);
4301 focus_link = l->data;
4302 info->selection_anchor = focus_link->start;
4303 info->selection_end = focus_link->start;
4304 gtk_widget_queue_draw (widget);
4316 gtk_label_button_press (GtkWidget *widget,
4317 GdkEventButton *event)
4319 GtkLabel *label = GTK_LABEL (widget);
4320 GtkLabelSelectionInfo *info = label->select_info;
4327 if (info->active_link)
4329 if (event->button == 1)
4331 info->link_clicked = 1;
4332 gtk_widget_queue_draw (widget);
4334 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4336 info->link_clicked = 1;
4337 gtk_label_do_popup (label, event);
4342 if (!info->selectable)
4345 info->in_drag = FALSE;
4346 info->select_words = FALSE;
4348 if (event->button == 1)
4350 if (!gtk_widget_has_focus (widget))
4352 label->in_click = TRUE;
4353 gtk_widget_grab_focus (widget);
4354 label->in_click = FALSE;
4357 if (event->type == GDK_3BUTTON_PRESS)
4359 gtk_label_select_region_index (label, 0, strlen (label->text));
4363 if (event->type == GDK_2BUTTON_PRESS)
4365 info->select_words = TRUE;
4366 gtk_label_select_word (label);
4370 get_layout_index (label, event->x, event->y, &index);
4372 min = MIN (info->selection_anchor, info->selection_end);
4373 max = MAX (info->selection_anchor, info->selection_end);
4375 if ((info->selection_anchor != info->selection_end) &&
4376 (event->state & GDK_SHIFT_MASK))
4378 /* extend (same as motion) */
4379 min = MIN (min, index);
4380 max = MAX (max, index);
4382 /* ensure the anchor is opposite index */
4390 gtk_label_select_region_index (label, min, max);
4394 if (event->type == GDK_3BUTTON_PRESS)
4395 gtk_label_select_region_index (label, 0, strlen (label->text));
4396 else if (event->type == GDK_2BUTTON_PRESS)
4397 gtk_label_select_word (label);
4398 else if (min < max && min <= index && index <= max)
4400 info->in_drag = TRUE;
4401 info->drag_start_x = event->x;
4402 info->drag_start_y = event->y;
4405 /* start a replacement */
4406 gtk_label_select_region_index (label, index, index);
4411 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4413 gtk_label_do_popup (label, event);
4421 gtk_label_button_release (GtkWidget *widget,
4422 GdkEventButton *event)
4425 GtkLabel *label = GTK_LABEL (widget);
4426 GtkLabelSelectionInfo *info = label->select_info;
4436 get_layout_index (label, event->x, event->y, &index);
4437 gtk_label_select_region_index (label, index, index);
4442 if (event->button != 1)
4445 if (info->active_link &&
4446 info->selection_anchor == info->selection_end &&
4449 emit_activate_link (label, info->active_link);
4450 info->link_clicked = 0;
4455 /* The goal here is to return TRUE iff we ate the
4456 * button press to start selecting.
4462 connect_mnemonics_visible_notify (GtkLabel *label)
4464 GtkLabelPrivate *priv = GTK_LABEL_GET_PRIVATE (label);
4465 GtkWidget *toplevel;
4468 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
4470 if (!GTK_IS_WINDOW (toplevel))
4473 /* always set up this widgets initial value */
4474 priv->mnemonics_visible =
4475 gtk_window_get_mnemonics_visible (GTK_WINDOW (toplevel));
4478 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (toplevel),
4479 "gtk-label-mnemonics-visible-connected"));
4483 g_signal_connect (toplevel,
4484 "notify::mnemonics-visible",
4485 G_CALLBACK (label_mnemonics_visible_changed),
4487 g_object_set_data (G_OBJECT (toplevel),
4488 "gtk-label-mnemonics-visible-connected",
4489 GINT_TO_POINTER (1));
4494 drag_begin_cb (GtkWidget *widget,
4495 GdkDragContext *context,
4499 GdkPixmap *pixmap = NULL;
4501 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
4503 label = GTK_LABEL (widget);
4505 if ((label->select_info->selection_anchor !=
4506 label->select_info->selection_end) &&
4512 start = MIN (label->select_info->selection_anchor,
4513 label->select_info->selection_end);
4514 end = MAX (label->select_info->selection_anchor,
4515 label->select_info->selection_end);
4517 len = strlen (label->text);
4525 pixmap = _gtk_text_util_create_drag_icon (widget,
4526 label->text + start,
4531 gtk_drag_set_icon_pixmap (context,
4532 gdk_drawable_get_colormap (pixmap),
4537 gtk_drag_set_icon_default (context);
4540 g_object_unref (pixmap);
4544 gtk_label_motion (GtkWidget *widget,
4545 GdkEventMotion *event)
4547 GtkLabel *label = GTK_LABEL (widget);
4548 GtkLabelSelectionInfo *info = label->select_info;
4555 if (info->links && !info->in_drag)
4559 gboolean found = FALSE;
4561 if (info->selection_anchor == info->selection_end)
4563 gdk_window_get_pointer (event->window, &x, &y, NULL);
4564 if (get_layout_index (label, x, y, &index))
4566 for (l = info->links; l != NULL; l = l->next)
4569 if (index >= link->start && index <= link->end)
4580 if (info->active_link != link)
4582 info->link_clicked = 0;
4583 info->active_link = link;
4584 gtk_label_update_cursor (label);
4585 gtk_widget_queue_draw (widget);
4590 if (info->active_link != NULL)
4592 info->link_clicked = 0;
4593 info->active_link = NULL;
4594 gtk_label_update_cursor (label);
4595 gtk_widget_queue_draw (widget);
4600 if (!info->selectable)
4603 if ((event->state & GDK_BUTTON1_MASK) == 0)
4606 gdk_window_get_pointer (info->window, &x, &y, NULL);
4610 if (gtk_drag_check_threshold (widget,
4613 event->x, event->y))
4615 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
4617 gtk_target_list_add_text_targets (target_list, 0);
4619 g_signal_connect (widget, "drag-begin",
4620 G_CALLBACK (drag_begin_cb), NULL);
4621 gtk_drag_begin (widget, target_list,
4623 1, (GdkEvent *)event);
4625 info->in_drag = FALSE;
4627 gtk_target_list_unref (target_list);
4632 get_layout_index (label, x, y, &index);
4634 if (info->select_words)
4637 gint old_min, old_max;
4640 min = gtk_label_move_backward_word (label, index);
4641 max = gtk_label_move_forward_word (label, index);
4643 anchor = info->selection_anchor;
4644 end = info->selection_end;
4646 old_min = MIN (anchor, end);
4647 old_max = MAX (anchor, end);
4654 else if (old_max < max)
4659 else if (anchor == old_min)
4670 gtk_label_select_region_index (label, anchor, end);
4673 gtk_label_select_region_index (label, info->selection_anchor, index);
4680 gtk_label_leave_notify (GtkWidget *widget,
4681 GdkEventCrossing *event)
4683 GtkLabel *label = GTK_LABEL (widget);
4685 if (label->select_info)
4687 label->select_info->active_link = NULL;
4688 gtk_label_update_cursor (label);
4689 gtk_widget_queue_draw (widget);
4692 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event)
4693 return GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event (widget, event);
4699 gtk_label_create_window (GtkLabel *label)
4702 GdkWindowAttr attributes;
4703 gint attributes_mask;
4705 g_assert (label->select_info);
4706 widget = GTK_WIDGET (label);
4707 g_assert (gtk_widget_get_realized (widget));
4709 if (label->select_info->window)
4712 attributes.x = widget->allocation.x;
4713 attributes.y = widget->allocation.y;
4714 attributes.width = widget->allocation.width;
4715 attributes.height = widget->allocation.height;
4716 attributes.window_type = GDK_WINDOW_CHILD;
4717 attributes.wclass = GDK_INPUT_ONLY;
4718 attributes.override_redirect = TRUE;
4719 attributes.event_mask = gtk_widget_get_events (widget) |
4720 GDK_BUTTON_PRESS_MASK |
4721 GDK_BUTTON_RELEASE_MASK |
4722 GDK_LEAVE_NOTIFY_MASK |
4723 GDK_BUTTON_MOTION_MASK |
4724 GDK_POINTER_MOTION_MASK |
4725 GDK_POINTER_MOTION_HINT_MASK;
4726 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
4727 if (gtk_widget_is_sensitive (widget))
4729 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
4731 attributes_mask |= GDK_WA_CURSOR;
4735 label->select_info->window = gdk_window_new (widget->window,
4736 &attributes, attributes_mask);
4737 gdk_window_set_user_data (label->select_info->window, widget);
4739 if (attributes_mask & GDK_WA_CURSOR)
4740 gdk_cursor_unref (attributes.cursor);
4744 gtk_label_destroy_window (GtkLabel *label)
4746 g_assert (label->select_info);
4748 if (label->select_info->window == NULL)
4751 gdk_window_set_user_data (label->select_info->window, NULL);
4752 gdk_window_destroy (label->select_info->window);
4753 label->select_info->window = NULL;
4757 gtk_label_ensure_select_info (GtkLabel *label)
4759 if (label->select_info == NULL)
4761 label->select_info = g_new0 (GtkLabelSelectionInfo, 1);
4763 gtk_widget_set_can_focus (GTK_WIDGET (label), TRUE);
4765 if (gtk_widget_get_realized (GTK_WIDGET (label)))
4766 gtk_label_create_window (label);
4768 if (gtk_widget_get_mapped (GTK_WIDGET (label)))
4769 gdk_window_show (label->select_info->window);
4774 gtk_label_clear_select_info (GtkLabel *label)
4776 if (label->select_info == NULL)
4779 if (!label->select_info->selectable && !label->select_info->links)
4781 gtk_label_destroy_window (label);
4783 g_free (label->select_info);
4784 label->select_info = NULL;
4786 gtk_widget_set_can_focus (GTK_WIDGET (label), FALSE);
4791 * gtk_label_set_selectable:
4792 * @label: a #GtkLabel
4793 * @setting: %TRUE to allow selecting text in the label
4795 * Selectable labels allow the user to select text from the label, for
4799 gtk_label_set_selectable (GtkLabel *label,
4802 gboolean old_setting;
4804 g_return_if_fail (GTK_IS_LABEL (label));
4806 setting = setting != FALSE;
4807 old_setting = label->select_info && label->select_info->selectable;
4811 gtk_label_ensure_select_info (label);
4812 label->select_info->selectable = TRUE;
4813 gtk_label_update_cursor (label);
4819 /* unselect, to give up the selection */
4820 gtk_label_select_region (label, 0, 0);
4822 label->select_info->selectable = FALSE;
4823 gtk_label_clear_select_info (label);
4824 gtk_label_update_cursor (label);
4827 if (setting != old_setting)
4829 g_object_freeze_notify (G_OBJECT (label));
4830 g_object_notify (G_OBJECT (label), "selectable");
4831 g_object_notify (G_OBJECT (label), "cursor-position");
4832 g_object_notify (G_OBJECT (label), "selection-bound");
4833 g_object_thaw_notify (G_OBJECT (label));
4834 gtk_widget_queue_draw (GTK_WIDGET (label));
4839 * gtk_label_get_selectable:
4840 * @label: a #GtkLabel
4842 * Gets the value set by gtk_label_set_selectable().
4844 * Return value: %TRUE if the user can copy text from the label
4847 gtk_label_get_selectable (GtkLabel *label)
4849 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
4851 return label->select_info && label->select_info->selectable;
4855 free_angle (gpointer angle)
4857 g_slice_free (gdouble, angle);
4861 * gtk_label_set_angle:
4862 * @label: a #GtkLabel
4863 * @angle: the angle that the baseline of the label makes with
4864 * the horizontal, in degrees, measured counterclockwise
4866 * Sets the angle of rotation for the label. An angle of 90 reads from
4867 * from bottom to top, an angle of 270, from top to bottom. The angle
4868 * setting for the label is ignored if the label is selectable,
4869 * wrapped, or ellipsized.
4874 gtk_label_set_angle (GtkLabel *label,
4877 gdouble *label_angle;
4879 g_return_if_fail (GTK_IS_LABEL (label));
4881 label_angle = (gdouble *)g_object_get_qdata (G_OBJECT (label), quark_angle);
4885 label_angle = g_slice_new (gdouble);
4887 g_object_set_qdata_full (G_OBJECT (label), quark_angle,
4888 label_angle, free_angle);
4891 /* Canonicalize to [0,360]. We don't canonicalize 360 to 0, because
4892 * double property ranges are inclusive, and changing 360 to 0 would
4893 * make a property editor behave strangely.
4895 if (angle < 0 || angle > 360.0)
4896 angle = angle - 360. * floor (angle / 360.);
4898 if (*label_angle != angle)
4900 *label_angle = angle;
4902 gtk_label_clear_layout (label);
4903 gtk_widget_queue_resize (GTK_WIDGET (label));
4905 g_object_notify (G_OBJECT (label), "angle");
4910 * gtk_label_get_angle:
4911 * @label: a #GtkLabel
4913 * Gets the angle of rotation for the label. See
4914 * gtk_label_set_angle().
4916 * Return value: the angle of rotation for the label
4921 gtk_label_get_angle (GtkLabel *label)
4925 g_return_val_if_fail (GTK_IS_LABEL (label), 0.0);
4927 angle = (gdouble *)g_object_get_qdata (G_OBJECT (label), quark_angle);
4936 gtk_label_set_selection_text (GtkLabel *label,
4937 GtkSelectionData *selection_data)
4939 if ((label->select_info->selection_anchor !=
4940 label->select_info->selection_end) &&
4946 start = MIN (label->select_info->selection_anchor,
4947 label->select_info->selection_end);
4948 end = MAX (label->select_info->selection_anchor,
4949 label->select_info->selection_end);
4951 len = strlen (label->text);
4959 gtk_selection_data_set_text (selection_data,
4960 label->text + start,
4966 gtk_label_drag_data_get (GtkWidget *widget,
4967 GdkDragContext *context,
4968 GtkSelectionData *selection_data,
4972 gtk_label_set_selection_text (GTK_LABEL (widget), selection_data);
4976 get_text_callback (GtkClipboard *clipboard,
4977 GtkSelectionData *selection_data,
4979 gpointer user_data_or_owner)
4981 gtk_label_set_selection_text (GTK_LABEL (user_data_or_owner), selection_data);
4985 clear_text_callback (GtkClipboard *clipboard,
4986 gpointer user_data_or_owner)
4990 label = GTK_LABEL (user_data_or_owner);
4992 if (label->select_info)
4994 label->select_info->selection_anchor = label->select_info->selection_end;
4996 gtk_widget_queue_draw (GTK_WIDGET (label));
5001 gtk_label_select_region_index (GtkLabel *label,
5005 g_return_if_fail (GTK_IS_LABEL (label));
5007 if (label->select_info && label->select_info->selectable)
5009 GtkClipboard *clipboard;
5011 if (label->select_info->selection_anchor == anchor_index &&
5012 label->select_info->selection_end == end_index)
5015 label->select_info->selection_anchor = anchor_index;
5016 label->select_info->selection_end = end_index;
5018 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label),
5019 GDK_SELECTION_PRIMARY);
5021 if (anchor_index != end_index)
5023 GtkTargetList *list;
5024 GtkTargetEntry *targets;
5027 list = gtk_target_list_new (NULL, 0);
5028 gtk_target_list_add_text_targets (list, 0);
5029 targets = gtk_target_table_new_from_list (list, &n_targets);
5031 gtk_clipboard_set_with_owner (clipboard,
5034 clear_text_callback,
5037 gtk_target_table_free (targets, n_targets);
5038 gtk_target_list_unref (list);
5042 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (label))
5043 gtk_clipboard_clear (clipboard);
5046 gtk_widget_queue_draw (GTK_WIDGET (label));
5048 g_object_freeze_notify (G_OBJECT (label));
5049 g_object_notify (G_OBJECT (label), "cursor-position");
5050 g_object_notify (G_OBJECT (label), "selection-bound");
5051 g_object_thaw_notify (G_OBJECT (label));
5056 * gtk_label_select_region:
5057 * @label: a #GtkLabel
5058 * @start_offset: start offset (in characters not bytes)
5059 * @end_offset: end offset (in characters not bytes)
5061 * Selects a range of characters in the label, if the label is selectable.
5062 * See gtk_label_set_selectable(). If the label is not selectable,
5063 * this function has no effect. If @start_offset or
5064 * @end_offset are -1, then the end of the label will be substituted.
5067 gtk_label_select_region (GtkLabel *label,
5071 g_return_if_fail (GTK_IS_LABEL (label));
5073 if (label->text && label->select_info)
5075 if (start_offset < 0)
5076 start_offset = g_utf8_strlen (label->text, -1);
5079 end_offset = g_utf8_strlen (label->text, -1);
5081 gtk_label_select_region_index (label,
5082 g_utf8_offset_to_pointer (label->text, start_offset) - label->text,
5083 g_utf8_offset_to_pointer (label->text, end_offset) - label->text);
5088 * gtk_label_get_selection_bounds:
5089 * @label: a #GtkLabel
5090 * @start: return location for start of selection, as a character offset
5091 * @end: return location for end of selection, as a character offset
5093 * Gets the selected range of characters in the label, returning %TRUE
5094 * if there's a selection.
5096 * Return value: %TRUE if selection is non-empty
5099 gtk_label_get_selection_bounds (GtkLabel *label,
5103 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5105 if (label->select_info == NULL)
5107 /* not a selectable label */
5117 gint start_index, end_index;
5118 gint start_offset, end_offset;
5121 start_index = MIN (label->select_info->selection_anchor,
5122 label->select_info->selection_end);
5123 end_index = MAX (label->select_info->selection_anchor,
5124 label->select_info->selection_end);
5126 len = strlen (label->text);
5128 if (end_index > len)
5131 if (start_index > len)
5134 start_offset = g_utf8_strlen (label->text, start_index);
5135 end_offset = g_utf8_strlen (label->text, end_index);
5137 if (start_offset > end_offset)
5139 gint tmp = start_offset;
5140 start_offset = end_offset;
5145 *start = start_offset;
5150 return start_offset != end_offset;
5156 * gtk_label_get_layout:
5157 * @label: a #GtkLabel
5159 * Gets the #PangoLayout used to display the label.
5160 * The layout is useful to e.g. convert text positions to
5161 * pixel positions, in combination with gtk_label_get_layout_offsets().
5162 * The returned layout is owned by the label so need not be
5163 * freed by the caller.
5165 * Return value: (transfer none): the #PangoLayout for this label
5168 gtk_label_get_layout (GtkLabel *label)
5170 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
5172 gtk_label_ensure_layout (label);
5174 return label->layout;
5178 * gtk_label_get_layout_offsets:
5179 * @label: a #GtkLabel
5180 * @x: (allow-none): location to store X offset of layout, or %NULL
5181 * @y: (allow-none): location to store Y offset of layout, or %NULL
5183 * Obtains the coordinates where the label will draw the #PangoLayout
5184 * representing the text in the label; useful to convert mouse events
5185 * into coordinates inside the #PangoLayout, e.g. to take some action
5186 * if some part of the label is clicked. Of course you will need to
5187 * create a #GtkEventBox to receive the events, and pack the label
5188 * inside it, since labels are a #GTK_NO_WINDOW widget. Remember
5189 * when using the #PangoLayout functions you need to convert to
5190 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
5193 gtk_label_get_layout_offsets (GtkLabel *label,
5197 g_return_if_fail (GTK_IS_LABEL (label));
5199 gtk_label_ensure_layout (label);
5201 get_layout_location (label, x, y);
5205 * gtk_label_set_use_markup:
5206 * @label: a #GtkLabel
5207 * @setting: %TRUE if the label's text should be parsed for markup.
5209 * Sets whether the text of the label contains markup in <link
5210 * linkend="PangoMarkupFormat">Pango's text markup
5211 * language</link>. See gtk_label_set_markup().
5214 gtk_label_set_use_markup (GtkLabel *label,
5217 g_return_if_fail (GTK_IS_LABEL (label));
5219 gtk_label_set_use_markup_internal (label, setting);
5220 gtk_label_recalculate (label);
5224 * gtk_label_get_use_markup:
5225 * @label: a #GtkLabel
5227 * Returns whether the label's text is interpreted as marked up with
5228 * the <link linkend="PangoMarkupFormat">Pango text markup
5229 * language</link>. See gtk_label_set_use_markup ().
5231 * Return value: %TRUE if the label's text will be parsed for markup.
5234 gtk_label_get_use_markup (GtkLabel *label)
5236 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5238 return label->use_markup;
5242 * gtk_label_set_use_underline:
5243 * @label: a #GtkLabel
5244 * @setting: %TRUE if underlines in the text indicate mnemonics
5246 * If true, an underline in the text indicates the next character should be
5247 * used for the mnemonic accelerator key.
5250 gtk_label_set_use_underline (GtkLabel *label,
5253 g_return_if_fail (GTK_IS_LABEL (label));
5255 gtk_label_set_use_underline_internal (label, setting);
5256 gtk_label_recalculate (label);
5260 gtk_label_get_full_size (GtkLabel *label)
5262 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5263 return GTK_LABEL_GET_PRIVATE (label)->full_size;
5267 gtk_label_set_full_size (GtkLabel *label,
5270 GtkLabelPrivate *priv;
5272 g_return_if_fail (GTK_IS_LABEL (label));
5273 priv = GTK_LABEL_GET_PRIVATE (label);
5275 if (priv->full_size != setting)
5277 priv->full_size = setting;
5279 g_object_notify (G_OBJECT (label), "full-size");
5280 gtk_label_invalidate_wrap_width (label);
5281 gtk_widget_queue_resize (GTK_WIDGET (label));
5286 * gtk_label_get_use_underline:
5287 * @label: a #GtkLabel
5289 * Returns whether an embedded underline in the label indicates a
5290 * mnemonic. See gtk_label_set_use_underline().
5292 * Return value: %TRUE whether an embedded underline in the label indicates
5293 * the mnemonic accelerator keys.
5296 gtk_label_get_use_underline (GtkLabel *label)
5298 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5300 return label->use_underline;
5304 * gtk_label_set_single_line_mode:
5305 * @label: a #GtkLabel
5306 * @single_line_mode: %TRUE if the label should be in single line mode
5308 * Sets whether the label is in single line mode.
5313 gtk_label_set_single_line_mode (GtkLabel *label,
5314 gboolean single_line_mode)
5316 g_return_if_fail (GTK_IS_LABEL (label));
5318 single_line_mode = single_line_mode != FALSE;
5320 if (label->single_line_mode != single_line_mode)
5322 label->single_line_mode = single_line_mode;
5324 gtk_label_clear_layout (label);
5325 gtk_widget_queue_resize (GTK_WIDGET (label));
5327 g_object_notify (G_OBJECT (label), "single-line-mode");
5332 * gtk_label_get_single_line_mode:
5333 * @label: a #GtkLabel
5335 * Returns whether the label is in single line mode.
5337 * Return value: %TRUE when the label is in single line mode.
5342 gtk_label_get_single_line_mode (GtkLabel *label)
5344 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5346 return label->single_line_mode;
5349 /* Compute the X position for an offset that corresponds to the "more important
5350 * cursor position for that offset. We use this when trying to guess to which
5351 * end of the selection we should go to when the user hits the left or
5355 get_better_cursor (GtkLabel *label,
5360 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5361 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5362 PangoDirection cursor_direction = get_cursor_direction (label);
5363 gboolean split_cursor;
5364 PangoRectangle strong_pos, weak_pos;
5366 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5367 "gtk-split-cursor", &split_cursor,
5370 gtk_label_ensure_layout (label);
5372 pango_layout_get_cursor_pos (label->layout, index,
5373 &strong_pos, &weak_pos);
5377 *x = strong_pos.x / PANGO_SCALE;
5378 *y = strong_pos.y / PANGO_SCALE;
5382 if (keymap_direction == cursor_direction)
5384 *x = strong_pos.x / PANGO_SCALE;
5385 *y = strong_pos.y / PANGO_SCALE;
5389 *x = weak_pos.x / PANGO_SCALE;
5390 *y = weak_pos.y / PANGO_SCALE;
5397 gtk_label_move_logically (GtkLabel *label,
5401 gint offset = g_utf8_pointer_to_offset (label->text,
5402 label->text + start);
5406 PangoLogAttr *log_attrs;
5410 gtk_label_ensure_layout (label);
5412 length = g_utf8_strlen (label->text, -1);
5414 pango_layout_get_log_attrs (label->layout, &log_attrs, &n_attrs);
5416 while (count > 0 && offset < length)
5420 while (offset < length && !log_attrs[offset].is_cursor_position);
5424 while (count < 0 && offset > 0)
5428 while (offset > 0 && !log_attrs[offset].is_cursor_position);
5436 return g_utf8_offset_to_pointer (label->text, offset) - label->text;
5440 gtk_label_move_visually (GtkLabel *label,
5450 int new_index, new_trailing;
5451 gboolean split_cursor;
5454 gtk_label_ensure_layout (label);
5456 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5457 "gtk-split-cursor", &split_cursor,
5464 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5465 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5467 strong = keymap_direction == get_cursor_direction (label);
5472 pango_layout_move_cursor_visually (label->layout, strong, index, 0, 1, &new_index, &new_trailing);
5477 pango_layout_move_cursor_visually (label->layout, strong, index, 0, -1, &new_index, &new_trailing);
5481 if (new_index < 0 || new_index == G_MAXINT)
5486 while (new_trailing--)
5487 index = g_utf8_next_char (label->text + new_index) - label->text;
5494 gtk_label_move_forward_word (GtkLabel *label,
5497 gint new_pos = g_utf8_pointer_to_offset (label->text,
5498 label->text + start);
5501 length = g_utf8_strlen (label->text, -1);
5502 if (new_pos < length)
5504 PangoLogAttr *log_attrs;
5507 gtk_label_ensure_layout (label);
5509 pango_layout_get_log_attrs (label->layout, &log_attrs, &n_attrs);
5511 /* Find the next word end */
5513 while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
5519 return g_utf8_offset_to_pointer (label->text, new_pos) - label->text;
5524 gtk_label_move_backward_word (GtkLabel *label,
5527 gint new_pos = g_utf8_pointer_to_offset (label->text,
5528 label->text + start);
5532 PangoLogAttr *log_attrs;
5535 gtk_label_ensure_layout (label);
5537 pango_layout_get_log_attrs (label->layout, &log_attrs, &n_attrs);
5541 /* Find the previous word beginning */
5542 while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
5548 return g_utf8_offset_to_pointer (label->text, new_pos) - label->text;
5552 gtk_label_move_cursor (GtkLabel *label,
5553 GtkMovementStep step,
5555 gboolean extend_selection)
5560 if (label->select_info == NULL)
5563 old_pos = new_pos = label->select_info->selection_end;
5565 if (label->select_info->selection_end != label->select_info->selection_anchor &&
5568 /* If we have a current selection and aren't extending it, move to the
5569 * start/or end of the selection as appropriate
5573 case GTK_MOVEMENT_VISUAL_POSITIONS:
5576 gint anchor_x, anchor_y;
5577 gboolean end_is_left;
5579 get_better_cursor (label, label->select_info->selection_end, &end_x, &end_y);
5580 get_better_cursor (label, label->select_info->selection_anchor, &anchor_x, &anchor_y);
5582 end_is_left = (end_y < anchor_y) || (end_y == anchor_y && end_x < anchor_x);
5585 new_pos = end_is_left ? label->select_info->selection_end : label->select_info->selection_anchor;
5587 new_pos = !end_is_left ? label->select_info->selection_end : label->select_info->selection_anchor;
5590 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5591 case GTK_MOVEMENT_WORDS:
5593 new_pos = MIN (label->select_info->selection_end, label->select_info->selection_anchor);
5595 new_pos = MAX (label->select_info->selection_end, label->select_info->selection_anchor);
5597 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5598 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5599 case GTK_MOVEMENT_BUFFER_ENDS:
5600 /* FIXME: Can do better here */
5601 new_pos = count < 0 ? 0 : strlen (label->text);
5603 case GTK_MOVEMENT_DISPLAY_LINES:
5604 case GTK_MOVEMENT_PARAGRAPHS:
5605 case GTK_MOVEMENT_PAGES:
5606 case GTK_MOVEMENT_HORIZONTAL_PAGES:
5614 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5615 new_pos = gtk_label_move_logically (label, new_pos, count);
5617 case GTK_MOVEMENT_VISUAL_POSITIONS:
5618 new_pos = gtk_label_move_visually (label, new_pos, count);
5619 if (new_pos == old_pos)
5621 if (!extend_selection)
5623 if (!gtk_widget_keynav_failed (GTK_WIDGET (label),
5625 GTK_DIR_RIGHT : GTK_DIR_LEFT))
5627 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
5630 gtk_widget_child_focus (toplevel,
5632 GTK_DIR_RIGHT : GTK_DIR_LEFT);
5637 gtk_widget_error_bell (GTK_WIDGET (label));
5641 case GTK_MOVEMENT_WORDS:
5644 new_pos = gtk_label_move_forward_word (label, new_pos);
5649 new_pos = gtk_label_move_backward_word (label, new_pos);
5652 if (new_pos == old_pos)
5653 gtk_widget_error_bell (GTK_WIDGET (label));
5655 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5656 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5657 case GTK_MOVEMENT_BUFFER_ENDS:
5658 /* FIXME: Can do better here */
5659 new_pos = count < 0 ? 0 : strlen (label->text);
5660 if (new_pos == old_pos)
5661 gtk_widget_error_bell (GTK_WIDGET (label));
5663 case GTK_MOVEMENT_DISPLAY_LINES:
5664 case GTK_MOVEMENT_PARAGRAPHS:
5665 case GTK_MOVEMENT_PAGES:
5666 case GTK_MOVEMENT_HORIZONTAL_PAGES:
5671 if (extend_selection)
5672 gtk_label_select_region_index (label,
5673 label->select_info->selection_anchor,
5676 gtk_label_select_region_index (label, new_pos, new_pos);
5680 gtk_label_copy_clipboard (GtkLabel *label)
5682 if (label->text && label->select_info)
5686 GtkClipboard *clipboard;
5688 start = MIN (label->select_info->selection_anchor,
5689 label->select_info->selection_end);
5690 end = MAX (label->select_info->selection_anchor,
5691 label->select_info->selection_end);
5693 len = strlen (label->text);
5701 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
5704 gtk_clipboard_set_text (clipboard, label->text + start, end - start);
5709 link = gtk_label_get_focus_link (label);
5711 gtk_clipboard_set_text (clipboard, link->uri, -1);
5717 gtk_label_select_all (GtkLabel *label)
5719 gtk_label_select_region_index (label, 0, strlen (label->text));
5722 /* Quick hack of a popup menu
5725 activate_cb (GtkWidget *menuitem,
5728 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
5729 g_signal_emit_by_name (label, signal);
5733 append_action_signal (GtkLabel *label,
5735 const gchar *stock_id,
5736 const gchar *signal,
5739 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
5741 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
5742 g_signal_connect (menuitem, "activate",
5743 G_CALLBACK (activate_cb), label);
5745 gtk_widget_set_sensitive (menuitem, sensitive);
5747 gtk_widget_show (menuitem);
5748 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5752 popup_menu_detach (GtkWidget *attach_widget,
5755 GtkLabel *label = GTK_LABEL (attach_widget);
5757 if (label->select_info)
5758 label->select_info->popup_menu = NULL;
5762 popup_position_func (GtkMenu *menu,
5773 label = GTK_LABEL (user_data);
5774 widget = GTK_WIDGET (label);
5776 g_return_if_fail (gtk_widget_get_realized (widget));
5778 screen = gtk_widget_get_screen (widget);
5779 gdk_window_get_origin (widget->window, x, y);
5781 *x += widget->allocation.x;
5782 *y += widget->allocation.y;
5784 gtk_widget_size_request (GTK_WIDGET (menu), &req);
5786 *x += widget->allocation.width / 2;
5787 *y += widget->allocation.height;
5789 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
5790 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
5794 open_link_activate_cb (GtkMenuItem *menu_item,
5799 link = gtk_label_get_current_link (label);
5802 emit_activate_link (label, link);
5806 copy_link_activate_cb (GtkMenuItem *menu_item,
5809 GtkClipboard *clipboard;
5812 uri = gtk_label_get_current_uri (label);
5815 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
5816 gtk_clipboard_set_text (clipboard, uri, -1);
5821 gtk_label_popup_menu (GtkWidget *widget)
5823 gtk_label_do_popup (GTK_LABEL (widget), NULL);
5829 gtk_label_do_popup (GtkLabel *label,
5830 GdkEventButton *event)
5832 GtkWidget *menuitem;
5835 gboolean have_selection;
5838 if (!label->select_info)
5841 if (label->select_info->popup_menu)
5842 gtk_widget_destroy (label->select_info->popup_menu);
5844 label->select_info->popup_menu = menu = gtk_menu_new ();
5846 gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (label), popup_menu_detach);
5849 label->select_info->selection_anchor != label->select_info->selection_end;
5853 if (label->select_info->link_clicked)
5854 link = label->select_info->active_link;
5859 link = gtk_label_get_focus_link (label);
5861 if (!have_selection && link)
5864 menuitem = gtk_image_menu_item_new_with_mnemonic (_("_Open Link"));
5865 gtk_widget_show (menuitem);
5866 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5868 g_signal_connect (G_OBJECT (menuitem), "activate",
5869 G_CALLBACK (open_link_activate_cb), label);
5871 image = gtk_image_new_from_stock (GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_MENU);
5872 gtk_widget_show (image);
5873 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
5875 /* Copy Link Address */
5876 menuitem = gtk_image_menu_item_new_with_mnemonic (_("Copy _Link Address"));
5877 gtk_widget_show (menuitem);
5878 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5880 g_signal_connect (G_OBJECT (menuitem), "activate",
5881 G_CALLBACK (copy_link_activate_cb), label);
5883 image = gtk_image_new_from_stock (GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
5884 gtk_widget_show (image);
5885 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
5889 append_action_signal (label, menu, GTK_STOCK_CUT, "cut-clipboard", FALSE);
5890 append_action_signal (label, menu, GTK_STOCK_COPY, "copy-clipboard", have_selection);
5891 append_action_signal (label, menu, GTK_STOCK_PASTE, "paste-clipboard", FALSE);
5893 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
5894 gtk_widget_set_sensitive (menuitem, FALSE);
5895 gtk_widget_show (menuitem);
5896 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5898 menuitem = gtk_separator_menu_item_new ();
5899 gtk_widget_show (menuitem);
5900 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5902 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
5903 g_signal_connect_swapped (menuitem, "activate",
5904 G_CALLBACK (gtk_label_select_all), label);
5905 gtk_widget_show (menuitem);
5906 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5909 g_signal_emit (label, signals[POPULATE_POPUP], 0, menu);
5912 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
5914 event->button, event->time);
5917 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
5918 popup_position_func, label,
5919 0, gtk_get_current_event_time ());
5920 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
5925 gtk_label_clear_links (GtkLabel *label)
5927 if (!label->select_info)
5930 g_list_foreach (label->select_info->links, (GFunc)link_free, NULL);
5931 g_list_free (label->select_info->links);
5932 label->select_info->links = NULL;
5933 label->select_info->active_link = NULL;
5937 gtk_label_rescan_links (GtkLabel *label)
5939 PangoLayout *layout = label->layout;
5940 PangoAttrList *attlist;
5941 PangoAttrIterator *iter;
5944 if (!label->select_info || !label->select_info->links)
5947 attlist = pango_layout_get_attributes (layout);
5949 if (attlist == NULL)
5952 iter = pango_attr_list_get_iterator (attlist);
5954 links = label->select_info->links;
5958 PangoAttribute *underline;
5959 PangoAttribute *color;
5961 underline = pango_attr_iterator_get (iter, PANGO_ATTR_UNDERLINE);
5962 color = pango_attr_iterator_get (iter, PANGO_ATTR_FOREGROUND);
5964 if (underline != NULL && color != NULL)
5967 PangoRectangle start_pos;
5968 PangoRectangle end_pos;
5971 pango_attr_iterator_range (iter, &start, &end);
5972 pango_layout_index_to_pos (layout, start, &start_pos);
5973 pango_layout_index_to_pos (layout, end, &end_pos);
5977 g_warning ("Ran out of links");
5981 links = links->next;
5982 link->start = start;
5985 } while (pango_attr_iterator_next (iter));
5987 pango_attr_iterator_destroy (iter);
5991 gtk_label_activate_link (GtkLabel *label,
5994 GtkWidget *widget = GTK_WIDGET (label);
5995 GError *error = NULL;
5997 if (!gtk_show_uri (gtk_widget_get_screen (widget),
5998 uri, gtk_get_current_event_time (), &error))
6000 g_warning ("Unable to show '%s': %s", uri, error->message);
6001 g_error_free (error);
6008 emit_activate_link (GtkLabel *label,
6013 g_signal_emit (label, signals[ACTIVATE_LINK], 0, link->uri, &handled);
6014 if (handled && label->track_links && !link->visited)
6016 link->visited = TRUE;
6017 /* FIXME: shouldn't have to redo everything here */
6018 gtk_label_recalculate (label);
6023 gtk_label_activate_current_link (GtkLabel *label)
6026 GtkWidget *widget = GTK_WIDGET (label);
6028 link = gtk_label_get_focus_link (label);
6032 emit_activate_link (label, link);
6036 GtkWidget *toplevel;
6039 toplevel = gtk_widget_get_toplevel (widget);
6040 if (GTK_IS_WINDOW (toplevel))
6042 window = GTK_WINDOW (toplevel);
6045 window->default_widget != widget &&
6046 !(widget == window->focus_widget &&
6047 (!window->default_widget || !gtk_widget_is_sensitive (window->default_widget))))
6048 gtk_window_activate_default (window);
6053 static GtkLabelLink *
6054 gtk_label_get_current_link (GtkLabel *label)
6058 if (!label->select_info)
6061 if (label->select_info->link_clicked)
6062 link = label->select_info->active_link;
6064 link = gtk_label_get_focus_link (label);
6070 * gtk_label_get_current_uri:
6071 * @label: a #GtkLabel
6073 * Returns the URI for the currently active link in the label.
6074 * The active link is the one under the mouse pointer or, in a
6075 * selectable label, the link in which the text cursor is currently
6078 * This function is intended for use in a #GtkLabel::activate-link handler
6079 * or for use in a #GtkWidget::query-tooltip handler.
6081 * Returns: the currently active URI. The string is owned by GTK+ and must
6082 * not be freed or modified.
6086 G_CONST_RETURN gchar *
6087 gtk_label_get_current_uri (GtkLabel *label)
6090 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
6092 link = gtk_label_get_current_link (label);
6101 * gtk_label_set_track_visited_links:
6102 * @label: a #GtkLabel
6103 * @track_links: %TRUE to track visited links
6105 * Sets whether the label should keep track of clicked
6106 * links (and use a different color for them).
6111 gtk_label_set_track_visited_links (GtkLabel *label,
6112 gboolean track_links)
6114 g_return_if_fail (GTK_IS_LABEL (label));
6116 track_links = track_links != FALSE;
6118 if (label->track_links != track_links)
6120 label->track_links = track_links;
6122 /* FIXME: shouldn't have to redo everything here */
6123 gtk_label_recalculate (label);
6125 g_object_notify (G_OBJECT (label), "track-visited-links");
6130 * gtk_label_get_track_visited_links:
6131 * @label: a #GtkLabel
6133 * Returns whether the label is currently keeping track
6136 * Returns: %TRUE if clicked links are remembered
6141 gtk_label_get_track_visited_links (GtkLabel *label)
6143 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
6145 return label->track_links;
6149 gtk_label_query_tooltip (GtkWidget *widget,
6152 gboolean keyboard_tip,
6153 GtkTooltip *tooltip)
6155 GtkLabel *label = GTK_LABEL (widget);
6156 GtkLabelSelectionInfo *info = label->select_info;
6160 if (info && info->links)
6164 if (info->selection_anchor == info->selection_end)
6165 index = info->selection_anchor;
6169 if (!get_layout_index (label, x, y, &index))
6175 for (l = info->links; l != NULL; l = l->next)
6177 GtkLabelLink *link = l->data;
6178 if (index >= link->start && index <= link->end)
6182 gtk_tooltip_set_markup (tooltip, link->title);
6191 return GTK_WIDGET_CLASS (gtk_label_parent_class)->query_tooltip (widget,
6198 #define __GTK_LABEL_C__
6199 #include "gtkaliasdef.c"