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
|
/*
* $Header: /home/dmarkle/xtrkcad-fork-cvs/xtrkcad/app/wlib/mswlib/mswdraw.c,v 1.6 2009-05-15 18:16:16 m_fischer Exp $
*/
#define _WIN32_WINNT 0x0500 /* for wheel mouse supposrt */
#include <windows.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#include <commdlg.h>
#include <math.h>
#include <winuser.h>
#ifdef WIN32
#define wFont_t tagLOGFONTA
#else
#define wFont_t tagLOGFONT
#endif
#include "mswint.h"
/*
*****************************************************************************
*
* Draw
*
*****************************************************************************
*/
static wBool_t initted = FALSE;
long wDebugFont;
static FARPROC oldDrawProc;
static long tmpOp = 0x990066;
static long setOp = 0x8800c6;
static long clrOp = 0xbb0226;
#define CENTERMARK_LENGTH 6
#ifdef SLOW
static wPos_t XPIX2INCH( wDraw_p d, int ix )
{
return (wPos_t)ix;
}
static wPos_t YPIX2INCH( wDraw_p d, int iy )
{
wPos_t y;
y = (wPos_t)(d->h-2-iy);
return y;
}
static int XINCH2PIX( wDraw_p d, wPos_t xx )
{
int ix;
ix = (int)(xx);
return ix;
}
static int YINCH2PIX( wDraw_p d, wPos_t y )
{
int iy;
iy = d->h-2 - (int)(y);
return iy;
}
static wPos_t XPIXELSTOINCH( wDraw_p d, int ix )
{
return (wPos_t)ix;
}
static wPos_t YPIXELSTOINCH( wDraw_p d, int iy )
{
return (wPos_t)iy;
}
#else
#define XPIX2INCH( d, ix ) \
((wPos_t)ix)
#define YPIX2INCH( d, iy ) \
((wPos_t)(d->h-2-iy))
#define XINCH2PIX( d, xx ) \
((int)(xx))
#define YINCH2PIX( d, y ) \
(d->h-2 - (int)(y))
#define XPIXELSTOINCH( d, ix ) \
((wPos_t)ix)
#define YPIXELSTOINCH( d, iy ) \
((wPos_t)iy)
#endif
/*
*****************************************************************************
*
* Basic Line Draw
*
*****************************************************************************
*/
static long noNegDrawArgs = -1;
static long noFlatEndCaps = 0;
void wDrawDelayUpdate(
wDraw_p d,
wBool_t delay )
{
}
/**
* Sets the proper pen and composition for the next drawing operation
*
*
* \param hDc IN device context
* \param d IN ???
* \param dw IN line width
* \param lt IN line type (dashed, solid, ...)
* \param dc IN color
* \param dopt IN ????
*/
static void setDrawMode(
HDC hDc,
wDraw_p d,
wDrawWidth dw,
wDrawLineType_e lt,
wDrawColor dc,
wDrawOpts dopt )
{
int mode;
HPEN hOldPen;
static wDraw_p d0;
static wDrawWidth dw0 = -1;
static wDrawLineType_e lt0 = (wDrawLineType_e)-1;
static wDrawColor dc0 = -1;
static int mode0 = -1;
static LOGBRUSH logBrush = { 0, 0, 0 };
DWORD penStyle;
if ( d->hasPalette ) {
int winPaletteClock = mswGetPaletteClock();
if ( d->paletteClock < winPaletteClock ) {
RealizePalette( hDc );
d->paletteClock = winPaletteClock;
}
}
if (dopt & wDrawOptTemp) {
mode = R2_NOTXORPEN;
} else {
mode = R2_COPYPEN;
}
SetROP2( hDc, mode );
if ( d == d0 && mode == mode0 && dw0 == dw && lt == lt0 && dc == dc0 )
return;
// make sure that the line width is at least 1!
if( !dw )
dw++;
d0 = d; mode0 = mode; dw0 = dw; lt0 = lt; dc0 = dc;
logBrush.lbColor = mswGetColor(d->hasPalette,dc);
if ( lt==wDrawLineSolid ) {
penStyle = PS_GEOMETRIC | PS_SOLID;
if ( noFlatEndCaps == FALSE )
penStyle |= PS_ENDCAP_FLAT;
d->hPen = ExtCreatePen( penStyle,
dw,
&logBrush,
0,
NULL );
/*colorPalette.palPalEntry[dc] );*/
} else {
d->hPen = CreatePen( PS_DOT, 0, mswGetColor( d->hasPalette, dc ) );
}
hOldPen = SelectObject( hDc, d->hPen );
DeleteObject( hOldPen );
}
static void setDrawBrush(
HDC hDc,
wDraw_p d,
wDrawColor dc,
wDrawOpts dopt )
{
HBRUSH hOldBrush;
static wDraw_p d0;
static wDrawColor dc0 = -1;
setDrawMode( hDc, d, 0, wDrawLineSolid, dc, dopt );
if ( d == d0 && dc == dc0 )
return;
d0 = d; dc0 = dc;
d->hBrush = CreateSolidBrush(
mswGetColor(d->hasPalette,dc) );
hOldBrush = SelectObject( hDc, d->hBrush );
DeleteObject( hOldBrush );
}
static void myInvalidateRect(
wDraw_p d,
RECT * prect )
{
if ( prect->top < 0 ) prect->top = 0;
if ( prect->left < 0 ) prect->left = 0;
if ( prect->bottom > d->h ) prect->bottom = d->h;
if ( prect->right > d->w ) prect->right = d->w;
InvalidateRect( d->hWnd, prect, FALSE );
}
static int clip0( POINT * p0, POINT * p1, wDraw_p d )
{
long int x0=p0->x, y0=p0->y, x1=p1->x, y1=p1->y;
long int dx, dy;
if ( x0<0 && x1<0 ) return 0;
if ( y0<0 && y1<0 ) return 0;
dx=x1-x0;
dy=y1-y0;
if ( x0 < 0 ) {
y0 -= x0*dy/dx;
x0 = 0;
}
if ( y0 < 0 ) {
if ( (x0 -= y0*dx/dy) < 0 ) return 0;
y0 = 0;
}
if ( x1 < 0 ) {
y1 -= x1*dy/dx;
x1 = 0;
}
if ( y1 < 0 ) {
if ( (x1 -= y1*dx/dy) < 0 ) return 0;
y1 = 0;
}
p0->x = (int)x0;
p0->y = (int)y0;
p1->x = (int)x1;
p1->y = (int)y1;
return 1;
}
void wDrawLine(
wDraw_p d,
wPos_t p0x,
wPos_t p0y,
wPos_t p1x,
wPos_t p1y,
wDrawWidth dw,
wDrawLineType_e lt,
wDrawColor dc,
wDrawOpts dopt )
{
POINT p0, p1;
RECT rect;
setDrawMode( d->hDc, d, dw, lt, dc, dopt );
p0.x = XINCH2PIX(d,p0x);
p0.y = YINCH2PIX(d,p0y);
p1.x = XINCH2PIX(d,p1x);
p1.y = YINCH2PIX(d,p1y);
if ( noNegDrawArgs>0 && !clip0( &p0, &p1, d ) )
return;
MoveTo( d->hDc, p0.x, p0.y );
LineTo( d->hDc, p1.x, p1.y );
if (d->hWnd) {
if (dw==0)
dw = 1;
dw++;
if (p0.y<p1.y) {
rect.top = p0.y-dw;
rect.bottom = p1.y+dw;
} else {
rect.top = p1.y-dw;
rect.bottom = p0.y+dw;
}
if (p0.x<p1.x) {
rect.left = p0.x-dw;
rect.right = p1.x+dw;
} else {
rect.left = p1.x-dw;
rect.right = p0.x+dw;
}
myInvalidateRect( d, &rect );
}
}
static double mswsin( double angle )
{
while (angle < 0.0) angle += 360.0;
while (angle >= 360.0) angle -= 360.0;
angle *= (M_PI*2.0)/360.0;
return sin( angle );
}
static double mswcos( double angle )
{
while (angle < 0.0) angle += 360.0;
while (angle >= 360.0) angle -= 360.0;
angle *= (M_PI*2.0)/360.0;
return cos( angle );
}
static double mswasin( double x, double h )
{
double angle;
angle = asin( x/h );
angle /= (M_PI*2.0)/360.0;
return angle;
}
/**
* Draw an arc around a specified center
*
* \param d IN ?
* \param px, py IN center of arc
* \param r IN radius
* \param a0, a1 IN start and end angle
* \param drawCenter draw marking for center
* \param dw line width
* \param lt line type
* \param dc color
* \param dopt ?
*/
void wDrawArc(
wDraw_p d,
wPos_t px,
wPos_t py,
wPos_t r,
double a0,
double a1,
int drawCenter,
wDrawWidth dw,
wDrawLineType_e lt,
wDrawColor dc,
wDrawOpts dopt )
{
int i, cnt;
POINT p0, p1, ps, pe, pp0, pp1, pp2, pc;
double psx, psy, pex, pey, len, aa;
RECT rect;
int needMoveTo;
wBool_t fakeArc = FALSE;
len = a1/360.0 * (2 * M_PI) * r;
if (len < 3)
return;
p0.x = XINCH2PIX(d,px-r);
p0.y = YINCH2PIX(d,py+r)+1;
p1.x = XINCH2PIX(d,px+r);
p1.y = YINCH2PIX(d,py-r)+1;
pex = px + r * mswsin(a0);
pey = py + r * mswcos(a0);
psx = px + r * mswsin(a0+a1);
psy = py + r * mswcos(a0+a1);
/*pointOnCircle( &pe, p, r, a0 );
pointOnCircle( &ps, p, r, a0+a1 );*/
ps.x = XINCH2PIX(d,(wPos_t)psx);
ps.y = YINCH2PIX(d,(wPos_t)psy);
pe.x = XINCH2PIX(d,(wPos_t)pex);
pe.y = YINCH2PIX(d,(wPos_t)pey);
setDrawMode( d->hDc, d, dw, lt, dc, dopt );
if (dw == 0)
dw = 1;
if (r>4096) {
/* The book says 32K but experience says otherwise */
fakeArc = TRUE;
}
if ( noNegDrawArgs > 0 ) {
if ( p0.x < 0 || p0.y < 0 || p1.x < 0 || p1.y < 0 )
fakeArc = TRUE;
}
if ( fakeArc ) {
cnt = (int)a1;
if ( cnt <= 0 ) cnt = 1;
if ( cnt > 360 ) cnt = 360;
aa = a1 / cnt;
psx = px + r * mswsin(a0);
psy = py + r * mswcos(a0);
pp0.x = XINCH2PIX( d, (wPos_t)psx );
pp0.y = YINCH2PIX( d, (wPos_t)psy );
needMoveTo = TRUE;
for ( i=0; i<cnt; i++ ) {
a0 += aa;
psx = px + r * mswsin(a0);
psy = py + r * mswcos(a0);
pp2.x = pp1.x = XINCH2PIX( d, (wPos_t)psx );
pp2.y = pp1.y = YINCH2PIX( d, (wPos_t)psy );
if ( clip0( &pp0, &pp1, d ) ) {
if (needMoveTo) {
MoveTo( d->hDc, pp0.x, pp0.y );
needMoveTo = FALSE;
}
LineTo( d->hDc, pp1.x, pp1.y );
} else {
needMoveTo = TRUE;
}
pp0.x = pp2.x; pp0.y = pp2.y;
}
} else {
if ( a0 == 0.0 && a1 == 360.0 ) {
Arc( d->hDc, p0.x, p1.y, p1.x, p0.y, ps.x, p0.y-1, pe.x, p1.y-1 );
Arc( d->hDc, p0.x, p1.y, p1.x, p0.y, ps.x, p1.y-1, pe.x, p0.y-1 );
} else {
Arc( d->hDc, p0.x, p1.y, p1.x, p0.y, ps.x, ps.y, pe.x, pe.y );
}
}
// should the center of the arc be drawn?
if( drawCenter ) {
// calculate the center coordinates
pc.x = XINCH2PIX( d, px );
pc.y = YINCH2PIX( d, py );
// now draw the crosshair
MoveTo( d->hDc, pc.x - CENTERMARK_LENGTH/2, pc.y );
LineTo( d->hDc, pc.x + CENTERMARK_LENGTH/2, pc.y );
MoveTo( d->hDc, pc.x, pc.y - CENTERMARK_LENGTH/2 );
LineTo( d->hDc, pc.x, pc.y + CENTERMARK_LENGTH/2 );
// invalidate the area of the crosshair
rect.top = pc.y - CENTERMARK_LENGTH / 2 - 1;
rect.bottom = pc.y + CENTERMARK_LENGTH / 2 + 1;
rect.left = pc.x - CENTERMARK_LENGTH / 2 - 1;
rect.right = pc.x + CENTERMARK_LENGTH / 2 + 1;
myInvalidateRect( d, &rect );
}
if (d->hWnd) {
dw++;
a1 += a0;
if (a1>360.0)
rect.top = p0.y;
else
rect.top = min(pe.y,ps.y);
if (a1>(a0>180?360.0:0.0)+180)
rect.bottom = p1.y;
else
rect.bottom = max(pe.y,ps.y);
if (a1>(a0>270?360.0:0.0)+270)
rect.left = p0.x;
else
rect.left = min(pe.x,ps.x);
if (a1>(a0>90?360.0:0.0)+90)
rect.right = p1.x;
else
rect.right = max(pe.x,ps.x);
rect.top -= dw;
rect.bottom += dw;
rect.left -= dw;
rect.right += dw;
myInvalidateRect( d, &rect );
}
}
void wDrawPoint(
wDraw_p d,
wPos_t px,
wPos_t py,
wDrawColor dc,
wDrawOpts dopt )
{
POINT p0;
RECT rect;
p0.x = XINCH2PIX(d,px);
p0.y = YINCH2PIX(d,py);
if ( p0.x < 0 || p0.y < 0 )
return;
if ( p0.x >= d->w || p0.y >= d->h )
return;
setDrawMode( d->hDc, d, 0, wDrawLineSolid, dc, dopt );
SetPixel( d->hDc, p0.x, p0.y, mswGetColor(d->hasPalette,dc) /*colorPalette.palPalEntry[dc]*/ );
if (d->hWnd) {
rect.top = p0.y-1;
rect.bottom = p0.y+1;
rect.left = p0.x-1;
rect.right = p0.x+1;
myInvalidateRect( d, &rect );
}
}
/*
*****************************************************************************
*
* Fonts
*
*****************************************************************************
*/
static LOGFONT logFont = {
/* Initial default values */
-24, 0, /* H, W */
0, /* A */
0,
FW_REGULAR,
0, 0, 0,/* I, U, SO */
ANSI_CHARSET,
0, /* OP */
0, /* CP */
0, /* Q */
0, /* P&F */
"Arial" };
static LOGFONT timesFont[2][2] = {
{ {
/* Initial default values */
0, 0, /* H, W */
0, /* A */
0,
FW_REGULAR,
0, 0, 0,/* I, U, SO */
ANSI_CHARSET,
0, /* OP */
0, /* CP */
0, /* Q */
0, /* P&F */
"Times" },
{
/* Initial default values */
0, 0, /* H, W */
0, /* A */
0,
FW_REGULAR,
1, 0, 0,/* I, U, SO */
ANSI_CHARSET,
0, /* OP */
0, /* CP */
0, /* Q */
0, /* P&F */
"Times" } },
{ {
/* Initial default values */
0, 0, /* H, W */
0, /* A */
0,
FW_BOLD,
0, 0, 0,/* I, U, SO */
ANSI_CHARSET,
0, /* OP */
0, /* CP */
0, /* Q */
0, /* P&F */
"Times" },
{
/* Initial default values */
0, 0, /* H, W */
0, /* A */
0,
FW_BOLD,
1, 0, 0,/* I, U, SO */
ANSI_CHARSET,
0, /* OP */
0, /* CP */
0, /* Q */
0, /* P&F */
"Times" } } };
static LOGFONT helvFont[2][2] = {
{ {
/* Initial default values */
0, 0, /* H, W */
0, /* A */
0,
FW_REGULAR,
0, 0, 0,/* I, U, SO */
ANSI_CHARSET,
0, /* OP */
0, /* CP */
0, /* Q */
0, /* P&F */
"Arial" },
{
/* Initial default values */
0, 0, /* H, W */
0, /* A */
0,
FW_REGULAR,
1, 0, 0,/* I, U, SO */
ANSI_CHARSET,
0, /* OP */
0, /* CP */
0, /* Q */
0, /* P&F */
"Arial" } },
{ {
/* Initial default values */
0, 0, /* H, W */
0, /* A */
0,
FW_BOLD,
0, 0, 0,/* I, U, SO */
ANSI_CHARSET,
0, /* OP */
0, /* CP */
0, /* Q */
0, /* P&F */
"Arial" },
{
/* Initial default values */
0, 0, /* H, W */
0, /* A */
0,
FW_BOLD,
1, 0, 0,/* I, U, SO */
ANSI_CHARSET,
0, /* OP */
0, /* CP */
0, /* Q */
0, /* P&F */
"Hevletica" } } };
void mswFontInit( void )
{
const char * face;
long size;
face = wPrefGetString( "msw window font", "face" );
wPrefGetInteger( "msw window font", "size", &size, -24 );
if (face) {
strncpy( logFont.lfFaceName, face, LF_FACESIZE );
}
logFont.lfHeight = (int)size;
}
static CHOOSEFONT chooseFont;
static wFontSize_t fontSize = 18;
static double fontFactor = 1.0;
static void doChooseFont( void )
{
int rc;
memset( &chooseFont, 0, sizeof chooseFont );
chooseFont.lStructSize = sizeof chooseFont;
chooseFont.hwndOwner = mswHWnd;
chooseFont.lpLogFont = &logFont;
chooseFont.Flags = CF_SCREENFONTS|CF_SCALABLEONLY|CF_INITTOLOGFONTSTRUCT;
chooseFont.nFontType = SCREEN_FONTTYPE;
rc = ChooseFont( &chooseFont );
if (rc) {
fontSize = (wFontSize_t)(-logFont.lfHeight * 72) / 96.0 / fontFactor;
if (fontSize < 1)
fontSize = 1;
wPrefSetString( "msw window font", "face", logFont.lfFaceName );
wPrefSetInteger( "msw window font", "size", logFont.lfHeight );
}
}
static int computeFontSize( wDraw_p d, double siz )
{
int ret;
siz = (siz * d->DPI) / 72.0;
ret = (int)(siz * fontFactor);
if (ret < 1)
ret = 1;
return -ret;
}
void wDrawGetTextSize(
wPos_t *w,
wPos_t *h,
wPos_t *d,
wDraw_p bd,
const char * text,
wFont_p fp,
double siz )
{
int x, y;
HFONT newFont, prevFont;
DWORD extent;
int oldLfHeight;
TEXTMETRIC textMetric;
if (fp == NULL)
fp = &logFont;
fp->lfEscapement = 0;
oldLfHeight = fp->lfHeight;
fp->lfHeight = computeFontSize( bd, siz );
fp->lfWidth = 0;
newFont = CreateFontIndirect( fp );
prevFont = SelectObject( bd->hDc, newFont );
extent = GetTextExtent( bd->hDc, CAST_AWAY_CONST text, strlen(text) );
GetTextMetrics(bd->hDc, &textMetric);
x = LOWORD(extent);
y = HIWORD(extent);
*w = XPIXELSTOINCH( bd, x );
*h = YPIXELSTOINCH( bd, y );
*d = YPIXELSTOINCH(bd, textMetric.tmDescent );
SelectObject( bd->hDc, prevFont );
DeleteObject( newFont );
fp->lfHeight = oldLfHeight;
}
void wDrawString(
wDraw_p d,
wPos_t px,
wPos_t py,
double angle,
const char * text,
wFont_p fp,
double siz,
wDrawColor dc,
wDrawOpts dopts )
{
int x, y;
HFONT newFont, prevFont;
HDC newDc;
HBITMAP oldBm, newBm;
DWORD extent;
int w, h;
RECT rect;
int oldLfHeight;
if (fp == NULL)
fp = &logFont;
oldLfHeight = fp->lfHeight;
fp->lfEscapement = (int)(angle*10.0);
fp->lfHeight = computeFontSize( d, siz );
fp->lfWidth = 0;
newFont = CreateFontIndirect( fp );
x = XINCH2PIX(d,px) + (int)(mswsin(angle)*fp->lfHeight-0.5);
y = YINCH2PIX(d,py) + (int)(mswcos(angle)*fp->lfHeight-0.5);
if ( noNegDrawArgs > 0 && ( x < 0 || y < 0 ) )
return;
if (dopts & wDrawOptTemp) {
setDrawMode( d->hDc, d, 0, wDrawLineSolid, dc, dopts );
newDc = CreateCompatibleDC( d->hDc );
prevFont = SelectObject( newDc, newFont );
extent = GetTextExtent( newDc, CAST_AWAY_CONST text, strlen(text) );
w = LOWORD(extent);
h = HIWORD(extent);
if ( h > w ) w = h;
newBm = CreateCompatibleBitmap( d->hDc, w*2, w*2 );
oldBm = SelectObject( newDc, newBm );
rect.top = rect.left = 0;
rect.bottom = rect.right = w*2;
FillRect( newDc, &rect, GetStockObject(WHITE_BRUSH) );
TextOut( newDc, w, w, text, strlen(text) );
BitBlt( d->hDc, x-w, y-w, w*2, w*2, newDc, 0, 0, tmpOp );
SelectObject( newDc, oldBm );
DeleteObject( newBm );
SelectObject( newDc, prevFont );
DeleteDC( newDc );
if (d->hWnd) {
rect.top = y-(w+1);
rect.bottom = y+(w+1);
rect.left = x-(w+1);
rect.right = x+(w+1);
myInvalidateRect( d, &rect );
}
#ifdef LATER
/* KLUDGE: Can't Invert text, so we just draw a bow - a pox on windows*/
MoveTo( d->hDc, x, y );
LineTo( d->hDc, x+w, y );
LineTo( d->hDc, x+w, y+h );
LineTo( d->hDc, x, y+h );
LineTo( d->hDc, x, y );
#endif
} else {
prevFont = SelectObject( d->hDc, newFont );
SetBkMode( d->hDc, TRANSPARENT );
if (dc != wDrawColorBlack) {
COLORREF old;
old = SetTextColor( d->hDc, mswGetColor(d->hasPalette,dc)/*colorPalette.palPalEntry[dc]*/ );
TextOut( d->hDc, x, y, text, strlen(text) );
SetTextColor( d->hDc, old );
} else
TextOut( d->hDc, x, y, text, strlen(text) );
extent = GetTextExtent( d->hDc, CAST_AWAY_CONST text, strlen(text) );
SelectObject( d->hDc, prevFont );
w = LOWORD(extent);
h = HIWORD(extent);
if (d->hWnd) {
rect.top = y-(w+h+1);
rect.bottom = y+(w+h+1);
rect.left = x-(w+h+1);
rect.right = x+(w+h+1);
myInvalidateRect( d, &rect );
}
}
DeleteObject( newFont );
fp->lfHeight = oldLfHeight;
}
static const char * wCurFont( void )
{
return logFont.lfFaceName;
}
void wInitializeFonts()
{
}
wFont_p wStandardFont( int family, wBool_t bold, wBool_t italic )
{
if (family == F_TIMES)
return ×Font[bold][italic];
else if (family == F_HELV)
return &helvFont[bold][italic];
else
return NULL;
}
void wSelectFont( const char * title )
{
doChooseFont();
}
wFontSize_t wSelectedFontSize( void )
{
return fontSize;
}
void wSetSelectedFontSize(int size)
{
fontSize = (wFontSize_t)size;
}
/*
*****************************************************************************
*
* Misc
*
*****************************************************************************
*/
void wDrawFilledRectangle(
wDraw_p d,
wPos_t px,
wPos_t py,
wPos_t sx,
wPos_t sy,
wDrawColor color,
wDrawOpts opts )
{
RECT rect;
if (d == NULL)
return;
setDrawBrush( d->hDc, d, color, opts );
rect.left = XINCH2PIX(d,px);
rect.right = XINCH2PIX(d,px+sx);
rect.top = YINCH2PIX(d,py+sy);
rect.bottom = YINCH2PIX(d,py);
if ( rect.right < 0 ||
rect.bottom < 0 )
return;
if ( rect.left < 0 )
rect.left = 0;
if ( rect.top < 0 )
rect.top = 0;
if ( rect.left > d->w ||
rect.top > d->h )
return;
if ( rect.right > d->w )
rect.right = d->w;
if ( rect.bottom > d->h )
rect.bottom = d->h;
Rectangle( d->hDc, rect.left, rect.top, rect.right, rect.bottom );
if (d->hWnd) {
rect.top--;
rect.left--;
rect.bottom++;
rect.right++;
myInvalidateRect( d, &rect );
}
}
#ifdef DRAWFILLPOLYLOG
static FILE * logF;
#endif
static int wFillPointsMax = 0;
static POINT * wFillPoints;
static void addPoint(
int * pk,
POINT * pp,
RECT * pr )
{
#ifdef DRAWFILLPOLYLOG
fprintf( logF, " q[%d] = {%d,%d}\n", *pk, pp->x, pp->y );
#endif
if ( *pk > 0 &&
wFillPoints[(*pk)-1].x == pp->x && wFillPoints[(*pk)-1].y == pp->y )
return;
wFillPoints[ (*pk)++ ] = *pp;
if (pp->x<pr->left)
pr->left = pp->x;
if (pp->x>pr->right)
pr->right = pp->x;
if (pp->y<pr->top)
pr->top = pp->y;
if (pp->y>pr->bottom)
pr->bottom = pp->y;
}
void wDrawFilledPolygon(
wDraw_p d,
wPos_t p[][2],
int cnt,
wDrawColor color,
wDrawOpts opts )
{
RECT rect;
int i, k;
POINT p0, p1, q0, q1;
static POINT zero = { 0, 0 };
wBool_t p1Clipped;
if (d == NULL)
return;
if (cnt*2 > wFillPointsMax) {
wFillPoints = realloc( wFillPoints, cnt * 2 * sizeof *(POINT*)NULL );
wFillPointsMax = cnt*2;
}
setDrawBrush( d->hDc, d, color, opts );
p1.x = rect.left = rect.right = XINCH2PIX(d,p[cnt-1][0]-1);
p1.y = rect.top = rect.bottom = YINCH2PIX(d,p[cnt-1][1]+1);
#ifdef DRAWFILLPOLYLOG
logF = fopen( "log.txt", "a" );
fprintf( logF, "\np[%d] = {%d,%d}\n", cnt-1, p1.x, p1.y );
#endif
p1Clipped = FALSE;
for ( i=k=0; i<cnt; i++ ) {
p0 = p1;
p1.x = XINCH2PIX(d,p[i][0]-1);
p1.y = YINCH2PIX(d,p[i][1]+1);
#ifdef DRAWFILLPOLYLOG
fprintf( logF, "p[%d] = {%d,%d}\n", i, p1.x, p1.y );
#endif
q0 = p0;
q1 = p1;
if ( clip0( &q0, &q1, NULL ) ) {
#ifdef DRAWFILLPOLYLOG
fprintf( logF, " clip( {%d,%d} {%d,%d} ) = {%d,%d} {%d,%d}\n", p0.x, p0.y, p1.x, p1.y, q0.x, q0.y, q1.x, q1.y );
#endif
if ( q0.x != p0.x || q0.y != p0.y ) {
if ( k > 0 && ( q0.x > q0.y ) != ( wFillPoints[k-1].x > wFillPoints[k-1].y ) )
addPoint( &k, &zero, &rect );
addPoint( &k, &q0, &rect );
}
addPoint( &k, &q1, &rect );
p1Clipped = ( q1.x != p1.x || q1.y != p1.y );
}
}
if ( p1Clipped &&
( wFillPoints[k-1].x > wFillPoints[k-1].y ) != ( wFillPoints[0].x > wFillPoints[0].y ) )
addPoint( &k, &zero, &rect );
#ifdef DRAWFILLPOLYLOG
fflush( logF );
fclose( logF );
#endif
if ( k <= 2 )
return;
Polygon( d->hDc, wFillPoints, k );
if (d->hWnd) {
rect.top--;
rect.left--;
rect.bottom++;
rect.right++;
myInvalidateRect( d, &rect );
}
}
#define MAX_FILLCIRCLE_POINTS (30)
void wDrawFilledCircle(
wDraw_p d,
wPos_t x,
wPos_t y,
wPos_t r,
wDrawColor color,
wDrawOpts opts )
{
POINT p0, p1;
RECT rect;
static wPos_t circlePts[MAX_FILLCIRCLE_POINTS][2];
int inx, cnt;
double dang;
p0.x = XINCH2PIX(d,x-r);
p0.y = YINCH2PIX(d,y+r)+1;
p1.x = XINCH2PIX(d,x+r);
p1.y = YINCH2PIX(d,y-r)+1;
setDrawBrush( d->hDc, d, color, opts );
if ( noNegDrawArgs > 0 && ( p0.x < 0 || p0.y < 0 ) ) {
if ( r > MAX_FILLCIRCLE_POINTS )
cnt = MAX_FILLCIRCLE_POINTS;
else if ( r > 8 )
cnt = r;
else
cnt = 8;
dang = 360.0/cnt;
for ( inx=0; inx<cnt; inx++ ) {
circlePts[inx][0] = x + (int)(r * mswcos( inx*dang ) + 0.5 );
circlePts[inx][1] = y + (int)(r * mswsin( inx*dang ) + 0.5 );
}
wDrawFilledPolygon( d, circlePts, cnt, color, opts );
} else {
Ellipse( d->hDc, p0.x, p0.y, p1.x, p1.y );
if (d->hWnd) {
rect.top = p0.y;
rect.bottom = p1.y;
rect.left = p0.x;
rect.right = p1.x;
myInvalidateRect( d, &rect );
}
}
}
/*
*****************************************************************************
*
* Misc
*
*****************************************************************************
*/
void wDrawSaveImage(
wDraw_p bd )
{
if ( bd->hBmBackup ) {
SelectObject( bd->hDcBackup, bd->hBmBackupOld );
DeleteObject( bd->hBmBackup );
bd->hBmBackup = (HBITMAP)0;
}
if ( bd->hDcBackup == (HDC)0 )
bd->hDcBackup = CreateCompatibleDC( bd->hDc );
bd->hBmBackup = CreateCompatibleBitmap( bd->hDc, bd->w, bd->h );
bd->hBmBackupOld = SelectObject( bd->hDcBackup, bd->hBmBackup );
BitBlt( bd->hDcBackup, 0, 0, bd->w, bd->h, bd->hDc, 0, 0, SRCCOPY );
}
void wDrawRestoreImage(
wDraw_p bd )
{
if ( bd->hBmBackup == (HBITMAP)0 ) {
mswFail( "wDrawRestoreImage: hBmBackup == 0" );
return;
}
BitBlt( bd->hDc, 0, 0, bd->w, bd->h, bd->hDcBackup, 0, 0, SRCCOPY );
InvalidateRect( bd->hWnd, NULL, FALSE );
}
void wDrawClear( wDraw_p d )
{
RECT rect;
SetROP2( d->hDc, R2_WHITE );
Rectangle( d->hDc, 0, 0, d->w, d->h );
if (d->hWnd) {
rect.top = 0;
rect.bottom = d->h;
rect.left = 0;
rect.right = d->w;
InvalidateRect( d->hWnd, &rect, FALSE );
}
}
void wDrawSetSize(
wDraw_p d,
wPos_t width,
wPos_t height )
{
d->w = width;
d->h = height;
if (!SetWindowPos( d->hWnd, HWND_TOP, 0, 0,
d->w, d->h, SWP_NOMOVE|SWP_NOZORDER)) {
mswFail("wDrawSetSize: SetWindowPos");
}
/*wRedraw( d );*/
}
void wDrawGetSize(
wDraw_p d,
wPos_t * width,
wPos_t * height )
{
*width = d->w-2;
*height = d->h-2;
}
void * wDrawGetContext( wDraw_p d )
{
return d->data;
}
double wDrawGetDPI( wDraw_p d )
{
return d->DPI;
}
double wDrawGetMaxRadius( wDraw_p d )
{
return 4096.0;
}
void wDrawClip(
wDraw_p d,
wPos_t x,
wPos_t y,
wPos_t w,
wPos_t h )
{
int ix0, iy0, ix1, iy1;
HRGN hRgnClip;
ix0 = XINCH2PIX(d,x);
iy0 = YINCH2PIX(d,y);
ix1 = XINCH2PIX(d,x+w);
iy1 = YINCH2PIX(d,y+h);
/* Note: Ydim is upside down so iy1<iy0 */
hRgnClip = CreateRectRgn( ix0, iy1, ix1, iy0 );
SelectClipRgn( d->hDc, hRgnClip );
DeleteObject( hRgnClip );
}
void wRedraw( wDraw_p d )
{
wDrawClear( d );
if (d->drawRepaint)
d->drawRepaint( d, d->data, 0, 0 );
}
/*
*****************************************************************************
*
* BitMap
*
*****************************************************************************
*/
struct wDrawBitMap_t {
wDrawBitMap_p next;
wPos_t x;
wPos_t y;
wPos_t w;
wPos_t h;
char * bmx;
wDrawColor color;
HBITMAP bm;
};
wDrawBitMap_p bmRoot = NULL;
void wDrawBitMap(
wDraw_p d,
wDrawBitMap_p bm,
wPos_t px,
wPos_t py,
wDrawColor dc,
wDrawOpts dopt )
{
HDC bmDc, hDc;
HBITMAP oldBm;
DWORD mode;
int x0, y0;
RECT rect;
x0 = XINCH2PIX(d,px-bm->x);
y0 = YINCH2PIX(d,py-bm->y+bm->h);
#ifdef LATER
if ( noNegDrawArgs > 0 && ( x0 < 0 || y0 < 0 ) )
return;
#endif
if (dopt & wDrawOptTemp) {
mode = tmpOp;
} else if (dc == wDrawColorWhite) {
mode = clrOp;
dc = wDrawColorBlack;
} else {
mode = setOp;
}
if ( bm->color != dc ) {
if ( bm->bm )
DeleteObject( bm->bm );
bm->bm = mswCreateBitMap( mswGetColor(d->hasPalette,dc) /*colorPalette.palPalEntry[dc]*/, RGB( 255, 255, 255 ),
RGB( 255, 255, 255 ), bm->w, bm->h, bm->bmx );
bm->color = dc;
}
if ( (dopt & wDrawOptNoClip) != 0 &&
( px < 0 || px >= d->w || py < 0 || py >= d->h ) ) {
x0 += d->x;
y0 += d->y;
hDc = GetDC( ((wControl_p)(d->parent))->hWnd );
bmDc = CreateCompatibleDC( hDc );
oldBm = SelectObject( bmDc, bm->bm );
BitBlt( hDc, x0, y0, bm->w, bm->h, bmDc, 0, 0, tmpOp );
SelectObject( bmDc, oldBm );
DeleteDC( bmDc );
ReleaseDC( ((wControl_p)(d->parent))->hWnd, hDc );
return;
}
bmDc = CreateCompatibleDC( d->hDc );
setDrawMode( d->hDc, d, 0, wDrawLineSolid, dc, dopt );
oldBm = SelectObject( bmDc, bm->bm );
BitBlt( d->hDc, x0, y0, bm->w, bm->h, bmDc, 0, 0, mode );
SelectObject( bmDc, oldBm );
DeleteDC( bmDc );
if (d->hWnd) {
rect.top = y0-1;
rect.bottom = rect.top+bm->h+1;
rect.left = x0-1;
rect.right = rect.left+bm->w+1;
myInvalidateRect( d, &rect );
}
}
wDrawBitMap_p wDrawBitMapCreate(
wDraw_p d,
int w,
int h,
int x,
int y,
const char * bits )
{
wDrawBitMap_p bm;
int bmSize = ((w+7)/8) * h;
bm = (wDrawBitMap_p)malloc( sizeof *bm );
if (bmRoot == NULL) {
bmRoot = bm;
bm->next = NULL;
} else {
bm->next = bmRoot;
bmRoot = bm;
}
bm->x = x;
bm->y = y;
bm->w = w;
bm->h = h;
bm->bmx = malloc( bmSize );
bm->bm = (HBITMAP)0;
bm->color = -1;
memcpy( bm->bmx, bits, bmSize );
/*bm->bm = mswCreateBitMap( GetSysColor(COLOR_BTNTEXT), RGB( 255, 255, 255 ), w, h, bits );*/
return bm;
}
/*
*****************************************************************************
*
* Create
*
*****************************************************************************
*/
int doSetFocus = 1;
long FAR PASCAL XEXPORT mswDrawPush(
HWND hWnd,
UINT message,
UINT wParam,
LONG lParam )
{
#ifdef WIN32
long inx = GetWindowLong( hWnd, GWL_ID );
#else
short inx = GetWindowWord( hWnd, GWW_ID );
#endif
wDraw_p b;
short int ix, iy;
wPos_t x, y;
HDC hDc;
PAINTSTRUCT ps;
wAction_t action;
RECT rect;
HWND activeWnd;
HWND focusWnd;
wAccelKey_e extChar;
switch( message ) {
case WM_CREATE:
b = (wDraw_p)mswMapIndex( inx );
hDc = GetDC(hWnd);
if ( b->option & BD_DIRECT ) {
b->hDc = hDc;
b->hBm = 0;
b->hBmOld = 0;
} else {
b->hDc = CreateCompatibleDC( hDc );
b->hBm = CreateCompatibleBitmap( hDc, b->w, b->h );
b->hBmOld = SelectObject( b->hDc, b->hBm );
}
if (mswPalette) {
SelectPalette( b->hDc, mswPalette, 0 );
RealizePalette( b->hDc );
}
b->wFactor = (double)GetDeviceCaps( b->hDc, LOGPIXELSX );
b->hFactor = (double)GetDeviceCaps( b->hDc, LOGPIXELSY );
b->DPI = 96.0; /*min( b->wFactor, b->hFactor );*/
b->hWnd = hWnd;
SetROP2( b->hDc, R2_WHITE );
Rectangle( b->hDc, 0, 0, b->w, b->h );
if ( (b->option & BD_DIRECT) == 0 ) {
SetROP2( hDc, R2_WHITE );
Rectangle( hDc, 0, 0, b->w, b->h );
ReleaseDC( hWnd, hDc );
}
break;
case WM_SIZE:
b = (wDraw_p)mswMapIndex( inx );
ix = LOWORD( lParam );
iy = HIWORD( lParam );
b->w = ix+2;
b->h = iy+2;
if (b->hWnd) {
if ( b->option & BD_DIRECT ) {
} else {
hDc = GetDC( b->hWnd );
b->hBm = CreateCompatibleBitmap( hDc, b->w, b->h );
DeleteObject(SelectObject( b->hDc, b->hBm ));
ReleaseDC( b->hWnd, hDc );
SetROP2( b->hDc, R2_WHITE );
Rectangle( b->hDc, 0, 0, b->w, b->h );
}
}
/*if (b->drawResize)
b->drawResize( b, b->size );*/
if (b->drawRepaint)
b->drawRepaint( b, b->data, 0, 0 );
return 0;
case WM_MOUSEMOVE:
activeWnd = GetActiveWindow();
focusWnd = GetFocus();
if (focusWnd != hWnd) {
b = (wDraw_p)mswMapIndex( inx );
if (!b)
break;
if ( !((wControl_p)b->parent) )
break;
if ( ((wControl_p)b->parent)->hWnd != activeWnd )
break;
}
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
case WM_RBUTTONDOWN:
case WM_RBUTTONUP:
if (message == WM_LBUTTONDOWN)
action = wActionLDown;
else if (message == WM_RBUTTONDOWN)
action = wActionRDown;
else if (message == WM_LBUTTONUP)
action = wActionLUp;
else if (message == WM_RBUTTONUP)
action = wActionRUp;
else {
if ( (wParam & MK_LBUTTON) != 0)
action = wActionLDrag;
else if ( (wParam & MK_RBUTTON) != 0)
action = wActionRDrag;
else
action = wActionMove;
}
b = (wDraw_p)mswMapIndex( inx );
if (!b)
break;
if (doSetFocus && message != WM_MOUSEMOVE)
SetFocus( ((wControl_p)b->parent)->hWnd );
if ( (b->option&BD_NOCAPTURE) == 0 ) {
if (message == WM_LBUTTONDOWN || message == WM_RBUTTONDOWN)
SetCapture( b->hWnd );
else if (message == WM_LBUTTONUP || message == WM_RBUTTONUP)
ReleaseCapture();
}
ix = LOWORD( lParam );
iy = HIWORD( lParam );
x = XPIX2INCH( b, ix );
y = YPIX2INCH( b, iy );
if (b->action)
b->action( b, b->data, action, x, y );
if (b->hWnd)
UpdateWindow(b->hWnd);
return 0;
case WM_CHAR:
b = (wDraw_p)mswMapIndex( inx );
extChar = wAccelKey_None;
if (lParam & 0x01000000L)
switch( wParam ) {
case VK_DELETE: extChar = wAccelKey_Del; break;
case VK_INSERT: extChar = wAccelKey_Ins; break;
case VK_HOME: extChar = wAccelKey_Home; break;
case VK_END: extChar = wAccelKey_End; break;
case VK_PRIOR: extChar = wAccelKey_Pgup; break;
case VK_NEXT: extChar = wAccelKey_Pgdn; break;
case VK_UP: extChar = wAccelKey_Up; break;
case VK_DOWN: extChar = wAccelKey_Down; break;
case VK_RIGHT: extChar = wAccelKey_Right; break;
case VK_LEFT: extChar = wAccelKey_Left; break;
case VK_BACK: extChar = wAccelKey_Back; break;
/*case VK_F1: extChar = wAccelKey_F1; break;*/
case VK_F2: extChar = wAccelKey_F2; break;
case VK_F3: extChar = wAccelKey_F3; break;
case VK_F4: extChar = wAccelKey_F4; break;
case VK_F5: extChar = wAccelKey_F5; break;
case VK_F6: extChar = wAccelKey_F6; break;
case VK_F7: extChar = wAccelKey_F7; break;
case VK_F8: extChar = wAccelKey_F8; break;
case VK_F9: extChar = wAccelKey_F9; break;
case VK_F10: extChar = wAccelKey_F10; break;
case VK_F11: extChar = wAccelKey_F11; break;
case VK_F12: extChar = wAccelKey_F12; break;
}
if (b && b->action) {
if (extChar != wAccelKey_None)
b->action( b, b->data, wActionExtKey + ( (int)extChar << 8 ), 0, 0 );
else
b->action( b, b->data, wActionText + ( wParam << 8 ), 0, 0 );
}
return 0;
case WM_PAINT:
b = (wDraw_p)mswMapIndex( inx );
if (b && b->type == B_DRAW) {
if (GetUpdateRect( b->hWnd, &rect, FALSE )) {
hDc = BeginPaint( hWnd, &ps );
if ( b->hasPalette ) {
int winPaletteClock = mswGetPaletteClock();
if ( b->paletteClock < winPaletteClock ) {
RealizePalette( hDc );
b->paletteClock = winPaletteClock;
}
}
BitBlt( hDc, rect.left, rect.top,
rect.right-rect.left, rect.bottom-rect.top,
b->hDc, rect.left, rect.top,
SRCCOPY );
EndPaint( hWnd, &ps );
}
}
break;
case WM_DESTROY:
b = (wDraw_p)mswMapIndex( inx );
if (b && b->type == B_DRAW) {
if (b->hDc) {
DeleteDC( b->hDc );
b->hDc = (HDC)0;
}
if (b->hDcBackup) {
DeleteDC( b->hDcBackup );
b->hDcBackup = (HDC)0;
}
}
break;
default:
break;
}
return DefWindowProc( hWnd, message, wParam, lParam );
}
static LRESULT drawMsgProc( wDraw_p b, HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
wAction_t action;
switch( message ) {
case WM_MOUSEWHEEL:
/* handle mouse wheel events */
/* fwKeys = GET_KEYSTATE_WPARAM(wParam); modifier keys are currently ignored */
if ( GET_WHEEL_DELTA_WPARAM(wParam) > 0 ) {
action = wActionWheelUp;
} else {
action = wActionWheelDown;
}
if (b->action)
b->action( b, b->data, action, 0, 0 );
return 0;
}
return DefWindowProc( hWnd, message, wParam, lParam );
}
static void drawDoneProc( wControl_p b )
{
wDraw_p d = (wDraw_p)b;
if (d->hBm) {
SelectObject( d->hDc, d->hBmOld );
DeleteObject( d->hBm );
d->hBm = (HBITMAP)0;
}
if (d->hPen) {
SelectObject( d->hDc, GetStockObject( BLACK_PEN ) );
DeleteObject( d->hPen );
d->hPen = (HPEN)0;
}
if (d->hBrush) {
SelectObject( d->hDc, GetStockObject( BLACK_BRUSH) );
DeleteObject( d->hBrush );
d->hBrush = (HBRUSH)0;
}
if (d->hDc) {
DeleteDC( d->hDc );
d->hDc = (HDC)0;
}
if ( d->hDcBackup ) {
DeleteDC( d->hDcBackup );
d->hDcBackup = (HDC)0;
}
while (bmRoot) {
if (bmRoot->bm)
DeleteObject( bmRoot->bm );
bmRoot = bmRoot->next;
}
}
static callBacks_t drawCallBacks = {
NULL,
drawDoneProc,
(messageCallback_p)drawMsgProc };
wDraw_p drawList = NULL;
void mswRedrawAll( void )
{
wDraw_p p;
for ( p=drawList; p; p=p->drawNext ) {
if (p->drawRepaint)
p->drawRepaint( p, p->data, 0, 0 );
}
}
void mswRepaintAll( void )
{
wDraw_p b;
HDC hDc;
RECT rect;
PAINTSTRUCT ps;
for ( b=drawList; b; b=b->drawNext ) {
if (GetUpdateRect( b->hWnd, &rect, FALSE )) {
hDc = BeginPaint( b->hWnd, &ps );
BitBlt( hDc, rect.left, rect.top,
rect.right-rect.left, rect.bottom-rect.top,
b->hDc, rect.left, rect.top,
SRCCOPY );
EndPaint( b->hWnd, &ps );
}
}
}
wDraw_p wDrawCreate(
wWin_p parent,
wPos_t x,
wPos_t y,
const char * helpStr,
long option,
wPos_t w,
wPos_t h,
void * data,
wDrawRedrawCallBack_p redrawProc,
wDrawActionCallBack_p action )
{
wDraw_p d;
RECT rect;
int index;
HDC hDc;
if ( noNegDrawArgs < 0 ) {
wPrefGetInteger( "msw tweak", "NoNegDrawArgs", &noNegDrawArgs, 0 );
wPrefGetInteger( "msw tweak", "NoFlatEndCaps", &noFlatEndCaps, 0 );
}
d = mswAlloc( parent, B_DRAW, NULL, sizeof *d, data, &index );
mswComputePos( (wControl_p)d, x, y );
d->w = w;
d->h = h;
d->drawRepaint = NULL;
d->action = action;
d->option = option;
d->hWnd = CreateWindow( mswDrawWindowClassName, NULL,
WS_CHILDWINDOW|WS_VISIBLE|WS_BORDER,
d->x, d->y, w, h,
((wControl_p)parent)->hWnd, (HMENU)index, mswHInst, NULL );
if (d->hWnd == (HWND)0) {
mswFail( "CreateWindow(DRAW)" );
return d;
}
GetWindowRect( d->hWnd, &rect );
d->w = rect.right - rect.left;
d->h = rect.bottom - rect.top;
d->drawRepaint = redrawProc;
/*if (d->drawRepaint)
d->drawRepaint( d, d->data, 0.0, 0.0 );*/
mswAddButton( (wControl_p)d, FALSE, helpStr );
mswCallBacks[B_DRAW] = &drawCallBacks;
d->drawNext = drawList;
drawList = d;
if (mswPalette) {
hDc = GetDC( d->hWnd );
d->hasPalette = TRUE;
SelectPalette( hDc, mswPalette, 0 );
ReleaseDC( d->hWnd, hDc );
}
return d;
}
/*
*****************************************************************************
*
* Bitmaps
*
*****************************************************************************
*/
wDraw_p wBitMapCreate( wPos_t w, wPos_t h, int planes )
{
wDraw_p d;
HDC hDc;
d = (wDraw_p)calloc(1,sizeof *d);
d->type = B_DRAW;
d->shown = TRUE;
d->x = 0;
d->y = 0;
d->w = w;
d->h = h;
d->drawRepaint = NULL;
d->action = NULL;
d->option = 0;
hDc = GetDC(mswHWnd);
d->hDc = CreateCompatibleDC( hDc );
if ( d->hDc == (HDC)0 ) {
wNoticeEx( NT_ERROR, "CreateBitMap: CreateDC fails", "Ok", NULL );
return FALSE;
}
d->hBm = CreateCompatibleBitmap( hDc, d->w, d->h );
if ( d->hBm == (HBITMAP)0 ) {
wNoticeEx( NT_ERROR, "CreateBitMap: CreateBM fails", "Ok", NULL );
return FALSE;
}
d->hasPalette = (GetDeviceCaps(hDc,RASTERCAPS ) & RC_PALETTE) != 0;
ReleaseDC( mswHWnd, hDc );
d->hBmOld = SelectObject( d->hDc, d->hBm );
if (mswPalette) {
SelectPalette( d->hDc, mswPalette, 0 );
RealizePalette( d->hDc );
}
d->wFactor = (double)GetDeviceCaps( d->hDc, LOGPIXELSX );
d->hFactor = (double)GetDeviceCaps( d->hDc, LOGPIXELSY );
d->DPI = 96.0; /*min( d->wFactor, d->hFactor );*/
d->hWnd = 0;
SetROP2( d->hDc, R2_WHITE );
Rectangle( d->hDc, 0, 0, d->w, d->h );
return d;
}
wBool_t wBitMapDelete( wDraw_p d )
{
if (d->hPen) {
SelectObject( d->hDc, GetStockObject( BLACK_PEN ) );
DeleteObject( d->hPen );
d->hPen = (HPEN)0;
}
if (d->hBm) {
SelectObject( d->hDc, d->hBmOld );
DeleteObject( d->hBm );
d->hBm = (HBITMAP)0;
}
if (d->hDc) {
DeleteDC( d->hDc );
d->hDc = (HDC)0;
}
free(d);
return TRUE;
}
wBool_t wBitMapWriteFile( wDraw_p d, const char * fileName )
{
char *pixels;
int j, ww, chunk;
FILE * f;
BITMAPFILEHEADER bmfh;
struct {
BITMAPINFOHEADER bmih;
RGBQUAD colors[256];
} bmi;
int rc;
if ( d->hBm == 0)
return FALSE;
f = wFileOpen( fileName, "wb" );
if (!f) {
wNoticeEx( NT_ERROR, fileName, "Ok", NULL );
return FALSE;
}
ww = ((d->w +3) / 4) * 4;
bmfh.bfType = 'B'+('M'<<8);
bmfh.bfSize = (long)(sizeof bmfh) + (long)(sizeof bmi.bmih) + (long)(sizeof bmi.colors) + (long)ww * (long)(d->h);
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
bmfh.bfOffBits = sizeof bmfh + sizeof bmi.bmih + sizeof bmi.colors;
fwrite( &bmfh, 1, sizeof bmfh, f );
bmi.bmih.biSize = sizeof bmi.bmih;
bmi.bmih.biWidth = d->w;
bmi.bmih.biHeight = d->h;
bmi.bmih.biPlanes = 1;
bmi.bmih.biBitCount = 8;
bmi.bmih.biCompression = BI_RGB;
bmi.bmih.biSizeImage = 0;
bmi.bmih.biXPelsPerMeter = 75*(10000/254);
bmi.bmih.biYPelsPerMeter = 75*(10000/254);
bmi.bmih.biClrUsed = bmi.bmih.biClrImportant = mswGetColorList( bmi.colors );
SelectObject( d->hDc, d->hBmOld );
rc = GetDIBits( d->hDc, d->hBm, 0, 1, NULL, (BITMAPINFO*)&bmi, DIB_RGB_COLORS );
if ( rc == 0 ) {
wNoticeEx( NT_ERROR, "WriteBitMap: Can't get bitmapinfo from Bitmap", "Ok", NULL );
return FALSE;
}
bmi.bmih.biClrUsed = 256;
fwrite( &bmi.bmih, 1, sizeof bmi.bmih, f );
fwrite( bmi.colors, 1, sizeof bmi.colors, f );
chunk = 32000/ww;
pixels = (char*)malloc( ww*chunk );
if ( pixels == NULL ) {
wNoticeEx( NT_ERROR, "WriteBitMap: no memory", "OK", NULL );
return FALSE;
}
for (j=0;j<d->h;j+=chunk) {
if (j+chunk>d->h)
chunk = d->h-j;
rc = GetDIBits( d->hDc, d->hBm, j, chunk, pixels, (BITMAPINFO*)&bmi, DIB_RGB_COLORS );
if ( rc == 0 )
if ( rc == 0 ) {
wNoticeEx( NT_ERROR, "WriteBitMap: Can't get bits from Bitmap", "Ok", NULL );
return FALSE;
}
rc = fwrite( pixels, 1, ww*chunk, f );
if (rc != ww*chunk) {
wNoticeEx( NT_ERROR, "WriteBitMap: Bad fwrite", "Ok", NULL);
}
}
free( pixels );
SelectObject( d->hDc, d->hBm );
fclose( f );
return TRUE;
}
|