GtkLabel

GtkLabel — A widget that displays a small to medium amount of text

Functions

GtkWidget * gtk_label_new ()
void gtk_label_set_text ()
void gtk_label_set_attributes ()
void gtk_label_set_markup ()
void gtk_label_set_markup_with_mnemonic ()
void gtk_label_set_justify ()
void gtk_label_set_xalign ()
void gtk_label_set_yalign ()
void gtk_label_set_ellipsize ()
void gtk_label_set_width_chars ()
void gtk_label_set_max_width_chars ()
void gtk_label_set_wrap ()
void gtk_label_set_wrap_mode ()
void gtk_label_set_lines ()
void gtk_label_get_layout_offsets ()
guint gtk_label_get_mnemonic_keyval ()
gboolean gtk_label_get_selectable ()
const char * gtk_label_get_text ()
GtkWidget * gtk_label_new_with_mnemonic ()
void gtk_label_select_region ()
void gtk_label_set_mnemonic_widget ()
void gtk_label_set_selectable ()
void gtk_label_set_text_with_mnemonic ()
PangoAttrList * gtk_label_get_attributes ()
GtkJustification gtk_label_get_justify ()
float gtk_label_get_xalign ()
float gtk_label_get_yalign ()
PangoEllipsizeMode gtk_label_get_ellipsize ()
int gtk_label_get_width_chars ()
int gtk_label_get_max_width_chars ()
const char * gtk_label_get_label ()
PangoLayout * gtk_label_get_layout ()
gboolean gtk_label_get_wrap ()
PangoWrapMode gtk_label_get_wrap_mode ()
int gtk_label_get_lines ()
GtkWidget * gtk_label_get_mnemonic_widget ()
gboolean gtk_label_get_selection_bounds ()
gboolean gtk_label_get_use_markup ()
gboolean gtk_label_get_use_underline ()
gboolean gtk_label_get_single_line_mode ()
void gtk_label_set_label ()
void gtk_label_set_use_markup ()
void gtk_label_set_use_underline ()
void gtk_label_set_single_line_mode ()
const char * gtk_label_get_current_uri ()
void gtk_label_set_extra_menu ()
GMenuModel * gtk_label_get_extra_menu ()

Properties

PangoAttrList * attributes Read / Write
PangoEllipsizeMode ellipsize Read / Write
GMenuModel * extra-menu Read / Write
GtkJustification justify Read / Write
char * label Read / Write
int lines Read / Write
int max-width-chars Read / Write
guint mnemonic-keyval Read
GtkWidget * mnemonic-widget Read / Write
gboolean selectable Read / Write
gboolean single-line-mode Read / Write
gboolean use-markup Read / Write
gboolean use-underline Read / Write
int width-chars Read / Write
gboolean wrap Read / Write
PangoWrapMode wrap-mode Read / Write
float xalign Read / Write
float yalign Read / Write

Signals

void activate-current-link Action
gboolean activate-link Run Last
void copy-clipboard Action
void move-cursor Action

Actions

  link.copy  
  link.open  
  selection.select-all  
  selection.delete  
  clipboard.paste  
  clipboard.copy  
  clipboard.cut  
  menu.popup  

Types and Values

Object Hierarchy

    GObject
    ╰── GInitiallyUnowned
        ╰── GtkWidget
            ╰── GtkLabel

Implemented Interfaces

GtkLabel implements GtkAccessible, GtkBuildable and GtkConstraintTarget.

Includes

#include <gtk/gtk.h>

Description

The GtkLabel widget displays a small amount of text. As the name implies, most labels are used to label another widget such as a GtkButton.

CSS nodes

1
2
3
4
5
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 PangoAttribute values for this label.

An example of a UI definition fragment specifying Pango attributes:

1
2
3
4
5
6
<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 gtk_label_new_with_mnemonic() or gtk_label_set_text_with_mnemonic().

