Sample Code 98: How to create a simple drag and drop image viewer? |
|
Written by kksou
|
|
Monday, 20 November 2006 |
|
Problem You want to create a simple image viewer that allows users to drag a gif/jpg/png image file from a file explorer and drop it into the image viewer as shown below:

Solution
Sample Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <?php $window = new GtkWindow(); $window->set_size_request(400, 300); $window->connect_simple('destroy', array('Gtk','main_quit'));
$img = new GtkImage(); $img->drag_dest_set(Gtk::DEST_DEFAULT_ALL, array( array( 'text/uri-list', 0, 0)), Gdk::ACTION_COPY); // note 1
$img->connect('drag-data-received', 'on_drop', $img); // note 1
$scrolled_win = new GtkScrolledWindow(); // note 2
$scrolled_win->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); $window->add($scrolled_win); $scrolled_win->add_with_viewport($img); // note 3
$window->show_all(); Gtk::main();
// process drop
|
- 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
- Set up the GtkImage so that it allows users to drop an image onto it.
- Create a scroll window.
- Stuff the img in the scroll window. Note the use of GtkScrolledwindow::add_with_viewport().
- Display the image using the filename "dropped" onto the image viwer.
Related Links
User reviews Average user ratings: 4.0 (from 2 users) Note: You have to be a registered member to leave a comment. Free registration here. |
February 27, 2008 2:57am
June 15, 2008 4:54pm