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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Copyright (C) 2002 Ricardo Fernández Pascual
* Copyright (C) 2003, 2004 Marco Pesenti Gritti
* Copyright (C) 2003, 2004, 2005 Christian Persch
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id$
*/
#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 <glib/gi18n.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtktoolbar.h>
#include <gtk/gtkentry.h>
#include <gtk/gtkwindow.h>
#include <gtk/gtkcellrenderertext.h>
#include <gtk/gtkcelllayout.h>
#include <gtk/gtktreemodelsort.h>
#include <gtk/gtkstock.h>
#include <gtk/gtkimage.h>
#include <gtk/gtkeventbox.h>
#include <gtk/gtkbox.h>
#include <gtk/gtkhbox.h>
#include <gtk/gtkvbox.h>
#include <gtk/gtkimagemenuitem.h>
#include <gtk/gtkseparatormenuitem.h>
#include <gtk/gtkframe.h>
#include <gtk/gtkalignment.h>
#include <gtk/gtkclipboard.h>
#include <string.h>
#define EPHY_LOCATION_ENTRY_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_LOCATION_ENTRY, EphyLocationEntryPrivate))
struct _EphyLocationEntryPrivate
{
GtkTooltips *tips;
GtkWidget *ebox;
GtkWidget *entry;
GtkWidget *icon_ebox;
GtkWidget *icon;
GtkWidget *lock_ebox;
GtkWidget *lock;
GdkPixbuf *favicon;
char *before_completion;
guint text_col;
guint action_col;
guint keywords_col;
guint relevance_col;
guint hash;
guint user_changed : 1;
guint original_address : 1;
};
static const struct
{
const char *prefix;
int len;
}
web_prefixes [] =
{
{ "http://www.", 11 },
{ "http://", 7 },
{ "https://www.", 12 },
{ "https://", 8 },
{ "www.", 4 }
};
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 void ephy_location_entry_class_init (EphyLocationEntryClass *klass);
static void ephy_location_entry_init (EphyLocationEntry *le);
static GObjectClass *parent_class = NULL;
enum signalsEnum
{
USER_CHANGED,
LOCK_CLICKED,
GET_LOCATION,
GET_TITLE,
LAST_SIGNAL
};
static gint signals[LAST_SIGNAL] = { 0 };
#define MAX_LOC_HISTORY_ITEMS 10
#define EPHY_LOC_HISTORY_XML_ROOT "ephy_location_history"
#define EPHY_LOC_HISTORY_XML_VERSION "0.1"
GType
ephy_location_entry_get_type (void)
{
static GType type = 0;
if (G_UNLIKELY (type == 0))
{
static const GTypeInfo our_info =
{
sizeof (EphyLocationEntryClass),
NULL,
NULL,
(GClassInitFunc) ephy_location_entry_class_init,
NULL,
NULL,
sizeof (EphyLocationEntry),
0,
(GInstanceInitFunc) ephy_location_entry_init
};
type = g_type_register_static (GTK_TYPE_TOOL_ITEM,
"EphyLocationEntry",
&our_info, 0);
}
return type;
}
static gboolean
ephy_location_entry_set_tooltip (GtkToolItem *tool_item,
GtkTooltips *tooltips,
const char *tip_text,
const char *tip_private)
{
EphyLocationEntry *entry = EPHY_LOCATION_ENTRY (tool_item);
EphyLocationEntryPrivate *priv = entry->priv;
gtk_tooltips_set_tip (tooltips, priv->entry, tip_text, tip_private);
return TRUE;
}
static void
ephy_location_entry_finalize (GObject *object)
{
EphyLocationEntry *entry = EPHY_LOCATION_ENTRY (object);
EphyLocationEntryPrivate *priv = entry->priv;
if (priv->favicon != NULL)
{
g_object_unref (priv->favicon);
}
g_object_unref (priv->tips);
parent_class->finalize (object);
}
static void
ephy_location_entry_class_init (EphyLocationEntryClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkToolItemClass *tool_item_class = GTK_TOOL_ITEM_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
object_class->finalize = ephy_location_entry_finalize;
tool_item_class->set_tooltip = ephy_location_entry_set_tooltip;
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);
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 *entry)
{
EphyLocationEntryPrivate *priv = entry->priv;
GtkImage *image = GTK_IMAGE (priv->icon);
/* Only show the favicon if the entry's text is the
* address of the current page.
*/
if (priv->favicon != NULL && priv->original_address)
{
gtk_image_set_from_pixbuf (image, priv->favicon);
}
else
{
gtk_image_set_from_stock (image,
GTK_STOCK_NEW,
GTK_ICON_SIZE_MENU);
}
}
static void
editable_changed_cb (GtkEditable *editable,
EphyLocationEntry *entry)
{
EphyLocationEntryPrivate *priv = entry->priv;
update_address_state (entry);
if (priv->user_changed == FALSE) return;
update_favicon (entry);
g_signal_emit (entry, signals[USER_CHANGED], 0);
}
static gboolean
entry_button_press_cb (GtkWidget *entry, GdkEventButton *event, EphyLocationEntry *le)
{
if (event->button == 1 && event->type == GDK_2BUTTON_PRESS)
{
ephy_location_entry_activate (le);
return TRUE;
}
return FALSE;
}
static gboolean
entry_key_press_cb (GtkEntry *entry,
GdkEventKey *event,
EphyLocationEntry *lentry)
{
guint mask = gtk_accelerator_get_default_mod_mask ();
if ((event->keyval == GDK_Return || event->keyval == GDK_ISO_Enter) &&
(event->state & mask) == GDK_CONTROL_MASK)
{
g_signal_emit_by_name (entry, "activate");
return TRUE;
}
else if (event->keyval == GDK_Escape && (event->state & mask) == 0)
{
ephy_location_entry_reset (lentry);
/* don't return TRUE since we want to cancel the autocompletion popup too */
}
return FALSE;
}
static gboolean
keyword_match (const char *list,
const char *keyword)
{
const char *p;
gsize keyword_len;
p = list;
keyword_len = strlen (keyword);
while (*p)
{
int i;
for (i = 0; i < keyword_len; i++)
{
if (p[i] != keyword[i])
{
goto next_token;
}
}
return TRUE;
next_token:
while (*p && *p != ' ') p++;
if (*p) p++;
}
return FALSE;
}
static gboolean
completion_func (GtkEntryCompletion *completion,
const char *key,
GtkTreeIter *iter,
gpointer data)
{
int i, len_key, len_prefix;
char *item = NULL;
char *keywords = NULL;
gboolean ret = FALSE;
EphyLocationEntry *le = EPHY_LOCATION_ENTRY (data);
GtkTreeModel *model;
model = gtk_entry_completion_get_model (completion);
gtk_tree_model_get (model, iter,
le->priv->text_col, &item,
le->priv->keywords_col, &keywords,
-1);
len_key = strlen (key);
if (!strncasecmp (key, item, len_key))
{
ret = TRUE;
}
else if (keyword_match (keywords, key))
{
ret = TRUE;
}
else
{
for (i = 0; i < G_N_ELEMENTS (web_prefixes); i++)
{
len_prefix = web_prefixes[i].len;
if (!strncmp (web_prefixes[i].prefix, item, len_prefix) &&
!strncasecmp (key, item + len_prefix, len_key))
{
ret = TRUE;
break;
}
}
}
g_free (item);
g_free (keywords);
return ret;
}
static gboolean
match_selected_cb (GtkEntryCompletion *completion,
GtkTreeModel *model,
GtkTreeIter *iter,
EphyLocationEntry *le)
{
char *item = NULL;
gtk_tree_model_get (model, iter,
le->priv->action_col, &item, -1);
if (item == NULL) return FALSE;
ephy_location_entry_set_location (le, item, NULL);
g_signal_emit_by_name (le->priv->entry, "activate");
g_free (item);
return 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->user_changed = FALSE;
gtk_entry_set_text (GTK_ENTRY (priv->entry), "");
priv->user_changed = TRUE;
}
static void
entry_populate_popup_cb (GtkEntry *entry,
GtkMenu *menu,
EphyLocationEntry *lentry)
{
EphyLocationEntryPrivate *priv = lentry->priv;
GtkWidget *image;
GtkWidget *menuitem;
GList *children, *item;
int pos = 0, sep = 0;
/* 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.)
*/
menuitem = gtk_image_menu_item_new_with_mnemonic (_("Cl_ear"));
gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM(menuitem), image);
g_signal_connect (menuitem , "activate",
G_CALLBACK (entry_clear_activate_cb), lentry);
gtk_widget_set_sensitive (menuitem,
gtk_editable_get_editable (GTK_EDITABLE (priv->entry)));
gtk_widget_show (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_MENU_SHELL (menu)->children;
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), menuitem, pos - 1);
}
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);
}
static void
favicon_drag_data_get_cb (GtkWidget *widget,
GdkDragContext *context,
GtkSelectionData *selection_data,
guint info,
guint32 time,
EphyLocationEntry *entry)
{
g_assert (widget != NULL);
g_return_if_fail (context != NULL);
ephy_dnd_drag_data_get (widget, context, selection_data,
time, entry, each_url_get_data_binder);
}
static gboolean
lock_button_press_event_cb (GtkWidget *ebox,
GdkEventButton *event,
EphyLocationEntry *entry)
{
if (event->type == GDK_BUTTON_PRESS && event->button == 1 /* left */)
{
g_signal_emit (entry, signals[LOCK_CLICKED], 0);
return TRUE;
}
return FALSE;
}
static void
modify_background (EphyLocationEntry *entry)
{
EphyLocationEntryPrivate *priv = entry->priv;
if (priv->entry->style == NULL || !GTK_WIDGET_REALIZED (priv->ebox)) return;
gtk_widget_modify_bg (priv->ebox, GTK_STATE_NORMAL,
&priv->entry->style->base[GTK_STATE_NORMAL]);
}
static void
entry_style_set_cb (GtkWidget *widget,
GtkStyle *previous_style,
EphyLocationEntry *entry)
{
LOG ("entry_style_set_cb");
modify_background (entry);
}
static void
entry_realize_cb (GtkWidget *widget,
EphyLocationEntry *entry)
{
LOG ("entry_realize_cb");
modify_background (entry);
}
static void
ephy_location_entry_construct_contents (EphyLocationEntry *entry)
{
EphyLocationEntryPrivate *priv = entry->priv;
GtkWidget *alignment, *frame, *hbox;
LOG ("EphyLocationEntry constructing contents %p", entry);
alignment = gtk_alignment_new (0.0, 0.5, 1.0, 0.0);
gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 0, 0, 1, 1);
gtk_container_add (GTK_CONTAINER (entry), alignment);
frame = gtk_frame_new (NULL);
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
gtk_container_add (GTK_CONTAINER (alignment), frame);
priv->ebox = gtk_event_box_new ();
gtk_container_set_border_width (GTK_CONTAINER (priv->ebox), 0);
gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->ebox), TRUE);
gtk_container_add (GTK_CONTAINER (frame), priv->ebox);
hbox = gtk_hbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (priv->ebox), hbox);
priv->icon_ebox = gtk_event_box_new ();
gtk_container_set_border_width (GTK_CONTAINER (priv->icon_ebox), 2);
gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->icon_ebox), FALSE);
gtk_box_pack_start (GTK_BOX (hbox), priv->icon_ebox, FALSE, FALSE, 2);
gtk_drag_source_set (priv->icon_ebox, GDK_BUTTON1_MASK,
url_drag_types, G_N_ELEMENTS (url_drag_types),
GDK_ACTION_ASK | GDK_ACTION_COPY | GDK_ACTION_LINK);
gtk_tooltips_set_tip (priv->tips, priv->icon_ebox,
_("Drag and drop this icon to create a link to this page"), NULL);
priv->icon = gtk_image_new ();
gtk_container_add (GTK_CONTAINER (priv->icon_ebox), priv->icon);
priv->entry = gtk_entry_new ();
gtk_entry_set_has_frame (GTK_ENTRY (priv->entry), FALSE);
gtk_box_pack_start (GTK_BOX (hbox), priv->entry, TRUE, TRUE, 0);
priv->lock_ebox = gtk_event_box_new ();
gtk_container_set_border_width (GTK_CONTAINER (priv->lock_ebox), 2);
gtk_widget_add_events (priv->lock_ebox, GDK_BUTTON_PRESS_MASK);
gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->lock_ebox), FALSE);
gtk_box_pack_end (GTK_BOX (hbox), priv->lock_ebox, FALSE, FALSE, 2);
priv->lock = gtk_image_new_from_stock (STOCK_LOCK_INSECURE,
GTK_ICON_SIZE_MENU);
gtk_container_add (GTK_CONTAINER (priv->lock_ebox), priv->lock);
gtk_widget_show_all (alignment);
/* monitor entry style so we can set the background behind the icons */
g_signal_connect_after (priv->entry, "style-set",
G_CALLBACK (entry_style_set_cb), entry);
g_signal_connect_after (priv->entry, "realize",
G_CALLBACK (entry_realize_cb), entry);
g_signal_connect (priv->icon_ebox, "drag_data_get",
G_CALLBACK (favicon_drag_data_get_cb), entry);
g_signal_connect (priv->entry, "populate_popup",
G_CALLBACK (entry_populate_popup_cb), entry);
g_signal_connect (priv->entry, "key-press-event",
G_CALLBACK (entry_key_press_cb), entry);
g_signal_connect (priv->entry, "button_press_event",
G_CALLBACK (entry_button_press_cb), entry);
g_signal_connect (priv->entry, "changed",
G_CALLBACK (editable_changed_cb), entry);
g_signal_connect (priv->entry, "drag_motion",
G_CALLBACK (entry_drag_motion_cb), entry);
g_signal_connect (priv->entry, "drag_drop",
G_CALLBACK (entry_drag_drop_cb), entry);
g_signal_connect (priv->lock_ebox, "button-press-event",
G_CALLBACK (lock_button_press_event_cb), 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 = TRUE;
p->tips = gtk_tooltips_new ();
g_object_ref (p->tips);
gtk_object_sink (GTK_OBJECT (p->tips));
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 gint
sort_func (GtkTreeModel *model,
GtkTreeIter *a,
GtkTreeIter *b,
gpointer data)
{
gint valuea, valueb;
EphyLocationEntry *le = EPHY_LOCATION_ENTRY (data);
gtk_tree_model_get (model, a,
le->priv->relevance_col, &valuea, -1);
gtk_tree_model_get (model, b,
le->priv->relevance_col, &valueb, -1);
return valueb - valuea;
}
void
ephy_location_entry_set_completion (EphyLocationEntry *le,
GtkTreeModel *model,
guint text_col,
guint action_col,
guint keywords_col,
guint relevance_col)
{
GtkTreeModel *sort_model;
GtkEntryCompletion *completion;
GtkCellRenderer *cell;
le->priv->text_col = text_col;
le->priv->action_col = action_col;
le->priv->keywords_col = keywords_col;
le->priv->relevance_col = relevance_col;
sort_model = gtk_tree_model_sort_new_with_model (model);
g_object_unref (model);
gtk_tree_sortable_set_default_sort_func
(GTK_TREE_SORTABLE (sort_model),
sort_func, le, NULL);
completion = gtk_entry_completion_new ();
gtk_entry_completion_set_model (completion, sort_model);
g_object_unref (sort_model);
gtk_entry_completion_set_match_func (completion, completion_func, le, NULL);
g_signal_connect (completion, "match_selected",
G_CALLBACK (match_selected_cb), le);
cell = gtk_cell_renderer_text_new ();
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion),
cell, TRUE);
gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (completion),
cell, "text", text_col);
gtk_entry_set_completion (GTK_ENTRY (le->priv->entry), completion);
g_object_unref (completion);
}
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 (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->user_changed = FALSE;
gtk_entry_set_text (GTK_ENTRY (priv->entry), text);
priv->user_changed = TRUE;
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);
}
}
const char *
ephy_location_entry_get_location (EphyLocationEntry *le)
{
return gtk_entry_get_text (GTK_ENTRY (le->priv->entry));
}
gboolean
ephy_location_entry_reset (EphyLocationEntry *entry)
{
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 : "";
retval = g_str_hash (text) != g_str_hash (old_text);
ephy_location_entry_set_location (entry, text, NULL);
g_free (url);
return retval;
}
void
ephy_location_entry_activate (EphyLocationEntry *le)
{
GtkWidget *toplevel;
toplevel = gtk_widget_get_toplevel (le->priv->entry);
gtk_editable_select_region (GTK_EDITABLE(le->priv->entry),
0, -1);
gtk_window_set_focus (GTK_WINDOW(toplevel),
le->priv->entry);
}
GtkWidget *
ephy_location_entry_get_entry (EphyLocationEntry *entry)
{
return entry->priv->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);
}
void
ephy_location_entry_set_show_lock (EphyLocationEntry *entry,
gboolean show_lock)
{
g_object_set (entry->priv->lock_ebox, "visible", show_lock, NULL);
}
void
ephy_location_entry_set_lock_stock (EphyLocationEntry *entry,
const char *stock_id)
{
EphyLocationEntryPrivate *priv = entry->priv;
gtk_image_set_from_stock (GTK_IMAGE (priv->lock), stock_id,
GTK_ICON_SIZE_MENU);
}
void
ephy_location_entry_set_lock_tooltip (EphyLocationEntry *entry,
const char *tooltip)
{
EphyLocationEntryPrivate *priv = entry->priv;
gtk_tooltips_set_tip (priv->tips, priv->lock_ebox, tooltip, NULL);
}
|