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:
Solution
- Create a basic horizontal ruler as explained in Part 1.
- As we need to know the mouse position, create a GtkEventBox and stuff the ruler inside.
- Use GtkWidget::motion-notify-event() to know when the user moves the mouse.
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:
- Create an eventbox and stuff the ruler inside.
- Call the callback function
on_motion
every time the user moves the mouse. - Write the mouse position in the window's title bar.
Read more...