[go: up one dir, main page]

File: SoDragger.cpp

package info (click to toggle)
coin3 3.1.3-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 48,344 kB
  • ctags: 70,042
  • sloc: cpp: 314,328; ansic: 15,927; sh: 13,635; makefile: 8,780; perl: 2,149; lex: 1,302; lisp: 1,247; yacc: 184; xml: 175; sed: 68
file content (1928 lines) | stat: -rw-r--r-- 56,623 bytes parent folder | download | duplicates (2)
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
/**************************************************************************\
 *
 *  This file is part of the Coin 3D visualization library.
 *  Copyright (C) by Kongsberg Oil & Gas Technologies.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  ("GPL") version 2 as published by the Free Software Foundation.
 *  See the file LICENSE.GPL at the root directory of this source
 *  distribution for additional information about the GNU GPL.
 *
 *  For using Coin with software that can not be combined with the GNU
 *  GPL, and for taking advantage of the additional benefits of our
 *  support services, please contact Kongsberg Oil & Gas Technologies
 *  about acquiring a Coin Professional Edition License.
 *
 *  See http://www.coin3d.org/ for more information.
 *
 *  Kongsberg Oil & Gas Technologies, Bygdoy Alle 5, 0257 Oslo, NORWAY.
 *  http://www.sim.no/  sales@sim.no  coin-support@coin3d.org
 *
\**************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif // HAVE_CONFIG_H

#ifdef HAVE_DRAGGERS

/*!
  \class SoDragger SoDragger.h Inventor/draggers/SoDragger.h
  \brief The SoDragger class is the base class for all draggers.
  \ingroup draggers

  Draggers is a mechanism used for letting the end-users of your
  application code interact with elements in 3D, by scaling, rotating
  or translating geometry or other instances in the scene (like
  cameras or lightsources).

  For a very thorough introduction and tutorial to the dragger classes
  and general concepts, we advise you to consult The Inventor
  Mentor, ISBN 0-201-62495-8, chapter 15.

  This is the common superclass for all dragger classes.

  It holds the current motion matrix, and offers lots of convenience
  methods to build from for it's subclasses -- that is, the
  non-abstract dragger classes to use as nodes in your scenegraph.

  The motion matrix is used to modify the model matrix during
  traversal, and this is a common dragger mechanism -- all draggers
  should update this during dragging.

  A number of the Coin dragger classes have built-in convenience
  wrapper classes, called \e manipulators. See for instance the
  SoTrackballDragger / SoTrackballManip pair.

  The matching manipulator class for any dragger class has basically
  two convenient additions to the functionality of the stand-alone
  dragger: 1) it makes swapping the dragger in and out of the
  scenegraph very straightforward (something which is often done for
  draggers in 3D user interfaces), 2) it wraps up the dragger with
  SoSurroundScale and SoAntiSquish nodes where applicable, so the
  dragger geometry automatically scales up or down to match the
  geometry it influences.

  This last functionality can also be duplicated in a rather
  straightforward manner outside of the context of a manipulator, as
  can be seen from the usage example in the SoSurroundScale class
  documentation.

  The appearance of draggers can be modified by either using the
  SoDragger::setPart() method (see usage example below) or by setting
  up external Inventor-format files which the geometry parts are read
  from. The latter method can be done by setting the environment
  variable \c SO_DRAGGER_DIR to point to a directory with replacement
  geometry files. The name of the new files and the name of the nodes
  / sub-graphs with the replacement geometries must follow a rigid
  scheme. We advise you to look at the Coin sourcecode directory
  Coin/data/draggerDefaults/ to see how the replacement geometry files
  should be named. Setting \c SO_DRAGGER_DIR to this directory and
  modifying the files there provides a convenient way to play around
  with new dragger geometry arrangements.


  As mentioned above, SoDragger::setPart() can be used to modify the
  appearance of a dragger by changing it's default geometry. One
  common technique is for instance to take advantage of this to use
  only \e parts of a dragger, by replacing / disabling the geometry
  that you don't want the end-user to interact with. The following
  code example shows how to remove the translation functionality of
  the SoTransformBoxDragger:

  \code
  #include <Inventor/Qt/SoQt.h>
  #include <Inventor/Qt/viewers/SoQtExaminerViewer.h>
  #include <Inventor/draggers/SoTransformBoxDragger.h>
  #include <Inventor/nodes/SoSeparator.h>


  int
  main(int argc, char ** argv)
  {
    QWidget * window = SoQt::init(argv[0]);

    SoTransformBoxDragger * dragger = new SoTransformBoxDragger;

    SbString str;
    for (int i = 1; i <= 6; i++) {
      str.sprintf("translator%d.translator", i);
      dragger->setPart(str.getString(), new SoSeparator);
    }

    SoQtExaminerViewer * viewer = new SoQtExaminerViewer(window);
    viewer->setSceneGraph(dragger);
    viewer->show();
    SoQt::show(window);

    SoQt::mainLoop();

    delete viewer;
    return 0;
  }
  \endcode

  Draggers are also node kits, and below is the catalog structure for
  this top-level dragger class.

  \NODEKIT_PRE_DIAGRAM

  \verbatim
  CLASS SoDragger
  -->"this"
        "callbackList"
        "topSeparator"
  -->      "motionMatrix"
           "geomSeparator"
  \endverbatim

  \NODEKIT_POST_DIAGRAM


  \NODEKIT_PRE_TABLE

  \verbatim
  CLASS SoDragger
  PVT   "this",  SoDragger  ---
        "callbackList",  SoNodeKitListPart [ SoCallback, SoEventCallback ]
  PVT   "topSeparator",  SoSeparator  ---
  PVT   "motionMatrix",  SoMatrixTransform  ---
  PVT   "geomSeparator",  SoSeparator  ---
  \endverbatim

  \NODEKIT_POST_TABLE
*/

//   FIXME: more class doc! The general concept of draggers should be
//   explained in more detail here -- just refering to the Inventor
//   Mentor is a cop-out. And include at least one general usage example
//   and some screenshots.  20011219 mortene.


/*!
  \var SoSFBool SoDragger::isActive
  Is \c TRUE whenever the user is interacting with the dragger. For
  compound draggers (draggers consisting of one or more subdraggers),
  the isActive field is updated only for the active subdragger, not
  for the compound dragger.
*/

/*!
  \enum SoDragger::ProjectorFrontSetting

  Holds various settings for projectors, which might affect
  cylindrical and spherical based draggers.  Specifies whether
  dragging should be based on the front or back of the sphere /
  cylinder, or if the picked point should be used to decide this.
*/

/*!
  \var SoDragger::ProjectorFrontSetting SoDragger::FRONT
  Always use front of projector.
*/

/*!
  \var SoDragger::ProjectorFrontSetting SoDragger::BACK
  Always use back of projector.
*/

/*!
  \var SoDragger::ProjectorFrontSetting SoDragger::USE_PICK
  Use picked point to decide front or back of projector.
*/

// FIXME: document DraggerCB typedef? 20010909 mortene.

#include <Inventor/draggers/SoDragger.h>

#include <Inventor/draggers/SoCenterballDragger.h>
#include <Inventor/draggers/SoDirectionalLightDragger.h>
#include <Inventor/draggers/SoDragPointDragger.h>
#include <Inventor/draggers/SoHandleBoxDragger.h>
#include <Inventor/draggers/SoJackDragger.h>
#include <Inventor/draggers/SoPointLightDragger.h>
#include <Inventor/draggers/SoRotateCylindricalDragger.h>
#include <Inventor/draggers/SoRotateDiscDragger.h>
#include <Inventor/draggers/SoRotateSphericalDragger.h>
#include <Inventor/draggers/SoScale1Dragger.h>
#include <Inventor/draggers/SoScale2Dragger.h>
#include <Inventor/draggers/SoScale2UniformDragger.h>
#include <Inventor/draggers/SoScaleUniformDragger.h>
#include <Inventor/draggers/SoSpotLightDragger.h>
#include <Inventor/draggers/SoTabBoxDragger.h>
#include <Inventor/draggers/SoTabPlaneDragger.h>
#include <Inventor/draggers/SoTrackballDragger.h>
#include <Inventor/draggers/SoTransformBoxDragger.h>
#include <Inventor/draggers/SoTransformerDragger.h>
#include <Inventor/draggers/SoTranslate1Dragger.h>
#include <Inventor/draggers/SoTranslate2Dragger.h>
#include <Inventor/actions/SoHandleEventAction.h>
#include <Inventor/actions/SoRayPickAction.h>
#include <Inventor/actions/SoGetMatrixAction.h>
#include <Inventor/events/SoMouseButtonEvent.h>
#include <Inventor/events/SoLocation2Event.h>
#include <Inventor/events/SoKeyboardEvent.h>
#include <Inventor/elements/SoViewVolumeElement.h>
#include <Inventor/elements/SoViewportRegionElement.h>

