summaryrefslogtreecommitdiff
path: root/scrollarea.c
blob: d5514cd4d32c8a83cac0db0568559c026f3944e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
#include <gdk/gdkprivate.h> /* For GDK_PARENT_RELATIVE_BG */
#include <gdk/gdkx.h>
#include <X11/Xlib.h>
#include "scrollarea.h"
#include "foo-marshal.h"
#include "backingstore.h"


G_DEFINE_TYPE (FooScrollArea, foo_scroll_area, GTK_TYPE_CONTAINER);

static GtkWidgetClass *parent_class;

typedef struct InputPath InputPath;
typedef struct InputRegion InputRegion;
typedef struct AutoScrollInfo AutoScrollInfo;

struct InputPath
{
    gboolean			is_stroke;
    cairo_fill_rule_t		fill_rule;
    double			line_width;
    cairo_path_t	       *path;		/* In canvas coordinates */

    FooScrollAreaEventFunc	func;
    gpointer			data;

    InputPath		       *next;
};

/* InputRegions are mutually disjoint */
struct InputRegion
{
    GdkRegion *region;		/* the boundary of this area in
				 * canvas coordinates
				 */
    InputPath *paths;
};

struct AutoScrollInfo
{
    int				dx;
    int				dy;
    int				timeout_id;
    int				begin_x;
    int				begin_y;
    double			res_x;
    double			res_y;
    GTimer		       *timer;
};

struct FooScrollAreaPrivate
{
    int				width;
    int				height;
    
    GtkAdjustment	       *hadj;
    GtkAdjustment	       *vadj;
    int			        x_offset;
    int				y_offset;
    
    int				min_width;
    int				min_height;

    GPtrArray		       *input_regions;
    
    AutoScrollInfo	       *auto_scroll_info;
    
    /* During expose, this region is set to the region
     * being exposed. At other times, it is NULL
     *
     * It is used for clipping of input areas
     */
    InputRegion		       *current_input;
    
    gboolean			grabbed;
    FooScrollAreaEventFunc	grab_func;
    gpointer			grab_data;

    BackingStore	       *store;
    
    guint32			motion_notify_time;
};

enum
{
    VIEWPORT_SIZE_CHANGED,
    VIEWPORT_CHANGED,
    PAINT,
    INPUT,
    LAST_SIGNAL,
};

static guint signals [LAST_SIGNAL] = { 0 };

static void     foo_scroll_area_size_request           (GtkWidget        *widget,
							GtkRequisition   *requisition);
static gboolean foo_scroll_area_expose                 (GtkWidget        *widget,
							GdkEventExpose   *expose);
static void     foo_scroll_area_size_allocate          (GtkWidget        *widget,
							GtkAllocation    *allocation);
static void     foo_scroll_area_set_scroll_adjustments (FooScrollArea    *scroll_area,
							GtkAdjustment    *hadj,
							GtkAdjustment    *vadj);
static void     foo_scroll_area_realize                (GtkWidget        *widget);
static void     foo_scroll_area_unrealize              (GtkWidget        *widget);
static void     foo_scroll_area_map                    (GtkWidget        *widget);
static void     foo_scroll_area_unmap                  (GtkWidget        *widget);
static gboolean foo_scroll_area_button_press           (GtkWidget        *widget,
							GdkEventButton   *event);
static gboolean foo_scroll_area_button_release         (GtkWidget        *widget,
							GdkEventButton   *event);
static gboolean foo_scroll_area_motion                 (GtkWidget        *widget,
							GdkEventMotion   *event);
static gboolean foo_scroll_area_crossing               (GtkWidget        *widget,
							GdkEventCrossing *event);


static void
foo_scroll_area_map (GtkWidget *widget)
{
    GTK_WIDGET_CLASS (parent_class)->map (widget);
}

static void
foo_scroll_area_unmap (GtkWidget *widget)
{
    GTK_WIDGET_CLASS (parent_class)->unmap (widget);
}

static void
foo_scroll_area_finalize (GObject *object)
{
    FooScrollArea *scroll_area = FOO_SCROLL_AREA (object);
    
    g_object_unref (scroll_area->priv->hadj);
    g_object_unref (scroll_area->priv->vadj);
    
    g_ptr_array_free (scroll_area->priv->input_regions, TRUE);
    
    g_free (scroll_area->priv);

    G_OBJECT_CLASS (foo_scroll_area_parent_class)->finalize (object);
}

static void
foo_scroll_area_class_init (FooScrollAreaClass *class)
{
    GObjectClass *object_class = G_OBJECT_CLASS (class);
    GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
    
    object_class->finalize = foo_scroll_area_finalize;
    widget_class->size_request = foo_scroll_area_size_request;
    widget_class->expose_event = foo_scroll_area_expose;
    widget_class->size_allocate = foo_scroll_area_size_allocate;
    widget_class->realize = foo_scroll_area_realize;
    widget_class->unrealize = foo_scroll_area_unrealize;
    widget_class->button_press_event = foo_scroll_area_button_press;
    widget_class->button_release_event = foo_scroll_area_button_release;
    widget_class->motion_notify_event = foo_scroll_area_motion;
    widget_class->enter_notify_event = foo_scroll_area_crossing;
    widget_class->leave_notify_event = foo_scroll_area_crossing;
    widget_class->map = foo_scroll_area_map;
    widget_class->unmap = foo_scroll_area_unmap;
    
    class->set_scroll_adjustments = foo_scroll_area_set_scroll_adjustments;
    
    parent_class = g_type_class_peek_parent (class);

    /* Emitted when the viewport changes size, but not when it only changes position */
    signals[VIEWPORT_SIZE_CHANGED] =
	g_signal_new ("viewport_size_changed",
		      G_OBJECT_CLASS_TYPE (object_class),
		      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		      G_STRUCT_OFFSET (FooScrollAreaClass,
				       viewport_size_changed),
		      NULL, NULL,
		      foo_marshal_VOID__BOXED_BOXED,
		      G_TYPE_NONE, 2,
		      GDK_TYPE_RECTANGLE,
		      GDK_TYPE_RECTANGLE);

    /* Emitted when the viewport changes position or size or both */
    signals[VIEWPORT_CHANGED] =
	g_signal_new ("viewport_changed",
		      G_OBJECT_CLASS_TYPE (object_class),
		      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		      G_STRUCT_OFFSET (FooScrollAreaClass,
				       viewport_changed),
		      NULL, NULL,
		      foo_marshal_VOID__BOXED_BOXED,
		      G_TYPE_NONE, 2,
		      GDK_TYPE_RECTANGLE,
		      GDK_TYPE_RECTANGLE);
    
    signals[PAINT] =
	g_signal_new ("paint",
		      G_OBJECT_CLASS_TYPE (object_class),
		      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		      G_STRUCT_OFFSET (FooScrollAreaClass,
				       paint),
		      NULL, NULL,
		      foo_marshal_VOID__POINTER_BOXED_POINTER,
		      G_TYPE_NONE, 3,
		      G_TYPE_POINTER,
		      GDK_TYPE_RECTANGLE, 
		      G_TYPE_POINTER);
    
    widget_class->set_scroll_adjustments_signal =
	g_signal_new ("set_scroll_adjustments",
		      G_OBJECT_CLASS_TYPE (object_class),
		      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		      G_STRUCT_OFFSET (FooScrollAreaClass,
				       set_scroll_adjustments),
		      NULL, NULL,
		      foo_marshal_VOID__OBJECT_OBJECT,
		      G_TYPE_NONE, 2,
		      GTK_TYPE_ADJUSTMENT,
		      GTK_TYPE_ADJUSTMENT);
}

