apply 46848 from yarotows
[reactos.git] / reactos / 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 <win32k.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, pdc->eboText.pbrush, 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, pdc->eboBackground.pbrush, 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 if(psurfBmp->hSecure)
301 {
302 /* Set DIBSECTION attribute */
303 pdcattr->ulDirty_ |= DC_DIBSECTION;
304 }
305 else
306 {
307 pdcattr->ulDirty_ &= ~DC_DIBSECTION;
308 }
309
310 /* Release the exclusive lock */
311 SURFACE_UnlockSurface(psurfBmp);
312
313 /* Mark the brushes invalid */
314 pdcattr->ulDirty_ |= DIRTY_FILL | DIRTY_LINE;
315
316 DC_UnlockDc(pDC);
317
318 if (hVisRgn)
319 {
320 GdiSelectVisRgn(hDC, hVisRgn);
321 REGION_FreeRgnByHandle(hVisRgn);
322 }
323
324 return hOrgBmp;
325 }
326
327
328 BOOL
329 APIENTRY
330 NtGdiSelectClipPath(
331 HDC hDC,
332 int Mode)
333 {
334 HRGN hrgnPath;
335 PPATH pPath;
336 BOOL success = FALSE;
337 PDC_ATTR pdcattr;
338 PDC pdc;
339
340 pdc = DC_LockDc(hDC);
341 if (!pdc)
342 {
343 SetLastWin32Error(ERROR_INVALID_PARAMETER);
344 return FALSE;
345 }
346 pdcattr = pdc->pdcattr;
347
348 pPath = PATH_LockPath(pdc->dclevel.hPath);
349 if (!pPath)
350 {
351 DC_UnlockDc(pdc);
352 return FALSE;
353 }
354
355 /* Check that path is closed */
356 if (pPath->state != PATH_Closed)
357 {
358 SetLastWin32Error(ERROR_CAN_NOT_COMPLETE);
359 return FALSE;
360 }
361
362 /* Construct a region from the path */
363 else if (PATH_PathToRegion(pPath, pdcattr->jFillMode, &hrgnPath))
364 {
365 success = GdiExtSelectClipRgn(pdc, hrgnPath, Mode) != ERROR;
366 GreDeleteObject( hrgnPath );
367
368 /* Empty the path */
369 if (success)
370 PATH_EmptyPath(pPath);
371
372 /* FIXME: Should this function delete the path even if it failed? */
373 }
374
375 PATH_UnlockPath(pPath);
376 DC_UnlockDc(pdc);
377
378 return success;
379 }
380
381 HANDLE
382 APIENTRY
383 NtGdiGetDCObject(HDC hDC, INT ObjectType)
384 {
385 HGDIOBJ SelObject;
386 DC *pdc;
387 PDC_ATTR pdcattr;
388
389 /* From Wine: GetCurrentObject does not SetLastError() on a null object */
390 if(!hDC) return NULL;
391
392 if(!(pdc = DC_LockDc(hDC)))
393 {
394 SetLastWin32Error(ERROR_INVALID_HANDLE);
395 return NULL;
396 }
397 pdcattr = pdc->pdcattr;
398
399 if (pdcattr->ulDirty_ & (DIRTY_FILL | DC_BRUSH_DIRTY))
400 DC_vUpdateFillBrush(pdc);
401
402 if (pdcattr->ulDirty_ & (DIRTY_LINE | DC_PEN_DIRTY))
403 DC_vUpdateLineBrush(pdc);
404
405 switch(ObjectType)
406 {
407 case GDI_OBJECT_TYPE_EXTPEN:
408 case GDI_OBJECT_TYPE_PEN:
409 SelObject = pdcattr->hpen;
410 break;
411
412 case GDI_OBJECT_TYPE_BRUSH:
413 SelObject = pdcattr->hbrush;
414 break;
415
416 case GDI_OBJECT_TYPE_PALETTE:
417 SelObject = pdc->dclevel.hpal;
418 break;
419
420 case GDI_OBJECT_TYPE_FONT:
421 SelObject = pdcattr->hlfntNew;
422 break;
423
424 case GDI_OBJECT_TYPE_BITMAP:
425 {
426 SURFACE *psurf = pdc->dclevel.pSurface;
427 SelObject = psurf ? psurf->BaseObject.hHmgr : NULL;
428 break;
429 }
430
431 case GDI_OBJECT_TYPE_COLORSPACE:
432 DPRINT1("FIXME: NtGdiGetCurrentObject() ObjectType OBJ_COLORSPACE not supported yet!\n");
433 // SelObject = dc->dclevel.pColorSpace.BaseObject.hHmgr; ?
434 SelObject = NULL;
435 break;
436
437 default:
438 SelObject = NULL;
439 SetLastWin32Error(ERROR_INVALID_PARAMETER);
440 break;
441 }
442
443 DC_UnlockDc(pdc);
444 return SelObject;
445 }
446
447 ULONG
448 APIENTRY
449 NtGdiEnumObjects(
450 IN HDC hdc,
451 IN INT iObjectType,
452 IN ULONG cjBuf,
453 OUT OPTIONAL PVOID pvBuf)
454 {
455 UNIMPLEMENTED;
456 return 0;
457 }
458