Matt-239 wrote:
I just upgraded from the Beta to php-gtk 2. I've been tweaking things to
work with it (mainly the Gtk::TYPE's to GObject::TYPE) but I've run
across one issue with GtkCellLayout::set_cell_data_func.
Before, I could pass it a callback function in the current class like so:
$col->set_cell_data_func($renderer, array($this, 'format_col'));
And that worked fine.. now it seems it won't take the array and returns
an error:
Warning: Unable to invoke callback 'Array' specified in File.php on line xxx
Hi Matt,
I've tried using set_cell_data_func() with class methods. It does work.
Attached a sample code below...
Regards,
/kksou
<?php
class app {
function __construct() {
$this->setup();
}
function setup() {
$this->window = new GtkWindow();
$this->window->set_size_request(400, 200);
$this->window->connect_simple('destroy', array('Gtk','main_quit'));
$this->window->add($this->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);
$this->vbox->pack_start($title, 0, 0);
$this->vbox->pack_start(new GtkLabel(), 0, 0);
$data = array(
array('row0', 'item 42', 2, 3.1),
array('row1', 'item 36', 20, 6.21),
array('row2', 'item 21', 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));
$this->display_table($data);
}
function display_table($data) {
// Set up a scroll window
$scrolled_win = new GtkScrolledWindow();
$scrolled_win->set_policy( Gtk::POLICY_AUTOMATIC,
Gtk::POLICY_AUTOMATIC);
$this->vbox->pack_start($scrolled_win);
// Creates the list store
$model = new GtkListStore(GObject::TYPE_STRING, GObject::TYPE_STRING,
GObject::TYPE_LONG, GObject::TYPE_DOUBLE);
$field_header = array('Row #', 'Description', 'Qty', 'Price');
$field_justification = array(0.0, 0.0, 0.5, 1.0);
// 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->set_property("xalign", $z);
$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, array($this, '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);
}
}
// 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';
$cell->set_property('cell-background', $row_color);
}
function go() {
echo "gtk ver: ".Gtk::get_version()."\n";
$this->window->show_all();
Gtk::main();
}
}
$app = new App();
$app->go();
?>