Como facilitar a criação de um botão personalizado
Explicação
Este post é uma continuação do post Como criar um botão personalizado com GtkEventBox. Na verdade uma classe para facilitar todo esse trabalho.
Imagem
Solução
Cria um GtkEventBox();
Conectar o GtkEventBox() a todos os sinais;
Verificar o Mouse Over, Mouse Out e o Expose;
Tratar qual imagem mostrar em cada sinal.
Exemplo
// Classe de personalização de botões
class GtkProButton extends GtkEventBox
{
private $img1 = "";
private $img2 = "";
private $curImage = NULL;
public function __construct()
{
parent::__construct();
// Conecta o eventbox aos eventos
$this->connect(
"event",
array($this, "onEvent")
);
}
// Método que trata os eventos do evtTeste
public function onEvent($widget, $event)
{
switch($event->type)
{
// Evento que pinta o widget
case 2:
if($this->curImage !== NULL)
{
$pixbuf = GdkPixbuf::new_from_file($this->curImage);
$w = $pixbuf->get_width();
$h = $pixbuf->get_height();
$x = $widget->allocation->width – $w;
$y = 0;
$widget->window->draw_pixbuf(
$widget->style->bg_gc[Gtk::STATE_NORMAL],
$pixbuf,
0,
0,
$x,
$y
);
}
return TRUE;
// On Mouse Over
case 10:
// Muda a imagem
$this->curImage = $this->img2;
$this->queue_draw();
// Muda o cursor
$cursor = new GdkCursor(Gdk::HAND2);
$widget->window->set_cursor($cursor);
break;
// On Mouse Out
case 11:
// Muda a imagem
$this->curImage = $this->img1;
$this->queue_draw();
break;
}
}
// Método que recebe a imagem 1
public function setImage1($value)
{
$this->curImage = $value;
$this->img1 = $value;
}
// Método que recebe a imagem 2
public function setImage2($value)
{
$this->img2 = $value;
}
}
// Classe de testes
class Teste
{
// Vetor que armazena todos os widgets da interface
public $widgets = array();
// Construtor da classe
public function __construct()
{
// Cria o container
$this->widgets['fixTeste'] = new GtkFixed();
// Cria a janela
$this->widgets['frmTeste'] = new GtkWindow();
$this->widgets['frmTeste']->set_size_request(200, 200);
// Cria o botão
$this->widgets['btnTeste'] = new GtkProButton();
$this->widgets['btnTeste']->set_size_request(59, 103);
$this->widgets['fixTeste']->put($this->widgets['btnTeste'], 8, 8);
// Configura as imagens
$this->widgets['btnTeste']->setImage1("btn01.png");
$this->widgets['btnTeste']->setImage2("btn02.png");
// Conecta o botão ao click
$this->widgets['btnTeste']->connect_simple("button-release-event", array($this, "btnTeste_onClick"));
// Mostra a janela
$this->widgets['frmTeste']->add($this->widgets['fixTeste']);
$this->widgets['frmTeste']->show_all();
// Conecta o destroy
$this->widgets['frmTeste']->connect_simple(
"destroy",
array("Gtk", "main_quit")
);
}
// Metodo que trata o click
public function btnTeste_onClick()
{
echo "ok";
}
}
// Inicia a aplicação
$teste = new Teste();
gtk::main();
