Advertisement
blogandor

AndOr - Adicionando colunas editáveis no GtkTreeview

Nov 15th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.06 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Adicionando colunas editáveis no GtkTreeview
  4.  * PHP-GTK
  5.  * http://andor.com.br
  6.  */
  7.  
  8. /**
  9.  * Seta a codificação do programa
  10.  */
  11. ini_set("php-gtk.codepage", "UTF-8");
  12.    
  13. /**
  14.  * Classe de exemplo
  15.  */
  16. class Demo {
  17.     /**
  18.      * Armazena os widgets necessarios
  19.      *
  20.      * @access private
  21.      * @property array $widgets
  22.      */
  23.     public $widgets = array();
  24.    
  25.     /**
  26.      * @name __construct()
  27.      * @return Demo
  28.      */
  29.     public function __construct() {
  30.         // Cria a janela
  31.         $this->widgets['frmDemo'] = new GtkWindow();
  32.         $this->widgets['frmDemo']->set_size_request(250, 250);
  33.         $this->widgets['frmDemo']->set_position(Gtk::WIN_POS_CENTER_ALWAYS);
  34.         $this->widgets['frmDemo']->set_title("Demo");
  35.         $this->widgets['frmDemo']->connect("destroy", array($this, "frmDemo_unload"));
  36.         $vbox = new GtkVBox();
  37.  
  38.         // Cria o modelo
  39.         $model = new GtkListStore(GObject::TYPE_STRING, GObject::TYPE_STRING);
  40.  
  41.         // Creia o treeview
  42.         $this->widgets['trvTeste'] = new GtkTreeView($model);
  43.  
  44.         // Cria a colunas
  45.         $render = new GtkCellRendererText();
  46.         $render->set_property("editable", TRUE);
  47.         $render->connect("edited",  array($this, "trvTeste_onChange"), 0);
  48.        
  49.         $column = new GtkTreeViewColumn("Couna 1", $render, "text", 0);
  50.         $this->widgets['trvTeste']->append_column($column);
  51.  
  52.         $render = new GtkCellRendererText();
  53.         $render->set_property("editable", TRUE);
  54.         $render->connect("edited",  array($this, "trvTeste_onChange"), 1);
  55.        
  56.         $column = new GtkTreeViewColumn("Coluna 2", $render, "text", 1);
  57.         $this->widgets['trvTeste']->append_column($column);
  58.  
  59.         // Popula o model
  60.         $model->append(array("Valor 1", "1,19"));
  61.         $model->append(array("Valor 2", "2,32"));
  62.         $model->append(array("Valor 3", "3,29"));
  63.         $model->append(array("Valor 4", "4,02"));
  64.         $model->append(array("Valor 5", "5,30"));
  65.        
  66.         // Inicia a aplicação
  67.         $vbox->pack_start($this->widgets['trvTeste']);
  68.         $this->widgets['frmDemo']->add($vbox);
  69.         $this->frmDemo_onload();
  70.     }
  71.    
  72.     /**
  73.      * Método do carregamento do formulario
  74.      *
  75.      * @name frmDemo_onload()
  76.      */
  77.     public function frmDemo_onload() {
  78.         // Inicia a aplicação
  79.         $this->widgets['frmDemo']->show_all();
  80.         Gtk::main();
  81.     }
  82.    
  83.     /**
  84.      * Método do descarregamento do formulario
  85.      *
  86.      * @name frmDemo_unload()
  87.      */
  88.     public function frmDemo_unload() {
  89.         // Encerra a aplicação
  90.         Gtk::main_quit();
  91.     }
  92.    
  93.     /**
  94.      * Método que edita as colunas do trvTeste
  95.      *
  96.      * @name trvTeste_onChange
  97.      * @param GtkCellRender $cellRenderer Renderizador da coluna
  98.      * @param integer $path Linha que foi editada
  99.      * @param mixed $value Valor que está sendo adicionado
  100.      * @param integer $column Numero da coluna editada
  101.      */
  102.     public function trvTeste_onChange($cellRenderer, $path, $value, $column) {
  103.         // Verifica se foi digitado alguma coisa
  104.         if(trim($value) == "") {
  105.             return FALSE;
  106.         }
  107.        
  108.         // Recupera o model do treeview
  109.         $model = $this->widgets['trvTeste']->get_model();
  110.         $iter = $model->get_iter($path);
  111.        
  112.         // Seta o valor no treeview
  113.         $model->set($iter, $column, $value);
  114.     }
  115. }
  116.    
  117. /**
  118.  * Inicia o demo
  119.  */
  120. new Demo();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement