aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/text/e-completion-view.c
blob: d09176c98a0bc407e044e2c7c29b022221de5015 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* 
 * e-completion-view.c - A text completion selection widget
 * Copyright 2000, 2001, Ximian, Inc.
 *
 * Authors:
 *   Miguel de Icaza <miguel@ximian.com>
 *   Adapted by Jon Trowbridge <trow@ximian.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License, version 2, as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#include <config.h>

#include "e-completion-view.h"

#include <math.h>
#include <gdk/gdkkeysyms.h>
#include <gal/e-table/e-table-simple.h>
#include <gal/e-table/e-table-scrolled.h>
#include <gal/util/e-i18n.h>

enum {
    E_COMPLETION_VIEW_NONEMPTY,
    E_COMPLETION_VIEW_ADDED,
    E_COMPLETION_VIEW_FULL,
    E_COMPLETION_VIEW_BROWSE,
    E_COMPLETION_VIEW_UNBROWSE,
    E_COMPLETION_VIEW_ACTIVATE,
    E_COMPLETION_VIEW_LAST_SIGNAL
};

static guint e_completion_view_signals[E_COMPLETION_VIEW_LAST_SIGNAL] = { 0 };

static void    e_completion_view_disconnect     (ECompletionView *cv);
static ETable *e_completion_view_table          (ECompletionView *cv);
static void    e_completion_view_clear_choices  (ECompletionView *cv);
static void    e_completion_view_set_cursor_row (ECompletionView *cv, gint r);
static void    e_completion_view_select         (ECompletionView *cv, gint r);

static gint    e_completion_view_key_press_handler (GtkWidget *w, GdkEventKey *key_event, gpointer user_data);

static void    e_completion_view_class_init (ECompletionViewClass *klass);
static void    e_completion_view_init       (ECompletionView *completion);
static void    e_completion_view_destroy    (GtkObject *object);

static GtkObjectClass *parent_class;



static gint
e_completion_view_local_key_press_handler (GtkWidget *w, GdkEventKey *ev)
{
    return e_completion_view_key_press_handler (w, ev, w);
}

static void
e_completion_view_paint (GtkWidget *widget, GdkRectangle *area)
{
    gint i;

    g_return_if_fail (widget != NULL);
    g_return_if_fail (E_IS_COMPLETION_VIEW (widget));
    g_return_if_fail (area != NULL);

    if (!GTK_WIDGET_DRAWABLE (widget))
        return;

    for (i = 0; i < E_COMPLETION_VIEW (widget)->border_width; ++i) {

        gdk_draw_rectangle (widget->window,
                    widget->style->black_gc,
                    FALSE, i, i, 
                    widget->allocation.width-1-2*i,
                    widget->allocation.height-1-2*i);

    }
    
}

#if 0
static void
e_completion_view_draw (GtkWidget *widget, GdkRectangle *area)
{
    GtkBin *bin;
    GdkRectangle child_area;

    g_return_if_fail (widget != NULL);
    g_return_if_fail (E_IS_COMPLETION_VIEW (widget));
    g_return_if_fail (area != NULL);

    if (GTK_WIDGET_DRAWABLE (widget)) {
        bin = GTK_BIN (widget);

        e_completion_view_paint (widget, area);

        if (bin->child && gtk_widget_intersect (bin->child, area, &child_area))
            gtk_widget_draw (bin->child, &child_area);
    }
}
#endif

static gint
e_completion_view_expose_event (GtkWidget *widget, GdkEventExpose *event)             
{
    GtkBin *bin;
    GdkEventExpose child_event;

    g_return_val_if_fail (widget != NULL, FALSE);
    g_return_val_if_fail (E_IS_COMPLETION_VIEW (widget), FALSE);
    g_return_val_if_fail (event != NULL, FALSE);

    if (GTK_WIDGET_DRAWABLE (widget)) {
        bin = GTK_BIN (widget);

        e_completion_view_paint (widget, &event->area);

        child_event = *event;
        if (bin->child &&
            GTK_WIDGET_NO_WINDOW (bin->child) &&
            gtk_widget_intersect (bin->child, &event->area, &child_event.area))
            gtk_widget_event (bin->child, (GdkEvent*) &child_event);
    }

    return FALSE;
}

