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
|
/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
*
* Copyright (C) 2000-2005 by Alfons Hoogervorst & The Sylpheed Claws Team.
*
* 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 of the License, 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.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "defs.h"
#include <glib.h>
#include <glib/gi18n.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtkmain.h>
#include <gtk/gtkwindow.h>
#include <gtk/gtkentry.h>
#include <gtk/gtkeditable.h>
#include <gtk/gtkclist.h>
#include <gtk/gtkscrolledwindow.h>
#include <string.h>
#include <ctype.h>
#include "xml.h"
#include "addr_compl.h"
#include "utils.h"
#include "addressbook.h"
#include "main.h"
/* How it works:
*
* The address book is read into memory. We set up an address list
* containing all address book entries. Next we make the completion
* list, which contains all the completable strings, and store a
* reference to the address entry it belongs to.
* After calling the g_completion_complete(), we get a reference
* to a valid email address.
*
* Completion is very simplified. We never complete on another prefix,
* i.e. we neglect the next smallest possible prefix for the current
* completion cache. This is simply done so we might break up the
* addresses a little more (e.g. break up alfons@proteus.demon.nl into
* something like alfons, proteus, demon, nl; and then completing on
* any of those words).
*/
/* address_entry - structure which refers to the original address entry in the
* address book
*/
typedef struct
{
gchar *name;
gchar *address;
} address_entry;
/* completion_entry - structure used to complete addresses, with a reference
* the the real address information.
*/
typedef struct
{
gchar *string; /* string to complete */
address_entry *ref; /* address the string belongs to */
} completion_entry;
/*******************************************************************************/
static gint g_ref_count; /* list ref count */
static GList *g_completion_list; /* list of strings to be checked */
static GList *g_address_list; /* address storage */
static GCompletion *g_completion; /* completion object */
/* To allow for continuing completion we have to keep track of the state
* using the following variables. No need to create a context object. */
static gint g_completion_count; /* nr of addresses incl. the prefix */
static gint g_completion_next; /* next prev address */
static GSList *g_completion_addresses; /* unique addresses found in the
completion cache. */
static gchar *g_completion_prefix; /* last prefix. (this is cached here
* because the prefix passed to g_completion
* is g_strdown()'ed */
/*******************************************************************************/
/* completion_func() - used by GTK to find the string data to be used for
* completion
*/
static gchar *completion_func(gpointer data)
{
g_return_val_if_fail(data != NULL, NULL);
return ((completion_entry *)data)->string;
}
static void init_all(void)
{
g_completion = g_completion_new(completion_func);
g_return_if_fail(g_completion != NULL);
}
static void free_all(void)
{
GList *walk;
walk = g_list_first(g_completion_list);
for (; walk != NULL; walk = g_list_next(walk)) {
completion_entry *ce = (completion_entry *) walk->data;
g_free(ce->string);
g_free(walk->data);
}
g_list_free(g_completion_list);
g_completion_list = NULL;
walk = g_address_list;
for (; walk != NULL; walk = g_list_next(walk)) {
address_entry *ae = (address_entry *) walk->data;
g_free(ae->name);
g_free(ae->address);
g_free(walk->data);
}
g_list_free(g_address_list);
g_address_list = NULL;
g_completion_free(g_completion);
g_completion = NULL;
}
/* add_address() - adds address to the completion list. this function looks
* complicated, but it's only allocation checks.
*/
static gint add_address(const gchar *name, const gchar *address)
{
address_entry *ae;
completion_entry *ce1;
completion_entry *ce2;
if (!name || !address) return -1;
ae = g_new0(address_entry, 1);
ce1 = g_new0(completion_entry, 1),
ce2 = g_new0(completion_entry, 1);
g_return_val_if_fail(ae != NULL, -1);
g_return_val_if_fail(ce1 != NULL && ce2 != NULL, -1);
ae->name = g_strdup(name);
ae->address = g_strdup(address);
/* GCompletion list is case sensitive */
ce1->string = g_utf8_strdown(name, -1);
ce2->string = g_utf8_strdown(address, -1);
ce1->ref = ce2->ref = ae;
g_completion_list = g_list_append(g_completion_list, ce1);
g_completion_list = g_list_append(g_completion_list, ce2);
g_address_list = g_list_append(g_address_list, ae);
return 0;
}
/* read_address_book()
*/
static void read_address_book(void) {
addressbook_load_completion( add_address );
}
/* start_address_completion() - returns the number of addresses
* that should be matched for completion.
*/
gint start_address_completion(void)
{
clear_completion_cache();
if (!g_ref_count) {
init_all();
/* open the address book */
read_address_book();
/* merge the completion entry list into g_completion */
if (g_completion_list)
g_completion_add_items(g_completion, g_completion_list);
}
g_ref_count++;
debug_print("start_address_completion ref count %d\n", g_ref_count);
return g_list_length(g_completion_list);
}
/* get_address_from_edit() - returns a possible address (or a part)
* from an entry box. To make life easier, we only look at the last valid address
* component; address completion only works at the last string component in
* the entry box.
*/
gchar *get_address_from_edit(GtkEntry *entry, gint *start_pos)
{
const gchar *edit_text, *p;
gint cur_pos;
gboolean in_quote = FALSE;
gboolean in_bracket = FALSE;
gchar *str;
edit_text = gtk_entry_get_text(entry);
if (edit_text == NULL) return NULL;
cur_pos = gtk_editable_get_position(GTK_EDITABLE(entry));
/* scan for a separator. doesn't matter if walk points at null byte. */
for (p = g_utf8_offset_to_pointer(edit_text, cur_pos);
p > edit_text;
p = g_utf8_prev_char(p)) {
if (*p == '"')
in_quote ^= TRUE;
else if (!in_quote) {
if (!in_bracket && *p == ',')
break;
else if (*p == '>')
in_bracket = TRUE;
else if (*p == '<')
in_bracket = FALSE;
}
}
/* have something valid */
if (g_utf8_strlen(p, -1) == 0)
return NULL;
#define IS_VALID_CHAR(x) \
(g_ascii_isalnum(x) || (x) == '"' || (x) == '<' || ((guchar)(x) > 0x7f))
/* now scan back until we hit a valid character */
for (; *p && !IS_VALID_CHAR(*p); p = g_utf8_next_char(p))
;
#undef IS_VALID_CHAR
if (g_utf8_strlen(p, -1) == 0)
return NULL;
if (start_pos) *start_pos = g_utf8_pointer_to_offset(edit_text, p);
str = g_strdup(p);
return str;
}
/* replace_address_in_edit() - replaces an incompleted address with a completed one.
*/
void replace_address_in_edit(GtkEntry *entry, const gchar *newtext,
gint start_pos)
{
if (!newtext) return;
gtk_editable_delete_text(GTK_EDITABLE(entry), start_pos, -1);
gtk_editable_insert_text(GTK_EDITABLE(entry), newtext, strlen(newtext),
&start_pos);
gtk_editable_set_position(GTK_EDITABLE(entry), -1);
}
/* complete_address() - tries to complete an addres, and returns the
* number of addresses found. use get_complete_address() to get one.
* returns zero if no match was found, otherwise the number of addresses,
* with the original prefix at index 0.
*/
guint complete_address(const gchar *str)
{
GList *result;
gchar *d;
guint count, cpl;
completion_entry *ce;
g_return_val_if_fail(str != NULL, 0);
clear_completion_cache();
g_completion_prefix = g_strdup(str);
/* g_completion is case sensitive */
d = g_utf8_strdown(str, -1);
result = g_completion_complete(g_completion, d, NULL);
count = g_list_length(result);
if (count) {
/* create list with unique addresses */
for (cpl = 0, result = g_list_first(result);
result != NULL;
result = g_list_next(result)) {
ce = (completion_entry *)(result->data);
if (NULL == g_slist_find(g_completion_addresses,
ce->ref)) {
cpl++;
g_completion_addresses =
g_slist_append(g_completion_addresses,
ce->ref);
}
}
count = cpl + 1; /* index 0 is the original prefix */
g_completion_next = 1; /* we start at the first completed one */
} else {
g_free(g_completion_prefix);
g_completion_prefix = NULL;
}
g_completion_count = count;
g_free(d);
return count;
}
/* get_complete_address() - returns a complete address. the returned
* string should be freed
*/
gchar *get_complete_address(gint index)
{
const address_entry *p;
gchar *address = NULL;
if (index < g_completion_count) {
if (index == 0)
address = g_strdup(g_completion_prefix);
else {
/* get something from the unique addresses */
p = (address_entry *)g_slist_nth_data
(g_completion_addresses, index - 1);
if (p != NULL) {
if (!p->name || p->name[0] == '\0')
address = g_strdup_printf(p->address);
else if (p->name[0] != '"' &&
strpbrk(p->name, ",.[]<>") != NULL)
address = g_strdup_printf
("\"%s\" <%s>", p->name, p->address);
else
address = g_strdup_printf
("%s <%s>", p->name, p->address);
}
}
}
return address;
}
gchar *get_next_complete_address(void)
{
if (is_completion_pending()) {
gchar *res;
res = get_complete_address(g_completion_next);
g_completion_next += 1;
if (g_completion_next >= g_completion_count)
g_completion_next = 0;
return res;
} else
return NULL;
}
gchar *get_prev_complete_address(void)
{
if (is_completion_pending()) {
int n = g_completion_next - 2;
/* real previous */
n = (n + (g_completion_count * 5)) % g_completion_count;
/* real next */
g_completion_next = n + 1;
if (g_completion_next >= g_completion_count)
g_completion_next = 0;
return get_complete_address(n);
} else
return NULL;
}
guint get_completion_count(void)
{
if (is_completion_pending())
return g_completion_count;
else
return 0;
}
/* should clear up anything after complete_address() */
void clear_completion_cache(void)
{
if (is_completion_pending()) {
if (g_completion_prefix)
g_free(g_completion_prefix);
if (g_completion_addresses) {
g_slist_free(g_completion_addresses);
g_completion_addresses = NULL;
}
g_completion_count = g_completion_next = 0;
}
}
gboolean is_completion_pending(void)
{
/* check if completion pending, i.e. we might satisfy a request for the next
* or previous address */
return g_completion_count;
}
/* invalidate_address_completion() - should be called if address book
* changed;
*/
gint invalidate_address_completion(void)
{
if (g_ref_count) {
/* simply the same as start_address_completion() */
debug_print("Invalidation request for address completion\n");
free_all();
init_all();
read_address_book();
if (g_completion_list)
g_completion_add_items(g_completion, g_completion_list);
clear_completion_cache();
}
return g_list_length(g_completion_list);
}
gint end_address_completion(void)
{
clear_completion_cache();
if (0 == --g_ref_count)
free_all();
debug_print("end_address_completion ref count %d\n", g_ref_count);
return g_ref_count;
}
/* address completion entry ui. the ui (completion list was inspired by galeon's
* auto completion list). remaining things powered by sylpheed's completion engine.
*/
#define ENTRY_DATA_TAB_HOOK "tab_hook" /* used to lookup entry */
#define WINDOW_DATA_COMPL_ENTRY "compl_entry" /* used to store entry for compl. window */
#define WINDOW_DATA_COMPL_CLIST "compl_clist" /* used to store clist for compl. window */
static void address_completion_mainwindow_set_focus (GtkWindow *window,
GtkWidget *widget,
gpointer data);
static gboolean address_completion_entry_key_pressed (GtkEntry *entry,
GdkEventKey *ev,
gpointer data);
static gboolean address_completion_complete_address_in_entry
(GtkEntry *entry,
gboolean next);
static void address_completion_create_completion_window (GtkEntry *entry);
static void completion_window_select_row(GtkCList *clist,
gint row,
gint col,
GdkEvent *event,
GtkWidget **completion_window);
static gboolean completion_window_button_press
(GtkWidget *widget,
GdkEventButton *event,
GtkWidget **completion_window);
static gboolean completion_window_key_press
(GtkWidget *widget,
GdkEventKey *event,
GtkWidget **completion_window);
static void completion_window_advance_to_row(GtkCList *clist, gint row)
{
g_return_if_fail(row < g_completion_count);
gtk_clist_select_row(clist, row, 0);
}
static void completion_window_advance_selection(GtkCList *clist, gboolean forward)
{
int row;
g_return_if_fail(clist != NULL);
g_return_if_fail(clist->selection != NULL);
row = GPOINTER_TO_INT(clist->selection->data);
row = forward ? (row + 1) % g_completion_count :
(row - 1) < 0 ? g_completion_count - 1 : row - 1;
gtk_clist_freeze(clist);
completion_window_advance_to_row(clist, row);
gtk_clist_thaw(clist);
}
#if 0
/* completion_window_accept_selection() - accepts the current selection in the
* clist, and destroys the window */
static void completion_window_accept_selection(GtkWidget **window,
GtkCList *clist,
GtkEntry *entry)
{
gchar *address = NULL, *text = NULL;
gint cursor_pos, row;
g_return_if_fail(window != NULL);
g_return_if_fail(*window != NULL);
g_return_if_fail(clist != NULL);
g_return_if_fail(entry != NULL);
g_return_if_fail(clist->selection != NULL);
/* FIXME: I believe it's acceptable to access the selection member directly */
row = GPOINTER_TO_INT(clist->selection->data);
/* we just need the cursor position */
address = get_address_from_edit(entry, &cursor_pos);
g_free(address);
gtk_clist_get_text(clist, row, 0, &text);
replace_address_in_edit(entry, text, cursor_pos);
clear_completion_cache();
gtk_widget_destroy(*window);
*window = NULL;
}
#endif
/* completion_window_apply_selection() - apply the current selection in the
* clist */
static void completion_window_apply_selection(GtkCList *clist, GtkEntry *entry)
{
gchar *address = NULL, *text = NULL;
gint cursor_pos, row;
g_return_if_fail(clist != NULL);
g_return_if_fail(entry != NULL);
g_return_if_fail(clist->selection != NULL);
row = GPOINTER_TO_INT(clist->selection->data);
address = get_address_from_edit(entry, &cursor_pos);
g_free(address);
gtk_clist_get_text(clist, row, 0, &text);
replace_address_in_edit(entry, text, cursor_pos);
}
/* should be called when creating the main window containing address
* completion entries */
void address_completion_start(GtkWidget *mainwindow)
{
start_address_completion();
/* register focus change hook */
g_signal_connect(G_OBJECT(mainwindow), "set_focus",
G_CALLBACK(address_completion_mainwindow_set_focus),
mainwindow);
}
/* Need unique data to make unregistering signal handler possible for the auto
* completed entry */
#define COMPLETION_UNIQUE_DATA (GINT_TO_POINTER(0xfeefaa))
void address_completion_register_entry(GtkEntry *entry)
{
g_return_if_fail(entry != NULL);
g_return_if_fail(GTK_IS_ENTRY(entry));
/* add hooked property */
g_object_set_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK, entry);
/* add keypress event */
g_signal_connect_closure
(G_OBJECT(entry), "key_press_event",
g_cclosure_new
(G_CALLBACK(address_completion_entry_key_pressed),
COMPLETION_UNIQUE_DATA, NULL),
FALSE);
}
void address_completion_unregister_entry(GtkEntry *entry)
{
GObject *entry_obj;
g_return_if_fail(entry != NULL);
g_return_if_fail(GTK_IS_ENTRY(entry));
entry_obj = g_object_get_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK);
g_return_if_fail(entry_obj);
g_return_if_fail(entry_obj == G_OBJECT(entry));
/* has the hooked property? */
g_object_set_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK, NULL);
/* remove the hook */
g_signal_handlers_disconnect_by_func
(G_OBJECT(entry),
G_CALLBACK(address_completion_entry_key_pressed),
COMPLETION_UNIQUE_DATA);
}
/* should be called when main window with address completion entries
* terminates.
* NOTE: this function assumes that it is called upon destruction of
* the window */
void address_completion_end(GtkWidget *mainwindow)
{
/* if address_completion_end() is really called on closing the window,
* we don't need to unregister the set_focus_cb */
end_address_completion();
}
/* if focus changes to another entry, then clear completion cache */
static void address_completion_mainwindow_set_focus(GtkWindow *window,
GtkWidget *widget,
gpointer data)
{
if (widget && GTK_IS_ENTRY(widget) &&
g_object_get_data(G_OBJECT(widget), ENTRY_DATA_TAB_HOOK))
clear_completion_cache();
}
/* watch for tabs in one of the address entries. if no tab then clear the
* completion cache */
static gboolean address_completion_entry_key_pressed(GtkEntry *entry,
GdkEventKey *ev,
gpointer data)
{
if (ev->keyval == GDK_Tab) {
if (address_completion_complete_address_in_entry(entry, TRUE)) {
address_completion_create_completion_window(entry);
/* route a void character to the default handler */
/* this is a dirty hack; we're actually changing a key
* reported by the system. */
ev->keyval = GDK_AudibleBell_Enable;
ev->state &= ~GDK_SHIFT_MASK;
//gtk_signal_emit_stop_by_name(G_OBJECT(entry),
// "key_press_event");
return TRUE; //
} else {
/* old behaviour */
}
} else if (ev->keyval == GDK_Shift_L
|| ev->keyval == GDK_Shift_R
|| ev->keyval == GDK_Control_L
|| ev->keyval == GDK_Control_R
|| ev->keyval == GDK_Caps_Lock
|| ev->keyval == GDK_Shift_Lock
|| ev->keyval == GDK_Meta_L
|| ev->keyval == GDK_Meta_R
|| ev->keyval == GDK_Alt_L
|| ev->keyval == GDK_Alt_R) {
/* these buttons should not clear the cache... */
} else
clear_completion_cache();
return FALSE;
}
/* initialize the completion cache and put first completed string
* in entry. this function used to do back cycling but this is not
* currently used. since the address completion behaviour has been
* changed regularly, we keep the feature in case someone changes
* his / her mind again. :) */
static gboolean address_completion_complete_address_in_entry(GtkEntry *entry,
gboolean next)
{
gint ncount, cursor_pos;
gchar *address, *new = NULL;
gboolean completed = FALSE;
g_return_val_if_fail(entry != NULL, FALSE);
if (!GTK_WIDGET_HAS_FOCUS(entry)) return FALSE;
/* get an address component from the cursor */
address = get_address_from_edit(entry, &cursor_pos);
if (!address) return FALSE;
/* still something in the cache */
if (is_completion_pending()) {
new = next ? get_next_complete_address() :
get_prev_complete_address();
} else {
if (0 < (ncount = complete_address(address)))
new = get_next_complete_address();
}
if (new) {
/* prevent "change" signal */
/* replace_address_in_edit(entry, new, cursor_pos); */
g_free(new);
completed = TRUE;
}
g_free(address);
return completed;
}
static void address_completion_create_completion_window(GtkEntry *entry_)
{
static GtkWidget *completion_window;
gint x, y, height, width, depth;
GtkWidget *scroll, *clist;
GtkRequisition r;
guint count = 0;
GtkWidget *entry = GTK_WIDGET(entry_);
if (completion_window) {
gtk_widget_destroy(completion_window);
completion_window = NULL;
}
scroll = gtk_scrolled_window_new(NULL, NULL);
clist = gtk_clist_new(1);
gtk_clist_set_selection_mode(GTK_CLIST(clist), GTK_SELECTION_SINGLE);
completion_window = gtk_window_new(GTK_WINDOW_POPUP);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
gtk_container_add(GTK_CONTAINER(completion_window), scroll);
gtk_container_add(GTK_CONTAINER(scroll), clist);
/* set the unique data so we can always get back the entry and
* clist window to which this completion window has been attached */
g_object_set_data(G_OBJECT(completion_window),
WINDOW_DATA_COMPL_ENTRY, entry_);
g_object_set_data(G_OBJECT(completion_window),
WINDOW_DATA_COMPL_CLIST, clist);
g_signal_connect(G_OBJECT(clist), "select_row",
G_CALLBACK(completion_window_select_row),
&completion_window);
for (count = 0; count < get_completion_count(); count++) {
gchar *text[] = {NULL, NULL};
text[0] = get_complete_address(count);
gtk_clist_append(GTK_CLIST(clist), text);
g_free(text[0]);
}
gdk_window_get_geometry(entry->window, &x, &y, &width, &height, &depth);
gdk_window_get_deskrelative_origin (entry->window, &x, &y);
y += height;
gtk_window_move(GTK_WINDOW(completion_window), x, y);
gtk_widget_size_request(clist, &r);
gtk_widget_set_size_request(completion_window, width, r.height);
gtk_widget_show_all(completion_window);
gtk_widget_size_request(clist, &r);
if ((y + r.height) > gdk_screen_height()) {
gtk_window_set_policy(GTK_WINDOW(completion_window),
TRUE, FALSE, FALSE);
gtk_widget_set_size_request(completion_window, width,
gdk_screen_height () - y);
}
g_signal_connect(G_OBJECT(completion_window),
"button-press-event",
G_CALLBACK(completion_window_button_press),
&completion_window);
g_signal_connect(G_OBJECT(completion_window),
"key-press-event",
G_CALLBACK(completion_window_key_press),
&completion_window);
gdk_pointer_grab(completion_window->window, TRUE,
GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK |
GDK_BUTTON_RELEASE_MASK,
NULL, NULL, GDK_CURRENT_TIME);
gtk_grab_add(completion_window);
/* this gets rid of the irritating focus rectangle that doesn't
* follow the selection */
GTK_WIDGET_UNSET_FLAGS(clist, GTK_CAN_FOCUS);
gtk_clist_select_row(GTK_CLIST(clist), 1, 0);
}
/* row selection sends completed address to entry.
* note: event is NULL if selected by anything else than a mouse button. */
static void completion_window_select_row(GtkCList *clist, gint row, gint col,
GdkEvent *event,
GtkWidget **completion_window)
{
GtkEntry *entry;
g_return_if_fail(completion_window != NULL);
g_return_if_fail(*completion_window != NULL);
entry = GTK_ENTRY(g_object_get_data(G_OBJECT(*completion_window),
WINDOW_DATA_COMPL_ENTRY));
g_return_if_fail(entry != NULL);
completion_window_apply_selection(clist, entry);
if (!event || event->type != GDK_BUTTON_RELEASE)
return;
clear_completion_cache();
gtk_widget_destroy(*completion_window);
*completion_window = NULL;
}
/* completion_window_button_press() - check is mouse click is anywhere
* else (not in the completion window). in that case the completion
* window is destroyed, and the original prefix is restored */
static gboolean completion_window_button_press(GtkWidget *widget,
GdkEventButton *event,
GtkWidget **completion_window)
{
GtkWidget *event_widget, *entry;
gchar *prefix;
gint cursor_pos;
gboolean restore = TRUE;
g_return_val_if_fail(completion_window != NULL, FALSE);
g_return_val_if_fail(*completion_window != NULL, FALSE);
entry = GTK_WIDGET(g_object_get_data(G_OBJECT(*completion_window),
WINDOW_DATA_COMPL_ENTRY));
g_return_val_if_fail(entry != NULL, FALSE);
event_widget = gtk_get_event_widget((GdkEvent *)event);
if (event_widget != widget) {
while (event_widget) {
if (event_widget == widget)
return FALSE;
else if (event_widget == entry) {
restore = FALSE;
break;
}
event_widget = event_widget->parent;
}
}
if (restore) {
prefix = get_complete_address(0);
g_free(get_address_from_edit(GTK_ENTRY(entry), &cursor_pos));
replace_address_in_edit(GTK_ENTRY(entry), prefix, cursor_pos);
g_free(prefix);
}
clear_completion_cache();
gtk_widget_destroy(*completion_window);
*completion_window = NULL;
return TRUE;
}
static gboolean completion_window_key_press(GtkWidget *widget,
GdkEventKey *event,
GtkWidget **completion_window)
{
GdkEventKey tmp_event;
GtkWidget *entry;
gchar *prefix;
gint cursor_pos;
GtkWidget *clist;
g_return_val_if_fail(completion_window != NULL, FALSE);
g_return_val_if_fail(*completion_window != NULL, FALSE);
entry = GTK_WIDGET(g_object_get_data(G_OBJECT(*completion_window),
WINDOW_DATA_COMPL_ENTRY));
clist = GTK_WIDGET(g_object_get_data(G_OBJECT(*completion_window),
WINDOW_DATA_COMPL_CLIST));
g_return_val_if_fail(entry != NULL, FALSE);
/* allow keyboard navigation in the alternatives clist */
if (event->keyval == GDK_Up || event->keyval == GDK_Down ||
event->keyval == GDK_Page_Up || event->keyval == GDK_Page_Down) {
completion_window_advance_selection
(GTK_CLIST(clist),
event->keyval == GDK_Down ||
event->keyval == GDK_Page_Down ? TRUE : FALSE);
return FALSE;
}
/* also make tab / shift tab go to next previous completion entry. we're
* changing the key value */
if (event->keyval == GDK_Tab || event->keyval == GDK_ISO_Left_Tab) {
event->keyval = (event->state & GDK_SHIFT_MASK)
? GDK_Up : GDK_Down;
/* need to reset shift state if going up */
if (event->state & GDK_SHIFT_MASK)
event->state &= ~GDK_SHIFT_MASK;
completion_window_advance_selection(GTK_CLIST(clist),
event->keyval == GDK_Down ? TRUE : FALSE);
return FALSE;
}
/* look for presses that accept the selection */
if (event->keyval == GDK_Return || event->keyval == GDK_space) {
clear_completion_cache();
gtk_widget_destroy(*completion_window);
*completion_window = NULL;
return FALSE;
}
/* key state keys should never be handled */
if (event->keyval == GDK_Shift_L
|| event->keyval == GDK_Shift_R
|| event->keyval == GDK_Control_L
|| event->keyval == GDK_Control_R
|| event->keyval == GDK_Caps_Lock
|| event->keyval == GDK_Shift_Lock
|| event->keyval == GDK_Meta_L
|| event->keyval == GDK_Meta_R
|| event->keyval == GDK_Alt_L
|| event->keyval == GDK_Alt_R) {
return FALSE;
}
/* other key, let's restore the prefix (orignal text) */
prefix = get_complete_address(0);
g_free(get_address_from_edit(GTK_ENTRY(entry), &cursor_pos));
replace_address_in_edit(GTK_ENTRY(entry), prefix, cursor_pos);
g_free(prefix);
clear_completion_cache();
/* make sure anything we typed comes in the edit box */
tmp_event.type = event->type;
tmp_event.window = entry->window;
tmp_event.send_event = TRUE;
tmp_event.time = event->time;
tmp_event.state = event->state;
tmp_event.keyval = event->keyval;
tmp_event.length = event->length;
tmp_event.string = event->string;
gtk_widget_event(entry, (GdkEvent *)&tmp_event);
/* and close the completion window */
gtk_widget_destroy(*completion_window);
*completion_window = NULL;
return TRUE;
}
|