Next: , Previous: GtkImage, Up: Top


11 GtkLabel

A widget that displays a small to medium amount of text

11.1 Overview

The <gtk-label> widget displays a small amount of text. As the name implies, most labels are used to label another widget such as a <gtk-button>, a <gtk-menu-item>, or a <gtk-option-menu>.

11.2 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 <gtk-button>; 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: There's a convenience function to create buttons with a mnemonic label already inside: To create a mnemonic for a widget alongside the label, such as a <gtk-entry>, you have to point the label at the entry with gtk-label-set-mnemonic-widget:

     
       /* Pressing Alt+H will activate this button */
       button = gtk_button_new ();
       label = gtk_label_new_with_mnemonic ("_Hello");
       gtk_container_add (GTK_CONTAINER (button), label);
     
       /* Pressing Alt+H will activate this button */
       button = gtk_button_new_with_mnemonic ("_Hello");
     
       /* Pressing Alt+H will focus the entry */
       entry = gtk_entry_new ();
       label = gtk_label_new_with_mnemonic ("_Hello");
       gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);

11.3 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: (See complete documentation of available tags in the Pango manual.)

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

The markup passed to gtk-label-set-markup must be valid; for example, literal </>/& characters must be escaped as &lt;, &gt;, and &amp;. 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 <pango-attr-list> on a label; gtk-label-set-attributes may be a simpler way to set attributes in some cases. Be careful though; <pango-attr-list> 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 <pango-attribute> requires knowledge of the exact string being displayed, so translations will cause problems.

11.4 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 &#x2014; such as error messages &#x2014; should be made selectable.

11.5 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-line-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 gtk-misc-set-alignment.

11.6 Usage

— Class: <gtk-label>

Derives from <gtk-misc>.

This class defines the following slots:

label
The text of the label
attributes
A list of style attributes to apply to the text of the label
use-markup
The text of the label includes XML markup. See pango_parse_markup()
use-underline
If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key
justify
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
pattern
A string with _ characters in positions correspond to characters in the text to underline
wrap
If set, wrap lines if the text becomes too wide
wrap-mode
If wrap is set, controls how linewrapping is done
selectable
Whether the label text can be selected with the mouse
mnemonic-keyval
The mnemonic accelerator key for this label
mnemonic-widget
The widget to be activated when the label's mnemonic key is pressed
cursor-position
The current position of the insertion cursor in chars
selection-bound
The position of the opposite end of the selection from the cursor in chars
ellipsize
The preferred place to ellipsize the string, if the label does not have enough room to display the entire string
width-chars
The desired width of the label, in characters
single-line-mode
Whether the label is in single line mode
angle
Angle at which the label is rotated
max-width-chars
The desired maximum width of the label, in characters
— Signal on <gtk-label>: move-cursor (arg0 <gtk-movement-step>) (arg1 <gint>) (arg2 <gboolean>)
— Signal on <gtk-label>: copy-clipboard
— Signal on <gtk-label>: populate-popup (arg0 <gtk-menu>)
— Function: gtk-label-new (str mchars) ⇒  (ret <gtk-widget>)

Creates a new label with the given text inside it. You can pass ‘#f’ to get an empty label widget.

str
The text of the label
ret
the new <gtk-label>
— Function: gtk-label-set-text (self <gtk-label>) (str mchars)
— Method: set-text

Sets the text within the <gtk-label> widget. It overwrites any text that was there before.

This will also clear any previously set mnemonic accelerators.

label
a <gtk-label>
str
The text you want to set.
— Function: gtk-label-set-attributes (self <gtk-label>) (attrs <pango-attr-list>)
— Method: set-attributes

Sets a <pango-attr-list>; the attributes in the list are applied to the label text. The attributes set with this function will be ignored if the "use_underline" property or the "use_markup" property is ‘#t’.

