aboutsummaryrefslogtreecommitdiffstats
path: root/lib/widgets/ephy-location-entry.c
blob: 77826374e99145f1b1e1721f5f2161b5b11baa61 (plain) (blame)
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
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Copyright © 2002  Ricardo Fernández Pascual
 *  Copyright © 2003, 2004  Marco Pesenti Gritti
 *  Copyright © 2003, 2004, 2005  Christian Persch
 *  Copyright © 2008  Xan López
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "config.h"

#include "ephy-tree-model-node.h"
#include "ephy-location-entry.h"
#include "ephy-marshal.h"
#include "ephy-signal-accumulator.h"
#include "ephy-dnd.h"
#include "egg-editable-toolbar.h"
#include "ephy-stock-icons.h"
#include "ephy-debug.h"
#include "ephy-gui.h"

#include <glib/gi18n.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>

#include <string.h>

#define EPHY_LOCATION_ENTRY_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_LOCATION_ENTRY, EphyLocationEntryPrivate))

struct _EphyLocationEntryPrivate
{
    GtkWidget *entry;
    char *lock_stock_id;
    GdkPixbuf *favicon;
    GdkColor secure_bg_colour;
    GdkColor secure_fg_colour;

    GSList *search_terms;

    char *before_completion;
    char *saved_text;

    guint text_col;
    guint action_col;
    guint keywords_col;
    guint relevance_col;
    guint url_col;
    guint extra_col;
    guint favicon_col;

    guint hash;

    guint user_changed : 1;
    guint can_redo : 1;
    guint block_update : 1;
    guint original_address : 1;
    guint secure : 1;
    guint apply_colours : 1;
    guint needs_reset : 1;
    guint show_lock : 1;
};

static const GtkTargetEntry url_drag_types [] =
{
    { EPHY_DND_URL_TYPE,        0, 0 },
    { EPHY_DND_URI_LIST_TYPE,   0, 1 },
    { EPHY_DND_TEXT_TYPE,       0, 2 }
};

static const GdkColor fallback_bg_colour = { 0, 0xf7f7, 0xf7f7, 0xbebe }; /* yellow-ish */
static const GdkColor fallback_fg_colour = { 0, 0, 0, 0 }; /* black */

static void ephy_location_entry_class_init (EphyLocationEntryClass *klass);
static void ephy_location_entry_init (EphyLocationEntry *le);
static gboolean ephy_location_entry_reset_internal (EphyLocationEntry *, gboolean);

static void textcell_data_func (GtkCellLayout *cell_layout,
                GtkCellRenderer *cell,
                GtkTreeModel *tree_model,
                GtkTreeIter *iter,
                gpointer data);
static void extracell_data_func (GtkCellLayout *cell_layout,
                 GtkCellRenderer *cell,
                 GtkTreeModel *tree_model,
                 GtkTreeIter *iter,
                 gpointer data);

enum signalsEnum
{
    USER_CHANGED,
    LOCK_CLICKED,
    GET_LOCATION,
    GET_TITLE,
    LAST_SIGNAL
};
static gint signals[LAST_SIGNAL] = { 0 };

G_DEFINE_TYPE (EphyLocationEntry, ephy_location_entry, GTK_TYPE_TOOL_ITEM)

static void
ephy_location_entry_style_set (GtkWidget *widget,
                   GtkStyle *previous_style)
{
    EphyLocationEntry *entry = EPHY_LOCATION_ENTRY (widget);
    EphyLocationEntryPrivate *priv = entry->priv;
    GtkSettings *settings;
    GdkColor *bg_colour = NULL, *fg_colour = NULL;
    GdkColor title_fg_colour;
    char *theme;
    gboolean is_a11y_theme;

    if (GTK_WIDGET_CLASS (ephy_location_entry_parent_class)->style_set)
    {
        GTK_WIDGET_CLASS (ephy_location_entry_parent_class)->style_set (widget, previous_style);
    }

    title_fg_colour = gtk_widget_get_style (widget)->text[GTK_STATE_INSENSITIVE];

    settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
    g_object_get (settings, "gtk-theme-name", &theme, NULL);
    is_a11y_theme = strstr (theme, "HighContrast") != NULL ||
            strstr (theme, "LowContrast") != NULL;
    g_free (theme);

    gtk_widget_style_get (widget,
                  "secure-fg-color", &fg_colour,
                  "secure-bg-color", &bg_colour,
                  NULL);

    /* We only use the fallback colours when we don't have an a11y theme */
    priv->apply_colours = !is_a11y_theme || (fg_colour != NULL && bg_colour != NULL);

    if (fg_colour != NULL)
    {
        priv->secure_fg_colour = *fg_colour;
        gdk_color_free (fg_colour);
    }
    else
    {
        priv->secure_fg_colour = fallback_fg_colour;
    }

    if (bg_colour != NULL)
    {
        priv->secure_bg_colour = *bg_colour;
        gdk_color_free (bg_colour);
    }
    else
    {
        priv->secure_bg_colour = fallback_bg_colour;
    }

    /* Apply the new style */
    ephy_location_entry_set_secure (entry, priv->secure);
}

inline static void
free_search_terms (GSList *search_terms)
{
    GSList *iter;
    
    for (iter = search_terms; iter != NULL; iter = iter->next)
        g_regex_unref ((GRegex*)iter->data);
    
    g_slist_free (search_terms);
}

