Re: Closing one window without exiting the app
| From: | kksou | Date: | Fri, 13 Oct 2006 16:03:48 +0000 |
| Subject: | Re: Closing one window without exiting the app | ||
| References: | 1 | Groups: | php.gtk.general |
| Request: | Send a blank email to php-gtk-general+get-7353@lists.php.net to get a copy of this message | ||
Veronica A. Bendersky wrote:
>
> I am trying to do this:
>
> 1 - I open the main window of the app, which has a menubar in it
> 2 - When a menu item is clicked, another widow opens with the info
> regarding that menu item (e.g.: to manage customers if user clicked on
> Customers menu item). I do this by including another .phpw file with the
> code to open and work on the new window. I call this window $window1.
> 3 - This window $window1 has its own menubar, which has a Close menu item.
> When users click on that menu item, I want the window to close, so only
> the main window of the app remains.
>
> I have tried different ways and I can't get this to work properly. I
> either get errors or the whole app closes.
>
Hi Veronica,
You can try using the signal 'delete-event' to hide window1 instead of
closing it.
Alternatively, here's a sample code that you can try. It makes use of
dialogs to open 5 simultaneous windows (main menu+ 4 modules). Each time one
will be active, while the other four hidden. When the user close the window
while in any of the 4 modules, the user will be returned to the main menu.
<?php
$modules = array("main", "sales", "purchase", "inventory",
"finance"); //
names of the modules
$module_size = array(array(400,200), array(600,400),
array(200,100), array(720,640), array(480,360)); // szie of each module
window
$i=0;
foreach($modules as $module) {
$apps[$module] = setup_app($module, $module_size[$i]);
++$i;
}
$apps['main']->show_all(); // show the main menu
$apps['main']->run(); // let's go!
// setup_app
function setup_app($module, $window_size) {
$dialog = new GtkDialog($module, null, Gtk::DIALOG_MODAL);
$dialog->set_position(Gtk::WIN_POS_CENTER_ALWAYS);
$dialog->set_size_request($window_size[0], $window_size[1]);
$top_area = $dialog->vbox;
if ($module=='main') {
$top_area->pack_start(new GtkLabel("this is main menu"));
$top_area->pack_start(new GtkLabel("Press F12 to quit"));
} else {
$top_area->pack_start(new GtkLabel("this is $module module"));
$top_area->pack_start(new GtkLabel("Press F12 to return to main
menu"));
$top_area->pack_start(new GtkLabel("or just close the window"));
}
$top_area->pack_start(new GtkLabel()); // used to "force" the button to
stay at bottom
if ($module=='main') show_buttons($top_area, $module); // shows the
buttons at bottom of windows
$dialog->set_has_separator(false);
$dialog->connect('delete-event', "on_delete_event", $module); // note 1
$dialog->connect('key-press-event', "on_key", $module); // catch F12
return $dialog; // returns the ID of the dialog
}
// shows the buttons at bottom of windows
function show_buttons($vbox, $current_module) {
global $modules;
$hbox = new GtkHBox();
$vbox->pack_start($hbox, 0, 0);
$hbox->pack_start(new GtkLabel());
foreach($modules as $module) {
if ($module=='main') continue;
$button = new
GtkButton(strtoupper(substr($module,0,1)).substr($module,1)); // cap 1st
letter
$button->set_size_request(80, 32); // makes all button the same size
if ($module == $current_module) { // sets the color of the
respective button
$button->modify_bg(Gtk::STATE_NORMAL,
GdkColor::parse("#95DDFF"));
$button->modify_bg(Gtk::STATE_ACTIVE,
GdkColor::parse("#95DDFF"));
$button->modify_bg(Gtk::STATE_PRELIGHT,
GdkColor::parse("#95DDFF"));
}
$hbox->pack_start($button, 0, 0);
$hbox->pack_start(new GtkLabel());
$button->connect('clicked', 'on_clicked', $module); // event handler
to handle button click
}
}
// process button click
function on_clicked($button, $activated_module) {
global $apps, $modules;
print "button_clicked: $activated_module\n";
$apps[$activated_module]->show_all(); // show the activated module
foreach($modules as $module) {
if ($module!=$activated_module) $apps[$module]->hide_all(); // hide
all others
}
$apps[$activated_module]->run();
}
//process window close event
function on_delete_event($widget, $event, $module) {
global $apps;
if ($module=='main') { // exit app if in main menu
return false;
} else { // not in main menu
$apps[$module]->hide(); // hide the module
$apps['main']->show_all(); // show the main menu
$apps['main']->run();
return true;
}
}
function on_key($widget, $event, $module) {// note 2
global $apps;
if ($event->keyval==Gdk::KEY_F12) {
if ($module=='main') {
$apps['main']->destroy();
} else {
$apps[$module]->hide();
$apps['main']->show_all(); // show the first module
$apps['main']->run(); // let's go!
}
}
}
?>
Sample output:
http://www.kksou.com/php-gtk2/gif/0058.gif
More explanations at:
http://www.kksou.com/php-gtk2/articles/run-multiple-applications-in-multiple-windows---Part-2.php
If you would like to have an object-oriented version of this example using
classes, please refer to:
http://www.kksou.com/php-gtk2/apps/application-template-02-multiple-modules-in-multiple-windows/application-template-02-multiple-modules-in-multiple-windows.php
The oo version also includes the menu bar.
Regards,
/kksou
--
View this message in context: http://www.nabble.com/Closing-one-window-without-exiting-the-app-tf2433318.html#a6798700
Sent from the Php - GTK - General mailing list archive at Nabble.com.