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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
|
/* GTK - The GIMP Toolkit
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* This library 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 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that 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 library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "gtkwidgetprivate.h"
typedef struct _CompareInfo CompareInfo;
enum Axis {
HORIZONTAL = 0,
VERTICAL = 1
};
struct _CompareInfo
{
GtkWidget *widget;
int x;
int y;
guint reverse : 1;
guint axis : 1;
};
static inline void
get_axis_info (const graphene_rect_t *bounds,
int axis,
int *start,
int *end)
{
if (axis == HORIZONTAL)
{
*start = bounds->origin.x;
*end = bounds->size.width;
}
else if (axis == VERTICAL)
{
*start = bounds->origin.y;
*end = bounds->size.height;
}
else
g_assert(FALSE);
}
/* Utility function, equivalent to g_list_reverse */
static void
reverse_ptr_array (GPtrArray *arr)
{
int i;
for (i = 0; i < arr->len / 2; i ++)
{
void *a = g_ptr_array_index (arr, i);
void *b = g_ptr_array_index (arr, arr->len - 1 - i);
arr->pdata[i] = b;
arr->pdata[arr->len - 1 - i] = a;
}
}
static int
tab_sort_func (gconstpointer a,
gconstpointer b,
gpointer user_data)
{
graphene_rect_t child_bounds1, child_bounds2;
GtkWidget *child1 = *((GtkWidget **)a);
GtkWidget *child2 = *((GtkWidget **)b);
GtkTextDirection text_direction = GPOINTER_TO_INT (user_data);
float y1, y2;
if (!gtk_widget_compute_bounds (child1, gtk_widget_get_parent (child1), &child_bounds1) ||
!gtk_widget_compute_bounds (child2, gtk_widget_get_parent (child2), &child_bounds2))
return 0;
y1 = child_bounds1.origin.y + (child_bounds1.size.height / 2.0f);
y2 = child_bounds2.origin.y + (child_bounds2.size.height / 2.0f);
if (y1 == y2)
{
const float x1 = child_bounds1.origin.x + (child_bounds1.size.width / 2.0f);
const float x2 = child_bounds2.origin.x + (child_bounds2.size.width / 2.0f);
if (text_direction == GTK_TEXT_DIR_RTL)
return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
else
return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
}
else
return (y1 < y2) ? -1 : 1;
}
static void
focus_sort_tab (GtkWidget *widget,
GtkDirectionType direction,
GPtrArray *focus_order)
{
GtkTextDirection text_direction = _gtk_widget_get_direction (widget);
g_ptr_array_sort_with_data (focus_order, tab_sort_func, GINT_TO_POINTER (text_direction));
if (direction == GTK_DIR_TAB_BACKWARD)
reverse_ptr_array (focus_order);
}
/* Look for a child in @children that is intermediate between
* the focus widget and container. This widget, if it exists,
* acts as the starting widget for focus navigation.
*/
static GtkWidget *
find_old_focus (GtkWidget *widget,
GPtrArray *children)
{
int i;
for (i = 0; i < children->len; i ++)
{
GtkWidget *child = g_ptr_array_index (children, i);
GtkWidget *child_ptr = child;
while (child_ptr && child_ptr != widget)
{
GtkWidget *parent;
parent = _gtk_widget_get_parent (child_ptr);
if (parent && (gtk_widget_get_focus_child (parent) != child_ptr))
{
child = NULL;
break;
}
child_ptr = parent;
}
if (child)
return child;
}
return NULL;
}
static gboolean
old_focus_coords (GtkWidget *widget,
graphene_rect_t *old_focus_bounds)
{
GtkWidget *old_focus;
old_focus = gtk_root_get_focus (gtk_widget_get_root (widget));
if (old_focus)
return gtk_widget_compute_bounds (old_focus, widget, old_focus_bounds);
return FALSE;
}
static int
axis_compare (gconstpointer a,
gconstpointer b,
gpointer user_data)
{
graphene_rect_t bounds1;
graphene_rect_t bounds2;
CompareInfo *compare = user_data;
int start1, end1;
int start2, end2;
if (!gtk_widget_compute_bounds (*((GtkWidget **)a), compare->widget, &bounds1) ||
!gtk_widget_compute_bounds (*((GtkWidget **)b), compare->widget, &bounds2))
return 0;
get_axis_info (&bounds1, compare->axis, &start1, &end1);
get_axis_info (&bounds2, compare->axis, &start2, &end2);
start1 = start1 + (end1 / 2);
start2 = start2 + (end2 / 2);
if (start1 == start2)
{
/* Now use origin/bounds to compare the 2 widgets on the other axis */
get_axis_info (&bounds1, 1 - compare->axis, &start1, &end1);
get_axis_info (&bounds2, 1 - compare->axis, &start2, &end2);
int x1 = abs (start1 + (end1 / 2) - compare->x);
int x2 = abs (start2 + (end2 / 2) - compare->x);
if (compare->reverse)
return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
else
return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
}
else
return (start1 < start2) ? -1 : 1;
}
static void
focus_sort_left_right (GtkWidget *widget,
GtkDirectionType direction,
GPtrArray *focus_order)
{
CompareInfo compare_info;
GtkWidget *old_focus = gtk_widget_get_focus_child (widget);
graphene_rect_t old_bounds;
compare_info.widget = widget;
compare_info.reverse = (direction == GTK_DIR_LEFT);
if (!old_focus)
old_focus = find_old_focus (widget, focus_order);
if (old_focus && gtk_widget_compute_bounds (old_focus, widget, &old_bounds))
{
float compare_y1;
float compare_y2;
float compare_x;
int i;
/* Delete widgets from list that don't match minimum criteria */
compare_y1 = old_bounds.origin.y;
compare_y2 = old_bounds.origin.y + old_bounds.size.height;
if (direction == GTK_DIR_LEFT)
compare_x = old_bounds.origin.x;
else
compare_x = old_bounds.origin.x + old_bounds.size.width;
for (i = 0; i < focus_order->len; i ++)
{
GtkWidget *child = g_ptr_array_index (focus_order, i);
if (child != old_focus)
{
graphene_rect_t child_bounds;
if (gtk_widget_compute_bounds (child, widget, &child_bounds))
{
const float child_y1 = child_bounds.origin.y;
const float child_y2 = child_bounds.origin.y + child_bounds.size.height;
if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
(direction == GTK_DIR_RIGHT && child_bounds.origin.x + child_bounds.size.width < compare_x) || /* Not to left */
(direction == GTK_DIR_LEFT && child_bounds.origin.x > compare_x)) /* Not to right */
{
g_ptr_array_remove_index (focus_order, i);
i --;
}
}
else
{
g_ptr_array_remove_index (focus_order, i);
i --;
}
}
}
compare_info.y = (compare_y1 + compare_y2) / 2;
compare_info.x = old_bounds.origin.x + (old_bounds.size.width / 2.0f);
}
else
{
/* No old focus widget, need to figure out starting x,y some other way
*/
graphene_rect_t bounds;
GtkWidget *parent;
graphene_rect_t old_focus_bounds;
parent = gtk_widget_get_parent (widget);
if (!gtk_widget_compute_bounds (widget, parent ? parent : widget, &bounds))
graphene_rect_init (&bounds, 0, 0, 0, 0);
if (old_focus_coords (widget, &old_focus_bounds))
{
compare_info.y = old_focus_bounds.origin.y + (old_focus_bounds.size.height / 2.0f);
}
else
{
if (!_gtk_widget_get_has_surface (widget))
compare_info.y = bounds.origin.y + bounds.size.height;
else
compare_info.y = bounds.size.height / 2.0f;
}
if (!_gtk_widget_get_has_surface (widget))
compare_info.x = (direction == GTK_DIR_RIGHT) ? bounds.origin.x : bounds.origin.x + bounds.size.width;
else
compare_info.x = (direction == GTK_DIR_RIGHT) ? 0 : bounds.size.width;
}
compare_info.axis = HORIZONTAL;
g_ptr_array_sort_with_data (focus_order, axis_compare, &compare_info);
if (compare_info.reverse)
reverse_ptr_array (focus_order);
}
static void
focus_sort_up_down (GtkWidget *widget,
GtkDirectionType direction,
GPtrArray *focus_order)
{
CompareInfo compare_info;
GtkWidget *old_focus = gtk_widget_get_focus_child (widget);
graphene_rect_t old_bounds;
compare_info.widget = widget;
compare_info.reverse = (direction == GTK_DIR_UP);
if (!old_focus)
old_focus = find_old_focus (widget, focus_order);
if (old_focus && gtk_widget_compute_bounds (old_focus, widget, &old_bounds))
{
float compare_x1;
float compare_x2;
float compare_y;
int i;
/* Delete widgets from list that don't match minimum criteria */
compare_x1 = old_bounds.origin.x;
compare_x2 = old_bounds.origin.x + old_bounds.size.width;
if (direction == GTK_DIR_UP)
compare_y = old_bounds.origin.y;
else
compare_y = old_bounds.origin.y + old_bounds.size.height;
for (i = 0; i < focus_order->len; i ++)
{
GtkWidget *child = g_ptr_array_index (focus_order, i);
if (child != old_focus)
{
graphene_rect_t child_bounds;
if (gtk_widget_compute_bounds (child, widget, &child_bounds))
{
const float child_x1 = child_bounds.origin.x;
const float child_x2 = child_bounds.origin.x + child_bounds.size.width;
if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
(direction == GTK_DIR_DOWN && child_bounds.origin.y + child_bounds.size.height < compare_y) || /* Not below */
(direction == GTK_DIR_UP && child_bounds.origin.y > compare_y)) /* Not above */
{
g_ptr_array_remove_index (focus_order, i);
i --;
}
}
else
{
g_ptr_array_remove_index (focus_order, i);
i --;
}
}
}
compare_info.x = (compare_x1 + compare_x2) / 2;
compare_info.y = old_bounds.origin.y + (old_bounds.size.height / 2.0f);
}
else
{
/* No old focus widget, need to figure out starting x,y some other way
*/
GtkWidget *parent;
graphene_rect_t bounds;
graphene_rect_t old_focus_bounds;
parent = gtk_widget_get_parent (widget);
if (!gtk_widget_compute_bounds (widget, parent ? parent : widget, &bounds))
graphene_rect_init (&bounds, 0, 0, 0, 0);
if (old_focus_coords (widget, &old_focus_bounds))
{
compare_info.x = old_focus_bounds.origin.x + (old_focus_bounds.size.width / 2.0f);
}
else
{
if (!_gtk_widget_get_has_surface (widget))
compare_info.x = bounds.origin.x + (bounds.size.width / 2.0f);
else
compare_info.x = bounds.size.width / 2.0f;
}
if (!_gtk_widget_get_has_surface (widget))
compare_info.y = (direction == GTK_DIR_DOWN) ? bounds.origin.y : bounds.origin.y + bounds.size.height;
else
compare_info.y = (direction == GTK_DIR_DOWN) ? 0 : + bounds.size.height;
}
compare_info.axis = VERTICAL;
g_ptr_array_sort_with_data (focus_order, axis_compare, &compare_info);
if (compare_info.reverse)
reverse_ptr_array (focus_order);
}
void
gtk_widget_focus_sort (GtkWidget *widget,
GtkDirectionType direction,
GPtrArray *focus_order)
{
GtkWidget *child;
g_assert (focus_order != NULL);
if (focus_order->len == 0)
{
/* Initialize the list with all realized child widgets */
for (child = _gtk_widget_get_first_child (widget);
child != NULL;
child = _gtk_widget_get_next_sibling (child))
{
if (_gtk_widget_get_realized (child) &&
_gtk_widget_is_drawable (child) &&
gtk_widget_get_sensitive (child))
g_ptr_array_add (focus_order, child);
}
}
/* Now sort that list depending on @direction */
switch (direction)
{
case GTK_DIR_TAB_FORWARD:
case GTK_DIR_TAB_BACKWARD:
focus_sort_tab (widget, direction, focus_order);
break;
case GTK_DIR_UP:
case GTK_DIR_DOWN:
focus_sort_up_down (widget, direction, focus_order);
break;
case GTK_DIR_LEFT:
case GTK_DIR_RIGHT:
focus_sort_left_right (widget, direction, focus_order);
break;
default:
g_assert_not_reached ();
}
}
gboolean
gtk_widget_focus_move (GtkWidget *widget,
GtkDirectionType direction)
{
GPtrArray *focus_order;
GtkWidget *focus_child = gtk_widget_get_focus_child (widget);
int i;
gboolean ret = FALSE;
focus_order = g_ptr_array_new ();
gtk_widget_focus_sort (widget, direction, focus_order);
for (i = 0; i < focus_order->len && !ret; i++)
{
GtkWidget *child = g_ptr_array_index (focus_order, i);
if (focus_child)
{
if (focus_child == child)
{
focus_child = NULL;
ret = gtk_widget_child_focus (child, direction);
}
}
else if (_gtk_widget_is_drawable (child) &&
gtk_widget_is_ancestor (child, widget))
{
ret = gtk_widget_child_focus (child, direction);
}
}
g_ptr_array_unref (focus_order);
return ret;
}
|