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
32 // Copyright (C) Novell Inc., 2004
37 // $Log: TrackBar.cs,v $
38 // Revision 1.7 2004/08/10 23:27:12 jordi
39 // add missing methods, properties, and restructure to hide extra ones
41 // Revision 1.6 2004/08/10 15:47:11 jackson
42 // Allow control to handle buffering
44 // Revision 1.5 2004/08/07 23:32:26 jordi
45 // throw exceptions of invalid enums values
47 // Revision 1.4 2004/08/06 23:18:06 pbartok
48 // - Fixed some rounding issues with float/int
50 // Revision 1.3 2004/07/27 15:53:02 jordi
51 // fixes trackbar events, def classname, methods signature
53 // Revision 1.2 2004/07/26 17:42:03 jordi
56 // Revision 1.1 2004/07/15 09:38:02 jordi
57 // Horizontal and Vertical TrackBar control implementation
63 using System.ComponentModel;
65 using System.Drawing.Imaging;
66 using System.Drawing.Drawing2D;
68 namespace System.Windows.Forms
70 public class TrackBar : Control, ISupportInitialize
74 private int tickFrequency;
75 private bool autosize;
77 private int smallChange;
78 private int largeChange;
79 private Orientation orientation;
80 private TickStyle tickStyle;
81 private Rectangle paint_area = new Rectangle ();
82 private Rectangle thumb_pos = new Rectangle (); /* Current position and size of the thumb */
83 private Rectangle thumb_area = new Rectangle (); /* Area where the thumb can scroll */
84 private bool thumb_pressed = false;
85 private int thumb_pixel_click_move;
86 private float pixel_per_pos = 0;
89 public event EventHandler Scroll;
90 public event EventHandler ValueChanged;
95 orientation = Orientation.Horizontal;
101 tickStyle = TickStyle.BottomRight;
107 SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
108 SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
111 #region Public Properties
113 public bool AutoSize {
114 get { return autosize; }
115 set { autosize = value;}
118 public override Image BackgroundImage {
119 get { return base.BackgroundImage; }
120 set { base.BackgroundImage = value; }
123 protected override CreateParams CreateParams {
125 CreateParams createParams = base.CreateParams;
126 createParams.ClassName = XplatUI.DefaultClassName;
128 createParams.Style = (int) (
129 WindowStyles.WS_CHILD |
130 WindowStyles.WS_VISIBLE);
136 protected override ImeMode DefaultImeMode {
137 get {return ImeMode.Disable; }
140 protected override Size DefaultSize {
141 get { return new System.Drawing.Size (104, 42); }
144 public override Font Font {
145 get { return base.Font; }
146 set { base.Font = value; }
149 public override Color ForeColor {
150 get { return base.ForeColor; }
151 set { base.ForeColor = value; }
155 public int LargeChange {
156 get { return largeChange; }
159 throw new Exception( string.Format("Value '{0}' must be greater than or equal to 0.", value));
167 get { return maximum; }
169 if (maximum != value) {
172 if (maximum < minimum)
181 get { return minimum; }
184 if (Minimum != value) {
187 if (minimum > maximum)
195 public Orientation Orientation {
196 get { return orientation; }
198 if (!Enum.IsDefined (typeof (Orientation), value))
199 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for Orientation", value));
201 /* Orientation can be changed once the control has been created*/
202 if (orientation != value) {
205 int old_witdh = Width;
213 public int SmallChange {
214 get { return smallChange;}
217 throw new Exception( string.Format("Value '{0}' must be greater than or equal to 0.", value));
219 if (smallChange != value) {
227 public override string Text {
228 get { return base.Text; }
229 set { base.Text = value; }
233 public int TickFrequency {
234 get { return tickFrequency; }
237 tickFrequency = value;
243 public TickStyle TickStyle {
244 get { return tickStyle; }
246 if (!Enum.IsDefined (typeof (TickStyle), value))
247 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for TickStyle", value));
249 if (tickStyle != value) {
258 get { return position; }
260 if (value < Minimum || value > Maximum)
261 throw new ArgumentException(
262 string.Format("'{0}' is not a valid value for 'Value'. 'Value' should be between 'Minimum' and 'Maximum'", value));
264 if (position != value) {
267 if (ValueChanged != null)
268 ValueChanged (this, new EventArgs ());
275 #endregion //Public Properties
277 #region Public Methods
279 public virtual void BeginInit ()
284 protected override void CreateHandle ()
286 base.CreateHandle ();
290 public virtual void EndInit ()
295 protected override bool IsInputKey (Keys keyData)
300 protected override void OnBackColorChanged (EventArgs e)
305 protected override void OnHandleCreated (EventArgs e)
308 if (Orientation == Orientation.Horizontal)
309 Size = new Size (Width, 45);
311 Size = new Size (50, Height);
314 CreateBuffers (Width, Height);
316 UpdatePos (Value, true);
317 UpdatePixelPerPos ();
320 UpdatePos (Value, true);
325 protected override void OnMouseWheel (MouseEventArgs e)
327 if (!Enabled) return;
336 protected virtual void OnScroll (EventArgs e)
342 protected virtual void OnValueChanged (EventArgs e)
344 if (ValueChanged != null)
345 ValueChanged (this, e);
348 public void SetRange (int minValue, int maxValue)
356 public override string ToString()
358 return string.Format("System.Windows.Forms.Trackbar, Minimum: {0}, Maximum: {1}, Value: {2}",
359 Minimum, Maximum, Value);
364 protected override void WndProc (ref Message m)
368 switch ((Msg) m.Msg) {
370 case Msg.WM_LBUTTONDOWN:
371 OnMouseDownTB (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
372 clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()),
377 case Msg.WM_MOUSEMOVE:
378 OnMouseMoveTB (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
380 LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()),
390 PaintEventArgs paint_event;
392 paint_event = XplatUI.PaintEventStart (Handle);
393 OnPaint_TB (paint_event);
394 XplatUI.PaintEventEnd (Handle);
398 case Msg.WM_ERASEBKGND:
399 m.Result = (IntPtr)1; /* Disable background painting to avoid flickering */
406 base.WndProc (ref m);
409 #endregion Public Methods
411 #region Private Methods
413 private void UpdateArea ()
415 paint_area.X = paint_area.Y = 0;
416 paint_area.Width = Width;
417 paint_area.Height = Height;
419 UpdatePixelPerPos ();
420 //Console.WriteLine ("UpdateArea: {0} {1} {2}", thumb_area.Width, thumb_pos.Width, pixel_per_pos);
424 private void UpdateThumbPos (int pixel, bool update_value)
427 //Console.WriteLine ("UpdateThumbPos: " + pixel + " per " + pixel_per_pos);
429 if (orientation == Orientation.Horizontal) {
431 if (pixel < thumb_area.X)
432 thumb_pos.X = thumb_area.X;
434 if (pixel > thumb_area.X + thumb_area.Width - thumb_pos.Width)
435 thumb_pos.X = thumb_area.X + thumb_area.Width - thumb_pos.Width;
439 new_pos = (float) (thumb_pos.X - thumb_area.X);
440 new_pos = new_pos / pixel_per_pos;
444 if (pixel < thumb_area.Y)
445 thumb_pos.Y = thumb_area.Y;
447 if (pixel > thumb_area.Y + thumb_area.Height - thumb_pos.Height)
448 thumb_pos.Y = thumb_area.Y + thumb_area.Height - thumb_pos.Height;
452 new_pos = (float) (thumb_pos.Y - thumb_area.Y);
453 new_pos = new_pos / pixel_per_pos;
458 //Console.WriteLine ("UpdateThumbPos: thumb_pos.Y {0} thumb_area.Y {1} pixel_per_pos {2}, new pos {3}, pixel {4}",
459 // thumb_pos.Y, thumb_area.Y, pixel_per_pos, new_pos, pixel);
462 UpdatePos ((int) new_pos, false);
465 private void LargeIncrement ()
467 UpdatePos (position + LargeChange, true);
469 OnScroll (new EventArgs ());
472 private void LargeDecrement ()
474 UpdatePos (position - LargeChange, true);
476 OnScroll (new EventArgs ());
479 private void SmallIncrement ()
481 UpdatePos (position + SmallChange, true);
483 OnScroll (new EventArgs ());
486 private void SmallDecrement ()
488 UpdatePos (position - SmallChange, true);
490 OnScroll (new EventArgs ());
493 private void UpdatePixelPerPos ()
495 pixel_per_pos = ((float)(thumb_area.Width)
496 / (float) (1 + Maximum - Minimum));
501 int ticks = (Maximum - Minimum) / tickFrequency;
503 ThemeEngine.Current.DrawTrackBar (DeviceContext, paint_area, ref thumb_pos, ref thumb_area,
504 tickStyle, ticks, Orientation, Focused);
507 private void UpdatePos (int newPos, bool update_trumbpos)
511 if (newPos < minimum)
514 if (newPos > maximum)
519 if (orientation == Orientation.Horizontal) {
521 UpdateThumbPos (thumb_area.X + (int)(((float)(Value - Minimum)) * pixel_per_pos), false);
525 UpdateThumbPos (thumb_area.Y + (int)(((float)(Value - Minimum)) * pixel_per_pos), false);
529 private void OnMouseDownTB (MouseEventArgs e)
531 if (!Enabled) return;
533 //System.Console.WriteLine ("OnMouseDown" + thumb_pos);
535 Point point = new Point (e.X, e.Y);
537 if (orientation == Orientation.Horizontal) {
539 if (thumb_pos.Contains (point)) {
540 //XplatUI.GrabWindow (Handle);
541 thumb_pressed = true;
543 thumb_pixel_click_move = e.X;
546 if (paint_area.Contains (point)) {
547 if (e.X > thumb_pos.X + thumb_pos.Width)
557 if (thumb_pos.Contains (point)) {
558 //XplatUI.GrabWindow (Handle);
559 thumb_pressed = true;
561 thumb_pixel_click_move = e.Y;
564 if (paint_area.Contains (point)) {
565 if (e.Y > thumb_pos.Y + thumb_pos.Height)
577 private void OnMouseMoveTB (MouseEventArgs e)
579 if (!Enabled) return;
581 Point pnt = new Point (e.X, e.Y);
583 /* Moving the thumb */
584 if (thumb_pos.Contains (pnt) && thumb_pressed) {
586 //System.Console.WriteLine ("OnMouseMove " + thumb_pressed);
587 //XplatUI.GrabWindow (Handle);
590 if (orientation == Orientation.Horizontal)
591 pixel_pos = e.X - (thumb_pixel_click_move - thumb_pos.X);
593 pixel_pos = e.Y - (thumb_pixel_click_move - thumb_pos.Y);
595 UpdateThumbPos (pixel_pos, true);
597 if (orientation == Orientation.Horizontal)
598 thumb_pixel_click_move = e.X;
600 thumb_pixel_click_move = e.Y;
602 OnScroll (new EventArgs ());
604 //System.Console.WriteLine ("OnMouseMove thumb "+ e.Y
605 // + " clickpos " + thumb_pixel_click_move + " pos:" + thumb_pos.Y);
610 if (!thumb_pos.Contains (pnt) && thumb_pressed) {
611 //XplatUI.ReleaseWindow (Handle);
612 thumb_pressed = false;
618 private void OnResize_TB ()
620 //Console.WriteLine ("OnResize");
622 if (Width <= 0 || Height <= 0)
626 CreateBuffers (Width, Height);
629 private void OnPaint_TB (PaintEventArgs pevent)
631 if (Width <= 0 || Height <= 0 || Visible == false)
634 /* Copies memory drawing buffer to screen*/
637 pevent.Graphics.DrawImage (ImageBuffer, 0, 0);
640 #endregion // Private Methods