WebControl.cs 7.8 KB

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