WebControl.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. {
  97. object o = ViewState["BackColor"];
  98. if(o != null)
  99. {
  100. return (Color)o;
  101. }
  102. return Color.Empty;
  103. }
  104. set
  105. {
  106. ViewState["BackColor"] = value;
  107. }
  108. }
  109. public virtual Color BorderColor
  110. {
  111. get
  112. {
  113. object o = ViewState["BorderColor"];
  114. if(o != null)
  115. {
  116. return (Color)o;
  117. }
  118. return Color.Empty;
  119. }
  120. set
  121. {
  122. ViewState["BorderColor"] = value;
  123. }
  124. }
  125. [MonoTODO("FIXME_Internal_method_calls")]
  126. public virtual BorderStyle BorderStyle
  127. {
  128. get
  129. {
  130. object o = ViewState["BorderStyle"];
  131. if(o != null)
  132. {
  133. return (BorderStyle)o;
  134. }
  135. return BorderStyle.NotSet;
  136. }
  137. set
  138. {
  139. if(!Enum.IsDefined(typeof(BorderStyle), value))
  140. {
  141. throw new ArgumentException();
  142. }
  143. ViewState["BorderStyle"] = value;
  144. }
  145. }
  146. public virtual Unit BorderWidth
  147. {
  148. get
  149. {
  150. object o = ViewState["BorderWidth"];
  151. if(o != null)
  152. {
  153. return (Unit)o;
  154. }
  155. return Unit.Empty;
  156. }
  157. set
  158. {
  159. if(value.Value < 0)
  160. {
  161. throw new ArgumentException();
  162. }
  163. ViewState["BorderWidth"] = value;
  164. }
  165. }
  166. [MonoTODO("FIXME_Internal_method_calls")]
  167. public Style ControlStyle
  168. {
  169. get
  170. {
  171. if(controlStyle == null)
  172. {
  173. controlStyle = CreateControlStyle();
  174. if(IsTrackingViewState)
  175. {
  176. controlStyle.TrackViewState();
  177. }
  178. controlStyle.LoadViewState(null);
  179. }
  180. return controlStyle;
  181. }
  182. }
  183. public bool ControlStyleCreated
  184. {
  185. get
  186. {
  187. return (controlStyle!=null);
  188. }
  189. }
  190. public virtual string CssClass
  191. {
  192. get
  193. {
  194. return ControlStyle.CssClass;
  195. }
  196. set
  197. {
  198. ControlStyle.CssClass = value;
  199. }
  200. }
  201. public virtual bool Enabled
  202. {
  203. get
  204. {
  205. return enabled;
  206. }
  207. set
  208. {
  209. enabled = value;
  210. }
  211. }
  212. public virtual FontInfo Font
  213. {
  214. get
  215. {
  216. return ControlStyle.Font;
  217. }
  218. }
  219. public virtual Color ForeColor
  220. {
  221. get
  222. {
  223. return ControlStyle.ForeColor;
  224. }
  225. set
  226. {
  227. ControlStyle.ForeColor = value;
  228. }
  229. }
  230. public virtual Unit Height
  231. {
  232. get
  233. {
  234. return ControlStyle.Height;
  235. }
  236. set
  237. {
  238. ControlStyle.Height = value;
  239. }
  240. }
  241. public CssStyleCollection Style
  242. {
  243. get
  244. {
  245. return Attributes.CssStyle;
  246. }
  247. }
  248. public virtual short TabIndex
  249. {
  250. get
  251. {
  252. object o = ViewState["TabIndex"];
  253. if(o!=null)
  254. return (short)o;
  255. return 0;
  256. }
  257. set
  258. {
  259. if(value < -32768 || value > 32767)
  260. throw new ArgumentException();
  261. ViewState["TabIndex"] = value;
  262. }
  263. }
  264. public virtual string ToolTip
  265. {
  266. get
  267. {
  268. object o = ViewState["ToolTip"];
  269. if(o!=null)
  270. return (string)o;
  271. return String.Empty;
  272. }
  273. set
  274. {
  275. ViewState["ToolTip"] = value;
  276. }
  277. }
  278. public virtual Unit Width
  279. {
  280. get
  281. {
  282. return ControlStyle.Width;
  283. }
  284. set
  285. {
  286. ControlStyle.Width = value;
  287. }
  288. }
  289. [MonoTODO("FIXME_Internal_method_calls")]
  290. public void ApplyStyle(Style s)
  291. {
  292. /* FIXME: Again internal problem
  293. if(!ControlStyle.IsEmpty)
  294. {
  295. */
  296. ControlStyle.CopyFrom(s);
  297. //}
  298. }
  299. public void CopyBaseAttributes(WebControl controlSrc)
  300. {
  301. /*
  302. * AccessKey, Enabled, ToolTip, TabIndex, Attributes
  303. */
  304. AccessKey = controlSrc.AccessKey;
  305. Enabled = controlSrc.Enabled;
  306. ToolTip = controlSrc.ToolTip;
  307. TabIndex = controlSrc.TabIndex;
  308. attributes = controlSrc.Attributes;
  309. AttributeCollection otherAtt = controlSrc.Attributes;
  310. foreach (string key in controlSrc.Attributes.Keys)
  311. Attributes [key] = otherAtt [key];
  312. }
  313. public void MergeStyle(Style s)
  314. {
  315. ControlStyle.MergeWith(s);
  316. }
  317. public virtual void RenderBeginTag(HtmlTextWriter writer)
  318. {
  319. AddAttributesToRender(writer);
  320. writer.RenderBeginTag(TagName);
  321. }
  322. public virtual void RenderEndTag(HtmlTextWriter writer)
  323. {
  324. writer.RenderEndTag();
  325. }
  326. protected virtual HtmlTextWriterTag TagKey
  327. {
  328. get
  329. {
  330. return tagKey;
  331. }
  332. }
  333. protected virtual string TagName
  334. {
  335. get
  336. {
  337. if(tagName == null && TagKey != 0)
  338. {
  339. tagName = Enum.Format(typeof(HtmlTextWriterTag), TagKey, "G").ToString();
  340. }
  341. // What if tagName is null and tagKey 0?
  342. return tagName;
  343. }
  344. }
  345. protected virtual void AddAttributesToRender(HtmlTextWriter writer)
  346. {
  347. if(ID!=null)
  348. {
  349. writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
  350. }
  351. if(AccessKey.Length>0)
  352. {
  353. writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, AccessKey);
  354. }
  355. if(!Enabled)
  356. {
  357. writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
  358. }
  359. if(ToolTip.Length>0)
  360. {
  361. writer.AddAttribute(HtmlTextWriterAttribute.Title, ToolTip);
  362. }
  363. if(TabIndex != 0)
  364. {
  365. writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, TabIndex.ToString());
  366. }
  367. if(ControlStyleCreated)
  368. {
  369. if(!ControlStyle.IsEmpty)
  370. {
  371. ControlStyle.AddAttributesToRender(writer, this);
  372. }
  373. }
  374. if(attributeState != null){
  375. IEnumerator ie = Attributes.Keys.GetEnumerator ();
  376. while (ie.MoveNext ()){
  377. string key = (string) ie.Current;
  378. writer.AddAttribute (key, Attributes [key]);
  379. }
  380. }
  381. }
  382. protected virtual Style CreateControlStyle()
  383. {
  384. return new Style(ViewState);
  385. }
  386. protected override void LoadViewState (object savedState)
  387. {
  388. if (savedState != null) {
  389. Triplet saved = (Triplet) savedState;
  390. base.LoadViewState (saved.First);
  391. if (ControlStyleCreated)
  392. ControlStyle.LoadViewState (saved.Second);
  393. if (attributeState != null)
  394. attributeState.LoadViewState (saved.Third);
  395. }
  396. }
  397. protected override void Render(HtmlTextWriter writer)
  398. {
  399. RenderBeginTag(writer);
  400. RenderContents(writer);
  401. RenderEndTag(writer);
  402. }
  403. protected virtual void RenderContents(HtmlTextWriter writer)
  404. {
  405. base.Render(writer);
  406. }
  407. protected override object SaveViewState()
  408. {
  409. object baseView = base.SaveViewState ();
  410. object controlView = null;
  411. if (ControlStyleCreated)
  412. controlView = ControlStyle.SaveViewState();
  413. object attrView = null;
  414. if (attributeState != null)
  415. attrView = attributeState.SaveViewState();
  416. return new Triplet (baseView, controlView, attrView);
  417. }
  418. protected override void TrackViewState()
  419. {
  420. base.TrackViewState();
  421. if (ControlStyleCreated)
  422. ControlStyle.TrackViewState ();
  423. if (attributeState != null)
  424. attributeState.TrackViewState ();
  425. }
  426. string IAttributeAccessor.GetAttribute(string key)
  427. {
  428. if(Attributes!=null)
  429. return Attributes[key] as string;
  430. return null;
  431. }
  432. void IAttributeAccessor.SetAttribute(string key, string value)
  433. {
  434. Attributes[key] = value;
  435. }
  436. }
  437. }