Colored Table, Row and Column Headers In Textview.

<?php
// Definition of widgets
$scrolled_win = new GtkScrolledWindow();
$textview = new GtkTextview();
$buffer = new GtkTextBuffer();
$win = new GtkWindow();

// Modify widgets
$scrolled_win->set_policy(1,1);
$textview->modify_font(new PangoFontDescription('Courier New  14'));
$win->maximize();
$win->connect_simple('destroy', array('Gtk', 'main_quit'));
       
// Pack widgets
$scrolled_win->add($textview);
$textview->set_buffer($buffer);
$win->add($scrolled_win);

/*** Get tags table ***/
$tag_table = $buffer->get_tag_table();

/*** Create and add tag ***/
// underline
$tag_underline = new GtkTextTag();   
$tag_underline->set_property('underline', Pango::UNDERLINE_SINGLE);
$tag_table->add($tag_underline);
// blue
$blue_tag = new GtkTextTag();
$blue_tag->set_property('foreground', "#0000ff");
$tag_table->add($blue_tag);
// bold
$tag_bold = new GtkTextTag();
$tag_bold->set_property('weight', Pango::WEIGHT_BOLD);
$tag_table->add($tag_bold);
// highlight
$tag_highlight = new GtkTextTag();
$tag_highlight->set_property('background', "#d9dae7");
$tag_table->add($tag_highlight);
// highlight2
$tag_highlight2 = new GtkTextTag();
$tag_highlight2->set_property('background', "#ccff99");
$tag_table->add($tag_highlight2);


/*** Create sample data ***/
$matrix;
for(
$i=0; $i<60;$i++)
{
   
$count=rand(6,48);
    for(
$j=0;$j<$count;$j++)
       
$matrix[$i][$j] = rand(-100,333);
}

/*** Decide length of the maximum element, add 2 extra spaces ***/
define(INDENT,1);
$len = strlen((string)array_get_max($matrix))+INDENT;


   
/*** Add the left corner ***/
   
$buffer->insert_at_cursor('row\col');
   
$start = $buffer->get_start_iter();
   
$end  = $buffer->get_end_iter();
   
$buffer->apply_tag($tag_underline, $start, $end);
   
$buffer->apply_tag($tag_bold, $start, $end);
   
$buffer->apply_tag($tag_highlight, $start, $end);
   
   
/***  Add coloumn header ***/
   
$count=0;
    foreach(
$matrix as $row)
       
$count=($count>=count($row))?$count:count($row);
    for(
$i=0; $i<$count; $i++)
    {           
       
//Insert the header
       
$buffer->insert_at_cursor($header = str_pad('c'.$i, $len , ' ', STR_PAD_LEFT));
       
$start = $buffer->get_end_iter();
       
$start->backward_chars(strlen($header)-INDENT);
       
$buffer->apply_tag($tag_underline, $start, $buffer->get_end_iter());
       
$buffer->apply_tag($tag_bold, $start, $buffer->get_end_iter());
       
$buffer->apply_tag($tag_highlight, $start, $buffer->get_end_iter());
    }

   
$buffer->insert_at_cursor("\n");
       
    foreach(
$matrix as $row_key => $row)
    {
       
/*** Insert row head ***/
       
$buffer->insert_at_cursor($header = str_pad('r'.$row_key, strlen('row\col') , ' ', STR_PAD_LEFT));
       
$start = $buffer->get_end_iter();
       
$start->backward_chars(strlen($header));
       
$buffer->apply_tag($tag_underline, $start, $buffer->get_end_iter());
       
$buffer->apply_tag($tag_bold, $start, $buffer->get_end_iter());
       
$buffer->apply_tag($tag_highlight, $start, $buffer->get_end_iter());
       
       
/*** Insert cell ***/   
       
for($i=0;$i<$count;$i++)
        {
            if(isset(
$row[$i]))
               
$cell = $row[$i];
            else
               
$cell = '';
           
$buffer->insert_at_cursor($cell = str_pad($cell, $len , ' ', STR_PAD_LEFT));
           
$start = $buffer->get_end_iter();
           
$start->backward_chars(strlen($cell)-INDENT);
           
$buffer->apply_tag($tag_highlight2, $start, $buffer->get_end_iter());
        }
       
//next line
       
$buffer->insert_at_cursor("\n");
    }

   
$win->show_all();
Gtk::main();
       

   
function
array_get_max($matrix)
{   
   
   
//initialize max
   
traverseArrayGetFirstValue($matrix,$max);
   
//find max
   
traverseArrayGetMax($matrix,$max);
   
//return max
   
return $max;
}
//function


   
function traverseArrayGetFirstValue($array, &$fv)
    {    
        foreach(
$array as $key=>$value)
            if(
is_array($value))
               
traverseArrayGetFirstValue($value,$fv);
            elseif(
$fv == null)
                return
$fv = $value;
            else
                return
$fv;   
    }
//function
   
function traverseArrayGetMax($array, &$max)
    {
        foreach(
$array as $key=>$value)
            if(
is_array($value))
               
traverseArrayGetMax($value,$max);
            elseif(
$value>$max)
               
$max = (float)$value;
    }
//function
?>