Mnemonics automatically activate any activatable widget the label is inside, such as a GtkButton; if the label is not inside the mnemonic’s target widget, you have to tell the label about the target using gtk_label_set_mnemonic_widget(). Here’s a simple example where the label is inside a button:

1
2
3
4
// 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:

1
2
// 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 GtkEntry, you have to point the label at the entry with gtk_label_set_mnemonic_widget():

1
2
3
4
// 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:

1
2
GtkWidget *label = gtk_label_new (NULL);
gtk_label_set_markup (GTK_LABEL (label), "<small>Small text</small>");

(See complete documentation of available tags in the Pango manual.)

The markup passed to 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 gtk_label_set_markup(), you’ll want to escape it with g_markup_escape_text() or g_markup_printf_escaped().

Markup strings are just a convenient way to set the PangoAttrList on a label; gtk_label_set_attributes() may be a simpler way to set attributes in some cases. Be careful though; PangoAttrList 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 PangoAttribute requires knowledge of the exact string being displayed, so translations will cause problems.


Selectable labels

Labels can be made selectable with 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 gtk_label_set_wrap().

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 “halign” and “valign” properties.

The “width-chars” and “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.

Note that the interpretation of “width-chars” and “max-width-chars” has changed a bit with the introduction of width-for-height geometry management.


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 looks like this:

1
2
3
4
5
6
const char *text =
"Go to the"
"<a href=\"http://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 “activate-link” signal and the gtk_label_get_current_uri() function.

Functions

gtk_label_new ()

GtkWidget *
gtk_label_new (const char *str);

Creates a new label with the given text inside it. You can pass NULL to get an empty label widget.

Parameters

str

The text of the label.

[nullable]

Returns

the new GtkLabel


gtk_label_set_text ()

void
gtk_label_set_text (GtkLabel *self,
                    const char *str);

Sets the text within the GtkLabel widget. It overwrites any text that was there before.

This function will clear any previously set mnemonic accelerators, and set the “use-underline” property to FALSE as a side effect.

This function will set the “use-markup” property to FALSE as a side effect.

See also: gtk_label_set_markup()

Parameters

self

a GtkLabel

 

str

The text you want to set

 

gtk_label_set_attributes ()

void
gtk_label_set_attributes (GtkLabel *self,
                          PangoAttrList *attrs);

Sets a PangoAttrList; the attributes in the list are applied to the label text.

The attributes set with this function will be applied and merged with any other attributes previously effected by way of the “use-underline” or “use-markup” properties. While it is not recommended to mix markup strings with manually set attributes, if you must; know that the attributes will be applied to the label after the markup string is parsed.

Parameters

self

a GtkLabel

 

attrs

a PangoAttrList, or NULL.

[nullable]

gtk_label_set_markup ()

void
gtk_label_set_markup (GtkLabel *self,
                      const char *str);

Parses str which is marked up with the Pango text markup language, setting the label’s text and attribute list based on the parse results.

If the str is external data, you may need to escape it with g_markup_escape_text() or g_markup_printf_escaped():

1
2
3
4
5
6
7
8
GtkWidget *self = gtk_label_new (NULL);
const char *str = "...";
const char *format = "<span style=\"italic\">\%s</span>";
char *markup;

markup = g_markup_printf_escaped (format, str);
gtk_label_set_markup (GTK_LABEL (self), markup);
g_free (markup);

This function will set the “use-markup” property to TRUE as a side effect.

If you set the label contents using the “label” property you should also ensure that you set the “use-markup” property accordingly.

See also: gtk_label_set_text()

Parameters

self

a GtkLabel

 

str

a markup string (see Pango markup format)

 

gtk_label_set_markup_with_mnemonic ()

void
gtk_label_set_markup_with_mnemonic (GtkLabel *self,
                                    const char *str);

Parses str which is marked up with the Pango text markup language, setting the label’s text and attribute list based on the parse results. If characters in str are preceded by an underscore, they are underlined indicating that they represent a keyboard accelerator called a mnemonic.