#include <Inventor/actions/SoGLRenderAction.h>
#include <Inventor/actions/SoCallbackAction.h>
#include <Inventor/actions/SoRayPickAction.h>
#include <Inventor/actions/SoGetPrimitiveCountAction.h>

#include <Inventor/elements/SoGLTextureImageElement.h>
#include <Inventor/elements/SoGLTextureEnabledElement.h>
#include <Inventor/elements/SoGLTexture3EnabledElement.h>
#include <Inventor/elements/SoShapeHintsElement.h>
#include <Inventor/elements/SoNormalBindingElement.h>
#include <Inventor/elements/SoMaterialBindingElement.h>
#include <Inventor/elements/SoLazyElement.h>
#include <Inventor/elements/SoNormalElement.h>
#include <Inventor/elements/SoLineWidthElement.h>
#include <Inventor/elements/SoLinePatternElement.h>
#include <Inventor/elements/SoCreaseAngleElement.h>
#include <Inventor/elements/SoComplexityElement.h>
#include <Inventor/elements/SoComplexityTypeElement.h>
#include <Inventor/nodes/SoCamera.h>

#include <Inventor/SbViewportRegion.h>
#include <Inventor/nodes/SoMatrixTransform.h>
#include <Inventor/SoNodeKitPath.h>
#include <Inventor/SoPickedPoint.h>

#include <Inventor/errors/SoDebugError.h>

#include "coindefs.h" // COIN_OBSOLETED
#include "tidbitsp.h"
#include "nodekits/SoSubKitP.h"
#include "SbBasicP.h"

// Internal helper class.
class SoDraggerCache {
public:
  SoDraggerCache(SoDragger * parent) :
    path(reclassify_cast<SoFullPath *>(new SoPath(4))),
    dragger(parent),
    matrixAction(new SoGetMatrixAction(dragger->getViewportRegion())),
    draggerToWorld(SbMatrix::identity()),
    worldToDragger(SbMatrix::identity())
  {
    this->path->ref();
  }

  ~SoDraggerCache() {
    delete this->matrixAction;
    this->path->unref();
  }

  void updateMatrix(void) {
    this->matrixAction->setViewportRegion(this->dragger->getViewportRegion());
    this->matrixAction->apply(this->path);
    this->draggerToWorld = this->matrixAction->getMatrix();
    this->worldToDragger = this->matrixAction->getInverse();
  }

  void update(const SoFullPath * newpath, const int draggeridx) {
    this->path->setHead(newpath->getHead());
    for (int i = 1; i <= draggeridx; i++) {
      this->path->append(newpath->getIndex(i));
    }
    this->updateMatrix();
  }

  void truncatePath(void) {
    this->path->truncate(0);
  }

  SoFullPath * path;
  SoDragger * dragger; // pointer to cache owner
  SoGetMatrixAction * matrixAction; // avoid reallocating this action each frame
  SbMatrix draggerToWorld;
  SbMatrix worldToDragger;
};

class SoDraggerP {
public:
  int mingesture;
  SoHandleEventAction * eventaction;
  SoDragger::ProjectorFrontSetting frontonprojector;
  SbBool valuechangedcbenabled;
  SbBool ignoreinbbox;
  const SoEvent * currentevent;
  SoPath * pickedpath;
  SoDraggerCache * draggercache;
  SbBool didmousemove;

  SoCallbackList startCB;
  SoCallbackList motionCB;
  SoCallbackList finishCB;
  SoCallbackList valueChangedCB;
  SoCallbackList otherEventCB;
  SbMatrix startmotionmatrix;
  SbVec3f startingpoint;
  SbViewVolume viewvolume;
  SbViewportRegion viewport;
  SbVec2s startlocaterpos;
  SoDragger * activechilddragger;
  SbBool isgrabbing;

  SbName surrogatename;
  SoPath * surrogateownerpath;
  SoPath * surrogatepath;

  SoCallbackAction * cbaction;
  float projectorepsilon;
};

#define PRIVATE(obj) ((obj)->pimpl)

SO_KIT_SOURCE(SoDragger);

float SoDragger::minscale = 0.001f;

/*!
  A protected constructor for this abstract superclass for all Coin
  draggers.
*/
SoDragger::SoDragger(void)
{
  SO_KIT_INTERNAL_CONSTRUCTOR(SoDragger);

  SO_KIT_ADD_CATALOG_ENTRY(motionMatrix, SoMatrixTransform, FALSE, topSeparator, geomSeparator, FALSE);

  SO_KIT_ADD_FIELD(isActive, (FALSE));

  SO_KIT_INIT_INSTANCE();

  PRIVATE(this)->mingesture = 8;
  PRIVATE(this)->eventaction = NULL;
  PRIVATE(this)->frontonprojector = USE_PICK;
  PRIVATE(this)->valuechangedcbenabled = TRUE;
  PRIVATE(this)->ignoreinbbox = FALSE;
  PRIVATE(this)->currentevent = NULL;
  PRIVATE(this)->pickedpath = NULL;
  PRIVATE(this)->draggercache = NULL;
  PRIVATE(this)->isgrabbing = FALSE;
  PRIVATE(this)->activechilddragger = NULL;
  PRIVATE(this)->surrogateownerpath = NULL;
  PRIVATE(this)->surrogatepath = NULL;
  PRIVATE(this)->cbaction = NULL;
  PRIVATE(this)->didmousemove = FALSE;
  PRIVATE(this)->projectorepsilon = 0.0f;
}

/*!
  Virtual protected destructor.
*/
SoDragger::~SoDragger()
{
  if (PRIVATE(this)->pickedpath)
    PRIVATE(this)->pickedpath->unref();
  if (PRIVATE(this)->surrogateownerpath)
    PRIVATE(this)->surrogateownerpath->unref();
  if (PRIVATE(this)->surrogatepath)
    PRIVATE(this)->surrogatepath->unref();
  delete PRIVATE(this)->draggercache;
  delete PRIVATE(this)->cbaction;
}

// Note: the following documentation for initClass() will also be used
// for dragger subclasses, so keep it general.
/*!
  Initializes type system for this dragger class.

  Application programmers should usually not have to invoke this
  method, see documentation of SoInteraction::init().
*/
void
SoDragger::initClass(void)
{
  SO_KIT_INTERNAL_INIT_CLASS(SoDragger, SO_FROM_INVENTOR_1);
  SoDragger::minscale = 0.001f;

  SoDragger::initClasses();

  SoType type = SoDragger::getClassTypeId();
  SoRayPickAction::addMethod(type, SoNode::rayPickS);
}

/*!
  Initializes all built-in draggers.
*/
void
SoDragger::initClasses(void)
{
  SoCenterballDragger::initClass();
  SoDirectionalLightDragger::initClass();
  SoDragPointDragger::initClass();
  SoHandleBoxDragger::initClass();
  SoJackDragger::initClass();
  SoPointLightDragger::initClass();
  SoRotateCylindricalDragger::initClass();
  SoRotateDiscDragger::initClass();
  SoRotateSphericalDragger::initClass();
  SoScale1Dragger::initClass();
  SoScale2Dragger::initClass();
  SoScale2UniformDragger::initClass();
  SoScaleUniformDragger::initClass();
  SoSpotLightDragger::initClass();
  SoTabBoxDragger::initClass();
  SoTabPlaneDragger::initClass();
  SoTrackballDragger::initClass();
  SoTransformBoxDragger::initClass();
  SoTransformerDragger::initClass();
  SoTranslate1Dragger::initClass();
  SoTranslate2Dragger::initClass();
}

