icon_folder_open = 'folder_open.gif'; $this->icon_folder_closed = 'folder_closed.gif'; $this->trace = false; $this->build(); $this->add_signals(); } function build(){ $this->model = new GtkTreeStore(Gtk::TYPE_STRING, Gtk::TYPE_STRING); # store file, path $this->scrolled_win = new GtkScrolledWindow(); $this->scrolled_win->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); $this->add($this->scrolled_win); // set up treeview $this->treeview = new GtkTreeView($this->model); $this->scrolled_win->add($this->treeview); //set up treeview columns $this->column = new GtkTreeViewColumn(); // for image $cell_renderer = new GtkCellRendererPixbuf(); $this->column->pack_start($cell_renderer, false); if(file_exists($this->icon_folder_open) && file_exists($this->icon_folder_open)){ $cell_renderer->set_property('pixbuf-expander-open', GdkPixbuf::new_from_file($this->icon_folder_open)); $cell_renderer->set_property('pixbuf-expander-closed', GdkPixbuf::new_from_file($this->icon_folder_closed)); } else trigger_error("PhpGtkDirectoryTree : icon file for folder not found", E_USER_WARNING); // for filename $cell_renderer = new GtkCellRendererText(); $this->column->pack_start($cell_renderer, true); $this->column->set_attributes($cell_renderer, 'text', 0); $this->treeview->append_column($this->column); } function add_signals(){ $signals = array( 'row-collapsed', 'row-expanded', 'move-cursor', 'row-activated', 'select-cursor-parent', 'select-all', 'select-cursor-row', 'set-scroll-adjustments', 'start-interactive-search', 'test-collapse-row', 'test-expand-row', 'toggle-cursor-row', 'unselect-all', 'columns-changed' ); foreach($signals as $sig) $this->treeview->connect_simple($sig, array($this,'treeview_signals'), $sig); # connect signal 'changed' to GtkTreeSelection object $this->treeview->get_selection()->connect_simple('changed', array($this,'selection_changed')); } # http://www.pygtk.org/pygtk2reference/class-gtktreeview.html function treeview_signals($userdata){ $signal_type = $userdata; echo "treeview_signals ($userdata)\n"; switch($signal_type){ case 'row-activated': $selection = $this->treeview->get_selection(); # GTkTreeSelection if(is_object($selection)) echo "selection class : " . get_class($selection) . "\n"; break; } } function selection_changed(){ echo "selection changed\n"; # list($tree_store, $path) = $this->treeview->get_selection()->get_selected_rows(); # $this->model->foreach(array($this, 'selection_changed_iter')); $this->treeview->get_selection()->selected_foreach(array($this, 'selection_foreach')); } function selection_foreach($model, $path, $iter){ $file = $model->get_value($iter, 0); $path = $model->get_value($iter, 1); echo "selection_foreach() : $file - $path\n"; } function open_directory($folder){ if(!is_dir($folder)){ trigger_error("PhpGtkDirectoryTree : directory not found", E_USER_WARNING); return false; } $this->model->clear(); $root = $folder; $dir_list = array($root); $nodes = array(); $nodes[$root] = null; while(count($dir_list)>0) { $dir = array_shift($dir_list); $this->trace("folder = $dir\n"); // add the directories first note 4 if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $fullpath = $dir.'/'.$file; if (is_dir($fullpath)) { $nodes[$fullpath] = $this->model->append($nodes[$dir], array($file, $dir)); array_push($dir_list, $fullpath); } } } closedir($handle); } $num_files = 0; // then add the files note 5 if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $fullpath = $dir.'/'.$file; if (!is_dir($fullpath)) { $nodes[$fullpath] = $this->model->append($nodes[$dir], array($file, $dir)); ++$num_files; } } } closedir($handle); } if ($num_files==0) $nodes[$fullpath] = $this->model->append($nodes[$dir], array('', '')); // note 6 } } function set_title($title){ $this->title = $title; $this->column->set_title($title); } function trace($txt){ if($this->trace) echo "$txt"; } function debug($txt, $obj){ if(is_object($obj)) echo "$txt : " . get_class($obj) . "\n"; else{ echo "$txt : " ; print_r($obj); } } } ?>