static void
e_completion_view_size_request (GtkWidget *widget, GtkRequisition *requisition)
{
    GtkBin *bin;

    g_return_if_fail (widget != NULL);
    g_return_if_fail (E_IS_COMPLETION_VIEW (widget));
    g_return_if_fail (requisition != NULL);

    bin = GTK_BIN (widget);
    
    requisition->width  = 2 * E_COMPLETION_VIEW (widget)->border_width;
    requisition->height = 2 * E_COMPLETION_VIEW (widget)->border_width;

    if (bin->child && GTK_WIDGET_VISIBLE (bin->child)) {
        GtkRequisition child_requisition;
        
        gtk_widget_size_request (bin->child, &child_requisition);
        
        requisition->width += child_requisition.width;
        requisition->height += child_requisition.height;
    }
}

static void
e_completion_view_size_allocate (GtkWidget *widget, GtkAllocation *allocation)               
{
    GtkBin *bin;
    GtkAllocation child_allocation;

    g_return_if_fail (widget != NULL);
    g_return_if_fail (E_IS_COMPLETION_VIEW (widget));
    g_return_if_fail (allocation != NULL);

    bin = GTK_BIN (widget);
    widget->allocation = *allocation;

    child_allocation.x = E_COMPLETION_VIEW (widget)->border_width;
    child_allocation.width = MAX(0, (gint)allocation->width - child_allocation.x * 2);

    child_allocation.y = E_COMPLETION_VIEW (widget)->border_width;
    child_allocation.height = MAX (0, (gint)allocation->height - child_allocation.y * 2);

    if (GTK_WIDGET_REALIZED (widget)) {
        gdk_window_move_resize (widget->window,
                    allocation->x,
                    allocation->y,
                    allocation->width,
                    allocation->height);
    }
    
    if (bin->child) {
        gtk_widget_size_allocate (bin->child, &child_allocation);
    }
}

GtkType
e_completion_view_get_type (void)
{
    static GtkType completion_view_type = 0;
  
    if (!completion_view_type) {
        GtkTypeInfo completion_view_info = {
            "ECompletionView",
            sizeof (ECompletionView),
            sizeof (ECompletionViewClass),
            (GtkClassInitFunc) e_completion_view_class_init,
            (GtkObjectInitFunc) e_completion_view_init,
            NULL, NULL, /* reserved */
            (GtkClassInitFunc) NULL
        };

        completion_view_type = gtk_type_unique (gtk_event_box_get_type (), &completion_view_info);
    }

    return completion_view_type;
}