static GtkAdjustment *
new_adjustment (void)
{
    return GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
}

static void
foo_scroll_area_init (FooScrollArea *scroll_area)
{
#if 0
    GTK_WIDGET_SET_FLAGS (scroll_area, GTK_NO_WINDOW);
#endif
    
    gtk_widget_set_redraw_on_allocate (GTK_WIDGET (scroll_area), FALSE);
    
    scroll_area->priv = g_new0 (FooScrollAreaPrivate, 1);
    scroll_area->priv->width = 0;
    scroll_area->priv->height = 0;
    scroll_area->priv->hadj = g_object_ref_sink (new_adjustment());
    scroll_area->priv->vadj = g_object_ref_sink (new_adjustment());
    scroll_area->priv->x_offset = 0.0;
    scroll_area->priv->y_offset = 0.0;
    scroll_area->priv->min_width = -1;
    scroll_area->priv->min_height = -1;
    scroll_area->priv->auto_scroll_info = NULL;
    scroll_area->priv->input_regions = g_ptr_array_new ();

    gtk_widget_set_double_buffered (GTK_WIDGET (scroll_area), FALSE);
}

static void
translate_cairo_device (cairo_t       *cr,
			int            x_offset,
			int            y_offset)
{
    cairo_surface_t *surface = cairo_get_target (cr);
    double dev_x;
    double dev_y;
    
    cairo_surface_get_device_offset (surface, &dev_x, &dev_y);
    dev_x += x_offset;
    dev_y += y_offset;
    cairo_surface_set_device_offset (surface, dev_x, dev_y);
}

#if 0
static void
print_region (const char *header, GdkRegion *region)
{
    GdkRectangle *rects;
    int n_rects;
    int i;
    
    g_print ("%s\n", header);
    
    gdk_region_get_rectangles (region, &rects, &n_rects);
    for (i = 0; i < n_rects; ++i)
    {
	GdkRectangle *rect = &(rects[i]);
	g_print ("  %d %d %d %d\n",
		 rect->x, rect->y, rect->width, rect->height);
    }
}
#endif

typedef void (* PathForeachFunc) (double  *x,
				  double  *y,
				  gpointer data);

static void
path_foreach_point (cairo_path_t     *path,
		    PathForeachFunc   func,
		    gpointer	      user_data)
{
    int i;
    
    for (i = 0; i < path->num_data; i += path->data[i].header.length)
    {
	cairo_path_data_t *data = &(path->data[i]);
	
	switch (data->header.type)
	{
	case CAIRO_PATH_MOVE_TO:
	case CAIRO_PATH_LINE_TO:
	    func (&(data[1].point.x), &(data[1].point.y), user_data);
	    break;
	    
	case CAIRO_PATH_CURVE_TO:
	    func (&(data[1].point.x), &(data[1].point.y), user_data);
	    func (&(data[2].point.x), &(data[2].point.y), user_data);
	    func (&(data[3].point.x), &(data[3].point.y), user_data);
	    break;
	    
	case CAIRO_PATH_CLOSE_PATH:
	    break;
	}
    }
}

typedef struct
{
    double x1, y1, x2, y2;
} Box;

#if 0
static void
update_box (double *x, double *y, gpointer data)
{
    Box *box = data;
    
    if (*x < box->x1)
	box->x1 = *x;

    if (*y < box->y1)
	box->y1 = *y;

    if (*y > box->y2)
	box->y2 = *y;
    
    if (*x > box->x2)
	box->x2 = *x;
}
#endif

#if 0
static void
path_compute_extents (cairo_path_t *path,
		      GdkRectangle *rect)
{
    if (rect)
    {
	Box box = { G_MAXDOUBLE, G_MAXDOUBLE, G_MINDOUBLE, G_MINDOUBLE };

	path_foreach_point (path, update_box, &box);

	rect->x = box.x1;
	rect->y = box.y1;
	rect->width = box.x2 - box.x1;
	rect->height = box.y2 - box.y1;
    }
}
#endif

static void
input_path_free_list (InputPath *paths)
{
    if (!paths)
	return;

    input_path_free_list (paths->next);
    cairo_path_destroy (paths->path);
    g_free (paths);
}

static void
input_region_free (InputRegion *region)
{
    input_path_free_list (region->paths);
    gdk_region_destroy (region->region);

    g_free (region);
}

