mshtml: Switch two checks (Coverity).
[wine/multimedia.git] / dlls / gdi32 / dc.c
blobb357cee2bd6314388f5b10b366b05874322e8db8
1 /*
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <assert.h>
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winreg.h"
31 #include "winternl.h"
32 #include "winerror.h"
33 #include "gdi_private.h"
34 #include "wine/unicode.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(dc);
39 static const WCHAR displayW[] = { 'd','i','s','p','l','a','y',0 };
41 static BOOL DC_DeleteObject( HGDIOBJ handle );
43 static const struct gdi_obj_funcs dc_funcs =
45 NULL, /* pSelectObject */
46 NULL, /* pGetObjectA */
47 NULL, /* pGetObjectW */
48 NULL, /* pUnrealizeObject */
49 DC_DeleteObject /* pDeleteObject */
53 static inline DC *get_dc_obj( HDC hdc )
55 DC *dc = GDI_GetObjPtr( hdc, 0 );
56 if (!dc) return NULL;
58 if ((dc->header.type != OBJ_DC) &&
59 (dc->header.type != OBJ_MEMDC) &&
60 (dc->header.type != OBJ_METADC) &&
61 (dc->header.type != OBJ_ENHMETADC))
63 GDI_ReleaseObj( hdc );
64 SetLastError( ERROR_INVALID_HANDLE );
65 dc = NULL;
67 return dc;
71 /***********************************************************************
72 * alloc_dc_ptr
74 DC *alloc_dc_ptr( WORD magic )
76 DC *dc;
78 if (!(dc = HeapAlloc( GetProcessHeap(), 0, sizeof(*dc) ))) return NULL;
80 dc->nulldrv.funcs = &null_driver;
81 dc->nulldrv.next = NULL;
82 dc->dibdrv = NULL;
83 dc->physDev = &dc->nulldrv;
84 dc->thread = GetCurrentThreadId();
85 dc->refcount = 1;
86 dc->dirty = 0;
87 dc->saveLevel = 0;
88 dc->saved_dc = 0;
89 dc->dwHookData = 0;
90 dc->hookProc = NULL;
91 dc->wndOrgX = 0;
92 dc->wndOrgY = 0;
93 dc->wndExtX = 1;
94 dc->wndExtY = 1;
95 dc->vportOrgX = 0;
96 dc->vportOrgY = 0;
97 dc->vportExtX = 1;
98 dc->vportExtY = 1;
99 dc->miterLimit = 10.0f; /* 10.0 is the default, from MSDN */
100 dc->flags = 0;
101 dc->layout = 0;
102 dc->hClipRgn = 0;
103 dc->hMetaRgn = 0;
104 dc->hMetaClipRgn = 0;
105 dc->hVisRgn = 0;
106 dc->hPen = GDI_inc_ref_count( GetStockObject( BLACK_PEN ));
107 dc->hBrush = GDI_inc_ref_count( GetStockObject( WHITE_BRUSH ));
108 dc->hFont = GDI_inc_ref_count( GetStockObject( SYSTEM_FONT ));
109 dc->hBitmap = 0;
110 dc->hDevice = 0;
111 dc->hPalette = GetStockObject( DEFAULT_PALETTE );
112 dc->gdiFont = 0;
113 dc->path = NULL;
114 dc->font_code_page = CP_ACP;
115 dc->ROPmode = R2_COPYPEN;
116 dc->polyFillMode = ALTERNATE;
117 dc->stretchBltMode = BLACKONWHITE;
118 dc->relAbsMode = ABSOLUTE;
119 dc->backgroundMode = OPAQUE;
120 dc->backgroundColor = RGB( 255, 255, 255 );
121 dc->dcBrushColor = RGB( 255, 255, 255 );
122 dc->dcPenColor = RGB( 0, 0, 0 );
123 dc->textColor = RGB( 0, 0, 0 );
124 dc->brushOrgX = 0;
125 dc->brushOrgY = 0;
126 dc->mapperFlags = 0;
127 dc->textAlign = TA_LEFT | TA_TOP | TA_NOUPDATECP;
128 dc->charExtra = 0;
129 dc->breakExtra = 0;
130 dc->breakRem = 0;
131 dc->MapMode = MM_TEXT;
132 dc->GraphicsMode = GM_COMPATIBLE;
133 dc->pAbortProc = NULL;
134 dc->CursPosX = 0;
135 dc->CursPosY = 0;
136 dc->ArcDirection = AD_COUNTERCLOCKWISE;
137 dc->xformWorld2Wnd.eM11 = 1.0f;
138 dc->xformWorld2Wnd.eM12 = 0.0f;
139 dc->xformWorld2Wnd.eM21 = 0.0f;
140 dc->xformWorld2Wnd.eM22 = 1.0f;
141 dc->xformWorld2Wnd.eDx = 0.0f;
142 dc->xformWorld2Wnd.eDy = 0.0f;
143 dc->xformWorld2Vport = dc->xformWorld2Wnd;
144 dc->xformVport2World = dc->xformWorld2Wnd;
145 dc->vport2WorldValid = TRUE;
146 dc->BoundsRect.left = 0;
147 dc->BoundsRect.top = 0;
148 dc->BoundsRect.right = 0;
149 dc->BoundsRect.bottom = 0;
151 if (!(dc->hSelf = alloc_gdi_handle( &dc->header, magic, &dc_funcs )))
153 HeapFree( GetProcessHeap(), 0, dc );
154 return NULL;
156 dc->nulldrv.hdc = dc->hSelf;
158 if (font_driver && !font_driver->pCreateDC( &dc->physDev, NULL, NULL, NULL, NULL ))
160 free_dc_ptr( dc );
161 return NULL;
163 return dc;
168 /***********************************************************************
169 * free_dc_state
171 static void free_dc_state( DC *dc )
173 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
174 if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
175 if (dc->hMetaClipRgn) DeleteObject( dc->hMetaClipRgn );
176 if (dc->hVisRgn) DeleteObject( dc->hVisRgn );
177 if (dc->path) free_gdi_path( dc->path );
178 HeapFree( GetProcessHeap(), 0, dc );
182 /***********************************************************************
183 * free_dc_ptr
185 void free_dc_ptr( DC *dc )
187 assert( dc->refcount == 1 );
189 while (dc->physDev != &dc->nulldrv)
191 PHYSDEV physdev = pop_dc_driver( &dc->physDev );
192 physdev->funcs->pDeleteDC( physdev );
193 if (physdev == dc->dibdrv) dc->dibdrv = NULL;
195 if (dc->dibdrv) dc->dibdrv->funcs->pDeleteDC( dc->dibdrv );
196 free_gdi_handle( dc->hSelf );
197 free_dc_state( dc );
201 /***********************************************************************
202 * get_dc_ptr
204 * Retrieve a DC pointer but release the GDI lock.
206 DC *get_dc_ptr( HDC hdc )
208 DC *dc = get_dc_obj( hdc );
209 if (!dc) return NULL;
211 if (!InterlockedCompareExchange( &dc->refcount, 1, 0 ))
213 dc->thread = GetCurrentThreadId();
215 else if (dc->thread != GetCurrentThreadId())
217 WARN( "dc %p belongs to thread %04x\n", hdc, dc->thread );
218 GDI_ReleaseObj( hdc );
219 return NULL;
221 else InterlockedIncrement( &dc->refcount );
223 GDI_ReleaseObj( hdc );
224 return dc;
228 /***********************************************************************
229 * release_dc_ptr
231 void release_dc_ptr( DC *dc )
233 LONG ref;
235 dc->thread = 0;
236 ref = InterlockedDecrement( &dc->refcount );
237 assert( ref >= 0 );
238 if (ref) dc->thread = GetCurrentThreadId(); /* we still own it */
242 /***********************************************************************
243 * update_dc
245 * Make sure the DC vis region is up to date.
246 * This function may call up to USER so the GDI lock should _not_
247 * be held when calling it.
249 void update_dc( DC *dc )
251 if (InterlockedExchange( &dc->dirty, 0 ) && dc->hookProc)
252 dc->hookProc( dc->hSelf, DCHC_INVALIDVISRGN, dc->dwHookData, 0 );
256 /***********************************************************************
257 * DC_DeleteObject
259 static BOOL DC_DeleteObject( HGDIOBJ handle )
261 return DeleteDC( handle );
265 /***********************************************************************
266 * DC_InitDC
268 * Setup device-specific DC values for a newly created DC.
270 void DC_InitDC( DC* dc )
272 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pRealizeDefaultPalette );
273 physdev->funcs->pRealizeDefaultPalette( physdev );
274 SetTextColor( dc->hSelf, dc->textColor );
275 SetBkColor( dc->hSelf, dc->backgroundColor );
276 SelectObject( dc->hSelf, dc->hPen );
277 SelectObject( dc->hSelf, dc->hBrush );
278 SelectObject( dc->hSelf, dc->hFont );
279 CLIPPING_UpdateGCRegion( dc );
280 SetVirtualResolution( dc->hSelf, 0, 0, 0, 0 );
284 /***********************************************************************
285 * DC_InvertXform
287 * Computes the inverse of the transformation xformSrc and stores it to
288 * xformDest. Returns TRUE if successful or FALSE if the xformSrc matrix
289 * is singular.
291 static BOOL DC_InvertXform( const XFORM *xformSrc, XFORM *xformDest )
293 double determinant;
295 determinant = xformSrc->eM11*xformSrc->eM22 -
296 xformSrc->eM12*xformSrc->eM21;
297 if (determinant > -1e-12 && determinant < 1e-12)
298 return FALSE;
300 xformDest->eM11 = xformSrc->eM22 / determinant;
301 xformDest->eM12 = -xformSrc->eM12 / determinant;
302 xformDest->eM21 = -xformSrc->eM21 / determinant;
303 xformDest->eM22 = xformSrc->eM11 / determinant;
304 xformDest->eDx = -xformSrc->eDx * xformDest->eM11 -
305 xformSrc->eDy * xformDest->eM21;
306 xformDest->eDy = -xformSrc->eDx * xformDest->eM12 -
307 xformSrc->eDy * xformDest->eM22;
309 return TRUE;
312 /* Construct a transformation to do the window-to-viewport conversion */
313 static void construct_window_to_viewport(DC *dc, XFORM *xform)
315 double scaleX, scaleY;
316 scaleX = (double)dc->vportExtX / (double)dc->wndExtX;
317 scaleY = (double)dc->vportExtY / (double)dc->wndExtY;
319 if (dc->layout & LAYOUT_RTL) scaleX = -scaleX;
320 xform->eM11 = scaleX;
321 xform->eM12 = 0.0;
322 xform->eM21 = 0.0;
323 xform->eM22 = scaleY;
324 xform->eDx = (double)dc->vportOrgX - scaleX * (double)dc->wndOrgX;
325 xform->eDy = (double)dc->vportOrgY - scaleY * (double)dc->wndOrgY;
326 if (dc->layout & LAYOUT_RTL) xform->eDx = dc->vis_rect.right - dc->vis_rect.left - 1 - xform->eDx;
329 /***********************************************************************
330 * DC_UpdateXforms
332 * Updates the xformWorld2Vport, xformVport2World and vport2WorldValid
333 * fields of the specified DC by creating a transformation that
334 * represents the current mapping mode and combining it with the DC's
335 * world transform. This function should be called whenever the
336 * parameters associated with the mapping mode (window and viewport
337 * extents and origins) or the world transform change.
339 void DC_UpdateXforms( DC *dc )
341 XFORM xformWnd2Vport, oldworld2vport;
343 construct_window_to_viewport(dc, &xformWnd2Vport);
345 oldworld2vport = dc->xformWorld2Vport;
346 /* Combine with the world transformation */
347 CombineTransform( &dc->xformWorld2Vport, &dc->xformWorld2Wnd,
348 &xformWnd2Vport );
350 /* Create inverse of world-to-viewport transformation */
351 dc->vport2WorldValid = DC_InvertXform( &dc->xformWorld2Vport,
352 &dc->xformVport2World );
354 /* Reselect the font and pen back into the dc so that the size
355 gets updated. */
356 if (memcmp(&oldworld2vport, &dc->xformWorld2Vport, sizeof(oldworld2vport)) &&
357 !GdiIsMetaFileDC(dc->hSelf))
359 SelectObject(dc->hSelf, GetCurrentObject(dc->hSelf, OBJ_FONT));
360 SelectObject(dc->hSelf, GetCurrentObject(dc->hSelf, OBJ_PEN));
365 /***********************************************************************
366 * nulldrv_SaveDC
368 INT nulldrv_SaveDC( PHYSDEV dev )
370 DC *newdc, *dc = get_nulldrv_dc( dev );
372 if (!(newdc = HeapAlloc( GetProcessHeap(), 0, sizeof(*newdc )))) return 0;
373 newdc->flags = dc->flags;
374 newdc->layout = dc->layout;
375 newdc->hPen = dc->hPen;
376 newdc->hBrush = dc->hBrush;
377 newdc->hFont = dc->hFont;
378 newdc->hBitmap = dc->hBitmap;
379 newdc->hDevice = dc->hDevice;
380 newdc->hPalette = dc->hPalette;
381 newdc->ROPmode = dc->ROPmode;
382 newdc->polyFillMode = dc->polyFillMode;
383 newdc->stretchBltMode = dc->stretchBltMode;
384 newdc->relAbsMode = dc->relAbsMode;
385 newdc->backgroundMode = dc->backgroundMode;
386 newdc->backgroundColor = dc->backgroundColor;
387 newdc->textColor = dc->textColor;
388 newdc->dcBrushColor = dc->dcBrushColor;
389 newdc->dcPenColor = dc->dcPenColor;
390 newdc->brushOrgX = dc->brushOrgX;
391 newdc->brushOrgY = dc->brushOrgY;
392 newdc->mapperFlags = dc->mapperFlags;
393 newdc->textAlign = dc->textAlign;
394 newdc->charExtra = dc->charExtra;
395 newdc->breakExtra = dc->breakExtra;
396 newdc->breakRem = dc->breakRem;
397 newdc->MapMode = dc->MapMode;
398 newdc->GraphicsMode = dc->GraphicsMode;
399 newdc->CursPosX = dc->CursPosX;
400 newdc->CursPosY = dc->CursPosY;
401 newdc->ArcDirection = dc->ArcDirection;
402 newdc->xformWorld2Wnd = dc->xformWorld2Wnd;
403 newdc->xformWorld2Vport = dc->xformWorld2Vport;
404 newdc->xformVport2World = dc->xformVport2World;
405 newdc->vport2WorldValid = dc->vport2WorldValid;
406 newdc->wndOrgX = dc->wndOrgX;
407 newdc->wndOrgY = dc->wndOrgY;
408 newdc->wndExtX = dc->wndExtX;
409 newdc->wndExtY = dc->wndExtY;
410 newdc->vportOrgX = dc->vportOrgX;
411 newdc->vportOrgY = dc->vportOrgY;
412 newdc->vportExtX = dc->vportExtX;
413 newdc->vportExtY = dc->vportExtY;
414 newdc->virtual_res = dc->virtual_res;
415 newdc->virtual_size = dc->virtual_size;
416 newdc->BoundsRect = dc->BoundsRect;
417 newdc->gdiFont = dc->gdiFont;
419 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
421 newdc->hVisRgn = 0;
422 newdc->hClipRgn = 0;
423 newdc->hMetaRgn = 0;
424 newdc->hMetaClipRgn = 0;
425 if (dc->hClipRgn)
427 newdc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
428 CombineRgn( newdc->hClipRgn, dc->hClipRgn, 0, RGN_COPY );
430 if (dc->hMetaRgn)
432 newdc->hMetaRgn = CreateRectRgn( 0, 0, 0, 0 );
433 CombineRgn( newdc->hMetaRgn, dc->hMetaRgn, 0, RGN_COPY );
436 /* don't bother recomputing hMetaClipRgn, we'll do that in SetDCState */
438 if (!PATH_SavePath( newdc, dc ))
440 release_dc_ptr( dc );
441 free_dc_state( newdc );
442 return 0;
445 newdc->saved_dc = dc->saved_dc;
446 dc->saved_dc = newdc;
447 return ++dc->saveLevel;
451 /***********************************************************************
452 * restore_dc_state
454 BOOL nulldrv_RestoreDC( PHYSDEV dev, INT level )
456 DC *dcs, *first_dcs, *dc = get_nulldrv_dc( dev );
457 INT save_level;
459 /* find the state level to restore */
461 if (abs(level) > dc->saveLevel || level == 0) return FALSE;
462 if (level < 0) level = dc->saveLevel + level + 1;
463 first_dcs = dc->saved_dc;
464 for (dcs = first_dcs, save_level = dc->saveLevel; save_level > level; save_level--)
465 dcs = dcs->saved_dc;
467 /* restore the state */
469 if (!PATH_RestorePath( dc, dcs )) return FALSE;
471 dc->flags = dcs->flags;
472 dc->layout = dcs->layout;
473 dc->hDevice = dcs->hDevice;
474 dc->ROPmode = dcs->ROPmode;
475 dc->polyFillMode = dcs->polyFillMode;
476 dc->stretchBltMode = dcs->stretchBltMode;
477 dc->relAbsMode = dcs->relAbsMode;
478 dc->backgroundMode = dcs->backgroundMode;
479 dc->backgroundColor = dcs->backgroundColor;
480 dc->textColor = dcs->textColor;
481 dc->dcBrushColor = dcs->dcBrushColor;
482 dc->dcPenColor = dcs->dcPenColor;
483 dc->brushOrgX = dcs->brushOrgX;
484 dc->brushOrgY = dcs->brushOrgY;
485 dc->mapperFlags = dcs->mapperFlags;
486 dc->textAlign = dcs->textAlign;
487 dc->charExtra = dcs->charExtra;
488 dc->breakExtra = dcs->breakExtra;
489 dc->breakRem = dcs->breakRem;
490 dc->MapMode = dcs->MapMode;
491 dc->GraphicsMode = dcs->GraphicsMode;
492 dc->CursPosX = dcs->CursPosX;
493 dc->CursPosY = dcs->CursPosY;
494 dc->ArcDirection = dcs->ArcDirection;
495 dc->xformWorld2Wnd = dcs->xformWorld2Wnd;
496 dc->xformWorld2Vport = dcs->xformWorld2Vport;
497 dc->xformVport2World = dcs->xformVport2World;
498 dc->vport2WorldValid = dcs->vport2WorldValid;
499 dc->BoundsRect = dcs->BoundsRect;
501 dc->wndOrgX = dcs->wndOrgX;
502 dc->wndOrgY = dcs->wndOrgY;
503 dc->wndExtX = dcs->wndExtX;
504 dc->wndExtY = dcs->wndExtY;
505 dc->vportOrgX = dcs->vportOrgX;
506 dc->vportOrgY = dcs->vportOrgY;
507 dc->vportExtX = dcs->vportExtX;
508 dc->vportExtY = dcs->vportExtY;
509 dc->virtual_res = dcs->virtual_res;
510 dc->virtual_size = dcs->virtual_size;
512 if (dcs->hClipRgn)
514 if (!dc->hClipRgn) dc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
515 CombineRgn( dc->hClipRgn, dcs->hClipRgn, 0, RGN_COPY );
517 else
519 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
520 dc->hClipRgn = 0;
522 if (dcs->hMetaRgn)
524 if (!dc->hMetaRgn) dc->hMetaRgn = CreateRectRgn( 0, 0, 0, 0 );
525 CombineRgn( dc->hMetaRgn, dcs->hMetaRgn, 0, RGN_COPY );
527 else
529 if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
530 dc->hMetaRgn = 0;
532 DC_UpdateXforms( dc );
533 CLIPPING_UpdateGCRegion( dc );
535 SelectObject( dev->hdc, dcs->hBitmap );
536 SelectObject( dev->hdc, dcs->hBrush );
537 SelectObject( dev->hdc, dcs->hFont );
538 SelectObject( dev->hdc, dcs->hPen );
539 SetBkColor( dev->hdc, dcs->backgroundColor);
540 SetTextColor( dev->hdc, dcs->textColor);
541 GDISelectPalette( dev->hdc, dcs->hPalette, FALSE );
543 dc->saved_dc = dcs->saved_dc;
544 dcs->saved_dc = 0;
545 dc->saveLevel = save_level - 1;
547 /* now destroy all the saved DCs */
549 while (first_dcs)
551 DC *next = first_dcs->saved_dc;
552 free_dc_state( first_dcs );
553 first_dcs = next;
555 return TRUE;
559 /***********************************************************************
560 * SaveDC (GDI32.@)
562 INT WINAPI SaveDC( HDC hdc )
564 DC * dc;
565 INT ret = 0;
567 if ((dc = get_dc_ptr( hdc )))
569 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSaveDC );
570 ret = physdev->funcs->pSaveDC( physdev );
571 release_dc_ptr( dc );
573 return ret;
577 /***********************************************************************
578 * RestoreDC (GDI32.@)
580 BOOL WINAPI RestoreDC( HDC hdc, INT level )
582 DC *dc;
583 BOOL success = FALSE;
585 TRACE("%p %d\n", hdc, level );
586 if ((dc = get_dc_ptr( hdc )))
588 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pRestoreDC );
589 update_dc( dc );
590 success = physdev->funcs->pRestoreDC( physdev, level );
591 release_dc_ptr( dc );
593 return success;
597 /***********************************************************************
598 * CreateDCW (GDI32.@)
600 HDC WINAPI CreateDCW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
601 const DEVMODEW *initData )
603 HDC hdc;
604 DC * dc;
605 const struct gdi_dc_funcs *funcs;
606 WCHAR buf[300];
608 GDI_CheckNotLock();
610 if (!device || !DRIVER_GetDriverName( device, buf, 300 ))
612 if (!driver)
614 ERR( "no device found for %s\n", debugstr_w(device) );
615 return 0;
617 strcpyW(buf, driver);
620 if (!(funcs = DRIVER_load_driver( buf )))
622 ERR( "no driver found for %s\n", debugstr_w(buf) );
623 return 0;
625 if (!(dc = alloc_dc_ptr( OBJ_DC ))) goto error;
626 hdc = dc->hSelf;
628 dc->hBitmap = GDI_inc_ref_count( GetStockObject( DEFAULT_BITMAP ));
629 if (!(dc->hVisRgn = CreateRectRgn( 0, 0, 1, 1 ))) goto error;
631 TRACE("(driver=%s, device=%s, output=%s): returning %p\n",
632 debugstr_w(driver), debugstr_w(device), debugstr_w(output), dc->hSelf );
634 if (funcs->pCreateDC)
636 if (!funcs->pCreateDC( &dc->physDev, buf, device, output, initData ))
638 WARN("creation aborted by device\n" );
639 goto error;
643 dc->vis_rect.left = 0;
644 dc->vis_rect.top = 0;
645 dc->vis_rect.right = GetDeviceCaps( hdc, DESKTOPHORZRES );
646 dc->vis_rect.bottom = GetDeviceCaps( hdc, DESKTOPVERTRES );
647 SetRectRgn(dc->hVisRgn, dc->vis_rect.left, dc->vis_rect.top, dc->vis_rect.right, dc->vis_rect.bottom);
649 DC_InitDC( dc );
650 release_dc_ptr( dc );
651 return hdc;
653 error:
654 if (dc) free_dc_ptr( dc );
655 return 0;
659 /***********************************************************************
660 * CreateDCA (GDI32.@)
662 HDC WINAPI CreateDCA( LPCSTR driver, LPCSTR device, LPCSTR output,
663 const DEVMODEA *initData )
665 UNICODE_STRING driverW, deviceW, outputW;
666 DEVMODEW *initDataW;
667 HDC ret;
669 if (driver) RtlCreateUnicodeStringFromAsciiz(&driverW, driver);
670 else driverW.Buffer = NULL;
672 if (device) RtlCreateUnicodeStringFromAsciiz(&deviceW, device);
673 else deviceW.Buffer = NULL;
675 if (output) RtlCreateUnicodeStringFromAsciiz(&outputW, output);
676 else outputW.Buffer = NULL;
678 initDataW = NULL;
679 if (initData)
681 /* don't convert initData for DISPLAY driver, it's not used */
682 if (!driverW.Buffer || strcmpiW( driverW.Buffer, displayW ))
683 initDataW = GdiConvertToDevmodeW(initData);
686 ret = CreateDCW( driverW.Buffer, deviceW.Buffer, outputW.Buffer, initDataW );
688 RtlFreeUnicodeString(&driverW);
689 RtlFreeUnicodeString(&deviceW);
690 RtlFreeUnicodeString(&outputW);
691 HeapFree(GetProcessHeap(), 0, initDataW);
692 return ret;
696 /***********************************************************************
697 * CreateICA (GDI32.@)
699 HDC WINAPI CreateICA( LPCSTR driver, LPCSTR device, LPCSTR output,
700 const DEVMODEA* initData )
702 /* Nothing special yet for ICs */
703 return CreateDCA( driver, device, output, initData );
707 /***********************************************************************
708 * CreateICW (GDI32.@)
710 HDC WINAPI CreateICW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
711 const DEVMODEW* initData )
713 /* Nothing special yet for ICs */
714 return CreateDCW( driver, device, output, initData );
718 /***********************************************************************
719 * CreateCompatibleDC (GDI32.@)
721 HDC WINAPI CreateCompatibleDC( HDC hdc )
723 DC *dc, *origDC;
724 HDC ret;
725 const struct gdi_dc_funcs *funcs = &null_driver;
726 PHYSDEV physDev = NULL;
728 GDI_CheckNotLock();
730 if (hdc)
732 if (!(origDC = get_dc_ptr( hdc ))) return 0;
733 physDev = GET_DC_PHYSDEV( origDC, pCreateCompatibleDC );
734 funcs = physDev->funcs;
735 release_dc_ptr( origDC );
738 if (!(dc = alloc_dc_ptr( OBJ_MEMDC ))) goto error;
740 TRACE("(%p): returning %p\n", hdc, dc->hSelf );
742 dc->hBitmap = GDI_inc_ref_count( GetStockObject( DEFAULT_BITMAP ));
743 dc->vis_rect.left = 0;
744 dc->vis_rect.top = 0;
745 dc->vis_rect.right = 1;
746 dc->vis_rect.bottom = 1;
747 if (!(dc->hVisRgn = CreateRectRgn( 0, 0, 1, 1 ))) goto error; /* default bitmap is 1x1 */
749 ret = dc->hSelf;
751 if (!funcs->pCreateCompatibleDC( physDev, &dc->physDev ))
753 WARN("creation aborted by device\n");
754 goto error;
756 DC_InitDC( dc );
757 release_dc_ptr( dc );
758 return ret;
760 error:
761 if (dc) free_dc_ptr( dc );
762 return 0;
766 /***********************************************************************
767 * DeleteDC (GDI32.@)
769 BOOL WINAPI DeleteDC( HDC hdc )
771 DC * dc;
773 TRACE("%p\n", hdc );
775 GDI_CheckNotLock();
777 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
778 if (dc->refcount != 1)
780 FIXME( "not deleting busy DC %p refcount %u\n", dc->hSelf, dc->refcount );
781 release_dc_ptr( dc );
782 return FALSE;
785 /* Call hook procedure to check whether is it OK to delete this DC */
786 if (dc->hookProc && !dc->hookProc( hdc, DCHC_DELETEDC, dc->dwHookData, 0 ))
788 release_dc_ptr( dc );
789 return TRUE;
792 while (dc->saveLevel)
794 DC *dcs = dc->saved_dc;
795 dc->saved_dc = dcs->saved_dc;
796 dc->saveLevel--;
797 free_dc_state( dcs );
800 AbortPath( hdc );
801 SelectObject( hdc, GetStockObject(BLACK_PEN) );
802 SelectObject( hdc, GetStockObject(WHITE_BRUSH) );
803 SelectObject( hdc, GetStockObject(SYSTEM_FONT) );
804 SelectObject( hdc, GetStockObject(DEFAULT_BITMAP) );
806 free_dc_ptr( dc );
807 return TRUE;
811 /***********************************************************************
812 * ResetDCW (GDI32.@)
814 HDC WINAPI ResetDCW( HDC hdc, const DEVMODEW *devmode )
816 DC *dc;
817 HDC ret = 0;
819 if ((dc = get_dc_ptr( hdc )))
821 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pResetDC );
822 ret = physdev->funcs->pResetDC( physdev, devmode );
823 if (ret) /* reset the visible region */
825 dc->dirty = 0;
826 dc->vis_rect.left = 0;
827 dc->vis_rect.top = 0;
828 dc->vis_rect.right = GetDeviceCaps( hdc, DESKTOPHORZRES );
829 dc->vis_rect.bottom = GetDeviceCaps( hdc, DESKTOPVERTRES );
830 SetRectRgn( dc->hVisRgn, dc->vis_rect.left, dc->vis_rect.top,
831 dc->vis_rect.right, dc->vis_rect.bottom );
832 CLIPPING_UpdateGCRegion( dc );
834 release_dc_ptr( dc );
836 return ret;
840 /***********************************************************************
841 * ResetDCA (GDI32.@)
843 HDC WINAPI ResetDCA( HDC hdc, const DEVMODEA *devmode )
845 DEVMODEW *devmodeW;
846 HDC ret;
848 if (devmode) devmodeW = GdiConvertToDevmodeW(devmode);
849 else devmodeW = NULL;
851 ret = ResetDCW(hdc, devmodeW);
853 HeapFree(GetProcessHeap(), 0, devmodeW);
854 return ret;
858 /***********************************************************************
859 * GetDeviceCaps (GDI32.@)
861 INT WINAPI GetDeviceCaps( HDC hdc, INT cap )
863 DC *dc;
864 INT ret = 0;
866 if ((dc = get_dc_ptr( hdc )))
868 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pGetDeviceCaps );
869 ret = physdev->funcs->pGetDeviceCaps( physdev, cap );
870 release_dc_ptr( dc );
872 return ret;
876 /***********************************************************************
877 * GetBkColor (GDI32.@)
879 COLORREF WINAPI GetBkColor( HDC hdc )
881 COLORREF ret = 0;
882 DC * dc = get_dc_ptr( hdc );
883 if (dc)
885 ret = dc->backgroundColor;
886 release_dc_ptr( dc );
888 return ret;
892 /***********************************************************************
893 * SetBkColor (GDI32.@)
895 COLORREF WINAPI SetBkColor( HDC hdc, COLORREF color )
897 COLORREF ret = CLR_INVALID;
898 DC * dc = get_dc_ptr( hdc );
900 TRACE("hdc=%p color=0x%08x\n", hdc, color);
902 if (dc)
904 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetBkColor );
905 color = physdev->funcs->pSetBkColor( physdev, color );
906 if (color != CLR_INVALID)
908 ret = dc->backgroundColor;
909 dc->backgroundColor = color;
911 release_dc_ptr( dc );
913 return ret;
917 /***********************************************************************
918 * GetTextColor (GDI32.@)
920 COLORREF WINAPI GetTextColor( HDC hdc )
922 COLORREF ret = 0;
923 DC * dc = get_dc_ptr( hdc );
924 if (dc)
926 ret = dc->textColor;
927 release_dc_ptr( dc );
929 return ret;
933 /***********************************************************************
934 * SetTextColor (GDI32.@)
936 COLORREF WINAPI SetTextColor( HDC hdc, COLORREF color )
938 COLORREF ret = CLR_INVALID;
939 DC * dc = get_dc_ptr( hdc );
941 TRACE(" hdc=%p color=0x%08x\n", hdc, color);
943 if (dc)
945 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetTextColor );
946 color = physdev->funcs->pSetTextColor( physdev, color );
947 if (color != CLR_INVALID)
949 ret = dc->textColor;
950 dc->textColor = color;
952 release_dc_ptr( dc );
954 return ret;
958 /***********************************************************************
959 * GetTextAlign (GDI32.@)
961 UINT WINAPI GetTextAlign( HDC hdc )
963 UINT ret = 0;
964 DC * dc = get_dc_ptr( hdc );
965 if (dc)
967 ret = dc->textAlign;
968 release_dc_ptr( dc );
970 return ret;
974 /***********************************************************************
975 * SetTextAlign (GDI32.@)
977 UINT WINAPI SetTextAlign( HDC hdc, UINT align )
979 UINT ret = GDI_ERROR;
980 DC *dc = get_dc_ptr( hdc );
982 TRACE("hdc=%p align=%d\n", hdc, align);
984 if (dc)
986 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetTextAlign );
987 align = physdev->funcs->pSetTextAlign( physdev, align );
988 if (align != GDI_ERROR)
990 ret = dc->textAlign;
991 dc->textAlign = align;
993 release_dc_ptr( dc );
995 return ret;
998 /***********************************************************************
999 * GetDCOrgEx (GDI32.@)
1001 BOOL WINAPI GetDCOrgEx( HDC hDC, LPPOINT lpp )
1003 DC * dc;
1005 if (!lpp) return FALSE;
1006 if (!(dc = get_dc_ptr( hDC ))) return FALSE;
1007 lpp->x = dc->vis_rect.left;
1008 lpp->y = dc->vis_rect.top;
1009 release_dc_ptr( dc );
1010 return TRUE;
1014 /***********************************************************************
1015 * GetGraphicsMode (GDI32.@)
1017 INT WINAPI GetGraphicsMode( HDC hdc )
1019 INT ret = 0;
1020 DC * dc = get_dc_ptr( hdc );
1021 if (dc)
1023 ret = dc->GraphicsMode;
1024 release_dc_ptr( dc );
1026 return ret;
1030 /***********************************************************************
1031 * SetGraphicsMode (GDI32.@)
1033 INT WINAPI SetGraphicsMode( HDC hdc, INT mode )
1035 INT ret = 0;
1036 DC *dc = get_dc_ptr( hdc );
1038 /* One would think that setting the graphics mode to GM_COMPATIBLE
1039 * would also reset the world transformation matrix to the unity
1040 * matrix. However, in Windows, this is not the case. This doesn't
1041 * make a lot of sense to me, but that's the way it is.
1043 if (!dc) return 0;
1044 if ((mode > 0) && (mode <= GM_LAST))
1046 ret = dc->GraphicsMode;
1047 dc->GraphicsMode = mode;
1049 release_dc_ptr( dc );
1050 return ret;
1054 /***********************************************************************
1055 * GetArcDirection (GDI32.@)
1057 INT WINAPI GetArcDirection( HDC hdc )
1059 INT ret = 0;
1060 DC * dc = get_dc_ptr( hdc );
1061 if (dc)
1063 ret = dc->ArcDirection;
1064 release_dc_ptr( dc );
1066 return ret;
1070 /***********************************************************************
1071 * SetArcDirection (GDI32.@)
1073 INT WINAPI SetArcDirection( HDC hdc, INT dir )
1075 DC * dc;
1076 INT ret = 0;
1078 if (dir != AD_COUNTERCLOCKWISE && dir != AD_CLOCKWISE)
1080 SetLastError(ERROR_INVALID_PARAMETER);
1081 return 0;
1084 if ((dc = get_dc_ptr( hdc )))
1086 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetArcDirection );
1087 dir = physdev->funcs->pSetArcDirection( physdev, dir );
1088 if (dir)
1090 ret = dc->ArcDirection;
1091 dc->ArcDirection = dir;
1093 release_dc_ptr( dc );
1095 return ret;
1099 /***********************************************************************
1100 * GetWorldTransform (GDI32.@)
1102 BOOL WINAPI GetWorldTransform( HDC hdc, LPXFORM xform )
1104 DC * dc;
1105 if (!xform) return FALSE;
1106 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
1107 *xform = dc->xformWorld2Wnd;
1108 release_dc_ptr( dc );
1109 return TRUE;
1113 /***********************************************************************
1114 * GetTransform (GDI32.@)
1116 * Undocumented
1118 * Returns one of the co-ordinate space transforms
1120 * PARAMS
1121 * hdc [I] Device context.
1122 * which [I] Which xform to return:
1123 * 0x203 World -> Page transform (that set by SetWorldTransform).
1124 * 0x304 Page -> Device transform (the mapping mode transform).
1125 * 0x204 World -> Device transform (the combination of the above two).
1126 * 0x402 Device -> World transform (the inversion of the above).
1127 * xform [O] The xform.
1130 BOOL WINAPI GetTransform( HDC hdc, DWORD which, XFORM *xform )
1132 BOOL ret = TRUE;
1133 DC *dc = get_dc_ptr( hdc );
1134 if (!dc) return FALSE;
1136 switch(which)
1138 case 0x203:
1139 *xform = dc->xformWorld2Wnd;
1140 break;
1142 case 0x304:
1143 construct_window_to_viewport(dc, xform);
1144 break;
1146 case 0x204:
1147 *xform = dc->xformWorld2Vport;
1148 break;
1150 case 0x402:
1151 *xform = dc->xformVport2World;
1152 break;
1154 default:
1155 FIXME("Unknown code %x\n", which);
1156 ret = FALSE;
1159 release_dc_ptr( dc );
1160 return ret;
1164 /****************************************************************************
1165 * CombineTransform [GDI32.@]
1166 * Combines two transformation matrices.
1168 * PARAMS
1169 * xformResult [O] Stores the result of combining the two matrices
1170 * xform1 [I] Specifies the first matrix to apply
1171 * xform2 [I] Specifies the second matrix to apply
1173 * REMARKS
1174 * The same matrix can be passed in for more than one of the parameters.
1176 * RETURNS
1177 * Success: TRUE.
1178 * Failure: FALSE. Use GetLastError() to determine the cause.
1180 BOOL WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
1181 const XFORM *xform2 )
1183 XFORM xformTemp;
1185 /* Check for illegal parameters */
1186 if (!xformResult || !xform1 || !xform2)
1187 return FALSE;
1189 /* Create the result in a temporary XFORM, since xformResult may be
1190 * equal to xform1 or xform2 */
1191 xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
1192 xform1->eM12 * xform2->eM21;
1193 xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
1194 xform1->eM12 * xform2->eM22;
1195 xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
1196 xform1->eM22 * xform2->eM21;
1197 xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
1198 xform1->eM22 * xform2->eM22;
1199 xformTemp.eDx = xform1->eDx * xform2->eM11 +
1200 xform1->eDy * xform2->eM21 +
1201 xform2->eDx;
1202 xformTemp.eDy = xform1->eDx * xform2->eM12 +
1203 xform1->eDy * xform2->eM22 +
1204 xform2->eDy;
1206 /* Copy the result to xformResult */
1207 *xformResult = xformTemp;
1209 return TRUE;
1213 /***********************************************************************
1214 * SetDCHook (GDI32.@)
1216 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1218 BOOL WINAPI SetDCHook( HDC hdc, DCHOOKPROC hookProc, DWORD_PTR dwHookData )
1220 DC *dc = get_dc_ptr( hdc );
1222 if (!dc) return FALSE;
1224 dc->dwHookData = dwHookData;
1225 dc->hookProc = hookProc;
1226 release_dc_ptr( dc );
1227 return TRUE;
1231 /***********************************************************************
1232 * GetDCHook (GDI32.@)
1234 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1236 DWORD_PTR WINAPI GetDCHook( HDC hdc, DCHOOKPROC *proc )
1238 DC *dc = get_dc_ptr( hdc );
1239 DWORD_PTR ret;
1241 if (!dc) return 0;
1242 if (proc) *proc = dc->hookProc;
1243 ret = dc->dwHookData;
1244 release_dc_ptr( dc );
1245 return ret;
1249 /***********************************************************************
1250 * SetHookFlags (GDI32.@)
1252 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1254 WORD WINAPI SetHookFlags( HDC hdc, WORD flags )
1256 DC *dc = get_dc_obj( hdc ); /* not get_dc_ptr, this needs to work from any thread */
1257 LONG ret = 0;
1259 if (!dc) return 0;
1261 /* "Undocumented Windows" info is slightly confusing. */
1263 TRACE("hDC %p, flags %04x\n",hdc,flags);
1265 if (flags & DCHF_INVALIDATEVISRGN)
1266 ret = InterlockedExchange( &dc->dirty, 1 );
1267 else if (flags & DCHF_VALIDATEVISRGN || !flags)
1268 ret = InterlockedExchange( &dc->dirty, 0 );
1270 GDI_ReleaseObj( hdc );
1271 return ret;
1274 /***********************************************************************
1275 * SetICMMode (GDI32.@)
1277 INT WINAPI SetICMMode(HDC hdc, INT iEnableICM)
1279 /*FIXME: Assume that ICM is always off, and cannot be turned on */
1280 if (iEnableICM == ICM_OFF) return ICM_OFF;
1281 if (iEnableICM == ICM_ON) return 0;
1282 if (iEnableICM == ICM_QUERY) return ICM_OFF;
1283 return 0;
1286 /***********************************************************************
1287 * GetDeviceGammaRamp (GDI32.@)
1289 BOOL WINAPI GetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1291 BOOL ret = FALSE;
1292 DC *dc = get_dc_ptr( hDC );
1294 TRACE("%p, %p\n", hDC, ptr);
1295 if( dc )
1297 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pGetDeviceGammaRamp );
1298 ret = physdev->funcs->pGetDeviceGammaRamp( physdev, ptr );
1299 release_dc_ptr( dc );
1301 return ret;
1304 /***********************************************************************
1305 * SetDeviceGammaRamp (GDI32.@)
1307 BOOL WINAPI SetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1309 BOOL ret = FALSE;
1310 DC *dc = get_dc_ptr( hDC );
1312 TRACE("%p, %p\n", hDC, ptr);
1313 if( dc )
1315 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetDeviceGammaRamp );
1316 ret = physdev->funcs->pSetDeviceGammaRamp( physdev, ptr );
1317 release_dc_ptr( dc );
1319 return ret;
1322 /***********************************************************************
1323 * GetColorSpace (GDI32.@)
1325 HCOLORSPACE WINAPI GetColorSpace(HDC hdc)
1327 /*FIXME Need to do whatever GetColorSpace actually does */
1328 return 0;
1331 /***********************************************************************
1332 * CreateColorSpaceA (GDI32.@)
1334 HCOLORSPACE WINAPI CreateColorSpaceA( LPLOGCOLORSPACEA lpLogColorSpace )
1336 FIXME( "stub\n" );
1337 return 0;
1340 /***********************************************************************
1341 * CreateColorSpaceW (GDI32.@)
1343 HCOLORSPACE WINAPI CreateColorSpaceW( LPLOGCOLORSPACEW lpLogColorSpace )
1345 FIXME( "stub\n" );
1346 return 0;
1349 /***********************************************************************
1350 * DeleteColorSpace (GDI32.@)
1352 BOOL WINAPI DeleteColorSpace( HCOLORSPACE hColorSpace )
1354 FIXME( "stub\n" );
1356 return TRUE;
1359 /***********************************************************************
1360 * SetColorSpace (GDI32.@)
1362 HCOLORSPACE WINAPI SetColorSpace( HDC hDC, HCOLORSPACE hColorSpace )
1364 FIXME( "stub\n" );
1366 return hColorSpace;
1369 /***********************************************************************
1370 * GetBoundsRect (GDI32.@)
1372 UINT WINAPI GetBoundsRect(HDC hdc, LPRECT rect, UINT flags)
1374 UINT ret = 0;
1375 DC *dc = get_dc_ptr( hdc );
1377 if ( !dc ) return 0;
1379 if (rect)
1381 *rect = dc->BoundsRect;
1382 ret = is_rect_empty( rect ) ? DCB_RESET : DCB_SET;
1383 DPtoLP( hdc, (POINT *)rect, 2 );
1385 if (flags & DCB_RESET)
1387 dc->BoundsRect.left = 0;
1388 dc->BoundsRect.top = 0;
1389 dc->BoundsRect.right = 0;
1390 dc->BoundsRect.bottom = 0;
1392 release_dc_ptr( dc );
1393 return ret;
1397 /***********************************************************************
1398 * SetBoundsRect (GDI32.@)
1400 UINT WINAPI SetBoundsRect(HDC hdc, const RECT* rect, UINT flags)
1402 UINT ret;
1403 DC *dc;
1405 if ((flags & DCB_ENABLE) && (flags & DCB_DISABLE)) return 0;
1406 if (!(dc = get_dc_ptr( hdc ))) return 0;
1408 ret = ((dc->flags & DC_BOUNDS_ENABLE) ? DCB_ENABLE : DCB_DISABLE) |
1409 (is_rect_empty( &dc->BoundsRect ) ? DCB_RESET : DCB_SET);
1411 if (flags & DCB_RESET)
1413 dc->BoundsRect.left = 0;
1414 dc->BoundsRect.top = 0;
1415 dc->BoundsRect.right = 0;
1416 dc->BoundsRect.bottom = 0;
1419 if ((flags & DCB_ACCUMULATE) && rect)
1421 RECT rc = *rect;
1423 LPtoDP( hdc, (POINT *)&rc, 2 );
1424 if (!is_rect_empty( &rc ))
1426 if (!is_rect_empty( &dc->BoundsRect))
1428 dc->BoundsRect.left = min( dc->BoundsRect.left, rc.left );
1429 dc->BoundsRect.top = min( dc->BoundsRect.top, rc.top );
1430 dc->BoundsRect.right = max( dc->BoundsRect.right, rc.right );
1431 dc->BoundsRect.bottom = max( dc->BoundsRect.bottom, rc.bottom );
1433 else dc->BoundsRect = rc;
1437 if (flags & DCB_ENABLE) dc->flags |= DC_BOUNDS_ENABLE;
1438 if (flags & DCB_DISABLE) dc->flags &= ~DC_BOUNDS_ENABLE;
1440 release_dc_ptr( dc );
1441 return ret;
1445 /***********************************************************************
1446 * GetRelAbs (GDI32.@)
1448 INT WINAPI GetRelAbs( HDC hdc, DWORD dwIgnore )
1450 INT ret = 0;
1451 DC *dc = get_dc_ptr( hdc );
1452 if (dc)
1454 ret = dc->relAbsMode;
1455 release_dc_ptr( dc );
1457 return ret;
1463 /***********************************************************************
1464 * GetBkMode (GDI32.@)
1466 INT WINAPI GetBkMode( HDC hdc )
1468 INT ret = 0;
1469 DC * dc = get_dc_ptr( hdc );
1470 if (dc)
1472 ret = dc->backgroundMode;
1473 release_dc_ptr( dc );
1475 return ret;
1479 /***********************************************************************
1480 * SetBkMode (GDI32.@)
1482 INT WINAPI SetBkMode( HDC hdc, INT mode )
1484 INT ret = 0;
1485 DC *dc;
1487 if ((mode <= 0) || (mode > BKMODE_LAST))
1489 SetLastError(ERROR_INVALID_PARAMETER);
1490 return 0;
1492 if ((dc = get_dc_ptr( hdc )))
1494 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetBkMode );
1495 mode = physdev->funcs->pSetBkMode( physdev, mode );
1496 if (mode)
1498 ret = dc->backgroundMode;
1499 dc->backgroundMode = mode;
1501 release_dc_ptr( dc );
1503 return ret;
1507 /***********************************************************************
1508 * GetROP2 (GDI32.@)
1510 INT WINAPI GetROP2( HDC hdc )
1512 INT ret = 0;
1513 DC * dc = get_dc_ptr( hdc );
1514 if (dc)
1516 ret = dc->ROPmode;
1517 release_dc_ptr( dc );
1519 return ret;
1523 /***********************************************************************
1524 * SetROP2 (GDI32.@)
1526 INT WINAPI SetROP2( HDC hdc, INT mode )
1528 INT ret = 0;
1529 DC *dc;
1531 if ((mode < R2_BLACK) || (mode > R2_WHITE))
1533 SetLastError(ERROR_INVALID_PARAMETER);
1534 return 0;
1536 if ((dc = get_dc_ptr( hdc )))
1538 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetROP2 );
1539 mode = physdev->funcs->pSetROP2( physdev, mode );
1540 if (mode)
1542 ret = dc->ROPmode;
1543 dc->ROPmode = mode;
1545 release_dc_ptr( dc );
1547 return ret;
1551 /***********************************************************************
1552 * SetRelAbs (GDI32.@)
1554 INT WINAPI SetRelAbs( HDC hdc, INT mode )
1556 INT ret = 0;
1557 DC *dc;
1559 if ((mode != ABSOLUTE) && (mode != RELATIVE))
1561 SetLastError(ERROR_INVALID_PARAMETER);
1562 return 0;
1564 if ((dc = get_dc_ptr( hdc )))
1566 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetRelAbs );
1567 mode = physdev->funcs->pSetRelAbs( physdev, mode );
1568 if (mode)
1570 ret = dc->relAbsMode;
1571 dc->relAbsMode = mode;
1573 release_dc_ptr( dc );
1575 return ret;
1579 /***********************************************************************
1580 * GetPolyFillMode (GDI32.@)
1582 INT WINAPI GetPolyFillMode( HDC hdc )
1584 INT ret = 0;
1585 DC * dc = get_dc_ptr( hdc );
1586 if (dc)
1588 ret = dc->polyFillMode;
1589 release_dc_ptr( dc );
1591 return ret;
1595 /***********************************************************************
1596 * SetPolyFillMode (GDI32.@)
1598 INT WINAPI SetPolyFillMode( HDC hdc, INT mode )
1600 INT ret = 0;
1601 DC *dc;
1603 if ((mode <= 0) || (mode > POLYFILL_LAST))
1605 SetLastError(ERROR_INVALID_PARAMETER);
1606 return 0;
1608 if ((dc = get_dc_ptr( hdc )))
1610 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetPolyFillMode );
1611 mode = physdev->funcs->pSetPolyFillMode( physdev, mode );
1612 if (mode)
1614 ret = dc->polyFillMode;
1615 dc->polyFillMode = mode;
1617 release_dc_ptr( dc );
1619 return ret;
1623 /***********************************************************************
1624 * GetStretchBltMode (GDI32.@)
1626 INT WINAPI GetStretchBltMode( HDC hdc )
1628 INT ret = 0;
1629 DC * dc = get_dc_ptr( hdc );
1630 if (dc)
1632 ret = dc->stretchBltMode;
1633 release_dc_ptr( dc );
1635 return ret;
1639 /***********************************************************************
1640 * SetStretchBltMode (GDI32.@)
1642 INT WINAPI SetStretchBltMode( HDC hdc, INT mode )
1644 INT ret = 0;
1645 DC *dc;
1647 if ((mode <= 0) || (mode > MAXSTRETCHBLTMODE))
1649 SetLastError(ERROR_INVALID_PARAMETER);
1650 return 0;
1652 if ((dc = get_dc_ptr( hdc )))
1654 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetStretchBltMode );
1655 mode = physdev->funcs->pSetStretchBltMode( physdev, mode );
1656 if (mode)
1658 ret = dc->stretchBltMode;
1659 dc->stretchBltMode = mode;
1661 release_dc_ptr( dc );
1663 return ret;
1667 /***********************************************************************
1668 * GetMapMode (GDI32.@)
1670 INT WINAPI GetMapMode( HDC hdc )
1672 INT ret = 0;
1673 DC * dc = get_dc_ptr( hdc );
1674 if (dc)
1676 ret = dc->MapMode;
1677 release_dc_ptr( dc );
1679 return ret;
1683 /***********************************************************************
1684 * GetBrushOrgEx (GDI32.@)
1686 BOOL WINAPI GetBrushOrgEx( HDC hdc, LPPOINT pt )
1688 DC * dc = get_dc_ptr( hdc );
1689 if (!dc) return FALSE;
1690 pt->x = dc->brushOrgX;
1691 pt->y = dc->brushOrgY;
1692 release_dc_ptr( dc );
1693 return TRUE;
1697 /***********************************************************************
1698 * GetCurrentPositionEx (GDI32.@)
1700 BOOL WINAPI GetCurrentPositionEx( HDC hdc, LPPOINT pt )
1702 DC * dc = get_dc_ptr( hdc );
1703 if (!dc) return FALSE;
1704 pt->x = dc->CursPosX;
1705 pt->y = dc->CursPosY;
1706 release_dc_ptr( dc );
1707 return TRUE;
1711 /***********************************************************************
1712 * GetViewportExtEx (GDI32.@)
1714 BOOL WINAPI GetViewportExtEx( HDC hdc, LPSIZE size )
1716 DC * dc = get_dc_ptr( hdc );
1717 if (!dc) return FALSE;
1718 size->cx = dc->vportExtX;
1719 size->cy = dc->vportExtY;
1720 release_dc_ptr( dc );
1721 return TRUE;
1725 /***********************************************************************
1726 * GetViewportOrgEx (GDI32.@)
1728 BOOL WINAPI GetViewportOrgEx( HDC hdc, LPPOINT pt )
1730 DC * dc = get_dc_ptr( hdc );
1731 if (!dc) return FALSE;
1732 pt->x = dc->vportOrgX;
1733 pt->y = dc->vportOrgY;
1734 release_dc_ptr( dc );
1735 return TRUE;
1739 /***********************************************************************
1740 * GetWindowExtEx (GDI32.@)
1742 BOOL WINAPI GetWindowExtEx( HDC hdc, LPSIZE size )
1744 DC * dc = get_dc_ptr( hdc );
1745 if (!dc) return FALSE;
1746 size->cx = dc->wndExtX;
1747 size->cy = dc->wndExtY;
1748 release_dc_ptr( dc );
1749 return TRUE;
1753 /***********************************************************************
1754 * GetWindowOrgEx (GDI32.@)
1756 BOOL WINAPI GetWindowOrgEx( HDC hdc, LPPOINT pt )
1758 DC * dc = get_dc_ptr( hdc );
1759 if (!dc) return FALSE;
1760 pt->x = dc->wndOrgX;
1761 pt->y = dc->wndOrgY;
1762 release_dc_ptr( dc );
1763 return TRUE;
1767 /***********************************************************************
1768 * GetLayout (GDI32.@)
1770 * Gets left->right or right->left text layout flags of a dc.
1773 DWORD WINAPI GetLayout(HDC hdc)
1775 DWORD layout = GDI_ERROR;
1777 DC * dc = get_dc_ptr( hdc );
1778 if (dc)
1780 layout = dc->layout;
1781 release_dc_ptr( dc );
1784 TRACE("hdc : %p, layout : %08x\n", hdc, layout);
1786 return layout;
1789 /***********************************************************************
1790 * SetLayout (GDI32.@)
1792 * Sets left->right or right->left text layout flags of a dc.
1795 DWORD WINAPI SetLayout(HDC hdc, DWORD layout)
1797 DWORD oldlayout = GDI_ERROR;
1799 DC * dc = get_dc_ptr( hdc );
1800 if (dc)
1802 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetLayout );
1803 layout = physdev->funcs->pSetLayout( physdev, layout );
1804 if (layout != GDI_ERROR)
1806 oldlayout = dc->layout;
1807 dc->layout = layout;
1808 if (layout != oldlayout)
1810 if (layout & LAYOUT_RTL) dc->MapMode = MM_ANISOTROPIC;
1811 DC_UpdateXforms( dc );
1814 release_dc_ptr( dc );
1817 TRACE("hdc : %p, old layout : %08x, new layout : %08x\n", hdc, oldlayout, layout);
1819 return oldlayout;
1822 /***********************************************************************
1823 * GetDCBrushColor (GDI32.@)
1825 COLORREF WINAPI GetDCBrushColor(HDC hdc)
1827 DC *dc;
1828 COLORREF dcBrushColor = CLR_INVALID;
1830 TRACE("hdc(%p)\n", hdc);
1832 dc = get_dc_ptr( hdc );
1833 if (dc)
1835 dcBrushColor = dc->dcBrushColor;
1836 release_dc_ptr( dc );
1839 return dcBrushColor;
1842 /***********************************************************************
1843 * SetDCBrushColor (GDI32.@)
1845 COLORREF WINAPI SetDCBrushColor(HDC hdc, COLORREF crColor)
1847 DC *dc;
1848 COLORREF oldClr = CLR_INVALID;
1850 TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
1852 dc = get_dc_ptr( hdc );
1853 if (dc)
1855 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetDCBrushColor );
1856 crColor = physdev->funcs->pSetDCBrushColor( physdev, crColor );
1857 if (crColor != CLR_INVALID)
1859 oldClr = dc->dcBrushColor;
1860 dc->dcBrushColor = crColor;
1862 release_dc_ptr( dc );
1865 return oldClr;
1868 /***********************************************************************
1869 * GetDCPenColor (GDI32.@)
1871 COLORREF WINAPI GetDCPenColor(HDC hdc)
1873 DC *dc;
1874 COLORREF dcPenColor = CLR_INVALID;
1876 TRACE("hdc(%p)\n", hdc);
1878 dc = get_dc_ptr( hdc );
1879 if (dc)
1881 dcPenColor = dc->dcPenColor;
1882 release_dc_ptr( dc );
1885 return dcPenColor;
1888 /***********************************************************************
1889 * SetDCPenColor (GDI32.@)
1891 COLORREF WINAPI SetDCPenColor(HDC hdc, COLORREF crColor)
1893 DC *dc;
1894 COLORREF oldClr = CLR_INVALID;
1896 TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
1898 dc = get_dc_ptr( hdc );
1899 if (dc)
1901 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetDCPenColor );
1902 crColor = physdev->funcs->pSetDCPenColor( physdev, crColor );
1903 if (crColor != CLR_INVALID)
1905 oldClr = dc->dcPenColor;
1906 dc->dcPenColor = crColor;
1908 release_dc_ptr( dc );
1911 return oldClr;
1914 /***********************************************************************
1915 * CancelDC (GDI32.@)
1917 BOOL WINAPI CancelDC(HDC hdc)
1919 FIXME("stub\n");
1920 return TRUE;
1923 /*******************************************************************
1924 * GetMiterLimit [GDI32.@]
1928 BOOL WINAPI GetMiterLimit(HDC hdc, PFLOAT peLimit)
1930 BOOL bRet = FALSE;
1931 DC *dc;
1933 TRACE("(%p,%p)\n", hdc, peLimit);
1935 dc = get_dc_ptr( hdc );
1936 if (dc)
1938 if (peLimit)
1939 *peLimit = dc->miterLimit;
1941 release_dc_ptr( dc );
1942 bRet = TRUE;
1944 return bRet;
1947 /*******************************************************************
1948 * SetMiterLimit [GDI32.@]
1952 BOOL WINAPI SetMiterLimit(HDC hdc, FLOAT eNewLimit, PFLOAT peOldLimit)
1954 BOOL bRet = FALSE;
1955 DC *dc;
1957 TRACE("(%p,%f,%p)\n", hdc, eNewLimit, peOldLimit);
1959 dc = get_dc_ptr( hdc );
1960 if (dc)
1962 if (peOldLimit)
1963 *peOldLimit = dc->miterLimit;
1964 dc->miterLimit = eNewLimit;
1965 release_dc_ptr( dc );
1966 bRet = TRUE;
1968 return bRet;
1971 /*******************************************************************
1972 * GdiIsMetaPrintDC [GDI32.@]
1974 BOOL WINAPI GdiIsMetaPrintDC(HDC hdc)
1976 FIXME("%p\n", hdc);
1977 return FALSE;
1980 /*******************************************************************
1981 * GdiIsMetaFileDC [GDI32.@]
1983 BOOL WINAPI GdiIsMetaFileDC(HDC hdc)
1985 TRACE("%p\n", hdc);
1987 switch( GetObjectType( hdc ) )
1989 case OBJ_METADC:
1990 case OBJ_ENHMETADC:
1991 return TRUE;
1993 return FALSE;
1996 /*******************************************************************
1997 * GdiIsPlayMetafileDC [GDI32.@]
1999 BOOL WINAPI GdiIsPlayMetafileDC(HDC hdc)
2001 FIXME("%p\n", hdc);
2002 return FALSE;