All pastes #261633 Raw Edit

Untitled

public text v1 · immutable
#261633 ·published 2006-11-29 18:52 UTC
rendered paste body
<?php

/**
 * Class for handling multipul GtkWindow objects
 * @author Leon Pegg <leon.pegg@gmail.com>
 */
class Application {

	/**
	 * GtkWindow object array
     *   Structure:
     *      [string GtkWindow_Name]
     *          array(
     *            GtkWindow - 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;
	}
	
	/**
	 * Removes a GtkWindow object from Application::$GtkWindows
	 *
	 * @param string $GtkWindow_Name
	 * @return GtkWindow
	 */
	public static function remove_window($GtkWindow_Name) {
		if ($GtkWindow_Name !== '' && array_key_exists($GtkWindow_Name, self::$GtkWindows)) {
			$GtkWindow = self::$GtkWindows[$GtkWindow_Name];
			unset(self::$GtkWindows[$GtkWindow_Name]);
			return $GtkWindow;
		}
		return false;
	}

	/**
	 * Retrives 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;
	}

	/**
	 * Retrives Application::$GtkWindows infomation array
     *   Structure:
     *      [int window_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;
	}

	/**
	 * Shows all windows managed by Application
	 *
	 */
	public static function show_all_windows() {
		foreach (self::$GtkWindows as $GtkWindow) {
			$GtkWindow->show_all();
		}
	}

	/**
	 * Hides all windows managed by Application
	 *
	 */
	public static function hide_all_windows() {
		foreach (self::$GtkWindows as $GtkWindow) {
			$GtkWindow->hide_all();
		}
	}

	public static function is_window($GtkWindow_Name) {
		return array_key_exists($GtkWindow_Name, self::$GtkWindows);
	}

}

function window($GtkWindow_Name) {
	return Application::window($GtkWindow_Name);
}

class wnd_main extends GtkWindow {
	
	protected $_widgets = array();
	
	public function __construct(){
		parent::__construct();
		parent::set_name('wnd_main');
		parent::set_title('wnd_main');
		parent::connect_simple('destroy',array($this,'_destroy'));
		$this->_widgets['vbox'] = new GtkVbox();
		$this->_widgets['vbox']->set_name('vbox');
		$this->_widgets['btn_add'] = new GtkButton('Add Window');
		$this->_widgets['btn_add']->connect_simple('clicked',array($this,'btn_add_clicked'));
		$this->_widgets['btn_remove'] = new GtkButton('Remove Window');
		$this->_widgets['btn_remove']->connect_simple('clicked',array($this,'btn_remove_clicked'));
		$this->_widgets['btn_show'] = new GtkButton('Show All Windows');
		$this->_widgets['btn_show']->connect_simple('clicked',array($this,'btn_show_clicked'));
		$this->_widgets['btn_hide'] = new GtkButton('Hide All Windows');
		$this->_widgets['btn_hide']->connect_simple('clicked',array($this,'btn_hide_clicked'));
		$this->_widgets['ent_name'] = new GtkEntry();
		$this->_widgets['vbox']->pack_start($this->_widgets['ent_name']);
		$this->_widgets['vbox']->pack_start($this->_widgets['btn_add']);
		$this->_widgets['vbox']->pack_start($this->_widgets['btn_remove']);
		$this->_widgets['vbox']->pack_start($this->_widgets['btn_show']);
		$this->_widgets['vbox']->pack_start($this->_widgets['btn_hide']);
		parent::add($this->_widgets['vbox']);
		parent::show_all();
	}
	
	public function widget($name) {
		return $this->_widgets[$name];
	}
	
	public function btn_add_clicked() {
		if ('' !== $this->widget('ent_name')->get_text() && !Application::is_window($this->widget('ent_name')->get_text())) {
			Application::add_window(new multi($this->widget('ent_name')->get_text()));
		}
	}
	
	public function btn_remove_clicked() {
		if (Application::is_window($this->widget('ent_name')->get_text())) {
			Application::window($this->widget('ent_name')->get_text())->hide_all();
			$GtkWindow = Application::remove_window($this->widget('ent_name')->get_text());
			$GtkWindow->destroy();
		}
	}
	
	public function _destroy(){
		echo func_name();
		Gtk::main_quit();
	}
	
	public function btn_show_clicked() {
		Application::show_all_windows();
	}
	
	public function btn_hide_clicked() {
		Application::hide_all_windows();
		parent::show_all();
	}
	
}

class multi extends GtkWindow {
	public function __construct($name) {
		parent::__construct();
		parent::set_name($name);
		parent::set_title($name);
		parent::connect_simple('delete-event',array($this,'delete_event'));
		parent::connect_simple('destroy',array($this,'_destroy'));
	}
	
	public function _destroy() {
		echo func_name();
	}
	
	public function delete_event(){
		return true;
	}
}

Application::add_window(new wnd_main());

Gtk::main();

?>