WebControl.cs 9.2 KB

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