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
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
/* CTK+ - accessibility implementations
 * Copyright 2001, 2002, 2003 Sun Microsystems Inc.
 *
 * 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 "config.h"

#include <glib/gi18n-lib.h>
#include <string.h>
#include <ctk/ctk.h>
#include "ctkpango.h"
#include "ctkentryaccessible.h"
#include "ctkentryprivate.h"
#include "ctkcomboboxaccessible.h"
#include "ctkstylecontextprivate.h"
#include "ctkwidgetprivate.h"

#define CTK_TYPE_ENTRY_ICON_ACCESSIBLE      (ctk_entry_icon_accessible_get_type ())
#define CTK_ENTRY_ICON_ACCESSIBLE(obj)      (G_TYPE_CHECK_INSTANCE_CAST ((obj), CTK_TYPE_ENTRY_ICON_ACCESSIBLE, CtkEntryIconAccessible))
#define CTK_IS_ENTRY_ICON_ACCESSIBLE(obj)   (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CTK_TYPE_ENTRY_ICON_ACCESSIBLE))

struct _CtkEntryAccessiblePrivate
{
  gint cursor_position;
  gint selection_bound;
  AtkObject *icons[2];
};

typedef struct _CtkEntryIconAccessible CtkEntryIconAccessible;
typedef struct _CtkEntryIconAccessibleClass CtkEntryIconAccessibleClass;

struct _CtkEntryIconAccessible
{
  AtkObject parent;

  CtkEntryAccessible *entry;
  CtkEntryIconPosition pos;
};

struct _CtkEntryIconAccessibleClass
{
  AtkObjectClass parent_class;
};

static void atk_action_interface_init (AtkActionIface *iface);

static void icon_atk_action_interface_init (AtkActionIface *iface);
static void icon_atk_component_interface_init (AtkComponentIface *iface);

GType ctk_entry_icon_accessible_get_type (void);

G_DEFINE_TYPE_WITH_CODE (CtkEntryIconAccessible, ctk_entry_icon_accessible, ATK_TYPE_OBJECT,<--- There is an unknown macro here somewhere. Configuration is required. If G_DEFINE_TYPE_WITH_CODE is a macro then please configure it.
                         G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, icon_atk_action_interface_init)
                         G_IMPLEMENT_INTERFACE (ATK_TYPE_COMPONENT, icon_atk_component_interface_init))

static void
ctk_entry_icon_accessible_remove_entry (gpointer data,
                                        GObject *obj G_GNUC_UNUSED)
{
  CtkEntryIconAccessible *icon = data;

  if (icon->entry)
    {
      icon->entry = NULL;
      g_object_notify (G_OBJECT (icon), "accessible-parent");
      atk_object_notify_state_change (ATK_OBJECT (icon), ATK_STATE_DEFUNCT, TRUE);
    }
}

static AtkObject *
ctk_entry_icon_accessible_new (CtkEntryAccessible *entry,
                               CtkEntryIconPosition pos)
{
  CtkEntryIconAccessible *icon;
  AtkObject *accessible;

  icon = g_object_new (ctk_entry_icon_accessible_get_type (), NULL);
  icon->entry = entry;
  g_object_weak_ref (G_OBJECT (entry),
                     ctk_entry_icon_accessible_remove_entry,
                     icon);
  icon->pos = pos;

  accessible = ATK_OBJECT (icon);
  atk_object_initialize (accessible, NULL);
  return accessible;
}

static void
ctk_entry_icon_accessible_init (CtkEntryIconAccessible *icon G_GNUC_UNUSED)
{
}

static void
ctk_entry_icon_accessible_initialize (AtkObject *obj,
                                      gpointer   data)
{
  CtkEntryIconAccessible *icon = CTK_ENTRY_ICON_ACCESSIBLE (obj);
  CtkWidget *widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (icon->entry));
  CtkEntry *ctk_entry = CTK_ENTRY (widget);
  const gchar *name;
  gchar *text;

  ATK_OBJECT_CLASS (ctk_entry_icon_accessible_parent_class)->initialize (obj, data);
  atk_object_set_role (obj, ATK_ROLE_ICON);

  name = ctk_entry_get_icon_name (ctk_entry, icon->pos);
  if (name)
    atk_object_set_name (obj, name);

  text = ctk_entry_get_icon_tooltip_text (ctk_entry, icon->pos);
  if (text)
    {
      atk_object_set_description (obj, text);
      g_free (text);
    }

  atk_object_set_parent (obj, ATK_OBJECT (icon->entry));
}

static AtkObject *
ctk_entry_icon_accessible_get_parent (AtkObject *accessible)
{
  CtkEntryIconAccessible *icon = CTK_ENTRY_ICON_ACCESSIBLE (accessible);

  return ATK_OBJECT (icon->entry);
}

static AtkStateSet *
ctk_entry_icon_accessible_ref_state_set (AtkObject *accessible)
{
  CtkEntryIconAccessible *icon = CTK_ENTRY_ICON_ACCESSIBLE (accessible);
  AtkStateSet *set = atk_state_set_new ();
  AtkStateSet *entry_set;
  CtkWidget *widget;
  CtkEntry *ctk_entry;

  if (!icon->entry)
    {
      atk_state_set_add_state (set, ATK_STATE_DEFUNCT);
      return set;
    }

  entry_set = atk_object_ref_state_set (ATK_OBJECT (icon->entry));
  if (!entry_set || atk_state_set_contains_state (entry_set, ATK_STATE_DEFUNCT))
    {
      atk_state_set_add_state (set, ATK_STATE_DEFUNCT);
    g_clear_object (&entry_set);
      return set;
    }

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (icon->entry));
  ctk_entry = CTK_ENTRY (widget);

  if (atk_state_set_contains_state (entry_set, ATK_STATE_ENABLED))
    atk_state_set_add_state (set, ATK_STATE_ENABLED);
  if (atk_state_set_contains_state (entry_set, ATK_STATE_SENSITIVE))
    atk_state_set_add_state (set, ATK_STATE_SENSITIVE);
  if (atk_state_set_contains_state (entry_set, ATK_STATE_SHOWING))
    atk_state_set_add_state (set, ATK_STATE_SHOWING);
  if (atk_state_set_contains_state (entry_set, ATK_STATE_VISIBLE))
    atk_state_set_add_state (set, ATK_STATE_VISIBLE);

  if (!ctk_entry_get_icon_sensitive (ctk_entry, icon->pos))
      atk_state_set_remove_state (set, ATK_STATE_SENSITIVE);
  if (!ctk_entry_get_icon_activatable (ctk_entry, icon->pos))
      atk_state_set_remove_state (set, ATK_STATE_ENABLED);

  g_object_unref (entry_set);
  return set;
}

static void
ctk_entry_icon_accessible_invalidate (CtkEntryIconAccessible *icon)
{
  if (!icon->entry)
    return;
  g_object_weak_unref (G_OBJECT (icon->entry),
                       ctk_entry_icon_accessible_remove_entry,
                       icon);
  ctk_entry_icon_accessible_remove_entry (icon, NULL);
}

static void
ctk_entry_icon_accessible_finalize (GObject *object)
{
  CtkEntryIconAccessible *icon = CTK_ENTRY_ICON_ACCESSIBLE (object);

  ctk_entry_icon_accessible_invalidate (icon);

  G_OBJECT_CLASS (ctk_entry_icon_accessible_parent_class)->finalize (object);
}

static void
ctk_entry_icon_accessible_class_init (CtkEntryIconAccessibleClass *klass)
{
  AtkObjectClass  *atk_class = ATK_OBJECT_CLASS (klass);
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  atk_class->initialize = ctk_entry_icon_accessible_initialize;
  atk_class->get_parent = ctk_entry_icon_accessible_get_parent;
  atk_class->ref_state_set = ctk_entry_icon_accessible_ref_state_set;

  gobject_class->finalize = ctk_entry_icon_accessible_finalize;
}

static gboolean
ctk_entry_icon_accessible_do_action (AtkAction *action,
                                     gint       i)
{
  CtkEntryIconAccessible *icon = (CtkEntryIconAccessible *)action;
  CtkWidget *widget;
  CtkEntry *ctk_entry;
  CdkEvent event;
  CdkRectangle icon_area;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (icon->entry));
  if (widget == NULL)
    return FALSE;

  if (i != 0)
    return FALSE;

  if (!ctk_widget_is_sensitive (widget) || !ctk_widget_get_visible (widget))
    return FALSE;

  ctk_entry = CTK_ENTRY (widget);

  if (!ctk_entry_get_icon_sensitive (ctk_entry, icon->pos) ||
      !ctk_entry_get_icon_activatable (ctk_entry, icon->pos))
    return FALSE;

  ctk_entry_get_icon_area (ctk_entry, icon->pos, &icon_area);
  memset (&event, 0, sizeof (event));
  event.button.type = CDK_BUTTON_PRESS;
  event.button.window = ctk_widget_get_window (widget);
  event.button.button = 1;
  event.button.send_event = TRUE;
  event.button.time = CDK_CURRENT_TIME;
  event.button.x = icon_area.x;
  event.button.y = icon_area.y;

  g_signal_emit_by_name (widget, "icon-press", 0, icon->pos, &event);
  return TRUE;
}

static gint
ctk_entry_icon_accessible_get_n_actions (AtkAction *action)
{
  CtkEntryIconAccessible *icon = CTK_ENTRY_ICON_ACCESSIBLE (action);
  CtkWidget *widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (icon->entry));
  CtkEntry *ctk_entry = CTK_ENTRY (widget);

  return (ctk_entry_get_icon_activatable (ctk_entry, icon->pos) ? 1 : 0);
}

static const gchar *
ctk_entry_icon_accessible_get_name (AtkAction *action,
                                    gint       i)
{
  CtkEntryIconAccessible *icon = CTK_ENTRY_ICON_ACCESSIBLE (action);
  CtkWidget *widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (icon->entry));
  CtkEntry *ctk_entry = CTK_ENTRY (widget);

  if (i != 0)
    return NULL;
  if (!ctk_entry_get_icon_activatable (ctk_entry, icon->pos))
    return NULL;

  return "activate";
}

static void
icon_atk_action_interface_init (AtkActionIface *iface)
{
  iface->do_action = ctk_entry_icon_accessible_do_action;
  iface->get_n_actions = ctk_entry_icon_accessible_get_n_actions;
  iface->get_name = ctk_entry_icon_accessible_get_name;
}

static void
ctk_entry_icon_accessible_get_extents (AtkComponent   *component,
                                       gint           *x,
                                       gint           *y,
                                       gint           *width,
                                       gint           *height,
                                       AtkCoordType    coord_type)
{
  CtkEntryIconAccessible *icon = CTK_ENTRY_ICON_ACCESSIBLE (component);
  CdkRectangle icon_area;
  CtkEntry *ctk_entry;
  CtkWidget *widget;

  *x = G_MININT;
  atk_component_get_extents (ATK_COMPONENT (icon->entry), x, y, width, height,
                             coord_type);
  if (*x == G_MININT)
    return;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (icon->entry));
  ctk_entry = CTK_ENTRY (widget);
  ctk_entry_get_icon_area (ctk_entry, icon->pos, &icon_area);
  *width = icon_area.width;
  *height = icon_area.height;
  *x += icon_area.x;
  *y += icon_area.y;
}

static void
ctk_entry_icon_accessible_get_position (AtkComponent   *component,
                                        gint           *x,
                                        gint           *y,
                                        AtkCoordType    coord_type)
{
  CtkEntryIconAccessible *icon = CTK_ENTRY_ICON_ACCESSIBLE (component);
  CdkRectangle icon_area;
  CtkEntry *ctk_entry;
  CtkWidget *widget;

  *x = G_MININT;
  atk_component_get_extents (ATK_COMPONENT (icon->entry), x, y, NULL, NULL,
                             coord_type);
  if (*x == G_MININT)
    return;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (icon->entry));
  ctk_entry = CTK_ENTRY (widget);
  ctk_entry_get_icon_area (ctk_entry, icon->pos, &icon_area);
  *x += icon_area.x;
  *y += icon_area.y;
}

static void
ctk_entry_icon_accessible_get_size (AtkComponent *component,
                                gint         *width,
                                gint         *height)
{
  CtkEntryIconAccessible *icon = CTK_ENTRY_ICON_ACCESSIBLE (component);
  CdkRectangle icon_area;
  CtkEntry *ctk_entry;
  CtkWidget *widget;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (icon->entry));
  ctk_entry = CTK_ENTRY (widget);
  ctk_entry_get_icon_area (ctk_entry, icon->pos, &icon_area);
  *width = icon_area.width;
  *height = icon_area.height;
}

static void
icon_atk_component_interface_init (AtkComponentIface *iface)
{
  iface->get_extents = ctk_entry_icon_accessible_get_extents;
  iface->get_size = ctk_entry_icon_accessible_get_size;
  iface->get_position = ctk_entry_icon_accessible_get_position;
}

/* Callbacks */