static void
get_viewport (FooScrollArea *scroll_area,
	      GdkRectangle  *viewport)
{
    GtkWidget *widget = GTK_WIDGET (scroll_area);
    
    viewport->x = scroll_area->priv->x_offset;
    viewport->y = scroll_area->priv->y_offset;
    viewport->width = widget->allocation.width;
    viewport->height = widget->allocation.height;
}

static void
allocation_to_canvas (FooScrollArea *area,
		      int           *x,
		      int           *y)
{
    *x += area->priv->x_offset;
    *y += area->priv->y_offset;
}

static void
clear_exposed_input_region (FooScrollArea *area,
			    GdkRegion *exposed)	/* in canvas coordinates */
{
    int i;
    GdkRegion *viewport_reg;
    GdkRectangle viewport;

    get_viewport (area, &viewport);
    
    viewport_reg = gdk_region_rectangle (&viewport);
    gdk_region_subtract (viewport_reg, exposed);
    
    for (i = 0; i < area->priv->input_regions->len; ++i)
    {
	InputRegion *region = area->priv->input_regions->pdata[i];

	gdk_region_intersect (region->region, viewport_reg);

	if (gdk_region_empty (region->region))
	{
	    input_region_free (region);
	    g_ptr_array_remove_index_fast (area->priv->input_regions, i--);
	}
    }

    gdk_region_destroy (viewport_reg);
}

static void
setup_background_cr (GdkWindow *window,
		     cairo_t   *cr,
		     int        x_offset,
		     int        y_offset)
{
    GdkWindowObject *private = (GdkWindowObject *)window;
    
    if (private->bg_pixmap == GDK_PARENT_RELATIVE_BG && private->parent)
    {
	x_offset += private->x;
	y_offset += private->y;
	
	setup_background_cr (GDK_WINDOW (private->parent), cr, x_offset, y_offset);
    }
    else if (private->bg_pixmap &&
	     private->bg_pixmap != GDK_PARENT_RELATIVE_BG &&
	     private->bg_pixmap != GDK_NO_BG)
    {
	gdk_cairo_set_source_pixmap (cr, private->bg_pixmap, -x_offset, -y_offset);
    }
    else
    {
	gdk_cairo_set_source_color (cr, &private->bg_color);
    }
}

static void
canvas_to_window (FooScrollArea *area,
		  GdkRegion *region)
{
    gdk_region_offset (region,
		       -area->priv->x_offset,
		       -area->priv->y_offset);
}

static void
window_to_canvas (FooScrollArea *area,
		  GdkRegion *region)
{
    gdk_region_offset (region,
		       area->priv->x_offset,
		       area->priv->y_offset);
}

static void
canvas_to_store (FooScrollArea *area,
		 GdkRegion *region)
{
    gdk_region_offset (region, -area->priv->x_offset, -area->priv->y_offset);
		       
}

static void
store_to_canvas (FooScrollArea *area,
		 GdkRegion *region)
{
    gdk_region_offset (region, area->priv->x_offset, area->priv->y_offset);
}

static gboolean paint_updates = FALSE;

static void
paint_region (FooScrollArea *area, GdkRegion *region, double r, double g, double b, double a)
{
    cairo_t *cr = gdk_cairo_create (GTK_WIDGET (area)->window);
    gdk_cairo_region (cr, region);
    cairo_clip (cr);
    
    cairo_set_source_rgba (cr, r, g, b, a);
    
    cairo_paint (cr);
    
    cairo_destroy (cr);
}

static void
do_exposes (cairo_t *cr,
	    GdkRegion *store_region,
	    gpointer data)
{
    FooScrollArea *scroll_area = data;
    GdkRegion *region;
    GdkRectangle rect;
    GdkRegion *r;

    /* Paint background */
    setup_background_cr (GTK_WIDGET (scroll_area)->window, cr, 0, 0);
    cairo_paint (cr);

    if (paint_updates)
	paint_region (scroll_area, store_region, 0.2, 0.2, 0.8, 0.4);
    
    /* Note that this function can be called at a time
     * where the adj->value is different from x_offset. 
     * Ie., the GtkScrolledWindow changed the adj->value
     * without emitting the value_changed signal. 
     *
     * Hence we must always use the value we got 
     * the last time the signal was emitted, ie.,
     * priv->{x,y}_offset.
     */
    region = gdk_region_copy (store_region);

    store_to_canvas (scroll_area, region);

    /* Set up input areas */
    clear_exposed_input_region (scroll_area, region);
    
    scroll_area->priv->current_input = g_new0 (InputRegion, 1);
    scroll_area->priv->current_input->region = gdk_region_copy (region);
    scroll_area->priv->current_input->paths = NULL;
    g_ptr_array_add (scroll_area->priv->input_regions,
		     scroll_area->priv->current_input);

    /* Set up cairo context and emit the signal */
    translate_cairo_device (cr,
			    -scroll_area->priv->x_offset,
			    -scroll_area->priv->y_offset);

    /* Restrict to the actual canvas */
    rect.x = 0;
    rect.y = 0;
    rect.width = scroll_area->priv->width;
    rect.height = scroll_area->priv->height;

    r = gdk_region_rectangle (&rect);
    gdk_region_intersect (region, r);
    gdk_region_destroy (r);

    cairo_rectangle (cr, rect.x, rect.y, rect.width, rect.height);
    cairo_clip (cr);
    
    if (!gdk_region_empty (region))
    {
	gdk_region_get_clipbox (region, &rect);

	g_signal_emit (scroll_area, signals[PAINT], 0, cr, &rect, region);
    }
    
    scroll_area->priv->current_input = NULL;
    gdk_region_destroy (region);
}

static gboolean
foo_scroll_area_expose (GtkWidget *widget,
			GdkEventExpose *expose)
{
    FooScrollArea *area = FOO_SCROLL_AREA (widget);

    backing_store_process_updates (area->priv->store, do_exposes, area);

    if (paint_updates)
    {
	paint_region (area, expose->region, 0.8, 0.2, 0.2, 0.4);
    
	gdk_flush ();
	g_usleep (70000);
    }
    
    backing_store_draw (area->priv->store, widget->window,
			expose->region, 0, 0);

    return TRUE;
}

