Initialize the output buffer parameters to 0 in RegQueryValue*
[wine.git] / objects / dc.c
blob0a4b071cb3ee8fab9b3a1c950c34cd6d02479e31
1 /*
2 * GDI Device Context functions
4 * Copyright 1993 Alexandre Julliard
6 */
8 #ifndef X_DISPLAY_MISSING
9 #include "ts_xlib.h"
10 #include "x11drv.h"
11 #endif /* !defined(X_DISPLAY_MISSING) */
13 #include <stdlib.h>
14 #include <string.h>
15 #include "dc.h"
16 #include "gdi.h"
17 #include "heap.h"
18 #include "metafiledrv.h"
19 #include "debug.h"
20 #include "font.h"
21 #include "winerror.h"
22 #include "wine/winuser16.h"
24 DEFAULT_DEBUG_CHANNEL(dc)
26 /***********************************************************************
27 * DC_Init_DC_INFO
29 * Fill the WIN_DC_INFO structure.
31 static void DC_Init_DC_INFO( WIN_DC_INFO *win_dc_info )
33 win_dc_info->flags = 0;
34 win_dc_info->devCaps = NULL;
35 win_dc_info->hClipRgn = 0;
36 win_dc_info->hVisRgn = 0;
37 win_dc_info->hGCClipRgn = 0;
38 win_dc_info->hPen = STOCK_BLACK_PEN;
39 win_dc_info->hBrush = STOCK_WHITE_BRUSH;
40 win_dc_info->hFont = STOCK_SYSTEM_FONT;
41 win_dc_info->hBitmap = 0;
42 win_dc_info->hFirstBitmap = 0;
43 win_dc_info->hDevice = 0;
44 win_dc_info->hPalette = STOCK_DEFAULT_PALETTE;
45 win_dc_info->ROPmode = R2_COPYPEN;
46 win_dc_info->polyFillMode = ALTERNATE;
47 win_dc_info->stretchBltMode = BLACKONWHITE;
48 win_dc_info->relAbsMode = ABSOLUTE;
49 win_dc_info->backgroundMode = OPAQUE;
50 win_dc_info->backgroundColor = RGB( 255, 255, 255 );
51 win_dc_info->textColor = RGB( 0, 0, 0 );
52 win_dc_info->brushOrgX = 0;
53 win_dc_info->brushOrgY = 0;
54 win_dc_info->textAlign = TA_LEFT | TA_TOP | TA_NOUPDATECP;
55 win_dc_info->charExtra = 0;
56 win_dc_info->breakTotalExtra = 0;
57 win_dc_info->breakCount = 0;
58 win_dc_info->breakExtra = 0;
59 win_dc_info->breakRem = 0;
60 win_dc_info->bitsPerPixel = 1;
61 win_dc_info->MapMode = MM_TEXT;
62 win_dc_info->GraphicsMode = GM_COMPATIBLE;
63 win_dc_info->DCOrgX = 0;
64 win_dc_info->DCOrgY = 0;
65 win_dc_info->lpfnPrint = NULL;
66 win_dc_info->CursPosX = 0;
67 win_dc_info->CursPosY = 0;
68 win_dc_info->ArcDirection = AD_COUNTERCLOCKWISE;
69 win_dc_info->xformWorld2Wnd.eM11 = 1.0f;
70 win_dc_info->xformWorld2Wnd.eM12 = 0.0f;
71 win_dc_info->xformWorld2Wnd.eM21 = 0.0f;
72 win_dc_info->xformWorld2Wnd.eM22 = 1.0f;
73 win_dc_info->xformWorld2Wnd.eDx = 0.0f;
74 win_dc_info->xformWorld2Wnd.eDy = 0.0f;
75 win_dc_info->xformWorld2Vport = win_dc_info->xformWorld2Wnd;
76 win_dc_info->xformVport2World = win_dc_info->xformWorld2Wnd;
77 win_dc_info->vport2WorldValid = TRUE;
79 PATH_InitGdiPath(&win_dc_info->path);
83 /***********************************************************************
84 * DC_AllocDC
86 DC *DC_AllocDC( const DC_FUNCTIONS *funcs )
88 HDC16 hdc;
89 DC *dc;
91 if (!(hdc = GDI_AllocObject( sizeof(DC), DC_MAGIC ))) return NULL;
92 dc = (DC *) GDI_HEAP_LOCK( hdc );
94 dc->hSelf = hdc;
95 dc->funcs = funcs;
96 dc->physDev = NULL;
97 dc->saveLevel = 0;
98 dc->dwHookData = 0L;
99 dc->hookProc = NULL;
100 dc->wndOrgX = 0;
101 dc->wndOrgY = 0;
102 dc->wndExtX = 1;
103 dc->wndExtY = 1;
104 dc->vportOrgX = 0;
105 dc->vportOrgY = 0;
106 dc->vportExtX = 1;
107 dc->vportExtY = 1;
109 DC_Init_DC_INFO( &dc->w );
111 return dc;
116 /***********************************************************************
117 * DC_GetDCPtr
119 DC *DC_GetDCPtr( HDC hdc )
121 GDIOBJHDR *ptr = (GDIOBJHDR *)GDI_HEAP_LOCK( hdc );
122 if (!ptr) return NULL;
123 if ((ptr->wMagic == DC_MAGIC) || (ptr->wMagic == METAFILE_DC_MAGIC))
124 return (DC *)ptr;
125 GDI_HEAP_UNLOCK( hdc );
126 return NULL;
130 /***********************************************************************
131 * DC_InitDC
133 * Setup device-specific DC values for a newly created DC.
135 void DC_InitDC( DC* dc )
137 RealizeDefaultPalette16( dc->hSelf );
138 SetTextColor( dc->hSelf, dc->w.textColor );
139 SetBkColor( dc->hSelf, dc->w.backgroundColor );
140 SelectObject( dc->hSelf, dc->w.hPen );
141 SelectObject( dc->hSelf, dc->w.hBrush );
142 SelectObject( dc->hSelf, dc->w.hFont );
143 CLIPPING_UpdateGCRegion( dc );
147 /***********************************************************************
148 * DC_InvertXform
150 * Computes the inverse of the transformation xformSrc and stores it to
151 * xformDest. Returns TRUE if successful or FALSE if the xformSrc matrix
152 * is singular.
154 static BOOL DC_InvertXform( const XFORM *xformSrc, XFORM *xformDest )
156 FLOAT determinant;
158 determinant = xformSrc->eM11*xformSrc->eM22 -
159 xformSrc->eM12*xformSrc->eM21;
160 if (determinant > -1e-12 && determinant < 1e-12)
161 return FALSE;
163 xformDest->eM11 = xformSrc->eM22 / determinant;
164 xformDest->eM12 = -xformSrc->eM12 / determinant;
165 xformDest->eM21 = -xformSrc->eM21 / determinant;
166 xformDest->eM22 = xformSrc->eM11 / determinant;
167 xformDest->eDx = -xformSrc->eDx * xformDest->eM11 -
168 xformSrc->eDy * xformDest->eM21;
169 xformDest->eDy = -xformSrc->eDx * xformDest->eM12 -
170 xformSrc->eDy * xformDest->eM22;
172 return TRUE;
176 /***********************************************************************
177 * DC_UpdateXforms
179 * Updates the xformWorld2Vport, xformVport2World and vport2WorldValid
180 * fields of the specified DC by creating a transformation that
181 * represents the current mapping mode and combining it with the DC's
182 * world transform. This function should be called whenever the
183 * parameters associated with the mapping mode (window and viewport
184 * extents and origins) or the world transform change.
186 void DC_UpdateXforms( DC *dc )
188 XFORM xformWnd2Vport;
189 FLOAT scaleX, scaleY;
191 /* Construct a transformation to do the window-to-viewport conversion */
192 scaleX = (FLOAT)dc->vportExtX / (FLOAT)dc->wndExtX;
193 scaleY = (FLOAT)dc->vportExtY / (FLOAT)dc->wndExtY;
194 xformWnd2Vport.eM11 = scaleX;
195 xformWnd2Vport.eM12 = 0.0;
196 xformWnd2Vport.eM21 = 0.0;
197 xformWnd2Vport.eM22 = scaleY;
198 xformWnd2Vport.eDx = (FLOAT)dc->vportOrgX -
199 scaleX * (FLOAT)dc->wndOrgX;
200 xformWnd2Vport.eDy = (FLOAT)dc->vportOrgY -
201 scaleY * (FLOAT)dc->wndOrgY;
203 /* Combine with the world transformation */
204 CombineTransform( &dc->w.xformWorld2Vport, &dc->w.xformWorld2Wnd,
205 &xformWnd2Vport );
207 /* Create inverse of world-to-viewport transformation */
208 dc->w.vport2WorldValid = DC_InvertXform( &dc->w.xformWorld2Vport,
209 &dc->w.xformVport2World );
213 /***********************************************************************
214 * GetDCState (GDI.179)
216 HDC16 WINAPI GetDCState16( HDC16 hdc )
218 DC * newdc, * dc;
219 HGDIOBJ16 handle;
221 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
222 if (!(handle = GDI_AllocObject( sizeof(DC), DC_MAGIC )))
224 GDI_HEAP_UNLOCK( hdc );
225 return 0;
227 newdc = (DC *) GDI_HEAP_LOCK( handle );
229 TRACE(dc, "(%04x): returning %04x\n", hdc, handle );
231 newdc->w.flags = dc->w.flags | DC_SAVED;
232 newdc->w.devCaps = dc->w.devCaps;
233 newdc->w.hPen = dc->w.hPen;
234 newdc->w.hBrush = dc->w.hBrush;
235 newdc->w.hFont = dc->w.hFont;
236 newdc->w.hBitmap = dc->w.hBitmap;
237 newdc->w.hFirstBitmap = dc->w.hFirstBitmap;
238 newdc->w.hDevice = dc->w.hDevice;
239 newdc->w.hPalette = dc->w.hPalette;
240 newdc->w.totalExtent = dc->w.totalExtent;
241 newdc->w.bitsPerPixel = dc->w.bitsPerPixel;
242 newdc->w.ROPmode = dc->w.ROPmode;
243 newdc->w.polyFillMode = dc->w.polyFillMode;
244 newdc->w.stretchBltMode = dc->w.stretchBltMode;
245 newdc->w.relAbsMode = dc->w.relAbsMode;
246 newdc->w.backgroundMode = dc->w.backgroundMode;
247 newdc->w.backgroundColor = dc->w.backgroundColor;
248 newdc->w.textColor = dc->w.textColor;
249 newdc->w.brushOrgX = dc->w.brushOrgX;
250 newdc->w.brushOrgY = dc->w.brushOrgY;
251 newdc->w.textAlign = dc->w.textAlign;
252 newdc->w.charExtra = dc->w.charExtra;
253 newdc->w.breakTotalExtra = dc->w.breakTotalExtra;
254 newdc->w.breakCount = dc->w.breakCount;
255 newdc->w.breakExtra = dc->w.breakExtra;
256 newdc->w.breakRem = dc->w.breakRem;
257 newdc->w.MapMode = dc->w.MapMode;
258 newdc->w.GraphicsMode = dc->w.GraphicsMode;
259 #if 0
260 /* Apparently, the DC origin is not changed by [GS]etDCState */
261 newdc->w.DCOrgX = dc->w.DCOrgX;
262 newdc->w.DCOrgY = dc->w.DCOrgY;
263 #endif
264 newdc->w.CursPosX = dc->w.CursPosX;
265 newdc->w.CursPosY = dc->w.CursPosY;
266 newdc->w.ArcDirection = dc->w.ArcDirection;
267 newdc->w.xformWorld2Wnd = dc->w.xformWorld2Wnd;
268 newdc->w.xformWorld2Vport = dc->w.xformWorld2Vport;
269 newdc->w.xformVport2World = dc->w.xformVport2World;
270 newdc->w.vport2WorldValid = dc->w.vport2WorldValid;
271 newdc->wndOrgX = dc->wndOrgX;
272 newdc->wndOrgY = dc->wndOrgY;
273 newdc->wndExtX = dc->wndExtX;
274 newdc->wndExtY = dc->wndExtY;
275 newdc->vportOrgX = dc->vportOrgX;
276 newdc->vportOrgY = dc->vportOrgY;
277 newdc->vportExtX = dc->vportExtX;
278 newdc->vportExtY = dc->vportExtY;
280 newdc->hSelf = (HDC)handle;
281 newdc->saveLevel = 0;
283 PATH_InitGdiPath( &newdc->w.path );
285 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
287 newdc->w.hGCClipRgn = newdc->w.hVisRgn = 0;
288 if (dc->w.hClipRgn)
290 newdc->w.hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
291 CombineRgn( newdc->w.hClipRgn, dc->w.hClipRgn, 0, RGN_COPY );
293 else
294 newdc->w.hClipRgn = 0;
295 GDI_HEAP_UNLOCK( handle );
296 GDI_HEAP_UNLOCK( hdc );
297 return handle;
301 /***********************************************************************
302 * SetDCState (GDI.180)
304 void WINAPI SetDCState16( HDC16 hdc, HDC16 hdcs )
306 DC *dc, *dcs;
308 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return;
309 if (!(dcs = (DC *) GDI_GetObjPtr( hdcs, DC_MAGIC )))
311 GDI_HEAP_UNLOCK( hdc );
312 return;
314 if (!dcs->w.flags & DC_SAVED)
316 GDI_HEAP_UNLOCK( hdc );
317 GDI_HEAP_UNLOCK( hdcs );
318 return;
320 TRACE(dc, "%04x %04x\n", hdc, hdcs );
322 dc->w.flags = dcs->w.flags & ~DC_SAVED;
323 dc->w.devCaps = dcs->w.devCaps;
324 dc->w.hFirstBitmap = dcs->w.hFirstBitmap;
325 dc->w.hDevice = dcs->w.hDevice;
326 dc->w.totalExtent = dcs->w.totalExtent;
327 dc->w.ROPmode = dcs->w.ROPmode;
328 dc->w.polyFillMode = dcs->w.polyFillMode;
329 dc->w.stretchBltMode = dcs->w.stretchBltMode;
330 dc->w.relAbsMode = dcs->w.relAbsMode;
331 dc->w.backgroundMode = dcs->w.backgroundMode;
332 dc->w.backgroundColor = dcs->w.backgroundColor;
333 dc->w.textColor = dcs->w.textColor;
334 dc->w.brushOrgX = dcs->w.brushOrgX;
335 dc->w.brushOrgY = dcs->w.brushOrgY;
336 dc->w.textAlign = dcs->w.textAlign;
337 dc->w.charExtra = dcs->w.charExtra;
338 dc->w.breakTotalExtra = dcs->w.breakTotalExtra;
339 dc->w.breakCount = dcs->w.breakCount;
340 dc->w.breakExtra = dcs->w.breakExtra;
341 dc->w.breakRem = dcs->w.breakRem;
342 dc->w.MapMode = dcs->w.MapMode;
343 dc->w.GraphicsMode = dcs->w.GraphicsMode;
344 #if 0
345 /* Apparently, the DC origin is not changed by [GS]etDCState */
346 dc->w.DCOrgX = dcs->w.DCOrgX;
347 dc->w.DCOrgY = dcs->w.DCOrgY;
348 #endif
349 dc->w.CursPosX = dcs->w.CursPosX;
350 dc->w.CursPosY = dcs->w.CursPosY;
351 dc->w.ArcDirection = dcs->w.ArcDirection;
352 dc->w.xformWorld2Wnd = dcs->w.xformWorld2Wnd;
353 dc->w.xformWorld2Vport = dcs->w.xformWorld2Vport;
354 dc->w.xformVport2World = dcs->w.xformVport2World;
355 dc->w.vport2WorldValid = dcs->w.vport2WorldValid;
357 dc->wndOrgX = dcs->wndOrgX;
358 dc->wndOrgY = dcs->wndOrgY;
359 dc->wndExtX = dcs->wndExtX;
360 dc->wndExtY = dcs->wndExtY;
361 dc->vportOrgX = dcs->vportOrgX;
362 dc->vportOrgY = dcs->vportOrgY;
363 dc->vportExtX = dcs->vportExtX;
364 dc->vportExtY = dcs->vportExtY;
366 if (!(dc->w.flags & DC_MEMORY)) dc->w.bitsPerPixel = dcs->w.bitsPerPixel;
368 if (dcs->w.hClipRgn)
370 if (!dc->w.hClipRgn) dc->w.hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
371 CombineRgn( dc->w.hClipRgn, dcs->w.hClipRgn, 0, RGN_COPY );
373 else
375 if (dc->w.hClipRgn) DeleteObject16( dc->w.hClipRgn );
376 dc->w.hClipRgn = 0;
378 CLIPPING_UpdateGCRegion( dc );
380 SelectObject( hdc, dcs->w.hBitmap );
381 SelectObject( hdc, dcs->w.hBrush );
382 SelectObject( hdc, dcs->w.hFont );
383 SelectObject( hdc, dcs->w.hPen );
384 SetBkColor( hdc, dcs->w.backgroundColor);
385 SetTextColor( hdc, dcs->w.textColor);
386 GDISelectPalette16( hdc, dcs->w.hPalette, FALSE );
387 GDI_HEAP_UNLOCK( hdc );
388 GDI_HEAP_UNLOCK( hdcs );
392 /***********************************************************************
393 * SaveDC16 (GDI.30)
395 INT16 WINAPI SaveDC16( HDC16 hdc )
397 return (INT16)SaveDC( hdc );
401 /***********************************************************************
402 * SaveDC32 (GDI32.292)
404 INT WINAPI SaveDC( HDC hdc )
406 HDC hdcs;
407 DC * dc, * dcs;
408 INT ret;
410 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
411 if (!dc)
413 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
414 if (!dc) return 0;
415 MFDRV_MetaParam0(dc, META_SAVEDC);
416 GDI_HEAP_UNLOCK( hdc );
417 return 1; /* ?? */
419 if (!(hdcs = GetDCState16( hdc )))
421 GDI_HEAP_UNLOCK( hdc );
422 return 0;
424 dcs = (DC *) GDI_HEAP_LOCK( hdcs );
426 /* Copy path. The reason why path saving / restoring is in SaveDC/
427 * RestoreDC and not in GetDCState/SetDCState is that the ...DCState
428 * functions are only in Win16 (which doesn't have paths) and that
429 * SetDCState doesn't allow us to signal an error (which can happen
430 * when copying paths).
432 if (!PATH_AssignGdiPath( &dcs->w.path, &dc->w.path ))
434 GDI_HEAP_UNLOCK( hdc );
435 GDI_HEAP_UNLOCK( hdcs );
436 DeleteDC( hdcs );
437 return 0;
440 dcs->header.hNext = dc->header.hNext;
441 dc->header.hNext = hdcs;
442 TRACE(dc, "(%04x): returning %d\n", hdc, dc->saveLevel+1 );
443 ret = ++dc->saveLevel;
444 GDI_HEAP_UNLOCK( hdcs );
445 GDI_HEAP_UNLOCK( hdc );
446 return ret;
450 /***********************************************************************
451 * RestoreDC16 (GDI.39)
453 BOOL16 WINAPI RestoreDC16( HDC16 hdc, INT16 level )
455 return RestoreDC( hdc, level );
459 /***********************************************************************
460 * RestoreDC32 (GDI32.290)
462 BOOL WINAPI RestoreDC( HDC hdc, INT level )
464 DC * dc, * dcs;
465 BOOL success;
467 TRACE(dc, "%04x %d\n", hdc, level );
468 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
469 if (!dc)
471 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
472 if (!dc) return FALSE;
473 if (level != -1)
475 GDI_HEAP_UNLOCK( hdc );
476 return FALSE;
478 MFDRV_MetaParam1(dc, META_RESTOREDC, level);
479 GDI_HEAP_UNLOCK( hdc );
480 return TRUE;
482 if (level == -1) level = dc->saveLevel;
483 if ((level < 1) || (level > dc->saveLevel))
485 GDI_HEAP_UNLOCK( hdc );
486 return FALSE;
489 success=TRUE;
490 while (dc->saveLevel >= level)
492 HDC16 hdcs = dc->header.hNext;
493 if (!(dcs = (DC *) GDI_GetObjPtr( hdcs, DC_MAGIC )))
495 GDI_HEAP_UNLOCK( hdc );
496 return FALSE;
498 dc->header.hNext = dcs->header.hNext;
499 if (--dc->saveLevel < level)
501 SetDCState16( hdc, hdcs );
502 if (!PATH_AssignGdiPath( &dc->w.path, &dcs->w.path ))
503 /* FIXME: This might not be quite right, since we're
504 * returning FALSE but still destroying the saved DC state */
505 success=FALSE;
507 DeleteDC( hdcs );
509 GDI_HEAP_UNLOCK( hdc );
510 return success;
514 /***********************************************************************
515 * CreateDC16 (GDI.53)
517 HDC16 WINAPI CreateDC16( LPCSTR driver, LPCSTR device, LPCSTR output,
518 const DEVMODE16 *initData )
520 DC * dc;
521 const DC_FUNCTIONS *funcs;
523 if (!(funcs = DRIVER_FindDriver( driver ))) return 0;
524 if (!(dc = DC_AllocDC( funcs ))) return 0;
525 dc->w.flags = 0;
527 TRACE(dc, "(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, driver, device, output, initData ))
533 WARN(dc, "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 DEVMODE16 *)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 DEVMODE16 *)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 DEVMODE16* 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(dc, "(%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 if (dc->funcs->pCreateDC &&
644 !dc->funcs->pCreateDC( dc, NULL, NULL, NULL, NULL ))
646 WARN(dc, "creation aborted by device\n");
647 DeleteObject( hbitmap );
648 GDI_HEAP_FREE( dc->hSelf );
649 return 0;
652 DC_InitDC( dc );
653 GDI_HEAP_UNLOCK( dc->hSelf );
654 return dc->hSelf;
658 /***********************************************************************
659 * DeleteDC16 (GDI.68)
661 BOOL16 WINAPI DeleteDC16( HDC16 hdc )
663 return DeleteDC( hdc );
667 /***********************************************************************
668 * DeleteDC32 (GDI32.67)
670 BOOL WINAPI DeleteDC( HDC hdc )
672 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
673 if (!dc) return FALSE;
675 TRACE(dc, "%04x\n", hdc );
677 while (dc->saveLevel)
679 DC * dcs;
680 HDC16 hdcs = dc->header.hNext;
681 if (!(dcs = (DC *) GDI_GetObjPtr( hdcs, DC_MAGIC ))) break;
682 dc->header.hNext = dcs->header.hNext;
683 dc->saveLevel--;
684 DeleteDC( hdcs );
687 if (!(dc->w.flags & DC_SAVED))
689 SelectObject( hdc, STOCK_BLACK_PEN );
690 SelectObject( hdc, STOCK_WHITE_BRUSH );
691 SelectObject( hdc, STOCK_SYSTEM_FONT );
692 if (dc->w.flags & DC_MEMORY) DeleteObject( dc->w.hFirstBitmap );
693 if (dc->funcs->pDeleteDC) dc->funcs->pDeleteDC(dc);
696 if (dc->w.hClipRgn) DeleteObject( dc->w.hClipRgn );
697 if (dc->w.hVisRgn) DeleteObject( dc->w.hVisRgn );
698 if (dc->w.hGCClipRgn) DeleteObject( dc->w.hGCClipRgn );
700 PATH_DestroyGdiPath(&dc->w.path);
702 return GDI_FreeObject( hdc );
706 /***********************************************************************
707 * ResetDC16 (GDI.376)
709 HDC16 WINAPI ResetDC16( HDC16 hdc, const DEVMODE16 *devmode )
711 FIXME(dc, "stub\n" );
712 return hdc;
716 /***********************************************************************
717 * ResetDC32A (GDI32.287)
719 HDC WINAPI ResetDCA( HDC hdc, const DEVMODEA *devmode )
721 FIXME(dc, "stub\n" );
722 return hdc;
726 /***********************************************************************
727 * ResetDC32W (GDI32.288)
729 HDC WINAPI ResetDCW( HDC hdc, const DEVMODEW *devmode )
731 FIXME(dc, "stub\n" );
732 return hdc;
736 /***********************************************************************
737 * GetDeviceCaps16 (GDI.80)
739 INT16 WINAPI GetDeviceCaps16( HDC16 hdc, INT16 cap )
741 return GetDeviceCaps( hdc, cap );
745 /***********************************************************************
746 * GetDeviceCaps32 (GDI32.171)
748 INT WINAPI GetDeviceCaps( HDC hdc, INT cap )
750 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
751 INT ret;
753 if (!dc) return 0;
755 if ((cap < 0) || (cap > sizeof(DeviceCaps)-sizeof(WORD)))
757 GDI_HEAP_UNLOCK( hdc );
758 return 0;
761 TRACE(dc, "(%04x,%d): returning %d\n",
762 hdc, cap, *(WORD *)(((char *)dc->w.devCaps) + cap) );
763 ret = *(WORD *)(((char *)dc->w.devCaps) + cap);
764 GDI_HEAP_UNLOCK( hdc );
765 return ret;
769 /***********************************************************************
770 * SetBkColor16 (GDI.1)
772 COLORREF WINAPI SetBkColor16( HDC16 hdc, COLORREF color )
774 return SetBkColor( hdc, color );
778 /***********************************************************************
779 * SetBkColor32 (GDI32.305)
781 COLORREF WINAPI SetBkColor( HDC hdc, COLORREF color )
783 COLORREF oldColor;
784 DC * dc = DC_GetDCPtr( hdc );
786 if (!dc) return 0x80000000;
787 if (dc->funcs->pSetBkColor)
788 oldColor = dc->funcs->pSetBkColor(dc, color);
789 else {
790 oldColor = dc->w.backgroundColor;
791 dc->w.backgroundColor = color;
793 GDI_HEAP_UNLOCK( hdc );
794 return oldColor;
798 /***********************************************************************
799 * SetTextColor16 (GDI.9)
801 COLORREF WINAPI SetTextColor16( HDC16 hdc, COLORREF color )
803 return SetTextColor( hdc, color );
807 /***********************************************************************
808 * SetTextColor32 (GDI32.338)
810 COLORREF WINAPI SetTextColor( HDC hdc, COLORREF color )
812 COLORREF oldColor;
813 DC * dc = DC_GetDCPtr( hdc );
815 if (!dc) return 0x80000000;
816 if (dc->funcs->pSetTextColor)
817 oldColor = dc->funcs->pSetTextColor(dc, color);
818 else {
819 oldColor = dc->w.textColor;
820 dc->w.textColor = color;
822 GDI_HEAP_UNLOCK( hdc );
823 return oldColor;
827 /***********************************************************************
828 * SetTextAlign16 (GDI.346)
830 UINT16 WINAPI SetTextAlign16( HDC16 hdc, UINT16 textAlign )
832 return SetTextAlign( hdc, textAlign );
836 /***********************************************************************
837 * SetTextAlign32 (GDI32.336)
839 UINT WINAPI SetTextAlign( HDC hdc, UINT textAlign )
841 UINT prevAlign;
842 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
843 if (!dc)
845 if (!(dc = (DC *)GDI_GetObjPtr( hdc, METAFILE_DC_MAGIC ))) return 0;
846 MFDRV_MetaParam1( dc, META_SETTEXTALIGN, textAlign );
847 GDI_HEAP_UNLOCK( hdc );
848 return 1;
850 prevAlign = dc->w.textAlign;
851 dc->w.textAlign = textAlign;
852 GDI_HEAP_UNLOCK( hdc );
853 return prevAlign;
857 /***********************************************************************
858 * GetDCOrgEx (GDI32.168)
860 BOOL WINAPI GetDCOrgEx( HDC hDC, LPPOINT lpp )
862 DC * dc;
864 if (!lpp) return FALSE;
865 if (!(dc = (DC *) GDI_GetObjPtr( hDC, DC_MAGIC ))) return FALSE;
867 #ifndef X_DISPLAY_MISSING
868 if (!(dc->w.flags & DC_MEMORY))
870 X11DRV_PDEVICE *physDev;
871 Window root;
872 int w, h, border, depth;
874 physDev = (X11DRV_PDEVICE *) dc->physDev;
876 /* FIXME: this is not correct for managed windows */
877 TSXGetGeometry( display, physDev->drawable, &root,
878 (int*)&lpp->x, (int*)&lpp->y, &w, &h, &border, &depth );
880 else
881 #endif /* !defined(X_DISPLAY_MISSING) */
882 lpp->x = lpp->y = 0;
884 lpp->x += dc->w.DCOrgX; lpp->y += dc->w.DCOrgY;
885 GDI_HEAP_UNLOCK( hDC );
886 return TRUE;
890 /***********************************************************************
891 * GetDCOrg (GDI.79)
893 DWORD WINAPI GetDCOrg16( HDC16 hdc )
895 POINT pt;
896 if( GetDCOrgEx( hdc, &pt) )
897 return MAKELONG( (WORD)pt.x, (WORD)pt.y );
898 return 0;
902 /***********************************************************************
903 * SetDCOrg (GDI.117)
905 DWORD WINAPI SetDCOrg16( HDC16 hdc, INT16 x, INT16 y )
907 DWORD prevOrg;
908 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
909 if (!dc) return 0;
910 prevOrg = dc->w.DCOrgX | (dc->w.DCOrgY << 16);
911 dc->w.DCOrgX = x;
912 dc->w.DCOrgY = y;
913 GDI_HEAP_UNLOCK( hdc );
914 return prevOrg;
918 /***********************************************************************
919 * GetGraphicsMode (GDI32.188)
921 INT WINAPI GetGraphicsMode( HDC hdc )
923 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
924 if (!dc) return 0;
925 return dc->w.GraphicsMode;
929 /***********************************************************************
930 * SetGraphicsMode (GDI32.317)
932 INT WINAPI SetGraphicsMode( HDC hdc, INT mode )
934 INT ret;
935 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
937 /* One would think that setting the graphics mode to GM_COMPATIBLE
938 * would also reset the world transformation matrix to the unity
939 * matrix. However, in Windows, this is not the case. This doesn't
940 * make a lot of sense to me, but that's the way it is.
943 if (!dc) return 0;
944 if ((mode <= 0) || (mode > GM_LAST)) return 0;
945 ret = dc->w.GraphicsMode;
946 dc->w.GraphicsMode = mode;
947 return ret;
951 /***********************************************************************
952 * GetArcDirection16 (GDI.524)
954 INT16 WINAPI GetArcDirection16( HDC16 hdc )
956 return GetArcDirection( (HDC)hdc );
960 /***********************************************************************
961 * GetArcDirection32 (GDI32.141)
963 INT WINAPI GetArcDirection( HDC hdc )
965 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
967 if (!dc)
968 return 0;
970 return dc->w.ArcDirection;
974 /***********************************************************************
975 * SetArcDirection16 (GDI.525)
977 INT16 WINAPI SetArcDirection16( HDC16 hdc, INT16 nDirection )
979 return SetArcDirection( (HDC)hdc, (INT)nDirection );
983 /***********************************************************************
984 * SetArcDirection32 (GDI32.302)
986 INT WINAPI SetArcDirection( HDC hdc, INT nDirection )
988 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
989 INT nOldDirection;
991 if (!dc)
992 return 0;
994 if (nDirection!=AD_COUNTERCLOCKWISE && nDirection!=AD_CLOCKWISE)
996 SetLastError(ERROR_INVALID_PARAMETER);
997 return 0;
1000 nOldDirection = dc->w.ArcDirection;
1001 dc->w.ArcDirection = nDirection;
1003 return nOldDirection;
1007 /***********************************************************************
1008 * GetWorldTransform (GDI32.244)
1010 BOOL WINAPI GetWorldTransform( HDC hdc, LPXFORM xform )
1012 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1014 if (!dc)
1015 return FALSE;
1016 if (!xform)
1017 return FALSE;
1019 *xform = dc->w.xformWorld2Wnd;
1021 return TRUE;
1025 /***********************************************************************
1026 * SetWorldTransform (GDI32.346)
1028 BOOL WINAPI SetWorldTransform( HDC hdc, const XFORM *xform )
1030 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1032 if (!dc)
1034 SetLastError( ERROR_INVALID_HANDLE );
1035 return FALSE;
1038 if (!xform)
1039 return FALSE;
1041 /* Check that graphics mode is GM_ADVANCED */
1042 if (dc->w.GraphicsMode!=GM_ADVANCED)
1043 return FALSE;
1045 dc->w.xformWorld2Wnd = *xform;
1047 DC_UpdateXforms( dc );
1049 return TRUE;
1053 /****************************************************************************
1054 * ModifyWorldTransform [GDI32.253]
1055 * Modifies the world transformation for a device context.
1057 * PARAMS
1058 * hdc [I] Handle to device context
1059 * xform [I] XFORM structure that will be used to modify the world
1060 * transformation
1061 * iMode [I] Specifies in what way to modify the world transformation
1062 * Possible values:
1063 * MWT_IDENTITY
1064 * Resets the world transformation to the identity matrix.
1065 * The parameter xform is ignored.
1066 * MWT_LEFTMULTIPLY
1067 * Multiplies xform into the world transformation matrix from
1068 * the left.
1069 * MWT_RIGHTMULTIPLY
1070 * Multiplies xform into the world transformation matrix from
1071 * the right.
1073 * RETURNS STD
1075 BOOL WINAPI ModifyWorldTransform( HDC hdc, const XFORM *xform,
1076 DWORD iMode )
1078 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1080 /* Check for illegal parameters */
1081 if (!dc)
1083 SetLastError( ERROR_INVALID_HANDLE );
1084 return FALSE;
1086 if (!xform)
1087 return FALSE;
1089 /* Check that graphics mode is GM_ADVANCED */
1090 if (dc->w.GraphicsMode!=GM_ADVANCED)
1091 return FALSE;
1093 switch (iMode)
1095 case MWT_IDENTITY:
1096 dc->w.xformWorld2Wnd.eM11 = 1.0f;
1097 dc->w.xformWorld2Wnd.eM12 = 0.0f;
1098 dc->w.xformWorld2Wnd.eM21 = 0.0f;
1099 dc->w.xformWorld2Wnd.eM22 = 1.0f;
1100 dc->w.xformWorld2Wnd.eDx = 0.0f;
1101 dc->w.xformWorld2Wnd.eDy = 0.0f;
1102 break;
1103 case MWT_LEFTMULTIPLY:
1104 CombineTransform( &dc->w.xformWorld2Wnd, xform,
1105 &dc->w.xformWorld2Wnd );
1106 break;
1107 case MWT_RIGHTMULTIPLY:
1108 CombineTransform( &dc->w.xformWorld2Wnd, &dc->w.xformWorld2Wnd,
1109 xform );
1110 break;
1111 default:
1112 return FALSE;
1115 DC_UpdateXforms( dc );
1117 return TRUE;
1121 /****************************************************************************
1122 * CombineTransform [GDI32.20]
1123 * Combines two transformation matrices.
1125 * PARAMS
1126 * xformResult [O] Stores the result of combining the two matrices
1127 * xform1 [I] Specifies the first matrix to apply
1128 * xform2 [I] Specifies the second matrix to apply
1130 * REMARKS
1131 * The same matrix can be passed in for more than one of the parameters.
1133 * RETURNS STD
1135 BOOL WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
1136 const XFORM *xform2 )
1138 XFORM xformTemp;
1140 /* Check for illegal parameters */
1141 if (!xformResult || !xform1 || !xform2)
1142 return FALSE;
1144 /* Create the result in a temporary XFORM, since xformResult may be
1145 * equal to xform1 or xform2 */
1146 xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
1147 xform1->eM12 * xform2->eM21;
1148 xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
1149 xform1->eM12 * xform2->eM22;
1150 xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
1151 xform1->eM22 * xform2->eM21;
1152 xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
1153 xform1->eM22 * xform2->eM22;
1154 xformTemp.eDx = xform1->eDx * xform2->eM11 +
1155 xform1->eDy * xform2->eM21 +
1156 xform2->eDx;
1157 xformTemp.eDy = xform1->eDx * xform2->eM12 +
1158 xform1->eDy * xform2->eM22 +
1159 xform2->eDy;
1161 /* Copy the result to xformResult */
1162 *xformResult = xformTemp;
1164 return TRUE;
1168 /***********************************************************************
1169 * SetDCHook (GDI.190)
1171 BOOL16 WINAPI SetDCHook( HDC16 hdc, FARPROC16 hookProc, DWORD dwHookData )
1173 DC *dc = (DC *)GDI_GetObjPtr( hdc, DC_MAGIC );
1175 TRACE(dc, "hookProc %08x, default is %08x\n",
1176 (UINT)hookProc, (UINT)DCHook16 );
1178 if (!dc) return FALSE;
1179 dc->hookProc = hookProc;
1180 dc->dwHookData = dwHookData;
1181 GDI_HEAP_UNLOCK( hdc );
1182 return TRUE;
1186 /***********************************************************************
1187 * GetDCHook (GDI.191)
1189 DWORD WINAPI GetDCHook( HDC16 hdc, FARPROC16 *phookProc )
1191 DC *dc = (DC *)GDI_GetObjPtr( hdc, DC_MAGIC );
1192 if (!dc) return 0;
1193 *phookProc = dc->hookProc;
1194 GDI_HEAP_UNLOCK( hdc );
1195 return dc->dwHookData;
1199 /***********************************************************************
1200 * SetHookFlags (GDI.192)
1202 WORD WINAPI SetHookFlags16(HDC16 hDC, WORD flags)
1204 DC* dc = (DC*)GDI_GetObjPtr( hDC, DC_MAGIC );
1206 if( dc )
1208 WORD wRet = dc->w.flags & DC_DIRTY;
1210 /* "Undocumented Windows" info is slightly confusing.
1213 TRACE(dc,"hDC %04x, flags %04x\n",hDC,flags);
1215 if( flags & DCHF_INVALIDATEVISRGN )
1216 dc->w.flags |= DC_DIRTY;
1217 else if( flags & DCHF_VALIDATEVISRGN || !flags )
1218 dc->w.flags &= ~DC_DIRTY;
1219 GDI_HEAP_UNLOCK( hDC );
1220 return wRet;
1222 return 0;
1225 /***********************************************************************
1226 * SetICMMode (GDI32.318)
1228 INT WINAPI SetICMMode(HDC hdc, INT iEnableICM)
1230 /*FIXME Asuming that ICM is always off, and cannot be turned on */
1231 if (iEnableICM == ICM_OFF) return ICM_OFF;
1232 if (iEnableICM == ICM_ON) return 0;
1233 if (iEnableICM == ICM_QUERY) return ICM_OFF;
1234 return 0;
1238 /***********************************************************************
1239 * GetColorSpace (GDI32.165)
1241 HCOLORSPACE WINAPI GetColorSpace(HDC hdc)
1243 /*FIXME Need to to whatever GetColorSpace actually does */
1244 return 0;
1247 /***********************************************************************
1248 * GetBoundsRect16 (GDI.194)
1250 UINT16 WINAPI GetBoundsRect16(HDC16 hdc, LPRECT16 rect, UINT16 flags)
1252 return DCB_RESET | DCB_DISABLE; /* bounding rectangle always empty and disabled*/
1255 /***********************************************************************
1256 * GetBoundsRect32 (GDI32.147)
1258 UINT WINAPI GetBoundsRect(HDC hdc, LPRECT rect, UINT flags)
1260 FIXME(dc, "(): stub\n");
1261 return DCB_RESET; /* bounding rectangle always empty */
1264 /***********************************************************************
1265 * SetBoundsRect16 (GDI.193)
1267 UINT16 WINAPI SetBoundsRect16(HDC16 hdc, const RECT16* rect, UINT16 flags)
1269 if ( (flags & DCB_ACCUMULATE) || (flags & DCB_ENABLE) )
1270 FIXME( dc, "(%04x, %p, %04x): stub\n", hdc, rect, flags );
1272 return DCB_RESET | DCB_DISABLE; /* bounding rectangle always empty and disabled*/
1275 /***********************************************************************
1276 * SetBoundsRect32 (GDI32.307)
1278 UINT WINAPI SetBoundsRect(HDC hdc, const RECT* rect, UINT flags)
1280 FIXME(dc, "(): stub\n");
1281 return DCB_DISABLE; /* bounding rectangle always empty */
1284 /***********************************************************************
1285 * Death (GDI.121)
1287 * Disables GDI, switches back to text mode.
1288 * We don't have to do anything here,
1289 * just let console support handle everything
1291 void WINAPI Death16(HDC16 hDC)
1293 MSG("Death(%04x) called. Application enters text mode...\n", hDC);
1296 /***********************************************************************
1297 * Resurrection (GDI.122)
1299 * Restores GDI functionality
1301 void WINAPI Resurrection16(HDC16 hDC,
1302 WORD w1, WORD w2, WORD w3, WORD w4, WORD w5, WORD w6)
1304 MSG("Resurrection(%04x, %04x, %04x, %04x, %04x, %04x, %04x) called. Application left text mode.\n", hDC, w1, w2, w3, w4, w5, w6);