2 * GDI Device Context functions
4 * Copyright 1993 Alexandre Julliard
15 #include "debugtools.h"
21 #include "wine/winuser16.h"
23 DEFAULT_DEBUG_CHANNEL(dc);
25 /***********************************************************************
28 * Fill the WIN_DC_INFO structure.
30 static void DC_Init_DC_INFO( WIN_DC_INFO *win_dc_info )
32 win_dc_info->flags = 0;
33 win_dc_info->devCaps = NULL;
34 win_dc_info->hClipRgn = 0;
35 win_dc_info->hVisRgn = 0;
36 win_dc_info->hGCClipRgn = 0;
37 win_dc_info->hPen = STOCK_BLACK_PEN;
38 win_dc_info->hBrush = STOCK_WHITE_BRUSH;
39 win_dc_info->hFont = STOCK_SYSTEM_FONT;
40 win_dc_info->hBitmap = 0;
41 win_dc_info->hDevice = 0;
42 win_dc_info->hPalette = STOCK_DEFAULT_PALETTE;
43 win_dc_info->ROPmode = R2_COPYPEN;
44 win_dc_info->polyFillMode = ALTERNATE;
45 win_dc_info->stretchBltMode = BLACKONWHITE;
46 win_dc_info->relAbsMode = ABSOLUTE;
47 win_dc_info->backgroundMode = OPAQUE;
48 win_dc_info->backgroundColor = RGB( 255, 255, 255 );
49 win_dc_info->textColor = RGB( 0, 0, 0 );
50 win_dc_info->brushOrgX = 0;
51 win_dc_info->brushOrgY = 0;
52 win_dc_info->textAlign = TA_LEFT | TA_TOP | TA_NOUPDATECP;
53 win_dc_info->charExtra = 0;
54 win_dc_info->breakTotalExtra = 0;
55 win_dc_info->breakCount = 0;
56 win_dc_info->breakExtra = 0;
57 win_dc_info->breakRem = 0;
58 win_dc_info->bitsPerPixel = 1;
59 win_dc_info->MapMode = MM_TEXT;
60 win_dc_info->GraphicsMode = GM_COMPATIBLE;
61 win_dc_info->DCOrgX = 0;
62 win_dc_info->DCOrgY = 0;
63 win_dc_info->pAbortProc = NULL;
64 win_dc_info->CursPosX = 0;
65 win_dc_info->CursPosY = 0;
66 win_dc_info->ArcDirection = AD_COUNTERCLOCKWISE;
67 win_dc_info->xformWorld2Wnd.eM11 = 1.0f;
68 win_dc_info->xformWorld2Wnd.eM12 = 0.0f;
69 win_dc_info->xformWorld2Wnd.eM21 = 0.0f;
70 win_dc_info->xformWorld2Wnd.eM22 = 1.0f;
71 win_dc_info->xformWorld2Wnd.eDx = 0.0f;
72 win_dc_info->xformWorld2Wnd.eDy = 0.0f;
73 win_dc_info->xformWorld2Vport = win_dc_info->xformWorld2Wnd;
74 win_dc_info->xformVport2World = win_dc_info->xformWorld2Wnd;
75 win_dc_info->vport2WorldValid = TRUE;
77 PATH_InitGdiPath(&win_dc_info->path);
81 /***********************************************************************
84 DC *DC_AllocDC( const DC_FUNCTIONS *funcs )
89 if (!(dc = GDI_AllocObject( sizeof(DC), DC_MAGIC, &hdc ))) return NULL;
106 DC_Init_DC_INFO( &dc->w );
113 /***********************************************************************
116 DC *DC_GetDCPtr( HDC hdc )
118 GDIOBJHDR *ptr = GDI_GetObjPtr( hdc, MAGIC_DONTCARE );
119 if (!ptr) return NULL;
120 if ((ptr->wMagic == DC_MAGIC) || (ptr->wMagic == METAFILE_DC_MAGIC) ||
121 (ptr->wMagic == ENHMETAFILE_DC_MAGIC))
123 GDI_ReleaseObj( hdc );
124 SetLastError( ERROR_INVALID_HANDLE );
128 /***********************************************************************
131 * Retrieve a DC ptr while making sure the visRgn is updated.
132 * This function may call up to USER so the GDI lock should _not_
133 * be held when calling it.
135 DC *DC_GetDCUpdate( HDC hdc )
137 DC *dc = DC_GetDCPtr( hdc );
138 if (!dc) return NULL;
139 while (dc->w.flags & DC_DIRTY)
141 dc->w.flags &= ~DC_DIRTY;
142 if (!(dc->w.flags & (DC_SAVED | DC_MEMORY)))
144 DCHOOKPROC proc = dc->hookThunk;
147 DWORD data = dc->dwHookData;
148 GDI_ReleaseObj( hdc );
149 proc( hdc, DCHC_INVALIDVISRGN, data, 0 );
150 if (!(dc = DC_GetDCPtr( hdc ))) break;
151 /* otherwise restart the loop in case it became dirty again in the meantime */
158 /***********************************************************************
161 * Setup device-specific DC values for a newly created DC.
163 void DC_InitDC( DC* dc )
165 RealizeDefaultPalette16( dc->hSelf );
166 SetTextColor( dc->hSelf, dc->w.textColor );
167 SetBkColor( dc->hSelf, dc->w.backgroundColor );
168 SelectObject( dc->hSelf, dc->w.hPen );
169 SelectObject( dc->hSelf, dc->w.hBrush );
170 SelectObject( dc->hSelf, dc->w.hFont );
171 CLIPPING_UpdateGCRegion( dc );
175 /***********************************************************************
178 * Computes the inverse of the transformation xformSrc and stores it to
179 * xformDest. Returns TRUE if successful or FALSE if the xformSrc matrix
182 static BOOL DC_InvertXform( const XFORM *xformSrc, XFORM *xformDest )
186 determinant = xformSrc->eM11*xformSrc->eM22 -
187 xformSrc->eM12*xformSrc->eM21;
188 if (determinant > -1e-12 && determinant < 1e-12)
191 xformDest->eM11 = xformSrc->eM22 / determinant;
192 xformDest->eM12 = -xformSrc->eM12 / determinant;
193 xformDest->eM21 = -xformSrc->eM21 / determinant;
194 xformDest->eM22 = xformSrc->eM11 / determinant;
195 xformDest->eDx = -xformSrc->eDx * xformDest->eM11 -
196 xformSrc->eDy * xformDest->eM21;
197 xformDest->eDy = -xformSrc->eDx * xformDest->eM12 -
198 xformSrc->eDy * xformDest->eM22;
204 /***********************************************************************
207 * Updates the xformWorld2Vport, xformVport2World and vport2WorldValid
208 * fields of the specified DC by creating a transformation that
209 * represents the current mapping mode and combining it with the DC's
210 * world transform. This function should be called whenever the
211 * parameters associated with the mapping mode (window and viewport
212 * extents and origins) or the world transform change.
214 void DC_UpdateXforms( DC *dc )
216 XFORM xformWnd2Vport;
217 FLOAT scaleX, scaleY;
219 /* Construct a transformation to do the window-to-viewport conversion */
220 scaleX = (FLOAT)dc->vportExtX / (FLOAT)dc->wndExtX;
221 scaleY = (FLOAT)dc->vportExtY / (FLOAT)dc->wndExtY;
222 xformWnd2Vport.eM11 = scaleX;
223 xformWnd2Vport.eM12 = 0.0;
224 xformWnd2Vport.eM21 = 0.0;
225 xformWnd2Vport.eM22 = scaleY;
226 xformWnd2Vport.eDx = (FLOAT)dc->vportOrgX -
227 scaleX * (FLOAT)dc->wndOrgX;
228 xformWnd2Vport.eDy = (FLOAT)dc->vportOrgY -
229 scaleY * (FLOAT)dc->wndOrgY;
231 /* Combine with the world transformation */
232 CombineTransform( &dc->w.xformWorld2Vport, &dc->w.xformWorld2Wnd,
235 /* Create inverse of world-to-viewport transformation */
236 dc->w.vport2WorldValid = DC_InvertXform( &dc->w.xformWorld2Vport,
237 &dc->w.xformVport2World );
241 /***********************************************************************
242 * GetDCState (GDI.179)
244 HDC16 WINAPI GetDCState16( HDC16 hdc )
249 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
250 if (!(newdc = GDI_AllocObject( sizeof(DC), DC_MAGIC, &handle )))
252 GDI_ReleaseObj( hdc );
255 TRACE("(%04x): returning %04x\n", hdc, handle );
257 newdc->w.flags = dc->w.flags | DC_SAVED;
258 newdc->w.devCaps = dc->w.devCaps;
259 newdc->w.hPen = dc->w.hPen;
260 newdc->w.hBrush = dc->w.hBrush;
261 newdc->w.hFont = dc->w.hFont;
262 newdc->w.hBitmap = dc->w.hBitmap;
263 newdc->w.hDevice = dc->w.hDevice;
264 newdc->w.hPalette = dc->w.hPalette;
265 newdc->w.totalExtent = dc->w.totalExtent;
266 newdc->w.bitsPerPixel = dc->w.bitsPerPixel;
267 newdc->w.ROPmode = dc->w.ROPmode;
268 newdc->w.polyFillMode = dc->w.polyFillMode;
269 newdc->w.stretchBltMode = dc->w.stretchBltMode;
270 newdc->w.relAbsMode = dc->w.relAbsMode;
271 newdc->w.backgroundMode = dc->w.backgroundMode;
272 newdc->w.backgroundColor = dc->w.backgroundColor;
273 newdc->w.textColor = dc->w.textColor;
274 newdc->w.brushOrgX = dc->w.brushOrgX;
275 newdc->w.brushOrgY = dc->w.brushOrgY;
276 newdc->w.textAlign = dc->w.textAlign;
277 newdc->w.charExtra = dc->w.charExtra;
278 newdc->w.breakTotalExtra = dc->w.breakTotalExtra;
279 newdc->w.breakCount = dc->w.breakCount;
280 newdc->w.breakExtra = dc->w.breakExtra;
281 newdc->w.breakRem = dc->w.breakRem;
282 newdc->w.MapMode = dc->w.MapMode;
283 newdc->w.GraphicsMode = dc->w.GraphicsMode;
285 /* Apparently, the DC origin is not changed by [GS]etDCState */
286 newdc->w.DCOrgX = dc->w.DCOrgX;
287 newdc->w.DCOrgY = dc->w.DCOrgY;
289 newdc->w.CursPosX = dc->w.CursPosX;
290 newdc->w.CursPosY = dc->w.CursPosY;
291 newdc->w.ArcDirection = dc->w.ArcDirection;
292 newdc->w.xformWorld2Wnd = dc->w.xformWorld2Wnd;
293 newdc->w.xformWorld2Vport = dc->w.xformWorld2Vport;
294 newdc->w.xformVport2World = dc->w.xformVport2World;
295 newdc->w.vport2WorldValid = dc->w.vport2WorldValid;
296 newdc->wndOrgX = dc->wndOrgX;
297 newdc->wndOrgY = dc->wndOrgY;
298 newdc->wndExtX = dc->wndExtX;
299 newdc->wndExtY = dc->wndExtY;
300 newdc->vportOrgX = dc->vportOrgX;
301 newdc->vportOrgY = dc->vportOrgY;
302 newdc->vportExtX = dc->vportExtX;
303 newdc->vportExtY = dc->vportExtY;
305 newdc->hSelf = (HDC)handle;
306 newdc->saveLevel = 0;
308 PATH_InitGdiPath( &newdc->w.path );
310 newdc->w.pAbortProc = NULL;
311 newdc->hookThunk = NULL;
313 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
315 newdc->w.hGCClipRgn = newdc->w.hVisRgn = 0;
318 newdc->w.hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
319 CombineRgn( newdc->w.hClipRgn, dc->w.hClipRgn, 0, RGN_COPY );
322 newdc->w.hClipRgn = 0;
323 GDI_ReleaseObj( handle );
324 GDI_ReleaseObj( hdc );
329 /***********************************************************************
330 * SetDCState (GDI.180)
332 void WINAPI SetDCState16( HDC16 hdc, HDC16 hdcs )
336 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return;
337 if (!(dcs = (DC *) GDI_GetObjPtr( hdcs, DC_MAGIC )))
339 GDI_ReleaseObj( hdc );
342 if (!dcs->w.flags & DC_SAVED)
344 GDI_ReleaseObj( hdc );
345 GDI_ReleaseObj( hdcs );
348 TRACE("%04x %04x\n", hdc, hdcs );
350 dc->w.flags = dcs->w.flags & ~DC_SAVED;
351 dc->w.devCaps = dcs->w.devCaps;
352 dc->w.hDevice = dcs->w.hDevice;
353 dc->w.totalExtent = dcs->w.totalExtent;
354 dc->w.ROPmode = dcs->w.ROPmode;
355 dc->w.polyFillMode = dcs->w.polyFillMode;
356 dc->w.stretchBltMode = dcs->w.stretchBltMode;
357 dc->w.relAbsMode = dcs->w.relAbsMode;
358 dc->w.backgroundMode = dcs->w.backgroundMode;
359 dc->w.backgroundColor = dcs->w.backgroundColor;
360 dc->w.textColor = dcs->w.textColor;
361 dc->w.brushOrgX = dcs->w.brushOrgX;
362 dc->w.brushOrgY = dcs->w.brushOrgY;
363 dc->w.textAlign = dcs->w.textAlign;
364 dc->w.charExtra = dcs->w.charExtra;
365 dc->w.breakTotalExtra = dcs->w.breakTotalExtra;
366 dc->w.breakCount = dcs->w.breakCount;
367 dc->w.breakExtra = dcs->w.breakExtra;
368 dc->w.breakRem = dcs->w.breakRem;
369 dc->w.MapMode = dcs->w.MapMode;
370 dc->w.GraphicsMode = dcs->w.GraphicsMode;
372 /* Apparently, the DC origin is not changed by [GS]etDCState */
373 dc->w.DCOrgX = dcs->w.DCOrgX;
374 dc->w.DCOrgY = dcs->w.DCOrgY;
376 dc->w.CursPosX = dcs->w.CursPosX;
377 dc->w.CursPosY = dcs->w.CursPosY;
378 dc->w.ArcDirection = dcs->w.ArcDirection;
379 dc->w.xformWorld2Wnd = dcs->w.xformWorld2Wnd;
380 dc->w.xformWorld2Vport = dcs->w.xformWorld2Vport;
381 dc->w.xformVport2World = dcs->w.xformVport2World;
382 dc->w.vport2WorldValid = dcs->w.vport2WorldValid;
384 dc->wndOrgX = dcs->wndOrgX;
385 dc->wndOrgY = dcs->wndOrgY;
386 dc->wndExtX = dcs->wndExtX;
387 dc->wndExtY = dcs->wndExtY;
388 dc->vportOrgX = dcs->vportOrgX;
389 dc->vportOrgY = dcs->vportOrgY;
390 dc->vportExtX = dcs->vportExtX;
391 dc->vportExtY = dcs->vportExtY;
393 if (!(dc->w.flags & DC_MEMORY)) dc->w.bitsPerPixel = dcs->w.bitsPerPixel;
397 if (!dc->w.hClipRgn) dc->w.hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
398 CombineRgn( dc->w.hClipRgn, dcs->w.hClipRgn, 0, RGN_COPY );
402 if (dc->w.hClipRgn) DeleteObject16( dc->w.hClipRgn );
405 CLIPPING_UpdateGCRegion( dc );
407 SelectObject( hdc, dcs->w.hBitmap );
408 SelectObject( hdc, dcs->w.hBrush );
409 SelectObject( hdc, dcs->w.hFont );
410 SelectObject( hdc, dcs->w.hPen );
411 SetBkColor( hdc, dcs->w.backgroundColor);
412 SetTextColor( hdc, dcs->w.textColor);
413 GDISelectPalette16( hdc, dcs->w.hPalette, FALSE );
414 GDI_ReleaseObj( hdcs );
415 GDI_ReleaseObj( hdc );
419 /***********************************************************************
422 INT16 WINAPI SaveDC16( HDC16 hdc )
424 return (INT16)SaveDC( hdc );
428 /***********************************************************************
431 INT WINAPI SaveDC( HDC hdc )
437 dc = DC_GetDCUpdate( hdc );
440 if(dc->funcs->pSaveDC)
442 ret = dc->funcs->pSaveDC( dc );
443 GDI_ReleaseObj( hdc );
447 if (!(hdcs = GetDCState16( hdc )))
449 GDI_ReleaseObj( hdc );
452 dcs = GDI_GetObjPtr( hdcs, DC_MAGIC );
454 /* Copy path. The reason why path saving / restoring is in SaveDC/
455 * RestoreDC and not in GetDCState/SetDCState is that the ...DCState
456 * functions are only in Win16 (which doesn't have paths) and that
457 * SetDCState doesn't allow us to signal an error (which can happen
458 * when copying paths).
460 if (!PATH_AssignGdiPath( &dcs->w.path, &dc->w.path ))
462 GDI_ReleaseObj( hdc );
463 GDI_ReleaseObj( hdcs );
468 dcs->header.hNext = dc->header.hNext;
469 dc->header.hNext = hdcs;
470 TRACE("(%04x): returning %d\n", hdc, dc->saveLevel+1 );
471 ret = ++dc->saveLevel;
472 GDI_ReleaseObj( hdcs );
473 GDI_ReleaseObj( hdc );
478 /***********************************************************************
479 * RestoreDC16 (GDI.39)
481 BOOL16 WINAPI RestoreDC16( HDC16 hdc, INT16 level )
483 return RestoreDC( hdc, level );
487 /***********************************************************************
488 * RestoreDC (GDI32.290)
490 BOOL WINAPI RestoreDC( HDC hdc, INT level )
495 TRACE("%04x %d\n", hdc, level );
496 dc = DC_GetDCPtr( hdc );
497 if(!dc) return FALSE;
498 if(dc->funcs->pRestoreDC)
500 success = dc->funcs->pRestoreDC( dc, level );
501 GDI_ReleaseObj( hdc );
505 if (level == -1) level = dc->saveLevel;
507 /* This pair of checks disagrees with MSDN "Platform SDK:
508 Windows GDI" July 2000 which says all negative values
509 for level will be interpreted as an instance relative
510 to the current state. Restricting it to just -1 does
512 || (level > dc->saveLevel))
514 GDI_ReleaseObj( hdc );
519 while (dc->saveLevel >= level)
521 HDC16 hdcs = dc->header.hNext;
522 if (!(dcs = (DC *) GDI_GetObjPtr( hdcs, DC_MAGIC )))
524 GDI_ReleaseObj( hdc );
527 dc->header.hNext = dcs->header.hNext;
528 if (--dc->saveLevel < level)
530 SetDCState16( hdc, hdcs );
531 if (!PATH_AssignGdiPath( &dc->w.path, &dcs->w.path ))
532 /* FIXME: This might not be quite right, since we're
533 * returning FALSE but still destroying the saved DC state */
536 GDI_ReleaseObj( hdcs );
539 GDI_ReleaseObj( hdc );
544 /***********************************************************************
545 * CreateDC16 (GDI.53)
547 HDC16 WINAPI CreateDC16( LPCSTR driver, LPCSTR device, LPCSTR output,
548 const DEVMODEA *initData )
552 const DC_FUNCTIONS *funcs;
555 if (!device || !DRIVER_GetDriverName( device, buf, sizeof(buf) ))
558 if (!(funcs = DRIVER_FindDriver( buf ))) return 0;
559 if (!(dc = DC_AllocDC( funcs ))) return 0;
562 TRACE("(driver=%s, device=%s, output=%s): returning %04x\n",
563 debugstr_a(driver), debugstr_a(device), debugstr_a(output), dc->hSelf );
565 if (dc->funcs->pCreateDC &&
566 !dc->funcs->pCreateDC( dc, buf, device, output, initData ))
568 WARN("creation aborted by device\n" );
569 GDI_FreeObject( dc->hSelf, dc );
575 GDI_ReleaseObj( hdc );
580 /***********************************************************************
583 HDC WINAPI CreateDCA( LPCSTR driver, LPCSTR device, LPCSTR output,
584 const DEVMODEA *initData )
586 return CreateDC16( driver, device, output, (const DEVMODEA *)initData );
590 /***********************************************************************
593 HDC WINAPI CreateDCW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
594 const DEVMODEW *initData )
596 LPSTR driverA = HEAP_strdupWtoA( GetProcessHeap(), 0, driver );
597 LPSTR deviceA = HEAP_strdupWtoA( GetProcessHeap(), 0, device );
598 LPSTR outputA = HEAP_strdupWtoA( GetProcessHeap(), 0, output );
599 HDC res = CreateDC16( driverA, deviceA, outputA,
600 (const DEVMODEA *)initData /*FIXME*/ );
601 HeapFree( GetProcessHeap(), 0, driverA );
602 HeapFree( GetProcessHeap(), 0, deviceA );
603 HeapFree( GetProcessHeap(), 0, outputA );
608 /***********************************************************************
609 * CreateIC16 (GDI.153)
611 HDC16 WINAPI CreateIC16( LPCSTR driver, LPCSTR device, LPCSTR output,
612 const DEVMODEA* initData )
614 /* Nothing special yet for ICs */
615 return CreateDC16( driver, device, output, initData );
619 /***********************************************************************
620 * CreateICA (GDI32.49)
622 HDC WINAPI CreateICA( LPCSTR driver, LPCSTR device, LPCSTR output,
623 const DEVMODEA* initData )
625 /* Nothing special yet for ICs */
626 return CreateDCA( driver, device, output, initData );
630 /***********************************************************************
631 * CreateICW (GDI32.50)
633 HDC WINAPI CreateICW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
634 const DEVMODEW* initData )
636 /* Nothing special yet for ICs */
637 return CreateDCW( driver, device, output, initData );
641 /***********************************************************************
642 * CreateCompatibleDC16 (GDI.52)
644 HDC16 WINAPI CreateCompatibleDC16( HDC16 hdc )
646 return (HDC16)CreateCompatibleDC( hdc );
650 /***********************************************************************
651 * CreateCompatibleDC (GDI32.31)
653 HDC WINAPI CreateCompatibleDC( HDC hdc )
656 const DC_FUNCTIONS *funcs;
658 if ((origDC = (DC *)GDI_GetObjPtr( hdc, DC_MAGIC ))) funcs = origDC->funcs;
659 else funcs = DRIVER_FindDriver( "DISPLAY" );
661 if (!funcs || !(dc = DC_AllocDC( funcs )))
663 if (origDC) GDI_ReleaseObj( hdc );
667 TRACE("(%04x): returning %04x\n",
670 dc->w.flags = DC_MEMORY;
671 dc->w.bitsPerPixel = 1;
672 dc->w.hBitmap = hPseudoStockBitmap;
674 /* Copy the driver-specific physical device info into
675 * the new DC. The driver may use this read-only info
676 * while creating the compatible DC below. */
678 dc->physDev = origDC->physDev;
680 if (dc->funcs->pCreateDC &&
681 !dc->funcs->pCreateDC( dc, NULL, NULL, NULL, NULL ))
683 WARN("creation aborted by device\n");
684 GDI_FreeObject( dc->hSelf, dc );
685 if (origDC) GDI_ReleaseObj( hdc );
690 GDI_ReleaseObj( dc->hSelf );
691 if (origDC) GDI_ReleaseObj( hdc );
696 /***********************************************************************
697 * DeleteDC16 (GDI.68)
699 BOOL16 WINAPI DeleteDC16( HDC16 hdc )
701 return DeleteDC( hdc );
705 /***********************************************************************
706 * DeleteDC (GDI32.67)
708 BOOL WINAPI DeleteDC( HDC hdc )
710 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
711 if (!dc) return FALSE;
713 TRACE("%04x\n", hdc );
715 /* Call hook procedure to check whether is it OK to delete this DC */
716 if (dc->hookThunk && !(dc->w.flags & (DC_SAVED | DC_MEMORY)))
718 DCHOOKPROC proc = dc->hookThunk;
721 DWORD data = dc->dwHookData;
722 GDI_ReleaseObj( hdc );
723 if (!proc( hdc, DCHC_DELETEDC, data, 0 )) return FALSE;
724 if (!(dc = DC_GetDCPtr( hdc ))) return FALSE;
728 while (dc->saveLevel)
731 HDC16 hdcs = dc->header.hNext;
732 if (!(dcs = (DC *) GDI_GetObjPtr( hdcs, DC_MAGIC ))) break;
733 dc->header.hNext = dcs->header.hNext;
735 GDI_ReleaseObj( hdcs );
739 if (!(dc->w.flags & DC_SAVED))
741 SelectObject( hdc, STOCK_BLACK_PEN );
742 SelectObject( hdc, STOCK_WHITE_BRUSH );
743 SelectObject( hdc, STOCK_SYSTEM_FONT );
744 if (dc->funcs->pDeleteDC) dc->funcs->pDeleteDC(dc);
747 if (dc->w.hClipRgn) DeleteObject( dc->w.hClipRgn );
748 if (dc->w.hVisRgn) DeleteObject( dc->w.hVisRgn );
749 if (dc->w.hGCClipRgn) DeleteObject( dc->w.hGCClipRgn );
750 if (dc->w.pAbortProc) THUNK_Free( (FARPROC)dc->w.pAbortProc );
751 if (dc->hookThunk) THUNK_Free( (FARPROC)dc->hookThunk );
752 PATH_DestroyGdiPath(&dc->w.path);
754 return GDI_FreeObject( hdc, dc );
758 /***********************************************************************
759 * ResetDC16 (GDI.376)
761 HDC16 WINAPI ResetDC16( HDC16 hdc, const DEVMODEA *devmode )
768 /***********************************************************************
769 * ResetDCA (GDI32.287)
771 HDC WINAPI ResetDCA( HDC hdc, const DEVMODEA *devmode )
778 /***********************************************************************
779 * ResetDCW (GDI32.288)
781 HDC WINAPI ResetDCW( HDC hdc, const DEVMODEW *devmode )
788 /***********************************************************************
789 * GetDeviceCaps16 (GDI.80)
791 INT16 WINAPI GetDeviceCaps16( HDC16 hdc, INT16 cap )
793 return GetDeviceCaps( hdc, cap );
797 /***********************************************************************
798 * GetDeviceCaps (GDI32.171)
800 INT WINAPI GetDeviceCaps( HDC hdc, INT cap )
802 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
808 /* Device capabilities for the printer */
812 if(Escape(hdc, GETPHYSPAGESIZE, 0, NULL, (LPVOID)&pt) > 0) ret = pt.x;
815 if(Escape(hdc, GETPHYSPAGESIZE, 0, NULL, (LPVOID)&pt) > 0) ret = pt.y;
817 case PHYSICALOFFSETX:
818 if(Escape(hdc, GETPRINTINGOFFSET, 0, NULL, (LPVOID)&pt) > 0) ret = pt.x;
820 case PHYSICALOFFSETY:
821 if(Escape(hdc, GETPRINTINGOFFSET, 0, NULL, (LPVOID)&pt) > 0) ret = pt.y;
824 if(Escape(hdc, GETSCALINGFACTOR, 0, NULL, (LPVOID)&pt) > 0) ret = pt.x;
827 if(Escape(hdc, GETSCALINGFACTOR, 0, NULL, (LPVOID)&pt) > 0) ret = pt.y;
830 if ((cap < 0) || (cap > sizeof(DeviceCaps)-sizeof(WORD))) break;
832 if (((cap>=46) && (cap<88)) || ((cap>=92) && (cap<104)))
833 FIXME("(%04x,%d): unsupported DeviceCaps capability, will yield 0!\n",
835 ret = *(WORD *)(((char *)dc->w.devCaps) + cap);
839 TRACE("(%04x,%d): returning %d\n", hdc, cap, ret );
840 GDI_ReleaseObj( hdc );
845 /***********************************************************************
846 * SetBkColor16 (GDI.1)
848 COLORREF WINAPI SetBkColor16( HDC16 hdc, COLORREF color )
850 return SetBkColor( hdc, color );
854 /***********************************************************************
855 * SetBkColor (GDI32.305)
857 COLORREF WINAPI SetBkColor( HDC hdc, COLORREF color )
860 DC * dc = DC_GetDCPtr( hdc );
862 if (!dc) return 0x80000000;
863 if (dc->funcs->pSetBkColor)
864 oldColor = dc->funcs->pSetBkColor(dc, color);
866 oldColor = dc->w.backgroundColor;
867 dc->w.backgroundColor = color;
869 GDI_ReleaseObj( hdc );
874 /***********************************************************************
875 * SetTextColor16 (GDI.9)
877 COLORREF WINAPI SetTextColor16( HDC16 hdc, COLORREF color )
879 return SetTextColor( hdc, color );
883 /***********************************************************************
884 * SetTextColor (GDI32.338)
886 COLORREF WINAPI SetTextColor( HDC hdc, COLORREF color )
889 DC * dc = DC_GetDCPtr( hdc );
891 if (!dc) return 0x80000000;
892 if (dc->funcs->pSetTextColor)
893 oldColor = dc->funcs->pSetTextColor(dc, color);
895 oldColor = dc->w.textColor;
896 dc->w.textColor = color;
898 GDI_ReleaseObj( hdc );
902 /***********************************************************************
903 * SetTextAlign16 (GDI.346)
905 UINT16 WINAPI SetTextAlign16( HDC16 hdc, UINT16 align )
907 return SetTextAlign( hdc, align );
911 /***********************************************************************
912 * SetTextAlign (GDI32.336)
914 UINT WINAPI SetTextAlign( HDC hdc, UINT align )
917 DC *dc = DC_GetDCPtr( hdc );
919 if (dc->funcs->pSetTextAlign)
920 prevAlign = dc->funcs->pSetTextAlign(dc, align);
922 prevAlign = dc->w.textAlign;
923 dc->w.textAlign = align;
925 GDI_ReleaseObj( hdc );
929 /***********************************************************************
930 * GetDCOrgEx (GDI32.168)
932 BOOL WINAPI GetDCOrgEx( HDC hDC, LPPOINT lpp )
936 if (!lpp) return FALSE;
937 if (!(dc = (DC *) GDI_GetObjPtr( hDC, DC_MAGIC ))) return FALSE;
940 if (dc->funcs->pGetDCOrgEx) dc->funcs->pGetDCOrgEx( dc, lpp );
941 lpp->x += dc->w.DCOrgX;
942 lpp->y += dc->w.DCOrgY;
943 GDI_ReleaseObj( hDC );
948 /***********************************************************************
951 DWORD WINAPI GetDCOrg16( HDC16 hdc )
954 if( GetDCOrgEx( hdc, &pt) )
955 return MAKELONG( (WORD)pt.x, (WORD)pt.y );
960 /***********************************************************************
963 DWORD WINAPI SetDCOrg16( HDC16 hdc, INT16 x, INT16 y )
966 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
968 prevOrg = dc->w.DCOrgX | (dc->w.DCOrgY << 16);
971 GDI_ReleaseObj( hdc );
976 /***********************************************************************
977 * SetGraphicsMode (GDI32.317)
979 INT WINAPI SetGraphicsMode( HDC hdc, INT mode )
982 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
984 /* One would think that setting the graphics mode to GM_COMPATIBLE
985 * would also reset the world transformation matrix to the unity
986 * matrix. However, in Windows, this is not the case. This doesn't
987 * make a lot of sense to me, but that's the way it is.
991 if ((mode > 0) || (mode <= GM_LAST))
993 ret = dc->w.GraphicsMode;
994 dc->w.GraphicsMode = mode;
996 GDI_ReleaseObj( hdc );
1001 /***********************************************************************
1002 * SetArcDirection16 (GDI.525)
1004 INT16 WINAPI SetArcDirection16( HDC16 hdc, INT16 nDirection )
1006 return SetArcDirection( (HDC)hdc, (INT)nDirection );
1010 /***********************************************************************
1011 * SetArcDirection (GDI32.302)
1013 INT WINAPI SetArcDirection( HDC hdc, INT nDirection )
1016 INT nOldDirection = 0;
1018 if (nDirection!=AD_COUNTERCLOCKWISE && nDirection!=AD_CLOCKWISE)
1020 SetLastError(ERROR_INVALID_PARAMETER);
1024 if ((dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC )))
1026 nOldDirection = dc->w.ArcDirection;
1027 dc->w.ArcDirection = nDirection;
1028 GDI_ReleaseObj( hdc );
1030 return nOldDirection;
1034 /***********************************************************************
1035 * GetWorldTransform (GDI32.244)
1037 BOOL WINAPI GetWorldTransform( HDC hdc, LPXFORM xform )
1040 if (!xform) return FALSE;
1041 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return FALSE;
1042 *xform = dc->w.xformWorld2Wnd;
1043 GDI_ReleaseObj( hdc );
1048 /***********************************************************************
1049 * SetWorldTransform (GDI32.346)
1051 BOOL WINAPI SetWorldTransform( HDC hdc, const XFORM *xform )
1054 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1058 SetLastError( ERROR_INVALID_HANDLE );
1062 if (!xform) goto done;
1064 /* Check that graphics mode is GM_ADVANCED */
1065 if (dc->w.GraphicsMode!=GM_ADVANCED) goto done;
1067 dc->w.xformWorld2Wnd = *xform;
1068 DC_UpdateXforms( dc );
1071 GDI_ReleaseObj( hdc );
1076 /****************************************************************************
1077 * ModifyWorldTransform [GDI32.253]
1078 * Modifies the world transformation for a device context.
1081 * hdc [I] Handle to device context
1082 * xform [I] XFORM structure that will be used to modify the world
1084 * iMode [I] Specifies in what way to modify the world transformation
1087 * Resets the world transformation to the identity matrix.
1088 * The parameter xform is ignored.
1090 * Multiplies xform into the world transformation matrix from
1093 * Multiplies xform into the world transformation matrix from
1098 BOOL WINAPI ModifyWorldTransform( HDC hdc, const XFORM *xform,
1102 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1104 /* Check for illegal parameters */
1107 SetLastError( ERROR_INVALID_HANDLE );
1110 if (!xform) goto done;
1112 /* Check that graphics mode is GM_ADVANCED */
1113 if (dc->w.GraphicsMode!=GM_ADVANCED) goto done;
1118 dc->w.xformWorld2Wnd.eM11 = 1.0f;
1119 dc->w.xformWorld2Wnd.eM12 = 0.0f;
1120 dc->w.xformWorld2Wnd.eM21 = 0.0f;
1121 dc->w.xformWorld2Wnd.eM22 = 1.0f;
1122 dc->w.xformWorld2Wnd.eDx = 0.0f;
1123 dc->w.xformWorld2Wnd.eDy = 0.0f;
1125 case MWT_LEFTMULTIPLY:
1126 CombineTransform( &dc->w.xformWorld2Wnd, xform,
1127 &dc->w.xformWorld2Wnd );
1129 case MWT_RIGHTMULTIPLY:
1130 CombineTransform( &dc->w.xformWorld2Wnd, &dc->w.xformWorld2Wnd,
1137 DC_UpdateXforms( dc );
1140 GDI_ReleaseObj( hdc );
1145 /****************************************************************************
1146 * CombineTransform [GDI32.20]
1147 * Combines two transformation matrices.
1150 * xformResult [O] Stores the result of combining the two matrices
1151 * xform1 [I] Specifies the first matrix to apply
1152 * xform2 [I] Specifies the second matrix to apply
1155 * The same matrix can be passed in for more than one of the parameters.
1159 BOOL WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
1160 const XFORM *xform2 )
1164 /* Check for illegal parameters */
1165 if (!xformResult || !xform1 || !xform2)
1168 /* Create the result in a temporary XFORM, since xformResult may be
1169 * equal to xform1 or xform2 */
1170 xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
1171 xform1->eM12 * xform2->eM21;
1172 xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
1173 xform1->eM12 * xform2->eM22;
1174 xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
1175 xform1->eM22 * xform2->eM21;
1176 xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
1177 xform1->eM22 * xform2->eM22;
1178 xformTemp.eDx = xform1->eDx * xform2->eM11 +
1179 xform1->eDy * xform2->eM21 +
1181 xformTemp.eDy = xform1->eDx * xform2->eM12 +
1182 xform1->eDy * xform2->eM22 +
1185 /* Copy the result to xformResult */
1186 *xformResult = xformTemp;
1192 /***********************************************************************
1193 * SetDCHook (GDI.190)
1195 /* ### start build ### */
1196 extern WORD CALLBACK GDI_CallTo16_word_wwll(FARPROC16,WORD,WORD,LONG,LONG);
1197 /* ### stop build ### */
1198 BOOL16 WINAPI SetDCHook( HDC16 hdc, FARPROC16 hookProc, DWORD dwHookData )
1200 DC *dc = (DC *)GDI_GetObjPtr( hdc, DC_MAGIC );
1201 if (!dc) return FALSE;
1204 * Note: We store the original SEGPTR 'hookProc' as we need to be
1205 * able to return it verbatim in GetDCHook,
1207 * On the other hand, we still call THUNK_Alloc and store the
1208 * 32-bit thunk into another DC member, because THUNK_Alloc
1209 * recognizes the (typical) case that the 'hookProc' is indeed
1210 * the 16-bit API entry point of a built-in routine (e.g. DCHook16)
1212 * We could perform that test every time the hook is about to
1213 * be called (or else we could live with the 32->16->32 detour),
1214 * but this way is the most efficient ...
1217 dc->hookProc = hookProc;
1218 dc->dwHookData = dwHookData;
1220 THUNK_Free( (FARPROC)dc->hookThunk );
1221 dc->hookThunk = (DCHOOKPROC)
1222 THUNK_Alloc( hookProc, (RELAY)GDI_CallTo16_word_wwll );
1224 GDI_ReleaseObj( hdc );
1229 /***********************************************************************
1230 * GetDCHook (GDI.191)
1232 DWORD WINAPI GetDCHook( HDC16 hdc, FARPROC16 *phookProc )
1234 DC *dc = (DC *)GDI_GetObjPtr( hdc, DC_MAGIC );
1236 *phookProc = dc->hookProc;
1237 GDI_ReleaseObj( hdc );
1238 return dc->dwHookData;
1242 /***********************************************************************
1243 * SetHookFlags (GDI.192)
1245 WORD WINAPI SetHookFlags16(HDC16 hDC, WORD flags)
1247 DC* dc = (DC*)GDI_GetObjPtr( hDC, DC_MAGIC );
1251 WORD wRet = dc->w.flags & DC_DIRTY;
1253 /* "Undocumented Windows" info is slightly confusing.
1256 TRACE("hDC %04x, flags %04x\n",hDC,flags);
1258 if( flags & DCHF_INVALIDATEVISRGN )
1259 dc->w.flags |= DC_DIRTY;
1260 else if( flags & DCHF_VALIDATEVISRGN || !flags )
1261 dc->w.flags &= ~DC_DIRTY;
1262 GDI_ReleaseObj( hDC );
1268 /***********************************************************************
1269 * SetICMMode (GDI32.318)
1271 INT WINAPI SetICMMode(HDC hdc, INT iEnableICM)
1273 /*FIXME Asuming that ICM is always off, and cannot be turned on */
1274 if (iEnableICM == ICM_OFF) return ICM_OFF;
1275 if (iEnableICM == ICM_ON) return 0;
1276 if (iEnableICM == ICM_QUERY) return ICM_OFF;
1281 /***********************************************************************
1282 * GetColorSpace (GDI32.165)
1284 HCOLORSPACE WINAPI GetColorSpace(HDC hdc)
1286 /*FIXME Need to to whatever GetColorSpace actually does */
1290 /***********************************************************************
1291 * CreateColorSpaceA (GDI32.???)
1293 HCOLORSPACE WINAPI CreateColorSpaceA( LPLOGCOLORSPACEA lpLogColorSpace )
1299 /***********************************************************************
1300 * CreateColorSpaceW (GDI32.???)
1302 HCOLORSPACE WINAPI CreateColorSpaceW( LPLOGCOLORSPACEW lpLogColorSpace )
1308 /***********************************************************************
1309 * DeleteColorSpace (GDI32.???)
1311 BOOL WINAPI DeleteColorSpace( HCOLORSPACE hColorSpace )
1318 /***********************************************************************
1319 * SetColorSpace (GDI32.???)
1321 HCOLORSPACE WINAPI SetColorSpace( HDC hDC, HCOLORSPACE hColorSpace )
1328 /***********************************************************************
1329 * GetBoundsRect16 (GDI.194)
1331 UINT16 WINAPI GetBoundsRect16(HDC16 hdc, LPRECT16 rect, UINT16 flags)
1333 return DCB_RESET | DCB_DISABLE; /* bounding rectangle always empty and disabled*/
1336 /***********************************************************************
1337 * GetBoundsRect (GDI32.147)
1339 UINT WINAPI GetBoundsRect(HDC hdc, LPRECT rect, UINT flags)
1341 FIXME("(): stub\n");
1342 return DCB_RESET; /* bounding rectangle always empty */
1345 /***********************************************************************
1346 * SetBoundsRect16 (GDI.193)
1348 UINT16 WINAPI SetBoundsRect16(HDC16 hdc, const RECT16* rect, UINT16 flags)
1350 if ( (flags & DCB_ACCUMULATE) || (flags & DCB_ENABLE) )
1351 FIXME("(%04x, %p, %04x): stub\n", hdc, rect, flags );
1353 return DCB_RESET | DCB_DISABLE; /* bounding rectangle always empty and disabled*/
1356 /***********************************************************************
1357 * SetBoundsRect (GDI32.307)
1359 UINT WINAPI SetBoundsRect(HDC hdc, const RECT* rect, UINT flags)
1361 FIXME("(): stub\n");
1362 return DCB_DISABLE; /* bounding rectangle always empty */
1366 /***********************************************************************
1367 * GetRelAbs (GDI32.218)
1369 INT WINAPI GetRelAbs( HDC hdc, DWORD dwIgnore )
1371 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1373 return dc->w.relAbsMode;
1376 /***********************************************************************
1379 * Disables GDI, switches back to text mode.
1380 * We don't have to do anything here,
1381 * just let console support handle everything
1383 void WINAPI Death16(HDC16 hDC)
1385 MESSAGE("Death(%04x) called. Application enters text mode...\n", hDC);
1388 /***********************************************************************
1389 * Resurrection (GDI.122)
1391 * Restores GDI functionality
1393 void WINAPI Resurrection16(HDC16 hDC,
1394 WORD w1, WORD w2, WORD w3, WORD w4, WORD w5, WORD w6)
1396 MESSAGE("Resurrection(%04x, %04x, %04x, %04x, %04x, %04x, %04x) called. Application left text mode.\n", hDC, w1, w2, w3, w4, w5, w6);
1399 /***********************************************************************
1400 * GetLayout (GDI32.321)
1402 * Gets left->right or right->left text layout flags of a dc.
1403 * win98 just returns 0 and sets ERROR_CALL_NOT_IMPLEMENTED so we do the same
1406 DWORD WINAPI GetLayout(HDC hdc)
1408 FIXME("(%08x): stub\n", hdc);
1409 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1413 /***********************************************************************
1414 * SetLayout (GDI32.450)
1416 * Sets left->right or right->left text layout flags of a dc.
1417 * win98 just returns 0 and sets ERROR_CALL_NOT_IMPLEMENTED so we do the same
1420 DWORD WINAPI SetLayout(HDC hdc, DWORD layout)
1422 FIXME("(%08x,%08lx): stub\n", hdc, layout);
1423 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);