Added some error checking ; made use of new 16/32 MCI message handling
[wine/multimedia.git] / objects / dc.c
blob10532c250107284d226748b3506a6a9ce8fdcf82
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"
19 /***********************************************************************
20 * DC_Init_DC_INFO
22 * Fill the WIN_DC_INFO structure.
24 static void DC_Init_DC_INFO( WIN_DC_INFO *win_dc_info )
26 win_dc_info->flags = 0;
27 win_dc_info->devCaps = NULL;
28 win_dc_info->hClipRgn = 0;
29 win_dc_info->hVisRgn = 0;
30 win_dc_info->hGCClipRgn = 0;
31 win_dc_info->hPen = STOCK_BLACK_PEN;
32 win_dc_info->hBrush = STOCK_WHITE_BRUSH;
33 win_dc_info->hFont = STOCK_SYSTEM_FONT;
34 win_dc_info->hBitmap = 0;
35 win_dc_info->hFirstBitmap = 0;
36 win_dc_info->hDevice = 0;
37 win_dc_info->hPalette = STOCK_DEFAULT_PALETTE;
38 win_dc_info->ROPmode = R2_COPYPEN;
39 win_dc_info->polyFillMode = ALTERNATE;
40 win_dc_info->stretchBltMode = BLACKONWHITE;
41 win_dc_info->relAbsMode = ABSOLUTE;
42 win_dc_info->backgroundMode = OPAQUE;
43 win_dc_info->backgroundColor = RGB( 255, 255, 255 );
44 win_dc_info->textColor = RGB( 0, 0, 0 );
45 win_dc_info->brushOrgX = 0;
46 win_dc_info->brushOrgY = 0;
47 win_dc_info->textAlign = TA_LEFT | TA_TOP | TA_NOUPDATECP;
48 win_dc_info->charExtra = 0;
49 win_dc_info->breakTotalExtra = 0;
50 win_dc_info->breakCount = 0;
51 win_dc_info->breakExtra = 0;
52 win_dc_info->breakRem = 0;
53 win_dc_info->bitsPerPixel = 1;
54 win_dc_info->MapMode = MM_TEXT;
55 win_dc_info->GraphicsMode = GM_COMPATIBLE;
56 win_dc_info->DCOrgX = 0;
57 win_dc_info->DCOrgY = 0;
58 win_dc_info->lpfnPrint = NULL;
59 win_dc_info->CursPosX = 0;
60 win_dc_info->CursPosY = 0;
61 win_dc_info->ArcDirection = AD_COUNTERCLOCKWISE;
62 win_dc_info->xformWorld2Wnd.eM11 = 1.0f;
63 win_dc_info->xformWorld2Wnd.eM12 = 0.0f;
64 win_dc_info->xformWorld2Wnd.eM21 = 0.0f;
65 win_dc_info->xformWorld2Wnd.eM22 = 1.0f;
66 win_dc_info->xformWorld2Wnd.eDx = 0.0f;
67 win_dc_info->xformWorld2Wnd.eDy = 0.0f;
68 win_dc_info->xformWorld2Vport = win_dc_info->xformWorld2Wnd;
69 win_dc_info->xformVport2World = win_dc_info->xformWorld2Wnd;
70 win_dc_info->vport2WorldValid = TRUE;
72 PATH_InitGdiPath(&win_dc_info->path);
76 /***********************************************************************
77 * DC_AllocDC
79 DC *DC_AllocDC( const DC_FUNCTIONS *funcs )
81 HDC16 hdc;
82 DC *dc;
84 if (!(hdc = GDI_AllocObject( sizeof(DC), DC_MAGIC ))) return NULL;
85 dc = (DC *) GDI_HEAP_LOCK( hdc );
87 dc->hSelf = hdc;
88 dc->funcs = funcs;
89 dc->physDev = NULL;
90 dc->saveLevel = 0;
91 dc->dwHookData = 0L;
92 dc->hookProc = NULL;
93 dc->wndOrgX = 0;
94 dc->wndOrgY = 0;
95 dc->wndExtX = 1;
96 dc->wndExtY = 1;
97 dc->vportOrgX = 0;
98 dc->vportOrgY = 0;
99 dc->vportExtX = 1;
100 dc->vportExtY = 1;
102 DC_Init_DC_INFO( &dc->w );
104 return dc;
109 /***********************************************************************
110 * DC_GetDCPtr
112 DC *DC_GetDCPtr( HDC32 hdc )
114 GDIOBJHDR *ptr = (GDIOBJHDR *)GDI_HEAP_LOCK( hdc );
115 if (!ptr) return NULL;
116 if ((ptr->wMagic == DC_MAGIC) || (ptr->wMagic == METAFILE_DC_MAGIC))
117 return (DC *)ptr;
118 GDI_HEAP_UNLOCK( hdc );
119 return NULL;
123 /***********************************************************************
124 * DC_InitDC
126 * Setup device-specific DC values for a newly created DC.
128 void DC_InitDC( DC* dc )
130 RealizeDefaultPalette( dc->hSelf );
131 SetTextColor32( dc->hSelf, dc->w.textColor );
132 SetBkColor32( dc->hSelf, dc->w.backgroundColor );
133 SelectObject32( dc->hSelf, dc->w.hPen );
134 SelectObject32( dc->hSelf, dc->w.hBrush );
135 SelectObject32( dc->hSelf, dc->w.hFont );
136 CLIPPING_UpdateGCRegion( dc );
140 /***********************************************************************
141 * DC_InvertXform
143 * Computes the inverse of the transformation xformSrc and stores it to
144 * xformDest. Returns TRUE if successful or FALSE if the xformSrc matrix
145 * is singular.
147 static BOOL32 DC_InvertXform( const XFORM *xformSrc, XFORM *xformDest )
149 FLOAT determinant;
151 determinant = xformSrc->eM11*xformSrc->eM22 -
152 xformSrc->eM12*xformSrc->eM21;
153 if (determinant > -1e-12 && determinant < 1e-12)
154 return FALSE;
156 xformDest->eM11 = xformSrc->eM22 / determinant;
157 xformDest->eM12 = -xformSrc->eM12 / determinant;
158 xformDest->eM21 = -xformSrc->eM21 / determinant;
159 xformDest->eM22 = xformSrc->eM11 / determinant;
160 xformDest->eDx = -xformSrc->eDx * xformDest->eM11 -
161 xformSrc->eDy * xformDest->eM21;
162 xformDest->eDy = -xformSrc->eDx * xformDest->eM12 -
163 xformSrc->eDy * xformDest->eM22;
165 return TRUE;
169 /***********************************************************************
170 * DC_UpdateXforms
172 * Updates the xformWorld2Vport, xformVport2World and vport2WorldValid
173 * fields of the specified DC by creating a transformation that
174 * represents the current mapping mode and combining it with the DC's
175 * world transform. This function should be called whenever the
176 * parameters associated with the mapping mode (window and viewport
177 * extents and origins) or the world transform change.
179 void DC_UpdateXforms( DC *dc )
181 XFORM xformWnd2Vport;
182 FLOAT scaleX, scaleY;
184 /* Construct a transformation to do the window-to-viewport conversion */
185 scaleX = (FLOAT)dc->vportExtX / (FLOAT)dc->wndExtX;
186 scaleY = (FLOAT)dc->vportExtY / (FLOAT)dc->wndExtY;
187 xformWnd2Vport.eM11 = scaleX;
188 xformWnd2Vport.eM12 = 0.0;
189 xformWnd2Vport.eM21 = 0.0;
190 xformWnd2Vport.eM22 = scaleY;
191 xformWnd2Vport.eDx = (FLOAT)dc->vportOrgX -
192 scaleX * (FLOAT)dc->wndOrgX;
193 xformWnd2Vport.eDy = (FLOAT)dc->vportOrgY -
194 scaleY * (FLOAT)dc->wndOrgY;
196 /* Combine with the world transformation */
197 CombineTransform( &dc->w.xformWorld2Vport, &dc->w.xformWorld2Wnd,
198 &xformWnd2Vport );
200 /* Create inverse of world-to-viewport transformation */
201 dc->w.vport2WorldValid = DC_InvertXform( &dc->w.xformWorld2Vport,
202 &dc->w.xformVport2World );
206 /***********************************************************************
207 * GetDCState (GDI.179)
209 HDC16 WINAPI GetDCState( HDC16 hdc )
211 DC * newdc, * dc;
212 HGDIOBJ16 handle;
214 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
215 if (!(handle = GDI_AllocObject( sizeof(DC), DC_MAGIC )))
217 GDI_HEAP_UNLOCK( hdc );
218 return 0;
220 newdc = (DC *) GDI_HEAP_LOCK( handle );
222 TRACE(dc, "(%04x): returning %04x\n", hdc, handle );
224 memset( &newdc->u.x, 0, sizeof(newdc->u.x) );
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 if (!lpp) return FALSE;
858 if (!(dc = (DC *) GDI_GetObjPtr( hDC, DC_MAGIC ))) return FALSE;
860 if (!(dc->w.flags & DC_MEMORY))
862 Window root;
863 int w, h, border, depth;
864 /* FIXME: this is not correct for managed windows */
865 TSXGetGeometry( display, dc->u.x.drawable, &root,
866 &lpp->x, &lpp->y, &w, &h, &border, &depth );
868 else lpp->x = lpp->y = 0;
869 lpp->x += dc->w.DCOrgX; lpp->y += dc->w.DCOrgY;
870 GDI_HEAP_UNLOCK( hDC );
871 return TRUE;
875 /***********************************************************************
876 * GetDCOrg (GDI.79)
878 DWORD WINAPI GetDCOrg( HDC16 hdc )
880 POINT32 pt;
881 if( GetDCOrgEx( hdc, &pt) )
882 return MAKELONG( (WORD)pt.x, (WORD)pt.y );
883 return 0;
887 /***********************************************************************
888 * SetDCOrg (GDI.117)
890 DWORD WINAPI SetDCOrg( HDC16 hdc, INT16 x, INT16 y )
892 DWORD prevOrg;
893 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
894 if (!dc) return 0;
895 prevOrg = dc->w.DCOrgX | (dc->w.DCOrgY << 16);
896 dc->w.DCOrgX = x;
897 dc->w.DCOrgY = y;
898 GDI_HEAP_UNLOCK( hdc );
899 return prevOrg;
903 /***********************************************************************
904 * GetGraphicsMode (GDI32.188)
906 INT32 WINAPI GetGraphicsMode( HDC32 hdc )
908 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
909 if (!dc) return 0;
910 return dc->w.GraphicsMode;
914 /***********************************************************************
915 * SetGraphicsMode (GDI32.317)
917 INT32 WINAPI SetGraphicsMode( HDC32 hdc, INT32 mode )
919 INT32 ret;
920 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
922 /* One would think that setting the graphics mode to GM_COMPATIBLE
923 * would also reset the world transformation matrix to the unity
924 * matrix. However, in Windows, this is not the case. This doesn't
925 * make a lot of sense to me, but that's the way it is.
928 if (!dc) return 0;
929 if ((mode <= 0) || (mode > GM_LAST)) return 0;
930 ret = dc->w.GraphicsMode;
931 dc->w.GraphicsMode = mode;
932 return ret;
936 /***********************************************************************
937 * GetArcDirection16 (GDI.524)
939 INT16 WINAPI GetArcDirection16( HDC16 hdc )
941 return GetArcDirection32( (HDC32)hdc );
945 /***********************************************************************
946 * GetArcDirection32 (GDI32.141)
948 INT32 WINAPI GetArcDirection32( HDC32 hdc )
950 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
952 if (!dc)
953 return 0;
955 return dc->w.ArcDirection;
959 /***********************************************************************
960 * SetArcDirection16 (GDI.525)
962 INT16 WINAPI SetArcDirection16( HDC16 hdc, INT16 nDirection )
964 return SetArcDirection32( (HDC32)hdc, (INT32)nDirection );
968 /***********************************************************************
969 * SetArcDirection32 (GDI32.302)
971 INT32 WINAPI SetArcDirection32( HDC32 hdc, INT32 nDirection )
973 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
974 INT32 nOldDirection;
976 if (!dc)
977 return 0;
979 if (nDirection!=AD_COUNTERCLOCKWISE && nDirection!=AD_CLOCKWISE)
981 SetLastError(ERROR_INVALID_PARAMETER);
982 return 0;
985 nOldDirection = dc->w.ArcDirection;
986 dc->w.ArcDirection = nDirection;
988 return nOldDirection;
992 /***********************************************************************
993 * GetWorldTransform (GDI32.244)
995 BOOL32 WINAPI GetWorldTransform( HDC32 hdc, LPXFORM xform )
997 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
999 if (!dc)
1000 return FALSE;
1001 if (!xform)
1002 return FALSE;
1004 *xform = dc->w.xformWorld2Wnd;
1006 return TRUE;
1010 /***********************************************************************
1011 * SetWorldTransform (GDI32.346)
1013 BOOL32 WINAPI SetWorldTransform( HDC32 hdc, const XFORM *xform )
1015 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1017 if (!dc)
1019 SetLastError( ERROR_INVALID_HANDLE );
1020 return FALSE;
1023 if (!xform)
1024 return FALSE;
1026 /* Check that graphics mode is GM_ADVANCED */
1027 if (dc->w.GraphicsMode!=GM_ADVANCED)
1028 return FALSE;
1030 dc->w.xformWorld2Wnd = *xform;
1032 DC_UpdateXforms( dc );
1034 return TRUE;
1038 /****************************************************************************
1039 * ModifyWorldTransform [GDI32.253]
1040 * Modifies the world transformation for a device context.
1042 * PARAMS
1043 * hdc [I] Handle to device context
1044 * xform [I] XFORM structure that will be used to modify the world
1045 * transformation
1046 * iMode [I] Specifies in what way to modify the world transformation
1047 * Possible values:
1048 * MWT_IDENTITY
1049 * Resets the world transformation to the identity matrix.
1050 * The parameter xform is ignored.
1051 * MWT_LEFTMULTIPLY
1052 * Multiplies xform into the world transformation matrix from
1053 * the left.
1054 * MWT_RIGHTMULTIPLY
1055 * Multiplies xform into the world transformation matrix from
1056 * the right.
1058 * RETURNS STD
1060 BOOL32 WINAPI ModifyWorldTransform( HDC32 hdc, const XFORM *xform,
1061 DWORD iMode )
1063 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1065 /* Check for illegal parameters */
1066 if (!dc)
1068 SetLastError( ERROR_INVALID_HANDLE );
1069 return FALSE;
1071 if (!xform)
1072 return FALSE;
1074 /* Check that graphics mode is GM_ADVANCED */
1075 if (dc->w.GraphicsMode!=GM_ADVANCED)
1076 return FALSE;
1078 switch (iMode)
1080 case MWT_IDENTITY:
1081 dc->w.xformWorld2Wnd.eM11 = 1.0f;
1082 dc->w.xformWorld2Wnd.eM12 = 0.0f;
1083 dc->w.xformWorld2Wnd.eM21 = 0.0f;
1084 dc->w.xformWorld2Wnd.eM22 = 1.0f;
1085 dc->w.xformWorld2Wnd.eDx = 0.0f;
1086 dc->w.xformWorld2Wnd.eDy = 0.0f;
1087 break;
1088 case MWT_LEFTMULTIPLY:
1089 CombineTransform( &dc->w.xformWorld2Wnd, xform,
1090 &dc->w.xformWorld2Wnd );
1091 break;
1092 case MWT_RIGHTMULTIPLY:
1093 CombineTransform( &dc->w.xformWorld2Wnd, &dc->w.xformWorld2Wnd,
1094 xform );
1095 break;
1096 default:
1097 return FALSE;
1100 DC_UpdateXforms( dc );
1102 return TRUE;
1106 /****************************************************************************
1107 * CombineTransform [GDI32.20]
1108 * Combines two transformation matrices.
1110 * PARAMS
1111 * xformResult [O] Stores the result of combining the two matrices
1112 * xform1 [I] Specifies the first matrix to apply
1113 * xform2 [I] Specifies the second matrix to apply
1115 * REMARKS
1116 * The same matrix can be passed in for more than one of the parameters.
1118 * RETURNS STD
1120 BOOL32 WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
1121 const XFORM *xform2 )
1123 XFORM xformTemp;
1125 /* Check for illegal parameters */
1126 if (!xformResult || !xform1 || !xform2)
1127 return FALSE;
1129 /* Create the result in a temporary XFORM, since xformResult may be
1130 * equal to xform1 or xform2 */
1131 xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
1132 xform1->eM12 * xform2->eM21;
1133 xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
1134 xform1->eM12 * xform2->eM22;
1135 xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
1136 xform1->eM22 * xform2->eM21;
1137 xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
1138 xform1->eM22 * xform2->eM22;
1139 xformTemp.eDx = xform1->eDx * xform2->eM11 +
1140 xform1->eDy * xform2->eM21 +
1141 xform2->eDx;
1142 xformTemp.eDy = xform1->eDx * xform2->eM12 +
1143 xform1->eDy * xform2->eM22 +
1144 xform2->eDy;
1146 /* Copy the result to xformResult */
1147 *xformResult = xformTemp;
1149 return TRUE;
1153 /***********************************************************************
1154 * SetDCHook (GDI.190)
1156 BOOL16 WINAPI SetDCHook( HDC16 hdc, FARPROC16 hookProc, DWORD dwHookData )
1158 DC *dc = (DC *)GDI_GetObjPtr( hdc, DC_MAGIC );
1160 TRACE(dc, "hookProc %08x, default is %08x\n",
1161 (UINT32)hookProc, (UINT32)DCHook );
1163 if (!dc) return FALSE;
1164 dc->hookProc = hookProc;
1165 dc->dwHookData = dwHookData;
1166 GDI_HEAP_UNLOCK( hdc );
1167 return TRUE;
1171 /***********************************************************************
1172 * GetDCHook (GDI.191)
1174 DWORD WINAPI GetDCHook( HDC16 hdc, FARPROC16 *phookProc )
1176 DC *dc = (DC *)GDI_GetObjPtr( hdc, DC_MAGIC );
1177 if (!dc) return 0;
1178 *phookProc = dc->hookProc;
1179 GDI_HEAP_UNLOCK( hdc );
1180 return dc->dwHookData;
1184 /***********************************************************************
1185 * SetHookFlags (GDI.192)
1187 WORD WINAPI SetHookFlags(HDC16 hDC, WORD flags)
1189 DC* dc = (DC*)GDI_GetObjPtr( hDC, DC_MAGIC );
1191 if( dc )
1193 WORD wRet = dc->w.flags & DC_DIRTY;
1195 /* "Undocumented Windows" info is slightly confusing.
1198 TRACE(dc,"hDC %04x, flags %04x\n",hDC,flags);
1200 if( flags & DCHF_INVALIDATEVISRGN )
1201 dc->w.flags |= DC_DIRTY;
1202 else if( flags & DCHF_VALIDATEVISRGN || !flags )
1203 dc->w.flags &= ~DC_DIRTY;
1204 GDI_HEAP_UNLOCK( hDC );
1205 return wRet;
1207 return 0;
1210 /***********************************************************************
1211 * SetICMMode (GDI32.318)
1213 INT32 WINAPI SetICMMode(HDC32 hdc, INT32 iEnableICM)
1215 /*FIXME Asuming that ICM is always off, and cannot be turned on */
1216 if (iEnableICM == ICM_OFF) return ICM_OFF;
1217 if (iEnableICM == ICM_ON) return 0;
1218 if (iEnableICM == ICM_QUERY) return ICM_OFF;
1219 return 0;
1223 /***********************************************************************
1224 * GetColorSpace (GDI32.165)
1226 HCOLORSPACE32 WINAPI GetColorSpace(HDC32 hdc)
1228 /*FIXME Need to to whatever GetColorSpace actually does */
1229 return 0;
1232 /***********************************************************************
1233 * GetBoundsRect16 (GDI.194)
1235 UINT16 WINAPI GetBoundsRect16(HDC16 hdc, LPRECT16 rect, UINT16 flags)
1237 FIXME(dc, "(): stub\n");
1238 return DCB_RESET; /* bounding rectangle always empty */
1241 /***********************************************************************
1242 * SetBoundsRect16 (GDI.193)
1244 UINT16 WINAPI SetBoundsRect16(HDC16 hdc, const RECT16* rect, UINT16 flags)
1246 FIXME(dc, "(): stub\n");
1247 return DCB_DISABLE; /* bounding rectangle always empty */