PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 119: How to have a Screen Ruler - Part 3 - switch between horizontal and vertical ruler?
Written by kksou   
Friday, 15 December 2006
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.

How to have a Screen Ruler - Part 3 - switch between horizontal and vertical ruler?


Solution

This example makes use of the technique as described in the article How to run multiple applications in multiple windows - Part 1?


Sample Code
1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
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   
44   
45   
46   
47   
48   
<?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
}

  • Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
  • Registration is free and immediate.
  • Have some doubt about the registration? Please read this forum article.
Explanation

This example makes use of the code in How to run multiple applications in multiple windows - Part 1?.

What's new here:

  1. Set up the horizontal ruler in a new GtkDialog.
  2. Set up the vertical ruler in another GtkDialog.
  3. 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.
  4. This empty GtkLabel helps to "push" the previous two labels up so that they stay at the top of the vertical ruler.
  5. When user press F12, alternate between hruler and vruler, hide the active ruler, and show the other one.
  6. Display the mouse position.

Related Links

User reviews

There are no user reviews yet.

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2008. kksou.com. All Rights Reserved