Set the encoding of the remaining German resources to UTF-8.
[wine/multimedia.git] / dlls / gdi32 / dc.c
blobdb8a91bab7467e9fdbd2596c21e4fc18c132fb01
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->hookThunk = NULL;
91 dc->wndOrgX = 0;
92 dc->wndOrgY = 0;
93 dc->wndExtX = 1;
94 dc->wndExtY = 1;
95 dc->vportOrgX = 0;
96 dc->vportOrgY = 0;
97 dc->vportExtX = 1;
98 dc->vportExtY = 1;
99 dc->miterLimit = 10.0f; /* 10.0 is the default, from MSDN */
100 dc->flags = 0;
101 dc->layout = 0;
102 dc->hClipRgn = 0;
103 dc->hMetaRgn = 0;
104 dc->hMetaClipRgn = 0;
105 dc->hVisRgn = 0;
106 dc->hPen = GDI_inc_ref_count( GetStockObject( BLACK_PEN ));
107 dc->hBrush = GDI_inc_ref_count( GetStockObject( WHITE_BRUSH ));
108 dc->hFont = GDI_inc_ref_count( GetStockObject( SYSTEM_FONT ));
109 dc->hBitmap = 0;
110 dc->hDevice = 0;
111 dc->hPalette = GetStockObject( DEFAULT_PALETTE );
112 dc->gdiFont = 0;
113 dc->font_code_page = CP_ACP;
114 dc->ROPmode = R2_COPYPEN;
115 dc->polyFillMode = ALTERNATE;
116 dc->stretchBltMode = BLACKONWHITE;
117 dc->relAbsMode = ABSOLUTE;
118 dc->backgroundMode = OPAQUE;
119 dc->backgroundColor = RGB( 255, 255, 255 );
120 dc->dcBrushColor = RGB( 255, 255, 255 );
121 dc->dcPenColor = RGB( 0, 0, 0 );
122 dc->textColor = RGB( 0, 0, 0 );
123 dc->brushOrgX = 0;
124 dc->brushOrgY = 0;
125 dc->textAlign = TA_LEFT | TA_TOP | TA_NOUPDATECP;
126 dc->charExtra = 0;
127 dc->breakExtra = 0;
128 dc->breakRem = 0;
129 dc->MapMode = MM_TEXT;
130 dc->GraphicsMode = GM_COMPATIBLE;
131 dc->pAbortProc = NULL;
132 dc->CursPosX = 0;
133 dc->CursPosY = 0;
134 dc->ArcDirection = AD_COUNTERCLOCKWISE;
135 dc->xformWorld2Wnd.eM11 = 1.0f;
136 dc->xformWorld2Wnd.eM12 = 0.0f;
137 dc->xformWorld2Wnd.eM21 = 0.0f;
138 dc->xformWorld2Wnd.eM22 = 1.0f;
139 dc->xformWorld2Wnd.eDx = 0.0f;
140 dc->xformWorld2Wnd.eDy = 0.0f;
141 dc->xformWorld2Vport = dc->xformWorld2Wnd;
142 dc->xformVport2World = dc->xformWorld2Wnd;
143 dc->vport2WorldValid = TRUE;
144 dc->BoundsRect.left = 0;
145 dc->BoundsRect.top = 0;
146 dc->BoundsRect.right = 0;
147 dc->BoundsRect.bottom = 0;
148 dc->saved_visrgn = NULL;
149 PATH_InitGdiPath(&dc->path);
151 if (!(dc->hSelf = alloc_gdi_handle( &dc->header, magic, &dc_funcs )))
153 HeapFree( GetProcessHeap(), 0, dc );
154 dc = NULL;
156 return dc;
161 /***********************************************************************
162 * free_dc_ptr
164 BOOL free_dc_ptr( DC *dc )
166 assert( dc->refcount == 1 );
167 if (free_gdi_handle( dc->hSelf ) != dc) return FALSE; /* shouldn't happen */
168 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
169 if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
170 if (dc->hMetaClipRgn) DeleteObject( dc->hMetaClipRgn );
171 if (dc->hVisRgn) DeleteObject( dc->hVisRgn );
172 PATH_DestroyGdiPath( &dc->path );
173 return HeapFree( GetProcessHeap(), 0, dc );
177 /***********************************************************************
178 * get_dc_ptr
180 * Retrieve a DC pointer but release the GDI lock.
182 DC *get_dc_ptr( HDC hdc )
184 DC *dc = get_dc_obj( hdc );
185 if (!dc) return NULL;
187 if (!InterlockedCompareExchange( &dc->refcount, 1, 0 ))
189 dc->thread = GetCurrentThreadId();
191 else if (dc->thread != GetCurrentThreadId())
193 WARN( "dc %p belongs to thread %04x\n", hdc, dc->thread );
194 GDI_ReleaseObj( hdc );
195 return NULL;
197 else InterlockedIncrement( &dc->refcount );
199 GDI_ReleaseObj( hdc );
200 return dc;
204 /***********************************************************************
205 * release_dc_ptr
207 void release_dc_ptr( DC *dc )
209 LONG ref;
211 dc->thread = 0;
212 ref = InterlockedDecrement( &dc->refcount );
213 assert( ref >= 0 );
214 if (ref) dc->thread = GetCurrentThreadId(); /* we still own it */
218 /***********************************************************************
219 * update_dc
221 * Make sure the DC vis region is up to date.
222 * This function may call up to USER so the GDI lock should _not_
223 * be held when calling it.
225 void update_dc( DC *dc )
227 if (InterlockedExchange( &dc->dirty, 0 ) && dc->hookThunk)
228 dc->hookThunk( dc->hSelf, DCHC_INVALIDVISRGN, dc->dwHookData, 0 );
232 /***********************************************************************
233 * DC_DeleteObject
235 static BOOL DC_DeleteObject( HGDIOBJ handle )
237 return DeleteDC( handle );
241 /***********************************************************************
242 * DC_InitDC
244 * Setup device-specific DC values for a newly created DC.
246 void DC_InitDC( DC* dc )
248 if (dc->funcs->pRealizeDefaultPalette) dc->funcs->pRealizeDefaultPalette( dc->physDev );
249 SetTextColor( dc->hSelf, dc->textColor );
250 SetBkColor( dc->hSelf, dc->backgroundColor );
251 SelectObject( dc->hSelf, dc->hPen );
252 SelectObject( dc->hSelf, dc->hBrush );
253 SelectObject( dc->hSelf, dc->hFont );
254 CLIPPING_UpdateGCRegion( dc );
255 SetVirtualResolution( dc->hSelf, 0, 0, 0, 0 );
259 /***********************************************************************
260 * DC_InvertXform
262 * Computes the inverse of the transformation xformSrc and stores it to
263 * xformDest. Returns TRUE if successful or FALSE if the xformSrc matrix
264 * is singular.
266 static BOOL DC_InvertXform( const XFORM *xformSrc, XFORM *xformDest )
268 double determinant;
270 determinant = xformSrc->eM11*xformSrc->eM22 -
271 xformSrc->eM12*xformSrc->eM21;
272 if (determinant > -1e-12 && determinant < 1e-12)
273 return FALSE;
275 xformDest->eM11 = xformSrc->eM22 / determinant;
276 xformDest->eM12 = -xformSrc->eM12 / determinant;
277 xformDest->eM21 = -xformSrc->eM21 / determinant;
278 xformDest->eM22 = xformSrc->eM11 / determinant;
279 xformDest->eDx = -xformSrc->eDx * xformDest->eM11 -
280 xformSrc->eDy * xformDest->eM21;
281 xformDest->eDy = -xformSrc->eDx * xformDest->eM12 -
282 xformSrc->eDy * xformDest->eM22;
284 return TRUE;
287 /* Construct a transformation to do the window-to-viewport conversion */
288 static void construct_window_to_viewport(DC *dc, XFORM *xform)
290 double scaleX, scaleY;
291 scaleX = (double)dc->vportExtX / (double)dc->wndExtX;
292 scaleY = (double)dc->vportExtY / (double)dc->wndExtY;
294 xform->eM11 = scaleX;
295 xform->eM12 = 0.0;
296 xform->eM21 = 0.0;
297 xform->eM22 = scaleY;
298 xform->eDx = (double)dc->vportOrgX - scaleX * (double)dc->wndOrgX;
299 xform->eDy = (double)dc->vportOrgY - scaleY * (double)dc->wndOrgY;
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->hookThunk = NULL;
407 newdc->hookProc = 0;
408 newdc->saved_visrgn = NULL;
410 if (!(newdc->hSelf = alloc_gdi_handle( &newdc->header, dc->header.type, &dc_funcs )))
412 HeapFree( GetProcessHeap(), 0, newdc );
413 release_dc_ptr( dc );
414 return 0;
417 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
419 newdc->hVisRgn = 0;
420 newdc->hClipRgn = 0;
421 newdc->hMetaRgn = 0;
422 newdc->hMetaClipRgn = 0;
423 if (dc->hClipRgn)
425 newdc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
426 CombineRgn( newdc->hClipRgn, dc->hClipRgn, 0, RGN_COPY );
428 if (dc->hMetaRgn)
430 newdc->hMetaRgn = CreateRectRgn( 0, 0, 0, 0 );
431 CombineRgn( newdc->hMetaRgn, dc->hMetaRgn, 0, RGN_COPY );
433 /* don't bother recomputing hMetaClipRgn, we'll do that in SetDCState */
435 if (!PATH_AssignGdiPath( &newdc->path, &dc->path ))
437 release_dc_ptr( dc );
438 free_dc_ptr( newdc );
439 return 0;
442 newdc->saved_dc = dc->saved_dc;
443 dc->saved_dc = newdc->hSelf;
444 ret = ++dc->saveLevel;
445 release_dc_ptr( newdc );
446 release_dc_ptr( dc );
447 return ret;
451 /***********************************************************************
452 * restore_dc_state
454 BOOL restore_dc_state( HDC hdc, INT level )
456 HDC hdcs, first_dcs;
457 DC *dc, *dcs;
458 INT save_level;
460 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
462 /* find the state level to restore */
464 if (abs(level) > dc->saveLevel || level == 0)
466 release_dc_ptr( dc );
467 return FALSE;
469 if (level < 0) level = dc->saveLevel + level + 1;
470 first_dcs = dc->saved_dc;
471 for (hdcs = first_dcs, save_level = dc->saveLevel; save_level > level; save_level--)
473 if (!(dcs = get_dc_ptr( hdcs )))
475 release_dc_ptr( dc );
476 return FALSE;
478 hdcs = dcs->saved_dc;
479 release_dc_ptr( dcs );
482 /* restore the state */
484 if (!(dcs = get_dc_ptr( hdcs )))
486 release_dc_ptr( dc );
487 return FALSE;
489 if (!PATH_AssignGdiPath( &dc->path, &dcs->path ))
491 release_dc_ptr( dcs );
492 release_dc_ptr( dc );
493 return FALSE;
496 dc->flags = dcs->flags & ~DC_SAVED;
497 dc->layout = dcs->layout;
498 dc->hDevice = dcs->hDevice;
499 dc->ROPmode = dcs->ROPmode;
500 dc->polyFillMode = dcs->polyFillMode;
501 dc->stretchBltMode = dcs->stretchBltMode;
502 dc->relAbsMode = dcs->relAbsMode;
503 dc->backgroundMode = dcs->backgroundMode;
504 dc->backgroundColor = dcs->backgroundColor;
505 dc->textColor = dcs->textColor;
506 dc->dcBrushColor = dcs->dcBrushColor;
507 dc->dcPenColor = dcs->dcPenColor;
508 dc->brushOrgX = dcs->brushOrgX;
509 dc->brushOrgY = dcs->brushOrgY;
510 dc->textAlign = dcs->textAlign;
511 dc->charExtra = dcs->charExtra;
512 dc->breakExtra = dcs->breakExtra;
513 dc->breakRem = dcs->breakRem;
514 dc->MapMode = dcs->MapMode;
515 dc->GraphicsMode = dcs->GraphicsMode;
516 dc->CursPosX = dcs->CursPosX;
517 dc->CursPosY = dcs->CursPosY;
518 dc->ArcDirection = dcs->ArcDirection;
519 dc->xformWorld2Wnd = dcs->xformWorld2Wnd;
520 dc->xformWorld2Vport = dcs->xformWorld2Vport;
521 dc->xformVport2World = dcs->xformVport2World;
522 dc->vport2WorldValid = dcs->vport2WorldValid;
523 dc->BoundsRect = dcs->BoundsRect;
525 dc->wndOrgX = dcs->wndOrgX;
526 dc->wndOrgY = dcs->wndOrgY;
527 dc->wndExtX = dcs->wndExtX;
528 dc->wndExtY = dcs->wndExtY;
529 dc->vportOrgX = dcs->vportOrgX;
530 dc->vportOrgY = dcs->vportOrgY;
531 dc->vportExtX = dcs->vportExtX;
532 dc->vportExtY = dcs->vportExtY;
533 dc->virtual_res = dcs->virtual_res;
534 dc->virtual_size = dcs->virtual_size;
536 if (dcs->hClipRgn)
538 if (!dc->hClipRgn) dc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
539 CombineRgn( dc->hClipRgn, dcs->hClipRgn, 0, RGN_COPY );
541 else
543 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
544 dc->hClipRgn = 0;
546 if (dcs->hMetaRgn)
548 if (!dc->hMetaRgn) dc->hMetaRgn = CreateRectRgn( 0, 0, 0, 0 );
549 CombineRgn( dc->hMetaRgn, dcs->hMetaRgn, 0, RGN_COPY );
551 else
553 if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
554 dc->hMetaRgn = 0;
556 CLIPPING_UpdateGCRegion( dc );
558 SelectObject( hdc, dcs->hBitmap );
559 SelectObject( hdc, dcs->hBrush );
560 SelectObject( hdc, dcs->hFont );
561 SelectObject( hdc, dcs->hPen );
562 SetBkColor( hdc, dcs->backgroundColor);
563 SetTextColor( hdc, dcs->textColor);
564 GDISelectPalette( hdc, dcs->hPalette, FALSE );
566 dc->saved_dc = dcs->saved_dc;
567 dcs->saved_dc = 0;
568 dc->saveLevel = save_level - 1;
570 release_dc_ptr( dcs );
572 /* now destroy all the saved DCs */
574 while (first_dcs)
576 if (!(dcs = get_dc_ptr( first_dcs ))) break;
577 hdcs = dcs->saved_dc;
578 free_dc_ptr( dcs );
579 first_dcs = hdcs;
581 release_dc_ptr( dc );
582 return TRUE;
586 /***********************************************************************
587 * SaveDC (GDI32.@)
589 INT WINAPI SaveDC( HDC hdc )
591 DC * dc;
592 INT ret;
594 if (!(dc = get_dc_ptr( hdc ))) return 0;
596 if(dc->funcs->pSaveDC)
597 ret = dc->funcs->pSaveDC( dc->physDev );
598 else
599 ret = save_dc_state( hdc );
601 release_dc_ptr( dc );
602 return ret;
606 /***********************************************************************
607 * RestoreDC (GDI32.@)
609 BOOL WINAPI RestoreDC( HDC hdc, INT level )
611 DC *dc;
612 BOOL success;
614 TRACE("%p %d\n", hdc, level );
615 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
616 update_dc( dc );
618 if(dc->funcs->pRestoreDC)
619 success = dc->funcs->pRestoreDC( dc->physDev, level );
620 else
621 success = restore_dc_state( hdc, level );
623 release_dc_ptr( dc );
624 return success;
628 /***********************************************************************
629 * CreateDCW (GDI32.@)
631 HDC WINAPI CreateDCW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
632 const DEVMODEW *initData )
634 HDC hdc;
635 DC * dc;
636 const DC_FUNCTIONS *funcs;
637 WCHAR buf[300];
639 GDI_CheckNotLock();
641 if (!device || !DRIVER_GetDriverName( device, buf, 300 ))
643 if (!driver)
645 ERR( "no device found for %s\n", debugstr_w(device) );
646 return 0;
648 strcpyW(buf, driver);
651 if (!(funcs = DRIVER_load_driver( buf )))
653 ERR( "no driver found for %s\n", debugstr_w(buf) );
654 return 0;
656 if (!(dc = alloc_dc_ptr( funcs, OBJ_DC ))) goto error;
657 hdc = dc->hSelf;
659 dc->hBitmap = GDI_inc_ref_count( GetStockObject( DEFAULT_BITMAP ));
660 if (!(dc->hVisRgn = CreateRectRgn( 0, 0, 1, 1 ))) goto error;
662 TRACE("(driver=%s, device=%s, output=%s): returning %p\n",
663 debugstr_w(driver), debugstr_w(device), debugstr_w(output), dc->hSelf );
665 if (dc->funcs->pCreateDC &&
666 !dc->funcs->pCreateDC( hdc, &dc->physDev, buf, device, output, initData ))
668 WARN("creation aborted by device\n" );
669 goto error;
672 SetRectRgn( dc->hVisRgn, 0, 0,
673 GetDeviceCaps( hdc, DESKTOPHORZRES ), GetDeviceCaps( hdc, DESKTOPVERTRES ) );
675 DC_InitDC( dc );
676 release_dc_ptr( dc );
677 return hdc;
679 error:
680 if (dc) free_dc_ptr( dc );
681 DRIVER_release_driver( funcs );
682 return 0;
686 /***********************************************************************
687 * CreateDCA (GDI32.@)
689 HDC WINAPI CreateDCA( LPCSTR driver, LPCSTR device, LPCSTR output,
690 const DEVMODEA *initData )
692 UNICODE_STRING driverW, deviceW, outputW;
693 DEVMODEW *initDataW;
694 HDC ret;
696 if (driver) RtlCreateUnicodeStringFromAsciiz(&driverW, driver);
697 else driverW.Buffer = NULL;
699 if (device) RtlCreateUnicodeStringFromAsciiz(&deviceW, device);
700 else deviceW.Buffer = NULL;
702 if (output) RtlCreateUnicodeStringFromAsciiz(&outputW, output);
703 else outputW.Buffer = NULL;
705 initDataW = NULL;
706 if (initData)
708 /* don't convert initData for DISPLAY driver, it's not used */
709 if (!driverW.Buffer || strcmpiW( driverW.Buffer, displayW ))
710 initDataW = GdiConvertToDevmodeW(initData);
713 ret = CreateDCW( driverW.Buffer, deviceW.Buffer, outputW.Buffer, initDataW );
715 RtlFreeUnicodeString(&driverW);
716 RtlFreeUnicodeString(&deviceW);
717 RtlFreeUnicodeString(&outputW);
718 HeapFree(GetProcessHeap(), 0, initDataW);
719 return ret;
723 /***********************************************************************
724 * CreateICA (GDI32.@)
726 HDC WINAPI CreateICA( LPCSTR driver, LPCSTR device, LPCSTR output,
727 const DEVMODEA* initData )
729 /* Nothing special yet for ICs */
730 return CreateDCA( driver, device, output, initData );
734 /***********************************************************************
735 * CreateICW (GDI32.@)
737 HDC WINAPI CreateICW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
738 const DEVMODEW* initData )
740 /* Nothing special yet for ICs */
741 return CreateDCW( driver, device, output, initData );
745 /***********************************************************************
746 * CreateCompatibleDC (GDI32.@)
748 HDC WINAPI CreateCompatibleDC( HDC hdc )
750 DC *dc, *origDC;
751 HDC ret;
752 const DC_FUNCTIONS *funcs = NULL;
753 PHYSDEV physDev = NULL;
755 GDI_CheckNotLock();
757 if ((origDC = get_dc_ptr( hdc )))
759 if (GetObjectType( hdc ) == OBJ_DC)
761 funcs = origDC->funcs;
762 physDev = origDC->physDev;
764 release_dc_ptr( origDC );
765 if (funcs) funcs = DRIVER_get_driver( funcs );
767 else if (hdc) return 0;
769 if (!funcs && !(funcs = DRIVER_load_driver( displayW ))) 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 if (!(dc->hVisRgn = CreateRectRgn( 0, 0, 1, 1 ))) goto error; /* default bitmap is 1x1 */
778 /* Copy the driver-specific physical device info into
779 * the new DC. The driver may use this read-only info
780 * while creating the compatible DC below. */
781 dc->physDev = physDev;
782 ret = dc->hSelf;
784 if (dc->funcs->pCreateDC &&
785 !dc->funcs->pCreateDC( dc->hSelf, &dc->physDev, NULL, NULL, NULL, NULL ))
787 WARN("creation aborted by device\n");
788 goto error;
791 DC_InitDC( dc );
792 release_dc_ptr( dc );
793 return ret;
795 error:
796 if (dc) free_dc_ptr( dc );
797 DRIVER_release_driver( funcs );
798 return 0;
802 /***********************************************************************
803 * DeleteDC (GDI32.@)
805 BOOL WINAPI DeleteDC( HDC hdc )
807 const DC_FUNCTIONS *funcs = NULL;
808 DC * dc;
810 TRACE("%p\n", hdc );
812 GDI_CheckNotLock();
814 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
815 if (dc->refcount != 1)
817 FIXME( "not deleting busy DC %p refcount %u\n", dc->hSelf, dc->refcount );
818 release_dc_ptr( dc );
819 return FALSE;
822 /* Call hook procedure to check whether is it OK to delete this DC */
823 if (dc->hookThunk && !dc->hookThunk( hdc, DCHC_DELETEDC, dc->dwHookData, 0 ))
825 release_dc_ptr( dc );
826 return FALSE;
829 while (dc->saveLevel)
831 DC * dcs;
832 HDC hdcs = dc->saved_dc;
833 if (!(dcs = get_dc_ptr( hdcs ))) break;
834 dc->saved_dc = dcs->saved_dc;
835 dc->saveLevel--;
836 free_dc_ptr( dcs );
839 if (!(dc->flags & DC_SAVED))
841 SelectObject( hdc, GetStockObject(BLACK_PEN) );
842 SelectObject( hdc, GetStockObject(WHITE_BRUSH) );
843 SelectObject( hdc, GetStockObject(SYSTEM_FONT) );
844 SelectObject( hdc, GetStockObject(DEFAULT_BITMAP) );
845 funcs = dc->funcs;
846 if (dc->funcs->pDeleteDC) dc->funcs->pDeleteDC(dc->physDev);
847 dc->physDev = NULL;
850 while (dc->saved_visrgn)
852 struct saved_visrgn *next = dc->saved_visrgn->next;
853 DeleteObject( dc->saved_visrgn->hrgn );
854 HeapFree( GetProcessHeap(), 0, dc->saved_visrgn );
855 dc->saved_visrgn = next;
858 free_dc_ptr( dc );
859 if (funcs) DRIVER_release_driver( funcs ); /* do that after releasing the GDI lock */
860 return TRUE;
864 /***********************************************************************
865 * ResetDCW (GDI32.@)
867 HDC WINAPI ResetDCW( HDC hdc, const DEVMODEW *devmode )
869 DC *dc;
870 HDC ret = hdc;
872 if ((dc = get_dc_ptr( hdc )))
874 if (dc->funcs->pResetDC) ret = dc->funcs->pResetDC( dc->physDev, devmode );
875 release_dc_ptr( dc );
877 return ret;
881 /***********************************************************************
882 * ResetDCA (GDI32.@)
884 HDC WINAPI ResetDCA( HDC hdc, const DEVMODEA *devmode )
886 DEVMODEW *devmodeW;
887 HDC ret;
889 if (devmode) devmodeW = GdiConvertToDevmodeW(devmode);
890 else devmodeW = NULL;
892 ret = ResetDCW(hdc, devmodeW);
894 HeapFree(GetProcessHeap(), 0, devmodeW);
895 return ret;
899 /***********************************************************************
900 * GetDeviceCaps (GDI32.@)
902 INT WINAPI GetDeviceCaps( HDC hdc, INT cap )
904 DC *dc;
905 INT ret = 0;
907 if ((dc = get_dc_ptr( hdc )))
909 if (dc->funcs->pGetDeviceCaps) ret = dc->funcs->pGetDeviceCaps( dc->physDev, cap );
910 else switch(cap) /* return meaningful values for some entries */
912 case HORZRES: ret = 640; break;
913 case VERTRES: ret = 480; break;
914 case BITSPIXEL: ret = 1; break;
915 case PLANES: ret = 1; break;
916 case NUMCOLORS: ret = 2; break;
917 case ASPECTX: ret = 36; break;
918 case ASPECTY: ret = 36; break;
919 case ASPECTXY: ret = 51; break;
920 case LOGPIXELSX: ret = 72; break;
921 case LOGPIXELSY: ret = 72; break;
922 case SIZEPALETTE: ret = 2; break;
924 release_dc_ptr( dc );
926 return ret;
930 /***********************************************************************
931 * GetBkColor (GDI32.@)
933 COLORREF WINAPI GetBkColor( HDC hdc )
935 COLORREF ret = 0;
936 DC * dc = get_dc_ptr( hdc );
937 if (dc)
939 ret = dc->backgroundColor;
940 release_dc_ptr( dc );
942 return ret;
946 /***********************************************************************
947 * SetBkColor (GDI32.@)
949 COLORREF WINAPI SetBkColor( HDC hdc, COLORREF color )
951 COLORREF oldColor;
952 DC * dc = get_dc_ptr( hdc );
954 TRACE("hdc=%p color=0x%08x\n", hdc, color);
956 if (!dc) return CLR_INVALID;
957 oldColor = dc->backgroundColor;
958 if (dc->funcs->pSetBkColor)
960 color = dc->funcs->pSetBkColor(dc->physDev, color);
961 if (color == CLR_INVALID) /* don't change it */
963 color = oldColor;
964 oldColor = CLR_INVALID;
967 dc->backgroundColor = color;
968 release_dc_ptr( dc );
969 return oldColor;
973 /***********************************************************************
974 * GetTextColor (GDI32.@)
976 COLORREF WINAPI GetTextColor( HDC hdc )
978 COLORREF ret = 0;
979 DC * dc = get_dc_ptr( hdc );
980 if (dc)
982 ret = dc->textColor;
983 release_dc_ptr( dc );
985 return ret;
989 /***********************************************************************
990 * SetTextColor (GDI32.@)
992 COLORREF WINAPI SetTextColor( HDC hdc, COLORREF color )
994 COLORREF oldColor;
995 DC * dc = get_dc_ptr( hdc );
997 TRACE(" hdc=%p color=0x%08x\n", hdc, color);
999 if (!dc) return CLR_INVALID;
1000 oldColor = dc->textColor;
1001 if (dc->funcs->pSetTextColor)
1003 color = dc->funcs->pSetTextColor(dc->physDev, color);
1004 if (color == CLR_INVALID) /* don't change it */
1006 color = oldColor;
1007 oldColor = CLR_INVALID;
1010 dc->textColor = color;
1011 release_dc_ptr( dc );
1012 return oldColor;
1016 /***********************************************************************
1017 * GetTextAlign (GDI32.@)
1019 UINT WINAPI GetTextAlign( HDC hdc )
1021 UINT ret = 0;
1022 DC * dc = get_dc_ptr( hdc );
1023 if (dc)
1025 ret = dc->textAlign;
1026 release_dc_ptr( dc );
1028 return ret;
1032 /***********************************************************************
1033 * SetTextAlign (GDI32.@)
1035 UINT WINAPI SetTextAlign( HDC hdc, UINT align )
1037 UINT ret;
1038 DC *dc = get_dc_ptr( hdc );
1040 TRACE("hdc=%p align=%d\n", hdc, align);
1042 if (!dc) return 0x0;
1043 ret = dc->textAlign;
1044 if (dc->funcs->pSetTextAlign)
1045 if (!dc->funcs->pSetTextAlign(dc->physDev, align))
1046 ret = GDI_ERROR;
1047 if (ret != GDI_ERROR)
1048 dc->textAlign = align;
1049 release_dc_ptr( dc );
1050 return ret;
1053 /***********************************************************************
1054 * GetDCOrgEx (GDI32.@)
1056 BOOL WINAPI GetDCOrgEx( HDC hDC, LPPOINT lpp )
1058 DC * dc;
1060 if (!lpp) return FALSE;
1061 if (!(dc = get_dc_ptr( hDC ))) return FALSE;
1063 lpp->x = lpp->y = 0;
1064 if (dc->funcs->pGetDCOrgEx) dc->funcs->pGetDCOrgEx( dc->physDev, lpp );
1065 release_dc_ptr( dc );
1066 return TRUE;
1070 /***********************************************************************
1071 * GetGraphicsMode (GDI32.@)
1073 INT WINAPI GetGraphicsMode( HDC hdc )
1075 INT ret = 0;
1076 DC * dc = get_dc_ptr( hdc );
1077 if (dc)
1079 ret = dc->GraphicsMode;
1080 release_dc_ptr( dc );
1082 return ret;
1086 /***********************************************************************
1087 * SetGraphicsMode (GDI32.@)
1089 INT WINAPI SetGraphicsMode( HDC hdc, INT mode )
1091 INT ret = 0;
1092 DC *dc = get_dc_ptr( hdc );
1094 /* One would think that setting the graphics mode to GM_COMPATIBLE
1095 * would also reset the world transformation matrix to the unity
1096 * matrix. However, in Windows, this is not the case. This doesn't
1097 * make a lot of sense to me, but that's the way it is.
1099 if (!dc) return 0;
1100 if ((mode > 0) && (mode <= GM_LAST))
1102 ret = dc->GraphicsMode;
1103 dc->GraphicsMode = mode;
1105 release_dc_ptr( dc );
1106 return ret;
1110 /***********************************************************************
1111 * GetArcDirection (GDI32.@)
1113 INT WINAPI GetArcDirection( HDC hdc )
1115 INT ret = 0;
1116 DC * dc = get_dc_ptr( hdc );
1117 if (dc)
1119 ret = dc->ArcDirection;
1120 release_dc_ptr( dc );
1122 return ret;
1126 /***********************************************************************
1127 * SetArcDirection (GDI32.@)
1129 INT WINAPI SetArcDirection( HDC hdc, INT nDirection )
1131 DC * dc;
1132 INT nOldDirection = 0;
1134 if (nDirection!=AD_COUNTERCLOCKWISE && nDirection!=AD_CLOCKWISE)
1136 SetLastError(ERROR_INVALID_PARAMETER);
1137 return 0;
1140 if ((dc = get_dc_ptr( hdc )))
1142 if (dc->funcs->pSetArcDirection)
1144 dc->funcs->pSetArcDirection(dc->physDev, nDirection);
1146 nOldDirection = dc->ArcDirection;
1147 dc->ArcDirection = nDirection;
1148 release_dc_ptr( dc );
1150 return nOldDirection;
1154 /***********************************************************************
1155 * GetWorldTransform (GDI32.@)
1157 BOOL WINAPI GetWorldTransform( HDC hdc, LPXFORM xform )
1159 DC * dc;
1160 if (!xform) return FALSE;
1161 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
1162 *xform = dc->xformWorld2Wnd;
1163 release_dc_ptr( dc );
1164 return TRUE;
1168 /***********************************************************************
1169 * GetTransform (GDI32.@)
1171 * Undocumented
1173 * Returns one of the co-ordinate space transforms
1175 * PARAMS
1176 * hdc [I] Device context.
1177 * which [I] Which xform to return:
1178 * 0x203 World -> Page transform (that set by SetWorldTransform).
1179 * 0x304 Page -> Device transform (the mapping mode transform).
1180 * 0x204 World -> Device transform (the combination of the above two).
1181 * 0x402 Device -> World transform (the inversion of the above).
1182 * xform [O] The xform.
1185 BOOL WINAPI GetTransform( HDC hdc, DWORD which, XFORM *xform )
1187 BOOL ret = TRUE;
1188 DC *dc = get_dc_ptr( hdc );
1189 if (!dc) return FALSE;
1191 switch(which)
1193 case 0x203:
1194 *xform = dc->xformWorld2Wnd;
1195 break;
1197 case 0x304:
1198 construct_window_to_viewport(dc, xform);
1199 break;
1201 case 0x204:
1202 *xform = dc->xformWorld2Vport;
1203 break;
1205 case 0x402:
1206 *xform = dc->xformVport2World;
1207 break;
1209 default:
1210 FIXME("Unknown code %x\n", which);
1211 ret = FALSE;
1214 release_dc_ptr( dc );
1215 return ret;
1219 /***********************************************************************
1220 * SetWorldTransform (GDI32.@)
1222 BOOL WINAPI SetWorldTransform( HDC hdc, const XFORM *xform )
1224 BOOL ret = FALSE;
1225 DC *dc;
1227 if (!xform) return FALSE;
1229 dc = get_dc_ptr( hdc );
1230 if (!dc) return FALSE;
1232 /* Check that graphics mode is GM_ADVANCED */
1233 if (dc->GraphicsMode!=GM_ADVANCED) goto done;
1235 TRACE("eM11 %f eM12 %f eM21 %f eM22 %f eDx %f eDy %f\n",
1236 xform->eM11, xform->eM12, xform->eM21, xform->eM22, xform->eDx, xform->eDy);
1238 /* The transform must conform to (eM11 * eM22 != eM12 * eM21) requirement */
1239 if (xform->eM11 * xform->eM22 == xform->eM12 * xform->eM21) goto done;
1241 if (dc->funcs->pSetWorldTransform)
1243 ret = dc->funcs->pSetWorldTransform(dc->physDev, xform);
1244 if (!ret) goto done;
1247 dc->xformWorld2Wnd = *xform;
1248 DC_UpdateXforms( dc );
1249 ret = TRUE;
1250 done:
1251 release_dc_ptr( dc );
1252 return ret;
1256 /****************************************************************************
1257 * ModifyWorldTransform [GDI32.@]
1258 * Modifies the world transformation for a device context.
1260 * PARAMS
1261 * hdc [I] Handle to device context
1262 * xform [I] XFORM structure that will be used to modify the world
1263 * transformation
1264 * iMode [I] Specifies in what way to modify the world transformation
1265 * Possible values:
1266 * MWT_IDENTITY
1267 * Resets the world transformation to the identity matrix.
1268 * The parameter xform is ignored.
1269 * MWT_LEFTMULTIPLY
1270 * Multiplies xform into the world transformation matrix from
1271 * the left.
1272 * MWT_RIGHTMULTIPLY
1273 * Multiplies xform into the world transformation matrix from
1274 * the right.
1276 * RETURNS
1277 * Success: TRUE.
1278 * Failure: FALSE. Use GetLastError() to determine the cause.
1280 BOOL WINAPI ModifyWorldTransform( HDC hdc, const XFORM *xform,
1281 DWORD iMode )
1283 BOOL ret = FALSE;
1284 DC *dc = get_dc_ptr( hdc );
1286 /* Check for illegal parameters */
1287 if (!dc) return FALSE;
1288 if (!xform && iMode != MWT_IDENTITY) goto done;
1290 /* Check that graphics mode is GM_ADVANCED */
1291 if (dc->GraphicsMode!=GM_ADVANCED) goto done;
1293 if (dc->funcs->pModifyWorldTransform)
1295 ret = dc->funcs->pModifyWorldTransform(dc->physDev, xform, iMode);
1296 if (!ret) goto done;
1299 switch (iMode)
1301 case MWT_IDENTITY:
1302 dc->xformWorld2Wnd.eM11 = 1.0f;
1303 dc->xformWorld2Wnd.eM12 = 0.0f;
1304 dc->xformWorld2Wnd.eM21 = 0.0f;
1305 dc->xformWorld2Wnd.eM22 = 1.0f;
1306 dc->xformWorld2Wnd.eDx = 0.0f;
1307 dc->xformWorld2Wnd.eDy = 0.0f;
1308 break;
1309 case MWT_LEFTMULTIPLY:
1310 CombineTransform( &dc->xformWorld2Wnd, xform,
1311 &dc->xformWorld2Wnd );
1312 break;
1313 case MWT_RIGHTMULTIPLY:
1314 CombineTransform( &dc->xformWorld2Wnd, &dc->xformWorld2Wnd,
1315 xform );
1316 break;
1317 default:
1318 goto done;
1321 DC_UpdateXforms( dc );
1322 ret = TRUE;
1323 done:
1324 release_dc_ptr( dc );
1325 return ret;
1329 /****************************************************************************
1330 * CombineTransform [GDI32.@]
1331 * Combines two transformation matrices.
1333 * PARAMS
1334 * xformResult [O] Stores the result of combining the two matrices
1335 * xform1 [I] Specifies the first matrix to apply
1336 * xform2 [I] Specifies the second matrix to apply
1338 * REMARKS
1339 * The same matrix can be passed in for more than one of the parameters.
1341 * RETURNS
1342 * Success: TRUE.
1343 * Failure: FALSE. Use GetLastError() to determine the cause.
1345 BOOL WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
1346 const XFORM *xform2 )
1348 XFORM xformTemp;
1350 /* Check for illegal parameters */
1351 if (!xformResult || !xform1 || !xform2)
1352 return FALSE;
1354 /* Create the result in a temporary XFORM, since xformResult may be
1355 * equal to xform1 or xform2 */
1356 xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
1357 xform1->eM12 * xform2->eM21;
1358 xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
1359 xform1->eM12 * xform2->eM22;
1360 xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
1361 xform1->eM22 * xform2->eM21;
1362 xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
1363 xform1->eM22 * xform2->eM22;
1364 xformTemp.eDx = xform1->eDx * xform2->eM11 +
1365 xform1->eDy * xform2->eM21 +
1366 xform2->eDx;
1367 xformTemp.eDy = xform1->eDx * xform2->eM12 +
1368 xform1->eDy * xform2->eM22 +
1369 xform2->eDy;
1371 /* Copy the result to xformResult */
1372 *xformResult = xformTemp;
1374 return TRUE;
1378 /***********************************************************************
1379 * SetDCHook (GDI32.@)
1381 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1383 BOOL WINAPI SetDCHook( HDC hdc, DCHOOKPROC hookProc, DWORD_PTR dwHookData )
1385 DC *dc = get_dc_ptr( hdc );
1387 if (!dc) return FALSE;
1389 if (!(dc->flags & DC_SAVED))
1391 dc->dwHookData = dwHookData;
1392 dc->hookThunk = hookProc;
1394 release_dc_ptr( dc );
1395 return TRUE;
1399 /***********************************************************************
1400 * GetDCHook (GDI32.@)
1402 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1404 DWORD_PTR WINAPI GetDCHook( HDC hdc, DCHOOKPROC *proc )
1406 DC *dc = get_dc_ptr( hdc );
1407 DWORD_PTR ret;
1409 if (!dc) return 0;
1410 if (proc) *proc = dc->hookThunk;
1411 ret = dc->dwHookData;
1412 release_dc_ptr( dc );
1413 return ret;
1417 /* relay function to call the 16-bit DC hook proc */
1418 static BOOL WINAPI call_dc_hook16( HDC hdc, WORD code, DWORD_PTR data, LPARAM lParam )
1420 WORD args[6];
1421 DWORD ret = 0;
1422 DC *dc = get_dc_ptr( hdc );
1424 if (!dc) return FALSE;
1425 if (dc->hookProc)
1427 args[5] = HDC_16(hdc);
1428 args[4] = code;
1429 args[3] = HIWORD(data);
1430 args[2] = LOWORD(data);
1431 args[1] = HIWORD(lParam);
1432 args[0] = LOWORD(lParam);
1433 WOWCallback16Ex( (DWORD)dc->hookProc, WCB16_PASCAL, sizeof(args), args, &ret );
1435 release_dc_ptr( dc );
1436 return LOWORD(ret);
1439 /***********************************************************************
1440 * SetDCHook (GDI.190)
1442 BOOL16 WINAPI SetDCHook16( HDC16 hdc16, FARPROC16 hookProc, DWORD dwHookData )
1444 DC *dc = get_dc_ptr( HDC_32(hdc16) );
1446 if (!dc) return FALSE;
1447 if (!(dc->flags & DC_SAVED))
1449 dc->dwHookData = dwHookData;
1450 dc->hookThunk = call_dc_hook16;
1451 dc->hookProc = hookProc;
1453 release_dc_ptr( dc );
1454 return TRUE;
1458 /***********************************************************************
1459 * GetDCHook (GDI.191)
1461 DWORD WINAPI GetDCHook16( HDC16 hdc16, FARPROC16 *phookProc )
1463 HDC hdc = HDC_32( hdc16 );
1464 DC *dc = get_dc_ptr( hdc );
1465 DWORD ret;
1467 if (!dc) return 0;
1468 *phookProc = dc->hookProc;
1469 ret = dc->dwHookData;
1470 release_dc_ptr( dc );
1471 return ret;
1475 /***********************************************************************
1476 * SetHookFlags (GDI32.@)
1478 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1480 WORD WINAPI SetHookFlags( HDC hdc, WORD flags )
1482 DC *dc = get_dc_obj( hdc ); /* not get_dc_ptr, this needs to work from any thread */
1483 LONG ret = 0;
1485 if (!dc) return 0;
1487 /* "Undocumented Windows" info is slightly confusing. */
1489 TRACE("hDC %p, flags %04x\n",hdc,flags);
1491 if (flags & DCHF_INVALIDATEVISRGN)
1492 ret = InterlockedExchange( &dc->dirty, 1 );
1493 else if (flags & DCHF_VALIDATEVISRGN || !flags)
1494 ret = InterlockedExchange( &dc->dirty, 0 );
1496 GDI_ReleaseObj( dc );
1497 return ret;
1500 /***********************************************************************
1501 * SetICMMode (GDI32.@)
1503 INT WINAPI SetICMMode(HDC hdc, INT iEnableICM)
1505 /*FIXME: Assume that ICM is always off, and cannot be turned on */
1506 if (iEnableICM == ICM_OFF) return ICM_OFF;
1507 if (iEnableICM == ICM_ON) return 0;
1508 if (iEnableICM == ICM_QUERY) return ICM_OFF;
1509 return 0;
1512 /***********************************************************************
1513 * GetDeviceGammaRamp (GDI32.@)
1515 BOOL WINAPI GetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1517 BOOL ret = FALSE;
1518 DC *dc = get_dc_ptr( hDC );
1520 if( dc )
1522 if (dc->funcs->pGetDeviceGammaRamp)
1523 ret = dc->funcs->pGetDeviceGammaRamp(dc->physDev, ptr);
1524 release_dc_ptr( dc );
1526 return ret;
1529 /***********************************************************************
1530 * SetDeviceGammaRamp (GDI32.@)
1532 BOOL WINAPI SetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1534 BOOL ret = FALSE;
1535 DC *dc = get_dc_ptr( hDC );
1537 if( dc )
1539 if (dc->funcs->pSetDeviceGammaRamp)
1540 ret = dc->funcs->pSetDeviceGammaRamp(dc->physDev, ptr);
1541 release_dc_ptr( dc );
1543 return ret;
1546 /***********************************************************************
1547 * GetColorSpace (GDI32.@)
1549 HCOLORSPACE WINAPI GetColorSpace(HDC hdc)
1551 /*FIXME Need to to whatever GetColorSpace actually does */
1552 return 0;
1555 /***********************************************************************
1556 * CreateColorSpaceA (GDI32.@)
1558 HCOLORSPACE WINAPI CreateColorSpaceA( LPLOGCOLORSPACEA lpLogColorSpace )
1560 FIXME( "stub\n" );
1561 return 0;
1564 /***********************************************************************
1565 * CreateColorSpaceW (GDI32.@)
1567 HCOLORSPACE WINAPI CreateColorSpaceW( LPLOGCOLORSPACEW lpLogColorSpace )
1569 FIXME( "stub\n" );
1570 return 0;
1573 /***********************************************************************
1574 * DeleteColorSpace (GDI32.@)
1576 BOOL WINAPI DeleteColorSpace( HCOLORSPACE hColorSpace )
1578 FIXME( "stub\n" );
1580 return TRUE;
1583 /***********************************************************************
1584 * SetColorSpace (GDI32.@)
1586 HCOLORSPACE WINAPI SetColorSpace( HDC hDC, HCOLORSPACE hColorSpace )
1588 FIXME( "stub\n" );
1590 return hColorSpace;
1593 /***********************************************************************
1594 * GetBoundsRect (GDI32.@)
1596 UINT WINAPI GetBoundsRect(HDC hdc, LPRECT rect, UINT flags)
1598 UINT ret;
1599 DC *dc = get_dc_ptr( hdc );
1601 if ( !dc ) return 0;
1603 if (rect) *rect = dc->BoundsRect;
1605 ret = ((dc->flags & DC_BOUNDS_SET) ? DCB_SET : DCB_RESET);
1607 if (flags & DCB_RESET)
1609 dc->BoundsRect.left = 0;
1610 dc->BoundsRect.top = 0;
1611 dc->BoundsRect.right = 0;
1612 dc->BoundsRect.bottom = 0;
1613 dc->flags &= ~DC_BOUNDS_SET;
1615 release_dc_ptr( dc );
1616 return ret;
1620 /***********************************************************************
1621 * SetBoundsRect (GDI32.@)
1623 UINT WINAPI SetBoundsRect(HDC hdc, const RECT* rect, UINT flags)
1625 UINT ret;
1626 DC *dc;
1628 if ((flags & DCB_ENABLE) && (flags & DCB_DISABLE)) return 0;
1629 if (!(dc = get_dc_ptr( hdc ))) return 0;
1631 ret = ((dc->flags & DC_BOUNDS_ENABLE) ? DCB_ENABLE : DCB_DISABLE) |
1632 ((dc->flags & DC_BOUNDS_SET) ? DCB_SET : DCB_RESET);
1634 if (flags & DCB_RESET)
1636 dc->BoundsRect.left = 0;
1637 dc->BoundsRect.top = 0;
1638 dc->BoundsRect.right = 0;
1639 dc->BoundsRect.bottom = 0;
1640 dc->flags &= ~DC_BOUNDS_SET;
1643 if ((flags & DCB_ACCUMULATE) && rect && rect->left < rect->right && rect->top < rect->bottom)
1645 if (dc->flags & DC_BOUNDS_SET)
1647 dc->BoundsRect.left = min( dc->BoundsRect.left, rect->left );
1648 dc->BoundsRect.top = min( dc->BoundsRect.top, rect->top );
1649 dc->BoundsRect.right = max( dc->BoundsRect.right, rect->right );
1650 dc->BoundsRect.bottom = max( dc->BoundsRect.bottom, rect->bottom );
1652 else
1654 dc->BoundsRect = *rect;
1655 dc->flags |= DC_BOUNDS_SET;
1659 if (flags & DCB_ENABLE) dc->flags |= DC_BOUNDS_ENABLE;
1660 if (flags & DCB_DISABLE) dc->flags &= ~DC_BOUNDS_ENABLE;
1662 release_dc_ptr( dc );
1663 return ret;
1667 /***********************************************************************
1668 * GetRelAbs (GDI32.@)
1670 INT WINAPI GetRelAbs( HDC hdc, DWORD dwIgnore )
1672 INT ret = 0;
1673 DC *dc = get_dc_ptr( hdc );
1674 if (dc)
1676 ret = dc->relAbsMode;
1677 release_dc_ptr( dc );
1679 return ret;
1685 /***********************************************************************
1686 * GetBkMode (GDI32.@)
1688 INT WINAPI GetBkMode( HDC hdc )
1690 INT ret = 0;
1691 DC * dc = get_dc_ptr( hdc );
1692 if (dc)
1694 ret = dc->backgroundMode;
1695 release_dc_ptr( dc );
1697 return ret;
1701 /***********************************************************************
1702 * SetBkMode (GDI32.@)
1704 INT WINAPI SetBkMode( HDC hdc, INT mode )
1706 INT ret;
1707 DC *dc;
1708 if ((mode <= 0) || (mode > BKMODE_LAST))
1710 SetLastError(ERROR_INVALID_PARAMETER);
1711 return 0;
1713 if (!(dc = get_dc_ptr( hdc ))) return 0;
1715 ret = dc->backgroundMode;
1716 if (dc->funcs->pSetBkMode)
1717 if (!dc->funcs->pSetBkMode( dc->physDev, mode ))
1718 ret = 0;
1719 if (ret)
1720 dc->backgroundMode = mode;
1721 release_dc_ptr( dc );
1722 return ret;
1726 /***********************************************************************
1727 * GetROP2 (GDI32.@)
1729 INT WINAPI GetROP2( HDC hdc )
1731 INT ret = 0;
1732 DC * dc = get_dc_ptr( hdc );
1733 if (dc)
1735 ret = dc->ROPmode;
1736 release_dc_ptr( dc );
1738 return ret;
1742 /***********************************************************************
1743 * SetROP2 (GDI32.@)
1745 INT WINAPI SetROP2( HDC hdc, INT mode )
1747 INT ret;
1748 DC *dc;
1749 if ((mode < R2_BLACK) || (mode > R2_WHITE))
1751 SetLastError(ERROR_INVALID_PARAMETER);
1752 return 0;
1754 if (!(dc = get_dc_ptr( hdc ))) return 0;
1755 ret = dc->ROPmode;
1756 if (dc->funcs->pSetROP2)
1757 if (!dc->funcs->pSetROP2( dc->physDev, mode ))
1758 ret = 0;
1759 if (ret)
1760 dc->ROPmode = mode;
1761 release_dc_ptr( dc );
1762 return ret;
1766 /***********************************************************************
1767 * SetRelAbs (GDI32.@)
1769 INT WINAPI SetRelAbs( HDC hdc, INT mode )
1771 INT ret;
1772 DC *dc;
1773 if ((mode != ABSOLUTE) && (mode != RELATIVE))
1775 SetLastError(ERROR_INVALID_PARAMETER);
1776 return 0;
1778 if (!(dc = get_dc_ptr( hdc ))) return 0;
1779 if (dc->funcs->pSetRelAbs)
1780 ret = dc->funcs->pSetRelAbs( dc->physDev, mode );
1781 else
1783 ret = dc->relAbsMode;
1784 dc->relAbsMode = mode;
1786 release_dc_ptr( dc );
1787 return ret;
1791 /***********************************************************************
1792 * GetPolyFillMode (GDI32.@)
1794 INT WINAPI GetPolyFillMode( HDC hdc )
1796 INT ret = 0;
1797 DC * dc = get_dc_ptr( hdc );
1798 if (dc)
1800 ret = dc->polyFillMode;
1801 release_dc_ptr( dc );
1803 return ret;
1807 /***********************************************************************
1808 * SetPolyFillMode (GDI32.@)
1810 INT WINAPI SetPolyFillMode( HDC hdc, INT mode )
1812 INT ret;
1813 DC *dc;
1814 if ((mode <= 0) || (mode > POLYFILL_LAST))
1816 SetLastError(ERROR_INVALID_PARAMETER);
1817 return 0;
1819 if (!(dc = get_dc_ptr( hdc ))) return 0;
1820 ret = dc->polyFillMode;
1821 if (dc->funcs->pSetPolyFillMode)
1822 if (!dc->funcs->pSetPolyFillMode( dc->physDev, mode ))
1823 ret = 0;
1824 if (ret)
1825 dc->polyFillMode = mode;
1826 release_dc_ptr( dc );
1827 return ret;
1831 /***********************************************************************
1832 * GetStretchBltMode (GDI32.@)
1834 INT WINAPI GetStretchBltMode( HDC hdc )
1836 INT ret = 0;
1837 DC * dc = get_dc_ptr( hdc );
1838 if (dc)
1840 ret = dc->stretchBltMode;
1841 release_dc_ptr( dc );
1843 return ret;
1847 /***********************************************************************
1848 * SetStretchBltMode (GDI32.@)
1850 INT WINAPI SetStretchBltMode( HDC hdc, INT mode )
1852 INT ret;
1853 DC *dc;
1854 if ((mode <= 0) || (mode > MAXSTRETCHBLTMODE))
1856 SetLastError(ERROR_INVALID_PARAMETER);
1857 return 0;
1859 if (!(dc = get_dc_ptr( hdc ))) return 0;
1860 ret = dc->stretchBltMode;
1861 if (dc->funcs->pSetStretchBltMode)
1862 if (!dc->funcs->pSetStretchBltMode( dc->physDev, mode ))
1863 ret = 0;
1864 if (ret)
1865 dc->stretchBltMode = mode;
1866 release_dc_ptr( dc );
1867 return ret;
1871 /***********************************************************************
1872 * GetMapMode (GDI32.@)
1874 INT WINAPI GetMapMode( HDC hdc )
1876 INT ret = 0;
1877 DC * dc = get_dc_ptr( hdc );
1878 if (dc)
1880 ret = dc->MapMode;
1881 release_dc_ptr( dc );
1883 return ret;
1887 /***********************************************************************
1888 * GetBrushOrgEx (GDI32.@)
1890 BOOL WINAPI GetBrushOrgEx( HDC hdc, LPPOINT pt )
1892 DC * dc = get_dc_ptr( hdc );
1893 if (!dc) return FALSE;
1894 pt->x = dc->brushOrgX;
1895 pt->y = dc->brushOrgY;
1896 release_dc_ptr( dc );
1897 return TRUE;
1901 /***********************************************************************
1902 * GetCurrentPositionEx (GDI32.@)
1904 BOOL WINAPI GetCurrentPositionEx( HDC hdc, LPPOINT pt )
1906 DC * dc = get_dc_ptr( hdc );
1907 if (!dc) return FALSE;
1908 pt->x = dc->CursPosX;
1909 pt->y = dc->CursPosY;
1910 release_dc_ptr( dc );
1911 return TRUE;
1915 /***********************************************************************
1916 * GetViewportExtEx (GDI32.@)
1918 BOOL WINAPI GetViewportExtEx( HDC hdc, LPSIZE size )
1920 DC * dc = get_dc_ptr( hdc );
1921 if (!dc) return FALSE;
1922 size->cx = dc->vportExtX;
1923 size->cy = dc->vportExtY;
1924 release_dc_ptr( dc );
1925 return TRUE;
1929 /***********************************************************************
1930 * GetViewportOrgEx (GDI32.@)
1932 BOOL WINAPI GetViewportOrgEx( HDC hdc, LPPOINT pt )
1934 DC * dc = get_dc_ptr( hdc );
1935 if (!dc) return FALSE;
1936 pt->x = dc->vportOrgX;
1937 pt->y = dc->vportOrgY;
1938 release_dc_ptr( dc );
1939 return TRUE;
1943 /***********************************************************************
1944 * GetWindowExtEx (GDI32.@)
1946 BOOL WINAPI GetWindowExtEx( HDC hdc, LPSIZE size )
1948 DC * dc = get_dc_ptr( hdc );
1949 if (!dc) return FALSE;
1950 size->cx = dc->wndExtX;
1951 size->cy = dc->wndExtY;
1952 release_dc_ptr( dc );
1953 return TRUE;
1957 /***********************************************************************
1958 * GetWindowOrgEx (GDI32.@)
1960 BOOL WINAPI GetWindowOrgEx( HDC hdc, LPPOINT pt )
1962 DC * dc = get_dc_ptr( hdc );
1963 if (!dc) return FALSE;
1964 pt->x = dc->wndOrgX;
1965 pt->y = dc->wndOrgY;
1966 release_dc_ptr( dc );
1967 return TRUE;
1971 /***********************************************************************
1972 * GetLayout (GDI32.@)
1974 * Gets left->right or right->left text layout flags of a dc.
1977 DWORD WINAPI GetLayout(HDC hdc)
1979 DWORD layout = GDI_ERROR;
1981 DC * dc = get_dc_ptr( hdc );
1982 if (dc)
1984 layout = dc->layout;
1985 release_dc_ptr( dc );
1988 TRACE("hdc : %p, layout : %08x\n", hdc, layout);
1990 return layout;
1993 /***********************************************************************
1994 * SetLayout (GDI32.@)
1996 * Sets left->right or right->left text layout flags of a dc.
1999 DWORD WINAPI SetLayout(HDC hdc, DWORD layout)
2001 DWORD oldlayout = GDI_ERROR;
2003 DC * dc = get_dc_ptr( hdc );
2004 if (dc)
2006 oldlayout = dc->layout;
2007 dc->layout = layout;
2008 release_dc_ptr( dc );
2011 TRACE("hdc : %p, old layout : %08x, new layout : %08x\n", hdc, oldlayout, layout);
2013 return oldlayout;
2016 /***********************************************************************
2017 * GetDCBrushColor (GDI32.@)
2019 * Retrieves the current brush color for the specified device
2020 * context (DC).
2023 COLORREF WINAPI GetDCBrushColor(HDC hdc)
2025 DC *dc;
2026 COLORREF dcBrushColor = CLR_INVALID;
2028 TRACE("hdc(%p)\n", hdc);
2030 dc = get_dc_ptr( hdc );
2031 if (dc)
2033 dcBrushColor = dc->dcBrushColor;
2034 release_dc_ptr( dc );
2037 return dcBrushColor;
2040 /***********************************************************************
2041 * SetDCBrushColor (GDI32.@)
2043 * Sets the current device context (DC) brush color to the specified
2044 * color value. If the device cannot represent the specified color
2045 * value, the color is set to the nearest physical color.
2048 COLORREF WINAPI SetDCBrushColor(HDC hdc, COLORREF crColor)
2050 DC *dc;
2051 COLORREF oldClr = CLR_INVALID;
2053 TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
2055 dc = get_dc_ptr( hdc );
2056 if (dc)
2058 if (dc->funcs->pSetDCBrushColor)
2059 crColor = dc->funcs->pSetDCBrushColor( dc->physDev, crColor );
2060 else if (dc->hBrush == GetStockObject( DC_BRUSH ))
2062 /* If DC_BRUSH is selected, update driver pen color */
2063 HBRUSH hBrush = CreateSolidBrush( crColor );
2064 dc->funcs->pSelectBrush( dc->physDev, hBrush );
2065 DeleteObject( hBrush );
2068 if (crColor != CLR_INVALID)
2070 oldClr = dc->dcBrushColor;
2071 dc->dcBrushColor = crColor;
2074 release_dc_ptr( dc );
2077 return oldClr;
2080 /***********************************************************************
2081 * GetDCPenColor (GDI32.@)
2083 * Retrieves the current pen color for the specified device
2084 * context (DC).
2087 COLORREF WINAPI GetDCPenColor(HDC hdc)
2089 DC *dc;
2090 COLORREF dcPenColor = CLR_INVALID;
2092 TRACE("hdc(%p)\n", hdc);
2094 dc = get_dc_ptr( hdc );
2095 if (dc)
2097 dcPenColor = dc->dcPenColor;
2098 release_dc_ptr( dc );
2101 return dcPenColor;
2104 /***********************************************************************
2105 * SetDCPenColor (GDI32.@)
2107 * Sets the current device context (DC) pen color to the specified
2108 * color value. If the device cannot represent the specified color
2109 * value, the color is set to the nearest physical color.
2112 COLORREF WINAPI SetDCPenColor(HDC hdc, COLORREF crColor)
2114 DC *dc;
2115 COLORREF oldClr = CLR_INVALID;
2117 TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
2119 dc = get_dc_ptr( hdc );
2120 if (dc)
2122 if (dc->funcs->pSetDCPenColor)
2123 crColor = dc->funcs->pSetDCPenColor( dc->physDev, crColor );
2124 else if (dc->hPen == GetStockObject( DC_PEN ))
2126 /* If DC_PEN is selected, update the driver pen color */
2127 LOGPEN logpen = { PS_SOLID, { 0, 0 }, crColor };
2128 HPEN hPen = CreatePenIndirect( &logpen );
2129 dc->funcs->pSelectPen( dc->physDev, hPen );
2130 DeleteObject( hPen );
2133 if (crColor != CLR_INVALID)
2135 oldClr = dc->dcPenColor;
2136 dc->dcPenColor = crColor;
2139 release_dc_ptr( dc );
2142 return oldClr;
2145 /***********************************************************************
2146 * CancelDC (GDI32.@)
2148 BOOL WINAPI CancelDC(HDC hdc)
2150 FIXME("stub\n");
2151 return TRUE;
2154 /*******************************************************************
2155 * GetMiterLimit [GDI32.@]
2159 BOOL WINAPI GetMiterLimit(HDC hdc, PFLOAT peLimit)
2161 BOOL bRet = FALSE;
2162 DC *dc;
2164 TRACE("(%p,%p)\n", hdc, peLimit);
2166 dc = get_dc_ptr( hdc );
2167 if (dc)
2169 if (peLimit)
2170 *peLimit = dc->miterLimit;
2172 release_dc_ptr( dc );
2173 bRet = TRUE;
2175 return bRet;
2178 /*******************************************************************
2179 * SetMiterLimit [GDI32.@]
2183 BOOL WINAPI SetMiterLimit(HDC hdc, FLOAT eNewLimit, PFLOAT peOldLimit)
2185 BOOL bRet = FALSE;
2186 DC *dc;
2188 TRACE("(%p,%f,%p)\n", hdc, eNewLimit, peOldLimit);
2190 dc = get_dc_ptr( hdc );
2191 if (dc)
2193 if (peOldLimit)
2194 *peOldLimit = dc->miterLimit;
2195 dc->miterLimit = eNewLimit;
2196 release_dc_ptr( dc );
2197 bRet = TRUE;
2199 return bRet;
2202 /*******************************************************************
2203 * GdiIsMetaPrintDC [GDI32.@]
2205 BOOL WINAPI GdiIsMetaPrintDC(HDC hdc)
2207 FIXME("%p\n", hdc);
2208 return FALSE;
2211 /*******************************************************************
2212 * GdiIsMetaFileDC [GDI32.@]
2214 BOOL WINAPI GdiIsMetaFileDC(HDC hdc)
2216 TRACE("%p\n", hdc);
2218 switch( GetObjectType( hdc ) )
2220 case OBJ_METADC:
2221 case OBJ_ENHMETADC:
2222 return TRUE;
2224 return FALSE;
2227 /*******************************************************************
2228 * GdiIsPlayMetafileDC [GDI32.@]
2230 BOOL WINAPI GdiIsPlayMetafileDC(HDC hdc)
2232 FIXME("%p\n", hdc);
2233 return FALSE;