2 * GDI Device Context functions
4 * Copyright 1993 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "wine/winuser16.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(dc);
36 static BOOL DC_DeleteObject( HGDIOBJ handle, void *obj );
38 static const struct gdi_obj_funcs dc_funcs =
40 NULL, /* pSelectObject */
41 NULL, /* pGetObject16 */
42 NULL, /* pGetObjectA */
43 NULL, /* pGetObjectW */
44 NULL, /* pUnrealizeObject */
45 DC_DeleteObject /* pDeleteObject */
48 /***********************************************************************
51 DC *DC_AllocDC( const DC_FUNCTIONS *funcs )
56 if (!(dc = GDI_AllocObject( sizeof(*dc), DC_MAGIC, (HGDIOBJ*)&hdc, &dc_funcs ))) return NULL;
77 dc->hPen = GetStockObject( BLACK_PEN );
78 dc->hBrush = GetStockObject( WHITE_BRUSH );
79 dc->hFont = GetStockObject( SYSTEM_FONT );
82 dc->hPalette = GetStockObject( DEFAULT_PALETTE );
84 dc->ROPmode = R2_COPYPEN;
85 dc->polyFillMode = ALTERNATE;
86 dc->stretchBltMode = BLACKONWHITE;
87 dc->relAbsMode = ABSOLUTE;
88 dc->backgroundMode = OPAQUE;
89 dc->backgroundColor = RGB( 255, 255, 255 );
90 dc->textColor = RGB( 0, 0, 0 );
93 dc->textAlign = TA_LEFT | TA_TOP | TA_NOUPDATECP;
95 dc->breakTotalExtra = 0;
99 dc->totalExtent.left = 0;
100 dc->totalExtent.top = 0;
101 dc->totalExtent.right = 0;
102 dc->totalExtent.bottom = 0;
103 dc->bitsPerPixel = 1;
104 dc->MapMode = MM_TEXT;
105 dc->GraphicsMode = GM_COMPATIBLE;
106 dc->pAbortProc = NULL;
109 dc->ArcDirection = AD_COUNTERCLOCKWISE;
110 dc->xformWorld2Wnd.eM11 = 1.0f;
111 dc->xformWorld2Wnd.eM12 = 0.0f;
112 dc->xformWorld2Wnd.eM21 = 0.0f;
113 dc->xformWorld2Wnd.eM22 = 1.0f;
114 dc->xformWorld2Wnd.eDx = 0.0f;
115 dc->xformWorld2Wnd.eDy = 0.0f;
116 dc->xformWorld2Vport = dc->xformWorld2Wnd;
117 dc->xformVport2World = dc->xformWorld2Wnd;
118 dc->vport2WorldValid = TRUE;
119 PATH_InitGdiPath(&dc->path);
125 /***********************************************************************
128 DC *DC_GetDCPtr( HDC hdc )
130 GDIOBJHDR *ptr = GDI_GetObjPtr( hdc, MAGIC_DONTCARE );
131 if (!ptr) return NULL;
132 if ((GDIMAGIC(ptr->wMagic) == DC_MAGIC) ||
133 (GDIMAGIC(ptr->wMagic) == METAFILE_DC_MAGIC) ||
134 (GDIMAGIC(ptr->wMagic) == ENHMETAFILE_DC_MAGIC))
136 GDI_ReleaseObj( hdc );
137 SetLastError( ERROR_INVALID_HANDLE );
141 /***********************************************************************
144 * Retrieve a DC ptr while making sure the visRgn is updated.
145 * This function may call up to USER so the GDI lock should _not_
146 * be held when calling it.
148 DC *DC_GetDCUpdate( HDC hdc )
150 DC *dc = DC_GetDCPtr( hdc );
151 if (!dc) return NULL;
152 while (dc->flags & DC_DIRTY)
154 dc->flags &= ~DC_DIRTY;
155 if (!(dc->flags & (DC_SAVED | DC_MEMORY)))
157 DCHOOKPROC proc = dc->hookThunk;
160 DWORD data = dc->dwHookData;
161 GDI_ReleaseObj( hdc );
162 proc( HDC_16(hdc), DCHC_INVALIDVISRGN, data, 0 );
163 if (!(dc = DC_GetDCPtr( hdc ))) break;
164 /* otherwise restart the loop in case it became dirty again in the meantime */
172 /***********************************************************************
175 static BOOL DC_DeleteObject( HGDIOBJ handle, void *obj )
177 GDI_ReleaseObj( handle );
178 return DeleteDC( handle );
182 /***********************************************************************
185 * Setup device-specific DC values for a newly created DC.
187 void DC_InitDC( DC* dc )
189 if (dc->funcs->pRealizeDefaultPalette) dc->funcs->pRealizeDefaultPalette( dc->physDev );
190 SetTextColor( dc->hSelf, dc->textColor );
191 SetBkColor( dc->hSelf, dc->backgroundColor );
192 SelectObject( dc->hSelf, dc->hPen );
193 SelectObject( dc->hSelf, dc->hBrush );
194 SelectObject( dc->hSelf, dc->hFont );
195 CLIPPING_UpdateGCRegion( dc );
199 /***********************************************************************
202 * Computes the inverse of the transformation xformSrc and stores it to
203 * xformDest. Returns TRUE if successful or FALSE if the xformSrc matrix
206 static BOOL DC_InvertXform( const XFORM *xformSrc, XFORM *xformDest )
210 determinant = xformSrc->eM11*xformSrc->eM22 -
211 xformSrc->eM12*xformSrc->eM21;
212 if (determinant > -1e-12 && determinant < 1e-12)
215 xformDest->eM11 = xformSrc->eM22 / determinant;
216 xformDest->eM12 = -xformSrc->eM12 / determinant;
217 xformDest->eM21 = -xformSrc->eM21 / determinant;
218 xformDest->eM22 = xformSrc->eM11 / determinant;
219 xformDest->eDx = -xformSrc->eDx * xformDest->eM11 -
220 xformSrc->eDy * xformDest->eM21;
221 xformDest->eDy = -xformSrc->eDx * xformDest->eM12 -
222 xformSrc->eDy * xformDest->eM22;
228 /***********************************************************************
231 * Updates the xformWorld2Vport, xformVport2World and vport2WorldValid
232 * fields of the specified DC by creating a transformation that
233 * represents the current mapping mode and combining it with the DC's
234 * world transform. This function should be called whenever the
235 * parameters associated with the mapping mode (window and viewport
236 * extents and origins) or the world transform change.
238 void DC_UpdateXforms( DC *dc )
240 XFORM xformWnd2Vport;
241 FLOAT scaleX, scaleY;
243 /* Construct a transformation to do the window-to-viewport conversion */
244 scaleX = (FLOAT)dc->vportExtX / (FLOAT)dc->wndExtX;
245 scaleY = (FLOAT)dc->vportExtY / (FLOAT)dc->wndExtY;
246 xformWnd2Vport.eM11 = scaleX;
247 xformWnd2Vport.eM12 = 0.0;
248 xformWnd2Vport.eM21 = 0.0;
249 xformWnd2Vport.eM22 = scaleY;
250 xformWnd2Vport.eDx = (FLOAT)dc->vportOrgX -
251 scaleX * (FLOAT)dc->wndOrgX;
252 xformWnd2Vport.eDy = (FLOAT)dc->vportOrgY -
253 scaleY * (FLOAT)dc->wndOrgY;
255 /* Combine with the world transformation */
256 CombineTransform( &dc->xformWorld2Vport, &dc->xformWorld2Wnd,
259 /* Create inverse of world-to-viewport transformation */
260 dc->vport2WorldValid = DC_InvertXform( &dc->xformWorld2Vport,
261 &dc->xformVport2World );
265 /***********************************************************************
266 * GetDCState (Not a Windows API)
268 HDC WINAPI GetDCState( HDC hdc )
273 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
274 if (!(newdc = GDI_AllocObject( sizeof(DC), DC_MAGIC, &handle, &dc_funcs )))
276 GDI_ReleaseObj( hdc );
279 TRACE("(%p): returning %p\n", hdc, handle );
281 newdc->flags = dc->flags | DC_SAVED;
282 newdc->hPen = dc->hPen;
283 newdc->hBrush = dc->hBrush;
284 newdc->hFont = dc->hFont;
285 newdc->hBitmap = dc->hBitmap;
286 newdc->hDevice = dc->hDevice;
287 newdc->hPalette = dc->hPalette;
288 newdc->totalExtent = dc->totalExtent;
289 newdc->bitsPerPixel = dc->bitsPerPixel;
290 newdc->ROPmode = dc->ROPmode;
291 newdc->polyFillMode = dc->polyFillMode;
292 newdc->stretchBltMode = dc->stretchBltMode;
293 newdc->relAbsMode = dc->relAbsMode;
294 newdc->backgroundMode = dc->backgroundMode;
295 newdc->backgroundColor = dc->backgroundColor;
296 newdc->textColor = dc->textColor;
297 newdc->brushOrgX = dc->brushOrgX;
298 newdc->brushOrgY = dc->brushOrgY;
299 newdc->textAlign = dc->textAlign;
300 newdc->charExtra = dc->charExtra;
301 newdc->breakTotalExtra = dc->breakTotalExtra;
302 newdc->breakCount = dc->breakCount;
303 newdc->breakExtra = dc->breakExtra;
304 newdc->breakRem = dc->breakRem;
305 newdc->MapMode = dc->MapMode;
306 newdc->GraphicsMode = dc->GraphicsMode;
307 newdc->CursPosX = dc->CursPosX;
308 newdc->CursPosY = dc->CursPosY;
309 newdc->ArcDirection = dc->ArcDirection;
310 newdc->xformWorld2Wnd = dc->xformWorld2Wnd;
311 newdc->xformWorld2Vport = dc->xformWorld2Vport;
312 newdc->xformVport2World = dc->xformVport2World;
313 newdc->vport2WorldValid = dc->vport2WorldValid;
314 newdc->wndOrgX = dc->wndOrgX;
315 newdc->wndOrgY = dc->wndOrgY;
316 newdc->wndExtX = dc->wndExtX;
317 newdc->wndExtY = dc->wndExtY;
318 newdc->vportOrgX = dc->vportOrgX;
319 newdc->vportOrgY = dc->vportOrgY;
320 newdc->vportExtX = dc->vportExtX;
321 newdc->vportExtY = dc->vportExtY;
323 newdc->hSelf = (HDC)handle;
324 newdc->saveLevel = 0;
326 PATH_InitGdiPath( &newdc->path );
328 newdc->pAbortProc = NULL;
329 newdc->hookThunk = NULL;
332 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
334 newdc->hGCClipRgn = newdc->hVisRgn = 0;
337 newdc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
338 CombineRgn( newdc->hClipRgn, dc->hClipRgn, 0, RGN_COPY );
344 newdc->gdiFont = dc->gdiFont;
348 GDI_ReleaseObj( handle );
349 GDI_ReleaseObj( hdc );
354 /***********************************************************************
355 * SetDCState (Not a Windows API)
357 void WINAPI SetDCState( HDC hdc, HDC hdcs )
361 if (!(dc = DC_GetDCUpdate( hdc ))) return;
362 if (!(dcs = DC_GetDCPtr( hdcs )))
364 GDI_ReleaseObj( hdc );
367 if (!dcs->flags & DC_SAVED)
369 GDI_ReleaseObj( hdc );
370 GDI_ReleaseObj( hdcs );
373 TRACE("%p %p\n", hdc, hdcs );
375 dc->flags = dcs->flags & ~(DC_SAVED | DC_DIRTY);
376 dc->hDevice = dcs->hDevice;
377 dc->totalExtent = dcs->totalExtent;
378 dc->ROPmode = dcs->ROPmode;
379 dc->polyFillMode = dcs->polyFillMode;
380 dc->stretchBltMode = dcs->stretchBltMode;
381 dc->relAbsMode = dcs->relAbsMode;
382 dc->backgroundMode = dcs->backgroundMode;
383 dc->backgroundColor = dcs->backgroundColor;
384 dc->textColor = dcs->textColor;
385 dc->brushOrgX = dcs->brushOrgX;
386 dc->brushOrgY = dcs->brushOrgY;
387 dc->textAlign = dcs->textAlign;
388 dc->charExtra = dcs->charExtra;
389 dc->breakTotalExtra = dcs->breakTotalExtra;
390 dc->breakCount = dcs->breakCount;
391 dc->breakExtra = dcs->breakExtra;
392 dc->breakRem = dcs->breakRem;
393 dc->MapMode = dcs->MapMode;
394 dc->GraphicsMode = dcs->GraphicsMode;
395 dc->CursPosX = dcs->CursPosX;
396 dc->CursPosY = dcs->CursPosY;
397 dc->ArcDirection = dcs->ArcDirection;
398 dc->xformWorld2Wnd = dcs->xformWorld2Wnd;
399 dc->xformWorld2Vport = dcs->xformWorld2Vport;
400 dc->xformVport2World = dcs->xformVport2World;
401 dc->vport2WorldValid = dcs->vport2WorldValid;
403 dc->wndOrgX = dcs->wndOrgX;
404 dc->wndOrgY = dcs->wndOrgY;
405 dc->wndExtX = dcs->wndExtX;
406 dc->wndExtY = dcs->wndExtY;
407 dc->vportOrgX = dcs->vportOrgX;
408 dc->vportOrgY = dcs->vportOrgY;
409 dc->vportExtX = dcs->vportExtX;
410 dc->vportExtY = dcs->vportExtY;
412 if (!(dc->flags & DC_MEMORY)) dc->bitsPerPixel = dcs->bitsPerPixel;
416 if (!dc->hClipRgn) dc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
417 CombineRgn( dc->hClipRgn, dcs->hClipRgn, 0, RGN_COPY );
421 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
424 CLIPPING_UpdateGCRegion( dc );
426 SelectObject( hdc, dcs->hBitmap );
427 SelectObject( hdc, dcs->hBrush );
428 SelectObject( hdc, dcs->hFont );
429 SelectObject( hdc, dcs->hPen );
430 SetBkColor( hdc, dcs->backgroundColor);
431 SetTextColor( hdc, dcs->textColor);
432 GDISelectPalette( hdc, dcs->hPalette, FALSE );
433 GDI_ReleaseObj( hdcs );
434 GDI_ReleaseObj( hdc );
438 /***********************************************************************
439 * GetDCState (GDI.179)
441 HDC16 WINAPI GetDCState16( HDC16 hdc )
443 return HDC_16( GetDCState( HDC_32(hdc) ));
447 /***********************************************************************
448 * SetDCState (GDI.180)
450 void WINAPI SetDCState16( HDC16 hdc, HDC16 hdcs )
452 SetDCState( HDC_32(hdc), HDC_32(hdcs) );
456 /***********************************************************************
459 INT WINAPI SaveDC( HDC hdc )
465 dc = DC_GetDCPtr( hdc );
468 if(dc->funcs->pSaveDC)
470 ret = dc->funcs->pSaveDC( dc->physDev );
471 GDI_ReleaseObj( hdc );
475 if (!(hdcs = GetDCState( hdc )))
477 GDI_ReleaseObj( hdc );
480 dcs = DC_GetDCPtr( hdcs );
482 /* Copy path. The reason why path saving / restoring is in SaveDC/
483 * RestoreDC and not in GetDCState/SetDCState is that the ...DCState
484 * functions are only in Win16 (which doesn't have paths) and that
485 * SetDCState doesn't allow us to signal an error (which can happen
486 * when copying paths).
488 if (!PATH_AssignGdiPath( &dcs->path, &dc->path ))
490 GDI_ReleaseObj( hdc );
491 GDI_ReleaseObj( hdcs );
496 dcs->header.hNext = dc->header.hNext;
497 dc->header.hNext = HDC_16(hdcs);
498 TRACE("(%p): returning %d\n", hdc, dc->saveLevel+1 );
499 ret = ++dc->saveLevel;
500 GDI_ReleaseObj( hdcs );
501 GDI_ReleaseObj( hdc );
506 /***********************************************************************
507 * RestoreDC (GDI32.@)
509 BOOL WINAPI RestoreDC( HDC hdc, INT level )
514 TRACE("%p %d\n", hdc, level );
515 dc = DC_GetDCUpdate( hdc );
516 if(!dc) return FALSE;
517 if(dc->funcs->pRestoreDC)
519 success = dc->funcs->pRestoreDC( dc->physDev, level );
520 GDI_ReleaseObj( hdc );
524 if (level == -1) level = dc->saveLevel;
526 /* This pair of checks disagrees with MSDN "Platform SDK:
527 Windows GDI" July 2000 which says all negative values
528 for level will be interpreted as an instance relative
529 to the current state. Restricting it to just -1 does
531 || (level > dc->saveLevel))
533 GDI_ReleaseObj( hdc );
538 while (dc->saveLevel >= level)
540 HDC hdcs = HDC_32(dc->header.hNext);
541 if (!(dcs = DC_GetDCPtr( hdcs )))
543 GDI_ReleaseObj( hdc );
546 dc->header.hNext = dcs->header.hNext;
547 if (--dc->saveLevel < level)
549 SetDCState( hdc, hdcs );
550 if (!PATH_AssignGdiPath( &dc->path, &dcs->path ))
551 /* FIXME: This might not be quite right, since we're
552 * returning FALSE but still destroying the saved DC state */
555 GDI_ReleaseObj( hdcs );
556 GDI_ReleaseObj( hdc );
558 if (!(dc = DC_GetDCPtr( hdc ))) return FALSE;
560 GDI_ReleaseObj( hdc );
565 /***********************************************************************
566 * CreateDCA (GDI32.@)
568 HDC WINAPI CreateDCA( LPCSTR driver, LPCSTR device, LPCSTR output,
569 const DEVMODEA *initData )
573 const DC_FUNCTIONS *funcs;
578 if (!device || !DRIVER_GetDriverName( device, buf, sizeof(buf) ))
580 if (!driver) return 0;
584 if (!(funcs = DRIVER_load_driver( buf )))
586 ERR( "no driver found for %s\n", buf );
589 if (!(dc = DC_AllocDC( funcs )))
591 DRIVER_release_driver( funcs );
596 dc->hBitmap = GetStockObject( DEFAULT_BITMAP );
598 TRACE("(driver=%s, device=%s, output=%s): returning %p\n",
599 debugstr_a(driver), debugstr_a(device), debugstr_a(output), dc->hSelf );
601 if (dc->funcs->pCreateDC &&
602 !dc->funcs->pCreateDC( dc, &dc->physDev, buf, device, output, initData ))
604 WARN("creation aborted by device\n" );
605 GDI_FreeObject( dc->hSelf, dc );
606 DRIVER_release_driver( funcs );
610 dc->totalExtent.left = 0;
611 dc->totalExtent.top = 0;
612 dc->totalExtent.right = GetDeviceCaps( dc->hSelf, HORZRES );
613 dc->totalExtent.bottom = GetDeviceCaps( dc->hSelf, VERTRES );
614 dc->hVisRgn = CreateRectRgnIndirect( &dc->totalExtent );
618 GDI_ReleaseObj( hdc );
623 /***********************************************************************
624 * CreateDCW (GDI32.@)
626 HDC WINAPI CreateDCW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
627 const DEVMODEW *initData )
629 LPSTR driverA = HEAP_strdupWtoA( GetProcessHeap(), 0, driver );
630 LPSTR deviceA = HEAP_strdupWtoA( GetProcessHeap(), 0, device );
631 LPSTR outputA = HEAP_strdupWtoA( GetProcessHeap(), 0, output );
632 HDC res = CreateDCA( driverA, deviceA, outputA,
633 (const DEVMODEA *)initData /*FIXME*/ );
634 HeapFree( GetProcessHeap(), 0, driverA );
635 HeapFree( GetProcessHeap(), 0, deviceA );
636 HeapFree( GetProcessHeap(), 0, outputA );
641 /***********************************************************************
642 * CreateICA (GDI32.@)
644 HDC WINAPI CreateICA( LPCSTR driver, LPCSTR device, LPCSTR output,
645 const DEVMODEA* initData )
647 /* Nothing special yet for ICs */
648 return CreateDCA( driver, device, output, initData );
652 /***********************************************************************
653 * CreateICW (GDI32.@)
655 HDC WINAPI CreateICW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
656 const DEVMODEW* initData )
658 /* Nothing special yet for ICs */
659 return CreateDCW( driver, device, output, initData );
663 /***********************************************************************
664 * CreateCompatibleDC (GDI32.@)
666 HDC WINAPI CreateCompatibleDC( HDC hdc )
669 const DC_FUNCTIONS *funcs;
673 if ((origDC = GDI_GetObjPtr( hdc, DC_MAGIC )))
675 funcs = origDC->funcs;
676 GDI_ReleaseObj( hdc ); /* can't hold the lock while loading the driver */
677 funcs = DRIVER_get_driver( funcs );
679 else funcs = DRIVER_load_driver( "DISPLAY" );
681 if (!funcs) return 0;
683 if (!(dc = DC_AllocDC( funcs )))
685 DRIVER_release_driver( funcs );
689 TRACE("(%p): returning %p\n", hdc, dc->hSelf );
691 dc->flags = DC_MEMORY;
692 dc->bitsPerPixel = 1;
693 dc->hBitmap = GetStockObject( DEFAULT_BITMAP );
695 /* Copy the driver-specific physical device info into
696 * the new DC. The driver may use this read-only info
697 * while creating the compatible DC below. */
698 if ((origDC = GDI_GetObjPtr( hdc, DC_MAGIC ))) dc->physDev = origDC->physDev;
700 if (dc->funcs->pCreateDC &&
701 !dc->funcs->pCreateDC( dc, &dc->physDev, NULL, NULL, NULL, NULL ))
703 WARN("creation aborted by device\n");
704 GDI_FreeObject( dc->hSelf, dc );
705 if (origDC) GDI_ReleaseObj( hdc );
706 DRIVER_release_driver( funcs );
710 dc->totalExtent.left = 0;
711 dc->totalExtent.top = 0;
712 dc->totalExtent.right = 1; /* default bitmap is 1x1 */
713 dc->totalExtent.bottom = 1;
714 dc->hVisRgn = CreateRectRgnIndirect( &dc->totalExtent );
717 GDI_ReleaseObj( dc->hSelf );
718 if (origDC) GDI_ReleaseObj( hdc );
723 /***********************************************************************
726 BOOL WINAPI DeleteDC( HDC hdc )
728 const DC_FUNCTIONS *funcs = NULL;
735 if (!(dc = GDI_GetObjPtr( hdc, DC_MAGIC ))) return FALSE;
737 /* Call hook procedure to check whether is it OK to delete this DC */
738 if (dc->hookThunk && !(dc->flags & (DC_SAVED | DC_MEMORY)))
740 DCHOOKPROC proc = dc->hookThunk;
743 DWORD data = dc->dwHookData;
744 GDI_ReleaseObj( hdc );
745 if (!proc( HDC_16(hdc), DCHC_DELETEDC, data, 0 )) return FALSE;
746 if (!(dc = DC_GetDCPtr( hdc ))) return TRUE; /* deleted by the hook */
750 while (dc->saveLevel)
753 HDC hdcs = HDC_32(dc->header.hNext);
754 if (!(dcs = DC_GetDCPtr( hdcs ))) break;
755 dc->header.hNext = dcs->header.hNext;
757 if (dcs->hClipRgn) DeleteObject( dcs->hClipRgn );
758 if (dcs->hVisRgn) DeleteObject( dcs->hVisRgn );
759 if (dcs->hGCClipRgn) DeleteObject( dcs->hGCClipRgn );
760 PATH_DestroyGdiPath(&dcs->path);
761 GDI_FreeObject( hdcs, dcs );
764 if (!(dc->flags & DC_SAVED))
766 SelectObject( hdc, GetStockObject(BLACK_PEN) );
767 SelectObject( hdc, GetStockObject(WHITE_BRUSH) );
768 SelectObject( hdc, GetStockObject(SYSTEM_FONT) );
769 SelectObject( hdc, GetStockObject(DEFAULT_BITMAP) );
771 if (dc->funcs->pDeleteDC) dc->funcs->pDeleteDC(dc->physDev);
775 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
776 if (dc->hVisRgn) DeleteObject( dc->hVisRgn );
777 if (dc->hGCClipRgn) DeleteObject( dc->hGCClipRgn );
778 PATH_DestroyGdiPath(&dc->path);
780 GDI_FreeObject( hdc, dc );
781 if (funcs) DRIVER_release_driver( funcs ); /* do that after releasing the GDI lock */
786 /***********************************************************************
789 HDC WINAPI ResetDCA( HDC hdc, const DEVMODEA *devmode )
794 if ((dc = DC_GetDCPtr( hdc )))
796 if (dc->funcs->pResetDC) ret = dc->funcs->pResetDC( dc->physDev, devmode );
797 GDI_ReleaseObj( hdc );
803 /***********************************************************************
806 HDC WINAPI ResetDCW( HDC hdc, const DEVMODEW *devmode )
808 return ResetDCA(hdc, (const DEVMODEA*)devmode); /* FIXME */
812 /***********************************************************************
813 * GetDeviceCaps (GDI32.@)
815 INT WINAPI GetDeviceCaps( HDC hdc, INT cap )
820 if ((dc = DC_GetDCPtr( hdc )))
822 if (dc->funcs->pGetDeviceCaps) ret = dc->funcs->pGetDeviceCaps( dc->physDev, cap );
823 GDI_ReleaseObj( hdc );
829 /***********************************************************************
830 * SetBkColor (GDI32.@)
832 COLORREF WINAPI SetBkColor( HDC hdc, COLORREF color )
835 DC * dc = DC_GetDCPtr( hdc );
837 if (!dc) return CLR_INVALID;
838 oldColor = dc->backgroundColor;
839 if (dc->funcs->pSetBkColor)
841 color = dc->funcs->pSetBkColor(dc->physDev, color);
842 if (color == CLR_INVALID) /* don't change it */
845 oldColor = CLR_INVALID;
848 dc->backgroundColor = color;
849 GDI_ReleaseObj( hdc );
854 /***********************************************************************
855 * SetTextColor (GDI32.@)
857 COLORREF WINAPI SetTextColor( HDC hdc, COLORREF color )
860 DC * dc = DC_GetDCPtr( hdc );
862 if (!dc) return CLR_INVALID;
863 oldColor = dc->textColor;
864 if (dc->funcs->pSetTextColor)
866 color = dc->funcs->pSetTextColor(dc->physDev, color);
867 if (color == CLR_INVALID) /* don't change it */
870 oldColor = CLR_INVALID;
873 dc->textColor = color;
874 GDI_ReleaseObj( hdc );
879 /***********************************************************************
880 * SetTextAlign (GDI32.@)
882 UINT WINAPI SetTextAlign( HDC hdc, UINT align )
885 DC *dc = DC_GetDCPtr( hdc );
887 if (dc->funcs->pSetTextAlign)
888 prevAlign = dc->funcs->pSetTextAlign(dc->physDev, align);
890 prevAlign = dc->textAlign;
891 dc->textAlign = align;
893 GDI_ReleaseObj( hdc );
897 /***********************************************************************
898 * GetDCOrgEx (GDI32.@)
900 BOOL WINAPI GetDCOrgEx( HDC hDC, LPPOINT lpp )
904 if (!lpp) return FALSE;
905 if (!(dc = DC_GetDCPtr( hDC ))) return FALSE;
908 if (dc->funcs->pGetDCOrgEx) dc->funcs->pGetDCOrgEx( dc->physDev, lpp );
909 GDI_ReleaseObj( hDC );
914 /***********************************************************************
917 DWORD WINAPI SetDCOrg16( HDC16 hdc16, INT16 x, INT16 y )
920 HDC hdc = HDC_32( hdc16 );
921 DC *dc = DC_GetDCPtr( hdc );
923 if (dc->funcs->pSetDCOrg) prevOrg = dc->funcs->pSetDCOrg( dc->physDev, x, y );
924 GDI_ReleaseObj( hdc );
929 /***********************************************************************
930 * SetGraphicsMode (GDI32.@)
932 INT WINAPI SetGraphicsMode( HDC hdc, INT mode )
935 DC *dc = DC_GetDCPtr( hdc );
937 /* One would think that setting the graphics mode to GM_COMPATIBLE
938 * would also reset the world transformation matrix to the unity
939 * matrix. However, in Windows, this is not the case. This doesn't
940 * make a lot of sense to me, but that's the way it is.
943 if ((mode > 0) && (mode <= GM_LAST))
945 ret = dc->GraphicsMode;
946 dc->GraphicsMode = mode;
948 GDI_ReleaseObj( hdc );
953 /***********************************************************************
954 * SetArcDirection (GDI32.@)
956 INT WINAPI SetArcDirection( HDC hdc, INT nDirection )
959 INT nOldDirection = 0;
961 if (nDirection!=AD_COUNTERCLOCKWISE && nDirection!=AD_CLOCKWISE)
963 SetLastError(ERROR_INVALID_PARAMETER);
967 if ((dc = DC_GetDCPtr( hdc )))
969 nOldDirection = dc->ArcDirection;
970 dc->ArcDirection = nDirection;
971 GDI_ReleaseObj( hdc );
973 return nOldDirection;
977 /***********************************************************************
978 * GetWorldTransform (GDI32.@)
980 BOOL WINAPI GetWorldTransform( HDC hdc, LPXFORM xform )
983 if (!xform) return FALSE;
984 if (!(dc = DC_GetDCPtr( hdc ))) return FALSE;
985 *xform = dc->xformWorld2Wnd;
986 GDI_ReleaseObj( hdc );
991 /***********************************************************************
992 * SetWorldTransform (GDI32.@)
994 BOOL WINAPI SetWorldTransform( HDC hdc, const XFORM *xform )
997 DC *dc = DC_GetDCPtr( hdc );
999 if (!dc) return FALSE;
1000 if (!xform) goto done;
1002 /* Check that graphics mode is GM_ADVANCED */
1003 if (dc->GraphicsMode!=GM_ADVANCED) goto done;
1005 dc->xformWorld2Wnd = *xform;
1006 DC_UpdateXforms( dc );
1009 GDI_ReleaseObj( hdc );
1014 /****************************************************************************
1015 * ModifyWorldTransform [GDI32.@]
1016 * Modifies the world transformation for a device context.
1019 * hdc [I] Handle to device context
1020 * xform [I] XFORM structure that will be used to modify the world
1022 * iMode [I] Specifies in what way to modify the world transformation
1025 * Resets the world transformation to the identity matrix.
1026 * The parameter xform is ignored.
1028 * Multiplies xform into the world transformation matrix from
1031 * Multiplies xform into the world transformation matrix from
1036 BOOL WINAPI ModifyWorldTransform( HDC hdc, const XFORM *xform,
1040 DC *dc = DC_GetDCPtr( hdc );
1042 /* Check for illegal parameters */
1043 if (!dc) return FALSE;
1044 if (!xform) goto done;
1046 /* Check that graphics mode is GM_ADVANCED */
1047 if (dc->GraphicsMode!=GM_ADVANCED) goto done;
1052 dc->xformWorld2Wnd.eM11 = 1.0f;
1053 dc->xformWorld2Wnd.eM12 = 0.0f;
1054 dc->xformWorld2Wnd.eM21 = 0.0f;
1055 dc->xformWorld2Wnd.eM22 = 1.0f;
1056 dc->xformWorld2Wnd.eDx = 0.0f;
1057 dc->xformWorld2Wnd.eDy = 0.0f;
1059 case MWT_LEFTMULTIPLY:
1060 CombineTransform( &dc->xformWorld2Wnd, xform,
1061 &dc->xformWorld2Wnd );
1063 case MWT_RIGHTMULTIPLY:
1064 CombineTransform( &dc->xformWorld2Wnd, &dc->xformWorld2Wnd,
1071 DC_UpdateXforms( dc );
1074 GDI_ReleaseObj( hdc );
1079 /****************************************************************************
1080 * CombineTransform [GDI32.@]
1081 * Combines two transformation matrices.
1084 * xformResult [O] Stores the result of combining the two matrices
1085 * xform1 [I] Specifies the first matrix to apply
1086 * xform2 [I] Specifies the second matrix to apply
1089 * The same matrix can be passed in for more than one of the parameters.
1093 BOOL WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
1094 const XFORM *xform2 )
1098 /* Check for illegal parameters */
1099 if (!xformResult || !xform1 || !xform2)
1102 /* Create the result in a temporary XFORM, since xformResult may be
1103 * equal to xform1 or xform2 */
1104 xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
1105 xform1->eM12 * xform2->eM21;
1106 xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
1107 xform1->eM12 * xform2->eM22;
1108 xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
1109 xform1->eM22 * xform2->eM21;
1110 xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
1111 xform1->eM22 * xform2->eM22;
1112 xformTemp.eDx = xform1->eDx * xform2->eM11 +
1113 xform1->eDy * xform2->eM21 +
1115 xformTemp.eDy = xform1->eDx * xform2->eM12 +
1116 xform1->eDy * xform2->eM22 +
1119 /* Copy the result to xformResult */
1120 *xformResult = xformTemp;
1126 /***********************************************************************
1127 * SetDCHook (GDI32.@)
1129 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1131 BOOL WINAPI SetDCHook( HDC hdc, DCHOOKPROC hookProc, DWORD dwHookData )
1133 DC *dc = DC_GetDCPtr( hdc );
1135 if (!dc) return FALSE;
1136 dc->dwHookData = dwHookData;
1137 dc->hookThunk = hookProc;
1138 GDI_ReleaseObj( hdc );
1143 /* relay function to call the 16-bit DC hook proc */
1144 static BOOL16 WINAPI call_dc_hook16( HDC16 hdc16, WORD code, DWORD data, LPARAM lParam )
1148 FARPROC16 proc = NULL;
1149 HDC hdc = HDC_32( hdc16 );
1150 DC *dc = DC_GetDCPtr( hdc );
1152 if (!dc) return FALSE;
1153 proc = dc->hookProc;
1154 GDI_ReleaseObj( hdc );
1155 if (!proc) return FALSE;
1158 args[3] = HIWORD(data);
1159 args[2] = LOWORD(data);
1160 args[1] = HIWORD(lParam);
1161 args[0] = LOWORD(lParam);
1162 WOWCallback16Ex( (DWORD)proc, WCB16_PASCAL, sizeof(args), args, &ret );
1166 /***********************************************************************
1167 * SetDCHook (GDI.190)
1169 BOOL16 WINAPI SetDCHook16( HDC16 hdc16, FARPROC16 hookProc, DWORD dwHookData )
1171 HDC hdc = HDC_32( hdc16 );
1172 DC *dc = DC_GetDCPtr( hdc );
1173 if (!dc) return FALSE;
1175 dc->hookProc = hookProc;
1176 GDI_ReleaseObj( hdc );
1177 return SetDCHook( hdc, call_dc_hook16, dwHookData );
1181 /***********************************************************************
1182 * GetDCHook (GDI.191)
1184 DWORD WINAPI GetDCHook16( HDC16 hdc16, FARPROC16 *phookProc )
1186 HDC hdc = HDC_32( hdc16 );
1187 DC *dc = DC_GetDCPtr( hdc );
1191 *phookProc = dc->hookProc;
1192 ret = dc->dwHookData;
1193 GDI_ReleaseObj( hdc );
1198 /***********************************************************************
1199 * SetHookFlags (GDI.192)
1201 WORD WINAPI SetHookFlags16(HDC16 hdc16, WORD flags)
1203 HDC hdc = HDC_32( hdc16 );
1204 DC *dc = DC_GetDCPtr( hdc );
1208 WORD wRet = dc->flags & DC_DIRTY;
1210 /* "Undocumented Windows" info is slightly confusing.
1213 TRACE("hDC %p, flags %04x\n",hdc,flags);
1215 if( flags & DCHF_INVALIDATEVISRGN )
1216 dc->flags |= DC_DIRTY;
1217 else if( flags & DCHF_VALIDATEVISRGN || !flags )
1218 dc->flags &= ~DC_DIRTY;
1219 GDI_ReleaseObj( hdc );
1225 /***********************************************************************
1226 * SetICMMode (GDI32.@)
1228 INT WINAPI SetICMMode(HDC hdc, INT iEnableICM)
1230 /*FIXME Asuming that ICM is always off, and cannot be turned on */
1231 if (iEnableICM == ICM_OFF) return ICM_OFF;
1232 if (iEnableICM == ICM_ON) return 0;
1233 if (iEnableICM == ICM_QUERY) return ICM_OFF;
1237 /***********************************************************************
1238 * GetDeviceGammaRamp (GDI32.@)
1240 BOOL WINAPI GetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1243 DC *dc = DC_GetDCPtr( hDC );
1247 if (dc->funcs->pGetDeviceGammaRamp)
1248 ret = dc->funcs->pGetDeviceGammaRamp(dc->physDev, ptr);
1249 GDI_ReleaseObj( hDC );
1254 /***********************************************************************
1255 * SetDeviceGammaRamp (GDI32.@)
1257 BOOL WINAPI SetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1260 DC *dc = DC_GetDCPtr( hDC );
1264 if (dc->funcs->pSetDeviceGammaRamp)
1265 ret = dc->funcs->pSetDeviceGammaRamp(dc->physDev, ptr);
1266 GDI_ReleaseObj( hDC );
1271 /***********************************************************************
1272 * GetColorSpace (GDI32.@)
1274 HCOLORSPACE WINAPI GetColorSpace(HDC hdc)
1276 /*FIXME Need to to whatever GetColorSpace actually does */
1280 /***********************************************************************
1281 * CreateColorSpaceA (GDI32.@)
1283 HCOLORSPACE WINAPI CreateColorSpaceA( LPLOGCOLORSPACEA lpLogColorSpace )
1289 /***********************************************************************
1290 * CreateColorSpaceW (GDI32.@)
1292 HCOLORSPACE WINAPI CreateColorSpaceW( LPLOGCOLORSPACEW lpLogColorSpace )
1298 /***********************************************************************
1299 * DeleteColorSpace (GDI32.@)
1301 BOOL WINAPI DeleteColorSpace( HCOLORSPACE hColorSpace )
1308 /***********************************************************************
1309 * SetColorSpace (GDI32.@)
1311 HCOLORSPACE WINAPI SetColorSpace( HDC hDC, HCOLORSPACE hColorSpace )
1318 /***********************************************************************
1319 * GetBoundsRect (GDI.194)
1321 UINT16 WINAPI GetBoundsRect16(HDC16 hdc, LPRECT16 rect, UINT16 flags)
1323 return DCB_RESET | DCB_DISABLE; /* bounding rectangle always empty and disabled*/
1326 /***********************************************************************
1327 * GetBoundsRect (GDI32.@)
1329 UINT WINAPI GetBoundsRect(HDC hdc, LPRECT rect, UINT flags)
1331 FIXME("(): stub\n");
1332 return DCB_RESET; /* bounding rectangle always empty */
1335 /***********************************************************************
1336 * SetBoundsRect (GDI.193)
1338 UINT16 WINAPI SetBoundsRect16(HDC16 hdc, const RECT16* rect, UINT16 flags)
1340 if ( (flags & DCB_ACCUMULATE) || (flags & DCB_ENABLE) )
1341 FIXME("(%04x, %p, %04x): stub\n", hdc, rect, flags );
1343 return DCB_RESET | DCB_DISABLE; /* bounding rectangle always empty and disabled*/
1346 /***********************************************************************
1347 * SetBoundsRect (GDI32.@)
1349 UINT WINAPI SetBoundsRect(HDC hdc, const RECT* rect, UINT flags)
1351 FIXME("(): stub\n");
1352 return DCB_DISABLE; /* bounding rectangle always empty */
1356 /***********************************************************************
1357 * GetRelAbs (GDI32.@)
1359 INT WINAPI GetRelAbs( HDC hdc, DWORD dwIgnore )
1362 DC *dc = DC_GetDCPtr( hdc );
1363 if (dc) ret = dc->relAbsMode;
1364 GDI_ReleaseObj( hdc );
1368 /***********************************************************************
1369 * GetLayout (GDI32.@)
1371 * Gets left->right or right->left text layout flags of a dc.
1372 * win98 just returns 0 and sets ERROR_CALL_NOT_IMPLEMENTED so we do the same
1375 DWORD WINAPI GetLayout(HDC hdc)
1377 FIXME("(%p): stub\n", hdc);
1378 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1382 /***********************************************************************
1383 * SetLayout (GDI32.@)
1385 * Sets left->right or right->left text layout flags of a dc.
1386 * win98 just returns 0 and sets ERROR_CALL_NOT_IMPLEMENTED so we do the same
1389 DWORD WINAPI SetLayout(HDC hdc, DWORD layout)
1391 FIXME("(%p,%08lx): stub\n", hdc, layout);
1392 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1396 /***********************************************************************
1397 * SetDCBrushColor (GDI32.@)
1399 * Sets the current device context (DC) brush color to the specified
1400 * color value. If the device cannot represent the specified color
1401 * value, the color is set to the nearest physical color.
1404 COLORREF WINAPI SetDCBrushColor(HDC hdc, COLORREF crColor)
1406 FIXME("(%p, %08lx): stub\n", hdc, crColor);
1407 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);