11.9. 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 method is used to create a new scrolled window.

Gtk::ScrolledWindow.new( hadjustment, vadjustment )

Where the first argument is the adjustment for the horizontal direction, and the second, the adjustment for the vertical direction. These are almost always set to nil.

Gtk::ScrolledWindow#set_policy( hscrollbar_policy, vscrollbar_policy )

This sets the policy to be used with respect to the scrollbars. The first argument is the scrolled window you wish to change. The second sets the policy for the horizontal scrollbar, and the third the policy for the vertical scrollbar.

The policy may be one of:

Gtk::POLICY_AUTOMATIC
Gtk::POLICY_ALWAYS
Gtk::POLICY_NEVER

Gtk::POLICY_AUTOMATIC will automatically decide whether you need scrollbars, whereas Gtk::POLICY_ALWAYS will always leave the scrollbars there. The last option of Gtk::POLICY_NEVER, will never display a scrollbar.

You can then place your object into the scrolled window using one of the following methods.

Gtk::ScrolledWindow#add_with_viewport( child )
Gtk::ScrolledWindow#add( 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.

require 'gtk2'

Gtk.init

# Create a new dialog window for the scrolled window to be
# packed into
window = Gtk::Dialog.new
window.signal_connect( "destroy") { Gtk.main_quit }
window.title=( "Scrolled Window Example" )
window.border_width=( 0 )
window.set_size_request( 300, 300 )

# Create a new scrolled window
scrolled_window = Gtk::ScrolledWindow.new( nil, nil )
scrolled_window.border_width=( 10 )
scrolled_window.set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_ALWAYS )

# The dialog window is created with a vbox packed into it
window.vbox.pack_start( scrolled_window, true, true, 0 )

# Create a table of 10 by 10 squares
table = Gtk::Table.new( 10, 10, false )

# Set 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 )

# This simple creates a grid of toggle buttons on the table
# to demonstrate the scrolled window
for i in 0..10
   for j in 0..10
      label = "Button #{i}, #{j}"
      button = Gtk::ToggleButton.new( label )
      table.attach_defaults( button, i, i + 1, j, j + 1)
   end
end

# add a close button to the bottom of the dialog
button = Gtk::Button.new( Gtk::Stock::CLOSE )
button.signal_connect( "clicked" ) { Gtk.main_quit }

# This make the button the default
button.set_flags( Gtk::Widget::CAN_DEFAULT )
window.action_area.pack_start( button, true, true, 0 )

# This grabs the button to be the default button. Simple hittin
# the enter key will cause this button to activate
button.grab_default

# Show everything
window.show_all
Gtk.main

Prev Next