Remove some redundant out-of-memory checks
[platform/upstream/at-spi2-core.git] / atspi / atspi-component.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001, 2002 Sun Microsystems Inc.,
6  * Copyright 2001, 2002 Ximian, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /*
25  *
26  * AtspiComponent function implementations
27  *
28  */
29
30 #include "atspi-private.h"
31
32 void
33 atspi_rect_free (AtspiRect *rect)
34 {
35   g_free (rect);
36 }
37
38 AtspiRect *
39 atspi_rect_copy (AtspiRect *src)
40 {
41   AtspiRect *dst = g_new (AtspiRect, 1);
42   dst->x = src->x;
43   dst->y = src->y;
44   dst->height = src->height;
45   dst->width = src->width;
46   return dst;
47 }
48
49 G_DEFINE_BOXED_TYPE (AtspiRect, atspi_rect, atspi_rect_copy, atspi_rect_free)
50
51 AtspiPoint *
52 atspi_point_copy (AtspiPoint *src)
53 {
54   AtspiPoint *dst = g_new (AtspiPoint, 1);
55   dst->x = src->x;
56   dst->y = src->y;
57   return dst;
58 }
59
60 G_DEFINE_BOXED_TYPE (AtspiPoint, atspi_point, atspi_point_copy, g_free)
61
62 /**
63  * atspi_component_contains:
64  * @obj: a pointer to the #AtspiComponent to query.
65  * @x: a #long specifying the x coordinate in question.
66  * @y: a #long specifying the y coordinate in question.
67  * @ctype: the desired coordinate system of the point (@x, @y)
68  *         (e.g. CSPI_COORD_TYPE_WINDOW, CSPI_COORD_TYPE_SCREEN).
69  *
70  * Query whether a given #AtspiComponent contains a particular point.
71  *
72  * Returns: a #TRUE if the specified component contains the point (@x, @y),
73  *          otherwise #FALSE.
74  **/
75 gboolean
76 atspi_component_contains (AtspiComponent *obj,
77                               gint x,
78                               gint y,
79                               AtspiCoordType ctype, GError **error)
80 {
81   dbus_bool_t retval = FALSE;
82   dbus_int32_t d_x = x, d_y = y;
83   dbus_uint32_t d_ctype = ctype;
84
85   g_return_val_if_fail (obj != NULL, FALSE);
86
87   _atspi_dbus_call (obj, atspi_interface_component, "Contains", error, "iin=>b", d_x, d_y, d_ctype, &retval);
88
89   return retval;
90 }
91
92 /**
93  * atspi_component_get_accessible_at_point:
94  * @obj: a pointer to the #AtspiComponent to query.
95  * @x: a #gint specifying the x coordinate of the point in question.
96  * @y: a #gint specifying the y coordinate of the point in question.
97  * @ctype: the coordinate system of the point (@x, @y)
98  *         (e.g. ATSPI_COORD_TYPE_WINDOW, ATSPI_COORD_TYPE_SCREEN).
99  *
100  * Get the accessible child at a given coordinate within an #AtspiComponent.
101  *
102  * Returns: (transfer full): a pointer to an #AtspiAccessible child of the
103  *          specified component which contains the point (@x, @y), or NULL of
104  *         no child contains the point.
105  **/
106 AtspiAccessible *
107 atspi_component_get_accessible_at_point (AtspiComponent *obj,
108                                           gint x,
109                                           gint y,
110                                           AtspiCoordType ctype, GError **error)
111 {
112   dbus_int32_t d_x = x, d_y = y;
113   dbus_uint32_t d_ctype = ctype;
114   DBusMessage *reply;
115
116   g_return_val_if_fail (obj != NULL, FALSE);
117
118   reply = _atspi_dbus_call_partial (obj, atspi_interface_component, "GetAccessibleAtPoint", error, "iiu", d_x, d_y, d_ctype);
119
120   return _atspi_dbus_return_accessible_from_message (reply);
121 }
122
123 /**
124  * atspi_component_get_extents:
125  * @obj: a pointer to the #AtspiComponent to query.
126  * @ctype: the desired coordinate system into which to return the results,
127  *         (e.g. ATSPI_COORD_TYPE_WINDOW, ATSPI_COORD_TYPE_SCREEN).
128  *
129  * Returns: A #AtspiRect giving the accessible's extents.
130  *
131  * Get the bounding box of the specified #AtspiComponent.
132  *
133  **/
134 AtspiRect *
135 atspi_component_get_extents (AtspiComponent *obj,
136                                 AtspiCoordType ctype, GError **error)
137 {
138   dbus_uint32_t d_ctype = ctype;
139   AtspiRect bbox;
140
141   bbox.x = bbox.y = bbox.width = bbox.height = -1;
142   g_return_val_if_fail (obj != NULL, atspi_rect_copy (&bbox));
143
144   _atspi_dbus_call (obj, atspi_interface_component, "GetExtents", error, "u=>(iiii)", d_ctype, &bbox);
145   return atspi_rect_copy (&bbox);
146 }
147
148 /**
149  * atspi_component_get_position:
150  * @obj: a pointer to the #AtspiComponent to query.
151  * @ctype: the desired coordinate system into which to return the results,
152  *         (e.g. ATSPI_COORD_TYPE_WINDOW, ATSPI_COORD_TYPE_SCREEN).
153  *
154  * returns: A #AtspiPoint giving the position.
155  * Get the minimum x and y coordinates of the specified #AtspiComponent.
156  *
157  **/
158 AtspiPoint *
159 atspi_component_get_position (AtspiComponent *obj,
160                                  AtspiCoordType ctype, GError **error)
161 {
162   dbus_int32_t d_x, d_y;
163   dbus_uint32_t d_ctype = ctype;
164   AtspiPoint ret;
165
166   ret.x = ret.y = -1;
167
168   if (!obj)
169     return atspi_point_copy (&ret);
170
171   _atspi_dbus_call (obj, atspi_interface_component, "GetPosition", error, "u=>ii", d_ctype, &d_x, &d_y);
172
173   ret.x = d_x;
174   ret.y = d_y;
175   return atspi_point_copy (&ret);
176 }
177
178 /**
179  * atspi_component_get_size:
180  * @obj: a pointer to the #AtspiComponent to query.
181  * returns: A #AtspiPoint giving the size.
182  *
183  * Get the size of the specified #AtspiComponent.
184  *
185  **/
186 AtspiPoint *
187 atspi_component_get_size (AtspiComponent *obj, GError **error)
188 {
189   dbus_int32_t d_w, d_h;
190   AtspiPoint ret;
191
192   ret.x = ret.y = -1;
193   if (!obj)
194     return atspi_point_copy (&ret);
195
196   _atspi_dbus_call (obj, atspi_interface_component, "GetSize", error, "=>ii", &d_w, &d_h);
197   ret.x = d_w;
198   ret.y = d_h;
199   return atspi_point_copy (&ret);
200 }
201
202 /**
203  * atspi_component_get_layer:
204  * @obj: a pointer to the #AtspiComponent to query.
205  *
206  * Query which layer the component is painted into, to help determine its 
207  *      visibility in terms of stacking order.
208  *
209  * Returns: the #AtspiComponentLayer into which this component is painted.
210  **/
211 AtspiComponentLayer
212 atspi_component_get_layer (AtspiComponent *obj, GError **error)
213 {
214   dbus_uint32_t zlayer = -1;
215
216   _atspi_dbus_call (obj, atspi_interface_component, "GetLayer", error, "=>u", &zlayer);
217
218   return zlayer;
219 }
220
221 /**
222  * atspi_component_get_mdi_z_order:
223  * @obj: a pointer to the #AtspiComponent to query.
224  *
225  * Query the z stacking order of a component which is in the MDI or window
226  *       layer. (Bigger z-order numbers mean nearer the top)
227  *
228  * Returns: a short integer indicating the stacking order of the component 
229  *       in the MDI layer, or -1 if the component is not in the MDI layer.
230  **/
231 gshort
232 atspi_component_get_mdi_z_order (AtspiComponent *obj, GError **error)
233 {
234   dbus_uint16_t retval = -1;
235
236   _atspi_dbus_call (obj, atspi_interface_component, "GetMDIZOrder", error, "=>n", &retval);
237
238   return retval;
239 }
240
241 /**
242  * atspi_component_grab_focus:
243  * @obj: a pointer to the #AtspiComponent on which to operate.
244  *
245  * Attempt to set the keyboard input focus to the specified
246  *         #AtspiComponent.
247  *
248  * Returns: #TRUE if successful, #FALSE otherwise.
249  *
250  **/
251 gboolean
252 atspi_component_grab_focus (AtspiComponent *obj, GError **error)
253 {
254   dbus_bool_t retval = FALSE;
255
256   _atspi_dbus_call (obj, atspi_interface_component, "GrabFocus", error, "=>b", &retval);
257
258   return retval;
259 }
260
261 /**
262  * atspi_component_get_alpha:
263  * @obj: The #AtspiComponent to be queried.
264  *
265  * Get the opacity/alpha value of a component, if alpha blending is in use.
266  *
267  * Returns: the opacity value of a component, as a double between 0.0 and 1.0. 
268  **/
269 gdouble      
270 atspi_component_get_alpha    (AtspiComponent *obj, GError **error)
271 {
272   double retval = 1;
273
274   _atspi_dbus_call (obj, atspi_interface_component, "GetAlpha", error, "=>d", &retval);
275
276   return retval;
277 }
278
279 /**
280  * atspi_component_set_extents:
281  * @obj: a pointer to the #AtspiComponent to move.
282  * @x: the new vertical position to which the component should be moved.
283  * @y: the new horizontal position to which the component should be moved.
284  * @width: the width to which the component should be resized.
285  * @height: the height to which the component should be resized.
286  * @ctype: the coordinate system in which the position is specified.
287  *         (e.g. ATSPI_COORD_TYPE_WINDOW, ATSPI_COORD_TYPE_SCREEN).
288  *
289  * Returns: #TRUE if successful; #FALSE otherwise.
290  *
291  * Move and resize the specified component.
292  *
293  **/
294 gboolean
295 atspi_component_set_extents (AtspiComponent *obj,
296                              gint x,
297                              gint y,
298                              gint width,
299                              gint height,
300                              AtspiCoordType ctype,
301                              GError **error)
302 {
303   dbus_int32_t d_x = x, d_y = y, d_width = width, d_height = height;
304   dbus_uint32_t d_ctype = ctype;
305   DBusMessageIter iter, iter_struct;
306   DBusMessage *message, *reply;
307   dbus_bool_t retval = FALSE;
308   AtspiAccessible *aobj = ATSPI_ACCESSIBLE (obj);
309
310   g_return_val_if_fail (obj != NULL, FALSE);
311
312   message = dbus_message_new_method_call (aobj->parent.app->bus_name,
313                                           aobj->parent.path,
314                                           atspi_interface_component,
315                                           "SetExtents");
316
317   dbus_message_iter_init_append (message, &iter);
318   if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_STRUCT, NULL, &iter_struct))
319   {
320     dbus_message_unref (message);
321     return FALSE;
322   }
323   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_INT32, &d_x);
324   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_INT32, &d_y);
325   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_INT32, &d_width);
326   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_INT32, &d_height);
327   dbus_message_iter_close_container (&iter, &iter_struct);
328   dbus_message_iter_append_basic (&iter, DBUS_TYPE_UINT32, &d_ctype);
329
330   reply = _atspi_dbus_send_with_reply_and_block (message, error);
331   dbus_message_get_args (reply, NULL, DBUS_TYPE_BOOLEAN, &retval,
332                               DBUS_TYPE_INVALID);
333   dbus_message_unref (reply);
334   return retval;
335 }
336
337 /**
338  * atspi_component_set_position:
339  * @obj: a pointer to the #AtspiComponent to move.
340  * @x: the new vertical position to which the component should be moved.
341  * @y: the new horizontal position to which the component should be moved.
342  * @ctype: the coordinate system in which the position is specified.
343  *         (e.g. ATSPI_COORD_TYPE_WINDOW, ATSPI_COORD_TYPE_SCREEN).
344  *
345  * Returns: #TRUE if successful; #FALSE otherwise.
346  *
347  * Moves the component to the specified position.
348  **/
349 gboolean
350 atspi_component_set_position (AtspiComponent *obj,
351                               gint x,
352                               gint y,
353                               AtspiCoordType ctype,
354                               GError **error)
355 {
356   dbus_int32_t d_x = x, d_y = y;
357   dbus_uint32_t d_ctype = ctype;
358   dbus_bool_t ret = FALSE;
359
360   g_return_val_if_fail (obj != NULL, FALSE);
361
362   _atspi_dbus_call (obj, atspi_interface_component, "SetPosition", error,
363                     "iiu=>b", d_x, d_y, d_ctype, &ret);
364
365   return ret;
366 }
367
368 /**
369  * atspi_component_set_size:
370  * @obj: a pointer to the #AtspiComponent to query.
371  * @width: the width to which the component should be resized.
372  * @height: the height to which the component should be resized.
373  *
374  * Returns: #TRUE if successful; #FALSE otherwise.
375  *
376  * Resize the specified component to the given coordinates.
377  **/
378 gboolean
379 atspi_component_set_size (AtspiComponent *obj,
380                           gint width,
381                           gint height,
382                           GError **error)
383 {
384   dbus_int32_t d_width = width, d_height = height;
385   dbus_bool_t ret = FALSE;
386
387   g_return_val_if_fail (obj != NULL, FALSE);
388
389   _atspi_dbus_call (obj, atspi_interface_component, "SetSize", error, "ii=>b",
390                     d_width, d_height, &ret);
391
392   return ret;
393 }
394
395 static void
396 atspi_component_base_init (AtspiComponent *klass)
397 {
398 }
399
400 GType
401 atspi_component_get_type (void)
402 {
403   static GType type = 0;
404
405   if (!type) {
406     static const GTypeInfo tinfo =
407     {
408       sizeof (AtspiComponent),
409       (GBaseInitFunc) atspi_component_base_init,
410       (GBaseFinalizeFunc) NULL,
411     };
412
413     type = g_type_register_static (G_TYPE_INTERFACE, "AtspiComponent", &tinfo, 0);
414
415   }
416   return type;
417 }