static void
ephy_location_entry_finalize (GObject *object)
{
    EphyLocationEntry *entry = EPHY_LOCATION_ENTRY (object);
    EphyLocationEntryPrivate *priv = entry->priv;
    
    g_free (priv->saved_text);

    if (priv->favicon != NULL)
    {
        g_object_unref (priv->favicon);
    }
    
    if (priv->search_terms)
    {
        free_search_terms (priv->search_terms);
        priv->search_terms = NULL;
    }

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

static void
ephy_location_entry_class_init (EphyLocationEntryClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
    GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

    object_class->finalize = ephy_location_entry_finalize;

    widget_class->style_set = ephy_location_entry_style_set;

    signals[USER_CHANGED] = g_signal_new (
        "user_changed", G_OBJECT_CLASS_TYPE (klass),
        G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST,
        G_STRUCT_OFFSET (EphyLocationEntryClass, user_changed),
        NULL, NULL,
        g_cclosure_marshal_VOID__VOID,
        G_TYPE_NONE,
        0,
        G_TYPE_NONE);

    signals[LOCK_CLICKED] = g_signal_new (
        "lock-clicked",
        EPHY_TYPE_LOCATION_ENTRY,
        G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST,
        G_STRUCT_OFFSET (EphyLocationEntryClass, lock_clicked),
        NULL, NULL,
        g_cclosure_marshal_VOID__VOID,
        G_TYPE_NONE,
        0);

    signals[GET_LOCATION] = g_signal_new (
        "get-location", G_OBJECT_CLASS_TYPE (klass),
        G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST,
        G_STRUCT_OFFSET (EphyLocationEntryClass, get_location),
        ephy_signal_accumulator_string, NULL,
        ephy_marshal_STRING__VOID,
        G_TYPE_STRING,
        0,
        G_TYPE_NONE);

    signals[GET_TITLE] = g_signal_new (
        "get-title", G_OBJECT_CLASS_TYPE (klass),
        G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST,
        G_STRUCT_OFFSET (EphyLocationEntryClass, get_title),
        ephy_signal_accumulator_string, NULL,
        ephy_marshal_STRING__VOID,
        G_TYPE_STRING,
        0,
        G_TYPE_NONE);

    gtk_widget_class_install_style_property (widget_class,
                         g_param_spec_boxed ("secure-bg-color",
                                     "Secure background colour",
                                     "Background colour to use for secure sites",
                                     GDK_TYPE_COLOR,
                                     G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));

    gtk_widget_class_install_style_property (widget_class,
                         g_param_spec_boxed ("secure-fg-color",
                                     "Secure foreground Colour",
                                     "Foreground colour to use for secure sites",
                                     GDK_TYPE_COLOR,
                                     G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));

    g_type_class_add_private (object_class, sizeof (EphyLocationEntryPrivate));
}

static void
update_address_state (EphyLocationEntry *entry)
{
    EphyLocationEntryPrivate *priv = entry->priv;
    const char *text;

    text = gtk_entry_get_text (GTK_ENTRY (priv->entry));
    priv->original_address = text != NULL &&
                 g_str_hash (text) == priv->hash;
}

static void
update_favicon (EphyLocationEntry *lentry)
{
    EphyLocationEntryPrivate *priv = lentry->priv;
    GtkEntry *entry = GTK_ENTRY (priv->entry);

    /* Only show the favicon if the entry's text is the
     * address of the current page.
     */
    if (priv->favicon != NULL && priv->original_address)
    {
        gtk_entry_set_icon_from_pixbuf (entry,
                        GTK_ENTRY_ICON_PRIMARY,
                        priv->favicon);
    }
    else
    {
        /* Here we could consider using fallback favicon that matches
         * the page MIME type, though text/html should be good enough
         * most of the time. See #337140
         */
        gtk_entry_set_icon_from_icon_name (entry,
                           GTK_ENTRY_ICON_PRIMARY,
                           "text-html");
    }
}

static void
editable_changed_cb (GtkEditable *editable,
             EphyLocationEntry *entry)
{
    EphyLocationEntryPrivate *priv = entry->priv;
    const char *text;
    char *pattern;

    update_address_state (entry);

    if (priv->block_update == TRUE) 
        return;
    else
    {
        priv->user_changed = TRUE;
        priv->can_redo = FALSE;
    }   
    
    if (priv->search_terms)
    {
        free_search_terms (priv->search_terms);
        priv->search_terms = NULL;
    }

    text = gtk_entry_get_text (GTK_ENTRY (editable));

    /*
     * user is specifying a regular expression, so we will
     * have only one search term
     */
    if (g_str_has_prefix (text, "re:"))
    {
        GRegex *regex;
        pattern = g_strdup (text+3);
        regex = g_regex_new (pattern,
                     G_REGEX_CASELESS | G_REGEX_OPTIMIZE,
                     G_REGEX_MATCH_NOTEMPTY, NULL);
        priv->search_terms = g_slist_append (priv->search_terms, regex);
        g_free (pattern);
    }
    else
    {
        const char *current;
        const char *ptr;
        char *tmp;
        char *term;
        GRegex *term_regex;
        GRegex *quote_regex;
        gint count;
        gboolean inside_quotes = FALSE;

        quote_regex = g_regex_new ("\"", G_REGEX_OPTIMIZE,
                       G_REGEX_MATCH_NOTEMPTY, NULL);
        
        /*
         * This code loops through the string using pointer arythmetics.
         * Although the string we are handling may contain UTF-8 chars
         * this works because only ASCII chars affect what is actually
         * copied from the string as a search term.
         */
        for (count = 0, current = ptr = text; ptr[0] != '\0'; ptr++, count++)
        {
            /*
             * If we found a double quote character; we will 
             * consume bytes up until the next quote, or
             * end of line;
             */
            if (ptr[0] == '"')
                inside_quotes = !inside_quotes;

            /*
             * If we found a space, and we are not looking for a
             * closing double quote, or if the next char is the
             * end of the string, append what we have already as
             * a search term.
             */
            if (((ptr[0] == ' ') && (!inside_quotes)) || ptr[1] == '\0')
            {
                /*
                 * We special-case the end of the line because
                 * we would otherwise not copy the last character
                 * of the search string, since the for loop will
                 * stop before that.
                 */
                if (ptr[1] == '\0')
                    count++;
                
                /*
                 * remove quotes, and quote any regex-sensitive
                 * characters
                 */
                tmp = g_regex_escape_string (current, count);
                term = g_regex_replace (quote_regex, tmp, -1, 0,
                            "", G_REGEX_MATCH_NOTEMPTY, NULL);
                g_strstrip (term);
                g_free (tmp);

                /* we don't want empty search terms */
                if (term[0] != '\0')
                {
                    term_regex = g_regex_new (term,
                                  G_REGEX_CASELESS | G_REGEX_OPTIMIZE,
                                  G_REGEX_MATCH_NOTEMPTY, NULL);
                    priv->search_terms = g_slist_append (priv->search_terms, term_regex);
                }
                g_free (term);

                 /* count will be incremented by the for loop */
                count = -1;
                current = ptr + 1;
            }
        }

        g_regex_unref (quote_regex);
    }

    update_favicon (entry);

    g_signal_emit (entry, signals[USER_CHANGED], 0);
}

static gboolean
entry_key_press_cb (GtkEntry *entry,
            GdkEventKey *event,
            EphyLocationEntry *lentry)
{
    guint state = event->state & gtk_accelerator_get_default_mod_mask ();

    if (event->keyval == GDK_Escape && state == 0)
    {
        ephy_location_entry_reset_internal (lentry, TRUE);
        /* don't return TRUE since we want to cancel the autocompletion popup too */
    }

    return FALSE;
}

static gboolean
entry_key_press_after_cb (GtkEntry *entry,
              GdkEventKey *event,
              EphyLocationEntry *lentry)
{
    EphyLocationEntryPrivate *priv = lentry->priv;

    guint state = event->state & gtk_accelerator_get_default_mod_mask ();

    if ((event->keyval == GDK_Return ||
         event->keyval == GDK_KP_Enter ||
         event->keyval == GDK_ISO_Enter) &&
        (state == GDK_CONTROL_MASK ||
         state == (GDK_CONTROL_MASK | GDK_SHIFT_MASK)))
    {
        //gtk_im_context_reset (entry->im_context);

        priv->needs_reset = TRUE;
        g_signal_emit_by_name (entry, "activate");

        return TRUE;
    }
    
    if ((event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
        && state == 0)
    {
        /* If we are focusing the entry, with the cursor at the end of it
         * we emit the changed signal, so that the completion popup appears */
        const char *string;
        
        string = gtk_entry_get_text (entry);
        if (gtk_editable_get_position (GTK_EDITABLE (entry)) == strlen (string))
        {
            g_signal_emit_by_name (entry, "changed", 0);
            return TRUE;
        }
    }

    return FALSE;
}

static void
entry_activate_after_cb (GtkEntry *entry,
             EphyLocationEntry *lentry)
{
    EphyLocationEntryPrivate *priv = lentry->priv;
    
    priv->user_changed = FALSE;

    if (priv->needs_reset)
    {
        ephy_location_entry_reset_internal (lentry, TRUE);
        priv->needs_reset = FALSE;
    }
}

static gboolean
match_selected_cb (GtkEntryCompletion *completion,
           GtkTreeModel *model,
           GtkTreeIter *iter,
           EphyLocationEntry *entry)
{
    EphyLocationEntryPrivate *priv = entry->priv;
    char *item = NULL;
    guint state;

    gtk_tree_model_get (model, iter,
                priv->action_col, &item, -1);
    if (item == NULL) return FALSE;

    ephy_gui_get_current_event (NULL, &state, NULL);

    priv->needs_reset = (state == GDK_CONTROL_MASK ||
                 state == (GDK_CONTROL_MASK | GDK_SHIFT_MASK));

    ephy_location_entry_set_location (entry, item, NULL);
    //gtk_im_context_reset (GTK_ENTRY (entry->priv->entry)->im_context);
    g_signal_emit_by_name (priv->entry, "activate");

    g_free (item);

    return TRUE;
}

static void
action_activated_after_cb (GtkEntryCompletion *completion,
               gint index,
               EphyLocationEntry *lentry)
{
    guint state, button;

    ephy_gui_get_current_event (NULL, &state, &button);
    if ((state == GDK_CONTROL_MASK ||
         state == (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) ||
        button == 2)
    {
        ephy_location_entry_reset_internal (lentry, TRUE);
    }
}

static gboolean
toolbar_is_editable (GtkWidget *widget)
{
    GtkWidget *etoolbar;

    etoolbar = gtk_widget_get_ancestor (widget, EGG_TYPE_EDITABLE_TOOLBAR);

    if (etoolbar)
    {
        return egg_editable_toolbar_get_edit_mode
            (EGG_EDITABLE_TOOLBAR (etoolbar));
    }

    return FALSE;
}

static gboolean
entry_drag_motion_cb (GtkWidget        *widget,
              GdkDragContext   *context,
              gint              x,
              gint              y,
              guint             time)
{
    if (toolbar_is_editable (widget))
    {
        g_signal_stop_emission_by_name (widget, "drag_motion");
    }

    return FALSE;
}

static gboolean
entry_drag_drop_cb (GtkWidget          *widget,
            GdkDragContext     *context,
            gint                x,
            gint                y,
            guint               time)
{
    if (toolbar_is_editable (widget))
    {
        g_signal_stop_emission_by_name (widget, "drag_drop");
    }

    return FALSE;
}

static void
entry_clear_activate_cb (GtkMenuItem *item,
             EphyLocationEntry *entry)
{
    EphyLocationEntryPrivate *priv = entry->priv;

    priv->block_update = TRUE;
    gtk_entry_set_text (GTK_ENTRY (priv->entry), "");
    priv->block_update = FALSE;
    priv->user_changed = TRUE;
}

static void
entry_redo_activate_cb (GtkMenuItem *item,
            EphyLocationEntry *entry)
{
    ephy_location_entry_undo_reset (entry);
}

static void
entry_undo_activate_cb (GtkMenuItem *item,
            EphyLocationEntry *entry)
{
    ephy_location_entry_reset_internal (entry, FALSE);
}

static void
entry_populate_popup_cb (GtkEntry *entry,
             GtkMenu *menu,
             EphyLocationEntry *lentry)
{
    EphyLocationEntryPrivate *priv = lentry->priv;
    GtkWidget *image;
    GtkWidget *clear_menuitem, *undo_menuitem, *redo_menuitem, *separator;
    GList *children, *item;
    int pos = 0, sep = 0;
    gboolean is_editable;

    /* Clear and Copy mnemonics conflict, make custom menuitem */
    image = gtk_image_new_from_stock (GTK_STOCK_CLEAR, GTK_ICON_SIZE_MENU);
    gtk_widget_show (image);

    /* Translators: the mnemonic shouldn't conflict with any of the
     * standard items in the GtkEntry context menu (Cut, Copy, Paste, Delete,
     * Select All, Input Methods and Insert Unicode control character.)
     */
    clear_menuitem = gtk_image_menu_item_new_with_mnemonic (_("Cl_ear"));
    gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (clear_menuitem), image);
    g_signal_connect (clear_menuitem , "activate",
              G_CALLBACK (entry_clear_activate_cb), lentry);
    is_editable = gtk_editable_get_editable (GTK_EDITABLE (priv->entry));
    gtk_widget_set_sensitive (clear_menuitem, is_editable);
    gtk_widget_show (clear_menuitem);

    /* search for the 2nd separator (the one after Select All) in the context
     * menu, and insert this menu item before it.
     * It's a bit of a hack, but there seems to be no better way to do it :/
     */
    children = gtk_container_get_children (GTK_CONTAINER (menu));
    for (item = children; item != NULL && sep < 2; item = item->next, pos++)
    {
        if (GTK_IS_SEPARATOR_MENU_ITEM (item->data)) sep++;
    }

    gtk_menu_shell_insert (GTK_MENU_SHELL (menu), clear_menuitem, pos - 1);
    
    undo_menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_UNDO, NULL);
    gtk_widget_set_sensitive (undo_menuitem, priv->user_changed);
    g_signal_connect (undo_menuitem, "activate",
              G_CALLBACK (entry_undo_activate_cb), lentry);
    gtk_widget_show (undo_menuitem);
    gtk_menu_shell_insert (GTK_MENU_SHELL (menu), undo_menuitem, 0);
    
    redo_menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_REDO, NULL);
    gtk_widget_set_sensitive (redo_menuitem, priv->can_redo);
    g_signal_connect (redo_menuitem, "activate",
              G_CALLBACK (entry_redo_activate_cb), lentry);
    gtk_widget_show (redo_menuitem);
    gtk_menu_shell_insert (GTK_MENU_SHELL (menu), redo_menuitem, 1);
    
    separator = gtk_separator_menu_item_new ();
    gtk_widget_show (separator);
    gtk_menu_shell_insert (GTK_MENU_SHELL (menu), separator, 2);
}

