- Sync gdiplus with Wine head
[reactos.git] / reactos / dll / win32 / gdiplus / region.c
1 /*
2 * Copyright (C) 2008 Google (Lei Zhang)
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 */
18
19 #include <stdarg.h>
20
21 #include "windef.h"
22 #include "winbase.h"
23 #include "wingdi.h"
24
25 #include "objbase.h"
26
27 #include "gdiplus.h"
28 #include "gdiplus_private.h"
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
32
33 /**********************************************************
34 *
35 * Data returned by GdipGetRegionData looks something like this:
36 *
37 * struct region_data_header
38 * {
39 * DWORD size; size in bytes of the data - 8.
40 * DWORD magic1; probably a checksum.
41 * DWORD magic2; always seems to be 0xdbc01001 - version?
42 * DWORD num_ops; number of combining ops * 2
43 * };
44 *
45 * Then follows a sequence of combining ops and region elements.
46 *
47 * A region element is either a RECTF or some path data.
48 *
49 * Combining ops are just stored as their CombineMode value.
50 *
51 * Each RECTF is preceded by the DWORD 0x10000000. An empty rect is
52 * stored as 0x10000002 (with no following RECTF) and an infinite rect
53 * is stored as 0x10000003 (again with no following RECTF).
54 *
55 * Path data is preceded by the DWORD 0x10000001. Then follows a
56 * DWORD size and then size bytes of data.
57 *
58 * The combining ops are stored in the reverse order to the region
59 * elements and in the reverse order to which the region was
60 * constructed.
61 *
62 * When two or more complex regions (ie those with more than one
63 * element) are combined, the combining op for the two regions comes
64 * first, then the combining ops for the region elements in region 1,
65 * followed by the region elements for region 1, then follows the
66 * combining ops for region 2 and finally region 2's region elements.
67 * Presumably you're supposed to use the 0x1000000x header to find the
68 * end of the op list (the count of the elements in each region is not
69 * stored).
70 *
71 * When a simple region (1 element) is combined, it's treated as if a
72 * single rect/path is being combined.
73 *
74 */
75
76 #define FLAGS_NOFLAGS 0x0
77 #define FLAGS_INTPATH 0x4000
78
79 /* Header size as far as header->size is concerned. This doesn't include
80 * header->size or header->checksum
81 */
82 static const INT sizeheader_size = sizeof(DWORD) * 2;
83
84 typedef struct packed_point
85 {
86 short X;
87 short Y;
88 } packed_point;
89
90 /* Everything is measured in DWORDS; round up if there's a remainder */
91 static inline INT get_pathtypes_size(const GpPath* path)
92 {
93 INT needed = path->pathdata.Count / sizeof(DWORD);
94
95 if (path->pathdata.Count % sizeof(DWORD) > 0)
96 needed++;
97
98 return needed * sizeof(DWORD);
99 }
100
101 static inline INT get_element_size(const region_element* element)
102 {
103 INT needed = sizeof(DWORD); /* DWORD for the type */
104 switch(element->type)
105 {
106 case RegionDataRect:
107 return needed + sizeof(GpRect);
108 case RegionDataPath:
109 needed += element->elementdata.pathdata.pathheader.size;
110 needed += sizeof(DWORD); /* Extra DWORD for pathheader.size */
111 return needed;
112 case RegionDataEmptyRect:
113 case RegionDataInfiniteRect:
114 return needed;
115 default:
116 needed += get_element_size(element->elementdata.combine.left);
117 needed += get_element_size(element->elementdata.combine.right);
118 return needed;
119 }
120
121 return 0;
122 }
123
124 /* Does not check parameters, caller must do that */
125 static inline GpStatus init_region(GpRegion* region, const RegionType type)
126 {
127 region->node.type = type;
128 region->header.checksum = 0xdeadbeef;
129 region->header.magic = VERSION_MAGIC;
130 region->header.num_children = 0;
131 region->header.size = sizeheader_size + get_element_size(&region->node);
132
133 return Ok;
134 }
135
136 static inline GpStatus clone_element(const region_element* element,
137 region_element** element2)
138 {
139 GpStatus stat;
140
141 /* root node is allocated with GpRegion */
142 if(!*element2){
143 *element2 = GdipAlloc(sizeof(region_element));
144 if (!*element2)
145 return OutOfMemory;
146 }
147
148 (*element2)->type = element->type;
149
150 switch (element->type)
151 {
152 case RegionDataRect:
153 (*element2)->elementdata.rect = element->elementdata.rect;
154 break;
155 case RegionDataEmptyRect:
156 case RegionDataInfiniteRect:
157 break;
158 case RegionDataPath:
159 (*element2)->elementdata.pathdata.pathheader = element->elementdata.pathdata.pathheader;
160 stat = GdipClonePath(element->elementdata.pathdata.path,
161 &(*element2)->elementdata.pathdata.path);
162 if (stat != Ok) goto clone_out;
163 break;
164 default:
165 (*element2)->elementdata.combine.left = NULL;
166 (*element2)->elementdata.combine.right = NULL;
167
168 stat = clone_element(element->elementdata.combine.left,
169 &(*element2)->elementdata.combine.left);
170 if (stat != Ok) goto clone_out;
171 stat = clone_element(element->elementdata.combine.right,
172 &(*element2)->elementdata.combine.right);
173 if (stat != Ok) goto clone_out;
174 break;
175 }
176
177 return Ok;
178
179 clone_out:
180 delete_element(*element2);
181 *element2 = NULL;
182 return stat;
183 }
184
185 /* Common code for CombineRegion*
186 * All the caller has to do is get its format into an element
187 */
188 static inline void fuse_region(GpRegion* region, region_element* left,
189 region_element* right, const CombineMode mode)
190 {
191 region->node.type = mode;
192 region->node.elementdata.combine.left = left;
193 region->node.elementdata.combine.right = right;
194
195 region->header.size = sizeheader_size + get_element_size(&region->node);
196 region->header.num_children += 2;
197 }
198
199 /*****************************************************************************
200 * GdipCloneRegion [GDIPLUS.@]
201 *
202 * Creates a deep copy of the region
203 *
204 * PARAMS
205 * region [I] source region
206 * clone [O] resulting clone
207 *
208 * RETURNS
209 * SUCCESS: Ok
210 * FAILURE: InvalidParameter or OutOfMemory
211 */
212 GpStatus WINGDIPAPI GdipCloneRegion(GpRegion *region, GpRegion **clone)
213 {
214 region_element *element;
215
216 TRACE("%p %p\n", region, clone);
217
218 if (!(region && clone))
219 return InvalidParameter;
220
221 *clone = GdipAlloc(sizeof(GpRegion));
222 if (!*clone)
223 return OutOfMemory;
224 element = &(*clone)->node;
225
226 (*clone)->header = region->header;
227 return clone_element(&region->node, &element);
228 }
229
230 /*****************************************************************************
231 * GdipCombineRegionPath [GDIPLUS.@]
232 */
233 GpStatus WINGDIPAPI GdipCombineRegionPath(GpRegion *region, GpPath *path, CombineMode mode)
234 {
235 GpRegion *path_region;
236 region_element *left, *right = NULL;
237 GpStatus stat;
238
239 TRACE("%p %p %d\n", region, path, mode);
240
241 if (!(region && path))
242 return InvalidParameter;
243
244 stat = GdipCreateRegionPath(path, &path_region);
245 if (stat != Ok)
246 return stat;
247
248 /* simply replace region data */
249 if(mode == CombineModeReplace){
250 delete_element(&region->node);
251 memcpy(region, path_region, sizeof(GpRegion));
252 return Ok;
253 }
254
255 left = GdipAlloc(sizeof(region_element));
256 if (!left)
257 goto out;
258 *left = region->node;
259
260 stat = clone_element(&path_region->node, &right);
261 if (stat != Ok)
262 goto out;
263
264 fuse_region(region, left, right, mode);
265
266 GdipDeleteRegion(path_region);
267 return Ok;
268
269 out:
270 GdipFree(left);
271 GdipDeleteRegion(path_region);
272 return stat;
273 }
274
275 /*****************************************************************************
276 * GdipCombineRegionRect [GDIPLUS.@]
277 */
278 GpStatus WINGDIPAPI GdipCombineRegionRect(GpRegion *region,
279 GDIPCONST GpRectF *rect, CombineMode mode)
280 {
281 GpRegion *rect_region;
282 region_element *left, *right = NULL;
283 GpStatus stat;
284
285 TRACE("%p %p %d\n", region, rect, mode);
286
287 if (!(region && rect))
288 return InvalidParameter;
289
290 stat = GdipCreateRegionRect(rect, &rect_region);
291 if (stat != Ok)
292 return stat;
293
294 /* simply replace region data */
295 if(mode == CombineModeReplace){
296 delete_element(&region->node);
297 memcpy(region, rect_region, sizeof(GpRegion));
298 return Ok;
299 }
300
301 left = GdipAlloc(sizeof(region_element));
302 if (!left)
303 goto out;
304 memcpy(left, &region->node, sizeof(region_element));
305
306 stat = clone_element(&rect_region->node, &right);
307 if (stat != Ok)
308 goto out;
309
310 fuse_region(region, left, right, mode);
311
312 GdipDeleteRegion(rect_region);
313 return Ok;
314
315 out:
316 GdipFree(left);
317 GdipDeleteRegion(rect_region);
318 return stat;
319 }
320
321 /*****************************************************************************
322 * GdipCombineRegionRectI [GDIPLUS.@]
323 */
324 GpStatus WINGDIPAPI GdipCombineRegionRectI(GpRegion *region,
325 GDIPCONST GpRect *rect, CombineMode mode)
326 {
327 GpRectF rectf;
328
329 TRACE("%p %p %d\n", region, rect, mode);
330
331 if (!rect)
332 return InvalidParameter;
333
334 rectf.X = (REAL)rect->X;
335 rectf.Y = (REAL)rect->Y;
336 rectf.Height = (REAL)rect->Height;
337 rectf.Width = (REAL)rect->Width;
338
339 return GdipCombineRegionRect(region, &rectf, mode);
340 }
341
342 GpStatus WINGDIPAPI GdipCombineRegionRegion(GpRegion *region1,
343 GpRegion *region2, CombineMode mode)
344 {
345 region_element *left, *right = NULL;
346 GpStatus stat;
347 GpRegion *reg2copy;
348
349 TRACE("%p %p %d\n", region1, region2, mode);
350
351 if(!(region1 && region2))
352 return InvalidParameter;
353
354 /* simply replace region data */
355 if(mode == CombineModeReplace){
356 stat = GdipCloneRegion(region2, &reg2copy);
357 if(stat != Ok) return stat;
358
359 delete_element(&region1->node);
360 memcpy(region1, reg2copy, sizeof(GpRegion));
361 GdipFree(reg2copy);
362 return Ok;
363 }
364
365 left = GdipAlloc(sizeof(region_element));
366 if (!left)
367 return OutOfMemory;
368
369 *left = region1->node;
370 stat = clone_element(&region2->node, &right);
371 if (stat != Ok)
372 {
373 GdipFree(left);
374 return OutOfMemory;
375 }
376
377 fuse_region(region1, left, right, mode);
378 region1->header.num_children += region2->header.num_children;
379
380 return Ok;
381 }
382
383 /*****************************************************************************
384 * GdipCreateRegion [GDIPLUS.@]
385 */
386 GpStatus WINGDIPAPI GdipCreateRegion(GpRegion **region)
387 {
388 TRACE("%p\n", region);
389
390 if(!region)
391 return InvalidParameter;
392
393 *region = GdipAlloc(sizeof(GpRegion));
394 if(!*region)
395 return OutOfMemory;
396
397 return init_region(*region, RegionDataInfiniteRect);
398 }
399
400 /*****************************************************************************
401 * GdipCreateRegionPath [GDIPLUS.@]
402 *
403 * Creates a GpRegion from a GpPath
404 *
405 * PARAMS
406 * path [I] path to base the region on
407 * region [O] pointer to the newly allocated region
408 *
409 * RETURNS
410 * SUCCESS: Ok
411 * FAILURE: InvalidParameter
412 *
413 * NOTES
414 * If a path has no floating point points, its points will be stored as shorts
415 * (INTPATH)
416 *
417 * If a path is empty, it is considered to be an INTPATH
418 */
419 GpStatus WINGDIPAPI GdipCreateRegionPath(GpPath *path, GpRegion **region)
420 {
421 region_element* element;
422 GpPoint *pointsi;
423 GpPointF *pointsf;
424
425 GpStatus stat;
426 DWORD flags = FLAGS_INTPATH;
427 INT count, i;
428
429 TRACE("%p, %p\n", path, region);
430
431 if (!(path && region))
432 return InvalidParameter;
433
434 *region = GdipAlloc(sizeof(GpRegion));
435 if(!*region)
436 return OutOfMemory;
437 stat = init_region(*region, RegionDataPath);
438 if (stat != Ok)
439 {
440 GdipDeleteRegion(*region);
441 return stat;
442 }
443 element = &(*region)->node;
444 count = path->pathdata.Count;
445
446 /* Test to see if the path is an Integer path */
447 if (count)
448 {
449 pointsi = GdipAlloc(sizeof(GpPoint) * count);
450 pointsf = GdipAlloc(sizeof(GpPointF) * count);
451 if (!(pointsi && pointsf))
452 {
453 GdipFree(pointsi);
454 GdipFree(pointsf);
455 GdipDeleteRegion(*region);
456 return OutOfMemory;
457 }
458
459 stat = GdipGetPathPointsI(path, pointsi, count);
460 if (stat != Ok)
461 {
462 GdipDeleteRegion(*region);
463 return stat;
464 }
465 stat = GdipGetPathPoints(path, pointsf, count);
466 if (stat != Ok)
467 {
468 GdipDeleteRegion(*region);
469 return stat;
470 }
471
472 for (i = 0; i < count; i++)
473 {
474 if (!(pointsi[i].X == pointsf[i].X &&
475 pointsi[i].Y == pointsf[i].Y ))
476 {
477 flags = FLAGS_NOFLAGS;
478 break;
479 }
480 }
481 GdipFree(pointsi);
482 GdipFree(pointsf);
483 }
484
485 stat = GdipClonePath(path, &element->elementdata.pathdata.path);
486 if (stat != Ok)
487 {
488 GdipDeleteRegion(*region);
489 return stat;
490 }
491
492 /* 3 for headers, once again size doesn't count itself */
493 element->elementdata.pathdata.pathheader.size = ((sizeof(DWORD) * 3));
494 switch(flags)
495 {
496 /* Floats, sent out as floats */
497 case FLAGS_NOFLAGS:
498 element->elementdata.pathdata.pathheader.size +=
499 (sizeof(DWORD) * count * 2);
500 break;
501 /* INTs, sent out as packed shorts */
502 case FLAGS_INTPATH:
503 element->elementdata.pathdata.pathheader.size +=
504 (sizeof(DWORD) * count);
505 break;
506 default:
507 FIXME("Unhandled flags (%08x). Expect wrong results.\n", flags);
508 }
509 element->elementdata.pathdata.pathheader.size += get_pathtypes_size(path);
510 element->elementdata.pathdata.pathheader.magic = VERSION_MAGIC;
511 element->elementdata.pathdata.pathheader.count = count;
512 element->elementdata.pathdata.pathheader.flags = flags;
513 (*region)->header.size = sizeheader_size + get_element_size(element);
514
515 return Ok;
516 }
517
518 /*****************************************************************************
519 * GdipCreateRegionRect [GDIPLUS.@]
520 */
521 GpStatus WINGDIPAPI GdipCreateRegionRect(GDIPCONST GpRectF *rect,
522 GpRegion **region)
523 {
524 GpStatus stat;
525
526 TRACE("%p, %p\n", rect, region);
527
528 if (!(rect && region))
529 return InvalidParameter;
530
531 *region = GdipAlloc(sizeof(GpRegion));
532 stat = init_region(*region, RegionDataRect);
533 if(stat != Ok)
534 {
535 GdipDeleteRegion(*region);
536 return stat;
537 }
538
539 (*region)->node.elementdata.rect.X = rect->X;
540 (*region)->node.elementdata.rect.Y = rect->Y;
541 (*region)->node.elementdata.rect.Width = rect->Width;
542 (*region)->node.elementdata.rect.Height = rect->Height;
543
544 return Ok;
545 }
546
547 GpStatus WINGDIPAPI GdipCreateRegionRectI(GDIPCONST GpRect *rect,
548 GpRegion **region)
549 {
550 GpRectF rectf;
551
552 TRACE("%p, %p\n", rect, region);
553
554 rectf.X = (REAL)rect->X;
555 rectf.Y = (REAL)rect->Y;
556 rectf.Width = (REAL)rect->Width;
557 rectf.Height = (REAL)rect->Height;
558
559 return GdipCreateRegionRect(&rectf, region);
560 }
561
562 GpStatus WINGDIPAPI GdipCreateRegionRgnData(GDIPCONST BYTE *data, INT size, GpRegion **region)
563 {
564 FIXME("(%p, %d, %p): stub\n", data, size, region);
565
566 *region = NULL;
567 return NotImplemented;
568 }
569
570 GpStatus WINGDIPAPI GdipCreateRegionHrgn(HRGN hrgn, GpRegion **region)
571 {
572 FIXME("(%p, %p): stub\n", hrgn, region);
573
574 if(!hrgn || !region)
575 return InvalidParameter;
576
577 *region = NULL;
578 return NotImplemented;
579 }
580
581 GpStatus WINGDIPAPI GdipDeleteRegion(GpRegion *region)
582 {
583 TRACE("%p\n", region);
584
585 if (!region)
586 return InvalidParameter;
587
588 delete_element(&region->node);
589 GdipFree(region);
590
591 return Ok;
592 }
593
594 GpStatus WINGDIPAPI GdipGetRegionBounds(GpRegion *region, GpGraphics *graphics, GpRectF *rect)
595 {
596 FIXME("(%p, %p, %p): stub\n", region, graphics, rect);
597
598 return NotImplemented;
599 }
600
601 GpStatus WINGDIPAPI GdipGetRegionBoundsI(GpRegion *region, GpGraphics *graphics, GpRect *rect)
602 {
603 FIXME("(%p, %p, %p): stub\n", region, graphics, rect);
604
605 return NotImplemented;
606 }
607
608 static inline void write_dword(DWORD* location, INT* offset, const DWORD write)
609 {
610 location[*offset] = write;
611 (*offset)++;
612 }
613
614 static inline void write_float(DWORD* location, INT* offset, const FLOAT write)
615 {
616 ((FLOAT*)location)[*offset] = write;
617 (*offset)++;
618 }
619
620 static inline void write_packed_point(DWORD* location, INT* offset,
621 const GpPointF* write)
622 {
623 packed_point point;
624
625 point.X = write->X;
626 point.Y = write->Y;
627 memcpy(location + *offset, &point, sizeof(packed_point));
628 (*offset)++;
629 }
630
631 static inline void write_path_types(DWORD* location, INT* offset,
632 const GpPath* path)
633 {
634 memcpy(location + *offset, path->pathdata.Types, path->pathdata.Count);
635
636 /* The unwritten parts of the DWORD (if any) must be cleared */
637 if (path->pathdata.Count % sizeof(DWORD))
638 ZeroMemory(((BYTE*)location) + (*offset * sizeof(DWORD)) +
639 path->pathdata.Count,
640 sizeof(DWORD) - path->pathdata.Count % sizeof(DWORD));
641 *offset += (get_pathtypes_size(path) / sizeof(DWORD));
642 }
643
644 static void write_element(const region_element* element, DWORD *buffer,
645 INT* filled)
646 {
647 write_dword(buffer, filled, element->type);
648 switch (element->type)
649 {
650 case CombineModeReplace:
651 case CombineModeIntersect:
652 case CombineModeUnion:
653 case CombineModeXor:
654 case CombineModeExclude:
655 case CombineModeComplement:
656 write_element(element->elementdata.combine.left, buffer, filled);
657 write_element(element->elementdata.combine.right, buffer, filled);
658 break;
659 case RegionDataRect:
660 write_float(buffer, filled, element->elementdata.rect.X);
661 write_float(buffer, filled, element->elementdata.rect.Y);
662 write_float(buffer, filled, element->elementdata.rect.Width);
663 write_float(buffer, filled, element->elementdata.rect.Height);
664 break;
665 case RegionDataPath:
666 {
667 INT i;
668 const GpPath* path = element->elementdata.pathdata.path;
669
670 memcpy(buffer + *filled, &element->elementdata.pathdata.pathheader,
671 sizeof(element->elementdata.pathdata.pathheader));
672 *filled += sizeof(element->elementdata.pathdata.pathheader) / sizeof(DWORD);
673 switch (element->elementdata.pathdata.pathheader.flags)
674 {
675 case FLAGS_NOFLAGS:
676 for (i = 0; i < path->pathdata.Count; i++)
677 {
678 write_float(buffer, filled, path->pathdata.Points[i].X);
679 write_float(buffer, filled, path->pathdata.Points[i].Y);
680 }
681 break;
682 case FLAGS_INTPATH:
683 for (i = 0; i < path->pathdata.Count; i++)
684 {
685 write_packed_point(buffer, filled,
686 &path->pathdata.Points[i]);
687 }
688 }
689 write_path_types(buffer, filled, path);
690 break;
691 }
692 case RegionDataEmptyRect:
693 case RegionDataInfiniteRect:
694 break;
695 }
696 }
697
698 /*****************************************************************************
699 * GdipGetRegionData [GDIPLUS.@]
700 *
701 * Returns the header, followed by combining ops and region elements.
702 *
703 * PARAMS
704 * region [I] region to retrieve from
705 * buffer [O] buffer to hold the resulting data
706 * size [I] size of the buffer
707 * needed [O] (optional) how much data was written
708 *
709 * RETURNS
710 * SUCCESS: Ok
711 * FAILURE: InvalidParamter
712 *
713 * NOTES
714 * The header contains the size, a checksum, a version string, and the number
715 * of children. The size does not count itself or the checksum.
716 * Version is always something like 0xdbc01001 or 0xdbc01002
717 *
718 * An element is a RECT, or PATH; Combining ops are stored as their
719 * CombineMode value. Special regions (infinite, empty) emit just their
720 * op-code; GpRectFs emit their code followed by their points; GpPaths emit
721 * their code followed by a second header for the path followed by the actual
722 * path data. Followed by the flags for each point. The pathheader contains
723 * the size of the data to follow, a version number again, followed by a count
724 * of how many points, and any special flags which may apply. 0x4000 means its
725 * a path of shorts instead of FLOAT.
726 *
727 * Combining Ops are stored in reverse order from when they were constructed;
728 * the output is a tree where the left side combining area is always taken
729 * first.
730 */
731 GpStatus WINGDIPAPI GdipGetRegionData(GpRegion *region, BYTE *buffer, UINT size,
732 UINT *needed)
733 {
734 INT filled = 0;
735
736 TRACE("%p, %p, %d, %p\n", region, buffer, size, needed);
737
738 if (!(region && buffer && size))
739 return InvalidParameter;
740
741 memcpy(buffer, &region->header, sizeof(region->header));
742 filled += sizeof(region->header) / sizeof(DWORD);
743 /* With few exceptions, everything written is DWORD aligned,
744 * so use that as our base */
745 write_element(&region->node, (DWORD*)buffer, &filled);
746
747 if (needed)
748 *needed = filled * sizeof(DWORD);
749
750 return Ok;
751 }
752
753 /*****************************************************************************
754 * GdipGetRegionDataSize [GDIPLUS.@]
755 */
756 GpStatus WINGDIPAPI GdipGetRegionDataSize(GpRegion *region, UINT *needed)
757 {
758 TRACE("%p, %p\n", region, needed);
759
760 if (!(region && needed))
761 return InvalidParameter;
762
763 /* header.size doesn't count header.size and header.checksum */
764 *needed = region->header.size + sizeof(DWORD) * 2;
765
766 return Ok;
767 }
768
769 static GpStatus get_path_hrgn(GpPath *path, GpGraphics *graphics, HRGN *hrgn)
770 {
771 HDC new_hdc=NULL;
772 GpStatus stat;
773 INT save_state;
774
775 if (!graphics)
776 {
777 new_hdc = GetDC(0);
778 if (!new_hdc)
779 return OutOfMemory;
780
781 stat = GdipCreateFromHDC(new_hdc, &graphics);
782 if (stat != Ok)
783 {
784 ReleaseDC(0, new_hdc);
785 return stat;
786 }
787 }
788
789 save_state = SaveDC(graphics->hdc);
790 EndPath(graphics->hdc);
791
792 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
793 : WINDING));
794
795 stat = trace_path(graphics, path);
796 if (stat == Ok)
797 {
798 *hrgn = PathToRegion(graphics->hdc);
799 stat = *hrgn ? Ok : OutOfMemory;
800 }
801
802 RestoreDC(graphics->hdc, save_state);
803 if (new_hdc)
804 {
805 ReleaseDC(0, new_hdc);
806 GdipDeleteGraphics(graphics);
807 }
808
809 return stat;
810 }
811
812 static GpStatus get_region_hrgn(struct region_element *element, GpGraphics *graphics, HRGN *hrgn)
813 {
814 switch (element->type)
815 {
816 case RegionDataInfiniteRect:
817 *hrgn = NULL;
818 return Ok;
819 case RegionDataEmptyRect:
820 *hrgn = CreateRectRgn(0, 0, 0, 0);
821 return *hrgn ? Ok : OutOfMemory;
822 case RegionDataPath:
823 return get_path_hrgn(element->elementdata.pathdata.path, graphics, hrgn);
824 case RegionDataRect:
825 {
826 GpPath* path;
827 GpStatus stat;
828 GpRectF* rc = &element->elementdata.rect;
829
830 stat = GdipCreatePath(FillModeAlternate, &path);
831 if (stat != Ok)
832 return stat;
833 stat = GdipAddPathRectangle(path, rc->X, rc->Y, rc->Width, rc->Height);
834
835 if (stat == Ok)
836 stat = get_path_hrgn(path, graphics, hrgn);
837
838 GdipDeletePath(path);
839
840 return stat;
841 }
842 case CombineModeIntersect:
843 case CombineModeUnion:
844 case CombineModeXor:
845 case CombineModeExclude:
846 case CombineModeComplement:
847 {
848 HRGN left, right;
849 GpStatus stat;
850 int ret;
851
852 stat = get_region_hrgn(element->elementdata.combine.left, graphics, &left);
853 if (stat != Ok)
854 {
855 *hrgn = NULL;
856 return stat;
857 }
858
859 if (left == NULL)
860 {
861 /* existing region is infinite */
862 switch (element->type)
863 {
864 case CombineModeIntersect:
865 return get_region_hrgn(element->elementdata.combine.right, graphics, hrgn);
866 case CombineModeXor: case CombineModeExclude:
867 FIXME("cannot exclude from an infinite region\n");
868 /* fall-through */
869 case CombineModeUnion: case CombineModeComplement:
870 *hrgn = NULL;
871 return Ok;
872 }
873 }
874
875 stat = get_region_hrgn(element->elementdata.combine.right, graphics, &right);
876 if (stat != Ok)
877 {
878 DeleteObject(left);
879 *hrgn = NULL;
880 return stat;
881 }
882
883 if (right == NULL)
884 {
885 /* new region is infinite */
886 switch (element->type)
887 {
888 case CombineModeIntersect:
889 *hrgn = left;
890 return Ok;
891 case CombineModeXor: case CombineModeComplement:
892 FIXME("cannot exclude from an infinite region\n");
893 /* fall-through */
894 case CombineModeUnion: case CombineModeExclude:
895 DeleteObject(left);
896 *hrgn = NULL;
897 return Ok;
898 }
899 }
900
901 switch (element->type)
902 {
903 case CombineModeIntersect:
904 ret = CombineRgn(left, left, right, RGN_AND);
905 break;
906 case CombineModeUnion:
907 ret = CombineRgn(left, left, right, RGN_OR);
908 break;
909 case CombineModeXor:
910 ret = CombineRgn(left, left, right, RGN_XOR);
911 break;
912 case CombineModeExclude:
913 ret = CombineRgn(left, left, right, RGN_DIFF);
914 break;
915 case CombineModeComplement:
916 ret = CombineRgn(left, right, left, RGN_DIFF);
917 break;
918 default:
919 ret = ERROR;
920 }
921
922 DeleteObject(right);
923
924 if (ret == ERROR)
925 {
926 DeleteObject(left);
927 *hrgn = NULL;
928 return GenericError;
929 }
930
931 *hrgn = left;
932 return Ok;
933 }
934 default:
935 FIXME("GdipGetRegionHRgn unimplemented for region type=%x\n", element->type);
936 *hrgn = NULL;
937 return NotImplemented;
938 }
939 }
940
941 /*****************************************************************************
942 * GdipGetRegionHRgn [GDIPLUS.@]
943 */
944 GpStatus WINGDIPAPI GdipGetRegionHRgn(GpRegion *region, GpGraphics *graphics, HRGN *hrgn)
945 {
946 TRACE("(%p, %p, %p)\n", region, graphics, hrgn);
947
948 if (!region || !hrgn)
949 return InvalidParameter;
950
951 return get_region_hrgn(&region->node, graphics, hrgn);
952 }
953
954 GpStatus WINGDIPAPI GdipIsEmptyRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
955 {
956 TRACE("(%p, %p, %p)\n", region, graphics, res);
957
958 if(!region || !graphics || !res)
959 return InvalidParameter;
960
961 *res = (region->node.type == RegionDataEmptyRect);
962
963 return Ok;
964 }
965
966 GpStatus WINGDIPAPI GdipIsEqualRegion(GpRegion *region, GpRegion *region2, GpGraphics *graphics,
967 BOOL *res)
968 {
969 FIXME("(%p, %p, %p, %p): stub\n", region, region2, graphics, res);
970
971 return NotImplemented;
972 }
973
974 /*****************************************************************************
975 * GdipIsInfiniteRegion [GDIPLUS.@]
976 */
977 GpStatus WINGDIPAPI GdipIsInfiniteRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
978 {
979 /* I think graphics is ignored here */
980 TRACE("(%p, %p, %p)\n", region, graphics, res);
981
982 if(!region || !graphics || !res)
983 return InvalidParameter;
984
985 *res = (region->node.type == RegionDataInfiniteRect);
986
987 return Ok;
988 }
989
990 /*****************************************************************************
991 * GdipSetEmpty [GDIPLUS.@]
992 */
993 GpStatus WINGDIPAPI GdipSetEmpty(GpRegion *region)
994 {
995 GpStatus stat;
996
997 TRACE("%p\n", region);
998
999 if (!region)
1000 return InvalidParameter;
1001
1002 delete_element(&region->node);
1003 stat = init_region(region, RegionDataEmptyRect);
1004
1005 return stat;
1006 }
1007
1008 GpStatus WINGDIPAPI GdipSetInfinite(GpRegion *region)
1009 {
1010 GpStatus stat;
1011
1012 TRACE("%p\n", region);
1013
1014 if (!region)
1015 return InvalidParameter;
1016
1017 delete_element(&region->node);
1018 stat = init_region(region, RegionDataInfiniteRect);
1019
1020 return stat;
1021 }
1022
1023 GpStatus WINGDIPAPI GdipTransformRegion(GpRegion *region, GpMatrix *matrix)
1024 {
1025 FIXME("(%p, %p): stub\n", region, matrix);
1026
1027 return NotImplemented;
1028 }
1029
1030 GpStatus WINGDIPAPI GdipTranslateRegion(GpRegion *region, REAL dx, REAL dy)
1031 {
1032 FIXME("(%p, %f, %f): stub\n", region, dx, dy);
1033
1034 return NotImplemented;
1035 }
1036
1037 GpStatus WINGDIPAPI GdipTranslateRegionI(GpRegion *region, INT dx, INT dy)
1038 {
1039 FIXME("(%p, %d, %d): stub\n", region, dx, dy);
1040
1041 return NotImplemented;
1042 }