<commit_before><?php
/*
* php5 discourages the use of dl(), so the user should add it to php.ini
*/
if (!class_exists('gtk')) {
    die('Please load the php-gtk2 module in your php.ini' . "\r\n");
}



class PHPGtk2Demo extends GtkWindow
{
    protected $demos = array();
    protected $description_buffer;
    protected $demobox;
    
    static protected $colors = array("blue", "red", "orange", "darkgreen", "black");
    
    protected $cache = array();
    
    function __construct()
    {
        parent::__construct();

        $this->connect_object('destroy', array('gtk', 'main_quit'));
        
        $this->set_title('PHP-Gtk2 Demos');
        $this->set_default_size(800, 400);
        
        $hbox = new GtkHBox(false, 3);
        $this->add($hbox);
        
        $treeview = $this->__create_treeview();
        $hbox->pack_start($treeview, false, false);
        
        $notebook = new GtkNotebook();
        $hbox->pack_start($notebook, true);
        
        $notebook->append_page($this->__create_text(), new GtkLabel('Info'));
        
        $scroll_demo = new GtkScrolledWindow();
        $scroll_demo->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
        
//        $this->demobox = new GtkHBox();
        $this->demobox = $scroll_demo;
        $page = $notebook->append_page($this->demobox, new GtkLabel('Demo'));

        list($text, $this->sourcebuffer) = $this->__create_source();
        $notebook->append_page($text, new GtkLabel('Source'));
        
        $this->show_all();
    }//function __construct()
    
    
    
    function __create_treeview()
    {
        $this->load_demos();
        
        $model = new GtkListStore(Gtk::TYPE_PHP_VALUE, Gtk::TYPE_STRING);
//		$model->append(array("asd", "aswdasd"));//???????
        
        foreach ($this->demos as $demo) {
            $iter = $model->append();
            $model->set($iter, 0, $demo);
        }
        
        $treeview = new GtkTreeView($model);
        
        $cell_renderer = new GtkCellRendererText();
        $treeview->insert_column_with_data_func(-1, 'Demos', $cell_renderer, array(&$this,'label_setter'));
        
        $selection = $treeview->get_selection();
        $selection->set_mode(Gtk::SELECTION_SINGLE);
        $selection->connect('changed', array($this, 'demo_selected'));
        
        //crashes as gboxed can't be instantiated
//		$treeview->connect('row-activated', array($this, 'demo_activate'));

//		$column = new GtkTreeViewColumn();
//		$column->set_title('Demos');
//		$treeview->append_column($column);

        return $treeview;
    }//function __create_treeview()
    
    
    
    function __create_source()
    {
        $scrolled_window = new GtkScrolledWindow();
        $scrolled_window->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
        $scrolled_window->set_shadow_type(Gtk::SHADOW_IN);
        
        $text_view = new GtkTextView();
        $scrolled_window->add($text_view);

        $buffer = new GtkTextBuffer();
        $text_view->set_buffer($buffer);
        $text_view->set_editable(false);
        $text_view->set_cursor_visible(false);

		$tag_table = $buffer->get_tag_table();	
	
		foreach(self::$colors as $color) {
			$tag = new GtkTextTag($color);
			$tag->set_property("foreground", $color);
			$tag_table->add($tag);
		}
        
//        $text_view->set_wrap_mode(false);
        
        return array($scrolled_window, $buffer);
    }
    
    
    function label_setter($column, $cell, $model, $iter)
    {
        $info = $model->get_value($iter, 0);
        $cell->set_property('text', $info->classname);
    }
    
    
    
    function demo_selected($selection)
    {
        list($model, $iter) = $selection->get_selected();
        if (!$iter) {
            return;
        }
        $info = $model->get_value($iter, 0);
    
        $text = $info->classname . "\r\n\r\n" . $info->description;

	//  [JSJ]: I get a too many arguements error using this form, so changed to the later form
	//  Seems silly we would require the strlen() anyway as PHP should be storing that in the zval already anyway
    //	$this->description_buffer->set_text($text, strlen($text));
        $this->description_buffer->set_text($text);
        
        //source code highlighting
        $this->highlightSource($info->file);
        
        $classname = $info->classname;
        $obj = new $classname();
    
        //remove kid
        if ($this->demobox->get_child() !== null) {
            $this->demobox->remove($this->demobox->get_child());
        }
        
        if (!isset($this->cache[$info->classname])) {
            $subobj = $obj->__create_box();
            $this->cache[$info->classname] = null;
            $this->cache[$info->classname] = $subobj;
        }
        $this->demobox->add_with_viewport($this->cache[$info->classname]);
        $this->demobox->show_all();
        
    }
    
    
    
