forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fl_accessible_text_field.cc
621 lines (521 loc) · 23.7 KB
/
fl_accessible_text_field.cc
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/shell/platform/linux/fl_accessible_text_field.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_standard_message_codec.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_value.h"
G_DEFINE_AUTOPTR_CLEANUP_FUNC(PangoContext, g_object_unref)
// PangoLayout g_autoptr macro weren't added until 1.49.4. Add them manually.
// https://gitlab.gnome.org/GNOME/pango/-/commit/0b84e14
#if !PANGO_VERSION_CHECK(1, 49, 4)
G_DEFINE_AUTOPTR_CLEANUP_FUNC(PangoLayout, g_object_unref)
#endif
typedef bool (*FlTextBoundaryCallback)(const PangoLogAttr* attr);
struct _FlAccessibleTextField {
FlAccessibleNode parent_instance;
gint selection_base;
gint selection_extent;
GtkEntryBuffer* buffer;
FlutterTextDirection text_direction;
};
static void fl_accessible_text_iface_init(AtkTextIface* iface);
static void fl_accessible_editable_text_iface_init(AtkEditableTextIface* iface);
G_DEFINE_TYPE_WITH_CODE(
FlAccessibleTextField,
fl_accessible_text_field,
fl_accessible_node_get_type(),
G_IMPLEMENT_INTERFACE(ATK_TYPE_TEXT, fl_accessible_text_iface_init)
G_IMPLEMENT_INTERFACE(ATK_TYPE_EDITABLE_TEXT,
fl_accessible_editable_text_iface_init))
static gchar* get_substring(FlAccessibleTextField* self,
glong start,
glong end) {
const gchar* value = gtk_entry_buffer_get_text(self->buffer);
if (end == -1) {
// g_utf8_substring() accepts -1 since 2.72
end = g_utf8_strlen(value, -1);
}
return g_utf8_substring(value, start, end);
}
static PangoContext* get_pango_context(FlAccessibleTextField* self) {
PangoFontMap* font_map = pango_cairo_font_map_get_default();
PangoContext* context = pango_font_map_create_context(font_map);
pango_context_set_base_dir(context,
self->text_direction == kFlutterTextDirectionRTL
? PANGO_DIRECTION_RTL
: PANGO_DIRECTION_LTR);
return context;
}
static PangoLayout* create_pango_layout(FlAccessibleTextField* self) {
g_autoptr(PangoContext) context = get_pango_context(self);
PangoLayout* layout = pango_layout_new(context);
pango_layout_set_text(layout, gtk_entry_buffer_get_text(self->buffer), -1);
return layout;
}
static gchar* get_string_at_offset(FlAccessibleTextField* self,
gint start,
gint end,
FlTextBoundaryCallback is_start,
FlTextBoundaryCallback is_end,
gint* start_offset,
gint* end_offset) {
g_autoptr(PangoLayout) layout = create_pango_layout(self);
gint n_attrs = 0;
const PangoLogAttr* attrs =
pango_layout_get_log_attrs_readonly(layout, &n_attrs);
while (start > 0 && !is_start(&attrs[start])) {
--start;
}
if (start_offset != nullptr) {
*start_offset = start;
}
while (end < n_attrs && !is_end(&attrs[end])) {
++end;
}
if (end_offset != nullptr) {
*end_offset = end;
}
return get_substring(self, start, end);
}
static gchar* get_char_at_offset(FlAccessibleTextField* self,
gint offset,
gint* start_offset,
gint* end_offset) {
return get_string_at_offset(
self, offset, offset + 1,
[](const PangoLogAttr* attr) -> bool { return attr->is_char_break; },
[](const PangoLogAttr* attr) -> bool { return attr->is_char_break; },
start_offset, end_offset);
}
static gchar* get_word_at_offset(FlAccessibleTextField* self,
gint offset,
gint* start_offset,
gint* end_offset) {
return get_string_at_offset(
self, offset, offset,
[](const PangoLogAttr* attr) -> bool { return attr->is_word_start; },
[](const PangoLogAttr* attr) -> bool { return attr->is_word_end; },
start_offset, end_offset);
}
static gchar* get_sentence_at_offset(FlAccessibleTextField* self,
gint offset,
gint* start_offset,
gint* end_offset) {
return get_string_at_offset(
self, offset, offset,
[](const PangoLogAttr* attr) -> bool { return attr->is_sentence_start; },
[](const PangoLogAttr* attr) -> bool { return attr->is_sentence_end; },
start_offset, end_offset);
}
static gchar* get_line_at_offset(FlAccessibleTextField* self,
gint offset,
gint* start_offset,
gint* end_offset) {
g_autoptr(PangoLayout) layout = create_pango_layout(self);
GSList* lines = pango_layout_get_lines_readonly(layout);
while (lines != nullptr) {
PangoLayoutLine* line = static_cast<PangoLayoutLine*>(lines->data);
if (offset >= line->start_index &&
offset <= line->start_index + line->length) {
if (start_offset != nullptr) {
*start_offset = line->start_index;
}
if (end_offset != nullptr) {
*end_offset = line->start_index + line->length;
}
return get_substring(self, line->start_index,
line->start_index + line->length);
}
lines = lines->next;
}
return nullptr;
}
static gchar* get_paragraph_at_offset(FlAccessibleTextField* self,
gint offset,
gint* start_offset,
gint* end_offset) {
g_autoptr(PangoLayout) layout = create_pango_layout(self);
PangoLayoutLine* start = nullptr;
PangoLayoutLine* end = nullptr;
gint n_lines = pango_layout_get_line_count(layout);
for (gint i = 0; i < n_lines; ++i) {
PangoLayoutLine* line = pango_layout_get_line(layout, i);
if (line->is_paragraph_start) {
end = line;
}
if (start != nullptr && end != nullptr && offset >= start->start_index &&
offset <= end->start_index + end->length) {
if (start_offset != nullptr) {
*start_offset = start->start_index;
}
if (end_offset != nullptr) {
*end_offset = end->start_index + end->length;
}
return get_substring(self, start->start_index,
end->start_index + end->length);
}
if (line->is_paragraph_start) {
start = line;
}
}
return nullptr;
}
static void perform_set_text_action(FlAccessibleTextField* self,
const char* text) {
g_autoptr(FlValue) value = fl_value_new_string(text);
g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
g_autoptr(GBytes) message =
fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), value, nullptr);
fl_accessible_node_perform_action(FL_ACCESSIBLE_NODE(self),
kFlutterSemanticsActionSetText, message);
}
static void perform_set_selection_action(FlAccessibleTextField* self,
gint base,
gint extent) {
g_autoptr(FlValue) value = fl_value_new_map();
fl_value_set_string_take(value, "base", fl_value_new_int(base));
fl_value_set_string_take(value, "extent", fl_value_new_int(extent));
g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
g_autoptr(GBytes) message =
fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), value, nullptr);
fl_accessible_node_perform_action(
FL_ACCESSIBLE_NODE(self), kFlutterSemanticsActionSetSelection, message);
}
// Implements GObject::dispose.
static void fl_accessible_text_field_dispose(GObject* object) {
FlAccessibleTextField* self = FL_ACCESSIBLE_TEXT_FIELD(object);
g_clear_object(&self->buffer);
G_OBJECT_CLASS(fl_accessible_text_field_parent_class)->dispose(object);
}
// Implements FlAccessibleNode::set_value.
static void fl_accessible_text_field_set_value(FlAccessibleNode* node,
const gchar* value) {
g_return_if_fail(FL_IS_ACCESSIBLE_TEXT_FIELD(node));
FlAccessibleTextField* self = FL_ACCESSIBLE_TEXT_FIELD(node);
if (g_strcmp0(gtk_entry_buffer_get_text(self->buffer), value) == 0) {
return;
}
gtk_entry_buffer_set_text(self->buffer, value, -1);
}
// Implements FlAccessibleNode::set_text_selection.
static void fl_accessible_text_field_set_text_selection(FlAccessibleNode* node,
gint base,
gint extent) {
g_return_if_fail(FL_IS_ACCESSIBLE_TEXT_FIELD(node));
FlAccessibleTextField* self = FL_ACCESSIBLE_TEXT_FIELD(node);
gboolean caret_moved = extent != self->selection_extent;
gboolean has_selection = base != extent;
gboolean had_selection = self->selection_base != self->selection_extent;
gboolean selection_changed = (has_selection || had_selection) &&
(caret_moved || base != self->selection_base);
self->selection_base = base;
self->selection_extent = extent;
if (selection_changed) {
g_signal_emit_by_name(self, "text-selection-changed", nullptr);
}
if (caret_moved) {
g_signal_emit_by_name(self, "text-caret-moved", extent, nullptr);
}
}
// Implements FlAccessibleNode::set_text_direction.
static void fl_accessible_text_field_set_text_direction(
FlAccessibleNode* node,
FlutterTextDirection direction) {
g_return_if_fail(FL_IS_ACCESSIBLE_TEXT_FIELD(node));
FlAccessibleTextField* self = FL_ACCESSIBLE_TEXT_FIELD(node);
self->text_direction = direction;
}
// Overrides FlAccessibleNode::perform_action.
void fl_accessible_text_field_perform_action(FlAccessibleNode* self,
FlutterSemanticsAction action,
GBytes* data) {
FlAccessibleNodeClass* parent_class =
FL_ACCESSIBLE_NODE_CLASS(fl_accessible_text_field_parent_class);
switch (action) {
case kFlutterSemanticsActionMoveCursorForwardByCharacter:
case kFlutterSemanticsActionMoveCursorBackwardByCharacter:
case kFlutterSemanticsActionMoveCursorForwardByWord:
case kFlutterSemanticsActionMoveCursorBackwardByWord: {
// These actions require a boolean argument that indicates whether the
// selection should be extended or collapsed when moving the cursor.
g_autoptr(FlValue) extend_selection = fl_value_new_bool(false);
g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
g_autoptr(GBytes) message = fl_message_codec_encode_message(
FL_MESSAGE_CODEC(codec), extend_selection, nullptr);
parent_class->perform_action(self, action, message);
break;
}
default:
parent_class->perform_action(self, action, data);
break;
}
}
// Implements AtkText::get_character_count.
static gint fl_accessible_text_field_get_character_count(AtkText* text) {
g_return_val_if_fail(FL_IS_ACCESSIBLE_TEXT_FIELD(text), 0);
FlAccessibleTextField* self = FL_ACCESSIBLE_TEXT_FIELD(text);
return gtk_entry_buffer_get_length(self->buffer);
}
// Implements AtkText::get_text.
static gchar* fl_accessible_text_field_get_text(AtkText* text,
gint start_offset,
gint end_offset) {
g_return_val_if_fail(FL_IS_ACCESSIBLE_TEXT_FIELD(text), nullptr);
FlAccessibleTextField* self = FL_ACCESSIBLE_TEXT_FIELD(text);
return get_substring(self, start_offset, end_offset);
}
// Implements AtkText::get_string_at_offset.
static gchar* fl_accessible_text_field_get_string_at_offset(
AtkText* text,
gint offset,
AtkTextGranularity granularity,
gint* start_offset,
gint* end_offset) {
g_return_val_if_fail(FL_IS_ACCESSIBLE_TEXT_FIELD(text), nullptr);
FlAccessibleTextField* self = FL_ACCESSIBLE_TEXT_FIELD(text);
switch (granularity) {
case ATK_TEXT_GRANULARITY_CHAR:
return get_char_at_offset(self, offset, start_offset, end_offset);
case ATK_TEXT_GRANULARITY_WORD:
return get_word_at_offset(self, offset, start_offset, end_offset);
case ATK_TEXT_GRANULARITY_SENTENCE:
return get_sentence_at_offset(self, offset, start_offset, end_offset);
case ATK_TEXT_GRANULARITY_LINE:
return get_line_at_offset(self, offset, start_offset, end_offset);
case ATK_TEXT_GRANULARITY_PARAGRAPH:
return get_paragraph_at_offset(self, offset, start_offset, end_offset);
default:
return nullptr;
}
}
// Implements AtkText::get_text_at_offset (deprecated but still commonly used).
static gchar* fl_accessible_text_field_get_text_at_offset(
AtkText* text,
gint offset,
AtkTextBoundary boundary_type,
gint* start_offset,
gint* end_offset) {
switch (boundary_type) {
case ATK_TEXT_BOUNDARY_CHAR:
return fl_accessible_text_field_get_string_at_offset(
text, offset, ATK_TEXT_GRANULARITY_CHAR, start_offset, end_offset);
break;
case ATK_TEXT_BOUNDARY_WORD_START:
case ATK_TEXT_BOUNDARY_WORD_END:
return fl_accessible_text_field_get_string_at_offset(
text, offset, ATK_TEXT_GRANULARITY_WORD, start_offset, end_offset);
break;
case ATK_TEXT_BOUNDARY_SENTENCE_START:
case ATK_TEXT_BOUNDARY_SENTENCE_END:
return fl_accessible_text_field_get_string_at_offset(
text, offset, ATK_TEXT_GRANULARITY_SENTENCE, start_offset,
end_offset);
break;
case ATK_TEXT_BOUNDARY_LINE_START:
case ATK_TEXT_BOUNDARY_LINE_END:
return fl_accessible_text_field_get_string_at_offset(
text, offset, ATK_TEXT_GRANULARITY_LINE, start_offset, end_offset);
break;
default:
return nullptr;
}
}
// Implements AtkText::get_caret_offset.
static gint fl_accessible_text_field_get_caret_offset(AtkText* text) {
g_return_val_if_fail(FL_IS_ACCESSIBLE_TEXT_FIELD(text), -1);
FlAccessibleTextField* self = FL_ACCESSIBLE_TEXT_FIELD(text);
return self->selection_extent;
}
// Implements AtkText::set_caret_offset.
static gboolean fl_accessible_text_field_set_caret_offset(AtkText* text,
gint offset) {
g_return_val_if_fail(FL_IS_ACCESSIBLE_TEXT_FIELD(text), false);
FlAccessibleTextField* self = FL_ACCESSIBLE_TEXT_FIELD(text);
perform_set_selection_action(self, offset, offset);
return TRUE;
}
// Implements AtkText::get_n_selections.
static gint fl_accessible_text_field_get_n_selections(AtkText* text) {
g_return_val_if_fail(FL_IS_ACCESSIBLE_TEXT_FIELD(text), 0);
FlAccessibleTextField* self = FL_ACCESSIBLE_TEXT_FIELD(text);
if (self->selection_base == self->selection_extent) {
return 0;
}
return 1;
}
// Implements AtkText::get_selection.
static gchar* fl_accessible_text_field_get_selection(AtkText* text,
gint selection_num,
gint* start_offset,
gint* end_offset) {
g_return_val_if_fail(FL_IS_ACCESSIBLE_TEXT_FIELD(text), nullptr);
FlAccessibleTextField* self = FL_ACCESSIBLE_TEXT_FIELD(text);
if (selection_num != 0 || self->selection_base == self->selection_extent) {
return nullptr;
}
gint start = MIN(self->selection_base, self->selection_extent);
gint end = MAX(self->selection_base, self->selection_extent);
if (start_offset != nullptr) {
*start_offset = start;
}
if (end_offset != nullptr) {
*end_offset = end;
}
return get_substring(self, start, end);
}
// Implements AtkText::add_selection.
static gboolean fl_accessible_text_field_add_selection(AtkText* text,
gint start_offset,
gint end_offset) {
g_return_val_if_fail(FL_IS_ACCESSIBLE_TEXT_FIELD(text), false);
FlAccessibleTextField* self = FL_ACCESSIBLE_TEXT_FIELD(text);
if (self->selection_base != self->selection_extent) {
return FALSE;
}
perform_set_selection_action(self, start_offset, end_offset);
return TRUE;
}
// Implements AtkText::remove_selection.
static gboolean fl_accessible_text_field_remove_selection(AtkText* text,
gint selection_num) {
g_return_val_if_fail(FL_IS_ACCESSIBLE_TEXT_FIELD(text), false);
FlAccessibleTextField* self = FL_ACCESSIBLE_TEXT_FIELD(text);
if (selection_num != 0 || self->selection_base == self->selection_extent) {
return FALSE;
}
perform_set_selection_action(self, self->selection_extent,
self->selection_extent);
return TRUE;
}
// Implements AtkText::set_selection.
static gboolean fl_accessible_text_field_set_selection(AtkText* text,
gint selection_num,
gint start_offset,
gint end_offset) {
g_return_val_if_fail(FL_IS_ACCESSIBLE_TEXT_FIELD(text), false);
FlAccessibleTextField* self = FL_ACCESSIBLE_TEXT_FIELD(text);
if (selection_num != 0) {
return FALSE;
}
perform_set_selection_action(self, start_offset, end_offset);
return TRUE;
}
// Implements AtkEditableText::set_text_contents.
static void fl_accessible_text_field_set_text_contents(
AtkEditableText* editable_text,
const gchar* string) {
g_return_if_fail(FL_IS_ACCESSIBLE_TEXT_FIELD(editable_text));
FlAccessibleTextField* self = FL_ACCESSIBLE_TEXT_FIELD(editable_text);
perform_set_text_action(self, string);
}
// Implements AtkEditableText::insert_text.
static void fl_accessible_text_field_insert_text(AtkEditableText* editable_text,
const gchar* string,
gint length,
gint* position) {
g_return_if_fail(FL_IS_ACCESSIBLE_TEXT_FIELD(editable_text));
FlAccessibleTextField* self = FL_ACCESSIBLE_TEXT_FIELD(editable_text);
*position +=
gtk_entry_buffer_insert_text(self->buffer, *position, string, length);
perform_set_text_action(self, gtk_entry_buffer_get_text(self->buffer));
perform_set_selection_action(self, *position, *position);
}
// Implements AtkEditableText::delete_text.
static void fl_accessible_node_delete_text(AtkEditableText* editable_text,
gint start_pos,
gint end_pos) {
g_return_if_fail(FL_IS_ACCESSIBLE_TEXT_FIELD(editable_text));
FlAccessibleTextField* self = FL_ACCESSIBLE_TEXT_FIELD(editable_text);
gtk_entry_buffer_delete_text(self->buffer, start_pos, end_pos - start_pos);
perform_set_text_action(self, gtk_entry_buffer_get_text(self->buffer));
perform_set_selection_action(self, start_pos, start_pos);
}
// Implement AtkEditableText::copy_text.
static void fl_accessible_text_field_copy_text(AtkEditableText* editable_text,
gint start_pos,
gint end_pos) {
g_return_if_fail(FL_IS_ACCESSIBLE_TEXT_FIELD(editable_text));
FlAccessibleTextField* self = FL_ACCESSIBLE_TEXT_FIELD(editable_text);
perform_set_selection_action(self, start_pos, end_pos);
fl_accessible_node_perform_action(FL_ACCESSIBLE_NODE(editable_text),
kFlutterSemanticsActionCopy, nullptr);
}
// Implements AtkEditableText::cut_text.
static void fl_accessible_text_field_cut_text(AtkEditableText* editable_text,
gint start_pos,
gint end_pos) {
g_return_if_fail(FL_IS_ACCESSIBLE_TEXT_FIELD(editable_text));
FlAccessibleTextField* self = FL_ACCESSIBLE_TEXT_FIELD(editable_text);
perform_set_selection_action(self, start_pos, end_pos);
fl_accessible_node_perform_action(FL_ACCESSIBLE_NODE(editable_text),
kFlutterSemanticsActionCut, nullptr);
}
// Implements AtkEditableText::paste_text.
static void fl_accessible_text_field_paste_text(AtkEditableText* editable_text,
gint position) {
g_return_if_fail(FL_IS_ACCESSIBLE_TEXT_FIELD(editable_text));
FlAccessibleTextField* self = FL_ACCESSIBLE_TEXT_FIELD(editable_text);
perform_set_selection_action(self, position, position);
fl_accessible_node_perform_action(FL_ACCESSIBLE_NODE(editable_text),
kFlutterSemanticsActionPaste, nullptr);
}
static void fl_accessible_text_field_class_init(
FlAccessibleTextFieldClass* klass) {
G_OBJECT_CLASS(klass)->dispose = fl_accessible_text_field_dispose;
FL_ACCESSIBLE_NODE_CLASS(klass)->set_value =
fl_accessible_text_field_set_value;
FL_ACCESSIBLE_NODE_CLASS(klass)->set_text_selection =
fl_accessible_text_field_set_text_selection;
FL_ACCESSIBLE_NODE_CLASS(klass)->set_text_direction =
fl_accessible_text_field_set_text_direction;
FL_ACCESSIBLE_NODE_CLASS(klass)->perform_action =
fl_accessible_text_field_perform_action;
}
static void fl_accessible_text_iface_init(AtkTextIface* iface) {
iface->get_character_count = fl_accessible_text_field_get_character_count;
iface->get_text = fl_accessible_text_field_get_text;
iface->get_text_at_offset = fl_accessible_text_field_get_text_at_offset;
iface->get_string_at_offset = fl_accessible_text_field_get_string_at_offset;
iface->get_caret_offset = fl_accessible_text_field_get_caret_offset;
iface->set_caret_offset = fl_accessible_text_field_set_caret_offset;
iface->get_n_selections = fl_accessible_text_field_get_n_selections;
iface->get_selection = fl_accessible_text_field_get_selection;
iface->add_selection = fl_accessible_text_field_add_selection;
iface->remove_selection = fl_accessible_text_field_remove_selection;
iface->set_selection = fl_accessible_text_field_set_selection;
}
static void fl_accessible_editable_text_iface_init(
AtkEditableTextIface* iface) {
iface->set_text_contents = fl_accessible_text_field_set_text_contents;
iface->insert_text = fl_accessible_text_field_insert_text;
iface->delete_text = fl_accessible_node_delete_text;
iface->copy_text = fl_accessible_text_field_copy_text;
iface->cut_text = fl_accessible_text_field_cut_text;
iface->paste_text = fl_accessible_text_field_paste_text;
}
static void fl_accessible_text_field_init(FlAccessibleTextField* self) {
self->selection_base = -1;
self->selection_extent = -1;
self->buffer = gtk_entry_buffer_new("", 0);
g_signal_connect_object(
self->buffer, "inserted-text",
G_CALLBACK(+[](FlAccessibleTextField* self, guint position, gchar* chars,
guint n_chars) {
g_signal_emit_by_name(self, "text-insert", position, n_chars, chars,
nullptr);
}),
self, G_CONNECT_SWAPPED);
g_signal_connect_object(self->buffer, "deleted-text",
G_CALLBACK(+[](FlAccessibleTextField* self,
guint position, guint n_chars) {
g_autofree gchar* chars = atk_text_get_text(
ATK_TEXT(self), position, position + n_chars);
g_signal_emit_by_name(self, "text-remove", position,
n_chars, chars, nullptr);
}),
self, G_CONNECT_SWAPPED);
}
FlAccessibleNode* fl_accessible_text_field_new(FlEngine* engine, int32_t id) {
return FL_ACCESSIBLE_NODE(g_object_new(fl_accessible_text_field_get_type(),
"engine", engine, "id", id, nullptr));
}