WebControl.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. [MonoTODO("FIXME_Internal_method_calls")]
  74. public AttributeCollection Attributes
  75. {
  76. get
  77. {
  78. throw new NotImplementedException();
  79. if(attributes==null)
  80. {
  81. //FIXME: 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. [MonoTODO("FIXME_Internal_method_calls")]
  101. public Style ControlStyle
  102. {
  103. get
  104. {
  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. [MonoTODO("FIXME_Internal_method_calls")]
  229. public void ApplyStyle(Style s)
  230. {
  231. /* FIXME: Again internal problem
  232. if(!ControlStyle.IsEmpty)
  233. {
  234. */
  235. ControlStyle.CopyFrom(s);
  236. //}
  237. }
  238. [MonoTODO]
  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(Enum.IsDefined(typeof(HtmlTextWriterTag), TagKey) )
  260. {
  261. writer.RenderBeginTag(TagKey);
  262. return;
  263. }
  264. writer.RenderBeginTag(tagName);
  265. }
  266. public virtual void RenderEndTag(HtmlTextWriter writer)
  267. {
  268. writer.RenderEndTag();
  269. }
  270. protected virtual HtmlTextWriterTag TagKey
  271. {
  272. get
  273. {
  274. return tagKey;
  275. }
  276. }
  277. protected virtual string TagName
  278. {
  279. get
  280. {
  281. if(tagName==null && Enum.IsDefined(typeof(HtmlTextWriterTag), tagKey) )
  282. {
  283. tagName = Enum.Format(typeof(HtmlTextWriterTag), tagKey, "G").ToString();
  284. }
  285. return tagName;
  286. }
  287. }
  288. protected virtual void AddAttributesToRender(HtmlTextWriter writer)
  289. {
  290. if(ID!=null)
  291. {
  292. writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
  293. }
  294. if(AccessKey.Length>0)
  295. {
  296. writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, AccessKey);
  297. }
  298. if(!Enabled)
  299. {
  300. writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
  301. }
  302. if(ToolTip.Length>0)
  303. {
  304. writer.AddAttribute(HtmlTextWriterAttribute.Title, ToolTip);
  305. }
  306. if(TabIndex != 0)
  307. {
  308. writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, TabIndex.ToString());
  309. }
  310. if(ControlStyleCreated)
  311. {
  312. if(!ControlStyle.IsEmpty)
  313. {
  314. ControlStyle.AddAttributesToRender(writer, this);
  315. }
  316. }
  317. if(attributeState!=null)
  318. {
  319. IEnumerator ie = Attributes.Keys.GetEnumerator();
  320. do
  321. {
  322. writer.AddAttribute((string)ie.Current, Attributes[(string)ie.Current]);
  323. } while(ie.MoveNext());
  324. }
  325. }
  326. protected virtual Style CreateControlStyle()
  327. {
  328. return new Style(ViewState);
  329. }
  330. [MonoTODO]
  331. protected override void LoadViewState(object savedState)
  332. {
  333. throw new NotImplementedException();
  334. //TODO: Load viewStates
  335. /*
  336. * May be will have to first look at Control::LoadViewState
  337. */
  338. }
  339. protected override void Render(HtmlTextWriter writer)
  340. {
  341. RenderBeginTag(writer);
  342. RenderContents(writer);
  343. RenderEndTag(writer);
  344. }
  345. protected virtual void RenderContents(HtmlTextWriter writer)
  346. {
  347. base.Render(writer);
  348. }
  349. [MonoTODO]
  350. protected override object SaveViewState()
  351. {
  352. throw new NotImplementedException();
  353. //TODO: Implement me!
  354. }
  355. protected override void TrackViewState()
  356. {
  357. TrackViewState();
  358. if(ControlStyleCreated)
  359. {
  360. ControlStyle.TrackViewState();
  361. }
  362. if(attributeState!=null)
  363. {
  364. attributeState.TrackViewState();
  365. }
  366. }
  367. string IAttributeAccessor.GetAttribute(string key)
  368. {
  369. if(Attributes!=null)
  370. return (string)Attributes[key];
  371. return null;
  372. }
  373. void IAttributeAccessor.SetAttribute(string key, string value)
  374. {
  375. Attributes[key] = value;
  376. }
  377. }
  378. }