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"
48 #include "gtkprivate.h"
51 #define GTK_LABEL_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_LABEL, GtkLabelPrivate))
61 struct _GtkLabelSelectionInfo
64 gint selection_anchor;
66 GtkWidget *popup_menu;
98 PROP_SINGLE_LINE_MODE,
103 static guint signals[LAST_SIGNAL] = { 0 };
105 static void gtk_label_set_property (GObject *object,
109 static void gtk_label_get_property (GObject *object,
113 static void gtk_label_destroy (GtkObject *object);
114 static void gtk_label_finalize (GObject *object);
115 static void gtk_label_size_request (GtkWidget *widget,
116 GtkRequisition *requisition);
117 static void gtk_label_size_allocate (GtkWidget *widget,
118 GtkAllocation *allocation);
119 static void gtk_label_state_changed (GtkWidget *widget,
121 static void gtk_label_style_set (GtkWidget *widget,
122 GtkStyle *previous_style);
123 static void gtk_label_direction_changed (GtkWidget *widget,
124 GtkTextDirection previous_dir);
125 static gint gtk_label_expose (GtkWidget *widget,
126 GdkEventExpose *event);
128 static void gtk_label_realize (GtkWidget *widget);
129 static void gtk_label_unrealize (GtkWidget *widget);
130 static void gtk_label_map (GtkWidget *widget);
131 static void gtk_label_unmap (GtkWidget *widget);
133 static gboolean gtk_label_button_press (GtkWidget *widget,
134 GdkEventButton *event);
135 static gboolean gtk_label_button_release (GtkWidget *widget,
136 GdkEventButton *event);
137 static gboolean gtk_label_motion (GtkWidget *widget,
138 GdkEventMotion *event);
139 static void gtk_label_grab_focus (GtkWidget *widget);
142 static void gtk_label_set_text_internal (GtkLabel *label,
144 static void gtk_label_set_label_internal (GtkLabel *label,
146 static void gtk_label_set_use_markup_internal (GtkLabel *label,
148 static void gtk_label_set_use_underline_internal (GtkLabel *label,
150 static void gtk_label_set_attributes_internal (GtkLabel *label,
151 PangoAttrList *attrs);
152 static void gtk_label_set_uline_text_internal (GtkLabel *label,
154 static void gtk_label_set_pattern_internal (GtkLabel *label,
155 const gchar *pattern);
156 static void set_markup (GtkLabel *label,
158 gboolean with_uline);
159 static void gtk_label_recalculate (GtkLabel *label);
160 static void gtk_label_hierarchy_changed (GtkWidget *widget,
161 GtkWidget *old_toplevel);
162 static void gtk_label_screen_changed (GtkWidget *widget,
163 GdkScreen *old_screen);
165 static void gtk_label_create_window (GtkLabel *label);
166 static void gtk_label_destroy_window (GtkLabel *label);
167 static void gtk_label_clear_layout (GtkLabel *label);
168 static void gtk_label_ensure_layout (GtkLabel *label);
169 static void gtk_label_invalidate_wrap_width (GtkLabel *label);
170 static void gtk_label_select_region_index (GtkLabel *label,
174 static gboolean gtk_label_mnemonic_activate (GtkWidget *widget,
175 gboolean group_cycling);
176 static void gtk_label_setup_mnemonic (GtkLabel *label,
178 static void gtk_label_drag_data_get (GtkWidget *widget,
179 GdkDragContext *context,
180 GtkSelectionData *selection_data,
184 static void gtk_label_buildable_interface_init (GtkBuildableIface *iface);
185 static gboolean gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
188 const gchar *tagname,
189 GMarkupParser *parser,
192 static void gtk_label_buildable_custom_finished (GtkBuildable *buildable,
195 const gchar *tagname,
199 /* For selectable lables: */
200 static void gtk_label_move_cursor (GtkLabel *label,
201 GtkMovementStep step,
203 gboolean extend_selection);
204 static void gtk_label_copy_clipboard (GtkLabel *label);
205 static void gtk_label_select_all (GtkLabel *label);
206 static void gtk_label_do_popup (GtkLabel *label,
207 GdkEventButton *event);
209 static gint gtk_label_move_forward_word (GtkLabel *label,
211 static gint gtk_label_move_backward_word (GtkLabel *label,
214 static GQuark quark_angle = 0;
216 static GtkBuildableIface *buildable_parent_iface = NULL;
218 G_DEFINE_TYPE_WITH_CODE (GtkLabel, gtk_label, GTK_TYPE_MISC,
219 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
220 gtk_label_buildable_interface_init));
223 add_move_binding (GtkBindingSet *binding_set,
226 GtkMovementStep step,
229 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
231 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
235 G_TYPE_BOOLEAN, FALSE);
237 /* Selection-extending version */
238 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
242 G_TYPE_BOOLEAN, TRUE);
246 gtk_label_class_init (GtkLabelClass *class)
248 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
249 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
250 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
251 GtkBindingSet *binding_set;
253 quark_angle = g_quark_from_static_string ("angle");
255 gobject_class->set_property = gtk_label_set_property;
256 gobject_class->get_property = gtk_label_get_property;
257 gobject_class->finalize = gtk_label_finalize;
259 object_class->destroy = gtk_label_destroy;
261 widget_class->size_request = gtk_label_size_request;
262 widget_class->size_allocate = gtk_label_size_allocate;
263 widget_class->state_changed = gtk_label_state_changed;
264 widget_class->style_set = gtk_label_style_set;
265 widget_class->direction_changed = gtk_label_direction_changed;
266 widget_class->expose_event = gtk_label_expose;
267 widget_class->realize = gtk_label_realize;
268 widget_class->unrealize = gtk_label_unrealize;
269 widget_class->map = gtk_label_map;
270 widget_class->unmap = gtk_label_unmap;
271 widget_class->button_press_event = gtk_label_button_press;
272 widget_class->button_release_event = gtk_label_button_release;
273 widget_class->motion_notify_event = gtk_label_motion;
274 widget_class->hierarchy_changed = gtk_label_hierarchy_changed;
275 widget_class->screen_changed = gtk_label_screen_changed;
276 widget_class->mnemonic_activate = gtk_label_mnemonic_activate;
277 widget_class->drag_data_get = gtk_label_drag_data_get;
278 widget_class->grab_focus = gtk_label_grab_focus;
280 class->move_cursor = gtk_label_move_cursor;
281 class->copy_clipboard = gtk_label_copy_clipboard;
283 signals[MOVE_CURSOR] =
284 g_signal_new (I_("move-cursor"),
285 G_OBJECT_CLASS_TYPE (gobject_class),
286 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
287 G_STRUCT_OFFSET (GtkLabelClass, move_cursor),
289 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
291 GTK_TYPE_MOVEMENT_STEP,
295 signals[COPY_CLIPBOARD] =
296 g_signal_new (I_("copy-clipboard"),
297 G_OBJECT_CLASS_TYPE (gobject_class),
298 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
299 G_STRUCT_OFFSET (GtkLabelClass, copy_clipboard),
301 _gtk_marshal_VOID__VOID,
304 signals[POPULATE_POPUP] =
305 g_signal_new (I_("populate-popup"),
306 G_OBJECT_CLASS_TYPE (gobject_class),
308 G_STRUCT_OFFSET (GtkLabelClass, populate_popup),
310 _gtk_marshal_VOID__OBJECT,
314 g_object_class_install_property (gobject_class,
316 g_param_spec_string ("label",
318 P_("The text of the label"),
320 GTK_PARAM_READWRITE));
321 g_object_class_install_property (gobject_class,
323 g_param_spec_boxed ("attributes",
325 P_("A list of style attributes to apply to the text of the label"),
326 PANGO_TYPE_ATTR_LIST,
327 GTK_PARAM_READWRITE));
328 g_object_class_install_property (gobject_class,
330 g_param_spec_boolean ("use-markup",
332 P_("The text of the label includes XML markup. See pango_parse_markup()"),
334 GTK_PARAM_READWRITE));
335 g_object_class_install_property (gobject_class,
337 g_param_spec_boolean ("use-underline",
339 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
341 GTK_PARAM_READWRITE));
343 g_object_class_install_property (gobject_class,
345 g_param_spec_enum ("justify",
347 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"),
348 GTK_TYPE_JUSTIFICATION,
350 GTK_PARAM_READWRITE));
352 g_object_class_install_property (gobject_class,
354 g_param_spec_string ("pattern",
356 P_("A string with _ characters in positions correspond to characters in the text to underline"),
358 GTK_PARAM_WRITABLE));
360 g_object_class_install_property (gobject_class,
362 g_param_spec_boolean ("wrap",
364 P_("If set, wrap lines if the text becomes too wide"),
366 GTK_PARAM_READWRITE));
368 * GtkLabel:wrap-mode:
370 * If line wrapping is on (see the #GtkLabel:wrap property) this controls
371 * how the line wrapping is done. The default is %PANGO_WRAP_WORD, which
372 * means wrap on word boundaries.
376 g_object_class_install_property (gobject_class,
378 g_param_spec_enum ("wrap-mode",
379 P_("Line wrap mode"),
380 P_("If wrap is set, controls how linewrapping is done"),
381 PANGO_TYPE_WRAP_MODE,
383 GTK_PARAM_READWRITE));
384 g_object_class_install_property (gobject_class,
386 g_param_spec_boolean ("selectable",
388 P_("Whether the label text can be selected with the mouse"),
390 GTK_PARAM_READWRITE));
391 g_object_class_install_property (gobject_class,
392 PROP_MNEMONIC_KEYVAL,
393 g_param_spec_uint ("mnemonic-keyval",
395 P_("The mnemonic accelerator key for this label"),
399 GTK_PARAM_READABLE));
400 g_object_class_install_property (gobject_class,
401 PROP_MNEMONIC_WIDGET,
402 g_param_spec_object ("mnemonic-widget",
403 P_("Mnemonic widget"),
404 P_("The widget to be activated when the label's mnemonic "
407 GTK_PARAM_READWRITE));
409 g_object_class_install_property (gobject_class,
410 PROP_CURSOR_POSITION,
411 g_param_spec_int ("cursor-position",
412 P_("Cursor Position"),
413 P_("The current position of the insertion cursor in chars"),
417 GTK_PARAM_READABLE));
419 g_object_class_install_property (gobject_class,
420 PROP_SELECTION_BOUND,
421 g_param_spec_int ("selection-bound",
422 P_("Selection Bound"),
423 P_("The position of the opposite end of the selection from the cursor in chars"),
427 GTK_PARAM_READABLE));
430 * GtkLabel:ellipsize:
432 * The preferred place to ellipsize the string, if the label does
433 * not have enough room to display the entire string, specified as a
434 * #PangoEllisizeMode.
436 * Note that setting this property to a value other than
437 * %PANGO_ELLIPSIZE_NONE has the side-effect that the label requests
438 * only enough space to display the ellipsis "...". In particular, this
439 * means that ellipsizing labels do not work well in notebook tabs, unless
440 * the tab's #GtkNotebook:tab-expand property is set to %TRUE. Other ways
441 * to set a label's width are gtk_widget_set_size_request() and
442 * gtk_label_set_width_chars().
446 g_object_class_install_property (gobject_class,
448 g_param_spec_enum ("ellipsize",
450 P_("The preferred place to ellipsize the string, if the label does not have enough room to display the entire string"),
451 PANGO_TYPE_ELLIPSIZE_MODE,
452 PANGO_ELLIPSIZE_NONE,
453 GTK_PARAM_READWRITE));
456 * GtkLabel:width-chars:
458 * The desired width of the label, in characters. If this property is set to
459 * -1, the width will be calculated automatically, otherwise the label will
460 * request either 3 characters or the property value, whichever is greater.
461 * If the "width-chars" property is set to a positive value, then the
462 * #GtkLabel:max-width-chars property is ignored.
466 g_object_class_install_property (gobject_class,
468 g_param_spec_int ("width-chars",
469 P_("Width In Characters"),
470 P_("The desired width of the label, in characters"),
474 GTK_PARAM_READWRITE));
477 * GtkLabel:single-line-mode:
479 * Whether the label is in single line mode. In single line mode,
480 * the height of the label does not depend on the actual text, it
481 * is always set to ascent + descent of the font. This can be an
482 * advantage in situations where resizing the label because of text
483 * changes would be distracting, e.g. in a statusbar.
487 g_object_class_install_property (gobject_class,
488 PROP_SINGLE_LINE_MODE,
489 g_param_spec_boolean ("single-line-mode",
490 P_("Single Line Mode"),
491 P_("Whether the label is in single line mode"),
493 GTK_PARAM_READWRITE));
498 * The angle that the baseline of the label makes with the horizontal,
499 * in degrees, measured counterclockwise. An angle of 90 reads from
500 * from bottom to top, an angle of 270, from top to bottom. Ignored
501 * if the label is selectable, wrapped, or ellipsized.
505 g_object_class_install_property (gobject_class,
507 g_param_spec_double ("angle",
509 P_("Angle at which the label is rotated"),
513 GTK_PARAM_READWRITE));
516 * GtkLabel:max-width-chars:
518 * The desired maximum width of the label, in characters. If this property
519 * is set to -1, the width will be calculated automatically, otherwise the
520 * label will request space for no more than the requested number of
521 * characters. If the #GtkLabel:width-chars property is set to a positive
522 * value, then the "max-width-chars" property is ignored.
526 g_object_class_install_property (gobject_class,
527 PROP_MAX_WIDTH_CHARS,
528 g_param_spec_int ("max-width-chars",
529 P_("Maximum Width In Characters"),
530 P_("The desired maximum width of the label, in characters"),
534 GTK_PARAM_READWRITE));
539 binding_set = gtk_binding_set_by_class (class);
541 /* Moving the insertion point */
542 add_move_binding (binding_set, GDK_Right, 0,
543 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
545 add_move_binding (binding_set, GDK_Left, 0,
546 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
548 add_move_binding (binding_set, GDK_KP_Right, 0,
549 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
551 add_move_binding (binding_set, GDK_KP_Left, 0,
552 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
554 add_move_binding (binding_set, GDK_f, GDK_CONTROL_MASK,
555 GTK_MOVEMENT_LOGICAL_POSITIONS, 1);
557 add_move_binding (binding_set, GDK_b, GDK_CONTROL_MASK,
558 GTK_MOVEMENT_LOGICAL_POSITIONS, -1);
560 add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
561 GTK_MOVEMENT_WORDS, 1);
563 add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
564 GTK_MOVEMENT_WORDS, -1);
566 add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
567 GTK_MOVEMENT_WORDS, 1);
569 add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
570 GTK_MOVEMENT_WORDS, -1);
573 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
575 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
577 G_TYPE_BOOLEAN, FALSE);
579 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
581 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
583 G_TYPE_BOOLEAN, TRUE);
585 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
587 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
589 G_TYPE_BOOLEAN, FALSE);
591 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
593 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
595 G_TYPE_BOOLEAN, TRUE);
598 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
600 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
602 G_TYPE_BOOLEAN, FALSE);
604 gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_CONTROL_MASK,
606 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
608 G_TYPE_BOOLEAN, FALSE);
610 add_move_binding (binding_set, GDK_f, GDK_MOD1_MASK,
611 GTK_MOVEMENT_WORDS, 1);
613 add_move_binding (binding_set, GDK_b, GDK_MOD1_MASK,
614 GTK_MOVEMENT_WORDS, -1);
616 add_move_binding (binding_set, GDK_Home, 0,
617 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
619 add_move_binding (binding_set, GDK_End, 0,
620 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
622 add_move_binding (binding_set, GDK_KP_Home, 0,
623 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
625 add_move_binding (binding_set, GDK_KP_End, 0,
626 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
628 add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
629 GTK_MOVEMENT_BUFFER_ENDS, -1);
631 add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
632 GTK_MOVEMENT_BUFFER_ENDS, 1);
634 add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
635 GTK_MOVEMENT_BUFFER_ENDS, -1);
637 add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
638 GTK_MOVEMENT_BUFFER_ENDS, 1);
641 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
642 "copy-clipboard", 0);
644 gtk_settings_install_property (g_param_spec_boolean ("gtk-label-select-on-focus",
645 P_("Select on focus"),
646 P_("Whether to select the contents of a selectable label when it is focused"),
648 GTK_PARAM_READWRITE));
651 g_type_class_add_private (class, sizeof (GtkLabelPrivate));
655 gtk_label_set_property (GObject *object,
662 label = GTK_LABEL (object);
667 gtk_label_set_label (label, g_value_get_string (value));
669 case PROP_ATTRIBUTES:
670 gtk_label_set_attributes (label, g_value_get_boxed (value));
672 case PROP_USE_MARKUP:
673 gtk_label_set_use_markup (label, g_value_get_boolean (value));
675 case PROP_USE_UNDERLINE:
676 gtk_label_set_use_underline (label, g_value_get_boolean (value));
679 gtk_label_set_justify (label, g_value_get_enum (value));
682 gtk_label_set_pattern (label, g_value_get_string (value));
685 gtk_label_set_line_wrap (label, g_value_get_boolean (value));
688 gtk_label_set_line_wrap_mode (label, g_value_get_enum (value));
690 case PROP_SELECTABLE:
691 gtk_label_set_selectable (label, g_value_get_boolean (value));
693 case PROP_MNEMONIC_WIDGET:
694 gtk_label_set_mnemonic_widget (label, (GtkWidget*) g_value_get_object (value));
697 gtk_label_set_ellipsize (label, g_value_get_enum (value));
699 case PROP_WIDTH_CHARS:
700 gtk_label_set_width_chars (label, g_value_get_int (value));
702 case PROP_SINGLE_LINE_MODE:
703 gtk_label_set_single_line_mode (label, g_value_get_boolean (value));
706 gtk_label_set_angle (label, g_value_get_double (value));
708 case PROP_MAX_WIDTH_CHARS:
709 gtk_label_set_max_width_chars (label, g_value_get_int (value));
712 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
718 gtk_label_get_property (GObject *object,
725 label = GTK_LABEL (object);
730 g_value_set_string (value, label->label);
732 case PROP_ATTRIBUTES:
733 g_value_set_boxed (value, label->attrs);
735 case PROP_USE_MARKUP:
736 g_value_set_boolean (value, label->use_markup);
738 case PROP_USE_UNDERLINE:
739 g_value_set_boolean (value, label->use_underline);
742 g_value_set_enum (value, label->jtype);
745 g_value_set_boolean (value, label->wrap);
748 g_value_set_enum (value, label->wrap_mode);
750 case PROP_SELECTABLE:
751 g_value_set_boolean (value, gtk_label_get_selectable (label));
753 case PROP_MNEMONIC_KEYVAL:
754 g_value_set_uint (value, label->mnemonic_keyval);
756 case PROP_MNEMONIC_WIDGET:
757 g_value_set_object (value, (GObject*) label->mnemonic_widget);
759 case PROP_CURSOR_POSITION:
760 if (label->select_info)
762 gint offset = g_utf8_pointer_to_offset (label->text,
763 label->text + label->select_info->selection_end);
764 g_value_set_int (value, offset);
767 g_value_set_int (value, 0);
769 case PROP_SELECTION_BOUND:
770 if (label->select_info)
772 gint offset = g_utf8_pointer_to_offset (label->text,
773 label->text + label->select_info->selection_anchor);
774 g_value_set_int (value, offset);
777 g_value_set_int (value, 0);
780 g_value_set_enum (value, label->ellipsize);
782 case PROP_WIDTH_CHARS:
783 g_value_set_int (value, gtk_label_get_width_chars (label));
785 case PROP_SINGLE_LINE_MODE:
786 g_value_set_boolean (value, gtk_label_get_single_line_mode (label));
789 g_value_set_double (value, gtk_label_get_angle (label));
791 case PROP_MAX_WIDTH_CHARS:
792 g_value_set_int (value, gtk_label_get_max_width_chars (label));
796 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
802 gtk_label_init (GtkLabel *label)
804 GtkLabelPrivate *priv;
806 GTK_WIDGET_SET_FLAGS (label, GTK_NO_WINDOW);
808 priv = GTK_LABEL_GET_PRIVATE (label);
809 priv->width_chars = -1;
810 priv->max_width_chars = -1;
811 priv->wrap_width = -1;
814 label->jtype = GTK_JUSTIFY_LEFT;
816 label->wrap_mode = PANGO_WRAP_WORD;
817 label->ellipsize = PANGO_ELLIPSIZE_NONE;
819 label->use_underline = FALSE;
820 label->use_markup = FALSE;
821 label->pattern_set = FALSE;
823 label->mnemonic_keyval = GDK_VoidSymbol;
824 label->layout = NULL;
828 label->mnemonic_widget = NULL;
829 label->mnemonic_window = NULL;
831 gtk_label_set_text (label, "");
836 gtk_label_buildable_interface_init (GtkBuildableIface *iface)
838 buildable_parent_iface = g_type_interface_peek_parent (iface);
840 iface->custom_tag_start = gtk_label_buildable_custom_tag_start;
841 iface->custom_finished = gtk_label_buildable_custom_finished;
847 PangoAttrList *attrs;
850 static PangoAttribute *
851 attribute_from_text (GtkBuilder *builder,
856 PangoAttribute *attribute = NULL;
858 PangoLanguage *language;
859 PangoFontDescription *font_desc;
863 if (!gtk_builder_value_from_string_type (builder, PANGO_TYPE_ATTR_TYPE, name, &val, error))
866 type = g_value_get_enum (&val);
867 g_value_unset (&val);
871 /* PangoAttrLanguage */
872 case PANGO_ATTR_LANGUAGE:
873 if ((language = pango_language_from_string (value)))
875 attribute = pango_attr_language_new (language);
876 g_value_init (&val, G_TYPE_INT);
880 case PANGO_ATTR_STYLE:
881 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STYLE, value, &val, error))
882 attribute = pango_attr_style_new (g_value_get_enum (&val));
884 case PANGO_ATTR_WEIGHT:
885 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_WEIGHT, value, &val, error))
886 attribute = pango_attr_weight_new (g_value_get_enum (&val));
888 case PANGO_ATTR_VARIANT:
889 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_VARIANT, value, &val, error))
890 attribute = pango_attr_variant_new (g_value_get_enum (&val));
892 case PANGO_ATTR_STRETCH:
893 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STRETCH, value, &val, error))
894 attribute = pango_attr_stretch_new (g_value_get_enum (&val));
896 case PANGO_ATTR_UNDERLINE:
897 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
898 attribute = pango_attr_underline_new (g_value_get_boolean (&val));
900 case PANGO_ATTR_STRIKETHROUGH:
901 if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
902 attribute = pango_attr_strikethrough_new (g_value_get_boolean (&val));
904 case PANGO_ATTR_GRAVITY:
905 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY, value, &val, error))
906 attribute = pango_attr_gravity_new (g_value_get_enum (&val));
908 case PANGO_ATTR_GRAVITY_HINT:
909 if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY_HINT,
911 attribute = pango_attr_gravity_hint_new (g_value_get_enum (&val));
914 /* PangoAttrString */
915 case PANGO_ATTR_FAMILY:
916 attribute = pango_attr_family_new (value);
917 g_value_init (&val, G_TYPE_INT);
921 case PANGO_ATTR_SIZE:
922 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
924 attribute = pango_attr_size_new (g_value_get_int (&val));
926 case PANGO_ATTR_ABSOLUTE_SIZE:
927 if (gtk_builder_value_from_string_type (builder, G_TYPE_INT,
929 attribute = pango_attr_size_new_absolute (g_value_get_int (&val));
932 /* PangoAttrFontDesc */
933 case PANGO_ATTR_FONT_DESC:
934 if ((font_desc = pango_font_description_from_string (value)))
936 attribute = pango_attr_font_desc_new (font_desc);
937 pango_font_description_free (font_desc);
938 g_value_init (&val, G_TYPE_INT);
943 case PANGO_ATTR_FOREGROUND:
944 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
947 color = g_value_get_boxed (&val);
948 attribute = pango_attr_foreground_new (color->red, color->green, color->blue);
951 case PANGO_ATTR_BACKGROUND:
952 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
955 color = g_value_get_boxed (&val);
956 attribute = pango_attr_background_new (color->red, color->green, color->blue);
959 case PANGO_ATTR_UNDERLINE_COLOR:
960 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
963 color = g_value_get_boxed (&val);
964 attribute = pango_attr_underline_color_new (color->red, color->green, color->blue);
967 case PANGO_ATTR_STRIKETHROUGH_COLOR:
968 if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR,
971 color = g_value_get_boxed (&val);
972 attribute = pango_attr_strikethrough_color_new (color->red, color->green, color->blue);
977 case PANGO_ATTR_SHAPE:
978 /* Unsupported for now */
981 case PANGO_ATTR_SCALE:
982 if (gtk_builder_value_from_string_type (builder, G_TYPE_DOUBLE,
984 attribute = pango_attr_scale_new (g_value_get_double (&val));
987 case PANGO_ATTR_INVALID:
988 case PANGO_ATTR_LETTER_SPACING:
989 case PANGO_ATTR_RISE:
990 case PANGO_ATTR_FALLBACK:
995 g_value_unset (&val);
1002 pango_start_element (GMarkupParseContext *context,
1003 const gchar *element_name,
1004 const gchar **names,
1005 const gchar **values,
1009 PangoParserData *data = (PangoParserData*)user_data;
1010 GValue val = { 0, };
1012 gint line_number, char_number;
1014 if (strcmp (element_name, "attribute") == 0)
1016 PangoAttribute *attr = NULL;
1017 const gchar *name = NULL;
1018 const gchar *value = NULL;
1019 const gchar *start = NULL;
1020 const gchar *end = NULL;
1021 guint start_val = 0;
1022 guint end_val = G_MAXUINT;
1024 for (i = 0; names[i]; i++)
1026 if (strcmp (names[i], "name") == 0)
1028 else if (strcmp (names[i], "value") == 0)
1030 else if (strcmp (names[i], "start") == 0)
1032 else if (strcmp (names[i], "end") == 0)
1036 g_markup_parse_context_get_position (context,
1041 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1042 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1044 line_number, char_number, names[i], "attribute");
1049 if (!name || !value)
1051 g_markup_parse_context_get_position (context,
1056 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1057 "%s:%d:%d <%s> requires attribute \"%s\"",
1059 line_number, char_number, "attribute",
1060 name ? "value" : "name");
1066 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1067 start, &val, error))
1069 start_val = g_value_get_uint (&val);
1070 g_value_unset (&val);
1075 if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT,
1078 end_val = g_value_get_uint (&val);
1079 g_value_unset (&val);
1082 attr = attribute_from_text (data->builder, name, value, error);
1083 attr->start_index = start_val;
1084 attr->end_index = end_val;
1089 data->attrs = pango_attr_list_new ();
1091 pango_attr_list_insert (data->attrs, attr);
1094 else if (strcmp (element_name, "attributes") == 0)
1097 g_warning ("Unsupported tag for GtkLabel: %s\n", element_name);
1100 static const GMarkupParser pango_parser =
1102 pango_start_element,
1106 gtk_label_buildable_custom_tag_start (GtkBuildable *buildable,
1107 GtkBuilder *builder,
1109 const gchar *tagname,
1110 GMarkupParser *parser,
1113 if (buildable_parent_iface->custom_tag_start (buildable, builder, child,
1114 tagname, parser, data))
1117 if (strcmp (tagname, "attributes") == 0)
1119 PangoParserData *parser_data;
1121 parser_data = g_slice_new0 (PangoParserData);
1122 parser_data->builder = g_object_ref (builder);
1123 parser_data->object = g_object_ref (buildable);
1124 *parser = pango_parser;
1125 *data = parser_data;
1132 gtk_label_buildable_custom_finished (GtkBuildable *buildable,
1133 GtkBuilder *builder,
1135 const gchar *tagname,
1138 PangoParserData *data;
1140 buildable_parent_iface->custom_finished (buildable, builder, child,
1141 tagname, user_data);
1143 if (strcmp (tagname, "attributes") == 0)
1145 data = (PangoParserData*)user_data;
1149 gtk_label_set_attributes (GTK_LABEL (buildable), data->attrs);
1150 pango_attr_list_unref (data->attrs);
1153 g_object_unref (data->object);
1154 g_object_unref (data->builder);
1155 g_slice_free (PangoParserData, data);
1162 * @str: The text of the label
1164 * Creates a new label with the given text inside it. You can
1165 * pass %NULL to get an empty label widget.
1167 * Return value: the new #GtkLabel
1170 gtk_label_new (const gchar *str)
1174 label = g_object_new (GTK_TYPE_LABEL, NULL);
1177 gtk_label_set_text (label, str);
1179 return GTK_WIDGET (label);
1183 * gtk_label_new_with_mnemonic:
1184 * @str: The text of the label, with an underscore in front of the
1185 * mnemonic character
1187 * Creates a new #GtkLabel, containing the text in @str.
1189 * If characters in @str are preceded by an underscore, they are
1190 * underlined. If you need a literal underscore character in a label, use
1191 * '__' (two underscores). The first underlined character represents a
1192 * keyboard accelerator called a mnemonic. The mnemonic key can be used
1193 * to activate another widget, chosen automatically, or explicitly using
1194 * gtk_label_set_mnemonic_widget().
1196 * If gtk_label_set_mnemonic_widget() is not called, then the first
1197 * activatable ancestor of the #GtkLabel will be chosen as the mnemonic
1198 * widget. For instance, if the label is inside a button or menu item,
1199 * the button or menu item will automatically become the mnemonic widget
1200 * and be activated by the mnemonic.
1202 * Return value: the new #GtkLabel
1205 gtk_label_new_with_mnemonic (const gchar *str)
1209 label = g_object_new (GTK_TYPE_LABEL, NULL);
1212 gtk_label_set_text_with_mnemonic (label, str);
1214 return GTK_WIDGET (label);
1218 gtk_label_mnemonic_activate (GtkWidget *widget,
1219 gboolean group_cycling)
1223 if (GTK_LABEL (widget)->mnemonic_widget)
1224 return gtk_widget_mnemonic_activate (GTK_LABEL (widget)->mnemonic_widget, group_cycling);
1226 /* Try to find the widget to activate by traversing the
1227 * widget's ancestry.
1229 parent = widget->parent;
1231 if (GTK_IS_NOTEBOOK (parent))
1236 if (GTK_WIDGET_CAN_FOCUS (parent) ||
1237 (!group_cycling && GTK_WIDGET_GET_CLASS (parent)->activate_signal) ||
1238 GTK_IS_NOTEBOOK (parent->parent) ||
1239 GTK_IS_MENU_ITEM (parent))
1240 return gtk_widget_mnemonic_activate (parent, group_cycling);
1241 parent = parent->parent;
1244 /* barf if there was nothing to activate */
1245 g_warning ("Couldn't find a target for a mnemonic activation.");
1246 gtk_widget_error_bell (widget);
1252 gtk_label_setup_mnemonic (GtkLabel *label,
1255 GtkWidget *widget = GTK_WIDGET (label);
1256 GtkWidget *toplevel;
1257 GtkWidget *mnemonic_menu;
1259 mnemonic_menu = g_object_get_data (G_OBJECT (label), "gtk-mnemonic-menu");
1261 if (last_key != GDK_VoidSymbol)
1263 if (label->mnemonic_window)
1265 gtk_window_remove_mnemonic (label->mnemonic_window,
1268 label->mnemonic_window = NULL;
1272 _gtk_menu_shell_remove_mnemonic (GTK_MENU_SHELL (mnemonic_menu),
1275 mnemonic_menu = NULL;
1279 if (label->mnemonic_keyval == GDK_VoidSymbol)
1282 toplevel = gtk_widget_get_toplevel (widget);
1283 if (GTK_WIDGET_TOPLEVEL (toplevel))
1285 GtkWidget *menu_shell;
1287 menu_shell = gtk_widget_get_ancestor (widget,
1288 GTK_TYPE_MENU_SHELL);
1292 _gtk_menu_shell_add_mnemonic (GTK_MENU_SHELL (menu_shell),
1293 label->mnemonic_keyval,
1295 mnemonic_menu = menu_shell;
1298 if (!GTK_IS_MENU (menu_shell))
1300 gtk_window_add_mnemonic (GTK_WINDOW (toplevel),
1301 label->mnemonic_keyval,
1303 label->mnemonic_window = GTK_WINDOW (toplevel);
1308 g_object_set_data (G_OBJECT (label), I_("gtk-mnemonic-menu"), mnemonic_menu);
1312 gtk_label_hierarchy_changed (GtkWidget *widget,
1313 GtkWidget *old_toplevel)
1315 GtkLabel *label = GTK_LABEL (widget);
1317 gtk_label_setup_mnemonic (label, label->mnemonic_keyval);
1321 label_shortcut_setting_apply (GtkLabel *label)
1323 gtk_label_recalculate (label);
1324 if (GTK_IS_ACCEL_LABEL (label))
1325 gtk_accel_label_refetch (GTK_ACCEL_LABEL (label));
1329 label_shortcut_setting_traverse_container (GtkWidget *widget,
1332 if (GTK_IS_LABEL (widget))
1333 label_shortcut_setting_apply (GTK_LABEL (widget));
1334 else if (GTK_IS_CONTAINER (widget))
1335 gtk_container_forall (GTK_CONTAINER (widget),
1336 label_shortcut_setting_traverse_container, data);
1340 label_shortcut_setting_changed (GtkSettings *settings)
1344 list = gtk_window_list_toplevels ();
1346 for (l = list; l ; l = l->next)
1348 GtkWidget *widget = l->data;
1350 if (gtk_widget_get_settings (widget) == settings)
1351 gtk_container_forall (GTK_CONTAINER (widget),
1352 label_shortcut_setting_traverse_container, NULL);
1359 gtk_label_screen_changed (GtkWidget *widget,
1360 GdkScreen *old_screen)
1362 GtkSettings *settings;
1363 gboolean shortcuts_connected;
1365 if (!gtk_widget_has_screen (widget))
1368 settings = gtk_widget_get_settings (widget);
1370 shortcuts_connected =
1371 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (settings),
1372 "gtk-label-shortcuts-connected"));
1374 if (! shortcuts_connected)
1376 g_signal_connect (settings, "notify::gtk-enable-mnemonics",
1377 G_CALLBACK (label_shortcut_setting_changed),
1379 g_signal_connect (settings, "notify::gtk-enable-accels",
1380 G_CALLBACK (label_shortcut_setting_changed),
1383 g_object_set_data (G_OBJECT (settings), "gtk-label-shortcuts-connected",
1384 GINT_TO_POINTER (TRUE));
1387 label_shortcut_setting_apply (GTK_LABEL (widget));
1392 label_mnemonic_widget_weak_notify (gpointer data,
1393 GObject *where_the_object_was)
1395 GtkLabel *label = data;
1397 label->mnemonic_widget = NULL;
1398 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1402 * gtk_label_set_mnemonic_widget:
1403 * @label: a #GtkLabel
1404 * @widget: the target #GtkWidget
1406 * If the label has been set so that it has an mnemonic key (using
1407 * i.e. gtk_label_set_markup_with_mnemonic(),
1408 * gtk_label_set_text_with_mnemonic(), gtk_label_new_with_mnemonic()
1409 * or the "use_underline" property) the label can be associated with a
1410 * widget that is the target of the mnemonic. When the label is inside
1411 * a widget (like a #GtkButton or a #GtkNotebook tab) it is
1412 * automatically associated with the correct widget, but sometimes
1413 * (i.e. when the target is a #GtkEntry next to the label) you need to
1414 * set it explicitly using this function.
1416 * The target widget will be accelerated by emitting the
1417 * GtkWidget::mnemonic-activate signal on it. The default handler for
1418 * this signal will activate the widget if there are no mnemonic collisions
1419 * and toggle focus between the colliding widgets otherwise.
1422 gtk_label_set_mnemonic_widget (GtkLabel *label,
1425 g_return_if_fail (GTK_IS_LABEL (label));
1427 g_return_if_fail (GTK_IS_WIDGET (widget));
1429 if (label->mnemonic_widget)
1431 gtk_widget_remove_mnemonic_label (label->mnemonic_widget, GTK_WIDGET (label));
1432 g_object_weak_unref (G_OBJECT (label->mnemonic_widget),
1433 label_mnemonic_widget_weak_notify,
1436 label->mnemonic_widget = widget;
1437 if (label->mnemonic_widget)
1439 g_object_weak_ref (G_OBJECT (label->mnemonic_widget),
1440 label_mnemonic_widget_weak_notify,
1442 gtk_widget_add_mnemonic_label (label->mnemonic_widget, GTK_WIDGET (label));
1445 g_object_notify (G_OBJECT (label), "mnemonic-widget");
1449 * gtk_label_get_mnemonic_widget:
1450 * @label: a #GtkLabel
1452 * Retrieves the target of the mnemonic (keyboard shortcut) of this
1453 * label. See gtk_label_set_mnemonic_widget().
1455 * Return value: the target of the label's mnemonic, or %NULL if none
1456 * has been set and the default algorithm will be used.
1459 gtk_label_get_mnemonic_widget (GtkLabel *label)
1461 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1463 return label->mnemonic_widget;
1467 * gtk_label_get_mnemonic_keyval:
1468 * @label: a #GtkLabel
1470 * If the label has been set so that it has an mnemonic key this function
1471 * returns the keyval used for the mnemonic accelerator. If there is no
1472 * mnemonic set up it returns #GDK_VoidSymbol.
1474 * Returns: GDK keyval usable for accelerators, or #GDK_VoidSymbol
1477 gtk_label_get_mnemonic_keyval (GtkLabel *label)
1479 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_VoidSymbol);
1481 return label->mnemonic_keyval;
1485 gtk_label_set_text_internal (GtkLabel *label,
1488 g_free (label->text);
1492 gtk_label_select_region_index (label, 0, 0);
1496 gtk_label_set_label_internal (GtkLabel *label,
1499 g_free (label->label);
1503 g_object_notify (G_OBJECT (label), "label");
1507 gtk_label_set_use_markup_internal (GtkLabel *label,
1511 if (label->use_markup != val)
1513 label->use_markup = val;
1515 g_object_notify (G_OBJECT (label), "use-markup");
1520 gtk_label_set_use_underline_internal (GtkLabel *label,
1524 if (label->use_underline != val)
1526 label->use_underline = val;
1528 g_object_notify (G_OBJECT (label), "use-underline");
1533 gtk_label_compose_effective_attrs (GtkLabel *label)
1535 PangoAttrIterator *iter;
1536 PangoAttribute *attr;
1537 GSList *iter_attrs, *l;
1541 if (label->effective_attrs)
1543 if ((iter = pango_attr_list_get_iterator (label->attrs)))
1546 iter_attrs = pango_attr_iterator_get_attrs (iter);
1547 for (l = iter_attrs; l; l = l->next)
1550 pango_attr_list_insert (label->effective_attrs, attr);
1552 g_slist_free (iter_attrs);
1554 while (pango_attr_iterator_next (iter));
1557 label->effective_attrs =
1558 pango_attr_list_ref (label->attrs);
1563 gtk_label_set_attributes_internal (GtkLabel *label,
1564 PangoAttrList *attrs)
1567 pango_attr_list_ref (attrs);
1570 pango_attr_list_unref (label->attrs);
1571 label->attrs = attrs;
1573 g_object_notify (G_OBJECT (label), "attributes");
1577 /* Calculates text, attrs and mnemonic_keyval from
1578 * label, use_underline and use_markup
1581 gtk_label_recalculate (GtkLabel *label)
1583 guint keyval = label->mnemonic_keyval;
1585 if (label->use_markup)
1587 set_markup (label, label->label, label->use_underline);
1588 gtk_label_compose_effective_attrs (label);
1592 if (label->use_underline)
1594 gtk_label_set_uline_text_internal (label, label->label);
1595 gtk_label_compose_effective_attrs (label);
1599 gtk_label_set_text_internal (label, g_strdup (label->label));
1601 pango_attr_list_ref (label->attrs);
1602 if (label->effective_attrs)
1603 pango_attr_list_unref (label->effective_attrs);
1604 label->effective_attrs = label->attrs;
1608 if (!label->use_underline)
1609 label->mnemonic_keyval = GDK_VoidSymbol;
1611 if (keyval != label->mnemonic_keyval)
1613 gtk_label_setup_mnemonic (label, keyval);
1614 g_object_notify (G_OBJECT (label), "mnemonic-keyval");
1617 gtk_label_clear_layout (label);
1618 gtk_widget_queue_resize (GTK_WIDGET (label));
1622 * gtk_label_set_text:
1623 * @label: a #GtkLabel
1624 * @str: The text you want to set
1626 * Sets the text within the #GtkLabel widget. It overwrites any text that
1629 * This will also clear any previously set mnemonic accelerators.
1632 gtk_label_set_text (GtkLabel *label,
1635 g_return_if_fail (GTK_IS_LABEL (label));
1637 g_object_freeze_notify (G_OBJECT (label));
1639 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
1640 gtk_label_set_use_markup_internal (label, FALSE);
1641 gtk_label_set_use_underline_internal (label, FALSE);
1643 gtk_label_recalculate (label);
1645 g_object_thaw_notify (G_OBJECT (label));
1649 * gtk_label_set_attributes:
1650 * @label: a #GtkLabel
1651 * @attrs: a #PangoAttrList
1653 * Sets a #PangoAttrList; the attributes in the list are applied to the
1656 * <note><para>The attributes set with this function will be applied
1657 * and merged with any other attributes previously effected by way
1658 * of the #GtkLabel:use-underline or #GtkLabel:use-markup properties.
1659 * While it is not recommended to mix markup strings with manually set
1660 * attributes, if you must; know that the attributes will be applied
1661 * to the label after the markup string is parsed.</para></note>
1664 gtk_label_set_attributes (GtkLabel *label,
1665 PangoAttrList *attrs)
1667 g_return_if_fail (GTK_IS_LABEL (label));
1669 gtk_label_set_attributes_internal (label, attrs);
1671 gtk_label_recalculate (label);
1673 gtk_label_clear_layout (label);
1674 gtk_widget_queue_resize (GTK_WIDGET (label));
1678 * gtk_label_get_attributes:
1679 * @label: a #GtkLabel
1681 * Gets the attribute list that was set on the label using
1682 * gtk_label_set_attributes(), if any. This function does
1683 * not reflect attributes that come from the labels markup
1684 * (see gtk_label_set_markup()). If you want to get the
1685 * effective attributes for the label, use
1686 * pango_layout_get_attribute (gtk_label_get_layout (label)).
1688 * Return value: the attribute list, or %NULL if none was set.
1691 gtk_label_get_attributes (GtkLabel *label)
1693 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1695 return label->attrs;
1699 * gtk_label_set_label:
1700 * @label: a #GtkLabel
1701 * @str: the new text to set for the label
1703 * Sets the text of the label. The label is interpreted as
1704 * including embedded underlines and/or Pango markup depending
1705 * on the values of the #GtkLabel:use-underline" and
1706 * #GtkLabel:use-markup properties.
1709 gtk_label_set_label (GtkLabel *label,
1712 g_return_if_fail (GTK_IS_LABEL (label));
1713 g_return_if_fail (str != NULL);
1715 g_object_freeze_notify (G_OBJECT (label));
1717 gtk_label_set_label_internal (label, g_strdup (str));
1718 gtk_label_recalculate (label);
1720 g_object_thaw_notify (G_OBJECT (label));
1724 * gtk_label_get_label:
1725 * @label: a #GtkLabel
1727 * Fetches the text from a label widget including any embedded
1728 * underlines indicating mnemonics and Pango markup. (See
1729 * gtk_label_get_text()).
1731 * Return value: the text of the label widget. This string is
1732 * owned by the widget and must not be modified or freed.
1734 G_CONST_RETURN gchar *
1735 gtk_label_get_label (GtkLabel *label)
1737 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1739 return label->label;
1743 set_markup (GtkLabel *label,
1745 gboolean with_uline)
1748 GError *error = NULL;
1749 PangoAttrList *attrs = NULL;
1750 gunichar accel_char = 0;
1752 if (!pango_parse_markup (str,
1754 with_uline ? '_' : 0,
1757 with_uline ? &accel_char : NULL,
1760 g_warning ("Failed to set text from markup due to error parsing markup: %s",
1762 g_error_free (error);
1767 gtk_label_set_text_internal (label, text);
1771 if (label->effective_attrs)
1772 pango_attr_list_unref (label->effective_attrs);
1773 label->effective_attrs = attrs;
1776 if (accel_char != 0)
1777 label->mnemonic_keyval = gdk_keyval_to_lower (gdk_unicode_to_keyval (accel_char));
1779 label->mnemonic_keyval = GDK_VoidSymbol;
1783 * gtk_label_set_markup:
1784 * @label: a #GtkLabel
1785 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
1787 * Parses @str which is marked up with the <link
1788 * linkend="PangoMarkupFormat">Pango text markup language</link>, setting the
1789 * label's text and attribute list based on the parse results. If the @str is
1790 * external data, you may need to escape it with g_markup_escape_text() or
1791 * g_markup_printf_escaped()<!-- -->:
1795 * markup = g_markup_printf_escaped ("<span style=\"italic\">%s</span>", str);
1796 * gtk_label_set_markup (GTK_LABEL (label), markup);
1801 gtk_label_set_markup (GtkLabel *label,
1804 g_return_if_fail (GTK_IS_LABEL (label));
1806 g_object_freeze_notify (G_OBJECT (label));
1808 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
1809 gtk_label_set_use_markup_internal (label, TRUE);
1810 gtk_label_set_use_underline_internal (label, FALSE);
1812 gtk_label_recalculate (label);
1814 g_object_thaw_notify (G_OBJECT (label));
1818 * gtk_label_set_markup_with_mnemonic:
1819 * @label: a #GtkLabel
1820 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
1822 * Parses @str which is marked up with the <link linkend="PangoMarkupFormat">Pango text markup language</link>,
1823 * setting the label's text and attribute list based on the parse results.
1824 * If characters in @str are preceded by an underscore, they are underlined
1825 * indicating that they represent a keyboard accelerator called a mnemonic.
1827 * The mnemonic key can be used to activate another widget, chosen
1828 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
1831 gtk_label_set_markup_with_mnemonic (GtkLabel *label,
1834 g_return_if_fail (GTK_IS_LABEL (label));
1836 g_object_freeze_notify (G_OBJECT (label));
1838 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
1839 gtk_label_set_use_markup_internal (label, TRUE);
1840 gtk_label_set_use_underline_internal (label, TRUE);
1842 gtk_label_recalculate (label);
1844 g_object_thaw_notify (G_OBJECT (label));
1848 * gtk_label_get_text:
1849 * @label: a #GtkLabel
1851 * Fetches the text from a label widget, as displayed on the
1852 * screen. This does not include any embedded underlines
1853 * indicating mnemonics or Pango markup. (See gtk_label_get_label())
1855 * Return value: the text in the label widget. This is the internal
1856 * string used by the label, and must not be modified.
1858 G_CONST_RETURN gchar *
1859 gtk_label_get_text (GtkLabel *label)
1861 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1866 static PangoAttrList *
1867 gtk_label_pattern_to_attrs (GtkLabel *label,
1868 const gchar *pattern)
1871 const char *p = label->text;
1872 const char *q = pattern;
1873 PangoAttrList *attrs;
1875 attrs = pango_attr_list_new ();
1879 while (*p && *q && *q != '_')
1881 p = g_utf8_next_char (p);
1885 while (*p && *q && *q == '_')
1887 p = g_utf8_next_char (p);
1893 PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_LOW);
1894 attr->start_index = start - label->text;
1895 attr->end_index = p - label->text;
1897 pango_attr_list_insert (attrs, attr);
1907 gtk_label_set_pattern_internal (GtkLabel *label,
1908 const gchar *pattern)
1910 PangoAttrList *attrs;
1911 gboolean enable_mnemonics;
1913 g_return_if_fail (GTK_IS_LABEL (label));
1915 if (label->pattern_set)
1918 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
1919 "gtk-enable-mnemonics", &enable_mnemonics,
1922 if (enable_mnemonics && pattern)
1923 attrs = gtk_label_pattern_to_attrs (label, pattern);
1927 if (label->effective_attrs)
1928 pango_attr_list_unref (label->effective_attrs);
1929 label->effective_attrs = attrs;
1933 gtk_label_set_pattern (GtkLabel *label,
1934 const gchar *pattern)
1936 g_return_if_fail (GTK_IS_LABEL (label));
1938 label->pattern_set = FALSE;
1942 gtk_label_set_pattern_internal (label, pattern);
1943 label->pattern_set = TRUE;
1946 gtk_label_recalculate (label);
1948 gtk_label_clear_layout (label);
1949 gtk_widget_queue_resize (GTK_WIDGET (label));
1954 * gtk_label_set_justify:
1955 * @label: a #GtkLabel
1956 * @jtype: a #GtkJustification
1958 * Sets the alignment of the lines in the text of the label relative to
1959 * each other. %GTK_JUSTIFY_LEFT is the default value when the
1960 * widget is first created with gtk_label_new(). If you instead want
1961 * to set the alignment of the label as a whole, use
1962 * gtk_misc_set_alignment() instead. gtk_label_set_justify() has no
1963 * effect on labels containing only a single line.
1966 gtk_label_set_justify (GtkLabel *label,
1967 GtkJustification jtype)
1969 g_return_if_fail (GTK_IS_LABEL (label));
1970 g_return_if_fail (jtype >= GTK_JUSTIFY_LEFT && jtype <= GTK_JUSTIFY_FILL);
1972 if ((GtkJustification) label->jtype != jtype)
1974 label->jtype = jtype;
1976 /* No real need to be this drastic, but easier than duplicating the code */
1977 gtk_label_clear_layout (label);
1979 g_object_notify (G_OBJECT (label), "justify");
1980 gtk_widget_queue_resize (GTK_WIDGET (label));
1985 * gtk_label_get_justify:
1986 * @label: a #GtkLabel
1988 * Returns the justification of the label. See gtk_label_set_justify().
1990 * Return value: #GtkJustification
1993 gtk_label_get_justify (GtkLabel *label)
1995 g_return_val_if_fail (GTK_IS_LABEL (label), 0);
1997 return label->jtype;
2001 * gtk_label_set_ellipsize:
2002 * @label: a #GtkLabel
2003 * @mode: a #PangoEllipsizeMode
2005 * Sets the mode used to ellipsize (add an ellipsis: "...") to the text
2006 * if there is not enough space to render the entire string.
2011 gtk_label_set_ellipsize (GtkLabel *label,
2012 PangoEllipsizeMode mode)
2014 g_return_if_fail (GTK_IS_LABEL (label));
2015 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE && mode <= PANGO_ELLIPSIZE_END);
2017 if ((PangoEllipsizeMode) label->ellipsize != mode)
2019 label->ellipsize = mode;
2021 /* No real need to be this drastic, but easier than duplicating the code */
2022 gtk_label_clear_layout (label);
2024 g_object_notify (G_OBJECT (label), "ellipsize");
2025 gtk_widget_queue_resize (GTK_WIDGET (label));
2030 * gtk_label_get_ellipsize:
2031 * @label: a #GtkLabel
2033 * Returns the ellipsizing position of the label. See gtk_label_set_ellipsize().
2035 * Return value: #PangoEllipsizeMode
2040 gtk_label_get_ellipsize (GtkLabel *label)
2042 g_return_val_if_fail (GTK_IS_LABEL (label), PANGO_ELLIPSIZE_NONE);
2044 return label->ellipsize;
2048 * gtk_label_set_width_chars:
2049 * @label: a #GtkLabel
2050 * @n_chars: the new desired width, in characters.
2052 * Sets the desired width in characters of @label to @n_chars.
2057 gtk_label_set_width_chars (GtkLabel *label,
2060 GtkLabelPrivate *priv;
2062 g_return_if_fail (GTK_IS_LABEL (label));
2064 priv = GTK_LABEL_GET_PRIVATE (label);
2066 if (priv->width_chars != n_chars)
2068 priv->width_chars = n_chars;
2069 g_object_notify (G_OBJECT (label), "width-chars");
2070 gtk_label_invalidate_wrap_width (label);
2071 gtk_widget_queue_resize (GTK_WIDGET (label));
2076 * gtk_label_get_width_chars:
2077 * @label: a #GtkLabel
2079 * Retrieves the desired width of @label, in characters. See
2080 * gtk_label_set_width_chars().
2082 * Return value: the width of the label in characters.
2087 gtk_label_get_width_chars (GtkLabel *label)
2089 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2091 return GTK_LABEL_GET_PRIVATE (label)->width_chars;
2095 * gtk_label_set_max_width_chars:
2096 * @label: a #GtkLabel
2097 * @n_chars: the new desired maximum width, in characters.
2099 * Sets the desired maximum width in characters of @label to @n_chars.
2104 gtk_label_set_max_width_chars (GtkLabel *label,
2107 GtkLabelPrivate *priv;
2109 g_return_if_fail (GTK_IS_LABEL (label));
2111 priv = GTK_LABEL_GET_PRIVATE (label);
2113 if (priv->max_width_chars != n_chars)
2115 priv->max_width_chars = n_chars;
2117 g_object_notify (G_OBJECT (label), "max-width-chars");
2118 gtk_label_invalidate_wrap_width (label);
2119 gtk_widget_queue_resize (GTK_WIDGET (label));
2124 * gtk_label_get_max_width_chars:
2125 * @label: a #GtkLabel
2127 * Retrieves the desired maximum width of @label, in characters. See
2128 * gtk_label_set_width_chars().
2130 * Return value: the maximum width of the label in characters.
2135 gtk_label_get_max_width_chars (GtkLabel *label)
2137 g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2139 return GTK_LABEL_GET_PRIVATE (label)->max_width_chars;
2143 * gtk_label_set_line_wrap:
2144 * @label: a #GtkLabel
2145 * @wrap: the setting
2147 * Toggles line wrapping within the #GtkLabel widget. %TRUE makes it break
2148 * lines if text exceeds the widget's size. %FALSE lets the text get cut off
2149 * by the edge of the widget if it exceeds the widget size.
2151 * Note that setting line wrapping to %TRUE does not make the label
2152 * wrap at its parent container's width, because GTK+ widgets
2153 * conceptually can't make their requisition depend on the parent
2154 * container's size. For a label that wraps at a specific position,
2155 * set the label's width using gtk_widget_set_size_request().
2158 gtk_label_set_line_wrap (GtkLabel *label,
2161 g_return_if_fail (GTK_IS_LABEL (label));
2163 wrap = wrap != FALSE;
2165 if (label->wrap != wrap)
2169 gtk_label_clear_layout (label);
2170 gtk_widget_queue_resize (GTK_WIDGET (label));
2171 g_object_notify (G_OBJECT (label), "wrap");
2176 * gtk_label_get_line_wrap:
2177 * @label: a #GtkLabel
2179 * Returns whether lines in the label are automatically wrapped.
2180 * See gtk_label_set_line_wrap().
2182 * Return value: %TRUE if the lines of the label are automatically wrapped.
2185 gtk_label_get_line_wrap (GtkLabel *label)
2187 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
2193 * gtk_label_set_line_wrap_mode:
2194 * @label: a #GtkLabel
2195 * @wrap_mode: the line wrapping mode
2197 * If line wrapping is on (see gtk_label_set_line_wrap()) this controls how
2198 * the line wrapping is done. The default is %PANGO_WRAP_WORD which means
2199 * wrap on word boundaries.
2204 gtk_label_set_line_wrap_mode (GtkLabel *label,
2205 PangoWrapMode wrap_mode)
2207 g_return_if_fail (GTK_IS_LABEL (label));
2209 if (label->wrap_mode != wrap_mode)
2211 label->wrap_mode = wrap_mode;
2212 g_object_notify (G_OBJECT (label), "wrap-mode");
2214 gtk_widget_queue_resize (GTK_WIDGET (label));
2219 * gtk_label_get_line_wrap_mode:
2220 * @label: a #GtkLabel
2222 * Returns line wrap mode used by the label. See gtk_label_set_line_wrap_mode().
2224 * Return value: %TRUE if the lines of the label are automatically wrapped.
2229 gtk_label_get_line_wrap_mode (GtkLabel *label)
2231 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
2233 return label->wrap_mode;
2238 gtk_label_get (GtkLabel *label,
2241 g_return_if_fail (GTK_IS_LABEL (label));
2242 g_return_if_fail (str != NULL);
2248 gtk_label_destroy (GtkObject *object)
2250 GtkLabel *label = GTK_LABEL (object);
2252 gtk_label_set_mnemonic_widget (label, NULL);
2254 GTK_OBJECT_CLASS (gtk_label_parent_class)->destroy (object);
2258 gtk_label_finalize (GObject *object)
2260 GtkLabel *label = GTK_LABEL (object);
2262 g_free (label->label);
2263 g_free (label->text);
2266 g_object_unref (label->layout);
2269 pango_attr_list_unref (label->attrs);
2271 if (label->effective_attrs)
2272 pango_attr_list_unref (label->effective_attrs);
2274 g_free (label->select_info);
2276 G_OBJECT_CLASS (gtk_label_parent_class)->finalize (object);
2280 gtk_label_clear_layout (GtkLabel *label)
2284 g_object_unref (label->layout);
2285 label->layout = NULL;
2290 get_label_char_width (GtkLabel *label)
2292 GtkLabelPrivate *priv;
2293 PangoContext *context;
2294 PangoFontMetrics *metrics;
2295 gint char_width, digit_width, char_pixels, w;
2297 priv = GTK_LABEL_GET_PRIVATE (label);
2299 context = pango_layout_get_context (label->layout);
2300 metrics = pango_context_get_metrics (context, GTK_WIDGET (label)->style->font_desc,
2301 pango_context_get_language (context));
2303 char_width = pango_font_metrics_get_approximate_char_width (metrics);
2304 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
2305 char_pixels = MAX (char_width, digit_width);
2306 pango_font_metrics_unref (metrics);
2308 if (priv->width_chars < 0)
2310 PangoRectangle rect;
2312 pango_layout_set_width (label->layout, -1);
2313 pango_layout_get_extents (label->layout, NULL, &rect);
2315 w = char_pixels * MAX (priv->max_width_chars, 3);
2316 w = MIN (rect.width, w);
2320 /* enforce minimum width for ellipsized labels at ~3 chars */
2321 w = char_pixels * MAX (priv->width_chars, 3);
2328 gtk_label_invalidate_wrap_width (GtkLabel *label)
2330 GtkLabelPrivate *priv;
2332 priv = GTK_LABEL_GET_PRIVATE (label);
2334 priv->wrap_width = -1;
2338 get_label_wrap_width (GtkLabel *label)
2340 GtkLabelPrivate *priv;
2342 priv = GTK_LABEL_GET_PRIVATE (label);
2344 if (priv->wrap_width < 0)
2346 if (priv->width_chars > 0 || priv->max_width_chars > 0)
2347 priv->wrap_width = get_label_char_width (label);
2350 PangoLayout *layout;
2352 layout = gtk_widget_create_pango_layout (GTK_WIDGET (label),
2353 "This long string gives a good enough length for any line to have.");
2354 pango_layout_get_size (layout, &priv->wrap_width, NULL);
2355 g_object_unref (layout);
2359 return priv->wrap_width;
2363 gtk_label_ensure_layout (GtkLabel *label)
2366 PangoRectangle logical_rect;
2369 widget = GTK_WIDGET (label);
2371 rtl = gtk_widget_get_direction(widget) == GTK_TEXT_DIR_RTL;
2375 PangoAlignment align = PANGO_ALIGN_LEFT; /* Quiet gcc */
2376 gdouble angle = gtk_label_get_angle (label);
2378 if (angle != 0.0 && !label->wrap && !label->ellipsize && !label->select_info)
2380 /* We rotate the standard singleton PangoContext for the widget,
2381 * depending on the fact that it's meant pretty much exclusively
2384 PangoMatrix matrix = PANGO_MATRIX_INIT;
2386 pango_matrix_rotate (&matrix, angle);
2388 pango_context_set_matrix (gtk_widget_get_pango_context (widget), &matrix);
2390 label->have_transform = TRUE;
2394 if (label->have_transform)
2395 pango_context_set_matrix (gtk_widget_get_pango_context (widget), NULL);
2397 label->have_transform = FALSE;
2400 label->layout = gtk_widget_create_pango_layout (widget, label->text);
2402 if (label->effective_attrs)
2403 pango_layout_set_attributes (label->layout, label->effective_attrs);
2405 switch (label->jtype)
2407 case GTK_JUSTIFY_LEFT:
2408 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
2410 case GTK_JUSTIFY_RIGHT:
2411 align = rtl ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
2413 case GTK_JUSTIFY_CENTER:
2414 align = PANGO_ALIGN_CENTER;
2416 case GTK_JUSTIFY_FILL:
2417 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
2418 pango_layout_set_justify (label->layout, TRUE);
2421 g_assert_not_reached();
2424 pango_layout_set_alignment (label->layout, align);
2425 pango_layout_set_ellipsize (label->layout, label->ellipsize);
2426 pango_layout_set_single_paragraph_mode (label->layout, label->single_line_mode);
2428 if (label->ellipsize)
2429 pango_layout_set_width (label->layout,
2430 widget->allocation.width * PANGO_SCALE);
2431 else if (label->wrap)
2433 GtkWidgetAuxInfo *aux_info;
2434 gint longest_paragraph;
2437 pango_layout_set_wrap (label->layout, label->wrap_mode);
2439 aux_info = _gtk_widget_get_aux_info (widget, FALSE);
2440 if (aux_info && aux_info->width > 0)
2441 pango_layout_set_width (label->layout, aux_info->width * PANGO_SCALE);
2444 GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (label));
2447 pango_layout_set_width (label->layout, -1);
2448 pango_layout_get_extents (label->layout, NULL, &logical_rect);
2450 width = logical_rect.width;
2452 /* Try to guess a reasonable maximum width */
2453 longest_paragraph = width;
2455 wrap_width = get_label_wrap_width (label);
2456 width = MIN (width, wrap_width);
2458 PANGO_SCALE * (gdk_screen_get_width (screen) + 1) / 2);
2460 pango_layout_set_width (label->layout, width);
2461 pango_layout_get_extents (label->layout, NULL, &logical_rect);
2462 width = logical_rect.width;
2463 height = logical_rect.height;
2465 /* Unfortunately, the above may leave us with a very unbalanced looking paragraph,
2466 * so we try short search for a narrower width that leaves us with the same height
2468 if (longest_paragraph > 0)
2470 gint nlines, perfect_width;
2472 nlines = pango_layout_get_line_count (label->layout);
2473 perfect_width = (longest_paragraph + nlines - 1) / nlines;
2475 if (perfect_width < width)
2477 pango_layout_set_width (label->layout, perfect_width);
2478 pango_layout_get_extents (label->layout, NULL, &logical_rect);
2480 if (logical_rect.height <= height)
2481 width = logical_rect.width;
2484 gint mid_width = (perfect_width + width) / 2;
2486 if (mid_width > perfect_width)
2488 pango_layout_set_width (label->layout, mid_width);
2489 pango_layout_get_extents (label->layout, NULL, &logical_rect);
2491 if (logical_rect.height <= height)
2492 width = logical_rect.width;
2497 pango_layout_set_width (label->layout, width);
2500 else /* !label->wrap */
2501 pango_layout_set_width (label->layout, -1);
2506 gtk_label_size_request (GtkWidget *widget,
2507 GtkRequisition *requisition)
2509 GtkLabel *label = GTK_LABEL (widget);
2510 GtkLabelPrivate *priv;
2512 PangoRectangle logical_rect;
2513 GtkWidgetAuxInfo *aux_info;
2515 priv = GTK_LABEL_GET_PRIVATE (widget);
2518 * If word wrapping is on, then the height requisition can depend
2521 * - Any width set on the widget via gtk_widget_set_size_request().
2522 * - The padding of the widget (xpad, set by gtk_misc_set_padding)
2524 * Instead of trying to detect changes to these quantities, if we
2525 * are wrapping, we just rewrap for each size request. Since
2526 * size requisitions are cached by the GTK+ core, this is not
2531 gtk_label_clear_layout (label);
2533 gtk_label_ensure_layout (label);
2535 width = label->misc.xpad * 2;
2536 height = label->misc.ypad * 2;
2538 aux_info = _gtk_widget_get_aux_info (widget, FALSE);
2540 if (label->have_transform)
2542 PangoRectangle rect;
2543 PangoContext *context = pango_layout_get_context (label->layout);
2544 const PangoMatrix *matrix = pango_context_get_matrix (context);
2546 pango_layout_get_extents (label->layout, NULL, &rect);
2547 pango_matrix_transform_rectangle (matrix, &rect);
2548 pango_extents_to_pixels (&rect, NULL);
2550 requisition->width = width + rect.width;
2551 requisition->height = height + rect.height;
2556 pango_layout_get_extents (label->layout, NULL, &logical_rect);
2558 if ((label->wrap || label->ellipsize ||
2559 priv->width_chars > 0 || priv->max_width_chars > 0) &&
2560 aux_info && aux_info->width > 0)
2561 width += aux_info->width;
2562 else if (label->ellipsize || priv->width_chars > 0 || priv->max_width_chars > 0)
2564 width += PANGO_PIXELS (get_label_char_width (label));
2567 width += PANGO_PIXELS (logical_rect.width);
2569 if (label->single_line_mode)
2571 PangoContext *context;
2572 PangoFontMetrics *metrics;
2573 gint ascent, descent;
2575 context = pango_layout_get_context (label->layout);
2576 metrics = pango_context_get_metrics (context, widget->style->font_desc,
2577 pango_context_get_language (context));
2579 ascent = pango_font_metrics_get_ascent (metrics);
2580 descent = pango_font_metrics_get_descent (metrics);
2581 pango_font_metrics_unref (metrics);
2583 height += PANGO_PIXELS (ascent + descent);
2586 height += PANGO_PIXELS (logical_rect.height);
2588 requisition->width = width;
2589 requisition->height = height;
2593 gtk_label_size_allocate (GtkWidget *widget,
2594 GtkAllocation *allocation)
2598 label = GTK_LABEL (widget);
2600 GTK_WIDGET_CLASS (gtk_label_parent_class)->size_allocate (widget, allocation);
2602 if (label->ellipsize)
2607 PangoRectangle logical;
2609 width = (allocation->width - label->misc.xpad * 2) * PANGO_SCALE;
2611 pango_layout_set_width (label->layout, -1);
2612 pango_layout_get_extents (label->layout, NULL, &logical);
2614 if (logical.width > width)
2615 pango_layout_set_width (label->layout, width);
2619 if (label->select_info && label->select_info->window)
2621 gdk_window_move_resize (label->select_info->window,
2625 allocation->height);
2630 gtk_label_state_changed (GtkWidget *widget,
2631 GtkStateType prev_state)
2636 label = GTK_LABEL (widget);
2638 if (label->select_info)
2640 gtk_label_select_region (label, 0, 0);
2642 if (GTK_WIDGET_REALIZED (widget))
2644 if (GTK_WIDGET_IS_SENSITIVE (widget))
2645 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
2649 gdk_window_set_cursor (label->select_info->window, cursor);
2652 gdk_cursor_unref (cursor);
2656 if (GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed)
2657 GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed (widget, prev_state);
2661 gtk_label_style_set (GtkWidget *widget,
2662 GtkStyle *previous_style)
2664 GtkLabel *label = GTK_LABEL (widget);
2666 /* We have to clear the layout, fonts etc. may have changed */
2667 gtk_label_clear_layout (label);
2668 gtk_label_invalidate_wrap_width (label);
2672 gtk_label_direction_changed (GtkWidget *widget,
2673 GtkTextDirection previous_dir)
2675 GtkLabel *label = GTK_LABEL (widget);
2678 pango_layout_context_changed (label->layout);
2680 GTK_WIDGET_CLASS (gtk_label_parent_class)->direction_changed (widget, previous_dir);
2684 get_layout_location (GtkLabel *label,
2690 GtkLabelPrivate *priv;
2692 gint req_width, x, y;
2693 PangoRectangle logical;
2695 misc = GTK_MISC (label);
2696 widget = GTK_WIDGET (label);
2697 priv = GTK_LABEL_GET_PRIVATE (label);
2699 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2700 xalign = misc->xalign;
2702 xalign = 1.0 - misc->xalign;
2704 pango_layout_get_pixel_extents (label->layout, NULL, &logical);
2706 if (label->ellipsize || priv->width_chars > 0)
2710 width = pango_layout_get_width (label->layout);
2712 req_width = logical.width;
2714 req_width = MIN(PANGO_PIXELS (width), req_width);
2715 req_width += 2 * misc->xpad;
2718 req_width = widget->requisition.width;
2720 x = floor (widget->allocation.x + (gint)misc->xpad +
2721 xalign * (widget->allocation.width - req_width));
2723 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2724 x = MAX (x, widget->allocation.x + misc->xpad);
2726 x = MIN (x, widget->allocation.x + widget->allocation.width - misc->xpad);
2729 y = floor (widget->allocation.y + (gint)misc->ypad
2730 + MAX (((widget->allocation.height - widget->requisition.height) * misc->yalign),
2741 draw_insertion_cursor (GtkLabel *label,
2742 GdkRectangle *cursor_location,
2743 gboolean is_primary,
2744 PangoDirection direction,
2745 gboolean draw_arrow)
2747 GtkWidget *widget = GTK_WIDGET (label);
2748 GtkTextDirection text_dir;
2750 if (direction == PANGO_DIRECTION_LTR)
2751 text_dir = GTK_TEXT_DIR_LTR;
2753 text_dir = GTK_TEXT_DIR_RTL;
2755 gtk_draw_insertion_cursor (widget, widget->window, &(widget->allocation),
2757 is_primary, text_dir, draw_arrow);
2760 static PangoDirection
2761 get_cursor_direction (GtkLabel *label)
2765 g_assert (label->select_info);
2767 gtk_label_ensure_layout (label);
2769 for (l = pango_layout_get_lines_readonly (label->layout); l; l = l->next)
2771 PangoLayoutLine *line = l->data;
2773 /* If label->select_info->selection_end is at the very end of
2774 * the line, we don't know if the cursor is on this line or
2775 * the next without looking ahead at the next line. (End
2776 * of paragraph is different from line break.) But it's
2777 * definitely in this paragraph, which is good enough
2778 * to figure out the resolved direction.
2780 if (line->start_index + line->length >= label->select_info->selection_end)
2781 return line->resolved_dir;
2784 return PANGO_DIRECTION_LTR;
2788 gtk_label_draw_cursor (GtkLabel *label, gint xoffset, gint yoffset)
2790 if (label->select_info == NULL)
2793 if (GTK_WIDGET_DRAWABLE (label))
2795 GtkWidget *widget = GTK_WIDGET (label);
2797 PangoDirection keymap_direction;
2798 PangoDirection cursor_direction;
2799 PangoRectangle strong_pos, weak_pos;
2800 gboolean split_cursor;
2801 PangoRectangle *cursor1 = NULL;
2802 PangoRectangle *cursor2 = NULL;
2803 GdkRectangle cursor_location;
2804 PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
2805 PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
2807 keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gtk_widget_get_display (widget)));
2808 cursor_direction = get_cursor_direction (label);
2810 gtk_label_ensure_layout (label);
2812 pango_layout_get_cursor_pos (label->layout, label->select_info->selection_end,
2813 &strong_pos, &weak_pos);
2815 g_object_get (gtk_widget_get_settings (widget),
2816 "gtk-split-cursor", &split_cursor,
2819 dir1 = cursor_direction;
2823 cursor1 = &strong_pos;
2825 if (strong_pos.x != weak_pos.x ||
2826 strong_pos.y != weak_pos.y)
2828 dir2 = (cursor_direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
2829 cursor2 = &weak_pos;
2834 if (keymap_direction == cursor_direction)
2835 cursor1 = &strong_pos;
2837 cursor1 = &weak_pos;
2840 cursor_location.x = xoffset + PANGO_PIXELS (cursor1->x);
2841 cursor_location.y = yoffset + PANGO_PIXELS (cursor1->y);
2842 cursor_location.width = 0;
2843 cursor_location.height = PANGO_PIXELS (cursor1->height);
2845 draw_insertion_cursor (label,
2846 &cursor_location, TRUE, dir1,
2847 dir2 != PANGO_DIRECTION_NEUTRAL);
2849 if (dir2 != PANGO_DIRECTION_NEUTRAL)
2851 cursor_location.x = xoffset + PANGO_PIXELS (cursor2->x);
2852 cursor_location.y = yoffset + PANGO_PIXELS (cursor2->y);
2853 cursor_location.width = 0;
2854 cursor_location.height = PANGO_PIXELS (cursor2->height);
2856 draw_insertion_cursor (label,
2857 &cursor_location, FALSE, dir2,
2865 gtk_label_expose (GtkWidget *widget,
2866 GdkEventExpose *event)
2871 g_return_val_if_fail (GTK_IS_LABEL (widget), FALSE);
2872 g_return_val_if_fail (event != NULL, FALSE);
2874 label = GTK_LABEL (widget);
2876 gtk_label_ensure_layout (label);
2878 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget) &&
2879 label->text && (*label->text != '\0'))
2881 get_layout_location (label, &x, &y);
2883 gtk_paint_layout (widget->style,
2885 GTK_WIDGET_STATE (widget),
2893 if (label->select_info &&
2894 (label->select_info->selection_anchor !=
2895 label->select_info->selection_end))
2901 range[0] = label->select_info->selection_anchor;
2902 range[1] = label->select_info->selection_end;
2904 if (range[0] > range[1])
2906 gint tmp = range[0];
2907 range[0] = range[1];
2911 clip = gdk_pango_layout_get_clip_region (label->layout,
2915 gdk_region_intersect (clip, event->region);
2917 /* FIXME should use gtk_paint, but it can't use a clip
2921 gdk_gc_set_clip_region (widget->style->black_gc, clip);
2924 state = GTK_STATE_SELECTED;
2925 if (!GTK_WIDGET_HAS_FOCUS (widget))
2926 state = GTK_STATE_ACTIVE;
2928 gdk_draw_layout_with_colors (widget->window,
2929 widget->style->black_gc,
2932 &widget->style->text[state],
2933 &widget->style->base[state]);
2935 gdk_gc_set_clip_region (widget->style->black_gc, NULL);
2936 gdk_region_destroy (clip);
2938 else if (label->select_info && GTK_WIDGET_HAS_FOCUS (widget))
2939 gtk_label_draw_cursor (label, x, y);
2946 gtk_label_set_uline_text_internal (GtkLabel *label,
2949 guint accel_key = GDK_VoidSymbol;
2954 gchar *dest, *pattern_dest;
2955 gboolean underscore;
2957 g_return_if_fail (GTK_IS_LABEL (label));
2958 g_return_if_fail (str != NULL);
2960 /* Split text into the base text and a separate pattern
2964 new_str = g_new (gchar, strlen (str) + 1);
2965 pattern = g_new (gchar, g_utf8_strlen (str, -1) + 1);
2974 pattern_dest = pattern;
2981 c = g_utf8_get_char (src);
2982 if (c == (gunichar)-1)
2984 g_warning ("Invalid input string");
2989 next_src = g_utf8_next_char (src);
2994 *pattern_dest++ = ' ';
2997 *pattern_dest++ = '_';
2998 if (accel_key == GDK_VoidSymbol)
2999 accel_key = gdk_keyval_to_lower (gdk_unicode_to_keyval (c));
3002 while (src < next_src)
3016 while (src < next_src)
3019 *pattern_dest++ = ' ';
3026 gtk_label_set_text_internal (label, new_str);
3027 gtk_label_set_pattern_internal (label, pattern);
3031 label->mnemonic_keyval = accel_key;
3035 gtk_label_parse_uline (GtkLabel *label,
3040 g_return_val_if_fail (GTK_IS_LABEL (label), GDK_VoidSymbol);
3041 g_return_val_if_fail (str != NULL, GDK_VoidSymbol);
3043 g_object_freeze_notify (G_OBJECT (label));
3045 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
3046 gtk_label_set_use_markup_internal (label, FALSE);
3047 gtk_label_set_use_underline_internal (label, TRUE);
3049 gtk_label_recalculate (label);
3051 keyval = label->mnemonic_keyval;
3052 if (keyval != GDK_VoidSymbol)
3054 label->mnemonic_keyval = GDK_VoidSymbol;
3055 gtk_label_setup_mnemonic (label, keyval);
3056 g_object_notify (G_OBJECT (label), "mnemonic-keyval");
3059 g_object_thaw_notify (G_OBJECT (label));
3065 * gtk_label_set_text_with_mnemonic:
3066 * @label: a #GtkLabel
3069 * Sets the label's text from the string @str.
3070 * If characters in @str are preceded by an underscore, they are underlined
3071 * indicating that they represent a keyboard accelerator called a mnemonic.
3072 * The mnemonic key can be used to activate another widget, chosen
3073 * automatically, or explicitly using gtk_label_set_mnemonic_widget().
3076 gtk_label_set_text_with_mnemonic (GtkLabel *label,
3079 g_return_if_fail (GTK_IS_LABEL (label));
3080 g_return_if_fail (str != NULL);
3082 g_object_freeze_notify (G_OBJECT (label));
3084 gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
3085 gtk_label_set_use_markup_internal (label, FALSE);
3086 gtk_label_set_use_underline_internal (label, TRUE);
3088 gtk_label_recalculate (label);
3090 g_object_thaw_notify (G_OBJECT (label));
3094 gtk_label_realize (GtkWidget *widget)
3098 label = GTK_LABEL (widget);
3100 GTK_WIDGET_CLASS (gtk_label_parent_class)->realize (widget);
3102 if (label->select_info)
3103 gtk_label_create_window (label);
3107 gtk_label_unrealize (GtkWidget *widget)
3111 label = GTK_LABEL (widget);
3113 if (label->select_info)
3114 gtk_label_destroy_window (label);
3116 GTK_WIDGET_CLASS (gtk_label_parent_class)->unrealize (widget);
3120 gtk_label_map (GtkWidget *widget)
3124 label = GTK_LABEL (widget);
3126 GTK_WIDGET_CLASS (gtk_label_parent_class)->map (widget);
3128 if (label->select_info)
3129 gdk_window_show (label->select_info->window);
3133 gtk_label_unmap (GtkWidget *widget)
3137 label = GTK_LABEL (widget);
3139 if (label->select_info)
3140 gdk_window_hide (label->select_info->window);
3142 GTK_WIDGET_CLASS (gtk_label_parent_class)->unmap (widget);
3146 window_to_layout_coords (GtkLabel *label,
3153 widget = GTK_WIDGET (label);
3155 /* get layout location in widget->window coords */
3156 get_layout_location (label, &lx, &ly);
3160 *x += widget->allocation.x; /* go to widget->window */
3161 *x -= lx; /* go to layout */
3166 *y += widget->allocation.y; /* go to widget->window */
3167 *y -= ly; /* go to layout */
3173 layout_to_window_coords (GtkLabel *label,
3180 widget = GTK_WIDGET (label);
3182 /* get layout location in widget->window coords */
3183 get_layout_location (label, &lx, &ly);
3187 *x += lx; /* go to widget->window */
3188 *x -= widget->allocation.x; /* go to selection window */
3193 *y += ly; /* go to widget->window */
3194 *y -= widget->allocation.y; /* go to selection window */
3200 get_layout_index (GtkLabel *label,
3206 const gchar *cluster;
3207 const gchar *cluster_end;
3211 gtk_label_ensure_layout (label);
3213 window_to_layout_coords (label, &x, &y);
3218 pango_layout_xy_to_index (label->layout,
3223 cluster = label->text + *index;
3224 cluster_end = cluster;
3227 cluster_end = g_utf8_next_char (cluster_end);
3231 *index += (cluster_end - cluster);
3235 gtk_label_select_word (GtkLabel *label)
3239 gint start_index = gtk_label_move_backward_word (label, label->select_info->selection_end);
3240 gint end_index = gtk_label_move_forward_word (label, label->select_info->selection_end);
3242 min = MIN (label->select_info->selection_anchor,
3243 label->select_info->selection_end);
3244 max = MAX (label->select_info->selection_anchor,
3245 label->select_info->selection_end);
3247 min = MIN (min, start_index);
3248 max = MAX (max, end_index);
3250 gtk_label_select_region_index (label, min, max);
3254 gtk_label_grab_focus (GtkWidget *widget)
3257 gboolean select_on_focus;
3259 label = GTK_LABEL (widget);
3261 if (label->select_info == NULL)
3264 GTK_WIDGET_CLASS (gtk_label_parent_class)->grab_focus (widget);
3266 g_object_get (gtk_widget_get_settings (widget),
3267 "gtk-label-select-on-focus",
3271 if (select_on_focus && !label->in_click)
3272 gtk_label_select_region (label, 0, -1);
3276 gtk_label_button_press (GtkWidget *widget,
3277 GdkEventButton *event)
3283 label = GTK_LABEL (widget);
3285 if (label->select_info == NULL)
3288 label->select_info->in_drag = FALSE;
3289 if (event->button == 1)
3291 if (!GTK_WIDGET_HAS_FOCUS (widget))
3293 label->in_click = TRUE;
3294 gtk_widget_grab_focus (widget);
3295 label->in_click = FALSE;
3298 if (event->type == GDK_3BUTTON_PRESS)
3300 gtk_label_select_region_index (label, 0, strlen (label->text));
3304 if (event->type == GDK_2BUTTON_PRESS)
3306 gtk_label_select_word (label);
3310 get_layout_index (label, event->x, event->y, &index);
3312 min = MIN (label->select_info->selection_anchor,
3313 label->select_info->selection_end);
3314 max = MAX (label->select_info->selection_anchor,
3315 label->select_info->selection_end);
3317 if ((label->select_info->selection_anchor !=
3318 label->select_info->selection_end) &&
3319 (event->state & GDK_SHIFT_MASK))
3321 /* extend (same as motion) */
3322 min = MIN (min, index);
3323 max = MAX (max, index);
3325 /* ensure the anchor is opposite index */
3333 gtk_label_select_region_index (label, min, max);
3337 if (event->type == GDK_3BUTTON_PRESS)
3338 gtk_label_select_region_index (label, 0, strlen (label->text));
3339 else if (event->type == GDK_2BUTTON_PRESS)
3340 gtk_label_select_word (label);
3341 else if (min < max && min <= index && index <= max)
3343 label->select_info->in_drag = TRUE;
3344 label->select_info->drag_start_x = event->x;
3345 label->select_info->drag_start_y = event->y;
3348 /* start a replacement */
3349 gtk_label_select_region_index (label, index, index);
3354 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
3356 gtk_label_do_popup (label, event);
3365 gtk_label_button_release (GtkWidget *widget,
3366 GdkEventButton *event)
3369 GtkLabel *label = GTK_LABEL (widget);
3372 if (label->select_info == NULL)
3375 if (label->select_info->in_drag)
3377 label->select_info->in_drag = 0;
3379 get_layout_index (label, event->x, event->y, &index);
3380 gtk_label_select_region_index (label, index, index);
3385 if (event->button != 1)
3388 /* The goal here is to return TRUE iff we ate the
3389 * button press to start selecting.
3396 drag_begin_cb (GtkWidget *widget,
3397 GdkDragContext *context,
3401 GdkPixmap *pixmap = NULL;
3403 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
3405 label = GTK_LABEL (widget);
3407 if ((label->select_info->selection_anchor !=
3408 label->select_info->selection_end) &&
3414 start = MIN (label->select_info->selection_anchor,
3415 label->select_info->selection_end);
3416 end = MAX (label->select_info->selection_anchor,
3417 label->select_info->selection_end);
3419 len = strlen (label->text);
3427 pixmap = _gtk_text_util_create_drag_icon (widget,
3428 label->text + start,
3433 gtk_drag_set_icon_pixmap (context,
3434 gdk_drawable_get_colormap (pixmap),
3439 gtk_drag_set_icon_default (context);
3442 g_object_unref (pixmap);
3446 gtk_label_motion (GtkWidget *widget,
3447 GdkEventMotion *event)
3453 label = GTK_LABEL (widget);
3455 if (label->select_info == NULL)
3459 if ((event->state & GDK_BUTTON1_MASK) == 0)
3462 gdk_window_get_pointer (label->select_info->window,
3465 if (label->select_info->in_drag)
3467 if (gtk_drag_check_threshold (widget,
3468 label->select_info->drag_start_x,
3469 label->select_info->drag_start_y,
3470 event->x, event->y))
3472 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
3474 gtk_target_list_add_text_targets (target_list, 0);
3476 g_signal_connect (widget, "drag-begin",
3477 G_CALLBACK (drag_begin_cb), NULL);
3478 gtk_drag_begin (widget, target_list,
3480 1, (GdkEvent *)event);
3482 label->select_info->in_drag = FALSE;
3484 gtk_target_list_unref (target_list);
3489 get_layout_index (label, x, y, &index);
3491 gtk_label_select_region_index (label,
3492 label->select_info->selection_anchor,
3500 gtk_label_create_window (GtkLabel *label)
3503 GdkWindowAttr attributes;
3504 gint attributes_mask;
3506 g_assert (label->select_info);
3507 g_assert (GTK_WIDGET_REALIZED (label));
3509 if (label->select_info->window)
3512 widget = GTK_WIDGET (label);
3514 attributes.x = widget->allocation.x;
3515 attributes.y = widget->allocation.y;
3516 attributes.width = widget->allocation.width;
3517 attributes.height = widget->allocation.height;
3518 attributes.window_type = GDK_WINDOW_CHILD;
3519 attributes.wclass = GDK_INPUT_ONLY;
3520 attributes.override_redirect = TRUE;
3521 attributes.event_mask = gtk_widget_get_events (widget) |
3522 GDK_BUTTON_PRESS_MASK |
3523 GDK_BUTTON_RELEASE_MASK |
3524 GDK_BUTTON_MOTION_MASK;
3525 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
3526 if (GTK_WIDGET_IS_SENSITIVE (widget))
3528 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
3530 attributes_mask |= GDK_WA_CURSOR;
3534 label->select_info->window = gdk_window_new (widget->window,
3535 &attributes, attributes_mask);
3536 gdk_window_set_user_data (label->select_info->window, widget);
3538 if (attributes_mask & GDK_WA_CURSOR)
3539 gdk_cursor_unref (attributes.cursor);
3543 gtk_label_destroy_window (GtkLabel *label)
3545 g_assert (label->select_info);
3547 if (label->select_info->window == NULL)
3550 gdk_window_set_user_data (label->select_info->window, NULL);
3551 gdk_window_destroy (label->select_info->window);
3552 label->select_info->window = NULL;
3556 * gtk_label_set_selectable:
3557 * @label: a #GtkLabel
3558 * @setting: %TRUE to allow selecting text in the label
3560 * Selectable labels allow the user to select text from the label, for
3564 gtk_label_set_selectable (GtkLabel *label,
3567 gboolean old_setting;
3569 g_return_if_fail (GTK_IS_LABEL (label));
3571 setting = setting != FALSE;
3572 old_setting = label->select_info != NULL;
3576 if (label->select_info == NULL)
3578 label->select_info = g_new0 (GtkLabelSelectionInfo, 1);
3580 GTK_WIDGET_SET_FLAGS (label, GTK_CAN_FOCUS);
3582 if (GTK_WIDGET_REALIZED (label))
3583 gtk_label_create_window (label);
3585 if (GTK_WIDGET_MAPPED (label))
3586 gdk_window_show (label->select_info->window);
3591 if (label->select_info)
3593 /* unselect, to give up the selection */
3594 gtk_label_select_region (label, 0, 0);
3596 if (label->select_info->window)
3598 gtk_label_destroy_window (label);
3601 g_free (label->select_info);
3603 label->select_info = NULL;
3605 GTK_WIDGET_UNSET_FLAGS (label, GTK_CAN_FOCUS);
3608 if (setting != old_setting)
3610 g_object_freeze_notify (G_OBJECT (label));
3611 g_object_notify (G_OBJECT (label), "selectable");
3612 g_object_notify (G_OBJECT (label), "cursor-position");
3613 g_object_notify (G_OBJECT (label), "selection-bound");
3614 g_object_thaw_notify (G_OBJECT (label));
3615 gtk_widget_queue_draw (GTK_WIDGET (label));
3620 * gtk_label_get_selectable:
3621 * @label: a #GtkLabel
3623 * Gets the value set by gtk_label_set_selectable().
3625 * Return value: %TRUE if the user can copy text from the label
3628 gtk_label_get_selectable (GtkLabel *label)
3630 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
3632 return label->select_info != NULL;
3636 free_angle (gpointer angle)
3638 g_slice_free (gdouble, angle);
3642 * gtk_label_set_angle:
3643 * @label: a #GtkLabel
3644 * @angle: the angle that the baseline of the label makes with
3645 * the horizontal, in degrees, measured counterclockwise
3647 * Sets the angle of rotation for the label. An angle of 90 reads from
3648 * from bottom to top, an angle of 270, from top to bottom. The angle
3649 * setting for the label is ignored if the label is selectable,
3650 * wrapped, or ellipsized.
3655 gtk_label_set_angle (GtkLabel *label,
3658 gdouble *label_angle;
3660 g_return_if_fail (GTK_IS_LABEL (label));
3662 label_angle = (gdouble *)g_object_get_qdata (G_OBJECT (label), quark_angle);
3666 label_angle = g_slice_new (gdouble);
3668 g_object_set_qdata_full (G_OBJECT (label), quark_angle,
3669 label_angle, free_angle);
3672 /* Canonicalize to [0,360]. We don't canonicalize 360 to 0, because
3673 * double property ranges are inclusive, and changing 360 to 0 would
3674 * make a property editor behave strangely.
3676 if (angle < 0 || angle > 360.0)
3677 angle = angle - 360. * floor (angle / 360.);
3679 if (*label_angle != angle)
3681 *label_angle = angle;
3683 gtk_label_clear_layout (label);
3684 gtk_widget_queue_resize (GTK_WIDGET (label));
3686 g_object_notify (G_OBJECT (label), "angle");
3691 * gtk_label_get_angle:
3692 * @label: a #GtkLabel
3694 * Gets the angle of rotation for the label. See
3695 * gtk_label_set_angle().
3697 * Return value: the angle of rotation for the label
3702 gtk_label_get_angle (GtkLabel *label)
3706 g_return_val_if_fail (GTK_IS_LABEL (label), 0.0);
3708 angle = (gdouble *)g_object_get_qdata (G_OBJECT (label), quark_angle);
3717 gtk_label_set_selection_text (GtkLabel *label,
3718 GtkSelectionData *selection_data)
3720 if ((label->select_info->selection_anchor !=
3721 label->select_info->selection_end) &&
3727 start = MIN (label->select_info->selection_anchor,
3728 label->select_info->selection_end);
3729 end = MAX (label->select_info->selection_anchor,
3730 label->select_info->selection_end);
3732 len = strlen (label->text);
3740 gtk_selection_data_set_text (selection_data,
3741 label->text + start,
3747 gtk_label_drag_data_get (GtkWidget *widget,
3748 GdkDragContext *context,
3749 GtkSelectionData *selection_data,
3753 gtk_label_set_selection_text (GTK_LABEL (widget), selection_data);
3757 get_text_callback (GtkClipboard *clipboard,
3758 GtkSelectionData *selection_data,
3760 gpointer user_data_or_owner)
3762 gtk_label_set_selection_text (GTK_LABEL (user_data_or_owner), selection_data);
3766 clear_text_callback (GtkClipboard *clipboard,
3767 gpointer user_data_or_owner)
3771 label = GTK_LABEL (user_data_or_owner);
3773 if (label->select_info)
3775 label->select_info->selection_anchor = label->select_info->selection_end;
3777 gtk_widget_queue_draw (GTK_WIDGET (label));
3782 gtk_label_select_region_index (GtkLabel *label,
3786 g_return_if_fail (GTK_IS_LABEL (label));
3788 if (label->select_info)
3790 GtkClipboard *clipboard;
3792 if (label->select_info->selection_anchor == anchor_index &&
3793 label->select_info->selection_end == end_index)
3796 label->select_info->selection_anchor = anchor_index;
3797 label->select_info->selection_end = end_index;
3799 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label),
3800 GDK_SELECTION_PRIMARY);
3802 if (anchor_index != end_index)
3804 GtkTargetList *list;
3805 GtkTargetEntry *targets;
3808 list = gtk_target_list_new (NULL, 0);
3809 gtk_target_list_add_text_targets (list, 0);
3810 targets = gtk_target_table_new_from_list (list, &n_targets);
3812 gtk_clipboard_set_with_owner (clipboard,
3815 clear_text_callback,
3818 gtk_target_table_free (targets, n_targets);
3819 gtk_target_list_unref (list);
3823 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (label))
3824 gtk_clipboard_clear (clipboard);
3827 gtk_widget_queue_draw (GTK_WIDGET (label));
3829 g_object_freeze_notify (G_OBJECT (label));
3830 g_object_notify (G_OBJECT (label), "cursor-position");
3831 g_object_notify (G_OBJECT (label), "selection-bound");
3832 g_object_thaw_notify (G_OBJECT (label));
3837 * gtk_label_select_region:
3838 * @label: a #GtkLabel
3839 * @start_offset: start offset (in characters not bytes)
3840 * @end_offset: end offset (in characters not bytes)
3842 * Selects a range of characters in the label, if the label is selectable.
3843 * See gtk_label_set_selectable(). If the label is not selectable,
3844 * this function has no effect. If @start_offset or
3845 * @end_offset are -1, then the end of the label will be substituted.
3848 gtk_label_select_region (GtkLabel *label,
3852 g_return_if_fail (GTK_IS_LABEL (label));
3854 if (label->text && label->select_info)
3856 if (start_offset < 0)
3857 start_offset = g_utf8_strlen (label->text, -1);
3860 end_offset = g_utf8_strlen (label->text, -1);
3862 gtk_label_select_region_index (label,
3863 g_utf8_offset_to_pointer (label->text, start_offset) - label->text,
3864 g_utf8_offset_to_pointer (label->text, end_offset) - label->text);
3869 * gtk_label_get_selection_bounds:
3870 * @label: a #GtkLabel
3871 * @start: return location for start of selection, as a character offset
3872 * @end: return location for end of selection, as a character offset
3874 * Gets the selected range of characters in the label, returning %TRUE
3875 * if there's a selection.
3877 * Return value: %TRUE if selection is non-empty
3880 gtk_label_get_selection_bounds (GtkLabel *label,
3884 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
3886 if (label->select_info == NULL)
3888 /* not a selectable label */
3898 gint start_index, end_index;
3899 gint start_offset, end_offset;
3902 start_index = MIN (label->select_info->selection_anchor,
3903 label->select_info->selection_end);
3904 end_index = MAX (label->select_info->selection_anchor,
3905 label->select_info->selection_end);
3907 len = strlen (label->text);
3909 if (end_index > len)
3912 if (start_index > len)
3915 start_offset = g_utf8_strlen (label->text, start_index);
3916 end_offset = g_utf8_strlen (label->text, end_index);
3918 if (start_offset > end_offset)
3920 gint tmp = start_offset;
3921 start_offset = end_offset;
3926 *start = start_offset;
3931 return start_offset != end_offset;
3937 * gtk_label_get_layout:
3938 * @label: a #GtkLabel
3940 * Gets the #PangoLayout used to display the label.
3941 * The layout is useful to e.g. convert text positions to
3942 * pixel positions, in combination with gtk_label_get_layout_offsets().
3943 * The returned layout is owned by the label so need not be
3944 * freed by the caller.
3946 * Return value: the #PangoLayout for this label
3949 gtk_label_get_layout (GtkLabel *label)
3951 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
3953 gtk_label_ensure_layout (label);
3955 return label->layout;
3959 * gtk_label_get_layout_offsets:
3960 * @label: a #GtkLabel
3961 * @x: location to store X offset of layout, or %NULL
3962 * @y: location to store Y offset of layout, or %NULL
3964 * Obtains the coordinates where the label will draw the #PangoLayout
3965 * representing the text in the label; useful to convert mouse events
3966 * into coordinates inside the #PangoLayout, e.g. to take some action
3967 * if some part of the label is clicked. Of course you will need to
3968 * create a #GtkEventBox to receive the events, and pack the label
3969 * inside it, since labels are a #GTK_NO_WINDOW widget. Remember
3970 * when using the #PangoLayout functions you need to convert to
3971 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
3974 gtk_label_get_layout_offsets (GtkLabel *label,
3978 g_return_if_fail (GTK_IS_LABEL (label));
3980 gtk_label_ensure_layout (label);
3982 get_layout_location (label, x, y);
3986 * gtk_label_set_use_markup:
3987 * @label: a #GtkLabel
3988 * @setting: %TRUE if the label's text should be parsed for markup.
3990 * Sets whether the text of the label contains markup in <link
3991 * linkend="PangoMarkupFormat">Pango's text markup
3992 * language</link>. See gtk_label_set_markup().
3995 gtk_label_set_use_markup (GtkLabel *label,
3998 g_return_if_fail (GTK_IS_LABEL (label));
4000 gtk_label_set_use_markup_internal (label, setting);
4001 gtk_label_recalculate (label);
4005 * gtk_label_get_use_markup:
4006 * @label: a #GtkLabel
4008 * Returns whether the label's text is interpreted as marked up with
4009 * the <link linkend="PangoMarkupFormat">Pango text markup
4010 * language</link>. See gtk_label_set_use_markup ().
4012 * Return value: %TRUE if the label's text will be parsed for markup.
4015 gtk_label_get_use_markup (GtkLabel *label)
4017 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
4019 return label->use_markup;
4023 * gtk_label_set_use_underline:
4024 * @label: a #GtkLabel
4025 * @setting: %TRUE if underlines in the text indicate mnemonics
4027 * If true, an underline in the text indicates the next character should be
4028 * used for the mnemonic accelerator key.
4031 gtk_label_set_use_underline (GtkLabel *label,
4034 g_return_if_fail (GTK_IS_LABEL (label));
4036 gtk_label_set_use_underline_internal (label, setting);
4037 gtk_label_recalculate (label);
4041 * gtk_label_get_use_underline:
4042 * @label: a #GtkLabel
4044 * Returns whether an embedded underline in the label indicates a
4045 * mnemonic. See gtk_label_set_use_underline().
4047 * Return value: %TRUE whether an embedded underline in the label indicates
4048 * the mnemonic accelerator keys.
4051 gtk_label_get_use_underline (GtkLabel *label)
4053 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
4055 return label->use_underline;
4059 * gtk_label_set_single_line_mode:
4060 * @label: a #GtkLabel
4061 * @single_line_mode: %TRUE if the label should be in single line mode
4063 * Sets whether the label is in single line mode.
4068 gtk_label_set_single_line_mode (GtkLabel *label,
4069 gboolean single_line_mode)
4071 g_return_if_fail (GTK_IS_LABEL (label));
4073 single_line_mode = single_line_mode != FALSE;
4075 if (label->single_line_mode != single_line_mode)
4077 label->single_line_mode = single_line_mode;
4079 gtk_label_clear_layout (label);
4080 gtk_widget_queue_resize (GTK_WIDGET (label));
4082 g_object_notify (G_OBJECT (label), "single-line-mode");
4087 * gtk_label_get_single_line_mode:
4088 * @label: a #GtkLabel
4090 * Returns whether the label is in single line mode.
4092 * Return value: %TRUE when the label is in single line mode.
4097 gtk_label_get_single_line_mode (GtkLabel *label)
4099 g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
4101 return label->single_line_mode;
4104 /* Compute the X position for an offset that corresponds to the "more important
4105 * cursor position for that offset. We use this when trying to guess to which
4106 * end of the selection we should go to when the user hits the left or
4110 get_better_cursor (GtkLabel *label,
4115 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
4116 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
4117 PangoDirection cursor_direction = get_cursor_direction (label);
4118 gboolean split_cursor;
4119 PangoRectangle strong_pos, weak_pos;
4121 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
4122 "gtk-split-cursor", &split_cursor,
4125 gtk_label_ensure_layout (label);
4127 pango_layout_get_cursor_pos (label->layout, index,
4128 &strong_pos, &weak_pos);
4132 *x = strong_pos.x / PANGO_SCALE;
4133 *y = strong_pos.y / PANGO_SCALE;
4137 if (keymap_direction == cursor_direction)
4139 *x = strong_pos.x / PANGO_SCALE;
4140 *y = strong_pos.y / PANGO_SCALE;
4144 *x = weak_pos.x / PANGO_SCALE;
4145 *y = weak_pos.y / PANGO_SCALE;
4152 gtk_label_move_logically (GtkLabel *label,
4156 gint offset = g_utf8_pointer_to_offset (label->text,
4157 label->text + start);
4161 PangoLogAttr *log_attrs;
4165 gtk_label_ensure_layout (label);
4167 length = g_utf8_strlen (label->text, -1);
4169 pango_layout_get_log_attrs (label->layout, &log_attrs, &n_attrs);
4171 while (count > 0 && offset < length)
4175 while (offset < length && !log_attrs[offset].is_cursor_position);
4179 while (count < 0 && offset > 0)
4183 while (offset > 0 && !log_attrs[offset].is_cursor_position);
4191 return g_utf8_offset_to_pointer (label->text, offset) - label->text;
4195 gtk_label_move_visually (GtkLabel *label,
4205 int new_index, new_trailing;
4206 gboolean split_cursor;
4209 gtk_label_ensure_layout (label);
4211 g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
4212 "gtk-split-cursor", &split_cursor,
4219 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
4220 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
4222 strong = keymap_direction == get_cursor_direction (label);
4227 pango_layout_move_cursor_visually (label->layout, strong, index, 0, 1, &new_index, &new_trailing);
4232 pango_layout_move_cursor_visually (label->layout, strong, index, 0, -1, &new_index, &new_trailing);
4236 if (new_index < 0 || new_index == G_MAXINT)
4241 while (new_trailing--)
4242 index = g_utf8_next_char (label->text + new_index) - label->text;
4249 gtk_label_move_forward_word (GtkLabel *label,
4252 gint new_pos = g_utf8_pointer_to_offset (label->text,
4253 label->text + start);
4256 length = g_utf8_strlen (label->text, -1);
4257 if (new_pos < length)
4259 PangoLogAttr *log_attrs;
4262 gtk_label_ensure_layout (label);
4264 pango_layout_get_log_attrs (label->layout, &log_attrs, &n_attrs);
4266 /* Find the next word end */
4268 while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
4274 return g_utf8_offset_to_pointer (label->text, new_pos) - label->text;
4279 gtk_label_move_backward_word (GtkLabel *label,
4282 gint new_pos = g_utf8_pointer_to_offset (label->text,
4283 label->text + start);
4287 PangoLogAttr *log_attrs;
4290 gtk_label_ensure_layout (label);
4292 pango_layout_get_log_attrs (label->layout, &log_attrs, &n_attrs);
4296 /* Find the previous word beginning */
4297 while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
4303 return g_utf8_offset_to_pointer (label->text, new_pos) - label->text;
4307 gtk_label_move_cursor (GtkLabel *label,
4308 GtkMovementStep step,
4310 gboolean extend_selection)
4315 if (label->select_info == NULL)
4318 old_pos = new_pos = label->select_info->selection_end;
4320 if (label->select_info->selection_end != label->select_info->selection_anchor &&
4323 /* If we have a current selection and aren't extending it, move to the
4324 * start/or end of the selection as appropriate
4328 case GTK_MOVEMENT_VISUAL_POSITIONS:
4331 gint anchor_x, anchor_y;
4332 gboolean end_is_left;
4334 get_better_cursor (label, label->select_info->selection_end, &end_x, &end_y);
4335 get_better_cursor (label, label->select_info->selection_anchor, &anchor_x, &anchor_y);
4337 end_is_left = (end_y < anchor_y) || (end_y == anchor_y && end_x < anchor_x);
4340 new_pos = end_is_left ? label->select_info->selection_end : label->select_info->selection_anchor;
4342 new_pos = !end_is_left ? label->select_info->selection_end : label->select_info->selection_anchor;
4345 case GTK_MOVEMENT_LOGICAL_POSITIONS:
4346 case GTK_MOVEMENT_WORDS:
4348 new_pos = MIN (label->select_info->selection_end, label->select_info->selection_anchor);
4350 new_pos = MAX (label->select_info->selection_end, label->select_info->selection_anchor);
4352 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4353 case GTK_MOVEMENT_PARAGRAPH_ENDS:
4354 case GTK_MOVEMENT_BUFFER_ENDS:
4355 /* FIXME: Can do better here */
4356 new_pos = count < 0 ? 0 : strlen (label->text);
4358 case GTK_MOVEMENT_DISPLAY_LINES:
4359 case GTK_MOVEMENT_PARAGRAPHS:
4360 case GTK_MOVEMENT_PAGES:
4361 case GTK_MOVEMENT_HORIZONTAL_PAGES:
4369 case GTK_MOVEMENT_LOGICAL_POSITIONS:
4370 new_pos = gtk_label_move_logically (label, new_pos, count);
4372 case GTK_MOVEMENT_VISUAL_POSITIONS:
4373 new_pos = gtk_label_move_visually (label, new_pos, count);
4374 if (new_pos == old_pos)
4376 if (!extend_selection)
4378 if (!gtk_widget_keynav_failed (GTK_WIDGET (label),
4380 GTK_DIR_RIGHT : GTK_DIR_LEFT))
4382 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
4385 gtk_widget_child_focus (toplevel,
4387 GTK_DIR_RIGHT : GTK_DIR_LEFT);
4392 gtk_widget_error_bell (GTK_WIDGET (label));
4396 case GTK_MOVEMENT_WORDS:
4399 new_pos = gtk_label_move_forward_word (label, new_pos);
4404 new_pos = gtk_label_move_backward_word (label, new_pos);
4407 if (new_pos == old_pos)
4408 gtk_widget_error_bell (GTK_WIDGET (label));
4410 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4411 case GTK_MOVEMENT_PARAGRAPH_ENDS:
4412 case GTK_MOVEMENT_BUFFER_ENDS:
4413 /* FIXME: Can do better here */
4414 new_pos = count < 0 ? 0 : strlen (label->text);
4415 if (new_pos == old_pos)
4416 gtk_widget_error_bell (GTK_WIDGET (label));
4418 case GTK_MOVEMENT_DISPLAY_LINES:
4419 case GTK_MOVEMENT_PARAGRAPHS:
4420 case GTK_MOVEMENT_PAGES:
4421 case GTK_MOVEMENT_HORIZONTAL_PAGES:
4426 if (extend_selection)
4427 gtk_label_select_region_index (label,
4428 label->select_info->selection_anchor,
4431 gtk_label_select_region_index (label, new_pos, new_pos);
4435 gtk_label_copy_clipboard (GtkLabel *label)
4437 if (label->text && label->select_info)
4442 start = MIN (label->select_info->selection_anchor,
4443 label->select_info->selection_end);
4444 end = MAX (label->select_info->selection_anchor,
4445 label->select_info->selection_end);
4447 len = strlen (label->text);
4456 gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (label),
4457 GDK_SELECTION_CLIPBOARD),
4458 label->text + start, end - start);
4463 gtk_label_select_all (GtkLabel *label)
4465 gtk_label_select_region_index (label, 0, strlen (label->text));
4468 /* Quick hack of a popup menu
4471 activate_cb (GtkWidget *menuitem,
4474 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
4475 g_signal_emit_by_name (label, signal);
4479 append_action_signal (GtkLabel *label,
4481 const gchar *stock_id,
4482 const gchar *signal,
4485 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
4487 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
4488 g_signal_connect (menuitem, "activate",
4489 G_CALLBACK (activate_cb), label);
4491 gtk_widget_set_sensitive (menuitem, sensitive);
4493 gtk_widget_show (menuitem);
4494 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
4498 popup_menu_detach (GtkWidget *attach_widget,
4502 label = GTK_LABEL (attach_widget);
4504 if (label->select_info)
4505 label->select_info->popup_menu = NULL;
4509 popup_position_func (GtkMenu *menu,
4520 label = GTK_LABEL (user_data);
4521 widget = GTK_WIDGET (label);
4523 if (label->select_info == NULL)
4526 g_return_if_fail (GTK_WIDGET_REALIZED (label));
4528 screen = gtk_widget_get_screen (widget);
4529 gdk_window_get_origin (widget->window, x, y);
4531 gtk_widget_size_request (label->select_info->popup_menu, &req);
4533 *x += widget->allocation.width / 2;
4534 *y += widget->allocation.height;
4536 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
4537 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
4542 gtk_label_do_popup (GtkLabel *label,
4543 GdkEventButton *event)
4545 GtkWidget *menuitem;
4546 gboolean have_selection;
4548 if (label->select_info == NULL)
4551 if (label->select_info->popup_menu)
4552 gtk_widget_destroy (label->select_info->popup_menu);
4554 label->select_info->popup_menu = gtk_menu_new ();
4556 gtk_menu_attach_to_widget (GTK_MENU (label->select_info->popup_menu),
4561 label->select_info->selection_anchor != label->select_info->selection_end;
4564 append_action_signal (label, label->select_info->popup_menu, GTK_STOCK_CUT, "cut-clipboard",
4566 append_action_signal (label, label->select_info->popup_menu, GTK_STOCK_COPY, "copy-clipboard",
4568 append_action_signal (label, label->select_info->popup_menu, GTK_STOCK_PASTE, "paste-clipboard",
4571 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
4572 gtk_widget_set_sensitive (menuitem, FALSE);
4573 gtk_widget_show (menuitem);
4574 gtk_menu_shell_append (GTK_MENU_SHELL (label->select_info->popup_menu), menuitem);
4576 menuitem = gtk_separator_menu_item_new ();
4577 gtk_widget_show (menuitem);
4578 gtk_menu_shell_append (GTK_MENU_SHELL (label->select_info->popup_menu), menuitem);
4580 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
4581 g_signal_connect_swapped (menuitem, "activate",
4582 G_CALLBACK (gtk_label_select_all), label);
4583 gtk_widget_show (menuitem);
4584 gtk_menu_shell_append (GTK_MENU_SHELL (label->select_info->popup_menu), menuitem);
4586 g_signal_emit (label,
4587 signals[POPULATE_POPUP],
4589 label->select_info->popup_menu);
4592 gtk_menu_popup (GTK_MENU (label->select_info->popup_menu), NULL, NULL,
4594 event->button, event->time);
4596 gtk_menu_popup (GTK_MENU (label->select_info->popup_menu), NULL, NULL,
4597 popup_position_func, label,
4598 0, gtk_get_current_event_time ());
4601 #define __GTK_LABEL_C__
4602 #include "gtkaliasdef.c"