File: ScrollProviderBehavior.cs

package info (click to toggle)
mono-uia-winforms 2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,408 kB
  • sloc: cs: 54,358; makefile: 810; sh: 669
file content (265 lines) | stat: -rw-r--r-- 8,515 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Permission is hereby granted, free of charge, to any person obtaining 
// a copy of this software and associated documentation files (the 
// "Software"), to deal in the Software without restriction, including 
// without limitation the rights to use, copy, modify, merge, publish, 
// distribute, sublicense, and/or sell copies of the Software, and to 
// permit persons to whom the Software is furnished to do so, subject to 
// the following conditions: 
//  
// The above copyright notice and this permission notice shall be 
// included in all copies or substantial portions of the Software. 
//  
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
// 
// Copyright (c) 2008,2009 Novell, Inc. (http://www.novell.com) 
// 
// Authors: 
//	Mario Carrion <mcarrion@novell.com>
// 
using System;
using SD = System.Drawing;
using System.Windows.Automation;
using System.Windows.Automation.Provider;
using SWF = System.Windows.Forms;
using System.Windows;
using System.Reflection;
using Mono.UIAutomation.Winforms;
using Mono.UIAutomation.Winforms.Events;

namespace Mono.UIAutomation.Winforms.Behaviors.Generic
{

