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