static void
each_url_get_data_binder (EphyDragEachSelectedItemDataGet iteratee,
              gpointer iterator_context,
              gpointer return_data)
{
    EphyLocationEntry *entry = EPHY_LOCATION_ENTRY (iterator_context);
    char *title = NULL, *address = NULL;

    g_signal_emit (entry, signals[GET_LOCATION], 0, &address);
    g_signal_emit (entry, signals[GET_TITLE], 0, &title);
    g_return_if_fail (address != NULL && title != NULL);

    iteratee (address, title, return_data);

    g_free (address);
    g_free (title);
}

#define DRAG_ICON_LAYOUT_BORDER     2
#define DRAG_ICON_ICON_SPACING      DRAG_ICON_LAYOUT_BORDER * 2
#define DRAG_ICON_MAX_WIDTH_CHARS   32

static GdkPixmap *
favicon_create_drag_pixmap (EphyLocationEntry *entry,
                GtkWidget *widget)
{
    EphyLocationEntryPrivate *priv = entry->priv;
    char *title = NULL, *address = NULL;
    GString *text;
    GtkStyle *style;
    GdkDrawable *drawable;
    PangoContext *context;
    PangoLayout  *layout;
    PangoFontMetrics *metrics;
    int pixmap_height, pixmap_width;
    int layout_width, layout_height;
    int icon_width = 0, icon_height = 0, offset_x = 0;
    int char_width;

    g_signal_emit (entry, signals[GET_LOCATION], 0, &address);
    g_signal_emit (entry, signals[GET_TITLE], 0, &title);
    if (address == NULL || title == NULL) return NULL;

    /* Compute text */
    title = g_strstrip (title);

    text = g_string_sized_new (strlen (address) + strlen (title) + 2);
    if (title[0] != '\0')
    {
        g_string_append (text, title);
        g_string_append (text, "\n");
    }

    if (address[0] != '\0')
    {
        g_string_append (text, address);
    }

    /* Now build the pixmap */

    if (priv->favicon != NULL)
    {
        icon_width = gdk_pixbuf_get_width (priv->favicon);
        icon_height = gdk_pixbuf_get_height (priv->favicon);
    }

    context = gtk_widget_get_pango_context (widget);
    layout = pango_layout_new (context);

    context = gtk_widget_get_pango_context (widget);
    style = gtk_widget_get_style (widget);
    metrics = pango_context_get_metrics (context,
                         style->font_desc,
                         pango_context_get_language (context));

    char_width = pango_font_metrics_get_approximate_digit_width (metrics);
    pango_font_metrics_unref (metrics);

    pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
    pango_layout_set_width (layout, char_width * DRAG_ICON_MAX_WIDTH_CHARS);
    pango_layout_set_text (layout, text->str, text->len);

    pango_layout_get_size (layout, &layout_width, &layout_height);

    pixmap_width  = layout_width  / PANGO_SCALE + DRAG_ICON_LAYOUT_BORDER * 2;

    if (priv->favicon != NULL)
    {
        offset_x = icon_width + 2 * DRAG_ICON_ICON_SPACING;
        pixmap_width += offset_x;
        pixmap_height = MAX (layout_height / PANGO_SCALE, icon_height) + DRAG_ICON_LAYOUT_BORDER * 2;
    }
    else
    {
        pixmap_height = layout_height / PANGO_SCALE + DRAG_ICON_LAYOUT_BORDER * 2;
    }

    drawable = gdk_pixmap_new (gtk_widget_get_window (widget),
                   pixmap_width  + 2,
                   pixmap_height + 2,
                   -1);

    gdk_draw_rectangle (drawable,
                style->base_gc [gtk_widget_get_state (widget)],
                TRUE,
                0, 0,
                pixmap_width + 1,
                pixmap_height + 1);


    if (priv->favicon != NULL)
    {
        gdk_draw_pixbuf (drawable,
                 style->fg_gc[gtk_widget_get_state (widget)],
                 priv->favicon,
                 0, 0, 
                 1 + DRAG_ICON_LAYOUT_BORDER + DRAG_ICON_ICON_SPACING,
                 1 + DRAG_ICON_LAYOUT_BORDER + (pixmap_height - icon_height) / 2,
                 -1, -1,
                 GDK_RGB_DITHER_NONE, 0, 0);
    }

    gdk_draw_layout (drawable,
             style->text_gc [gtk_widget_get_state (widget)],
             1 + DRAG_ICON_LAYOUT_BORDER + offset_x,
             1 + DRAG_ICON_LAYOUT_BORDER,
             layout);

    gdk_draw_rectangle (drawable,
                style->black_gc,
                FALSE,
                0, 0,
                pixmap_width + 1,
                pixmap_height + 1);

    g_object_unref (layout);

    g_free (address);
    g_free (title);
    g_string_free (text,TRUE);

    return drawable;
}

