All pastes #269384 Raw Edit

mq

public php v1 · immutable
·published 2006-12-06 15:25 UTC
rendered paste body
<?php class Graph_Core extends GtkDrawingArea {  # prefix class attributes with "_" to avoid collisions  # with GtkDrawingArea attributes.  protected $_pixmap;  protected $_gc;  protected $_realized;   function __construct() {    parent::__construct();     $this->connect('expose_event',      array($this, 'expose_event'));    $this->connect('configure_event',      array($this, 'configure_event'));    $this->connect('realize',      array($this, 'realize'));    $this->set_events(Gdk::EXPOSURE_MASK      | Gdk::LEAVE_NOTIFY_MASK);     $this->_pixmap = null;    $this->_gc = null;    $this->_realized = false;  }   function realize() {    $this->_realized = true;  }   function realized() {    return $this->_realized;  }   function configure_event($widget, $event) {    $w = $this->width();    $h = $this->height();     # allocate a new pixmap of the requested size ($this->allocation)    $this->_pixmap = new GdkPixmap($this->window, $w, $h, -1);     # clear pixmap (white color)    $this->_pixmap->draw_rectangle($this->style->white_gc, true,      0, 0, $w, $h);     return true;  }   function expose_event($widget, $event) {    $this->window->draw_drawable($this->style->fg_gc[$this->state],      $this->pixmap(),      $event->area->x, $event->area->y,      $event->area->x, $event->area->y,      $event->area->width, $event->area->height    );    return false;  }   function set_color($color) {    $col = $this->colormap()->alloc_color($color);    if ($col === false)      return $this->foreground('black');    return $this->gc()->set_foreground($col);  }   function colormap() {    return $this->gc()->get_colormap();  }   function pixmap() {    if (!$this->realized()) {      trigger_error("Widget not realized ; can't get pixmap");      return null;    }    return $this->_pixmap;  }   function gc() {    if (!$this->realized()) {      trigger_error("Widget not realized ; can't get GC");      return null;    }    if ($this->_gc == null)      $this->_gc = new GdkGc($this->pixmap());    return $this->_gc;  }   function width() {    return $this->allocation->width;  }   function height() {    return $this->allocation->height;  }   function w() {    return $this->width();  }   function h() {    return $this->height();  }}  class Grapher extends Graph_Core{         function draw_line($x1, $y1, $x2, $y2){                $this->pixmap()->draw_line($this->gc(), $x1, $y1, $x2, $y2);                $w = $this->w();                $h = $this->h();                $this->queue_draw_area(0,0, $w, $h);        }         function refresh(){                $w = $this->w();                $h = $this->h();                $this->queue_draw_area(0,0, $w, $h);        }         function configure_event($widget, $event) {                parent::configure_event($widget, $event);                 $this->redraw($this);        }         function redraw(){                 if(!$this->realized())                        return;                 $graph = $this;                echo "graph draw - start\n";                $this->set_color('blue');                $lines = file_get_contents('world.xy');                $lines = unserialize($lines);                 # look at optimisations here                $pixmap = $this->pixmap();                $gc = $this->gc();                $w = $this->w() / 2;                $h = $this->h();                 $scale = 8;                 $count = count($lines);                 echo "count = $count\n";                foreach ($lines as $line) {                        $pixmap->draw_line($gc, $w-($line[1]*$scale), $h-($line[0]*$scale), $w-($line[3]*$scale), $h-($line[2]*$scale));                }                 $this->refresh();                 echo "graph draw - done\n";        }} $grapher = new Grapher();  // note 1$grapher->set_size_request(800, 600);Gtk::timeout_add(100, array($grapher, 'redraw')); $window = new GtkWindow();$window->connect_simple('destroy', array('gtk', 'main_quit'));$window->set_title('Graph demo');$window->set_position(Gtk::WIN_POS_CENTER);$window->set_border_width(8);$window->add($grapher);$window->show_all(); Gtk::main();?>