DC hook proc thunk management simplified.
[wine/multimedia.git] / objects / dc.c
blob1dfb530aab13ac334c2533e5f9dbdfc8715ef605
1 /*
2 * GDI Device Context functions
4 * Copyright 1993 Alexandre Julliard
6 */
8 #include "config.h"
10 #ifndef X_DISPLAY_MISSING
11 #include "ts_xlib.h"
12 #include "x11drv.h"
13 #endif /* !defined(X_DISPLAY_MISSING) */
15 #include <stdlib.h>
16 #include <string.h>
17 #include "dc.h"
18 #include "gdi.h"
19 #include "heap.h"
20 #include "debugtools.h"
21 #include "font.h"
22 #include "callback.h"
23 #include "winerror.h"
24 #include "wine/winuser16.h"
26 DEFAULT_DEBUG_CHANNEL(dc)
28 /***********************************************************************
29 * DC_Init_DC_INFO
31 * Fill the WIN_DC_INFO structure.
33 static void DC_Init_DC_INFO( WIN_DC_INFO *win_dc_info )
35 win_dc_info->flags = 0;
36 win_dc_info->devCaps = NULL;
37 win_dc_info->hClipRgn = 0;
38 win_dc_info->hVisRgn = 0;
39 win_dc_info->hGCClipRgn = 0;
40 win_dc_info->hPen = STOCK_BLACK_PEN;
41 win_dc_info->hBrush = STOCK_WHITE_BRUSH;
42 win_dc_info->hFont = STOCK_SYSTEM_FONT;
43 win_dc_info->hBitmap = 0;
44 win_dc_info->hFirstBitmap = 0;
45 win_dc_info->hDevice = 0;
46 win_dc_info->hPalette = STOCK_DEFAULT_PALETTE;
47 win_dc_info->ROPmode = R2_COPYPEN;
48 win_dc_info->polyFillMode = ALTERNATE;
49 win_dc_info->stretchBltMode = BLACKONWHITE;
50 win_dc_info->relAbsMode = ABSOLUTE;
51 win_dc_info->backgroundMode = OPAQUE;
52 win_dc_info->backgroundColor = RGB( 255, 255, 255 );
53 win_dc_info->textColor = RGB( 0, 0, 0 );
54 win_dc_info->brushOrgX = 0;
55 win_dc_info->brushOrgY = 0;
56 win_dc_info->textAlign = TA_LEFT | TA_TOP | TA_NOUPDATECP;
57 win_dc_info->charExtra = 0;
58 win_dc_info->breakTotalExtra = 0;
59 win_dc_info->breakCount = 0;
60 win_dc_info->breakExtra = 0;
61 win_dc_info->breakRem = 0;
62 win_dc_info->bitsPerPixel = 1;
63 win_dc_info->MapMode = MM_TEXT;
64 win_dc_info->GraphicsMode = GM_COMPATIBLE;
65 win_dc_info->DCOrgX = 0;
66 win_dc_info->DCOrgY = 0;
67 win_dc_info->pAbortProc = NULL;
68 win_dc_info->CursPosX = 0;
69 win_dc_info->CursPosY = 0;
70 win_dc_info->ArcDirection = AD_COUNTERCLOCKWISE;
71 win_dc_info->xformWorld2Wnd.eM11 = 1.0f;
72 win_dc_info->xformWorld2Wnd.eM12 = 0.0f;
73 win_dc_info->xformWorld2Wnd.eM21 = 0.0f;
74 win_dc_info->xformWorld2Wnd.eM22 = 1.0f;
75 win_dc_info->xformWorld2Wnd.eDx = 0.0f;
76 win_dc_info->xformWorld2Wnd.eDy = 0.0f;
77 win_dc_info->xformWorld2Vport = win_dc_info->xformWorld2Wnd;
78 win_dc_info->xformVport2World = win_dc_info->xformWorld2Wnd;
79 win_dc_info->vport2WorldValid = TRUE;
81 PATH_InitGdiPath(&win_dc_info->path);
85 /***********************************************************************
86 * DC_AllocDC
88 DC *DC_AllocDC( const DC_FUNCTIONS *funcs )
90 HDC16 hdc;
91 DC *dc;
93 if (!(hdc = GDI_AllocObject( sizeof(DC), DC_MAGIC ))) return NULL;
94 dc = (DC *) GDI_HEAP_LOCK( hdc );
96 dc->hSelf = hdc;
97 dc->funcs = funcs;
98 dc->physDev = NULL;
99 dc->saveLevel = 0;
100 dc->dwHookData = 0L;
101 dc->hookProc = NULL;
102 dc->hookThunk = NULL;
103 dc->wndOrgX = 0;
104 dc->wndOrgY = 0;
105 dc->wndExtX = 1;
106 dc->wndExtY = 1;
107 dc->vportOrgX = 0;
108 dc->vportOrgY = 0;
109 dc->vportExtX = 1;
110 dc->vportExtY = 1;
112 DC_Init_DC_INFO( &dc->w );
114 return dc;
119 /***********************************************************************
120 * DC_GetDCPtr
122 DC *DC_GetDCPtr( HDC hdc )
124 GDIOBJHDR *ptr = (GDIOBJHDR *)GDI_HEAP_LOCK( hdc );
125 if (!ptr) return NULL;
126 if ((ptr->wMagic == DC_MAGIC) || (ptr->wMagic == METAFILE_DC_MAGIC) ||
127 (ptr->wMagic == ENHMETAFILE_DC_MAGIC))
128 return (DC *)ptr;
129 GDI_HEAP_UNLOCK( hdc );
130 return NULL;
134 /***********************************************************************
135 * DC_InitDC
137 * Setup device-specific DC values for a newly created DC.
139 void DC_InitDC( DC* dc )
141 RealizeDefaultPalette16( dc->hSelf );
142 SetTextColor( dc->hSelf, dc->w.textColor );
143 SetBkColor( dc->hSelf, dc->w.backgroundColor );
144 SelectObject( dc->hSelf, dc->w.hPen );
145 SelectObject( dc->hSelf, dc->w.hBrush );
146 SelectObject( dc->hSelf, dc->w.hFont );
147 CLIPPING_UpdateGCRegion( dc );
151 /***********************************************************************
152 * DC_InvertXform
154 * Computes the inverse of the transformation xformSrc and stores it to
155 * xformDest. Returns TRUE if successful or FALSE if the xformSrc matrix
156 * is singular.
158 static BOOL DC_InvertXform( const XFORM *xformSrc, XFORM *xformDest )
160 FLOAT determinant;
162 determinant = xformSrc->eM11*xformSrc->eM22 -
163 xformSrc->eM12*xformSrc->eM21;
164 if (determinant > -1e-12 && determinant < 1e-12)
165 return FALSE;
167 xformDest->eM11 = xformSrc->eM22 / determinant;
168 xformDest->eM12 = -xformSrc->eM12 / determinant;
169 xformDest->eM21 = -xformSrc->eM21 / determinant;
170 xformDest->eM22 = xformSrc->eM11 / determinant;
171 xformDest->eDx = -xformSrc->eDx * xformDest->eM11 -
172 xformSrc->eDy * xformDest->eM21;
173 xformDest->eDy = -xformSrc->eDx * xformDest->eM12 -
174 xformSrc->eDy * xformDest->eM22;
176 return TRUE;
180 /***********************************************************************
181 * DC_UpdateXforms
183 * Updates the xformWorld2Vport, xformVport2World and vport2WorldValid
184 * fields of the specified DC by creating a transformation that
185 * represents the current mapping mode and combining it with the DC's
186 * world transform. This function should be called whenever the
187 * parameters associated with the mapping mode (window and viewport
188 * extents and origins) or the world transform change.
190 void DC_UpdateXforms( DC *dc )
192 XFORM xformWnd2Vport;
193 FLOAT scaleX, scaleY;
195 /* Construct a transformation to do the window-to-viewport conversion */
196 scaleX = (FLOAT)dc->vportExtX / (FLOAT)dc->wndExtX;
197 scaleY = (FLOAT)dc->vportExtY / (FLOAT)dc->wndExtY;
198 xformWnd2Vport.eM11 = scaleX;
199 xformWnd2Vport.eM12 = 0.0;
200 xformWnd2Vport.eM21 = 0.0;
201 xformWnd2Vport.eM22 = scaleY;
202 xformWnd2Vport.eDx = (FLOAT)dc->vportOrgX -
203 scaleX * (FLOAT)dc->wndOrgX;
204 xformWnd2Vport.eDy = (FLOAT)dc->vportOrgY -
205 scaleY * (FLOAT)dc->wndOrgY;
207 /* Combine with the world transformation */
208 CombineTransform( &dc->w.xformWorld2Vport, &dc->w.xformWorld2Wnd,
209 &xformWnd2Vport );
211 /* Create inverse of world-to-viewport transformation */
212 dc->w.vport2WorldValid = DC_InvertXform( &dc->w.xformWorld2Vport,
213 &dc->w.xformVport2World );
217 /***********************************************************************
218 * GetDCState (GDI.179)
220 HDC16 WINAPI GetDCState16( HDC16 hdc )
222 DC * newdc, * dc;
223 HGDIOBJ16 handle;
225 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
226 if (!(handle = GDI_AllocObject( sizeof(DC), DC_MAGIC )))
228 GDI_HEAP_UNLOCK( hdc );
229 return 0;
231 newdc = (DC *) GDI_HEAP_LOCK( handle );
233 TRACE("(%04x): returning %04x\n", hdc, handle );
235 newdc->w.flags = dc->w.flags | DC_SAVED;
236 newdc->w.devCaps = dc->w.devCaps;
237 newdc->w.hPen = dc->w.hPen;
238 newdc->w.hBrush = dc->w.hBrush;
239 newdc->w.hFont = dc->w.hFont;
240 newdc->w.hBitmap = dc->w.hBitmap;
241 newdc->w.hFirstBitmap = dc->w.hFirstBitmap;
242 newdc->w.hDevice = dc->w.hDevice;
243 newdc->w.hPalette = dc->w.hPalette;
244 newdc->w.totalExtent = dc->w.totalExtent;
245 newdc->w.bitsPerPixel = dc->w.bitsPerPixel;
246 newdc->w.ROPmode = dc->w.ROPmode;
247 newdc->w.polyFillMode = dc->w.polyFillMode;
248 newdc->w.stretchBltMode = dc->w.stretchBltMode;
249 newdc->w.relAbsMode = dc->w.relAbsMode;
250 newdc->w.backgroundMode = dc->w.backgroundMode;
251 newdc->w.backgroundColor = dc->w.backgroundColor;
252 newdc->w.textColor = dc->w.textColor;
253 newdc->w.brushOrgX = dc->w.brushOrgX;
254 newdc->w.brushOrgY = dc->w.brushOrgY;
255 newdc->w.textAlign = dc->w.textAlign;
256 newdc->w.charExtra = dc->w.charExtra;
257 newdc->w.breakTotalExtra = dc->w.breakTotalExtra;
258 newdc->w.breakCount = dc->w.breakCount;
259 newdc->w.breakExtra = dc->w.breakExtra;
260 newdc->w.breakRem = dc->w.breakRem;
261 newdc->w.MapMode = dc->w.MapMode;
262 newdc->w.GraphicsMode = dc->w.GraphicsMode;
263 #if 0
264 /* Apparently, the DC origin is not changed by [GS]etDCState */
265 newdc->w.DCOrgX = dc->w.DCOrgX;
266 newdc->w.DCOrgY = dc->w.DCOrgY;
267 #endif
268 newdc->w.CursPosX = dc->w.CursPosX;
269 newdc->w.CursPosY = dc->w.CursPosY;
270 newdc->w.ArcDirection = dc->w.ArcDirection;
271 newdc->w.xformWorld2Wnd = dc->w.xformWorld2Wnd;
272 newdc->w.xformWorld2Vport = dc->w.xformWorld2Vport;
273 newdc->w.xformVport2World = dc->w.xformVport2World;
274 newdc->w.vport2WorldValid = dc->w.vport2WorldValid;
275 newdc->wndOrgX = dc->wndOrgX;
276 newdc->wndOrgY = dc->wndOrgY;
277 newdc->wndExtX = dc->wndExtX;
278 newdc->wndExtY = dc->wndExtY;
279 newdc->vportOrgX = dc->vportOrgX;
280 newdc->vportOrgY = dc->vportOrgY;
281 newdc->vportExtX = dc->vportExtX;
282 newdc->vportExtY = dc->vportExtY;
284 newdc->hSelf = (HDC)handle;
285 newdc->saveLevel = 0;
287 PATH_InitGdiPath( &newdc->w.path );
289 newdc->w.pAbortProc = NULL;
291 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
293 newdc->w.hGCClipRgn = newdc->w.hVisRgn = 0;
294 if (dc->w.hClipRgn)
296 newdc->w.hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
297 CombineRgn( newdc->w.hClipRgn, dc->w.hClipRgn, 0, RGN_COPY );
299 else
300 newdc->w.hClipRgn = 0;
301 GDI_HEAP_UNLOCK( handle );
302 GDI_HEAP_UNLOCK( hdc );
303 return handle;
307 /***********************************************************************
308 * SetDCState (GDI.180)
310 void WINAPI SetDCState16( HDC16 hdc, HDC16 hdcs )
312 DC *dc, *dcs;
314 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return;
315 if (!(dcs = (DC *) GDI_GetObjPtr( hdcs, DC_MAGIC )))
317 GDI_HEAP_UNLOCK( hdc );
318 return;
320 if (!dcs->w.flags & DC_SAVED)
322 GDI_HEAP_UNLOCK( hdc );
323 GDI_HEAP_UNLOCK( hdcs );
324 return;
326 TRACE("%04x %04x\n", hdc, hdcs );
328 dc->w.flags = dcs->w.flags & ~DC_SAVED;
329 dc->w.devCaps = dcs->w.devCaps;
330 dc->w.hFirstBitmap = dcs->w.hFirstBitmap;
331 dc->w.hDevice = dcs->w.hDevice;
332 dc->w.totalExtent = dcs->w.totalExtent;
333 dc->w.ROPmode = dcs->w.ROPmode;
334 dc->w.polyFillMode = dcs->w.polyFillMode;
335 dc->w.stretchBltMode = dcs->w.stretchBltMode;
336 dc->w.relAbsMode = dcs->w.relAbsMode;
337 dc->w.backgroundMode = dcs->w.backgroundMode;
338 dc->w.backgroundColor = dcs->w.backgroundColor;
339 dc->w.textColor = dcs->w.textColor;
340 dc->w.brushOrgX = dcs->w.brushOrgX;
341 dc->w.brushOrgY = dcs->w.brushOrgY;
342 dc->w.textAlign = dcs->w.textAlign;
343 dc->w.charExtra = dcs->w.charExtra;
344 dc->w.breakTotalExtra = dcs->w.breakTotalExtra;
345 dc->w.breakCount = dcs->w.breakCount;
346 dc->w.breakExtra = dcs->w.breakExtra;
347 dc->w.breakRem = dcs->w.breakRem;
348 dc->w.MapMode = dcs->w.MapMode;
349 dc->w.GraphicsMode = dcs->w.GraphicsMode;
350 #if 0
351 /* Apparently, the DC origin is not changed by [GS]etDCState */
352 dc->w.DCOrgX = dcs->w.DCOrgX;
353 dc->w.DCOrgY = dcs->w.DCOrgY;
354 #endif
355 dc->w.CursPosX = dcs->w.CursPosX;
356 dc->w.CursPosY = dcs->w.CursPosY;
357 dc->w.ArcDirection = dcs->w.ArcDirection;
358 dc->w.xformWorld2Wnd = dcs->w.xformWorld2Wnd;
359 dc->w.xformWorld2Vport = dcs->w.xformWorld2Vport;
360 dc->w.xformVport2World = dcs->w.xformVport2World;
361 dc->w.vport2WorldValid = dcs->w.vport2WorldValid;
363 dc->wndOrgX = dcs->wndOrgX;
364 dc->wndOrgY = dcs->wndOrgY;
365 dc->wndExtX = dcs->wndExtX;
366 dc->wndExtY = dcs->wndExtY;
367 dc->vportOrgX = dcs->vportOrgX;
368 dc->vportOrgY = dcs->vportOrgY;
369 dc->vportExtX = dcs->vportExtX;
370 dc->vportExtY = dcs->vportExtY;
372 if (!(dc->w.flags & DC_MEMORY)) dc->w.bitsPerPixel = dcs->w.bitsPerPixel;
374 if (dcs->w.hClipRgn)
376 if (!dc->w.hClipRgn) dc->w.hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
377 CombineRgn( dc->w.hClipRgn, dcs->w.hClipRgn, 0, RGN_COPY );
379 else
381 if (dc->w.hClipRgn) DeleteObject16( dc->w.hClipRgn );
382 dc->w.hClipRgn = 0;
384 CLIPPING_UpdateGCRegion( dc );
386 SelectObject( hdc, dcs->w.hBitmap );
387 SelectObject( hdc, dcs->w.hBrush );
388 SelectObject( hdc, dcs->w.hFont );
389 SelectObject( hdc, dcs->w.hPen );
390 SetBkColor( hdc, dcs->w.backgroundColor);
391 SetTextColor( hdc, dcs->w.textColor);
392 GDISelectPalette16( hdc, dcs->w.hPalette, FALSE );
393 GDI_HEAP_UNLOCK( hdc );
394 GDI_HEAP_UNLOCK( hdcs );
398 /***********************************************************************
399 * SaveDC16 (GDI.30)
401 INT16 WINAPI SaveDC16( HDC16 hdc )
403 return (INT16)SaveDC( hdc );
407 /***********************************************************************
408 * SaveDC32 (GDI32.292)
410 INT WINAPI SaveDC( HDC hdc )
412 HDC hdcs;
413 DC * dc, * dcs;
414 INT ret;
416 dc = DC_GetDCPtr( hdc );
417 if (!dc) return 0;
419 if(dc->funcs->pSaveDC)
420 return dc->funcs->pSaveDC( dc );
422 if (!(hdcs = GetDCState16( hdc )))
424 GDI_HEAP_UNLOCK( hdc );
425 return 0;
427 dcs = (DC *) GDI_HEAP_LOCK( hdcs );
429 /* Copy path. The reason why path saving / restoring is in SaveDC/
430 * RestoreDC and not in GetDCState/SetDCState is that the ...DCState
431 * functions are only in Win16 (which doesn't have paths) and that
432 * SetDCState doesn't allow us to signal an error (which can happen
433 * when copying paths).
435 if (!PATH_AssignGdiPath( &dcs->w.path, &dc->w.path ))
437 GDI_HEAP_UNLOCK( hdc );
438 GDI_HEAP_UNLOCK( hdcs );
439 DeleteDC( hdcs );
440 return 0;
443 dcs->header.hNext = dc->header.hNext;
444 dc->header.hNext = hdcs;
445 TRACE("(%04x): returning %d\n", hdc, dc->saveLevel+1 );
446 ret = ++dc->saveLevel;
447 GDI_HEAP_UNLOCK( hdcs );
448 GDI_HEAP_UNLOCK( hdc );
449 return ret;
453 /***********************************************************************
454 * RestoreDC16 (GDI.39)
456 BOOL16 WINAPI RestoreDC16( HDC16 hdc, INT16 level )
458 return RestoreDC( hdc, level );
462 /***********************************************************************
463 * RestoreDC32 (GDI32.290)
465 BOOL WINAPI RestoreDC( HDC hdc, INT level )
467 DC * dc, * dcs;
468 BOOL success;
470 TRACE("%04x %d\n", hdc, level );
471 dc = DC_GetDCPtr( hdc );
472 if(!dc) return FALSE;
473 if(dc->funcs->pRestoreDC)
474 return dc->funcs->pRestoreDC( dc, level );
476 if (level == -1) level = dc->saveLevel;
477 if ((level < 1) || (level > dc->saveLevel))
479 GDI_HEAP_UNLOCK( hdc );
480 return FALSE;
483 success=TRUE;
484 while (dc->saveLevel >= level)
486 HDC16 hdcs = dc->header.hNext;
487 if (!(dcs = (DC *) GDI_GetObjPtr( hdcs, DC_MAGIC )))
489 GDI_HEAP_UNLOCK( hdc );
490 return FALSE;
492 dc->header.hNext = dcs->header.hNext;
493 if (--dc->saveLevel < level)
495 SetDCState16( hdc, hdcs );
496 if (!PATH_AssignGdiPath( &dc->w.path, &dcs->w.path ))
497 /* FIXME: This might not be quite right, since we're
498 * returning FALSE but still destroying the saved DC state */
499 success=FALSE;
501 DeleteDC( hdcs );
503 GDI_HEAP_UNLOCK( hdc );
504 return success;
508 /***********************************************************************
509 * CreateDC16 (GDI.53)
511 HDC16 WINAPI CreateDC16( LPCSTR driver, LPCSTR device, LPCSTR output,
512 const DEVMODEA *initData )
514 DC * dc;
515 const DC_FUNCTIONS *funcs;
516 char buf[300];
518 if (device) {
519 if(!DRIVER_GetDriverName( device, buf, sizeof(buf) )) return 0;
520 } else
521 strcpy(buf, driver);
523 if (!(funcs = DRIVER_FindDriver( buf ))) return 0;
524 if (!(dc = DC_AllocDC( funcs ))) return 0;
525 dc->w.flags = 0;
527 TRACE("(driver=%s, device=%s, output=%s): returning %04x\n",
528 debugstr_a(driver), debugstr_a(device), debugstr_a(output), dc->hSelf );
530 if (dc->funcs->pCreateDC &&
531 !dc->funcs->pCreateDC( dc, buf, device, output, initData ))
533 WARN("creation aborted by device\n" );
534 GDI_HEAP_FREE( dc->hSelf );
535 return 0;
538 DC_InitDC( dc );
539 GDI_HEAP_UNLOCK( dc->hSelf );
540 return dc->hSelf;
544 /***********************************************************************
545 * CreateDC32A (GDI32.)
547 HDC WINAPI CreateDCA( LPCSTR driver, LPCSTR device, LPCSTR output,
548 const DEVMODEA *initData )
550 return CreateDC16( driver, device, output, (const DEVMODEA *)initData );
554 /***********************************************************************
555 * CreateDC32W (GDI32.)
557 HDC WINAPI CreateDCW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
558 const DEVMODEW *initData )
560 LPSTR driverA = HEAP_strdupWtoA( GetProcessHeap(), 0, driver );
561 LPSTR deviceA = HEAP_strdupWtoA( GetProcessHeap(), 0, device );
562 LPSTR outputA = HEAP_strdupWtoA( GetProcessHeap(), 0, output );
563 HDC res = CreateDC16( driverA, deviceA, outputA,
564 (const DEVMODEA *)initData /*FIXME*/ );
565 HeapFree( GetProcessHeap(), 0, driverA );
566 HeapFree( GetProcessHeap(), 0, deviceA );
567 HeapFree( GetProcessHeap(), 0, outputA );
568 return res;
572 /***********************************************************************
573 * CreateIC16 (GDI.153)
575 HDC16 WINAPI CreateIC16( LPCSTR driver, LPCSTR device, LPCSTR output,
576 const DEVMODEA* initData )
578 /* Nothing special yet for ICs */
579 return CreateDC16( driver, device, output, initData );
583 /***********************************************************************
584 * CreateIC32A (GDI32.49)
586 HDC WINAPI CreateICA( LPCSTR driver, LPCSTR device, LPCSTR output,
587 const DEVMODEA* initData )
589 /* Nothing special yet for ICs */
590 return CreateDCA( driver, device, output, initData );
594 /***********************************************************************
595 * CreateIC32W (GDI32.50)
597 HDC WINAPI CreateICW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
598 const DEVMODEW* initData )
600 /* Nothing special yet for ICs */
601 return CreateDCW( driver, device, output, initData );
605 /***********************************************************************
606 * CreateCompatibleDC16 (GDI.52)
608 HDC16 WINAPI CreateCompatibleDC16( HDC16 hdc )
610 return (HDC16)CreateCompatibleDC( hdc );
614 /***********************************************************************
615 * CreateCompatibleDC32 (GDI32.31)
617 HDC WINAPI CreateCompatibleDC( HDC hdc )
619 DC *dc, *origDC;
620 HBITMAP hbitmap;
621 const DC_FUNCTIONS *funcs;
623 if ((origDC = (DC *)GDI_GetObjPtr( hdc, DC_MAGIC ))) funcs = origDC->funcs;
624 else funcs = DRIVER_FindDriver( "DISPLAY" );
625 if (!funcs) return 0;
627 if (!(dc = DC_AllocDC( funcs ))) return 0;
629 TRACE("(%04x): returning %04x\n",
630 hdc, dc->hSelf );
632 /* Create default bitmap */
633 if (!(hbitmap = CreateBitmap( 1, 1, 1, 1, NULL )))
635 GDI_HEAP_FREE( dc->hSelf );
636 return 0;
638 dc->w.flags = DC_MEMORY;
639 dc->w.bitsPerPixel = 1;
640 dc->w.hBitmap = hbitmap;
641 dc->w.hFirstBitmap = hbitmap;
643 /* Copy the driver-specific physical device info into
644 * the new DC. The driver may use this read-only info
645 * while creating the compatible DC below. */
646 if (origDC)
647 dc->physDev = origDC->physDev;
649 if (dc->funcs->pCreateDC &&
650 !dc->funcs->pCreateDC( dc, NULL, NULL, NULL, NULL ))
652 WARN("creation aborted by device\n");
653 DeleteObject( hbitmap );
654 GDI_HEAP_FREE( dc->hSelf );
655 return 0;
658 DC_InitDC( dc );
659 GDI_HEAP_UNLOCK( dc->hSelf );
660 return dc->hSelf;
664 /***********************************************************************
665 * DeleteDC16 (GDI.68)
667 BOOL16 WINAPI DeleteDC16( HDC16 hdc )
669 return DeleteDC( hdc );
673 /***********************************************************************
674 * DeleteDC32 (GDI32.67)
676 BOOL WINAPI DeleteDC( HDC hdc )
678 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
679 if (!dc) return FALSE;
681 TRACE("%04x\n", hdc );
683 /* Call hook procedure to check whether is it OK to delete this DC */
684 if ( dc->hookThunk && !(dc->w.flags & (DC_SAVED | DC_MEMORY))
685 && dc->hookThunk( hdc, DCHC_DELETEDC, dc->dwHookData, 0 ) == FALSE )
687 GDI_HEAP_UNLOCK( hdc );
688 return FALSE;
691 while (dc->saveLevel)
693 DC * dcs;
694 HDC16 hdcs = dc->header.hNext;
695 if (!(dcs = (DC *) GDI_GetObjPtr( hdcs, DC_MAGIC ))) break;
696 dc->header.hNext = dcs->header.hNext;
697 dc->saveLevel--;
698 DeleteDC( hdcs );
701 if (!(dc->w.flags & DC_SAVED))
703 SelectObject( hdc, STOCK_BLACK_PEN );
704 SelectObject( hdc, STOCK_WHITE_BRUSH );
705 SelectObject( hdc, STOCK_SYSTEM_FONT );
706 if (dc->w.flags & DC_MEMORY) DeleteObject( dc->w.hFirstBitmap );
707 if (dc->funcs->pDeleteDC) dc->funcs->pDeleteDC(dc);
710 if (dc->w.hClipRgn) DeleteObject( dc->w.hClipRgn );
711 if (dc->w.hVisRgn) DeleteObject( dc->w.hVisRgn );
712 if (dc->w.hGCClipRgn) DeleteObject( dc->w.hGCClipRgn );
713 if (dc->w.pAbortProc) THUNK_Free( (FARPROC)dc->w.pAbortProc );
714 if (dc->hookThunk) THUNK_Free( (FARPROC)dc->hookThunk );
715 PATH_DestroyGdiPath(&dc->w.path);
717 return GDI_FreeObject( hdc );
721 /***********************************************************************
722 * ResetDC16 (GDI.376)
724 HDC16 WINAPI ResetDC16( HDC16 hdc, const DEVMODEA *devmode )
726 FIXME("stub\n" );
727 return hdc;
731 /***********************************************************************
732 * ResetDC32A (GDI32.287)
734 HDC WINAPI ResetDCA( HDC hdc, const DEVMODEA *devmode )
736 FIXME("stub\n" );
737 return hdc;
741 /***********************************************************************
742 * ResetDC32W (GDI32.288)
744 HDC WINAPI ResetDCW( HDC hdc, const DEVMODEW *devmode )
746 FIXME("stub\n" );
747 return hdc;
751 /***********************************************************************
752 * GetDeviceCaps16 (GDI.80)
754 INT16 WINAPI GetDeviceCaps16( HDC16 hdc, INT16 cap )
756 return GetDeviceCaps( hdc, cap );
760 /***********************************************************************
761 * GetDeviceCaps32 (GDI32.171)
763 INT WINAPI GetDeviceCaps( HDC hdc, INT cap )
765 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
766 INT ret;
767 POINT pt;
769 if (!dc) return 0;
771 /* Device capabilities for the printer */
772 switch (cap)
774 case PHYSICALWIDTH:
775 if(Escape(hdc, GETPHYSPAGESIZE, 0, NULL, (LPVOID)&pt) > 0)
776 return pt.x;
777 case PHYSICALHEIGHT:
778 if(Escape(hdc, GETPHYSPAGESIZE, 0, NULL, (LPVOID)&pt) > 0)
779 return pt.y;
780 case PHYSICALOFFSETX:
781 if(Escape(hdc, GETPRINTINGOFFSET, 0, NULL, (LPVOID)&pt) > 0)
782 return pt.x;
783 case PHYSICALOFFSETY:
784 if(Escape(hdc, GETPRINTINGOFFSET, 0, NULL, (LPVOID)&pt) > 0)
785 return pt.y;
786 case SCALINGFACTORX:
787 if(Escape(hdc, GETSCALINGFACTOR, 0, NULL, (LPVOID)&pt) > 0)
788 return pt.x;
789 case SCALINGFACTORY:
790 if(Escape(hdc, GETSCALINGFACTOR, 0, NULL, (LPVOID)&pt) > 0)
791 return pt.y;
794 if ((cap < 0) || (cap > sizeof(DeviceCaps)-sizeof(WORD)))
796 GDI_HEAP_UNLOCK( hdc );
797 return 0;
800 TRACE("(%04x,%d): returning %d\n",
801 hdc, cap, *(WORD *)(((char *)dc->w.devCaps) + cap) );
802 ret = *(WORD *)(((char *)dc->w.devCaps) + cap);
803 GDI_HEAP_UNLOCK( hdc );
804 return ret;
808 /***********************************************************************
809 * SetBkColor16 (GDI.1)
811 COLORREF WINAPI SetBkColor16( HDC16 hdc, COLORREF color )
813 return SetBkColor( hdc, color );
817 /***********************************************************************
818 * SetBkColor32 (GDI32.305)
820 COLORREF WINAPI SetBkColor( HDC hdc, COLORREF color )
822 COLORREF oldColor;
823 DC * dc = DC_GetDCPtr( hdc );
825 if (!dc) return 0x80000000;
826 if (dc->funcs->pSetBkColor)
827 oldColor = dc->funcs->pSetBkColor(dc, color);
828 else {
829 oldColor = dc->w.backgroundColor;
830 dc->w.backgroundColor = color;
832 GDI_HEAP_UNLOCK( hdc );
833 return oldColor;
837 /***********************************************************************
838 * SetTextColor16 (GDI.9)
840 COLORREF WINAPI SetTextColor16( HDC16 hdc, COLORREF color )
842 return SetTextColor( hdc, color );
846 /***********************************************************************
847 * SetTextColor32 (GDI32.338)
849 COLORREF WINAPI SetTextColor( HDC hdc, COLORREF color )
851 COLORREF oldColor;
852 DC * dc = DC_GetDCPtr( hdc );
854 if (!dc) return 0x80000000;
855 if (dc->funcs->pSetTextColor)
856 oldColor = dc->funcs->pSetTextColor(dc, color);
857 else {
858 oldColor = dc->w.textColor;
859 dc->w.textColor = color;
861 GDI_HEAP_UNLOCK( hdc );
862 return oldColor;
865 /***********************************************************************
866 * SetTextAlign16 (GDI.346)
868 UINT16 WINAPI SetTextAlign16( HDC16 hdc, UINT16 align )
870 return SetTextAlign( hdc, align );
874 /***********************************************************************
875 * SetTextAlign (GDI32.336)
877 UINT WINAPI SetTextAlign( HDC hdc, UINT align )
879 UINT prevAlign;
880 DC *dc = DC_GetDCPtr( hdc );
881 if (!dc) return 0x0;
882 if (dc->funcs->pSetTextAlign)
883 prevAlign = dc->funcs->pSetTextAlign(dc, align);
884 else {
885 prevAlign = dc->w.textAlign;
886 dc->w.textAlign = align;
888 GDI_HEAP_UNLOCK( hdc );
889 return prevAlign;
892 /***********************************************************************
893 * GetDCOrgEx (GDI32.168)
895 BOOL WINAPI GetDCOrgEx( HDC hDC, LPPOINT lpp )
897 DC * dc;
899 if (!lpp) return FALSE;
900 if (!(dc = (DC *) GDI_GetObjPtr( hDC, DC_MAGIC ))) return FALSE;
902 #ifndef X_DISPLAY_MISSING
903 if (!(dc->w.flags & DC_MEMORY))
905 X11DRV_PDEVICE *physDev;
906 Window root;
907 int w, h, border, depth;
909 physDev = (X11DRV_PDEVICE *) dc->physDev;
911 /* FIXME: this is not correct for managed windows */
912 TSXGetGeometry( display, physDev->drawable, &root,
913 (int*)&lpp->x, (int*)&lpp->y, &w, &h, &border, &depth );
915 else
916 #endif /* !defined(X_DISPLAY_MISSING) */
917 lpp->x = lpp->y = 0;
919 lpp->x += dc->w.DCOrgX; lpp->y += dc->w.DCOrgY;
920 GDI_HEAP_UNLOCK( hDC );
921 return TRUE;
925 /***********************************************************************
926 * GetDCOrg (GDI.79)
928 DWORD WINAPI GetDCOrg16( HDC16 hdc )
930 POINT pt;
931 if( GetDCOrgEx( hdc, &pt) )
932 return MAKELONG( (WORD)pt.x, (WORD)pt.y );
933 return 0;
937 /***********************************************************************
938 * SetDCOrg (GDI.117)
940 DWORD WINAPI SetDCOrg16( HDC16 hdc, INT16 x, INT16 y )
942 DWORD prevOrg;
943 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
944 if (!dc) return 0;
945 prevOrg = dc->w.DCOrgX | (dc->w.DCOrgY << 16);
946 dc->w.DCOrgX = x;
947 dc->w.DCOrgY = y;
948 GDI_HEAP_UNLOCK( hdc );
949 return prevOrg;
953 /***********************************************************************
954 * GetGraphicsMode (GDI32.188)
956 INT WINAPI GetGraphicsMode( HDC hdc )
958 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
959 if (!dc) return 0;
960 return dc->w.GraphicsMode;
964 /***********************************************************************
965 * SetGraphicsMode (GDI32.317)
967 INT WINAPI SetGraphicsMode( HDC hdc, INT mode )
969 INT ret;
970 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
972 /* One would think that setting the graphics mode to GM_COMPATIBLE
973 * would also reset the world transformation matrix to the unity
974 * matrix. However, in Windows, this is not the case. This doesn't
975 * make a lot of sense to me, but that's the way it is.
978 if (!dc) return 0;
979 if ((mode <= 0) || (mode > GM_LAST)) return 0;
980 ret = dc->w.GraphicsMode;
981 dc->w.GraphicsMode = mode;
982 return ret;
986 /***********************************************************************
987 * GetArcDirection16 (GDI.524)
989 INT16 WINAPI GetArcDirection16( HDC16 hdc )
991 return GetArcDirection( (HDC)hdc );
995 /***********************************************************************
996 * GetArcDirection32 (GDI32.141)
998 INT WINAPI GetArcDirection( HDC hdc )
1000 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1002 if (!dc)
1003 return 0;
1005 return dc->w.ArcDirection;
1009 /***********************************************************************
1010 * SetArcDirection16 (GDI.525)
1012 INT16 WINAPI SetArcDirection16( HDC16 hdc, INT16 nDirection )
1014 return SetArcDirection( (HDC)hdc, (INT)nDirection );
1018 /***********************************************************************
1019 * SetArcDirection32 (GDI32.302)
1021 INT WINAPI SetArcDirection( HDC hdc, INT nDirection )
1023 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1024 INT nOldDirection;
1026 if (!dc)
1027 return 0;
1029 if (nDirection!=AD_COUNTERCLOCKWISE && nDirection!=AD_CLOCKWISE)
1031 SetLastError(ERROR_INVALID_PARAMETER);
1032 return 0;
1035 nOldDirection = dc->w.ArcDirection;
1036 dc->w.ArcDirection = nDirection;
1038 return nOldDirection;
1042 /***********************************************************************
1043 * GetWorldTransform (GDI32.244)
1045 BOOL WINAPI GetWorldTransform( HDC hdc, LPXFORM xform )
1047 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1049 if (!dc)
1050 return FALSE;
1051 if (!xform)
1052 return FALSE;
1054 *xform = dc->w.xformWorld2Wnd;
1056 return TRUE;
1060 /***********************************************************************
1061 * SetWorldTransform (GDI32.346)
1063 BOOL WINAPI SetWorldTransform( HDC hdc, const XFORM *xform )
1065 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1067 if (!dc)
1069 SetLastError( ERROR_INVALID_HANDLE );
1070 return FALSE;
1073 if (!xform)
1074 return FALSE;
1076 /* Check that graphics mode is GM_ADVANCED */
1077 if (dc->w.GraphicsMode!=GM_ADVANCED)
1078 return FALSE;
1080 dc->w.xformWorld2Wnd = *xform;
1082 DC_UpdateXforms( dc );
1084 return TRUE;
1088 /****************************************************************************
1089 * ModifyWorldTransform [GDI32.253]
1090 * Modifies the world transformation for a device context.
1092 * PARAMS
1093 * hdc [I] Handle to device context
1094 * xform [I] XFORM structure that will be used to modify the world
1095 * transformation
1096 * iMode [I] Specifies in what way to modify the world transformation
1097 * Possible values:
1098 * MWT_IDENTITY
1099 * Resets the world transformation to the identity matrix.
1100 * The parameter xform is ignored.
1101 * MWT_LEFTMULTIPLY
1102 * Multiplies xform into the world transformation matrix from
1103 * the left.
1104 * MWT_RIGHTMULTIPLY
1105 * Multiplies xform into the world transformation matrix from
1106 * the right.
1108 * RETURNS STD
1110 BOOL WINAPI ModifyWorldTransform( HDC hdc, const XFORM *xform,
1111 DWORD iMode )
1113 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1115 /* Check for illegal parameters */
1116 if (!dc)
1118 SetLastError( ERROR_INVALID_HANDLE );
1119 return FALSE;
1121 if (!xform)
1122 return FALSE;
1124 /* Check that graphics mode is GM_ADVANCED */
1125 if (dc->w.GraphicsMode!=GM_ADVANCED)
1126 return FALSE;
1128 switch (iMode)
1130 case MWT_IDENTITY:
1131 dc->w.xformWorld2Wnd.eM11 = 1.0f;
1132 dc->w.xformWorld2Wnd.eM12 = 0.0f;
1133 dc->w.xformWorld2Wnd.eM21 = 0.0f;
1134 dc->w.xformWorld2Wnd.eM22 = 1.0f;
1135 dc->w.xformWorld2Wnd.eDx = 0.0f;
1136 dc->w.xformWorld2Wnd.eDy = 0.0f;
1137 break;
1138 case MWT_LEFTMULTIPLY:
1139 CombineTransform( &dc->w.xformWorld2Wnd, xform,
1140 &dc->w.xformWorld2Wnd );
1141 break;
1142 case MWT_RIGHTMULTIPLY:
1143 CombineTransform( &dc->w.xformWorld2Wnd, &dc->w.xformWorld2Wnd,
1144 xform );
1145 break;
1146 default:
1147 return FALSE;
1150 DC_UpdateXforms( dc );
1152 return TRUE;
1156 /****************************************************************************
1157 * CombineTransform [GDI32.20]
1158 * Combines two transformation matrices.
1160 * PARAMS
1161 * xformResult [O] Stores the result of combining the two matrices
1162 * xform1 [I] Specifies the first matrix to apply
1163 * xform2 [I] Specifies the second matrix to apply
1165 * REMARKS
1166 * The same matrix can be passed in for more than one of the parameters.
1168 * RETURNS STD
1170 BOOL WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
1171 const XFORM *xform2 )
1173 XFORM xformTemp;
1175 /* Check for illegal parameters */
1176 if (!xformResult || !xform1 || !xform2)
1177 return FALSE;
1179 /* Create the result in a temporary XFORM, since xformResult may be
1180 * equal to xform1 or xform2 */
1181 xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
1182 xform1->eM12 * xform2->eM21;
1183 xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
1184 xform1->eM12 * xform2->eM22;
1185 xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
1186 xform1->eM22 * xform2->eM21;
1187 xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
1188 xform1->eM22 * xform2->eM22;
1189 xformTemp.eDx = xform1->eDx * xform2->eM11 +
1190 xform1->eDy * xform2->eM21 +
1191 xform2->eDx;
1192 xformTemp.eDy = xform1->eDx * xform2->eM12 +
1193 xform1->eDy * xform2->eM22 +
1194 xform2->eDy;
1196 /* Copy the result to xformResult */
1197 *xformResult = xformTemp;
1199 return TRUE;
1203 /***********************************************************************
1204 * SetDCHook (GDI.190)
1206 /* ### start build ### */
1207 extern WORD CALLBACK GDI_CallTo16_word_wwll(FARPROC16,WORD,WORD,LONG,LONG);
1208 /* ### stop build ### */
1209 BOOL16 WINAPI SetDCHook( HDC16 hdc, FARPROC16 hookProc, DWORD dwHookData )
1211 DC *dc = (DC *)GDI_GetObjPtr( hdc, DC_MAGIC );
1212 if (!dc) return FALSE;
1215 * Note: We store the original SEGPTR 'hookProc' as we need to be
1216 * able to return it verbatim in GetDCHook,
1218 * On the other hand, we still call THUNK_Alloc and store the
1219 * 32-bit thunk into another DC member, because THUNK_Alloc
1220 * recognizes the (typical) case that the 'hookProc' is indeed
1221 * the 16-bit API entry point of a built-in routine (e.g. DCHook16)
1223 * We could perform that test every time the hook is about to
1224 * be called (or else we could live with the 32->16->32 detour),
1225 * but this way is the most efficient ...
1228 dc->hookProc = hookProc;
1229 dc->dwHookData = dwHookData;
1231 THUNK_Free( (FARPROC)dc->hookThunk );
1232 dc->hookThunk = (DCHOOKPROC)
1233 THUNK_Alloc( hookProc, (RELAY)GDI_CallTo16_word_wwll );
1235 GDI_HEAP_UNLOCK( hdc );
1236 return TRUE;
1240 /***********************************************************************
1241 * GetDCHook (GDI.191)
1243 DWORD WINAPI GetDCHook( HDC16 hdc, FARPROC16 *phookProc )
1245 DC *dc = (DC *)GDI_GetObjPtr( hdc, DC_MAGIC );
1246 if (!dc) return 0;
1247 *phookProc = dc->hookProc;
1248 GDI_HEAP_UNLOCK( hdc );
1249 return dc->dwHookData;
1253 /***********************************************************************
1254 * SetHookFlags (GDI.192)
1256 WORD WINAPI SetHookFlags16(HDC16 hDC, WORD flags)
1258 DC* dc = (DC*)GDI_GetObjPtr( hDC, DC_MAGIC );
1260 if( dc )
1262 WORD wRet = dc->w.flags & DC_DIRTY;
1264 /* "Undocumented Windows" info is slightly confusing.
1267 TRACE("hDC %04x, flags %04x\n",hDC,flags);
1269 if( flags & DCHF_INVALIDATEVISRGN )
1270 dc->w.flags |= DC_DIRTY;
1271 else if( flags & DCHF_VALIDATEVISRGN || !flags )
1272 dc->w.flags &= ~DC_DIRTY;
1273 GDI_HEAP_UNLOCK( hDC );
1274 return wRet;
1276 return 0;
1279 /***********************************************************************
1280 * SetICMMode (GDI32.318)
1282 INT WINAPI SetICMMode(HDC hdc, INT iEnableICM)
1284 /*FIXME Asuming that ICM is always off, and cannot be turned on */
1285 if (iEnableICM == ICM_OFF) return ICM_OFF;
1286 if (iEnableICM == ICM_ON) return 0;
1287 if (iEnableICM == ICM_QUERY) return ICM_OFF;
1288 return 0;
1292 /***********************************************************************
1293 * GetColorSpace (GDI32.165)
1295 HCOLORSPACE WINAPI GetColorSpace(HDC hdc)
1297 /*FIXME Need to to whatever GetColorSpace actually does */
1298 return 0;
1301 /***********************************************************************
1302 * GetBoundsRect16 (GDI.194)
1304 UINT16 WINAPI GetBoundsRect16(HDC16 hdc, LPRECT16 rect, UINT16 flags)
1306 return DCB_RESET | DCB_DISABLE; /* bounding rectangle always empty and disabled*/
1309 /***********************************************************************
1310 * GetBoundsRect32 (GDI32.147)
1312 UINT WINAPI GetBoundsRect(HDC hdc, LPRECT rect, UINT flags)
1314 FIXME("(): stub\n");
1315 return DCB_RESET; /* bounding rectangle always empty */
1318 /***********************************************************************
1319 * SetBoundsRect16 (GDI.193)
1321 UINT16 WINAPI SetBoundsRect16(HDC16 hdc, const RECT16* rect, UINT16 flags)
1323 if ( (flags & DCB_ACCUMULATE) || (flags & DCB_ENABLE) )
1324 FIXME("(%04x, %p, %04x): stub\n", hdc, rect, flags );
1326 return DCB_RESET | DCB_DISABLE; /* bounding rectangle always empty and disabled*/
1329 /***********************************************************************
1330 * SetBoundsRect32 (GDI32.307)
1332 UINT WINAPI SetBoundsRect(HDC hdc, const RECT* rect, UINT flags)
1334 FIXME("(): stub\n");
1335 return DCB_DISABLE; /* bounding rectangle always empty */
1338 /***********************************************************************
1339 * Death (GDI.121)
1341 * Disables GDI, switches back to text mode.
1342 * We don't have to do anything here,
1343 * just let console support handle everything
1345 void WINAPI Death16(HDC16 hDC)
1347 MESSAGE("Death(%04x) called. Application enters text mode...\n", hDC);
1350 /***********************************************************************
1351 * Resurrection (GDI.122)
1353 * Restores GDI functionality
1355 void WINAPI Resurrection16(HDC16 hDC,
1356 WORD w1, WORD w2, WORD w3, WORD w4, WORD w5, WORD w6)
1358 MESSAGE("Resurrection(%04x, %04x, %04x, %04x, %04x, %04x, %04x) called. Application left text mode.\n", hDC, w1, w2, w3, w4, w5, w6);