static void
favicon_drag_begin_cb (GtkWidget *widget,
               GdkDragContext *context,
               EphyLocationEntry *lentry)
{
    GdkPixmap *pixmap;
    GtkEntry *entry;
    gint index;

    entry = GTK_ENTRY (widget);
    
    index = gtk_entry_get_current_icon_drag_source (entry);
    if (index != GTK_ENTRY_ICON_PRIMARY)
        return;

    pixmap = favicon_create_drag_pixmap (lentry, widget);

    if (pixmap != NULL)
    {
        gtk_drag_set_icon_pixmap (context,
                      gdk_drawable_get_colormap (pixmap),
                      pixmap, NULL, -2, -2);
        g_object_unref (pixmap);
    }
}

static void
favicon_drag_data_get_cb (GtkWidget *widget,
              GdkDragContext *context,
              GtkSelectionData *selection_data,
              guint info,
              guint32 time,
              EphyLocationEntry *lentry)
{
    gint index;
    GtkEntry *entry;

    g_assert (widget != NULL);
    g_return_if_fail (context != NULL);

    entry = GTK_ENTRY (widget);

    index = gtk_entry_get_current_icon_drag_source (entry);
    if (index == GTK_ENTRY_ICON_PRIMARY)
    {
        ephy_dnd_drag_data_get (widget, context, selection_data,
                    time, lentry, each_url_get_data_binder);
    }
}