static void
e_completion_view_class_init (ECompletionViewClass *klass)
{
    GtkObjectClass *object_class = (GtkObjectClass *) klass;
    GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

    parent_class = GTK_OBJECT_CLASS (gtk_type_class (gtk_event_box_get_type ()));

    e_completion_view_signals[E_COMPLETION_VIEW_NONEMPTY] =
        gtk_signal_new ("nonempty",
                GTK_RUN_LAST,
                E_OBJECT_CLASS_TYPE (object_class),
                GTK_SIGNAL_OFFSET (ECompletionViewClass, nonempty),
                gtk_marshal_NONE__NONE,
                GTK_TYPE_NONE, 0);

    e_completion_view_signals[E_COMPLETION_VIEW_ADDED] =
        gtk_signal_new ("added",
                GTK_RUN_LAST,
                E_OBJECT_CLASS_TYPE (object_class),
                GTK_SIGNAL_OFFSET (ECompletionViewClass, added),
                gtk_marshal_NONE__NONE,
                GTK_TYPE_NONE, 0);

    e_completion_view_signals[E_COMPLETION_VIEW_FULL] =
        gtk_signal_new ("full",
                GTK_RUN_LAST,
                E_OBJECT_CLASS_TYPE (object_class),
                GTK_SIGNAL_OFFSET (ECompletionViewClass, full),
                gtk_marshal_NONE__NONE,
                GTK_TYPE_NONE, 0);

    e_completion_view_signals[E_COMPLETION_VIEW_BROWSE] =
        gtk_signal_new ("browse",
                GTK_RUN_LAST,
                E_OBJECT_CLASS_TYPE (object_class),
                GTK_SIGNAL_OFFSET (ECompletionViewClass, browse),
                gtk_marshal_NONE__POINTER,
                GTK_TYPE_NONE, 1,
                GTK_TYPE_POINTER);

    e_completion_view_signals[E_COMPLETION_VIEW_UNBROWSE] =
        gtk_signal_new ("unbrowse",
                GTK_RUN_LAST,
                E_OBJECT_CLASS_TYPE (object_class),
                GTK_SIGNAL_OFFSET (ECompletionViewClass, unbrowse),
                gtk_marshal_NONE__NONE,
                GTK_TYPE_NONE, 0);

    e_completion_view_signals[E_COMPLETION_VIEW_ACTIVATE] =
        gtk_signal_new ("activate",
                GTK_RUN_LAST,
                E_OBJECT_CLASS_TYPE (object_class),
                GTK_SIGNAL_OFFSET (ECompletionViewClass, activate),
                gtk_marshal_NONE__POINTER,
                GTK_TYPE_NONE, 1,
                GTK_TYPE_POINTER);

    E_OBJECT_CLASS_ADD_SIGNALS (object_class, e_completion_view_signals, E_COMPLETION_VIEW_LAST_SIGNAL);

    object_class->destroy = e_completion_view_destroy;

    widget_class->key_press_event = e_completion_view_local_key_press_handler;
    widget_class->expose_event = e_completion_view_expose_event;
    widget_class->size_request = e_completion_view_size_request;
    widget_class->size_allocate = e_completion_view_size_allocate;
}

static void
e_completion_view_init (ECompletionView *completion)
{
    completion->border_width = 2;
    completion->choices = g_ptr_array_new ();
}

static void
e_completion_view_destroy (GtkObject *object)
{
    ECompletionView *cv = E_COMPLETION_VIEW (object);

    e_completion_view_disconnect (cv);

    if (cv->choices) {
        e_completion_view_clear_choices (cv);

        g_ptr_array_free (cv->choices, TRUE);
        cv->choices = NULL;
    }

    if (cv->key_widget) {
        gtk_signal_disconnect (GTK_OBJECT (cv->key_widget), cv->key_signal_id);
        gtk_object_unref (GTK_OBJECT (cv->key_widget));
        cv->key_widget = NULL;
    }

    if (cv->completion)
        gtk_object_unref (GTK_OBJECT (cv->completion));
    cv->completion = NULL;

    if (parent_class->destroy)
        (parent_class->destroy) (object);
}

static void
e_completion_view_disconnect (ECompletionView *cv)
{
    g_return_if_fail (cv != NULL);
    g_return_if_fail (E_IS_COMPLETION_VIEW (cv));

    if (cv->begin_signal_id)
        gtk_signal_disconnect (GTK_OBJECT (cv->completion), cv->begin_signal_id);
    if (cv->comp_signal_id)
        gtk_signal_disconnect (GTK_OBJECT (cv->completion), cv->comp_signal_id);
    if (cv->restart_signal_id)
        gtk_signal_disconnect (GTK_OBJECT (cv->completion), cv->restart_signal_id);
    if (cv->cancel_signal_id)
        gtk_signal_disconnect (GTK_OBJECT (cv->completion), cv->cancel_signal_id);
    if (cv->end_signal_id)
        gtk_signal_disconnect (GTK_OBJECT (cv->completion), cv->end_signal_id);
    if (cv->clear_signal_id)
        gtk_signal_disconnect (GTK_OBJECT (cv->completion), cv->clear_signal_id);
    if (cv->lost_signal_id)
        gtk_signal_disconnect (GTK_OBJECT (cv->completion), cv->lost_signal_id);

    cv->begin_signal_id   = 0;
    cv->comp_signal_id    = 0;
    cv->restart_signal_id = 0;
    cv->cancel_signal_id   = 0;
    cv->end_signal_id     = 0;
    cv->clear_signal_id   = 0;
    cv->lost_signal_id    = 0;
}