// Private method that sets some elements to default (for our
// draggers) values.
void
SoDragger::updateElements(SoState * state)
{
  if (state->isElementEnabled(SoShapeHintsElement::getClassStackIndex())) {
    // turn on backface culling for draggers
    SoShapeHintsElement::set(state, this,
                             SoShapeHintsElement::COUNTERCLOCKWISE,
                             SoShapeHintsElement::SOLID,
                             SoShapeHintsElement::CONVEX);
  }
  if (state->isElementEnabled(SoTextureEnabledElement::getClassStackIndex())) {
    // disable 2D texture
    SoTextureEnabledElement::set(state, this, FALSE);
  }
  if (state->isElementEnabled(SoTexture3EnabledElement::getClassStackIndex())) {
    // disable 3D texture
    SoTexture3EnabledElement::set(state, this, FALSE);
  }
  if (state->isElementEnabled(SoNormalBindingElement::getClassStackIndex())) {
    // make default
    SoNormalBindingElement::set(state, SoNormalBindingElement::DEFAULT);
  }
  if (state->isElementEnabled(SoMaterialBindingElement::getClassStackIndex())) {
    // make default
    SoMaterialBindingElement::set(state, SoMaterialBindingElement::DEFAULT);
  }
  if (state->isElementEnabled(SoLazyElement::getClassStackIndex())) {
    // we need phong shading for our geometry
    SoLazyElement::setLightModel(state, SoLazyElement::PHONG);
  }

  if (state->isElementEnabled(SoNormalElement::getClassStackIndex())) {
    // lines/shapes will use normals if on state. We don't want that.
    SoNormalElement::set(state, this, 0, NULL);
  }
  if (state->isElementEnabled(SoLineWidthElement::getClassStackIndex())) {
    // make default
    SoLineWidthElement::set(state, this, SoLineWidthElement::getDefault());
  }
  if (state->isElementEnabled(SoLinePatternElement::getClassStackIndex())) {
    // make default
    SoLinePatternElement::set(state, this, SoLinePatternElement::getDefault());
  }
  if (state->isElementEnabled(SoCreaseAngleElement::getClassStackIndex())) {
    // set to 0.5, which is the value we like
    SoCreaseAngleElement::set(state, this, 0.5f);
  }
  if (state->isElementEnabled(SoComplexityElement::getClassStackIndex())) {
    // make default
    SoComplexityElement::set(state, this, SoComplexityElement::getDefault());
  }
  if (state->isElementEnabled(SoComplexityElement::getClassStackIndex())) {
    // make default
    SoComplexityTypeElement::set(state, this, SoComplexityTypeElement::getDefault());
  }
  // hopefully we didn't forget something...
}

// The action methods are overridden in case we decide to do some
// extra work before passing the control on the SoBaseKit.

// Doc in superclass. Overridden to initialize some elements before
// traversing children.
void
SoDragger::callback(SoCallbackAction * action)
{
  SoState * state = action->getState();
  state->push();
  inherited::callback(action);
  state->pop();
}

// Doc in superclass. Overridden to initialize some elements before
// traversing children.
void
SoDragger::GLRender(SoGLRenderAction * action)
{
  SoState * state = action->getState();
  state->push();
  this->updateElements(state);
  inherited::GLRender(action);
  state->pop();
}

// Doc in superclass. Overridden to initialize some elements before
// traversing children.
void
SoDragger::getMatrix(SoGetMatrixAction * action)
{
  // no need to update any elements here
  inherited::getMatrix(action);
}

// Doc in superclass. Overridden to initialize some elements before
// traversing children.
void
SoDragger::rayPick(SoRayPickAction * action)
{
  SoState * state = action->getState();
  state->push();
  this->updateElements(state);
  inherited::rayPick(action);
  state->pop();
}

// Doc in superclass. Overridden to initialize some elements before
// traversing children.
void
SoDragger::search(SoSearchAction * action)
{
  // no need to update any elements here
  inherited::search(action);
}

// Doc in superclass. Overridden to initialize some elements before
// traversing children.
void
SoDragger::write(SoWriteAction * action)
{
  // no need to update any elements here
  inherited::write(action);
}

// Doc in superclass. Overridden to initialize some elements before
// traversing children.
void
SoDragger::getPrimitiveCount(SoGetPrimitiveCountAction * action)
{
  SoState * state = action->getState();
  state->push();
  this->updateElements(state);
  inherited::getPrimitiveCount(action);
  state->pop();
}

/*!
  Sets the epsilon used for restricting the draggers when the
  intersection line is almost parallel with the projector direction.

  For line projectors this is based on the dot product between the
  picking ray and the projector line. For plane projector, the dot
  product between the plane normal and the picking ray is used.

  Default value is 0.0.

  \since Coin 3.0
*/
void
SoDragger::setProjectorEpsilon(const float epsilon)
{
  PRIVATE(this)->projectorepsilon = epsilon;
}

/*
  Returns the projector epsilon set for this dragger.

  \since Coin 3.0
*/
float
SoDragger::getProjectorEpsilon(void) const
{
  return PRIVATE(this)->projectorepsilon;
}

/*!
  Adds a callback which is called at the start of a drag, after the
  mouse button 1 is pressed, and dragger is picked.
*/
void
SoDragger::addStartCallback(SoDraggerCB * func, void * data)
{
  PRIVATE(this)->startCB.addCallback(reinterpret_cast<SoCallbackListCB *>(func), data);
}

/*!
  Removes a previously registered start callback.

  \sa addStartCallback()
*/
void
SoDragger::removeStartCallback(SoDraggerCB * func, void * data)
{
  PRIVATE(this)->startCB.removeCallback(reinterpret_cast<SoCallbackListCB *>(func), data);
}

/*!
  Adds a callback which is called for each mouse movement during
  dragging.
*/
void
SoDragger::addMotionCallback(SoDraggerCB * func, void * data)
{
  PRIVATE(this)->motionCB.addCallback(reinterpret_cast<SoCallbackListCB *>(func), data);
}

/*!
  Removes a previously registered motion callback.

  \sa addMotionCallback()
*/
void
SoDragger::removeMotionCallback(SoDraggerCB * func, void * data)
{
  PRIVATE(this)->motionCB.removeCallback(reinterpret_cast<SoCallbackListCB *>(func), data);
}

/*!
  Adds a callback which is called after dragging is finished.
*/
void
SoDragger::addFinishCallback(SoDraggerCB * func, void * data)
{
  PRIVATE(this)->finishCB.addCallback(reinterpret_cast<SoCallbackListCB *>(func), data);
}

/*!
  Removes a finish callback.

  \sa addFinishCallback()
*/
void
SoDragger::removeFinishCallback(SoDraggerCB * func, void * data)
{
  PRIVATE(this)->finishCB.removeCallback(reinterpret_cast<SoCallbackListCB *>(func), data);
}

/*!
  Adds a callback which is called after a dragger has changed a field.
  It is not called if the SoDragger::isActive field is changed.

  \sa enableValueChangedCallback()
*/
void
SoDragger::addValueChangedCallback(SoDraggerCB * func, void * data)
{
  PRIVATE(this)->valueChangedCB.addCallback(reinterpret_cast<SoCallbackListCB *>(func), data);
}

/*!
  Removes a value changed callback.

  \sa addValueChangedCallback()
*/
void
SoDragger::removeValueChangedCallback(SoDraggerCB * func, void * data)
{
  PRIVATE(this)->valueChangedCB.removeCallback(reinterpret_cast<SoCallbackListCB *>(func), data);
}

