WebControl.cs 9.0 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(): 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 = 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 virtual 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. [MonoTODO]
  311. public void CopyBaseAttributes(WebControl controlSrc)
  312. {
  313. //TODO: tocopy
  314. /*
  315. * AccessKey, Enabled, ToolTip, TabIndex, Attributes
  316. */
  317. AccessKey = controlSrc.AccessKey;
  318. Enabled = controlSrc.Enabled;
  319. ToolTip = controlSrc.ToolTip;
  320. TabIndex = controlSrc.TabIndex;
  321. attributes = controlSrc.Attributes;
  322. throw new NotImplementedException();
  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(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. {
  387. IEnumerator ie = Attributes.Keys.GetEnumerator();
  388. do
  389. {
  390. writer.AddAttribute((string)ie.Current, Attributes[(string)ie.Current]);
  391. } while(ie.MoveNext());
  392. }
  393. }
  394. protected virtual Style CreateControlStyle()
  395. {
  396. return new Style(ViewState);
  397. }
  398. [MonoTODO]
  399. protected override void LoadViewState(object savedState)
  400. {
  401. throw new NotImplementedException();
  402. //TODO: Load viewStates
  403. /*
  404. * May be will have to first look at Control::LoadViewState
  405. */
  406. }
  407. protected override void Render(HtmlTextWriter writer)
  408. {
  409. RenderBeginTag(writer);
  410. RenderContents(writer);
  411. RenderEndTag(writer);
  412. }
  413. protected virtual void RenderContents(HtmlTextWriter writer)
  414. {
  415. base.Render(writer);
  416. }
  417. [MonoTODO]
  418. protected override object SaveViewState()
  419. {
  420. throw new NotImplementedException();
  421. //TODO: Implement me!
  422. }
  423. protected override void TrackViewState()
  424. {
  425. TrackViewState();
  426. if(ControlStyleCreated)
  427. {
  428. ControlStyle.TrackViewState();
  429. }
  430. if(attributeState!=null)
  431. {
  432. attributeState.TrackViewState();
  433. }
  434. }
  435. string IAttributeAccessor.GetAttribute(string key)
  436. {
  437. if(Attributes!=null)
  438. return (string)Attributes[key];
  439. return null;
  440. }
  441. void IAttributeAccessor.SetAttribute(string key, string value)
  442. {
  443. Attributes[key] = value;
  444. }
  445. }
  446. }