A widget that displays a small to medium amount of text
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>
.
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);
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 <, >, 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 <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.
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.
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
.
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
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>
<gtk-label>
) (str mchars
)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.
<gtk-label>
) (attrs <pango-attr-list>
)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>
<gtk-label>
) (str mchars
)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
org-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)
<gtk-label>
) (str mchars
)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)
<gtk-label>
) (pattern mchars
)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.
<gtk-label>
) (jtype <gtk-justification>
)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, usegtk-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>
<gtk-label>
) (mode <pango-ellipsize-mode>
)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
<gtk-label>
) (n_chars int
)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
<gtk-label>
) (n_chars int
)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
<gtk-label>
) (wrap bool
)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 usinggtk-widget-set-size-request
.
- label
- a
<gtk-label>
- wrap
- the setting
<gtk-label>
) (wrap_mode <pango-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
<gtk-label>
) ⇒ (x int
) (y int
)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 usingpango-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
’
<gtk-label>
) ⇒ (ret unsigned-int
)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>
<gtk-label>
) ⇒ (ret bool
)Gets the value set by
gtk-label-set-selectable
.
- label
- a
<gtk-label>
- ret
- ‘
#t
’ if the user can copy text from the label
<gtk-label>
) ⇒ (ret mchars
)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.
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>
<gtk-label>
) (start_offset int
) (end_offset int
)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)
<gtk-label>
) (widget <gtk-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>
<gtk-label>
) (setting bool
)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
<gtk-label>
) (str mchars
)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
<gtk-label>
) ⇒ (ret <pango-attr-list>
)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 (seegtk-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.
<gtk-label>
) ⇒ (ret <gtk-justification>
)Returns the justification of the label. See
gtk-label-set-justify
.
- label
- a
<gtk-label>
- ret
<gtk-justification>
<gtk-label>
) ⇒ (ret <pango-ellipsize-mode>
)Returns the ellipsizing position of the label. See
gtk-label-set-ellipsize
.
- label
- a
<gtk-label>
- ret
<pango-ellipsize-mode>
Since 2.6
<gtk-label>
) ⇒ (ret int
)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
<gtk-label>
) ⇒ (ret int
)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
<gtk-label>
) ⇒ (ret mchars
)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.
<gtk-label>
) ⇒ (ret <pango-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 withgtk-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
<gtk-label>
) ⇒ (ret bool
)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.
<gtk-label>
) ⇒ (ret <pango-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
<gtk-label>
) ⇒ (ret <gtk-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.
<gtk-label>
) ⇒ (ret bool
) (start int
) (end int
)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
<gtk-label>
) ⇒ (ret bool
)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.
<gtk-label>
) ⇒ (ret bool
)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.
<gtk-label>
) ⇒ (ret bool
)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
<gtk-label>
) ⇒ (ret double
)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
<gtk-label>
) (str mchars
)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
<gtk-label>
) (setting bool
)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.
<gtk-label>
) (setting bool
)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
<gtk-label>
) (single_line_mode bool
)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 modeSince 2.6
<gtk-label>
) (angle double
)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