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