cvs: pear /PEAR_Frontend_Gtk/Gtk WidgetHTML.php
| From: | Alan Knowles | Date: | Wed, 05 Jun 2002 12:19:16 +0000 |
| Subject: | cvs: pear /PEAR_Frontend_Gtk/Gtk WidgetHTML.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-4420@lists.php.net to get a copy of this message | ||
alan_k Wed Jun 5 08:19:16 2002 EDT
Modified files:
/pear/PEAR_Frontend_Gtk/Gtk WidgetHTML.php
Log:
HTML Browser can now handle tables - to some degree
Index: pear/PEAR_Frontend_Gtk/Gtk/WidgetHTML.php diff -u pear/PEAR_Frontend_Gtk/Gtk/WidgetHTML.php:1.1 pear/PEAR_Frontend_Gtk/Gtk/WidgetHTML.php:1.2 --- pear/PEAR_Frontend_Gtk/Gtk/WidgetHTML.php:1.1 Tue Jun 4 06:14:40 2002 +++ pear/PEAR_Frontend_Gtk/Gtk/WidgetHTML.php Wed Jun 5 08:19:15 2002 @@ -2,19 +2,19 @@ -class PEAR_Frontend_Gtk_WidgetHTML { - +class PEAR_Front_Gtk_WidgetHTML { + var $pass=1; var $_source; // raw HTML - function test() { - $this->_source = implode('',file('http://pear.php.net/manual/en/packages.templates.it.php')); - $fh = fopen('/tmp/test','w'); fwrite($fh,$this->_source ); fclose($fh); + function test($file) { + $this->_source = implode('',file($file)); + //$fh = fopen('/tmp/test','w'); fwrite($fh,$this->_source ); fclose($fh); } var $_tokens = array(); // HTML Tokens - function tokenize() { + function tokenize() { $tok = strtok($this->_source,'<'); $a[0] = $tok; while( $tok !== FALSE ) { @@ -38,6 +38,7 @@ } if (strcspn($b," \n") == $l+1) { $this->_tokens[] = array(strtoupper(substr($b,0,strpos($b,'>')))); + $this->_tokens[] = substr($b,strpos($b,'>')+1) ; continue; } if (strcspn($b," \n") > strpos($b,'>')) { @@ -51,26 +52,44 @@ $this->_tokens[] = array($tag,$attribs); $this->_tokens[] = substr($b,strpos($b,'>')+1); } + //print_r($this->_tokens); + //exit; } - - function build() { - $this->widget->freeze(); + var $Start = FALSE; // start rering (eg. ignore headers) + + function build($startpos,$endpos) { + $this->layout->freeze(); $this->Start=FALSE; - foreach($this->_tokens as $pos=>$item) { + $this->_DrawingAreaClear(); + + $this->_line_y = array(); + $this->_y =10; + $this->_states = array(); + $this->states = array(); + $this->cur = array(); + $this->curid = array(); + + + $this->pushState(0); + for($pos = $startpos;$pos < $endpos;$pos++) { + $item = $this->_tokens[$pos]; if (is_Array($item)) { $method = "push"; if (!$item[0]) continue; + //echo $pos; //echo "\nIN:".serialize($item)."\n"; + $this->outputTAG($item[0]); if ($item[0]{0} == '/') { $method = "pop"; $item[0] = substr($item[0],1); $item[1] = '/'; } - + //if (!$draw) + + switch (trim($item[0])) { case '!DOCTYPE': case 'HTML': - case 'META': case 'LINK': case 'HEAD': @@ -96,48 +115,60 @@ case 'TABLE': // unhandled stauff if ($method == 'push') { // start + $this->pushState($pos); // move us down abit and start a new row $this->_x = $this->_left; - $this->_y +=16; - $this->calctable($pos,$this->_right- $this->_x); + if ($this->pass == 1) + $this->calctable($pos,$this->_x ,$this->_right); + $this->push($item[0],$pos,@$item[1]); //$this->output("TABLE:$pos"); - } else { //end + } else { // + $table = $this->pop('TABLE'); + $this->popState($table); $this->_x = $this->_left; - $this->_y +=16; + $this->_y +=16; + + + $this->clearStack('TD',$table); + $this->clearStack('BGCOLOR',$table); + $this->clearStack('FGCOLOR',$table); + $this->clearStack('TR',$table); + } + + break; //case 'TR': case 'TD': - if (!$this->td[$pos]) break; + if ($method == 'push') { // start - echo "TDPstart: $pos L:{$this->_left},{$this->_right}\n"; - $this->push('right',$this->td[$pos]['right']); - $this->push('left',$this->td[$pos]['left']); + if (!@$this->td[$pos]) break; + //echo "TDPstart: $pos L:{$this->_left},{$this->_right}\n"; + $this->_left = $this->td[$pos]['left']; $this->_right = $this->td[$pos]['right']; - $row = $this->td[$pos]['row']; - if (!$this->tr[$row]) - $this->tr[$row] = $this->_y+16; + + + $this->td[$pos]['height'] = 0; + if (@isset($this->td[$pos]['topline'])) { + $this->_y = $this->td[$pos]['topline']; + } else { + $this->_y+16; + } $this->_x = $this->_left; - - $this->_y =$this->tr[$row] ; - //$this->output("TD:$pos"); - //echo "TDstart: $pos L:{$this->_left},{$this->_right}\n"; + $this->push($item[0],$pos,@$item[1]); } else { // pull back! - $this->pop('right'); - $this->pop('left'); - $this->_left = 10; - if ($s = $this->in('left')) - $this->_left = $s; - $this->_right = 600; - if ($r = $this->in('right')) - $this->_right = $r; - echo "endtd: $pos L:{$this->_left},{$this->_right}\n"; + $td = $this->pop('TD'); + + $this->clearStack('TD',$td); + $this->clearStack('BGCOLOR',$td); + $this->clearStack('FGCOLOR',$td); + $this->clearStack('TR',$td); } - + break; @@ -145,8 +176,9 @@ $this->Start = TRUE; break; case 'PRE': + $this->output($item[0]); $this->linebr($item[0]); - $this->$method($item[0]); + $this->$method($item[0],$pos); case 'SELECT': // hide this stuff case 'TEXTAREA': @@ -156,7 +188,7 @@ case 'CODE': case 'B': case 'I': - $this->$method($item[0]); + $this->$method($item[0],$pos); break; case 'SMALL': @@ -167,17 +199,20 @@ case 'H4': case 'H5': case 'H6': + //$this->output($item[0]); $this->linebr(); - $this->$method('H',$item[0]{1}); + $this->$method('H',$pos,$item[0]{1}); break; - case 'DIV': // ?? this stuf could contain formating? - case 'FONT': + case 'DIV': // ?? this stuf could contain formating? + case 'FONT': + case 'TR': case 'A': - $this->$method($item[0],@$item[1]); + $this->$method($item[0],$pos,@$item[1]); break; - //case 'TR': + case 'BR': - case 'P': + case 'P': + //$this->output($item[0]); $this->linebr($item[0]); break; default: @@ -187,17 +222,42 @@ continue; } + //echo $pos; // strings only! $this->output($item); } $this->linebr(); - $this->widget->set_size(600, $this->_y+16); - $this->widget->thaw(); + $this->_area_y = $this->_y+16; + $this->layout->set_size($this->_area_x , $this->_area_y ); + $this->layout->thaw(); + //return; + //print_r($this->td); + if ($this->pass == 1) { + $this->pass++; + $this->build($startpos,$endpos); + } + } - var $check = "xxxx"; + var $check = ""; - function push($what,$attributes='') { + var $_states = array(); // array of pos'id => various data!!! + function pushState($id) { + foreach(array('_left','_right') as $k) + $this->_states[$id][$k] = $this->$k; + + } + + function popState($id) { + foreach(array('_left','_right') as $k) + $this->$k = $this->_states[$id][$k]; + + } + var $stack;// array of item stack + var $cur; // top of stack + var $curid; // id of top of stack + + function push($what,$pos,$attributes='') { //echo "PUSH: $what, $attributes\n"; if ($attributes && $attributes{strlen($attributes)-1} == '/') { //echo "SKIP"; @@ -206,79 +266,106 @@ if (!@$this->stack[$what]) $this->stack[$what] = array(); if (!$attributes) $attributes = ":"; - $this->stack[$what][] = $attributes; + $this->stack[$what][] = array($pos,$attributes); $this->cur[$what] = $attributes; - $this->_stackAttributes($attributes,'push'); + $this->curid[$what] = $pos; + $this->_stackAttributes($attributes,$pos,'push'); - if ($what != $this->check) return; - echo "\nPUSH:$what:".serialize($this->stack[$what]); - echo "\nCUR:{$this->cur[$what]}"; + if ($this->check && preg_match("/^(".$this->check.")$/",$what)) { + echo "\nPUSH:$what:".serialize($this->stack[$what]); + echo "\nCUR:{$this->cur[$what]}"; + } } - function pop($what,$attributes=array()) { + function pop($what,$pos=0,$attributes=array()) { if (!@$this->stack[$what]) return; - $remove = array_pop($this->stack[$what]); + list($id,$remove) = array_pop($this->stack[$what]); $this->cur[$what] = ""; if ($this->stack[$what]) - $this->cur[$what] = $this->stack[$what][count($this->stack[$what])-1]; - $this->_stackAttributes($remove,'pop'); - if ($what != $this->check) return; - echo "\nPOP:$what:".serialize($this->stack[$what]); - echo "\nCUR:{$this->cur[$what]}"; + list($this->curid[$what],$this->cur[$what]) = $this->stack[$what][count($this->stack[$what])-1]; + $this->_stackAttributes($remove,$pos,'pop'); + /* debugging*/ + if ($this->check && preg_match("/^(".$this->check.")$/",$what)) { + echo "\nPOP:$what:".serialize($this->stack[$what]); + echo "\nCUR:{$this->cur[$what]}"; + } + return $id; + } + + function clearStack($what,$pos) { + //echo "CLEARING STACK OF $what for $pos\n "; + if (!@$this->stack[$what]) return; + $c = count(@$this->stack[$what]) -1; + for($i=$c;$i>-1;$i--) { + list($id,$attr) = $this->stack[$what][$i]; + if ($id >= $pos) $this->pop($what); + } } - function _stackAttributes($attributes,$method) { + + + function _stackAttributes($attributes,$pos,$method) { if ($attributes == ":") return; if ($attributes == '/') return; if ($attributes{0} == "#") return; $args = array(); - if (preg_match("/ color\=[\"\']?(\#[0-9A-F]+)[\"\']?/mi",' '.$attributes,$args)) - $this->$method("FGCOLOR",$args[1]); + if (preg_match("/\scolor\=[\"\']?(\#[0-9A-F]+)[\"\']?/mi",' '.$attributes,$args)) + $this->$method("FGCOLOR",$pos,$args[1]); $args = array(); - if (preg_match("/ bgcolor\=[\"\']?(\#[0-9A-F]+)[\"\']?/i",' '.$attributes,$args)) - $this->$method("BGCOLOR",$args[1]); + if (preg_match("/\sbgcolor\=[\"\']?(\#[0-9A-F]+)[\"\']?/mi",' '.$attributes,$args)) + $this->$method("BGCOLOR",$pos,$args[1]); $args = array(); - if (preg_match("/ text\=[\"\']?(\#[0-9A-F]+)[\"\']?/i",' '.$attributes,$args)) - $this->$method("FGCOLOR",$args[1]); - + if (preg_match("/\stext\=[\"\']?(\#[0-9A-F]+)[\"\']?/mi",' '.$attributes,$args)) + $this->$method("FGCOLOR",$pos,$args[1]); + + if (preg_match("/\sclass\=[\"\']?([A-Z]+)[\"\']?/mi",' '.$attributes,$args)) + $this->_stackStyle($method,$pos,$args[1]); + } + function _stackStyle($method,$pos,$class) { + return; + /* TODO?? */ + switch ($class) { + case 'programlisting': + $this->$method("BGCOLOR",''); + } + + } + + function in($what) { return @$this->cur[$what]; } - + function inID($what) { + return @$this->curid[$what]; + } function linebr($item='') { if (($item == "P") && ($this->lastbr == "P")) return; //if ($item && $this->lastbr && ($this->lastbr != $item)) return; //$this->widget->insert($this->_font,$this->_fgcolor,$this->bg_color,":$item:\n"); - - $this->_y += 14; + $this->_nextLine(); $this->_x = $this->_left; } var $buffer=""; - var $_fonts = array(); // associative array of fonts - var $_colors = array(); // associative array of colors - - var $_font = NULL; - var $_fgcolor = NULL; - var $_bgcolor = NULL; + var $_left = 10; var $_right = 600; var $_x = 10; - var $_y = 0; + var $_y = 20; function output($string) { if (!$this->Start) return; @@ -293,7 +380,7 @@ $this->_makeFont(); $this->_makeColors(); - $this->_setStyle(); + //$this->_setStyle(); if ($this->in('PRE')) { $this->outputPRE($string); return; @@ -301,25 +388,64 @@ $this->outputTEXT($string); } - + + function outputTAG($tag) { + return; + $this->_makeFont('-adobe-helvetica-bold-r-normal-*-*-60-*-*-p-*-iso8859-1'); + $this->_makeColors("#000000","#FFFF00",FALSE); + //$x= $this->_x; + //$y= $this->_y; + $this->outputTEXT("<{$tag}>",TRUE); + //$this->_x = $x; + //$this->_y = $y; + } + + var $line =0; + + function outputTEXT($string) { $array = $this->_breakString($this->_x,$this->_left,$this->_right,$string); // array of lines (startpos,len,text) - - foreach($array as $line) { + $h = $this->_font->ascent + $this->_font->descent; + $c = count($array) -1; + foreach($array as $i=>$line) { + if ($line[2]) { - $widget = &new GtkLabel($line[2]); - $widget->set_style($this->_style); - //echo "ADD: {$line[0]},{$this->_y} : {$line[2]}\n"; - $this->widget->put($widget,$line[0],$this->_y); - $widget->show(); + //$widget = &new GtkLabel($line[2]); + //$widget->set_style($this->_style); + if ($this->pass == 2) + //echo "ADD: {$line[0]},{$this->_y} : {$line[2]}\n"; + //$this->layout->put($widget,$line[0],$this->_y); + + gdk::draw_rectangle($this->pixmap, + $this->_bggc,true, + $line[0], $this->_y-$this->_font->ascent, + $line[1], $h + ); + + gdk::draw_text($this->pixmap, + $this->_font, + $this->_gc , + $line[0], + $this->_y, + $line[2], strlen($line[2]) + ); + + $this->drawing_area->draw( + new GdkRectangle( + $line[0], $this->_y-$this->_font->ascent, + $line[1], $h)); + //$widget->show(); + if ($c != $i) + $this->_nextLine(); } - $this->_y += 14; + + } - $this->_y -= 14; + $this->_x = $line[0] + $line[1]; $this->lastbr = ''; } @@ -338,11 +464,12 @@ foreach($array as $line) { $this->outputTEXT($line); - $this->_y += 14; + $this->_nextLine(); $this->_x = $this->_left; + } - $this->_y -= 14; + $this->_line--; $this->lastbr = ''; } @@ -399,12 +526,54 @@ } + function unhtmlentities ($string) { + $trans_tbl = get_html_translation_table (HTML_ENTITIES); + $trans_tbl = array_flip ($trans_tbl); + $ret = strtr ($string, $trans_tbl); + return preg_replace('/\&\#([0-9]+)\;/me',"chr('\\1')",$ret); + } + var $_line; + function _nextLine() { + // store the line height of the current line.. + + if (@$this->_lines[$this->_line]['ascent'] < $this->_font->ascent ) + $this->_lines[$this->_line]['ascent'] = $this->_font->ascent; + if (@$this->_lines[$this->_line]['decent'] < $this->_font->decent ) + $this->_lines[$this->_line]['decent'] = $this->_font->decent; + $this->_lines[$this->_line]['height'] = + @$this->_lines[$this->_line]['decent'] + @$this->_lines[$this->_line]['ascent']; + + // store the active block heights.... + + if ($id = $this->inID('TD')) + $this->td[$id]['lines'][] = $this->_line; + + $this->_line++; + + if (@$this->_line_y[$this->_line]) { + $this->_y = $this->_line_y[$this->_line]; + + } else if (@$this->_lines[$this->_line]['height']) { + $this->_y += $this->_lines[$this->_line]['height']; + + } else { + $this->_y += 16; + } + $this->_line_y[$this->_line] =$this->_y;; + // + + + } + /*------------------------------FONTS AND COLORS --------------------------*/ - function _makeFont() { + var $_fonts = array(); // associative array of fonts + var $_font = NULL; + function _makeFont($default='') { + $font['pointsize'] = 100; $font['space'] = 'p'; // or 'm' for monospaced $font['family'] = 'times'; // or helvetica, courier @@ -439,6 +608,9 @@ $fontname = $this->_getFontString($font); + //echo $fontname; + if ($default) $fontname = $default; + if (@!$this->_fonts[$fontname]) $this->_fonts[$fontname] = gdk::font_load($fontname); if (!$this->_fonts[$fontname]) @@ -447,7 +619,6 @@ $this->_font = &$this->_fonts[$fontname]; } - function _getFontString($array) { $ret = ""; foreach(array( @@ -455,54 +626,60 @@ 'addedstyle', 'pixelsize','pointsize','resx','resy', 'space','averagewidth','registry','encoding') as $i) { $a = '*'; - if ($array[$i]) $a = $array[$i]; + if (@$array[$i]) $a = $array[$i]; $ret .= "-{$a}"; } return $ret; } + + var $_gcs = array(); // associative array fo fgcolor:bgcolor to GC object + var $_gc = NULL; // link to current GtkGC - function _makeColors() { - $fgcolor = "#000000"; - $bgcolor = "#FFFFFF"; - - if ($c = $this->in('FGCOLOR')) - $fgcolor = $c; - if ($c = $this->in('BGCOLOR')) - $bgcolor = $c; - + var $_colors = array(); // associative array of colors + var $_fgcolor = NULL;// link to current GdkColor + var $_bgcolor = NULL;// link to current GdkColor + + function _makeColors($fgcolor = "#000000",$bgcolor = "#FFFFFF",$read=TRUE) { + if ($read) { + if ($c = $this->in('FGCOLOR')) + $fgcolor = $c; + if ($c = $this->in('BGCOLOR')) + $bgcolor = $c; + } //echo "$fgcolor:$bgcolor\n"; - if (!$this->_colors[$fgcolor]) + if (!@$this->_colors[$fgcolor]) $this->_colors[$fgcolor] = &new GdkColor($fgcolor); - if (!$this->_colors[$bgcolor]) + if (!@$this->_colors[$bgcolor]) $this->_colors[$bgcolor] = &new GdkColor($bgcolor); $this->_fgcolor = &$this->_colors[$fgcolor]; $this->_bgcolor = &$this->_colors[$bgcolor]; + /* GC STUFF */ + + + $id = $fgcolor . $bgcolor; + + if (!@$this->_gcs[$id]) { + $window = $this->drawing_area->window; + $this->_gcs[$id] = $window->new_gc(); + $cmap = $this->drawing_area->get_colormap(); + $this->_gcs[$id]->foreground = $cmap->alloc($fgcolor); + $this->_gcs[$id]->background = $cmap->alloc($bgcolor); + } + /* REVERSED - for background rectangle*/ + $bgid = $bgcolor . $fgcolor; + if (!@$this->_gcs[$bgid]) { + $this->_gcs[$bgid] = $window->new_gc(); + $this->_gcs[$bgid]->foreground = $this->_gcs[$id]->background ; + $this->_gcs[$bgid]->background = $this->_gcs[$id]->foreground; + } + $this->_gc = &$this->_gcs[$id]; + $this->_bggc = &$this->_gcs[$bgid]; + } - - - function unhtmlentities ($string) { - $trans_tbl = get_html_translation_table (HTML_ENTITIES); - $trans_tbl = array_flip ($trans_tbl); - $ret = strtr ($string, $trans_tbl); - return preg_replace('/\&\#([0-9]+)\;/me',"chr('\\1')",$ret); - } - - function testInterface() { - $w = &new GtkWindow; - $s = &new GtkScrolledWindow(); - $hadj = $s->get_hadjustment(); - $vadj = $s->get_vadjustment(); - $this->widget = &new GtkLayout($hadj,$vadj); - $this->widget->set_size(600, 12000); - $s->add($this->widget); - $w->add($s); - $w->show_all(); - - } var $_style = NULL; function _setStyle() { //echo "SET: $widgetname: $fgcolor/$bgcolor ". ((int) $copy) . "\n"; @@ -532,10 +709,125 @@ $this->_style = &$style; } + + + + + + + /* ------------------------------ BASIC WIDGET STUFF ------------------*/ + var $_area_x= 600; + var $_area_y= 10000; + + var $layout; // GtkLayout + + function testInterface() { + $w = &new GtkWindow; + $s = &new GtkScrolledWindow(); + $hadj = $s->get_hadjustment(); + $vadj = $s->get_vadjustment(); + $this->layout = &new GtkLayout($hadj,$vadj); + $this->layout->set_size($this->_area_x,$this->_area_y); + $this->_DrawingArea(); + $s->add($this->layout); + $w->add($s); + $w->show_all(); + + } + + + var $drawing_area; // GtkDrawingArea + function _DrawingArea() { // Drawing Area + //echo "PAINTER"; + $this->drawing_area = &new GtkDrawingArea(); + $this->drawing_area->size($this->_area_x,$this->_area_y); + $this->drawing_area->set_events( + GDK_EXPOSURE_MASK + | GDK_LEAVE_NOTIFY_MASK + | GDK_BUTTON_PRESS_MASK + | GDK_BUTTON_RELEASE_MASK + // | GDK_BUTTON_MOTION_MASK + | GDK_POINTER_MOTION_MASK + | GDK_KEY_PRESS_MASK + | GDK_POINTER_MOTION_HINT_MASK + ); + + //$this->drawing_area->set_events( GDK_ALL_EVENTS_MASK ); + // $this->layout->add_events( GDK_KEY_PRESS_MASK ); + + $this->drawing_area->set_flags( GTK_CAN_FOCUS); + + + + $this->drawing_area->connect("configure_event", array(&$this,"_DrawingAreaCallbackConfigure")); + $this->drawing_area->connect("expose_event", array(&$this,"_DrawingAreaCallbackExpose")); + //$this->drawing_area->connect("key_press_event", array(&$this,"_DrawingAreaCallbackKeyPress")); + //$this->drawing_area->connect("button_release_event", array(&$this,"_DrawingAreaCallbackBtnPress")); + //$this->drawing_area->connect("button_press_event", array(&$this,"_DrawingAreacallbackBtnPress")); + //$this->html->drawing_area->connect_after("motion_notify_event", array(&$this,"callback_pointer_motion_event")); + //$this->html->drawing_area->connect("expose_event", + // array(&$this,"callback_expose_event")); + $this->drawing_area->show(); + + + $this->layout->put($this->drawing_area,0,0); + + } + + var $pixmap; + + function _DrawingAreaCallbackConfigure($widget, $event) { + if (@$this->pixmap) return; + + $this->pixmap = &new GdkPixmap($widget->window, + $this->_area_x,$this->_area_y, + -1); + + $this->_DrawingAreaClear(); + // draw somethin on it. + + //$this->drawing_area->realize(); + + + if (!$this->Start) { + $this->build(0,count($this->_tokens),1); + //$this->build(FALSE); + } + + return true; + } + + function _DrawingAreaClear() { + gdk::draw_rectangle($this->pixmap, + $this->drawing_area->style->white_gc, + true, 0, 0, + $this->_area_x,$this->_area_y); + // draw somethin on it. + $this->drawing_area->realize(); + + } + + function _DrawingAreaCallbackExpose($widget,$event) { + + gdk::draw_pixmap($this->drawing_area->window, + $widget->style->fg_gc[$widget->state], + $this->pixmap, + $event->area->x, $event->area->y, + $event->area->x, $event->area->y, + $event->area->width, $event->area->height); + + } + + + + /* ------------------------------ TABLE STUFF ------------------*/ + + + /* @@ -581,23 +873,26 @@ /* first version just divides available area! */ var $td; // associative array of [posid]['width'|'start'] - function calctable($pos,$max) { - - echo "START CALCTABLE $pos,$max\n"; + + function calctable($pos,$left,$right) { + // if ($pos ==332 ) unset($this->td); + //echo "START CALCTABLE $pos,$max\n"; // read the width if there is one? - - $attributes = $this->tokens[$pos][1]; + $tableid = $pos; + $attributes = $this->_tokens[$pos][1]; if (preg_match("/ width\=[\"\']?(\[0-9]+)([%]*)[\"\']?/mi",' '.$attributes,$args)) { - $max = $args[1]; - if ($args[2]) $max = (int) (0.01 * $args[1] * $max); + $right = $left + $args[1]; + if ($args[2]) $right = (int) (0.01 * $args[1] * ($right - $left)); } $pos++; - $rows = 0; - $rowmax = 600; - $cols =0; - $totalcols = 0; - $done =0; + + + $table = array(); // table[row][col] + $totalcols= 1; + $done = 0; + $col =1; + $row =0; $c = count($this->_tokens); while ($pos < $c) { if (!is_array($this->_tokens[$pos])) { @@ -606,26 +901,44 @@ } switch ($this->_tokens[$pos][0]) { case "TR": - $rows++; - $rowid = $pos; + $row++; + if ($col > $totalcols) $totalcols = $col-1; + $col = 1; break; case "TD"; - + + while (@isset($table[$row][$col]['pos'])) // find next empty col. + $col++; $args = array(); $span =1; - if (preg_match("/ colspan\=[\"\']?([0-9]+)[\"\']?/mi",' '.$this->_tokens[$pos][1],$args)) + $rowspan =1; + if (preg_match("/\scolspan\=[\"\']?([0-9]+)[\"\']?/mi",' '.@$this->_tokens[$pos][1],$args)) $span = $args[1]; - echo "SPAN $span\n"; - $td[$pos]['col'] = $cols; - $td[$pos]['row'] = $rowid; - $td[$pos]['span'] = $span; - $cols+=$span; + if (preg_match("/\srowspan\=[\"\']?([0-9]+)[\"\']?/mi",' '.@$this->_tokens[$pos][1],$args)) + $rowspan = $args[1]; + + $table[$row][$col]['pos'] = $pos; + $table[$row][$col]['span'] = $span; + $table[$row][$col]['rowspan'] = $rowspan; + + + for ($i=1;$i<$span;$i++) + $table[$row][$col+$i]['pos'] = $pos; + for ($i=1;$i<$rowspan;$i++) + for ($j=0;$j<$span;$j++) + $table[$row+$i][$col+$j]['pos'] = $pos; + + + $this->td[$pos]['row'] = $row; + $this->td[$pos]['col'] = $col; + + $this->td[$pos]['colspan'] = $span; + $this->td[$pos]['rowspan'] = $rowspan; + $this->td[$pos]['table'] = $tableid; + + $col += $span; break; case "/TR": - if ($cols > $totalcols) $totalcols = $cols; - //if ($rowsize > $rowmax) $rowmax= $rowsize; - $cols = 0; - //$rowsize = 0; break; case "TABLE": $pos = $this->_findSubTables($pos); // skip sub tables @@ -638,40 +951,75 @@ if ($done) break; $pos++; } - $endoftable = $pos; - echo "ENDTABLE $pos : W $totalcols\n"; + // I now have 2 arrays: $table[row][col][pos|span|rowspan] and $td[pos][col|row] + // and totalcols; + // do do a guess on the stuff... + + + + if (!$totalcols) return; - $colwidth = $max / $totalcols; - $x=$this->_left; + $colwidth = (int) (($right-$left) / $totalcols); + $x=$left; $row =0; - foreach ($td as $pos=>$array) { - if ($array['row'] != $row) $x=$this->_left; - $row = $array['row']; - - $this->td[$pos]['right'] = $x+($colwidth * $array['span']); - $this->td[$pos]['left'] = $x; - $this->td[$pos]['row'] = $array['row']; - $this->td[$pos]['span'] = $array['span']; - - echo "SET TD:$pos: L$x, R".($x+$colwidth). "\n"; + foreach ($table as $row=>$cols) { + $x = $left; + foreach($cols as $col=>$data) { + if (!isset($col_left[$col])) { + $col_left[$col] = $x; + } else { + $x=$col_left[$col] ; + } + + $td_id = $data['pos']; + // set left. if not set already + if (!isset($this->td[$td_id]['left'])) + $this->td[$td_id]['left'] = $col_left[$col]; + + + if (!isset($this->td[$td_id]['right'])) + $this->td[$td_id]['right'] = $col_left[$col]; + + $this->td[$td_id]['colwidth'] = $colwidth; + $this->td[$td_id]['totalcols'] = $totalcols; + if ($this->td[$td_id]['row'] == $row) + $this->td[$td_id]['right'] += $colwidth; + //echo "R{$row}:C:{$col}:{$this->td[$td_id]['left']},{$this->td[$td_id]['right']}\n"; + $x += $colwidth; + } - $x += ($colwidth * $array['span']); } - echo "END SETTABLE\n"; - return $endoftable; + //if ($tableid ==142 ) { + // print_r($this->td); + // exit; + // } + $this->tables[$tableid] =$table; + } + + function _TDcalcHeight($id) { + $h=0; + foreach ($this->td[$id]['lines'] as $lineid) + $h += $this->_lines[$lineid]['ascent'] + $this->_lines[$lineid]['decent']; + $this->td[$id]['height'] = $h; + } + function _TRcalcHeight($id) { + } + + } dl('php_gtk.so'); - -$t = new PEAR_Frontend_Gtk_WidgetHTML; -$t->test(); +error_reporting(E_ALL); +$t = new PEAR_Front_Gtk_WidgetHTML; +//$t->test(dirname(__FILE__).'/tests/test3.html'); +$t->test('http://pear.php.net/manual/en/packages.templates.it.php'); $t->tokenize(); $t->testInterface(); -$t->build(); +//$t->build(); gtk::main();
Index: pear/PEAR_Frontend_Gtk/Gtk/WidgetHTML.php diff -u pear/PEAR_Frontend_Gtk/Gtk/WidgetHTML.php:1.1 pear/PEAR_Frontend_Gtk/Gtk/WidgetHTML.php:1.2 --- pear/PEAR_Frontend_Gtk/Gtk/WidgetHTML.php:1.1 Tue Jun 4 06:14:40 2002 +++ pear/PEAR_Frontend_Gtk/Gtk/WidgetHTML.php Wed Jun 5 08:19:15 2002 @@ -2,19 +2,19 @@ -class PEAR_Frontend_Gtk_WidgetHTML { - +class PEAR_Front_Gtk_WidgetHTML { + var $pass=1; var $_source; // raw HTML - function test() { - $this->_source = implode('',file('http://pear.php.net/manual/en/packages.templates.it.php')); - $fh = fopen('/tmp/test','w'); fwrite($fh,$this->_source ); fclose($fh); + function test($file) { + $this->_source = implode('',file($file)); + //$fh = fopen('/tmp/test','w'); fwrite($fh,$this->_source ); fclose($fh); } var $_tokens = array(); // HTML Tokens - function tokenize() { + function tokenize() { $tok = strtok($this->_source,'<'); $a[0] = $tok; while( $tok !== FALSE ) { @@ -38,6 +38,7 @@ } if (strcspn($b," \n") == $l+1) { $this->_tokens[] = array(strtoupper(substr($b,0,strpos($b,'>')))); + $this->_tokens[] = substr($b,strpos($b,'>')+1) ; continue; } if (strcspn($b," \n") > strpos($b,'>')) { @@ -51,26 +52,44 @@ $this->_tokens[] = array($tag,$attribs); $this->_tokens[] = substr($b,strpos($b,'>')+1); } + //print_r($this->_tokens); + //exit; } - - function build() { - $this->widget->freeze(); + var $Start = FALSE; // start rering (eg. ignore headers) + + function build($startpos,$endpos) { + $this->layout->freeze(); $this->Start=FALSE; - foreach($this->_tokens as $pos=>$item) { + $this->_DrawingAreaClear(); + + $this->_line_y = array(); + $this->_y =10; + $this->_states = array(); + $this->states = array(); + $this->cur = array(); + $this->curid = array(); + + + $this->pushState(0); + for($pos = $startpos;$pos < $endpos;$pos++) { + $item = $this->_tokens[$pos]; if (is_Array($item)) { $method = "push"; if (!$item[0]) continue; + //echo $pos; //echo "\nIN:".serialize($item)."\n"; + $this->outputTAG($item[0]); if ($item[0]{0} == '/') { $method = "pop"; $item[0] = substr($item[0],1); $item[1] = '/'; } - + //if (!$draw) + + switch (trim($item[0])) { case '!DOCTYPE': case 'HTML': - case 'META': case 'LINK': case 'HEAD': @@ -96,48 +115,60 @@ case 'TABLE': // unhandled stauff if ($method == 'push') { // start + $this->pushState($pos); // move us down abit and start a new row $this->_x = $this->_left; - $this->_y +=16; - $this->calctable($pos,$this->_right- $this->_x); + if ($this->pass == 1) + $this->calctable($pos,$this->_x ,$this->_right); + $this->push($item[0],$pos,@$item[1]); //$this->output("TABLE:$pos"); - } else { //end + } else { // + $table = $this->pop('TABLE'); + $this->popState($table); $this->_x = $this->_left; - $this->_y +=16; + $this->_y +=16; + + + $this->clearStack('TD',$table); + $this->clearStack('BGCOLOR',$table); + $this->clearStack('FGCOLOR',$table); + $this->clearStack('TR',$table); + } + + break; //case 'TR': case 'TD': - if (!$this->td[$pos]) break; + if ($method == 'push') { // start - echo "TDPstart: $pos L:{$this->_left},{$this->_right}\n"; - $this->push('right',$this->td[$pos]['right']); - $this->push('left',$this->td[$pos]['left']); + if (!@$this->td[$pos]) break; + //echo "TDPstart: $pos L:{$this->_left},{$this->_right}\n"; + $this->_left = $this->td[$pos]['left']; $this->_right = $this->td[$pos]['right']; - $row = $this->td[$pos]['row']; - if (!$this->tr[$row]) - $this->tr[$row] = $this->_y+16; + + + $this->td[$pos]['height'] = 0; + if (@isset($this->td[$pos]['topline'])) { + $this->_y = $this->td[$pos]['topline']; + } else { + $this->_y+16; + } $this->_x = $this->_left; - - $this->_y =$this->tr[$row] ; - //$this->output("TD:$pos"); - //echo "TDstart: $pos L:{$this->_left},{$this->_right}\n"; + $this->push($item[0],$pos,@$item[1]); } else { // pull back! - $this->pop('right'); - $this->pop('left'); - $this->_left = 10; - if ($s = $this->in('left')) - $this->_left = $s; - $this->_right = 600; - if ($r = $this->in('right')) - $this->_right = $r; - echo "endtd: $pos L:{$this->_left},{$this->_right}\n"; + $td = $this->pop('TD'); + + $this->clearStack('TD',$td); + $this->clearStack('BGCOLOR',$td); + $this->clearStack('FGCOLOR',$td); + $this->clearStack('TR',$td); } - + break; @@ -145,8 +176,9 @@ $this->Start = TRUE; break; case 'PRE': + $this->output($item[0]); $this->linebr($item[0]); - $this->$method($item[0]); + $this->$method($item[0],$pos); case 'SELECT': // hide this stuff case 'TEXTAREA': @@ -156,7 +188,7 @@ case 'CODE': case 'B': case 'I': - $this->$method($item[0]); + $this->$method($item[0],$pos); break; case 'SMALL': @@ -167,17 +199,20 @@ case 'H4': case 'H5': case 'H6': + //$this->output($item[0]); $this->linebr(); - $this->$method('H',$item[0]{1}); + $this->$method('H',$pos,$item[0]{1}); break; - case 'DIV': // ?? this stuf could contain formating? - case 'FONT': + case 'DIV': // ?? this stuf could contain formating? + case 'FONT': + case 'TR': case 'A': - $this->$method($item[0],@$item[1]); + $this->$method($item[0],$pos,@$item[1]); break; - //case 'TR': + case 'BR': - case 'P': + case 'P': + //$this->output($item[0]); $this->linebr($item[0]); break; default: @@ -187,17 +222,42 @@ continue; } + //echo $pos; // strings only! $this->output($item); } $this->linebr(); - $this->widget->set_size(600, $this->_y+16); - $this->widget->thaw(); + $this->_area_y = $this->_y+16; + $this->layout->set_size($this->_area_x , $this->_area_y ); + $this->layout->thaw(); + //return; + //print_r($this->td); + if ($this->pass == 1) { + $this->pass++; + $this->build($startpos,$endpos); + } + } - var $check = "xxxx"; + var $check = ""; - function push($what,$attributes='') { + var $_states = array(); // array of pos'id => various data!!! + function pushState($id) { + foreach(array('_left','_right') as $k) + $this->_states[$id][$k] = $this->$k; + + } + + function popState($id) { + foreach(array('_left','_right') as $k) + $this->$k = $this->_states[$id][$k]; + + } + var $stack;// array of item stack + var $cur; // top of stack + var $curid; // id of top of stack + + function push($what,$pos,$attributes='') { //echo "PUSH: $what, $attributes\n"; if ($attributes && $attributes{strlen($attributes)-1} == '/') { //echo "SKIP"; @@ -206,79 +266,106 @@ if (!@$this->stack[$what]) $this->stack[$what] = array(); if (!$attributes) $attributes = ":"; - $this->stack[$what][] = $attributes; + $this->stack[$what][] = array($pos,$attributes); $this->cur[$what] = $attributes; - $this->_stackAttributes($attributes,'push'); + $this->curid[$what] = $pos; + $this->_stackAttributes($attributes,$pos,'push'); - if ($what != $this->check) return; - echo "\nPUSH:$what:".serialize($this->stack[$what]); - echo "\nCUR:{$this->cur[$what]}"; + if ($this->check && preg_match("/^(".$this->check.")$/",$what)) { + echo "\nPUSH:$what:".serialize($this->stack[$what]); + echo "\nCUR:{$this->cur[$what]}"; + } } - function pop($what,$attributes=array()) { + function pop($what,$pos=0,$attributes=array()) { if (!@$this->stack[$what]) return; - $remove = array_pop($this->stack[$what]); + list($id,$remove) = array_pop($this->stack[$what]); $this->cur[$what] = ""; if ($this->stack[$what]) - $this->cur[$what] = $this->stack[$what][count($this->stack[$what])-1]; - $this->_stackAttributes($remove,'pop'); - if ($what != $this->check) return; - echo "\nPOP:$what:".serialize($this->stack[$what]); - echo "\nCUR:{$this->cur[$what]}"; + list($this->curid[$what],$this->cur[$what]) = $this->stack[$what][count($this->stack[$what])-1]; + $this->_stackAttributes($remove,$pos,'pop'); + /* debugging*/ + if ($this->check && preg_match("/^(".$this->check.")$/",$what)) { + echo "\nPOP:$what:".serialize($this->stack[$what]); + echo "\nCUR:{$this->cur[$what]}"; + } + return $id; + } + + function clearStack($what,$pos) { + //echo "CLEARING STACK OF $what for $pos\n "; + if (!@$this->stack[$what]) return; + $c = count(@$this->stack[$what]) -1; + for($i=$c;$i>-1;$i--) { + list($id,$attr) = $this->stack[$what][$i]; + if ($id >= $pos) $this->pop($what); + } } - function _stackAttributes($attributes,$method) { + + + function _stackAttributes($attributes,$pos,$method) { if ($attributes == ":") return; if ($attributes == '/') return; if ($attributes{0} == "#") return; $args = array(); - if (preg_match("/ color\=[\"\']?(\#[0-9A-F]+)[\"\']?/mi",' '.$attributes,$args)) - $this->$method("FGCOLOR",$args[1]); + if (preg_match("/\scolor\=[\"\']?(\#[0-9A-F]+)[\"\']?/mi",' '.$attributes,$args)) + $this->$method("FGCOLOR",$pos,$args[1]); $args = array(); - if (preg_match("/ bgcolor\=[\"\']?(\#[0-9A-F]+)[\"\']?/i",' '.$attributes,$args)) - $this->$method("BGCOLOR",$args[1]); + if (preg_match("/\sbgcolor\=[\"\']?(\#[0-9A-F]+)[\"\']?/mi",' '.$attributes,$args)) + $this->$method("BGCOLOR",$pos,$args[1]); $args = array(); - if (preg_match("/ text\=[\"\']?(\#[0-9A-F]+)[\"\']?/i",' '.$attributes,$args)) - $this->$method("FGCOLOR",$args[1]); - + if (preg_match("/\stext\=[\"\']?(\#[0-9A-F]+)[\"\']?/mi",' '.$attributes,$args)) + $this->$method("FGCOLOR",$pos,$args[1]); + + if (preg_match("/\sclass\=[\"\']?([A-Z]+)[\"\']?/mi",' '.$attributes,$args)) + $this->_stackStyle($method,$pos,$args[1]); + } + function _stackStyle($method,$pos,$class) { + return; + /* TODO?? */ + switch ($class) { + case 'programlisting': + $this->$method("BGCOLOR",''); + } + + } + + function in($what) { return @$this->cur[$what]; } - + function inID($what) { + return @$this->curid[$what]; + } function linebr($item='') { if (($item == "P") && ($this->lastbr == "P")) return; //if ($item && $this->lastbr && ($this->lastbr != $item)) return; //$this->widget->insert($this->_font,$this->_fgcolor,$this->bg_color,":$item:\n"); - - $this->_y += 14; + $this->_nextLine(); $this->_x = $this->_left; } var $buffer=""; - var $_fonts = array(); // associative array of fonts - var $_colors = array(); // associative array of colors - - var $_font = NULL; - var $_fgcolor = NULL; - var $_bgcolor = NULL; + var $_left = 10; var $_right = 600; var $_x = 10; - var $_y = 0; + var $_y = 20; function output($string) { if (!$this->Start) return; @@ -293,7 +380,7 @@ $this->_makeFont(); $this->_makeColors(); - $this->_setStyle(); + //$this->_setStyle(); if ($this->in('PRE')) { $this->outputPRE($string); return; @@ -301,25 +388,64 @@ $this->outputTEXT($string); } - + + function outputTAG($tag) { + return; + $this->_makeFont('-adobe-helvetica-bold-r-normal-*-*-60-*-*-p-*-iso8859-1'); + $this->_makeColors("#000000","#FFFF00",FALSE); + //$x= $this->_x; + //$y= $this->_y; + $this->outputTEXT("<{$tag}>",TRUE); + //$this->_x = $x; + //$this->_y = $y; + } + + var $line =0; + + function outputTEXT($string) { $array = $this->_breakString($this->_x,$this->_left,$this->_right,$string); // array of lines (startpos,len,text) - - foreach($array as $line) { + $h = $this->_font->ascent + $this->_font->descent; + $c = count($array) -1; + foreach($array as $i=>$line) { + if ($line[2]) { - $widget = &new GtkLabel($line[2]); - $widget->set_style($this->_style); - //echo "ADD: {$line[0]},{$this->_y} : {$line[2]}\n"; - $this->widget->put($widget,$line[0],$this->_y); - $widget->show(); + //$widget = &new GtkLabel($line[2]); + //$widget->set_style($this->_style); + if ($this->pass == 2) + //echo "ADD: {$line[0]},{$this->_y} : {$line[2]}\n"; + //$this->layout->put($widget,$line[0],$this->_y); + + gdk::draw_rectangle($this->pixmap, + $this->_bggc,true, + $line[0], $this->_y-$this->_font->ascent, + $line[1], $h + ); + + gdk::draw_text($this->pixmap, + $this->_font, + $this->_gc , + $line[0], + $this->_y, + $line[2], strlen($line[2]) + ); + + $this->drawing_area->draw( + new GdkRectangle( + $line[0], $this->_y-$this->_font->ascent, + $line[1], $h)); + //$widget->show(); + if ($c != $i) + $this->_nextLine(); } - $this->_y += 14; + + } - $this->_y -= 14; + $this->_x = $line[0] + $line[1]; $this->lastbr = ''; } @@ -338,11 +464,12 @@ foreach($array as $line) { $this->outputTEXT($line); - $this->_y += 14; + $this->_nextLine(); $this->_x = $this->_left; + } - $this->_y -= 14; + $this->_line--; $this->lastbr = ''; } @@ -399,12 +526,54 @@ } + function unhtmlentities ($string) { + $trans_tbl = get_html_translation_table (HTML_ENTITIES); + $trans_tbl = array_flip ($trans_tbl); + $ret = strtr ($string, $trans_tbl); + return preg_replace('/\&\#([0-9]+)\;/me',"chr('\\1')",$ret); + } + var $_line; + function _nextLine() { + // store the line height of the current line.. + + if (@$this->_lines[$this->_line]['ascent'] < $this->_font->ascent ) + $this->_lines[$this->_line]['ascent'] = $this->_font->ascent; + if (@$this->_lines[$this->_line]['decent'] < $this->_font->decent ) + $this->_lines[$this->_line]['decent'] = $this->_font->decent; + $this->_lines[$this->_line]['height'] = + @$this->_lines[$this->_line]['decent'] + @$this->_lines[$this->_line]['ascent']; + + // store the active block heights.... + + if ($id = $this->inID('TD')) + $this->td[$id]['lines'][] = $this->_line; + + $this->_line++; + + if (@$this->_line_y[$this->_line]) { + $this->_y = $this->_line_y[$this->_line]; + + } else if (@$this->_lines[$this->_line]['height']) { + $this->_y += $this->_lines[$this->_line]['height']; + + } else { + $this->_y += 16; + } + $this->_line_y[$this->_line] =$this->_y;; + // + + + } + /*------------------------------FONTS AND COLORS --------------------------*/ - function _makeFont() { + var $_fonts = array(); // associative array of fonts + var $_font = NULL; + function _makeFont($default='') { + $font['pointsize'] = 100; $font['space'] = 'p'; // or 'm' for monospaced $font['family'] = 'times'; // or helvetica, courier @@ -439,6 +608,9 @@ $fontname = $this->_getFontString($font); + //echo $fontname; + if ($default) $fontname = $default; + if (@!$this->_fonts[$fontname]) $this->_fonts[$fontname] = gdk::font_load($fontname); if (!$this->_fonts[$fontname]) @@ -447,7 +619,6 @@ $this->_font = &$this->_fonts[$fontname]; } - function _getFontString($array) { $ret = ""; foreach(array( @@ -455,54 +626,60 @@ 'addedstyle', 'pixelsize','pointsize','resx','resy', 'space','averagewidth','registry','encoding') as $i) { $a = '*'; - if ($array[$i]) $a = $array[$i]; + if (@$array[$i]) $a = $array[$i]; $ret .= "-{$a}"; } return $ret; } + + var $_gcs = array(); // associative array fo fgcolor:bgcolor to GC object + var $_gc = NULL; // link to current GtkGC - function _makeColors() { - $fgcolor = "#000000"; - $bgcolor = "#FFFFFF"; - - if ($c = $this->in('FGCOLOR')) - $fgcolor = $c; - if ($c = $this->in('BGCOLOR')) - $bgcolor = $c; - + var $_colors = array(); // associative array of colors + var $_fgcolor = NULL;// link to current GdkColor + var $_bgcolor = NULL;// link to current GdkColor + + function _makeColors($fgcolor = "#000000",$bgcolor = "#FFFFFF",$read=TRUE) { + if ($read) { + if ($c = $this->in('FGCOLOR')) + $fgcolor = $c; + if ($c = $this->in('BGCOLOR')) + $bgcolor = $c; + } //echo "$fgcolor:$bgcolor\n"; - if (!$this->_colors[$fgcolor]) + if (!@$this->_colors[$fgcolor]) $this->_colors[$fgcolor] = &new GdkColor($fgcolor); - if (!$this->_colors[$bgcolor]) + if (!@$this->_colors[$bgcolor]) $this->_colors[$bgcolor] = &new GdkColor($bgcolor); $this->_fgcolor = &$this->_colors[$fgcolor]; $this->_bgcolor = &$this->_colors[$bgcolor]; + /* GC STUFF */ + + + $id = $fgcolor . $bgcolor; + + if (!@$this->_gcs[$id]) { + $window = $this->drawing_area->window; + $this->_gcs[$id] = $window->new_gc(); + $cmap = $this->drawing_area->get_colormap(); + $this->_gcs[$id]->foreground = $cmap->alloc($fgcolor); + $this->_gcs[$id]->background = $cmap->alloc($bgcolor); + } + /* REVERSED - for background rectangle*/ + $bgid = $bgcolor . $fgcolor; + if (!@$this->_gcs[$bgid]) { + $this->_gcs[$bgid] = $window->new_gc(); + $this->_gcs[$bgid]->foreground = $this->_gcs[$id]->background ; + $this->_gcs[$bgid]->background = $this->_gcs[$id]->foreground; + } + $this->_gc = &$this->_gcs[$id]; + $this->_bggc = &$this->_gcs[$bgid]; + } - - - function unhtmlentities ($string) { - $trans_tbl = get_html_translation_table (HTML_ENTITIES); - $trans_tbl = array_flip ($trans_tbl); - $ret = strtr ($string, $trans_tbl); - return preg_replace('/\&\#([0-9]+)\;/me',"chr('\\1')",$ret); - } - - function testInterface() { - $w = &new GtkWindow; - $s = &new GtkScrolledWindow(); - $hadj = $s->get_hadjustment(); - $vadj = $s->get_vadjustment(); - $this->widget = &new GtkLayout($hadj,$vadj); - $this->widget->set_size(600, 12000); - $s->add($this->widget); - $w->add($s); - $w->show_all(); - - } var $_style = NULL; function _setStyle() { //echo "SET: $widgetname: $fgcolor/$bgcolor ". ((int) $copy) . "\n"; @@ -532,10 +709,125 @@ $this->_style = &$style; } + + + + + + + /* ------------------------------ BASIC WIDGET STUFF ------------------*/ + var $_area_x= 600; + var $_area_y= 10000; + + var $layout; // GtkLayout + + function testInterface() { + $w = &new GtkWindow; + $s = &new GtkScrolledWindow(); + $hadj = $s->get_hadjustment(); + $vadj = $s->get_vadjustment(); + $this->layout = &new GtkLayout($hadj,$vadj); + $this->layout->set_size($this->_area_x,$this->_area_y); + $this->_DrawingArea(); + $s->add($this->layout); + $w->add($s); + $w->show_all(); + + } + + + var $drawing_area; // GtkDrawingArea + function _DrawingArea() { // Drawing Area + //echo "PAINTER"; + $this->drawing_area = &new GtkDrawingArea(); + $this->drawing_area->size($this->_area_x,$this->_area_y); + $this->drawing_area->set_events( + GDK_EXPOSURE_MASK + | GDK_LEAVE_NOTIFY_MASK + | GDK_BUTTON_PRESS_MASK + | GDK_BUTTON_RELEASE_MASK + // | GDK_BUTTON_MOTION_MASK + | GDK_POINTER_MOTION_MASK + | GDK_KEY_PRESS_MASK + | GDK_POINTER_MOTION_HINT_MASK + ); + + //$this->drawing_area->set_events( GDK_ALL_EVENTS_MASK ); + // $this->layout->add_events( GDK_KEY_PRESS_MASK ); + + $this->drawing_area->set_flags( GTK_CAN_FOCUS); + + + + $this->drawing_area->connect("configure_event", array(&$this,"_DrawingAreaCallbackConfigure")); + $this->drawing_area->connect("expose_event", array(&$this,"_DrawingAreaCallbackExpose")); + //$this->drawing_area->connect("key_press_event", array(&$this,"_DrawingAreaCallbackKeyPress")); + //$this->drawing_area->connect("button_release_event", array(&$this,"_DrawingAreaCallbackBtnPress")); + //$this->drawing_area->connect("button_press_event", array(&$this,"_DrawingAreacallbackBtnPress")); + //$this->html->drawing_area->connect_after("motion_notify_event", array(&$this,"callback_pointer_motion_event")); + //$this->html->drawing_area->connect("expose_event", + // array(&$this,"callback_expose_event")); + $this->drawing_area->show(); + + + $this->layout->put($this->drawing_area,0,0); + + } + + var $pixmap; + + function _DrawingAreaCallbackConfigure($widget, $event) { + if (@$this->pixmap) return; + + $this->pixmap = &new GdkPixmap($widget->window, + $this->_area_x,$this->_area_y, + -1); + + $this->_DrawingAreaClear(); + // draw somethin on it. + + //$this->drawing_area->realize(); + + + if (!$this->Start) { + $this->build(0,count($this->_tokens),1); + //$this->build(FALSE); + } + + return true; + } + + function _DrawingAreaClear() { + gdk::draw_rectangle($this->pixmap, + $this->drawing_area->style->white_gc, + true, 0, 0, + $this->_area_x,$this->_area_y); + // draw somethin on it. + $this->drawing_area->realize(); + + } + + function _DrawingAreaCallbackExpose($widget,$event) { + + gdk::draw_pixmap($this->drawing_area->window, + $widget->style->fg_gc[$widget->state], + $this->pixmap, + $event->area->x, $event->area->y, + $event->area->x, $event->area->y, + $event->area->width, $event->area->height); + + } + + + + /* ------------------------------ TABLE STUFF ------------------*/ + + + /* @@ -581,23 +873,26 @@ /* first version just divides available area! */ var $td; // associative array of [posid]['width'|'start'] - function calctable($pos,$max) { - - echo "START CALCTABLE $pos,$max\n"; + + function calctable($pos,$left,$right) { + // if ($pos ==332 ) unset($this->td); + //echo "START CALCTABLE $pos,$max\n"; // read the width if there is one? - - $attributes = $this->tokens[$pos][1]; + $tableid = $pos; + $attributes = $this->_tokens[$pos][1]; if (preg_match("/ width\=[\"\']?(\[0-9]+)([%]*)[\"\']?/mi",' '.$attributes,$args)) { - $max = $args[1]; - if ($args[2]) $max = (int) (0.01 * $args[1] * $max); + $right = $left + $args[1]; + if ($args[2]) $right = (int) (0.01 * $args[1] * ($right - $left)); } $pos++; - $rows = 0; - $rowmax = 600; - $cols =0; - $totalcols = 0; - $done =0; + + + $table = array(); // table[row][col] + $totalcols= 1; + $done = 0; + $col =1; + $row =0; $c = count($this->_tokens); while ($pos < $c) { if (!is_array($this->_tokens[$pos])) { @@ -606,26 +901,44 @@ } switch ($this->_tokens[$pos][0]) { case "TR": - $rows++; - $rowid = $pos; + $row++; + if ($col > $totalcols) $totalcols = $col-1; + $col = 1; break; case "TD"; - + + while (@isset($table[$row][$col]['pos'])) // find next empty col. + $col++; $args = array(); $span =1; - if (preg_match("/ colspan\=[\"\']?([0-9]+)[\"\']?/mi",' '.$this->_tokens[$pos][1],$args)) + $rowspan =1; + if (preg_match("/\scolspan\=[\"\']?([0-9]+)[\"\']?/mi",' '.@$this->_tokens[$pos][1],$args)) $span = $args[1]; - echo "SPAN $span\n"; - $td[$pos]['col'] = $cols; - $td[$pos]['row'] = $rowid; - $td[$pos]['span'] = $span; - $cols+=$span; + if (preg_match("/\srowspan\=[\"\']?([0-9]+)[\"\']?/mi",' '.@$this->_tokens[$pos][1],$args)) + $rowspan = $args[1]; + + $table[$row][$col]['pos'] = $pos; + $table[$row][$col]['span'] = $span; + $table[$row][$col]['rowspan'] = $rowspan; + + + for ($i=1;$i<$span;$i++) + $table[$row][$col+$i]['pos'] = $pos; + for ($i=1;$i<$rowspan;$i++) + for ($j=0;$j<$span;$j++) + $table[$row+$i][$col+$j]['pos'] = $pos; + + + $this->td[$pos]['row'] = $row; + $this->td[$pos]['col'] = $col; + + $this->td[$pos]['colspan'] = $span; + $this->td[$pos]['rowspan'] = $rowspan; + $this->td[$pos]['table'] = $tableid; + + $col += $span; break; case "/TR": - if ($cols > $totalcols) $totalcols = $cols; - //if ($rowsize > $rowmax) $rowmax= $rowsize; - $cols = 0; - //$rowsize = 0; break; case "TABLE": $pos = $this->_findSubTables($pos); // skip sub tables @@ -638,40 +951,75 @@ if ($done) break; $pos++; } - $endoftable = $pos; - echo "ENDTABLE $pos : W $totalcols\n"; + // I now have 2 arrays: $table[row][col][pos|span|rowspan] and $td[pos][col|row] + // and totalcols; + // do do a guess on the stuff... + + + + if (!$totalcols) return; - $colwidth = $max / $totalcols; - $x=$this->_left; + $colwidth = (int) (($right-$left) / $totalcols); + $x=$left; $row =0; - foreach ($td as $pos=>$array) { - if ($array['row'] != $row) $x=$this->_left; - $row = $array['row']; - - $this->td[$pos]['right'] = $x+($colwidth * $array['span']); - $this->td[$pos]['left'] = $x; - $this->td[$pos]['row'] = $array['row']; - $this->td[$pos]['span'] = $array['span']; - - echo "SET TD:$pos: L$x, R".($x+$colwidth). "\n"; + foreach ($table as $row=>$cols) { + $x = $left; + foreach($cols as $col=>$data) { + if (!isset($col_left[$col])) { + $col_left[$col] = $x; + } else { + $x=$col_left[$col] ; + } + + $td_id = $data['pos']; + // set left. if not set already + if (!isset($this->td[$td_id]['left'])) + $this->td[$td_id]['left'] = $col_left[$col]; + + + if (!isset($this->td[$td_id]['right'])) + $this->td[$td_id]['right'] = $col_left[$col]; + + $this->td[$td_id]['colwidth'] = $colwidth; + $this->td[$td_id]['totalcols'] = $totalcols; + if ($this->td[$td_id]['row'] == $row) + $this->td[$td_id]['right'] += $colwidth; + //echo "R{$row}:C:{$col}:{$this->td[$td_id]['left']},{$this->td[$td_id]['right']}\n"; + $x += $colwidth; + } - $x += ($colwidth * $array['span']); } - echo "END SETTABLE\n"; - return $endoftable; + //if ($tableid ==142 ) { + // print_r($this->td); + // exit; + // } + $this->tables[$tableid] =$table; + } + + function _TDcalcHeight($id) { + $h=0; + foreach ($this->td[$id]['lines'] as $lineid) + $h += $this->_lines[$lineid]['ascent'] + $this->_lines[$lineid]['decent']; + $this->td[$id]['height'] = $h; + } + function _TRcalcHeight($id) { + } + + } dl('php_gtk.so'); - -$t = new PEAR_Frontend_Gtk_WidgetHTML; -$t->test(); +error_reporting(E_ALL); +$t = new PEAR_Front_Gtk_WidgetHTML; +//$t->test(dirname(__FILE__).'/tests/test3.html'); +$t->test('http://pear.php.net/manual/en/packages.templates.it.php'); $t->tokenize(); $t->testInterface(); -$t->build(); +//$t->build(); gtk::main();