static ETable *
e_completion_view_table (ECompletionView *cv)
{
    return e_table_scrolled_get_table (E_TABLE_SCROLLED (cv->table));
}

static void
e_completion_view_clear_choices (ECompletionView *cv)
{
    ECompletionMatch *match;
    GPtrArray *m;
    int i;

    g_return_if_fail (E_IS_COMPLETION_VIEW (cv));

    m = cv->choices;
    for (i = 0; i < m->len; i++) {
        match = g_ptr_array_index (m, i);
        e_completion_match_unref (match);
    }
    g_ptr_array_set_size (m, 0);
}

static void
e_completion_view_set_cursor_row (ECompletionView *cv, gint r)
{
    ETable *table;
    GtkAdjustment *adj;
    gint x, y1, y2, r1, r2, c;
    double fracline;
    gint iteration_count=0;

    g_return_if_fail (cv != NULL);
    g_return_if_fail (E_IS_COMPLETION_VIEW (cv));
#ifndef G_DISABLE_CHECKS
    /* choices->len is unsigned, but it is reasonable for r to be
     * < 0 */
    if (r > 0) {
        g_return_if_fail (r < cv->choices->len);
    }
#endif

    adj = e_scroll_frame_get_vadjustment (E_SCROLL_FRAME (cv->table));  

    table = e_completion_view_table (cv);

    if (r < 0) {
        e_selection_model_clear (E_SELECTION_MODEL(table->selection));
        
        /* Move back to the top when we clear the selection */
        gtk_adjustment_set_value (adj, adj->lower);
        return;
    }

    e_table_set_cursor_row (table, r);

    /* OK, now the tricky bit.  We try to insure that this row is
       visible. */
    
    /* If we are selecting the first or last row, then it is easy.  We just
       cram the vadjustment all the way up/down. */
    if (r == 0) {
        gtk_adjustment_set_value (adj, adj->lower);
        return;
    } else if (r == cv->choices->len - 1) {     
        gtk_adjustment_set_value (adj, adj->upper - adj->page_size);
        return;
    }
    
    fracline = ((adj->upper - adj->lower - adj->page_size) / (gint)cv->choices->len) / 4;

    while (iteration_count < 100) {
        x = GTK_LAYOUT(table->table_canvas)->hadjustment->value;
        y1 = GTK_LAYOUT(table->table_canvas)->vadjustment->value;
        
        y2 = y1 + cv->table->allocation.height;
        
        e_table_group_compute_location (e_completion_view_table (cv)->group, &x, &y1, &r1, &c);
        e_table_group_compute_location (e_completion_view_table (cv)->group, &x, &y2, &r2, &c);
    
        if (r <= r1) {
            gtk_adjustment_set_value (adj, adj->value - fracline);
        } else if (r >= r2) {
            gtk_adjustment_set_value (adj, adj->value + fracline);
        } else 
            return;

        ++iteration_count;
    }

    g_assert_not_reached ();
}

static void
e_completion_view_select (ECompletionView *cv, gint r)
{
    ECompletionMatch *match;

    match = g_ptr_array_index (cv->choices, r);

    cv->selection = r;
    e_completion_view_set_cursor_row (cv, r);
    gtk_signal_emit (GTK_OBJECT (cv), e_completion_view_signals[E_COMPLETION_VIEW_ACTIVATE], match);
}

