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