Problem
This is in response to ecc's post titled "How to retain control of Popups in php-gtk2?"
This example shows two levels of popup dialogs and how data is passed along between the dialogs as shown below:
Solution
- This example uses just plain simple GtkDialog.
- We make use of the codes from How to allow user to change font using GtkFontSelectionDialog? to display the font selection dialog.
- We make use of the codes from How to select a folder with GtkFileChooser? to display the path selection dialog.
Sample Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 16 17 18 19 20 21 22 23 24 25 26 27 28 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 48 49 50 51 52 53 54 55 56 57 58 59 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 80 81 82 83 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 109 110 111 112 113 114 115 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | <?php $window = new GtkWindow(); $window->set_size_request(480, 160); $window->connect_simple('destroy', array('Gtk','main_quit')); $window->add($vbox = new GtkVBox()); // display title $title = new GtkLabel("Retain control of Popup Dialogs"); $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); // setup button $hbox = new gtkHBox(); $vbox->pack_start($hbox, 0); $hbox->pack_start(new GtkLabel( " Press the Edit button to display the edit dialog: "), 0); $button = new Gtkbutton(" Edit "); $button->connect('clicked', 'on_edit_button'); $hbox->pack_start($button, 0); $label_font = new GtkLabel(); $label_path = new GtkLabel(); $vbox->pack_start($label_font); $vbox->pack_start($label_path); $window->show_all(); Gtk::main(); function on_edit_button($button) { global $label_font, $label_path; $prompt = new Prompt(); // note 1 $font = $prompt->entry_font->get_text(); // note 4 $path = $prompt->entry_path->get_text(); // note 5 $label_font->set_text("font: $font"); // note 6 $label_path->set_text("path: $path"); // note 6 } // wrapper for class GetFont function get_font() { // note 1 $getfont_dialog = new GetFont(); // note 2 $font_name = $getfont_dialog->main(); return $font_name; } class GetFont{ // note 1 function GetFont() { $dialog = new GtkFontSelectionDialog('Select Font'); $this->dialog = $dialog; $dialog->show_all(); } function main() { $this->dialog->run(); $fontname = $this->dialog->get_font_name(); $this->dialog->destroy(); return $fontname; } } class Prompt{ var $entry; // the user input function Prompt() { $dialog = new GtkDialog('Prompt', null, Gtk::DIALOG_MODAL); $top_area = $dialog->vbox; $hbox = new GtkHBox(); $top_area->pack_start($hbox, 0, 0); $hbox->pack_start(new GtkLabel('Font: '), 0); $this->entry_font = new GtkEntry(); $hbox->pack_start($this->entry_font, 0, 0); $hbox->pack_start(new GtkLabel(' '), 0, 0); $hbox->pack_start($font_button = new GtkButton('Select Font'), 0, 0); $font_button->connect('clicked', array($this, 'on_button_font')); $hbox2 = new GtkHBox(); $top_area->pack_start($hbox2, 0, 0); $hbox2->pack_start(new GtkLabel('Path: '), 0); $this->entry_path = new GtkEntry(); $hbox2->pack_start($this->entry_path, 0, 0); $hbox2->pack_start(new GtkLabel(' '), 0, 0); $hbox2->pack_start($font_button = new GtkButton('Select Path'), 0, 0); $font_button->connect('clicked', array($this, 'on_button_path')); $dialog->add_button(Gtk::STOCK_OK, Gtk::RESPONSE_OK); $buttons = $dialog->action_area->get_children(); $button_ok = $buttons[0]; // get the ID of the OK button // simulate button click when user press enter $dialog->set_has_separator(false); $dialog->show_all(); $dialog->run(); $dialog->destroy(); } function on_button_font($button) { $selected_font = get_font(); echo "selected_font = $selected_font\n"; $this->entry_font->set_text($selected_font); } function on_button_path($button) { $dialog = new GtkFileChooserDialog("Select Folder", // note 3 null, Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER, array(Gtk::STOCK_OK, Gtk::RESPONSE_OK), null); $dialog->show_all(); if ($dialog->run() == Gtk::RESPONSE_OK) { $selected_folder = $dialog->get_filename(); echo "selected_folder= $selected_folder\n"; $this->entry_path->set_text($selected_folder); } $dialog->destroy(); } } ?> |
Output
As shown above.Explanation
- Display the first popup dialog to prompt for font and path.
- Display the second level font selection dialog.
- Display the second level path selection dialog.
- Get the selected font.
- Get the selected path.
- Display the selected font and path on root window.
Read more...