Problem
You have set up a horizontal screen ruler in Part 2. Now you would like to be able to switch between horizontal ruler and vertical ruler at the press of a key (F12) as shown below. This example also shows how to have two windows running concurrently in a php-gtk application.
Solution
This example makes use of the technique as described in the article How to run multiple applications in multiple windows - Part 1?
- Create a horizontal ruler and a vertical ruler, each one residing in its own GtkDialog.
- Use GtkWidget::key-press-event() to know when the user presses F12.
- When the user presses F12, we hide the active ruler with $dialog->hide_all() and show the other ruler with $dialog->show_all().
Sample Code
1 2 4 5 6 7 8 11 12 13 14 15 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 45 46 47 48 49 50 58 59 60 61 66 67 68 69 70 71 72 73 74 75 76 77 81 82 83 84 86 87 88 89 | <?php $apps['hruler'] = setup_app('hruler'); // note 1 $apps['vruler'] = setup_app('vruler'); // note 2 $apps['hruler']->show_all(); $apps['hruler']->run(); // setup_app function setup_app($module) { $dialog = new GtkDialog($module, null, Gtk::DIALOG_MODAL|Gtk::DIALOG_NO_SEPARATOR); $dialog->set_position(Gtk::WIN_POS_CENTER_ALWAYS); $eventbox = new GtkEventBox(); if ($module=='hruler') { $ruler = new GtkHRuler(); // note 1 $ruler->set_range(0, 800, 100, 1024); $ruler->set_size_request(800, 20); $eventbox->add($ruler); } else { $ruler = new GtkVRuler(); // note 2 $ruler->set_range(0, 400, 100, 800); $ruler->set_size_request(20, 400); $hbox = new GtkHBox(); global $ypos; $ypos = new GtkLabel(); // note 3 $vbox = new GtkVBox(); $vbox->pack_start($ypos, 0, 0); $vbox->pack_start(new GtkLabel(), 0, 0); $vbox->pack_start(new GtkLabel("Press F12 to switch \nto horizontal ruler"), 0, 0); $vbox->pack_start(new GtkLabel()); // note 4 $hbox->pack_start($vbox); $hbox->pack_start($ruler, 0, 0); $eventbox->add($hbox); } $eventbox->connect('motion-notify-event', 'on_motion', $dialog, $module); $dialog->vbox->pack_end($eventbox, 0, 0); $dialog->connect('key-press-event', "on_key", $module); // catch F12 $vbox_contents = $dialog->vbox->get_children(); $hbuttonbox = $vbox_contents[1]; $class_name = $hbuttonbox->get_name(); $hbuttonbox->set_size_request(0,0); return $dialog; // returns the ID of the dialog } function on_motion($widget, $event, $dialog, $module) { if ($module=='hruler') { $dialog->set_title("php-gtk2 Screen Ruler. x=$event->x. (Press F12 to switch to vertical ruler)"); // note 6 } else { global $ypos; $ypos->set_text("y=$event->y"); // note 6 } return true; } function on_key($widget, $event, $module) {// note 5 global $apps; if ($event->keyval==Gdk::KEY_F12) { $module2 = ($module=='hruler') ? 'vruler' : 'hruler'; $apps[$module]->hide(); // hide the active one; $apps[$module2]->show_all(); // show the other one $apps[$module2]->run(); // let's go! } } ?> |
Output
As shown above.
Explanation
This example makes use of the code in How to run multiple applications in multiple windows - Part 1?.
What's new here:
- Set up the horizontal ruler in a new GtkDialog.
- Set up the vertical ruler in another GtkDialog.
- For the horizontal ruler, we display the x-pos of the mouse location in the dialog title. For the vertical ruler, there is not enough space in the title bar. So we need to create a GtkLabel to display the y-pos of the mouse.
- This empty GtkLabel helps to "push" the previous two labels up so that they stay at the top of the vertical ruler.
- When user press F12, alternate between hruler and vruler, hide the active ruler, and show the other one.
- Display the mouse position.
Read more...