void
foo_scroll_area_get_viewport (FooScrollArea *scroll_area,
			      GdkRectangle  *viewport)
{
    g_return_if_fail (FOO_IS_SCROLL_AREA (scroll_area));
    
    if (!viewport)
	return;
    
    get_viewport (scroll_area, viewport);
}

static void
process_event (FooScrollArea	       *scroll_area,
	       FooScrollAreaEventType	input_type,
	       int			x,
	       int			y,
	       int			button);

static void
emit_viewport_signals (FooScrollArea *scroll_area,
		       GdkRectangle  *new_viewport,
		       GdkRectangle  *old_viewport)
{
    gboolean position_changed;
    gboolean size_changed;

    size_changed = (new_viewport->width != old_viewport->width ||
		    new_viewport->height != old_viewport->height);
    
    position_changed = (new_viewport->x != old_viewport->x ||
			new_viewport->y != old_viewport->y);
    
    if (size_changed || position_changed)
    {
	int px, py;
	
	g_signal_emit (scroll_area, signals[VIEWPORT_CHANGED], 0, 
		       new_viewport, old_viewport);

	if (size_changed)
	{
	    g_signal_emit (scroll_area, signals[VIEWPORT_SIZE_CHANGED], 0,
			   new_viewport, old_viewport);
	}

	/* Fake an event when the viewport changes
	 * FIXME: keep track of the last position in the window
	 */
	gdk_window_get_pointer (GTK_WIDGET (scroll_area)->window,
				&px, &py, NULL);
	    
	process_event (scroll_area, FOO_MOTION, px, py, 0);
    }
}

static void
clamp_adjustment (GtkAdjustment *adj)
{
    double old_value = adj->value;
    
    if (adj->upper >= adj->page_size)
	adj->value = CLAMP (adj->value, 0.0, adj->upper - adj->page_size);
    else
	adj->value = 0.0;
    
    if (old_value != adj->value)
	gtk_adjustment_value_changed (adj);
    
    gtk_adjustment_changed (adj);
}

static gboolean
set_adjustment_values (FooScrollArea *scroll_area)
{
    GtkAllocation *allocation = &GTK_WIDGET (scroll_area)->allocation;
    
    GtkAdjustment *hadj = scroll_area->priv->hadj;
    GtkAdjustment *vadj = scroll_area->priv->vadj;

    /* Horizontal */
    hadj->page_size = allocation->width;
    hadj->step_increment = 0.1 * allocation->width;
    hadj->page_increment = 0.9 * allocation->width;
    hadj->lower = 0.0;
    hadj->upper = scroll_area->priv->width;
    
    /* Vertical */
    vadj->page_size = allocation->height;
    vadj->step_increment = 0.1 * allocation->height;
    vadj->page_increment = 0.9 * allocation->height;
    vadj->lower = 0.0;
    vadj->upper = scroll_area->priv->height;

    clamp_adjustment (hadj);
    clamp_adjustment (vadj);
    
    return TRUE;
}

static void
foo_scroll_area_realize (GtkWidget *widget)
{
    FooScrollArea *area = FOO_SCROLL_AREA (widget);
    GdkWindowAttr attributes;
    gint attributes_mask;

    GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
    
    attributes.window_type = GDK_WINDOW_CHILD;
    attributes.x = widget->allocation.x;
    attributes.y = widget->allocation.y;
    attributes.width = widget->allocation.width;
    attributes.height = widget->allocation.height;
    attributes.wclass = GDK_INPUT_OUTPUT;
    attributes.event_mask = gtk_widget_get_events (widget);
    attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
			      GDK_BUTTON_RELEASE_MASK |
			      GDK_BUTTON1_MOTION_MASK |
			      GDK_BUTTON2_MOTION_MASK |
			      GDK_BUTTON3_MOTION_MASK |
			      GDK_POINTER_MOTION_MASK |
			      GDK_EXPOSURE_MASK |
			      GDK_ENTER_NOTIFY_MASK |
			      GDK_LEAVE_NOTIFY_MASK);
    attributes.visual = gtk_widget_get_visual (widget);
    attributes.colormap = gtk_widget_get_colormap (widget);
    
    attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
    
    widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
				     &attributes, attributes_mask);
    gdk_window_set_user_data (widget->window, area);
    
    area->priv->store = backing_store_new (widget->window, widget->allocation.width,
					   widget->allocation.height);

    widget->style = gtk_style_attach (widget->style, widget->window);

    gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
}

static void
foo_scroll_area_unrealize (GtkWidget *widget)
{
    FooScrollArea *area = FOO_SCROLL_AREA (widget);
    
#if 0
    if (area->priv->input_window)
    {
	gdk_window_set_user_data (area->priv->input_window, NULL);
	gdk_window_destroy (area->priv->input_window);
	area->priv->input_window = NULL;
    }
#endif

    backing_store_free (area->priv->store);
    
    GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
}

#if 0
static void
allocation_to_canvas_region (FooScrollArea *area,
			     GdkRegion *region)
{
    gdk_region_offset (region, area->priv->x_offset, area->priv->y_offset);
}
#endif

static void
foo_scroll_area_size_allocate (GtkWidget     *widget,
			       GtkAllocation *allocation)
{
    FooScrollArea *scroll_area = FOO_SCROLL_AREA (widget);
    GdkRectangle new_viewport;
    GdkRectangle old_viewport;

    get_viewport (scroll_area, &old_viewport);
    
    widget->allocation = *allocation;
    
    if (GTK_WIDGET_REALIZED (widget))
    {
	gdk_window_move_resize (widget->window,
				allocation->x, allocation->y,
				allocation->width, allocation->height);

	/* FIXME: We should possibly restrict the size of the
	 * backing store to be not more than the size of
	 * the area itself. The only problem with that is that
	 * we still need to paint the background of the exposed
	 * areas outside the area.
	 */
	backing_store_resize (scroll_area->priv->store,
			      widget->allocation.width,
			      widget->allocation.height);
    }

    set_adjustment_values (scroll_area);
    
    get_viewport (scroll_area, &new_viewport);

    emit_viewport_signals (scroll_area, &new_viewport, &old_viewport);
}

