Statistics
| Revision:

root / src / addr_compl.c @ 1560

History | View | Annotate | Download (26.9 kB)

1
/*
2
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3
 *
4
 * Copyright (C) 2000-2005 by Alfons Hoogervorst & The Sylpheed Claws Team.
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
 */
20
21
#ifdef HAVE_CONFIG_H
22
#  include "config.h"
23
#endif
24
#include "defs.h"
25
26
#include <glib.h>
27
#include <glib/gi18n.h>
28
#include <gdk/gdkkeysyms.h>
29
#include <gtk/gtkmain.h>
30
#include <gtk/gtkwindow.h>
31
#include <gtk/gtkentry.h>
32
#include <gtk/gtkeditable.h>
33
#include <gtk/gtkclist.h>
34
#include <gtk/gtkscrolledwindow.h>
35
36
#include <string.h>
37
#include <ctype.h>
38
39
#include "xml.h"
40
#include "addr_compl.h"
41
#include "utils.h"
42
#include "addressbook.h"
43
#include "main.h"
44
45
/* How it works:
46
 *
47
 * The address book is read into memory. We set up an address list
48
 * containing all address book entries. Next we make the completion
49
 * list, which contains all the completable strings, and store a
50
 * reference to the address entry it belongs to.
51
 * After calling the g_completion_complete(), we get a reference
52
 * to a valid email address.  
53
 *
54
 * Completion is very simplified. We never complete on another prefix,
55
 * i.e. we neglect the next smallest possible prefix for the current
56
 * completion cache. This is simply done so we might break up the
57
 * addresses a little more (e.g. break up alfons@proteus.demon.nl into
58
 * something like alfons, proteus, demon, nl; and then completing on
59
 * any of those words).
60
 */ 
61
        
62
/* address_entry - structure which refers to the original address entry in the
63
 * address book 
64
 */
65
typedef struct
66
{
67
        gchar *name;
68
        gchar *address;
69
} address_entry;
70
71
/* completion_entry - structure used to complete addresses, with a reference
72
 * the the real address information.
73
 */
74
typedef struct
75
{
76
        gchar                *string; /* string to complete */
77
        address_entry        *ref;         /* address the string belongs to  */
78
} completion_entry;
79
80
/*******************************************************************************/
81
82
static gint            g_ref_count;        /* list ref count */
83
static GList            *g_completion_list;        /* list of strings to be checked */
84
static GList            *g_address_list;        /* address storage */
85
static GCompletion *g_completion;        /* completion object */
86
87
/* To allow for continuing completion we have to keep track of the state
88
 * using the following variables. No need to create a context object. */
89
90
static gint            g_completion_count;                /* nr of addresses incl. the prefix */
91
static gint            g_completion_next;                /* next prev address */
92
static GSList           *g_completion_addresses;        /* unique addresses found in the
93
                                                   completion cache. */
94
static gchar           *g_completion_prefix;        /* last prefix. (this is cached here
95
                                                 * because the prefix passed to g_completion
96
                                                 * is g_strdown()'ed */
97
98
/*******************************************************************************/
99
100
/* completion_func() - used by GTK to find the string data to be used for 
101
 * completion 
102
 */
103
static gchar *completion_func(gpointer data)
104
{
105
        g_return_val_if_fail(data != NULL, NULL);
106
107
        return ((completion_entry *)data)->string;
108
} 
109
110
static void init_all(void)
111
{
112
        g_completion = g_completion_new(completion_func);
113
        g_return_if_fail(g_completion != NULL);
114
}
115
116
static void free_all(void)
117
{
118
        GList *walk;
119
        
120
        walk = g_list_first(g_completion_list);
121
        for (; walk != NULL; walk = g_list_next(walk)) {
122
                completion_entry *ce = (completion_entry *) walk->data;
123
                g_free(ce->string);
124
                g_free(walk->data);
125
        }
126
        g_list_free(g_completion_list);
127
        g_completion_list = NULL;
128
        
129
        walk = g_address_list;
130
        for (; walk != NULL; walk = g_list_next(walk)) {
131
                address_entry *ae = (address_entry *) walk->data;
132
                g_free(ae->name);
133
                g_free(ae->address);
134
                g_free(walk->data);
135
        }
136
        g_list_free(g_address_list);
137
        g_address_list = NULL;
138
        
139
        g_completion_free(g_completion);
140
        g_completion = NULL;
141
}
142
143
/* add_address() - adds address to the completion list. this function looks
144
 * complicated, but it's only allocation checks.
145
 */
