118. How to have a Screen Ruler - Part 2 - with mouse position displayed?

Problem

You have set up a basic screen ruler in Part 1. Now you would like to have the exact mouse position displayed in the title bar as shown below:

How to have a Screen Ruler - Part 2 - with mouse position displayed?


Solution


Sample Code

1   
2   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
19   
20   
21   
22   
23   
28   
29   
30   
31   
32   
<?php
$window = new GtkWindow();
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

$ruler = new GtkHRuler();
$ruler->set_range(0, 800, 100, 1024);
$ruler->set_size_request(800, 20);

$eventbox = new GtkEventBox(); // note 1
$eventbox->add($ruler);
$eventbox->connect('motion-notify-event', 'on_motion', $window); // note 2

$vbox->pack_start($eventbox, 0, 0);

$window->show_all();
Gtk::main();

function on_motion($widget, $event, $window) {
    $window->set_title("php-gtk2 Screen Ruler. x=$event->x");  // note 3
    return true;
}

?>

Output

As shown above.

 

Explanation

This example makes use of the code in Part 1.

What's new here:

  1. Create an eventbox and stuff the ruler inside.
  2. Call the callback function on_motion every time the user moves the mouse.
  3. Write the mouse position in the window's title bar.

Related Links

Add comment


Security code
Refresh