GTK+ Reference Manual
<<< Prev | Home | Up | Next >>>

## GtkLabel

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

##

# Synopsis

    #include <gtk/gtk.h>

    struct      GtkLabel;
    GtkWidget*  gtk_label_new                   (const char *str);
    void        gtk_label_set_text              (GtkLabel *label,
                                                 const char *str);
    void        gtk_label_set_attributes        (GtkLabel *label,
                                                 PangoAttrList *attrs);
    void        gtk_label_set_markup            (GtkLabel *label,
                                                 const gchar *str);
    void        gtk_label_set_markup_with_mnemonic
                                                (GtkLabel *label,
                                                 const gchar *str);
    void        gtk_label_set_pattern           (GtkLabel *label,
                                                 const gchar *pattern);
    void        gtk_label_set_justify           (GtkLabel *label,
                                                 GtkJustification jtype);
    void        gtk_label_get                   (GtkLabel *label,
                                                 char **str);
    guint       gtk_label_parse_uline           (GtkLabel *label,
                                                 const gchar *string);
    void        gtk_label_set_line_wrap         (GtkLabel *label,
                                                 gboolean wrap);
    #define     gtk_label_set
    void        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);
    G_CONST_RETURN gchar* gtk_label_get_text    (GtkLabel *label);
    GtkWidget*  gtk_label_new_with_mnemonic     (const char *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,
                                                 const gchar *str);
    PangoAttrList* gtk_label_get_attributes     (GtkLabel *label);
    GtkJustification gtk_label_get_justify      (GtkLabel *label);
    G_CONST_RETURN gchar* gtk_label_get_label   (GtkLabel *label);
    PangoLayout* gtk_label_get_layout           (GtkLabel *label);
    gboolean    gtk_label_get_line_wrap         (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);
    void        gtk_label_set_label             (GtkLabel *label,
                                                 const gchar *str);
    void        gtk_label_set_use_markup        (GtkLabel *label,
                                                 gboolean setting);
    void        gtk_label_set_use_underline     (GtkLabel *label,
                                                 gboolean setting);

## 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, a GtkMenuItem, or a GtkOptionMenu.

### 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:

      /* 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

      /* 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);

gtk_label_set_mnemonic_widget():

### 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:

      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 </>/& 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().

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

## Details

### struct GtkLabel

    struct GtkLabel
    {
      GtkMisc misc;

      /*< private >*/
      gchar  *label;
      guint   jtype : 2;
      guint   wrap : 1;
      guint   use_underline : 1;
      guint   use_markup : 1;

      guint   mnemonic_keyval;

      gchar  *text;
      PangoAttrList *attrs;
      PangoAttrList *effective_attrs;

      PangoLayout *layout;

      GtkWidget *mnemonic_widget;
      GtkWindow *mnemonic_window;

      GtkLabelSelectionInfo *select_info;
    };

This should not be accessed directly. Use the accessor functions as described below.

### 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.

`str` : | The text of the label
Returns : | the new GtkLabel

