PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 229: How to setup a dialog box - Part 1 - hello dialog?
Written by kksou   
Thursday, 10 May 2007
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   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
27   
28   
<?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();
  • Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
  • Registration is free and immediate.
  • Have some doubt about the registration? Please read this forum article.
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

User reviews   Average user ratings:    0.0   (from 2 users)
  1. Harry from UK
    July 15, 2007 6:34am

    Hi there, thanks for the tutorial. Each time i click the "popup hello dialog" button the dialog shows but is not consistent in where it positions itself. It seems to show in a diagonal pattern from top left to bottom right. You can see this by opening and closing the dialog over and over again.

  2. kksou
    July 18, 2007 12:46am

    As this is the "hello world" for dialog, I did not include any code to position the dialog. To center the dialog, you could use $dialog->set_position(Gtk::WIN_POS_CENTER_ALWAYS)

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2008. kksou.com. All Rights Reserved