2 * GDI Device Context functions
4 * Copyright 1993 Alexandre Julliard
14 #include "debugtools.h"
20 #include "wine/winuser16.h"
22 DEFAULT_DEBUG_CHANNEL(dc);
25 /***********************************************************************
28 DC *DC_AllocDC( const DC_FUNCTIONS *funcs )
33 if (!(dc = GDI_AllocObject( sizeof(*dc), DC_MAGIC, &hdc ))) return NULL;
54 dc->hPen = GetStockObject( BLACK_PEN );
55 dc->hBrush = GetStockObject( WHITE_BRUSH );
56 dc->hFont = GetStockObject( SYSTEM_FONT );
59 dc->hPalette = GetStockObject( DEFAULT_PALETTE );
60 dc->ROPmode = R2_COPYPEN;
61 dc->polyFillMode = ALTERNATE;
62 dc->stretchBltMode = BLACKONWHITE;
63 dc->relAbsMode = ABSOLUTE;
64 dc->backgroundMode = OPAQUE;
65 dc->backgroundColor = RGB( 255, 255, 255 );
66 dc->textColor = RGB( 0, 0, 0 );
69 dc->textAlign = TA_LEFT | TA_TOP | TA_NOUPDATECP;
71 dc->breakTotalExtra = 0;
75 dc->totalExtent.left = 0;
76 dc->totalExtent.top = 0;
77 dc->totalExtent.right = 0;
78 dc->totalExtent.bottom = 0;
80 dc->MapMode = MM_TEXT;
81 dc->GraphicsMode = GM_COMPATIBLE;
84 dc->pAbortProc = NULL;
87 dc->ArcDirection = AD_COUNTERCLOCKWISE;
88 dc->xformWorld2Wnd.eM11 = 1.0f;
89 dc->xformWorld2Wnd.eM12 = 0.0f;
90 dc->xformWorld2Wnd.eM21 = 0.0f;
91 dc->xformWorld2Wnd.eM22 = 1.0f;
92 dc->xformWorld2Wnd.eDx = 0.0f;
93 dc->xformWorld2Wnd.eDy = 0.0f;
94 dc->xformWorld2Vport = dc->xformWorld2Wnd;
95 dc->xformVport2World = dc->xformWorld2Wnd;
96 dc->vport2WorldValid = TRUE;
97 PATH_InitGdiPath(&dc->path);
103 /***********************************************************************
106 DC *DC_GetDCPtr( HDC hdc )
108 GDIOBJHDR *ptr = GDI_GetObjPtr( hdc, MAGIC_DONTCARE );
109 if (!ptr) return NULL;
110 if ((GDIMAGIC(ptr->wMagic) == DC_MAGIC) ||
111 (GDIMAGIC(ptr->wMagic) == METAFILE_DC_MAGIC) ||
112 (GDIMAGIC(ptr->wMagic) == ENHMETAFILE_DC_MAGIC))
114 GDI_ReleaseObj( hdc );
115 SetLastError( ERROR_INVALID_HANDLE );
119 /***********************************************************************
122 * Retrieve a DC ptr while making sure the visRgn is updated.
123 * This function may call up to USER so the GDI lock should _not_
124 * be held when calling it.
126 DC *DC_GetDCUpdate( HDC hdc )
128 DC *dc = DC_GetDCPtr( hdc );
129 if (!dc) return NULL;
130 while (dc->flags & DC_DIRTY)
132 dc->flags &= ~DC_DIRTY;
133 if (!(dc->flags & (DC_SAVED | DC_MEMORY)))
135 DCHOOKPROC proc = dc->hookThunk;
138 DWORD data = dc->dwHookData;
139 GDI_ReleaseObj( hdc );
140 proc( hdc, DCHC_INVALIDVISRGN, data, 0 );
141 if (!(dc = DC_GetDCPtr( hdc ))) break;
142 /* otherwise restart the loop in case it became dirty again in the meantime */
149 /***********************************************************************
152 * Setup device-specific DC values for a newly created DC.
154 void DC_InitDC( DC* dc )
156 RealizeDefaultPalette16( dc->hSelf );
157 SetTextColor( dc->hSelf, dc->textColor );
158 SetBkColor( dc->hSelf, dc->backgroundColor );
159 SelectObject( dc->hSelf, dc->hPen );
160 SelectObject( dc->hSelf, dc->hBrush );
161 SelectObject( dc->hSelf, dc->hFont );
162 CLIPPING_UpdateGCRegion( dc );
166 /***********************************************************************
169 * Computes the inverse of the transformation xformSrc and stores it to
170 * xformDest. Returns TRUE if successful or FALSE if the xformSrc matrix
173 static BOOL DC_InvertXform( const XFORM *xformSrc, XFORM *xformDest )
177 determinant = xformSrc->eM11*xformSrc->eM22 -
178 xformSrc->eM12*xformSrc->eM21;
179 if (determinant > -1e-12 && determinant < 1e-12)
182 xformDest->eM11 = xformSrc->eM22 / determinant;
183 xformDest->eM12 = -xformSrc->eM12 / determinant;
184 xformDest->eM21 = -xformSrc->eM21 / determinant;
185 xformDest->eM22 = xformSrc->eM11 / determinant;
186 xformDest->eDx = -xformSrc->eDx * xformDest->eM11 -
187 xformSrc->eDy * xformDest->eM21;
188 xformDest->eDy = -xformSrc->eDx * xformDest->eM12 -
189 xformSrc->eDy * xformDest->eM22;
195 /***********************************************************************
198 * Updates the xformWorld2Vport, xformVport2World and vport2WorldValid
199 * fields of the specified DC by creating a transformation that
200 * represents the current mapping mode and combining it with the DC's
201 * world transform. This function should be called whenever the
202 * parameters associated with the mapping mode (window and viewport
203 * extents and origins) or the world transform change.
205 void DC_UpdateXforms( DC *dc )
207 XFORM xformWnd2Vport;
208 FLOAT scaleX, scaleY;
210 /* Construct a transformation to do the window-to-viewport conversion */
211 scaleX = (FLOAT)dc->vportExtX / (FLOAT)dc->wndExtX;
212 scaleY = (FLOAT)dc->vportExtY / (FLOAT)dc->wndExtY;
213 xformWnd2Vport.eM11 = scaleX;
214 xformWnd2Vport.eM12 = 0.0;
215 xformWnd2Vport.eM21 = 0.0;
216 xformWnd2Vport.eM22 = scaleY;
217 xformWnd2Vport.eDx = (FLOAT)dc->vportOrgX -
218 scaleX * (FLOAT)dc->wndOrgX;
219 xformWnd2Vport.eDy = (FLOAT)dc->vportOrgY -
220 scaleY * (FLOAT)dc->wndOrgY;
222 /* Combine with the world transformation */
223 CombineTransform( &dc->xformWorld2Vport, &dc->xformWorld2Wnd,
226 /* Create inverse of world-to-viewport transformation */
227 dc->vport2WorldValid = DC_InvertXform( &dc->xformWorld2Vport,
228 &dc->xformVport2World );
232 /***********************************************************************
233 * GetDCState (GDI.179)
235 HDC16 WINAPI GetDCState16( HDC16 hdc )
240 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
241 if (!(newdc = GDI_AllocObject( sizeof(DC), DC_MAGIC, &handle )))
243 GDI_ReleaseObj( hdc );
246 TRACE("(%04x): returning %04x\n", hdc, handle );
248 newdc->flags = dc->flags | DC_SAVED;
249 newdc->hPen = dc->hPen;
250 newdc->hBrush = dc->hBrush;
251 newdc->hFont = dc->hFont;
252 newdc->hBitmap = dc->hBitmap;
253 newdc->hDevice = dc->hDevice;
254 newdc->hPalette = dc->hPalette;
255 newdc->totalExtent = dc->totalExtent;
256 newdc->bitsPerPixel = dc->bitsPerPixel;
257 newdc->ROPmode = dc->ROPmode;
258 newdc->polyFillMode = dc->polyFillMode;
259 newdc->stretchBltMode = dc->stretchBltMode;
260 newdc->relAbsMode = dc->relAbsMode;
261 newdc->backgroundMode = dc->backgroundMode;
262 newdc->backgroundColor = dc->backgroundColor;
263 newdc->textColor = dc->textColor;
264 newdc->brushOrgX = dc->brushOrgX;
265 newdc->brushOrgY = dc->brushOrgY;
266 newdc->textAlign = dc->textAlign;
267 newdc->charExtra = dc->charExtra;
268 newdc->breakTotalExtra = dc->breakTotalExtra;
269 newdc->breakCount = dc->breakCount;
270 newdc->breakExtra = dc->breakExtra;
271 newdc->breakRem = dc->breakRem;
272 newdc->MapMode = dc->MapMode;
273 newdc->GraphicsMode = dc->GraphicsMode;
275 /* Apparently, the DC origin is not changed by [GS]etDCState */
276 newdc->DCOrgX = dc->DCOrgX;
277 newdc->DCOrgY = dc->DCOrgY;
279 newdc->CursPosX = dc->CursPosX;
280 newdc->CursPosY = dc->CursPosY;
281 newdc->ArcDirection = dc->ArcDirection;
282 newdc->xformWorld2Wnd = dc->xformWorld2Wnd;
283 newdc->xformWorld2Vport = dc->xformWorld2Vport;
284 newdc->xformVport2World = dc->xformVport2World;
285 newdc->vport2WorldValid = dc->vport2WorldValid;
286 newdc->wndOrgX = dc->wndOrgX;
287 newdc->wndOrgY = dc->wndOrgY;
288 newdc->wndExtX = dc->wndExtX;
289 newdc->wndExtY = dc->wndExtY;
290 newdc->vportOrgX = dc->vportOrgX;
291 newdc->vportOrgY = dc->vportOrgY;
292 newdc->vportExtX = dc->vportExtX;
293 newdc->vportExtY = dc->vportExtY;
295 newdc->hSelf = (HDC)handle;
296 newdc->saveLevel = 0;
298 PATH_InitGdiPath( &newdc->path );
300 newdc->pAbortProc = NULL;
301 newdc->hookThunk = NULL;
304 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
306 newdc->hGCClipRgn = newdc->hVisRgn = 0;
309 newdc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
310 CombineRgn( newdc->hClipRgn, dc->hClipRgn, 0, RGN_COPY );
314 GDI_ReleaseObj( handle );
315 GDI_ReleaseObj( hdc );
320 /***********************************************************************
321 * SetDCState (GDI.180)
323 void WINAPI SetDCState16( HDC16 hdc, HDC16 hdcs )
327 if (!(dc = GDI_GetObjPtr( hdc, DC_MAGIC ))) return;
328 if (!(dcs = GDI_GetObjPtr( hdcs, DC_MAGIC )))
330 GDI_ReleaseObj( hdc );
333 if (!dcs->flags & DC_SAVED)
335 GDI_ReleaseObj( hdc );
336 GDI_ReleaseObj( hdcs );
339 TRACE("%04x %04x\n", hdc, hdcs );
341 dc->flags = dcs->flags & ~DC_SAVED;
342 dc->hDevice = dcs->hDevice;
343 dc->totalExtent = dcs->totalExtent;
344 dc->ROPmode = dcs->ROPmode;
345 dc->polyFillMode = dcs->polyFillMode;
346 dc->stretchBltMode = dcs->stretchBltMode;
347 dc->relAbsMode = dcs->relAbsMode;
348 dc->backgroundMode = dcs->backgroundMode;
349 dc->backgroundColor = dcs->backgroundColor;
350 dc->textColor = dcs->textColor;
351 dc->brushOrgX = dcs->brushOrgX;
352 dc->brushOrgY = dcs->brushOrgY;
353 dc->textAlign = dcs->textAlign;
354 dc->charExtra = dcs->charExtra;
355 dc->breakTotalExtra = dcs->breakTotalExtra;
356 dc->breakCount = dcs->breakCount;
357 dc->breakExtra = dcs->breakExtra;
358 dc->breakRem = dcs->breakRem;
359 dc->MapMode = dcs->MapMode;
360 dc->GraphicsMode = dcs->GraphicsMode;
362 /* Apparently, the DC origin is not changed by [GS]etDCState */
363 dc->DCOrgX = dcs->DCOrgX;
364 dc->DCOrgY = dcs->DCOrgY;
366 dc->CursPosX = dcs->CursPosX;
367 dc->CursPosY = dcs->CursPosY;
368 dc->ArcDirection = dcs->ArcDirection;
369 dc->xformWorld2Wnd = dcs->xformWorld2Wnd;
370 dc->xformWorld2Vport = dcs->xformWorld2Vport;
371 dc->xformVport2World = dcs->xformVport2World;
372 dc->vport2WorldValid = dcs->vport2WorldValid;
374 dc->wndOrgX = dcs->wndOrgX;
375 dc->wndOrgY = dcs->wndOrgY;
376 dc->wndExtX = dcs->wndExtX;
377 dc->wndExtY = dcs->wndExtY;
378 dc->vportOrgX = dcs->vportOrgX;
379 dc->vportOrgY = dcs->vportOrgY;
380 dc->vportExtX = dcs->vportExtX;
381 dc->vportExtY = dcs->vportExtY;
383 if (!(dc->flags & DC_MEMORY)) dc->bitsPerPixel = dcs->bitsPerPixel;
387 if (!dc->hClipRgn) dc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
388 CombineRgn( dc->hClipRgn, dcs->hClipRgn, 0, RGN_COPY );
392 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
395 CLIPPING_UpdateGCRegion( dc );
397 SelectObject( hdc, dcs->hBitmap );
398 SelectObject( hdc, dcs->hBrush );
399 SelectObject( hdc, dcs->hFont );
400 SelectObject( hdc, dcs->hPen );
401 SetBkColor( hdc, dcs->backgroundColor);
402 SetTextColor( hdc, dcs->textColor);
403 GDISelectPalette16( hdc, dcs->hPalette, FALSE );
404 GDI_ReleaseObj( hdcs );
405 GDI_ReleaseObj( hdc );
409 /***********************************************************************
412 INT16 WINAPI SaveDC16( HDC16 hdc )
414 return (INT16)SaveDC( hdc );
418 /***********************************************************************
421 INT WINAPI SaveDC( HDC hdc )
427 dc = DC_GetDCUpdate( hdc );
430 if(dc->funcs->pSaveDC)
432 ret = dc->funcs->pSaveDC( dc );
433 GDI_ReleaseObj( hdc );
437 if (!(hdcs = GetDCState16( hdc )))
439 GDI_ReleaseObj( hdc );
442 dcs = GDI_GetObjPtr( hdcs, DC_MAGIC );
444 /* Copy path. The reason why path saving / restoring is in SaveDC/
445 * RestoreDC and not in GetDCState/SetDCState is that the ...DCState
446 * functions are only in Win16 (which doesn't have paths) and that
447 * SetDCState doesn't allow us to signal an error (which can happen
448 * when copying paths).
450 if (!PATH_AssignGdiPath( &dcs->path, &dc->path ))
452 GDI_ReleaseObj( hdc );
453 GDI_ReleaseObj( hdcs );
458 dcs->header.hNext = dc->header.hNext;
459 dc->header.hNext = hdcs;
460 TRACE("(%04x): returning %d\n", hdc, dc->saveLevel+1 );
461 ret = ++dc->saveLevel;
462 GDI_ReleaseObj( hdcs );
463 GDI_ReleaseObj( hdc );
468 /***********************************************************************
471 BOOL16 WINAPI RestoreDC16( HDC16 hdc, INT16 level )
473 return RestoreDC( hdc, level );
477 /***********************************************************************
478 * RestoreDC (GDI32.@)
480 BOOL WINAPI RestoreDC( HDC hdc, INT level )
485 TRACE("%04x %d\n", hdc, level );
486 dc = DC_GetDCPtr( hdc );
487 if(!dc) return FALSE;
488 if(dc->funcs->pRestoreDC)
490 success = dc->funcs->pRestoreDC( dc, level );
491 GDI_ReleaseObj( hdc );
495 if (level == -1) level = dc->saveLevel;
497 /* This pair of checks disagrees with MSDN "Platform SDK:
498 Windows GDI" July 2000 which says all negative values
499 for level will be interpreted as an instance relative
500 to the current state. Restricting it to just -1 does
502 || (level > dc->saveLevel))
504 GDI_ReleaseObj( hdc );
509 while (dc->saveLevel >= level)
511 HDC16 hdcs = dc->header.hNext;
512 if (!(dcs = GDI_GetObjPtr( hdcs, DC_MAGIC )))
514 GDI_ReleaseObj( hdc );
517 dc->header.hNext = dcs->header.hNext;
518 if (--dc->saveLevel < level)
520 SetDCState16( hdc, hdcs );
521 if (!PATH_AssignGdiPath( &dc->path, &dcs->path ))
522 /* FIXME: This might not be quite right, since we're
523 * returning FALSE but still destroying the saved DC state */
526 GDI_ReleaseObj( hdcs );
529 GDI_ReleaseObj( hdc );
534 /***********************************************************************
537 HDC16 WINAPI CreateDC16( LPCSTR driver, LPCSTR device, LPCSTR output,
538 const DEVMODEA *initData )
540 return CreateDCA( driver, device, output, initData );
543 /***********************************************************************
544 * CreateDCA (GDI32.@)
546 HDC WINAPI CreateDCA( LPCSTR driver, LPCSTR device, LPCSTR output,
547 const DEVMODEA *initData )
551 const DC_FUNCTIONS *funcs;
556 if (!device || !DRIVER_GetDriverName( device, buf, sizeof(buf) ))
559 if (!(funcs = DRIVER_load_driver( buf )))
561 ERR( "no driver found for %s\n", buf );
564 if (!(dc = DC_AllocDC( funcs )))
566 DRIVER_release_driver( funcs );
572 TRACE("(driver=%s, device=%s, output=%s): returning %04x\n",
573 debugstr_a(driver), debugstr_a(device), debugstr_a(output), dc->hSelf );
575 if (dc->funcs->pCreateDC &&
576 !dc->funcs->pCreateDC( dc, buf, device, output, initData ))
578 WARN("creation aborted by device\n" );
579 GDI_FreeObject( dc->hSelf, dc );
580 DRIVER_release_driver( funcs );
586 GDI_ReleaseObj( hdc );
591 /***********************************************************************
592 * CreateDCW (GDI32.@)
594 HDC WINAPI CreateDCW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
595 const DEVMODEW *initData )
597 LPSTR driverA = HEAP_strdupWtoA( GetProcessHeap(), 0, driver );
598 LPSTR deviceA = HEAP_strdupWtoA( GetProcessHeap(), 0, device );
599 LPSTR outputA = HEAP_strdupWtoA( GetProcessHeap(), 0, output );
600 HDC res = CreateDCA( driverA, deviceA, outputA,
601 (const DEVMODEA *)initData /*FIXME*/ );
602 HeapFree( GetProcessHeap(), 0, driverA );
603 HeapFree( GetProcessHeap(), 0, deviceA );
604 HeapFree( GetProcessHeap(), 0, outputA );
609 /***********************************************************************
612 HDC16 WINAPI CreateIC16( LPCSTR driver, LPCSTR device, LPCSTR output,
613 const DEVMODEA* initData )
615 /* Nothing special yet for ICs */
616 return CreateDC16( driver, device, output, initData );
620 /***********************************************************************
621 * CreateICA (GDI32.@)
623 HDC WINAPI CreateICA( LPCSTR driver, LPCSTR device, LPCSTR output,
624 const DEVMODEA* initData )
626 /* Nothing special yet for ICs */
627 return CreateDCA( driver, device, output, initData );
631 /***********************************************************************
632 * CreateICW (GDI32.@)
634 HDC WINAPI CreateICW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
635 const DEVMODEW* initData )
637 /* Nothing special yet for ICs */
638 return CreateDCW( driver, device, output, initData );
642 /***********************************************************************
643 * CreateCompatibleDC (GDI.52)
645 HDC16 WINAPI CreateCompatibleDC16( HDC16 hdc )
647 return (HDC16)CreateCompatibleDC( hdc );
651 /***********************************************************************
652 * CreateCompatibleDC (GDI32.@)
654 HDC WINAPI CreateCompatibleDC( HDC hdc )
657 const DC_FUNCTIONS *funcs;
661 if ((origDC = GDI_GetObjPtr( hdc, DC_MAGIC )))
663 funcs = origDC->funcs;
664 GDI_ReleaseObj( hdc ); /* can't hold the lock while loading the driver */
665 funcs = DRIVER_get_driver( funcs );
667 else funcs = DRIVER_load_driver( "DISPLAY" );
669 if (!funcs) return 0;
671 if (!(dc = DC_AllocDC( funcs )))
673 DRIVER_release_driver( funcs );
677 TRACE("(%04x): returning %04x\n",
680 dc->flags = DC_MEMORY;
681 dc->bitsPerPixel = 1;
682 dc->hBitmap = hPseudoStockBitmap;
684 /* Copy the driver-specific physical device info into
685 * the new DC. The driver may use this read-only info
686 * while creating the compatible DC below. */
687 if ((origDC = GDI_GetObjPtr( hdc, DC_MAGIC ))) dc->physDev = origDC->physDev;
689 if (dc->funcs->pCreateDC &&
690 !dc->funcs->pCreateDC( dc, NULL, NULL, NULL, NULL ))
692 WARN("creation aborted by device\n");
693 GDI_FreeObject( dc->hSelf, dc );
694 if (origDC) GDI_ReleaseObj( hdc );
695 DRIVER_release_driver( funcs );
700 GDI_ReleaseObj( dc->hSelf );
701 if (origDC) GDI_ReleaseObj( hdc );
706 /***********************************************************************
709 BOOL16 WINAPI DeleteDC16( HDC16 hdc )
711 return DeleteDC( hdc );
715 /***********************************************************************
718 BOOL WINAPI DeleteDC( HDC hdc )
720 const DC_FUNCTIONS *funcs = NULL;
723 TRACE("%04x\n", hdc );
727 if (!(dc = GDI_GetObjPtr( hdc, DC_MAGIC ))) return FALSE;
729 /* Call hook procedure to check whether is it OK to delete this DC */
730 if (dc->hookThunk && !(dc->flags & (DC_SAVED | DC_MEMORY)))
732 DCHOOKPROC proc = dc->hookThunk;
735 DWORD data = dc->dwHookData;
736 GDI_ReleaseObj( hdc );
737 if (!proc( hdc, DCHC_DELETEDC, data, 0 )) return FALSE;
738 if (!(dc = DC_GetDCPtr( hdc ))) return FALSE;
742 while (dc->saveLevel)
745 HDC16 hdcs = dc->header.hNext;
746 if (!(dcs = GDI_GetObjPtr( hdcs, DC_MAGIC ))) break;
747 dc->header.hNext = dcs->header.hNext;
749 GDI_ReleaseObj( hdcs );
753 if (!(dc->flags & DC_SAVED))
755 SelectObject( hdc, GetStockObject(BLACK_PEN) );
756 SelectObject( hdc, GetStockObject(WHITE_BRUSH) );
757 SelectObject( hdc, GetStockObject(SYSTEM_FONT) );
759 if (dc->funcs->pDeleteDC) dc->funcs->pDeleteDC(dc);
762 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
763 if (dc->hVisRgn) DeleteObject( dc->hVisRgn );
764 if (dc->hGCClipRgn) DeleteObject( dc->hGCClipRgn );
765 if (dc->pAbortProc) THUNK_Free( (FARPROC)dc->pAbortProc );
766 if (dc->hookThunk) THUNK_Free( (FARPROC)dc->hookThunk );
767 PATH_DestroyGdiPath(&dc->path);
769 GDI_FreeObject( hdc, dc );
770 if (funcs) DRIVER_release_driver( funcs ); /* do that after releasing the GDI lock */
775 /***********************************************************************
778 HDC16 WINAPI ResetDC16( HDC16 hdc, const DEVMODEA *devmode )
785 /***********************************************************************
788 HDC WINAPI ResetDCA( HDC hdc, const DEVMODEA *devmode )
795 /***********************************************************************
798 HDC WINAPI ResetDCW( HDC hdc, const DEVMODEW *devmode )
805 /***********************************************************************
806 * GetDeviceCaps (GDI.80)
808 INT16 WINAPI GetDeviceCaps16( HDC16 hdc, INT16 cap )
810 INT16 ret = GetDeviceCaps( hdc, cap );
811 /* some apps don't expect -1 and think it's a B&W screen */
812 if ((cap == NUMCOLORS) && (ret == -1)) ret = 2048;
817 /***********************************************************************
818 * GetDeviceCaps (GDI32.@)
820 INT WINAPI GetDeviceCaps( HDC hdc, INT cap )
825 if ((dc = DC_GetDCPtr( hdc )))
827 if (dc->funcs->pGetDeviceCaps) ret = dc->funcs->pGetDeviceCaps( dc, cap );
828 GDI_ReleaseObj( hdc );
834 /***********************************************************************
837 COLORREF WINAPI SetBkColor16( HDC16 hdc, COLORREF color )
839 return SetBkColor( hdc, color );
843 /***********************************************************************
844 * SetBkColor (GDI32.@)
846 COLORREF WINAPI SetBkColor( HDC hdc, COLORREF color )
849 DC * dc = DC_GetDCPtr( hdc );
851 if (!dc) return 0x80000000;
852 if (dc->funcs->pSetBkColor)
853 oldColor = dc->funcs->pSetBkColor(dc, color);
855 oldColor = dc->backgroundColor;
856 dc->backgroundColor = color;
858 GDI_ReleaseObj( hdc );
863 /***********************************************************************
864 * SetTextColor (GDI.9)
866 COLORREF WINAPI SetTextColor16( HDC16 hdc, COLORREF color )
868 return SetTextColor( hdc, color );
872 /***********************************************************************
873 * SetTextColor (GDI32.@)
875 COLORREF WINAPI SetTextColor( HDC hdc, COLORREF color )
878 DC * dc = DC_GetDCPtr( hdc );
880 if (!dc) return 0x80000000;
881 if (dc->funcs->pSetTextColor)
882 oldColor = dc->funcs->pSetTextColor(dc, color);
884 oldColor = dc->textColor;
885 dc->textColor = color;
887 GDI_ReleaseObj( hdc );
891 /***********************************************************************
892 * SetTextAlign (GDI.346)
894 UINT16 WINAPI SetTextAlign16( HDC16 hdc, UINT16 align )
896 return SetTextAlign( hdc, align );
900 /***********************************************************************
901 * SetTextAlign (GDI32.@)
903 UINT WINAPI SetTextAlign( HDC hdc, UINT align )
906 DC *dc = DC_GetDCPtr( hdc );
908 if (dc->funcs->pSetTextAlign)
909 prevAlign = dc->funcs->pSetTextAlign(dc, align);
911 prevAlign = dc->textAlign;
912 dc->textAlign = align;
914 GDI_ReleaseObj( hdc );
918 /***********************************************************************
919 * GetDCOrgEx (GDI32.@)
921 BOOL WINAPI GetDCOrgEx( HDC hDC, LPPOINT lpp )
925 if (!lpp) return FALSE;
926 if (!(dc = DC_GetDCPtr( hDC ))) return FALSE;
929 if (dc->funcs->pGetDCOrgEx) dc->funcs->pGetDCOrgEx( dc, lpp );
930 lpp->x += dc->DCOrgX;
931 lpp->y += dc->DCOrgY;
932 GDI_ReleaseObj( hDC );
937 /***********************************************************************
940 DWORD WINAPI GetDCOrg16( HDC16 hdc )
943 if( GetDCOrgEx( hdc, &pt) )
944 return MAKELONG( (WORD)pt.x, (WORD)pt.y );
949 /***********************************************************************
952 DWORD WINAPI SetDCOrg16( HDC16 hdc, INT16 x, INT16 y )
955 DC *dc = DC_GetDCPtr( hdc );
957 prevOrg = dc->DCOrgX | (dc->DCOrgY << 16);
960 GDI_ReleaseObj( hdc );
965 /***********************************************************************
966 * SetGraphicsMode (GDI32.@)
968 INT WINAPI SetGraphicsMode( HDC hdc, INT mode )
971 DC *dc = DC_GetDCPtr( hdc );
973 /* One would think that setting the graphics mode to GM_COMPATIBLE
974 * would also reset the world transformation matrix to the unity
975 * matrix. However, in Windows, this is not the case. This doesn't
976 * make a lot of sense to me, but that's the way it is.
979 if ((mode > 0) || (mode <= GM_LAST))
981 ret = dc->GraphicsMode;
982 dc->GraphicsMode = mode;
984 GDI_ReleaseObj( hdc );
989 /***********************************************************************
990 * SetArcDirection (GDI.525)
992 INT16 WINAPI SetArcDirection16( HDC16 hdc, INT16 nDirection )
994 return SetArcDirection( (HDC)hdc, (INT)nDirection );
998 /***********************************************************************
999 * SetArcDirection (GDI32.@)
1001 INT WINAPI SetArcDirection( HDC hdc, INT nDirection )
1004 INT nOldDirection = 0;
1006 if (nDirection!=AD_COUNTERCLOCKWISE && nDirection!=AD_CLOCKWISE)
1008 SetLastError(ERROR_INVALID_PARAMETER);
1012 if ((dc = DC_GetDCPtr( hdc )))
1014 nOldDirection = dc->ArcDirection;
1015 dc->ArcDirection = nDirection;
1016 GDI_ReleaseObj( hdc );
1018 return nOldDirection;
1022 /***********************************************************************
1023 * GetWorldTransform (GDI32.@)
1025 BOOL WINAPI GetWorldTransform( HDC hdc, LPXFORM xform )
1028 if (!xform) return FALSE;
1029 if (!(dc = DC_GetDCPtr( hdc ))) return FALSE;
1030 *xform = dc->xformWorld2Wnd;
1031 GDI_ReleaseObj( hdc );
1036 /***********************************************************************
1037 * SetWorldTransform (GDI32.@)
1039 BOOL WINAPI SetWorldTransform( HDC hdc, const XFORM *xform )
1042 DC *dc = DC_GetDCPtr( hdc );
1044 if (!dc) return FALSE;
1045 if (!xform) goto done;
1047 /* Check that graphics mode is GM_ADVANCED */
1048 if (dc->GraphicsMode!=GM_ADVANCED) goto done;
1050 dc->xformWorld2Wnd = *xform;
1051 DC_UpdateXforms( dc );
1054 GDI_ReleaseObj( hdc );
1059 /****************************************************************************
1060 * ModifyWorldTransform [GDI32.@]
1061 * Modifies the world transformation for a device context.
1064 * hdc [I] Handle to device context
1065 * xform [I] XFORM structure that will be used to modify the world
1067 * iMode [I] Specifies in what way to modify the world transformation
1070 * Resets the world transformation to the identity matrix.
1071 * The parameter xform is ignored.
1073 * Multiplies xform into the world transformation matrix from
1076 * Multiplies xform into the world transformation matrix from
1081 BOOL WINAPI ModifyWorldTransform( HDC hdc, const XFORM *xform,
1085 DC *dc = DC_GetDCPtr( hdc );
1087 /* Check for illegal parameters */
1088 if (!dc) return FALSE;
1089 if (!xform) goto done;
1091 /* Check that graphics mode is GM_ADVANCED */
1092 if (dc->GraphicsMode!=GM_ADVANCED) goto done;
1097 dc->xformWorld2Wnd.eM11 = 1.0f;
1098 dc->xformWorld2Wnd.eM12 = 0.0f;
1099 dc->xformWorld2Wnd.eM21 = 0.0f;
1100 dc->xformWorld2Wnd.eM22 = 1.0f;
1101 dc->xformWorld2Wnd.eDx = 0.0f;
1102 dc->xformWorld2Wnd.eDy = 0.0f;
1104 case MWT_LEFTMULTIPLY:
1105 CombineTransform( &dc->xformWorld2Wnd, xform,
1106 &dc->xformWorld2Wnd );
1108 case MWT_RIGHTMULTIPLY:
1109 CombineTransform( &dc->xformWorld2Wnd, &dc->xformWorld2Wnd,
1116 DC_UpdateXforms( dc );
1119 GDI_ReleaseObj( hdc );
1124 /****************************************************************************
1125 * CombineTransform [GDI32.@]
1126 * Combines two transformation matrices.
1129 * xformResult [O] Stores the result of combining the two matrices
1130 * xform1 [I] Specifies the first matrix to apply
1131 * xform2 [I] Specifies the second matrix to apply
1134 * The same matrix can be passed in for more than one of the parameters.
1138 BOOL WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
1139 const XFORM *xform2 )
1143 /* Check for illegal parameters */
1144 if (!xformResult || !xform1 || !xform2)
1147 /* Create the result in a temporary XFORM, since xformResult may be
1148 * equal to xform1 or xform2 */
1149 xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
1150 xform1->eM12 * xform2->eM21;
1151 xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
1152 xform1->eM12 * xform2->eM22;
1153 xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
1154 xform1->eM22 * xform2->eM21;
1155 xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
1156 xform1->eM22 * xform2->eM22;
1157 xformTemp.eDx = xform1->eDx * xform2->eM11 +
1158 xform1->eDy * xform2->eM21 +
1160 xformTemp.eDy = xform1->eDx * xform2->eM12 +
1161 xform1->eDy * xform2->eM22 +
1164 /* Copy the result to xformResult */
1165 *xformResult = xformTemp;
1171 /***********************************************************************
1172 * SetDCHook (GDI.190)
1175 /* ### start build ### */
1176 extern WORD CALLBACK GDI_CallTo16_word_wwll(FARPROC16,WORD,WORD,LONG,LONG);
1177 /* ### stop build ### */
1179 /**********************************************************************/
1181 BOOL16 WINAPI SetDCHook( HDC16 hdc, FARPROC16 hookProc, DWORD dwHookData )
1183 DC *dc = DC_GetDCPtr( hdc );
1184 if (!dc) return FALSE;
1187 * Note: We store the original SEGPTR 'hookProc' as we need to be
1188 * able to return it verbatim in GetDCHook,
1190 * On the other hand, we still call THUNK_Alloc and store the
1191 * 32-bit thunk into another DC member, because THUNK_Alloc
1192 * recognizes the (typical) case that the 'hookProc' is indeed
1193 * the 16-bit API entry point of a built-in routine (e.g. DCHook16)
1195 * We could perform that test every time the hook is about to
1196 * be called (or else we could live with the 32->16->32 detour),
1197 * but this way is the most efficient ...
1200 dc->hookProc = hookProc;
1201 dc->dwHookData = dwHookData;
1203 THUNK_Free( (FARPROC)dc->hookThunk );
1204 dc->hookThunk = (DCHOOKPROC)
1205 THUNK_Alloc( hookProc, (RELAY)GDI_CallTo16_word_wwll );
1207 GDI_ReleaseObj( hdc );
1212 /***********************************************************************
1213 * GetDCHook (GDI.191)
1215 DWORD WINAPI GetDCHook( HDC16 hdc, FARPROC16 *phookProc )
1217 DC *dc = DC_GetDCPtr( hdc );
1219 *phookProc = dc->hookProc;
1220 GDI_ReleaseObj( hdc );
1221 return dc->dwHookData;
1225 /***********************************************************************
1226 * SetHookFlags (GDI.192)
1228 WORD WINAPI SetHookFlags16(HDC16 hDC, WORD flags)
1230 DC *dc = DC_GetDCPtr( hDC );
1234 WORD wRet = dc->flags & DC_DIRTY;
1236 /* "Undocumented Windows" info is slightly confusing.
1239 TRACE("hDC %04x, flags %04x\n",hDC,flags);
1241 if( flags & DCHF_INVALIDATEVISRGN )
1242 dc->flags |= DC_DIRTY;
1243 else if( flags & DCHF_VALIDATEVISRGN || !flags )
1244 dc->flags &= ~DC_DIRTY;
1245 GDI_ReleaseObj( hDC );
1251 /***********************************************************************
1252 * SetICMMode (GDI32.@)
1254 INT WINAPI SetICMMode(HDC hdc, INT iEnableICM)
1256 /*FIXME Asuming that ICM is always off, and cannot be turned on */
1257 if (iEnableICM == ICM_OFF) return ICM_OFF;
1258 if (iEnableICM == ICM_ON) return 0;
1259 if (iEnableICM == ICM_QUERY) return ICM_OFF;
1263 /***********************************************************************
1264 * GetDeviceGammaRamp (GDI32.@)
1266 BOOL WINAPI GetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1269 DC *dc = DC_GetDCPtr( hDC );
1273 if (dc->funcs->pGetDeviceGammaRamp)
1274 ret = dc->funcs->pGetDeviceGammaRamp(dc, ptr);
1275 GDI_ReleaseObj( hDC );
1280 /***********************************************************************
1281 * SetDeviceGammaRamp (GDI32.@)
1283 BOOL WINAPI SetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1286 DC *dc = DC_GetDCPtr( hDC );
1290 if (dc->funcs->pSetDeviceGammaRamp)
1291 ret = dc->funcs->pSetDeviceGammaRamp(dc, ptr);
1292 GDI_ReleaseObj( hDC );
1297 /***********************************************************************
1298 * GetColorSpace (GDI32.@)
1300 HCOLORSPACE WINAPI GetColorSpace(HDC hdc)
1302 /*FIXME Need to to whatever GetColorSpace actually does */
1306 /***********************************************************************
1307 * CreateColorSpaceA (GDI32.@)
1309 HCOLORSPACE WINAPI CreateColorSpaceA( LPLOGCOLORSPACEA lpLogColorSpace )
1315 /***********************************************************************
1316 * CreateColorSpaceW (GDI32.@)
1318 HCOLORSPACE WINAPI CreateColorSpaceW( LPLOGCOLORSPACEW lpLogColorSpace )
1324 /***********************************************************************
1325 * DeleteColorSpace (GDI32.@)
1327 BOOL WINAPI DeleteColorSpace( HCOLORSPACE hColorSpace )
1334 /***********************************************************************
1335 * SetColorSpace (GDI32.@)
1337 HCOLORSPACE WINAPI SetColorSpace( HDC hDC, HCOLORSPACE hColorSpace )
1344 /***********************************************************************
1345 * GetBoundsRect (GDI.194)
1347 UINT16 WINAPI GetBoundsRect16(HDC16 hdc, LPRECT16 rect, UINT16 flags)
1349 return DCB_RESET | DCB_DISABLE; /* bounding rectangle always empty and disabled*/
1352 /***********************************************************************
1353 * GetBoundsRect (GDI32.@)
1355 UINT WINAPI GetBoundsRect(HDC hdc, LPRECT rect, UINT flags)
1357 FIXME("(): stub\n");
1358 return DCB_RESET; /* bounding rectangle always empty */
1361 /***********************************************************************
1362 * SetBoundsRect (GDI.193)
1364 UINT16 WINAPI SetBoundsRect16(HDC16 hdc, const RECT16* rect, UINT16 flags)
1366 if ( (flags & DCB_ACCUMULATE) || (flags & DCB_ENABLE) )
1367 FIXME("(%04x, %p, %04x): stub\n", hdc, rect, flags );
1369 return DCB_RESET | DCB_DISABLE; /* bounding rectangle always empty and disabled*/
1372 /***********************************************************************
1373 * SetBoundsRect (GDI32.@)
1375 UINT WINAPI SetBoundsRect(HDC hdc, const RECT* rect, UINT flags)
1377 FIXME("(): stub\n");
1378 return DCB_DISABLE; /* bounding rectangle always empty */
1382 /***********************************************************************
1383 * GetRelAbs (GDI32.@)
1385 INT WINAPI GetRelAbs( HDC hdc, DWORD dwIgnore )
1388 DC *dc = DC_GetDCPtr( hdc );
1389 if (dc) ret = dc->relAbsMode;
1390 GDI_ReleaseObj( hdc );
1394 /***********************************************************************
1397 * Disables GDI, switches back to text mode.
1398 * We don't have to do anything here,
1399 * just let console support handle everything
1401 void WINAPI Death16(HDC16 hDC)
1403 MESSAGE("Death(%04x) called. Application enters text mode...\n", hDC);
1406 /***********************************************************************
1407 * Resurrection (GDI.122)
1409 * Restores GDI functionality
1411 void WINAPI Resurrection16(HDC16 hDC,
1412 WORD w1, WORD w2, WORD w3, WORD w4, WORD w5, WORD w6)
1414 MESSAGE("Resurrection(%04x, %04x, %04x, %04x, %04x, %04x, %04x) called. Application left text mode.\n", hDC, w1, w2, w3, w4, w5, w6);
1417 /***********************************************************************
1418 * GetLayout (GDI32.@)
1420 * Gets left->right or right->left text layout flags of a dc.
1421 * win98 just returns 0 and sets ERROR_CALL_NOT_IMPLEMENTED so we do the same
1424 DWORD WINAPI GetLayout(HDC hdc)
1426 FIXME("(%08x): stub\n", hdc);
1427 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1431 /***********************************************************************
1432 * SetLayout (GDI32.@)
1434 * Sets left->right or right->left text layout flags of a dc.
1435 * win98 just returns 0 and sets ERROR_CALL_NOT_IMPLEMENTED so we do the same
1438 DWORD WINAPI SetLayout(HDC hdc, DWORD layout)
1440 FIXME("(%08x,%08lx): stub\n", hdc, layout);
1441 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);