File: myx_gc_view.cpp

package info (click to toggle)
mysql-query-browser 1.2.5beta-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 63,792 kB
  • ctags: 46,485
  • sloc: pascal: 249,299; ansic: 80,111; cpp: 72,467; sh: 25,271; objc: 20,015; yacc: 10,755; java: 9,917; xml: 4,580; php: 2,806; python: 1,566; sql: 1,563; makefile: 1,452; perl: 3
file content (2178 lines) | stat: -rw-r--r-- 70,529 bytes parent folder | download | duplicates (4)
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
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
/* Copyright (C) 2004 MySQL AB

   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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/**
 * @file myx_gc_view.cpp 
 * @brief Implementation of the view class.
 * 
 */

#include "myx_gc_canvas.h"
#include "myx_gc_feedback.h"
#include "gc_glext.h"
#include "myx_gc_gl_helper.h"

extern const TMatrix identity;

//----------------- CFigureInstanceEnumerator --------------------------------------------------------------------------

/**
 * Constructor of the enumerator class.
 *
 * @param view The view which contains the layers which are to be enumerated.
 */
CFigureInstanceEnumerator::CFigureInstanceEnumerator(CGCView* view)
{
  FView = view;
  reset();
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Determines if there is a next figure instance to enumerate.
 *
 * @return True if there is still a figure instance otherwise false.
 */
bool CFigureInstanceEnumerator::hasNext(void)
{
  return FLayerIterator != FView->FLayers.end();
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Returns the next figure instance in the sequence.
 *
 * @return The next figure instance.
 */
CFigureInstance* CFigureInstanceEnumerator::next(void)
{
  CFigureInstance* result = *FFigureInstanceIterator;

  // Advance to next instance.
  ++FFigureInstanceIterator;
  while (true)
  {
    CLayer* layer = *FLayerIterator;
    if (FFigureInstanceIterator != layer->FInstances.end())
      break;

    // This layer is exhausted. Get the next one.
    ++FLayerIterator;
    if (FLayerIterator == FView->FLayers.end())
    {
      // No more layers.
      break;
    };

    if (!(*FLayerIterator)->FEnabled)
      FFigureInstanceIterator = (*FLayerIterator)->FInstances.end();
  };

  return result;
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Frees this enumerator instance. Usually called by non-C++ languages as memory is managed by the C++ runtime.
 */
void CFigureInstanceEnumerator::release(void)
{
  delete this;
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Resets the enumerator to the first figure instance in the canvas.
 */
void CFigureInstanceEnumerator::reset(void)
{
  FLayerIterator = FView->FLayers.begin();
  while (FLayerIterator != FView->FLayers.end())
  {
    if ((*FLayerIterator)->FEnabled)
    {
      CLayer* layer = *FLayerIterator;
      FFigureInstanceIterator = layer->FInstances.begin();
      if (FFigureInstanceIterator == layer->FInstances.end())
      {
        ++FLayerIterator;
      }
      else
        break; // Found the first instance.
    }
    else
      ++FLayerIterator;
  };
}

//----------------- CLayerListener -----------------------------------------------------------------------------

void CGCView::CLayerListener::onAction(CGCBase* sender, CGCBase* origin, TAction** action)
{
  view->action(origin, action);
}

//----------------------------------------------------------------------------------------------------------------------

void CGCView::CLayerListener::onChange(CGCBase* sender, CGCBase* origin, TGCChangeReason reason)
{
  view->handleChange(origin, reason);
};

//----------------------------------------------------------------------------------------------------------------------

void CGCView::CLayerListener::onDestroy(CGCBase* object)
{
  view->removeLayer((CLayer*) object);
}

//----------------------------------------------------------------------------------------------------------------------

void CGCView::CLayerListener::onError(CGCBase* sender, CGCBase* origin, const char* message)
{
  view->error(origin, message);
}

//----------------- CGCView --------------------------------------------------------------------------------------------

CGCView::CGCView(CGenericCanvas* canvas, string name): CGCBase(canvas)
{
  _className = "CGCView";
  FName = utf8ToUtf16(name);
  FColor[0] = 0;
  FColor[1] = 0;
  FColor[2] = 0;
  FColor[3] = 1;
  FZoom = 1;
  FOffsetX = 0;
  FOffsetY = 0;
  FWorkspaceWidth = 1000;
  FWorkspaceHeight = 1000;
  FNearPlane = -1000;
  FFarPlane = 1000;
  FJitter = 0;
  FListener.view = this;
  FStates = 0;
  FOrigin[0] = 0;
  FOrigin[1] = 0;
  FOrigin[2] = 0;

  FGrid = new CGridLayer(this);
  FFeedbackLayer = new CFeedbackLayer(this);
  FFeedbackLayer->addListener(&FListener);
  FConnectionLayer = new CConnectionLayer(this);
  FConnectionLayer->addListener(&FListener);
  FExposeFloater = NULL;
  FPrimitiveLayer = new CPrimitiveLayer(canvas);
  FCache = NULL;
  FDirty = true;
  FInAnimation = false;
}

//----------------------------------------------------------------------------------------------------------------------

CGCView::~CGCView()
{
  delete FCache;
  FCache = NULL;

  for (CLayers::const_iterator iterator = FLayers.begin(); iterator != FLayers.end(); ++iterator)
    (*iterator)->removeListener(&FListener);

  delete FGrid;
  delete FFeedbackLayer;
  delete FConnectionLayer;
  delete FPrimitiveLayer;
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Used to set up things that need only to be done once if a view becomes active.
 */
void CGCView::activate(void)
{
  // TODO: make this dynamically configurable (where applicable).
  glFrontFace(GL_CCW);
  glDisable(GL_CULL_FACE);
  glDisable(GL_DITHER);
  glDisable(GL_DEPTH_TEST);

  // The following is only useful if a multisampling pixel format was set up.
  if (canvas()->supportsExtension(GC_OE_MULTISAMPLING))
    glEnable(GL_MULTISAMPLE_ARB); 
  
  // Line and polygon smoothing is done via the multisample extension (if existant).
  glDisable(GL_LINE_SMOOTH);
  glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
  glDisable(GL_POLYGON_SMOOTH);
  glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
  
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

  glEnable(GL_AUTO_NORMAL);
  glEnable(GL_NORMALIZE);

  glDisable(GL_FOG);
  glDisable(GL_LOGIC_OP);
  glDisable(GL_STENCIL_TEST);
  glDisable(GL_TEXTURE_1D);
  glDisable(GL_TEXTURE_2D);
  glDisable(GL_LIGHTING);
  
  // Disable extensions that could slow down glDrawPixels.
  if (canvas()->supportsExtension(GC_OE_CONVOLUTION))
  {
    // A recent glext.h include file is required for these constants.
    // Get one from www.opengl.org.
#ifdef GL_VERSION_1_3
    glDisable(GL_CONVOLUTION_1D);
    glDisable(GL_CONVOLUTION_2D);
    glDisable(GL_SEPARABLE_2D);
#else
    glDisable(GL_CONVOLUTION_1D_EXT);
    glDisable(GL_CONVOLUTION_2D_EXT);
    glDisable(GL_SEPARABLE_2D_EXT);
#endif
  };

  if (canvas()->supportsExtension(GC_OE_HISTOGRAM))
  {
#ifdef GL_VERSION_1_3
    glDisable(GL_HISTOGRAM);
    glDisable(GL_MINMAX);
#else
    glDisable(GL_HISTOGRAM_EXT);
    glDisable(GL_MINMAX_EXT);
#endif
  };

  if (canvas()->supportsExtension(GC_OE_TEXTURE3D))
  {
#ifdef GL_VERSION_1_3
    glDisable(GL_TEXTURE_3D);
#else
    glDisable(GL_TEXTURE_3D_EXT);
#endif
  };
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Called by the animation manager when an animation of this view starts freshly or after a suspension.
 */
void CGCView::animationStarted(void)
{
  FInAnimation = true;
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Called by the animation manager when an animation of this view is stopped (either finished or suspended).
 *
 * @param zoom If true then a notification is sent out about a zoom change.
 * @param offset If true then a notification is sent out about an offset change.
 */
void CGCView::animationStopped(bool zoom, bool offset)
{
  FInAnimation = false;
  notifyZoomChange();
  if (zoom)
    change(this, GC_CHANGE_VIEW_ZOOM);
  if (offset)
    change(this, GC_CHANGE_VIEW_OFFSET);
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Sets up the current projection and modelview matrices for normal rendering.
 */
void CGCView::applyTransformations(void)
{
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(-FOffsetX / FZoom - FJitter, (FViewport.width - FOffsetX) / FZoom - FJitter, 
    (FViewport.height - FOffsetY) / FZoom - FJitter, -FOffsetY / FZoom - FJitter, FNearPlane, FFarPlane);

  glViewport(FViewport.left, FViewport.top, FViewport.width, FViewport.height);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Draws a rectangle around the entire view to indicate the focused state of the canvas.
 */
void CGCView::drawFocusRect(void)
{
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0, FViewport.width, FViewport.height, 0, FNearPlane, FFarPlane);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

#ifdef __WIN__
  glColor3f(0, 128.0f / 255, 192.0f / 255);
  glEnable (GL_LINE_STIPPLE);
  glLineStipple (1, 0x3333);  //  dotted
  glLineWidth(2);
  glBegin(GL_LINE_LOOP);
    glVertex3i(0, 0, 100);
    glVertex3i(FViewport.width, 0, 100);
    glVertex3i(FViewport.width, FViewport.height, 100);
    glVertex3i(0, FViewport.height, 100);
  glEnd();
  glLineWidth(1);
  glDisable(GL_LINE_STIPPLE);
#elif defined(__APPLE__)
  glColor4f(0.52, 0.62, 0.71, 0.8);
  glLineWidth(1);
  glBegin(GL_LINE_LOOP);
    glVertex3i(0, 0, 100);
    glVertex3i(FViewport.width, 0, 100);
    glVertex3i(FViewport.width, FViewport.height, 100);
    glVertex3i(0, FViewport.height, 100);
  glEnd();
  for (int i= 1; i <= 3; i++)
  {
    glColor4f(0.57, 0.72, 0.85, 0.8 - (float)i/6);
    glBegin(GL_LINE_LOOP);
      glVertex3i(i, i, 100);
      glVertex3i(FViewport.width-i, i-1, 100);
      glVertex3i(FViewport.width-i, FViewport.height-i, 100);
      glVertex3i(i, FViewport.height-i, 100);
    glEnd();
  }
#else
  glColor3f(0, 0, 0);
  glEnable (GL_LINE_STIPPLE);
  glLineStipple (1, 0xaaaa);  //  dotted
  glLineWidth(1);
  glBegin(GL_LINE_LOOP);
    glVertex3i(1, 1, 100);
    glVertex3i(FViewport.width, 1, 100);
    glVertex3i(FViewport.width, FViewport.height, 100);
    glVertex3i(1, FViewport.height, 100);
  glEnd();
  glDisable(GL_LINE_STIPPLE);
#endif
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Checks for an action that can be executed in the view.
 *
 * @param button Which button has been pressed (left, middle, right, x1, x2).
 * @param event The mouse event that caused the handling chain to start (up, down etc.).
 * @param modifiers Special flags that control the processing.
 * @param windowX Horizontal mouse coordinate in window space.
 * @param windowY Vertical mouse coordinate in window space.
 * @param viewCoords Mouse coordinates converted to view space.
 * @param feedbackInfo Info about previous feedback checks by the caller.
 * @param instance If set it holds a figure instance determined by previous feedback code.
 * @return The last action that was executed or NULL if there was none.
 */
TAction* CGCView::handleAction(TMouseButton button, TMouseEvent event, TModifiers modifiers, int windowX, int windowY, 
                               TVertex& viewCoords, TFeedbackInfo feedbackInfo, CFigureInstance* instance)
{
  static TAction defaultAction;
  defaultAction.type = GC_ACTION_NONE;
  TAction* lastAction = &defaultAction;

  bool keepHitInfo = false;
  if (instance == NULL || feedbackInfo == GC_FI_NONE)
  {
    // Determine if an element is hit.
    updateCache();

    CGraphicElement* hit = NULL;
    if (instance == NULL)
      hit = FCache->findElement(viewCoords);
    else
      hit = instance;
    // If an element was hit for which we have no further feedback info then just set the feedback to "on body".
    if (hit != NULL && feedbackInfo == GC_FI_NONE)
      feedbackInfo = GC_FI_ON_OBJECT;

    if (feedbackInfo != GC_FI_NONE)
    {
      // Since there is a small gap between object and selection decoration
      // hit can be NULL.
      if (hit != NULL)
      {
        instance = (CFigureInstance*) hit;
        TAction* action = instance->executeAssociatedActions(button, event, modifiers, viewCoords);
        if (action != NULL)
        {
          lastAction = action;
          if (action->type == GC_ACTION_DRAG)
          {
            // Ask app about dragging the figure.
            action = &defaultAction;
            action->type = GC_ACTION_DRAG_INSTANCE;
            instance->action(instance, &action);
            if (action->type == GC_ACTION_DRAG_INSTANCE)
            {
              lastAction = action;
              FStates |= GC_STATE_DRAG_PENDING;
              if (modifiers == GC_MODIFIER_NONE)
                FStates |= GC_STATE_CLEAR_PENDING;
              else
                if ((modifiers & GC_MODIFIER_SHIFT) != 0)
                  FFeedbackLayer->addToSelection(instance);
                else
                  if ((modifiers & GC_MODIFIER_CONTROL) != 0)
                    if (instance->selected())
                      FFeedbackLayer->removeFromSelection(instance);
                    else
                      FFeedbackLayer->addToSelection(instance);

              // If the hit is not yet selected then drag only this one instance,
              // otherwise the entire selection will be dragged.
              FDragSelection = instance->selected();
              keepHitInfo = true;
            };
          }
          else
            if (action->type == GC_ACTION_RESIZE)
            {
              feedbackInfo = action->feedbackInfo;
              keepHitInfo = true;
            };
        };
      };
    };
  };

  switch (feedbackInfo)
  {
    case GC_FI_RESIZE_NORTH:
    case GC_FI_RESIZE_NORTH_EAST:
    case GC_FI_RESIZE_EAST:
    case GC_FI_RESIZE_SOUTH_EAST:
    case GC_FI_RESIZE_SOUTH:
    case GC_FI_RESIZE_SOUTH_WEST:
    case GC_FI_RESIZE_WEST:
    case GC_FI_RESIZE_NORTH_WEST:
      {
        lastAction->type = GC_ACTION_RESIZE;
        action(this, &lastAction);
        if (lastAction != NULL && lastAction->type == GC_ACTION_RESIZE)
        {
          FStates |= GC_STATE_RESIZING;
          FResizeInfo = feedbackInfo;
          keepHitInfo = true;
        };
        break;
      };
  };

  if (keepHitInfo)
  {
    FLastHit = instance;
    FLastWindowX = windowX;
    FLastWindowY= windowY;
    FLastViewX = viewCoords.x;
    FLastViewY = viewCoords.y;
  };

  return lastAction;
};

//----------------------------------------------------------------------------------------------------------------------

/**
 * Main handler routine for double click handling.
 *
 * @param button Which button has been pressed (left, middle, right, x1, x2).
 * @param modifiers Special flags that control the processing.
 * @param windowX Horizontal mouse coordinate in window space.
 * @param windowY Vertical mouse coordinate in window space.
 * @param viewCoords Mouse coordinates converted to view space.
 * @return True if the input was handled, otherwise false.
 */
bool CGCView::handleMouseDblClick(TMouseButton button, TModifiers modifiers, int windowX, int windowY, TVertex& viewCoords)
{
  switch (button)
  {
    case GC_MOUSE_BUTTON_LEFT:
      {
        FStates |= GC_STATE_LBUTTON_DOWN;
        break;
      };
    case GC_MOUSE_BUTTON_MIDDLE:
      {
        FStates |= GC_STATE_MBUTTON_DOWN;
        break;
      };
    case GC_MOUSE_BUTTON_RIGHT:
      {
        FStates |= GC_STATE_RBUTTON_DOWN;
        break;
      };
  };

  TAction* action = handleAction(button, GC_MOUSE_DBL_CLICK, modifiers, windowX, windowY, viewCoords, GC_FI_NONE, NULL);

  return (action != NULL);
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Main handler routine for mouse button down handling.
 *
 * @param button Which button has been pressed (left, middle, right, x1, x2).
 * @param modifiers Special flags that control the processing.
 * @param windowX Horizontal mouse coordinate in window space.
 * @param windowY Vertical mouse coordinate in window space.
 * @param viewCoords Mouse coordinates converted to view space.
 * @return True if the input was handled, otherwise false.
 */
bool CGCView::handleMouseDown(TMouseButton button, TModifiers modifiers, int windowX, int windowY, TVertex& viewCoords)
{
  bool result = false;
  TAction defaultAction;
  defaultAction.type = GC_ACTION_NONE;
  TAction* lastAction = &defaultAction;

  switch (button)
  {
    case GC_MOUSE_BUTTON_LEFT:
      {
        FStates |= GC_STATE_LBUTTON_DOWN;
        break;
      };
    case GC_MOUSE_BUTTON_MIDDLE:
      {
        FStates |= GC_STATE_MBUTTON_DOWN;
        break;
      };
    case GC_MOUSE_BUTTON_RIGHT:
      {
        FStates |= GC_STATE_RBUTTON_DOWN;
        break;
      };
  };

  // First check special layers with interactive objects.
  CFigureInstance* instance = NULL;

  TFeedbackInfo feedbackInfo = FFeedbackLayer->getFeedbackInfo(modifiers, viewCoords, FZoom, &instance);
  if (feedbackInfo == GC_FI_FLOATER)
  {
    lastAction->type = GC_ACTION_FLOATER_DRAG;
    action(this, &lastAction);
    if (lastAction != NULL && lastAction->type == GC_ACTION_FLOATER_DRAG)
    {
      FStates |= GC_STATE_OVERVIEW_DRAG;
      FLastWindowX = windowX;
      FLastWindowY= windowY;
      FLastViewX = viewCoords.x;
      FLastViewY = viewCoords.y;
      result = true;
    };
  };

  if (!result)
  {
    TAction* action = handleAction(button, GC_MOUSE_DOWN, modifiers, windowX, windowY, viewCoords, feedbackInfo, instance);
    if (action != NULL && action->type != GC_ACTION_NONE)
    {
      lastAction = action;
      result = true;
    };
  };

  if (feedbackInfo == GC_FI_NONE)
    result = FConnectionLayer->handleMouseDown(button, modifiers, windowX, windowY, viewCoords);

  if (!result && (lastAction->type == GC_ACTION_NONE) && (button == GC_MOUSE_BUTTON_LEFT))
  {
    // Nothing happend so far so start the rubber rectangle if allowed.
    lastAction->type = GC_ACTION_RUBBER_RECT;
    action(this, &lastAction);
    if (lastAction != NULL && lastAction->type == GC_ACTION_RUBBER_RECT)
    {
      rubberRectStart(GC_RRSTYLE_BLENDED_SOLID, viewCoords, modifiers <= GC_MODIFIER_NONE);
      FStates |= GC_STATE_SELECTION_RECTANGLE;
      result = true;
    };
  };

  return result;
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Main handler routine for mouse button move handling.
 *
 * @param modifiers Any combination of TModifier. Specifies further how to handle the input.
 * @param windowX Horizontal mouse coordinate in window space.
 * @param windowY Vertical mouse coordinate in window space.
 * @param viewCoords Mouse coordinates converted to view space.
 * @return True if the input was handled, otherwise false.
 */
bool CGCView::handleMouseMove(TModifiers modifiers, int windowX, int windowY, TVertex& viewCoords)
{
  bool result = false;

  if ((FStates & GC_STATE_RUBBER_RECTANGLE) != 0)
  {
    TRRSelectionAction action = GC_RRACTION_NONE;
    if ((FStates & GC_STATE_SELECTION_RECTANGLE) != 0)
    {
      action = GC_RRACTION_SELECT_REMOVE;
      if ((modifiers & GC_MODIFIER_SHIFT) != 0)
        action = GC_RRACTION_SELECT; // Always-add has higher prio than toggle.
      else
        if ((modifiers & GC_MODIFIER_CONTROL) != 0)
          action = GC_RRACTION_TOGGLE;
    };

    FFeedbackLayer->rubberRectResize(viewCoords, action);
    result = true;
  };

  if ((FStates & GC_STATE_RUBBER_BAND) != 0)
  {
    FFeedbackLayer->rubberBandResize(viewCoords);
    result = true;
  };

  if ((FStates & GC_STATE_DRAG_PENDING) != 0)
  {
    // Drag is still waiting to start. Check if the user moved the mouse a bit.
    float threshold = 4; // Empirically found.
    if (abs(FLastWindowX - windowX) >= threshold || abs(FLastWindowY - windowY) >= threshold)
    {
      FStates &= ~(GC_STATE_CLEAR_PENDING | GC_STATE_DRAG_PENDING);
      FStates |= GC_STATE_DRAGGING;
      change(this, GC_CHANGE_VIEW_DRAGGING_START);
    };
  };

  // Now the moved distance becomes important.
  float dX = viewCoords.x - FLastViewX;
  float dY = viewCoords.y - FLastViewY;
  FLastViewX = viewCoords.x;
  FLastViewY = viewCoords.y;
  
  if ((FStates & GC_STATE_DRAGGING) != 0)
  {
    if (FDragSelection)
      FFeedbackLayer->moveSelectedInstances(dX, dY, 0, true);
    else
      FLastHit->translate(dX, dY, 0, true);
    #if USE_BSP_TREE
      FCache->invalidate();
    #endif
    result = true;
  }
  else
    if ((FStates & GC_STATE_RESIZING) != 0)
    {
      FLastHit->resize(dX, dY, FResizeInfo);
      #if USE_BSP_TREE
        FCache->invalidate();
      #endif
      result = true;
    }
    else
      if ((FStates & GC_STATE_OVERVIEW_DRAG) != 0)
      {
        FLastOffsetX -= dX * FLastZoom;
        FLastOffsetY -= dY * FLastZoom;
        FExposeFloater->move(dX, dY);
        result = true;
      };

  if (!result)
    result = FConnectionLayer->handleMouseMove(modifiers, windowX, windowY, viewCoords);

  return result;
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Main handler routine for mouse button up handling.
 *
 * @param button Which button has been released (left, middle, right).
 * @param modifiers Special flags that control the processing.
 * @param windowX Horizontal mouse coordinate in window space.
 * @param windowY Vertical mouse coordinate in window space.
 * @param viewCoords Mouse coordinates converted to view space.
 * @return True if the input was handled, otherwise false.
 */
bool CGCView::handleMouseUp(TMouseButton button, TModifiers modifiers, int windowX, int windowY, TVertex& viewCoords)
{
  bool handled = false;

  if ((FStates & GC_STATE_SELECTION_RECTANGLE) != 0)
  {
    rubberRectStop();
    FStates &= ~GC_STATE_SELECTION_RECTANGLE;
    handled = true;
  };

  if (!handled)
    handled = FConnectionLayer->handleMouseUp(button, modifiers, windowX, windowY, viewCoords);

  // All following processing is always done regardless of the results from the special layers (except for actions).
  if ((FStates & GC_STATE_CLEAR_PENDING) != 0)
  {
    // A pending clear selection action must be finished. If there is an instance hit from mouse down
    // then select this single instance.
    FStates &= ~GC_STATE_CLEAR_PENDING;
    FFeedbackLayer->clearSelection();
    if (FLastHit != NULL)
      FFeedbackLayer->addToSelection(FLastHit);
  };

  FLastHit = NULL;

  if ((FStates & GC_STATE_DRAGGING) != 0)
  {
    FStates &= ~GC_STATE_DRAGGING;
    change(this, GC_CHANGE_VIEW_DRAGGING_STOP);
  };

  // Remove all states which can be stopped without further action.
  FStates &= ~(GC_STATE_DRAG_PENDING | GC_STATE_RESIZING | GC_STATE_OVERVIEW_DRAG);

  switch (button)
  {
    case GC_MOUSE_BUTTON_LEFT:
      {
        FStates &= ~GC_STATE_LBUTTON_DOWN;
        break;
      };
    case GC_MOUSE_BUTTON_MIDDLE:
      {
        FStates &= ~GC_STATE_MBUTTON_DOWN;
        break;
      };
    case GC_MOUSE_BUTTON_RIGHT:
      {
        FStates &= ~GC_STATE_RBUTTON_DOWN;
        break;
      };
  };

  // Now handle actions bound to mouse up.
  if (!handled)
    handleAction(button, GC_MOUSE_UP, modifiers, windowX, windowY, viewCoords, GC_FI_NONE, NULL);

  return handled;
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Internal render routine used by different other functions.
 *
 * @param content Determines what to render.
 * @param visibleBounds The visible area that needs to be rendered. Anything outside can be omitted.
 */
void CGCView::internalRender(TGCRenderContent content, const TBoundingBox& visibleBounds)
{
  if ((content & GC_RENDER_GRID) != 0)
    FGrid->render(visibleBounds);

  if ((content & GC_RENDER_FRAME) != 0)
  {
    // A simple frame marks the virtual workspace size.
    glColor3ub(0, 0, 0);
    glBegin(GL_LINE_LOOP); 
      glVertex2f(0, 0);
      glVertex2f(FWorkspaceWidth - 1, 0); 
      glVertex2f(FWorkspaceWidth - 1, FWorkspaceHeight - 1);
      glVertex2f(0, FWorkspaceHeight - 1); 
    glEnd();
  };

  for (CLayers::iterator iterator = FLayers.begin(); iterator != FLayers.end(); ++iterator)
    (*iterator)->render(visibleBounds);

  FPrimitiveLayer->render(visibleBounds);

  if ((content & GC_RENDER_CONNECTIONS) != 0)
    FConnectionLayer->render(visibleBounds);

  if ((content & GC_RENDER_FEEDBACK) != 0)
    FFeedbackLayer->render(visibleBounds);

  if (canvas()->focusedGet() && ((content & GC_RENDER_FOCUS) != 0))
    drawFocusRect();
 };

//----------------------------------------------------------------------------------------------------------------------

/**
 * Notfies all layers in this view about a zoom change. This is necessary to have them updated element captions.
 */
void CGCView::notifyZoomChange(void)
{
  // Tell all layers in this view about the change. They don't have a listener as they can appear
  // in more than one view
  FGrid->zoomChanged(FZoom);
  for (CLayers::iterator iterator = FLayers.begin(); iterator != FLayers.end(); ++iterator)
    (*iterator)->zoomChanged(FZoom);
  FPrimitiveLayer->zoomChanged(FZoom);
  FConnectionLayer->zoomChanged(FZoom);
  FFeedbackLayer->zoomChanged(FZoom);
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * This is the main paint routine. It is called by the canvas if this view is the current view.
 *
 * @param The content to render.
 */
void CGCView::render(TGCRenderContent content)
{
  applyTransformations();

  // Transform viewport coordinates into view space. They are used for occlusion culling.
  TBoundingBox visibleBounds;
  visibleBounds.upper.x = -FOffsetX / FZoom;
  visibleBounds.upper.y = -FOffsetY / FZoom;
  visibleBounds.lower.x = (FViewport.width - FOffsetX) / FZoom;
  visibleBounds.lower.y = (FViewport.height - FOffsetY) / FZoom;

  internalRender(content, visibleBounds);
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Renders the entire content of the view to memory. The function tries to use an OpenGL extension (frame buffer objects)
 * which may not be available on all graphics boards. If this is the case or the set up of this extension fails then
 * a fall back is use, rendering the tradional (but slow and inaccurate) way.
 *
 * @param format Determines the order of the color components in the result.
 * @param content A set of flags indicating what additional info to render.
 * @param zoom The zoom level to be used for the output.
 * @param bounds [in/out] The coordinates of the area that should be rendered (in view space). When traditional rendering
 *                        is used then it might be necessary to implicitly restrict this area to the current viewport.
 *                        In this case bounds are used to return actual area to the caller.
 * @param memory The memory into which the view is to be rendered.
 */
void CGCView::renderToMemory(TGCColorFormat format, TGCRenderContent content, float zoom, TGCViewport& bounds, 
                             unsigned char* memory)
{
  GLenum glFormat = GL_RGBA;
  if (format == GC_COLOR_FORMAT_BGRA)
    glFormat = GL_BGRA;

  bool canUseFBOs = canvas()->supportsExtension(GC_OE_FRAME_BUFFER_OBJECTS);
  if (canUseFBOs)
  {
    // Get current draw buffer for later restoration.
    GLint currentDrawBuffer = 0;
    glGetIntegerv(GL_DRAW_BUFFER, &currentDrawBuffer);

    // Make sure we do not allocate more buffer space than supported.
    GLint maxBufferSize = 0x7FFFFFFF;
    glGetIntegerv(GL_MAX_RENDERBUFFER_SIZE_EXT, &maxBufferSize);

    GLint maxViewport[2];
    glGetIntegerv(GL_MAX_VIEWPORT_DIMS, maxViewport);

    // Determine area to render.
    float left = (float) bounds.left;
    float top = (float) bounds.top;
    float width = (float) bounds.width;
    if (width < 0)
      width = FWorkspaceWidth;
    float height = (float) bounds.height;
    if (height < 0)
      height = FWorkspaceHeight;

    int bufferWidth = 0;
    int bufferHeight = 0;

    GLuint frameBuffer = 0;
    GLuint renderBuffer = 0;

    GLenum status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
    while (maxBufferSize >= 128)
    {
      // Set maximum possible viewport here (restore old one on exit).
      bufferWidth = (maxViewport[0] < maxBufferSize) ? maxViewport[0] : maxBufferSize;
      bufferHeight = (maxViewport[1] < maxBufferSize) ? maxViewport[1] : maxBufferSize;

      if (width < bufferWidth)
        bufferWidth = (int) width;
      if (height < bufferHeight)
        bufferHeight = (int) height;

      // Frame buffer objects allow for hardware accelerated off-screen rendering.
      glGenFramebuffersEXT(1, &frameBuffer);
      glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameBuffer);
      glGenRenderbuffersEXT(1, &renderBuffer);
      glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, renderBuffer);
      glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, renderBuffer);

      glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA8, bufferWidth, bufferHeight); 
      
      // Check validity of the frame buffer and try other configs if possible. 
      // If all fails go back to traditional rendering, but don't expect good results then.
      status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
      if (status != GL_FRAMEBUFFER_UNSUPPORTED_EXT)
        break;

      // The FBO configuration did not work, so free the buffers and try again with smaller size.
      glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
      glDeleteRenderbuffersEXT(1, &renderBuffer);
      glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
      glDeleteFramebuffersEXT(1, &frameBuffer);
      maxBufferSize = maxBufferSize / 2;
    };

    if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
      canUseFBOs = false;
    
    // Now that we know the dimensions we can set the viewport.
    glViewport(0, 0, bufferWidth, bufferHeight);

    if (canUseFBOs)
    {
      float originalZoom = FZoom;
      FZoom = zoom;
      notifyZoomChange();
      validate();

      glPixelStoref(GL_PACK_ROW_LENGTH, width);

      // Render the entire workspace by splitting it in pieces of the render buffer size.
      for (float y = 0; y < height; y += bufferHeight)
      {
        for (float x = 0; x < width; x += bufferWidth)
        {
          int currentWidth = (width - x < bufferWidth) ? int(width - x) : bufferWidth;
          int currentHeight = (height - y < bufferHeight) ? int(height - y) : bufferHeight;
          
          float verticalOffset = height - y - currentHeight;
          int offset = int(verticalOffset * width + x) * 4; // * 4 because we have 4 components per pixel.

          glMatrixMode(GL_PROJECTION);
          glLoadIdentity();

          // Set an orthogonal (parallel) projection, mapping the current view area in the entire workspace to the 
          // viewport coordinates.
          glOrtho((x + left) / zoom, (x + left + bufferWidth) / zoom, 
            (y + top + bufferHeight) / zoom, (y + top) / zoom, FNearPlane, FFarPlane);
          glMatrixMode(GL_MODELVIEW);
          glLoadIdentity();
          canvas()->clearBuffers();

          // Transform viewport coordinates into view space. They are used for occlusion culling.
          TBoundingBox visibleBounds;
          visibleBounds.upper.x = (x + left) / zoom;
          visibleBounds.upper.y = (y + top) / zoom;
          visibleBounds.lower.x = (x + left + currentWidth) / zoom;
          visibleBounds.lower.y = (y + top + currentHeight) / zoom;

          internalRender(content, visibleBounds);
          glReadPixels(0, bufferHeight - currentHeight, currentWidth, currentHeight, glFormat, GL_UNSIGNED_BYTE, memory + offset);
        };
      };
      glPixelStoref(GL_PACK_ROW_LENGTH, 0);
      FZoom = originalZoom;
      notifyZoomChange();
    };

    // Release frame buffer binding to enable normal rendering again.
    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
    glDeleteRenderbuffersEXT(1, &renderBuffer);
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
    glDeleteFramebuffersEXT(1, &frameBuffer);

    glViewport(FViewport.left, FViewport.top, FViewport.width, FViewport.height);

    // Return the actually used bounds to the caller. Particularly width and height are important to
    // handle following interpretation of the rendered data.
    bounds.left = (int) left;
    bounds.top = (int) top;
    bounds.width = (int) width;
    bounds.height = (int) height;
  };

  // Fallback in case either the frame buffer objects extension is not supported or could not properly be set up.
  if (!canUseFBOs)
  {
    // Determine area to render. We can here only render what's currently in the viewport, so limit the area to that.
    // For validation we need the coords in window space, though.
    int left;
    int top;
    viewToWindow(TVertex(bounds.left, bounds.top, 0), left, top);

    int right;
    int bottom;

    int width = bounds.width;
    if (width < 0)
      width = (int) FWorkspaceWidth;
    int height = bounds.height;
    if (height < 0)
      height = (int) FWorkspaceHeight;
    viewToWindow(TVertex(bounds.left + width, bounds.top + height, 0), right, bottom);
    if (left < 0)
      left = 0;
    if (top < 0)
      top = 0;
    if (right > FViewport.width)
      right = FViewport.width;
    if (bottom > FViewport.height)
      bottom = FViewport.height;
    width = right - left;
    height = bottom - top;

    glPixelStorei(GL_PACK_ROW_LENGTH, width);
    glReadBuffer(GL_BACK);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glOrtho(-FOffsetX / zoom - FJitter, (FViewport.width - FOffsetX) / zoom - FJitter, 
      (FViewport.height - FOffsetY) / zoom - FJitter, -FOffsetY / zoom - FJitter, FNearPlane, FFarPlane);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    canvas()->clearBuffers();

    // Transform viewport coordinates into view space. They are used for occlusion culling.
    TBoundingBox visibleBounds;
    visibleBounds.upper.x = -FOffsetX / zoom;
    visibleBounds.upper.y = -FOffsetY / zoom;
    visibleBounds.lower.x = (FViewport.width - FOffsetX) / zoom;
    visibleBounds.lower.y = (FViewport.height - FOffsetY) / zoom;

    internalRender(content, visibleBounds);

    glReadPixels(left, FViewport.height - top - height, width, height, glFormat, GL_UNSIGNED_BYTE, memory);
    glPixelStoref(GL_PACK_ROW_LENGTH, 0);

    // Return the actually used bounds to the caller. Particularly width and height are important to
    // handle following interpretation of the rendered data.
    bounds.left = left;
    bounds.top = top;
    bounds.width = width;
    bounds.height = height;
  };

  canvas()->checkError();
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Checks if the view's element cache must be created and validated.
 */
void CGCView::updateCache(void)
{
  if (FCache == NULL)
    FCache = new CElementLookupCache(FWorkspaceWidth, FWorkspaceHeight);

  if (!FCache->isValid())
  {
    CFigureInstanceEnumerator* enumerator = getFigureInstanceEnumerator();
    while (enumerator->hasNext())
      FCache->addElement(enumerator->next());
    delete enumerator;

    FCache->validate();
  };
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Validates all associated structures (layers etc.).
 */
void CGCView::validate(void)
{
  if (FDirty)
  {
    FDirty = false;
    FGrid->validate();

    glClearColor(FColor[0], FColor[1], FColor[2], FColor[3]);

    for (CLayers::iterator iterator = FLayers.begin(); iterator != FLayers.end(); ++iterator)
      (*iterator)->validate();

    FPrimitiveLayer->validate();
    FConnectionLayer->validate();
    FFeedbackLayer->validate();
  };
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Adds a layer to the internal list of layers that belong to this view (only if not yet there).
 *
 * @param layer The layer to add.
 */
void CGCView::addLayer(CLayer* layer)
{
  for (CLayers::const_iterator iterator = FLayers.begin(); iterator != FLayers.end(); ++iterator)
    if (*iterator == layer)
      return;

  layer->addListener(&FListener);
  FLayers.push_back(layer);
  
  change(this, GC_CHANGE_VIEW_ADD_LAYER);
  canvas()->refresh();
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Adds the given instance to the current selection if it isn't already.
 *
 * @param instance The figure instance to add to the current selection. It must belong to one of the layers in this view
 *                 otherwise it will not be added.
 */
void CGCView::addToSelection(CFigureInstance* instance)
{
  if (instance != NULL && !instance->selected())
  {
    CLayer* layer = instance->layer();
    if (layer != NULL)
    {
      CLayers::iterator iterator = find(FLayers.begin(), FLayers.end(), layer);
      if (iterator != FLayers.end())
        FFeedbackLayer->addToSelection(instance);
    };
  };
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Removes the current content of this view.
 *
 * @param removeLayers If true then all currently defined (normal) layers are removed from this view (but not the canvas)
 *                     in addition to being cleared.
 */
void CGCView::clearContent(bool removeLayers)
{
  beginUpdate();

  FCache->invalidate();

  for (CLayers::const_iterator iterator = FLayers.begin(); iterator != FLayers.end(); ++iterator)
    (*iterator)->clear();

  if (removeLayers)
    FLayers.clear();

  endUpdate();
  change(this, GC_CHANGE_VIEW_CLEAR);
  canvas()->refresh();
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Removes all primitives.
 */
void CGCView::clearPrimitives(void)
{
  FPrimitiveLayer->clear();
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Removes all currently selected figure instances from the selection set.
 */
void CGCView::clearSelection(void)
{
  FFeedbackLayer->clearSelection();
  change(this, GC_CHANGE_SELECTION_CLEAR);
  canvas()->refresh();
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Sets the new background color of this view.
 *
 * @param red The red color component.
 * @param green The green color component.
 * @param blue The blue color component.
 * @param alpha The transparency component.
 */
void CGCView::color(float red, float green, float blue, float alpha)
{
  FColor[0] = red;
  FColor[1] = green;
  FColor[2] = blue;
  FColor[3] = alpha;

  change(this, GC_CHANGE_VIEW_PROPERTY);
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Sets the new background color of this view
 *
 * @param newColor The new color to use.
 */
void CGCView::colorV(GLfloat* newColor)
{
  FColor[0] = newColor[0];
  FColor[1] = newColor[1];
  FColor[2] = newColor[2];
  FColor[3] = newColor[3];

  change(this, GC_CHANGE_VIEW_PROPERTY);
  canvas()->refresh();
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Tells the caller whether this view contains a reference to the given layer.
 *
 * @param layer The layer to look for.
 * @param True if the layer is referenced in this view otherwise false.
 */
bool CGCView::contains(CLayer* layer)
{
  for (CLayers::const_iterator iterator = FLayers.begin(); iterator != FLayers.end(); ++iterator)
    if (*iterator == layer)
      return true;

  return false;
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Creates a circle primitive and returns it.
 *
 * @param center The center position of the circle.
 * @param outerRadius The overall radius of the circle.
 * @param innerRadius The radius of the circle's hole. Can be 0 to not produce that hole.
 * @param color The fill and line color of the primitive.
 * @param filled True if filled, otherwise only the outline will be drawn.
 * @result The newly created primitive.
 */
CPrimitive* CGCView::createCircle(const TVertex& center, float outerRadius, float innerRadius, GLfloat* color, bool filled)
{
  CPrimitive* result = new CCircle(center, outerRadius, innerRadius, color, filled);
  FPrimitiveLayer->addPrimitive(result);

  return result;
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Proxy function for the connection layer. Read there for a description.
 */
CConnectionInstance* CGCView::createConnectionInstance(CConnection* connection, CFigureInstance* endPoint1, 
                                                       CFigureInstance* endPoint2)
{
  return FConnectionLayer->createInstance(connection, endPoint1, endPoint2);
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Create a floater on the feedback layer. Simply forwards the call there.
 */
CFloater* CGCView::createFloater(const TBoundingBox& bounds, GLfloat* color1, GLfloat* color2, TFloaterDecoration decoration)
{
  return FFeedbackLayer->createFloater(bounds, color1, color2, decoration);
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Creates a line primitive and returns it.
 *
 * @param start The start coordinate of the line.
 * @param end The end coordinate of the line.
 * @param color The fill and line color of the primitive.
 * @param width The line's thickness.
 * @param stipple If not 0 or 0xFFFF then it determinse the pattern used for the line.
 * @result The newly created primitive.
 */
CPrimitive* CGCView::createLine(const TVertex& start, const TVertex& end, GLfloat* color, GLfloat width, GLushort stipple)
{
  CPrimitive* result = new CLine(start, end, color, width, stipple);
  FPrimitiveLayer->addPrimitive(result);

  return result;
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Creates a rectangle primitive and returns it.
 *
 * @param bounds The outer bounds of the primitive.
 * @param color The fill and line color of the primitive.
 * @param filled True if filled, otherwise only the outline will be drawn.
 * @result The newly created primitive.
 */
CPrimitive* CGCView::createRectangle(const TBoundingBox& bounds, GLfloat* color, bool filled)
{
  CPrimitive* result = new CRectangle(bounds, color, filled);
  FPrimitiveLayer->addPrimitive(result);

  return result;
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Determines what action could be executed at the given position. Considered are any feedback state (selection, resize etc.)
 * from the feedback layer as well as actions defined in a figure element. The returned info is usually used to 
 * set an indicator (e.g. the mouse pointer) to a certain state to reflect what is possible at that point.
 *
 * @param modifiers Any combination
 * @param windowX The horizontal target position in window coordinates.
 * @param windowY The vertical target position in window coordinate.
 * @return A flag indicating the possible action state.
 */
TFeedbackInfo CGCView::getFeedbackInfo(TModifiers modifiers, int windowX, int windowY)
{
  TFeedbackInfo result = GC_FI_NONE;

  updateCache();

  if ((FStates & GC_STATE_RESIZING) != 0)
    result = FResizeInfo;
  else
  {
    TVertex viewCoords; 
    windowToView(windowX, windowY, viewCoords);

    CFigureInstance* instance;
    result = FFeedbackLayer->getFeedbackInfo(modifiers, viewCoords, FZoom, &instance);
    
    if (result == GC_FI_NONE)
    {
      CGraphicElement* element = FCache->findElement(viewCoords);
      if (element != NULL)
        result = element->getFeedbackInfo(modifiers, viewCoords);
    };

    if (result == GC_FI_NONE)
      result = FConnectionLayer->getFeedbackInfo(modifiers, viewCoords);
  };

  return result;
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Creates and returns a new figure instance enumerator instance. The caller is responsible for freeing
 * the returned instance.
 *
 * @return The new enumerator.
 */
CFigureInstanceEnumerator* CGCView::getFigureInstanceEnumerator(void)
{
  return new CFigureInstanceEnumerator(this);
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Takes the given coordinates and tries to find a graphic element that was rendered at this position.
 * Positions must be given in view space.
 *
 * @param point The point to check given in view space. If necessary convert window coordinates first by using windowToView.
 * @param singleHit If true then search for hits is stopped after the first one was found.
 * @return A hit result class is returned regardless of the actual number of hits. It must be freed by the caller.
 */
CHitResults* CGCView::getHitTestInfoAt(TVertex point, bool singleHit)
{
  CHitResults* result = new CHitResults();
                      
  updateCache();

  FConnectionLayer->getHitTestInfoAt(result, point, singleHit);
  if (result->count() == 0)
  {
    if (singleHit)
    {
      CGraphicElement* element = FCache->findElement(point);
      if (element != NULL)
        result->addHit(element);
    }
    else
      FCache->findElements(point, result);
  };

  return result;
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Returns the current or last set rubber rect/band bounds.
 *
 * @param box [out] Receives the rubber bounds in view coordinates.
 */
void CGCView::getLastRubberBounds(TBoundingBox& box)
{
  box = FFeedbackLayer->rubberBounds();
  FFeedbackLayer->layerToView(box.lower, box.lower);
  FFeedbackLayer->layerToView(box.upper, box.upper);
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Returns the current origin of the view.
 *
 * @param x [out] The x coordinate of the virtual origin.
 * @param y [out] The y coordinate of the virtual origin.
 * @param z [out] The z coordinate of the virtual origin.
 */
void CGCView::getOrigin(float* x, float* y, float* z)
{
  *x = FOrigin[0];
  *y = FOrigin[1];
  *z = FOrigin[2];
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Retrieves a selection enumerator from the feedback layer and returns it. The caller is reponsible for freeing
 * the returned instance.
 *
 * @param The new enumerator.
 */
CSelectionEnumerator* CGCView::getSelectionEnumerator(void)
{
  return FFeedbackLayer->getSelectionEnumerator();
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Returns the current virtual workspace size within the view.
 *
 * @param width [out] The width of the workspace.
 * @param height [out] The height of the workspace.
 */
void CGCView::getWorkspace(float* width, float* height)
{
  *width = FWorkspaceWidth;
  *height = FWorkspaceHeight;
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Takes care for certain change events that must be considered special.
 */
void CGCView::handleChange(CGCBase* origin, TGCChangeReason reason)
{
  if (!destroying())
  {
    FDirty = true;
    switch (reason)
    {
      case GC_CHANGE_LAYER_ADD_INSTANCE:
      case GC_CHANGE_LAYER_CLEAR:
      case GC_CHANGE_LAYER_REMOVE_INSTANCE:
      case GC_CHANGE_LAYER_ADD_GROUP:
      case GC_CHANGE_LAYER_REMOVE_GROUP:
        {
          if (FCache != NULL)
            FCache->invalidate();

          break;
        };
      default:
        change(origin, reason);
    };
  };
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Called by the viewer to let the current view handle user input with the mouse.
 *
 * @param event The actual event (e.g. mouse down or up).
 * @param button Specifies the mouse button for which this event was triggered.
 * @param modifiers Any combination of TModifier. Specifies further how to handle the input.
 * @param x The horizontal window coordinate of the mouse pointer.
 * @param y The vertical window coordinate of the mouse pointer.
 * @return True if the mouse input was handled in some way, otherwise false.
 */
bool CGCView::handleMouseInput(TMouseEvent event, TMouseButton button, TModifiers modifiers, int x, int y)
{
  bool result = false;

  TVertex point;
  windowToView(x, y, point);

  beginUpdate();
  switch (event)
  {
    case GC_MOUSE_DOWN:
      {
        result = handleMouseDown(button, modifiers, x, y, point);
        break;
      };
    case GC_MOUSE_UP:
      {
        result = handleMouseUp(button, modifiers, x, y, point);
        break;
      };
    case GC_MOUSE_MOVE:
      {
        result = handleMouseMove(modifiers, x, y, point);
        break;
      };
    case GC_MOUSE_DBL_CLICK:
      {
        result = handleMouseDblClick(button, modifiers, x, y, point);
        break;
      };
  };
  endUpdate();

  return result;
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Returns the current jitter value.
 *
 * @return The current jitter value.
 */
float CGCView::jitterGet(void)
{
  return FJitter;
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Jittering the viewport a little bit sometimes improves display quality (e.g. for thin lines). This function
 * sets this value for this view.
 *
 * @param value The new jitter value.
 */
void CGCView::jitterSet(float value)
{
  FJitter = value;
  change(this, GC_CHANGE_VIEW_PROPERTY);
  canvas()->refresh();
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Returns the current horizontal offset.
 *
 * @return The current horizontal offset.
 */
float CGCView::offsetXGet(void)
{
  return FOffsetX;
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Sets the horizontal offset of the view. The offset is value about which the content of the view is moved.
 *
 * @param value The new horizontal offset.
 */
void CGCView::offsetXSet(float value)
{
  FOffsetX = value;
  canvas()->refresh();
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Returns the current vertical offset.
 *
 * @return The curretn vertical offset.
 */
float CGCView::offsetYGet(void)
{
  return FOffsetY;
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Sets the new vertical offset.
 *
 * @param value The new vertical offset.
 */
void CGCView::offsetYSet(float value)
{
  FOffsetY = value;
  canvas()->refresh();
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Tells the caller if this view is currently in overview mode.
 *
 * @result True if the overview mode is active.
 */
bool CGCView::overviewActive(void)
{
  return (FStates & GC_STATE_OVERVIEW) != 0;
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Starts the overview mode for this view, that is, the view is zoomed out to workspace size and a floater is
 * displayed and can be moved to select a new display area.
 *
 * @param animated If true then a zoom animation is created for going into overview mode.
 */
void CGCView::overviewStart(bool animated)
{
  if ((FStates & GC_STATE_OVERVIEW) == 0)
  {
    FStates |= GC_STATE_OVERVIEW;
    FLastZoom = FZoom;

    // Convert the coordinates of the current viewport into view space. This is the area to which we return
    // after the overview mode is finished. It is indicated by a special floater on the feedback layer.
    TBoundingBox exposeBox;
    windowToView(FViewport.left, FViewport.top, exposeBox.upper);
    windowToView(FViewport.left + FViewport.width, FViewport.top + FViewport.height, exposeBox.lower);

    GLfloat color1[] = {0.0f, 0.0f, 0.0f, 0.04f};
    GLfloat color2[] = {0.0f, 0.0f, 0.0f, 0.3f};
    FExposeFloater = FFeedbackLayer->createFloater(exposeBox, color1, color2, GC_FLOATER_DECO_CUT_MARKERS);
    FFeedbackLayer->visibleParts(GC_FBPART_ALL);

    // Compute new zoom factor so that the full work area is displayed).
    float zoomX = FViewport.width / FWorkspaceWidth;
    float zoomY = FViewport.height / FWorkspaceHeight;
    float newZoom = (zoomX < zoomY) ? zoomX : zoomY;

    FLastOffsetX = FOffsetX;
    FLastOffsetY = FOffsetY;

    if (animated)
    {
      canvas()->animationManager()->createViewOffsetAnimation(0, 0, 200, false, this);
      canvas()->animationManager()->createViewZoomAnimation(newZoom, 200, false, this);
    }
    else
    {
      FOffsetX = 0;
      FOffsetY = 0;
      FZoom = newZoom;

      notifyZoomChange();
      canvas()->refresh();
    };
    change(this, GC_CHANGE_VIEW_OVERVIEW_START);
  };
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Stops the overview mode in this view (if it is active) and optionally returns to the last zoom level and display offset.
 *
 * @param returnToZoomed If true then the view is set to the currently stored zoom level and display offset.
 * @param animated If true then a zoom animation is created for leaving overview mode but only if also returnToZoomed is true.
 */
void CGCView::overviewStop(bool returnToZoomed, bool animated)
{
  if ((FStates & GC_STATE_OVERVIEW) != 0)
  {
    FStates &= ~GC_STATE_OVERVIEW;

    delete FExposeFloater;
    FExposeFloater = NULL;
    FFeedbackLayer->visibleParts(GC_FBPART_SELECTION | GC_FBPART_RUBBERRECT | GC_FBPART_RUBBERBAND);

    if (returnToZoomed)
    {
      if (animated)
      {
        canvas()->animationManager()->createViewOffsetAnimation(FLastOffsetX, FLastOffsetY, 200, false, this);
        canvas()->animationManager()->createViewZoomAnimation(FLastZoom, 200, false, this);
      }
      else
      {
        FZoom = FLastZoom;
        FOffsetX = FLastOffsetX;
        FOffsetY = FLastOffsetY;
        notifyZoomChange();
        canvas()->refresh();
      };
    }
    else
      canvas()->refresh();

    change(this, GC_CHANGE_VIEW_OVERVIEW_STOP);
  };
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Retrieves the value of the property given by path. The path syntax is must be something like (here expressed as regex)
 * (container)*(property), where container is a slash and the name of a container class (e.g. layers, figures) and
 * property is the name of a simple property of that container.
 *
 * @param name The name of the property.
 * @param index If the property is a list then this parameter gives the index into that list.
 * @return A description of the property value and, if the property is simple, the actual value.
 */
TGCVariant CGCView::propertyGet(const char* name, unsigned int index)
{
  TGCVariant result;

  switch (getContainerID(name))
  {
    case GC_CONTAINER_UNKNOWN:
      {
        switch (getPropertyID(name))
        {
          case GC_PROPERTY_NAME:
            {
              result = utf16ToUtf8(FName);
              break;
            };
          case GC_PROPERTY_DESCRIPTION:
            {
              result = "A view comprising a set of layers.";
              break;
            };
          case GC_PROPERTY_OWNER:
            {
              result = canvas();
              break;
            };
          case GC_PROPERTY_COLOR:
            {
              result = colorToString(FColor);
              break;
            };
          case GC_PROPERTY_ZOOM:
            {
              result = FZoom;
              break;
            };
          case GC_PROPERTY_X:
            {
              result = FOffsetX;
              break;
			};
          case GC_PROPERTY_Y:
            {
              result = FOffsetY;
              break;
            };
          case GC_PROPERTY_JITTER:
            {
              result = FJitter;
              break;
            };
        };
        break;
      };
    case GC_CONTAINER_LAYERS:
      {
        if (index < FLayers.size())
          result = (CGCBase*) FLayers[index];
        break;
      };
  };

  return result;
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Sets the value of the given property, which must be a simple property.
 *
 * @param name The name of the property.
 * @param index If the property is a list then this parameter gives the index into that list.
 * @param value The new value of the property. Automatic conversion is performed where possible.
 */
void CGCView::propertySet(const char* name, unsigned int index, TGCVariant value)
{
  switch (getPropertyID(name))
  {
	case GC_PROPERTY_COLOR:
	  {
        stringToColor(value, FColor);
        FColor[3] = 1;
        change(this, GC_CHANGE_VIEW_PROPERTY);
        break;
      };
    case GC_PROPERTY_ZOOM:
      {
        FZoom = value;
        change(this, GC_CHANGE_VIEW_ZOOM);
        break;
      };
    case GC_PROPERTY_X:
      {
        FOffsetX = value;
        change(this, GC_CHANGE_VIEW_PROPERTY);
        break;
      };
    case GC_PROPERTY_Y:
      {
        FOffsetY = value;
        change(this, GC_CHANGE_VIEW_PROPERTY);
        break;
      };
    case GC_PROPERTY_JITTER:
      {
        FJitter = value;
        change(this, GC_CHANGE_VIEW_PROPERTY);
        break;
      };
  };
  canvas()->refresh();
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Removes the given instance from the current selection if it selected.
 *
 * @param instance The figure instance to remove. It must belong to one of the layers in this view
 *                 otherwise it will not be removed.
 */
void CGCView::removeFromSelection(CFigureInstance* instance)
{
  if (instance != NULL && !instance->selected())
  {
    CLayer* layer = instance->layer();
    if (layer != NULL)
    {
      CLayers::iterator iterator = find(FLayers.begin(), FLayers.end(), layer);
      if (iterator != FLayers.end())
        FFeedbackLayer->removeFromSelection(instance);
    };
  };
}

//----------------------------------------------------------------------------------------------------------------------

/**
 *  Removes the given layer from the list of layers that comprise this view.
 *
 * @param layer The layer to remove.
 */
void CGCView::removeLayer(CLayer* layer)
{
  if (!destroying())
  {
    for (CLayers::iterator iterator = FLayers.begin(); iterator != FLayers.end(); ++iterator)
      if (*iterator == layer)
      {
        if (!layer->destroying())
          layer->removeListener(&FListener);
        FLayers.erase(iterator);
        if (FCache)
          FCache->invalidate();

        break;
      };
  };
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Starts the rubber band mode.
 *
 * @param style The visual style of the rubber band.
 * @param coords The start coordinates of the band in view space.
 */
void CGCView::rubberBandStart(TRubberBandStyle style, const TVertex& coords)
{
  // Implizitely stops any active rubber band.
  FFeedbackLayer->rubberBandStart(style, coords);
  FStates |= GC_STATE_RUBBER_BAND;
  change(this, GC_CHANGE_VIEW_RUBBERBAND_START);
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Stops the current rubber retangle action if there is one.
 */
void CGCView::rubberBandStop(void)
{
  FStates &= ~GC_STATE_RUBBER_BAND;
  FFeedbackLayer->rubberBandStop();
  change(this, GC_CHANGE_VIEW_RUBBERBAND_STOP);
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Starts the rubber rectangle mode.
 *
 * @param style The visual style of the rubber rectangle.
 * @param coords The start coordinates of the rectangle in view space.
 * @param removeSelection If true then the current selection is cleared.
 */
void CGCView::rubberRectStart(TRubberRectStyle style, const TVertex& coords, bool removeSelection)
{
  // Implicitely stops any active rubber rectangle.
  FFeedbackLayer->rubberRectStart(style, coords, removeSelection);
  FStates |= GC_STATE_RUBBER_RECTANGLE;
  change(this, GC_CHANGE_VIEW_RUBBERRECT_START);
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Stops the current rubber retangle action if there is one.
 */
void CGCView::rubberRectStop(void)
{
  FStates &= ~GC_STATE_RUBBER_RECTANGLE;
  FFeedbackLayer->rubberRectStop();
  change(this, GC_CHANGE_VIEW_RUBBERRECT_STOP);
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Returns the number of currently selected items.
 *
 * @result The number of currently selected items.
 */
int CGCView::selectionCount(void)
{
  return FFeedbackLayer->selectionCount();
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Sets the virtual center of the view.
 *
 * @param x The x coordinate of the new center.
 * @param y The x coordinate of the new center.
 * @param z The x coordinate of the new center.
 */
void CGCView::setOrigin(float x, float y, float z)
{
  FOrigin[0] = x;
  FOrigin[1] = y;
  FOrigin[2] = z;

  canvas()->refresh();
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Sets a new size for the current virtual workspace in this view.
 *
 * @param width The new workspace width.
 * @param height The new workspace height.
 */
void CGCView::workspaceSet(const float width, const float height)
{
  FWorkspaceWidth = width;
  FWorkspaceHeight = height;

  FConnectionLayer->workspaceChanged();

  change(this, GC_CHANGE_VIEW_WORKSPACE);

  // The workspace size may also directly influence the cache.
  #if USE_BSP_TREE
    delete FCache;
    FCache = NULL;
  #endif
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Gets the workspace size in this view.
 *
 * @param width The workspace width.
 * @param height The workspace height.
 */
void CGCView::workspaceGet(float &width, float &height)
{
  width= FWorkspaceWidth;
  height= FWorkspaceHeight;
}


//----------------------------------------------------------------------------------------------------------------------

/**
 * Hides or shows the current selection.
 *
 * @param visible If true the selection is shown, otherwise not.
 */
void CGCView::showSelection(bool visible)
{
  FFeedbackLayer->visibleSet(visible);
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Sets the new viewport for this view.
 *
 * @param NewViewport The new viewport to be used.
 */
void CGCView::viewportSet(const TGCViewport& newViewport)
{
  FViewport = newViewport;
  glViewport(FViewport.left, FViewport.top, FViewport.width, FViewport.height);
  change(this, GC_CHANGE_VIEW_PROPERTY);
  canvas()->refresh();
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Converts the given view coordinates into window coordinates
 * @note This function resets the current projection and view matrices.
 *
 * @param coords A vertex containing view coordiantes.
 * @param x [out] Horizontal window coordinate in pixels.
 * @param y [out] Vertical window coordinate in pixels.
 */
void CGCView::viewToWindow(const TVertex& coords, int& x, int& y)
{
  applyTransformations();

  GLint viewport[4];
  glGetIntegerv(GL_VIEWPORT, viewport);

  GLdouble modelviewMatrix[16];
  glGetDoublev(GL_MODELVIEW_MATRIX, modelviewMatrix);

  GLdouble projectionMatrix[16];
  glGetDoublev(GL_PROJECTION_MATRIX, projectionMatrix);

  double winX, winY, winZ;
  gluProject(coords.x, coords.y, coords.z, modelviewMatrix, projectionMatrix, viewport, &winX, &winY, &winZ);

  x = int(winX);
  y = int(viewport[3] - winY); 
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Converts the given window (viewer) coordinates into local (view) space.
 * @note This function resets the current projection and view matrices.
 *
 * @param x Horizontal window coordinate in pixels.
 * @param y Vertical window coordinate in pixels.
 * @param coords [out] A vertex getting the local coordiantes.
 */
void CGCView::windowToView(int x, int y, TVertex& coords)
{
  applyTransformations();

  GLint viewport[4];
  glGetIntegerv(GL_VIEWPORT, viewport);

  GLdouble modelviewMatrix[16];
  glGetDoublev(GL_MODELVIEW_MATRIX, modelviewMatrix);

  GLdouble projectionMatrix[16];
  glGetDoublev(GL_PROJECTION_MATRIX, projectionMatrix);

  double localX, localY, localZ;
  gluUnProject(x, viewport[3] - y, 0, modelviewMatrix, projectionMatrix, viewport, &localX, &localY, &localZ);

  coords = TVertex(localX - FOrigin[0], localY - FOrigin[1], localZ - FOrigin[2]);
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Returns the current zoom factor.
 *
 * @return The current zoom factor.
 */
float CGCView::zoomGet(void)
{
  return FZoom;
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Sets a new zoom factor.
 *
 * @param value The new zoom factor.
 */
void CGCView::zoomSet(float value)
{
  FZoom = value;

  notifyZoomChange();
  change(this, GC_CHANGE_VIEW_ZOOM);
  canvas()->refresh();
}

//----------------------------------------------------------------------------------------------------------------------