Moved a bunch of definitions from gdi.h into a new gdi_private.h to
[wine] / objects / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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.h"
35 #include "gdi_private.h"
36 #include "wine/unicode.h"
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(dc);
40
41 static BOOL DC_DeleteObject( HGDIOBJ handle, void *obj );
42
43 static const struct gdi_obj_funcs dc_funcs =
44 {
45     NULL,             /* pSelectObject */
46     NULL,             /* pGetObject16 */
47     NULL,             /* pGetObjectA */
48     NULL,             /* pGetObjectW */
49     NULL,             /* pUnrealizeObject */
50     DC_DeleteObject   /* pDeleteObject */
51 };
52
53 /***********************************************************************
54  *           DC_AllocDC
55  */
56 DC *DC_AllocDC( const DC_FUNCTIONS *funcs, WORD magic )
57 {
58     HDC hdc;
59     DC *dc;
60
61     if (!(dc = GDI_AllocObject( sizeof(*dc), magic, (HGDIOBJ*)&hdc, &dc_funcs ))) return NULL;
62
63     dc->hSelf               = hdc;
64     dc->funcs               = funcs;
65     dc->physDev             = NULL;
66     dc->saveLevel           = 0;
67     dc->dwHookData          = 0;
68     dc->hookProc            = NULL;
69     dc->hookThunk           = NULL;
70     dc->wndOrgX             = 0;
71     dc->wndOrgY             = 0;
72     dc->wndExtX             = 1;
73     dc->wndExtY             = 1;
74     dc->vportOrgX           = 0;
75     dc->vportOrgY           = 0;
76     dc->vportExtX           = 1;
77     dc->vportExtY           = 1;
78     dc->flags               = 0;
79     dc->hClipRgn            = 0;
80     dc->hVisRgn             = 0;
81     dc->hGCClipRgn          = 0;
82     dc->hPen                = GetStockObject( BLACK_PEN );
83     dc->hBrush              = GetStockObject( WHITE_BRUSH );
84     dc->hFont               = GetStockObject( SYSTEM_FONT );
85     dc->hBitmap             = 0;
86     dc->hDevice             = 0;
87     dc->hPalette            = GetStockObject( DEFAULT_PALETTE );
88     dc->gdiFont             = 0;
89     dc->ROPmode             = R2_COPYPEN;
90     dc->polyFillMode        = ALTERNATE;
91     dc->stretchBltMode      = BLACKONWHITE;
92     dc->relAbsMode          = ABSOLUTE;
93     dc->backgroundMode      = OPAQUE;
94     dc->backgroundColor     = RGB( 255, 255, 255 );
95     dc->dcBrushColor        = RGB( 255, 255, 255 );
96     dc->dcPenColor          = RGB( 0, 0, 0 );
97     dc->textColor           = RGB( 0, 0, 0 );
98     dc->brushOrgX           = 0;
99     dc->brushOrgY           = 0;
100     dc->textAlign           = TA_LEFT | TA_TOP | TA_NOUPDATECP;
101     dc->charExtra           = 0;
102     dc->breakTotalExtra     = 0;
103     dc->breakCount          = 0;
104     dc->breakExtra          = 0;
105     dc->breakRem            = 0;
106     dc->totalExtent.left    = 0;
107     dc->totalExtent.top     = 0;
108     dc->totalExtent.right   = 0;
109     dc->totalExtent.bottom  = 0;
110     dc->bitsPerPixel        = 1;
111     dc->MapMode             = MM_TEXT;
112     dc->GraphicsMode        = GM_COMPATIBLE;
113     dc->pAbortProc          = NULL;
114     dc->CursPosX            = 0;
115     dc->CursPosY            = 0;
116     dc->ArcDirection        = AD_COUNTERCLOCKWISE;
117     dc->xformWorld2Wnd.eM11 = 1.0f;
118     dc->xformWorld2Wnd.eM12 = 0.0f;
119     dc->xformWorld2Wnd.eM21 = 0.0f;
120     dc->xformWorld2Wnd.eM22 = 1.0f;
121     dc->xformWorld2Wnd.eDx  = 0.0f;
122     dc->xformWorld2Wnd.eDy  = 0.0f;
123     dc->xformWorld2Vport    = dc->xformWorld2Wnd;
124     dc->xformVport2World    = dc->xformWorld2Wnd;
125     dc->vport2WorldValid    = TRUE;
126     PATH_InitGdiPath(&dc->path);
127     return dc;
128 }
129
130
131
132 /***********************************************************************
133  *           DC_GetDCPtr
134  */
135 DC *DC_GetDCPtr( HDC hdc )
136 {
137     GDIOBJHDR *ptr = GDI_GetObjPtr( hdc, MAGIC_DONTCARE );
138     if (!ptr) return NULL;
139     if ((GDIMAGIC(ptr->wMagic) == DC_MAGIC) ||
140         (GDIMAGIC(ptr->wMagic) == MEMORY_DC_MAGIC) ||
141         (GDIMAGIC(ptr->wMagic) == METAFILE_DC_MAGIC) ||
142         (GDIMAGIC(ptr->wMagic) == ENHMETAFILE_DC_MAGIC))
143         return (DC *)ptr;
144     GDI_ReleaseObj( hdc );
145     SetLastError( ERROR_INVALID_HANDLE );
146     return NULL;
147 }
148
149 /***********************************************************************
150  *           DC_GetDCUpdate
151  *
152  * Retrieve a DC ptr while making sure the visRgn is updated.
153  * This function may call up to USER so the GDI lock should _not_
154  * be held when calling it.
155  */
156 DC *DC_GetDCUpdate( HDC hdc )
157 {
158     DC *dc = DC_GetDCPtr( hdc );
159     if (!dc) return NULL;
160     while (dc->flags & DC_DIRTY)
161     {
162         DCHOOKPROC proc = dc->hookThunk;
163         dc->flags &= ~DC_DIRTY;
164         if (proc)
165         {
166             DWORD data = dc->dwHookData;
167             GDI_ReleaseObj( hdc );
168             proc( HDC_16(hdc), DCHC_INVALIDVISRGN, data, 0 );
169             if (!(dc = DC_GetDCPtr( hdc ))) break;
170             /* otherwise restart the loop in case it became dirty again in the meantime */
171         }
172     }
173     return dc;
174 }
175
176
177 /***********************************************************************
178  *           DC_DeleteObject
179  */
180 static BOOL DC_DeleteObject( HGDIOBJ handle, void *obj )
181 {
182     GDI_ReleaseObj( handle );
183     return DeleteDC( handle );
184 }
185
186
187 /***********************************************************************
188  *           DC_InitDC
189  *
190  * Setup device-specific DC values for a newly created DC.
191  */
192 void DC_InitDC( DC* dc )
193 {
194     if (dc->funcs->pRealizeDefaultPalette) dc->funcs->pRealizeDefaultPalette( dc->physDev );
195     SetTextColor( dc->hSelf, dc->textColor );
196     SetBkColor( dc->hSelf, dc->backgroundColor );
197     SelectObject( dc->hSelf, dc->hPen );
198     SelectObject( dc->hSelf, dc->hBrush );
199     SelectObject( dc->hSelf, dc->hFont );
200     CLIPPING_UpdateGCRegion( dc );
201 }
202
203
204 /***********************************************************************
205  *           DC_InvertXform
206  *
207  * Computes the inverse of the transformation xformSrc and stores it to
208  * xformDest. Returns TRUE if successful or FALSE if the xformSrc matrix
209  * is singular.
210  */
211 static BOOL DC_InvertXform( const XFORM *xformSrc, XFORM *xformDest )
212 {
213     FLOAT determinant;
214
215     determinant = xformSrc->eM11*xformSrc->eM22 -
216         xformSrc->eM12*xformSrc->eM21;
217     if (determinant > -1e-12 && determinant < 1e-12)
218         return FALSE;
219
220     xformDest->eM11 =  xformSrc->eM22 / determinant;
221     xformDest->eM12 = -xformSrc->eM12 / determinant;
222     xformDest->eM21 = -xformSrc->eM21 / determinant;
223     xformDest->eM22 =  xformSrc->eM11 / determinant;
224     xformDest->eDx  = -xformSrc->eDx * xformDest->eM11 -
225                        xformSrc->eDy * xformDest->eM21;
226     xformDest->eDy  = -xformSrc->eDx * xformDest->eM12 -
227                        xformSrc->eDy * xformDest->eM22;
228
229     return TRUE;
230 }
231
232
233 /***********************************************************************
234  *           DC_UpdateXforms
235  *
236  * Updates the xformWorld2Vport, xformVport2World and vport2WorldValid
237  * fields of the specified DC by creating a transformation that
238  * represents the current mapping mode and combining it with the DC's
239  * world transform. This function should be called whenever the
240  * parameters associated with the mapping mode (window and viewport
241  * extents and origins) or the world transform change.
242  */
243 void DC_UpdateXforms( DC *dc )
244 {
245     XFORM xformWnd2Vport, oldworld2vport;
246     FLOAT scaleX, scaleY;
247
248     /* Construct a transformation to do the window-to-viewport conversion */
249     scaleX = (FLOAT)dc->vportExtX / (FLOAT)dc->wndExtX;
250     scaleY = (FLOAT)dc->vportExtY / (FLOAT)dc->wndExtY;
251     xformWnd2Vport.eM11 = scaleX;
252     xformWnd2Vport.eM12 = 0.0;
253     xformWnd2Vport.eM21 = 0.0;
254     xformWnd2Vport.eM22 = scaleY;
255     xformWnd2Vport.eDx  = (FLOAT)dc->vportOrgX -
256         scaleX * (FLOAT)dc->wndOrgX;
257     xformWnd2Vport.eDy  = (FLOAT)dc->vportOrgY -
258         scaleY * (FLOAT)dc->wndOrgY;
259
260     oldworld2vport = dc->xformWorld2Vport;
261     /* Combine with the world transformation */
262     CombineTransform( &dc->xformWorld2Vport, &dc->xformWorld2Wnd,
263         &xformWnd2Vport );
264
265     /* Create inverse of world-to-viewport transformation */
266     dc->vport2WorldValid = DC_InvertXform( &dc->xformWorld2Vport,
267         &dc->xformVport2World );
268
269     /* Reselect the font back into the dc so that the font size
270        gets updated. */
271     if(memcmp(&oldworld2vport, &dc->xformWorld2Vport, sizeof(oldworld2vport)))
272         SelectObject(dc->hSelf, GetCurrentObject(dc->hSelf, OBJ_FONT));
273 }
274
275
276 /***********************************************************************
277  *           GetDCState   (Not a Windows API)
278  */
279 HDC WINAPI GetDCState( HDC hdc )
280 {
281     DC * newdc, * dc;
282     HGDIOBJ handle;
283
284     if (!(dc = DC_GetDCPtr( hdc ))) return 0;
285     if (!(newdc = GDI_AllocObject( sizeof(DC), GDIMAGIC(dc->header.wMagic), &handle, &dc_funcs )))
286     {
287       GDI_ReleaseObj( hdc );
288       return 0;
289     }
290     TRACE("(%p): returning %p\n", hdc, handle );
291
292     newdc->flags            = dc->flags | DC_SAVED;
293     newdc->hPen             = dc->hPen;
294     newdc->hBrush           = dc->hBrush;
295     newdc->hFont            = dc->hFont;
296     newdc->hBitmap          = dc->hBitmap;
297     newdc->hDevice          = dc->hDevice;
298     newdc->hPalette         = dc->hPalette;
299     newdc->totalExtent      = dc->totalExtent;
300     newdc->bitsPerPixel     = dc->bitsPerPixel;
301     newdc->ROPmode          = dc->ROPmode;
302     newdc->polyFillMode     = dc->polyFillMode;
303     newdc->stretchBltMode   = dc->stretchBltMode;
304     newdc->relAbsMode       = dc->relAbsMode;
305     newdc->backgroundMode   = dc->backgroundMode;
306     newdc->backgroundColor  = dc->backgroundColor;
307     newdc->textColor        = dc->textColor;
308     newdc->dcBrushColor     = dc->dcBrushColor;
309     newdc->dcPenColor       = dc->dcPenColor;
310     newdc->brushOrgX        = dc->brushOrgX;
311     newdc->brushOrgY        = dc->brushOrgY;
312     newdc->textAlign        = dc->textAlign;
313     newdc->charExtra        = dc->charExtra;
314     newdc->breakTotalExtra  = dc->breakTotalExtra;
315     newdc->breakCount       = dc->breakCount;
316     newdc->breakExtra       = dc->breakExtra;
317     newdc->breakRem         = dc->breakRem;
318     newdc->MapMode          = dc->MapMode;
319     newdc->GraphicsMode     = dc->GraphicsMode;
320     newdc->CursPosX         = dc->CursPosX;
321     newdc->CursPosY         = dc->CursPosY;
322     newdc->ArcDirection     = dc->ArcDirection;
323     newdc->xformWorld2Wnd   = dc->xformWorld2Wnd;
324     newdc->xformWorld2Vport = dc->xformWorld2Vport;
325     newdc->xformVport2World = dc->xformVport2World;
326     newdc->vport2WorldValid = dc->vport2WorldValid;
327     newdc->wndOrgX          = dc->wndOrgX;
328     newdc->wndOrgY          = dc->wndOrgY;
329     newdc->wndExtX          = dc->wndExtX;
330     newdc->wndExtY          = dc->wndExtY;
331     newdc->vportOrgX        = dc->vportOrgX;
332     newdc->vportOrgY        = dc->vportOrgY;
333     newdc->vportExtX        = dc->vportExtX;
334     newdc->vportExtY        = dc->vportExtY;
335
336     newdc->hSelf = (HDC)handle;
337     newdc->saveLevel = 0;
338
339     PATH_InitGdiPath( &newdc->path );
340
341     newdc->pAbortProc = NULL;
342     newdc->hookThunk  = NULL;
343     newdc->hookProc   = 0;
344
345     /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
346
347     newdc->hGCClipRgn = newdc->hVisRgn = 0;
348     if (dc->hClipRgn)
349     {
350         newdc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
351         CombineRgn( newdc->hClipRgn, dc->hClipRgn, 0, RGN_COPY );
352     }
353     else
354         newdc->hClipRgn = 0;
355
356     if(dc->gdiFont) {
357         newdc->gdiFont = dc->gdiFont;
358     } else
359         newdc->gdiFont = 0;
360
361     GDI_ReleaseObj( handle );
362     GDI_ReleaseObj( hdc );
363     return handle;
364 }
365
366
367 /***********************************************************************
368  *           SetDCState   (Not a Windows API)
369  */
370 void WINAPI SetDCState( HDC hdc, HDC hdcs )
371 {
372     DC *dc, *dcs;
373
374     if (!(dc = DC_GetDCUpdate( hdc ))) return;
375     if (!(dcs = DC_GetDCPtr( hdcs )))
376     {
377       GDI_ReleaseObj( hdc );
378       return;
379     }
380     if (!dcs->flags & DC_SAVED)
381     {
382       GDI_ReleaseObj( hdc );
383       GDI_ReleaseObj( hdcs );
384       return;
385     }
386     TRACE("%p %p\n", hdc, hdcs );
387
388     dc->flags            = dcs->flags & ~(DC_SAVED | DC_DIRTY);
389     dc->hDevice          = dcs->hDevice;
390     dc->totalExtent      = dcs->totalExtent;
391     dc->ROPmode          = dcs->ROPmode;
392     dc->polyFillMode     = dcs->polyFillMode;
393     dc->stretchBltMode   = dcs->stretchBltMode;
394     dc->relAbsMode       = dcs->relAbsMode;
395     dc->backgroundMode   = dcs->backgroundMode;
396     dc->backgroundColor  = dcs->backgroundColor;
397     dc->textColor        = dcs->textColor;
398     dc->dcBrushColor     = dcs->dcBrushColor;
399     dc->dcPenColor       = dcs->dcPenColor;
400     dc->brushOrgX        = dcs->brushOrgX;
401     dc->brushOrgY        = dcs->brushOrgY;
402     dc->textAlign        = dcs->textAlign;
403     dc->charExtra        = dcs->charExtra;
404     dc->breakTotalExtra  = dcs->breakTotalExtra;
405     dc->breakCount       = dcs->breakCount;
406     dc->breakExtra       = dcs->breakExtra;
407     dc->breakRem         = dcs->breakRem;
408     dc->MapMode          = dcs->MapMode;
409     dc->GraphicsMode     = dcs->GraphicsMode;
410     dc->CursPosX         = dcs->CursPosX;
411     dc->CursPosY         = dcs->CursPosY;
412     dc->ArcDirection     = dcs->ArcDirection;
413     dc->xformWorld2Wnd   = dcs->xformWorld2Wnd;
414     dc->xformWorld2Vport = dcs->xformWorld2Vport;
415     dc->xformVport2World = dcs->xformVport2World;
416     dc->vport2WorldValid = dcs->vport2WorldValid;
417
418     dc->wndOrgX          = dcs->wndOrgX;
419     dc->wndOrgY          = dcs->wndOrgY;
420     dc->wndExtX          = dcs->wndExtX;
421     dc->wndExtY          = dcs->wndExtY;
422     dc->vportOrgX        = dcs->vportOrgX;
423     dc->vportOrgY        = dcs->vportOrgY;
424     dc->vportExtX        = dcs->vportExtX;
425     dc->vportExtY        = dcs->vportExtY;
426
427     if (GDIMAGIC(dc->header.wMagic) != MEMORY_DC_MAGIC) dc->bitsPerPixel = dcs->bitsPerPixel;
428
429     if (dcs->hClipRgn)
430     {
431         if (!dc->hClipRgn) dc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
432         CombineRgn( dc->hClipRgn, dcs->hClipRgn, 0, RGN_COPY );
433     }
434     else
435     {
436         if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
437         dc->hClipRgn = 0;
438     }
439     CLIPPING_UpdateGCRegion( dc );
440
441     SelectObject( hdc, dcs->hBitmap );
442     SelectObject( hdc, dcs->hBrush );
443     SelectObject( hdc, dcs->hFont );
444     SelectObject( hdc, dcs->hPen );
445     SetBkColor( hdc, dcs->backgroundColor);
446     SetTextColor( hdc, dcs->textColor);
447     GDISelectPalette( hdc, dcs->hPalette, FALSE );
448     GDI_ReleaseObj( hdcs );
449     GDI_ReleaseObj( hdc );
450 }
451
452
453 /***********************************************************************
454  *           GetDCState   (GDI.179)
455  */
456 HDC16 WINAPI GetDCState16( HDC16 hdc )
457 {
458     return HDC_16( GetDCState( HDC_32(hdc) ));
459 }
460
461
462 /***********************************************************************
463  *           SetDCState   (GDI.180)
464  */
465 void WINAPI SetDCState16( HDC16 hdc, HDC16 hdcs )
466 {
467     SetDCState( HDC_32(hdc), HDC_32(hdcs) );
468 }
469
470
471 /***********************************************************************
472  *           SaveDC    (GDI32.@)
473  */
474 INT WINAPI SaveDC( HDC hdc )
475 {
476     HDC hdcs;
477     DC * dc, * dcs;
478     INT ret;
479
480     dc = DC_GetDCPtr( hdc );
481     if (!dc) return 0;
482
483     if(dc->funcs->pSaveDC)
484     {
485         ret = dc->funcs->pSaveDC( dc->physDev );
486         GDI_ReleaseObj( hdc );
487         return ret;
488     }
489
490     if (!(hdcs = GetDCState( hdc )))
491     {
492       GDI_ReleaseObj( hdc );
493       return 0;
494     }
495     dcs = DC_GetDCPtr( hdcs );
496
497     /* Copy path. The reason why path saving / restoring is in SaveDC/
498      * RestoreDC and not in GetDCState/SetDCState is that the ...DCState
499      * functions are only in Win16 (which doesn't have paths) and that
500      * SetDCState doesn't allow us to signal an error (which can happen
501      * when copying paths).
502      */
503     if (!PATH_AssignGdiPath( &dcs->path, &dc->path ))
504     {
505         GDI_ReleaseObj( hdc );
506         GDI_ReleaseObj( hdcs );
507         DeleteDC( hdcs );
508         return 0;
509     }
510
511     dcs->header.hNext = dc->header.hNext;
512     dc->header.hNext = HDC_16(hdcs);
513     TRACE("(%p): returning %d\n", hdc, dc->saveLevel+1 );
514     ret = ++dc->saveLevel;
515     GDI_ReleaseObj( hdcs );
516     GDI_ReleaseObj( hdc );
517     return ret;
518 }
519
520
521 /***********************************************************************
522  *           RestoreDC    (GDI32.@)
523  */
524 BOOL WINAPI RestoreDC( HDC hdc, INT level )
525 {
526     DC * dc, * dcs;
527     BOOL success;
528
529     TRACE("%p %d\n", hdc, level );
530     dc = DC_GetDCUpdate( hdc );
531     if(!dc) return FALSE;
532     if(dc->funcs->pRestoreDC)
533     {
534         success = dc->funcs->pRestoreDC( dc->physDev, level );
535         GDI_ReleaseObj( hdc );
536         return success;
537     }
538
539     if (level == -1) level = dc->saveLevel;
540     if ((level < 1)
541             /* This pair of checks disagrees with MSDN "Platform SDK:
542                Windows GDI" July 2000 which says all negative values
543                for level will be interpreted as an instance relative
544                to the current state.  Restricting it to just -1 does
545                not satisfy this */
546         || (level > dc->saveLevel))
547     {
548         GDI_ReleaseObj( hdc );
549         return FALSE;
550     }
551
552     success=TRUE;
553     while (dc->saveLevel >= level)
554     {
555         HDC hdcs = HDC_32(dc->header.hNext);
556         if (!(dcs = DC_GetDCPtr( hdcs )))
557         {
558           GDI_ReleaseObj( hdc );
559           return FALSE;
560         }
561         dc->header.hNext = dcs->header.hNext;
562         if (--dc->saveLevel < level)
563         {
564             SetDCState( hdc, hdcs );
565             if (!PATH_AssignGdiPath( &dc->path, &dcs->path ))
566                 /* FIXME: This might not be quite right, since we're
567                  * returning FALSE but still destroying the saved DC state */
568                 success=FALSE;
569         }
570         GDI_ReleaseObj( hdcs );
571         GDI_ReleaseObj( hdc );
572         DeleteDC( hdcs );
573         if (!(dc = DC_GetDCPtr( hdc ))) return FALSE;
574     }
575     GDI_ReleaseObj( hdc );
576     return success;
577 }
578
579
580 /***********************************************************************
581  *           CreateDCW    (GDI32.@)
582  */
583 HDC WINAPI CreateDCW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
584                       const DEVMODEW *initData )
585 {
586     HDC hdc;
587     DC * dc;
588     const DC_FUNCTIONS *funcs;
589     WCHAR buf[300];
590
591     GDI_CheckNotLock();
592
593     if (!device || !DRIVER_GetDriverName( device, buf, 300 ))
594     {
595         if (!driver) return 0;
596         strcpyW(buf, driver);
597     }
598
599     if (!(funcs = DRIVER_load_driver( buf )))
600     {
601         ERR( "no driver found for %s\n", debugstr_w(buf) );
602         return 0;
603     }
604     if (!(dc = DC_AllocDC( funcs, DC_MAGIC )))
605     {
606         DRIVER_release_driver( funcs );
607         return 0;
608     }
609
610     dc->hBitmap = GetStockObject( DEFAULT_BITMAP );
611
612     TRACE("(driver=%s, device=%s, output=%s): returning %p\n",
613           debugstr_w(driver), debugstr_w(device), debugstr_w(output), dc->hSelf );
614
615     if (dc->funcs->pCreateDC &&
616         !dc->funcs->pCreateDC( dc, &dc->physDev, buf, device, output, initData ))
617     {
618         WARN("creation aborted by device\n" );
619         GDI_FreeObject( dc->hSelf, dc );
620         DRIVER_release_driver( funcs );
621         return 0;
622     }
623
624     dc->totalExtent.left   = 0;
625     dc->totalExtent.top    = 0;
626     dc->totalExtent.right  = GetDeviceCaps( dc->hSelf, HORZRES );
627     dc->totalExtent.bottom = GetDeviceCaps( dc->hSelf, VERTRES );
628     dc->hVisRgn = CreateRectRgnIndirect( &dc->totalExtent );
629
630     DC_InitDC( dc );
631     hdc = dc->hSelf;
632     GDI_ReleaseObj( hdc );
633     return hdc;
634 }
635
636
637 /***********************************************************************
638  *           CreateDCA    (GDI32.@)
639  */
640 HDC WINAPI CreateDCA( LPCSTR driver, LPCSTR device, LPCSTR output,
641                       const DEVMODEA *initData )
642 {
643     UNICODE_STRING driverW, deviceW, outputW;
644     DEVMODEW *initDataW;
645     HDC ret;
646
647     if (driver) RtlCreateUnicodeStringFromAsciiz(&driverW, driver);
648     else driverW.Buffer = NULL;
649
650     if (device) RtlCreateUnicodeStringFromAsciiz(&deviceW, device);
651     else deviceW.Buffer = NULL;
652
653     if (output) RtlCreateUnicodeStringFromAsciiz(&outputW, output);
654     else outputW.Buffer = NULL;
655
656     if (initData) initDataW = GdiConvertToDevmodeW(initData);
657     else initDataW = NULL;
658
659     ret = CreateDCW( driverW.Buffer, deviceW.Buffer, outputW.Buffer, initDataW );
660
661     RtlFreeUnicodeString(&driverW);
662     RtlFreeUnicodeString(&deviceW);
663     RtlFreeUnicodeString(&outputW);
664     if (initDataW) HeapFree(GetProcessHeap(), 0, initDataW);
665     return ret;
666 }
667
668
669 /***********************************************************************
670  *           CreateICA    (GDI32.@)
671  */
672 HDC WINAPI CreateICA( LPCSTR driver, LPCSTR device, LPCSTR output,
673                           const DEVMODEA* initData )
674 {
675       /* Nothing special yet for ICs */
676     return CreateDCA( driver, device, output, initData );
677 }
678
679
680 /***********************************************************************
681  *           CreateICW    (GDI32.@)
682  */
683 HDC WINAPI CreateICW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
684                           const DEVMODEW* initData )
685 {
686       /* Nothing special yet for ICs */
687     return CreateDCW( driver, device, output, initData );
688 }
689
690
691 /***********************************************************************
692  *           CreateCompatibleDC   (GDI32.@)
693  */
694 HDC WINAPI CreateCompatibleDC( HDC hdc )
695 {
696     DC *dc, *origDC;
697     const DC_FUNCTIONS *funcs;
698     PHYSDEV physDev;
699
700     GDI_CheckNotLock();
701
702     if ((origDC = GDI_GetObjPtr( hdc, DC_MAGIC )))
703     {
704         funcs = origDC->funcs;
705         physDev = origDC->physDev;
706         GDI_ReleaseObj( hdc ); /* can't hold the lock while loading the driver */
707         funcs = DRIVER_get_driver( funcs );
708     }
709     else
710     {
711         static const WCHAR displayW[] = { 'd','i','s','p','l','a','y',0 };
712         funcs = DRIVER_load_driver( displayW );
713         physDev = NULL;
714     }
715
716     if (!funcs) return 0;
717
718     if (!(dc = DC_AllocDC( funcs, MEMORY_DC_MAGIC )))
719     {
720         DRIVER_release_driver( funcs );
721         return 0;
722     }
723
724     TRACE("(%p): returning %p\n", hdc, dc->hSelf );
725
726     dc->bitsPerPixel = 1;
727     dc->hBitmap      = GetStockObject( DEFAULT_BITMAP );
728
729     /* Copy the driver-specific physical device info into
730      * the new DC. The driver may use this read-only info
731      * while creating the compatible DC below. */
732     dc->physDev = physDev;
733
734     if (dc->funcs->pCreateDC &&
735         !dc->funcs->pCreateDC( dc, &dc->physDev, NULL, NULL, NULL, NULL ))
736     {
737         WARN("creation aborted by device\n");
738         GDI_FreeObject( dc->hSelf, dc );
739         DRIVER_release_driver( funcs );
740         return 0;
741     }
742
743     dc->totalExtent.left   = 0;
744     dc->totalExtent.top    = 0;
745     dc->totalExtent.right  = 1;  /* default bitmap is 1x1 */
746     dc->totalExtent.bottom = 1;
747     dc->hVisRgn = CreateRectRgnIndirect( &dc->totalExtent );
748
749     DC_InitDC( dc );
750     GDI_ReleaseObj( dc->hSelf );
751     return dc->hSelf;
752 }
753
754
755 /***********************************************************************
756  *           DeleteDC    (GDI32.@)
757  */
758 BOOL WINAPI DeleteDC( HDC hdc )
759 {
760     const DC_FUNCTIONS *funcs = NULL;
761     DC * dc;
762
763     TRACE("%p\n", hdc );
764
765     GDI_CheckNotLock();
766
767     if (!(dc = DC_GetDCPtr( hdc ))) return FALSE;
768
769     /* Call hook procedure to check whether is it OK to delete this DC */
770     if (dc->hookThunk)
771     {
772         DCHOOKPROC proc = dc->hookThunk;
773         DWORD data = dc->dwHookData;
774         GDI_ReleaseObj( hdc );
775         if (!proc( HDC_16(hdc), DCHC_DELETEDC, data, 0 )) return FALSE;
776         if (!(dc = DC_GetDCPtr( hdc ))) return TRUE;  /* deleted by the hook */
777     }
778
779     while (dc->saveLevel)
780     {
781         DC * dcs;
782         HDC hdcs = HDC_32(dc->header.hNext);
783         if (!(dcs = DC_GetDCPtr( hdcs ))) break;
784         dc->header.hNext = dcs->header.hNext;
785         dc->saveLevel--;
786         if (dcs->hClipRgn) DeleteObject( dcs->hClipRgn );
787         if (dcs->hVisRgn) DeleteObject( dcs->hVisRgn );
788         if (dcs->hGCClipRgn) DeleteObject( dcs->hGCClipRgn );
789         PATH_DestroyGdiPath(&dcs->path);
790         GDI_FreeObject( hdcs, dcs );
791     }
792
793     if (!(dc->flags & DC_SAVED))
794     {
795         SelectObject( hdc, GetStockObject(BLACK_PEN) );
796         SelectObject( hdc, GetStockObject(WHITE_BRUSH) );
797         SelectObject( hdc, GetStockObject(SYSTEM_FONT) );
798         SelectObject( hdc, GetStockObject(DEFAULT_BITMAP) );
799         funcs = dc->funcs;
800         if (dc->funcs->pDeleteDC) dc->funcs->pDeleteDC(dc->physDev);
801         dc->physDev = NULL;
802     }
803
804     if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
805     if (dc->hVisRgn) DeleteObject( dc->hVisRgn );
806     if (dc->hGCClipRgn) DeleteObject( dc->hGCClipRgn );
807     PATH_DestroyGdiPath(&dc->path);
808
809     GDI_FreeObject( hdc, dc );
810     if (funcs) DRIVER_release_driver( funcs );  /* do that after releasing the GDI lock */
811     return TRUE;
812 }
813
814
815 /***********************************************************************
816  *           ResetDCW    (GDI32.@)
817  */
818 HDC WINAPI ResetDCW( HDC hdc, const DEVMODEW *devmode )
819 {
820     DC *dc;
821     HDC ret = hdc;
822
823     if ((dc = DC_GetDCPtr( hdc )))
824     {
825         if (dc->funcs->pResetDC) ret = dc->funcs->pResetDC( dc->physDev, devmode );
826         GDI_ReleaseObj( hdc );
827     }
828     return ret;
829 }
830
831
832 /***********************************************************************
833  *           ResetDCA    (GDI32.@)
834  */
835 HDC WINAPI ResetDCA( HDC hdc, const DEVMODEA *devmode )
836 {
837     DEVMODEW *devmodeW;
838     HDC ret;
839
840     if (devmode) devmodeW = GdiConvertToDevmodeW(devmode);
841     else devmodeW = NULL;
842
843     ret = ResetDCW(hdc, devmodeW);
844
845     if (devmodeW) HeapFree(GetProcessHeap(), 0, devmodeW);
846     return ret;
847 }
848
849
850 /***********************************************************************
851  *           GetDeviceCaps    (GDI32.@)
852  */
853 INT WINAPI GetDeviceCaps( HDC hdc, INT cap )
854 {
855     DC *dc;
856     INT ret = 0;
857
858     if ((dc = DC_GetDCPtr( hdc )))
859     {
860         if (dc->funcs->pGetDeviceCaps) ret = dc->funcs->pGetDeviceCaps( dc->physDev, cap );
861         GDI_ReleaseObj( hdc );
862     }
863     return ret;
864 }
865
866
867 /***********************************************************************
868  *           SetBkColor    (GDI32.@)
869  */
870 COLORREF WINAPI SetBkColor( HDC hdc, COLORREF color )
871 {
872     COLORREF oldColor;
873     DC * dc = DC_GetDCPtr( hdc );
874
875     TRACE("hdc=%p color=0x%08lx\n", hdc, color);
876
877     if (!dc) return CLR_INVALID;
878     oldColor = dc->backgroundColor;
879     if (dc->funcs->pSetBkColor)
880     {
881         color = dc->funcs->pSetBkColor(dc->physDev, color);
882         if (color == CLR_INVALID)  /* don't change it */
883         {
884             color = oldColor;
885             oldColor = CLR_INVALID;
886         }
887     }
888     dc->backgroundColor = color;
889     GDI_ReleaseObj( hdc );
890     return oldColor;
891 }
892
893
894 /***********************************************************************
895  *           SetTextColor    (GDI32.@)
896  */
897 COLORREF WINAPI SetTextColor( HDC hdc, COLORREF color )
898 {
899     COLORREF oldColor;
900     DC * dc = DC_GetDCPtr( hdc );
901
902     TRACE(" hdc=%p color=0x%08lx\n", hdc, color);
903
904     if (!dc) return CLR_INVALID;
905     oldColor = dc->textColor;
906     if (dc->funcs->pSetTextColor)
907     {
908         color = dc->funcs->pSetTextColor(dc->physDev, color);
909         if (color == CLR_INVALID)  /* don't change it */
910         {
911             color = oldColor;
912             oldColor = CLR_INVALID;
913         }
914     }
915     dc->textColor = color;
916     GDI_ReleaseObj( hdc );
917     return oldColor;
918 }
919
920
921 /***********************************************************************
922  *           SetTextAlign    (GDI32.@)
923  */
924 UINT WINAPI SetTextAlign( HDC hdc, UINT align )
925 {
926     UINT prevAlign;
927     DC *dc = DC_GetDCPtr( hdc );
928
929     TRACE("hdc=%p align=%d\n", hdc, align);
930
931     if (!dc) return 0x0;
932     if (dc->funcs->pSetTextAlign)
933         prevAlign = dc->funcs->pSetTextAlign(dc->physDev, align);
934     else {
935         prevAlign = dc->textAlign;
936         dc->textAlign = align;
937     }
938     GDI_ReleaseObj( hdc );
939     return prevAlign;
940 }
941
942 /***********************************************************************
943  *           GetDCOrgEx  (GDI32.@)
944  */
945 BOOL WINAPI GetDCOrgEx( HDC hDC, LPPOINT lpp )
946 {
947     DC * dc;
948
949     if (!lpp) return FALSE;
950     if (!(dc = DC_GetDCPtr( hDC ))) return FALSE;
951
952     lpp->x = lpp->y = 0;
953     if (dc->funcs->pGetDCOrgEx) dc->funcs->pGetDCOrgEx( dc->physDev, lpp );
954     GDI_ReleaseObj( hDC );
955     return TRUE;
956 }
957
958
959 /***********************************************************************
960  *           SetDCOrg   (GDI.117)
961  */
962 DWORD WINAPI SetDCOrg16( HDC16 hdc16, INT16 x, INT16 y )
963 {
964     DWORD prevOrg = 0;
965     HDC hdc = HDC_32( hdc16 );
966     DC *dc = DC_GetDCPtr( hdc );
967     if (!dc) return 0;
968     if (dc->funcs->pSetDCOrg) prevOrg = dc->funcs->pSetDCOrg( dc->physDev, x, y );
969     GDI_ReleaseObj( hdc );
970     return prevOrg;
971 }
972
973
974 /***********************************************************************
975  *           SetGraphicsMode    (GDI32.@)
976  */
977 INT WINAPI SetGraphicsMode( HDC hdc, INT mode )
978 {
979     INT ret = 0;
980     DC *dc = DC_GetDCPtr( hdc );
981
982     /* One would think that setting the graphics mode to GM_COMPATIBLE
983      * would also reset the world transformation matrix to the unity
984      * matrix. However, in Windows, this is not the case. This doesn't
985      * make a lot of sense to me, but that's the way it is.
986      */
987     if (!dc) return 0;
988     if ((mode > 0) && (mode <= GM_LAST))
989     {
990         ret = dc->GraphicsMode;
991         dc->GraphicsMode = mode;
992     }
993     GDI_ReleaseObj( hdc );
994     return ret;
995 }
996
997
998 /***********************************************************************
999  *           SetArcDirection    (GDI32.@)
1000  */
1001 INT WINAPI SetArcDirection( HDC hdc, INT nDirection )
1002 {
1003     DC * dc;
1004     INT nOldDirection = 0;
1005
1006     if (nDirection!=AD_COUNTERCLOCKWISE && nDirection!=AD_CLOCKWISE)
1007     {
1008         SetLastError(ERROR_INVALID_PARAMETER);
1009         return 0;
1010     }
1011
1012     if ((dc = DC_GetDCPtr( hdc )))
1013     {
1014         if (dc->funcs->pSetArcDirection)
1015         {
1016             dc->funcs->pSetArcDirection(dc->physDev, nDirection);
1017         }
1018         nOldDirection = dc->ArcDirection;
1019         dc->ArcDirection = nDirection;
1020         GDI_ReleaseObj( hdc );
1021     }
1022     return nOldDirection;
1023 }
1024
1025
1026 /***********************************************************************
1027  *           GetWorldTransform    (GDI32.@)
1028  */
1029 BOOL WINAPI GetWorldTransform( HDC hdc, LPXFORM xform )
1030 {
1031     DC * dc;
1032     if (!xform) return FALSE;
1033     if (!(dc = DC_GetDCPtr( hdc ))) return FALSE;
1034     *xform = dc->xformWorld2Wnd;
1035     GDI_ReleaseObj( hdc );
1036     return TRUE;
1037 }
1038
1039
1040 /***********************************************************************
1041  *           GetTransform    (GDI32.@)
1042  */
1043 BOOL WINAPI GetTransform( HDC hdc, DWORD unknown, LPXFORM xform )
1044 {
1045     if (unknown == 0x0203) return GetWorldTransform( hdc, xform );
1046     FIXME("stub: don't know what to do for code %lx\n", unknown );
1047     return FALSE;
1048 }
1049
1050
1051 /***********************************************************************
1052  *           SetWorldTransform    (GDI32.@)
1053  */
1054 BOOL WINAPI SetWorldTransform( HDC hdc, const XFORM *xform )
1055 {
1056     BOOL ret = FALSE;
1057     DC *dc = DC_GetDCPtr( hdc );
1058
1059     if (!dc) return FALSE;
1060     if (!xform) goto done;
1061
1062     /* Check that graphics mode is GM_ADVANCED */
1063     if (dc->GraphicsMode!=GM_ADVANCED) goto done;
1064
1065     if (dc->funcs->pSetWorldTransform)
1066     {
1067         ret = dc->funcs->pSetWorldTransform(dc->physDev, xform);
1068         if (!ret) goto done;
1069     }
1070
1071     dc->xformWorld2Wnd = *xform;
1072     DC_UpdateXforms( dc );
1073     ret = TRUE;
1074  done:
1075     GDI_ReleaseObj( hdc );
1076     return ret;
1077 }
1078
1079
1080 /****************************************************************************
1081  * ModifyWorldTransform [GDI32.@]
1082  * Modifies the world transformation for a device context.
1083  *
1084  * PARAMS
1085  *    hdc   [I] Handle to device context
1086  *    xform [I] XFORM structure that will be used to modify the world
1087  *              transformation
1088  *    iMode [I] Specifies in what way to modify the world transformation
1089  *              Possible values:
1090  *              MWT_IDENTITY
1091  *                 Resets the world transformation to the identity matrix.
1092  *                 The parameter xform is ignored.
1093  *              MWT_LEFTMULTIPLY
1094  *                 Multiplies xform into the world transformation matrix from
1095  *                 the left.
1096  *              MWT_RIGHTMULTIPLY
1097  *                 Multiplies xform into the world transformation matrix from
1098  *                 the right.
1099  *
1100  * RETURNS STD
1101  */
1102 BOOL WINAPI ModifyWorldTransform( HDC hdc, const XFORM *xform,
1103     DWORD iMode )
1104 {
1105     BOOL ret = FALSE;
1106     DC *dc = DC_GetDCPtr( hdc );
1107
1108     /* Check for illegal parameters */
1109     if (!dc) return FALSE;
1110     if (!xform) goto done;
1111
1112     /* Check that graphics mode is GM_ADVANCED */
1113     if (dc->GraphicsMode!=GM_ADVANCED) goto done;
1114
1115     if (dc->funcs->pModifyWorldTransform)
1116     {
1117         ret = dc->funcs->pModifyWorldTransform(dc->physDev, xform, iMode);
1118         if (!ret) goto done;
1119     }
1120
1121     switch (iMode)
1122     {
1123         case MWT_IDENTITY:
1124             dc->xformWorld2Wnd.eM11 = 1.0f;
1125             dc->xformWorld2Wnd.eM12 = 0.0f;
1126             dc->xformWorld2Wnd.eM21 = 0.0f;
1127             dc->xformWorld2Wnd.eM22 = 1.0f;
1128             dc->xformWorld2Wnd.eDx  = 0.0f;
1129             dc->xformWorld2Wnd.eDy  = 0.0f;
1130             break;
1131         case MWT_LEFTMULTIPLY:
1132             CombineTransform( &dc->xformWorld2Wnd, xform,
1133                 &dc->xformWorld2Wnd );
1134             break;
1135         case MWT_RIGHTMULTIPLY:
1136             CombineTransform( &dc->xformWorld2Wnd, &dc->xformWorld2Wnd,
1137                 xform );
1138             break;
1139         default:
1140             goto done;
1141     }
1142
1143     DC_UpdateXforms( dc );
1144     ret = TRUE;
1145  done:
1146     GDI_ReleaseObj( hdc );
1147     return ret;
1148 }
1149
1150
1151 /****************************************************************************
1152  * CombineTransform [GDI32.@]
1153  * Combines two transformation matrices.
1154  *
1155  * PARAMS
1156  *    xformResult [O] Stores the result of combining the two matrices
1157  *    xform1      [I] Specifies the first matrix to apply
1158  *    xform2      [I] Specifies the second matrix to apply
1159  *
1160  * REMARKS
1161  *    The same matrix can be passed in for more than one of the parameters.
1162  *
1163  * RETURNS STD
1164  */
1165 BOOL WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
1166     const XFORM *xform2 )
1167 {
1168     XFORM xformTemp;
1169
1170     /* Check for illegal parameters */
1171     if (!xformResult || !xform1 || !xform2)
1172         return FALSE;
1173
1174     /* Create the result in a temporary XFORM, since xformResult may be
1175      * equal to xform1 or xform2 */
1176     xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
1177                      xform1->eM12 * xform2->eM21;
1178     xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
1179                      xform1->eM12 * xform2->eM22;
1180     xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
1181                      xform1->eM22 * xform2->eM21;
1182     xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
1183                      xform1->eM22 * xform2->eM22;
1184     xformTemp.eDx  = xform1->eDx  * xform2->eM11 +
1185                      xform1->eDy  * xform2->eM21 +
1186                      xform2->eDx;
1187     xformTemp.eDy  = xform1->eDx  * xform2->eM12 +
1188                      xform1->eDy  * xform2->eM22 +
1189                      xform2->eDy;
1190
1191     /* Copy the result to xformResult */
1192     *xformResult = xformTemp;
1193
1194     return TRUE;
1195 }
1196
1197
1198 /***********************************************************************
1199  *           SetDCHook   (GDI32.@)
1200  *
1201  * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1202  */
1203 BOOL WINAPI SetDCHook( HDC hdc, DCHOOKPROC hookProc, DWORD dwHookData )
1204 {
1205     DC *dc = GDI_GetObjPtr( hdc, DC_MAGIC );
1206
1207     if (!dc) return FALSE;
1208
1209     if (!(dc->flags & DC_SAVED))
1210     {
1211         dc->dwHookData = dwHookData;
1212         dc->hookThunk = hookProc;
1213     }
1214     GDI_ReleaseObj( hdc );
1215     return TRUE;
1216 }
1217
1218
1219 /* relay function to call the 16-bit DC hook proc */
1220 static BOOL16 WINAPI call_dc_hook16( HDC16 hdc16, WORD code, DWORD data, LPARAM lParam )
1221 {
1222     WORD args[6];
1223     DWORD ret;
1224     FARPROC16 proc = NULL;
1225     HDC hdc = HDC_32( hdc16 );
1226     DC *dc = DC_GetDCPtr( hdc );
1227
1228     if (!dc) return FALSE;
1229     proc = dc->hookProc;
1230     GDI_ReleaseObj( hdc );
1231     if (!proc) return FALSE;
1232     args[5] = hdc16;
1233     args[4] = code;
1234     args[3] = HIWORD(data);
1235     args[2] = LOWORD(data);
1236     args[1] = HIWORD(lParam);
1237     args[0] = LOWORD(lParam);
1238     WOWCallback16Ex( (DWORD)proc, WCB16_PASCAL, sizeof(args), args, &ret );
1239     return LOWORD(ret);
1240 }
1241
1242 /***********************************************************************
1243  *           SetDCHook   (GDI.190)
1244  */
1245 BOOL16 WINAPI SetDCHook16( HDC16 hdc16, FARPROC16 hookProc, DWORD dwHookData )
1246 {
1247     HDC hdc = HDC_32( hdc16 );
1248     DC *dc = DC_GetDCPtr( hdc );
1249     if (!dc) return FALSE;
1250
1251     dc->hookProc = hookProc;
1252     GDI_ReleaseObj( hdc );
1253     return SetDCHook( hdc, call_dc_hook16, dwHookData );
1254 }
1255
1256
1257 /***********************************************************************
1258  *           GetDCHook   (GDI.191)
1259  */
1260 DWORD WINAPI GetDCHook16( HDC16 hdc16, FARPROC16 *phookProc )
1261 {
1262     HDC hdc = HDC_32( hdc16 );
1263     DC *dc = DC_GetDCPtr( hdc );
1264     DWORD ret;
1265
1266     if (!dc) return 0;
1267     *phookProc = dc->hookProc;
1268     ret = dc->dwHookData;
1269     GDI_ReleaseObj( hdc );
1270     return ret;
1271 }
1272
1273
1274 /***********************************************************************
1275  *           SetHookFlags   (GDI.192)
1276  */
1277 WORD WINAPI SetHookFlags16(HDC16 hdc16, WORD flags)
1278 {
1279     HDC hdc = HDC_32( hdc16 );
1280     DC *dc = DC_GetDCPtr( hdc );
1281
1282     if( dc )
1283     {
1284         WORD wRet = dc->flags & DC_DIRTY;
1285
1286         /* "Undocumented Windows" info is slightly confusing.
1287          */
1288
1289         TRACE("hDC %p, flags %04x\n",hdc,flags);
1290
1291         if( flags & DCHF_INVALIDATEVISRGN )
1292             dc->flags |= DC_DIRTY;
1293         else if( flags & DCHF_VALIDATEVISRGN || !flags )
1294             dc->flags &= ~DC_DIRTY;
1295         GDI_ReleaseObj( hdc );
1296         return wRet;
1297     }
1298     return 0;
1299 }
1300
1301 /***********************************************************************
1302  *           SetICMMode    (GDI32.@)
1303  */
1304 INT WINAPI SetICMMode(HDC hdc, INT iEnableICM)
1305 {
1306 /*FIXME  Asuming that ICM is always off, and cannot be turned on */
1307     if (iEnableICM == ICM_OFF) return ICM_OFF;
1308     if (iEnableICM == ICM_ON) return 0;
1309     if (iEnableICM == ICM_QUERY) return ICM_OFF;
1310     return 0;
1311 }
1312
1313 /***********************************************************************
1314  *           GetDeviceGammaRamp    (GDI32.@)
1315  */
1316 BOOL WINAPI GetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1317 {
1318     BOOL ret = FALSE;
1319     DC *dc = DC_GetDCPtr( hDC );
1320
1321     if( dc )
1322     {
1323         if (dc->funcs->pGetDeviceGammaRamp)
1324             ret = dc->funcs->pGetDeviceGammaRamp(dc->physDev, ptr);
1325         GDI_ReleaseObj( hDC );
1326     }
1327     return ret;
1328 }
1329
1330 /***********************************************************************
1331  *           SetDeviceGammaRamp    (GDI32.@)
1332  */
1333 BOOL WINAPI SetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1334 {
1335     BOOL ret = FALSE;
1336     DC *dc = DC_GetDCPtr( hDC );
1337
1338     if( dc )
1339     {
1340         if (dc->funcs->pSetDeviceGammaRamp)
1341             ret = dc->funcs->pSetDeviceGammaRamp(dc->physDev, ptr);
1342         GDI_ReleaseObj( hDC );
1343     }
1344     return ret;
1345 }
1346
1347 /***********************************************************************
1348  *           GetColorSpace    (GDI32.@)
1349  */
1350 HCOLORSPACE WINAPI GetColorSpace(HDC hdc)
1351 {
1352 /*FIXME    Need to to whatever GetColorSpace actually does */
1353     return 0;
1354 }
1355
1356 /***********************************************************************
1357  *           CreateColorSpaceA    (GDI32.@)
1358  */
1359 HCOLORSPACE WINAPI CreateColorSpaceA( LPLOGCOLORSPACEA lpLogColorSpace )
1360 {
1361   FIXME( "stub\n" );
1362   return 0;
1363 }
1364
1365 /***********************************************************************
1366  *           CreateColorSpaceW    (GDI32.@)
1367  */
1368 HCOLORSPACE WINAPI CreateColorSpaceW( LPLOGCOLORSPACEW lpLogColorSpace )
1369 {
1370   FIXME( "stub\n" );
1371   return 0;
1372 }
1373
1374 /***********************************************************************
1375  *           DeleteColorSpace     (GDI32.@)
1376  */
1377 BOOL WINAPI DeleteColorSpace( HCOLORSPACE hColorSpace )
1378 {
1379   FIXME( "stub\n" );
1380
1381   return TRUE;
1382 }
1383
1384 /***********************************************************************
1385  *           SetColorSpace     (GDI32.@)
1386  */
1387 HCOLORSPACE WINAPI SetColorSpace( HDC hDC, HCOLORSPACE hColorSpace )
1388 {
1389   FIXME( "stub\n" );
1390
1391   return hColorSpace;
1392 }
1393
1394 /***********************************************************************
1395  *           GetBoundsRect    (GDI.194)
1396  */
1397 UINT16 WINAPI GetBoundsRect16(HDC16 hdc, LPRECT16 rect, UINT16 flags)
1398 {
1399     return DCB_RESET | DCB_DISABLE; /* bounding rectangle always empty and disabled*/
1400 }
1401
1402 /***********************************************************************
1403  *           GetBoundsRect    (GDI32.@)
1404  */
1405 UINT WINAPI GetBoundsRect(HDC hdc, LPRECT rect, UINT flags)
1406 {
1407     FIXME("(): stub\n");
1408     return DCB_RESET;   /* bounding rectangle always empty */
1409 }
1410
1411 /***********************************************************************
1412  *           SetBoundsRect    (GDI.193)
1413  */
1414 UINT16 WINAPI SetBoundsRect16(HDC16 hdc, const RECT16* rect, UINT16 flags)
1415 {
1416     if ( (flags & DCB_ACCUMULATE) || (flags & DCB_ENABLE) )
1417         FIXME("(%04x, %p, %04x): stub\n", hdc, rect, flags );
1418
1419     return DCB_RESET | DCB_DISABLE; /* bounding rectangle always empty and disabled*/
1420 }
1421
1422 /***********************************************************************
1423  *           SetBoundsRect    (GDI32.@)
1424  */
1425 UINT WINAPI SetBoundsRect(HDC hdc, const RECT* rect, UINT flags)
1426 {
1427     FIXME("(): stub\n");
1428     return DCB_DISABLE;   /* bounding rectangle always empty */
1429 }
1430
1431
1432 /***********************************************************************
1433  *              GetRelAbs               (GDI32.@)
1434  */
1435 INT WINAPI GetRelAbs( HDC hdc, DWORD dwIgnore )
1436 {
1437     INT ret = 0;
1438     DC *dc = DC_GetDCPtr( hdc );
1439     if (dc) ret = dc->relAbsMode;
1440     GDI_ReleaseObj( hdc );
1441     return ret;
1442 }
1443
1444 /***********************************************************************
1445  *           GetLayout    (GDI32.@)
1446  *
1447  * Gets left->right or right->left text layout flags of a dc.
1448  * win98 just returns 0 and sets ERROR_CALL_NOT_IMPLEMENTED so we do the same
1449  *
1450  */
1451 DWORD WINAPI GetLayout(HDC hdc)
1452 {
1453     FIXME("(%p): stub\n", hdc);
1454     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1455     return 0;
1456 }
1457
1458 /***********************************************************************
1459  *           SetLayout    (GDI32.@)
1460  *
1461  * Sets left->right or right->left text layout flags of a dc.
1462  * win98 just returns 0 and sets ERROR_CALL_NOT_IMPLEMENTED so we do the same
1463  *
1464  */
1465 DWORD WINAPI SetLayout(HDC hdc, DWORD layout)
1466 {
1467     FIXME("(%p,%08lx): stub\n", hdc, layout);
1468     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1469     return 0;
1470 }
1471
1472 /***********************************************************************
1473  *           GetDCBrushColor    (GDI32.@)
1474  *
1475  * Retrieves the current brush color for the specified device
1476  * context (DC).
1477  *
1478  */
1479 COLORREF WINAPI GetDCBrushColor(HDC hdc)
1480 {
1481     DC *dc;
1482     COLORREF dcBrushColor = CLR_INVALID;
1483
1484     TRACE("hdc(%p)\n", hdc);
1485
1486     dc = DC_GetDCPtr( hdc );
1487     if (dc)
1488     {
1489         dcBrushColor = dc->dcBrushColor;
1490         GDI_ReleaseObj( hdc );
1491     }
1492
1493     return dcBrushColor;
1494 }
1495
1496 /***********************************************************************
1497  *           SetDCBrushColor    (GDI32.@)
1498  *
1499  * Sets the current device context (DC) brush color to the specified
1500  * color value. If the device cannot represent the specified color
1501  * value, the color is set to the nearest physical color.
1502  *
1503  */
1504 COLORREF WINAPI SetDCBrushColor(HDC hdc, COLORREF crColor)
1505 {
1506     DC *dc;
1507     COLORREF oldClr = CLR_INVALID;
1508
1509     TRACE("hdc(%p) crColor(%08lx)\n", hdc, crColor);
1510
1511     dc = DC_GetDCPtr( hdc );
1512     if (dc)
1513     {
1514         if (dc->funcs->pSetDCBrushColor)
1515             crColor = dc->funcs->pSetDCBrushColor( dc->physDev, crColor );
1516         else if (dc->hBrush == GetStockObject( DC_BRUSH ))
1517         {
1518             /* If DC_BRUSH is selected, update driver pen color */
1519             HBRUSH hBrush = CreateSolidBrush( crColor );
1520             dc->funcs->pSelectBrush( dc->physDev, hBrush );
1521             DeleteObject( hBrush );
1522         }
1523
1524         if (crColor != CLR_INVALID)
1525         {
1526             oldClr = dc->dcBrushColor;
1527             dc->dcBrushColor = crColor;
1528         }
1529
1530         GDI_ReleaseObj( hdc );
1531     }
1532
1533     return oldClr;
1534 }
1535
1536 /***********************************************************************
1537  *           GetDCPenColor    (GDI32.@)
1538  *
1539  * Retrieves the current pen color for the specified device
1540  * context (DC).
1541  *
1542  */
1543 COLORREF WINAPI GetDCPenColor(HDC hdc)
1544 {
1545     DC *dc;
1546     COLORREF dcPenColor = CLR_INVALID;
1547
1548     TRACE("hdc(%p)\n", hdc);
1549
1550     dc = DC_GetDCPtr( hdc );
1551     if (dc)
1552     {
1553         dcPenColor = dc->dcPenColor;
1554         GDI_ReleaseObj( hdc );
1555     }
1556
1557     return dcPenColor;
1558 }
1559
1560 /***********************************************************************
1561  *           SetDCPenColor    (GDI32.@)
1562  *
1563  * Sets the current device context (DC) pen color to the specified
1564  * color value. If the device cannot represent the specified color
1565  * value, the color is set to the nearest physical color.
1566  *
1567  */
1568 COLORREF WINAPI SetDCPenColor(HDC hdc, COLORREF crColor)
1569 {
1570     DC *dc;
1571     COLORREF oldClr = CLR_INVALID;
1572
1573     TRACE("hdc(%p) crColor(%08lx)\n", hdc, crColor);
1574
1575     dc = DC_GetDCPtr( hdc );
1576     if (dc)
1577     {
1578         if (dc->funcs->pSetDCPenColor)
1579             crColor = dc->funcs->pSetDCPenColor( dc->physDev, crColor );
1580         else if (dc->hPen == GetStockObject( DC_PEN ))
1581         {
1582             /* If DC_PEN is selected, update the driver pen color */
1583             LOGPEN logpen = { PS_SOLID, { 0, 0 }, crColor };
1584             HPEN hPen = CreatePenIndirect( &logpen );
1585             dc->funcs->pSelectPen( dc->physDev, hPen );
1586             DeleteObject( hPen );
1587         }
1588
1589         if (crColor != CLR_INVALID)
1590         {
1591             oldClr = dc->dcPenColor;
1592             dc->dcPenColor = crColor;
1593         }
1594
1595         GDI_ReleaseObj( hdc );
1596     }
1597
1598     return oldClr;
1599 }
1600
1601 /***********************************************************************
1602  *           SetVirtualResolution   (GDI32.@)
1603  *
1604  * Undocumented on msdn.  Called when PowerPoint XP saves a file.
1605  */
1606 DWORD WINAPI SetVirtualResolution(HDC hdc, DWORD dw2, DWORD dw3, DWORD dw4, DWORD dw5)
1607 {
1608     FIXME("(%p %08lx %08lx %08lx %08lx): stub!\n", hdc, dw2, dw3, dw4, dw5);
1609     return FALSE;
1610 }