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