static void     insert_text_cb             (CtkEditable        *editable,
                                            gchar              *new_text,
                                            gint                new_text_length,
                                            gint               *position);
static void     delete_text_cb             (CtkEditable        *editable,
                                            gint                start,
                                            gint                end);

static gboolean check_for_selection_change (CtkEntryAccessible *entry,
                                            CtkEntry           *ctk_entry);


static void atk_editable_text_interface_init (AtkEditableTextIface *iface);
static void atk_text_interface_init          (AtkTextIface         *iface);
static void atk_action_interface_init        (AtkActionIface       *iface);


G_DEFINE_TYPE_WITH_CODE (CtkEntryAccessible, ctk_entry_accessible, CTK_TYPE_WIDGET_ACCESSIBLE,
                         G_ADD_PRIVATE (CtkEntryAccessible)
                         G_IMPLEMENT_INTERFACE (ATK_TYPE_EDITABLE_TEXT, atk_editable_text_interface_init)
                         G_IMPLEMENT_INTERFACE (ATK_TYPE_TEXT, atk_text_interface_init)
                         G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init))


static AtkStateSet *
ctk_entry_accessible_ref_state_set (AtkObject *accessible)
{
  AtkStateSet *state_set;
  gboolean value;
  CtkWidget *widget;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (accessible));
  if (widget == NULL)
    return NULL;

  state_set = ATK_OBJECT_CLASS (ctk_entry_accessible_parent_class)->ref_state_set (accessible);

  g_object_get (G_OBJECT (widget), "editable", &value, NULL);
  if (value)
    atk_state_set_add_state (state_set, ATK_STATE_EDITABLE);
  atk_state_set_add_state (state_set, ATK_STATE_SINGLE_LINE);

  return state_set;
}

