ANSI C fixes.
[wine/testsucceed.git] / objects / dc.c
blob48997179efb25063fba6f734134a8459cdfbf692
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 "debug.h"
19 #include "font.h"
20 #include "winerror.h"
21 #include "wine/winuser16.h"
23 DEFAULT_DEBUG_CHANNEL(dc)
25 /***********************************************************************
26 * DC_Init_DC_INFO
28 * Fill the WIN_DC_INFO structure.
30 static void DC_Init_DC_INFO( WIN_DC_INFO *win_dc_info )
32 win_dc_info->flags = 0;
33 win_dc_info->devCaps = NULL;
34 win_dc_info->hClipRgn = 0;
35 win_dc_info->hVisRgn = 0;
36 win_dc_info->hGCClipRgn = 0;
37 win_dc_info->hPen = STOCK_BLACK_PEN;
38 win_dc_info->hBrush = STOCK_WHITE_BRUSH;
39 win_dc_info->hFont = STOCK_SYSTEM_FONT;
40 win_dc_info->hBitmap = 0;
41 win_dc_info->hFirstBitmap = 0;
42 win_dc_info->hDevice = 0;
43 win_dc_info->hPalette = STOCK_DEFAULT_PALETTE;
44 win_dc_info->ROPmode = R2_COPYPEN;
45 win_dc_info->polyFillMode = ALTERNATE;
46 win_dc_info->stretchBltMode = BLACKONWHITE;
47 win_dc_info->relAbsMode = ABSOLUTE;
48 win_dc_info->backgroundMode = OPAQUE;
49 win_dc_info->backgroundColor = RGB( 255, 255, 255 );
50 win_dc_info->textColor = RGB( 0, 0, 0 );
51 win_dc_info->brushOrgX = 0;
52 win_dc_info->brushOrgY = 0;
53 win_dc_info->textAlign = TA_LEFT | TA_TOP | TA_NOUPDATECP;
54 win_dc_info->charExtra = 0;
55 win_dc_info->breakTotalExtra = 0;
56 win_dc_info->breakCount = 0;
57 win_dc_info->breakExtra = 0;
58 win_dc_info->breakRem = 0;
59 win_dc_info->bitsPerPixel = 1;
60 win_dc_info->MapMode = MM_TEXT;
61 win_dc_info->GraphicsMode = GM_COMPATIBLE;
62 win_dc_info->DCOrgX = 0;
63 win_dc_info->DCOrgY = 0;
64 win_dc_info->lpfnPrint = NULL;
65 win_dc_info->CursPosX = 0;
66 win_dc_info->CursPosY = 0;
67 win_dc_info->ArcDirection = AD_COUNTERCLOCKWISE;
68 win_dc_info->xformWorld2Wnd.eM11 = 1.0f;
69 win_dc_info->xformWorld2Wnd.eM12 = 0.0f;
70 win_dc_info->xformWorld2Wnd.eM21 = 0.0f;
71 win_dc_info->xformWorld2Wnd.eM22 = 1.0f;
72 win_dc_info->xformWorld2Wnd.eDx = 0.0f;
73 win_dc_info->xformWorld2Wnd.eDy = 0.0f;
74 win_dc_info->xformWorld2Vport = win_dc_info->xformWorld2Wnd;
75 win_dc_info->xformVport2World = win_dc_info->xformWorld2Wnd;
76 win_dc_info->vport2WorldValid = TRUE;
78 PATH_InitGdiPath(&win_dc_info->path);
82 /***********************************************************************
83 * DC_AllocDC
85 DC *DC_AllocDC( const DC_FUNCTIONS *funcs )
87 HDC16 hdc;
88 DC *dc;
90 if (!(hdc = GDI_AllocObject( sizeof(DC), DC_MAGIC ))) return NULL;
91 dc = (DC *) GDI_HEAP_LOCK( hdc );
93 dc->hSelf = hdc;
94 dc->funcs = funcs;
95 dc->physDev = NULL;
96 dc->saveLevel = 0;
97 dc->dwHookData = 0L;
98 dc->hookProc = NULL;
99 dc->wndOrgX = 0;
100 dc->wndOrgY = 0;
101 dc->wndExtX = 1;
102 dc->wndExtY = 1;
103 dc->vportOrgX = 0;
104 dc->vportOrgY = 0;
105 dc->vportExtX = 1;
106 dc->vportExtY = 1;
108 DC_Init_DC_INFO( &dc->w );
110 return dc;
115 /***********************************************************************
116 * DC_GetDCPtr
118 DC *DC_GetDCPtr( HDC hdc )
120 GDIOBJHDR *ptr = (GDIOBJHDR *)GDI_HEAP_LOCK( hdc );
121 if (!ptr) return NULL;
122 if ((ptr->wMagic == DC_MAGIC) || (ptr->wMagic == METAFILE_DC_MAGIC) ||
123 (ptr->wMagic == ENHMETAFILE_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_GetDCPtr( hdc );
411 if (!dc) return 0;
413 if(dc->funcs->pSaveDC)
414 return dc->funcs->pSaveDC( dc );
416 if (!(hdcs = GetDCState16( hdc )))
418 GDI_HEAP_UNLOCK( hdc );
419 return 0;
421 dcs = (DC *) GDI_HEAP_LOCK( hdcs );
423 /* Copy path. The reason why path saving / restoring is in SaveDC/
424 * RestoreDC and not in GetDCState/SetDCState is that the ...DCState
425 * functions are only in Win16 (which doesn't have paths) and that
426 * SetDCState doesn't allow us to signal an error (which can happen
427 * when copying paths).
429 if (!PATH_AssignGdiPath( &dcs->w.path, &dc->w.path ))
431 GDI_HEAP_UNLOCK( hdc );
432 GDI_HEAP_UNLOCK( hdcs );
433 DeleteDC( hdcs );
434 return 0;
437 dcs->header.hNext = dc->header.hNext;
438 dc->header.hNext = hdcs;
439 TRACE(dc, "(%04x): returning %d\n", hdc, dc->saveLevel+1 );
440 ret = ++dc->saveLevel;
441 GDI_HEAP_UNLOCK( hdcs );
442 GDI_HEAP_UNLOCK( hdc );
443 return ret;
447 /***********************************************************************
448 * RestoreDC16 (GDI.39)
450 BOOL16 WINAPI RestoreDC16( HDC16 hdc, INT16 level )
452 return RestoreDC( hdc, level );
456 /***********************************************************************
457 * RestoreDC32 (GDI32.290)
459 BOOL WINAPI RestoreDC( HDC hdc, INT level )
461 DC * dc, * dcs;
462 BOOL success;
464 TRACE(dc, "%04x %d\n", hdc, level );
465 dc = DC_GetDCPtr( hdc );
466 if(!dc) return FALSE;
467 if(dc->funcs->pRestoreDC)
468 return dc->funcs->pRestoreDC( dc, level );
470 if (level == -1) level = dc->saveLevel;
471 if ((level < 1) || (level > dc->saveLevel))
473 GDI_HEAP_UNLOCK( hdc );
474 return FALSE;
477 success=TRUE;
478 while (dc->saveLevel >= level)
480 HDC16 hdcs = dc->header.hNext;
481 if (!(dcs = (DC *) GDI_GetObjPtr( hdcs, DC_MAGIC )))
483 GDI_HEAP_UNLOCK( hdc );
484 return FALSE;
486 dc->header.hNext = dcs->header.hNext;
487 if (--dc->saveLevel < level)
489 SetDCState16( hdc, hdcs );
490 if (!PATH_AssignGdiPath( &dc->w.path, &dcs->w.path ))
491 /* FIXME: This might not be quite right, since we're
492 * returning FALSE but still destroying the saved DC state */
493 success=FALSE;
495 DeleteDC( hdcs );
497 GDI_HEAP_UNLOCK( hdc );
498 return success;
502 /***********************************************************************
503 * CreateDC16 (GDI.53)
505 HDC16 WINAPI CreateDC16( LPCSTR driver, LPCSTR device, LPCSTR output,
506 const DEVMODE16 *initData )
508 DC * dc;
509 const DC_FUNCTIONS *funcs;
511 if (!(funcs = DRIVER_FindDriver( driver ))) return 0;
512 if (!(dc = DC_AllocDC( funcs ))) return 0;
513 dc->w.flags = 0;
515 TRACE(dc, "(driver=%s, device=%s, output=%s): returning %04x\n",
516 debugstr_a(driver), debugstr_a(device), debugstr_a(output), dc->hSelf );
518 if (dc->funcs->pCreateDC &&
519 !dc->funcs->pCreateDC( dc, driver, device, output, initData ))
521 WARN(dc, "creation aborted by device\n" );
522 GDI_HEAP_FREE( dc->hSelf );
523 return 0;
526 DC_InitDC( dc );
527 GDI_HEAP_UNLOCK( dc->hSelf );
528 return dc->hSelf;
532 /***********************************************************************
533 * CreateDC32A (GDI32.)
535 HDC WINAPI CreateDCA( LPCSTR driver, LPCSTR device, LPCSTR output,
536 const DEVMODEA *initData )
538 return CreateDC16( driver, device, output, (const DEVMODE16 *)initData );
542 /***********************************************************************
543 * CreateDC32W (GDI32.)
545 HDC WINAPI CreateDCW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
546 const DEVMODEW *initData )
548 LPSTR driverA = HEAP_strdupWtoA( GetProcessHeap(), 0, driver );
549 LPSTR deviceA = HEAP_strdupWtoA( GetProcessHeap(), 0, device );
550 LPSTR outputA = HEAP_strdupWtoA( GetProcessHeap(), 0, output );
551 HDC res = CreateDC16( driverA, deviceA, outputA,
552 (const DEVMODE16 *)initData /*FIXME*/ );
553 HeapFree( GetProcessHeap(), 0, driverA );
554 HeapFree( GetProcessHeap(), 0, deviceA );
555 HeapFree( GetProcessHeap(), 0, outputA );
556 return res;
560 /***********************************************************************
561 * CreateIC16 (GDI.153)
563 HDC16 WINAPI CreateIC16( LPCSTR driver, LPCSTR device, LPCSTR output,
564 const DEVMODE16* initData )
566 /* Nothing special yet for ICs */
567 return CreateDC16( driver, device, output, initData );
571 /***********************************************************************
572 * CreateIC32A (GDI32.49)
574 HDC WINAPI CreateICA( LPCSTR driver, LPCSTR device, LPCSTR output,
575 const DEVMODEA* initData )
577 /* Nothing special yet for ICs */
578 return CreateDCA( driver, device, output, initData );
582 /***********************************************************************
583 * CreateIC32W (GDI32.50)
585 HDC WINAPI CreateICW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
586 const DEVMODEW* initData )
588 /* Nothing special yet for ICs */
589 return CreateDCW( driver, device, output, initData );
593 /***********************************************************************
594 * CreateCompatibleDC16 (GDI.52)
596 HDC16 WINAPI CreateCompatibleDC16( HDC16 hdc )
598 return (HDC16)CreateCompatibleDC( hdc );
602 /***********************************************************************
603 * CreateCompatibleDC32 (GDI32.31)
605 HDC WINAPI CreateCompatibleDC( HDC hdc )
607 DC *dc, *origDC;
608 HBITMAP hbitmap;
609 const DC_FUNCTIONS *funcs;
611 if ((origDC = (DC *)GDI_GetObjPtr( hdc, DC_MAGIC ))) funcs = origDC->funcs;
612 else funcs = DRIVER_FindDriver( "DISPLAY" );
613 if (!funcs) return 0;
615 if (!(dc = DC_AllocDC( funcs ))) return 0;
617 TRACE(dc, "(%04x): returning %04x\n",
618 hdc, dc->hSelf );
620 /* Create default bitmap */
621 if (!(hbitmap = CreateBitmap( 1, 1, 1, 1, NULL )))
623 GDI_HEAP_FREE( dc->hSelf );
624 return 0;
626 dc->w.flags = DC_MEMORY;
627 dc->w.bitsPerPixel = 1;
628 dc->w.hBitmap = hbitmap;
629 dc->w.hFirstBitmap = hbitmap;
631 if (dc->funcs->pCreateDC &&
632 !dc->funcs->pCreateDC( dc, NULL, NULL, NULL, NULL ))
634 WARN(dc, "creation aborted by device\n");
635 DeleteObject( hbitmap );
636 GDI_HEAP_FREE( dc->hSelf );
637 return 0;
640 DC_InitDC( dc );
641 GDI_HEAP_UNLOCK( dc->hSelf );
642 return dc->hSelf;
646 /***********************************************************************
647 * DeleteDC16 (GDI.68)
649 BOOL16 WINAPI DeleteDC16( HDC16 hdc )
651 return DeleteDC( hdc );
655 /***********************************************************************
656 * DeleteDC32 (GDI32.67)
658 BOOL WINAPI DeleteDC( HDC hdc )
660 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
661 if (!dc) return FALSE;
663 TRACE(dc, "%04x\n", hdc );
665 /* Call hook procedure to check whether is it OK to delete this DC */
666 if ( dc->hookProc && !(dc->w.flags & (DC_SAVED | DC_MEMORY))
667 && dc->hookProc( hdc, DCHC_DELETEDC, dc->dwHookData, 0 ) == FALSE )
669 GDI_HEAP_UNLOCK( hdc );
670 return FALSE;
673 while (dc->saveLevel)
675 DC * dcs;
676 HDC16 hdcs = dc->header.hNext;
677 if (!(dcs = (DC *) GDI_GetObjPtr( hdcs, DC_MAGIC ))) break;
678 dc->header.hNext = dcs->header.hNext;
679 dc->saveLevel--;
680 DeleteDC( hdcs );
683 if (!(dc->w.flags & DC_SAVED))
685 SelectObject( hdc, STOCK_BLACK_PEN );
686 SelectObject( hdc, STOCK_WHITE_BRUSH );
687 SelectObject( hdc, STOCK_SYSTEM_FONT );
688 if (dc->w.flags & DC_MEMORY) DeleteObject( dc->w.hFirstBitmap );
689 if (dc->funcs->pDeleteDC) dc->funcs->pDeleteDC(dc);
692 if (dc->w.hClipRgn) DeleteObject( dc->w.hClipRgn );
693 if (dc->w.hVisRgn) DeleteObject( dc->w.hVisRgn );
694 if (dc->w.hGCClipRgn) DeleteObject( dc->w.hGCClipRgn );
696 PATH_DestroyGdiPath(&dc->w.path);
698 return GDI_FreeObject( hdc );
702 /***********************************************************************
703 * ResetDC16 (GDI.376)
705 HDC16 WINAPI ResetDC16( HDC16 hdc, const DEVMODE16 *devmode )
707 FIXME(dc, "stub\n" );
708 return hdc;
712 /***********************************************************************
713 * ResetDC32A (GDI32.287)
715 HDC WINAPI ResetDCA( HDC hdc, const DEVMODEA *devmode )
717 FIXME(dc, "stub\n" );
718 return hdc;
722 /***********************************************************************
723 * ResetDC32W (GDI32.288)
725 HDC WINAPI ResetDCW( HDC hdc, const DEVMODEW *devmode )
727 FIXME(dc, "stub\n" );
728 return hdc;
732 /***********************************************************************
733 * GetDeviceCaps16 (GDI.80)
735 INT16 WINAPI GetDeviceCaps16( HDC16 hdc, INT16 cap )
737 return GetDeviceCaps( hdc, cap );
741 /***********************************************************************
742 * GetDeviceCaps32 (GDI32.171)
744 INT WINAPI GetDeviceCaps( HDC hdc, INT cap )
746 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
747 INT ret;
749 if (!dc) return 0;
751 if ((cap < 0) || (cap > sizeof(DeviceCaps)-sizeof(WORD)))
753 GDI_HEAP_UNLOCK( hdc );
754 return 0;
757 TRACE(dc, "(%04x,%d): returning %d\n",
758 hdc, cap, *(WORD *)(((char *)dc->w.devCaps) + cap) );
759 ret = *(WORD *)(((char *)dc->w.devCaps) + cap);
760 GDI_HEAP_UNLOCK( hdc );
761 return ret;
765 /***********************************************************************
766 * SetBkColor16 (GDI.1)
768 COLORREF WINAPI SetBkColor16( HDC16 hdc, COLORREF color )
770 return SetBkColor( hdc, color );
774 /***********************************************************************
775 * SetBkColor32 (GDI32.305)
777 COLORREF WINAPI SetBkColor( HDC hdc, COLORREF color )
779 COLORREF oldColor;
780 DC * dc = DC_GetDCPtr( hdc );
782 if (!dc) return 0x80000000;
783 if (dc->funcs->pSetBkColor)
784 oldColor = dc->funcs->pSetBkColor(dc, color);
785 else {
786 oldColor = dc->w.backgroundColor;
787 dc->w.backgroundColor = color;
789 GDI_HEAP_UNLOCK( hdc );
790 return oldColor;
794 /***********************************************************************
795 * SetTextColor16 (GDI.9)
797 COLORREF WINAPI SetTextColor16( HDC16 hdc, COLORREF color )
799 return SetTextColor( hdc, color );
803 /***********************************************************************
804 * SetTextColor32 (GDI32.338)
806 COLORREF WINAPI SetTextColor( HDC hdc, COLORREF color )
808 COLORREF oldColor;
809 DC * dc = DC_GetDCPtr( hdc );
811 if (!dc) return 0x80000000;
812 if (dc->funcs->pSetTextColor)
813 oldColor = dc->funcs->pSetTextColor(dc, color);
814 else {
815 oldColor = dc->w.textColor;
816 dc->w.textColor = color;
818 GDI_HEAP_UNLOCK( hdc );
819 return oldColor;
822 /***********************************************************************
823 * SetTextAlign16 (GDI.346)
825 UINT16 WINAPI SetTextAlign16( HDC16 hdc, UINT16 align )
827 return SetTextAlign( hdc, align );
831 /***********************************************************************
832 * SetTextAlign (GDI32.336)
834 UINT WINAPI SetTextAlign( HDC hdc, UINT align )
836 UINT prevAlign;
837 DC *dc = DC_GetDCPtr( hdc );
838 if (!dc) return 0x0;
839 if (dc->funcs->pSetTextAlign)
840 prevAlign = dc->funcs->pSetTextAlign(dc, align);
841 else {
842 prevAlign = dc->w.textAlign;
843 dc->w.textAlign = align;
845 GDI_HEAP_UNLOCK( hdc );
846 return prevAlign;
849 /***********************************************************************
850 * GetDCOrgEx (GDI32.168)
852 BOOL WINAPI GetDCOrgEx( HDC hDC, LPPOINT lpp )
854 DC * dc;
856 if (!lpp) return FALSE;
857 if (!(dc = (DC *) GDI_GetObjPtr( hDC, DC_MAGIC ))) return FALSE;
859 #ifndef X_DISPLAY_MISSING
860 if (!(dc->w.flags & DC_MEMORY))
862 X11DRV_PDEVICE *physDev;
863 Window root;
864 int w, h, border, depth;
866 physDev = (X11DRV_PDEVICE *) dc->physDev;
868 /* FIXME: this is not correct for managed windows */
869 TSXGetGeometry( display, physDev->drawable, &root,
870 (int*)&lpp->x, (int*)&lpp->y, &w, &h, &border, &depth );
872 else
873 #endif /* !defined(X_DISPLAY_MISSING) */
874 lpp->x = lpp->y = 0;
876 lpp->x += dc->w.DCOrgX; lpp->y += dc->w.DCOrgY;
877 GDI_HEAP_UNLOCK( hDC );
878 return TRUE;
882 /***********************************************************************
883 * GetDCOrg (GDI.79)
885 DWORD WINAPI GetDCOrg16( HDC16 hdc )
887 POINT pt;
888 if( GetDCOrgEx( hdc, &pt) )
889 return MAKELONG( (WORD)pt.x, (WORD)pt.y );
890 return 0;
894 /***********************************************************************
895 * SetDCOrg (GDI.117)
897 DWORD WINAPI SetDCOrg16( HDC16 hdc, INT16 x, INT16 y )
899 DWORD prevOrg;
900 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
901 if (!dc) return 0;
902 prevOrg = dc->w.DCOrgX | (dc->w.DCOrgY << 16);
903 dc->w.DCOrgX = x;
904 dc->w.DCOrgY = y;
905 GDI_HEAP_UNLOCK( hdc );
906 return prevOrg;
910 /***********************************************************************
911 * GetGraphicsMode (GDI32.188)
913 INT WINAPI GetGraphicsMode( HDC hdc )
915 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
916 if (!dc) return 0;
917 return dc->w.GraphicsMode;
921 /***********************************************************************
922 * SetGraphicsMode (GDI32.317)
924 INT WINAPI SetGraphicsMode( HDC hdc, INT mode )
926 INT ret;
927 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
929 /* One would think that setting the graphics mode to GM_COMPATIBLE
930 * would also reset the world transformation matrix to the unity
931 * matrix. However, in Windows, this is not the case. This doesn't
932 * make a lot of sense to me, but that's the way it is.
935 if (!dc) return 0;
936 if ((mode <= 0) || (mode > GM_LAST)) return 0;
937 ret = dc->w.GraphicsMode;
938 dc->w.GraphicsMode = mode;
939 return ret;
943 /***********************************************************************
944 * GetArcDirection16 (GDI.524)
946 INT16 WINAPI GetArcDirection16( HDC16 hdc )
948 return GetArcDirection( (HDC)hdc );
952 /***********************************************************************
953 * GetArcDirection32 (GDI32.141)
955 INT WINAPI GetArcDirection( HDC hdc )
957 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
959 if (!dc)
960 return 0;
962 return dc->w.ArcDirection;
966 /***********************************************************************
967 * SetArcDirection16 (GDI.525)
969 INT16 WINAPI SetArcDirection16( HDC16 hdc, INT16 nDirection )
971 return SetArcDirection( (HDC)hdc, (INT)nDirection );
975 /***********************************************************************
976 * SetArcDirection32 (GDI32.302)
978 INT WINAPI SetArcDirection( HDC hdc, INT nDirection )
980 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
981 INT nOldDirection;
983 if (!dc)
984 return 0;
986 if (nDirection!=AD_COUNTERCLOCKWISE && nDirection!=AD_CLOCKWISE)
988 SetLastError(ERROR_INVALID_PARAMETER);
989 return 0;
992 nOldDirection = dc->w.ArcDirection;
993 dc->w.ArcDirection = nDirection;
995 return nOldDirection;
999 /***********************************************************************
1000 * GetWorldTransform (GDI32.244)
1002 BOOL WINAPI GetWorldTransform( HDC hdc, LPXFORM xform )
1004 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1006 if (!dc)
1007 return FALSE;
1008 if (!xform)
1009 return FALSE;
1011 *xform = dc->w.xformWorld2Wnd;
1013 return TRUE;
1017 /***********************************************************************
1018 * SetWorldTransform (GDI32.346)
1020 BOOL WINAPI SetWorldTransform( HDC hdc, const XFORM *xform )
1022 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1024 if (!dc)
1026 SetLastError( ERROR_INVALID_HANDLE );
1027 return FALSE;
1030 if (!xform)
1031 return FALSE;
1033 /* Check that graphics mode is GM_ADVANCED */
1034 if (dc->w.GraphicsMode!=GM_ADVANCED)
1035 return FALSE;
1037 dc->w.xformWorld2Wnd = *xform;
1039 DC_UpdateXforms( dc );
1041 return TRUE;
1045 /****************************************************************************
1046 * ModifyWorldTransform [GDI32.253]
1047 * Modifies the world transformation for a device context.
1049 * PARAMS
1050 * hdc [I] Handle to device context
1051 * xform [I] XFORM structure that will be used to modify the world
1052 * transformation
1053 * iMode [I] Specifies in what way to modify the world transformation
1054 * Possible values:
1055 * MWT_IDENTITY
1056 * Resets the world transformation to the identity matrix.
1057 * The parameter xform is ignored.
1058 * MWT_LEFTMULTIPLY
1059 * Multiplies xform into the world transformation matrix from
1060 * the left.
1061 * MWT_RIGHTMULTIPLY
1062 * Multiplies xform into the world transformation matrix from
1063 * the right.
1065 * RETURNS STD
1067 BOOL WINAPI ModifyWorldTransform( HDC hdc, const XFORM *xform,
1068 DWORD iMode )
1070 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1072 /* Check for illegal parameters */
1073 if (!dc)
1075 SetLastError( ERROR_INVALID_HANDLE );
1076 return FALSE;
1078 if (!xform)
1079 return FALSE;
1081 /* Check that graphics mode is GM_ADVANCED */
1082 if (dc->w.GraphicsMode!=GM_ADVANCED)
1083 return FALSE;
1085 switch (iMode)
1087 case MWT_IDENTITY:
1088 dc->w.xformWorld2Wnd.eM11 = 1.0f;
1089 dc->w.xformWorld2Wnd.eM12 = 0.0f;
1090 dc->w.xformWorld2Wnd.eM21 = 0.0f;
1091 dc->w.xformWorld2Wnd.eM22 = 1.0f;
1092 dc->w.xformWorld2Wnd.eDx = 0.0f;
1093 dc->w.xformWorld2Wnd.eDy = 0.0f;
1094 break;
1095 case MWT_LEFTMULTIPLY:
1096 CombineTransform( &dc->w.xformWorld2Wnd, xform,
1097 &dc->w.xformWorld2Wnd );
1098 break;
1099 case MWT_RIGHTMULTIPLY:
1100 CombineTransform( &dc->w.xformWorld2Wnd, &dc->w.xformWorld2Wnd,
1101 xform );
1102 break;
1103 default:
1104 return FALSE;
1107 DC_UpdateXforms( dc );
1109 return TRUE;
1113 /****************************************************************************
1114 * CombineTransform [GDI32.20]
1115 * Combines two transformation matrices.
1117 * PARAMS
1118 * xformResult [O] Stores the result of combining the two matrices
1119 * xform1 [I] Specifies the first matrix to apply
1120 * xform2 [I] Specifies the second matrix to apply
1122 * REMARKS
1123 * The same matrix can be passed in for more than one of the parameters.
1125 * RETURNS STD
1127 BOOL WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
1128 const XFORM *xform2 )
1130 XFORM xformTemp;
1132 /* Check for illegal parameters */
1133 if (!xformResult || !xform1 || !xform2)
1134 return FALSE;
1136 /* Create the result in a temporary XFORM, since xformResult may be
1137 * equal to xform1 or xform2 */
1138 xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
1139 xform1->eM12 * xform2->eM21;
1140 xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
1141 xform1->eM12 * xform2->eM22;
1142 xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
1143 xform1->eM22 * xform2->eM21;
1144 xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
1145 xform1->eM22 * xform2->eM22;
1146 xformTemp.eDx = xform1->eDx * xform2->eM11 +
1147 xform1->eDy * xform2->eM21 +
1148 xform2->eDx;
1149 xformTemp.eDy = xform1->eDx * xform2->eM12 +
1150 xform1->eDy * xform2->eM22 +
1151 xform2->eDy;
1153 /* Copy the result to xformResult */
1154 *xformResult = xformTemp;
1156 return TRUE;
1160 /***********************************************************************
1161 * SetDCHook (GDI.190)
1163 BOOL16 WINAPI SetDCHook( HDC16 hdc, FARPROC16 hookProc, DWORD dwHookData )
1165 DC *dc = (DC *)GDI_GetObjPtr( hdc, DC_MAGIC );
1167 TRACE(dc, "hookProc %08x, default is %08x\n",
1168 (UINT)hookProc, (UINT)DCHook16 );
1170 if (!dc) return FALSE;
1171 dc->hookProc = hookProc;
1172 dc->dwHookData = dwHookData;
1173 GDI_HEAP_UNLOCK( hdc );
1174 return TRUE;
1178 /***********************************************************************
1179 * GetDCHook (GDI.191)
1181 DWORD WINAPI GetDCHook( HDC16 hdc, FARPROC16 *phookProc )
1183 DC *dc = (DC *)GDI_GetObjPtr( hdc, DC_MAGIC );
1184 if (!dc) return 0;
1185 *phookProc = dc->hookProc;
1186 GDI_HEAP_UNLOCK( hdc );
1187 return dc->dwHookData;
1191 /***********************************************************************
1192 * SetHookFlags (GDI.192)
1194 WORD WINAPI SetHookFlags16(HDC16 hDC, WORD flags)
1196 DC* dc = (DC*)GDI_GetObjPtr( hDC, DC_MAGIC );
1198 if( dc )
1200 WORD wRet = dc->w.flags & DC_DIRTY;
1202 /* "Undocumented Windows" info is slightly confusing.
1205 TRACE(dc,"hDC %04x, flags %04x\n",hDC,flags);
1207 if( flags & DCHF_INVALIDATEVISRGN )
1208 dc->w.flags |= DC_DIRTY;
1209 else if( flags & DCHF_VALIDATEVISRGN || !flags )
1210 dc->w.flags &= ~DC_DIRTY;
1211 GDI_HEAP_UNLOCK( hDC );
1212 return wRet;
1214 return 0;
1217 /***********************************************************************
1218 * SetICMMode (GDI32.318)
1220 INT WINAPI SetICMMode(HDC hdc, INT iEnableICM)
1222 /*FIXME Asuming that ICM is always off, and cannot be turned on */
1223 if (iEnableICM == ICM_OFF) return ICM_OFF;
1224 if (iEnableICM == ICM_ON) return 0;
1225 if (iEnableICM == ICM_QUERY) return ICM_OFF;
1226 return 0;
1230 /***********************************************************************
1231 * GetColorSpace (GDI32.165)
1233 HCOLORSPACE WINAPI GetColorSpace(HDC hdc)
1235 /*FIXME Need to to whatever GetColorSpace actually does */
1236 return 0;
1239 /***********************************************************************
1240 * GetBoundsRect16 (GDI.194)
1242 UINT16 WINAPI GetBoundsRect16(HDC16 hdc, LPRECT16 rect, UINT16 flags)
1244 return DCB_RESET | DCB_DISABLE; /* bounding rectangle always empty and disabled*/
1247 /***********************************************************************
1248 * GetBoundsRect32 (GDI32.147)
1250 UINT WINAPI GetBoundsRect(HDC hdc, LPRECT rect, UINT flags)
1252 FIXME(dc, "(): stub\n");
1253 return DCB_RESET; /* bounding rectangle always empty */
1256 /***********************************************************************
1257 * SetBoundsRect16 (GDI.193)
1259 UINT16 WINAPI SetBoundsRect16(HDC16 hdc, const RECT16* rect, UINT16 flags)
1261 if ( (flags & DCB_ACCUMULATE) || (flags & DCB_ENABLE) )
1262 FIXME( dc, "(%04x, %p, %04x): stub\n", hdc, rect, flags );
1264 return DCB_RESET | DCB_DISABLE; /* bounding rectangle always empty and disabled*/
1267 /***********************************************************************
1268 * SetBoundsRect32 (GDI32.307)
1270 UINT WINAPI SetBoundsRect(HDC hdc, const RECT* rect, UINT flags)
1272 FIXME(dc, "(): stub\n");
1273 return DCB_DISABLE; /* bounding rectangle always empty */
1276 /***********************************************************************
1277 * Death (GDI.121)
1279 * Disables GDI, switches back to text mode.
1280 * We don't have to do anything here,
1281 * just let console support handle everything
1283 void WINAPI Death16(HDC16 hDC)
1285 MSG("Death(%04x) called. Application enters text mode...\n", hDC);
1288 /***********************************************************************
1289 * Resurrection (GDI.122)
1291 * Restores GDI functionality
1293 void WINAPI Resurrection16(HDC16 hDC,
1294 WORD w1, WORD w2, WORD w3, WORD w4, WORD w5, WORD w6)
1296 MSG("Resurrection(%04x, %04x, %04x, %04x, %04x, %04x, %04x) called. Application left text mode.\n", hDC, w1, w2, w3, w4, w5, w6);