6. Rulers

Ruler widgets are used to indicate the location of the mouse pointer in a given window. A window can have a vertical ruler spanning across the width and a horizontal ruler spanning down the height. A small triangular indicator on the ruler shows the exact location of the pointer relative to the ruler.

A ruler must first be created. Horizontal and vertical rulers are created using

$ruler = Gtk2::HRuler->new;	# horizontal ruler

$ruler = Gtk2::VRuler->new;	# vertical ruler

Once a ruler is created, we can define the unit of measurement. Units of measure for rulers can be'pixels'/'GTK_PIXELS', 'inches'/'GTK_INCHES' or 'centimeters'/'GTK_CENTIMETERS'. This is set using

Gtk2::Ruler->set_metric($ruler, $metric);

The default measure is 'pixels'/'GTK_PIXELS'.

Gtk2::Ruler->set_metric($ruler, 'pixels');

Other important characteristics of a ruler are how to mark the units of scale and where the position indicator is initially placed. These are set for a ruler using

Gtk2::Ruler->set_range($ruler, $lower, $upper, $position, $max_size);

The lower and upper arguments define the extent of the ruler, and max_size is the largest possible number that will be displayed. Position defines the initial position of the pointer indicator within the ruler.

A vertical ruler can span an 800 pixel wide window thus

Gtk2::Ruler->set_range($vruler, 0, 800, 0, 800);

The markings displayed on the ruler will be from 0 to 800, with a number for every 100 pixels. If instead we wanted the ruler to range from 7 to 16, we would code

Gtk2::Ruler->set_range($vruler, 7, 16, 0, 20);

The indicator on the ruler is a small triangular mark that indicates the position of the pointer relative to the ruler. If the ruler is used to follow the mouse pointer, the motion_notify_event signal should be connected to the motion_notify_event method of the ruler. To follow all mouse movements within a window area, we would use

$area->signal_connect(motion_notify_event => sub {
		$ruler->motion_notify_event;
	});

The following example creates a drawing area with a horizontal ruler above it and a vertical ruler to the left of it. The size of the drawing area is 600 pixels wide by 400 pixels high. The horizontal ruler spans from 7 to 13 with a mark every 100 pixels, while the vertical ruler spans from 0 to 400 with a mark every 100 pixels. Placement of the drawing area and the rulers is done using a table.



use Glib qw/TRUE FALSE/;
use Gtk2 '-init';

use constant { XSIZE => 600, YSIZE => 400 };

# Create the main window
my $window = Gtk2::Window->new('toplevel');
$window->signal_connect(delete_event => sub { Gtk2->main_quit; FALSE; });
$window->set_border_width(10);

# Create a table for placing the ruler and the drawing area
my $table = Gtk2::Table->new(3, 2, FALSE);
$window->add($table);

my $area = Gtk2::DrawingArea->new;
$area->set_size_request(XSIZE, YSIZE);
$table->attach($area, 1, 2, 1, 2,
               ['expand', 'fill'], 'fill',
	       0, 0);
$area->set_events(['pointer-motion-mask', 'pointer-motion-hint-mask']);

# The horizontal ruler goes on top. As the mouse moves across the drawing
# area, a motion_notify_event is passed to the appropriate event handler for
# the ruler.
$hrule = Gtk2::HRuler->new;
$hrule->set_metric('pixels');
$hrule->set_range(7, 13, 0, 20);
$area->signal_connect(motion_notify_event => sub {
		my ($widget, $event) = @_;
		$hrule->event($event);
	});
$table->attach($hrule, 1, 2, 0, 1,
               ['expand', 'shrink', 'fill'], 'fill',
	       0, 0);

# The vertical ruler goes on the left. As the mouse moves across the drawing
# area, a motion_notify_event is passed to the appropriate event handler for
# the ruler.
$vrule = Gtk2::VRuler->new;
$vrule->set_metric('pixels');
$vrule->set_range(0, YSIZE, 10, YSIZE);
$area->signal_connect(motion_notify_event => sub {
		# take the event reference directly from the stack.
		$vrule->event($_[1]);
	});
$table->attach($vrule, 0, 1, 1, 2,
               'fill', ['expand', 'shrink', 'fill'],
	       0, 0);

# Now show everything.
$area->show;
$vrule->show;
$hrule->show;
$table->show;
$window->show;

Gtk2->main;

0;