The mnemonic key can be used to activate another widget, chosen automatically, or explicitly using gtk_label_set_mnemonic_widget().

Parameters

self

a GtkLabel

 

str

a markup string (see Pango markup format)

 

gtk_label_set_justify ()

void
gtk_label_set_justify (GtkLabel *self,
                       GtkJustification jtype);

Sets the alignment of the lines in the text of the label relative to each other. GTK_JUSTIFY_LEFT is the default value when the widget is first created with gtk_label_new(). If you instead want to set the alignment of the label as a whole, use gtk_widget_set_halign() instead. gtk_label_set_justify() has no effect on labels containing only a single line.

Parameters

self

a GtkLabel

 

jtype

a GtkJustification

 

gtk_label_set_xalign ()

void
gtk_label_set_xalign (GtkLabel *self,
                      float xalign);

Sets the “xalign” property for label .

Parameters

self

a GtkLabel

 

xalign

the new xalign value, between 0 and 1

 

gtk_label_set_yalign ()

void
gtk_label_set_yalign (GtkLabel *self,
                      float yalign);

Sets the “yalign” property for label .

Parameters

self

a GtkLabel

 

yalign

the new yalign value, between 0 and 1

 

gtk_label_set_ellipsize ()

void
gtk_label_set_ellipsize (GtkLabel *self,
                         PangoEllipsizeMode mode);

Sets the mode used to ellipsize (add an ellipsis: "...") to the text if there is not enough space to render the entire string.

Parameters

self

a GtkLabel

 

mode

a PangoEllipsizeMode

 

gtk_label_set_width_chars ()

void
gtk_label_set_width_chars (GtkLabel *self,
                           int n_chars);

Sets the desired width in characters of label to n_chars .

Parameters

self

a GtkLabel

 

n_chars

the new desired width, in characters.

 

gtk_label_set_max_width_chars ()

void
gtk_label_set_max_width_chars (GtkLabel *self,
                               int n_chars);

Sets the desired maximum width in characters of label to n_chars .

Parameters

self

a GtkLabel

 

n_chars

the new desired maximum width, in characters.

 

gtk_label_set_wrap ()

void
gtk_label_set_wrap (GtkLabel *self,
                    gboolean wrap);

Toggles line wrapping within the GtkLabel widget. TRUE makes it break lines if text exceeds the widget’s size. FALSE lets the text get cut off by the edge of the widget if it exceeds the widget size.

Note that setting line wrapping to TRUE does not make the label wrap at its parent container’s width, because GTK widgets conceptually can’t make their requisition depend on the parent container’s size. For a label that wraps at a specific position, set the label’s width using gtk_widget_set_size_request().

Parameters

self

a GtkLabel

 

wrap

the setting

 

gtk_label_set_wrap_mode ()

void
gtk_label_set_wrap_mode (GtkLabel *self,
                         PangoWrapMode wrap_mode);

If line wrapping is on (see gtk_label_set_wrap()) this controls how the line wrapping is done. The default is PANGO_WRAP_WORD which means wrap on word boundaries.

Parameters

self

a GtkLabel

 

wrap_mode

the line wrapping mode

 

gtk_label_set_lines ()

void
gtk_label_set_lines (GtkLabel *self,
                     int lines);

Sets the number of lines to which an ellipsized, wrapping label should be limited. This has no effect if the label is not wrapping or ellipsized. Set this to -1 if you don’t want to limit the number of lines.

Parameters

self

a GtkLabel

 

lines

the desired number of lines, or -1

 

gtk_label_get_layout_offsets ()

void
gtk_label_get_layout_offsets (GtkLabel *self,
                              int *x,
                              int *y);

Obtains the coordinates where the label will draw the PangoLayout representing the text in the label; useful to convert mouse events into coordinates inside the PangoLayout, e.g. to take some action if some part of the label is clicked. Remember when using the PangoLayout functions you need to convert to and from pixels using PANGO_PIXELS() or PANGO_SCALE.

Parameters

self

a GtkLabel

 

x

