Popisek (Label)

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

Šířka popisku se přizpůsobuje automaticky. Můžete vytvářet i víceřádkové popisky vloženém zalomení ("\n") do textového řetězce popisku.

Text popisku může být zarovnán pomocí metody set_justify(). Rovněž zvládá zalamování slov, které se dá aktivovat pomocí set_line_wrap().

Gtk::Label supports some simple formatting, for instance allowing you to make some text bold, colored, or larger. You can do this by providing a string to set_markup(), using the Pango Markup syntax. For instance, <b>bold text</b> and <s>strikethrough text</s> .

Reference

8.1.1. Příklad

Níže je krátký příklad, který ilustruje tyto funkce. Pro lepší ukázku stylů popisku vyžívá widget Frame. (Widget Frame je vysvětlen v kapitole Rám (Frame).) Podle nastavení systému je možné, že první znak v m_Label_Normal se zobrazí podtržený, jen když zmáčknete klávesu Alt.

Obrázek 8-1Popisek (Label)

Source Code

File: examplewindow.h (For use with gtkmm 4)

#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H

#include <gtkmm.h>

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

protected:

  //Child widgets:
  Gtk::Box m_HBox;
  Gtk::Box 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: main.cc (For use with gtkmm 4)

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

int main(int argc, char *argv[])
{
  auto app = Gtk::Application::create("org.gtkmm.example");

  //Shows the window and returns when it is closed.
  return app->make_window_and_run<ExampleWindow>(argc, argv);
}

File: examplewindow.cc (For use with gtkmm 4)

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

ExampleWindow::ExampleWindow()
:
  m_HBox(Gtk::Orientation::HORIZONTAL, 5),
  m_VBox(Gtk::Orientation::VERTICAL, 5),
  m_VBox2(Gtk::Orientation::VERTICAL, 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.\nThird line"),
  m_Label_Underlined("<u>This label is underlined!</u>\n"
          "<u>T</u>h<u>is one is</u> <u>u</u>n<u>derlin</u>ed "
          "in<u> q</u>u<u>ite a f</u>u<u>nky</u> fashion")
{
  set_title("Label");

  m_HBox.set_margin(5);
  set_child(m_HBox);

  m_HBox.append(m_VBox);

  m_Frame_Normal.set_child(m_Label_Normal);
  m_VBox.append(m_Frame_Normal);

  m_Frame_Multi.set_child(m_Label_Multi);
  m_VBox.append(m_Frame_Multi);

  m_Label_Left.set_justify(Gtk::Justification::LEFT);
  m_Frame_Left.set_child(m_Label_Left);
  m_VBox.append(m_Frame_Left);

  m_Label_Right.set_justify(Gtk::Justification::RIGHT);
  m_Frame_Right.set_child(m_Label_Right);
  m_VBox.append(m_Frame_Right);

  m_HBox.append(m_VBox2);

  m_Label_LineWrapped.set_text(
          "This is an example of a line-wrapped label.  It "
          /* add a big space to the next line to test spacing */
          "should not be taking up the entire             "
          "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_wrap();
  m_Frame_LineWrapped.set_child(m_Label_LineWrapped);
  m_VBox2.append(m_Frame_LineWrapped);

  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::Justification::FILL);
  m_Label_FilledWrapped.set_wrap();
  m_Frame_FilledWrapped.set_child(m_Label_FilledWrapped);
  m_VBox2.append(m_Frame_FilledWrapped);

  m_Label_Underlined.set_justify(Gtk::Justification::LEFT);
  m_Label_Underlined.set_use_markup(true);
  m_Frame_Underlined.set_child(m_Label_Underlined);
  m_VBox2.append(m_Frame_Underlined);
}

ExampleWindow::~ExampleWindow()
{
}