static AtkAttributeSet *
ctk_entry_accessible_get_attributes (AtkObject *accessible)
{
  CtkWidget *widget;
  AtkAttributeSet *attributes;
  AtkAttribute *placeholder_text;
  const gchar *text;

  attributes = ATK_OBJECT_CLASS (ctk_entry_accessible_parent_class)->get_attributes (accessible);

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (accessible));
  if (widget == NULL)
    return attributes;

  text = ctk_entry_get_placeholder_text (CTK_ENTRY (widget));
  if (text == NULL)
    return attributes;

  placeholder_text = g_malloc (sizeof (AtkAttribute));
  placeholder_text->name = g_strdup ("placeholder-text");
  placeholder_text->value = g_strdup (text);

  attributes = g_slist_append (attributes, placeholder_text);

  return attributes;
}

static void
ctk_entry_accessible_initialize (AtkObject *obj,
                                 gpointer   data)
{
  CtkEntry *entry;
  CtkEntryAccessible *ctk_entry_accessible;
  gint start_pos, end_pos;

  ATK_OBJECT_CLASS (ctk_entry_accessible_parent_class)->initialize (obj, data);

  ctk_entry_accessible = CTK_ENTRY_ACCESSIBLE (obj);

  entry = CTK_ENTRY (data);
  ctk_editable_get_selection_bounds (CTK_EDITABLE (entry), &start_pos, &end_pos);
  ctk_entry_accessible->priv->cursor_position = end_pos;
  ctk_entry_accessible->priv->selection_bound = start_pos;

  /* Set up signal callbacks */
  g_signal_connect_after (entry, "insert-text", G_CALLBACK (insert_text_cb), NULL);
  g_signal_connect (entry, "delete-text", G_CALLBACK (delete_text_cb), NULL);

  if (ctk_entry_get_visibility (entry))
    obj->role = ATK_ROLE_TEXT;
  else
    obj->role = ATK_ROLE_PASSWORD_TEXT;
}

