Location: PHPKode > projects > Aukyla Platform > base/Windows.php
<?php
/*
     Windows.php, provides classes for managing multiple windows
     Copyright (C) 2005 Arend van Beelen, Auton Rijnsburg

     This program is free software; you can redistribute it and/or modify it
     under the terms of the GNU General Public License as published by the Free
     Software Foundation; either version 2 of the License, or (at your option)
     any later version.

     This program is distributed in the hope that it will be useful, but WITHOUT
     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
     more details.

     You should have received a copy of the GNU General Public License along
     with this program; if not, write to the Free Software Foundation, Inc.,
     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

     For any questions, comments or whatever, you may mail me at: hide@address.com
*/

require_once('Config.php');
require_once('Constants.php');
require_once('Decoration.php');
require_once('GUI.php');
require_once('Locale.php');
require_once('Output.php');
require_once('Widgets.php');

/**
 * @brief Manages multiple windows.
 *
 * The window manager has a critical role in selecting which window is to be
 * shown. For instance, the user can likely request several different windows
 * through your application's interface. The window manager then catches this
 * request and shows the correct window. If no special requests are done, the
 * main window will be shown.
 *
 * @sa MainWindow, Window
 *
 * @since Aukyla 1.1
 */
class WindowManager extends RootContainer
{

	/**
	 * Returns a window manager instance.
	 *
	 * @return Instance of the window manager.
	 */
	public static function instance()
	{
		if(self::$instance == null)
		{
			self::$instance = new WindowManager();
		}

		return self::$instance;
	}

	/**
	 * Constructor.
	 *
	 * @note Only one WindowManager class can be created. You should use
	 *       instance() to get this instance.
	 *
	 * @sa instance()
	 */
	public function __construct()
	{
		if(self::$instance != null)
		{
			die('There can be only one instance of WindowManager!');
		}

		parent::__construct();

		$this->metaTags = '';
		$this->requestedWindow = Config::request('window');
	}

	/**
	 * Returns the requested window.
	 *
	 * @return ID of the requested window or an empty string if the default
	 *         window was requested.
	 */
	public function window()
	{
		return $this->requestedWindow;
	}

	/**
	 * Shows the requested window.
	 */
	public function show()
	{
		if($this->requestedWindow != '')
		{
			foreach($this->children as $child)
			{
				if($child->id() == $this->requestedWindow)
				{
					$child->show();
					return;
				}
			}

			die(i18n('The requested window does not exist.'));
		}

		MainWindow::instance()->show();
	}

	protected $children;

	private static $instance = null;
	private $requestedWindow;
}

/**
 * @internal
 *
 * @brief Class for CSS stylesheets.
 */
class Stylesheet
{
	function __construct($css, $media = 'screen')
	{
		$this->css = $css;
		$this->media = $media;
	}

	public $css;
	public $media;
}

/**
 * @brief The minimal class for creating new windows
 *
 * The Window class is used for creating applications which use multiple
 * windows. If you want more control to define your own custom windows, you
 * should use this class. Every application should have at least one MainWindow.
 *
 * @sa MainWindow
 *
 * @since Aukyla 1.1
 */
class Window extends RootContainer
{
	/**
	 * Constructor.
	 *
	 * Creates a new window.
	 *
	 * @param parent The WindowManager which will manage this window.
	 * @param id     The ID of the window.
	 * @param title  Optional window title of the window.
	 */
	public function __construct($parent, $id, $title = '')
	{
		parent::__construct($id);
		if($parent !== null)
		{
			$parent->addWidget($this);
		}

		$this->windowTag = 'window';

		$this->javaScriptHeaders = new RootContainer();
		$this->menus = array();
		$this->stylesheets = array();
		$this->title = $title;
	}

	/**
	 * Returns the title of this window.
	 *
	 * @return The window title of this window.
	 *
	 * @sa setTitle()
	 */
	public function title()
	{
		return $this->title;
	}

	/**
	 * Returns the title of this window.
	 *
	 * @return The window title of this window.
	 *
	 * @sa title()
	 */
	public function setTitle($title)
	{
		$this->title = $title;
	}

	/**
	 * Returns the set shortcut icon.
	 *
	 * @return The link to the set shortcut icon.
	 *
	 * @sa setShortcutIcon()
	 */
	public function shortcutIcon()
	{
		return $this->shortcutIcon;
	}

	/**
	 * Sets a link to a PNG image acting as shortcut icon.
	 *
	 * @param shortcutIcon The URL to the shortcut icon. This can be either
	 *                     an absolute URL, or a URL relative to the
	 *                     document root of the webserver.
	 *
	 * @sa shortcutIcon()
	 */
	public function setShortcutIcon($shortcutIcon)
	{
		$this->shortcutIcon = $shortcutIcon;
	}