146
static gint add_address(const gchar *name, const gchar *address)
147
{
148
        address_entry    *ae;
149
        completion_entry *ce1;
150
        completion_entry *ce2;
151
152
        if (!name || !address) return -1;
153
154
        ae = g_new0(address_entry, 1);
155
        ce1 = g_new0(completion_entry, 1),
156
        ce2 = g_new0(completion_entry, 1);
157
158
        g_return_val_if_fail(ae != NULL, -1);
159
        g_return_val_if_fail(ce1 != NULL && ce2 != NULL, -1);        
160
161
        ae->name    = g_strdup(name);
162
        ae->address = g_strdup(address);                
163
164
        /* GCompletion list is case sensitive */
165
        ce1->string = g_utf8_strdown(name, -1);
166
        ce2->string = g_utf8_strdown(address, -1);
167
168
        ce1->ref = ce2->ref = ae;
169
170
        g_completion_list = g_list_append(g_completion_list, ce1);
171
        g_completion_list = g_list_append(g_completion_list, ce2);
172
        g_address_list           = g_list_append(g_address_list,    ae);
173
174
        return 0;
175
}
176
177
/* read_address_book()
178
 */ 
179
static void read_address_book(void) {        
180
        addressbook_load_completion( add_address );
181
}
182
183
/* start_address_completion() - returns the number of addresses 
184
 * that should be matched for completion.
185
 */
186
gint start_address_completion(void)
187
{
188
        clear_completion_cache();
189
        if (!g_ref_count) {
190
                init_all();
191
                /* open the address book */
192
                read_address_book();
193
                /* merge the completion entry list into g_completion */
194
                if (g_completion_list)
195
                        g_completion_add_items(g_completion, g_completion_list);
196
        }
197
        g_ref_count++;
198
        debug_print("start_address_completion ref count %d\n", g_ref_count);
199
200
        return g_list_length(g_completion_list);
201
}
202
203
/* get_address_from_edit() - returns a possible address (or a part)
204
 * from an entry box. To make life easier, we only look at the last valid address 
205
 * component; address completion only works at the last string component in
206
 * the entry box. 
207
 */ 
208
gchar *get_address_from_edit(GtkEntry *entry, gint *start_pos)
209
{
210
        const gchar *edit_text, *p;
211
        gint cur_pos;
212
        gboolean in_quote = FALSE;
213
        gboolean in_bracket = FALSE;
214
        gchar *str;
215
216
        edit_text = gtk_entry_get_text(entry);
217
        if (edit_text == NULL) return NULL;
218
219
        cur_pos = gtk_editable_get_position(GTK_EDITABLE(entry));
220
221
        /* scan for a separator. doesn't matter if walk points at null byte. */
222
        for (p = g_utf8_offset_to_pointer(edit_text, cur_pos);
223
             p > edit_text;
224
             p = g_utf8_prev_char(p)) {
225
                if (*p == '"')
226
                        in_quote ^= TRUE;
227
                else if (!in_quote) {
228
                        if (!in_bracket && *p == ',')
229
                                break;
230
                        else if (*p == '>')
231
                                in_bracket = TRUE;
232
                        else if (*p == '<')
233
                                in_bracket = FALSE;
234
                }
235
        }
236
237
        /* have something valid */
238
        if (g_utf8_strlen(p, -1) == 0)
239
                return NULL;
240
241
#define IS_VALID_CHAR(x) \
242
        (g_ascii_isalnum(x) || (x) == '"' || (x) == '<' || ((guchar)(x) > 0x7f))
243
244
        /* now scan back until we hit a valid character */
245
        for (; *p && !IS_VALID_CHAR(*p); p = g_utf8_next_char(p))
246
                ;
247
248
#undef IS_VALID_CHAR
249
250
        if (g_utf8_strlen(p, -1) == 0)
251
                return NULL;
252
253
        if (start_pos) *start_pos = g_utf8_pointer_to_offset(edit_text, p);
254
255
        str = g_strdup(p);
256
257
        return str;
258
} 
259
260
/* replace_address_in_edit() - replaces an incompleted address with a completed one.
261
 */
262
void replace_address_in_edit(GtkEntry *entry, const gchar *newtext,
263
                             gint start_pos)
