PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 519: How to keep track of the image filenames of 12 drag and drop images - Part 5 - display tooltip?
Written by kksou   
Friday, 25 July 2008
Problem

This is in response to Mote's post titled "2 questions about GtkImage".

He has set up an application that allows drag-and-drop of 12 different images. And he would like to be able to retrieve the URI (or filenames) of these 12 images at some later time for processing.

In Part 1, you can only drag and drop one image at a time.

In Part 2, you can drag and drop multiple images at one go.

In Part 3, you can delete unwanted images with right mouse click.

In Part 4, images will be automatically scaled to 100 by 100 pixels if the image is larger than 100 by 100 pixels.

In this Part 5, I'll show you how to display the image's URI in tooltip when the user hovers its mouse over an image as shown below:

How to keep track of the image filenames of 12 drag and drop images - Part 5 - display tooltip?


Solution

Important Note: This only works for PHP-GTK v2.0 (or PHP-GTK2 compliled with gtk+ v2.12 and above. If you are using an older version, for linux, you may follow the step-by-step instructions to recompile php-gtk2 with gtk+ v2.12. For windows, please refer to How to install php gtk2 on windows?


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   
49   
50   
51   
52   
53   
54   
55   
56   
57   
58   
59   
60   
61   
62   
63   
64   
65   
66   
67   
68   
69   
70   
71   
72   
73   
74   
75   
76   
77   
78   
79   
80   
81   
82   
83   
84   
85   
86   
87   
88   
89   
90   
91   
92   
93   
94   
95   
96   
97   
98   
99   
100   
101   
102   
103   
104   
105   
106   
107   
108   
109   
110   
111   
112   
113   
114   
115   
116   
117   
118   
119   
120   
121   
122   
123   
124   
125   
126   
127   
128   
129   
130   
131   
132   
133   
<?php
$app = new App;
$app->go();

class App {

    var $num_rows = 4;
    var $num_cols = 3;
    var $img_width = 100;
    var $img_height = 100;
    var $img = array();
    var $img_filename = array();

    public function __construct() {

        $window = new GtkWindow();
        global $argv;
        $window->set_title($argv[0]);
        $window->set_size_request(400, 400);
        $window->connect_simple('destroy', array('Gtk','main_quit'));
        $window->add($vbox = new GtkVBox());

        // display title
        $title = new GtkLabel("      Drag and drog your images\n".
        "into any of the 12 image positions\n".
        "          Part 5 - display tooltips");
        $title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
        $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
        $title->set_size_request(-1, 60);
        $vbox->pack_start($title, 0);
        $vbox->pack_start(new GtkLabel(), 0);

        // add a process button
        $hbox = new GtkHBox();
        $vbox->pack_start($hbox, 0);
        $process_button = new GtkButton('Process Images');
        $process_button->connect('clicked', array(&$this, 'on_process_button'));
        $hbox->pack_start($process_button, 0);

        // init the filenames
        for($i=0; $i<$this->num_rows*$this->num_cols; ++$i) {
            $this->img_filename[$i] = '';
        }

        // set up the images
        $scrolled_win = new GtkScrolledWindow();
        $scrolled_win->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
        $vbox->pack_start($scrolled_win);
        $img_vbox = new GtkVBox();
        $scrolled_win->add_with_viewport($img_vbox);

        for ($row=0; $row<$this->num_rows; ++$row) {
            $hbox = new GtkHBox();
            $img_vbox->pack_start($hbox, 0);
            for ($col=0; $col<$this->num_cols; ++$col) {
                $k = $row*$this->num_cols + $col;
                $img = new GtkImage();
                $img->set_size_request($this->img_width, $this->img_height);
                $img->drag_dest_set(Gtk::DEST_DEFAULT_ALL,
                    array( array( 'text/uri-list', 0, 0)), Gdk::ACTION_COPY);
                $img->connect('drag-data-received', array(&$this, 'on_drop'), $k);

                $img->set_property('has-tooltip', true); // note 1
                $img->connect('query-tooltip', array(&$this, 'on_tooltip'), $k); // note 2

                $this->img[$k] = $img;

                // add image to eventbox
                $eventbox = new GtkEventBox();
                $eventbox->add($img);
                $eventbox->connect('button-press-event',
                    array(&$this, 'on_button_press_img'), $k);

                // add eventbox to frame
                $frame = new GtkFrame();
                $frame->add($eventbox);

                // add frame to the hbox of image_vbox
                $hbox->pack_start($frame, 0);
                $this->add_margin($hbox, 10);
            }
        }

        $window->show_all();
    }

    public function go() {
        Gtk::main();
    }

        protected function add_margin($hbox, $n) {
        $space = new GtkHBox();
        $space->set_size_request($n, -1);
        $hbox->pack_start($space, 0);
    }

    public function on_drop($widget, $context, $x, $y, $data, $info, $time, $id) {
        $uri_list = explode("\n",$data->data);
        for ($i=0; $i<count($uri_list); ++$i) {
            $img_file = $uri_list[$i];
            if ($img_file!='') {
                $img_file = str_replace("file:///", "", $img_file);
                $img_file = str_replace("\r", "", $img_file);

                $pixbuf=GdkPixbuf::new_from_file($img_file);
                $width = $pixbuf->get_width();
                $height = $pixbuf->get_height();

                if ($width>$height && $width>$this->img_width) {
                    $scale_factor = $this->img_width/$width;
                    $require_scaling = 1;
                } elseif ($height>$width && $height>$this->img_height) {
                    $scale_factor = $this->img_height/$height;
                    $require_scaling = 1;
                } else {
                    $require_scaling = 0;
                }

                if ($require_scaling) {
                    $new_width = $width * $scale_factor;
                    $new_height = $height * $scale_factor;
                    $new_pixbuf = $pixbuf->scale_simple($new_width, $new_height,
                        Gdk::INTERP_HYPER);
                    $this->img[$id]->set_from_pixbuf($new_pixbuf);
                } else {
                    $this->img[$id]->set_from_pixbuf($pixbuf);
                }


                $this->img_filename[$id] = $img_file;
                ++$id;
                if ($id>=$this->num_rows*$this->num_cols) break;
            }
  • 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

We make use of the code from Part 4.

What's new here:

  1. Enable tooltip on the widget.
  2. Connect to the signal 'query-tooltip'.
  3. Display the image's URI in the tooltip.

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