push 0bdbb9493a3f833df10c918231fa8ec20b56a418
[wine/hacks.git] / dlls / gdi32 / dc.c
blobfd28663a5a7848240b6aeef8d447e2bef24a4628
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 "wownt32.h"
34 #include "gdi_private.h"
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(dc);
40 static const WCHAR displayW[] = { 'd','i','s','p','l','a','y',0 };
42 static BOOL DC_DeleteObject( HGDIOBJ handle );
44 static const struct gdi_obj_funcs dc_funcs =
46 NULL, /* pSelectObject */
47 NULL, /* pGetObjectA */
48 NULL, /* pGetObjectW */
49 NULL, /* pUnrealizeObject */
50 DC_DeleteObject /* pDeleteObject */
54 static inline DC *get_dc_obj( HDC hdc )
56 DC *dc = GDI_GetObjPtr( hdc, 0 );
57 if (!dc) return NULL;
59 if ((dc->header.type != OBJ_DC) &&
60 (dc->header.type != OBJ_MEMDC) &&
61 (dc->header.type != OBJ_METADC) &&
62 (dc->header.type != OBJ_ENHMETADC))
64 GDI_ReleaseObj( hdc );
65 SetLastError( ERROR_INVALID_HANDLE );
66 dc = NULL;
68 return dc;
72 /***********************************************************************
73 * alloc_dc_ptr
75 DC *alloc_dc_ptr( const DC_FUNCTIONS *funcs, WORD magic )
77 DC *dc;
79 if (!(dc = HeapAlloc( GetProcessHeap(), 0, sizeof(*dc) ))) return NULL;
81 dc->funcs = funcs;
82 dc->physDev = NULL;
83 dc->thread = GetCurrentThreadId();
84 dc->refcount = 1;
85 dc->dirty = 0;
86 dc->saveLevel = 0;
87 dc->saved_dc = 0;
88 dc->dwHookData = 0;
89 dc->hookProc = NULL;
90 dc->hookThunk = 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->font_code_page = CP_ACP;
114 dc->ROPmode = R2_COPYPEN;
115 dc->polyFillMode = ALTERNATE;
116 dc->stretchBltMode = BLACKONWHITE;
117 dc->relAbsMode = ABSOLUTE;
118 dc->backgroundMode = OPAQUE;
119 dc->backgroundColor = RGB( 255, 255, 255 );
120 dc->dcBrushColor = RGB( 255, 255, 255 );
121 dc->dcPenColor = RGB( 0, 0, 0 );
122 dc->textColor = RGB( 0, 0, 0 );
123 dc->brushOrgX = 0;
124 dc->brushOrgY = 0;
125 dc->textAlign = TA_LEFT | TA_TOP | TA_NOUPDATECP;
126 dc->charExtra = 0;
127 dc->breakExtra = 0;
128 dc->breakRem = 0;
129 dc->MapMode = MM_TEXT;
130 dc->GraphicsMode = GM_COMPATIBLE;
131 dc->pAbortProc = NULL;
132 dc->CursPosX = 0;
133 dc->CursPosY = 0;
134 dc->ArcDirection = AD_COUNTERCLOCKWISE;
135 dc->xformWorld2Wnd.eM11 = 1.0f;
136 dc->xformWorld2Wnd.eM12 = 0.0f;
137 dc->xformWorld2Wnd.eM21 = 0.0f;
138 dc->xformWorld2Wnd.eM22 = 1.0f;
139 dc->xformWorld2Wnd.eDx = 0.0f;
140 dc->xformWorld2Wnd.eDy = 0.0f;
141 dc->xformWorld2Vport = dc->xformWorld2Wnd;
142 dc->xformVport2World = dc->xformWorld2Wnd;
143 dc->vport2WorldValid = TRUE;
144 dc->BoundsRect.left = 0;
145 dc->BoundsRect.top = 0;
146 dc->BoundsRect.right = 0;
147 dc->BoundsRect.bottom = 0;
148 dc->saved_visrgn = NULL;
149 PATH_InitGdiPath(&dc->path);
151 if (!(dc->hSelf = alloc_gdi_handle( &dc->header, magic, &dc_funcs )))
153 HeapFree( GetProcessHeap(), 0, dc );
154 dc = NULL;
156 return dc;
161 /***********************************************************************
162 * free_dc_ptr
164 BOOL free_dc_ptr( DC *dc )
166 assert( dc->refcount == 1 );
167 if (free_gdi_handle( dc->hSelf ) != dc) return FALSE; /* shouldn't happen */
168 return HeapFree( GetProcessHeap(), 0, dc );
172 /***********************************************************************
173 * get_dc_ptr
175 * Retrieve a DC pointer but release the GDI lock.
177 DC *get_dc_ptr( HDC hdc )
179 DC *dc = get_dc_obj( hdc );
180 if (!dc) return NULL;
182 if (!InterlockedCompareExchange( &dc->refcount, 1, 0 ))
184 dc->thread = GetCurrentThreadId();
186 else if (dc->thread != GetCurrentThreadId())
188 WARN( "dc %p belongs to thread %04x\n", hdc, dc->thread );
189 GDI_ReleaseObj( hdc );
190 return NULL;
192 else InterlockedIncrement( &dc->refcount );
194 GDI_ReleaseObj( hdc );
195 return dc;
199 /***********************************************************************
200 * release_dc_ptr
202 void release_dc_ptr( DC *dc )
204 LONG ref;
206 dc->thread = 0;
207 ref = InterlockedDecrement( &dc->refcount );
208 assert( ref >= 0 );
209 if (ref) dc->thread = GetCurrentThreadId(); /* we still own it */
213 /***********************************************************************
214 * update_dc
216 * Make sure the DC vis region is up to date.
217 * This function may call up to USER so the GDI lock should _not_
218 * be held when calling it.
220 void update_dc( DC *dc )
222 if (InterlockedExchange( &dc->dirty, 0 ) && dc->hookThunk)
223 dc->hookThunk( dc->hSelf, DCHC_INVALIDVISRGN, dc->dwHookData, 0 );
227 /***********************************************************************
228 * DC_DeleteObject
230 static BOOL DC_DeleteObject( HGDIOBJ handle )
232 return DeleteDC( handle );
236 /***********************************************************************
237 * DC_InitDC
239 * Setup device-specific DC values for a newly created DC.
241 void DC_InitDC( DC* dc )
243 if (dc->funcs->pRealizeDefaultPalette) dc->funcs->pRealizeDefaultPalette( dc->physDev );
244 SetTextColor( dc->hSelf, dc->textColor );
245 SetBkColor( dc->hSelf, dc->backgroundColor );
246 SelectObject( dc->hSelf, dc->hPen );
247 SelectObject( dc->hSelf, dc->hBrush );
248 SelectObject( dc->hSelf, dc->hFont );
249 CLIPPING_UpdateGCRegion( dc );
253 /***********************************************************************
254 * DC_InvertXform
256 * Computes the inverse of the transformation xformSrc and stores it to
257 * xformDest. Returns TRUE if successful or FALSE if the xformSrc matrix
258 * is singular.
260 static BOOL DC_InvertXform( const XFORM *xformSrc, XFORM *xformDest )
262 double determinant;
264 determinant = xformSrc->eM11*xformSrc->eM22 -
265 xformSrc->eM12*xformSrc->eM21;
266 if (determinant > -1e-12 && determinant < 1e-12)
267 return FALSE;
269 xformDest->eM11 = xformSrc->eM22 / determinant;
270 xformDest->eM12 = -xformSrc->eM12 / determinant;
271 xformDest->eM21 = -xformSrc->eM21 / determinant;
272 xformDest->eM22 = xformSrc->eM11 / determinant;
273 xformDest->eDx = -xformSrc->eDx * xformDest->eM11 -
274 xformSrc->eDy * xformDest->eM21;
275 xformDest->eDy = -xformSrc->eDx * xformDest->eM12 -
276 xformSrc->eDy * xformDest->eM22;
278 return TRUE;
282 /***********************************************************************
283 * DC_UpdateXforms
285 * Updates the xformWorld2Vport, xformVport2World and vport2WorldValid
286 * fields of the specified DC by creating a transformation that
287 * represents the current mapping mode and combining it with the DC's
288 * world transform. This function should be called whenever the
289 * parameters associated with the mapping mode (window and viewport
290 * extents and origins) or the world transform change.
292 void DC_UpdateXforms( DC *dc )
294 XFORM xformWnd2Vport, oldworld2vport;
295 double scaleX, scaleY;
297 /* Construct a transformation to do the window-to-viewport conversion */
298 scaleX = (double)dc->vportExtX / (double)dc->wndExtX;
299 scaleY = (double)dc->vportExtY / (double)dc->wndExtY;
300 xformWnd2Vport.eM11 = scaleX;
301 xformWnd2Vport.eM12 = 0.0;
302 xformWnd2Vport.eM21 = 0.0;
303 xformWnd2Vport.eM22 = scaleY;
304 xformWnd2Vport.eDx = (double)dc->vportOrgX -
305 scaleX * (double)dc->wndOrgX;
306 xformWnd2Vport.eDy = (double)dc->vportOrgY -
307 scaleY * (double)dc->wndOrgY;
309 oldworld2vport = dc->xformWorld2Vport;
310 /* Combine with the world transformation */
311 CombineTransform( &dc->xformWorld2Vport, &dc->xformWorld2Wnd,
312 &xformWnd2Vport );
314 /* Create inverse of world-to-viewport transformation */
315 dc->vport2WorldValid = DC_InvertXform( &dc->xformWorld2Vport,
316 &dc->xformVport2World );
318 /* Reselect the font and pen back into the dc so that the size
319 gets updated. */
320 if (memcmp(&oldworld2vport, &dc->xformWorld2Vport, sizeof(oldworld2vport)) &&
321 !GdiIsMetaFileDC(dc->hSelf))
323 SelectObject(dc->hSelf, GetCurrentObject(dc->hSelf, OBJ_FONT));
324 SelectObject(dc->hSelf, GetCurrentObject(dc->hSelf, OBJ_PEN));
329 /***********************************************************************
330 * get_dc_state (Not a Windows API)
332 HDC get_dc_state( HDC hdc )
334 DC * newdc, * dc;
335 HGDIOBJ handle;
337 if (!(dc = get_dc_ptr( hdc ))) return 0;
338 if (!(newdc = HeapAlloc( GetProcessHeap(), 0, sizeof(*newdc ))))
340 release_dc_ptr( dc );
341 return 0;
344 newdc->flags = dc->flags | DC_SAVED;
345 newdc->layout = dc->layout;
346 newdc->hPen = dc->hPen;
347 newdc->hBrush = dc->hBrush;
348 newdc->hFont = dc->hFont;
349 newdc->hBitmap = dc->hBitmap;
350 newdc->hDevice = dc->hDevice;
351 newdc->hPalette = dc->hPalette;
352 newdc->ROPmode = dc->ROPmode;
353 newdc->polyFillMode = dc->polyFillMode;
354 newdc->stretchBltMode = dc->stretchBltMode;
355 newdc->relAbsMode = dc->relAbsMode;
356 newdc->backgroundMode = dc->backgroundMode;
357 newdc->backgroundColor = dc->backgroundColor;
358 newdc->textColor = dc->textColor;
359 newdc->dcBrushColor = dc->dcBrushColor;
360 newdc->dcPenColor = dc->dcPenColor;
361 newdc->brushOrgX = dc->brushOrgX;
362 newdc->brushOrgY = dc->brushOrgY;
363 newdc->textAlign = dc->textAlign;
364 newdc->charExtra = dc->charExtra;
365 newdc->breakExtra = dc->breakExtra;
366 newdc->breakRem = dc->breakRem;
367 newdc->MapMode = dc->MapMode;
368 newdc->GraphicsMode = dc->GraphicsMode;
369 newdc->CursPosX = dc->CursPosX;
370 newdc->CursPosY = dc->CursPosY;
371 newdc->ArcDirection = dc->ArcDirection;
372 newdc->xformWorld2Wnd = dc->xformWorld2Wnd;
373 newdc->xformWorld2Vport = dc->xformWorld2Vport;
374 newdc->xformVport2World = dc->xformVport2World;
375 newdc->vport2WorldValid = dc->vport2WorldValid;
376 newdc->wndOrgX = dc->wndOrgX;
377 newdc->wndOrgY = dc->wndOrgY;
378 newdc->wndExtX = dc->wndExtX;
379 newdc->wndExtY = dc->wndExtY;
380 newdc->vportOrgX = dc->vportOrgX;
381 newdc->vportOrgY = dc->vportOrgY;
382 newdc->vportExtX = dc->vportExtX;
383 newdc->vportExtY = dc->vportExtY;
384 newdc->BoundsRect = dc->BoundsRect;
385 newdc->gdiFont = dc->gdiFont;
387 newdc->thread = GetCurrentThreadId();
388 newdc->refcount = 1;
389 newdc->saveLevel = 0;
390 newdc->saved_dc = 0;
392 PATH_InitGdiPath( &newdc->path );
394 newdc->pAbortProc = NULL;
395 newdc->hookThunk = NULL;
396 newdc->hookProc = 0;
397 newdc->saved_visrgn = NULL;
399 if (!(newdc->hSelf = alloc_gdi_handle( &newdc->header, dc->header.type, &dc_funcs )))
401 HeapFree( GetProcessHeap(), 0, newdc );
402 release_dc_ptr( dc );
403 return 0;
405 handle = newdc->hSelf;
406 TRACE("(%p): returning %p\n", hdc, handle );
408 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
410 newdc->hVisRgn = 0;
411 newdc->hClipRgn = 0;
412 newdc->hMetaRgn = 0;
413 newdc->hMetaClipRgn = 0;
414 if (dc->hClipRgn)
416 newdc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
417 CombineRgn( newdc->hClipRgn, dc->hClipRgn, 0, RGN_COPY );
419 if (dc->hMetaRgn)
421 newdc->hMetaRgn = CreateRectRgn( 0, 0, 0, 0 );
422 CombineRgn( newdc->hMetaRgn, dc->hMetaRgn, 0, RGN_COPY );
424 /* don't bother recomputing hMetaClipRgn, we'll do that in SetDCState */
426 release_dc_ptr( newdc );
427 release_dc_ptr( dc );
428 return handle;
432 /***********************************************************************
433 * set_dc_state (Not a Windows API)
435 void set_dc_state( HDC hdc, HDC hdcs )
437 DC *dc, *dcs;
439 if (!(dc = get_dc_ptr( hdc ))) return;
440 if (!(dcs = get_dc_ptr( hdcs )))
442 release_dc_ptr( dc );
443 return;
445 if (!(dcs->flags & DC_SAVED))
447 release_dc_ptr( dc );
448 release_dc_ptr( dcs );
449 return;
451 TRACE("%p %p\n", hdc, hdcs );
453 update_dc( dc );
454 dc->flags = dcs->flags & ~DC_SAVED;
455 dc->layout = dcs->layout;
456 dc->hDevice = dcs->hDevice;
457 dc->ROPmode = dcs->ROPmode;
458 dc->polyFillMode = dcs->polyFillMode;
459 dc->stretchBltMode = dcs->stretchBltMode;
460 dc->relAbsMode = dcs->relAbsMode;
461 dc->backgroundMode = dcs->backgroundMode;
462 dc->backgroundColor = dcs->backgroundColor;
463 dc->textColor = dcs->textColor;
464 dc->dcBrushColor = dcs->dcBrushColor;
465 dc->dcPenColor = dcs->dcPenColor;
466 dc->brushOrgX = dcs->brushOrgX;
467 dc->brushOrgY = dcs->brushOrgY;
468 dc->textAlign = dcs->textAlign;
469 dc->charExtra = dcs->charExtra;
470 dc->breakExtra = dcs->breakExtra;
471 dc->breakRem = dcs->breakRem;
472 dc->MapMode = dcs->MapMode;
473 dc->GraphicsMode = dcs->GraphicsMode;
474 dc->CursPosX = dcs->CursPosX;
475 dc->CursPosY = dcs->CursPosY;
476 dc->ArcDirection = dcs->ArcDirection;
477 dc->xformWorld2Wnd = dcs->xformWorld2Wnd;
478 dc->xformWorld2Vport = dcs->xformWorld2Vport;
479 dc->xformVport2World = dcs->xformVport2World;
480 dc->vport2WorldValid = dcs->vport2WorldValid;
481 dc->BoundsRect = dcs->BoundsRect;
483 dc->wndOrgX = dcs->wndOrgX;
484 dc->wndOrgY = dcs->wndOrgY;
485 dc->wndExtX = dcs->wndExtX;
486 dc->wndExtY = dcs->wndExtY;
487 dc->vportOrgX = dcs->vportOrgX;
488 dc->vportOrgY = dcs->vportOrgY;
489 dc->vportExtX = dcs->vportExtX;
490 dc->vportExtY = dcs->vportExtY;
492 if (dcs->hClipRgn)
494 if (!dc->hClipRgn) dc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
495 CombineRgn( dc->hClipRgn, dcs->hClipRgn, 0, RGN_COPY );
497 else
499 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
500 dc->hClipRgn = 0;
502 if (dcs->hMetaRgn)
504 if (!dc->hMetaRgn) dc->hMetaRgn = CreateRectRgn( 0, 0, 0, 0 );
505 CombineRgn( dc->hMetaRgn, dcs->hMetaRgn, 0, RGN_COPY );
507 else
509 if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
510 dc->hMetaRgn = 0;
512 CLIPPING_UpdateGCRegion( dc );
514 SelectObject( hdc, dcs->hBitmap );
515 SelectObject( hdc, dcs->hBrush );
516 SelectObject( hdc, dcs->hFont );
517 SelectObject( hdc, dcs->hPen );
518 SetBkColor( hdc, dcs->backgroundColor);
519 SetTextColor( hdc, dcs->textColor);
520 GDISelectPalette( hdc, dcs->hPalette, FALSE );
521 release_dc_ptr( dc );
522 release_dc_ptr( dcs );
526 /***********************************************************************
527 * SaveDC (GDI32.@)
529 INT WINAPI SaveDC( HDC hdc )
531 HDC hdcs;
532 DC * dc, * dcs;
533 INT ret;
535 dc = get_dc_ptr( hdc );
536 if (!dc) return 0;
538 if(dc->funcs->pSaveDC)
540 ret = dc->funcs->pSaveDC( dc->physDev );
541 if(ret)
542 ret = ++dc->saveLevel;
543 release_dc_ptr( dc );
544 return ret;
547 if (!(hdcs = get_dc_state( hdc )))
549 release_dc_ptr( dc );
550 return 0;
552 dcs = get_dc_ptr( hdcs );
554 /* Copy path. The reason why path saving / restoring is in SaveDC/
555 * RestoreDC and not in GetDCState/SetDCState is that the ...DCState
556 * functions are only in Win16 (which doesn't have paths) and that
557 * SetDCState doesn't allow us to signal an error (which can happen
558 * when copying paths).
560 if (!PATH_AssignGdiPath( &dcs->path, &dc->path ))
562 release_dc_ptr( dc );
563 release_dc_ptr( dcs );
564 DeleteDC( hdcs );
565 return 0;
568 dcs->saved_dc = dc->saved_dc;
569 dc->saved_dc = hdcs;
570 TRACE("(%p): returning %d\n", hdc, dc->saveLevel+1 );
571 ret = ++dc->saveLevel;
572 release_dc_ptr( dcs );
573 release_dc_ptr( dc );
574 return ret;
578 /***********************************************************************
579 * RestoreDC (GDI32.@)
581 BOOL WINAPI RestoreDC( HDC hdc, INT level )
583 DC * dc, * dcs;
584 BOOL success;
586 TRACE("%p %d\n", hdc, level );
587 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
589 if(abs(level) > dc->saveLevel || level == 0)
591 release_dc_ptr( dc );
592 return FALSE;
595 update_dc( dc );
597 if(dc->funcs->pRestoreDC)
599 success = dc->funcs->pRestoreDC( dc->physDev, level );
600 if(level < 0) level = dc->saveLevel + level + 1;
601 if(success)
602 dc->saveLevel = level - 1;
603 release_dc_ptr( dc );
604 return success;
607 if (level < 0) level = dc->saveLevel + level + 1;
608 success=TRUE;
609 while (dc->saveLevel >= level)
611 HDC hdcs = dc->saved_dc;
612 if (!(dcs = get_dc_ptr( hdcs )))
614 success = FALSE;
615 break;
617 dc->saved_dc = dcs->saved_dc;
618 dcs->saved_dc = 0;
619 if (--dc->saveLevel < level)
621 set_dc_state( hdc, hdcs );
622 if (!PATH_AssignGdiPath( &dc->path, &dcs->path ))
623 /* FIXME: This might not be quite right, since we're
624 * returning FALSE but still destroying the saved DC state */
625 success=FALSE;
627 release_dc_ptr( dcs );
628 DeleteDC( hdcs );
630 release_dc_ptr( dc );
631 return success;
635 /***********************************************************************
636 * CreateDCW (GDI32.@)
638 HDC WINAPI CreateDCW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
639 const DEVMODEW *initData )
641 HDC hdc;
642 DC * dc;
643 const DC_FUNCTIONS *funcs;
644 WCHAR buf[300];
646 GDI_CheckNotLock();
648 if (!device || !DRIVER_GetDriverName( device, buf, 300 ))
650 if (!driver)
652 ERR( "no device found for %s\n", debugstr_w(device) );
653 return 0;
655 strcpyW(buf, driver);
658 if (!(funcs = DRIVER_load_driver( buf )))
660 ERR( "no driver found for %s\n", debugstr_w(buf) );
661 return 0;
663 if (!(dc = alloc_dc_ptr( funcs, OBJ_DC ))) goto error;
664 hdc = dc->hSelf;
666 dc->hBitmap = GDI_inc_ref_count( GetStockObject( DEFAULT_BITMAP ));
667 if (!(dc->hVisRgn = CreateRectRgn( 0, 0, 1, 1 ))) goto error;
669 TRACE("(driver=%s, device=%s, output=%s): returning %p\n",
670 debugstr_w(driver), debugstr_w(device), debugstr_w(output), dc->hSelf );
672 if (dc->funcs->pCreateDC &&
673 !dc->funcs->pCreateDC( hdc, &dc->physDev, buf, device, output, initData ))
675 WARN("creation aborted by device\n" );
676 goto error;
679 SetRectRgn( dc->hVisRgn, 0, 0,
680 GetDeviceCaps( hdc, DESKTOPHORZRES ), GetDeviceCaps( hdc, DESKTOPVERTRES ) );
682 DC_InitDC( dc );
683 release_dc_ptr( dc );
684 return hdc;
686 error:
687 if (dc && dc->hVisRgn) DeleteObject( dc->hVisRgn );
688 if (dc) free_dc_ptr( dc );
689 DRIVER_release_driver( funcs );
690 return 0;
694 /***********************************************************************
695 * CreateDCA (GDI32.@)
697 HDC WINAPI CreateDCA( LPCSTR driver, LPCSTR device, LPCSTR output,
698 const DEVMODEA *initData )
700 UNICODE_STRING driverW, deviceW, outputW;
701 DEVMODEW *initDataW;
702 HDC ret;
704 if (driver) RtlCreateUnicodeStringFromAsciiz(&driverW, driver);
705 else driverW.Buffer = NULL;
707 if (device) RtlCreateUnicodeStringFromAsciiz(&deviceW, device);
708 else deviceW.Buffer = NULL;
710 if (output) RtlCreateUnicodeStringFromAsciiz(&outputW, output);
711 else outputW.Buffer = NULL;
713 initDataW = NULL;
714 if (initData)
716 /* don't convert initData for DISPLAY driver, it's not used */
717 if (!driverW.Buffer || strcmpiW( driverW.Buffer, displayW ))
718 initDataW = GdiConvertToDevmodeW(initData);
721 ret = CreateDCW( driverW.Buffer, deviceW.Buffer, outputW.Buffer, initDataW );
723 RtlFreeUnicodeString(&driverW);
724 RtlFreeUnicodeString(&deviceW);
725 RtlFreeUnicodeString(&outputW);
726 HeapFree(GetProcessHeap(), 0, initDataW);
727 return ret;
731 /***********************************************************************
732 * CreateICA (GDI32.@)
734 HDC WINAPI CreateICA( LPCSTR driver, LPCSTR device, LPCSTR output,
735 const DEVMODEA* initData )
737 /* Nothing special yet for ICs */
738 return CreateDCA( driver, device, output, initData );
742 /***********************************************************************
743 * CreateICW (GDI32.@)
745 HDC WINAPI CreateICW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
746 const DEVMODEW* initData )
748 /* Nothing special yet for ICs */
749 return CreateDCW( driver, device, output, initData );
753 /***********************************************************************
754 * CreateCompatibleDC (GDI32.@)
756 HDC WINAPI CreateCompatibleDC( HDC hdc )
758 DC *dc, *origDC;
759 HDC ret;
760 const DC_FUNCTIONS *funcs = NULL;
761 PHYSDEV physDev = NULL;
763 GDI_CheckNotLock();
765 if ((origDC = get_dc_ptr( hdc )))
767 if (GetObjectType( hdc ) == OBJ_DC)
769 funcs = origDC->funcs;
770 physDev = origDC->physDev;
772 release_dc_ptr( origDC );
773 if (funcs) funcs = DRIVER_get_driver( funcs );
775 else if (hdc) return 0;
777 if (!funcs && !(funcs = DRIVER_load_driver( displayW ))) return 0;
779 if (!(dc = alloc_dc_ptr( funcs, OBJ_MEMDC ))) goto error;
781 TRACE("(%p): returning %p\n", hdc, dc->hSelf );
783 dc->hBitmap = GDI_inc_ref_count( GetStockObject( DEFAULT_BITMAP ));
784 if (!(dc->hVisRgn = CreateRectRgn( 0, 0, 1, 1 ))) goto error; /* default bitmap is 1x1 */
786 /* Copy the driver-specific physical device info into
787 * the new DC. The driver may use this read-only info
788 * while creating the compatible DC below. */
789 dc->physDev = physDev;
790 ret = dc->hSelf;
792 if (dc->funcs->pCreateDC &&
793 !dc->funcs->pCreateDC( dc->hSelf, &dc->physDev, NULL, NULL, NULL, NULL ))
795 WARN("creation aborted by device\n");
796 goto error;
799 DC_InitDC( dc );
800 release_dc_ptr( dc );
801 return ret;
803 error:
804 if (dc && dc->hVisRgn) DeleteObject( dc->hVisRgn );
805 if (dc) free_dc_ptr( dc );
806 DRIVER_release_driver( funcs );
807 return 0;
811 /***********************************************************************
812 * DeleteDC (GDI32.@)
814 BOOL WINAPI DeleteDC( HDC hdc )
816 const DC_FUNCTIONS *funcs = NULL;
817 DC * dc;
819 TRACE("%p\n", hdc );
821 GDI_CheckNotLock();
823 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
824 if (dc->refcount != 1)
826 FIXME( "not deleting busy DC %p refcount %u\n", dc->hSelf, dc->refcount );
827 release_dc_ptr( dc );
828 return FALSE;
831 /* Call hook procedure to check whether is it OK to delete this DC */
832 if (dc->hookThunk && !dc->hookThunk( hdc, DCHC_DELETEDC, dc->dwHookData, 0 ))
834 release_dc_ptr( dc );
835 return FALSE;
838 while (dc->saveLevel)
840 DC * dcs;
841 HDC hdcs = dc->saved_dc;
842 if (!(dcs = get_dc_ptr( hdcs ))) break;
843 dc->saved_dc = dcs->saved_dc;
844 dc->saveLevel--;
845 if (dcs->hClipRgn) DeleteObject( dcs->hClipRgn );
846 if (dcs->hMetaRgn) DeleteObject( dcs->hMetaRgn );
847 if (dcs->hMetaClipRgn) DeleteObject( dcs->hMetaClipRgn );
848 if (dcs->hVisRgn) DeleteObject( dcs->hVisRgn );
849 PATH_DestroyGdiPath(&dcs->path);
850 free_dc_ptr( dcs );
853 if (!(dc->flags & DC_SAVED))
855 SelectObject( hdc, GetStockObject(BLACK_PEN) );
856 SelectObject( hdc, GetStockObject(WHITE_BRUSH) );
857 SelectObject( hdc, GetStockObject(SYSTEM_FONT) );
858 SelectObject( hdc, GetStockObject(DEFAULT_BITMAP) );
859 funcs = dc->funcs;
860 if (dc->funcs->pDeleteDC) dc->funcs->pDeleteDC(dc->physDev);
861 dc->physDev = NULL;
864 while (dc->saved_visrgn)
866 struct saved_visrgn *next = dc->saved_visrgn->next;
867 DeleteObject( dc->saved_visrgn->hrgn );
868 HeapFree( GetProcessHeap(), 0, dc->saved_visrgn );
869 dc->saved_visrgn = next;
871 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
872 if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
873 if (dc->hMetaClipRgn) DeleteObject( dc->hMetaClipRgn );
874 if (dc->hVisRgn) DeleteObject( dc->hVisRgn );
875 PATH_DestroyGdiPath(&dc->path);
877 free_dc_ptr( dc );
878 if (funcs) DRIVER_release_driver( funcs ); /* do that after releasing the GDI lock */
879 return TRUE;
883 /***********************************************************************
884 * ResetDCW (GDI32.@)
886 HDC WINAPI ResetDCW( HDC hdc, const DEVMODEW *devmode )
888 DC *dc;
889 HDC ret = hdc;
891 if ((dc = get_dc_ptr( hdc )))
893 if (dc->funcs->pResetDC) ret = dc->funcs->pResetDC( dc->physDev, devmode );
894 release_dc_ptr( dc );
896 return ret;
900 /***********************************************************************
901 * ResetDCA (GDI32.@)
903 HDC WINAPI ResetDCA( HDC hdc, const DEVMODEA *devmode )
905 DEVMODEW *devmodeW;
906 HDC ret;
908 if (devmode) devmodeW = GdiConvertToDevmodeW(devmode);
909 else devmodeW = NULL;
911 ret = ResetDCW(hdc, devmodeW);
913 HeapFree(GetProcessHeap(), 0, devmodeW);
914 return ret;
918 /***********************************************************************
919 * GetDeviceCaps (GDI32.@)
921 INT WINAPI GetDeviceCaps( HDC hdc, INT cap )
923 DC *dc;
924 INT ret = 0;
926 if ((dc = get_dc_ptr( hdc )))
928 if (dc->funcs->pGetDeviceCaps) ret = dc->funcs->pGetDeviceCaps( dc->physDev, cap );
929 else switch(cap) /* return meaningful values for some entries */
931 case HORZRES: ret = 640; break;
932 case VERTRES: ret = 480; break;
933 case BITSPIXEL: ret = 1; break;
934 case PLANES: ret = 1; break;
935 case NUMCOLORS: ret = 2; break;
936 case ASPECTX: ret = 36; break;
937 case ASPECTY: ret = 36; break;
938 case ASPECTXY: ret = 51; break;
939 case LOGPIXELSX: ret = 72; break;
940 case LOGPIXELSY: ret = 72; break;
941 case SIZEPALETTE: ret = 2; break;
943 release_dc_ptr( dc );
945 return ret;
949 /***********************************************************************
950 * GetBkColor (GDI32.@)
952 COLORREF WINAPI GetBkColor( HDC hdc )
954 COLORREF ret = 0;
955 DC * dc = get_dc_ptr( hdc );
956 if (dc)
958 ret = dc->backgroundColor;
959 release_dc_ptr( dc );
961 return ret;
965 /***********************************************************************
966 * SetBkColor (GDI32.@)
968 COLORREF WINAPI SetBkColor( HDC hdc, COLORREF color )
970 COLORREF oldColor;
971 DC * dc = get_dc_ptr( hdc );
973 TRACE("hdc=%p color=0x%08x\n", hdc, color);
975 if (!dc) return CLR_INVALID;
976 oldColor = dc->backgroundColor;
977 if (dc->funcs->pSetBkColor)
979 color = dc->funcs->pSetBkColor(dc->physDev, color);
980 if (color == CLR_INVALID) /* don't change it */
982 color = oldColor;
983 oldColor = CLR_INVALID;
986 dc->backgroundColor = color;
987 release_dc_ptr( dc );
988 return oldColor;
992 /***********************************************************************
993 * GetTextColor (GDI32.@)
995 COLORREF WINAPI GetTextColor( HDC hdc )
997 COLORREF ret = 0;
998 DC * dc = get_dc_ptr( hdc );
999 if (dc)
1001 ret = dc->textColor;
1002 release_dc_ptr( dc );
1004 return ret;
1008 /***********************************************************************
1009 * SetTextColor (GDI32.@)
1011 COLORREF WINAPI SetTextColor( HDC hdc, COLORREF color )
1013 COLORREF oldColor;
1014 DC * dc = get_dc_ptr( hdc );
1016 TRACE(" hdc=%p color=0x%08x\n", hdc, color);
1018 if (!dc) return CLR_INVALID;
1019 oldColor = dc->textColor;
1020 if (dc->funcs->pSetTextColor)
1022 color = dc->funcs->pSetTextColor(dc->physDev, color);
1023 if (color == CLR_INVALID) /* don't change it */
1025 color = oldColor;
1026 oldColor = CLR_INVALID;
1029 dc->textColor = color;
1030 release_dc_ptr( dc );
1031 return oldColor;
1035 /***********************************************************************
1036 * GetTextAlign (GDI32.@)
1038 UINT WINAPI GetTextAlign( HDC hdc )
1040 UINT ret = 0;
1041 DC * dc = get_dc_ptr( hdc );
1042 if (dc)
1044 ret = dc->textAlign;
1045 release_dc_ptr( dc );
1047 return ret;
1051 /***********************************************************************
1052 * SetTextAlign (GDI32.@)
1054 UINT WINAPI SetTextAlign( HDC hdc, UINT align )
1056 UINT ret;
1057 DC *dc = get_dc_ptr( hdc );
1059 TRACE("hdc=%p align=%d\n", hdc, align);
1061 if (!dc) return 0x0;
1062 ret = dc->textAlign;
1063 if (dc->funcs->pSetTextAlign)
1064 if (!dc->funcs->pSetTextAlign(dc->physDev, align))
1065 ret = GDI_ERROR;
1066 if (ret != GDI_ERROR)
1067 dc->textAlign = align;
1068 release_dc_ptr( dc );
1069 return ret;
1072 /***********************************************************************
1073 * GetDCOrgEx (GDI32.@)
1075 BOOL WINAPI GetDCOrgEx( HDC hDC, LPPOINT lpp )
1077 DC * dc;
1079 if (!lpp) return FALSE;
1080 if (!(dc = get_dc_ptr( hDC ))) return FALSE;
1082 lpp->x = lpp->y = 0;
1083 if (dc->funcs->pGetDCOrgEx) dc->funcs->pGetDCOrgEx( dc->physDev, lpp );
1084 release_dc_ptr( dc );
1085 return TRUE;
1089 /***********************************************************************
1090 * GetGraphicsMode (GDI32.@)
1092 INT WINAPI GetGraphicsMode( HDC hdc )
1094 INT ret = 0;
1095 DC * dc = get_dc_ptr( hdc );
1096 if (dc)
1098 ret = dc->GraphicsMode;
1099 release_dc_ptr( dc );
1101 return ret;
1105 /***********************************************************************
1106 * SetGraphicsMode (GDI32.@)
1108 INT WINAPI SetGraphicsMode( HDC hdc, INT mode )
1110 INT ret = 0;
1111 DC *dc = get_dc_ptr( hdc );
1113 /* One would think that setting the graphics mode to GM_COMPATIBLE
1114 * would also reset the world transformation matrix to the unity
1115 * matrix. However, in Windows, this is not the case. This doesn't
1116 * make a lot of sense to me, but that's the way it is.
1118 if (!dc) return 0;
1119 if ((mode > 0) && (mode <= GM_LAST))
1121 ret = dc->GraphicsMode;
1122 dc->GraphicsMode = mode;
1124 release_dc_ptr( dc );
1125 return ret;
1129 /***********************************************************************
1130 * GetArcDirection (GDI32.@)
1132 INT WINAPI GetArcDirection( HDC hdc )
1134 INT ret = 0;
1135 DC * dc = get_dc_ptr( hdc );
1136 if (dc)
1138 ret = dc->ArcDirection;
1139 release_dc_ptr( dc );
1141 return ret;
1145 /***********************************************************************
1146 * SetArcDirection (GDI32.@)
1148 INT WINAPI SetArcDirection( HDC hdc, INT nDirection )
1150 DC * dc;
1151 INT nOldDirection = 0;
1153 if (nDirection!=AD_COUNTERCLOCKWISE && nDirection!=AD_CLOCKWISE)
1155 SetLastError(ERROR_INVALID_PARAMETER);
1156 return 0;
1159 if ((dc = get_dc_ptr( hdc )))
1161 if (dc->funcs->pSetArcDirection)
1163 dc->funcs->pSetArcDirection(dc->physDev, nDirection);
1165 nOldDirection = dc->ArcDirection;
1166 dc->ArcDirection = nDirection;
1167 release_dc_ptr( dc );
1169 return nOldDirection;
1173 /***********************************************************************
1174 * GetWorldTransform (GDI32.@)
1176 BOOL WINAPI GetWorldTransform( HDC hdc, LPXFORM xform )
1178 DC * dc;
1179 if (!xform) return FALSE;
1180 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
1181 *xform = dc->xformWorld2Wnd;
1182 release_dc_ptr( dc );
1183 return TRUE;
1187 /***********************************************************************
1188 * GetTransform (GDI32.@)
1190 BOOL WINAPI GetTransform( HDC hdc, DWORD unknown, LPXFORM xform )
1192 if (unknown == 0x0203) return GetWorldTransform( hdc, xform );
1193 FIXME("stub: don't know what to do for code %x\n", unknown );
1194 return FALSE;
1198 /***********************************************************************
1199 * SetWorldTransform (GDI32.@)
1201 BOOL WINAPI SetWorldTransform( HDC hdc, const XFORM *xform )
1203 BOOL ret = FALSE;
1204 DC *dc;
1206 if (!xform) return FALSE;
1208 dc = get_dc_ptr( hdc );
1209 if (!dc) return FALSE;
1211 /* Check that graphics mode is GM_ADVANCED */
1212 if (dc->GraphicsMode!=GM_ADVANCED) goto done;
1214 TRACE("eM11 %f eM12 %f eM21 %f eM22 %f eDx %f eDy %f\n",
1215 xform->eM11, xform->eM12, xform->eM21, xform->eM22, xform->eDx, xform->eDy);
1217 /* The transform must conform to (eM11 * eM22 != eM12 * eM21) requirement */
1218 if (xform->eM11 * xform->eM22 == xform->eM12 * xform->eM21) goto done;
1220 if (dc->funcs->pSetWorldTransform)
1222 ret = dc->funcs->pSetWorldTransform(dc->physDev, xform);
1223 if (!ret) goto done;
1226 dc->xformWorld2Wnd = *xform;
1227 DC_UpdateXforms( dc );
1228 ret = TRUE;
1229 done:
1230 release_dc_ptr( dc );
1231 return ret;
1235 /****************************************************************************
1236 * ModifyWorldTransform [GDI32.@]
1237 * Modifies the world transformation for a device context.
1239 * PARAMS
1240 * hdc [I] Handle to device context
1241 * xform [I] XFORM structure that will be used to modify the world
1242 * transformation
1243 * iMode [I] Specifies in what way to modify the world transformation
1244 * Possible values:
1245 * MWT_IDENTITY
1246 * Resets the world transformation to the identity matrix.
1247 * The parameter xform is ignored.
1248 * MWT_LEFTMULTIPLY
1249 * Multiplies xform into the world transformation matrix from
1250 * the left.
1251 * MWT_RIGHTMULTIPLY
1252 * Multiplies xform into the world transformation matrix from
1253 * the right.
1255 * RETURNS
1256 * Success: TRUE.
1257 * Failure: FALSE. Use GetLastError() to determine the cause.
1259 BOOL WINAPI ModifyWorldTransform( HDC hdc, const XFORM *xform,
1260 DWORD iMode )
1262 BOOL ret = FALSE;
1263 DC *dc = get_dc_ptr( hdc );
1265 /* Check for illegal parameters */
1266 if (!dc) return FALSE;
1267 if (!xform && iMode != MWT_IDENTITY) goto done;
1269 /* Check that graphics mode is GM_ADVANCED */
1270 if (dc->GraphicsMode!=GM_ADVANCED) goto done;
1272 if (dc->funcs->pModifyWorldTransform)
1274 ret = dc->funcs->pModifyWorldTransform(dc->physDev, xform, iMode);
1275 if (!ret) goto done;
1278 switch (iMode)
1280 case MWT_IDENTITY:
1281 dc->xformWorld2Wnd.eM11 = 1.0f;
1282 dc->xformWorld2Wnd.eM12 = 0.0f;
1283 dc->xformWorld2Wnd.eM21 = 0.0f;
1284 dc->xformWorld2Wnd.eM22 = 1.0f;
1285 dc->xformWorld2Wnd.eDx = 0.0f;
1286 dc->xformWorld2Wnd.eDy = 0.0f;
1287 break;
1288 case MWT_LEFTMULTIPLY:
1289 CombineTransform( &dc->xformWorld2Wnd, xform,
1290 &dc->xformWorld2Wnd );
1291 break;
1292 case MWT_RIGHTMULTIPLY:
1293 CombineTransform( &dc->xformWorld2Wnd, &dc->xformWorld2Wnd,
1294 xform );
1295 break;
1296 default:
1297 goto done;
1300 DC_UpdateXforms( dc );
1301 ret = TRUE;
1302 done:
1303 release_dc_ptr( dc );
1304 return ret;
1308 /****************************************************************************
1309 * CombineTransform [GDI32.@]
1310 * Combines two transformation matrices.
1312 * PARAMS
1313 * xformResult [O] Stores the result of combining the two matrices
1314 * xform1 [I] Specifies the first matrix to apply
1315 * xform2 [I] Specifies the second matrix to apply
1317 * REMARKS
1318 * The same matrix can be passed in for more than one of the parameters.
1320 * RETURNS
1321 * Success: TRUE.
1322 * Failure: FALSE. Use GetLastError() to determine the cause.
1324 BOOL WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
1325 const XFORM *xform2 )
1327 XFORM xformTemp;
1329 /* Check for illegal parameters */
1330 if (!xformResult || !xform1 || !xform2)
1331 return FALSE;
1333 /* Create the result in a temporary XFORM, since xformResult may be
1334 * equal to xform1 or xform2 */
1335 xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
1336 xform1->eM12 * xform2->eM21;
1337 xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
1338 xform1->eM12 * xform2->eM22;
1339 xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
1340 xform1->eM22 * xform2->eM21;
1341 xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
1342 xform1->eM22 * xform2->eM22;
1343 xformTemp.eDx = xform1->eDx * xform2->eM11 +
1344 xform1->eDy * xform2->eM21 +
1345 xform2->eDx;
1346 xformTemp.eDy = xform1->eDx * xform2->eM12 +
1347 xform1->eDy * xform2->eM22 +
1348 xform2->eDy;
1350 /* Copy the result to xformResult */
1351 *xformResult = xformTemp;
1353 return TRUE;
1357 /***********************************************************************
1358 * SetDCHook (GDI32.@)
1360 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1362 BOOL WINAPI SetDCHook( HDC hdc, DCHOOKPROC hookProc, DWORD_PTR dwHookData )
1364 DC *dc = get_dc_ptr( hdc );
1366 if (!dc) return FALSE;
1368 if (!(dc->flags & DC_SAVED))
1370 dc->dwHookData = dwHookData;
1371 dc->hookThunk = hookProc;
1373 release_dc_ptr( dc );
1374 return TRUE;
1378 /***********************************************************************
1379 * GetDCHook (GDI32.@)
1381 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1383 DWORD_PTR WINAPI GetDCHook( HDC hdc, DCHOOKPROC *proc )
1385 DC *dc = get_dc_ptr( hdc );
1386 DWORD_PTR ret;
1388 if (!dc) return 0;
1389 if (proc) *proc = dc->hookThunk;
1390 ret = dc->dwHookData;
1391 release_dc_ptr( dc );
1392 return ret;
1396 /* relay function to call the 16-bit DC hook proc */
1397 static BOOL WINAPI call_dc_hook16( HDC hdc, WORD code, DWORD_PTR data, LPARAM lParam )
1399 WORD args[6];
1400 DWORD ret = 0;
1401 DC *dc = get_dc_ptr( hdc );
1403 if (!dc) return FALSE;
1404 if (dc->hookProc)
1406 args[5] = HDC_16(hdc);
1407 args[4] = code;
1408 args[3] = HIWORD(data);
1409 args[2] = LOWORD(data);
1410 args[1] = HIWORD(lParam);
1411 args[0] = LOWORD(lParam);
1412 WOWCallback16Ex( (DWORD)dc->hookProc, WCB16_PASCAL, sizeof(args), args, &ret );
1414 release_dc_ptr( dc );
1415 return LOWORD(ret);
1418 /***********************************************************************
1419 * SetDCHook (GDI.190)
1421 BOOL16 WINAPI SetDCHook16( HDC16 hdc16, FARPROC16 hookProc, DWORD dwHookData )
1423 DC *dc = get_dc_ptr( HDC_32(hdc16) );
1425 if (!dc) return FALSE;
1426 if (!(dc->flags & DC_SAVED))
1428 dc->dwHookData = dwHookData;
1429 dc->hookThunk = call_dc_hook16;
1430 dc->hookProc = hookProc;
1432 release_dc_ptr( dc );
1433 return TRUE;
1437 /***********************************************************************
1438 * GetDCHook (GDI.191)
1440 DWORD WINAPI GetDCHook16( HDC16 hdc16, FARPROC16 *phookProc )
1442 HDC hdc = HDC_32( hdc16 );
1443 DC *dc = get_dc_ptr( hdc );
1444 DWORD ret;
1446 if (!dc) return 0;
1447 *phookProc = dc->hookProc;
1448 ret = dc->dwHookData;
1449 release_dc_ptr( dc );
1450 return ret;
1454 /***********************************************************************
1455 * SetHookFlags (GDI32.@)
1457 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1459 WORD WINAPI SetHookFlags( HDC hdc, WORD flags )
1461 DC *dc = get_dc_obj( hdc ); /* not get_dc_ptr, this needs to work from any thread */
1462 LONG ret = 0;
1464 if (!dc) return 0;
1466 /* "Undocumented Windows" info is slightly confusing. */
1468 TRACE("hDC %p, flags %04x\n",hdc,flags);
1470 if (flags & DCHF_INVALIDATEVISRGN)
1471 ret = InterlockedExchange( &dc->dirty, 1 );
1472 else if (flags & DCHF_VALIDATEVISRGN || !flags)
1473 ret = InterlockedExchange( &dc->dirty, 0 );
1475 GDI_ReleaseObj( dc );
1476 return ret;
1479 /***********************************************************************
1480 * SetICMMode (GDI32.@)
1482 INT WINAPI SetICMMode(HDC hdc, INT iEnableICM)
1484 /*FIXME: Assume that ICM is always off, and cannot be turned on */
1485 if (iEnableICM == ICM_OFF) return ICM_OFF;
1486 if (iEnableICM == ICM_ON) return 0;
1487 if (iEnableICM == ICM_QUERY) return ICM_OFF;
1488 return 0;
1491 /***********************************************************************
1492 * GetDeviceGammaRamp (GDI32.@)
1494 BOOL WINAPI GetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1496 BOOL ret = FALSE;
1497 DC *dc = get_dc_ptr( hDC );
1499 if( dc )
1501 if (dc->funcs->pGetDeviceGammaRamp)
1502 ret = dc->funcs->pGetDeviceGammaRamp(dc->physDev, ptr);
1503 release_dc_ptr( dc );
1505 return ret;
1508 /***********************************************************************
1509 * SetDeviceGammaRamp (GDI32.@)
1511 BOOL WINAPI SetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1513 BOOL ret = FALSE;
1514 DC *dc = get_dc_ptr( hDC );
1516 if( dc )
1518 if (dc->funcs->pSetDeviceGammaRamp)
1519 ret = dc->funcs->pSetDeviceGammaRamp(dc->physDev, ptr);
1520 release_dc_ptr( dc );
1522 return ret;
1525 /***********************************************************************
1526 * GetColorSpace (GDI32.@)
1528 HCOLORSPACE WINAPI GetColorSpace(HDC hdc)
1530 /*FIXME Need to to whatever GetColorSpace actually does */
1531 return 0;
1534 /***********************************************************************
1535 * CreateColorSpaceA (GDI32.@)
1537 HCOLORSPACE WINAPI CreateColorSpaceA( LPLOGCOLORSPACEA lpLogColorSpace )
1539 FIXME( "stub\n" );
1540 return 0;
1543 /***********************************************************************
1544 * CreateColorSpaceW (GDI32.@)
1546 HCOLORSPACE WINAPI CreateColorSpaceW( LPLOGCOLORSPACEW lpLogColorSpace )
1548 FIXME( "stub\n" );
1549 return 0;
1552 /***********************************************************************
1553 * DeleteColorSpace (GDI32.@)
1555 BOOL WINAPI DeleteColorSpace( HCOLORSPACE hColorSpace )
1557 FIXME( "stub\n" );
1559 return TRUE;
1562 /***********************************************************************
1563 * SetColorSpace (GDI32.@)
1565 HCOLORSPACE WINAPI SetColorSpace( HDC hDC, HCOLORSPACE hColorSpace )
1567 FIXME( "stub\n" );
1569 return hColorSpace;
1572 /***********************************************************************
1573 * GetBoundsRect (GDI32.@)
1575 UINT WINAPI GetBoundsRect(HDC hdc, LPRECT rect, UINT flags)
1577 UINT ret;
1578 DC *dc = get_dc_ptr( hdc );
1580 if ( !dc ) return 0;
1582 if (rect) *rect = dc->BoundsRect;
1584 ret = ((dc->flags & DC_BOUNDS_SET) ? DCB_SET : DCB_RESET);
1586 if (flags & DCB_RESET)
1588 dc->BoundsRect.left = 0;
1589 dc->BoundsRect.top = 0;
1590 dc->BoundsRect.right = 0;
1591 dc->BoundsRect.bottom = 0;
1592 dc->flags &= ~DC_BOUNDS_SET;
1594 release_dc_ptr( dc );
1595 return ret;
1599 /***********************************************************************
1600 * SetBoundsRect (GDI32.@)
1602 UINT WINAPI SetBoundsRect(HDC hdc, const RECT* rect, UINT flags)
1604 UINT ret;
1605 DC *dc;
1607 if ((flags & DCB_ENABLE) && (flags & DCB_DISABLE)) return 0;
1608 if (!(dc = get_dc_ptr( hdc ))) return 0;
1610 ret = ((dc->flags & DC_BOUNDS_ENABLE) ? DCB_ENABLE : DCB_DISABLE) |
1611 ((dc->flags & DC_BOUNDS_SET) ? DCB_SET : DCB_RESET);
1613 if (flags & DCB_RESET)
1615 dc->BoundsRect.left = 0;
1616 dc->BoundsRect.top = 0;
1617 dc->BoundsRect.right = 0;
1618 dc->BoundsRect.bottom = 0;
1619 dc->flags &= ~DC_BOUNDS_SET;
1622 if ((flags & DCB_ACCUMULATE) && rect && rect->left < rect->right && rect->top < rect->bottom)
1624 if (dc->flags & DC_BOUNDS_SET)
1626 dc->BoundsRect.left = min( dc->BoundsRect.left, rect->left );
1627 dc->BoundsRect.top = min( dc->BoundsRect.top, rect->top );
1628 dc->BoundsRect.right = max( dc->BoundsRect.right, rect->right );
1629 dc->BoundsRect.bottom = max( dc->BoundsRect.bottom, rect->bottom );
1631 else
1633 dc->BoundsRect = *rect;
1634 dc->flags |= DC_BOUNDS_SET;
1638 if (flags & DCB_ENABLE) dc->flags |= DC_BOUNDS_ENABLE;
1639 if (flags & DCB_DISABLE) dc->flags &= ~DC_BOUNDS_ENABLE;
1641 release_dc_ptr( dc );
1642 return ret;
1646 /***********************************************************************
1647 * GetRelAbs (GDI32.@)
1649 INT WINAPI GetRelAbs( HDC hdc, DWORD dwIgnore )
1651 INT ret = 0;
1652 DC *dc = get_dc_ptr( hdc );
1653 if (dc)
1655 ret = dc->relAbsMode;
1656 release_dc_ptr( dc );
1658 return ret;
1664 /***********************************************************************
1665 * GetBkMode (GDI32.@)
1667 INT WINAPI GetBkMode( HDC hdc )
1669 INT ret = 0;
1670 DC * dc = get_dc_ptr( hdc );
1671 if (dc)
1673 ret = dc->backgroundMode;
1674 release_dc_ptr( dc );
1676 return ret;
1680 /***********************************************************************
1681 * SetBkMode (GDI32.@)
1683 INT WINAPI SetBkMode( HDC hdc, INT mode )
1685 INT ret;
1686 DC *dc;
1687 if ((mode <= 0) || (mode > BKMODE_LAST))
1689 SetLastError(ERROR_INVALID_PARAMETER);
1690 return 0;
1692 if (!(dc = get_dc_ptr( hdc ))) return 0;
1694 ret = dc->backgroundMode;
1695 if (dc->funcs->pSetBkMode)
1696 if (!dc->funcs->pSetBkMode( dc->physDev, mode ))
1697 ret = 0;
1698 if (ret)
1699 dc->backgroundMode = mode;
1700 release_dc_ptr( dc );
1701 return ret;
1705 /***********************************************************************
1706 * GetROP2 (GDI32.@)
1708 INT WINAPI GetROP2( HDC hdc )
1710 INT ret = 0;
1711 DC * dc = get_dc_ptr( hdc );
1712 if (dc)
1714 ret = dc->ROPmode;
1715 release_dc_ptr( dc );
1717 return ret;
1721 /***********************************************************************
1722 * SetROP2 (GDI32.@)
1724 INT WINAPI SetROP2( HDC hdc, INT mode )
1726 INT ret;
1727 DC *dc;
1728 if ((mode < R2_BLACK) || (mode > R2_WHITE))
1730 SetLastError(ERROR_INVALID_PARAMETER);
1731 return 0;
1733 if (!(dc = get_dc_ptr( hdc ))) return 0;
1734 ret = dc->ROPmode;
1735 if (dc->funcs->pSetROP2)
1736 if (!dc->funcs->pSetROP2( dc->physDev, mode ))
1737 ret = 0;
1738 if (ret)
1739 dc->ROPmode = mode;
1740 release_dc_ptr( dc );
1741 return ret;
1745 /***********************************************************************
1746 * SetRelAbs (GDI32.@)
1748 INT WINAPI SetRelAbs( HDC hdc, INT mode )
1750 INT ret;
1751 DC *dc;
1752 if ((mode != ABSOLUTE) && (mode != RELATIVE))
1754 SetLastError(ERROR_INVALID_PARAMETER);
1755 return 0;
1757 if (!(dc = get_dc_ptr( hdc ))) return 0;
1758 if (dc->funcs->pSetRelAbs)
1759 ret = dc->funcs->pSetRelAbs( dc->physDev, mode );
1760 else
1762 ret = dc->relAbsMode;
1763 dc->relAbsMode = mode;
1765 release_dc_ptr( dc );
1766 return ret;
1770 /***********************************************************************
1771 * GetPolyFillMode (GDI32.@)
1773 INT WINAPI GetPolyFillMode( HDC hdc )
1775 INT ret = 0;
1776 DC * dc = get_dc_ptr( hdc );
1777 if (dc)
1779 ret = dc->polyFillMode;
1780 release_dc_ptr( dc );
1782 return ret;
1786 /***********************************************************************
1787 * SetPolyFillMode (GDI32.@)
1789 INT WINAPI SetPolyFillMode( HDC hdc, INT mode )
1791 INT ret;
1792 DC *dc;
1793 if ((mode <= 0) || (mode > POLYFILL_LAST))
1795 SetLastError(ERROR_INVALID_PARAMETER);
1796 return 0;
1798 if (!(dc = get_dc_ptr( hdc ))) return 0;
1799 ret = dc->polyFillMode;
1800 if (dc->funcs->pSetPolyFillMode)
1801 if (!dc->funcs->pSetPolyFillMode( dc->physDev, mode ))
1802 ret = 0;
1803 if (ret)
1804 dc->polyFillMode = mode;
1805 release_dc_ptr( dc );
1806 return ret;
1810 /***********************************************************************
1811 * GetStretchBltMode (GDI32.@)
1813 INT WINAPI GetStretchBltMode( HDC hdc )
1815 INT ret = 0;
1816 DC * dc = get_dc_ptr( hdc );
1817 if (dc)
1819 ret = dc->stretchBltMode;
1820 release_dc_ptr( dc );
1822 return ret;
1826 /***********************************************************************
1827 * SetStretchBltMode (GDI32.@)
1829 INT WINAPI SetStretchBltMode( HDC hdc, INT mode )
1831 INT ret;
1832 DC *dc;
1833 if ((mode <= 0) || (mode > MAXSTRETCHBLTMODE))
1835 SetLastError(ERROR_INVALID_PARAMETER);
1836 return 0;
1838 if (!(dc = get_dc_ptr( hdc ))) return 0;
1839 ret = dc->stretchBltMode;
1840 if (dc->funcs->pSetStretchBltMode)
1841 if (!dc->funcs->pSetStretchBltMode( dc->physDev, mode ))
1842 ret = 0;
1843 if (ret)
1844 dc->stretchBltMode = mode;
1845 release_dc_ptr( dc );
1846 return ret;
1850 /***********************************************************************
1851 * GetMapMode (GDI32.@)
1853 INT WINAPI GetMapMode( HDC hdc )
1855 INT ret = 0;
1856 DC * dc = get_dc_ptr( hdc );
1857 if (dc)
1859 ret = dc->MapMode;
1860 release_dc_ptr( dc );
1862 return ret;
1866 /***********************************************************************
1867 * GetBrushOrgEx (GDI32.@)
1869 BOOL WINAPI GetBrushOrgEx( HDC hdc, LPPOINT pt )
1871 DC * dc = get_dc_ptr( hdc );
1872 if (!dc) return FALSE;
1873 pt->x = dc->brushOrgX;
1874 pt->y = dc->brushOrgY;
1875 release_dc_ptr( dc );
1876 return TRUE;
1880 /***********************************************************************
1881 * GetCurrentPositionEx (GDI32.@)
1883 BOOL WINAPI GetCurrentPositionEx( HDC hdc, LPPOINT pt )
1885 DC * dc = get_dc_ptr( hdc );
1886 if (!dc) return FALSE;
1887 pt->x = dc->CursPosX;
1888 pt->y = dc->CursPosY;
1889 release_dc_ptr( dc );
1890 return TRUE;
1894 /***********************************************************************
1895 * GetViewportExtEx (GDI32.@)
1897 BOOL WINAPI GetViewportExtEx( HDC hdc, LPSIZE size )
1899 DC * dc = get_dc_ptr( hdc );
1900 if (!dc) return FALSE;
1901 size->cx = dc->vportExtX;
1902 size->cy = dc->vportExtY;
1903 release_dc_ptr( dc );
1904 return TRUE;
1908 /***********************************************************************
1909 * GetViewportOrgEx (GDI32.@)
1911 BOOL WINAPI GetViewportOrgEx( HDC hdc, LPPOINT pt )
1913 DC * dc = get_dc_ptr( hdc );
1914 if (!dc) return FALSE;
1915 pt->x = dc->vportOrgX;
1916 pt->y = dc->vportOrgY;
1917 release_dc_ptr( dc );
1918 return TRUE;
1922 /***********************************************************************
1923 * GetWindowExtEx (GDI32.@)
1925 BOOL WINAPI GetWindowExtEx( HDC hdc, LPSIZE size )
1927 DC * dc = get_dc_ptr( hdc );
1928 if (!dc) return FALSE;
1929 size->cx = dc->wndExtX;
1930 size->cy = dc->wndExtY;
1931 release_dc_ptr( dc );
1932 return TRUE;
1936 /***********************************************************************
1937 * GetWindowOrgEx (GDI32.@)
1939 BOOL WINAPI GetWindowOrgEx( HDC hdc, LPPOINT pt )
1941 DC * dc = get_dc_ptr( hdc );
1942 if (!dc) return FALSE;
1943 pt->x = dc->wndOrgX;
1944 pt->y = dc->wndOrgY;
1945 release_dc_ptr( dc );
1946 return TRUE;
1950 /***********************************************************************
1951 * GetLayout (GDI32.@)
1953 * Gets left->right or right->left text layout flags of a dc.
1956 DWORD WINAPI GetLayout(HDC hdc)
1958 DWORD layout = GDI_ERROR;
1960 DC * dc = get_dc_ptr( hdc );
1961 if (dc)
1963 layout = dc->layout;
1964 release_dc_ptr( dc );
1967 TRACE("hdc : %p, layout : %08x\n", hdc, layout);
1969 return layout;
1972 /***********************************************************************
1973 * SetLayout (GDI32.@)
1975 * Sets left->right or right->left text layout flags of a dc.
1978 DWORD WINAPI SetLayout(HDC hdc, DWORD layout)
1980 DWORD oldlayout = GDI_ERROR;
1982 DC * dc = get_dc_ptr( hdc );
1983 if (dc)
1985 oldlayout = dc->layout;
1986 dc->layout = layout;
1987 release_dc_ptr( dc );
1990 TRACE("hdc : %p, old layout : %08x, new layout : %08x\n", hdc, oldlayout, layout);
1992 return oldlayout;
1995 /***********************************************************************
1996 * GetDCBrushColor (GDI32.@)
1998 * Retrieves the current brush color for the specified device
1999 * context (DC).
2002 COLORREF WINAPI GetDCBrushColor(HDC hdc)
2004 DC *dc;
2005 COLORREF dcBrushColor = CLR_INVALID;
2007 TRACE("hdc(%p)\n", hdc);
2009 dc = get_dc_ptr( hdc );
2010 if (dc)
2012 dcBrushColor = dc->dcBrushColor;
2013 release_dc_ptr( dc );
2016 return dcBrushColor;
2019 /***********************************************************************
2020 * SetDCBrushColor (GDI32.@)
2022 * Sets the current device context (DC) brush color to the specified
2023 * color value. If the device cannot represent the specified color
2024 * value, the color is set to the nearest physical color.
2027 COLORREF WINAPI SetDCBrushColor(HDC hdc, COLORREF crColor)
2029 DC *dc;
2030 COLORREF oldClr = CLR_INVALID;
2032 TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
2034 dc = get_dc_ptr( hdc );
2035 if (dc)
2037 if (dc->funcs->pSetDCBrushColor)
2038 crColor = dc->funcs->pSetDCBrushColor( dc->physDev, crColor );
2039 else if (dc->hBrush == GetStockObject( DC_BRUSH ))
2041 /* If DC_BRUSH is selected, update driver pen color */
2042 HBRUSH hBrush = CreateSolidBrush( crColor );
2043 dc->funcs->pSelectBrush( dc->physDev, hBrush );
2044 DeleteObject( hBrush );
2047 if (crColor != CLR_INVALID)
2049 oldClr = dc->dcBrushColor;
2050 dc->dcBrushColor = crColor;
2053 release_dc_ptr( dc );
2056 return oldClr;
2059 /***********************************************************************
2060 * GetDCPenColor (GDI32.@)
2062 * Retrieves the current pen color for the specified device
2063 * context (DC).
2066 COLORREF WINAPI GetDCPenColor(HDC hdc)
2068 DC *dc;
2069 COLORREF dcPenColor = CLR_INVALID;
2071 TRACE("hdc(%p)\n", hdc);
2073 dc = get_dc_ptr( hdc );
2074 if (dc)
2076 dcPenColor = dc->dcPenColor;
2077 release_dc_ptr( dc );
2080 return dcPenColor;
2083 /***********************************************************************
2084 * SetDCPenColor (GDI32.@)
2086 * Sets the current device context (DC) pen color to the specified
2087 * color value. If the device cannot represent the specified color
2088 * value, the color is set to the nearest physical color.
2091 COLORREF WINAPI SetDCPenColor(HDC hdc, COLORREF crColor)
2093 DC *dc;
2094 COLORREF oldClr = CLR_INVALID;
2096 TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
2098 dc = get_dc_ptr( hdc );
2099 if (dc)
2101 if (dc->funcs->pSetDCPenColor)
2102 crColor = dc->funcs->pSetDCPenColor( dc->physDev, crColor );
2103 else if (dc->hPen == GetStockObject( DC_PEN ))
2105 /* If DC_PEN is selected, update the driver pen color */
2106 LOGPEN logpen = { PS_SOLID, { 0, 0 }, crColor };
2107 HPEN hPen = CreatePenIndirect( &logpen );
2108 dc->funcs->pSelectPen( dc->physDev, hPen );
2109 DeleteObject( hPen );
2112 if (crColor != CLR_INVALID)
2114 oldClr = dc->dcPenColor;
2115 dc->dcPenColor = crColor;
2118 release_dc_ptr( dc );
2121 return oldClr;
2124 /***********************************************************************
2125 * CancelDC (GDI32.@)
2127 BOOL WINAPI CancelDC(HDC hdc)
2129 FIXME("stub\n");
2130 return TRUE;
2133 /***********************************************************************
2134 * SetVirtualResolution (GDI32.@)
2136 * Undocumented on msdn. Called when PowerPoint XP saves a file.
2138 DWORD WINAPI SetVirtualResolution(HDC hdc, DWORD dw2, DWORD dw3, DWORD dw4, DWORD dw5)
2140 FIXME("(%p %08x %08x %08x %08x): stub!\n", hdc, dw2, dw3, dw4, dw5);
2141 return FALSE;
2144 /*******************************************************************
2145 * GetMiterLimit [GDI32.@]
2149 BOOL WINAPI GetMiterLimit(HDC hdc, PFLOAT peLimit)
2151 BOOL bRet = FALSE;
2152 DC *dc;
2154 TRACE("(%p,%p)\n", hdc, peLimit);
2156 dc = get_dc_ptr( hdc );
2157 if (dc)
2159 if (peLimit)
2160 *peLimit = dc->miterLimit;
2162 release_dc_ptr( dc );
2163 bRet = TRUE;
2165 return bRet;
2168 /*******************************************************************
2169 * SetMiterLimit [GDI32.@]
2173 BOOL WINAPI SetMiterLimit(HDC hdc, FLOAT eNewLimit, PFLOAT peOldLimit)
2175 BOOL bRet = FALSE;
2176 DC *dc;
2178 TRACE("(%p,%f,%p)\n", hdc, eNewLimit, peOldLimit);
2180 dc = get_dc_ptr( hdc );
2181 if (dc)
2183 if (peOldLimit)
2184 *peOldLimit = dc->miterLimit;
2185 dc->miterLimit = eNewLimit;
2186 release_dc_ptr( dc );
2187 bRet = TRUE;
2189 return bRet;
2192 /*******************************************************************
2193 * GdiIsMetaPrintDC [GDI32.@]
2195 BOOL WINAPI GdiIsMetaPrintDC(HDC hdc)
2197 FIXME("%p\n", hdc);
2198 return FALSE;
2201 /*******************************************************************
2202 * GdiIsMetaFileDC [GDI32.@]
2204 BOOL WINAPI GdiIsMetaFileDC(HDC hdc)
2206 TRACE("%p\n", hdc);
2208 switch( GetObjectType( hdc ) )
2210 case OBJ_METADC:
2211 case OBJ_ENHMETADC:
2212 return TRUE;
2214 return FALSE;
2217 /*******************************************************************
2218 * GdiIsPlayMetafileDC [GDI32.@]
2220 BOOL WINAPI GdiIsPlayMetafileDC(HDC hdc)
2222 FIXME("%p\n", hdc);
2223 return FALSE;