Chapter 6. Miscellaneous Widgets
Prev |  | Next

## Chapter 6. Miscellaneous Widgets

## Label

Labels are the main method of placing non-editable text in windows, for instance to place a title next to a `Entry` widget. You can specify the text in the constructor, or with the `set_text()` method.

The width of the label will be adjusted automatically. You can produce multi-line labels by putting line breaks ("\n") in the label string.

The label text can be justified using the `set_justify()` method. The widget is also capable of word-wrapping - this can be activated with `set_line_wrap()`.

TODO: gtkmm2: markup.

Reference

### Example

Below is a short example to illustrate these functions. This example makes use of the Frame widget to better demonstrate the label styles. (The Frame widget is explained in the Frame section.)

Figure 6.1. Label

Source Code

File: examplewindow.h

    #ifndef GTKMM_EXAMPLEWINDOW_H
    #define GTKMM_EXAMPLEWINDOW_H

    #include <gtkmm.h>

    class ExampleWindow : public Gtk::Window
    {
    public:
      ExampleWindow();
      virtual ~ExampleWindow();

    protected:

      //Child widgets:
      Gtk::HBox m_HBox;
      Gtk::VBox m_VBox, m_VBox2;
      Gtk::Frame m_Frame_Normal, m_Frame_Multi, m_Frame_Left, m_Frame_Right,
        m_Frame_LineWrapped, m_Frame_FilledWrapped, m_Frame_Underlined;
      Gtk::Label m_Label_Normal, m_Label_Multi, m_Label_Left, m_Label_Right,
        m_Label_LineWrapped, m_Label_FilledWrapped, m_Label_Underlined;
    };

    #endif //GTKMM_EXAMPLEWINDOW_H

File: examplewindow.cc

    #include "examplewindow.h"
    #include <iostream>

    ExampleWindow::ExampleWindow()
    :
      m_HBox(false, 5),
      m_VBox(false, 5),
      m_Frame_Normal("Normal Label"),
      m_Frame_Multi("Multi-line Label"),
      m_Frame_Left("Left Justified Label"),
      m_Frame_Right("Right Justified Label"),
      m_Frame_LineWrapped("Line wrapped label"),
      m_Frame_FilledWrapped("Filled, wrapped label"),
      m_Frame_Underlined("Underlined label"),
      m_Label_Normal("_This is a Normal label", true),
      m_Label_Multi("This is a Multi-line label.\nSecond line\nThird line"),
      m_Label_Left("This is a Left-Justified\nMulti-line label.\nThird line"),
      m_Label_Right("This is a Right-Justified\nMulti-line label.\nFourth line, (j/k)"),
      m_Label_Underlined("This label is underlined!\nThis one is underlined in quite a funky fashion")
    {
      set_title("Label");
      set_border_width(5);

      add(m_HBox);

      m_HBox.pack_start(m_VBox, Gtk::PACK_SHRINK);

      m_Frame_Normal.add(m_Label_Normal);
      m_VBox.pack_start(m_Frame_Normal, Gtk::PACK_SHRINK);

      m_Frame_Multi.add(m_Label_Multi);
      m_VBox.pack_start(m_Frame_Multi, Gtk::PACK_SHRINK);

      m_Label_Left.set_justify(Gtk::JUSTIFY_LEFT);
      m_Frame_Left.add(m_Label_Left);
      m_VBox.pack_start(m_Frame_Left, Gtk::PACK_SHRINK);

      m_Label_Right.set_justify(Gtk::JUSTIFY_RIGHT);
      m_Frame_Right.add(m_Label_Right);
      m_VBox.pack_start(m_Frame_Right, Gtk::PACK_SHRINK);

      m_HBox.pack_start(m_VBox2, Gtk::PACK_SHRINK);

      m_Label_LineWrapped.set_text("This is an example of a line-wrapped label.  It " \
        "should not be taking up the entire             " /* big space to test spacing */\
        "width allocated to it, but automatically " \
        "wraps the words to fit.  " \
        "The time has come, for all good men, to come to " \
        "the aid of their party.  " \
        "The sixth sheik's six sheep's sick.\n" \
        "     It supports multiple paragraphs correctly, " \
        "and  correctly   adds "\
        "many          extra  spaces. ");
      m_Label_LineWrapped.set_line_wrap();
      m_Frame_LineWrapped.add(m_Label_LineWrapped);
      m_VBox2.pack_start(m_Frame_LineWrapped, Gtk::PACK_SHRINK);

      m_Label_FilledWrapped.set_text("This is an example of a line-wrapped, filled label.  " \
        "It should be taking " \
        "up the entire              width allocated to it.  " \
        "Here is a sentence to prove "\
        "my point.  Here is another sentence. "\
        "Here comes the sun, do de do de do.\n"\
        "    This is a new paragraph.\n"\
        "    This is another newer, longer, better " \
        "paragraph.  It is coming to an end, "\
        "unfortunately.");
      m_Label_FilledWrapped.set_justify(Gtk::JUSTIFY_FILL);
      m_Label_FilledWrapped.set_line_wrap();
      m_Frame_FilledWrapped.add(m_Label_FilledWrapped);
      m_VBox2.pack_start(m_Frame_FilledWrapped, Gtk::PACK_SHRINK);

      m_Label_Underlined.set_justify(Gtk::JUSTIFY_LEFT);
      m_Label_Underlined.set_pattern ("_________________________ _ _________ _ ______     __ _______ ___");
      m_Frame_Underlined.add(m_Label_Underlined);
      m_VBox2.pack_start(m_Frame_Underlined, Gtk::PACK_SHRINK);

      show_all_children();
    }

    ExampleWindow::~ExampleWindow()
    {
    }

File: main.cc

    #include <gtkmm/main.h>
    #include "examplewindow.h"

    int main(int argc, char *argv[])
    {
      Gtk::Main kit(argc, argv);

      ExampleWindow window;
      Gtk::Main::run(window); //Shows the window and returns when it is closed.

      return 0;
    }

Prev | Up | Next
Example | Home | Entry