Label
The GtkLabel
widget displays a small amount of text.
As the name implies, most labels are used to label another widget such as a class@Button.
Shortcuts and Gestures
GtkLabel
supports the following keyboard shortcuts, when the cursor is visible:
Shift+F10 or Menu opens the context menu.
Ctrl+A or Ctrl+
/
selects all.Ctrl+Shift+A or Ctrl+
\
unselects all.
Additionally, the following signals have default keybindings:
signal@Gtk.Label::activate-current-link
signal@Gtk.Label::copy-clipboard
signal@Gtk.Label::move-cursor
Actions
GtkLabel
defines a set of built-in actions:
clipboard.copy
copies the text to the clipboard.clipboard.cut
doesn't do anything, since text in labels can't be deleted.clipboard.paste
doesn't do anything, since text in labels can't be edited.link.open
opens the link, when activated on a link inside the label.link.copy
copies the link to the clipboard, when activated on a link inside the label.menu.popup
opens the context menu.selection.delete
doesn't do anything, since text in labels can't be deleted.selection.select-all
selects all of the text, if the label allows selection.
CSS nodes
label
├── [selection]
├── [link]
┊
╰── [link]
GtkLabel
has a single CSS node with the name label. A wide variety of style classes may be applied to labels, such as .title, .subtitle, .dim-label, etc. In the GtkShortcutsWindow
, labels are used with the .keycap style class.
If the label has a selection, it gets a subnode with name selection.
If the label has links, there is one subnode per link. These subnodes carry the link or visited state depending on whether they have been visited. In this case, label node also gets a .link style class.
GtkLabel as GtkBuildable
The GtkLabel implementation of the GtkBuildable interface supports a custom <attributes>
element, which supports any number of <attribute>
elements. The <attribute>
element has attributes named “name“, “value“, “start“ and “end“ and allows you to specify struct@Pango.Attribute values for this label.
An example of a UI definition fragment specifying Pango attributes:
<object class="GtkLabel">
<attributes>
<attribute name="weight" value="PANGO_WEIGHT_BOLD"/>
<attribute name="background" value="red" start="5" end="10"/>
</attributes>
</object>
The start and end attributes specify the range of characters to which the Pango attribute applies. If start and end are not specified, the attribute is applied to the whole text. Note that specifying ranges does not make much sense with translatable attributes. Use markup embedded in the translatable content instead.
Accessibility
GtkLabel
uses the %GTK_ACCESSIBLE_ROLE_LABEL role.
Mnemonics
Labels may contain “mnemonics”. Mnemonics are underlined characters in the label, used for keyboard navigation. Mnemonics are created by providing a string with an underscore before the mnemonic character, such as "_File"
, to the functions ctor@Gtk.Label.new_with_mnemonic or method@Gtk.Label.set_text_with_mnemonic.
Mnemonics automatically activate any activatable widget the label is inside, such as a class@Gtk.Button; if the label is not inside the mnemonic’s target widget, you have to tell the label about the target using method@Gtk.Label.set_mnemonic_widget.
Here’s a simple example where the label is inside a button:
// Pressing Alt+H will activate this button
GtkWidget *button = gtk_button_new ();
GtkWidget *label = gtk_label_new_with_mnemonic ("_Hello");
gtk_button_set_child (GTK_BUTTON (button), label);
There’s a convenience function to create buttons with a mnemonic label already inside:
// Pressing Alt+H will activate this button
GtkWidget *button = gtk_button_new_with_mnemonic ("_Hello");
To create a mnemonic for a widget alongside the label, such as a class@Gtk.Entry, you have to point the label at the entry with method@Gtk.Label.set_mnemonic_widget:
// Pressing Alt+H will focus the entry
GtkWidget *entry = gtk_entry_new ();
GtkWidget *label = gtk_label_new_with_mnemonic ("_Hello");
gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
Markup (styled text)
To make it easy to format text in a label (changing colors, fonts, etc.), label text can be provided in a simple markup format:
Here’s how to create a label with a small font:
GtkWidget *label = gtk_label_new (NULL);
gtk_label_set_markup (GTK_LABEL (label), "<small>Small text</small>");
(See the Pango manual for complete documentation] of available tags, func@Pango.parse_markup)
The markup passed to method@Gtk.Label.set_markup must be valid; for example, literal <
, >
and &
characters must be escaped as `<`
, `>`
, and `&`
. If you pass text obtained from the user, file, or a network to method@Gtk.Label.set_markup, you’ll want to escape it with func@GLib.markup_escape_text or func@GLib.markup_printf_escaped.
Markup strings are just a convenient way to set the struct@Pango.AttrList on a label; method@Gtk.Label.set_attributes may be a simpler way to set attributes in some cases. Be careful though; struct@Pango.AttrList tends to cause internationalization problems, unless you’re applying attributes to the entire string (i.e. unless you set the range of each attribute to [0, %G_MAXINT)). The reason is that specifying the start_index and end_index for a struct@Pango.Attribute requires knowledge of the exact string being displayed, so translations will cause problems.
Selectable labels
Labels can be made selectable with method@Gtk.Label.set_selectable. Selectable labels allow the user to copy the label contents to the clipboard. Only labels that contain useful-to-copy information—such as error messages—should be made selectable.
Text layout
A label can contain any number of paragraphs, but will have performance problems if it contains more than a small number. Paragraphs are separated by newlines or other paragraph separators understood by Pango.
Labels can automatically wrap text if you call method@Gtk.Label.set_wrap.
method@Gtk.Label.set_justify sets how the lines in a label align with one another. If you want to set how the label as a whole aligns in its available space, see the property@Gtk.Widget:halign and property@Gtk.Widget:valign properties.
The property@Gtk.Label:width-chars and property@Gtk.Label:max-width-chars properties can be used to control the size allocation of ellipsized or wrapped labels. For ellipsizing labels, if either is specified (and less than the actual text size), it is used as the minimum width, and the actual text size is used as the natural width of the label. For wrapping labels, width-chars is used as the minimum width, if specified, and max-width-chars is used as the natural width. Even if max-width-chars specified, wrapping labels will be rewrapped to use all of the available width.
Links
GTK supports markup for clickable hyperlinks in addition to regular Pango markup. The markup for links is borrowed from HTML, using the <a>
with “href“, “title“ and “class“ attributes. GTK renders links similar to the way they appear in web browsers, with colored, underlined text. The “title“ attribute is displayed as a tooltip on the link. The “class“ attribute is used as style class on the CSS node for the link.
An example of inline links looks like this:
const char *text =
"Go to the "
"<a href=\"https://www.gtk.org\" title=\"`<`i`>`Our`<`/i`>` website\">"
"GTK website</a> for more...";
GtkWidget *label = gtk_label_new (NULL);
gtk_label_set_markup (GTK_LABEL (label), text);
It is possible to implement custom handling for links and their tooltips with the signal@Gtk.Label::activate-link signal and the method@Gtk.Label.get_current_uri function.
Skipped during bindings generation
parameter
x
: x: Out parameter is not supportedparameter
start
: start: Out parameter is not supported
Constructors
Properties
The accessible role of the given GtkAccessible
implementation.
A list of style attributes to apply to the text of the label.
A list of css classes applied to this widget.
Whether the widget should grab focus when it is clicked with the mouse.
Enables or disables the emission of the ::query-tooltip signal on @widget.
Whether to use the hexpand
property.
The alignment of the lines in the text of the label, relative to each other.
The GtkLayoutManager
instance to use to compute the preferred size of the widget, and allocate its children.
Margin on bottom side of widget.
Margin on start of widget, horizontally.
The desired maximum width of the label, in characters.
The mnemonic accelerator key for the label.
The widget to be activated when the labels mnemonic key is pressed.
Select the line wrapping for the natural size request.
Whether the widget will receive the default action when it is focused.
The scale factor of the widget.
Whether the label text can be selected with the mouse.
Whether the label is in single line mode.
Sets the text of tooltip to be the given string, which is marked up with Pango markup.
Sets the text of tooltip to be the given string.
true if the text of the label indicates a mnemonic with an _ before the mnemonic character.
Whether to use the vexpand
property.
The desired width of the label, in characters.
Functions
Enable or disable an action installed with gtk_widget_class_install_action().
Looks up the action in the action groups associated with
Activates the default.activate
action from @widget.
For widgets that can be “activated” (buttons, menu items, etc.), this function activates them.
Adds @controller to @widget so that it will receive events.
Adds a style class to @widget.
Adds a widget to the list of mnemonic labels for this widget.
Queues an animation frame update and adds a callback to be called before each frame.
This function is only used by GtkWidget
subclasses, to assign a size, position and (optionally) baseline to their child widgets.
Requests the user's screen reader to announce the given message.
Called by widgets as the user moves around the window using keyboard shortcuts.
Computes the bounds for @widget in the coordinate space of @target.
Computes whether a container should give this widget extra space when possible.
Translates the given @point in @widget's coordinates to coordinates relative to @target’s coordinate system.
Computes a matrix suitable to describe a transformation from
Gets emitted when the user activates a link in the label.
Gets emitted to activate a URI.
Gets emitted to copy the selection to the clipboard.
Signals that all holders of a reference to the widget should release the reference that they hold.
Emitted when the text direction of a widget changes.
Emitted when @widget is hidden.
Emitted if keyboard navigation fails.
Emitted when @widget is going to be mapped.
Emitted when a widget is activated via a mnemonic.
Gets emitted when the user initiates a cursor movement.
Emitted when the focus is moved.
Emitted when the widget’s tooltip is about to be shown.
Emitted when @widget is associated with a GdkSurface
.
Emitted when @widget is shown.
Emitted when the widget state changes.
Emitted when @widget is going to be unmapped.
Emitted when the GdkSurface
associated with @widget is destroyed.
Creates a new PangoContext
with the appropriate font map, font options, font description, and base direction for drawing text for this widget.
Creates a new PangoLayout
with the appropriate font map, font description, and base direction for drawing text for this widget.
Clears the template children for the given widget.
Checks to see if a drag movement has passed the GTK drag threshold.
Retrieves the accessible parent for an accessible object.
Retrieves the accessible role of an accessible object.
Returns the baseline that has currently been allocated to @widget.
Returns the height that has currently been allocated to @widget.
Returns the width that has currently been allocated to @widget.
Gets the first ancestor of @widget with type @widget_type.
Retrieves the accessible implementation for the given GtkAccessible
.
Returns the baseline that has currently been allocated to @widget.
Gets the ID of the @buildable object.
Gets the value set with gtk_widget_set_child_visible().
Gets the clipboard object for @widget.
Returns the URI for the currently active link in the label.
Gets the reading direction for a particular widget.
Get the GdkDisplay
for the toplevel window associated with this widget.
Retrieves the first accessible child of an accessible object.
Returns the widget’s first child.
Returns the current focus child of @widget.
Gets the font map of @widget.
Obtains the frame clock for a widget.
Returns the widget’s last child.
Retrieves the next accessible sibling of an accessible object
Returns the widget’s next sibling.
Gets a PangoContext
with the appropriate font map, font description, and base direction for this widget.
Query a platform state, such as focus.
Retrieves the minimum and natural size of a widget, taking into account the widget’s preference for height-for-width management.
Returns the widget’s previous sibling.
Gets the primary clipboard of @widget.
Determines whether @widget is realized.
Gets whether the widget prefers a height-for-width layout or a width-for-height layout.
Returns the widget’s sensitivity.
Gets the settings object holding the settings used for this widget.
Returns the content width or height of the widget.
Returns the widget state as a flag set.
Returns the style context associated to @widget.
Fetch an object build from the template XML for @widget_type in this @widget instance.
Determines whether the widget is visible.
Returns whether @css_class is currently applied to @widget.
Determines whether @widget is the current default widget within its toplevel.
Determines if the widget should show a visible indication that it has the global input focus.
Returns whether the widget is currently being destroyed.
Creates and initializes child widgets defined in templates.
Inserts @group into @widget.
Inserts @widget into the child widget list of @parent.
Inserts @widget into the child widget list of @parent.
Determines whether @widget is somewhere inside @ancestor, possibly with intermediate containers.
Determines whether @widget can be drawn to.
Returns the widget’s effective sensitivity.
Emits the ::keynav-failed
signal on the widget.
Returns the widgets for which this widget is the target of a mnemonic.
Emits the ::mnemonic-activate signal.
Returns a GListModel
to track the children of @widget.
Returns a GListModel
to track the class@Gtk.EventControllers of @widget.
Flags the widget for a rerun of the vfunc@Gtk.Widget.size_allocate function.
Flags a widget to have its size renegotiated.
Removes @controller from @widget, so that it doesn't process events anymore.
Removes a style from @widget.
Removes a widget from the list of mnemonic labels for this widget.
Removes a tick callback previously registered with gtk_widget_add_tick_callback().
Resets the accessible @property to its default value.
Resets the accessible @relation to its default value.
Resets the accessible @state to its default value.
Selects a range of characters in the label, if the label is selectable.
Sets the parent and sibling of an accessible object.
Sets whether @widget should be mapped along with its parent.
Sets a named cursor to be shown when pointer devices point towards @widget.
Sets the reading direction on a particular widget.
Set @child as the current focus child of @widget.
Sets the font map to use for Pango rendering.
Set all margins to the same value.
Set start and end margin to horizontal and top and bottom margin to vertical
Set margins.
Sets the labels text, attributes and mnemonic from markup.
Sets the sensitivity of a widget.
Sets the minimum size of a widget.
Turns on flag values in the current widget state.
Sets the label’s text from the string @str.
Sets the visibility state of @widget.
Returns whether @widget should contribute to the measuring and allocation of its parent.
Triggers a tooltip query on the display where the toplevel of @widget is located.
Turns off flag values for the current widget state.
Updates the position of the caret.
Notifies assistive technologies of a change in contents.
Updates the next accessible sibling of @self.
Updates the boundary of the selection.