Contenedor de un sólo elemento
Los contenedores de un sólo elemento derivan de Gtk::Bin, que proporciona los métodos add() y remove() para el widget hijo. Tenga en cuenta que Gtk::Button y Gtk::Window son, técnicamente, contenedores de un sólo elemento, pero ya se ha hablado de ellos anteriormente.
También se habla del widget Gtk::Paned, que le permite dividir una ventana en dos «paneles» separados. El widget, en realidad, contiene dos widgets hijos, pero el número es fijo, por lo que parece apropiado.
- 8.1.1. Marco
- 8.1.2. Con paneles
- 8.1.3. ScrolledWindow
- 8.1.4. AspectFrame
- 8.1.5. Alignment
8.1.1. Marco
Los cuadros pueden encerrar un grupo de widgets dentro de una caja, con un título opcional. Por ejemplo, tal vez quiera poner un grupo de RadioButton o CheckButton en un Frame.
- 8.1.1.1. Ejemplo
8.1.1.1. Ejemplo
File: examplewindow.h (For use with gtkmm 3, not gtkmm 2)
#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H
#include <gtkmm.h>
class ExampleWindow : public Gtk::Window
{
public:
ExampleWindow();
virtual ~ExampleWindow();
protected:
//Child widgets:
Gtk::Frame m_Frame;
};
#endif //GTKMM_EXAMPLEWINDOW_H
File: examplewindow.cc (For use with gtkmm 3, not gtkmm 2)
#include "examplewindow.h"
ExampleWindow::ExampleWindow()
{
/* Set some window properties */
set_title("Frame Example");
set_size_request(300, 300);
/* Sets the border width of the window. */
set_border_width(10);
add(m_Frame);
/* Set the frames label */
m_Frame.set_label("Gtk::Frame Widget");
/* Align the label at the right of the frame */
//m_Frame.set_label_align(Gtk::ALIGN_END, Gtk::ALIGN_START);
/* Set the style of the frame */
m_Frame.set_shadow_type(Gtk::SHADOW_ETCHED_OUT);
show_all_children();
}
ExampleWindow::~ExampleWindow()
{
}
File: main.cc (For use with gtkmm 3, not gtkmm 2)
#include "examplewindow.h"
#include <gtkmm/application.h>
int main(int argc, char *argv[])
{
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
ExampleWindow window;
//Shows the window and returns when it is closed.
return app->run(window);
}
8.1.2. Con paneles
Se pueden usar paneles para dividir un widget en dos mitades, separadas por un divisor móvil. Las dos mitades (paneles) pueden orientarse tanto horizontal (lado a lado) como verticalmente (uno encima de otro).
A diferencia de los otros widgets en esta sección, los paneles no contienen un widget hijo, sino dos, uno en cada panel. Por lo tanto, debe usar los métodos add1() y add2() en vez de add().
Puede ajustar la posición del divisor usando el método set_position(), y probablemente lo necesite.
- 8.1.2.1. Ejemplo
8.1.2.1. Ejemplo
File: messageslist.h (For use with gtkmm 3, not gtkmm 2)
#ifndef GTKMM_EXAMPLE_MESSAGESLIST_H
#define GTKMM_EXAMPLE_MESSAGESLIST_H
#include <gtkmm.h>
class MessagesList: public Gtk::ScrolledWindow
{
public:
MessagesList();
virtual ~MessagesList();
class ModelColumns : public Gtk::TreeModel::ColumnRecord
{
public:
ModelColumns()
{ add(m_col_text); }
Gtk::TreeModelColumn<Glib::ustring> m_col_text;
};
ModelColumns m_Columns;
protected:
Glib::RefPtr<Gtk::ListStore> m_refListStore; //The Tree Model.
Gtk::TreeView m_TreeView; //The Tree View.
};
#endif //GTKMM_EXAMPLE_MESSAGESLIST_H
File: messagetext.h (For use with gtkmm 3, not gtkmm 2)
#ifndef GTKMM_EXAMPLE_MESSAGETEXT_H
#define GTKMM_EXAMPLE_MESSAGETEXT_H
#include <gtkmm.h>
class MessageText : public Gtk::ScrolledWindow
{
public:
MessageText();
virtual ~MessageText();
void insert_text();
protected:
Gtk::TextView m_TextView;
};
#endif //GTKMM_EXAMPLE_MESSAGETEXT_H
File: examplewindow.h (For use with gtkmm 3, not gtkmm 2)
#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H
#include "messageslist.h"
#include "messagetext.h"
#include <gtkmm.h>
class ExampleWindow : public Gtk::Window
{
public:
ExampleWindow();
virtual ~ExampleWindow();
protected:
//Child widgets:
Gtk::Paned m_VPaned;
MessagesList m_MessagesList;
MessageText m_MessageText;
};
#endif //GTKMM_EXAMPLEWINDOW_H
File: messagetext.cc (For use with gtkmm 3, not gtkmm 2)
#include "messagetext.h"
MessageText::MessageText()
{
set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
add(m_TextView);
insert_text();
show_all_children();
}
MessageText::~MessageText()
{
}
void MessageText::insert_text()
{
Glib::RefPtr<Gtk::TextBuffer> refTextBuffer = m_TextView.get_buffer();
Gtk::TextBuffer::iterator iter = refTextBuffer->get_iter_at_offset(0);
refTextBuffer->insert(iter,
"From: [email protected]\n"
"To: [email protected]\n"
"Subject: Made it!\n"
"\n"
"We just got in this morning. The weather has been\n"
"great - clear but cold, and there are lots of fun sights.\n"
"Sojourner says hi. See you soon.\n"
" -Path\n");
}
File: messageslist.cc (For use with gtkmm 3, not gtkmm 2)
#include "messageslist.h"
#include <sstream>
MessagesList::MessagesList()
{
/* Create a new scrolled window, with scrollbars only if needed */
set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
add(m_TreeView);
/* create list store */
m_refListStore = Gtk::ListStore::create(m_Columns);
m_TreeView.set_model(m_refListStore);
/* Add some messages to the window */
for(int i = 0; i < 10; ++i)
{
std::ostringstream text;
text << "message #" << i;
Gtk::TreeModel::Row row = *(m_refListStore->append());
row[m_Columns.m_col_text] = text.str();
}
//Add the Model's column to the View's columns:
m_TreeView.append_column("Messages", m_Columns.m_col_text);
show_all_children();
}
MessagesList::~MessagesList()
{
}
File: examplewindow.cc (For use with gtkmm 3, not gtkmm 2)
#include "examplewindow.h"
ExampleWindow::ExampleWindow()
: m_VPaned(Gtk::ORIENTATION_VERTICAL)
{
set_title ("Paned Windows");
set_border_width(10);
set_default_size(450, 400);
/* Add a vpaned widget to our toplevel window */
add(m_VPaned);
/* Now add the contents of the two halves of the window */
m_VPaned.add1(m_MessagesList);
m_VPaned.add2(m_MessageText);
show_all_children();
}
ExampleWindow::~ExampleWindow()
{
}
File: main.cc (For use with gtkmm 3, not gtkmm 2)
#include "examplewindow.h"
#include <gtkmm/application.h>
int main(int argc, char *argv[])
{
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
ExampleWindow window;
//Shows the window and returns when it is closed.
return app->run(window);
}
8.1.3. ScrolledWindow
Los widgets ScrolledWindow crean un área desplazable. Puede insertar cualquier tipo de widget en una ventana ScrolledWindow, y será accesible sin importar su tamaño mediante el uso de barras de desplazamiento. Tenga en cuenta que ScrolledWindow no es un Gtk::Window, a pesar del nombre ligeramente confuso.
Las ventanas desplazables tienen políticas de desplazamiento que determinan si se muestran las Scrollbar o no. Las políticas se pueden establecer mediante el método set_policy(). La política puede ser Gtk::POLICY_AUTOMATIC o Gtk::POLICY_ALWAYS. Gtk::POLICY_AUTOMATIC hace que la ventana desplazable muestre la barra de desplazamiento sólo si el widget contenido es más grande que el área visible. Gtk::POLICY_ALWAYS hace que la barra de desplazamiento se muestre siempre.
- 8.1.3.1. Ejemplo
8.1.3.1. Ejemplo
Aquí hay un ejemplo simple que incluye 100 botones conmutadores en una ventana ScrolledWindow. Intente redimensionar la ventana para ver reaccionar a las barras de desplazamiento.
File: examplewindow.h (For use with gtkmm 3, not gtkmm 2)
#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H
#include <gtkmm.h>
class ExampleWindow : public Gtk::Dialog
{
public:
ExampleWindow();
virtual ~ExampleWindow();
protected:
//Signal handlers:
void on_button_close();
//Child widgets:
Gtk::ScrolledWindow m_ScrolledWindow;
Gtk::Grid m_Grid;
Gtk::Button m_Button_Close;
};
#endif //GTKMM_EXAMPLEWINDOW_H
File: examplewindow.cc (For use with gtkmm 3, not gtkmm 2)
#include "examplewindow.h"
#include <cstdio>
ExampleWindow::ExampleWindow()
: m_Button_Close("Close")
{
set_title("Gtk::ScrolledWindow example");
set_border_width(0);
set_size_request(300, 300);
m_ScrolledWindow.set_border_width(10);
/* the policy is one of Gtk::POLICY AUTOMATIC, or Gtk::POLICY_ALWAYS.
* Gtk::POLICY_AUTOMATIC will automatically decide whether you need
* scrollbars, whereas Gtk::POLICY_ALWAYS will always leave the scrollbars
* there. The first one is the horizontal scrollbar, the second,
* the vertical. */
m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_ALWAYS);
get_content_area()->pack_start(m_ScrolledWindow);
/* set the spacing to 10 on x and 10 on y */
m_Grid.set_row_spacing(10);
m_Grid.set_column_spacing(10);
/* pack the grid into the scrolled window */
m_ScrolledWindow.add(m_Grid);
/* this simply creates a grid of toggle buttons
* to demonstrate the scrolled window. */
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; j++)
{
char buffer[32];
sprintf(buffer, "button (%d,%d)\n", i, j);
Gtk::Button* pButton = Gtk::manage(new Gtk::ToggleButton(buffer));
m_Grid.attach(*pButton, i, j, 1, 1);
}
}
/* Add a "close" button to the bottom of the dialog */
m_Button_Close.signal_clicked().connect( sigc::mem_fun(*this,
&ExampleWindow::on_button_close));
/* this makes it so the button is the default. */
m_Button_Close.set_can_default();
Gtk::Box* pBox = get_action_area();
if(pBox)
pBox->pack_start(m_Button_Close);
/* This grabs this button to be the default button. Simply hitting
* the "Enter" key will cause this button to activate. */
m_Button_Close.grab_default();
show_all_children();
}
ExampleWindow::~ExampleWindow()
{
}
void ExampleWindow::on_button_close()
{
hide();
}
File: main.cc (For use with gtkmm 3, not gtkmm 2)
#include "examplewindow.h"
#include <gtkmm/application.h>
int main(int argc, char *argv[])
{
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
ExampleWindow window;
//Shows the window and returns when it is closed.
return app->run(window);
}
8.1.4. AspectFrame
El widget AspectFrame se ve como un widget Frame, pero fuerza la relación de aspecto (la razón entre la altura y el ancho) del widget hijo, añadiendo espacio adicional de ser necesario. Por ejemplo, esto le permitiría mostrar una fotografía sin permitirle al usuario distorsionarla horizontal o verticalmente cuando la redimensione.
- 8.1.4.1. Ejemplo
8.1.4.1. Ejemplo
El siguiente programa usa un Gtk::AspectFrame para presentar un área de dibujo cuya relación de aspecto siempre es 2:1, sin importar cómo el usuario redimensiona la ventana superior.
File: examplewindow.h (For use with gtkmm 3, not gtkmm 2)
#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H
#include <gtkmm.h>
class ExampleWindow : public Gtk::Window
{
public:
ExampleWindow();
virtual ~ExampleWindow();
protected:
//Child widgets:
Gtk::AspectFrame m_AspectFrame;
Gtk::DrawingArea m_DrawingArea;
};
#endif //GTKMM_EXAMPLEWINDOW_H
File: examplewindow.cc (For use with gtkmm 3, not gtkmm 2)
#include "examplewindow.h"
ExampleWindow::ExampleWindow()
: m_AspectFrame("2x1", /* label */
Gtk::ALIGN_CENTER, /* center x */
Gtk::ALIGN_CENTER, /* center y */
2.0, /* xsize/ysize = 2 */
false /* ignore child's aspect */)
{
set_title("Aspect Frame");
set_border_width(10);
// Add a child widget to the aspect frame */
// Ask for a 200x200 window, but the AspectFrame will give us a 200x100
// window since we are forcing a 2x1 aspect ratio */
m_DrawingArea.set_size_request(200, 200);
m_AspectFrame.add(m_DrawingArea);
// Add the aspect frame to our toplevel window:
add(m_AspectFrame);
show_all_children();
}
ExampleWindow::~ExampleWindow()
{
}
File: main.cc (For use with gtkmm 3, not gtkmm 2)
#include "examplewindow.h"
#include <gtkmm/application.h>
int main(int argc, char *argv[])
{
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
ExampleWindow window;
//Shows the window and returns when it is closed.
return app->run(window);
}
8.1.5. Alignment
El widget Alignment le permite poner un widget en una posición y tamaño relativos al tamaño del widget Alignment en sí. Por ejemplo, puede usarse para centrar un widget.
Debe especificar las características del Alignment al constructor, o al método set(). En particular, no notará ningún efecto a menos que especifique un número distinto a 1.0 para los parámetros xscale e yscale, porque 1.0 simplemente significa que el widget se expandirá hasta cubrir todo el espacio disponible.
- 8.1.5.1. Ejemplo
8.1.5.1. Ejemplo
Este ejemplo alinea a la derecha un botón en una ventana mediante el uso de un widget Alignment.
File: examplewindow.h (For use with gtkmm 3, not gtkmm 2)
#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H
#include <gtkmm.h>
class ExampleWindow : public Gtk::Window
{
public:
ExampleWindow();
virtual ~ExampleWindow();
protected:
//Signal handlers:
void on_button_clicked();
//Child widgets:
Gtk::Alignment m_Alignment;
Gtk::Button m_Button;
};
#endif //GTKMM_EXAMPLEWINDOW_H
File: examplewindow.cc (For use with gtkmm 3, not gtkmm 2)
#include "examplewindow.h"
ExampleWindow::ExampleWindow()
: m_Alignment(Gtk::ALIGN_END, Gtk::ALIGN_CENTER, 0.0, 0.0),
m_Button("Close")
{
set_title("Gtk::Alignement");
set_border_width(10);
set_default_size(200, 50);
add(m_Alignment);
m_Alignment.add(m_Button);
m_Button.signal_clicked().connect( sigc::mem_fun(*this,
&ExampleWindow::on_button_clicked) );
show_all_children();
}
ExampleWindow::~ExampleWindow()
{
}
void ExampleWindow::on_button_clicked()
{
hide();
}
File: main.cc (For use with gtkmm 3, not gtkmm 2)
#include "examplewindow.h"
#include <gtkmm/application.h>
int main(int argc, char *argv[])
{
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
ExampleWindow window;
//Shows the window and returns when it is closed.
return app->run(window);
}
Consulte la sección ProgressBar para ver otro ejemplo que usa un Alignment.




