1<?php
2# SIMPLE: SIMPLE Layout Engine V. 1.0
3# (c) by Dietrich Wittenberg 03/2008
4# E-Mail: info@wwwittenberg.de
5# classes to define a css-layout with boxes
6# layout is defined only by an xml-file
7# creates an css-structure
8#
9# you may use this file for free but you need to
10# let this copyright - info unchanged !!!
11
12class mybox {
13
14	function mybox ($id, $top, $right, $bottom, $left, $type="px") {
15		$this->id			=$id;
16		$this->top		=$top;
17		$this->right	=$right;
18		$this->bottom	=$bottom;
19		$this->left		=$left;
20		$this->type		=$type;
21	}
22	function make() {
23		$out[]="#".$this->id;
24		$out[]="{";
25		if (($align=$this->top->align) == $this->bottom->align)
26		{ // fixed height
27			if     ($align == "top")	// up side
28				{
29				$out[]="top     : " . $this->top->val . $this->type .";";
30				$out[]="height  : " . ($this->bottom->val - $this->top->val) . $this->type .";";
31				}
32			else
33				{ 													// bottom side
34				$out[]="bottom  : " . $this->bottom->val . $this->type .";";
35				$out[]="height  : " . ($this->top->val - $this->bottom->val) . $this->type .";";
36				}
37		}
38		else
39		{	// calculated height
40			$out[]  ="top     : " . $this->top->val . $this->type .";";
41			$out[]  ="height  : expression(document.body.clientHeight +(" . (-$this->bottom->val - $this->top->val) . ") +\"" . $this->type . "\")" .";";
42			$out[]  ="bottom  : " . $this->bottom->val . $this->type . "; /* Firefox / IE7 */";
43		}
44		if (($align=$this->left->align) == $this->right->align)
45		{ // fixed height
46			if     ($align == "left")	// left side
47				{
48				$out[]="left    : " . $this->left->val . $this->type .";";
49				$out[]="width   : " . ($this->right->val - $this->left->val) . $this->type .";";
50				}
51			else
52				{														// right side
53				$out[]="right   : " . $this->right->val . $this->type .";";
54				$out[]="width   : " . ($this->left->val - $this->right->val) . $this->type .";";
55				}
56		}
57		else
58		{	// calculated width
59			$out[]  ="left    : " . $this->left->val  . $this->type .";";
60			$out[]  ="width   : expression(document.body.clientWidth +(" . (-$this->right->val - $this->left->val) . ")+ \"" . $this->type . "\")" .";";
61			$out[]  ="right   : " . $this->right->val . $this->type . "; /* Firefox / IE7 */";
62		}
63		if (is_string($this->style) && ($this->style != ""))
64			$out[]=$this->style. ";";
65		else
66			if (is_array($this->style))
67				foreach ($this->style as $c)
68					$out[]=$c.";";
69
70		$out[]="}";
71		return $out;
72	}
73}
74class myline {
75	function myline($val, $align) {
76		$this->val=$val;
77		$this->align=$align;
78	}
79}
80
81class layout {
82
83	function layout($file) {
84		$this->lines=0;
85		$this->boxes=0;
86		$this->loadxml($file);
87	}
88	function addline($id, $val=0, $align) {
89		$this->line[$id]=new myline($val, $align);
90	}
91	function addlineval($id, $val=0) {
92		$this->line[$id]->val=$val;
93	}
94	function addlines($vals, $align) {
95		foreach ($vals as $id => $val)
96			$this->addline($id, $val, $align);
97	}
98	function addbox($id, $top, $right, $bottom, $left) {
99		$this->box[$id]=new mybox($id, $this->line[$top], $this->line[$right], $this->line[$bottom], $this->line[$left], $this->type);
100	}
101	function addboxstyle($id, $style) {
102		$this->box[$id]->style[]=$style;
103	}
104	function outExp($pre, $style) {
105		foreach($this->box as $box)
106			$tmp[]=$pre."#".$box->id;
107		$out=implode(",\n", $tmp)."\n{\n".$style."\n}\n";
108		return $out;
109	}
110	function outall() {
111		$out =$this->outExp("", "position: fixed; \noverflow: auto;  /* Firefox */ ");
112		$out.=$this->outExp("* html ", "position: absolute; \nz-index : 1;  /* IE6 */ ");
113		foreach($this->box as $box)
114			$out.=implode("\n", $box->make())."\n";
115		echo $out;
116	}
117	function loadxml($file) {
118		$this->parser = xml_parser_create();
119		xml_set_object($this->parser, $this);
120		xml_set_element_handler($this->parser, "startElement", "endElement");
121		xml_set_character_data_handler($this->parser, "cdata");
122
123		if (!($this->fp = fopen($file, "r")))   die("could not open XML input");
124	}
125
126	function parsexml() {
127		while ($data = fread($this->fp, 4096)) {
128		    if (!xml_parse($this->parser, $data, feof($this->fp))) {
129		        die(sprintf("XML error: %s at line %d",
130		                    xml_error_string(xml_get_error_code($this->parser)),
131		                    xml_get_current_line_number($this->parser)));
132		    }
133		}
134		xml_parser_free($this->parser);
135	}
136	function startElement($parser, $name, $attrs) {
137
138		$this->tag=$name;
139		switch ($name) {
140			case "BOX":
141				$this->boxid    =(isset($attrs["ID"]))     ? $attrs["ID"]     : $this->boxindex++;
142				$this->boxtop   =(isset($attrs["TOP"]))    ? $attrs["TOP"]    : 0;
143				$this->boxright =(isset($attrs["RIGHT"]))  ? $attrs["RIGHT"]  : 0;
144				$this->boxbottom=(isset($attrs["BOTTOM"])) ? $attrs["BOTTOM"] : 0;
145				$this->boxleft  =(isset($attrs["LEFT"]))   ? $attrs["LEFT"]   : 0;
146				$this->addbox($this->boxid , $this->boxtop, $this->boxright, $this->boxbottom, $this->boxleft);
147			break;
148			case "LINE":
149				$this->lineid   =(isset($attrs["ID"]))    ? $attrs["ID"]    : $this->lineindex++;
150				$this->linealign=(isset($attrs["ALIGN"])) ? $attrs["ALIGN"] : "top";
151			break;
152			case "LINES":
153				$this->linealign=(isset($attrs["ALIGN"])) ? $attrs["ALIGN"] : "top";
154			break;
155			}
156	}
157	function endElement($parser, $name) {
158		switch ($name) {
159			case "DIMENSION":
160				$this->type=$this->inhalt;
161			break;
162			case "LINE":
163				$this->addline($this->lineid, $this->inhalt, $this->linealign);
164			break;
165			case "LINES":
166				$this->addlines(explode(",", $this->inhalt), $this->linealign);
167			break;
168			case "STYLE":
169				$this->addboxstyle($this->boxid, $this->inhalt);
170			break;
171		}
172	}
173
174	function cdata($parser, $element_inhalt) {
175		//$inhalt=preg_replace("/^[ ]*([^\s]*)[ \s]*$/","$1",$element_inhalt);
176		$inhalt=trim($element_inhalt);
177
178		if ($inhalt!="") {
179				$this->inhalt=$inhalt;
180		}
181	}
182}
183
184function createcss($file) {
185	$csslayout=new layout($file);
186	$csslayout->parsexml();
187	$csslayout->outall($explorer);
188}
189
190?>
191