crypt32: Introduce function to encode an array of items as a set.
[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_PTR data = dc->dwHookData;
170             GDI_ReleaseObj( hdc );
171             proc( 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_PTR data = dc->dwHookData;
795         GDI_ReleaseObj( hdc );
796         if (!proc( 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_PTR 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 BOOL WINAPI call_dc_hook16( HDC hdc, WORD code, DWORD_PTR data, LPARAM lParam )
1349 {
1350     WORD args[6];
1351     DWORD ret;
1352     FARPROC16 proc = NULL;
1353     DC *dc = DC_GetDCPtr( hdc );
1354
1355     if (!dc) return FALSE;
1356     proc = dc->hookProc;
1357     GDI_ReleaseObj( hdc );
1358     if (!proc) return FALSE;
1359     args[5] = HDC_16(hdc);
1360     args[4] = code;
1361     args[3] = HIWORD(data);
1362     args[2] = LOWORD(data);
1363     args[1] = HIWORD(lParam);
1364     args[0] = LOWORD(lParam);
1365     WOWCallback16Ex( (DWORD)proc, WCB16_PASCAL, sizeof(args), args, &ret );
1366     return LOWORD(ret);
1367 }
1368
1369 /***********************************************************************
1370  *           SetDCHook   (GDI.190)
1371  */
1372 BOOL16 WINAPI SetDCHook16( HDC16 hdc16, FARPROC16 hookProc, DWORD dwHookData )
1373 {
1374     HDC hdc = HDC_32( hdc16 );
1375     DC *dc = DC_GetDCPtr( hdc );
1376     if (!dc) return FALSE;
1377
1378     dc->hookProc = hookProc;
1379     GDI_ReleaseObj( hdc );
1380     return SetDCHook( hdc, call_dc_hook16, dwHookData );
1381 }
1382
1383
1384 /***********************************************************************
1385  *           GetDCHook   (GDI.191)
1386  */
1387 DWORD WINAPI GetDCHook16( HDC16 hdc16, FARPROC16 *phookProc )
1388 {
1389     HDC hdc = HDC_32( hdc16 );
1390     DC *dc = DC_GetDCPtr( hdc );
1391     DWORD ret;
1392
1393     if (!dc) return 0;
1394     *phookProc = dc->hookProc;
1395     ret = dc->dwHookData;
1396     GDI_ReleaseObj( hdc );
1397     return ret;
1398 }
1399
1400
1401 /***********************************************************************
1402  *           SetHookFlags   (GDI.192)
1403  */
1404 WORD WINAPI SetHookFlags16(HDC16 hdc16, WORD flags)
1405 {
1406     HDC hdc = HDC_32( hdc16 );
1407     DC *dc = DC_GetDCPtr( hdc );
1408
1409     if( dc )
1410     {
1411         WORD wRet = dc->flags & DC_DIRTY;
1412
1413         /* "Undocumented Windows" info is slightly confusing.
1414          */
1415
1416         TRACE("hDC %p, flags %04x\n",hdc,flags);
1417
1418         if( flags & DCHF_INVALIDATEVISRGN )
1419             dc->flags |= DC_DIRTY;
1420         else if( flags & DCHF_VALIDATEVISRGN || !flags )
1421             dc->flags &= ~DC_DIRTY;
1422         GDI_ReleaseObj( hdc );
1423         return wRet;
1424     }
1425     return 0;
1426 }
1427
1428 /***********************************************************************
1429  *           SetICMMode    (GDI32.@)
1430  */
1431 INT WINAPI SetICMMode(HDC hdc, INT iEnableICM)
1432 {
1433 /*FIXME  Asuming that ICM is always off, and cannot be turned on */
1434     if (iEnableICM == ICM_OFF) return ICM_OFF;
1435     if (iEnableICM == ICM_ON) return 0;
1436     if (iEnableICM == ICM_QUERY) return ICM_OFF;
1437     return 0;
1438 }
1439
1440 /***********************************************************************
1441  *           GetDeviceGammaRamp    (GDI32.@)
1442  */
1443 BOOL WINAPI GetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1444 {
1445     BOOL ret = FALSE;
1446     DC *dc = DC_GetDCPtr( hDC );
1447
1448     if( dc )
1449     {
1450         if (dc->funcs->pGetDeviceGammaRamp)
1451             ret = dc->funcs->pGetDeviceGammaRamp(dc->physDev, ptr);
1452         GDI_ReleaseObj( hDC );
1453     }
1454     return ret;
1455 }
1456
1457 /***********************************************************************
1458  *           SetDeviceGammaRamp    (GDI32.@)
1459  */
1460 BOOL WINAPI SetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1461 {
1462     BOOL ret = FALSE;
1463     DC *dc = DC_GetDCPtr( hDC );
1464
1465     if( dc )
1466     {
1467         if (dc->funcs->pSetDeviceGammaRamp)
1468             ret = dc->funcs->pSetDeviceGammaRamp(dc->physDev, ptr);
1469         GDI_ReleaseObj( hDC );
1470     }
1471     return ret;
1472 }
1473
1474 /***********************************************************************
1475  *           GetColorSpace    (GDI32.@)
1476  */
1477 HCOLORSPACE WINAPI GetColorSpace(HDC hdc)
1478 {
1479 /*FIXME    Need to to whatever GetColorSpace actually does */
1480     return 0;
1481 }
1482
1483 /***********************************************************************
1484  *           CreateColorSpaceA    (GDI32.@)
1485  */
1486 HCOLORSPACE WINAPI CreateColorSpaceA( LPLOGCOLORSPACEA lpLogColorSpace )
1487 {
1488   FIXME( "stub\n" );
1489   return 0;
1490 }
1491
1492 /***********************************************************************
1493  *           CreateColorSpaceW    (GDI32.@)
1494  */
1495 HCOLORSPACE WINAPI CreateColorSpaceW( LPLOGCOLORSPACEW lpLogColorSpace )
1496 {
1497   FIXME( "stub\n" );
1498   return 0;
1499 }
1500
1501 /***********************************************************************
1502  *           DeleteColorSpace     (GDI32.@)
1503  */
1504 BOOL WINAPI DeleteColorSpace( HCOLORSPACE hColorSpace )
1505 {
1506   FIXME( "stub\n" );
1507
1508   return TRUE;
1509 }
1510
1511 /***********************************************************************
1512  *           SetColorSpace     (GDI32.@)
1513  */
1514 HCOLORSPACE WINAPI SetColorSpace( HDC hDC, HCOLORSPACE hColorSpace )
1515 {
1516   FIXME( "stub\n" );
1517
1518   return hColorSpace;
1519 }
1520
1521 /***********************************************************************
1522  *           GetBoundsRect    (GDI32.@)
1523  */
1524 UINT WINAPI GetBoundsRect(HDC hdc, LPRECT rect, UINT flags)
1525 {
1526     UINT ret;
1527     DC *dc = DC_GetDCPtr( hdc );
1528
1529     if ( !dc ) return 0;
1530
1531     if (rect) *rect = dc->BoundsRect;
1532
1533     ret = ((dc->flags & DC_BOUNDS_SET) ? DCB_SET : DCB_RESET);
1534
1535     if (flags & DCB_RESET)
1536     {
1537         dc->BoundsRect.left   = 0;
1538         dc->BoundsRect.top    = 0;
1539         dc->BoundsRect.right  = 0;
1540         dc->BoundsRect.bottom = 0;
1541         dc->flags &= ~DC_BOUNDS_SET;
1542     }
1543     GDI_ReleaseObj( hdc );
1544     return ret;
1545 }
1546
1547
1548 /***********************************************************************
1549  *           SetBoundsRect    (GDI32.@)
1550  */
1551 UINT WINAPI SetBoundsRect(HDC hdc, const RECT* rect, UINT flags)
1552 {
1553     UINT ret;
1554     DC *dc;
1555
1556     if ((flags & DCB_ENABLE) && (flags & DCB_DISABLE)) return 0;
1557     if (!(dc = DC_GetDCPtr( hdc ))) return 0;
1558
1559     ret = ((dc->flags & DC_BOUNDS_ENABLE) ? DCB_ENABLE : DCB_DISABLE) |
1560           ((dc->flags & DC_BOUNDS_SET) ? DCB_SET : DCB_RESET);
1561
1562     if (flags & DCB_RESET)
1563     {
1564         dc->BoundsRect.left   = 0;
1565         dc->BoundsRect.top    = 0;
1566         dc->BoundsRect.right  = 0;
1567         dc->BoundsRect.bottom = 0;
1568         dc->flags &= ~DC_BOUNDS_SET;
1569     }
1570
1571     if ((flags & DCB_ACCUMULATE) && rect && rect->left < rect->right && rect->top < rect->bottom)
1572     {
1573         if (dc->flags & DC_BOUNDS_SET)
1574         {
1575             dc->BoundsRect.left   = min( dc->BoundsRect.left, rect->left );
1576             dc->BoundsRect.top    = min( dc->BoundsRect.top, rect->top );
1577             dc->BoundsRect.right  = max( dc->BoundsRect.right, rect->right );
1578             dc->BoundsRect.bottom = max( dc->BoundsRect.bottom, rect->bottom );
1579         }
1580         else
1581         {
1582             dc->BoundsRect = *rect;
1583             dc->flags |= DC_BOUNDS_SET;
1584         }
1585     }
1586
1587     if (flags & DCB_ENABLE) dc->flags |= DC_BOUNDS_ENABLE;
1588     if (flags & DCB_DISABLE) dc->flags &= ~DC_BOUNDS_ENABLE;
1589
1590     GDI_ReleaseObj( hdc );
1591     return ret;
1592 }
1593
1594
1595 /***********************************************************************
1596  *              GetRelAbs               (GDI32.@)
1597  */
1598 INT WINAPI GetRelAbs( HDC hdc, DWORD dwIgnore )
1599 {
1600     INT ret = 0;
1601     DC *dc = DC_GetDCPtr( hdc );
1602     if (dc) ret = dc->relAbsMode;
1603     GDI_ReleaseObj( hdc );
1604     return ret;
1605 }
1606
1607
1608
1609
1610 /***********************************************************************
1611  *              GetBkMode (GDI32.@)
1612  */
1613 INT WINAPI GetBkMode( HDC hdc )
1614 {
1615     INT ret = 0;
1616     DC * dc = DC_GetDCPtr( hdc );
1617     if (dc)
1618     {
1619         ret = dc->backgroundMode;
1620         GDI_ReleaseObj( hdc );
1621     }
1622     return ret;
1623 }
1624
1625
1626 /***********************************************************************
1627  *              SetBkMode (GDI32.@)
1628  */
1629 INT WINAPI SetBkMode( HDC hdc, INT mode )
1630 {
1631     INT ret;
1632     DC *dc;
1633     if ((mode <= 0) || (mode > BKMODE_LAST))
1634     {
1635         SetLastError(ERROR_INVALID_PARAMETER);
1636         return 0;
1637     }
1638     if (!(dc = DC_GetDCPtr( hdc ))) return 0;
1639
1640     ret = dc->backgroundMode;
1641     if (dc->funcs->pSetBkMode)
1642         if (!dc->funcs->pSetBkMode( dc->physDev, mode ))
1643             ret = 0;
1644     if (ret)
1645         dc->backgroundMode = mode;
1646     GDI_ReleaseObj( hdc );
1647     return ret;
1648 }
1649
1650
1651 /***********************************************************************
1652  *              GetROP2 (GDI32.@)
1653  */
1654 INT WINAPI GetROP2( HDC hdc )
1655 {
1656     INT ret = 0;
1657     DC * dc = DC_GetDCPtr( hdc );
1658     if (dc)
1659     {
1660         ret = dc->ROPmode;
1661         GDI_ReleaseObj( hdc );
1662     }
1663     return ret;
1664 }
1665
1666
1667 /***********************************************************************
1668  *              SetROP2 (GDI32.@)
1669  */
1670 INT WINAPI SetROP2( HDC hdc, INT mode )
1671 {
1672     INT ret;
1673     DC *dc;
1674     if ((mode < R2_BLACK) || (mode > R2_WHITE))
1675     {
1676         SetLastError(ERROR_INVALID_PARAMETER);
1677         return 0;
1678     }
1679     if (!(dc = DC_GetDCPtr( hdc ))) return 0;
1680     ret = dc->ROPmode;
1681     if (dc->funcs->pSetROP2)
1682         if (!dc->funcs->pSetROP2( dc->physDev, mode ))
1683             ret = 0;
1684     if (ret)
1685         dc->ROPmode = mode;
1686     GDI_ReleaseObj( hdc );
1687     return ret;
1688 }
1689
1690
1691 /***********************************************************************
1692  *              SetRelAbs (GDI32.@)
1693  */
1694 INT WINAPI SetRelAbs( HDC hdc, INT mode )
1695 {
1696     INT ret;
1697     DC *dc;
1698     if ((mode != ABSOLUTE) && (mode != RELATIVE))
1699     {
1700         SetLastError(ERROR_INVALID_PARAMETER);
1701         return 0;
1702     }
1703     if (!(dc = DC_GetDCPtr( hdc ))) return 0;
1704     if (dc->funcs->pSetRelAbs)
1705         ret = dc->funcs->pSetRelAbs( dc->physDev, mode );
1706     else
1707     {
1708         ret = dc->relAbsMode;
1709         dc->relAbsMode = mode;
1710     }
1711     GDI_ReleaseObj( hdc );
1712     return ret;
1713 }
1714
1715
1716 /***********************************************************************
1717  *              GetPolyFillMode (GDI32.@)
1718  */
1719 INT WINAPI GetPolyFillMode( HDC hdc )
1720 {
1721     INT ret = 0;
1722     DC * dc = DC_GetDCPtr( hdc );
1723     if (dc)
1724     {
1725         ret = dc->polyFillMode;
1726         GDI_ReleaseObj( hdc );
1727     }
1728     return ret;
1729 }
1730
1731
1732 /***********************************************************************
1733  *              SetPolyFillMode (GDI32.@)
1734  */
1735 INT WINAPI SetPolyFillMode( HDC hdc, INT mode )
1736 {
1737     INT ret;
1738     DC *dc;
1739     if ((mode <= 0) || (mode > POLYFILL_LAST))
1740     {
1741         SetLastError(ERROR_INVALID_PARAMETER);
1742         return 0;
1743     }
1744     if (!(dc = DC_GetDCPtr( hdc ))) return 0;
1745     ret = dc->polyFillMode;
1746     if (dc->funcs->pSetPolyFillMode)
1747         if (!dc->funcs->pSetPolyFillMode( dc->physDev, mode ))
1748             ret = 0;
1749     if (ret)
1750         dc->polyFillMode = mode;
1751     GDI_ReleaseObj( hdc );
1752     return ret;
1753 }
1754
1755
1756 /***********************************************************************
1757  *              GetStretchBltMode (GDI32.@)
1758  */
1759 INT WINAPI GetStretchBltMode( HDC hdc )
1760 {
1761     INT ret = 0;
1762     DC * dc = DC_GetDCPtr( hdc );
1763     if (dc)
1764     {
1765         ret = dc->stretchBltMode;
1766         GDI_ReleaseObj( hdc );
1767     }
1768     return ret;
1769 }
1770
1771
1772 /***********************************************************************
1773  *              SetStretchBltMode (GDI32.@)
1774  */
1775 INT WINAPI SetStretchBltMode( HDC hdc, INT mode )
1776 {
1777     INT ret;
1778     DC *dc;
1779     if ((mode <= 0) || (mode > MAXSTRETCHBLTMODE))
1780     {
1781         SetLastError(ERROR_INVALID_PARAMETER);
1782         return 0;
1783     }
1784     if (!(dc = DC_GetDCPtr( hdc ))) return 0;
1785     ret = dc->stretchBltMode;
1786     if (dc->funcs->pSetStretchBltMode)
1787         if (!dc->funcs->pSetStretchBltMode( dc->physDev, mode ))
1788             ret = 0;
1789     if (ret)
1790         dc->stretchBltMode = mode;
1791     GDI_ReleaseObj( hdc );
1792     return ret;
1793 }
1794
1795
1796 /***********************************************************************
1797  *              GetMapMode (GDI32.@)
1798  */
1799 INT WINAPI GetMapMode( HDC hdc )
1800 {
1801     INT ret = 0;
1802     DC * dc = DC_GetDCPtr( hdc );
1803     if (dc)
1804     {
1805         ret = dc->MapMode;
1806         GDI_ReleaseObj( hdc );
1807     }
1808     return ret;
1809 }
1810
1811
1812 /***********************************************************************
1813  *              GetBrushOrgEx (GDI32.@)
1814  */
1815 BOOL WINAPI GetBrushOrgEx( HDC hdc, LPPOINT pt )
1816 {
1817     DC * dc = DC_GetDCPtr( hdc );
1818     if (!dc) return FALSE;
1819     pt->x = dc->brushOrgX;
1820     pt->y = dc->brushOrgY;
1821     GDI_ReleaseObj( hdc );
1822     return TRUE;
1823 }
1824
1825
1826 /***********************************************************************
1827  *              GetCurrentPositionEx (GDI32.@)
1828  */
1829 BOOL WINAPI GetCurrentPositionEx( HDC hdc, LPPOINT pt )
1830 {
1831     DC * dc = DC_GetDCPtr( hdc );
1832     if (!dc) return FALSE;
1833     pt->x = dc->CursPosX;
1834     pt->y = dc->CursPosY;
1835     GDI_ReleaseObj( hdc );
1836     return TRUE;
1837 }
1838
1839
1840 /***********************************************************************
1841  *              GetViewportExtEx (GDI32.@)
1842  */
1843 BOOL WINAPI GetViewportExtEx( HDC hdc, LPSIZE size )
1844 {
1845     DC * dc = DC_GetDCPtr( hdc );
1846     if (!dc) return FALSE;
1847     size->cx = dc->vportExtX;
1848     size->cy = dc->vportExtY;
1849     GDI_ReleaseObj( hdc );
1850     return TRUE;
1851 }
1852
1853
1854 /***********************************************************************
1855  *              GetViewportOrgEx (GDI32.@)
1856  */
1857 BOOL WINAPI GetViewportOrgEx( HDC hdc, LPPOINT pt )
1858 {
1859     DC * dc = DC_GetDCPtr( hdc );
1860     if (!dc) return FALSE;
1861     pt->x = dc->vportOrgX;
1862     pt->y = dc->vportOrgY;
1863     GDI_ReleaseObj( hdc );
1864     return TRUE;
1865 }
1866
1867
1868 /***********************************************************************
1869  *              GetWindowExtEx (GDI32.@)
1870  */
1871 BOOL WINAPI GetWindowExtEx( HDC hdc, LPSIZE size )
1872 {
1873     DC * dc = DC_GetDCPtr( hdc );
1874     if (!dc) return FALSE;
1875     size->cx = dc->wndExtX;
1876     size->cy = dc->wndExtY;
1877     GDI_ReleaseObj( hdc );
1878     return TRUE;
1879 }
1880
1881
1882 /***********************************************************************
1883  *              GetWindowOrgEx (GDI32.@)
1884  */
1885 BOOL WINAPI GetWindowOrgEx( HDC hdc, LPPOINT pt )
1886 {
1887     DC * dc = DC_GetDCPtr( hdc );
1888     if (!dc) return FALSE;
1889     pt->x = dc->wndOrgX;
1890     pt->y = dc->wndOrgY;
1891     GDI_ReleaseObj( hdc );
1892     return TRUE;
1893 }
1894
1895
1896 /***********************************************************************
1897  *              InquireVisRgn   (GDI.131)
1898  */
1899 HRGN16 WINAPI InquireVisRgn16( HDC16 hdc )
1900 {
1901     HRGN16 ret = 0;
1902     DC * dc = DC_GetDCPtr( HDC_32(hdc) );
1903     if (dc)
1904     {
1905         ret = HRGN_16(dc->hVisRgn);
1906         GDI_ReleaseObj( HDC_32(hdc) );
1907     }
1908     return ret;
1909 }
1910
1911
1912 /***********************************************************************
1913  *              GetClipRgn (GDI.173)
1914  */
1915 HRGN16 WINAPI GetClipRgn16( HDC16 hdc )
1916 {
1917     HRGN16 ret = 0;
1918     DC * dc = DC_GetDCPtr( HDC_32(hdc) );
1919     if (dc)
1920     {
1921         ret = HRGN_16(dc->hClipRgn);
1922         GDI_ReleaseObj( HDC_32(hdc) );
1923     }
1924     return ret;
1925 }
1926
1927
1928 /***********************************************************************
1929  *           GetLayout    (GDI32.@)
1930  *
1931  * Gets left->right or right->left text layout flags of a dc.
1932  *
1933  */
1934 DWORD WINAPI GetLayout(HDC hdc)
1935 {
1936     DWORD layout = GDI_ERROR;
1937
1938     DC * dc = DC_GetDCPtr( hdc );
1939     if (dc)
1940     {
1941         layout = dc->layout;
1942         GDI_ReleaseObj( hdc );
1943     }
1944
1945     TRACE("hdc : %p, layout : %08x\n", hdc, layout);
1946
1947     return layout;
1948 }
1949
1950 /***********************************************************************
1951  *           SetLayout    (GDI32.@)
1952  *
1953  * Sets left->right or right->left text layout flags of a dc.
1954  *
1955  */
1956 DWORD WINAPI SetLayout(HDC hdc, DWORD layout)
1957 {
1958     DWORD oldlayout = GDI_ERROR;
1959
1960     DC * dc = DC_GetDCPtr( hdc );
1961     if (dc)
1962     {
1963         oldlayout = dc->layout;
1964         dc->layout = layout;
1965         GDI_ReleaseObj( hdc );
1966     }
1967
1968     TRACE("hdc : %p, old layout : %08x, new layout : %08x\n", hdc, oldlayout, layout);
1969
1970     return oldlayout;
1971 }
1972
1973 /***********************************************************************
1974  *           GetDCBrushColor    (GDI32.@)
1975  *
1976  * Retrieves the current brush color for the specified device
1977  * context (DC).
1978  *
1979  */
1980 COLORREF WINAPI GetDCBrushColor(HDC hdc)
1981 {
1982     DC *dc;
1983     COLORREF dcBrushColor = CLR_INVALID;
1984
1985     TRACE("hdc(%p)\n", hdc);
1986
1987     dc = DC_GetDCPtr( hdc );
1988     if (dc)
1989     {
1990         dcBrushColor = dc->dcBrushColor;
1991         GDI_ReleaseObj( hdc );
1992     }
1993
1994     return dcBrushColor;
1995 }
1996
1997 /***********************************************************************
1998  *           SetDCBrushColor    (GDI32.@)
1999  *
2000  * Sets the current device context (DC) brush color to the specified
2001  * color value. If the device cannot represent the specified color
2002  * value, the color is set to the nearest physical color.
2003  *
2004  */
2005 COLORREF WINAPI SetDCBrushColor(HDC hdc, COLORREF crColor)
2006 {
2007     DC *dc;
2008     COLORREF oldClr = CLR_INVALID;
2009
2010     TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
2011
2012     dc = DC_GetDCPtr( hdc );
2013     if (dc)
2014     {
2015         if (dc->funcs->pSetDCBrushColor)
2016             crColor = dc->funcs->pSetDCBrushColor( dc->physDev, crColor );
2017         else if (dc->hBrush == GetStockObject( DC_BRUSH ))
2018         {
2019             /* If DC_BRUSH is selected, update driver pen color */
2020             HBRUSH hBrush = CreateSolidBrush( crColor );
2021             dc->funcs->pSelectBrush( dc->physDev, hBrush );
2022             DeleteObject( hBrush );
2023         }
2024
2025         if (crColor != CLR_INVALID)
2026         {
2027             oldClr = dc->dcBrushColor;
2028             dc->dcBrushColor = crColor;
2029         }
2030
2031         GDI_ReleaseObj( hdc );
2032     }
2033
2034     return oldClr;
2035 }
2036
2037 /***********************************************************************
2038  *           GetDCPenColor    (GDI32.@)
2039  *
2040  * Retrieves the current pen color for the specified device
2041  * context (DC).
2042  *
2043  */
2044 COLORREF WINAPI GetDCPenColor(HDC hdc)
2045 {
2046     DC *dc;
2047     COLORREF dcPenColor = CLR_INVALID;
2048
2049     TRACE("hdc(%p)\n", hdc);
2050
2051     dc = DC_GetDCPtr( hdc );
2052     if (dc)
2053     {
2054         dcPenColor = dc->dcPenColor;
2055         GDI_ReleaseObj( hdc );
2056     }
2057
2058     return dcPenColor;
2059 }
2060
2061 /***********************************************************************
2062  *           SetDCPenColor    (GDI32.@)
2063  *
2064  * Sets the current device context (DC) pen color to the specified
2065  * color value. If the device cannot represent the specified color
2066  * value, the color is set to the nearest physical color.
2067  *
2068  */
2069 COLORREF WINAPI SetDCPenColor(HDC hdc, COLORREF crColor)
2070 {
2071     DC *dc;
2072     COLORREF oldClr = CLR_INVALID;
2073
2074     TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
2075
2076     dc = DC_GetDCPtr( hdc );
2077     if (dc)
2078     {
2079         if (dc->funcs->pSetDCPenColor)
2080             crColor = dc->funcs->pSetDCPenColor( dc->physDev, crColor );
2081         else if (dc->hPen == GetStockObject( DC_PEN ))
2082         {
2083             /* If DC_PEN is selected, update the driver pen color */
2084             LOGPEN logpen = { PS_SOLID, { 0, 0 }, crColor };
2085             HPEN hPen = CreatePenIndirect( &logpen );
2086             dc->funcs->pSelectPen( dc->physDev, hPen );
2087             DeleteObject( hPen );
2088         }
2089
2090         if (crColor != CLR_INVALID)
2091         {
2092             oldClr = dc->dcPenColor;
2093             dc->dcPenColor = crColor;
2094         }
2095
2096         GDI_ReleaseObj( hdc );
2097     }
2098
2099     return oldClr;
2100 }
2101
2102 /***********************************************************************
2103  *           CancelDC    (GDI32.@)
2104  */
2105 BOOL WINAPI CancelDC(HDC hdc)
2106 {
2107     FIXME("stub\n");
2108     return TRUE;
2109 }
2110
2111 /***********************************************************************
2112  *           SetVirtualResolution   (GDI32.@)
2113  *
2114  * Undocumented on msdn.  Called when PowerPoint XP saves a file.
2115  */
2116 DWORD WINAPI SetVirtualResolution(HDC hdc, DWORD dw2, DWORD dw3, DWORD dw4, DWORD dw5)
2117 {
2118     FIXME("(%p %08x %08x %08x %08x): stub!\n", hdc, dw2, dw3, dw4, dw5);
2119     return FALSE;
2120 }
2121
2122 /*******************************************************************
2123  *      GetMiterLimit [GDI32.@]
2124  *
2125  *
2126  */
2127 BOOL WINAPI GetMiterLimit(HDC hdc, PFLOAT peLimit)
2128 {
2129     BOOL bRet = FALSE;
2130     DC *dc;
2131
2132     TRACE("(%p,%p)\n", hdc, peLimit);
2133
2134     dc = DC_GetDCPtr( hdc );
2135     if (dc)
2136     {
2137         if (peLimit)
2138             *peLimit = dc->miterLimit;
2139
2140         GDI_ReleaseObj( hdc );
2141         bRet = TRUE;
2142     }
2143     return bRet;
2144 }
2145
2146 /*******************************************************************
2147  *      SetMiterLimit [GDI32.@]
2148  *
2149  *
2150  */
2151 BOOL WINAPI SetMiterLimit(HDC hdc, FLOAT eNewLimit, PFLOAT peOldLimit)
2152 {
2153     BOOL bRet = FALSE;
2154     DC *dc;
2155
2156     TRACE("(%p,%f,%p)\n", hdc, eNewLimit, peOldLimit);
2157
2158     dc = DC_GetDCPtr( hdc );
2159     if (dc)
2160     {
2161         if (peOldLimit)
2162             *peOldLimit = dc->miterLimit;
2163         dc->miterLimit = eNewLimit;
2164         GDI_ReleaseObj( hdc );
2165         bRet = TRUE;
2166     }
2167     return bRet;
2168 }
2169
2170 /*******************************************************************
2171  *      GdiIsMetaPrintDC [GDI32.@]
2172  */
2173 BOOL WINAPI GdiIsMetaPrintDC(HDC hdc)
2174 {
2175     FIXME("%p\n", hdc);
2176     return FALSE;
2177 }
2178
2179 /*******************************************************************
2180  *      GdiIsMetaFileDC [GDI32.@]
2181  */
2182 BOOL WINAPI GdiIsMetaFileDC(HDC hdc)
2183 {
2184     TRACE("%p\n", hdc);
2185
2186     switch( GetObjectType( hdc ) )
2187     {
2188     case OBJ_METADC:
2189     case OBJ_ENHMETADC:
2190         return TRUE;
2191     }
2192     return FALSE;
2193 }
2194
2195 /*******************************************************************
2196  *      GdiIsPlayMetafileDC [GDI32.@]
2197  */
2198 BOOL WINAPI GdiIsPlayMetafileDC(HDC hdc)
2199 {
2200     FIXME("%p\n", hdc);
2201     return FALSE;
2202 }