static gboolean
icon_button_press_event_cb (GtkWidget *entry,
                GtkEntryIconPosition position,
                GdkEventButton *event,
                EphyLocationEntry *lentry)
{
    guint state = event->state & gtk_accelerator_get_default_mod_mask ();

    if (event->type == GDK_BUTTON_PRESS && 
        event->button == 1 &&
        state == 0 /* left */)
    {
        if (position == GTK_ENTRY_ICON_PRIMARY)
        {
            GtkWidget *toplevel;
        
            toplevel = gtk_widget_get_toplevel (GTK_WIDGET (entry));
            gtk_window_set_focus (GTK_WINDOW (toplevel), entry);

            gtk_editable_select_region (GTK_EDITABLE (entry), 
                            0, -1);
        }
        else
        {
            g_signal_emit (lentry, signals[LOCK_CLICKED], 0);
        }

        return TRUE;
    }

    return FALSE;
}

static void
ephy_location_entry_construct_contents (EphyLocationEntry *lentry)
{
    EphyLocationEntryPrivate *priv = lentry->priv;
    GtkTargetList *targetlist;

    LOG ("EphyLocationEntry constructing contents %p", lentry);

    priv->entry = gtk_entry_new ();
    gtk_container_add (GTK_CONTAINER (lentry), priv->entry);

    /* Favicon */
    targetlist = gtk_target_list_new (url_drag_types,
                      G_N_ELEMENTS (url_drag_types));
    gtk_entry_set_icon_drag_source (GTK_ENTRY (priv->entry),
                    GTK_ENTRY_ICON_PRIMARY,
                    targetlist,
                    GDK_ACTION_ASK | GDK_ACTION_COPY | GDK_ACTION_LINK);
    gtk_target_list_unref (targetlist);

    gtk_entry_set_icon_tooltip_text (GTK_ENTRY (priv->entry),
                     GTK_ENTRY_ICON_PRIMARY,
                     _("Drag and drop this icon to create a link to this page"));

    gtk_entry_set_icon_activatable (GTK_ENTRY (priv->entry),
                    GTK_ENTRY_ICON_PRIMARY,
                    TRUE);
    gtk_entry_set_icon_from_stock (GTK_ENTRY (priv->entry),
                       GTK_ENTRY_ICON_SECONDARY,
                       STOCK_LOCK_INSECURE);
    gtk_entry_set_icon_activatable (GTK_ENTRY (priv->entry),
                    GTK_ENTRY_ICON_SECONDARY,
                    TRUE);

    g_object_connect (priv->entry,
              "signal::icon-press", G_CALLBACK (icon_button_press_event_cb), lentry,
              "signal::populate-popup", G_CALLBACK (entry_populate_popup_cb), lentry,
              "signal::key-press-event", G_CALLBACK (entry_key_press_cb), lentry,
              "signal::changed", G_CALLBACK (editable_changed_cb), lentry,
              "signal::drag-motion", G_CALLBACK (entry_drag_motion_cb), lentry,
              "signal::drag-drop", G_CALLBACK (entry_drag_drop_cb), lentry,
              "signal::drag-data-get", G_CALLBACK (favicon_drag_data_get_cb), lentry,
              NULL);
              
    g_signal_connect_after (priv->entry, "key-press-event",
                G_CALLBACK (entry_key_press_after_cb), lentry);
    g_signal_connect_after (priv->entry, "activate",
                G_CALLBACK (entry_activate_after_cb), lentry);
    g_signal_connect_after (priv->entry, "drag-begin",
                G_CALLBACK (favicon_drag_begin_cb), lentry);

    gtk_widget_show (priv->entry);
}

static void
ephy_location_entry_init (EphyLocationEntry *le)
{
    EphyLocationEntryPrivate *p;

    LOG ("EphyLocationEntry initialising %p", le);

    p = EPHY_LOCATION_ENTRY_GET_PRIVATE (le);
    le->priv = p;

    p->user_changed = FALSE;
    p->block_update = FALSE;
    p->saved_text = NULL;
    p->search_terms = NULL;
    p->show_lock = FALSE;
    
    ephy_location_entry_construct_contents (le);

    gtk_tool_item_set_expand (GTK_TOOL_ITEM (le), TRUE);
}

GtkWidget *
ephy_location_entry_new (void)
{
    return GTK_WIDGET (g_object_new (EPHY_TYPE_LOCATION_ENTRY, NULL));
}

static gboolean
cursor_on_match_cb  (GtkEntryCompletion *completion,
             GtkTreeModel *model,
             GtkTreeIter *iter,
             EphyLocationEntry *le)
{
    char *item = NULL;
    GtkWidget *entry;

    gtk_tree_model_get (model, iter,
                le->priv->url_col,
                &item, -1);
    entry = gtk_entry_completion_get_entry (completion);

    /* Prevent the update so we keep the highlight from our input.
     * See textcell_data_func().
     */
    le->priv->block_update = TRUE;
    gtk_entry_set_text (GTK_ENTRY (entry), item);
    gtk_editable_set_position (GTK_EDITABLE (entry), -1);
    le->priv->block_update = FALSE;

    g_free (item);

    return TRUE;
}

