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