2 * GDI Device Context functions
4 * Copyright 1993 Alexandre Julliard
15 #include "debugtools.h"
21 #include "wine/winuser16.h"
23 DEFAULT_DEBUG_CHANNEL(dc);
25 /***********************************************************************
28 * Fill the WIN_DC_INFO structure.
30 static void DC_Init_DC_INFO( WIN_DC_INFO *win_dc_info )
32 win_dc_info->flags = 0;
33 win_dc_info->devCaps = NULL;
34 win_dc_info->hClipRgn = 0;
35 win_dc_info->hVisRgn = 0;
36 win_dc_info->hGCClipRgn = 0;
37 win_dc_info->hPen = STOCK_BLACK_PEN;
38 win_dc_info->hBrush = STOCK_WHITE_BRUSH;
39 win_dc_info->hFont = STOCK_SYSTEM_FONT;
40 win_dc_info->hBitmap = 0;
41 win_dc_info->hDevice = 0;
42 win_dc_info->hPalette = STOCK_DEFAULT_PALETTE;
43 win_dc_info->ROPmode = R2_COPYPEN;
44 win_dc_info->polyFillMode = ALTERNATE;
45 win_dc_info->stretchBltMode = BLACKONWHITE;
46 win_dc_info->relAbsMode = ABSOLUTE;
47 win_dc_info->backgroundMode = OPAQUE;
48 win_dc_info->backgroundColor = RGB( 255, 255, 255 );
49 win_dc_info->textColor = RGB( 0, 0, 0 );
50 win_dc_info->brushOrgX = 0;
51 win_dc_info->brushOrgY = 0;
52 win_dc_info->textAlign = TA_LEFT | TA_TOP | TA_NOUPDATECP;
53 win_dc_info->charExtra = 0;
54 win_dc_info->breakTotalExtra = 0;
55 win_dc_info->breakCount = 0;
56 win_dc_info->breakExtra = 0;
57 win_dc_info->breakRem = 0;
58 win_dc_info->bitsPerPixel = 1;
59 win_dc_info->MapMode = MM_TEXT;
60 win_dc_info->GraphicsMode = GM_COMPATIBLE;
61 win_dc_info->DCOrgX = 0;
62 win_dc_info->DCOrgY = 0;
63 win_dc_info->pAbortProc = NULL;
64 win_dc_info->CursPosX = 0;
65 win_dc_info->CursPosY = 0;
66 win_dc_info->ArcDirection = AD_COUNTERCLOCKWISE;
67 win_dc_info->xformWorld2Wnd.eM11 = 1.0f;
68 win_dc_info->xformWorld2Wnd.eM12 = 0.0f;
69 win_dc_info->xformWorld2Wnd.eM21 = 0.0f;
70 win_dc_info->xformWorld2Wnd.eM22 = 1.0f;
71 win_dc_info->xformWorld2Wnd.eDx = 0.0f;
72 win_dc_info->xformWorld2Wnd.eDy = 0.0f;
73 win_dc_info->xformWorld2Vport = win_dc_info->xformWorld2Wnd;
74 win_dc_info->xformVport2World = win_dc_info->xformWorld2Wnd;
75 win_dc_info->vport2WorldValid = TRUE;
77 PATH_InitGdiPath(&win_dc_info->path);
81 /***********************************************************************
84 DC *DC_AllocDC( const DC_FUNCTIONS *funcs )
89 if (!(dc = GDI_AllocObject( sizeof(DC), DC_MAGIC, &hdc ))) return NULL;
106 DC_Init_DC_INFO( &dc->w );
113 /***********************************************************************
116 DC *DC_GetDCPtr( HDC hdc )
118 GDIOBJHDR *ptr = GDI_GetObjPtr( hdc, MAGIC_DONTCARE );
119 if (!ptr) return NULL;
120 if ((GDIMAGIC(ptr->wMagic) == DC_MAGIC) ||
121 (GDIMAGIC(ptr->wMagic) == METAFILE_DC_MAGIC) ||
122 (GDIMAGIC(ptr->wMagic) == ENHMETAFILE_DC_MAGIC))
124 GDI_ReleaseObj( hdc );
125 SetLastError( ERROR_INVALID_HANDLE );
129 /***********************************************************************
132 * Retrieve a DC ptr while making sure the visRgn is updated.
133 * This function may call up to USER so the GDI lock should _not_
134 * be held when calling it.
136 DC *DC_GetDCUpdate( HDC hdc )
138 DC *dc = DC_GetDCPtr( hdc );
139 if (!dc) return NULL;
140 while (dc->w.flags & DC_DIRTY)
142 dc->w.flags &= ~DC_DIRTY;
143 if (!(dc->w.flags & (DC_SAVED | DC_MEMORY)))
145 DCHOOKPROC proc = dc->hookThunk;
148 DWORD data = dc->dwHookData;
149 GDI_ReleaseObj( hdc );
150 proc( hdc, DCHC_INVALIDVISRGN, data, 0 );
151 if (!(dc = DC_GetDCPtr( hdc ))) break;
152 /* otherwise restart the loop in case it became dirty again in the meantime */
159 /***********************************************************************
162 * Setup device-specific DC values for a newly created DC.
164 void DC_InitDC( DC* dc )
166 RealizeDefaultPalette16( dc->hSelf );
167 SetTextColor( dc->hSelf, dc->w.textColor );
168 SetBkColor( dc->hSelf, dc->w.backgroundColor );
169 SelectObject( dc->hSelf, dc->w.hPen );
170 SelectObject( dc->hSelf, dc->w.hBrush );
171 SelectObject( dc->hSelf, dc->w.hFont );
172 CLIPPING_UpdateGCRegion( dc );
176 /***********************************************************************
179 * Computes the inverse of the transformation xformSrc and stores it to
180 * xformDest. Returns TRUE if successful or FALSE if the xformSrc matrix
183 static BOOL DC_InvertXform( const XFORM *xformSrc, XFORM *xformDest )
187 determinant = xformSrc->eM11*xformSrc->eM22 -
188 xformSrc->eM12*xformSrc->eM21;
189 if (determinant > -1e-12 && determinant < 1e-12)
192 xformDest->eM11 = xformSrc->eM22 / determinant;
193 xformDest->eM12 = -xformSrc->eM12 / determinant;
194 xformDest->eM21 = -xformSrc->eM21 / determinant;
195 xformDest->eM22 = xformSrc->eM11 / determinant;
196 xformDest->eDx = -xformSrc->eDx * xformDest->eM11 -
197 xformSrc->eDy * xformDest->eM21;
198 xformDest->eDy = -xformSrc->eDx * xformDest->eM12 -
199 xformSrc->eDy * xformDest->eM22;
205 /***********************************************************************
208 * Updates the xformWorld2Vport, xformVport2World and vport2WorldValid
209 * fields of the specified DC by creating a transformation that
210 * represents the current mapping mode and combining it with the DC's
211 * world transform. This function should be called whenever the
212 * parameters associated with the mapping mode (window and viewport
213 * extents and origins) or the world transform change.
215 void DC_UpdateXforms( DC *dc )
217 XFORM xformWnd2Vport;
218 FLOAT scaleX, scaleY;
220 /* Construct a transformation to do the window-to-viewport conversion */
221 scaleX = (FLOAT)dc->vportExtX / (FLOAT)dc->wndExtX;
222 scaleY = (FLOAT)dc->vportExtY / (FLOAT)dc->wndExtY;
223 xformWnd2Vport.eM11 = scaleX;
224 xformWnd2Vport.eM12 = 0.0;
225 xformWnd2Vport.eM21 = 0.0;
226 xformWnd2Vport.eM22 = scaleY;
227 xformWnd2Vport.eDx = (FLOAT)dc->vportOrgX -
228 scaleX * (FLOAT)dc->wndOrgX;
229 xformWnd2Vport.eDy = (FLOAT)dc->vportOrgY -
230 scaleY * (FLOAT)dc->wndOrgY;
232 /* Combine with the world transformation */
233 CombineTransform( &dc->w.xformWorld2Vport, &dc->w.xformWorld2Wnd,
236 /* Create inverse of world-to-viewport transformation */
237 dc->w.vport2WorldValid = DC_InvertXform( &dc->w.xformWorld2Vport,
238 &dc->w.xformVport2World );
242 /***********************************************************************
243 * GetDCState (GDI.179)
245 HDC16 WINAPI GetDCState16( HDC16 hdc )
250 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
251 if (!(newdc = GDI_AllocObject( sizeof(DC), DC_MAGIC, &handle )))
253 GDI_ReleaseObj( hdc );
256 TRACE("(%04x): returning %04x\n", hdc, handle );
258 newdc->w.flags = dc->w.flags | DC_SAVED;
259 newdc->w.devCaps = dc->w.devCaps;
260 newdc->w.hPen = dc->w.hPen;
261 newdc->w.hBrush = dc->w.hBrush;
262 newdc->w.hFont = dc->w.hFont;
263 newdc->w.hBitmap = dc->w.hBitmap;
264 newdc->w.hDevice = dc->w.hDevice;
265 newdc->w.hPalette = dc->w.hPalette;
266 newdc->w.totalExtent = dc->w.totalExtent;
267 newdc->w.bitsPerPixel = dc->w.bitsPerPixel;
268 newdc->w.ROPmode = dc->w.ROPmode;
269 newdc->w.polyFillMode = dc->w.polyFillMode;
270 newdc->w.stretchBltMode = dc->w.stretchBltMode;
271 newdc->w.relAbsMode = dc->w.relAbsMode;
272 newdc->w.backgroundMode = dc->w.backgroundMode;
273 newdc->w.backgroundColor = dc->w.backgroundColor;
274 newdc->w.textColor = dc->w.textColor;
275 newdc->w.brushOrgX = dc->w.brushOrgX;
276 newdc->w.brushOrgY = dc->w.brushOrgY;
277 newdc->w.textAlign = dc->w.textAlign;
278 newdc->w.charExtra = dc->w.charExtra;
279 newdc->w.breakTotalExtra = dc->w.breakTotalExtra;
280 newdc->w.breakCount = dc->w.breakCount;
281 newdc->w.breakExtra = dc->w.breakExtra;
282 newdc->w.breakRem = dc->w.breakRem;
283 newdc->w.MapMode = dc->w.MapMode;
284 newdc->w.GraphicsMode = dc->w.GraphicsMode;
286 /* Apparently, the DC origin is not changed by [GS]etDCState */
287 newdc->w.DCOrgX = dc->w.DCOrgX;
288 newdc->w.DCOrgY = dc->w.DCOrgY;
290 newdc->w.CursPosX = dc->w.CursPosX;
291 newdc->w.CursPosY = dc->w.CursPosY;
292 newdc->w.ArcDirection = dc->w.ArcDirection;
293 newdc->w.xformWorld2Wnd = dc->w.xformWorld2Wnd;
294 newdc->w.xformWorld2Vport = dc->w.xformWorld2Vport;
295 newdc->w.xformVport2World = dc->w.xformVport2World;
296 newdc->w.vport2WorldValid = dc->w.vport2WorldValid;
297 newdc->wndOrgX = dc->wndOrgX;
298 newdc->wndOrgY = dc->wndOrgY;
299 newdc->wndExtX = dc->wndExtX;
300 newdc->wndExtY = dc->wndExtY;
301 newdc->vportOrgX = dc->vportOrgX;
302 newdc->vportOrgY = dc->vportOrgY;
303 newdc->vportExtX = dc->vportExtX;
304 newdc->vportExtY = dc->vportExtY;
306 newdc->hSelf = (HDC)handle;
307 newdc->saveLevel = 0;
309 PATH_InitGdiPath( &newdc->w.path );
311 newdc->w.pAbortProc = NULL;
312 newdc->hookThunk = NULL;
314 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
316 newdc->w.hGCClipRgn = newdc->w.hVisRgn = 0;
319 newdc->w.hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
320 CombineRgn( newdc->w.hClipRgn, dc->w.hClipRgn, 0, RGN_COPY );
323 newdc->w.hClipRgn = 0;
324 GDI_ReleaseObj( handle );
325 GDI_ReleaseObj( hdc );
330 /***********************************************************************
331 * SetDCState (GDI.180)
333 void WINAPI SetDCState16( HDC16 hdc, HDC16 hdcs )
337 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return;
338 if (!(dcs = (DC *) GDI_GetObjPtr( hdcs, DC_MAGIC )))
340 GDI_ReleaseObj( hdc );
343 if (!dcs->w.flags & DC_SAVED)
345 GDI_ReleaseObj( hdc );
346 GDI_ReleaseObj( hdcs );
349 TRACE("%04x %04x\n", hdc, hdcs );
351 dc->w.flags = dcs->w.flags & ~DC_SAVED;
352 dc->w.devCaps = dcs->w.devCaps;
353 dc->w.hDevice = dcs->w.hDevice;
354 dc->w.totalExtent = dcs->w.totalExtent;
355 dc->w.ROPmode = dcs->w.ROPmode;
356 dc->w.polyFillMode = dcs->w.polyFillMode;
357 dc->w.stretchBltMode = dcs->w.stretchBltMode;
358 dc->w.relAbsMode = dcs->w.relAbsMode;
359 dc->w.backgroundMode = dcs->w.backgroundMode;
360 dc->w.backgroundColor = dcs->w.backgroundColor;
361 dc->w.textColor = dcs->w.textColor;
362 dc->w.brushOrgX = dcs->w.brushOrgX;
363 dc->w.brushOrgY = dcs->w.brushOrgY;
364 dc->w.textAlign = dcs->w.textAlign;
365 dc->w.charExtra = dcs->w.charExtra;
366 dc->w.breakTotalExtra = dcs->w.breakTotalExtra;
367 dc->w.breakCount = dcs->w.breakCount;
368 dc->w.breakExtra = dcs->w.breakExtra;
369 dc->w.breakRem = dcs->w.breakRem;
370 dc->w.MapMode = dcs->w.MapMode;
371 dc->w.GraphicsMode = dcs->w.GraphicsMode;
373 /* Apparently, the DC origin is not changed by [GS]etDCState */
374 dc->w.DCOrgX = dcs->w.DCOrgX;
375 dc->w.DCOrgY = dcs->w.DCOrgY;
377 dc->w.CursPosX = dcs->w.CursPosX;
378 dc->w.CursPosY = dcs->w.CursPosY;
379 dc->w.ArcDirection = dcs->w.ArcDirection;
380 dc->w.xformWorld2Wnd = dcs->w.xformWorld2Wnd;
381 dc->w.xformWorld2Vport = dcs->w.xformWorld2Vport;
382 dc->w.xformVport2World = dcs->w.xformVport2World;
383 dc->w.vport2WorldValid = dcs->w.vport2WorldValid;
385 dc->wndOrgX = dcs->wndOrgX;
386 dc->wndOrgY = dcs->wndOrgY;
387 dc->wndExtX = dcs->wndExtX;
388 dc->wndExtY = dcs->wndExtY;
389 dc->vportOrgX = dcs->vportOrgX;
390 dc->vportOrgY = dcs->vportOrgY;
391 dc->vportExtX = dcs->vportExtX;
392 dc->vportExtY = dcs->vportExtY;
394 if (!(dc->w.flags & DC_MEMORY)) dc->w.bitsPerPixel = dcs->w.bitsPerPixel;
398 if (!dc->w.hClipRgn) dc->w.hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
399 CombineRgn( dc->w.hClipRgn, dcs->w.hClipRgn, 0, RGN_COPY );
403 if (dc->w.hClipRgn) DeleteObject16( dc->w.hClipRgn );
406 CLIPPING_UpdateGCRegion( dc );
408 SelectObject( hdc, dcs->w.hBitmap );
409 SelectObject( hdc, dcs->w.hBrush );
410 SelectObject( hdc, dcs->w.hFont );
411 SelectObject( hdc, dcs->w.hPen );
412 SetBkColor( hdc, dcs->w.backgroundColor);
413 SetTextColor( hdc, dcs->w.textColor);
414 GDISelectPalette16( hdc, dcs->w.hPalette, FALSE );
415 GDI_ReleaseObj( hdcs );
416 GDI_ReleaseObj( hdc );
420 /***********************************************************************
423 INT16 WINAPI SaveDC16( HDC16 hdc )
425 return (INT16)SaveDC( hdc );
429 /***********************************************************************
432 INT WINAPI SaveDC( HDC hdc )
438 dc = DC_GetDCUpdate( hdc );
441 if(dc->funcs->pSaveDC)
443 ret = dc->funcs->pSaveDC( dc );
444 GDI_ReleaseObj( hdc );
448 if (!(hdcs = GetDCState16( hdc )))
450 GDI_ReleaseObj( hdc );
453 dcs = GDI_GetObjPtr( hdcs, DC_MAGIC );
455 /* Copy path. The reason why path saving / restoring is in SaveDC/
456 * RestoreDC and not in GetDCState/SetDCState is that the ...DCState
457 * functions are only in Win16 (which doesn't have paths) and that
458 * SetDCState doesn't allow us to signal an error (which can happen
459 * when copying paths).
461 if (!PATH_AssignGdiPath( &dcs->w.path, &dc->w.path ))
463 GDI_ReleaseObj( hdc );
464 GDI_ReleaseObj( hdcs );
469 dcs->header.hNext = dc->header.hNext;
470 dc->header.hNext = hdcs;
471 TRACE("(%04x): returning %d\n", hdc, dc->saveLevel+1 );
472 ret = ++dc->saveLevel;
473 GDI_ReleaseObj( hdcs );
474 GDI_ReleaseObj( hdc );
479 /***********************************************************************
480 * RestoreDC16 (GDI.39)
482 BOOL16 WINAPI RestoreDC16( HDC16 hdc, INT16 level )
484 return RestoreDC( hdc, level );
488 /***********************************************************************
489 * RestoreDC (GDI32.290)
491 BOOL WINAPI RestoreDC( HDC hdc, INT level )
496 TRACE("%04x %d\n", hdc, level );
497 dc = DC_GetDCPtr( hdc );
498 if(!dc) return FALSE;
499 if(dc->funcs->pRestoreDC)
501 success = dc->funcs->pRestoreDC( dc, level );
502 GDI_ReleaseObj( hdc );
506 if (level == -1) level = dc->saveLevel;
508 /* This pair of checks disagrees with MSDN "Platform SDK:
509 Windows GDI" July 2000 which says all negative values
510 for level will be interpreted as an instance relative
511 to the current state. Restricting it to just -1 does
513 || (level > dc->saveLevel))
515 GDI_ReleaseObj( hdc );
520 while (dc->saveLevel >= level)
522 HDC16 hdcs = dc->header.hNext;
523 if (!(dcs = (DC *) GDI_GetObjPtr( hdcs, DC_MAGIC )))
525 GDI_ReleaseObj( hdc );
528 dc->header.hNext = dcs->header.hNext;
529 if (--dc->saveLevel < level)
531 SetDCState16( hdc, hdcs );
532 if (!PATH_AssignGdiPath( &dc->w.path, &dcs->w.path ))
533 /* FIXME: This might not be quite right, since we're
534 * returning FALSE but still destroying the saved DC state */
537 GDI_ReleaseObj( hdcs );
540 GDI_ReleaseObj( hdc );
545 /***********************************************************************
546 * CreateDC16 (GDI.53)
548 HDC16 WINAPI CreateDC16( LPCSTR driver, LPCSTR device, LPCSTR output,
549 const DEVMODEA *initData )
553 const DC_FUNCTIONS *funcs;
556 if (!device || !DRIVER_GetDriverName( device, buf, sizeof(buf) ))
559 if (!(funcs = DRIVER_FindDriver( buf ))) return 0;
560 if (!(dc = DC_AllocDC( funcs ))) return 0;
563 TRACE("(driver=%s, device=%s, output=%s): returning %04x\n",
564 debugstr_a(driver), debugstr_a(device), debugstr_a(output), dc->hSelf );
566 if (dc->funcs->pCreateDC &&
567 !dc->funcs->pCreateDC( dc, buf, device, output, initData ))
569 WARN("creation aborted by device\n" );
570 GDI_FreeObject( dc->hSelf, dc );
576 GDI_ReleaseObj( hdc );
581 /***********************************************************************
584 HDC WINAPI CreateDCA( LPCSTR driver, LPCSTR device, LPCSTR output,
585 const DEVMODEA *initData )
587 return CreateDC16( driver, device, output, (const DEVMODEA *)initData );
591 /***********************************************************************
594 HDC WINAPI CreateDCW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
595 const DEVMODEW *initData )
597 LPSTR driverA = HEAP_strdupWtoA( GetProcessHeap(), 0, driver );
598 LPSTR deviceA = HEAP_strdupWtoA( GetProcessHeap(), 0, device );
599 LPSTR outputA = HEAP_strdupWtoA( GetProcessHeap(), 0, output );
600 HDC res = CreateDC16( driverA, deviceA, outputA,
601 (const DEVMODEA *)initData /*FIXME*/ );
602 HeapFree( GetProcessHeap(), 0, driverA );
603 HeapFree( GetProcessHeap(), 0, deviceA );
604 HeapFree( GetProcessHeap(), 0, outputA );
609 /***********************************************************************
610 * CreateIC16 (GDI.153)
612 HDC16 WINAPI CreateIC16( LPCSTR driver, LPCSTR device, LPCSTR output,
613 const DEVMODEA* initData )
615 /* Nothing special yet for ICs */
616 return CreateDC16( driver, device, output, initData );
620 /***********************************************************************
621 * CreateICA (GDI32.49)
623 HDC WINAPI CreateICA( LPCSTR driver, LPCSTR device, LPCSTR output,
624 const DEVMODEA* initData )
626 /* Nothing special yet for ICs */
627 return CreateDCA( driver, device, output, initData );
631 /***********************************************************************
632 * CreateICW (GDI32.50)
634 HDC WINAPI CreateICW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
635 const DEVMODEW* initData )
637 /* Nothing special yet for ICs */
638 return CreateDCW( driver, device, output, initData );
642 /***********************************************************************
643 * CreateCompatibleDC16 (GDI.52)
645 HDC16 WINAPI CreateCompatibleDC16( HDC16 hdc )
647 return (HDC16)CreateCompatibleDC( hdc );
651 /***********************************************************************
652 * CreateCompatibleDC (GDI32.31)
654 HDC WINAPI CreateCompatibleDC( HDC hdc )
657 const DC_FUNCTIONS *funcs;
659 if ((origDC = (DC *)GDI_GetObjPtr( hdc, DC_MAGIC ))) funcs = origDC->funcs;
660 else funcs = DRIVER_FindDriver( "DISPLAY" );
662 if (!funcs || !(dc = DC_AllocDC( funcs )))
664 if (origDC) GDI_ReleaseObj( hdc );
668 TRACE("(%04x): returning %04x\n",
671 dc->w.flags = DC_MEMORY;
672 dc->w.bitsPerPixel = 1;
673 dc->w.hBitmap = hPseudoStockBitmap;
675 /* Copy the driver-specific physical device info into
676 * the new DC. The driver may use this read-only info
677 * while creating the compatible DC below. */
679 dc->physDev = origDC->physDev;
681 if (dc->funcs->pCreateDC &&
682 !dc->funcs->pCreateDC( dc, NULL, NULL, NULL, NULL ))
684 WARN("creation aborted by device\n");
685 GDI_FreeObject( dc->hSelf, dc );
686 if (origDC) GDI_ReleaseObj( hdc );
691 GDI_ReleaseObj( dc->hSelf );
692 if (origDC) GDI_ReleaseObj( hdc );
697 /***********************************************************************
698 * DeleteDC16 (GDI.68)
700 BOOL16 WINAPI DeleteDC16( HDC16 hdc )
702 return DeleteDC( hdc );
706 /***********************************************************************
707 * DeleteDC (GDI32.67)
709 BOOL WINAPI DeleteDC( HDC hdc )
711 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
712 if (!dc) return FALSE;
714 TRACE("%04x\n", hdc );
716 /* Call hook procedure to check whether is it OK to delete this DC */
717 if (dc->hookThunk && !(dc->w.flags & (DC_SAVED | DC_MEMORY)))
719 DCHOOKPROC proc = dc->hookThunk;
722 DWORD data = dc->dwHookData;
723 GDI_ReleaseObj( hdc );
724 if (!proc( hdc, DCHC_DELETEDC, data, 0 )) return FALSE;
725 if (!(dc = DC_GetDCPtr( hdc ))) return FALSE;
729 while (dc->saveLevel)
732 HDC16 hdcs = dc->header.hNext;
733 if (!(dcs = (DC *) GDI_GetObjPtr( hdcs, DC_MAGIC ))) break;
734 dc->header.hNext = dcs->header.hNext;
736 GDI_ReleaseObj( hdcs );
740 if (!(dc->w.flags & DC_SAVED))
742 SelectObject( hdc, STOCK_BLACK_PEN );
743 SelectObject( hdc, STOCK_WHITE_BRUSH );
744 SelectObject( hdc, STOCK_SYSTEM_FONT );
745 if (dc->funcs->pDeleteDC) dc->funcs->pDeleteDC(dc);
748 if (dc->w.hClipRgn) DeleteObject( dc->w.hClipRgn );
749 if (dc->w.hVisRgn) DeleteObject( dc->w.hVisRgn );
750 if (dc->w.hGCClipRgn) DeleteObject( dc->w.hGCClipRgn );
751 if (dc->w.pAbortProc) THUNK_Free( (FARPROC)dc->w.pAbortProc );
752 if (dc->hookThunk) THUNK_Free( (FARPROC)dc->hookThunk );
753 PATH_DestroyGdiPath(&dc->w.path);
755 return GDI_FreeObject( hdc, dc );
759 /***********************************************************************
760 * ResetDC16 (GDI.376)
762 HDC16 WINAPI ResetDC16( HDC16 hdc, const DEVMODEA *devmode )
769 /***********************************************************************
770 * ResetDCA (GDI32.287)
772 HDC WINAPI ResetDCA( HDC hdc, const DEVMODEA *devmode )
779 /***********************************************************************
780 * ResetDCW (GDI32.288)
782 HDC WINAPI ResetDCW( HDC hdc, const DEVMODEW *devmode )
789 /***********************************************************************
790 * GetDeviceCaps16 (GDI.80)
792 INT16 WINAPI GetDeviceCaps16( HDC16 hdc, INT16 cap )
794 INT16 ret = GetDeviceCaps( hdc, cap );
795 /* some apps don't expect -1 and think it's a B&W screen */
796 if ((cap == NUMCOLORS) && (ret == -1)) ret = 2048;
801 /***********************************************************************
802 * GetDeviceCaps (GDI32.171)
804 INT WINAPI GetDeviceCaps( HDC hdc, INT cap )
806 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
812 /* Device capabilities for the printer */
816 if(Escape(hdc, GETPHYSPAGESIZE, 0, NULL, (LPVOID)&pt) > 0) ret = pt.x;
819 if(Escape(hdc, GETPHYSPAGESIZE, 0, NULL, (LPVOID)&pt) > 0) ret = pt.y;
821 case PHYSICALOFFSETX:
822 if(Escape(hdc, GETPRINTINGOFFSET, 0, NULL, (LPVOID)&pt) > 0) ret = pt.x;
824 case PHYSICALOFFSETY:
825 if(Escape(hdc, GETPRINTINGOFFSET, 0, NULL, (LPVOID)&pt) > 0) ret = pt.y;
828 if(Escape(hdc, GETSCALINGFACTOR, 0, NULL, (LPVOID)&pt) > 0) ret = pt.x;
831 if(Escape(hdc, GETSCALINGFACTOR, 0, NULL, (LPVOID)&pt) > 0) ret = pt.y;
834 if ((cap < 0) || (cap > sizeof(DeviceCaps)-sizeof(WORD))) break;
836 if (((cap>=46) && (cap<88)) || ((cap>=92) && (cap<104)))
837 FIXME("(%04x,%d): unsupported DeviceCaps capability, will yield 0!\n",
839 ret = *(WORD *)(((char *)dc->w.devCaps) + cap);
841 if ((cap == NUMCOLORS) && (ret == 0xffff))
846 TRACE("(%04x,%d): returning %d\n", hdc, cap, ret );
847 GDI_ReleaseObj( hdc );
852 /***********************************************************************
853 * SetBkColor16 (GDI.1)
855 COLORREF WINAPI SetBkColor16( HDC16 hdc, COLORREF color )
857 return SetBkColor( hdc, color );
861 /***********************************************************************
862 * SetBkColor (GDI32.305)
864 COLORREF WINAPI SetBkColor( HDC hdc, COLORREF color )
867 DC * dc = DC_GetDCPtr( hdc );
869 if (!dc) return 0x80000000;
870 if (dc->funcs->pSetBkColor)
871 oldColor = dc->funcs->pSetBkColor(dc, color);
873 oldColor = dc->w.backgroundColor;
874 dc->w.backgroundColor = color;
876 GDI_ReleaseObj( hdc );
881 /***********************************************************************
882 * SetTextColor16 (GDI.9)
884 COLORREF WINAPI SetTextColor16( HDC16 hdc, COLORREF color )
886 return SetTextColor( hdc, color );
890 /***********************************************************************
891 * SetTextColor (GDI32.338)
893 COLORREF WINAPI SetTextColor( HDC hdc, COLORREF color )
896 DC * dc = DC_GetDCPtr( hdc );
898 if (!dc) return 0x80000000;
899 if (dc->funcs->pSetTextColor)
900 oldColor = dc->funcs->pSetTextColor(dc, color);
902 oldColor = dc->w.textColor;
903 dc->w.textColor = color;
905 GDI_ReleaseObj( hdc );
909 /***********************************************************************
910 * SetTextAlign16 (GDI.346)
912 UINT16 WINAPI SetTextAlign16( HDC16 hdc, UINT16 align )
914 return SetTextAlign( hdc, align );
918 /***********************************************************************
919 * SetTextAlign (GDI32.336)
921 UINT WINAPI SetTextAlign( HDC hdc, UINT align )
924 DC *dc = DC_GetDCPtr( hdc );
926 if (dc->funcs->pSetTextAlign)
927 prevAlign = dc->funcs->pSetTextAlign(dc, align);
929 prevAlign = dc->w.textAlign;
930 dc->w.textAlign = align;
932 GDI_ReleaseObj( hdc );
936 /***********************************************************************
937 * GetDCOrgEx (GDI32.168)
939 BOOL WINAPI GetDCOrgEx( HDC hDC, LPPOINT lpp )
943 if (!lpp) return FALSE;
944 if (!(dc = (DC *) GDI_GetObjPtr( hDC, DC_MAGIC ))) return FALSE;
947 if (dc->funcs->pGetDCOrgEx) dc->funcs->pGetDCOrgEx( dc, lpp );
948 lpp->x += dc->w.DCOrgX;
949 lpp->y += dc->w.DCOrgY;
950 GDI_ReleaseObj( hDC );
955 /***********************************************************************
958 DWORD WINAPI GetDCOrg16( HDC16 hdc )
961 if( GetDCOrgEx( hdc, &pt) )
962 return MAKELONG( (WORD)pt.x, (WORD)pt.y );
967 /***********************************************************************
970 DWORD WINAPI SetDCOrg16( HDC16 hdc, INT16 x, INT16 y )
973 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
975 prevOrg = dc->w.DCOrgX | (dc->w.DCOrgY << 16);
978 GDI_ReleaseObj( hdc );
983 /***********************************************************************
984 * SetGraphicsMode (GDI32.317)
986 INT WINAPI SetGraphicsMode( HDC hdc, INT mode )
989 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
991 /* One would think that setting the graphics mode to GM_COMPATIBLE
992 * would also reset the world transformation matrix to the unity
993 * matrix. However, in Windows, this is not the case. This doesn't
994 * make a lot of sense to me, but that's the way it is.
998 if ((mode > 0) || (mode <= GM_LAST))
1000 ret = dc->w.GraphicsMode;
1001 dc->w.GraphicsMode = mode;
1003 GDI_ReleaseObj( hdc );
1008 /***********************************************************************
1009 * SetArcDirection16 (GDI.525)
1011 INT16 WINAPI SetArcDirection16( HDC16 hdc, INT16 nDirection )
1013 return SetArcDirection( (HDC)hdc, (INT)nDirection );
1017 /***********************************************************************
1018 * SetArcDirection (GDI32.302)
1020 INT WINAPI SetArcDirection( HDC hdc, INT nDirection )
1023 INT nOldDirection = 0;
1025 if (nDirection!=AD_COUNTERCLOCKWISE && nDirection!=AD_CLOCKWISE)
1027 SetLastError(ERROR_INVALID_PARAMETER);
1031 if ((dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC )))
1033 nOldDirection = dc->w.ArcDirection;
1034 dc->w.ArcDirection = nDirection;
1035 GDI_ReleaseObj( hdc );
1037 return nOldDirection;
1041 /***********************************************************************
1042 * GetWorldTransform (GDI32.244)
1044 BOOL WINAPI GetWorldTransform( HDC hdc, LPXFORM xform )
1047 if (!xform) return FALSE;
1048 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return FALSE;
1049 *xform = dc->w.xformWorld2Wnd;
1050 GDI_ReleaseObj( hdc );
1055 /***********************************************************************
1056 * SetWorldTransform (GDI32.346)
1058 BOOL WINAPI SetWorldTransform( HDC hdc, const XFORM *xform )
1061 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1065 SetLastError( ERROR_INVALID_HANDLE );
1069 if (!xform) goto done;
1071 /* Check that graphics mode is GM_ADVANCED */
1072 if (dc->w.GraphicsMode!=GM_ADVANCED) goto done;
1074 dc->w.xformWorld2Wnd = *xform;
1075 DC_UpdateXforms( dc );
1078 GDI_ReleaseObj( hdc );
1083 /****************************************************************************
1084 * ModifyWorldTransform [GDI32.253]
1085 * Modifies the world transformation for a device context.
1088 * hdc [I] Handle to device context
1089 * xform [I] XFORM structure that will be used to modify the world
1091 * iMode [I] Specifies in what way to modify the world transformation
1094 * Resets the world transformation to the identity matrix.
1095 * The parameter xform is ignored.
1097 * Multiplies xform into the world transformation matrix from
1100 * Multiplies xform into the world transformation matrix from
1105 BOOL WINAPI ModifyWorldTransform( HDC hdc, const XFORM *xform,
1109 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1111 /* Check for illegal parameters */
1114 SetLastError( ERROR_INVALID_HANDLE );
1117 if (!xform) goto done;
1119 /* Check that graphics mode is GM_ADVANCED */
1120 if (dc->w.GraphicsMode!=GM_ADVANCED) goto done;
1125 dc->w.xformWorld2Wnd.eM11 = 1.0f;
1126 dc->w.xformWorld2Wnd.eM12 = 0.0f;
1127 dc->w.xformWorld2Wnd.eM21 = 0.0f;
1128 dc->w.xformWorld2Wnd.eM22 = 1.0f;
1129 dc->w.xformWorld2Wnd.eDx = 0.0f;
1130 dc->w.xformWorld2Wnd.eDy = 0.0f;
1132 case MWT_LEFTMULTIPLY:
1133 CombineTransform( &dc->w.xformWorld2Wnd, xform,
1134 &dc->w.xformWorld2Wnd );
1136 case MWT_RIGHTMULTIPLY:
1137 CombineTransform( &dc->w.xformWorld2Wnd, &dc->w.xformWorld2Wnd,
1144 DC_UpdateXforms( dc );
1147 GDI_ReleaseObj( hdc );
1152 /****************************************************************************
1153 * CombineTransform [GDI32.20]
1154 * Combines two transformation matrices.
1157 * xformResult [O] Stores the result of combining the two matrices
1158 * xform1 [I] Specifies the first matrix to apply
1159 * xform2 [I] Specifies the second matrix to apply
1162 * The same matrix can be passed in for more than one of the parameters.
1166 BOOL WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
1167 const XFORM *xform2 )
1171 /* Check for illegal parameters */
1172 if (!xformResult || !xform1 || !xform2)
1175 /* Create the result in a temporary XFORM, since xformResult may be
1176 * equal to xform1 or xform2 */
1177 xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
1178 xform1->eM12 * xform2->eM21;
1179 xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
1180 xform1->eM12 * xform2->eM22;
1181 xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
1182 xform1->eM22 * xform2->eM21;
1183 xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
1184 xform1->eM22 * xform2->eM22;
1185 xformTemp.eDx = xform1->eDx * xform2->eM11 +
1186 xform1->eDy * xform2->eM21 +
1188 xformTemp.eDy = xform1->eDx * xform2->eM12 +
1189 xform1->eDy * xform2->eM22 +
1192 /* Copy the result to xformResult */
1193 *xformResult = xformTemp;
1199 /***********************************************************************
1200 * SetDCHook (GDI.190)
1202 /* ### start build ### */
1203 extern WORD CALLBACK GDI_CallTo16_word_wwll(FARPROC16,WORD,WORD,LONG,LONG);
1204 /* ### stop build ### */
1205 BOOL16 WINAPI SetDCHook( HDC16 hdc, FARPROC16 hookProc, DWORD dwHookData )
1207 DC *dc = (DC *)GDI_GetObjPtr( hdc, DC_MAGIC );
1208 if (!dc) return FALSE;
1211 * Note: We store the original SEGPTR 'hookProc' as we need to be
1212 * able to return it verbatim in GetDCHook,
1214 * On the other hand, we still call THUNK_Alloc and store the
1215 * 32-bit thunk into another DC member, because THUNK_Alloc
1216 * recognizes the (typical) case that the 'hookProc' is indeed
1217 * the 16-bit API entry point of a built-in routine (e.g. DCHook16)
1219 * We could perform that test every time the hook is about to
1220 * be called (or else we could live with the 32->16->32 detour),
1221 * but this way is the most efficient ...
1224 dc->hookProc = hookProc;
1225 dc->dwHookData = dwHookData;
1227 THUNK_Free( (FARPROC)dc->hookThunk );
1228 dc->hookThunk = (DCHOOKPROC)
1229 THUNK_Alloc( hookProc, (RELAY)GDI_CallTo16_word_wwll );
1231 GDI_ReleaseObj( hdc );
1236 /***********************************************************************
1237 * GetDCHook (GDI.191)
1239 DWORD WINAPI GetDCHook( HDC16 hdc, FARPROC16 *phookProc )
1241 DC *dc = (DC *)GDI_GetObjPtr( hdc, DC_MAGIC );
1243 *phookProc = dc->hookProc;
1244 GDI_ReleaseObj( hdc );
1245 return dc->dwHookData;
1249 /***********************************************************************
1250 * SetHookFlags (GDI.192)
1252 WORD WINAPI SetHookFlags16(HDC16 hDC, WORD flags)
1254 DC* dc = (DC*)GDI_GetObjPtr( hDC, DC_MAGIC );
1258 WORD wRet = dc->w.flags & DC_DIRTY;
1260 /* "Undocumented Windows" info is slightly confusing.
1263 TRACE("hDC %04x, flags %04x\n",hDC,flags);
1265 if( flags & DCHF_INVALIDATEVISRGN )
1266 dc->w.flags |= DC_DIRTY;
1267 else if( flags & DCHF_VALIDATEVISRGN || !flags )
1268 dc->w.flags &= ~DC_DIRTY;
1269 GDI_ReleaseObj( hDC );
1275 /***********************************************************************
1276 * SetICMMode (GDI32.318)
1278 INT WINAPI SetICMMode(HDC hdc, INT iEnableICM)
1280 /*FIXME Asuming that ICM is always off, and cannot be turned on */
1281 if (iEnableICM == ICM_OFF) return ICM_OFF;
1282 if (iEnableICM == ICM_ON) return 0;
1283 if (iEnableICM == ICM_QUERY) return ICM_OFF;
1288 /***********************************************************************
1289 * GetColorSpace (GDI32.165)
1291 HCOLORSPACE WINAPI GetColorSpace(HDC hdc)
1293 /*FIXME Need to to whatever GetColorSpace actually does */
1297 /***********************************************************************
1298 * CreateColorSpaceA (GDI32.???)
1300 HCOLORSPACE WINAPI CreateColorSpaceA( LPLOGCOLORSPACEA lpLogColorSpace )
1306 /***********************************************************************
1307 * CreateColorSpaceW (GDI32.???)
1309 HCOLORSPACE WINAPI CreateColorSpaceW( LPLOGCOLORSPACEW lpLogColorSpace )
1315 /***********************************************************************
1316 * DeleteColorSpace (GDI32.???)
1318 BOOL WINAPI DeleteColorSpace( HCOLORSPACE hColorSpace )
1325 /***********************************************************************
1326 * SetColorSpace (GDI32.???)
1328 HCOLORSPACE WINAPI SetColorSpace( HDC hDC, HCOLORSPACE hColorSpace )
1335 /***********************************************************************
1336 * GetBoundsRect16 (GDI.194)
1338 UINT16 WINAPI GetBoundsRect16(HDC16 hdc, LPRECT16 rect, UINT16 flags)
1340 return DCB_RESET | DCB_DISABLE; /* bounding rectangle always empty and disabled*/
1343 /***********************************************************************
1344 * GetBoundsRect (GDI32.147)
1346 UINT WINAPI GetBoundsRect(HDC hdc, LPRECT rect, UINT flags)
1348 FIXME("(): stub\n");
1349 return DCB_RESET; /* bounding rectangle always empty */
1352 /***********************************************************************
1353 * SetBoundsRect16 (GDI.193)
1355 UINT16 WINAPI SetBoundsRect16(HDC16 hdc, const RECT16* rect, UINT16 flags)
1357 if ( (flags & DCB_ACCUMULATE) || (flags & DCB_ENABLE) )
1358 FIXME("(%04x, %p, %04x): stub\n", hdc, rect, flags );
1360 return DCB_RESET | DCB_DISABLE; /* bounding rectangle always empty and disabled*/
1363 /***********************************************************************
1364 * SetBoundsRect (GDI32.307)
1366 UINT WINAPI SetBoundsRect(HDC hdc, const RECT* rect, UINT flags)
1368 FIXME("(): stub\n");
1369 return DCB_DISABLE; /* bounding rectangle always empty */
1373 /***********************************************************************
1374 * GetRelAbs (GDI32.218)
1376 INT WINAPI GetRelAbs( HDC hdc, DWORD dwIgnore )
1378 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1380 return dc->w.relAbsMode;
1383 /***********************************************************************
1386 * Disables GDI, switches back to text mode.
1387 * We don't have to do anything here,
1388 * just let console support handle everything
1390 void WINAPI Death16(HDC16 hDC)
1392 MESSAGE("Death(%04x) called. Application enters text mode...\n", hDC);
1395 /***********************************************************************
1396 * Resurrection (GDI.122)
1398 * Restores GDI functionality
1400 void WINAPI Resurrection16(HDC16 hDC,
1401 WORD w1, WORD w2, WORD w3, WORD w4, WORD w5, WORD w6)
1403 MESSAGE("Resurrection(%04x, %04x, %04x, %04x, %04x, %04x, %04x) called. Application left text mode.\n", hDC, w1, w2, w3, w4, w5, w6);
1406 /***********************************************************************
1407 * GetLayout (GDI32.321)
1409 * Gets left->right or right->left text layout flags of a dc.
1410 * win98 just returns 0 and sets ERROR_CALL_NOT_IMPLEMENTED so we do the same
1413 DWORD WINAPI GetLayout(HDC hdc)
1415 FIXME("(%08x): stub\n", hdc);
1416 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1420 /***********************************************************************
1421 * SetLayout (GDI32.450)
1423 * Sets left->right or right->left text layout flags of a dc.
1424 * win98 just returns 0 and sets ERROR_CALL_NOT_IMPLEMENTED so we do the same
1427 DWORD WINAPI SetLayout(HDC hdc, DWORD layout)
1429 FIXME("(%08x,%08lx): stub\n", hdc, layout);
1430 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);