1 | /* $Id: line.cpp,v 1.1 1999-12-03 17:31:51 cbratschi Exp $ */
|
---|
2 | /*
|
---|
3 | * Line API's
|
---|
4 | *
|
---|
5 | * Copyright 1999 Christoph Bratschi (cbratschi@datacomm.ch)
|
---|
6 | *
|
---|
7 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
8 | *
|
---|
9 | */
|
---|
10 |
|
---|
11 | #include <os2win.h>
|
---|
12 | #include <math.h>
|
---|
13 | #include "misc.h"
|
---|
14 | #include "callback.h"
|
---|
15 |
|
---|
16 | //CB: todo: rewrite them without using Open32
|
---|
17 |
|
---|
18 | #define ROUND_FLOAT(x) ((INT)((x < 0) ? x-0.5:x+0.5))
|
---|
19 | #define sqr(x) (pow(x,2))
|
---|
20 |
|
---|
21 | POINT ToWin32LineEnd(POINT startPt,INT nXEnd,INT nYEnd)
|
---|
22 | {
|
---|
23 | POINT pt;
|
---|
24 |
|
---|
25 | if (startPt.x != nXEnd || startPt.y != nYEnd)
|
---|
26 | {
|
---|
27 | if (nXEnd == startPt.x)
|
---|
28 | {
|
---|
29 | pt.x = nXEnd;
|
---|
30 | pt.y = (nYEnd > startPt.y) ? nYEnd-1:nYEnd+1;
|
---|
31 | } else if (nYEnd == startPt.y)
|
---|
32 | {
|
---|
33 | pt.x = (nXEnd > startPt.x) ? nXEnd-1:nXEnd+1;
|
---|
34 | pt.y = nYEnd;
|
---|
35 | } else
|
---|
36 | {
|
---|
37 | DOUBLE len = sqrt(sqr(nXEnd-startPt.x)+sqr(nYEnd-startPt.y));
|
---|
38 | DOUBLE lenDif = (len-1)/len;
|
---|
39 | INT w = nXEnd-startPt.x,h = nYEnd-startPt.y;
|
---|
40 |
|
---|
41 | pt.x = startPt.x+ROUND_FLOAT(w*lenDif);
|
---|
42 | pt.y = startPt.y+ROUND_FLOAT(h*lenDif);
|
---|
43 | }
|
---|
44 | } else
|
---|
45 | {
|
---|
46 | pt.x = nXEnd;
|
---|
47 | pt.y = nYEnd;
|
---|
48 | }
|
---|
49 |
|
---|
50 | return pt;
|
---|
51 | }
|
---|
52 |
|
---|
53 | VOID DrawSingleLinePoint(HDC hdc,POINT pt)
|
---|
54 | {
|
---|
55 | LOGPEN penInfo;
|
---|
56 |
|
---|
57 | if (!GetObjectA(GetCurrentObject(hdc,OBJ_PEN),sizeof(penInfo),(LPVOID)&penInfo)) return;
|
---|
58 | if (penInfo.lopnWidth.x <= 1 && penInfo.lopnWidth.y <= 1) SetPixel(hdc,pt.x,pt.y,penInfo.lopnColor); else
|
---|
59 | {
|
---|
60 | INT x = pt.x-penInfo.lopnWidth.x/2;
|
---|
61 | INT y = pt.y-penInfo.lopnWidth.y/2;
|
---|
62 | Ellipse(hdc,x,y,x+penInfo.lopnWidth.x,y+penInfo.lopnWidth.y);
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | BOOL WIN32API LineTo( HDC hdc, int nXEnd, int nYEnd)
|
---|
67 | {
|
---|
68 | POINT oldPt,pt;
|
---|
69 |
|
---|
70 | dprintf(("GDI32: LineTo"));
|
---|
71 |
|
---|
72 | //CB: Open32 draws a pixel too much!
|
---|
73 | GetCurrentPositionEx(hdc,&oldPt);
|
---|
74 | pt = ToWin32LineEnd(oldPt,nXEnd,nYEnd);
|
---|
75 |
|
---|
76 | BOOL rc;
|
---|
77 |
|
---|
78 | if (oldPt.x == pt.x && oldPt.y == pt.y)
|
---|
79 | {
|
---|
80 | DrawSingleLinePoint(hdc,pt);
|
---|
81 |
|
---|
82 | rc = TRUE;
|
---|
83 | } else rc = O32_LineTo(hdc,pt.x,pt.y);
|
---|
84 | MoveToEx(hdc,nXEnd,nYEnd,NULL);
|
---|
85 |
|
---|
86 | return rc;
|
---|
87 | }
|
---|
88 | //******************************************************************************
|
---|
89 | //******************************************************************************
|
---|
90 | BOOL WIN32API LineDDA( int nXStart, int nYStart, int nXEnd, int nYEnd, LINEDDAPROC lpLineFunc, LPARAM lpData)
|
---|
91 | {
|
---|
92 | BOOL rc;
|
---|
93 | LineDDAProcCallback *callback = new LineDDAProcCallback(lpLineFunc, lpData);
|
---|
94 | POINT startPt,endPt;
|
---|
95 |
|
---|
96 | dprintf(("GDI32: LineDDA\n"));
|
---|
97 |
|
---|
98 | //CB: don't know if Open32 reports the last pixel, but all other line functions do
|
---|
99 | startPt.x = nXStart;
|
---|
100 | startPt.y = nYStart;
|
---|
101 | endPt = ToWin32LineEnd(startPt,nXEnd,nYEnd);
|
---|
102 |
|
---|
103 | rc = O32_LineDDA(startPt.x,startPt.y,endPt.x,endPt.y,callback->GetOS2Callback(),(LPARAM)callback);
|
---|
104 | if(callback)
|
---|
105 | delete callback;
|
---|
106 | return(rc);
|
---|
107 | }
|
---|
108 | //******************************************************************************
|
---|
109 | //******************************************************************************
|
---|
110 | BOOL WIN32API Polyline( HDC hdc, const POINT *lppt, int cPoints)
|
---|
111 | {
|
---|
112 | dprintf(("GDI32: Polyline"));
|
---|
113 |
|
---|
114 | if (cPoints == 0) return TRUE;
|
---|
115 | if (cPoints < 0)
|
---|
116 | {
|
---|
117 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
118 |
|
---|
119 | return FALSE;
|
---|
120 | }
|
---|
121 |
|
---|
122 | if (cPoints == 1)
|
---|
123 | {
|
---|
124 | DrawSingleLinePoint(hdc,*lppt); //CB: check metafile recording
|
---|
125 |
|
---|
126 | return TRUE;
|
---|
127 | }
|
---|
128 |
|
---|
129 | //CB: Open32 draw a pixel too much!
|
---|
130 | POINT *points = (POINT*)lppt;
|
---|
131 | POINT lastPt = lppt[cPoints-1];
|
---|
132 | BOOL rc;
|
---|
133 |
|
---|
134 | points[cPoints-1] = ToWin32LineEnd(lppt[cPoints-2],lastPt.x,lastPt.y);
|
---|
135 | rc = O32_Polyline(hdc,lppt,cPoints);
|
---|
136 | points[cPoints-1] = lastPt;
|
---|
137 |
|
---|
138 | return rc;
|
---|
139 | }
|
---|
140 | //******************************************************************************
|
---|
141 | //******************************************************************************
|
---|
142 | BOOL WIN32API PolylineTo( HDC hdc, const POINT * lppt, DWORD cCount)
|
---|
143 | {
|
---|
144 | dprintf(("GDI32: PolylineTo"));
|
---|
145 |
|
---|
146 | if (cCount == 0) return TRUE;
|
---|
147 |
|
---|
148 | if (cCount == 1)
|
---|
149 | {
|
---|
150 | DrawSingleLinePoint(hdc,*lppt);
|
---|
151 |
|
---|
152 | return TRUE; //CB: check metafile recording
|
---|
153 | }
|
---|
154 |
|
---|
155 | //CB: Open32 draw a pixel too much!
|
---|
156 | POINT *points = (POINT*)lppt;
|
---|
157 | POINT lastPt = lppt[cCount-1];
|
---|
158 | BOOL rc;
|
---|
159 |
|
---|
160 | points[cCount-1] = ToWin32LineEnd(lppt[cCount-2],lastPt.x,lastPt.y);
|
---|
161 | rc = O32_PolylineTo(hdc,lppt,cCount);
|
---|
162 | points[cCount-1] = lastPt;
|
---|
163 | MoveToEx(hdc,lastPt.x,lastPt.y,NULL);
|
---|
164 |
|
---|
165 | return rc;
|
---|
166 | }
|
---|
167 | //******************************************************************************
|
---|
168 | //******************************************************************************
|
---|
169 |
|
---|