| 1 | /* $Id: oslibres.cpp,v 1.37 2003-03-03 16:36:26 sandervl Exp $ */ | 
|---|
| 2 | /* | 
|---|
| 3 | * Window API wrappers for OS/2 | 
|---|
| 4 | * | 
|---|
| 5 | * | 
|---|
| 6 | * Copyright 1999 Sander van Leeuwen (sandervl@xs4all.nl) | 
|---|
| 7 | * | 
|---|
| 8 | * | 
|---|
| 9 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 10 | * | 
|---|
| 11 | */ | 
|---|
| 12 | #define  INCL_WIN | 
|---|
| 13 | #define  INCL_PM | 
|---|
| 14 | #include <os2wrap.h> | 
|---|
| 15 | #include <stdlib.h> | 
|---|
| 16 | #include <stdio.h> | 
|---|
| 17 | #include <string.h> | 
|---|
| 18 |  | 
|---|
| 19 | #include <misc.h> | 
|---|
| 20 | #include <winconst.h> | 
|---|
| 21 | #include "oslibwin.h" | 
|---|
| 22 | #include "oslibutil.h" | 
|---|
| 23 | #include "oslibmsg.h" | 
|---|
| 24 | #include "oslibgdi.h" | 
|---|
| 25 | #include "oslibres.h" | 
|---|
| 26 | #include "pmwindow.h" | 
|---|
| 27 | #include <wingdi32.h> | 
|---|
| 28 | #include <custombuild.h> | 
|---|
| 29 |  | 
|---|
| 30 | #define DBG_LOCALLOG    DBG_oslibres | 
|---|
| 31 | #include "dbglocal.h" | 
|---|
| 32 |  | 
|---|
| 33 | //****************************************************************************** | 
|---|
| 34 | //****************************************************************************** | 
|---|
| 35 | HANDLE OSLibWinSetAccelTable(HWND hwnd, HANDLE hAccel, PVOID acceltemplate) | 
|---|
| 36 | { | 
|---|
| 37 | HAB    hab = WinQueryAnchorBlock(hwnd); | 
|---|
| 38 |  | 
|---|
| 39 | if(hAccel == 0) { | 
|---|
| 40 | hAccel = WinCreateAccelTable(hab, (PACCELTABLE)acceltemplate); | 
|---|
| 41 | if(hAccel == 0) { | 
|---|
| 42 | dprintf(("OSLibWinSetAccelTable: WinCreateAccelTable returned 0")); | 
|---|
| 43 | return FALSE; | 
|---|
| 44 | } | 
|---|
| 45 | } | 
|---|
| 46 | if(WinSetAccelTable(hab, hAccel, hwnd) == TRUE) { | 
|---|
| 47 | return hAccel; | 
|---|
| 48 | } | 
|---|
| 49 | else    return 0; | 
|---|
| 50 | } | 
|---|
| 51 | #if 0 | 
|---|
| 52 | //****************************************************************************** | 
|---|
| 53 | //TODO: change search method for icon array (cxDesired, cyDesired) | 
|---|
| 54 | //TODO: PM rescales the icon internally!!! ($W(#*&$(*%&) | 
|---|
| 55 | //****************************************************************************** | 
|---|
| 56 | HANDLE OSLibWinCreateIcon(PVOID iconbitmap, ULONG cxDesired, ULONG cyDesired) | 
|---|
| 57 | { | 
|---|
| 58 | POINTERINFO pointerInfo = {0}; | 
|---|
| 59 | HBITMAP     hbmColor, hbmMask; | 
|---|
| 60 | BITMAPARRAYFILEHEADER2 *bafh = (BITMAPARRAYFILEHEADER2 *)iconbitmap; | 
|---|
| 61 | BITMAPFILEHEADER2 *bfhBW; | 
|---|
| 62 | BITMAPFILEHEADER2 *bfhColor; | 
|---|
| 63 | HPS         hps; | 
|---|
| 64 | HANDLE      hIcon; | 
|---|
| 65 |  | 
|---|
| 66 | if(iconbitmap == NULL) { | 
|---|
| 67 | dprintf(("OSLibWinCreateIcon iconbitmap == NULL!!")); | 
|---|
| 68 | return 0; | 
|---|
| 69 | } | 
|---|
| 70 | if(bafh->usType == BFT_BITMAPARRAY && bafh->cbSize == sizeof(BITMAPARRAYFILEHEADER2)) { | 
|---|
| 71 | // search best icon for the current screen, | 
|---|
| 72 | // TODO: maybe compare icon size with screen size. | 
|---|
| 73 | // Some bugs with black/white Icons ? | 
|---|
| 74 | BITMAPARRAYFILEHEADER2 *next, *found; | 
|---|
| 75 | LONG bitcountScreen, bitcountIcon=-1, cxIcon=-1, cyIcon=-1; | 
|---|
| 76 |  | 
|---|
| 77 | HPS hps = WinGetPS(HWND_DESKTOP); | 
|---|
| 78 | HDC hdc = GpiQueryDevice(hps); | 
|---|
| 79 | DevQueryCaps(hdc, CAPS_COLOR_BITCOUNT, 1, &bitcountScreen); | 
|---|
| 80 | WinReleasePS(hps); | 
|---|
| 81 |  | 
|---|
| 82 | next = found = bafh; | 
|---|
| 83 | while(TRUE) | 
|---|
| 84 | { | 
|---|
| 85 | bfhColor = (BITMAPFILEHEADER2 *)((char *)&next->bfh2 + sizeof(RGB2)*2 + sizeof(BITMAPFILEHEADER2)); | 
|---|
| 86 | if(bfhColor->bmp2.cBitCount <= bitcountScreen && | 
|---|
| 87 | bfhColor->bmp2.cBitCount > bitcountIcon || | 
|---|
| 88 | (bfhColor->bmp2.cBitCount == bitcountIcon && | 
|---|
| 89 | (cxIcon < bfhColor->bmp2.cx || cyIcon < bfhColor->bmp2.cy))) | 
|---|
| 90 | { | 
|---|
| 91 | found = next; | 
|---|
| 92 | bitcountIcon = bfhColor->bmp2.cBitCount; | 
|---|
| 93 | cxIcon = bfhColor->bmp2.cx; | 
|---|
| 94 | cyIcon = bfhColor->bmp2.cy; | 
|---|
| 95 | } | 
|---|
| 96 | if(next->offNext != 0) | 
|---|
| 97 | next = (BITMAPARRAYFILEHEADER2 *) ((char *)bafh + next->offNext); | 
|---|
| 98 | else | 
|---|
| 99 | break; | 
|---|
| 100 | } | 
|---|
| 101 | bfhBW    = &found->bfh2; | 
|---|
| 102 | bfhColor = (BITMAPFILEHEADER2 *)((char *)bfhBW + sizeof(RGB2)*2 + sizeof(BITMAPFILEHEADER2)); | 
|---|
| 103 | } | 
|---|
| 104 | else {//single icon | 
|---|
| 105 | bfhBW    = (BITMAPFILEHEADER2 *)iconbitmap; | 
|---|
| 106 | bfhColor = (BITMAPFILEHEADER2 *)((char *)bfhBW + sizeof(RGB2)*2 + sizeof(BITMAPFILEHEADER2)); | 
|---|
| 107 | bafh     = (BITMAPARRAYFILEHEADER2 *)bfhBW; //for calculation bitmap offset | 
|---|
| 108 | } | 
|---|
| 109 | hps = WinGetScreenPS(HWND_DESKTOP); | 
|---|
| 110 |  | 
|---|
| 111 | //Resize icon bitmap if requested size is different | 
|---|
| 112 | if(cxDesired != bfhColor->bmp2.cx|| cyDesired != bfhColor->bmp2.cy) | 
|---|
| 113 | { | 
|---|
| 114 | BITMAPINFOHEADER2 infohdr = bfhColor->bmp2; | 
|---|
| 115 |  | 
|---|
| 116 | infohdr.cx = cxDesired; | 
|---|
| 117 | infohdr.cy = cyDesired; | 
|---|
| 118 | hbmColor = GpiCreateBitmap(hps, &infohdr, CBM_INIT, | 
|---|
| 119 | (char *)bafh + bfhColor->offBits, | 
|---|
| 120 | (BITMAPINFO2 *)&bfhColor->bmp2); | 
|---|
| 121 | } | 
|---|
| 122 | else { | 
|---|
| 123 | hbmColor = GpiCreateBitmap(hps, &bfhColor->bmp2, CBM_INIT, | 
|---|
| 124 | (char *)bafh + bfhColor->offBits, | 
|---|
| 125 | (BITMAPINFO2 *)&bfhColor->bmp2); | 
|---|
| 126 | } | 
|---|
| 127 | if(hbmColor == GPI_ERROR) { | 
|---|
| 128 | dprintf(("OSLibWinCreateIcon: GpiCreateBitmap failed!")); | 
|---|
| 129 | WinReleasePS(hps); | 
|---|
| 130 | return 0; | 
|---|
| 131 | } | 
|---|
| 132 | //Resize icon mask if requested size is different | 
|---|
| 133 | if(cxDesired != bfhBW->bmp2.cx|| cyDesired*2 != bfhBW->bmp2.cy) | 
|---|
| 134 | { | 
|---|
| 135 | BITMAPINFOHEADER2 infohdr = bfhBW->bmp2; | 
|---|
| 136 |  | 
|---|
| 137 | infohdr.cx = cxDesired; | 
|---|
| 138 | infohdr.cy = cyDesired; | 
|---|
| 139 | hbmMask = GpiCreateBitmap(hps, &infohdr, CBM_INIT, | 
|---|
| 140 | (char *)bafh + bfhBW->offBits, | 
|---|
| 141 | (BITMAPINFO2 *)&bfhBW->bmp2); | 
|---|
| 142 | } | 
|---|
| 143 | else { | 
|---|
| 144 | hbmMask = GpiCreateBitmap(hps, &bfhBW->bmp2, CBM_INIT, | 
|---|
| 145 | (char *)bafh + bfhBW->offBits, | 
|---|
| 146 | (BITMAPINFO2 *)&bfhBW->bmp2); | 
|---|
| 147 | } | 
|---|
| 148 | if(hbmMask == GPI_ERROR) { | 
|---|
| 149 | dprintf(("OSLibWinCreateIcon: GpiCreateBitmap hbmMask failed!")); | 
|---|
| 150 | GpiDeleteBitmap(hbmColor); | 
|---|
| 151 | WinReleasePS(hps); | 
|---|
| 152 | return 0; | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | pointerInfo.fPointer   = FALSE; //icon | 
|---|
| 156 | pointerInfo.xHotspot   = bfhBW->xHotspot; | 
|---|
| 157 | pointerInfo.yHotspot   = bfhBW->yHotspot; | 
|---|
| 158 | pointerInfo.hbmColor   = hbmColor; | 
|---|
| 159 | pointerInfo.hbmPointer = hbmMask; | 
|---|
| 160 | hIcon = WinCreatePointerIndirect(HWND_DESKTOP, &pointerInfo); | 
|---|
| 161 | if(hIcon == NULL) { | 
|---|
| 162 | dprintf(("OSLibWinCreateIcon: WinCreatePointerIndirect failed!")); | 
|---|
| 163 | } | 
|---|
| 164 | GpiDeleteBitmap(hbmMask); | 
|---|
| 165 | GpiDeleteBitmap(hbmColor); | 
|---|
| 166 | WinReleasePS(hps); | 
|---|
| 167 |  | 
|---|
| 168 | return hIcon; | 
|---|
| 169 | } | 
|---|
| 170 | //****************************************************************************** | 
|---|
| 171 | //TODO: change cursor size if required!! (cxDesired, cyDesired) | 
|---|
| 172 | //****************************************************************************** | 
|---|
| 173 | HANDLE OSLibWinCreatePointer(PVOID cursorbitmap, ULONG cxDesired, ULONG cyDesired) | 
|---|
| 174 | { | 
|---|
| 175 | POINTERINFO pointerInfo = {0}; | 
|---|
| 176 | HBITMAP     hbmColor = 0, hbmMask = 0; | 
|---|
| 177 | BITMAPARRAYFILEHEADER2 *bafh = (BITMAPARRAYFILEHEADER2 *)cursorbitmap; | 
|---|
| 178 | BITMAPFILEHEADER2 *bfh = (BITMAPFILEHEADER2 *)cursorbitmap, *bfhColor = 0; | 
|---|
| 179 | HPS         hps; | 
|---|
| 180 | HANDLE      hPointer; | 
|---|
| 181 |  | 
|---|
| 182 | if(cursorbitmap == NULL) { | 
|---|
| 183 | dprintf(("OSLibWinCreatePointer cursorbitmap == NULL!!")); | 
|---|
| 184 | return 0; | 
|---|
| 185 | } | 
|---|
| 186 | if(bafh->usType == BFT_BITMAPARRAY && bafh->cbSize == sizeof(BITMAPARRAYFILEHEADER2)) { | 
|---|
| 187 | bfh  = &bafh->bfh2; | 
|---|
| 188 | bfhColor = (BITMAPFILEHEADER2 *)((char *)bfh + sizeof(RGB2)*2 + sizeof(BITMAPFILEHEADER2)); | 
|---|
| 189 | } | 
|---|
| 190 | else {//single cursor | 
|---|
| 191 | bfh  = (BITMAPFILEHEADER2 *)cursorbitmap; | 
|---|
| 192 | bfhColor = (BITMAPFILEHEADER2 *)((char *)bfh + sizeof(RGB2)*2 + sizeof(BITMAPFILEHEADER2)); | 
|---|
| 193 | bafh = (BITMAPARRAYFILEHEADER2 *)bfh; //for calculation bitmap offset | 
|---|
| 194 | } | 
|---|
| 195 | //skip xor/and mask | 
|---|
| 196 | hps = WinGetScreenPS(HWND_DESKTOP); | 
|---|
| 197 | hbmMask = GpiCreateBitmap(hps, &bfh->bmp2, CBM_INIT, | 
|---|
| 198 | (char *)bafh + bfh->offBits, | 
|---|
| 199 | (BITMAPINFO2 *)&bfh->bmp2); | 
|---|
| 200 | if(hbmMask == GPI_ERROR) { | 
|---|
| 201 | dprintf(("OSLibWinCreatePointer: GpiCreateBitmap failed!")); | 
|---|
| 202 | WinReleasePS(hps); | 
|---|
| 203 | return 0; | 
|---|
| 204 | } | 
|---|
| 205 |  | 
|---|
| 206 | if((ULONG)((char *)bafh+bfh->offBits) != (ULONG)bfhColor) | 
|---|
| 207 | {//color bitmap present | 
|---|
| 208 | hbmColor = GpiCreateBitmap(hps, &bfhColor->bmp2, CBM_INIT, | 
|---|
| 209 | (char *)bafh + bfhColor->offBits, | 
|---|
| 210 | (BITMAPINFO2 *)&bfhColor->bmp2); | 
|---|
| 211 | if(hbmColor == GPI_ERROR) { | 
|---|
| 212 | dprintf(("OSLibWinCreateIcon: GpiCreateBitmap failed!")); | 
|---|
| 213 | GpiDeleteBitmap(hbmMask); | 
|---|
| 214 | WinReleasePS(hps); | 
|---|
| 215 | return 0; | 
|---|
| 216 | } | 
|---|
| 217 | } | 
|---|
| 218 |  | 
|---|
| 219 | pointerInfo.fPointer   = TRUE; | 
|---|
| 220 | pointerInfo.xHotspot   = bfh->xHotspot; | 
|---|
| 221 | pointerInfo.yHotspot   = bfh->yHotspot; | 
|---|
| 222 | pointerInfo.hbmColor   = hbmColor; | 
|---|
| 223 | pointerInfo.hbmPointer = hbmMask; | 
|---|
| 224 | hPointer = WinCreatePointerIndirect(HWND_DESKTOP, &pointerInfo); | 
|---|
| 225 |  | 
|---|
| 226 | if(hPointer == NULL) { | 
|---|
| 227 | dprintf(("OSLibWinCreatePointer: WinCreatePointerIndirect failed!")); | 
|---|
| 228 | } | 
|---|
| 229 | GpiDeleteBitmap(hbmMask); | 
|---|
| 230 | if(hbmColor) GpiDeleteBitmap(hbmColor); | 
|---|
| 231 | WinReleasePS(hps); | 
|---|
| 232 | return hPointer; | 
|---|
| 233 | } | 
|---|
| 234 | #endif | 
|---|
| 235 | //****************************************************************************** | 
|---|
| 236 | //****************************************************************************** | 
|---|
| 237 | BOOL isMonoBitmap(BITMAP_W *pXorBmp, PBYTE os2rgb) | 
|---|
| 238 | { | 
|---|
| 239 | ULONG pixel, color[2]; | 
|---|
| 240 | char *bmpdata; | 
|---|
| 241 | int i, j, nrcolors = 0, increment; | 
|---|
| 242 |  | 
|---|
| 243 | increment = pXorBmp->bmBitsPixel/8; | 
|---|
| 244 |  | 
|---|
| 245 | for(i=0;i<pXorBmp->bmHeight;i++) { | 
|---|
| 246 | bmpdata = (char *)os2rgb; | 
|---|
| 247 | for(j=0;j<pXorBmp->bmWidth;j++) { | 
|---|
| 248 | pixel = 0; | 
|---|
| 249 | memcpy(&pixel, os2rgb, increment); | 
|---|
| 250 | if(nrcolors == 0) { | 
|---|
| 251 | color[0] = pixel; | 
|---|
| 252 | nrcolors = 1; | 
|---|
| 253 | } | 
|---|
| 254 | else | 
|---|
| 255 | if(nrcolors == 1 && color[0] != pixel) { | 
|---|
| 256 | color[1] = pixel; | 
|---|
| 257 | nrcolors = 2; | 
|---|
| 258 | } | 
|---|
| 259 | else { | 
|---|
| 260 | if(color[0] != pixel && color[1] != pixel) | 
|---|
| 261 | { | 
|---|
| 262 | return FALSE; | 
|---|
| 263 | } | 
|---|
| 264 | } | 
|---|
| 265 | os2rgb += increment; | 
|---|
| 266 | } | 
|---|
| 267 | os2rgb = bmpdata + pXorBmp->bmWidthBytes; | 
|---|
| 268 | } | 
|---|
| 269 | return TRUE; | 
|---|
| 270 | } | 
|---|
| 271 | //****************************************************************************** | 
|---|
| 272 | //****************************************************************************** | 
|---|
| 273 | char *colorToMonoBitmap(HBITMAP bmpsrc, BITMAPINFO2 *pBmpDest) | 
|---|
| 274 | { | 
|---|
| 275 | HDC hdcDest = 0;            /* device context handle                */ | 
|---|
| 276 | HPS hpsDest = 0; | 
|---|
| 277 | SIZEL sizl = { 0, 0 };  /* use same page size as device         */ | 
|---|
| 278 | DEVOPENSTRUC dop = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L}; | 
|---|
| 279 | LONG lHits; | 
|---|
| 280 | char *bmpbuffer = 0; | 
|---|
| 281 | BITMAPINFO2 *bmpinfo = NULL; | 
|---|
| 282 | HAB hab; | 
|---|
| 283 |  | 
|---|
| 284 | dprintf2(("Convert color bitmap to mono (%d,%d) %d", pBmpDest->cx, pBmpDest->cy, pBmpDest->cBitCount)); | 
|---|
| 285 |  | 
|---|
| 286 | hab = GetThreadHAB(); | 
|---|
| 287 |  | 
|---|
| 288 | /* create memory device context */ | 
|---|
| 289 | hdcDest = DevOpenDC(hab, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&dop, NULLHANDLE); | 
|---|
| 290 |  | 
|---|
| 291 | /* Create the presentation and associate the memory device | 
|---|
| 292 | context. */ | 
|---|
| 293 | hpsDest = GpiCreatePS(hab, hdcDest, &sizl, PU_PELS | | 
|---|
| 294 | GPIT_MICRO | GPIA_ASSOC); | 
|---|
| 295 | if(!hpsDest) goto fail; | 
|---|
| 296 |  | 
|---|
| 297 | GpiSetBitmap(hpsDest, bmpsrc); | 
|---|
| 298 |  | 
|---|
| 299 | bmpinfo   = (BITMAPINFO2 *)malloc(2*sizeof(BITMAPINFOHEADER2) + sizeof(RGB2)); | 
|---|
| 300 | bmpbuffer = (char *)malloc(pBmpDest->cy*pBmpDest->cx); | 
|---|
| 301 | memset(bmpinfo, 0, sizeof(BITMAPINFOHEADER2) + sizeof(RGB2)); | 
|---|
| 302 | bmpinfo->cbFix            = sizeof(BITMAPINFOHEADER2); | 
|---|
| 303 | bmpinfo->cx               = pBmpDest->cx; | 
|---|
| 304 | bmpinfo->cy               = pBmpDest->cy; | 
|---|
| 305 | bmpinfo->cPlanes          = 1; | 
|---|
| 306 | bmpinfo->cBitCount        = 1; | 
|---|
| 307 | bmpinfo->ulCompression    = BCA_UNCOMP; | 
|---|
| 308 | bmpinfo->ulColorEncoding  = BCE_RGB; | 
|---|
| 309 |  | 
|---|
| 310 | lHits = GpiQueryBitmapBits(hpsDest, 0, pBmpDest->cy, bmpbuffer, bmpinfo); | 
|---|
| 311 | if(lHits == GPI_ERROR) goto fail; | 
|---|
| 312 |  | 
|---|
| 313 | //#define DEBUG_CURSOR | 
|---|
| 314 | #ifdef DEBUG_CURSOR | 
|---|
| 315 | { | 
|---|
| 316 | dprintf(("colorToMonoBitmap %d %d (%x,%x,%x)(%x,%x,%x)", pBmpDest->cx, pBmpDest->cy, bmpinfo->argbColor[0].bRed, bmpinfo->argbColor[0].bGreen, bmpinfo->argbColor[0].bBlue, bmpinfo->argbColor[1].bRed, bmpinfo->argbColor[1].bGreen, bmpinfo->argbColor[1].bBlue)); | 
|---|
| 317 | for(int i=pBmpDest->cy-1;i>=0;i--) { | 
|---|
| 318 | for(int j=0;j<pBmpDest->cx;j++) { | 
|---|
| 319 | if(j<8) { | 
|---|
| 320 | if((*(bmpbuffer+i*4)) & (1<<(7-j))) { | 
|---|
| 321 | WriteLogNoEOL("X"); | 
|---|
| 322 | } | 
|---|
| 323 | else WriteLogNoEOL("."); | 
|---|
| 324 | } | 
|---|
| 325 | else | 
|---|
| 326 | if(j<16) { | 
|---|
| 327 | if((*(bmpbuffer+1+i*4)) & (1<<(15-j))) { | 
|---|
| 328 | WriteLogNoEOL("X"); | 
|---|
| 329 | } | 
|---|
| 330 | else WriteLogNoEOL("."); | 
|---|
| 331 | } | 
|---|
| 332 | else | 
|---|
| 333 | if(j<24) { | 
|---|
| 334 | if((*(bmpbuffer+2+i*4)) & (1<<(23-j))) { | 
|---|
| 335 | WriteLogNoEOL("X"); | 
|---|
| 336 | } | 
|---|
| 337 | else WriteLogNoEOL("."); | 
|---|
| 338 | } | 
|---|
| 339 | else { | 
|---|
| 340 | if((*(bmpbuffer+3+i*4)) & (1<<(31-j))) { | 
|---|
| 341 | WriteLogNoEOL("X"); | 
|---|
| 342 | } | 
|---|
| 343 | else WriteLogNoEOL("."); | 
|---|
| 344 | } | 
|---|
| 345 | } | 
|---|
| 346 | WriteLogNoEOL("\n"); | 
|---|
| 347 | } | 
|---|
| 348 | } | 
|---|
| 349 | #endif | 
|---|
| 350 |  | 
|---|
| 351 | GpiSetBitmap(hpsDest, NULL); | 
|---|
| 352 |  | 
|---|
| 353 | GpiAssociate(hpsDest, NULLHANDLE); /* disassociate device context */ | 
|---|
| 354 | GpiDestroyPS(hpsDest);       /* destroys presentation space */ | 
|---|
| 355 | DevCloseDC(hdcDest);         /* closes device context       */ | 
|---|
| 356 | free(bmpinfo); | 
|---|
| 357 |  | 
|---|
| 358 | return bmpbuffer; | 
|---|
| 359 |  | 
|---|
| 360 | fail: | 
|---|
| 361 | if(bmpinfo)   free(bmpinfo); | 
|---|
| 362 | if(bmpbuffer) free(bmpbuffer); | 
|---|
| 363 |  | 
|---|
| 364 | if(hpsDest) { | 
|---|
| 365 | GpiSetBitmap(hpsDest, NULL); | 
|---|
| 366 | GpiAssociate(hpsDest, NULLHANDLE); /* disassociate device context */ | 
|---|
| 367 | GpiDestroyPS(hpsDest);       /* destroys presentation space */ | 
|---|
| 368 | } | 
|---|
| 369 | if(hdcDest) DevCloseDC(hdcDest);         /* closes device context       */ | 
|---|
| 370 | return 0; | 
|---|
| 371 | } | 
|---|
| 372 | //****************************************************************************** | 
|---|
| 373 | //****************************************************************************** | 
|---|
| 374 | //NOTE: Depends on origin of bitmap data!!! | 
|---|
| 375 | //      Assumes 1 bpp bitmaps have a top left origin and all others have a bottom left origin | 
|---|
| 376 | //****************************************************************************** | 
|---|
| 377 | HANDLE OSLibWinCreatePointer(CURSORICONINFO *pInfo, char *pAndBits, BITMAP_W *pAndBmp, char *pXorBits, | 
|---|
| 378 | BITMAP_W *pXorBmp, BOOL fCursor) | 
|---|
| 379 | { | 
|---|
| 380 | POINTERINFO  pointerInfo = {0}; | 
|---|
| 381 | HANDLE       hPointer; | 
|---|
| 382 | HBITMAP      hbmColor = 0, hbmMask = 0; | 
|---|
| 383 | BITMAPINFO2 *pBmpColor = 0, *pBmpMask = 0; | 
|---|
| 384 | int          masksize, colorsize, rgbsize, i; | 
|---|
| 385 | HPS          hps; | 
|---|
| 386 | char        *dest, *src, *pOS2XorBits = 0; | 
|---|
| 387 |  | 
|---|
| 388 | hps = WinGetScreenPS(HWND_DESKTOP); | 
|---|
| 389 |  | 
|---|
| 390 | if(pXorBits) | 
|---|
| 391 | {//color bitmap present | 
|---|
| 392 | RGBQUAD *rgb; | 
|---|
| 393 | RGB2    *os2rgb; | 
|---|
| 394 |  | 
|---|
| 395 | if(pXorBmp->bmBitsPixel <= 8) | 
|---|
| 396 | rgbsize = (1<<pXorBmp->bmBitsPixel)*sizeof(RGB2); | 
|---|
| 397 | else rgbsize = 0; | 
|---|
| 398 |  | 
|---|
| 399 | colorsize = sizeof(BITMAPINFO2) + (pXorBmp->bmHeight * pXorBmp->bmWidthBytes) + rgbsize; | 
|---|
| 400 | pBmpColor = (BITMAPINFO2 *)malloc(colorsize); | 
|---|
| 401 | if(pBmpColor == NULL) { | 
|---|
| 402 | DebugInt3(); | 
|---|
| 403 | return 0; | 
|---|
| 404 | } | 
|---|
| 405 | memset(pBmpColor, 0, colorsize); | 
|---|
| 406 | pBmpColor->cbFix            = sizeof(BITMAPINFOHEADER2); | 
|---|
| 407 | pBmpColor->cx               = (USHORT)pXorBmp->bmWidth; | 
|---|
| 408 | pBmpColor->cy               = (USHORT)pXorBmp->bmHeight; | 
|---|
| 409 | pBmpColor->cPlanes          = pXorBmp->bmPlanes; | 
|---|
| 410 | pBmpColor->cBitCount        = pXorBmp->bmBitsPixel; | 
|---|
| 411 | pBmpColor->ulCompression    = BCA_UNCOMP; | 
|---|
| 412 | pBmpColor->ulColorEncoding  = BCE_RGB; | 
|---|
| 413 |  | 
|---|
| 414 | os2rgb                      = &pBmpColor->argbColor[0]; | 
|---|
| 415 | rgb                         = (RGBQUAD *)(pXorBits); | 
|---|
| 416 |  | 
|---|
| 417 | #if 0 | 
|---|
| 418 | if(pXorBmp->bmBitsPixel == 1) { | 
|---|
| 419 | os2rgb->bRed = os2rgb->bBlue = os2rgb->bGreen = 0; | 
|---|
| 420 | os2rgb++; | 
|---|
| 421 | os2rgb->bRed = os2rgb->bBlue = os2rgb->bGreen = 0xff; | 
|---|
| 422 | os2rgb++; | 
|---|
| 423 | } | 
|---|
| 424 | else | 
|---|
| 425 | #endif | 
|---|
| 426 | if(pXorBmp->bmBitsPixel <= 8) { | 
|---|
| 427 | for(i=0;i<(1<<pXorBmp->bmBitsPixel);i++) { | 
|---|
| 428 | os2rgb->bRed   = rgb->rgbRed; | 
|---|
| 429 | os2rgb->bBlue  = rgb->rgbBlue; | 
|---|
| 430 | os2rgb->bGreen = rgb->rgbGreen; | 
|---|
| 431 | os2rgb++; | 
|---|
| 432 | rgb++; | 
|---|
| 433 | } | 
|---|
| 434 | } | 
|---|
| 435 |  | 
|---|
| 436 | if(pXorBmp->bmBitsPixel == 1) { | 
|---|
| 437 | //copy Xor bits (must reverse scanlines because origin is top left instead of bottom left) | 
|---|
| 438 | src  = (char *)rgb; | 
|---|
| 439 | dest = ((char *)os2rgb) + (pXorBmp->bmHeight - 1) * pXorBmp->bmWidthBytes; | 
|---|
| 440 | for(i=0;i<pXorBmp->bmHeight;i++) { | 
|---|
| 441 | memcpy(dest, src, pXorBmp->bmWidthBytes); | 
|---|
| 442 | dest -= pXorBmp->bmWidthBytes; | 
|---|
| 443 | src  += pXorBmp->bmWidthBytes; | 
|---|
| 444 | } | 
|---|
| 445 | } | 
|---|
| 446 | else | 
|---|
| 447 | if(pXorBmp->bmBitsPixel == 16) { | 
|---|
| 448 | ConvertRGB555to565(os2rgb, rgb, pXorBmp->bmHeight * pXorBmp->bmWidthBytes); | 
|---|
| 449 | } | 
|---|
| 450 | else    memcpy(os2rgb, rgb, pXorBmp->bmHeight * pXorBmp->bmWidthBytes); | 
|---|
| 451 |  | 
|---|
| 452 | hbmColor = GpiCreateBitmap(hps, (BITMAPINFOHEADER2 *)pBmpColor, CBM_INIT, | 
|---|
| 453 | (PBYTE)os2rgb, pBmpColor); | 
|---|
| 454 |  | 
|---|
| 455 | if(hbmColor == GPI_ERROR) { | 
|---|
| 456 | dprintf(("OSLibWinCreateIcon: GpiCreateBitmap failed!")); | 
|---|
| 457 | goto fail; | 
|---|
| 458 | } | 
|---|
| 459 | if(fCursor && pXorBmp->bmBitsPixel >= 8) | 
|---|
| 460 | { | 
|---|
| 461 | if(fForceMonoCursor || isMonoBitmap(pXorBmp, (PBYTE)os2rgb) == TRUE) | 
|---|
| 462 | { | 
|---|
| 463 | pOS2XorBits = colorToMonoBitmap(hbmColor, pBmpColor); | 
|---|
| 464 | if(pOS2XorBits) { | 
|---|
| 465 | GpiDeleteBitmap(hbmColor); | 
|---|
| 466 | hbmColor = 0; | 
|---|
| 467 | } | 
|---|
| 468 | } | 
|---|
| 469 | } | 
|---|
| 470 | if(hbmColor) { | 
|---|
| 471 | dprintf2(("OSLibWinCreatePointer: using real color cursor/icon (fCursor %d)", fCursor)); | 
|---|
| 472 | } | 
|---|
| 473 | else dprintf2(("OSLibWinCreatePointer: converted color cursor/icon to mono (fCursor %d)", fCursor)); | 
|---|
| 474 | } | 
|---|
| 475 |  | 
|---|
| 476 | //SvL: 2*sizeof(RGB2) is enough, but GpiCreateBitmap seems to touch more | 
|---|
| 477 | //     memory. (Adobe Photoshop 6 running in the debugger) | 
|---|
| 478 | //bird: We should reserve the amount required anythingelse is stupid. | 
|---|
| 479 | //      Looks like it's reading 3 bytes too much... Hopefully that's due to the | 
|---|
| 480 | //      &pBmpMask->argbColor[2] which it assumes is 16 colors long. But no proofs. | 
|---|
| 481 | masksize = sizeof(BITMAPINFO2) + (pAndBmp->bmHeight * 2 * pAndBmp->bmWidthBytes) + (16+2)*sizeof(RGB2); | 
|---|
| 482 | pBmpMask = (BITMAPINFO2 *)malloc(masksize); | 
|---|
| 483 | if(pBmpMask == NULL) { | 
|---|
| 484 | DebugInt3(); | 
|---|
| 485 | return 0; | 
|---|
| 486 | } | 
|---|
| 487 | memset(pBmpMask, 0, masksize); | 
|---|
| 488 | pBmpMask->cbFix             = sizeof(BITMAPINFOHEADER2); | 
|---|
| 489 | pBmpMask->cx                = (USHORT)pAndBmp->bmWidth; | 
|---|
| 490 | pBmpMask->cy                = (USHORT)pAndBmp->bmHeight*2; | 
|---|
| 491 | pBmpMask->cPlanes           = pAndBmp->bmPlanes; | 
|---|
| 492 | pBmpMask->cBitCount         = 1; | 
|---|
| 493 | pBmpMask->ulCompression     = BCA_UNCOMP; | 
|---|
| 494 | pBmpMask->ulColorEncoding   = BCE_RGB; | 
|---|
| 495 | memset(&pBmpMask->argbColor[0], 0, sizeof(RGB2)); | 
|---|
| 496 | memset(&pBmpMask->argbColor[1], 0xff, sizeof(RGB)); //not the reserved byte | 
|---|
| 497 | if(pOS2XorBits) { | 
|---|
| 498 | dest = ((char *)&pBmpMask->argbColor[2]); | 
|---|
| 499 | memcpy(dest, pOS2XorBits, pAndBmp->bmWidthBytes*pAndBmp->bmHeight); | 
|---|
| 500 | free(pOS2XorBits); | 
|---|
| 501 | pOS2XorBits = NULL; | 
|---|
| 502 | } | 
|---|
| 503 | // else Xor bits are already 0 | 
|---|
| 504 |  | 
|---|
| 505 | //copy And bits (must reverse scanlines because origin is top left instead of bottom left) | 
|---|
| 506 | src  = pAndBits; | 
|---|
| 507 | dest = ((char *)&pBmpMask->argbColor[2]) + (pAndBmp->bmHeight * 2 - 1) * (pAndBmp->bmWidthBytes); | 
|---|
| 508 | for(i=0;i<pAndBmp->bmHeight;i++) { | 
|---|
| 509 | memcpy(dest, src, pAndBmp->bmWidthBytes); | 
|---|
| 510 | dest -= pAndBmp->bmWidthBytes; | 
|---|
| 511 | src  += pAndBmp->bmWidthBytes; | 
|---|
| 512 | } | 
|---|
| 513 | hbmMask = GpiCreateBitmap(hps, (BITMAPINFOHEADER2 *)pBmpMask, CBM_INIT, | 
|---|
| 514 | (PBYTE)&pBmpMask->argbColor[2], pBmpMask); | 
|---|
| 515 |  | 
|---|
| 516 | if(hbmMask == GPI_ERROR) { | 
|---|
| 517 | dprintf(("OSLibWinCreatePointer: GpiCreateBitmap failed!")); | 
|---|
| 518 | goto fail; | 
|---|
| 519 | } | 
|---|
| 520 |  | 
|---|
| 521 | pointerInfo.fPointer   = fCursor; //FALSE = icon | 
|---|
| 522 | pointerInfo.xHotspot   = pInfo->ptHotSpot.x; | 
|---|
| 523 | pointerInfo.yHotspot   = mapY(pInfo->nHeight, pInfo->ptHotSpot.y); | 
|---|
| 524 | pointerInfo.hbmColor   = hbmColor; | 
|---|
| 525 | pointerInfo.hbmPointer = hbmMask; | 
|---|
| 526 | dprintf2(("WinCreatePointerIndirect %d (%d,%d) (org %d,%d)", pointerInfo.fPointer, pointerInfo.xHotspot, pointerInfo.yHotspot, pInfo->ptHotSpot.x, pInfo->ptHotSpot.y)); | 
|---|
| 527 | hPointer = WinCreatePointerIndirect(HWND_DESKTOP, &pointerInfo); | 
|---|
| 528 |  | 
|---|
| 529 | if(hPointer == NULL) { | 
|---|
| 530 | dprintf(("OSLibWinCreateCursor: WinCreatePointerIndirect failed! (lasterr=%x)", WinGetLastError(GetThreadHAB()))); | 
|---|
| 531 | } | 
|---|
| 532 | GpiDeleteBitmap(hbmMask); | 
|---|
| 533 | if(hbmColor) GpiDeleteBitmap(hbmColor); | 
|---|
| 534 | WinReleasePS(hps); | 
|---|
| 535 |  | 
|---|
| 536 | free(pBmpMask); | 
|---|
| 537 | free(pBmpColor); | 
|---|
| 538 |  | 
|---|
| 539 | dprintf2(("OSLibWinCreatePointer: PM pointer %x", hPointer)); | 
|---|
| 540 | return hPointer; | 
|---|
| 541 |  | 
|---|
| 542 | fail: | 
|---|
| 543 | if(hbmMask) GpiDeleteBitmap(hbmMask); | 
|---|
| 544 | if(hbmColor) GpiDeleteBitmap(hbmColor); | 
|---|
| 545 | WinReleasePS(hps); | 
|---|
| 546 | if(pBmpMask)  free(pBmpMask); | 
|---|
| 547 | if(pBmpColor) free(pBmpColor); | 
|---|
| 548 | return 0; | 
|---|
| 549 | } | 
|---|
| 550 | //****************************************************************************** | 
|---|
| 551 | //****************************************************************************** | 
|---|
| 552 | HANDLE OSLibWinQuerySysIcon(ULONG type,INT w,INT h) | 
|---|
| 553 | { | 
|---|
| 554 | ULONG os2type = 0; | 
|---|
| 555 | HPOINTER hPointer; | 
|---|
| 556 |  | 
|---|
| 557 | switch(type) { | 
|---|
| 558 | case IDI_APPLICATION_W: | 
|---|
| 559 | os2type = SPTR_PROGRAM; | 
|---|
| 560 | break; | 
|---|
| 561 | case IDI_HAND_W: | 
|---|
| 562 | os2type = SPTR_ICONWARNING; | 
|---|
| 563 | break; | 
|---|
| 564 | case IDI_QUESTION_W: | 
|---|
| 565 | os2type = SPTR_ICONQUESTION; | 
|---|
| 566 | break; | 
|---|
| 567 | case IDI_EXCLAMATION_W: | 
|---|
| 568 | os2type = SPTR_ICONWARNING; | 
|---|
| 569 | break; | 
|---|
| 570 | case IDI_ASTERISK_W: | 
|---|
| 571 | os2type = SPTR_ICONINFORMATION; | 
|---|
| 572 | break; | 
|---|
| 573 | default: | 
|---|
| 574 | return 0; | 
|---|
| 575 | } | 
|---|
| 576 |  | 
|---|
| 577 | hPointer = WinQuerySysPointer(HWND_DESKTOP, os2type, TRUE); | 
|---|
| 578 |  | 
|---|
| 579 | if (hPointer) | 
|---|
| 580 | { | 
|---|
| 581 | INT sysW = WinQuerySysValue(HWND_DESKTOP,SV_CXICON),sysH = WinQuerySysValue(HWND_DESKTOP,SV_CYICON); | 
|---|
| 582 |  | 
|---|
| 583 | if (sysW != w || sysH != h) | 
|---|
| 584 | { | 
|---|
| 585 | POINTERINFO pi; | 
|---|
| 586 |  | 
|---|
| 587 | WinQueryPointerInfo(hPointer,&pi); | 
|---|
| 588 | //CB: todo: change icon size | 
|---|
| 589 |  | 
|---|
| 590 | } | 
|---|
| 591 | } | 
|---|
| 592 |  | 
|---|
| 593 | return hPointer; | 
|---|
| 594 | } | 
|---|
| 595 | //****************************************************************************** | 
|---|
| 596 | //****************************************************************************** | 
|---|
| 597 | HANDLE OSLibWinQuerySysPointer(ULONG type,INT w,INT h) | 
|---|
| 598 | { | 
|---|
| 599 | ULONG os2type = 0; | 
|---|
| 600 |  | 
|---|
| 601 | switch(type) { | 
|---|
| 602 | case IDC_ARROW_W: | 
|---|
| 603 | os2type = SPTR_ARROW; | 
|---|
| 604 | break; | 
|---|
| 605 | case IDC_UPARROW_W: | 
|---|
| 606 | os2type = SPTR_ARROW; | 
|---|
| 607 | break; | 
|---|
| 608 | case IDC_IBEAM_W: | 
|---|
| 609 | os2type = SPTR_TEXT; | 
|---|
| 610 | break; | 
|---|
| 611 | case IDC_ICON_W: | 
|---|
| 612 | os2type = SPTR_PROGRAM; | 
|---|
| 613 | break; | 
|---|
| 614 | case IDC_NO_W: | 
|---|
| 615 | os2type = SPTR_ILLEGAL; | 
|---|
| 616 | break; | 
|---|
| 617 | case IDC_CROSS_W: | 
|---|
| 618 | os2type = SPTR_MOVE; | 
|---|
| 619 | break; | 
|---|
| 620 | case IDC_SIZE_W: | 
|---|
| 621 | os2type = SPTR_MOVE; | 
|---|
| 622 | break; | 
|---|
| 623 | case IDC_SIZEALL_W: | 
|---|
| 624 | os2type = SPTR_MOVE; | 
|---|
| 625 | break; | 
|---|
| 626 | case IDC_SIZENESW_W: | 
|---|
| 627 | os2type = SPTR_SIZENESW; | 
|---|
| 628 | break; | 
|---|
| 629 | case IDC_SIZENS_W: | 
|---|
| 630 | os2type = SPTR_SIZENS; | 
|---|
| 631 | break; | 
|---|
| 632 | case IDC_SIZENWSE_W: | 
|---|
| 633 | os2type = SPTR_SIZENWSE; | 
|---|
| 634 | break; | 
|---|
| 635 | case IDC_SIZEWE_W: | 
|---|
| 636 | os2type = SPTR_SIZEWE; | 
|---|
| 637 | break; | 
|---|
| 638 | case IDC_WAIT_W: | 
|---|
| 639 | os2type = SPTR_WAIT; | 
|---|
| 640 | break; | 
|---|
| 641 | case IDC_APPSTARTING_W: | 
|---|
| 642 | os2type = SPTR_WAIT; | 
|---|
| 643 | break; | 
|---|
| 644 | case IDC_HELP_W: //TODO: Create a cursor for this one | 
|---|
| 645 | os2type = SPTR_WAIT; | 
|---|
| 646 | break; | 
|---|
| 647 | default: | 
|---|
| 648 | return 0; | 
|---|
| 649 | } | 
|---|
| 650 | //Note: Does not create a copy | 
|---|
| 651 | return WinQuerySysPointer(HWND_DESKTOP, os2type, FALSE); | 
|---|
| 652 | } | 
|---|
| 653 | //****************************************************************************** | 
|---|
| 654 | //****************************************************************************** | 
|---|
| 655 | VOID OSLibWinDestroyPointer(HANDLE hPointer) | 
|---|
| 656 | { | 
|---|
| 657 | WinDestroyPointer(hPointer); | 
|---|
| 658 | } | 
|---|
| 659 | //****************************************************************************** | 
|---|
| 660 | //****************************************************************************** | 
|---|
| 661 | BOOL OSLibWinSetPointer(HANDLE hPointer) | 
|---|
| 662 | { | 
|---|
| 663 | return WinSetPointer(HWND_DESKTOP, hPointer); | 
|---|
| 664 | } | 
|---|
| 665 | //****************************************************************************** | 
|---|
| 666 | //****************************************************************************** | 
|---|
| 667 | HANDLE  OSLibWinQueryPointer() | 
|---|
| 668 | { | 
|---|
| 669 | return WinQueryPointer(HWND_DESKTOP); | 
|---|
| 670 | } | 
|---|
| 671 | //****************************************************************************** | 
|---|
| 672 | //****************************************************************************** | 
|---|
| 673 | BOOL OSLibWinClipCursor(const RECT * pRect) | 
|---|
| 674 | { | 
|---|
| 675 | RECTL  rectl; | 
|---|
| 676 | PRECTL ptr = NULL; | 
|---|
| 677 |  | 
|---|
| 678 | if (pRect != NULL) | 
|---|
| 679 | { | 
|---|
| 680 | rectl.xLeft = max(pRect->left, 0); | 
|---|
| 681 | rectl.xRight = min(pRect->right, ScreenWidth-1); | 
|---|
| 682 | rectl.yBottom = max(ScreenHeight - pRect->bottom, 0); | 
|---|
| 683 | rectl.yTop = min(ScreenHeight - pRect->top, ScreenHeight-1); | 
|---|
| 684 | ptr = &rectl; | 
|---|
| 685 | } | 
|---|
| 686 | return WinSetPointerClipRect (HWND_DESKTOP, ptr); | 
|---|
| 687 | } | 
|---|
| 688 | //****************************************************************************** | 
|---|
| 689 | //****************************************************************************** | 
|---|
| 690 | BOOL OSLibWinGetClipCursor(LPRECT pRect) | 
|---|
| 691 | { | 
|---|
| 692 | RECTL  rectl; | 
|---|
| 693 |  | 
|---|
| 694 | if (WinQueryPointerClipRect(HWND_DESKTOP, &rectl)) | 
|---|
| 695 | { | 
|---|
| 696 | pRect->left   = rectl.xLeft; | 
|---|
| 697 | pRect->right  = rectl.xRight; | 
|---|
| 698 | pRect->bottom = ScreenHeight - rectl.yBottom; | 
|---|
| 699 | pRect->top    = ScreenHeight - rectl.yTop; | 
|---|
| 700 | return TRUE; | 
|---|
| 701 | } | 
|---|
| 702 | return FALSE; | 
|---|
| 703 | } | 
|---|
| 704 | //****************************************************************************** | 
|---|
| 705 | //****************************************************************************** | 
|---|