2 * GDI Device Context functions
4 * Copyright 1993 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "wine/debug.h"
32 #include "wine/winuser16.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(dc);
36 /* ### start build ### */
37 extern WORD CALLBACK GDI_CallTo16_word_wwll(FARPROC16,WORD,WORD,LONG,LONG);
38 /* ### stop build ### */
40 static BOOL DC_DeleteObject( HGDIOBJ handle, void *obj );
42 static const struct gdi_obj_funcs dc_funcs =
44 NULL, /* pSelectObject */
45 NULL, /* pGetObject16 */
46 NULL, /* pGetObjectA */
47 NULL, /* pGetObjectW */
48 NULL, /* pUnrealizeObject */
49 DC_DeleteObject /* pDeleteObject */
52 /***********************************************************************
55 DC *DC_AllocDC( const DC_FUNCTIONS *funcs )
60 if (!(dc = GDI_AllocObject( sizeof(*dc), DC_MAGIC, &hdc, &dc_funcs ))) return NULL;
81 dc->hPen = GetStockObject( BLACK_PEN );
82 dc->hBrush = GetStockObject( WHITE_BRUSH );
83 dc->hFont = GetStockObject( SYSTEM_FONT );
86 dc->hPalette = GetStockObject( DEFAULT_PALETTE );
88 dc->ROPmode = R2_COPYPEN;
89 dc->polyFillMode = ALTERNATE;
90 dc->stretchBltMode = BLACKONWHITE;
91 dc->relAbsMode = ABSOLUTE;
92 dc->backgroundMode = OPAQUE;
93 dc->backgroundColor = RGB( 255, 255, 255 );
94 dc->textColor = RGB( 0, 0, 0 );
97 dc->textAlign = TA_LEFT | TA_TOP | TA_NOUPDATECP;
99 dc->breakTotalExtra = 0;
103 dc->totalExtent.left = 0;
104 dc->totalExtent.top = 0;
105 dc->totalExtent.right = 0;
106 dc->totalExtent.bottom = 0;
107 dc->bitsPerPixel = 1;
108 dc->MapMode = MM_TEXT;
109 dc->GraphicsMode = GM_COMPATIBLE;
110 dc->pAbortProc = NULL;
113 dc->ArcDirection = AD_COUNTERCLOCKWISE;
114 dc->xformWorld2Wnd.eM11 = 1.0f;
115 dc->xformWorld2Wnd.eM12 = 0.0f;
116 dc->xformWorld2Wnd.eM21 = 0.0f;
117 dc->xformWorld2Wnd.eM22 = 1.0f;
118 dc->xformWorld2Wnd.eDx = 0.0f;
119 dc->xformWorld2Wnd.eDy = 0.0f;
120 dc->xformWorld2Vport = dc->xformWorld2Wnd;
121 dc->xformVport2World = dc->xformWorld2Wnd;
122 dc->vport2WorldValid = TRUE;
123 PATH_InitGdiPath(&dc->path);
129 /***********************************************************************
132 DC *DC_GetDCPtr( HDC hdc )
134 GDIOBJHDR *ptr = GDI_GetObjPtr( hdc, MAGIC_DONTCARE );
135 if (!ptr) return NULL;
136 if ((GDIMAGIC(ptr->wMagic) == DC_MAGIC) ||
137 (GDIMAGIC(ptr->wMagic) == METAFILE_DC_MAGIC) ||
138 (GDIMAGIC(ptr->wMagic) == ENHMETAFILE_DC_MAGIC))
140 GDI_ReleaseObj( hdc );
141 SetLastError( ERROR_INVALID_HANDLE );
145 /***********************************************************************
148 * Retrieve a DC ptr while making sure the visRgn is updated.
149 * This function may call up to USER so the GDI lock should _not_
150 * be held when calling it.
152 DC *DC_GetDCUpdate( HDC hdc )
154 DC *dc = DC_GetDCPtr( hdc );
155 if (!dc) return NULL;
156 while (dc->flags & DC_DIRTY)
158 dc->flags &= ~DC_DIRTY;
159 if (!(dc->flags & (DC_SAVED | DC_MEMORY)))
161 DCHOOKPROC proc = dc->hookThunk;
164 DWORD data = dc->dwHookData;
165 GDI_ReleaseObj( hdc );
166 proc( hdc, DCHC_INVALIDVISRGN, data, 0 );
167 if (!(dc = DC_GetDCPtr( hdc ))) break;
168 /* otherwise restart the loop in case it became dirty again in the meantime */
176 /***********************************************************************
179 static BOOL DC_DeleteObject( HGDIOBJ handle, void *obj )
181 GDI_ReleaseObj( handle );
182 return DeleteDC( handle );
186 /***********************************************************************
189 * Setup device-specific DC values for a newly created DC.
191 void DC_InitDC( DC* dc )
193 RealizeDefaultPalette16( dc->hSelf );
194 SetTextColor( dc->hSelf, dc->textColor );
195 SetBkColor( dc->hSelf, dc->backgroundColor );
196 SelectObject( dc->hSelf, dc->hPen );
197 SelectObject( dc->hSelf, dc->hBrush );
198 SelectObject( dc->hSelf, dc->hFont );
199 CLIPPING_UpdateGCRegion( dc );
203 /***********************************************************************
206 * Computes the inverse of the transformation xformSrc and stores it to
207 * xformDest. Returns TRUE if successful or FALSE if the xformSrc matrix
210 static BOOL DC_InvertXform( const XFORM *xformSrc, XFORM *xformDest )
214 determinant = xformSrc->eM11*xformSrc->eM22 -
215 xformSrc->eM12*xformSrc->eM21;
216 if (determinant > -1e-12 && determinant < 1e-12)
219 xformDest->eM11 = xformSrc->eM22 / determinant;
220 xformDest->eM12 = -xformSrc->eM12 / determinant;
221 xformDest->eM21 = -xformSrc->eM21 / determinant;
222 xformDest->eM22 = xformSrc->eM11 / determinant;
223 xformDest->eDx = -xformSrc->eDx * xformDest->eM11 -
224 xformSrc->eDy * xformDest->eM21;
225 xformDest->eDy = -xformSrc->eDx * xformDest->eM12 -
226 xformSrc->eDy * xformDest->eM22;
232 /***********************************************************************
235 * Updates the xformWorld2Vport, xformVport2World and vport2WorldValid
236 * fields of the specified DC by creating a transformation that
237 * represents the current mapping mode and combining it with the DC's
238 * world transform. This function should be called whenever the
239 * parameters associated with the mapping mode (window and viewport
240 * extents and origins) or the world transform change.
242 void DC_UpdateXforms( DC *dc )
244 XFORM xformWnd2Vport;
245 FLOAT scaleX, scaleY;
247 /* Construct a transformation to do the window-to-viewport conversion */
248 scaleX = (FLOAT)dc->vportExtX / (FLOAT)dc->wndExtX;
249 scaleY = (FLOAT)dc->vportExtY / (FLOAT)dc->wndExtY;
250 xformWnd2Vport.eM11 = scaleX;
251 xformWnd2Vport.eM12 = 0.0;
252 xformWnd2Vport.eM21 = 0.0;
253 xformWnd2Vport.eM22 = scaleY;
254 xformWnd2Vport.eDx = (FLOAT)dc->vportOrgX -
255 scaleX * (FLOAT)dc->wndOrgX;
256 xformWnd2Vport.eDy = (FLOAT)dc->vportOrgY -
257 scaleY * (FLOAT)dc->wndOrgY;
259 /* Combine with the world transformation */
260 CombineTransform( &dc->xformWorld2Vport, &dc->xformWorld2Wnd,
263 /* Create inverse of world-to-viewport transformation */
264 dc->vport2WorldValid = DC_InvertXform( &dc->xformWorld2Vport,
265 &dc->xformVport2World );
269 /***********************************************************************
270 * GetDCState (GDI.179)
272 HDC16 WINAPI GetDCState16( HDC16 hdc )
277 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
278 if (!(newdc = GDI_AllocObject( sizeof(DC), DC_MAGIC, &handle, &dc_funcs )))
280 GDI_ReleaseObj( hdc );
283 TRACE("(%04x): returning %04x\n", hdc, handle );
285 newdc->flags = dc->flags | DC_SAVED;
286 newdc->hPen = dc->hPen;
287 newdc->hBrush = dc->hBrush;
288 newdc->hFont = dc->hFont;
289 newdc->hBitmap = dc->hBitmap;
290 newdc->hDevice = dc->hDevice;
291 newdc->hPalette = dc->hPalette;
292 newdc->totalExtent = dc->totalExtent;
293 newdc->bitsPerPixel = dc->bitsPerPixel;
294 newdc->ROPmode = dc->ROPmode;
295 newdc->polyFillMode = dc->polyFillMode;
296 newdc->stretchBltMode = dc->stretchBltMode;
297 newdc->relAbsMode = dc->relAbsMode;
298 newdc->backgroundMode = dc->backgroundMode;
299 newdc->backgroundColor = dc->backgroundColor;
300 newdc->textColor = dc->textColor;
301 newdc->brushOrgX = dc->brushOrgX;
302 newdc->brushOrgY = dc->brushOrgY;
303 newdc->textAlign = dc->textAlign;
304 newdc->charExtra = dc->charExtra;
305 newdc->breakTotalExtra = dc->breakTotalExtra;
306 newdc->breakCount = dc->breakCount;
307 newdc->breakExtra = dc->breakExtra;
308 newdc->breakRem = dc->breakRem;
309 newdc->MapMode = dc->MapMode;
310 newdc->GraphicsMode = dc->GraphicsMode;
311 newdc->CursPosX = dc->CursPosX;
312 newdc->CursPosY = dc->CursPosY;
313 newdc->ArcDirection = dc->ArcDirection;
314 newdc->xformWorld2Wnd = dc->xformWorld2Wnd;
315 newdc->xformWorld2Vport = dc->xformWorld2Vport;
316 newdc->xformVport2World = dc->xformVport2World;
317 newdc->vport2WorldValid = dc->vport2WorldValid;
318 newdc->wndOrgX = dc->wndOrgX;
319 newdc->wndOrgY = dc->wndOrgY;
320 newdc->wndExtX = dc->wndExtX;
321 newdc->wndExtY = dc->wndExtY;
322 newdc->vportOrgX = dc->vportOrgX;
323 newdc->vportOrgY = dc->vportOrgY;
324 newdc->vportExtX = dc->vportExtX;
325 newdc->vportExtY = dc->vportExtY;
327 newdc->hSelf = (HDC)handle;
328 newdc->saveLevel = 0;
330 PATH_InitGdiPath( &newdc->path );
332 newdc->pAbortProc = NULL;
333 newdc->hookThunk = NULL;
336 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
338 newdc->hGCClipRgn = newdc->hVisRgn = 0;
341 newdc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
342 CombineRgn( newdc->hClipRgn, dc->hClipRgn, 0, RGN_COPY );
348 newdc->gdiFont = dc->gdiFont;
352 GDI_ReleaseObj( handle );
353 GDI_ReleaseObj( hdc );
358 /***********************************************************************
359 * SetDCState (GDI.180)
361 void WINAPI SetDCState16( HDC16 hdc, HDC16 hdcs )
365 if (!(dc = DC_GetDCUpdate( hdc ))) return;
366 if (!(dcs = DC_GetDCPtr( hdcs )))
368 GDI_ReleaseObj( hdc );
371 if (!dcs->flags & DC_SAVED)
373 GDI_ReleaseObj( hdc );
374 GDI_ReleaseObj( hdcs );
377 TRACE("%04x %04x\n", hdc, hdcs );
379 dc->flags = dcs->flags & ~(DC_SAVED | DC_DIRTY);
380 dc->hDevice = dcs->hDevice;
381 dc->totalExtent = dcs->totalExtent;
382 dc->ROPmode = dcs->ROPmode;
383 dc->polyFillMode = dcs->polyFillMode;
384 dc->stretchBltMode = dcs->stretchBltMode;
385 dc->relAbsMode = dcs->relAbsMode;
386 dc->backgroundMode = dcs->backgroundMode;
387 dc->backgroundColor = dcs->backgroundColor;
388 dc->textColor = dcs->textColor;
389 dc->brushOrgX = dcs->brushOrgX;
390 dc->brushOrgY = dcs->brushOrgY;
391 dc->textAlign = dcs->textAlign;
392 dc->charExtra = dcs->charExtra;
393 dc->breakTotalExtra = dcs->breakTotalExtra;
394 dc->breakCount = dcs->breakCount;
395 dc->breakExtra = dcs->breakExtra;
396 dc->breakRem = dcs->breakRem;
397 dc->MapMode = dcs->MapMode;
398 dc->GraphicsMode = dcs->GraphicsMode;
399 dc->CursPosX = dcs->CursPosX;
400 dc->CursPosY = dcs->CursPosY;
401 dc->ArcDirection = dcs->ArcDirection;
402 dc->xformWorld2Wnd = dcs->xformWorld2Wnd;
403 dc->xformWorld2Vport = dcs->xformWorld2Vport;
404 dc->xformVport2World = dcs->xformVport2World;
405 dc->vport2WorldValid = dcs->vport2WorldValid;
407 dc->wndOrgX = dcs->wndOrgX;
408 dc->wndOrgY = dcs->wndOrgY;
409 dc->wndExtX = dcs->wndExtX;
410 dc->wndExtY = dcs->wndExtY;
411 dc->vportOrgX = dcs->vportOrgX;
412 dc->vportOrgY = dcs->vportOrgY;
413 dc->vportExtX = dcs->vportExtX;
414 dc->vportExtY = dcs->vportExtY;
416 if (!(dc->flags & DC_MEMORY)) dc->bitsPerPixel = dcs->bitsPerPixel;
420 if (!dc->hClipRgn) dc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
421 CombineRgn( dc->hClipRgn, dcs->hClipRgn, 0, RGN_COPY );
425 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
428 CLIPPING_UpdateGCRegion( dc );
430 SelectObject( hdc, dcs->hBitmap );
431 SelectObject( hdc, dcs->hBrush );
432 SelectObject( hdc, dcs->hFont );
433 SelectObject( hdc, dcs->hPen );
434 SetBkColor( hdc, dcs->backgroundColor);
435 SetTextColor( hdc, dcs->textColor);
436 GDISelectPalette16( hdc, dcs->hPalette, FALSE );
437 GDI_ReleaseObj( hdcs );
438 GDI_ReleaseObj( hdc );
442 /***********************************************************************
445 INT16 WINAPI SaveDC16( HDC16 hdc )
447 return (INT16)SaveDC( hdc );
451 /***********************************************************************
454 INT WINAPI SaveDC( HDC hdc )
460 dc = DC_GetDCPtr( hdc );
463 if(dc->funcs->pSaveDC)
465 ret = dc->funcs->pSaveDC( dc->physDev );
466 GDI_ReleaseObj( hdc );
470 if (!(hdcs = GetDCState16( hdc )))
472 GDI_ReleaseObj( hdc );
475 dcs = DC_GetDCPtr( hdcs );
477 /* Copy path. The reason why path saving / restoring is in SaveDC/
478 * RestoreDC and not in GetDCState/SetDCState is that the ...DCState
479 * functions are only in Win16 (which doesn't have paths) and that
480 * SetDCState doesn't allow us to signal an error (which can happen
481 * when copying paths).
483 if (!PATH_AssignGdiPath( &dcs->path, &dc->path ))
485 GDI_ReleaseObj( hdc );
486 GDI_ReleaseObj( hdcs );
491 dcs->header.hNext = dc->header.hNext;
492 dc->header.hNext = hdcs;
493 TRACE("(%04x): returning %d\n", hdc, dc->saveLevel+1 );
494 ret = ++dc->saveLevel;
495 GDI_ReleaseObj( hdcs );
496 GDI_ReleaseObj( hdc );
501 /***********************************************************************
504 BOOL16 WINAPI RestoreDC16( HDC16 hdc, INT16 level )
506 return RestoreDC( hdc, level );
510 /***********************************************************************
511 * RestoreDC (GDI32.@)
513 BOOL WINAPI RestoreDC( HDC hdc, INT level )
518 TRACE("%04x %d\n", hdc, level );
519 dc = DC_GetDCUpdate( hdc );
520 if(!dc) return FALSE;
521 if(dc->funcs->pRestoreDC)
523 success = dc->funcs->pRestoreDC( dc->physDev, level );
524 GDI_ReleaseObj( hdc );
528 if (level == -1) level = dc->saveLevel;
530 /* This pair of checks disagrees with MSDN "Platform SDK:
531 Windows GDI" July 2000 which says all negative values
532 for level will be interpreted as an instance relative
533 to the current state. Restricting it to just -1 does
535 || (level > dc->saveLevel))
537 GDI_ReleaseObj( hdc );
542 while (dc->saveLevel >= level)
544 HDC16 hdcs = dc->header.hNext;
545 if (!(dcs = DC_GetDCPtr( hdcs )))
547 GDI_ReleaseObj( hdc );
550 dc->header.hNext = dcs->header.hNext;
551 if (--dc->saveLevel < level)
553 SetDCState16( hdc, hdcs );
554 if (!PATH_AssignGdiPath( &dc->path, &dcs->path ))
555 /* FIXME: This might not be quite right, since we're
556 * returning FALSE but still destroying the saved DC state */
559 GDI_ReleaseObj( hdcs );
560 GDI_ReleaseObj( hdc );
562 if (!(dc = DC_GetDCPtr( hdc ))) return FALSE;
564 GDI_ReleaseObj( hdc );
569 /***********************************************************************
572 HDC16 WINAPI CreateDC16( LPCSTR driver, LPCSTR device, LPCSTR output,
573 const DEVMODEA *initData )
575 return CreateDCA( driver, device, output, initData );
578 /***********************************************************************
579 * CreateDCA (GDI32.@)
581 HDC WINAPI CreateDCA( LPCSTR driver, LPCSTR device, LPCSTR output,
582 const DEVMODEA *initData )
586 const DC_FUNCTIONS *funcs;
589 if ((!driver) && (!device))
594 if (!device || !DRIVER_GetDriverName( device, buf, sizeof(buf) ))
597 if (!(funcs = DRIVER_load_driver( buf )))
599 ERR( "no driver found for %s\n", buf );
602 if (!(dc = DC_AllocDC( funcs )))
604 DRIVER_release_driver( funcs );
610 TRACE("(driver=%s, device=%s, output=%s): returning %04x\n",
611 debugstr_a(driver), debugstr_a(device), debugstr_a(output), dc->hSelf );
613 if (dc->funcs->pCreateDC &&
614 !dc->funcs->pCreateDC( dc, &dc->physDev, buf, device, output, initData ))
616 WARN("creation aborted by device\n" );
617 GDI_FreeObject( dc->hSelf, dc );
618 DRIVER_release_driver( funcs );
622 dc->totalExtent.left = 0;
623 dc->totalExtent.top = 0;
624 dc->totalExtent.right = GetDeviceCaps( dc->hSelf, HORZRES );
625 dc->totalExtent.bottom = GetDeviceCaps( dc->hSelf, VERTRES );
626 dc->hVisRgn = CreateRectRgnIndirect( &dc->totalExtent );
630 GDI_ReleaseObj( hdc );
635 /***********************************************************************
636 * CreateDCW (GDI32.@)
638 HDC WINAPI CreateDCW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
639 const DEVMODEW *initData )
641 LPSTR driverA = HEAP_strdupWtoA( GetProcessHeap(), 0, driver );
642 LPSTR deviceA = HEAP_strdupWtoA( GetProcessHeap(), 0, device );
643 LPSTR outputA = HEAP_strdupWtoA( GetProcessHeap(), 0, output );
644 HDC res = CreateDCA( driverA, deviceA, outputA,
645 (const DEVMODEA *)initData /*FIXME*/ );
646 HeapFree( GetProcessHeap(), 0, driverA );
647 HeapFree( GetProcessHeap(), 0, deviceA );
648 HeapFree( GetProcessHeap(), 0, outputA );
653 /***********************************************************************
656 HDC16 WINAPI CreateIC16( LPCSTR driver, LPCSTR device, LPCSTR output,
657 const DEVMODEA* initData )
659 /* Nothing special yet for ICs */
660 return CreateDC16( driver, device, output, initData );
664 /***********************************************************************
665 * CreateICA (GDI32.@)
667 HDC WINAPI CreateICA( LPCSTR driver, LPCSTR device, LPCSTR output,
668 const DEVMODEA* initData )
670 /* Nothing special yet for ICs */
671 return CreateDCA( driver, device, output, initData );
675 /***********************************************************************
676 * CreateICW (GDI32.@)
678 HDC WINAPI CreateICW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
679 const DEVMODEW* initData )
681 /* Nothing special yet for ICs */
682 return CreateDCW( driver, device, output, initData );
686 /***********************************************************************
687 * CreateCompatibleDC (GDI.52)
689 HDC16 WINAPI CreateCompatibleDC16( HDC16 hdc )
691 return (HDC16)CreateCompatibleDC( hdc );
695 /***********************************************************************
696 * CreateCompatibleDC (GDI32.@)
698 HDC WINAPI CreateCompatibleDC( HDC hdc )
701 const DC_FUNCTIONS *funcs;
705 if ((origDC = GDI_GetObjPtr( hdc, DC_MAGIC )))
707 funcs = origDC->funcs;
708 GDI_ReleaseObj( hdc ); /* can't hold the lock while loading the driver */
709 funcs = DRIVER_get_driver( funcs );
711 else funcs = DRIVER_load_driver( "DISPLAY" );
713 if (!funcs) return 0;
715 if (!(dc = DC_AllocDC( funcs )))
717 DRIVER_release_driver( funcs );
721 TRACE("(%04x): returning %04x\n",
724 dc->flags = DC_MEMORY;
725 dc->bitsPerPixel = 1;
726 dc->hBitmap = GetStockObject( DEFAULT_BITMAP );
728 /* Copy the driver-specific physical device info into
729 * the new DC. The driver may use this read-only info
730 * while creating the compatible DC below. */
731 if ((origDC = GDI_GetObjPtr( hdc, DC_MAGIC ))) dc->physDev = origDC->physDev;
733 if (dc->funcs->pCreateDC &&
734 !dc->funcs->pCreateDC( dc, &dc->physDev, NULL, NULL, NULL, NULL ))
736 WARN("creation aborted by device\n");
737 GDI_FreeObject( dc->hSelf, dc );
738 if (origDC) GDI_ReleaseObj( hdc );
739 DRIVER_release_driver( funcs );
743 dc->totalExtent.left = 0;
744 dc->totalExtent.top = 0;
745 dc->totalExtent.right = 1; /* default bitmap is 1x1 */
746 dc->totalExtent.bottom = 1;
747 dc->hVisRgn = CreateRectRgnIndirect( &dc->totalExtent );
750 GDI_ReleaseObj( dc->hSelf );
751 if (origDC) GDI_ReleaseObj( hdc );
756 /***********************************************************************
759 BOOL16 WINAPI DeleteDC16( HDC16 hdc )
761 return DeleteDC( hdc );
765 /***********************************************************************
768 BOOL WINAPI DeleteDC( HDC hdc )
770 const DC_FUNCTIONS *funcs = NULL;
773 TRACE("%04x\n", hdc );
777 if (!(dc = GDI_GetObjPtr( hdc, DC_MAGIC ))) return FALSE;
779 /* Call hook procedure to check whether is it OK to delete this DC */
780 if (dc->hookThunk && !(dc->flags & (DC_SAVED | DC_MEMORY)))
782 DCHOOKPROC proc = dc->hookThunk;
785 DWORD data = dc->dwHookData;
786 GDI_ReleaseObj( hdc );
787 if (!proc( hdc, DCHC_DELETEDC, data, 0 )) return FALSE;
788 if (!(dc = DC_GetDCPtr( hdc ))) return TRUE; /* deleted by the hook */
792 while (dc->saveLevel)
795 HDC16 hdcs = dc->header.hNext;
796 if (!(dcs = DC_GetDCPtr( hdcs ))) break;
797 dc->header.hNext = dcs->header.hNext;
799 if (dcs->hClipRgn) DeleteObject( dcs->hClipRgn );
800 if (dcs->hVisRgn) DeleteObject( dcs->hVisRgn );
801 if (dcs->hGCClipRgn) DeleteObject( dcs->hGCClipRgn );
802 PATH_DestroyGdiPath(&dcs->path);
803 GDI_FreeObject( hdcs, dcs );
806 if (!(dc->flags & DC_SAVED))
808 SelectObject( hdc, GetStockObject(BLACK_PEN) );
809 SelectObject( hdc, GetStockObject(WHITE_BRUSH) );
810 SelectObject( hdc, GetStockObject(SYSTEM_FONT) );
811 SelectObject( hdc, GetStockObject(DEFAULT_BITMAP) );
813 if (dc->funcs->pDeleteDC) dc->funcs->pDeleteDC(dc->physDev);
817 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
818 if (dc->hVisRgn) DeleteObject( dc->hVisRgn );
819 if (dc->hGCClipRgn) DeleteObject( dc->hGCClipRgn );
820 PATH_DestroyGdiPath(&dc->path);
822 GDI_FreeObject( hdc, dc );
823 if (funcs) DRIVER_release_driver( funcs ); /* do that after releasing the GDI lock */
828 /***********************************************************************
831 HDC16 WINAPI ResetDC16( HDC16 hdc, const DEVMODEA *devmode )
833 return ResetDCA(hdc, devmode);
837 /***********************************************************************
840 HDC WINAPI ResetDCA( HDC hdc, const DEVMODEA *devmode )
845 if ((dc = DC_GetDCPtr( hdc )))
847 if (dc->funcs->pResetDC) ret = dc->funcs->pResetDC( dc->physDev, devmode );
848 GDI_ReleaseObj( hdc );
854 /***********************************************************************
857 HDC WINAPI ResetDCW( HDC hdc, const DEVMODEW *devmode )
859 return ResetDCA(hdc, (const DEVMODEA*)devmode); /* FIXME */
863 /***********************************************************************
864 * GetDeviceCaps (GDI.80)
866 INT16 WINAPI GetDeviceCaps16( HDC16 hdc, INT16 cap )
868 INT16 ret = GetDeviceCaps( hdc, cap );
869 /* some apps don't expect -1 and think it's a B&W screen */
870 if ((cap == NUMCOLORS) && (ret == -1)) ret = 2048;
875 /***********************************************************************
876 * GetDeviceCaps (GDI32.@)
878 INT WINAPI GetDeviceCaps( HDC hdc, INT cap )
883 if ((dc = DC_GetDCPtr( hdc )))
885 if (dc->funcs->pGetDeviceCaps) ret = dc->funcs->pGetDeviceCaps( dc->physDev, cap );
886 GDI_ReleaseObj( hdc );
892 /***********************************************************************
895 COLORREF WINAPI SetBkColor16( HDC16 hdc, COLORREF color )
897 return SetBkColor( hdc, color );
901 /***********************************************************************
902 * SetBkColor (GDI32.@)
904 COLORREF WINAPI SetBkColor( HDC hdc, COLORREF color )
907 DC * dc = DC_GetDCPtr( hdc );
909 if (!dc) return CLR_INVALID;
910 oldColor = dc->backgroundColor;
911 if (dc->funcs->pSetBkColor)
913 color = dc->funcs->pSetBkColor(dc->physDev, color);
914 if (color == CLR_INVALID) /* don't change it */
917 oldColor = CLR_INVALID;
920 dc->backgroundColor = color;
921 GDI_ReleaseObj( hdc );
926 /***********************************************************************
927 * SetTextColor (GDI.9)
929 COLORREF WINAPI SetTextColor16( HDC16 hdc, COLORREF color )
931 return SetTextColor( hdc, color );
935 /***********************************************************************
936 * SetTextColor (GDI32.@)
938 COLORREF WINAPI SetTextColor( HDC hdc, COLORREF color )
941 DC * dc = DC_GetDCPtr( hdc );
943 if (!dc) return CLR_INVALID;
944 oldColor = dc->textColor;
945 if (dc->funcs->pSetTextColor)
947 color = dc->funcs->pSetTextColor(dc->physDev, color);
948 if (color == CLR_INVALID) /* don't change it */
951 oldColor = CLR_INVALID;
954 dc->textColor = color;
955 GDI_ReleaseObj( hdc );
959 /***********************************************************************
960 * SetTextAlign (GDI.346)
962 UINT16 WINAPI SetTextAlign16( HDC16 hdc, UINT16 align )
964 return SetTextAlign( hdc, align );
968 /***********************************************************************
969 * SetTextAlign (GDI32.@)
971 UINT WINAPI SetTextAlign( HDC hdc, UINT align )
974 DC *dc = DC_GetDCPtr( hdc );
976 if (dc->funcs->pSetTextAlign)
977 prevAlign = dc->funcs->pSetTextAlign(dc->physDev, align);
979 prevAlign = dc->textAlign;
980 dc->textAlign = align;
982 GDI_ReleaseObj( hdc );
986 /***********************************************************************
987 * GetDCOrgEx (GDI32.@)
989 BOOL WINAPI GetDCOrgEx( HDC hDC, LPPOINT lpp )
993 if (!lpp) return FALSE;
994 if (!(dc = DC_GetDCPtr( hDC ))) return FALSE;
997 if (dc->funcs->pGetDCOrgEx) dc->funcs->pGetDCOrgEx( dc->physDev, lpp );
998 GDI_ReleaseObj( hDC );
1003 /***********************************************************************
1006 DWORD WINAPI GetDCOrg16( HDC16 hdc )
1009 if( GetDCOrgEx( hdc, &pt) )
1010 return MAKELONG( (WORD)pt.x, (WORD)pt.y );
1015 /***********************************************************************
1016 * SetDCOrg (GDI.117)
1018 DWORD WINAPI SetDCOrg16( HDC16 hdc, INT16 x, INT16 y )
1021 DC *dc = DC_GetDCPtr( hdc );
1023 if (dc->funcs->pSetDCOrg) prevOrg = dc->funcs->pSetDCOrg( dc->physDev, x, y );
1024 GDI_ReleaseObj( hdc );
1029 /***********************************************************************
1030 * SetGraphicsMode (GDI32.@)
1032 INT WINAPI SetGraphicsMode( HDC hdc, INT mode )
1035 DC *dc = DC_GetDCPtr( hdc );
1037 /* One would think that setting the graphics mode to GM_COMPATIBLE
1038 * would also reset the world transformation matrix to the unity
1039 * matrix. However, in Windows, this is not the case. This doesn't
1040 * make a lot of sense to me, but that's the way it is.
1043 if ((mode > 0) || (mode <= GM_LAST))
1045 ret = dc->GraphicsMode;
1046 dc->GraphicsMode = mode;
1048 GDI_ReleaseObj( hdc );
1053 /***********************************************************************
1054 * SetArcDirection (GDI.525)
1056 INT16 WINAPI SetArcDirection16( HDC16 hdc, INT16 nDirection )
1058 return SetArcDirection( (HDC)hdc, (INT)nDirection );
1062 /***********************************************************************
1063 * SetArcDirection (GDI32.@)
1065 INT WINAPI SetArcDirection( HDC hdc, INT nDirection )
1068 INT nOldDirection = 0;
1070 if (nDirection!=AD_COUNTERCLOCKWISE && nDirection!=AD_CLOCKWISE)
1072 SetLastError(ERROR_INVALID_PARAMETER);
1076 if ((dc = DC_GetDCPtr( hdc )))
1078 nOldDirection = dc->ArcDirection;
1079 dc->ArcDirection = nDirection;
1080 GDI_ReleaseObj( hdc );
1082 return nOldDirection;
1086 /***********************************************************************
1087 * GetWorldTransform (GDI32.@)
1089 BOOL WINAPI GetWorldTransform( HDC hdc, LPXFORM xform )
1092 if (!xform) return FALSE;
1093 if (!(dc = DC_GetDCPtr( hdc ))) return FALSE;
1094 *xform = dc->xformWorld2Wnd;
1095 GDI_ReleaseObj( hdc );
1100 /***********************************************************************
1101 * SetWorldTransform (GDI32.@)
1103 BOOL WINAPI SetWorldTransform( HDC hdc, const XFORM *xform )
1106 DC *dc = DC_GetDCPtr( hdc );
1108 if (!dc) return FALSE;
1109 if (!xform) goto done;
1111 /* Check that graphics mode is GM_ADVANCED */
1112 if (dc->GraphicsMode!=GM_ADVANCED) goto done;
1114 dc->xformWorld2Wnd = *xform;
1115 DC_UpdateXforms( dc );
1118 GDI_ReleaseObj( hdc );
1123 /****************************************************************************
1124 * ModifyWorldTransform [GDI32.@]
1125 * Modifies the world transformation for a device context.
1128 * hdc [I] Handle to device context
1129 * xform [I] XFORM structure that will be used to modify the world
1131 * iMode [I] Specifies in what way to modify the world transformation
1134 * Resets the world transformation to the identity matrix.
1135 * The parameter xform is ignored.
1137 * Multiplies xform into the world transformation matrix from
1140 * Multiplies xform into the world transformation matrix from
1145 BOOL WINAPI ModifyWorldTransform( HDC hdc, const XFORM *xform,
1149 DC *dc = DC_GetDCPtr( hdc );
1151 /* Check for illegal parameters */
1152 if (!dc) return FALSE;
1153 if (!xform) goto done;
1155 /* Check that graphics mode is GM_ADVANCED */
1156 if (dc->GraphicsMode!=GM_ADVANCED) goto done;
1161 dc->xformWorld2Wnd.eM11 = 1.0f;
1162 dc->xformWorld2Wnd.eM12 = 0.0f;
1163 dc->xformWorld2Wnd.eM21 = 0.0f;
1164 dc->xformWorld2Wnd.eM22 = 1.0f;
1165 dc->xformWorld2Wnd.eDx = 0.0f;
1166 dc->xformWorld2Wnd.eDy = 0.0f;
1168 case MWT_LEFTMULTIPLY:
1169 CombineTransform( &dc->xformWorld2Wnd, xform,
1170 &dc->xformWorld2Wnd );
1172 case MWT_RIGHTMULTIPLY:
1173 CombineTransform( &dc->xformWorld2Wnd, &dc->xformWorld2Wnd,
1180 DC_UpdateXforms( dc );
1183 GDI_ReleaseObj( hdc );
1188 /****************************************************************************
1189 * CombineTransform [GDI32.@]
1190 * Combines two transformation matrices.
1193 * xformResult [O] Stores the result of combining the two matrices
1194 * xform1 [I] Specifies the first matrix to apply
1195 * xform2 [I] Specifies the second matrix to apply
1198 * The same matrix can be passed in for more than one of the parameters.
1202 BOOL WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
1203 const XFORM *xform2 )
1207 /* Check for illegal parameters */
1208 if (!xformResult || !xform1 || !xform2)
1211 /* Create the result in a temporary XFORM, since xformResult may be
1212 * equal to xform1 or xform2 */
1213 xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
1214 xform1->eM12 * xform2->eM21;
1215 xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
1216 xform1->eM12 * xform2->eM22;
1217 xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
1218 xform1->eM22 * xform2->eM21;
1219 xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
1220 xform1->eM22 * xform2->eM22;
1221 xformTemp.eDx = xform1->eDx * xform2->eM11 +
1222 xform1->eDy * xform2->eM21 +
1224 xformTemp.eDy = xform1->eDx * xform2->eM12 +
1225 xform1->eDy * xform2->eM22 +
1228 /* Copy the result to xformResult */
1229 *xformResult = xformTemp;
1235 /***********************************************************************
1236 * SetDCHook (GDI32.@)
1238 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1240 BOOL WINAPI SetDCHook( HDC hdc, DCHOOKPROC hookProc, DWORD dwHookData )
1242 DC *dc = DC_GetDCPtr( hdc );
1244 if (!dc) return FALSE;
1245 dc->dwHookData = dwHookData;
1246 dc->hookThunk = hookProc;
1247 GDI_ReleaseObj( hdc );
1252 /* relay function to call the 16-bit DC hook proc */
1253 static BOOL16 WINAPI call_dc_hook16( HDC16 hdc, WORD code, DWORD data, LPARAM lParam )
1255 FARPROC16 proc = NULL;
1256 DC *dc = DC_GetDCPtr( hdc );
1257 if (!dc) return FALSE;
1258 proc = dc->hookProc;
1259 GDI_ReleaseObj( hdc );
1260 if (!proc) return FALSE;
1261 return GDI_CallTo16_word_wwll( proc, hdc, code, data, lParam );
1264 /***********************************************************************
1265 * SetDCHook (GDI.190)
1267 BOOL16 WINAPI SetDCHook16( HDC16 hdc, FARPROC16 hookProc, DWORD dwHookData )
1269 DC *dc = DC_GetDCPtr( hdc );
1270 if (!dc) return FALSE;
1272 dc->hookProc = hookProc;
1273 GDI_ReleaseObj( hdc );
1274 return SetDCHook( hdc, call_dc_hook16, dwHookData );
1278 /***********************************************************************
1279 * GetDCHook (GDI.191)
1281 DWORD WINAPI GetDCHook( HDC16 hdc, FARPROC16 *phookProc )
1283 DC *dc = DC_GetDCPtr( hdc );
1285 *phookProc = dc->hookProc;
1286 GDI_ReleaseObj( hdc );
1287 return dc->dwHookData;
1291 /***********************************************************************
1292 * SetHookFlags (GDI.192)
1294 WORD WINAPI SetHookFlags16(HDC16 hDC, WORD flags)
1296 DC *dc = DC_GetDCPtr( hDC );
1300 WORD wRet = dc->flags & DC_DIRTY;
1302 /* "Undocumented Windows" info is slightly confusing.
1305 TRACE("hDC %04x, flags %04x\n",hDC,flags);
1307 if( flags & DCHF_INVALIDATEVISRGN )
1308 dc->flags |= DC_DIRTY;
1309 else if( flags & DCHF_VALIDATEVISRGN || !flags )
1310 dc->flags &= ~DC_DIRTY;
1311 GDI_ReleaseObj( hDC );
1317 /***********************************************************************
1318 * SetICMMode (GDI32.@)
1320 INT WINAPI SetICMMode(HDC hdc, INT iEnableICM)
1322 /*FIXME Asuming that ICM is always off, and cannot be turned on */
1323 if (iEnableICM == ICM_OFF) return ICM_OFF;
1324 if (iEnableICM == ICM_ON) return 0;
1325 if (iEnableICM == ICM_QUERY) return ICM_OFF;
1329 /***********************************************************************
1330 * GetDeviceGammaRamp (GDI32.@)
1332 BOOL WINAPI GetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1335 DC *dc = DC_GetDCPtr( hDC );
1339 if (dc->funcs->pGetDeviceGammaRamp)
1340 ret = dc->funcs->pGetDeviceGammaRamp(dc->physDev, ptr);
1341 GDI_ReleaseObj( hDC );
1346 /***********************************************************************
1347 * SetDeviceGammaRamp (GDI32.@)
1349 BOOL WINAPI SetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1352 DC *dc = DC_GetDCPtr( hDC );
1356 if (dc->funcs->pSetDeviceGammaRamp)
1357 ret = dc->funcs->pSetDeviceGammaRamp(dc->physDev, ptr);
1358 GDI_ReleaseObj( hDC );
1363 /***********************************************************************
1364 * GetColorSpace (GDI32.@)
1366 HCOLORSPACE WINAPI GetColorSpace(HDC hdc)
1368 /*FIXME Need to to whatever GetColorSpace actually does */
1372 /***********************************************************************
1373 * CreateColorSpaceA (GDI32.@)
1375 HCOLORSPACE WINAPI CreateColorSpaceA( LPLOGCOLORSPACEA lpLogColorSpace )
1381 /***********************************************************************
1382 * CreateColorSpaceW (GDI32.@)
1384 HCOLORSPACE WINAPI CreateColorSpaceW( LPLOGCOLORSPACEW lpLogColorSpace )
1390 /***********************************************************************
1391 * DeleteColorSpace (GDI32.@)
1393 BOOL WINAPI DeleteColorSpace( HCOLORSPACE hColorSpace )
1400 /***********************************************************************
1401 * SetColorSpace (GDI32.@)
1403 HCOLORSPACE WINAPI SetColorSpace( HDC hDC, HCOLORSPACE hColorSpace )
1410 /***********************************************************************
1411 * GetBoundsRect (GDI.194)
1413 UINT16 WINAPI GetBoundsRect16(HDC16 hdc, LPRECT16 rect, UINT16 flags)
1415 return DCB_RESET | DCB_DISABLE; /* bounding rectangle always empty and disabled*/
1418 /***********************************************************************
1419 * GetBoundsRect (GDI32.@)
1421 UINT WINAPI GetBoundsRect(HDC hdc, LPRECT rect, UINT flags)
1423 FIXME("(): stub\n");
1424 return DCB_RESET; /* bounding rectangle always empty */
1427 /***********************************************************************
1428 * SetBoundsRect (GDI.193)
1430 UINT16 WINAPI SetBoundsRect16(HDC16 hdc, const RECT16* rect, UINT16 flags)
1432 if ( (flags & DCB_ACCUMULATE) || (flags & DCB_ENABLE) )
1433 FIXME("(%04x, %p, %04x): stub\n", hdc, rect, flags );
1435 return DCB_RESET | DCB_DISABLE; /* bounding rectangle always empty and disabled*/
1438 /***********************************************************************
1439 * SetBoundsRect (GDI32.@)
1441 UINT WINAPI SetBoundsRect(HDC hdc, const RECT* rect, UINT flags)
1443 FIXME("(): stub\n");
1444 return DCB_DISABLE; /* bounding rectangle always empty */
1448 /***********************************************************************
1449 * GetRelAbs (GDI32.@)
1451 INT WINAPI GetRelAbs( HDC hdc, DWORD dwIgnore )
1454 DC *dc = DC_GetDCPtr( hdc );
1455 if (dc) ret = dc->relAbsMode;
1456 GDI_ReleaseObj( hdc );
1460 /***********************************************************************
1463 * Disables GDI, switches back to text mode.
1464 * We don't have to do anything here,
1465 * just let console support handle everything
1467 void WINAPI Death16(HDC16 hDC)
1469 MESSAGE("Death(%04x) called. Application enters text mode...\n", hDC);
1472 /***********************************************************************
1473 * Resurrection (GDI.122)
1475 * Restores GDI functionality
1477 void WINAPI Resurrection16(HDC16 hDC,
1478 WORD w1, WORD w2, WORD w3, WORD w4, WORD w5, WORD w6)
1480 MESSAGE("Resurrection(%04x, %04x, %04x, %04x, %04x, %04x, %04x) called. Application left text mode.\n", hDC, w1, w2, w3, w4, w5, w6);
1483 /***********************************************************************
1484 * GetLayout (GDI32.@)
1486 * Gets left->right or right->left text layout flags of a dc.
1487 * win98 just returns 0 and sets ERROR_CALL_NOT_IMPLEMENTED so we do the same
1490 DWORD WINAPI GetLayout(HDC hdc)
1492 FIXME("(%08x): stub\n", hdc);
1493 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1497 /***********************************************************************
1498 * SetLayout (GDI32.@)
1500 * Sets left->right or right->left text layout flags of a dc.
1501 * win98 just returns 0 and sets ERROR_CALL_NOT_IMPLEMENTED so we do the same
1504 DWORD WINAPI SetLayout(HDC hdc, DWORD layout)
1506 FIXME("(%08x,%08lx): stub\n", hdc, layout);
1507 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);