/*!
  Sets the number of pixel movement needed to trigger a constraint
  gesture.  Default is 8 pixels.
*/
void
SoDragger::setMinGesture(int pixels)
{
  PRIVATE(this)->mingesture = pixels;
}

/*!
  Returns the gesture pixels threshold value.

  \sa setMinGesture()
*/
int
SoDragger::getMinGesture(void) const
{
  return PRIVATE(this)->mingesture;
}

/*!
  Enable or disable "value changed" callbacks.

  \sa addValueChangedCallback()
*/
SbBool
SoDragger::enableValueChangedCallbacks(SbBool val)
{
  SbBool oldval = PRIVATE(this)->valuechangedcbenabled;
  PRIVATE(this)->valuechangedcbenabled = val;
  return oldval;
}

/*!
  Returns the motion matrix for this dragger.
*/
const SbMatrix &
SoDragger::getMotionMatrix(void)
{
  SoMatrixTransform * node =
    SO_GET_ANY_PART(this, "motionMatrix", SoMatrixTransform);
  assert(node);
  return node->matrix.getValue();
}

/*!
  Adds an event callback for events other then drag events.  As soon
  as dragging starts, the dragger grabs all events (until mouse button
  is released). This method can be used to handle other events during
  dragging.
*/
void
SoDragger::addOtherEventCallback(SoDraggerCB * func, void * data)
{
  PRIVATE(this)->otherEventCB.addCallback(reinterpret_cast<SoCallbackListCB *>(func), data);
}

/*!
  Removes a other event callback.

  \sa addOtherEventCallback()
*/
void
SoDragger::removeOtherEventCallback(SoDraggerCB * func, void * data)
{
  PRIVATE(this)->otherEventCB.removeCallback(reinterpret_cast<SoCallbackListCB *>(func), data);
}

/*!
  Should be called by compound draggers to register child draggers.
*/
void
SoDragger::registerChildDragger(SoDragger * child)
{
  child->addStartCallback(SoDragger::childStartCB, this);
  child->addMotionCallback(SoDragger::childMotionCB, this);
  child->addFinishCallback(SoDragger::childFinishCB, this);
  child->addOtherEventCallback(SoDragger::childOtherEventCB, this);
  child->addValueChangedCallback(SoDragger::childTransferMotionAndValueChangedCB, this);
}

/*!
  Should be called by compound draggers to unregister child draggers.
*/
void
SoDragger::unregisterChildDragger(SoDragger * child)
{
  child->removeStartCallback(SoDragger::childStartCB, this);
  child->removeMotionCallback(SoDragger::childMotionCB, this);
  child->removeFinishCallback(SoDragger::childFinishCB, this);
  child->removeOtherEventCallback(SoDragger::childOtherEventCB, this);
  child->removeValueChangedCallback(SoDragger::childTransferMotionAndValueChangedCB, this);
}

/*!
  Should be called by compund draggers to register child draggers that
  should move independently of their parent.
*/
void
SoDragger::registerChildDraggerMovingIndependently(SoDragger * child)
{
  child->addStartCallback(SoDragger::childStartCB, this);
  child->addMotionCallback(SoDragger::childMotionCB, this);
  child->addFinishCallback(SoDragger::childFinishCB, this);
  child->addOtherEventCallback(SoDragger::childOtherEventCB, this);
  child->addValueChangedCallback(SoDragger::childValueChangedCB, this);
}

/*!
  Should be called by compund draggers to unregister child draggers.
  \sa registerChildDraggerMovingIndependently()
*/
void
SoDragger::unregisterChildDraggerMovingIndependently(SoDragger * child)
{
  child->removeStartCallback(SoDragger::childStartCB, this);
  child->removeMotionCallback(SoDragger::childMotionCB, this);
  child->removeFinishCallback(SoDragger::childFinishCB, this);
  child->removeOtherEventCallback(SoDragger::childOtherEventCB, this);
  child->removeValueChangedCallback(SoDragger::childValueChangedCB, this);
}

/*!
  Returns a matrix that converts from local to world space.
*/
SbMatrix
SoDragger::getLocalToWorldMatrix(void)
{
  if (PRIVATE(this)->draggercache) {
    SbMatrix m = PRIVATE(this)->draggercache->draggerToWorld;
    m.multLeft(this->getMotionMatrix());
    return m;
  }
  return SbMatrix::identity();
}


/*!
  Returns a matrix that converts from world to local space.
*/
SbMatrix
SoDragger::getWorldToLocalMatrix(void)
{
  if (PRIVATE(this)->draggercache) {
    SbMatrix m = PRIVATE(this)->draggercache->worldToDragger;
    m.multRight(this->getMotionMatrix().inverse());
    return m;
  }
  return SbMatrix::identity();
}

/*!
  Returns the drag starting point in the local coordinate system.
*/
SbVec3f
SoDragger::getLocalStartingPoint(void)
{
  SbVec3f res;
  this->getWorldToLocalMatrix().multVecMatrix(PRIVATE(this)->startingpoint, res);
  return res;
}

/*!
  Returns the drag starting point in the world coordinate system.
*/
SbVec3f
SoDragger::getWorldStartingPoint(void)
{
  return PRIVATE(this)->startingpoint;
}

/*!
  Returns matrices that will convert between local space and the space in
  which \a partname lies in.
*/
void
SoDragger::getPartToLocalMatrix(const SbName & partname, SbMatrix & parttolocalmatrix, SbMatrix & localtopartmatrix)
{
  // ref, in case somebody is operating on a zero-ref instance to the
  // dragger.
  this->ref();
  // we need to create a path from the root node, since
  // SoSurroundScale nodes need the entire path to calculate the
  // surround parameters correctly.
  SoPath * pathtothis = this->createPathToThis();
  assert(pathtothis);
  pathtothis->ref();
  SoPath * path = reclassify_cast<SoPath *>(this->createPathToAnyPart(partname, FALSE, FALSE, FALSE, pathtothis));
  assert(path);
  pathtothis->unref();

  path->ref();
  SoGetMatrixAction action(PRIVATE(this)->viewport);
  action.apply(path);
  SbMatrix p2w = action.getMatrix();
  SbMatrix w2p = action.getInverse();
  path->unref();

  // premultiply with matrix to/from this dragger to remove
  // contributions before the dragger.
  parttolocalmatrix = p2w;
  parttolocalmatrix.multRight(this->getWorldToLocalMatrix());

  localtopartmatrix = this->getLocalToWorldMatrix();
  localtopartmatrix.multRight(w2p);

  // we ref'ed at the beginning of the function
  this->unrefNoDelete();
}

/*!
  Convenience method that transforms the local \a frommatrix to a world
  coordinate systems matrix.
*/
void
SoDragger::transformMatrixLocalToWorld(const SbMatrix & frommatrix, SbMatrix & tomatrix)
{
  if (&tomatrix != &frommatrix) tomatrix = frommatrix;
  tomatrix.multRight(this->getLocalToWorldMatrix());
  tomatrix.multLeft(this->getWorldToLocalMatrix());
}

/*!
  Convenience method that transforms the world \a frommatrix to a local
  coordinate systems matrix.
*/
void
SoDragger::transformMatrixWorldToLocal(const SbMatrix & frommatrix, SbMatrix & tomatrix)
{
  if (&tomatrix != &frommatrix) tomatrix = frommatrix;
  tomatrix.multRight(this->getWorldToLocalMatrix());
  tomatrix.multLeft(this->getLocalToWorldMatrix());
}

/*!
  Transforms a matrix that lies in the \a frompartname coordinate system into
  the local coordinate system.
*/
void
SoDragger::transformMatrixToLocalSpace(const SbMatrix & frommatrix, SbMatrix & tomatrix, const SbName & fromspacepartname)
{
  if (&tomatrix != &frommatrix) tomatrix = frommatrix;
  SbMatrix parttolocal, localtopart;
  this->getPartToLocalMatrix(fromspacepartname, parttolocal, localtopart);
  tomatrix.multRight(parttolocal);
  tomatrix.multLeft(localtopart);
}