static gint
e_completion_view_key_press_handler (GtkWidget *w, GdkEventKey *key_event, gpointer user_data)
{
    ECompletionView *cv = E_COMPLETION_VIEW (user_data);
    gint dir = 0;
    gboolean key_handled = TRUE, complete_key = FALSE, uncomplete_key = FALSE, is_space = FALSE;

    /* FIXME: This is totally lame.
       The ECompletionView should be able to specify multiple completion/uncompletion keys, or just
       have sensible defaults. */
    
    if ((cv->complete_key && key_event->keyval == cv->complete_key)
        || ((key_event->keyval == GDK_n || key_event->keyval == GDK_N) && (key_event->state & GDK_CONTROL_MASK)))
        complete_key = TRUE;

    if ((cv->uncomplete_key && key_event->keyval == cv->uncomplete_key)
        || ((key_event->keyval == GDK_p || key_event->keyval == GDK_P) && (key_event->state & GDK_CONTROL_MASK)))
        uncomplete_key = TRUE;
    
    /* Start up a completion.*/
    if (complete_key && !cv->editable) {
        gtk_signal_emit (GTK_OBJECT (cv), e_completion_view_signals[E_COMPLETION_VIEW_BROWSE], NULL);
        goto stop_emission;
    }

    /* Stop our completion. */
    if (uncomplete_key && cv->editable && cv->selection < 0) {
        e_completion_view_set_cursor_row (cv, -1);
        gtk_signal_emit (GTK_OBJECT (cv), e_completion_view_signals[E_COMPLETION_VIEW_UNBROWSE]);
        goto stop_emission;
    }

    if (!cv->editable)
        return FALSE;

    switch (key_event->keyval) {

    case GDK_n:
    case GDK_N:
        /* We (heart) emacs: treat ctrl-n as down */
        if (! (key_event->state & GDK_CONTROL_MASK))
            return FALSE;

    case GDK_Down:
    case GDK_KP_Down:
        dir = 1;
        break;

    case GDK_p:
    case GDK_P:
        /* Treat ctrl-p as up */
        if (! (key_event->state & GDK_CONTROL_MASK))
            return FALSE;
        
    case GDK_Up:
    case GDK_KP_Up:
        dir = -1;
        break;
        
    case GDK_Tab:
        /* If our cursor is still up in the entry, move down into
           the popup.  Otherwise unbrowse. */
        if (cv->choices->len > 0) {
            if (cv->selection < 0) {
                cv->selection = 0;
                dir = 0;
            } else {
                cv->selection = -1;
                dir = 0;
                key_handled = FALSE;
            }
        }
        break;

    case GDK_space:
    case GDK_KP_Space:
        is_space = TRUE;

    case GDK_Return:
    case GDK_KP_Enter:
        if (cv->selection < 0) {
            /* We don't have a selection yet, move to the first selection if there is
               more than one option.  If there is only one option, select it automatically. */

            /* Let space pass through. */
            if (is_space)
                return FALSE;
            
            if (cv->choices->len == 1) {
                e_completion_view_select (cv, 0);
                goto stop_emission;
            } else {
                cv->selection = 0;
                dir = 0;
            }

        } else {
            /* Our cursor is down in the pop-up, so we make our selection. */
            e_completion_view_select (cv, cv->selection);
            goto stop_emission;
        }
        break;

    case GDK_Escape:
        /* Unbrowse hack */
        cv->selection = -1;
        dir = 0;
        break;

    default:
        return FALSE;
    }

    cv->selection += dir;

    if (cv->selection >= (int)cv->choices->len) {
        cv->selection = cv->choices->len - 1;
        /* Don't re-emit the browse signal */
        goto stop_emission;
    }

    e_completion_view_set_cursor_row (cv, cv->selection);

    if (cv->selection >= 0)
        gtk_signal_emit (GTK_OBJECT (cv), e_completion_view_signals[E_COMPLETION_VIEW_BROWSE], 
                 g_ptr_array_index (cv->choices, cv->selection));
    else
        gtk_signal_emit (GTK_OBJECT (cv), e_completion_view_signals[E_COMPLETION_VIEW_UNBROWSE]);

 stop_emission:
    
    if (key_handled)
        gtk_signal_emit_stop_by_name (GTK_OBJECT (w), "key_press_event");

    return key_handled;
}

