|
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 this Part 3, I'll show you how you can delete an image with right mouse click as shown below:

Solution
Important Note: This only works for PHP-GTK2 compliled with gtk+ v2.10 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.10. For windows, please refer to How to install php gtk2 on windows? You may also want to take a look here to see some of the new exciting PHP-GTK2 Functionalities.
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
| <?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 3 - delete image"); $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); $this->img[$k] = $img;
// add image to eventbox
$eventbox = new GtkEventBox(); // note 1
$eventbox->add($img); // note 2
$eventbox->connect('button-press-event', // note 3
array(&$this, 'on_button_press_img'), $k); // note 3
// 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); $this->img[$id]->set_from_file($img_file); $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 2.
What's new here:
- Create a new eventbox.
- Stuff the image in the eventbox.
- Register for the 'button-press-event' so that you can detect the right mouse click.
- Check for right mouse click.
- The menu definition for the popup menu.
- popup the menu!
- Clear the image.
- Echo the URI of the selected image in the command window.
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. |