/*!
  Sets a new current motion matrix for the dragger geometry.

  Triggers value changed callbacks if \a matrix is unequal to the
  previous motion matrix.
*/
void
SoDragger::setMotionMatrix(const SbMatrix & matrix)
{
  SoMatrixTransform * node = SO_GET_ANY_PART(this, "motionMatrix", SoMatrixTransform);
  if (matrix != node->matrix.getValue()) {
    node->matrix = matrix;
    this->valueChanged();
  }
}

/*!
  Can be called by subclasses to trigger value changed callbacks. This might
  be needed if a field is changed without changing the motion matrix.
*/
void
SoDragger::valueChanged(void)
{
  if (PRIVATE(this)->valuechangedcbenabled) {
    PRIVATE(this)->valueChangedCB.invokeCallbacks(this);
  }
}

/*!
  Returns the motion matrix as it was when saveStartParameters() was called.
*/
const SbMatrix &
SoDragger::getStartMotionMatrix(void)
{
  return PRIVATE(this)->startmotionmatrix;
}

/*!
  This is invoked to save start parameters, to enable draggers to
  calculate relative motion.

  Default method in superclass SoDragger just saves the motion matrix,
  but subclasses should overload this method if other data needs to be
  saved.
*/
void
SoDragger::saveStartParameters(void)
{
  PRIVATE(this)->startmotionmatrix = this->getMotionMatrix();
}

/*!
  Returns the picked path.
*/
const SoPath *
SoDragger::getPickPath(void) const
{
  return PRIVATE(this)->pickedpath;
}

/*!
  Returns the current event.
*/
const SoEvent *
SoDragger::getEvent(void) const
{
  return PRIVATE(this)->currentevent;
}

/*!
  Creates a new path to this dragger. Don't forget to ref() and
  unref() since this method creates a fresh copy for you.
*/
SoPath *
SoDragger::createPathToThis(void)
{
  assert(PRIVATE(this)->draggercache);
  assert(PRIVATE(this)->draggercache->path);
  SoPath * orgpath = reclassify_cast<SoPath *>(PRIVATE(this)->draggercache->path);
  return new SoPath(*orgpath);
}

/*!
  Returns the path to the SoInteractionKit that holds the current surrogate
  path.
*/
const SoPath *
SoDragger::getSurrogatePartPickedOwner(void) const
{
  return PRIVATE(this)->surrogateownerpath;
}

/*!
  Returns the name of the path in the SoInteractionKit that holds the current
  surrogate path.
*/
const SbName &
SoDragger::getSurrogatePartPickedName(void) const
{
  return PRIVATE(this)->surrogatename;
}

/*!
  Returns the current surrogate path.
*/
const SoPath *
SoDragger::getSurrogatePartPickedPath(void) const
{
  return PRIVATE(this)->surrogatepath;
}

/*!
  Sets the staring point for the drag. \a point is usually a
  picked point from a SoRayPickAction.
*/
void
SoDragger::setStartingPoint(const SoPickedPoint * point)
{
  PRIVATE(this)->startingpoint = point->getPoint();
}

/*!
  Sets the starting point for a drag.
*/
void
SoDragger::setStartingPoint(const SbVec3f & point)
{
  PRIVATE(this)->startingpoint = point;
}

/*!
  Return the current view volume.
*/
const SbViewVolume &
SoDragger::getViewVolume(void)
{
  return PRIVATE(this)->viewvolume;
}

/*!
  Sets the current view volume.
*/
void
SoDragger::setViewVolume(const SbViewVolume & vv)
{
  PRIVATE(this)->viewvolume = vv;
}

/*!
  Returns the current viewport region.
*/
const SbViewportRegion &
SoDragger::getViewportRegion(void)
{
  return PRIVATE(this)->viewport;
}

/*!
  Sets the current viewport region.
*/
void
SoDragger::setViewportRegion(const SbViewportRegion & vp)
{
  PRIVATE(this)->viewport = vp;
}

/*!
  Return the current (most recent) SoHandleEventAction.
*/
SoHandleEventAction *
SoDragger::getHandleEventAction(void) const
{
  return PRIVATE(this)->eventaction;
}

/*!
  Stores a handle event action.
*/
void
SoDragger::setHandleEventAction(SoHandleEventAction * action)
{
  PRIVATE(this)->eventaction = action;
}

/*!
  This function is part of the original SGI Inventor 2.1 API, but has
  not been implemented in Coin as it looks like a function which
  should probably have been private in Open Inventor.
*/
void
SoDragger::setTempPathToThis(const SoPath *)
{
  COIN_OBSOLETED();
}

/*!
  Called when dragger starts grabbing events (mouse button down).
  Overload if you need to do something extra in your dragger.
  \sa grabEventCleanup()
*/
void
SoDragger::grabEventsSetup(void)
{
  assert(PRIVATE(this)->eventaction);
  PRIVATE(this)->eventaction->setGrabber(this);
}

/*!
  Called when dragger stops grabbing events (mouse button up).
  \sa grabEventSetup()
*/
void
SoDragger::grabEventsCleanup(void)
{
  assert(PRIVATE(this)->eventaction);
  PRIVATE(this)->eventaction->releaseGrabber();
}

/*!
  Examines the fields of the dragger, changes the matrix according to
  those fields and leaves the rest of the matrix as it was.  The
  following field names are supported: translation, scaleFactor,
  rotation and scaleOrientation.
*/
void
SoDragger::workFieldsIntoTransform(SbMatrix & matrix)
{
  SoSFVec3f * vecfield;
  SoSFRotation * rotfield;
  const SbVec3f * translation = NULL;
  const SbVec3f * scaleFactor = NULL;
  const SbRotation * rotation = NULL;
  const SbRotation * scaleOrientation = NULL;
  const SbVec3f * center = NULL;

  vecfield = coin_safe_cast<SoSFVec3f *>(this->getField("translation"));
  if (vecfield) translation = &vecfield->getValue();

  vecfield = coin_safe_cast<SoSFVec3f *>(this->getField("scaleFactor"));
  if (vecfield) scaleFactor = &vecfield->getValue();

  vecfield = coin_safe_cast<SoSFVec3f *>(this->getField("center"));
  if (vecfield) center = &vecfield->getValue();

  rotfield = coin_safe_cast<SoSFRotation *>(this->getField("rotation"));
  if (rotfield) rotation = &rotfield->getValue();

  rotfield = coin_safe_cast<SoSFRotation *>(this->getField("scaleOrientation"));
  if (rotfield) scaleOrientation = &rotfield->getValue();

  this->workValuesIntoTransform(matrix, translation, rotation,
                                scaleFactor, scaleOrientation, center);
}

/*!
  Controls the behaviour of the SbProjector.
*/
void
SoDragger::setFrontOnProjector(ProjectorFrontSetting val)
{
  PRIVATE(this)->frontonprojector = val;
}

/*!
  Returns the behaviour of the SbProjector.
*/
SoDragger::ProjectorFrontSetting
SoDragger::getFrontOnProjector(void) const
{
  return PRIVATE(this)->frontonprojector;
}

/*!
  Sets the minimum scale value all scale factors are clamped against.
  This is used in workFieldsIntoTransform(). The default value is 0.01
*/
void
SoDragger::setMinScale(float minscalearg)
{
  SoDragger::minscale = minscalearg;
}

/*!
  Returns the minimum scale value.
  \sa setMinScale()
*/
float
SoDragger::getMinScale(void)
{
  return SoDragger::minscale;
}