264
{
265
        if (!newtext) return;
266
267
        gtk_editable_delete_text(GTK_EDITABLE(entry), start_pos, -1);
268
        gtk_editable_insert_text(GTK_EDITABLE(entry), newtext, strlen(newtext),
269
                                 &start_pos);
270
        gtk_editable_set_position(GTK_EDITABLE(entry), -1);
271
}
272
273
/* complete_address() - tries to complete an addres, and returns the
274
 * number of addresses found. use get_complete_address() to get one.
275
 * returns zero if no match was found, otherwise the number of addresses,
276
 * with the original prefix at index 0. 
277
 */
278
guint complete_address(const gchar *str)
279
{
280
        GList *result;
281
        gchar *d;
282
        guint  count, cpl;
283
        completion_entry *ce;
284
285
        g_return_val_if_fail(str != NULL, 0);
286
287
        clear_completion_cache();
288
        g_completion_prefix = g_strdup(str);
289
290
        /* g_completion is case sensitive */
291
        d = g_utf8_strdown(str, -1);
292
        result = g_completion_complete(g_completion, d, NULL);
293
294
        count = g_list_length(result);
295
        if (count) {
296
                /* create list with unique addresses  */
297
                for (cpl = 0, result = g_list_first(result);
298
                     result != NULL;
299
                     result = g_list_next(result)) {
300
                        ce = (completion_entry *)(result->data);
301
                        if (NULL == g_slist_find(g_completion_addresses,
302
                                                 ce->ref)) {
303
                                cpl++;
304
                                g_completion_addresses =
305
                                        g_slist_append(g_completion_addresses,
306
                                                       ce->ref);
307
                        }
308
                }
309
                count = cpl + 1;        /* index 0 is the original prefix */
310
                g_completion_next = 1;        /* we start at the first completed one */
311
        } else {
312
                g_free(g_completion_prefix);
313
                g_completion_prefix = NULL;
314
        }
315
316
        g_completion_count = count;
317
318
        g_free(d);
319
320
        return count;
321
}
322
323
/* get_complete_address() - returns a complete address. the returned
324
 * string should be freed 
325
 */
326
gchar *get_complete_address(gint index)
327
{
328
        const address_entry *p;
329
        gchar *address = NULL;
330
331
        if (index < g_completion_count) {
332
                if (index == 0)
333
                        address = g_strdup(g_completion_prefix);
334
                else {
335
                        /* get something from the unique addresses */
336
                        p = (address_entry *)g_slist_nth_data
337
                                (g_completion_addresses, index - 1);
338
                        if (p != NULL) {
339
                                if (!p->name || p->name[0] == '\0')
340
                                        address = g_strdup_printf(p->address);
341
                                else if (p->name[0] != '"' &&
342
                                         strpbrk(p->name, ",.[]<>") != NULL)
343
                                        address = g_strdup_printf
344
                                                ("\"%s\" <%s>", p->name, p->address);
345
                                else
346
                                        address = g_strdup_printf
347
                                                ("%s <%s>", p->name, p->address);
348
                        }
349
                }
350
        }
351
352
        return address;
353
}
354
355
gchar *get_next_complete_address(void)
356
{
357
        if (is_completion_pending()) {
358
                gchar *res;
359
360
                res = get_complete_address(g_completion_next);
361
                g_completion_next += 1;
362
                if (g_completion_next >= g_completion_count)
363
                        g_completion_next = 0;
364
365
                return res;
366
        } else
367
                return NULL;
368
}
369
370
gchar *get_prev_complete_address(void)
371
{
372
        if (is_completion_pending()) {
373
                int n = g_completion_next - 2;
374
375
                /* real previous */
376
                n = (n + (g_completion_count * 5)) % g_completion_count;
377
378
                /* real next */
379
                g_completion_next = n + 1;
380
                if (g_completion_next >=  g_completion_count)
381
                        g_completion_next = 0;
382
                return get_complete_address(n);
383
        } else
384
                return NULL;
385
}
386
387
guint get_completion_count(void)
388
{
389
        if (is_completion_pending())
390
                return g_completion_count;
391
        else
392
                return 0;
393
}
394
395
/* should clear up anything after complete_address() */
396
void clear_completion_cache(void)
397
{
398
        if (is_completion_pending()) {
399
                if (g_completion_prefix)
400
                        g_free(g_completion_prefix);
401
402
                if (g_completion_addresses) {
403
                        g_slist_free(g_completion_addresses);
404
                        g_completion_addresses = NULL;
405
                }
406
407
                g_completion_count = g_completion_next = 0;
408
        }
409
}
410
411
gboolean is_completion_pending(void)
412
{
413
        /* check if completion pending, i.e. we might satisfy a request for the next
414
         * or previous address */
415
         return g_completion_count;
416
}
417
418
/* invalidate_address_completion() - should be called if address book
419
 * changed; 
420
 */
