Location: PHPKode > projects > Aukyla Platform > base/Menus.php
<?php
/*
     Menu.php, module which adds a simple menu
     Copyright (C) 2003 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('GUI.php');
require_once('JavaScript.php');
require_once('Output.php');
require_once('Widgets.php');

/**
 * @brief A simple menu.
 *
 * A simple class that collects menu entries and shows them.
 */
class Menu extends Container
{
	public function __construct(Container $parent, $id = '')
	{
		parent::__construct($parent, $id);
		MainWindow::instance()->registerMenu($this);

		$this->sorted = true;
	}

	/**
	 * @copydoc Container::addWidget()
	 */
	public function addWidget(MenuEntry $widget)
	{
		parent::addWidget($widget);
		$this->sorted = false;
	}

	/**
	 * Show the menu with all entries registered thus far. You should be
	 * aware though that this function does not print an entire menu, but
	 * just a list of entries. You should provide any layout yourself using
	 * CSS. The entries are displayed as simple links with class="entry".
	 */
	public function show()
	{
		$this->sortEntries();

		Output::write('<menu'.$this->parentProperties().'>');
		parent::show();
		Output::write('</menu>');
	}

	// sorts the entries before showing them
	private function sortEntries()
	{
		if($this->sorted == true)
		{
			return;
		}

		// bubble sort 'm (this is not very time critical)
		for($i = 1; $i < sizeof($this->children); $i++)
		{
			if($this->children[$i]->priority() > $this->children[$i - 1]->priority())
			{
				$tmp = $this->children[$i];
				$this->children[$i] = $this->children[$i - 1];
				$this->children[$i - 1] = $tmp;
				if($i > 1)
				{
					$i -= 2;
				}
			}
		}

		$this->sorted = true;
	}

	protected $children;
	protected $id;

	private $sorted;
}

/**
 * @brief A menu entry.
 */
class MenuEntry extends RawContainer
{
	/**
	 * Constructor.
	 *
	 * @param parent Menu to add this entry to.
	 * @param text   Text of the entry as shown to the user.
	 */
	public function __construct($parent, $text = '')
	{
		if(!is_a($parent, 'Menu') && !is_a($parent, 'SubMenu'))
		{
			die('Can only add a MenuEntry to a Menu or SubMenu!');
		}

		parent::__construct($parent);

		$this->setCssClass('entry');
		$this->setText($text);

		$this->link = '';
		$this->priority = 0;
	}

	/**
	 * Sets the link this menu entry points to.
	 *
	 * @param link Link where this entry will bring you if clicked upon.
	 */
	public function setLink($link)
	{
		$this->link = $link;
	}

	/**
	 * Sets the priority of the entry.
	 *
	 * The priority can be both a positive or a negative integer, the higher the
	 * priority, the higher the entry will be listed in the menu.
	 *
	 * @param priority The new priority of the entry.
	 *
	 * @sa priority()
	 */
	public function setPriority($priority)
	{
		$this->priority = $priority;
	}

	/**
	 * Returns the priority of the entry.
	 *
	 * @return The priority, 0 means a neutral priority.
	 *
	 * @sa setPriority()
	 */
	public function priority()
	{
		return $this->priority;
	}

	public function show()
	{
		$hrefProperty = ($this->link != '' ? " href=\"{$this->link}\"" : '');

		Output::write("<menuitem label=\"{$this->text}\"$hrefProperty".$this->parentProperties().'>');
		Container::show();
		Output::write('</menuitem>');
	}

	protected $cssClass;
	protected $cssStyle;
	protected $text;

	private $link;
	private $priority;
}

/**
 * @brief Menu head entry.
 */
class MenuHead extends MenuEntry
{
	public function __construct($parent, $text = '')
	{
		parent::__construct($parent, $text);

		$this->setCssClass('head');
	}
}

/**
 * @brief A submenu.
 *
 * Sub-menu's are added to a regular Menu just like a MenuEntry. When the user
 * moves their mouse over them, the sub-menu unfolds and shows its entries.
 *
 *
 */
class SubMenu extends MenuEntry
{
	/**
	 * @copydoc Container::__construct()
	 */
	public function __construct($parent, $id)
	{
		parent::__construct($parent);

		$parent->setCssClass('aukyla-menu-system');
		$this->setCssClass('submenu');

		if($this->subMenuUsed == false)
		{
			$mainWindow = MainWindow::instance();
			$mainWindow->addStylesheet('resources/base/Menus/cssjsmenudhtml.css');
			$mainWindow->addStylesheet('resources/base/Menus/cssjsmenuhover.css');
		}

		$this->subMenuUsed = true;

		$this->setId($id);
	}

	/**
	 * Sets the link this menu entry points to.
	 *
	 * @param link Link where this entry will bring you if clicked upon.
	 */
	public function setLink($link)
	{
		$this->link = $link;
	}

	public function show()
	{
		Output::write("<submenu href=\"{$this->link}\" label=\"{$this->text}\"".$this->parentProperties().'>');
		Container::show();
		Output::write('</submenu>');
	}

	protected $children;
	protected $text;

	private $link;
	private static $subMenuUsed = false;
}

?>
Return current item: Aukyla Platform