WebControl.cs 8.2 KB

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