| 1 | /* $Id: line.cpp,v 1.14 2004-01-11 11:42:18 sandervl 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 | #include "oslibgpi.h" | 
|---|
| 16 | #include <dcdata.h> | 
|---|
| 17 |  | 
|---|
| 18 | #define DBG_LOCALLOG    DBG_line | 
|---|
| 19 | #include "dbglocal.h" | 
|---|
| 20 |  | 
|---|
| 21 | #define ROUND_FLOAT(x) ((INT)((x < 0) ? x-0.5:x+0.5)) | 
|---|
| 22 |  | 
|---|
| 23 | //****************************************************************************** | 
|---|
| 24 | //****************************************************************************** | 
|---|
| 25 | VOID toWin32LineEnd(PPOINTLOS2 startPt,INT nXEnd,INT nYEnd,PPOINTLOS2 pt) | 
|---|
| 26 | { | 
|---|
| 27 | if ((startPt->x != nXEnd) || (startPt->y != nYEnd)) | 
|---|
| 28 | { | 
|---|
| 29 | if (nXEnd == startPt->x) | 
|---|
| 30 | { | 
|---|
| 31 | pt->x = nXEnd; | 
|---|
| 32 | pt->y = (nYEnd > startPt->y) ? nYEnd-1:nYEnd+1; | 
|---|
| 33 | } else if (nYEnd == startPt->y) | 
|---|
| 34 | { | 
|---|
| 35 | pt->x = (nXEnd > startPt->x) ? nXEnd-1:nXEnd+1; | 
|---|
| 36 | pt->y = nYEnd; | 
|---|
| 37 | } else | 
|---|
| 38 | { | 
|---|
| 39 | INT w = nXEnd-startPt->x,h = nYEnd-startPt->y; | 
|---|
| 40 | DOUBLE len = hypot(w,h); | 
|---|
| 41 | DOUBLE lenQuot = (len-1)/len; | 
|---|
| 42 |  | 
|---|
| 43 | pt->x = startPt->x+ROUND_FLOAT(w*lenQuot); | 
|---|
| 44 | pt->y = startPt->y+ROUND_FLOAT(h*lenQuot); | 
|---|
| 45 | } | 
|---|
| 46 | } else | 
|---|
| 47 | { | 
|---|
| 48 | pt->x = nXEnd; | 
|---|
| 49 | pt->y = nYEnd; | 
|---|
| 50 | } | 
|---|
| 51 | } | 
|---|
| 52 | //****************************************************************************** | 
|---|
| 53 | //****************************************************************************** | 
|---|
| 54 | BOOL drawSingleLinePoint(HDC hdc,pDCData pHps,PPOINTLOS2 pt) | 
|---|
| 55 | { | 
|---|
| 56 | LOGPEN    penInfo; | 
|---|
| 57 | EXTLOGPEN extpenInfo; | 
|---|
| 58 |  | 
|---|
| 59 | if (!GetObjectA(GetCurrentObject(hdc,OBJ_PEN),sizeof(penInfo),(LPVOID)&penInfo)) | 
|---|
| 60 | {//try extlogpen | 
|---|
| 61 | if (!GetObjectA(GetCurrentObject(hdc,OBJ_PEN),sizeof(extpenInfo),(LPVOID)&extpenInfo)) { | 
|---|
| 62 | return FALSE; | 
|---|
| 63 | } | 
|---|
| 64 | penInfo.lopnStyle   = extpenInfo.elpPenStyle; | 
|---|
| 65 | penInfo.lopnWidth.x = extpenInfo.elpWidth; | 
|---|
| 66 | penInfo.lopnWidth.y = 0; //?????? | 
|---|
| 67 | penInfo.lopnColor   = extpenInfo.elpColor; | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | if ((penInfo.lopnWidth.x > 1) || (penInfo.lopnWidth.y > 1)) | 
|---|
| 71 | { | 
|---|
| 72 | if ((penInfo.lopnStyle != PS_INSIDEFRAME) && (penInfo.lopnStyle != PS_SOLID)) | 
|---|
| 73 | return FALSE; | 
|---|
| 74 |  | 
|---|
| 75 | LONG color = GetBValue(penInfo.lopnColor) | (GetGValue(penInfo.lopnColor)<<8) | (GetRValue(penInfo.lopnColor)<<16); | 
|---|
| 76 |  | 
|---|
| 77 | return drawLinePointCircle(pHps,penInfo.lopnWidth.x,penInfo.lopnWidth.y,color); | 
|---|
| 78 | } else | 
|---|
| 79 | { | 
|---|
| 80 | LONG color = GetBValue(penInfo.lopnColor) | (GetGValue(penInfo.lopnColor)<<8) | (GetRValue(penInfo.lopnColor)<<16); | 
|---|
| 81 |  | 
|---|
| 82 | return drawLinePoint(pHps,pt,color); | 
|---|
| 83 | } | 
|---|
| 84 | } | 
|---|
| 85 | //****************************************************************************** | 
|---|
| 86 | //****************************************************************************** | 
|---|
| 87 | BOOL WIN32API MoveToEx( HDC hdc, int X, int Y, LPPOINT lpPoint) | 
|---|
| 88 | { | 
|---|
| 89 | pDCData pHps = (pDCData)OSLibGpiQueryDCData(hdc); | 
|---|
| 90 |  | 
|---|
| 91 | dprintf(("GDI32: MoveToEx %x (%d,%d)", hdc, X, Y)); | 
|---|
| 92 |  | 
|---|
| 93 | if (pHps) | 
|---|
| 94 | { | 
|---|
| 95 | POINTLOS2 newPoint = {X,Y}; | 
|---|
| 96 |  | 
|---|
| 97 | if (lpPoint) | 
|---|
| 98 | { | 
|---|
| 99 | POINTLOS2 lastPoint; | 
|---|
| 100 |  | 
|---|
| 101 | OSLibGpiQueryCurrentPosition(pHps,&lastPoint); | 
|---|
| 102 | lpPoint->x = lastPoint.x; | 
|---|
| 103 | lpPoint->y = lastPoint.y; | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | #ifndef INVERT | 
|---|
| 107 | if(pHps->yInvert > 0) { | 
|---|
| 108 | newPoint.y =  pHps->yInvert - newPoint.y; | 
|---|
| 109 | if (lpPoint) { | 
|---|
| 110 | lpPoint->y = pHps->yInvert - lpPoint->y; | 
|---|
| 111 | } | 
|---|
| 112 | } | 
|---|
| 113 | #endif | 
|---|
| 114 |  | 
|---|
| 115 | if (OSLibGpiMove(pHps,&newPoint)) | 
|---|
| 116 | { | 
|---|
| 117 | //CB: add metafile info | 
|---|
| 118 | return TRUE; | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | return FALSE; | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | SetLastError(ERROR_INVALID_HANDLE); | 
|---|
| 125 | return FALSE; | 
|---|
| 126 | } | 
|---|
| 127 | //****************************************************************************** | 
|---|
| 128 | //****************************************************************************** | 
|---|
| 129 | BOOL WIN32API LineTo( HDC hdc, int nXEnd, int  nYEnd) | 
|---|
| 130 | { | 
|---|
| 131 | pDCData pHps = (pDCData)OSLibGpiQueryDCData(hdc); | 
|---|
| 132 | BOOL rc = TRUE; | 
|---|
| 133 |  | 
|---|
| 134 | dprintf(("GDI32: LineTo %x (%d,%d)", hdc, nXEnd, nYEnd)); | 
|---|
| 135 |  | 
|---|
| 136 | if (pHps) | 
|---|
| 137 | { | 
|---|
| 138 | POINTLOS2 oldPoint,newPoint; | 
|---|
| 139 | BOOL bWideLine; | 
|---|
| 140 |  | 
|---|
| 141 | #ifndef INVERT | 
|---|
| 142 | if (pHps->yInvert > 0) { | 
|---|
| 143 | nYEnd = pHps->yInvert - nYEnd; | 
|---|
| 144 | } | 
|---|
| 145 | #endif | 
|---|
| 146 |  | 
|---|
| 147 | //CB: add metafile info | 
|---|
| 148 |  | 
|---|
| 149 | OSLibGpiQueryCurrentPosition(pHps,&oldPoint); | 
|---|
| 150 | toWin32LineEnd(&oldPoint,nXEnd,nYEnd,&newPoint); | 
|---|
| 151 |  | 
|---|
| 152 | if ((oldPoint.x == newPoint.x) && (oldPoint.y == newPoint.y)) | 
|---|
| 153 | { | 
|---|
| 154 | rc = drawSingleLinePoint(hdc,pHps,&newPoint); | 
|---|
| 155 | } else | 
|---|
| 156 | { | 
|---|
| 157 | if (getIsWideLine(pHps)) | 
|---|
| 158 | { | 
|---|
| 159 | rc = O32_LineTo(hdc,newPoint.x,newPoint.y); //CB: wide line not supported | 
|---|
| 160 | } else | 
|---|
| 161 | { | 
|---|
| 162 | if (OSLibGpiLine(pHps,&newPoint) == FALSE) | 
|---|
| 163 | rc = FALSE; | 
|---|
| 164 | } | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | newPoint.x = nXEnd; | 
|---|
| 168 | newPoint.y = nYEnd; | 
|---|
| 169 | OSLibGpiMove(pHps,&newPoint); | 
|---|
| 170 | } else | 
|---|
| 171 | { | 
|---|
| 172 | SetLastError(ERROR_INVALID_HANDLE); | 
|---|
| 173 | rc = FALSE; | 
|---|
| 174 | } | 
|---|
| 175 |  | 
|---|
| 176 | return rc; | 
|---|
| 177 | } | 
|---|
| 178 | //****************************************************************************** | 
|---|
| 179 | //****************************************************************************** | 
|---|
| 180 | BOOL WIN32API LineDDA( int nXStart, int nYStart, int nXEnd, int nYEnd, LINEDDAPROC lpLineFunc, LPARAM lpData) | 
|---|
| 181 | { | 
|---|
| 182 | BOOL                 rc; | 
|---|
| 183 | LineDDAProcCallback *callback = new LineDDAProcCallback(lpLineFunc, lpData); | 
|---|
| 184 | POINTLOS2 startPt,endPt; | 
|---|
| 185 |  | 
|---|
| 186 | #if 0 //CB: the Open32 function is ok -> to check | 
|---|
| 187 | startPt.x = nXStart; | 
|---|
| 188 | startPt.y = nYStart; | 
|---|
| 189 | toWin32LineEnd(&startPt,nXEnd,nYEnd,&endPt); | 
|---|
| 190 |  | 
|---|
| 191 | rc = O32_LineDDA(startPt.x,startPt.y,endPt.x,endPt.y, lpLineFunc, lpData); | 
|---|
| 192 | #else | 
|---|
| 193 | rc = O32_LineDDA(nXStart,nYStart,nXEnd,nYEnd,callback->GetOS2Callback(),(LPARAM)callback); | 
|---|
| 194 | if(callback) | 
|---|
| 195 | delete callback; | 
|---|
| 196 | #endif | 
|---|
| 197 | return(rc); | 
|---|
| 198 | } | 
|---|
| 199 | //****************************************************************************** | 
|---|
| 200 | //****************************************************************************** | 
|---|
| 201 | BOOL WIN32API Polyline( HDC hdc, const POINT *lppt, int cPoints) | 
|---|
| 202 | { | 
|---|
| 203 | pDCData pHps = (pDCData)OSLibGpiQueryDCData(hdc); | 
|---|
| 204 |  | 
|---|
| 205 | if (!pHps) | 
|---|
| 206 | { | 
|---|
| 207 | SetLastError(ERROR_INVALID_HANDLE); | 
|---|
| 208 | return FALSE; | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 | if (cPoints == 0) return TRUE; | 
|---|
| 212 | if (cPoints < 0) | 
|---|
| 213 | { | 
|---|
| 214 | SetLastError(ERROR_INVALID_PARAMETER); | 
|---|
| 215 | return FALSE; | 
|---|
| 216 | } | 
|---|
| 217 |  | 
|---|
| 218 | if (cPoints == 1) | 
|---|
| 219 | { | 
|---|
| 220 | drawSingleLinePoint(hdc,pHps,(PPOINTLOS2)lppt); //CB: check metafile recording | 
|---|
| 221 | return TRUE; | 
|---|
| 222 | } | 
|---|
| 223 |  | 
|---|
| 224 | POINT *points = (POINT*)lppt; | 
|---|
| 225 | POINT lastPt = lppt[cPoints-1]; | 
|---|
| 226 | BOOL rc; | 
|---|
| 227 |  | 
|---|
| 228 | toWin32LineEnd((PPOINTLOS2)&lppt[cPoints-2],lastPt.x,lastPt.y,(PPOINTLOS2)&points[cPoints-1]); | 
|---|
| 229 | rc = O32_Polyline(hdc,lppt,cPoints); | 
|---|
| 230 | points[cPoints-1] = lastPt; | 
|---|
| 231 |  | 
|---|
| 232 | return rc; | 
|---|
| 233 | } | 
|---|
| 234 | //****************************************************************************** | 
|---|
| 235 | //****************************************************************************** | 
|---|
| 236 | BOOL WIN32API PolylineTo( HDC hdc, const POINT * lppt, DWORD cCount) | 
|---|
| 237 | { | 
|---|
| 238 | pDCData pHps = (pDCData)OSLibGpiQueryDCData(hdc); | 
|---|
| 239 |  | 
|---|
| 240 | if (!pHps) | 
|---|
| 241 | { | 
|---|
| 242 | SetLastError(ERROR_INVALID_HANDLE); | 
|---|
| 243 | return FALSE; | 
|---|
| 244 | } | 
|---|
| 245 |  | 
|---|
| 246 | if (cCount == 0) return TRUE; | 
|---|
| 247 |  | 
|---|
| 248 | //CB: add metafile info | 
|---|
| 249 |  | 
|---|
| 250 | if (cCount == 1) | 
|---|
| 251 | { | 
|---|
| 252 | drawSingleLinePoint(hdc,pHps,(PPOINTLOS2)lppt); | 
|---|
| 253 | return TRUE; | 
|---|
| 254 | } | 
|---|
| 255 |  | 
|---|
| 256 | POINT *points = (POINT*)lppt; | 
|---|
| 257 | POINT lastPt = lppt[cCount-1]; | 
|---|
| 258 | BOOL rc; | 
|---|
| 259 |  | 
|---|
| 260 | toWin32LineEnd((PPOINTLOS2)&lppt[cCount-2],lastPt.x,lastPt.y,(PPOINTLOS2)&points[cCount-1]); | 
|---|
| 261 | rc = O32_PolylineTo(hdc,lppt,cCount); | 
|---|
| 262 | points[cCount-1] = lastPt; | 
|---|
| 263 | OSLibGpiMove(pHps,(PPOINTLOS2)&lastPt); | 
|---|
| 264 |  | 
|---|
| 265 | return rc; | 
|---|
| 266 | } | 
|---|
| 267 | //****************************************************************************** | 
|---|
| 268 | //****************************************************************************** | 
|---|
| 269 |  | 
|---|