![]() |
![]() |
![]() |
GTK+ Reference Manual | ![]() |
---|---|---|---|---|
#include <gtk/gtk.h> GtkLabel; GtkWidget* gtk_label_new (constgchar *str);void gtk_label_set_text (GtkLabel *label, constgchar *str);void gtk_label_set_attributes (GtkLabel *label,PangoAttrList *attrs);void gtk_label_set_markup (GtkLabel *label, constgchar *str);void gtk_label_set_markup_with_mnemonic (GtkLabel *label, constgchar *str);void gtk_label_set_pattern (GtkLabel *label, constgchar *pattern);void gtk_label_set_justify (GtkLabel *label, GtkJustification jtype);void gtk_label_set_ellipsize (GtkLabel *label,PangoEllipsizeMode mode);void gtk_label_set_width_chars (GtkLabel *label,gint n_chars);void gtk_label_set_max_width_chars (GtkLabel *label,gint n_chars);void gtk_label_get (GtkLabel *label,gchar **str);guint gtk_label_parse_uline (GtkLabel *label, constgchar *string);void gtk_label_set_line_wrap (GtkLabel *label,gboolean wrap);void gtk_label_set_line_wrap_mode (GtkLabel *label,PangoWrapMode wrap_mode); #define gtk_label_setvoid gtk_label_get_layout_offsets (GtkLabel *label,gint *x,gint *y);guint gtk_label_get_mnemonic_keyval (GtkLabel *label);gboolean gtk_label_get_selectable (GtkLabel *label); constgchar * gtk_label_get_text (GtkLabel *label); GtkWidget* gtk_label_new_with_mnemonic (constgchar *str);void gtk_label_select_region (GtkLabel *label,gint start_offset,gint end_offset);void gtk_label_set_mnemonic_widget (GtkLabel *label, GtkWidget *widget);void gtk_label_set_selectable (GtkLabel *label,gboolean setting);void gtk_label_set_text_with_mnemonic (GtkLabel *label, constgchar *str);PangoAttrList * gtk_label_get_attributes (GtkLabel *label); GtkJustification gtk_label_get_justify (GtkLabel *label);PangoEllipsizeMode gtk_label_get_ellipsize (GtkLabel *label);gint gtk_label_get_width_chars (GtkLabel *label);gint gtk_label_get_max_width_chars (GtkLabel *label); constgchar * gtk_label_get_label (GtkLabel *label);PangoLayout * gtk_label_get_layout (GtkLabel *label);gboolean gtk_label_get_line_wrap (GtkLabel *label);PangoWrapMode gtk_label_get_line_wrap_mode (GtkLabel *label); GtkWidget* gtk_label_get_mnemonic_widget (GtkLabel *label);gboolean gtk_label_get_selection_bounds (GtkLabel *label,gint *start,gint *end);gboolean gtk_label_get_use_markup (GtkLabel *label);gboolean gtk_label_get_use_underline (GtkLabel *label);gboolean gtk_label_get_single_line_mode (GtkLabel *label);gdouble gtk_label_get_angle (GtkLabel *label);void gtk_label_set_label (GtkLabel *label, constgchar *str);void gtk_label_set_use_markup (GtkLabel *label,gboolean setting);void gtk_label_set_use_underline (GtkLabel *label,gboolean setting);void gtk_label_set_single_line_mode (GtkLabel *label,gboolean single_line_mode);void gtk_label_set_angle (GtkLabel *label,gdouble angle);
GObject +----GInitiallyUnowned +----GtkObject +----GtkWidget +----GtkMisc +----GtkLabel +----GtkAccelLabel +----GtkTipsQuery
"angle"gdouble : Read / Write "attributes"PangoAttrList : Read / Write "cursor-position"gint : Read "ellipsize"PangoEllipsizeMode : Read / Write "justify" GtkJustification : Read / Write "label"gchararray : Read / Write "max-width-chars"gint : Read / Write "mnemonic-keyval"guint : Read "mnemonic-widget" GtkWidget : Read / Write "pattern"gchararray : Write "selectable"gboolean : Read / Write "selection-bound"gint : Read "single-line-mode"gboolean : Read / Write "use-markup"gboolean : Read / Write "use-underline"gboolean : Read / Write "width-chars"gint : Read / Write "wrap"gboolean : Read / Write "wrap-mode"PangoWrapMode : Read / Write
"copy-clipboard" : Run Last / Action "move-cursor" : Run Last / Action "populate-popup" : Run Last
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, a GtkMenuItem, or a GtkOptionMenu.
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:
/* 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);
There's a convenience function to create buttons with a mnemonic label already inside:
/* Pressing Alt+H will activate this button */ 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()
:
/* 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
label = gtk_label_new (NULL); gtk_label_set_markup (GTK_LABEL (label), "<small>Small text</small>");
(See
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()
g_markup_printf_escaped()
Markup strings are just a convenient way to set the gtk_label_set_attributes()
may be a simpler way to set
attributes in some cases. Be careful though;
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()
.
typedef struct _GtkLabel GtkLabel;
This should not be accessed directly. Use the accessor functions as described below.
GtkWidget* gtk_label_new (constgchar *str);
Creates a new label with the given text inside it. You can
pass NULL
|
The text of the label |
Returns : |
the new GtkLabel |
void gtk_label_set_text (GtkLabel *label, constgchar *str);
Sets the text within the GtkLabel widget. It overwrites any text that was there before.
This will also clear any previously set mnemonic accelerators.
|
a GtkLabel |
|
The text you want to set |
void gtk_label_set_attributes (GtkLabel *label,PangoAttrList *attrs);
Sets a TRUE
|
a GtkLabel |
|
a |
void gtk_label_set_markup (GtkLabel *label, constgchar *str);
Parses str
which is marked up with the str
is
external data, you may need to escape it with g_markup_escape_text()
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);
|
a GtkLabel |
|
a markup string (see |
void gtk_label_set_markup_with_mnemonic (GtkLabel *label, constgchar *str);
Parses str
which is marked up with the 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()
.
|
a GtkLabel |
|
a markup string (see |
void gtk_label_set_pattern (GtkLabel *label, constgchar *pattern);
The pattern of underlines you want under the existing text within the GtkLabel widget. For example if the current text of the label says "FooBarBaz" passing a pattern of "___ ___" will underline "Foo" and "Baz" but not "Bar".
|
The GtkLabel you want to set the pattern to. |
|
The pattern as described above. |
void gtk_label_set_justify (GtkLabel *label, 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_misc_set_alignment()
instead. gtk_label_set_justify()
has no
effect on labels containing only a single line.
|
a GtkLabel |
|
a GtkJustification |
void gtk_label_set_ellipsize (GtkLabel *label,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.
|
a GtkLabel |
|
a |
Since 2.6
void gtk_label_set_width_chars (GtkLabel *label,gint n_chars);
Sets the desired width in characters of label
to n_chars
.
|
a GtkLabel |
|
the new desired width, in characters. |
Since 2.6
void gtk_label_set_max_width_chars (GtkLabel *label,gint n_chars);
Sets the desired maximum width in characters of label
to n_chars
.
|
a GtkLabel |
|
the new desired maximum width, in characters. |
Since 2.6
void gtk_label_get (GtkLabel *label,gchar **str);
gtk_label_get
is deprecated and should not be used in newly-written code.
Gets the current string of text within the GtkLabel and writes it to
the given str
argument. It does not make a copy of this string so you
must not write to it.
|
The GtkLabel widget you want to get the text from. |
|
The reference to the pointer you want to point to the text. |
guint gtk_label_parse_uline (GtkLabel *label, constgchar *string);
gtk_label_parse_uline
is deprecated and should not be used in newly-written code.
Parses the given string for underscores and converts the next character to an underlined character. The last character that was underlined will have its lower-cased accelerator keyval returned (i.e. "_File" would return the keyval for "f". This is probably only used within the GTK+ library itself for menu items and such.
|
The GtkLabel you want to affect. |
|
The string you want to parse for underlines. |
Returns : |
The lowercase keyval of the last character underlined. |
void gtk_label_set_line_wrap (GtkLabel *label,gboolean wrap);
Toggles line wrapping within the GtkLabel widget. TRUE
FALSE
Note that setting line wrapping to TRUE
gtk_widget_set_size_request()
.
|
a GtkLabel |
|
the setting |
void gtk_label_set_line_wrap_mode (GtkLabel *label,PangoWrapMode 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
|
a GtkLabel |
|
the line wrapping mode |
Since 2.10
#define gtk_label_set gtk_label_set_text
gtk_label_set
is deprecated and should not be used in newly-written code.
Aliases gtk_label_set_text()
. Probably used for backward compatibility with
GTK+ 1.0.x.
void gtk_label_get_layout_offsets (GtkLabel *label,gint *x,gint *y);
Obtains the coordinates where the label will draw the PANGO_PIXELS()
|
a GtkLabel |
|
location to store X offset of layout, or NULL |
|
location to store Y offset of layout, or NULL |
guint gtk_label_get_mnemonic_keyval (GtkLabel *label);
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
|
a GtkLabel |
Returns : |
GDK keyval usable for accelerators, or |
gboolean gtk_label_get_selectable (GtkLabel *label);
Gets the value set by gtk_label_set_selectable()
.
|
a GtkLabel |
Returns : |
TRUE |
constgchar * gtk_label_get_text (GtkLabel *label);
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()
)
|
a GtkLabel |
Returns : |
the text in the label widget. This is the internal string used by the label, and must not be modified. |
GtkWidget* gtk_label_new_with_mnemonic (constgchar *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.
|
The text of the label, with an underscore in front of the mnemonic character |
Returns : |
the new GtkLabel |
void gtk_label_select_region (GtkLabel *label,gint start_offset,gint 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.
|
a GtkLabel |
|
start offset (in characters not bytes) |
|
end offset (in characters not bytes) |
void gtk_label_set_mnemonic_widget (GtkLabel *label, GtkWidget *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 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.
void gtk_label_set_selectable (GtkLabel *label,gboolean setting);
Selectable labels allow the user to select text from the label, for copy-and-paste.
|
a GtkLabel |
|
TRUE |
void gtk_label_set_text_with_mnemonic (GtkLabel *label, constgchar *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()
.
|
a GtkLabel |
|
a string |
PangoAttrList * gtk_label_get_attributes (GtkLabel *label);
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)).
|
a GtkLabel |
Returns : |
the attribute list, or NULL |
GtkJustification gtk_label_get_justify (GtkLabel *label);
Returns the justification of the label. See gtk_label_set_justify()
.
|
a GtkLabel |
Returns : |
GtkJustification |
PangoEllipsizeMode gtk_label_get_ellipsize (GtkLabel *label);
Returns the ellipsizing position of the label. See gtk_label_set_ellipsize()
.
|
a GtkLabel |
Returns : |
|
Since 2.6
gint gtk_label_get_width_chars (GtkLabel *label);
Retrieves the desired width of label
, in characters. See
gtk_label_set_width_chars()
.
|
a GtkLabel |
Returns : |
the width of the label in characters. |
Since 2.6
gint gtk_label_get_max_width_chars (GtkLabel *label);
Retrieves the desired maximum width of label
, in characters. See
gtk_label_set_width_chars()
.
|
a GtkLabel |
Returns : |
the maximum width of the label in characters. |
Since 2.6
constgchar * gtk_label_get_label (GtkLabel *label);
Fetches the text from a label widget including any embedded
underlines indicating mnemonics and Pango markup. (See
gtk_label_get_text()
).
|
a GtkLabel |
Returns : |
the text of the label widget. This string is owned by the widget and must not be modified or freed. |
PangoLayout * gtk_label_get_layout (GtkLabel *label);
Gets the gtk_label_get_layout_offsets()
.
The returned layout is owned by the label so need not be
freed by the caller.
|
a GtkLabel |
Returns : |
the |
gboolean gtk_label_get_line_wrap (GtkLabel *label);
Returns whether lines in the label are automatically wrapped.
See gtk_label_set_line_wrap()
.
|
a GtkLabel |
Returns : |
TRUE |
PangoWrapMode gtk_label_get_line_wrap_mode (GtkLabel *label);
Returns line wrap mode used by the label. See gtk_label_set_line_wrap_mode()
.
|
a GtkLabel |
Returns : |
TRUE |
Since 2.10
GtkWidget* gtk_label_get_mnemonic_widget (GtkLabel *label);
Retrieves the target of the mnemonic (keyboard shortcut) of this
label. See gtk_label_set_mnemonic_widget()
.
|
a GtkLabel |
Returns : |
the target of the label's mnemonic, or NULL |
gboolean gtk_label_get_selection_bounds (GtkLabel *label,gint *start,gint *end);
Gets the selected range of characters in the label, returning TRUE
|
a GtkLabel |
|
return location for start of selection, as a character offset |
|
return location for end of selection, as a character offset |
Returns : |
TRUE |
gboolean gtk_label_get_use_markup (GtkLabel *label);
Returns whether the label's text is interpreted as marked up with
the gtk_label_set_use_markup()
.
|
a GtkLabel |
Returns : |
TRUE |
gboolean gtk_label_get_use_underline (GtkLabel *label);
Returns whether an embedded underline in the label indicates a
mnemonic. See gtk_label_set_use_underline()
.
|
a GtkLabel |
Returns : |
TRUE |
gboolean gtk_label_get_single_line_mode (GtkLabel *label);
Returns whether the label is in single line mode.
|
a GtkLabel |
Returns : |
TRUE |
Since 2.6
gdouble gtk_label_get_angle (GtkLabel *label);
Gets the angle of rotation for the label. See
gtk_label_set_angle()
.
|
a GtkLabel |
Returns : |
the angle of rotation for the label |
Since 2.6
void gtk_label_set_label (GtkLabel *label, constgchar *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.
|
a GtkLabel |
|
the new text to set for the label |
void gtk_label_set_use_markup (GtkLabel *label,gboolean setting);
Sets whether the text of the label contains markup in gtk_label_set_markup()
.
|
a GtkLabel |
|
TRUE |
void gtk_label_set_use_underline (GtkLabel *label,gboolean setting);
If true, an underline in the text indicates the next character should be used for the mnemonic accelerator key.
|
a GtkLabel |
|
TRUE |
void gtk_label_set_single_line_mode (GtkLabel *label,gboolean single_line_mode);
Sets whether the label is in single line mode.
|
a GtkLabel |
|
TRUE |
Since 2.6
void gtk_label_set_angle (GtkLabel *label,gdouble 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.
|
a GtkLabel |
|
the angle that the baseline of the label makes with the horizontal, in degrees, measured counterclockwise |
Since 2.6
"angle"
property"angle"gdouble : Read / Write
The angle that the baseline of the label makes with the horizontal, in degrees, measured counterclockwise. An angle of 90 reads from from bottom to top, an angle of 270, from top to bottom. Ignored if the label is selectable, wrapped, or ellipsized.
Allowed values: [0,360]
Default value: 0
Since 2.6
"attributes"
property"attributes"PangoAttrList : Read / Write
A list of style attributes to apply to the text of the label.
"cursor-position"
property"cursor-position"gint : Read
The current position of the insertion cursor in chars.
Allowed values: >= 0
Default value: 0
"ellipsize"
property"ellipsize"PangoEllipsizeMode : Read / Write
The preferred place to ellipsize the string, if the label does
not have enough room to display the entire string, specified as a
Note that setting this property to a value other than
PANGO_ELLIPSIZE_NONE
TRUE
gtk_widget_set_size_request()
and
gtk_label_set_width_chars()
.
Default value: PANGO_ELLIPSIZE_NONE
Since 2.6
"justify"
property"justify" GtkJustification : Read / Write
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.
Default value: GTK_JUSTIFY_LEFT
"max-width-chars"
property"max-width-chars"gint : Read / Write
The desired maximum width of the label, in characters. If this property is set to -1, the width will be calculated automatically, otherwise the label will request space for no more than the requested number of characters. If the "width-chars" property is set to a positive value, then the "max-width-chars" property is ignored.
Allowed values: >= -1
Default value: -1
Since 2.6
"mnemonic-keyval"
property"mnemonic-keyval"guint : Read
The mnemonic accelerator key for this label.
Default value: 16777215
"mnemonic-widget"
property"mnemonic-widget" GtkWidget : Read / Write
The widget to be activated when the label's mnemonic key is pressed.
"pattern"
property"pattern"gchararray : Write
A string with _ characters in positions correspond to characters in the text to underline.
Default value: NULL
"selectable"
property"selectable"gboolean : Read / Write
Whether the label text can be selected with the mouse.
Default value: FALSE
"selection-bound"
property"selection-bound"gint : Read
The position of the opposite end of the selection from the cursor in chars.
Allowed values: >= 0
Default value: 0
"single-line-mode"
property"single-line-mode"gboolean : Read / Write
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.
Default value: FALSE
Since 2.6
"use-markup"
property"use-markup"gboolean : Read / Write
The text of the label includes XML markup. See pango_parse_markup().
Default value: FALSE
"use-underline"
property"use-underline"gboolean : Read / Write
If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key.
Default value: FALSE
"width-chars"
property"width-chars"gint : Read / Write
The desired width of the label, in characters. If this property is set to -1, the width will be calculated automatically, otherwise the label will request either 3 characters or the property value, whichever is greater. If the "width-chars" property is set to a positive value, then the "max-width-chars" property is ignored.
Allowed values: >= -1
Default value: -1
Since 2.6
"wrap"
property"wrap"gboolean : Read / Write
If set, wrap lines if the text becomes too wide.
Default value: FALSE
"wrap-mode"
property"wrap-mode"PangoWrapMode : Read / Write
If line wrapping is on (see the "wrap" property) this controls
how the line wrapping is done. The default is PANGO_WRAP_WORD
Default value: PANGO_WRAP_WORD
Since 2.10
"copy-clipboard"
signalvoid user_function (GtkLabel *label,gpointer user_data) : Run Last / Action
|
the object which received the signal. |
|
user data set when the signal handler was connected. |
"move-cursor"
signalvoid user_function (GtkLabel *label, GtkMovementStep arg1,gint arg2,gboolean arg3,gpointer user_data) : Run Last / Action
|
the object which received the signal. |
|
|
|
|
|
|
|
user data set when the signal handler was connected. |