Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TWebControl class file.
4
 *
5
 * @author Qiang Xue <qiang.xue@gmail.com>
6
 * @link http://www.pradosoft.com/
7
 * @copyright Copyright &copy; 2005-2008 PradoSoft
8
 * @license http://www.pradosoft.com/license/
9
 * @version $Id: TWebControl.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Web.UI.WebControls
11
 */
12
 
13
/**
14
 * Includes TStyle and TWebAdapter definition
15
 */
16
Prado::using('System.Web.UI.WebControls.TStyle');
17
Prado::using('System.Web.UI.WebControls.TWebControlAdapter');
18
 
19
/**
20
 * TWebControl class
21
 *
22
 * TWebControl is the base class for controls that share a common set
23
 * of UI-related properties and methods. TWebControl-derived controls
24
 * are usually associated with HTML tags. They thus have tag name, attributes
25
 * and body contents. You can override {@link getTagName} to specify the tag name,
26
 * {@link addAttributesToRender} to specify the attributes to be rendered,
27
 * and {@link renderContents} to customize the body content rendering.
28
 * TWebControl encapsulates a set of properties related with CSS style fields,
29
 * such as {@link getBackColor BackColor}, {@link getBorderWidth BorderWidth}, etc.
30
 *
31
 * Subclasses of TWebControl typically needs to override {@link addAttributesToRender}
32
 * and {@link renderContents}. The former is used to render the attributes
33
 * of the HTML tag associated with the control, while the latter is to render
34
 * the body contents enclosed within the HTML tag.
35
 *
36
 * @author Qiang Xue <qiang.xue@gmail.com>
37
 * @version $Id: TWebControl.php 2541 2008-10-21 15:05:13Z qiang.xue $
38
 * @package System.Web.UI.WebControls
39
 * @since 3.0
40
 */
