3 * TWebControl class file.
5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link http://www.pradosoft.com/
7 * @copyright Copyright © 2005-2014 PradoSoft
8 * @license http://www.pradosoft.com/license/
9 * @package System.Web.UI.WebControls
13 * Includes TStyle and TWebAdapter definition
15 Prado::using('System.Web.UI.WebControls.TStyle');
16 Prado::using('System.Web.UI.WebControls.TWebControlAdapter');
17 Prado::using('System.Web.UI.WebControls.TWebControlDecorator');
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.
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.
36 * @author Qiang Xue <qiang.xue@gmail.com>
37 * @package System.Web.UI.WebControls
40 class TWebControl extends TControl implements IStyleable
43 * @var boolean ensures the inclusion the id in the tag rendering.
45 private $_ensureid=false;
48 * @var TWebControlDecorator this render things before and after both the open and close tag
50 protected $_decorator;
54 * Subclasses can override getEnsureId or just set this property. eg. If your subclass
55 * control does work with javascript and your class wants to flag that it requires an id
56 * to operate properly. Once set to true, it stays that way.
57 * @param boolean pass true to enable enforcement of the tag attribute id.
59 public function setEnsureId($value)
61 $this->_ensureid |= TPropertyValue::ensureBoolean($value);
65 * @return whether this web control must have an id
67 public function getEnsureId()
69 return $this->_ensureid;
73 * @return TWebControlDecorator
75 public function getDecorator($create=true)
77 if($create && !$this->_decorator)
78 $this->_decorator = Prado::createComponent('TWebControlDecorator', $this);
79 return $this->_decorator;
83 * Copies basic control attributes from another control.
84 * Properties including AccessKey, ToolTip, TabIndex, Enabled
85 * and Attributes are copied.
86 * @param TWebControl source control
88 public function copyBaseAttributes(TWebControl $control)
90 $this->setAccessKey($control->getAccessKey());
91 $this->setToolTip($control->getToolTip());
92 $this->setTabIndex($control->getTabIndex());
93 if(!$control->getEnabled())
94 $this->setEnabled(false);
95 if($control->getHasAttributes())
96 $this->getAttributes()->copyFrom($control->getAttributes());
100 * @return string the access key of the control
102 public function getAccessKey()
104 return $this->getViewState('AccessKey','');
108 * Sets the access key of the control.
109 * Only one-character string can be set, or an exception will be raised.
110 * Pass in an empty string if you want to disable access key.
111 * @param string the access key to be set
112 * @throws TInvalidDataValueException if the access key is specified with more than one character
114 public function setAccessKey($value)
117 throw new TInvalidDataValueException('webcontrol_accesskey_invalid',get_class($this),$value);
118 $this->setViewState('AccessKey',$value,'');
122 * @return string the background color of the control
124 public function getBackColor()
126 if($style=$this->getViewState('Style',null))
127 return $style->getBackColor();
133 * @param string the background color of the control
135 public function setBackColor($value)
137 $this->getStyle()->setBackColor($value);
141 * @return string the border color of the control
143 public function getBorderColor()
145 if($style=$this->getViewState('Style',null))
146 return $style->getBorderColor();
152 * @param string the border color of the control
154 public function setBorderColor($value)
156 $this->getStyle()->setBorderColor($value);
160 * @return string the border style of the control
162 public function getBorderStyle()
164 if($style=$this->getViewState('Style',null))
165 return $style->getBorderStyle();
171 * @param string the border style of the control
173 public function setBorderStyle($value)
175 $this->getStyle()->setBorderStyle($value);
179 * @return string the border width of the control
181 public function getBorderWidth()
183 if($style=$this->getViewState('Style',null))
184 return $style->getBorderWidth();
190 * @param string the border width of the control
192 public function setBorderWidth($value)
194 $this->getStyle()->setBorderWidth($value);
198 * @return TFont the font of the control
200 public function getFont()
202 return $this->getStyle()->getFont();
206 * @return string the foreground color of the control
208 public function getForeColor()
210 if($style=$this->getViewState('Style',null))
211 return $style->getForeColor();
217 * @param string the foreground color of the control
219 public function setForeColor($value)
221 $this->getStyle()->setForeColor($value);
225 * @return string the height of the control
227 public function getHeight()
229 if($style=$this->getViewState('Style',null))
230 return $style->getHeight();
236 * @param TDisplayStyle display style of the control, default is TDisplayStyle::Fixed
238 public function setDisplay($value)
240 $this->getStyle()->setDisplayStyle($value);
244 * @return TDisplayStyle display style of the control, default is TDisplayStyle::Fixed
246 public function getDisplay()
248 return $this->getStyle()->getDisplayStyle();
252 * @param string the css class of the control
254 public function setCssClass($value)
256 $this->getStyle()->setCssClass($value);
260 * @return string the css class of the control
262 public function getCssClass()
264 if($style=$this->getViewState('Style',null))
265 return $style->getCssClass();
271 * @param string the height of the control
273 public function setHeight($value)
275 $this->getStyle()->setHeight($value);
279 * @return boolean whether the control has defined any style information
281 public function getHasStyle()
283 return $this->getViewState('Style',null)!==null;
287 * Creates a style object to be used by the control.
288 * This method may be overriden by controls to provide customized style.
289 * @return TStyle the default style created for TWebControl
291 protected function createStyle()
297 * @return TStyle the object representing the css style of the control
299 public function getStyle()
301 if($style=$this->getViewState('Style',null))
305 $style=$this->createStyle();
306 $this->setViewState('Style',$style,null);
312 * Sets the css style string of the control.
313 * The style string will be prefixed to the styles set via other control properties (e.g. Height, Width).
314 * @param string the css style string
315 * @throws TInvalidDataValueException if the parameter is not a string
317 public function setStyle($value)
319 if(is_string($value))
320 $this->getStyle()->setCustomStyle($value);
322 throw new TInvalidDataValueException('webcontrol_style_invalid',get_class($this));
326 * Removes all style data.
328 public function clearStyle()
330 $this->clearViewState('Style');
334 * @return integer the tab index of the control
336 public function getTabIndex()
338 return $this->getViewState('TabIndex',0);
342 * Sets the tab index of the control.
343 * Pass 0 if you want to disable tab index.
344 * @param integer the tab index to be set
346 public function setTabIndex($value)
348 $this->setViewState('TabIndex',TPropertyValue::ensureInteger($value),0);
352 * Returns the tag name used for this control.
353 * By default, the tag name is 'span'.
354 * You can override this method to provide customized tag names.
355 * @return string tag name of the control to be rendered
357 protected function getTagName()
363 * @return string the tooltip of the control
365 public function getToolTip()
367 return $this->getViewState('ToolTip','');
371 * Sets the tooltip of the control.
372 * Pass an empty string if you want to disable tooltip.
373 * @param string the tooltip to be set
375 public function setToolTip($value)
377 $this->setViewState('ToolTip',$value,'');
381 * @return string the width of the control
383 public function getWidth()
385 if($style=$this->getViewState('Style',null))
386 return $style->getWidth();
392 * @param string the width of the control
394 public function setWidth($value)
396 $this->getStyle()->setWidth($value);
401 * If your subclass overrides the onPreRender method be sure to call
402 * this method through parent::onPreRender($param); so your sub-class can be decorated,
403 * among other things.
404 * @param TEventParameter event parameter to be passed to the event handlers
406 public function onPreRender($param) {
407 if($decorator = $this->getDecorator(false))
408 $decorator->instantiate();
410 parent::onPreRender($param);
414 * Adds attribute name-value pairs to renderer.
415 * By default, the method will render 'id', 'accesskey', 'disabled',
416 * 'tabindex', 'title' and all custom attributes.
417 * The method can be overriden to provide customized attribute rendering.
418 * @param THtmlWriter the writer used for the rendering purpose
420 protected function addAttributesToRender($writer)
422 if($this->getID()!=='' || $this->getEnsureId())
423 $writer->addAttribute('id',$this->getClientID());
424 if(($accessKey=$this->getAccessKey())!=='')
425 $writer->addAttribute('accesskey',$accessKey);
426 if(!$this->getEnabled())
427 $writer->addAttribute('disabled','disabled');
428 if(($tabIndex=$this->getTabIndex())>0)
429 $writer->addAttribute('tabindex',"$tabIndex");
430 if(($toolTip=$this->getToolTip())!=='')
431 $writer->addAttribute('title',$toolTip);
432 if($style=$this->getViewState('Style',null))
433 $style->addAttributesToRender($writer);
434 if($this->getHasAttributes())
436 foreach($this->getAttributes() as $name=>$value)
437 $writer->addAttribute($name,$value);
442 * Renders the control.
443 * This method overrides the parent implementation by replacing it with
444 * the following sequence:
445 * - {@link renderBeginTag}
446 * - {@link renderContents}
447 * - {@link renderEndTag}
448 * @param THtmlWriter the writer used for the rendering purpose
450 public function render($writer)
452 $this->renderBeginTag($writer);
453 $this->renderContents($writer);
454 $this->renderEndTag($writer);
458 * Renders the openning tag for the control (including attributes)
459 * @param THtmlWriter the writer used for the rendering purpose
461 public function renderBeginTag($writer)
463 if($decorator = $this->getDecorator(false)) {
464 $decorator->renderPreTagText($writer);
465 $this->addAttributesToRender($writer);
466 $writer->renderBeginTag($this->getTagName());
467 $decorator->renderPreContentsText($writer);
469 $this->addAttributesToRender($writer);
470 $writer->renderBeginTag($this->getTagName());
475 * Renders the body content enclosed between the control tag.
476 * By default, child controls and text strings will be rendered.
477 * You can override this method to provide customized content rendering.
478 * @param THtmlWriter the writer used for the rendering purpose
480 public function renderContents($writer)
482 parent::renderChildren($writer);
486 * Renders the closing tag for the control
487 * @param THtmlWriter the writer used for the rendering purpose
489 public function renderEndTag($writer)
491 if($decorator = $this->getDecorator(false)) {
492 $decorator->renderPostContentsText($writer);
493 $writer->renderEndTag();
494 $decorator->renderPostTagText($writer);
496 $writer->renderEndTag($writer);