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