Allow control to handle buffering
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / TrackBar.cs
1 //
2 // System.Windows.Forms.TrackBar.cs
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 //
23 // Autors:
24 //              Jordi Mas i Hernandez, jordi@ximian.com
25 //
26 // TODO:
27 //              - Draging the thumb does not behave exactly like in Win32
28 //              - The AutoSize functionality seems quite broken for vertical controls in .Net 1.1. Not
29 //              sure if we are implementing it the right way.
30 //              - Vertical orientation still needs some work
31 //              - OnMouseWheel event
32 //
33 // Copyright (C) Novell Inc., 2004
34 //
35 //
36 // $Revision: 1.6 $
37 // $Modtime: $
38 // $Log: TrackBar.cs,v $
39 // Revision 1.6  2004/08/10 15:47:11  jackson
40 // Allow control to handle buffering
41 //
42 // Revision 1.5  2004/08/07 23:32:26  jordi
43 // throw exceptions of invalid enums values
44 //
45 // Revision 1.4  2004/08/06 23:18:06  pbartok
46 // - Fixed some rounding issues with float/int
47 //
48 // Revision 1.3  2004/07/27 15:53:02  jordi
49 // fixes trackbar events, def classname, methods signature
50 //
51 // Revision 1.2  2004/07/26 17:42:03  jordi
52 // Theme support
53 //
54 // Revision 1.1  2004/07/15 09:38:02  jordi
55 // Horizontal and Vertical TrackBar control implementation
56 //
57 //
58
59 // NOT COMPLETE
60
61 using System.ComponentModel;
62 using System.Drawing;
63 using System.Drawing.Imaging;
64 using System.Drawing.Drawing2D;
65
66 namespace System.Windows.Forms
67 {       
68         public class TrackBar : Control, ISupportInitialize
69         {
70                 private int minimum;
71                 private int maximum;
72                 private int tickFrequency;
73                 private bool autosize;
74                 private int position;
75                 private int smallChange;
76                 private int largeChange;
77                 private Orientation orientation;
78                 private TickStyle tickStyle;
79                 private Rectangle paint_area = new Rectangle ();
80                 private Rectangle thumb_pos = new Rectangle ();  /* Current position and size of the thumb */
81                 private Rectangle thumb_area = new Rectangle (); /* Area where the thumb can scroll */
82                 private bool thumb_pressed = false;
83                 private int thumb_pixel_click_move;
84                 private float pixel_per_pos = 0;                
85
86                 #region Events
87                 public event EventHandler Scroll;
88                 public event EventHandler ValueChanged;         
89
90                 #endregion // Events
91
92                 public TrackBar ()
93                 {
94                         orientation = Orientation.Horizontal;
95                         minimum = 0;
96                         maximum = 10;
97                         tickFrequency = 1;
98                         autosize = true;
99                         position = 0;
100                         tickStyle = TickStyle.BottomRight;
101                         smallChange = 1;
102                         largeChange = 5;
103                         Scroll = null;
104                         ValueChanged  = null;           
105
106                         SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
107                         SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);                     
108                 }
109
110                 #region Public Properties
111
112                 public bool AutoSize {
113                         get { return autosize; }
114                         set { autosize = value;}
115                 }
116
117                 public override Image BackgroundImage {
118                         get { return base.BackgroundImage; }
119                         set { base.BackgroundImage = value; }
120                 }
121
122                 public override Font Font {
123                         get { return base.Font; }
124                         set { base.Font = value; }
125                 }
126
127                 public override Color ForeColor {
128                         get { return base.ForeColor; }
129                         set { base.ForeColor = value; }
130                 }
131
132
133                 public int LargeChange {
134                         get { return largeChange; }
135                         set {
136                                 if (value < 0)
137                                         throw new Exception( string.Format("Value '{0}' must be greater than or equal to 0.", value));
138
139                                 largeChange = value;
140                                 Invalidate ();
141                         }
142                 }
143
144                 public int Maximum {
145                         get { return maximum; }
146                         set {
147                                 maximum = value;
148
149                                 if (maximum < minimum)
150                                         minimum = maximum;
151
152                                 Invalidate ();
153                         }
154                 }
155
156                 public int Minimum {
157                         get { return minimum; }
158                         set {
159                                 minimum = value;
160
161                                 if (minimum > maximum)
162                                         maximum = minimum;
163
164                                 Invalidate ();
165                         }
166                 }
167
168                 public Orientation Orientation {
169                         get { return orientation; }
170                         set {
171                                 if (!Enum.IsDefined (typeof (Orientation), value))
172                                         throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for Orientation", value));
173
174                                 /* Orientation can be changed once the control has been created*/
175                                 orientation = value;
176                                 
177                                 int old_witdh = Width;
178                                 Width = Height;
179                                 Height = old_witdh;
180                                 Invalidate (); 
181                         }
182                 }
183
184                 public int SmallChange {
185                         get { return smallChange;}
186                         set {
187                                 if ( value < 0 )
188                                         throw new Exception( string.Format("Value '{0}' must be greater than or equal to 0.", value));
189
190                                 smallChange = value;
191                                 Invalidate ();
192                         }
193                 }
194
195
196                 public override string Text {
197                         get {   return base.Text; }
198                         set {   base.Text = value; }
199                 }
200
201
202                 public int TickFrequency {
203                         get { return tickFrequency; }
204                         set {
205                                 if ( value > 0 ) {
206                                         tickFrequency = value;
207                                         Invalidate ();
208                                 }
209                         }
210                 }
211
212                 public TickStyle TickStyle {
213                         get { return tickStyle; }
214                         set { 
215                                 
216                                 if (!Enum.IsDefined (typeof (TickStyle), value))
217                                         throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for TickStyle", value));
218
219                                 tickStyle = value;
220                         }
221                 }
222
223
224                 public int Value {
225                         get { return position; }
226                         set {
227                                 if (value < Minimum || value > Maximum)
228                                         throw new ArgumentException(
229                                                 string.Format("'{0}' is not a valid value for 'Value'. 'Value' should be between 'Minimum' and 'Maximum'", value));
230                                 
231                                 if (position != value) {                                                                                                        
232                                         position = value;                                       
233                                         
234                                         if (ValueChanged != null)                               
235                                                 ValueChanged (this, new EventArgs ());
236                                                 
237                                         Invalidate ();
238                                 }                               
239                         }
240                 }
241
242                 #endregion //Public Properties
243
244                 #region Public Methods
245
246                 public void SetRange (int minValue, int maxValue)
247                 {
248                         Minimum = minValue;
249                         Maximum = maxValue;
250
251                         Invalidate ();
252                 }
253
254                 public override string ToString()
255                 {
256                         return string.Format("System.Windows.Forms.Trackbar, Minimum: {0}, Maximum: {1}, Value: {2}",
257                                                 Minimum, Maximum, Value);
258                 }
259
260                 protected override CreateParams CreateParams {
261                         get {
262                                 CreateParams createParams = base.CreateParams;
263                                 createParams.ClassName = XplatUI.DefaultClassName;
264
265                                 createParams.Style = (int) (
266                                         WindowStyles.WS_CHILD |
267                                         WindowStyles.WS_VISIBLE);
268
269                                 return createParams;
270                         }
271                 }
272
273                 protected override ImeMode DefaultImeMode {
274                         get {return ImeMode.Disable; }
275                 }
276
277                 protected override Size DefaultSize {
278                         get { return new System.Drawing.Size (104, 42); }
279                 }       
280
281                 public virtual void BeginInit()         
282                 {
283
284                 }
285
286                 public virtual void EndInit()           
287                 {
288
289                 }
290
291                 #endregion Public Methods
292
293                 #region Private Methods
294
295                 private void fire_scroll_event ()
296                 {
297                         if (Scroll == null)
298                                 return;
299
300                         Scroll (this, new EventArgs ());
301                 }
302
303                 private void UpdateArea ()
304                 {
305                         paint_area.X = paint_area.Y = 0;
306                         paint_area.Width = Width;
307                         paint_area.Height = Height;
308
309                         UpdatePixelPerPos ();
310                         //Console.WriteLine ("UpdateArea: {0} {1} {2}", thumb_area.Width, thumb_pos.Width, pixel_per_pos);
311
312                 }
313
314                 private void UpdateThumbPos (int pixel, bool update_value)
315                 {
316                         float new_pos = 0;
317                         //Console.WriteLine ("UpdateThumbPos: " + pixel  + " per " + pixel_per_pos);
318
319                         if (orientation == Orientation.Horizontal) {
320
321                                 if (pixel < thumb_area.X)
322                                         thumb_pos.X = thumb_area.X;
323                                 else
324                                         if (pixel > thumb_area.X + thumb_area.Width - thumb_pos.Width)
325                                                 thumb_pos.X = thumb_area.X +  thumb_area.Width - thumb_pos.Width;
326                                         else
327                                                 thumb_pos.X = pixel;
328
329                                 new_pos = (float) (thumb_pos.X - thumb_area.X);
330                                 new_pos = new_pos / pixel_per_pos;
331
332                         } else {
333
334                                 if (pixel < thumb_area.Y)
335                                         thumb_pos.Y = thumb_area.Y;
336                                 else
337                                         if (pixel > thumb_area.Y + thumb_area.Height - thumb_pos.Height)
338                                                 thumb_pos.Y = thumb_area.Y +  thumb_area.Height - thumb_pos.Height;
339                                         else
340                                                 thumb_pos.Y = pixel;
341
342                                 new_pos = (float) (thumb_pos.Y - thumb_area.Y);
343                                 new_pos = new_pos / pixel_per_pos;
344
345                         }
346
347
348                         //Console.WriteLine ("UpdateThumbPos: thumb_pos.Y {0} thumb_area.Y {1} pixel_per_pos {2}, new pos {3}, pixel {4}",
349                         //      thumb_pos.Y, thumb_area.Y, pixel_per_pos, new_pos, pixel);
350
351                         if (update_value)
352                                 UpdatePos ((int) new_pos, false);
353                 }
354
355                 private void LargeIncrement()
356                 {
357                         //Console.WriteLine ("large inc: {0} {1}", position, LargeChange);
358                         UpdatePos (position + LargeChange, true);
359
360                         fire_scroll_event ();
361                 }
362
363                 private void LargeDecrement()
364                 {
365                         //Console.WriteLine ("large dec: {0} {1}", position, LargeChange);
366                         UpdatePos (position - LargeChange, true);
367
368                         fire_scroll_event ();
369                 }
370
371                 private void UpdatePixelPerPos ()
372                 {
373                         pixel_per_pos = ((float)(thumb_area.Width)
374                                 / (float) (1 + Maximum - Minimum));
375                 }
376
377                 private void UpdatePos (int newPos, bool update_trumbpos)
378                 {
379                         int old = position;
380
381                         if (newPos < minimum)
382                                 Value = minimum;
383                         else
384                                 if (newPos > maximum)
385                                         Value = maximum;
386                                 else
387                                         Value = newPos;
388
389                         if (orientation == Orientation.Horizontal) {
390                                 if (update_trumbpos)
391                                         UpdateThumbPos (thumb_area.X + (int)(((float)(Value - Minimum)) * pixel_per_pos), false);
392                         }
393                         else {
394                                 if (update_trumbpos)
395                                         UpdateThumbPos (thumb_area.Y + (int)(((float)(Value - Minimum)) * pixel_per_pos), false);
396                         }
397                 }
398
399                 #endregion // Private Methods
400
401                 #region Override Event Methods
402
403                 protected override void OnResize (EventArgs e)
404                 {
405                         //Console.WriteLine ("OnResize");
406                         base.OnResize (e);
407
408                         if (Width <= 0 || Height <= 0)
409                                 return;
410
411                         UpdateArea ();
412                         CreateBuffers (Width, Height);
413                 }
414
415
416                 protected override void OnHandleCreated (EventArgs e)
417                 {
418                         base.OnHandleCreated(e);
419                         //Console.WriteLine ("OnHandleCreated");
420                         UpdateArea ();
421
422                         CreateBuffers (Width, Height);
423
424                         UpdatePos (Value, true);
425                         UpdatePixelPerPos ();
426
427                         Draw();
428                         UpdatePos (Value, true);
429
430                         if (AutoSize)
431                                 if (Orientation == Orientation.Horizontal)
432                                         Size = new Size (Width, 45);
433                                 else
434                                         Size = new Size (50, Height);
435                 }
436
437
438                 /* Disable background painting to avoid flickering, since we do our painting*/
439                 protected override void OnPaintBackground (PaintEventArgs pevent)
440                 {
441                         // None
442                 }
443
444                 private void Draw ()
445                 {
446                         int ticks = (Maximum - Minimum) / tickFrequency;
447                         
448                         ThemeEngine.Current.DrawTrackBar (DeviceContext, paint_area, ref thumb_pos, ref thumb_area,
449                                 tickStyle, ticks, Orientation, Focused);
450                 }
451
452                 protected override void OnPaint (PaintEventArgs pevent)
453                 {               
454                         Console.WriteLine ("OnDraw");
455
456                         if (Width <= 0 || Height <=  0 || Visible == false)
457                                 return;
458
459                         /* Copies memory drawing buffer to screen*/
460                         UpdateArea ();
461                         Draw();
462                         pevent.Graphics.DrawImage (ImageBuffer, 0, 0);
463                 }
464
465                 protected override void OnMouseDown (MouseEventArgs e)
466                 {
467                         if (!Enabled) return;
468                         
469                         //System.Console.WriteLine ("OnMouseDown" + thumb_pos);                 
470                         
471                         Point point = new Point (e.X, e.Y);
472
473                         if (orientation == Orientation.Horizontal) {
474
475                                 if (thumb_pos.Contains (point)) {
476                                         thumb_pressed = true;
477                                         Invalidate ();
478                                         thumb_pixel_click_move = e.X;
479                                 }
480                                 else {
481                                         if (paint_area.Contains (point)) {
482                                                 if (e.X > thumb_pos.X + thumb_pos.Width)
483                                                         LargeIncrement ();
484                                                 else
485                                                         LargeDecrement ();
486
487                                                 Invalidate ();
488                                         }
489                                 }
490                         }
491                         else {
492                                 if (thumb_pos.Contains (point)) {
493                                         thumb_pressed = true;
494                                         Invalidate ();
495                                         thumb_pixel_click_move = e.Y;
496                                 }
497                                 else {
498                                         if (paint_area.Contains (point)) {
499                                                 if (e.Y > thumb_pos.Y + thumb_pos.Height)
500                                                         LargeIncrement ();
501                                                 else
502                                                         LargeDecrement ();
503
504                                                 Invalidate ();
505                                         }
506                                 }
507
508                         }
509                 }
510
511                 protected override void OnMouseMove (MouseEventArgs e)
512                 {                       
513                         if (!Enabled) return;
514                         
515                         //System.Console.WriteLine ("OnMouseMove " + thumb_pressed);
516                         Point pnt = new Point (e.X, e.Y);
517
518                         /* Moving the thumb */
519                         if (thumb_pos.Contains (pnt) && thumb_pressed) {
520
521                                 System.Console.WriteLine ("OnMouseMove " + thumb_pressed);
522
523                                 int pixel_pos;
524
525                                 if (orientation == Orientation.Horizontal)
526                                         pixel_pos = e.X - (thumb_pixel_click_move - thumb_pos.X);
527                                 else
528                                         pixel_pos = e.Y - (thumb_pixel_click_move - thumb_pos.Y);
529
530                                 UpdateThumbPos (pixel_pos, true);
531
532                                 if (orientation == Orientation.Horizontal)
533                                         thumb_pixel_click_move = e.X;
534                                 else
535                                         thumb_pixel_click_move = e.Y;
536
537
538                                 fire_scroll_event ();
539
540                                 //System.Console.WriteLine ("OnMouseMove thumb "+ e.Y
541                                 //      + " clickpos " + thumb_pixel_click_move   + " pos:" + thumb_pos.Y);
542
543                                 Invalidate ();
544                         }
545
546                         if (!thumb_pos.Contains (pnt) && thumb_pressed) {
547                                 thumb_pressed = false;
548                                 Invalidate ();
549                         }
550
551                 }
552                 
553                 protected override void OnMouseWheel (MouseEventArgs e) 
554                 {
555                         if (!Enabled) return;
556                         
557                         System.Console.WriteLine ("OnMouseWheel delta: " + e.Delta + " clicks:" + e.Clicks);
558                                         
559                 }
560                 
561                 #endregion //Override Event Methods
562         }
563 }
564