declare function arguments as (void) rather than ()
[platform/upstream/atk.git] / atk / atkcomponent.c
1 /* ATK -  Accessibility Toolkit
2  * Copyright 2001 Sun Microsystems Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20
21 #include "atkcomponent.h"
22
23 static gboolean   atk_component_real_contains                (AtkComponent *component,
24                                                               gint         x,
25                                                               gint         y,
26                                                               AtkCoordType coord_type);
27
28 static AtkObject* atk_component_real_ref_accessible_at_point (AtkComponent *component,
29                                                               gint         x,
30                                                               gint         y,
31                                                               AtkCoordType coord_type);
32
33 static void      atk_component_real_get_position             (AtkComponent *component,
34                                                               gint         *x,
35                                                               gint         *y,
36                                                               AtkCoordType coord_type);
37
38 static void      atk_component_real_get_size                 (AtkComponent *component,
39                                                               gint         *width,
40                                                               gint         *height);
41
42 GType
43 atk_component_get_type (void)
44 {
45   static GType type = 0;
46
47   if (!type) {
48     static const GTypeInfo tinfo =
49     {
50       sizeof (AtkComponentIface),
51       (GBaseInitFunc) NULL,
52       (GBaseFinalizeFunc) NULL,
53
54     };
55
56     type = g_type_register_static (G_TYPE_INTERFACE, "AtkComponent", &tinfo, 0);
57   }
58
59   return type;
60 }
61
62 /**
63  * atk_component_add_focus_handler:
64  * @component: The #AtkComponent to attach the @handler to
65  * @handler: The #AtkFocusHandler to be attached to @component
66  *
67  * Add the specified handler to the set of functions to be called 
68  * when this object receives focus events (in or out). If the handler is
69  * already added it is not added again
70  *
71  * Returns: a handler id which can be used in atk_component_remove_focus_handler
72  * or zero if the handler was already added.
73  **/
74 guint
75 atk_component_add_focus_handler (AtkComponent    *component,
76                                  AtkFocusHandler handler)
77 {
78   AtkComponentIface *iface = NULL;
79   g_return_val_if_fail (ATK_IS_COMPONENT (component), 0);
80
81   iface = ATK_COMPONENT_GET_IFACE (component);
82
83   if (iface->add_focus_handler)
84     return (iface->add_focus_handler) (component, handler);
85   else
86     return 0;
87 }
88
89 /**
90  * atk_component_remove_focus_handler:
91  * @component: the #AtkComponent to remove the focus handler from
92  * @handler_id: the handler id of the focus handler to be removed
93  * from @component
94  *
95  * Remove the handler specified by @handler_id from the list of
96  * functions to be executed when this object receives focus events 
97  * (in or out).
98  **/
99 void
100 atk_component_remove_focus_handler (AtkComponent    *component,
101                                     guint           handler_id)
102 {
103   AtkComponentIface *iface = NULL;
104   g_return_if_fail (ATK_IS_COMPONENT (component));
105
106   iface = ATK_COMPONENT_GET_IFACE (component);
107
108   if (iface->remove_focus_handler)
109     (iface->remove_focus_handler) (component, handler_id);
110 }
111
112 /**
113  * atk_component_contains:
114  * @component: the #AtkComponent
115  * @x: x coordinate
116  * @y: y coordinate
117  * @coord_type: specifies whether the coordinates are relative to the screen
118  * or to the components top level window
119  *
120  * Checks whether the specified point is within the extent of the @component.
121  *
122  * Returns: %TRUE or %FALSE indicating whether the specified point is within
123  * the extent of the @component or not
124  **/
125 gboolean
126 atk_component_contains (AtkComponent    *component,
127                         gint            x,
128                         gint            y,
129                         AtkCoordType    coord_type)
130 {
131   AtkComponentIface *iface = NULL;
132   g_return_val_if_fail (ATK_IS_COMPONENT (component), FALSE);
133
134   iface = ATK_COMPONENT_GET_IFACE (component);
135
136   if (iface->contains)
137     return (iface->contains) (component, x, y, coord_type);
138   else
139   {
140     /*
141      * if this method is not overridden use the default implementation.
142      */
143     return atk_component_real_contains (component, x, y, coord_type);
144   }
145 }
146
147 /**
148  * atk_component_ref_accessible_at_point:
149  * @component: the #AtkComponent
150  * @x: x coordinate
151  * @y: y coordinate
152  * @coord_type: specifies whether the coordinates are relative to the screen
153  * or to the components top level window
154  *
155  * Gets a reference to the accessible child, if one exists, at the
156  * coordinate point specified by @x and @y.
157  *
158  * Returns: a reference to the accessible child, if one exists
159  **/
160 AtkObject*
161 atk_component_ref_accessible_at_point (AtkComponent    *component,
162                                        gint            x,
163                                        gint            y,
164                                        AtkCoordType    coord_type)
165 {
166   AtkComponentIface *iface = NULL;
167   g_return_val_if_fail (ATK_IS_COMPONENT (component), NULL);
168
169   iface = ATK_COMPONENT_GET_IFACE (component);
170
171   if (iface->ref_accessible_at_point)
172     return (iface->ref_accessible_at_point) (component, x, y, coord_type);
173   else
174   {
175     /*
176      * if this method is not overridden use the default implementation.
177      */
178     return atk_component_real_ref_accessible_at_point (component, x, y, coord_type);
179   }
180 }
181
182 /**
183  * atk_component_get_extents:
184  * @component: an #AtkComponent
185  * @x: address of #gint to put x coordinate
186  * @y: address of #gint to put y coordinate
187  * @width: address of #gint to put width
188  * @height: address of #gint to put height
189  * @coord_type: specifies whether the coordinates are relative to the screen
190  * or to the components top level window
191  *
192  * Gets the rectangle which gives the extent of the @component.
193  *
194  **/
195 void
196 atk_component_get_extents    (AtkComponent    *component,
197                               gint            *x,
198                               gint            *y,
199                               gint            *width,
200                               gint            *height,
201                               AtkCoordType    coord_type)
202 {
203   AtkComponentIface *iface = NULL;
204   gint local_x, local_y, local_width, local_height;
205   gint *real_x, *real_y, *real_width, *real_height;
206
207   g_return_if_fail (ATK_IS_COMPONENT (component));
208
209   if (x)
210     real_x = x;
211   else
212     real_x = &local_x;
213   if (y)
214     real_y = y;
215   else
216     real_y = &local_y;
217   if (width)
218     real_width = width;
219   else
220     real_width = &local_width;
221   if (height)
222     real_height = height;
223   else
224     real_height = &local_height;
225
226   iface = ATK_COMPONENT_GET_IFACE (component);
227
228   if (iface->get_extents)
229     (iface->get_extents) (component, real_x, real_y, real_width, real_height, coord_type);
230 }
231
232 /**
233  * atk_component_get_position:
234  * @component: an #AtkComponent
235  * @x: address of #gint to put x coordinate position
236  * @y: address of #gint to put y coordinate position
237  * @coord_type: specifies whether the coordinates are relative to the screen
238  * or to the components top level window
239  *
240  * Gets the position of @component in the form of 
241  * a point specifying @component's top-left corner.
242  **/
243 void
244 atk_component_get_position   (AtkComponent    *component,
245                               gint            *x,
246                               gint            *y,
247                               AtkCoordType    coord_type)
248 {
249   AtkComponentIface *iface = NULL;
250   gint local_x, local_y;
251   gint *real_x, *real_y;
252
253   g_return_if_fail (ATK_IS_COMPONENT (component));
254
255   if (x)
256     real_x = x;
257   else
258     real_x = &local_x;
259   if (y)
260     real_y = y;
261   else
262     real_y = &local_y;
263
264   iface = ATK_COMPONENT_GET_IFACE (component);
265
266   if (iface->get_position)
267     (iface->get_position) (component, real_x, real_y, coord_type);
268   else
269   {
270     /*
271      * if this method is not overridden use the default implementation.
272      */
273     atk_component_real_get_position (component, real_x, real_y, coord_type);
274   }
275 }
276
277 /**
278  * atk_component_get_size:
279  * @component: an #AtkComponent
280  * @width: address of #gint to put width of @component
281  * @height: address of #gint to put height of @component
282  *
283  * Gets the size of the @component in terms of width and height.
284  **/
285 void
286 atk_component_get_size       (AtkComponent    *component,
287                               gint            *width,
288                               gint            *height)
289 {
290   AtkComponentIface *iface = NULL;
291   gint local_width, local_height;
292   gint *real_width, *real_height;
293
294   g_return_if_fail (ATK_IS_COMPONENT (component));
295
296   if (width)
297     real_width = width;
298   else
299     real_width = &local_width;
300   if (height)
301     real_height = height;
302   else
303     real_height = &local_height;
304
305   iface = ATK_COMPONENT_GET_IFACE (component);
306   g_return_if_fail (ATK_IS_COMPONENT (component));
307
308   iface = ATK_COMPONENT_GET_IFACE (component);
309
310   if (iface->get_size)
311     (iface->get_size) (component, real_width, real_height);
312   else
313   {
314     /*
315      * if this method is not overridden use the default implementation.
316      */
317     atk_component_real_get_size (component, real_width, real_height);
318   }
319 }
320
321 /**
322  * atk_component_grab_focus:
323  * @component: an #AtkComponent
324  *
325  * Grabs focus for this @component.
326  *
327  * Returns: %TRUE if successful, %FALSE otherwise.
328  **/
329 gboolean
330 atk_component_grab_focus (AtkComponent    *component)
331 {
332   AtkComponentIface *iface = NULL;
333   g_return_val_if_fail (ATK_IS_COMPONENT (component), FALSE);
334
335   iface = ATK_COMPONENT_GET_IFACE (component);
336
337   if (iface->grab_focus)
338     return (iface->grab_focus) (component);
339   else
340     return FALSE;
341 }
342
343 /**
344  * atk_component_set_extents:
345  * @component: an #AtkComponent
346  * @x: x coordinate
347  * @y: y coordinate
348  * @width: width to set for @component
349  * @height: height to set for @component
350  * @coord_type: specifies whether the coordinates are relative to the screen
351  * or to the components top level window
352  *
353  * Sets the extents of @component.
354  *
355  * Returns: %TRUE or %FALSE whether the extents were set or not
356  **/
357 gboolean
358 atk_component_set_extents   (AtkComponent    *component,
359                              gint            x,
360                              gint            y,
361                              gint            width,
362                              gint            height,
363                              AtkCoordType    coord_type)
364 {
365   AtkComponentIface *iface = NULL;
366   g_return_val_if_fail (ATK_IS_COMPONENT (component), FALSE);
367
368   iface = ATK_COMPONENT_GET_IFACE (component);
369
370   if (iface->set_extents)
371     return (iface->set_extents) (component, x, y, width, height, coord_type);
372   else
373     return FALSE;
374 }
375
376 /**
377  * atk_component_set_position:
378  * @component: an #AtkComponent
379  * @x: x coordinate
380  * @y: y coordinate
381  * @coord_type: specifies whether the coordinates are relative to the screen
382  * or to the components top level window
383  *
384  * Sets the postition of @component.
385  * 
386  * Returns: %TRUE or %FALSE whether or not the position was set or not
387  **/
388 gboolean
389 atk_component_set_position   (AtkComponent    *component,
390                               gint            x,
391                               gint            y,
392                               AtkCoordType    coord_type)
393 {
394   AtkComponentIface *iface = NULL;
395   g_return_val_if_fail (ATK_IS_COMPONENT (component), FALSE);
396
397   iface = ATK_COMPONENT_GET_IFACE (component);
398
399   if (iface->set_position)
400     return (iface->set_position) (component, x, y, coord_type);
401   else
402     return FALSE;
403 }
404
405 /**
406  * atk_component_set_size:
407  * @component: an #AtkComponent
408  * @width: width to set for @component
409  * @height: height to set for @component
410  *
411  * Set the size of the @component in terms of width and height.
412  *
413  * Returns: %TRUE or %FALSE whether the size was set or not
414  **/
415 gboolean
416 atk_component_set_size       (AtkComponent    *component,
417                               gint            x,
418                               gint            y)
419 {
420   AtkComponentIface *iface = NULL;
421   g_return_val_if_fail (ATK_IS_COMPONENT (component), FALSE);
422
423   iface = ATK_COMPONENT_GET_IFACE (component);
424
425   if (iface->set_size)
426     return (iface->set_size) (component, x, y);
427   else
428     return FALSE;
429 }
430
431 static gboolean
432 atk_component_real_contains (AtkComponent *component,
433                              gint         x,
434                              gint         y,
435                              AtkCoordType coord_type)
436 {
437   gint real_x, real_y, width, height;
438
439   real_x = real_y = width = height = 0;
440
441   atk_component_get_extents (component, &real_x, &real_y, &width, &height, coord_type);
442
443   if ((x >= real_x) &&
444       (x < real_x + width) &&
445       (y >= real_y) &&
446       (y < real_y + height))
447     return TRUE;
448   else
449     return FALSE;
450 }
451
452 static AtkObject* 
453 atk_component_real_ref_accessible_at_point (AtkComponent *component,
454                                             gint         x,
455                                             gint         y,
456                                             AtkCoordType coord_type)
457 {
458   gint count, i;
459
460   count = atk_object_get_n_accessible_children (ATK_OBJECT (component));
461
462   for (i = 0; i < count; i++)
463   {
464     AtkObject *obj;
465
466     obj = atk_object_ref_accessible_child (ATK_OBJECT (component), i);
467
468     if (obj != NULL)
469     {
470       if (atk_component_contains (ATK_COMPONENT (obj), x, y, coord_type))
471       {
472         return obj;
473       }
474       else
475       {
476         g_object_unref (obj);
477       }
478     }
479   }
480   return NULL;
481 }
482
483 static void
484 atk_component_real_get_position (AtkComponent *component,
485                                  gint         *x,
486                                  gint         *y,
487                                  AtkCoordType coord_type)
488 {
489   gint width, height;
490
491   atk_component_get_extents (component, x, y, &width, &height, coord_type);
492 }
493
494 static void
495 atk_component_real_get_size (AtkComponent *component,
496                              gint         *width,
497                              gint         *height)
498 {
499   gint x, y;
500   AtkCoordType coord_type;
501
502   /*
503    * Pick one coordinate type; it does not matter for size
504    */
505   coord_type = ATK_XY_WINDOW;
506
507   atk_component_get_extents (component, &x, &y, width, height, coord_type);
508 }