WebControl.cs 8.2 KB

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