static void
begin_completion_cb (ECompletion *completion, const gchar *txt, gint pos, gint limit, gpointer user_data)
{
    ECompletionView *cv = E_COMPLETION_VIEW (user_data);

    e_completion_view_clear_choices (cv);
    cv->have_all_choices = FALSE;

    e_table_model_changed (cv->model);
}

static void
restart_completion_cb (ECompletion *completion, gpointer user_data)
{
    /* For now, handle restarts like the beginning of a new completion. */
    begin_completion_cb (completion, NULL, 0, 0, user_data);
}

static void
cancel_completion_cb (ECompletion *completion, gpointer user_data)
{
    ECompletionView *cv = E_COMPLETION_VIEW (user_data);

    /* On a cancel, clear our choices and issue an "unbrowse" signal. */
    e_completion_view_clear_choices (cv);
    cv->have_all_choices = TRUE;
    e_completion_view_set_cursor_row (cv, -1);
    e_table_model_changed (cv->model);

    gtk_signal_emit (GTK_OBJECT (cv), e_completion_view_signals[E_COMPLETION_VIEW_UNBROWSE]);
}

static void
completion_cb (ECompletion *completion, ECompletionMatch *match, gpointer user_data)
{
    ECompletionView *cv = E_COMPLETION_VIEW (user_data);
    gint r = cv->choices->len;
    gboolean first = (cv->choices->len == 0);

    e_completion_match_ref (match);
    g_ptr_array_add (cv->choices, match);

    e_table_model_row_inserted (cv->model, r);

    if (first)
        gtk_signal_emit (GTK_OBJECT (cv), e_completion_view_signals[E_COMPLETION_VIEW_NONEMPTY]);

    gtk_signal_emit (GTK_OBJECT (cv), e_completion_view_signals[E_COMPLETION_VIEW_ADDED]);
}

static void
end_completion_cb (ECompletion *completion, gpointer user_data)
{
    ECompletionView *cv = E_COMPLETION_VIEW (user_data);

    /* Do a final refresh of the table. */
    e_table_model_changed (cv->model);

    cv->have_all_choices = TRUE;
    gtk_signal_emit (GTK_OBJECT (cv), e_completion_view_signals[E_COMPLETION_VIEW_FULL]);
}

static void
clear_completion_cb (ECompletion *completion, gpointer user_data)
{
    ECompletionView *cv = E_COMPLETION_VIEW (user_data);

    e_completion_view_clear_choices (cv);
    cv->have_all_choices = FALSE;

    e_table_model_changed (cv->model);
}

static void
lost_completion_cb (ECompletion *completion, ECompletionMatch *match, gpointer user_data)
{
    ECompletionView *cv = E_COMPLETION_VIEW (user_data);
    int i;
    GPtrArray *c = cv->choices;

    for (i = 0; i < c->len; i++)
        if (g_ptr_array_index (c, i) == match)
            break;

    g_return_if_fail (i == c->len);

    /* FIXME: do remove_index_fast(), then row_changed and
     * row_deleted (if there are more than 1 row still) */
    g_ptr_array_remove_index (c, i);
    e_table_model_row_deleted (cv->model, i);
    
    e_completion_match_unref (match);
}

/*** Table Callbacks ***/

static char *simple_spec = 
"<ETableSpecification no-headers=\"true\" draw-grid=\"false\" cursor-mode=\"line\" alternating-row-colors=\"false\" gettext-domain=\"" E_I18N_DOMAIN "\">"
"  <ETableColumn model_col=\"0\" _title=\"Node\" expansion=\"1.0\" "
"         minimum_width=\"16\" resizable=\"true\" cell=\"string\" "
"         compare=\"string\"/> "
"       <ETableState>                  "
"           <column source=\"0\"/> "
"           <grouping></grouping>  "
"        </ETableState>                "
"</ETableSpecification>";

