2
0

WebControl.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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. namespace System.Web.UI.WebControls {
  29. #if NET_2_0
  30. [PersistChildrenAttribute (false, false)]
  31. [ParseChildrenAttribute (true, ChildControlType = typeof(Control))]
  32. [Themeable (true)]
  33. #else
  34. [PersistChildrenAttribute (false)]
  35. [ParseChildrenAttribute (true)]
  36. #endif
  37. public class WebControl : Control, IAttributeAccessor {
  38. Style style;
  39. HtmlTextWriterTag tag;
  40. string tag_name;
  41. AttributeCollection attributes;
  42. StateBag attribute_state;
  43. bool enabled;
  44. public WebControl (HtmlTextWriterTag tag)
  45. {
  46. this.tag = tag;
  47. this.enabled = true;
  48. }
  49. protected WebControl () : this (HtmlTextWriterTag.Span)
  50. {
  51. }
  52. protected WebControl (string tag)
  53. {
  54. this.tag = HtmlTextWriterTag.Unknown;
  55. this.tag_name = tag;
  56. this.enabled = true;
  57. }
  58. #if ONLY_1_1
  59. [Bindable(true)]
  60. #endif
  61. [DefaultValue("")]
  62. [WebSysDescription ("")]
  63. [WebCategory ("Behavior")]
  64. public virtual string AccessKey {
  65. get {
  66. return ViewState.GetString ("AccessKey", string.Empty);
  67. }
  68. set {
  69. if (value == null || value.Length < 2)
  70. ViewState ["AccessKey"] = value;
  71. else
  72. throw new ArgumentException ("AccessKey can only be null, empty or a single character", "value");
  73. }
  74. }
  75. [Browsable(false)]
  76. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  77. [WebSysDescription ("")]
  78. [WebCategory ("Behavior")]
  79. public AttributeCollection Attributes {
  80. get {
  81. if (attributes == null) {
  82. attribute_state = new StateBag ();
  83. if (IsTrackingViewState)
  84. attribute_state.TrackViewState ();
  85. attributes = new AttributeCollection (attribute_state);
  86. }
  87. return attributes;
  88. }
  89. }
  90. #if ONLY_1_1
  91. [Bindable(true)]
  92. #endif
  93. [DefaultValue(typeof (Color), "")]
  94. [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
  95. [WebSysDescription ("")]
  96. [WebCategory ("Appearance")]
  97. public virtual Color BackColor {
  98. get {
  99. if (style == null)
  100. return Color.Empty;
  101. return style.BackColor;
  102. }
  103. set {
  104. ControlStyle.BackColor = value;
  105. }
  106. }
  107. #if ONLY_1_1
  108. [Bindable(true)]
  109. #endif
  110. [DefaultValue(typeof (Color), "")]
  111. [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
  112. [WebSysDescription ("")]
  113. [WebCategory ("Appearance")]
  114. public virtual Color BorderColor {
  115. get {
  116. if (style == null)
  117. return Color.Empty;
  118. return style.BorderColor;
  119. }
  120. set {
  121. ControlStyle.BorderColor = value;
  122. }
  123. }
  124. #if ONLY_1_1
  125. [Bindable(true)]
  126. #endif
  127. [DefaultValue(BorderStyle.NotSet)]
  128. [WebSysDescription ("")]
  129. [WebCategory ("Appearance")]
  130. public virtual BorderStyle BorderStyle {
  131. get {
  132. if (style == null)
  133. return BorderStyle.NotSet;
  134. return style.BorderStyle;
  135. }
  136. set {
  137. if (value < BorderStyle.NotSet || value > BorderStyle.Outset)
  138. throw new ArgumentOutOfRangeException ("value");
  139. ControlStyle.BorderStyle = value;
  140. }
  141. }
  142. #if ONLY_1_1
  143. [Bindable(true)]
  144. #endif
  145. [DefaultValue(typeof (Unit), "")]
  146. [WebSysDescription ("")]
  147. [WebCategory ("Appearance")]
  148. public virtual Unit BorderWidth {
  149. get {
  150. if (style == null)
  151. return Unit.Empty;
  152. return style.BorderWidth;
  153. }
  154. set {
  155. ControlStyle.BorderWidth = value;
  156. }
  157. }
  158. [Browsable(false)]
  159. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  160. [WebSysDescription ("")]
  161. [WebCategory ("Appearance")]
  162. public Style ControlStyle {
  163. get {
  164. if (style == null) {
  165. style = this.CreateControlStyle ();
  166. if (IsTrackingViewState)
  167. style.TrackViewState ();
  168. }
  169. return style;
  170. }
  171. }
  172. [Browsable(false)]
  173. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  174. public bool ControlStyleCreated {
  175. get {
  176. return style != null;
  177. }
  178. }
  179. #if ONLY_1_1
  180. [Bindable(true)]
  181. #endif
  182. [DefaultValue("")]
  183. [WebSysDescription ("")]
  184. [WebCategory ("Appearance")]
  185. public virtual string CssClass {
  186. get {
  187. if (style == null)
  188. return string.Empty;
  189. return style.CssClass;
  190. }
  191. set {
  192. ControlStyle.CssClass = value;
  193. }
  194. }
  195. [Bindable(true)]
  196. [DefaultValue(true)]
  197. #if NET_2_0
  198. [Themeable (false)]
  199. #endif
  200. public virtual bool Enabled {
  201. get {
  202. return enabled;
  203. }
  204. set {
  205. if (enabled != value) {
  206. ViewState ["Enabled"] = value;
  207. enabled = value;
  208. }
  209. }
  210. }
  211. #if NET_2_0
  212. [Browsable (true)]
  213. [MonoTODO]
  214. public virtual new bool EnableTheming
  215. {
  216. get {
  217. throw new NotImplementedException ();
  218. }
  219. set {
  220. throw new NotImplementedException ();
  221. }
  222. }
  223. #endif
  224. #if ONLY_1_1
  225. [DefaultValue(null)]
  226. #endif
  227. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  228. [NotifyParentProperty(true)]
  229. [WebSysDescription ("")]
  230. [WebCategory ("Appearance")]
  231. public virtual FontInfo Font {
  232. get {
  233. // Oddly enough, it looks like we have to let it create the style
  234. // since we can't create a FontInfo without a style owner
  235. return ControlStyle.Font;
  236. }
  237. }
  238. #if ONLY_1_1
  239. [Bindable(true)]
  240. #endif
  241. [DefaultValue(typeof (Color), "")]
  242. [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
  243. [WebSysDescription ("")]
  244. [WebCategory ("Appearance")]
  245. public virtual Color ForeColor {
  246. get {
  247. if (style == null)
  248. return Color.Empty;
  249. return style.ForeColor;
  250. }
  251. set {
  252. ControlStyle.ForeColor = value;
  253. }
  254. }
  255. #if NET_2_0
  256. [Browsable (false)]
  257. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  258. [MonoTODO]
  259. public bool HasAttributes
  260. {
  261. get {
  262. throw new NotImplementedException ();
  263. }
  264. }
  265. #endif
  266. #if ONLY_1_1
  267. [Bindable(true)]
  268. #endif
  269. [DefaultValue(typeof (Unit), "")]
  270. [WebSysDescription ("")]
  271. [WebCategory ("Layout")]
  272. public virtual Unit Height {
  273. get {
  274. if (style == null)
  275. return Unit.Empty;
  276. return style.Height;
  277. }
  278. set {
  279. ControlStyle.Height = value;
  280. }
  281. }
  282. #if NET_2_0
  283. [Browsable (true)]
  284. [MonoTODO]
  285. public virtual new string SkinID
  286. {
  287. get {
  288. throw new NotImplementedException ();
  289. }
  290. set {
  291. throw new NotImplementedException ();
  292. }
  293. }
  294. #endif
  295. [Browsable(false)]
  296. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  297. [WebSysDescription ("")]
  298. [WebCategory ("Style")]
  299. public CssStyleCollection Style {
  300. get {
  301. return Attributes.CssStyle;
  302. }
  303. }
  304. [DefaultValue((short)0)]
  305. [WebSysDescription ("")]
  306. [WebCategory ("Behavior")]
  307. public virtual short TabIndex {
  308. get {
  309. return ViewState.GetShort ("TabIndex", 0);
  310. }
  311. set {
  312. ViewState ["TabIndex"] = value;
  313. }
  314. }
  315. #if ONLY_1_1
  316. [Bindable(true)]
  317. #endif
  318. [DefaultValue("")]
  319. #if NET_2_0
  320. [Localizable (true)]
  321. #endif
  322. [WebSysDescription ("")]
  323. [WebCategory ("Behavior")]
  324. public virtual string ToolTip {
  325. get {
  326. return ViewState.GetString ("ToolTip", string.Empty);
  327. }
  328. set {
  329. ViewState ["ToolTip"] = value;
  330. }
  331. }
  332. #if ONLY_1_1
  333. [Bindable(true)]
  334. #endif
  335. [DefaultValue(typeof (Unit), "")]
  336. [WebSysDescription ("")]
  337. [WebCategory ("Layout")]
  338. public virtual Unit Width {
  339. get {
  340. if (style == null)
  341. return Unit.Empty;
  342. return style.Width;
  343. }
  344. set {
  345. ControlStyle.Width = value;
  346. }
  347. }
  348. [Browsable(false)]
  349. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  350. protected virtual HtmlTextWriterTag TagKey {
  351. get {
  352. return tag;
  353. }
  354. }
  355. [Browsable(false)]
  356. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  357. protected virtual string TagName {
  358. get {
  359. // do this here to avoid potentially costly lookups on every control
  360. if (tag_name == null)
  361. tag_name = HtmlTextWriter.StaticGetTagName (TagKey);
  362. return tag_name;
  363. }
  364. }
  365. #if NET_2_0
  366. [MonoTODO]
  367. protected internal bool IsEnabled
  368. {
  369. get {
  370. throw new NotImplementedException ();
  371. }
  372. }
  373. #endif
  374. public void ApplyStyle (Style s)
  375. {
  376. if (s != null && !s.IsEmpty)
  377. ControlStyle.CopyFrom(s);
  378. }
  379. public void CopyBaseAttributes (WebControl controlSrc)
  380. {
  381. object o;
  382. if (controlSrc == null)
  383. return;
  384. o = controlSrc.ViewState ["Enabled"];
  385. if (o != null) {
  386. enabled = (bool)o;
  387. }
  388. o = controlSrc.ViewState ["AccessKey"];
  389. if (o != null)
  390. ViewState ["AccessKey"] = o;
  391. o = controlSrc.ViewState ["TabIndex"];
  392. if (o != null)
  393. ViewState ["TabIndex"] = o;
  394. o = controlSrc.ViewState ["ToolTip"];
  395. if (o != null)
  396. ViewState ["ToolTip"] = o;
  397. if (controlSrc.attributes != null)
  398. foreach (string s in controlSrc.attributes.Keys)
  399. Attributes [s] = controlSrc.attributes [s];
  400. }
  401. public void MergeStyle (Style s)
  402. {
  403. if (s != null && !s.IsEmpty)
  404. ControlStyle.MergeWith(s);
  405. }
  406. public virtual void RenderBeginTag (HtmlTextWriter writer)
  407. {
  408. AddAttributesToRender (writer);
  409. if (TagKey == HtmlTextWriterTag.Unknown)
  410. writer.RenderBeginTag (TagName);
  411. else
  412. writer.RenderBeginTag (TagKey);
  413. }
  414. public virtual void RenderEndTag (HtmlTextWriter writer)
  415. {
  416. writer.RenderEndTag ();
  417. }
  418. protected virtual void AddAttributesToRender (HtmlTextWriter writer)
  419. {
  420. if (ID != null)
  421. writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
  422. if (AccessKey != string.Empty)
  423. writer.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
  424. if (!enabled)
  425. writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
  426. if (ToolTip != string.Empty)
  427. writer.AddAttribute (HtmlTextWriterAttribute.Title, ToolTip);
  428. if (TabIndex != 0)
  429. writer.AddAttribute (HtmlTextWriterAttribute.Tabindex, TabIndex.ToString ());
  430. if (style != null && !style.IsEmpty)
  431. style.AddAttributesToRender(writer, this);
  432. if (attributes != null)
  433. foreach(string s in attributes.Keys)
  434. writer.AddAttribute (s, attributes [s]);
  435. }
  436. protected virtual Style CreateControlStyle()
  437. {
  438. style = new Style (ViewState);
  439. return style;
  440. }
  441. protected override void LoadViewState (object savedState)
  442. {
  443. if (savedState == null) {
  444. base.LoadViewState(null);
  445. return;
  446. }
  447. Triplet triplet = (Triplet) savedState;
  448. base.LoadViewState (triplet.First);
  449. if (triplet.Second != null) {
  450. if (attribute_state == null) {
  451. attribute_state = new StateBag ();
  452. if (IsTrackingViewState)
  453. attribute_state.TrackViewState ();
  454. }
  455. attribute_state.LoadViewState (triplet.Second);
  456. attributes = new AttributeCollection(attribute_state);
  457. }
  458. if (triplet.Third != null) {
  459. if (style == null)
  460. style = CreateControlStyle ();
  461. style.LoadViewState (triplet.Third);
  462. }
  463. enabled = ViewState.GetBool("Enabled", true);
  464. }
  465. #if NET_2_0
  466. protected internal
  467. #else
  468. protected
  469. #endif
  470. override void Render (HtmlTextWriter writer)
  471. {
  472. RenderBeginTag (writer);
  473. RenderContents (writer);
  474. RenderEndTag (writer);
  475. }
  476. #if NET_2_0
  477. protected internal
  478. #else
  479. protected
  480. #endif
  481. virtual void RenderContents (HtmlTextWriter writer)
  482. {
  483. base.Render (writer);
  484. }
  485. protected override object SaveViewState ()
  486. {
  487. object view_state;
  488. object attr_view_state = null;
  489. object style_view_state = null;
  490. view_state = base.SaveViewState ();
  491. if (attribute_state != null)
  492. attr_view_state = attribute_state.SaveViewState ();
  493. if (style != null)
  494. style_view_state = style.SaveViewState ();
  495. if (view_state == null && attr_view_state == null && style_view_state == null)
  496. return null;
  497. return new Triplet (view_state, attr_view_state, style_view_state);
  498. }
  499. protected override void TrackViewState()
  500. {
  501. if (style != null)
  502. style.TrackViewState ();
  503. if (attribute_state != null)
  504. attribute_state.TrackViewState ();
  505. base.TrackViewState ();
  506. }
  507. string IAttributeAccessor.GetAttribute (string key)
  508. {
  509. if (attributes != null)
  510. return attributes [key];
  511. return null;
  512. }
  513. void IAttributeAccessor.SetAttribute (string key, string value)
  514. {
  515. Attributes [key] = value;
  516. }
  517. }
  518. }