Mastering Glade for PHP-GTK2
PHP-GTK2 Cookbook Vol.2
PHP-GTK2 Cookbook Vol.1
Intro to Glade for PHP-GTK2
PHP-GTK2 Demystified
PHP-GTK ExplorerYour "secret weapon" for learning, exploring, understanding and mastering PHP-GTK2!
[Free Download Here]
|
|
|
| Application Template 02 - Multiple Modules in Multiple Windows |
|
This is an extension of Application Template 01 with the following new features:
- The five modules (including the main menu) each now has different window size.
- The application now "traps" the window close event. If you close the window in any of the four application modules, the user will be returned to the main menu. The applicatin window is not closed, but only hidden. In Application Template 01, closing the window will exit the application entirely.
- You can exit the application by closing the main menu window, or by pressing F12.
- Have added a menu bar on top. You can also activate each module through this menu.

Note
The Code
The codes below produces exactly the same output as How to run multiple applications in multiple windows - Part 2?. The only difference is that it's re-written using classes.
Note: If you have installed php-gtk2 using Gnope Installer on Windows, and if running the sample code below gives you warning that the Symbolic names for keys (e.g. Gdk::KEY_Return) is not defined, you might want to update your php-gtk2 with the latest php-gtk2.dll available here. Simply download the php-gtk2.dll and replace the copy in the folder php-gtk2\ext. The latest compilation has put in the Symbolic names for keys listed here. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 17 18 19 20 21 22 23 24 25 26 27 28 29 30 32 33 34 39 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 101 102 103 104 105 106 107 108 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 201 202 203 204 205 206 208 209 210 211 212 213
| <?php
$app = new App(); $app->main();
class App {
function App() { $this->modules = array("main", "sales", "purchase", "inventory", "finance"); // names of the modules
}
function main() { foreach($this->modules as $module) { $this->apps[$module] = new $module($this); $this->apps[$module]->dialog->connect('delete-event', array( &$this, "on_delete_event"), $module); $this->apps[$module]->dialog->connect('key-press-event', array( &$this, "on_key"), $module); } $this->apps['main']->dialog->show_all(); // show the first module
$this->apps['main']->dialog->run(); // let's go!
}
// process button click
function on_clicked($button, $activated_module) { print "button_clicked: $activated_module\n"; $this->apps[$activated_module]->dialog->show_all(); // show the activated module
foreach($this->modules as $module) { if ($module!=$activated_module) $this->apps[$module]->dialog->hide_all(); // hide all others
} $this->apps[$activated_module]->dialog->run(); }
function on_delete_event($widget, $event, $module) { if ($module=='main') { $this->apps['main']->dialog->destroy(); } else { $this->apps[$module]->dialog->hide(); $this->apps['main']->dialog->show_all(); // show the main menu
$this->apps['main']->dialog->run(); } return true; }
function on_key($widget, $event, $module) { global $apps; if ($event->keyval==Gdk::KEY_F12) { if ($module=='main') { $this->apps['main']->dialog->destroy(); } else { $this->apps[$module]->dialog->hide(); $this->apps['main']->dialog->show_all(); // show the main menu
$this->apps['main']->dialog->run(); } } }
// process menu item selection
function on_menu_select($menu_item) { $item = $menu_item->child->get_label(); echo "menu selected: $item\n"; if ($item=='_Quit') { $this->apps['main']->dialog->destroy(); } else { $module = strtolower($item); print "module = $module\n"; $this->apps['main']->dialog->hide(); $this->apps[$module]->dialog->show_all(); $this->apps[$module]->dialog->run(); // let's go!
} }
}
class base_module {
function base_module($obj) { $this->main = $obj; // keep a copy of the module names
$module = get_class($this); print "base_module::module = $module\n"; $dialog = new GtkDialog($module, null, Gtk::DIALOG_MODAL); $dialog->set_position(Gtk::WIN_POS_CENTER_ALWAYS); $dialog->set_size_request(400, 200); $top_area = $dialog->vbox; $this->dialog = $dialog; // keep a copy of the dialog ID
// run the setup for each module
$this->setup($top_area);
$top_area->pack_start(new GtkLabel()); // used to "force" the button to stay at bottom
$this->show_buttons($top_area); // shows the buttons at bottom of windows
$dialog->set_has_separator(false); }
function show_buttons($vbox) {} }
class sales extends base_module { function setup($vbox) { $this->dialog->set_size_request(600, 400); $vbox->pack_start(new GtkLabel(get_class($this))); } }
class purchase extends base_module { function setup($vbox) { $this->dialog->set_size_request(200, 100); $vbox->pack_start(new GtkLabel(get_class($this))); } }
class inventory extends base_module { function setup($vbox) { $this->dialog->set_size_request(720, 640); $vbox->pack_start(new GtkLabel(get_class($this))); } }
class finance extends base_module { function setup($vbox) { $this->dialog->set_size_request(480, 360); $vbox->pack_start(new GtkLabel(get_class($this))); } }
class main extends base_module { function setup($vbox) { //define menu definition
$menu_definition = array( '_File' => array('_Quit'), '_Apps' => array('Sales', 'Purchase', 'Inventory', 'Finance') ); $this->setup_menu($vbox, $menu_definition);
$vbox->pack_start(new GtkLabel('This is main menu')); }
// shows the buttons at bottom of windows only for main_menu
function show_buttons($vbox) { global $modules; $hbox = new GtkHBox(); $vbox->pack_start($hbox, 0, 0); $hbox->pack_start(new GtkLabel()); foreach($this->main->modules as $module) { if ($module=='main') continue; // skip the main_menu button
$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 == get_class($this)) { // 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', array(&$this->main,'on_clicked'), $module); // event handler to handle button click
} }
// setup menu
function setup_menu($vbox, $menus) { $menubar = new GtkMenuBar(); $vbox->pack_start($menubar, 0, 0); foreach($menus as $toplevel => $sublevels) { $menubar->append($top_menu = new GtkMenuItem($toplevel)); $menu = new GtkMenu(); $top_menu->set_submenu($menu); foreach($sublevels as $submenu) { if ($submenu=='<hr>') { $menu->append(new GtkSeparatorMenuItem()); } else { $menu->append($menu_item = new GtkMenuItem($submenu)); $menu_item->connect('activate', array(&$this->main, 'on_menu_select')); } } } } }
?>
|
Related Articles
|
Search PHP-GTK2 Manual
Full-text search on php-gtk2 manual
|