### Tutorial

« OptionMenu | Statusbars »

## 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 with the following constructors respectively:

`HRuler(); VRuler();`

Once a ruler is created, we can define the unit of measurement. Units of measure for rulers can be one of the values in the Gtk::MetricType enumeration:

- PIXELS - the default
- INCHES
- CENTIMETERS

The metric type can be set by calling:

`void set_metric(Gtk::MetricType 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:

`void set_range(double lower, double upper, double position, double 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. The position argument defines the initial position of the pointer indicator within the ruler.
A vertical ruler can span an 800 pixel wide window thus

`ruler->set_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

`ruler->set_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

`window->sig_motion_notify_event().connect(ruler, ruler->gtk_widget_class()->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.
The header file for rulers is rulers.h

`#include<inti/main.h> #include <inti/core.h> #include <inti/gtk/ruler.h> using namespace Inti; class RulerWindow : public Gtk::Window { public: RulerWindow(); ~RulerWindow(); };`

and the source file is rulers.cc

`#include"rulers.h"
#include <inti/gtk/table.h>
#include <inti/gtk/drawingarea.h>`
`#include <inti/bind.h>
RulerWindow::RulerWindow()
{
set_border_width(10);
// Create a table for placing the ruler and the drawing area
Gtk::Table *table = new Gtk::Table(3, 2);
add(*table);
Gtk::DrawingArea *area = new Gtk::DrawingArea(600, 400);
table->attach(*area, 1, 2, 1, 2, Gtk::EXPAND | Gtk::FILL, Gtk::FILL);
area->set_events(GDK_POINTER_MOTION_MASK | GDK_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.
Gtk::HRuler *hruler = new Gtk::HRuler;
hruler->set_metric(Gtk::PIXELS);
hruler->set_range(7, 13, 0, 20);
area->sig_motion_notify_event().connect(hruler, hruler->gtk_widget_class()->motion_notify_event);
table->attach(*hruler, 1, 2, 0, 1, Gtk::EXPAND | Gtk::SHRINK | Gtk::FILL, Gtk::FILL);
// 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.
Gtk::VRuler *vruler = new Gtk::VRuler;
vruler->set_metric(Gtk::PIXELS);
vruler->set_range(0, 400, 10, 400);
area->sig_motion_notify_event().connect(vruler, vruler->gtk_widget_class()->motion_notify_event);
table->attach(*vruler, 0, 1, 1, 2, Gtk::FILL, Gtk::EXPAND | Gtk::SHRINK | Gtk::FILL);
// Now show everything
show_all();
}
RulerWindow::~RulerWindow()
{
}
int main (int argc, char *argv[])
{
using namespace Main;
init(&argc, &argv);
RulerWindow window;
window.sig_destroy().connect(slot(&Inti::Main::quit));
run();
return 0;
}`

« OptionMenu | Index Miscellaneous Widgets Top | Statusbars »