Xfce Foundation Classes
 « Main Page | Index

Paned Windows


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, you can use these two methods:

void add1(Gtk::Widget& child);

void add2(Gtk::Widget& child);

or these two methods:

void pack1(Widget& child, bool resize = false, bool shrink = true);

void pack2(Widget& child, bool resize = true, bool shrink = true);

The pack1() method adds a child to the top or left pane and pack2() adds a child to the bottom or right pane. If 'resize' is true the child expands when the paned widget is resized. If 'shrink' is true the child can be made smaller than its requsition. Calling add1(child) is equivalent to calling pack1(child, false, true) and calling add2(child) is equivalent to calling pack2(child, true, true).

You can set the position of the handle between the two panes by calling this next method:

void set_position(int position);

The 'position' argument is the pixel position of divider. A negative value means that the position should be unset.

Paned Window Example

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.

The header file for the Paned Window example is <paned.hh>:

#include <xfc/main.hh>
#include <xfc/gtk/scrolledwindow.hh>
#include <xfc/gtk/window.hh>>

using namespace Xfc;

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.hh"
#include <xfc/gtk/cellrenderer.hh>
#include <xfc/gtk/liststore.hh>
#include <xfc/gtk/paned.hh>
#include <xfc/gtk/textview.hh>
#include <xfc/gtk/treeview.hh>

// 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);
    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.signal_destroy().connect(sigc::ptr_fun(&Xfc::Main::quit));

    run();
    return 0;
}

Compiling Paned Window

If you compiled and installed XFC yourself, you will find the source code for Paned Window in the <examples/howto/paned> source directory along with a Makefile. If XFC came pre-installed, or you installed it from an RPM package, you will find the source code in the </usr/share/doc/xfcui-X.X/examples/howto/paned> subdirectory. In this case you will have to create the Makefile yourself (replace X.X with the version number of the libXFCui library you have installed).

To create a Makefile for Paned Window, add the following lines to a new text file and save it using the name "Makefile":

CC = g++

CFLAGS = -Wall -O2

paned: paned.cc paned.hh
    $(CC) paned.cc -o paned $(CFLAGS) `pkg-config xfcui-X.X --cflags --libs`

clean:
    rm -f *.o paned


If you cut and paste these lines make sure the whitespace before $(CC) and rm is a tab character. When The source code for the Paned Window example can be found in the <examples/howto/paned> subdirectory along with a Makefile. If you compile and run this program you will see the following window appear:




Copyright © 2004-2005 The XFC Development Team Top
XFC 4.4