File: myx_gc_feedback.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 (1560 lines) | stat: -rw-r--r-- 47,611 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
/* 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_feedback.cpp 
 * @brief Implementation of the feedback layer and its related classes and structures.
 * 
 */

#include "myx_gc_feedback.h"
#include "myx_gc_canvas.h"

// Fixed floater decoration data.
const GLfloat romanNumber1[] = {
  4.0f, // Number of vertices
  4.5f, 0, 5.438f, 0, 5.438f, 10.0f, 4.5f, 10.0f,
  0     // Number of vertices => 0 == end marker
};
const GLfloat romanNumber2[] = {
  4.0f,
  3.5f, 0, 4.438f, 0, 4.438f, 10.0f, 3.5f, 10.0f, 
  4.0f,
  5.5f, 0, 6.438f, 0, 6.438f, 10.0f, 5.5f, 10.0f,
  0
};
const GLfloat romanNumber3[] = {
  4.0f,
  2.5f, 0, 3.438f, 0, 3.438f, 10.0f, 2.5f, 10.0f, 
  4.0f,
  4.5f, 0, 5.438f, 0, 5.438f, 10.0f, 4.5f, 10.0f,
  4.0f,
  6.5f, 0, 7.438f, 0, 7.438f, 10.0f, 6.5f, 10.0f,
  0
};
const GLfloat romanNumber4[] = {
  4.0f,
  8.59f, 0, 6.6f, 10.0f, 6.1f, 7.486f, 7.59f, 0, 
  5.0f,
  6.6f, 10.0f, 5.6f, 10.0f, 3.611f, 0, 4.611f, 0, 6.1f, 7.486f,  
  4.0f,
  1.6f, 0.0f, 2.538f, 0.0f, 2.538f, 10.0f, 1.6f, 10.0f,
  0
};
const GLfloat romanNumber5[] = {
  4.0f,
  6.479f, 0, 4.989f, 7.486f, 5.489f, 10.0f, 7.479f, 0,
  5.0f,
  4.989f, 7.486f, 3.5f, 0, 2.5f, 0, 4.489f, 10.0f, 5.489f, 10.0f,
  0
};
const GLfloat romanNumber6[] = {
  4.0f,
  5.479f, 0, 3.989f, 7.486f, 4.489f, 10.0f, 6.479f, 0,
  5.0f,
  3.989f, 7.486f, 2.5f, 0, 1.5f, 0, 3.489f, 10.0f, 4.489f, 10.0f,
  4.0f,
  7.5f, 0.0f, 8.438f, 0.0f, 8.438f, 10.0f, 7.5f, 10.0f,
  0
};
const GLfloat romanNumber7[] = {
  4.0f,
  4.479f, 0, 2.989f, 7.486f, 3.489f, 10.0f, 5.479f, 0,
  5.0f,
  2.989f, 7.486f, 1.5f, 0, 0.5f, 0, 2.489f, 10.0f, 3.489f, 10.0f,
  4.0f,
  6.563f, 0.0f, 7.501f, 0.0f, 7.501f, 10.0f, 6.563f, 10.0f,
  4.0f,
  8.563f, 0.0f, 9.501f, 0.0f, 9.501f, 10.0f, 8.563f, 10.0f,
  0
};
const GLfloat romanNumber8[] = {
  4.0f,
  3.979f, 0, 2.489f, 7.486f, 2.989f, 10.0f, 4.979f, 0,
  5.0f,
  2.489f, 7.486f, 1.0f, 0, 0, 0, 1.989f, 10.0f, 2.989f, 10.0f,
  4.0f,
  5.25f, 0.0f, 6.188f, 0.0f, 6.188f, 10.0f, 5.25f, 10.0f,
  4.0f,
  7.188f, 0.0f, 8.126f, 0.0f, 8.126f, 10.0f, 7.188f, 10.0f,
  4.0f,
  9.0f, 0.0f, 9.938f, 0.0f, 9.938f, 10.0f, 9.0f, 10.0f,
  0
};
const GLfloat romanNumber9[] = {
  4.0f,
  8.5f, 10.0f, 4.5f, 0, 3.5f, 0, 7.5f, 10.0f,
  4.0f,
  6.5f, 5.0f, 8.5f, 0, 7.5f, 0, 6.0f, 3.75f,
  4.0f,
  5.5f, 5.0f, 3.5f, 10.0f, 4.5f, 10.0f, 6.0f, 6.25f,
  4.0f,
  1.5f, 0.0f, 2.5f, 0.0f, 2.5f, 10.0f, 1.5f, 10.0f,
  0
};
const GLfloat romanNumber10[] = {
  4.0f,
  7.5f, 10.0f, 3.5f, 0, 2.5f, 0, 6.5f, 10.0f,
  4.0f,
  5.5f, 5.0f, 7.5f, 0, 6.5f, 0, 5.0f, 3.75f, 
  4.0f,
  4.5f, 5.0f, 2.5, 10.0f, 3.5f, 10.0f, 5.0f, 6.25f, 6.5f, 10.0f,
  0
};
const GLfloat* romanNumbers[] = {
  romanNumber1, romanNumber2, romanNumber3, romanNumber4, romanNumber5, 
  romanNumber6, romanNumber7, romanNumber8, romanNumber9, romanNumber10 
};

//----------------- CFloater -------------------------------------------------------------------------------------------