421
gint invalidate_address_completion(void)
422
{
423
        if (g_ref_count) {
424
                /* simply the same as start_address_completion() */
425
                debug_print("Invalidation request for address completion\n");
426
                free_all();
427
                init_all();
428
                read_address_book();
429
                if (g_completion_list)
430
                        g_completion_add_items(g_completion, g_completion_list);
431
                clear_completion_cache();
432
        }
433
434
        return g_list_length(g_completion_list);
435
}
436
437
gint end_address_completion(void)
438
{
439
        clear_completion_cache();
440
441
        if (0 == --g_ref_count)
442
                free_all();
443
444
        debug_print("end_address_completion ref count %d\n", g_ref_count);
445
446
        return g_ref_count; 
447
}
448
449
450
/* address completion entry ui. the ui (completion list was inspired by galeon's
451
 * auto completion list). remaining things powered by sylpheed's completion engine.
452
 */
453
454
#define ENTRY_DATA_TAB_HOOK        "tab_hook"                        /* used to lookup entry */
455
#define WINDOW_DATA_COMPL_ENTRY        "compl_entry"        /* used to store entry for compl. window */
456
#define WINDOW_DATA_COMPL_CLIST "compl_clist"        /* used to store clist for compl. window */
457
458
static void address_completion_mainwindow_set_focus        (GtkWindow   *window,
459
                                                         GtkWidget   *widget,
460
                                                         gpointer     data);
461
static gboolean address_completion_entry_key_pressed        (GtkEntry    *entry,
462
                                                         GdkEventKey *ev,
463
                                                         gpointer     data);
464
static gboolean address_completion_complete_address_in_entry
465
                                                        (GtkEntry    *entry,
466
                                                         gboolean     next);
467
static void address_completion_create_completion_window        (GtkEntry    *entry);
468
469
static void completion_window_select_row(GtkCList         *clist,
470
                                         gint                  row,
471
                                         gint                  col,
472
                                         GdkEvent         *event,
473
                                         GtkWidget        **completion_window);
474
static gboolean completion_window_button_press
475
                                        (GtkWidget         *widget,
476
                                         GdkEventButton  *event,
477
                                         GtkWidget        **completion_window);
478
static gboolean completion_window_key_press
479
                                        (GtkWidget         *widget,
480
                                         GdkEventKey         *event,
481
                                         GtkWidget        **completion_window);
482
483
484
static void completion_window_advance_to_row(GtkCList *clist, gint row)
485
{
486
        g_return_if_fail(row < g_completion_count);
487
        gtk_clist_select_row(clist, row, 0);
488
}
489
490
static void completion_window_advance_selection(GtkCList *clist, gboolean forward)
491
{
492
        int row;
493
494
        g_return_if_fail(clist != NULL);
495
        g_return_if_fail(clist->selection != NULL);
496
497
        row = GPOINTER_TO_INT(clist->selection->data);
498
499
        row = forward ? (row + 1) % g_completion_count :
500
                        (row - 1) < 0 ? g_completion_count - 1 : row - 1;
501
502
        gtk_clist_freeze(clist);
503
        completion_window_advance_to_row(clist, row);                                        
504
        gtk_clist_thaw(clist);
505
}
506
507
#if 0
508
/* completion_window_accept_selection() - accepts the current selection in the
509
 * clist, and destroys the window */
510
static void completion_window_accept_selection(GtkWidget **window,
511
                                               GtkCList *clist,
512
                                               GtkEntry *entry)