static void
ctk_entry_accessible_notify_ctk (GObject    *obj,
                                 GParamSpec *pspec)
{
  CtkWidget *widget;
  AtkObject* atk_obj;
  CtkEntry* ctk_entry;
  CtkEntryAccessible* entry;
  CtkEntryAccessiblePrivate *priv;

  widget = CTK_WIDGET (obj);
  atk_obj = ctk_widget_get_accessible (widget);
  ctk_entry = CTK_ENTRY (widget);
  entry = CTK_ENTRY_ACCESSIBLE (atk_obj);
  priv = entry->priv;

  if (g_strcmp0 (pspec->name, "cursor-position") == 0)
    {
      if (check_for_selection_change (entry, ctk_entry))
        g_signal_emit_by_name (atk_obj, "text-selection-changed");
      /*
       * The entry cursor position has moved so generate the signal.
       */
      g_signal_emit_by_name (atk_obj, "text-caret-moved",
                             entry->priv->cursor_position);
    }
  else if (g_strcmp0 (pspec->name, "selection-bound") == 0)
    {
      if (check_for_selection_change (entry, ctk_entry))
        g_signal_emit_by_name (atk_obj, "text-selection-changed");
    }
  else if (g_strcmp0 (pspec->name, "editable") == 0)
    {
      gboolean value;

      g_object_get (obj, "editable", &value, NULL);
      atk_object_notify_state_change (atk_obj, ATK_STATE_EDITABLE, value);
    }
  else if (g_strcmp0 (pspec->name, "visibility") == 0)
    {
      gboolean visibility;
      AtkRole new_role;

      visibility = ctk_entry_get_visibility (ctk_entry);
      new_role = visibility ? ATK_ROLE_TEXT : ATK_ROLE_PASSWORD_TEXT;
      atk_object_set_role (atk_obj, new_role);
    }
  else if (g_strcmp0 (pspec->name, "primary-icon-storage-type") == 0)
    {
      if (ctk_entry_get_icon_storage_type (ctk_entry, CTK_ENTRY_ICON_PRIMARY) != CTK_IMAGE_EMPTY && !priv->icons[CTK_ENTRY_ICON_PRIMARY])
        {
          priv->icons[CTK_ENTRY_ICON_PRIMARY] = ctk_entry_icon_accessible_new (entry, CTK_ENTRY_ICON_PRIMARY);
          g_signal_emit_by_name (entry, "children-changed::add", 0,
                                 priv->icons[CTK_ENTRY_ICON_PRIMARY], NULL);
        }
      else if (ctk_entry_get_icon_storage_type (ctk_entry, CTK_ENTRY_ICON_PRIMARY) == CTK_IMAGE_EMPTY && priv->icons[CTK_ENTRY_ICON_PRIMARY])
        {
          ctk_entry_icon_accessible_invalidate (CTK_ENTRY_ICON_ACCESSIBLE (priv->icons[CTK_ENTRY_ICON_PRIMARY]));
          g_signal_emit_by_name (entry, "children-changed::remove", 0,
                                 priv->icons[CTK_ENTRY_ICON_PRIMARY], NULL);
          g_clear_object (&priv->icons[CTK_ENTRY_ICON_PRIMARY]);
        }
    }
  else if (g_strcmp0 (pspec->name, "secondary-icon-storage-type") == 0)
    {
      gint index = (priv->icons[CTK_ENTRY_ICON_PRIMARY] ? 1 : 0);
      if (ctk_entry_get_icon_storage_type (ctk_entry, CTK_ENTRY_ICON_SECONDARY) != CTK_IMAGE_EMPTY && !priv->icons[CTK_ENTRY_ICON_SECONDARY])
        {
          priv->icons[CTK_ENTRY_ICON_SECONDARY] = ctk_entry_icon_accessible_new (entry, CTK_ENTRY_ICON_SECONDARY);
          g_signal_emit_by_name (entry, "children-changed::add", index,
                                 priv->icons[CTK_ENTRY_ICON_SECONDARY], NULL);
        }
      else if (ctk_entry_get_icon_storage_type (ctk_entry, CTK_ENTRY_ICON_SECONDARY) == CTK_IMAGE_EMPTY && priv->icons[CTK_ENTRY_ICON_SECONDARY])
        {
          ctk_entry_icon_accessible_invalidate (CTK_ENTRY_ICON_ACCESSIBLE (priv->icons[CTK_ENTRY_ICON_SECONDARY]));
          g_signal_emit_by_name (entry, "children-changed::remove", index,
                                 priv->icons[CTK_ENTRY_ICON_SECONDARY], NULL);
          g_clear_object (&priv->icons[CTK_ENTRY_ICON_SECONDARY]);
        }
    }
  else if (g_strcmp0 (pspec->name, "primary-icon-name") == 0)
    {
      if (priv->icons[CTK_ENTRY_ICON_PRIMARY])
        {
          const gchar *name;
          name = ctk_entry_get_icon_name (ctk_entry,
                                          CTK_ENTRY_ICON_PRIMARY);
          if (name)
            atk_object_set_name (priv->icons[CTK_ENTRY_ICON_PRIMARY], name);
        }
    }
  else if (g_strcmp0 (pspec->name, "secondary-icon-name") == 0)
    {
      if (priv->icons[CTK_ENTRY_ICON_SECONDARY])
        {
          const gchar *name;
          name = ctk_entry_get_icon_name (ctk_entry,
                                          CTK_ENTRY_ICON_SECONDARY);
          if (name)
            atk_object_set_name (priv->icons[CTK_ENTRY_ICON_SECONDARY], name);
        }
    }
  else if (g_strcmp0 (pspec->name, "primary-icon-tooltip-text") == 0)
    {
      if (priv->icons[CTK_ENTRY_ICON_PRIMARY])
        {
          gchar *text;
          text = ctk_entry_get_icon_tooltip_text (ctk_entry,
                                                    CTK_ENTRY_ICON_PRIMARY);
          if (text)
            {
              atk_object_set_description (priv->icons[CTK_ENTRY_ICON_PRIMARY],
                                      text);
              g_free (text);
            }
          else
            {
              atk_object_set_description (priv->icons[CTK_ENTRY_ICON_PRIMARY],
                                      "");
            }
        }
    }
  else if (g_strcmp0 (pspec->name, "secondary-icon-tooltip-text") == 0)
    {
      if (priv->icons[CTK_ENTRY_ICON_SECONDARY])
        {
          gchar *text;
          text = ctk_entry_get_icon_tooltip_text (ctk_entry,
                                                    CTK_ENTRY_ICON_SECONDARY);
          if (text)
            {
              atk_object_set_description (priv->icons[CTK_ENTRY_ICON_SECONDARY],
                                      text);
              g_free (text);
            }
          else
            {
              atk_object_set_description (priv->icons[CTK_ENTRY_ICON_SECONDARY],
                                      "");
            }
        }
    }
  else if (g_strcmp0 (pspec->name, "primary-icon-activatable") == 0)
    {
      if (priv->icons[CTK_ENTRY_ICON_PRIMARY])
        {
          gboolean on = ctk_entry_get_icon_activatable (ctk_entry, CTK_ENTRY_ICON_PRIMARY);
          atk_object_notify_state_change (priv->icons[CTK_ENTRY_ICON_PRIMARY],
                                          ATK_STATE_ENABLED, on);
        }
    }
  else if (g_strcmp0 (pspec->name, "secondary-icon-activatable") == 0)
    {
      if (priv->icons[CTK_ENTRY_ICON_SECONDARY])
        {
          gboolean on = ctk_entry_get_icon_activatable (ctk_entry, CTK_ENTRY_ICON_SECONDARY);
          atk_object_notify_state_change (priv->icons[CTK_ENTRY_ICON_SECONDARY],
                                          ATK_STATE_ENABLED, on);
        }
    }
  else if (g_strcmp0 (pspec->name, "primary-icon-sensitive") == 0)
    {
      if (priv->icons[CTK_ENTRY_ICON_PRIMARY])
        {
          gboolean on = ctk_entry_get_icon_sensitive (ctk_entry, CTK_ENTRY_ICON_PRIMARY);
          atk_object_notify_state_change (priv->icons[CTK_ENTRY_ICON_PRIMARY],
                                          ATK_STATE_SENSITIVE, on);
        }
    }
  else if (g_strcmp0 (pspec->name, "secondary-icon-sensitive") == 0)
    {
      if (priv->icons[CTK_ENTRY_ICON_SECONDARY])
        {
          gboolean on = ctk_entry_get_icon_sensitive (ctk_entry, CTK_ENTRY_ICON_SECONDARY);
          atk_object_notify_state_change (priv->icons[CTK_ENTRY_ICON_SECONDARY],
                                          ATK_STATE_SENSITIVE, on);
        }
    }
  else
    CTK_WIDGET_ACCESSIBLE_CLASS (ctk_entry_accessible_parent_class)->notify_ctk (obj, pspec);
}

static gint
ctk_entry_accessible_get_index_in_parent (AtkObject *accessible)
{
  /*
   * If the parent widget is a combo box then the index is 1
   * otherwise do the normal thing.
   */
  if (accessible->accessible_parent)
    if (CTK_IS_COMBO_BOX_ACCESSIBLE (accessible->accessible_parent))
      return 1;

  return ATK_OBJECT_CLASS (ctk_entry_accessible_parent_class)->get_index_in_parent (accessible);
}

