Intro
This code snippet shows one of the many ways to handle multiple windows in
PHP-GTK 2. In the following example will build 3 classes
(Application, GtkWindow_One
and GtkWindow_Two).
Class : Application
This class will implement 3 static methods for the managment of
GtkWindow classes. A more complete version of this
class is available at
php-gtk.pastebin.com
<?php
/**
* Class for handling multiple GtkWindow objects
* @author Leon Pegg <leon.pegg@gmail.com>
*/
class Application {
/**
* GtkWindow object array
* Structure:
* [int window_id]
* array(
* GtkWindow_Name - Store GtkWindow object
* )
*
* @var array GtkWindow
*/
protected static $GtkWindows = array();
private function __construct() {
}
/**
* Adds a GtkWindow object to Application::$GtkWindows
*
* @param GtkWindow $GtkWindow
* @return bool
*/
public static function add_window(GtkWindow $GtkWindow) {
$GtkWindow_Name = $GtkWindow->get_name();
if ($GtkWindow_Name !== '' && !array_key_exists($GtkWindow_Name,
self::$GtkWindows)) {
self::$GtkWindows[$GtkWindow_Name] = $GtkWindow;
return true;
}
return false;
}
/**
* Retrieves GtkWindow object from Application::$GtkWindows
*
* @param string $GtkWindow_Name
* @return GtkWindow
*/
public static function window($GtkWindow_Name) {
if ($GtkWindow_Name !== '' && array_key_exists($GtkWindow_Name,
self::$GtkWindows)) {
return self::$GtkWindows[$GtkWindow_Name];
}
}
/**
* Retrieves Application::$GtkWindows infomation array
* Structure:
* [int server_id]
* array(
* name - Name of GtkWindow
* class - Name of GtkWindow class
* )
*
* @return array
*/
public static function list_windows() {
$Window_List = array();
foreach (self::$GtkWindows as $GtkWindow_Name => $Class) {
$Class_Name = get_class($Class);
$Window_List[] = array(
'name' => $GtkWindow_Name,
'class' => $Class_Name);
}
return $Window_List;
}
}
?>
Method Application::add_window
This method is used to add instances of GtkWindow
or derived from GtkWindow to the Application.
- Sucsesful : returns true
- Failed : returns false
This method will fail in two cases:
- The GtkWindow name has not been set
- The GtkWindow name is already defined in the
Application::$GtkWindows array.
Method Application::window
This method is used to retrieve instances of
GtkWindow or derived from it.
- Successful : returns
GtkWindow instance
- Failed : returns
false
This method will fail in one case:
- The GtkWindow name does not exist in the
Application::$GtkWindows array.
Method Application::list_windows
This method is used to retrieve an array of infomation
about the current GtkWindow or objects
derived from GtkWindow objects in the
Application::$GtkWindows.
Information is returned in the following format:
array(
array(
'name' => [GtkWindow name],
'class' => [Class name]
)
)
Class : GtkWindow_One
... about the class ...
<?php
class GtkWindow_One extends GtkWindow {
protected $widgets = array();
public function __construct() {
parent::__construct();
parent::set_name('wnd_one');
$this->build_ui();
Application::add_window($this);
}
protected function build_ui() {
$btn_show = new GtkButton("Show window two");
$btn_show->set_name('btn_show');
parent::add($btn_show);
$this->widgets['btn_show'] = $btn_show;
$btn_show->connect_simple('clicked',
array($this, 'clicked_btn_show'));
parent::connect_simple('destroy',
array('Gtk', 'main_quit'));
}
public function widget($widget_name) {
if ($widget_name !== '' && array_key_exists($widget_name,
$this->widgets)) {
return $this->widgets[$widget_name];
}
return false;
}
public function clicked_btn_show() {
if (Application::window('wnd_two') !== false) {
Application::window('wnd_two')->show_all();
}
}
}
?>
Class : GtkWindow_Two
... about the class ...
<?php
class GtkWindow_Two extends GtkWindow {
protected $widgets = array();
public function __construct() {
parent::__construct();
parent::set_name('wnd_two');
$this->build_ui();
Application::add_window($this);
}
protected function build_ui() {
$btn_hide = new GtkButton("Hide window");
$btn_hide->set_name('btn_hide');
parent::add($btn_hide);
$this->widgets['btn_hide'] = $btn_hide;
$btn_hide->connect_simple('clicked',
array($this, 'clicked_btn_hide'));
parent::connect_simple('delete-event',
array($this, 'delete_event'));
}
public function widget($widget_name) {
if ($widget_name !== '' && array_key_exists($widget_name,
$this->widgets)) {
return $this->widgets[$widget_name];
}
return false;
}
public function clicked_btn_hide() {
parent::hide_all();
}
public function delete_event() {
parent::hide_all();
return true;
}
}
?>
Putting it all together
<?php
/**
* A class to handle multiple GtkWindow objects
* @author Leon Pegg <leon.pegg@gmail.com>
*/
class Application {
/**
* GtkWindow object array
* Structure:
* [int window_id]
* array(
* GtkWindow_Name - Store GtkWindow object
* )
*
* @var array GtkWindow
*/
protected static $GtkWindows = array();
private function __construct() {
}
/**
* Adds a GtkWindow object to Application::$GtkWindows
*
* @param GtkWindow $GtkWindow
* @return bool
*/
public static function add_window(GtkWindow $GtkWindow) {
$GtkWindow_Name = $GtkWindow->get_name();
if ($GtkWindow_Name !== ''
&& !array_key_exists($GtkWindow_Name, self::$GtkWindows)) {
self::$GtkWindows[$GtkWindow_Name] = $GtkWindow;
return true;
}
return false;
}
/**
* Retrieves GtkWindow object from Application::$GtkWindows
*
* @param string $GtkWindow_Name
* @return GtkWindow
*/
public static function window($GtkWindow_Name) {
if ($GtkWindow_Name !== '' &&
array_key_exists($GtkWindow_Name, self::$GtkWindows)) {
return self::$GtkWindows[$GtkWindow_Name];
}
return false;
}
/**
* Retrieves Application::$GtkWindows infomation array
* Structure:
* [int server_id]
* array(
* name - Name of GtkWindow
* class - Name of GtkWindow class
* )
*
* @return array
*/
public static function list_windows() {
$Window_List = array();
foreach (self::$GtkWindows as $GtkWindow_Name => $Class) {
$Class_Name = get_class($Class);
$Window_List[] = array(
'name' => $GtkWindow_Name,
'class' => $Class_Name);
}
return $Window_List;
}
}
class GtkWindow_One extends GtkWindow {
protected $widgets = array();
public function __construct() {
parent::__construct();
parent::set_name('wnd_one');
$this->build_ui();
Application::add_window($this);
}
protected function build_ui() {
$btn_show = new GtkButton("Show window two");
$btn_show->set_name('btn_show');
parent::add($btn_show);
$this->widgets['btn_show'] = $btn_show;
$btn_show->connect_simple('clicked',
array($this, 'clicked_btn_show'));
parent::connect_simple('destroy',
array('Gtk', 'main_quit'));
}
public function widget($widget_name) {
if ($widget_name !== ''
&& array_key_exists($widget_name, $this->widgets)) {
return $this->widgets[$widget_name];
}
return false;
}
public function clicked_btn_show() {
if (Application::window('wnd_two') !== false) {
Application::window('wnd_two')->show_all();
}
}
}
class GtkWindow_Two extends GtkWindow {
protected $widgets = array();
public function __construct() {
parent::__construct();
parent::set_name('wnd_two');
$this->build_ui();
Application::add_window($this);
}
protected function build_ui() {
$btn_hide = new GtkButton("Hide window");
$btn_hide->set_name('btn_hide');
parent::add($btn_hide);
$this->widgets['btn_hide'] = $btn_hide;
$btn_hide->connect_simple('clicked',
array($this, 'clicked_btn_hide'));
parent::connect_simple('delete-event',
array($this, 'delete_event'));
}
public function widget($widget_name) {
if ($widget_name !== ''
&& array_key_exists($widget_name, $this->widgets)) {
return $this->widgets[$widget_name];
}
return false;
}
public function clicked_btn_hide() {
parent::hide_all();
}
public function delete_event() {
parent::hide_all();
return true;
}
}
new GtkWindow_One();
new GtkWindow_Two();
Application::window('wnd_one')->show_all();
Gtk::main();
?>
Recent comments
6 weeks 12 hours ago
11 weeks 6 days ago
13 weeks 4 days ago
20 weeks 9 hours ago
21 weeks 2 days ago
21 weeks 3 days ago
22 weeks 4 days ago
23 weeks 3 days ago
34 weeks 1 day ago
34 weeks 2 days ago