    function __create_text()
    {
        $scrolled_window = new GtkScrolledWindow();
        $scrolled_window->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
        $scrolled_window->set_shadow_type(Gtk::SHADOW_IN);
        
        $text_view = new GtkTextView();
        $scrolled_window->add($text_view);
        
        $this->description_buffer = new GtkTextBuffer();
        $text_view->set_buffer($this->description_buffer);
        $text_view->set_editable(false);
        $text_view->set_cursor_visible(false);
        $text_view->set_wrap_mode(Gtk::WRAP_WORD);
        
        return $scrolled_window;<commit_msg>Disabled caching of created demos as this doesn't seem to work<commit_after><?php
/*
* php5 discourages the use of dl(), so the user should add it to php.ini
*/
if (!class_exists('gtk')) {
    die('Please load the php-gtk2 module in your php.ini' . "\r\n");
}



class PHPGtk2Demo extends GtkWindow
{
    protected $demos = array();
    protected $description_buffer;
    protected $demobox;
    
    static protected $colors = array("blue", "red", "orange", "darkgreen", "black");
    
    protected $cache = array();
    
    function __construct()
    {
        parent::__construct();

        $this->connect_simple('destroy', array('gtk', 'main_quit'));
        
        $this->set_title('PHP-Gtk2 Demos');
        $this->set_default_size(800, 400);
        
        $hbox = new GtkHBox(false, 3);
        $this->add($hbox);
        
        $treeview = $this->__create_treeview();
        $hbox->pack_start($treeview, false, false);
        
        $notebook = new GtkNotebook();
        $hbox->pack_start($notebook, true);
        
        $notebook->append_page($this->__create_text(), new GtkLabel('Info'));
        
        $scroll_demo = new GtkScrolledWindow();
        $scroll_demo->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
        
//        $this->demobox = new GtkHBox();
        $this->demobox = $scroll_demo;
        $page = $notebook->append_page($this->demobox, new GtkLabel('Demo'));

        list($text, $this->sourcebuffer) = $this->__create_source();
        $notebook->append_page($text, new GtkLabel('Source'));
        
        $this->show_all();
    }//function __construct()
    
    
    
    function __create_treeview()
    {
        $this->load_demos();
        
        $model = new GtkListStore(Gtk::TYPE_PHP_VALUE, Gtk::TYPE_STRING);
//		$model->append(array("asd", "aswdasd"));//???????
        
        foreach ($this->demos as $demo) {
            $iter = $model->append();
            $model->set($iter, 0, $demo);
        }
        
        $treeview = new GtkTreeView($model);
        
        $cell_renderer = new GtkCellRendererText();
        $treeview->insert_column_with_data_func(-1, 'Demos', $cell_renderer, array(&$this,'label_setter'));
        
        $selection = $treeview->get_selection();
        $selection->set_mode(Gtk::SELECTION_SINGLE);
        $selection->connect('changed', array($this, 'demo_selected'));
        
        //crashes as gboxed can't be instantiated
//		$treeview->connect('row-activated', array($this, 'demo_activate'));

//		$column = new GtkTreeViewColumn();
//		$column->set_title('Demos');
//		$treeview->append_column($column);

        return $treeview;
    }//function __create_treeview()
    
    
    
    function __create_source()
    {
        $scrolled_window = new GtkScrolledWindow();
        $scrolled_window->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
        $scrolled_window->set_shadow_type(Gtk::SHADOW_IN);
        
        $text_view = new GtkTextView();
        $scrolled_window->add($text_view);

        $buffer = new GtkTextBuffer();
        $text_view->set_buffer($buffer);
        $text_view->set_editable(false);
        $text_view->set_cursor_visible(false);

		$tag_table = $buffer->get_tag_table();	
	
		foreach(self::$colors as $color) {
			$tag = new GtkTextTag($color);
			$tag->set_property("foreground", $color);
			$tag_table->add($tag);
		}
        
//        $text_view->set_wrap_mode(false);
        
        return array($scrolled_window, $buffer);
    }
    
    
    function label_setter($column, $cell, $model, $iter)
    {
        $info = $model->get_value($iter, 0);
        $cell->set_property('text', $info->classname);
    }
    
    
    
    function demo_selected($selection)
    {
        list($model, $iter) = $selection->get_selected();
        if (!$iter) {
            return;
        }
        $info = $model->get_value($iter, 0);
    
        $text = $info->classname . "\r\n\r\n" . $info->description;

	//  [JSJ]: I get a too many arguements error using this form, so changed to the later form
	//  Seems silly we would require the strlen() anyway as PHP should be storing that in the zval already anyway
    //	$this->description_buffer->set_text($text, strlen($text));
        $this->description_buffer->set_text($text);
        
        //source code highlighting
        $this->highlightSource($info->file);
        
        $classname = $info->classname;
        $obj = new $classname();
    
        //remove kid
        if ($this->demobox->get_child() !== null) {
            $this->demobox->remove($this->demobox->get_child());
        }
        
//        if (!isset($this->cache[$info->classname])) {
            $subobj = $obj->__create_box();
            $this->cache[$info->classname] = null;
            $this->cache[$info->classname] = $subobj;
//        }
        $this->demobox->add_with_viewport($this->cache[$info->classname]);
        $this->demobox->show_all();
        
    }
    
    
    
    function __create_text()
    {
        $scrolled_window = new GtkScrolledWindow();
        $scrolled_window->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
        $scrolled_window->set_shadow_type(Gtk::SHADOW_IN);
        
        $text_view = new GtkTextView();
        $scrolled_window->add($text_view);
        
        $this->description_buffer = new GtkTextBuffer();
        $text_view->set_buffer($this->description_buffer);
        $text_view->set_editable(false);
        $text_view->set_cursor_visible(false);
        $text_view->set_wrap_mode(Gtk::WRAP_WORD);
        
        return $scrolled_window;
{'fraction_non_alphanumeric': 0.1563031224836718, 'fraction_numerical': 0.0026840833855238436, 'mean_word_length': 2.4789915966386555, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 167, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1421593', 'n_tokens_mistral': 3675, 'n_tokens_neox': 3569, 'n_words': 597}