static void
emit_input (FooScrollArea *scroll_area,
	    FooScrollAreaEventType type,
	    int			   x,
	    int			   y,
	    int			   button,
	    FooScrollAreaEventFunc func,
	    gpointer		data)
{
    FooScrollAreaEvent event;
    
    if (!func)
	return;

    if (type != FOO_MOTION)
	emit_input (scroll_area, FOO_MOTION, x, y, button, func, data);
    
#if 0
    x += scroll_area->priv->x_offset;
    y += scroll_area->priv->y_offset;
#endif
    
    event.type = type;
    event.x = x;
    event.y = y;
    event.button = button;
    
    func (scroll_area, &event, data);
}

#if 0
static void
print_path (const char *header,
	    cairo_path_t *path)
{
    int i;

    g_print ("%s\n", header);

    for (i=0; i < path->num_data; i += path->data[i].header.length)
    {
	cairo_path_data_t *data = &(path->data[i]);
	
	switch (data->header.type)
	{
	case CAIRO_PATH_MOVE_TO:
	    g_print ("move to:    %f, %f\n", data[1].point.x, data[1].point.y);
	    break;
	    
	case CAIRO_PATH_LINE_TO:
	    g_print ("line to:    %f, %f\n", data[1].point.x, data[1].point.y);
	    break;
	    
	case CAIRO_PATH_CURVE_TO:
	    g_print ("curve to:   %f, %f\n", data[1].point.x, data[1].point.y);
	    g_print ("            %f, %f\n", data[1].point.x, data[1].point.y);
	    g_print ("            %f, %f\n", data[1].point.x, data[1].point.y);
	    break;
	    
	case CAIRO_PATH_CLOSE_PATH:
	    break;
	}
    }
}
#endif

static void
process_event (FooScrollArea	       *scroll_area,
	       FooScrollAreaEventType	input_type,
	       int			x,
	       int			y,
	       int			button)
{
    GtkWidget *widget = GTK_WIDGET (scroll_area);
    int i;

    allocation_to_canvas (scroll_area, &x, &y);
    
    if (scroll_area->priv->grabbed)
    {
	emit_input (scroll_area, input_type, x, y, button,
		    scroll_area->priv->grab_func,
		    scroll_area->priv->grab_data);
	return;
    }

    
#if 0
    x += widget->allocation.x;
    y += widget->allocation.y;
#endif

#if 0
    g_print ("number of input regions: %d\n", scroll_area->priv->input_regions->len);
#endif
    
    for (i = 0; i < scroll_area->priv->input_regions->len; ++i)
    {
	InputRegion *region = scroll_area->priv->input_regions->pdata[i];

#if 0
	g_print ("%d ", i);
	print_region ("region:", region->region);
#endif
	
	if (gdk_region_point_in (region->region, x, y))
	{
	    InputPath *path;
	    int n_paths = 0;

	    path = region->paths;
	    while (path)
	    {
		cairo_t *cr;
		gboolean inside;

		cr = gdk_cairo_create (widget->window);
		cairo_set_fill_rule (cr, path->fill_rule);
		cairo_set_line_width (cr, path->line_width);
		cairo_append_path (cr, path->path);

		if (path->is_stroke)
		    inside = cairo_in_stroke (cr, x, y);
		else
		    inside = cairo_in_fill (cr, x, y);

		cairo_destroy (cr);
		
		if (inside)
		{
		    emit_input (scroll_area, input_type,
				x, y, button,
				path->func,
				path->data);
		    return;
		}
		
		path = path->next;
		n_paths++;
	    }

	    /* Since the regions are all disjoint, no other region
	     * can match. Of course we could be clever and try and
	     * sort the regions, but so far I have been unable to
	     * make this loop show up on a profile.
	     */
	    return;
	}
    }
}

static void
process_gdk_event (FooScrollArea *scroll_area,
		   int		  x,
		   int	          y,
		   GdkEvent      *event)
{
    FooScrollAreaEventType input_type = FOO_BUTTON_PRESS;
    int button = 0;
    
    if (event->type == GDK_BUTTON_PRESS)
    {
	input_type = FOO_BUTTON_PRESS;
	button = event->button.button;
    }
    else if (event->type == GDK_BUTTON_RELEASE)
    {
	input_type = FOO_BUTTON_RELEASE;
	button = event->button.button;
    }
    else if (event->type == GDK_MOTION_NOTIFY	||
	     event->type == GDK_LEAVE_NOTIFY	||
	     event->type == GDK_ENTER_NOTIFY)
    {
	input_type = FOO_MOTION;
	button = 0;
    }
    
    process_event (scroll_area, input_type, x, y, button);
}

static gboolean
foo_scroll_area_button_press (GtkWidget *widget,
			      GdkEventButton *event)
{
    FooScrollArea *area = FOO_SCROLL_AREA (widget);
    
    process_gdk_event (area, event->x, event->y, (GdkEvent *)event);
    
    return TRUE;
}

static gboolean
foo_scroll_area_button_release (GtkWidget *widget,
				GdkEventButton *event)
{
    FooScrollArea *area = FOO_SCROLL_AREA (widget);

    process_gdk_event (area, event->x, event->y, (GdkEvent *)event);
    
    return FALSE;
}

typedef struct
{
    Window	window;
    guint32	last_motion_time;
    gboolean	stop_compressing;
} EventScannerData;

static Bool
get_motion_time (Display  *dpy,
		 XEvent   *xevent,
		 XPointer  arg)
{
    EventScannerData *esd = (EventScannerData *)arg;
  
    if (esd->stop_compressing)
	return FALSE;

    if (esd->window != xevent->xany.window)
    {
	esd->stop_compressing = TRUE;
	return FALSE;
    }

    if (xevent->type == MotionNotify)
    {
	esd->last_motion_time = xevent->xmotion.time;
	return FALSE;
    }

    if (xevent->type == EnterNotify		||
	xevent->type == LeaveNotify)
    {
	esd->last_motion_time = xevent->xcrossing.time;
	return FALSE;
    }
    
    return FALSE;
}

