WebControl.cs 9.1 KB

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