WebControl.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Authors:
  23. // Peter Bartok ([email protected])
  24. //
  25. //
  26. using System.ComponentModel;
  27. using System.Drawing;
  28. using System.Security.Permissions;
  29. namespace System.Web.UI.WebControls {
  30. // CAS
  31. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  32. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  33. // attributes
  34. [ParseChildren (true)]
  35. #if NET_2_0
  36. [PersistChildrenAttribute (false, false)]
  37. [Themeable (true)]
  38. #else
  39. [PersistChildrenAttribute (false)]
  40. #endif
  41. public class WebControl : Control, IAttributeAccessor {
  42. Style style;
  43. HtmlTextWriterTag tag;
  44. string tag_name;
  45. AttributeCollection attributes;
  46. StateBag attribute_state;
  47. bool enabled;
  48. public WebControl (HtmlTextWriterTag tag)
  49. {
  50. this.tag = tag;
  51. this.enabled = true;
  52. }
  53. protected WebControl () : this (HtmlTextWriterTag.Span)
  54. {
  55. }
  56. protected WebControl (string tag)
  57. {
  58. this.tag = HtmlTextWriterTag.Unknown;
  59. this.tag_name = tag;
  60. this.enabled = true;
  61. }
  62. #if ONLY_1_1
  63. [Bindable(true)]
  64. #endif
  65. [DefaultValue("")]
  66. [WebSysDescription ("")]
  67. [WebCategory ("Behavior")]
  68. public virtual string AccessKey {
  69. get {
  70. return ViewState.GetString ("AccessKey", string.Empty);
  71. }
  72. set {
  73. if (value == null || value.Length < 2)
  74. ViewState ["AccessKey"] = value;
  75. else
  76. throw new ArgumentException ("AccessKey can only be null, empty or a single character", "value");
  77. }
  78. }
  79. [Browsable(false)]
  80. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  81. [WebSysDescription ("")]
  82. [WebCategory ("Behavior")]
  83. public AttributeCollection Attributes {
  84. get {
  85. if (attributes == null) {
  86. attribute_state = new StateBag (true);
  87. if (IsTrackingViewState)
  88. attribute_state.TrackViewState ();
  89. attributes = new AttributeCollection (attribute_state);
  90. }
  91. return attributes;
  92. }
  93. }
  94. #if ONLY_1_1
  95. [Bindable(true)]
  96. #endif
  97. [DefaultValue(typeof (Color), "")]
  98. [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
  99. [WebSysDescription ("")]
  100. [WebCategory ("Appearance")]
  101. public virtual Color BackColor {
  102. get {
  103. if (style == null)
  104. return Color.Empty;
  105. return style.BackColor;
  106. }
  107. set {
  108. ControlStyle.BackColor = value;
  109. }
  110. }
  111. #if ONLY_1_1
  112. [Bindable(true)]
  113. #endif
  114. [DefaultValue(typeof (Color), "")]
  115. [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
  116. [WebSysDescription ("")]
  117. [WebCategory ("Appearance")]
  118. public virtual Color BorderColor {
  119. get {
  120. if (style == null)
  121. return Color.Empty;
  122. return style.BorderColor;
  123. }
  124. set {
  125. ControlStyle.BorderColor = value;
  126. }
  127. }
  128. #if ONLY_1_1
  129. [Bindable(true)]
  130. #endif
  131. [DefaultValue(BorderStyle.NotSet)]
  132. [WebSysDescription ("")]
  133. [WebCategory ("Appearance")]
  134. public virtual BorderStyle BorderStyle {
  135. get {
  136. if (style == null)
  137. return BorderStyle.NotSet;
  138. return style.BorderStyle;
  139. }
  140. set {
  141. if (value < BorderStyle.NotSet || value > BorderStyle.Outset)
  142. throw new ArgumentOutOfRangeException ("value");
  143. ControlStyle.BorderStyle = value;
  144. }
  145. }
  146. #if ONLY_1_1
  147. [Bindable(true)]
  148. #endif
  149. [DefaultValue(typeof (Unit), "")]
  150. [WebSysDescription ("")]
  151. [WebCategory ("Appearance")]
  152. public virtual Unit BorderWidth {
  153. get {
  154. if (style == null)
  155. return Unit.Empty;
  156. return style.BorderWidth;
  157. }
  158. set {
  159. ControlStyle.BorderWidth = value;
  160. }
  161. }
  162. [Browsable(false)]
  163. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  164. [WebSysDescription ("")]
  165. [WebCategory ("Appearance")]
  166. public Style ControlStyle {
  167. get {
  168. if (style == null) {
  169. style = this.CreateControlStyle ();
  170. if (IsTrackingViewState)
  171. style.TrackViewState ();
  172. }
  173. return style;
  174. }
  175. }
  176. [Browsable(false)]
  177. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  178. #if NET_2_0
  179. [EditorBrowsable (EditorBrowsableState.Never)]
  180. #endif
  181. public bool ControlStyleCreated {
  182. get {
  183. return style != null;
  184. }
  185. }
  186. #if ONLY_1_1
  187. [Bindable(true)]
  188. #endif
  189. [DefaultValue("")]
  190. [WebSysDescription ("")]
  191. [WebCategory ("Appearance")]
  192. public virtual string CssClass {
  193. get {
  194. if (style == null)
  195. return string.Empty;
  196. return style.CssClass;
  197. }
  198. set {
  199. ControlStyle.CssClass = value;
  200. }
  201. }
  202. [Bindable(true)]
  203. [DefaultValue(true)]
  204. #if NET_2_0
  205. [Themeable (false)]
  206. #endif
  207. public virtual bool Enabled {
  208. get {
  209. return enabled;
  210. }
  211. set {
  212. if (enabled != value) {
  213. ViewState ["Enabled"] = value;
  214. enabled = value;
  215. }
  216. }
  217. }
  218. #if NET_2_0
  219. [Browsable (true)]
  220. [MonoTODO]
  221. public virtual new bool EnableTheming
  222. {
  223. get { return base.EnableTheming; }
  224. set { base.EnableTheming = value; }
  225. }
  226. #endif
  227. #if ONLY_1_1
  228. [DefaultValue(null)]
  229. #endif
  230. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  231. [NotifyParentProperty(true)]
  232. [WebSysDescription ("")]
  233. [WebCategory ("Appearance")]
  234. public virtual FontInfo Font {
  235. get {
  236. // Oddly enough, it looks like we have to let it create the style
  237. // since we can't create a FontInfo without a style owner
  238. return ControlStyle.Font;
  239. }
  240. }
  241. #if ONLY_1_1
  242. [Bindable(true)]
  243. #endif
  244. [DefaultValue(typeof (Color), "")]
  245. [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
  246. [WebSysDescription ("")]
  247. [WebCategory ("Appearance")]
  248. public virtual Color ForeColor {
  249. get {
  250. if (style == null)
  251. return Color.Empty;
  252. return style.ForeColor;
  253. }
  254. set {
  255. ControlStyle.ForeColor = value;
  256. }
  257. }
  258. #if NET_2_0
  259. [Browsable (false)]
  260. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  261. [MonoTODO]
  262. public bool HasAttributes
  263. {
  264. get {
  265. throw new NotImplementedException ();
  266. }
  267. }
  268. #endif
  269. #if ONLY_1_1
  270. [Bindable(true)]
  271. #endif
  272. [DefaultValue(typeof (Unit), "")]
  273. [WebSysDescription ("")]
  274. [WebCategory ("Layout")]
  275. public virtual Unit Height {
  276. get {
  277. if (style == null)
  278. return Unit.Empty;
  279. return style.Height;
  280. }
  281. set {
  282. ControlStyle.Height = value;
  283. }
  284. }
  285. #if NET_2_0
  286. [Browsable (true)]
  287. [MonoTODO]
  288. public virtual new string SkinID
  289. {
  290. get { return base.SkinID; }
  291. set { base.SkinID = value; }
  292. }
  293. #endif
  294. [Browsable(false)]
  295. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  296. [WebSysDescription ("")]
  297. [WebCategory ("Style")]
  298. public CssStyleCollection Style {
  299. get {
  300. return Attributes.CssStyle;
  301. }
  302. }
  303. [DefaultValue((short)0)]
  304. [WebSysDescription ("")]
  305. [WebCategory ("Behavior")]
  306. public virtual short TabIndex {
  307. get {
  308. return ViewState.GetShort ("TabIndex", 0);
  309. }
  310. set {
  311. ViewState ["TabIndex"] = value;
  312. }
  313. }
  314. #if ONLY_1_1
  315. [Bindable(true)]
  316. #endif
  317. [DefaultValue("")]
  318. #if NET_2_0
  319. [Localizable (true)]
  320. #endif
  321. [WebSysDescription ("")]
  322. [WebCategory ("Behavior")]
  323. public virtual string ToolTip {
  324. get {
  325. return ViewState.GetString ("ToolTip", string.Empty);
  326. }
  327. set {
  328. ViewState ["ToolTip"] = value;
  329. }
  330. }
  331. #if ONLY_1_1
  332. [Bindable(true)]
  333. #endif
  334. [DefaultValue(typeof (Unit), "")]
  335. [WebSysDescription ("")]
  336. [WebCategory ("Layout")]
  337. public virtual Unit Width {
  338. get {
  339. if (style == null)
  340. return Unit.Empty;
  341. return style.Width;
  342. }
  343. set {
  344. ControlStyle.Width = value;
  345. }
  346. }
  347. [Browsable(false)]
  348. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  349. protected virtual HtmlTextWriterTag TagKey {
  350. get {
  351. return tag;
  352. }
  353. }
  354. [Browsable(false)]
  355. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  356. protected virtual string TagName {
  357. get {
  358. // do this here to avoid potentially costly lookups on every control
  359. if (tag_name == null)
  360. tag_name = HtmlTextWriter.StaticGetTagName (TagKey);
  361. return tag_name;
  362. }
  363. }
  364. #if NET_2_0
  365. [MonoTODO]
  366. protected internal bool IsEnabled
  367. {
  368. get {
  369. throw new NotImplementedException ();
  370. }
  371. }
  372. #endif
  373. public void ApplyStyle (Style s)
  374. {
  375. if (s != null && !s.IsEmpty)
  376. ControlStyle.CopyFrom(s);
  377. }
  378. public void CopyBaseAttributes (WebControl controlSrc)
  379. {
  380. object o;
  381. if (controlSrc == null)
  382. return;
  383. o = controlSrc.ViewState ["Enabled"];
  384. if (o != null) {
  385. enabled = (bool)o;
  386. }
  387. o = controlSrc.ViewState ["AccessKey"];
  388. if (o != null)
  389. ViewState ["AccessKey"] = o;
  390. o = controlSrc.ViewState ["TabIndex"];
  391. if (o != null)
  392. ViewState ["TabIndex"] = o;
  393. o = controlSrc.ViewState ["ToolTip"];
  394. if (o != null)
  395. ViewState ["ToolTip"] = o;
  396. if (controlSrc.attributes != null)
  397. foreach (string s in controlSrc.attributes.Keys)
  398. Attributes [s] = controlSrc.attributes [s];
  399. }
  400. public void MergeStyle (Style s)
  401. {
  402. if (s != null && !s.IsEmpty)
  403. ControlStyle.MergeWith(s);
  404. }
  405. public virtual void RenderBeginTag (HtmlTextWriter writer)
  406. {
  407. AddAttributesToRender (writer);
  408. if (TagKey == HtmlTextWriterTag.Unknown)
  409. writer.RenderBeginTag (TagName);
  410. else
  411. writer.RenderBeginTag (TagKey);
  412. }
  413. public virtual void RenderEndTag (HtmlTextWriter writer)
  414. {
  415. writer.RenderEndTag ();
  416. }
  417. protected virtual void AddAttributesToRender (HtmlTextWriter writer)
  418. {
  419. if (ID != null)
  420. writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
  421. if (AccessKey != string.Empty)
  422. writer.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
  423. if (!enabled)
  424. writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
  425. if (ToolTip != string.Empty)
  426. writer.AddAttribute (HtmlTextWriterAttribute.Title, ToolTip);
  427. if (TabIndex != 0)
  428. writer.AddAttribute (HtmlTextWriterAttribute.Tabindex, TabIndex.ToString ());
  429. if (style != null && !style.IsEmpty) {
  430. #if NET_2_0
  431. //unbelievable, but see WebControlTest.RenderBeginTag_BorderWidth_xxx
  432. if (TagKey == HtmlTextWriterTag.Span)
  433. if (style.BorderWidth != Unit.Empty
  434. || style.BorderStyle != BorderStyle.NotSet)
  435. writer.AddStyleAttribute (HtmlTextWriterStyle.Display, "inline-block");
  436. #endif
  437. style.AddAttributesToRender(writer, this);
  438. }
  439. if (attributes != null)
  440. foreach(string s in attributes.Keys)
  441. writer.AddAttribute (s, attributes [s]);
  442. }
  443. protected virtual Style CreateControlStyle()
  444. {
  445. style = new Style (ViewState);
  446. return style;
  447. }
  448. protected override void LoadViewState (object savedState)
  449. {
  450. if (savedState == null) {
  451. base.LoadViewState(null);
  452. return;
  453. }
  454. Triplet triplet = (Triplet) savedState;
  455. base.LoadViewState (triplet.First);
  456. if (triplet.Second != null) {
  457. if (attribute_state == null) {
  458. attribute_state = new StateBag ();
  459. if (IsTrackingViewState)
  460. attribute_state.TrackViewState ();
  461. }
  462. attribute_state.LoadViewState (triplet.Second);
  463. attributes = new AttributeCollection(attribute_state);
  464. }
  465. if (triplet.Third != null) {
  466. if (style == null)
  467. style = CreateControlStyle ();
  468. style.LoadViewState (triplet.Third);
  469. }
  470. enabled = ViewState.GetBool("Enabled", true);
  471. }
  472. #if NET_2_0
  473. protected internal
  474. #else
  475. protected
  476. #endif
  477. override void Render (HtmlTextWriter writer)
  478. {
  479. RenderBeginTag (writer);
  480. RenderContents (writer);
  481. RenderEndTag (writer);
  482. }
  483. #if NET_2_0
  484. protected internal
  485. #else
  486. protected
  487. #endif
  488. virtual void RenderContents (HtmlTextWriter writer)
  489. {
  490. base.Render (writer);
  491. }
  492. protected override object SaveViewState ()
  493. {
  494. object view_state;
  495. object attr_view_state = null;
  496. object style_view_state = null;
  497. view_state = base.SaveViewState ();
  498. if (attribute_state != null)
  499. attr_view_state = attribute_state.SaveViewState ();
  500. if (style != null)
  501. style_view_state = style.SaveViewState ();
  502. if (view_state == null && attr_view_state == null && style_view_state == null)
  503. return null;
  504. return new Triplet (view_state, attr_view_state, style_view_state);
  505. }
  506. protected override void TrackViewState()
  507. {
  508. if (style != null)
  509. style.TrackViewState ();
  510. if (attribute_state != null)
  511. attribute_state.TrackViewState ();
  512. base.TrackViewState ();
  513. }
  514. string IAttributeAccessor.GetAttribute (string key)
  515. {
  516. if (attributes != null)
  517. return attributes [key];
  518. return null;
  519. }
  520. void IAttributeAccessor.SetAttribute (string key, string value)
  521. {
  522. Attributes [key] = value;
  523. }
  524. }
  525. }