location to store X offset of layout, or NULL.

[out][optional]

y

location to store Y offset of layout, or NULL.

[out][optional]

gtk_label_get_mnemonic_keyval ()

guint
gtk_label_get_mnemonic_keyval (GtkLabel *self);

If the label has been set so that it has a mnemonic key this function returns the keyval used for the mnemonic accelerator. If there is no mnemonic set up it returns GDK_KEY_VoidSymbol.

Parameters

self

a GtkLabel

 

Returns

GDK keyval usable for accelerators, or GDK_KEY_VoidSymbol


gtk_label_get_selectable ()

gboolean
gtk_label_get_selectable (GtkLabel *self);

Gets the value set by gtk_label_set_selectable().

Parameters

self

a GtkLabel

 

Returns

TRUE if the user can copy text from the label


gtk_label_get_text ()

const char *
gtk_label_get_text (GtkLabel *self);

Fetches the text from a label widget, as displayed on the screen. This does not include any embedded underlines indicating mnemonics or Pango markup. (See gtk_label_get_label())

Parameters

self

a GtkLabel

 

Returns

the text in the label widget. This is the internal string used by the label, and must not be modified.


gtk_label_new_with_mnemonic ()

GtkWidget *
gtk_label_new_with_mnemonic (const char *str);

Creates a new GtkLabel, containing the text in str .

If characters in str are preceded by an underscore, they are underlined. If you need a literal underscore character in a label, use '__' (two underscores). The first underlined character represents a keyboard accelerator called a mnemonic. The mnemonic key can be used to activate another widget, chosen automatically, or explicitly using gtk_label_set_mnemonic_widget().

If gtk_label_set_mnemonic_widget() is not called, then the first activatable ancestor of the GtkLabel will be chosen as the mnemonic widget. For instance, if the label is inside a button or menu item, the button or menu item will automatically become the mnemonic widget and be activated by the mnemonic.

Parameters

str

The text of the label, with an underscore in front of the mnemonic character.

[nullable]

Returns

the new GtkLabel


gtk_label_select_region ()

void
gtk_label_select_region (GtkLabel *self,
                         int start_offset,
                         int end_offset);

Selects a range of characters in the label, if the label is selectable. See gtk_label_set_selectable(). If the label is not selectable, this function has no effect. If start_offset or end_offset are -1, then the end of the label will be substituted.

Parameters

self

a GtkLabel

 

start_offset

start offset (in characters not bytes)

 

end_offset

end offset (in characters not bytes)

 

gtk_label_set_mnemonic_widget ()

void
gtk_label_set_mnemonic_widget (GtkLabel *self,
                               GtkWidget *widget);

If the label has been set so that it has a mnemonic key (using i.e. gtk_label_set_markup_with_mnemonic(), gtk_label_set_text_with_mnemonic(), gtk_label_new_with_mnemonic() or the “use_underline” property) the label can be associated with a widget that is the target of the mnemonic. When the label is inside a widget (like a GtkButton or a GtkNotebook tab) it is automatically associated with the correct widget, but sometimes (i.e. when the target is a GtkEntry next to the label) you need to set it explicitly using this function.

The target widget will be accelerated by emitting the GtkWidget::mnemonic-activate signal on it. The default handler for this signal will activate the widget if there are no mnemonic collisions and toggle focus between the colliding widgets otherwise.

Parameters

self

a GtkLabel

 

widget

the target GtkWidget, or NULL to unset.

[nullable]

gtk_label_set_selectable ()

void
gtk_label_set_selectable (GtkLabel *self,
                          gboolean setting);

Selectable labels allow the user to select text from the label, for copy-and-paste.

Parameters

self

a GtkLabel

 

setting

TRUE to allow selecting text in the label

 

gtk_label_set_text_with_mnemonic ()

void
gtk_label_set_text_with_mnemonic (GtkLabel *self,
                                  const char *str);

