Initial revision
[platform/upstream/atk.git] / atk / atkutil.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 #include "atkutil.h"
21
22 /*
23  * This file supports the addition and removal of multiple focus handlers
24  * as long as they are all called in the same thread.
25  */
26 static AtkFocusTrackerInit  focus_tracker_init = NULL;
27
28 static gboolean init_done = FALSE;
29
30 /*
31  * Array of FocusTracker structs
32  */
33 static GArray *trackers = NULL;
34 static guint  index = 0;
35
36 struct _FocusTracker {
37   guint index;
38   AtkFocusTracker func;
39 };
40 typedef struct _FocusTracker FocusTracker;
41   
42 void
43 atk_focus_tracker_init (AtkFocusTrackerInit    init)
44 {
45   if (focus_tracker_init == NULL)
46     focus_tracker_init = init;
47 }
48
49 guint
50 atk_add_focus_tracker (AtkFocusTracker   focus_tracker)
51 {
52   g_return_val_if_fail ((focus_tracker != NULL), 0);
53
54   if (!init_done)
55   {
56     if (focus_tracker_init != NULL)
57     {
58       focus_tracker_init ();
59     }
60     trackers = g_array_sized_new (FALSE, TRUE, sizeof (FocusTracker), 0);
61     init_done = TRUE;
62   }
63   if (init_done)
64   {
65     FocusTracker item;
66
67     item.index = ++index;
68     item.func = focus_tracker;
69     trackers = g_array_append_val (trackers, item); 
70     return index;
71   }
72   else
73   {
74     return 0;
75   }
76 }
77
78 void
79 atk_remove_focus_tracker (guint            tracker_id)
80 {
81   FocusTracker *item;
82   guint i;
83
84   if (trackers == NULL)
85     return;
86
87   if (tracker_id == 0)
88     return;
89
90   for (i = 0; i < trackers->len; i++)
91   {
92     item = &g_array_index (trackers, FocusTracker, i);
93     if (item->index == tracker_id)
94     {
95       trackers = g_array_remove_index (trackers, i);
96       break;
97     }
98   }
99 }
100
101 void
102 atk_focus_tracker_notify (AtkObject       *object)
103 {
104   FocusTracker *item;
105   guint i;
106
107   if (trackers == NULL)
108     return;
109
110   for (i = 0; i < trackers->len; i++)
111   {
112     item = &g_array_index (trackers, FocusTracker, i);
113     g_return_if_fail (item != NULL);
114     item->func (object);
115   }
116 }