Dialogs

In this part of the PHP GTK programming tutorial, we will introduce dialogs.

Dialog windows or dialogs are an indispensable part of most modern GUI applications. A dialog is defined as a conversation between two or more persons. In a computer application a dialog is a window which is used to "talk" to the application. A dialog is used to input data, modify data, change the application settings etc. Dialogs are important means of communication between a user and a computer program.

GtkMessageDialog

Message dialogs are convenient dialogs that provide messages to the user of the application. The message consists of textual and image data. The GtkMessageDialog is used to create message dialogs.

<?php

/* 
ZetCode PHP GTK tutorial

This example demonstrates a
GtkMessageDialog.

author: Jan Bodnar
website: www.zetcode.com
last modified: September 2011
*/
 
class Example extends GtkWindow { 
     
    public function __construct() { 

        parent::__construct(); 
         
        $this->init_ui();

    } 

    public function init_ui() {

        $this->set_title('GtkMessageDialog');         
        $this->connect_simple('destroy', array('gtk', 'main_quit')); 

        $fixed = new GtkFixed();              

        $button = new GtkButton("Information");
        $button->set_size_request($button->size_request());
        $button->connect('clicked', array($this, 'on_clicked'));
        
        $fixed->put($button, 50, 50);
        $this->add($fixed); 

        $this->set_default_size(250, 200); 
        $this->set_position(GTK::WIN_POS_CENTER);
        $this->show_all();         
    }

    public function on_clicked($sender) {

            $md = new GtkMessageDialog($this, Gtk::DIALOG_MODAL,
                Gtk::MESSAGE_INFO, Gtk::BUTTONS_OK, "Download completed.");
            $md->set_title("Information");    
            $md->run();
            $md->destroy();
    }
} 
     
new Example(); 
Gtk::main();
 
?>

We show a button on the window. When we click on the button, an information message is displayed.

$button = new GtkButton("Information");

This is a button, which will show a dialog, when we click on it.

public function on_clicked($sender) {

        $md = new GtkMessageDialog($this, Gtk::DIALOG_MODAL,
            Gtk::MESSAGE_INFO, Gtk::BUTTONS_OK, "Download completed.");
        $md->set_title("Information");    
        $md->run();
        $md->destroy();
}

If we click on the info button, the Information dialog is displayed. The Gtk::DIALOG_MODAL flag makes the dialog modal. The Gtk::MESSAGE_INFO specifies the type of the dialog. In our case it is an information dialog. Different icons are chosen for various dialog types. The Gtk::BUTTONS_OK shows the OK button on the dialog. The last parameter is the message displayed. We set the title of the dialog with the set_title() method. The dialog is displayed with the run() method. The programmer must also call either the destroy() or the hide() method in the end.

Information message dialog
Figure: Information message dialog

GtkAboutDialog

The GtkAboutDialog displays information about the application. It can display a logo, the name of the application, version, copyright, website or license information. It is also possible to give credits to the authors, documenters, translators, and artists.

<?php

/* 
ZetCode PHP GTK tutorial

This example demonstrates the
AboutDialog dialog.

author: Jan Bodnar
website: www.zetcode.com
last modified: September 2011
*/
 
class Example extends GtkWindow { 
     
    public function __construct() { 

        parent::__construct(); 
         
        $this->init_ui();

    } 

    public function init_ui() {

        $this->set_title('About Battery');         
        $this->connect_simple('destroy', array('gtk', 'main_quit')); 

        $fixed = new GtkFixed();              

        $button = new GtkButton("About");
        $button->set_size_request(80, 30);
        $button->connect('clicked', array($this, 'on_clicked'));
        
        $fixed->put($button, 50, 50);
        $this->add($fixed); 

        $this->set_default_size(250, 200); 
        $this->set_position(GTK::WIN_POS_CENTER);
        $this->show_all();         
    }

    public function on_clicked($sender) {

        $about = new GtkAboutDialog();
        $about->set_program_name("Battery");
        $about->set_version("0.1");
        $about->set_copyright("(c) Jan Bodnar");
        $about->set_comments("Battery is a simple tool for battery checking");
        $about->set_website("http://www.zetcode.com");
        $about->set_logo(GdkPixbuf::new_from_file("battery.png"));
        $about->run();
        $about->destroy();
    }

} 
     
new Example(); 
Gtk::main();
 
?>

The code example uses a GtkAboutDialog with some of its features.

$about = new GtkAboutDialog();

We create an instance of the GtkAboutDialog.

$about->set_program_name("Battery");
$about->set_version("0.1");
$about->set_copyright("(c) Jan Bodnar");

