setupapi: Add stubs for SetupQueryInfOriginalFileInformationA/W.
[wine] / dlls / gdi32 / dc.c
1 /*
2  * GDI Device Context functions
3  *
4  * Copyright 1993 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "winreg.h"
30 #include "winternl.h"
31 #include "winerror.h"
32 #include "wownt32.h"
33 #include "wine/winuser16.h"
34 #include "gdi_private.h"
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(dc);
39
40 static const WCHAR displayW[] = { 'd','i','s','p','l','a','y',0 };
41
42 static BOOL DC_DeleteObject( HGDIOBJ handle, void *obj );
43
44 static const struct gdi_obj_funcs dc_funcs =
45 {
46     NULL,             /* pSelectObject */
47     NULL,             /* pGetObject16 */
48     NULL,             /* pGetObjectA */
49     NULL,             /* pGetObjectW */
50     NULL,             /* pUnrealizeObject */
51     DC_DeleteObject   /* pDeleteObject */
52 };
53
54 /***********************************************************************
55  *           DC_AllocDC
56  */
57 DC *DC_AllocDC( const DC_FUNCTIONS *funcs, WORD magic )
58 {
59     HDC hdc;
60     DC *dc;
61
62     if (!(dc = GDI_AllocObject( sizeof(*dc), magic, (HGDIOBJ*)&hdc, &dc_funcs ))) return NULL;
63
64     dc->hSelf               = hdc;
65     dc->funcs               = funcs;
66     dc->physDev             = NULL;
67     dc->saveLevel           = 0;
68     dc->saved_dc            = 0;
69     dc->dwHookData          = 0;
70     dc->hookProc            = NULL;
71     dc->hookThunk           = NULL;
72     dc->wndOrgX             = 0;
73     dc->wndOrgY             = 0;
74     dc->wndExtX             = 1;
75     dc->wndExtY             = 1;
76     dc->vportOrgX           = 0;
77     dc->vportOrgY           = 0;
78     dc->vportExtX           = 1;
79     dc->vportExtY           = 1;
80     dc->miterLimit          = 10.0f; /* 10.0 is the default, from MSDN */
81     dc->flags               = 0;
82     dc->layout              = 0;
83     dc->hClipRgn            = 0;
84     dc->hMetaRgn            = 0;
85     dc->hMetaClipRgn        = 0;
86     dc->hVisRgn             = 0;
87     dc->hPen                = GetStockObject( BLACK_PEN );
88     dc->hBrush              = GetStockObject( WHITE_BRUSH );
89     dc->hFont               = GetStockObject( SYSTEM_FONT );
90     dc->hBitmap             = 0;
91     dc->hDevice             = 0;
92     dc->hPalette            = GetStockObject( DEFAULT_PALETTE );
93     dc->gdiFont             = 0;
94     dc->ROPmode             = R2_COPYPEN;
95     dc->polyFillMode        = ALTERNATE;
96     dc->stretchBltMode      = BLACKONWHITE;
97     dc->relAbsMode          = ABSOLUTE;
98     dc->backgroundMode      = OPAQUE;
99     dc->backgroundColor     = RGB( 255, 255, 255 );
100     dc->dcBrushColor        = RGB( 255, 255, 255 );
101     dc->dcPenColor          = RGB( 0, 0, 0 );
102     dc->textColor           = RGB( 0, 0, 0 );
103     dc->brushOrgX           = 0;
104     dc->brushOrgY           = 0;
105     dc->textAlign           = TA_LEFT | TA_TOP | TA_NOUPDATECP;
106     dc->charExtra           = 0;
107     dc->breakExtra          = 0;
108     dc->breakRem            = 0;
109     dc->MapMode             = MM_TEXT;
110     dc->GraphicsMode        = GM_COMPATIBLE;
111     dc->pAbortProc          = NULL;
112     dc->CursPosX            = 0;
113     dc->CursPosY            = 0;
114     dc->ArcDirection        = AD_COUNTERCLOCKWISE;
115     dc->xformWorld2Wnd.eM11 = 1.0f;
116     dc->xformWorld2Wnd.eM12 = 0.0f;
117     dc->xformWorld2Wnd.eM21 = 0.0f;
118     dc->xformWorld2Wnd.eM22 = 1.0f;
119     dc->xformWorld2Wnd.eDx  = 0.0f;
120     dc->xformWorld2Wnd.eDy  = 0.0f;
121     dc->xformWorld2Vport    = dc->xformWorld2Wnd;
122     dc->xformVport2World    = dc->xformWorld2Wnd;
123     dc->vport2WorldValid    = TRUE;
124     dc->BoundsRect.left     = 0;
125     dc->BoundsRect.top      = 0;
126     dc->BoundsRect.right    = 0;
127     dc->BoundsRect.bottom   = 0;
128     dc->saved_visrgn        = NULL;
129     PATH_InitGdiPath(&dc->path);
130     return dc;
131 }
132
133
134
135 /***********************************************************************
136  *           DC_GetDCPtr
137  */
138 DC *DC_GetDCPtr( HDC hdc )
139 {
140     GDIOBJHDR *ptr = GDI_GetObjPtr( hdc, MAGIC_DONTCARE );
141     if (!ptr) return NULL;
142     if ((GDIMAGIC(ptr->wMagic) == DC_MAGIC) ||
143         (GDIMAGIC(ptr->wMagic) == MEMORY_DC_MAGIC) ||
144         (GDIMAGIC(ptr->wMagic) == METAFILE_DC_MAGIC) ||
145         (GDIMAGIC(ptr->wMagic) == ENHMETAFILE_DC_MAGIC))
146         return (DC *)ptr;
147     GDI_ReleaseObj( hdc );
148     SetLastError( ERROR_INVALID_HANDLE );
149     return NULL;
150 }
151
152 /***********************************************************************
153  *           DC_GetDCUpdate
154  *
155  * Retrieve a DC ptr while making sure the visRgn is updated.
156  * This function may call up to USER so the GDI lock should _not_
157  * be held when calling it.
158  */
159 DC *DC_GetDCUpdate( HDC hdc )
160 {
161     DC *dc = DC_GetDCPtr( hdc );
162     if (!dc) return NULL;
163     while (dc->flags & DC_DIRTY)
164     {
165         DCHOOKPROC proc = dc->hookThunk;
166         dc->flags &= ~DC_DIRTY;
167         if (proc)
168         {
169             DWORD data = dc->dwHookData;
170             GDI_ReleaseObj( hdc );
171             proc( HDC_16(hdc), DCHC_INVALIDVISRGN, data, 0 );
172             if (!(dc = DC_GetDCPtr( hdc ))) break;
173             /* otherwise restart the loop in case it became dirty again in the meantime */
174         }
175     }
176     return dc;
177 }
178
179
180 /***********************************************************************
181  *           DC_DeleteObject
182  */
183 static BOOL DC_DeleteObject( HGDIOBJ handle, void *obj )
184 {
185     GDI_ReleaseObj( handle );
186     return DeleteDC( handle );
187 }
188
189
190 /***********************************************************************
191  *           DC_InitDC
192  *
193  * Setup device-specific DC values for a newly created DC.
194  */
195 void DC_InitDC( DC* dc )
196 {
197     if (dc->funcs->pRealizeDefaultPalette) dc->funcs->pRealizeDefaultPalette( dc->physDev );
198     SetTextColor( dc->hSelf, dc->textColor );
199     SetBkColor( dc->hSelf, dc->backgroundColor );
200     SelectObject( dc->hSelf, dc->hPen );
201     SelectObject( dc->hSelf, dc->hBrush );
202     SelectObject( dc->hSelf, dc->hFont );
203     CLIPPING_UpdateGCRegion( dc );
204 }
205
206
207 /***********************************************************************
208  *           DC_InvertXform
209  *
210  * Computes the inverse of the transformation xformSrc and stores it to
211  * xformDest. Returns TRUE if successful or FALSE if the xformSrc matrix
212  * is singular.
213  */
214 static BOOL DC_InvertXform( const XFORM *xformSrc, XFORM *xformDest )
215 {
216     FLOAT determinant;
217
218     determinant = xformSrc->eM11*xformSrc->eM22 -
219         xformSrc->eM12*xformSrc->eM21;
220     if (determinant > -1e-12 && determinant < 1e-12)
221         return FALSE;
222
223     xformDest->eM11 =  xformSrc->eM22 / determinant;
224     xformDest->eM12 = -xformSrc->eM12 / determinant;
225     xformDest->eM21 = -xformSrc->eM21 / determinant;
226     xformDest->eM22 =  xformSrc->eM11 / determinant;
227     xformDest->eDx  = -xformSrc->eDx * xformDest->eM11 -
228                        xformSrc->eDy * xformDest->eM21;
229     xformDest->eDy  = -xformSrc->eDx * xformDest->eM12 -
230                        xformSrc->eDy * xformDest->eM22;
231
232     return TRUE;
233 }
234
235
236 /***********************************************************************
237  *           DC_UpdateXforms
238  *
239  * Updates the xformWorld2Vport, xformVport2World and vport2WorldValid
240  * fields of the specified DC by creating a transformation that
241  * represents the current mapping mode and combining it with the DC's
242  * world transform. This function should be called whenever the
243  * parameters associated with the mapping mode (window and viewport
244  * extents and origins) or the world transform change.
245  */
246 void DC_UpdateXforms( DC *dc )
247 {
248     XFORM xformWnd2Vport, oldworld2vport;
249     FLOAT scaleX, scaleY;
250
251     /* Construct a transformation to do the window-to-viewport conversion */
252     scaleX = (FLOAT)dc->vportExtX / (FLOAT)dc->wndExtX;
253     scaleY = (FLOAT)dc->vportExtY / (FLOAT)dc->wndExtY;
254     xformWnd2Vport.eM11 = scaleX;
255     xformWnd2Vport.eM12 = 0.0;
256     xformWnd2Vport.eM21 = 0.0;
257     xformWnd2Vport.eM22 = scaleY;
258     xformWnd2Vport.eDx  = (FLOAT)dc->vportOrgX -
259         scaleX * (FLOAT)dc->wndOrgX;
260     xformWnd2Vport.eDy  = (FLOAT)dc->vportOrgY -
261         scaleY * (FLOAT)dc->wndOrgY;
262
263     oldworld2vport = dc->xformWorld2Vport;
264     /* Combine with the world transformation */
265     CombineTransform( &dc->xformWorld2Vport, &dc->xformWorld2Wnd,
266         &xformWnd2Vport );
267
268     /* Create inverse of world-to-viewport transformation */
269     dc->vport2WorldValid = DC_InvertXform( &dc->xformWorld2Vport,
270         &dc->xformVport2World );
271
272     /* Reselect the font and pen back into the dc so that the size
273        gets updated. */
274     if(memcmp(&oldworld2vport, &dc->xformWorld2Vport, sizeof(oldworld2vport)))
275     {
276         SelectObject(dc->hSelf, GetCurrentObject(dc->hSelf, OBJ_FONT));
277         SelectObject(dc->hSelf, GetCurrentObject(dc->hSelf, OBJ_PEN));
278     }
279 }
280
281
282 /***********************************************************************
283  *           GetDCState   (Not a Windows API)
284  */
285 HDC WINAPI GetDCState( HDC hdc )
286 {
287     DC * newdc, * dc;
288     HGDIOBJ handle;
289
290     if (!(dc = DC_GetDCPtr( hdc ))) return 0;
291     if (!(newdc = GDI_AllocObject( sizeof(DC), GDIMAGIC(dc->header.wMagic), &handle, &dc_funcs )))
292     {
293       GDI_ReleaseObj( hdc );
294       return 0;
295     }
296     TRACE("(%p): returning %p\n", hdc, handle );
297
298     newdc->flags            = dc->flags | DC_SAVED;
299     newdc->layout           = dc->layout;
300     newdc->hPen             = dc->hPen;
301     newdc->hBrush           = dc->hBrush;
302     newdc->hFont            = dc->hFont;
303     newdc->hBitmap          = dc->hBitmap;
304     newdc->hDevice          = dc->hDevice;
305     newdc->hPalette         = dc->hPalette;
306     newdc->ROPmode          = dc->ROPmode;
307     newdc->polyFillMode     = dc->polyFillMode;
308     newdc->stretchBltMode   = dc->stretchBltMode;
309     newdc->relAbsMode       = dc->relAbsMode;
310     newdc->backgroundMode   = dc->backgroundMode;
311     newdc->backgroundColor  = dc->backgroundColor;
312     newdc->textColor        = dc->textColor;
313     newdc->dcBrushColor     = dc->dcBrushColor;
314     newdc->dcPenColor       = dc->dcPenColor;
315     newdc->brushOrgX        = dc->brushOrgX;
316     newdc->brushOrgY        = dc->brushOrgY;
317     newdc->textAlign        = dc->textAlign;
318     newdc->charExtra        = dc->charExtra;
319     newdc->breakExtra       = dc->breakExtra;
320     newdc->breakRem         = dc->breakRem;
321     newdc->MapMode          = dc->MapMode;
322     newdc->GraphicsMode     = dc->GraphicsMode;
323     newdc->CursPosX         = dc->CursPosX;
324     newdc->CursPosY         = dc->CursPosY;
325     newdc->ArcDirection     = dc->ArcDirection;
326     newdc->xformWorld2Wnd   = dc->xformWorld2Wnd;
327     newdc->xformWorld2Vport = dc->xformWorld2Vport;
328     newdc->xformVport2World = dc->xformVport2World;
329     newdc->vport2WorldValid = dc->vport2WorldValid;
330     newdc->wndOrgX          = dc->wndOrgX;
331     newdc->wndOrgY          = dc->wndOrgY;
332     newdc->wndExtX          = dc->wndExtX;
333     newdc->wndExtY          = dc->wndExtY;
334     newdc->vportOrgX        = dc->vportOrgX;
335     newdc->vportOrgY        = dc->vportOrgY;
336     newdc->vportExtX        = dc->vportExtX;
337     newdc->vportExtY        = dc->vportExtY;
338     newdc->BoundsRect       = dc->BoundsRect;
339
340     newdc->hSelf = (HDC)handle;
341     newdc->saveLevel = 0;
342     newdc->saved_dc  = 0;
343
344     PATH_InitGdiPath( &newdc->path );
345
346     newdc->pAbortProc = NULL;
347     newdc->hookThunk  = NULL;
348     newdc->hookProc   = 0;
349     newdc->saved_visrgn = NULL;
350
351     /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
352
353     newdc->hVisRgn      = 0;
354     newdc->hClipRgn     = 0;
355     newdc->hMetaRgn     = 0;
356     newdc->hMetaClipRgn = 0;
357     if (dc->hClipRgn)
358     {
359         newdc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
360         CombineRgn( newdc->hClipRgn, dc->hClipRgn, 0, RGN_COPY );
361     }
362     if (dc->hMetaRgn)
363     {
364         newdc->hMetaRgn = CreateRectRgn( 0, 0, 0, 0 );
365         CombineRgn( newdc->hMetaRgn, dc->hMetaRgn, 0, RGN_COPY );
366     }
367     /* don't bother recomputing hMetaClipRgn, we'll do that in SetDCState */
368
369     if(dc->gdiFont) {
370         newdc->gdiFont = dc->gdiFont;
371     } else
372         newdc->gdiFont = 0;
373
374     GDI_ReleaseObj( handle );
375     GDI_ReleaseObj( hdc );
376     return handle;
377 }
378
379
380 /***********************************************************************
381  *           SetDCState   (Not a Windows API)
382  */
383 void WINAPI SetDCState( HDC hdc, HDC hdcs )
384 {
385     DC *dc, *dcs;
386
387     if (!(dc = DC_GetDCUpdate( hdc ))) return;
388     if (!(dcs = DC_GetDCPtr( hdcs )))
389     {
390       GDI_ReleaseObj( hdc );
391       return;
392     }
393     if (!dcs->flags & DC_SAVED)
394     {
395       GDI_ReleaseObj( hdc );
396       GDI_ReleaseObj( hdcs );
397       return;
398     }
399     TRACE("%p %p\n", hdc, hdcs );
400
401     dc->flags            = dcs->flags & ~(DC_SAVED | DC_DIRTY);
402     dc->layout           = dcs->layout;
403     dc->hDevice          = dcs->hDevice;
404     dc->ROPmode          = dcs->ROPmode;
405     dc->polyFillMode     = dcs->polyFillMode;
406     dc->stretchBltMode   = dcs->stretchBltMode;
407     dc->relAbsMode       = dcs->relAbsMode;
408     dc->backgroundMode   = dcs->backgroundMode;
409     dc->backgroundColor  = dcs->backgroundColor;
410     dc->textColor        = dcs->textColor;
411     dc->dcBrushColor     = dcs->dcBrushColor;
412     dc->dcPenColor       = dcs->dcPenColor;
413     dc->brushOrgX        = dcs->brushOrgX;
414     dc->brushOrgY        = dcs->brushOrgY;
415     dc->textAlign        = dcs->textAlign;
416     dc->charExtra        = dcs->charExtra;
417     dc->breakExtra       = dcs->breakExtra;
418     dc->breakRem         = dcs->breakRem;
419     dc->MapMode          = dcs->MapMode;
420     dc->GraphicsMode     = dcs->GraphicsMode;
421     dc->CursPosX         = dcs->CursPosX;
422     dc->CursPosY         = dcs->CursPosY;
423     dc->ArcDirection     = dcs->ArcDirection;
424     dc->xformWorld2Wnd   = dcs->xformWorld2Wnd;
425     dc->xformWorld2Vport = dcs->xformWorld2Vport;
426     dc->xformVport2World = dcs->xformVport2World;
427     dc->vport2WorldValid = dcs->vport2WorldValid;
428     dc->BoundsRect       = dcs->BoundsRect;
429
430     dc->wndOrgX          = dcs->wndOrgX;
431     dc->wndOrgY          = dcs->wndOrgY;
432     dc->wndExtX          = dcs->wndExtX;
433     dc->wndExtY          = dcs->wndExtY;
434     dc->vportOrgX        = dcs->vportOrgX;
435     dc->vportOrgY        = dcs->vportOrgY;
436     dc->vportExtX        = dcs->vportExtX;
437     dc->vportExtY        = dcs->vportExtY;
438
439     if (dcs->hClipRgn)
440     {
441         if (!dc->hClipRgn) dc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
442         CombineRgn( dc->hClipRgn, dcs->hClipRgn, 0, RGN_COPY );
443     }
444     else
445     {
446         if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
447         dc->hClipRgn = 0;
448     }
449     if (dcs->hMetaRgn)
450     {
451         if (!dc->hMetaRgn) dc->hMetaRgn = CreateRectRgn( 0, 0, 0, 0 );
452         CombineRgn( dc->hMetaRgn, dcs->hMetaRgn, 0, RGN_COPY );
453     }
454     else
455     {
456         if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
457         dc->hMetaRgn = 0;
458     }
459     CLIPPING_UpdateGCRegion( dc );
460
461     SelectObject( hdc, dcs->hBitmap );
462     SelectObject( hdc, dcs->hBrush );
463     SelectObject( hdc, dcs->hFont );
464     SelectObject( hdc, dcs->hPen );
465     SetBkColor( hdc, dcs->backgroundColor);
466     SetTextColor( hdc, dcs->textColor);
467     GDISelectPalette( hdc, dcs->hPalette, FALSE );
468     GDI_ReleaseObj( hdcs );
469     GDI_ReleaseObj( hdc );
470 }
471
472
473 /***********************************************************************
474  *           GetDCState   (GDI.179)
475  */
476 HDC16 WINAPI GetDCState16( HDC16 hdc )
477 {
478     return HDC_16( GetDCState( HDC_32(hdc) ));
479 }
480
481
482 /***********************************************************************
483  *           SetDCState   (GDI.180)
484  */
485 void WINAPI SetDCState16( HDC16 hdc, HDC16 hdcs )
486 {
487     SetDCState( HDC_32(hdc), HDC_32(hdcs) );
488 }
489
490
491 /***********************************************************************
492  *           SaveDC    (GDI32.@)
493  */
494 INT WINAPI SaveDC( HDC hdc )
495 {
496     HDC hdcs;
497     DC * dc, * dcs;
498     INT ret;
499
500     dc = DC_GetDCPtr( hdc );
501     if (!dc) return 0;
502
503     if(dc->funcs->pSaveDC)
504     {
505         ret = dc->funcs->pSaveDC( dc->physDev );
506         if(ret)
507             ret = ++dc->saveLevel;
508         GDI_ReleaseObj( hdc );
509         return ret;
510     }
511
512     if (!(hdcs = GetDCState( hdc )))
513     {
514       GDI_ReleaseObj( hdc );
515       return 0;
516     }
517     dcs = DC_GetDCPtr( hdcs );
518
519     /* Copy path. The reason why path saving / restoring is in SaveDC/
520      * RestoreDC and not in GetDCState/SetDCState is that the ...DCState
521      * functions are only in Win16 (which doesn't have paths) and that
522      * SetDCState doesn't allow us to signal an error (which can happen
523      * when copying paths).
524      */
525     if (!PATH_AssignGdiPath( &dcs->path, &dc->path ))
526     {
527         GDI_ReleaseObj( hdc );
528         GDI_ReleaseObj( hdcs );
529         DeleteDC( hdcs );
530         return 0;
531     }
532
533     dcs->saved_dc = dc->saved_dc;
534     dc->saved_dc = hdcs;
535     TRACE("(%p): returning %d\n", hdc, dc->saveLevel+1 );
536     ret = ++dc->saveLevel;
537     GDI_ReleaseObj( hdcs );
538     GDI_ReleaseObj( hdc );
539     return ret;
540 }
541
542
543 /***********************************************************************
544  *           RestoreDC    (GDI32.@)
545  */
546 BOOL WINAPI RestoreDC( HDC hdc, INT level )
547 {
548     DC * dc, * dcs;
549     BOOL success;
550
551     TRACE("%p %d\n", hdc, level );
552     dc = DC_GetDCUpdate( hdc );
553     if(!dc) return FALSE;
554
555     if(abs(level) > dc->saveLevel || level == 0)
556     {
557         GDI_ReleaseObj( hdc );
558         return FALSE;
559     }
560         
561     if(dc->funcs->pRestoreDC)
562     {
563         success = dc->funcs->pRestoreDC( dc->physDev, level );
564         if(level < 0) level = dc->saveLevel + level + 1;
565         if(success)
566             dc->saveLevel = level - 1;
567         GDI_ReleaseObj( hdc );
568         return success;
569     }
570
571     if (level < 0) level = dc->saveLevel + level + 1;
572     success=TRUE;
573     while (dc->saveLevel >= level)
574     {
575         HDC hdcs = dc->saved_dc;
576         if (!(dcs = DC_GetDCPtr( hdcs )))
577         {
578           GDI_ReleaseObj( hdc );
579           return FALSE;
580         }
581         dc->saved_dc = dcs->saved_dc;
582         dcs->saved_dc = 0;
583         if (--dc->saveLevel < level)
584         {
585             SetDCState( hdc, hdcs );
586             if (!PATH_AssignGdiPath( &dc->path, &dcs->path ))
587                 /* FIXME: This might not be quite right, since we're
588                  * returning FALSE but still destroying the saved DC state */
589                 success=FALSE;
590         }
591         GDI_ReleaseObj( hdcs );
592         GDI_ReleaseObj( hdc );
593         DeleteDC( hdcs );
594         if (!(dc = DC_GetDCPtr( hdc ))) return FALSE;
595     }
596     GDI_ReleaseObj( hdc );
597     return success;
598 }
599
600
601 /***********************************************************************
602  *           CreateDCW    (GDI32.@)
603  */
604 HDC WINAPI CreateDCW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
605                       const DEVMODEW *initData )
606 {
607     HDC hdc;
608     DC * dc;
609     const DC_FUNCTIONS *funcs;
610     WCHAR buf[300];
611
612     GDI_CheckNotLock();
613
614     if (!device || !DRIVER_GetDriverName( device, buf, 300 ))
615     {
616         if (!driver)
617         {
618             ERR( "no device found for %s\n", debugstr_w(device) );
619             return 0;
620         }
621         strcpyW(buf, driver);
622     }
623
624     if (!(funcs = DRIVER_load_driver( buf )))
625     {
626         ERR( "no driver found for %s\n", debugstr_w(buf) );
627         return 0;
628     }
629     if (!(dc = DC_AllocDC( funcs, DC_MAGIC ))) goto error;
630     hdc = dc->hSelf;
631
632     dc->hBitmap = GetStockObject( DEFAULT_BITMAP );
633     if (!(dc->hVisRgn = CreateRectRgn( 0, 0, 1, 1 ))) goto error;
634
635     TRACE("(driver=%s, device=%s, output=%s): returning %p\n",
636           debugstr_w(driver), debugstr_w(device), debugstr_w(output), dc->hSelf );
637
638     if (dc->funcs->pCreateDC &&
639         !dc->funcs->pCreateDC( hdc, &dc->physDev, buf, device, output, initData ))
640     {
641         WARN("creation aborted by device\n" );
642         goto error;
643     }
644
645     SetRectRgn( dc->hVisRgn, 0, 0,
646                 GetDeviceCaps( hdc, DESKTOPHORZRES ), GetDeviceCaps( hdc, DESKTOPVERTRES ) );
647
648     DC_InitDC( dc );
649     GDI_ReleaseObj( hdc );
650     return hdc;
651
652 error:
653     if (dc && dc->hVisRgn) DeleteObject( dc->hVisRgn );
654     if (dc) GDI_FreeObject( dc->hSelf, dc );
655     DRIVER_release_driver( funcs );
656     return 0;
657 }
658
659
660 /***********************************************************************
661  *           CreateDCA    (GDI32.@)
662  */
663 HDC WINAPI CreateDCA( LPCSTR driver, LPCSTR device, LPCSTR output,
664                       const DEVMODEA *initData )
665 {
666     UNICODE_STRING driverW, deviceW, outputW;
667     DEVMODEW *initDataW;
668     HDC ret;
669
670     if (driver) RtlCreateUnicodeStringFromAsciiz(&driverW, driver);
671     else driverW.Buffer = NULL;
672
673     if (device) RtlCreateUnicodeStringFromAsciiz(&deviceW, device);
674     else deviceW.Buffer = NULL;
675
676     if (output) RtlCreateUnicodeStringFromAsciiz(&outputW, output);
677     else outputW.Buffer = NULL;
678
679     initDataW = NULL;
680     if (initData)
681     {
682         /* don't convert initData for DISPLAY driver, it's not used */
683         if (!driverW.Buffer || strcmpiW( driverW.Buffer, displayW ))
684             initDataW = GdiConvertToDevmodeW(initData);
685     }
686
687     ret = CreateDCW( driverW.Buffer, deviceW.Buffer, outputW.Buffer, initDataW );
688
689     RtlFreeUnicodeString(&driverW);
690     RtlFreeUnicodeString(&deviceW);
691     RtlFreeUnicodeString(&outputW);
692     HeapFree(GetProcessHeap(), 0, initDataW);
693     return ret;
694 }
695
696
697 /***********************************************************************
698  *           CreateICA    (GDI32.@)
699  */
700 HDC WINAPI CreateICA( LPCSTR driver, LPCSTR device, LPCSTR output,
701                           const DEVMODEA* initData )
702 {
703       /* Nothing special yet for ICs */
704     return CreateDCA( driver, device, output, initData );
705 }
706
707
708 /***********************************************************************
709  *           CreateICW    (GDI32.@)
710  */
711 HDC WINAPI CreateICW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
712                           const DEVMODEW* initData )
713 {
714       /* Nothing special yet for ICs */
715     return CreateDCW( driver, device, output, initData );
716 }
717
718
719 /***********************************************************************
720  *           CreateCompatibleDC   (GDI32.@)
721  */
722 HDC WINAPI CreateCompatibleDC( HDC hdc )
723 {
724     DC *dc, *origDC;
725     const DC_FUNCTIONS *funcs;
726     PHYSDEV physDev;
727
728     GDI_CheckNotLock();
729
730     if ((origDC = GDI_GetObjPtr( hdc, DC_MAGIC )))
731     {
732         funcs = origDC->funcs;
733         physDev = origDC->physDev;
734         GDI_ReleaseObj( hdc ); /* can't hold the lock while loading the driver */
735         funcs = DRIVER_get_driver( funcs );
736     }
737     else
738     {
739         funcs = DRIVER_load_driver( displayW );
740         physDev = NULL;
741     }
742
743     if (!funcs) return 0;
744
745     if (!(dc = DC_AllocDC( funcs, MEMORY_DC_MAGIC ))) goto error;
746
747     TRACE("(%p): returning %p\n", hdc, dc->hSelf );
748
749     dc->hBitmap = GetStockObject( DEFAULT_BITMAP );
750     if (!(dc->hVisRgn = CreateRectRgn( 0, 0, 1, 1 ))) goto error;   /* default bitmap is 1x1 */
751
752     /* Copy the driver-specific physical device info into
753      * the new DC. The driver may use this read-only info
754      * while creating the compatible DC below. */
755     dc->physDev = physDev;
756
757     if (dc->funcs->pCreateDC &&
758         !dc->funcs->pCreateDC( dc->hSelf, &dc->physDev, NULL, NULL, NULL, NULL ))
759     {
760         WARN("creation aborted by device\n");
761         goto error;
762     }
763
764     DC_InitDC( dc );
765     GDI_ReleaseObj( dc->hSelf );
766     return dc->hSelf;
767
768 error:
769     if (dc && dc->hVisRgn) DeleteObject( dc->hVisRgn );
770     if (dc) GDI_FreeObject( dc->hSelf, dc );
771     DRIVER_release_driver( funcs );
772     return 0;
773 }
774
775
776 /***********************************************************************
777  *           DeleteDC    (GDI32.@)
778  */
779 BOOL WINAPI DeleteDC( HDC hdc )
780 {
781     const DC_FUNCTIONS *funcs = NULL;
782     DC * dc;
783
784     TRACE("%p\n", hdc );
785
786     GDI_CheckNotLock();
787
788     if (!(dc = DC_GetDCPtr( hdc ))) return FALSE;
789
790     /* Call hook procedure to check whether is it OK to delete this DC */
791     if (dc->hookThunk)
792     {
793         DCHOOKPROC proc = dc->hookThunk;
794         DWORD data = dc->dwHookData;
795         GDI_ReleaseObj( hdc );
796         if (!proc( HDC_16(hdc), DCHC_DELETEDC, data, 0 )) return FALSE;
797         if (!(dc = DC_GetDCPtr( hdc ))) return TRUE;  /* deleted by the hook */
798     }
799
800     while (dc->saveLevel)
801     {
802         DC * dcs;
803         HDC hdcs = dc->saved_dc;
804         if (!(dcs = DC_GetDCPtr( hdcs ))) break;
805         dc->saved_dc = dcs->saved_dc;
806         dc->saveLevel--;
807         if (dcs->hClipRgn) DeleteObject( dcs->hClipRgn );
808         if (dcs->hMetaRgn) DeleteObject( dcs->hMetaRgn );
809         if (dcs->hMetaClipRgn) DeleteObject( dcs->hMetaClipRgn );
810         if (dcs->hVisRgn) DeleteObject( dcs->hVisRgn );
811         PATH_DestroyGdiPath(&dcs->path);
812         GDI_FreeObject( hdcs, dcs );
813     }
814
815     if (!(dc->flags & DC_SAVED))
816     {
817         SelectObject( hdc, GetStockObject(BLACK_PEN) );
818         SelectObject( hdc, GetStockObject(WHITE_BRUSH) );
819         SelectObject( hdc, GetStockObject(SYSTEM_FONT) );
820         SelectObject( hdc, GetStockObject(DEFAULT_BITMAP) );
821         funcs = dc->funcs;
822         if (dc->funcs->pDeleteDC) dc->funcs->pDeleteDC(dc->physDev);
823         dc->physDev = NULL;
824     }
825
826     while (dc->saved_visrgn)
827     {
828         struct saved_visrgn *next = dc->saved_visrgn->next;
829         DeleteObject( dc->saved_visrgn->hrgn );
830         HeapFree( GetProcessHeap(), 0, dc->saved_visrgn );
831         dc->saved_visrgn = next;
832     }
833     if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
834     if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
835     if (dc->hMetaClipRgn) DeleteObject( dc->hMetaClipRgn );
836     if (dc->hVisRgn) DeleteObject( dc->hVisRgn );
837     PATH_DestroyGdiPath(&dc->path);
838
839     GDI_FreeObject( hdc, dc );
840     if (funcs) DRIVER_release_driver( funcs );  /* do that after releasing the GDI lock */
841     return TRUE;
842 }
843
844
845 /***********************************************************************
846  *           ResetDCW    (GDI32.@)
847  */
848 HDC WINAPI ResetDCW( HDC hdc, const DEVMODEW *devmode )
849 {
850     DC *dc;
851     HDC ret = hdc;
852
853     if ((dc = DC_GetDCPtr( hdc )))
854     {
855         if (dc->funcs->pResetDC) ret = dc->funcs->pResetDC( dc->physDev, devmode );
856         GDI_ReleaseObj( hdc );
857     }
858     return ret;
859 }
860
861
862 /***********************************************************************
863  *           ResetDCA    (GDI32.@)
864  */
865 HDC WINAPI ResetDCA( HDC hdc, const DEVMODEA *devmode )
866 {
867     DEVMODEW *devmodeW;
868     HDC ret;
869
870     if (devmode) devmodeW = GdiConvertToDevmodeW(devmode);
871     else devmodeW = NULL;
872
873     ret = ResetDCW(hdc, devmodeW);
874
875     HeapFree(GetProcessHeap(), 0, devmodeW);
876     return ret;
877 }
878
879
880 /***********************************************************************
881  *           GetDeviceCaps    (GDI32.@)
882  */
883 INT WINAPI GetDeviceCaps( HDC hdc, INT cap )
884 {
885     DC *dc;
886     INT ret = 0;
887
888     if ((dc = DC_GetDCPtr( hdc )))
889     {
890         if (dc->funcs->pGetDeviceCaps) ret = dc->funcs->pGetDeviceCaps( dc->physDev, cap );
891         else switch(cap)  /* return meaningful values for some entries */
892         {
893         case HORZRES:     ret = 640; break;
894         case VERTRES:     ret = 480; break;
895         case BITSPIXEL:   ret = 1; break;
896         case PLANES:      ret = 1; break;
897         case NUMCOLORS:   ret = 2; break;
898         case ASPECTX:     ret = 36; break;
899         case ASPECTY:     ret = 36; break;
900         case ASPECTXY:    ret = 51; break;
901         case LOGPIXELSX:  ret = 72; break;
902         case LOGPIXELSY:  ret = 72; break;
903         case SIZEPALETTE: ret = 2; break;
904         }
905         GDI_ReleaseObj( hdc );
906     }
907     return ret;
908 }
909
910
911 /***********************************************************************
912  *              GetBkColor (GDI32.@)
913  */
914 COLORREF WINAPI GetBkColor( HDC hdc )
915 {
916     COLORREF ret = 0;
917     DC * dc = DC_GetDCPtr( hdc );
918     if (dc)
919     {
920         ret = dc->backgroundColor;
921         GDI_ReleaseObj( hdc );
922     }
923     return ret;
924 }
925
926
927 /***********************************************************************
928  *           SetBkColor    (GDI32.@)
929  */
930 COLORREF WINAPI SetBkColor( HDC hdc, COLORREF color )
931 {
932     COLORREF oldColor;
933     DC * dc = DC_GetDCPtr( hdc );
934
935     TRACE("hdc=%p color=0x%08x\n", hdc, color);
936
937     if (!dc) return CLR_INVALID;
938     oldColor = dc->backgroundColor;
939     if (dc->funcs->pSetBkColor)
940     {
941         color = dc->funcs->pSetBkColor(dc->physDev, color);
942         if (color == CLR_INVALID)  /* don't change it */
943         {
944             color = oldColor;
945             oldColor = CLR_INVALID;
946         }
947     }
948     dc->backgroundColor = color;
949     GDI_ReleaseObj( hdc );
950     return oldColor;
951 }
952
953
954 /***********************************************************************
955  *              GetTextColor (GDI32.@)
956  */
957 COLORREF WINAPI GetTextColor( HDC hdc )
958 {
959     COLORREF ret = 0;
960     DC * dc = DC_GetDCPtr( hdc );
961     if (dc)
962     {
963         ret = dc->textColor;
964         GDI_ReleaseObj( hdc );
965     }
966     return ret;
967 }
968
969
970 /***********************************************************************
971  *           SetTextColor    (GDI32.@)
972  */
973 COLORREF WINAPI SetTextColor( HDC hdc, COLORREF color )
974 {
975     COLORREF oldColor;
976     DC * dc = DC_GetDCPtr( hdc );
977
978     TRACE(" hdc=%p color=0x%08x\n", hdc, color);
979
980     if (!dc) return CLR_INVALID;
981     oldColor = dc->textColor;
982     if (dc->funcs->pSetTextColor)
983     {
984         color = dc->funcs->pSetTextColor(dc->physDev, color);
985         if (color == CLR_INVALID)  /* don't change it */
986         {
987             color = oldColor;
988             oldColor = CLR_INVALID;
989         }
990     }
991     dc->textColor = color;
992     GDI_ReleaseObj( hdc );
993     return oldColor;
994 }
995
996
997 /***********************************************************************
998  *              GetTextAlign (GDI32.@)
999  */
1000 UINT WINAPI GetTextAlign( HDC hdc )
1001 {
1002     UINT ret = 0;
1003     DC * dc = DC_GetDCPtr( hdc );
1004     if (dc)
1005     {
1006         ret = dc->textAlign;
1007         GDI_ReleaseObj( hdc );
1008     }
1009     return ret;
1010 }
1011
1012
1013 /***********************************************************************
1014  *           SetTextAlign    (GDI32.@)
1015  */
1016 UINT WINAPI SetTextAlign( HDC hdc, UINT align )
1017 {
1018     UINT ret;
1019     DC *dc = DC_GetDCPtr( hdc );
1020
1021     TRACE("hdc=%p align=%d\n", hdc, align);
1022
1023     if (!dc) return 0x0;
1024     ret = dc->textAlign;
1025     if (dc->funcs->pSetTextAlign)
1026         if (!dc->funcs->pSetTextAlign(dc->physDev, align))
1027             ret = GDI_ERROR;
1028     if (ret != GDI_ERROR)
1029         dc->textAlign = align;
1030     GDI_ReleaseObj( hdc );
1031     return ret;
1032 }
1033
1034 /***********************************************************************
1035  *           GetDCOrgEx  (GDI32.@)
1036  */
1037 BOOL WINAPI GetDCOrgEx( HDC hDC, LPPOINT lpp )
1038 {
1039     DC * dc;
1040
1041     if (!lpp) return FALSE;
1042     if (!(dc = DC_GetDCPtr( hDC ))) return FALSE;
1043
1044     lpp->x = lpp->y = 0;
1045     if (dc->funcs->pGetDCOrgEx) dc->funcs->pGetDCOrgEx( dc->physDev, lpp );
1046     GDI_ReleaseObj( hDC );
1047     return TRUE;
1048 }
1049
1050
1051 /***********************************************************************
1052  *           SetDCOrg   (GDI.117)
1053  */
1054 DWORD WINAPI SetDCOrg16( HDC16 hdc16, INT16 x, INT16 y )
1055 {
1056     DWORD prevOrg = 0;
1057     HDC hdc = HDC_32( hdc16 );
1058     DC *dc = DC_GetDCPtr( hdc );
1059     if (!dc) return 0;
1060     if (dc->funcs->pSetDCOrg) prevOrg = dc->funcs->pSetDCOrg( dc->physDev, x, y );
1061     GDI_ReleaseObj( hdc );
1062     return prevOrg;
1063 }
1064
1065
1066 /***********************************************************************
1067  *              GetGraphicsMode (GDI32.@)
1068  */
1069 INT WINAPI GetGraphicsMode( HDC hdc )
1070 {
1071     INT ret = 0;
1072     DC * dc = DC_GetDCPtr( hdc );
1073     if (dc)
1074     {
1075         ret = dc->GraphicsMode;
1076         GDI_ReleaseObj( hdc );
1077     }
1078     return ret;
1079 }
1080
1081
1082 /***********************************************************************
1083  *           SetGraphicsMode    (GDI32.@)
1084  */
1085 INT WINAPI SetGraphicsMode( HDC hdc, INT mode )
1086 {
1087     INT ret = 0;
1088     DC *dc = DC_GetDCPtr( hdc );
1089
1090     /* One would think that setting the graphics mode to GM_COMPATIBLE
1091      * would also reset the world transformation matrix to the unity
1092      * matrix. However, in Windows, this is not the case. This doesn't
1093      * make a lot of sense to me, but that's the way it is.
1094      */
1095     if (!dc) return 0;
1096     if ((mode > 0) && (mode <= GM_LAST))
1097     {
1098         ret = dc->GraphicsMode;
1099         dc->GraphicsMode = mode;
1100     }
1101     GDI_ReleaseObj( hdc );
1102     return ret;
1103 }
1104
1105
1106 /***********************************************************************
1107  *              GetArcDirection (GDI32.@)
1108  */
1109 INT WINAPI GetArcDirection( HDC hdc )
1110 {
1111     INT ret = 0;
1112     DC * dc = DC_GetDCPtr( hdc );
1113     if (dc)
1114     {
1115         ret = dc->ArcDirection;
1116         GDI_ReleaseObj( hdc );
1117     }
1118     return ret;
1119 }
1120
1121
1122 /***********************************************************************
1123  *           SetArcDirection    (GDI32.@)
1124  */
1125 INT WINAPI SetArcDirection( HDC hdc, INT nDirection )
1126 {
1127     DC * dc;
1128     INT nOldDirection = 0;
1129
1130     if (nDirection!=AD_COUNTERCLOCKWISE && nDirection!=AD_CLOCKWISE)
1131     {
1132         SetLastError(ERROR_INVALID_PARAMETER);
1133         return 0;
1134     }
1135
1136     if ((dc = DC_GetDCPtr( hdc )))
1137     {
1138         if (dc->funcs->pSetArcDirection)
1139         {
1140             dc->funcs->pSetArcDirection(dc->physDev, nDirection);
1141         }
1142         nOldDirection = dc->ArcDirection;
1143         dc->ArcDirection = nDirection;
1144         GDI_ReleaseObj( hdc );
1145     }
1146     return nOldDirection;
1147 }
1148
1149
1150 /***********************************************************************
1151  *           GetWorldTransform    (GDI32.@)
1152  */
1153 BOOL WINAPI GetWorldTransform( HDC hdc, LPXFORM xform )
1154 {
1155     DC * dc;
1156     if (!xform) return FALSE;
1157     if (!(dc = DC_GetDCPtr( hdc ))) return FALSE;
1158     *xform = dc->xformWorld2Wnd;
1159     GDI_ReleaseObj( hdc );
1160     return TRUE;
1161 }
1162
1163
1164 /***********************************************************************
1165  *           GetTransform    (GDI32.@)
1166  */
1167 BOOL WINAPI GetTransform( HDC hdc, DWORD unknown, LPXFORM xform )
1168 {
1169     if (unknown == 0x0203) return GetWorldTransform( hdc, xform );
1170     FIXME("stub: don't know what to do for code %x\n", unknown );
1171     return FALSE;
1172 }
1173
1174
1175 /***********************************************************************
1176  *           SetWorldTransform    (GDI32.@)
1177  */
1178 BOOL WINAPI SetWorldTransform( HDC hdc, const XFORM *xform )
1179 {
1180     BOOL ret = FALSE;
1181     DC *dc = DC_GetDCPtr( hdc );
1182
1183     if (!dc) return FALSE;
1184     if (!xform) goto done;
1185
1186     /* Check that graphics mode is GM_ADVANCED */
1187     if (dc->GraphicsMode!=GM_ADVANCED) goto done;
1188
1189     if (dc->funcs->pSetWorldTransform)
1190     {
1191         ret = dc->funcs->pSetWorldTransform(dc->physDev, xform);
1192         if (!ret) goto done;
1193     }
1194
1195     dc->xformWorld2Wnd = *xform;
1196     DC_UpdateXforms( dc );
1197     ret = TRUE;
1198  done:
1199     GDI_ReleaseObj( hdc );
1200     return ret;
1201 }
1202
1203
1204 /****************************************************************************
1205  * ModifyWorldTransform [GDI32.@]
1206  * Modifies the world transformation for a device context.
1207  *
1208  * PARAMS
1209  *    hdc   [I] Handle to device context
1210  *    xform [I] XFORM structure that will be used to modify the world
1211  *              transformation
1212  *    iMode [I] Specifies in what way to modify the world transformation
1213  *              Possible values:
1214  *              MWT_IDENTITY
1215  *                 Resets the world transformation to the identity matrix.
1216  *                 The parameter xform is ignored.
1217  *              MWT_LEFTMULTIPLY
1218  *                 Multiplies xform into the world transformation matrix from
1219  *                 the left.
1220  *              MWT_RIGHTMULTIPLY
1221  *                 Multiplies xform into the world transformation matrix from
1222  *                 the right.
1223  *
1224  * RETURNS
1225  *  Success: TRUE.
1226  *  Failure: FALSE. Use GetLastError() to determine the cause.
1227  */
1228 BOOL WINAPI ModifyWorldTransform( HDC hdc, const XFORM *xform,
1229     DWORD iMode )
1230 {
1231     BOOL ret = FALSE;
1232     DC *dc = DC_GetDCPtr( hdc );
1233
1234     /* Check for illegal parameters */
1235     if (!dc) return FALSE;
1236     if (!xform && iMode != MWT_IDENTITY) goto done;
1237
1238     /* Check that graphics mode is GM_ADVANCED */
1239     if (dc->GraphicsMode!=GM_ADVANCED) goto done;
1240
1241     if (dc->funcs->pModifyWorldTransform)
1242     {
1243         ret = dc->funcs->pModifyWorldTransform(dc->physDev, xform, iMode);
1244         if (!ret) goto done;
1245     }
1246
1247     switch (iMode)
1248     {
1249         case MWT_IDENTITY:
1250             dc->xformWorld2Wnd.eM11 = 1.0f;
1251             dc->xformWorld2Wnd.eM12 = 0.0f;
1252             dc->xformWorld2Wnd.eM21 = 0.0f;
1253             dc->xformWorld2Wnd.eM22 = 1.0f;
1254             dc->xformWorld2Wnd.eDx  = 0.0f;
1255             dc->xformWorld2Wnd.eDy  = 0.0f;
1256             break;
1257         case MWT_LEFTMULTIPLY:
1258             CombineTransform( &dc->xformWorld2Wnd, xform,
1259                 &dc->xformWorld2Wnd );
1260             break;
1261         case MWT_RIGHTMULTIPLY:
1262             CombineTransform( &dc->xformWorld2Wnd, &dc->xformWorld2Wnd,
1263                 xform );
1264             break;
1265         default:
1266             goto done;
1267     }
1268
1269     DC_UpdateXforms( dc );
1270     ret = TRUE;
1271  done:
1272     GDI_ReleaseObj( hdc );
1273     return ret;
1274 }
1275
1276
1277 /****************************************************************************
1278  * CombineTransform [GDI32.@]
1279  * Combines two transformation matrices.
1280  *
1281  * PARAMS
1282  *    xformResult [O] Stores the result of combining the two matrices
1283  *    xform1      [I] Specifies the first matrix to apply
1284  *    xform2      [I] Specifies the second matrix to apply
1285  *
1286  * REMARKS
1287  *    The same matrix can be passed in for more than one of the parameters.
1288  *
1289  * RETURNS
1290  *  Success: TRUE.
1291  *  Failure: FALSE. Use GetLastError() to determine the cause.
1292  */
1293 BOOL WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
1294     const XFORM *xform2 )
1295 {
1296     XFORM xformTemp;
1297
1298     /* Check for illegal parameters */
1299     if (!xformResult || !xform1 || !xform2)
1300         return FALSE;
1301
1302     /* Create the result in a temporary XFORM, since xformResult may be
1303      * equal to xform1 or xform2 */
1304     xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
1305                      xform1->eM12 * xform2->eM21;
1306     xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
1307                      xform1->eM12 * xform2->eM22;
1308     xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
1309                      xform1->eM22 * xform2->eM21;
1310     xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
1311                      xform1->eM22 * xform2->eM22;
1312     xformTemp.eDx  = xform1->eDx  * xform2->eM11 +
1313                      xform1->eDy  * xform2->eM21 +
1314                      xform2->eDx;
1315     xformTemp.eDy  = xform1->eDx  * xform2->eM12 +
1316                      xform1->eDy  * xform2->eM22 +
1317                      xform2->eDy;
1318
1319     /* Copy the result to xformResult */
1320     *xformResult = xformTemp;
1321
1322     return TRUE;
1323 }
1324
1325
1326 /***********************************************************************
1327  *           SetDCHook   (GDI32.@)
1328  *
1329  * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1330  */
1331 BOOL WINAPI SetDCHook( HDC hdc, DCHOOKPROC hookProc, DWORD dwHookData )
1332 {
1333     DC *dc = GDI_GetObjPtr( hdc, DC_MAGIC );
1334
1335     if (!dc) return FALSE;
1336
1337     if (!(dc->flags & DC_SAVED))
1338     {
1339         dc->dwHookData = dwHookData;
1340         dc->hookThunk = hookProc;
1341     }
1342     GDI_ReleaseObj( hdc );
1343     return TRUE;
1344 }
1345
1346
1347 /* relay function to call the 16-bit DC hook proc */
1348 static BOOL16 WINAPI call_dc_hook16( HDC16 hdc16, WORD code, DWORD data, LPARAM lParam )
1349 {
1350     WORD args[6];
1351     DWORD ret;
1352     FARPROC16 proc = NULL;
1353     HDC hdc = HDC_32( hdc16 );
1354     DC *dc = DC_GetDCPtr( hdc );
1355
1356     if (!dc) return FALSE;
1357     proc = dc->hookProc;
1358     GDI_ReleaseObj( hdc );
1359     if (!proc) return FALSE;
1360     args[5] = hdc16;
1361     args[4] = code;
1362     args[3] = HIWORD(data);
1363     args[2] = LOWORD(data);
1364     args[1] = HIWORD(lParam);
1365     args[0] = LOWORD(lParam);
1366     WOWCallback16Ex( (DWORD)proc, WCB16_PASCAL, sizeof(args), args, &ret );
1367     return LOWORD(ret);
1368 }
1369
1370 /***********************************************************************
1371  *           SetDCHook   (GDI.190)
1372  */
1373 BOOL16 WINAPI SetDCHook16( HDC16 hdc16, FARPROC16 hookProc, DWORD dwHookData )
1374 {
1375     HDC hdc = HDC_32( hdc16 );
1376     DC *dc = DC_GetDCPtr( hdc );
1377     if (!dc) return FALSE;
1378
1379     dc->hookProc = hookProc;
1380     GDI_ReleaseObj( hdc );
1381     return SetDCHook( hdc, call_dc_hook16, dwHookData );
1382 }
1383
1384
1385 /***********************************************************************
1386  *           GetDCHook   (GDI.191)
1387  */
1388 DWORD WINAPI GetDCHook16( HDC16 hdc16, FARPROC16 *phookProc )
1389 {
1390     HDC hdc = HDC_32( hdc16 );
1391     DC *dc = DC_GetDCPtr( hdc );
1392     DWORD ret;
1393
1394     if (!dc) return 0;
1395     *phookProc = dc->hookProc;
1396     ret = dc->dwHookData;
1397     GDI_ReleaseObj( hdc );
1398     return ret;
1399 }
1400
1401
1402 /***********************************************************************
1403  *           SetHookFlags   (GDI.192)
1404  */
1405 WORD WINAPI SetHookFlags16(HDC16 hdc16, WORD flags)
1406 {
1407     HDC hdc = HDC_32( hdc16 );
1408     DC *dc = DC_GetDCPtr( hdc );
1409
1410     if( dc )
1411     {
1412         WORD wRet = dc->flags & DC_DIRTY;
1413
1414         /* "Undocumented Windows" info is slightly confusing.
1415          */
1416
1417         TRACE("hDC %p, flags %04x\n",hdc,flags);
1418
1419         if( flags & DCHF_INVALIDATEVISRGN )
1420             dc->flags |= DC_DIRTY;
1421         else if( flags & DCHF_VALIDATEVISRGN || !flags )
1422             dc->flags &= ~DC_DIRTY;
1423         GDI_ReleaseObj( hdc );
1424         return wRet;
1425     }
1426     return 0;
1427 }
1428
1429 /***********************************************************************
1430  *           SetICMMode    (GDI32.@)
1431  */
1432 INT WINAPI SetICMMode(HDC hdc, INT iEnableICM)
1433 {
1434 /*FIXME  Asuming that ICM is always off, and cannot be turned on */
1435     if (iEnableICM == ICM_OFF) return ICM_OFF;
1436     if (iEnableICM == ICM_ON) return 0;
1437     if (iEnableICM == ICM_QUERY) return ICM_OFF;
1438     return 0;
1439 }
1440
1441 /***********************************************************************
1442  *           GetDeviceGammaRamp    (GDI32.@)
1443  */
1444 BOOL WINAPI GetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1445 {
1446     BOOL ret = FALSE;
1447     DC *dc = DC_GetDCPtr( hDC );
1448
1449     if( dc )
1450     {
1451         if (dc->funcs->pGetDeviceGammaRamp)
1452             ret = dc->funcs->pGetDeviceGammaRamp(dc->physDev, ptr);
1453         GDI_ReleaseObj( hDC );
1454     }
1455     return ret;
1456 }
1457
1458 /***********************************************************************
1459  *           SetDeviceGammaRamp    (GDI32.@)
1460  */
1461 BOOL WINAPI SetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1462 {
1463     BOOL ret = FALSE;
1464     DC *dc = DC_GetDCPtr( hDC );
1465
1466     if( dc )
1467     {
1468         if (dc->funcs->pSetDeviceGammaRamp)
1469             ret = dc->funcs->pSetDeviceGammaRamp(dc->physDev, ptr);
1470         GDI_ReleaseObj( hDC );
1471     }
1472     return ret;
1473 }
1474
1475 /***********************************************************************
1476  *           GetColorSpace    (GDI32.@)
1477  */
1478 HCOLORSPACE WINAPI GetColorSpace(HDC hdc)
1479 {
1480 /*FIXME    Need to to whatever GetColorSpace actually does */
1481     return 0;
1482 }
1483
1484 /***********************************************************************
1485  *           CreateColorSpaceA    (GDI32.@)
1486  */
1487 HCOLORSPACE WINAPI CreateColorSpaceA( LPLOGCOLORSPACEA lpLogColorSpace )
1488 {
1489   FIXME( "stub\n" );
1490   return 0;
1491 }
1492
1493 /***********************************************************************
1494  *           CreateColorSpaceW    (GDI32.@)
1495  */
1496 HCOLORSPACE WINAPI CreateColorSpaceW( LPLOGCOLORSPACEW lpLogColorSpace )
1497 {
1498   FIXME( "stub\n" );
1499   return 0;
1500 }
1501
1502 /***********************************************************************
1503  *           DeleteColorSpace     (GDI32.@)
1504  */
1505 BOOL WINAPI DeleteColorSpace( HCOLORSPACE hColorSpace )
1506 {
1507   FIXME( "stub\n" );
1508
1509   return TRUE;
1510 }
1511
1512 /***********************************************************************
1513  *           SetColorSpace     (GDI32.@)
1514  */
1515 HCOLORSPACE WINAPI SetColorSpace( HDC hDC, HCOLORSPACE hColorSpace )
1516 {
1517   FIXME( "stub\n" );
1518
1519   return hColorSpace;
1520 }
1521
1522 /***********************************************************************
1523  *           GetBoundsRect    (GDI32.@)
1524  */
1525 UINT WINAPI GetBoundsRect(HDC hdc, LPRECT rect, UINT flags)
1526 {
1527     UINT ret;
1528     DC *dc = DC_GetDCPtr( hdc );
1529
1530     if ( !dc ) return 0;
1531
1532     if (rect) *rect = dc->BoundsRect;
1533
1534     ret = ((dc->flags & DC_BOUNDS_SET) ? DCB_SET : DCB_RESET);
1535
1536     if (flags & DCB_RESET)
1537     {
1538         dc->BoundsRect.left   = 0;
1539         dc->BoundsRect.top    = 0;
1540         dc->BoundsRect.right  = 0;
1541         dc->BoundsRect.bottom = 0;
1542         dc->flags &= ~DC_BOUNDS_SET;
1543     }
1544     GDI_ReleaseObj( hdc );
1545     return ret;
1546 }
1547
1548
1549 /***********************************************************************
1550  *           SetBoundsRect    (GDI32.@)
1551  */
1552 UINT WINAPI SetBoundsRect(HDC hdc, const RECT* rect, UINT flags)
1553 {
1554     UINT ret;
1555     DC *dc;
1556
1557     if ((flags & DCB_ENABLE) && (flags & DCB_DISABLE)) return 0;
1558     if (!(dc = DC_GetDCPtr( hdc ))) return 0;
1559
1560     ret = ((dc->flags & DC_BOUNDS_ENABLE) ? DCB_ENABLE : DCB_DISABLE) |
1561           ((dc->flags & DC_BOUNDS_SET) ? DCB_SET : DCB_RESET);
1562
1563     if (flags & DCB_RESET)
1564     {
1565         dc->BoundsRect.left   = 0;
1566         dc->BoundsRect.top    = 0;
1567         dc->BoundsRect.right  = 0;
1568         dc->BoundsRect.bottom = 0;
1569         dc->flags &= ~DC_BOUNDS_SET;
1570     }
1571
1572     if ((flags & DCB_ACCUMULATE) && rect && rect->left < rect->right && rect->top < rect->bottom)
1573     {
1574         if (dc->flags & DC_BOUNDS_SET)
1575         {
1576             dc->BoundsRect.left   = min( dc->BoundsRect.left, rect->left );
1577             dc->BoundsRect.top    = min( dc->BoundsRect.top, rect->top );
1578             dc->BoundsRect.right  = max( dc->BoundsRect.right, rect->right );
1579             dc->BoundsRect.bottom = max( dc->BoundsRect.bottom, rect->bottom );
1580         }
1581         else
1582         {
1583             dc->BoundsRect = *rect;
1584             dc->flags |= DC_BOUNDS_SET;
1585         }
1586     }
1587
1588     if (flags & DCB_ENABLE) dc->flags |= DC_BOUNDS_ENABLE;
1589     if (flags & DCB_DISABLE) dc->flags &= ~DC_BOUNDS_ENABLE;
1590
1591     GDI_ReleaseObj( hdc );
1592     return ret;
1593 }
1594
1595
1596 /***********************************************************************
1597  *              GetRelAbs               (GDI32.@)
1598  */
1599 INT WINAPI GetRelAbs( HDC hdc, DWORD dwIgnore )
1600 {
1601     INT ret = 0;
1602     DC *dc = DC_GetDCPtr( hdc );
1603     if (dc) ret = dc->relAbsMode;
1604     GDI_ReleaseObj( hdc );
1605     return ret;
1606 }
1607
1608
1609
1610
1611 /***********************************************************************
1612  *              GetBkMode (GDI32.@)
1613  */
1614 INT WINAPI GetBkMode( HDC hdc )
1615 {
1616     INT ret = 0;
1617     DC * dc = DC_GetDCPtr( hdc );
1618     if (dc)
1619     {
1620         ret = dc->backgroundMode;
1621         GDI_ReleaseObj( hdc );
1622     }
1623     return ret;
1624 }
1625
1626
1627 /***********************************************************************
1628  *              SetBkMode (GDI32.@)
1629  */
1630 INT WINAPI SetBkMode( HDC hdc, INT mode )
1631 {
1632     INT ret;
1633     DC *dc;
1634     if ((mode <= 0) || (mode > BKMODE_LAST))
1635     {
1636         SetLastError(ERROR_INVALID_PARAMETER);
1637         return 0;
1638     }
1639     if (!(dc = DC_GetDCPtr( hdc ))) return 0;
1640
1641     ret = dc->backgroundMode;
1642     if (dc->funcs->pSetBkMode)
1643         if (!dc->funcs->pSetBkMode( dc->physDev, mode ))
1644             ret = 0;
1645     if (ret)
1646         dc->backgroundMode = mode;
1647     GDI_ReleaseObj( hdc );
1648     return ret;
1649 }
1650
1651
1652 /***********************************************************************
1653  *              GetROP2 (GDI32.@)
1654  */
1655 INT WINAPI GetROP2( HDC hdc )
1656 {
1657     INT ret = 0;
1658     DC * dc = DC_GetDCPtr( hdc );
1659     if (dc)
1660     {
1661         ret = dc->ROPmode;
1662         GDI_ReleaseObj( hdc );
1663     }
1664     return ret;
1665 }
1666
1667
1668 /***********************************************************************
1669  *              SetROP2 (GDI32.@)
1670  */
1671 INT WINAPI SetROP2( HDC hdc, INT mode )
1672 {
1673     INT ret;
1674     DC *dc;
1675     if ((mode < R2_BLACK) || (mode > R2_WHITE))
1676     {
1677         SetLastError(ERROR_INVALID_PARAMETER);
1678         return 0;
1679     }
1680     if (!(dc = DC_GetDCPtr( hdc ))) return 0;
1681     ret = dc->ROPmode;
1682     if (dc->funcs->pSetROP2)
1683         if (!dc->funcs->pSetROP2( dc->physDev, mode ))
1684             ret = 0;
1685     if (ret)
1686         dc->ROPmode = mode;
1687     GDI_ReleaseObj( hdc );
1688     return ret;
1689 }
1690
1691
1692 /***********************************************************************
1693  *              SetRelAbs (GDI32.@)
1694  */
1695 INT WINAPI SetRelAbs( HDC hdc, INT mode )
1696 {
1697     INT ret;
1698     DC *dc;
1699     if ((mode != ABSOLUTE) && (mode != RELATIVE))
1700     {
1701         SetLastError(ERROR_INVALID_PARAMETER);
1702         return 0;
1703     }
1704     if (!(dc = DC_GetDCPtr( hdc ))) return 0;
1705     if (dc->funcs->pSetRelAbs)
1706         ret = dc->funcs->pSetRelAbs( dc->physDev, mode );
1707     else
1708     {
1709         ret = dc->relAbsMode;
1710         dc->relAbsMode = mode;
1711     }
1712     GDI_ReleaseObj( hdc );
1713     return ret;
1714 }
1715
1716
1717 /***********************************************************************
1718  *              GetPolyFillMode (GDI32.@)
1719  */
1720 INT WINAPI GetPolyFillMode( HDC hdc )
1721 {
1722     INT ret = 0;
1723     DC * dc = DC_GetDCPtr( hdc );
1724     if (dc)
1725     {
1726         ret = dc->polyFillMode;
1727         GDI_ReleaseObj( hdc );
1728     }
1729     return ret;
1730 }
1731
1732
1733 /***********************************************************************
1734  *              SetPolyFillMode (GDI32.@)
1735  */
1736 INT WINAPI SetPolyFillMode( HDC hdc, INT mode )
1737 {
1738     INT ret;
1739     DC *dc;
1740     if ((mode <= 0) || (mode > POLYFILL_LAST))
1741     {
1742         SetLastError(ERROR_INVALID_PARAMETER);
1743         return 0;
1744     }
1745     if (!(dc = DC_GetDCPtr( hdc ))) return 0;
1746     ret = dc->polyFillMode;
1747     if (dc->funcs->pSetPolyFillMode)
1748         if (!dc->funcs->pSetPolyFillMode( dc->physDev, mode ))
1749             ret = 0;
1750     if (ret)
1751         dc->polyFillMode = mode;
1752     GDI_ReleaseObj( hdc );
1753     return ret;
1754 }
1755
1756
1757 /***********************************************************************
1758  *              GetStretchBltMode (GDI32.@)
1759  */
1760 INT WINAPI GetStretchBltMode( HDC hdc )
1761 {
1762     INT ret = 0;
1763     DC * dc = DC_GetDCPtr( hdc );
1764     if (dc)
1765     {
1766         ret = dc->stretchBltMode;
1767         GDI_ReleaseObj( hdc );
1768     }
1769     return ret;
1770 }
1771
1772
1773 /***********************************************************************
1774  *              SetStretchBltMode (GDI32.@)
1775  */
1776 INT WINAPI SetStretchBltMode( HDC hdc, INT mode )
1777 {
1778     INT ret;
1779     DC *dc;
1780     if ((mode <= 0) || (mode > MAXSTRETCHBLTMODE))
1781     {
1782         SetLastError(ERROR_INVALID_PARAMETER);
1783         return 0;
1784     }
1785     if (!(dc = DC_GetDCPtr( hdc ))) return 0;
1786     ret = dc->stretchBltMode;
1787     if (dc->funcs->pSetStretchBltMode)
1788         if (!dc->funcs->pSetStretchBltMode( dc->physDev, mode ))
1789             ret = 0;
1790     if (ret)
1791         dc->stretchBltMode = mode;
1792     GDI_ReleaseObj( hdc );
1793     return ret;
1794 }
1795
1796
1797 /***********************************************************************
1798  *              GetMapMode (GDI32.@)
1799  */
1800 INT WINAPI GetMapMode( HDC hdc )
1801 {
1802     INT ret = 0;
1803     DC * dc = DC_GetDCPtr( hdc );
1804     if (dc)
1805     {
1806         ret = dc->MapMode;
1807         GDI_ReleaseObj( hdc );
1808     }
1809     return ret;
1810 }
1811
1812
1813 /***********************************************************************
1814  *              GetBrushOrgEx (GDI32.@)
1815  */
1816 BOOL WINAPI GetBrushOrgEx( HDC hdc, LPPOINT pt )
1817 {
1818     DC * dc = DC_GetDCPtr( hdc );
1819     if (!dc) return FALSE;
1820     pt->x = dc->brushOrgX;
1821     pt->y = dc->brushOrgY;
1822     GDI_ReleaseObj( hdc );
1823     return TRUE;
1824 }
1825
1826
1827 /***********************************************************************
1828  *              GetCurrentPositionEx (GDI32.@)
1829  */
1830 BOOL WINAPI GetCurrentPositionEx( HDC hdc, LPPOINT pt )
1831 {
1832     DC * dc = DC_GetDCPtr( hdc );
1833     if (!dc) return FALSE;
1834     pt->x = dc->CursPosX;
1835     pt->y = dc->CursPosY;
1836     GDI_ReleaseObj( hdc );
1837     return TRUE;
1838 }
1839
1840
1841 /***********************************************************************
1842  *              GetViewportExtEx (GDI32.@)
1843  */
1844 BOOL WINAPI GetViewportExtEx( HDC hdc, LPSIZE size )
1845 {
1846     DC * dc = DC_GetDCPtr( hdc );
1847     if (!dc) return FALSE;
1848     size->cx = dc->vportExtX;
1849     size->cy = dc->vportExtY;
1850     GDI_ReleaseObj( hdc );
1851     return TRUE;
1852 }
1853
1854
1855 /***********************************************************************
1856  *              GetViewportOrgEx (GDI32.@)
1857  */
1858 BOOL WINAPI GetViewportOrgEx( HDC hdc, LPPOINT pt )
1859 {
1860     DC * dc = DC_GetDCPtr( hdc );
1861     if (!dc) return FALSE;
1862     pt->x = dc->vportOrgX;
1863     pt->y = dc->vportOrgY;
1864     GDI_ReleaseObj( hdc );
1865     return TRUE;
1866 }
1867
1868
1869 /***********************************************************************
1870  *              GetWindowExtEx (GDI32.@)
1871  */
1872 BOOL WINAPI GetWindowExtEx( HDC hdc, LPSIZE size )
1873 {
1874     DC * dc = DC_GetDCPtr( hdc );
1875     if (!dc) return FALSE;
1876     size->cx = dc->wndExtX;
1877     size->cy = dc->wndExtY;
1878     GDI_ReleaseObj( hdc );
1879     return TRUE;
1880 }
1881
1882
1883 /***********************************************************************
1884  *              GetWindowOrgEx (GDI32.@)
1885  */
1886 BOOL WINAPI GetWindowOrgEx( HDC hdc, LPPOINT pt )
1887 {
1888     DC * dc = DC_GetDCPtr( hdc );
1889     if (!dc) return FALSE;
1890     pt->x = dc->wndOrgX;
1891     pt->y = dc->wndOrgY;
1892     GDI_ReleaseObj( hdc );
1893     return TRUE;
1894 }
1895
1896
1897 /***********************************************************************
1898  *              InquireVisRgn   (GDI.131)
1899  */
1900 HRGN16 WINAPI InquireVisRgn16( HDC16 hdc )
1901 {
1902     HRGN16 ret = 0;
1903     DC * dc = DC_GetDCPtr( HDC_32(hdc) );
1904     if (dc)
1905     {
1906         ret = HRGN_16(dc->hVisRgn);
1907         GDI_ReleaseObj( HDC_32(hdc) );
1908     }
1909     return ret;
1910 }
1911
1912
1913 /***********************************************************************
1914  *              GetClipRgn (GDI.173)
1915  */
1916 HRGN16 WINAPI GetClipRgn16( HDC16 hdc )
1917 {
1918     HRGN16 ret = 0;
1919     DC * dc = DC_GetDCPtr( HDC_32(hdc) );
1920     if (dc)
1921     {
1922         ret = HRGN_16(dc->hClipRgn);
1923         GDI_ReleaseObj( HDC_32(hdc) );
1924     }
1925     return ret;
1926 }
1927
1928
1929 /***********************************************************************
1930  *           GetLayout    (GDI32.@)
1931  *
1932  * Gets left->right or right->left text layout flags of a dc.
1933  *
1934  */
1935 DWORD WINAPI GetLayout(HDC hdc)
1936 {
1937     DWORD layout = GDI_ERROR;
1938
1939     DC * dc = DC_GetDCPtr( hdc );
1940     if (dc)
1941     {
1942         layout = dc->layout;
1943         GDI_ReleaseObj( hdc );
1944     }
1945
1946     TRACE("hdc : %p, layout : %08x\n", hdc, layout);
1947
1948     return layout;
1949 }
1950
1951 /***********************************************************************
1952  *           SetLayout    (GDI32.@)
1953  *
1954  * Sets left->right or right->left text layout flags of a dc.
1955  *
1956  */
1957 DWORD WINAPI SetLayout(HDC hdc, DWORD layout)
1958 {
1959     DWORD oldlayout = GDI_ERROR;
1960
1961     DC * dc = DC_GetDCPtr( hdc );
1962     if (dc)
1963     {
1964         oldlayout = dc->layout;
1965         dc->layout = layout;
1966         GDI_ReleaseObj( hdc );
1967     }
1968
1969     TRACE("hdc : %p, old layout : %08x, new layout : %08x\n", hdc, oldlayout, layout);
1970
1971     return oldlayout;
1972 }
1973
1974 /***********************************************************************
1975  *           GetDCBrushColor    (GDI32.@)
1976  *
1977  * Retrieves the current brush color for the specified device
1978  * context (DC).
1979  *
1980  */
1981 COLORREF WINAPI GetDCBrushColor(HDC hdc)
1982 {
1983     DC *dc;
1984     COLORREF dcBrushColor = CLR_INVALID;
1985
1986     TRACE("hdc(%p)\n", hdc);
1987
1988     dc = DC_GetDCPtr( hdc );
1989     if (dc)
1990     {
1991         dcBrushColor = dc->dcBrushColor;
1992         GDI_ReleaseObj( hdc );
1993     }
1994
1995     return dcBrushColor;
1996 }
1997
1998 /***********************************************************************
1999  *           SetDCBrushColor    (GDI32.@)
2000  *
2001  * Sets the current device context (DC) brush color to the specified
2002  * color value. If the device cannot represent the specified color
2003  * value, the color is set to the nearest physical color.
2004  *
2005  */
2006 COLORREF WINAPI SetDCBrushColor(HDC hdc, COLORREF crColor)
2007 {
2008     DC *dc;
2009     COLORREF oldClr = CLR_INVALID;
2010
2011     TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
2012
2013     dc = DC_GetDCPtr( hdc );
2014     if (dc)
2015     {
2016         if (dc->funcs->pSetDCBrushColor)
2017             crColor = dc->funcs->pSetDCBrushColor( dc->physDev, crColor );
2018         else if (dc->hBrush == GetStockObject( DC_BRUSH ))
2019         {
2020             /* If DC_BRUSH is selected, update driver pen color */
2021             HBRUSH hBrush = CreateSolidBrush( crColor );
2022             dc->funcs->pSelectBrush( dc->physDev, hBrush );
2023             DeleteObject( hBrush );
2024         }
2025
2026         if (crColor != CLR_INVALID)
2027         {
2028             oldClr = dc->dcBrushColor;
2029             dc->dcBrushColor = crColor;
2030         }
2031
2032         GDI_ReleaseObj( hdc );
2033     }
2034
2035     return oldClr;
2036 }
2037
2038 /***********************************************************************
2039  *           GetDCPenColor    (GDI32.@)
2040  *
2041  * Retrieves the current pen color for the specified device
2042  * context (DC).
2043  *
2044  */
2045 COLORREF WINAPI GetDCPenColor(HDC hdc)
2046 {
2047     DC *dc;
2048     COLORREF dcPenColor = CLR_INVALID;
2049
2050     TRACE("hdc(%p)\n", hdc);
2051
2052     dc = DC_GetDCPtr( hdc );
2053     if (dc)
2054     {
2055         dcPenColor = dc->dcPenColor;
2056         GDI_ReleaseObj( hdc );
2057     }
2058
2059     return dcPenColor;
2060 }
2061
2062 /***********************************************************************
2063  *           SetDCPenColor    (GDI32.@)
2064  *
2065  * Sets the current device context (DC) pen color to the specified
2066  * color value. If the device cannot represent the specified color
2067  * value, the color is set to the nearest physical color.
2068  *
2069  */
2070 COLORREF WINAPI SetDCPenColor(HDC hdc, COLORREF crColor)
2071 {
2072     DC *dc;
2073     COLORREF oldClr = CLR_INVALID;
2074
2075     TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
2076
2077     dc = DC_GetDCPtr( hdc );
2078     if (dc)
2079     {
2080         if (dc->funcs->pSetDCPenColor)
2081             crColor = dc->funcs->pSetDCPenColor( dc->physDev, crColor );
2082         else if (dc->hPen == GetStockObject( DC_PEN ))
2083         {
2084             /* If DC_PEN is selected, update the driver pen color */
2085             LOGPEN logpen = { PS_SOLID, { 0, 0 }, crColor };
2086             HPEN hPen = CreatePenIndirect( &logpen );
2087             dc->funcs->pSelectPen( dc->physDev, hPen );
2088             DeleteObject( hPen );
2089         }
2090
2091         if (crColor != CLR_INVALID)
2092         {
2093             oldClr = dc->dcPenColor;
2094             dc->dcPenColor = crColor;
2095         }
2096
2097         GDI_ReleaseObj( hdc );
2098     }
2099
2100     return oldClr;
2101 }
2102
2103 /***********************************************************************
2104  *           CancelDC    (GDI32.@)
2105  */
2106 BOOL WINAPI CancelDC(HDC hdc)
2107 {
2108     FIXME("stub\n");
2109     return TRUE;
2110 }
2111
2112 /***********************************************************************
2113  *           SetVirtualResolution   (GDI32.@)
2114  *
2115  * Undocumented on msdn.  Called when PowerPoint XP saves a file.
2116  */
2117 DWORD WINAPI SetVirtualResolution(HDC hdc, DWORD dw2, DWORD dw3, DWORD dw4, DWORD dw5)
2118 {
2119     FIXME("(%p %08x %08x %08x %08x): stub!\n", hdc, dw2, dw3, dw4, dw5);
2120     return FALSE;
2121 }
2122
2123 /*******************************************************************
2124  *      GetMiterLimit [GDI32.@]
2125  *
2126  *
2127  */
2128 BOOL WINAPI GetMiterLimit(HDC hdc, PFLOAT peLimit)
2129 {
2130     BOOL bRet = FALSE;
2131     DC *dc;
2132
2133     TRACE("(%p,%p)\n", hdc, peLimit);
2134
2135     dc = DC_GetDCPtr( hdc );
2136     if (dc)
2137     {
2138         if (peLimit)
2139             *peLimit = dc->miterLimit;
2140
2141         GDI_ReleaseObj( hdc );
2142         bRet = TRUE;
2143     }
2144     return bRet;
2145 }
2146
2147 /*******************************************************************
2148  *      SetMiterLimit [GDI32.@]
2149  *
2150  *
2151  */
2152 BOOL WINAPI SetMiterLimit(HDC hdc, FLOAT eNewLimit, PFLOAT peOldLimit)
2153 {
2154     BOOL bRet = FALSE;
2155     DC *dc;
2156
2157     TRACE("(%p,%f,%p)\n", hdc, eNewLimit, peOldLimit);
2158
2159     dc = DC_GetDCPtr( hdc );
2160     if (dc)
2161     {
2162         if (peOldLimit)
2163             *peOldLimit = dc->miterLimit;
2164         dc->miterLimit = eNewLimit;
2165         GDI_ReleaseObj( hdc );
2166         bRet = TRUE;
2167     }
2168     return bRet;
2169 }
2170
2171 /*******************************************************************
2172  *      GdiIsMetaPrintDC [GDI32.@]
2173  */
2174 BOOL WINAPI GdiIsMetaPrintDC(HDC hdc)
2175 {
2176     FIXME("%p\n", hdc);
2177     return FALSE;
2178 }
2179
2180 /*******************************************************************
2181  *      GdiIsMetaFileDC [GDI32.@]
2182  */
2183 BOOL WINAPI GdiIsMetaFileDC(HDC hdc)
2184 {
2185     TRACE("%p\n", hdc);
2186
2187     switch( GetObjectType( hdc ) )
2188     {
2189     case OBJ_METADC:
2190     case OBJ_ENHMETADC:
2191         return TRUE;
2192     }
2193     return FALSE;
2194 }
2195
2196 /*******************************************************************
2197  *      GdiIsPlayMetafileDC [GDI32.@]
2198  */
2199 BOOL WINAPI GdiIsPlayMetafileDC(HDC hdc)
2200 {
2201     FIXME("%p\n", hdc);
2202     return FALSE;
2203 }