Added stub for WIN32S16.EXP1 (most likely LoadPeResource16).
[wine/dcerpc.git] / objects / dc.c
blobc53cf75ad56aa436994b8ed03ab4f7cfc1af1cab
1 /*
2 * GDI Device Context functions
4 * Copyright 1993 Alexandre Julliard
6 */
8 #include "config.h"
10 #ifndef X_DISPLAY_MISSING
11 #include "ts_xlib.h"
12 #include "x11drv.h"
13 #endif /* !defined(X_DISPLAY_MISSING) */
15 #include <stdlib.h>
16 #include <string.h>
17 #include "dc.h"
18 #include "gdi.h"
19 #include "heap.h"
20 #include "debugtools.h"
21 #include "font.h"
22 #include "winerror.h"
23 #include "wine/winuser16.h"
25 DEFAULT_DEBUG_CHANNEL(dc)
27 /***********************************************************************
28 * DC_Init_DC_INFO
30 * Fill the WIN_DC_INFO structure.
32 static void DC_Init_DC_INFO( WIN_DC_INFO *win_dc_info )
34 win_dc_info->flags = 0;
35 win_dc_info->devCaps = NULL;
36 win_dc_info->hClipRgn = 0;
37 win_dc_info->hVisRgn = 0;
38 win_dc_info->hGCClipRgn = 0;
39 win_dc_info->hPen = STOCK_BLACK_PEN;
40 win_dc_info->hBrush = STOCK_WHITE_BRUSH;
41 win_dc_info->hFont = STOCK_SYSTEM_FONT;
42 win_dc_info->hBitmap = 0;
43 win_dc_info->hFirstBitmap = 0;
44 win_dc_info->hDevice = 0;
45 win_dc_info->hPalette = STOCK_DEFAULT_PALETTE;
46 win_dc_info->ROPmode = R2_COPYPEN;
47 win_dc_info->polyFillMode = ALTERNATE;
48 win_dc_info->stretchBltMode = BLACKONWHITE;
49 win_dc_info->relAbsMode = ABSOLUTE;
50 win_dc_info->backgroundMode = OPAQUE;
51 win_dc_info->backgroundColor = RGB( 255, 255, 255 );
52 win_dc_info->textColor = RGB( 0, 0, 0 );
53 win_dc_info->brushOrgX = 0;
54 win_dc_info->brushOrgY = 0;
55 win_dc_info->textAlign = TA_LEFT | TA_TOP | TA_NOUPDATECP;
56 win_dc_info->charExtra = 0;
57 win_dc_info->breakTotalExtra = 0;
58 win_dc_info->breakCount = 0;
59 win_dc_info->breakExtra = 0;
60 win_dc_info->breakRem = 0;
61 win_dc_info->bitsPerPixel = 1;
62 win_dc_info->MapMode = MM_TEXT;
63 win_dc_info->GraphicsMode = GM_COMPATIBLE;
64 win_dc_info->DCOrgX = 0;
65 win_dc_info->DCOrgY = 0;
66 win_dc_info->spfnPrint = 0;
67 win_dc_info->lpfnPrint = NULL;
68 win_dc_info->CursPosX = 0;
69 win_dc_info->CursPosY = 0;
70 win_dc_info->ArcDirection = AD_COUNTERCLOCKWISE;
71 win_dc_info->xformWorld2Wnd.eM11 = 1.0f;
72 win_dc_info->xformWorld2Wnd.eM12 = 0.0f;
73 win_dc_info->xformWorld2Wnd.eM21 = 0.0f;
74 win_dc_info->xformWorld2Wnd.eM22 = 1.0f;
75 win_dc_info->xformWorld2Wnd.eDx = 0.0f;
76 win_dc_info->xformWorld2Wnd.eDy = 0.0f;
77 win_dc_info->xformWorld2Vport = win_dc_info->xformWorld2Wnd;
78 win_dc_info->xformVport2World = win_dc_info->xformWorld2Wnd;
79 win_dc_info->vport2WorldValid = TRUE;
81 PATH_InitGdiPath(&win_dc_info->path);
85 /***********************************************************************
86 * DC_AllocDC
88 DC *DC_AllocDC( const DC_FUNCTIONS *funcs )
90 HDC16 hdc;
91 DC *dc;
93 if (!(hdc = GDI_AllocObject( sizeof(DC), DC_MAGIC ))) return NULL;
94 dc = (DC *) GDI_HEAP_LOCK( hdc );
96 dc->hSelf = hdc;
97 dc->funcs = funcs;
98 dc->physDev = NULL;
99 dc->saveLevel = 0;
100 dc->dwHookData = 0L;
101 dc->hookProc = NULL;
102 dc->wndOrgX = 0;
103 dc->wndOrgY = 0;
104 dc->wndExtX = 1;
105 dc->wndExtY = 1;
106 dc->vportOrgX = 0;
107 dc->vportOrgY = 0;
108 dc->vportExtX = 1;
109 dc->vportExtY = 1;
111 DC_Init_DC_INFO( &dc->w );
113 return dc;
118 /***********************************************************************
119 * DC_GetDCPtr
121 DC *DC_GetDCPtr( HDC hdc )
123 GDIOBJHDR *ptr = (GDIOBJHDR *)GDI_HEAP_LOCK( hdc );
124 if (!ptr) return NULL;
125 if ((ptr->wMagic == DC_MAGIC) || (ptr->wMagic == METAFILE_DC_MAGIC) ||
126 (ptr->wMagic == ENHMETAFILE_DC_MAGIC))
127 return (DC *)ptr;
128 GDI_HEAP_UNLOCK( hdc );
129 return NULL;
133 /***********************************************************************
134 * DC_InitDC
136 * Setup device-specific DC values for a newly created DC.
138 void DC_InitDC( DC* dc )
140 RealizeDefaultPalette16( dc->hSelf );
141 SetTextColor( dc->hSelf, dc->w.textColor );
142 SetBkColor( dc->hSelf, dc->w.backgroundColor );
143 SelectObject( dc->hSelf, dc->w.hPen );
144 SelectObject( dc->hSelf, dc->w.hBrush );
145 SelectObject( dc->hSelf, dc->w.hFont );
146 CLIPPING_UpdateGCRegion( dc );
150 /***********************************************************************
151 * DC_InvertXform
153 * Computes the inverse of the transformation xformSrc and stores it to
154 * xformDest. Returns TRUE if successful or FALSE if the xformSrc matrix
155 * is singular.
157 static BOOL DC_InvertXform( const XFORM *xformSrc, XFORM *xformDest )
159 FLOAT determinant;
161 determinant = xformSrc->eM11*xformSrc->eM22 -
162 xformSrc->eM12*xformSrc->eM21;
163 if (determinant > -1e-12 && determinant < 1e-12)
164 return FALSE;
166 xformDest->eM11 = xformSrc->eM22 / determinant;
167 xformDest->eM12 = -xformSrc->eM12 / determinant;
168 xformDest->eM21 = -xformSrc->eM21 / determinant;
169 xformDest->eM22 = xformSrc->eM11 / determinant;
170 xformDest->eDx = -xformSrc->eDx * xformDest->eM11 -
171 xformSrc->eDy * xformDest->eM21;
172 xformDest->eDy = -xformSrc->eDx * xformDest->eM12 -
173 xformSrc->eDy * xformDest->eM22;
175 return TRUE;
179 /***********************************************************************
180 * DC_UpdateXforms
182 * Updates the xformWorld2Vport, xformVport2World and vport2WorldValid
183 * fields of the specified DC by creating a transformation that
184 * represents the current mapping mode and combining it with the DC's
185 * world transform. This function should be called whenever the
186 * parameters associated with the mapping mode (window and viewport
187 * extents and origins) or the world transform change.
189 void DC_UpdateXforms( DC *dc )
191 XFORM xformWnd2Vport;
192 FLOAT scaleX, scaleY;
194 /* Construct a transformation to do the window-to-viewport conversion */
195 scaleX = (FLOAT)dc->vportExtX / (FLOAT)dc->wndExtX;
196 scaleY = (FLOAT)dc->vportExtY / (FLOAT)dc->wndExtY;
197 xformWnd2Vport.eM11 = scaleX;
198 xformWnd2Vport.eM12 = 0.0;
199 xformWnd2Vport.eM21 = 0.0;
200 xformWnd2Vport.eM22 = scaleY;
201 xformWnd2Vport.eDx = (FLOAT)dc->vportOrgX -
202 scaleX * (FLOAT)dc->wndOrgX;
203 xformWnd2Vport.eDy = (FLOAT)dc->vportOrgY -
204 scaleY * (FLOAT)dc->wndOrgY;
206 /* Combine with the world transformation */
207 CombineTransform( &dc->w.xformWorld2Vport, &dc->w.xformWorld2Wnd,
208 &xformWnd2Vport );
210 /* Create inverse of world-to-viewport transformation */
211 dc->w.vport2WorldValid = DC_InvertXform( &dc->w.xformWorld2Vport,
212 &dc->w.xformVport2World );
216 /***********************************************************************
217 * GetDCState (GDI.179)
219 HDC16 WINAPI GetDCState16( HDC16 hdc )
221 DC * newdc, * dc;
222 HGDIOBJ16 handle;
224 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
225 if (!(handle = GDI_AllocObject( sizeof(DC), DC_MAGIC )))
227 GDI_HEAP_UNLOCK( hdc );
228 return 0;
230 newdc = (DC *) GDI_HEAP_LOCK( handle );
232 TRACE("(%04x): returning %04x\n", hdc, handle );
234 newdc->w.flags = dc->w.flags | DC_SAVED;
235 newdc->w.devCaps = dc->w.devCaps;
236 newdc->w.hPen = dc->w.hPen;
237 newdc->w.hBrush = dc->w.hBrush;
238 newdc->w.hFont = dc->w.hFont;
239 newdc->w.hBitmap = dc->w.hBitmap;
240 newdc->w.hFirstBitmap = dc->w.hFirstBitmap;
241 newdc->w.hDevice = dc->w.hDevice;
242 newdc->w.hPalette = dc->w.hPalette;
243 newdc->w.totalExtent = dc->w.totalExtent;
244 newdc->w.bitsPerPixel = dc->w.bitsPerPixel;
245 newdc->w.ROPmode = dc->w.ROPmode;
246 newdc->w.polyFillMode = dc->w.polyFillMode;
247 newdc->w.stretchBltMode = dc->w.stretchBltMode;
248 newdc->w.relAbsMode = dc->w.relAbsMode;
249 newdc->w.backgroundMode = dc->w.backgroundMode;
250 newdc->w.backgroundColor = dc->w.backgroundColor;
251 newdc->w.textColor = dc->w.textColor;
252 newdc->w.brushOrgX = dc->w.brushOrgX;
253 newdc->w.brushOrgY = dc->w.brushOrgY;
254 newdc->w.textAlign = dc->w.textAlign;
255 newdc->w.charExtra = dc->w.charExtra;
256 newdc->w.breakTotalExtra = dc->w.breakTotalExtra;
257 newdc->w.breakCount = dc->w.breakCount;
258 newdc->w.breakExtra = dc->w.breakExtra;
259 newdc->w.breakRem = dc->w.breakRem;
260 newdc->w.MapMode = dc->w.MapMode;
261 newdc->w.GraphicsMode = dc->w.GraphicsMode;
262 #if 0
263 /* Apparently, the DC origin is not changed by [GS]etDCState */
264 newdc->w.DCOrgX = dc->w.DCOrgX;
265 newdc->w.DCOrgY = dc->w.DCOrgY;
266 #endif
267 newdc->w.CursPosX = dc->w.CursPosX;
268 newdc->w.CursPosY = dc->w.CursPosY;
269 newdc->w.ArcDirection = dc->w.ArcDirection;
270 newdc->w.xformWorld2Wnd = dc->w.xformWorld2Wnd;
271 newdc->w.xformWorld2Vport = dc->w.xformWorld2Vport;
272 newdc->w.xformVport2World = dc->w.xformVport2World;
273 newdc->w.vport2WorldValid = dc->w.vport2WorldValid;
274 newdc->wndOrgX = dc->wndOrgX;
275 newdc->wndOrgY = dc->wndOrgY;
276 newdc->wndExtX = dc->wndExtX;
277 newdc->wndExtY = dc->wndExtY;
278 newdc->vportOrgX = dc->vportOrgX;
279 newdc->vportOrgY = dc->vportOrgY;
280 newdc->vportExtX = dc->vportExtX;
281 newdc->vportExtY = dc->vportExtY;
283 newdc->hSelf = (HDC)handle;
284 newdc->saveLevel = 0;
286 PATH_InitGdiPath( &newdc->w.path );
288 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
290 newdc->w.hGCClipRgn = newdc->w.hVisRgn = 0;
291 if (dc->w.hClipRgn)
293 newdc->w.hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
294 CombineRgn( newdc->w.hClipRgn, dc->w.hClipRgn, 0, RGN_COPY );
296 else
297 newdc->w.hClipRgn = 0;
298 GDI_HEAP_UNLOCK( handle );
299 GDI_HEAP_UNLOCK( hdc );
300 return handle;
304 /***********************************************************************
305 * SetDCState (GDI.180)
307 void WINAPI SetDCState16( HDC16 hdc, HDC16 hdcs )
309 DC *dc, *dcs;
311 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return;
312 if (!(dcs = (DC *) GDI_GetObjPtr( hdcs, DC_MAGIC )))
314 GDI_HEAP_UNLOCK( hdc );
315 return;
317 if (!dcs->w.flags & DC_SAVED)
319 GDI_HEAP_UNLOCK( hdc );
320 GDI_HEAP_UNLOCK( hdcs );
321 return;
323 TRACE("%04x %04x\n", hdc, hdcs );
325 dc->w.flags = dcs->w.flags & ~DC_SAVED;
326 dc->w.devCaps = dcs->w.devCaps;
327 dc->w.hFirstBitmap = dcs->w.hFirstBitmap;
328 dc->w.hDevice = dcs->w.hDevice;
329 dc->w.totalExtent = dcs->w.totalExtent;
330 dc->w.ROPmode = dcs->w.ROPmode;
331 dc->w.polyFillMode = dcs->w.polyFillMode;
332 dc->w.stretchBltMode = dcs->w.stretchBltMode;
333 dc->w.relAbsMode = dcs->w.relAbsMode;
334 dc->w.backgroundMode = dcs->w.backgroundMode;
335 dc->w.backgroundColor = dcs->w.backgroundColor;
336 dc->w.textColor = dcs->w.textColor;
337 dc->w.brushOrgX = dcs->w.brushOrgX;
338 dc->w.brushOrgY = dcs->w.brushOrgY;
339 dc->w.textAlign = dcs->w.textAlign;
340 dc->w.charExtra = dcs->w.charExtra;
341 dc->w.breakTotalExtra = dcs->w.breakTotalExtra;
342 dc->w.breakCount = dcs->w.breakCount;
343 dc->w.breakExtra = dcs->w.breakExtra;
344 dc->w.breakRem = dcs->w.breakRem;
345 dc->w.MapMode = dcs->w.MapMode;
346 dc->w.GraphicsMode = dcs->w.GraphicsMode;
347 #if 0
348 /* Apparently, the DC origin is not changed by [GS]etDCState */
349 dc->w.DCOrgX = dcs->w.DCOrgX;
350 dc->w.DCOrgY = dcs->w.DCOrgY;
351 #endif
352 dc->w.CursPosX = dcs->w.CursPosX;
353 dc->w.CursPosY = dcs->w.CursPosY;
354 dc->w.ArcDirection = dcs->w.ArcDirection;
355 dc->w.xformWorld2Wnd = dcs->w.xformWorld2Wnd;
356 dc->w.xformWorld2Vport = dcs->w.xformWorld2Vport;
357 dc->w.xformVport2World = dcs->w.xformVport2World;
358 dc->w.vport2WorldValid = dcs->w.vport2WorldValid;
360 dc->wndOrgX = dcs->wndOrgX;
361 dc->wndOrgY = dcs->wndOrgY;
362 dc->wndExtX = dcs->wndExtX;
363 dc->wndExtY = dcs->wndExtY;
364 dc->vportOrgX = dcs->vportOrgX;
365 dc->vportOrgY = dcs->vportOrgY;
366 dc->vportExtX = dcs->vportExtX;
367 dc->vportExtY = dcs->vportExtY;
369 if (!(dc->w.flags & DC_MEMORY)) dc->w.bitsPerPixel = dcs->w.bitsPerPixel;
371 if (dcs->w.hClipRgn)
373 if (!dc->w.hClipRgn) dc->w.hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
374 CombineRgn( dc->w.hClipRgn, dcs->w.hClipRgn, 0, RGN_COPY );
376 else
378 if (dc->w.hClipRgn) DeleteObject16( dc->w.hClipRgn );
379 dc->w.hClipRgn = 0;
381 CLIPPING_UpdateGCRegion( dc );
383 SelectObject( hdc, dcs->w.hBitmap );
384 SelectObject( hdc, dcs->w.hBrush );
385 SelectObject( hdc, dcs->w.hFont );
386 SelectObject( hdc, dcs->w.hPen );
387 SetBkColor( hdc, dcs->w.backgroundColor);
388 SetTextColor( hdc, dcs->w.textColor);
389 GDISelectPalette16( hdc, dcs->w.hPalette, FALSE );
390 GDI_HEAP_UNLOCK( hdc );
391 GDI_HEAP_UNLOCK( hdcs );
395 /***********************************************************************
396 * SaveDC16 (GDI.30)
398 INT16 WINAPI SaveDC16( HDC16 hdc )
400 return (INT16)SaveDC( hdc );
404 /***********************************************************************
405 * SaveDC32 (GDI32.292)
407 INT WINAPI SaveDC( HDC hdc )
409 HDC hdcs;
410 DC * dc, * dcs;
411 INT ret;
413 dc = DC_GetDCPtr( hdc );
414 if (!dc) return 0;
416 if(dc->funcs->pSaveDC)
417 return dc->funcs->pSaveDC( dc );
419 if (!(hdcs = GetDCState16( hdc )))
421 GDI_HEAP_UNLOCK( hdc );
422 return 0;
424 dcs = (DC *) GDI_HEAP_LOCK( hdcs );
426 /* Copy path. The reason why path saving / restoring is in SaveDC/
427 * RestoreDC and not in GetDCState/SetDCState is that the ...DCState
428 * functions are only in Win16 (which doesn't have paths) and that
429 * SetDCState doesn't allow us to signal an error (which can happen
430 * when copying paths).
432 if (!PATH_AssignGdiPath( &dcs->w.path, &dc->w.path ))
434 GDI_HEAP_UNLOCK( hdc );
435 GDI_HEAP_UNLOCK( hdcs );
436 DeleteDC( hdcs );
437 return 0;
440 dcs->header.hNext = dc->header.hNext;
441 dc->header.hNext = hdcs;
442 TRACE("(%04x): returning %d\n", hdc, dc->saveLevel+1 );
443 ret = ++dc->saveLevel;
444 GDI_HEAP_UNLOCK( hdcs );
445 GDI_HEAP_UNLOCK( hdc );
446 return ret;
450 /***********************************************************************
451 * RestoreDC16 (GDI.39)
453 BOOL16 WINAPI RestoreDC16( HDC16 hdc, INT16 level )
455 return RestoreDC( hdc, level );
459 /***********************************************************************
460 * RestoreDC32 (GDI32.290)
462 BOOL WINAPI RestoreDC( HDC hdc, INT level )
464 DC * dc, * dcs;
465 BOOL success;
467 TRACE("%04x %d\n", hdc, level );
468 dc = DC_GetDCPtr( hdc );
469 if(!dc) return FALSE;
470 if(dc->funcs->pRestoreDC)
471 return dc->funcs->pRestoreDC( dc, level );
473 if (level == -1) level = dc->saveLevel;
474 if ((level < 1) || (level > dc->saveLevel))
476 GDI_HEAP_UNLOCK( hdc );
477 return FALSE;
480 success=TRUE;
481 while (dc->saveLevel >= level)
483 HDC16 hdcs = dc->header.hNext;
484 if (!(dcs = (DC *) GDI_GetObjPtr( hdcs, DC_MAGIC )))
486 GDI_HEAP_UNLOCK( hdc );
487 return FALSE;
489 dc->header.hNext = dcs->header.hNext;
490 if (--dc->saveLevel < level)
492 SetDCState16( hdc, hdcs );
493 if (!PATH_AssignGdiPath( &dc->w.path, &dcs->w.path ))
494 /* FIXME: This might not be quite right, since we're
495 * returning FALSE but still destroying the saved DC state */
496 success=FALSE;
498 DeleteDC( hdcs );
500 GDI_HEAP_UNLOCK( hdc );
501 return success;
505 /***********************************************************************
506 * CreateDC16 (GDI.53)
508 HDC16 WINAPI CreateDC16( LPCSTR driver, LPCSTR device, LPCSTR output,
509 const DEVMODEA *initData )
511 DC * dc;
512 const DC_FUNCTIONS *funcs;
513 char buf[300];
515 if (device) {
516 if(!DRIVER_GetDriverName( device, buf, sizeof(buf) )) return 0;
517 } else
518 strcpy(buf, driver);
520 if (!(funcs = DRIVER_FindDriver( buf ))) return 0;
521 if (!(dc = DC_AllocDC( funcs ))) return 0;
522 dc->w.flags = 0;
524 TRACE("(driver=%s, device=%s, output=%s): returning %04x\n",
525 debugstr_a(driver), debugstr_a(device), debugstr_a(output), dc->hSelf );
527 if (dc->funcs->pCreateDC &&
528 !dc->funcs->pCreateDC( dc, driver, device, output, initData ))
530 WARN("creation aborted by device\n" );
531 GDI_HEAP_FREE( dc->hSelf );
532 return 0;
535 DC_InitDC( dc );
536 GDI_HEAP_UNLOCK( dc->hSelf );
537 return dc->hSelf;
541 /***********************************************************************
542 * CreateDC32A (GDI32.)
544 HDC WINAPI CreateDCA( LPCSTR driver, LPCSTR device, LPCSTR output,
545 const DEVMODEA *initData )
547 return CreateDC16( driver, device, output, (const DEVMODEA *)initData );
551 /***********************************************************************
552 * CreateDC32W (GDI32.)
554 HDC WINAPI CreateDCW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
555 const DEVMODEW *initData )
557 LPSTR driverA = HEAP_strdupWtoA( GetProcessHeap(), 0, driver );
558 LPSTR deviceA = HEAP_strdupWtoA( GetProcessHeap(), 0, device );
559 LPSTR outputA = HEAP_strdupWtoA( GetProcessHeap(), 0, output );
560 HDC res = CreateDC16( driverA, deviceA, outputA,
561 (const DEVMODEA *)initData /*FIXME*/ );
562 HeapFree( GetProcessHeap(), 0, driverA );
563 HeapFree( GetProcessHeap(), 0, deviceA );
564 HeapFree( GetProcessHeap(), 0, outputA );
565 return res;
569 /***********************************************************************
570 * CreateIC16 (GDI.153)
572 HDC16 WINAPI CreateIC16( LPCSTR driver, LPCSTR device, LPCSTR output,
573 const DEVMODEA* initData )
575 /* Nothing special yet for ICs */
576 return CreateDC16( driver, device, output, initData );
580 /***********************************************************************
581 * CreateIC32A (GDI32.49)
583 HDC WINAPI CreateICA( LPCSTR driver, LPCSTR device, LPCSTR output,
584 const DEVMODEA* initData )
586 /* Nothing special yet for ICs */
587 return CreateDCA( driver, device, output, initData );
591 /***********************************************************************
592 * CreateIC32W (GDI32.50)
594 HDC WINAPI CreateICW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
595 const DEVMODEW* initData )
597 /* Nothing special yet for ICs */
598 return CreateDCW( driver, device, output, initData );
602 /***********************************************************************
603 * CreateCompatibleDC16 (GDI.52)
605 HDC16 WINAPI CreateCompatibleDC16( HDC16 hdc )
607 return (HDC16)CreateCompatibleDC( hdc );
611 /***********************************************************************
612 * CreateCompatibleDC32 (GDI32.31)
614 HDC WINAPI CreateCompatibleDC( HDC hdc )
616 DC *dc, *origDC;
617 HBITMAP hbitmap;
618 const DC_FUNCTIONS *funcs;
620 if ((origDC = (DC *)GDI_GetObjPtr( hdc, DC_MAGIC ))) funcs = origDC->funcs;
621 else funcs = DRIVER_FindDriver( "DISPLAY" );
622 if (!funcs) return 0;
624 if (!(dc = DC_AllocDC( funcs ))) return 0;
626 TRACE("(%04x): returning %04x\n",
627 hdc, dc->hSelf );
629 /* Create default bitmap */
630 if (!(hbitmap = CreateBitmap( 1, 1, 1, 1, NULL )))
632 GDI_HEAP_FREE( dc->hSelf );
633 return 0;
635 dc->w.flags = DC_MEMORY;
636 dc->w.bitsPerPixel = 1;
637 dc->w.hBitmap = hbitmap;
638 dc->w.hFirstBitmap = hbitmap;
640 /* Copy the driver-specific physical device info into
641 * the new DC. The driver may use this read-only info
642 * while creating the compatible DC below. */
643 if (origDC)
644 dc->physDev = origDC->physDev;
646 if (dc->funcs->pCreateDC &&
647 !dc->funcs->pCreateDC( dc, NULL, NULL, NULL, NULL ))
649 WARN("creation aborted by device\n");
650 DeleteObject( hbitmap );
651 GDI_HEAP_FREE( dc->hSelf );
652 return 0;
655 DC_InitDC( dc );
656 GDI_HEAP_UNLOCK( dc->hSelf );
657 return dc->hSelf;
661 /***********************************************************************
662 * DeleteDC16 (GDI.68)
664 BOOL16 WINAPI DeleteDC16( HDC16 hdc )
666 return DeleteDC( hdc );
670 /***********************************************************************
671 * DeleteDC32 (GDI32.67)
673 BOOL WINAPI DeleteDC( HDC hdc )
675 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
676 if (!dc) return FALSE;
678 TRACE("%04x\n", hdc );
680 /* Call hook procedure to check whether is it OK to delete this DC */
681 if ( dc->hookProc && !(dc->w.flags & (DC_SAVED | DC_MEMORY))
682 && dc->hookProc( hdc, DCHC_DELETEDC, dc->dwHookData, 0 ) == FALSE )
684 GDI_HEAP_UNLOCK( hdc );
685 return FALSE;
688 while (dc->saveLevel)
690 DC * dcs;
691 HDC16 hdcs = dc->header.hNext;
692 if (!(dcs = (DC *) GDI_GetObjPtr( hdcs, DC_MAGIC ))) break;
693 dc->header.hNext = dcs->header.hNext;
694 dc->saveLevel--;
695 DeleteDC( hdcs );
698 if (!(dc->w.flags & DC_SAVED))
700 SelectObject( hdc, STOCK_BLACK_PEN );
701 SelectObject( hdc, STOCK_WHITE_BRUSH );
702 SelectObject( hdc, STOCK_SYSTEM_FONT );
703 if (dc->w.flags & DC_MEMORY) DeleteObject( dc->w.hFirstBitmap );
704 if (dc->funcs->pDeleteDC) dc->funcs->pDeleteDC(dc);
707 if (dc->w.hClipRgn) DeleteObject( dc->w.hClipRgn );
708 if (dc->w.hVisRgn) DeleteObject( dc->w.hVisRgn );
709 if (dc->w.hGCClipRgn) DeleteObject( dc->w.hGCClipRgn );
711 PATH_DestroyGdiPath(&dc->w.path);
713 return GDI_FreeObject( hdc );
717 /***********************************************************************
718 * ResetDC16 (GDI.376)
720 HDC16 WINAPI ResetDC16( HDC16 hdc, const DEVMODEA *devmode )
722 FIXME("stub\n" );
723 return hdc;
727 /***********************************************************************
728 * ResetDC32A (GDI32.287)
730 HDC WINAPI ResetDCA( HDC hdc, const DEVMODEA *devmode )
732 FIXME("stub\n" );
733 return hdc;
737 /***********************************************************************
738 * ResetDC32W (GDI32.288)
740 HDC WINAPI ResetDCW( HDC hdc, const DEVMODEW *devmode )
742 FIXME("stub\n" );
743 return hdc;
747 /***********************************************************************
748 * GetDeviceCaps16 (GDI.80)
750 INT16 WINAPI GetDeviceCaps16( HDC16 hdc, INT16 cap )
752 return GetDeviceCaps( hdc, cap );
756 /***********************************************************************
757 * GetDeviceCaps32 (GDI32.171)
759 INT WINAPI GetDeviceCaps( HDC hdc, INT cap )
761 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
762 INT ret;
763 POINT pt;
765 if (!dc) return 0;
767 /* Device capabilities for the printer */
768 switch (cap)
770 case PHYSICALWIDTH:
771 if(Escape(hdc, GETPHYSPAGESIZE, 0, NULL, (LPVOID)&pt) > 0)
772 return pt.x;
773 case PHYSICALHEIGHT:
774 if(Escape(hdc, GETPHYSPAGESIZE, 0, NULL, (LPVOID)&pt) > 0)
775 return pt.y;
776 case PHYSICALOFFSETX:
777 if(Escape(hdc, GETPRINTINGOFFSET, 0, NULL, (LPVOID)&pt) > 0)
778 return pt.x;
779 case PHYSICALOFFSETY:
780 if(Escape(hdc, GETPRINTINGOFFSET, 0, NULL, (LPVOID)&pt) > 0)
781 return pt.y;
782 case SCALINGFACTORX:
783 if(Escape(hdc, GETSCALINGFACTOR, 0, NULL, (LPVOID)&pt) > 0)
784 return pt.x;
785 case SCALINGFACTORY:
786 if(Escape(hdc, GETSCALINGFACTOR, 0, NULL, (LPVOID)&pt) > 0)
787 return pt.y;
790 if ((cap < 0) || (cap > sizeof(DeviceCaps)-sizeof(WORD)))
792 GDI_HEAP_UNLOCK( hdc );
793 return 0;
796 TRACE("(%04x,%d): returning %d\n",
797 hdc, cap, *(WORD *)(((char *)dc->w.devCaps) + cap) );
798 ret = *(WORD *)(((char *)dc->w.devCaps) + cap);
799 GDI_HEAP_UNLOCK( hdc );
800 return ret;
804 /***********************************************************************
805 * SetBkColor16 (GDI.1)
807 COLORREF WINAPI SetBkColor16( HDC16 hdc, COLORREF color )
809 return SetBkColor( hdc, color );
813 /***********************************************************************
814 * SetBkColor32 (GDI32.305)
816 COLORREF WINAPI SetBkColor( HDC hdc, COLORREF color )
818 COLORREF oldColor;
819 DC * dc = DC_GetDCPtr( hdc );
821 if (!dc) return 0x80000000;
822 if (dc->funcs->pSetBkColor)
823 oldColor = dc->funcs->pSetBkColor(dc, color);
824 else {
825 oldColor = dc->w.backgroundColor;
826 dc->w.backgroundColor = color;
828 GDI_HEAP_UNLOCK( hdc );
829 return oldColor;
833 /***********************************************************************
834 * SetTextColor16 (GDI.9)
836 COLORREF WINAPI SetTextColor16( HDC16 hdc, COLORREF color )
838 return SetTextColor( hdc, color );
842 /***********************************************************************
843 * SetTextColor32 (GDI32.338)
845 COLORREF WINAPI SetTextColor( HDC hdc, COLORREF color )
847 COLORREF oldColor;
848 DC * dc = DC_GetDCPtr( hdc );
850 if (!dc) return 0x80000000;
851 if (dc->funcs->pSetTextColor)
852 oldColor = dc->funcs->pSetTextColor(dc, color);
853 else {
854 oldColor = dc->w.textColor;
855 dc->w.textColor = color;
857 GDI_HEAP_UNLOCK( hdc );
858 return oldColor;
861 /***********************************************************************
862 * SetTextAlign16 (GDI.346)
864 UINT16 WINAPI SetTextAlign16( HDC16 hdc, UINT16 align )
866 return SetTextAlign( hdc, align );
870 /***********************************************************************
871 * SetTextAlign (GDI32.336)
873 UINT WINAPI SetTextAlign( HDC hdc, UINT align )
875 UINT prevAlign;
876 DC *dc = DC_GetDCPtr( hdc );
877 if (!dc) return 0x0;
878 if (dc->funcs->pSetTextAlign)
879 prevAlign = dc->funcs->pSetTextAlign(dc, align);
880 else {
881 prevAlign = dc->w.textAlign;
882 dc->w.textAlign = align;
884 GDI_HEAP_UNLOCK( hdc );
885 return prevAlign;
888 /***********************************************************************
889 * GetDCOrgEx (GDI32.168)
891 BOOL WINAPI GetDCOrgEx( HDC hDC, LPPOINT lpp )
893 DC * dc;
895 if (!lpp) return FALSE;
896 if (!(dc = (DC *) GDI_GetObjPtr( hDC, DC_MAGIC ))) return FALSE;
898 #ifndef X_DISPLAY_MISSING
899 if (!(dc->w.flags & DC_MEMORY))
901 X11DRV_PDEVICE *physDev;
902 Window root;
903 int w, h, border, depth;
905 physDev = (X11DRV_PDEVICE *) dc->physDev;
907 /* FIXME: this is not correct for managed windows */
908 TSXGetGeometry( display, physDev->drawable, &root,
909 (int*)&lpp->x, (int*)&lpp->y, &w, &h, &border, &depth );
911 else
912 #endif /* !defined(X_DISPLAY_MISSING) */
913 lpp->x = lpp->y = 0;
915 lpp->x += dc->w.DCOrgX; lpp->y += dc->w.DCOrgY;
916 GDI_HEAP_UNLOCK( hDC );
917 return TRUE;
921 /***********************************************************************
922 * GetDCOrg (GDI.79)
924 DWORD WINAPI GetDCOrg16( HDC16 hdc )
926 POINT pt;
927 if( GetDCOrgEx( hdc, &pt) )
928 return MAKELONG( (WORD)pt.x, (WORD)pt.y );
929 return 0;
933 /***********************************************************************
934 * SetDCOrg (GDI.117)
936 DWORD WINAPI SetDCOrg16( HDC16 hdc, INT16 x, INT16 y )
938 DWORD prevOrg;
939 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
940 if (!dc) return 0;
941 prevOrg = dc->w.DCOrgX | (dc->w.DCOrgY << 16);
942 dc->w.DCOrgX = x;
943 dc->w.DCOrgY = y;
944 GDI_HEAP_UNLOCK( hdc );
945 return prevOrg;
949 /***********************************************************************
950 * GetGraphicsMode (GDI32.188)
952 INT WINAPI GetGraphicsMode( HDC hdc )
954 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
955 if (!dc) return 0;
956 return dc->w.GraphicsMode;
960 /***********************************************************************
961 * SetGraphicsMode (GDI32.317)
963 INT WINAPI SetGraphicsMode( HDC hdc, INT mode )
965 INT ret;
966 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
968 /* One would think that setting the graphics mode to GM_COMPATIBLE
969 * would also reset the world transformation matrix to the unity
970 * matrix. However, in Windows, this is not the case. This doesn't
971 * make a lot of sense to me, but that's the way it is.
974 if (!dc) return 0;
975 if ((mode <= 0) || (mode > GM_LAST)) return 0;
976 ret = dc->w.GraphicsMode;
977 dc->w.GraphicsMode = mode;
978 return ret;
982 /***********************************************************************
983 * GetArcDirection16 (GDI.524)
985 INT16 WINAPI GetArcDirection16( HDC16 hdc )
987 return GetArcDirection( (HDC)hdc );
991 /***********************************************************************
992 * GetArcDirection32 (GDI32.141)
994 INT WINAPI GetArcDirection( HDC hdc )
996 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
998 if (!dc)
999 return 0;
1001 return dc->w.ArcDirection;
1005 /***********************************************************************
1006 * SetArcDirection16 (GDI.525)
1008 INT16 WINAPI SetArcDirection16( HDC16 hdc, INT16 nDirection )
1010 return SetArcDirection( (HDC)hdc, (INT)nDirection );
1014 /***********************************************************************
1015 * SetArcDirection32 (GDI32.302)
1017 INT WINAPI SetArcDirection( HDC hdc, INT nDirection )
1019 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1020 INT nOldDirection;
1022 if (!dc)
1023 return 0;
1025 if (nDirection!=AD_COUNTERCLOCKWISE && nDirection!=AD_CLOCKWISE)
1027 SetLastError(ERROR_INVALID_PARAMETER);
1028 return 0;
1031 nOldDirection = dc->w.ArcDirection;
1032 dc->w.ArcDirection = nDirection;
1034 return nOldDirection;
1038 /***********************************************************************
1039 * GetWorldTransform (GDI32.244)
1041 BOOL WINAPI GetWorldTransform( HDC hdc, LPXFORM xform )
1043 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1045 if (!dc)
1046 return FALSE;
1047 if (!xform)
1048 return FALSE;
1050 *xform = dc->w.xformWorld2Wnd;
1052 return TRUE;
1056 /***********************************************************************
1057 * SetWorldTransform (GDI32.346)
1059 BOOL WINAPI SetWorldTransform( HDC hdc, const XFORM *xform )
1061 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1063 if (!dc)
1065 SetLastError( ERROR_INVALID_HANDLE );
1066 return FALSE;
1069 if (!xform)
1070 return FALSE;
1072 /* Check that graphics mode is GM_ADVANCED */
1073 if (dc->w.GraphicsMode!=GM_ADVANCED)
1074 return FALSE;
1076 dc->w.xformWorld2Wnd = *xform;
1078 DC_UpdateXforms( dc );
1080 return TRUE;
1084 /****************************************************************************
1085 * ModifyWorldTransform [GDI32.253]
1086 * Modifies the world transformation for a device context.
1088 * PARAMS
1089 * hdc [I] Handle to device context
1090 * xform [I] XFORM structure that will be used to modify the world
1091 * transformation
1092 * iMode [I] Specifies in what way to modify the world transformation
1093 * Possible values:
1094 * MWT_IDENTITY
1095 * Resets the world transformation to the identity matrix.
1096 * The parameter xform is ignored.
1097 * MWT_LEFTMULTIPLY
1098 * Multiplies xform into the world transformation matrix from
1099 * the left.
1100 * MWT_RIGHTMULTIPLY
1101 * Multiplies xform into the world transformation matrix from
1102 * the right.
1104 * RETURNS STD
1106 BOOL WINAPI ModifyWorldTransform( HDC hdc, const XFORM *xform,
1107 DWORD iMode )
1109 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1111 /* Check for illegal parameters */
1112 if (!dc)
1114 SetLastError( ERROR_INVALID_HANDLE );
1115 return FALSE;
1117 if (!xform)
1118 return FALSE;
1120 /* Check that graphics mode is GM_ADVANCED */
1121 if (dc->w.GraphicsMode!=GM_ADVANCED)
1122 return FALSE;
1124 switch (iMode)
1126 case MWT_IDENTITY:
1127 dc->w.xformWorld2Wnd.eM11 = 1.0f;
1128 dc->w.xformWorld2Wnd.eM12 = 0.0f;
1129 dc->w.xformWorld2Wnd.eM21 = 0.0f;
1130 dc->w.xformWorld2Wnd.eM22 = 1.0f;
1131 dc->w.xformWorld2Wnd.eDx = 0.0f;
1132 dc->w.xformWorld2Wnd.eDy = 0.0f;
1133 break;
1134 case MWT_LEFTMULTIPLY:
1135 CombineTransform( &dc->w.xformWorld2Wnd, xform,
1136 &dc->w.xformWorld2Wnd );
1137 break;
1138 case MWT_RIGHTMULTIPLY:
1139 CombineTransform( &dc->w.xformWorld2Wnd, &dc->w.xformWorld2Wnd,
1140 xform );
1141 break;
1142 default:
1143 return FALSE;
1146 DC_UpdateXforms( dc );
1148 return TRUE;
1152 /****************************************************************************
1153 * CombineTransform [GDI32.20]
1154 * Combines two transformation matrices.
1156 * PARAMS
1157 * xformResult [O] Stores the result of combining the two matrices
1158 * xform1 [I] Specifies the first matrix to apply
1159 * xform2 [I] Specifies the second matrix to apply
1161 * REMARKS
1162 * The same matrix can be passed in for more than one of the parameters.
1164 * RETURNS STD
1166 BOOL WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
1167 const XFORM *xform2 )
1169 XFORM xformTemp;
1171 /* Check for illegal parameters */
1172 if (!xformResult || !xform1 || !xform2)
1173 return FALSE;
1175 /* Create the result in a temporary XFORM, since xformResult may be
1176 * equal to xform1 or xform2 */
1177 xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
1178 xform1->eM12 * xform2->eM21;
1179 xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
1180 xform1->eM12 * xform2->eM22;
1181 xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
1182 xform1->eM22 * xform2->eM21;
1183 xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
1184 xform1->eM22 * xform2->eM22;
1185 xformTemp.eDx = xform1->eDx * xform2->eM11 +
1186 xform1->eDy * xform2->eM21 +
1187 xform2->eDx;
1188 xformTemp.eDy = xform1->eDx * xform2->eM12 +
1189 xform1->eDy * xform2->eM22 +
1190 xform2->eDy;
1192 /* Copy the result to xformResult */
1193 *xformResult = xformTemp;
1195 return TRUE;
1199 /***********************************************************************
1200 * SetDCHook (GDI.190)
1202 BOOL16 WINAPI SetDCHook( HDC16 hdc, FARPROC16 hookProc, DWORD dwHookData )
1204 DC *dc = (DC *)GDI_GetObjPtr( hdc, DC_MAGIC );
1206 TRACE("hookProc %08x, default is %08x\n",
1207 (UINT)hookProc, (UINT)DCHook16 );
1209 if (!dc) return FALSE;
1210 dc->hookProc = hookProc;
1211 dc->dwHookData = dwHookData;
1212 GDI_HEAP_UNLOCK( hdc );
1213 return TRUE;
1217 /***********************************************************************
1218 * GetDCHook (GDI.191)
1220 DWORD WINAPI GetDCHook( HDC16 hdc, FARPROC16 *phookProc )
1222 DC *dc = (DC *)GDI_GetObjPtr( hdc, DC_MAGIC );
1223 if (!dc) return 0;
1224 *phookProc = dc->hookProc;
1225 GDI_HEAP_UNLOCK( hdc );
1226 return dc->dwHookData;
1230 /***********************************************************************
1231 * SetHookFlags (GDI.192)
1233 WORD WINAPI SetHookFlags16(HDC16 hDC, WORD flags)
1235 DC* dc = (DC*)GDI_GetObjPtr( hDC, DC_MAGIC );
1237 if( dc )
1239 WORD wRet = dc->w.flags & DC_DIRTY;
1241 /* "Undocumented Windows" info is slightly confusing.
1244 TRACE("hDC %04x, flags %04x\n",hDC,flags);
1246 if( flags & DCHF_INVALIDATEVISRGN )
1247 dc->w.flags |= DC_DIRTY;
1248 else if( flags & DCHF_VALIDATEVISRGN || !flags )
1249 dc->w.flags &= ~DC_DIRTY;
1250 GDI_HEAP_UNLOCK( hDC );
1251 return wRet;
1253 return 0;
1256 /***********************************************************************
1257 * SetICMMode (GDI32.318)
1259 INT WINAPI SetICMMode(HDC hdc, INT iEnableICM)
1261 /*FIXME Asuming that ICM is always off, and cannot be turned on */
1262 if (iEnableICM == ICM_OFF) return ICM_OFF;
1263 if (iEnableICM == ICM_ON) return 0;
1264 if (iEnableICM == ICM_QUERY) return ICM_OFF;
1265 return 0;
1269 /***********************************************************************
1270 * GetColorSpace (GDI32.165)
1272 HCOLORSPACE WINAPI GetColorSpace(HDC hdc)
1274 /*FIXME Need to to whatever GetColorSpace actually does */
1275 return 0;
1278 /***********************************************************************
1279 * GetBoundsRect16 (GDI.194)
1281 UINT16 WINAPI GetBoundsRect16(HDC16 hdc, LPRECT16 rect, UINT16 flags)
1283 return DCB_RESET | DCB_DISABLE; /* bounding rectangle always empty and disabled*/
1286 /***********************************************************************
1287 * GetBoundsRect32 (GDI32.147)
1289 UINT WINAPI GetBoundsRect(HDC hdc, LPRECT rect, UINT flags)
1291 FIXME("(): stub\n");
1292 return DCB_RESET; /* bounding rectangle always empty */
1295 /***********************************************************************
1296 * SetBoundsRect16 (GDI.193)
1298 UINT16 WINAPI SetBoundsRect16(HDC16 hdc, const RECT16* rect, UINT16 flags)
1300 if ( (flags & DCB_ACCUMULATE) || (flags & DCB_ENABLE) )
1301 FIXME("(%04x, %p, %04x): stub\n", hdc, rect, flags );
1303 return DCB_RESET | DCB_DISABLE; /* bounding rectangle always empty and disabled*/
1306 /***********************************************************************
1307 * SetBoundsRect32 (GDI32.307)
1309 UINT WINAPI SetBoundsRect(HDC hdc, const RECT* rect, UINT flags)
1311 FIXME("(): stub\n");
1312 return DCB_DISABLE; /* bounding rectangle always empty */
1315 /***********************************************************************
1316 * Death (GDI.121)
1318 * Disables GDI, switches back to text mode.
1319 * We don't have to do anything here,
1320 * just let console support handle everything
1322 void WINAPI Death16(HDC16 hDC)
1324 MESSAGE("Death(%04x) called. Application enters text mode...\n", hDC);
1327 /***********************************************************************
1328 * Resurrection (GDI.122)
1330 * Restores GDI functionality
1332 void WINAPI Resurrection16(HDC16 hDC,
1333 WORD w1, WORD w2, WORD w3, WORD w4, WORD w5, WORD w6)
1335 MESSAGE("Resurrection(%04x, %04x, %04x, %04x, %04x, %04x, %04x) called. Application left text mode.\n", hDC, w1, w2, w3, w4, w5, w6);