<?php
/**
*+-----------------------------------------------------------------------+
*| MenuBar - 07 Oct 2006 |
*+-----------------------------------------------------------------------+
*| Diego do Nascimento Feitosa |
*| hide@address.com |
*| www.dnfeitosa.com |
*| São Paulo/SP - Brasil |
*+-----------------------------------------------------------------------+
*| MenuBar 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. |
*| |
*| MenuBar 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 MenuBar; if not, write to the Free Software |
*| Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
*| 02111-1307 USA |
*+-----------------------------------------------------------------------+
**/
require_once("MBFactory.inc");
class MenuBar extends GtkMenuBar {
protected $xml;
protected $widgets = array();
protected $addTearoffs = true;
public function __construct() {
parent::__construct();
$this->fct = MBFactory::getFactory();
}
public function createFromString($string) {
$this->xml = DOMDocument::loadXML($string);
$this->startBuilding();
}
public function createFromFile($file) {
$this->xml = DOMDocument::load($file);
$this->startBuilding();
}
private function startBuilding() {
if ($this->xml->validate()) {
$nodes = $this->xml->documentElement->childNodes;
$this->build($this, $nodes);
} else {
throw new Exception("Invalid XML format.");
}
}
protected function build($obj, $nodes) {
foreach ($nodes as $node) {
if ($node->nodeType != 1)
continue;
$ret = $this->buildItems($node);
$obj->append($ret);
}
}
protected function buildItems($node, $type = null) {
$fct = $this->fct;
if (!$type)
$type = $node->nodeName;
$id = $node->getAttribute("id");
if ($type != "separator" && is_null($id)) {
throw new Exception("Item without id");
}
$display = $node->getAttribute("display");
$icon = $node->getAttribute("icon");
$tearoff = $node->getAttribute("tearoff") == "true";
switch ($type) {
case "item":
if (!is_null($display)) {
if (!is_null($icon)) {
$item = $fct->imageMenuItem($display);
$image = $fct->icon($icon);
$item->set_image($image);
} else {
$item = $fct->menuItem($display);
}
} else if (!is_null($icon)) {
$item = $fct->imageMenuItem($icon);
} else {
throw new Exception("Each item should have at least an icon or display text");
}
$this->register($id, $item);
return $item;
case "separator":
return $fct->itemSeparator();
default:
$menu = $fct->menu();
if ($tearoff && $this->addTearoffs)
$menu->append($fct->tearoff());
$this->build($menu, $node->childNodes);
$item = $this->buildItems($node, "item");
$item->set_submenu($menu);
return $item;
}
}
protected function register($id, $obj) {
if (!array_key_exists($id, $this->widgets))
$this->widgets[$id] = $obj;
else
throw new Exception("There are currently a widget with the same id");
}
public function getWidget($id) {
if (array_key_exists($id, $this->widgets))
return $this->widgets[$id];
return null;
}
public function addTearoffs($add) {
$this->addTearoffs = $add;
}
}
?>