Intro
this script is based on example in the PHP-GTK 2 manual, which was left unfinished. So here is a working example with editable columns.
With editable cells, you can display cells, and edit cell content
in the same place. This is a very convenient way to manage list of items
without having to popup some boring editor window. To edit text cells,
you just need to click a second time on an item when it has been selected.
Details
To make cells editable, you just need to set
GtkCellRendererText object as editable :
<?php
$model = new GtkListStore(Gtk::TYPE_STRING, Gtk::TYPE_BOOLEAN);
...
$cell_renderer = new GtkCellRendererText();
$cell_renderer->set_property('editable', true);
?>
then connect a signal callback function to the "edited" signal;
this callback will update the data model
<?php
$cell_renderer->connect('edited', 'callback_text_cell_edited');
function callback_text_cell_edited($cellrenderertext, $path, $new_text){
global $model;
$iter = $model->get_iter($path);
$model->set($iter, 0, $new_text);
}
?>
For toggle renderers, connect "toggled" signal and
swap the toggle state.
Source code
<?php
/**
* This sample shows how to use the
* GtkCellRenderer along with GtkTreeView
*/
// Creates the main window
$window = new GtkWindow;
$window->set_title('Cell Renderers');
$window->connect_simple('destroy', array('Gtk', 'main_quit'));
$window->set_position(GTK::WIN_POS_CENTER);
$window->set_default_size(280,140);
// Creates the data model
$model = new GtkListStore(Gtk::TYPE_STRING, Gtk::TYPE_BOOLEAN);
// Creates the view to display the content
$view = new GtkTreeView($model);
// Creates two columns
$column1 = new GtkTreeViewColumn('Language');
$column2 = new GtkTreeViewColumn('Open Source?');
// Add the columns to the view
$view->append_column($column1);
$view->append_column($column2);
// Creates two cell-renderers
$cell_renderer1 = new GtkCellRendererText();
$cell_renderer2 = new GtkCellRendererToggle();
// change the property 'width'
$cell_renderer1->set_property('width', 180);
$cell_renderer1->set_property('editable', true);
$cell_renderer2->set_property('width', -1);
$cell_renderer1->connect('edited', 'callback_text_cell_edited');
$cell_renderer2->connect('toggled', 'callback_toggle_cell_toggled');
// Pack the cell-renderers
$column1->pack_start($cell_renderer1, true);
$column2->pack_start($cell_renderer2, true);
// link the renderers to the model
$column1->set_attributes($cell_renderer1, 'text', 0);
$column2->set_attributes($cell_renderer2, 'active', 1);
// Add some data
$model->append(array('PHP', true));
$model->append(array('Python', true));
$model->append(array('Delphi', false));
$model->append(array('Visual Basic', false));
// pack the view inside the window
$window->add($view);
// show the window
$window->show_all();
Gtk::main();
function callback_text_cell_edited($cellrenderertext, $path, $new_text) {
global $model;
$iter = $model->get_iter($path);
$model->set($iter, 0, $new_text);
}
function callback_toggle_cell_toggled($cellrenderer, $path){
global $model;
$value = $cellrenderer->get_active();
$iter = $model->get_iter($path);
$model->set($iter, 1, !$value);
}
?>
Comments
Some additional questions
Hello Marc,
first of all thank you for this "demo". It is exactly what I've been looking for in my application. Unfortunately there is one line which I don't fully understand:
$column1->set_attributes($cell_renderer1, 'text', 0);What does this mean? Which attributes can I use?
Also I have one additional question: Is it possible to add a button to each line, which would delete it? Or at least a global button, which would delete the currently selected line. Thank you.
Best Regards,
Vladimir