	/**
	 * Adds a reference to a CSS stylesheet to the main window.
	 *
	 * @note A reference to the CSS sheet of the current theme is added
	 *       automatically.
	 *
	 * @param css   The URL to the CSS file. This can be either an absolute
	 *              URL, or a URL relative to the document root of the
	 *              webserver.
	 * @param media Media to which the CSS sheet applies.
	 */
	public function addStylesheet($css, $media = 'screen')
	{
		$this->stylesheets[] = new Stylesheet($css, $media);
	}

	/**
	 * @deprecated Use addExternalJavaScript() instead.
	 */
	public function addJavaScriptReference($url, $id = '')
	{
		$this->addExternalJavaScript($url, $id);
	}

	/**
	 * Adds a JavaScript reference.
	 *
	 * @param url URL to the JavaScript source file to reference.
	 * @param id  Optional ID to attach to the script.
	 *
	 * @sa addJavaScript()
	 */
	public function addExternalJavaScript($url, $id = '')
	{
		$externalScript = new ExternalJavaScript($this->javaScriptHeaders, $url);
		if($id != '')
		{
			$externalScript->setId($id);
		}
	}

	/**
	 * @deprecated Use addJavaScript() instead.
	 */
	public function addJavaScriptBlock($code, $id = '')
	{
		$this->addJavaScript($code, $id);
	}

	/**
	 * Adds a JavaScript code block.
	 *
	 * @param code The JavaScript code to include.
	 * @param id   Optional ID to attach to the script.
	 *
	 * @sa addExternalJavaScript()
	 */
	public function addJavaScript($code, $id = '')
	{
		$script = new JavaScript($this->javaScriptHeaders, $code);
		if($id != '')
		{
			$script->setId($id);
		}
	}

	/**
	 * Registers a menu to the window.
	 *
	 * You shouldn't have to call this function yourself as the constructor
	 * of Menu will already call this function for you.
	 *
	 * @param menu The menu to register.
	 *
	 * @sa mainMenu(), menu()
	 */
	public function registerMenu(Menu $menu)
	{
		$this->menus[] = $menu;
	}

	/**
	 * Returns the window's main menu.
	 *
	 * @note The main menu is nothing more than the first registered menu.
	 *
	 * @return The first registered menu object, or NULL if no menus have been
	 *         registered yet.
	 *
	 * @sa registerMenu(), menu()
	 */
	public function mainMenu()
	{
		if(sizeof($this->menus) == 0)
		{
			return null;
		}

		return $this->menus[0];
	}

	/**
	 * Returns the window's menu with the given @p id.
	 *
	 * @param id ID of the menu to return.
	 *
	 * @return The requested menu object, or @p null if it could not be
	 *         found.
	 *
	 * @sa registerMenu(), mainMenu()
	 */
	public function menu($id)
	{
		foreach($this->menus as $menu)
		{
			if($menu->id() == $id)
			{
				return $menu;
			}
		}

		return null;
	}

	public function show()
	{
		$theme = Decoration::theme();
		$frontend = Config::globals('frontend');
		if(file_exists(AUKYLA_DIR."/htdocs/resources/plugins/Frontends/$frontend/themes/$theme.css"))
		{
			$theme = "plugins/Frontends/$frontend/themes/$theme.css";
		}
		else if(file_exists(AUKYLA_DIR."/htdocs/resources/base/themes/$theme.css"))
		{
			$theme = "base/themes/$theme.css";
		}
		else
		{
			$theme = '';
		}

		Output::write("<{$this->windowTag} xmlns=\"http://www.auton.nl/aukyla/output/1.0\">".
		              '<metadata>'.
		              "<caption>{$this->title}</caption>");
		if(isset($this->shortcutIcon))
		{
			Output::write("<link rel=\"shortcut icon\" href=\"{$this->shortcutIcon}\" type=\"image/png\" />");
		}
		Output::write($theme == '' ? '' : "<link rel=\"stylesheet\" href=\"resources/$theme\" media=\"screen\" type=\"text/css\" />");
		foreach($this->stylesheets as $stylesheet)
		{
			Output::write("<link rel=\"stylesheet\" href=\"{$stylesheet->css}\" media=\"{$stylesheet->media}\" type=\"text/css\" />");
		}
		$this->javaScriptHeaders->show();
		Output::write($this->metaTags.
		              '</metadata>'.
		              '<content'.$this->parentProperties().'>');
		parent::show();
		Output::write('</content>'.
		              "</{$this->windowTag}>");
	}

	/**
	 * Set this variable to insert custom meta tags.
	 */
	protected $metaTags;

	/**
	 * Set this variable to a different value if your subclass should use a
	 * different tag name as root. Default = 'window'.
	 */
	protected $windowTag;

	private $javaScriptHeaders;
	private $language;
	private $menus;
	private $shortcutIcon;
	private $stylesheets;
	private $title;
}

?>
Return current item: Aukyla Platform