grab_highlight callback added to the AtkComponentInterface
[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_set_extents:
495  * @component: an #AtkComponent
496  * @x: x coordinate
497  * @y: y coordinate
498  * @width: width to set for @component
499  * @height: height to set for @component
500  * @coord_type: specifies whether the coordinates are relative to the screen
501  * or to the components top level window
502  *
503  * Sets the extents of @component.
504  *
505  * Returns: %TRUE or %FALSE whether the extents were set or not
506  **/
507 gboolean
508 atk_component_set_extents   (AtkComponent    *component,
509                              gint            x,
510                              gint            y,
511                              gint            width,
512                              gint            height,
513                              AtkCoordType    coord_type)
514 {
515   AtkComponentIface *iface = NULL;
516   g_return_val_if_fail (ATK_IS_COMPONENT (component), FALSE);
517
518   iface = ATK_COMPONENT_GET_IFACE (component);
519
520   if (iface->set_extents)
521     return (iface->set_extents) (component, x, y, width, height, coord_type);
522   else
523     return FALSE;
524 }
525
526 /**
527  * atk_component_set_position:
528  * @component: an #AtkComponent
529  * @x: x coordinate
530  * @y: y coordinate
531  * @coord_type: specifies whether the coordinates are relative to the screen
532  * or to the components top level window
533  *
534  * Sets the postition of @component.
535  * 
536  * Returns: %TRUE or %FALSE whether or not the position was set or not
537  **/
538 gboolean
539 atk_component_set_position   (AtkComponent    *component,
540                               gint            x,
541                               gint            y,
542                               AtkCoordType    coord_type)
543 {
544   AtkComponentIface *iface = NULL;
545   g_return_val_if_fail (ATK_IS_COMPONENT (component), FALSE);
546
547   iface = ATK_COMPONENT_GET_IFACE (component);
548
549   if (iface->set_position)
550     return (iface->set_position) (component, x, y, coord_type);
551   else
552     return FALSE;
553 }
554
555 /**
556  * atk_component_set_size:
557  * @component: an #AtkComponent
558  * @width: width to set for @component
559  * @height: height to set for @component
560  *
561  * Set the size of the @component in terms of width and height.
562  *
563  * Returns: %TRUE or %FALSE whether the size was set or not
564  **/
565 gboolean
566 atk_component_set_size       (AtkComponent    *component,
567                               gint            x,
568                               gint            y)
569 {
570   AtkComponentIface *iface = NULL;
571   g_return_val_if_fail (ATK_IS_COMPONENT (component), FALSE);
572
573   iface = ATK_COMPONENT_GET_IFACE (component);
574
575   if (iface->set_size)
576     return (iface->set_size) (component, x, y);
577   else
578     return FALSE;
579 }
580
581 static gboolean
582 atk_component_real_contains (AtkComponent *component,
583                              gint         x,
584                              gint         y,
585                              AtkCoordType coord_type)
586 {
587   gint real_x, real_y, width, height;
588
589   real_x = real_y = width = height = 0;
590
591   atk_component_get_extents (component, &real_x, &real_y, &width, &height, coord_type);
592
593   if ((x >= real_x) &&
594       (x < real_x + width) &&
595       (y >= real_y) &&
596       (y < real_y + height))
597     return TRUE;
598   else
599     return FALSE;
600 }
601
602 static AtkObject* 
603 atk_component_real_ref_accessible_at_point (AtkComponent *component,
604                                             gint         x,
605                                             gint         y,
606                                             AtkCoordType coord_type)
607 {
608   gint count, i;
609
610   count = atk_object_get_n_accessible_children (ATK_OBJECT (component));
611
612   for (i = 0; i < count; i++)
613   {
614     AtkObject *obj;
615
616     obj = atk_object_ref_accessible_child (ATK_OBJECT (component), i);
617
618     if (obj != NULL)
619     {
620       if (atk_component_contains (ATK_COMPONENT (obj), x, y, coord_type))
621       {
622         return obj;
623       }
624       else
625       {
626         g_object_unref (obj);
627       }
628     }
629   }
630   return NULL;
631 }
632
633 static void
634 atk_component_real_get_position (AtkComponent *component,
635                                  gint         *x,
636                                  gint         *y,
637                                  AtkCoordType coord_type)
638 {
639   gint width, height;
640
641   atk_component_get_extents (component, x, y, &width, &height, coord_type);
642 }
643
644 static void
645 atk_component_real_get_size (AtkComponent *component,
646                              gint         *width,
647                              gint         *height)
648 {
649   gint x, y;
650   AtkCoordType coord_type;
651
652   /*
653    * Pick one coordinate type; it does not matter for size
654    */
655   coord_type = ATK_XY_WINDOW;
656
657   atk_component_get_extents (component, &x, &y, width, height, coord_type);
658 }
659
660 static AtkRectangle *
661 atk_rectangle_copy (const AtkRectangle *rectangle)
662 {
663   AtkRectangle *result = g_new (AtkRectangle, 1);
664   *result = *rectangle;
665
666   return result;
667 }
668
669 GType
670 atk_rectangle_get_type (void)
671 {
672   static GType our_type = 0;
673
674   if (our_type == 0)
675     our_type = g_boxed_type_register_static ("AtkRectangle",
676                                              (GBoxedCopyFunc)atk_rectangle_copy,
677                                              (GBoxedFreeFunc)g_free);
678   return our_type;
679 }
680