Fixed the parsing of id1.id2 which could be either access to field id2
[wine/gsoc_dplay.git] / objects / dc.c
blob56b353d66daa20398c58200ad12f53b3e4228c04
1 /*
2 * GDI Device Context functions
4 * Copyright 1993 Alexandre Julliard
6 */
8 #include "config.h"
10 #include <stdlib.h>
11 #include <string.h>
12 #include "gdi.h"
13 #include "heap.h"
14 #include "debugtools.h"
15 #include "font.h"
16 #include "callback.h"
17 #include "winerror.h"
18 #include "windef.h"
19 #include "wingdi.h"
20 #include "wine/winuser16.h"
22 DEFAULT_DEBUG_CHANNEL(dc);
25 /***********************************************************************
26 * DC_AllocDC
28 DC *DC_AllocDC( const DC_FUNCTIONS *funcs )
30 HDC hdc;
31 DC *dc;
33 if (!(dc = GDI_AllocObject( sizeof(*dc), DC_MAGIC, &hdc ))) return NULL;
35 dc->hSelf = hdc;
36 dc->funcs = funcs;
37 dc->physDev = NULL;
38 dc->saveLevel = 0;
39 dc->dwHookData = 0;
40 dc->hookProc = NULL;
41 dc->hookThunk = NULL;
42 dc->wndOrgX = 0;
43 dc->wndOrgY = 0;
44 dc->wndExtX = 1;
45 dc->wndExtY = 1;
46 dc->vportOrgX = 0;
47 dc->vportOrgY = 0;
48 dc->vportExtX = 1;
49 dc->vportExtY = 1;
50 dc->flags = 0;
51 dc->hClipRgn = 0;
52 dc->hVisRgn = 0;
53 dc->hGCClipRgn = 0;
54 dc->hPen = GetStockObject( BLACK_PEN );
55 dc->hBrush = GetStockObject( WHITE_BRUSH );
56 dc->hFont = GetStockObject( SYSTEM_FONT );
57 dc->hBitmap = 0;
58 dc->hDevice = 0;
59 dc->hPalette = GetStockObject( DEFAULT_PALETTE );
60 dc->ROPmode = R2_COPYPEN;
61 dc->polyFillMode = ALTERNATE;
62 dc->stretchBltMode = BLACKONWHITE;
63 dc->relAbsMode = ABSOLUTE;
64 dc->backgroundMode = OPAQUE;
65 dc->backgroundColor = RGB( 255, 255, 255 );
66 dc->textColor = RGB( 0, 0, 0 );
67 dc->brushOrgX = 0;
68 dc->brushOrgY = 0;
69 dc->textAlign = TA_LEFT | TA_TOP | TA_NOUPDATECP;
70 dc->charExtra = 0;
71 dc->breakTotalExtra = 0;
72 dc->breakCount = 0;
73 dc->breakExtra = 0;
74 dc->breakRem = 0;
75 dc->totalExtent.left = 0;
76 dc->totalExtent.top = 0;
77 dc->totalExtent.right = 0;
78 dc->totalExtent.bottom = 0;
79 dc->bitsPerPixel = 1;
80 dc->MapMode = MM_TEXT;
81 dc->GraphicsMode = GM_COMPATIBLE;
82 dc->DCOrgX = 0;
83 dc->DCOrgY = 0;
84 dc->pAbortProc = NULL;
85 dc->CursPosX = 0;
86 dc->CursPosY = 0;
87 dc->ArcDirection = AD_COUNTERCLOCKWISE;
88 dc->xformWorld2Wnd.eM11 = 1.0f;
89 dc->xformWorld2Wnd.eM12 = 0.0f;
90 dc->xformWorld2Wnd.eM21 = 0.0f;
91 dc->xformWorld2Wnd.eM22 = 1.0f;
92 dc->xformWorld2Wnd.eDx = 0.0f;
93 dc->xformWorld2Wnd.eDy = 0.0f;
94 dc->xformWorld2Vport = dc->xformWorld2Wnd;
95 dc->xformVport2World = dc->xformWorld2Wnd;
96 dc->vport2WorldValid = TRUE;
97 PATH_InitGdiPath(&dc->path);
98 return dc;
103 /***********************************************************************
104 * DC_GetDCPtr
106 DC *DC_GetDCPtr( HDC hdc )
108 GDIOBJHDR *ptr = GDI_GetObjPtr( hdc, MAGIC_DONTCARE );
109 if (!ptr) return NULL;
110 if ((GDIMAGIC(ptr->wMagic) == DC_MAGIC) ||
111 (GDIMAGIC(ptr->wMagic) == METAFILE_DC_MAGIC) ||
112 (GDIMAGIC(ptr->wMagic) == ENHMETAFILE_DC_MAGIC))
113 return (DC *)ptr;
114 GDI_ReleaseObj( hdc );
115 SetLastError( ERROR_INVALID_HANDLE );
116 return NULL;
119 /***********************************************************************
120 * DC_GetDCUpdate
122 * Retrieve a DC ptr while making sure the visRgn is updated.
123 * This function may call up to USER so the GDI lock should _not_
124 * be held when calling it.
126 DC *DC_GetDCUpdate( HDC hdc )
128 DC *dc = DC_GetDCPtr( hdc );
129 if (!dc) return NULL;
130 while (dc->flags & DC_DIRTY)
132 dc->flags &= ~DC_DIRTY;
133 if (!(dc->flags & (DC_SAVED | DC_MEMORY)))
135 DCHOOKPROC proc = dc->hookThunk;
136 if (proc)
138 DWORD data = dc->dwHookData;
139 GDI_ReleaseObj( hdc );
140 proc( hdc, DCHC_INVALIDVISRGN, data, 0 );
141 if (!(dc = DC_GetDCPtr( hdc ))) break;
142 /* otherwise restart the loop in case it became dirty again in the meantime */
146 return dc;
149 /***********************************************************************
150 * DC_InitDC
152 * Setup device-specific DC values for a newly created DC.
154 void DC_InitDC( DC* dc )
156 RealizeDefaultPalette16( dc->hSelf );
157 SetTextColor( dc->hSelf, dc->textColor );
158 SetBkColor( dc->hSelf, dc->backgroundColor );
159 SelectObject( dc->hSelf, dc->hPen );
160 SelectObject( dc->hSelf, dc->hBrush );
161 SelectObject( dc->hSelf, dc->hFont );
162 CLIPPING_UpdateGCRegion( dc );
166 /***********************************************************************
167 * DC_InvertXform
169 * Computes the inverse of the transformation xformSrc and stores it to
170 * xformDest. Returns TRUE if successful or FALSE if the xformSrc matrix
171 * is singular.
173 static BOOL DC_InvertXform( const XFORM *xformSrc, XFORM *xformDest )
175 FLOAT determinant;
177 determinant = xformSrc->eM11*xformSrc->eM22 -
178 xformSrc->eM12*xformSrc->eM21;
179 if (determinant > -1e-12 && determinant < 1e-12)
180 return FALSE;
182 xformDest->eM11 = xformSrc->eM22 / determinant;
183 xformDest->eM12 = -xformSrc->eM12 / determinant;
184 xformDest->eM21 = -xformSrc->eM21 / determinant;
185 xformDest->eM22 = xformSrc->eM11 / determinant;
186 xformDest->eDx = -xformSrc->eDx * xformDest->eM11 -
187 xformSrc->eDy * xformDest->eM21;
188 xformDest->eDy = -xformSrc->eDx * xformDest->eM12 -
189 xformSrc->eDy * xformDest->eM22;
191 return TRUE;
195 /***********************************************************************
196 * DC_UpdateXforms
198 * Updates the xformWorld2Vport, xformVport2World and vport2WorldValid
199 * fields of the specified DC by creating a transformation that
200 * represents the current mapping mode and combining it with the DC's
201 * world transform. This function should be called whenever the
202 * parameters associated with the mapping mode (window and viewport
203 * extents and origins) or the world transform change.
205 void DC_UpdateXforms( DC *dc )
207 XFORM xformWnd2Vport;
208 FLOAT scaleX, scaleY;
210 /* Construct a transformation to do the window-to-viewport conversion */
211 scaleX = (FLOAT)dc->vportExtX / (FLOAT)dc->wndExtX;
212 scaleY = (FLOAT)dc->vportExtY / (FLOAT)dc->wndExtY;
213 xformWnd2Vport.eM11 = scaleX;
214 xformWnd2Vport.eM12 = 0.0;
215 xformWnd2Vport.eM21 = 0.0;
216 xformWnd2Vport.eM22 = scaleY;
217 xformWnd2Vport.eDx = (FLOAT)dc->vportOrgX -
218 scaleX * (FLOAT)dc->wndOrgX;
219 xformWnd2Vport.eDy = (FLOAT)dc->vportOrgY -
220 scaleY * (FLOAT)dc->wndOrgY;
222 /* Combine with the world transformation */
223 CombineTransform( &dc->xformWorld2Vport, &dc->xformWorld2Wnd,
224 &xformWnd2Vport );
226 /* Create inverse of world-to-viewport transformation */
227 dc->vport2WorldValid = DC_InvertXform( &dc->xformWorld2Vport,
228 &dc->xformVport2World );
232 /***********************************************************************
233 * GetDCState (GDI.179)
235 HDC16 WINAPI GetDCState16( HDC16 hdc )
237 DC * newdc, * dc;
238 HGDIOBJ handle;
240 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
241 if (!(newdc = GDI_AllocObject( sizeof(DC), DC_MAGIC, &handle )))
243 GDI_ReleaseObj( hdc );
244 return 0;
246 TRACE("(%04x): returning %04x\n", hdc, handle );
248 newdc->flags = dc->flags | DC_SAVED;
249 newdc->hPen = dc->hPen;
250 newdc->hBrush = dc->hBrush;
251 newdc->hFont = dc->hFont;
252 newdc->hBitmap = dc->hBitmap;
253 newdc->hDevice = dc->hDevice;
254 newdc->hPalette = dc->hPalette;
255 newdc->totalExtent = dc->totalExtent;
256 newdc->bitsPerPixel = dc->bitsPerPixel;
257 newdc->ROPmode = dc->ROPmode;
258 newdc->polyFillMode = dc->polyFillMode;
259 newdc->stretchBltMode = dc->stretchBltMode;
260 newdc->relAbsMode = dc->relAbsMode;
261 newdc->backgroundMode = dc->backgroundMode;
262 newdc->backgroundColor = dc->backgroundColor;
263 newdc->textColor = dc->textColor;
264 newdc->brushOrgX = dc->brushOrgX;
265 newdc->brushOrgY = dc->brushOrgY;
266 newdc->textAlign = dc->textAlign;
267 newdc->charExtra = dc->charExtra;
268 newdc->breakTotalExtra = dc->breakTotalExtra;
269 newdc->breakCount = dc->breakCount;
270 newdc->breakExtra = dc->breakExtra;
271 newdc->breakRem = dc->breakRem;
272 newdc->MapMode = dc->MapMode;
273 newdc->GraphicsMode = dc->GraphicsMode;
274 #if 0
275 /* Apparently, the DC origin is not changed by [GS]etDCState */
276 newdc->DCOrgX = dc->DCOrgX;
277 newdc->DCOrgY = dc->DCOrgY;
278 #endif
279 newdc->CursPosX = dc->CursPosX;
280 newdc->CursPosY = dc->CursPosY;
281 newdc->ArcDirection = dc->ArcDirection;
282 newdc->xformWorld2Wnd = dc->xformWorld2Wnd;
283 newdc->xformWorld2Vport = dc->xformWorld2Vport;
284 newdc->xformVport2World = dc->xformVport2World;
285 newdc->vport2WorldValid = dc->vport2WorldValid;
286 newdc->wndOrgX = dc->wndOrgX;
287 newdc->wndOrgY = dc->wndOrgY;
288 newdc->wndExtX = dc->wndExtX;
289 newdc->wndExtY = dc->wndExtY;
290 newdc->vportOrgX = dc->vportOrgX;
291 newdc->vportOrgY = dc->vportOrgY;
292 newdc->vportExtX = dc->vportExtX;
293 newdc->vportExtY = dc->vportExtY;
295 newdc->hSelf = (HDC)handle;
296 newdc->saveLevel = 0;
298 PATH_InitGdiPath( &newdc->path );
300 newdc->pAbortProc = NULL;
301 newdc->hookThunk = NULL;
302 newdc->hookProc = 0;
304 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
306 newdc->hGCClipRgn = newdc->hVisRgn = 0;
307 if (dc->hClipRgn)
309 newdc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
310 CombineRgn( newdc->hClipRgn, dc->hClipRgn, 0, RGN_COPY );
312 else
313 newdc->hClipRgn = 0;
314 GDI_ReleaseObj( handle );
315 GDI_ReleaseObj( hdc );
316 return handle;
320 /***********************************************************************
321 * SetDCState (GDI.180)
323 void WINAPI SetDCState16( HDC16 hdc, HDC16 hdcs )
325 DC *dc, *dcs;
327 if (!(dc = GDI_GetObjPtr( hdc, DC_MAGIC ))) return;
328 if (!(dcs = GDI_GetObjPtr( hdcs, DC_MAGIC )))
330 GDI_ReleaseObj( hdc );
331 return;
333 if (!dcs->flags & DC_SAVED)
335 GDI_ReleaseObj( hdc );
336 GDI_ReleaseObj( hdcs );
337 return;
339 TRACE("%04x %04x\n", hdc, hdcs );
341 dc->flags = dcs->flags & ~DC_SAVED;
342 dc->hDevice = dcs->hDevice;
343 dc->totalExtent = dcs->totalExtent;
344 dc->ROPmode = dcs->ROPmode;
345 dc->polyFillMode = dcs->polyFillMode;
346 dc->stretchBltMode = dcs->stretchBltMode;
347 dc->relAbsMode = dcs->relAbsMode;
348 dc->backgroundMode = dcs->backgroundMode;
349 dc->backgroundColor = dcs->backgroundColor;
350 dc->textColor = dcs->textColor;
351 dc->brushOrgX = dcs->brushOrgX;
352 dc->brushOrgY = dcs->brushOrgY;
353 dc->textAlign = dcs->textAlign;
354 dc->charExtra = dcs->charExtra;
355 dc->breakTotalExtra = dcs->breakTotalExtra;
356 dc->breakCount = dcs->breakCount;
357 dc->breakExtra = dcs->breakExtra;
358 dc->breakRem = dcs->breakRem;
359 dc->MapMode = dcs->MapMode;
360 dc->GraphicsMode = dcs->GraphicsMode;
361 #if 0
362 /* Apparently, the DC origin is not changed by [GS]etDCState */
363 dc->DCOrgX = dcs->DCOrgX;
364 dc->DCOrgY = dcs->DCOrgY;
365 #endif
366 dc->CursPosX = dcs->CursPosX;
367 dc->CursPosY = dcs->CursPosY;
368 dc->ArcDirection = dcs->ArcDirection;
369 dc->xformWorld2Wnd = dcs->xformWorld2Wnd;
370 dc->xformWorld2Vport = dcs->xformWorld2Vport;
371 dc->xformVport2World = dcs->xformVport2World;
372 dc->vport2WorldValid = dcs->vport2WorldValid;
374 dc->wndOrgX = dcs->wndOrgX;
375 dc->wndOrgY = dcs->wndOrgY;
376 dc->wndExtX = dcs->wndExtX;
377 dc->wndExtY = dcs->wndExtY;
378 dc->vportOrgX = dcs->vportOrgX;
379 dc->vportOrgY = dcs->vportOrgY;
380 dc->vportExtX = dcs->vportExtX;
381 dc->vportExtY = dcs->vportExtY;
383 if (!(dc->flags & DC_MEMORY)) dc->bitsPerPixel = dcs->bitsPerPixel;
385 if (dcs->hClipRgn)
387 if (!dc->hClipRgn) dc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
388 CombineRgn( dc->hClipRgn, dcs->hClipRgn, 0, RGN_COPY );
390 else
392 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
393 dc->hClipRgn = 0;
395 CLIPPING_UpdateGCRegion( dc );
397 SelectObject( hdc, dcs->hBitmap );
398 SelectObject( hdc, dcs->hBrush );
399 SelectObject( hdc, dcs->hFont );
400 SelectObject( hdc, dcs->hPen );
401 SetBkColor( hdc, dcs->backgroundColor);
402 SetTextColor( hdc, dcs->textColor);
403 GDISelectPalette16( hdc, dcs->hPalette, FALSE );
404 GDI_ReleaseObj( hdcs );
405 GDI_ReleaseObj( hdc );
409 /***********************************************************************
410 * SaveDC (GDI.30)
412 INT16 WINAPI SaveDC16( HDC16 hdc )
414 return (INT16)SaveDC( hdc );
418 /***********************************************************************
419 * SaveDC (GDI32.@)
421 INT WINAPI SaveDC( HDC hdc )
423 HDC hdcs;
424 DC * dc, * dcs;
425 INT ret;
427 dc = DC_GetDCUpdate( hdc );
428 if (!dc) return 0;
430 if(dc->funcs->pSaveDC)
432 ret = dc->funcs->pSaveDC( dc );
433 GDI_ReleaseObj( hdc );
434 return ret;
437 if (!(hdcs = GetDCState16( hdc )))
439 GDI_ReleaseObj( hdc );
440 return 0;
442 dcs = GDI_GetObjPtr( hdcs, DC_MAGIC );
444 /* Copy path. The reason why path saving / restoring is in SaveDC/
445 * RestoreDC and not in GetDCState/SetDCState is that the ...DCState
446 * functions are only in Win16 (which doesn't have paths) and that
447 * SetDCState doesn't allow us to signal an error (which can happen
448 * when copying paths).
450 if (!PATH_AssignGdiPath( &dcs->path, &dc->path ))
452 GDI_ReleaseObj( hdc );
453 GDI_ReleaseObj( hdcs );
454 DeleteDC( hdcs );
455 return 0;
458 dcs->header.hNext = dc->header.hNext;
459 dc->header.hNext = hdcs;
460 TRACE("(%04x): returning %d\n", hdc, dc->saveLevel+1 );
461 ret = ++dc->saveLevel;
462 GDI_ReleaseObj( hdcs );
463 GDI_ReleaseObj( hdc );
464 return ret;
468 /***********************************************************************
469 * RestoreDC (GDI.39)
471 BOOL16 WINAPI RestoreDC16( HDC16 hdc, INT16 level )
473 return RestoreDC( hdc, level );
477 /***********************************************************************
478 * RestoreDC (GDI32.@)
480 BOOL WINAPI RestoreDC( HDC hdc, INT level )
482 DC * dc, * dcs;
483 BOOL success;
485 TRACE("%04x %d\n", hdc, level );
486 dc = DC_GetDCPtr( hdc );
487 if(!dc) return FALSE;
488 if(dc->funcs->pRestoreDC)
490 success = dc->funcs->pRestoreDC( dc, level );
491 GDI_ReleaseObj( hdc );
492 return success;
495 if (level == -1) level = dc->saveLevel;
496 if ((level < 1)
497 /* This pair of checks disagrees with MSDN "Platform SDK:
498 Windows GDI" July 2000 which says all negative values
499 for level will be interpreted as an instance relative
500 to the current state. Restricting it to just -1 does
501 not satisfy this */
502 || (level > dc->saveLevel))
504 GDI_ReleaseObj( hdc );
505 return FALSE;
508 success=TRUE;
509 while (dc->saveLevel >= level)
511 HDC16 hdcs = dc->header.hNext;
512 if (!(dcs = GDI_GetObjPtr( hdcs, DC_MAGIC )))
514 GDI_ReleaseObj( hdc );
515 return FALSE;
517 dc->header.hNext = dcs->header.hNext;
518 if (--dc->saveLevel < level)
520 SetDCState16( hdc, hdcs );
521 if (!PATH_AssignGdiPath( &dc->path, &dcs->path ))
522 /* FIXME: This might not be quite right, since we're
523 * returning FALSE but still destroying the saved DC state */
524 success=FALSE;
526 GDI_ReleaseObj( hdcs );
527 DeleteDC( hdcs );
529 GDI_ReleaseObj( hdc );
530 return success;
534 /***********************************************************************
535 * CreateDC (GDI.53)
537 HDC16 WINAPI CreateDC16( LPCSTR driver, LPCSTR device, LPCSTR output,
538 const DEVMODEA *initData )
540 return CreateDCA( driver, device, output, initData );
543 /***********************************************************************
544 * CreateDCA (GDI32.@)
546 HDC WINAPI CreateDCA( LPCSTR driver, LPCSTR device, LPCSTR output,
547 const DEVMODEA *initData )
549 HDC hdc;
550 DC * dc;
551 const DC_FUNCTIONS *funcs;
552 char buf[300];
554 if (!device || !DRIVER_GetDriverName( device, buf, sizeof(buf) ))
555 strcpy(buf, driver);
557 if (!(funcs = DRIVER_load_driver( buf )))
559 ERR( "no driver found for %s\n", buf );
560 return 0;
562 if (!(dc = DC_AllocDC( funcs )))
564 DRIVER_release_driver( funcs );
565 return 0;
568 dc->flags = 0;
570 TRACE("(driver=%s, device=%s, output=%s): returning %04x\n",
571 debugstr_a(driver), debugstr_a(device), debugstr_a(output), dc->hSelf );
573 if (dc->funcs->pCreateDC &&
574 !dc->funcs->pCreateDC( dc, buf, device, output, initData ))
576 WARN("creation aborted by device\n" );
577 GDI_FreeObject( dc->hSelf, dc );
578 DRIVER_release_driver( funcs );
579 return 0;
582 DC_InitDC( dc );
583 hdc = dc->hSelf;
584 GDI_ReleaseObj( hdc );
585 return hdc;
589 /***********************************************************************
590 * CreateDCW (GDI32.@)
592 HDC WINAPI CreateDCW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
593 const DEVMODEW *initData )
595 LPSTR driverA = HEAP_strdupWtoA( GetProcessHeap(), 0, driver );
596 LPSTR deviceA = HEAP_strdupWtoA( GetProcessHeap(), 0, device );
597 LPSTR outputA = HEAP_strdupWtoA( GetProcessHeap(), 0, output );
598 HDC res = CreateDCA( driverA, deviceA, outputA,
599 (const DEVMODEA *)initData /*FIXME*/ );
600 HeapFree( GetProcessHeap(), 0, driverA );
601 HeapFree( GetProcessHeap(), 0, deviceA );
602 HeapFree( GetProcessHeap(), 0, outputA );
603 return res;
607 /***********************************************************************
608 * CreateIC (GDI.153)
610 HDC16 WINAPI CreateIC16( LPCSTR driver, LPCSTR device, LPCSTR output,
611 const DEVMODEA* initData )
613 /* Nothing special yet for ICs */
614 return CreateDC16( driver, device, output, initData );
618 /***********************************************************************
619 * CreateICA (GDI32.@)
621 HDC WINAPI CreateICA( LPCSTR driver, LPCSTR device, LPCSTR output,
622 const DEVMODEA* initData )
624 /* Nothing special yet for ICs */
625 return CreateDCA( driver, device, output, initData );
629 /***********************************************************************
630 * CreateICW (GDI32.@)
632 HDC WINAPI CreateICW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
633 const DEVMODEW* initData )
635 /* Nothing special yet for ICs */
636 return CreateDCW( driver, device, output, initData );
640 /***********************************************************************
641 * CreateCompatibleDC (GDI.52)
643 HDC16 WINAPI CreateCompatibleDC16( HDC16 hdc )
645 return (HDC16)CreateCompatibleDC( hdc );
649 /***********************************************************************
650 * CreateCompatibleDC (GDI32.@)
652 HDC WINAPI CreateCompatibleDC( HDC hdc )
654 DC *dc, *origDC;
655 const DC_FUNCTIONS *funcs;
657 if ((origDC = GDI_GetObjPtr( hdc, DC_MAGIC ))) funcs = DRIVER_get_driver(origDC->funcs);
658 else funcs = DRIVER_load_driver( "DISPLAY" );
660 if (!funcs)
662 if (origDC) GDI_ReleaseObj( hdc );
663 return 0;
666 if (!(dc = DC_AllocDC( funcs )))
668 DRIVER_release_driver( funcs );
669 if (origDC) GDI_ReleaseObj( hdc );
670 return 0;
674 TRACE("(%04x): returning %04x\n",
675 hdc, dc->hSelf );
677 dc->flags = DC_MEMORY;
678 dc->bitsPerPixel = 1;
679 dc->hBitmap = hPseudoStockBitmap;
681 /* Copy the driver-specific physical device info into
682 * the new DC. The driver may use this read-only info
683 * while creating the compatible DC below. */
684 if (origDC)
685 dc->physDev = origDC->physDev;
687 if (dc->funcs->pCreateDC &&
688 !dc->funcs->pCreateDC( dc, NULL, NULL, NULL, NULL ))
690 WARN("creation aborted by device\n");
691 GDI_FreeObject( dc->hSelf, dc );
692 DRIVER_release_driver( funcs );
693 if (origDC) GDI_ReleaseObj( hdc );
694 return 0;
697 DC_InitDC( dc );
698 GDI_ReleaseObj( dc->hSelf );
699 if (origDC) GDI_ReleaseObj( hdc );
700 return dc->hSelf;
704 /***********************************************************************
705 * DeleteDC (GDI.68)
707 BOOL16 WINAPI DeleteDC16( HDC16 hdc )
709 return DeleteDC( hdc );
713 /***********************************************************************
714 * DeleteDC (GDI32.@)
716 BOOL WINAPI DeleteDC( HDC hdc )
718 DC * dc = GDI_GetObjPtr( hdc, DC_MAGIC );
719 if (!dc) return FALSE;
721 TRACE("%04x\n", hdc );
723 /* Call hook procedure to check whether is it OK to delete this DC */
724 if (dc->hookThunk && !(dc->flags & (DC_SAVED | DC_MEMORY)))
726 DCHOOKPROC proc = dc->hookThunk;
727 if (proc)
729 DWORD data = dc->dwHookData;
730 GDI_ReleaseObj( hdc );
731 if (!proc( hdc, DCHC_DELETEDC, data, 0 )) return FALSE;
732 if (!(dc = DC_GetDCPtr( hdc ))) return FALSE;
736 while (dc->saveLevel)
738 DC * dcs;
739 HDC16 hdcs = dc->header.hNext;
740 if (!(dcs = GDI_GetObjPtr( hdcs, DC_MAGIC ))) break;
741 dc->header.hNext = dcs->header.hNext;
742 dc->saveLevel--;
743 GDI_ReleaseObj( hdcs );
744 DeleteDC( hdcs );
747 if (!(dc->flags & DC_SAVED))
749 SelectObject( hdc, GetStockObject(BLACK_PEN) );
750 SelectObject( hdc, GetStockObject(WHITE_BRUSH) );
751 SelectObject( hdc, GetStockObject(SYSTEM_FONT) );
752 if (dc->funcs->pDeleteDC) dc->funcs->pDeleteDC(dc);
753 DRIVER_release_driver( dc->funcs );
756 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
757 if (dc->hVisRgn) DeleteObject( dc->hVisRgn );
758 if (dc->hGCClipRgn) DeleteObject( dc->hGCClipRgn );
759 if (dc->pAbortProc) THUNK_Free( (FARPROC)dc->pAbortProc );
760 if (dc->hookThunk) THUNK_Free( (FARPROC)dc->hookThunk );
761 PATH_DestroyGdiPath(&dc->path);
763 return GDI_FreeObject( hdc, dc );
767 /***********************************************************************
768 * ResetDC (GDI.376)
770 HDC16 WINAPI ResetDC16( HDC16 hdc, const DEVMODEA *devmode )
772 FIXME("stub\n" );
773 return hdc;
777 /***********************************************************************
778 * ResetDCA (GDI32.@)
780 HDC WINAPI ResetDCA( HDC hdc, const DEVMODEA *devmode )
782 FIXME("stub\n" );
783 return hdc;
787 /***********************************************************************
788 * ResetDCW (GDI32.@)
790 HDC WINAPI ResetDCW( HDC hdc, const DEVMODEW *devmode )
792 FIXME("stub\n" );
793 return hdc;
797 /***********************************************************************
798 * GetDeviceCaps (GDI.80)
800 INT16 WINAPI GetDeviceCaps16( HDC16 hdc, INT16 cap )
802 INT16 ret = GetDeviceCaps( hdc, cap );
803 /* some apps don't expect -1 and think it's a B&W screen */
804 if ((cap == NUMCOLORS) && (ret == -1)) ret = 2048;
805 return ret;
809 /***********************************************************************
810 * GetDeviceCaps (GDI32.@)
812 INT WINAPI GetDeviceCaps( HDC hdc, INT cap )
814 DC *dc;
815 INT ret = 0;
817 if ((dc = DC_GetDCPtr( hdc )))
819 if (dc->funcs->pGetDeviceCaps) ret = dc->funcs->pGetDeviceCaps( dc, cap );
820 GDI_ReleaseObj( hdc );
822 return ret;
826 /***********************************************************************
827 * SetBkColor (GDI.1)
829 COLORREF WINAPI SetBkColor16( HDC16 hdc, COLORREF color )
831 return SetBkColor( hdc, color );
835 /***********************************************************************
836 * SetBkColor (GDI32.@)
838 COLORREF WINAPI SetBkColor( HDC hdc, COLORREF color )
840 COLORREF oldColor;
841 DC * dc = DC_GetDCPtr( hdc );
843 if (!dc) return 0x80000000;
844 if (dc->funcs->pSetBkColor)
845 oldColor = dc->funcs->pSetBkColor(dc, color);
846 else {
847 oldColor = dc->backgroundColor;
848 dc->backgroundColor = color;
850 GDI_ReleaseObj( hdc );
851 return oldColor;
855 /***********************************************************************
856 * SetTextColor (GDI.9)
858 COLORREF WINAPI SetTextColor16( HDC16 hdc, COLORREF color )
860 return SetTextColor( hdc, color );
864 /***********************************************************************
865 * SetTextColor (GDI32.@)
867 COLORREF WINAPI SetTextColor( HDC hdc, COLORREF color )
869 COLORREF oldColor;
870 DC * dc = DC_GetDCPtr( hdc );
872 if (!dc) return 0x80000000;
873 if (dc->funcs->pSetTextColor)
874 oldColor = dc->funcs->pSetTextColor(dc, color);
875 else {
876 oldColor = dc->textColor;
877 dc->textColor = color;
879 GDI_ReleaseObj( hdc );
880 return oldColor;
883 /***********************************************************************
884 * SetTextAlign (GDI.346)
886 UINT16 WINAPI SetTextAlign16( HDC16 hdc, UINT16 align )
888 return SetTextAlign( hdc, align );
892 /***********************************************************************
893 * SetTextAlign (GDI32.@)
895 UINT WINAPI SetTextAlign( HDC hdc, UINT align )
897 UINT prevAlign;
898 DC *dc = DC_GetDCPtr( hdc );
899 if (!dc) return 0x0;
900 if (dc->funcs->pSetTextAlign)
901 prevAlign = dc->funcs->pSetTextAlign(dc, align);
902 else {
903 prevAlign = dc->textAlign;
904 dc->textAlign = align;
906 GDI_ReleaseObj( hdc );
907 return prevAlign;
910 /***********************************************************************
911 * GetDCOrgEx (GDI32.@)
913 BOOL WINAPI GetDCOrgEx( HDC hDC, LPPOINT lpp )
915 DC * dc;
917 if (!lpp) return FALSE;
918 if (!(dc = DC_GetDCPtr( hDC ))) return FALSE;
920 lpp->x = lpp->y = 0;
921 if (dc->funcs->pGetDCOrgEx) dc->funcs->pGetDCOrgEx( dc, lpp );
922 lpp->x += dc->DCOrgX;
923 lpp->y += dc->DCOrgY;
924 GDI_ReleaseObj( hDC );
925 return TRUE;
929 /***********************************************************************
930 * GetDCOrg (GDI.79)
932 DWORD WINAPI GetDCOrg16( HDC16 hdc )
934 POINT pt;
935 if( GetDCOrgEx( hdc, &pt) )
936 return MAKELONG( (WORD)pt.x, (WORD)pt.y );
937 return 0;
941 /***********************************************************************
942 * SetDCOrg (GDI.117)
944 DWORD WINAPI SetDCOrg16( HDC16 hdc, INT16 x, INT16 y )
946 DWORD prevOrg;
947 DC *dc = DC_GetDCPtr( hdc );
948 if (!dc) return 0;
949 prevOrg = dc->DCOrgX | (dc->DCOrgY << 16);
950 dc->DCOrgX = x;
951 dc->DCOrgY = y;
952 GDI_ReleaseObj( hdc );
953 return prevOrg;
957 /***********************************************************************
958 * SetGraphicsMode (GDI32.@)
960 INT WINAPI SetGraphicsMode( HDC hdc, INT mode )
962 INT ret = 0;
963 DC *dc = DC_GetDCPtr( hdc );
965 /* One would think that setting the graphics mode to GM_COMPATIBLE
966 * would also reset the world transformation matrix to the unity
967 * matrix. However, in Windows, this is not the case. This doesn't
968 * make a lot of sense to me, but that's the way it is.
970 if (!dc) return 0;
971 if ((mode > 0) || (mode <= GM_LAST))
973 ret = dc->GraphicsMode;
974 dc->GraphicsMode = mode;
976 GDI_ReleaseObj( hdc );
977 return ret;
981 /***********************************************************************
982 * SetArcDirection (GDI.525)
984 INT16 WINAPI SetArcDirection16( HDC16 hdc, INT16 nDirection )
986 return SetArcDirection( (HDC)hdc, (INT)nDirection );
990 /***********************************************************************
991 * SetArcDirection (GDI32.@)
993 INT WINAPI SetArcDirection( HDC hdc, INT nDirection )
995 DC * dc;
996 INT nOldDirection = 0;
998 if (nDirection!=AD_COUNTERCLOCKWISE && nDirection!=AD_CLOCKWISE)
1000 SetLastError(ERROR_INVALID_PARAMETER);
1001 return 0;
1004 if ((dc = DC_GetDCPtr( hdc )))
1006 nOldDirection = dc->ArcDirection;
1007 dc->ArcDirection = nDirection;
1008 GDI_ReleaseObj( hdc );
1010 return nOldDirection;
1014 /***********************************************************************
1015 * GetWorldTransform (GDI32.@)
1017 BOOL WINAPI GetWorldTransform( HDC hdc, LPXFORM xform )
1019 DC * dc;
1020 if (!xform) return FALSE;
1021 if (!(dc = DC_GetDCPtr( hdc ))) return FALSE;
1022 *xform = dc->xformWorld2Wnd;
1023 GDI_ReleaseObj( hdc );
1024 return TRUE;
1028 /***********************************************************************
1029 * SetWorldTransform (GDI32.@)
1031 BOOL WINAPI SetWorldTransform( HDC hdc, const XFORM *xform )
1033 BOOL ret = FALSE;
1034 DC *dc = DC_GetDCPtr( hdc );
1036 if (!dc) return FALSE;
1037 if (!xform) goto done;
1039 /* Check that graphics mode is GM_ADVANCED */
1040 if (dc->GraphicsMode!=GM_ADVANCED) goto done;
1042 dc->xformWorld2Wnd = *xform;
1043 DC_UpdateXforms( dc );
1044 ret = TRUE;
1045 done:
1046 GDI_ReleaseObj( hdc );
1047 return ret;
1051 /****************************************************************************
1052 * ModifyWorldTransform [GDI32.@]
1053 * Modifies the world transformation for a device context.
1055 * PARAMS
1056 * hdc [I] Handle to device context
1057 * xform [I] XFORM structure that will be used to modify the world
1058 * transformation
1059 * iMode [I] Specifies in what way to modify the world transformation
1060 * Possible values:
1061 * MWT_IDENTITY
1062 * Resets the world transformation to the identity matrix.
1063 * The parameter xform is ignored.
1064 * MWT_LEFTMULTIPLY
1065 * Multiplies xform into the world transformation matrix from
1066 * the left.
1067 * MWT_RIGHTMULTIPLY
1068 * Multiplies xform into the world transformation matrix from
1069 * the right.
1071 * RETURNS STD
1073 BOOL WINAPI ModifyWorldTransform( HDC hdc, const XFORM *xform,
1074 DWORD iMode )
1076 BOOL ret = FALSE;
1077 DC *dc = DC_GetDCPtr( hdc );
1079 /* Check for illegal parameters */
1080 if (!dc) return FALSE;
1081 if (!xform) goto done;
1083 /* Check that graphics mode is GM_ADVANCED */
1084 if (dc->GraphicsMode!=GM_ADVANCED) goto done;
1086 switch (iMode)
1088 case MWT_IDENTITY:
1089 dc->xformWorld2Wnd.eM11 = 1.0f;
1090 dc->xformWorld2Wnd.eM12 = 0.0f;
1091 dc->xformWorld2Wnd.eM21 = 0.0f;
1092 dc->xformWorld2Wnd.eM22 = 1.0f;
1093 dc->xformWorld2Wnd.eDx = 0.0f;
1094 dc->xformWorld2Wnd.eDy = 0.0f;
1095 break;
1096 case MWT_LEFTMULTIPLY:
1097 CombineTransform( &dc->xformWorld2Wnd, xform,
1098 &dc->xformWorld2Wnd );
1099 break;
1100 case MWT_RIGHTMULTIPLY:
1101 CombineTransform( &dc->xformWorld2Wnd, &dc->xformWorld2Wnd,
1102 xform );
1103 break;
1104 default:
1105 goto done;
1108 DC_UpdateXforms( dc );
1109 ret = TRUE;
1110 done:
1111 GDI_ReleaseObj( hdc );
1112 return ret;
1116 /****************************************************************************
1117 * CombineTransform [GDI32.@]
1118 * Combines two transformation matrices.
1120 * PARAMS
1121 * xformResult [O] Stores the result of combining the two matrices
1122 * xform1 [I] Specifies the first matrix to apply
1123 * xform2 [I] Specifies the second matrix to apply
1125 * REMARKS
1126 * The same matrix can be passed in for more than one of the parameters.
1128 * RETURNS STD
1130 BOOL WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
1131 const XFORM *xform2 )
1133 XFORM xformTemp;
1135 /* Check for illegal parameters */
1136 if (!xformResult || !xform1 || !xform2)
1137 return FALSE;
1139 /* Create the result in a temporary XFORM, since xformResult may be
1140 * equal to xform1 or xform2 */
1141 xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
1142 xform1->eM12 * xform2->eM21;
1143 xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
1144 xform1->eM12 * xform2->eM22;
1145 xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
1146 xform1->eM22 * xform2->eM21;
1147 xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
1148 xform1->eM22 * xform2->eM22;
1149 xformTemp.eDx = xform1->eDx * xform2->eM11 +
1150 xform1->eDy * xform2->eM21 +
1151 xform2->eDx;
1152 xformTemp.eDy = xform1->eDx * xform2->eM12 +
1153 xform1->eDy * xform2->eM22 +
1154 xform2->eDy;
1156 /* Copy the result to xformResult */
1157 *xformResult = xformTemp;
1159 return TRUE;
1163 /***********************************************************************
1164 * SetDCHook (GDI.190)
1167 /* ### start build ### */
1168 extern WORD CALLBACK GDI_CallTo16_word_wwll(FARPROC16,WORD,WORD,LONG,LONG);
1169 /* ### stop build ### */
1171 /**********************************************************************/
1173 BOOL16 WINAPI SetDCHook( HDC16 hdc, FARPROC16 hookProc, DWORD dwHookData )
1175 DC *dc = DC_GetDCPtr( hdc );
1176 if (!dc) return FALSE;
1179 * Note: We store the original SEGPTR 'hookProc' as we need to be
1180 * able to return it verbatim in GetDCHook,
1182 * On the other hand, we still call THUNK_Alloc and store the
1183 * 32-bit thunk into another DC member, because THUNK_Alloc
1184 * recognizes the (typical) case that the 'hookProc' is indeed
1185 * the 16-bit API entry point of a built-in routine (e.g. DCHook16)
1187 * We could perform that test every time the hook is about to
1188 * be called (or else we could live with the 32->16->32 detour),
1189 * but this way is the most efficient ...
1192 dc->hookProc = hookProc;
1193 dc->dwHookData = dwHookData;
1195 THUNK_Free( (FARPROC)dc->hookThunk );
1196 dc->hookThunk = (DCHOOKPROC)
1197 THUNK_Alloc( hookProc, (RELAY)GDI_CallTo16_word_wwll );
1199 GDI_ReleaseObj( hdc );
1200 return TRUE;
1204 /***********************************************************************
1205 * GetDCHook (GDI.191)
1207 DWORD WINAPI GetDCHook( HDC16 hdc, FARPROC16 *phookProc )
1209 DC *dc = DC_GetDCPtr( hdc );
1210 if (!dc) return 0;
1211 *phookProc = dc->hookProc;
1212 GDI_ReleaseObj( hdc );
1213 return dc->dwHookData;
1217 /***********************************************************************
1218 * SetHookFlags (GDI.192)
1220 WORD WINAPI SetHookFlags16(HDC16 hDC, WORD flags)
1222 DC *dc = DC_GetDCPtr( hDC );
1224 if( dc )
1226 WORD wRet = dc->flags & DC_DIRTY;
1228 /* "Undocumented Windows" info is slightly confusing.
1231 TRACE("hDC %04x, flags %04x\n",hDC,flags);
1233 if( flags & DCHF_INVALIDATEVISRGN )
1234 dc->flags |= DC_DIRTY;
1235 else if( flags & DCHF_VALIDATEVISRGN || !flags )
1236 dc->flags &= ~DC_DIRTY;
1237 GDI_ReleaseObj( hDC );
1238 return wRet;
1240 return 0;
1243 /***********************************************************************
1244 * SetICMMode (GDI32.@)
1246 INT WINAPI SetICMMode(HDC hdc, INT iEnableICM)
1248 /*FIXME Asuming that ICM is always off, and cannot be turned on */
1249 if (iEnableICM == ICM_OFF) return ICM_OFF;
1250 if (iEnableICM == ICM_ON) return 0;
1251 if (iEnableICM == ICM_QUERY) return ICM_OFF;
1252 return 0;
1255 /***********************************************************************
1256 * GetDeviceGammaRamp (GDI32.@)
1258 BOOL WINAPI GetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1260 BOOL ret = FALSE;
1261 DC *dc = DC_GetDCPtr( hDC );
1263 if( dc )
1265 if (dc->funcs->pGetDeviceGammaRamp)
1266 ret = dc->funcs->pGetDeviceGammaRamp(dc, ptr);
1267 GDI_ReleaseObj( hDC );
1269 return ret;
1272 /***********************************************************************
1273 * SetDeviceGammaRamp (GDI32.@)
1275 BOOL WINAPI SetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1277 BOOL ret = FALSE;
1278 DC *dc = DC_GetDCPtr( hDC );
1280 if( dc )
1282 if (dc->funcs->pSetDeviceGammaRamp)
1283 ret = dc->funcs->pSetDeviceGammaRamp(dc, ptr);
1284 GDI_ReleaseObj( hDC );
1286 return ret;
1289 /***********************************************************************
1290 * GetColorSpace (GDI32.@)
1292 HCOLORSPACE WINAPI GetColorSpace(HDC hdc)
1294 /*FIXME Need to to whatever GetColorSpace actually does */
1295 return 0;
1298 /***********************************************************************
1299 * CreateColorSpaceA (GDI32.@)
1301 HCOLORSPACE WINAPI CreateColorSpaceA( LPLOGCOLORSPACEA lpLogColorSpace )
1303 FIXME( "stub\n" );
1304 return 0;
1307 /***********************************************************************
1308 * CreateColorSpaceW (GDI32.@)
1310 HCOLORSPACE WINAPI CreateColorSpaceW( LPLOGCOLORSPACEW lpLogColorSpace )
1312 FIXME( "stub\n" );
1313 return 0;
1316 /***********************************************************************
1317 * DeleteColorSpace (GDI32.@)
1319 BOOL WINAPI DeleteColorSpace( HCOLORSPACE hColorSpace )
1321 FIXME( "stub\n" );
1323 return TRUE;
1326 /***********************************************************************
1327 * SetColorSpace (GDI32.@)
1329 HCOLORSPACE WINAPI SetColorSpace( HDC hDC, HCOLORSPACE hColorSpace )
1331 FIXME( "stub\n" );
1333 return hColorSpace;
1336 /***********************************************************************
1337 * GetBoundsRect (GDI.194)
1339 UINT16 WINAPI GetBoundsRect16(HDC16 hdc, LPRECT16 rect, UINT16 flags)
1341 return DCB_RESET | DCB_DISABLE; /* bounding rectangle always empty and disabled*/
1344 /***********************************************************************
1345 * GetBoundsRect (GDI32.@)
1347 UINT WINAPI GetBoundsRect(HDC hdc, LPRECT rect, UINT flags)
1349 FIXME("(): stub\n");
1350 return DCB_RESET; /* bounding rectangle always empty */
1353 /***********************************************************************
1354 * SetBoundsRect (GDI.193)
1356 UINT16 WINAPI SetBoundsRect16(HDC16 hdc, const RECT16* rect, UINT16 flags)
1358 if ( (flags & DCB_ACCUMULATE) || (flags & DCB_ENABLE) )
1359 FIXME("(%04x, %p, %04x): stub\n", hdc, rect, flags );
1361 return DCB_RESET | DCB_DISABLE; /* bounding rectangle always empty and disabled*/
1364 /***********************************************************************
1365 * SetBoundsRect (GDI32.@)
1367 UINT WINAPI SetBoundsRect(HDC hdc, const RECT* rect, UINT flags)
1369 FIXME("(): stub\n");
1370 return DCB_DISABLE; /* bounding rectangle always empty */
1374 /***********************************************************************
1375 * GetRelAbs (GDI32.@)
1377 INT WINAPI GetRelAbs( HDC hdc, DWORD dwIgnore )
1379 INT ret = 0;
1380 DC *dc = DC_GetDCPtr( hdc );
1381 if (dc) ret = dc->relAbsMode;
1382 GDI_ReleaseObj( hdc );
1383 return ret;
1386 /***********************************************************************
1387 * Death (GDI.121)
1389 * Disables GDI, switches back to text mode.
1390 * We don't have to do anything here,
1391 * just let console support handle everything
1393 void WINAPI Death16(HDC16 hDC)
1395 MESSAGE("Death(%04x) called. Application enters text mode...\n", hDC);
1398 /***********************************************************************
1399 * Resurrection (GDI.122)
1401 * Restores GDI functionality
1403 void WINAPI Resurrection16(HDC16 hDC,
1404 WORD w1, WORD w2, WORD w3, WORD w4, WORD w5, WORD w6)
1406 MESSAGE("Resurrection(%04x, %04x, %04x, %04x, %04x, %04x, %04x) called. Application left text mode.\n", hDC, w1, w2, w3, w4, w5, w6);
1409 /***********************************************************************
1410 * GetLayout (GDI32.@)
1412 * Gets left->right or right->left text layout flags of a dc.
1413 * win98 just returns 0 and sets ERROR_CALL_NOT_IMPLEMENTED so we do the same
1416 DWORD WINAPI GetLayout(HDC hdc)
1418 FIXME("(%08x): stub\n", hdc);
1419 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1420 return 0;
1423 /***********************************************************************
1424 * SetLayout (GDI32.@)
1426 * Sets left->right or right->left text layout flags of a dc.
1427 * win98 just returns 0 and sets ERROR_CALL_NOT_IMPLEMENTED so we do the same
1430 DWORD WINAPI SetLayout(HDC hdc, DWORD layout)
1432 FIXME("(%08x,%08lx): stub\n", hdc, layout);
1433 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1434 return 0;