[WIN32K]
[reactos.git] / subsystems / win32 / win32k / objects / dcobjs.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * PURPOSE: Functions for creation and destruction of DCs
5 * FILE: subsystem/win32/win32k/objects/dcobjs.c
6 * PROGRAMER: Timo Kreuzer (timo.kreuzer@rectos.org)
7 */
8
9 #include <w32k.h>
10
11 #define NDEBUG
12 #include <debug.h>
13
14 VOID
15 FASTCALL
16 DC_vUpdateFillBrush(PDC pdc)
17 {
18 PDC_ATTR pdcattr = pdc->pdcattr;
19 PBRUSH pbrFill;
20
21 /* Check if the brush handle has changed */
22 if (pdcattr->hbrush != pdc->dclevel.pbrFill->BaseObject.hHmgr)
23 {
24 /* Try to lock the new brush */
25 pbrFill = BRUSH_ShareLockBrush(pdcattr->hbrush);
26 if (pbrFill)
27 {
28 /* Unlock old brush, set new brush */
29 BRUSH_ShareUnlockBrush(pdc->dclevel.pbrFill);
30 pdc->dclevel.pbrFill = pbrFill;
31
32 /* Mark eboFill as dirty */
33 pdcattr->ulDirty_ |= DIRTY_FILL;
34 }
35 else
36 {
37 /* Invalid brush handle, restore old one */
38 pdcattr->hbrush = pdc->dclevel.pbrFill->BaseObject.hHmgr;
39 }
40 }
41
42 /* Check if the EBRUSHOBJ needs update */
43 if (pdcattr->ulDirty_ & DIRTY_FILL)
44 {
45 /* Update eboFill */
46 EBRUSHOBJ_vUpdate(&pdc->eboFill, pdc->dclevel.pbrFill, pdc);
47 }
48
49 /* Check for DC brush */
50 if (pdcattr->hbrush == StockObjects[DC_BRUSH])
51 {
52 /* ROS HACK, should use surf xlate */
53 /* Update the eboFill's solid color */
54 EBRUSHOBJ_vSetSolidBrushColor(&pdc->eboFill, pdcattr->crPenClr);
55 }
56
57 /* Clear flags */
58 pdcattr->ulDirty_ &= ~(DIRTY_FILL | DC_BRUSH_DIRTY);
59 }
60
61 VOID
62 FASTCALL
63 DC_vUpdateLineBrush(PDC pdc)
64 {
65 PDC_ATTR pdcattr = pdc->pdcattr;
66 PBRUSH pbrLine;
67
68 /* Check if the pen handle has changed */
69 if (pdcattr->hpen != pdc->dclevel.pbrLine->BaseObject.hHmgr)
70 {
71 /* Try to lock the new pen */
72 pbrLine = PEN_ShareLockPen(pdcattr->hpen);
73 if (pbrLine)
74 {
75 /* Unlock old brush, set new brush */
76 BRUSH_ShareUnlockBrush(pdc->dclevel.pbrLine);
77 pdc->dclevel.pbrLine = pbrLine;
78
79 /* Mark eboLine as dirty */
80 pdcattr->ulDirty_ |= DIRTY_LINE;
81 }
82 else
83 {
84 /* Invalid pen handle, restore old one */
85 pdcattr->hpen = pdc->dclevel.pbrLine->BaseObject.hHmgr;
86 }
87 }
88
89 /* Check if the EBRUSHOBJ needs update */
90 if (pdcattr->ulDirty_ & DIRTY_LINE)
91 {
92 /* Update eboLine */
93 EBRUSHOBJ_vUpdate(&pdc->eboLine, pdc->dclevel.pbrLine, pdc);
94 }
95
96 /* Check for DC pen */
97 if (pdcattr->hpen == StockObjects[DC_PEN])
98 {
99 /* Update the eboLine's solid color */
100 EBRUSHOBJ_vSetSolidBrushColor(&pdc->eboLine, pdcattr->crPenClr);
101 }
102
103 /* Clear flags */
104 pdcattr->ulDirty_ &= ~(DIRTY_LINE | DC_PEN_DIRTY);
105 }
106
107 VOID
108 FASTCALL
109 DC_vUpdateTextBrush(PDC pdc)
110 {
111 PDC_ATTR pdcattr = pdc->pdcattr;
112
113 if(pdcattr->ulDirty_ & DIRTY_TEXT)
114 EBRUSHOBJ_vUpdate(&pdc->eboText, pbrDefaultBrush, pdc);
115
116 /* Update the eboText's solid color */
117 EBRUSHOBJ_vSetSolidBrushColor(&pdc->eboText, pdcattr->crForegroundClr);
118
119 /* Clear flag */
120 pdcattr->ulDirty_ &= ~DIRTY_TEXT;
121 }
122
123 VOID
124 FASTCALL
125 DC_vUpdateBackgroundBrush(PDC pdc)
126 {
127 PDC_ATTR pdcattr = pdc->pdcattr;
128
129 if(pdcattr->ulDirty_ & DIRTY_BACKGROUND)
130 EBRUSHOBJ_vUpdate(&pdc->eboBackground, pbrDefaultBrush, pdc);
131
132 /* Update the eboBackground's solid color */
133 EBRUSHOBJ_vSetSolidBrushColor(&pdc->eboBackground, pdcattr->crBackgroundClr);
134
135 /* Clear flag */
136 pdcattr->ulDirty_ &= ~DIRTY_BACKGROUND;
137 }
138
139 HPALETTE
140 FASTCALL
141 GdiSelectPalette(
142 HDC hDC,
143 HPALETTE hpal,
144 BOOL ForceBackground)
145 {
146 PDC pdc;
147 HPALETTE oldPal = NULL;
148 PPALETTE ppal;
149
150 // FIXME: mark the palette as a [fore\back]ground pal
151 pdc = DC_LockDc(hDC);
152 if (!pdc)
153 {
154 return NULL;
155 }
156
157 /* Check if this is a valid palette handle */
158 ppal = PALETTE_ShareLockPalette(hpal);
159 if (!ppal)
160 {
161 DC_UnlockDc(pdc);
162 return NULL;
163 }
164
165 /* Is this a valid palette for this depth? */
166 if ((BitsPerFormat(pdc->dclevel.pSurface->SurfObj.iBitmapFormat) <= 8
167 && ppal->Mode == PAL_INDEXED) ||
168 (BitsPerFormat(pdc->dclevel.pSurface->SurfObj.iBitmapFormat) > 8))
169 {
170 /* Get old palette, set new one */
171 oldPal = pdc->dclevel.hpal;
172 pdc->dclevel.hpal = hpal;
173 DC_vSelectPalette(pdc, ppal);
174
175 /* Mark the brushes invalid */
176 pdc->pdcattr->ulDirty_ |= DIRTY_FILL | DIRTY_LINE |
177 DIRTY_BACKGROUND | DIRTY_TEXT;
178 }
179
180 PALETTE_ShareUnlockPalette(ppal);
181 DC_UnlockDc(pdc);
182
183 return oldPal;
184 }
185
186 /*
187 * @implemented
188 */
189 HBRUSH
190 APIENTRY
191 NtGdiSelectBrush(
192 IN HDC hDC,
193 IN HBRUSH hBrush)
194 {
195 PDC pDC;
196 HBRUSH hOrgBrush;
197
198 if (hDC == NULL || hBrush == NULL) return NULL;
199
200 pDC = DC_LockDc(hDC);
201 if (!pDC)
202 {
203 return NULL;
204 }
205
206 /* Simply return the user mode value, without checking */
207 hOrgBrush = pDC->pdcattr->hbrush;
208 pDC->pdcattr->hbrush = hBrush;
209 DC_vUpdateFillBrush(pDC);
210
211 DC_UnlockDc(pDC);
212
213 return hOrgBrush;
214 }
215
216 /*
217 * @implemented
218 */
219 HPEN
220 APIENTRY
221 NtGdiSelectPen(
222 IN HDC hDC,
223 IN HPEN hPen)
224 {
225 PDC pDC;
226 HPEN hOrgPen;
227
228 if (hDC == NULL || hPen == NULL) return NULL;
229
230 pDC = DC_LockDc(hDC);
231 if (!pDC)
232 {
233 return NULL;
234 }
235
236 /* Simply return the user mode value, without checking */
237 hOrgPen = pDC->pdcattr->hpen;
238 pDC->pdcattr->hpen = hPen;
239 DC_vUpdateLineBrush(pDC);
240
241 DC_UnlockDc(pDC);
242
243 return hOrgPen;
244 }
245
246 /*
247 * @implemented
248 */
249 HBITMAP
250 APIENTRY
251 NtGdiSelectBitmap(
252 IN HDC hDC,
253 IN HBITMAP hBmp)
254 {
255 PDC pDC;
256 PDC_ATTR pdcattr;
257 HBITMAP hOrgBmp;
258 PSURFACE psurfBmp, psurfOld;
259 HRGN hVisRgn;
260
261 if (hDC == NULL || hBmp == NULL) return NULL;
262
263 pDC = DC_LockDc(hDC);
264 if (!pDC)
265 {
266 return NULL;
267 }
268 pdcattr = pDC->pdcattr;
269
270 /* must be memory dc to select bitmap */
271 if (pDC->dctype != DC_TYPE_MEMORY)
272 {
273 DC_UnlockDc(pDC);
274 return NULL;
275 }
276
277 psurfBmp = SURFACE_LockSurface(hBmp);
278 if (!psurfBmp)
279 {
280 DC_UnlockDc(pDC);
281 return NULL;
282 }
283
284 /* Get the handle for the old bitmap */
285 psurfOld = pDC->dclevel.pSurface;
286 hOrgBmp = psurfOld ? psurfOld->BaseObject.hHmgr : NULL;
287
288 /* Release the old bitmap, reference the new */
289 DC_vSelectSurface(pDC, psurfBmp);
290
291 // If Info DC this is zero and pSurface is moved to DC->pSurfInfo.
292 psurfBmp->hDC = hDC;
293
294 /* FIXME; improve by using a region without a handle and selecting it */
295 hVisRgn = IntSysCreateRectRgn( 0,
296 0,
297 psurfBmp->SurfObj.sizlBitmap.cx,
298 psurfBmp->SurfObj.sizlBitmap.cy);
299
300 /* Release the exclusive lock */
301 SURFACE_UnlockSurface(psurfBmp);
302
303 /* Mark the brushes invalid */
304 pdcattr->ulDirty_ |= DIRTY_FILL | DIRTY_LINE;
305
306 DC_UnlockDc(pDC);
307
308 if (hVisRgn)
309 {
310 GdiSelectVisRgn(hDC, hVisRgn);
311 REGION_FreeRgnByHandle(hVisRgn);
312 }
313
314 return hOrgBmp;
315 }
316
317
318 BOOL
319 APIENTRY
320 NtGdiSelectClipPath(
321 HDC hDC,
322 int Mode)
323 {
324 HRGN hrgnPath;
325 PPATH pPath;
326 BOOL success = FALSE;
327 PDC_ATTR pdcattr;
328 PDC pdc;
329
330 pdc = DC_LockDc(hDC);
331 if (!pdc)
332 {
333 SetLastWin32Error(ERROR_INVALID_PARAMETER);
334 return FALSE;
335 }
336 pdcattr = pdc->pdcattr;
337
338 pPath = PATH_LockPath(pdc->dclevel.hPath);
339 if (!pPath)
340 {
341 DC_UnlockDc(pdc);
342 return FALSE;
343 }
344
345 /* Check that path is closed */
346 if (pPath->state != PATH_Closed)
347 {
348 SetLastWin32Error(ERROR_CAN_NOT_COMPLETE);
349 DC_UnlockDc(pdc);
350 return FALSE;
351 }
352
353 /* Construct a region from the path */
354 else if (PATH_PathToRegion(pPath, pdcattr->jFillMode, &hrgnPath))
355 {
356 success = GdiExtSelectClipRgn(pdc, hrgnPath, Mode) != ERROR;
357 GreDeleteObject( hrgnPath );
358
359 /* Empty the path */
360 if (success)
361 PATH_EmptyPath(pPath);
362
363 /* FIXME: Should this function delete the path even if it failed? */
364 }
365
366 PATH_UnlockPath(pPath);
367 DC_UnlockDc(pdc);
368
369 return success;
370 }
371
372 HANDLE
373 APIENTRY
374 NtGdiGetDCObject(HDC hDC, INT ObjectType)
375 {
376 HGDIOBJ SelObject;
377 DC *pdc;
378 PDC_ATTR pdcattr;
379
380 /* From Wine: GetCurrentObject does not SetLastError() on a null object */
381 if(!hDC) return NULL;
382
383 if(!(pdc = DC_LockDc(hDC)))
384 {
385 SetLastWin32Error(ERROR_INVALID_HANDLE);
386 return NULL;
387 }
388 pdcattr = pdc->pdcattr;
389
390 if (pdcattr->ulDirty_ & (DIRTY_FILL | DC_BRUSH_DIRTY))
391 DC_vUpdateFillBrush(pdc);
392
393 if (pdcattr->ulDirty_ & (DIRTY_LINE | DC_PEN_DIRTY))
394 DC_vUpdateLineBrush(pdc);
395
396 switch(ObjectType)
397 {
398 case GDI_OBJECT_TYPE_EXTPEN:
399 case GDI_OBJECT_TYPE_PEN:
400 SelObject = pdcattr->hpen;
401 break;
402
403 case GDI_OBJECT_TYPE_BRUSH:
404 SelObject = pdcattr->hbrush;
405 break;
406
407 case GDI_OBJECT_TYPE_PALETTE:
408 SelObject = pdc->dclevel.hpal;
409 break;
410
411 case GDI_OBJECT_TYPE_FONT:
412 SelObject = pdcattr->hlfntNew;
413 break;
414
415 case GDI_OBJECT_TYPE_BITMAP:
416 {
417 SURFACE *psurf = pdc->dclevel.pSurface;
418 SelObject = psurf ? psurf->BaseObject.hHmgr : NULL;
419 break;
420 }
421
422 case GDI_OBJECT_TYPE_COLORSPACE:
423 DPRINT1("FIXME: NtGdiGetCurrentObject() ObjectType OBJ_COLORSPACE not supported yet!\n");
424 // SelObject = dc->dclevel.pColorSpace.BaseObject.hHmgr; ?
425 SelObject = NULL;
426 break;
427
428 default:
429 SelObject = NULL;
430 SetLastWin32Error(ERROR_INVALID_PARAMETER);
431 break;
432 }
433
434 DC_UnlockDc(pdc);
435 return SelObject;
436 }
437
438 ULONG
439 APIENTRY
440 NtGdiEnumObjects(
441 IN HDC hdc,
442 IN INT iObjectType,
443 IN ULONG cjBuf,
444 OUT OPTIONAL PVOID pvBuf)
445 {
446 UNIMPLEMENTED;
447 return 0;
448 }
449