static gint
ctk_entry_accessible_get_n_children (AtkObject* obj)
{
  CtkWidget *widget;
  CtkEntry *entry;
  gint count = 0;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (obj));
  if (widget == NULL)
    return 0;

  entry = CTK_ENTRY (widget);

  if (ctk_entry_get_icon_storage_type (entry, CTK_ENTRY_ICON_PRIMARY) != CTK_IMAGE_EMPTY)
    count++;
  if (ctk_entry_get_icon_storage_type (entry, CTK_ENTRY_ICON_SECONDARY) != CTK_IMAGE_EMPTY)
    count++;
  return count;
}

static AtkObject *
ctk_entry_accessible_ref_child (AtkObject *obj,
                                gint i)
{
  CtkEntryAccessible *accessible = CTK_ENTRY_ACCESSIBLE (obj);
  CtkEntryAccessiblePrivate *priv = accessible->priv;
  CtkWidget *widget;
  CtkEntry *entry;
  CtkEntryIconPosition pos;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (obj));
  if (widget == NULL)
    return NULL;

  entry = CTK_ENTRY (widget);

  switch (i)
    {
    case 0:
      if (ctk_entry_get_icon_storage_type (entry, CTK_ENTRY_ICON_PRIMARY) != CTK_IMAGE_EMPTY)
        pos = CTK_ENTRY_ICON_PRIMARY;
      else if (ctk_entry_get_icon_storage_type (entry, CTK_ENTRY_ICON_SECONDARY) != CTK_IMAGE_EMPTY)
        pos = CTK_ENTRY_ICON_SECONDARY;
      else
        return NULL;
      break;
    case 1:
      if (ctk_entry_get_icon_storage_type (entry, CTK_ENTRY_ICON_PRIMARY) == CTK_IMAGE_EMPTY)
        return NULL;
      if (ctk_entry_get_icon_storage_type (entry, CTK_ENTRY_ICON_SECONDARY) == CTK_IMAGE_EMPTY)
        return NULL;
      pos = CTK_ENTRY_ICON_SECONDARY;
      break;
    default:
      return NULL;
    }

  if (!priv->icons[pos])
    priv->icons[pos] = ctk_entry_icon_accessible_new (accessible, pos);
  return g_object_ref (priv->icons[pos]);
}

static void
ctk_entry_accessible_finalize (GObject *object)
{
  CtkEntryAccessible *entry = CTK_ENTRY_ACCESSIBLE (object);
  CtkEntryAccessiblePrivate *priv = entry->priv;

  g_clear_object (&priv->icons[CTK_ENTRY_ICON_PRIMARY]);
  g_clear_object (&priv->icons[CTK_ENTRY_ICON_SECONDARY]);

  G_OBJECT_CLASS (ctk_entry_accessible_parent_class)->finalize (object);
}

static void
ctk_entry_accessible_class_init (CtkEntryAccessibleClass *klass)
{
  AtkObjectClass  *class = ATK_OBJECT_CLASS (klass);
  CtkWidgetAccessibleClass *widget_class = (CtkWidgetAccessibleClass*)klass;
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  class->ref_state_set = ctk_entry_accessible_ref_state_set;
  class->get_index_in_parent = ctk_entry_accessible_get_index_in_parent;
  class->initialize = ctk_entry_accessible_initialize;
  class->get_attributes = ctk_entry_accessible_get_attributes;
  class->get_n_children = ctk_entry_accessible_get_n_children;
  class->ref_child = ctk_entry_accessible_ref_child;

  widget_class->notify_ctk = ctk_entry_accessible_notify_ctk;

  gobject_class->finalize = ctk_entry_accessible_finalize;
}

static void
ctk_entry_accessible_init (CtkEntryAccessible *entry)
{
  entry->priv = ctk_entry_accessible_get_instance_private (entry);
  entry->priv->cursor_position = 0;
  entry->priv->selection_bound = 0;
}

static gchar *
ctk_entry_accessible_get_text (AtkText *atk_text,
                               gint     start_pos,
                               gint     end_pos)
{
  CtkWidget *widget;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (atk_text));
  if (widget == NULL)
    return NULL;

  return _ctk_entry_get_display_text (CTK_ENTRY (widget), start_pos, end_pos);
}

static gchar *
ctk_entry_accessible_get_text_before_offset (AtkText         *text,
                                             gint             offset,
                                             AtkTextBoundary  boundary_type,
                                             gint            *start_offset,
                                             gint            *end_offset)
{
  CtkWidget *widget;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (text));
  if (widget == NULL)
    return NULL;

  return _ctk_pango_get_text_before (ctk_entry_get_layout (CTK_ENTRY (widget)),
                                     boundary_type, offset,
                                     start_offset, end_offset);
}

static gchar *
ctk_entry_accessible_get_text_at_offset (AtkText         *text,
                                         gint             offset,
                                         AtkTextBoundary  boundary_type,
                                         gint            *start_offset,
                                         gint            *end_offset)
{
  CtkWidget *widget;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (text));
  if (widget == NULL)
    return NULL;

  return _ctk_pango_get_text_at (ctk_entry_get_layout (CTK_ENTRY (widget)),
                                 boundary_type, offset,
                                 start_offset, end_offset);
}

static gchar *
ctk_entry_accessible_get_text_after_offset (AtkText         *text,
                                            gint             offset,
                                            AtkTextBoundary  boundary_type,
                                            gint            *start_offset,
                                            gint            *end_offset)
{
  CtkWidget *widget;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (text));
  if (widget == NULL)
    return NULL;

  return _ctk_pango_get_text_after (ctk_entry_get_layout (CTK_ENTRY (widget)),
                                    boundary_type, offset,
                                    start_offset, end_offset);
}

static gint
ctk_entry_accessible_get_character_count (AtkText *atk_text)
{
  CtkWidget *widget;
  gchar *text;
  glong char_count;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (atk_text));
  if (widget == NULL)
    return 0;

  text = _ctk_entry_get_display_text (CTK_ENTRY (widget), 0, -1);

  char_count = 0;
  if (text)
    {
      char_count = g_utf8_strlen (text, -1);
      g_free (text);
    }

  return char_count;
}

static gint
ctk_entry_accessible_get_caret_offset (AtkText *text)
{
  CtkWidget *widget;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (text));
  if (widget == NULL)
    return 0;

  return ctk_editable_get_position (CTK_EDITABLE (widget));
}

