GTKCellRenderer problem with editing activated
| From: | Sven Siwek | Date: | Thu, 28 Dec 2006 17:57:48 +0000 |
| Subject: | GTKCellRenderer problem with editing activated | ||
| Groups: | php.gtk.general | ||
| Request: | Send a blank email to php-gtk-general+get-7539@lists.php.net to get a copy of this message | ||
Hi List,
I have some trouble with GTKCellRenderers with attribute editable.
I borrowed some code from kksou.com Website ( I hope that is allright ) to make a little example.
I only added the function CellRenderer_Edited and the needed coding to activate the edit-modus.
My itention behind all this is, that the cursor is positioned in the next line when editing(and validation is done).
But I do get very funny results on the screen ... but see for yourself.
First select a cell and start editing(press space) then move your mouse to an other cell and click to activate an other cell.
Then move arround with your cursor keys - you may see cell in invers or nothing in the cell. But when activating it shows a value.
Just play arround and use mouse to activate an other cell when in edting modus....
Maybe this is a bug - I am unsure about this.
regards
Sven
<?php
$window = new GtkWindow();
$window->set_size_request(400, 200);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());
// display title
$title = new GtkLabel("Display 2D Array in GtkTreeView - Part 5\n
get user selection");$title->modify_font(new PangoFontDescription("Times New Roman Italic 10")); $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff")); $title->set_size_request(-1, 40); $vbox->pack_start($title, 0, 0); $vbox->pack_start(new GtkLabel(), 0, 0); // add a small gap between the title and scroll_win // Set up a scroll window $scrolled_win = new GtkScrolledWindow(); $scrolled_win->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); $vbox->pack_start($scrolled_win); // the 2D table $data = array( array('row0', 'item 19', 2, 3.1), array('row1', 'item 16', 20, 6.21), array('row2', 'item 13', 8, 9.36), array('row3', 'item 10', 11, 12.4), array('row4', 'item 7', 5, 15.5), array('row5', 'item 4', 17, 18.6), array('row6', 'item 3', 20, 21.73)); display_table ($scrolled_win, $data); function display_table($scrolled_win, $data) {
// Creates the list store
$model = new GtkListStore(Gtk::TYPE_STRING, Gtk::TYPE_STRING,
Gtk::TYPE_LONG, Gtk::TYPE_DOUBLE);
$field_header = array('Row #', 'Description', 'Qty', 'Price');
$field_justification = array(0, 0, 0.5, 1);
// Creates the view to display the list store
$view = new GtkTreeView($model);
$scrolled_win->add($view);
// Creates the columns
for ($col=0; $col<count($field_header); ++$col) {
$cell_renderer = new GtkCellRendererText();
$cell_renderer->set_property("xalign", $field_justification[$col]);
$cell_renderer->connect_after("edited", "CellRenderer_Edited", $view, $col);
$cell_renderer->set_property('editable', true);
$column = new GtkTreeViewColumn($field_header[$col], $cell_renderer, 'text', $col);
$column->set_alignment($field_justification[$col]);
$column->set_sort_column_id($col);
// set the header font and color
$label = new GtkLabel($field_header[$col]);
$label->modify_font(new PangoFontDescription("Arial Bold"));
$label->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000FF"));
$column->set_widget($label);
$label->show();
// setup self-defined function to display alternate row color
$column->set_cell_data_func($cell_renderer, "format_col", $col);
$view->append_column($column);
}
// pupulates the data
for ($row=0; $row<count($data); ++$row) {
$values = array();
for ($col=0; $col<count($data[$row]); ++$col) {
$values[] = $data[$row][$col];
}
$model->append($values);
}
// setup selection
$selection = $view->get_selection(); // get the selection associated with the view
$selection->connect('changed', 'on_selection'); // note 1
}
// self-defined function to format the price column
function format_col($column, $cell, $model, $iter, $col_num) {
$path = $model->get_path($iter); // get the current path
$row_num = $path[0]; // get the row number
if ($col_num==3) {
$amt = $model->get_value($iter, 3);
$cell->set_property('text', '$'.number_format($amt,2));
}
$row_color = ($row_num%2==1) ? '#dddddd' : '#ffffff'; // sets the row color for odd and even rows
$cell->set_property('cell-background', $row_color); // sets the background color
}
// the function that is called when user selects a row
function on_selection($selection) { // note 2
list($model, $iter) = $selection->get_selected(); // get the $model and $iter
$desc = $model->get_value($iter, 1); // get the $desc (1st col)
$qty = $model->get_value($iter, 2); // get the $qty (2nd col)
$price = $model->get_value($iter, 3); // get the $price (3rd col)
print "You have selected $desc: $qty ($price)\n";
}
function CellRenderer_Edited($cell, $path, $newText,$view,$col){
print "nice effects in the list\n";
$model=$view->get_model();
$iter = $model->get_iter_from_string($path);
$model->set($iter, $col, $newText);
$nextiter = $model->iter_next($iter);
if ($nextiter) {
$nextpath = $model->get_path($nextiter);
list ($path, $column) = $view->get_cursor();
$view->set_cursor($nextpath, $column, true);
}
}
$window->show_all();
Gtk::main();
?>