label
a <gtk-label>
attrs
a <pango-attr-list>
— Function: gtk-label-set-markup (self <gtk-label>) (str mchars)
— Method: set-markup

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:

          
          char *markup;
          
          markup = g_markup_printf_escaped ("<span style=\"italic\">%s</span>", str);
          gtk_label_set_markup (GTK_LABEL (label), markup);
          g_free (markup);
label
a <gtk-label>
str
a markup string (see Pango markup format)
— Function: gtk-label-set-markup-with-mnemonic (self <gtk-label>) (str mchars)
— Method: set-markup-with-mnemonic

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.

label
a <gtk-label>
str
a markup string (see Pango markup format)
— Function: gtk-label-set-pattern (self <gtk-label>) (pattern mchars)
— Method: set-pattern

The pattern of underlines you want under the existing text within the <gtk-label> widget. For example if the current text of the label says "FooBarBaz" passing a pattern of "___ ___" will underline "Foo" and "Baz" but not "Bar".

label
The <gtk-label> you want to set the pattern to.
pattern
The pattern as described above.
— Function: gtk-label-set-justify (self <gtk-label>) (jtype <gtk-justification>)
— Method: set-justify

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-misc-set-alignment instead. gtk-label-set-justify has no effect on labels containing only a single line.

label
a <gtk-label>
jtype
a <gtk-justification>
— Function: gtk-label-set-ellipsize (self <gtk-label>) (mode <pango-ellipsize-mode>)
— Method: set-ellipsize

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

label
a <gtk-label>
mode
a <pango-ellipsize-mode>

Since 2.6

— Function: gtk-label-set-width-chars (self <gtk-label>) (n_chars int)
— Method: set-width-chars

Sets the desired width in characters of label to n-chars.

label
a <gtk-label>
n-chars
the new desired width, in characters.

Since 2.6

— Function: gtk-label-set-max-width-chars (self <gtk-label>) (n_chars int)
— Method: set-max-width-chars

Sets the desired maximum width in characters of label to n-chars.

label
a <gtk-label>
n-chars
the new desired maximum width, in characters.

Since 2.6

— Function: gtk-label-set-line-wrap (self <gtk-label>) (wrap bool)
— Method: set-line-wrap

Toggles line wrapping within the <gtk-label> widget. ‘#t’ makes it break lines if text exceeds the widget's size. ‘#f’ lets the text get cut off by the edge of the widget if it exceeds the widget size.

Note that setting line wrapping to ‘#t’ 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.

label
a <gtk-label>
wrap
the setting
— Function: gtk-label-set-line-wrap-mode (self <gtk-label>) (wrap_mode <pango-wrap-mode>)
— Method: set-line-wrap-mode

If line wrapping is on (see gtk-label-set-line-wrap) this controls how the line wrapping is done. The default is ‘PANGO_WRAP_WORD’ which means wrap on word boundaries.

label
a <gtk-label>
wrap-mode
the line wrapping mode

Since 2.10

— Function: gtk-label-get-layout-offsets (self <gtk-label>) ⇒  (int) (int)
— Method: get-layout-offsets

Obtains the coordinates where the label will draw the <pango-layout> representing the text in the label; useful to convert mouse events into coordinates inside the <pango-layout>, e.g. to take some action if some part of the label is clicked. Of course you will need to create a <gtk-event-box> to receive the events, and pack the label inside it, since labels are a <gtk-no-window> widget. Remember when using the <pango-layout> functions you need to convert to and from pixels using pango-pixels or <pango-scale>.

label
a <gtk-label>
x
location to store X offset of layout, or ‘#f
y
location to store Y offset of layout, or ‘#f
— Function: gtk-label-get-mnemonic-keyval (self <gtk-label>) ⇒  (ret unsigned-int)
— Method: get-mnemonic-keyval

If the label has been set so that it has an mnemonic key this function returns the keyval used for the mnemonic accelerator. If there is no mnemonic set up it returns <gdk--void-symbol>.