513
{
514
        gchar *address = NULL, *text = NULL;
515
        gint   cursor_pos, row;
516
517
        g_return_if_fail(window != NULL);
518
        g_return_if_fail(*window != NULL);
519
        g_return_if_fail(clist != NULL);
520
        g_return_if_fail(entry != NULL);
521
        g_return_if_fail(clist->selection != NULL);
522
523
        /* FIXME: I believe it's acceptable to access the selection member directly  */
524
        row = GPOINTER_TO_INT(clist->selection->data);
525
526
        /* we just need the cursor position */
527
        address = get_address_from_edit(entry, &cursor_pos);
528
        g_free(address);
529
        gtk_clist_get_text(clist, row, 0, &text);
530
        replace_address_in_edit(entry, text, cursor_pos);
531
532
        clear_completion_cache();
533
        gtk_widget_destroy(*window);
534
        *window = NULL;
535
}
536
#endif
537
538
/* completion_window_apply_selection() - apply the current selection in the
539
 * clist */
540
static void completion_window_apply_selection(GtkCList *clist, GtkEntry *entry)
541
{
542
        gchar *address = NULL, *text = NULL;
543
        gint   cursor_pos, row;
544
545
        g_return_if_fail(clist != NULL);
546
        g_return_if_fail(entry != NULL);
547
        g_return_if_fail(clist->selection != NULL);
548
549
        row = GPOINTER_TO_INT(clist->selection->data);
550
551
        address = get_address_from_edit(entry, &cursor_pos);
552
        g_free(address);
553
        gtk_clist_get_text(clist, row, 0, &text);
554
        replace_address_in_edit(entry, text, cursor_pos);
555
}
556
557
/* should be called when creating the main window containing address
558
 * completion entries */
559
void address_completion_start(GtkWidget *mainwindow)
560
{
561
        start_address_completion();
562
563
        /* register focus change hook */
564
        g_signal_connect(G_OBJECT(mainwindow), "set_focus",
565
                         G_CALLBACK(address_completion_mainwindow_set_focus),
566
                         mainwindow);
567
}
568
569
/* Need unique data to make unregistering signal handler possible for the auto
570
 * completed entry */
571
#define COMPLETION_UNIQUE_DATA (GINT_TO_POINTER(0xfeefaa))
572
573
void address_completion_register_entry(GtkEntry *entry)
574
{
575
        g_return_if_fail(entry != NULL);
576
        g_return_if_fail(GTK_IS_ENTRY(entry));
577
578
        /* add hooked property */
579
        g_object_set_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK, entry);
580
581
        /* add keypress event */
582
        g_signal_connect_closure
583
                (G_OBJECT(entry), "key_press_event",
584
                 g_cclosure_new
585
                        (G_CALLBACK(address_completion_entry_key_pressed),
586
                         COMPLETION_UNIQUE_DATA, NULL),
587
                 FALSE);
588
}
589
590
void address_completion_unregister_entry(GtkEntry *entry)
591
{
592
        GObject *entry_obj;
593
594
        g_return_if_fail(entry != NULL);
595
        g_return_if_fail(GTK_IS_ENTRY(entry));
596
597
        entry_obj = g_object_get_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK);
598
        g_return_if_fail(entry_obj);
599
        g_return_if_fail(entry_obj == G_OBJECT(entry));
600
601
        /* has the hooked property? */
602
        g_object_set_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK, NULL);
603
604
        /* remove the hook */
605
        g_signal_handlers_disconnect_by_func
606
                (G_OBJECT(entry), 
607
                 G_CALLBACK(address_completion_entry_key_pressed),
608
                 COMPLETION_UNIQUE_DATA);
609
}
610
611
/* should be called when main window with address completion entries
612
 * terminates.
613
 * NOTE: this function assumes that it is called upon destruction of
614
 * the window */
615
void address_completion_end(GtkWidget *mainwindow)
616
{
617
        /* if address_completion_end() is really called on closing the window,
618
         * we don't need to unregister the set_focus_cb */
619
        end_address_completion();
620
}
621
622
/* if focus changes to another entry, then clear completion cache */
623
static void address_completion_mainwindow_set_focus(GtkWindow *window,
624
                                                    GtkWidget *widget,
625
                                                    gpointer   data)
626
{
627
        if (widget && GTK_IS_ENTRY(widget) &&
628
            g_object_get_data(G_OBJECT(widget), ENTRY_DATA_TAB_HOOK)) 
629
                clear_completion_cache();
630
}
631
632
/* watch for tabs in one of the address entries. if no tab then clear the
633
 * completion cache */
634
static gboolean address_completion_entry_key_pressed(GtkEntry    *entry,
635
                                                     GdkEventKey *ev,
636
                                                     gpointer     data)