CFloater::CFloater(CGenericCanvas* canvas, const TBoundingBox& bounds, GLfloat* color1, GLfloat* color2, 
                   TFloaterDecoration decoration):CGraphicElement(canvas)
{
  _className = "CFloater";
  FDisplayList = 0;

  FTranslation[0] = bounds.upper.x;
  FTranslation[1] = bounds.upper.y;
  FTranslation[2] = bounds.upper.z;
  FWidth = bounds.lower.x - bounds.upper.x;
  FHeight = bounds.lower.y - bounds.upper.y;
  FDecoration = decoration;
  FColor1[0] = color1[0];
  FColor1[1] = color1[1];
  FColor1[2] = color1[2];
  FColor1[3] = color1[3];
  FColor2[0] = color2[0];
  FColor2[1] = color2[1];
  FColor2[2] = color2[2];
  FColor2[3] = color2[3];
}

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

CFloater::~CFloater(void)
{
  if (FDisplayList != 0)
    glDeleteLists(FDisplayList, 1);
}

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

/**
 * Floaters have their own feedback flag, which is returned if the given coordinates are with the floater's bounds.
 *
 * @param modifiers Any combination of Alt, Control and Shift key.
 * @param coords The coordinates to check against the floater's bounds in layer space.
 * @result A flag telling either the coordinates are within the floater's bounds or not.
 */
TFeedbackInfo CFloater::getFeedbackInfo(int modifiers, const TVertex& coords)
{
  if (containsPoint(coords.x, coords.y))
    return GC_FI_FLOATER;
  else
    return GC_FI_NONE;
}


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

/**
 * Moves the floater to a new position.
 *
 * @param dX The horizontal amount to move.
 * @param d> The vertical amount to move.
 */
void CFloater::move(float dX, float dY)
{
  FTranslation[0] += dX;
  FTranslation[1] += dY;
  makeDirty();
  canvas()->refresh();
}

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

/**
 * 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 to return.
 * @param index If the property is a list then this is the index into that list.
 * @return A description of the property value and, if the property is simple, the actual value.
 */