static gboolean
ctk_entry_accessible_set_caret_offset (AtkText *text,
                                       gint     offset)
{
  CtkWidget *widget;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (text));
  if (widget == NULL)
    return FALSE;

  ctk_editable_set_position (CTK_EDITABLE (widget), offset);

  return TRUE;
}

static AtkAttributeSet *
add_text_attribute (AtkAttributeSet  *attributes,
                    AtkTextAttribute  attr,
                    gint              i)
{
  AtkAttribute *at;

  at = g_new (AtkAttribute, 1);
  at->name = g_strdup (atk_text_attribute_get_name (attr));
  at->value = g_strdup (atk_text_attribute_get_value (attr, i));

  return g_slist_prepend (attributes, at);
}

static AtkAttributeSet *
ctk_entry_accessible_get_run_attributes (AtkText *text,
                                         gint     offset,
                                         gint    *start_offset,
                                         gint    *end_offset)
{
  CtkWidget *widget;
  AtkAttributeSet *attributes;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (text));
  if (widget == NULL)
    return NULL;

  attributes = NULL;
  attributes = add_text_attribute (attributes, ATK_TEXT_ATTR_DIRECTION,
                                   ctk_widget_get_direction (widget));
  attributes = _ctk_pango_get_run_attributes (attributes,
                                              ctk_entry_get_layout (CTK_ENTRY (widget)),
                                              offset,
                                              start_offset,
                                              end_offset);

  return attributes;
}

static AtkAttributeSet *
ctk_entry_accessible_get_default_attributes (AtkText *text)
{
  CtkWidget *widget;
  AtkAttributeSet *attributes;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (text));
  if (widget == NULL)
    return NULL;

  attributes = NULL;
  attributes = add_text_attribute (attributes, ATK_TEXT_ATTR_DIRECTION,
                                   ctk_widget_get_direction (widget));
  attributes = _ctk_pango_get_default_attributes (attributes,
                                                  ctk_entry_get_layout (CTK_ENTRY (widget)));
  attributes = _ctk_style_context_get_attributes (attributes,
                                                  ctk_widget_get_style_context (widget),
                                                  ctk_widget_get_state_flags (widget));

  return attributes;
}

static void
ctk_entry_accessible_get_character_extents (AtkText      *text,
                                            gint          offset,
                                            gint         *x,
                                            gint         *y,
                                            gint         *width,
                                            gint         *height,
                                            AtkCoordType  coords)
{
  CtkWidget *widget;
  CtkEntry *entry;
  PangoRectangle char_rect;
  gchar *entry_text;
  gint index, x_layout, y_layout;
  CdkWindow *window;
  gint x_window, y_window;
  CtkAllocation allocation;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (text));
  if (widget == NULL)
    return;

  entry = CTK_ENTRY (widget);

  ctk_entry_get_layout_offsets (entry, &x_layout, &y_layout);
  entry_text = _ctk_entry_get_display_text (entry, 0, -1);
  index = g_utf8_offset_to_pointer (entry_text, offset) - entry_text;
  g_free (entry_text);

  pango_layout_index_to_pos (ctk_entry_get_layout (entry), index, &char_rect);
  pango_extents_to_pixels (&char_rect, NULL);

  _ctk_widget_get_allocation (widget, &allocation);

  window = ctk_widget_get_window (widget);
  cdk_window_get_origin (window, &x_window, &y_window);

  *x = x_window + allocation.x + x_layout + char_rect.x;
  *y = y_window + allocation.y + y_layout + char_rect.y;
  *width = char_rect.width;
  *height = char_rect.height;

  if (coords == ATK_XY_WINDOW)
    {
      window = cdk_window_get_toplevel (window);
      cdk_window_get_origin (window, &x_window, &y_window);

      *x -= x_window;
      *y -= y_window;
    }
}

static gint
ctk_entry_accessible_get_offset_at_point (AtkText      *atk_text,
                                          gint          x,
                                          gint          y,
                                          AtkCoordType  coords)
{
  CtkWidget *widget;
  CtkEntry *entry;
  gint index, x_layout, y_layout;
  gint x_window, y_window;
  gint x_local, y_local;
  CdkWindow *window;
  glong offset;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (atk_text));
  if (widget == NULL)
    return -1;

  entry = CTK_ENTRY (widget);

  ctk_entry_get_layout_offsets (entry, &x_layout, &y_layout);

  window = ctk_widget_get_window (widget);
  cdk_window_get_origin (window, &x_window, &y_window);

  x_local = x - x_layout - x_window;
  y_local = y - y_layout - y_window;

  if (coords == ATK_XY_WINDOW)
    {
      window = cdk_window_get_toplevel (window);
      cdk_window_get_origin (window, &x_window, &y_window);

      x_local += x_window;
      y_local += y_window;
    }
  if (!pango_layout_xy_to_index (ctk_entry_get_layout (entry),
                                 x_local * PANGO_SCALE,
                                 y_local * PANGO_SCALE,
                                 &index, NULL))
    {
      if (x_local < 0 || y_local < 0)
        index = 0;
      else
        index = -1;
    }

  offset = -1;
  if (index != -1)
    {
      gchar *text;

      text = _ctk_entry_get_display_text (entry, 0, -1);
      offset = g_utf8_pointer_to_offset (text, text + index);
      g_free (text);
    }

  return offset;
}

static gint
ctk_entry_accessible_get_n_selections (AtkText *text)
{
  CtkWidget *widget;
  gint start, end;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (text));
  if (widget == NULL)
    return 0;

  if (ctk_editable_get_selection_bounds (CTK_EDITABLE (widget), &start, &end))
    return 1;

  return 0;
}

static gchar *
ctk_entry_accessible_get_selection (AtkText *text,
                                    gint     selection_num,
                                    gint    *start_pos,
                                    gint    *end_pos)
{
  CtkWidget *widget;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (text));
  if (widget == NULL)
    return NULL;

  if (selection_num != 0)
     return NULL;

  if (ctk_editable_get_selection_bounds (CTK_EDITABLE (widget), start_pos, end_pos))
    return ctk_editable_get_chars (CTK_EDITABLE (widget), *start_pos, *end_pos);

  return NULL;
}

static gboolean
ctk_entry_accessible_add_selection (AtkText *text,
                                    gint     start_pos,
                                    gint     end_pos)
{
  CtkEntry *entry;
  CtkWidget *widget;
  gint start, end;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (text));
  if (widget == NULL)
    return FALSE;

  entry = CTK_ENTRY (widget);

  if (!ctk_editable_get_selection_bounds (CTK_EDITABLE (entry), &start, &end))
    {
      ctk_editable_select_region (CTK_EDITABLE (entry), start_pos, end_pos);
      return TRUE;
    }
  else
    return FALSE;
}

