All pastes #509500 Raw Edit

Marc Quinton

public php v1 · immutable
·published 2007-05-25 14:18 UTC
rendered paste body
<?phperror_reporting(E_ALL);/** * author : Marc Quinton / may 2007. * licence : LGPL. * * some classes to make php-gtk work as glade managing widget selection * */class EventBox extends GtkEventBox{	protected $widget;	protected $_name;	protected $gc;	public function __construct($widget=null, $name=''){		parent::__construct();		$this->widget = $widget;		$this->_name = $name;		$this->gc = null;		if($widget != null)			$this->add($this->widget);		$this->show_all();	}	public function draw_handle(){				if($this->gc == null){			$this->gc = new GdkGC($this->window);			$this->gc->set_rgb_fg_color(new GdkColor(0,0,0));		}		$x = 0;		$y = 0;		$w = $this->allocation->width -1;		$h = $this->allocation->height -1;		$this->window->draw_rectangle($this->gc, $filled=false, $x, $y, $w, $h);		$x = 0;		$y = 0;		$w = 10;		$h = 10;		$this->window->draw_rectangle($this->gc, $filled=true, $x, $y, $w, $h);		$x = $this->allocation->width - $w;		$y = 0;		$this->window->draw_rectangle($this->gc, $filled=true, $x, $y, $w, $h);		$x = $this->allocation->width - $w;		$y = $this->allocation->height - $w;		$this->window->draw_rectangle($this->gc, $filled=true, $x, $y, $w, $h);		$x = 0;		$y = $this->allocation->height - $w;		$this->window->draw_rectangle($this->gc, $filled=true, $x, $y, $w, $h);		return true;	}	public function get_widget(){		return $this->widget;	}}class HBox extends EventBox{	protected $num_childs;	public function __construct($childs = 3){		parent::__construct();		$this->num_childs = $childs;		$this->widget = new Hbox();		$this->add($this->widget);		$this->show_all();	}}class Manager extends GtkWindow{	protected $widgets;	protected $selected_widget;	public function __construct(){		parent::__construct();		$this->connect_after('expose-event', array($this, 'on_expose'));		$this->widgets = array();		$this->selected = null;	}	public function manage_reparent($widget){		$this->widgets[] = $box = new EventBox();		$this->do_reparent($widget, $box);		if(is_a($widget, 'GtkLabel')){			# register button clicks			$box->add_events(Gdk::BUTTON_PRESS_MASK);			$box->connect_simple('button-press-event', array($this, 'on_button_press'), $box);		}		elseif(is_a($widget, 'GtkButton')){			$widget->connect_simple('clicked', array($this, 'on_button_click'), $box);			$widget->add_events(Gdk::LEAVE_NOTIFY_MASK|Gdk::ENTER_NOTIFY_MASK);			$widget->connect_simple_after('leave-notify-event', array($this, 'on_button_enter_leave'), $box);			$widget->connect_simple_after('enter-notify-event', array($this, 'on_button_enter_leave'), $box);		}		elseif(is_a($widget, 'GtkVbox')|| is_a($widget, 'GtkFrame')){			echo "widget composite\n";			$box->add_events(Gdk::BUTTON_PRESS_MASK);			$box->connect_simple_after('button-press-event', array($this, 'on_button_press'), $box);		}	}	public function do_reparent($widget, $box){		# reparent $widget		$parent = $widget->get_parent();		if(is_a($parent, 'GtkBox')){			list($expand, $fill, $padding, $pack_type) = $packing_info = $parent->query_child_packing($widget);			if($pack_type == Gtk::PACK_START)				$parent->pack_start($box, $expand, $fill, $padding);			else				$parent->pack_end($box, $expand, $fill, $padding);			$widget->reparent($box);		} else {			$widget->reparent($box);			$parent->add($box);		}	}	public function manage($event_box){		if(!is_a($event_box, 'EventBox')){			trigger_error('widget should be an EventBox class');			return false;		}		$this->widgets[] = $event_box;		if(is_a($event_box->get_widget(), 'GtkLabel')){			# register button clicks			$event_box->add_events(Gdk::BUTTON_PRESS_MASK);			$event_box->connect_simple('button-press-event', array($this, 'on_button_press'), $event_box);		}		elseif(is_a($event_box->get_widget(), 'GtkButton')){			$event_box->get_widget()->connect_simple('clicked', array($this, 'on_button_click'), $event_box);			$event_box->add_events(Gdk::LEAVE_NOTIFY_MASK|Gdk::ENTER_NOTIFY_MASK);			$event_box->connect_simple_after('leave-notify-event', array($this, 'on_button_enter_leave'), $event_box);			$event_box->connect_simple_after('enter-notify-event', array($this, 'on_button_enter_leave'), $event_box);		}		elseif(is_a($event_box->get_widget(), 'GtkEntry')){			echo "manage gtk-entry\n";			$event_box->get_widget()->add_events(Gdk::BUTTON_PRESS_MASK);			$event_box->get_widget()->connect_after('button-press-event', array($this, 'on_entry_button_press'), $event_box);		}	}	public function on_expose(){		if($this->selected_widget != null)			$this->show_selection();	}	public function on_button_click($widget){		$this->selected_widget = $widget;		$this->queue_draw();	}	public function on_button_press($widget){		$this->selected_widget = $widget;		$this->queue_draw();	}	public function on_button_enter_leave($widget){		$this->show_selection();	}	protected function show_selection(){		if($this->selected_widget != null){			# $this->selected_widget->draw_handle();			Gtk::timeout_add(3, array($this, 'on_timeout_show_selection'));		}	}	public function on_timeout_show_selection(){		$this->selected_widget->draw_handle();		return false;	}}$manager = new Manager();$manager->connect_simple('destroy', array('Gtk', 'main_quit'));$manager->set_position(Gtk::WIN_POS_CENTER); # place window to screen center$vbox = new GtkVbox();$manager->add($vbox);$hbox = new GtkHbox();$vbox->pack_start($hbox, false, false);# $hbox->set_border_width(2);# $manager->manage($hbox);/*$hbox->pack_start($w = new EventBox($w2 = new GtkLabel('a label'))); 	$manager->manage($w);$hbox->pack_start($w = new EventBox($w2 = new GtkButton('OK'))); 	$manager->manage($w);$hbox->pack_start($w = new EventBox($w2 = new GtkLabel('other label'))); 	$manager->manage($w);$hbox->pack_start($w = new EventBox($w2 = new GtkButton('OK2'))); 	$manager->manage($w);$hbox->pack_start($w = new EventBox($w2 = new GtkEntry('text-entry'))); 	$manager->manage($w);$table = new GtkTable();	$vbox->pack_start($table, false, false);	$table->set_row_spacings(10);	$table->set_col_spacings(10);	$manager->manage($table);	$x = 0 ; $y = 0;	$table->attach(		$w = new EventBox($w2 = new GtkLabel('label1')),		$x, $x+1, $y, $y+1	);	$manager->manage($w);	$table->attach(		$w = new EventBox($w2 = new GtkButton('xxx')),		$x+1, $x+2, $y, $y+1	);	$manager->manage($w);	$y++;	$table->attach(		$w = new EventBox($w2 = new GtkLabel('label2')),		$x, $x+1, $y, $y+1	);	$manager->manage($w);	$table->attach(		$w = new EventBox($w2 = new GtkButton('xxx')),		$x+1, $x+2, $y, $y+1	);	$manager->manage($w);*/$hbox->pack_start($w = new GtkLabel('a label')); 	$manager->manage_reparent($w);$hbox->pack_start($w = new GtkButton('OK')); 	$manager->manage_reparent($w);$hbox->pack_start($vbox2 = new GtkVbox(true)); 	# $manager->manage_reparent($vbox2);		$vbox2->pack_start($w = new GtkButton('1')); $manager->manage_reparent($w);		$vbox2->pack_start($w = new GtkButton('2')); $manager->manage_reparent($w);		$vbox2->pack_end($w = new GtkFrame('frame')); $manager->manage_reparent($w);			$w->add($w2 = new GtkButton('4')); $manager->manage_reparent($w2);		$vbox2->pack_start($w = new GtkButton('3')); $manager->manage_reparent($w);		$vbox2->pack_start($w = new GtkVbox); $manager->manage_reparent($w);/*$hbox->pack_start($w = new GtkLabel('second label')); 	$manager->manage_reparent($w);$hbox->pack_start($w = new GtkButton('OK2')); 	$manager->manage_reparent($w);*/$manager->show_all();Gtk::main();?>