This script displays a 2D array inside a textview. All elements on the same row and coloumn are underlined and colored blue.
<?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
$underline_tag = new GtkTextTag();
$underline_tag->set_property('underline', Pango::UNDERLINE_SINGLE);
$tag_table->add($underline_tag);
//blue
$blue_tag = new GtkTextTag();
$blue_tag->set_property('foreground', "#0000ff");
$tag_table->add($blue_tag);
/*** Create sample data ***/
$a=Array(1,2,3,4,5);
$b=Array(6,7,8,9324,10);
$c=Array(11,12,13,14,15);
$matrix = array($a,$b,$c);
/*** Decide length of the maximum element, add 2 extra spaces ***/
$len = strlen((string)array_get_max($matrix))+2;
$txt='';
foreach($matrix as $row_key => $row)
{
foreach($row as $col_key => $cell)
{
//if row = col
if($row_key == $col_key)
{
/*** Find the iterators' position ***/
$start = $buffer->get_char_count()+strlen($txt)+$indent;
$end = $start + strlen((string)$cell);
//insert the text
$buffer->insert($buffer->get_end_iter(),$txt.str_pad($cell, $len , ' ', STR_PAD_RIGHT));
//get the iterators
$start=$buffer->get_iter_at_offset($start);
$end = $buffer->get_iter_at_offset($end);
//apply the style
$buffer->apply_tag($underline_tag, $start, $end);
$buffer->apply_tag($blue_tag, $start, $end);
//reset the variable
$txt = '';
}
else
$txt .=str_pad($cell, $len , ' ', STR_PAD_RIGHT);
}//foreach
$txt.="\n";
}//foreach
//Insert what remained in the $txt variable
$buffer->insert($buffer->get_end_iter(),$txt);
$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
?>
Comentários recentes
5 anos 39 semanas atrás
5 anos 40 semanas atrás
5 anos 40 semanas atrás
5 anos 43 semanas atrás
6 anos 11 semanas atrás
6 anos 15 semanas atrás
6 anos 19 semanas atrás
6 anos 19 semanas atrás
6 anos 19 semanas atrás
6 anos 36 semanas atrás