405. How to open one window from another window - Part 2 - using GtkWindow?

Problem

Suppose you have set up an application that first displays a menu (let's call this app A). When the user selects an option, the corresponding application (let's call this B) will be launched.

In Part 1, we make use of GtkDialog.

In this Part 2, we use the standard GtkWindow as shown below:

How to open one window from another window - Part 2 - using GtkWindow?


Solution

  • Using GtkWindow isn't that complicated.
  • You just need to make sure that the GTK main loop of the first application is stopped before starting the main loop of the second application.

Sample Code

1   
2   
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   
29   
31   
32   
33   
34   
35   
36   
37   
38   
39   
40   
42   
43   
44   
45   
46   
47   
48   
49   
50   
51   
52   
53   
54   
55   
56   
57   
58   
59   
61   
62   
63   
<?php
setup_appA(); // note 1

function setup_appA() {
    $window = new GtkWindow();
    $window->set_size_request(400, 200);
    $window->connect_simple('destroy', array('Gtk','main_quit'));
    $window->add($vbox = new GtkVBox());

    // display title
    $title = new GtkLabel("Opening app B from app A\n".
    " Part 2 - using GtkWindow");
    $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);
    $vbox->pack_start($title, 0, 0);

    $vbox->pack_start(new GtkLabel('This is app A'));

    $vbox->pack_start($hbox = new GtkHBox(), 0);
    $hbox->pack_start(new GtkLabel());
    $hbox->pack_start($button = new GtkButton('Launch app B'), 0);
    $hbox->pack_start(new GtkLabel());
    $vbox->pack_start(new GtkLabel());
    $button->connect('clicked', 'on_click', $window, 'A');

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

function on_click($button, $window, $id) {
    echo "on_click: $id!\n";
    $window->destroy(); // note 2
    if ($id=='A') {
        setup_appB(); // note 3
    } elseif ($id='B') {
        setup_appA(); // note 4
    }
}

function setup_appB() {
    $window = new GtkWindow();
    $window->set_size_request(200, 100);
    $window->connect_simple('destroy', array('Gtk','main_quit'));
    $window->add($vbox = new GtkVBox());

    $vbox->pack_start(new GtkLabel('This is app B'));

    $vbox->pack_start($hbox = new GtkHBox(), 0);
    $hbox->pack_start(new GtkLabel());
    $hbox->pack_start($button = new GtkButton('Go back to app A'), 0);
    $hbox->pack_start(new GtkLabel());
    $button->connect('clicked', 'on_click', $window, 'B');

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

?>

Output

As shown above.

 

Explanation

  1. Set up and display app A.
  2. To launch app B, we first destroy app A (which in the process also stops the main loop of app A).
  3. Then set up and display app B.
  4. From app B back to app A, we first destroy app B, then set up and display app A.

Note

You might want to compare the above with How to open one window from another window - Part 1 - using GtkDialog? to get a better understanding of the differences between using GtkDialog and GtkWindow, and also how to manually start and stop the GTK main loop.

Related Links

Add comment


Security code
Refresh