Added support for loading a Winelib app linked as a .so from the wine
[wine/multimedia.git] / objects / dc.c
blob1f0ab34ab48908de1fd1380cd3466855f4f43d72
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->devCaps = NULL;
52 dc->hClipRgn = 0;
53 dc->hVisRgn = 0;
54 dc->hGCClipRgn = 0;
55 dc->hPen = GetStockObject( BLACK_PEN );
56 dc->hBrush = GetStockObject( WHITE_BRUSH );
57 dc->hFont = GetStockObject( SYSTEM_FONT );
58 dc->hBitmap = 0;
59 dc->hDevice = 0;
60 dc->hPalette = GetStockObject( DEFAULT_PALETTE );
61 dc->ROPmode = R2_COPYPEN;
62 dc->polyFillMode = ALTERNATE;
63 dc->stretchBltMode = BLACKONWHITE;
64 dc->relAbsMode = ABSOLUTE;
65 dc->backgroundMode = OPAQUE;
66 dc->backgroundColor = RGB( 255, 255, 255 );
67 dc->textColor = RGB( 0, 0, 0 );
68 dc->brushOrgX = 0;
69 dc->brushOrgY = 0;
70 dc->textAlign = TA_LEFT | TA_TOP | TA_NOUPDATECP;
71 dc->charExtra = 0;
72 dc->breakTotalExtra = 0;
73 dc->breakCount = 0;
74 dc->breakExtra = 0;
75 dc->breakRem = 0;
76 dc->totalExtent.left = 0;
77 dc->totalExtent.top = 0;
78 dc->totalExtent.right = 0;
79 dc->totalExtent.bottom = 0;
80 dc->bitsPerPixel = 1;
81 dc->MapMode = MM_TEXT;
82 dc->GraphicsMode = GM_COMPATIBLE;
83 dc->DCOrgX = 0;
84 dc->DCOrgY = 0;
85 dc->pAbortProc = NULL;
86 dc->CursPosX = 0;
87 dc->CursPosY = 0;
88 dc->ArcDirection = AD_COUNTERCLOCKWISE;
89 dc->xformWorld2Wnd.eM11 = 1.0f;
90 dc->xformWorld2Wnd.eM12 = 0.0f;
91 dc->xformWorld2Wnd.eM21 = 0.0f;
92 dc->xformWorld2Wnd.eM22 = 1.0f;
93 dc->xformWorld2Wnd.eDx = 0.0f;
94 dc->xformWorld2Wnd.eDy = 0.0f;
95 dc->xformWorld2Vport = dc->xformWorld2Wnd;
96 dc->xformVport2World = dc->xformWorld2Wnd;
97 dc->vport2WorldValid = TRUE;
98 PATH_InitGdiPath(&dc->path);
99 return dc;
104 /***********************************************************************
105 * DC_GetDCPtr
107 DC *DC_GetDCPtr( HDC hdc )
109 GDIOBJHDR *ptr = GDI_GetObjPtr( hdc, MAGIC_DONTCARE );
110 if (!ptr) return NULL;
111 if ((GDIMAGIC(ptr->wMagic) == DC_MAGIC) ||
112 (GDIMAGIC(ptr->wMagic) == METAFILE_DC_MAGIC) ||
113 (GDIMAGIC(ptr->wMagic) == ENHMETAFILE_DC_MAGIC))
114 return (DC *)ptr;
115 GDI_ReleaseObj( hdc );
116 SetLastError( ERROR_INVALID_HANDLE );
117 return NULL;
120 /***********************************************************************
121 * DC_GetDCUpdate
123 * Retrieve a DC ptr while making sure the visRgn is updated.
124 * This function may call up to USER so the GDI lock should _not_
125 * be held when calling it.
127 DC *DC_GetDCUpdate( HDC hdc )
129 DC *dc = DC_GetDCPtr( hdc );
130 if (!dc) return NULL;
131 while (dc->flags & DC_DIRTY)
133 dc->flags &= ~DC_DIRTY;
134 if (!(dc->flags & (DC_SAVED | DC_MEMORY)))
136 DCHOOKPROC proc = dc->hookThunk;
137 if (proc)
139 DWORD data = dc->dwHookData;
140 GDI_ReleaseObj( hdc );
141 proc( hdc, DCHC_INVALIDVISRGN, data, 0 );
142 if (!(dc = DC_GetDCPtr( hdc ))) break;
143 /* otherwise restart the loop in case it became dirty again in the meantime */
147 return dc;
150 /***********************************************************************
151 * DC_InitDC
153 * Setup device-specific DC values for a newly created DC.
155 void DC_InitDC( DC* dc )
157 RealizeDefaultPalette16( dc->hSelf );
158 SetTextColor( dc->hSelf, dc->textColor );
159 SetBkColor( dc->hSelf, dc->backgroundColor );
160 SelectObject( dc->hSelf, dc->hPen );
161 SelectObject( dc->hSelf, dc->hBrush );
162 SelectObject( dc->hSelf, dc->hFont );
163 CLIPPING_UpdateGCRegion( dc );
167 /***********************************************************************
168 * DC_InvertXform
170 * Computes the inverse of the transformation xformSrc and stores it to
171 * xformDest. Returns TRUE if successful or FALSE if the xformSrc matrix
172 * is singular.
174 static BOOL DC_InvertXform( const XFORM *xformSrc, XFORM *xformDest )
176 FLOAT determinant;
178 determinant = xformSrc->eM11*xformSrc->eM22 -
179 xformSrc->eM12*xformSrc->eM21;
180 if (determinant > -1e-12 && determinant < 1e-12)
181 return FALSE;
183 xformDest->eM11 = xformSrc->eM22 / determinant;
184 xformDest->eM12 = -xformSrc->eM12 / determinant;
185 xformDest->eM21 = -xformSrc->eM21 / determinant;
186 xformDest->eM22 = xformSrc->eM11 / determinant;
187 xformDest->eDx = -xformSrc->eDx * xformDest->eM11 -
188 xformSrc->eDy * xformDest->eM21;
189 xformDest->eDy = -xformSrc->eDx * xformDest->eM12 -
190 xformSrc->eDy * xformDest->eM22;
192 return TRUE;
196 /***********************************************************************
197 * DC_UpdateXforms
199 * Updates the xformWorld2Vport, xformVport2World and vport2WorldValid
200 * fields of the specified DC by creating a transformation that
201 * represents the current mapping mode and combining it with the DC's
202 * world transform. This function should be called whenever the
203 * parameters associated with the mapping mode (window and viewport
204 * extents and origins) or the world transform change.
206 void DC_UpdateXforms( DC *dc )
208 XFORM xformWnd2Vport;
209 FLOAT scaleX, scaleY;
211 /* Construct a transformation to do the window-to-viewport conversion */
212 scaleX = (FLOAT)dc->vportExtX / (FLOAT)dc->wndExtX;
213 scaleY = (FLOAT)dc->vportExtY / (FLOAT)dc->wndExtY;
214 xformWnd2Vport.eM11 = scaleX;
215 xformWnd2Vport.eM12 = 0.0;
216 xformWnd2Vport.eM21 = 0.0;
217 xformWnd2Vport.eM22 = scaleY;
218 xformWnd2Vport.eDx = (FLOAT)dc->vportOrgX -
219 scaleX * (FLOAT)dc->wndOrgX;
220 xformWnd2Vport.eDy = (FLOAT)dc->vportOrgY -
221 scaleY * (FLOAT)dc->wndOrgY;
223 /* Combine with the world transformation */
224 CombineTransform( &dc->xformWorld2Vport, &dc->xformWorld2Wnd,
225 &xformWnd2Vport );
227 /* Create inverse of world-to-viewport transformation */
228 dc->vport2WorldValid = DC_InvertXform( &dc->xformWorld2Vport,
229 &dc->xformVport2World );
233 /***********************************************************************
234 * GetDCState (GDI.179)
236 HDC16 WINAPI GetDCState16( HDC16 hdc )
238 DC * newdc, * dc;
239 HGDIOBJ handle;
241 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
242 if (!(newdc = GDI_AllocObject( sizeof(DC), DC_MAGIC, &handle )))
244 GDI_ReleaseObj( hdc );
245 return 0;
247 TRACE("(%04x): returning %04x\n", hdc, handle );
249 newdc->flags = dc->flags | DC_SAVED;
250 newdc->devCaps = dc->devCaps;
251 newdc->hPen = dc->hPen;
252 newdc->hBrush = dc->hBrush;
253 newdc->hFont = dc->hFont;
254 newdc->hBitmap = dc->hBitmap;
255 newdc->hDevice = dc->hDevice;
256 newdc->hPalette = dc->hPalette;
257 newdc->totalExtent = dc->totalExtent;
258 newdc->bitsPerPixel = dc->bitsPerPixel;
259 newdc->ROPmode = dc->ROPmode;
260 newdc->polyFillMode = dc->polyFillMode;
261 newdc->stretchBltMode = dc->stretchBltMode;
262 newdc->relAbsMode = dc->relAbsMode;
263 newdc->backgroundMode = dc->backgroundMode;
264 newdc->backgroundColor = dc->backgroundColor;
265 newdc->textColor = dc->textColor;
266 newdc->brushOrgX = dc->brushOrgX;
267 newdc->brushOrgY = dc->brushOrgY;
268 newdc->textAlign = dc->textAlign;
269 newdc->charExtra = dc->charExtra;
270 newdc->breakTotalExtra = dc->breakTotalExtra;
271 newdc->breakCount = dc->breakCount;
272 newdc->breakExtra = dc->breakExtra;
273 newdc->breakRem = dc->breakRem;
274 newdc->MapMode = dc->MapMode;
275 newdc->GraphicsMode = dc->GraphicsMode;
276 #if 0
277 /* Apparently, the DC origin is not changed by [GS]etDCState */
278 newdc->DCOrgX = dc->DCOrgX;
279 newdc->DCOrgY = dc->DCOrgY;
280 #endif
281 newdc->CursPosX = dc->CursPosX;
282 newdc->CursPosY = dc->CursPosY;
283 newdc->ArcDirection = dc->ArcDirection;
284 newdc->xformWorld2Wnd = dc->xformWorld2Wnd;
285 newdc->xformWorld2Vport = dc->xformWorld2Vport;
286 newdc->xformVport2World = dc->xformVport2World;
287 newdc->vport2WorldValid = dc->vport2WorldValid;
288 newdc->wndOrgX = dc->wndOrgX;
289 newdc->wndOrgY = dc->wndOrgY;
290 newdc->wndExtX = dc->wndExtX;
291 newdc->wndExtY = dc->wndExtY;
292 newdc->vportOrgX = dc->vportOrgX;
293 newdc->vportOrgY = dc->vportOrgY;
294 newdc->vportExtX = dc->vportExtX;
295 newdc->vportExtY = dc->vportExtY;
297 newdc->hSelf = (HDC)handle;
298 newdc->saveLevel = 0;
300 PATH_InitGdiPath( &newdc->path );
302 newdc->pAbortProc = NULL;
303 newdc->hookThunk = NULL;
304 newdc->hookProc = 0;
306 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
308 newdc->hGCClipRgn = newdc->hVisRgn = 0;
309 if (dc->hClipRgn)
311 newdc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
312 CombineRgn( newdc->hClipRgn, dc->hClipRgn, 0, RGN_COPY );
314 else
315 newdc->hClipRgn = 0;
316 GDI_ReleaseObj( handle );
317 GDI_ReleaseObj( hdc );
318 return handle;
322 /***********************************************************************
323 * SetDCState (GDI.180)
325 void WINAPI SetDCState16( HDC16 hdc, HDC16 hdcs )
327 DC *dc, *dcs;
329 if (!(dc = GDI_GetObjPtr( hdc, DC_MAGIC ))) return;
330 if (!(dcs = GDI_GetObjPtr( hdcs, DC_MAGIC )))
332 GDI_ReleaseObj( hdc );
333 return;
335 if (!dcs->flags & DC_SAVED)
337 GDI_ReleaseObj( hdc );
338 GDI_ReleaseObj( hdcs );
339 return;
341 TRACE("%04x %04x\n", hdc, hdcs );
343 dc->flags = dcs->flags & ~DC_SAVED;
344 dc->devCaps = dcs->devCaps;
345 dc->hDevice = dcs->hDevice;
346 dc->totalExtent = dcs->totalExtent;
347 dc->ROPmode = dcs->ROPmode;
348 dc->polyFillMode = dcs->polyFillMode;
349 dc->stretchBltMode = dcs->stretchBltMode;
350 dc->relAbsMode = dcs->relAbsMode;
351 dc->backgroundMode = dcs->backgroundMode;
352 dc->backgroundColor = dcs->backgroundColor;
353 dc->textColor = dcs->textColor;
354 dc->brushOrgX = dcs->brushOrgX;
355 dc->brushOrgY = dcs->brushOrgY;
356 dc->textAlign = dcs->textAlign;
357 dc->charExtra = dcs->charExtra;
358 dc->breakTotalExtra = dcs->breakTotalExtra;
359 dc->breakCount = dcs->breakCount;
360 dc->breakExtra = dcs->breakExtra;
361 dc->breakRem = dcs->breakRem;
362 dc->MapMode = dcs->MapMode;
363 dc->GraphicsMode = dcs->GraphicsMode;
364 #if 0
365 /* Apparently, the DC origin is not changed by [GS]etDCState */
366 dc->DCOrgX = dcs->DCOrgX;
367 dc->DCOrgY = dcs->DCOrgY;
368 #endif
369 dc->CursPosX = dcs->CursPosX;
370 dc->CursPosY = dcs->CursPosY;
371 dc->ArcDirection = dcs->ArcDirection;
372 dc->xformWorld2Wnd = dcs->xformWorld2Wnd;
373 dc->xformWorld2Vport = dcs->xformWorld2Vport;
374 dc->xformVport2World = dcs->xformVport2World;
375 dc->vport2WorldValid = dcs->vport2WorldValid;
377 dc->wndOrgX = dcs->wndOrgX;
378 dc->wndOrgY = dcs->wndOrgY;
379 dc->wndExtX = dcs->wndExtX;
380 dc->wndExtY = dcs->wndExtY;
381 dc->vportOrgX = dcs->vportOrgX;
382 dc->vportOrgY = dcs->vportOrgY;
383 dc->vportExtX = dcs->vportExtX;
384 dc->vportExtY = dcs->vportExtY;
386 if (!(dc->flags & DC_MEMORY)) dc->bitsPerPixel = dcs->bitsPerPixel;
388 if (dcs->hClipRgn)
390 if (!dc->hClipRgn) dc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
391 CombineRgn( dc->hClipRgn, dcs->hClipRgn, 0, RGN_COPY );
393 else
395 if (dc->hClipRgn) DeleteObject16( dc->hClipRgn );
396 dc->hClipRgn = 0;
398 CLIPPING_UpdateGCRegion( dc );
400 SelectObject( hdc, dcs->hBitmap );
401 SelectObject( hdc, dcs->hBrush );
402 SelectObject( hdc, dcs->hFont );
403 SelectObject( hdc, dcs->hPen );
404 SetBkColor( hdc, dcs->backgroundColor);
405 SetTextColor( hdc, dcs->textColor);
406 GDISelectPalette16( hdc, dcs->hPalette, FALSE );
407 GDI_ReleaseObj( hdcs );
408 GDI_ReleaseObj( hdc );
412 /***********************************************************************
413 * SaveDC16 (GDI.30)
415 INT16 WINAPI SaveDC16( HDC16 hdc )
417 return (INT16)SaveDC( hdc );
421 /***********************************************************************
422 * SaveDC (GDI32.292)
424 INT WINAPI SaveDC( HDC hdc )
426 HDC hdcs;
427 DC * dc, * dcs;
428 INT ret;
430 dc = DC_GetDCUpdate( hdc );
431 if (!dc) return 0;
433 if(dc->funcs->pSaveDC)
435 ret = dc->funcs->pSaveDC( dc );
436 GDI_ReleaseObj( hdc );
437 return ret;
440 if (!(hdcs = GetDCState16( hdc )))
442 GDI_ReleaseObj( hdc );
443 return 0;
445 dcs = GDI_GetObjPtr( hdcs, DC_MAGIC );
447 /* Copy path. The reason why path saving / restoring is in SaveDC/
448 * RestoreDC and not in GetDCState/SetDCState is that the ...DCState
449 * functions are only in Win16 (which doesn't have paths) and that
450 * SetDCState doesn't allow us to signal an error (which can happen
451 * when copying paths).
453 if (!PATH_AssignGdiPath( &dcs->path, &dc->path ))
455 GDI_ReleaseObj( hdc );
456 GDI_ReleaseObj( hdcs );
457 DeleteDC( hdcs );
458 return 0;
461 dcs->header.hNext = dc->header.hNext;
462 dc->header.hNext = hdcs;
463 TRACE("(%04x): returning %d\n", hdc, dc->saveLevel+1 );
464 ret = ++dc->saveLevel;
465 GDI_ReleaseObj( hdcs );
466 GDI_ReleaseObj( hdc );
467 return ret;
471 /***********************************************************************
472 * RestoreDC16 (GDI.39)
474 BOOL16 WINAPI RestoreDC16( HDC16 hdc, INT16 level )
476 return RestoreDC( hdc, level );
480 /***********************************************************************
481 * RestoreDC (GDI32.290)
483 BOOL WINAPI RestoreDC( HDC hdc, INT level )
485 DC * dc, * dcs;
486 BOOL success;
488 TRACE("%04x %d\n", hdc, level );
489 dc = DC_GetDCPtr( hdc );
490 if(!dc) return FALSE;
491 if(dc->funcs->pRestoreDC)
493 success = dc->funcs->pRestoreDC( dc, level );
494 GDI_ReleaseObj( hdc );
495 return success;
498 if (level == -1) level = dc->saveLevel;
499 if ((level < 1)
500 /* This pair of checks disagrees with MSDN "Platform SDK:
501 Windows GDI" July 2000 which says all negative values
502 for level will be interpreted as an instance relative
503 to the current state. Restricting it to just -1 does
504 not satisfy this */
505 || (level > dc->saveLevel))
507 GDI_ReleaseObj( hdc );
508 return FALSE;
511 success=TRUE;
512 while (dc->saveLevel >= level)
514 HDC16 hdcs = dc->header.hNext;
515 if (!(dcs = GDI_GetObjPtr( hdcs, DC_MAGIC )))
517 GDI_ReleaseObj( hdc );
518 return FALSE;
520 dc->header.hNext = dcs->header.hNext;
521 if (--dc->saveLevel < level)
523 SetDCState16( hdc, hdcs );
524 if (!PATH_AssignGdiPath( &dc->path, &dcs->path ))
525 /* FIXME: This might not be quite right, since we're
526 * returning FALSE but still destroying the saved DC state */
527 success=FALSE;
529 GDI_ReleaseObj( hdcs );
530 DeleteDC( hdcs );
532 GDI_ReleaseObj( hdc );
533 return success;
537 /***********************************************************************
538 * CreateDC16 (GDI.53)
540 HDC16 WINAPI CreateDC16( LPCSTR driver, LPCSTR device, LPCSTR output,
541 const DEVMODEA *initData )
543 HDC hdc;
544 DC * dc;
545 const DC_FUNCTIONS *funcs;
546 char buf[300];
548 if (!device || !DRIVER_GetDriverName( device, buf, sizeof(buf) ))
549 strcpy(buf, driver);
551 if (!(funcs = DRIVER_FindDriver( buf ))) return 0;
552 if (!(dc = DC_AllocDC( funcs ))) return 0;
553 dc->flags = 0;
555 TRACE("(driver=%s, device=%s, output=%s): returning %04x\n",
556 debugstr_a(driver), debugstr_a(device), debugstr_a(output), dc->hSelf );
558 if (dc->funcs->pCreateDC &&
559 !dc->funcs->pCreateDC( dc, buf, device, output, initData ))
561 WARN("creation aborted by device\n" );
562 GDI_FreeObject( dc->hSelf, dc );
563 return 0;
566 DC_InitDC( dc );
567 hdc = dc->hSelf;
568 GDI_ReleaseObj( hdc );
569 return hdc;
573 /***********************************************************************
574 * CreateDCA (GDI32.)
576 HDC WINAPI CreateDCA( LPCSTR driver, LPCSTR device, LPCSTR output,
577 const DEVMODEA *initData )
579 return CreateDC16( driver, device, output, (const DEVMODEA *)initData );
583 /***********************************************************************
584 * CreateDCW (GDI32.)
586 HDC WINAPI CreateDCW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
587 const DEVMODEW *initData )
589 LPSTR driverA = HEAP_strdupWtoA( GetProcessHeap(), 0, driver );
590 LPSTR deviceA = HEAP_strdupWtoA( GetProcessHeap(), 0, device );
591 LPSTR outputA = HEAP_strdupWtoA( GetProcessHeap(), 0, output );
592 HDC res = CreateDC16( driverA, deviceA, outputA,
593 (const DEVMODEA *)initData /*FIXME*/ );
594 HeapFree( GetProcessHeap(), 0, driverA );
595 HeapFree( GetProcessHeap(), 0, deviceA );
596 HeapFree( GetProcessHeap(), 0, outputA );
597 return res;
601 /***********************************************************************
602 * CreateIC16 (GDI.153)
604 HDC16 WINAPI CreateIC16( LPCSTR driver, LPCSTR device, LPCSTR output,
605 const DEVMODEA* initData )
607 /* Nothing special yet for ICs */
608 return CreateDC16( driver, device, output, initData );
612 /***********************************************************************
613 * CreateICA (GDI32.49)
615 HDC WINAPI CreateICA( LPCSTR driver, LPCSTR device, LPCSTR output,
616 const DEVMODEA* initData )
618 /* Nothing special yet for ICs */
619 return CreateDCA( driver, device, output, initData );
623 /***********************************************************************
624 * CreateICW (GDI32.50)
626 HDC WINAPI CreateICW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
627 const DEVMODEW* initData )
629 /* Nothing special yet for ICs */
630 return CreateDCW( driver, device, output, initData );
634 /***********************************************************************
635 * CreateCompatibleDC16 (GDI.52)
637 HDC16 WINAPI CreateCompatibleDC16( HDC16 hdc )
639 return (HDC16)CreateCompatibleDC( hdc );
643 /***********************************************************************
644 * CreateCompatibleDC (GDI32.31)
646 HDC WINAPI CreateCompatibleDC( HDC hdc )
648 DC *dc, *origDC;
649 const DC_FUNCTIONS *funcs;
651 if ((origDC = GDI_GetObjPtr( hdc, DC_MAGIC ))) funcs = origDC->funcs;
652 else funcs = DRIVER_FindDriver( "DISPLAY" );
654 if (!funcs || !(dc = DC_AllocDC( funcs )))
656 if (origDC) GDI_ReleaseObj( hdc );
657 return 0;
660 TRACE("(%04x): returning %04x\n",
661 hdc, dc->hSelf );
663 dc->flags = DC_MEMORY;
664 dc->bitsPerPixel = 1;
665 dc->hBitmap = hPseudoStockBitmap;
667 /* Copy the driver-specific physical device info into
668 * the new DC. The driver may use this read-only info
669 * while creating the compatible DC below. */
670 if (origDC)
671 dc->physDev = origDC->physDev;
673 if (dc->funcs->pCreateDC &&
674 !dc->funcs->pCreateDC( dc, NULL, NULL, NULL, NULL ))
676 WARN("creation aborted by device\n");
677 GDI_FreeObject( dc->hSelf, dc );
678 if (origDC) GDI_ReleaseObj( hdc );
679 return 0;
682 DC_InitDC( dc );
683 GDI_ReleaseObj( dc->hSelf );
684 if (origDC) GDI_ReleaseObj( hdc );
685 return dc->hSelf;
689 /***********************************************************************
690 * DeleteDC16 (GDI.68)
692 BOOL16 WINAPI DeleteDC16( HDC16 hdc )
694 return DeleteDC( hdc );
698 /***********************************************************************
699 * DeleteDC (GDI32.67)
701 BOOL WINAPI DeleteDC( HDC hdc )
703 DC * dc = GDI_GetObjPtr( hdc, DC_MAGIC );
704 if (!dc) return FALSE;
706 TRACE("%04x\n", hdc );
708 /* Call hook procedure to check whether is it OK to delete this DC */
709 if (dc->hookThunk && !(dc->flags & (DC_SAVED | DC_MEMORY)))
711 DCHOOKPROC proc = dc->hookThunk;
712 if (proc)
714 DWORD data = dc->dwHookData;
715 GDI_ReleaseObj( hdc );
716 if (!proc( hdc, DCHC_DELETEDC, data, 0 )) return FALSE;
717 if (!(dc = DC_GetDCPtr( hdc ))) return FALSE;
721 while (dc->saveLevel)
723 DC * dcs;
724 HDC16 hdcs = dc->header.hNext;
725 if (!(dcs = GDI_GetObjPtr( hdcs, DC_MAGIC ))) break;
726 dc->header.hNext = dcs->header.hNext;
727 dc->saveLevel--;
728 GDI_ReleaseObj( hdcs );
729 DeleteDC( hdcs );
732 if (!(dc->flags & DC_SAVED))
734 SelectObject( hdc, GetStockObject(BLACK_PEN) );
735 SelectObject( hdc, GetStockObject(WHITE_BRUSH) );
736 SelectObject( hdc, GetStockObject(SYSTEM_FONT) );
737 if (dc->funcs->pDeleteDC) dc->funcs->pDeleteDC(dc);
740 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
741 if (dc->hVisRgn) DeleteObject( dc->hVisRgn );
742 if (dc->hGCClipRgn) DeleteObject( dc->hGCClipRgn );
743 if (dc->pAbortProc) THUNK_Free( (FARPROC)dc->pAbortProc );
744 if (dc->hookThunk) THUNK_Free( (FARPROC)dc->hookThunk );
745 PATH_DestroyGdiPath(&dc->path);
747 return GDI_FreeObject( hdc, dc );
751 /***********************************************************************
752 * ResetDC16 (GDI.376)
754 HDC16 WINAPI ResetDC16( HDC16 hdc, const DEVMODEA *devmode )
756 FIXME("stub\n" );
757 return hdc;
761 /***********************************************************************
762 * ResetDCA (GDI32.287)
764 HDC WINAPI ResetDCA( HDC hdc, const DEVMODEA *devmode )
766 FIXME("stub\n" );
767 return hdc;
771 /***********************************************************************
772 * ResetDCW (GDI32.288)
774 HDC WINAPI ResetDCW( HDC hdc, const DEVMODEW *devmode )
776 FIXME("stub\n" );
777 return hdc;
781 /***********************************************************************
782 * GetDeviceCaps16 (GDI.80)
784 INT16 WINAPI GetDeviceCaps16( HDC16 hdc, INT16 cap )
786 INT16 ret = GetDeviceCaps( hdc, cap );
787 /* some apps don't expect -1 and think it's a B&W screen */
788 if ((cap == NUMCOLORS) && (ret == -1)) ret = 2048;
789 return ret;
793 /***********************************************************************
794 * GetDeviceCaps (GDI32.171)
796 INT WINAPI GetDeviceCaps( HDC hdc, INT cap )
798 DC *dc;
799 INT ret = 0;
800 POINT pt;
802 /* Device capabilities for the printer */
803 switch (cap)
805 case PHYSICALWIDTH:
806 if(Escape(hdc, GETPHYSPAGESIZE, 0, NULL, (LPVOID)&pt) > 0) ret = pt.x;
807 break;
808 case PHYSICALHEIGHT:
809 if(Escape(hdc, GETPHYSPAGESIZE, 0, NULL, (LPVOID)&pt) > 0) ret = pt.y;
810 break;
811 case PHYSICALOFFSETX:
812 if(Escape(hdc, GETPRINTINGOFFSET, 0, NULL, (LPVOID)&pt) > 0) ret = pt.x;
813 break;
814 case PHYSICALOFFSETY:
815 if(Escape(hdc, GETPRINTINGOFFSET, 0, NULL, (LPVOID)&pt) > 0) ret = pt.y;
816 break;
817 case SCALINGFACTORX:
818 if(Escape(hdc, GETSCALINGFACTOR, 0, NULL, (LPVOID)&pt) > 0) ret = pt.x;
819 break;
820 case SCALINGFACTORY:
821 if(Escape(hdc, GETSCALINGFACTOR, 0, NULL, (LPVOID)&pt) > 0) ret = pt.y;
822 break;
823 default:
824 if ((cap < 0) || (cap > sizeof(DeviceCaps)-sizeof(WORD))) break;
826 if (((cap>=46) && (cap<88)) || ((cap>=92) && (cap<104)))
827 FIXME("(%04x,%d): unsupported DeviceCaps capability, will yield 0!\n",
828 hdc,cap );
829 if ((dc = DC_GetDCPtr( hdc )))
831 ret = *(WORD *)(((char *)dc->devCaps) + cap);
832 GDI_ReleaseObj( hdc );
833 if ((cap == NUMCOLORS) && (ret == 0xffff)) ret = -1;
835 break;
838 TRACE("(%04x,%d): returning %d\n", hdc, cap, ret );
839 return ret;
843 /***********************************************************************
844 * SetBkColor16 (GDI.1)
846 COLORREF WINAPI SetBkColor16( HDC16 hdc, COLORREF color )
848 return SetBkColor( hdc, color );
852 /***********************************************************************
853 * SetBkColor (GDI32.305)
855 COLORREF WINAPI SetBkColor( HDC hdc, COLORREF color )
857 COLORREF oldColor;
858 DC * dc = DC_GetDCPtr( hdc );
860 if (!dc) return 0x80000000;
861 if (dc->funcs->pSetBkColor)
862 oldColor = dc->funcs->pSetBkColor(dc, color);
863 else {
864 oldColor = dc->backgroundColor;
865 dc->backgroundColor = color;
867 GDI_ReleaseObj( hdc );
868 return oldColor;
872 /***********************************************************************
873 * SetTextColor16 (GDI.9)
875 COLORREF WINAPI SetTextColor16( HDC16 hdc, COLORREF color )
877 return SetTextColor( hdc, color );
881 /***********************************************************************
882 * SetTextColor (GDI32.338)
884 COLORREF WINAPI SetTextColor( HDC hdc, COLORREF color )
886 COLORREF oldColor;
887 DC * dc = DC_GetDCPtr( hdc );
889 if (!dc) return 0x80000000;
890 if (dc->funcs->pSetTextColor)
891 oldColor = dc->funcs->pSetTextColor(dc, color);
892 else {
893 oldColor = dc->textColor;
894 dc->textColor = color;
896 GDI_ReleaseObj( hdc );
897 return oldColor;
900 /***********************************************************************
901 * SetTextAlign16 (GDI.346)
903 UINT16 WINAPI SetTextAlign16( HDC16 hdc, UINT16 align )
905 return SetTextAlign( hdc, align );
909 /***********************************************************************
910 * SetTextAlign (GDI32.336)
912 UINT WINAPI SetTextAlign( HDC hdc, UINT align )
914 UINT prevAlign;
915 DC *dc = DC_GetDCPtr( hdc );
916 if (!dc) return 0x0;
917 if (dc->funcs->pSetTextAlign)
918 prevAlign = dc->funcs->pSetTextAlign(dc, align);
919 else {
920 prevAlign = dc->textAlign;
921 dc->textAlign = align;
923 GDI_ReleaseObj( hdc );
924 return prevAlign;
927 /***********************************************************************
928 * GetDCOrgEx (GDI32.168)
930 BOOL WINAPI GetDCOrgEx( HDC hDC, LPPOINT lpp )
932 DC * dc;
934 if (!lpp) return FALSE;
935 if (!(dc = DC_GetDCPtr( hDC ))) return FALSE;
937 lpp->x = lpp->y = 0;
938 if (dc->funcs->pGetDCOrgEx) dc->funcs->pGetDCOrgEx( dc, lpp );
939 lpp->x += dc->DCOrgX;
940 lpp->y += dc->DCOrgY;
941 GDI_ReleaseObj( hDC );
942 return TRUE;
946 /***********************************************************************
947 * GetDCOrg (GDI.79)
949 DWORD WINAPI GetDCOrg16( HDC16 hdc )
951 POINT pt;
952 if( GetDCOrgEx( hdc, &pt) )
953 return MAKELONG( (WORD)pt.x, (WORD)pt.y );
954 return 0;
958 /***********************************************************************
959 * SetDCOrg (GDI.117)
961 DWORD WINAPI SetDCOrg16( HDC16 hdc, INT16 x, INT16 y )
963 DWORD prevOrg;
964 DC *dc = DC_GetDCPtr( hdc );
965 if (!dc) return 0;
966 prevOrg = dc->DCOrgX | (dc->DCOrgY << 16);
967 dc->DCOrgX = x;
968 dc->DCOrgY = y;
969 GDI_ReleaseObj( hdc );
970 return prevOrg;
974 /***********************************************************************
975 * SetGraphicsMode (GDI32.317)
977 INT WINAPI SetGraphicsMode( HDC hdc, INT mode )
979 INT ret = 0;
980 DC *dc = DC_GetDCPtr( hdc );
982 /* One would think that setting the graphics mode to GM_COMPATIBLE
983 * would also reset the world transformation matrix to the unity
984 * matrix. However, in Windows, this is not the case. This doesn't
985 * make a lot of sense to me, but that's the way it is.
987 if (!dc) return 0;
988 if ((mode > 0) || (mode <= GM_LAST))
990 ret = dc->GraphicsMode;
991 dc->GraphicsMode = mode;
993 GDI_ReleaseObj( hdc );
994 return ret;
998 /***********************************************************************
999 * SetArcDirection16 (GDI.525)
1001 INT16 WINAPI SetArcDirection16( HDC16 hdc, INT16 nDirection )
1003 return SetArcDirection( (HDC)hdc, (INT)nDirection );
1007 /***********************************************************************
1008 * SetArcDirection (GDI32.302)
1010 INT WINAPI SetArcDirection( HDC hdc, INT nDirection )
1012 DC * dc;
1013 INT nOldDirection = 0;
1015 if (nDirection!=AD_COUNTERCLOCKWISE && nDirection!=AD_CLOCKWISE)
1017 SetLastError(ERROR_INVALID_PARAMETER);
1018 return 0;
1021 if ((dc = DC_GetDCPtr( hdc )))
1023 nOldDirection = dc->ArcDirection;
1024 dc->ArcDirection = nDirection;
1025 GDI_ReleaseObj( hdc );
1027 return nOldDirection;
1031 /***********************************************************************
1032 * GetWorldTransform (GDI32.244)
1034 BOOL WINAPI GetWorldTransform( HDC hdc, LPXFORM xform )
1036 DC * dc;
1037 if (!xform) return FALSE;
1038 if (!(dc = DC_GetDCPtr( hdc ))) return FALSE;
1039 *xform = dc->xformWorld2Wnd;
1040 GDI_ReleaseObj( hdc );
1041 return TRUE;
1045 /***********************************************************************
1046 * SetWorldTransform (GDI32.346)
1048 BOOL WINAPI SetWorldTransform( HDC hdc, const XFORM *xform )
1050 BOOL ret = FALSE;
1051 DC *dc = DC_GetDCPtr( hdc );
1053 if (!dc) return FALSE;
1054 if (!xform) goto done;
1056 /* Check that graphics mode is GM_ADVANCED */
1057 if (dc->GraphicsMode!=GM_ADVANCED) goto done;
1059 dc->xformWorld2Wnd = *xform;
1060 DC_UpdateXforms( dc );
1061 ret = TRUE;
1062 done:
1063 GDI_ReleaseObj( hdc );
1064 return ret;
1068 /****************************************************************************
1069 * ModifyWorldTransform [GDI32.253]
1070 * Modifies the world transformation for a device context.
1072 * PARAMS
1073 * hdc [I] Handle to device context
1074 * xform [I] XFORM structure that will be used to modify the world
1075 * transformation
1076 * iMode [I] Specifies in what way to modify the world transformation
1077 * Possible values:
1078 * MWT_IDENTITY
1079 * Resets the world transformation to the identity matrix.
1080 * The parameter xform is ignored.
1081 * MWT_LEFTMULTIPLY
1082 * Multiplies xform into the world transformation matrix from
1083 * the left.
1084 * MWT_RIGHTMULTIPLY
1085 * Multiplies xform into the world transformation matrix from
1086 * the right.
1088 * RETURNS STD
1090 BOOL WINAPI ModifyWorldTransform( HDC hdc, const XFORM *xform,
1091 DWORD iMode )
1093 BOOL ret = FALSE;
1094 DC *dc = DC_GetDCPtr( hdc );
1096 /* Check for illegal parameters */
1097 if (!dc) return FALSE;
1098 if (!xform) goto done;
1100 /* Check that graphics mode is GM_ADVANCED */
1101 if (dc->GraphicsMode!=GM_ADVANCED) goto done;
1103 switch (iMode)
1105 case MWT_IDENTITY:
1106 dc->xformWorld2Wnd.eM11 = 1.0f;
1107 dc->xformWorld2Wnd.eM12 = 0.0f;
1108 dc->xformWorld2Wnd.eM21 = 0.0f;
1109 dc->xformWorld2Wnd.eM22 = 1.0f;
1110 dc->xformWorld2Wnd.eDx = 0.0f;
1111 dc->xformWorld2Wnd.eDy = 0.0f;
1112 break;
1113 case MWT_LEFTMULTIPLY:
1114 CombineTransform( &dc->xformWorld2Wnd, xform,
1115 &dc->xformWorld2Wnd );
1116 break;
1117 case MWT_RIGHTMULTIPLY:
1118 CombineTransform( &dc->xformWorld2Wnd, &dc->xformWorld2Wnd,
1119 xform );
1120 break;
1121 default:
1122 goto done;
1125 DC_UpdateXforms( dc );
1126 ret = TRUE;
1127 done:
1128 GDI_ReleaseObj( hdc );
1129 return ret;
1133 /****************************************************************************
1134 * CombineTransform [GDI32.20]
1135 * Combines two transformation matrices.
1137 * PARAMS
1138 * xformResult [O] Stores the result of combining the two matrices
1139 * xform1 [I] Specifies the first matrix to apply
1140 * xform2 [I] Specifies the second matrix to apply
1142 * REMARKS
1143 * The same matrix can be passed in for more than one of the parameters.
1145 * RETURNS STD
1147 BOOL WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
1148 const XFORM *xform2 )
1150 XFORM xformTemp;
1152 /* Check for illegal parameters */
1153 if (!xformResult || !xform1 || !xform2)
1154 return FALSE;
1156 /* Create the result in a temporary XFORM, since xformResult may be
1157 * equal to xform1 or xform2 */
1158 xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
1159 xform1->eM12 * xform2->eM21;
1160 xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
1161 xform1->eM12 * xform2->eM22;
1162 xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
1163 xform1->eM22 * xform2->eM21;
1164 xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
1165 xform1->eM22 * xform2->eM22;
1166 xformTemp.eDx = xform1->eDx * xform2->eM11 +
1167 xform1->eDy * xform2->eM21 +
1168 xform2->eDx;
1169 xformTemp.eDy = xform1->eDx * xform2->eM12 +
1170 xform1->eDy * xform2->eM22 +
1171 xform2->eDy;
1173 /* Copy the result to xformResult */
1174 *xformResult = xformTemp;
1176 return TRUE;
1180 /***********************************************************************
1181 * SetDCHook (GDI.190)
1183 /* ### start build ### */
1184 extern WORD CALLBACK GDI_CallTo16_word_wwll(FARPROC16,WORD,WORD,LONG,LONG);
1185 /* ### stop build ### */
1186 BOOL16 WINAPI SetDCHook( HDC16 hdc, FARPROC16 hookProc, DWORD dwHookData )
1188 DC *dc = DC_GetDCPtr( hdc );
1189 if (!dc) return FALSE;
1192 * Note: We store the original SEGPTR 'hookProc' as we need to be
1193 * able to return it verbatim in GetDCHook,
1195 * On the other hand, we still call THUNK_Alloc and store the
1196 * 32-bit thunk into another DC member, because THUNK_Alloc
1197 * recognizes the (typical) case that the 'hookProc' is indeed
1198 * the 16-bit API entry point of a built-in routine (e.g. DCHook16)
1200 * We could perform that test every time the hook is about to
1201 * be called (or else we could live with the 32->16->32 detour),
1202 * but this way is the most efficient ...
1205 dc->hookProc = hookProc;
1206 dc->dwHookData = dwHookData;
1208 THUNK_Free( (FARPROC)dc->hookThunk );
1209 dc->hookThunk = (DCHOOKPROC)
1210 THUNK_Alloc( hookProc, (RELAY)GDI_CallTo16_word_wwll );
1212 GDI_ReleaseObj( hdc );
1213 return TRUE;
1217 /***********************************************************************
1218 * GetDCHook (GDI.191)
1220 DWORD WINAPI GetDCHook( HDC16 hdc, FARPROC16 *phookProc )
1222 DC *dc = DC_GetDCPtr( hdc );
1223 if (!dc) return 0;
1224 *phookProc = dc->hookProc;
1225 GDI_ReleaseObj( hdc );
1226 return dc->dwHookData;
1230 /***********************************************************************
1231 * SetHookFlags (GDI.192)
1233 WORD WINAPI SetHookFlags16(HDC16 hDC, WORD flags)
1235 DC *dc = DC_GetDCPtr( hDC );
1237 if( dc )
1239 WORD wRet = dc->flags & DC_DIRTY;
1241 /* "Undocumented Windows" info is slightly confusing.
1244 TRACE("hDC %04x, flags %04x\n",hDC,flags);
1246 if( flags & DCHF_INVALIDATEVISRGN )
1247 dc->flags |= DC_DIRTY;
1248 else if( flags & DCHF_VALIDATEVISRGN || !flags )
1249 dc->flags &= ~DC_DIRTY;
1250 GDI_ReleaseObj( hDC );
1251 return wRet;
1253 return 0;
1256 /***********************************************************************
1257 * SetICMMode (GDI32.318)
1259 INT WINAPI SetICMMode(HDC hdc, INT iEnableICM)
1261 /*FIXME Asuming that ICM is always off, and cannot be turned on */
1262 if (iEnableICM == ICM_OFF) return ICM_OFF;
1263 if (iEnableICM == ICM_ON) return 0;
1264 if (iEnableICM == ICM_QUERY) return ICM_OFF;
1265 return 0;
1268 /***********************************************************************
1269 * GetDeviceGammaRamp (GDI32.*)
1271 BOOL WINAPI GetDeviceGammaRamp(HDC hdc,LPVOID ptr) {
1272 FIXME("(%x,%p), empty stub!\n",hdc,ptr);
1273 return FALSE;
1276 /***********************************************************************
1277 * GetColorSpace (GDI32.165)
1279 HCOLORSPACE WINAPI GetColorSpace(HDC hdc)
1281 /*FIXME Need to to whatever GetColorSpace actually does */
1282 return 0;
1285 /***********************************************************************
1286 * CreateColorSpaceA (GDI32.???)
1288 HCOLORSPACE WINAPI CreateColorSpaceA( LPLOGCOLORSPACEA lpLogColorSpace )
1290 FIXME( "stub\n" );
1291 return 0;
1294 /***********************************************************************
1295 * CreateColorSpaceW (GDI32.???)
1297 HCOLORSPACE WINAPI CreateColorSpaceW( LPLOGCOLORSPACEW lpLogColorSpace )
1299 FIXME( "stub\n" );
1300 return 0;
1303 /***********************************************************************
1304 * DeleteColorSpace (GDI32.???)
1306 BOOL WINAPI DeleteColorSpace( HCOLORSPACE hColorSpace )
1308 FIXME( "stub\n" );
1310 return TRUE;
1313 /***********************************************************************
1314 * SetColorSpace (GDI32.???)
1316 HCOLORSPACE WINAPI SetColorSpace( HDC hDC, HCOLORSPACE hColorSpace )
1318 FIXME( "stub\n" );
1320 return hColorSpace;
1323 /***********************************************************************
1324 * GetBoundsRect16 (GDI.194)
1326 UINT16 WINAPI GetBoundsRect16(HDC16 hdc, LPRECT16 rect, UINT16 flags)
1328 return DCB_RESET | DCB_DISABLE; /* bounding rectangle always empty and disabled*/
1331 /***********************************************************************
1332 * GetBoundsRect (GDI32.147)
1334 UINT WINAPI GetBoundsRect(HDC hdc, LPRECT rect, UINT flags)
1336 FIXME("(): stub\n");
1337 return DCB_RESET; /* bounding rectangle always empty */
1340 /***********************************************************************
1341 * SetBoundsRect16 (GDI.193)
1343 UINT16 WINAPI SetBoundsRect16(HDC16 hdc, const RECT16* rect, UINT16 flags)
1345 if ( (flags & DCB_ACCUMULATE) || (flags & DCB_ENABLE) )
1346 FIXME("(%04x, %p, %04x): stub\n", hdc, rect, flags );
1348 return DCB_RESET | DCB_DISABLE; /* bounding rectangle always empty and disabled*/
1351 /***********************************************************************
1352 * SetBoundsRect (GDI32.307)
1354 UINT WINAPI SetBoundsRect(HDC hdc, const RECT* rect, UINT flags)
1356 FIXME("(): stub\n");
1357 return DCB_DISABLE; /* bounding rectangle always empty */
1361 /***********************************************************************
1362 * GetRelAbs (GDI32.218)
1364 INT WINAPI GetRelAbs( HDC hdc, DWORD dwIgnore )
1366 INT ret = 0;
1367 DC *dc = DC_GetDCPtr( hdc );
1368 if (dc) ret = dc->relAbsMode;
1369 GDI_ReleaseObj( hdc );
1370 return ret;
1373 /***********************************************************************
1374 * Death (GDI.121)
1376 * Disables GDI, switches back to text mode.
1377 * We don't have to do anything here,
1378 * just let console support handle everything
1380 void WINAPI Death16(HDC16 hDC)
1382 MESSAGE("Death(%04x) called. Application enters text mode...\n", hDC);
1385 /***********************************************************************
1386 * Resurrection (GDI.122)
1388 * Restores GDI functionality
1390 void WINAPI Resurrection16(HDC16 hDC,
1391 WORD w1, WORD w2, WORD w3, WORD w4, WORD w5, WORD w6)
1393 MESSAGE("Resurrection(%04x, %04x, %04x, %04x, %04x, %04x, %04x) called. Application left text mode.\n", hDC, w1, w2, w3, w4, w5, w6);
1396 /***********************************************************************
1397 * GetLayout (GDI32.321)
1399 * Gets left->right or right->left text layout flags of a dc.
1400 * win98 just returns 0 and sets ERROR_CALL_NOT_IMPLEMENTED so we do the same
1403 DWORD WINAPI GetLayout(HDC hdc)
1405 FIXME("(%08x): stub\n", hdc);
1406 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1407 return 0;
1410 /***********************************************************************
1411 * SetLayout (GDI32.450)
1413 * Sets left->right or right->left text layout flags of a dc.
1414 * win98 just returns 0 and sets ERROR_CALL_NOT_IMPLEMENTED so we do the same
1417 DWORD WINAPI SetLayout(HDC hdc, DWORD layout)
1419 FIXME("(%08x,%08lx): stub\n", hdc, layout);
1420 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1421 return 0;