Sets the label’s text from the string str . If characters in str are preceded by an underscore, they are underlined indicating that they represent a keyboard accelerator called a mnemonic. The mnemonic key can be used to activate another widget, chosen automatically, or explicitly using gtk_label_set_mnemonic_widget().

Parameters

self

a GtkLabel

 

str

a string

 

gtk_label_get_attributes ()

PangoAttrList *
gtk_label_get_attributes (GtkLabel *self);

Gets the attribute list that was set on the label using gtk_label_set_attributes(), if any. This function does not reflect attributes that come from the labels markup (see gtk_label_set_markup()). If you want to get the effective attributes for the label, use pango_layout_get_attribute (gtk_label_get_layout (self)).

Parameters

self

a GtkLabel

 

Returns

the attribute list, or NULL if none was set.

[nullable][transfer none]


gtk_label_get_justify ()

GtkJustification
gtk_label_get_justify (GtkLabel *self);

Returns the justification of the label. See gtk_label_set_justify().

Parameters

self

a GtkLabel

 

gtk_label_get_xalign ()

float
gtk_label_get_xalign (GtkLabel *self);

Gets the “xalign” property for label .

Parameters

self

a GtkLabel

 

Returns

the xalign property


gtk_label_get_yalign ()

float
gtk_label_get_yalign (GtkLabel *self);

Gets the “yalign” property for label .

Parameters

self

a GtkLabel

 

Returns

the yalign property


gtk_label_get_ellipsize ()

PangoEllipsizeMode
gtk_label_get_ellipsize (GtkLabel *self);

Returns the ellipsizing position of the label. See gtk_label_set_ellipsize().

Parameters

self

a GtkLabel

 

gtk_label_get_width_chars ()

int
gtk_label_get_width_chars (GtkLabel *self);

Retrieves the desired width of label , in characters. See gtk_label_set_width_chars().

Parameters

self

a GtkLabel

 

Returns

the width of the label in characters.


gtk_label_get_max_width_chars ()

int
gtk_label_get_max_width_chars (GtkLabel *self);

Retrieves the desired maximum width of label , in characters. See gtk_label_set_width_chars().

Parameters

self

a GtkLabel

 

Returns

the maximum width of the label in characters.


gtk_label_get_label ()

const char *
gtk_label_get_label (GtkLabel *self);

Fetches the text from a label widget including any embedded underlines indicating mnemonics and Pango markup. (See gtk_label_get_text()).

Parameters

self

a GtkLabel

 

Returns

the text of the label widget. This string is owned by the widget and must not be modified or freed.


gtk_label_get_layout ()

PangoLayout *
gtk_label_get_layout (GtkLabel *self);

Gets the PangoLayout used to display the label. The layout is useful to e.g. convert text positions to pixel positions, in combination with gtk_label_get_layout_offsets(). The returned layout is owned by the label so need not be freed by the caller. The label is free to recreate its layout at any time, so it should be considered read-only.

Parameters

self

a GtkLabel

 

Returns

the PangoLayout for this label.

[transfer none]


gtk_label_get_wrap ()

gboolean
gtk_label_get_wrap (GtkLabel *self);

Returns whether lines in the label are automatically wrapped. See gtk_label_set_wrap().

Parameters

self

a GtkLabel

 

Returns

TRUE if the lines of the label are automatically wrapped.


gtk_label_get_wrap_mode ()

PangoWrapMode
gtk_label_get_wrap_mode (GtkLabel *self);

Returns line wrap mode used by the label. See gtk_label_set_wrap_mode().

Parameters

self

a GtkLabel

 

Returns

TRUE if the lines of the label are automatically wrapped.


gtk_label_get_lines ()

int
gtk_label_get_lines (GtkLabel *self);

Gets the number of lines to which an ellipsized, wrapping label should be limited. See gtk_label_set_lines().

Parameters

self

a GtkLabel

 

Returns

The number of lines


gtk_label_get_mnemonic_widget ()

GtkWidget *
gtk_label_get_mnemonic_widget (GtkLabel *self);

