All pastes #330939 Raw Edit

Mine

public text v1 · immutable
#330939 ·published 2007-01-28 12:22 UTC
rendered paste body
<?php

class tipwindow extends GtkWindow {
	
	public function __construct($tip){
		parent::__construct(Gtk::WINDOW_POPUP);
		$this->set_border_width (0);

		$eb = new gtkeventbox();
		$eb->set_border_width (1);
		$this->add($eb);

		$label = new Gtklabel($tip);
		$eb->add($label);
	

		$pfd = new pangofontdescription("courier");
		$label->modify_font($pfd);

		$cmap = $this->get_colormap();
		$color = new GdkColor(0,0,0);
		if ($cmap->alloc_color($color)) { 
			$this->modify_bg(Gtk::STATE_NORMAL,$color); 
		}else{ 
			trigger_error("Color allocation failed!", E_USER_WARNING);
		}
	
		$cmap = $eb->get_colormap();

		$color = new GdkColor(65535,65535,45535);
		if ($cmap->alloc_color($color)) {
			$eb->modify_bg(Gtk::STATE_NORMAL, $color);
		}else{
			trigger_error("Color allocation failed!", E_USER_WARNING); 
		}		
	}
	
}

class textviewtip extends GtkTextView {
	
	protected $buffer;
	protected $tipwin;
	protected $timeoutid;
	
	public function __construct(){
		parent::__construct();
		$this->buffer = new gtktextbuffer();
		$this->set_buffer($this->buffer);
		$this->buffer->connect('insert-text',array($this,'buffer_insert_text'));
	}
	
	public function buffer_insert_text($textbuffer, $arg1, $arg2, $arg3){
		if (strcmp ($arg2, "(") == 0){
			$this->insert_open_brace($arg1);
	    }
		if (strcmp ($arg2, ")") == 0){
			if($this->tipwin !== null) {
	      		$this->tipwin->destroy();
	      		$this->tipwin = null;
	      	}
	      	if ($this->timeoutid !== null) {
				Gtk::timeout_remove($this->timeoutid);
			}
	    }
	}
	
	public function insert_open_brace($arg1){
		$text_buffer = $this->buffer;
		$start = $text_buffer->get_iter_at_mark($text_buffer->get_insert());
		if (!$start->backward_word_start()) {
			return;
		}
		$text = $arg1->get_text($start);
		$tip_text = get_tip($text);
		if ($tip_text == NULL) return;
		$buf_loc = $this->get_iter_location($arg1);
		$win_x_y = $this->buffer_to_window_coords(Gtk::TEXT_WINDOW_WIDGET,$buf_loc->x,$buf_loc->y);

		list($win_x, $win_y) = $win_x_y;
		$win = $this->get_window(Gtk::TEXT_WINDOW_WIDGET);
		$x_y = $win->get_origin();
		list($x,$y) = $x_y;

		if($this->tipwin !== NULL){
			$this->tipwin->destroy(); 
 	 		$this->tipwin = null;
		}
		if ($this->timeoutid !== null) {
			Gtk::timeout_remove($this->timeoutid);
		}
		$this->tipwin = new tipwindow($tip_text);
		$this->tipwin->move($win_x+$x+($buf_loc->height),$win_y+$y+($buf_loc->height));
		$this->tipwin->show_all();
		$this->timeoutid = Gtk::timeout_add(2500,array($this,'tip_timeout'));
	}
	
	public function tip_timeout(){
		if ($this->tipwin !== null){
			$this->tipwin->destroy();
			$this->tipwin = null;
		}
	}
}

$tips = array();

$api = file_get_contents('php-gphpedit.api');
$api = explode("\n",$api);
foreach ($api as $value){
	$function = explode('|',$value);
	$tip = $function[1].' '.$function[0].$function[2]."\n".$function[3];
	$tips[] = array($function[0], $tip);
}

define('NUM_TIPS',count($tips));

$texttip = new textviewtip();

