Colapseable tree view example
| From: | karlbowden at gmail dot com | Date: | Wed, 28 Dec 2005 21:43:25 +0000 |
| Subject: | Colapseable tree view example | ||
| Groups: | php.gtk.doc | ||
| Request: | Send a blank email to php-gtk-doc+get-1747@lists.php.net to get a copy of this message | ||
Would a colapasable treeview example come in handy? It's executes,
buts going to need some tidying up though. I was also not writing to
anybodys standard, just how I felt like. The example was just for my
benefit, so I could see how the treeview worked, and thought somebody
else might find it handy too.
class treeview extends GtkWindow {
function __construct () {
parent::__construct ();
$this->set_title ("Treeview Example");
$this->set_default_size (500, 500);
$this->connect_simple ('destroy', array ('gtk',
'main_quit'));
$this->create_mainframe ();
$this->add ($this->mainframe);
$this->show_all ();
}
function create_mainframe () {
$this->mainframe = new GtkVBox (false, 3);
$this->mainframe->pack_start ($hb1 = new GtkHBox (2));
$this->mainframe->pack_start ($this->status = new GtkStatusBar
(), false, false);
$this->create_model ();
$this->create_view ();
$hb1->pack_start ($this->create_menu_list ());
$hb1->pack_start ($this->right = new GtkLabel ("Right Panel"));
}
function create_model () {
$this->model = new GtkTreeStore (Gtk::TYPE_PHP_VALUE, Gtk::TYPE_STRING);
$m = array ('Title 1' => 'link name',
'Submenu 1' => array ('Submenu item 1' =>
'itemlink',
'Submenu item 2' => 'itemlink'),
'Submenu 2' => array ('Anothermenu item' =>
'itemlink',
'Annaother one' => 'link'),
'Main Item again' => 'main');
foreach ($m as $t1 => $l1) {
if (is_array ($l1)) {
$item = array ('type' => 'submenu', 'title' =>
$t1);
$iter = $this->model->append ();
$this->model->set ($iter, 0, $item);
foreach ($l1 as $t2 => $l2) {
if (is_string ($l2)) {
$item2 = array ('type' => 'menuitem',
'title'
=> $t2, 'link' => $l2);
$iter2 = $this->model->append ($iter);
$this->model->set ($iter2, 0, $item2);
}
}
}
elseif (is_string ($l1)) {
$item = array ('type' => 'menuitem', 'title' =>
$t1,
'link' => $l1);
$iter = $this->model->append ();
$this->model->set ($iter, 0, $item);
}
}
return ($this->model);
}
function create_view () {
$this->tv = new GtkTreeView ($this->model);
$this->tv->set_rules_hint (true);
$col1 = new GtkTreeViewColumn ();
$cell_text = new GtkCellRendererText ();
$col1->set_title ("Treeview menu");
$col1->pack_start ($cell_text, true);
$col1->set_cell_data_func ($cell_text, array ($this, 'view_rend_text'));
$this->tv->append_column ($col1);
$this->tv->set_expander_column ($col1);
$selection = $this->tv->get_selection();
$selection->connect ('changed', array ($this, 'tv_selected'));
return $this->tv;
}
function create_menu_list () {
$scrolled = new GtkScrolledWindow ();
$scrolled->set_shadow_type (Gtk::SHADOW_ETCHED_IN);
$scrolled->set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
$scrolled->add ($this->tv);
return $scrolled;
}
function view_rend_text ($column, $cell, $model, $iter)
{
$item = $model->get_value ($iter, 0);
$cell->set_property ('text', $item['title']);
}
function tv_selected ($selection)
{
list ($model, $iter) = $selection->get_selected ();
if (!$iter) {
return;
}
$item = $model->get_value ($iter, 0);
$this->right->set_text ("Selected $item[title] - $item[link]\n");
}
}
$tree = new treeview ();
Gtk::main ();