1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
21 * file for a list of people on the GTK+ Team. See the ChangeLog
22 * files for a list of changes. These files are distributed with
23 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
32 #include "gtkaccellabel.h"
35 #include "gtkmarshalers.h"
36 #include "gtkwindow.h"
37 #include "gdk/gdkkeysyms.h"
38 #include "gtkclipboard.h"
39 #include "gtkimagemenuitem.h"
41 #include "gtkseparatormenuitem.h"
42 #include "gtktextutil.h"
43 #include "gtkmenuitem.h"
44 #include "gtknotebook.h"
46 #include "gtkbindings.h"
47 #include "gtkbuildable.h"
50 #include "gtktooltip.h"
51 #include "gtkprivate.h"
54 #define GTK_LABEL_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_LABEL, GtkLabelPrivate))
62 gboolean mnemonics_visible;
65 /* Notes about the handling of links:
67 * Links share the GtkLabelSelectionInfo struct with selectable labels.
68 * There are some new fields for links. The links field contains the list
69 * of GtkLabelLink structs that describe the links which are embedded in
70 * the label. The active_link field points to the link under the mouse
71 * pointer. For keyboard navigation, the 'focus' link is determined by
72 * finding the link which contains the selection_anchor position.
73 * The link_clicked field is used with button press and release events
74 * to ensure that pressing inside a link and releasing outside of it
75 * does not activate the link.
77 * Links are rendered with the link-color/visited-link-color colors
78 * that are determined by the style and with an underline. When the mouse
79 * pointer is over a link, the pointer is changed to indicate the link,
80 * and the background behind the link is rendered with the base[PRELIGHT]
81 * color. While a button is pressed over a link, the background is rendered
82 * with the base[ACTIVE] color.
84 * Labels with links accept keyboard focus, and it is possible to move
85 * the focus between the embedded links using Tab/Shift-Tab. The focus
86 * is indicated by a focus rectangle that is drawn around the link text.
87 * Pressing Enter activates the focussed link, and there is a suitable
88 * context menu for links that can be opened with the Menu key. Pressing
89 * Control-C copies the link URI to the clipboard.
91 * In selectable labels with links, link functionality is only available
92 * when the selection is empty.
97 gchar *title; /* the title attribute, used as tooltip */
98 gboolean visited; /* get set when the link is activated; this flag
99 * gets preserved over later set_markup() calls
101 gint start; /* position of the link in the PangoLayout */
105 struct _GtkLabelSelectionInfo
108 gint selection_anchor;
110 GtkWidget *popup_menu;
113 GtkLabelLink *active_link;
119 guint select_words : 1;
120 guint selectable : 1;
121 guint link_clicked : 1;
129 ACTIVATE_CURRENT_LINK,
144 PROP_MNEMONIC_KEYVAL,
145 PROP_MNEMONIC_WIDGET,
146 PROP_CURSOR_POSITION,
147 PROP_SELECTION_BOUND,
150 PROP_SINGLE_LINE_MODE,
152 PROP_MAX_WIDTH_CHARS,
153 PROP_TRACK_VISITED_LINKS
156 static guint signals[LAST_SIGNAL] = { 0 };
158 static const GdkColor default_link_color = { 0, 0, 0, 0xeeee };
159 static const GdkColor default_visited_link_color = { 0, 0x5555, 0x1a1a, 0x8b8b };
161 static void gtk_label_set_property (GObject *object,
165 static void gtk_label_get_property (GObject *object,
169 static void gtk_label_destroy (GtkObject *object);
170 static void gtk_label_finalize (GObject *object);
171 static void gtk_label_size_request (GtkWidget *widget,
172 GtkRequisition *requisition);
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 GQuark quark_angle = 0;
306 static GtkBuildableIface *buildable_parent_iface = NULL;
308 G_DEFINE_TYPE_WITH_CODE (GtkLabel, gtk_label, GTK_TYPE_MISC,
309 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
310 gtk_label_buildable_interface_init));
313 add_move_binding (GtkBindingSet *binding_set,
316 GtkMovementStep step,
319 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
321 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
325 G_TYPE_BOOLEAN, FALSE);
327 /* Selection-extending version */
328 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
332 G_TYPE_BOOLEAN, TRUE);
336 gtk_label_class_init (GtkLabelClass *class)
338 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
339 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
340 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
341 GtkBindingSet *binding_set;
343 quark_angle = g_quark_from_static_string ("angle");
345 gobject_class->set_property = gtk_label_set_property;
346 gobject_class->get_property = gtk_label_get_property;
347 gobject_class->finalize = gtk_label_finalize;
349 object_class->destroy = gtk_label_destroy;
351 widget_class->size_request = gtk_label_size_request;
352 widget_class->size_allocate = gtk_label_size_allocate;
353 widget_class->state_changed = gtk_label_state_changed;
354 widget_class->style_set = gtk_label_style_set;
355 widget_class->query_tooltip = gtk_label_query_tooltip;
356 widget_class->direction_changed = gtk_label_direction_changed;
357 widget_class->expose_event = gtk_label_expose;
358 widget_class->realize = gtk_label_realize;
359 widget_class->unrealize = gtk_label_unrealize;
360 widget_class->map = gtk_label_map;
361 widget_class->unmap = gtk_label_unmap;
362 widget_class->button_press_event = gtk_label_button_press;
363 widget_class->button_release_event = gtk_label_button_release;
364 widget_class->motion_notify_event = gtk_label_motion;
365 widget_class->leave_notify_event = gtk_label_leave_notify;
366 widget_class->hierarchy_changed = gtk_label_hierarchy_changed;
367 widget_class->screen_changed = gtk_label_screen_changed;
368 widget_class->mnemonic_activate = gtk_label_mnemonic_activate;
369 widget_class->drag_data_get = gtk_label_drag_data_get;
370 widget_class->grab_focus = gtk_label_grab_focus;
371 widget_class->popup_menu = gtk_label_popup_menu;
372 widget_class->focus = gtk_label_focus;
374 class->move_cursor = gtk_label_move_cursor;
375 class->copy_clipboard = gtk_label_copy_clipboard;
376 class->activate_link = gtk_label_activate_link;
379 * GtkLabel::move-cursor:
380 * @entry: the object which received the signal
381 * @step: the granularity of the move, as a #GtkMovementStep
382 * @count: the number of @step units to move
383 * @extend_selection: %TRUE if the move should extend the selection
385 * The ::move-cursor signal is a
386 * <link linkend="keybinding-signals">keybinding signal</link>
387 * which gets emitted when the user initiates a cursor movement.
388 * If the cursor is not visible in @entry, this signal causes
389 * the viewport to be moved instead.
391 * Applications should not connect to it, but may emit it with
392 * g_signal_emit_by_name() if they need to control the cursor
395 * The default bindings for this signal come in two variants,
396 * the variant with the Shift modifier extends the selection,
397 * the variant without the Shift modifer does not.
398 * There are too many key combinations to list them all here.
400 * <listitem>Arrow keys move by individual characters/lines</listitem>
401 * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
402 * <listitem>Home/End keys move to the ends of the buffer</listitem>
405 signals[MOVE_CURSOR] =
406 g_signal_new (I_("move-cursor"),
407 G_OBJECT_CLASS_TYPE (gobject_class),
408 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
409 G_STRUCT_OFFSET (GtkLabelClass, move_cursor),
411 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
413 GTK_TYPE_MOVEMENT_STEP,
418 * GtkLabel::copy-clipboard:
419 * @label: the object which received the signal
421 * The ::copy-clipboard signal is a
422 * <link linkend="keybinding-signals">keybinding signal</link>
423 * which gets emitted to copy the selection to the clipboard.
425 * The default binding for this signal is Ctrl-c.
427 signals[COPY_CLIPBOARD] =
428 g_signal_new (I_("copy-clipboard"),
429 G_OBJECT_CLASS_TYPE (gobject_class),
430 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
431 G_STRUCT_OFFSET (GtkLabelClass, copy_clipboard),
433 _gtk_marshal_VOID__VOID,
437 * GtkLabel::populate-popup:
438 * @label: The label on which the signal is emitted
439 * @menu: the menu that is being populated
441 * The ::populate-popup signal gets emitted before showing the
442 * context menu of the label. Note that only selectable labels
443 * have context menus.
445 * If you need to add items to the context menu, connect
446 * to this signal and append your menuitems to the @menu.
448 signals[POPULATE_POPUP] =
449 g_signal_new (I_("populate-popup"),
450 G_OBJECT_CLASS_TYPE (gobject_class),
452 G_STRUCT_OFFSET (GtkLabelClass, populate_popup),
454 _gtk_marshal_VOID__OBJECT,
459 * GtkLabel::activate-current-link:
460 * @label: The label on which the signal was emitted
462 * A <link linkend="keybinding-signals">keybinding signal</link>
463 * which gets emitted when the user activates a link in the label.
465 * Applications may also emit the signal with g_signal_emit_by_name()
466 * if they need to control activation of URIs programmatically.
468 * The default bindings for this signal are all forms of the Enter key.
472 signals[ACTIVATE_CURRENT_LINK] =
473 g_signal_new_class_handler ("activate-current-link",
474 G_TYPE_FROM_CLASS (object_class),
475 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
476 G_CALLBACK (gtk_label_activate_current_link),
478 _gtk_marshal_VOID__VOID,
482 * GtkLabel::activate-link:
483 * @label: The label on which the signal was emitted
484 * @uri: the URI that is activated
486 * The signal which gets emitted to activate a URI.
487 * Applications may connect to it to override the default behaviour,
488 * which is to call gtk_show_uri().
490 * Returns: %TRUE if the link has been activated
494 signals[ACTIVATE_LINK] =
495 g_signal_new ("activate-link",
496 G_TYPE_FROM_CLASS (object_class),
498 G_STRUCT_OFFSET (GtkLabelClass, activate_link),
499 _gtk_boolean_handled_accumulator, NULL,
500 _gtk_marshal_BOOLEAN__STRING,
501 G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
503 g_object_class_install_property (gobject_class,
505 g_param_spec_string ("label",
507 P_("The text of the label"),
509 GTK_PARAM_READWRITE));
510 g_object_class_install_property (gobject_class,
512 g_param_spec_boxed ("attributes",
514 P_("A list of style attributes to apply to the text of the label"),
515 PANGO_TYPE_ATTR_LIST,
516 GTK_PARAM_READWRITE));
517 g_object_class_install_property (gobject_class,
519 g_param_spec_boolean ("use-markup",
521 P_("The text of the label includes XML markup. See pango_parse_markup()"),
523 GTK_PARAM_READWRITE));
524 g_object_class_install_property (gobject_class,
526 g_param_spec_boolean ("use-underline",
528 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
530 GTK_PARAM_READWRITE));
532 g_object_class_install_property (gobject_class,
534 g_param_spec_enum ("justify",
536 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"),
537 GTK_TYPE_JUSTIFICATION,
539 GTK_PARAM_READWRITE));
541 g_object_class_install_property (gobject_class,
543 g_param_spec_string ("pattern",
545 P_("A string with _ characters in positions correspond to characters in the text to underline"),
547 GTK_PARAM_WRITABLE));
549 g_object_class_install_property (gobject_class,
551 g_param_spec_boolean ("wrap",
553 P_("If set, wrap lines if the text becomes too wide"),
555 GTK_PARAM_READWRITE));
557 * GtkLabel:wrap-mode:
559 * If line wrapping is on (see the #GtkLabel:wrap property) this controls
560 * how the line wrapping is done. The default is %PANGO_WRAP_WORD, which
561 * means wrap on word boundaries.
565 g_object_class_install_property (gobject_class,
567 g_param_spec_enum ("wrap-mode",
568 P_("Line wrap mode"),
569 P_("If wrap is set, controls how linewrapping is done"),
570 PANGO_TYPE_WRAP_MODE,
572 GTK_PARAM_READWRITE));
573 g_object_class_install_property (gobject_class,
575 g_param_spec_boolean ("selectable",
577 P_("Whether the label text can be selected with the mouse"),
579 GTK_PARAM_READWRITE));
580 g_object_class_install_property (gobject_class,
581 PROP_MNEMONIC_KEYVAL,
582 g_param_spec_uint ("mnemonic-keyval",
584 P_("The mnemonic accelerator key for this label"),
588 GTK_PARAM_READABLE));
589 g_object_class_install_property (gobject_class,
590 PROP_MNEMONIC_WIDGET,
591 g_param_spec_object ("mnemonic-widget",
592 P_("Mnemonic widget"),
593 P_("The widget to be activated when the label's mnemonic "
596 GTK_PARAM_READWRITE));
598 g_object_class_install_property (gobject_class,
599 PROP_CURSOR_POSITION,
600 g_param_spec_int ("cursor-position",
601 P_("Cursor Position"),
602 P_("The current position of the insertion cursor in chars"),
606 GTK_PARAM_READABLE));
608 g_object_class_install_property (gobject_class,
609 PROP_SELECTION_BOUND,
610 g_param_spec_int ("selection-bound",
611 P_("Selection Bound"),
612 P_("The position of the opposite end of the selection from the cursor in chars"),
616 GTK_PARAM_READABLE));
619 * GtkLabel:ellipsize:
621 * The preferred place to ellipsize the string, if the label does
622 * not have enough room to display the entire string, specified as a
623 * #PangoEllisizeMode.
625 * Note that setting this property to a value other than
626 * %PANGO_ELLIPSIZE_NONE has the side-effect that the label requests
627 * only enough space to display the ellipsis "...". In particular, this
628 * means that ellipsizing labels do not work well in notebook tabs, unless
629 * the tab's #GtkNotebook:tab-expand property is set to %TRUE. Other ways
630 * to set a label's width are gtk_widget_set_size_request() and
631 * gtk_label_set_width_chars().
635 g_object_class_install_property (gobject_class,
637 g_param_spec_enum ("ellipsize",
639 P_("The preferred place to ellipsize the string, if the label does not have enough room to display the entire string"),
640 PANGO_TYPE_ELLIPSIZE_MODE,
641 PANGO_ELLIPSIZE_NONE,
642 GTK_PARAM_READWRITE));
645 * GtkLabel:width-chars:
647 * The desired width of the label, in characters. If this property is set to
648 * -1, the width will be calculated automatically, otherwise the label will
649 * request either 3 characters or the property value, whichever is greater.
650 * If the "width-chars" property is set to a positive value, then the
651 * #GtkLabel:max-width-chars property is ignored.
655 g_object_class_install_property (gobject_class,
657 g_param_spec_int ("width-chars",
658 P_("Width In Characters"),
659 P_("The desired width of the label, in characters"),
663 GTK_PARAM_READWRITE));
666 * GtkLabel:single-line-mode:
668 * Whether the label is in single line mode. In single line mode,
669 * the height of the label does not depend on the actual text, it
670 * is always set to ascent + descent of the font. This can be an
671 * advantage in situations where resizing the label because of text
672 * changes would be distracting, e.g. in a statusbar.
676 g_object_class_install_property (gobject_class,
677 PROP_SINGLE_LINE_MODE,
678 g_param_spec_boolean ("single-line-mode",
679 P_("Single Line Mode"),
680 P_("Whether the label is in single line mode"),
682 GTK_PARAM_READWRITE));
687 * The angle that the baseline of the label makes with the horizontal,
688 * in degrees, measured counterclockwise. An angle of 90 reads from
689 * from bottom to top, an angle of 270, from top to bottom. Ignored
690 * if the label is selectable, wrapped, or ellipsized.
694 g_object_class_install_property (gobject_class,
696 g_param_spec_double ("angle",
698 P_("Angle at which the label is rotated"),
702 GTK_PARAM_READWRITE));
705 * GtkLabel:max-width-chars:
707 * The desired maximum width of the label, in characters. If this property
708 * is set to -1, the width will be calculated automatically, otherwise the
709 * label will request space for no more than the requested number of
710 * characters. If the #GtkLabel:width-chars property is set to a positive
711 * value, then the "max-width-chars" property is ignored.
715 g_object_class_install_property (gobject_class,
716 PROP_MAX_WIDTH_CHARS,
717 g_param_spec_int ("max-width-chars",
718 P_("Maximum Width In Characters"),
719 P_("The desired maximum width of the label, in characters"),
723 GTK_PARAM_READWRITE));
726 * GtkLabel:track-visited-links:
728 * Set this property to %TRUE to make the label track which links
729 * have been clicked. It will then apply the ::visited-link-color
730 * color, instead of ::link-color.
734 g_object_class_install_property (gobject_class,
735 PROP_TRACK_VISITED_LINKS,
736 g_param_spec_boolean ("track-visited-links",
737 P_("Track visited links"),
738 P_("Whether visited links should be tracked"),
740 GTK_PARAM_READWRITE));
745 binding_set = gtk_binding_set_by_class (class);
747 /* Moving the insertion point */
748 add_move_binding (binding_set, GDK_Right, 0,
749 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
751 add_move_binding (binding_set, GDK_Left, 0,
752 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
754 add_move_binding (binding_set, GDK_KP_Right, 0,
755 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
757 add_move_binding (binding_set, GDK_KP_Left, 0,
758 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
760 add_move_binding (binding_set, GDK_f, GDK_CONTROL_MASK,
761 GTK_MOVEMENT_LOGICAL_POSITIONS, 1);
763 add_move_binding (binding_set, GDK_b, GDK_CONTROL_MASK,
764 GTK_MOVEMENT_LOGICAL_POSITIONS, -1);
766 add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
767 GTK_MOVEMENT_WORDS, 1);
769 add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
770 GTK_MOVEMENT_WORDS, -1);
772 add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
773 GTK_MOVEMENT_WORDS, 1);
775 add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
776 GTK_MOVEMENT_WORDS, -1);
779 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
781 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
783 G_TYPE_BOOLEAN, FALSE);
785 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
787 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
789 G_TYPE_BOOLEAN, TRUE);
791 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
793 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
795 G_TYPE_BOOLEAN, FALSE);
797 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
799 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
801 G_TYPE_BOOLEAN, TRUE);
804 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
806 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
808 G_TYPE_BOOLEAN, FALSE);
810 gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_CONTROL_MASK,
812 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
814 G_TYPE_BOOLEAN, FALSE);
816 add_move_binding (binding_set, GDK_f, GDK_MOD1_MASK,
817 GTK_MOVEMENT_WORDS, 1);
819 add_move_binding (binding_set, GDK_b, GDK_MOD1_MASK,
820 GTK_MOVEMENT_WORDS, -1);
822 add_move_binding (binding_set, GDK_Home, 0,
823 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
825 add_move_binding (binding_set, GDK_End, 0,
826 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
828 add_move_binding (binding_set, GDK_KP_Home, 0,
829 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
831 add_move_binding (binding_set, GDK_KP_End, 0,
832 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
834 add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
835 GTK_MOVEMENT_BUFFER_ENDS, -1);
837 add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
838 GTK_MOVEMENT_BUFFER_ENDS, 1);
840 add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
841 GTK_MOVEMENT_BUFFER_ENDS, -1);
843 add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
844 GTK_MOVEMENT_BUFFER_ENDS, 1);
847 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
848 "copy-clipboard", 0);
850 gtk_binding_entry_add_signal (binding_set, GDK_Return, 0,
851 "activate-current-link", 0);
852 gtk_binding_entry_add_signal (binding_set, GDK_ISO_Enter, 0,
853 "activate-current-link", 0);
854 gtk_binding_entry_add_signal (binding_set, GDK_KP_Enter, 0,
855 "activate-current-link", 0);
857 gtk_settings_install_property (g_param_spec_boolean ("gtk-label-select-on-focus",
858 P_("Select on focus"),
859 P_("Whether to select the contents of a selectable label when it is focused"),
861 GTK_PARAM_READWRITE));
864 g_type_class_add_private (class, sizeof (GtkLabelPrivate));
868 gtk_label_set_property (GObject *object,
875 label = GTK_LABEL (object);
880 gtk_label_set_label (label, g_value_get_string (value));
882 case PROP_ATTRIBUTES:
883 gtk_label_set_attributes (label, g_value_get_boxed (value));
885 case PROP_USE_MARKUP:
886 gtk_label_set_use_markup (label, g_value_get_boolean (value));
888 case PROP_USE_UNDERLINE:
889 gtk_label_set_use_underline (label, g_value_get_boolean (value));
892 gtk_label_set_justify (label, g_value_get_enum (value));
895 gtk_label_set_pattern (label, g_value_get_string (value));
898 gtk_label_set_line_wrap (label, g_value_get_boolean (value));
901 gtk_label_set_line_wrap_mode (label, g_value_get_enum (value));
903 case PROP_SELECTABLE:
904 gtk_label_set_selectable (label, g_value_get_boolean (value));
906 case PROP_MNEMONIC_WIDGET:
907 gtk_label_set_mnemonic_widget (label, (GtkWidget*) g_value_get_object (value));
910 gtk_label_set_ellipsize (label, g_value_get_enum (value));
912 case PROP_WIDTH_CHARS:
913 gtk_label_set_width_chars (label, g_value_get_int (value));
915 case PROP_SINGLE_LINE_MODE:
916 gtk_label_set_single_line_mode (label, g_value_get_boolean (value));
919 gtk_label_set_angle (label, g_value_get_double (value));
921 case PROP_MAX_WIDTH_CHARS:
922 gtk_label_set_max_width_chars (label, g_value_get_int (value));
924 case PROP_TRACK_VISITED_LINKS:
925 gtk_label_set_track_visited_links (label, g_value_get_boolean (value));
928 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
934 gtk_label_get_property (GObject *object,
941 label = GTK_LABEL (object);
946 g_value_set_string (value, label->label);
948 case PROP_ATTRIBUTES:
949 g_value_set_boxed (value, label->attrs);
951 case PROP_USE_MARKUP:
952 g_value_set_boolean (value, label->use_markup);
954 case PROP_USE_UNDERLINE:
955 g_value_set_boolean (value, label->use_underline);
958 g_value_set_enum (value, label->jtype);
961 g_value_set_boolean (value, label->wrap);
964 g_value_set_enum (value, label->wrap_mode);
966 case PROP_SELECTABLE:
967 g_value_set_boolean (value, gtk_label_get_selectable (label));
969 case PROP_MNEMONIC_KEYVAL:
970 g_value_set_uint (value, label->mnemonic_keyval);
972 case PROP_MNEMONIC_WIDGET:
973 g_value_set_object (value, (GObject*) label->mnemonic_widget);
975 case PROP_CURSOR_POSITION:
976 if (label->select_info && label->select_info->selectable)
978 gint offset = g_utf8_pointer_to_offset (label->text,
979 label->text + label->select_info->selection_end);
980 g_value_set_int (value, offset);
983 g_value_set_int (value, 0);
985 case PROP_SELECTION_BOUND:
986 if (label->select_info && label->select_info->selectable)
988 gint offset = g_utf8_pointer_to_offset (label->text,
989 label->text + label->select_info->selection_anchor);
990 g_value_set_int (value, offset);
993 g_value_set_int (value, 0);
996 g_value_set_enum (value, label->ellipsize);
998 case PROP_WIDTH_CHARS:
999 g_value_set_int (value, gtk_label_get_width_chars (label));
1001 case PROP_SINGLE_LINE_MODE:
1002 g_value_set_boolean (value, gtk_label_get_single_line_mode (label));
1005 g_value_set_double (value, gtk_label_get_angle (label));
1007 case PROP_MAX_WIDTH_CHARS:
1008 g_value_set_int (value, gtk_label_get_max_width_chars (label));
1010 case PROP_TRACK_VISITED_LINKS:
1011 g_value_set_boolean (value, gtk_label_get_track_visited_links (label));
1014 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1020 gtk_label_init (GtkLabel *label)
1022 GtkLabelPrivate *priv;
1024 GTK_WIDGET_SET_FLAGS (label, GTK_NO_WINDOW);
1026 priv = GTK_LABEL_GET_PRIVATE (label);
1027 priv->width_chars = -1;
1028 priv->max_width_chars = -1;
1029 priv->wrap_width = -1;
1030 label->label = NULL;
1032 label->jtype = GTK_JUSTIFY_LEFT;
1033 label->wrap = FALSE;
1034 label->wrap_mode = PANGO_WRAP_WORD;
1035 label->ellipsize = PANGO_ELLIPSIZE_NONE;
1037 label->use_underline = FALSE;
1038 label->use_markup = FALSE;
1039 label->pattern_set = FALSE;
1040 label->track_links = TRUE;
1042 label->mnemonic_keyval = GDK_VoidSymbol;
1043 label->layout = NULL;
1045 label->attrs = NULL;
1047 label->mnemonic_widget = NULL;
1048 label->mnemonic_window = NULL;
1050 priv->mnemonics_visible = TRUE;
1052 gtk_label_set_text (label, "");
1057 gtk_label_buildable_interface_init (GtkBuildableIface *iface)
1059 buildable_parent_iface = g_type_interface_peek_parent (iface);
1061 iface->custom_tag_start = gtk_label_buildable_custom_tag_start;
1062 iface->custom_finished = gtk_label_buildable_custom_finished;
1066 GtkBuilder *builder;
1068 PangoAttrList *attrs;
1071 static PangoAttribute *
1072 attribute_from_text (GtkBuilder *builder,
1077 PangoAttribute *attribute = NULL;
1079 PangoLanguage *language;
1080 PangoFontDescription *font_desc;
1082 GValue val = { 0, };
1084 if (!gtk_builder_value_from_string_type (builder, PANGO_TYPE_ATTR_TYPE, name, &val, error))
1087 type = g_value_get_enum (&val);
1088 g_value_unset (&val);
1092 /* PangoAttrLanguage */
1093 case PANGO_ATTR_LANGUAGE:
1094 if ((language = pango_language_from_string (value)))
1096 attribute = pango_attr_language_new (language);
1097 g_value_init (&val, G_TYPE_INT);
1101 case PANGO_ATTR_STYLE:
1102 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STYLE, value, &val, error))
1103 attribute = pango_attr_style_new (g_value_get_enum (&val));
1105 case PANGO_ATTR_WEIGHT:
1106 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_WEIGHT, value, &val, error))
1107 attribute = pango_attr_weight_new (g_value_get_enum (&val));
1109 case PANGO_ATTR_VARIANT:
1110 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_VARIANT, value, &val, error))
1111 attribute = pango_attr_variant_new (g_value_get_enum (&val));
1113 case PANGO_ATTR_STRETCH:
1114 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STRETCH, value, &val, error))
1115 attribute = pango_attr_stretch_new (g_value_get_enum (&val));
1117 case PANGO_ATTR_UNDERLINE:
1118 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1119 attribute = pango_attr_underline_new (g_value_get_boolean (&val));
1121 case PANGO_ATTR_STRIKETHROUGH:
1122 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1123 attribute = pango_attr_strikethrough_new (g_value_get_boolean (&val));
1125 case PANGO_ATTR_GRAVITY:
1126 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY, value, &val, error))
1127 attribute = pango_attr_gravity_new (g_value_get_enum (&val));
1129 case PANGO_ATTR_GRAVITY_HINT:
1130 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY_HINT,
1131 value, &val, error))
1132 attribute = pango_attr_gravity_hint_new (g_value_get_enum (&val));
1135 /* PangoAttrString */
1136 case PANGO_ATTR_FAMILY:
1137 attribute = pango_attr_family_new (value);
1138 g_value_init (&val, G_TYPE_INT);
1142 case PANGO_ATTR_SIZE:
1143 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1144 value, &val, error))
1145 attribute = pango_attr_size_new (g_value_get_int (&val));
1147 case PANGO_ATTR_ABSOLUTE_SIZE:
1148 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
1149 value, &val, error))
1150 attribute = pango_attr_size_new_absolute (g_value_get_int (&val));
1153 /* PangoAttrFontDesc */
1154 case PANGO_ATTR_FONT_DESC:
1155 if ((font_desc = pango_font_description_from_string (value)))
1157 attribute = pango_attr_font_desc_new (font_desc);
1158 pango_font_description_free (font_desc);
1159 g_value_init (&val, G_TYPE_INT);
1163 /* PangoAttrColor */
1164 case PANGO_ATTR_FOREGROUND:
1165 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1166 value, &val, error))
1168 color = g_value_get_boxed (&val);
1169 attribute = pango_attr_foreground_new (color->red, color->green, color->blue);
1172 case PANGO_ATTR_BACKGROUND:
1173 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1174 value, &val, error))
1176 color = g_value_get_boxed (&val);
1177 attribute = pango_attr_background_new (color->red, color->green, color->blue);
1180 case PANGO_ATTR_UNDERLINE_COLOR:
1181 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1182 value, &val, error))
1184 color = g_value_get_boxed (&val);
1185 attribute = pango_attr_underline_color_new (color->red, color->green, color->blue);
1188 case PANGO_ATTR_STRIKETHROUGH_COLOR:
1189 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
1190 value, &val, error))
1192 color = g_value_get_boxed (&val);
1193 attribute = pango_attr_strikethrough_color_new (color->red, color->green, color->blue);
1197 /* PangoAttrShape */
1198 case PANGO_ATTR_SHAPE:
1199 /* Unsupported for now */
1201 /* PangoAttrFloat */
1202 case PANGO_ATTR_SCALE:
1203 if (gtk_builder_value_from_string_type (builder, G_TYPE_DOUBLE,
1204 value, &val, error))
1205 attribute = pango_attr_scale_new (g_value_get_double (&val));
1208 case PANGO_ATTR_INVALID:
1209 case PANGO_ATTR_LETTER_SPACING:
1210 case PANGO_ATTR_RISE:
1211 case PANGO_ATTR_FALLBACK:
1216 g_value_unset (&val);
1223 pango_start_element (GMarkupParseContext *context,
1224 const gchar *element_name,
1225 const gchar **names,
1226 const gchar **values,
1230 PangoParserData *data = (PangoParserData*)user_data;
1231 GValue val = { 0, };
1233 gint line_number, char_number;
1235 if (strcmp (element_name, "attribute") == 0)
1237 PangoAttribute *attr = NULL;
1238 const gchar *name = NULL;
1239 const gchar *value = NULL;
1240 const gchar *start = NULL;
1241 const gchar *end = NULL;
1242 guint start_val = 0;
1243 guint end_val = G_MAXUINT;
1245 for (i = 0; names[i]; i++)
1247 if (strcmp (names[i], "name") == 0)
1249 else if (strcmp (names[i], "value") == 0)
1251 else if (strcmp (names[i], "start") == 0)
1253 else if (strcmp (names[i], "end") == 0)
1257 g_markup_parse_context_get_position (context,
1262 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1263 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1265 line_number, char_number, names[i], "attribute");
1270 if (!name || !value)
1272 g_markup_parse_context_get_position (context,
1277 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1278 "%s:%d:%d <%s> requires attribute \"%s\"",
1280 line_number, char_number, "attribute",
1281 name ? "value" : "name");
1287 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1288 start, &val, error))
1290 start_val = g_value_get_uint (&val);
1291 g_value_unset (&val);
1296 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1299 end_val = g_value_get_uint (&val);
1300 g_value_unset (&val);
1303 attr = attribute_from_text (data->builder, name, value, error);
1304 attr->start_index = start_val;
1305 attr->end_index = end_val;
1310 data->attrs = pango_attr_list_new ();
1312 pango_attr_list_insert (data->attrs, attr);
1315 else if (strcmp (element_name, "attributes") == 0)
1318 g_warning ("Unsupported tag for GtkLabel: %s\n", element_name);
1321 static const GMarkupParser pango_parser =
1323 pango_start_element,
1327 gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
1328 GtkBuilder *builder,
1330 const gchar *tagname,
1331 GMarkupParser *parser,
1334 if (buildable_parent_iface->custom_tag_start (buildable, builder, child,
1335 tagname, parser, data))
1338 if (strcmp (tagname, "attributes") == 0)
1340 PangoParserData *parser_data;
1342 parser_data = g_slice_new0 (PangoParserData);
1343 parser_data->builder = g_object_ref (builder);
1344 parser_data->object = g_object_ref (buildable);
1345 *parser = pango_parser;
1346 *data = parser_data;
1353 gtk_label_buildable_custom_finished (GtkBuildable *buildable,
1354 GtkBuilder *builder,
1356 const gchar *tagname,
1359 PangoParserData *data;
1361 buildable_parent_iface->custom_finished (buildable, builder, child,
1362 tagname, user_data);
1364 if (strcmp (tagname, "attributes") == 0)
1366 data = (PangoParserData*)user_data;
1370 gtk_label_set_attributes (GTK_LABEL (buildable), data->attrs);
1371 pango_attr_list_unref (data->attrs);
1374 g_object_unref (data->object);
1375 g_object_unref (data->builder);
1376 g_slice_free (PangoParserData, data);
1383 * @str: The text of the label
1385 * Creates a new label with the given text inside it. You can
1386 * pass %NULL to get an empty label widget.
1388 * Return value: the new #GtkLabel
1391 gtk_label_new (const gchar *str)
1395 label = g_object_new (GTK_TYPE_LABEL, NULL);
1398 gtk_label_set_text (label, str);
1400 return GTK_WIDGET (label);
1404 * gtk_label_new_with_mnemonic:
1405 * @str: The text of the label, with an underscore in front of the
1406 * mnemonic character
1408 * Creates a new #GtkLabel, containing the text in @str.
1410 * If characters in @str are preceded by an underscore, they are
1411 * underlined. If you need a literal underscore character in a label, use
1412 * '__' (two underscores). The first underlined character represents a
1413 * keyboard accelerator called a mnemonic. The mnemonic key can be used
1414 * to activate another widget, chosen automatically, or explicitly using
1415 * gtk_label_set_mnemonic_widget().
1417 * If gtk_label_set_mnemonic_widget() is not called, then the first
1418 * activatable ancestor of the #GtkLabel will be chosen as the mnemonic
1419 * widget. For instance, if the label is inside a button or menu item,
1420 * the button or menu item will automatically become the mnemonic widget
1421 * and be activated by the mnemonic.
1423 * Return value: the new #GtkLabel
1426 gtk_label_new_with_mnemonic (const gchar *str)
1430 label = g_object_new (GTK_TYPE_LABEL, NULL);
1433 gtk_label_set_text_with_mnemonic (label, str);
1435 return GTK_WIDGET (label);
1439 gtk_label_mnemonic_activate (GtkWidget *widget,
1440 gboolean group_cycling)
1444 if (GTK_LABEL (widget)->mnemonic_widget)
1445 return gtk_widget_mnemonic_activate (GTK_LABEL (widget)->mnemonic_widget, group_cycling);
1447 /* Try to find the widget to activate by traversing the
1448 * widget's ancestry.
1450 parent = widget->parent;
1452 if (GTK_IS_NOTEBOOK (parent))
1457 if (gtk_widget_get_can_focus (parent) ||
1458 (!group_cycling && GTK_WIDGET_GET_CLASS (parent)->activate_signal) ||
1459 GTK_IS_NOTEBOOK (parent->parent) ||
1460 GTK_IS_MENU_ITEM (parent))
1461 return gtk_widget_mnemonic_activate (parent, group_cycling);
1462 parent = parent->parent;
1465 /* barf if there was nothing to activate */
1466 g_warning ("Couldn't find a target for a mnemonic activation.");
1467 gtk_widget_error_bell (widget);
1473 gtk_label_setup_mnemonic (GtkLabel *label,
1476 GtkWidget *widget = GTK_WIDGET (label);
1477 GtkWidget *toplevel;
1478 GtkWidget *mnemonic_menu;
1480 mnemonic_menu = g_object_get_data (G_OBJECT (label), "gtk-mnemonic-menu");
1482 if (last_key != GDK_VoidSymbol)
1484 if (label->mnemonic_window)
1486 gtk_window_remove_mnemonic (label->mnemonic_window,
1489 label->mnemonic_window = NULL;
1493 _gtk_menu_shell_remove_mnemonic (GTK_MENU_SHELL (mnemonic_menu),
1496 mnemonic_menu = NULL;
1500 if (label->mnemonic_keyval == GDK_VoidSymbol)
1503 connect_mnemonics_visible_notify (GTK_LABEL (widget));
1505 toplevel = gtk_widget_get_toplevel (widget);
1506 if (gtk_widget_is_toplevel (toplevel))
1508 GtkWidget *menu_shell;
1510 menu_shell = gtk_widget_get_ancestor (widget,
1511 GTK_TYPE_MENU_SHELL);
1515 _gtk_menu_shell_add_mnemonic (GTK_MENU_SHELL (menu_shell),
1516 label->mnemonic_keyval,
1518 mnemonic_menu = menu_shell;
1521 if (!GTK_IS_MENU (menu_shell))
1523 gtk_window_add_mnemonic (GTK_WINDOW (toplevel),
1524 label->mnemonic_keyval,
1526 label->mnemonic_window = GTK_WINDOW (toplevel);
1531 g_object_set_data (G_OBJECT (label), I_("gtk-mnemonic-menu"), mnemonic_menu);
1535 gtk_label_hierarchy_changed (GtkWidget *widget,
1536 GtkWidget *old_toplevel)
1538 GtkLabel *label = GTK_LABEL (widget);
1540 gtk_label_setup_mnemonic (label, label->mnemonic_keyval);
1544 label_shortcut_setting_apply (GtkLabel *label)
1546 gtk_label_recalculate (label);
1547 if (GTK_IS_ACCEL_LABEL (label))
1548 gtk_accel_label_refetch (GTK_ACCEL_LABEL (label));
1552 label_shortcut_setting_traverse_container (GtkWidget *widget,
1555 if (GTK_IS_LABEL (widget))
1556 label_shortcut_setting_apply (GTK_LABEL (widget));
1557 else if (GTK_IS_CONTAINER (widget))
1558 gtk_container_forall (GTK_CONTAINER (widget),
1559 label_shortcut_setting_traverse_container, data);
1563 label_shortcut_setting_changed (GtkSettings *settings)
1567 list = gtk_window_list_toplevels ();
1569 for (l = list; l ; l = l->next)
1571 GtkWidget *widget = l->data;
1573 if (gtk_widget_get_settings (widget) == settings)
1574 gtk_container_forall (GTK_CONTAINER (widget),
1575 label_shortcut_setting_traverse_container, NULL);
1582 mnemonics_visible_apply (GtkWidget *widget,
1583 gboolean mnemonics_visible)
1586 GtkLabelPrivate *priv;
1588 label = GTK_LABEL (widget);
1590 priv = GTK_LABEL_GET_PRIVATE (label);
1592 mnemonics_visible = mnemonics_visible != FALSE;
1594 if (priv->mnemonics_visible != mnemonics_visible)
1596 priv->mnemonics_visible = mnemonics_visible;
1598 gtk_label_recalculate (label);
1603 label_mnemonics_visible_traverse_container (GtkWidget *widget,
1606 gboolean mnemonics_visible = GPOINTER_TO_INT (data);
1608 _gtk_label_mnemonics_visible_apply_recursively (widget, mnemonics_visible);
1612 _gtk_label_mnemonics_visible_apply_recursively (GtkWidget *widget,
1613 gboolean mnemonics_visible)
1615 if (GTK_IS_LABEL (widget))
1616 mnemonics_visible_apply (widget, mnemonics_visible);
1617 else if (GTK_IS_CONTAINER (widget))
1618 gtk_container_forall (GTK_CONTAINER (widget),
1619 label_mnemonics_visible_traverse_container,
1620 GINT_TO_POINTER (mnemonics_visible));
1624 label_mnemonics_visible_changed (GtkWindow *window,
1628 gboolean mnemonics_visible;
1630 g_object_get (window, "mnemonics-visible", &mnemonics_visible, NULL);
1632 gtk_container_forall (GTK_CONTAINER (window),
1633 label_mnemonics_visible_traverse_container,
1634 GINT_TO_POINTER (mnemonics_visible));
1638 gtk_label_screen_changed (GtkWidget *widget,
1639 GdkScreen *old_screen)
1641 GtkSettings *settings;
1642 gboolean shortcuts_connected;
1644 if (!gtk_widget_has_screen (widget))
1647 settings = gtk_widget_get_settings (widget);
1649 shortcuts_connected =
1650 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (settings),
1651 "gtk-label-shortcuts-connected"));
1653 if (! shortcuts_connected)
1655 g_signal_connect (settings, "notify::gtk-enable-mnemonics",
1656 G_CALLBACK (label_shortcut_setting_changed),
1658 g_signal_connect (settings, "notify::gtk-enable-accels",
1659 G_CALLBACK (label_shortcut_setting_changed),
1662 g_object_set_data (G_OBJECT (settings), "gtk-label-shortcuts-connected",
1663 GINT_TO_POINTER (TRUE));
1666 label_shortcut_setting_apply (GTK_LABEL (widget));
1671 label_mnemonic_widget_weak_notify (gpointer data,
1672 GObject *where_the_object_was)
1674 GtkLabel *label = data;
1676 label->mnemonic_widget = NULL;
1677 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1681 * gtk_label_set_mnemonic_widget:
1682 * @label: a #GtkLabel
1683 * @widget: (allow-none): the target #GtkWidget
1685 * If the label has been set so that it has an mnemonic key (using
1686 * i.e. gtk_label_set_markup_with_mnemonic(),
1687 * gtk_label_set_text_with_mnemonic(), gtk_label_new_with_mnemonic()
1688 * or the "use_underline" property) the label can be associated with a
1689 * widget that is the target of the mnemonic. When the label is inside
1690 * a widget (like a #GtkButton or a #GtkNotebook tab) it is
1691 * automatically associated with the correct widget, but sometimes
1692 * (i.e. when the target is a #GtkEntry next to the label) you need to
1693 * set it explicitly using this function.
1695 * The target widget will be accelerated by emitting the
1696 * GtkWidget::mnemonic-activate signal on it. The default handler for
1697 * this signal will activate the widget if there are no mnemonic collisions
1698 * and toggle focus between the colliding widgets otherwise.
1701 gtk_label_set_mnemonic_widget (GtkLabel *label,
1704 g_return_if_fail (GTK_IS_LABEL (label));
1706 g_return_if_fail (GTK_IS_WIDGET (widget));
1708 if (label->mnemonic_widget)
1710 gtk_widget_remove_mnemonic_label (label->mnemonic_widget, GTK_WIDGET (label));
1711 g_object_weak_unref (G_OBJECT (label->mnemonic_widget),
1712 label_mnemonic_widget_weak_notify,
1715 label->mnemonic_widget = widget;
1716 if (label->mnemonic_widget)
1718 g_object_weak_ref (G_OBJECT (label->mnemonic_widget),
1719 label_mnemonic_widget_weak_notify,
1721 gtk_widget_add_mnemonic_label (label->mnemonic_widget, GTK_WIDGET (label));
1724 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1728 * gtk_label_get_mnemonic_widget:
1729 * @label: a #GtkLabel
1731 * Retrieves the target of the mnemonic (keyboard shortcut) of this
1732 * label. See gtk_label_set_mnemonic_widget().
1734 * Return value: the target of the label's mnemonic, or %NULL if none
1735 * has been set and the default algorithm will be used.
1738 gtk_label_get_mnemonic_widget (GtkLabel *label)
1740 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1742 return label->mnemonic_widget;
1746 * gtk_label_get_mnemonic_keyval:
1747 * @label: a #GtkLabel
1749 * If the label has been set so that it has an mnemonic key this function
1750 * returns the keyval used for the mnemonic accelerator. If there is no
1751 * mnemonic set up it returns #GDK_VoidSymbol.
1753 * Returns: GDK keyval usable for accelerators, or #GDK_VoidSymbol
1756 gtk_label_get_mnemonic_keyval (GtkLabel *label)
1758 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_VoidSymbol);
1760 return label->mnemonic_keyval;
1764 gtk_label_set_text_internal (GtkLabel *label,
1767 g_free (label->text);
1771 gtk_label_select_region_index (label, 0, 0);
1775 gtk_label_set_label_internal (GtkLabel *label,
1778 g_free (label->label);
1782 g_object_notify (G_OBJECT (label), "label");
1786 gtk_label_set_use_markup_internal (GtkLabel *label,
1790 if (label->use_markup != val)
1792 label->use_markup = val;
1794 g_object_notify (G_OBJECT (label), "use-markup");
1799 gtk_label_set_use_underline_internal (GtkLabel *label,
1803 if (label->use_underline != val)
1805 label->use_underline = val;
1807 g_object_notify (G_OBJECT (label), "use-underline");
1812 gtk_label_compose_effective_attrs (GtkLabel *label)
1814 PangoAttrIterator *iter;
1815 PangoAttribute *attr;
1816 GSList *iter_attrs, *l;
1820 if (label->effective_attrs)
1822 if ((iter = pango_attr_list_get_iterator (label->attrs)))
1826 iter_attrs = pango_attr_iterator_get_attrs (iter);
1827 for (l = iter_attrs; l; l = l->next)
1830 pango_attr_list_insert (label->effective_attrs, attr);
1832 g_slist_free (iter_attrs);
1834 while (pango_attr_iterator_next (iter));
1835 pango_attr_iterator_destroy (iter);
1839 label->effective_attrs =
1840 pango_attr_list_ref (label->attrs);
1845 gtk_label_set_attributes_internal (GtkLabel *label,
1846 PangoAttrList *attrs)
1849 pango_attr_list_ref (attrs);
1852 pango_attr_list_unref (label->attrs);
1853 label->attrs = attrs;
1855 g_object_notify (G_OBJECT (label), "attributes");
1859 /* Calculates text, attrs and mnemonic_keyval from
1860 * label, use_underline and use_markup
1863 gtk_label_recalculate (GtkLabel *label)
1865 guint keyval = label->mnemonic_keyval;
1867 if (label->use_markup)
1868 gtk_label_set_markup_internal (label, label->label, label->use_underline);
1871 if (label->use_underline)
1872 gtk_label_set_uline_text_internal (label, label->label);
1875 if (label->effective_attrs)
1876 pango_attr_list_unref (label->effective_attrs);
1877 label->effective_attrs = NULL;
1878 gtk_label_set_text_internal (label, g_strdup (label->label));
1882 gtk_label_compose_effective_attrs (label);
1884 if (!label->use_underline)
1885 label->mnemonic_keyval = GDK_VoidSymbol;
1887 if (keyval != label->mnemonic_keyval)
1889 gtk_label_setup_mnemonic (label, keyval);
1890 g_object_notify (G_OBJECT (label), "mnemonic-keyval");
1893 gtk_label_clear_layout (label);
1894 gtk_label_clear_select_info (label);
1895 gtk_widget_queue_resize (GTK_WIDGET (label));
1899 * gtk_label_set_text:
1900 * @label: a #GtkLabel
1901 * @str: The text you want to set
1903 * Sets the text within the #GtkLabel widget. It overwrites any text that
1906 * This will also clear any previously set mnemonic accelerators.
1909 gtk_label_set_text (GtkLabel *label,
1912 g_return_if_fail (GTK_IS_LABEL (label));
1914 g_object_freeze_notify (G_OBJECT (label));
1916 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
1917 gtk_label_set_use_markup_internal (label, FALSE);
1918 gtk_label_set_use_underline_internal (label, FALSE);
1920 gtk_label_recalculate (label);
1922 g_object_thaw_notify (G_OBJECT (label));
1926 * gtk_label_set_attributes:
1927 * @label: a #GtkLabel
1928 * @attrs: a #PangoAttrList
1930 * Sets a #PangoAttrList; the attributes in the list are applied to the
1933 * <note><para>The attributes set with this function will be applied
1934 * and merged with any other attributes previously effected by way
1935 * of the #GtkLabel:use-underline or #GtkLabel:use-markup properties.
1936 * While it is not recommended to mix markup strings with manually set
1937 * attributes, if you must; know that the attributes will be applied
1938 * to the label after the markup string is parsed.</para></note>
1941 gtk_label_set_attributes (GtkLabel *label,
1942 PangoAttrList *attrs)
1944 g_return_if_fail (GTK_IS_LABEL (label));
1946 gtk_label_set_attributes_internal (label, attrs);
1948 gtk_label_recalculate (label);
1950 gtk_label_clear_layout (label);
1951 gtk_widget_queue_resize (GTK_WIDGET (label));
1955 * gtk_label_get_attributes:
1956 * @label: a #GtkLabel
1958 * Gets the attribute list that was set on the label using
1959 * gtk_label_set_attributes(), if any. This function does
1960 * not reflect attributes that come from the labels markup
1961 * (see gtk_label_set_markup()). If you want to get the
1962 * effective attributes for the label, use
1963 * pango_layout_get_attribute (gtk_label_get_layout (label)).
1965 * Return value: the attribute list, or %NULL if none was set.
1968 gtk_label_get_attributes (GtkLabel *label)
1970 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1972 return label->attrs;
1976 * gtk_label_set_label:
1977 * @label: a #GtkLabel
1978 * @str: the new text to set for the label
1980 * Sets the text of the label. The label is interpreted as
1981 * including embedded underlines and/or Pango markup depending
1982 * on the values of the #GtkLabel:use-underline" and
1983 * #GtkLabel:use-markup properties.
1986 gtk_label_set_label (GtkLabel *label,
1989 g_return_if_fail (GTK_IS_LABEL (label));
1991 g_object_freeze_notify (G_OBJECT (label));
1993 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
1994 gtk_label_recalculate (label);
1996 g_object_thaw_notify (G_OBJECT (label));
2000 * gtk_label_get_label:
2001 * @label: a #GtkLabel
2003 * Fetches the text from a label widget including any embedded
2004 * underlines indicating mnemonics and Pango markup. (See
2005 * gtk_label_get_text()).
2007 * Return value: the text of the label widget. This string is
2008 * owned by the widget and must not be modified or freed.
2010 G_CONST_RETURN gchar *
2011 gtk_label_get_label (GtkLabel *label)
2013 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2015 return label->label;
2023 GdkColor *link_color;
2024 GdkColor *visited_link_color;
2028 start_element_handler (GMarkupParseContext *context,
2029 const gchar *element_name,
2030 const gchar **attribute_names,
2031 const gchar **attribute_values,
2035 UriParserData *pdata = user_data;
2037 if (strcmp (element_name, "a") == 0)
2040 const gchar *uri = NULL;
2041 const gchar *title = NULL;
2042 gboolean visited = FALSE;
2046 GdkColor *color = NULL;
2048 g_markup_parse_context_get_position (context, &line_number, &char_number);
2050 for (i = 0; attribute_names[i] != NULL; i++)
2052 const gchar *attr = attribute_names[i];
2054 if (strcmp (attr, "href") == 0)
2055 uri = attribute_values[i];
2056 else if (strcmp (attr, "title") == 0)
2057 title = attribute_values[i];
2062 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
2063 "Attribute '%s' is not allowed on the <a> tag "
2064 "on line %d char %d",
2065 attr, line_number, char_number);
2074 G_MARKUP_ERROR_INVALID_CONTENT,
2075 "Attribute 'href' was missing on the <a> tag "
2076 "on line %d char %d",
2077 line_number, char_number);
2082 if (pdata->label->track_links && pdata->label->select_info)
2085 for (l = pdata->label->select_info->links; l; l = l->next)
2088 if (strcmp (uri, link->uri) == 0)
2090 visited = link->visited;
2097 color = pdata->visited_link_color;
2099 color = pdata->link_color;
2101 g_string_append_printf (pdata->new_str,
2102 "<span color=\"#%04x%04x%04x\" underline=\"single\">",
2107 link = g_new0 (GtkLabelLink, 1);
2108 link->uri = g_strdup (uri);
2109 link->title = g_strdup (title);
2110 link->visited = visited;
2111 pdata->links = g_list_append (pdata->links, link);
2117 g_string_append_c (pdata->new_str, '<');
2118 g_string_append (pdata->new_str, element_name);
2120 for (i = 0; attribute_names[i] != NULL; i++)
2122 const gchar *attr = attribute_names[i];
2123 const gchar *value = attribute_values[i];
2126 newvalue = g_markup_escape_text (value, -1);
2128 g_string_append_c (pdata->new_str, ' ');
2129 g_string_append (pdata->new_str, attr);
2130 g_string_append (pdata->new_str, "=\"");
2131 g_string_append (pdata->new_str, newvalue);
2132 g_string_append_c (pdata->new_str, '\"');
2136 g_string_append_c (pdata->new_str, '>');
2141 end_element_handler (GMarkupParseContext *context,
2142 const gchar *element_name,
2146 UriParserData *pdata = user_data;
2148 if (!strcmp (element_name, "a"))
2149 g_string_append (pdata->new_str, "</span>");
2152 g_string_append (pdata->new_str, "</");
2153 g_string_append (pdata->new_str, element_name);
2154 g_string_append_c (pdata->new_str, '>');
2159 text_handler (GMarkupParseContext *context,
2165 UriParserData *pdata = user_data;
2168 newtext = g_markup_escape_text (text, text_len);
2169 g_string_append (pdata->new_str, newtext);
2173 static const GMarkupParser markup_parser =
2175 start_element_handler,
2176 end_element_handler,
2183 xml_isspace (gchar c)
2185 return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
2189 link_free (GtkLabelLink *link)
2192 g_free (link->title);
2197 gtk_label_get_link_colors (GtkWidget *widget,
2198 GdkColor **link_color,
2199 GdkColor **visited_link_color)
2201 gtk_widget_ensure_style (widget);
2202 gtk_widget_style_get (widget,
2203 "link-color", link_color,
2204 "visited-link-color", visited_link_color,
2207 *link_color = gdk_color_copy (&default_link_color);
2208 if (!*visited_link_color)
2209 *visited_link_color = gdk_color_copy (&default_visited_link_color);
2213 parse_uri_markup (GtkLabel *label,
2219 GMarkupParseContext *context = NULL;
2220 const gchar *p, *end;
2221 gboolean needs_root = TRUE;
2223 UriParserData pdata;
2225 length = strlen (str);
2229 pdata.label = label;
2231 pdata.new_str = g_string_sized_new (length);
2233 gtk_label_get_link_colors (GTK_WIDGET (label), &pdata.link_color, &pdata.visited_link_color);
2235 while (p != end && xml_isspace (*p))
2238 if (end - p >= 8 && strncmp (p, "<markup>", 8) == 0)
2241 context = g_markup_parse_context_new (&markup_parser, 0, &pdata, NULL);
2245 if (!g_markup_parse_context_parse (context, "<markup>", -1, error))
2249 if (!g_markup_parse_context_parse (context, str, length, error))
2254 if (!g_markup_parse_context_parse (context, "</markup>", -1, error))
2258 if (!g_markup_parse_context_end_parse (context, error))
2261 g_markup_parse_context_free (context);
2263 *new_str = g_string_free (pdata.new_str, FALSE);
2264 *links = pdata.links;
2266 gdk_color_free (pdata.link_color);
2267 gdk_color_free (pdata.visited_link_color);
2272 g_markup_parse_context_free (context);
2273 g_string_free (pdata.new_str, TRUE);
2274 g_list_foreach (pdata.links, (GFunc)link_free, NULL);
2275 g_list_free (pdata.links);
2276 gdk_color_free (pdata.link_color);
2277 gdk_color_free (pdata.visited_link_color);
2283 gtk_label_ensure_has_tooltip (GtkLabel *label)
2286 gboolean has_tooltip = FALSE;
2288 for (l = label->select_info->links; l; l = l->next)
2290 GtkLabelLink *link = l->data;
2298 gtk_widget_set_has_tooltip (GTK_WIDGET (label), has_tooltip);
2302 gtk_label_set_markup_internal (GtkLabel *label,
2304 gboolean with_uline)
2306 GtkLabelPrivate *priv = GTK_LABEL_GET_PRIVATE (label);
2308 GError *error = NULL;
2309 PangoAttrList *attrs = NULL;
2310 gunichar accel_char = 0;
2312 GList *links = NULL;
2314 if (!parse_uri_markup (label, str, &new_str, &links, &error))
2316 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2318 g_error_free (error);
2322 gtk_label_clear_links (label);
2325 gtk_label_ensure_select_info (label);
2326 label->select_info->links = links;
2327 gtk_label_ensure_has_tooltip (label);
2332 gboolean enable_mnemonics;
2333 gboolean auto_mnemonics;
2335 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2336 "gtk-enable-mnemonics", &enable_mnemonics,
2337 "gtk-auto-mnemonics", &auto_mnemonics,
2340 if (!(enable_mnemonics && priv->mnemonics_visible &&
2342 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2343 (!label->mnemonic_widget ||
2344 gtk_widget_is_sensitive (label->mnemonic_widget))))))
2350 if (separate_uline_pattern (new_str, &key, &tmp, &pattern))
2359 if (!pango_parse_markup (new_str,
2361 with_uline ? '_' : 0,
2364 with_uline ? &accel_char : NULL,
2367 g_warning ("Failed to set text from markup due to error parsing markup: %s",
2370 g_error_free (error);
2377 gtk_label_set_text_internal (label, text);
2381 if (label->effective_attrs)
2382 pango_attr_list_unref (label->effective_attrs);
2383 label->effective_attrs = attrs;
2386 if (accel_char != 0)
2387 label->mnemonic_keyval = gdk_keyval_to_lower (gdk_unicode_to_keyval (accel_char));
2389 label->mnemonic_keyval = GDK_VoidSymbol;
2393 * gtk_label_set_markup:
2394 * @label: a #GtkLabel
2395 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
2397 * Parses @str which is marked up with the <link
2398 * linkend="PangoMarkupFormat">Pango text markup language</link>, setting the
2399 * label's text and attribute list based on the parse results. If the @str is
2400 * external data, you may need to escape it with g_markup_escape_text() or
2401 * g_markup_printf_escaped()<!-- -->:
2405 * markup = g_markup_printf_escaped ("<span style=\"italic\">%s</span>", str);
2406 * gtk_label_set_markup (GTK_LABEL (label), markup);
2411 gtk_label_set_markup (GtkLabel *label,
2414 g_return_if_fail (GTK_IS_LABEL (label));
2416 g_object_freeze_notify (G_OBJECT (label));
2418 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2419 gtk_label_set_use_markup_internal (label, TRUE);
2420 gtk_label_set_use_underline_internal (label, FALSE);
2422 gtk_label_recalculate (label);
2424 g_object_thaw_notify (G_OBJECT (label));
2428 * gtk_label_set_markup_with_mnemonic:
2429 * @label: a #GtkLabel
2430 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
2432 * Parses @str which is marked up with the <link linkend="PangoMarkupFormat">Pango text markup language</link>,
2433 * setting the label's text and attribute list based on the parse results.
2434 * If characters in @str are preceded by an underscore, they are underlined
2435 * indicating that they represent a keyboard accelerator called a mnemonic.
2437 * The mnemonic key can be used to activate another widget, chosen
2438 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
2441 gtk_label_set_markup_with_mnemonic (GtkLabel *label,
2444 g_return_if_fail (GTK_IS_LABEL (label));
2446 g_object_freeze_notify (G_OBJECT (label));
2448 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2449 gtk_label_set_use_markup_internal (label, TRUE);
2450 gtk_label_set_use_underline_internal (label, TRUE);
2452 gtk_label_recalculate (label);
2454 g_object_thaw_notify (G_OBJECT (label));
2458 * gtk_label_get_text:
2459 * @label: a #GtkLabel
2461 * Fetches the text from a label widget, as displayed on the
2462 * screen. This does not include any embedded underlines
2463 * indicating mnemonics or Pango markup. (See gtk_label_get_label())
2465 * Return value: the text in the label widget. This is the internal
2466 * string used by the label, and must not be modified.
2468 G_CONST_RETURN gchar *
2469 gtk_label_get_text (GtkLabel *label)
2471 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2476 static PangoAttrList *
2477 gtk_label_pattern_to_attrs (GtkLabel *label,
2478 const gchar *pattern)
2481 const char *p = label->text;
2482 const char *q = pattern;
2483 PangoAttrList *attrs;
2485 attrs = pango_attr_list_new ();
2489 while (*p && *q && *q != '_')
2491 p = g_utf8_next_char (p);
2495 while (*p && *q && *q == '_')
2497 p = g_utf8_next_char (p);
2503 PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_LOW);
2504 attr->start_index = start - label->text;
2505 attr->end_index = p - label->text;
2507 pango_attr_list_insert (attrs, attr);
2517 gtk_label_set_pattern_internal (GtkLabel *label,
2518 const gchar *pattern,
2519 gboolean is_mnemonic)
2521 GtkLabelPrivate *priv = GTK_LABEL_GET_PRIVATE (label);
2522 PangoAttrList *attrs;
2523 gboolean enable_mnemonics;
2524 gboolean auto_mnemonics;
2526 g_return_if_fail (GTK_IS_LABEL (label));
2528 if (label->pattern_set)
2533 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2534 "gtk-enable-mnemonics", &enable_mnemonics,
2535 "gtk-auto-mnemonics", &auto_mnemonics,
2538 if (enable_mnemonics && priv->mnemonics_visible && pattern &&
2540 (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2541 (!label->mnemonic_widget ||
2542 gtk_widget_is_sensitive (label->mnemonic_widget)))))
2543 attrs = gtk_label_pattern_to_attrs (label, pattern);
2548 attrs = gtk_label_pattern_to_attrs (label, pattern);
2550 if (label->effective_attrs)
2551 pango_attr_list_unref (label->effective_attrs);
2552 label->effective_attrs = attrs;
2556 gtk_label_set_pattern (GtkLabel *label,
2557 const gchar *pattern)
2559 g_return_if_fail (GTK_IS_LABEL (label));
2561 label->pattern_set = FALSE;
2565 gtk_label_set_pattern_internal (label, pattern, FALSE);
2566 label->pattern_set = TRUE;
2569 gtk_label_recalculate (label);
2571 gtk_label_clear_layout (label);
2572 gtk_widget_queue_resize (GTK_WIDGET (label));
2577 * gtk_label_set_justify:
2578 * @label: a #GtkLabel
2579 * @jtype: a #GtkJustification
2581 * Sets the alignment of the lines in the text of the label relative to
2582 * each other. %GTK_JUSTIFY_LEFT is the default value when the
2583 * widget is first created with gtk_label_new(). If you instead want
2584 * to set the alignment of the label as a whole, use
2585 * gtk_misc_set_alignment() instead. gtk_label_set_justify() has no
2586 * effect on labels containing only a single line.
2589 gtk_label_set_justify (GtkLabel *label,
2590 GtkJustification jtype)
2592 g_return_if_fail (GTK_IS_LABEL (label));
2593 g_return_if_fail (jtype >= GTK_JUSTIFY_LEFT && jtype <= GTK_JUSTIFY_FILL);
2595 if ((GtkJustification) label->jtype != jtype)
2597 label->jtype = jtype;
2599 /* No real need to be this drastic, but easier than duplicating the code */
2600 gtk_label_clear_layout (label);
2602 g_object_notify (G_OBJECT (label), "justify");
2603 gtk_widget_queue_resize (GTK_WIDGET (label));
2608 * gtk_label_get_justify:
2609 * @label: a #GtkLabel
2611 * Returns the justification of the label. See gtk_label_set_justify().
2613 * Return value: #GtkJustification
2616 gtk_label_get_justify (GtkLabel *label)
2618 g_return_val_if_fail (GTK_IS_LABEL (label), 0);
2620 return label->jtype;
2624 * gtk_label_set_ellipsize:
2625 * @label: a #GtkLabel
2626 * @mode: a #PangoEllipsizeMode
2628 * Sets the mode used to ellipsize (add an ellipsis: "...") to the text
2629 * if there is not enough space to render the entire string.
2634 gtk_label_set_ellipsize (GtkLabel *label,
2635 PangoEllipsizeMode mode)
2637 g_return_if_fail (GTK_IS_LABEL (label));
2638 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE && mode <= PANGO_ELLIPSIZE_END);
2640 if ((PangoEllipsizeMode) label->ellipsize != mode)
2642 label->ellipsize = mode;
2644 /* No real need to be this drastic, but easier than duplicating the code */
2645 gtk_label_clear_layout (label);
2647 g_object_notify (G_OBJECT (label), "ellipsize");
2648 gtk_widget_queue_resize (GTK_WIDGET (label));
2653 * gtk_label_get_ellipsize:
2654 * @label: a #GtkLabel
2656 * Returns the ellipsizing position of the label. See gtk_label_set_ellipsize().
2658 * Return value: #PangoEllipsizeMode
2663 gtk_label_get_ellipsize (GtkLabel *label)
2665 g_return_val_if_fail (GTK_IS_LABEL (label), PANGO_ELLIPSIZE_NONE);
2667 return label->ellipsize;
2671 * gtk_label_set_width_chars:
2672 * @label: a #GtkLabel
2673 * @n_chars: the new desired width, in characters.
2675 * Sets the desired width in characters of @label to @n_chars.
2680 gtk_label_set_width_chars (GtkLabel *label,
2683 GtkLabelPrivate *priv;
2685 g_return_if_fail (GTK_IS_LABEL (label));
2687 priv = GTK_LABEL_GET_PRIVATE (label);
2689 if (priv->width_chars != n_chars)
2691 priv->width_chars = n_chars;
2692 g_object_notify (G_OBJECT (label), "width-chars");
2693 gtk_label_invalidate_wrap_width (label);
2694 gtk_widget_queue_resize (GTK_WIDGET (label));
2699 * gtk_label_get_width_chars:
2700 * @label: a #GtkLabel
2702 * Retrieves the desired width of @label, in characters. See
2703 * gtk_label_set_width_chars().
2705 * Return value: the width of the label in characters.
2710 gtk_label_get_width_chars (GtkLabel *label)
2712 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2714 return GTK_LABEL_GET_PRIVATE (label)->width_chars;
2718 * gtk_label_set_max_width_chars:
2719 * @label: a #GtkLabel
2720 * @n_chars: the new desired maximum width, in characters.
2722 * Sets the desired maximum width in characters of @label to @n_chars.
2727 gtk_label_set_max_width_chars (GtkLabel *label,
2730 GtkLabelPrivate *priv;
2732 g_return_if_fail (GTK_IS_LABEL (label));
2734 priv = GTK_LABEL_GET_PRIVATE (label);
2736 if (priv->max_width_chars != n_chars)
2738 priv->max_width_chars = n_chars;
2740 g_object_notify (G_OBJECT (label), "max-width-chars");
2741 gtk_label_invalidate_wrap_width (label);
2742 gtk_widget_queue_resize (GTK_WIDGET (label));
2747 * gtk_label_get_max_width_chars:
2748 * @label: a #GtkLabel
2750 * Retrieves the desired maximum width of @label, in characters. See
2751 * gtk_label_set_width_chars().
2753 * Return value: the maximum width of the label in characters.
2758 gtk_label_get_max_width_chars (GtkLabel *label)
2760 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2762 return GTK_LABEL_GET_PRIVATE (label)->max_width_chars;
2766 * gtk_label_set_line_wrap:
2767 * @label: a #GtkLabel
2768 * @wrap: the setting
2770 * Toggles line wrapping within the #GtkLabel widget. %TRUE makes it break
2771 * lines if text exceeds the widget's size. %FALSE lets the text get cut off
2772 * by the edge of the widget if it exceeds the widget size.
2774 * Note that setting line wrapping to %TRUE does not make the label
2775 * wrap at its parent container's width, because GTK+ widgets
2776 * conceptually can't make their requisition depend on the parent
2777 * container's size. For a label that wraps at a specific position,
2778 * set the label's width using gtk_widget_set_size_request().
2781 gtk_label_set_line_wrap (GtkLabel *label,
2784 g_return_if_fail (GTK_IS_LABEL (label));
2786 wrap = wrap != FALSE;
2788 if (label->wrap != wrap)
2792 gtk_label_clear_layout (label);
2793 gtk_widget_queue_resize (GTK_WIDGET (label));
2794 g_object_notify (G_OBJECT (label), "wrap");
2799 * gtk_label_get_line_wrap:
2800 * @label: a #GtkLabel
2802 * Returns whether lines in the label are automatically wrapped.
2803 * See gtk_label_set_line_wrap().
2805 * Return value: %TRUE if the lines of the label are automatically wrapped.
2808 gtk_label_get_line_wrap (GtkLabel *label)
2810 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
2816 * gtk_label_set_line_wrap_mode:
2817 * @label: a #GtkLabel
2818 * @wrap_mode: the line wrapping mode
2820 * If line wrapping is on (see gtk_label_set_line_wrap()) this controls how
2821 * the line wrapping is done. The default is %PANGO_WRAP_WORD which means
2822 * wrap on word boundaries.
2827 gtk_label_set_line_wrap_mode (GtkLabel *label,
2828 PangoWrapMode wrap_mode)
2830 g_return_if_fail (GTK_IS_LABEL (label));
2832 if (label->wrap_mode != wrap_mode)
2834 label->wrap_mode = wrap_mode;
2835 g_object_notify (G_OBJECT (label), "wrap-mode");
2837 gtk_widget_queue_resize (GTK_WIDGET (label));
2842 * gtk_label_get_line_wrap_mode:
2843 * @label: a #GtkLabel
2845 * Returns line wrap mode used by the label. See gtk_label_set_line_wrap_mode().
2847 * Return value: %TRUE if the lines of the label are automatically wrapped.
2852 gtk_label_get_line_wrap_mode (GtkLabel *label)
2854 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
2856 return label->wrap_mode;
2861 gtk_label_get (GtkLabel *label,
2864 g_return_if_fail (GTK_IS_LABEL (label));
2865 g_return_if_fail (str != NULL);
2871 gtk_label_destroy (GtkObject *object)
2873 GtkLabel *label = GTK_LABEL (object);
2875 gtk_label_set_mnemonic_widget (label, NULL);
2877 GTK_OBJECT_CLASS (gtk_label_parent_class)->destroy (object);
2881 gtk_label_finalize (GObject *object)
2883 GtkLabel *label = GTK_LABEL (object);
2885 g_free (label->label);
2886 g_free (label->text);
2889 g_object_unref (label->layout);
2892 pango_attr_list_unref (label->attrs);
2894 if (label->effective_attrs)
2895 pango_attr_list_unref (label->effective_attrs);
2897 gtk_label_clear_links (label);
2898 g_free (label->select_info);
2900 G_OBJECT_CLASS (gtk_label_parent_class)->finalize (object);
2904 gtk_label_clear_layout (GtkLabel *label)
2908 g_object_unref (label->layout);
2909 label->layout = NULL;
2911 //gtk_label_clear_links (label);
2916 get_label_char_width (GtkLabel *label)
2918 GtkLabelPrivate *priv;
2919 PangoContext *context;
2920 PangoFontMetrics *metrics;
2921 gint char_width, digit_width, char_pixels, w;
2923 priv = GTK_LABEL_GET_PRIVATE (label);
2925 context = pango_layout_get_context (label->layout);
2926 metrics = pango_context_get_metrics (context, GTK_WIDGET (label)->style->font_desc,
2927 pango_context_get_language (context));
2929 char_width = pango_font_metrics_get_approximate_char_width (metrics);
2930 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
2931 char_pixels = MAX (char_width, digit_width);
2932 pango_font_metrics_unref (metrics);
2934 if (priv->width_chars < 0)
2936 PangoRectangle rect;
2938 pango_layout_set_width (label->layout, -1);
2939 pango_layout_get_extents (label->layout, NULL, &rect);
2941 w = char_pixels * MAX (priv->max_width_chars, 3);
2942 w = MIN (rect.width, w);
2946 /* enforce minimum width for ellipsized labels at ~3 chars */
2947 w = char_pixels * MAX (priv->width_chars, 3);
2954 gtk_label_invalidate_wrap_width (GtkLabel *label)
2956 GtkLabelPrivate *priv;
2958 priv = GTK_LABEL_GET_PRIVATE (label);
2960 priv->wrap_width = -1;
2964 get_label_wrap_width (GtkLabel *label)
2966 GtkLabelPrivate *priv;
2968 priv = GTK_LABEL_GET_PRIVATE (label);
2970 if (priv->wrap_width < 0)
2972 if (priv->width_chars > 0 || priv->max_width_chars > 0)
2973 priv->wrap_width = get_label_char_width (label);
2976 PangoLayout *layout;
2978 layout = gtk_widget_create_pango_layout (GTK_WIDGET (label),
2979 "This long string gives a good enough length for any line to have.");
2980 pango_layout_get_size (layout, &priv->wrap_width, NULL);
2981 g_object_unref (layout);
2985 return priv->wrap_width;
2989 gtk_label_ensure_layout (GtkLabel *label)
2992 PangoRectangle logical_rect;
2995 widget = GTK_WIDGET (label);
2997 rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
3001 PangoAlignment align = PANGO_ALIGN_LEFT; /* Quiet gcc */
3002 gdouble angle = gtk_label_get_angle (label);
3004 if (angle != 0.0 && !label->wrap && !label->ellipsize && !label->select_info)
3006 /* We rotate the standard singleton PangoContext for the widget,
3007 * depending on the fact that it's meant pretty much exclusively
3010 PangoMatrix matrix = PANGO_MATRIX_INIT;
3012 pango_matrix_rotate (&matrix, angle);
3014 pango_context_set_matrix (gtk_widget_get_pango_context (widget), &matrix);
3016 label->have_transform = TRUE;
3020 if (label->have_transform)
3021 pango_context_set_matrix (gtk_widget_get_pango_context (widget), NULL);
3023 label->have_transform = FALSE;
3026 label->layout = gtk_widget_create_pango_layout (widget, label->text);
3028 if (label->effective_attrs)
3029 pango_layout_set_attributes (label->layout, label->effective_attrs);
3031 gtk_label_rescan_links (label);
3033 switch (label->jtype)
3035 case GTK_JUSTIFY_LEFT:
3036 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3038 case GTK_JUSTIFY_RIGHT:
3039 align = rtl ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
3041 case GTK_JUSTIFY_CENTER:
3042 align = PANGO_ALIGN_CENTER;
3044 case GTK_JUSTIFY_FILL:
3045 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3046 pango_layout_set_justify (label->layout, TRUE);
3049 g_assert_not_reached();
3052 pango_layout_set_alignment (label->layout, align);
3053 pango_layout_set_ellipsize (label->layout, label->ellipsize);
3054 pango_layout_set_single_paragraph_mode (label->layout, label->single_line_mode);
3056 if (label->ellipsize)
3057 pango_layout_set_width (label->layout,
3058 widget->allocation.width * PANGO_SCALE);
3059 else if (label->wrap)
3061 GtkWidgetAuxInfo *aux_info;
3062 gint longest_paragraph;
3065 pango_layout_set_wrap (label->layout, label->wrap_mode);
3067 aux_info = _gtk_widget_get_aux_info (widget, FALSE);
3068 if (aux_info && aux_info->width > 0)
3069 pango_layout_set_width (label->layout, aux_info->width * PANGO_SCALE);
3072 GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (label));
3075 pango_layout_set_width (label->layout, -1);
3076 pango_layout_get_extents (label->layout, NULL, &logical_rect);
3078 width = logical_rect.width;
3080 /* Try to guess a reasonable maximum width */
3081 longest_paragraph = width;
3083 wrap_width = get_label_wrap_width (label);
3084 width = MIN (width, wrap_width);
3086 PANGO_SCALE * (gdk_screen_get_width (screen) + 1) / 2);
3088 pango_layout_set_width (label->layout, width);
3089 pango_layout_get_extents (label->layout, NULL, &logical_rect);
3090 width = logical_rect.width;
3091 height = logical_rect.height;
3093 /* Unfortunately, the above may leave us with a very unbalanced looking paragraph,
3094 * so we try short search for a narrower width that leaves us with the same height
3096 if (longest_paragraph > 0)
3098 gint nlines, perfect_width;
3100 nlines = pango_layout_get_line_count (label->layout);
3101 perfect_width = (longest_paragraph + nlines - 1) / nlines;
3103 if (perfect_width < width)
3105 pango_layout_set_width (label->layout, perfect_width);
3106 pango_layout_get_extents (label->layout, NULL, &logical_rect);
3108 if (logical_rect.height <= height)
3109 width = logical_rect.width;
3112 gint mid_width = (perfect_width + width) / 2;
3114 if (mid_width > perfect_width)
3116 pango_layout_set_width (label->layout, mid_width);
3117 pango_layout_get_extents (label->layout, NULL, &logical_rect);
3119 if (logical_rect.height <= height)
3120 width = logical_rect.width;
3125 pango_layout_set_width (label->layout, width);
3128 else /* !label->wrap */
3129 pango_layout_set_width (label->layout, -1);
3134 gtk_label_size_request (GtkWidget *widget,
3135 GtkRequisition *requisition)
3137 GtkLabel *label = GTK_LABEL (widget);
3138 GtkLabelPrivate *priv;
3140 PangoRectangle logical_rect;
3141 GtkWidgetAuxInfo *aux_info;
3143 priv = GTK_LABEL_GET_PRIVATE (widget);
3146 * If word wrapping is on, then the height requisition can depend
3149 * - Any width set on the widget via gtk_widget_set_size_request().
3150 * - The padding of the widget (xpad, set by gtk_misc_set_padding)
3152 * Instead of trying to detect changes to these quantities, if we
3153 * are wrapping, we just rewrap for each size request. Since
3154 * size requisitions are cached by the GTK+ core, this is not
3159 gtk_label_clear_layout (label);
3161 gtk_label_ensure_layout (label);
3163 width = label->misc.xpad * 2;
3164 height = label->misc.ypad * 2;
3166 aux_info = _gtk_widget_get_aux_info (widget, FALSE);
3168 if (label->have_transform)
3170 PangoRectangle rect;
3171 PangoContext *context = pango_layout_get_context (label->layout);
3172 const PangoMatrix *matrix = pango_context_get_matrix (context);
3174 pango_layout_get_extents (label->layout, NULL, &rect);
3175 pango_matrix_transform_rectangle (matrix, &rect);
3176 pango_extents_to_pixels (&rect, NULL);
3178 requisition->width = width + rect.width;
3179 requisition->height = height + rect.height;
3184 pango_layout_get_extents (label->layout, NULL, &logical_rect);
3186 if ((label->wrap || label->ellipsize ||
3187 priv->width_chars > 0 || priv->max_width_chars > 0) &&
3188 aux_info && aux_info->width > 0)
3189 width += aux_info->width;
3190 else if (label->ellipsize || priv->width_chars > 0 || priv->max_width_chars > 0)
3192 width += PANGO_PIXELS (get_label_char_width (label));
3195 width += PANGO_PIXELS (logical_rect.width);
3197 if (label->single_line_mode)
3199 PangoContext *context;
3200 PangoFontMetrics *metrics;
3201 gint ascent, descent;
3203 context = pango_layout_get_context (label->layout);
3204 metrics = pango_context_get_metrics (context, widget->style->font_desc,
3205 pango_context_get_language (context));
3207 ascent = pango_font_metrics_get_ascent (metrics);
3208 descent = pango_font_metrics_get_descent (metrics);
3209 pango_font_metrics_unref (metrics);
3211 height += PANGO_PIXELS (ascent + descent);
3214 height += PANGO_PIXELS (logical_rect.height);
3216 requisition->width = width;
3217 requisition->height = height;
3221 gtk_label_size_allocate (GtkWidget *widget,
3222 GtkAllocation *allocation)
3226 label = GTK_LABEL (widget);
3228 GTK_WIDGET_CLASS (gtk_label_parent_class)->size_allocate (widget, allocation);
3230 if (label->ellipsize)
3235 PangoRectangle logical;
3237 width = (allocation->width - label->misc.xpad * 2) * PANGO_SCALE;
3239 pango_layout_set_width (label->layout, -1);
3240 pango_layout_get_extents (label->layout, NULL, &logical);
3242 if (logical.width > width)
3243 pango_layout_set_width (label->layout, width);
3247 if (label->select_info && label->select_info->window)
3249 gdk_window_move_resize (label->select_info->window,
3253 allocation->height);
3258 gtk_label_update_cursor (GtkLabel *label)
3260 if (!label->select_info)
3263 if (GTK_WIDGET_REALIZED (label))
3265 GdkDisplay *display;
3268 if (gtk_widget_is_sensitive (GTK_WIDGET (label)))
3270 display = gtk_widget_get_display (GTK_WIDGET (label));
3272 if (label->select_info->active_link)
3273 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
3274 else if (label->select_info->selectable)
3275 cursor = gdk_cursor_new_for_display (display, GDK_XTERM);
3282 gdk_window_set_cursor (label->select_info->window, cursor);
3285 gdk_cursor_unref (cursor);
3290 gtk_label_state_changed (GtkWidget *widget,
3291 GtkStateType prev_state)
3293 GtkLabel *label = GTK_LABEL (widget);
3295 if (label->select_info)
3297 gtk_label_select_region (label, 0, 0);
3298 gtk_label_update_cursor (label);
3301 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed)
3302 GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed (widget, prev_state);
3306 gtk_label_style_set (GtkWidget *widget,
3307 GtkStyle *previous_style)
3309 GtkLabel *label = GTK_LABEL (widget);
3311 /* We have to clear the layout, fonts etc. may have changed */
3312 gtk_label_clear_layout (label);
3313 gtk_label_invalidate_wrap_width (label);
3317 gtk_label_direction_changed (GtkWidget *widget,
3318 GtkTextDirection previous_dir)
3320 GtkLabel *label = GTK_LABEL (widget);
3323 pango_layout_context_changed (label->layout);
3325 GTK_WIDGET_CLASS (gtk_label_parent_class)->direction_changed (widget, previous_dir);
3329 get_layout_location (GtkLabel *label,
3335 GtkLabelPrivate *priv;
3337 gint req_width, x, y;
3338 PangoRectangle logical;
3340 misc = GTK_MISC (label);
3341 widget = GTK_WIDGET (label);
3342 priv = GTK_LABEL_GET_PRIVATE (label);
3344 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
3345 xalign = misc->xalign;
3347 xalign = 1.0 - misc->xalign;
3349 pango_layout_get_pixel_extents (label->layout, NULL, &logical);
3351 if (label->ellipsize || priv->width_chars > 0)
3355 width = pango_layout_get_width (label->layout);
3357 req_width = logical.width;
3359 req_width = MIN(PANGO_PIXELS (width), req_width);
3360 req_width += 2 * misc->xpad;
3363 req_width = widget->requisition.width;
3365 x = floor (widget->allocation.x + (gint)misc->xpad +
3366 xalign * (widget->allocation.width - req_width));
3368 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
3369 x = MAX (x, widget->allocation.x + misc->xpad);
3371 x = MIN (x, widget->allocation.x + widget->allocation.width - misc->xpad);
3374 /* bgo#315462 - For single-line labels, *do* align the requisition with
3375 * respect to the allocation, even if we are under-allocated. For multi-line
3376 * labels, always show the top of the text when they are under-allocated. The
3377 * rationale is this:
3379 * - Single-line labels appear in GtkButtons, and it is very easy to get them
3380 * to be smaller than their requisition. The button may clip the label, but
3381 * the label will still be able to show most of itself and the focus
3382 * rectangle. Also, it is fairly easy to read a single line of clipped text.
3384 * - Multi-line labels should not be clipped to showing "something in the
3385 * middle". You want to read the first line, at least, to get some context.
3387 if (pango_layout_get_line_count (label->layout) == 1)
3388 y = floor (widget->allocation.y + (gint)misc->ypad
3389 + (widget->allocation.height - widget->requisition.height) * misc->yalign);
3391 y = floor (widget->allocation.y + (gint)misc->ypad
3392 + MAX (((widget->allocation.height - widget->requisition.height) * misc->yalign),
3403 draw_insertion_cursor (GtkLabel *label,
3404 GdkRectangle *cursor_location,
3405 gboolean is_primary,
3406 PangoDirection direction,
3407 gboolean draw_arrow)
3409 GtkWidget *widget = GTK_WIDGET (label);
3410 GtkTextDirection text_dir;
3412 if (direction == PANGO_DIRECTION_LTR)
3413 text_dir = GTK_TEXT_DIR_LTR;
3415 text_dir = GTK_TEXT_DIR_RTL;
3417 gtk_draw_insertion_cursor (widget, widget->window, &(widget->allocation),
3419 is_primary, text_dir, draw_arrow);
3422 static PangoDirection
3423 get_cursor_direction (GtkLabel *label)
3427 g_assert (label->select_info);
3429 gtk_label_ensure_layout (label);
3431 for (l = pango_layout_get_lines_readonly (label->layout); l; l = l->next)
3433 PangoLayoutLine *line = l->data;
3435 /* If label->select_info->selection_end is at the very end of
3436 * the line, we don't know if the cursor is on this line or
3437 * the next without looking ahead at the next line. (End
3438 * of paragraph is different from line break.) But it's
3439 * definitely in this paragraph, which is good enough
3440 * to figure out the resolved direction.
3442 if (line->start_index + line->length >= label->select_info->selection_end)
3443 return line->resolved_dir;
3446 return PANGO_DIRECTION_LTR;
3450 gtk_label_draw_cursor (GtkLabel *label, gint xoffset, gint yoffset)
3454 if (label->select_info == NULL)
3457 widget = GTK_WIDGET (label);
3459 if (gtk_widget_is_drawable (widget))
3461 PangoDirection keymap_direction;
3462 PangoDirection cursor_direction;
3463 PangoRectangle strong_pos, weak_pos;
3464 gboolean split_cursor;
3465 PangoRectangle *cursor1 = NULL;
3466 PangoRectangle *cursor2 = NULL;
3467 GdkRectangle cursor_location;
3468 PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
3469 PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
3471 keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gtk_widget_get_display (widget)));
3472 cursor_direction = get_cursor_direction (label);
3474 gtk_label_ensure_layout (label);
3476 pango_layout_get_cursor_pos (label->layout, label->select_info->selection_end,
3477 &strong_pos, &weak_pos);
3479 g_object_get (gtk_widget_get_settings (widget),
3480 "gtk-split-cursor", &split_cursor,
3483 dir1 = cursor_direction;
3487 cursor1 = &strong_pos;
3489 if (strong_pos.x != weak_pos.x ||
3490 strong_pos.y != weak_pos.y)
3492 dir2 = (cursor_direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
3493 cursor2 = &weak_pos;
3498 if (keymap_direction == cursor_direction)
3499 cursor1 = &strong_pos;
3501 cursor1 = &weak_pos;
3504 cursor_location.x = xoffset + PANGO_PIXELS (cursor1->x);
3505 cursor_location.y = yoffset + PANGO_PIXELS (cursor1->y);
3506 cursor_location.width = 0;
3507 cursor_location.height = PANGO_PIXELS (cursor1->height);
3509 draw_insertion_cursor (label,
3510 &cursor_location, TRUE, dir1,
3511 dir2 != PANGO_DIRECTION_NEUTRAL);
3513 if (dir2 != PANGO_DIRECTION_NEUTRAL)
3515 cursor_location.x = xoffset + PANGO_PIXELS (cursor2->x);
3516 cursor_location.y = yoffset + PANGO_PIXELS (cursor2->y);
3517 cursor_location.width = 0;
3518 cursor_location.height = PANGO_PIXELS (cursor2->height);
3520 draw_insertion_cursor (label,
3521 &cursor_location, FALSE, dir2,
3527 static GtkLabelLink *
3528 gtk_label_get_focus_link (GtkLabel *label)
3530 GtkLabelSelectionInfo *info = label->select_info;
3536 if (info->selection_anchor != info->selection_end)
3539 for (l = info->links; l; l = l->next)
3541 GtkLabelLink *link = l->data;
3542 if (link->start <= info->selection_anchor &&
3543 info->selection_anchor <= link->end)
3551 gtk_label_expose (GtkWidget *widget,
3552 GdkEventExpose *event)
3554 GtkLabel *label = GTK_LABEL (widget);
3555 GtkLabelSelectionInfo *info = label->select_info;
3558 gtk_label_ensure_layout (label);
3560 if (gtk_widget_get_visible (widget) && GTK_WIDGET_MAPPED (widget) &&
3561 label->text && (*label->text != '\0'))
3563 get_layout_location (label, &x, &y);
3565 gtk_paint_layout (widget->style,
3567 GTK_WIDGET_STATE (widget),
3576 (info->selection_anchor != info->selection_end))
3582 range[0] = info->selection_anchor;
3583 range[1] = info->selection_end;
3585 if (range[0] > range[1])
3587 gint tmp = range[0];
3588 range[0] = range[1];
3592 clip = gdk_pango_layout_get_clip_region (label->layout,
3596 gdk_region_intersect (clip, event->region);
3598 /* FIXME should use gtk_paint, but it can't use a clip
3602 gdk_gc_set_clip_region (widget->style->black_gc, clip);
3605 state = GTK_STATE_SELECTED;
3606 if (!gtk_widget_has_focus (widget))
3607 state = GTK_STATE_ACTIVE;
3609 gdk_draw_layout_with_colors (widget->window,
3610 widget->style->black_gc,
3613 &widget->style->text[state],
3614 &widget->style->base[state]);
3616 gdk_gc_set_clip_region (widget->style->black_gc, NULL);
3617 gdk_region_destroy (clip);
3621 GtkLabelLink *focus_link;
3622 GtkLabelLink *active_link;
3626 GdkColor *text_color;
3627 GdkColor *base_color;
3628 GdkColor *link_color;
3629 GdkColor *visited_link_color;
3631 if (info->selectable && gtk_widget_has_focus (widget))
3632 gtk_label_draw_cursor (label, x, y);
3634 focus_link = gtk_label_get_focus_link (label);
3635 active_link = info->active_link;
3639 range[0] = active_link->start;
3640 range[1] = active_link->end;
3642 clip = gdk_pango_layout_get_clip_region (label->layout,
3646 gdk_gc_set_clip_region (widget->style->black_gc, clip);
3648 gtk_label_get_link_colors (widget, &link_color, &visited_link_color);
3649 if (active_link->visited)
3650 text_color = visited_link_color;
3652 text_color = link_color;
3653 if (info->link_clicked)
3654 base_color = &widget->style->base[GTK_STATE_ACTIVE];
3656 base_color = &widget->style->base[GTK_STATE_PRELIGHT];
3657 gdk_draw_layout_with_colors (widget->window,
3658 widget->style->black_gc,
3663 gdk_color_free (link_color);
3664 gdk_color_free (visited_link_color);
3666 gdk_gc_set_clip_region (widget->style->black_gc, NULL);
3667 gdk_region_destroy (clip);
3670 if (focus_link && gtk_widget_has_focus (widget))
3672 range[0] = focus_link->start;
3673 range[1] = focus_link->end;
3675 clip = gdk_pango_layout_get_clip_region (label->layout,
3679 gdk_region_get_clipbox (clip, &rect);
3681 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
3682 &event->area, widget, "label",
3683 rect.x, rect.y, rect.width, rect.height);
3685 gdk_region_destroy (clip);
3694 separate_uline_pattern (const gchar *str,
3699 gboolean underscore;
3702 gchar *pattern_dest;
3704 *accel_key = GDK_VoidSymbol;
3705 *new_str = g_new (gchar, strlen (str) + 1);
3706 *pattern = g_new (gchar, g_utf8_strlen (str, -1) + 1);
3712 pattern_dest = *pattern;
3717 const gchar *next_src;
3719 c = g_utf8_get_char (src);
3720 if (c == (gunichar)-1)
3722 g_warning ("Invalid input string");
3728 next_src = g_utf8_next_char (src);
3733 *pattern_dest++ = ' ';
3736 *pattern_dest++ = '_';
3737 if (*accel_key == GDK_VoidSymbol)
3738 *accel_key = gdk_keyval_to_lower (gdk_unicode_to_keyval (c));
3741 while (src < next_src)
3755 while (src < next_src)
3758 *pattern_dest++ = ' ';
3770 gtk_label_set_uline_text_internal (GtkLabel *label,
3773 guint accel_key = GDK_VoidSymbol;
3777 g_return_if_fail (GTK_IS_LABEL (label));
3778 g_return_if_fail (str != NULL);
3780 /* Split text into the base text and a separate pattern
3783 if (!separate_uline_pattern (str, &accel_key, &new_str, &pattern))
3786 gtk_label_set_text_internal (label, new_str);
3787 gtk_label_set_pattern_internal (label, pattern, TRUE);
3788 label->mnemonic_keyval = accel_key;
3794 gtk_label_parse_uline (GtkLabel *label,
3799 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_VoidSymbol);
3800 g_return_val_if_fail (str != NULL, GDK_VoidSymbol);
3802 g_object_freeze_notify (G_OBJECT (label));
3804 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
3805 gtk_label_set_use_markup_internal (label, FALSE);
3806 gtk_label_set_use_underline_internal (label, TRUE);
3808 gtk_label_recalculate (label);
3810 keyval = label->mnemonic_keyval;
3811 if (keyval != GDK_VoidSymbol)
3813 label->mnemonic_keyval = GDK_VoidSymbol;
3814 gtk_label_setup_mnemonic (label, keyval);
3815 g_object_notify (G_OBJECT (label), "mnemonic-keyval");
3818 g_object_thaw_notify (G_OBJECT (label));
3824 * gtk_label_set_text_with_mnemonic:
3825 * @label: a #GtkLabel
3828 * Sets the label's text from the string @str.
3829 * If characters in @str are preceded by an underscore, they are underlined
3830 * indicating that they represent a keyboard accelerator called a mnemonic.
3831 * The mnemonic key can be used to activate another widget, chosen
3832 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
3835 gtk_label_set_text_with_mnemonic (GtkLabel *label,
3838 g_return_if_fail (GTK_IS_LABEL (label));
3839 g_return_if_fail (str != NULL);
3841 g_object_freeze_notify (G_OBJECT (label));
3843 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
3844 gtk_label_set_use_markup_internal (label, FALSE);
3845 gtk_label_set_use_underline_internal (label, TRUE);
3847 gtk_label_recalculate (label);
3849 g_object_thaw_notify (G_OBJECT (label));
3853 gtk_label_realize (GtkWidget *widget)
3857 label = GTK_LABEL (widget);
3859 GTK_WIDGET_CLASS (gtk_label_parent_class)->realize (widget);
3861 if (label->select_info)
3862 gtk_label_create_window (label);
3866 gtk_label_unrealize (GtkWidget *widget)
3870 label = GTK_LABEL (widget);
3872 if (label->select_info)
3873 gtk_label_destroy_window (label);
3875 GTK_WIDGET_CLASS (gtk_label_parent_class)->unrealize (widget);
3879 gtk_label_map (GtkWidget *widget)
3883 label = GTK_LABEL (widget);
3885 GTK_WIDGET_CLASS (gtk_label_parent_class)->map (widget);
3887 if (label->select_info)
3888 gdk_window_show (label->select_info->window);
3892 gtk_label_unmap (GtkWidget *widget)
3896 label = GTK_LABEL (widget);
3898 if (label->select_info)
3899 gdk_window_hide (label->select_info->window);
3901 GTK_WIDGET_CLASS (gtk_label_parent_class)->unmap (widget);
3905 window_to_layout_coords (GtkLabel *label,
3912 widget = GTK_WIDGET (label);
3914 /* get layout location in widget->window coords */
3915 get_layout_location (label, &lx, &ly);
3919 *x += widget->allocation.x; /* go to widget->window */
3920 *x -= lx; /* go to layout */
3925 *y += widget->allocation.y; /* go to widget->window */
3926 *y -= ly; /* go to layout */
3932 layout_to_window_coords (GtkLabel *label,
3939 widget = GTK_WIDGET (label);
3941 /* get layout location in widget->window coords */
3942 get_layout_location (label, &lx, &ly);
3946 *x += lx; /* go to widget->window */
3947 *x -= widget->allocation.x; /* go to selection window */
3952 *y += ly; /* go to widget->window */
3953 *y -= widget->allocation.y; /* go to selection window */
3959 get_layout_index (GtkLabel *label,
3965 const gchar *cluster;
3966 const gchar *cluster_end;
3971 gtk_label_ensure_layout (label);
3973 window_to_layout_coords (label, &x, &y);
3978 inside = pango_layout_xy_to_index (label->layout,
3982 cluster = label->text + *index;
3983 cluster_end = cluster;
3986 cluster_end = g_utf8_next_char (cluster_end);
3990 *index += (cluster_end - cluster);
3996 gtk_label_select_word (GtkLabel *label)
4000 gint start_index = gtk_label_move_backward_word (label, label->select_info->selection_end);
4001 gint end_index = gtk_label_move_forward_word (label, label->select_info->selection_end);
4003 min = MIN (label->select_info->selection_anchor,
4004 label->select_info->selection_end);
4005 max = MAX (label->select_info->selection_anchor,
4006 label->select_info->selection_end);
4008 min = MIN (min, start_index);
4009 max = MAX (max, end_index);
4011 gtk_label_select_region_index (label, min, max);
4015 gtk_label_grab_focus (GtkWidget *widget)
4018 gboolean select_on_focus;
4021 label = GTK_LABEL (widget);
4023 if (label->select_info == NULL)
4026 GTK_WIDGET_CLASS (gtk_label_parent_class)->grab_focus (widget);
4028 if (label->select_info->selectable)
4030 g_object_get (gtk_widget_get_settings (widget),
4031 "gtk-label-select-on-focus",
4035 if (select_on_focus && !label->in_click)
4036 gtk_label_select_region (label, 0, -1);
4040 if (label->select_info->links && !label->in_click)
4042 link = label->select_info->links->data;
4043 label->select_info->selection_anchor = link->start;
4044 label->select_info->selection_end = link->start;
4050 gtk_label_focus (GtkWidget *widget,
4051 GtkDirectionType direction)
4053 GtkLabel *label = GTK_LABEL (widget);
4054 GtkLabelSelectionInfo *info = label->select_info;
4055 GtkLabelLink *focus_link;
4058 if (!gtk_widget_is_focus (widget))
4060 gtk_widget_grab_focus (widget);
4063 focus_link = gtk_label_get_focus_link (label);
4064 if (focus_link && direction == GTK_DIR_TAB_BACKWARD)
4066 l = g_list_last (info->links);
4067 focus_link = l->data;
4068 info->selection_anchor = focus_link->start;
4069 info->selection_end = focus_link->start;
4079 if (info->selectable)
4083 if (info->selection_anchor != info->selection_end)
4086 index = info->selection_anchor;
4088 if (direction == GTK_DIR_TAB_FORWARD)
4089 for (l = info->links; l; l = l->next)
4091 GtkLabelLink *link = l->data;
4093 if (link->start > index)
4095 gtk_label_select_region_index (label, link->start, link->start);
4099 else if (direction == GTK_DIR_TAB_BACKWARD)
4100 for (l = g_list_last (info->links); l; l = l->prev)
4102 GtkLabelLink *link = l->data;
4104 if (link->end < index)
4106 gtk_label_select_region_index (label, link->start, link->start);
4115 focus_link = gtk_label_get_focus_link (label);
4118 case GTK_DIR_TAB_FORWARD:
4121 l = g_list_find (info->links, focus_link);
4128 case GTK_DIR_TAB_BACKWARD:
4131 l = g_list_find (info->links, focus_link);
4135 l = g_list_last (info->links);
4144 focus_link = l->data;
4145 info->selection_anchor = focus_link->start;
4146 info->selection_end = focus_link->start;
4147 gtk_widget_queue_draw (widget);
4159 gtk_label_button_press (GtkWidget *widget,
4160 GdkEventButton *event)
4162 GtkLabel *label = GTK_LABEL (widget);
4163 GtkLabelSelectionInfo *info = label->select_info;
4170 if (info->active_link)
4172 if (event->button == 1)
4174 info->link_clicked = 1;
4175 gtk_widget_queue_draw (widget);
4177 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4179 info->link_clicked = 1;
4180 gtk_label_do_popup (label, event);
4185 if (!info->selectable)
4188 info->in_drag = FALSE;
4189 info->select_words = FALSE;
4191 if (event->button == 1)
4193 if (!gtk_widget_has_focus (widget))
4195 label->in_click = TRUE;
4196 gtk_widget_grab_focus (widget);
4197 label->in_click = FALSE;
4200 if (event->type == GDK_3BUTTON_PRESS)
4202 gtk_label_select_region_index (label, 0, strlen (label->text));
4206 if (event->type == GDK_2BUTTON_PRESS)
4208 info->select_words = TRUE;
4209 gtk_label_select_word (label);
4213 get_layout_index (label, event->x, event->y, &index);
4215 min = MIN (info->selection_anchor, info->selection_end);
4216 max = MAX (info->selection_anchor, info->selection_end);
4218 if ((info->selection_anchor != info->selection_end) &&
4219 (event->state & GDK_SHIFT_MASK))
4221 /* extend (same as motion) */
4222 min = MIN (min, index);
4223 max = MAX (max, index);
4225 /* ensure the anchor is opposite index */
4233 gtk_label_select_region_index (label, min, max);
4237 if (event->type == GDK_3BUTTON_PRESS)
4238 gtk_label_select_region_index (label, 0, strlen (label->text));
4239 else if (event->type == GDK_2BUTTON_PRESS)
4240 gtk_label_select_word (label);
4241 else if (min < max && min <= index && index <= max)
4243 info->in_drag = TRUE;
4244 info->drag_start_x = event->x;
4245 info->drag_start_y = event->y;
4248 /* start a replacement */
4249 gtk_label_select_region_index (label, index, index);
4254 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4256 gtk_label_do_popup (label, event);
4264 gtk_label_button_release (GtkWidget *widget,
4265 GdkEventButton *event)
4268 GtkLabel *label = GTK_LABEL (widget);
4269 GtkLabelSelectionInfo *info = label->select_info;
4279 get_layout_index (label, event->x, event->y, &index);
4280 gtk_label_select_region_index (label, index, index);
4285 if (event->button != 1)
4288 if (info->active_link &&
4289 info->selection_anchor == info->selection_end &&
4292 emit_activate_link (label, info->active_link);
4293 info->link_clicked = 0;
4298 /* The goal here is to return TRUE iff we ate the
4299 * button press to start selecting.
4305 connect_mnemonics_visible_notify (GtkLabel *label)
4307 GtkLabelPrivate *priv = GTK_LABEL_GET_PRIVATE (label);
4308 GtkWidget *toplevel;
4311 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
4313 if (!GTK_IS_WINDOW (toplevel))
4316 /* always set up this widgets initial value */
4317 priv->mnemonics_visible =
4318 gtk_window_get_mnemonics_visible (GTK_WINDOW (toplevel));
4321 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (toplevel),
4322 "gtk-label-mnemonics-visible-connected"));
4326 g_signal_connect (toplevel,
4327 "notify::mnemonics-visible",
4328 G_CALLBACK (label_mnemonics_visible_changed),
4330 g_object_set_data (G_OBJECT (toplevel),
4331 "gtk-label-mnemonics-visible-connected",
4332 GINT_TO_POINTER (1));
4337 drag_begin_cb (GtkWidget *widget,
4338 GdkDragContext *context,
4342 GdkPixmap *pixmap = NULL;
4344 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
4346 label = GTK_LABEL (widget);
4348 if ((label->select_info->selection_anchor !=
4349 label->select_info->selection_end) &&
4355 start = MIN (label->select_info->selection_anchor,
4356 label->select_info->selection_end);
4357 end = MAX (label->select_info->selection_anchor,
4358 label->select_info->selection_end);
4360 len = strlen (label->text);
4368 pixmap = _gtk_text_util_create_drag_icon (widget,
4369 label->text + start,
4374 gtk_drag_set_icon_pixmap (context,
4375 gdk_drawable_get_colormap (pixmap),
4380 gtk_drag_set_icon_default (context);
4383 g_object_unref (pixmap);
4387 gtk_label_motion (GtkWidget *widget,
4388 GdkEventMotion *event)
4390 GtkLabel *label = GTK_LABEL (widget);
4391 GtkLabelSelectionInfo *info = label->select_info;
4398 if (info->links && !info->in_drag)
4402 gboolean found = FALSE;
4404 if (info->selection_anchor == info->selection_end)
4406 gdk_window_get_pointer (event->window, &x, &y, NULL);
4407 if (get_layout_index (label, x, y, &index))
4409 for (l = info->links; l != NULL; l = l->next)
4412 if (index >= link->start && index <= link->end)
4423 if (info->active_link != link)
4425 info->link_clicked = 0;
4426 info->active_link = link;
4427 gtk_label_update_cursor (label);
4428 gtk_widget_queue_draw (widget);
4433 if (info->active_link != NULL)
4435 info->link_clicked = 0;
4436 info->active_link = NULL;
4437 gtk_label_update_cursor (label);
4438 gtk_widget_queue_draw (widget);
4443 if (!info->selectable)
4446 if ((event->state & GDK_BUTTON1_MASK) == 0)
4449 gdk_window_get_pointer (info->window, &x, &y, NULL);
4453 if (gtk_drag_check_threshold (widget,
4456 event->x, event->y))
4458 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
4460 gtk_target_list_add_text_targets (target_list, 0);
4462 g_signal_connect (widget, "drag-begin",
4463 G_CALLBACK (drag_begin_cb), NULL);
4464 gtk_drag_begin (widget, target_list,
4466 1, (GdkEvent *)event);
4468 info->in_drag = FALSE;
4470 gtk_target_list_unref (target_list);
4475 get_layout_index (label, x, y, &index);
4477 if (info->select_words)
4480 gint old_min, old_max;
4483 min = gtk_label_move_backward_word (label, index);
4484 max = gtk_label_move_forward_word (label, index);
4486 anchor = info->selection_anchor;
4487 end = info->selection_end;
4489 old_min = MIN (anchor, end);
4490 old_max = MAX (anchor, end);
4497 else if (old_max < max)
4502 else if (anchor == old_min)
4513 gtk_label_select_region_index (label, anchor, end);
4516 gtk_label_select_region_index (label, info->selection_anchor, index);
4523 gtk_label_leave_notify (GtkWidget *widget,
4524 GdkEventCrossing *event)
4526 GtkLabel *label = GTK_LABEL (widget);
4528 if (label->select_info)
4530 label->select_info->active_link = NULL;
4531 gtk_label_update_cursor (label);
4532 gtk_widget_queue_draw (widget);
4535 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event)
4536 return GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event (widget, event);
4542 gtk_label_create_window (GtkLabel *label)
4545 GdkWindowAttr attributes;
4546 gint attributes_mask;
4548 g_assert (label->select_info);
4549 g_assert (GTK_WIDGET_REALIZED (label));
4551 if (label->select_info->window)
4554 widget = GTK_WIDGET (label);
4556 attributes.x = widget->allocation.x;
4557 attributes.y = widget->allocation.y;
4558 attributes.width = widget->allocation.width;
4559 attributes.height = widget->allocation.height;
4560 attributes.window_type = GDK_WINDOW_CHILD;
4561 attributes.wclass = GDK_INPUT_ONLY;
4562 attributes.override_redirect = TRUE;
4563 attributes.event_mask = gtk_widget_get_events (widget) |
4564 GDK_BUTTON_PRESS_MASK |
4565 GDK_BUTTON_RELEASE_MASK |
4566 GDK_LEAVE_NOTIFY_MASK |
4567 GDK_BUTTON_MOTION_MASK |
4568 GDK_POINTER_MOTION_MASK |
4569 GDK_POINTER_MOTION_HINT_MASK;
4570 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
4571 if (gtk_widget_is_sensitive (widget))
4573 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
4575 attributes_mask |= GDK_WA_CURSOR;
4579 label->select_info->window = gdk_window_new (widget->window,
4580 &attributes, attributes_mask);
4581 gdk_window_set_user_data (label->select_info->window, widget);
4583 if (attributes_mask & GDK_WA_CURSOR)
4584 gdk_cursor_unref (attributes.cursor);
4588 gtk_label_destroy_window (GtkLabel *label)
4590 g_assert (label->select_info);
4592 if (label->select_info->window == NULL)
4595 gdk_window_set_user_data (label->select_info->window, NULL);
4596 gdk_window_destroy (label->select_info->window);
4597 label->select_info->window = NULL;
4601 gtk_label_ensure_select_info (GtkLabel *label)
4603 if (label->select_info == NULL)
4605 label->select_info = g_new0 (GtkLabelSelectionInfo, 1);
4607 GTK_WIDGET_SET_FLAGS (label, GTK_CAN_FOCUS);
4609 if (GTK_WIDGET_REALIZED (label))
4610 gtk_label_create_window (label);
4612 if (GTK_WIDGET_MAPPED (label))
4613 gdk_window_show (label->select_info->window);
4618 gtk_label_clear_select_info (GtkLabel *label)
4620 if (label->select_info == NULL)
4623 if (!label->select_info->selectable && !label->select_info->links)
4625 gtk_label_destroy_window (label);
4627 g_free (label->select_info);
4628 label->select_info = NULL;
4630 GTK_WIDGET_UNSET_FLAGS (label, GTK_CAN_FOCUS);
4635 * gtk_label_set_selectable:
4636 * @label: a #GtkLabel
4637 * @setting: %TRUE to allow selecting text in the label
4639 * Selectable labels allow the user to select text from the label, for
4643 gtk_label_set_selectable (GtkLabel *label,
4646 gboolean old_setting;
4648 g_return_if_fail (GTK_IS_LABEL (label));
4650 setting = setting != FALSE;
4651 old_setting = label->select_info && label->select_info->selectable;
4655 gtk_label_ensure_select_info (label);
4656 label->select_info->selectable = TRUE;
4657 gtk_label_update_cursor (label);
4663 /* unselect, to give up the selection */
4664 gtk_label_select_region (label, 0, 0);
4666 label->select_info->selectable = FALSE;
4667 gtk_label_clear_select_info (label);
4668 gtk_label_update_cursor (label);
4671 if (setting != old_setting)
4673 g_object_freeze_notify (G_OBJECT (label));
4674 g_object_notify (G_OBJECT (label), "selectable");
4675 g_object_notify (G_OBJECT (label), "cursor-position");
4676 g_object_notify (G_OBJECT (label), "selection-bound");
4677 g_object_thaw_notify (G_OBJECT (label));
4678 gtk_widget_queue_draw (GTK_WIDGET (label));
4683 * gtk_label_get_selectable:
4684 * @label: a #GtkLabel
4686 * Gets the value set by gtk_label_set_selectable().
4688 * Return value: %TRUE if the user can copy text from the label
4691 gtk_label_get_selectable (GtkLabel *label)
4693 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
4695 return label->select_info && label->select_info->selectable;
4699 free_angle (gpointer angle)
4701 g_slice_free (gdouble, angle);
4705 * gtk_label_set_angle:
4706 * @label: a #GtkLabel
4707 * @angle: the angle that the baseline of the label makes with
4708 * the horizontal, in degrees, measured counterclockwise
4710 * Sets the angle of rotation for the label. An angle of 90 reads from
4711 * from bottom to top, an angle of 270, from top to bottom. The angle
4712 * setting for the label is ignored if the label is selectable,
4713 * wrapped, or ellipsized.
4718 gtk_label_set_angle (GtkLabel *label,
4721 gdouble *label_angle;
4723 g_return_if_fail (GTK_IS_LABEL (label));
4725 label_angle = (gdouble *)g_object_get_qdata (G_OBJECT (label), quark_angle);
4729 label_angle = g_slice_new (gdouble);
4731 g_object_set_qdata_full (G_OBJECT (label), quark_angle,
4732 label_angle, free_angle);
4735 /* Canonicalize to [0,360]. We don't canonicalize 360 to 0, because
4736 * double property ranges are inclusive, and changing 360 to 0 would
4737 * make a property editor behave strangely.
4739 if (angle < 0 || angle > 360.0)
4740 angle = angle - 360. * floor (angle / 360.);
4742 if (*label_angle != angle)
4744 *label_angle = angle;
4746 gtk_label_clear_layout (label);
4747 gtk_widget_queue_resize (GTK_WIDGET (label));
4749 g_object_notify (G_OBJECT (label), "angle");
4754 * gtk_label_get_angle:
4755 * @label: a #GtkLabel
4757 * Gets the angle of rotation for the label. See
4758 * gtk_label_set_angle().
4760 * Return value: the angle of rotation for the label
4765 gtk_label_get_angle (GtkLabel *label)
4769 g_return_val_if_fail (GTK_IS_LABEL (label), 0.0);
4771 angle = (gdouble *)g_object_get_qdata (G_OBJECT (label), quark_angle);
4780 gtk_label_set_selection_text (GtkLabel *label,
4781 GtkSelectionData *selection_data)
4783 if ((label->select_info->selection_anchor !=
4784 label->select_info->selection_end) &&
4790 start = MIN (label->select_info->selection_anchor,
4791 label->select_info->selection_end);
4792 end = MAX (label->select_info->selection_anchor,
4793 label->select_info->selection_end);
4795 len = strlen (label->text);
4803 gtk_selection_data_set_text (selection_data,
4804 label->text + start,
4810 gtk_label_drag_data_get (GtkWidget *widget,
4811 GdkDragContext *context,
4812 GtkSelectionData *selection_data,
4816 gtk_label_set_selection_text (GTK_LABEL (widget), selection_data);
4820 get_text_callback (GtkClipboard *clipboard,
4821 GtkSelectionData *selection_data,
4823 gpointer user_data_or_owner)
4825 gtk_label_set_selection_text (GTK_LABEL (user_data_or_owner), selection_data);
4829 clear_text_callback (GtkClipboard *clipboard,
4830 gpointer user_data_or_owner)
4834 label = GTK_LABEL (user_data_or_owner);
4836 if (label->select_info)
4838 label->select_info->selection_anchor = label->select_info->selection_end;
4840 gtk_widget_queue_draw (GTK_WIDGET (label));
4845 gtk_label_select_region_index (GtkLabel *label,
4849 g_return_if_fail (GTK_IS_LABEL (label));
4851 if (label->select_info && label->select_info->selectable)
4853 GtkClipboard *clipboard;
4855 if (label->select_info->selection_anchor == anchor_index &&
4856 label->select_info->selection_end == end_index)
4859 label->select_info->selection_anchor = anchor_index;
4860 label->select_info->selection_end = end_index;
4862 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label),
4863 GDK_SELECTION_PRIMARY);
4865 if (anchor_index != end_index)
4867 GtkTargetList *list;
4868 GtkTargetEntry *targets;
4871 list = gtk_target_list_new (NULL, 0);
4872 gtk_target_list_add_text_targets (list, 0);
4873 targets = gtk_target_table_new_from_list (list, &n_targets);
4875 gtk_clipboard_set_with_owner (clipboard,
4878 clear_text_callback,
4881 gtk_target_table_free (targets, n_targets);
4882 gtk_target_list_unref (list);
4886 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (label))
4887 gtk_clipboard_clear (clipboard);
4890 gtk_widget_queue_draw (GTK_WIDGET (label));
4892 g_object_freeze_notify (G_OBJECT (label));
4893 g_object_notify (G_OBJECT (label), "cursor-position");
4894 g_object_notify (G_OBJECT (label), "selection-bound");
4895 g_object_thaw_notify (G_OBJECT (label));
4900 * gtk_label_select_region:
4901 * @label: a #GtkLabel
4902 * @start_offset: start offset (in characters not bytes)
4903 * @end_offset: end offset (in characters not bytes)
4905 * Selects a range of characters in the label, if the label is selectable.
4906 * See gtk_label_set_selectable(). If the label is not selectable,
4907 * this function has no effect. If @start_offset or
4908 * @end_offset are -1, then the end of the label will be substituted.
4911 gtk_label_select_region (GtkLabel *label,
4915 g_return_if_fail (GTK_IS_LABEL (label));
4917 if (label->text && label->select_info)
4919 if (start_offset < 0)
4920 start_offset = g_utf8_strlen (label->text, -1);
4923 end_offset = g_utf8_strlen (label->text, -1);
4925 gtk_label_select_region_index (label,
4926 g_utf8_offset_to_pointer (label->text, start_offset) - label->text,
4927 g_utf8_offset_to_pointer (label->text, end_offset) - label->text);
4932 * gtk_label_get_selection_bounds:
4933 * @label: a #GtkLabel
4934 * @start: return location for start of selection, as a character offset
4935 * @end: return location for end of selection, as a character offset
4937 * Gets the selected range of characters in the label, returning %TRUE
4938 * if there's a selection.
4940 * Return value: %TRUE if selection is non-empty
4943 gtk_label_get_selection_bounds (GtkLabel *label,
4947 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
4949 if (label->select_info == NULL)
4951 /* not a selectable label */
4961 gint start_index, end_index;
4962 gint start_offset, end_offset;
4965 start_index = MIN (label->select_info->selection_anchor,
4966 label->select_info->selection_end);
4967 end_index = MAX (label->select_info->selection_anchor,
4968 label->select_info->selection_end);
4970 len = strlen (label->text);
4972 if (end_index > len)
4975 if (start_index > len)
4978 start_offset = g_utf8_strlen (label->text, start_index);
4979 end_offset = g_utf8_strlen (label->text, end_index);
4981 if (start_offset > end_offset)
4983 gint tmp = start_offset;
4984 start_offset = end_offset;
4989 *start = start_offset;
4994 return start_offset != end_offset;
5000 * gtk_label_get_layout:
5001 * @label: a #GtkLabel
5003 * Gets the #PangoLayout used to display the label.
5004 * The layout is useful to e.g. convert text positions to
5005 * pixel positions, in combination with gtk_label_get_layout_offsets().
5006 * The returned layout is owned by the label so need not be
5007 * freed by the caller.
5009 * Return value: (transfer none): the #PangoLayout for this label
5012 gtk_label_get_layout (GtkLabel *label)
5014 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
5016 gtk_label_ensure_layout (label);
5018 return label->layout;
5022 * gtk_label_get_layout_offsets:
5023 * @label: a #GtkLabel
5024 * @x: (allow-none): location to store X offset of layout, or %NULL
5025 * @y: (allow-none): location to store Y offset of layout, or %NULL
5027 * Obtains the coordinates where the label will draw the #PangoLayout
5028 * representing the text in the label; useful to convert mouse events
5029 * into coordinates inside the #PangoLayout, e.g. to take some action
5030 * if some part of the label is clicked. Of course you will need to
5031 * create a #GtkEventBox to receive the events, and pack the label
5032 * inside it, since labels are a #GTK_NO_WINDOW widget. Remember
5033 * when using the #PangoLayout functions you need to convert to
5034 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
5037 gtk_label_get_layout_offsets (GtkLabel *label,
5041 g_return_if_fail (GTK_IS_LABEL (label));
5043 gtk_label_ensure_layout (label);
5045 get_layout_location (label, x, y);
5049 * gtk_label_set_use_markup:
5050 * @label: a #GtkLabel
5051 * @setting: %TRUE if the label's text should be parsed for markup.
5053 * Sets whether the text of the label contains markup in <link
5054 * linkend="PangoMarkupFormat">Pango's text markup
5055 * language</link>. See gtk_label_set_markup().
5058 gtk_label_set_use_markup (GtkLabel *label,
5061 g_return_if_fail (GTK_IS_LABEL (label));
5063 gtk_label_set_use_markup_internal (label, setting);
5064 gtk_label_recalculate (label);
5068 * gtk_label_get_use_markup:
5069 * @label: a #GtkLabel
5071 * Returns whether the label's text is interpreted as marked up with
5072 * the <link linkend="PangoMarkupFormat">Pango text markup
5073 * language</link>. See gtk_label_set_use_markup ().
5075 * Return value: %TRUE if the label's text will be parsed for markup.
5078 gtk_label_get_use_markup (GtkLabel *label)
5080 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5082 return label->use_markup;
5086 * gtk_label_set_use_underline:
5087 * @label: a #GtkLabel
5088 * @setting: %TRUE if underlines in the text indicate mnemonics
5090 * If true, an underline in the text indicates the next character should be
5091 * used for the mnemonic accelerator key.
5094 gtk_label_set_use_underline (GtkLabel *label,
5097 g_return_if_fail (GTK_IS_LABEL (label));
5099 gtk_label_set_use_underline_internal (label, setting);
5100 gtk_label_recalculate (label);
5104 * gtk_label_get_use_underline:
5105 * @label: a #GtkLabel
5107 * Returns whether an embedded underline in the label indicates a
5108 * mnemonic. See gtk_label_set_use_underline().
5110 * Return value: %TRUE whether an embedded underline in the label indicates
5111 * the mnemonic accelerator keys.
5114 gtk_label_get_use_underline (GtkLabel *label)
5116 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5118 return label->use_underline;
5122 * gtk_label_set_single_line_mode:
5123 * @label: a #GtkLabel
5124 * @single_line_mode: %TRUE if the label should be in single line mode
5126 * Sets whether the label is in single line mode.
5131 gtk_label_set_single_line_mode (GtkLabel *label,
5132 gboolean single_line_mode)
5134 g_return_if_fail (GTK_IS_LABEL (label));
5136 single_line_mode = single_line_mode != FALSE;
5138 if (label->single_line_mode != single_line_mode)
5140 label->single_line_mode = single_line_mode;
5142 gtk_label_clear_layout (label);
5143 gtk_widget_queue_resize (GTK_WIDGET (label));
5145 g_object_notify (G_OBJECT (label), "single-line-mode");
5150 * gtk_label_get_single_line_mode:
5151 * @label: a #GtkLabel
5153 * Returns whether the label is in single line mode.
5155 * Return value: %TRUE when the label is in single line mode.
5160 gtk_label_get_single_line_mode (GtkLabel *label)
5162 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5164 return label->single_line_mode;
5167 /* Compute the X position for an offset that corresponds to the "more important
5168 * cursor position for that offset. We use this when trying to guess to which
5169 * end of the selection we should go to when the user hits the left or
5173 get_better_cursor (GtkLabel *label,
5178 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5179 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5180 PangoDirection cursor_direction = get_cursor_direction (label);
5181 gboolean split_cursor;
5182 PangoRectangle strong_pos, weak_pos;
5184 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5185 "gtk-split-cursor", &split_cursor,
5188 gtk_label_ensure_layout (label);
5190 pango_layout_get_cursor_pos (label->layout, index,
5191 &strong_pos, &weak_pos);
5195 *x = strong_pos.x / PANGO_SCALE;
5196 *y = strong_pos.y / PANGO_SCALE;
5200 if (keymap_direction == cursor_direction)
5202 *x = strong_pos.x / PANGO_SCALE;
5203 *y = strong_pos.y / PANGO_SCALE;
5207 *x = weak_pos.x / PANGO_SCALE;
5208 *y = weak_pos.y / PANGO_SCALE;
5215 gtk_label_move_logically (GtkLabel *label,
5219 gint offset = g_utf8_pointer_to_offset (label->text,
5220 label->text + start);
5224 PangoLogAttr *log_attrs;
5228 gtk_label_ensure_layout (label);
5230 length = g_utf8_strlen (label->text, -1);
5232 pango_layout_get_log_attrs (label->layout, &log_attrs, &n_attrs);
5234 while (count > 0 && offset < length)
5238 while (offset < length && !log_attrs[offset].is_cursor_position);
5242 while (count < 0 && offset > 0)
5246 while (offset > 0 && !log_attrs[offset].is_cursor_position);
5254 return g_utf8_offset_to_pointer (label->text, offset) - label->text;
5258 gtk_label_move_visually (GtkLabel *label,
5268 int new_index, new_trailing;
5269 gboolean split_cursor;
5272 gtk_label_ensure_layout (label);
5274 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5275 "gtk-split-cursor", &split_cursor,
5282 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5283 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5285 strong = keymap_direction == get_cursor_direction (label);
5290 pango_layout_move_cursor_visually (label->layout, strong, index, 0, 1, &new_index, &new_trailing);
5295 pango_layout_move_cursor_visually (label->layout, strong, index, 0, -1, &new_index, &new_trailing);
5299 if (new_index < 0 || new_index == G_MAXINT)
5304 while (new_trailing--)
5305 index = g_utf8_next_char (label->text + new_index) - label->text;
5312 gtk_label_move_forward_word (GtkLabel *label,
5315 gint new_pos = g_utf8_pointer_to_offset (label->text,
5316 label->text + start);
5319 length = g_utf8_strlen (label->text, -1);
5320 if (new_pos < length)
5322 PangoLogAttr *log_attrs;
5325 gtk_label_ensure_layout (label);
5327 pango_layout_get_log_attrs (label->layout, &log_attrs, &n_attrs);
5329 /* Find the next word end */
5331 while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
5337 return g_utf8_offset_to_pointer (label->text, new_pos) - label->text;
5342 gtk_label_move_backward_word (GtkLabel *label,
5345 gint new_pos = g_utf8_pointer_to_offset (label->text,
5346 label->text + start);
5350 PangoLogAttr *log_attrs;
5353 gtk_label_ensure_layout (label);
5355 pango_layout_get_log_attrs (label->layout, &log_attrs, &n_attrs);
5359 /* Find the previous word beginning */
5360 while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
5366 return g_utf8_offset_to_pointer (label->text, new_pos) - label->text;
5370 gtk_label_move_cursor (GtkLabel *label,
5371 GtkMovementStep step,
5373 gboolean extend_selection)
5378 if (label->select_info == NULL)
5381 old_pos = new_pos = label->select_info->selection_end;
5383 if (label->select_info->selection_end != label->select_info->selection_anchor &&
5386 /* If we have a current selection and aren't extending it, move to the
5387 * start/or end of the selection as appropriate
5391 case GTK_MOVEMENT_VISUAL_POSITIONS:
5394 gint anchor_x, anchor_y;
5395 gboolean end_is_left;
5397 get_better_cursor (label, label->select_info->selection_end, &end_x, &end_y);
5398 get_better_cursor (label, label->select_info->selection_anchor, &anchor_x, &anchor_y);
5400 end_is_left = (end_y < anchor_y) || (end_y == anchor_y && end_x < anchor_x);
5403 new_pos = end_is_left ? label->select_info->selection_end : label->select_info->selection_anchor;
5405 new_pos = !end_is_left ? label->select_info->selection_end : label->select_info->selection_anchor;
5408 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5409 case GTK_MOVEMENT_WORDS:
5411 new_pos = MIN (label->select_info->selection_end, label->select_info->selection_anchor);
5413 new_pos = MAX (label->select_info->selection_end, label->select_info->selection_anchor);
5415 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5416 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5417 case GTK_MOVEMENT_BUFFER_ENDS:
5418 /* FIXME: Can do better here */
5419 new_pos = count < 0 ? 0 : strlen (label->text);
5421 case GTK_MOVEMENT_DISPLAY_LINES:
5422 case GTK_MOVEMENT_PARAGRAPHS:
5423 case GTK_MOVEMENT_PAGES:
5424 case GTK_MOVEMENT_HORIZONTAL_PAGES:
5432 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5433 new_pos = gtk_label_move_logically (label, new_pos, count);
5435 case GTK_MOVEMENT_VISUAL_POSITIONS:
5436 new_pos = gtk_label_move_visually (label, new_pos, count);
5437 if (new_pos == old_pos)
5439 if (!extend_selection)
5441 if (!gtk_widget_keynav_failed (GTK_WIDGET (label),
5443 GTK_DIR_RIGHT : GTK_DIR_LEFT))
5445 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
5448 gtk_widget_child_focus (toplevel,
5450 GTK_DIR_RIGHT : GTK_DIR_LEFT);
5455 gtk_widget_error_bell (GTK_WIDGET (label));
5459 case GTK_MOVEMENT_WORDS:
5462 new_pos = gtk_label_move_forward_word (label, new_pos);
5467 new_pos = gtk_label_move_backward_word (label, new_pos);
5470 if (new_pos == old_pos)
5471 gtk_widget_error_bell (GTK_WIDGET (label));
5473 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5474 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5475 case GTK_MOVEMENT_BUFFER_ENDS:
5476 /* FIXME: Can do better here */
5477 new_pos = count < 0 ? 0 : strlen (label->text);
5478 if (new_pos == old_pos)
5479 gtk_widget_error_bell (GTK_WIDGET (label));
5481 case GTK_MOVEMENT_DISPLAY_LINES:
5482 case GTK_MOVEMENT_PARAGRAPHS:
5483 case GTK_MOVEMENT_PAGES:
5484 case GTK_MOVEMENT_HORIZONTAL_PAGES:
5489 if (extend_selection)
5490 gtk_label_select_region_index (label,
5491 label->select_info->selection_anchor,
5494 gtk_label_select_region_index (label, new_pos, new_pos);
5498 gtk_label_copy_clipboard (GtkLabel *label)
5500 if (label->text && label->select_info)
5504 GtkClipboard *clipboard;
5506 start = MIN (label->select_info->selection_anchor,
5507 label->select_info->selection_end);
5508 end = MAX (label->select_info->selection_anchor,
5509 label->select_info->selection_end);
5511 len = strlen (label->text);
5519 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
5522 gtk_clipboard_set_text (clipboard, label->text + start, end - start);
5527 link = gtk_label_get_focus_link (label);
5529 gtk_clipboard_set_text (clipboard, link->uri, -1);
5535 gtk_label_select_all (GtkLabel *label)
5537 gtk_label_select_region_index (label, 0, strlen (label->text));
5540 /* Quick hack of a popup menu
5543 activate_cb (GtkWidget *menuitem,
5546 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
5547 g_signal_emit_by_name (label, signal);
5551 append_action_signal (GtkLabel *label,
5553 const gchar *stock_id,
5554 const gchar *signal,
5557 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
5559 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
5560 g_signal_connect (menuitem, "activate",
5561 G_CALLBACK (activate_cb), label);
5563 gtk_widget_set_sensitive (menuitem, sensitive);
5565 gtk_widget_show (menuitem);
5566 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5570 popup_menu_detach (GtkWidget *attach_widget,
5573 GtkLabel *label = GTK_LABEL (attach_widget);
5575 if (label->select_info)
5576 label->select_info->popup_menu = NULL;
5580 popup_position_func (GtkMenu *menu,
5591 label = GTK_LABEL (user_data);
5592 widget = GTK_WIDGET (label);
5594 g_return_if_fail (GTK_WIDGET_REALIZED (label));
5596 screen = gtk_widget_get_screen (widget);
5597 gdk_window_get_origin (widget->window, x, y);
5599 *x += widget->allocation.x;
5600 *y += widget->allocation.y;
5602 gtk_widget_size_request (GTK_WIDGET (menu), &req);
5604 *x += widget->allocation.width / 2;
5605 *y += widget->allocation.height;
5607 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
5608 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
5612 open_link_activate_cb (GtkMenuItem *menu_item,
5617 link = gtk_label_get_current_link (label);
5620 emit_activate_link (label, link);
5624 copy_link_activate_cb (GtkMenuItem *menu_item,
5627 GtkClipboard *clipboard;
5630 uri = gtk_label_get_current_uri (label);
5633 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
5634 gtk_clipboard_set_text (clipboard, uri, -1);
5639 gtk_label_popup_menu (GtkWidget *widget)
5641 gtk_label_do_popup (GTK_LABEL (widget), NULL);
5647 gtk_label_do_popup (GtkLabel *label,
5648 GdkEventButton *event)
5650 GtkWidget *menuitem;
5653 gboolean have_selection;
5656 if (!label->select_info)
5659 if (label->select_info->popup_menu)
5660 gtk_widget_destroy (label->select_info->popup_menu);
5662 label->select_info->popup_menu = menu = gtk_menu_new ();
5664 gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (label), popup_menu_detach);
5667 label->select_info->selection_anchor != label->select_info->selection_end;
5671 if (label->select_info->link_clicked)
5672 link = label->select_info->active_link;
5677 link = gtk_label_get_focus_link (label);
5679 if (!have_selection && link)
5682 menuitem = gtk_image_menu_item_new_with_mnemonic (_("_Open Link"));
5683 gtk_widget_show (menuitem);
5684 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5686 g_signal_connect (G_OBJECT (menuitem), "activate",
5687 G_CALLBACK (open_link_activate_cb), label);
5689 image = gtk_image_new_from_stock (GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_MENU);
5690 gtk_widget_show (image);
5691 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
5693 /* Copy Link Address */
5694 menuitem = gtk_image_menu_item_new_with_mnemonic (_("Copy _Link Address"));
5695 gtk_widget_show (menuitem);
5696 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5698 g_signal_connect (G_OBJECT (menuitem), "activate",
5699 G_CALLBACK (copy_link_activate_cb), label);
5701 image = gtk_image_new_from_stock (GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
5702 gtk_widget_show (image);
5703 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
5707 append_action_signal (label, menu, GTK_STOCK_CUT, "cut-clipboard", FALSE);
5708 append_action_signal (label, menu, GTK_STOCK_COPY, "copy-clipboard", have_selection);
5709 append_action_signal (label, menu, GTK_STOCK_PASTE, "paste-clipboard", FALSE);
5711 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
5712 gtk_widget_set_sensitive (menuitem, FALSE);
5713 gtk_widget_show (menuitem);
5714 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5716 menuitem = gtk_separator_menu_item_new ();
5717 gtk_widget_show (menuitem);
5718 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5720 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
5721 g_signal_connect_swapped (menuitem, "activate",
5722 G_CALLBACK (gtk_label_select_all), label);
5723 gtk_widget_show (menuitem);
5724 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5727 g_signal_emit (label, signals[POPULATE_POPUP], 0, menu);
5730 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
5732 event->button, event->time);
5735 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
5736 popup_position_func, label,
5737 0, gtk_get_current_event_time ());
5738 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
5743 gtk_label_clear_links (GtkLabel *label)
5745 if (!label->select_info)
5748 g_list_foreach (label->select_info->links, (GFunc)link_free, NULL);
5749 g_list_free (label->select_info->links);
5750 label->select_info->links = NULL;
5751 label->select_info->active_link = NULL;
5755 gtk_label_rescan_links (GtkLabel *label)
5757 PangoLayout *layout = label->layout;
5758 PangoAttrList *attlist;
5759 PangoAttrIterator *iter;
5762 if (!label->select_info || !label->select_info->links)
5765 attlist = pango_layout_get_attributes (layout);
5767 if (attlist == NULL)
5770 iter = pango_attr_list_get_iterator (attlist);
5772 links = label->select_info->links;
5776 PangoAttribute *underline;
5777 PangoAttribute *color;
5779 underline = pango_attr_iterator_get (iter, PANGO_ATTR_UNDERLINE);
5780 color = pango_attr_iterator_get (iter, PANGO_ATTR_FOREGROUND);
5782 if (underline != NULL && color != NULL)
5785 PangoRectangle start_pos;
5786 PangoRectangle end_pos;
5789 pango_attr_iterator_range (iter, &start, &end);
5790 pango_layout_index_to_pos (layout, start, &start_pos);
5791 pango_layout_index_to_pos (layout, end, &end_pos);
5795 g_warning ("Ran out of links");
5799 links = links->next;
5800 link->start = start;
5803 } while (pango_attr_iterator_next (iter));
5805 pango_attr_iterator_destroy (iter);
5809 gtk_label_activate_link (GtkLabel *label,
5812 GtkWidget *widget = GTK_WIDGET (label);
5813 GError *error = NULL;
5815 if (!gtk_show_uri (gtk_widget_get_screen (widget),
5816 uri, gtk_get_current_event_time (), &error))
5818 g_warning ("Unable to show '%s': %s", uri, error->message);
5819 g_error_free (error);
5826 emit_activate_link (GtkLabel *label,
5831 g_signal_emit (label, signals[ACTIVATE_LINK], 0, link->uri, &handled);
5832 if (handled && label->track_links && !link->visited)
5834 link->visited = TRUE;
5835 /* FIXME: shouldn't have to redo everything here */
5836 gtk_label_recalculate (label);
5841 gtk_label_activate_current_link (GtkLabel *label)
5844 GtkWidget *widget = GTK_WIDGET (label);
5846 link = gtk_label_get_focus_link (label);
5850 emit_activate_link (label, link);
5854 GtkWidget *toplevel;
5857 toplevel = gtk_widget_get_toplevel (widget);
5858 if (GTK_IS_WINDOW (toplevel))
5860 window = GTK_WINDOW (toplevel);
5863 window->default_widget != widget &&
5864 !(widget == window->focus_widget &&
5865 (!window->default_widget || !gtk_widget_is_sensitive (window->default_widget))))
5866 gtk_window_activate_default (window);
5871 static GtkLabelLink *
5872 gtk_label_get_current_link (GtkLabel *label)
5876 if (!label->select_info)
5879 if (label->select_info->link_clicked)
5880 link = label->select_info->active_link;
5882 link = gtk_label_get_focus_link (label);
5888 * gtk_label_get_current_uri:
5889 * @label: a #GtkLabel
5891 * Returns the URI for the currently active link in the label.
5892 * The active link is the one under the mouse pointer or, in a
5893 * selectable label, the link in which the text cursor is currently
5896 * This function is intended for use in a #GtkLabel::activate-link handler
5897 * or for use in a #GtkWidget::query-tooltip handler.
5899 * Returns: the currently active URI. The string is owned by GTK+ and must
5900 * not be freed or modified.
5904 G_CONST_RETURN gchar *
5905 gtk_label_get_current_uri (GtkLabel *label)
5908 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
5910 link = gtk_label_get_current_link (label);
5919 * gtk_label_set_track_visited_links:
5920 * @label: a #GtkLabel
5921 * @track_links: %TRUE to track visited links
5923 * Sets whether the label should keep track of clicked
5924 * links (and use a different color for them).
5929 gtk_label_set_track_visited_links (GtkLabel *label,
5930 gboolean track_links)
5932 g_return_if_fail (GTK_IS_LABEL (label));
5934 track_links = track_links != FALSE;
5936 if (label->track_links != track_links)
5938 label->track_links = track_links;
5940 /* FIXME: shouldn't have to redo everything here */
5941 gtk_label_recalculate (label);
5943 g_object_notify (G_OBJECT (label), "track-visited-links");
5948 * gtk_label_get_track_visited_links:
5949 * @label: a #GtkLabel
5951 * Returns whether the label is currently keeping track
5954 * Returns: %TRUE if clicked links are remembered
5959 gtk_label_get_track_visited_links (GtkLabel *label)
5961 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5963 return label->track_links;
5967 gtk_label_query_tooltip (GtkWidget *widget,
5970 gboolean keyboard_tip,
5971 GtkTooltip *tooltip)
5973 GtkLabel *label = GTK_LABEL (widget);
5974 GtkLabelSelectionInfo *info = label->select_info;
5978 if (info && info->links)
5982 if (info->selection_anchor == info->selection_end)
5983 index = info->selection_anchor;
5987 if (!get_layout_index (label, x, y, &index))
5993 for (l = info->links; l != NULL; l = l->next)
5995 GtkLabelLink *link = l->data;
5996 if (index >= link->start && index <= link->end)
6000 gtk_tooltip_set_markup (tooltip, link->title);
6009 return GTK_WIDGET_CLASS (gtk_label_parent_class)->query_tooltip (widget,
6016 #define __GTK_LABEL_C__
6017 #include "gtkaliasdef.c"