Forum und email

Carregando o arquivo

A primeira coisa a fazer é carregar o arquivo .glade da seção anterior. O construtor GladeXML leva o caminho do arquivo como primeiro parâmetro, assim tudo o que precisamos fazer é:

Exemplo 4.2. Carregando o arquivo .glade

<?php
//Create a new glade instance, load the 
// widgets from the file passed as parameter
$glade = new GladeXML('helloglade.glade');

//Start the main loop
Gtk::main();
?>

Ao executar o script, você irá notar que a janela com o botão aparece na sua tela, mas não reage a nada além de fechar a janela. Mesmo assim, a janela é destruída, mas seu script continua a executar - claramente um caso de falta de conexão de sinais.

Conectando os sinais na mão

Proximo, nós iremos conectar os sinais que nós conhecemos: Chamar connect ou connect_simple no objeto do widget. Para obter o objeto, use o metodo get_widget() e passe o nome do widget (id) para ele. Então faça o trabalho como de costume:

Exemplo 4.3. Obtendo e conectando widgets

<?php
//Create a new glade instance, load the
// widgets from the file passed as parameter
//We use the absolute file path as it is not uncommon
// that the application is run from a different working directory
$glade = new GladeXML(dirname(__FILE__) . '/helloglade.glade');

//Nothing happened when you clicked the button or closed
// the window with Step 1's code.
//Here we manually connect the widget signals as you know it
$window = $glade->get_widget('wndClose');
$window->connect_simple('destroy', array('Gtk', 'main_quit'));

//Again, get the widget object and connect the clicked signal
$button = $glade->get_widget('btnClose');
$button->connect_simple('clicked', 'onClickButton');

//This method is called when the button is clicked
function onClickButton() {
    echo "button clicked!\r\n";
    Gtk::main_quit();
}

//Start the main loop
Gtk::main();
?>

Usando o Glade para conectar os sinais

Você provavelmente notou as tags <signal> no arquivo .glade - é possível definir manipuladores de sinais diretamente no arquivo. Tudo o que nós temos que fazer é dizer ao Glade para estabelecer as conexões usando signal_autoconnect() .

Você pode definir nome de funções simples como manipulador que será chamado quando um evento ocorrer, ou usar uma notação especial para metodos estáticos, separando o nome de uma classe e o metodo da classe com dois pontos duplo como em NomeClasse::metodoClasse.

Exemplo 4.4. Usando signal_autoconnect

<?php
//Create a new glade instance, load the
// widgets from the file passed as parameter
//We use the absolute file path as it is not uncommon
// that the application is run from a different working directory
$glade = new GladeXML(dirname(__FILE__) . '/helloglade.glade');

//Let glade do all the signal connections we specified in the file
$glade->signal_autoconnect();

//This method is called when the button is clicked
function onClickButton() {
    echo "button clicked!\r\n";
    Gtk::main_quit();
}

//Start the main loop
Gtk::main();
?>

Conectando com metodos de objetos

Apenas conectar funções ou metodos estáticos não prenche na realidade as necessidades de um bom programador. Para sair do código espagueti, nós precisamos ser capazes de conectar sinais a metodos de objetos.

Fazer isto é realmente simples: Apenas use signal_autoconnect_instance() com o objeto como primeiro parâmetro ao invéz de usar signal_autoconnect() :

Exemplo 4.5. Usando signal_autoconnect

<?php
//Here we use an object and connect all the
// signals to *object methods* instead of
// functions

class MyClass {
    //This method is called when the button is clicked
    function onClickButton() {
        echo "MyClass->onClickButton!\r\n";
        Gtk::main_quit();
    }

    function staticMethod() {
        echo "MyClass::staticMethod()\r\n";
    }
}

$glade = new GladeXML(dirname(__FILE__) . '/helloglade.glade');

//Let glade do all the signal connections we specified in the file
// but this time, connect to the object methods
$myClassInstance = new MyClass();
$glade->signal_autoconnect_instance($myClassInstance);

//Start the main loop
Gtk::main();
?>