label
a <gtk-label>
ret
GDK keyval usable for accelerators, or <gdk--void-symbol>
— Function: gtk-label-get-selectable (self <gtk-label>) ⇒  (ret bool)
— Method: get-selectable

Gets the value set by gtk-label-set-selectable.

label
a <gtk-label>
ret
#t’ if the user can copy text from the label
— Function: gtk-label-get-text (self <gtk-label>) ⇒  (ret mchars)
— Method: get-text

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)

label
a <gtk-label>
ret
the text in the label widget. This is the internal string used by the label, and must not be modified.
— Function: gtk-label-new-with-mnemonic (str mchars) ⇒  (ret <gtk-widget>)

Creates a new <gtk-label>, 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 <gtk-label> 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.

str
The text of the label, with an underscore in front of the mnemonic character
ret
the new <gtk-label>
— Function: gtk-label-select-region (self <gtk-label>) (start_offset int) (end_offset int)
— Method: select-region

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.

label
a <gtk-label>
start-offset
start offset (in characters not bytes)
end-offset
end offset (in characters not bytes)
— Function: gtk-label-set-mnemonic-widget (self <gtk-label>) (widget <gtk-widget>)
— Method: set-mnemonic-widget

If the label has been set so that it has an 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 <gtk-button> or a <gtk-notebook> tab) it is automatically associated with the correct widget, but sometimes (i.e. when the target is a <gtk-entry> next to the label) you need to set it explicitly using this function.

The target widget will be accelerated by emitting "mnemonic_activate" 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.

label
a <gtk-label>
widget
the target <gtk-widget>
— Function: gtk-label-set-selectable (self <gtk-label>) (setting bool)
— Method: set-selectable

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

label
a <gtk-label>
setting
#t’ to allow selecting text in the label
— Function: gtk-label-set-text-with-mnemonic (self <gtk-label>) (str mchars)
— Method: set-text-with-mnemonic

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.

label
a <gtk-label>
str
a string
— Function: gtk-label-get-attributes (self <gtk-label>) ⇒  (ret <pango-attr-list>)
— Method: get-attributes

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 (label)).

label
a <gtk-label>
ret
the attribute list, or ‘#f’ if none was set.
— Function: gtk-label-get-justify (self <gtk-label>) ⇒  (ret <gtk-justification>)
— Method: get-justify

Returns the justification of the label. See gtk-label-set-justify.

label
a <gtk-label>
ret
<gtk-justification>
— Function: gtk-label-get-ellipsize (self <gtk-label>) ⇒  (ret <pango-ellipsize-mode>)
— Method: get-ellipsize

Returns the ellipsizing position of the label. See gtk-label-set-ellipsize.

label
a <gtk-label>
ret
<pango-ellipsize-mode>

Since 2.6

— Function: gtk-label-get-width-chars (self <gtk-label>) ⇒  (ret int)
— Method: get-width-chars

Retrieves the desired width of label, in characters. See gtk-label-set-width-chars.

label
a <gtk-label>
ret
the width of the label in characters.

Since 2.6

— Function: gtk-label-get-max-width-chars (self <gtk-label>) ⇒  (ret int)
— Method: get-max-width-chars

Retrieves the desired maximum width of label, in characters. See gtk-label-set-width-chars.

label
a <gtk-label>
ret
the maximum width of the label in characters.

Since 2.6

— Function: gtk-label-get-label (self <gtk-label>) ⇒  (ret mchars)
— Method: get-label

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

label
a <gtk-label>
ret
the text of the label widget. This string is owned by the widget and must not be modified or freed.
— Function: gtk-label-get-layout (self <gtk-label>) ⇒  (ret <pango-layout>)
— Method: get-layout

Gets the <pango-layout> 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.

label
a <gtk-label>
ret
the <pango-layout> for this label
— Function: gtk-label-get-line-wrap (self <gtk-label>) ⇒  (ret bool)
— Method: get-line-wrap

Returns whether lines in the label are automatically wrapped. See gtk-label-set-line-wrap.