Retrieves the target of the mnemonic (keyboard shortcut) of this label. See gtk_label_set_mnemonic_widget().

Parameters

self

a GtkLabel

 

Returns

the target of the label’s mnemonic, or NULL if none has been set and the default algorithm will be used.

[nullable][transfer none]


gtk_label_get_selection_bounds ()

gboolean
gtk_label_get_selection_bounds (GtkLabel *self,
                                int *start,
                                int *end);

Gets the selected range of characters in the label, returning TRUE if there’s a selection.

Parameters

self

a GtkLabel

 

start

return location for start of selection, as a character offset.

[out]

end

return location for end of selection, as a character offset.

[out]

Returns

TRUE if selection is non-empty


gtk_label_get_use_markup ()

gboolean
gtk_label_get_use_markup (GtkLabel *self);

Returns whether the label’s text is interpreted as marked up with the Pango text markup language. See gtk_label_set_use_markup().

Parameters

self

a GtkLabel

 

Returns

TRUE if the label’s text will be parsed for markup.


gtk_label_get_use_underline ()

gboolean
gtk_label_get_use_underline (GtkLabel *self);

Returns whether an embedded underline in the label indicates a mnemonic. See gtk_label_set_use_underline().

Parameters

self

a GtkLabel

 

Returns

TRUE whether an embedded underline in the label indicates the mnemonic accelerator keys.


gtk_label_get_single_line_mode ()

gboolean
gtk_label_get_single_line_mode (GtkLabel *self);

Returns whether the label is in single line mode.

Parameters

self

a GtkLabel

 

Returns

TRUE when the label is in single line mode.


gtk_label_set_label ()

void
gtk_label_set_label (GtkLabel *self,
                     const char *str);

Sets the text of the label. The label is interpreted as including embedded underlines and/or Pango markup depending on the values of the “use-underline” and “use-markup” properties.

Parameters

self

a GtkLabel

 

str

the new text to set for the label

 

gtk_label_set_use_markup ()

void
gtk_label_set_use_markup (GtkLabel *self,
                          gboolean setting);

Sets whether the text of the label contains markup in Pango’s text markup language. See gtk_label_set_markup().

Parameters

self

a GtkLabel

 

setting

TRUE if the label’s text should be parsed for markup.

 

gtk_label_set_use_underline ()

void
gtk_label_set_use_underline (GtkLabel *self,
                             gboolean setting);

If true, an underline in the text indicates the next character should be used for the mnemonic accelerator key.

Parameters

self

a GtkLabel

 

setting

TRUE if underlines in the text indicate mnemonics

 

gtk_label_set_single_line_mode ()

void
gtk_label_set_single_line_mode (GtkLabel *self,
                                gboolean single_line_mode);

Sets whether the label is in single line mode.

Parameters

self

a GtkLabel

 

single_line_mode

TRUE if the label should be in single line mode

 

gtk_label_get_current_uri ()

const char *
gtk_label_get_current_uri (GtkLabel *self);

Returns the URI for the currently active link in the label. The active link is the one under the mouse pointer or, in a selectable label, the link in which the text cursor is currently positioned.

This function is intended for use in a “activate-link” handler or for use in a “query-tooltip” handler.

Parameters

self

a GtkLabel

 

Returns

the currently active URI or NULL if there is none. The string is owned by GTK and must not be freed or modified.

[nullable]


gtk_label_set_extra_menu ()

void
gtk_label_set_extra_menu (GtkLabel *self,
                          GMenuModel *model);

Sets a menu model to add when constructing the context menu for label .

Parameters

self

a GtkLabel

 

model

a GMenuModel.

[allow-none]

gtk_label_get_extra_menu ()

GMenuModel *
gtk_label_get_extra_menu (GtkLabel *self);

Gets the menu model set with gtk_label_set_extra_menu().

Parameters

self

a GtkLabel

 

Returns

the menu model.

[transfer none][nullable]

Types and Values

GtkLabel

typedef struct _GtkLabel GtkLabel;