static gboolean
ctk_entry_accessible_remove_selection (AtkText *text,
                                       gint     selection_num)
{
  CtkWidget *widget;
  gint start, end;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (text));
  if (widget == NULL)
    return FALSE;

  if (selection_num != 0)
     return FALSE;

  if (ctk_editable_get_selection_bounds (CTK_EDITABLE (widget), &start, &end))
    {
      ctk_editable_select_region (CTK_EDITABLE (widget), end, end);
      return TRUE;
    }
  else
    return FALSE;
}

static gboolean
ctk_entry_accessible_set_selection (AtkText *text,
                                    gint     selection_num,
                                    gint     start_pos,
                                    gint     end_pos)
{
  CtkWidget *widget;
  gint start, end;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (text));
  if (widget == NULL)
    return FALSE;

  if (selection_num != 0)
     return FALSE;

  if (ctk_editable_get_selection_bounds (CTK_EDITABLE (widget), &start, &end))
    {
      ctk_editable_select_region (CTK_EDITABLE (widget), start_pos, end_pos);
      return TRUE;
    }
  else
    return FALSE;
}

static gunichar
ctk_entry_accessible_get_character_at_offset (AtkText *atk_text,
                                              gint     offset)
{
  CtkWidget *widget;
  gchar *text;
  gunichar result;

  result = '\0';

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (atk_text));
  if (widget == NULL)
    return result;

  if (!ctk_entry_get_visibility (CTK_ENTRY (widget)))
    return result;

  text = _ctk_entry_get_display_text (CTK_ENTRY (widget), 0, -1);
  if (offset < g_utf8_strlen (text, -1))
    {
      gchar *index;

      index = g_utf8_offset_to_pointer (text, offset);
      result = g_utf8_get_char (index);
      g_free (text);
    }

  return result;
}

static void
atk_text_interface_init (AtkTextIface *iface)
{
  iface->get_text = ctk_entry_accessible_get_text;
  iface->get_character_at_offset = ctk_entry_accessible_get_character_at_offset;
  iface->get_text_before_offset = ctk_entry_accessible_get_text_before_offset;
  iface->get_text_at_offset = ctk_entry_accessible_get_text_at_offset;
  iface->get_text_after_offset = ctk_entry_accessible_get_text_after_offset;
  iface->get_caret_offset = ctk_entry_accessible_get_caret_offset;
  iface->set_caret_offset = ctk_entry_accessible_set_caret_offset;
  iface->get_character_count = ctk_entry_accessible_get_character_count;
  iface->get_n_selections = ctk_entry_accessible_get_n_selections;
  iface->get_selection = ctk_entry_accessible_get_selection;
  iface->add_selection = ctk_entry_accessible_add_selection;
  iface->remove_selection = ctk_entry_accessible_remove_selection;
  iface->set_selection = ctk_entry_accessible_set_selection;
  iface->get_run_attributes = ctk_entry_accessible_get_run_attributes;
  iface->get_default_attributes = ctk_entry_accessible_get_default_attributes;
  iface->get_character_extents = ctk_entry_accessible_get_character_extents;
  iface->get_offset_at_point = ctk_entry_accessible_get_offset_at_point;
}

static void
ctk_entry_accessible_set_text_contents (AtkEditableText *text,
                                        const gchar     *string)
{
  CtkWidget *widget;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (text));
  if (widget == NULL)
    return;

  if (!ctk_editable_get_editable (CTK_EDITABLE (widget)))
    return;

  ctk_entry_set_text (CTK_ENTRY (widget), string);
}

static void
ctk_entry_accessible_insert_text (AtkEditableText *text,
                                  const gchar     *string,
                                  gint             length,
                                  gint            *position)
{
  CtkWidget *widget;
  CtkEditable *editable;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (text));
  if (widget == NULL)
    return;

  editable = CTK_EDITABLE (widget);
  if (!ctk_editable_get_editable (editable))
    return;

  ctk_editable_insert_text (editable, string, length, position);
  ctk_editable_set_position (editable, *position);
}

static void
ctk_entry_accessible_copy_text (AtkEditableText *text,
                                gint             start_pos,
                                gint             end_pos)
{
  CtkWidget *widget;
  CtkEditable *editable;
  gchar *str;
  CtkClipboard *clipboard;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (text));
  if (widget == NULL)
    return;

  if (!ctk_widget_has_screen (widget))
    return;

  editable = CTK_EDITABLE (widget);
  str = ctk_editable_get_chars (editable, start_pos, end_pos);
  clipboard = ctk_widget_get_clipboard (widget, CDK_SELECTION_CLIPBOARD);
  ctk_clipboard_set_text (clipboard, str, -1);
  g_free (str);
}

static void
ctk_entry_accessible_cut_text (AtkEditableText *text,
                               gint             start_pos,
                               gint             end_pos)
{
  CtkWidget *widget;
  CtkEditable *editable;
  gchar *str;
  CtkClipboard *clipboard;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (text));
  if (widget == NULL)
    return;

  if (!ctk_widget_has_screen (widget))
    return;

  editable = CTK_EDITABLE (widget);
  if (!ctk_editable_get_editable (editable))
    return;

  str = ctk_editable_get_chars (editable, start_pos, end_pos);
  clipboard = ctk_widget_get_clipboard (widget, CDK_SELECTION_CLIPBOARD);
  ctk_clipboard_set_text (clipboard, str, -1);
  ctk_editable_delete_text (editable, start_pos, end_pos);
}

static void
ctk_entry_accessible_delete_text (AtkEditableText *text,
                                  gint             start_pos,
                                  gint             end_pos)
{
  CtkWidget *widget;
  CtkEditable *editable;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (text));
  if (widget == NULL)
    return;

  editable = CTK_EDITABLE (widget);
  if (!ctk_editable_get_editable (editable))
    return;

  ctk_editable_delete_text (editable, start_pos, end_pos);
}

typedef struct
{
  CtkEntry* entry;
  gint position;
} PasteData;

static void
paste_received_cb (CtkClipboard *clipboard G_GNUC_UNUSED,
                   const gchar  *text,
                   gpointer      data)
{
  PasteData *paste = data;

  if (text)
    ctk_editable_insert_text (CTK_EDITABLE (paste->entry), text, -1,
                              &paste->position);

  g_object_unref (paste->entry);
  g_free (paste);
}

