]> Pileus Git - ~andy/gtk/blob - modules/other/gail/tests/testvalues.c
611401966debc6bf253db538d87be287a593a2b9
[~andy/gtk] / modules / other / gail / tests / testvalues.c
1 #include <string.h>
2 #include <atk/atk.h>
3 #include <gtk/gtk.h>
4
5 static void _traverse_children (AtkObject *obj);
6 static void _add_handler (AtkObject *obj);
7 static void _check_values (AtkObject *obj);
8 static void _value_change_handler (AtkObject   *obj,
9                                       AtkPropertyValues *values);
10
11 static guint id;
12
13 static void _value_change_handler (AtkObject   *obj,
14                                    AtkPropertyValues   *values)
15 {
16   G_CONST_RETURN gchar *type_name = g_type_name (G_TYPE_FROM_INSTANCE (obj));
17    GValue *value_back, val;
18
19   value_back = &val;
20     
21   if (!ATK_IS_VALUE (obj)) { 
22         return;
23   }
24
25   if (strcmp (values->property_name, "accessible-value") == 0) {
26         g_print ("_value_change_handler: Accessible Type: %s\n",
27            type_name ? type_name : "NULL");
28         if(G_VALUE_HOLDS_DOUBLE (&values->new_value))
29     {
30                 g_print( "adjustment value changed : new value: %f\n", 
31                 g_value_get_double (&values->new_value));
32         }
33
34         g_print("Now calling the AtkValue interface functions\n");
35
36         atk_value_get_current_value (ATK_VALUE(obj), value_back);
37         g_return_if_fail (G_VALUE_HOLDS_DOUBLE (value_back));
38         g_print ("atk_value_get_current_value returns %f\n",
39                         g_value_get_double (value_back) );
40
41         atk_value_get_maximum_value (ATK_VALUE (obj), value_back);
42         g_return_if_fail (G_VALUE_HOLDS_DOUBLE (value_back));
43         g_print ("atk_value_get_maximum returns %f\n",
44                         g_value_get_double (value_back));
45
46         atk_value_get_minimum_value (ATK_VALUE (obj), value_back);
47         g_return_if_fail (G_VALUE_HOLDS_DOUBLE (value_back));
48         g_print ("atk_value_get_minimum returns %f\n", 
49                         g_value_get_double (value_back));
50         
51  
52     }
53   
54 }
55
56 static void _traverse_children (AtkObject *obj)
57 {
58   gint n_children, i;
59
60   n_children = atk_object_get_n_accessible_children (obj);
61   for (i = 0; i < n_children; i++)
62   {
63     AtkObject *child;
64
65     child = atk_object_ref_accessible_child (obj, i);
66     _add_handler (child);
67     _traverse_children (child);
68     g_object_unref (G_OBJECT (child));
69   }
70 }
71
72 static void _add_handler (AtkObject *obj)
73 {
74   static GPtrArray *obj_array = NULL;
75   gboolean found = FALSE;
76   gint i;
77
78   /*
79    * We create a property handler for each object if one was not associated 
80    * with it already.
81    *
82    * We add it to our array of objects which have property handlers; if an
83    * object is destroyed it remains in the array.
84    */
85   if (obj_array == NULL)
86     obj_array = g_ptr_array_new ();
87  
88   for (i = 0; i < obj_array->len; i++)
89   {
90     if (obj == g_ptr_array_index (obj_array, i))
91     {
92       found = TRUE;
93       break;
94     }
95   }
96   if (!found)
97   {
98     atk_object_connect_property_change_handler (obj,
99                    (AtkPropertyChangeHandler*) _value_change_handler);
100     g_ptr_array_add (obj_array, obj);
101   }
102 }
103
104 static void _set_values (AtkObject *obj) {
105
106   GValue *value_back, val;
107   static gint count = 0;
108   gdouble double_value;
109
110   value_back = &val;
111
112   if(ATK_IS_VALUE(obj)) {
113         /* Spin button also inherits the text interfaces from GailEntry. 
114          * Check when spin button recieves focus.
115      */
116
117         if(ATK_IS_TEXT(obj) && ATK_IS_EDITABLE_TEXT(obj)) {
118                 if(count == 0) {        
119                         gint x;
120                         gchar* text;
121                         count++;
122                         x = atk_text_get_character_count (ATK_TEXT (obj));
123                         text = atk_text_get_text (ATK_TEXT (obj), 0, x);
124                         g_print("Text : %s\n", text);
125                         text = "5.7";
126                         atk_editable_text_set_text_contents(ATK_EDITABLE_TEXT(obj),text);
127                         g_print("Set text to %s\n",text);
128                         atk_value_get_current_value(ATK_VALUE(obj), value_back);
129                         g_return_if_fail (G_VALUE_HOLDS_DOUBLE (value_back));
130                         g_print("atk_value_get_current_value returns %f\n", 
131                                 g_value_get_double( value_back));
132                         } 
133         } else {
134                 memset (value_back, 0, sizeof (GValue));
135                 g_value_init (value_back, G_TYPE_DOUBLE);
136                 g_value_set_double (value_back, 10.0);  
137                 if (atk_value_set_current_value (ATK_VALUE (obj), value_back))
138                 {
139                         double_value = g_value_get_double (value_back);
140                         g_print("atk_value_set_current_value returns %f\n", 
141                         double_value);
142                 }
143         }
144   }
145 }
146
147 static void _check_values (AtkObject *obj)
148 {
149   static gint calls = 0;
150   AtkRole role;
151
152   g_print ("Start of _check_values\n");
153
154   _set_values(obj);
155
156   _add_handler (obj);
157
158   if (++calls < 2)
159   { 
160     /*
161      * Just do this on this on the first 2 objects visited
162      */
163     atk_object_set_name (obj, "test123");
164     atk_object_set_description (obj, "test123");
165   }
166
167   role = atk_object_get_role (obj);
168
169   if (role == ATK_ROLE_FRAME || role == ATK_ROLE_DIALOG)
170   {
171     /*
172      * Add handlers to all children.
173      */
174     _traverse_children (obj);
175   }
176   g_print ("End of _check_values\n");
177 }
178
179 static void
180 _create_event_watcher (void)
181 {
182   id = atk_add_focus_tracker (_check_values);
183 }
184
185 int
186 gtk_module_init(gint argc, char* argv[])
187 {
188   g_print("testvalues Module loaded\n");
189
190   _create_event_watcher();
191
192   return 0;
193 }