WebControl.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: WebControl
  4. *
  5. * Author: Gaurav Vaish
  6. * Maintainer: [email protected]
  7. * Contact: <[email protected]>, <[email protected]>
  8. * Implementation: yes
  9. * Status: 40%
  10. *
  11. * (C) Gaurav Vaish (2001)
  12. */
  13. using System;
  14. using System.Collections;
  15. using System.ComponentModel;
  16. using System.Web;
  17. using System.Web.UI;
  18. using System.Drawing;
  19. using System.Collections.Specialized;
  20. namespace System.Web.UI.WebControls
  21. {
  22. [PersistChildrenAttribute(false)]
  23. [ParseChildrenAttribute(true)]
  24. public class WebControl : Control, IAttributeAccessor
  25. {
  26. //TODO: A list of private members may be incomplete
  27. private HtmlTextWriterTag tagKey;
  28. private string stringTag;
  29. private AttributeCollection attributes;
  30. private StateBag attributeState;
  31. private Style controlStyle;
  32. private bool enabled;
  33. private string tagName;
  34. // TODO: The constructors definitions
  35. protected WebControl () : this (HtmlTextWriterTag.Span)
  36. {
  37. }
  38. public WebControl(HtmlTextWriterTag tag): base()
  39. {
  40. //FIXME: am i right?
  41. tagKey = tag;
  42. //stringTag = null;
  43. Initialize();
  44. }
  45. protected WebControl(string tag): base()
  46. {
  47. //FIXME: am i right?
  48. stringTag = tag;
  49. Initialize();
  50. }
  51. private void Initialize()
  52. {
  53. controlStyle = null;
  54. enabled = true;
  55. tagName = stringTag;
  56. attributeState = null;
  57. }
  58. public virtual string AccessKey
  59. {
  60. get
  61. {
  62. object o = ViewState["AccessKey"];
  63. if(o!=null)
  64. return (string)o;
  65. return String.Empty;
  66. }
  67. set
  68. {
  69. ViewState["AccessKey"] = value;
  70. }
  71. }
  72. [MonoTODO("FIXME_Internal_method_calls")]
  73. public AttributeCollection Attributes
  74. {
  75. get
  76. {
  77. if(attributes==null)
  78. {
  79. //FIXME: From where to get StateBag and how? I think this method is OK!
  80. if(attributeState == null)
  81. {
  82. attributeState = new StateBag(true);
  83. if(IsTrackingViewState)
  84. {
  85. attributeState.TrackViewState();
  86. }
  87. }
  88. attributes = new AttributeCollection(attributeState);
  89. }
  90. return attributes;
  91. }
  92. }
  93. public virtual Color BackColor
  94. {
  95. get {
  96. if (!ControlStyleCreated)
  97. return Color.Empty;
  98. return ControlStyle.BackColor;
  99. }
  100. set {
  101. ControlStyle.BackColor = value;
  102. }
  103. }
  104. public virtual Color BorderColor
  105. {
  106. get {
  107. if (!ControlStyleCreated)
  108. return Color.Empty;
  109. return ControlStyle.BorderColor;
  110. }
  111. set {
  112. ControlStyle.BorderColor = value;
  113. }
  114. }
  115. public virtual BorderStyle BorderStyle
  116. {
  117. get {
  118. if (!ControlStyleCreated)
  119. return BorderStyle.NotSet;
  120. return ControlStyle.BorderStyle;
  121. }
  122. set {
  123. ControlStyle.BorderStyle = value;
  124. }
  125. }
  126. public virtual Unit BorderWidth
  127. {
  128. get {
  129. if (!ControlStyleCreated)
  130. return Unit.Empty;
  131. return ControlStyle.BorderWidth;
  132. }
  133. set {
  134. if (value.Value < 0)
  135. throw new ArgumentException();
  136. ControlStyle.BorderWidth = value;
  137. }
  138. }
  139. public Style ControlStyle
  140. {
  141. get
  142. {
  143. if(controlStyle == null)
  144. {
  145. controlStyle = CreateControlStyle();
  146. if(IsTrackingViewState)
  147. {
  148. controlStyle.TrackViewState();
  149. }
  150. controlStyle.LoadViewState(null);
  151. }
  152. return controlStyle;
  153. }
  154. }
  155. public bool ControlStyleCreated
  156. {
  157. get
  158. {
  159. return (controlStyle!=null);
  160. }
  161. }
  162. public virtual string CssClass
  163. {
  164. get
  165. {
  166. return ControlStyle.CssClass;
  167. }
  168. set
  169. {
  170. ControlStyle.CssClass = value;
  171. }
  172. }
  173. public virtual bool Enabled
  174. {
  175. get
  176. {
  177. return enabled;
  178. }
  179. set
  180. {
  181. enabled = value;
  182. }
  183. }
  184. public virtual FontInfo Font
  185. {
  186. get
  187. {
  188. return ControlStyle.Font;
  189. }
  190. }
  191. public virtual Color ForeColor
  192. {
  193. get {
  194. if (!ControlStyleCreated)
  195. return Color.Empty;
  196. return ControlStyle.ForeColor;
  197. }
  198. set {
  199. ControlStyle.ForeColor = value;
  200. }
  201. }
  202. public virtual Unit Height
  203. {
  204. get
  205. {
  206. return ControlStyle.Height;
  207. }
  208. set
  209. {
  210. ControlStyle.Height = value;
  211. }
  212. }
  213. public CssStyleCollection Style
  214. {
  215. get
  216. {
  217. return Attributes.CssStyle;
  218. }
  219. }
  220. public virtual short TabIndex
  221. {
  222. get
  223. {
  224. object o = ViewState["TabIndex"];
  225. if(o!=null)
  226. return (short)o;
  227. return 0;
  228. }
  229. set
  230. {
  231. if(value < -32768 || value > 32767)
  232. throw new ArgumentException();
  233. ViewState["TabIndex"] = value;
  234. }
  235. }
  236. public virtual string ToolTip
  237. {
  238. get
  239. {
  240. object o = ViewState["ToolTip"];
  241. if(o!=null)
  242. return (string)o;
  243. return String.Empty;
  244. }
  245. set
  246. {
  247. ViewState["ToolTip"] = value;
  248. }
  249. }
  250. public virtual Unit Width
  251. {
  252. get
  253. {
  254. return ControlStyle.Width;
  255. }
  256. set
  257. {
  258. ControlStyle.Width = value;
  259. }
  260. }
  261. public void ApplyStyle(Style s)
  262. {
  263. if (s != null && !s.IsEmpty)
  264. ControlStyle.CopyFrom (s);
  265. }
  266. public void CopyBaseAttributes(WebControl controlSrc)
  267. {
  268. /*
  269. * AccessKey, Enabled, ToolTip, TabIndex, Attributes
  270. */
  271. AccessKey = controlSrc.AccessKey;
  272. Enabled = controlSrc.Enabled;
  273. ToolTip = controlSrc.ToolTip;
  274. TabIndex = controlSrc.TabIndex;
  275. attributes = controlSrc.Attributes;
  276. AttributeCollection otherAtt = controlSrc.Attributes;
  277. foreach (string key in controlSrc.Attributes.Keys)
  278. Attributes [key] = otherAtt [key];
  279. }
  280. public void MergeStyle(Style s)
  281. {
  282. ControlStyle.MergeWith(s);
  283. }
  284. public virtual void RenderBeginTag(HtmlTextWriter writer)
  285. {
  286. AddAttributesToRender(writer);
  287. writer.RenderBeginTag(TagName);
  288. }
  289. public virtual void RenderEndTag(HtmlTextWriter writer)
  290. {
  291. writer.RenderEndTag();
  292. }
  293. protected virtual HtmlTextWriterTag TagKey
  294. {
  295. get
  296. {
  297. return tagKey;
  298. }
  299. }
  300. protected virtual string TagName
  301. {
  302. get
  303. {
  304. if(tagName == null && TagKey != 0)
  305. {
  306. tagName = Enum.Format(typeof(HtmlTextWriterTag), TagKey, "G").ToString();
  307. }
  308. // What if tagName is null and tagKey 0?
  309. return tagName;
  310. }
  311. }
  312. protected virtual void AddAttributesToRender(HtmlTextWriter writer)
  313. {
  314. if(ID!=null)
  315. {
  316. writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
  317. }
  318. if(AccessKey.Length>0)
  319. {
  320. writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, AccessKey);
  321. }
  322. if(!Enabled)
  323. {
  324. writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
  325. }
  326. if(ToolTip.Length>0)
  327. {
  328. writer.AddAttribute(HtmlTextWriterAttribute.Title, ToolTip);
  329. }
  330. if(TabIndex != 0)
  331. {
  332. writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, TabIndex.ToString());
  333. }
  334. if(ControlStyleCreated)
  335. {
  336. if(!ControlStyle.IsEmpty)
  337. {
  338. ControlStyle.AddAttributesToRender(writer, this);
  339. }
  340. }
  341. if(attributeState != null){
  342. IEnumerator ie = Attributes.Keys.GetEnumerator ();
  343. while (ie.MoveNext ()){
  344. string key = (string) ie.Current;
  345. writer.AddAttribute (key, Attributes [key]);
  346. }
  347. }
  348. }
  349. protected virtual Style CreateControlStyle()
  350. {
  351. return new Style(ViewState);
  352. }
  353. protected override void LoadViewState (object savedState)
  354. {
  355. if (savedState != null) {
  356. Triplet saved = (Triplet) savedState;
  357. base.LoadViewState (saved.First);
  358. if (ControlStyleCreated)
  359. ControlStyle.LoadViewState (saved.Second);
  360. if (attributeState != null)
  361. attributeState.LoadViewState (saved.Third);
  362. }
  363. }
  364. protected override void Render(HtmlTextWriter writer)
  365. {
  366. RenderBeginTag(writer);
  367. RenderContents(writer);
  368. RenderEndTag(writer);
  369. }
  370. protected virtual void RenderContents(HtmlTextWriter writer)
  371. {
  372. base.Render(writer);
  373. }
  374. protected override object SaveViewState()
  375. {
  376. object baseView = base.SaveViewState ();
  377. object controlView = null;
  378. if (ControlStyleCreated)
  379. controlView = ControlStyle.SaveViewState();
  380. object attrView = null;
  381. if (attributeState != null)
  382. attrView = attributeState.SaveViewState();
  383. return new Triplet (baseView, controlView, attrView);
  384. }
  385. protected override void TrackViewState()
  386. {
  387. base.TrackViewState();
  388. if (ControlStyleCreated)
  389. ControlStyle.TrackViewState ();
  390. if (attributeState != null)
  391. attributeState.TrackViewState ();
  392. }
  393. string IAttributeAccessor.GetAttribute(string key)
  394. {
  395. if(Attributes!=null)
  396. return Attributes[key] as string;
  397. return null;
  398. }
  399. void IAttributeAccessor.SetAttribute(string key, string value)
  400. {
  401. Attributes[key] = value;
  402. }
  403. }
  404. }