Tutorial

Inti Logo

« Viewports
Button Boxes »

Scrolled Windows

Scrolled windows are used to create a scrollable area with another widget inside it. You may insert any type of widget into a scrolled window, and it will be accessible regardless of the size by using the scrollbars.

The following constructors can be used to create a new scrolled window.

ScrolledWindow();

ScrolledWindow(Gtk::Adjustment *hadjustment, Gtk::Adjustment *vadjustment);

The hadjustment argument is the adjustment for the horizontal direction, and the vadjustment argument the adjustment for the vertical direction. Unless you specifically need to create your own Adjustments you should use the first constructor which forces the scrolled window to create it's own Adjustments.

You can set and get the adjustments after the scrolled window has been created using the following four methods:

void set_hadjustment(Gtk::Adjustment *hadjustment);

void set_vadjustment(Gtk::Adjustment *vadjustment);

Gtk::Adjustment* get_hadjustment() const;

Gtk::Adjustment* get_vadjustment() const;

You can set the scrollbar policy with the following method:

void set_policy(Gtk::PolicyType hscrollbar_policy, Gtk::PolicyType vscrollbar_policy);

The first argument sets the policy for the horizontal scrollbar, and the second the policy for the vertical scrollbar.

The policy may be one of values from the Gtk::PolicyType enumeration:
You place your object into the scrolled window using the following method:

void add_with_viewport(Gtk::Widget& child);

Here is a simple example that packs a table with 100 toggle buttons into a scrolled window. I've only commented on the parts that may be new to you.

Scrolled Window

The header file for Gtk::ScrolledWindow example is scrolledwindow.h:

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


using namespace Inti;

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

and the source file is scrolledwindow.cc:

#include"scrolledwindow.h"
#include <inti/gtk/scrolledwindow.h>
#include <inti/gtk/table.h>
#include <inti/gtk/togglebutton.h>
#include <inti/gtk/buttonbox.h>


ScrolledWindow::ScrolledWindow()
{
    set_title("Gtk::ScrolledWindow example");
    set_border_width(0);
    set_size_request(300, 300);
   
    // Create a new scrolled window.
    Gtk::ScrolledWindow *scrolled_window = new Gtk::ScrolledWindow;
    scrolled_window->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.
    scrolled_window->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_ALWAYS);
   
    // The dialog window is created with a vbox (client_area) packed into it.                               
    client_area()->pack_start(*scrolled_window);
    scrolled_window->show();
   
    // Create a table of 10 by 10 squares.
    Gtk::Table *table = new Gtk::Table(10, 10);
   
    // Set the spacing to 10 on x and 10 on y
    table->set_row_spacings(10);
    table->set_col_spacings(10);
   
    // Pack the table into the scrolled window
    scrolled_window->add_with_viewport(*table);
    table->show();
   
    // This simply creates a grid of toggle buttons on the table to demonstrate the scrolled window.
    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 10; j++)
        {
            String text = String::format("button (%d,%d)\n", i, j);
            Gtk::ToggleButton *button = new Gtk::ToggleButton(text);
            table->attach(*button, i, i + 1, j, j + 1);
            button->show();
        }
    }

    // Add a "close" button to the bottom of the dialog
    Gtk::Button *button = new Gtk::Button("close");
    button->sig_clicked().connect(slot(this, &ScrolledWindow::dispose));

    // This makes it so the button is the default.
    button->set_flags(Gtk::CAN_DEFAULT);
    action_area()->pack_start(*button);

    // This grabs this button to be the default button. Simply hitting the "Enter" key will activate this button.
    button->grab_default();
    button->show();
}

ScrolledWindow::~ScrolledWindow()
{
}

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

    init(&argc, &argv);

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

    run();
    return 0;
}



« Viewports Index
Container Widgets
Top
Button Boxes »