Here we specify the name, the version and the copyright of the program.

$about->set_logo(GdkPixbuf::new_from_file("battery.png"));

This line creates a logo.

GtkAboutDialog
Figure: GtkAboutDialog

GtkFontSelectionDialog

The GtkFontSelectionDialog is a dialog for selecting fonts. It is typically used in applications that do some text editing or formatting.

<?php

/* 
ZetCode PHP GTK tutorial

In this example, we change the font
of a label with the GtkFontSelectionDialog.

author: Jan Bodnar
website: www.zetcode.com
last modified: September 2011
*/
 
class Example extends GtkWindow { 
     
    private $label;

    public function __construct() { 

        parent::__construct(); 
         
        $this->init_ui();

    } 

    private function init_ui() {

        $this->set_title('FontSelectionDialog');         
        $this->connect_simple('destroy', array('gtk', 'main_quit')); 

        $this->set_border_width(10);
        $this->label = new GtkLabel("The only victory over love is flight.");
        $button = new GtkButton("Select font");
        $button->connect('clicked', array($this, 'on_clicked'));

        $fixed = new GtkFixed();
        $fixed->put($button, 100, 30);
        $fixed->put($this->label, 30, 90);
        $this->add($fixed);
       
        $this->set_default_size(350, 200); 
        $this->set_position(GTK::WIN_POS_CENTER);
        $this->show_all();         
    }

    public function on_clicked($sender) {

        $fdia = new GtkFontSelectionDialog("Select font name");
        $response = $fdia->run();
              
        if ($response == Gtk::RESPONSE_OK) {

            $font_desc = new PangoFontDescription($fdia->get_font_name());
            print($fdia->get_font_name());
            if ($font_desc) {
                $this->label->modify_font($font_desc);
            }
            
        }

        $fdia->destroy();
    }
} 
     
new Example(); 
Gtk::main();
 
?>

In the code example, we have a button and a label. We show the GtkFontSelectionDialog by clicking on the button.

$fdia = new GtkFontSelectionDialog("Select font name");

We create the GtkFontSelectionDialog.

if ($response == Gtk::RESPONSE_OK) {

    $font_desc = new PangoFontDescription($fdia->get_font_name());
    print($fdia->get_font_name());
    if ($font_desc) {
        $this->label->modify_font($font_desc);
    }
    
}

If we click on the OK button, the font of the label widget changes to the one that we selected in the dialog.

GtkFontSelectionDialog
Figure: GtkFontSelectionDialog

GtkColorSelectionDialog

The GtkColorSelectionDialog is a dialog for selecting colours.

<?php

/* 
ZetCode PHP GTK tutorial

This example works with the
GtkColorSelectionDialog.

author: Jan Bodnar
website: www.zetcode.com
last modified: August 2011
*/
 
class Example extends GtkWindow { 
     
    private $label;

    public function __construct() { 

        parent::__construct(); 
         
        $this->init_ui();

    } 

    private function init_ui() {

        $this->set_title('GtkColorSelectionDialog');         
        $this->connect_simple('destroy', array('gtk', 'main_quit')); 

        $this->set_border_width(10);
        $this->label = new GtkLabel("The only victory over love is flight.");
        $button = new GtkButton("Select color");
        $button->connect('clicked', array($this, 'on_clicked'));

        $fixed = new GtkFixed();
        $fixed->put($button, 100, 30);
        $fixed->put($this->label, 30, 90);
        $this->add($fixed);
       
        $this->set_default_size(350, 200); 
        $this->set_position(GTK::WIN_POS_CENTER);
        $this->show_all();         
    }

    public function on_clicked($sender) {

        $cdia = new GtkColorSelectionDialog("Select color");
        $response = $cdia->run();
              
        if ($response == Gtk::RESPONSE_OK) {
            $colorsel = $cdia->colorsel;
            $color = $colorsel->get_current_color();
            $this->label->modify_fg(Gtk::STATE_NORMAL, $color);
        }
        
        $cdia->destroy();
    }
} 
     
new Example(); 
Gtk::main();
 
?>

The example is very similar to the previous one. This time we change the color of the label.

$cdia = new GtkColorSelectionDialog("Select color");
$response = $cdia->run();

We create and run the GtkFontSelectionDialog.

if ($response == Gtk::RESPONSE_OK) {
    $colorsel = $cdia->colorsel;
    $color = $colorsel->get_current_color();
    $this->label->modify_fg(Gtk::STATE_NORMAL, $color);
}

If the user presses OK, we get the color value and modify the label's color.

In this part of the PHP GTK tutorial, we presented dialogs.