Remove all instances of g_return_if_fail (foo != NULL); that are
[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 AtkObject* atk_component_real_get_accessible_at_point (AtkComponent *component,
24                                                               gint         x,
25                                                               gint         y,
26                                                               AtkCoordType coord_type);
27
28 GType
29 atk_component_get_type ()
30 {
31   static GType type = 0;
32
33   if (!type) {
34     static const GTypeInfo tinfo =
35     {
36       sizeof (AtkComponentIface),
37       (GBaseInitFunc) NULL,
38       (GBaseFinalizeFunc) NULL,
39
40     };
41
42     type = g_type_register_static (G_TYPE_INTERFACE, "AtkComponent", &tinfo, 0);
43   }
44
45   return type;
46 }
47
48 /**
49  * atk_component_add_focus_handler:
50  * @component: The #AtkComponent to attach the @handler to
51  * @handler: The #AtkFocusHandler to be attached to @component
52  *
53  * Add the specified handler to the set of functions to be called 
54  * when this object receives focus events (in or out).
55  *
56  * Returns:
57  **/
58 guint
59 atk_component_add_focus_handler (AtkComponent    *component,
60                                  AtkFocusHandler handler)
61 {
62   AtkComponentIface *iface = NULL;
63   g_return_val_if_fail (component != NULL, 0);
64   g_return_val_if_fail (ATK_IS_COMPONENT (component), 0);
65
66   iface = ATK_COMPONENT_GET_IFACE (component);
67
68   if (iface->add_focus_handler)
69     return (iface->add_focus_handler) (component, handler);
70   else
71     return 0;
72 }
73
74 /**
75  * atk_component_remove_focus_handler:
76  * @component: the #AtkComponent to remove the focus handler from
77  * @handler_id: the handler id of the focus handler to be removed
78  * from @component
79  *
80  * Remove the handler specified by @handler_id from the list of
81  * functions to be executed when this object receives focus events 
82  * (in or out).
83  **/
84 void
85 atk_component_remove_focus_handler (AtkComponent    *component,
86                                     guint           handler_id)
87 {
88   AtkComponentIface *iface = NULL;
89   g_return_if_fail (ATK_IS_COMPONENT (component));
90
91   iface = ATK_COMPONENT_GET_IFACE (component);
92
93   if (iface->remove_focus_handler)
94     (iface->remove_focus_handler) (component, handler_id);
95 }
96
97 /**
98  * atk_component_contains:
99  * @component: the #AtkComponent
100  * @x: x coordinate
101  * @y: y coordinate
102  * @coord_type: specifies whether the coordinates are relative to the screen
103  * or to the components top level window
104  *
105  * Checks whether the specified point is within the extent of the @component.
106  *
107  * Returns: %TRUE or %FALSE indicating whether the specified point is within
108  * the extent of the @component or not
109  **/
110 gboolean
111 atk_component_contains (AtkComponent    *component,
112                         gint            x,
113                         gint            y,
114                         AtkCoordType    coord_type)
115 {
116   AtkComponentIface *iface = NULL;
117   g_return_val_if_fail (component != NULL, FALSE);
118   g_return_val_if_fail (ATK_IS_COMPONENT (component), FALSE);
119
120   iface = ATK_COMPONENT_GET_IFACE (component);
121
122   if (iface->contains)
123     return (iface->contains) (component, x, y, coord_type);
124   else
125     return FALSE;
126 }
127
128 /**
129  * atk_component_get_accessible_at_point:
130  * @component: the #AtkComponent
131  * @x: x coordinate
132  * @y: y coordinate
133  * @coord_type: specifies whether the coordinates are relative to the screen
134  * or to the components top level window
135  *
136  * Gets the accessible child, if one exists, contained at the
137  * coordinate point specified by @x and @y.
138  *
139  * Returns: the accessible child, if one exists
140  **/
141 AtkObject*
142 atk_component_get_accessible_at_point (AtkComponent    *component,
143                                        gint            x,
144                                        gint            y,
145                                        AtkCoordType    coord_type)
146 {
147   AtkComponentIface *iface = NULL;
148   g_return_val_if_fail (component != NULL, NULL);
149   g_return_val_if_fail (ATK_IS_COMPONENT (component), NULL);
150
151   iface = ATK_COMPONENT_GET_IFACE (component);
152
153   if (iface->get_accessible_at_point)
154     return (iface->get_accessible_at_point) (component, x, y, coord_type);
155   else
156   {
157     /*
158      * if this method is not overridden use the default implementation.
159      */
160     return atk_component_real_get_accessible_at_point (component, x, y, coord_type);
161   }
162 }
163
164 /**
165  * atk_component_get_extents:
166  * @component: an #AtkComponent
167  * @x: address of #gint to put x coordinate
168  * @y: address of #gint to put y coordinate
169  * @width: address of #gint to put width
170  * @height: address of #gint to put height
171  * @coord_type: specifies whether the coordinates are relative to the screen
172  * or to the components top level window
173  *
174  * Gets the rectangle which gives the extent of the @component.
175  *
176  **/
177 void
178 atk_component_get_extents    (AtkComponent    *component,
179                               gint            *x,
180                               gint            *y,
181                               gint            *width,
182                               gint            *height,
183                               AtkCoordType    coord_type)
184 {
185   AtkComponentIface *iface = NULL;
186   g_return_if_fail (ATK_IS_COMPONENT (component));
187
188   iface = ATK_COMPONENT_GET_IFACE (component);
189
190   if (iface->get_extents)
191     (iface->get_extents) (component, x, y, width, height, coord_type);
192 }
193
194 /**
195  * atk_component_get_position:
196  * @component: an #AtkComponent
197  * @x: address of #gint to put x coordinate position
198  * @y: address of #gint to put y coordinate position
199  * @coord_type: specifies whether the coordinates are relative to the screen
200  * or to the components top level window
201  *
202  * Gets the position of @component in the form of 
203  * a point specifying @component's top-left corner.
204  **/
205 void
206 atk_component_get_position   (AtkComponent    *component,
207                               gint            *x,
208                               gint            *y,
209                               AtkCoordType    coord_type)
210 {
211   AtkComponentIface *iface = NULL;
212   g_return_if_fail (ATK_IS_COMPONENT (component));
213
214   iface = ATK_COMPONENT_GET_IFACE (component);
215
216   if (iface->get_position)
217     (iface->get_position) (component, x, y, coord_type);
218 }
219
220 /**
221  * atk_component_get_size:
222  * @component: an #AtkComponent
223  * @width: address of #gint to put width of @component
224  * @height: address of #gint to put height of @component
225  *
226  * Gets the size of the @component in terms of width and height.
227  **/
228 void
229 atk_component_get_size       (AtkComponent    *component,
230                               gint            *x,
231                               gint            *y)
232 {
233   AtkComponentIface *iface = NULL;
234   g_return_if_fail (ATK_IS_COMPONENT (component));
235
236   iface = ATK_COMPONENT_GET_IFACE (component);
237
238   if (iface->get_size)
239     (iface->get_size) (component, x, y);
240 }
241
242 /**
243  * atk_component_grab_focus:
244  * @component: an #AtkComponent
245  *
246  * Grabs focus for this @component.
247  *
248  * Returns: %TRUE if successful, %FALSE otherwise.
249  **/
250 gboolean
251 atk_component_grab_focus (AtkComponent    *component)
252 {
253   AtkComponentIface *iface = NULL;
254   g_return_val_if_fail (component != NULL, FALSE);
255   g_return_val_if_fail (ATK_IS_COMPONENT (component), FALSE);
256
257   iface = ATK_COMPONENT_GET_IFACE (component);
258
259   if (iface->grab_focus)
260     return (iface->grab_focus) (component);
261   else
262     return FALSE;
263 }
264
265 /**
266  * atk_component_set_extents:
267  * @component: an #AtkComponent
268  * @x: x coordinate
269  * @y: y coordinate
270  * @width: width to set for @component
271  * @height: height to set for @component
272  * @coord_type: specifies whether the coordinates are relative to the screen
273  * or to the components top level window
274  *
275  * Sets the extents of @component.
276  *
277  * Returns: %TRUE or %FALSE whether the extents were set or not
278  **/
279 gboolean
280 atk_component_set_extents   (AtkComponent    *component,
281                              gint            x,
282                              gint            y,
283                              gint            width,
284                              gint            height,
285                              AtkCoordType    coord_type)
286 {
287   AtkComponentIface *iface = NULL;
288   g_return_val_if_fail (component != NULL, FALSE);
289   g_return_val_if_fail (ATK_IS_COMPONENT (component), FALSE);
290
291   iface = ATK_COMPONENT_GET_IFACE (component);
292
293   if (iface->set_extents)
294     return (iface->set_extents) (component, x, y, width, height, coord_type);
295   else
296     return FALSE;
297 }
298
299 /**
300  * atk_component_set_position:
301  * @component: an #AtkComponent
302  * @x: x coordinate
303  * @y: y coordinate
304  * @coord_type: specifies whether the coordinates are relative to the screen
305  * or to the components top level window
306  *
307  * Sets the postition of @component.
308  * 
309  * Returns: %TRUE or %FALSE whether or not the position was set or not
310  **/
311 gboolean
312 atk_component_set_position   (AtkComponent    *component,
313                               gint            x,
314                               gint            y,
315                               AtkCoordType    coord_type)
316 {
317   AtkComponentIface *iface = NULL;
318   g_return_val_if_fail (component != NULL, FALSE);
319   g_return_val_if_fail (ATK_IS_COMPONENT (component), FALSE);
320
321   iface = ATK_COMPONENT_GET_IFACE (component);
322
323   if (iface->set_position)
324     return (iface->set_position) (component, x, y, coord_type);
325   else
326     return FALSE;
327 }
328
329 /**
330  * atk_component_set_size:
331  * @component: an #AtkComponent
332  * @width: width to set for @component
333  * @height: height to set for @component
334  *
335  * Set the size of the @component in terms of width and height.
336  *
337  * Returns: %TRUE or %FALSE whether the size was set or not
338  **/
339 gboolean
340 atk_component_set_size       (AtkComponent    *component,
341                               gint            x,
342                               gint            y)
343 {
344   AtkComponentIface *iface = NULL;
345   g_return_val_if_fail (component != NULL, FALSE);
346   g_return_val_if_fail (ATK_IS_COMPONENT (component), FALSE);
347
348   iface = ATK_COMPONENT_GET_IFACE (component);
349
350   if (iface->set_size)
351     return (iface->set_size) (component, x, y);
352   else
353     return FALSE;
354 }
355
356 static AtkObject* 
357 atk_component_real_get_accessible_at_point (AtkComponent *component,
358                                             gint         x,
359                                             gint         y,
360                                             AtkCoordType coord_type)
361 {
362   gint count, i;
363
364   count = atk_object_get_n_accessible_children (ATK_OBJECT (component));
365
366   g_return_val_if_fail (count != 0, NULL);
367
368   for (i = 0; i < count; i++)
369   {
370     AtkObject *obj;
371
372     obj = atk_object_ref_accessible_child (ATK_OBJECT (component), i);
373
374     if (obj != NULL)
375     {
376       if (atk_component_contains (ATK_COMPONENT (obj), x, y, coord_type))
377       {
378         g_object_unref (obj);
379         return obj;
380       }
381       else
382       {
383         g_object_unref (obj);
384       }
385     }
386   }
387   return NULL;
388 }