1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/*
* st-focus-manager.c: Keyboard focus manager
*
* Copyright 2010 Red Hat, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* SECTION:st-focus-manager
* @short_description: Keyboard focus management
*
* #StFocusManager handles keyboard focus for all actors on the stage.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <clutter/clutter.h>
#include "st-focus-manager.h"
struct _StFocusManagerPrivate
{
GHashTable *groups;
};
G_DEFINE_TYPE_WITH_PRIVATE (StFocusManager, st_focus_manager, G_TYPE_OBJECT)
static void
st_focus_manager_dispose (GObject *object)
{
StFocusManager *manager = ST_FOCUS_MANAGER (object);
if (manager->priv->groups)
{
g_hash_table_destroy (manager->priv->groups);
manager->priv->groups = NULL;
}
G_OBJECT_CLASS (st_focus_manager_parent_class)->dispose (object);
}
static void
st_focus_manager_class_init (StFocusManagerClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->dispose = st_focus_manager_dispose;
}
static void
st_focus_manager_init (StFocusManager *manager)
{
manager->priv = st_focus_manager_get_instance_private (manager);
manager->priv->groups = g_hash_table_new (NULL, NULL);
}
static gboolean
st_focus_manager_stage_event (ClutterActor *stage,
ClutterEvent *event,
gpointer user_data)
{
StFocusManager *manager = user_data;
StDirectionType direction;
gboolean wrap_around = FALSE;
ClutterActor *focused, *group;
if (event->type != CLUTTER_KEY_PRESS)
return FALSE;
switch (event->key.keyval)
{
case CLUTTER_KEY_Up:
direction = ST_DIR_UP;
break;
case CLUTTER_KEY_Down:
direction = ST_DIR_DOWN;
break;
case CLUTTER_KEY_Left:
direction = ST_DIR_LEFT;
break;
case CLUTTER_KEY_Right:
direction = ST_DIR_RIGHT;
break;
case CLUTTER_KEY_Tab:
if (event->key.modifier_state & CLUTTER_SHIFT_MASK)
direction = ST_DIR_TAB_BACKWARD;
else
direction = ST_DIR_TAB_FORWARD;
wrap_around = TRUE;
break;
case CLUTTER_KEY_ISO_Left_Tab:
direction = ST_DIR_TAB_BACKWARD;
wrap_around = TRUE;
break;
default:
return FALSE;
}
focused = clutter_stage_get_key_focus (CLUTTER_STAGE (stage));
if (!focused)
return FALSE;
for (group = focused; group != stage; group = clutter_actor_get_parent (group))
{
if (g_hash_table_lookup (manager->priv->groups, group))
{
return st_widget_navigate_focus (ST_WIDGET (group), focused,
direction, wrap_around);
}
}
return FALSE;
}
/**
* st_focus_manager_get_for_stage:
* @stage: a #ClutterStage
*
* Gets the #StFocusManager for @stage, creating it if necessary.
*
* Returns: (transfer none): the focus manager for @stage
*/
StFocusManager *
st_focus_manager_get_for_stage (ClutterStage *stage)
{
StFocusManager *manager;
manager = g_object_get_data (G_OBJECT (stage), "st-focus-manager");
if (!manager)
{
manager = g_object_new (ST_TYPE_FOCUS_MANAGER, NULL);
g_object_set_data_full (G_OBJECT (stage), "st-focus-manager",
manager, g_object_unref);
g_signal_connect (stage, "event",
G_CALLBACK (st_focus_manager_stage_event), manager);
}
return manager;
}
static void
remove_destroyed_group (ClutterActor *actor,
gpointer user_data)
{
StFocusManager *manager = user_data;
st_focus_manager_remove_group (manager, ST_WIDGET (actor));
}
/**
* st_focus_manager_add_group:
* @manager: the #StFocusManager
* @root: the root container of the group
*
* Adds a new focus group to @manager. When the focus is in an actor
* that is a descendant of @root, @manager will handle moving focus
* from one actor to another within @root based on keyboard events.
*/
void
st_focus_manager_add_group (StFocusManager *manager,
StWidget *root)
{
gpointer count_p = g_hash_table_lookup (manager->priv->groups, root);
int count = count_p ? GPOINTER_TO_INT (count_p) : 0;
g_signal_connect (root, "destroy",
G_CALLBACK (remove_destroyed_group),
manager);
g_hash_table_insert (manager->priv->groups, root, GINT_TO_POINTER (++count));
}
/**
* st_focus_manager_remove_group:
* @manager: the #StFocusManager
* @root: the root container of the group
*
* Removes the group rooted at @root from @manager
*/
void
st_focus_manager_remove_group (StFocusManager *manager,
StWidget *root)
{
gpointer count_p = g_hash_table_lookup (manager->priv->groups, root);
int count = count_p ? GPOINTER_TO_INT (count_p) : 0;
if (count == 0)
return;
if (count == 1)
g_hash_table_remove (manager->priv->groups, root);
else
g_hash_table_insert (manager->priv->groups, root, GINT_TO_POINTER(--count));
}
/**
* st_focus_manager_get_group:
* @manager: the #StFocusManager
* @widget: an #StWidget
*
* Checks if @widget is inside a focus group, and if so, returns
* the root of that group.
*
* Returns: (transfer none): the focus group root, or %NULL if
* @widget is not in a focus group
*/
StWidget *
st_focus_manager_get_group (StFocusManager *manager,
StWidget *widget)
{
ClutterActor *actor = CLUTTER_ACTOR (widget);
while (actor && !g_hash_table_lookup (manager->priv->groups, actor))
actor = clutter_actor_get_parent (actor);
return ST_WIDGET (actor);
}
/**
* st_focus_manager_navigate_from_event:
* @manager: the #StFocusManager
* @event: a #ClutterEvent
*
* Try to navigate from @event as if it bubbled all the way up to
* the stage. This is useful in complex event handling situations
* where you want key navigation, but a parent might be stopping
* the key navigation event from bubbling all the way up to the stage.
*
* Returns: Whether a new actor was navigated to
*/
gboolean
st_focus_manager_navigate_from_event (StFocusManager *manager,
ClutterEvent *event)
{
ClutterActor *stage;
if (event->type != CLUTTER_KEY_PRESS)
return FALSE;
stage = CLUTTER_ACTOR (event->key.stage);
return st_focus_manager_stage_event (stage, event, manager);
}
|