Property Details

The “attributes” property

  “attributes”               PangoAttrList *

A list of style attributes to apply to the text of the label.

Owner: GtkLabel

Flags: Read / Write


The “ellipsize” property

  “ellipsize”                PangoEllipsizeMode

The preferred place to ellipsize the string, if the label does not have enough room to display the entire string, specified as a PangoEllipsizeMode.

Note that setting this property to a value other than PANGO_ELLIPSIZE_NONE has the side-effect that the label requests only enough space to display the ellipsis "...". In particular, this means that ellipsizing labels do not work well in notebook tabs, unless the GtkNotebook tab-expand child property is set to TRUE. Other ways to set a label's width are gtk_widget_set_size_request() and gtk_label_set_width_chars().

Owner: GtkLabel

Flags: Read / Write

Default value: PANGO_ELLIPSIZE_NONE


The “extra-menu” property

  “extra-menu”               GMenuModel *

A menu model whose contents will be appended to the context menu.

Owner: GtkLabel

Flags: Read / Write


The “justify” property

  “justify”                  GtkJustification

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 GtkLabel:xalign for that.

Owner: GtkLabel

Flags: Read / Write

Default value: GTK_JUSTIFY_LEFT


The “label” property

  “label”                    char *

The contents of the label.

If the string contains Pango XML markup, you will have to set the “use-markup” property to TRUE in order for the label to display the markup attributes. See also gtk_label_set_markup() for a convenience function that sets both this property and the “use-markup” property at the same time.

If the string contains underlines acting as mnemonics, you will have to set the “use-underline” property to TRUE in order for the label to display them.

Owner: GtkLabel

Flags: Read / Write

Default value: ""


The “lines” property

  “lines”                    int

The number of lines to which an ellipsized, wrapping label should be limited. This property has no effect if the label is not wrapping or ellipsized. Set this property to -1 if you don't want to limit the number of lines.

Owner: GtkLabel

Flags: Read / Write

Allowed values: >= -1

Default value: -1


The “max-width-chars” property

  “max-width-chars”          int

The desired maximum width of the label, in characters. If this property is set to -1, the width will be calculated automatically.

See the section on text layout for details of how “width-chars” and “max-width-chars” determine the width of ellipsized and wrapped labels.

Owner: GtkLabel

Flags: Read / Write

Allowed values: >= -1

Default value: -1


The “mnemonic-keyval” property

  “mnemonic-keyval”          guint

The mnemonic accelerator key for this label.

Owner: GtkLabel

Flags: Read

Default value: 16777215


The “mnemonic-widget” property

  “mnemonic-widget”          GtkWidget *

The widget to be activated when the label’s mnemonic key is pressed.

Owner: GtkLabel

Flags: Read / Write


The “selectable” property

  “selectable”               gboolean

Whether the label text can be selected with the mouse.

Owner: GtkLabel

Flags: Read / Write

Default value: FALSE


The “single-line-mode” property

  “single-line-mode”         gboolean

Whether the label is in single line mode. In single line mode, the height of the label does not depend on the actual text, it is always set to ascent + descent of the font. This can be an advantage in situations where resizing the label because of text changes would be distracting, e.g. in a statusbar.

Owner: GtkLabel

Flags: Read / Write

Default value: FALSE


The “use-markup” property

  “use-markup”               gboolean

The text of the label includes XML markup. See pango_parse_markup().

Owner: GtkLabel

Flags: Read / Write

Default value: FALSE


The “use-underline” property

  “use-underline”            gboolean

If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key.

Owner: GtkLabel

Flags: Read / Write

Default value: FALSE


The “width-chars” property

  “width-chars”              int

The desired width of the label, in characters. If this property is set to -1, the width will be calculated automatically.

See the section on text layout for details of how “width-chars” and “max-width-chars” determine the width of ellipsized and wrapped labels.

Owner: GtkLabel

Flags: Read / Write

Allowed values: >= -1

Default value: -1


