Sample Code 405: How to open one window from another window - Part 2 - using GtkWindow? |
|
Written by kksou
|
|
Thursday, 10 January 2008 |
|
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:

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 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 29 30 31 32 33 34 35 36 37 38 39 40 41
| <?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() {
|
- 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
- Set up and display app A.
- To launch app B, we first destroy app A (which in the process also stops the main loop of app A).
- Then set up and display app B.
- 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
User reviews There are no user reviews yet. Note: You have to be a registered member to leave a comment. Free registration here. |