Etiqueta

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.

El ancho de la etiqueta se ajusta automáticamente. Puede producir etiquetas con varias líneas poniendo saltos de línea («\n») en la cadena de la etiqueta.

El texto de la etiqueta se puede justificar usando el método set_justify(). El widget también es capaz de ajustar el texto, lo que se puede activar con 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. Ejemplo

Abajo se muestra un ejemplo corto que ilustra estas funciones. Este ejemplo hace uso del widget «Marco» para demostrar mejor los estilos de etiquetas. (El widget «Marco» se explica en la sección Marco). Esto es posible gracias que el primer carácter de m_Label_Normal aparece subrayado cuando pulsa la tecla Alt.

Figura 8-1Etiqueta

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");

  ExampleWindow window;

  //Shows the window and returns when it is closed.
  return app->run(window, 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("This label is underlined!\n"
          "This one is underlined in quite a funky fashion")
{
  set_title("Label");

  m_HBox.set_margin(5);
  add(m_HBox);

  m_HBox.add(m_VBox);

  m_Frame_Normal.add(m_Label_Normal);
  m_VBox.add(m_Frame_Normal);

  m_Frame_Multi.add(m_Label_Multi);
  m_VBox.add(m_Frame_Multi);

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

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

  m_HBox.add(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.add(m_Label_LineWrapped);
  m_VBox2.add(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.add(m_Label_FilledWrapped);
  m_VBox2.add(m_Frame_FilledWrapped);

  m_Label_Underlined.set_justify(Gtk::Justification::LEFT);
  m_Label_Underlined.set_pattern (
          "_________________________ _ _________ _ ______"
          "     __ _______ ___");
  m_Frame_Underlined.add(m_Label_Underlined);
  m_VBox2.add(m_Frame_Underlined);
}

ExampleWindow::~ExampleWindow()
{
}