PHP-GTK2 Newsletter
PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou
Sample Code 515: How to keep track of the image filenames of 12 drag and drop images - Part 1? |
|
Written by kksou
|
|
Thursday, 24 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 this Part 1, I will show you how to keep track of the filenames of the 12 images as shown below:

Solution
- We make use of the technique as described in How to create a simple drag and drop image viewer? to set up the drag and drop of images.
- To avoid too many global variables, I used classes in this example.
- The filenames of the image are stored in the instance variable $img_filename.
- You can view the entire list of the URIs of the respective images by clicking the "Process Images" button.
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
| <?php $app = new App; $app->go();
class App {
var $num_rows = 4; // note 1
var $num_cols = 3; // note 1
var $img_width = 100; // note 2
var $img_height = 100; // note 2
var $img = array(); // note 3
var $img_filename = array(); // note 4
public function __construct() {
$window = new GtkWindow(); $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 1 - drag and drop one image at a time"); $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, // note 5
array( array( 'text/uri-list', 0, 0)), Gdk::ACTION_COPY); $img->connect('drag-data-received', array(&$this, 'on_drop'), $k); $this->img[$k] = $img; // note 6
$frame = new GtkFrame(); $frame->add($img); $hbox->pack_start($frame, 0); $this->add_margin($hbox, 10); } }
|
- 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
- Defines the number of rows and columns of images to be displayed. Here I used 4 rows by 3 columns.
- Defines the width and height of each image. Here I used 100 x 100 pixels for each image.
- Stores the pointer to the GtkImage for each image.
- Stores the URI for each image.
- Set up drag and drop for each image.
- Make a copy of the pointer to the GtkImage.
- Display the dropped image.
- Make a copy of the URI of the image.
- Output the entire list of the URIs of the respective images to the command window when the user clicks the "Process Images" button.
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. |
|