ErrorProvider.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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;
  27. using System.Collections;
  28. using System.ComponentModel;
  29. using System.Drawing;
  30. namespace System.Windows.Forms {
  31. [ToolboxItemFilter("System.Windows.Forms")]
  32. [ProvideProperty("IconAlignment", "System.Windows.Forms.Control, " + Consts.AssemblySystem_Windows_Forms)]
  33. [ProvideProperty("IconPadding", "System.Windows.Forms.Control, " + Consts.AssemblySystem_Windows_Forms)]
  34. [ProvideProperty("Error", "System.Windows.Forms.Control, " + Consts.AssemblySystem_Windows_Forms)]
  35. #if NET_2_0
  36. [ComplexBindingProperties ("DataSource", "DataMember")]
  37. #endif
  38. public class ErrorProvider : Component, IExtenderProvider
  39. {
  40. private class ErrorWindow : UserControl
  41. {
  42. public ErrorWindow ()
  43. {
  44. SetStyle (ControlStyles.Selectable, false);
  45. }
  46. }
  47. #region Private Classes
  48. private class ErrorProperty {
  49. public ErrorIconAlignment alignment;
  50. public int padding;
  51. public string text;
  52. public Control control;
  53. public ErrorProvider ep;
  54. private ErrorWindow window;
  55. private bool visible;
  56. private int blink_count;
  57. private EventHandler tick;
  58. private System.Windows.Forms.Timer timer;
  59. public ErrorProperty(ErrorProvider ep, Control control) {
  60. this.ep = ep;
  61. this.control = control;
  62. alignment = ErrorIconAlignment.MiddleRight;
  63. padding = 0;
  64. text = string.Empty;
  65. blink_count = 0;
  66. tick = new EventHandler(window_Tick);
  67. window = new ErrorWindow ();
  68. window.Visible = false;
  69. window.Width = ep.icon.Width;
  70. window.Height = ep.icon.Height;
  71. if (ep.container != null) {
  72. ep.container.Controls.Add(window);
  73. ep.container.Controls.SetChildIndex(window, 0);
  74. } else {
  75. control.Parent.Controls.Add(window);
  76. control.Parent.Controls.SetChildIndex(window, 0);
  77. }
  78. window.Paint += new PaintEventHandler(window_Paint);
  79. window.MouseEnter += new EventHandler(window_MouseEnter);
  80. window.MouseLeave += new EventHandler(window_MouseLeave);
  81. control.SizeChanged += new EventHandler(control_SizeLocationChanged);
  82. control.LocationChanged += new EventHandler(control_SizeLocationChanged);
  83. // Do we want to block mouse clicks? if so we need a few more events handled
  84. CalculateAlignment();
  85. }
  86. public string Text {
  87. get {
  88. return text;
  89. }
  90. set {
  91. text = value;
  92. if (text != String.Empty) {
  93. window.Visible = true;
  94. } else {
  95. window.Visible = false;
  96. }
  97. if (ep.blinkstyle != ErrorBlinkStyle.NeverBlink) {
  98. if (timer == null) {
  99. timer = new System.Windows.Forms.Timer();
  100. }
  101. timer.Interval = ep.blinkrate;
  102. timer.Tick += tick;
  103. blink_count = 0;
  104. timer.Enabled = true;
  105. }
  106. }
  107. }
  108. public ErrorIconAlignment Alignment {
  109. get {
  110. return alignment;
  111. }
  112. set {
  113. if (alignment != value) {
  114. alignment = value;
  115. CalculateAlignment();
  116. }
  117. }
  118. }
  119. public int Padding {
  120. get {
  121. return padding;
  122. }
  123. set {
  124. if (padding != value) {
  125. padding = value;
  126. CalculateAlignment();
  127. }
  128. }
  129. }
  130. private void CalculateAlignment() {
  131. if (visible) {
  132. visible = false;
  133. ep.tooltip.Visible = false;
  134. }
  135. switch (alignment) {
  136. case ErrorIconAlignment.TopLeft: {
  137. window.Left = control.Left - ep.icon.Width - padding;
  138. window.Top = control.Top;
  139. break;
  140. }
  141. case ErrorIconAlignment.TopRight: {
  142. window.Left = control.Left + control.Width + padding;
  143. window.Top = control.Top;
  144. break;
  145. }
  146. case ErrorIconAlignment.MiddleLeft: {
  147. window.Left = control.Left - ep.icon.Width - padding;
  148. window.Top = control.Top + (control.Height - ep.icon.Height) / 2;
  149. break;
  150. }
  151. case ErrorIconAlignment.MiddleRight: {
  152. window.Left = control.Left + control.Width + padding;
  153. window.Top = control.Top + (control.Height - ep.icon.Height) / 2;
  154. break;
  155. }
  156. case ErrorIconAlignment.BottomLeft: {
  157. window.Left = control.Left - ep.icon.Width - padding;
  158. window.Top = control.Top + control.Height - ep.icon.Height;
  159. break;
  160. }
  161. case ErrorIconAlignment.BottomRight: {
  162. window.Left = control.Left + control.Width + padding;
  163. window.Top = control.Top + control.Height - ep.icon.Height;
  164. break;
  165. }
  166. }
  167. }
  168. private void window_Paint(object sender, PaintEventArgs e) {
  169. if (text != string.Empty) {
  170. e.Graphics.DrawIcon(this.ep.icon, 0, 0);
  171. }
  172. }
  173. private void window_MouseEnter(object sender, EventArgs e) {
  174. if (!visible) {
  175. Size size;
  176. Point pt;
  177. visible = true;
  178. pt = Control.MousePosition;
  179. size = ThemeEngine.Current.ToolTipSize(ep.tooltip, text);
  180. ep.tooltip.Width = size.Width;
  181. ep.tooltip.Height = size.Height;
  182. ep.tooltip.Text = text;
  183. if ((pt.X + size.Width) < SystemInformation.WorkingArea.Width) {
  184. ep.tooltip.Left = pt.X;
  185. } else {
  186. ep.tooltip.Left = pt.X - size.Width;
  187. }
  188. if ((pt.Y + size.Height) < (SystemInformation.WorkingArea.Height - 16)) {
  189. ep.tooltip.Top = pt.Y + 16;
  190. } else {
  191. ep.tooltip.Top = pt.Y - size.Height;
  192. }
  193. ep.tooltip.Visible = true;
  194. }
  195. }
  196. private void window_MouseLeave(object sender, EventArgs e) {
  197. if (visible) {
  198. visible = false;
  199. ep.tooltip.Visible = false;
  200. }
  201. }
  202. private void control_SizeLocationChanged(object sender, EventArgs e) {
  203. if (visible) {
  204. visible = false;
  205. ep.tooltip.Visible = false;
  206. }
  207. CalculateAlignment();
  208. }
  209. private void window_Tick(object sender, EventArgs e) {
  210. if (timer.Enabled) {
  211. Graphics g;
  212. blink_count++;
  213. // Dunno why this POS doesn't reliably blink
  214. g = window.CreateGraphics();
  215. if ((blink_count % 2) == 0) {
  216. g.FillRectangle(ThemeEngine.Current.ResPool.GetSolidBrush(window.Parent.BackColor), window.ClientRectangle);
  217. } else {
  218. g.DrawIcon(this.ep.icon, 0, 0);
  219. }
  220. g.Dispose();
  221. if ((blink_count > 6) && (ep.blinkstyle == ErrorBlinkStyle.BlinkIfDifferentError)) {
  222. timer.Stop();
  223. blink_count = 0;
  224. }
  225. }
  226. }
  227. }
  228. #endregion
  229. #region Local Variables
  230. private int blinkrate;
  231. private ErrorBlinkStyle blinkstyle;
  232. private string datamember;
  233. private object datasource;
  234. private ContainerControl container;
  235. private Icon icon;
  236. private Hashtable controls;
  237. private ToolTip.ToolTipWindow tooltip;
  238. #if NET_2_0
  239. private object tag;
  240. #endif
  241. #endregion // Local Variables
  242. #region Public Constructors
  243. public ErrorProvider()
  244. {
  245. controls = new Hashtable();
  246. blinkrate = 250;
  247. blinkstyle = ErrorBlinkStyle.BlinkIfDifferentError;
  248. icon = (Icon)Locale.GetResource("errorProvider.ico");
  249. tooltip = new ToolTip.ToolTipWindow();
  250. }
  251. public ErrorProvider(ContainerControl parentControl) : this()
  252. {
  253. container = parentControl;
  254. }
  255. #endregion // Public Constructors
  256. #region Public Instance Properties
  257. [DefaultValue(250)]
  258. [RefreshProperties(RefreshProperties.Repaint)]
  259. public int BlinkRate {
  260. get {
  261. return blinkrate;
  262. }
  263. set {
  264. blinkrate = value;
  265. }
  266. }
  267. [DefaultValue(ErrorBlinkStyle.BlinkIfDifferentError)]
  268. public ErrorBlinkStyle BlinkStyle {
  269. get {
  270. return blinkstyle;
  271. }
  272. set {
  273. blinkstyle = value;
  274. }
  275. }
  276. [DefaultValue(null)]
  277. public ContainerControl ContainerControl {
  278. get {
  279. return container;
  280. }
  281. set {
  282. container = value;
  283. }
  284. }
  285. [MonoTODO]
  286. [DefaultValue(null)]
  287. [Editor ("System.Windows.Forms.Design.DataMemberListEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  288. public string DataMember {
  289. get {
  290. return datamember;
  291. }
  292. set {
  293. datamember = value;
  294. // FIXME - add binding magic and also update BindToDataAndErrors with it
  295. }
  296. }
  297. [MonoTODO]
  298. [DefaultValue(null)]
  299. #if NET_2_0
  300. [AttributeProvider (typeof (IListSource))]
  301. #else
  302. [TypeConverter("System.Windows.Forms.Design.DataSourceConverter, " + Consts.AssemblySystem_Design)]
  303. #endif
  304. public object DataSource {
  305. get {
  306. return datasource;
  307. }
  308. set {
  309. datasource = value;
  310. // FIXME - add binding magic and also update BindToDataAndErrors with it
  311. }
  312. }
  313. [Localizable(true)]
  314. public Icon Icon {
  315. get {
  316. return icon;
  317. }
  318. set {
  319. icon = value;
  320. }
  321. }
  322. public override ISite Site {
  323. set {
  324. base.Site = value;
  325. }
  326. }
  327. #if NET_2_0
  328. [Localizable (false)]
  329. [Bindable (true)]
  330. [TypeConverter (typeof (StringConverter))]
  331. [DefaultValue (null)]
  332. [MWFCategory ("Data")]
  333. public object Tag {
  334. get { return this.tag; }
  335. set { this.tag = value; }
  336. }
  337. #endif
  338. #endregion // Public Instance Properties
  339. #region Public Instance Methods
  340. [MonoTODO]
  341. public void BindToDataAndErrors(object newDataSource, string newDataMember) {
  342. datasource = newDataSource;
  343. datamember = newDataMember;
  344. // FIXME - finish
  345. }
  346. public bool CanExtend(object extendee) {
  347. if (!(extendee is Control)) {
  348. return false;
  349. }
  350. if ((extendee is Form) || (extendee is ToolBar)) {
  351. return false;
  352. }
  353. return true;
  354. }
  355. [Localizable(true)]
  356. [DefaultValue("")]
  357. public string GetError(Control control) {
  358. return GetErrorProperty(control).Text;
  359. }
  360. [Localizable(true)]
  361. [DefaultValue(ErrorIconAlignment.MiddleRight)]
  362. public ErrorIconAlignment GetIconAlignment(Control control) {
  363. return GetErrorProperty(control).Alignment;
  364. }
  365. [Localizable(true)]
  366. [DefaultValue(0)]
  367. public int GetIconPadding(Control control) {
  368. return GetErrorProperty(control).padding;
  369. }
  370. public void SetError(Control control, string value) {
  371. GetErrorProperty(control).Text = value;
  372. }
  373. public void SetIconAlignment(Control control, ErrorIconAlignment value) {
  374. GetErrorProperty(control).Alignment = value;
  375. }
  376. public void SetIconPadding(Control control, int padding) {
  377. GetErrorProperty(control).Padding = padding;
  378. }
  379. [MonoTODO]
  380. public void UpdateBinding() {
  381. }
  382. #endregion // Public Instance Methods
  383. #region Protected Instance Methods
  384. protected override void Dispose(bool disposing) {
  385. base.Dispose (disposing);
  386. }
  387. #endregion // Protected Instance Methods
  388. #region Private Methods
  389. private ErrorProperty GetErrorProperty(Control control) {
  390. ErrorProperty ep;
  391. ep = (ErrorProperty)controls[control];
  392. if (ep == null) {
  393. ep = new ErrorProperty(this, control);
  394. controls[control] = ep;
  395. }
  396. return ep;
  397. }
  398. #endregion // Private Methods
  399. }
  400. }