2 // System.Windows.Forms.TrackBar.cs
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:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
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.
24 // Jordi Mas i Hernandez, jordi@ximian.com
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
33 // Copyright (C) Novell Inc., 2004
38 // $Log: TrackBar.cs,v $
39 // Revision 1.6 2004/08/10 15:47:11 jackson
40 // Allow control to handle buffering
42 // Revision 1.5 2004/08/07 23:32:26 jordi
43 // throw exceptions of invalid enums values
45 // Revision 1.4 2004/08/06 23:18:06 pbartok
46 // - Fixed some rounding issues with float/int
48 // Revision 1.3 2004/07/27 15:53:02 jordi
49 // fixes trackbar events, def classname, methods signature
51 // Revision 1.2 2004/07/26 17:42:03 jordi
54 // Revision 1.1 2004/07/15 09:38:02 jordi
55 // Horizontal and Vertical TrackBar control implementation
61 using System.ComponentModel;
63 using System.Drawing.Imaging;
64 using System.Drawing.Drawing2D;
66 namespace System.Windows.Forms
68 public class TrackBar : Control, ISupportInitialize
72 private int tickFrequency;
73 private bool autosize;
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;
87 public event EventHandler Scroll;
88 public event EventHandler ValueChanged;
94 orientation = Orientation.Horizontal;
100 tickStyle = TickStyle.BottomRight;
106 SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
107 SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
110 #region Public Properties
112 public bool AutoSize {
113 get { return autosize; }
114 set { autosize = value;}
117 public override Image BackgroundImage {
118 get { return base.BackgroundImage; }
119 set { base.BackgroundImage = value; }
122 public override Font Font {
123 get { return base.Font; }
124 set { base.Font = value; }
127 public override Color ForeColor {
128 get { return base.ForeColor; }
129 set { base.ForeColor = value; }
133 public int LargeChange {
134 get { return largeChange; }
137 throw new Exception( string.Format("Value '{0}' must be greater than or equal to 0.", value));
145 get { return maximum; }
149 if (maximum < minimum)
157 get { return minimum; }
161 if (minimum > maximum)
168 public Orientation Orientation {
169 get { return orientation; }
171 if (!Enum.IsDefined (typeof (Orientation), value))
172 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for Orientation", value));
174 /* Orientation can be changed once the control has been created*/
177 int old_witdh = Width;
184 public int SmallChange {
185 get { return smallChange;}
188 throw new Exception( string.Format("Value '{0}' must be greater than or equal to 0.", value));
196 public override string Text {
197 get { return base.Text; }
198 set { base.Text = value; }
202 public int TickFrequency {
203 get { return tickFrequency; }
206 tickFrequency = value;
212 public TickStyle TickStyle {
213 get { return tickStyle; }
216 if (!Enum.IsDefined (typeof (TickStyle), value))
217 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for TickStyle", value));
225 get { return position; }
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));
231 if (position != value) {
234 if (ValueChanged != null)
235 ValueChanged (this, new EventArgs ());
242 #endregion //Public Properties
244 #region Public Methods
246 public void SetRange (int minValue, int maxValue)
254 public override string ToString()
256 return string.Format("System.Windows.Forms.Trackbar, Minimum: {0}, Maximum: {1}, Value: {2}",
257 Minimum, Maximum, Value);
260 protected override CreateParams CreateParams {
262 CreateParams createParams = base.CreateParams;
263 createParams.ClassName = XplatUI.DefaultClassName;
265 createParams.Style = (int) (
266 WindowStyles.WS_CHILD |
267 WindowStyles.WS_VISIBLE);
273 protected override ImeMode DefaultImeMode {
274 get {return ImeMode.Disable; }
277 protected override Size DefaultSize {
278 get { return new System.Drawing.Size (104, 42); }
281 public virtual void BeginInit()
286 public virtual void EndInit()
291 #endregion Public Methods
293 #region Private Methods
295 private void fire_scroll_event ()
300 Scroll (this, new EventArgs ());
303 private void UpdateArea ()
305 paint_area.X = paint_area.Y = 0;
306 paint_area.Width = Width;
307 paint_area.Height = Height;
309 UpdatePixelPerPos ();
310 //Console.WriteLine ("UpdateArea: {0} {1} {2}", thumb_area.Width, thumb_pos.Width, pixel_per_pos);
314 private void UpdateThumbPos (int pixel, bool update_value)
317 //Console.WriteLine ("UpdateThumbPos: " + pixel + " per " + pixel_per_pos);
319 if (orientation == Orientation.Horizontal) {
321 if (pixel < thumb_area.X)
322 thumb_pos.X = thumb_area.X;
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;
329 new_pos = (float) (thumb_pos.X - thumb_area.X);
330 new_pos = new_pos / pixel_per_pos;
334 if (pixel < thumb_area.Y)
335 thumb_pos.Y = thumb_area.Y;
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;
342 new_pos = (float) (thumb_pos.Y - thumb_area.Y);
343 new_pos = new_pos / pixel_per_pos;
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);
352 UpdatePos ((int) new_pos, false);
355 private void LargeIncrement()
357 //Console.WriteLine ("large inc: {0} {1}", position, LargeChange);
358 UpdatePos (position + LargeChange, true);
360 fire_scroll_event ();
363 private void LargeDecrement()
365 //Console.WriteLine ("large dec: {0} {1}", position, LargeChange);
366 UpdatePos (position - LargeChange, true);
368 fire_scroll_event ();
371 private void UpdatePixelPerPos ()
373 pixel_per_pos = ((float)(thumb_area.Width)
374 / (float) (1 + Maximum - Minimum));
377 private void UpdatePos (int newPos, bool update_trumbpos)
381 if (newPos < minimum)
384 if (newPos > maximum)
389 if (orientation == Orientation.Horizontal) {
391 UpdateThumbPos (thumb_area.X + (int)(((float)(Value - Minimum)) * pixel_per_pos), false);
395 UpdateThumbPos (thumb_area.Y + (int)(((float)(Value - Minimum)) * pixel_per_pos), false);
399 #endregion // Private Methods
401 #region Override Event Methods
403 protected override void OnResize (EventArgs e)
405 //Console.WriteLine ("OnResize");
408 if (Width <= 0 || Height <= 0)
412 CreateBuffers (Width, Height);
416 protected override void OnHandleCreated (EventArgs e)
418 base.OnHandleCreated(e);
419 //Console.WriteLine ("OnHandleCreated");
422 CreateBuffers (Width, Height);
424 UpdatePos (Value, true);
425 UpdatePixelPerPos ();
428 UpdatePos (Value, true);
431 if (Orientation == Orientation.Horizontal)
432 Size = new Size (Width, 45);
434 Size = new Size (50, Height);
438 /* Disable background painting to avoid flickering, since we do our painting*/
439 protected override void OnPaintBackground (PaintEventArgs pevent)
446 int ticks = (Maximum - Minimum) / tickFrequency;
448 ThemeEngine.Current.DrawTrackBar (DeviceContext, paint_area, ref thumb_pos, ref thumb_area,
449 tickStyle, ticks, Orientation, Focused);
452 protected override void OnPaint (PaintEventArgs pevent)
454 Console.WriteLine ("OnDraw");
456 if (Width <= 0 || Height <= 0 || Visible == false)
459 /* Copies memory drawing buffer to screen*/
462 pevent.Graphics.DrawImage (ImageBuffer, 0, 0);
465 protected override void OnMouseDown (MouseEventArgs e)
467 if (!Enabled) return;
469 //System.Console.WriteLine ("OnMouseDown" + thumb_pos);
471 Point point = new Point (e.X, e.Y);
473 if (orientation == Orientation.Horizontal) {
475 if (thumb_pos.Contains (point)) {
476 thumb_pressed = true;
478 thumb_pixel_click_move = e.X;
481 if (paint_area.Contains (point)) {
482 if (e.X > thumb_pos.X + thumb_pos.Width)
492 if (thumb_pos.Contains (point)) {
493 thumb_pressed = true;
495 thumb_pixel_click_move = e.Y;
498 if (paint_area.Contains (point)) {
499 if (e.Y > thumb_pos.Y + thumb_pos.Height)
511 protected override void OnMouseMove (MouseEventArgs e)
513 if (!Enabled) return;
515 //System.Console.WriteLine ("OnMouseMove " + thumb_pressed);
516 Point pnt = new Point (e.X, e.Y);
518 /* Moving the thumb */
519 if (thumb_pos.Contains (pnt) && thumb_pressed) {
521 System.Console.WriteLine ("OnMouseMove " + thumb_pressed);
525 if (orientation == Orientation.Horizontal)
526 pixel_pos = e.X - (thumb_pixel_click_move - thumb_pos.X);
528 pixel_pos = e.Y - (thumb_pixel_click_move - thumb_pos.Y);
530 UpdateThumbPos (pixel_pos, true);
532 if (orientation == Orientation.Horizontal)
533 thumb_pixel_click_move = e.X;
535 thumb_pixel_click_move = e.Y;
538 fire_scroll_event ();
540 //System.Console.WriteLine ("OnMouseMove thumb "+ e.Y
541 // + " clickpos " + thumb_pixel_click_move + " pos:" + thumb_pos.Y);
546 if (!thumb_pos.Contains (pnt) && thumb_pressed) {
547 thumb_pressed = false;
553 protected override void OnMouseWheel (MouseEventArgs e)
555 if (!Enabled) return;
557 System.Console.WriteLine ("OnMouseWheel delta: " + e.Delta + " clicks:" + e.Clicks);
561 #endregion //Override Event Methods