gdi32/tests: Fix an uninitialized value check and a structure size for Win64.
[wine] / dlls / gdi32 / palette.c
1 /*
2  * GDI palette objects
3  *
4  * Copyright 1993,1994 Alexandre Julliard
5  * Copyright 1996 Alex Korobka
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  *
21  * NOTES:
22  * PALETTEOBJ is documented in the Dr. Dobbs Journal May 1993.
23  * Information in the "Undocumented Windows" is incorrect.
24  */
25
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "wownt32.h"
34 #include "wine/winuser16.h"
35 #include "gdi_private.h"
36 #include "wine/debug.h"
37 #include "winerror.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(palette);
40
41 typedef struct tagPALETTEOBJ
42 {
43     GDIOBJHDR           header;
44     const DC_FUNCTIONS *funcs;      /* DC function table */
45     LOGPALETTE          logpalette; /* _MUST_ be the last field */
46 } PALETTEOBJ;
47
48 static INT PALETTE_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer );
49 static BOOL PALETTE_UnrealizeObject( HGDIOBJ handle, void *obj );
50 static BOOL PALETTE_DeleteObject( HGDIOBJ handle, void *obj );
51
52 static const struct gdi_obj_funcs palette_funcs =
53 {
54     NULL,                     /* pSelectObject */
55     PALETTE_GetObject,        /* pGetObjectA */
56     PALETTE_GetObject,        /* pGetObjectW */
57     PALETTE_UnrealizeObject,  /* pUnrealizeObject */
58     PALETTE_DeleteObject      /* pDeleteObject */
59 };
60
61 /* Pointers to USER implementation of SelectPalette/RealizePalette */
62 /* they will be patched by USER on startup */
63 HPALETTE (WINAPI *pfnSelectPalette)(HDC hdc, HPALETTE hpal, WORD bkgnd ) = GDISelectPalette;
64 UINT (WINAPI *pfnRealizePalette)(HDC hdc) = GDIRealizePalette;
65
66 static UINT SystemPaletteUse = SYSPAL_STATIC;  /* currently not considered */
67
68 static HPALETTE hPrimaryPalette = 0; /* used for WM_PALETTECHANGED */
69 static HPALETTE hLastRealizedPalette = 0; /* UnrealizeObject() needs it */
70
71 #define NB_RESERVED_COLORS  20   /* number of fixed colors in system palette */
72
73 static const PALETTEENTRY sys_pal_template[NB_RESERVED_COLORS] =
74 {
75     /* first 10 entries in the system palette */
76     /* red  green blue  flags */
77     { 0x00, 0x00, 0x00, 0 },
78     { 0x80, 0x00, 0x00, 0 },
79     { 0x00, 0x80, 0x00, 0 },
80     { 0x80, 0x80, 0x00, 0 },
81     { 0x00, 0x00, 0x80, 0 },
82     { 0x80, 0x00, 0x80, 0 },
83     { 0x00, 0x80, 0x80, 0 },
84     { 0xc0, 0xc0, 0xc0, 0 },
85     { 0xc0, 0xdc, 0xc0, 0 },
86     { 0xa6, 0xca, 0xf0, 0 },
87
88     /* ... c_min/2 dynamic colorcells */
89
90     /* ... gap (for sparse palettes) */
91
92     /* ... c_min/2 dynamic colorcells */
93
94     { 0xff, 0xfb, 0xf0, 0 },
95     { 0xa0, 0xa0, 0xa4, 0 },
96     { 0x80, 0x80, 0x80, 0 },
97     { 0xff, 0x00, 0x00, 0 },
98     { 0x00, 0xff, 0x00, 0 },
99     { 0xff, 0xff, 0x00, 0 },
100     { 0x00, 0x00, 0xff, 0 },
101     { 0xff, 0x00, 0xff, 0 },
102     { 0x00, 0xff, 0xff, 0 },
103     { 0xff, 0xff, 0xff, 0 }     /* last 10 */
104 };
105
106 /***********************************************************************
107  *           PALETTE_Init
108  *
109  * Create the system palette.
110  */
111 HPALETTE PALETTE_Init(void)
112 {
113     HPALETTE          hpalette;
114     LOGPALETTE *        palPtr;
115
116     /* create default palette (20 system colors) */
117
118     palPtr = HeapAlloc( GetProcessHeap(), 0,
119              sizeof(LOGPALETTE) + (NB_RESERVED_COLORS-1)*sizeof(PALETTEENTRY));
120     if (!palPtr) return FALSE;
121
122     palPtr->palVersion = 0x300;
123     palPtr->palNumEntries = NB_RESERVED_COLORS;
124     memcpy( palPtr->palPalEntry, sys_pal_template, sizeof(sys_pal_template) );
125     hpalette = CreatePalette( palPtr );
126     HeapFree( GetProcessHeap(), 0, palPtr );
127     return hpalette;
128 }
129
130
131 /***********************************************************************
132  * CreatePalette [GDI32.@]
133  *
134  * Creates a logical color palette.
135  *
136  * RETURNS
137  *    Success: Handle to logical palette
138  *    Failure: NULL
139  */
140 HPALETTE WINAPI CreatePalette(
141     const LOGPALETTE* palette) /* [in] Pointer to logical color palette */
142 {
143     PALETTEOBJ * palettePtr;
144     HPALETTE hpalette;
145     int size;
146
147     if (!palette) return 0;
148     TRACE("entries=%i\n", palette->palNumEntries);
149
150     size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
151
152     if (!(palettePtr = GDI_AllocObject( size + sizeof(int*) +sizeof(GDIOBJHDR),
153                                         PALETTE_MAGIC, (HGDIOBJ *)&hpalette,
154                                         &palette_funcs ))) return 0;
155     memcpy( &palettePtr->logpalette, palette, size );
156     palettePtr->funcs = NULL;
157     GDI_ReleaseObj( hpalette );
158
159     TRACE("   returning %p\n", hpalette);
160     return hpalette;
161 }
162
163
164 /***********************************************************************
165  * CreateHalftonePalette [GDI32.@]
166  *
167  * Creates a halftone palette.
168  *
169  * RETURNS
170  *    Success: Handle to logical halftone palette
171  *    Failure: 0
172  *
173  * FIXME: This simply creates the halftone palette derived from running
174  *        tests on a windows NT machine. This is assuming a color depth
175  *        of greater that 256 color. On a 256 color device the halftone
176  *        palette will be different and this function will be incorrect
177  */
178 HPALETTE WINAPI CreateHalftonePalette(
179     HDC hdc) /* [in] Handle to device context */
180 {
181     int i;
182     struct {
183         WORD Version;
184         WORD NumberOfEntries;
185         PALETTEENTRY aEntries[256];
186     } Palette;
187
188     Palette.Version = 0x300;
189     Palette.NumberOfEntries = 256;
190     GetSystemPaletteEntries(hdc, 0, 256, Palette.aEntries);
191
192     Palette.NumberOfEntries = 20;
193
194     for (i = 0; i < Palette.NumberOfEntries; i++)
195     {
196         Palette.aEntries[i].peRed=0xff;
197         Palette.aEntries[i].peGreen=0xff;
198         Palette.aEntries[i].peBlue=0xff;
199         Palette.aEntries[i].peFlags=0x00;
200     }
201
202     Palette.aEntries[0].peRed=0x00;
203     Palette.aEntries[0].peBlue=0x00;
204     Palette.aEntries[0].peGreen=0x00;
205
206     /* the first 6 */
207     for (i=1; i <= 6; i++)
208     {
209         Palette.aEntries[i].peRed=(i%2)?0x80:0;
210         Palette.aEntries[i].peGreen=(i==2)?0x80:(i==3)?0x80:(i==6)?0x80:0;
211         Palette.aEntries[i].peBlue=(i>3)?0x80:0;
212     }
213
214     for (i=7;  i <= 12; i++)
215     {
216         switch(i)
217         {
218             case 7:
219                 Palette.aEntries[i].peRed=0xc0;
220                 Palette.aEntries[i].peBlue=0xc0;
221                 Palette.aEntries[i].peGreen=0xc0;
222                 break;
223             case 8:
224                 Palette.aEntries[i].peRed=0xc0;
225                 Palette.aEntries[i].peGreen=0xdc;
226                 Palette.aEntries[i].peBlue=0xc0;
227                 break;
228             case 9:
229                 Palette.aEntries[i].peRed=0xa6;
230                 Palette.aEntries[i].peGreen=0xca;
231                 Palette.aEntries[i].peBlue=0xf0;
232                 break;
233             case 10:
234                 Palette.aEntries[i].peRed=0xff;
235                 Palette.aEntries[i].peGreen=0xfb;
236                 Palette.aEntries[i].peBlue=0xf0;
237                 break;
238             case 11:
239                 Palette.aEntries[i].peRed=0xa0;
240                 Palette.aEntries[i].peGreen=0xa0;
241                 Palette.aEntries[i].peBlue=0xa4;
242                 break;
243             case 12:
244                 Palette.aEntries[i].peRed=0x80;
245                 Palette.aEntries[i].peGreen=0x80;
246                 Palette.aEntries[i].peBlue=0x80;
247         }
248     }
249
250    for (i=13; i <= 18; i++)
251     {
252         Palette.aEntries[i].peRed=(i%2)?0xff:0;
253         Palette.aEntries[i].peGreen=(i==14)?0xff:(i==15)?0xff:(i==18)?0xff:0;
254         Palette.aEntries[i].peBlue=(i>15)?0xff:0x00;
255     }
256
257     return CreatePalette((LOGPALETTE *)&Palette);
258 }
259
260
261 /***********************************************************************
262  * GetPaletteEntries [GDI32.@]
263  *
264  * Retrieves palette entries.
265  *
266  * RETURNS
267  *    Success: Number of entries from logical palette
268  *    Failure: 0
269  */
270 UINT WINAPI GetPaletteEntries(
271     HPALETTE hpalette,    /* [in]  Handle of logical palette */
272     UINT start,           /* [in]  First entry to receive */
273     UINT count,           /* [in]  Number of entries to receive */
274     LPPALETTEENTRY entries) /* [out] Address of array receiving entries */
275 {
276     PALETTEOBJ * palPtr;
277     UINT numEntries;
278
279     TRACE("hpal = %p, count=%i\n", hpalette, count );
280
281     palPtr = GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
282     if (!palPtr) return 0;
283
284     /* NOTE: not documented but test show this to be the case */
285     if (count == 0)
286     {
287         int rc = palPtr->logpalette.palNumEntries;
288             GDI_ReleaseObj( hpalette );
289         return rc;
290     }
291
292     numEntries = palPtr->logpalette.palNumEntries;
293     if (start+count > numEntries) count = numEntries - start;
294     if (entries)
295     {
296       if (start >= numEntries)
297       {
298         GDI_ReleaseObj( hpalette );
299         return 0;
300       }
301       memcpy( entries, &palPtr->logpalette.palPalEntry[start],
302               count * sizeof(PALETTEENTRY) );
303     }
304
305     GDI_ReleaseObj( hpalette );
306     return count;
307 }
308
309
310 /***********************************************************************
311  * SetPaletteEntries [GDI32.@]
312  *
313  * Sets color values for range in palette.
314  *
315  * RETURNS
316  *    Success: Number of entries that were set
317  *    Failure: 0
318  */
319 UINT WINAPI SetPaletteEntries(
320     HPALETTE hpalette,    /* [in] Handle of logical palette */
321     UINT start,           /* [in] Index of first entry to set */
322     UINT count,           /* [in] Number of entries to set */
323     const PALETTEENTRY *entries) /* [in] Address of array of structures */
324 {
325     PALETTEOBJ * palPtr;
326     UINT numEntries;
327
328     TRACE("hpal=%p,start=%i,count=%i\n",hpalette,start,count );
329
330     if (hpalette == GetStockObject(DEFAULT_PALETTE)) return 0;
331     palPtr = GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
332     if (!palPtr) return 0;
333
334     numEntries = palPtr->logpalette.palNumEntries;
335     if (start >= numEntries)
336     {
337       GDI_ReleaseObj( hpalette );
338       return 0;
339     }
340     if (start+count > numEntries) count = numEntries - start;
341     memcpy( &palPtr->logpalette.palPalEntry[start], entries,
342             count * sizeof(PALETTEENTRY) );
343     UnrealizeObject( hpalette );
344     GDI_ReleaseObj( hpalette );
345     return count;
346 }
347
348
349 /***********************************************************************
350  * ResizePalette [GDI32.@]
351  *
352  * Resizes logical palette.
353  *
354  * RETURNS
355  *    Success: TRUE
356  *    Failure: FALSE
357  */
358 BOOL WINAPI ResizePalette(
359     HPALETTE hPal, /* [in] Handle of logical palette */
360     UINT cEntries) /* [in] Number of entries in logical palette */
361 {
362     PALETTEOBJ * palPtr = GDI_GetObjPtr( hPal, PALETTE_MAGIC );
363     UINT         cPrevEnt, prevVer;
364     int          prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
365
366     TRACE("hpal = %p, prev = %i, new = %i\n",
367           hPal, palPtr ? palPtr->logpalette.palNumEntries : -1, cEntries );
368     if( !palPtr ) return FALSE;
369     cPrevEnt = palPtr->logpalette.palNumEntries;
370     prevVer = palPtr->logpalette.palVersion;
371     prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) +
372                                         sizeof(int*) + sizeof(GDIOBJHDR);
373     size += sizeof(int*) + sizeof(GDIOBJHDR);
374
375     if (!(palPtr = GDI_ReallocObject( size, hPal, palPtr ))) return FALSE;
376
377     PALETTE_UnrealizeObject( hPal, palPtr );
378
379     if( cEntries > cPrevEnt ) memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
380     palPtr->logpalette.palNumEntries = cEntries;
381     palPtr->logpalette.palVersion = prevVer;
382     GDI_ReleaseObj( hPal );
383     return TRUE;
384 }
385
386
387 /***********************************************************************
388  * AnimatePalette [GDI32.@]
389  *
390  * Replaces entries in logical palette.
391  *
392  * RETURNS
393  *    Success: TRUE
394  *    Failure: FALSE
395  *
396  * FIXME
397  *    Should use existing mapping when animating a primary palette
398  */
399 BOOL WINAPI AnimatePalette(
400     HPALETTE hPal,              /* [in] Handle to logical palette */
401     UINT StartIndex,            /* [in] First entry in palette */
402     UINT NumEntries,            /* [in] Count of entries in palette */
403     const PALETTEENTRY* PaletteColors) /* [in] Pointer to first replacement */
404 {
405     TRACE("%p (%i - %i)\n", hPal, StartIndex,StartIndex+NumEntries);
406
407     if( hPal != GetStockObject(DEFAULT_PALETTE) )
408     {
409         PALETTEOBJ * palPtr;
410         UINT pal_entries;
411         const PALETTEENTRY *pptr = PaletteColors;
412
413         palPtr = GDI_GetObjPtr( hPal, PALETTE_MAGIC );
414         if (!palPtr) return 0;
415
416         pal_entries = palPtr->logpalette.palNumEntries;
417         if (StartIndex >= pal_entries)
418         {
419           GDI_ReleaseObj( hPal );
420           return 0;
421         }
422         if (StartIndex+NumEntries > pal_entries) NumEntries = pal_entries - StartIndex;
423         
424         for (NumEntries += StartIndex; StartIndex < NumEntries; StartIndex++, pptr++) {
425           /* According to MSDN, only animate PC_RESERVED colours */
426           if (palPtr->logpalette.palPalEntry[StartIndex].peFlags & PC_RESERVED) {
427             TRACE("Animating colour (%d,%d,%d) to (%d,%d,%d)\n",
428               palPtr->logpalette.palPalEntry[StartIndex].peRed,
429               palPtr->logpalette.palPalEntry[StartIndex].peGreen,
430               palPtr->logpalette.palPalEntry[StartIndex].peBlue,
431               pptr->peRed, pptr->peGreen, pptr->peBlue);
432             memcpy( &palPtr->logpalette.palPalEntry[StartIndex], pptr,
433                     sizeof(PALETTEENTRY) );
434           } else {
435             TRACE("Not animating entry %d -- not PC_RESERVED\n", StartIndex);
436           }
437         }
438         if (palPtr->funcs && palPtr->funcs->pRealizePalette)
439             palPtr->funcs->pRealizePalette( NULL, hPal, hPal == hPrimaryPalette );
440
441         GDI_ReleaseObj( hPal );
442     }
443     return TRUE;
444 }
445
446
447 /***********************************************************************
448  * SetSystemPaletteUse [GDI32.@]
449  *
450  * Specify whether the system palette contains 2 or 20 static colors.
451  *
452  * RETURNS
453  *    Success: Previous system palette
454  *    Failure: SYSPAL_ERROR
455  */
456 UINT WINAPI SetSystemPaletteUse(
457     HDC hdc,  /* [in] Handle of device context */
458     UINT use) /* [in] Palette-usage flag */
459 {
460     UINT old = SystemPaletteUse;
461
462     /* Device doesn't support colour palettes */
463     if (!(GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)) {
464         return SYSPAL_ERROR;
465     }
466
467     switch (use) {
468         case SYSPAL_NOSTATIC:
469         case SYSPAL_NOSTATIC256:        /* WINVER >= 0x0500 */
470         case SYSPAL_STATIC:
471             SystemPaletteUse = use;
472             return old;
473         default:
474             return SYSPAL_ERROR;
475     }
476 }
477
478
479 /***********************************************************************
480  * GetSystemPaletteUse [GDI32.@]
481  *
482  * Gets state of system palette.
483  *
484  * RETURNS
485  *    Current state of system palette
486  */
487 UINT WINAPI GetSystemPaletteUse(
488     HDC hdc) /* [in] Handle of device context */
489 {
490     return SystemPaletteUse;
491 }
492
493
494 /***********************************************************************
495  * GetSystemPaletteEntries [GDI32.@]
496  *
497  * Gets range of palette entries.
498  *
499  * RETURNS
500  *    Success: Number of entries retrieved from palette
501  *    Failure: 0
502  */
503 UINT WINAPI GetSystemPaletteEntries(
504     HDC hdc,              /* [in]  Handle of device context */
505     UINT start,           /* [in]  Index of first entry to be retrieved */
506     UINT count,           /* [in]  Number of entries to be retrieved */
507     LPPALETTEENTRY entries) /* [out] Array receiving system-palette entries */
508 {
509     UINT ret = 0;
510     DC *dc;
511
512     TRACE("hdc=%p,start=%i,count=%i\n", hdc,start,count);
513
514     if ((dc = get_dc_ptr( hdc )))
515     {
516         if (dc->funcs->pGetSystemPaletteEntries)
517             ret = dc->funcs->pGetSystemPaletteEntries( dc->physDev, start, count, entries );
518         release_dc_ptr( dc );
519     }
520     return ret;
521 }
522
523
524 /***********************************************************************
525  * GetNearestPaletteIndex [GDI32.@]
526  *
527  * Gets palette index for color.
528  *
529  * NOTES
530  *    Should index be initialized to CLR_INVALID instead of 0?
531  *
532  * RETURNS
533  *    Success: Index of entry in logical palette
534  *    Failure: CLR_INVALID
535  */
536 UINT WINAPI GetNearestPaletteIndex(
537     HPALETTE hpalette, /* [in] Handle of logical color palette */
538     COLORREF color)      /* [in] Color to be matched */
539 {
540     PALETTEOBJ* palObj = GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
541     UINT index  = 0;
542
543     if( palObj )
544     {
545         int i, diff = 0x7fffffff;
546         int r,g,b;
547         PALETTEENTRY* entry = palObj->logpalette.palPalEntry;
548
549         for( i = 0; i < palObj->logpalette.palNumEntries && diff ; i++, entry++)
550         {
551             r = entry->peRed - GetRValue(color);
552             g = entry->peGreen - GetGValue(color);
553             b = entry->peBlue - GetBValue(color);
554
555             r = r*r + g*g + b*b;
556
557             if( r < diff ) { index = i; diff = r; }
558         }
559         GDI_ReleaseObj( hpalette );
560     }
561     TRACE("(%p,%06x): returning %d\n", hpalette, color, index );
562     return index;
563 }
564
565
566 /***********************************************************************
567  * GetNearestColor [GDI32.@]
568  *
569  * Gets a system color to match.
570  *
571  * RETURNS
572  *    Success: Color from system palette that corresponds to given color
573  *    Failure: CLR_INVALID
574  */
575 COLORREF WINAPI GetNearestColor(
576     HDC hdc,      /* [in] Handle of device context */
577     COLORREF color) /* [in] Color to be matched */
578 {
579     unsigned char spec_type;
580     COLORREF nearest;
581     DC          *dc;
582
583     if (!(dc = get_dc_ptr( hdc ))) return CLR_INVALID;
584
585     if (dc->funcs->pGetNearestColor)
586     {
587         nearest = dc->funcs->pGetNearestColor( dc->physDev, color );
588         release_dc_ptr( dc );
589         return nearest;
590     }
591
592     if (!(GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE))
593     {
594         release_dc_ptr( dc );
595         return color;
596     }
597
598     spec_type = color >> 24;
599     if (spec_type == 1 || spec_type == 2)
600     {
601         /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
602
603         UINT index;
604         PALETTEENTRY entry;
605         HPALETTE hpal = dc->hPalette ? dc->hPalette : GetStockObject( DEFAULT_PALETTE );
606
607         if (spec_type == 2) /* PALETTERGB */
608             index = GetNearestPaletteIndex( hpal, color );
609         else  /* PALETTEINDEX */
610             index = LOWORD(color);
611
612         if (!GetPaletteEntries( hpal, index, 1, &entry ))
613         {
614             WARN("RGB(%x) : idx %d is out of bounds, assuming NULL\n", color, index );
615             if (!GetPaletteEntries( hpal, 0, 1, &entry ))
616             {
617                 release_dc_ptr( dc );
618                 return CLR_INVALID;
619             }
620         }
621         color = RGB( entry.peRed, entry.peGreen, entry.peBlue );
622     }
623     nearest = color & 0x00ffffff;
624     release_dc_ptr( dc );
625
626     TRACE("(%06x): returning %06x\n", color, nearest );
627     return nearest;
628 }
629
630
631 /***********************************************************************
632  *           PALETTE_GetObject
633  */
634 static INT PALETTE_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
635 {
636     PALETTEOBJ *palette = obj;
637
638     if( !buffer )
639         return sizeof(WORD);
640
641     if (count > sizeof(WORD)) count = sizeof(WORD);
642     memcpy( buffer, &palette->logpalette.palNumEntries, count );
643     return count;
644 }
645
646
647 /***********************************************************************
648  *           PALETTE_UnrealizeObject
649  */
650 static BOOL PALETTE_UnrealizeObject( HGDIOBJ handle, void *obj )
651 {
652     PALETTEOBJ *palette = obj;
653
654     if (palette->funcs)
655     {
656         if (palette->funcs->pUnrealizePalette)
657             palette->funcs->pUnrealizePalette( handle );
658         palette->funcs = NULL;
659     }
660
661     if (InterlockedCompareExchangePointer( (void **)&hLastRealizedPalette, 0, handle ) == handle)
662         TRACE("unrealizing palette %p\n", handle);
663
664     return TRUE;
665 }
666
667
668 /***********************************************************************
669  *           PALETTE_DeleteObject
670  */
671 static BOOL PALETTE_DeleteObject( HGDIOBJ handle, void *obj )
672 {
673     PALETTE_UnrealizeObject( handle, obj );
674     return GDI_FreeObject( handle, obj );
675 }
676
677
678 /***********************************************************************
679  *           GDISelectPalette    (Not a Windows API)
680  */
681 HPALETTE WINAPI GDISelectPalette( HDC hdc, HPALETTE hpal, WORD wBkg)
682 {
683     HPALETTE ret;
684     DC *dc;
685
686     TRACE("%p %p\n", hdc, hpal );
687
688     if (GetObjectType(hpal) != OBJ_PAL)
689     {
690       WARN("invalid selected palette %p\n",hpal);
691       return 0;
692     }
693     if (!(dc = get_dc_ptr( hdc ))) return 0;
694     ret = dc->hPalette;
695     if (dc->funcs->pSelectPalette) hpal = dc->funcs->pSelectPalette( dc->physDev, hpal, FALSE );
696     if (hpal)
697     {
698         dc->hPalette = hpal;
699         if (!wBkg) hPrimaryPalette = hpal;
700     }
701     else ret = 0;
702     release_dc_ptr( dc );
703     return ret;
704 }
705
706
707 /***********************************************************************
708  *           GDIRealizePalette    (Not a Windows API)
709  */
710 UINT WINAPI GDIRealizePalette( HDC hdc )
711 {
712     UINT realized = 0;
713     DC* dc = get_dc_ptr( hdc );
714
715     if (!dc) return 0;
716
717     TRACE("%p...\n", hdc );
718
719     if( dc->hPalette == GetStockObject( DEFAULT_PALETTE ))
720     {
721         if (dc->funcs->pRealizeDefaultPalette)
722             realized = dc->funcs->pRealizeDefaultPalette( dc->physDev );
723     }
724     else if (InterlockedExchangePointer( (void **)&hLastRealizedPalette, dc->hPalette ) != dc->hPalette)
725     {
726         if (dc->funcs->pRealizePalette)
727         {
728             PALETTEOBJ *palPtr = GDI_GetObjPtr( dc->hPalette, PALETTE_MAGIC );
729             if (palPtr)
730             {
731                 realized = dc->funcs->pRealizePalette( dc->physDev, dc->hPalette,
732                                                        (dc->hPalette == hPrimaryPalette) );
733                 palPtr->funcs = dc->funcs;
734                 GDI_ReleaseObj( dc->hPalette );
735             }
736         }
737     }
738     else TRACE("  skipping (hLastRealizedPalette = %p)\n", hLastRealizedPalette);
739
740     release_dc_ptr( dc );
741     TRACE("   realized %i colors.\n", realized );
742     return realized;
743 }
744
745
746 /***********************************************************************
747  *           RealizeDefaultPalette    (GDI.365)
748  */
749 UINT16 WINAPI RealizeDefaultPalette16( HDC16 hdc )
750 {
751     UINT16 ret = 0;
752     DC          *dc;
753
754     TRACE("%04x\n", hdc );
755
756     if (!(dc = get_dc_ptr( HDC_32(hdc) ))) return 0;
757
758     if (dc->funcs->pRealizeDefaultPalette) ret = dc->funcs->pRealizeDefaultPalette( dc->physDev );
759     release_dc_ptr( dc );
760     return ret;
761 }
762
763 /***********************************************************************
764  *           IsDCCurrentPalette   (GDI.412)
765  */
766 BOOL16 WINAPI IsDCCurrentPalette16(HDC16 hDC)
767 {
768     DC *dc = get_dc_ptr( HDC_32(hDC) );
769     if (dc)
770     {
771       BOOL bRet = dc->hPalette == hPrimaryPalette;
772       release_dc_ptr( dc );
773       return bRet;
774     }
775     return FALSE;
776 }
777
778
779 /***********************************************************************
780  * SelectPalette [GDI32.@]
781  *
782  * Selects logical palette into DC.
783  *
784  * RETURNS
785  *    Success: Previous logical palette
786  *    Failure: NULL
787  */
788 HPALETTE WINAPI SelectPalette(
789     HDC hDC,               /* [in] Handle of device context */
790     HPALETTE hPal,         /* [in] Handle of logical color palette */
791     BOOL bForceBackground) /* [in] Foreground/background mode */
792 {
793     return pfnSelectPalette( hDC, hPal, bForceBackground );
794 }
795
796
797 /***********************************************************************
798  * RealizePalette [GDI32.@]
799  *
800  * Maps palette entries to system palette.
801  *
802  * RETURNS
803  *    Success: Number of entries in logical palette
804  *    Failure: GDI_ERROR
805  */
806 UINT WINAPI RealizePalette(
807     HDC hDC) /* [in] Handle of device context */
808 {
809     return pfnRealizePalette( hDC );
810 }
811
812
813 typedef HWND (WINAPI *WindowFromDC_funcptr)( HDC );
814 typedef BOOL (WINAPI *RedrawWindow_funcptr)( HWND, const RECT *, HRGN, UINT );
815
816 /**********************************************************************
817  * UpdateColors [GDI32.@]
818  *
819  * Remaps current colors to logical palette.
820  *
821  * RETURNS
822  *    Success: TRUE
823  *    Failure: FALSE
824  */
825 BOOL WINAPI UpdateColors(
826     HDC hDC) /* [in] Handle of device context */
827 {
828     HMODULE mod;
829     int size = GetDeviceCaps( hDC, SIZEPALETTE );
830
831     if (!size) return 0;
832
833     mod = GetModuleHandleA("user32.dll");
834     if (mod)
835     {
836         WindowFromDC_funcptr pWindowFromDC = (WindowFromDC_funcptr)GetProcAddress(mod,"WindowFromDC");
837         if (pWindowFromDC)
838         {
839             HWND hWnd = pWindowFromDC( hDC );
840
841             /* Docs say that we have to remap current drawable pixel by pixel
842              * but it would take forever given the speed of XGet/PutPixel.
843              */
844             if (hWnd && size)
845             {
846                 RedrawWindow_funcptr pRedrawWindow = (void *)GetProcAddress( mod, "RedrawWindow" );
847                 if (pRedrawWindow) pRedrawWindow( hWnd, NULL, 0, RDW_INVALIDATE );
848             }
849         }
850     }
851     return 0x666;
852 }
853
854
855 /*********************************************************************
856  *           SetMagicColors   (GDI.606)
857  */
858 VOID WINAPI SetMagicColors16(HDC16 hDC, COLORREF color, UINT16 index)
859 {
860     FIXME("(hDC %04x, color %04x, index %04x): stub\n", hDC, (int)color, index);
861
862 }
863
864 /*********************************************************************
865  *           SetMagicColors   (GDI32.@)
866  */
867 BOOL WINAPI SetMagicColors(HDC hdc, ULONG u1, ULONG u2)
868 {
869     FIXME("(%p 0x%08x 0x%08x): stub\n", hdc, u1, u2);
870     return TRUE;
871 }