The GtkEntry widget is more pleasant to use
when it is fitted with auto-completion help using the
GtkEntryCompletion widget. This example show how
to use it.
This feature is used by Mozilla for example when you
type an URL in the adress bar.
this is a standard widget creation script.
PhpGtkEntryCompletionDirectory
which will read the current directory ('.')GtkEntry widget<?php
/*
widget tree :
window (GtkWindow) | Gtk::main_quit()
vbox (GtkVbox)
title (GtkLabel) "GtkEntryCompletion test"
hbox (GtkHbox)
label (GtkLabel) : "Select:"
entry (PhpGtkEntryCompletion)
label (GtkLabel) : " "
button (GtkButton) : "Submit" | on_button()
*/
$window = new GtkWindow();
$vbox = new GtkVBox();
$window->add($vbox);
// display title
$title = new GtkLabel("GtkEntryCompletion test");
$title->set_size_request(-1, 60);
$vbox->pack_start($title, 0, 0);
$vbox->pack_start($hbox = new GtkHBox(), 0, 0);
$hbox->pack_start(new GtkLabel('Select: '), 0, 0);
# note 1 - entry completion on file system
$entry = new PhpGtkEntryCompletionDirectory($_dir='.');
// note 2 - entry completion test from a list
# $list = array('red', 'blue', 'yellow', 'green', 'black', 'cyan');
# $entry = new PhpGtkEntryCompletion($list);
// Set up a hbox to contain both the combobox and a Submit button
$hbox->pack_start($entry, 0, 0);
# Label
$hbox->pack_start(new GtkLabel(' '), 0, 0);
$hbox->pack_start($button = new GtkButton('Submit'), 0, 0);
$button->set_size_request(60, 24);
// Set up the event handler to respond to button click
$button->connect('clicked', "on_button", $entry);
$window->connect_simple('destroy', array( 'Gtk', 'main_quit'));
$window->set_size_request(400,150);
// The callback function that is called when user clicks the submit button
function on_button($button, $entry) {
$txt = $entry->get_text();
echo "you have selected : $txt\n";
}
$window->show_all();
Gtk::main();
?>There are 2 user-defined classes involved in these examples,
in addition to GtkEntryCompletion and the
underlying GtkEntry:
<?php
class PhpGtkEntryCompletion extends GtkEntry {
protected $_list;
protected $_model;
protected $_completion;
function __construct($list = null) {
parent::__construct();
$this->_list = $list;
$this->create();
if ($this->_list !== null)
$this->completion_add_words($this->_list);
}
protected function create() {
// Create a model
$this->_model = new GtkListStore(Gtk::TYPE_STRING);
// create completion entry
$this->_completion = new GtkentryCompletion();
$this->_completion->connect('match-selected',
array($this, 'completion_match_select'));
$this->_completion->set_model($this->_model);
$this->_completion->set_text_column(0);
$this->set_completion($this->_completion);
}
function completion_add_word($word) {
$this->_model->append(array($word));
}
function completion_add_words($words) {
foreach($words as $word)
$this->_model->append(array($word));
}
function completion_clear() {
$this->_model->clear();
}
protected function completion_match($completion, $key, $iter) {
$txt = $model->get_value($iter, 0);
if (strpos($key, $txt, 0) === false)
return false;
return true;
}
public function completion_match_select(
$completion, $model, $iter) {
$txt = $model->get_value($iter, 0);
$this->set_text($txt);
$this->set_position(-1);
}
}
class PhpGtkEntryCompletionDirectory extends
PhpGtkEntryCompletion {
protected $_dir;
function __construct($dir) {
parent::__construct($list=null);
$this->_dir = $dir;
$files = $this->read_dir($this->_dir);
$this->completion_add_words($files);
}
public function read_dir($dir, $make_full_path=false, $sort=true) {
if (!is_dir($dir))
return array();
$d = opendir($dir);
if (!$d)
return array();
$list = array();
while (($file = readdir($d)) !== false) {
if ($file != '.' && $file != '..') {
if ($make_full_path) {
$fullpath = $dir . '/' . $file;
$list[] = $file;
} else
$list[] = $file;
}
}
if ($sort)
$this->sort($list);
return $list;
}
function sort(&$array) {
natcasesort($array);
}
}
?>| Attachment | Size |
|---|---|
| entry-completion.php_.txt | 3.66 KB |
Recent comments
47 weeks 2 days ago
48 weeks 1 day ago
48 weeks 5 days ago
51 weeks 1 day ago
1 year 19 weeks ago
1 year 23 weeks ago
1 year 27 weeks ago
1 year 27 weeks ago
1 year 27 weeks ago
1 year 44 weeks ago