The “wrap” property

  “wrap”                     gboolean

If set, wrap lines if the text becomes too wide.

Owner: GtkLabel

Flags: Read / Write

Default value: FALSE


The “wrap-mode” property

  “wrap-mode”                PangoWrapMode

If line wrapping is on (see the “wrap” property) this controls how the line wrapping is done. The default is PANGO_WRAP_WORD, which means wrap on word boundaries.

Owner: GtkLabel

Flags: Read / Write

Default value: PANGO_WRAP_WORD


The “xalign” property

  “xalign”                   float

The xalign property determines the horizontal alignment of the label text inside the labels size allocation. Compare this to “halign”, which determines how the labels size allocation is positioned in the space available for the label.

Owner: GtkLabel

Flags: Read / Write

Allowed values: [0,1]

Default value: 0.5


The “yalign” property

  “yalign”                   float

The yalign property determines the vertical alignment of the label text inside the labels size allocation. Compare this to “valign”, which determines how the labels size allocation is positioned in the space available for the label.

Owner: GtkLabel

Flags: Read / Write

Allowed values: [0,1]

Default value: 0.5

Signal Details

The “activate-current-link” signal

void
user_function (GtkLabel *self,
               gpointer  user_data)

A keybinding signal which gets emitted when the user activates a link in the label.

Applications may also emit the signal with g_signal_emit_by_name() if they need to control activation of URIs programmatically.

The default bindings for this signal are all forms of the Enter key.

Parameters

self

The label on which the signal was emitted

 

user_data

user data set when the signal handler was connected.

 

Flags: Action


The “activate-link” signal

gboolean
user_function (GtkLabel *self,
               char     *uri,
               gpointer  user_data)

The signal which gets emitted to activate a URI. Applications may connect to it to override the default behaviour, which is to call gtk_show_uri().

Parameters

self

The label on which the signal was emitted

 

uri

the URI that is activated

 

user_data

user data set when the signal handler was connected.

 

Returns

TRUE if the link has been activated

Flags: Run Last


The “copy-clipboard” signal

void
user_function (GtkLabel *self,
               gpointer  user_data)

The ::copy-clipboard signal is a keybinding signal which gets emitted to copy the selection to the clipboard.

The default binding for this signal is Ctrl-c.

Parameters

self

the object which received the signal

 

user_data

user data set when the signal handler was connected.

 

Flags: Action


The “move-cursor” signal

void
user_function (GtkLabel       *entry,
               GtkMovementStep step,
               int             count,
               gboolean        extend_selection,
               gpointer        user_data)

The ::move-cursor signal is a keybinding signal which gets emitted when the user initiates a cursor movement. If the cursor is not visible in entry , this signal causes the viewport to be moved instead.

Applications should not connect to it, but may emit it with g_signal_emit_by_name() if they need to control the cursor programmatically.

The default bindings for this signal come in two variants, the variant with the Shift modifier extends the selection, the variant without the Shift modifier does not. There are too many key combinations to list them all here.

  • Arrow keys move by individual characters/lines

  • Ctrl-arrow key combinations move by words/paragraphs

  • Home/End keys move to the ends of the buffer

Parameters

entry

the object which received the signal

 

step

the granularity of the move, as a GtkMovementStep

 

count

the number of step units to move

 

extend_selection

TRUE if the move should extend the selection

 

user_data

user data set when the signal handler was connected.

 

Flags: Action

Action Details

The “link.copy” action

Copies the link to the clipboard, when activated on a link inside the label.


The “link.open” action

Opens the link, when activated on a link inside the label.


The “selection.select-all” action

Selects all of the text, if the label allows selection.


The “selection.delete” action

Doesn't do anything, since text in labels can't be deleted.


The “clipboard.paste” action

Doesn't do anything, since text in labels can't be edited.


The “clipboard.copy” action

Copies the text to the clipboard.


The “clipboard.cut” action

Doesn't do anything, since text in labels can't be deleted.


The “menu.popup” action

Opens the context menu.