/*!
  Same as above, but pointers to values are supplied. If a pointer is
  \c NULL, the matrix value for that argument is used when
  reconstructing the matrix.
*/
void
SoDragger::workValuesIntoTransform(SbMatrix & matrix, const SbVec3f * translationptr, const SbRotation * rotationptr, const SbVec3f * scalefactorptr, const SbRotation * scaleorientationptr, const SbVec3f * centerptr)
{
  SbVec3f t, s;
  SbRotation r, so;
  if (centerptr) matrix.getTransform(t, r, s, so, *centerptr);
  else matrix.getTransform(t, r, s, so);

  if (translationptr) t = *translationptr;
  if (rotationptr) r = *rotationptr;
  if (scalefactorptr) s = *scalefactorptr;
  if (scaleorientationptr) so = *scaleorientationptr;

  if (centerptr) matrix.setTransform(t, r, s, so, *centerptr);
  else matrix.setTransform(t, r, s, so);
}

/*!
  Can be used when there is no scaleorientation. Faster than
  SoDragger::workValuesIntoTransform().
*/
void
SoDragger::getTransformFast(SbMatrix & matrix, SbVec3f & translation,
                            SbRotation & rotation, SbVec3f & scalefactor,
                            SbRotation & scaleorientation,
                            const SbVec3f & center)
{
  // FIXME: faster code not implemented, we just forward the call to
  // workValuesIntoTransform() anyway. 20011219 mortene.

  SoDragger::workValuesIntoTransform(matrix, &translation, &rotation,
                                     &scalefactor,
                                     &scaleorientation, &center);
}

/*!
  \overload
*/
void
SoDragger::getTransformFast(SbMatrix & matrix, SbVec3f & translation,
                            SbRotation & rotation, SbVec3f & scalefactor,
                            SbRotation & scaleorientation)
{
  // FIXME: faster code not implemented, we just forward the call to
  // workValuesIntoTransform() anyway. 20011219 mortene.

  SoDragger::workValuesIntoTransform(matrix, &translation, &rotation,
                                     &scalefactor,
                                     &scaleorientation, NULL);
}

/*!
  Returns \a matrix after \a translation has been appended.  If \a
  conversion != \c NULL it is used to transform \a translation into
  the space \a matrix is defined.
*/
SbMatrix
SoDragger::appendTranslation(const SbMatrix & matrix, const SbVec3f & translation, const SbMatrix * conversion)
{
  SbMatrix transform;
  transform.setTranslate(translation);
  if (conversion) {
    transform.multRight(*conversion);
    transform.multLeft(conversion->inverse());
  }
  SbMatrix res = matrix;
  return res.multLeft(transform);
}

/*!
  Returns \a matrix after \a scale and \a scalecenter has been
  appended.  If \a conversion != \c NULL it is used to transform scale
  into the space \a matrix is defined.
*/

SbMatrix
SoDragger::appendScale(const SbMatrix & matrix, const SbVec3f & scale, const SbVec3f & scalecenter, const SbMatrix * conversion)
{
  // Clamp the dragger values so that huge scaling in one go does not
  // work, and negative scale values are made positive. The explicit
  // casts are done to humour the HPUX aCC compiler, which will
  // otherwise say ``Template deduction failed to find a match for the
  // call to 'SbMax'''.  mortene.
  SbVec3f clampedscale;
  clampedscale[0] = SbMax(static_cast<float>(scale[0]), SoDragger::minscale);
  clampedscale[1] = SbMax(static_cast<float>(scale[1]), SoDragger::minscale);
  clampedscale[2] = SbMax(static_cast<float>(scale[2]), SoDragger::minscale);

  SbMatrix transform, tmp;
  // Calculate the scaled matrix without doing any testing if the
  // scale of the resulting matrix is above SoDragger::minscale.
  transform.setTranslate(-scalecenter);
  tmp.setScale(clampedscale);
  transform.multRight(tmp);
  tmp.setTranslate(scalecenter);
  transform.multRight(tmp);

  if (conversion) {
    transform.multRight(*conversion);
    transform.multLeft(conversion->inverse());
  }
  SbMatrix res = matrix;
  res.multLeft(transform);

  // Decompose the calculated matrix to be able to clamp the scale.
  SbVec3f t, s;
  SbRotation r, so;
  res.getTransform(t, r, s, so);

  // If the scale of the resulting matrix is smaller in x,y or z than
  // SoDragger::minscale, then the scale value has to be corrected. If
  // no correction is needed, the res matrix is returned without more
  // calculations.
  if (s[0] < SoDragger::minscale ||
      s[1] < SoDragger::minscale ||
      s[2] < SoDragger::minscale) {

    // Clamp the values of the scale to be SoDragger::minscale or
    // above.
    int i;
    for (i = 0; i < 3; i++) {
      if (s[i] < SoDragger::minscale) {
        s[i] = SoDragger::minscale;
      }
    }

    // Set the new transform with the adjusted scale
    transform.setTransform(t, r, s, so);
    // Just returning this matrix does not work as expected.
    // The dragger gets translated along the screen while trying
    // to scale below SoDragger::minscale. This is why further
    // processing is needed. We use the relationship between the
    // matrices to calculate the required scale vector.
    //
    // The scale matrix is found by the following equality:
    //
    // M = C^-1 * P^-1 * S * P * C * Mold
    //
    // By having the scale on one side and moving all other matrices
    // to the other side, we end up with this equation:
    //
    // S = P * C * M * Mold^-1 * C^-1 * P^-1
    //
    // Where S is the scale matrix which has a diagonal containing
    // the required scale vector.

    // Calculate the scale vector:
    transform.multRight(matrix.inverse());
    if (conversion) {
      transform.multLeft(*conversion);
      transform.multRight(conversion->inverse());
    }
    tmp.setTranslate(scalecenter);
    transform.multLeft(tmp);
    tmp.setTranslate(-scalecenter);
    transform.multRight(tmp);

    // SbMatrix does not have a getScale method, so just read the
    // values directly out of the matrix. This matrix does not only
    // contain scale. It might also have some other junk that we are
    // not interested in, so just read the scale values and ignore the
    // rest.
    SbVec3f newscale;
    newscale[0] = transform[0][0];
    newscale[1] = transform[1][1];
    newscale[2] = transform[2][2];

    // Calling appendScale here might cause a neverending loop if the
    // adjusted scale is just below SoDragger::minscale, so redo the
    // calculation of the resulting matrix without recursion.
    transform.setTranslate(-scalecenter);
    tmp.setScale(newscale);
    transform.multRight(tmp);
    tmp.setTranslate(scalecenter);
    transform.multRight(tmp);

    if (conversion) {
      transform.multRight(*conversion);
      transform.multLeft(conversion->inverse());
    }
    res = matrix;
    res.multLeft(transform);
  }

  return res;
}

/*!
  Appends \a rot, around \a rotcenter, to \a matrix. If \a conversion
  is != \c NULL, this is used to move the rotation into that
  coordinate systems before appending the rotation.
*/
SbMatrix
SoDragger::appendRotation(const SbMatrix & matrix, const SbRotation & rot, const SbVec3f & rotcenter, const SbMatrix * conversion)
{
  SbMatrix transform, tmp;
  transform.setTranslate(-rotcenter);
  tmp.setRotate(rot);
  transform.multRight(tmp);
  tmp.setTranslate(rotcenter);
  transform.multRight(tmp);
  if (conversion) {
    transform.multRight(*conversion);
    transform.multLeft(conversion->inverse());
  }
  transform.multRight(matrix);
  return transform;
}

/*!
  Returns the position of the locater.
*/
SbVec2f
SoDragger::getNormalizedLocaterPosition(void)
{
  if (PRIVATE(this)->currentevent) {
    return PRIVATE(this)->currentevent->getNormalizedPosition(PRIVATE(this)->viewport);
  }
#if COIN_DEBUG && 1 // debug
  SoDebugError::postInfo("SoDragger::getLocaterPosition",
                         "current event is not set");
#endif // debug
  return SbVec2f(0, 0);
}

/*!
  \overload
*/
SbVec2s
SoDragger::getLocaterPosition(void)
{
  if (PRIVATE(this)->currentevent) {
    return PRIVATE(this)->currentevent->getPosition();
  }
#if COIN_DEBUG && 1 // debug
  SoDebugError::postInfo("SoDragger::getLocaterPosition",
                         "current event is not set");
#endif // debug
  return SbVec2s(0, 0);
}

