Indirection for INSTR_EmulateInstruction for use by DOS code.
[wine] / graphics / painting.c
1 /*
2  * Misc. graphics operations
3  *
4  * Copyright 1993, 1994 Alexandre Julliard
5  * Copyright 1997 Bertho A. Stultiens
6  */
7
8 #include <math.h>
9 #include <stdlib.h>
10 #include "ts_xlib.h"
11 #include "ts_xutil.h"
12 #include <X11/Intrinsic.h>
13 #ifndef PI
14 #define PI M_PI
15 #endif
16 #include "gdi.h"
17 #include "dc.h"
18 #include "bitmap.h"
19 #include "callback.h"
20 #include "heap.h"
21 #include "metafile.h"
22 #include "palette.h"
23 #include "cache.h"
24 #include "color.h"
25 #include "region.h"
26 #include "path.h"
27 #include "debug.h"
28
29 /***********************************************************************
30  *           LineTo16    (GDI.19)
31  */
32 BOOL16 WINAPI LineTo16( HDC16 hdc, INT16 x, INT16 y )
33 {
34     return LineTo32( hdc, x, y );
35 }
36
37
38 /***********************************************************************
39  *           LineTo32    (GDI32.249)
40  */
41 BOOL32 WINAPI LineTo32( HDC32 hdc, INT32 x, INT32 y )
42 {
43     DC * dc = DC_GetDCPtr( hdc );
44
45     if(dc && PATH_IsPathOpen(dc->w.path))
46         if(!PATH_LineTo(hdc, x, y))
47            return FALSE;
48     
49     return dc && dc->funcs->pLineTo &&
50            dc->funcs->pLineTo(dc,x,y);
51 }
52
53
54 /***********************************************************************
55  *           MoveTo    (GDI.20)
56  */
57 DWORD WINAPI MoveTo( HDC16 hdc, INT16 x, INT16 y )
58 {
59     POINT16     pt;
60
61     if (!MoveToEx16(hdc,x,y,&pt))
62         return 0;
63     return MAKELONG(pt.x,pt.y);
64 }
65
66
67 /***********************************************************************
68  *           MoveToEx16    (GDI.483)
69  */
70 BOOL16 WINAPI MoveToEx16( HDC16 hdc, INT16 x, INT16 y, LPPOINT16 pt )
71 {
72     POINT32 pt32;
73
74     if (!MoveToEx32( (HDC32)hdc, (INT32)x, (INT32)y, &pt32 )) return FALSE;
75     if (pt) CONV_POINT32TO16( &pt32, pt );
76     return TRUE;
77
78 }
79
80
81 /***********************************************************************
82  *           MoveToEx32    (GDI32.254)
83  */
84 BOOL32 WINAPI MoveToEx32( HDC32 hdc, INT32 x, INT32 y, LPPOINT32 pt )
85 {
86     DC * dc = DC_GetDCPtr( hdc );
87   
88     if(dc && PATH_IsPathOpen(dc->w.path))
89         if(!PATH_MoveTo(hdc))
90             return FALSE;
91
92     return dc && dc->funcs->pMoveToEx &&
93            dc->funcs->pMoveToEx(dc,x,y,pt);
94 }
95
96
97 /***********************************************************************
98  *           Arc16    (GDI.23)
99  */
100 BOOL16 WINAPI Arc16( HDC16 hdc, INT16 left, INT16 top, INT16 right,
101                      INT16 bottom, INT16 xstart, INT16 ystart,
102                      INT16 xend, INT16 yend )
103 {
104     return Arc32( (HDC32)hdc, (INT32)left, (INT32)top, (INT32)right,
105                   (INT32)bottom, (INT32)xstart, (INT32)ystart, (INT32)xend,
106                   (INT32)yend );
107 }
108
109
110 /***********************************************************************
111  *           Arc32    (GDI32.7)
112  */
113 BOOL32 WINAPI Arc32( HDC32 hdc, INT32 left, INT32 top, INT32 right,
114                      INT32 bottom, INT32 xstart, INT32 ystart,
115                      INT32 xend, INT32 yend )
116 {
117     DC * dc = DC_GetDCPtr( hdc );
118   
119     if(dc && PATH_IsPathOpen(dc->w.path))
120         if(!PATH_Arc(hdc, left, top, right, bottom, xstart, ystart, xend,
121            yend))
122            return FALSE;
123     
124     return dc && dc->funcs->pArc &&
125            dc->funcs->pArc(dc,left,top,right,bottom,xstart,ystart,xend,yend);
126 }
127
128
129 /***********************************************************************
130  *           Pie16    (GDI.26)
131  */
132 BOOL16 WINAPI Pie16( HDC16 hdc, INT16 left, INT16 top,
133                      INT16 right, INT16 bottom, INT16 xstart, INT16 ystart,
134                      INT16 xend, INT16 yend )
135 {
136     return Pie32( (HDC32)hdc, (INT32)left, (INT32)top, (INT32)right,
137                   (INT32)bottom, (INT32)xstart, (INT32)ystart, (INT32)xend,
138                   (INT32)yend );
139 }
140
141
142 /***********************************************************************
143  *           Pie32   (GDI32.262)
144  */
145 BOOL32 WINAPI Pie32( HDC32 hdc, INT32 left, INT32 top,
146                      INT32 right, INT32 bottom, INT32 xstart, INT32 ystart,
147                      INT32 xend, INT32 yend )
148 {
149     DC * dc = DC_GetDCPtr( hdc );
150   
151     return dc && dc->funcs->pPie &&
152            dc->funcs->pPie(dc,left,top,right,bottom,xstart,ystart,xend,yend);
153 }
154
155
156 /***********************************************************************
157  *           Chord16    (GDI.348)
158  */
159 BOOL16 WINAPI Chord16( HDC16 hdc, INT16 left, INT16 top,
160                        INT16 right, INT16 bottom, INT16 xstart, INT16 ystart,
161                        INT16 xend, INT16 yend )
162 {
163     return Chord32( hdc, left, top, right, bottom, xstart, ystart, xend, yend );
164 }
165
166
167 /***********************************************************************
168  *           Chord32    (GDI32.14)
169  */
170 BOOL32 WINAPI Chord32( HDC32 hdc, INT32 left, INT32 top,
171                        INT32 right, INT32 bottom, INT32 xstart, INT32 ystart,
172                        INT32 xend, INT32 yend )
173 {
174     DC * dc = DC_GetDCPtr( hdc );
175   
176     return dc && dc->funcs->pChord &&
177            dc->funcs->pChord(dc,left,top,right,bottom,xstart,ystart,xend,yend);
178 }
179
180
181 /***********************************************************************
182  *           Ellipse16    (GDI.24)
183  */
184 BOOL16 WINAPI Ellipse16( HDC16 hdc, INT16 left, INT16 top,
185                          INT16 right, INT16 bottom )
186 {
187     return Ellipse32( hdc, left, top, right, bottom );
188 }
189
190
191 /***********************************************************************
192  *           Ellipse32    (GDI32.75)
193  */
194 BOOL32 WINAPI Ellipse32( HDC32 hdc, INT32 left, INT32 top,
195                          INT32 right, INT32 bottom )
196 {
197     DC * dc = DC_GetDCPtr( hdc );
198   
199     return dc && dc->funcs->pEllipse &&
200            dc->funcs->pEllipse(dc,left,top,right,bottom);
201 }
202
203
204 /***********************************************************************
205  *           Rectangle16    (GDI.27)
206  */
207 BOOL16 WINAPI Rectangle16( HDC16 hdc, INT16 left, INT16 top,
208                            INT16 right, INT16 bottom )
209 {
210     return Rectangle32( hdc, left, top, right, bottom );
211 }
212
213
214 /***********************************************************************
215  *           Rectangle32    (GDI32.283)
216  */
217 BOOL32 WINAPI Rectangle32( HDC32 hdc, INT32 left, INT32 top,
218                            INT32 right, INT32 bottom )
219 {
220     DC * dc = DC_GetDCPtr( hdc );
221   
222     if(dc && PATH_IsPathOpen(dc->w.path))
223         if(!PATH_Rectangle(hdc, left, top, right, bottom))
224            return FALSE;
225
226     return dc && dc->funcs->pRectangle &&
227            dc->funcs->pRectangle(dc,left,top,right,bottom);
228 }
229
230
231 /***********************************************************************
232  *           RoundRect16    (GDI.28)
233  */
234 BOOL16 WINAPI RoundRect16( HDC16 hdc, INT16 left, INT16 top, INT16 right,
235                            INT16 bottom, INT16 ell_width, INT16 ell_height )
236 {
237     return RoundRect32( hdc, left, top, right, bottom, ell_width, ell_height );
238 }
239
240
241 /***********************************************************************
242  *           RoundRect32    (GDI32.291)
243  */
244 BOOL32 WINAPI RoundRect32( HDC32 hdc, INT32 left, INT32 top, INT32 right,
245                            INT32 bottom, INT32 ell_width, INT32 ell_height )
246 {
247     DC * dc = DC_GetDCPtr( hdc );
248   
249     return dc && dc->funcs->pRoundRect &&
250            dc->funcs->pRoundRect(dc,left,top,right,bottom,ell_width,ell_height);
251 }
252
253
254 /***********************************************************************
255  *           FillRect16    (USER.81)
256  */
257 INT16 WINAPI FillRect16( HDC16 hdc, const RECT16 *rect, HBRUSH16 hbrush )
258 {
259     HBRUSH16 prevBrush;
260
261     /* coordinates are logical so we cannot fast-check 'rect',
262      * it will be done later in the PatBlt().
263      */
264
265     if (!(prevBrush = SelectObject16( hdc, hbrush ))) return 0;
266     PatBlt32( hdc, rect->left, rect->top,
267               rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
268     SelectObject16( hdc, prevBrush );
269     return 1;
270 }
271
272
273 /***********************************************************************
274  *           FillRect32    (USER32.197)
275  */
276 INT32 WINAPI FillRect32( HDC32 hdc, const RECT32 *rect, HBRUSH32 hbrush )
277 {
278     HBRUSH32 prevBrush;
279
280     if (!(prevBrush = SelectObject32( hdc, hbrush ))) return 0;
281     PatBlt32( hdc, rect->left, rect->top,
282               rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
283     SelectObject32( hdc, prevBrush );
284     return 1;
285 }
286
287
288 /***********************************************************************
289  *           InvertRect16    (USER.82)
290  */
291 void WINAPI InvertRect16( HDC16 hdc, const RECT16 *rect )
292 {
293     PatBlt32( hdc, rect->left, rect->top,
294               rect->right - rect->left, rect->bottom - rect->top, DSTINVERT );
295 }
296
297
298 /***********************************************************************
299  *           InvertRect32    (USER32.330)
300  */
301 void WINAPI InvertRect32( HDC32 hdc, const RECT32 *rect )
302 {
303     PatBlt32( hdc, rect->left, rect->top,
304               rect->right - rect->left, rect->bottom - rect->top, DSTINVERT );
305 }
306
307
308 /***********************************************************************
309  *           FrameRect16    (USER.83)
310  */
311 INT16 WINAPI FrameRect16( HDC16 hdc, const RECT16 *rect, HBRUSH16 hbrush )
312 {
313     HBRUSH16 prevBrush;
314     int left, top, right, bottom;
315
316     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
317     if (!dc) return FALSE;
318
319     left   = XLPTODP( dc, rect->left );
320     top    = YLPTODP( dc, rect->top );
321     right  = XLPTODP( dc, rect->right );
322     bottom = YLPTODP( dc, rect->bottom );
323
324     if ( (right <= left) || (bottom <= top) ) return 0;
325     if (!(prevBrush = SelectObject16( hdc, hbrush ))) return 0;
326     
327     if (DC_SetupGCForBrush( dc ))
328     {
329         PatBlt32( hdc, rect->left, rect->top, 1,
330                   rect->bottom - rect->top, PATCOPY );
331         PatBlt32( hdc, rect->right - 1, rect->top, 1,
332                   rect->bottom - rect->top, PATCOPY );
333         PatBlt32( hdc, rect->left, rect->top,
334                   rect->right - rect->left, 1, PATCOPY );
335         PatBlt32( hdc, rect->left, rect->bottom - 1,
336                   rect->right - rect->left, 1, PATCOPY );
337     }
338     SelectObject16( hdc, prevBrush );
339     return 1;
340 }
341
342
343 /***********************************************************************
344  *           FrameRect32    (USER32.203)
345  */
346 INT32 WINAPI FrameRect32( HDC32 hdc, const RECT32 *rect, HBRUSH32 hbrush )
347 {
348     RECT16 rect16;
349     CONV_RECT32TO16( rect, &rect16 );
350     return FrameRect16( (HDC16)hdc, &rect16, (HBRUSH16)hbrush );
351 }
352
353
354 /***********************************************************************
355  *           SetPixel16    (GDI.31)
356  */
357 COLORREF WINAPI SetPixel16( HDC16 hdc, INT16 x, INT16 y, COLORREF color )
358 {
359     return SetPixel32( hdc, x, y, color );
360 }
361
362
363 /***********************************************************************
364  *           SetPixel32    (GDI32.327)
365  */
366 COLORREF WINAPI SetPixel32( HDC32 hdc, INT32 x, INT32 y, COLORREF color )
367 {
368     DC * dc = DC_GetDCPtr( hdc );
369   
370     if (!dc || !dc->funcs->pSetPixel) return 0;
371     return dc->funcs->pSetPixel(dc,x,y,color);
372 }
373
374 /***********************************************************************
375  *           SetPixelV32    (GDI32.329)
376  */
377 BOOL32 WINAPI SetPixelV32( HDC32 hdc, INT32 x, INT32 y, COLORREF color )
378 {
379     DC * dc = DC_GetDCPtr( hdc );
380   
381     if (!dc || !dc->funcs->pSetPixel) return FALSE;
382     dc->funcs->pSetPixel(dc,x,y,color);
383     return TRUE;
384 }
385
386 /***********************************************************************
387  *           GetPixel16    (GDI.83)
388  */
389 COLORREF WINAPI GetPixel16( HDC16 hdc, INT16 x, INT16 y )
390 {
391     return GetPixel32( hdc, x, y );
392 }
393
394
395 /***********************************************************************
396  *           GetPixel32    (GDI32.211)
397  */
398 COLORREF WINAPI GetPixel32( HDC32 hdc, INT32 x, INT32 y )
399 {
400     DC * dc = DC_GetDCPtr( hdc );
401
402     if (!dc) return 0;
403 #ifdef SOLITAIRE_SPEED_HACK
404     return 0;
405 #endif
406
407     /* FIXME: should this be in the graphics driver? */
408     if (!PtVisible32( hdc, x, y )) return 0;
409     if (!dc || !dc->funcs->pGetPixel) return 0;
410     return dc->funcs->pGetPixel(dc,x,y);
411 }
412
413
414 /******************************************************************************
415  * ChoosePixelFormat [GDI32.13]
416  * Matches a pixel format to given format
417  *
418  * PARAMS
419  *    hdc  [I] Device context to search for best pixel match
420  *    ppfd [I] Pixel format for which a match is sought
421  *
422  * RETURNS
423  *    Success: Pixel format index closest to given format
424  *    Failure: 0
425  */
426 INT32 WINAPI ChoosePixelFormat( HDC32 hdc, const PIXELFORMATDESCRIPTOR* ppfd )
427 {
428     FIXME(gdi, "(%d,%p): stub\n",hdc,ppfd);
429     return 1;
430 }
431
432
433 /******************************************************************************
434  * SetPixelFormat [GDI32.328]
435  * Sets pixel format of device context
436  *
437  * PARAMS
438  *    hdc          [I] Device context to search for best pixel match
439  *    iPixelFormat [I] Pixel format index
440  *    ppfd         [I] Pixel format for which a match is sought
441  *
442  * RETURNS STD
443  */
444 BOOL32 WINAPI SetPixelFormat( HDC32 hdc, int iPixelFormat, 
445                               const PIXELFORMATDESCRIPTOR* ppfd)
446 {
447     FIXME(gdi, "(%d,%d,%p): stub\n",hdc,iPixelFormat,ppfd);
448     return TRUE;
449 }
450
451
452 /******************************************************************************
453  * GetPixelFormat [GDI32.212]
454  * Gets index of pixel format of DC
455  *
456  * PARAMETERS
457  *    hdc [I] Device context whose pixel format index is sought
458  *
459  * RETURNS
460  *    Success: Currently selected pixel format
461  *    Failure: 0
462  */
463 int WINAPI GetPixelFormat( HDC32 hdc )
464 {
465     FIXME(gdi, "(%d): stub\n",hdc);
466     return 1;
467 }
468
469
470 /******************************************************************************
471  * DescribePixelFormat [GDI32.71]
472  * Gets info about pixel format from DC
473  *
474  * PARAMS
475  *    hdc          [I] Device context
476  *    iPixelFormat [I] Pixel format selector
477  *    nBytes       [I] Size of buffer
478  *    ppfd         [O] Pointer to structure to receive pixel format data
479  *
480  * RETURNS
481  *    Success: Maximum pixel format index of the device context
482  *    Failure: 0
483  */
484 int WINAPI DescribePixelFormat( HDC32 hdc, int iPixelFormat, UINT32 nBytes,
485                                 LPPIXELFORMATDESCRIPTOR ppfd )
486 {
487     FIXME(gdi, "(%d,%d,%d,%p): stub\n",hdc,iPixelFormat,nBytes,ppfd);
488     ppfd->nSize = nBytes;
489     ppfd->nVersion = 1;
490     return 3;
491 }
492
493
494 /******************************************************************************
495  * SwapBuffers [GDI32.354]
496  * Exchanges front and back buffers of window
497  *
498  * PARAMS
499  *    hdc [I] Device context whose buffers get swapped
500  *
501  * RETURNS STD
502  */
503 BOOL32 WINAPI SwapBuffers( HDC32 hdc )
504 {
505     FIXME(gdi, "(%d): stub\n",hdc);
506     return TRUE;
507 }
508
509
510 /***********************************************************************
511  *           PaintRgn16    (GDI.43)
512  */
513 BOOL16 WINAPI PaintRgn16( HDC16 hdc, HRGN16 hrgn )
514 {
515     return PaintRgn32( hdc, hrgn );
516 }
517
518
519 /***********************************************************************
520  *           PaintRgn32    (GDI32.259)
521  */
522 BOOL32 WINAPI PaintRgn32( HDC32 hdc, HRGN32 hrgn )
523 {
524     DC * dc = DC_GetDCPtr( hdc );
525
526     return dc && dc->funcs->pPaintRgn &&
527            dc->funcs->pPaintRgn(dc,hrgn);
528 }
529
530
531 /***********************************************************************
532  *           FillRgn16    (GDI.40)
533  */
534 BOOL16 WINAPI FillRgn16( HDC16 hdc, HRGN16 hrgn, HBRUSH16 hbrush )
535 {
536     return FillRgn32( hdc, hrgn, hbrush );
537 }
538
539     
540 /***********************************************************************
541  *           FillRgn32    (GDI32.101)
542  */
543 BOOL32 WINAPI FillRgn32( HDC32 hdc, HRGN32 hrgn, HBRUSH32 hbrush )
544 {
545     BOOL32 retval;
546     HBRUSH32 prevBrush = SelectObject32( hdc, hbrush );
547     if (!prevBrush) return FALSE;
548     retval = PaintRgn32( hdc, hrgn );
549     SelectObject32( hdc, prevBrush );
550     return retval;
551 }
552
553
554 /***********************************************************************
555  *           FrameRgn16     (GDI.41)
556  */
557 BOOL16 WINAPI FrameRgn16( HDC16 hdc, HRGN16 hrgn, HBRUSH16 hbrush,
558                           INT16 nWidth, INT16 nHeight )
559 {
560     return FrameRgn32( hdc, hrgn, hbrush, nWidth, nHeight );
561 }
562
563
564 /***********************************************************************
565  *           FrameRgn32     (GDI32.105)
566  */
567 BOOL32 WINAPI FrameRgn32( HDC32 hdc, HRGN32 hrgn, HBRUSH32 hbrush,
568                           INT32 nWidth, INT32 nHeight )
569 {
570     HRGN32 tmp = CreateRectRgn32( 0, 0, 0, 0 );
571     if(!REGION_FrameRgn( tmp, hrgn, nWidth, nHeight )) return FALSE;
572     FillRgn32( hdc, tmp, hbrush );
573     DeleteObject32( tmp );
574     return TRUE;
575 }
576
577
578 /***********************************************************************
579  *           InvertRgn16    (GDI.42)
580  */
581 BOOL16 WINAPI InvertRgn16( HDC16 hdc, HRGN16 hrgn )
582 {
583     return InvertRgn32( hdc, hrgn );
584 }
585
586
587 /***********************************************************************
588  *           InvertRgn32    (GDI32.246)
589  */
590 BOOL32 WINAPI InvertRgn32( HDC32 hdc, HRGN32 hrgn )
591 {
592     HBRUSH32 prevBrush = SelectObject32( hdc, GetStockObject32(BLACK_BRUSH) );
593     INT32 prevROP = SetROP232( hdc, R2_NOT );
594     BOOL32 retval = PaintRgn32( hdc, hrgn );
595     SelectObject32( hdc, prevBrush );
596     SetROP232( hdc, prevROP );
597     return retval;
598 }
599
600
601 /***********************************************************************
602  *           DrawFocusRect16    (USER.466)
603  */
604 void WINAPI DrawFocusRect16( HDC16 hdc, const RECT16* rc )
605 {
606     RECT32 rect32;
607     CONV_RECT16TO32( rc, &rect32 );
608     DrawFocusRect32( hdc, &rect32 );
609 }
610
611
612 /***********************************************************************
613  *           DrawFocusRect32    (USER32.156)
614  *
615  * FIXME: PatBlt(PATINVERT) with background brush.
616  */
617 void WINAPI DrawFocusRect32( HDC32 hdc, const RECT32* rc )
618 {
619     HPEN32 hOldPen, hnewPen;
620     INT32 oldDrawMode, oldBkMode;
621     INT32 left, top, right, bottom;
622
623     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
624     if (!dc) return;
625
626     left   = XLPTODP( dc, rc->left );
627     top    = YLPTODP( dc, rc->top );
628     right  = XLPTODP( dc, rc->right );
629     bottom = YLPTODP( dc, rc->bottom );
630
631     if(left == right || top == bottom)
632         return;
633
634     hnewPen = CreatePen32(PS_DOT, 1, GetSysColor32(COLOR_WINDOWTEXT) );
635     hOldPen = SelectObject32( hdc, hnewPen );
636     oldDrawMode = SetROP232(hdc, R2_XORPEN);
637     oldBkMode = SetBkMode32(hdc, TRANSPARENT);
638
639     /* Hack: make sure the XORPEN operation has an effect */
640     dc->u.x.pen.pixel = (1 << screenDepth) - 1;
641
642     if (DC_SetupGCForPen( dc ))
643         TSXDrawRectangle( display, dc->u.x.drawable, dc->u.x.gc,
644                         dc->w.DCOrgX + left, dc->w.DCOrgY + top,
645                         right-left-1, bottom-top-1 );
646
647     SetBkMode32(hdc, oldBkMode);
648     SetROP232(hdc, oldDrawMode);
649     SelectObject32(hdc, hOldPen);
650     DeleteObject32(hnewPen);
651 }
652
653
654 /**********************************************************************
655  *          Polyline16  (GDI.37)
656  */
657 BOOL16 WINAPI Polyline16( HDC16 hdc, const POINT16* pt, INT16 count )
658 {
659     register int i;
660     BOOL16 ret;
661     LPPOINT32 pt32 = (LPPOINT32)HeapAlloc( GetProcessHeap(), 0,
662                                            count*sizeof(POINT32) );
663
664     if (!pt32) return FALSE;
665     for (i=count;i--;) CONV_POINT16TO32(&(pt[i]),&(pt32[i]));
666     ret = Polyline32(hdc,pt32,count);
667     HeapFree( GetProcessHeap(), 0, pt32 );
668     return ret;
669 }
670
671
672 /**********************************************************************
673  *          Polyline32   (GDI32.276)
674  */
675 BOOL32 WINAPI Polyline32( HDC32 hdc, const POINT32* pt, INT32 count )
676 {
677     DC * dc = DC_GetDCPtr( hdc );
678
679     return dc && dc->funcs->pPolyline &&
680            dc->funcs->pPolyline(dc,pt,count);
681 }
682
683
684 /**********************************************************************
685  *          Polygon16  (GDI.36)
686  */
687 BOOL16 WINAPI Polygon16( HDC16 hdc, const POINT16* pt, INT16 count )
688 {
689     register int i;
690     BOOL32 ret;
691     LPPOINT32 pt32 = (LPPOINT32)HeapAlloc( GetProcessHeap(), 0,
692                                            count*sizeof(POINT32) );
693
694     if (!pt32) return FALSE;
695     for (i=count;i--;) CONV_POINT16TO32(&(pt[i]),&(pt32[i]));
696     ret = Polygon32(hdc,pt32,count);
697     HeapFree( GetProcessHeap(), 0, pt32 );
698     return ret;
699 }
700
701
702 /**********************************************************************
703  *          Polygon32  (GDI32.275)
704  */
705 BOOL32 WINAPI Polygon32( HDC32 hdc, const POINT32* pt, INT32 count )
706 {
707     DC * dc = DC_GetDCPtr( hdc );
708
709     return dc && dc->funcs->pPolygon &&
710            dc->funcs->pPolygon(dc,pt,count);
711 }
712
713
714 /**********************************************************************
715  *          PolyPolygon16  (GDI.450)
716  */
717 BOOL16 WINAPI PolyPolygon16( HDC16 hdc, const POINT16* pt, const INT16* counts,
718                              UINT16 polygons )
719 {
720     int         i,nrpts;
721     LPPOINT32   pt32;
722     LPINT32     counts32;
723     BOOL16      ret;
724
725     nrpts=0;
726     for (i=polygons;i--;)
727         nrpts+=counts[i];
728     pt32 = (LPPOINT32)HEAP_xalloc( GetProcessHeap(), 0, sizeof(POINT32)*nrpts);
729     for (i=nrpts;i--;)
730         CONV_POINT16TO32(&(pt[i]),&(pt32[i]));
731     counts32 = (LPINT32)HEAP_xalloc( GetProcessHeap(), 0,
732                                      polygons*sizeof(INT32) );
733     for (i=polygons;i--;) counts32[i]=counts[i];
734    
735     ret = PolyPolygon32(hdc,pt32,counts32,polygons);
736     HeapFree( GetProcessHeap(), 0, counts32 );
737     HeapFree( GetProcessHeap(), 0, pt32 );
738     return ret;
739 }
740
741 /**********************************************************************
742  *          PolyPolygon32  (GDI.450)
743  */
744 BOOL32 WINAPI PolyPolygon32( HDC32 hdc, const POINT32* pt, const INT32* counts,
745                              UINT32 polygons )
746 {
747     DC * dc = DC_GetDCPtr( hdc );
748
749     return dc && dc->funcs->pPolyPolygon &&
750            dc->funcs->pPolyPolygon(dc,pt,counts,polygons);
751 }
752
753 /**********************************************************************
754  *          PolyPolyline32  (GDI32.272)
755  */
756 BOOL32 WINAPI PolyPolyline32( HDC32 hdc, const POINT32* pt, const DWORD* counts,
757                              DWORD polylines )
758 {
759     DC * dc = DC_GetDCPtr( hdc );
760
761     return dc && dc->funcs->pPolyPolyline &&
762            dc->funcs->pPolyPolyline(dc,pt,counts,polylines);
763 }
764
765 /**********************************************************************
766  *          ExtFloodFill16   (GDI.372)
767  */
768 BOOL16 WINAPI ExtFloodFill16( HDC16 hdc, INT16 x, INT16 y, COLORREF color,
769                               UINT16 fillType )
770 {
771     return ExtFloodFill32( hdc, x, y, color, fillType );
772 }
773
774
775 /**********************************************************************
776  *          ExtFloodFill32   (GDI32.96)
777  */
778 BOOL32 WINAPI ExtFloodFill32( HDC32 hdc, INT32 x, INT32 y, COLORREF color,
779                               UINT32 fillType )
780 {
781     DC *dc = DC_GetDCPtr( hdc );
782
783     return dc && dc->funcs->pExtFloodFill &&
784            dc->funcs->pExtFloodFill(dc,x,y,color,fillType);
785 }
786
787
788 /**********************************************************************
789  *          FloodFill16   (GDI.25)
790  */
791 BOOL16 WINAPI FloodFill16( HDC16 hdc, INT16 x, INT16 y, COLORREF color )
792 {
793     return ExtFloodFill32( hdc, x, y, color, FLOODFILLBORDER );
794 }
795
796
797 /**********************************************************************
798  *          FloodFill32   (GDI32.104)
799  */
800 BOOL32 WINAPI FloodFill32( HDC32 hdc, INT32 x, INT32 y, COLORREF color )
801 {
802     return ExtFloodFill32( hdc, x, y, color, FLOODFILLBORDER );
803 }
804
805
806 /**********************************************************************
807  *          DrawAnimatedRects32  (USER32.153)
808  */
809 BOOL32 WINAPI DrawAnimatedRects32( HWND32 hwnd, int idAni,
810                                    const RECT32* lprcFrom,
811                                    const RECT32* lprcTo )
812 {
813     FIXME(gdi,"(0x%x,%d,%p,%p): stub\n",hwnd,idAni,lprcFrom,lprcTo);
814     return TRUE;
815 }
816
817
818 /**********************************************************************
819  *          PAINTING_DrawStateJam
820  *
821  * Jams in the requested type in the dc
822  */
823 static BOOL32 PAINTING_DrawStateJam(HDC32 hdc, UINT32 opcode,
824                                     DRAWSTATEPROC32 func, LPARAM lp, WPARAM32 wp, 
825                                     LPRECT32 rc, UINT32 dtflags,
826                                     BOOL32 unicode, BOOL32 _32bit)
827 {
828     HDC32 memdc;
829     HBITMAP32 hbmsave;
830     BOOL32 retval;
831     INT32 cx = rc->right - rc->left;
832     INT32 cy = rc->bottom - rc->top;
833     
834     switch(opcode)
835     {
836     case DST_TEXT:
837     case DST_PREFIXTEXT:
838         if(unicode)
839             return DrawText32W(hdc, (LPWSTR)lp, (INT32)wp, rc, dtflags);
840         else if(_32bit)
841             return DrawText32A(hdc, (LPSTR)lp, (INT32)wp, rc, dtflags);
842         else
843             return DrawText32A(hdc, (LPSTR)PTR_SEG_TO_LIN(lp), (INT32)wp, rc, dtflags);
844
845     case DST_ICON:
846         return DrawIcon32(hdc, rc->left, rc->top, (HICON32)lp);
847
848     case DST_BITMAP:
849         memdc = CreateCompatibleDC32(hdc);
850         if(!memdc) return FALSE;
851         hbmsave = (HBITMAP32)SelectObject32(memdc, (HBITMAP32)lp);
852         if(!hbmsave) 
853         {
854             DeleteDC32(memdc);
855             return FALSE;
856         }
857         retval = BitBlt32(hdc, rc->left, rc->top, cx, cy, memdc, 0, 0, SRCCOPY);
858         SelectObject32(memdc, hbmsave);
859         DeleteDC32(memdc);
860         return retval;
861             
862     case DST_COMPLEX:
863         if(func)
864             if(_32bit)
865                 return func(hdc, lp, wp, cx, cy);
866             else
867                 return (BOOL32)((DRAWSTATEPROC16)func)((HDC16)hdc, (LPARAM)lp, (WPARAM16)wp, (INT16)cx, (INT16)cy);
868         else
869             return FALSE;
870     }
871     return FALSE;
872 }
873
874 /**********************************************************************
875  *      PAINTING_DrawState32()
876  */
877 static BOOL32 PAINTING_DrawState32(HDC32 hdc, HBRUSH32 hbr, 
878                                    DRAWSTATEPROC32 func, LPARAM lp, WPARAM32 wp,
879                                    INT32 x, INT32 y, INT32 cx, INT32 cy, 
880                                    UINT32 flags, BOOL32 unicode, BOOL32 _32bit)
881 {
882     HBITMAP32 hbm, hbmsave;
883     HFONT32 hfsave;
884     HBRUSH32 hbsave;
885     HDC32 memdc;
886     RECT32 rc;
887     UINT32 dtflags = DT_NOCLIP;
888     COLORREF fg, bg;
889     UINT32 opcode = flags & 0xf;
890     INT32 len = wp;
891     BOOL32 retval, tmp;
892
893     if((opcode == DST_TEXT || opcode == DST_PREFIXTEXT) && !len)    /* The string is '\0' terminated */
894     {
895         if(unicode)
896             len = lstrlen32W((LPWSTR)lp);
897         else if(_32bit)
898             len = lstrlen32A((LPSTR)lp);
899         else
900             len = lstrlen32A((LPSTR)PTR_SEG_TO_LIN(lp));
901     }
902
903     /* Find out what size the image has if not given by caller */
904     if(!cx || !cy)
905     {
906         SIZE32 s;
907         CURSORICONINFO *ici;
908         BITMAPOBJ *bmp;
909
910         switch(opcode)
911         {
912         case DST_TEXT:
913         case DST_PREFIXTEXT:
914             if(unicode)
915                 retval = GetTextExtentPoint32W(hdc, (LPWSTR)lp, len, &s);
916             else if(_32bit)
917                 retval = GetTextExtentPoint32A(hdc, (LPSTR)lp, len, &s);
918             else
919                 retval = GetTextExtentPoint32A(hdc, PTR_SEG_TO_LIN(lp), len, &s);
920             if(!retval) return FALSE;
921             break;
922             
923         case DST_ICON:
924             ici = (CURSORICONINFO *)GlobalLock16((HGLOBAL16)lp);
925             if(!ici) return FALSE;
926             s.cx = ici->nWidth;
927             s.cy = ici->nHeight;
928             GlobalUnlock16((HGLOBAL16)lp);
929             break;            
930
931         case DST_BITMAP:
932             bmp = (BITMAPOBJ *)GDI_GetObjPtr((HBITMAP16)lp, BITMAP_MAGIC);
933             if(!bmp) return FALSE;
934             s.cx = bmp->bitmap.bmWidth;
935             s.cy = bmp->bitmap.bmHeight;
936             break;
937             
938         case DST_COMPLEX: /* cx and cy must be set in this mode */
939             return FALSE;
940         }
941                     
942         if(!cx) cx = s.cx;
943         if(!cy) cy = s.cy;
944     }
945
946     rc.left   = x;
947     rc.top    = y;
948     rc.right  = x + cx;
949     rc.bottom = y + cy;
950
951     if(flags & DSS_RIGHT)    /* This one is not documented in the win32.hlp file */
952         dtflags |= DT_RIGHT;
953     if(opcode == DST_TEXT)
954         dtflags |= DT_NOPREFIX;
955
956     /* For DSS_NORMAL we just jam in the image and return */
957     if((flags & 0x7ff0) == DSS_NORMAL)
958     {
959         return PAINTING_DrawStateJam(hdc, opcode, func, lp, len, &rc, dtflags, unicode, _32bit);
960     }
961
962     /* For all other states we need to convert the image to B/W in a local bitmap */
963     /* before it is displayed */
964     fg = SetTextColor32(hdc, RGB(0, 0, 0));
965     bg = SetBkColor32(hdc, RGB(255, 255, 255));
966     hbm = (HBITMAP32)NULL; hbmsave = (HBITMAP32)NULL;
967     memdc = (HDC32)NULL; hbsave = (HBRUSH32)NULL;
968     retval = FALSE; /* assume failure */
969     
970     /* From here on we must use "goto cleanup" when something goes wrong */
971     hbm     = CreateBitmap32(cx, cy, 1, 1, NULL);
972     if(!hbm) goto cleanup;
973     memdc   = CreateCompatibleDC32(hdc);
974     if(!memdc) goto cleanup;
975     hbmsave = (HBITMAP32)SelectObject32(memdc, hbm);
976     if(!hbmsave) goto cleanup;
977     rc.left = rc.top = 0;
978     rc.right = cx;
979     rc.bottom = cy;
980     if(!FillRect32(memdc, &rc, (HBRUSH32)GetStockObject32(WHITE_BRUSH))) goto cleanup;
981     SetBkColor32(memdc, RGB(255, 255, 255));
982     SetTextColor32(memdc, RGB(0, 0, 0));
983     hfsave  = (HFONT32)SelectObject32(memdc, GetCurrentObject(hdc, OBJ_FONT));
984     if(!hfsave && (opcode == DST_TEXT || opcode == DST_PREFIXTEXT)) goto cleanup;
985     tmp = PAINTING_DrawStateJam(memdc, opcode, func, lp, len, &rc, dtflags, unicode, _32bit);
986     if(hfsave) SelectObject32(memdc, hfsave);
987     if(!tmp) goto cleanup;
988     
989     /* These states cause the image to be dithered */
990     if(flags & (DSS_UNION|DSS_DISABLED))
991     {
992         hbsave = (HBRUSH32)SelectObject32(memdc, CACHE_GetPattern55AABrush());
993         if(!hbsave) goto cleanup;
994         tmp = PatBlt32(memdc, 0, 0, cx, cy, 0x00FA0089);
995         if(hbsave) SelectObject32(memdc, hbsave);
996         if(!tmp) goto cleanup;
997     }
998
999     hbsave = (HBRUSH32)SelectObject32(hdc, hbr ? hbr : GetStockObject32(WHITE_BRUSH));
1000     if(!hbsave) goto cleanup;
1001     
1002     if(!BitBlt32(hdc, x, y, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1003     
1004     /* DSS_DEFAULT makes the image boldface */
1005     if(flags & DSS_DEFAULT)
1006     {
1007         if(!BitBlt32(hdc, x+1, y, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1008     }
1009
1010     retval = TRUE; /* We succeeded */
1011     
1012 cleanup:    
1013     SetTextColor32(hdc, fg);
1014     SetBkColor32(hdc, bg);
1015
1016     if(hbsave)  SelectObject32(hdc, hbsave);
1017     if(hbmsave) SelectObject32(memdc, hbmsave);
1018     if(hbm)     DeleteObject32(hbm);
1019     if(memdc)   DeleteDC32(memdc);
1020
1021     return retval;
1022 }
1023
1024 /**********************************************************************
1025  *      DrawState32A()   (USER32.162)
1026  */
1027 BOOL32 WINAPI DrawState32A(HDC32 hdc, HBRUSH32 hbr,
1028                    DRAWSTATEPROC32 func, LPARAM ldata, WPARAM32 wdata,
1029                    INT32 x, INT32 y, INT32 cx, INT32 cy, UINT32 flags)
1030 {
1031     return PAINTING_DrawState32(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, FALSE, TRUE);
1032 }
1033
1034 /**********************************************************************
1035  *      DrawState32W()   (USER32.163)
1036  */
1037 BOOL32 WINAPI DrawState32W(HDC32 hdc, HBRUSH32 hbr,
1038                    DRAWSTATEPROC32 func, LPARAM ldata, WPARAM32 wdata,
1039                    INT32 x, INT32 y, INT32 cx, INT32 cy, UINT32 flags)
1040 {
1041     return PAINTING_DrawState32(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, TRUE, TRUE);
1042 }
1043
1044 /**********************************************************************
1045  *      DrawState16()   (USER.449)
1046  */
1047 BOOL16 WINAPI DrawState16(HDC16 hdc, HBRUSH16 hbr,
1048                    DRAWSTATEPROC16 func, LPARAM ldata, WPARAM16 wdata,
1049                    INT16 x, INT16 y, INT16 cx, INT16 cy, UINT16 flags)
1050 {
1051     return PAINTING_DrawState32(hdc, hbr, (DRAWSTATEPROC32)func, ldata, wdata, x, y, cx, cy, flags, FALSE, FALSE);
1052 }
1053
1054
1055 /******************************************************************************
1056  * PolyBezier16 [GDI.502]
1057  */
1058 BOOL16 WINAPI PolyBezier16( HDC16 hDc, const POINT16* lppt, INT16 cPoints )
1059 {
1060     int i;
1061     BOOL16 ret;
1062     LPPOINT32 pt32 = (LPPOINT32)HeapAlloc( GetProcessHeap(), 0,
1063                                            cPoints*sizeof(POINT32) );
1064     if(!pt32) return FALSE;
1065     for (i=cPoints;i--;) CONV_POINT16TO32(&(lppt[i]),&(pt32[i]));
1066     ret= PolyBezier32(hDc, pt32, cPoints);
1067     HeapFree( GetProcessHeap(), 0, pt32 );
1068     return ret;
1069 }
1070
1071 /******************************************************************************
1072  * PolyBezierTo16 [GDI.503]
1073  */
1074 BOOL16 WINAPI PolyBezierTo16( HDC16 hDc, const POINT16* lppt, INT16 cPoints )
1075 {
1076     int i;
1077     BOOL16 ret;
1078     LPPOINT32 pt32 = (LPPOINT32)HeapAlloc( GetProcessHeap(), 0,
1079                                            cPoints*sizeof(POINT32) );
1080     if(!pt32) return FALSE;
1081     for (i=cPoints;i--;) CONV_POINT16TO32(&(lppt[i]),&(pt32[i]));
1082     ret= PolyBezierTo32(hDc, pt32, cPoints);
1083     HeapFree( GetProcessHeap(), 0, pt32 );
1084     return ret;
1085 }
1086
1087 /******************************************************************************
1088  * PolyBezier32 [GDI32.268]
1089  * Draws one or more Bezier curves
1090  *
1091  * PARAMS
1092  *    hDc     [I] Handle to device context
1093  *    lppt    [I] Pointer to endpoints and control points
1094  *    cPoints [I] Count of endpoints and control points
1095  *
1096  * RETURNS STD
1097  */
1098 BOOL32 WINAPI PolyBezier32( HDC32 hdc, const POINT32* lppt, DWORD cPoints )
1099 {
1100     DC * dc = DC_GetDCPtr( hdc );
1101     if(!dc) return FALSE;
1102     if(dc && PATH_IsPathOpen(dc->w.path))
1103         FIXME(gdi, "PATH_PolyBezier is not implemented!\n");
1104 /*        if(!PATH_PolyBezier(hdc, x, y))
1105            return FALSE; */
1106     return dc->funcs->pPolyBezier&&
1107            dc->funcs->pPolyBezier(dc, lppt[0], lppt+1, cPoints-1);
1108 }
1109
1110 /******************************************************************************
1111  * PolyBezierTo32 [GDI32.269]
1112  * Draws one or more Bezier curves
1113  *
1114  * PARAMS
1115  *    hDc     [I] Handle to device context
1116  *    lppt    [I] Pointer to endpoints and control points
1117  *    cPoints [I] Count of endpoints and control points
1118  *
1119  * RETURNS STD
1120  */
1121 BOOL32 WINAPI PolyBezierTo32( HDC32 hdc, const POINT32* lppt, DWORD cPoints )
1122 {
1123     DC * dc = DC_GetDCPtr( hdc );
1124     POINT32 pt;
1125     BOOL32 ret;
1126     if(!dc) return FALSE;
1127     pt.x=dc->w.CursPosX;
1128     pt.y=dc->w.CursPosY;
1129     if(dc && PATH_IsPathOpen(dc->w.path))
1130         FIXME(gdi, "PATH_PolyBezierTo is not implemented!\n");
1131 /*        if(!PATH_PolyBezier(hdc, x, y))
1132            return FALSE; */
1133     ret= dc->funcs->pPolyBezier &&
1134            dc->funcs->pPolyBezier(dc, pt, lppt, cPoints);
1135     if( dc->funcs->pMoveToEx)
1136            dc->funcs->pMoveToEx(dc,lppt[cPoints].x,lppt[cPoints].y,&pt);
1137     return ret;
1138 }