Basic Entry Dropdown with Auto-Complete
Building a quick entry with auto-complete and dropdown complete on the entry history.
Topic: GtkEntryCompletion (View All Tutorials)
Keywords: gtkentry gtkcomboboxentry gtkentrycompletion autocomplete models new_text
Updated: 312 Days Ago, 2008/01/25 04:08
There are two basic ways to do auto-complete on a text entry field. The first way is to use a GtkComboBoxEntry widget which adds drop down list functionality to the Entry so that the user can choose from a list.
The second, GTK has the ability to create a quick auto-complete setup for basic GtkEntry widgets. In short, it involves creating a GtkEntryCompletion object and then assigning it to the GtkEntry you want to have it work on. This method allows you to do live auto-complete fairly easily.

Nice thing about this is that both of these methods are driven by the same backend, a GtkListStore, so manipulating the lists is a breeze. In fact it is so easy, this code demonstration will use the same GtkListStore for both the drop down and the auto-complete this way both of them contain the same data. When you type into the text entry, that text will be added to the history so that it can auto-complete that word later on. However after a while the list would get pretty long and fill the screen, so we will keep it trimmed to the ten most recent.
GtkEntryCompletion Demo
#!/usr/bin/php -c/etc/gtk/php.ini
<?php
class bobWindow extends GtkWindow {
public $vbox,$hbox;
public $label;
public $combo,$entry;
public $list;
public $auto;
private $history = array();
public function __construct() {
parent::__construct();
$this->vbox = new GtkVBox;
$this->hbox = new GtkHBox;
$this->label = new GtkLabel('Entry Box: ');
// note 1
$this->combo = GtkComboBoxEntry::new_text();
$this->entry = $this->combo->child;
$this->auto = new GtkEntryCompletion;
// note 2
$this->list = $this->combo->get_model();
$this->auto->set_model($this->list);
$this->auto->set_text_column(0);
$this->entry->set_completion($this->auto);
$this->set_title('GtkEntryCompletion Demo');
$this->set_position(Gtk::WIN_POS_CENTER);
$this->set_size_request(300,75);
$this->connect_simple('delete-event',array($this,'on_quit'));
$this->entry->connect_simple('activate',array($this,'on_text_entry'));
//. just seeding the history with data for demonstration.
foreach(array('bob','orange','php-gtk','php','mistress') as $word) {
$this->on_text_entry($word);
}
$this->hbox->pack_start($this->label,false,false,3);
$this->hbox->pack_start($this->combo,true,true,3);
$this->vbox->pack_start($this->hbox,true,false,3);
$this->add($this->vbox);
$this->show_all();
return;
}
public function on_text_entry($text = null) {
if(!$text) {
$text = $this->entry->get_text();
$this->entry->set_text('');
}
// note 3
if(($key = array_search($text,$this->history)) !== false) {
unset($this->history[$key]);
}
// note 4
array_unshift($this->history,$text);
if(count($this->history) > 10) {
array_pop($this->history);
}
// note 5
$this->list->clear();
foreach($this->history as $history) {
$this->list->append(array($history));
}
return;
}
public function on_quit() {
$this->hide();
Gtk::main_quit();
return;
}
}
$window = new bobWindow;
Gtk::main();
$window->destroy();
unset($window);
?>
Note 1
I've created a GtkComboBoxEntry by using the static new_text() method instead of constructing a new instance. This builds a complete drop down text entry widget, all we have to do is fetch the GtkListStore out of it. The actual text entry widget is a subwidget of the drop down menu called child, and I've linked it to make it easier to reference.
Note 2
Here I take the default GtkListStore from the GtkComboBoxEntry, and pass it to the auto-complete object. Then the completion object must be told which column of the list to use. The column we want is the first (and only). Now both objects are reading from the same list of data.
Note 3
Here we check to make sure that what was entered into the entry is not already in the history. If it is, we delete it.
Note 4
Then the new text is literally shoved onto the front of the array. After adding this text if our array exceeds 10 items, pop the last one off so it only has 10 again.
Note 5
The list is then cleared and rebuilt from the updated history array. Now both the drop down and the auto-complete will be using the most recent history data.
End Game