static gint
table_col_count (ETableModel *etm, gpointer data)
{
    return 1;
}

static gint
table_row_count (ETableModel *etm, gpointer data)
{
    ECompletionView *cv = E_COMPLETION_VIEW (data);
    return cv->choices->len;
}

static gboolean
table_is_cell_editable (ETableModel *etm, gint c, gint r, gpointer data)
{
    return FALSE;
}

static gpointer
table_value_at (ETableModel *etm, gint c, gint r, gpointer data)
{
    ECompletionView *cv = E_COMPLETION_VIEW (data);
    ECompletionMatch *match;

    match = g_ptr_array_index (cv->choices, r);

    return (gpointer) e_completion_match_get_menu_text (match);
}

static gchar *
table_value_to_string (ETableModel *em, gint col, gconstpointer val, gpointer data)
{
    return (gchar *) val;
}

static void
table_click_cb (ETable *et, gint r, gint c, GdkEvent *ev, gpointer data)
{
    ECompletionView *cv = E_COMPLETION_VIEW (data);

    e_completion_view_select (cv, r);
}

void
e_completion_view_construct (ECompletionView *cv, ECompletion *completion)
{
    g_return_if_fail (cv != NULL);
    g_return_if_fail (E_IS_COMPLETION_VIEW (cv));
    g_return_if_fail (completion != NULL);
    g_return_if_fail (E_IS_COMPLETION (completion));

    /* Make sure we don't call construct twice. */
    g_return_if_fail (cv->completion == NULL);

    GTK_WIDGET_SET_FLAGS (GTK_WIDGET (cv), GTK_CAN_FOCUS);

    cv->completion = completion;
    gtk_object_ref (GTK_OBJECT (completion));

    cv->begin_signal_id   = gtk_signal_connect (GTK_OBJECT (completion),
                            "begin_completion",
                            GTK_SIGNAL_FUNC (begin_completion_cb),
                            cv);
    cv->comp_signal_id    = gtk_signal_connect (GTK_OBJECT (completion),
                            "completion",
                            GTK_SIGNAL_FUNC (completion_cb),
                            cv);
    cv->restart_signal_id = gtk_signal_connect (GTK_OBJECT (completion),
                            "restart_completion",
                            GTK_SIGNAL_FUNC (restart_completion_cb),
                            cv);
    cv->cancel_signal_id  = gtk_signal_connect (GTK_OBJECT (completion),
                            "cancel_completion",
                            GTK_SIGNAL_FUNC (cancel_completion_cb),
                            cv);
    cv->end_signal_id     = gtk_signal_connect (GTK_OBJECT (completion),
                            "end_completion",
                            GTK_SIGNAL_FUNC (end_completion_cb),
                            cv);
    cv->clear_signal_id   = gtk_signal_connect (GTK_OBJECT (completion),
                            "clear_completion",
                            GTK_SIGNAL_FUNC (clear_completion_cb),
                            cv);
    cv->lost_signal_id    = gtk_signal_connect (GTK_OBJECT (completion),
                            "lost_completion",
                            GTK_SIGNAL_FUNC (lost_completion_cb),
                            cv);

    cv->model = e_table_simple_new (table_col_count,
                    table_row_count,
                    NULL,

                    table_value_at,
                    NULL,
                    table_is_cell_editable,

                    NULL, NULL,

                    NULL, NULL, NULL, NULL,
                    table_value_to_string,
                    cv);

    cv->table = e_table_scrolled_new (cv->model, NULL, simple_spec, NULL);
    gtk_object_unref (GTK_OBJECT (cv->model));

    e_scroll_frame_set_shadow_type (E_SCROLL_FRAME (cv->table), GTK_SHADOW_NONE);
    e_scroll_frame_set_scrollbar_spacing (E_SCROLL_FRAME (cv->table), 0);
    e_scroll_frame_set_policy (E_SCROLL_FRAME (cv->table), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);

    gtk_container_add (GTK_CONTAINER (cv), cv->table);
    gtk_widget_show_all (cv->table);

    gtk_signal_connect (GTK_OBJECT (e_completion_view_table (cv)),
                "click",
                GTK_SIGNAL_FUNC (table_click_cb),
                cv);

    cv->selection = -1;
}