41
class TWebControl extends TControl implements IStyleable
42
{
43
	/**
44
	 * Copies basic control attributes from another control.
45
	 * Properties including AccessKey, ToolTip, TabIndex, Enabled
46
	 * and Attributes are copied.
47
	 * @param TWebControl source control
48
	 */
49
	public function copyBaseAttributes(TWebControl $control)
50
	{
51
		$this->setAccessKey($control->getAccessKey());
52
		$this->setToolTip($control->getToolTip());
53
		$this->setTabIndex($control->getTabIndex());
54
		if(!$control->getEnabled())
55
			$this->setEnabled(false);
56
		if($control->getHasAttributes())
57
			$this->getAttributes()->copyFrom($control->getAttributes());
58
	}
59
 
60
	/**
61
	 * @return string the access key of the control
62
	 */
63
	public function getAccessKey()
64
	{
65
		return $this->getViewState('AccessKey','');
66
	}
67
 
68
	/**
69
	 * Sets the access key of the control.
70
	 * Only one-character string can be set, or an exception will be raised.
71
	 * Pass in an empty string if you want to disable access key.
72
	 * @param string the access key to be set
73
	 * @throws TInvalidDataValueException if the access key is specified with more than one character
74
	 */
75
	public function setAccessKey($value)
76
	{
77
		if(strlen($value)>1)
78
			throw new TInvalidDataValueException('webcontrol_accesskey_invalid',get_class($this),$value);
79
		$this->setViewState('AccessKey',$value,'');
80
	}
81
 
82
	/**
83
	 * @return string the background color of the control
84
	 */
85
	public function getBackColor()
86
	{
87
		if($style=$this->getViewState('Style',null))
88
			return $style->getBackColor();
89
		else
90
			return '';
91
	}
92
 
93
	/**
94
	 * @param string the background color of the control
95
	 */
96
	public function setBackColor($value)
97
	{
98
		$this->getStyle()->setBackColor($value);
99
	}
100
 
101
	/**
102
	 * @return string the border color of the control
103
	 */
104
	public function getBorderColor()
105
	{
106
		if($style=$this->getViewState('Style',null))
107
			return $style->getBorderColor();
108
		else
109
			return '';
110
	}
111
 
112
	/**
113
	 * @param string the border color of the control
114
	 */
115
	public function setBorderColor($value)
116
	{
117
		$this->getStyle()->setBorderColor($value);
118
	}
119
 
120
	/**
121
	 * @return string the border style of the control
122
	 */
123
	public function getBorderStyle()
124
	{
125
		if($style=$this->getViewState('Style',null))
126
			return $style->getBorderStyle();
127
		else
128
			return '';
129
	}
130
 
131
	/**
132
	 * @param string the border style of the control
133
	 */
134
	public function setBorderStyle($value)
135
	{
136
		$this->getStyle()->setBorderStyle($value);
137
	}
138
 
139
	/**
140
	 * @return string the border width of the control
141
	 */
142
	public function getBorderWidth()
143
	{
144
		if($style=$this->getViewState('Style',null))
145
			return $style->getBorderWidth();
146
		else
147
			return '';
148
	}
149
 
150
	/**
151
	 * @param string the border width of the control
152
	 */
153
	public function setBorderWidth($value)
154
	{
155
		$this->getStyle()->setBorderWidth($value);
156
	}
157
 
158
	/**
159
	 * @return TFont the font of the control
160
	 */
161
	public function getFont()
162
	{
163
		return $this->getStyle()->getFont();
164
	}
165
 
166
	/**
167
	 * @return string the foreground color of the control
168
	 */
169
	public function getForeColor()
170
	{
171
		if($style=$this->getViewState('Style',null))
172
			return $style->getForeColor();
173
		else
174
			return '';
175
	}
176
 
177
	/**
178
	 * @param string the foreground color of the control
179
	 */
180
	public function setForeColor($value)
181
	{
182
		$this->getStyle()->setForeColor($value);
183
	}
184
 
185
	/**
186
	 * @return string the height of the control
187
	 */
188
	public function getHeight()
189
	{
190
		if($style=$this->getViewState('Style',null))
191
			return $style->getHeight();
192
		else
193
			return '';
194
	}
195
 
196
	/**
197
	 * @param TDisplayStyle display style of the control, default is TDisplayStyle::Fixed
198
	 */
199
	public function setDisplay($value)
200
	{
201
		$this->getStyle()->setDisplayStyle($value);
202
	}
203
 
204
	/**
205
	 * @return TDisplayStyle display style of the control, default is TDisplayStyle::Fixed
206
	 */
207
	public function getDisplay()
208
	{
209
		return $this->getStyle()->getDisplayStyle();
210
	}
211
 
212
	/**
213
	 * @param string the css class of the control
214
	 */
215
	public function setCssClass($value)
216
	{
217
		$this->getStyle()->setCssClass($value);
218
	}
219
 
220
	/**
221
	 * @return string the css class of the control
222
	 */
223
	public function getCssClass()
224
	{
225
		if($style=$this->getViewState('Style',null))
226
			return $style->getCssClass();
227
		else
228
			return '';
229
	}
230
 
231
	/**
232
	 * @param string the height of the control
233
	 */
234
	public function setHeight($value)
235
	{
236
		$this->getStyle()->setHeight($value);
237
	}
238
 
239
	/**
240
	 * @return boolean whether the control has defined any style information
241
	 */
242
	public function getHasStyle()
243
	{
244
		return $this->getViewState('Style',null)!==null;
245
	}
246
 
247
	/**
248
	 * Creates a style object to be used by the control.
249
	 * This method may be overriden by controls to provide customized style.
250
	 * @return TStyle the default style created for TWebControl
251
	 */
252
	protected function createStyle()
253
	{
254
		return new TStyle;
255
	}
256
 
257
	/**
258
	 * @return TStyle the object representing the css style of the control
259
	 */
260
	public function getStyle()
261
	{
262
		if($style=$this->getViewState('Style',null))
263
			return $style;
264
		else
265
		{
266
			$style=$this->createStyle();
267
			$this->setViewState('Style',$style,null);
268
			return $style;
269
		}
270
	}
271
 
272
	/**
273
	 * Sets the css style string of the control.
274
	 * The style string will be prefixed to the styles set via other control properties (e.g. Height, Width).
275
	 * @param string the css style string
276
	 * @throws TInvalidDataValueException if the parameter is not a string
277
	 */
278
	public function setStyle($value)
279
	{
280
		if(is_string($value))
281
			$this->getStyle()->setCustomStyle($value);
282
		else
283
			throw new TInvalidDataValueException('webcontrol_style_invalid',get_class($this));
284
	}
285
 
286
	/**
287
	 * Removes all style data.
288
	 */
289
	public function clearStyle()
290
	{
291
		$this->clearViewState('Style');
292
	}
293
 
294
	/**
295
	 * @return integer the tab index of the control
296
	 */
297
	public function getTabIndex()
298
	{
299
		return $this->getViewState('TabIndex',0);
300
	}
301
 
302
	/**
303
	 * Sets the tab index of the control.
304
	 * Pass 0 if you want to disable tab index.
305
	 * @param integer the tab index to be set
306
	 */
307
	public function setTabIndex($value)
308
	{
309
		$this->setViewState('TabIndex',TPropertyValue::ensureInteger($value),0);
310
	}
311
 
312
	/**
313
	 * Returns the tag name used for this control.
314
	 * By default, the tag name is 'span'.
315
	 * You can override this method to provide customized tag names.
316
	 * @return string tag name of the control to be rendered
317
	 */
318
	protected function getTagName()
319
	{
320
		return 'span';
321
	}
322
 
323
	/**
324
	 * @return string the tooltip of the control
325
	 */
326
	public function getToolTip()
327
	{
328
		return $this->getViewState('ToolTip','');
329
	}
330
 
331
	/**
332
	 * Sets the tooltip of the control.
333
	 * Pass an empty string if you want to disable tooltip.
334
	 * @param string the tooltip to be set
335
	 */
336
	public function setToolTip($value)
337
	{
338
		$this->setViewState('ToolTip',$value,'');
339
	}
340
 
341
	/**
342
	 * @return string the width of the control
343
	 */
344
	public function getWidth()
345
	{
346
		if($style=$this->getViewState('Style',null))
347
			return $style->getWidth();
348
		else
349
			return '';
350
	}
351
 
352
	/**
353
	 * @param string the width of the control
354
	 */
355
	public function setWidth($value)
356
	{
357
		$this->getStyle()->setWidth($value);
358
	}
359
 
360
	/**
361
	 * Adds attribute name-value pairs to renderer.
362
	 * By default, the method will render 'id', 'accesskey', 'disabled',
363
	 * 'tabindex', 'title' and all custom attributes.
364
	 * The method can be overriden to provide customized attribute rendering.
365
	 * @param THtmlWriter the writer used for the rendering purpose
366
	 */
367
	protected function addAttributesToRender($writer)
368
	{
369
		if($this->getID()!=='')
370
			$writer->addAttribute('id',$this->getClientID());
371
		if(($accessKey=$this->getAccessKey())!=='')
372
			$writer->addAttribute('accesskey',$accessKey);
373
		if(!$this->getEnabled())
374
			$writer->addAttribute('disabled','disabled');
375
		if(($tabIndex=$this->getTabIndex())>0)
376
			$writer->addAttribute('tabindex',"$tabIndex");
377
		if(($toolTip=$this->getToolTip())!=='')
378
			$writer->addAttribute('title',$toolTip);
379
		if($style=$this->getViewState('Style',null))
380
			$style->addAttributesToRender($writer);
381
		if($this->getHasAttributes())
382
		{
383
			foreach($this->getAttributes() as $name=>$value)
384
				$writer->addAttribute($name,$value);
385
		}
386
	}
387
 
388
	/**
389
	 * Renders the control.
390
	 * This method overrides the parent implementation by replacing it with
391
	 * the following sequence:
392
	 * - {@link renderBeginTag}
393
	 * - {@link renderContents}
394
	 * - {@link renderEndTag}
395
	 * @param THtmlWriter the writer used for the rendering purpose
396
	 */
397
	public function render($writer)
398
	{
399
		$this->renderBeginTag($writer);
400
		$this->renderContents($writer);
401
		$this->renderEndTag($writer);
402
	}
403
 
404
	/**
405
	 * Renders the openning tag for the control (including attributes)
406
	 * @param THtmlWriter the writer used for the rendering purpose
407
	 */
408
	public function renderBeginTag($writer)
409
	{
410
		$this->addAttributesToRender($writer);
411
		$writer->renderBeginTag($this->getTagName());
412
	}
413
 
414
	/**
415
	 * Renders the body content enclosed between the control tag.
416
	 * By default, child controls and text strings will be rendered.
417
	 * You can override this method to provide customized content rendering.
418
	 * @param THtmlWriter the writer used for the rendering purpose
419
	 */
420
	public function renderContents($writer)
421
	{
422
		parent::renderChildren($writer);
423
	}
424
 
425
	/**
426
	 * Renders the closing tag for the control
427
	 * @param THtmlWriter the writer used for the rendering purpose
428
	 */
429
	public function renderEndTag($writer)
430
	{
431
		$writer->renderEndTag();
432
	}
433
}
434