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
|
/*
* Copyright (C) 2002 Ricardo Fernández Pascual
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <gtk/gtkcellrenderertext.h>
#include <gtk/gtktreeview.h>
#include <gtk/gtkscrolledwindow.h>
#include <gtk/gtktreeselection.h>
#include <gtk/gtkliststore.h>
#include <gtk/gtkwindow.h>
#include <gtk/gtkmain.h>
#include <gtk/gtkvbox.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtkframe.h>
#include "ephy-autocompletion-window.h"
#include "ephy-string.h"
#include "ephy-marshal.h"
#include "ephy-gui.h"
#include "ephy-debug.h"
/* This is copied from gtkscrollbarwindow.c */
#define DEFAULT_SCROLLBAR_SPACING 3
#define SCROLLBAR_SPACING(w) \
(GTK_SCROLLED_WINDOW_GET_CLASS (w)->scrollbar_spacing >= 0 ? \
GTK_SCROLLED_WINDOW_GET_CLASS (w)->scrollbar_spacing : DEFAULT_SCROLLBAR_SPACING)
#define MAX_VISIBLE_ROWS 9
#define MAX_COMPLETION_ALTERNATIVES 7
/**
* Private data
*/
struct _EphyAutocompletionWindowPrivate {
EphyAutocompletion *autocompletion;
GtkWidget *parent;
GtkWidget *window;
GtkScrolledWindow *scrolled_window;
GtkTreeView *tree_view;
GtkTreeViewColumn *col1;
GtkTreeView *action_tree_view;
GtkTreeViewColumn *action_col1;
GtkTreeView *active_tree_view;
gboolean only_actions;
char *selected;
GtkListStore *list_store;
GtkListStore *action_list_store;
guint last_added_match;
int view_nitems;
gboolean shown;
};
/**
* Private functions, only availble from this file
*/
static void ephy_autocompletion_window_class_init (EphyAutocompletionWindowClass *klass);
static void ephy_autocompletion_window_init (EphyAutocompletionWindow *aw);
static void ephy_autocompletion_window_finalize_impl (GObject *o);
static void ephy_autocompletion_window_init_widgets (EphyAutocompletionWindow *aw);
static void ephy_autocompletion_window_selection_changed_cb (GtkTreeSelection *treeselection,
EphyAutocompletionWindow *aw);
static gboolean ephy_autocompletion_window_button_press_event_cb (GtkWidget *widget,
GdkEventButton *event,
EphyAutocompletionWindow *aw);
static gboolean ephy_autocompletion_window_key_press_cb (GtkWidget *widget,
GdkEventKey *event,
EphyAutocompletionWindow *aw);
static void ephy_autocompletion_window_event_after_cb (GtkWidget *wid, GdkEvent *event,
EphyAutocompletionWindow *aw);
static void ephy_autocompletion_window_fill_store_chunk (EphyAutocompletionWindow *aw);
static gpointer g_object_class;
enum EphyAutocompletionWindowSignalsEnum {
ACTIVATED,
SELECTED,
EPHY_AUTOCOMPLETION_WINDOW_HIDDEN,
EPHY_AUTOCOMPLETION_WINDOW_LAST_SIGNAL
};
static gint EphyAutocompletionWindowSignals[EPHY_AUTOCOMPLETION_WINDOW_LAST_SIGNAL];
GType
ephy_autocompletion_window_get_type (void)
{
static GType ephy_autocompletion_window_type = 0;
if (ephy_autocompletion_window_type == 0)
{
static const GTypeInfo our_info =
{
sizeof (EphyAutocompletionWindowClass),
NULL,
NULL,
(GClassInitFunc) ephy_autocompletion_window_class_init,
NULL,
NULL,
sizeof (EphyAutocompletionWindow),
0,
(GInstanceInitFunc) ephy_autocompletion_window_init
};
ephy_autocompletion_window_type = g_type_register_static (G_TYPE_OBJECT,
"EphyAutocompletionWindow",
&our_info, 0);
}
return ephy_autocompletion_window_type;
}
static void
ephy_autocompletion_window_class_init (EphyAutocompletionWindowClass *klass)
{
G_OBJECT_CLASS (klass)->finalize = ephy_autocompletion_window_finalize_impl;
g_object_class = g_type_class_peek_parent (klass);
EphyAutocompletionWindowSignals[ACTIVATED] = g_signal_new (
"activated", G_OBJECT_CLASS_TYPE (klass),
G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP,
G_STRUCT_OFFSET (EphyAutocompletionWindowClass, activated),
NULL, NULL,
ephy_marshal_VOID__STRING_INT,
G_TYPE_NONE,
2,
G_TYPE_STRING,
G_TYPE_INT);
EphyAutocompletionWindowSignals[SELECTED] = g_signal_new (
"selected", G_OBJECT_CLASS_TYPE (klass),
G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP,
G_STRUCT_OFFSET (EphyAutocompletionWindowClass, selected),
NULL, NULL,
ephy_marshal_VOID__STRING_INT,
G_TYPE_NONE,
2,
G_TYPE_STRING,
G_TYPE_INT);
EphyAutocompletionWindowSignals[EPHY_AUTOCOMPLETION_WINDOW_HIDDEN] = g_signal_new (
"hidden", G_OBJECT_CLASS_TYPE (klass),
G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP,
G_STRUCT_OFFSET (EphyAutocompletionWindowClass, hidden),
NULL, NULL,
ephy_marshal_VOID__VOID,
G_TYPE_NONE, 0);
}
static void
ephy_autocompletion_window_init (EphyAutocompletionWindow *aw)
{
EphyAutocompletionWindowPrivate *p = g_new0 (EphyAutocompletionWindowPrivate, 1);
GtkTreeSelection *s;
aw->priv = p;
p->selected = NULL;
ephy_autocompletion_window_init_widgets (aw);
s = gtk_tree_view_get_selection (p->tree_view);
/* I would like to use GTK_SELECTION_SINGLE, but it seems to require that one
item is selected always */
gtk_tree_selection_set_mode (s, GTK_SELECTION_MULTIPLE);
g_signal_connect (s, "changed", G_CALLBACK (ephy_autocompletion_window_selection_changed_cb), aw);
s = gtk_tree_view_get_selection (p->action_tree_view);
gtk_tree_selection_set_mode (s, GTK_SELECTION_MULTIPLE);
g_signal_connect (s, "changed", G_CALLBACK (ephy_autocompletion_window_selection_changed_cb), aw);
}
static void
ephy_autocompletion_window_finalize_impl (GObject *o)
{
EphyAutocompletionWindow *aw = EPHY_AUTOCOMPLETION_WINDOW (o);
EphyAutocompletionWindowPrivate *p = aw->priv;
if (p->list_store) g_object_unref (p->list_store);
if (p->action_list_store) g_object_unref (p->action_list_store);
if (p->parent) g_object_unref (p->parent);
if (p->window) gtk_widget_destroy (p->window);
if (p->autocompletion)
{
g_signal_handlers_disconnect_matched (p->autocompletion, G_SIGNAL_MATCH_DATA, 0, 0,
NULL, NULL, aw);
g_object_unref (p->autocompletion);
}
g_free (p->selected);
g_free (p);
gdk_pointer_ungrab (GDK_CURRENT_TIME);
gdk_keyboard_ungrab (GDK_CURRENT_TIME);
G_OBJECT_CLASS (g_object_class)->finalize (o);
}
static void
ephy_autocompletion_window_init_widgets (EphyAutocompletionWindow *aw)
{
EphyAutocompletionWindowPrivate *p = aw->priv;
GtkWidget *sw;
GtkCellRenderer *renderer;
GtkWidget *frame;
GtkWidget *vbox;
GdkColor *bg_color;
GtkStyle *style;
GValue v = { 0 };
p->window = gtk_window_new (GTK_WINDOW_POPUP);
gtk_window_set_resizable (GTK_WINDOW (p->window), FALSE);
frame = gtk_frame_new (NULL);
gtk_frame_set_shadow_type (GTK_FRAME (frame),
GTK_SHADOW_OUT);
gtk_container_add (GTK_CONTAINER (p->window), frame);
gtk_widget_show (frame);
vbox = gtk_vbox_new (FALSE, 0);
gtk_widget_show (vbox);
gtk_container_add (GTK_CONTAINER (frame), vbox);
sw = gtk_scrolled_window_new (NULL, NULL);
gtk_box_pack_start (GTK_BOX (vbox),
sw, TRUE, TRUE, 0);
gtk_scrolled_window_set_shadow_type
(GTK_SCROLLED_WINDOW(sw), GTK_SHADOW_IN);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
p->scrolled_window = GTK_SCROLLED_WINDOW (sw);
gtk_widget_show (sw);
p->tree_view = GTK_TREE_VIEW (gtk_tree_view_new ());
gtk_container_add (GTK_CONTAINER (sw), GTK_WIDGET (p->tree_view));
gtk_widget_realize (GTK_WIDGET (p->tree_view));
renderer = gtk_cell_renderer_text_new ();
p->col1 = gtk_tree_view_column_new ();
gtk_tree_view_column_pack_start (p->col1, renderer, TRUE);
gtk_tree_view_column_set_attributes (p->col1, renderer,
"text", 0,
NULL);
gtk_tree_view_append_column (p->tree_view, p->col1);
gtk_tree_view_set_headers_visible (p->tree_view, FALSE);
gtk_widget_show (GTK_WIDGET(p->tree_view));
p->action_tree_view = GTK_TREE_VIEW (gtk_tree_view_new ());
gtk_box_pack_start (GTK_BOX (vbox),
GTK_WIDGET (p->action_tree_view),
FALSE, TRUE, 0);
renderer = gtk_cell_renderer_text_new ();
g_value_init (&v, GDK_TYPE_COLOR);
g_object_get_property (G_OBJECT (renderer), "cell_background_gdk", &v);
bg_color = g_value_peek_pointer (&v);
style = gtk_widget_get_style (p->window);
*bg_color = style->bg[GTK_STATE_NORMAL];
g_object_set_property (G_OBJECT (renderer), "cell_background_gdk", &v);
p->action_col1 = gtk_tree_view_column_new ();
gtk_tree_view_column_pack_start (p->action_col1, renderer, TRUE);
gtk_tree_view_column_set_attributes (p->action_col1, renderer,
"text", 0,
NULL);
gtk_tree_view_append_column (p->action_tree_view, p->action_col1);
gtk_tree_view_set_headers_visible (p->action_tree_view, FALSE);
gtk_widget_show (GTK_WIDGET(p->action_tree_view));
}
EphyAutocompletionWindow *
ephy_autocompletion_window_new (EphyAutocompletion *ac, GtkWidget *w)
{
EphyAutocompletionWindow *ret = g_object_new (EPHY_TYPE_AUTOCOMPLETION_WINDOW, NULL);
ephy_autocompletion_window_set_parent_widget (ret, w);
ephy_autocompletion_window_set_autocompletion (ret, ac);
return ret;
}
void
ephy_autocompletion_window_set_parent_widget (EphyAutocompletionWindow *aw, GtkWidget *w)
{
if (aw->priv->parent) g_object_unref (aw->priv->parent);
aw->priv->parent = g_object_ref (w);
}
void
ephy_autocompletion_window_set_autocompletion (EphyAutocompletionWindow *aw,
EphyAutocompletion *ac)
{
EphyAutocompletionWindowPrivate *p = aw->priv;
if (p->autocompletion)
{
g_signal_handlers_disconnect_matched (p->autocompletion, G_SIGNAL_MATCH_DATA, 0, 0,
NULL, NULL, aw);
g_object_unref (p->autocompletion);
}
p->autocompletion = g_object_ref (ac);
}
static void
ephy_autocompletion_window_selection_changed_cb (GtkTreeSelection *treeselection,
EphyAutocompletionWindow *aw)
{
GList *l;
GtkTreeModel *model;
if (aw->priv->selected) g_free (aw->priv->selected);
l = gtk_tree_selection_get_selected_rows (treeselection, &model);
if (l)
{
GtkTreePath *path;
GtkTreeIter iter;
path = (GtkTreePath *)l->data;
gtk_tree_model_get_iter (model, &iter, path);
gtk_tree_model_get (model, &iter, 1,
&aw->priv->selected, -1);
g_list_foreach (l, (GFunc)gtk_tree_path_free, NULL);
g_list_free (l);
}
else
{
aw->priv->selected = NULL;
}
}
static void
ephy_autocompletion_window_get_dimensions (EphyAutocompletionWindow *aw,
int *x, int *y, int *width, int *height)
{
GtkBin *popwin;
GtkWidget *widget;
GtkScrolledWindow *popup;
gint real_height;
GtkRequisition list_requisition;
gboolean show_vscroll = FALSE;
gint avail_height;
gint min_height;
gint alloc_width;
gint work_height;
gint old_height;
gint old_width;
int row_height;
widget = GTK_WIDGET (aw->priv->parent);
popup = GTK_SCROLLED_WINDOW (aw->priv->scrolled_window);
popwin = GTK_BIN (aw->priv->window);
gdk_window_get_origin (widget->window, x, y);
real_height = MIN (widget->requisition.height,
widget->allocation.height);
*y += real_height;
avail_height = gdk_screen_height () - *y;
gtk_widget_size_request (GTK_WIDGET(aw->priv->tree_view),
&list_requisition);
alloc_width = (widget->allocation.width -
2 * popwin->child->style->xthickness -
2 * GTK_CONTAINER (popwin->child)->border_width -
2 * GTK_CONTAINER (popup)->border_width -
2 * GTK_CONTAINER (GTK_BIN (popup)->child)->border_width -
2 * GTK_BIN (popup)->child->style->xthickness);
work_height = (2 * popwin->child->style->ythickness +
2 * GTK_CONTAINER (popwin->child)->border_width +
2 * GTK_CONTAINER (popup)->border_width +
2 * GTK_CONTAINER (GTK_BIN (popup)->child)->border_width +
2 * GTK_BIN (popup)->child->style->ythickness);
min_height = MIN (list_requisition.height,
popup->vscrollbar->requisition.height);
row_height = list_requisition.height / MAX (aw->priv->view_nitems, 1);
LOG ("Real list requisition %d, Items %d", list_requisition.height, aw->priv->view_nitems)
list_requisition.height = MIN (row_height * MAX_VISIBLE_ROWS, list_requisition.height);
LOG ("Row Height %d, Fake list requisition %d", row_height, list_requisition.height)
do
{
old_width = alloc_width;
old_height = work_height;
if (!show_vscroll &&
work_height + list_requisition.height > avail_height)
{
if (work_height + min_height > avail_height &&
*y - real_height > avail_height)
{
*y -= (work_height + list_requisition.height +
real_height);
break;
}
alloc_width -= (popup->vscrollbar->requisition.width +
SCROLLBAR_SPACING (popup));
show_vscroll = TRUE;
}
} while (old_width != alloc_width || old_height != work_height);
*width = widget->allocation.width;
if (*x < 0) *x = 0;
*height = MIN (work_height + list_requisition.height,
avail_height);
/* Action view */
work_height = (2 * GTK_CONTAINER (popup)->border_width +
2 * GTK_WIDGET (popup)->style->ythickness);
if (!GTK_WIDGET_VISIBLE (aw->priv->scrolled_window))
{
*height = work_height;
}
gtk_widget_size_request (GTK_WIDGET(aw->priv->action_tree_view),
&list_requisition);
if (GTK_WIDGET_VISIBLE (aw->priv->action_tree_view))
{
*height += list_requisition.height;
}
}
static void
ephy_autocompletion_window_fill_store_chunk (EphyAutocompletionWindow *aw)
{
EphyAutocompletionWindowPrivate *p = aw->priv;
const EphyAutocompletionMatch *matches;
guint i;
gboolean changed;
guint nmatches;
guint last;
guint completion_nitems = 0, action_nitems = 0, substring_nitems = 0;
START_PROFILER ("Fill store")
nmatches = ephy_autocompletion_get_num_matches (p->autocompletion);
matches = ephy_autocompletion_get_matches_sorted_by_score (p->autocompletion,
&changed);
if (!changed) return;
if (p->list_store) g_object_unref (p->list_store);
p->list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
if (p->action_list_store) g_object_unref (p->action_list_store);
p->action_list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
last = p->last_added_match = 0;
for (i = 0; last < nmatches; i++, last++)
{
const EphyAutocompletionMatch *m = &matches[last];
GtkTreeIter iter;
GtkListStore *store;
if (m->is_action || m->substring ||
completion_nitems <= MAX_COMPLETION_ALTERNATIVES)
{
if (m->is_action)
{
store = p->action_list_store;
action_nitems ++;
}
else if (m->substring)
{
store = p->list_store;
substring_nitems ++;
}
else
{
store = p->list_store;
completion_nitems ++;
}
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter,
0, m->title,
1, m->target,
-1);
}
}
p->view_nitems = substring_nitems + completion_nitems;
gtk_widget_show (GTK_WIDGET (p->scrolled_window));
gtk_widget_show (GTK_WIDGET (p->action_tree_view));
if (p->view_nitems == 0)
{
gtk_widget_hide (GTK_WIDGET (p->scrolled_window));
}
if (action_nitems == 0)
{
gtk_widget_hide (GTK_WIDGET (p->action_tree_view));
}
p->last_added_match = last;
STOP_PROFILER ("Fill store")
}
void
ephy_autocompletion_window_show (EphyAutocompletionWindow *aw)
{
EphyAutocompletionWindowPrivate *p = aw->priv;
gint x, y, height, width;
guint nmatches;
g_return_if_fail (p->window);
g_return_if_fail (p->autocompletion);
nmatches = ephy_autocompletion_get_num_matches (p->autocompletion);
if (nmatches <= 0)
{
ephy_autocompletion_window_hide (aw);
return;
}
START_PROFILER ("Showing window")
ephy_autocompletion_window_fill_store_chunk (aw);
p->only_actions = (!GTK_WIDGET_VISIBLE (p->scrolled_window) ||
GTK_WIDGET_HAS_FOCUS (p->action_tree_view));
if (p->only_actions)
{
p->active_tree_view = p->action_tree_view;
}
else
{
p->active_tree_view = p->tree_view;
}
gtk_tree_view_set_model (p->tree_view, GTK_TREE_MODEL (p->list_store));
gtk_tree_view_set_model (p->action_tree_view, GTK_TREE_MODEL (p->action_list_store));
ephy_autocompletion_window_get_dimensions (aw, &x, &y, &width, &height);
gtk_widget_set_size_request (GTK_WIDGET (p->window), width,
height);
gtk_window_move (GTK_WINDOW (p->window), x, y);
if (!p->shown)
{
gtk_widget_show (p->window);
gdk_pointer_grab (p->parent->window, TRUE,
GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK |
GDK_BUTTON_RELEASE_MASK,
NULL, NULL, GDK_CURRENT_TIME);
gdk_keyboard_grab (p->parent->window, TRUE, GDK_CURRENT_TIME);\
gtk_grab_add (p->window);
g_signal_connect (p->window, "button-press-event",
G_CALLBACK (ephy_autocompletion_window_button_press_event_cb),
aw);
g_signal_connect (p->window, "key-press-event",
G_CALLBACK (ephy_autocompletion_window_key_press_cb),
aw);
g_signal_connect (p->tree_view, "event-after",
G_CALLBACK (ephy_autocompletion_window_event_after_cb),
aw);
g_signal_connect (p->action_tree_view, "event-after",
G_CALLBACK (ephy_autocompletion_window_event_after_cb),
aw);
p->shown = TRUE;
}
gtk_tree_view_scroll_to_point (GTK_TREE_VIEW (p->tree_view), 0, 0);
gtk_widget_grab_focus (GTK_WIDGET (p->tree_view));
STOP_PROFILER ("Showing window")
}
static gboolean
ephy_autocompletion_window_button_press_event_cb (GtkWidget *widget,
GdkEventButton *event,
EphyAutocompletionWindow *aw)
{
GtkWidget *event_widget;
event_widget = gtk_get_event_widget ((GdkEvent *) event);
/* Check to see if button press happened inside the alternatives
window. If not, destroy the window. */
if (event_widget != aw->priv->window)
{
while (event_widget)
{
if (event_widget == aw->priv->window)
return FALSE;
event_widget = event_widget->parent;
}
}
ephy_autocompletion_window_hide (aw);
return TRUE;
}
static GtkTreeView *
hack_tree_view_move_selection (EphyAutocompletionWindow *aw, GtkTreeView *tv,
GtkTreeView *alternate, int dir)
{
GtkTreeSelection *ts = gtk_tree_view_get_selection (tv);
GtkTreeModel *model;
GList *selected = NULL;
gboolean prev_result = TRUE;
selected = gtk_tree_selection_get_selected_rows (ts, &model);
gtk_tree_selection_unselect_all (ts);
if (!selected)
{
GtkTreePath *p = gtk_tree_path_new_first ();
gtk_tree_selection_select_path (ts, p);
gtk_tree_view_scroll_to_cell (tv, p, NULL, FALSE, 0, 0);
gtk_tree_path_free (p);
}
else
{
GtkTreePath *p = selected->data;
int i;
gboolean exit = FALSE;
if (dir > 0)
{
for (i = 0; i < dir; ++i)
{
gtk_tree_path_next (p);
}
}
else
{
for (i = 0; i > dir; --i)
{
prev_result = gtk_tree_path_prev (p);
}
}
if (prev_result)
{
gtk_tree_selection_select_path (ts, p);
gtk_tree_view_scroll_to_cell (tv, p, NULL, FALSE, 0, 0);
}
else
{
g_signal_emit (aw, EphyAutocompletionWindowSignals
[SELECTED], 0, NULL, FALSE);
exit = TRUE;
}
g_list_foreach (selected, (GFunc) gtk_tree_path_free, NULL);
g_list_free (selected);
if (exit) return NULL;
}
if (!prev_result)
{
GtkTreeModel *model;
int c;
GtkTreeIter iter;
GtkTreePath *p;
GtkTreeSelection *selection;
model = gtk_tree_view_get_model (alternate);
c = gtk_tree_model_iter_n_children (model, NULL);
if (c > 0 && alternate)
{
gtk_tree_model_iter_nth_child (model, &iter, NULL, c - 1);
p = gtk_tree_model_get_path (model, &iter);
selection = gtk_tree_view_get_selection (alternate);
gtk_tree_selection_select_path (selection, p);
gtk_tree_view_scroll_to_cell (alternate, p, NULL, FALSE, 0, 0);
gtk_tree_path_free (p);
}
return alternate;
}
else if (gtk_tree_selection_count_selected_rows (ts) == 0)
{
hack_tree_view_move_selection (aw, alternate, tv, dir);
return alternate;
}
return tv;
}
static gboolean
ephy_autocompletion_window_key_press_hack (EphyAutocompletionWindow *aw,
guint keyval)
{
GtkTreeView *tree_view, *alt;
EphyAutocompletionWindowPrivate *p = aw->priv;
gboolean action;
action = (p->active_tree_view == p->action_tree_view);
tree_view = action ? p->action_tree_view : p->tree_view;
alt = action ? p->tree_view : p->action_tree_view;
alt = p->only_actions ? p->action_tree_view : alt;
switch (keyval)
{
case GDK_Up:
p->active_tree_view = hack_tree_view_move_selection
(aw, tree_view, alt, -1);
break;
case GDK_Down:
p->active_tree_view = hack_tree_view_move_selection
(aw, tree_view, alt, +1);
break;
case GDK_Page_Down:
p->active_tree_view = hack_tree_view_move_selection
(aw, tree_view, alt, +5);
break;
case GDK_Page_Up:
p->active_tree_view = hack_tree_view_move_selection
(aw, tree_view, alt, -5);
break;
case GDK_Return:
case GDK_space:
if (aw->priv->selected)
{
g_signal_emit (aw, EphyAutocompletionWindowSignals
[ACTIVATED], 0, aw->priv->selected, action);
}
break;
default:
g_warning ("Unexpected keyval");
break;
}
action = (p->active_tree_view == p->action_tree_view);
switch (keyval)
{
case GDK_Up:
case GDK_Down:
case GDK_Page_Down:
case GDK_Page_Up:
if (aw->priv->selected)
{
g_signal_emit (aw, EphyAutocompletionWindowSignals
[SELECTED], 0, aw->priv->selected, action);
}
break;
default:
break;
}
return TRUE;
}
static gboolean
ephy_autocompletion_window_key_press_cb (GtkWidget *widget,
GdkEventKey *event,
EphyAutocompletionWindow *aw)
{
GdkEventKey tmp_event;
EphyAutocompletionWindowPrivate *p = aw->priv;
GtkWidget *dest_widget;
/* 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
|| ((event->keyval == GDK_space || event->keyval == GDK_Return)
&& p->selected))
{
return ephy_autocompletion_window_key_press_hack
(aw, event->keyval);
}
else
{
dest_widget = p->parent;
}
if (dest_widget != widget)
{
tmp_event = *event;
gtk_widget_event (dest_widget, (GdkEvent *)&tmp_event);
return TRUE;
}
else
{
return FALSE;
}
}
void
ephy_autocompletion_window_hide (EphyAutocompletionWindow *aw)
{
if (aw->priv->window)
{
gtk_widget_hide (aw->priv->window);
gtk_grab_remove (aw->priv->window);
gdk_pointer_ungrab (GDK_CURRENT_TIME);
gdk_keyboard_ungrab (GDK_CURRENT_TIME);
ephy_autocompletion_window_unselect (aw);
g_signal_emit (aw, EphyAutocompletionWindowSignals[EPHY_AUTOCOMPLETION_WINDOW_HIDDEN], 0);
}
g_free (aw->priv->selected);
aw->priv->selected = NULL;
aw->priv->shown = FALSE;
}
void
ephy_autocompletion_window_unselect (EphyAutocompletionWindow *aw)
{
EphyAutocompletionWindowPrivate *p = aw->priv;
GtkTreeSelection *ts = gtk_tree_view_get_selection (p->tree_view);
gtk_tree_selection_unselect_all (ts);
}
static void
ephy_autocompletion_window_event_after_cb (GtkWidget *wid, GdkEvent *event,
EphyAutocompletionWindow *aw)
{
gboolean action;
EphyAutocompletionWindowPrivate *p = aw->priv;
action = (wid == GTK_WIDGET (p->action_tree_view));
if (event->type == GDK_BUTTON_PRESS
&& ((GdkEventButton *) event)->button == 1)
{
if (p->selected)
{
g_signal_emit (aw, EphyAutocompletionWindowSignals
[ACTIVATED], 0, p->selected, action);
}
}
}
|