static void
textcell_data_func (GtkCellLayout *cell_layout,
            GtkCellRenderer *cell,
            GtkTreeModel *tree_model,
            GtkTreeIter *iter,
            gpointer data)
{
    EphyLocationEntryPrivate *priv;
    PangoAttrList *list;
    PangoAttribute *att;
    GMatchInfo *match;

    int start, end;

    char *ctext;
    char *title;
    char *url;

    GtkStyle *style;
    GdkColor color;

    GValue text = { 0, };

    priv = EPHY_LOCATION_ENTRY (data)->priv;
    gtk_tree_model_get (tree_model, iter,
            priv->text_col, &title,
            priv->url_col, &url,
            -1);

    list = pango_attr_list_new ();

    if (url)
    {
        ctext = g_strdup_printf ("%s\n%s", title, url);

        style = gtk_widget_get_style (priv->entry);
        color = style->text[GTK_STATE_INSENSITIVE];

        att = pango_attr_foreground_new
            (color.red, color.green, color.blue);
        att->start_index = strlen (title)+1;

        pango_attr_list_insert (list, att);
            g_free (title);
    }
    else
    {
        ctext = title;
    }

    if (priv->search_terms)
    {
        GSList *iter;
        GRegex *regex;

        for (iter = priv->search_terms; iter != NULL; iter = iter->next)
        {
            regex = (GRegex*) iter->data;
            g_regex_match (regex, ctext, G_REGEX_MATCH_NOTEMPTY, &match);

            while (g_match_info_matches (match))
            {
                g_match_info_fetch_pos (match, 0, &start, &end);

                att = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
                att->start_index = start;
                att->end_index = end;

                pango_attr_list_insert (list, att);
                g_match_info_next (match, NULL);
            }

            g_match_info_free (match);
            match = NULL;
        }

    }

    g_object_set (cell,
              "attributes", list,
              NULL);

    g_value_init (&text, G_TYPE_STRING);
    g_value_take_string (&text, ctext);
    g_object_set_property (G_OBJECT (cell), "text", &text);
    g_value_unset (&text);

    pango_attr_list_unref (list);

    g_free (url);
}

static void
extracell_data_func (GtkCellLayout *cell_layout,
            GtkCellRenderer *cell,
            GtkTreeModel *tree_model,
            GtkTreeIter *iter,
            gpointer data)
{
    EphyLocationEntryPrivate *priv;
    gboolean is_bookmark = FALSE;
    GValue visible = { 0, };

    priv = EPHY_LOCATION_ENTRY (data)->priv;
    gtk_tree_model_get (tree_model, iter,
                priv->extra_col, &is_bookmark,
                -1);

    if (is_bookmark)
        g_object_set (cell,
                  "stock-id", EPHY_STOCK_BOOKMARK,
                  NULL);

    g_value_init (&visible, G_TYPE_BOOLEAN);
    g_value_set_boolean (&visible, is_bookmark);
    g_object_set_property (G_OBJECT (cell), "visible", &visible);
    g_value_unset (&visible);
}

/**
 * ephy_location_entry_set_match_func:
 * @entry: an #EphyLocationEntry widget
 * @match_func: a #GtkEntryCompletionMatchFunc
 * @user_data: user_data to pass to the @match_func
 * @notify: a #GDestroyNotify, like the one given to
 * gtk_entry_completion_set_match_func
 *
 * Sets the match_func for the internal #GtkEntryCompletion to @match_func.
 *
 **/
void
ephy_location_entry_set_match_func (EphyLocationEntry *entry, 
                GtkEntryCompletionMatchFunc match_func,
                gpointer user_data,
                GDestroyNotify notify)
{
    EphyLocationEntryPrivate *priv = entry->priv;
    GtkEntryCompletion *completion;
    
    completion = gtk_entry_get_completion (GTK_ENTRY (priv->entry));
    gtk_entry_completion_set_match_func (completion, match_func, user_data, notify);
}

/**
 * ephy_location_entry_set_completion:
 * @entry: an #EphyLocationEntry widget
 * @model: the #GtkModel for the completion
 * @text_col: column id to access #GtkModel relevant data
 * @action_col: column id to access #GtkModel relevant data
 * @keywords_col: column id to access #GtkModel relevant data
 * @relevance_col: column id to access #GtkModel relevant data
 * @url_col: column id to access #GtkModel relevant data
 * @extra_col: column id to access #GtkModel relevant data
 * @favicon_col: column id to access #GtkModel relevant data
 *
 * Initializes @entry to have a #GtkEntryCompletion using @model as the
 * internal #GtkModel. The *_col arguments are for internal data retrieval from
 * @model, like when setting the text property of one of the #GtkCellRenderer
 * of the completion.
 *
 **/
void
ephy_location_entry_set_completion (EphyLocationEntry *entry,
                    GtkTreeModel *model,
                    guint text_col,
                    guint action_col,
                    guint keywords_col,
                    guint relevance_col,
                    guint url_col,
                    guint extra_col,
                    guint favicon_col)
{
    EphyLocationEntryPrivate *priv = entry->priv;
    GtkTreeModel *sort_model;
    GtkEntryCompletion *completion;
    GtkCellRenderer *cell;

    entry->priv->text_col = text_col;
    entry->priv->action_col = action_col;
    entry->priv->keywords_col = keywords_col;
    entry->priv->relevance_col = relevance_col;
    entry->priv->url_col = url_col;
    entry->priv->extra_col = extra_col;
    entry->priv->favicon_col = favicon_col;

    sort_model = gtk_tree_model_sort_new_with_model (model);

    gtk_tree_sortable_set_sort_column_id 
            (GTK_TREE_SORTABLE (sort_model),
             entry->priv->relevance_col,
             GTK_SORT_DESCENDING);

    completion = gtk_entry_completion_new ();
    gtk_entry_completion_set_model (completion, sort_model);
    g_object_unref (sort_model);
    g_signal_connect (completion, "match-selected",
              G_CALLBACK (match_selected_cb), entry);
    g_signal_connect_after (completion, "action-activated",
                G_CALLBACK (action_activated_after_cb), entry);

#if 0
    cell = gtk_cell_renderer_pixbuf_new ();
    gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion),
                    cell, FALSE);
    gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (completion),
                       cell, "pixbuf", favicon_col);
