This FileSelectionDialog provides a wrapper
around the standard GtkFileSelection class
in PHP-GTK2, to make it easier to use.
Note : you can check a
simpler introductory example
to
GtkFileSelection before delving into this class.
Main methods :
You can see how it easy to get a file ; all you need to do is
to create a FileSelectionDialog instance and call
the get_selection() method.
<?php
$fs = new FileSelectionDialog('test');
$fs->run();
$file = $fs->get_selection();
if ($file === FALSE)
echo "canceled\n";
else
echo "selected file $file\n";
?><?php
error_reporting(E_ALL);
/**
* library class (need to be extended)
* @author Marc Quinton / september 2006.
*/
define('FS_ACTIVATE', 0);
define('FS_CANCEL', 1);
class FileSelectionDialog {
private $fs; # file selection dialog widget
private $title;
private $status;
public function __construct($title) {
$this->title = $title;
$this->fs = new GtkFileSelection($title);
$this->status = null;
$this->fs->ok_button->connect_simple( 'clicked' ,
array($this, 'activate'));
$this->fs->cancel_button->connect_simple( 'clicked' ,
array($this, 'cancel'));
# need to catch double-click in list widget
}
public function show() {
$this->fs->show();
}
public function hide() {
$this->fs->hide();
}
function activate() {
$this->status = FS_ACTIVATE;
$this->hide();
Gtk::main_quit();
}
function cancel() {
$this->status = FS_CANCEL;
$this->hide();
Gtk::main_quit();
}
public function run() {
$this->show();
gtk::main();
}
public function get_selection() {
if ($this->status == FS_ACTIVATE)
return $this->fs->get_filename();
else
return false;
}
}
class MyFileSelectionDialog extends FileSelectionDialog {
function activate() {
parent::activate();
# do what you need here ...
echo "activate\n";
}
function cancel() {
parent::cancel();
echo "cancel\n";
}
}
/**
* test script
*/
function activate_fs_ok($fs) {
echo "fs_ok\n";
$fs->hide();
}
function activate_select() {
# to handle cancel and OK button ; not really needed
# $fs = new MyFileSelectionDialog('test');
$fs = new FileSelectionDialog('test');
$fs->run();
$file = $fs->get_selection();
if ($file === false)
echo "canceled\n";
else
echo "selected file $file\n";
}
// standard stuff for window creation
$wnd = new GtkWindow();
$wnd->connect_simple('destroy', array('Gtk', 'main_quit'));
$wnd->set_position(Gtk::WIN_POS_CENTER);
$wnd->set_size_request(100, 100);
$btn = new GtkButton('select');
$btn->connect_simple('clicked', 'activate_select');
$wnd->add($btn);
$wnd->show_all();
Gtk::main();
?>