WebControl.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 tagKey;
  25. private string stringTag;
  26. private AttributeCollection attributes;
  27. private StateBag attributeState;
  28. private Style controlStyle;
  29. private bool enabled;
  30. private string tagName;
  31. // TODO: The constructors definitions
  32. protected WebControl(): base()
  33. {
  34. //todo: what now? To be rendered as SPAN tag!
  35. Initialize();
  36. }
  37. public WebControl(HtmlTextWriterTag tag): base()
  38. {
  39. //FIXME: am i right?
  40. tagKey = tag;
  41. //stringTag = null;
  42. Initialize();
  43. }
  44. protected WebControl(string tag): base()
  45. {
  46. //FIXME: am i right?
  47. stringTag = tag;
  48. Initialize();
  49. }
  50. private void Initialize()
  51. {
  52. controlStyle = null;
  53. enabled = true;
  54. tagName = null;
  55. attributeState = null;
  56. }
  57. public virtual string AccessKey
  58. {
  59. get
  60. {
  61. object o = ViewState["AccessKey"];
  62. if(o!=null)
  63. return (string)o;
  64. return String.Empty;
  65. }
  66. set
  67. {
  68. ViewState["AccessKey"] = value;
  69. }
  70. }
  71. [MonoTODO("FIXME_Internal_method_calls")]
  72. public AttributeCollection Attributes
  73. {
  74. get
  75. {
  76. throw new NotImplementedException();
  77. if(attributes==null)
  78. {
  79. //FIXME: 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 virtual Color BackColor
  99. {
  100. get
  101. {
  102. object o = ViewState["BackColor"];
  103. if(o != null)
  104. {
  105. return (Color)o;
  106. }
  107. return Color.Empty;
  108. }
  109. set
  110. {
  111. ViewState["BackColor"] = value;
  112. }
  113. }
  114. public virtual Color BorderColor
  115. {
  116. get
  117. {
  118. object o = ViewState["BorderColor"];
  119. if(o != null)
  120. {
  121. return (Color)o;
  122. }
  123. return Color.Empty;
  124. }
  125. set
  126. {
  127. ViewState["BorderColor"] = value;
  128. }
  129. }
  130. [MonoTODO("FIXME_Internal_method_calls")]
  131. public virtual BorderStyle BorderStyle
  132. {
  133. get
  134. {
  135. object o = ViewState["BorderStyle"];
  136. if(o != null)
  137. {
  138. return (BorderStyle)o;
  139. }
  140. return BorderStyle.NotSet;
  141. }
  142. set
  143. {
  144. if(!Enum.IsDefined(typeof(BorderStyle), value))
  145. {
  146. throw new ArgumentException();
  147. }
  148. ViewState["BorderStyle"] = value;
  149. }
  150. }
  151. public virtual Unit BorderWidth
  152. {
  153. get
  154. {
  155. object o = ViewState["BorderWidth"];
  156. if(o != null)
  157. {
  158. return (Unit)o;
  159. }
  160. return Unit.Empty;
  161. }
  162. set
  163. {
  164. if(value.Value < 0)
  165. {
  166. throw new ArgumentException();
  167. }
  168. ViewState["BorderWidth"] = value;
  169. }
  170. }
  171. [MonoTODO("FIXME_Internal_method_calls")]
  172. public virtual Style ControlStyle
  173. {
  174. get
  175. {
  176. if(controlStyle == null)
  177. {
  178. controlStyle = CreateControlStyle();
  179. //FIXME: Uncomment the following in the final release
  180. // commented because of the assembly problem.
  181. //The functions TrackViewState() and LoadViewState() are internal
  182. /*
  183. if(IsTrackingViewState)
  184. {
  185. controlStyle.TrackViewState();
  186. }
  187. controlStyle.LoadViewState(null);
  188. */
  189. }
  190. return controlStyle;
  191. }
  192. }
  193. public bool ControlStyleCreated
  194. {
  195. get
  196. {
  197. return (controlStyle!=null);
  198. }
  199. }
  200. public virtual string CssClass
  201. {
  202. get
  203. {
  204. return ControlStyle.CssClass;
  205. }
  206. set
  207. {
  208. ControlStyle.CssClass = value;
  209. }
  210. }
  211. public virtual bool Enabled
  212. {
  213. get
  214. {
  215. return enabled;
  216. }
  217. set
  218. {
  219. enabled = value;
  220. }
  221. }
  222. public virtual FontInfo Font
  223. {
  224. get
  225. {
  226. return ControlStyle.Font;
  227. }
  228. }
  229. public virtual Color ForeColor
  230. {
  231. get
  232. {
  233. return ControlStyle.ForeColor;
  234. }
  235. set
  236. {
  237. ControlStyle.ForeColor = value;
  238. }
  239. }
  240. public virtual Unit Height
  241. {
  242. get
  243. {
  244. return ControlStyle.Height;
  245. }
  246. set
  247. {
  248. ControlStyle.Height = value;
  249. }
  250. }
  251. public CssStyleCollection Style
  252. {
  253. get
  254. {
  255. return Attributes.CssStyle;
  256. }
  257. }
  258. public virtual short TabIndex
  259. {
  260. get
  261. {
  262. object o = ViewState["TabIndex"];
  263. if(o!=null)
  264. return (short)o;
  265. return 0;
  266. }
  267. set
  268. {
  269. if(value < -32768 || value > 32767)
  270. throw new ArgumentException();
  271. ViewState["TabIndex"] = value;
  272. }
  273. }
  274. public virtual string ToolTip
  275. {
  276. get
  277. {
  278. object o = ViewState["ToolTip"];
  279. if(o!=null)
  280. return (string)o;
  281. return String.Empty;
  282. }
  283. set
  284. {
  285. ViewState["ToolTip"] = value;
  286. }
  287. }
  288. public virtual Unit Width
  289. {
  290. get
  291. {
  292. return ControlStyle.Width;
  293. }
  294. set
  295. {
  296. ControlStyle.Width = value;
  297. }
  298. }
  299. [MonoTODO("FIXME_Internal_method_calls")]
  300. public void ApplyStyle(Style s)
  301. {
  302. /* FIXME: Again internal problem
  303. if(!ControlStyle.IsEmpty)
  304. {
  305. */
  306. ControlStyle.CopyFrom(s);
  307. //}
  308. }
  309. [MonoTODO]
  310. public void CopyBaseAttributes(WebControl controlSrc)
  311. {
  312. //TODO: tocopy
  313. /*
  314. * AccessKey, Enabled, ToolTip, TabIndex, Attributes
  315. */
  316. AccessKey = controlSrc.AccessKey;
  317. Enabled = controlSrc.Enabled;
  318. ToolTip = controlSrc.ToolTip;
  319. TabIndex = controlSrc.TabIndex;
  320. attributes = controlSrc.Attributes;
  321. throw new NotImplementedException();
  322. }
  323. public void MergeStyle(Style s)
  324. {
  325. ControlStyle.MergeWith(s);
  326. }
  327. public virtual void RenderBeginTag(HtmlTextWriter writer)
  328. {
  329. AddAttributesToRender(writer);
  330. if(Enum.IsDefined(typeof(HtmlTextWriterTag), TagKey) )
  331. {
  332. writer.RenderBeginTag(TagKey);
  333. return;
  334. }
  335. writer.RenderBeginTag(tagName);
  336. }
  337. public virtual void RenderEndTag(HtmlTextWriter writer)
  338. {
  339. writer.RenderEndTag();
  340. }
  341. protected virtual HtmlTextWriterTag TagKey
  342. {
  343. get
  344. {
  345. return tagKey;
  346. }
  347. }
  348. protected virtual string TagName
  349. {
  350. get
  351. {
  352. if(tagName==null && Enum.IsDefined(typeof(HtmlTextWriterTag), tagKey) )
  353. {
  354. tagName = Enum.Format(typeof(HtmlTextWriterTag), tagKey, "G").ToString();
  355. }
  356. return tagName;
  357. }
  358. }
  359. protected virtual void AddAttributesToRender(HtmlTextWriter writer)
  360. {
  361. if(ID!=null)
  362. {
  363. writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
  364. }
  365. if(AccessKey.Length>0)
  366. {
  367. writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, AccessKey);
  368. }
  369. if(!Enabled)
  370. {
  371. writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
  372. }
  373. if(ToolTip.Length>0)
  374. {
  375. writer.AddAttribute(HtmlTextWriterAttribute.Title, ToolTip);
  376. }
  377. if(TabIndex != 0)
  378. {
  379. writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, TabIndex.ToString());
  380. }
  381. if(ControlStyleCreated)
  382. {
  383. if(!ControlStyle.IsEmpty)
  384. {
  385. ControlStyle.AddAttributesToRender(writer, this);
  386. }
  387. }
  388. if(attributeState!=null)
  389. {
  390. IEnumerator ie = Attributes.Keys.GetEnumerator();
  391. do
  392. {
  393. writer.AddAttribute((string)ie.Current, Attributes[(string)ie.Current]);
  394. } while(ie.MoveNext());
  395. }
  396. }
  397. protected virtual Style CreateControlStyle()
  398. {
  399. return new Style(ViewState);
  400. }
  401. [MonoTODO]
  402. protected override void LoadViewState(object savedState)
  403. {
  404. throw new NotImplementedException();
  405. //TODO: Load viewStates
  406. /*
  407. * May be will have to first look at Control::LoadViewState
  408. */
  409. }
  410. protected override void Render(HtmlTextWriter writer)
  411. {
  412. RenderBeginTag(writer);
  413. RenderContents(writer);
  414. RenderEndTag(writer);
  415. }
  416. protected virtual void RenderContents(HtmlTextWriter writer)
  417. {
  418. base.Render(writer);
  419. }
  420. [MonoTODO]
  421. protected override object SaveViewState()
  422. {
  423. throw new NotImplementedException();
  424. //TODO: Implement me!
  425. }
  426. protected override void TrackViewState()
  427. {
  428. TrackViewState();
  429. if(ControlStyleCreated)
  430. {
  431. ControlStyle.TrackViewState();
  432. }
  433. if(attributeState!=null)
  434. {
  435. attributeState.TrackViewState();
  436. }
  437. }
  438. string IAttributeAccessor.GetAttribute(string key)
  439. {
  440. if(Attributes!=null)
  441. return (string)Attributes[key];
  442. return null;
  443. }
  444. void IAttributeAccessor.SetAttribute(string key, string value)
  445. {
  446. Attributes[key] = value;
  447. }
  448. }
  449. }