push ac694015ba9a1d7cf8fc6346c6e51d4c35a62962
[wine/hacks.git] / dlls / gdi32 / dc.c
blob04e4942fbc9db1f4c25efc4bc714090e397c4df7
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 );
258 /***********************************************************************
259 * DC_InvertXform
261 * Computes the inverse of the transformation xformSrc and stores it to
262 * xformDest. Returns TRUE if successful or FALSE if the xformSrc matrix
263 * is singular.
265 static BOOL DC_InvertXform( const XFORM *xformSrc, XFORM *xformDest )
267 double determinant;
269 determinant = xformSrc->eM11*xformSrc->eM22 -
270 xformSrc->eM12*xformSrc->eM21;
271 if (determinant > -1e-12 && determinant < 1e-12)
272 return FALSE;
274 xformDest->eM11 = xformSrc->eM22 / determinant;
275 xformDest->eM12 = -xformSrc->eM12 / determinant;
276 xformDest->eM21 = -xformSrc->eM21 / determinant;
277 xformDest->eM22 = xformSrc->eM11 / determinant;
278 xformDest->eDx = -xformSrc->eDx * xformDest->eM11 -
279 xformSrc->eDy * xformDest->eM21;
280 xformDest->eDy = -xformSrc->eDx * xformDest->eM12 -
281 xformSrc->eDy * xformDest->eM22;
283 return TRUE;
287 /***********************************************************************
288 * DC_UpdateXforms
290 * Updates the xformWorld2Vport, xformVport2World and vport2WorldValid
291 * fields of the specified DC by creating a transformation that
292 * represents the current mapping mode and combining it with the DC's
293 * world transform. This function should be called whenever the
294 * parameters associated with the mapping mode (window and viewport
295 * extents and origins) or the world transform change.
297 void DC_UpdateXforms( DC *dc )
299 XFORM xformWnd2Vport, oldworld2vport;
300 double scaleX, scaleY;
302 /* Construct a transformation to do the window-to-viewport conversion */
303 scaleX = (double)dc->vportExtX / (double)dc->wndExtX;
304 scaleY = (double)dc->vportExtY / (double)dc->wndExtY;
305 xformWnd2Vport.eM11 = scaleX;
306 xformWnd2Vport.eM12 = 0.0;
307 xformWnd2Vport.eM21 = 0.0;
308 xformWnd2Vport.eM22 = scaleY;
309 xformWnd2Vport.eDx = (double)dc->vportOrgX -
310 scaleX * (double)dc->wndOrgX;
311 xformWnd2Vport.eDy = (double)dc->vportOrgY -
312 scaleY * (double)dc->wndOrgY;
314 oldworld2vport = dc->xformWorld2Vport;
315 /* Combine with the world transformation */
316 CombineTransform( &dc->xformWorld2Vport, &dc->xformWorld2Wnd,
317 &xformWnd2Vport );
319 /* Create inverse of world-to-viewport transformation */
320 dc->vport2WorldValid = DC_InvertXform( &dc->xformWorld2Vport,
321 &dc->xformVport2World );
323 /* Reselect the font and pen back into the dc so that the size
324 gets updated. */
325 if (memcmp(&oldworld2vport, &dc->xformWorld2Vport, sizeof(oldworld2vport)) &&
326 !GdiIsMetaFileDC(dc->hSelf))
328 SelectObject(dc->hSelf, GetCurrentObject(dc->hSelf, OBJ_FONT));
329 SelectObject(dc->hSelf, GetCurrentObject(dc->hSelf, OBJ_PEN));
334 /***********************************************************************
335 * save_dc_state
337 INT save_dc_state( HDC hdc )
339 DC * newdc, * dc;
340 INT ret;
342 if (!(dc = get_dc_ptr( hdc ))) return 0;
343 if (!(newdc = HeapAlloc( GetProcessHeap(), 0, sizeof(*newdc ))))
345 release_dc_ptr( dc );
346 return 0;
349 newdc->flags = dc->flags | DC_SAVED;
350 newdc->layout = dc->layout;
351 newdc->hPen = dc->hPen;
352 newdc->hBrush = dc->hBrush;
353 newdc->hFont = dc->hFont;
354 newdc->hBitmap = dc->hBitmap;
355 newdc->hDevice = dc->hDevice;
356 newdc->hPalette = dc->hPalette;
357 newdc->ROPmode = dc->ROPmode;
358 newdc->polyFillMode = dc->polyFillMode;
359 newdc->stretchBltMode = dc->stretchBltMode;
360 newdc->relAbsMode = dc->relAbsMode;
361 newdc->backgroundMode = dc->backgroundMode;
362 newdc->backgroundColor = dc->backgroundColor;
363 newdc->textColor = dc->textColor;
364 newdc->dcBrushColor = dc->dcBrushColor;
365 newdc->dcPenColor = dc->dcPenColor;
366 newdc->brushOrgX = dc->brushOrgX;
367 newdc->brushOrgY = dc->brushOrgY;
368 newdc->textAlign = dc->textAlign;
369 newdc->charExtra = dc->charExtra;
370 newdc->breakExtra = dc->breakExtra;
371 newdc->breakRem = dc->breakRem;
372 newdc->MapMode = dc->MapMode;
373 newdc->GraphicsMode = dc->GraphicsMode;
374 newdc->CursPosX = dc->CursPosX;
375 newdc->CursPosY = dc->CursPosY;
376 newdc->ArcDirection = dc->ArcDirection;
377 newdc->xformWorld2Wnd = dc->xformWorld2Wnd;
378 newdc->xformWorld2Vport = dc->xformWorld2Vport;
379 newdc->xformVport2World = dc->xformVport2World;
380 newdc->vport2WorldValid = dc->vport2WorldValid;
381 newdc->wndOrgX = dc->wndOrgX;
382 newdc->wndOrgY = dc->wndOrgY;
383 newdc->wndExtX = dc->wndExtX;
384 newdc->wndExtY = dc->wndExtY;
385 newdc->vportOrgX = dc->vportOrgX;
386 newdc->vportOrgY = dc->vportOrgY;
387 newdc->vportExtX = dc->vportExtX;
388 newdc->vportExtY = dc->vportExtY;
389 newdc->BoundsRect = dc->BoundsRect;
390 newdc->gdiFont = dc->gdiFont;
392 newdc->thread = GetCurrentThreadId();
393 newdc->refcount = 1;
394 newdc->saveLevel = 0;
395 newdc->saved_dc = 0;
397 PATH_InitGdiPath( &newdc->path );
399 newdc->pAbortProc = NULL;
400 newdc->hookThunk = NULL;
401 newdc->hookProc = 0;
402 newdc->saved_visrgn = NULL;
404 if (!(newdc->hSelf = alloc_gdi_handle( &newdc->header, dc->header.type, &dc_funcs )))
406 HeapFree( GetProcessHeap(), 0, newdc );
407 release_dc_ptr( dc );
408 return 0;
411 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
413 newdc->hVisRgn = 0;
414 newdc->hClipRgn = 0;
415 newdc->hMetaRgn = 0;
416 newdc->hMetaClipRgn = 0;
417 if (dc->hClipRgn)
419 newdc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
420 CombineRgn( newdc->hClipRgn, dc->hClipRgn, 0, RGN_COPY );
422 if (dc->hMetaRgn)
424 newdc->hMetaRgn = CreateRectRgn( 0, 0, 0, 0 );
425 CombineRgn( newdc->hMetaRgn, dc->hMetaRgn, 0, RGN_COPY );
427 /* don't bother recomputing hMetaClipRgn, we'll do that in SetDCState */
429 if (!PATH_AssignGdiPath( &newdc->path, &dc->path ))
431 release_dc_ptr( dc );
432 free_dc_ptr( newdc );
433 return 0;
436 newdc->saved_dc = dc->saved_dc;
437 dc->saved_dc = newdc->hSelf;
438 ret = ++dc->saveLevel;
439 release_dc_ptr( newdc );
440 release_dc_ptr( dc );
441 return ret;
445 /***********************************************************************
446 * restore_dc_state
448 BOOL restore_dc_state( HDC hdc, INT level )
450 HDC hdcs, first_dcs;
451 DC *dc, *dcs;
452 INT save_level;
454 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
456 /* find the state level to restore */
458 if (abs(level) > dc->saveLevel || level == 0)
460 release_dc_ptr( dc );
461 return FALSE;
463 if (level < 0) level = dc->saveLevel + level + 1;
464 first_dcs = dc->saved_dc;
465 for (hdcs = first_dcs, save_level = dc->saveLevel; save_level > level; save_level--)
467 if (!(dcs = get_dc_ptr( hdcs )))
469 release_dc_ptr( dc );
470 return FALSE;
472 hdcs = dcs->saved_dc;
473 release_dc_ptr( dcs );
476 /* restore the state */
478 if (!(dcs = get_dc_ptr( hdcs )))
480 release_dc_ptr( dc );
481 return FALSE;
483 if (!PATH_AssignGdiPath( &dc->path, &dcs->path ))
485 release_dc_ptr( dcs );
486 release_dc_ptr( dc );
487 return FALSE;
490 dc->flags = dcs->flags & ~DC_SAVED;
491 dc->layout = dcs->layout;
492 dc->hDevice = dcs->hDevice;
493 dc->ROPmode = dcs->ROPmode;
494 dc->polyFillMode = dcs->polyFillMode;
495 dc->stretchBltMode = dcs->stretchBltMode;
496 dc->relAbsMode = dcs->relAbsMode;
497 dc->backgroundMode = dcs->backgroundMode;
498 dc->backgroundColor = dcs->backgroundColor;
499 dc->textColor = dcs->textColor;
500 dc->dcBrushColor = dcs->dcBrushColor;
501 dc->dcPenColor = dcs->dcPenColor;
502 dc->brushOrgX = dcs->brushOrgX;
503 dc->brushOrgY = dcs->brushOrgY;
504 dc->textAlign = dcs->textAlign;
505 dc->charExtra = dcs->charExtra;
506 dc->breakExtra = dcs->breakExtra;
507 dc->breakRem = dcs->breakRem;
508 dc->MapMode = dcs->MapMode;
509 dc->GraphicsMode = dcs->GraphicsMode;
510 dc->CursPosX = dcs->CursPosX;
511 dc->CursPosY = dcs->CursPosY;
512 dc->ArcDirection = dcs->ArcDirection;
513 dc->xformWorld2Wnd = dcs->xformWorld2Wnd;
514 dc->xformWorld2Vport = dcs->xformWorld2Vport;
515 dc->xformVport2World = dcs->xformVport2World;
516 dc->vport2WorldValid = dcs->vport2WorldValid;
517 dc->BoundsRect = dcs->BoundsRect;
519 dc->wndOrgX = dcs->wndOrgX;
520 dc->wndOrgY = dcs->wndOrgY;
521 dc->wndExtX = dcs->wndExtX;
522 dc->wndExtY = dcs->wndExtY;
523 dc->vportOrgX = dcs->vportOrgX;
524 dc->vportOrgY = dcs->vportOrgY;
525 dc->vportExtX = dcs->vportExtX;
526 dc->vportExtY = dcs->vportExtY;
528 if (dcs->hClipRgn)
530 if (!dc->hClipRgn) dc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
531 CombineRgn( dc->hClipRgn, dcs->hClipRgn, 0, RGN_COPY );
533 else
535 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
536 dc->hClipRgn = 0;
538 if (dcs->hMetaRgn)
540 if (!dc->hMetaRgn) dc->hMetaRgn = CreateRectRgn( 0, 0, 0, 0 );
541 CombineRgn( dc->hMetaRgn, dcs->hMetaRgn, 0, RGN_COPY );
543 else
545 if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
546 dc->hMetaRgn = 0;
548 CLIPPING_UpdateGCRegion( dc );
550 SelectObject( hdc, dcs->hBitmap );
551 SelectObject( hdc, dcs->hBrush );
552 SelectObject( hdc, dcs->hFont );
553 SelectObject( hdc, dcs->hPen );
554 SetBkColor( hdc, dcs->backgroundColor);
555 SetTextColor( hdc, dcs->textColor);
556 GDISelectPalette( hdc, dcs->hPalette, FALSE );
558 dc->saved_dc = dcs->saved_dc;
559 dcs->saved_dc = 0;
560 dc->saveLevel = save_level - 1;
562 release_dc_ptr( dcs );
564 /* now destroy all the saved DCs */
566 while (first_dcs)
568 if (!(dcs = get_dc_ptr( first_dcs ))) break;
569 hdcs = dcs->saved_dc;
570 free_dc_ptr( dcs );
571 first_dcs = hdcs;
573 release_dc_ptr( dc );
574 return TRUE;
578 /***********************************************************************
579 * SaveDC (GDI32.@)
581 INT WINAPI SaveDC( HDC hdc )
583 DC * dc;
584 INT ret;
586 if (!(dc = get_dc_ptr( hdc ))) return 0;
588 if(dc->funcs->pSaveDC)
589 ret = dc->funcs->pSaveDC( dc->physDev );
590 else
591 ret = save_dc_state( hdc );
593 release_dc_ptr( dc );
594 return ret;
598 /***********************************************************************
599 * RestoreDC (GDI32.@)
601 BOOL WINAPI RestoreDC( HDC hdc, INT level )
603 DC *dc;
604 BOOL success;
606 TRACE("%p %d\n", hdc, level );
607 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
608 update_dc( dc );
610 if(dc->funcs->pRestoreDC)
611 success = dc->funcs->pRestoreDC( dc->physDev, level );
612 else
613 success = restore_dc_state( hdc, level );
615 release_dc_ptr( dc );
616 return success;
620 /***********************************************************************
621 * CreateDCW (GDI32.@)
623 HDC WINAPI CreateDCW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
624 const DEVMODEW *initData )
626 HDC hdc;
627 DC * dc;
628 const DC_FUNCTIONS *funcs;
629 WCHAR buf[300];
631 GDI_CheckNotLock();
633 if (!device || !DRIVER_GetDriverName( device, buf, 300 ))
635 if (!driver)
637 ERR( "no device found for %s\n", debugstr_w(device) );
638 return 0;
640 strcpyW(buf, driver);
643 if (!(funcs = DRIVER_load_driver( buf )))
645 ERR( "no driver found for %s\n", debugstr_w(buf) );
646 return 0;
648 if (!(dc = alloc_dc_ptr( funcs, OBJ_DC ))) goto error;
649 hdc = dc->hSelf;
651 dc->hBitmap = GDI_inc_ref_count( GetStockObject( DEFAULT_BITMAP ));
652 if (!(dc->hVisRgn = CreateRectRgn( 0, 0, 1, 1 ))) goto error;
654 TRACE("(driver=%s, device=%s, output=%s): returning %p\n",
655 debugstr_w(driver), debugstr_w(device), debugstr_w(output), dc->hSelf );
657 if (dc->funcs->pCreateDC &&
658 !dc->funcs->pCreateDC( hdc, &dc->physDev, buf, device, output, initData ))
660 WARN("creation aborted by device\n" );
661 goto error;
664 SetRectRgn( dc->hVisRgn, 0, 0,
665 GetDeviceCaps( hdc, DESKTOPHORZRES ), GetDeviceCaps( hdc, DESKTOPVERTRES ) );
667 DC_InitDC( dc );
668 release_dc_ptr( dc );
669 return hdc;
671 error:
672 if (dc) free_dc_ptr( dc );
673 DRIVER_release_driver( funcs );
674 return 0;
678 /***********************************************************************
679 * CreateDCA (GDI32.@)
681 HDC WINAPI CreateDCA( LPCSTR driver, LPCSTR device, LPCSTR output,
682 const DEVMODEA *initData )
684 UNICODE_STRING driverW, deviceW, outputW;
685 DEVMODEW *initDataW;
686 HDC ret;
688 if (driver) RtlCreateUnicodeStringFromAsciiz(&driverW, driver);
689 else driverW.Buffer = NULL;
691 if (device) RtlCreateUnicodeStringFromAsciiz(&deviceW, device);
692 else deviceW.Buffer = NULL;
694 if (output) RtlCreateUnicodeStringFromAsciiz(&outputW, output);
695 else outputW.Buffer = NULL;
697 initDataW = NULL;
698 if (initData)
700 /* don't convert initData for DISPLAY driver, it's not used */
701 if (!driverW.Buffer || strcmpiW( driverW.Buffer, displayW ))
702 initDataW = GdiConvertToDevmodeW(initData);
705 ret = CreateDCW( driverW.Buffer, deviceW.Buffer, outputW.Buffer, initDataW );
707 RtlFreeUnicodeString(&driverW);
708 RtlFreeUnicodeString(&deviceW);
709 RtlFreeUnicodeString(&outputW);
710 HeapFree(GetProcessHeap(), 0, initDataW);
711 return ret;
715 /***********************************************************************
716 * CreateICA (GDI32.@)
718 HDC WINAPI CreateICA( LPCSTR driver, LPCSTR device, LPCSTR output,
719 const DEVMODEA* initData )
721 /* Nothing special yet for ICs */
722 return CreateDCA( driver, device, output, initData );
726 /***********************************************************************
727 * CreateICW (GDI32.@)
729 HDC WINAPI CreateICW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
730 const DEVMODEW* initData )
732 /* Nothing special yet for ICs */
733 return CreateDCW( driver, device, output, initData );
737 /***********************************************************************
738 * CreateCompatibleDC (GDI32.@)
740 HDC WINAPI CreateCompatibleDC( HDC hdc )
742 DC *dc, *origDC;
743 HDC ret;
744 const DC_FUNCTIONS *funcs = NULL;
745 PHYSDEV physDev = NULL;
747 GDI_CheckNotLock();
749 if ((origDC = get_dc_ptr( hdc )))
751 if (GetObjectType( hdc ) == OBJ_DC)
753 funcs = origDC->funcs;
754 physDev = origDC->physDev;
756 release_dc_ptr( origDC );
757 if (funcs) funcs = DRIVER_get_driver( funcs );
759 else if (hdc) return 0;
761 if (!funcs && !(funcs = DRIVER_load_driver( displayW ))) return 0;
763 if (!(dc = alloc_dc_ptr( funcs, OBJ_MEMDC ))) goto error;
765 TRACE("(%p): returning %p\n", hdc, dc->hSelf );
767 dc->hBitmap = GDI_inc_ref_count( GetStockObject( DEFAULT_BITMAP ));
768 if (!(dc->hVisRgn = CreateRectRgn( 0, 0, 1, 1 ))) goto error; /* default bitmap is 1x1 */
770 /* Copy the driver-specific physical device info into
771 * the new DC. The driver may use this read-only info
772 * while creating the compatible DC below. */
773 dc->physDev = physDev;
774 ret = dc->hSelf;
776 if (dc->funcs->pCreateDC &&
777 !dc->funcs->pCreateDC( dc->hSelf, &dc->physDev, NULL, NULL, NULL, NULL ))
779 WARN("creation aborted by device\n");
780 goto error;
783 DC_InitDC( dc );
784 release_dc_ptr( dc );
785 return ret;
787 error:
788 if (dc) free_dc_ptr( dc );
789 DRIVER_release_driver( funcs );
790 return 0;
794 /***********************************************************************
795 * DeleteDC (GDI32.@)
797 BOOL WINAPI DeleteDC( HDC hdc )
799 const DC_FUNCTIONS *funcs = NULL;
800 DC * dc;
802 TRACE("%p\n", hdc );
804 GDI_CheckNotLock();
806 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
807 if (dc->refcount != 1)
809 FIXME( "not deleting busy DC %p refcount %u\n", dc->hSelf, dc->refcount );
810 release_dc_ptr( dc );
811 return FALSE;
814 /* Call hook procedure to check whether is it OK to delete this DC */
815 if (dc->hookThunk && !dc->hookThunk( hdc, DCHC_DELETEDC, dc->dwHookData, 0 ))
817 release_dc_ptr( dc );
818 return FALSE;
821 while (dc->saveLevel)
823 DC * dcs;
824 HDC hdcs = dc->saved_dc;
825 if (!(dcs = get_dc_ptr( hdcs ))) break;
826 dc->saved_dc = dcs->saved_dc;
827 dc->saveLevel--;
828 free_dc_ptr( dcs );
831 if (!(dc->flags & DC_SAVED))
833 SelectObject( hdc, GetStockObject(BLACK_PEN) );
834 SelectObject( hdc, GetStockObject(WHITE_BRUSH) );
835 SelectObject( hdc, GetStockObject(SYSTEM_FONT) );
836 SelectObject( hdc, GetStockObject(DEFAULT_BITMAP) );
837 funcs = dc->funcs;
838 if (dc->funcs->pDeleteDC) dc->funcs->pDeleteDC(dc->physDev);
839 dc->physDev = NULL;
842 while (dc->saved_visrgn)
844 struct saved_visrgn *next = dc->saved_visrgn->next;
845 DeleteObject( dc->saved_visrgn->hrgn );
846 HeapFree( GetProcessHeap(), 0, dc->saved_visrgn );
847 dc->saved_visrgn = next;
850 free_dc_ptr( dc );
851 if (funcs) DRIVER_release_driver( funcs ); /* do that after releasing the GDI lock */
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) ret = dc->funcs->pResetDC( dc->physDev, devmode );
867 release_dc_ptr( dc );
869 return ret;
873 /***********************************************************************
874 * ResetDCA (GDI32.@)
876 HDC WINAPI ResetDCA( HDC hdc, const DEVMODEA *devmode )
878 DEVMODEW *devmodeW;
879 HDC ret;
881 if (devmode) devmodeW = GdiConvertToDevmodeW(devmode);
882 else devmodeW = NULL;
884 ret = ResetDCW(hdc, devmodeW);
886 HeapFree(GetProcessHeap(), 0, devmodeW);
887 return ret;
891 /***********************************************************************
892 * GetDeviceCaps (GDI32.@)
894 INT WINAPI GetDeviceCaps( HDC hdc, INT cap )
896 DC *dc;
897 INT ret = 0;
899 if ((dc = get_dc_ptr( hdc )))
901 if (dc->funcs->pGetDeviceCaps) ret = dc->funcs->pGetDeviceCaps( dc->physDev, cap );
902 else switch(cap) /* return meaningful values for some entries */
904 case HORZRES: ret = 640; break;
905 case VERTRES: ret = 480; break;
906 case BITSPIXEL: ret = 1; break;
907 case PLANES: ret = 1; break;
908 case NUMCOLORS: ret = 2; break;
909 case ASPECTX: ret = 36; break;
910 case ASPECTY: ret = 36; break;
911 case ASPECTXY: ret = 51; break;
912 case LOGPIXELSX: ret = 72; break;
913 case LOGPIXELSY: ret = 72; break;
914 case SIZEPALETTE: ret = 2; break;
916 release_dc_ptr( dc );
918 return ret;
922 /***********************************************************************
923 * GetBkColor (GDI32.@)
925 COLORREF WINAPI GetBkColor( HDC hdc )
927 COLORREF ret = 0;
928 DC * dc = get_dc_ptr( hdc );
929 if (dc)
931 ret = dc->backgroundColor;
932 release_dc_ptr( dc );
934 return ret;
938 /***********************************************************************
939 * SetBkColor (GDI32.@)
941 COLORREF WINAPI SetBkColor( HDC hdc, COLORREF color )
943 COLORREF oldColor;
944 DC * dc = get_dc_ptr( hdc );
946 TRACE("hdc=%p color=0x%08x\n", hdc, color);
948 if (!dc) return CLR_INVALID;
949 oldColor = dc->backgroundColor;
950 if (dc->funcs->pSetBkColor)
952 color = dc->funcs->pSetBkColor(dc->physDev, color);
953 if (color == CLR_INVALID) /* don't change it */
955 color = oldColor;
956 oldColor = CLR_INVALID;
959 dc->backgroundColor = color;
960 release_dc_ptr( dc );
961 return oldColor;
965 /***********************************************************************
966 * GetTextColor (GDI32.@)
968 COLORREF WINAPI GetTextColor( HDC hdc )
970 COLORREF ret = 0;
971 DC * dc = get_dc_ptr( hdc );
972 if (dc)
974 ret = dc->textColor;
975 release_dc_ptr( dc );
977 return ret;
981 /***********************************************************************
982 * SetTextColor (GDI32.@)
984 COLORREF WINAPI SetTextColor( HDC hdc, COLORREF color )
986 COLORREF oldColor;
987 DC * dc = get_dc_ptr( hdc );
989 TRACE(" hdc=%p color=0x%08x\n", hdc, color);
991 if (!dc) return CLR_INVALID;
992 oldColor = dc->textColor;
993 if (dc->funcs->pSetTextColor)
995 color = dc->funcs->pSetTextColor(dc->physDev, color);
996 if (color == CLR_INVALID) /* don't change it */
998 color = oldColor;
999 oldColor = CLR_INVALID;
1002 dc->textColor = color;
1003 release_dc_ptr( dc );
1004 return oldColor;
1008 /***********************************************************************
1009 * GetTextAlign (GDI32.@)
1011 UINT WINAPI GetTextAlign( HDC hdc )
1013 UINT ret = 0;
1014 DC * dc = get_dc_ptr( hdc );
1015 if (dc)
1017 ret = dc->textAlign;
1018 release_dc_ptr( dc );
1020 return ret;
1024 /***********************************************************************
1025 * SetTextAlign (GDI32.@)
1027 UINT WINAPI SetTextAlign( HDC hdc, UINT align )
1029 UINT ret;
1030 DC *dc = get_dc_ptr( hdc );
1032 TRACE("hdc=%p align=%d\n", hdc, align);
1034 if (!dc) return 0x0;
1035 ret = dc->textAlign;
1036 if (dc->funcs->pSetTextAlign)
1037 if (!dc->funcs->pSetTextAlign(dc->physDev, align))
1038 ret = GDI_ERROR;
1039 if (ret != GDI_ERROR)
1040 dc->textAlign = align;
1041 release_dc_ptr( dc );
1042 return ret;
1045 /***********************************************************************
1046 * GetDCOrgEx (GDI32.@)
1048 BOOL WINAPI GetDCOrgEx( HDC hDC, LPPOINT lpp )
1050 DC * dc;
1052 if (!lpp) return FALSE;
1053 if (!(dc = get_dc_ptr( hDC ))) return FALSE;
1055 lpp->x = lpp->y = 0;
1056 if (dc->funcs->pGetDCOrgEx) dc->funcs->pGetDCOrgEx( dc->physDev, lpp );
1057 release_dc_ptr( dc );
1058 return TRUE;
1062 /***********************************************************************
1063 * GetGraphicsMode (GDI32.@)
1065 INT WINAPI GetGraphicsMode( HDC hdc )
1067 INT ret = 0;
1068 DC * dc = get_dc_ptr( hdc );
1069 if (dc)
1071 ret = dc->GraphicsMode;
1072 release_dc_ptr( dc );
1074 return ret;
1078 /***********************************************************************
1079 * SetGraphicsMode (GDI32.@)
1081 INT WINAPI SetGraphicsMode( HDC hdc, INT mode )
1083 INT ret = 0;
1084 DC *dc = get_dc_ptr( hdc );
1086 /* One would think that setting the graphics mode to GM_COMPATIBLE
1087 * would also reset the world transformation matrix to the unity
1088 * matrix. However, in Windows, this is not the case. This doesn't
1089 * make a lot of sense to me, but that's the way it is.
1091 if (!dc) return 0;
1092 if ((mode > 0) && (mode <= GM_LAST))
1094 ret = dc->GraphicsMode;
1095 dc->GraphicsMode = mode;
1097 release_dc_ptr( dc );
1098 return ret;
1102 /***********************************************************************
1103 * GetArcDirection (GDI32.@)
1105 INT WINAPI GetArcDirection( HDC hdc )
1107 INT ret = 0;
1108 DC * dc = get_dc_ptr( hdc );
1109 if (dc)
1111 ret = dc->ArcDirection;
1112 release_dc_ptr( dc );
1114 return ret;
1118 /***********************************************************************
1119 * SetArcDirection (GDI32.@)
1121 INT WINAPI SetArcDirection( HDC hdc, INT nDirection )
1123 DC * dc;
1124 INT nOldDirection = 0;
1126 if (nDirection!=AD_COUNTERCLOCKWISE && nDirection!=AD_CLOCKWISE)
1128 SetLastError(ERROR_INVALID_PARAMETER);
1129 return 0;
1132 if ((dc = get_dc_ptr( hdc )))
1134 if (dc->funcs->pSetArcDirection)
1136 dc->funcs->pSetArcDirection(dc->physDev, nDirection);
1138 nOldDirection = dc->ArcDirection;
1139 dc->ArcDirection = nDirection;
1140 release_dc_ptr( dc );
1142 return nOldDirection;
1146 /***********************************************************************
1147 * GetWorldTransform (GDI32.@)
1149 BOOL WINAPI GetWorldTransform( HDC hdc, LPXFORM xform )
1151 DC * dc;
1152 if (!xform) return FALSE;
1153 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
1154 *xform = dc->xformWorld2Wnd;
1155 release_dc_ptr( dc );
1156 return TRUE;
1160 /***********************************************************************
1161 * GetTransform (GDI32.@)
1163 BOOL WINAPI GetTransform( HDC hdc, DWORD unknown, LPXFORM xform )
1165 if (unknown == 0x0203) return GetWorldTransform( hdc, xform );
1166 FIXME("stub: don't know what to do for code %x\n", unknown );
1167 return FALSE;
1171 /***********************************************************************
1172 * SetWorldTransform (GDI32.@)
1174 BOOL WINAPI SetWorldTransform( HDC hdc, const XFORM *xform )
1176 BOOL ret = FALSE;
1177 DC *dc;
1179 if (!xform) return FALSE;
1181 dc = get_dc_ptr( hdc );
1182 if (!dc) return FALSE;
1184 /* Check that graphics mode is GM_ADVANCED */
1185 if (dc->GraphicsMode!=GM_ADVANCED) goto done;
1187 TRACE("eM11 %f eM12 %f eM21 %f eM22 %f eDx %f eDy %f\n",
1188 xform->eM11, xform->eM12, xform->eM21, xform->eM22, xform->eDx, xform->eDy);
1190 /* The transform must conform to (eM11 * eM22 != eM12 * eM21) requirement */
1191 if (xform->eM11 * xform->eM22 == xform->eM12 * xform->eM21) goto done;
1193 if (dc->funcs->pSetWorldTransform)
1195 ret = dc->funcs->pSetWorldTransform(dc->physDev, xform);
1196 if (!ret) goto done;
1199 dc->xformWorld2Wnd = *xform;
1200 DC_UpdateXforms( dc );
1201 ret = TRUE;
1202 done:
1203 release_dc_ptr( dc );
1204 return ret;
1208 /****************************************************************************
1209 * ModifyWorldTransform [GDI32.@]
1210 * Modifies the world transformation for a device context.
1212 * PARAMS
1213 * hdc [I] Handle to device context
1214 * xform [I] XFORM structure that will be used to modify the world
1215 * transformation
1216 * iMode [I] Specifies in what way to modify the world transformation
1217 * Possible values:
1218 * MWT_IDENTITY
1219 * Resets the world transformation to the identity matrix.
1220 * The parameter xform is ignored.
1221 * MWT_LEFTMULTIPLY
1222 * Multiplies xform into the world transformation matrix from
1223 * the left.
1224 * MWT_RIGHTMULTIPLY
1225 * Multiplies xform into the world transformation matrix from
1226 * the right.
1228 * RETURNS
1229 * Success: TRUE.
1230 * Failure: FALSE. Use GetLastError() to determine the cause.
1232 BOOL WINAPI ModifyWorldTransform( HDC hdc, const XFORM *xform,
1233 DWORD iMode )
1235 BOOL ret = FALSE;
1236 DC *dc = get_dc_ptr( hdc );
1238 /* Check for illegal parameters */
1239 if (!dc) return FALSE;
1240 if (!xform && iMode != MWT_IDENTITY) goto done;
1242 /* Check that graphics mode is GM_ADVANCED */
1243 if (dc->GraphicsMode!=GM_ADVANCED) goto done;
1245 if (dc->funcs->pModifyWorldTransform)
1247 ret = dc->funcs->pModifyWorldTransform(dc->physDev, xform, iMode);
1248 if (!ret) goto done;
1251 switch (iMode)
1253 case MWT_IDENTITY:
1254 dc->xformWorld2Wnd.eM11 = 1.0f;
1255 dc->xformWorld2Wnd.eM12 = 0.0f;
1256 dc->xformWorld2Wnd.eM21 = 0.0f;
1257 dc->xformWorld2Wnd.eM22 = 1.0f;
1258 dc->xformWorld2Wnd.eDx = 0.0f;
1259 dc->xformWorld2Wnd.eDy = 0.0f;
1260 break;
1261 case MWT_LEFTMULTIPLY:
1262 CombineTransform( &dc->xformWorld2Wnd, xform,
1263 &dc->xformWorld2Wnd );
1264 break;
1265 case MWT_RIGHTMULTIPLY:
1266 CombineTransform( &dc->xformWorld2Wnd, &dc->xformWorld2Wnd,
1267 xform );
1268 break;
1269 default:
1270 goto done;
1273 DC_UpdateXforms( dc );
1274 ret = TRUE;
1275 done:
1276 release_dc_ptr( dc );
1277 return ret;
1281 /****************************************************************************
1282 * CombineTransform [GDI32.@]
1283 * Combines two transformation matrices.
1285 * PARAMS
1286 * xformResult [O] Stores the result of combining the two matrices
1287 * xform1 [I] Specifies the first matrix to apply
1288 * xform2 [I] Specifies the second matrix to apply
1290 * REMARKS
1291 * The same matrix can be passed in for more than one of the parameters.
1293 * RETURNS
1294 * Success: TRUE.
1295 * Failure: FALSE. Use GetLastError() to determine the cause.
1297 BOOL WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
1298 const XFORM *xform2 )
1300 XFORM xformTemp;
1302 /* Check for illegal parameters */
1303 if (!xformResult || !xform1 || !xform2)
1304 return FALSE;
1306 /* Create the result in a temporary XFORM, since xformResult may be
1307 * equal to xform1 or xform2 */
1308 xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
1309 xform1->eM12 * xform2->eM21;
1310 xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
1311 xform1->eM12 * xform2->eM22;
1312 xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
1313 xform1->eM22 * xform2->eM21;
1314 xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
1315 xform1->eM22 * xform2->eM22;
1316 xformTemp.eDx = xform1->eDx * xform2->eM11 +
1317 xform1->eDy * xform2->eM21 +
1318 xform2->eDx;
1319 xformTemp.eDy = xform1->eDx * xform2->eM12 +
1320 xform1->eDy * xform2->eM22 +
1321 xform2->eDy;
1323 /* Copy the result to xformResult */
1324 *xformResult = xformTemp;
1326 return TRUE;
1330 /***********************************************************************
1331 * SetDCHook (GDI32.@)
1333 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1335 BOOL WINAPI SetDCHook( HDC hdc, DCHOOKPROC hookProc, DWORD_PTR dwHookData )
1337 DC *dc = get_dc_ptr( hdc );
1339 if (!dc) return FALSE;
1341 if (!(dc->flags & DC_SAVED))
1343 dc->dwHookData = dwHookData;
1344 dc->hookThunk = hookProc;
1346 release_dc_ptr( dc );
1347 return TRUE;
1351 /***********************************************************************
1352 * GetDCHook (GDI32.@)
1354 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1356 DWORD_PTR WINAPI GetDCHook( HDC hdc, DCHOOKPROC *proc )
1358 DC *dc = get_dc_ptr( hdc );
1359 DWORD_PTR ret;
1361 if (!dc) return 0;
1362 if (proc) *proc = dc->hookThunk;
1363 ret = dc->dwHookData;
1364 release_dc_ptr( dc );
1365 return ret;
1369 /* relay function to call the 16-bit DC hook proc */
1370 static BOOL WINAPI call_dc_hook16( HDC hdc, WORD code, DWORD_PTR data, LPARAM lParam )
1372 WORD args[6];
1373 DWORD ret = 0;
1374 DC *dc = get_dc_ptr( hdc );
1376 if (!dc) return FALSE;
1377 if (dc->hookProc)
1379 args[5] = HDC_16(hdc);
1380 args[4] = code;
1381 args[3] = HIWORD(data);
1382 args[2] = LOWORD(data);
1383 args[1] = HIWORD(lParam);
1384 args[0] = LOWORD(lParam);
1385 WOWCallback16Ex( (DWORD)dc->hookProc, WCB16_PASCAL, sizeof(args), args, &ret );
1387 release_dc_ptr( dc );
1388 return LOWORD(ret);
1391 /***********************************************************************
1392 * SetDCHook (GDI.190)
1394 BOOL16 WINAPI SetDCHook16( HDC16 hdc16, FARPROC16 hookProc, DWORD dwHookData )
1396 DC *dc = get_dc_ptr( HDC_32(hdc16) );
1398 if (!dc) return FALSE;
1399 if (!(dc->flags & DC_SAVED))
1401 dc->dwHookData = dwHookData;
1402 dc->hookThunk = call_dc_hook16;
1403 dc->hookProc = hookProc;
1405 release_dc_ptr( dc );
1406 return TRUE;
1410 /***********************************************************************
1411 * GetDCHook (GDI.191)
1413 DWORD WINAPI GetDCHook16( HDC16 hdc16, FARPROC16 *phookProc )
1415 HDC hdc = HDC_32( hdc16 );
1416 DC *dc = get_dc_ptr( hdc );
1417 DWORD ret;
1419 if (!dc) return 0;
1420 *phookProc = 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( dc );
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) *rect = dc->BoundsRect;
1557 ret = ((dc->flags & DC_BOUNDS_SET) ? DCB_SET : DCB_RESET);
1559 if (flags & DCB_RESET)
1561 dc->BoundsRect.left = 0;
1562 dc->BoundsRect.top = 0;
1563 dc->BoundsRect.right = 0;
1564 dc->BoundsRect.bottom = 0;
1565 dc->flags &= ~DC_BOUNDS_SET;
1567 release_dc_ptr( dc );
1568 return ret;
1572 /***********************************************************************
1573 * SetBoundsRect (GDI32.@)
1575 UINT WINAPI SetBoundsRect(HDC hdc, const RECT* rect, UINT flags)
1577 UINT ret;
1578 DC *dc;
1580 if ((flags & DCB_ENABLE) && (flags & DCB_DISABLE)) return 0;
1581 if (!(dc = get_dc_ptr( hdc ))) return 0;
1583 ret = ((dc->flags & DC_BOUNDS_ENABLE) ? DCB_ENABLE : DCB_DISABLE) |
1584 ((dc->flags & DC_BOUNDS_SET) ? DCB_SET : DCB_RESET);
1586 if (flags & DCB_RESET)
1588 dc->BoundsRect.left = 0;
1589 dc->BoundsRect.top = 0;
1590 dc->BoundsRect.right = 0;
1591 dc->BoundsRect.bottom = 0;
1592 dc->flags &= ~DC_BOUNDS_SET;
1595 if ((flags & DCB_ACCUMULATE) && rect && rect->left < rect->right && rect->top < rect->bottom)
1597 if (dc->flags & DC_BOUNDS_SET)
1599 dc->BoundsRect.left = min( dc->BoundsRect.left, rect->left );
1600 dc->BoundsRect.top = min( dc->BoundsRect.top, rect->top );
1601 dc->BoundsRect.right = max( dc->BoundsRect.right, rect->right );
1602 dc->BoundsRect.bottom = max( dc->BoundsRect.bottom, rect->bottom );
1604 else
1606 dc->BoundsRect = *rect;
1607 dc->flags |= DC_BOUNDS_SET;
1611 if (flags & DCB_ENABLE) dc->flags |= DC_BOUNDS_ENABLE;
1612 if (flags & DCB_DISABLE) dc->flags &= ~DC_BOUNDS_ENABLE;
1614 release_dc_ptr( dc );
1615 return ret;
1619 /***********************************************************************
1620 * GetRelAbs (GDI32.@)
1622 INT WINAPI GetRelAbs( HDC hdc, DWORD dwIgnore )
1624 INT ret = 0;
1625 DC *dc = get_dc_ptr( hdc );
1626 if (dc)
1628 ret = dc->relAbsMode;
1629 release_dc_ptr( dc );
1631 return ret;
1637 /***********************************************************************
1638 * GetBkMode (GDI32.@)
1640 INT WINAPI GetBkMode( HDC hdc )
1642 INT ret = 0;
1643 DC * dc = get_dc_ptr( hdc );
1644 if (dc)
1646 ret = dc->backgroundMode;
1647 release_dc_ptr( dc );
1649 return ret;
1653 /***********************************************************************
1654 * SetBkMode (GDI32.@)
1656 INT WINAPI SetBkMode( HDC hdc, INT mode )
1658 INT ret;
1659 DC *dc;
1660 if ((mode <= 0) || (mode > BKMODE_LAST))
1662 SetLastError(ERROR_INVALID_PARAMETER);
1663 return 0;
1665 if (!(dc = get_dc_ptr( hdc ))) return 0;
1667 ret = dc->backgroundMode;
1668 if (dc->funcs->pSetBkMode)
1669 if (!dc->funcs->pSetBkMode( dc->physDev, mode ))
1670 ret = 0;
1671 if (ret)
1672 dc->backgroundMode = mode;
1673 release_dc_ptr( dc );
1674 return ret;
1678 /***********************************************************************
1679 * GetROP2 (GDI32.@)
1681 INT WINAPI GetROP2( HDC hdc )
1683 INT ret = 0;
1684 DC * dc = get_dc_ptr( hdc );
1685 if (dc)
1687 ret = dc->ROPmode;
1688 release_dc_ptr( dc );
1690 return ret;
1694 /***********************************************************************
1695 * SetROP2 (GDI32.@)
1697 INT WINAPI SetROP2( HDC hdc, INT mode )
1699 INT ret;
1700 DC *dc;
1701 if ((mode < R2_BLACK) || (mode > R2_WHITE))
1703 SetLastError(ERROR_INVALID_PARAMETER);
1704 return 0;
1706 if (!(dc = get_dc_ptr( hdc ))) return 0;
1707 ret = dc->ROPmode;
1708 if (dc->funcs->pSetROP2)
1709 if (!dc->funcs->pSetROP2( dc->physDev, mode ))
1710 ret = 0;
1711 if (ret)
1712 dc->ROPmode = mode;
1713 release_dc_ptr( dc );
1714 return ret;
1718 /***********************************************************************
1719 * SetRelAbs (GDI32.@)
1721 INT WINAPI SetRelAbs( HDC hdc, INT mode )
1723 INT ret;
1724 DC *dc;
1725 if ((mode != ABSOLUTE) && (mode != RELATIVE))
1727 SetLastError(ERROR_INVALID_PARAMETER);
1728 return 0;
1730 if (!(dc = get_dc_ptr( hdc ))) return 0;
1731 if (dc->funcs->pSetRelAbs)
1732 ret = dc->funcs->pSetRelAbs( dc->physDev, mode );
1733 else
1735 ret = dc->relAbsMode;
1736 dc->relAbsMode = mode;
1738 release_dc_ptr( dc );
1739 return ret;
1743 /***********************************************************************
1744 * GetPolyFillMode (GDI32.@)
1746 INT WINAPI GetPolyFillMode( HDC hdc )
1748 INT ret = 0;
1749 DC * dc = get_dc_ptr( hdc );
1750 if (dc)
1752 ret = dc->polyFillMode;
1753 release_dc_ptr( dc );
1755 return ret;
1759 /***********************************************************************
1760 * SetPolyFillMode (GDI32.@)
1762 INT WINAPI SetPolyFillMode( HDC hdc, INT mode )
1764 INT ret;
1765 DC *dc;
1766 if ((mode <= 0) || (mode > POLYFILL_LAST))
1768 SetLastError(ERROR_INVALID_PARAMETER);
1769 return 0;
1771 if (!(dc = get_dc_ptr( hdc ))) return 0;
1772 ret = dc->polyFillMode;
1773 if (dc->funcs->pSetPolyFillMode)
1774 if (!dc->funcs->pSetPolyFillMode( dc->physDev, mode ))
1775 ret = 0;
1776 if (ret)
1777 dc->polyFillMode = mode;
1778 release_dc_ptr( dc );
1779 return ret;
1783 /***********************************************************************
1784 * GetStretchBltMode (GDI32.@)
1786 INT WINAPI GetStretchBltMode( HDC hdc )
1788 INT ret = 0;
1789 DC * dc = get_dc_ptr( hdc );
1790 if (dc)
1792 ret = dc->stretchBltMode;
1793 release_dc_ptr( dc );
1795 return ret;
1799 /***********************************************************************
1800 * SetStretchBltMode (GDI32.@)
1802 INT WINAPI SetStretchBltMode( HDC hdc, INT mode )
1804 INT ret;
1805 DC *dc;
1806 if ((mode <= 0) || (mode > MAXSTRETCHBLTMODE))
1808 SetLastError(ERROR_INVALID_PARAMETER);
1809 return 0;
1811 if (!(dc = get_dc_ptr( hdc ))) return 0;
1812 ret = dc->stretchBltMode;
1813 if (dc->funcs->pSetStretchBltMode)
1814 if (!dc->funcs->pSetStretchBltMode( dc->physDev, mode ))
1815 ret = 0;
1816 if (ret)
1817 dc->stretchBltMode = mode;
1818 release_dc_ptr( dc );
1819 return ret;
1823 /***********************************************************************
1824 * GetMapMode (GDI32.@)
1826 INT WINAPI GetMapMode( HDC hdc )
1828 INT ret = 0;
1829 DC * dc = get_dc_ptr( hdc );
1830 if (dc)
1832 ret = dc->MapMode;
1833 release_dc_ptr( dc );
1835 return ret;
1839 /***********************************************************************
1840 * GetBrushOrgEx (GDI32.@)
1842 BOOL WINAPI GetBrushOrgEx( HDC hdc, LPPOINT pt )
1844 DC * dc = get_dc_ptr( hdc );
1845 if (!dc) return FALSE;
1846 pt->x = dc->brushOrgX;
1847 pt->y = dc->brushOrgY;
1848 release_dc_ptr( dc );
1849 return TRUE;
1853 /***********************************************************************
1854 * GetCurrentPositionEx (GDI32.@)
1856 BOOL WINAPI GetCurrentPositionEx( HDC hdc, LPPOINT pt )
1858 DC * dc = get_dc_ptr( hdc );
1859 if (!dc) return FALSE;
1860 pt->x = dc->CursPosX;
1861 pt->y = dc->CursPosY;
1862 release_dc_ptr( dc );
1863 return TRUE;
1867 /***********************************************************************
1868 * GetViewportExtEx (GDI32.@)
1870 BOOL WINAPI GetViewportExtEx( HDC hdc, LPSIZE size )
1872 DC * dc = get_dc_ptr( hdc );
1873 if (!dc) return FALSE;
1874 size->cx = dc->vportExtX;
1875 size->cy = dc->vportExtY;
1876 release_dc_ptr( dc );
1877 return TRUE;
1881 /***********************************************************************
1882 * GetViewportOrgEx (GDI32.@)
1884 BOOL WINAPI GetViewportOrgEx( HDC hdc, LPPOINT pt )
1886 DC * dc = get_dc_ptr( hdc );
1887 if (!dc) return FALSE;
1888 pt->x = dc->vportOrgX;
1889 pt->y = dc->vportOrgY;
1890 release_dc_ptr( dc );
1891 return TRUE;
1895 /***********************************************************************
1896 * GetWindowExtEx (GDI32.@)
1898 BOOL WINAPI GetWindowExtEx( HDC hdc, LPSIZE size )
1900 DC * dc = get_dc_ptr( hdc );
1901 if (!dc) return FALSE;
1902 size->cx = dc->wndExtX;
1903 size->cy = dc->wndExtY;
1904 release_dc_ptr( dc );
1905 return TRUE;
1909 /***********************************************************************
1910 * GetWindowOrgEx (GDI32.@)
1912 BOOL WINAPI GetWindowOrgEx( HDC hdc, LPPOINT pt )
1914 DC * dc = get_dc_ptr( hdc );
1915 if (!dc) return FALSE;
1916 pt->x = dc->wndOrgX;
1917 pt->y = dc->wndOrgY;
1918 release_dc_ptr( dc );
1919 return TRUE;
1923 /***********************************************************************
1924 * GetLayout (GDI32.@)
1926 * Gets left->right or right->left text layout flags of a dc.
1929 DWORD WINAPI GetLayout(HDC hdc)
1931 DWORD layout = GDI_ERROR;
1933 DC * dc = get_dc_ptr( hdc );
1934 if (dc)
1936 layout = dc->layout;
1937 release_dc_ptr( dc );
1940 TRACE("hdc : %p, layout : %08x\n", hdc, layout);
1942 return layout;
1945 /***********************************************************************
1946 * SetLayout (GDI32.@)
1948 * Sets left->right or right->left text layout flags of a dc.
1951 DWORD WINAPI SetLayout(HDC hdc, DWORD layout)
1953 DWORD oldlayout = GDI_ERROR;
1955 DC * dc = get_dc_ptr( hdc );
1956 if (dc)
1958 oldlayout = dc->layout;
1959 dc->layout = layout;
1960 release_dc_ptr( dc );
1963 TRACE("hdc : %p, old layout : %08x, new layout : %08x\n", hdc, oldlayout, layout);
1965 return oldlayout;
1968 /***********************************************************************
1969 * GetDCBrushColor (GDI32.@)
1971 * Retrieves the current brush color for the specified device
1972 * context (DC).
1975 COLORREF WINAPI GetDCBrushColor(HDC hdc)
1977 DC *dc;
1978 COLORREF dcBrushColor = CLR_INVALID;
1980 TRACE("hdc(%p)\n", hdc);
1982 dc = get_dc_ptr( hdc );
1983 if (dc)
1985 dcBrushColor = dc->dcBrushColor;
1986 release_dc_ptr( dc );
1989 return dcBrushColor;
1992 /***********************************************************************
1993 * SetDCBrushColor (GDI32.@)
1995 * Sets the current device context (DC) brush color to the specified
1996 * color value. If the device cannot represent the specified color
1997 * value, the color is set to the nearest physical color.
2000 COLORREF WINAPI SetDCBrushColor(HDC hdc, COLORREF crColor)
2002 DC *dc;
2003 COLORREF oldClr = CLR_INVALID;
2005 TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
2007 dc = get_dc_ptr( hdc );
2008 if (dc)
2010 if (dc->funcs->pSetDCBrushColor)
2011 crColor = dc->funcs->pSetDCBrushColor( dc->physDev, crColor );
2012 else if (dc->hBrush == GetStockObject( DC_BRUSH ))
2014 /* If DC_BRUSH is selected, update driver pen color */
2015 HBRUSH hBrush = CreateSolidBrush( crColor );
2016 dc->funcs->pSelectBrush( dc->physDev, hBrush );
2017 DeleteObject( hBrush );
2020 if (crColor != CLR_INVALID)
2022 oldClr = dc->dcBrushColor;
2023 dc->dcBrushColor = crColor;
2026 release_dc_ptr( dc );
2029 return oldClr;
2032 /***********************************************************************
2033 * GetDCPenColor (GDI32.@)
2035 * Retrieves the current pen color for the specified device
2036 * context (DC).
2039 COLORREF WINAPI GetDCPenColor(HDC hdc)
2041 DC *dc;
2042 COLORREF dcPenColor = CLR_INVALID;
2044 TRACE("hdc(%p)\n", hdc);
2046 dc = get_dc_ptr( hdc );
2047 if (dc)
2049 dcPenColor = dc->dcPenColor;
2050 release_dc_ptr( dc );
2053 return dcPenColor;
2056 /***********************************************************************
2057 * SetDCPenColor (GDI32.@)
2059 * Sets the current device context (DC) pen color to the specified
2060 * color value. If the device cannot represent the specified color
2061 * value, the color is set to the nearest physical color.
2064 COLORREF WINAPI SetDCPenColor(HDC hdc, COLORREF crColor)
2066 DC *dc;
2067 COLORREF oldClr = CLR_INVALID;
2069 TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
2071 dc = get_dc_ptr( hdc );
2072 if (dc)
2074 if (dc->funcs->pSetDCPenColor)
2075 crColor = dc->funcs->pSetDCPenColor( dc->physDev, crColor );
2076 else if (dc->hPen == GetStockObject( DC_PEN ))
2078 /* If DC_PEN is selected, update the driver pen color */
2079 LOGPEN logpen = { PS_SOLID, { 0, 0 }, crColor };
2080 HPEN hPen = CreatePenIndirect( &logpen );
2081 dc->funcs->pSelectPen( dc->physDev, hPen );
2082 DeleteObject( hPen );
2085 if (crColor != CLR_INVALID)
2087 oldClr = dc->dcPenColor;
2088 dc->dcPenColor = crColor;
2091 release_dc_ptr( dc );
2094 return oldClr;
2097 /***********************************************************************
2098 * CancelDC (GDI32.@)
2100 BOOL WINAPI CancelDC(HDC hdc)
2102 FIXME("stub\n");
2103 return TRUE;
2106 /***********************************************************************
2107 * SetVirtualResolution (GDI32.@)
2109 * Undocumented on msdn. Called when PowerPoint XP saves a file.
2111 DWORD WINAPI SetVirtualResolution(HDC hdc, DWORD dw2, DWORD dw3, DWORD dw4, DWORD dw5)
2113 FIXME("(%p %08x %08x %08x %08x): stub!\n", hdc, dw2, dw3, dw4, dw5);
2114 return FALSE;
2117 /*******************************************************************
2118 * GetMiterLimit [GDI32.@]
2122 BOOL WINAPI GetMiterLimit(HDC hdc, PFLOAT peLimit)
2124 BOOL bRet = FALSE;
2125 DC *dc;
2127 TRACE("(%p,%p)\n", hdc, peLimit);
2129 dc = get_dc_ptr( hdc );
2130 if (dc)
2132 if (peLimit)
2133 *peLimit = dc->miterLimit;
2135 release_dc_ptr( dc );
2136 bRet = TRUE;
2138 return bRet;
2141 /*******************************************************************
2142 * SetMiterLimit [GDI32.@]
2146 BOOL WINAPI SetMiterLimit(HDC hdc, FLOAT eNewLimit, PFLOAT peOldLimit)
2148 BOOL bRet = FALSE;
2149 DC *dc;
2151 TRACE("(%p,%f,%p)\n", hdc, eNewLimit, peOldLimit);
2153 dc = get_dc_ptr( hdc );
2154 if (dc)
2156 if (peOldLimit)
2157 *peOldLimit = dc->miterLimit;
2158 dc->miterLimit = eNewLimit;
2159 release_dc_ptr( dc );
2160 bRet = TRUE;
2162 return bRet;
2165 /*******************************************************************
2166 * GdiIsMetaPrintDC [GDI32.@]
2168 BOOL WINAPI GdiIsMetaPrintDC(HDC hdc)
2170 FIXME("%p\n", hdc);
2171 return FALSE;
2174 /*******************************************************************
2175 * GdiIsMetaFileDC [GDI32.@]
2177 BOOL WINAPI GdiIsMetaFileDC(HDC hdc)
2179 TRACE("%p\n", hdc);
2181 switch( GetObjectType( hdc ) )
2183 case OBJ_METADC:
2184 case OBJ_ENHMETADC:
2185 return TRUE;
2187 return FALSE;
2190 /*******************************************************************
2191 * GdiIsPlayMetafileDC [GDI32.@]
2193 BOOL WINAPI GdiIsPlayMetafileDC(HDC hdc)
2195 FIXME("%p\n", hdc);
2196 return FALSE;