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 as shown below:
Solution
- In this Part 1, we make use of GtkDialog instead of GtkWindow.
- With GtkDialog, you do not need to worry about how to pass the GTK main loop back and forth between app A and app B.
- The use of GtkDialog::run() and GtkDialog::destroy() simplify things a lot.
Sample Code
1 2 5 6 8 9 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 36 37 38 39 40 41 42 44 45 47 49 50 51 53 54 57 58 59 60 61 62 63 64 65 66 67 68 69 72 73 74 | <?php setup_appA(); // note 1 function setup_appA() { $dialog = new GtkDialog(); $dialog->set_size_request(400, 200); $vbox = $dialog->vbox; // display title $title = new GtkLabel("Opening app B from app A\n". " Part 1 - using GtkDialog"); $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', $dialog, 'A'); $dialog->set_has_separator(false); $dialog->show_all(); $dialog->run(); $dialog->destroy(); } function on_click($button, $dialog, $id) { echo "on_click: $id!\n"; $dialog->destroy(); // note 2 if ($id=='A') { setup_appB(); // note 3 } elseif ($id='B') { setup_appA(); // note 4 } } function setup_appB() { $dialog = new GtkDialog(); $dialog->set_size_request(200, 100); $vbox = $dialog->vbox; $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 B'), 0); $hbox->pack_start(new GtkLabel()); $button->connect('clicked', 'on_click', $dialog, 'B'); $dialog->set_has_separator(false); $dialog->show_all(); $dialog->run(); } ?> |
Output
As shown above.
Explanation
- Set up and display app A.
- To launch app B, we first destroy dialog A.
- Then set up and display app B.
- From app B back to app A, we first destroy dialog B, then set up and display app A.
Read more...