637
{
638
        if (ev->keyval == GDK_Tab) {
639
                if (address_completion_complete_address_in_entry(entry, TRUE)) {
640
                        address_completion_create_completion_window(entry);
641
                        /* route a void character to the default handler */
642
                        /* this is a dirty hack; we're actually changing a key
643
                         * reported by the system. */
644
                        ev->keyval = GDK_AudibleBell_Enable;
645
                        ev->state &= ~GDK_SHIFT_MASK;
646
                        //gtk_signal_emit_stop_by_name(G_OBJECT(entry),
647
                        //                             "key_press_event");
648
                        return TRUE; //
649
                } else {
650
                        /* old behaviour */
651
                }
652
        } else if (ev->keyval == GDK_Shift_L
653
                || ev->keyval == GDK_Shift_R
654
                || ev->keyval == GDK_Control_L
655
                || ev->keyval == GDK_Control_R
656
                || ev->keyval == GDK_Caps_Lock
657
                || ev->keyval == GDK_Shift_Lock
658
                || ev->keyval == GDK_Meta_L
659
                || ev->keyval == GDK_Meta_R
660
                || ev->keyval == GDK_Alt_L
661
                || ev->keyval == GDK_Alt_R) {
662
                /* these buttons should not clear the cache... */
663
        } else
664
                clear_completion_cache();
665
666
        return FALSE;
667
}
668
669
/* initialize the completion cache and put first completed string
670
 * in entry. this function used to do back cycling but this is not
671
 * currently used. since the address completion behaviour has been
672
 * changed regularly, we keep the feature in case someone changes
673
 * his / her mind again. :) */
674
static gboolean address_completion_complete_address_in_entry(GtkEntry *entry,
675
                                                             gboolean  next)
676
{
677
        gint ncount, cursor_pos;
678
        gchar *address, *new = NULL;
679
        gboolean completed = FALSE;
680
681
        g_return_val_if_fail(entry != NULL, FALSE);
682
683
        if (!GTK_WIDGET_HAS_FOCUS(entry)) return FALSE;
684
685
        /* get an address component from the cursor */
686
        address = get_address_from_edit(entry, &cursor_pos);
687
        if (!address) return FALSE;
688
689
        /* still something in the cache */
690
        if (is_completion_pending()) {
691
                new = next ? get_next_complete_address() :
692
                        get_prev_complete_address();
693
        } else {
694
                if (0 < (ncount = complete_address(address)))
695
                        new = get_next_complete_address();
696
        }
697
698
        if (new) {
699
                /* prevent "change" signal */
700
                /* replace_address_in_edit(entry, new, cursor_pos); */
701
                g_free(new);
702
                completed = TRUE;
703
        }
704
705
        g_free(address);
706
707
        return completed;
708
}
709
710
static void address_completion_create_completion_window(GtkEntry *entry_)
711
{
712
        static GtkWidget *completion_window;
713
        gint x, y, height, width, depth;
714
        GtkWidget *scroll, *clist;
715
        GtkRequisition r;
716
        guint count = 0;
717
        GtkWidget *entry = GTK_WIDGET(entry_);
718
719
        if (completion_window) {
720
                gtk_widget_destroy(completion_window);
721
                completion_window = NULL;
722
        }
723
724
        scroll = gtk_scrolled_window_new(NULL, NULL);
725
        clist  = gtk_clist_new(1);
726
        gtk_clist_set_selection_mode(GTK_CLIST(clist), GTK_SELECTION_SINGLE);
727
        
728
        completion_window = gtk_window_new(GTK_WINDOW_POPUP);
729
730
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
731
                                       GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
732
        gtk_container_add(GTK_CONTAINER(completion_window), scroll);
733
        gtk_container_add(GTK_CONTAINER(scroll), clist);
734
735
        /* set the unique data so we can always get back the entry and
736
         * clist window to which this completion window has been attached */
737
        g_object_set_data(G_OBJECT(completion_window),
738
                          WINDOW_DATA_COMPL_ENTRY, entry_);
739
        g_object_set_data(G_OBJECT(completion_window),
740
                          WINDOW_DATA_COMPL_CLIST, clist);
741
742
        g_signal_connect(G_OBJECT(clist), "select_row",
743
                         G_CALLBACK(completion_window_select_row),
744
                         &completion_window);
745
746
        for (count = 0; count < get_completion_count(); count++) {
747
                gchar *text[] = {NULL, NULL};
748
749
                text[0] = get_complete_address(count);
750
                gtk_clist_append(GTK_CLIST(clist), text);
751
                g_free(text[0]);
752
        }
753
754
        gdk_window_get_geometry(entry->window, &x, &y, &width, &height, &depth);
755
        gdk_window_get_deskrelative_origin (entry->window, &x, &y);
756
        y += height;
757
        gtk_window_move(GTK_WINDOW(completion_window), x, y);
758
759
        gtk_widget_size_request(clist, &r);
760
        gtk_widget_set_size_request(completion_window, width, r.height);
761
        gtk_widget_show_all(completion_window);
762
        gtk_widget_size_request(clist, &r);
763
764
        if ((y + r.height) > gdk_screen_height()) {
765
                gtk_window_set_policy(GTK_WINDOW(completion_window),
766
                                      TRUE, FALSE, FALSE);
767
                gtk_widget_set_size_request(completion_window, width,
768
                                            gdk_screen_height () - y);
769
        }
770
771
        g_signal_connect(G_OBJECT(completion_window),
772
                         "button-press-event",
773
                         G_CALLBACK(completion_window_button_press),
774
                         &completion_window);
775
        g_signal_connect(G_OBJECT(completion_window),
776
                         "key-press-event",
777
                         G_CALLBACK(completion_window_key_press),
778
                         &completion_window);
779
        gdk_pointer_grab(completion_window->window, TRUE,
780
                         GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK |
781
                         GDK_BUTTON_RELEASE_MASK,
782
                         NULL, NULL, GDK_CURRENT_TIME);
783
        gtk_grab_add(completion_window);
784
785
        /* this gets rid of the irritating focus rectangle that doesn't
786
         * follow the selection */
787
        GTK_WIDGET_UNSET_FLAGS(clist, GTK_CAN_FOCUS);
788
        gtk_clist_select_row(GTK_CLIST(clist), 1, 0);
789
}
790
791
792
/* row selection sends completed address to entry.
793
 * note: event is NULL if selected by anything else than a mouse button. */