### gtk_label_set_text ()

    void        gtk_label_set_text              (GtkLabel *label,
                                                 const char *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.

`label` : | a GtkLabel
`str` : | The text you want to set.

### gtk_label_set_attributes ()

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

Sets a PangoAttrList; 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 `TRUE`.

`label` : | a GtkLabel
`attrs` : | a PangoAttrList

### gtk_label_set_markup ()

    void        gtk_label_set_markup            (GtkLabel *label,
                                                 const gchar *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.

`label` : | a GtkLabel
`str` : | a markup string (see Pango markup format)

### gtk_label_set_markup_with_mnemonic ()

    void        gtk_label_set_markup_with_mnemonic
                                                (GtkLabel *label,
                                                 const gchar *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().

`label` : | a GtkLabel
`str` : | a markup string (see Pango markup format)

### gtk_label_set_pattern ()

    void        gtk_label_set_pattern           (GtkLabel *label,
                                                 const gchar *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".

`label` : | The GtkLabel you want to set the pattern to.
`pattern` : | The pattern as described above.

### gtk_label_set_justify ()

    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.

`label` : | a GtkLabel
`jtype` : | a GtkJustification

### gtk_label_get ()

    void        gtk_label_get                   (GtkLabel *label,
                                                 char **str);

### Warning

`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.

`label` : | The GtkLabel widget you want to get the text from.
`str` : | The reference to the pointer you want to point to the text.

### gtk_label_parse_uline ()

    guint       gtk_label_parse_uline           (GtkLabel *label,
                                                 const gchar *string);

### Warning

`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.

`label` : | The GtkLabel you want to affect.
`string` : | The string you want to parse for underlines.
Returns : | The lowercase keyval of the last character underlined.

### gtk_label_set_line_wrap ()

    void        gtk_label_set_line_wrap         (GtkLabel *label,
                                                 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.

`label` : | a GtkLabel
`wrap` : | the setting

### gtk_label_set

    #define  gtk_label_set           gtk_label_set_text

### Warning

`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.

### gtk_label_get_layout_offsets ()

    void        gtk_label_get_layout_offsets    (GtkLabel *label,
                                                 gint *x,
                                                 gint *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. Of course you will need to create a GtkEventBox to receive the events, and pack the label inside it, since labels are a GTK_NO_WINDOW widget. Remember when using the PangoLayout functions you need to convert to and from pixels using PANGO_PIXELS() or PANGO_SCALE.

`label` : | a GtkLabel
`x` : | location to store X offset of layout, or `NULL`
`y` : | location to store Y offset of layout, or `NULL`

### gtk_label_get_mnemonic_keyval ()

    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 GDK_VoidSymbol.

`label` : | a GtkLabel
Returns : | GDK keyval usable for accelerators, or GDK_VoidSymbol

### gtk_label_get_selectable ()

    gboolean    gtk_label_get_selectable        (GtkLabel *label);

Gets the value set by gtk_label_set_selectable().

`label` : | a GtkLabel
Returns : | `TRUE` if the user can copy text from the label

### gtk_label_get_text ()

    G_CONST_RETURN gchar* 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())

`label` : | 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.

`str` : | The text of the label, with an underscore in front of the mnemonic character
Returns : | the new GtkLabel

### gtk_label_select_region ()

    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.

`label` : | 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 *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 "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 GtkLabel
`widget` : | the target GtkWidget

### gtk_label_set_selectable ()

    void        gtk_label_set_selectable        (GtkLabel *label,
                                                 gboolean setting);

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

`label` : | 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 *label,
                                                 const gchar *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().

`label` : | a GtkLabel
`str` : | a string

### gtk_label_get_attributes ()

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

`label` : | a GtkLabel
Returns : | the attribute list, or `NULL` if none was set.

### gtk_label_get_justify ()

    GtkJustification gtk_label_get_justify      (GtkLabel *label);

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

`label` : | a GtkLabel
Returns : | GtkJustification

### gtk_label_get_label ()

    G_CONST_RETURN gchar* 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()).

`label` : | 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 *label);

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.

`label` : | a GtkLabel
Returns : | the PangoLayout for this label

### gtk_label_get_line_wrap ()

    gboolean    gtk_label_get_line_wrap         (GtkLabel *label);

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

`label` : | a GtkLabel
Returns : | `TRUE` if the lines of the label are automatically wrapped.

### gtk_label_get_mnemonic_widget ()

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

`label` : | a GtkLabel
Returns : | the target of the label's mnemonic, or `NULL` if none has been set and the default algorithm will be used.

### gtk_label_get_selection_bounds ()

    gboolean    gtk_label_get_selection_bounds  (GtkLabel *label,
                                                 gint *start,
                                                 gint *end);

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

`label` : | a GtkLabel
`start` : | return location for start of selection, as a character offset
`end` : | return location for end of selection, as a character offset
Returns : | `TRUE` if selection is non-empty

### gtk_label_get_use_markup ()

    gboolean    gtk_label_get_use_markup        (GtkLabel *label);

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 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 *label);

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

`label` : | a GtkLabel
Returns : | `TRUE` whether an embedded underline in the label indicates the mnemonic accelerator keys.

### gtk_label_set_label ()

    void        gtk_label_set_label             (GtkLabel *label,
                                                 const gchar *str);

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 GtkLabel
`str` : | the new text to set for the label

### gtk_label_set_use_markup ()

    void        gtk_label_set_use_markup        (GtkLabel *label,
                                                 gboolean setting);

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

`label` : | 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 *label,
                                                 gboolean setting);

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

`label` : | a GtkLabel
`setting` : | `TRUE` if underlines in the text indicate mnemonics

<<< Prev | Home | Up | Next >>>
Display Widgets | GtkProgressBar