Recovery of release 990110 after disk crash.
[wine/multimedia.git] / objects / dc.c
blob981d5d3ae8551796d30fdd12edd387a35f7b479c
1 /*
2 * GDI Device Context functions
4 * Copyright 1993 Alexandre Julliard
6 */
8 #include <stdlib.h>
9 #include <string.h>
10 #include "dc.h"
11 #include "gdi.h"
12 #include "heap.h"
13 #include "metafile.h"
14 #include "debug.h"
15 #include "font.h"
16 #include "winerror.h"
17 #include "x11drv.h"
18 #include "ts_xlib.h"
20 /***********************************************************************
21 * DC_Init_DC_INFO
23 * Fill the WIN_DC_INFO structure.
25 static void DC_Init_DC_INFO( WIN_DC_INFO *win_dc_info )
27 win_dc_info->flags = 0;
28 win_dc_info->devCaps = NULL;
29 win_dc_info->hClipRgn = 0;
30 win_dc_info->hVisRgn = 0;
31 win_dc_info->hGCClipRgn = 0;
32 win_dc_info->hPen = STOCK_BLACK_PEN;
33 win_dc_info->hBrush = STOCK_WHITE_BRUSH;
34 win_dc_info->hFont = STOCK_SYSTEM_FONT;
35 win_dc_info->hBitmap = 0;
36 win_dc_info->hFirstBitmap = 0;
37 win_dc_info->hDevice = 0;
38 win_dc_info->hPalette = STOCK_DEFAULT_PALETTE;
39 win_dc_info->ROPmode = R2_COPYPEN;
40 win_dc_info->polyFillMode = ALTERNATE;
41 win_dc_info->stretchBltMode = BLACKONWHITE;
42 win_dc_info->relAbsMode = ABSOLUTE;
43 win_dc_info->backgroundMode = OPAQUE;
44 win_dc_info->backgroundColor = RGB( 255, 255, 255 );
45 win_dc_info->textColor = RGB( 0, 0, 0 );
46 win_dc_info->brushOrgX = 0;
47 win_dc_info->brushOrgY = 0;
48 win_dc_info->textAlign = TA_LEFT | TA_TOP | TA_NOUPDATECP;
49 win_dc_info->charExtra = 0;
50 win_dc_info->breakTotalExtra = 0;
51 win_dc_info->breakCount = 0;
52 win_dc_info->breakExtra = 0;
53 win_dc_info->breakRem = 0;
54 win_dc_info->bitsPerPixel = 1;
55 win_dc_info->MapMode = MM_TEXT;
56 win_dc_info->GraphicsMode = GM_COMPATIBLE;
57 win_dc_info->DCOrgX = 0;
58 win_dc_info->DCOrgY = 0;
59 win_dc_info->lpfnPrint = NULL;
60 win_dc_info->CursPosX = 0;
61 win_dc_info->CursPosY = 0;
62 win_dc_info->ArcDirection = AD_COUNTERCLOCKWISE;
63 win_dc_info->xformWorld2Wnd.eM11 = 1.0f;
64 win_dc_info->xformWorld2Wnd.eM12 = 0.0f;
65 win_dc_info->xformWorld2Wnd.eM21 = 0.0f;
66 win_dc_info->xformWorld2Wnd.eM22 = 1.0f;
67 win_dc_info->xformWorld2Wnd.eDx = 0.0f;
68 win_dc_info->xformWorld2Wnd.eDy = 0.0f;
69 win_dc_info->xformWorld2Vport = win_dc_info->xformWorld2Wnd;
70 win_dc_info->xformVport2World = win_dc_info->xformWorld2Wnd;
71 win_dc_info->vport2WorldValid = TRUE;
73 PATH_InitGdiPath(&win_dc_info->path);
77 /***********************************************************************
78 * DC_AllocDC
80 DC *DC_AllocDC( const DC_FUNCTIONS *funcs )
82 HDC16 hdc;
83 DC *dc;
85 if (!(hdc = GDI_AllocObject( sizeof(DC), DC_MAGIC ))) return NULL;
86 dc = (DC *) GDI_HEAP_LOCK( hdc );
88 dc->hSelf = hdc;
89 dc->funcs = funcs;
90 dc->physDev = NULL;
91 dc->saveLevel = 0;
92 dc->dwHookData = 0L;
93 dc->hookProc = NULL;
94 dc->wndOrgX = 0;
95 dc->wndOrgY = 0;
96 dc->wndExtX = 1;
97 dc->wndExtY = 1;
98 dc->vportOrgX = 0;
99 dc->vportOrgY = 0;
100 dc->vportExtX = 1;
101 dc->vportExtY = 1;
103 DC_Init_DC_INFO( &dc->w );
105 return dc;
110 /***********************************************************************
111 * DC_GetDCPtr
113 DC *DC_GetDCPtr( HDC32 hdc )
115 GDIOBJHDR *ptr = (GDIOBJHDR *)GDI_HEAP_LOCK( hdc );
116 if (!ptr) return NULL;
117 if ((ptr->wMagic == DC_MAGIC) || (ptr->wMagic == METAFILE_DC_MAGIC))
118 return (DC *)ptr;
119 GDI_HEAP_UNLOCK( hdc );
120 return NULL;
124 /***********************************************************************
125 * DC_InitDC
127 * Setup device-specific DC values for a newly created DC.
129 void DC_InitDC( DC* dc )
131 RealizeDefaultPalette( dc->hSelf );
132 SetTextColor32( dc->hSelf, dc->w.textColor );
133 SetBkColor32( dc->hSelf, dc->w.backgroundColor );
134 SelectObject32( dc->hSelf, dc->w.hPen );
135 SelectObject32( dc->hSelf, dc->w.hBrush );
136 SelectObject32( dc->hSelf, dc->w.hFont );
137 CLIPPING_UpdateGCRegion( dc );
141 /***********************************************************************
142 * DC_InvertXform
144 * Computes the inverse of the transformation xformSrc and stores it to
145 * xformDest. Returns TRUE if successful or FALSE if the xformSrc matrix
146 * is singular.
148 static BOOL32 DC_InvertXform( const XFORM *xformSrc, XFORM *xformDest )
150 FLOAT determinant;
152 determinant = xformSrc->eM11*xformSrc->eM22 -
153 xformSrc->eM12*xformSrc->eM21;
154 if (determinant > -1e-12 && determinant < 1e-12)
155 return FALSE;
157 xformDest->eM11 = xformSrc->eM22 / determinant;
158 xformDest->eM12 = -xformSrc->eM12 / determinant;
159 xformDest->eM21 = -xformSrc->eM21 / determinant;
160 xformDest->eM22 = xformSrc->eM11 / determinant;
161 xformDest->eDx = -xformSrc->eDx * xformDest->eM11 -
162 xformSrc->eDy * xformDest->eM21;
163 xformDest->eDy = -xformSrc->eDx * xformDest->eM12 -
164 xformSrc->eDy * xformDest->eM22;
166 return TRUE;
170 /***********************************************************************
171 * DC_UpdateXforms
173 * Updates the xformWorld2Vport, xformVport2World and vport2WorldValid
174 * fields of the specified DC by creating a transformation that
175 * represents the current mapping mode and combining it with the DC's
176 * world transform. This function should be called whenever the
177 * parameters associated with the mapping mode (window and viewport
178 * extents and origins) or the world transform change.
180 void DC_UpdateXforms( DC *dc )
182 XFORM xformWnd2Vport;
183 FLOAT scaleX, scaleY;
185 /* Construct a transformation to do the window-to-viewport conversion */
186 scaleX = (FLOAT)dc->vportExtX / (FLOAT)dc->wndExtX;
187 scaleY = (FLOAT)dc->vportExtY / (FLOAT)dc->wndExtY;
188 xformWnd2Vport.eM11 = scaleX;
189 xformWnd2Vport.eM12 = 0.0;
190 xformWnd2Vport.eM21 = 0.0;
191 xformWnd2Vport.eM22 = scaleY;
192 xformWnd2Vport.eDx = (FLOAT)dc->vportOrgX -
193 scaleX * (FLOAT)dc->wndOrgX;
194 xformWnd2Vport.eDy = (FLOAT)dc->vportOrgY -
195 scaleY * (FLOAT)dc->wndOrgY;
197 /* Combine with the world transformation */
198 CombineTransform( &dc->w.xformWorld2Vport, &dc->w.xformWorld2Wnd,
199 &xformWnd2Vport );
201 /* Create inverse of world-to-viewport transformation */
202 dc->w.vport2WorldValid = DC_InvertXform( &dc->w.xformWorld2Vport,
203 &dc->w.xformVport2World );
207 /***********************************************************************
208 * GetDCState (GDI.179)
210 HDC16 WINAPI GetDCState( HDC16 hdc )
212 DC * newdc, * dc;
213 HGDIOBJ16 handle;
215 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
216 if (!(handle = GDI_AllocObject( sizeof(DC), DC_MAGIC )))
218 GDI_HEAP_UNLOCK( hdc );
219 return 0;
221 newdc = (DC *) GDI_HEAP_LOCK( handle );
223 TRACE(dc, "(%04x): returning %04x\n", hdc, handle );
225 newdc->w.flags = dc->w.flags | DC_SAVED;
226 newdc->w.devCaps = dc->w.devCaps;
227 newdc->w.hPen = dc->w.hPen;
228 newdc->w.hBrush = dc->w.hBrush;
229 newdc->w.hFont = dc->w.hFont;
230 newdc->w.hBitmap = dc->w.hBitmap;
231 newdc->w.hFirstBitmap = dc->w.hFirstBitmap;
232 newdc->w.hDevice = dc->w.hDevice;
233 newdc->w.hPalette = dc->w.hPalette;
234 newdc->w.totalExtent = dc->w.totalExtent;
235 newdc->w.bitsPerPixel = dc->w.bitsPerPixel;
236 newdc->w.ROPmode = dc->w.ROPmode;
237 newdc->w.polyFillMode = dc->w.polyFillMode;
238 newdc->w.stretchBltMode = dc->w.stretchBltMode;
239 newdc->w.relAbsMode = dc->w.relAbsMode;
240 newdc->w.backgroundMode = dc->w.backgroundMode;
241 newdc->w.backgroundColor = dc->w.backgroundColor;
242 newdc->w.textColor = dc->w.textColor;
243 newdc->w.brushOrgX = dc->w.brushOrgX;
244 newdc->w.brushOrgY = dc->w.brushOrgY;
245 newdc->w.textAlign = dc->w.textAlign;
246 newdc->w.charExtra = dc->w.charExtra;
247 newdc->w.breakTotalExtra = dc->w.breakTotalExtra;
248 newdc->w.breakCount = dc->w.breakCount;
249 newdc->w.breakExtra = dc->w.breakExtra;
250 newdc->w.breakRem = dc->w.breakRem;
251 newdc->w.MapMode = dc->w.MapMode;
252 newdc->w.GraphicsMode = dc->w.GraphicsMode;
253 #if 0
254 /* Apparently, the DC origin is not changed by [GS]etDCState */
255 newdc->w.DCOrgX = dc->w.DCOrgX;
256 newdc->w.DCOrgY = dc->w.DCOrgY;
257 #endif
258 newdc->w.CursPosX = dc->w.CursPosX;
259 newdc->w.CursPosY = dc->w.CursPosY;
260 newdc->w.ArcDirection = dc->w.ArcDirection;
261 newdc->w.xformWorld2Wnd = dc->w.xformWorld2Wnd;
262 newdc->w.xformWorld2Vport = dc->w.xformWorld2Vport;
263 newdc->w.xformVport2World = dc->w.xformVport2World;
264 newdc->w.vport2WorldValid = dc->w.vport2WorldValid;
265 newdc->wndOrgX = dc->wndOrgX;
266 newdc->wndOrgY = dc->wndOrgY;
267 newdc->wndExtX = dc->wndExtX;
268 newdc->wndExtY = dc->wndExtY;
269 newdc->vportOrgX = dc->vportOrgX;
270 newdc->vportOrgY = dc->vportOrgY;
271 newdc->vportExtX = dc->vportExtX;
272 newdc->vportExtY = dc->vportExtY;
274 newdc->hSelf = (HDC32)handle;
275 newdc->saveLevel = 0;
277 PATH_InitGdiPath( &newdc->w.path );
279 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
281 newdc->w.hGCClipRgn = newdc->w.hVisRgn = 0;
282 if (dc->w.hClipRgn)
284 newdc->w.hClipRgn = CreateRectRgn32( 0, 0, 0, 0 );
285 CombineRgn32( newdc->w.hClipRgn, dc->w.hClipRgn, 0, RGN_COPY );
287 else
288 newdc->w.hClipRgn = 0;
289 GDI_HEAP_UNLOCK( handle );
290 GDI_HEAP_UNLOCK( hdc );
291 return handle;
295 /***********************************************************************
296 * SetDCState (GDI.180)
298 void WINAPI SetDCState( HDC16 hdc, HDC16 hdcs )
300 DC *dc, *dcs;
302 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return;
303 if (!(dcs = (DC *) GDI_GetObjPtr( hdcs, DC_MAGIC )))
305 GDI_HEAP_UNLOCK( hdc );
306 return;
308 if (!dcs->w.flags & DC_SAVED)
310 GDI_HEAP_UNLOCK( hdc );
311 GDI_HEAP_UNLOCK( hdcs );
312 return;
314 TRACE(dc, "%04x %04x\n", hdc, hdcs );
316 dc->w.flags = dcs->w.flags & ~DC_SAVED;
317 dc->w.devCaps = dcs->w.devCaps;
318 dc->w.hFirstBitmap = dcs->w.hFirstBitmap;
319 dc->w.hDevice = dcs->w.hDevice;
320 dc->w.totalExtent = dcs->w.totalExtent;
321 dc->w.ROPmode = dcs->w.ROPmode;
322 dc->w.polyFillMode = dcs->w.polyFillMode;
323 dc->w.stretchBltMode = dcs->w.stretchBltMode;
324 dc->w.relAbsMode = dcs->w.relAbsMode;
325 dc->w.backgroundMode = dcs->w.backgroundMode;
326 dc->w.backgroundColor = dcs->w.backgroundColor;
327 dc->w.textColor = dcs->w.textColor;
328 dc->w.brushOrgX = dcs->w.brushOrgX;
329 dc->w.brushOrgY = dcs->w.brushOrgY;
330 dc->w.textAlign = dcs->w.textAlign;
331 dc->w.charExtra = dcs->w.charExtra;
332 dc->w.breakTotalExtra = dcs->w.breakTotalExtra;
333 dc->w.breakCount = dcs->w.breakCount;
334 dc->w.breakExtra = dcs->w.breakExtra;
335 dc->w.breakRem = dcs->w.breakRem;
336 dc->w.MapMode = dcs->w.MapMode;
337 dc->w.GraphicsMode = dcs->w.GraphicsMode;
338 #if 0
339 /* Apparently, the DC origin is not changed by [GS]etDCState */
340 dc->w.DCOrgX = dcs->w.DCOrgX;
341 dc->w.DCOrgY = dcs->w.DCOrgY;
342 #endif
343 dc->w.CursPosX = dcs->w.CursPosX;
344 dc->w.CursPosY = dcs->w.CursPosY;
345 dc->w.ArcDirection = dcs->w.ArcDirection;
346 dc->w.xformWorld2Wnd = dcs->w.xformWorld2Wnd;
347 dc->w.xformWorld2Vport = dcs->w.xformWorld2Vport;
348 dc->w.xformVport2World = dcs->w.xformVport2World;
349 dc->w.vport2WorldValid = dcs->w.vport2WorldValid;
351 dc->wndOrgX = dcs->wndOrgX;
352 dc->wndOrgY = dcs->wndOrgY;
353 dc->wndExtX = dcs->wndExtX;
354 dc->wndExtY = dcs->wndExtY;
355 dc->vportOrgX = dcs->vportOrgX;
356 dc->vportOrgY = dcs->vportOrgY;
357 dc->vportExtX = dcs->vportExtX;
358 dc->vportExtY = dcs->vportExtY;
360 if (!(dc->w.flags & DC_MEMORY)) dc->w.bitsPerPixel = dcs->w.bitsPerPixel;
362 if (dcs->w.hClipRgn)
364 if (!dc->w.hClipRgn) dc->w.hClipRgn = CreateRectRgn32( 0, 0, 0, 0 );
365 CombineRgn32( dc->w.hClipRgn, dcs->w.hClipRgn, 0, RGN_COPY );
367 else
369 if (dc->w.hClipRgn) DeleteObject16( dc->w.hClipRgn );
370 dc->w.hClipRgn = 0;
372 CLIPPING_UpdateGCRegion( dc );
374 SelectObject32( hdc, dcs->w.hBitmap );
375 SelectObject32( hdc, dcs->w.hBrush );
376 SelectObject32( hdc, dcs->w.hFont );
377 SelectObject32( hdc, dcs->w.hPen );
378 SetBkColor32( hdc, dcs->w.backgroundColor);
379 SetTextColor32( hdc, dcs->w.textColor);
380 GDISelectPalette( hdc, dcs->w.hPalette, FALSE );
381 GDI_HEAP_UNLOCK( hdc );
382 GDI_HEAP_UNLOCK( hdcs );
386 /***********************************************************************
387 * SaveDC16 (GDI.30)
389 INT16 WINAPI SaveDC16( HDC16 hdc )
391 return (INT16)SaveDC32( hdc );
395 /***********************************************************************
396 * SaveDC32 (GDI32.292)
398 INT32 WINAPI SaveDC32( HDC32 hdc )
400 HDC32 hdcs;
401 DC * dc, * dcs;
402 INT32 ret;
404 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
405 if (!dc)
407 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
408 if (!dc) return 0;
409 MF_MetaParam0(dc, META_SAVEDC);
410 GDI_HEAP_UNLOCK( hdc );
411 return 1; /* ?? */
413 if (!(hdcs = GetDCState( hdc )))
415 GDI_HEAP_UNLOCK( hdc );
416 return 0;
418 dcs = (DC *) GDI_HEAP_LOCK( hdcs );
420 /* Copy path. The reason why path saving / restoring is in SaveDC/
421 * RestoreDC and not in GetDCState/SetDCState is that the ...DCState
422 * functions are only in Win16 (which doesn't have paths) and that
423 * SetDCState doesn't allow us to signal an error (which can happen
424 * when copying paths).
426 if (!PATH_AssignGdiPath( &dcs->w.path, &dc->w.path ))
428 GDI_HEAP_UNLOCK( hdc );
429 GDI_HEAP_UNLOCK( hdcs );
430 DeleteDC32( hdcs );
431 return 0;
434 dcs->header.hNext = dc->header.hNext;
435 dc->header.hNext = hdcs;
436 TRACE(dc, "(%04x): returning %d\n", hdc, dc->saveLevel+1 );
437 ret = ++dc->saveLevel;
438 GDI_HEAP_UNLOCK( hdcs );
439 GDI_HEAP_UNLOCK( hdc );
440 return ret;
444 /***********************************************************************
445 * RestoreDC16 (GDI.39)
447 BOOL16 WINAPI RestoreDC16( HDC16 hdc, INT16 level )
449 return RestoreDC32( hdc, level );
453 /***********************************************************************
454 * RestoreDC32 (GDI32.290)
456 BOOL32 WINAPI RestoreDC32( HDC32 hdc, INT32 level )
458 DC * dc, * dcs;
459 BOOL32 success;
461 TRACE(dc, "%04x %d\n", hdc, level );
462 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
463 if (!dc)
465 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
466 if (!dc) return FALSE;
467 if (level != -1)
469 GDI_HEAP_UNLOCK( hdc );
470 return FALSE;
472 MF_MetaParam1(dc, META_RESTOREDC, level);
473 GDI_HEAP_UNLOCK( hdc );
474 return TRUE;
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 SetDCState( 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 DeleteDC32( 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 DEVMODE16 *initData )
514 DC * dc;
515 const DC_FUNCTIONS *funcs;
517 if (!(funcs = DRIVER_FindDriver( driver ))) return 0;
518 if (!(dc = DC_AllocDC( funcs ))) return 0;
519 dc->w.flags = 0;
521 TRACE(dc, "(driver=%s, device=%s, output=%s): returning %04x\n",
522 debugstr_a(driver), debugstr_a(device), debugstr_a(output), dc->hSelf );
524 if (dc->funcs->pCreateDC &&
525 !dc->funcs->pCreateDC( dc, driver, device, output, initData ))
527 WARN(dc, "creation aborted by device\n" );
528 GDI_HEAP_FREE( dc->hSelf );
529 return 0;
532 DC_InitDC( dc );
533 GDI_HEAP_UNLOCK( dc->hSelf );
534 return dc->hSelf;
538 /***********************************************************************
539 * CreateDC32A (GDI32.)
541 HDC32 WINAPI CreateDC32A( LPCSTR driver, LPCSTR device, LPCSTR output,
542 const DEVMODE32A *initData )
544 return CreateDC16( driver, device, output, (const DEVMODE16 *)initData );
548 /***********************************************************************
549 * CreateDC32W (GDI32.)
551 HDC32 WINAPI CreateDC32W( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
552 const DEVMODE32W *initData )
554 LPSTR driverA = HEAP_strdupWtoA( GetProcessHeap(), 0, driver );
555 LPSTR deviceA = HEAP_strdupWtoA( GetProcessHeap(), 0, device );
556 LPSTR outputA = HEAP_strdupWtoA( GetProcessHeap(), 0, output );
557 HDC32 res = CreateDC16( driverA, deviceA, outputA,
558 (const DEVMODE16 *)initData /*FIXME*/ );
559 HeapFree( GetProcessHeap(), 0, driverA );
560 HeapFree( GetProcessHeap(), 0, deviceA );
561 HeapFree( GetProcessHeap(), 0, outputA );
562 return res;
566 /***********************************************************************
567 * CreateIC16 (GDI.153)
569 HDC16 WINAPI CreateIC16( LPCSTR driver, LPCSTR device, LPCSTR output,
570 const DEVMODE16* initData )
572 /* Nothing special yet for ICs */
573 return CreateDC16( driver, device, output, initData );
577 /***********************************************************************
578 * CreateIC32A (GDI32.49)
580 HDC32 WINAPI CreateIC32A( LPCSTR driver, LPCSTR device, LPCSTR output,
581 const DEVMODE32A* initData )
583 /* Nothing special yet for ICs */
584 return CreateDC32A( driver, device, output, initData );
588 /***********************************************************************
589 * CreateIC32W (GDI32.50)
591 HDC32 WINAPI CreateIC32W( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
592 const DEVMODE32W* initData )
594 /* Nothing special yet for ICs */
595 return CreateDC32W( driver, device, output, initData );
599 /***********************************************************************
600 * CreateCompatibleDC16 (GDI.52)
602 HDC16 WINAPI CreateCompatibleDC16( HDC16 hdc )
604 return (HDC16)CreateCompatibleDC32( hdc );
608 /***********************************************************************
609 * CreateCompatibleDC32 (GDI32.31)
611 HDC32 WINAPI CreateCompatibleDC32( HDC32 hdc )
613 DC *dc, *origDC;
614 HBITMAP32 hbitmap;
615 const DC_FUNCTIONS *funcs;
617 if ((origDC = (DC *)GDI_GetObjPtr( hdc, DC_MAGIC ))) funcs = origDC->funcs;
618 else funcs = DRIVER_FindDriver( "DISPLAY" );
619 if (!funcs) return 0;
621 if (!(dc = DC_AllocDC( funcs ))) return 0;
623 TRACE(dc, "(%04x): returning %04x\n",
624 hdc, dc->hSelf );
626 /* Create default bitmap */
627 if (!(hbitmap = CreateBitmap32( 1, 1, 1, 1, NULL )))
629 GDI_HEAP_FREE( dc->hSelf );
630 return 0;
632 dc->w.flags = DC_MEMORY;
633 dc->w.bitsPerPixel = 1;
634 dc->w.hBitmap = hbitmap;
635 dc->w.hFirstBitmap = hbitmap;
637 if (dc->funcs->pCreateDC &&
638 !dc->funcs->pCreateDC( dc, NULL, NULL, NULL, NULL ))
640 WARN(dc, "creation aborted by device\n");
641 DeleteObject32( hbitmap );
642 GDI_HEAP_FREE( dc->hSelf );
643 return 0;
646 DC_InitDC( dc );
647 GDI_HEAP_UNLOCK( dc->hSelf );
648 return dc->hSelf;
652 /***********************************************************************
653 * DeleteDC16 (GDI.68)
655 BOOL16 WINAPI DeleteDC16( HDC16 hdc )
657 return DeleteDC32( hdc );
661 /***********************************************************************
662 * DeleteDC32 (GDI32.67)
664 BOOL32 WINAPI DeleteDC32( HDC32 hdc )
666 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
667 if (!dc) return FALSE;
669 TRACE(dc, "%04x\n", hdc );
671 while (dc->saveLevel)
673 DC * dcs;
674 HDC16 hdcs = dc->header.hNext;
675 if (!(dcs = (DC *) GDI_GetObjPtr( hdcs, DC_MAGIC ))) break;
676 dc->header.hNext = dcs->header.hNext;
677 dc->saveLevel--;
678 DeleteDC32( hdcs );
681 if (!(dc->w.flags & DC_SAVED))
683 SelectObject32( hdc, STOCK_BLACK_PEN );
684 SelectObject32( hdc, STOCK_WHITE_BRUSH );
685 SelectObject32( hdc, STOCK_SYSTEM_FONT );
686 if (dc->w.flags & DC_MEMORY) DeleteObject32( dc->w.hFirstBitmap );
687 if (dc->funcs->pDeleteDC) dc->funcs->pDeleteDC(dc);
690 if (dc->w.hClipRgn) DeleteObject32( dc->w.hClipRgn );
691 if (dc->w.hVisRgn) DeleteObject32( dc->w.hVisRgn );
692 if (dc->w.hGCClipRgn) DeleteObject32( dc->w.hGCClipRgn );
694 PATH_DestroyGdiPath(&dc->w.path);
696 return GDI_FreeObject( hdc );
700 /***********************************************************************
701 * ResetDC16 (GDI.376)
703 HDC16 WINAPI ResetDC16( HDC16 hdc, const DEVMODE16 *devmode )
705 FIXME(dc, "stub\n" );
706 return hdc;
710 /***********************************************************************
711 * ResetDC32A (GDI32.287)
713 HDC32 WINAPI ResetDC32A( HDC32 hdc, const DEVMODE32A *devmode )
715 FIXME(dc, "stub\n" );
716 return hdc;
720 /***********************************************************************
721 * ResetDC32W (GDI32.288)
723 HDC32 WINAPI ResetDC32W( HDC32 hdc, const DEVMODE32W *devmode )
725 FIXME(dc, "stub\n" );
726 return hdc;
730 /***********************************************************************
731 * GetDeviceCaps16 (GDI.80)
733 INT16 WINAPI GetDeviceCaps16( HDC16 hdc, INT16 cap )
735 return GetDeviceCaps32( hdc, cap );
739 /***********************************************************************
740 * GetDeviceCaps32 (GDI32.171)
742 INT32 WINAPI GetDeviceCaps32( HDC32 hdc, INT32 cap )
744 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
745 INT32 ret;
747 if (!dc) return 0;
749 if ((cap < 0) || (cap > sizeof(DeviceCaps)-sizeof(WORD)))
751 GDI_HEAP_UNLOCK( hdc );
752 return 0;
755 TRACE(dc, "(%04x,%d): returning %d\n",
756 hdc, cap, *(WORD *)(((char *)dc->w.devCaps) + cap) );
757 ret = *(WORD *)(((char *)dc->w.devCaps) + cap);
758 GDI_HEAP_UNLOCK( hdc );
759 return ret;
763 /***********************************************************************
764 * SetBkColor16 (GDI.1)
766 COLORREF WINAPI SetBkColor16( HDC16 hdc, COLORREF color )
768 return SetBkColor32( hdc, color );
772 /***********************************************************************
773 * SetBkColor32 (GDI32.305)
775 COLORREF WINAPI SetBkColor32( HDC32 hdc, COLORREF color )
777 COLORREF oldColor;
778 DC * dc = DC_GetDCPtr( hdc );
780 if (!dc) return 0x80000000;
781 if (dc->funcs->pSetBkColor)
782 oldColor = dc->funcs->pSetBkColor(dc, color);
783 else {
784 oldColor = dc->w.backgroundColor;
785 dc->w.backgroundColor = color;
787 GDI_HEAP_UNLOCK( hdc );
788 return oldColor;
792 /***********************************************************************
793 * SetTextColor16 (GDI.9)
795 COLORREF WINAPI SetTextColor16( HDC16 hdc, COLORREF color )
797 return SetTextColor32( hdc, color );
801 /***********************************************************************
802 * SetTextColor32 (GDI32.338)
804 COLORREF WINAPI SetTextColor32( HDC32 hdc, COLORREF color )
806 COLORREF oldColor;
807 DC * dc = DC_GetDCPtr( hdc );
809 if (!dc) return 0x80000000;
810 if (dc->funcs->pSetTextColor)
811 oldColor = dc->funcs->pSetTextColor(dc, color);
812 else {
813 oldColor = dc->w.textColor;
814 dc->w.textColor = color;
816 GDI_HEAP_UNLOCK( hdc );
817 return oldColor;
821 /***********************************************************************
822 * SetTextAlign16 (GDI.346)
824 UINT16 WINAPI SetTextAlign16( HDC16 hdc, UINT16 textAlign )
826 return SetTextAlign32( hdc, textAlign );
830 /***********************************************************************
831 * SetTextAlign32 (GDI32.336)
833 UINT32 WINAPI SetTextAlign32( HDC32 hdc, UINT32 textAlign )
835 UINT32 prevAlign;
836 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
837 if (!dc)
839 if (!(dc = (DC *)GDI_GetObjPtr( hdc, METAFILE_DC_MAGIC ))) return 0;
840 MF_MetaParam1( dc, META_SETTEXTALIGN, textAlign );
841 GDI_HEAP_UNLOCK( hdc );
842 return 1;
844 prevAlign = dc->w.textAlign;
845 dc->w.textAlign = textAlign;
846 GDI_HEAP_UNLOCK( hdc );
847 return prevAlign;
851 /***********************************************************************
852 * GetDCOrgEx (GDI32.168)
854 BOOL32 WINAPI GetDCOrgEx( HDC32 hDC, LPPOINT32 lpp )
856 DC * dc;
857 X11DRV_PDEVICE *physDev;
859 if (!lpp) return FALSE;
860 if (!(dc = (DC *) GDI_GetObjPtr( hDC, DC_MAGIC ))) return FALSE;
861 physDev = (X11DRV_PDEVICE *)dc->physDev;
863 if (!(dc->w.flags & DC_MEMORY))
865 Window root;
866 int w, h, border, depth;
867 /* FIXME: this is not correct for managed windows */
868 TSXGetGeometry( display, physDev->drawable, &root,
869 &lpp->x, &lpp->y, &w, &h, &border, &depth );
871 else lpp->x = lpp->y = 0;
872 lpp->x += dc->w.DCOrgX; lpp->y += dc->w.DCOrgY;
873 GDI_HEAP_UNLOCK( hDC );
874 return TRUE;
878 /***********************************************************************
879 * GetDCOrg (GDI.79)
881 DWORD WINAPI GetDCOrg( HDC16 hdc )
883 POINT32 pt;
884 if( GetDCOrgEx( hdc, &pt) )
885 return MAKELONG( (WORD)pt.x, (WORD)pt.y );
886 return 0;
890 /***********************************************************************
891 * SetDCOrg (GDI.117)
893 DWORD WINAPI SetDCOrg( HDC16 hdc, INT16 x, INT16 y )
895 DWORD prevOrg;
896 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
897 if (!dc) return 0;
898 prevOrg = dc->w.DCOrgX | (dc->w.DCOrgY << 16);
899 dc->w.DCOrgX = x;
900 dc->w.DCOrgY = y;
901 GDI_HEAP_UNLOCK( hdc );
902 return prevOrg;
906 /***********************************************************************
907 * GetGraphicsMode (GDI32.188)
909 INT32 WINAPI GetGraphicsMode( HDC32 hdc )
911 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
912 if (!dc) return 0;
913 return dc->w.GraphicsMode;
917 /***********************************************************************
918 * SetGraphicsMode (GDI32.317)
920 INT32 WINAPI SetGraphicsMode( HDC32 hdc, INT32 mode )
922 INT32 ret;
923 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
925 /* One would think that setting the graphics mode to GM_COMPATIBLE
926 * would also reset the world transformation matrix to the unity
927 * matrix. However, in Windows, this is not the case. This doesn't
928 * make a lot of sense to me, but that's the way it is.
931 if (!dc) return 0;
932 if ((mode <= 0) || (mode > GM_LAST)) return 0;
933 ret = dc->w.GraphicsMode;
934 dc->w.GraphicsMode = mode;
935 return ret;
939 /***********************************************************************
940 * GetArcDirection16 (GDI.524)
942 INT16 WINAPI GetArcDirection16( HDC16 hdc )
944 return GetArcDirection32( (HDC32)hdc );
948 /***********************************************************************
949 * GetArcDirection32 (GDI32.141)
951 INT32 WINAPI GetArcDirection32( HDC32 hdc )
953 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
955 if (!dc)
956 return 0;
958 return dc->w.ArcDirection;
962 /***********************************************************************
963 * SetArcDirection16 (GDI.525)
965 INT16 WINAPI SetArcDirection16( HDC16 hdc, INT16 nDirection )
967 return SetArcDirection32( (HDC32)hdc, (INT32)nDirection );
971 /***********************************************************************
972 * SetArcDirection32 (GDI32.302)
974 INT32 WINAPI SetArcDirection32( HDC32 hdc, INT32 nDirection )
976 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
977 INT32 nOldDirection;
979 if (!dc)
980 return 0;
982 if (nDirection!=AD_COUNTERCLOCKWISE && nDirection!=AD_CLOCKWISE)
984 SetLastError(ERROR_INVALID_PARAMETER);
985 return 0;
988 nOldDirection = dc->w.ArcDirection;
989 dc->w.ArcDirection = nDirection;
991 return nOldDirection;
995 /***********************************************************************
996 * GetWorldTransform (GDI32.244)
998 BOOL32 WINAPI GetWorldTransform( HDC32 hdc, LPXFORM xform )
1000 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1002 if (!dc)
1003 return FALSE;
1004 if (!xform)
1005 return FALSE;
1007 *xform = dc->w.xformWorld2Wnd;
1009 return TRUE;
1013 /***********************************************************************
1014 * SetWorldTransform (GDI32.346)
1016 BOOL32 WINAPI SetWorldTransform( HDC32 hdc, const XFORM *xform )
1018 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1020 if (!dc)
1022 SetLastError( ERROR_INVALID_HANDLE );
1023 return FALSE;
1026 if (!xform)
1027 return FALSE;
1029 /* Check that graphics mode is GM_ADVANCED */
1030 if (dc->w.GraphicsMode!=GM_ADVANCED)
1031 return FALSE;
1033 dc->w.xformWorld2Wnd = *xform;
1035 DC_UpdateXforms( dc );
1037 return TRUE;
1041 /****************************************************************************
1042 * ModifyWorldTransform [GDI32.253]
1043 * Modifies the world transformation for a device context.
1045 * PARAMS
1046 * hdc [I] Handle to device context
1047 * xform [I] XFORM structure that will be used to modify the world
1048 * transformation
1049 * iMode [I] Specifies in what way to modify the world transformation
1050 * Possible values:
1051 * MWT_IDENTITY
1052 * Resets the world transformation to the identity matrix.
1053 * The parameter xform is ignored.
1054 * MWT_LEFTMULTIPLY
1055 * Multiplies xform into the world transformation matrix from
1056 * the left.
1057 * MWT_RIGHTMULTIPLY
1058 * Multiplies xform into the world transformation matrix from
1059 * the right.
1061 * RETURNS STD
1063 BOOL32 WINAPI ModifyWorldTransform( HDC32 hdc, const XFORM *xform,
1064 DWORD iMode )
1066 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1068 /* Check for illegal parameters */
1069 if (!dc)
1071 SetLastError( ERROR_INVALID_HANDLE );
1072 return FALSE;
1074 if (!xform)
1075 return FALSE;
1077 /* Check that graphics mode is GM_ADVANCED */
1078 if (dc->w.GraphicsMode!=GM_ADVANCED)
1079 return FALSE;
1081 switch (iMode)
1083 case MWT_IDENTITY:
1084 dc->w.xformWorld2Wnd.eM11 = 1.0f;
1085 dc->w.xformWorld2Wnd.eM12 = 0.0f;
1086 dc->w.xformWorld2Wnd.eM21 = 0.0f;
1087 dc->w.xformWorld2Wnd.eM22 = 1.0f;
1088 dc->w.xformWorld2Wnd.eDx = 0.0f;
1089 dc->w.xformWorld2Wnd.eDy = 0.0f;
1090 break;
1091 case MWT_LEFTMULTIPLY:
1092 CombineTransform( &dc->w.xformWorld2Wnd, xform,
1093 &dc->w.xformWorld2Wnd );
1094 break;
1095 case MWT_RIGHTMULTIPLY:
1096 CombineTransform( &dc->w.xformWorld2Wnd, &dc->w.xformWorld2Wnd,
1097 xform );
1098 break;
1099 default:
1100 return FALSE;
1103 DC_UpdateXforms( dc );
1105 return TRUE;
1109 /****************************************************************************
1110 * CombineTransform [GDI32.20]
1111 * Combines two transformation matrices.
1113 * PARAMS
1114 * xformResult [O] Stores the result of combining the two matrices
1115 * xform1 [I] Specifies the first matrix to apply
1116 * xform2 [I] Specifies the second matrix to apply
1118 * REMARKS
1119 * The same matrix can be passed in for more than one of the parameters.
1121 * RETURNS STD
1123 BOOL32 WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
1124 const XFORM *xform2 )
1126 XFORM xformTemp;
1128 /* Check for illegal parameters */
1129 if (!xformResult || !xform1 || !xform2)
1130 return FALSE;
1132 /* Create the result in a temporary XFORM, since xformResult may be
1133 * equal to xform1 or xform2 */
1134 xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
1135 xform1->eM12 * xform2->eM21;
1136 xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
1137 xform1->eM12 * xform2->eM22;
1138 xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
1139 xform1->eM22 * xform2->eM21;
1140 xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
1141 xform1->eM22 * xform2->eM22;
1142 xformTemp.eDx = xform1->eDx * xform2->eM11 +
1143 xform1->eDy * xform2->eM21 +
1144 xform2->eDx;
1145 xformTemp.eDy = xform1->eDx * xform2->eM12 +
1146 xform1->eDy * xform2->eM22 +
1147 xform2->eDy;
1149 /* Copy the result to xformResult */
1150 *xformResult = xformTemp;
1152 return TRUE;
1156 /***********************************************************************
1157 * SetDCHook (GDI.190)
1159 BOOL16 WINAPI SetDCHook( HDC16 hdc, FARPROC16 hookProc, DWORD dwHookData )
1161 DC *dc = (DC *)GDI_GetObjPtr( hdc, DC_MAGIC );
1163 TRACE(dc, "hookProc %08x, default is %08x\n",
1164 (UINT32)hookProc, (UINT32)DCHook );
1166 if (!dc) return FALSE;
1167 dc->hookProc = hookProc;
1168 dc->dwHookData = dwHookData;
1169 GDI_HEAP_UNLOCK( hdc );
1170 return TRUE;
1174 /***********************************************************************
1175 * GetDCHook (GDI.191)
1177 DWORD WINAPI GetDCHook( HDC16 hdc, FARPROC16 *phookProc )
1179 DC *dc = (DC *)GDI_GetObjPtr( hdc, DC_MAGIC );
1180 if (!dc) return 0;
1181 *phookProc = dc->hookProc;
1182 GDI_HEAP_UNLOCK( hdc );
1183 return dc->dwHookData;
1187 /***********************************************************************
1188 * SetHookFlags (GDI.192)
1190 WORD WINAPI SetHookFlags(HDC16 hDC, WORD flags)
1192 DC* dc = (DC*)GDI_GetObjPtr( hDC, DC_MAGIC );
1194 if( dc )
1196 WORD wRet = dc->w.flags & DC_DIRTY;
1198 /* "Undocumented Windows" info is slightly confusing.
1201 TRACE(dc,"hDC %04x, flags %04x\n",hDC,flags);
1203 if( flags & DCHF_INVALIDATEVISRGN )
1204 dc->w.flags |= DC_DIRTY;
1205 else if( flags & DCHF_VALIDATEVISRGN || !flags )
1206 dc->w.flags &= ~DC_DIRTY;
1207 GDI_HEAP_UNLOCK( hDC );
1208 return wRet;
1210 return 0;
1213 /***********************************************************************
1214 * SetICMMode (GDI32.318)
1216 INT32 WINAPI SetICMMode(HDC32 hdc, INT32 iEnableICM)
1218 /*FIXME Asuming that ICM is always off, and cannot be turned on */
1219 if (iEnableICM == ICM_OFF) return ICM_OFF;
1220 if (iEnableICM == ICM_ON) return 0;
1221 if (iEnableICM == ICM_QUERY) return ICM_OFF;
1222 return 0;
1226 /***********************************************************************
1227 * GetColorSpace (GDI32.165)
1229 HCOLORSPACE32 WINAPI GetColorSpace(HDC32 hdc)
1231 /*FIXME Need to to whatever GetColorSpace actually does */
1232 return 0;
1235 /***********************************************************************
1236 * GetBoundsRect16 (GDI.194)
1238 UINT16 WINAPI GetBoundsRect16(HDC16 hdc, LPRECT16 rect, UINT16 flags)
1240 return DCB_RESET | DCB_DISABLE; /* bounding rectangle always empty and disabled*/
1243 /***********************************************************************
1244 * GetBoundsRect32 (GDI32.147)
1246 UINT32 WINAPI GetBoundsRect32(HDC32 hdc, LPRECT32 rect, UINT32 flags)
1248 FIXME(dc, "(): stub\n");
1249 return DCB_RESET; /* bounding rectangle always empty */
1252 /***********************************************************************
1253 * SetBoundsRect16 (GDI.193)
1255 UINT16 WINAPI SetBoundsRect16(HDC16 hdc, const RECT16* rect, UINT16 flags)
1257 if ( (flags & DCB_ACCUMULATE) || (flags & DCB_ENABLE) )
1258 FIXME( dc, "(%04x, %p, %04x): stub\n", hdc, rect, flags );
1260 return DCB_RESET | DCB_DISABLE; /* bounding rectangle always empty and disabled*/
1263 /***********************************************************************
1264 * SetBoundsRect32 (GDI32.307)
1266 UINT32 WINAPI SetBoundsRect32(HDC32 hdc, const RECT32* rect, UINT32 flags)
1268 FIXME(dc, "(): stub\n");
1269 return DCB_DISABLE; /* bounding rectangle always empty */