794
static void completion_window_select_row(GtkCList *clist, gint row, gint col,
795
                                         GdkEvent *event,
796
                                         GtkWidget **completion_window)
797
{
798
        GtkEntry *entry;
799
800
        g_return_if_fail(completion_window != NULL);
801
        g_return_if_fail(*completion_window != NULL);
802
803
        entry = GTK_ENTRY(g_object_get_data(G_OBJECT(*completion_window),
804
                                            WINDOW_DATA_COMPL_ENTRY));
805
        g_return_if_fail(entry != NULL);
806
807
        completion_window_apply_selection(clist, entry);
808
809
        if (!event || event->type != GDK_BUTTON_RELEASE)
810
                return;
811
812
        clear_completion_cache();
813
        gtk_widget_destroy(*completion_window);
814
        *completion_window = NULL;
815
}
816
817
/* completion_window_button_press() - check is mouse click is anywhere
818
 * else (not in the completion window). in that case the completion
819
 * window is destroyed, and the original prefix is restored */
820
static gboolean completion_window_button_press(GtkWidget *widget,
821
                                               GdkEventButton *event,
822
                                               GtkWidget **completion_window)
823
{
824
        GtkWidget *event_widget, *entry;
825
        gchar *prefix;
826
        gint cursor_pos;
827
        gboolean restore = TRUE;
828
829
        g_return_val_if_fail(completion_window != NULL, FALSE);
830
        g_return_val_if_fail(*completion_window != NULL, FALSE);
831
832
        entry = GTK_WIDGET(g_object_get_data(G_OBJECT(*completion_window),
833
                                             WINDOW_DATA_COMPL_ENTRY));
834
        g_return_val_if_fail(entry != NULL, FALSE);
835
836
        event_widget = gtk_get_event_widget((GdkEvent *)event);
837
        if (event_widget != widget) {
838
                while (event_widget) {
839
                        if (event_widget == widget)
840
                                return FALSE;
841
                        else if (event_widget == entry) {
842
                                restore = FALSE;
843
                                break;
844
                        }
845
                    event_widget = event_widget->parent;
846
                }
847
        }
848
849
        if (restore) {
850
                prefix = get_complete_address(0);
851
                g_free(get_address_from_edit(GTK_ENTRY(entry), &cursor_pos));
852
                replace_address_in_edit(GTK_ENTRY(entry), prefix, cursor_pos);
853
                g_free(prefix);
854
        }
855
856
        clear_completion_cache();
857
        gtk_widget_destroy(*completion_window);
858
        *completion_window = NULL;
859
860
        return TRUE;
861
}
862
863
static gboolean completion_window_key_press(GtkWidget *widget,
864
                                            GdkEventKey *event,
865
                                            GtkWidget **completion_window)
