10.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

Gtk::HRuler.new    # horizontal ruler

Gtk::VRuler.new    # vertical ruler

Once a ruler is created, we can define the unit of measurement. Units of measure for rulers can be Gtk::PIXELS, Gtk::INCHES or Gtk::CENTIMETERS. This is set using

Gtk::Ruler#set_metric( metric )
Gtk::Ruler#metric=( metric )

The default measure is Gtk::PIXELS.

Gtk::Ruler#set_metric( Gtk::PIXELS )
Gtk::Ruler#metric=( Gtk::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

Gtk::Ruler#set_range( lower, upper, position, max_size )
Gtk::Ruler#range=( 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

Gtk::Ruler#set_range( 0, 800, 0, 800 )
Gtk::Ruler#range=( 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

Gtk::Ruler#set_range( 7, 16, 0, 20 )
Gtk::Ruler#range=( 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

??????

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.

INSERT RULER SCREENSHOT HERE

CODE
CODE
CODE
CODE
CODE

Prev Next