/*!
  Returns the position when mouse button 1 was pressed.
*/
SbVec2s
SoDragger::getStartLocaterPosition(void) const
{
  return PRIVATE(this)->startlocaterpos;
}

/*!
  The start locater position is automatically set when mouse button 1 goes
  down, but subclasses can use this method to reset the value.
*/
void
SoDragger::setStartLocaterPosition(SbVec2s pos)
{
  PRIVATE(this)->startlocaterpos = pos;
}

/*!
  Checks if the mouse pointer has been moved enough after the end-user
  hit a constraint mode key (which is typically \c SHIFT, sometimes
  also \c CTRL for the built-in draggers) that we should act upon and
  decide which direction the constraint should be set to.
*/
SbBool
SoDragger::isAdequateConstraintMotion(void)
{
  SbVec2s delta =
    this->getStartLocaterPosition() -
    this->getLocaterPosition();

  // The cast is done to avoid HPUX aCC failing with an "ambiguity
  // error", as sqrt() can be either "long double sqrt(long double)"
  // or "float sqrt(float)".  mortene.
  double len = sqrt(double(delta[0]*delta[0] + delta[1]*delta[1]));

  if (len >= static_cast<double>(PRIVATE(this)->mingesture)) return TRUE;
  return FALSE;
}

/*!
  Checks if \a pickpath contains \a surrogatepath and returns \c TRUE
  if the tail of \a surrogatepath is before any dragger in \a
  pickpath.
*/
SbBool
SoDragger::shouldGrabBasedOnSurrogate(const SoPath * pickpath, const SoPath * surrogatepath)
{
  if (!pickpath->containsPath(surrogatepath)) return FALSE;

  const SoFullPath * pick = reclassify_cast<const SoFullPath *>(pickpath);
  const SoFullPath * surr = reclassify_cast<const SoFullPath *>(surrogatepath);

  SoNode * tail = surr->getTail();
  SoType draggertype = SoDragger::getClassTypeId();

  for (int i = pick->getLength()-1; i >= 0; i--) {
    SoNode * node = pick->getNode(i);
    if (node == tail) return TRUE;
    if (node->isOfType(draggertype))
      return FALSE;
  }
  return FALSE;
}

typedef struct {
  SbViewVolume vv;
} sodragger_vv_data;

static sodragger_vv_data * vvdata = NULL;

static SoCallbackAction::Response
sodragger_vv_cb(void * userdata, SoCallbackAction * action, const SoNode * COIN_UNUSED_ARG(node))
{
  sodragger_vv_data * data = static_cast<sodragger_vv_data *>(userdata);
  data->vv = SoViewVolumeElement::get(action->getState());
  return SoCallbackAction::CONTINUE;
}

extern "C" {

static void vv_data_cleanup(void)
{
  delete vvdata;
  vvdata = NULL;
}

} // extern "C"

/*!
  Store data about the current camera in the given action.
*/
void
SoDragger::setCameraInfo(SoAction * action)
{
  SoState * state = action->getState();
  PRIVATE(this)->viewport = SoViewportRegionElement::get(state);;
  PRIVATE(this)->viewvolume = SoViewVolumeElement::get(state);

  // FIXME: this is a temporary fix. We should really make
  // SoHandleEventAction support transformations so that the correct
  // view volume can be found directly. pederb, 2002-10-30

  if (PRIVATE(this)->draggercache && PRIVATE(this)->draggercache->path) {
    if (vvdata == NULL) {
      vvdata = new sodragger_vv_data;
      coin_atexit(static_cast<coin_atexit_f *>(vv_data_cleanup), CC_ATEXIT_NORMAL);
    }
    if (PRIVATE(this)->cbaction == NULL) {
      PRIVATE(this)->cbaction = new SoCallbackAction;
      PRIVATE(this)->cbaction->addPostCallback(SoCamera::getClassTypeId(), sodragger_vv_cb, vvdata);
    }
    PRIVATE(this)->cbaction->setViewportRegion(PRIVATE(this)->viewport);
    PRIVATE(this)->cbaction->apply(PRIVATE(this)->draggercache->path);
    PRIVATE(this)->viewvolume = vvdata->vv;
  }
}

// Documented in superclass. Overridden to detect picks on dragger.
void
SoDragger::handleEvent(SoHandleEventAction * action)
{
  const SoEvent * event = action->getEvent();

  if (this->isActive.getValue() || this->getActiveChildDragger()) {
    if (!action->getGrabber())
      this->updateDraggerCache(action->getCurPath());
    else
      this->updateDraggerCache(NULL);
  }
  // try child draggers first
  if (action->getGrabber() != this) {
    inherited::handleEvent(action);
  }
  // return if handled by a child
  if (action->isHandled()) return;

  // this is a special case, to be able to detect when somebody
  // clicks ctrl over a dragger. This has a special meaning for
  // some draggers, and it's the only time the otherEvent callbacks
  // are called when the dragger is not active.
  //
  // If you disable autoredraws via
  // SoGuiRenderArea::setAutoRedraw(FALSE) you must remember to add an
  // otherEventCallback if you want to schedule a redraw when ctrl is
  // tapped.

  if (!this->isActive.getValue() &&
      (SO_KEY_PRESS_EVENT(event, LEFT_CONTROL) ||
       SO_KEY_PRESS_EVENT(event, RIGHT_CONTROL))) {
    const SoPickedPoint * pp = action->getPickedPoint();
    if (pp && this->isPicked(pp->getPath())) {
      this->eventHandled(event, action);
      PRIVATE(this)->otherEventCB.invokeCallbacks(this);
    }
  }
  else if (SO_MOUSE_PRESS_EVENT(event, BUTTON1)) {
    const SoPickedPoint * pp = action->getPickedPoint();

    SbBool didpick = FALSE;

    if (pp && this->isPicked(pp->getPath())) didpick = TRUE;
    else if (pp) { // check surrogate paths
      SoPath * owner, * path;
      SbName name;
      if (this->isPathSurrogateInMySubgraph(pp->getPath(), owner, name, path)) {
        owner->ref();
        path->ref();
        if (this->shouldGrabBasedOnSurrogate(pp->getPath(), path)) {
          if (PRIVATE(this)->surrogateownerpath)
            PRIVATE(this)->surrogateownerpath->unref();
          PRIVATE(this)->surrogateownerpath = owner;
          PRIVATE(this)->surrogateownerpath->ref();
          if (PRIVATE(this)->surrogatepath)
            PRIVATE(this)->surrogatepath->unref();
          PRIVATE(this)->surrogatepath = path;
          PRIVATE(this)->surrogatepath->ref();
          PRIVATE(this)->surrogatename = name;
          didpick = TRUE;
        }
        owner->unref();
        path->unref();
      }
    }
    if (didpick) {
      if (!action->getGrabber())
        this->updateDraggerCache(action->getCurPath());
      else
        this->updateDraggerCache(NULL);

      this->isActive = TRUE;
      PRIVATE(this)->didmousemove = FALSE;
      this->setCameraInfo(action);
      this->setStartingPoint(pp);
      this->eventHandled(event, action);
      if (PRIVATE(this)->pickedpath)
        PRIVATE(this)->pickedpath->unref();
      PRIVATE(this)->pickedpath = pp->getPath();
      PRIVATE(this)->pickedpath->ref();

      PRIVATE(this)->startlocaterpos = event->getPosition();
      PRIVATE(this)->isgrabbing = FALSE;
      this->saveStartParameters();
      PRIVATE(this)->startCB.invokeCallbacks(this);
    }
  }
  else if (this->isActive.getValue() && SO_MOUSE_RELEASE_EVENT(event, BUTTON1)) {
    this->isActive = FALSE;
    if (PRIVATE(this)->didmousemove) {
      this->eventHandled(event, action);
      PRIVATE(this)->didmousemove = FALSE;
    }
    if (PRIVATE(this)->isgrabbing) this->grabEventsCleanup();
    if (PRIVATE(this)->pickedpath) {
      PRIVATE(this)->pickedpath->unref();
      PRIVATE(this)->pickedpath = NULL;
    }
    PRIVATE(this)->surrogatename = "";
    if (PRIVATE(this)->surrogateownerpath) {
      PRIVATE(this)->surrogateownerpath->unref();
      PRIVATE(this)->surrogateownerpath = NULL;
    }
    if (PRIVATE(this)->surrogatepath) {
      PRIVATE(this)->surrogatepath->unref();
      PRIVATE(this)->surrogatepath = NULL;
    }

    PRIVATE(this)->finishCB.invokeCallbacks(this);
    PRIVATE(this)->draggercache->truncatePath();
  }
  else if (this->isActive.getValue() && event->isOfType(SoLocation2Event::getClassTypeId())) {
    this->eventHandled(event, action);
    PRIVATE(this)->didmousemove = TRUE;
    PRIVATE(this)->motionCB.invokeCallbacks(this);
    if (!PRIVATE(this)->isgrabbing) {
      this->grabEventsSetup();
      PRIVATE(this)->isgrabbing = TRUE;
    }
  }
  else if (this->isActive.getValue()) {
    PRIVATE(this)->eventaction = action;
    PRIVATE(this)->currentevent = event;
    PRIVATE(this)->otherEventCB.invokeCallbacks(this);
  }
  if (!action->isHandled())
    inherited::handleEvent(action);
}

