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