2 // System.Windows.Forms.ScrollBar.cs
5 // stubbed out by Daniel Carrera (dcarrera@math.toronto.edu)
6 // Dennis Hayes (dennish@raytek.com)
7 // Aleksey Ryabchuk (ryabchuk@yahoo.com)
9 // (C) 2002 Ximian, Inc
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //public void add_Click(EventHandler value);
34 //public void add_MouseDown(MouseEventHandler value);
35 //public void add_MouseMove(MouseEventHandler value);
36 //public void add_MouseUp(MouseEventHandler value);
37 //public void add_Paint(PaintEventHandler value);
39 //public Font Font {get; set;}
43 using System
.ComponentModel
;
44 using System
.Runtime
.InteropServices
;
46 namespace System
.Windows
.Forms
{
49 // Implements the basic functionality of a scroll bar control.
52 public class ScrollBar
: Control
{
60 public ScrollBar() : base()
62 //spec says tabstop defaults to false.
71 [EditorBrowsable (EditorBrowsableState
.Never
)]
72 public override Color BackColor
{
73 get { return base.BackColor; }
74 set { base.BackColor = value;}
77 [EditorBrowsable (EditorBrowsableState
.Never
)]
78 public override Image BackgroundImage
{
79 get { return base.BackgroundImage; }
80 set { base.BackgroundImage = value; }
83 [EditorBrowsable (EditorBrowsableState
.Never
)]
84 public override Color ForeColor
{
85 get { return base.ForeColor; }
86 set { base.ForeColor = value; }
89 [EditorBrowsable (EditorBrowsableState
.Never
)]
90 public new ImeMode ImeMode
{
91 get { return base.ImeMode; }
92 set { base.ImeMode = value; }
96 public int LargeChange
{
97 get { return largeChange; }
100 throw new Exception( string.Format("Value '{0}' must be greater than or equal to 0.", value));
108 get { return maximum; }
112 if ( maximum
< minimum
)
115 if ( IsHandleCreated
)
116 setScrollRange ( Minimum
, maximum
);
122 get { return minimum; }
126 if ( minimum
> maximum
)
129 if ( IsHandleCreated
)
130 setScrollRange ( minimum
, Maximum
);
135 public int SmallChange
{
136 get { return smallChange; }
139 throw new Exception( string.Format("Value '{0}' must be greater than or equal to 0.", value));
145 [EditorBrowsable (EditorBrowsableState
.Never
)]
146 public override string Text
{
147 get { return base.Text; }
148 set { base.Text = value; }
153 get { return value_; }
155 if ( value < Minimum
|| value > Maximum
)
156 throw new ArgumentException(
157 string.Format("'{0}' is not a valid value for 'Value'. 'Value' should be between 'Minimum' and 'Maximum'", value));
159 bool raiseEvent
= ( value_
!= value );
163 if ( IsHandleCreated
)
164 setScrollPos ( value_
);
167 OnValueChanged ( EventArgs
.Empty
);
171 public override string ToString()
173 return string.Format("{0}, Minimum: {1}, Maximum: {2}, Value: {3}",
174 GetType( ).FullName
.ToString( ), Minimum
, Maximum
, Value
);
177 public event ScrollEventHandler Scroll
;
178 public event EventHandler ValueChanged
;
180 protected override CreateParams CreateParams
{
182 CreateParams createParams
= base.CreateParams
;
183 createParams
.ClassName
= "SCROLLBAR";
184 createParams
.Style
|= (int) (WindowStyles
.WS_CHILD
);
189 protected override ImeMode DefaultImeMode
{
190 get { return ImeMode.Disable; }
194 protected override void OnEnabledChanged(EventArgs e
)
197 base.OnEnabledChanged(e
);
200 protected virtual void OnValueChanged( EventArgs e
)
202 if ( ValueChanged
!= null )
203 ValueChanged ( this, e
);
206 protected virtual void OnScroll( ScrollEventArgs se
)
209 if ( Scroll
!= null )
214 protected override void OnHandleCreated(EventArgs e
)
217 base.OnHandleCreated(e
);
218 setScrollRange ( Minimum
, Maximum
);
219 setScrollPos ( Value
);
223 protected void UpdateScrollInfo()
227 protected override void WndProc(ref Message m
) {
228 switch ((Msg
) m
.Msg
) {
231 switch ( (ScrollBarRequests
) Win32
.LOW_ORDER ( m
.WParam
.ToInt32 ( ) ) ) {
232 case ScrollBarRequests
.SB_LEFT
/*SB_TOP*/:
233 fireScroll ( ScrollEventType
.First
, Minimum
);
235 case ScrollBarRequests
.SB_RIGHT
/*SB_BOTTOM*/:
236 fireScroll ( ScrollEventType
.Last
, Minimum
);
238 case ScrollBarRequests
.SB_LINELEFT
/*SB_LINEUP*/:
239 fireScroll ( ScrollEventType
.SmallDecrement
, Value
- SmallChange
);
241 case ScrollBarRequests
.SB_LINERIGHT
/*SB_LINEDOWN*/:
242 fireScroll ( ScrollEventType
.SmallIncrement
, Value
+ SmallChange
);
244 case ScrollBarRequests
.SB_PAGELEFT
/*SB_PAGEUP*/:
245 fireScroll ( ScrollEventType
.LargeDecrement
, Value
- LargeChange
);
247 case ScrollBarRequests
.SB_PAGERIGHT
/*SB_PAGEDOWN*/:
248 fireScroll ( ScrollEventType
.LargeIncrement
, Value
+ LargeChange
);
250 case ScrollBarRequests
.SB_THUMBTRACK
:
251 fireScroll ( ScrollEventType
.ThumbTrack
, getTrackPos ( ) );
253 case ScrollBarRequests
.SB_THUMBPOSITION
:
254 fireScroll ( ScrollEventType
.ThumbPosition
, getScrollPos ( ) );
256 case ScrollBarRequests
.SB_ENDSCROLL
:
257 fireScroll ( ScrollEventType
.EndScroll
, getScrollPos ( ) );
262 CallControlWndProc( ref m
);
267 private void setScrollRange ( int minimum
, int maximum
)
269 SCROLLINFO scrinfo
= new SCROLLINFO ( );
270 scrinfo
.cbSize
= Marshal
.SizeOf ( scrinfo
);
271 scrinfo
.fMask
= (int) ScrollBarInfoFlags
.SIF_RANGE
;
272 scrinfo
.nMin
= minimum
;
273 scrinfo
.nMax
= maximum
;
274 Win32
.SetScrollInfo ( Handle
, (int) ScrollBarTypes
.SB_CTL
, ref scrinfo
, 1 );
277 private void setScrollPos ( int val
)
279 SCROLLINFO scrinfo
= new SCROLLINFO ( );
280 scrinfo
.cbSize
= Marshal
.SizeOf ( scrinfo
);
281 scrinfo
.fMask
= (int) ScrollBarInfoFlags
.SIF_POS
;
283 Win32
.SetScrollInfo ( Handle
, (int) ScrollBarTypes
.SB_CTL
, ref scrinfo
, 1 );
286 private int getScrollPos ( )
288 SCROLLINFO scrinfo
= new SCROLLINFO ( );
289 scrinfo
.cbSize
= Marshal
.SizeOf ( scrinfo
);
290 scrinfo
.fMask
= (int) ScrollBarInfoFlags
.SIF_POS
;
291 Win32
.GetScrollInfo ( Handle
, (int) ScrollBarTypes
.SB_CTL
, ref scrinfo
);
295 private int getTrackPos ( )
297 SCROLLINFO scrinfo
= new SCROLLINFO ( );
298 scrinfo
.cbSize
= Marshal
.SizeOf ( scrinfo
);
299 scrinfo
.fMask
= (int) ScrollBarInfoFlags
.SIF_TRACKPOS
;
300 Win32
.GetScrollInfo ( Handle
, (int) ScrollBarTypes
.SB_CTL
, ref scrinfo
);
301 return scrinfo
.nTrackPos
;
304 private void fireScroll ( ScrollEventType type
, int Val
)
306 OnScroll ( new ScrollEventArgs ( type
, clip ( Val
) ) );
309 private int clip ( int val
)