866
{
867
        GdkEventKey tmp_event;
868
        GtkWidget *entry;
869
        gchar *prefix;
870
        gint cursor_pos;
871
        GtkWidget *clist;
872
873
        g_return_val_if_fail(completion_window != NULL, FALSE);
874
        g_return_val_if_fail(*completion_window != NULL, FALSE);
875
876
        entry = GTK_WIDGET(g_object_get_data(G_OBJECT(*completion_window),
877
                                             WINDOW_DATA_COMPL_ENTRY));
878
        clist = GTK_WIDGET(g_object_get_data(G_OBJECT(*completion_window),
879
                                             WINDOW_DATA_COMPL_CLIST));
880
        g_return_val_if_fail(entry != NULL, FALSE);
881
882
        /* allow keyboard navigation in the alternatives clist */
883
        if (event->keyval == GDK_Up || event->keyval == GDK_Down ||
884
            event->keyval == GDK_Page_Up || event->keyval == GDK_Page_Down) {
885
                completion_window_advance_selection
886
                        (GTK_CLIST(clist),
887
                         event->keyval == GDK_Down ||
888
                         event->keyval == GDK_Page_Down ? TRUE : FALSE);
889
                return FALSE;
890
        }                
891
892
        /* also make tab / shift tab go to next previous completion entry. we're
893
         * changing the key value */
894
        if (event->keyval == GDK_Tab || event->keyval == GDK_ISO_Left_Tab) {
895
                event->keyval = (event->state & GDK_SHIFT_MASK)
896
                        ? GDK_Up : GDK_Down;
897
                /* need to reset shift state if going up */
898
                if (event->state & GDK_SHIFT_MASK)
899
                        event->state &= ~GDK_SHIFT_MASK;
900
                completion_window_advance_selection(GTK_CLIST(clist), 
901
                        event->keyval == GDK_Down ? TRUE : FALSE);
902
                return FALSE;
903
        }
904
905
        /* look for presses that accept the selection */
906
        if (event->keyval == GDK_Return || event->keyval == GDK_space) {
907
                clear_completion_cache();
908
                gtk_widget_destroy(*completion_window);
909
                *completion_window = NULL;
910
                return FALSE;
911
        }
912
913
        /* key state keys should never be handled */
914
        if (event->keyval == GDK_Shift_L
915
                 || event->keyval == GDK_Shift_R
916
                 || event->keyval == GDK_Control_L
917
                 || event->keyval == GDK_Control_R
918
                 || event->keyval == GDK_Caps_Lock
919
                 || event->keyval == GDK_Shift_Lock
920
                 || event->keyval == GDK_Meta_L
921
                 || event->keyval == GDK_Meta_R
922
                 || event->keyval == GDK_Alt_L
923
                 || event->keyval == GDK_Alt_R) {
924
                return FALSE;
925
        }
926
927
        /* other key, let's restore the prefix (orignal text) */
928
        prefix = get_complete_address(0);
929
        g_free(get_address_from_edit(GTK_ENTRY(entry), &cursor_pos));
930
        replace_address_in_edit(GTK_ENTRY(entry), prefix, cursor_pos);
931
        g_free(prefix);
932
        clear_completion_cache();
933
934
        /* make sure anything we typed comes in the edit box */
935
        if (event->length > 0 && event->keyval != GDK_Escape) {
936
                tmp_event.type       = event->type;
937
                tmp_event.window     = entry->window;
938
                tmp_event.send_event = TRUE;
939
                tmp_event.time       = event->time;
940
                tmp_event.state      = event->state;
941
                tmp_event.keyval     = event->keyval;
942
                tmp_event.length     = event->length;
943
                tmp_event.string     = event->string;
944
                gtk_widget_event(entry, (GdkEvent *)&tmp_event);
945
        }
946
947
        /* and close the completion window */
948
        gtk_widget_destroy(*completion_window);
949
        *completion_window = NULL;
950
951
        return TRUE;
952
}