Página Inicial > GtkEventBox, GtkNoteBook > Como adicionar mais eventos às abas do GtkNoteBook

Como adicionar mais eventos às abas do GtkNoteBook

Explicação

Nem sempre utilizamos as abas de um notebook simplesmente para muda-las. É conveniente colocarmos, por exemplo, um menu no mesmo, para facilitar a interação do usuário com a aplicação. Para tais eventos, precisamos adicionar eventos nas abas. Uma solução simples, é utilizar um GtkEventBox no lugar de um GtkLabel, e dentro do GtkEventBox sim, usaremos um GtkLabel. Com o eventbox adicionado, podemos adicionar qualquer evento que precisarmos.

Imagem

screenshot1

Solução

Utilizamos o widget GtkEventBox para adicionar um GtkLabel.

Exemplo

<?php
   // Classe de testes
   class Teste
   {
      // Vetor que armazena todos os widgets da interface
      public $widgets = array();
      
      // Construtor da classe
      public function __construct()
      {
         // Cria o notebook
         $this->widgets['ntbTeste'] = new GtkNoteBook();
         
         // Cria a primeira aba
         $eventbox = new GtkEventBox();
         $lbl = new GtkLabel("Aba 1");
         $eventbox->add($lbl);
         $lbl->show();
         $this->widgets['ntbTeste']->append_page(new GtkLabel("Aba 1"), $eventbox);
         $eventbox->connect("button-press-event", array($this, "ntbTeste_onButtonPress"), 1);
         
         // Cria a segunda aba
         $eventbox = new GtkEventBox();
         $lbl = new GtkLabel("Aba 2");
         $eventbox->add($lbl);
         $lbl->show();
         $this->widgets['ntbTeste']->append_page(new GtkLabel("Aba 2"), $eventbox);
         $eventbox->connect("button-press-event", array($this, "ntbTeste_onButtonPress"), 2);
         
         // Cria a terceira aba
         $eventbox = new GtkEventBox();
         $lbl = new GtkLabel("Aba 3");
         $eventbox->add($lbl);
         $lbl->show();
         $this->widgets['ntbTeste']->append_page(new GtkLabel("Aba 3"), $eventbox);
         $eventbox->connect("button-press-event", array($this, "ntbTeste_onButtonPress"), 3);
         
         // Cria a janela
         $this->widgets['frmTeste'] = new GtkWindow();
         $this->widgets['frmTeste']->set_size_request(200, 200);
         $this->widgets['frmTeste']->set_resizable(FALSE);
         $this->widgets['frmTeste']->set_title("Teste");
         $this->widgets['frmTeste']->add($this->widgets['ntbTeste']);
         $this->widgets['frmTeste']->set_position(GTK::WIN_POS_CENTER);
         $this->widgets['frmTeste']->show_all();
         
         // Conecta o destroy
         $this->widgets['frmTeste']->connect_simple(
            "destroy",
            array("Gtk", "main_quit")
         );
      }
      
      public function ntbTeste_onButtonPress($widget, $event, $tab)
      {
         echo "Botao " . $event->button . " da aba " . $tab . "\n";
      }
   }

   // Inicia a aplicação
   $teste = new Teste();
   gtk::main();
 

Referências

http://gtk.php.net/manual/en/gtk.gtkwidget.php#signals
http://gtk.php.net/manual/en/gtk.gtknotebook.method.append_page.php

Categories: GtkEventBox, GtkNoteBook Tags:
  1. Nenhum comentário ainda.
  1. Nenhum trackback ainda.