summaryrefslogtreecommitdiff
path: root/framework/Web/UI/WebControls/TStyle.php
blob: e1e92b1ba179e9e0102cc2d9f13e1df0c4c812eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/**
 * TStyle class file.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link http://www.xisc.com/
 * @copyright Copyright &copy; 2004-2005, Qiang Xue
 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
 * @version $Revision: $  $Date: $
 * @package System.Web.UI.WebControls
 */

/**
 * TStyle class
 *
 * TStyle encapsulates the CSS style applied to a control.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @version $Revision: $  $Date: $
 * @package System.Web.UI.WebControls
 * @since 3.0
 */
class TStyle extends TComponent
{
	/**
	 * @var array The enumerable type for border styles
	 */
	public static $ENUM_BORDER_STYLE=array('NotSet','None','Dashed','Dotted','Solid','Double','Groove','Ridge','Inset','Outset');

	/**
	 * Various CSS fields
	 */
	const FLD_BACKCOLOR=0;
	const FLD_BORDERCOLOR=1;
	const FLD_BORDERWIDTH=2;
	const FLD_BORDERSTYLE=3;
	const FLD_FONT=4;
	const FLD_FORECOLOR=5;
	const FLD_HEIGHT=6;
	const FLD_WIDTH=7;
	const FLD_CSSCLASS=8;
	const FLD_STYLE=9;

	/**
	 * @var array storage of CSS fields
	 */
	private $_data=array();

	/**
	 * @return string the background color of the control
	 */
	public function getBackColor()
	{
		return isset($this->_data[self::FLD_BACKCOLOR])?$this->_data[self::FLD_BACKCOLOR]:'';
	}

	/**
	 * @param string the background color of the control
	 */
	public function setBackColor($value)
	{
		if($value==='')
			unset($this->_data[self::FLD_BACKCOLOR]);
		else
			$this->_data[self::FLD_BACKCOLOR]=$value;
	}

	/**
	 * @return string the border color of the control
	 */
	public function getBorderColor()
	{
		return isset($this->_data[self::FLD_BORDERCOLOR])?$this->_data[self::FLD_BORDERCOLOR]:'';
	}

	/**
	 * @param string the border color of the control
	 */
	public function setBorderColor($value)
	{
		if($value==='')
			unset($this->_data[self::FLD_BORDERCOLOR]);
		else
			$this->_data[self::FLD_BORDERCOLOR]=$value;
	}

	/**
	 * @return string the border style of the control
	 */
	public function getBorderStyle()
	{
		return isset($this->_data[self::FLD_BORDERSTYLE])?$this->_data[self::FLD_BORDERSTYLE]:'';
	}

	/**
	 * Sets the border style of the control.
	 * Valid values include: 
	 * 'NotSet','None','Dashed','Dotted','Solid','Double','Groove','Ridge','Inset','Outset'
	 * @param string the border style of the control
	 */
	public function setBorderStyle($value)
	{
		if($value==='')
			unset($this->_data[self::FLD_BORDERSTYLE]);
		else
			$this->_data[self::FLD_BORDERSTYLE]=TPropertyValue::ensureEnum($value,self::$ENUM_BORDER_STYLE);
	}

	/**
	 * @return string the border width of the control
	 */
	public function getBorderWidth()
	{
		return isset($this->_data[self::FLD_BORDERWIDTH])?$this->_data[self::FLD_BORDERWIDTH]:'';
	}

	/**
	 * @param string the border width of the control
	 */
	public function setBorderWidth($value)
	{
		if($value==='')
			unset($this->_data[self::FLD_BORDERWIDTH]);
		else
			$this->_data[self::FLD_BORDERWIDTH]=$value;
	}

	/**
	 * @return string the CSS class of the control
	 */
	public function getCssClass()
	{
		return isset($this->_data[self::FLD_CSSCLASS])?$this->_data[self::FLD_CSSCLASS]:'';
	}

	/**
	 * @param string the name of the CSS class of the control
	 */
	public function setCssClass($value)
	{
		if($value==='')
			unset($this->_data[self::FLD_CSSCLASS]);
		else
			$this->_data[self::FLD_CSSCLASS]=$value;
	}

	/**
	 * @return TFont the font of the control
	 */
	public function getFont()
	{
		if(!isset($this->_data[self::FLD_FONT]))
			$this->_data[self::FLD_FONT]=new TFont;
		return $this->_data[self::FLD_FONT];
	}

	/**
	 * @return string the foreground color of the control
	 */
	public function getForeColor()
	{
		return isset($this->_data[self::FLD_FORECOLOR])?$this->_data[self::FLD_FORECOLOR]:'';
	}

	/**
	 * @param string the foreground color of the control
	 */
	public function setForeColor($value)
	{
		if($value==='')
			unset($this->_data[self::FLD_FORECOLOR]);
		else
			$this->_data[self::FLD_FORECOLOR]=$value;
	}

	/**
	 * @return string the height of the control
	 */
	public function getHeight()
	{
		return isset($this->_data[self::FLD_HEIGHT])?$this->_data[self::FLD_HEIGHT]:'';
	}

	/**
	 * @param string the height of the control
	 */
	public function setHeight($value)
	{
		if($value==='')
			unset($this->_data[self::FLD_HEIGHT]);
		else
			$this->_data[self::FLD_HEIGHT]=$value;
	}