static gboolean
use_this_motion_notify (GdkWindow *window,
			guint32    event_time,
			guint32	  *motion_notify_time)
{
    if (*motion_notify_time)
    {
	if (event_time == *motion_notify_time)
	{
	    *motion_notify_time = 0;

	    return TRUE;
	}
	else
	{
	    /* There are more motion events already queued up */
	    
	    return FALSE;
	}
    }
    else
    {
	Display *dpy = gdk_x11_get_default_xdisplay();
	EventScannerData esd = { GDK_WINDOW_XWINDOW (window), 0, FALSE };
	XEvent dummy;
    
	XCheckIfEvent (dpy, &dummy, get_motion_time, (XPointer)&esd);
	
	if (esd.last_motion_time == 0)
	{
	    return TRUE;
	}
	else
	{
	    *motion_notify_time = esd.last_motion_time;
	    return FALSE;
	}
    }
}

static gboolean
foo_scroll_area_motion (GtkWidget *widget,
			GdkEventMotion *event)
{
    FooScrollArea *area = FOO_SCROLL_AREA (widget);

    if (use_this_motion_notify (event->window,
				event->time,
				&(area->priv->motion_notify_time)))
    {
	process_gdk_event (area, event->x, event->y, (GdkEvent *)event);
    }
    
    return TRUE;
}

static gboolean
foo_scroll_area_crossing (GtkWidget *widget,
			  GdkEventCrossing *event)
{
    FooScrollArea *area = FOO_SCROLL_AREA (widget);

    if (use_this_motion_notify (event->window,
				event->time,
				&(area->priv->motion_notify_time)))
    {
	process_gdk_event (area, event->x, event->y, (GdkEvent *)event);
    }
    
    return TRUE;
}

void
foo_scroll_area_set_size_fixed_y (FooScrollArea	       *scroll_area,
				  int			width,
				  int			height,
				  int			old_y,
				  int			new_y)
{
    int dy = new_y - old_y;
    
    scroll_area->priv->width = width;
    scroll_area->priv->height = height;
    
    scroll_area->priv->vadj->value += dy;
    
    set_adjustment_values (scroll_area);
    
    if (dy != 0)
	gtk_adjustment_value_changed  (scroll_area->priv->vadj);
}

void
foo_scroll_area_set_size (FooScrollArea	       *scroll_area,
			  int			width,
			  int			height)
{
    g_return_if_fail (FOO_IS_SCROLL_AREA (scroll_area));
    
    /* FIXME: Default scroll algorithm should probably be to
     * keep the same *area* outside the screen as before.
     *
     * For wrapper widgets that will do something roughly
     * right. For widgets that don't change size, it
     * will do the right thing. Except for idle-layouting
     * widgets.
     *
     * Maybe there should be some generic support for those
     * widgets. Can that even be done?
     *
     * Should we have a version of this function using 
     * fixed points?
     */
    
    scroll_area->priv->width = width;
    scroll_area->priv->height = height;
    
    set_adjustment_values (scroll_area);
}

static void
foo_scroll_area_size_request (GtkWidget      *widget,
			      GtkRequisition *requisition)
{
    FooScrollArea *scroll_area = FOO_SCROLL_AREA (widget);
    
    requisition->width = scroll_area->priv->min_width;
    requisition->height = scroll_area->priv->min_height;
    
#if 0
    g_print ("request %d %d\n", requisition->width, requisition->height);
#endif
}

static void
foo_scrollbar_adjustment_changed (GtkAdjustment *adj,
				  FooScrollArea *scroll_area)
{
    GtkWidget *widget = GTK_WIDGET (scroll_area);
    gint dx = 0;
    gint dy = 0;
    GdkRectangle old_viewport, new_viewport;

    /* FIXME: When dragging diagonally, this function will be called
     * twice, causing two separate scrolls in the backing store.
     *
     * On the other hand with the constant time scrolling, it shouldn't
     * really matter.
     */
    get_viewport (scroll_area, &old_viewport);
    
    if (adj == scroll_area->priv->hadj)
    {
	/* FIXME: do we treat the offset as int or double, and,
	 * if int, how do we round?
	 */
	dx = (int)adj->value - scroll_area->priv->x_offset;
	scroll_area->priv->x_offset = adj->value;
    }
    else if (adj == scroll_area->priv->vadj)
    {
	dy = (int)adj->value - scroll_area->priv->y_offset;
	scroll_area->priv->y_offset = adj->value;
    }
    else
    {
	g_assert_not_reached ();
    }
    
    if (GTK_WIDGET_REALIZED (widget) && (dx || dy))
    {
	backing_store_scroll (scroll_area->priv->store, -dx, -dy);

	gtk_widget_queue_draw (GTK_WIDGET (scroll_area));
    }
    
    get_viewport (scroll_area, &new_viewport);
    
    emit_viewport_signals (scroll_area, &new_viewport, &old_viewport);
}

static void
set_one_adjustment (FooScrollArea *scroll_area,
		    GtkAdjustment *adjustment,
		    GtkAdjustment **location)
{
    g_return_if_fail (location != NULL);
    
    if (adjustment == *location)
	return;
    
    if (!adjustment)
	adjustment = new_adjustment ();
    
    g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
    
    if (*location)
    {
	g_signal_handlers_disconnect_by_func (
	    *location, foo_scrollbar_adjustment_changed, scroll_area);
	
	g_object_unref (*location);
    }
    
    *location = adjustment;
    
    g_object_ref_sink (*location);
    
    g_signal_connect (*location, "value_changed",
		      G_CALLBACK (foo_scrollbar_adjustment_changed),
		      scroll_area);
}

static void
foo_scroll_area_set_scroll_adjustments (FooScrollArea *scroll_area,
					GtkAdjustment *hadjustment,
					GtkAdjustment *vadjustment)
{
    set_one_adjustment (scroll_area, hadjustment, &scroll_area->priv->hadj);
    set_one_adjustment (scroll_area, vadjustment, &scroll_area->priv->vadj);
    
    set_adjustment_values (scroll_area);
}

