Merge tag 'upstream/2.34.0' into tizen
[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 Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 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  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, 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  * The returned values are meaningful only if the Component has both
134  * STATE_VISIBLE and STATE_SHOWING.
135  *
136  * Returns: An #AtspiRect giving the accessible's extents.
137  **/
138 AtspiRect *
139 atspi_component_get_extents (AtspiComponent *obj,
140                                 AtspiCoordType ctype, GError **error)
141 {
142   dbus_uint32_t d_ctype = ctype;
143   AtspiRect bbox;
144   AtspiAccessible *accessible;
145
146   bbox.x = bbox.y = bbox.width = bbox.height = -1;
147   g_return_val_if_fail (obj != NULL, atspi_rect_copy (&bbox));
148
149   accessible = ATSPI_ACCESSIBLE (obj);
150   if (accessible->priv->cache && ctype == ATSPI_COORD_TYPE_SCREEN)
151   {
152     GValue *val = g_hash_table_lookup (accessible->priv->cache, "Component.ScreenExtents");
153     if (val)
154     {
155       return g_value_dup_boxed (val);
156     }
157   }
158
159   _atspi_dbus_call (obj, atspi_interface_component, "GetExtents", error, "u=>(iiii)", d_ctype, &bbox);
160   return atspi_rect_copy (&bbox);
161 }
162
163 /**
164  * atspi_component_get_position:
165  * @obj: a pointer to the #AtspiComponent to query.
166  * @ctype: the desired coordinate system into which to return the results,
167  *         (e.g. ATSPI_COORD_TYPE_WINDOW, ATSPI_COORD_TYPE_SCREEN).
168  *
169  * Gets the minimum x and y coordinates of the specified #AtspiComponent.
170  * The returned values are meaningful only if the Component has both
171  * STATE_VISIBLE and STATE_SHOWING.
172  *
173  * returns: An #AtspiPoint giving the @obj's position.
174  **/
175 AtspiPoint *
176 atspi_component_get_position (AtspiComponent *obj,
177                                  AtspiCoordType ctype, GError **error)
178 {
179   dbus_int32_t d_x, d_y;
180   dbus_uint32_t d_ctype = ctype;
181   AtspiPoint ret;
182
183   ret.x = ret.y = -1;
184
185   if (!obj)
186     return atspi_point_copy (&ret);
187
188   _atspi_dbus_call (obj, atspi_interface_component, "GetPosition", error, "u=>ii", d_ctype, &d_x, &d_y);
189
190   ret.x = d_x;
191   ret.y = d_y;
192   return atspi_point_copy (&ret);
193 }
194
195 /**
196  * atspi_component_get_size:
197  * @obj: a pointer to the #AtspiComponent to query.
198  *
199  * Gets the size of the specified #AtspiComponent.
200  * The returned values are meaningful only if the Component has both
201  * STATE_VISIBLE and STATE_SHOWING.
202  *
203  * returns: An #AtspiPoint giving the @obj's size.
204  **/
205 AtspiPoint *
206 atspi_component_get_size (AtspiComponent *obj, GError **error)
207 {
208   dbus_int32_t d_w, d_h;
209   AtspiPoint ret;
210
211   ret.x = ret.y = -1;
212   if (!obj)
213     return atspi_point_copy (&ret);
214
215   _atspi_dbus_call (obj, atspi_interface_component, "GetSize", error, "=>ii", &d_w, &d_h);
216   ret.x = d_w;
217   ret.y = d_h;
218   return atspi_point_copy (&ret);
219 }
220
221 /**
222  * atspi_component_get_layer:
223  * @obj: a pointer to the #AtspiComponent to query.
224  *
225  * Queries which layer the component is painted into, to help determine its
226  *      visibility in terms of stacking order.
227  *
228  * Returns: the #AtspiComponentLayer into which this component is painted.
229  **/
230 AtspiComponentLayer
231 atspi_component_get_layer (AtspiComponent *obj, GError **error)
232 {
233   dbus_uint32_t zlayer = -1;
234
235   _atspi_dbus_call (obj, atspi_interface_component, "GetLayer", error, "=>u", &zlayer);
236
237   return zlayer;
238 }
239
240 /**
241  * atspi_component_get_mdi_z_order:
242  * @obj: a pointer to the #AtspiComponent to query.
243  *
244  * Queries the z stacking order of a component which is in the MDI or window
245  *       layer. (Bigger z-order numbers mean nearer the top)
246  *
247  * Returns: a #gshort indicating the stacking order of the component
248  *       in the MDI layer, or -1 if the component is not in the MDI layer.
249  **/
250 gshort
251 atspi_component_get_mdi_z_order (AtspiComponent *obj, GError **error)
252 {
253   dbus_uint16_t retval = -1;
254
255   _atspi_dbus_call (obj, atspi_interface_component, "GetMDIZOrder", error, "=>n", &retval);
256
257   return retval;
258 }
259
260 /**
261  * atspi_component_grab_focus:
262  * @obj: a pointer to the #AtspiComponent on which to operate.
263  *
264  * Attempts to set the keyboard input focus to the specified
265  *         #AtspiComponent.
266  *
267  * Returns: #TRUE if successful, #FALSE otherwise.
268  *
269  **/
270 gboolean
271 atspi_component_grab_focus (AtspiComponent *obj, GError **error)
272 {
273   dbus_bool_t retval = FALSE;
274
275   _atspi_dbus_call (obj, atspi_interface_component, "GrabFocus", error, "=>b", &retval);
276
277   return retval;
278 }
279
280 /**
281  * atspi_component_grab_highlight:
282  * @obj: a pointer to the #AtspiComponent on which to operate.
283  *
284  * Attempts to set highlight to the specified
285  *         #AtspiComponent.
286  *
287  * Returns: #TRUE if successful, #FALSE otherwise.
288  *
289  **/
290 gboolean
291 atspi_component_grab_highlight (AtspiComponent *obj, GError **error)
292 {
293   dbus_bool_t retval = FALSE;
294
295   _atspi_dbus_call (obj, atspi_interface_component, "GrabHighlight", error, "=>b", &retval);
296
297   return retval;
298 }
299
300 /**
301  * atspi_component_clear_highlight:
302  * @obj: a pointer to the #AtspiComponent on which to operate.
303  *
304  * Attempts to clear highlight on the specified
305  *         #AtspiComponent.
306  *
307  * Returns: #TRUE if successful, #FALSE otherwise.
308  *
309  **/
310 gboolean
311 atspi_component_clear_highlight (AtspiComponent *obj, GError **error)
312 {
313   dbus_bool_t retval = FALSE;
314
315   _atspi_dbus_call (obj, atspi_interface_component, "ClearHighlight", error, "=>b", &retval);
316
317   return retval;
318 }
319
320 /**
321  * atspi_component_get_alpha:
322  * @obj: The #AtspiComponent to be queried.
323  *
324  * Gets the opacity/alpha value of a component, if alpha blending is in use.
325  *
326  * Returns: the opacity value of a component, as a #gdouble between 0.0 and 1.0.
327  **/
328 gdouble
329 atspi_component_get_alpha    (AtspiComponent *obj, GError **error)
330 {
331   double retval = 1;
332
333   _atspi_dbus_call (obj, atspi_interface_component, "GetAlpha", error, "=>d", &retval);
334
335   return retval;
336 }
337
338 /**
339  * atspi_component_set_extents:
340  * @obj: a pointer to the #AtspiComponent to move.
341  * @x: the new vertical position to which the component should be moved.
342  * @y: the new horizontal position to which the component should be moved.
343  * @width: the width to which the component should be resized.
344  * @height: the height to which the component should be resized.
345  * @ctype: the coordinate system in which the position is specified.
346  *         (e.g. ATSPI_COORD_TYPE_WINDOW, ATSPI_COORD_TYPE_SCREEN).
347  *
348  * Moves and resizes the specified component.
349  *
350  * Returns: #TRUE if successful; #FALSE otherwise.
351  **/
352 gboolean
353 atspi_component_set_extents (AtspiComponent *obj,
354                              gint x,
355                              gint y,
356                              gint width,
357                              gint height,
358                              AtspiCoordType ctype,
359                              GError **error)
360 {
361   dbus_int32_t d_x = x, d_y = y, d_width = width, d_height = height;
362   dbus_uint32_t d_ctype = ctype;
363   DBusMessageIter iter, iter_struct;
364   DBusMessage *message, *reply;
365   dbus_bool_t retval = FALSE;
366   AtspiAccessible *aobj = ATSPI_ACCESSIBLE (obj);
367
368   g_return_val_if_fail (obj != NULL, FALSE);
369
370   if (!aobj->parent.app || !aobj->parent.app->bus_name)
371   {
372     g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_APPLICATION_GONE,
373                           _("The application no longer exists"));
374     return FALSE;
375   }
376
377   message = dbus_message_new_method_call (aobj->parent.app->bus_name,
378                                           aobj->parent.path,
379                                           atspi_interface_component,
380                                           "SetExtents");
381   if (!message)
382     return FALSE;
383
384   dbus_message_iter_init_append (message, &iter);
385   if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_STRUCT, NULL, &iter_struct))
386   {
387     dbus_message_unref (message);
388     return FALSE;
389   }
390   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_INT32, &d_x);
391   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_INT32, &d_y);
392   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_INT32, &d_width);
393   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_INT32, &d_height);
394   dbus_message_iter_close_container (&iter, &iter_struct);
395   dbus_message_iter_append_basic (&iter, DBUS_TYPE_UINT32, &d_ctype);
396
397   reply = _atspi_dbus_send_with_reply_and_block (message, error);
398   dbus_message_get_args (reply, NULL, DBUS_TYPE_BOOLEAN, &retval,
399                               DBUS_TYPE_INVALID);
400   dbus_message_unref (reply);
401   return retval;
402 }
403
404 /**
405  * atspi_component_set_position:
406  * @obj: a pointer to the #AtspiComponent to move.
407  * @x: the new vertical position to which the component should be moved.
408  * @y: the new horizontal position to which the component should be moved.
409  * @ctype: the coordinate system in which the position is specified.
410  *         (e.g. ATSPI_COORD_TYPE_WINDOW, ATSPI_COORD_TYPE_SCREEN).
411  *
412  * Moves the component to the specified position.
413  *
414  * Returns: #TRUE if successful; #FALSE otherwise.
415  **/
416 gboolean
417 atspi_component_set_position (AtspiComponent *obj,
418                               gint x,
419                               gint y,
420                               AtspiCoordType ctype,
421                               GError **error)
422 {
423   dbus_int32_t d_x = x, d_y = y;
424   dbus_uint32_t d_ctype = ctype;
425   dbus_bool_t ret = FALSE;
426
427   g_return_val_if_fail (obj != NULL, FALSE);
428
429   _atspi_dbus_call (obj, atspi_interface_component, "SetPosition", error,
430                     "iiu=>b", d_x, d_y, d_ctype, &ret);
431
432   return ret;
433 }
434
435 /**
436  * atspi_component_set_size:
437  * @obj: a pointer to the #AtspiComponent to query.
438  * @width: the width to which the component should be resized.
439  * @height: the height to which the component should be resized.
440  *
441  * Resizes the specified component to the given coordinates.
442  *
443  * Returns: #TRUE if successful; #FALSE otherwise.
444  **/
445 gboolean
446 atspi_component_set_size (AtspiComponent *obj,
447                           gint width,
448                           gint height,
449                           GError **error)
450 {
451   dbus_int32_t d_width = width, d_height = height;
452   dbus_bool_t ret = FALSE;
453
454   g_return_val_if_fail (obj != NULL, FALSE);
455
456   _atspi_dbus_call (obj, atspi_interface_component, "SetSize", error, "ii=>b",
457                     d_width, d_height, &ret);
458
459   return ret;
460 }
461
462 /**
463 <<<<<<< HEAD
464  * atspi_component_scroll_to:
465  * @obj: a pointer to the #AtspiComponent object on which to operate.
466  * @type: a #AtspiScrollType indicating where the object should be placed on the
467  *        screen.
468  *
469  * Scrolls whatever container of the #AtspiComponent object so it becomes
470  * visible on the screen.
471  *
472  * Returns: #TRUE if successful, #FALSE otherwise.
473  **/
474 gboolean
475 atspi_component_scroll_to (AtspiComponent *obj,
476                            AtspiScrollType type,
477                            GError **error)
478 {
479   dbus_bool_t retval = FALSE;
480
481   g_return_val_if_fail (obj != NULL, FALSE);
482
483   _atspi_dbus_call (obj, atspi_interface_component,
484                     "ScrollTo", error, "u=>b", type, &retval);
485
486   return retval;
487 }
488
489 /**
490  * atspi_component_scroll_to_point:
491  * @obj: a pointer to the #AtspiComponent object on which to operate.
492  * @coords: a #AtspiCoordType indicating whether the coordinates are relative to
493  *          the screen, to the window, or to the parent object.
494  * @x: the x coordinate of the point to reach
495  * @y: the y coordinate of the point to reach
496  * @error: return location for a #GError
497  *
498  * Scrolls whatever container of the #AtspiComponent object so it becomes
499  * visible on the screen at a given position.
500  *
501  * Returns: #TRUE if successful, #FALSE otherwise.
502  **/
503 gboolean
504 atspi_component_scroll_to_point (AtspiComponent *obj,
505                                  AtspiCoordType coords,
506                                  gint x,
507                                  gint y,
508                                  GError **error)
509 {
510   dbus_bool_t retval = FALSE;
511
512   g_return_val_if_fail (obj != NULL, FALSE);
513
514   _atspi_dbus_call (obj, atspi_interface_component,
515                     "ScrollToPoint", error, "uii=>b", coords, x, y, &retval);
516
517   return retval;
518 }
519
520 /**
521  * atspi_component_get_highlight_index
522  * @obj: a pointer to the #AtspiComponent to query.
523  *
524  * Returns: highlight index of object if (>0), 0 if highlight index is not set
525  *          or -1 if an error occured.
526  **/
527 int
528 atspi_component_get_highlight_index (AtspiComponent *obj, GError **error)
529 {
530    gint ret = -1;
531    g_return_val_if_fail (obj != NULL, -1);
532    _atspi_dbus_get_property (obj, atspi_interface_component,
533                              "HighlightIndex", error, "i", &ret);
534    return ret;
535 }
536
537 static void
538 atspi_component_base_init (AtspiComponent *klass)
539 {
540 }
541
542 GType
543 atspi_component_get_type (void)
544 {
545   static GType type = 0;
546
547   if (!type) {
548     static const GTypeInfo tinfo =
549     {
550       sizeof (AtspiComponent),
551       (GBaseInitFunc) atspi_component_base_init,
552       (GBaseFinalizeFunc) NULL,
553     };
554
555     type = g_type_register_static (G_TYPE_INTERFACE, "AtspiComponent", &tinfo, 0);
556
557   }
558   return type;
559 }