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