Tutorial

Inti Logo

« Aspect Frames
Viewports »

Paned Window Widgets

The paned window widgets are useful when you want to divide an area into two parts, with the relative size of the two parts controlled by the user. A groove is drawn between the two portions with a handle that the user can drag to change the ratio. The division can either be horizontal (HPaned) or vertical (VPaned).

To create a new paned window, call one of the following constructors:

HPaned();

VPaned();

After creating the paned window widget, you need to add child widgets to its two halves. To do this, use the methods:

void add1(Gtk::Widget& child);

void add2(Gtk::Widget& child);

Gtk::Paned::add1() adds the child widget to the left or top half of the paned window. Gtk::Paned::add2() adds the child widget to the right or bottom half of the paned window.

As an example, we will create part of the user interface of an imaginary email program. A window is divided into two portions vertically, with the top portion being a list of email messages and the bottom portion the text of the email message. Most of the program is pretty straightforward. Notice that the program also shows you how to use the new Gtk::ListStore and Gtk::TextView widgets.

Paned Windows

The header file for Paned Windows is paned.h:

#include<inti/main.h>
#include <inti/core.h>
#include <inti/gtk/scrolledwindow.h>


using namespace Inti;

class MessageList : public Gtk::ScrolledWindow
{
public:
    MessageList();
    virtual
~MessageList();
};

class TextWindow : public Gtk::ScrolledWindow
{
public:
    TextWindow();
    virtual
~TextWindow();
};

class PanedWindow : public Gtk::Window
{
public:
    PanedWindow();
    virtual
~PanedWindow();
};


and the source file is paned.cc:

#include"paned.h"
#include <inti/gtk/liststore.h>
#include <inti/gtk/treeview.h>
#include <inti/gtk/cellrenderer.h>
#include <inti/gtk/paned.h>
#include <inti/gtk/textview.h>


// MessageList

MessageList::MessageList()
{
    set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
    Pointer<Gtk::ListStore> model = new Gtk::ListStore(1, G_TYPE_STRING);
    Gtk::TreeView *tree_view = new Gtk::TreeView(*model);
    add_with_viewport(*tree_view);
    tree_view->show();

    // Add some messages to the window
    for (int i = 0; i < 10; i++)
    {
        String msg = String::format("Message #%d", i);
        Gtk::TreeIter iter = model->append();
        model->set_value(iter, 0, msg);
    }

    Gtk::CellRendererText *cell = new Gtk::CellRendererText;
    Gtk::TreeViewColumn *column = new Gtk::TreeViewColumn ("Messages", cell, "text", 0, 0);
    tree_view->append_column(*column);
}

MessageList::~MessageList()
{
}

// TextWindow

TextWindow::TextWindow()
{
    set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
   
    Gtk::TextView *view = new Gtk::TextView;
    Gtk::TextBuffer *buffer = view->get_buffer();
    add(*view);

    // Add some text to our text widget
    Gtk::TextIter iter = buffer->get_iter_at_offset(0);
    buffer->insert(iter, "From: pathfinder@nasa.gov\n"
                         "To: mom@nasa.gov\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");
    show_all();
}

TextWindow::~TextWindow()
{
}

// PanedWindow

PanedWindow::PanedWindow()
{
    set_title("Paned Windows");
    set_border_width(10);
    set_size_request(450, 400);

    // Create a vpaned widget and add it to our toplevel window
    Gtk::VPaned *vpaned = new Gtk::VPaned;
    add(*vpaned);
    vpaned->show();

    // Now create the contents of the two halves of the window
    MessageList *list = new MessageList;
    vpaned->add1(*list);
    list->show();

    // Create a scrolled text area that displays a "message"
    TextWindow *text = new TextWindow;
    vpaned->add2(*text);
    show();
}

PanedWindow::~PanedWindow()
{
}

int main (int argc, char *argv[])
{
    using namespace Main;

    init(&argc, &argv);

    PanedWindow window;
    window.sig_destroy().connect(slot(&Inti::Main::quit));

    run();
    return 0;
}




« Aspect Frames Index
Container Widgets
Top
Viewports »