fixed tests
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / AccessibleObject.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2004-2006 Novell, Inc.
21 //
22 // Authors:
23 //      Peter Dennis Bartok     pbartok@novell.com
24 //
25
26
27 // NOT COMPLETE
28
29 using Accessibility;
30 using System.Drawing;
31 using System.Globalization;
32 using System.Reflection;
33 using System.Runtime.InteropServices;
34
35 namespace System.Windows.Forms {
36         [ComVisible(true)]
37         public class AccessibleObject : MarshalByRefObject, IReflect, IAccessible {
38                 #region Private Variables
39                 internal string         name;
40                 internal string         value;
41                 private Control owner;
42                 internal AccessibleRole role;
43                 internal AccessibleStates       state;
44                 internal string         default_action;
45                 internal string         description;
46                 internal string         help;
47                 internal string         keyboard_shortcut;
48                 #endregion      // Private Variables
49
50                 #region Public Constructors
51                 public AccessibleObject() {
52                         this.owner = null;
53                         this.value = null;
54                         this.name = null;
55                         this.role = AccessibleRole.Default;
56                         this.default_action = null;
57                         this.description = null;
58                         this.help = null;
59                         this.keyboard_shortcut = null;
60                         this.state = AccessibleStates.None;
61                 }
62                 #endregion      // Public Constructors
63
64                 #region Private Constructors
65                 internal AccessibleObject(Control owner) : this () {
66                         this.owner=owner;
67                 }
68                 #endregion      // Private Constructors
69
70                 #region Public Instance Properties
71                 public virtual Rectangle Bounds {
72                         get {
73                                 return owner.Bounds;
74                         }
75                 }
76
77                 public virtual string DefaultAction {
78                         get {
79                                 return default_action;
80                         }
81                 }
82
83                 public virtual string Description {
84                         get {
85                                 return description;
86                         }
87                 }
88
89                 public virtual string Help {
90                         get {
91                                 return help;
92                         }
93                 }
94
95                 public virtual string KeyboardShortcut {
96                         get {
97                                 return keyboard_shortcut;
98                         }
99                 }
100
101                 public virtual string Name {
102                         get {
103                                 return name;
104                         }
105
106                         set {
107                                 name=value;
108                         }
109                 }
110
111                 public virtual AccessibleObject Parent {
112                         get {
113                                 if ((owner!=null) && (owner.Parent!=null)) {
114                                         return owner.Parent.AccessibilityObject;
115                                 }
116                                 return null;
117                         }
118                 }
119
120                 public virtual AccessibleRole Role {
121                         get {
122                                 return role;
123                         }
124                 }
125
126                 public virtual AccessibleStates State {
127                         get {
128 #if not
129                                 if (owner!=null) {
130                                         if (owner.Focused) {
131                                                 state |= AccessibleStates.Focused;
132                                         }
133
134                                         if (!owner.Visible) {
135                                                 state |= AccessibleStates.Invisible;
136                                         }
137                                 }
138 #endif
139                                 return state;
140                         }
141                 }
142
143                 public virtual string Value {
144                         get {
145                                 return this.value;
146                         }
147
148                         set {
149                                 this.value=value;
150                         }
151                 }
152                 #endregion      // Public Instance Properties
153
154                 #region Public Instance Methods
155                 public virtual void DoDefaultAction() {
156                         if (owner!=null) {
157                                 owner.DoDefaultAction();
158                         }
159                 }
160
161                 public virtual AccessibleObject GetChild(int index) {
162                         if (owner!=null) {
163                                 if (index<owner.Controls.Count) {
164                                         return owner.Controls[index].AccessibilityObject;
165                                 }
166                         }
167                         return null;
168                 }
169
170                 public virtual int GetChildCount() {
171                         if (owner!=null) {
172                                 return owner.Controls.Count;
173                         }
174                         return -1;
175                 }
176
177                 public virtual AccessibleObject GetFocused() {
178                         if (owner.has_focus) {
179                                 return owner.AccessibilityObject;
180                         }
181
182                         return FindFocusControl(owner);
183                 }
184
185                 public virtual int GetHelpTopic(out string FileName) {
186
187                         FileName = null;
188
189                         return -1;
190                 }
191
192                 public virtual AccessibleObject GetSelected() {
193                         if ((state & AccessibleStates.Selected) != 0) {
194                                 return this;
195                         }
196
197                         return FindSelectedControl(owner);
198                 }
199
200                 public virtual AccessibleObject HitTest(int x, int y) {
201                         Control result;
202
203                         result = FindHittestControl(owner, x, y);
204
205                         if (result != null) {
206                                 return result.AccessibilityObject;
207                         }
208
209                         return null;
210                 }
211
212                 public virtual AccessibleObject Navigate(AccessibleNavigation navdir) {
213                         int     index;
214
215                         // I'm not throwing exceptions if an object doesn't exist in the specified direction
216                         // Might not be too helpful to a blind dude trying to navigate. Instead we return
217                         // our own object
218
219                         if (owner.Parent != null) {
220                                 index = owner.Parent.Controls.IndexOf(owner);
221                         } else {
222                                 index = -1;
223                         }
224
225                         switch (navdir) {
226                                 // Spatial navigation; limited to siblings
227                                 case AccessibleNavigation.Up: {
228                                         if (owner.Parent != null) {
229                                                 for (int i=0; i<owner.Parent.Controls.Count; i++) {
230                                                         if ((owner != owner.Parent.Controls[i]) && (owner.Parent.Controls[i].Top<owner.Top)) {
231                                                                 return owner.Parent.Controls[i].AccessibilityObject;
232                                                         }
233                                                 }
234                                                 
235                                         }
236                                         return owner.AccessibilityObject;
237                                 }
238
239                                 case AccessibleNavigation.Down: {
240                                         if (owner.Parent != null) {
241                                                 for (int i=0; i<owner.Parent.Controls.Count; i++) {
242                                                         if ((owner != owner.Parent.Controls[i]) && (owner.Parent.Controls[i].Top>owner.Bottom)) {
243                                                                 return owner.Parent.Controls[i].AccessibilityObject;
244                                                         }
245                                                 }
246                                                 
247                                         }
248                                         return owner.AccessibilityObject;
249                                 }
250
251                                 case AccessibleNavigation.Left: {
252                                         if (owner.Parent != null) {
253                                                 for (int i=0; i<owner.Parent.Controls.Count; i++) {
254                                                         if ((owner != owner.Parent.Controls[i]) && (owner.Parent.Controls[i].Left<owner.Left)) {
255                                                                 return owner.Parent.Controls[i].AccessibilityObject;
256                                                         }
257                                                 }
258                                                 
259                                         }
260                                         return owner.AccessibilityObject;
261                                 }
262
263                                 case AccessibleNavigation.Right: {
264                                         if (owner.Parent != null) {
265                                                 for (int i=0; i<owner.Parent.Controls.Count; i++) {
266                                                         if ((owner != owner.Parent.Controls[i]) && (owner.Parent.Controls[i].Left>owner.Right)) {
267                                                                 return owner.Parent.Controls[i].AccessibilityObject;
268                                                         }
269                                                 }
270                                                 
271                                         }
272                                         return owner.AccessibilityObject;
273                                 }
274
275                                 // Logical navigation
276                                 case AccessibleNavigation.Next: {
277                                         if (owner.Parent != null) {
278                                                 if ((index+1)<owner.Parent.Controls.Count) {
279                                                         return owner.Parent.Controls[index+1].AccessibilityObject;
280                                                 } else {
281                                                         return owner.Parent.Controls[0].AccessibilityObject;
282                                                 }
283                                         } else {
284                                                 return owner.AccessibilityObject;
285                                         }
286                                 }
287
288                                 case AccessibleNavigation.Previous: {
289                                         if (owner.Parent != null) {
290                                                 if (index>0) {
291                                                         return owner.Parent.Controls[index-1].AccessibilityObject;
292                                                 } else {
293                                                         return owner.Parent.Controls[owner.Parent.Controls.Count-1].AccessibilityObject;
294                                                 }
295                                         } else {
296                                                 return owner.AccessibilityObject;
297                                         }
298                                 }
299
300                                 case AccessibleNavigation.FirstChild: {
301                                         if (owner.Controls.Count>0) {
302                                                 return owner.Controls[0].AccessibilityObject;
303                                         } else {
304                                                 return owner.AccessibilityObject;
305                                         }
306                                 }
307
308                                 case AccessibleNavigation.LastChild: {
309                                         if (owner.Controls.Count>0) {
310                                                 return owner.Controls[owner.Controls.Count-1].AccessibilityObject;
311                                         } else {
312                                                 return owner.AccessibilityObject;
313                                         }
314                                 }
315                         }
316
317                         return owner.AccessibilityObject;
318                 }
319
320                 public virtual void Select(AccessibleSelection flags) {
321                         if ((flags & AccessibleSelection.TakeFocus) != 0){
322                                 owner.Focus();
323                         }
324                         return;
325                 }
326                 #endregion      // Public Instance Methods
327
328                 #region Protected Instance Methods
329                 protected void UseStdAccessibleObjects(IntPtr handle) {
330                 }
331
332                 protected void UseStdAccessibleObjects(IntPtr handle, int objid) {
333                         UseStdAccessibleObjects(handle, 0);
334                 }
335                 #endregion      // Protected Instance Methods
336
337
338                 #region Internal Methods
339                 internal static AccessibleObject FindFocusControl(Control parent) {
340                         Control child;
341
342                         if (parent != null) {
343                                 for (int i=0; i < parent.Controls.Count; i++) {
344                                         child = parent.Controls[i];
345                                         if ((child.AccessibilityObject.state & AccessibleStates.Focused) != 0) {
346                                                 return child.AccessibilityObject;
347                                         }
348
349                                         if (child.Controls.Count>0) {
350                                                 AccessibleObject result;
351
352                                                 result = FindFocusControl(child);
353                                                 if (result != null) {
354                                                         return result;
355                                                 }
356                                         }
357                                 }
358                         }
359                         return null;
360                 }
361
362                 internal static AccessibleObject FindSelectedControl(Control parent) {
363                         Control child;
364
365                         if (parent != null) {
366                                 for (int i=0; i < parent.Controls.Count; i++) {
367                                         child = parent.Controls[i];
368                                         if ((child.AccessibilityObject.state & AccessibleStates.Selected) != 0) {
369                                                 return child.AccessibilityObject;
370                                         }
371                                         if (child.Controls.Count>0) {
372                                                 AccessibleObject result;
373
374                                                 result = FindSelectedControl(child);
375                                                 if (result != null) {
376                                                         return result;
377                                                 }
378                                         }
379                                 }
380                         }
381                         return null;
382                 }
383
384                 internal static Control FindHittestControl(Control parent, int x, int y) {
385                         Control child;
386                         Point   child_point;
387                         Point   hittest_point;
388
389                         hittest_point = new Point(x, y);
390
391                         child_point = parent.PointToClient(hittest_point);
392                         if (parent.ClientRectangle.Contains(child_point)) {
393                                 return parent;
394                         }
395
396                         for (int i=0; i < parent.Controls.Count; i++) {
397                                 child=parent.Controls[i];
398                                 child_point = child.PointToClient(hittest_point);
399                                 if (child.ClientRectangle.Contains(child_point)) {
400                                         return child;
401                                 }
402                                 if (child.Controls.Count>0) {
403                                         Control result;
404
405                                         result = FindHittestControl(child, x, y);
406                                         if (result != null) {
407                                                 return result;
408                                         }
409                                 }
410                         }
411                         return null;
412                 }
413                 #endregion      // Internal Methods
414
415                 #region IReflection Methods and Properties
416                 FieldInfo IReflect.GetField(String name, BindingFlags bindingAttr) {
417                         throw new NotImplementedException();
418                 }       
419
420                 FieldInfo[] IReflect.GetFields(BindingFlags bindingAttr) {
421                         throw new NotImplementedException();
422                 }    
423
424                 MemberInfo[] IReflect.GetMember(String name, BindingFlags bindingAttr) {
425                         throw new NotImplementedException();
426                 }
427
428                 MemberInfo[] IReflect.GetMembers(BindingFlags bindingAttr) {
429                         throw new NotImplementedException();
430                 }
431
432                 MethodInfo IReflect.GetMethod(String name, BindingFlags bindingAttr) {
433                         throw new NotImplementedException();
434                 }
435
436                 MethodInfo IReflect.GetMethod(String name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers) {
437                         throw new NotImplementedException();
438                 }
439
440                 MethodInfo[] IReflect.GetMethods(BindingFlags bindingAttr) {
441                         throw new NotImplementedException();
442                 }
443
444                 PropertyInfo IReflect.GetProperty(String name, BindingFlags bindingAttr) {
445                         throw new NotImplementedException();
446                 }
447
448                 PropertyInfo IReflect.GetProperty(String name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) {
449                         throw new NotImplementedException();
450                 }
451
452                 PropertyInfo[] IReflect.GetProperties(BindingFlags bindingAttr) {
453                         throw new NotImplementedException();
454                 }
455
456                 Object IReflect.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParameters) {
457                         throw new NotImplementedException();
458                 }
459
460                 Type IReflect.UnderlyingSystemType {
461                         get {
462                                 throw new NotImplementedException();
463                         }
464                 }
465                 #endregion      // IReflection Methods and Properties
466
467                 #region IAccessible Methods and Properties
468                 void IAccessible.accDoDefaultAction(object childID) {
469                         throw new NotImplementedException();
470                 }
471
472                 int IAccessible.accChildCount {
473                         get {
474                                 throw new NotImplementedException();
475                         }
476                 }
477
478                 object IAccessible.accFocus {
479                         get {
480                                 throw new NotImplementedException();
481                         }
482                 }
483
484                 object IAccessible.accHitTest(int xLeft, int yTop) {
485                         throw new NotImplementedException();
486                 }
487
488                 void IAccessible.accLocation(out int pxLeft, out int pyTop, out int pcxWidth, out int pcyHeight, object childID) {
489                         throw new NotImplementedException();
490                 }
491
492                 object IAccessible.accNavigate(int navDir, object childID) {
493                         throw new NotImplementedException();
494                 }
495
496                 object IAccessible.accParent {
497                         get {
498                                 throw new NotImplementedException();
499                         }
500                 }
501
502                 void IAccessible.accSelect(int flagsSelect, object childID) {
503                         throw new NotImplementedException();
504                 }
505
506                 object IAccessible.accSelection {
507                         get {
508                                 throw new NotImplementedException();
509                         }
510                 }
511
512                 object IAccessible.get_accChild(object childID) {
513                         throw new NotImplementedException();
514                 }
515
516                 string IAccessible.get_accDefaultAction(object childID) {
517                         throw new NotImplementedException();
518                 }
519
520                 string IAccessible.get_accDescription(object childID) {
521                         throw new NotImplementedException();
522                 }
523
524                 string IAccessible.get_accHelp(object childID) {
525                         throw new NotImplementedException();
526                 }
527
528                 int IAccessible.get_accHelpTopic(out string pszHelpFile,object childID) {
529                         throw new NotImplementedException();
530                 }
531
532                 string IAccessible.get_accKeyboardShortcut(object childID) {
533                         throw new NotImplementedException();
534                 }
535
536                 string IAccessible.get_accName(object childID) {
537                         throw new NotImplementedException();
538                 }
539
540                 object IAccessible.get_accRole(object childID) {
541                         throw new NotImplementedException();
542                 }
543
544                 object IAccessible.get_accState(object childID) {
545                         throw new NotImplementedException();
546                 }
547
548                 string IAccessible.get_accValue(object childID) {
549                         throw new NotImplementedException();
550                 }
551
552                 void IAccessible.set_accName(object childID, string newName) {
553                         throw new NotImplementedException();
554                 }
555
556                 void IAccessible.set_accValue(object childID, string newValue) {
557                         throw new NotImplementedException();
558                 }
559                 #endregion      // IAccessible Methods and Properties
560         }
561 }