FooScrollArea *
foo_scroll_area_new (void)
{
    return g_object_new (FOO_TYPE_SCROLL_AREA, NULL);
}

void
foo_scroll_area_set_min_size (FooScrollArea *scroll_area,
			      int		   min_width,
			      int            min_height)
{
    scroll_area->priv->min_width = min_width;
    scroll_area->priv->min_height = min_height;
    
    /* FIXME: think through invalidation.
     *
     * Goals: - no repainting everything on size_allocate(),
     *        - make sure input boxes are invalidated when
     *          needed
     */
    gtk_widget_queue_resize (GTK_WIDGET (scroll_area));
}

#if 0
static void
warn_about_adding_input_outside_expose (const char *func)
{
    static gboolean warned = FALSE;
    
    if (!warned)
    {
	g_warning ("%s() can only be called "
		   "from the paint handler for the FooScrollArea\n", func);
	
	warned = TRUE;
    }
}
#endif

static void
user_to_device (double *x, double *y,
		gpointer data)
{
    cairo_t *cr = data;
    
    cairo_user_to_device (cr, x, y);
}

static void
add_path (FooScrollArea *area,
	   cairo_t *cr,
	   gboolean is_stroke,
	   FooScrollAreaEventFunc func,
	   gpointer data)
{
    InputPath *path = g_new0 (InputPath, 1);
#if 0
    double x1, y1, x2, y2;
    GdkRectangle r;

    if (is_stroke)
	cairo_stroke_extents (cr, &x1, &y1, &x2, &y2);
    else
	cairo_fill_extents (cr, &x1, &y1, &x2, &y2);

    cairo_user_to_device (cr, &x1, &y1);
    cairo_user_to_device (cr, &x2, &y2);
    
    r.x = x1 - 1;
    r.y = y1 - 1;
    r.width = x2 - x1 + 1;
    r.height = y2 - y1 + 1;

    if (gdk_region_rect_in (area->priv->current_input->region, &r) != GDK_OVERLAP_RECTANGLE_OUT)
    {
#endif
	path->is_stroke = is_stroke;
	path->fill_rule = cairo_get_fill_rule (cr);
	path->line_width = cairo_get_line_width (cr);
	path->path = cairo_copy_path (cr);
	path_foreach_point (path->path, user_to_device, cr);
	path->func = func;
	path->data = data;
	path->next = area->priv->current_input->paths;
	area->priv->current_input->paths = path;
#if 0
    }
#endif
}

/* FIXME: we probably really want a
 *
 *	foo_scroll_area_add_input_from_fill (area, cr, ...);
 * and
 *      foo_scroll_area_add_input_from_stroke (area, cr, ...);
 * as well.
 */
void
foo_scroll_area_add_input_from_fill (FooScrollArea           *scroll_area,
				     cairo_t	             *cr,
				     FooScrollAreaEventFunc   func,
				     gpointer                 data)
{
    g_return_if_fail (FOO_IS_SCROLL_AREA (scroll_area));
    g_return_if_fail (cr != NULL);
    g_return_if_fail (scroll_area->priv->current_input);

    add_path (scroll_area, cr, FALSE, func, data);
}

void
foo_scroll_area_add_input_from_stroke (FooScrollArea           *scroll_area,
				       cairo_t	                *cr,
				       FooScrollAreaEventFunc   func,
				       gpointer                 data)
{
    g_return_if_fail (FOO_IS_SCROLL_AREA (scroll_area));
    g_return_if_fail (cr != NULL);
    g_return_if_fail (scroll_area->priv->current_input);

    add_path (scroll_area, cr, TRUE, func, data);
}

void
foo_scroll_area_invalidate_region (FooScrollArea *area,
				   GdkRegion     *region)
{
    g_return_if_fail (FOO_IS_SCROLL_AREA (area));

    if (GTK_WIDGET_REALIZED (area))
    {
	canvas_to_store (area, region);
	backing_store_invalidate_region (area->priv->store, region);
	store_to_canvas (area, region);

	canvas_to_window (area, region);
	gdk_window_invalidate_region (GTK_WIDGET (area)->window, region, TRUE);
	window_to_canvas (area, region);
    }
}

void
foo_scroll_area_invalidate_rect (FooScrollArea *scroll_area,
				 int	        x,
				 int	        y,
				 int	        width,
				 int	        height)
{
    GdkRectangle rect = { x, y, width, height };
    GdkRegion *region;
    
    g_return_if_fail (FOO_IS_SCROLL_AREA (scroll_area));

    region = gdk_region_rectangle (&rect);

    foo_scroll_area_invalidate_region (scroll_area, region);

    gdk_region_destroy (region);
}

void
foo_scroll_area_invalidate (FooScrollArea *scroll_area)
{
    GtkWidget *widget = GTK_WIDGET (scroll_area);

    foo_scroll_area_invalidate_rect (scroll_area,
				     scroll_area->priv->x_offset, scroll_area->priv->y_offset,
				     widget->allocation.width,
				     widget->allocation.height);
}

void
foo_scroll_area_begin_grab (FooScrollArea *scroll_area,
			    FooScrollAreaEventFunc func,
			    gpointer       input_data)
{
    g_return_if_fail (FOO_IS_SCROLL_AREA (scroll_area));
    g_return_if_fail (!scroll_area->priv->grabbed);
    
    scroll_area->priv->grabbed = TRUE;
    scroll_area->priv->grab_func = func;
    scroll_area->priv->grab_data = input_data;
    
    /* FIXME: we should probably take a server grab */
    /* Also, maybe there should be support for setting the grab cursor */
}

void
foo_scroll_area_end_grab (FooScrollArea *scroll_area)
{
    g_return_if_fail (FOO_IS_SCROLL_AREA (scroll_area));
    
    scroll_area->priv->grabbed = FALSE;
    scroll_area->priv->grab_func = NULL;
    scroll_area->priv->grab_data = NULL;
}

gboolean
foo_scroll_area_is_grabbed (FooScrollArea *scroll_area)
{
    return scroll_area->priv->grabbed;
}

