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:



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
    virtual ~MessageList();

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

class PanedWindow : public Gtk::Window
    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

    Pointer<Gtk::ListStore> model = new Gtk::ListStore(1, G_TYPE_STRING);
    Gtk::TreeView *tree_view = new Gtk::TreeView(*model);

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


// TextWindow

    Gtk::TextView *view = new Gtk::TextView;
    Gtk::TextBuffer *buffer = view->get_buffer();

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


// PanedWindow

    set_title("Paned Windows");
    set_size_request(450, 400);

    // Create a vpaned widget and add it to our toplevel window
    Gtk::VPaned *vpaned = new Gtk::VPaned;

    // Now create the contents of the two halves of the window
    MessageList *list = new MessageList;

    // Create a scrolled text area that displays a "message"
    TextWindow *text = new TextWindow;


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

    init(&argc, &argv);

    PanedWindow window;

    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`

    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