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_has_window (GTK_WIDGET (label), FALSE);
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)
3262 if (!label->select_info)
3265 widget = GTK_WIDGET (label);
3267 if (gtk_widget_get_realized (widget))
3269 GdkDisplay *display;
3272 if (gtk_widget_is_sensitive (widget))
3274 display = gtk_widget_get_display (widget);
3276 if (label->select_info->active_link)
3277 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
3278 else if (label->select_info->selectable)
3279 cursor = gdk_cursor_new_for_display (display, GDK_XTERM);
3286 gdk_window_set_cursor (label->select_info->window, cursor);
3289 gdk_cursor_unref (cursor);
3294 gtk_label_state_changed (GtkWidget *widget,
3295 GtkStateType prev_state)
3297 GtkLabel *label = GTK_LABEL (widget);
3299 if (label->select_info)
3301 gtk_label_select_region (label, 0, 0);
3302 gtk_label_update_cursor (label);
3305 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed)
3306 GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed (widget, prev_state);
3310 gtk_label_style_set (GtkWidget *widget,
3311 GtkStyle *previous_style)
3313 GtkLabel *label = GTK_LABEL (widget);
3315 /* We have to clear the layout, fonts etc. may have changed */
3316 gtk_label_clear_layout (label);
3317 gtk_label_invalidate_wrap_width (label);
3321 gtk_label_direction_changed (GtkWidget *widget,
3322 GtkTextDirection previous_dir)
3324 GtkLabel *label = GTK_LABEL (widget);
3327 pango_layout_context_changed (label->layout);
3329 GTK_WIDGET_CLASS (gtk_label_parent_class)->direction_changed (widget, previous_dir);
3333 get_layout_location (GtkLabel *label,
3339 GtkLabelPrivate *priv;
3341 gint req_width, x, y;
3342 PangoRectangle logical;
3344 misc = GTK_MISC (label);
3345 widget = GTK_WIDGET (label);
3346 priv = GTK_LABEL_GET_PRIVATE (label);
3348 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
3349 xalign = misc->xalign;
3351 xalign = 1.0 - misc->xalign;
3353 pango_layout_get_pixel_extents (label->layout, NULL, &logical);
3355 if (label->ellipsize || priv->width_chars > 0)
3359 width = pango_layout_get_width (label->layout);
3361 req_width = logical.width;
3363 req_width = MIN(PANGO_PIXELS (width), req_width);
3364 req_width += 2 * misc->xpad;
3367 req_width = widget->requisition.width;
3369 x = floor (widget->allocation.x + (gint)misc->xpad +
3370 xalign * (widget->allocation.width - req_width));
3372 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
3373 x = MAX (x, widget->allocation.x + misc->xpad);
3375 x = MIN (x, widget->allocation.x + widget->allocation.width - misc->xpad);
3378 /* bgo#315462 - For single-line labels, *do* align the requisition with
3379 * respect to the allocation, even if we are under-allocated. For multi-line
3380 * labels, always show the top of the text when they are under-allocated. The
3381 * rationale is this:
3383 * - Single-line labels appear in GtkButtons, and it is very easy to get them
3384 * to be smaller than their requisition. The button may clip the label, but
3385 * the label will still be able to show most of itself and the focus
3386 * rectangle. Also, it is fairly easy to read a single line of clipped text.
3388 * - Multi-line labels should not be clipped to showing "something in the
3389 * middle". You want to read the first line, at least, to get some context.
3391 if (pango_layout_get_line_count (label->layout) == 1)
3392 y = floor (widget->allocation.y + (gint)misc->ypad
3393 + (widget->allocation.height - widget->requisition.height) * misc->yalign);
3395 y = floor (widget->allocation.y + (gint)misc->ypad
3396 + MAX (((widget->allocation.height - widget->requisition.height) * misc->yalign),
3407 draw_insertion_cursor (GtkLabel *label,
3408 GdkRectangle *cursor_location,
3409 gboolean is_primary,
3410 PangoDirection direction,
3411 gboolean draw_arrow)
3413 GtkWidget *widget = GTK_WIDGET (label);
3414 GtkTextDirection text_dir;
3416 if (direction == PANGO_DIRECTION_LTR)
3417 text_dir = GTK_TEXT_DIR_LTR;
3419 text_dir = GTK_TEXT_DIR_RTL;
3421 gtk_draw_insertion_cursor (widget, widget->window, &(widget->allocation),
3423 is_primary, text_dir, draw_arrow);
3426 static PangoDirection
3427 get_cursor_direction (GtkLabel *label)
3431 g_assert (label->select_info);
3433 gtk_label_ensure_layout (label);
3435 for (l = pango_layout_get_lines_readonly (label->layout); l; l = l->next)
3437 PangoLayoutLine *line = l->data;
3439 /* If label->select_info->selection_end is at the very end of
3440 * the line, we don't know if the cursor is on this line or
3441 * the next without looking ahead at the next line. (End
3442 * of paragraph is different from line break.) But it's
3443 * definitely in this paragraph, which is good enough
3444 * to figure out the resolved direction.
3446 if (line->start_index + line->length >= label->select_info->selection_end)
3447 return line->resolved_dir;
3450 return PANGO_DIRECTION_LTR;
3454 gtk_label_draw_cursor (GtkLabel *label, gint xoffset, gint yoffset)
3458 if (label->select_info == NULL)
3461 widget = GTK_WIDGET (label);
3463 if (gtk_widget_is_drawable (widget))
3465 PangoDirection keymap_direction;
3466 PangoDirection cursor_direction;
3467 PangoRectangle strong_pos, weak_pos;
3468 gboolean split_cursor;
3469 PangoRectangle *cursor1 = NULL;
3470 PangoRectangle *cursor2 = NULL;
3471 GdkRectangle cursor_location;
3472 PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
3473 PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
3475 keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gtk_widget_get_display (widget)));
3476 cursor_direction = get_cursor_direction (label);
3478 gtk_label_ensure_layout (label);
3480 pango_layout_get_cursor_pos (label->layout, label->select_info->selection_end,
3481 &strong_pos, &weak_pos);
3483 g_object_get (gtk_widget_get_settings (widget),
3484 "gtk-split-cursor", &split_cursor,
3487 dir1 = cursor_direction;
3491 cursor1 = &strong_pos;
3493 if (strong_pos.x != weak_pos.x ||
3494 strong_pos.y != weak_pos.y)
3496 dir2 = (cursor_direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
3497 cursor2 = &weak_pos;
3502 if (keymap_direction == cursor_direction)
3503 cursor1 = &strong_pos;
3505 cursor1 = &weak_pos;
3508 cursor_location.x = xoffset + PANGO_PIXELS (cursor1->x);
3509 cursor_location.y = yoffset + PANGO_PIXELS (cursor1->y);
3510 cursor_location.width = 0;
3511 cursor_location.height = PANGO_PIXELS (cursor1->height);
3513 draw_insertion_cursor (label,
3514 &cursor_location, TRUE, dir1,
3515 dir2 != PANGO_DIRECTION_NEUTRAL);
3517 if (dir2 != PANGO_DIRECTION_NEUTRAL)
3519 cursor_location.x = xoffset + PANGO_PIXELS (cursor2->x);
3520 cursor_location.y = yoffset + PANGO_PIXELS (cursor2->y);
3521 cursor_location.width = 0;
3522 cursor_location.height = PANGO_PIXELS (cursor2->height);
3524 draw_insertion_cursor (label,
3525 &cursor_location, FALSE, dir2,
3531 static GtkLabelLink *
3532 gtk_label_get_focus_link (GtkLabel *label)
3534 GtkLabelSelectionInfo *info = label->select_info;
3540 if (info->selection_anchor != info->selection_end)
3543 for (l = info->links; l; l = l->next)
3545 GtkLabelLink *link = l->data;
3546 if (link->start <= info->selection_anchor &&
3547 info->selection_anchor <= link->end)
3555 gtk_label_expose (GtkWidget *widget,
3556 GdkEventExpose *event)
3558 GtkLabel *label = GTK_LABEL (widget);
3559 GtkLabelSelectionInfo *info = label->select_info;
3562 gtk_label_ensure_layout (label);
3564 if (gtk_widget_get_visible (widget) && gtk_widget_get_mapped (widget) &&
3565 label->text && (*label->text != '\0'))
3567 get_layout_location (label, &x, &y);
3569 gtk_paint_layout (widget->style,
3571 gtk_widget_get_state (widget),
3580 (info->selection_anchor != info->selection_end))
3586 range[0] = info->selection_anchor;
3587 range[1] = info->selection_end;
3589 if (range[0] > range[1])
3591 gint tmp = range[0];
3592 range[0] = range[1];
3596 clip = gdk_pango_layout_get_clip_region (label->layout,
3600 gdk_region_intersect (clip, event->region);
3602 /* FIXME should use gtk_paint, but it can't use a clip
3606 gdk_gc_set_clip_region (widget->style->black_gc, clip);
3609 state = GTK_STATE_SELECTED;
3610 if (!gtk_widget_has_focus (widget))
3611 state = GTK_STATE_ACTIVE;
3613 gdk_draw_layout_with_colors (widget->window,
3614 widget->style->black_gc,
3617 &widget->style->text[state],
3618 &widget->style->base[state]);
3620 gdk_gc_set_clip_region (widget->style->black_gc, NULL);
3621 gdk_region_destroy (clip);
3625 GtkLabelLink *focus_link;
3626 GtkLabelLink *active_link;
3630 GdkColor *text_color;
3631 GdkColor *base_color;
3632 GdkColor *link_color;
3633 GdkColor *visited_link_color;
3635 if (info->selectable && gtk_widget_has_focus (widget))
3636 gtk_label_draw_cursor (label, x, y);
3638 focus_link = gtk_label_get_focus_link (label);
3639 active_link = info->active_link;
3643 range[0] = active_link->start;
3644 range[1] = active_link->end;
3646 clip = gdk_pango_layout_get_clip_region (label->layout,
3650 gdk_gc_set_clip_region (widget->style->black_gc, clip);
3652 gtk_label_get_link_colors (widget, &link_color, &visited_link_color);
3653 if (active_link->visited)
3654 text_color = visited_link_color;
3656 text_color = link_color;
3657 if (info->link_clicked)
3658 base_color = &widget->style->base[GTK_STATE_ACTIVE];
3660 base_color = &widget->style->base[GTK_STATE_PRELIGHT];
3661 gdk_draw_layout_with_colors (widget->window,
3662 widget->style->black_gc,
3667 gdk_color_free (link_color);
3668 gdk_color_free (visited_link_color);
3670 gdk_gc_set_clip_region (widget->style->black_gc, NULL);
3671 gdk_region_destroy (clip);
3674 if (focus_link && gtk_widget_has_focus (widget))
3676 range[0] = focus_link->start;
3677 range[1] = focus_link->end;
3679 clip = gdk_pango_layout_get_clip_region (label->layout,
3683 gdk_region_get_clipbox (clip, &rect);
3685 gtk_paint_focus (widget->style, widget->window, gtk_widget_get_state (widget),
3686 &event->area, widget, "label",
3687 rect.x, rect.y, rect.width, rect.height);
3689 gdk_region_destroy (clip);
3698 separate_uline_pattern (const gchar *str,
3703 gboolean underscore;
3706 gchar *pattern_dest;
3708 *accel_key = GDK_VoidSymbol;
3709 *new_str = g_new (gchar, strlen (str) + 1);
3710 *pattern = g_new (gchar, g_utf8_strlen (str, -1) + 1);
3716 pattern_dest = *pattern;
3721 const gchar *next_src;
3723 c = g_utf8_get_char (src);
3724 if (c == (gunichar)-1)
3726 g_warning ("Invalid input string");
3732 next_src = g_utf8_next_char (src);
3737 *pattern_dest++ = ' ';
3740 *pattern_dest++ = '_';
3741 if (*accel_key == GDK_VoidSymbol)
3742 *accel_key = gdk_keyval_to_lower (gdk_unicode_to_keyval (c));
3745 while (src < next_src)
3759 while (src < next_src)
3762 *pattern_dest++ = ' ';
3774 gtk_label_set_uline_text_internal (GtkLabel *label,
3777 guint accel_key = GDK_VoidSymbol;
3781 g_return_if_fail (GTK_IS_LABEL (label));
3782 g_return_if_fail (str != NULL);
3784 /* Split text into the base text and a separate pattern
3787 if (!separate_uline_pattern (str, &accel_key, &new_str, &pattern))
3790 gtk_label_set_text_internal (label, new_str);
3791 gtk_label_set_pattern_internal (label, pattern, TRUE);
3792 label->mnemonic_keyval = accel_key;
3798 gtk_label_parse_uline (GtkLabel *label,
3803 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_VoidSymbol);
3804 g_return_val_if_fail (str != NULL, GDK_VoidSymbol);
3806 g_object_freeze_notify (G_OBJECT (label));
3808 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
3809 gtk_label_set_use_markup_internal (label, FALSE);
3810 gtk_label_set_use_underline_internal (label, TRUE);
3812 gtk_label_recalculate (label);
3814 keyval = label->mnemonic_keyval;
3815 if (keyval != GDK_VoidSymbol)
3817 label->mnemonic_keyval = GDK_VoidSymbol;
3818 gtk_label_setup_mnemonic (label, keyval);
3819 g_object_notify (G_OBJECT (label), "mnemonic-keyval");
3822 g_object_thaw_notify (G_OBJECT (label));
3828 * gtk_label_set_text_with_mnemonic:
3829 * @label: a #GtkLabel
3832 * Sets the label's text from the string @str.
3833 * If characters in @str are preceded by an underscore, they are underlined
3834 * indicating that they represent a keyboard accelerator called a mnemonic.
3835 * The mnemonic key can be used to activate another widget, chosen
3836 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
3839 gtk_label_set_text_with_mnemonic (GtkLabel *label,
3842 g_return_if_fail (GTK_IS_LABEL (label));
3843 g_return_if_fail (str != NULL);
3845 g_object_freeze_notify (G_OBJECT (label));
3847 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
3848 gtk_label_set_use_markup_internal (label, FALSE);
3849 gtk_label_set_use_underline_internal (label, TRUE);
3851 gtk_label_recalculate (label);
3853 g_object_thaw_notify (G_OBJECT (label));
3857 gtk_label_realize (GtkWidget *widget)
3861 label = GTK_LABEL (widget);
3863 GTK_WIDGET_CLASS (gtk_label_parent_class)->realize (widget);
3865 if (label->select_info)
3866 gtk_label_create_window (label);
3870 gtk_label_unrealize (GtkWidget *widget)
3874 label = GTK_LABEL (widget);
3876 if (label->select_info)
3877 gtk_label_destroy_window (label);
3879 GTK_WIDGET_CLASS (gtk_label_parent_class)->unrealize (widget);
3883 gtk_label_map (GtkWidget *widget)
3887 label = GTK_LABEL (widget);
3889 GTK_WIDGET_CLASS (gtk_label_parent_class)->map (widget);
3891 if (label->select_info)
3892 gdk_window_show (label->select_info->window);
3896 gtk_label_unmap (GtkWidget *widget)
3900 label = GTK_LABEL (widget);
3902 if (label->select_info)
3903 gdk_window_hide (label->select_info->window);
3905 GTK_WIDGET_CLASS (gtk_label_parent_class)->unmap (widget);
3909 window_to_layout_coords (GtkLabel *label,
3916 widget = GTK_WIDGET (label);
3918 /* get layout location in widget->window coords */
3919 get_layout_location (label, &lx, &ly);
3923 *x += widget->allocation.x; /* go to widget->window */
3924 *x -= lx; /* go to layout */
3929 *y += widget->allocation.y; /* go to widget->window */
3930 *y -= ly; /* go to layout */
3936 layout_to_window_coords (GtkLabel *label,
3943 widget = GTK_WIDGET (label);
3945 /* get layout location in widget->window coords */
3946 get_layout_location (label, &lx, &ly);
3950 *x += lx; /* go to widget->window */
3951 *x -= widget->allocation.x; /* go to selection window */
3956 *y += ly; /* go to widget->window */
3957 *y -= widget->allocation.y; /* go to selection window */
3963 get_layout_index (GtkLabel *label,
3969 const gchar *cluster;
3970 const gchar *cluster_end;
3975 gtk_label_ensure_layout (label);
3977 window_to_layout_coords (label, &x, &y);
3982 inside = pango_layout_xy_to_index (label->layout,
3986 cluster = label->text + *index;
3987 cluster_end = cluster;
3990 cluster_end = g_utf8_next_char (cluster_end);
3994 *index += (cluster_end - cluster);
4000 gtk_label_select_word (GtkLabel *label)
4004 gint start_index = gtk_label_move_backward_word (label, label->select_info->selection_end);
4005 gint end_index = gtk_label_move_forward_word (label, label->select_info->selection_end);
4007 min = MIN (label->select_info->selection_anchor,
4008 label->select_info->selection_end);
4009 max = MAX (label->select_info->selection_anchor,
4010 label->select_info->selection_end);
4012 min = MIN (min, start_index);
4013 max = MAX (max, end_index);
4015 gtk_label_select_region_index (label, min, max);
4019 gtk_label_grab_focus (GtkWidget *widget)
4022 gboolean select_on_focus;
4025 label = GTK_LABEL (widget);
4027 if (label->select_info == NULL)
4030 GTK_WIDGET_CLASS (gtk_label_parent_class)->grab_focus (widget);
4032 if (label->select_info->selectable)
4034 g_object_get (gtk_widget_get_settings (widget),
4035 "gtk-label-select-on-focus",
4039 if (select_on_focus && !label->in_click)
4040 gtk_label_select_region (label, 0, -1);
4044 if (label->select_info->links && !label->in_click)
4046 link = label->select_info->links->data;
4047 label->select_info->selection_anchor = link->start;
4048 label->select_info->selection_end = link->start;
4054 gtk_label_focus (GtkWidget *widget,
4055 GtkDirectionType direction)
4057 GtkLabel *label = GTK_LABEL (widget);
4058 GtkLabelSelectionInfo *info = label->select_info;
4059 GtkLabelLink *focus_link;
4062 if (!gtk_widget_is_focus (widget))
4064 gtk_widget_grab_focus (widget);
4067 focus_link = gtk_label_get_focus_link (label);
4068 if (focus_link && direction == GTK_DIR_TAB_BACKWARD)
4070 l = g_list_last (info->links);
4071 focus_link = l->data;
4072 info->selection_anchor = focus_link->start;
4073 info->selection_end = focus_link->start;
4083 if (info->selectable)
4087 if (info->selection_anchor != info->selection_end)
4090 index = info->selection_anchor;
4092 if (direction == GTK_DIR_TAB_FORWARD)
4093 for (l = info->links; l; l = l->next)
4095 GtkLabelLink *link = l->data;
4097 if (link->start > index)
4099 gtk_label_select_region_index (label, link->start, link->start);
4103 else if (direction == GTK_DIR_TAB_BACKWARD)
4104 for (l = g_list_last (info->links); l; l = l->prev)
4106 GtkLabelLink *link = l->data;
4108 if (link->end < index)
4110 gtk_label_select_region_index (label, link->start, link->start);
4119 focus_link = gtk_label_get_focus_link (label);
4122 case GTK_DIR_TAB_FORWARD:
4125 l = g_list_find (info->links, focus_link);
4132 case GTK_DIR_TAB_BACKWARD:
4135 l = g_list_find (info->links, focus_link);
4139 l = g_list_last (info->links);
4148 focus_link = l->data;
4149 info->selection_anchor = focus_link->start;
4150 info->selection_end = focus_link->start;
4151 gtk_widget_queue_draw (widget);
4163 gtk_label_button_press (GtkWidget *widget,
4164 GdkEventButton *event)
4166 GtkLabel *label = GTK_LABEL (widget);
4167 GtkLabelSelectionInfo *info = label->select_info;
4174 if (info->active_link)
4176 if (event->button == 1)
4178 info->link_clicked = 1;
4179 gtk_widget_queue_draw (widget);
4181 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4183 info->link_clicked = 1;
4184 gtk_label_do_popup (label, event);
4189 if (!info->selectable)
4192 info->in_drag = FALSE;
4193 info->select_words = FALSE;
4195 if (event->button == 1)
4197 if (!gtk_widget_has_focus (widget))
4199 label->in_click = TRUE;
4200 gtk_widget_grab_focus (widget);
4201 label->in_click = FALSE;
4204 if (event->type == GDK_3BUTTON_PRESS)
4206 gtk_label_select_region_index (label, 0, strlen (label->text));
4210 if (event->type == GDK_2BUTTON_PRESS)
4212 info->select_words = TRUE;
4213 gtk_label_select_word (label);
4217 get_layout_index (label, event->x, event->y, &index);
4219 min = MIN (info->selection_anchor, info->selection_end);
4220 max = MAX (info->selection_anchor, info->selection_end);
4222 if ((info->selection_anchor != info->selection_end) &&
4223 (event->state & GDK_SHIFT_MASK))
4225 /* extend (same as motion) */
4226 min = MIN (min, index);
4227 max = MAX (max, index);
4229 /* ensure the anchor is opposite index */
4237 gtk_label_select_region_index (label, min, max);
4241 if (event->type == GDK_3BUTTON_PRESS)
4242 gtk_label_select_region_index (label, 0, strlen (label->text));
4243 else if (event->type == GDK_2BUTTON_PRESS)
4244 gtk_label_select_word (label);
4245 else if (min < max && min <= index && index <= max)
4247 info->in_drag = TRUE;
4248 info->drag_start_x = event->x;
4249 info->drag_start_y = event->y;
4252 /* start a replacement */
4253 gtk_label_select_region_index (label, index, index);
4258 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4260 gtk_label_do_popup (label, event);
4268 gtk_label_button_release (GtkWidget *widget,
4269 GdkEventButton *event)
4272 GtkLabel *label = GTK_LABEL (widget);
4273 GtkLabelSelectionInfo *info = label->select_info;
4283 get_layout_index (label, event->x, event->y, &index);
4284 gtk_label_select_region_index (label, index, index);
4289 if (event->button != 1)
4292 if (info->active_link &&
4293 info->selection_anchor == info->selection_end &&
4296 emit_activate_link (label, info->active_link);
4297 info->link_clicked = 0;
4302 /* The goal here is to return TRUE iff we ate the
4303 * button press to start selecting.
4309 connect_mnemonics_visible_notify (GtkLabel *label)
4311 GtkLabelPrivate *priv = GTK_LABEL_GET_PRIVATE (label);
4312 GtkWidget *toplevel;
4315 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
4317 if (!GTK_IS_WINDOW (toplevel))
4320 /* always set up this widgets initial value */
4321 priv->mnemonics_visible =
4322 gtk_window_get_mnemonics_visible (GTK_WINDOW (toplevel));
4325 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (toplevel),
4326 "gtk-label-mnemonics-visible-connected"));
4330 g_signal_connect (toplevel,
4331 "notify::mnemonics-visible",
4332 G_CALLBACK (label_mnemonics_visible_changed),
4334 g_object_set_data (G_OBJECT (toplevel),
4335 "gtk-label-mnemonics-visible-connected",
4336 GINT_TO_POINTER (1));
4341 drag_begin_cb (GtkWidget *widget,
4342 GdkDragContext *context,
4346 GdkPixmap *pixmap = NULL;
4348 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
4350 label = GTK_LABEL (widget);
4352 if ((label->select_info->selection_anchor !=
4353 label->select_info->selection_end) &&
4359 start = MIN (label->select_info->selection_anchor,
4360 label->select_info->selection_end);
4361 end = MAX (label->select_info->selection_anchor,
4362 label->select_info->selection_end);
4364 len = strlen (label->text);
4372 pixmap = _gtk_text_util_create_drag_icon (widget,
4373 label->text + start,
4378 gtk_drag_set_icon_pixmap (context,
4379 gdk_drawable_get_colormap (pixmap),
4384 gtk_drag_set_icon_default (context);
4387 g_object_unref (pixmap);
4391 gtk_label_motion (GtkWidget *widget,
4392 GdkEventMotion *event)
4394 GtkLabel *label = GTK_LABEL (widget);
4395 GtkLabelSelectionInfo *info = label->select_info;
4402 if (info->links && !info->in_drag)
4406 gboolean found = FALSE;
4408 if (info->selection_anchor == info->selection_end)
4410 gdk_window_get_pointer (event->window, &x, &y, NULL);
4411 if (get_layout_index (label, x, y, &index))
4413 for (l = info->links; l != NULL; l = l->next)
4416 if (index >= link->start && index <= link->end)
4427 if (info->active_link != link)
4429 info->link_clicked = 0;
4430 info->active_link = link;
4431 gtk_label_update_cursor (label);
4432 gtk_widget_queue_draw (widget);
4437 if (info->active_link != NULL)
4439 info->link_clicked = 0;
4440 info->active_link = NULL;
4441 gtk_label_update_cursor (label);
4442 gtk_widget_queue_draw (widget);
4447 if (!info->selectable)
4450 if ((event->state & GDK_BUTTON1_MASK) == 0)
4453 gdk_window_get_pointer (info->window, &x, &y, NULL);
4457 if (gtk_drag_check_threshold (widget,
4460 event->x, event->y))
4462 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
4464 gtk_target_list_add_text_targets (target_list, 0);
4466 g_signal_connect (widget, "drag-begin",
4467 G_CALLBACK (drag_begin_cb), NULL);
4468 gtk_drag_begin (widget, target_list,
4470 1, (GdkEvent *)event);
4472 info->in_drag = FALSE;
4474 gtk_target_list_unref (target_list);
4479 get_layout_index (label, x, y, &index);
4481 if (info->select_words)
4484 gint old_min, old_max;
4487 min = gtk_label_move_backward_word (label, index);
4488 max = gtk_label_move_forward_word (label, index);
4490 anchor = info->selection_anchor;
4491 end = info->selection_end;
4493 old_min = MIN (anchor, end);
4494 old_max = MAX (anchor, end);
4501 else if (old_max < max)
4506 else if (anchor == old_min)
4517 gtk_label_select_region_index (label, anchor, end);
4520 gtk_label_select_region_index (label, info->selection_anchor, index);
4527 gtk_label_leave_notify (GtkWidget *widget,
4528 GdkEventCrossing *event)
4530 GtkLabel *label = GTK_LABEL (widget);
4532 if (label->select_info)
4534 label->select_info->active_link = NULL;
4535 gtk_label_update_cursor (label);
4536 gtk_widget_queue_draw (widget);
4539 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event)
4540 return GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event (widget, event);
4546 gtk_label_create_window (GtkLabel *label)
4549 GdkWindowAttr attributes;
4550 gint attributes_mask;
4552 g_assert (label->select_info);
4553 widget = GTK_WIDGET (label);
4554 g_assert (gtk_widget_get_realized (widget));
4556 if (label->select_info->window)
4559 attributes.x = widget->allocation.x;
4560 attributes.y = widget->allocation.y;
4561 attributes.width = widget->allocation.width;
4562 attributes.height = widget->allocation.height;
4563 attributes.window_type = GDK_WINDOW_CHILD;
4564 attributes.wclass = GDK_INPUT_ONLY;
4565 attributes.override_redirect = TRUE;
4566 attributes.event_mask = gtk_widget_get_events (widget) |
4567 GDK_BUTTON_PRESS_MASK |
4568 GDK_BUTTON_RELEASE_MASK |
4569 GDK_LEAVE_NOTIFY_MASK |
4570 GDK_BUTTON_MOTION_MASK |
4571 GDK_POINTER_MOTION_MASK |
4572 GDK_POINTER_MOTION_HINT_MASK;
4573 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
4574 if (gtk_widget_is_sensitive (widget))
4576 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
4578 attributes_mask |= GDK_WA_CURSOR;
4582 label->select_info->window = gdk_window_new (widget->window,
4583 &attributes, attributes_mask);
4584 gdk_window_set_user_data (label->select_info->window, widget);
4586 if (attributes_mask & GDK_WA_CURSOR)
4587 gdk_cursor_unref (attributes.cursor);
4591 gtk_label_destroy_window (GtkLabel *label)
4593 g_assert (label->select_info);
4595 if (label->select_info->window == NULL)
4598 gdk_window_set_user_data (label->select_info->window, NULL);
4599 gdk_window_destroy (label->select_info->window);
4600 label->select_info->window = NULL;
4604 gtk_label_ensure_select_info (GtkLabel *label)
4606 if (label->select_info == NULL)
4608 label->select_info = g_new0 (GtkLabelSelectionInfo, 1);
4610 gtk_widget_set_can_focus (GTK_WIDGET (label), TRUE);
4612 if (gtk_widget_get_realized (GTK_WIDGET (label)))
4613 gtk_label_create_window (label);
4615 if (gtk_widget_get_mapped (GTK_WIDGET (label)))
4616 gdk_window_show (label->select_info->window);
4621 gtk_label_clear_select_info (GtkLabel *label)
4623 if (label->select_info == NULL)
4626 if (!label->select_info->selectable && !label->select_info->links)
4628 gtk_label_destroy_window (label);
4630 g_free (label->select_info);
4631 label->select_info = NULL;
4633 gtk_widget_set_can_focus (GTK_WIDGET (label), FALSE);
4638 * gtk_label_set_selectable:
4639 * @label: a #GtkLabel
4640 * @setting: %TRUE to allow selecting text in the label
4642 * Selectable labels allow the user to select text from the label, for
4646 gtk_label_set_selectable (GtkLabel *label,
4649 gboolean old_setting;
4651 g_return_if_fail (GTK_IS_LABEL (label));
4653 setting = setting != FALSE;
4654 old_setting = label->select_info && label->select_info->selectable;
4658 gtk_label_ensure_select_info (label);
4659 label->select_info->selectable = TRUE;
4660 gtk_label_update_cursor (label);
4666 /* unselect, to give up the selection */
4667 gtk_label_select_region (label, 0, 0);
4669 label->select_info->selectable = FALSE;
4670 gtk_label_clear_select_info (label);
4671 gtk_label_update_cursor (label);
4674 if (setting != old_setting)
4676 g_object_freeze_notify (G_OBJECT (label));
4677 g_object_notify (G_OBJECT (label), "selectable");
4678 g_object_notify (G_OBJECT (label), "cursor-position");
4679 g_object_notify (G_OBJECT (label), "selection-bound");
4680 g_object_thaw_notify (G_OBJECT (label));
4681 gtk_widget_queue_draw (GTK_WIDGET (label));
4686 * gtk_label_get_selectable:
4687 * @label: a #GtkLabel
4689 * Gets the value set by gtk_label_set_selectable().
4691 * Return value: %TRUE if the user can copy text from the label
4694 gtk_label_get_selectable (GtkLabel *label)
4696 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
4698 return label->select_info && label->select_info->selectable;
4702 free_angle (gpointer angle)
4704 g_slice_free (gdouble, angle);
4708 * gtk_label_set_angle:
4709 * @label: a #GtkLabel
4710 * @angle: the angle that the baseline of the label makes with
4711 * the horizontal, in degrees, measured counterclockwise
4713 * Sets the angle of rotation for the label. An angle of 90 reads from
4714 * from bottom to top, an angle of 270, from top to bottom. The angle
4715 * setting for the label is ignored if the label is selectable,
4716 * wrapped, or ellipsized.
4721 gtk_label_set_angle (GtkLabel *label,
4724 gdouble *label_angle;
4726 g_return_if_fail (GTK_IS_LABEL (label));
4728 label_angle = (gdouble *)g_object_get_qdata (G_OBJECT (label), quark_angle);
4732 label_angle = g_slice_new (gdouble);
4734 g_object_set_qdata_full (G_OBJECT (label), quark_angle,
4735 label_angle, free_angle);
4738 /* Canonicalize to [0,360]. We don't canonicalize 360 to 0, because
4739 * double property ranges are inclusive, and changing 360 to 0 would
4740 * make a property editor behave strangely.
4742 if (angle < 0 || angle > 360.0)
4743 angle = angle - 360. * floor (angle / 360.);
4745 if (*label_angle != angle)
4747 *label_angle = angle;
4749 gtk_label_clear_layout (label);
4750 gtk_widget_queue_resize (GTK_WIDGET (label));
4752 g_object_notify (G_OBJECT (label), "angle");
4757 * gtk_label_get_angle:
4758 * @label: a #GtkLabel
4760 * Gets the angle of rotation for the label. See
4761 * gtk_label_set_angle().
4763 * Return value: the angle of rotation for the label
4768 gtk_label_get_angle (GtkLabel *label)
4772 g_return_val_if_fail (GTK_IS_LABEL (label), 0.0);
4774 angle = (gdouble *)g_object_get_qdata (G_OBJECT (label), quark_angle);
4783 gtk_label_set_selection_text (GtkLabel *label,
4784 GtkSelectionData *selection_data)
4786 if ((label->select_info->selection_anchor !=
4787 label->select_info->selection_end) &&
4793 start = MIN (label->select_info->selection_anchor,
4794 label->select_info->selection_end);
4795 end = MAX (label->select_info->selection_anchor,
4796 label->select_info->selection_end);
4798 len = strlen (label->text);
4806 gtk_selection_data_set_text (selection_data,
4807 label->text + start,
4813 gtk_label_drag_data_get (GtkWidget *widget,
4814 GdkDragContext *context,
4815 GtkSelectionData *selection_data,
4819 gtk_label_set_selection_text (GTK_LABEL (widget), selection_data);
4823 get_text_callback (GtkClipboard *clipboard,
4824 GtkSelectionData *selection_data,
4826 gpointer user_data_or_owner)
4828 gtk_label_set_selection_text (GTK_LABEL (user_data_or_owner), selection_data);
4832 clear_text_callback (GtkClipboard *clipboard,
4833 gpointer user_data_or_owner)
4837 label = GTK_LABEL (user_data_or_owner);
4839 if (label->select_info)
4841 label->select_info->selection_anchor = label->select_info->selection_end;
4843 gtk_widget_queue_draw (GTK_WIDGET (label));
4848 gtk_label_select_region_index (GtkLabel *label,
4852 g_return_if_fail (GTK_IS_LABEL (label));
4854 if (label->select_info && label->select_info->selectable)
4856 GtkClipboard *clipboard;
4858 if (label->select_info->selection_anchor == anchor_index &&
4859 label->select_info->selection_end == end_index)
4862 label->select_info->selection_anchor = anchor_index;
4863 label->select_info->selection_end = end_index;
4865 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label),
4866 GDK_SELECTION_PRIMARY);
4868 if (anchor_index != end_index)
4870 GtkTargetList *list;
4871 GtkTargetEntry *targets;
4874 list = gtk_target_list_new (NULL, 0);
4875 gtk_target_list_add_text_targets (list, 0);
4876 targets = gtk_target_table_new_from_list (list, &n_targets);
4878 gtk_clipboard_set_with_owner (clipboard,
4881 clear_text_callback,
4884 gtk_target_table_free (targets, n_targets);
4885 gtk_target_list_unref (list);
4889 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (label))
4890 gtk_clipboard_clear (clipboard);
4893 gtk_widget_queue_draw (GTK_WIDGET (label));
4895 g_object_freeze_notify (G_OBJECT (label));
4896 g_object_notify (G_OBJECT (label), "cursor-position");
4897 g_object_notify (G_OBJECT (label), "selection-bound");
4898 g_object_thaw_notify (G_OBJECT (label));
4903 * gtk_label_select_region:
4904 * @label: a #GtkLabel
4905 * @start_offset: start offset (in characters not bytes)
4906 * @end_offset: end offset (in characters not bytes)
4908 * Selects a range of characters in the label, if the label is selectable.
4909 * See gtk_label_set_selectable(). If the label is not selectable,
4910 * this function has no effect. If @start_offset or
4911 * @end_offset are -1, then the end of the label will be substituted.
4914 gtk_label_select_region (GtkLabel *label,
4918 g_return_if_fail (GTK_IS_LABEL (label));
4920 if (label->text && label->select_info)
4922 if (start_offset < 0)
4923 start_offset = g_utf8_strlen (label->text, -1);
4926 end_offset = g_utf8_strlen (label->text, -1);
4928 gtk_label_select_region_index (label,
4929 g_utf8_offset_to_pointer (label->text, start_offset) - label->text,
4930 g_utf8_offset_to_pointer (label->text, end_offset) - label->text);
4935 * gtk_label_get_selection_bounds:
4936 * @label: a #GtkLabel
4937 * @start: return location for start of selection, as a character offset
4938 * @end: return location for end of selection, as a character offset
4940 * Gets the selected range of characters in the label, returning %TRUE
4941 * if there's a selection.
4943 * Return value: %TRUE if selection is non-empty
4946 gtk_label_get_selection_bounds (GtkLabel *label,
4950 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
4952 if (label->select_info == NULL)
4954 /* not a selectable label */
4964 gint start_index, end_index;
4965 gint start_offset, end_offset;
4968 start_index = MIN (label->select_info->selection_anchor,
4969 label->select_info->selection_end);
4970 end_index = MAX (label->select_info->selection_anchor,
4971 label->select_info->selection_end);
4973 len = strlen (label->text);
4975 if (end_index > len)
4978 if (start_index > len)
4981 start_offset = g_utf8_strlen (label->text, start_index);
4982 end_offset = g_utf8_strlen (label->text, end_index);
4984 if (start_offset > end_offset)
4986 gint tmp = start_offset;
4987 start_offset = end_offset;
4992 *start = start_offset;
4997 return start_offset != end_offset;
5003 * gtk_label_get_layout:
5004 * @label: a #GtkLabel
5006 * Gets the #PangoLayout used to display the label.
5007 * The layout is useful to e.g. convert text positions to
5008 * pixel positions, in combination with gtk_label_get_layout_offsets().
5009 * The returned layout is owned by the label so need not be
5010 * freed by the caller.
5012 * Return value: (transfer none): the #PangoLayout for this label
5015 gtk_label_get_layout (GtkLabel *label)
5017 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
5019 gtk_label_ensure_layout (label);
5021 return label->layout;
5025 * gtk_label_get_layout_offsets:
5026 * @label: a #GtkLabel
5027 * @x: (allow-none): location to store X offset of layout, or %NULL
5028 * @y: (allow-none): location to store Y offset of layout, or %NULL
5030 * Obtains the coordinates where the label will draw the #PangoLayout
5031 * representing the text in the label; useful to convert mouse events
5032 * into coordinates inside the #PangoLayout, e.g. to take some action
5033 * if some part of the label is clicked. Of course you will need to
5034 * create a #GtkEventBox to receive the events, and pack the label
5035 * inside it, since labels are a #GTK_NO_WINDOW widget. Remember
5036 * when using the #PangoLayout functions you need to convert to
5037 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
5040 gtk_label_get_layout_offsets (GtkLabel *label,
5044 g_return_if_fail (GTK_IS_LABEL (label));
5046 gtk_label_ensure_layout (label);
5048 get_layout_location (label, x, y);
5052 * gtk_label_set_use_markup:
5053 * @label: a #GtkLabel
5054 * @setting: %TRUE if the label's text should be parsed for markup.
5056 * Sets whether the text of the label contains markup in <link
5057 * linkend="PangoMarkupFormat">Pango's text markup
5058 * language</link>. See gtk_label_set_markup().
5061 gtk_label_set_use_markup (GtkLabel *label,
5064 g_return_if_fail (GTK_IS_LABEL (label));
5066 gtk_label_set_use_markup_internal (label, setting);
5067 gtk_label_recalculate (label);
5071 * gtk_label_get_use_markup:
5072 * @label: a #GtkLabel
5074 * Returns whether the label's text is interpreted as marked up with
5075 * the <link linkend="PangoMarkupFormat">Pango text markup
5076 * language</link>. See gtk_label_set_use_markup ().
5078 * Return value: %TRUE if the label's text will be parsed for markup.
5081 gtk_label_get_use_markup (GtkLabel *label)
5083 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5085 return label->use_markup;
5089 * gtk_label_set_use_underline:
5090 * @label: a #GtkLabel
5091 * @setting: %TRUE if underlines in the text indicate mnemonics
5093 * If true, an underline in the text indicates the next character should be
5094 * used for the mnemonic accelerator key.
5097 gtk_label_set_use_underline (GtkLabel *label,
5100 g_return_if_fail (GTK_IS_LABEL (label));
5102 gtk_label_set_use_underline_internal (label, setting);
5103 gtk_label_recalculate (label);
5107 * gtk_label_get_use_underline:
5108 * @label: a #GtkLabel
5110 * Returns whether an embedded underline in the label indicates a
5111 * mnemonic. See gtk_label_set_use_underline().
5113 * Return value: %TRUE whether an embedded underline in the label indicates
5114 * the mnemonic accelerator keys.
5117 gtk_label_get_use_underline (GtkLabel *label)
5119 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5121 return label->use_underline;
5125 * gtk_label_set_single_line_mode:
5126 * @label: a #GtkLabel
5127 * @single_line_mode: %TRUE if the label should be in single line mode
5129 * Sets whether the label is in single line mode.
5134 gtk_label_set_single_line_mode (GtkLabel *label,
5135 gboolean single_line_mode)
5137 g_return_if_fail (GTK_IS_LABEL (label));
5139 single_line_mode = single_line_mode != FALSE;
5141 if (label->single_line_mode != single_line_mode)
5143 label->single_line_mode = single_line_mode;
5145 gtk_label_clear_layout (label);
5146 gtk_widget_queue_resize (GTK_WIDGET (label));
5148 g_object_notify (G_OBJECT (label), "single-line-mode");
5153 * gtk_label_get_single_line_mode:
5154 * @label: a #GtkLabel
5156 * Returns whether the label is in single line mode.
5158 * Return value: %TRUE when the label is in single line mode.
5163 gtk_label_get_single_line_mode (GtkLabel *label)
5165 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5167 return label->single_line_mode;
5170 /* Compute the X position for an offset that corresponds to the "more important
5171 * cursor position for that offset. We use this when trying to guess to which
5172 * end of the selection we should go to when the user hits the left or
5176 get_better_cursor (GtkLabel *label,
5181 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5182 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5183 PangoDirection cursor_direction = get_cursor_direction (label);
5184 gboolean split_cursor;
5185 PangoRectangle strong_pos, weak_pos;
5187 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5188 "gtk-split-cursor", &split_cursor,
5191 gtk_label_ensure_layout (label);
5193 pango_layout_get_cursor_pos (label->layout, index,
5194 &strong_pos, &weak_pos);
5198 *x = strong_pos.x / PANGO_SCALE;
5199 *y = strong_pos.y / PANGO_SCALE;
5203 if (keymap_direction == cursor_direction)
5205 *x = strong_pos.x / PANGO_SCALE;
5206 *y = strong_pos.y / PANGO_SCALE;
5210 *x = weak_pos.x / PANGO_SCALE;
5211 *y = weak_pos.y / PANGO_SCALE;
5218 gtk_label_move_logically (GtkLabel *label,
5222 gint offset = g_utf8_pointer_to_offset (label->text,
5223 label->text + start);
5227 PangoLogAttr *log_attrs;
5231 gtk_label_ensure_layout (label);
5233 length = g_utf8_strlen (label->text, -1);
5235 pango_layout_get_log_attrs (label->layout, &log_attrs, &n_attrs);
5237 while (count > 0 && offset < length)
5241 while (offset < length && !log_attrs[offset].is_cursor_position);
5245 while (count < 0 && offset > 0)
5249 while (offset > 0 && !log_attrs[offset].is_cursor_position);
5257 return g_utf8_offset_to_pointer (label->text, offset) - label->text;
5261 gtk_label_move_visually (GtkLabel *label,
5271 int new_index, new_trailing;
5272 gboolean split_cursor;
5275 gtk_label_ensure_layout (label);
5277 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5278 "gtk-split-cursor", &split_cursor,
5285 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5286 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5288 strong = keymap_direction == get_cursor_direction (label);
5293 pango_layout_move_cursor_visually (label->layout, strong, index, 0, 1, &new_index, &new_trailing);
5298 pango_layout_move_cursor_visually (label->layout, strong, index, 0, -1, &new_index, &new_trailing);
5302 if (new_index < 0 || new_index == G_MAXINT)
5307 while (new_trailing--)
5308 index = g_utf8_next_char (label->text + new_index) - label->text;
5315 gtk_label_move_forward_word (GtkLabel *label,
5318 gint new_pos = g_utf8_pointer_to_offset (label->text,
5319 label->text + start);
5322 length = g_utf8_strlen (label->text, -1);
5323 if (new_pos < length)
5325 PangoLogAttr *log_attrs;
5328 gtk_label_ensure_layout (label);
5330 pango_layout_get_log_attrs (label->layout, &log_attrs, &n_attrs);
5332 /* Find the next word end */
5334 while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
5340 return g_utf8_offset_to_pointer (label->text, new_pos) - label->text;
5345 gtk_label_move_backward_word (GtkLabel *label,
5348 gint new_pos = g_utf8_pointer_to_offset (label->text,
5349 label->text + start);
5353 PangoLogAttr *log_attrs;
5356 gtk_label_ensure_layout (label);
5358 pango_layout_get_log_attrs (label->layout, &log_attrs, &n_attrs);
5362 /* Find the previous word beginning */
5363 while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
5369 return g_utf8_offset_to_pointer (label->text, new_pos) - label->text;
5373 gtk_label_move_cursor (GtkLabel *label,
5374 GtkMovementStep step,
5376 gboolean extend_selection)
5381 if (label->select_info == NULL)
5384 old_pos = new_pos = label->select_info->selection_end;
5386 if (label->select_info->selection_end != label->select_info->selection_anchor &&
5389 /* If we have a current selection and aren't extending it, move to the
5390 * start/or end of the selection as appropriate
5394 case GTK_MOVEMENT_VISUAL_POSITIONS:
5397 gint anchor_x, anchor_y;
5398 gboolean end_is_left;
5400 get_better_cursor (label, label->select_info->selection_end, &end_x, &end_y);
5401 get_better_cursor (label, label->select_info->selection_anchor, &anchor_x, &anchor_y);
5403 end_is_left = (end_y < anchor_y) || (end_y == anchor_y && end_x < anchor_x);
5406 new_pos = end_is_left ? label->select_info->selection_end : label->select_info->selection_anchor;
5408 new_pos = !end_is_left ? label->select_info->selection_end : label->select_info->selection_anchor;
5411 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5412 case GTK_MOVEMENT_WORDS:
5414 new_pos = MIN (label->select_info->selection_end, label->select_info->selection_anchor);
5416 new_pos = MAX (label->select_info->selection_end, label->select_info->selection_anchor);
5418 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5419 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5420 case GTK_MOVEMENT_BUFFER_ENDS:
5421 /* FIXME: Can do better here */
5422 new_pos = count < 0 ? 0 : strlen (label->text);
5424 case GTK_MOVEMENT_DISPLAY_LINES:
5425 case GTK_MOVEMENT_PARAGRAPHS:
5426 case GTK_MOVEMENT_PAGES:
5427 case GTK_MOVEMENT_HORIZONTAL_PAGES:
5435 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5436 new_pos = gtk_label_move_logically (label, new_pos, count);
5438 case GTK_MOVEMENT_VISUAL_POSITIONS:
5439 new_pos = gtk_label_move_visually (label, new_pos, count);
5440 if (new_pos == old_pos)
5442 if (!extend_selection)
5444 if (!gtk_widget_keynav_failed (GTK_WIDGET (label),
5446 GTK_DIR_RIGHT : GTK_DIR_LEFT))
5448 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
5451 gtk_widget_child_focus (toplevel,
5453 GTK_DIR_RIGHT : GTK_DIR_LEFT);
5458 gtk_widget_error_bell (GTK_WIDGET (label));
5462 case GTK_MOVEMENT_WORDS:
5465 new_pos = gtk_label_move_forward_word (label, new_pos);
5470 new_pos = gtk_label_move_backward_word (label, new_pos);
5473 if (new_pos == old_pos)
5474 gtk_widget_error_bell (GTK_WIDGET (label));
5476 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5477 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5478 case GTK_MOVEMENT_BUFFER_ENDS:
5479 /* FIXME: Can do better here */
5480 new_pos = count < 0 ? 0 : strlen (label->text);
5481 if (new_pos == old_pos)
5482 gtk_widget_error_bell (GTK_WIDGET (label));
5484 case GTK_MOVEMENT_DISPLAY_LINES:
5485 case GTK_MOVEMENT_PARAGRAPHS:
5486 case GTK_MOVEMENT_PAGES:
5487 case GTK_MOVEMENT_HORIZONTAL_PAGES:
5492 if (extend_selection)
5493 gtk_label_select_region_index (label,
5494 label->select_info->selection_anchor,
5497 gtk_label_select_region_index (label, new_pos, new_pos);
5501 gtk_label_copy_clipboard (GtkLabel *label)
5503 if (label->text && label->select_info)
5507 GtkClipboard *clipboard;
5509 start = MIN (label->select_info->selection_anchor,
5510 label->select_info->selection_end);
5511 end = MAX (label->select_info->selection_anchor,
5512 label->select_info->selection_end);
5514 len = strlen (label->text);
5522 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
5525 gtk_clipboard_set_text (clipboard, label->text + start, end - start);
5530 link = gtk_label_get_focus_link (label);
5532 gtk_clipboard_set_text (clipboard, link->uri, -1);
5538 gtk_label_select_all (GtkLabel *label)
5540 gtk_label_select_region_index (label, 0, strlen (label->text));
5543 /* Quick hack of a popup menu
5546 activate_cb (GtkWidget *menuitem,
5549 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
5550 g_signal_emit_by_name (label, signal);
5554 append_action_signal (GtkLabel *label,
5556 const gchar *stock_id,
5557 const gchar *signal,
5560 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
5562 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
5563 g_signal_connect (menuitem, "activate",
5564 G_CALLBACK (activate_cb), label);
5566 gtk_widget_set_sensitive (menuitem, sensitive);
5568 gtk_widget_show (menuitem);
5569 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5573 popup_menu_detach (GtkWidget *attach_widget,
5576 GtkLabel *label = GTK_LABEL (attach_widget);
5578 if (label->select_info)
5579 label->select_info->popup_menu = NULL;
5583 popup_position_func (GtkMenu *menu,
5594 label = GTK_LABEL (user_data);
5595 widget = GTK_WIDGET (label);
5597 g_return_if_fail (gtk_widget_get_realized (widget));
5599 screen = gtk_widget_get_screen (widget);
5600 gdk_window_get_origin (widget->window, x, y);
5602 *x += widget->allocation.x;
5603 *y += widget->allocation.y;
5605 gtk_widget_size_request (GTK_WIDGET (menu), &req);
5607 *x += widget->allocation.width / 2;
5608 *y += widget->allocation.height;
5610 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
5611 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
5615 open_link_activate_cb (GtkMenuItem *menu_item,
5620 link = gtk_label_get_current_link (label);
5623 emit_activate_link (label, link);
5627 copy_link_activate_cb (GtkMenuItem *menu_item,
5630 GtkClipboard *clipboard;
5633 uri = gtk_label_get_current_uri (label);
5636 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
5637 gtk_clipboard_set_text (clipboard, uri, -1);
5642 gtk_label_popup_menu (GtkWidget *widget)
5644 gtk_label_do_popup (GTK_LABEL (widget), NULL);
5650 gtk_label_do_popup (GtkLabel *label,
5651 GdkEventButton *event)
5653 GtkWidget *menuitem;
5656 gboolean have_selection;
5659 if (!label->select_info)
5662 if (label->select_info->popup_menu)
5663 gtk_widget_destroy (label->select_info->popup_menu);
5665 label->select_info->popup_menu = menu = gtk_menu_new ();
5667 gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (label), popup_menu_detach);
5670 label->select_info->selection_anchor != label->select_info->selection_end;
5674 if (label->select_info->link_clicked)
5675 link = label->select_info->active_link;
5680 link = gtk_label_get_focus_link (label);
5682 if (!have_selection && link)
5685 menuitem = gtk_image_menu_item_new_with_mnemonic (_("_Open Link"));
5686 gtk_widget_show (menuitem);
5687 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5689 g_signal_connect (G_OBJECT (menuitem), "activate",
5690 G_CALLBACK (open_link_activate_cb), label);
5692 image = gtk_image_new_from_stock (GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_MENU);
5693 gtk_widget_show (image);
5694 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
5696 /* Copy Link Address */
5697 menuitem = gtk_image_menu_item_new_with_mnemonic (_("Copy _Link Address"));
5698 gtk_widget_show (menuitem);
5699 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5701 g_signal_connect (G_OBJECT (menuitem), "activate",
5702 G_CALLBACK (copy_link_activate_cb), label);
5704 image = gtk_image_new_from_stock (GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
5705 gtk_widget_show (image);
5706 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
5710 append_action_signal (label, menu, GTK_STOCK_CUT, "cut-clipboard", FALSE);
5711 append_action_signal (label, menu, GTK_STOCK_COPY, "copy-clipboard", have_selection);
5712 append_action_signal (label, menu, GTK_STOCK_PASTE, "paste-clipboard", FALSE);
5714 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
5715 gtk_widget_set_sensitive (menuitem, FALSE);
5716 gtk_widget_show (menuitem);
5717 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5719 menuitem = gtk_separator_menu_item_new ();
5720 gtk_widget_show (menuitem);
5721 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5723 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
5724 g_signal_connect_swapped (menuitem, "activate",
5725 G_CALLBACK (gtk_label_select_all), label);
5726 gtk_widget_show (menuitem);
5727 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5730 g_signal_emit (label, signals[POPULATE_POPUP], 0, menu);
5733 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
5735 event->button, event->time);
5738 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
5739 popup_position_func, label,
5740 0, gtk_get_current_event_time ());
5741 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
5746 gtk_label_clear_links (GtkLabel *label)
5748 if (!label->select_info)
5751 g_list_foreach (label->select_info->links, (GFunc)link_free, NULL);
5752 g_list_free (label->select_info->links);
5753 label->select_info->links = NULL;
5754 label->select_info->active_link = NULL;
5758 gtk_label_rescan_links (GtkLabel *label)
5760 PangoLayout *layout = label->layout;
5761 PangoAttrList *attlist;
5762 PangoAttrIterator *iter;
5765 if (!label->select_info || !label->select_info->links)
5768 attlist = pango_layout_get_attributes (layout);
5770 if (attlist == NULL)
5773 iter = pango_attr_list_get_iterator (attlist);
5775 links = label->select_info->links;
5779 PangoAttribute *underline;
5780 PangoAttribute *color;
5782 underline = pango_attr_iterator_get (iter, PANGO_ATTR_UNDERLINE);
5783 color = pango_attr_iterator_get (iter, PANGO_ATTR_FOREGROUND);
5785 if (underline != NULL && color != NULL)
5788 PangoRectangle start_pos;
5789 PangoRectangle end_pos;
5792 pango_attr_iterator_range (iter, &start, &end);
5793 pango_layout_index_to_pos (layout, start, &start_pos);
5794 pango_layout_index_to_pos (layout, end, &end_pos);
5798 g_warning ("Ran out of links");
5802 links = links->next;
5803 link->start = start;
5806 } while (pango_attr_iterator_next (iter));
5808 pango_attr_iterator_destroy (iter);
5812 gtk_label_activate_link (GtkLabel *label,
5815 GtkWidget *widget = GTK_WIDGET (label);
5816 GError *error = NULL;
5818 if (!gtk_show_uri (gtk_widget_get_screen (widget),
5819 uri, gtk_get_current_event_time (), &error))
5821 g_warning ("Unable to show '%s': %s", uri, error->message);
5822 g_error_free (error);
5829 emit_activate_link (GtkLabel *label,
5834 g_signal_emit (label, signals[ACTIVATE_LINK], 0, link->uri, &handled);
5835 if (handled && label->track_links && !link->visited)
5837 link->visited = TRUE;
5838 /* FIXME: shouldn't have to redo everything here */
5839 gtk_label_recalculate (label);
5844 gtk_label_activate_current_link (GtkLabel *label)
5847 GtkWidget *widget = GTK_WIDGET (label);
5849 link = gtk_label_get_focus_link (label);
5853 emit_activate_link (label, link);
5857 GtkWidget *toplevel;
5860 toplevel = gtk_widget_get_toplevel (widget);
5861 if (GTK_IS_WINDOW (toplevel))
5863 window = GTK_WINDOW (toplevel);
5866 window->default_widget != widget &&
5867 !(widget == window->focus_widget &&
5868 (!window->default_widget || !gtk_widget_is_sensitive (window->default_widget))))
5869 gtk_window_activate_default (window);
5874 static GtkLabelLink *
5875 gtk_label_get_current_link (GtkLabel *label)
5879 if (!label->select_info)
5882 if (label->select_info->link_clicked)
5883 link = label->select_info->active_link;
5885 link = gtk_label_get_focus_link (label);
5891 * gtk_label_get_current_uri:
5892 * @label: a #GtkLabel
5894 * Returns the URI for the currently active link in the label.
5895 * The active link is the one under the mouse pointer or, in a
5896 * selectable label, the link in which the text cursor is currently
5899 * This function is intended for use in a #GtkLabel::activate-link handler
5900 * or for use in a #GtkWidget::query-tooltip handler.
5902 * Returns: the currently active URI. The string is owned by GTK+ and must
5903 * not be freed or modified.
5907 G_CONST_RETURN gchar *
5908 gtk_label_get_current_uri (GtkLabel *label)
5911 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
5913 link = gtk_label_get_current_link (label);
5922 * gtk_label_set_track_visited_links:
5923 * @label: a #GtkLabel
5924 * @track_links: %TRUE to track visited links
5926 * Sets whether the label should keep track of clicked
5927 * links (and use a different color for them).
5932 gtk_label_set_track_visited_links (GtkLabel *label,
5933 gboolean track_links)
5935 g_return_if_fail (GTK_IS_LABEL (label));
5937 track_links = track_links != FALSE;
5939 if (label->track_links != track_links)
5941 label->track_links = track_links;
5943 /* FIXME: shouldn't have to redo everything here */
5944 gtk_label_recalculate (label);
5946 g_object_notify (G_OBJECT (label), "track-visited-links");
5951 * gtk_label_get_track_visited_links:
5952 * @label: a #GtkLabel
5954 * Returns whether the label is currently keeping track
5957 * Returns: %TRUE if clicked links are remembered
5962 gtk_label_get_track_visited_links (GtkLabel *label)
5964 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5966 return label->track_links;
5970 gtk_label_query_tooltip (GtkWidget *widget,
5973 gboolean keyboard_tip,
5974 GtkTooltip *tooltip)
5976 GtkLabel *label = GTK_LABEL (widget);
5977 GtkLabelSelectionInfo *info = label->select_info;
5981 if (info && info->links)
5985 if (info->selection_anchor == info->selection_end)
5986 index = info->selection_anchor;
5990 if (!get_layout_index (label, x, y, &index))
5996 for (l = info->links; l != NULL; l = l->next)
5998 GtkLabelLink *link = l->data;
5999 if (index >= link->start && index <= link->end)
6003 gtk_tooltip_set_markup (tooltip, link->title);
6012 return GTK_WIDGET_CLASS (gtk_label_parent_class)->query_tooltip (widget,
6019 #define __GTK_LABEL_C__
6020 #include "gtkaliasdef.c"