	/**
	 * @return string the custom style of the control
	 */
	public function getStyle()
	{
		return isset($this->_data[self::FLD_STYLE])?$this->_data[self::FLD_STYLE]:'';
	}

	/**
	 * @param string the custom style of the control
	 */
	public function setStyle($value)
	{
		if($value==='')
			unset($this->_data[self::FLD_STYLE]);
		else
			$this->_data[self::FLD_STYLE]=$value;
	}

	/**
	 * @return string the width of the control
	 */
	public function getWidth()
	{
		return isset($this->_data[self::FLD_WIDTH])?$this->_data[self::FLD_WIDTH]:'';
	}

	/**
	 * @param string the width of the control
	 */
	public function setWidth($value)
	{
		if($value==='')
			unset($this->_data[self::FLD_WIDTH]);
		else
			$this->_data[self::FLD_WIDTH]=$value;
	}

	/**
	 * @param boolean if the style contains nothing
	 */
	public function getIsEmpty()
	{
		return empty($this->_data) || (isset($this->_data[self::FLD_FONT]) && $this->_data[self::FLD_FONT]->getIsEmpty());
	}

	/**
	 * Resets the style to the original empty state.
	 */
	public function reset()
	{
		$this->_data=array();
		$this->flags=0;
	}

	/**
	 * Merges the current style with another one.
	 * If the two styles have the same style field, the new one
	 * will overwrite the current one.
	 * @param TStyle the new style
	 */
	public function mergeWith($style)
	{
		if($style===null)
			return;
		if(isset($style->_data[self::FLD_BACKCOLOR]))
			$this->_data[self::FLD_BACKCOLOR]=$style->_data[self::FLD_BACKCOLOR];
		if(isset($style->_data[self::FLD_BORDERCOLOR]))
			$this->_data[self::FLD_BORDERCOLOR]=$style->_data[self::FLD_BORDERCOLOR];
		if(isset($style->_data[self::FLD_BORDERWIDTH]))
			$this->_data[self::FLD_BORDERWIDTH]=$style->_data[self::FLD_BORDERWIDTH];
		if(isset($style->_data[self::FLD_BORDERSTYLE]))
			$this->_data[self::FLD_BORDERSTYLE]=$style->_data[self::FLD_BORDERSTYLE];
		if(isset($style->_data[self::FLD_FORECOLOR]))
			$this->_data[self::FLD_FORECOLOR]=$style->_data[self::FLD_FORECOLOR];
		if(isset($style->_data[self::FLD_HEIGHT]))
			$this->_data[self::FLD_HEIGHT]=$style->_data[self::FLD_HEIGHT];
		if(isset($style->_data[self::FLD_WIDTH]))
			$this->_data[self::FLD_WIDTH]=$style->_data[self::FLD_WIDTH];
		if(isset($style->_data[self::FLD_FONT]))
			$this->getFont()->mergeWith($style->_data[self::FLD_FONT]);
		if(isset($style->_data[self::FLD_CSSCLASS]))
			$this->_data[self::FLD_CSSCLASS]=$style->_data[self::FLD_CSSCLASS];
	}

	/**
	 * Copies from a style.
	 * Existing style will be reset first.
	 * @param TStyle the new style
	 */
	public function copyFrom($style)
	{
		$this->reset();
		$this->mergeWith($style);
	}

	/**
	 * Converts the style into a string representation suitable for rendering.
	 * @return string the string representation of the style
	 */
	public function toString()
	{
		if($this->getIsEmpty())
			return '';
		if(($str=$this->getStyle())!=='')
			$str=rtrim($str).';';
		if(isset($this->_data[self::FLD_BACKCOLOR]))
			$str.='background-color:'.$this->_data[self::FLD_BACKCOLOR].';';
		if(isset($this->_data[self::FLD_BORDERCOLOR]))
			$str.='border-color:'.$this->_data[self::FLD_BORDERCOLOR].';';
		if(isset($this->_data[self::FLD_BORDERWIDTH]))
			$str.='border-width:'.$this->_data[self::FLD_BORDERWIDTH].';';
		if(isset($this->_data[self::FLD_BORDERSTYLE]))
			$str.='border-style:'.$this->_data[self::FLD_BORDERSTYLE].';';
		if(isset($this->_data[self::FLD_FORECOLOR]))
			$str.='color:'.$this->_data[self::FLD_FORECOLOR].';';
		if(isset($this->_data[self::FLD_HEIGHT]))
			$str.='height:'.$this->_data[self::FLD_HEIGHT].';';
		if(isset($this->_data[self::FLD_WIDTH]))
			$str.='width:'.$this->_data[self::FLD_WIDTH].';';
		if(isset($this->_data[self::FLD_FONT]))
			$str.=$this->_data[self::FLD_FONT]->toString();
		return $str;
	}

	/**
	 * Adds attributes related to CSS styles to renderer.
	 * @param THtmlTextWriter the writer used for the rendering purpose
	 */
	public function addAttributesToRender($writer)
	{
		$str=$this->toString();
		if($str!=='')
			$writer->addAttribute('style',$str);
		if(isset($this->_data[self::FLD_CSSCLASS]))
			$writer->addAttribute('class',$this->_data[self::FLD_CSSCLASS]);
	}
}

?>