GtkWidget *
e_completion_view_new (ECompletion *completion)
{
    gpointer p;

    g_return_val_if_fail (completion != NULL, NULL);
    g_return_val_if_fail (E_IS_COMPLETION (completion), NULL);

    p = gtk_type_new (e_completion_view_get_type ());

    e_completion_view_construct (E_COMPLETION_VIEW (p), completion);

    return GTK_WIDGET (p);
}

void
e_completion_view_connect_keys (ECompletionView *cv, GtkWidget *w)
{
    g_return_if_fail (cv != NULL);
    g_return_if_fail (E_IS_COMPLETION_VIEW (cv));
    g_return_if_fail (w == NULL || GTK_IS_WIDGET (w));

    if (cv->key_widget) {
        gtk_signal_disconnect (GTK_OBJECT (cv->key_widget), cv->key_signal_id);
        gtk_object_unref (GTK_OBJECT (cv->key_widget));
    }

    if (w) {
        cv->key_widget = w;
        gtk_object_ref (GTK_OBJECT (w));

        cv->key_signal_id = gtk_signal_connect (GTK_OBJECT (w),
                            "key_press_event",
                            GTK_SIGNAL_FUNC (e_completion_view_key_press_handler),
                            cv);
    } else {
        cv->key_widget = NULL;
        cv->key_signal_id = 0;
    }
}

void
e_completion_view_set_complete_key (ECompletionView *cv, gint keyval)
{
    g_return_if_fail (cv != NULL);
    g_return_if_fail (E_IS_COMPLETION_VIEW (cv));

    cv->complete_key = keyval;
}

void
e_completion_view_set_uncomplete_key (ECompletionView *cv, gint keyval)
{
    g_return_if_fail (cv != NULL);
    g_return_if_fail (E_IS_COMPLETION_VIEW (cv));

    cv->uncomplete_key = keyval;
}

void
e_completion_view_set_width (ECompletionView *cv, gint width)
{
    GtkWidget *w;
    gint y, r, dummy, line_height, final_height;
    double drop_room, lines;

    g_return_if_fail (cv != NULL);
    g_return_if_fail (E_IS_COMPLETION_VIEW (cv));
    g_return_if_fail (width > 0);

    w = GTK_WIDGET (cv);

    if (! GTK_WIDGET_REALIZED (w)) {
        gtk_widget_set_usize (w, width, -1);
        return;
    }

    /* A Horrible Hack(tm) to figure out the height of a single table row */
    
    for (line_height=5, r=0; r == 0 && line_height < 1000; line_height += 2) {
        dummy = 0;
        e_table_group_compute_location (e_completion_view_table (cv)->group,
                        &dummy, &line_height, &r, &dummy);
    }

    if (line_height >= 1000) {
        /* Something went wrong, so we make a (possibly very lame) guess */
        line_height = 30;
    }


    gdk_window_get_origin (w->window, NULL, &y);
    y += w->allocation.y;

    lines = 5; /* default maximum */
    lines = MIN (lines, cv->choices->len);
    
    drop_room = (gdk_screen_height () - y) / (double)line_height;
    drop_room = MAX (drop_room, 1);

    lines = MIN (lines, drop_room);

    /* We reduce the total height by a bit; in practice, this seems to work out well. */
    final_height = (gint) floor (line_height * (0.5 + (float)lines) * 0.97);
    gtk_widget_set_usize (w, width, final_height);
}

void
e_completion_view_set_editable (ECompletionView *cv, gboolean x)
{
    g_return_if_fail (cv != NULL);
    g_return_if_fail (E_IS_COMPLETION_VIEW (cv));

    if (x == cv->editable)
        return;

    cv->editable = x;
    cv->selection = -1;
    e_completion_view_set_cursor_row (cv, -1);
}