#endif

    cell = gtk_cell_renderer_text_new ();
    g_object_set (cell,
              "ellipsize", PANGO_ELLIPSIZE_END,
              "ellipsize-set", TRUE,
              NULL);
    gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion),
                    cell, TRUE);
    gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (completion),
                       cell, "text", text_col);
        /*
         * As the width of the entry completion is known in advance 
         * (as big as the entry you are completing on), we can set 
         * any fixed width (the 1 is just this random number here). 
         * Since the height is known too, we avoid computing the actual 
         * sizes of the cells, which takes a lot of CPU time and does
         * not get used anyway.
         */
    gtk_cell_renderer_set_fixed_size (cell, 1, -1);
    gtk_cell_renderer_text_set_fixed_height_from_font (GTK_CELL_RENDERER_TEXT (cell), 2);

    gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (completion),
                    cell, textcell_data_func,
                    entry,
                    NULL);

    cell = gtk_cell_renderer_pixbuf_new ();
    gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (completion),
                  cell, FALSE);
    gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (completion),
                        cell, extracell_data_func,
                        entry,
                        NULL);

    g_object_set (completion, "inline-selection", TRUE, NULL);
    g_signal_connect (completion, "cursor-on-match",
              G_CALLBACK (cursor_on_match_cb), entry);

    gtk_entry_set_completion (GTK_ENTRY (priv->entry), completion);
    g_object_unref (completion);
}

/**
 * ephy_location_entry_set_location:
 * @entry: an #EphyLocationEntry widget
 * @address: the address to be set as the current location
 * @typed_address: the address to be shown in the location entry or NULL
 *
 * Sets the current address of @entry to @address, alternatively a
 * @typed_address can be used to determine the text to be displayed in the
 * location entry.
 *
 **/
void
ephy_location_entry_set_location (EphyLocationEntry *entry,
                  const char *address,
                  const char *typed_address)
{
    EphyLocationEntryPrivate *priv = entry->priv;
    GtkClipboard *clipboard;
    const char *text;
    char* selection = NULL;
    int start, end;

    g_return_if_fail (address != NULL);

    /* Setting a new text will clear the clipboard. This makes it impossible
     * to copy&paste from the location entry of one tab into another tab, see
     * bug #155824. So we save the selection iff the clipboard was owned by
     * the location entry.
     */
    if (GTK_WIDGET_REALIZED (GTK_WIDGET (priv->entry)))
    {
        clipboard = gtk_widget_get_clipboard (priv->entry,
                              GDK_SELECTION_PRIMARY);
        g_return_if_fail (clipboard != NULL);

        if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (priv->entry) &&
            gtk_editable_get_selection_bounds (GTK_EDITABLE (priv->entry),
                                   &start, &end))
        {
            selection = gtk_editable_get_chars (GTK_EDITABLE (priv->entry),
                                start, end);
        }
    }

    if (typed_address != NULL)
    {
        text = typed_address;
    }
    else if (address != NULL && strcmp (address, "about:blank") != 0)
    {
        text = address;
    }
    else
    {
        text = "";
    }

    /* First record the new hash, then update the entry text */
    priv->hash = g_str_hash (address);

    priv->block_update = TRUE;
    gtk_entry_set_text (GTK_ENTRY (priv->entry), text);
    priv->block_update = FALSE;

    /* We need to call update_address_state() here, as the 'changed' signal
     * may not get called if the user has typed in the exact correct url */
    update_address_state (entry);
    update_favicon (entry);

    /* Now restore the selection.
     * Note that it's not owned by the entry anymore!
     */
    if (selection != NULL)
    {
        gtk_clipboard_set_text (gtk_clipboard_get (GDK_SELECTION_PRIMARY),
                    selection, strlen (selection));
        g_free (selection);
    }
}

/**
 * ephy_location_entry_get_can_undo:
 * @entry: an #EphyLocationEntry widget
 *
 * Wheter @entry can restore the displayed user modified text to the unmodified 
 * previous text.
 *
 * Return value: TRUE or FALSE indicating if the text can be restored
 *
 **/
gboolean
ephy_location_entry_get_can_undo (EphyLocationEntry *entry)
{
    EphyLocationEntryPrivate *priv = entry->priv;
    
    return priv->user_changed;
}

/**
 * ephy_location_entry_get_can_redo:
 * @entry: an #EphyLocationEntry widget
 *
 * Wheter @entry can restore the displayed text to the user modified version
 * before the undo.
 *
 * Return value: TRUE or FALSE indicating if the text can be restored
 *
 **/
gboolean
ephy_location_entry_get_can_redo (EphyLocationEntry *entry)
{
    EphyLocationEntryPrivate *priv = entry->priv;
    
    return priv->can_redo;
}

/**
 * ephy_location_entry_get_location:
 * @entry: an #EphyLocationEntry widget
 *
 * Retrieves the text displayed by the internal #GtkEntry of @entry. This is
 * the currently displayed text, like in any #GtkEntry.
 *
 * Return value: the text inside the inner #GtkEntry of @entry, owned by GTK+
 *
 **/
const char *
ephy_location_entry_get_location (EphyLocationEntry *entry)
{
    EphyLocationEntryPrivate *priv = entry->priv;

    return gtk_entry_get_text (GTK_ENTRY (priv->entry));
}

static gboolean
ephy_location_entry_reset_internal (EphyLocationEntry *entry,
                    gboolean notify)
{
    EphyLocationEntryPrivate *priv = entry->priv;
    const char *text, *old_text;
    char *url = NULL;
    gboolean retval;

    g_signal_emit (entry, signals[GET_LOCATION], 0, &url);
    text = url != NULL ? url : "";
    old_text = gtk_entry_get_text (GTK_ENTRY (priv->entry));
    old_text = old_text != NULL ? old_text : "";

    g_free (priv->saved_text);
    priv->saved_text = g_strdup (old_text);
    priv->can_redo = TRUE;

    retval = g_str_hash (text) != g_str_hash (old_text);

    ephy_location_entry_set_location (entry, text, NULL);
    g_free (url);

    if (notify)
    {
        g_signal_emit (entry, signals[USER_CHANGED], 0);
    }
    
    priv->user_changed = FALSE;

    return retval;
}

/**
 * ephy_location_entry_undo_reset:
 * @entry: an #EphyLocationEntry widget
 *
 * Undo a previous ephy_location_entry_reset.
 *
 **/
void
ephy_location_entry_undo_reset (EphyLocationEntry *entry)
{
    EphyLocationEntryPrivate *priv = entry->priv;
    
    gtk_entry_set_text (GTK_ENTRY (priv->entry), priv->saved_text);
    priv->can_redo = FALSE;
    priv->user_changed = TRUE;
}

