229. How to setup a dialog box - Part 1 - hello dialog?

Problem

Besides GtkTreeView, I use GtkDialogs a lot. I use them to display edit forms, help messages, progress of long tasks, search results, etc.

I think there are too little resources and examples out there (the official php-gtk2 documentation and others on Internet) to really show the power of GtkDialog.

I will start to write a series of examples focusing on GtkDialog. So if you're interested, do stay tuned.

Let's start our journey of GtkDialog with the most basic dialog — hello_dialog — as shown below. It looks simple.

Yes, it is. But we can learn a lot of things from this simple example.

How to setup a dialog box - Part 1 - hello dialog?


Solution

  • First of all, compare this example with the Hello Word example for PHP-GTK2 — so that you can see clearly the similaries and differences between using a GtkWindow and GtkDialog.
  • Note that GtkDialog is a descendent of GtkWindow. So it should come with no surprise that they share a lot of similaries between them.
  • Each GtkDialog comes set up and predefined with two areas. The top area is usually refered to as the top area. It's a GtkVBox and is accessed with $dialog->vbox.
  • The bottom area is usually refered to as the action area, because this is usually the place where we stuff the action buttons. By the way, in case you're wondering, this is a GtkHButtonBox, not a GtkVBox.
  • Since this is a hello world example for GtkDialog, we have only stuffed a GtkLabel in the top area, and no buttons in the action area.
  • Take note of the separator line sitting between the top area and the action area. It's there by default. Of course it can be turned off. We will show this in the next example.
  • When we use a GtkWindow, we use Gtk::main() to start the main loop. For GtkDialog, we use $dialog->run instead.
  • Please also note that in a GtkWindow, we need to explicitly define a signal handler for the destroy event: $window->connect_simple('destroy', array('Gtk','main_quit'));
  • For a GtkDialog, the destroy event is taken care of internally. Try closing the dialog. You will see that the dialog will exit cleanly.

Sample Code

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
18   
19   
20   
21   
24   
25   
26   
27   
28   
29   
30   
31   
32   
33   
34   
35   
36   
37   
38   
42   
43   
44   
45   
46   
47   
<?php
$window = new GtkWindow();
$window->set_title($argv[0]);
$window->set_size_request(200, 120);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Set up dialog box\n".
"Part 1 - hello dialog");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 40);
$title->set_justify(Gtk::JUSTIFY_CENTER);
$alignment = new GtkAlignment(0.5, 0, 0, 0);
$alignment->add($title);
$vbox->pack_start($alignment, 0, 0);
$vbox->pack_start(new GtkLabel(), 0, 0);

$vbox->pack_start($hbox = new GtkHBox(), 0);
$hbox->pack_start($button = new GtkButton('popup hello dialog'), 0);
$button->connect('clicked', 'on_click');

$window->show_all();
Gtk::main();

function on_click() {
    setup_hello_dialog();
}

function setup_hello_dialog() {

    $dialog = new GtkDialog(); // note 1
    $label = new GtkLabel("hello dialog!"); // note 2
    $dialog->vbox->pack_start($label); // note 3
    $dialog->show_all(); // note 4
    $dialog->run(); // note 5
    $dialog->destroy(); // note 6
}

?>

Output

As shown above.
 

Explanation

  1. Create a new dialog.
  2. Create a label containing the text "hello dialog".
  3. Stuff the label in the top area.
  4. Show the dialog and its contents.
  5. Run the dialog!
  6. Close the dialog

Related Links

Add comment


Security code
Refresh