void
foo_scroll_area_set_viewport_pos (FooScrollArea  *scroll_area,
				  int		  x,
				  int		  y)
{
    int x_changed;
    int y_changed;
    GtkAdjustment *hadj, *vadj;

    hadj = scroll_area->priv->hadj;
    vadj = scroll_area->priv->vadj;

    x_changed = (int)hadj->value != x;
    y_changed = (int)vadj->value != y;

    hadj->value = x;
    vadj->value = y;

    set_adjustment_values (scroll_area);

    if (x_changed && hadj->upper >= hadj->page_size)
    {
#if 0
	g_print ("changed x\n");
#endif
	gtk_adjustment_value_changed (hadj);
    }
    
    if (y_changed && vadj->upper >= vadj->page_size)
    {
#if 0
	g_print ("changed y\n");
#endif
	gtk_adjustment_value_changed (vadj);
    }
}

static gboolean
rect_contains (const GdkRectangle *rect, int x, int y)
{
    return (x >= rect->x		&&
	    y >= rect->y		&&
	    x  < rect->x + rect->width	&&
	    y  < rect->y + rect->height);
}

static void
stop_scrolling (FooScrollArea *area)
{
#if 0
    g_print ("stop scrolling\n");
#endif
    if (area->priv->auto_scroll_info)
    {
	g_source_remove (area->priv->auto_scroll_info->timeout_id);
	g_timer_destroy (area->priv->auto_scroll_info->timer);
	g_free (area->priv->auto_scroll_info);
	
	area->priv->auto_scroll_info = NULL;
    }
}

static gboolean
scroll_idle (gpointer data)
{
    GdkRectangle viewport, new_viewport;
    FooScrollArea *area = data;
    AutoScrollInfo *info = area->priv->auto_scroll_info;
#if 0
    int dx, dy;
#endif
    int new_x, new_y;
    double elapsed;
    
    get_viewport (area, &viewport);
    
#if 0
    g_print ("old info: %d %d\n", info->dx, info->dy);
    
    g_print ("timeout (%d %d)\n", dx, dy);
#endif
    
#if 0
    viewport.x += info->dx;
    viewport.y += info->dy;
#endif
    
#if 0
    g_print ("new info %d %d\n", info->dx, info->dy);
#endif

    elapsed = g_timer_elapsed (info->timer, NULL);

    info->res_x = elapsed * info->dx / 0.2;
    info->res_y = elapsed * info->dy / 0.2;

#if 0
    g_print ("%f %f\n", info->res_x, info->res_y);
#endif
    
    new_x = viewport.x + info->res_x;
    new_y = viewport.y + info->res_y;

#if 0
    g_print ("%f\n", elapsed * (info->dx / 0.2));
#endif
    
#if 0
    g_print ("new_x, new_y\n: %d %d\n", new_x, new_y);
#endif
    
    foo_scroll_area_set_viewport_pos (area, new_x, new_y);
#if 0
				      viewport.x + info->dx,
				      viewport.y + info->dy);
#endif

    get_viewport (area, &new_viewport);

    if (viewport.x == new_viewport.x		&&
	viewport.y == new_viewport.y		&&
	(info->res_x > 1.0			||
	 info->res_y > 1.0			||
	 info->res_x < -1.0			||
	 info->res_y < -1.0))
    {
	stop_scrolling (area);
	
	/* stop scrolling if it didn't have an effect */
	return FALSE;
    }
    
    return TRUE;
}

static void
ensure_scrolling (FooScrollArea *area,
		  int		 dx,
		  int		 dy)
{
    if (!area->priv->auto_scroll_info)
    {
#if 0
	g_print ("start scrolling\n");
#endif
	area->priv->auto_scroll_info = g_new0 (AutoScrollInfo, 1);
	area->priv->auto_scroll_info->timeout_id =
	    g_idle_add (scroll_idle, area);
	area->priv->auto_scroll_info->timer = g_timer_new ();
    }
    
#if 0
    g_print ("setting scrolling to %d %d\n", dx, dy);
#endif

#if 0
    g_print ("dx, dy: %d %d\n", dx, dy);
#endif
    
    area->priv->auto_scroll_info->dx = dx;
    area->priv->auto_scroll_info->dy = dy;
}

void
foo_scroll_area_auto_scroll (FooScrollArea *scroll_area,
			     FooScrollAreaEvent *event)
{
#define SCROLL_MARGIN 4
    
    GdkRectangle viewport;

    get_viewport (scroll_area, &viewport);

    viewport.x += SCROLL_MARGIN;
    viewport.y += SCROLL_MARGIN;
    viewport.width -= 2 * SCROLL_MARGIN;
    viewport.height -= 2 * SCROLL_MARGIN;

    if (rect_contains (&viewport, event->x, event->y))
    {
#if 0
	g_print ("in viewport\n");
#endif
	
	stop_scrolling (scroll_area);
    }
    else
    {
	int dx, dy;
	
	dx = dy = 0;
	
	if (event->y < viewport.y)
	{
	    dy = event->y - viewport.y;
	    dy = MIN (dy + 2, 0);
	}
	else if (event->y >= viewport.y + viewport.height)
	{
#if 0
	    g_print ("event->y: %d, y + h: %d\n",
		     event->y, viewport.y + viewport.height);
#endif
	    
	    dy = event->y - (viewport.y + viewport.height - 1);
	    dy = MAX (dy - 2, 0);
	}
	
	if (event->x < viewport.x)
	{
	    dx = event->x - viewport.x;
	    dx = MIN (dx + 2, 0);
	}
	else if (event->x >= viewport.x + viewport.width)
	{
	    dx = event->x - (viewport.x + viewport.width - 1);
	    dx = MAX (dx - 2, 0);
	}

#if 0
	g_print ("dx, dy: %d %d\n", dx, dy);
#endif
	
	ensure_scrolling (scroll_area, dx, dy);
    }
}

void
foo_scroll_area_begin_auto_scroll (FooScrollArea *scroll_area)
{
    /* noop  for now */
}

void
foo_scroll_area_end_auto_scroll (FooScrollArea *scroll_area)
{
    stop_scrolling (scroll_area);
}