Saturday, September 04, 2004

GtkCList with sort support

this is my first php-gtk related post on this blog.
bellow you ca find a class derived from GtkClist to which I've added sort cappabilities for columns.


<?php
class IList extends GtkCList
{
    var
$lastSortColumn; //last sorted column
    
var $lastSortType; //last sort type
    
var $labels; //column titles
    
    /**
    * @return IList
    * @param array $labels
    * @desc constructor
    */
    
function IList($labels)
    {
        
GtkCList::GtkCList(count($labels), $labels);
        
$this->labels = $labels;
        
GtkCList::connect("click_column", array(&$this, "_sortColumn"));
    }
    
    
/**
    * @return private
    * @param IList $list
    * @param int $column
    * @desc sorts the column
    */
    
function _sortColumn($list, $column)
    {
        if (
$column == $this->lastSortColumn)
        {
            
$this->lastSortType = ($this->lastSortType) ? false : true;
        }
        else
        {
            
$this->lastSortType = true;
        }
        
IList::_setSortColumn($column, $this->lastSortType);
        
        
GtkCList::set_sort_type($this->lastSortType ? GTK_SORT_ASCENDING : GTK_SORT_DESCENDING);
        
$GLOBALS["curentListSort"] = array($this->lastSortColumn, $this->lastSortType);
        
GtkCList::set_sort_column($this->lastSortColumn);
        
GtkCList::sort();
    }
    
    
/**
    * @return void
    * @param int $column
    * @param boolean $ascending
    * @desc
    */
    
function _setSortColumn($column, $ascending = true)
    {
        if (!
is_null($this->lastSortColumn))
        {
            
$widget = GtkCList::get_column_widget($this->lastSortColumn);
            
$widget->destroy();
            for (
$i=0; $i<count($this->labels); $i++)
            {
                
GtkCList::set_column_title($i, $this->labels[$i]);
            }
        }
        
$hbox = &new GtkHBox();
        
$label = &new GtkLabel($this->labels[$column]);
        if (
$ascending === true)
        {
            
$image = &new GtkArrow(GTK_ARROW_UP, GTK_SHADOW_ETCHED_IN);
        }
        else
        {
            
$image = &new GtkArrow(GTK_ARROW_DOWN, GTK_SHADOW_ETCHED_IN);
        }
        
        
$hbox->pack_start($label, false, false,0);
        
$hbox->pack_end($image, false, false,0);
        
$hbox->show_all();
        
GtkCList::set_button_actions(1, 1);
        
GtkCList::set_shadow_type(GTK_SHADOW_OUT);
        
GtkCList::set_column_widget($column, $hbox);
        
$this->lastSortColumn = $column;
    }
}
?>



... more to come. stay close

0 Comments:

Post a Comment

<< Home