Change (32 bit) HMETAFILEs to GDI objects (HMETAFILE16s remain as
[wine.git] / objects / dc.c
blob5f8a41fa0c6ee7cc635fb5c4ae97d6d9adc3f4b9
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 /***********************************************************************
25 * DC_Init_DC_INFO
27 * Fill the WIN_DC_INFO structure.
29 static void DC_Init_DC_INFO( WIN_DC_INFO *win_dc_info )
31 win_dc_info->flags = 0;
32 win_dc_info->devCaps = NULL;
33 win_dc_info->hClipRgn = 0;
34 win_dc_info->hVisRgn = 0;
35 win_dc_info->hGCClipRgn = 0;
36 win_dc_info->hPen = STOCK_BLACK_PEN;
37 win_dc_info->hBrush = STOCK_WHITE_BRUSH;
38 win_dc_info->hFont = STOCK_SYSTEM_FONT;
39 win_dc_info->hBitmap = 0;
40 win_dc_info->hFirstBitmap = 0;
41 win_dc_info->hDevice = 0;
42 win_dc_info->hPalette = STOCK_DEFAULT_PALETTE;
43 win_dc_info->ROPmode = R2_COPYPEN;
44 win_dc_info->polyFillMode = ALTERNATE;
45 win_dc_info->stretchBltMode = BLACKONWHITE;
46 win_dc_info->relAbsMode = ABSOLUTE;
47 win_dc_info->backgroundMode = OPAQUE;
48 win_dc_info->backgroundColor = RGB( 255, 255, 255 );
49 win_dc_info->textColor = RGB( 0, 0, 0 );
50 win_dc_info->brushOrgX = 0;
51 win_dc_info->brushOrgY = 0;
52 win_dc_info->textAlign = TA_LEFT | TA_TOP | TA_NOUPDATECP;
53 win_dc_info->charExtra = 0;
54 win_dc_info->breakTotalExtra = 0;
55 win_dc_info->breakCount = 0;
56 win_dc_info->breakExtra = 0;
57 win_dc_info->breakRem = 0;
58 win_dc_info->bitsPerPixel = 1;
59 win_dc_info->MapMode = MM_TEXT;
60 win_dc_info->GraphicsMode = GM_COMPATIBLE;
61 win_dc_info->DCOrgX = 0;
62 win_dc_info->DCOrgY = 0;
63 win_dc_info->lpfnPrint = NULL;
64 win_dc_info->CursPosX = 0;
65 win_dc_info->CursPosY = 0;
66 win_dc_info->ArcDirection = AD_COUNTERCLOCKWISE;
67 win_dc_info->xformWorld2Wnd.eM11 = 1.0f;
68 win_dc_info->xformWorld2Wnd.eM12 = 0.0f;
69 win_dc_info->xformWorld2Wnd.eM21 = 0.0f;
70 win_dc_info->xformWorld2Wnd.eM22 = 1.0f;
71 win_dc_info->xformWorld2Wnd.eDx = 0.0f;
72 win_dc_info->xformWorld2Wnd.eDy = 0.0f;
73 win_dc_info->xformWorld2Vport = win_dc_info->xformWorld2Wnd;
74 win_dc_info->xformVport2World = win_dc_info->xformWorld2Wnd;
75 win_dc_info->vport2WorldValid = TRUE;
77 PATH_InitGdiPath(&win_dc_info->path);
81 /***********************************************************************
82 * DC_AllocDC
84 DC *DC_AllocDC( const DC_FUNCTIONS *funcs )
86 HDC16 hdc;
87 DC *dc;
89 if (!(hdc = GDI_AllocObject( sizeof(DC), DC_MAGIC ))) return NULL;
90 dc = (DC *) GDI_HEAP_LOCK( hdc );
92 dc->hSelf = hdc;
93 dc->funcs = funcs;
94 dc->physDev = NULL;
95 dc->saveLevel = 0;
96 dc->dwHookData = 0L;
97 dc->hookProc = NULL;
98 dc->wndOrgX = 0;
99 dc->wndOrgY = 0;
100 dc->wndExtX = 1;
101 dc->wndExtY = 1;
102 dc->vportOrgX = 0;
103 dc->vportOrgY = 0;
104 dc->vportExtX = 1;
105 dc->vportExtY = 1;
107 DC_Init_DC_INFO( &dc->w );
109 return dc;
114 /***********************************************************************
115 * DC_GetDCPtr
117 DC *DC_GetDCPtr( HDC hdc )
119 GDIOBJHDR *ptr = (GDIOBJHDR *)GDI_HEAP_LOCK( hdc );
120 if (!ptr) return NULL;
121 if ((ptr->wMagic == DC_MAGIC) || (ptr->wMagic == METAFILE_DC_MAGIC))
122 return (DC *)ptr;
123 GDI_HEAP_UNLOCK( hdc );
124 return NULL;
128 /***********************************************************************
129 * DC_InitDC
131 * Setup device-specific DC values for a newly created DC.
133 void DC_InitDC( DC* dc )
135 RealizeDefaultPalette16( dc->hSelf );
136 SetTextColor( dc->hSelf, dc->w.textColor );
137 SetBkColor( dc->hSelf, dc->w.backgroundColor );
138 SelectObject( dc->hSelf, dc->w.hPen );
139 SelectObject( dc->hSelf, dc->w.hBrush );
140 SelectObject( dc->hSelf, dc->w.hFont );
141 CLIPPING_UpdateGCRegion( dc );
145 /***********************************************************************
146 * DC_InvertXform
148 * Computes the inverse of the transformation xformSrc and stores it to
149 * xformDest. Returns TRUE if successful or FALSE if the xformSrc matrix
150 * is singular.
152 static BOOL DC_InvertXform( const XFORM *xformSrc, XFORM *xformDest )
154 FLOAT determinant;
156 determinant = xformSrc->eM11*xformSrc->eM22 -
157 xformSrc->eM12*xformSrc->eM21;
158 if (determinant > -1e-12 && determinant < 1e-12)
159 return FALSE;
161 xformDest->eM11 = xformSrc->eM22 / determinant;
162 xformDest->eM12 = -xformSrc->eM12 / determinant;
163 xformDest->eM21 = -xformSrc->eM21 / determinant;
164 xformDest->eM22 = xformSrc->eM11 / determinant;
165 xformDest->eDx = -xformSrc->eDx * xformDest->eM11 -
166 xformSrc->eDy * xformDest->eM21;
167 xformDest->eDy = -xformSrc->eDx * xformDest->eM12 -
168 xformSrc->eDy * xformDest->eM22;
170 return TRUE;
174 /***********************************************************************
175 * DC_UpdateXforms
177 * Updates the xformWorld2Vport, xformVport2World and vport2WorldValid
178 * fields of the specified DC by creating a transformation that
179 * represents the current mapping mode and combining it with the DC's
180 * world transform. This function should be called whenever the
181 * parameters associated with the mapping mode (window and viewport
182 * extents and origins) or the world transform change.
184 void DC_UpdateXforms( DC *dc )
186 XFORM xformWnd2Vport;
187 FLOAT scaleX, scaleY;
189 /* Construct a transformation to do the window-to-viewport conversion */
190 scaleX = (FLOAT)dc->vportExtX / (FLOAT)dc->wndExtX;
191 scaleY = (FLOAT)dc->vportExtY / (FLOAT)dc->wndExtY;
192 xformWnd2Vport.eM11 = scaleX;
193 xformWnd2Vport.eM12 = 0.0;
194 xformWnd2Vport.eM21 = 0.0;
195 xformWnd2Vport.eM22 = scaleY;
196 xformWnd2Vport.eDx = (FLOAT)dc->vportOrgX -
197 scaleX * (FLOAT)dc->wndOrgX;
198 xformWnd2Vport.eDy = (FLOAT)dc->vportOrgY -
199 scaleY * (FLOAT)dc->wndOrgY;
201 /* Combine with the world transformation */
202 CombineTransform( &dc->w.xformWorld2Vport, &dc->w.xformWorld2Wnd,
203 &xformWnd2Vport );
205 /* Create inverse of world-to-viewport transformation */
206 dc->w.vport2WorldValid = DC_InvertXform( &dc->w.xformWorld2Vport,
207 &dc->w.xformVport2World );
211 /***********************************************************************
212 * GetDCState (GDI.179)
214 HDC16 WINAPI GetDCState16( HDC16 hdc )
216 DC * newdc, * dc;
217 HGDIOBJ16 handle;
219 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
220 if (!(handle = GDI_AllocObject( sizeof(DC), DC_MAGIC )))
222 GDI_HEAP_UNLOCK( hdc );
223 return 0;
225 newdc = (DC *) GDI_HEAP_LOCK( handle );
227 TRACE(dc, "(%04x): returning %04x\n", hdc, handle );
229 newdc->w.flags = dc->w.flags | DC_SAVED;
230 newdc->w.devCaps = dc->w.devCaps;
231 newdc->w.hPen = dc->w.hPen;
232 newdc->w.hBrush = dc->w.hBrush;
233 newdc->w.hFont = dc->w.hFont;
234 newdc->w.hBitmap = dc->w.hBitmap;
235 newdc->w.hFirstBitmap = dc->w.hFirstBitmap;
236 newdc->w.hDevice = dc->w.hDevice;
237 newdc->w.hPalette = dc->w.hPalette;
238 newdc->w.totalExtent = dc->w.totalExtent;
239 newdc->w.bitsPerPixel = dc->w.bitsPerPixel;
240 newdc->w.ROPmode = dc->w.ROPmode;
241 newdc->w.polyFillMode = dc->w.polyFillMode;
242 newdc->w.stretchBltMode = dc->w.stretchBltMode;
243 newdc->w.relAbsMode = dc->w.relAbsMode;
244 newdc->w.backgroundMode = dc->w.backgroundMode;
245 newdc->w.backgroundColor = dc->w.backgroundColor;
246 newdc->w.textColor = dc->w.textColor;
247 newdc->w.brushOrgX = dc->w.brushOrgX;
248 newdc->w.brushOrgY = dc->w.brushOrgY;
249 newdc->w.textAlign = dc->w.textAlign;
250 newdc->w.charExtra = dc->w.charExtra;
251 newdc->w.breakTotalExtra = dc->w.breakTotalExtra;
252 newdc->w.breakCount = dc->w.breakCount;
253 newdc->w.breakExtra = dc->w.breakExtra;
254 newdc->w.breakRem = dc->w.breakRem;
255 newdc->w.MapMode = dc->w.MapMode;
256 newdc->w.GraphicsMode = dc->w.GraphicsMode;
257 #if 0
258 /* Apparently, the DC origin is not changed by [GS]etDCState */
259 newdc->w.DCOrgX = dc->w.DCOrgX;
260 newdc->w.DCOrgY = dc->w.DCOrgY;
261 #endif
262 newdc->w.CursPosX = dc->w.CursPosX;
263 newdc->w.CursPosY = dc->w.CursPosY;
264 newdc->w.ArcDirection = dc->w.ArcDirection;
265 newdc->w.xformWorld2Wnd = dc->w.xformWorld2Wnd;
266 newdc->w.xformWorld2Vport = dc->w.xformWorld2Vport;
267 newdc->w.xformVport2World = dc->w.xformVport2World;
268 newdc->w.vport2WorldValid = dc->w.vport2WorldValid;
269 newdc->wndOrgX = dc->wndOrgX;
270 newdc->wndOrgY = dc->wndOrgY;
271 newdc->wndExtX = dc->wndExtX;
272 newdc->wndExtY = dc->wndExtY;
273 newdc->vportOrgX = dc->vportOrgX;
274 newdc->vportOrgY = dc->vportOrgY;
275 newdc->vportExtX = dc->vportExtX;
276 newdc->vportExtY = dc->vportExtY;
278 newdc->hSelf = (HDC)handle;
279 newdc->saveLevel = 0;
281 PATH_InitGdiPath( &newdc->w.path );
283 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
285 newdc->w.hGCClipRgn = newdc->w.hVisRgn = 0;
286 if (dc->w.hClipRgn)
288 newdc->w.hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
289 CombineRgn( newdc->w.hClipRgn, dc->w.hClipRgn, 0, RGN_COPY );
291 else
292 newdc->w.hClipRgn = 0;
293 GDI_HEAP_UNLOCK( handle );
294 GDI_HEAP_UNLOCK( hdc );
295 return handle;
299 /***********************************************************************
300 * SetDCState (GDI.180)
302 void WINAPI SetDCState16( HDC16 hdc, HDC16 hdcs )
304 DC *dc, *dcs;
306 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return;
307 if (!(dcs = (DC *) GDI_GetObjPtr( hdcs, DC_MAGIC )))
309 GDI_HEAP_UNLOCK( hdc );
310 return;
312 if (!dcs->w.flags & DC_SAVED)
314 GDI_HEAP_UNLOCK( hdc );
315 GDI_HEAP_UNLOCK( hdcs );
316 return;
318 TRACE(dc, "%04x %04x\n", hdc, hdcs );
320 dc->w.flags = dcs->w.flags & ~DC_SAVED;
321 dc->w.devCaps = dcs->w.devCaps;
322 dc->w.hFirstBitmap = dcs->w.hFirstBitmap;
323 dc->w.hDevice = dcs->w.hDevice;
324 dc->w.totalExtent = dcs->w.totalExtent;
325 dc->w.ROPmode = dcs->w.ROPmode;
326 dc->w.polyFillMode = dcs->w.polyFillMode;
327 dc->w.stretchBltMode = dcs->w.stretchBltMode;
328 dc->w.relAbsMode = dcs->w.relAbsMode;
329 dc->w.backgroundMode = dcs->w.backgroundMode;
330 dc->w.backgroundColor = dcs->w.backgroundColor;
331 dc->w.textColor = dcs->w.textColor;
332 dc->w.brushOrgX = dcs->w.brushOrgX;
333 dc->w.brushOrgY = dcs->w.brushOrgY;
334 dc->w.textAlign = dcs->w.textAlign;
335 dc->w.charExtra = dcs->w.charExtra;
336 dc->w.breakTotalExtra = dcs->w.breakTotalExtra;
337 dc->w.breakCount = dcs->w.breakCount;
338 dc->w.breakExtra = dcs->w.breakExtra;
339 dc->w.breakRem = dcs->w.breakRem;
340 dc->w.MapMode = dcs->w.MapMode;
341 dc->w.GraphicsMode = dcs->w.GraphicsMode;
342 #if 0
343 /* Apparently, the DC origin is not changed by [GS]etDCState */
344 dc->w.DCOrgX = dcs->w.DCOrgX;
345 dc->w.DCOrgY = dcs->w.DCOrgY;
346 #endif
347 dc->w.CursPosX = dcs->w.CursPosX;
348 dc->w.CursPosY = dcs->w.CursPosY;
349 dc->w.ArcDirection = dcs->w.ArcDirection;
350 dc->w.xformWorld2Wnd = dcs->w.xformWorld2Wnd;
351 dc->w.xformWorld2Vport = dcs->w.xformWorld2Vport;
352 dc->w.xformVport2World = dcs->w.xformVport2World;
353 dc->w.vport2WorldValid = dcs->w.vport2WorldValid;
355 dc->wndOrgX = dcs->wndOrgX;
356 dc->wndOrgY = dcs->wndOrgY;
357 dc->wndExtX = dcs->wndExtX;
358 dc->wndExtY = dcs->wndExtY;
359 dc->vportOrgX = dcs->vportOrgX;
360 dc->vportOrgY = dcs->vportOrgY;
361 dc->vportExtX = dcs->vportExtX;
362 dc->vportExtY = dcs->vportExtY;
364 if (!(dc->w.flags & DC_MEMORY)) dc->w.bitsPerPixel = dcs->w.bitsPerPixel;
366 if (dcs->w.hClipRgn)
368 if (!dc->w.hClipRgn) dc->w.hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
369 CombineRgn( dc->w.hClipRgn, dcs->w.hClipRgn, 0, RGN_COPY );
371 else
373 if (dc->w.hClipRgn) DeleteObject16( dc->w.hClipRgn );
374 dc->w.hClipRgn = 0;
376 CLIPPING_UpdateGCRegion( dc );
378 SelectObject( hdc, dcs->w.hBitmap );
379 SelectObject( hdc, dcs->w.hBrush );
380 SelectObject( hdc, dcs->w.hFont );
381 SelectObject( hdc, dcs->w.hPen );
382 SetBkColor( hdc, dcs->w.backgroundColor);
383 SetTextColor( hdc, dcs->w.textColor);
384 GDISelectPalette16( hdc, dcs->w.hPalette, FALSE );
385 GDI_HEAP_UNLOCK( hdc );
386 GDI_HEAP_UNLOCK( hdcs );
390 /***********************************************************************
391 * SaveDC16 (GDI.30)
393 INT16 WINAPI SaveDC16( HDC16 hdc )
395 return (INT16)SaveDC( hdc );
399 /***********************************************************************
400 * SaveDC32 (GDI32.292)
402 INT WINAPI SaveDC( HDC hdc )
404 HDC hdcs;
405 DC * dc, * dcs;
406 INT ret;
408 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
409 if (!dc)
411 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
412 if (!dc) return 0;
413 MFDRV_MetaParam0(dc, META_SAVEDC);
414 GDI_HEAP_UNLOCK( hdc );
415 return 1; /* ?? */
417 if (!(hdcs = GetDCState16( hdc )))
419 GDI_HEAP_UNLOCK( hdc );
420 return 0;
422 dcs = (DC *) GDI_HEAP_LOCK( hdcs );
424 /* Copy path. The reason why path saving / restoring is in SaveDC/
425 * RestoreDC and not in GetDCState/SetDCState is that the ...DCState
426 * functions are only in Win16 (which doesn't have paths) and that
427 * SetDCState doesn't allow us to signal an error (which can happen
428 * when copying paths).
430 if (!PATH_AssignGdiPath( &dcs->w.path, &dc->w.path ))
432 GDI_HEAP_UNLOCK( hdc );
433 GDI_HEAP_UNLOCK( hdcs );
434 DeleteDC( hdcs );
435 return 0;
438 dcs->header.hNext = dc->header.hNext;
439 dc->header.hNext = hdcs;
440 TRACE(dc, "(%04x): returning %d\n", hdc, dc->saveLevel+1 );
441 ret = ++dc->saveLevel;
442 GDI_HEAP_UNLOCK( hdcs );
443 GDI_HEAP_UNLOCK( hdc );
444 return ret;
448 /***********************************************************************
449 * RestoreDC16 (GDI.39)
451 BOOL16 WINAPI RestoreDC16( HDC16 hdc, INT16 level )
453 return RestoreDC( hdc, level );
457 /***********************************************************************
458 * RestoreDC32 (GDI32.290)
460 BOOL WINAPI RestoreDC( HDC hdc, INT level )
462 DC * dc, * dcs;
463 BOOL success;
465 TRACE(dc, "%04x %d\n", hdc, level );
466 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
467 if (!dc)
469 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
470 if (!dc) return FALSE;
471 if (level != -1)
473 GDI_HEAP_UNLOCK( hdc );
474 return FALSE;
476 MFDRV_MetaParam1(dc, META_RESTOREDC, level);
477 GDI_HEAP_UNLOCK( hdc );
478 return TRUE;
480 if (level == -1) level = dc->saveLevel;
481 if ((level < 1) || (level > dc->saveLevel))
483 GDI_HEAP_UNLOCK( hdc );
484 return FALSE;
487 success=TRUE;
488 while (dc->saveLevel >= level)
490 HDC16 hdcs = dc->header.hNext;
491 if (!(dcs = (DC *) GDI_GetObjPtr( hdcs, DC_MAGIC )))
493 GDI_HEAP_UNLOCK( hdc );
494 return FALSE;
496 dc->header.hNext = dcs->header.hNext;
497 if (--dc->saveLevel < level)
499 SetDCState16( hdc, hdcs );
500 if (!PATH_AssignGdiPath( &dc->w.path, &dcs->w.path ))
501 /* FIXME: This might not be quite right, since we're
502 * returning FALSE but still destroying the saved DC state */
503 success=FALSE;
505 DeleteDC( hdcs );
507 GDI_HEAP_UNLOCK( hdc );
508 return success;
512 /***********************************************************************
513 * CreateDC16 (GDI.53)
515 HDC16 WINAPI CreateDC16( LPCSTR driver, LPCSTR device, LPCSTR output,
516 const DEVMODE16 *initData )
518 DC * dc;
519 const DC_FUNCTIONS *funcs;
521 if (!(funcs = DRIVER_FindDriver( driver ))) return 0;
522 if (!(dc = DC_AllocDC( funcs ))) return 0;
523 dc->w.flags = 0;
525 TRACE(dc, "(driver=%s, device=%s, output=%s): returning %04x\n",
526 debugstr_a(driver), debugstr_a(device), debugstr_a(output), dc->hSelf );
528 if (dc->funcs->pCreateDC &&
529 !dc->funcs->pCreateDC( dc, driver, device, output, initData ))
531 WARN(dc, "creation aborted by device\n" );
532 GDI_HEAP_FREE( dc->hSelf );
533 return 0;
536 DC_InitDC( dc );
537 GDI_HEAP_UNLOCK( dc->hSelf );
538 return dc->hSelf;
542 /***********************************************************************
543 * CreateDC32A (GDI32.)
545 HDC WINAPI CreateDCA( LPCSTR driver, LPCSTR device, LPCSTR output,
546 const DEVMODEA *initData )
548 return CreateDC16( driver, device, output, (const DEVMODE16 *)initData );
552 /***********************************************************************
553 * CreateDC32W (GDI32.)
555 HDC WINAPI CreateDCW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
556 const DEVMODEW *initData )
558 LPSTR driverA = HEAP_strdupWtoA( GetProcessHeap(), 0, driver );
559 LPSTR deviceA = HEAP_strdupWtoA( GetProcessHeap(), 0, device );
560 LPSTR outputA = HEAP_strdupWtoA( GetProcessHeap(), 0, output );
561 HDC res = CreateDC16( driverA, deviceA, outputA,
562 (const DEVMODE16 *)initData /*FIXME*/ );
563 HeapFree( GetProcessHeap(), 0, driverA );
564 HeapFree( GetProcessHeap(), 0, deviceA );
565 HeapFree( GetProcessHeap(), 0, outputA );
566 return res;
570 /***********************************************************************
571 * CreateIC16 (GDI.153)
573 HDC16 WINAPI CreateIC16( LPCSTR driver, LPCSTR device, LPCSTR output,
574 const DEVMODE16* initData )
576 /* Nothing special yet for ICs */
577 return CreateDC16( driver, device, output, initData );
581 /***********************************************************************
582 * CreateIC32A (GDI32.49)
584 HDC WINAPI CreateICA( LPCSTR driver, LPCSTR device, LPCSTR output,
585 const DEVMODEA* initData )
587 /* Nothing special yet for ICs */
588 return CreateDCA( driver, device, output, initData );
592 /***********************************************************************
593 * CreateIC32W (GDI32.50)
595 HDC WINAPI CreateICW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
596 const DEVMODEW* initData )
598 /* Nothing special yet for ICs */
599 return CreateDCW( driver, device, output, initData );
603 /***********************************************************************
604 * CreateCompatibleDC16 (GDI.52)
606 HDC16 WINAPI CreateCompatibleDC16( HDC16 hdc )
608 return (HDC16)CreateCompatibleDC( hdc );
612 /***********************************************************************
613 * CreateCompatibleDC32 (GDI32.31)
615 HDC WINAPI CreateCompatibleDC( HDC hdc )
617 DC *dc, *origDC;
618 HBITMAP hbitmap;
619 const DC_FUNCTIONS *funcs;
621 if ((origDC = (DC *)GDI_GetObjPtr( hdc, DC_MAGIC ))) funcs = origDC->funcs;
622 else funcs = DRIVER_FindDriver( "DISPLAY" );
623 if (!funcs) return 0;
625 if (!(dc = DC_AllocDC( funcs ))) return 0;
627 TRACE(dc, "(%04x): returning %04x\n",
628 hdc, dc->hSelf );
630 /* Create default bitmap */
631 if (!(hbitmap = CreateBitmap( 1, 1, 1, 1, NULL )))
633 GDI_HEAP_FREE( dc->hSelf );
634 return 0;
636 dc->w.flags = DC_MEMORY;
637 dc->w.bitsPerPixel = 1;
638 dc->w.hBitmap = hbitmap;
639 dc->w.hFirstBitmap = hbitmap;
641 if (dc->funcs->pCreateDC &&
642 !dc->funcs->pCreateDC( dc, NULL, NULL, NULL, NULL ))
644 WARN(dc, "creation aborted by device\n");
645 DeleteObject( hbitmap );
646 GDI_HEAP_FREE( dc->hSelf );
647 return 0;
650 DC_InitDC( dc );
651 GDI_HEAP_UNLOCK( dc->hSelf );
652 return dc->hSelf;
656 /***********************************************************************
657 * DeleteDC16 (GDI.68)
659 BOOL16 WINAPI DeleteDC16( HDC16 hdc )
661 return DeleteDC( hdc );
665 /***********************************************************************
666 * DeleteDC32 (GDI32.67)
668 BOOL WINAPI DeleteDC( HDC hdc )
670 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
671 if (!dc) return FALSE;
673 TRACE(dc, "%04x\n", hdc );
675 while (dc->saveLevel)
677 DC * dcs;
678 HDC16 hdcs = dc->header.hNext;
679 if (!(dcs = (DC *) GDI_GetObjPtr( hdcs, DC_MAGIC ))) break;
680 dc->header.hNext = dcs->header.hNext;
681 dc->saveLevel--;
682 DeleteDC( hdcs );
685 if (!(dc->w.flags & DC_SAVED))
687 SelectObject( hdc, STOCK_BLACK_PEN );
688 SelectObject( hdc, STOCK_WHITE_BRUSH );
689 SelectObject( hdc, STOCK_SYSTEM_FONT );
690 if (dc->w.flags & DC_MEMORY) DeleteObject( dc->w.hFirstBitmap );
691 if (dc->funcs->pDeleteDC) dc->funcs->pDeleteDC(dc);
694 if (dc->w.hClipRgn) DeleteObject( dc->w.hClipRgn );
695 if (dc->w.hVisRgn) DeleteObject( dc->w.hVisRgn );
696 if (dc->w.hGCClipRgn) DeleteObject( dc->w.hGCClipRgn );
698 PATH_DestroyGdiPath(&dc->w.path);
700 return GDI_FreeObject( hdc );
704 /***********************************************************************
705 * ResetDC16 (GDI.376)
707 HDC16 WINAPI ResetDC16( HDC16 hdc, const DEVMODE16 *devmode )
709 FIXME(dc, "stub\n" );
710 return hdc;
714 /***********************************************************************
715 * ResetDC32A (GDI32.287)
717 HDC WINAPI ResetDCA( HDC hdc, const DEVMODEA *devmode )
719 FIXME(dc, "stub\n" );
720 return hdc;
724 /***********************************************************************
725 * ResetDC32W (GDI32.288)
727 HDC WINAPI ResetDCW( HDC hdc, const DEVMODEW *devmode )
729 FIXME(dc, "stub\n" );
730 return hdc;
734 /***********************************************************************
735 * GetDeviceCaps16 (GDI.80)
737 INT16 WINAPI GetDeviceCaps16( HDC16 hdc, INT16 cap )
739 return GetDeviceCaps( hdc, cap );
743 /***********************************************************************
744 * GetDeviceCaps32 (GDI32.171)
746 INT WINAPI GetDeviceCaps( HDC hdc, INT cap )
748 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
749 INT ret;
751 if (!dc) return 0;
753 if ((cap < 0) || (cap > sizeof(DeviceCaps)-sizeof(WORD)))
755 GDI_HEAP_UNLOCK( hdc );
756 return 0;
759 TRACE(dc, "(%04x,%d): returning %d\n",
760 hdc, cap, *(WORD *)(((char *)dc->w.devCaps) + cap) );
761 ret = *(WORD *)(((char *)dc->w.devCaps) + cap);
762 GDI_HEAP_UNLOCK( hdc );
763 return ret;
767 /***********************************************************************
768 * SetBkColor16 (GDI.1)
770 COLORREF WINAPI SetBkColor16( HDC16 hdc, COLORREF color )
772 return SetBkColor( hdc, color );
776 /***********************************************************************
777 * SetBkColor32 (GDI32.305)
779 COLORREF WINAPI SetBkColor( HDC hdc, COLORREF color )
781 COLORREF oldColor;
782 DC * dc = DC_GetDCPtr( hdc );
784 if (!dc) return 0x80000000;
785 if (dc->funcs->pSetBkColor)
786 oldColor = dc->funcs->pSetBkColor(dc, color);
787 else {
788 oldColor = dc->w.backgroundColor;
789 dc->w.backgroundColor = color;
791 GDI_HEAP_UNLOCK( hdc );
792 return oldColor;
796 /***********************************************************************
797 * SetTextColor16 (GDI.9)
799 COLORREF WINAPI SetTextColor16( HDC16 hdc, COLORREF color )
801 return SetTextColor( hdc, color );
805 /***********************************************************************
806 * SetTextColor32 (GDI32.338)
808 COLORREF WINAPI SetTextColor( HDC hdc, COLORREF color )
810 COLORREF oldColor;
811 DC * dc = DC_GetDCPtr( hdc );
813 if (!dc) return 0x80000000;
814 if (dc->funcs->pSetTextColor)
815 oldColor = dc->funcs->pSetTextColor(dc, color);
816 else {
817 oldColor = dc->w.textColor;
818 dc->w.textColor = color;
820 GDI_HEAP_UNLOCK( hdc );
821 return oldColor;
825 /***********************************************************************
826 * SetTextAlign16 (GDI.346)
828 UINT16 WINAPI SetTextAlign16( HDC16 hdc, UINT16 textAlign )
830 return SetTextAlign( hdc, textAlign );
834 /***********************************************************************
835 * SetTextAlign32 (GDI32.336)
837 UINT WINAPI SetTextAlign( HDC hdc, UINT textAlign )
839 UINT prevAlign;
840 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
841 if (!dc)
843 if (!(dc = (DC *)GDI_GetObjPtr( hdc, METAFILE_DC_MAGIC ))) return 0;
844 MFDRV_MetaParam1( dc, META_SETTEXTALIGN, textAlign );
845 GDI_HEAP_UNLOCK( hdc );
846 return 1;
848 prevAlign = dc->w.textAlign;
849 dc->w.textAlign = textAlign;
850 GDI_HEAP_UNLOCK( hdc );
851 return prevAlign;
855 /***********************************************************************
856 * GetDCOrgEx (GDI32.168)
858 BOOL WINAPI GetDCOrgEx( HDC hDC, LPPOINT lpp )
860 DC * dc;
862 if (!lpp) return FALSE;
863 if (!(dc = (DC *) GDI_GetObjPtr( hDC, DC_MAGIC ))) return FALSE;
865 #ifndef X_DISPLAY_MISSING
866 if (!(dc->w.flags & DC_MEMORY))
868 X11DRV_PDEVICE *physDev;
869 Window root;
870 int w, h, border, depth;
872 physDev = (X11DRV_PDEVICE *) dc->physDev;
874 /* FIXME: this is not correct for managed windows */
875 TSXGetGeometry( display, physDev->drawable, &root,
876 (int*)&lpp->x, (int*)&lpp->y, &w, &h, &border, &depth );
878 else
879 #endif /* !defined(X_DISPLAY_MISSING) */
880 lpp->x = lpp->y = 0;
882 lpp->x += dc->w.DCOrgX; lpp->y += dc->w.DCOrgY;
883 GDI_HEAP_UNLOCK( hDC );
884 return TRUE;
888 /***********************************************************************
889 * GetDCOrg (GDI.79)
891 DWORD WINAPI GetDCOrg16( HDC16 hdc )
893 POINT pt;
894 if( GetDCOrgEx( hdc, &pt) )
895 return MAKELONG( (WORD)pt.x, (WORD)pt.y );
896 return 0;
900 /***********************************************************************
901 * SetDCOrg (GDI.117)
903 DWORD WINAPI SetDCOrg16( HDC16 hdc, INT16 x, INT16 y )
905 DWORD prevOrg;
906 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
907 if (!dc) return 0;
908 prevOrg = dc->w.DCOrgX | (dc->w.DCOrgY << 16);
909 dc->w.DCOrgX = x;
910 dc->w.DCOrgY = y;
911 GDI_HEAP_UNLOCK( hdc );
912 return prevOrg;
916 /***********************************************************************
917 * GetGraphicsMode (GDI32.188)
919 INT WINAPI GetGraphicsMode( HDC hdc )
921 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
922 if (!dc) return 0;
923 return dc->w.GraphicsMode;
927 /***********************************************************************
928 * SetGraphicsMode (GDI32.317)
930 INT WINAPI SetGraphicsMode( HDC hdc, INT mode )
932 INT ret;
933 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
935 /* One would think that setting the graphics mode to GM_COMPATIBLE
936 * would also reset the world transformation matrix to the unity
937 * matrix. However, in Windows, this is not the case. This doesn't
938 * make a lot of sense to me, but that's the way it is.
941 if (!dc) return 0;
942 if ((mode <= 0) || (mode > GM_LAST)) return 0;
943 ret = dc->w.GraphicsMode;
944 dc->w.GraphicsMode = mode;
945 return ret;
949 /***********************************************************************
950 * GetArcDirection16 (GDI.524)
952 INT16 WINAPI GetArcDirection16( HDC16 hdc )
954 return GetArcDirection( (HDC)hdc );
958 /***********************************************************************
959 * GetArcDirection32 (GDI32.141)
961 INT WINAPI GetArcDirection( HDC hdc )
963 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
965 if (!dc)
966 return 0;
968 return dc->w.ArcDirection;
972 /***********************************************************************
973 * SetArcDirection16 (GDI.525)
975 INT16 WINAPI SetArcDirection16( HDC16 hdc, INT16 nDirection )
977 return SetArcDirection( (HDC)hdc, (INT)nDirection );
981 /***********************************************************************
982 * SetArcDirection32 (GDI32.302)
984 INT WINAPI SetArcDirection( HDC hdc, INT nDirection )
986 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
987 INT nOldDirection;
989 if (!dc)
990 return 0;
992 if (nDirection!=AD_COUNTERCLOCKWISE && nDirection!=AD_CLOCKWISE)
994 SetLastError(ERROR_INVALID_PARAMETER);
995 return 0;
998 nOldDirection = dc->w.ArcDirection;
999 dc->w.ArcDirection = nDirection;
1001 return nOldDirection;
1005 /***********************************************************************
1006 * GetWorldTransform (GDI32.244)
1008 BOOL WINAPI GetWorldTransform( HDC hdc, LPXFORM xform )
1010 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1012 if (!dc)
1013 return FALSE;
1014 if (!xform)
1015 return FALSE;
1017 *xform = dc->w.xformWorld2Wnd;
1019 return TRUE;
1023 /***********************************************************************
1024 * SetWorldTransform (GDI32.346)
1026 BOOL WINAPI SetWorldTransform( HDC hdc, const XFORM *xform )
1028 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1030 if (!dc)
1032 SetLastError( ERROR_INVALID_HANDLE );
1033 return FALSE;
1036 if (!xform)
1037 return FALSE;
1039 /* Check that graphics mode is GM_ADVANCED */
1040 if (dc->w.GraphicsMode!=GM_ADVANCED)
1041 return FALSE;
1043 dc->w.xformWorld2Wnd = *xform;
1045 DC_UpdateXforms( dc );
1047 return TRUE;
1051 /****************************************************************************
1052 * ModifyWorldTransform [GDI32.253]
1053 * Modifies the world transformation for a device context.
1055 * PARAMS
1056 * hdc [I] Handle to device context
1057 * xform [I] XFORM structure that will be used to modify the world
1058 * transformation
1059 * iMode [I] Specifies in what way to modify the world transformation
1060 * Possible values:
1061 * MWT_IDENTITY
1062 * Resets the world transformation to the identity matrix.
1063 * The parameter xform is ignored.
1064 * MWT_LEFTMULTIPLY
1065 * Multiplies xform into the world transformation matrix from
1066 * the left.
1067 * MWT_RIGHTMULTIPLY
1068 * Multiplies xform into the world transformation matrix from
1069 * the right.
1071 * RETURNS STD
1073 BOOL WINAPI ModifyWorldTransform( HDC hdc, const XFORM *xform,
1074 DWORD iMode )
1076 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1078 /* Check for illegal parameters */
1079 if (!dc)
1081 SetLastError( ERROR_INVALID_HANDLE );
1082 return FALSE;
1084 if (!xform)
1085 return FALSE;
1087 /* Check that graphics mode is GM_ADVANCED */
1088 if (dc->w.GraphicsMode!=GM_ADVANCED)
1089 return FALSE;
1091 switch (iMode)
1093 case MWT_IDENTITY:
1094 dc->w.xformWorld2Wnd.eM11 = 1.0f;
1095 dc->w.xformWorld2Wnd.eM12 = 0.0f;
1096 dc->w.xformWorld2Wnd.eM21 = 0.0f;
1097 dc->w.xformWorld2Wnd.eM22 = 1.0f;
1098 dc->w.xformWorld2Wnd.eDx = 0.0f;
1099 dc->w.xformWorld2Wnd.eDy = 0.0f;
1100 break;
1101 case MWT_LEFTMULTIPLY:
1102 CombineTransform( &dc->w.xformWorld2Wnd, xform,
1103 &dc->w.xformWorld2Wnd );
1104 break;
1105 case MWT_RIGHTMULTIPLY:
1106 CombineTransform( &dc->w.xformWorld2Wnd, &dc->w.xformWorld2Wnd,
1107 xform );
1108 break;
1109 default:
1110 return FALSE;
1113 DC_UpdateXforms( dc );
1115 return TRUE;
1119 /****************************************************************************
1120 * CombineTransform [GDI32.20]
1121 * Combines two transformation matrices.
1123 * PARAMS
1124 * xformResult [O] Stores the result of combining the two matrices
1125 * xform1 [I] Specifies the first matrix to apply
1126 * xform2 [I] Specifies the second matrix to apply
1128 * REMARKS
1129 * The same matrix can be passed in for more than one of the parameters.
1131 * RETURNS STD
1133 BOOL WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
1134 const XFORM *xform2 )
1136 XFORM xformTemp;
1138 /* Check for illegal parameters */
1139 if (!xformResult || !xform1 || !xform2)
1140 return FALSE;
1142 /* Create the result in a temporary XFORM, since xformResult may be
1143 * equal to xform1 or xform2 */
1144 xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
1145 xform1->eM12 * xform2->eM21;
1146 xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
1147 xform1->eM12 * xform2->eM22;
1148 xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
1149 xform1->eM22 * xform2->eM21;
1150 xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
1151 xform1->eM22 * xform2->eM22;
1152 xformTemp.eDx = xform1->eDx * xform2->eM11 +
1153 xform1->eDy * xform2->eM21 +
1154 xform2->eDx;
1155 xformTemp.eDy = xform1->eDx * xform2->eM12 +
1156 xform1->eDy * xform2->eM22 +
1157 xform2->eDy;
1159 /* Copy the result to xformResult */
1160 *xformResult = xformTemp;
1162 return TRUE;
1166 /***********************************************************************
1167 * SetDCHook (GDI.190)
1169 BOOL16 WINAPI SetDCHook( HDC16 hdc, FARPROC16 hookProc, DWORD dwHookData )
1171 DC *dc = (DC *)GDI_GetObjPtr( hdc, DC_MAGIC );
1173 TRACE(dc, "hookProc %08x, default is %08x\n",
1174 (UINT)hookProc, (UINT)DCHook16 );
1176 if (!dc) return FALSE;
1177 dc->hookProc = hookProc;
1178 dc->dwHookData = dwHookData;
1179 GDI_HEAP_UNLOCK( hdc );
1180 return TRUE;
1184 /***********************************************************************
1185 * GetDCHook (GDI.191)
1187 DWORD WINAPI GetDCHook( HDC16 hdc, FARPROC16 *phookProc )
1189 DC *dc = (DC *)GDI_GetObjPtr( hdc, DC_MAGIC );
1190 if (!dc) return 0;
1191 *phookProc = dc->hookProc;
1192 GDI_HEAP_UNLOCK( hdc );
1193 return dc->dwHookData;
1197 /***********************************************************************
1198 * SetHookFlags (GDI.192)
1200 WORD WINAPI SetHookFlags16(HDC16 hDC, WORD flags)
1202 DC* dc = (DC*)GDI_GetObjPtr( hDC, DC_MAGIC );
1204 if( dc )
1206 WORD wRet = dc->w.flags & DC_DIRTY;
1208 /* "Undocumented Windows" info is slightly confusing.
1211 TRACE(dc,"hDC %04x, flags %04x\n",hDC,flags);
1213 if( flags & DCHF_INVALIDATEVISRGN )
1214 dc->w.flags |= DC_DIRTY;
1215 else if( flags & DCHF_VALIDATEVISRGN || !flags )
1216 dc->w.flags &= ~DC_DIRTY;
1217 GDI_HEAP_UNLOCK( hDC );
1218 return wRet;
1220 return 0;
1223 /***********************************************************************
1224 * SetICMMode (GDI32.318)
1226 INT WINAPI SetICMMode(HDC hdc, INT iEnableICM)
1228 /*FIXME Asuming that ICM is always off, and cannot be turned on */
1229 if (iEnableICM == ICM_OFF) return ICM_OFF;
1230 if (iEnableICM == ICM_ON) return 0;
1231 if (iEnableICM == ICM_QUERY) return ICM_OFF;
1232 return 0;
1236 /***********************************************************************
1237 * GetColorSpace (GDI32.165)
1239 HCOLORSPACE WINAPI GetColorSpace(HDC hdc)
1241 /*FIXME Need to to whatever GetColorSpace actually does */
1242 return 0;
1245 /***********************************************************************
1246 * GetBoundsRect16 (GDI.194)
1248 UINT16 WINAPI GetBoundsRect16(HDC16 hdc, LPRECT16 rect, UINT16 flags)
1250 return DCB_RESET | DCB_DISABLE; /* bounding rectangle always empty and disabled*/
1253 /***********************************************************************
1254 * GetBoundsRect32 (GDI32.147)
1256 UINT WINAPI GetBoundsRect(HDC hdc, LPRECT rect, UINT flags)
1258 FIXME(dc, "(): stub\n");
1259 return DCB_RESET; /* bounding rectangle always empty */
1262 /***********************************************************************
1263 * SetBoundsRect16 (GDI.193)
1265 UINT16 WINAPI SetBoundsRect16(HDC16 hdc, const RECT16* rect, UINT16 flags)
1267 if ( (flags & DCB_ACCUMULATE) || (flags & DCB_ENABLE) )
1268 FIXME( dc, "(%04x, %p, %04x): stub\n", hdc, rect, flags );
1270 return DCB_RESET | DCB_DISABLE; /* bounding rectangle always empty and disabled*/
1273 /***********************************************************************
1274 * SetBoundsRect32 (GDI32.307)
1276 UINT WINAPI SetBoundsRect(HDC hdc, const RECT* rect, UINT flags)
1278 FIXME(dc, "(): stub\n");
1279 return DCB_DISABLE; /* bounding rectangle always empty */
1282 /***********************************************************************
1283 * Death (GDI.121)
1285 * Disables GDI, switches back to text mode.
1286 * We don't have to do anything here,
1287 * just let console support handle everything
1289 void WINAPI Death16(HDC16 hDC)
1291 MSG("Death(%04x) called. Application enters text mode...\n", hDC);
1294 /***********************************************************************
1295 * Resurrection (GDI.122)
1297 * Restores GDI functionality
1299 void WINAPI Resurrection16(HDC16 hDC,
1300 WORD w1, WORD w2, WORD w3, WORD w4, WORD w5, WORD w6)
1302 MSG("Resurrection(%04x, %04x, %04x, %04x, %04x, %04x, %04x) called. Application left text mode.\n", hDC, w1, w2, w3, w4, w5, w6);