static void
ctk_entry_accessible_paste_text (AtkEditableText *text,
                                 gint             position)
{
  CtkWidget *widget;
  CtkEditable *editable;
  PasteData *paste;
  CtkClipboard *clipboard;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (text));
  if (widget == NULL)
    return;

  if (!ctk_widget_has_screen (widget))
    return;

  editable = CTK_EDITABLE (widget);
  if (!ctk_editable_get_editable (editable))
    return;

  paste = g_new0 (PasteData, 1);
  paste->entry = CTK_ENTRY (widget);
  paste->position = position;

  g_object_ref (paste->entry);
  clipboard = ctk_widget_get_clipboard (widget, CDK_SELECTION_CLIPBOARD);
  ctk_clipboard_request_text (clipboard, paste_received_cb, paste);
}

static void
atk_editable_text_interface_init (AtkEditableTextIface *iface)
{
  iface->set_text_contents = ctk_entry_accessible_set_text_contents;
  iface->insert_text = ctk_entry_accessible_insert_text;
  iface->copy_text = ctk_entry_accessible_copy_text;
  iface->cut_text = ctk_entry_accessible_cut_text;
  iface->delete_text = ctk_entry_accessible_delete_text;
  iface->paste_text = ctk_entry_accessible_paste_text;
  iface->set_run_attributes = NULL;
}

static void
insert_text_cb (CtkEditable *editable,
                gchar       *new_text,
                gint         new_text_length,
                gint        *position)
{
  CtkEntryAccessible *accessible;
  gint length;

  if (new_text_length == 0)
    return;

  accessible = CTK_ENTRY_ACCESSIBLE (ctk_widget_get_accessible (CTK_WIDGET (editable)));
  length = g_utf8_strlen (new_text, new_text_length);

  g_signal_emit_by_name (accessible,
                         "text-changed::insert",
                         *position - length,
                          length);
}

/* We connect to CtkEditable::delete-text, since it carries
 * the information we need. But we delay emitting our own
 * text_changed::delete signal until the entry has update
 * all its internal state and emits CtkEntry::changed.
 */
static void
delete_text_cb (CtkEditable *editable,
                gint         start,
                gint         end)
{
  CtkEntryAccessible *accessible;

  accessible = CTK_ENTRY_ACCESSIBLE (ctk_widget_get_accessible (CTK_WIDGET (editable)));

  if (end < 0)
    {
      gchar *text;

      text = _ctk_entry_get_display_text (CTK_ENTRY (editable), 0, -1);
      end = g_utf8_strlen (text, -1);
      g_free (text);
    }

  if (end == start)
    return;

  g_signal_emit_by_name (accessible,
                         "text-changed::delete",
                         start,
                         end - start);
}

static gboolean
check_for_selection_change (CtkEntryAccessible *accessible,
                            CtkEntry           *entry)
{
  gboolean ret_val = FALSE;
  gint start, end;

  if (ctk_editable_get_selection_bounds (CTK_EDITABLE (entry), &start, &end))
    {
      if (end != accessible->priv->cursor_position ||
          start != accessible->priv->selection_bound)
        /*
         * This check is here as this function can be called
         * for notification of selection_bound and current_pos.
         * The values of current_pos and selection_bound may be the same
         * for both notifications and we only want to generate one
         * text_selection_changed signal.
         */
        ret_val = TRUE;
    }
  else
    {
      /* We had a selection */
      ret_val = (accessible->priv->cursor_position != accessible->priv->selection_bound);
    }

  accessible->priv->cursor_position = end;
  accessible->priv->selection_bound = start;

  return ret_val;
}

static gboolean
ctk_entry_accessible_do_action (AtkAction *action,
                                gint       i)
{
  CtkWidget *widget;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (action));
  if (widget == NULL)
    return FALSE;

  if (!ctk_widget_get_sensitive (widget) || !ctk_widget_get_visible (widget))
    return FALSE;

  if (i != 0)
    return FALSE;

  ctk_widget_activate (widget);

  return TRUE;
}

static gint
ctk_entry_accessible_get_n_actions (AtkAction *action G_GNUC_UNUSED)
{
  return 1;
}

static const gchar *
ctk_entry_accessible_get_keybinding (AtkAction *action,
                                     gint       i)
{
  CtkWidget *widget;
  CtkWidget *label;
  AtkRelationSet *set;
  AtkRelation *relation;
  gpointer target_object;
  guint key_val;

  if (i != 0)
    return NULL;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (action));
  if (widget == NULL)
    return NULL;

  set = atk_object_ref_relation_set (ATK_OBJECT (action));
  if (!set)
    return NULL;

  label = NULL;
  relation = atk_relation_set_get_relation_by_type (set, ATK_RELATION_LABELLED_BY);
  if (relation)
    {
      GPtrArray *target;

      target = atk_relation_get_target (relation);

      target_object = g_ptr_array_index (target, 0);
      label = ctk_accessible_get_widget (CTK_ACCESSIBLE (target_object));
    }

  g_object_unref (set);

  if (CTK_IS_LABEL (label))
    {
      key_val = ctk_label_get_mnemonic_keyval (CTK_LABEL (label));
      if (key_val != CDK_KEY_VoidSymbol)
        return ctk_accelerator_name (key_val, CDK_MOD1_MASK);
    }

  return NULL;
}

static const gchar*
ctk_entry_accessible_action_get_name (AtkAction *action G_GNUC_UNUSED,
                                      gint       i)
{
  if (i == 0)
    return "activate";
  return NULL;
}

static const gchar*
ctk_entry_accessible_action_get_localized_name (AtkAction *action G_GNUC_UNUSED,
                                                gint       i)
{
  if (i == 0)
    return C_("Action name", "Activate");
  return NULL;
}

static const gchar*
ctk_entry_accessible_action_get_description (AtkAction *action G_GNUC_UNUSED,
                                             gint       i)
{
  if (i == 0)
    return C_("Action description", "Activates the entry");
  return NULL;
}

static void
atk_action_interface_init (AtkActionIface *iface)
{
  iface->do_action = ctk_entry_accessible_do_action;
  iface->get_n_actions = ctk_entry_accessible_get_n_actions;
  iface->get_keybinding = ctk_entry_accessible_get_keybinding;
  iface->get_name = ctk_entry_accessible_action_get_name;
  iface->get_localized_name = ctk_entry_accessible_action_get_localized_name;
  iface->get_description = ctk_entry_accessible_action_get_description;
}