	internal abstract class ScrollProviderBehavior<T> 
		: ProviderBehavior, IScrollProvider 
			where T : FragmentControlProvider, IScrollBehaviorSubject
	{
		
		#region Constructors

		protected ScrollProviderBehavior (T subject) : base (subject)
		{
			genericProvider = subject;
			hscrollbar = subject.ScrollBehaviorObserver.HorizontalScrollBar;
			vscrollbar = subject.ScrollBehaviorObserver.VerticalScrollBar;
		}

		#endregion
		
		#region IProviderBehavior Interface

		public override AutomationPattern ProviderPattern { 
			get { return ScrollPatternIdentifiers.Pattern; }
		}
		
		public override void Disconnect ()
		{		
			Provider.SetEvent (ProviderEventType.ScrollPatternHorizontallyScrollableProperty,
			                   null);
			Provider.SetEvent (ProviderEventType.ScrollPatternHorizontalScrollPercentProperty,
			                   null);
			Provider.SetEvent (ProviderEventType.ScrollPatternHorizontalViewSizeProperty,
			                   null);
			Provider.SetEvent (ProviderEventType.ScrollPatternVerticallyScrollableProperty,
			                   null);
			Provider.SetEvent (ProviderEventType.ScrollPatternVerticalScrollPercentProperty,
			                   null);
			Provider.SetEvent (ProviderEventType.ScrollPatternVerticalViewSizeProperty,
			                   null);
		}

		public override object GetPropertyValue (int propertyId)
		{
			if (propertyId == ScrollPatternIdentifiers.HorizontallyScrollableProperty.Id)
				return HorizontallyScrollable;
			else if (propertyId == ScrollPatternIdentifiers.HorizontalScrollPercentProperty.Id)
				return HorizontalScrollPercent;
			else if (propertyId == ScrollPatternIdentifiers.HorizontalViewSizeProperty.Id)
				return HorizontalViewSize;
			else if (propertyId == ScrollPatternIdentifiers.VerticallyScrollableProperty.Id)
				return VerticallyScrollable;
			else if (propertyId == ScrollPatternIdentifiers.VerticalScrollPercentProperty.Id)
				return VerticalScrollPercent;
			else if (propertyId == ScrollPatternIdentifiers.VerticalViewSizeProperty.Id)
				return VerticalViewSize;
			else
				return base.GetPropertyValue (propertyId);
		}
		
		#endregion
		
		#region IScrollProvider Interface
			
		public bool HorizontallyScrollable {
			get { return genericProvider.ScrollBehaviorObserver.HasHorizontalScrollbar; }
		}
		
		//FIXME: This MUST BE locale-specific
		public double HorizontalScrollPercent {
			get { 
				if (HorizontallyScrollable == false || hscrollbar.Maximum == 0)
					return ScrollPatternIdentifiers.NoScroll;
				else
					return (hscrollbar.Value * 100) / hscrollbar.Maximum; 
			}
		}
		
		public double HorizontalViewSize {
			get { 
				if (HorizontallyScrollable == false)
					return 100;
				else {
					SD.Rectangle thumbArea = hscrollbar.UIAThumbPosition;
					return ((thumbArea.Width + (thumbArea.Height * 2)) * 100)
						/ Provider.Control.Width;
				}
			}
		}

		public bool VerticallyScrollable {
			get { return genericProvider.ScrollBehaviorObserver.HasVerticalScrollbar; }
		}

		//FIXME: This MUST BE locale-specific
		public double VerticalScrollPercent {
			get { 
				if (VerticallyScrollable == false || vscrollbar.Maximum == 0)
					return ScrollPatternIdentifiers.NoScroll;
				else
					return (vscrollbar.Value * 100) / vscrollbar.Maximum; 
			}
		}

		public double VerticalViewSize {
			get { 
				if (VerticallyScrollable == false)
					return 100;
				else {
					SD.Rectangle thumbArea = vscrollbar.UIAThumbPosition;
					return ((thumbArea.Height+ (thumbArea.Width * 2)) * 100)
						/ Provider.Control.Height;
				}
			}
		}
		
		public void Scroll (ScrollAmount horizontalAmount, 
		                    ScrollAmount verticalAmount)
		{
			if (horizontalAmount != ScrollAmount.NoAmount
			    && HorizontallyScrollable)
				ScrollByAmount (hscrollbar, GetAmountString (horizontalAmount));
			if (verticalAmount != ScrollAmount.NoAmount
			    && VerticallyScrollable)
				ScrollByAmount (vscrollbar, GetAmountString (verticalAmount));
		}

		public void SetScrollPercent (double horizontalPercent, 
		                              double verticalPercent)
		{
			if (horizontalPercent != ScrollPatternIdentifiers.NoScroll) {
				if (horizontalPercent < 0 || horizontalPercent > 100)
					throw new ArgumentOutOfRangeException ();
				else
					PerformScrollByPercent (hscrollbar, (int) ((horizontalPercent * hscrollbar.Maximum) / 100));
			}
			
			if (verticalPercent != ScrollPatternIdentifiers.NoScroll) {
			    if (verticalPercent < 0 || verticalPercent > 100)
					throw new ArgumentOutOfRangeException ();
				else 
					PerformScrollByPercent (vscrollbar, (int) ((verticalPercent * vscrollbar.Maximum) / 100));
			}
		}

		#endregion

		#region Protected Properties

		protected T GenericProvider {
			get { return genericProvider; }
		}

		#endregion
		
		#region Private Methods

		private string GetAmountString (ScrollAmount amount) 
		{
			string str = string.Empty;

			switch (amount) {
			case ScrollAmount.LargeDecrement:
				str = "UIALargeDecrement";
				break;
			case ScrollAmount.LargeIncrement:
				str = "UIALargeIncrement";
				break;
			case ScrollAmount.SmallDecrement:
				str = "UIASmallDecrement";
				break;
			case ScrollAmount.SmallIncrement:
				str = "UIASmallIncrement";
				break;
			}

			return str;
		}
		
		private void ScrollByAmount (SWF.ScrollBar scrollbar, string method) 
		{
			if (method == string.Empty)
				return;
			
			MethodInfo methodInfo = typeof (SWF.ScrollBar).GetMethod (method,
			                                                          BindingFlags.InvokeMethod
			                                                          | BindingFlags.NonPublic
			                                                          | BindingFlags.Instance);
			invoke
				= (Action<SWF.ScrollBar>) Delegate.CreateDelegate (typeof (Action<SWF.ScrollBar>), 
				                                                   methodInfo);
			PerformScrollByAmount (scrollbar);
		}
		
		private void PerformScrollByAmount (SWF.ScrollBar scrollbar)
		{
			if (scrollbar.InvokeRequired == true) {
				scrollbar.BeginInvoke (new ScrollByAmountDelegate (PerformScrollByAmount),
				                       new object [] { scrollbar });
				return;
			}
			invoke (scrollbar);
			invoke = null;
		}
		
		private void PerformScrollByPercent (SWF.ScrollBar scrollbar, int value)
		{
			if (scrollbar.InvokeRequired == true) {
				scrollbar.BeginInvoke (new ScrollByPercentDelegate (PerformScrollByPercent),
				                       new object [] { scrollbar, value });
				return;
			}
			scrollbar.Value = value;
		}

		#endregion
		
		#region Private Fields
		
		private SWF.ScrollBar hscrollbar;
		private SWF.ScrollBar vscrollbar;
		private Action<SWF.ScrollBar> invoke;
		private T genericProvider;
		
		#endregion

	}
	
	delegate void ScrollByAmountDelegate (SWF.ScrollBar scrollbar);
	delegate void ScrollByPercentDelegate (SWF.ScrollBar scrollbar, int value);
				                      
}