Thanks for your help kksou,
I modify your script :
- merge the two classes
- add a timeout
- add aparameter column
- As I had problem with class destructor, I used hide/show
- move all signal connexion into the class
$search_app = new Interactive_Search($treeview);
class Interactive_Search extends GtkWindow {
protected $treeview; // the tree view
protected $search_entry;
protected $timeout_handler_delay = 3000; // autoclose the search window
function __construct($treeview, $col = 0 ) {
parent::__construct();
$this->treeview = $treeview;
$this->add($vbox = new GtkVBox());
$this->set_modal(0);
$this->set_decorated(0);
// I don't know if this is necessary
// $this->set_parent_window($this->treeview->get_toplevel()->window);
$this->set_transient_for($this->treeview->get_toplevel());
$this->search_entry = new GtkEntry();
$vbox->pack_start($frame = new GtkFrame(), 0);
$frame->add($this->search_entry);
$this->treeview->set_enable_search(false);
$this->treeview->connect_simple('start-interactive-search',
array($this, 'start_interactive_search'));
$this->treeview->connect('key-press-event', array($this,
'start_interactive_search'));
$this->search_entry->connect('key-press-event', array($this,
'on_entry_keypress'));
$this->search_entry->connect('focus-out-event', array($this,
'stop_interactive_search'));
$this->treeview->set_search_entry($this->search_entry);
$this->treeview->set_search_column($col);
}
public function start_interactive_search($treeview = null, $event = null){
$this->show_all();
// if start from treeview key_pressed
if($event->string!=""){
$this->search_entry->set_text($event->string);
$this->search_entry->set_position(-1);
}
// Position of the search window
$win_pos = $this->treeview->get_toplevel()->get_position();
$x = $win_pos[0] + $this->treeview->allocation->width -
$this->search_entry->allocation->width + $this->x_shift;
$y = $win_pos[1] + $this->treeview->allocation->height +
$this->treeview->allocation->y +
$this->search_entry->allocation->height + $this->y_shift ;
$this->move($x, $y);
// Timeout signal connexion
if($this->timeout_handler_id){
Gtk::timeout_remove($this->timeout_handler_id);
}
$this->timeout_handler_id =
Gtk::timeout_add($this->timeout_handler_delay, array($this,
'stop_interactive_search'));
}
public function stop_interactive_search(){
Gtk::timeout_remove($this->timeout_handler_id);
$this->hide();
return false;
}
public function on_entry_keypress($entry, $event) {
if ($event->keyval==Gdk::KEY_Return ||
$event->keyval==Gdk::KEY_Escape) {
$this->stop_interactive_search();
} else {
// Refresh timeout
Gtk::timeout_remove($this->timeout_handler_id);
$this->timeout_handler_id =
Gtk::timeout_add($this->timeout_handler_delay, array($this,
'stop_interactive_search'));
}
return false;
}
}
kksou a écrit :