aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/gfx/gfx_widgets.cpp
blob: 2207309f48d413347e0dde8ddc5dbe6d61afd21b (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
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 *
 */

#include "sci/sci.h"
#include "sci/gfx/gfx_gui.h"		// for kWindowAutoRestore
#include "sci/gfx/gfx_widgets.h"
#include "sci/gfx/gfx_state_internal.h"

namespace Sci {

#undef GFXW_DEBUG_DIRTY // Enable to debug dirty rectangle propagation (writes to stderr)

#ifdef GFXW_DEBUG_DIRTY
#  define DDIRTY fprintf(stderr, "%s:%5d| ", __FILE__, __LINE__); fprintf
#else
#  define DDIRTY if (0) fprintf
#endif

Common::Point gfxw_point_zero(0, 0);

#define MAX_SERIAL_NUMBER 0x7fffffff
static int widget_serial_number_counter = 0x10000; // Avoid confusion with IDs

#ifdef GFXW_DEBUG_WIDGETS

GfxWidget *debug_widgets[GFXW_DEBUG_WIDGETS];
int debug_widget_pos = 0;

static void _gfxw_debug_add_widget(GfxWidget *widget) {
	if (debug_widget_pos == GFXW_DEBUG_WIDGETS)
		error("WIDGET DEBUG: Allocated the maximum number of %d widgets- Aborting!\n", GFXW_DEBUG_WIDGETS);
	debug_widgets[debug_widget_pos++] = widget;
}

static void _gfxw_debug_remove_widget(GfxWidget *widget) {
	int i;
	int found = 0;
	for (i = 0; i < debug_widget_pos; i++) {
		if (debug_widgets[i] == widget) {
			memmove(debug_widgets + i, debug_widgets + i + 1, (sizeof(GfxWidget *)) * (debug_widget_pos - i - 1));
			debug_widgets[debug_widget_pos--] = NULL;
			found++;
		}
	}

	if (found > 1) {
		error("While removing widget: Found it %d times!\n", found);
	}

	if (found == 0) {
		error("Attempted removal of unregistered widget!\n");
	}
}
#else // !GFXW_DEBUG_WIDGETS
#define _gfxw_debug_add_widget(a)
#define _gfxw_debug_remove_widget(a)
#endif


static void indent(int indentation) {
	for (int i = 0; i < indentation; i++)
		sciprintf("    ");
}

void GfxWidget::printIntern(int indentation) const {
	unsigned int i;
	char flags_list[] = "VOCDTMI";

	indent(indentation);

	if (_magic == GFXW_MAGIC_VALID) {
		if (_visual)
			sciprintf("v ");
		else
			sciprintf("NoVis ");
	} else if (_magic == GFXW_MAGIC_INVALID)
		sciprintf("INVALID ");

	sciprintf("S%08x", _serial);

	if (_ID != GFXW_NO_ID) {
		sciprintf("#%x", _ID);

		if (_subID != GFXW_NO_ID)
			sciprintf(":%x ", _subID);
		else
			sciprintf(" ");
	}

	sciprintf("[(%d,%d)(%dx%d)]", _bounds.x, _bounds.y, _bounds.width, _bounds.height);

	for (i = 0; i < strlen(flags_list); i++)
		if (_flags & (1 << i))
			sciprintf("%c", flags_list[i]);

	sciprintf(" ");
}

void GfxWidget::print(int indentation) const {
	printIntern(indentation);
	sciprintf("<untyped #%d>", _type);
}

GfxWidget::GfxWidget(gfxw_widget_type_t type_) {
	_magic = GFXW_MAGIC_VALID;

	_serial = widget_serial_number_counter++;
	widget_serial_number_counter &= MAX_SERIAL_NUMBER;

	_flags = GFXW_FLAG_DIRTY;
	_type = type_;
	_bounds = gfx_rect(0, 0, 0, 0);
	_next = NULL;
	_ID = GFXW_NO_ID;
	_subID = GFXW_NO_ID;
	_parent = NULL;
	_visual = NULL;
	_widgetPriority = -1;

	compare_to = NULL;
	equals = NULL;
	should_replace = NULL;
	superarea_of = NULL;

	_gfxw_debug_add_widget(this);
}

static int verify_widget(GfxWidget *widget) {
	if (!widget) {
		GFXERROR("Attempt to use NULL widget\n");
		return 1;
	} else if (widget->_magic != GFXW_MAGIC_VALID) {
		if (widget->_magic == GFXW_MAGIC_INVALID) {
			GFXERROR("Attempt to use invalidated widget\n");
		} else {
			GFXERROR("Attempt to use non-widget\n");
		}
		return 1;
	}
	return 0;
}

#define VERIFY_WIDGET(w) \
	if (verify_widget((GfxWidget *)(w))) { GFXERROR("Error occured while validating widget\n"); }

#define GFX_ASSERT(_x) \
{ \
	int retval = (_x); \
	if (retval == GFX_ERROR) { \
		GFXERROR("Error occured while drawing widget!\n"); \
		return 1; \
	} else if (retval == GFX_FATAL) { \
		error("Fatal error occured while drawing widget!\nGraphics state invalid; aborting program..."); \
	} \
}

//********** Widgets *************

// Base class operations and common stuff

// Assertion for drawing
#define DRAW_ASSERT(widget, exp_type) \
	if (!(widget)) { \
		sciprintf("L%d: NULL widget", __LINE__); \
		return 1; \
	} \
	if ((widget)->_type != (exp_type)) { \
		sciprintf("L%d: Error in widget: Expected type " # exp_type "(%d) but got %d\n", __LINE__, exp_type, (widget)->_type); \
		sciprintf("Erroneous widget: "); \
		widget->print(4); \
		sciprintf("\n"); \
		return 1; \
	} \
	if (!(widget->_flags & GFXW_FLAG_VISIBLE)) \
		return 0; \
	if (!(widget->_type == GFXW_VISUAL || widget->_visual)) { \
		sciprintf("L%d: Error while drawing widget: Widget has no visual\n", __LINE__); \
		sciprintf("Erroneous widget: "); \
		widget->print(1); \
		sciprintf("\n"); \
		return 1; \
	}


// TODO: Turn this into an operator==
static int _color_equals(gfx_color_t a, gfx_color_t b) {
	if (a.mask != b.mask)
		return 0;

	if (a.mask & GFX_MASK_VISUAL) {
		if (a.visual.r != b.visual.r || a.visual.g != b.visual.g || a.visual.b != b.visual.b || a.alpha != b.alpha)
			return 0;
	}

	if (a.mask & GFX_MASK_PRIORITY)
		if (a.priority != b.priority)
			return 0;

	if (a.mask & GFX_MASK_CONTROL)
		if (a.control != b.control)
			return 0;

	return 1;
}

int GfxWidget::setVisual(GfxVisual *visual) {
	_visual = visual;

	if (_parent) {
		DDIRTY(stderr, "GfxWidget::setVisual: DOWNWARDS rel(%d,%d,%d,%d, 1)\n", GFX_PRINT_RECT(_bounds));
		_parent->add_dirty_rel(_parent, _bounds, 1);
	}

	return 0;
}

static int _gfxwop_basic_should_replace(GfxWidget *widget, GfxWidget *other) {
	return 0;
}

static void _gfxw_set_ops(GfxWidget *widget,
	gfxw_bin_op *compare_to, gfxw_bin_op *equals, gfxw_bin_op *superarea_of) {
	widget->compare_to = compare_to;
	widget->equals = equals;
	widget->superarea_of = superarea_of;

	widget->should_replace = _gfxwop_basic_should_replace;
}

void gfxw_remove_widget_from_container(GfxContainer *container, GfxWidget *widget) {
	GfxWidget **seekerp;

	if (!container) {
		GFXERROR("Attempt to remove widget from NULL container!\n");
		error("gfxw_remove_widget_from_container() failed. Breakpoint in %s, line %d", __FILE__, __LINE__);
	}

	seekerp = &(container->_contents);

	if (GFXW_IS_LIST(widget) && GFXW_IS_PORT(container)) {
		GfxPort *port = (GfxPort *) container;
		if (port->_decorations == (GfxList *) widget) {
			port->_decorations = NULL;
			return;
		}
	}

	while (*seekerp && *seekerp != widget)
		seekerp = &((*seekerp)->_next);

	if (!*seekerp) {
		GFXERROR("Internal error: Attempt to remove widget from container it was not contained in!\n");
		sciprintf("Widget:");
		widget->print(1);
		sciprintf("Container:");
		widget->print(1);
		error("gfxw_remove_widget_from_container() failed. Breakpoint in %s, line %d", __FILE__, __LINE__);
		return;
	}

	if (container->_nextpp == &(widget->_next))
		container->_nextpp = seekerp;

	*seekerp = widget->_next; // Remove it
	widget->_parent = NULL;
	widget->_next = NULL;
}

GfxWidget::~GfxWidget() {
	DDIRTY(stderr, "BASIC-FREE: SomeAddDirty\n");	// FIXME: What is this?

	if (_parent) {
		if (GFXW_IS_CONTAINER(this))
			_parent->add_dirty_abs(_parent, _bounds, 1);
		else
			_parent->add_dirty_rel(_parent, _bounds, 1);

		gfxw_remove_widget_from_container(_parent, this);
	}

	_magic = GFXW_MAGIC_INVALID;
	_gfxw_debug_remove_widget(this);
}

static int _gfxwop_basic_compare_to(GfxWidget *widget, GfxWidget *other) {
	return 1;
}

static int _gfxwop_basic_equals(GfxWidget *widget, GfxWidget *other) {
	return 0;
}

static int _gfxwop_basic_superarea_of(GfxWidget *widget, GfxWidget *other) {
	return (widget == other);
}

//*** Boxes ***

static rect_t _move_rect(rect_t rect, Common::Point point) {
	return gfx_rect(rect.x + point.x, rect.y + point.y, rect.width, rect.height);
}

static void _split_rect(rect_t rect, Common::Point *p1, Common::Point *p2) {
	p1->x = rect.x;
	p1->y = rect.y;
	p2->x = rect.x + rect.width;
	p2->y = rect.y + rect.height;
}

static Common::Point _move_point(rect_t rect, Common::Point point) {
	return Common::Point(rect.x + point.x, rect.y + point.y);
}

int GfxBox::draw(const Common::Point &pos) {
	DRAW_ASSERT(this, GFXW_BOX);
	GFX_ASSERT(gfxop_draw_box(_visual->_gfxState, _move_rect(_bounds, pos), _color1, _color2, _shadeType));
	return 0;
}

void GfxBox::print(int indentation) const {
	printIntern(indentation);
	sciprintf("BOX");
}

static int _gfxwop_box_superarea_of(GfxWidget *widget, GfxWidget *other) {
	GfxBox *box = (GfxBox *) widget;

	if (box->_color1.alpha)
		return 0;

	if (box->_shadeType != GFX_BOX_SHADE_FLAT && box->_color2.alpha)
		return 0;

	// Note: the check for box->_bounds and other->_bounds is NOT the same as contains()
	// in Common::Rect (this one includes equality too)
	if (!(box->_bounds.x <= other->_bounds.x && box->_bounds.y <= other->_bounds.y &&
		  box->_bounds.x + box->_bounds.width >= other->_bounds.x + other->_bounds.width &&
		  box->_bounds.y + box->_bounds.height >= other->_bounds.y + other->_bounds.height))
		return 0;

	return 1;
}

static int _gfxwop_box_equals(GfxWidget *widget, GfxWidget *other) {
	GfxBox *wbox = (GfxBox *)widget, *obox;
	if (other->_type != GFXW_BOX)
		return 0;

	obox = (GfxBox *) other;

	if (!toCommonRect(wbox->_bounds).equals(toCommonRect(obox->_bounds)))
		return 0;

	if (!_color_equals(wbox->_color1, obox->_color1))
		return 0;

	if (wbox->_shadeType != obox->_shadeType)
		return 0;

	if (wbox->_shadeType != GFX_BOX_SHADE_FLAT
	        && _color_equals(wbox->_color2, obox->_color2))
		return 0;

	return 1;
}

void _gfxw_set_ops_BOX(GfxWidget *widget) {
	_gfxw_set_ops(widget, _gfxwop_basic_compare_to, _gfxwop_box_equals, _gfxwop_box_superarea_of);
}

static int _gfxw_color_get_priority(gfx_color_t color) {
	return (color.mask & GFX_MASK_PRIORITY) ? color.priority : -1;
}

GfxBox *gfxw_new_box(GfxState *state, rect_t area, gfx_color_t color1, gfx_color_t color2, gfx_box_shade_t shade_type) {
	return new GfxBox(state, area, color1, color2, shade_type);
}

GfxBox::GfxBox(GfxState *state, rect_t area, gfx_color_t color1, gfx_color_t color2, gfx_box_shade_t shade_type)
	: GfxWidget(GFXW_BOX) {

	_widgetPriority = _gfxw_color_get_priority(color1);
	_bounds = area;
	_color1 = color1;
	_color2 = color2;
	_shadeType = shade_type;

	_flags |= GFXW_FLAG_VISIBLE;

	if ((_color1.mask & GFX_MASK_VISUAL) && ((state && (state->driver->getMode()->palette)) || (!_color1.alpha && !_color2.alpha)))
		_flags |= GFXW_FLAG_OPAQUE;

	_gfxw_set_ops_BOX(this);
}

GfxPrimitive::GfxPrimitive(rect_t area, gfx_color_t color_, gfx_line_mode_t mode,
						gfx_line_style_t style, gfxw_widget_type_t type_)
	: GfxWidget(type_) {

	_widgetPriority = _gfxw_color_get_priority(color_);
	_bounds = area;
	_color = color_;
	_lineMode = mode;
	_lineStyle = style;

	_flags |= GFXW_FLAG_VISIBLE;
}

//*** Rectangles ***

struct GfxRect : public GfxPrimitive {
	GfxRect(rect_t rect, gfx_color_t color, gfx_line_mode_t line_mode, gfx_line_style_t line_style);

	virtual int draw(const Common::Point &pos);
	virtual void print(int indentation) const;
};

static int _gfxwop_primitive_equals(GfxWidget *widget, GfxWidget *other) {
	GfxPrimitive *wprim = (GfxPrimitive *) widget, *oprim;
	if (widget->_type != other->_type)
		return 0;

	oprim = (GfxPrimitive *) other;

	// Check if the two primitives are equal (note: the primitives aren't always rectangles, so
	// the "width" and the "height" here could be negative)
	if (wprim->_bounds.x != oprim->_bounds.x || wprim->_bounds.y != oprim->_bounds.y ||
		wprim->_bounds.width != oprim->_bounds.width || wprim->_bounds.height != oprim->_bounds.height)
		return 0;

	if (!_color_equals(wprim->_color, oprim->_color))
		return 0;

	if (wprim->_lineMode != oprim->_lineMode)
		return 0;

	if (wprim->_lineStyle != oprim->_lineStyle)
		return 0;

	return 1;
}

int GfxRect::draw(const Common::Point &pos) {
	DRAW_ASSERT(this, GFXW_RECT);

	GFX_ASSERT(gfxop_draw_rectangle(_visual->_gfxState, gfx_rect(_bounds.x + pos.x, _bounds.y + pos.y,
	                                         _bounds.width - 1, _bounds.height - 1), _color, _lineMode, _lineStyle));
	return 0;
}

void GfxRect::print(int indentation) const {
	printIntern(indentation);
	sciprintf("RECT");
}

void _gfxw_set_ops_RECT(GfxWidget *prim) {
	_gfxw_set_ops(prim,
	              _gfxwop_basic_compare_to, _gfxwop_primitive_equals, _gfxwop_basic_superarea_of);
}

GfxPrimitive *gfxw_new_rect(rect_t rect, gfx_color_t color, gfx_line_mode_t line_mode, gfx_line_style_t line_style) {
	return new GfxRect(rect, color, line_mode, line_style);
}

GfxRect::GfxRect(rect_t rect, gfx_color_t color_, gfx_line_mode_t line_mode_, gfx_line_style_t line_style_)
	: GfxPrimitive(rect, color_, line_mode_, line_style_, GFXW_RECT) {

	_bounds.width++;
	_bounds.height++; // Since it is actually one pixel bigger in each direction

	_gfxw_set_ops_RECT(this);
}

//*** Lines ***

struct GfxLine : public GfxPrimitive {
	GfxLine(Common::Point start, Common::Point end, gfx_color_t color, gfx_line_mode_t line_mode, gfx_line_style_t line_style);

	virtual int draw(const Common::Point &pos);
	virtual void print(int indentation) const;
};

int GfxLine::draw(const Common::Point &pos) {
	rect_t linepos = _bounds;
	Common::Point p1, p2;

	linepos.width--;
	linepos.height--;

	DRAW_ASSERT(this, GFXW_LINE);

	_split_rect(_move_rect(linepos, pos), &p1, &p2);
	GFX_ASSERT(gfxop_draw_line(_visual->_gfxState, p1, p2, _color, _lineMode, _lineStyle));
	return 0;
}

void GfxLine::print(int indentation) const {
	printIntern(indentation);
//	sciprintf("LINE");
}

void _gfxw_set_ops_LINE(GfxWidget *prim) {
	_gfxw_set_ops(prim,
	              _gfxwop_basic_compare_to, _gfxwop_primitive_equals, _gfxwop_basic_superarea_of);
}

GfxPrimitive *gfxw_new_line(Common::Point start, Common::Point end, gfx_color_t color, gfx_line_mode_t line_mode, gfx_line_style_t line_style) {
	return new GfxLine(start, end, color, line_mode, line_style);
}

GfxLine::GfxLine(Common::Point start, Common::Point end, gfx_color_t color_, gfx_line_mode_t line_mode_, gfx_line_style_t line_style_)
	: GfxPrimitive(gfx_rect(start.x, start.y, end.x - start.x + 1, end.y - start.y + 1), color_, line_mode_, line_style_, GFXW_LINE) {
	_gfxw_set_ops_LINE(this);
}

//*** Views and static views ***


GfxView::GfxView(GfxState *state, Common::Point pos_, int view_, int loop_, int cel_, int palette_, int priority, int control,
	gfx_alignment_t halign, gfx_alignment_t valign, int flags_)
	: GfxWidget((flags_ & GFXW_VIEW_FLAG_STATIC) ? GFXW_STATIC_VIEW : GFXW_VIEW) {

	int width, height;
	Common::Point offset;

	if (!state) {
		error("Attempt to create view widget with NULL state");
	}

	if (gfxop_get_cel_parameters(state, view_, loop_, cel_, &width, &height, &offset)) {
		error("Attempt to retrieve cel parameters for (%d/%d/%d) failed (Maybe the values weren't checked beforehand?)",
		         view_, cel_, loop_);
	}

	_pos = pos_;
	_color.mask = ((priority < 0) ? 0 : GFX_MASK_PRIORITY) | ((control < 0) ? 0 : GFX_MASK_CONTROL);
	_widgetPriority = priority;
	_color.priority = priority;
	_color.control = control;
	_view = view_;
	_loop = loop_;
	_cel = cel_;
	_palette = palette_;

	if (halign == ALIGN_CENTER)
		_pos.x -= width >> 1;
	else if (halign == ALIGN_RIGHT)
		_pos.x -= width;

	if (valign == ALIGN_CENTER)
		_pos.y -= height >> 1;
	else if (valign == ALIGN_BOTTOM)
		_pos.y -= height;

	_bounds = gfx_rect(_pos.x - offset.x, _pos.y - offset.y, width, height);

	_flags |= GFXW_FLAG_VISIBLE;
}

int GfxView::draw(const Common::Point &pos) {
	if (_type == GFXW_VIEW) {
		DRAW_ASSERT(this, GFXW_VIEW);
		GFX_ASSERT(gfxop_draw_cel(_visual->_gfxState, _view, _loop, _cel,
					Common::Point(_pos.x + pos.x, _pos.y + pos.y), _color, _palette));
	} else {
		// FIXME: _gfxwop_static_view_draw checked for GFXW_VIEW here, instead of GFXW_STATIC_VIEW.
		//DRAW_ASSERT(this, GFXW_VIEW);
		DRAW_ASSERT(this, GFXW_STATIC_VIEW);
		GFX_ASSERT(gfxop_draw_cel_static(_visual->_gfxState, _view, _loop, _cel,
	                _move_point(_bounds, pos), _color, _palette));
	}
	return 0;
}

void GfxView::print(int indentation) const {
	printIntern(indentation);

	if (_type == GFXW_STATIC_VIEW)
		sciprintf("STATICVIEW");
	else if (_type == GFXW_VIEW)
		sciprintf("VIEW");
	else
		error("GfxView::print: Invalid type %d", _type);

	sciprintf("(%d/%d/%d)@(%d,%d)[p:%d,c:%d]", _view, _loop, _cel, _pos.x, _pos.y,
	          (_color.mask & GFX_MASK_PRIORITY) ? _color.priority : -1,
	          (_color.mask & GFX_MASK_CONTROL) ? _color.control : -1);
}

void _gfxw_set_ops_VIEW(GfxWidget *view, char stat) {
	_gfxw_set_ops(view, _gfxwop_basic_compare_to, _gfxwop_basic_equals, _gfxwop_basic_superarea_of);
}

GfxView *gfxw_new_view(GfxState *state, Common::Point pos, int view_nr, int loop, int cel, int palette, int priority, int control,
	gfx_alignment_t halign, gfx_alignment_t valign, int flags) {
	GfxView *view;

	if (flags & GFXW_VIEW_FLAG_DONT_MODIFY_OFFSET) {
		int foo;
		Common::Point offset;
		gfxop_get_cel_parameters(state, view_nr, loop, cel, &foo, &foo, &offset);
		pos.x += offset.x;
		pos.y += offset.y;
	}

	view = new GfxView(state, pos, view_nr, loop, cel, palette, priority, control, halign, valign,
	                             (flags & GFXW_VIEW_FLAG_STATIC) ? GFXW_STATIC_VIEW : GFXW_VIEW);

	_gfxw_set_ops_VIEW(view, (char)(flags & GFXW_VIEW_FLAG_STATIC));

	return view;
}

//*** Dynamic Views ***

int GfxDynView::draw(const Common::Point &pos) {
	if (_type == GFXW_DYN_VIEW) {
		DRAW_ASSERT(this, GFXW_DYN_VIEW);

		GFX_ASSERT(gfxop_draw_cel(_visual->_gfxState, _view, _loop,  _cel,
		                         _move_point(draw_bounds, pos), _color, _palette));

		/*
		  gfx_color_t red;
		  red.visual.r = 0xff;
		  red.visual.g = red.visual.b = 0;
		  red.mask = GFX_MASK_VISUAL;
		  GFX_ASSERT(gfxop_draw_rectangle(view->visual->_gfxState,
		  gfx_rect(view->_bounds.x + pos.x, view->_bounds.y + pos.y, view->_bounds.width - 1, view->_bounds.height - 1), red, 0, 0));
		*/
	} else {
		DRAW_ASSERT(this, GFXW_PIC_VIEW);

		if (_isDrawn)
			return 0;

		GFX_ASSERT(gfxop_set_clip_zone(_visual->_gfxState, _parent->zone));
		GFX_ASSERT(gfxop_draw_cel_static_clipped(_visual->_gfxState, _view, _loop,
				   _cel, _move_point(draw_bounds, pos), _color, _palette));

		// Draw again on the back buffer
		GFX_ASSERT(gfxop_draw_cel(_visual->_gfxState, _view, _loop, _cel,
								  _move_point(draw_bounds, pos), _color, _palette));


		_isDrawn = true; // No more drawing needs to be done
	}

	return 0;
}

 void GfxDynView::print(int indentation) const {
	printIntern(indentation);

	if (_type == GFXW_DYN_VIEW)
		sciprintf("DYNVIEW");
	else if (_type == GFXW_PIC_VIEW)
		sciprintf("PICVIEW");
	else
		error("GfxDynView::print: Invalid type %d", _type);

	sciprintf(" SORT=%d z=%d seq=%d (%d/%d/%d)@(%d,%d)[p:%d,c:%d]; sig[%04x@%04X:%04X[%d]]", force_precedence, _z,
	          sequence, _view, _loop, _cel, _pos.x, _pos.y,
	          (_color.mask & GFX_MASK_PRIORITY) ? _color.priority : -1,
	          (_color.mask & GFX_MASK_CONTROL) ? _color.control : -1, signal, signalp.obj.segment, signalp.obj.offset, signalp.varindex);
}

static int _gfxwop_dyn_view_equals(GfxWidget *widget, GfxWidget *other) {
	GfxDynView *wview = (GfxDynView *)widget, *oview;
	if (!GFXW_IS_DYN_VIEW(other))
		return 0;

	oview = (GfxDynView *)other;

	if (wview->_pos.x != oview->_pos.x || wview->_pos.y != oview->_pos.y || wview->_z != oview->_z)
		return 0;

	if (wview->_view != oview->_view || wview->_loop != oview->_loop || wview->_cel != oview->_cel)
		return 0;

	if (!_color_equals(wview->_color, oview->_color))
		return 0;

	if (wview->_flags != oview->_flags)
		return 0;

	return 1;
}

static int _gfxwop_dyn_view_compare_to(GfxWidget *widget, GfxWidget *other) {
	int retval;
	GfxDynView *wview = (GfxDynView *) widget, *oview;
	if (!GFXW_IS_DYN_VIEW(other))
		return 1;

	oview = (GfxDynView *) other;

	retval = wview->force_precedence - oview->force_precedence;
	if (retval)
		return retval;

	retval = wview->_pos.y - oview->_pos.y;
	if (retval)
		return retval;

	retval = (wview->_z - oview->_z);
	if (retval)
		return retval;

	return -(wview->sequence - oview->sequence);
}

void _gfxw_set_ops_DYNVIEW(GfxWidget *widget) {
	_gfxw_set_ops(widget, _gfxwop_dyn_view_compare_to, _gfxwop_dyn_view_equals, _gfxwop_basic_superarea_of);
}

void _gfxw_set_ops_PICVIEW(GfxWidget *widget) {
	_gfxw_set_ops_DYNVIEW(widget);
}

GfxDynView *gfxw_new_dyn_view(GfxState *state, Common::Point pos, int z, int view, int loop, int cel, int palette, int priority, int control,
	gfx_alignment_t halign, gfx_alignment_t valign, int sequence) {

	return new GfxDynView(state, pos, z, view, loop, cel, palette, priority, control, halign, valign, sequence);
}

GfxDynView::GfxDynView(GfxState *state, Common::Point pos_, int z_, int view_, int loop_, int cel_, int palette_, int priority, int control,
	gfx_alignment_t halign, gfx_alignment_t valign, int sequence_)
	: GfxView(state, pos_, view_, loop_, cel_, palette_, priority, control, halign, valign, 0) {
	int width, height;
	int xalignmod, yalignmod;
	Common::Point offset;

	_type = GFXW_DYN_VIEW;

	if (!state) {
		error("Attempt to create view widget with NULL state");
	}

	if (gfxop_get_cel_parameters(state, view_, loop_, cel_, &width, &height, &offset)) {
		error("Attempt to retrieve cel parameters for (%d/%d/%d) failed (Maybe the values weren't checked beforehand?)",
		         view_, cel_, loop_);
	}

	_pos = pos_;
	_color.mask = ((priority < 0) ? 0 : GFX_MASK_PRIORITY) | ((control < 0) ? 0 : GFX_MASK_CONTROL);
	_widgetPriority = priority;
	_color.priority = priority;
	_color.control = control;
	_view = view_;
	_loop = loop_;
	_cel = cel_;
	_palette = palette_;

	_color.alpha = 0;
	_color.visual = PaletteEntry(0,0,0); // FIXME: black!

	if (halign == ALIGN_CENTER)
		xalignmod = width >> 1;
	else if (halign == ALIGN_RIGHT)
		xalignmod = width;
	else
		xalignmod = 0;

	if (valign == ALIGN_CENTER)
		yalignmod = height >> 1;
	else if (valign == ALIGN_BOTTOM)
		yalignmod = height;
	else
		yalignmod = 0;

	draw_bounds = gfx_rect(_pos.x - xalignmod, _pos.y - yalignmod - z_, width, height);
	_bounds = gfx_rect(_pos.x - offset.x - xalignmod, _pos.y - offset.y - yalignmod - z_, width, height);

	under_bitsp.obj = NULL_REG;
	under_bits = 0;
	signalp.obj = NULL_REG;
	signal = 0;
	_z = z_;
	sequence = sequence_;
	force_precedence = 0;

	_isDrawn = false;

	_flags |= GFXW_FLAG_VISIBLE;

	_gfxw_set_ops_DYNVIEW(this);
}

//*** Text ***

GfxText::~GfxText() {
	if (_textHandle) {
		GfxState *state = _visual ? _visual->_gfxState : NULL;
		if (!state) {
			GFXERROR("Attempt to free text without supplying mode to free it from!\n");
			error("GfxText destructor failed. Breakpoint in %s, line %d", __FILE__, __LINE__);
		} else {
			gfxop_free_text(state, _textHandle);
			_textHandle = NULL;
		}
	}
}

int GfxText::draw(const Common::Point &pos) {
	DRAW_ASSERT(this, GFXW_TEXT);

	if (_textHandle == 0) {
		_textHandle = gfxop_new_text(_visual->_gfxState, _font, _text, _bounds.width,
	                   halign, valign, _color1, _color2, _bgcolor, _textFlags);
	}

	GFX_ASSERT(gfxop_draw_text(_visual->_gfxState, _textHandle, _move_rect(_bounds, pos)));
	return 0;
}

void GfxText::print(int indentation) const {
	printIntern(indentation);
	sciprintf("TEXT:'%s'", _text.c_str());
}

static int _gfxwop_text_equals(GfxWidget *widget, GfxWidget *other) {
	GfxText *wtext = (GfxText *)widget, *otext;
	if (other->_type != GFXW_TEXT)
		return 0;

	otext = (GfxText *)other;

	if ((wtext->_bounds.x != otext->_bounds.x) || (wtext->_bounds.y != otext->_bounds.y))
		return 0;

	if (wtext->halign != otext->halign || wtext->valign != otext->valign)
		return 0;

	if (wtext->_textFlags != otext->_textFlags)
		return 0;

	if (wtext->_font != otext->_font)
		return 0;

	/* if (!(_color_equals(wtext->_color1, otext->_color1) && _color_equals(wtext->_color2, otext->_color2)
			&& _color_equals(wtext->_bgcolor, otext->_bgcolor)))
		return 0; */

	return 1;
}

static int _gfxwop_text_should_replace(GfxWidget *widget, GfxWidget *other) {
	GfxText *wtext = (GfxText *)widget, *otext;

	if (other->_type != GFXW_TEXT)
		return 0;

	otext = (GfxText *)other;

	return wtext->_text != otext->_text;
}

static int _gfxwop_text_compare_to(GfxWidget *widget, GfxWidget *other) {
	return 1;
}

void _gfxw_set_ops_TEXT(GfxWidget *widget) {
	_gfxw_set_ops(widget,
	              _gfxwop_text_compare_to, _gfxwop_text_equals,
	              _gfxwop_basic_superarea_of);
	widget->should_replace = _gfxwop_text_should_replace;
}

GfxText *gfxw_new_text(GfxState *state, rect_t area, int font, const char *text, gfx_alignment_t halign,
	gfx_alignment_t valign, gfx_color_t color1, gfx_color_t color2, gfx_color_t bgcolor, int text_flags) {
	return new GfxText(state, area, font, text, halign, valign, color1, color2, bgcolor, text_flags);
}

GfxText::GfxText(GfxState *state, rect_t area, int font, const char *text, gfx_alignment_t halign_,
	gfx_alignment_t valign_, gfx_color_t color1_, gfx_color_t color2_, gfx_color_t bgcolor_, int text_flags_)
	: GfxWidget(GFXW_TEXT) {

	_widgetPriority = _gfxw_color_get_priority(color1_);
	_font = font;
	_text = text;
	halign = halign_;
	valign = valign_;
	_color1 = color1_;
	_color2 = color2_;
	_bgcolor = bgcolor_;
	_textFlags = text_flags_;
	_textHandle = NULL;

	gfxop_get_text_params(state, font, text, area.width, &width, &height, _textFlags,
	                      &lines_nr, &lineheight, &lastline_width);

	/* FIXME: Window is too big
	area.x += _calc_needmove(halign, area.width, width);
	area.y += _calc_needmove(valign, area.height, height);
	*/

	if (halign == ALIGN_LEFT)
		area.width = width;
	if (valign == ALIGN_TOP)
		area.height = height;

	_bounds = area;

	_flags |= GFXW_FLAG_VISIBLE;

	_gfxw_set_ops_TEXT(this);
}

void gfxw_text_info(GfxState *state, GfxText *text, int *lines, int *lineheight, int *offset) {
	if (lines)
		*lines = text->lines_nr;
	if (lineheight)
		*lineheight = text->lineheight;
	if (offset)
		*offset = text->lastline_width;
}

//-- Container types --

static int _gfxwop_container_add_dirty_rel(GfxContainer *cont, rect_t rect, int propagate) {
	DDIRTY(stderr, "->container_add_dirty_rel(%d,%d,%d,%d, %d)\n", GFX_PRINT_RECT(rect), propagate);
	return cont->add_dirty_abs(cont, _move_rect(rect, Common::Point(cont->zone.x, cont->zone.y)), propagate);
}

static void _gfxw_set_container_ops(GfxContainer *container,
	gfxw_bin_op *compare_to, gfxw_bin_op *equals,
	gfxw_bin_op *superarea_of,
	gfxw_unary_container_op *free_tagged, gfxw_unary_container_op *free_contents,
	gfxw_rect_op *add_dirty, gfxw_container_op *add) {
	_gfxw_set_ops(container, compare_to, equals, superarea_of);

	container->free_tagged = free_tagged;
	container->free_contents = free_contents;
	container->add_dirty_abs = add_dirty;
	container->add_dirty_rel = _gfxwop_container_add_dirty_rel;
	container->add = add;
}

static int _w_gfxwop_container_print_contents(const char *name, GfxWidget *widget, int indentation) {
	GfxWidget *seeker = widget;

	indent(indentation);

	sciprintf("--%s:\n", name);

	while (seeker) {
		seeker->print(indentation + 1);
		sciprintf("\n");
		seeker = seeker->_next;
	}

	return 0;
}

void GfxContainer::print(int indentation) const {
	sciprintf(" viszone=((%d,%d),(%dx%d))\n", zone.x, zone.y, zone.width, zone.height);

	indent(indentation);
	sciprintf("--dirty:\n");

	for (DirtyRectList::const_iterator dirty = _dirtyRects.begin(); dirty != _dirtyRects.end(); ++dirty) {
		indent(indentation + 1);
		sciprintf("dirty(%d,%d, (%dx%d))\n", dirty->x, dirty->y, dirty->width, dirty->height);
	}

	_w_gfxwop_container_print_contents("contents", _contents, indentation);
}


GfxContainer::GfxContainer(rect_t area, gfxw_widget_type_t type_)
	: GfxWidget(type_) {
	_bounds = zone = area;
	_contents = NULL;
	_nextpp = &_contents;

	free_tagged = NULL;
	free_contents = NULL;
	add_dirty_abs = NULL;
	add_dirty_rel = NULL;
	add = NULL;

	_flags |= GFXW_FLAG_VISIBLE | GFXW_FLAG_CONTAINER;
}

static int _gfxw_dirty_rect_overlaps_normal_rect(rect_t port_zone, rect_t bounds, rect_t dirty) {
	bounds.x += port_zone.x;
	bounds.y += port_zone.y;

	return gfx_rects_overlap(bounds, dirty);
}

static int _gfxwop_container_draw_contents(GfxWidget *widget, GfxWidget *contents) {
	GfxContainer *container = (GfxContainer *)widget;
	GfxState *gfx_state = (widget->_visual) ? widget->_visual->_gfxState : ((GfxVisual *) widget)->_gfxState;
	int draw_ports;
	rect_t nullzone = {0, 0, 0, 0};

	if (!contents)
		return 0;

	DirtyRectList::iterator dirty = container->_dirtyRects.begin();
	while (dirty != container->_dirtyRects.end()) {
		GfxWidget *seeker = contents;

		while (seeker) {
			if (_gfxw_dirty_rect_overlaps_normal_rect(GFXW_IS_CONTAINER(seeker) ? nullzone : container->zone,
			        // Containers have absolute coordinates, reflect this.
			        seeker->_bounds, *dirty)) {

				if (GFXW_IS_CONTAINER(seeker)) {// Propagate dirty rectangles /upwards/
					DDIRTY(stderr, "container_draw_contents: propagate upwards (%d,%d,%d,%d ,0)\n", GFX_PRINT_RECT(*dirty));
					((GfxContainer *)seeker)->add_dirty_abs((GfxContainer *)seeker, *dirty, 0);
				}

				seeker->_flags |= GFXW_FLAG_DIRTY;
			}

			seeker = seeker->_next;
		}

		++dirty;
	}

	// The draw loop is executed twice: Once for normal data, and once for ports.
	for (draw_ports = 0; draw_ports < 2; draw_ports++) {

		dirty = container->_dirtyRects.begin();
		while (dirty != container->_dirtyRects.end()) {
			GfxWidget *seeker = contents;
			// FIXME: Ugly hack to check whether this is the last dirty rect or not
			DirtyRectList::iterator next = dirty;
			++next;
			const bool isLastDirtyRect = (next == container->_dirtyRects.end());

			while (seeker && (draw_ports || !GFXW_IS_PORT(seeker))) {
				rect_t small_rect = *dirty;
				bool draw_noncontainers = !_gfxop_clip(&small_rect, container->_bounds);

				if (seeker->_flags & GFXW_FLAG_DIRTY) {

					if (!GFXW_IS_CONTAINER(seeker) && draw_noncontainers) {
						GFX_ASSERT(gfxop_set_clip_zone(gfx_state, small_rect));
					}
					/* Clip zone must be reset after each element, because we might
					** descend into containers.
					** Doing this is relatively cheap, though. */
					if (draw_noncontainers || GFXW_IS_CONTAINER(seeker))
						seeker->draw(Common::Point(container->zone.x, container->zone.y));

					if (isLastDirtyRect)
						seeker->_flags &= ~GFXW_FLAG_DIRTY;
				}

				seeker = seeker->_next;
			}
			++dirty;
		}
	}
	// Remember that the dirty rects should be freed afterwards!

	return 0;
}

GfxContainer::~GfxContainer() {
	GfxWidget *seeker = _contents;

	while (seeker) {
		GfxWidget *next = seeker->_next;
		delete seeker;
		seeker = next;
	}

	_dirtyRects.clear();
}

void GfxContainer::tag() {
	// FIXME: Should we also tag this object itself?
	GfxWidget *seeker = _contents;
	while (seeker) {
		seeker->tag();
		seeker = seeker->_next;
	}
}

int GfxContainer::setVisual(GfxVisual *visual) {
	_visual = visual;
	if (_parent) {
		if (!(GFXW_IS_LIST(this) && !_contents)) {
			DDIRTY(stderr, "set_visual::DOWNWARDS abs(%d,%d,%d,%d, 1)\n", GFX_PRINT_RECT(_bounds));
			_parent->add_dirty_abs(_parent, _bounds, 1);
		}
	}

	GfxWidget *seeker = _contents;
	while (seeker) {
		seeker->setVisual(visual);
		seeker = seeker->_next;
	}
	return 0;
}

static int _gfxwop_container_free_tagged(GfxContainer *container) {
	GfxWidget *seeker = container->_contents;

	while (seeker) {
		GfxWidget *next = seeker->_next;
		if (seeker->_flags & GFXW_FLAG_TAGGED)
			delete seeker;
		seeker = next;
	}

	return 0;
}

static int _gfxwop_container_free_contents(GfxContainer *container) {
	GfxWidget *seeker = container->_contents;

	while (seeker) {
		GfxWidget *next = seeker->_next;
		delete seeker;
		seeker = next;
	}
	return 0;
}

static void _gfxw_dirtify_container(GfxContainer *container, GfxWidget *widget) {
	if (GFXW_IS_CONTAINER(widget))
		container->add_dirty_abs(container, widget->_bounds, 1);
	else
		container->add_dirty_rel(container, widget->_bounds, 1);
}

static int _parentize_widget(GfxContainer *container, GfxWidget *widget) {
	if (widget->_parent) {
		GFXERROR("_gfxwop_container_add(): Attempt to give second parent node to widget!\nWidget:");
		widget->print(3);
		sciprintf("\nContainer:");
		container->print(3);

		return 1;
	}

	widget->_parent = container;

	if (GFXW_IS_VISUAL(container))
		widget->setVisual((GfxVisual *)container);
	else if (container->_visual)
		widget->setVisual(container->_visual);

	return 0;
}

static int _gfxw_container_id_equals(GfxContainer *container, GfxWidget *widget) {
	GfxWidget **seekerp = &(container->_contents);

	if (GFXW_IS_PORT(widget))
		return 0;

	if (widget->_ID == GFXW_NO_ID)
		return 0;

	while (*seekerp && ((*seekerp)->_ID != widget->_ID || (*seekerp)->_subID != widget->_subID))
		seekerp = &((*seekerp)->_next);

	if (!*seekerp)
		return 0;

	if ((*seekerp)->equals(*seekerp, widget) && !(*seekerp)->should_replace(*seekerp, widget)) {
		delete widget;
		(*seekerp)->_flags &= ~GFXW_FLAG_TAGGED;
		return 1;
	} else {
		if (!(widget->_flags & GFXW_FLAG_MULTI_ID))
			delete *seekerp;
		return 0;
	}
}

static int _gfxwop_container_add_dirty(GfxContainer *container, rect_t dirty, int propagate) {
#if 0
	// This code has been disabled because containers may contain sub-containers with
	// bounds greater than their own.
	if (_gfxop_clip(&dirty, container->_bounds))
		return 0;
#endif

	DDIRTY(stderr, "Effectively adding dirty %d,%d,%d,%d %d to ID %d\n", GFX_PRINT_RECT(dirty), propagate, container->_ID);
	gfxdr_add_dirty(container->_dirtyRects, dirty, GFXW_DIRTY_STRATEGY);
	return 0;
}

static int _gfxwop_container_add(GfxContainer *container, GfxWidget *widget) {
	if (_gfxw_container_id_equals(container, widget))
		return 0;

	if (_parentize_widget(container, widget))
		return 1;

	if (!(GFXW_IS_LIST(widget) && (!((GfxContainer *)widget)->_contents))) { // Don't dirtify self on empty lists
		DDIRTY(stderr, "container_add: dirtify DOWNWARDS (%d,%d,%d,%d, 1)\n", GFX_PRINT_RECT(widget->_bounds));
		_gfxw_dirtify_container(container, widget);
	}

	*(container->_nextpp) = widget;
	container->_nextpp = &(widget->_next);

	return 0;
}

//*** Lists and sorted lists ***

int GfxList::draw(const Common::Point &pos) {
	if (_type == GFXW_LIST) {
		DRAW_ASSERT(this, GFXW_LIST);

		_gfxwop_container_draw_contents(this, _contents);
		_flags &= ~GFXW_FLAG_DIRTY;
	} else {
		DRAW_ASSERT(this, GFXW_SORTED_LIST);

		_gfxwop_container_draw_contents(this, _contents);
	}
	_dirtyRects.clear();

	return 0;
}


void GfxList::print(int indentation) const {
	printIntern(indentation);

	if (_type == GFXW_LIST)
		sciprintf("LIST");
	else if (_type == GFXW_SORTED_LIST)
		sciprintf("SORTED_LIST");
	else
		error("GfxList::print: Invalid type %d", _type);

	GfxContainer::print(indentation);
}

static int _gfxwop_list_equals(GfxWidget *widget, GfxWidget *other) {
	// Requires identical order of list elements.
	GfxList *wlist, *olist;

	if (widget->_type != other->_type)
		return 0;

	if (!GFXW_IS_LIST(widget)) {
		warning("[GFX] _gfxwop_list_equals(): Method called on non-list");
		widget->print(0);
		sciprintf("\n");
		return 0;
	}

	wlist = (GfxList *)widget;
	olist = (GfxList *)other;

	if (memcmp(&(wlist->_bounds), &(olist->_bounds), sizeof(rect_t)))
		return 0;

	widget = wlist->_contents;
	other = olist->_contents;

	while (widget && other) {
		if (!(widget->equals(widget, other) && !widget->should_replace(widget, other)))
			return 0;

		widget = widget->_next;
		other = other->_next;
	}

	return (!widget && !other); // True if both are finished now
}

static int _gfxwop_list_add_dirty(GfxContainer *container, rect_t dirty, int propagate) {
	// Lists add dirty boxes to both themselves and their parenting port/visual

	container->_flags |= GFXW_FLAG_DIRTY;

	DDIRTY(stderr, "list_add_dirty %d,%d,%d,%d %d\n", GFX_PRINT_RECT(dirty), propagate);
	if (propagate)
		if (container->_parent) {
			DDIRTY(stderr, "->PROPAGATING\n");
			container->_parent->add_dirty_abs(container->_parent, dirty, 1);
		}

	return _gfxwop_container_add_dirty(container, dirty, propagate);
}

int _gfxwop_ordered_add(GfxContainer *container, GfxWidget *widget, int compare_all) {
	// O(n)
	GfxWidget **seekerp = &(container->_contents);

	if (widget->_next) {
		GFXERROR("_gfxwop_sorted_list_add(): Attempt to add widget to two lists!\nWidget:");
		widget->print(3);
		sciprintf("\nList:");
		container->print(3);
		error("Breakpoint in %s, line %d", __FILE__, __LINE__);
	}

	if (_gfxw_container_id_equals(container, widget))
		return 0;

	while (*seekerp && (compare_all || (widget->compare_to(widget, *seekerp) >= 0))) {

		if (widget->equals(widget, *seekerp)) {
			if (compare_all) {
				if ((*seekerp)->_visual)
					delete *seekerp; // If it's a fresh widget
				else
					gfxw_annihilate(*seekerp);

				return _gfxwop_ordered_add(container, widget, compare_all); // We might have destroyed the container's contents
			} else {
				widget->_next = (*seekerp)->_next;
				delete *seekerp;
				*seekerp = widget;
				return (_parentize_widget(container, widget));
			}
		}

		if (*seekerp)
			seekerp = &((*seekerp)->_next);
	}

	widget->_next = *seekerp;
	*seekerp = widget;

	return _parentize_widget(container, widget);
}

static int _gfxwop_sorted_list_add(GfxContainer *container, GfxWidget *widget) {
	// O(n)
	return _gfxwop_ordered_add(container, widget, 0);
}

void _gfxw_set_ops_LIST(GfxContainer *list, char sorted) {
	_gfxw_set_container_ops((GfxContainer *)list,
	                        _gfxwop_basic_compare_to, sorted ? _gfxwop_basic_equals : _gfxwop_list_equals,
	                        _gfxwop_basic_superarea_of,
	                        _gfxwop_container_free_tagged, _gfxwop_container_free_contents,
	                        _gfxwop_list_add_dirty, sorted ? _gfxwop_sorted_list_add : _gfxwop_container_add);
}

GfxList *gfxw_new_list(rect_t area, int sorted) {
	return new GfxList(area, sorted);
}

GfxList::GfxList(rect_t area, bool sorted)
	: GfxContainer(area, sorted ? GFXW_SORTED_LIST : GFXW_LIST) {

	_gfxw_set_ops_LIST(this, sorted);
}



//*** Visuals ***

int GfxVisual::draw(const Common::Point &pos) {
	DRAW_ASSERT(this, GFXW_VISUAL);

	for (DirtyRectList::iterator dirty = _dirtyRects.begin(); dirty != _dirtyRects.end(); ++dirty) {
		int err = gfxop_clear_box(_gfxState, *dirty);

		if (err) {
			GFXERROR("Error while clearing dirty rect (%d,%d,(%dx%d))\n", dirty->x,
			         dirty->y, dirty->width, dirty->height);
			if (err == GFX_FATAL)
				return err;
		}
	}

	_gfxwop_container_draw_contents(this, _contents);

	_dirtyRects.clear();
	_flags &= ~GFXW_FLAG_DIRTY;

	return 0;
}

void GfxVisual::print(int indentation) const {
	printIntern(indentation);
	sciprintf("VISUAL; ports={");
	for (uint i = 0; i < _portRefs.size(); i++) {
		if (_portRefs[i]) {
			if (i != 0)
				sciprintf(",");
			sciprintf("%d", i);
		}
	}
	sciprintf("}\n");

	GfxContainer::print(indentation);
}

int GfxVisual::setVisual(GfxVisual *visual) {
	if (this != visual) {
		warning("Attempt to set a visual's parent visual to something else");
	} else {
		warning("Attempt to set a visual's parent visual to itself");
	}

	return 1;
}

void _gfxw_set_ops_VISUAL(GfxContainer *visual) {
	_gfxw_set_container_ops((GfxContainer *)visual,
	                        _gfxwop_basic_compare_to,
	                        _gfxwop_basic_equals, _gfxwop_basic_superarea_of,
	                        _gfxwop_container_free_tagged, _gfxwop_container_free_contents,
	                        _gfxwop_container_add_dirty, _gfxwop_container_add);
}

GfxVisual::GfxVisual(GfxState *state, int font)
	: GfxContainer(gfx_rect(0, 0, 320, 200), GFXW_VISUAL) {

	_font = font;
	_gfxState = state;

	_gfxw_set_ops_VISUAL(this);
}

GfxVisual::~GfxVisual() {
	// HACK: We must dispose all content *here* already, because our child widgets
	// still may have references to this object, and will try to invoke methods
	// of this object which try to access the already cleared _portRefs array
	// when they are destroyed.
	GfxWidget *seeker = _contents;

	while (seeker) {
		GfxWidget *next = seeker->_next;
		delete seeker;
		seeker = next;
	}
	_contents = 0;
}

static int _visual_find_free_ID(GfxVisual *visual) {
	uint id = 0;

	while (id < visual->_portRefs.size() && visual->_portRefs[id])
		id++;

	if (id == visual->_portRefs.size()) { // Out of ports?
		visual->_portRefs.push_back(0);
	}

	return id;
}

//*** Ports ***

int GfxPort::draw(const Common::Point &pos) {
	DRAW_ASSERT(this, GFXW_PORT);

	if (_decorations) {
		DDIRTY(stderr, "Getting/applying deco dirty (multi)\n");

		DDIRTY(stderr, "Adding multiple dirty to #%d\n", _decorations->_ID);
		for (DirtyRectList::iterator dirty = _dirtyRects.begin(); dirty != _dirtyRects.end(); ++dirty) {
			gfxdr_add_dirty(_decorations->_dirtyRects, *dirty, GFXW_DIRTY_STRATEGY);
		}

		if (_decorations->draw(gfxw_point_zero)) {
			_decorations->_dirtyRects.clear();
			return 1;	// error
		}
		_decorations->_dirtyRects.clear();
	}

	_gfxwop_container_draw_contents(this, _contents);

	_dirtyRects.clear();
	_flags &= ~GFXW_FLAG_DIRTY;

	return 0;
}

GfxPort::~GfxPort() {
	if (_visual) {
		if (_ID < 0 || _ID >= (int)_visual->_portRefs.size()) {
			error("Attempt to free port #%d; allowed: [0..%d]", _ID, _visual->_portRefs.size());
		}

		if (_visual->_portRefs[_ID] != this) {
			warning("[GFX] While freeing port %d: Port is at %p, but port list indicates %p", _ID, (void *)this, (void *)_visual->_portRefs[_ID]);
		} else
			_visual->_portRefs[_ID] = NULL;

	}

	delete _decorations;
}

void GfxPort::print(int indentation) const {
	printIntern(indentation);
	sciprintf("PORT");
	sciprintf(" font=%d drawpos=(%d,%d)", _font, draw_pos.x, draw_pos.y);
	if (gray_text)
		sciprintf(" (gray)");

	GfxContainer::print(indentation);
	_w_gfxwop_container_print_contents("decorations", _decorations, indentation);
}

static int _gfxwop_port_superarea_of(GfxWidget *self, GfxWidget *other) {
	GfxPort *port = (GfxPort *) self;

	if (!port->port_bg)
		return _gfxwop_basic_superarea_of(self, other);

	return port->port_bg->superarea_of(port->port_bg, other);
}

int GfxPort::setVisual(GfxVisual *visual) {
	_visual = visual;

	if (_decorations)
		if (_decorations->setVisual(visual)) {
			warning("[GFX] Setting the visual for decorations failed for port ");
			this->print(1);
			return 1;
		}

	return GfxContainer::setVisual(visual);
}

static int _gfxwop_port_add_dirty(GfxContainer *widget, rect_t dirty, int propagate) {
	GfxPort *self = (GfxPort *) widget;

	self->_flags |= GFXW_FLAG_DIRTY;

	_gfxwop_container_add_dirty(widget, dirty, propagate);

	DDIRTY(stderr, "Added dirty to ID %d\n", widget->_ID);
	DDIRTY(stderr, "dirty= (%d,%d,%d,%d) bounds (%d,%d,%d,%d)\n", dirty.x, dirty.x, dirty.width, dirty.height,
	       widget->_bounds.x, widget->_bounds.y, widget->_bounds.width, widget->_bounds.height);
#if 0
	// FIXME: This is a worthwhile optimization
	if (self->port_bg) {
		GfxWidget foo;

		foo.bounds = dirty; // Yeah, sub-elegant, I know
		foo.bounds.x -= self->zone.x;
		foo.bounds.y -= self->zone.y;
		if (self->port_bg->superarea_of(self->port_bg, &foo)) {
			GfxContainer *parent = self->_parent;
			while (parent) {
				fprintf(stderr, "Dirtifying parent id %d\n", parent->_ID);
				parent->_flags |= GFXW_FLAG_DIRTY;
				parent = parent->_parent;
			}
			return 0;
		}
	} // else propagate to the parent, since we're not 'catching' the dirty rect
#endif

	if (propagate)
		if (self->_parent) {
			DDIRTY(stderr, "PROPAGATE\n");
			return self->_parent->add_dirty_abs(self->_parent, dirty, 1);
		}

	return 0;
}

static int _gfxwop_port_add(GfxContainer *container, GfxWidget *widget) {
	// O(n)
	return _gfxwop_ordered_add(container, widget, 1);
}

void _gfxw_set_ops_PORT(GfxContainer *widget) {
	_gfxw_set_container_ops((GfxContainer *)widget,
	                        _gfxwop_basic_compare_to, _gfxwop_basic_equals, _gfxwop_port_superarea_of,
	                        _gfxwop_container_free_tagged, _gfxwop_container_free_contents,
	                        _gfxwop_port_add_dirty, _gfxwop_port_add);
}

GfxPort::GfxPort(GfxVisual *visual_, rect_t area, gfx_color_t fgcolor, gfx_color_t bgcolor_)
	: GfxContainer(area, GFXW_PORT) {
	VERIFY_WIDGET(visual_);

	port_bg = NULL;
	_parent = NULL;
	_decorations = NULL;
	title_text = NULL;
	draw_pos = Common::Point(0, 0);
	gray_text = 0;
	_color = fgcolor;
	_bgcolor = bgcolor_;
	_font = visual_->_font;
	_ID = _visual_find_free_ID(visual_);
	visual_->_portRefs[_ID] = this;

	_gfxw_set_ops_PORT(this);
}

void gfxw_port_auto_restore_background(GfxVisual *visual, GfxPort *window, rect_t auto_rect) {
	window->port_flags |= kWindowAutoRestore;
	window->restore_snap = gfxw_make_snapshot(visual, auto_rect);
}

GfxPort *gfxw_remove_port(GfxVisual *visual, GfxPort *port) {
	GfxPort *parent;
	VERIFY_WIDGET(visual);
	VERIFY_WIDGET(port);

	if (!visual->_contents) {
		warning("[GFX] Attempt to remove port from empty visual");
		return NULL;
	}

	parent = (GfxPort *)port->_parent;
	if (port->port_flags & kWindowAutoRestore)
		gfxw_restore_snapshot(visual, port->restore_snap);

	delete port;

	while (parent && !GFXW_IS_PORT(parent))
		parent = (GfxPort *)parent->_parent; // Ascend through ancestors

	return parent;
}

GfxPort *gfxw_find_default_port(GfxVisual *visual) {
	int id = visual->_portRefs.size();

	while (id--) {
		GfxPort *port = visual->_portRefs[id];

		if (port)
			return port;
	}

	return NULL;
}

// - other functions -

GfxWidget *gfxw_set_id(GfxWidget *widget, int ID, int subID) {
	if (widget) {
		widget->_ID = ID;
		widget->_subID = subID;
	}

	return widget;
}

GfxDynView *gfxw_dyn_view_set_params(GfxDynView *widget, int under_bits, const ObjVarRef& under_bitsp, int signal, const ObjVarRef& signalp) {
	if (!widget)
		return NULL;

	widget->under_bits = under_bits;
	widget->under_bitsp = under_bitsp;
	widget->signal = signal;
	widget->signalp = signalp;

	return widget;
}

GfxWidget *gfxw_remove_id(GfxContainer *container, int ID, int subID) {
	GfxWidget **wp = &(container->_contents);

	while (*wp) {
		if ((*wp)->_ID == ID && (subID == GFXW_NO_ID || (*wp)->_subID == subID)) {
			GfxWidget *widget = *wp;

			*wp = (*wp)->_next;
			widget->_next = NULL;
			widget->_parent = NULL;
			widget->_visual = NULL;

			return widget;
		}

		wp = &((*wp)->_next);
	}

	return NULL;
}

GfxWidget *gfxw_hide_widget(GfxWidget *widget) {
	if (widget->_flags & GFXW_FLAG_VISIBLE) {
		widget->_flags &= ~GFXW_FLAG_VISIBLE;

		if (widget->_parent)
			widget->_parent->add_dirty_rel(widget->_parent, widget->_bounds, 1);
	}

	return widget;
}

GfxWidget *gfxw_show_widget(GfxWidget *widget) {
	if (!(widget->_flags & GFXW_FLAG_VISIBLE)) {
		widget->_flags |= GFXW_FLAG_VISIBLE;

		if (widget->_parent)
			widget->_parent->add_dirty_rel(widget->_parent, widget->_bounds, 1);
	}

	return widget;
}

gfxw_snapshot_t *gfxw_make_snapshot(GfxVisual *visual, rect_t area) {
	gfxw_snapshot_t *retval = (gfxw_snapshot_t*)malloc(sizeof(gfxw_snapshot_t));

	retval->serial = widget_serial_number_counter++;

	retval->area = area;

	// Work around subset semantics in gfx_rect_subset.
	// This fixes the help icon in LSL5. */
	if (retval->area.width == 320)
		retval->area.width = 321;

	return retval;
}

int gfxw_widget_matches_snapshot(gfxw_snapshot_t *snapshot, GfxWidget *widget) {
	int free_below = (snapshot->serial < widget_serial_number_counter) ? 0 : widget_serial_number_counter;
	int free_above_eq = snapshot->serial;
	rect_t bounds = widget->_bounds;

	if (!GFXW_IS_CONTAINER(widget) && widget->_parent) {
		bounds.x += widget->_parent->_bounds.x;
		bounds.y += widget->_parent->_bounds.y;
	}

	// Note: the check for snapshot->area and bounds is NOT the same as contains() in Common::Rect
	// (this one includes equality too)
	return ((widget->_serial >= free_above_eq || widget->_serial < free_below) &&
			(snapshot->area.x <= bounds.x && snapshot->area.y <= bounds.y &&
			 snapshot->area.width >= bounds.width &&
			 snapshot->area.height >= bounds.height));
}

#define MAGIC_FREE_NUMBER -42

void _gfxw_free_contents_appropriately(GfxContainer *container, gfxw_snapshot_t *snapshot, int priority) {
	GfxWidget *widget = container->_contents;

	while (widget) {
		GfxWidget *next = widget->_next;

		if (gfxw_widget_matches_snapshot(snapshot, widget) && !(widget->_flags & GFXW_FLAG_IMMUNE_TO_SNAPSHOTS)
		        && (priority == MAGIC_FREE_NUMBER || priority <= widget->_widgetPriority || widget->_widgetPriority == -1)) {
			delete widget;
		} else {
			if (GFXW_IS_CONTAINER(widget))
				_gfxw_free_contents_appropriately((GfxContainer *)widget, snapshot, priority);
		}

		widget = next;
	}
}

gfxw_snapshot_t *gfxw_restore_snapshot(GfxVisual *visual, gfxw_snapshot_t *snapshot) {
	_gfxw_free_contents_appropriately((GfxContainer *)visual, snapshot, MAGIC_FREE_NUMBER);

	return snapshot;
}

void gfxw_annihilate(GfxWidget *widget) {
	GfxVisual *visual = widget->_visual;
	int widget_priority = 0;
	int free_overdrawn = 0;

	gfxw_snapshot_t snapshot;
	if (!GFXW_IS_CONTAINER(widget) && widget->_parent && visual && (widget->_flags & GFXW_FLAG_VISIBLE)) {
		snapshot.serial = 0;
		snapshot.area = widget->_bounds;
		snapshot.area.x += widget->_parent->zone.x;
		snapshot.area.y += widget->_parent->zone.y;
		free_overdrawn = 1;
		widget_priority = widget->_widgetPriority;
	}

	delete widget;

	if (free_overdrawn)
		_gfxw_free_contents_appropriately((GfxContainer *)visual, &snapshot, widget_priority);
}

GfxDynView *gfxw_picviewize_dynview(GfxDynView *dynview) {
	dynview->_type = GFXW_PIC_VIEW;
	dynview->_flags |= GFXW_FLAG_DIRTY;

	_gfxw_set_ops_PICVIEW(dynview);

	if (dynview->_parent)
		_gfxw_dirtify_container(dynview->_parent, dynview);

	return dynview;
}

} // End of namespace Sci