wer: Add new stubbed wer.dll.
[wine/hramrach.git] / dlls / gdi32 / dc.c
bloba23413db78e7acf9a9c898edc98edf4bfed21bd4
1 /*
2 * GDI Device Context functions
4 * Copyright 1993 Alexandre Julliard
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <assert.h>
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winreg.h"
31 #include "winternl.h"
32 #include "winerror.h"
33 #include "wownt32.h"
34 #include "gdi_private.h"
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(dc);
40 static const WCHAR displayW[] = { 'd','i','s','p','l','a','y',0 };
42 static BOOL DC_DeleteObject( HGDIOBJ handle );
44 static const struct gdi_obj_funcs dc_funcs =
46 NULL, /* pSelectObject */
47 NULL, /* pGetObjectA */
48 NULL, /* pGetObjectW */
49 NULL, /* pUnrealizeObject */
50 DC_DeleteObject /* pDeleteObject */
54 static inline DC *get_dc_obj( HDC hdc )
56 DC *dc = GDI_GetObjPtr( hdc, 0 );
57 if (!dc) return NULL;
59 if ((dc->header.type != OBJ_DC) &&
60 (dc->header.type != OBJ_MEMDC) &&
61 (dc->header.type != OBJ_METADC) &&
62 (dc->header.type != OBJ_ENHMETADC))
64 GDI_ReleaseObj( hdc );
65 SetLastError( ERROR_INVALID_HANDLE );
66 dc = NULL;
68 return dc;
72 /***********************************************************************
73 * alloc_dc_ptr
75 DC *alloc_dc_ptr( const DC_FUNCTIONS *funcs, WORD magic )
77 DC *dc;
79 if (!(dc = HeapAlloc( GetProcessHeap(), 0, sizeof(*dc) ))) return NULL;
81 dc->funcs = funcs;
82 dc->physDev = NULL;
83 dc->thread = GetCurrentThreadId();
84 dc->refcount = 1;
85 dc->dirty = 0;
86 dc->saveLevel = 0;
87 dc->saved_dc = 0;
88 dc->dwHookData = 0;
89 dc->hookProc = NULL;
90 dc->wndOrgX = 0;
91 dc->wndOrgY = 0;
92 dc->wndExtX = 1;
93 dc->wndExtY = 1;
94 dc->vportOrgX = 0;
95 dc->vportOrgY = 0;
96 dc->vportExtX = 1;
97 dc->vportExtY = 1;
98 dc->miterLimit = 10.0f; /* 10.0 is the default, from MSDN */
99 dc->flags = 0;
100 dc->layout = 0;
101 dc->hClipRgn = 0;
102 dc->hMetaRgn = 0;
103 dc->hMetaClipRgn = 0;
104 dc->hVisRgn = 0;
105 dc->hPen = GDI_inc_ref_count( GetStockObject( BLACK_PEN ));
106 dc->hBrush = GDI_inc_ref_count( GetStockObject( WHITE_BRUSH ));
107 dc->hFont = GDI_inc_ref_count( GetStockObject( SYSTEM_FONT ));
108 dc->hBitmap = 0;
109 dc->hDevice = 0;
110 dc->hPalette = GetStockObject( DEFAULT_PALETTE );
111 dc->gdiFont = 0;
112 dc->font_code_page = CP_ACP;
113 dc->ROPmode = R2_COPYPEN;
114 dc->polyFillMode = ALTERNATE;
115 dc->stretchBltMode = BLACKONWHITE;
116 dc->relAbsMode = ABSOLUTE;
117 dc->backgroundMode = OPAQUE;
118 dc->backgroundColor = RGB( 255, 255, 255 );
119 dc->dcBrushColor = RGB( 255, 255, 255 );
120 dc->dcPenColor = RGB( 0, 0, 0 );
121 dc->textColor = RGB( 0, 0, 0 );
122 dc->brushOrgX = 0;
123 dc->brushOrgY = 0;
124 dc->textAlign = TA_LEFT | TA_TOP | TA_NOUPDATECP;
125 dc->charExtra = 0;
126 dc->breakExtra = 0;
127 dc->breakRem = 0;
128 dc->MapMode = MM_TEXT;
129 dc->GraphicsMode = GM_COMPATIBLE;
130 dc->pAbortProc = NULL;
131 dc->CursPosX = 0;
132 dc->CursPosY = 0;
133 dc->ArcDirection = AD_COUNTERCLOCKWISE;
134 dc->xformWorld2Wnd.eM11 = 1.0f;
135 dc->xformWorld2Wnd.eM12 = 0.0f;
136 dc->xformWorld2Wnd.eM21 = 0.0f;
137 dc->xformWorld2Wnd.eM22 = 1.0f;
138 dc->xformWorld2Wnd.eDx = 0.0f;
139 dc->xformWorld2Wnd.eDy = 0.0f;
140 dc->xformWorld2Vport = dc->xformWorld2Wnd;
141 dc->xformVport2World = dc->xformWorld2Wnd;
142 dc->vport2WorldValid = TRUE;
143 dc->BoundsRect.left = 0;
144 dc->BoundsRect.top = 0;
145 dc->BoundsRect.right = 0;
146 dc->BoundsRect.bottom = 0;
147 PATH_InitGdiPath(&dc->path);
149 if (!(dc->hSelf = alloc_gdi_handle( &dc->header, magic, &dc_funcs )))
151 HeapFree( GetProcessHeap(), 0, dc );
152 dc = NULL;
154 return dc;
159 /***********************************************************************
160 * free_dc_ptr
162 BOOL free_dc_ptr( DC *dc )
164 assert( dc->refcount == 1 );
165 if (free_gdi_handle( dc->hSelf ) != dc) return FALSE; /* shouldn't happen */
166 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
167 if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
168 if (dc->hMetaClipRgn) DeleteObject( dc->hMetaClipRgn );
169 if (dc->hVisRgn) DeleteObject( dc->hVisRgn );
170 PATH_DestroyGdiPath( &dc->path );
171 return HeapFree( GetProcessHeap(), 0, dc );
175 /***********************************************************************
176 * get_dc_ptr
178 * Retrieve a DC pointer but release the GDI lock.
180 DC *get_dc_ptr( HDC hdc )
182 DC *dc = get_dc_obj( hdc );
183 if (!dc) return NULL;
185 if (!InterlockedCompareExchange( &dc->refcount, 1, 0 ))
187 dc->thread = GetCurrentThreadId();
189 else if (dc->thread != GetCurrentThreadId())
191 WARN( "dc %p belongs to thread %04x\n", hdc, dc->thread );
192 GDI_ReleaseObj( hdc );
193 return NULL;
195 else InterlockedIncrement( &dc->refcount );
197 GDI_ReleaseObj( hdc );
198 return dc;
202 /***********************************************************************
203 * release_dc_ptr
205 void release_dc_ptr( DC *dc )
207 LONG ref;
209 dc->thread = 0;
210 ref = InterlockedDecrement( &dc->refcount );
211 assert( ref >= 0 );
212 if (ref) dc->thread = GetCurrentThreadId(); /* we still own it */
216 /***********************************************************************
217 * update_dc
219 * Make sure the DC vis region is up to date.
220 * This function may call up to USER so the GDI lock should _not_
221 * be held when calling it.
223 void update_dc( DC *dc )
225 if (InterlockedExchange( &dc->dirty, 0 ) && dc->hookProc)
226 dc->hookProc( dc->hSelf, DCHC_INVALIDVISRGN, dc->dwHookData, 0 );
230 /***********************************************************************
231 * DC_DeleteObject
233 static BOOL DC_DeleteObject( HGDIOBJ handle )
235 return DeleteDC( handle );
239 /***********************************************************************
240 * DC_InitDC
242 * Setup device-specific DC values for a newly created DC.
244 void DC_InitDC( DC* dc )
246 if (dc->funcs->pRealizeDefaultPalette) dc->funcs->pRealizeDefaultPalette( dc->physDev );
247 SetTextColor( dc->hSelf, dc->textColor );
248 SetBkColor( dc->hSelf, dc->backgroundColor );
249 SelectObject( dc->hSelf, dc->hPen );
250 SelectObject( dc->hSelf, dc->hBrush );
251 SelectObject( dc->hSelf, dc->hFont );
252 CLIPPING_UpdateGCRegion( dc );
253 SetVirtualResolution( dc->hSelf, 0, 0, 0, 0 );
257 /***********************************************************************
258 * DC_InvertXform
260 * Computes the inverse of the transformation xformSrc and stores it to
261 * xformDest. Returns TRUE if successful or FALSE if the xformSrc matrix
262 * is singular.
264 static BOOL DC_InvertXform( const XFORM *xformSrc, XFORM *xformDest )
266 double determinant;
268 determinant = xformSrc->eM11*xformSrc->eM22 -
269 xformSrc->eM12*xformSrc->eM21;
270 if (determinant > -1e-12 && determinant < 1e-12)
271 return FALSE;
273 xformDest->eM11 = xformSrc->eM22 / determinant;
274 xformDest->eM12 = -xformSrc->eM12 / determinant;
275 xformDest->eM21 = -xformSrc->eM21 / determinant;
276 xformDest->eM22 = xformSrc->eM11 / determinant;
277 xformDest->eDx = -xformSrc->eDx * xformDest->eM11 -
278 xformSrc->eDy * xformDest->eM21;
279 xformDest->eDy = -xformSrc->eDx * xformDest->eM12 -
280 xformSrc->eDy * xformDest->eM22;
282 return TRUE;
285 /* Construct a transformation to do the window-to-viewport conversion */
286 static void construct_window_to_viewport(DC *dc, XFORM *xform)
288 double scaleX, scaleY;
289 scaleX = (double)dc->vportExtX / (double)dc->wndExtX;
290 scaleY = (double)dc->vportExtY / (double)dc->wndExtY;
292 if (dc->layout & LAYOUT_RTL) scaleX = -scaleX;
293 xform->eM11 = scaleX;
294 xform->eM12 = 0.0;
295 xform->eM21 = 0.0;
296 xform->eM22 = scaleY;
297 xform->eDx = (double)dc->vportOrgX - scaleX * (double)dc->wndOrgX;
298 xform->eDy = (double)dc->vportOrgY - scaleY * (double)dc->wndOrgY;
299 if (dc->layout & LAYOUT_RTL) xform->eDx = dc->vis_rect.right - dc->vis_rect.left - 1 - xform->eDx;
302 /***********************************************************************
303 * DC_UpdateXforms
305 * Updates the xformWorld2Vport, xformVport2World and vport2WorldValid
306 * fields of the specified DC by creating a transformation that
307 * represents the current mapping mode and combining it with the DC's
308 * world transform. This function should be called whenever the
309 * parameters associated with the mapping mode (window and viewport
310 * extents and origins) or the world transform change.
312 void DC_UpdateXforms( DC *dc )
314 XFORM xformWnd2Vport, oldworld2vport;
316 construct_window_to_viewport(dc, &xformWnd2Vport);
318 oldworld2vport = dc->xformWorld2Vport;
319 /* Combine with the world transformation */
320 CombineTransform( &dc->xformWorld2Vport, &dc->xformWorld2Wnd,
321 &xformWnd2Vport );
323 /* Create inverse of world-to-viewport transformation */
324 dc->vport2WorldValid = DC_InvertXform( &dc->xformWorld2Vport,
325 &dc->xformVport2World );
327 /* Reselect the font and pen back into the dc so that the size
328 gets updated. */
329 if (memcmp(&oldworld2vport, &dc->xformWorld2Vport, sizeof(oldworld2vport)) &&
330 !GdiIsMetaFileDC(dc->hSelf))
332 SelectObject(dc->hSelf, GetCurrentObject(dc->hSelf, OBJ_FONT));
333 SelectObject(dc->hSelf, GetCurrentObject(dc->hSelf, OBJ_PEN));
338 /***********************************************************************
339 * save_dc_state
341 INT save_dc_state( HDC hdc )
343 DC * newdc, * dc;
344 INT ret;
346 if (!(dc = get_dc_ptr( hdc ))) return 0;
347 if (!(newdc = HeapAlloc( GetProcessHeap(), 0, sizeof(*newdc ))))
349 release_dc_ptr( dc );
350 return 0;
353 newdc->flags = dc->flags | DC_SAVED;
354 newdc->layout = dc->layout;
355 newdc->hPen = dc->hPen;
356 newdc->hBrush = dc->hBrush;
357 newdc->hFont = dc->hFont;
358 newdc->hBitmap = dc->hBitmap;
359 newdc->hDevice = dc->hDevice;
360 newdc->hPalette = dc->hPalette;
361 newdc->ROPmode = dc->ROPmode;
362 newdc->polyFillMode = dc->polyFillMode;
363 newdc->stretchBltMode = dc->stretchBltMode;
364 newdc->relAbsMode = dc->relAbsMode;
365 newdc->backgroundMode = dc->backgroundMode;
366 newdc->backgroundColor = dc->backgroundColor;
367 newdc->textColor = dc->textColor;
368 newdc->dcBrushColor = dc->dcBrushColor;
369 newdc->dcPenColor = dc->dcPenColor;
370 newdc->brushOrgX = dc->brushOrgX;
371 newdc->brushOrgY = dc->brushOrgY;
372 newdc->textAlign = dc->textAlign;
373 newdc->charExtra = dc->charExtra;
374 newdc->breakExtra = dc->breakExtra;
375 newdc->breakRem = dc->breakRem;
376 newdc->MapMode = dc->MapMode;
377 newdc->GraphicsMode = dc->GraphicsMode;
378 newdc->CursPosX = dc->CursPosX;
379 newdc->CursPosY = dc->CursPosY;
380 newdc->ArcDirection = dc->ArcDirection;
381 newdc->xformWorld2Wnd = dc->xformWorld2Wnd;
382 newdc->xformWorld2Vport = dc->xformWorld2Vport;
383 newdc->xformVport2World = dc->xformVport2World;
384 newdc->vport2WorldValid = dc->vport2WorldValid;
385 newdc->wndOrgX = dc->wndOrgX;
386 newdc->wndOrgY = dc->wndOrgY;
387 newdc->wndExtX = dc->wndExtX;
388 newdc->wndExtY = dc->wndExtY;
389 newdc->vportOrgX = dc->vportOrgX;
390 newdc->vportOrgY = dc->vportOrgY;
391 newdc->vportExtX = dc->vportExtX;
392 newdc->vportExtY = dc->vportExtY;
393 newdc->virtual_res = dc->virtual_res;
394 newdc->virtual_size = dc->virtual_size;
395 newdc->BoundsRect = dc->BoundsRect;
396 newdc->gdiFont = dc->gdiFont;
398 newdc->thread = GetCurrentThreadId();
399 newdc->refcount = 1;
400 newdc->saveLevel = 0;
401 newdc->saved_dc = 0;
403 PATH_InitGdiPath( &newdc->path );
405 newdc->pAbortProc = NULL;
406 newdc->hookProc = NULL;
408 if (!(newdc->hSelf = alloc_gdi_handle( &newdc->header, dc->header.type, &dc_funcs )))
410 HeapFree( GetProcessHeap(), 0, newdc );
411 release_dc_ptr( dc );
412 return 0;
415 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
417 newdc->hVisRgn = 0;
418 newdc->hClipRgn = 0;
419 newdc->hMetaRgn = 0;
420 newdc->hMetaClipRgn = 0;
421 if (dc->hClipRgn)
423 newdc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
424 CombineRgn( newdc->hClipRgn, dc->hClipRgn, 0, RGN_COPY );
426 if (dc->hMetaRgn)
428 newdc->hMetaRgn = CreateRectRgn( 0, 0, 0, 0 );
429 CombineRgn( newdc->hMetaRgn, dc->hMetaRgn, 0, RGN_COPY );
431 /* don't bother recomputing hMetaClipRgn, we'll do that in SetDCState */
433 if (!PATH_AssignGdiPath( &newdc->path, &dc->path ))
435 release_dc_ptr( dc );
436 free_dc_ptr( newdc );
437 return 0;
440 newdc->saved_dc = dc->saved_dc;
441 dc->saved_dc = newdc->hSelf;
442 ret = ++dc->saveLevel;
443 release_dc_ptr( newdc );
444 release_dc_ptr( dc );
445 return ret;
449 /***********************************************************************
450 * restore_dc_state
452 BOOL restore_dc_state( HDC hdc, INT level )
454 HDC hdcs, first_dcs;
455 DC *dc, *dcs;
456 INT save_level;
458 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
460 /* find the state level to restore */
462 if (abs(level) > dc->saveLevel || level == 0)
464 release_dc_ptr( dc );
465 return FALSE;
467 if (level < 0) level = dc->saveLevel + level + 1;
468 first_dcs = dc->saved_dc;
469 for (hdcs = first_dcs, save_level = dc->saveLevel; save_level > level; save_level--)
471 if (!(dcs = get_dc_ptr( hdcs )))
473 release_dc_ptr( dc );
474 return FALSE;
476 hdcs = dcs->saved_dc;
477 release_dc_ptr( dcs );
480 /* restore the state */
482 if (!(dcs = get_dc_ptr( hdcs )))
484 release_dc_ptr( dc );
485 return FALSE;
487 if (!PATH_AssignGdiPath( &dc->path, &dcs->path ))
489 release_dc_ptr( dcs );
490 release_dc_ptr( dc );
491 return FALSE;
494 dc->flags = dcs->flags & ~DC_SAVED;
495 dc->layout = dcs->layout;
496 dc->hDevice = dcs->hDevice;
497 dc->ROPmode = dcs->ROPmode;
498 dc->polyFillMode = dcs->polyFillMode;
499 dc->stretchBltMode = dcs->stretchBltMode;
500 dc->relAbsMode = dcs->relAbsMode;
501 dc->backgroundMode = dcs->backgroundMode;
502 dc->backgroundColor = dcs->backgroundColor;
503 dc->textColor = dcs->textColor;
504 dc->dcBrushColor = dcs->dcBrushColor;
505 dc->dcPenColor = dcs->dcPenColor;
506 dc->brushOrgX = dcs->brushOrgX;
507 dc->brushOrgY = dcs->brushOrgY;
508 dc->textAlign = dcs->textAlign;
509 dc->charExtra = dcs->charExtra;
510 dc->breakExtra = dcs->breakExtra;
511 dc->breakRem = dcs->breakRem;
512 dc->MapMode = dcs->MapMode;
513 dc->GraphicsMode = dcs->GraphicsMode;
514 dc->CursPosX = dcs->CursPosX;
515 dc->CursPosY = dcs->CursPosY;
516 dc->ArcDirection = dcs->ArcDirection;
517 dc->xformWorld2Wnd = dcs->xformWorld2Wnd;
518 dc->xformWorld2Vport = dcs->xformWorld2Vport;
519 dc->xformVport2World = dcs->xformVport2World;
520 dc->vport2WorldValid = dcs->vport2WorldValid;
521 dc->BoundsRect = dcs->BoundsRect;
523 dc->wndOrgX = dcs->wndOrgX;
524 dc->wndOrgY = dcs->wndOrgY;
525 dc->wndExtX = dcs->wndExtX;
526 dc->wndExtY = dcs->wndExtY;
527 dc->vportOrgX = dcs->vportOrgX;
528 dc->vportOrgY = dcs->vportOrgY;
529 dc->vportExtX = dcs->vportExtX;
530 dc->vportExtY = dcs->vportExtY;
531 dc->virtual_res = dcs->virtual_res;
532 dc->virtual_size = dcs->virtual_size;
534 if (dcs->hClipRgn)
536 if (!dc->hClipRgn) dc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
537 CombineRgn( dc->hClipRgn, dcs->hClipRgn, 0, RGN_COPY );
539 else
541 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
542 dc->hClipRgn = 0;
544 if (dcs->hMetaRgn)
546 if (!dc->hMetaRgn) dc->hMetaRgn = CreateRectRgn( 0, 0, 0, 0 );
547 CombineRgn( dc->hMetaRgn, dcs->hMetaRgn, 0, RGN_COPY );
549 else
551 if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
552 dc->hMetaRgn = 0;
554 DC_UpdateXforms( dc );
555 CLIPPING_UpdateGCRegion( dc );
557 SelectObject( hdc, dcs->hBitmap );
558 SelectObject( hdc, dcs->hBrush );
559 SelectObject( hdc, dcs->hFont );
560 SelectObject( hdc, dcs->hPen );
561 SetBkColor( hdc, dcs->backgroundColor);
562 SetTextColor( hdc, dcs->textColor);
563 GDISelectPalette( hdc, dcs->hPalette, FALSE );
565 dc->saved_dc = dcs->saved_dc;
566 dcs->saved_dc = 0;
567 dc->saveLevel = save_level - 1;
569 release_dc_ptr( dcs );
571 /* now destroy all the saved DCs */
573 while (first_dcs)
575 if (!(dcs = get_dc_ptr( first_dcs ))) break;
576 hdcs = dcs->saved_dc;
577 free_dc_ptr( dcs );
578 first_dcs = hdcs;
580 release_dc_ptr( dc );
581 return TRUE;
585 /***********************************************************************
586 * SaveDC (GDI32.@)
588 INT WINAPI SaveDC( HDC hdc )
590 DC * dc;
591 INT ret;
593 if (!(dc = get_dc_ptr( hdc ))) return 0;
595 if(dc->funcs->pSaveDC)
596 ret = dc->funcs->pSaveDC( dc->physDev );
597 else
598 ret = save_dc_state( hdc );
600 release_dc_ptr( dc );
601 return ret;
605 /***********************************************************************
606 * RestoreDC (GDI32.@)
608 BOOL WINAPI RestoreDC( HDC hdc, INT level )
610 DC *dc;
611 BOOL success;
613 TRACE("%p %d\n", hdc, level );
614 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
615 update_dc( dc );
617 if(dc->funcs->pRestoreDC)
618 success = dc->funcs->pRestoreDC( dc->physDev, level );
619 else
620 success = restore_dc_state( hdc, level );
622 release_dc_ptr( dc );
623 return success;
627 /***********************************************************************
628 * CreateDCW (GDI32.@)
630 HDC WINAPI CreateDCW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
631 const DEVMODEW *initData )
633 HDC hdc;
634 DC * dc;
635 const DC_FUNCTIONS *funcs;
636 WCHAR buf[300];
638 GDI_CheckNotLock();
640 if (!device || !DRIVER_GetDriverName( device, buf, 300 ))
642 if (!driver)
644 ERR( "no device found for %s\n", debugstr_w(device) );
645 return 0;
647 strcpyW(buf, driver);
650 if (!(funcs = DRIVER_load_driver( buf )))
652 ERR( "no driver found for %s\n", debugstr_w(buf) );
653 return 0;
655 if (!(dc = alloc_dc_ptr( funcs, OBJ_DC ))) goto error;
656 hdc = dc->hSelf;
658 dc->hBitmap = GDI_inc_ref_count( GetStockObject( DEFAULT_BITMAP ));
659 if (!(dc->hVisRgn = CreateRectRgn( 0, 0, 1, 1 ))) goto error;
661 TRACE("(driver=%s, device=%s, output=%s): returning %p\n",
662 debugstr_w(driver), debugstr_w(device), debugstr_w(output), dc->hSelf );
664 if (dc->funcs->pCreateDC &&
665 !dc->funcs->pCreateDC( hdc, &dc->physDev, buf, device, output, initData ))
667 WARN("creation aborted by device\n" );
668 goto error;
671 dc->vis_rect.left = 0;
672 dc->vis_rect.top = 0;
673 dc->vis_rect.right = GetDeviceCaps( hdc, DESKTOPHORZRES );
674 dc->vis_rect.bottom = GetDeviceCaps( hdc, DESKTOPVERTRES );
675 SetRectRgn(dc->hVisRgn, dc->vis_rect.left, dc->vis_rect.top, dc->vis_rect.right, dc->vis_rect.bottom);
677 DC_InitDC( dc );
678 release_dc_ptr( dc );
679 return hdc;
681 error:
682 if (dc) free_dc_ptr( dc );
683 return 0;
687 /***********************************************************************
688 * CreateDCA (GDI32.@)
690 HDC WINAPI CreateDCA( LPCSTR driver, LPCSTR device, LPCSTR output,
691 const DEVMODEA *initData )
693 UNICODE_STRING driverW, deviceW, outputW;
694 DEVMODEW *initDataW;
695 HDC ret;
697 if (driver) RtlCreateUnicodeStringFromAsciiz(&driverW, driver);
698 else driverW.Buffer = NULL;
700 if (device) RtlCreateUnicodeStringFromAsciiz(&deviceW, device);
701 else deviceW.Buffer = NULL;
703 if (output) RtlCreateUnicodeStringFromAsciiz(&outputW, output);
704 else outputW.Buffer = NULL;
706 initDataW = NULL;
707 if (initData)
709 /* don't convert initData for DISPLAY driver, it's not used */
710 if (!driverW.Buffer || strcmpiW( driverW.Buffer, displayW ))
711 initDataW = GdiConvertToDevmodeW(initData);
714 ret = CreateDCW( driverW.Buffer, deviceW.Buffer, outputW.Buffer, initDataW );
716 RtlFreeUnicodeString(&driverW);
717 RtlFreeUnicodeString(&deviceW);
718 RtlFreeUnicodeString(&outputW);
719 HeapFree(GetProcessHeap(), 0, initDataW);
720 return ret;
724 /***********************************************************************
725 * CreateICA (GDI32.@)
727 HDC WINAPI CreateICA( LPCSTR driver, LPCSTR device, LPCSTR output,
728 const DEVMODEA* initData )
730 /* Nothing special yet for ICs */
731 return CreateDCA( driver, device, output, initData );
735 /***********************************************************************
736 * CreateICW (GDI32.@)
738 HDC WINAPI CreateICW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
739 const DEVMODEW* initData )
741 /* Nothing special yet for ICs */
742 return CreateDCW( driver, device, output, initData );
746 /***********************************************************************
747 * CreateCompatibleDC (GDI32.@)
749 HDC WINAPI CreateCompatibleDC( HDC hdc )
751 DC *dc, *origDC;
752 HDC ret;
753 const DC_FUNCTIONS *funcs = NULL;
754 PHYSDEV physDev = NULL;
756 GDI_CheckNotLock();
758 if (hdc)
760 if (!(origDC = get_dc_ptr( hdc ))) return 0;
761 if (GetObjectType( hdc ) == OBJ_DC)
763 funcs = origDC->funcs;
764 physDev = origDC->physDev;
766 release_dc_ptr( origDC );
769 if (!funcs && !(funcs = DRIVER_get_display_driver())) return 0;
771 if (!(dc = alloc_dc_ptr( funcs, OBJ_MEMDC ))) goto error;
773 TRACE("(%p): returning %p\n", hdc, dc->hSelf );
775 dc->hBitmap = GDI_inc_ref_count( GetStockObject( DEFAULT_BITMAP ));
776 dc->vis_rect.left = 0;
777 dc->vis_rect.top = 0;
778 dc->vis_rect.right = 1;
779 dc->vis_rect.bottom = 1;
780 if (!(dc->hVisRgn = CreateRectRgn( 0, 0, 1, 1 ))) goto error; /* default bitmap is 1x1 */
782 /* Copy the driver-specific physical device info into
783 * the new DC. The driver may use this read-only info
784 * while creating the compatible DC below. */
785 dc->physDev = physDev;
786 ret = dc->hSelf;
788 if (dc->funcs->pCreateDC &&
789 !dc->funcs->pCreateDC( dc->hSelf, &dc->physDev, NULL, NULL, NULL, NULL ))
791 WARN("creation aborted by device\n");
792 goto error;
795 DC_InitDC( dc );
796 release_dc_ptr( dc );
797 return ret;
799 error:
800 if (dc) free_dc_ptr( dc );
801 return 0;
805 /***********************************************************************
806 * DeleteDC (GDI32.@)
808 BOOL WINAPI DeleteDC( HDC hdc )
810 DC * dc;
812 TRACE("%p\n", hdc );
814 GDI_CheckNotLock();
816 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
817 if (dc->refcount != 1)
819 FIXME( "not deleting busy DC %p refcount %u\n", dc->hSelf, dc->refcount );
820 release_dc_ptr( dc );
821 return FALSE;
824 /* Call hook procedure to check whether is it OK to delete this DC */
825 if (dc->hookProc && !dc->hookProc( hdc, DCHC_DELETEDC, dc->dwHookData, 0 ))
827 release_dc_ptr( dc );
828 return TRUE;
831 while (dc->saveLevel)
833 DC * dcs;
834 HDC hdcs = dc->saved_dc;
835 if (!(dcs = get_dc_ptr( hdcs ))) break;
836 dc->saved_dc = dcs->saved_dc;
837 dc->saveLevel--;
838 free_dc_ptr( dcs );
841 if (!(dc->flags & DC_SAVED))
843 SelectObject( hdc, GetStockObject(BLACK_PEN) );
844 SelectObject( hdc, GetStockObject(WHITE_BRUSH) );
845 SelectObject( hdc, GetStockObject(SYSTEM_FONT) );
846 SelectObject( hdc, GetStockObject(DEFAULT_BITMAP) );
847 if (dc->funcs->pDeleteDC) dc->funcs->pDeleteDC(dc->physDev);
848 dc->physDev = NULL;
851 free_dc_ptr( dc );
852 return TRUE;
856 /***********************************************************************
857 * ResetDCW (GDI32.@)
859 HDC WINAPI ResetDCW( HDC hdc, const DEVMODEW *devmode )
861 DC *dc;
862 HDC ret = hdc;
864 if ((dc = get_dc_ptr( hdc )))
866 if (dc->funcs->pResetDC)
868 ret = dc->funcs->pResetDC( dc->physDev, devmode );
869 if (ret) /* reset the visible region */
871 dc->dirty = 0;
872 dc->vis_rect.left = 0;
873 dc->vis_rect.top = 0;
874 dc->vis_rect.right = GetDeviceCaps( hdc, DESKTOPHORZRES );
875 dc->vis_rect.bottom = GetDeviceCaps( hdc, DESKTOPVERTRES );
876 SetRectRgn( dc->hVisRgn, dc->vis_rect.left, dc->vis_rect.top,
877 dc->vis_rect.right, dc->vis_rect.bottom );
878 CLIPPING_UpdateGCRegion( dc );
881 release_dc_ptr( dc );
883 return ret;
887 /***********************************************************************
888 * ResetDCA (GDI32.@)
890 HDC WINAPI ResetDCA( HDC hdc, const DEVMODEA *devmode )
892 DEVMODEW *devmodeW;
893 HDC ret;
895 if (devmode) devmodeW = GdiConvertToDevmodeW(devmode);
896 else devmodeW = NULL;
898 ret = ResetDCW(hdc, devmodeW);
900 HeapFree(GetProcessHeap(), 0, devmodeW);
901 return ret;
905 /***********************************************************************
906 * GetDeviceCaps (GDI32.@)
908 INT WINAPI GetDeviceCaps( HDC hdc, INT cap )
910 DC *dc;
911 INT ret = 0;
913 if ((dc = get_dc_ptr( hdc )))
915 if (dc->funcs->pGetDeviceCaps) ret = dc->funcs->pGetDeviceCaps( dc->physDev, cap );
916 else switch(cap) /* return meaningful values for some entries */
918 case HORZRES: ret = 640; break;
919 case VERTRES: ret = 480; break;
920 case BITSPIXEL: ret = 1; break;
921 case PLANES: ret = 1; break;
922 case NUMCOLORS: ret = 2; break;
923 case ASPECTX: ret = 36; break;
924 case ASPECTY: ret = 36; break;
925 case ASPECTXY: ret = 51; break;
926 case LOGPIXELSX: ret = 72; break;
927 case LOGPIXELSY: ret = 72; break;
928 case SIZEPALETTE: ret = 2; break;
929 case TEXTCAPS:
930 ret = (TC_OP_CHARACTER | TC_OP_STROKE | TC_CP_STROKE |
931 TC_CR_ANY | TC_SF_X_YINDEP | TC_SA_DOUBLE | TC_SA_INTEGER |
932 TC_SA_CONTIN | TC_UA_ABLE | TC_SO_ABLE | TC_RA_ABLE | TC_VA_ABLE);
933 break;
935 release_dc_ptr( dc );
937 return ret;
941 /***********************************************************************
942 * GetBkColor (GDI32.@)
944 COLORREF WINAPI GetBkColor( HDC hdc )
946 COLORREF ret = 0;
947 DC * dc = get_dc_ptr( hdc );
948 if (dc)
950 ret = dc->backgroundColor;
951 release_dc_ptr( dc );
953 return ret;
957 /***********************************************************************
958 * SetBkColor (GDI32.@)
960 COLORREF WINAPI SetBkColor( HDC hdc, COLORREF color )
962 COLORREF oldColor;
963 DC * dc = get_dc_ptr( hdc );
965 TRACE("hdc=%p color=0x%08x\n", hdc, color);
967 if (!dc) return CLR_INVALID;
968 oldColor = dc->backgroundColor;
969 if (dc->funcs->pSetBkColor)
971 color = dc->funcs->pSetBkColor(dc->physDev, color);
972 if (color == CLR_INVALID) /* don't change it */
974 color = oldColor;
975 oldColor = CLR_INVALID;
978 dc->backgroundColor = color;
979 release_dc_ptr( dc );
980 return oldColor;
984 /***********************************************************************
985 * GetTextColor (GDI32.@)
987 COLORREF WINAPI GetTextColor( HDC hdc )
989 COLORREF ret = 0;
990 DC * dc = get_dc_ptr( hdc );
991 if (dc)
993 ret = dc->textColor;
994 release_dc_ptr( dc );
996 return ret;
1000 /***********************************************************************
1001 * SetTextColor (GDI32.@)
1003 COLORREF WINAPI SetTextColor( HDC hdc, COLORREF color )
1005 COLORREF oldColor;
1006 DC * dc = get_dc_ptr( hdc );
1008 TRACE(" hdc=%p color=0x%08x\n", hdc, color);
1010 if (!dc) return CLR_INVALID;
1011 oldColor = dc->textColor;
1012 if (dc->funcs->pSetTextColor)
1014 color = dc->funcs->pSetTextColor(dc->physDev, color);
1015 if (color == CLR_INVALID) /* don't change it */
1017 color = oldColor;
1018 oldColor = CLR_INVALID;
1021 dc->textColor = color;
1022 release_dc_ptr( dc );
1023 return oldColor;
1027 /***********************************************************************
1028 * GetTextAlign (GDI32.@)
1030 UINT WINAPI GetTextAlign( HDC hdc )
1032 UINT ret = 0;
1033 DC * dc = get_dc_ptr( hdc );
1034 if (dc)
1036 ret = dc->textAlign;
1037 release_dc_ptr( dc );
1039 return ret;
1043 /***********************************************************************
1044 * SetTextAlign (GDI32.@)
1046 UINT WINAPI SetTextAlign( HDC hdc, UINT align )
1048 UINT ret;
1049 DC *dc = get_dc_ptr( hdc );
1051 TRACE("hdc=%p align=%d\n", hdc, align);
1053 if (!dc) return 0x0;
1054 ret = dc->textAlign;
1055 if (dc->funcs->pSetTextAlign)
1056 if (!dc->funcs->pSetTextAlign(dc->physDev, align))
1057 ret = GDI_ERROR;
1058 if (ret != GDI_ERROR)
1059 dc->textAlign = align;
1060 release_dc_ptr( dc );
1061 return ret;
1064 /***********************************************************************
1065 * GetDCOrgEx (GDI32.@)
1067 BOOL WINAPI GetDCOrgEx( HDC hDC, LPPOINT lpp )
1069 DC * dc;
1071 if (!lpp) return FALSE;
1072 if (!(dc = get_dc_ptr( hDC ))) return FALSE;
1073 lpp->x = dc->vis_rect.left;
1074 lpp->y = dc->vis_rect.top;
1075 release_dc_ptr( dc );
1076 return TRUE;
1080 /***********************************************************************
1081 * GetGraphicsMode (GDI32.@)
1083 INT WINAPI GetGraphicsMode( HDC hdc )
1085 INT ret = 0;
1086 DC * dc = get_dc_ptr( hdc );
1087 if (dc)
1089 ret = dc->GraphicsMode;
1090 release_dc_ptr( dc );
1092 return ret;
1096 /***********************************************************************
1097 * SetGraphicsMode (GDI32.@)
1099 INT WINAPI SetGraphicsMode( HDC hdc, INT mode )
1101 INT ret = 0;
1102 DC *dc = get_dc_ptr( hdc );
1104 /* One would think that setting the graphics mode to GM_COMPATIBLE
1105 * would also reset the world transformation matrix to the unity
1106 * matrix. However, in Windows, this is not the case. This doesn't
1107 * make a lot of sense to me, but that's the way it is.
1109 if (!dc) return 0;
1110 if ((mode > 0) && (mode <= GM_LAST))
1112 ret = dc->GraphicsMode;
1113 dc->GraphicsMode = mode;
1115 release_dc_ptr( dc );
1116 return ret;
1120 /***********************************************************************
1121 * GetArcDirection (GDI32.@)
1123 INT WINAPI GetArcDirection( HDC hdc )
1125 INT ret = 0;
1126 DC * dc = get_dc_ptr( hdc );
1127 if (dc)
1129 ret = dc->ArcDirection;
1130 release_dc_ptr( dc );
1132 return ret;
1136 /***********************************************************************
1137 * SetArcDirection (GDI32.@)
1139 INT WINAPI SetArcDirection( HDC hdc, INT nDirection )
1141 DC * dc;
1142 INT nOldDirection = 0;
1144 if (nDirection!=AD_COUNTERCLOCKWISE && nDirection!=AD_CLOCKWISE)
1146 SetLastError(ERROR_INVALID_PARAMETER);
1147 return 0;
1150 if ((dc = get_dc_ptr( hdc )))
1152 if (dc->funcs->pSetArcDirection)
1154 dc->funcs->pSetArcDirection(dc->physDev, nDirection);
1156 nOldDirection = dc->ArcDirection;
1157 dc->ArcDirection = nDirection;
1158 release_dc_ptr( dc );
1160 return nOldDirection;
1164 /***********************************************************************
1165 * GetWorldTransform (GDI32.@)
1167 BOOL WINAPI GetWorldTransform( HDC hdc, LPXFORM xform )
1169 DC * dc;
1170 if (!xform) return FALSE;
1171 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
1172 *xform = dc->xformWorld2Wnd;
1173 release_dc_ptr( dc );
1174 return TRUE;
1178 /***********************************************************************
1179 * GetTransform (GDI32.@)
1181 * Undocumented
1183 * Returns one of the co-ordinate space transforms
1185 * PARAMS
1186 * hdc [I] Device context.
1187 * which [I] Which xform to return:
1188 * 0x203 World -> Page transform (that set by SetWorldTransform).
1189 * 0x304 Page -> Device transform (the mapping mode transform).
1190 * 0x204 World -> Device transform (the combination of the above two).
1191 * 0x402 Device -> World transform (the inversion of the above).
1192 * xform [O] The xform.
1195 BOOL WINAPI GetTransform( HDC hdc, DWORD which, XFORM *xform )
1197 BOOL ret = TRUE;
1198 DC *dc = get_dc_ptr( hdc );
1199 if (!dc) return FALSE;
1201 switch(which)
1203 case 0x203:
1204 *xform = dc->xformWorld2Wnd;
1205 break;
1207 case 0x304:
1208 construct_window_to_viewport(dc, xform);
1209 break;
1211 case 0x204:
1212 *xform = dc->xformWorld2Vport;
1213 break;
1215 case 0x402:
1216 *xform = dc->xformVport2World;
1217 break;
1219 default:
1220 FIXME("Unknown code %x\n", which);
1221 ret = FALSE;
1224 release_dc_ptr( dc );
1225 return ret;
1229 /***********************************************************************
1230 * SetWorldTransform (GDI32.@)
1232 BOOL WINAPI SetWorldTransform( HDC hdc, const XFORM *xform )
1234 BOOL ret = FALSE;
1235 DC *dc;
1237 if (!xform) return FALSE;
1239 dc = get_dc_ptr( hdc );
1240 if (!dc) return FALSE;
1242 /* Check that graphics mode is GM_ADVANCED */
1243 if (dc->GraphicsMode!=GM_ADVANCED) goto done;
1245 TRACE("eM11 %f eM12 %f eM21 %f eM22 %f eDx %f eDy %f\n",
1246 xform->eM11, xform->eM12, xform->eM21, xform->eM22, xform->eDx, xform->eDy);
1248 /* The transform must conform to (eM11 * eM22 != eM12 * eM21) requirement */
1249 if (xform->eM11 * xform->eM22 == xform->eM12 * xform->eM21) goto done;
1251 if (dc->funcs->pSetWorldTransform)
1253 ret = dc->funcs->pSetWorldTransform(dc->physDev, xform);
1254 if (!ret) goto done;
1257 dc->xformWorld2Wnd = *xform;
1258 DC_UpdateXforms( dc );
1259 ret = TRUE;
1260 done:
1261 release_dc_ptr( dc );
1262 return ret;
1266 /****************************************************************************
1267 * ModifyWorldTransform [GDI32.@]
1268 * Modifies the world transformation for a device context.
1270 * PARAMS
1271 * hdc [I] Handle to device context
1272 * xform [I] XFORM structure that will be used to modify the world
1273 * transformation
1274 * iMode [I] Specifies in what way to modify the world transformation
1275 * Possible values:
1276 * MWT_IDENTITY
1277 * Resets the world transformation to the identity matrix.
1278 * The parameter xform is ignored.
1279 * MWT_LEFTMULTIPLY
1280 * Multiplies xform into the world transformation matrix from
1281 * the left.
1282 * MWT_RIGHTMULTIPLY
1283 * Multiplies xform into the world transformation matrix from
1284 * the right.
1286 * RETURNS
1287 * Success: TRUE.
1288 * Failure: FALSE. Use GetLastError() to determine the cause.
1290 BOOL WINAPI ModifyWorldTransform( HDC hdc, const XFORM *xform,
1291 DWORD iMode )
1293 BOOL ret = FALSE;
1294 DC *dc = get_dc_ptr( hdc );
1296 /* Check for illegal parameters */
1297 if (!dc) return FALSE;
1298 if (!xform && iMode != MWT_IDENTITY) goto done;
1300 /* Check that graphics mode is GM_ADVANCED */
1301 if (dc->GraphicsMode!=GM_ADVANCED) goto done;
1303 if (dc->funcs->pModifyWorldTransform)
1305 ret = dc->funcs->pModifyWorldTransform(dc->physDev, xform, iMode);
1306 if (!ret) goto done;
1309 switch (iMode)
1311 case MWT_IDENTITY:
1312 dc->xformWorld2Wnd.eM11 = 1.0f;
1313 dc->xformWorld2Wnd.eM12 = 0.0f;
1314 dc->xformWorld2Wnd.eM21 = 0.0f;
1315 dc->xformWorld2Wnd.eM22 = 1.0f;
1316 dc->xformWorld2Wnd.eDx = 0.0f;
1317 dc->xformWorld2Wnd.eDy = 0.0f;
1318 break;
1319 case MWT_LEFTMULTIPLY:
1320 CombineTransform( &dc->xformWorld2Wnd, xform,
1321 &dc->xformWorld2Wnd );
1322 break;
1323 case MWT_RIGHTMULTIPLY:
1324 CombineTransform( &dc->xformWorld2Wnd, &dc->xformWorld2Wnd,
1325 xform );
1326 break;
1327 default:
1328 goto done;
1331 DC_UpdateXforms( dc );
1332 ret = TRUE;
1333 done:
1334 release_dc_ptr( dc );
1335 return ret;
1339 /****************************************************************************
1340 * CombineTransform [GDI32.@]
1341 * Combines two transformation matrices.
1343 * PARAMS
1344 * xformResult [O] Stores the result of combining the two matrices
1345 * xform1 [I] Specifies the first matrix to apply
1346 * xform2 [I] Specifies the second matrix to apply
1348 * REMARKS
1349 * The same matrix can be passed in for more than one of the parameters.
1351 * RETURNS
1352 * Success: TRUE.
1353 * Failure: FALSE. Use GetLastError() to determine the cause.
1355 BOOL WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
1356 const XFORM *xform2 )
1358 XFORM xformTemp;
1360 /* Check for illegal parameters */
1361 if (!xformResult || !xform1 || !xform2)
1362 return FALSE;
1364 /* Create the result in a temporary XFORM, since xformResult may be
1365 * equal to xform1 or xform2 */
1366 xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
1367 xform1->eM12 * xform2->eM21;
1368 xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
1369 xform1->eM12 * xform2->eM22;
1370 xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
1371 xform1->eM22 * xform2->eM21;
1372 xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
1373 xform1->eM22 * xform2->eM22;
1374 xformTemp.eDx = xform1->eDx * xform2->eM11 +
1375 xform1->eDy * xform2->eM21 +
1376 xform2->eDx;
1377 xformTemp.eDy = xform1->eDx * xform2->eM12 +
1378 xform1->eDy * xform2->eM22 +
1379 xform2->eDy;
1381 /* Copy the result to xformResult */
1382 *xformResult = xformTemp;
1384 return TRUE;
1388 /***********************************************************************
1389 * SetDCHook (GDI32.@)
1391 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1393 BOOL WINAPI SetDCHook( HDC hdc, DCHOOKPROC hookProc, DWORD_PTR dwHookData )
1395 DC *dc = get_dc_ptr( hdc );
1397 if (!dc) return FALSE;
1399 if (!(dc->flags & DC_SAVED))
1401 dc->dwHookData = dwHookData;
1402 dc->hookProc = hookProc;
1404 release_dc_ptr( dc );
1405 return TRUE;
1409 /***********************************************************************
1410 * GetDCHook (GDI32.@)
1412 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1414 DWORD_PTR WINAPI GetDCHook( HDC hdc, DCHOOKPROC *proc )
1416 DC *dc = get_dc_ptr( hdc );
1417 DWORD_PTR ret;
1419 if (!dc) return 0;
1420 if (proc) *proc = dc->hookProc;
1421 ret = dc->dwHookData;
1422 release_dc_ptr( dc );
1423 return ret;
1427 /***********************************************************************
1428 * SetHookFlags (GDI32.@)
1430 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1432 WORD WINAPI SetHookFlags( HDC hdc, WORD flags )
1434 DC *dc = get_dc_obj( hdc ); /* not get_dc_ptr, this needs to work from any thread */
1435 LONG ret = 0;
1437 if (!dc) return 0;
1439 /* "Undocumented Windows" info is slightly confusing. */
1441 TRACE("hDC %p, flags %04x\n",hdc,flags);
1443 if (flags & DCHF_INVALIDATEVISRGN)
1444 ret = InterlockedExchange( &dc->dirty, 1 );
1445 else if (flags & DCHF_VALIDATEVISRGN || !flags)
1446 ret = InterlockedExchange( &dc->dirty, 0 );
1448 GDI_ReleaseObj( hdc );
1449 return ret;
1452 /***********************************************************************
1453 * SetICMMode (GDI32.@)
1455 INT WINAPI SetICMMode(HDC hdc, INT iEnableICM)
1457 /*FIXME: Assume that ICM is always off, and cannot be turned on */
1458 if (iEnableICM == ICM_OFF) return ICM_OFF;
1459 if (iEnableICM == ICM_ON) return 0;
1460 if (iEnableICM == ICM_QUERY) return ICM_OFF;
1461 return 0;
1464 /***********************************************************************
1465 * GetDeviceGammaRamp (GDI32.@)
1467 BOOL WINAPI GetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1469 BOOL ret = FALSE;
1470 DC *dc = get_dc_ptr( hDC );
1472 if( dc )
1474 if (dc->funcs->pGetDeviceGammaRamp)
1475 ret = dc->funcs->pGetDeviceGammaRamp(dc->physDev, ptr);
1476 release_dc_ptr( dc );
1478 return ret;
1481 /***********************************************************************
1482 * SetDeviceGammaRamp (GDI32.@)
1484 BOOL WINAPI SetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1486 BOOL ret = FALSE;
1487 DC *dc = get_dc_ptr( hDC );
1489 if( dc )
1491 if (dc->funcs->pSetDeviceGammaRamp)
1492 ret = dc->funcs->pSetDeviceGammaRamp(dc->physDev, ptr);
1493 release_dc_ptr( dc );
1495 return ret;
1498 /***********************************************************************
1499 * GetColorSpace (GDI32.@)
1501 HCOLORSPACE WINAPI GetColorSpace(HDC hdc)
1503 /*FIXME Need to to whatever GetColorSpace actually does */
1504 return 0;
1507 /***********************************************************************
1508 * CreateColorSpaceA (GDI32.@)
1510 HCOLORSPACE WINAPI CreateColorSpaceA( LPLOGCOLORSPACEA lpLogColorSpace )
1512 FIXME( "stub\n" );
1513 return 0;
1516 /***********************************************************************
1517 * CreateColorSpaceW (GDI32.@)
1519 HCOLORSPACE WINAPI CreateColorSpaceW( LPLOGCOLORSPACEW lpLogColorSpace )
1521 FIXME( "stub\n" );
1522 return 0;
1525 /***********************************************************************
1526 * DeleteColorSpace (GDI32.@)
1528 BOOL WINAPI DeleteColorSpace( HCOLORSPACE hColorSpace )
1530 FIXME( "stub\n" );
1532 return TRUE;
1535 /***********************************************************************
1536 * SetColorSpace (GDI32.@)
1538 HCOLORSPACE WINAPI SetColorSpace( HDC hDC, HCOLORSPACE hColorSpace )
1540 FIXME( "stub\n" );
1542 return hColorSpace;
1545 /***********************************************************************
1546 * GetBoundsRect (GDI32.@)
1548 UINT WINAPI GetBoundsRect(HDC hdc, LPRECT rect, UINT flags)
1550 UINT ret;
1551 DC *dc = get_dc_ptr( hdc );
1553 if ( !dc ) return 0;
1555 if (rect)
1557 *rect = dc->BoundsRect;
1558 ret = ((dc->flags & DC_BOUNDS_SET) ? DCB_SET : DCB_RESET);
1560 else
1561 ret = 0;
1563 if (flags & DCB_RESET)
1565 dc->BoundsRect.left = 0;
1566 dc->BoundsRect.top = 0;
1567 dc->BoundsRect.right = 0;
1568 dc->BoundsRect.bottom = 0;
1569 dc->flags &= ~DC_BOUNDS_SET;
1571 release_dc_ptr( dc );
1572 return ret;
1576 /***********************************************************************
1577 * SetBoundsRect (GDI32.@)
1579 UINT WINAPI SetBoundsRect(HDC hdc, const RECT* rect, UINT flags)
1581 UINT ret;
1582 DC *dc;
1584 if ((flags & DCB_ENABLE) && (flags & DCB_DISABLE)) return 0;
1585 if (!(dc = get_dc_ptr( hdc ))) return 0;
1587 ret = ((dc->flags & DC_BOUNDS_ENABLE) ? DCB_ENABLE : DCB_DISABLE) |
1588 ((dc->flags & DC_BOUNDS_SET) ? DCB_SET : DCB_RESET);
1590 if (flags & DCB_RESET)
1592 dc->BoundsRect.left = 0;
1593 dc->BoundsRect.top = 0;
1594 dc->BoundsRect.right = 0;
1595 dc->BoundsRect.bottom = 0;
1596 dc->flags &= ~DC_BOUNDS_SET;
1599 if ((flags & DCB_ACCUMULATE) && rect && rect->left < rect->right && rect->top < rect->bottom)
1601 if (dc->flags & DC_BOUNDS_SET)
1603 dc->BoundsRect.left = min( dc->BoundsRect.left, rect->left );
1604 dc->BoundsRect.top = min( dc->BoundsRect.top, rect->top );
1605 dc->BoundsRect.right = max( dc->BoundsRect.right, rect->right );
1606 dc->BoundsRect.bottom = max( dc->BoundsRect.bottom, rect->bottom );
1608 else
1610 dc->BoundsRect = *rect;
1611 dc->flags |= DC_BOUNDS_SET;
1615 if (flags & DCB_ENABLE) dc->flags |= DC_BOUNDS_ENABLE;
1616 if (flags & DCB_DISABLE) dc->flags &= ~DC_BOUNDS_ENABLE;
1618 release_dc_ptr( dc );
1619 return ret;
1623 /***********************************************************************
1624 * GetRelAbs (GDI32.@)
1626 INT WINAPI GetRelAbs( HDC hdc, DWORD dwIgnore )
1628 INT ret = 0;
1629 DC *dc = get_dc_ptr( hdc );
1630 if (dc)
1632 ret = dc->relAbsMode;
1633 release_dc_ptr( dc );
1635 return ret;
1641 /***********************************************************************
1642 * GetBkMode (GDI32.@)
1644 INT WINAPI GetBkMode( HDC hdc )
1646 INT ret = 0;
1647 DC * dc = get_dc_ptr( hdc );
1648 if (dc)
1650 ret = dc->backgroundMode;
1651 release_dc_ptr( dc );
1653 return ret;
1657 /***********************************************************************
1658 * SetBkMode (GDI32.@)
1660 INT WINAPI SetBkMode( HDC hdc, INT mode )
1662 INT ret;
1663 DC *dc;
1664 if ((mode <= 0) || (mode > BKMODE_LAST))
1666 SetLastError(ERROR_INVALID_PARAMETER);
1667 return 0;
1669 if (!(dc = get_dc_ptr( hdc ))) return 0;
1671 ret = dc->backgroundMode;
1672 if (dc->funcs->pSetBkMode)
1673 if (!dc->funcs->pSetBkMode( dc->physDev, mode ))
1674 ret = 0;
1675 if (ret)
1676 dc->backgroundMode = mode;
1677 release_dc_ptr( dc );
1678 return ret;
1682 /***********************************************************************
1683 * GetROP2 (GDI32.@)
1685 INT WINAPI GetROP2( HDC hdc )
1687 INT ret = 0;
1688 DC * dc = get_dc_ptr( hdc );
1689 if (dc)
1691 ret = dc->ROPmode;
1692 release_dc_ptr( dc );
1694 return ret;
1698 /***********************************************************************
1699 * SetROP2 (GDI32.@)
1701 INT WINAPI SetROP2( HDC hdc, INT mode )
1703 INT ret;
1704 DC *dc;
1705 if ((mode < R2_BLACK) || (mode > R2_WHITE))
1707 SetLastError(ERROR_INVALID_PARAMETER);
1708 return 0;
1710 if (!(dc = get_dc_ptr( hdc ))) return 0;
1711 ret = dc->ROPmode;
1712 if (dc->funcs->pSetROP2)
1713 if (!dc->funcs->pSetROP2( dc->physDev, mode ))
1714 ret = 0;
1715 if (ret)
1716 dc->ROPmode = mode;
1717 release_dc_ptr( dc );
1718 return ret;
1722 /***********************************************************************
1723 * SetRelAbs (GDI32.@)
1725 INT WINAPI SetRelAbs( HDC hdc, INT mode )
1727 INT ret;
1728 DC *dc;
1729 if ((mode != ABSOLUTE) && (mode != RELATIVE))
1731 SetLastError(ERROR_INVALID_PARAMETER);
1732 return 0;
1734 if (!(dc = get_dc_ptr( hdc ))) return 0;
1735 if (dc->funcs->pSetRelAbs)
1736 ret = dc->funcs->pSetRelAbs( dc->physDev, mode );
1737 else
1739 ret = dc->relAbsMode;
1740 dc->relAbsMode = mode;
1742 release_dc_ptr( dc );
1743 return ret;
1747 /***********************************************************************
1748 * GetPolyFillMode (GDI32.@)
1750 INT WINAPI GetPolyFillMode( HDC hdc )
1752 INT ret = 0;
1753 DC * dc = get_dc_ptr( hdc );
1754 if (dc)
1756 ret = dc->polyFillMode;
1757 release_dc_ptr( dc );
1759 return ret;
1763 /***********************************************************************
1764 * SetPolyFillMode (GDI32.@)
1766 INT WINAPI SetPolyFillMode( HDC hdc, INT mode )
1768 INT ret;
1769 DC *dc;
1770 if ((mode <= 0) || (mode > POLYFILL_LAST))
1772 SetLastError(ERROR_INVALID_PARAMETER);
1773 return 0;
1775 if (!(dc = get_dc_ptr( hdc ))) return 0;
1776 ret = dc->polyFillMode;
1777 if (dc->funcs->pSetPolyFillMode)
1778 if (!dc->funcs->pSetPolyFillMode( dc->physDev, mode ))
1779 ret = 0;
1780 if (ret)
1781 dc->polyFillMode = mode;
1782 release_dc_ptr( dc );
1783 return ret;
1787 /***********************************************************************
1788 * GetStretchBltMode (GDI32.@)
1790 INT WINAPI GetStretchBltMode( HDC hdc )
1792 INT ret = 0;
1793 DC * dc = get_dc_ptr( hdc );
1794 if (dc)
1796 ret = dc->stretchBltMode;
1797 release_dc_ptr( dc );
1799 return ret;
1803 /***********************************************************************
1804 * SetStretchBltMode (GDI32.@)
1806 INT WINAPI SetStretchBltMode( HDC hdc, INT mode )
1808 INT ret;
1809 DC *dc;
1810 if ((mode <= 0) || (mode > MAXSTRETCHBLTMODE))
1812 SetLastError(ERROR_INVALID_PARAMETER);
1813 return 0;
1815 if (!(dc = get_dc_ptr( hdc ))) return 0;
1816 ret = dc->stretchBltMode;
1817 if (dc->funcs->pSetStretchBltMode)
1818 if (!dc->funcs->pSetStretchBltMode( dc->physDev, mode ))
1819 ret = 0;
1820 if (ret)
1821 dc->stretchBltMode = mode;
1822 release_dc_ptr( dc );
1823 return ret;
1827 /***********************************************************************
1828 * GetMapMode (GDI32.@)
1830 INT WINAPI GetMapMode( HDC hdc )
1832 INT ret = 0;
1833 DC * dc = get_dc_ptr( hdc );
1834 if (dc)
1836 ret = dc->MapMode;
1837 release_dc_ptr( dc );
1839 return ret;
1843 /***********************************************************************
1844 * GetBrushOrgEx (GDI32.@)
1846 BOOL WINAPI GetBrushOrgEx( HDC hdc, LPPOINT pt )
1848 DC * dc = get_dc_ptr( hdc );
1849 if (!dc) return FALSE;
1850 pt->x = dc->brushOrgX;
1851 pt->y = dc->brushOrgY;
1852 release_dc_ptr( dc );
1853 return TRUE;
1857 /***********************************************************************
1858 * GetCurrentPositionEx (GDI32.@)
1860 BOOL WINAPI GetCurrentPositionEx( HDC hdc, LPPOINT pt )
1862 DC * dc = get_dc_ptr( hdc );
1863 if (!dc) return FALSE;
1864 pt->x = dc->CursPosX;
1865 pt->y = dc->CursPosY;
1866 release_dc_ptr( dc );
1867 return TRUE;
1871 /***********************************************************************
1872 * GetViewportExtEx (GDI32.@)
1874 BOOL WINAPI GetViewportExtEx( HDC hdc, LPSIZE size )
1876 DC * dc = get_dc_ptr( hdc );
1877 if (!dc) return FALSE;
1878 size->cx = dc->vportExtX;
1879 size->cy = dc->vportExtY;
1880 release_dc_ptr( dc );
1881 return TRUE;
1885 /***********************************************************************
1886 * GetViewportOrgEx (GDI32.@)
1888 BOOL WINAPI GetViewportOrgEx( HDC hdc, LPPOINT pt )
1890 DC * dc = get_dc_ptr( hdc );
1891 if (!dc) return FALSE;
1892 pt->x = dc->vportOrgX;
1893 pt->y = dc->vportOrgY;
1894 release_dc_ptr( dc );
1895 return TRUE;
1899 /***********************************************************************
1900 * GetWindowExtEx (GDI32.@)
1902 BOOL WINAPI GetWindowExtEx( HDC hdc, LPSIZE size )
1904 DC * dc = get_dc_ptr( hdc );
1905 if (!dc) return FALSE;
1906 size->cx = dc->wndExtX;
1907 size->cy = dc->wndExtY;
1908 release_dc_ptr( dc );
1909 return TRUE;
1913 /***********************************************************************
1914 * GetWindowOrgEx (GDI32.@)
1916 BOOL WINAPI GetWindowOrgEx( HDC hdc, LPPOINT pt )
1918 DC * dc = get_dc_ptr( hdc );
1919 if (!dc) return FALSE;
1920 pt->x = dc->wndOrgX;
1921 pt->y = dc->wndOrgY;
1922 release_dc_ptr( dc );
1923 return TRUE;
1927 /***********************************************************************
1928 * GetLayout (GDI32.@)
1930 * Gets left->right or right->left text layout flags of a dc.
1933 DWORD WINAPI GetLayout(HDC hdc)
1935 DWORD layout = GDI_ERROR;
1937 DC * dc = get_dc_ptr( hdc );
1938 if (dc)
1940 layout = dc->layout;
1941 release_dc_ptr( dc );
1944 TRACE("hdc : %p, layout : %08x\n", hdc, layout);
1946 return layout;
1949 /***********************************************************************
1950 * SetLayout (GDI32.@)
1952 * Sets left->right or right->left text layout flags of a dc.
1955 DWORD WINAPI SetLayout(HDC hdc, DWORD layout)
1957 DWORD oldlayout = GDI_ERROR;
1959 DC * dc = get_dc_ptr( hdc );
1960 if (dc)
1962 oldlayout = dc->layout;
1963 dc->layout = layout;
1964 if (layout != oldlayout)
1966 if (layout & LAYOUT_RTL) dc->MapMode = MM_ANISOTROPIC;
1967 DC_UpdateXforms( dc );
1969 release_dc_ptr( dc );
1972 TRACE("hdc : %p, old layout : %08x, new layout : %08x\n", hdc, oldlayout, layout);
1974 return oldlayout;
1977 /***********************************************************************
1978 * GetDCBrushColor (GDI32.@)
1980 * Retrieves the current brush color for the specified device
1981 * context (DC).
1984 COLORREF WINAPI GetDCBrushColor(HDC hdc)
1986 DC *dc;
1987 COLORREF dcBrushColor = CLR_INVALID;
1989 TRACE("hdc(%p)\n", hdc);
1991 dc = get_dc_ptr( hdc );
1992 if (dc)
1994 dcBrushColor = dc->dcBrushColor;
1995 release_dc_ptr( dc );
1998 return dcBrushColor;
2001 /***********************************************************************
2002 * SetDCBrushColor (GDI32.@)
2004 * Sets the current device context (DC) brush color to the specified
2005 * color value. If the device cannot represent the specified color
2006 * value, the color is set to the nearest physical color.
2009 COLORREF WINAPI SetDCBrushColor(HDC hdc, COLORREF crColor)
2011 DC *dc;
2012 COLORREF oldClr = CLR_INVALID;
2014 TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
2016 dc = get_dc_ptr( hdc );
2017 if (dc)
2019 if (dc->funcs->pSetDCBrushColor)
2020 crColor = dc->funcs->pSetDCBrushColor( dc->physDev, crColor );
2021 else if (dc->hBrush == GetStockObject( DC_BRUSH ))
2023 /* If DC_BRUSH is selected, update driver pen color */
2024 HBRUSH hBrush = CreateSolidBrush( crColor );
2025 dc->funcs->pSelectBrush( dc->physDev, hBrush );
2026 DeleteObject( hBrush );
2029 if (crColor != CLR_INVALID)
2031 oldClr = dc->dcBrushColor;
2032 dc->dcBrushColor = crColor;
2035 release_dc_ptr( dc );
2038 return oldClr;
2041 /***********************************************************************
2042 * GetDCPenColor (GDI32.@)
2044 * Retrieves the current pen color for the specified device
2045 * context (DC).
2048 COLORREF WINAPI GetDCPenColor(HDC hdc)
2050 DC *dc;
2051 COLORREF dcPenColor = CLR_INVALID;
2053 TRACE("hdc(%p)\n", hdc);
2055 dc = get_dc_ptr( hdc );
2056 if (dc)
2058 dcPenColor = dc->dcPenColor;
2059 release_dc_ptr( dc );
2062 return dcPenColor;
2065 /***********************************************************************
2066 * SetDCPenColor (GDI32.@)
2068 * Sets the current device context (DC) pen color to the specified
2069 * color value. If the device cannot represent the specified color
2070 * value, the color is set to the nearest physical color.
2073 COLORREF WINAPI SetDCPenColor(HDC hdc, COLORREF crColor)
2075 DC *dc;
2076 COLORREF oldClr = CLR_INVALID;
2078 TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
2080 dc = get_dc_ptr( hdc );
2081 if (dc)
2083 if (dc->funcs->pSetDCPenColor)
2084 crColor = dc->funcs->pSetDCPenColor( dc->physDev, crColor );
2085 else if (dc->hPen == GetStockObject( DC_PEN ))
2087 /* If DC_PEN is selected, update the driver pen color */
2088 LOGPEN logpen = { PS_SOLID, { 0, 0 }, crColor };
2089 HPEN hPen = CreatePenIndirect( &logpen );
2090 dc->funcs->pSelectPen( dc->physDev, hPen );
2091 DeleteObject( hPen );
2094 if (crColor != CLR_INVALID)
2096 oldClr = dc->dcPenColor;
2097 dc->dcPenColor = crColor;
2100 release_dc_ptr( dc );
2103 return oldClr;
2106 /***********************************************************************
2107 * CancelDC (GDI32.@)
2109 BOOL WINAPI CancelDC(HDC hdc)
2111 FIXME("stub\n");
2112 return TRUE;
2115 /*******************************************************************
2116 * GetMiterLimit [GDI32.@]
2120 BOOL WINAPI GetMiterLimit(HDC hdc, PFLOAT peLimit)
2122 BOOL bRet = FALSE;
2123 DC *dc;
2125 TRACE("(%p,%p)\n", hdc, peLimit);
2127 dc = get_dc_ptr( hdc );
2128 if (dc)
2130 if (peLimit)
2131 *peLimit = dc->miterLimit;
2133 release_dc_ptr( dc );
2134 bRet = TRUE;
2136 return bRet;
2139 /*******************************************************************
2140 * SetMiterLimit [GDI32.@]
2144 BOOL WINAPI SetMiterLimit(HDC hdc, FLOAT eNewLimit, PFLOAT peOldLimit)
2146 BOOL bRet = FALSE;
2147 DC *dc;
2149 TRACE("(%p,%f,%p)\n", hdc, eNewLimit, peOldLimit);
2151 dc = get_dc_ptr( hdc );
2152 if (dc)
2154 if (peOldLimit)
2155 *peOldLimit = dc->miterLimit;
2156 dc->miterLimit = eNewLimit;
2157 release_dc_ptr( dc );
2158 bRet = TRUE;
2160 return bRet;
2163 /*******************************************************************
2164 * GdiIsMetaPrintDC [GDI32.@]
2166 BOOL WINAPI GdiIsMetaPrintDC(HDC hdc)
2168 FIXME("%p\n", hdc);
2169 return FALSE;
2172 /*******************************************************************
2173 * GdiIsMetaFileDC [GDI32.@]
2175 BOOL WINAPI GdiIsMetaFileDC(HDC hdc)
2177 TRACE("%p\n", hdc);
2179 switch( GetObjectType( hdc ) )
2181 case OBJ_METADC:
2182 case OBJ_ENHMETADC:
2183 return TRUE;
2185 return FALSE;
2188 /*******************************************************************
2189 * GdiIsPlayMetafileDC [GDI32.@]
2191 BOOL WINAPI GdiIsPlayMetafileDC(HDC hdc)
2193 FIXME("%p\n", hdc);
2194 return FALSE;