WebControl.cs 8.3 KB

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