Re: gtktreeStore witth different combo as cell renderers.
| From: | kksou | Date: | Mon, 05 Mar 2007 15:41:32 +0000 |
| Subject: | Re: gtktreeStore witth different combo as cell renderers. | ||
| References: | 1 | Groups: | php.gtk.general |
| Request: | Send a blank email to php-gtk-general+get-7724@lists.php.net to get a copy of this message | ||
Malk0 wrote:
> what i want to achieved is rendering a gtktreestore with different
> combo value in the same column i explain.
> i want a column 2 which can handle a combo exactly as the example here
>
> http://www.kksou.com/php-gtk2/articles/use-GtkCellRendererCombo---Part-1---setup.php
>
> but what i want is on one line a list of choice as val1, val2, val3 in
> the col category
> and val5, val6, val7 on another line and different values or no values
> at all for another line.
>
> Do you think this is possible and if yes is someone can help me with a
> little example?
>
> nathan
Hi Nathan,
Attached below is a sample code to have custom pulldown values for each row:
More explanation at:
http://www.kksou.com/php-gtk2/articles/use-GtkCellRendererCombo---Part-4---custom-pulldown-values-for-each-row.php
Regards,
/kksou
<?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("Using GtkCellRendererCombo - Part 4\n".
"custom pulldown values for each row");
$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);
// 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 1', 2, 3.1),
array('row1', 'item 4', 5, 6.21),
array('row2', 'item 7', 8, 9.36),
array('row3', 'item 10', 11, 12.4),
array('row4', 'item 13', 14, 15.5),
array('row5', 'item 16', 17, 18.6),
array('row6', 'item 19', 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, Gtk::TYPE_STRING);
$field_header = array('Row #', 'Description', 'Qty',
'Price');
// creates the view to display the list store
$view = new GtkTreeView($model);
$scrolled_win->add($view);
// creates columns
for ($col=0; $col<count($field_header); ++$col) {
$cell_renderer = new GtkCellRendererText();
$column = new GtkTreeViewColumn($field_header[$col], $cell_renderer,
'text', $col);
$column->set_cell_data_func($cell_renderer, "format_col", $col);
$view->append_column($column);
}
// setup combo box
$cell_renderer = new GtkCellRendererCombo();
/*$category = new GtkListStore(Gtk::TYPE_STRING);
$list = array('grp 1', 'grp 2', 'grp 3', 'grp 4');
foreach($list as $choice) {
$category->append(array($choice));
}
$cell_renderer->set_property('model', $category);*/
$cell_renderer->set_property('text-column', 0);
$cell_renderer->set_property('editable', true);
$cell_renderer->connect('edited', 'on_combo', $model);
$column = new GtkTreeViewColumn('Category', $cell_renderer, 'text', 4);
$column->set_cell_data_func($cell_renderer, "format_col", 4); // note 1
$view->append_column($column);
// setup selection
$selection = $view->get_selection(); // get the selection associated
with the view
$selection->connect('changed', 'on_selection');
// pupulates the data
for ($row=0; $row<count($data); ++$row) {
$values = array();
for ($col=0; $col<count($data[$row]); ++$col) {
$values[] = $data[$row][$col];
}
$values[] = '';
$model->append($values);
}
}
// self-defined function to display alternate row color
function format_col($column, $cell, $model, $iter, $col_num) {
$path = $model->get_path($iter);
$row_num = $path[0];
if ($col_num==4) {
$qty = $model->get_value($iter, 2);
$category = new GtkListStore(Gtk::TYPE_STRING);
$list = array($qty, $qty*10, $qty*100, $qty*1000);
foreach($list as $choice) {
$category->append(array($choice)); // note 2
}
$cell->set_property('model', $category);
}
$row_color = ($row_num%2==1) ? '#dddddd' : '#ffffff';
$cell->set_property('cell-background', $row_color);
}
function on_combo($renderer, $path, $selection, $model) {
$iter = $model->get_iter($path);
$model->set($iter, 4, $selection);
}
// the function that is called when user selects a row
function on_selection($selection) {
list($model, $iter) = $selection->get_selected();
$desc = $model->get_value($iter, 1);
$qty = $model->get_value($iter, 2);
$price = $model->get_value($iter, 3);
$grp = $model->get_value($iter, 4);
echo "You have selected $desc: $qty ($price) category = $grp\n";
}
$window->show_all();
Gtk::main();
?>
--
View this message in context: http://www.nabble.com/gtktreeStore-witth-different-combo-as-cell-renderers.-tf3348114.html#a9313855
Sent from the Php - GTK - General mailing list archive at Nabble.com.