/**
 * ephy_location_entry_reset:
 * @entry: an #EphyLocationEntry widget
 *
 * Restore the @entry to the text corresponding to the current location, this
 * does not fire the user_changed signal. This is called each time the user
 * presses Escape while the location entry is selected.
 *
 * Return value: TRUE on success, FALSE otherwise
 *
 **/
gboolean
ephy_location_entry_reset (EphyLocationEntry *entry)
{
    return ephy_location_entry_reset_internal (entry, FALSE);
}

/**
 * ephy_location_entry_activate:
 * @entry: an #EphyLocationEntry widget
 *
 * Set focus on @entry and select the text whithin. This is called when the
 * user hits Control+L.
 *
 **/
void
ephy_location_entry_activate (EphyLocationEntry *entry)
{
    EphyLocationEntryPrivate *priv = entry->priv;
    GtkWidget *toplevel;

    toplevel = gtk_widget_get_toplevel (GTK_WIDGET (entry));

    gtk_editable_select_region (GTK_EDITABLE (priv->entry),
                    0, -1);
    gtk_window_set_focus (GTK_WINDOW (toplevel),
                  priv->entry);
}

/**
 * ephy_location_entry_get_entry:
 * @entry: an #EphyLocationEntry widget
 *
 * Retrieve the internal #GtkEntry of the #EphyIconEntry inside @entry.
 *
 * Return value: a pointer to the internal #GtkEntry of @entry
 *
 **/
GtkWidget *
ephy_location_entry_get_entry (EphyLocationEntry *entry)
{
    EphyLocationEntryPrivate *priv = entry->priv;

    return priv->entry;
}

/**
 * ephy_location_entry_set_favicon:
 * @entry: an #EphyLocationEntry widget
 * @pixbuf: a #GdkPixbuf to be set as the icon of the entry
 *
 * Sets the icon in the internal #EphyIconEntry of @entry
 *
 **/
void
ephy_location_entry_set_favicon (EphyLocationEntry *entry,
                 GdkPixbuf *pixbuf)
{
    EphyLocationEntryPrivate *priv = entry->priv;

    if (priv->favicon != NULL)
    {
        g_object_unref (priv->favicon);
    }

    priv->favicon = pixbuf ? g_object_ref (pixbuf) : NULL;

    update_favicon (entry);
}

/**
 * ephy_location_entry_set_secure:
 * @entry: an #EphyLocationEntry widget
 * @secure: whether the page is secure and thus the location bar should reflect
 * that
 *
 * Set @entry to give visual feedback if the page is @secure. If it is, the
 * location bar will have its background painted differently (yellow by
 * default).
 *
 **/
void
ephy_location_entry_set_secure (EphyLocationEntry *entry,
                gboolean secure)
{
    EphyLocationEntryPrivate *priv = entry->priv;
    GtkWidget *widget = GTK_WIDGET (entry);
    GtkWidget *gentry = priv->entry;

    priv->secure = secure;

    /* We have to set the colour of the GtkEntry in the EphyIconEntry */
    if (priv->secure && priv->apply_colours)
    {
        gtk_widget_modify_text (gentry, GTK_STATE_NORMAL, &priv->secure_fg_colour);
        gtk_widget_modify_base (gentry, GTK_STATE_NORMAL, &priv->secure_bg_colour);
    }
    else
    {
        gtk_widget_modify_text (gentry, GTK_STATE_NORMAL, NULL);
        gtk_widget_modify_base (gentry, GTK_STATE_NORMAL, NULL);
    }

    gtk_widget_queue_draw (widget);
}

/**
 * ephy_location_entry_set_show_lock:
 * @entry: an #EphyLocationEntry widget
 * @show_lock: if @entry should show a lock icon indicating the security level
 * of the page
 *
 * If @show_lock is TRUE, the location bar will show an icon reflecting the
 * security level of the page, by default it's shown only in secure and
 * insecure pages (insecure meaning secure pages with something broken in such
 * security)
 *
 **/
void
ephy_location_entry_set_show_lock (EphyLocationEntry *entry,
                   gboolean show_lock)
{
    EphyLocationEntryPrivate *priv;

    g_return_if_fail (EPHY_IS_LOCATION_ENTRY (entry));

    priv = entry->priv;

    priv->show_lock = show_lock != FALSE;

    gtk_entry_set_icon_from_stock (GTK_ENTRY (priv->entry),
                       GTK_ENTRY_ICON_SECONDARY,
                       show_lock ? priv->lock_stock_id : NULL);
}

/**
 * ephy_location_entry_set_lock_stock:
 * @entry: an #EphyLocationEntry widget
 * @stock_id: a stock_id from GTK+ stock icons
 *
 * Set the lock icon to be displayed, to actually show the icon see 
 * ephy_location_entry_set_show_lock.
 *
 **/
void
ephy_location_entry_set_lock_stock (EphyLocationEntry *entry,
                    const char *stock_id)

{
    g_return_if_fail (EPHY_IS_LOCATION_ENTRY (entry));

    entry->priv->lock_stock_id = (char*)stock_id;

    if (entry->priv->show_lock)
        gtk_entry_set_icon_from_stock (GTK_ENTRY (entry->priv->entry),
                           GTK_ENTRY_ICON_SECONDARY,
                           stock_id);
}

/**
 * ephy_location_entry_set_lock_tooltip:
 * @entry: an #EphyLocationEntry widget
 * @tooltip: the text to be set in the tooltip for the lock icon
 *
 * Set the text to be displayed when hovering the lock icon of @entry.
 *
 **/
void
ephy_location_entry_set_lock_tooltip (EphyLocationEntry *entry,
                      const char *tooltip)
{
    EphyLocationEntryPrivate *priv = entry->priv;

    gtk_entry_set_icon_tooltip_text (GTK_ENTRY (priv->entry),
                     GTK_ENTRY_ICON_SECONDARY,
                     tooltip);
}

/**
 * ephy_location_entry_get_search_terms:
 * @entry: an #EphyLocationEntry widget
 *
 * Return the internal #GSList containing the search terms as #GRegex
 * instances, formed in @entry on user changes.
 *
 * Return value: the internal #GSList
 *
 **/
GSList *
ephy_location_entry_get_search_terms (EphyLocationEntry *entry)
{
    EphyLocationEntryPrivate *priv = entry->priv;
    
    return priv->search_terms;
}