2010-05-06 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[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 #if NET_2_0
38         public class AccessibleObject : StandardOleMarshalObject, IReflect, IAccessible {
39 #else
40         public class AccessibleObject : MarshalByRefObject, IReflect, IAccessible {
41 #endif
42                 #region Private Variables
43                 internal string         name;
44                 internal string         value;
45                 internal Control owner;
46                 internal AccessibleRole role;
47                 internal AccessibleStates       state;
48                 internal string         default_action;
49                 internal string         description;
50                 internal string         help;
51                 internal string         keyboard_shortcut;
52                 #endregion      // Private Variables
53
54                 #region Public Constructors
55                 public AccessibleObject() {
56                         this.owner = null;
57                         this.value = null;
58                         this.name = null;
59                         this.role = AccessibleRole.Default;
60                         this.default_action = null;
61                         this.description = null;
62                         this.help = null;
63                         this.keyboard_shortcut = null;
64                         this.state = AccessibleStates.None;
65                 }
66                 #endregion      // Public Constructors
67
68                 #region Private Constructors
69                 internal AccessibleObject(Control owner) : this () {
70                         this.owner=owner;
71                 }
72                 #endregion      // Private Constructors
73
74                 #region Public Instance Properties
75                 public virtual Rectangle Bounds {
76                         get {
77                                 return owner.Bounds;
78                         }
79                 }
80
81                 public virtual string DefaultAction {
82                         get {
83                                 return default_action;
84                         }
85                 }
86
87                 public virtual string Description {
88                         get {
89                                 return description;
90                         }
91                 }
92
93                 public virtual string Help {
94                         get {
95                                 return help;
96                         }
97                 }
98
99                 public virtual string KeyboardShortcut {
100                         get {
101                                 return keyboard_shortcut;
102                         }
103                 }
104
105                 public virtual string Name {
106                         get {
107                                 return name;
108                         }
109
110                         set {
111                                 name=value;
112                         }
113                 }
114
115                 public virtual AccessibleObject Parent {
116                         get {
117                                 if ((owner!=null) && (owner.Parent!=null)) {
118                                         return owner.Parent.AccessibilityObject;
119                                 }
120                                 return null;
121                         }
122                 }
123
124                 public virtual AccessibleRole Role {
125                         get {
126                                 return role;
127                         }
128                 }
129
130                 public virtual AccessibleStates State {
131                         get {
132 #if not
133                                 if (owner!=null) {
134                                         if (owner.Focused) {
135                                                 state |= AccessibleStates.Focused;
136                                         }
137
138                                         if (!owner.Visible) {
139                                                 state |= AccessibleStates.Invisible;
140                                         }
141                                 }
142 #endif
143                                 return state;
144                         }
145                 }
146
147                 public virtual string Value {
148                         get {
149                                 return this.value;
150                         }
151
152                         set {
153                                 this.value=value;
154                         }
155                 }
156                 #endregion      // Public Instance Properties
157
158                 #region Public Instance Methods
159                 public virtual void DoDefaultAction() {
160                         if (owner!=null) {
161                                 owner.DoDefaultAction();
162                         }
163                 }
164
165                 public virtual AccessibleObject GetChild(int index) {
166                         if (owner!=null) {
167                                 if (index<owner.Controls.Count) {
168                                         return owner.Controls[index].AccessibilityObject;
169                                 }
170                         }
171                         return null;
172                 }
173
174                 public virtual int GetChildCount() {
175                         if (owner!=null) {
176                                 return owner.Controls.Count;
177                         }
178                         return -1;
179                 }
180
181                 public virtual AccessibleObject GetFocused() {
182                         if (owner.has_focus) {
183                                 return owner.AccessibilityObject;
184                         }
185
186                         return FindFocusControl(owner);
187                 }
188
189                 public virtual int GetHelpTopic (out string fileName)
190                 {
191                         fileName = null;
192                         return -1;
193                 }
194
195                 public virtual AccessibleObject GetSelected() {
196                         if ((state & AccessibleStates.Selected) != 0) {
197                                 return this;
198                         }
199
200                         return FindSelectedControl(owner);
201                 }
202
203                 public virtual AccessibleObject HitTest(int x, int y) {
204                         Control result;
205
206                         result = FindHittestControl(owner, x, y);
207
208                         if (result != null) {
209                                 return result.AccessibilityObject;
210                         }
211
212                         return null;
213                 }
214
215                 public virtual AccessibleObject Navigate(AccessibleNavigation navdir) {
216                         int     index;
217
218                         // I'm not throwing exceptions if an object doesn't exist in the specified direction
219                         // Might not be too helpful to a blind dude trying to navigate. Instead we return
220                         // our own object
221
222                         if (owner.Parent != null) {
223                                 index = owner.Parent.Controls.IndexOf(owner);
224                         } else {
225                                 index = -1;
226                         }
227
228                         switch (navdir) {
229                                 // Spatial navigation; limited to siblings
230                                 case AccessibleNavigation.Up: {
231                                         if (owner.Parent != null) {
232                                                 for (int i=0; i<owner.Parent.Controls.Count; i++) {
233                                                         if ((owner != owner.Parent.Controls[i]) && (owner.Parent.Controls[i].Top<owner.Top)) {
234                                                                 return owner.Parent.Controls[i].AccessibilityObject;
235                                                         }
236                                                 }
237                                                 
238                                         }
239                                         return owner.AccessibilityObject;
240                                 }
241
242                                 case AccessibleNavigation.Down: {
243                                         if (owner.Parent != null) {
244                                                 for (int i=0; i<owner.Parent.Controls.Count; i++) {
245                                                         if ((owner != owner.Parent.Controls[i]) && (owner.Parent.Controls[i].Top>owner.Bottom)) {
246                                                                 return owner.Parent.Controls[i].AccessibilityObject;
247                                                         }
248                                                 }
249                                                 
250                                         }
251                                         return owner.AccessibilityObject;
252                                 }
253
254                                 case AccessibleNavigation.Left: {
255                                         if (owner.Parent != null) {
256                                                 for (int i=0; i<owner.Parent.Controls.Count; i++) {
257                                                         if ((owner != owner.Parent.Controls[i]) && (owner.Parent.Controls[i].Left<owner.Left)) {
258                                                                 return owner.Parent.Controls[i].AccessibilityObject;
259                                                         }
260                                                 }
261                                                 
262                                         }
263                                         return owner.AccessibilityObject;
264                                 }
265
266                                 case AccessibleNavigation.Right: {
267                                         if (owner.Parent != null) {
268                                                 for (int i=0; i<owner.Parent.Controls.Count; i++) {
269                                                         if ((owner != owner.Parent.Controls[i]) && (owner.Parent.Controls[i].Left>owner.Right)) {
270                                                                 return owner.Parent.Controls[i].AccessibilityObject;
271                                                         }
272                                                 }
273                                                 
274                                         }
275                                         return owner.AccessibilityObject;
276                                 }
277
278                                 // Logical navigation
279                                 case AccessibleNavigation.Next: {
280                                         if (owner.Parent != null) {
281                                                 if ((index+1)<owner.Parent.Controls.Count) {
282                                                         return owner.Parent.Controls[index+1].AccessibilityObject;
283                                                 } else {
284                                                         return owner.Parent.Controls[0].AccessibilityObject;
285                                                 }
286                                         } else {
287                                                 return owner.AccessibilityObject;
288                                         }
289                                 }
290
291                                 case AccessibleNavigation.Previous: {
292                                         if (owner.Parent != null) {
293                                                 if (index>0) {
294                                                         return owner.Parent.Controls[index-1].AccessibilityObject;
295                                                 } else {
296                                                         return owner.Parent.Controls[owner.Parent.Controls.Count-1].AccessibilityObject;
297                                                 }
298                                         } else {
299                                                 return owner.AccessibilityObject;
300                                         }
301                                 }
302
303                                 case AccessibleNavigation.FirstChild: {
304                                         if (owner.Controls.Count>0) {
305                                                 return owner.Controls[0].AccessibilityObject;
306                                         } else {
307                                                 return owner.AccessibilityObject;
308                                         }
309                                 }
310
311                                 case AccessibleNavigation.LastChild: {
312                                         if (owner.Controls.Count>0) {
313                                                 return owner.Controls[owner.Controls.Count-1].AccessibilityObject;
314                                         } else {
315                                                 return owner.AccessibilityObject;
316                                         }
317                                 }
318                         }
319
320                         return owner.AccessibilityObject;
321                 }
322
323                 public virtual void Select(AccessibleSelection flags) {
324                         if ((flags & AccessibleSelection.TakeFocus) != 0){
325                                 owner.Focus();
326                         }
327                         return;
328                 }
329                 #endregion      // Public Instance Methods
330
331                 #region Protected Instance Methods
332                 protected void UseStdAccessibleObjects(IntPtr handle) {
333                 }
334
335                 protected void UseStdAccessibleObjects(IntPtr handle, int objid) {
336                         UseStdAccessibleObjects(handle, 0);
337                 }
338                 #endregion      // Protected Instance Methods
339
340
341                 #region Internal Methods
342                 internal static AccessibleObject FindFocusControl(Control parent) {
343                         Control child;
344
345                         if (parent != null) {
346                                 for (int i=0; i < parent.Controls.Count; i++) {
347                                         child = parent.Controls[i];
348                                         if ((child.AccessibilityObject.state & AccessibleStates.Focused) != 0) {
349                                                 return child.AccessibilityObject;
350                                         }
351
352                                         if (child.Controls.Count>0) {
353                                                 AccessibleObject result;
354
355                                                 result = FindFocusControl(child);
356                                                 if (result != null) {
357                                                         return result;
358                                                 }
359                                         }
360                                 }
361                         }
362                         return null;
363                 }
364
365                 internal static AccessibleObject FindSelectedControl(Control parent) {
366                         Control child;
367
368                         if (parent != null) {
369                                 for (int i=0; i < parent.Controls.Count; i++) {
370                                         child = parent.Controls[i];
371                                         if ((child.AccessibilityObject.state & AccessibleStates.Selected) != 0) {
372                                                 return child.AccessibilityObject;
373                                         }
374                                         if (child.Controls.Count>0) {
375                                                 AccessibleObject result;
376
377                                                 result = FindSelectedControl(child);
378                                                 if (result != null) {
379                                                         return result;
380                                                 }
381                                         }
382                                 }
383                         }
384                         return null;
385                 }
386
387                 internal static Control FindHittestControl(Control parent, int x, int y) {
388                         Control child;
389                         Point   child_point;
390                         Point   hittest_point;
391
392                         hittest_point = new Point(x, y);
393
394                         child_point = parent.PointToClient(hittest_point);
395                         if (parent.ClientRectangle.Contains(child_point)) {
396                                 return parent;
397                         }
398
399                         for (int i=0; i < parent.Controls.Count; i++) {
400                                 child=parent.Controls[i];
401                                 child_point = child.PointToClient(hittest_point);
402                                 if (child.ClientRectangle.Contains(child_point)) {
403                                         return child;
404                                 }
405                                 if (child.Controls.Count>0) {
406                                         Control result;
407
408                                         result = FindHittestControl(child, x, y);
409                                         if (result != null) {
410                                                 return result;
411                                         }
412                                 }
413                         }
414                         return null;
415                 }
416                 #endregion      // Internal Methods
417
418                 #region IReflection Methods and Properties
419                 FieldInfo IReflect.GetField(String name, BindingFlags bindingAttr) {
420                         throw new NotImplementedException();
421                 }       
422
423                 FieldInfo[] IReflect.GetFields(BindingFlags bindingAttr) {
424                         throw new NotImplementedException();
425                 }    
426
427                 MemberInfo[] IReflect.GetMember(String name, BindingFlags bindingAttr) {
428                         throw new NotImplementedException();
429                 }
430
431                 MemberInfo[] IReflect.GetMembers(BindingFlags bindingAttr) {
432                         throw new NotImplementedException();
433                 }
434
435                 MethodInfo IReflect.GetMethod(String name, BindingFlags bindingAttr) {
436                         throw new NotImplementedException();
437                 }
438
439                 MethodInfo IReflect.GetMethod(String name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers) {
440                         throw new NotImplementedException();
441                 }
442
443                 MethodInfo[] IReflect.GetMethods(BindingFlags bindingAttr) {
444                         throw new NotImplementedException();
445                 }
446
447                 PropertyInfo IReflect.GetProperty(String name, BindingFlags bindingAttr) {
448                         throw new NotImplementedException();
449                 }
450
451                 PropertyInfo IReflect.GetProperty(String name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) {
452                         throw new NotImplementedException();
453                 }
454
455                 PropertyInfo[] IReflect.GetProperties(BindingFlags bindingAttr) {
456                         throw new NotImplementedException();
457                 }
458
459                 Object IReflect.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParameters) {
460                         throw new NotImplementedException();
461                 }
462
463                 Type IReflect.UnderlyingSystemType {
464                         get {
465                                 throw new NotImplementedException();
466                         }
467                 }
468                 #endregion      // IReflection Methods and Properties
469
470                 #region IAccessible Methods and Properties
471                 void IAccessible.accDoDefaultAction(object childID) {
472                         throw new NotImplementedException();
473                 }
474
475                 int IAccessible.accChildCount {
476                         get {
477                                 throw new NotImplementedException();
478                         }
479                 }
480
481                 object IAccessible.accFocus {
482                         get {
483                                 throw new NotImplementedException();
484                         }
485                 }
486
487                 object IAccessible.accHitTest(int xLeft, int yTop) {
488                         throw new NotImplementedException();
489                 }
490
491                 void IAccessible.accLocation(out int pxLeft, out int pyTop, out int pcxWidth, out int pcyHeight, object childID) {
492                         throw new NotImplementedException();
493                 }
494
495                 object IAccessible.accNavigate(int navDir, object childID) {
496                         throw new NotImplementedException();
497                 }
498
499                 object IAccessible.accParent {
500                         get {
501                                 throw new NotImplementedException();
502                         }
503                 }
504
505                 void IAccessible.accSelect(int flagsSelect, object childID) {
506                         throw new NotImplementedException();
507                 }
508
509                 object IAccessible.accSelection {
510                         get {
511                                 throw new NotImplementedException();
512                         }
513                 }
514
515                 object IAccessible.get_accChild(object childID) {
516                         throw new NotImplementedException();
517                 }
518
519                 string IAccessible.get_accDefaultAction(object childID) {
520                         throw new NotImplementedException();
521                 }
522
523                 string IAccessible.get_accDescription(object childID) {
524                         throw new NotImplementedException();
525                 }
526
527                 string IAccessible.get_accHelp(object childID) {
528                         throw new NotImplementedException();
529                 }
530
531                 int IAccessible.get_accHelpTopic(out string pszHelpFile,object childID) {
532                         throw new NotImplementedException();
533                 }
534
535                 string IAccessible.get_accKeyboardShortcut(object childID) {
536                         throw new NotImplementedException();
537                 }
538
539                 string IAccessible.get_accName(object childID) {
540                         throw new NotImplementedException();
541                 }
542
543                 object IAccessible.get_accRole(object childID) {
544                         throw new NotImplementedException();
545                 }
546
547                 object IAccessible.get_accState(object childID) {
548                         throw new NotImplementedException();
549                 }
550
551                 string IAccessible.get_accValue(object childID) {
552                         throw new NotImplementedException();
553                 }
554
555                 void IAccessible.set_accName(object childID, string newName) {
556                         throw new NotImplementedException();
557                 }
558
559                 void IAccessible.set_accValue(object childID, string newValue) {
560                         throw new NotImplementedException();
561                 }
562                 #endregion      // IAccessible Methods and Properties
563         }
564 }