$wnd = new gtkwindow();
$wnd->connect_simple('destroy',array('gtk','main_quit'));
$wnd->add($texttip);
$wnd->show_all();
gtk::main();

function get_tip($text){
	global $tips;

	$found = FALSE;
	for ($i = 0; $i < NUM_TIPS; $i++){
    	if (strcmp($text, $tips[$i][0]) == 0){
        	$found = TRUE;
        	break;
        }
    }
  	if (!$found) return NULL;
  	return $tips[$i][1];
}

/*function tip_window_new($tip){
	$win = new gtkwindow(Gtk::WINDOW_POPUP);
	$win->set_border_width (0);

	$eb = new gtkeventbox();
	$eb->set_border_width (1);
	$win->add($eb);

	$label = new Gtklabel($tip);
	$eb->add($label);
	

	$pfd = new pangofontdescription("courier");
	$label->modify_font($pfd);

	$cmap = $win->get_colormap();
	$color = new GdkColor(0,0,0);
	if ($cmap->alloc_color($color)) { 
		$win->modify_bg(Gtk::STATE_NORMAL,$color); 
	}else{ 
		trigger_error("Color allocation failed!",E_USER_WARNING);
	}
	
	$cmap = $eb->get_colormap();

	$color = new GdkColor(65535,65535,45535);
	if ($cmap->alloc_color($color)) { 
		$eb->modify_bg(Gtk::STATE_NORMAL,$color); 
	}else{
		trigger_error("Color allocation failed!",E_USER_WARNING); 
	}
	
	return $win;

}

function insert_open_brace(&$tip_win, $text_view, $arg1){
$text_buffer = $text_view->get_buffer();
$start = $text_buffer->get_iter_at_mark($text_buffer->get_insert());
	if (!$start->backward_word_start()) {
		return;
	}
	$text = $arg1->get_text($start);
	echo "$text\n";
	$tip_text = get_tip($text);
	echo $tip_text;  
	if ($tip_text == NULL) return;
	$buf_loc = $text_view->get_iter_location($arg1);
	printf("Buffer: %d, %d\n",$buf_loc->x,$buf_loc->y);
	$win_x_y = $text_view->buffer_to_window_coords(Gtk::TEXT_WINDOW_WIDGET,$buf_loc->x,$buf_loc->y);

	list($win_x, $win_y) = $win_x_y;
	printf("Window: %d, %d\n", $win_x, $win_y);
	$win = $text_view->get_window(Gtk::TEXT_WINDOW_WIDGET);
	$x_y = $win->get_origin();
	list($x,$y) = $x_y;

	if($tip_win !== NULL){
		$tip_win->destroy(); 
  		$tip_win = null;
	}
	$tip_win = tip_window_new($tip_text);
	$tip_win->move($win_x+$x+($buf_loc->height),$win_y+$y+($buf_loc->height));
	$tip_win->show_all();
	Gtk::timeout_add(2500,'tip_timeout',$tip_win);
	return $tip_win;
}

function tip_timeout(&$tip_win){
	$tip_win->destroy();
	$tip_win = null;
}

function buffer_insert_text ($textbuffer, $arg1, $arg2, $arg3, $user_data){
  static $tip_win = null;

  if (strcmp ($arg2, "(") == 0)
    {
      $tip_win = insert_open_brace($tip_win, $user_data, $arg1);
    }

  if (strcmp ($arg2, ")") == 0)
    {
      if($tip_win !== null) {
      	$tip_win->destroy();
      	$tip_win = null;
      }
    }
}


function main($argv, $argc){

$win = new gtkwindow(Gtk::WINDOW_TOPLEVEL);
$win->connect_simple('destroy',array('gtk','main_quit'));

$text_view = new gtktextview();
$buffer = new gtktextbuffer();
$text_view->set_buffer($buffer);
$buffer->connect('insert-text','buffer_insert_text',$text_view);
$win->add($text_view);

$win->show_all();

Gtk::main();
}

main($argv, $argc);
*/

?>