511. How to set up about dialog?

Problem

Suppose you want to set an About dialog for your application.

This can be easily done in PHP-GTK using GtkAboutDialog as shown below:

How to set up about dialog?

 

Solution


Sample Code

The following image file is required by the sample code below. Please save a copy of the image file and put it in the same directory where you store the sample code.

 button_play1.png

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
22   
23   
24   
25   
26   
27   
28   
29   
30   
31   
32   
33   
34   
35   
36   
37   
39   
40   
41   
42   
43   
45   
46   
47   
48   
49   
50   
51   
52   
53   
54   
55   
56   
57   
58   
59   
60   
61   
62   
64   
65   
66   
67   
68   
69   
70   
71   
72   
73   
74   
75   
76   
77   
78   
79   
80   
81   
82   
83   
84   
85   
86   
88   
89   
90   
91   
92   
93   
94   
95   
96   
98   
99   
100   
101   
102   
103   
104   
105   
106   
107   
108   
109   
113   
114   
116   
117   
118   
119   
120   
121   
122   
123   
124   
125   
126   
127   
128   
<?php
$window = new GtkWindow();
$window->set_title($argv[0]);
$window->set_size_request(400, 150);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());
$accel_group = new GtkAccelGroup();
$window->add_accel_group($accel_group);

// define menu definition
$menu_definition = array(
    '_File' => array('_Quit'),
    '_Help' => array('About'), // note 1
);
setup_menu($vbox, $menu_definition); 

// display title
$title = new GtkLabel("Set up about dialog");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$vbox->pack_start($title);
$vbox->pack_start(new GtkLabel(''));

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

// setup menu
function setup_menu($vbox, $menus) {
    global $accel_group;
    $menubar = new GtkMenuBar();
    $vbox->pack_start($menubar, 0, 0);
    foreach($menus as $toplevel => $sublevels) {
        $menubar->append($top_menu = new GtkMenuItem($toplevel));
        $menu = new GtkMenu();
        $top_menu->set_submenu($menu);
        foreach($sublevels as $submenu) {
            if (strpos("$submenu", '|') === false) {
                $accel_key = '';
            } else {
                list($submenu, $accel_key) = explode('|', $submenu);
            }

            if (is_array($submenu)) { // set up radio menus
                $i=0;
                $radio[0] = null;
                foreach($submenu as $radio_item) {
                    $radio[$i] = new GtkRadioMenuItem($radio[0], $radio_item);
                    $radio[$i]->connect('toggled', "on_toggle");
                    $menu->append($radio[$i]);
                    ++$i;
                }
                $radio[0]->set_active(1); // select the first item
            } else {
                if ($submenu=='<hr>') {
                    $menu->append(new GtkSeparatorMenuItem());
                } else {
                    $submenu2 = str_replace('_', '', $submenu);
                    $submenu2 = str_replace(' ', '_', $submenu2);
                    $stock_image_name = 'Gtk::STOCK_'.strtoupper($submenu2);
                    if (defined($stock_image_name)) {
                        $menu_item = new GtkImageMenuItem(
                                        constant($stock_image_name));
                    } else {
                        $menu_item = new GtkMenuItem($submenu);
                    }
                    if ($accel_key!='') {
                        $menu_item->add_accelerator("activate", $accel_group,
                        ord($accel_key), Gdk::CONTROL_MASK, 1);
                    }

                    $menu->append($menu_item);
                    $menu_item->connect('activate', 'on_menu_select');
                }
            }
        }
    }
}

// process radio menu selection
function on_toggle($radio) {
    $label = $radio->child->get_label();
    $active = $radio->get_active();
    echo("radio menu selected: $label\n");
}

// process menu item selection
function on_menu_select($menu_item) {
    $item = $menu_item->child->get_label();
    echo "menu selected: $item\n";
    if ($item=='_Quit') Gtk::main_quit();
    if ($item=='_About') about();
}

function about() {
    $dialog = new GtkAboutDialog();

    $dialog->set_name('About'); // note 2
    $dialog->set_version('1.0.2'); // note 3

    $dialog->set_comments("This is the comment line\n". 
    "Note tha tthe comments can span\nmultiple lines"); // note 4
    $dialog->set_copyright('Copyright (C) 2007-2008 kksou.com'); // note 5
    $dialog->set_license("Provide your license info here."); // note 6

    $pixbuf=GdkPixbuf::new_from_file("button_play1.png"); // note 7
    $dialog->set_logo($pixbuf); // note 8

    $dialog->set_website('http://www.kksou.com/php-gtk2'); //n4 note 9
    $dialog->set_authors(array("author1", "author2", "author3")); // note 10
    $dialog->set_translator_credits("Laguage 1 - Translator 1\n". // note 11
    "Language 2 - Translator 2");


    $dialog->run();
    $dialog->destroy();
}

?>

Output

As shown above.

 

Explanation

We make use of the code from How to set up menu and radio menu - Part 3 - add accelerators? to set up the menu.

What's new here:

  1. Set up the About menu item.
  2. Set the name.
  3. Set the version.
  4. Set the comments.
  5. Set the copyright info.
  6. Set the license info.
  7. Set the logo.
  8. Set the website.
  9. Set the authors.
  10. Set the translators.

Related Links

Add comment


Security code
Refresh