/*!
  \COININTERNAL
*/
void
SoDragger::transferMotion(SoDragger * child)
{
  SbMatrix childmatrix = child->getMotionMatrix();
  child->setMotionMatrix(SbMatrix::identity());
  child->transformMatrixLocalToWorld(childmatrix, childmatrix);
  this->transformMatrixWorldToLocal(childmatrix, childmatrix);

  SbMatrix matrix = this->getStartMotionMatrix();
  matrix.multLeft(childmatrix);
  this->setMotionMatrix(matrix);
}

/*!
  Sets whether dragger geometry should be ignored when calculating bbox.
*/
void
SoDragger::setIgnoreInBbox(SbBool val)
{
  PRIVATE(this)->ignoreinbbox = val;
}

/*!
  Returns whether dragger geometry should be ignored when calculating bbox.
*/
SbBool
SoDragger::isIgnoreInBbox(void)
{
  return PRIVATE(this)->ignoreinbbox;
}

// Documented in superclass. Overridden to ignore dragger bounding box
// if SoDragger::isIgnoreInBbox() is \c TRUE.
void
SoDragger::getBoundingBox(SoGetBoundingBoxAction * action)
{
  if (!PRIVATE(this)->ignoreinbbox) inherited::getBoundingBox(action);

}

/*!
  \COININTERNAL
*/
void
SoDragger::setActiveChildDragger(SoDragger * childdragger)
{
  PRIVATE(this)->activechilddragger = childdragger;
}

/*!
  \COININTERNAL
*/
SoDragger *
SoDragger::getActiveChildDragger(void) const
{
  return PRIVATE(this)->activechilddragger;
}

// Documented in superclass. Overridden to set default on
// SoDragger::isActive, SoDragger::motionMatrix and on common
// subdragger fields: translation, center, scaleFactor and rotation.
void
SoDragger::setDefaultOnNonWritingFields(void)
{
#define CHECK_DEFAULT(name, type, val) \
  do { \
    SoField * f = this->getField(name); \
    if (f) { \
      if ((coin_assert_cast<type *>(f))->getValue() == val) f->setDefault(TRUE); \
    } \
  } while (0)

  // check common subdragger fields
  CHECK_DEFAULT("translation", SoSFVec3f, SbVec3f(0.0f, 0.0f, 0.0f));
  CHECK_DEFAULT("center", SoSFVec3f, SbVec3f(0.0f, 0.0f, 0.0f));
  CHECK_DEFAULT("scaleFactor", SoSFVec3f, SbVec3f(1.0f, 1.0f, 1.0f));
  CHECK_DEFAULT("rotation", SoSFRotation, SbRotation::identity());

  // check isActive
  CHECK_DEFAULT("isActive", SoSFBool, FALSE);
#undef CHECK_DEFAULT

  this->motionMatrix.setDefault(TRUE);

  inherited::setDefaultOnNonWritingFields();
}

/*!
  \COININTERNAL
*/
void
SoDragger::childTransferMotionAndValueChangedCB(void * data, SoDragger * child)
{
  SoDragger * thisp = static_cast<SoDragger *>(data);
  child->removeValueChangedCallback(SoDragger::childTransferMotionAndValueChangedCB, thisp);
  thisp->transferMotion(child);
  child->addValueChangedCallback(SoDragger::childTransferMotionAndValueChangedCB, thisp);
}

/*!
  \COININTERNAL
*/
void
SoDragger::childValueChangedCB(void * data, SoDragger * COIN_UNUSED_ARG(child))
{
  SoDragger * thisp = static_cast<SoDragger *>(data);
  thisp->valueChanged();
}

/*!
  \COININTERNAL
*/
void
SoDragger::childStartCB(void * data, SoDragger * child)
{
  SoDragger * thisp = static_cast<SoDragger *>(data);
  // make child use the same projector epsilon as its parent
  child->setProjectorEpsilon(thisp->getProjectorEpsilon());
  thisp->saveStartParameters();
  thisp->setActiveChildDragger(child);
  PRIVATE(thisp)->startCB.invokeCallbacks(thisp);
}

/*!
  \COININTERNAL
*/
void
SoDragger::childMotionCB(void * data, SoDragger * COIN_UNUSED_ARG(child))
{
  SoDragger * thisp = static_cast<SoDragger *>(data);
  PRIVATE(thisp)->motionCB.invokeCallbacks(thisp);
}

/*!
  \COININTERNAL
*/
void
SoDragger::childFinishCB(void * data, SoDragger * COIN_UNUSED_ARG(child))
{
  SoDragger * thisp = static_cast<SoDragger *>(data);
  PRIVATE(thisp)->finishCB.invokeCallbacks(thisp);
  thisp->setActiveChildDragger(NULL);
  if (PRIVATE(thisp)->draggercache) PRIVATE(thisp)->draggercache->truncatePath();
}

/*!
  \COININTERNAL
*/
void
SoDragger::childOtherEventCB(void * data, SoDragger * child)
{
  SoDragger * thisp = static_cast<SoDragger *>(data);
  PRIVATE(thisp)->currentevent = child->pimpl->currentevent;
  PRIVATE(thisp)->eventaction = child->pimpl->eventaction;
  PRIVATE(thisp)->otherEventCB.invokeCallbacks(thisp);
}

// Returns whether path goes through this node (dragger is picked).
SbBool
SoDragger::isPicked(SoPath * path)
{
  // last dragger in path must be this one
  SoFullPath * fullpath = reclassify_cast<SoFullPath *>(path);

  int i = fullpath->findNode(this);
  if (i < 0) return FALSE;

  // if this is a composite dragger, the path will go through this
  // dragger, but it should not be regarded as picked if a child
  // dragger is picked.
  int n = fullpath->getLength();
  for (++i; i < n; i++) {
    SoNode * node = fullpath->getNode(i);
    if (node->isOfType(SoDragger::getClassTypeId())) return FALSE;
  }
  return TRUE;
}

void
SoDragger::eventHandled(const SoEvent * event, SoHandleEventAction * action)
{
  action->setHandled();
  PRIVATE(this)->currentevent = event;
  PRIVATE(this)->eventaction = action;
}

/*!
  \COININTERNAL
*/
void
SoDragger::updateDraggerCache(const SoPath * path)
{
  if (PRIVATE(this)->draggercache == NULL)
    PRIVATE(this)->draggercache = new SoDraggerCache(this);
  if (path) PRIVATE(this)->draggercache->update(reclassify_cast<const SoFullPath *>(path), path->findNode(this));
  else PRIVATE(this)->draggercache->updateMatrix();
}

#undef PRIVATE
#endif // HAVE_DRAGGERS