Avoid returning uninitialized data for some IPC errors
[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   if (dst)
43   {
44     dst->x = src->x;
45     dst->y = src->y;
46     dst->height = src->height;
47     dst->width = src->width;
48   }
49   return dst;
50 }
51
52 G_DEFINE_BOXED_TYPE (AtspiRect, atspi_rect, atspi_rect_copy, atspi_rect_free)
53
54 AtspiPoint *
55 atspi_point_copy (AtspiPoint *src)
56 {
57   AtspiPoint *dst = g_new (AtspiPoint, 1);
58   if (dst)
59   {
60     dst->x = src->x;
61     dst->y = src->y;
62   }
63   return dst;
64 }
65
66 G_DEFINE_BOXED_TYPE (AtspiPoint, atspi_point, atspi_point_copy, g_free)
67
68 /**
69  * atspi_component_contains:
70  * @obj: a pointer to the #AtspiComponent to query.
71  * @x: a #long specifying the x coordinate in question.
72  * @y: a #long specifying the y coordinate in question.
73  * @ctype: the desired coordinate system of the point (@x, @y)
74  *         (e.g. CSPI_COORD_TYPE_WINDOW, CSPI_COORD_TYPE_SCREEN).
75  *
76  * Query whether a given #AtspiComponent contains a particular point.
77  *
78  * Returns: a #TRUE if the specified component contains the point (@x, @y),
79  *          otherwise #FALSE.
80  **/
81 gboolean
82 atspi_component_contains (AtspiComponent *obj,
83                               gint x,
84                               gint y,
85                               AtspiCoordType ctype, GError **error)
86 {
87   dbus_bool_t retval = FALSE;
88   dbus_int32_t d_x = x, d_y = y;
89   dbus_uint32_t d_ctype = ctype;
90
91   g_return_val_if_fail (obj != NULL, FALSE);
92
93   _atspi_dbus_call (obj, atspi_interface_component, "Contains", error, "iin=>b", d_x, d_y, d_ctype, &retval);
94
95   return retval;
96 }
97
98 /**
99  * atspi_component_get_accessible_at_point:
100  * @obj: a pointer to the #AtspiComponent to query.
101  * @x: a #gint specifying the x coordinate of the point in question.
102  * @y: a #gint specifying the y coordinate of the point in question.
103  * @ctype: the coordinate system of the point (@x, @y)
104  *         (e.g. ATSPI_COORD_TYPE_WINDOW, ATSPI_COORD_TYPE_SCREEN).
105  *
106  * Get the accessible child at a given coordinate within an #AtspiComponent.
107  *
108  * Returns: (transfer full): a pointer to an #AtspiAccessible child of the
109  *          specified component which contains the point (@x, @y), or NULL of
110  *         no child contains the point.
111  **/
112 AtspiAccessible *
113 atspi_component_get_accessible_at_point (AtspiComponent *obj,
114                                           gint x,
115                                           gint y,
116                                           AtspiCoordType ctype, GError **error)
117 {
118   dbus_int32_t d_x = x, d_y = y;
119   dbus_uint16_t d_ctype = ctype;
120   DBusMessage *reply;
121
122   g_return_val_if_fail (obj != NULL, FALSE);
123
124   reply = _atspi_dbus_call_partial (obj, atspi_interface_component, "GetAccessibleAtPoint", error, "iin", d_x, d_y, d_ctype);
125
126   return _atspi_dbus_return_accessible_from_message (reply);
127 }
128
129 /**
130  * atspi_component_get_extents:
131  * @obj: a pointer to the #AtspiComponent to query.
132  * @ctype: the desired coordinate system into which to return the results,
133  *         (e.g. ATSPI_COORD_TYPE_WINDOW, ATSPI_COORD_TYPE_SCREEN).
134  *
135  * Returns: A #AtspiRect giving the accessible's extents.
136  *
137  * Get the bounding box of the specified #AtspiComponent.
138  *
139  **/
140 AtspiRect *
141 atspi_component_get_extents (AtspiComponent *obj,
142                                 AtspiCoordType ctype, GError **error)
143 {
144   dbus_uint32_t d_ctype = ctype;
145   AtspiRect bbox;
146
147   bbox.x = bbox.y = bbox.width = bbox.height = -1;
148   g_return_val_if_fail (obj != NULL, atspi_rect_copy (&bbox));
149
150   _atspi_dbus_call (obj, atspi_interface_component, "GetExtents", error, "u=>(iiii)", d_ctype, &bbox);
151   return atspi_rect_copy (&bbox);
152 }
153
154 /**
155  * atspi_component_get_position:
156  * @obj: a pointer to the #AtspiComponent to query.
157  * @ctype: the desired coordinate system into which to return the results,
158  *         (e.g. ATSPI_COORD_TYPE_WINDOW, ATSPI_COORD_TYPE_SCREEN).
159  *
160  * returns: A #AtspiPoint giving the position.
161  * Get the minimum x and y coordinates of the specified #AtspiComponent.
162  *
163  **/
164 AtspiPoint *
165 atspi_component_get_position (AtspiComponent *obj,
166                                  AtspiCoordType ctype, GError **error)
167 {
168   dbus_int32_t d_x, d_y;
169   dbus_uint32_t d_ctype = ctype;
170   AtspiPoint ret;
171
172   ret.x = ret.y = -1;
173
174   if (!obj)
175     return atspi_point_copy (&ret);
176
177   _atspi_dbus_call (obj, atspi_interface_component, "GetPosition", error, "u=>ii", d_ctype, &d_x, &d_y);
178
179   ret.x = d_x;
180   ret.y = d_y;
181   return atspi_point_copy (&ret);
182 }
183
184 /**
185  * atspi_component_get_size:
186  * @obj: a pointer to the #AtspiComponent to query.
187  * returns: A #AtspiPoint giving the size.
188  *
189  * Get the size of the specified #AtspiComponent.
190  *
191  **/
192 AtspiPoint *
193 atspi_component_get_size (AtspiComponent *obj, GError **error)
194 {
195   dbus_int32_t d_w, d_h;
196   AtspiPoint ret;
197
198   ret.x = ret.y = -1;
199   if (!obj)
200     return atspi_point_copy (&ret);
201
202   _atspi_dbus_call (obj, atspi_interface_component, "GetSize", error, "=>ii", &d_w, &d_h);
203   ret.x = d_w;
204   ret.y = d_h;
205   return atspi_point_copy (&ret);
206 }
207
208 /**
209  * atspi_component_get_layer:
210  * @obj: a pointer to the #AtspiComponent to query.
211  *
212  * Query which layer the component is painted into, to help determine its 
213  *      visibility in terms of stacking order.
214  *
215  * Returns: the #AtspiComponentLayer into which this component is painted.
216  **/
217 AtspiComponentLayer
218 atspi_component_get_layer (AtspiComponent *obj, GError **error)
219 {
220   dbus_uint32_t zlayer = -1;
221
222   _atspi_dbus_call (obj, atspi_interface_component, "GetLayer", error, "=>u", &zlayer);
223
224   return zlayer;
225 }
226
227 /**
228  * atspi_component_get_mdi_z_order:
229  * @obj: a pointer to the #AtspiComponent to query.
230  *
231  * Query the z stacking order of a component which is in the MDI or window
232  *       layer. (Bigger z-order numbers mean nearer the top)
233  *
234  * Returns: a short integer indicating the stacking order of the component 
235  *       in the MDI layer, or -1 if the component is not in the MDI layer.
236  **/
237 gshort
238 atspi_component_get_mdi_z_order (AtspiComponent *obj, GError **error)
239 {
240   dbus_uint16_t retval = -1;
241
242   _atspi_dbus_call (obj, atspi_interface_component, "GetMDIZOrder", error, "=>n", &retval);
243
244   return retval;
245 }
246
247 /**
248  * atspi_component_grab_focus:
249  * @obj: a pointer to the #AtspiComponent on which to operate.
250  *
251  * Attempt to set the keyboard input focus to the specified
252  *         #AtspiComponent.
253  *
254  * Returns: #TRUE if successful, #FALSE otherwise.
255  *
256  **/
257 gboolean
258 atspi_component_grab_focus (AtspiComponent *obj, GError **error)
259 {
260   dbus_bool_t retval = FALSE;
261
262   _atspi_dbus_call (obj, atspi_interface_component, "GrabFocus", error, "=>b", &retval);
263
264   return retval;
265 }
266
267 /**
268  * atspi_component_get_alpha:
269  * @obj: The #AtspiComponent to be queried.
270  *
271  * Get the opacity/alpha value of a component, if alpha blending is in use.
272  *
273  * Returns: the opacity value of a component, as a double between 0.0 and 1.0. 
274  **/
275 gdouble      
276 atspi_component_get_alpha    (AtspiComponent *obj, GError **error)
277 {
278   double retval = 1;
279
280   _atspi_dbus_call (obj, atspi_interface_component, "GetAlpha", error, "=>d", &retval);
281
282   return retval;
283 }
284
285 /**
286  * atspi_component_set_extents:
287  * @obj: a pointer to the #AtspiComponent to move.
288  * @x: the new vertical position to which the component should be moved.
289  * @y: the new horizontal position to which the component should be moved.
290  * @width: the width to which the component should be resized.
291  * @height: the height to which the component should be resized.
292  * @ctype: the coordinate system in which the position is specified.
293  *         (e.g. ATSPI_COORD_TYPE_WINDOW, ATSPI_COORD_TYPE_SCREEN).
294  *
295  * Returns: #TRUE if successful; #FALSE otherwise.
296  *
297  * Move and resize the specified component.
298  *
299  **/
300 gboolean
301 atspi_component_set_extents (AtspiComponent *obj,
302                              gint x,
303                              gint y,
304                              gint width,
305                              gint height,
306                              AtspiCoordType ctype,
307                              GError **error)
308 {
309   dbus_int32_t d_x = x, d_y = y, d_width = width, d_height = height;
310   dbus_uint32_t d_ctype = ctype;
311   DBusMessageIter iter, iter_struct;
312   DBusMessage *message, *reply;
313   dbus_bool_t retval = FALSE;
314   AtspiAccessible *aobj = ATSPI_ACCESSIBLE (obj);
315
316   g_return_val_if_fail (obj != NULL, FALSE);
317
318   message = dbus_message_new_method_call (aobj->parent.app->bus_name,
319                                           aobj->parent.path,
320                                           atspi_interface_component,
321                                           "SetExtents");
322
323   dbus_message_iter_init_append (message, &iter);
324   if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_STRUCT, NULL, &iter_struct))
325   {
326     dbus_message_unref (message);
327     return FALSE;
328   }
329   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_INT32, &d_x);
330   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_INT32, &d_y);
331   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_INT32, &d_width);
332   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_INT32, &d_height);
333   dbus_message_iter_close_container (&iter, &iter_struct);
334   dbus_message_iter_append_basic (&iter, DBUS_TYPE_UINT32, &d_ctype);
335
336   reply = _atspi_dbus_send_with_reply_and_block (message, error);
337   dbus_message_get_args (reply, NULL, DBUS_TYPE_BOOLEAN, &retval,
338                               DBUS_TYPE_INVALID);
339   dbus_message_unref (reply);
340   return retval;
341 }
342
343 /**
344  * atspi_component_set_position:
345  * @obj: a pointer to the #AtspiComponent to move.
346  * @x: the new vertical position to which the component should be moved.
347  * @y: the new horizontal position to which the component should be moved.
348  * @ctype: the coordinate system in which the position is specified.
349  *         (e.g. ATSPI_COORD_TYPE_WINDOW, ATSPI_COORD_TYPE_SCREEN).
350  *
351  * Returns: #TRUE if successful; #FALSE otherwise.
352  *
353  * Moves the component to the specified position.
354  **/
355 gboolean
356 atspi_component_set_position (AtspiComponent *obj,
357                               gint x,
358                               gint y,
359                               AtspiCoordType ctype,
360                               GError **error)
361 {
362   dbus_int32_t d_x = x, d_y = y;
363   dbus_uint32_t d_ctype = ctype;
364   dbus_bool_t ret = FALSE;
365
366   g_return_val_if_fail (obj != NULL, FALSE);
367
368   _atspi_dbus_call (obj, atspi_interface_component, "SetPosition", error,
369                     "iiu=>b", d_x, d_y, d_ctype, &ret);
370
371   return ret;
372 }
373
374 /**
375  * atspi_component_set_size:
376  * @obj: a pointer to the #AtspiComponent to query.
377  * @width: the width to which the component should be resized.
378  * @height: the height to which the component should be resized.
379  *
380  * Returns: #TRUE if successful; #FALSE otherwise.
381  *
382  * Resize the specified component to the given coordinates.
383  **/
384 gboolean
385 atspi_component_set_size (AtspiComponent *obj,
386                           gint width,
387                           gint height,
388                           GError **error)
389 {
390   dbus_int32_t d_width = width, d_height = height;
391   dbus_bool_t ret = FALSE;
392
393   g_return_val_if_fail (obj != NULL, FALSE);
394
395   _atspi_dbus_call (obj, atspi_interface_component, "SetSize", error, "ii=>b",
396                     d_width, d_height, &ret);
397
398   return ret;
399 }
400
401 static void
402 atspi_component_base_init (AtspiComponent *klass)
403 {
404 }
405
406 GType
407 atspi_component_get_type (void)
408 {
409   static GType type = 0;
410
411   if (!type) {
412     static const GTypeInfo tinfo =
413     {
414       sizeof (AtspiComponent),
415       (GBaseInitFunc) atspi_component_base_init,
416       (GBaseFinalizeFunc) NULL,
417     };
418
419     type = g_type_register_static (G_TYPE_INTERFACE, "AtspiComponent", &tinfo, 0);
420
421   }
422   return type;
423 }