]> Pileus Git - ~andy/gtk/blob - tests/a11y/testfocus.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / tests / a11y / testfocus.c
1 #include <gtk/gtk.h>
2
3 static const gchar *
4 get_name (gpointer obj)
5 {
6   GtkWidget *widget;
7   if (obj == NULL)
8     return "(nil)";
9   else if (GTK_IS_WIDGET (obj))
10     widget = GTK_WIDGET (obj);
11   else if (GTK_IS_ACCESSIBLE (obj))
12     widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
13   else
14     return "OOPS";
15   if (GTK_IS_BUILDABLE (widget))
16     return gtk_buildable_get_name (GTK_BUILDABLE (widget));
17   else
18     return G_OBJECT_TYPE_NAME (widget);
19 }
20
21 static gboolean
22 compare_focus (gpointer data)
23 {
24   AtkObject *atk_focus;
25   AtkObject *gtk_focus;
26   GtkWidget *focus_widget;
27   GList *list, *l;
28
29   atk_focus = atk_get_focus_object ();
30
31   focus_widget = NULL;
32   list = gtk_window_list_toplevels ();
33   for (l = list; l; l = l->next)
34     {
35       GtkWindow *w = l->data;
36       if (gtk_window_is_active (w))
37         {
38           focus_widget = gtk_window_get_focus (w);
39           break;
40         }
41     }
42   g_list_free (list);
43
44   if (GTK_IS_WIDGET (focus_widget))
45     gtk_focus = gtk_widget_get_accessible (focus_widget);
46   else
47     gtk_focus = NULL;
48
49   if (gtk_focus != atk_focus)
50     g_print ("gtk focus: %s != atk focus: %s\n",
51              get_name (gtk_focus), get_name (atk_focus));
52
53   return G_SOURCE_CONTINUE;
54 }
55
56 static void
57 notify_cb (GObject *obj, GParamSpec *pspec, gpointer data)
58 {
59   gboolean value;
60
61   if (g_strcmp0 (pspec->name, "has-focus") != 0)
62     return;
63
64   g_object_get (obj, "has-focus", &value, NULL);
65   g_print ("widget %s %p has-focus -> %d\n", get_name (obj), obj, value);
66 }
67
68 static void
69 state_change_cb (AtkObject *obj, const gchar *name, gboolean state_set)
70 {
71   AtkStateSet *set;
72
73   set = atk_object_ref_state_set (obj);
74   g_print ("accessible %s %p focused -> %d\n", get_name (obj), obj,
75            atk_state_set_contains_state (set, ATK_STATE_FOCUSED));
76   g_object_unref (set);
77 }
78
79 int
80 main (int argc, char *argv[])
81 {
82   GtkBuilder *builder;
83   GtkWidget *window;
84   GSList *o, *l;
85   GtkWidget *widget;
86   AtkObject *accessible;
87
88   gtk_init (&argc, &argv);
89
90   builder = gtk_builder_new ();
91   gtk_builder_add_from_file (builder, argv[1], NULL);
92
93   window = (GtkWidget *)gtk_builder_get_object (builder, "window1");
94
95   o = gtk_builder_get_objects (builder);
96   for (l = o; l;l = l->next)
97     {
98        if (!GTK_IS_WIDGET (l->data))
99          continue;
100
101        widget = l->data;
102        g_signal_connect (widget, "notify::has-focus", G_CALLBACK (notify_cb), NULL);
103        accessible = gtk_widget_get_accessible (widget);
104        g_signal_connect (accessible, "state-change::focused", G_CALLBACK (state_change_cb), NULL);
105
106     }
107   g_slist_free (o);
108
109   g_timeout_add (100, compare_focus, NULL);
110
111   gtk_widget_show_all (window);
112
113   gtk_main ();
114
115   return 0;
116 }
117