label
a <gtk-label>
ret
#t’ if the lines of the label are automatically wrapped.
— Function: gtk-label-get-line-wrap-mode (self <gtk-label>) ⇒  (ret <pango-wrap-mode>)
— Method: get-line-wrap-mode

Returns line wrap mode used by the label. See gtk-label-set-line-wrap-mode.

label
a <gtk-label>
ret
#t’ if the lines of the label are automatically wrapped.

Since 2.10

— Function: gtk-label-get-mnemonic-widget (self <gtk-label>) ⇒  (ret <gtk-widget>)
— Method: get-mnemonic-widget

Retrieves the target of the mnemonic (keyboard shortcut) of this label. See gtk-label-set-mnemonic-widget.

label
a <gtk-label>
ret
the target of the label's mnemonic, or ‘#f’ if none has been set and the default algorithm will be used.
— Function: gtk-label-get-selection-bounds (self <gtk-label>) ⇒  (ret bool) (start int) (end int)
— Method: get-selection-bounds

Gets the selected range of characters in the label, returning ‘#t’ if there's a selection.

label
a <gtk-label>
start
return location for start of selection, as a character offset
end
return location for end of selection, as a character offset
ret
#t’ if selection is non-empty
— Function: gtk-label-get-use-markup (self <gtk-label>) ⇒  (ret bool)
— Method: get-use-markup

Returns whether the label's text is interpreted as marked up with the Pango text markup language. See gtk-label-set-use-markup.

label
a <gtk-label>
ret
#t’ if the label's text will be parsed for markup.
— Function: gtk-label-get-use-underline (self <gtk-label>) ⇒  (ret bool)
— Method: get-use-underline

Returns whether an embedded underline in the label indicates a mnemonic. See gtk-label-set-use-underline.

label
a <gtk-label>
ret
#t’ whether an embedded underline in the label indicates the mnemonic accelerator keys.
— Function: gtk-label-get-single-line-mode (self <gtk-label>) ⇒  (ret bool)
— Method: get-single-line-mode

Returns whether the label is in single line mode.

label
a <gtk-label>
ret
#t’ when the label is in single line mode.

Since 2.6

— Function: gtk-label-get-angle (self <gtk-label>) ⇒  (ret double)
— Method: get-angle

Gets the angle of rotation for the label. See gtk_label_set_angle.

label
a <gtk-label>
ret
the angle of rotation for the label

Since 2.6

— Function: gtk-label-set-label (self <gtk-label>) (str mchars)
— Method: set-label

Sets the text of the label. The label is interpreted as including embedded underlines and/or Pango markup depending on the values of label->use_underline and label->use_markup.

label
a <gtk-label>
str
the new text to set for the label
— Function: gtk-label-set-use-markup (self <gtk-label>) (setting bool)
— Method: set-use-markup

Sets whether the text of the label contains markup in Pango's text markup language. See gtk-label-set-markup.

label
a <gtk-label>
setting
#t’ if the label's text should be parsed for markup.
— Function: gtk-label-set-use-underline (self <gtk-label>) (setting bool)
— Method: set-use-underline

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

label
a <gtk-label>
setting
#t’ if underlines in the text indicate mnemonics
— Function: gtk-label-set-single-line-mode (self <gtk-label>) (single_line_mode bool)
— Method: set-single-line-mode

Sets whether the label is in single line mode.

label
a <gtk-label>
single-line-mode
#t’ if the label should be in single line mode

Since 2.6

— Function: gtk-label-set-angle (self <gtk-label>) (angle double)
— Method: set-angle

Sets the angle of rotation for the label. An angle of 90 reads from from bottom to top, an angle of 270, from top to bottom. The angle setting for the label is ignored if the label is selectable, wrapped, or ellipsized.

label
a <gtk-label>
angle
the angle that the baseline of the label makes with the horizontal, in degrees, measured counterclockwise

Since 2.6