 |
| View previous topic :: View next topic |
| Author |
Message |
kingrs
Joined: 15 Jan 2006 Posts: 30 Location: Stafford, UK
|
Posted: Thu Jan 19, 2006 7:00 pm Post subject: Right aligned column formatted to 2 decimal places |
|
|
This is the code example:
| Code: |
<?
class StockItemBrowserDemo extends GtkWindow {
function __construct($parent = null)
{
parent::__construct();
if ($parent)
$this->set_screen($parent->get_screen());
else
$this->connect('destroy', 'main_quit');
$this->set_title('Programming languages');
$this->set_position(Gtk::WIN_POS_CENTER);
$this->set_default_size(250, 200);
$this->set_border_width(8);
//the model
$store = new GtkListStore(Gtk::TYPE_STRING, Gtk::TYPE_DOUBLE);
//add some data to the data store
$store->append(array('PHP for idots' , 123.00));
$store->append(array('C for beginners' ,8.95));
$store->append(array('C++ Guide', 533.33));
$store->append(array('Ruby for the novist',2333.11));
//sort by price by default
$store->set_sort_column_id(1, Gtk::SORT_ASCENDING);
//We want to display our data in a GtkTreeView
$treeview = new GtkTreeView($store);
//the text renderer is used to display text
$cell_renderer = new GtkCellRendererText();
//Create the first column, make it resizable and sortable
$colLanguage = new GtkTreeViewColumn('Book', $cell_renderer, 'text', 0);
//make the column resizable in width
$colLanguage->set_resizable(true);
//make it sortable and let it sort after model column 1
$colLanguage->set_sort_column_id(0);
//add the column to the view
$treeview->append_column($colLanguage);
$cell_renderer_price = new GtkCellRendererText();
//right align the cell *** this took me ages to figure out ***
// ********************************************************************************
$cell_renderer_price->set_property("xalign",1);
// ********************************************************************************
$cell_renderer_price->set_property("cell-background","goldenrod");
//second column, also sortable
$colPrice = new GtkTreeViewColumn('Price', $cell_renderer_price, 'text', 1);
$colPrice->set_sort_column_id(1);
// format the number to 2 dp
// ********************************************************************************
$colPrice->set_cell_data_func($cell_renderer_price,'format_num');
// ********************************************************************************
$treeview->append_column($colPrice);
$selection = $treeview->get_selection();
$selection->set_mode(Gtk::SELECTION_SINGLE);
$selection->connect('changed', array($this, 'on_treeview_row_activate'));
//to make the view scrollable, we need a scrolled window
$scrwnd = new GtkScrolledWindow();
$scrwnd->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
$scrwnd->add($treeview);
$this->add($scrwnd);
$this->show_all();
}
function on_treeview_row_activate ($selection) {
list($model, $iter) = $selection->get_selected();
if ($iter) {
$book = $model->get_value($iter, 0);
$price = $model->get_value($iter, 1);
echo "book: $book, price=$price\n";
}
}
}
function format_num($column, $cell, $model, $iter)
{
$info = $model->get_value($iter, 1);
$cell->set_property('text', number_format ($info, 2));
}
function main_quit () {
gtk::main_quit();
}
new StockItemBrowserDemo();
Gtk::main();
?>
|
|
|
| Back to top |
|
 |
rajuvk
Joined: 02 May 2006 Posts: 10 Location: Kerala, India
|
Posted: Tue May 02, 2006 8:43 am Post subject: |
|
|
These (right align and precision setting) has wasted my whole two days). Curse me for my failure to look up at this forum. Any way thanks to kingrs for this nice tip.
Raju |
|
| Back to top |
|
 |
rajuvk
Joined: 02 May 2006 Posts: 10 Location: Kerala, India
|
Posted: Wed May 03, 2006 11:40 am Post subject: |
|
|
The decimal setting given in the example works for a single column only as the argument for column number is not passed in the function call. From my tests it is found that by passing the column number as an argument will work for any number of columns. The modifications are as follows:
In the function call use:
| Code: | | $colPrice->set_cell_data_func($cell_renderer_price,'format_num', $col_no=1); |
instead of :
| Code: | | $colPrice->set_cell_data_func($cell_renderer_price,'format_num'); |
Modify the function as:
| Code: | function format_num($column, $cell, $model, $iter, $col_no)
{
$info = $model->get_value($iter, $col_no);
$cell->set_property('text', number_format ($info, 2));
}
|
To format a third column containing numbers with two digit precision call the function with $col_no=2 and so on.
As the documentation is not complete I could not find a better method. If there is a better one please post here.
Raju |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
 |