Revert "Merge remote-tracking branch 'origin/sandbox/mniesluchow/upstream_2_16' into...
[platform/upstream/atk.git] / atk / atkcomponent.c
1 /* ATK -  Accessibility Toolkit
2  * Copyright 2001 Sun Microsystems Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20
21 #include "atkcomponent.h"
22
23 /**
24  * SECTION:atkcomponent
25  * @Short_description: The ATK interface provided by UI components
26  * which occupy a physical area on the screen.
27  * which the user can activate/interact with.
28  * @Title:AtkComponent
29  *
30  * #AtkComponent should be implemented by most if not all UI elements
31  * with an actual on-screen presence, i.e. components which can be
32  * said to have a screen-coordinate bounding box.  Virtually all
33  * widgets will need to have #AtkComponent implementations provided
34  * for their corresponding #AtkObject class.  In short, only UI
35  * elements which are *not* GUI elements will omit this ATK interface.
36  *
37  * A possible exception might be textual information with a
38  * transparent background, in which case text glyph bounding box
39  * information is provided by #AtkText.
40  */
41
42 enum {
43   BOUNDS_CHANGED,
44   LAST_SIGNAL
45 };
46
47 static void       atk_component_base_init (AtkComponentIface *class);
48
49 static gboolean   atk_component_real_contains                (AtkComponent *component,
50                                                               gint         x,
51                                                               gint         y,
52                                                               AtkCoordType coord_type);
53
54 static AtkObject* atk_component_real_ref_accessible_at_point (AtkComponent *component,
55                                                               gint         x,
56                                                               gint         y,
57                                                               AtkCoordType coord_type);
58
59 static void      atk_component_real_get_position             (AtkComponent *component,
60                                                               gint         *x,
61                                                               gint         *y,
62                                                               AtkCoordType coord_type);
63
64 static void      atk_component_real_get_size                 (AtkComponent *component,
65                                                               gint         *width,
66                                                               gint         *height);
67
68 static guint atk_component_signals[LAST_SIGNAL] = { 0 };
69
70 GType
71 atk_component_get_type (void)
72 {
73   static GType type = 0;
74
75   if (!type) {
76     static const GTypeInfo tinfo =
77     {
78       sizeof (AtkComponentIface),
79       (GBaseInitFunc) atk_component_base_init,
80       (GBaseFinalizeFunc) NULL,
81
82     };
83
84     type = g_type_register_static (G_TYPE_INTERFACE, "AtkComponent", &tinfo, 0);
85   }
86
87   return type;
88 }
89
90 static void
91 atk_component_base_init (AtkComponentIface *class)
92 {
93   static gboolean initialized = FALSE;
94
95   if (! initialized)
96     {
97       class->ref_accessible_at_point = atk_component_real_ref_accessible_at_point;
98       class->contains = atk_component_real_contains;
99       class->get_position = atk_component_real_get_position;
100       class->get_size = atk_component_real_get_size;
101
102
103       /**
104        * AtkComponent::bounds-changed:
105        * @atkcomponent: the object which received the signal.
106        * @arg1: The AtkRectangle giving the new position and size.
107        *
108        * The 'bounds-changed" signal is emitted when the bposition or
109        * size of the component changes.
110        */
111       atk_component_signals[BOUNDS_CHANGED] =
112         g_signal_new ("bounds_changed",
113                       ATK_TYPE_COMPONENT,
114                       G_SIGNAL_RUN_LAST,
115                       G_STRUCT_OFFSET (AtkComponentIface, bounds_changed),
116                       (GSignalAccumulator) NULL, NULL,
117                       g_cclosure_marshal_VOID__BOXED,
118                       G_TYPE_NONE, 1,
119                       ATK_TYPE_RECTANGLE | G_SIGNAL_TYPE_STATIC_SCOPE);
120
121       initialized = TRUE;
122     }
123 }
124
125
126 /**
127  * atk_component_add_focus_handler:
128  * @component: The #AtkComponent to attach the @handler to
129  * @handler: The #AtkFocusHandler to be attached to @component
130  *
131  * Add the specified handler to the set of functions to be called 
132  * when this object receives focus events (in or out). If the handler is
133  * already added it is not added again
134  *
135  * Deprecated: This method is deprecated since ATK version 2.9.4. If
136  * you need to track when an object gains or lose the focus, use
137  * state-changed:focused notification instead.
138  *
139  * Returns: a handler id which can be used in atk_component_remove_focus_handler()
140  * or zero if the handler was already added.
141  **/
142 guint
143 atk_component_add_focus_handler (AtkComponent    *component,
144                                  AtkFocusHandler handler)
145 {
146   AtkComponentIface *iface = NULL;
147   g_return_val_if_fail (ATK_IS_COMPONENT (component), 0);
148
149   iface = ATK_COMPONENT_GET_IFACE (component);
150
151   if (iface->add_focus_handler)
152     return (iface->add_focus_handler) (component, handler);
153   else
154     return 0;
155 }
156
157 /**
158  * atk_component_remove_focus_handler:
159  * @component: the #AtkComponent to remove the focus handler from
160  * @handler_id: the handler id of the focus handler to be removed
161  * from @component
162  *
163  * Remove the handler specified by @handler_id from the list of
164  * functions to be executed when this object receives focus events 
165  * (in or out).
166  *
167  * Deprecated: This method is deprecated since ATK version 2.9.4. If
168  * you need to track when an object gains or lose the focus, use
169  * state-changed:focused notification instead.
170  *
171  **/
172 void
173 atk_component_remove_focus_handler (AtkComponent    *component,
174                                     guint           handler_id)
175 {
176   AtkComponentIface *iface = NULL;
177   g_return_if_fail (ATK_IS_COMPONENT (component));
178
179   iface = ATK_COMPONENT_GET_IFACE (component);
180
181   if (iface->remove_focus_handler)
182     (iface->remove_focus_handler) (component, handler_id);
183 }
184
185 /**
186  * atk_component_contains:
187  * @component: the #AtkComponent
188  * @x: x coordinate
189  * @y: y coordinate
190  * @coord_type: specifies whether the coordinates are relative to the screen
191  * or to the components top level window
192  *
193  * Checks whether the specified point is within the extent of the @component.
194  *
195  * Toolkit implementor note: ATK provides a default implementation for
196  * this virtual method. In general there are little reason to
197  * re-implement it.
198  *
199  * Returns: %TRUE or %FALSE indicating whether the specified point is within
200  * the extent of the @component or not
201  **/
202 gboolean
203 atk_component_contains (AtkComponent    *component,
204                         gint            x,
205                         gint            y,
206                         AtkCoordType    coord_type)
207 {
208   AtkComponentIface *iface = NULL;
209   g_return_val_if_fail (ATK_IS_COMPONENT (component), FALSE);
210
211   iface = ATK_COMPONENT_GET_IFACE (component);
212
213   if (iface->contains)
214     return (iface->contains) (component, x, y, coord_type);
215   else
216     return FALSE;
217 }
218
219 /**
220  * atk_component_ref_accessible_at_point:
221  * @component: the #AtkComponent
222  * @x: x coordinate
223  * @y: y coordinate
224  * @coord_type: specifies whether the coordinates are relative to the screen
225  * or to the components top level window
226  *
227  * Gets a reference to the accessible child, if one exists, at the
228  * coordinate point specified by @x and @y.
229  *
230  * Returns: (transfer full): a reference to the accessible child, if one exists
231  **/
232 AtkObject*
233 atk_component_ref_accessible_at_point (AtkComponent    *component,
234                                        gint            x,
235                                        gint            y,
236                                        AtkCoordType    coord_type)
237 {
238   AtkComponentIface *iface = NULL;
239   g_return_val_if_fail (ATK_IS_COMPONENT (component), NULL);
240
241   iface = ATK_COMPONENT_GET_IFACE (component);
242
243   if (iface->ref_accessible_at_point)
244     return (iface->ref_accessible_at_point) (component, x, y, coord_type);
245   else
246     return NULL;
247 }
248
249 /**
250  * atk_component_get_extents:
251  * @component: an #AtkComponent
252  * @x: address of #gint to put x coordinate
253  * @y: address of #gint to put y coordinate
254  * @width: address of #gint to put width
255  * @height: address of #gint to put height
256  * @coord_type: specifies whether the coordinates are relative to the screen
257  * or to the components top level window
258  *
259  * Gets the rectangle which gives the extent of the @component.
260  *
261  **/
262 void
263 atk_component_get_extents    (AtkComponent    *component,
264                               gint            *x,
265                               gint            *y,
266                               gint            *width,
267                               gint            *height,
268                               AtkCoordType    coord_type)
269 {
270   AtkComponentIface *iface = NULL;
271   gint local_x, local_y, local_width, local_height;
272   gint *real_x, *real_y, *real_width, *real_height;
273
274   g_return_if_fail (ATK_IS_COMPONENT (component));
275
276   if (x)
277     real_x = x;
278   else
279     real_x = &local_x;
280   if (y)
281     real_y = y;
282   else
283     real_y = &local_y;
284   if (width)
285     real_width = width;
286   else
287     real_width = &local_width;
288   if (height)
289     real_height = height;
290   else
291     real_height = &local_height;
292
293   iface = ATK_COMPONENT_GET_IFACE (component);
294
295   if (iface->get_extents)
296     (iface->get_extents) (component, real_x, real_y, real_width, real_height, coord_type);
297 }
298
299 /**
300  * atk_component_get_position:
301  * @component: an #AtkComponent
302  * @x: address of #gint to put x coordinate position
303  * @y: address of #gint to put y coordinate position
304  * @coord_type: specifies whether the coordinates are relative to the screen
305  * or to the components top level window
306  *
307  * Gets the position of @component in the form of 
308  * a point specifying @component's top-left corner.
309  *
310  * Deprecated: Since 2.12. Use atk_component_get_extents() instead.
311  **/
312 void
313 atk_component_get_position   (AtkComponent    *component,
314                               gint            *x,
315                               gint            *y,
316                               AtkCoordType    coord_type)
317 {
318   AtkComponentIface *iface = NULL;
319   gint local_x, local_y;
320   gint *real_x, *real_y;
321
322   g_return_if_fail (ATK_IS_COMPONENT (component));
323
324   if (x)
325     real_x = x;
326   else
327     real_x = &local_x;
328   if (y)
329     real_y = y;
330   else
331     real_y = &local_y;
332
333   iface = ATK_COMPONENT_GET_IFACE (component);
334
335   if (iface->get_position)
336     (iface->get_position) (component, real_x, real_y, coord_type);
337 }
338
339 /**
340  * atk_component_get_size:
341  * @component: an #AtkComponent
342  * @width: address of #gint to put width of @component
343  * @height: address of #gint to put height of @component
344  *
345  * Gets the size of the @component in terms of width and height.
346  *
347  * Deprecated: Since 2.12. Use atk_component_get_extents() instead.
348  **/
349 void
350 atk_component_get_size       (AtkComponent    *component,
351                               gint            *width,
352                               gint            *height)
353 {
354   AtkComponentIface *iface = NULL;
355   gint local_width, local_height;
356   gint *real_width, *real_height;
357
358   g_return_if_fail (ATK_IS_COMPONENT (component));
359
360   if (width)
361     real_width = width;
362   else
363     real_width = &local_width;
364   if (height)
365     real_height = height;
366   else
367     real_height = &local_height;
368
369   g_return_if_fail (ATK_IS_COMPONENT (component));
370
371   iface = ATK_COMPONENT_GET_IFACE (component);
372
373   if (iface->get_size)
374     (iface->get_size) (component, real_width, real_height);
375 }
376
377 /**
378  * atk_component_get_layer:
379  * @component: an #AtkComponent
380  *
381  * Gets the layer of the component.
382  *
383  * Returns: an #AtkLayer which is the layer of the component
384  **/
385 AtkLayer
386 atk_component_get_layer (AtkComponent *component) 
387 {
388   AtkComponentIface *iface;
389
390   g_return_val_if_fail (ATK_IS_COMPONENT (component), ATK_LAYER_INVALID);
391
392   iface = ATK_COMPONENT_GET_IFACE (component);
393   if (iface->get_layer)
394     return (iface->get_layer) (component);
395   else
396     return ATK_LAYER_WIDGET;
397 }
398
399 /**
400  * atk_component_get_mdi_zorder:
401  * @component: an #AtkComponent
402  *
403  * Gets the zorder of the component. The value G_MININT will be returned 
404  * if the layer of the component is not ATK_LAYER_MDI or ATK_LAYER_WINDOW.
405  *
406  * Returns: a gint which is the zorder of the component, i.e. the depth at 
407  * which the component is shown in relation to other components in the same 
408  * container.
409  **/
410 gint
411 atk_component_get_mdi_zorder (AtkComponent *component) 
412 {
413   AtkComponentIface *iface;
414
415   g_return_val_if_fail (ATK_IS_COMPONENT (component), G_MININT);
416
417   iface = ATK_COMPONENT_GET_IFACE (component);
418   if (iface->get_mdi_zorder)
419     return (iface->get_mdi_zorder) (component);
420   else
421     return G_MININT;
422 }
423
424 /**
425  * atk_component_get_alpha:
426  * @component: an #AtkComponent
427  *
428  * Returns the alpha value (i.e. the opacity) for this
429  * @component, on a scale from 0 (fully transparent) to 1.0
430  * (fully opaque).
431  *
432  * Returns: An alpha value from 0 to 1.0, inclusive.
433  * Since: 1.12
434  **/
435 gdouble
436 atk_component_get_alpha (AtkComponent    *component)
437 {
438   AtkComponentIface *iface;
439
440   g_return_val_if_fail (ATK_IS_COMPONENT (component), G_MININT);
441
442   iface = ATK_COMPONENT_GET_IFACE (component);
443   if (iface->get_alpha)
444     return (iface->get_alpha) (component);
445   else
446     return (gdouble) 1.0;
447 }
448
449 /**
450  * atk_component_grab_focus:
451  * @component: an #AtkComponent
452  *
453  * Grabs focus for this @component.
454  *
455  * Returns: %TRUE if successful, %FALSE otherwise.
456  **/
457 gboolean
458 atk_component_grab_focus (AtkComponent    *component)
459 {
460   AtkComponentIface *iface = NULL;
461   g_return_val_if_fail (ATK_IS_COMPONENT (component), FALSE);
462
463   iface = ATK_COMPONENT_GET_IFACE (component);
464
465   if (iface->grab_focus)
466     return (iface->grab_focus) (component);
467   else
468     return FALSE;
469 }
470
471 /**
472  * atk_component_grab_highlight:
473  * @component: an #AtkComponent
474  *
475  * Grabs highlight for this @component.
476  *
477  * Returns: %TRUE if successful, %FALSE otherwise.
478  **/
479 gboolean
480 atk_component_grab_highlight (AtkComponent    *component)
481 {
482   AtkComponentIface *iface = NULL;
483   g_return_val_if_fail (ATK_IS_COMPONENT (component), FALSE);
484
485   iface = ATK_COMPONENT_GET_IFACE (component);
486
487   if (iface->grab_highlight)
488     return (iface->grab_highlight) (component);
489   else
490     return FALSE;
491 }
492
493 /**
494  * atk_component_clear_highlight:
495  * @component: an #AtkComponent
496  *
497  * Clears highlight for this @component.
498  *
499  * Returns: %TRUE if successful, %FALSE otherwise.
500  **/
501 gboolean
502 atk_component_clear_highlight (AtkComponent    *component)
503 {
504   AtkComponentIface *iface = NULL;
505   g_return_val_if_fail (ATK_IS_COMPONENT (component), FALSE);
506
507   iface = ATK_COMPONENT_GET_IFACE (component);
508
509   if (iface->clear_highlight)
510     return (iface->clear_highlight) (component);
511   else
512     return FALSE;
513 }
514
515 /**
516  * atk_component_get_highlight_index:
517  * @component: an #AtkComponent
518  *
519  * Returns: highlight index of the @component (if >0),
520  * 0 if highlight index is not set or -1 if an error occured.
521  *
522  **/
523 gint
524 atk_component_get_highlight_index (AtkComponent    *component)
525 {
526   AtkComponentIface *iface = NULL;
527   g_return_val_if_fail (ATK_IS_COMPONENT (component), -1);
528
529   iface = ATK_COMPONENT_GET_IFACE (component);
530
531   if (iface->get_highlight_index)
532     return (iface->get_highlight_index) (component);
533   else
534     return -1;
535 }
536
537 /**
538  * atk_component_set_extents:
539  * @component: an #AtkComponent
540  * @x: x coordinate
541  * @y: y coordinate
542  * @width: width to set for @component
543  * @height: height to set for @component
544  * @coord_type: specifies whether the coordinates are relative to the screen
545  * or to the components top level window
546  *
547  * Sets the extents of @component.
548  *
549  * Returns: %TRUE or %FALSE whether the extents were set or not
550  **/
551 gboolean
552 atk_component_set_extents   (AtkComponent    *component,
553                              gint            x,
554                              gint            y,
555                              gint            width,
556                              gint            height,
557                              AtkCoordType    coord_type)
558 {
559   AtkComponentIface *iface = NULL;
560   g_return_val_if_fail (ATK_IS_COMPONENT (component), FALSE);
561
562   iface = ATK_COMPONENT_GET_IFACE (component);
563
564   if (iface->set_extents)
565     return (iface->set_extents) (component, x, y, width, height, coord_type);
566   else
567     return FALSE;
568 }
569
570 /**
571  * atk_component_set_position:
572  * @component: an #AtkComponent
573  * @x: x coordinate
574  * @y: y coordinate
575  * @coord_type: specifies whether the coordinates are relative to the screen
576  * or to the components top level window
577  *
578  * Sets the postition of @component.
579  * 
580  * Returns: %TRUE or %FALSE whether or not the position was set or not
581  **/
582 gboolean
583 atk_component_set_position   (AtkComponent    *component,
584                               gint            x,
585                               gint            y,
586                               AtkCoordType    coord_type)
587 {
588   AtkComponentIface *iface = NULL;
589   g_return_val_if_fail (ATK_IS_COMPONENT (component), FALSE);
590
591   iface = ATK_COMPONENT_GET_IFACE (component);
592
593   if (iface->set_position)
594     return (iface->set_position) (component, x, y, coord_type);
595   else
596     return FALSE;
597 }
598
599 /**
600  * atk_component_set_size:
601  * @component: an #AtkComponent
602  * @width: width to set for @component
603  * @height: height to set for @component
604  *
605  * Set the size of the @component in terms of width and height.
606  *
607  * Returns: %TRUE or %FALSE whether the size was set or not
608  **/
609 gboolean
610 atk_component_set_size       (AtkComponent    *component,
611                               gint            x,
612                               gint            y)
613 {
614   AtkComponentIface *iface = NULL;
615   g_return_val_if_fail (ATK_IS_COMPONENT (component), FALSE);
616
617   iface = ATK_COMPONENT_GET_IFACE (component);
618
619   if (iface->set_size)
620     return (iface->set_size) (component, x, y);
621   else
622     return FALSE;
623 }
624
625 static gboolean
626 atk_component_real_contains (AtkComponent *component,
627                              gint         x,
628                              gint         y,
629                              AtkCoordType coord_type)
630 {
631   gint real_x, real_y, width, height;
632
633   real_x = real_y = width = height = 0;
634
635   atk_component_get_extents (component, &real_x, &real_y, &width, &height, coord_type);
636
637   if ((x >= real_x) &&
638       (x < real_x + width) &&
639       (y >= real_y) &&
640       (y < real_y + height))
641     return TRUE;
642   else
643     return FALSE;
644 }
645
646 static AtkObject* 
647 atk_component_real_ref_accessible_at_point (AtkComponent *component,
648                                             gint         x,
649                                             gint         y,
650                                             AtkCoordType coord_type)
651 {
652   gint count, i;
653
654   count = atk_object_get_n_accessible_children (ATK_OBJECT (component));
655
656   for (i = 0; i < count; i++)
657   {
658     AtkObject *obj;
659
660     obj = atk_object_ref_accessible_child (ATK_OBJECT (component), i);
661
662     if (obj != NULL)
663     {
664       if (atk_component_contains (ATK_COMPONENT (obj), x, y, coord_type))
665       {
666         return obj;
667       }
668       else
669       {
670         g_object_unref (obj);
671       }
672     }
673   }
674   return NULL;
675 }
676
677 static void
678 atk_component_real_get_position (AtkComponent *component,
679                                  gint         *x,
680                                  gint         *y,
681                                  AtkCoordType coord_type)
682 {
683   gint width, height;
684
685   atk_component_get_extents (component, x, y, &width, &height, coord_type);
686 }
687
688 static void
689 atk_component_real_get_size (AtkComponent *component,
690                              gint         *width,
691                              gint         *height)
692 {
693   gint x, y;
694   AtkCoordType coord_type;
695
696   /*
697    * Pick one coordinate type; it does not matter for size
698    */
699   coord_type = ATK_XY_WINDOW;
700
701   atk_component_get_extents (component, &x, &y, width, height, coord_type);
702 }
703
704 static AtkRectangle *
705 atk_rectangle_copy (const AtkRectangle *rectangle)
706 {
707   AtkRectangle *result = g_new (AtkRectangle, 1);
708   *result = *rectangle;
709
710   return result;
711 }
712
713 GType
714 atk_rectangle_get_type (void)
715 {
716   static GType our_type = 0;
717
718   if (our_type == 0)
719     our_type = g_boxed_type_register_static ("AtkRectangle",
720                                              (GBoxedCopyFunc)atk_rectangle_copy,
721                                              (GBoxedFreeFunc)g_free);
722   return our_type;
723 }
724