WebControl.cs 9.2 KB

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