TGCVariant CFloater::propertyGet(const char* name, unsigned int index)
{
  TGCVariant result;

  switch (getContainerID(name))
  {
    case GC_CONTAINER_UNKNOWN:
      {
        switch (getPropertyID(name))
        {
          case GC_PROPERTY_NAME:
            {
              result = "";
              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 is the index into that list.
 * @param Value The new value of the property. Automatic conversion is performed where possible.
 */
void CFloater::propertySet(const char* name, unsigned int index, TGCVariant value)
{
  switch (getContainerID(name))
  {
    case GC_CONTAINER_UNKNOWN:
      {
        switch (getPropertyID(name))
        {
          case GC_PROPERTY_NAME:
            {
              // place holder
              break;
            };
        };
        break;
      };
  };
}

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

/**
 * Renders this floater.
 */
void CFloater::render(void)
{
  validate();

  glTranslatef(FTranslation[0], FTranslation[1], FTranslation[2]);
  glCallList(FDisplayList);
}

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

/**
 * Validates this floater by creating its display list.
 */
void CFloater::validate(void)
{
  if (dirty())
  {
    CGraphicElement::validate();

    boundsNew();
    boundsAdd(TVertex(FTranslation[0], FTranslation[1], 0));
    boundsAdd(TVertex(FTranslation[0] + FWidth, FTranslation[1] + FHeight, 0));
    boundsFinished();

    if (FDisplayList == 0)
      FDisplayList = glGenLists(1);
    glNewList(FDisplayList, GL_COMPILE);

    if (canvas()->supportsExtension(GC_OE_MULTISAMPLING))
      glDisable(GL_MULTISAMPLE_ARB);

    // Except for the checker board style the floater is always filled.
    if (FDecoration != GC_FLOATER_DECO_CHECKER_BOARD)
    {
      // The interior.
      glColor4fv(FColor1);
      glRectf(0, 0, FWidth, FHeight);
    };

    switch (FDecoration)
    {
      case GC_FLOATER_DECO_CHECKER_BOARD:
        {
          glLineWidth(1);
          float increment = 100;

          bool oddRow = false; 
          float y1 = 0;
          float y2;
          while (y1 < FHeight)
          {
            y2 = y1 + increment;
            if (y2 > FHeight)
              y2 = FHeight;
            bool oddCell = oddRow;
            float x1 = 0;
            float x2;
            while (x1 < FWidth)
            {
              x2 = x1 + increment;
              if (x2 > FWidth)
                x2 = FWidth;
              if (oddCell)
                glColor4fv(FColor1);
              else
                glColor4fv(FColor2);
              glBegin(GL_POLYGON);
                glVertex2f(x1, y1);
                glVertex2f(x2, y1);
                glVertex2f(x2, y2);
                glVertex2f(x1, y2);
              glEnd();

              oddCell = !oddCell;
              x1 = x2;
            };
            oddRow = !oddRow;
            y1 = y2;
          };

          break;
        };
      case GC_FLOATER_DECO_CUT_MARKERS:
        {
          // The transparent rectangle is decorated with 8 short lines two of them at each corner.
          glColor4fv(FColor2);
          glBegin(GL_LINES);
            glVertex2f(-120,    0);
            glVertex2f( -40,    0);
            glVertex2f(   0,  -40);
            glVertex2f(   0, -120);

            glVertex2f(FWidth,        -40);
            glVertex2f(FWidth,       -120);
            glVertex2f(FWidth +  40,    0);
            glVertex2f(FWidth + 120,    0);

            glVertex2f(FWidth +  40, FHeight);
            glVertex2f(FWidth + 120, FHeight);
            glVertex2f(FWidth,       FHeight +  40);
            glVertex2f(FWidth,       FHeight + 120);

            glVertex2f(-120, FHeight);
            glVertex2f( -40, FHeight);
            glVertex2f(   0, FHeight +  40);
            glVertex2f(   0, FHeight + 120);
          glEnd();

          break;
        };
      case GC_FLOATER_DECO_ROMAN_1:
      case GC_FLOATER_DECO_ROMAN_2:
      case GC_FLOATER_DECO_ROMAN_3:
      case GC_FLOATER_DECO_ROMAN_4:
      case GC_FLOATER_DECO_ROMAN_5:
      case GC_FLOATER_DECO_ROMAN_6:
      case GC_FLOATER_DECO_ROMAN_7:
      case GC_FLOATER_DECO_ROMAN_8:
      case GC_FLOATER_DECO_ROMAN_9:
      case GC_FLOATER_DECO_ROMAN_10:
        {
          glPushMatrix();

          // Scale the number so that it fills the entire floater and center it.
          // All numbers are specified in the range (0, 0) - (10, 10).
          float scaleFactor = (FWidth < FHeight ? FWidth : FHeight) / 12; // 12, to have a small border.
          float dX = (FWidth - 10 * scaleFactor) / 2;
          float dY = (FHeight - 10 * scaleFactor) / 2;
          glTranslatef(dX, dY, 0);
          glScalef(scaleFactor, scaleFactor, 1);

          glColor4fv(FColor2);
          int index = 0;
          const GLfloat* vertices = romanNumbers[FDecoration - GC_FLOATER_DECO_ROMAN_1];
          int count = (int) vertices[index++];
          while (count > 0)
          {
            glBegin(GL_POLYGON);
            while (count-- > 0)
            {
              glVertex2f(vertices[index], vertices[index + 1]);
              index += 2;
            };
            glEnd();

            count = (int) vertices[index++];
          };
          glPopMatrix();

          break;
        };
    };

    // The border, always drawn.
    glColor4fv(FColor2);
    glBegin(GL_LINE_LOOP);
      glVertex2f(0, 0);
      glVertex2f(FWidth, 0); 
      glVertex2f(FWidth, FHeight);
      glVertex2f(0, FHeight); 
    glEnd();

    if (canvas()->supportsExtension(GC_OE_MULTISAMPLING))
      glEnable(GL_MULTISAMPLE_ARB);

    glEndList();

    canvas()->checkError();
  };
}

//----------------- CSelectionEnumerator -------------------------------------------------------------------------------

/**
 * Constructor of the enumerator class.
 *
 * @param layer The feedback layer which contains the selection.
 */
CSelectionEnumerator::CSelectionEnumerator(CFeedbackLayer* layer)
{
  FLayer = layer;
  reset();
}

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

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

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

/**
 * Returns the next figure instance in the sequence.
 *
 * @return The next figure instance.
 */
CFigureInstance* CSelectionEnumerator::next(void)
{
  CFigureInstance* result = FIterator->first;

  // Advance to next instance.
  ++FIterator;

  return result;
}

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

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

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

/**
 * Resets the enumerator to the first figure instance in the canvas.
 */
void CSelectionEnumerator::reset(void)
{
  FIterator = FLayer->FSelection.begin();
}

//----------------- CFeedbackInstanceListener --------------------------------------------------------------------------

void CFeedbackLayer::CFeedbackInstanceListener::onChange(CGCBase* sender, CGCBase* origin, TGCChangeReason reason)
{
  layer->invalidateBounds((CFigureInstance*) sender);
}

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

void CFeedbackLayer::CFeedbackInstanceListener::onDestroy(CGCBase* object)
{
  if (object->classIs("CFloater"))
    layer->removeFloater((CFloater*) object);
  else
    layer->removeFromSelection((CFigureInstance*) object);
}

//----------------- CFeedbackLayer ------------------------------------------------------------------------------------

CFeedbackLayer::CFeedbackLayer(CGCView* view): CLayer("feedback", view->canvas())
{
  _className = "CFeedbackLayer";
  FView = view;
  FHandleSize = 5;
  FRubberRectDisplayList = 0;
  FStates = 0;
  FSelectionDecoration = 0;
  FListener.layer = this;
  FVisibleParts = GC_FBPART_ALL;
}

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

CFeedbackLayer::~CFeedbackLayer(void)
{
  clearFloaters();
  clearSelection();
  if (FSelectionDecoration != 0)
    glDeleteLists(FSelectionDecoration, 1);
  if (FRubberRectDisplayList != 0)
    glDeleteLists(FRubberRectDisplayList, 1);
}

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

/**
 * Creates the display list for the selection decoration, which is shared among all selection entries.
 */
void CFeedbackLayer::createSelectionDecoration(void)
{
  GLubyte Color[4];
  if (!colorByName("Highlight", Color))
  {
    // If the system's highlight color could not be found then use a default one.
    Color[0] = 64;
    Color[1] = 64;
    Color[2] = 128;
  };

  FSelectionDecoration = glGenLists(1);
  glNewList(FSelectionDecoration, GL_COMPILE);

  // The interior.
  /*
  Color[3] = 30;
  glColor4ubv(Color);
  glRectd(0, 1, 1, 0);
  */
  glPushAttrib(GL_LINE_BIT);
  glDisable(GL_LINE_SMOOTH);

  // The border.
  Color[3] = 200;
  glColor4ubv(Color);
  glBegin(GL_LINE_LOOP);
    glVertex2d(0, 1);
    glVertex2d(1, 1); 
    glVertex2d(1, 0);
    glVertex2d(0, 0); 
  glEnd();

  // The handles.
  Color[3] = 100;
  glColor4ubv(Color);
  glPointSize(FHandleSize);
  glBegin(GL_POINTS);
    glVertex2d(0, 1);
    glVertex2d(0.5, 1);
    glVertex2d(1, 1);

    glVertex2d(0, 0.5);
    glVertex2d(1, 0.5);

    glVertex2d(0, 0);
    glVertex2d(0.5, 0);
    glVertex2d(1, 0);
  glEnd();

  glPopAttrib();

  glEndList();

  checkError();
}

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

/**
 * Helper method to add a figure instance to the selection list. No change event is triggered.
 *
 * @param instance The instance to add.
 * @return If the instance was added (because it wasn't already there) then true is returned, otherwise false.
 */
bool CFeedbackLayer::internalAddToSelection(CFigureInstance* instance)
{
  bool result = !instance->FSelected;
  if (result)
  {
    instance->FSelected = true;
    TSelectionEntry* entry = new TSelectionEntry;
    entry->instance = instance;
    entry->dirty = true;
    FSelection[instance] = entry;

    instance->addListener(&FListener);
  };

  return result;
}

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

/**
 * Helper method to remove a figure instance from the selection list. No change event is triggered.
 *
 * @param instance The instance to add.
 */
void CFeedbackLayer::internalRemoveFromSelection(CFigureInstance* instance)
{
  if (instance->FSelected)
  {
    if (!instance->destroying())
    {
      instance->FSelected = false;
      instance->removeListener(&FListener);
    };
    CSelectionIterator iterator = FSelection.find(instance);
    if (iterator != FSelection.end())
    {
      delete iterator->second;
      FSelection.erase(iterator);
    };
  };
}

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

/**
 * Renders the decorations for all figure instances that are currently selected.
 */
void CFeedbackLayer::renderLayerContent(TBoundingBox bounds)
{
  // Selection decorations.
  if ((GC_FBPART_SELECTION & FVisibleParts) != 0)
  {
    for (CSelectionIterator iterator = FSelection.begin(); iterator != FSelection.end(); ++iterator)
    {
      TSelectionEntry* Entry = iterator->second;
      TBoundingBox localBounds;
      viewToLayer(bounds.lower, localBounds.lower);
      viewToLayer(bounds.upper, localBounds.upper);
      if (boundsIntersect(Entry->bounds, localBounds))
      {
        glPushMatrix();
        glTranslatef(Entry->bounds.upper.x, Entry->bounds.upper.y, 1);
        glScalef(Entry->bounds.lower.x - Entry->bounds.upper.x, Entry->bounds.lower.y - Entry->bounds.upper.y, 1);
        glCallList(FSelectionDecoration);
        glPopMatrix();
      };
    };
  };

  // Floaters.
  if ((GC_FBPART_FLOATERS & FVisibleParts) != 0)
  {
    for (CFloaters::const_iterator iterator = FFloaters.begin(); iterator != FFloaters.end(); ++iterator)
    {
      glPushMatrix();
      (*iterator)->render();
      glPopMatrix();
    };
  };

  // Rubber rectangle.
  if ((GC_FBPART_RUBBERRECT & FVisibleParts) != 0 && (FStates & GC_FBSTATE_RUBBERRECT) != 0)
  {
    // In order to avoid backface culling if the coordinates of the rubber band do not form a counter-clock-wise
    // face we simply disable face culling for the moment.
    glPushAttrib(GL_ENABLE_BIT);
    glDisable(GL_CULL_FACE);
    glTranslatef(FRubberBounds.upper.x, FRubberBounds.upper.y, 0);
    glScalef(FRubberBounds.lower.x - FRubberBounds.upper.x, FRubberBounds.lower.y - FRubberBounds.upper.y, 1);
    glCallList(FRubberRectDisplayList);
    glPopAttrib();
  };

  // Rubber band.
  if ((GC_FBPART_RUBBERBAND & FVisibleParts) != 0 && (FStates & GC_FBSTATE_RUBBERBAND) != 0)
  {
    glTranslatef(FRubberBounds.upper.x, FRubberBounds.upper.y, 0);
    glScalef(FRubberBounds.lower.x - FRubberBounds.upper.x, FRubberBounds.lower.y - FRubberBounds.upper.y, 1);
    glCallList(FRubberBandDisplayList);
  };
}

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

/**
 * Returns the number of currently selected items.
 *
 * @result The number of currently selected items.
 */
int CFeedbackLayer::selectionCount(void)
{
  return (int) FSelection.size();
}

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

/**
 * Creates display lists for all invalid decorations.
 */
void CFeedbackLayer::validateLayerContent(void)
{
  if (FSelectionDecoration == 0)
    createSelectionDecoration();

  for (CSelectionIterator iterator = FSelection.begin(); iterator != FSelection.end(); iterator++)
    if (iterator->second->dirty)
    {
      iterator->second->dirty = false;

      // Update the bounding box of the figure instance.
      TBoundingBox bounds = iterator->second->instance->bounds();

      // Give a small border around the figure instance.
      bounds.upper.x -= 1;
      bounds.upper.y -= 1;
      bounds.lower.x += 1;
      bounds.lower.y += 1;
      iterator->second->bounds = bounds;
    };
}

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

/**
 * Adds the given figure instance to the current selection.
 *
 * @param instance The instance to be added to the selection. If it is already in the set it won't be added again.
 */
void CFeedbackLayer::addToSelection(CFigureInstance* instance)
{
  if (internalAddToSelection(instance))
  {
    change(instance, GC_CHANGE_SELECTION_ADD);
    if (!updating())
      makeDirty();
  };
}

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

/**
 * Removes all floaters.
 */
void CFeedbackLayer::clearFloaters(void)
{
  if (FFloaters.size() > 0)
  {
    for (CFloaters::iterator iterator = FFloaters.begin(); iterator != FFloaters.end(); ++iterator)
    {
      CFloater* floater = *iterator;
      floater->removeListener(&FListener);
      delete floater;
    };

    FFloaters.clear();
    if (!updating())
      canvas()->refresh();
  };
}

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

/**
 * Removes all figure instances from the selection set, making it empty.
 */
void CFeedbackLayer::clearSelection(void)
{
  if (FSelection.size() > 0)
  {
    for (CSelectionIterator iterator = FSelection.begin(); iterator != FSelection.end(); ++iterator)
    {
      CFigureInstance* instance = iterator->second->instance;
      instance->removeListener(&FListener);
      instance->FSelected = false;
      
      delete iterator->second;
    };

    FSelection.clear();
    change(NULL, GC_CHANGE_SELECTION_CLEAR);
    if (!updating())
      makeDirty();
  };
}

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

/**
 * Creates a new floater object. The new floater is automatically added to the internal floaters list.
 *
 * @param bounds The bounds of the new floater.
 * @param color1 The normal interior color.
 * @param color2 The border color (also used as alternating color in the checker board).
 * @param decoration The style or additional decoration of the floater.
 * @return The reference of the new floater.
 */
CFloater* CFeedbackLayer::createFloater(const TBoundingBox& bounds, GLfloat* color1, GLfloat* color2, 
                                        TFloaterDecoration decoration)
{
  CFloater* result = new CFloater(canvas(), bounds, color1, color2, decoration);
  result->addListener(&FListener);
  FFloaters.push_back(result);
  makeDirty();

  return result;
}

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

/**
 * Determines what feedback action could be executed at the given position. 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 of Alt, Control and Shift key.
 * @param coords The target position in layer space.
 * @param instance [out] Returns the instance that was hit, if any.
 * @return A flag indicating the possible action state.
 */
TFeedbackInfo CFeedbackLayer::getFeedbackInfo(int modifiers, const TVertex& coords, float zoom, 
                                              CFigureInstance** instance)
{
  TFeedbackInfo result = GC_FI_NONE;

  for (CFloaters::reverse_iterator iterator = FFloaters.rbegin(); iterator != FFloaters.rend(); ++iterator)
  {
    result = (*iterator)->getFeedbackInfo(modifiers, coords);
    if (result != GC_FI_NONE)
      break;
  };

  if (result == GC_FI_NONE)
  {
    // Try to find a decoration that is located at the given position.
    // We examine only those that are not dirty. dirty decorations cannot be in view.
    // Iteration happens backwards to find decorations on top earlier than others.
    float distance = 4 / zoom;
    for (CSelectionIteratorReverse iterator = FSelection.rbegin(); iterator != FSelection.rend(); ++iterator)
    {
      if (!iterator->second->dirty)
      {
        TBoundingBox bounds = iterator->second->bounds;
        if ((coords.x >= (bounds.upper.x - distance)) && (coords.x <= (bounds.lower.x + distance)) &&
          (coords.y > (bounds.upper.y - distance)) && (coords.y <= (bounds.lower.y + distance)))
        {                                                
          // Found a decoration. Check if a handle is hit.
          *instance = iterator->first;

          bool leftColumn = ::fabs(bounds.upper.x - coords.x) <= distance;
          bool middleColumn = ::fabs((bounds.upper.x + bounds.lower.x) / 2 - coords.x) <= distance;
          bool rightColumn = ::fabs(bounds.lower.x - coords.x) <= distance;
          bool topRow = ::fabs(bounds.upper.y - coords.y) <= distance;
          bool middleRow = ::fabs((bounds.upper.y + bounds.lower.y) / 2 - coords.y) <= distance;
          bool bottomRow = ::fabs(bounds.lower.y - coords.y) <= distance;

          if (leftColumn)
          {
            if (topRow)
              result = GC_FI_RESIZE_NORTH_WEST;
            else
              if (middleRow)
                result = GC_FI_RESIZE_WEST;
              else
                if (bottomRow)
                  result = GC_FI_RESIZE_SOUTH_WEST;
          }
          else
            if (middleColumn)
            {
              if (topRow)
                result = GC_FI_RESIZE_NORTH;
              else
                if (bottomRow)
                  result = GC_FI_RESIZE_SOUTH;
            }
            else
              if (rightColumn)
              {
                if (topRow)
                  result = GC_FI_RESIZE_NORTH_EAST;
                else
                  if (middleRow)
                    result = GC_FI_RESIZE_EAST;
                  else
                    if (bottomRow)
                      result = GC_FI_RESIZE_SOUTH_EAST;
              };

          break;
        };
      };
    };
  };

  return result;
}

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

/**
 * Creates and returns a selection entry enumerator.
 *
 * @return A new selection enumerator instance. The caller is responsible for freeing it!
 */
CSelectionEnumerator* CFeedbackLayer::getSelectionEnumerator(void)
{
  return new CSelectionEnumerator(this);
}

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

/**
 * Invalidates the selection decoration of the given instance (or all instances if instance is NULL) so they are recomputed
 * next time the selection layer draws them.
 *
 * @param instance The figure instance whose bounds need recomputation. If this parameter is NULL then all bounds are invalidated.
 */
void CFeedbackLayer::invalidateBounds(CFigureInstance* instance)
{
  if (instance == NULL)
  {
    for (CSelectionIterator iterator = FSelection.begin(); iterator != FSelection.end(); ++iterator)
      iterator->second->dirty = true;
    makeDirty();
  }
  else
    if (instance->FSelected)
    {
      CSelectionIterator iterator = FSelection.find(instance);
      if (iterator != FSelection.end())
      {
        iterator->second->dirty = true;
        makeDirty();
      };
    };
}

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

/**
 * Translates all currently selected figure instances by the given amount.
 *
 * @param x The horizontal amount for moving.
 * @param y The vertical amount for moving.
 * @param z The depth amount for moving.
 * @param accumulative Relative or absolute move.
 */
void CFeedbackLayer::moveSelectedInstances(float x, float y, float z, bool accumulative)
{
  if (FSelection.size() > 0)
  {
    for (CSelectionIterator iterator = FSelection.begin(); iterator != FSelection.end(); iterator++)
    {
      CFigureInstance* instance = iterator->second->instance;
      instance->translate(x, y, z, accumulative);
    };
  };
}

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

/**
 * Removes the given floater from the floater list.
 *
 * @param floater The floater to be removed.
 */
void CFeedbackLayer::removeFloater(CFloater* floater)
{
  CFloaters::iterator iterator = find(FFloaters.begin(), FFloaters.end(), floater);
  if (iterator != FFloaters.end())
    FFloaters.erase(iterator);
}

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

/**
 * Removes the given figure instance from the current selection.
 *
 * @param instance The instance to be removed. If it isn't actually selected then nothing happens.
 */
void CFeedbackLayer::removeFromSelection(CFigureInstance* instance)
{
  if (instance->FSelected)
  {
    internalRemoveFromSelection(instance);
    change(instance, GC_CHANGE_SELECTION_REMOVE);
    if (!updating())
      makeDirty();
  };
}

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

/**
 * Method from CLayer overriden to also remove the instance from the current selection if necessary.
 *
 * @param instance The instance to be removed.
 */
void CFeedbackLayer::removeInstance(CFigureInstance* instance)
{
  if (updating())
    internalRemoveFromSelection(instance);
  else
    removeFromSelection(instance);
  CLayer::removeInstance(instance);
}

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

/**
 * When in rubber band mode then this function extends the current rubber band from the start point to
 * the given coordinates.
 *
 * @param coords The coordinate of the new endpoint in view space.
 */
void CFeedbackLayer::rubberBandResize(const TVertex& coords)
{
  if ((FStates & GC_FBSTATE_RUBBERBAND) != 0)
  {
    viewToLayer(coords, FRubberBounds.lower);
    canvas()->refresh();
  };
}

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

/**
 * Starts the rubber band if none is active currently. Otherwise it does nothing.
 *
 * @param style Determines the visible style of the rubber band.
 * @param coords The coordinates of the start point in view space.
 */
void CFeedbackLayer::rubberBandStart(TRubberBandStyle style, const TVertex& coords)
{
  if ((FStates & GC_FBSTATE_RUBBERBAND) != 0)
    rubberBandStop();

  FStates |= GC_FBSTATE_RUBBERBAND;

  viewToLayer(coords, FRubberBounds.upper);
  FRubberBounds.lower = FRubberBounds.upper;

  if (FRubberBandDisplayList == 0 || FLastRBStyle != style)
  {
    FLastRBStyle = style;
    if (FRubberBandDisplayList == 0)
      FRubberBandDisplayList = glGenLists(1);
    glNewList(FRubberBandDisplayList, GL_COMPILE);

    glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT);
    glEnable(GL_LINE_SMOOTH);
    glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);

    glColor4f(0, 0, 0, 0.5);
    switch (style)
    {
      case GC_RBSTYLE_SOLID_THIN:   // A one pixel thick solid black line.
        {
          glLineWidth(1);
          break;
        };
      case GC_RBSTYLE_SOLID_THICK:  // A three pixels thick solid black line.
        {
          glLineWidth(3);
          break;
        };
      case GC_RBSTYLE_DOTTED_THIN:  // A one pixel thick dotted black line.
        {
          glLineWidth(1);
          glEnable(GL_LINE_STIPPLE);
          glLineStipple(1, 0xFF);
          break;
        };
      case GC_RBSTYLE_DOTTED_THICK: // A three pixels thick solid black line.
        {
          glLineWidth(3);
          glEnable(GL_LINE_STIPPLE);
          glLineStipple(1, 0xFF);
          break;
        };
    };

    glBegin(GL_LINES);
      glVertex3d(0, 0, 1);
      glVertex3d(1, 1, 1);
    glEnd();

    if (canvas()->supportsExtension(GC_OE_MULTISAMPLING))
      glEnable(GL_MULTISAMPLE_ARB);

    glPopAttrib();
    glEndList();
  };
}

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

/**
 * Stops the rubber band mode if it is active currently. Does nothing if not.
 */
void CFeedbackLayer::rubberBandStop(void)
{
  if ((FStates & GC_FBSTATE_RUBBERBAND) != 0)
  {
    FStates &= ~GC_FBSTATE_RUBBERBAND;
    canvas()->refresh();
  };
}

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

/**
 * When in rubber rectangle mode then this function extends the current rubber rectangle from the start point to
 * the given coordinates and handles selection/deselection of figure instances.
 *
 * @param coords The coordinate of the new corner in view space.
 * @param Action Determines if and how figure instance selection is to be handled. See TRRSelectionAction for
 *               a description of the various modes.
 */
void CFeedbackLayer::rubberRectResize(const TVertex& coords, TRRSelectionAction Action)
{
  if ((FStates & GC_FBSTATE_RUBBERRECT) != 0)
  {
    TBoundingBox oldBounds = sortBounds(FRubberBounds);
    viewToLayer(coords, FRubberBounds.lower);
    TBoundingBox newBounds = sortBounds(FRubberBounds);

    bool haveSelectionRemoved = false;
    bool haveSelectionAdded = false;

    if (Action != GC_RRACTION_NONE)
    {
      CFigureInstanceEnumerator* enumerator = FView->getFigureInstanceEnumerator();
      switch (Action)
      {
        case GC_RRACTION_SELECT:
          {
            // Select all figure instances if they intersect. Leave all others alone.
            while (enumerator->hasNext())
            {
              CFigureInstance* instance = enumerator->next();
              TBoundingBox instanceBounds = instance->bounds();
              if (!boundsAreEmpty(instanceBounds))
              {
                //if (boundsIntersect(newBounds, instanceBounds))
                if (boundsContainBounds(newBounds, instanceBounds))
                {
                  internalAddToSelection(instance);
                  haveSelectionAdded = true;
                };
              };
            };
            if (haveSelectionAdded)
              change(NULL, GC_CHANGE_SELECTION_ADD);

            break;
          };
        case GC_RRACTION_SELECT_REMOVE:
          {
            // Select figure instances, which intersect, unselect all others.
            while (enumerator->hasNext())
            {
              CFigureInstance* instance = enumerator->next();
              TBoundingBox instanceBounds = instance->bounds();
              if (!boundsAreEmpty(instanceBounds))
              {
                if (instance->FSelected)
                {
                  // instance is selected. See if it is still within the rubberband bounds.
                  // Remove it from the selection list if not.
                  //if (!boundsIntersect(newBounds, instanceBounds))
                  if (!boundsContainBounds(newBounds, instanceBounds))
                  {
                    internalRemoveFromSelection(instance);
                    haveSelectionRemoved = true;
                  };
                }
                else
                {
                  // instance is not selected. Add it to the current selection if its bounds
                  // fall within the rubberband bounds.
                  //if (boundsIntersect(newBounds, instanceBounds))
                  if (boundsContainBounds(newBounds, instanceBounds))
                  {
                    internalAddToSelection(instance);
                    haveSelectionAdded = true;
                  };
                };
              };
            };

            if (haveSelectionRemoved)
              change(NULL, GC_CHANGE_SELECTION_REMOVE);

            if (haveSelectionAdded)
              change(NULL, GC_CHANGE_SELECTION_ADD);

            break;
          };
        case GC_RRACTION_TOGGLE:
          {
            // Switch figure instance selection state if their bounds intersect either
            // the new rubber band bounds or the old, but not both.
            while (enumerator->hasNext())
            {
              CFigureInstance* instance = enumerator->next();
              TBoundingBox instanceBounds = instance->bounds();

              if (!boundsAreEmpty(instanceBounds))
              {
                //if (boundsIntersect(newBounds, instanceBounds) != boundsIntersect(oldBounds, instanceBounds))
                if (boundsContainBounds(newBounds, instanceBounds) != boundsContainBounds(oldBounds, instanceBounds))
                {
                  if (instance->FSelected)
                  {
                    internalRemoveFromSelection(instance);
                    haveSelectionRemoved = true;
                  }
                  else
                  {
                    internalAddToSelection(instance);
                    haveSelectionAdded = true;
                  };
                };
              };
            };

            if (haveSelectionRemoved)
              change(NULL, GC_CHANGE_SELECTION_REMOVE);

            if (haveSelectionAdded)
              change(NULL, GC_CHANGE_SELECTION_ADD);

            break;
          };
      };
      delete enumerator;
    };
    
    if (haveSelectionAdded || haveSelectionRemoved)
    {
      makeDirty();
      change(this, GC_CHANGE_SELECTION_CHANGE);
    }
    else
      canvas()->refresh();
  };
}

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

/**
 * Starts the rubber rectangle if none is active currently. Otherwise it does nothing.
 *
 * @param style Determines the visible style of the rubber rectangle.
 * @param coords The coordinates of the start point in view space.
 * @param removeSelection If true then the current selection will be cleared.
 */
void CFeedbackLayer::rubberRectStart(TRubberRectStyle style, const TVertex& coords, bool removeSelection)
{
  if ((FStates & GC_FBSTATE_RUBBERRECT) != 0)
    rubberRectStop();

  FStates |= GC_FBSTATE_RUBBERRECT;

  if (removeSelection)
    clearSelection();

  viewToLayer(coords, FRubberBounds.upper);
  FRubberBounds.lower = FRubberBounds.upper;

  if (FRubberRectDisplayList == 0 || FLastRRStyle != style)
  {
    FLastRRStyle = style;
    if (FRubberRectDisplayList == 0)
      FRubberRectDisplayList = glGenLists(1);
    glNewList(FRubberRectDisplayList, GL_COMPILE);
    glPushAttrib(GL_LINE_BIT);
    glDisable(GL_LINE_SMOOTH);

    switch (style)
    {
      case GC_RRSTYLE_SOLID_THIN:   // A simple black rectangle with a one pixel wide border.
        {
          GLubyte Color[4];

          if (!colorByName("Highlight", Color))
          {
            // If the system's highlight color could not be found then use a default one.
            Color[0] = 64;
            Color[1] = 64;
            Color[2] = 128;
          };

          Color[3] = 80;
          glColor4ubv(Color);
          glBegin(GL_POLYGON);
            glVertex3d(0, 0, 1);
            glVertex3d(1, 0, 1);
            glVertex3d(1, 1, 1);
            glVertex3d(0, 1, 1);
          glEnd();

          Color[3] = 255;
          glColor4ubv(Color);
          glLineWidth(1);
          glBegin(GL_LINE_LOOP);
            glVertex3d(0, 0, 1);
            glVertex3d(1, 0, 1);
            glVertex3d(1, 1, 1);
            glVertex3d(0, 1, 1);
          glEnd();

          break;
        };
      case GC_RRSTYLE_SOLID_THICK:  // A simple black rectangle with a 3 pixel wide border.
        {
          GLubyte Color[4];

          if (!colorByName("Highlight", Color))
          {
            // If the system's highlight color could not be found then use a default one.
            Color[0] = 64;
            Color[1] = 64;
            Color[2] = 128;
          };

          Color[3] = 80;
          glColor4ubv(Color);
          glBegin(GL_POLYGON);
            glVertex3d(0, 0, 1);
            glVertex3d(1, 0, 1);
            glVertex3d(1, 1, 1);
            glVertex3d(0, 1, 1);
          glEnd();

          Color[3] = 255;
          glColor4ubv(Color);
          glLineWidth(3);
          glBegin(GL_LINE_LOOP);
            glVertex3d(0, 0, 1);
            glVertex3d(1, 0, 1);
            glVertex3d(1, 1, 1);
            glVertex3d(0, 1, 1);
          glEnd();

          break;
        };
      case GC_RRSTYLE_DOTTED_THIN:  // A simple black rectangle with a one pixel wide dotted border.
        {
          glColor3f(0, 0, 0);
          glLineWidth(1);
          glEnable(GL_LINE_STIPPLE);
          glLineStipple(1, 0xFF);
          glBegin(GL_LINE_LOOP);
            glVertex3d(0, 0, 1);
            glVertex3d(1, 0, 1);
            glVertex3d(1, 1, 1);
            glVertex3d(0, 1, 1);
          glEnd();
          break;
        };
      case GC_RRSTYLE_DOTTED_THICK: // A simple black rectangle with a 3 pixel wide dotted border.
        {
          glColor3f(0, 0, 0);
          glLineWidth(3);
          glEnable(GL_LINE_STIPPLE);
          glLineStipple(1, 0xFF);
          glBegin(GL_LINE_LOOP);
            glVertex3d(0, 0, 1);
            glVertex3d(1, 0, 1);
            glVertex3d(1, 1, 1);
            glVertex3d(0, 1, 1);
          glEnd();
          break;
        };
      case GC_RRSTYLE_BLENDED_CHECKERBOARD: // A filled rectangle with a one pixel border and a translucent interior.
        {
          GLubyte Color[4];
          if (!colorByName("Highlight", Color))
          {
            // If the system's highlight color could not be found then use a default one.
            Color[0] = 64;
            Color[1] = 64;
            Color[2] = 128;
          };

          glLineWidth(1);
          for (int row = 0; row < 10; row++)
          {
            for (int cell = 0; cell < 10; cell++)
            {
              if (((row + cell) & 1) == 0)
                Color[3] = 60;
              else
                Color[3] = 70;
              glColor4ubv(Color);
              glBegin(GL_POLYGON);
                glVertex3d(cell * 0.1, row * 0.1, 1);
                glVertex3d(cell * 0.1, (row + 1) * 0.1, 1);
                glVertex3d((cell + 1) * 0.1, (row + 1) * 0.1, 1);
                glVertex3d((cell + 1) * 0.1, row * 0.1, 1);
              glEnd();
            };
          };

          Color[3] = 200;
          glColor4ubv(Color);
          glBegin(GL_LINE_LOOP);
            glVertex3d(0, 0, 1);
            glVertex3d(1, 0, 1);
            glVertex3d(1, 1, 1);
            glVertex3d(0, 1, 1);
          glEnd();
          break;
        };
      case GC_RRSTYLE_BLENDED_DIAGONALS: // A filled rectangle with a one pixel border and a translucent interior.
        {
          GLubyte Color[4];
          if (!colorByName("Highlight", Color))
          {
            // If the system's highlight color could not be found then use a default one.
            Color[0] = 64;
            Color[1] = 64;
            Color[2] = 128;
          };

          glLineWidth(1);

          // Interior.
          int Steps = 50;
          double dStep = 1.0 / Steps;

          // left lower half.
          for (int x = 0; x < Steps; x++)
          {
            if (x % 2 == 0)
              Color[3] = 60;
            else
              Color[3] = 100;

            glColor4ubv(Color);
            glBegin(GL_POLYGON);
              glVertex3d(x * dStep, 0, 1);
              glVertex3d(0, x * dStep, 1);
              glVertex3d(0, (x + 1) * dStep, 1);
              glVertex3d((x + 1) * dStep, 0, 1);
            glEnd();
          };

          // right upper half.
          for (int y = 0; y < Steps; y++)
          {
            if (y % 2 == 0)
              Color[3] = 60;
            else
              Color[3] = 100;

            glColor4ubv(Color);
            glBegin(GL_POLYGON);
              glVertex3d(1, y * dStep, 1);
              glVertex3d(y * dStep, 1, 1);
              glVertex3d((y + 1) * dStep, 1, 1);
              glVertex3d(1, (y + 1) * dStep, 1);
            glEnd();
          };

          // Border.
          Color[3] = 200;
          glColor4ubv(Color);
          glBegin(GL_LINE_LOOP);
            glVertex3d(0, 0, 1);
            glVertex3d(1, 0, 1);
            glVertex3d(1, 1, 1);
            glVertex3d(0, 1, 1);
          glEnd();
          break;
        };
      case GC_RRSTYLE_BLENDED_SOLID: // A filled rectangle with a one pixel border and a translucent interior
      {
        GLubyte Color[4];
        if (!colorByName("Highlight", Color))
        {
          // If the system's highlight color could not be found then use a default one.
          Color[0] = 100;
          Color[1] = 100;
          Color[2] = 100;
        };
        Color[3] = 100;
        
        glLineWidth(1);
        glColor4ubv(Color);
        glBegin(GL_POLYGON);
        glVertex3d(0, 0, 1);
        glVertex3d(1, 0, 1);
        glVertex3d(1, 1, 1);
        glVertex3d(0, 1, 1);
        glEnd();
        
        Color[3] = 200;
        glColor4ubv(Color);
        glBegin(GL_LINE_LOOP);
        glVertex3d(0, 0, 1);
        glVertex3d(1, 0, 1);
        glVertex3d(1, 1, 1);
        glVertex3d(0, 1, 1);
        glEnd();
        break;
      };
    };
    glPopAttrib();
    glEndList();
  };
}

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

/**
 * Stops the rubber rectangle if it is active currently. Does nothing if not.
 */
void CFeedbackLayer::rubberRectStop(void)
{
  if ((FStates & GC_FBSTATE_RUBBERRECT) != 0)
  {
    FStates &= ~GC_FBSTATE_RUBBERRECT;
    canvas()->refresh();
  };
}

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

/**
 * Used to set which part is to be displayed and which remains hidden.
 */
void CFeedbackLayer::visibleParts(TFeedbackParts parts)
{
  FVisibleParts = parts;
  canvas()->refresh();
}

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