Moved some more code to the X11 driver.
[wine] / objects / palette.c
1 /*
2  * GDI palette objects
3  *
4  * Copyright 1993,1994 Alexandre Julliard
5  * Copyright 1996 Alex Korobka
6  *
7  * PALETTEOBJ is documented in the Dr. Dobbs Journal May 1993.
8  * Information in the "Undocumented Windows" is incorrect.
9  */
10
11 #include <stdlib.h>
12 #include <string.h>
13 #include "gdi.h"
14 #include "color.h"
15 #include "palette.h"
16 #include "xmalloc.h"
17 #include "debug.h"
18 #include "wine/winuser16.h"
19
20 FARPROC32 pfnSelectPalette = NULL;
21 FARPROC32 pfnRealizePalette = NULL;
22
23 static UINT32 SystemPaletteUse = SYSPAL_STATIC;  /* currently not considered */
24
25 static HPALETTE16 hPrimaryPalette = 0; /* used for WM_PALETTECHANGED */
26 static HPALETTE16 hLastRealizedPalette = 0; /* UnrealizeObject() needs it */
27
28
29 /***********************************************************************
30  *           PALETTE_Init
31  *
32  * Create the system palette.
33  */
34 HPALETTE16 PALETTE_Init(void)
35 {
36     int                 i;
37     HPALETTE16          hpalette;
38     LOGPALETTE *        palPtr;
39     PALETTEOBJ*         palObj;
40     const PALETTEENTRY* __sysPalTemplate = COLOR_GetSystemPaletteTemplate();
41
42     /* create default palette (20 system colors) */
43
44     palPtr = HeapAlloc( GetProcessHeap(), 0,
45              sizeof(LOGPALETTE) + (NB_RESERVED_COLORS-1)*sizeof(PALETTEENTRY));
46     if (!palPtr) return FALSE;
47
48     palPtr->palVersion = 0x300;
49     palPtr->palNumEntries = NB_RESERVED_COLORS;
50     for( i = 0; i < NB_RESERVED_COLORS; i ++ )
51     {
52         palPtr->palPalEntry[i].peRed = __sysPalTemplate[i].peRed;
53         palPtr->palPalEntry[i].peGreen = __sysPalTemplate[i].peGreen;
54         palPtr->palPalEntry[i].peBlue = __sysPalTemplate[i].peBlue;
55         palPtr->palPalEntry[i].peFlags = 0;
56     }
57     hpalette = CreatePalette16( palPtr );
58
59     palObj = (PALETTEOBJ*) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
60     if (palObj)
61     {
62         palObj->mapping = xmalloc( sizeof(int) * 20 );
63
64         GDI_HEAP_UNLOCK( hpalette );
65
66         HeapFree( GetProcessHeap(), 0, palPtr );
67     }
68         
69     return hpalette;
70 }
71
72 /***********************************************************************
73  *           PALETTE_ValidateFlags
74  */
75 void PALETTE_ValidateFlags(PALETTEENTRY* lpPalE, int size)
76 {
77     int i = 0;
78     for( ; i<size ; i++ )
79         lpPalE[i].peFlags = PC_SYS_USED | (lpPalE[i].peFlags & 0x07);
80 }
81
82
83 /***********************************************************************
84  *           CreatePalette16    (GDI.360)
85  */
86 HPALETTE16 WINAPI CreatePalette16( const LOGPALETTE* palette )
87 {
88     return CreatePalette32( palette );
89 }
90
91
92 /***********************************************************************
93  * CreatePalette32 [GDI32.53]  Creates a logical color palette
94  *
95  * RETURNS
96  *    Success: Handle to logical palette
97  *    Failure: NULL
98  */
99 HPALETTE32 WINAPI CreatePalette32(
100     const LOGPALETTE* palette) /* [in] Pointer to logical color palette */
101 {
102     PALETTEOBJ * palettePtr;
103     HPALETTE32 hpalette;
104     int size;
105     
106     if (!palette) return 0;
107     TRACE(palette,"entries=%i\n", palette->palNumEntries);
108
109     size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
110
111     hpalette = GDI_AllocObject( size + sizeof(int*) +sizeof(GDIOBJHDR) , PALETTE_MAGIC );
112     if (!hpalette) return 0;
113
114     palettePtr = (PALETTEOBJ *) GDI_HEAP_LOCK( hpalette );
115     memcpy( &palettePtr->logpalette, palette, size );
116     PALETTE_ValidateFlags(palettePtr->logpalette.palPalEntry, 
117                           palettePtr->logpalette.palNumEntries);
118     palettePtr->mapping = NULL;
119     GDI_HEAP_UNLOCK( hpalette );
120
121     TRACE(palette,"   returning %04x\n", hpalette);
122     return hpalette;
123 }
124
125
126 /***********************************************************************
127  * CreateHalftonePalette [GDI32.47]  Creates a halftone palette
128  *
129  * RETURNS
130  *    Success: Handle to logical halftone palette
131  *    Failure: 0
132  *
133  * FIXME: not truly tested
134  */
135 HPALETTE32 WINAPI CreateHalftonePalette(HDC32 hdc) /* [in] Handle to device context */
136 {       int r,g,b,i;
137         HPALETTE32 hPalette = 0;
138         int palNumEntries = 216 + NB_RESERVED_COLORS;
139         
140         const PALETTEENTRY* __sysPalTemplate = COLOR_GetSystemPaletteTemplate();
141
142         LOGPALETTE * pLogPal = (LOGPALETTE*) HeapAlloc( GetProcessHeap(), 0,
143              sizeof(LOGPALETTE) + (palNumEntries-1)*sizeof(PALETTEENTRY)); 
144         
145         TRACE(palette,"(0x%x)\n", hdc);
146
147         pLogPal->palVersion = 0x300;
148         pLogPal->palNumEntries = palNumEntries;
149         
150         for( i = 0; i < NB_RESERVED_COLORS; i ++ )
151         { pLogPal->palPalEntry[i].peRed   = __sysPalTemplate[i].peRed;
152           pLogPal->palPalEntry[i].peGreen = __sysPalTemplate[i].peGreen;
153           pLogPal->palPalEntry[i].peBlue  = __sysPalTemplate[i].peBlue;
154           pLogPal->palPalEntry[i].peFlags = 0;
155         }
156         
157         for (r=0; r<6; r++)
158         { for (g=0; g<6; g++)
159           { for (b=0; b<6; b++) 
160             { pLogPal->palPalEntry[NB_RESERVED_COLORS+r*36+g*6+b].peRed   = r*51;
161               pLogPal->palPalEntry[NB_RESERVED_COLORS+r*36+g*6+b].peGreen = g*51;
162               pLogPal->palPalEntry[NB_RESERVED_COLORS+r*36+g*6+b].peBlue  = b*51;
163               pLogPal->palPalEntry[NB_RESERVED_COLORS+r*36+g*6+b].peFlags = 0;
164             }    
165           }
166         }
167         hPalette = CreatePalette32 (pLogPal);
168         
169         if (hPalette)
170         { SelectPalette32 (hdc, hPalette, FALSE);
171         }
172         
173         HeapFree (GetProcessHeap(), 0, pLogPal);
174         return hPalette;
175 }
176
177
178 /***********************************************************************
179  *           GetPaletteEntries16    (GDI.363)
180  */
181 UINT16 WINAPI GetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
182                                    UINT16 count, LPPALETTEENTRY entries )
183 {
184     return GetPaletteEntries32( hpalette, start, count, entries );
185 }
186
187
188 /***********************************************************************
189  * GetPaletteEntries32 [GDI32.209]  Retrieves palette entries
190  *
191  * RETURNS
192  *    Success: Number of entries from logical palette
193  *    Failure: 0
194  */
195 UINT32 WINAPI GetPaletteEntries32(
196     HPALETTE32 hpalette,    /* [in]  Handle of logical palette */
197     UINT32 start,           /* [in]  First entry to receive */
198     UINT32 count,           /* [in]  Number of entries to receive */
199     LPPALETTEENTRY entries) /* [out] Address of array receiving entries */
200 {
201     PALETTEOBJ * palPtr;
202     INT32 numEntries;
203
204     TRACE(palette,"hpal = %04x, count=%i\n", hpalette, count );
205         
206     palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
207     if (!palPtr) return 0;
208
209     numEntries = palPtr->logpalette.palNumEntries;
210     if (start+count > numEntries) count = numEntries - start;
211     if (entries)
212     { 
213       if (start >= numEntries) 
214       {
215         GDI_HEAP_UNLOCK( hpalette );
216         return 0;
217       }
218       memcpy( entries, &palPtr->logpalette.palPalEntry[start],
219               count * sizeof(PALETTEENTRY) );
220       for( numEntries = 0; numEntries < count ; numEntries++ )
221            if (entries[numEntries].peFlags & 0xF0)
222                entries[numEntries].peFlags = 0;
223       GDI_HEAP_UNLOCK( hpalette );
224     }
225
226     return count;
227 }
228
229
230 /***********************************************************************
231  *           SetPaletteEntries16    (GDI.364)
232  */
233 UINT16 WINAPI SetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
234                                    UINT16 count, LPPALETTEENTRY entries )
235 {
236     return SetPaletteEntries32( hpalette, start, count, entries );
237 }
238
239
240 /***********************************************************************
241  * SetPaletteEntries32 [GDI32.326]  Sets color values for range in palette
242  *
243  * RETURNS
244  *    Success: Number of entries that were set
245  *    Failure: 0
246  */
247 UINT32 WINAPI SetPaletteEntries32(
248     HPALETTE32 hpalette,    /* [in] Handle of logical palette */
249     UINT32 start,           /* [in] Index of first entry to set */
250     UINT32 count,           /* [in] Number of entries to set */
251     LPPALETTEENTRY entries) /* [in] Address of array of structures */
252 {
253     PALETTEOBJ * palPtr;
254     INT32 numEntries;
255
256     TRACE(palette,"hpal=%04x,start=%i,count=%i\n",hpalette,start,count );
257
258     palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
259     if (!palPtr) return 0;
260
261     numEntries = palPtr->logpalette.palNumEntries;
262     if (start >= numEntries) 
263     {
264       GDI_HEAP_UNLOCK( hpalette );
265       return 0;
266     }
267     if (start+count > numEntries) count = numEntries - start;
268     memcpy( &palPtr->logpalette.palPalEntry[start], entries,
269             count * sizeof(PALETTEENTRY) );
270     PALETTE_ValidateFlags(palPtr->logpalette.palPalEntry, 
271                           palPtr->logpalette.palNumEntries);
272     free(palPtr->mapping);
273     palPtr->mapping = NULL;
274     GDI_HEAP_UNLOCK( hpalette );
275     return count;
276 }
277
278
279 /***********************************************************************
280  *           ResizePalette16   (GDI.368)
281  */
282 BOOL16 WINAPI ResizePalette16( HPALETTE16 hPal, UINT16 cEntries )
283 {
284     return ResizePalette32( hPal, cEntries );
285 }
286
287
288 /***********************************************************************
289  * ResizePalette32 [GDI32.289]  Resizes logical palette
290  *
291  * RETURNS
292  *    Success: TRUE
293  *    Failure: FALSE
294  */
295 BOOL32 WINAPI ResizePalette32(
296     HPALETTE32 hPal, /* [in] Handle of logical palette */
297     UINT32 cEntries) /* [in] Number of entries in logical palette */
298 {
299     PALETTEOBJ * palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
300     UINT32       cPrevEnt, prevVer;
301     int          prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
302     int*         mapping = NULL;
303
304     TRACE(palette,"hpal = %04x, prev = %i, new = %i\n",
305                     hPal, palPtr ? palPtr->logpalette.palNumEntries : -1,
306                     cEntries );
307     if( !palPtr ) return FALSE;
308     cPrevEnt = palPtr->logpalette.palNumEntries;
309     prevVer = palPtr->logpalette.palVersion;
310     prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) +
311                                         sizeof(int*) + sizeof(GDIOBJHDR);
312     size += sizeof(int*) + sizeof(GDIOBJHDR);
313     mapping = palPtr->mapping;
314     
315     GDI_HEAP_UNLOCK( hPal );
316     
317     hPal = GDI_HEAP_REALLOC( hPal, size );
318     palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
319     if( !palPtr ) return FALSE;
320
321     if( mapping )
322         palPtr->mapping = (int*) xrealloc( mapping, cEntries * sizeof(int) );
323     if( cEntries > cPrevEnt ) 
324     {
325         if( mapping )
326             memset(palPtr->mapping + cPrevEnt, 0, (cEntries - cPrevEnt)*sizeof(int));
327         memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
328         PALETTE_ValidateFlags((PALETTEENTRY*)((BYTE*)palPtr + prevsize), 
329                                                      cEntries - cPrevEnt );
330     }
331     palPtr->logpalette.palNumEntries = cEntries;
332     palPtr->logpalette.palVersion = prevVer;
333     GDI_HEAP_UNLOCK( hPal );
334     return TRUE;
335 }
336
337
338 /***********************************************************************
339  *           AnimatePalette16   (GDI.367)
340  */
341 void WINAPI AnimatePalette16( HPALETTE16 hPal, UINT16 StartIndex,
342                               UINT16 NumEntries, const PALETTEENTRY* PaletteColors)
343 {
344     AnimatePalette32( hPal, StartIndex, NumEntries, PaletteColors );
345 }
346
347
348 /***********************************************************************
349  * AnimatePalette32 [GDI32.6]  Replaces entries in logical palette
350  *
351  * RETURNS
352  *    Success: TRUE
353  *    Failure: FALSE
354  *
355  * FIXME
356  *    Should use existing mapping when animating a primary palette
357  */
358 BOOL32 WINAPI AnimatePalette32(
359     HPALETTE32 hPal,              /* [in] Handle to logical palette */
360     UINT32 StartIndex,            /* [in] First entry in palette */
361     UINT32 NumEntries,            /* [in] Count of entries in palette */
362     const PALETTEENTRY* PaletteColors) /* [in] Pointer to first replacement */
363 {
364     TRACE(palette, "%04x (%i - %i)\n", hPal, StartIndex,StartIndex+NumEntries);
365
366     if( hPal != STOCK_DEFAULT_PALETTE ) 
367     {
368         PALETTEOBJ* palPtr = (PALETTEOBJ *)GDI_GetObjPtr(hPal, PALETTE_MAGIC);
369         if (!palPtr) return FALSE;
370
371         if( (StartIndex + NumEntries) <= palPtr->logpalette.palNumEntries )
372         {
373             UINT32 u;
374             for( u = 0; u < NumEntries; u++ )
375                 palPtr->logpalette.palPalEntry[u + StartIndex] = PaletteColors[u];
376             COLOR_SetMapping(palPtr, StartIndex, NumEntries,
377                              hPal != hPrimaryPalette );
378             GDI_HEAP_UNLOCK( hPal );
379             return TRUE;
380         }
381     }
382     return FALSE;
383 }
384
385
386 /***********************************************************************
387  *           SetSystemPaletteUse16   (GDI.373)
388  */
389 UINT16 WINAPI SetSystemPaletteUse16( HDC16 hdc, UINT16 use )
390 {
391     return SetSystemPaletteUse32( hdc, use );
392 }
393
394
395 /***********************************************************************
396  * SetSystemPaletteUse32 [GDI32.335]
397  *
398  * RETURNS
399  *    Success: Previous system palette
400  *    Failure: SYSPAL_ERROR
401  */
402 UINT32 WINAPI SetSystemPaletteUse32(
403     HDC32 hdc,  /* [in] Handle of device context */
404     UINT32 use) /* [in] Palette-usage flag */
405 {
406     UINT32 old = SystemPaletteUse;
407     FIXME(palette,"(%04x,%04x): stub\n", hdc, use );
408     SystemPaletteUse = use;
409     return old;
410 }
411
412
413 /***********************************************************************
414  *           GetSystemPaletteUse16   (GDI.374)
415  */
416 UINT16 WINAPI GetSystemPaletteUse16( HDC16 hdc )
417 {
418     return SystemPaletteUse;
419 }
420
421
422 /***********************************************************************
423  * GetSystemPaletteUse32 [GDI32.223]  Gets state of system palette
424  *
425  * RETURNS
426  *    Current state of system palette
427  */
428 UINT32 WINAPI GetSystemPaletteUse32(
429     HDC32 hdc) /* [in] Handle of device context */
430 {
431     return SystemPaletteUse;
432 }
433
434
435 /***********************************************************************
436  *           GetSystemPaletteEntries16   (GDI.375)
437  */
438 UINT16 WINAPI GetSystemPaletteEntries16( HDC16 hdc, UINT16 start, UINT16 count,
439                                          LPPALETTEENTRY entries )
440 {
441     return GetSystemPaletteEntries32( hdc, start, count, entries );
442 }
443
444
445 /***********************************************************************
446  * GetSystemPaletteEntries32 [GDI32.222]  Gets range of palette entries
447  *
448  * RETURNS
449  *    Success: Number of entries retrieved from palette
450  *    Failure: 0
451  */
452 UINT32 WINAPI GetSystemPaletteEntries32(
453     HDC32 hdc,              /* [in]  Handle of device context */
454     UINT32 start,           /* [in]  Index of first entry to be retrieved */
455     UINT32 count,           /* [in]  Number of entries to be retrieved */
456     LPPALETTEENTRY entries) /* [out] Array receiving system-palette entries */
457 {
458     UINT32 i;
459     DC *dc;
460
461     TRACE(palette, "hdc=%04x,start=%i,count=%i\n", hdc,start,count);
462
463     if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
464     if (!entries) return COLOR_GetSystemPaletteSize();
465     if (start >= dc->w.devCaps->sizePalette)
466       {
467         GDI_HEAP_UNLOCK( hdc );
468         return 0;
469       }
470     if (start+count >= dc->w.devCaps->sizePalette)
471         count = dc->w.devCaps->sizePalette - start;
472     for (i = 0; i < count; i++)
473     {
474         *(COLORREF*)(entries + i) = COLOR_GetSystemPaletteEntry( start + i );
475
476         TRACE(palette,"\tidx(%02x) -> RGB(%08lx)\n",
477                          start + i, *(COLORREF*)(entries + i) );
478     }
479     GDI_HEAP_UNLOCK( hdc );
480     return count;
481 }
482
483
484 /***********************************************************************
485  *           GetNearestPaletteIndex16   (GDI.370)
486  */
487 UINT16 WINAPI GetNearestPaletteIndex16( HPALETTE16 hpalette, COLORREF color )
488 {
489     return GetNearestPaletteIndex32( hpalette, color );
490 }
491
492
493 /***********************************************************************
494  * GetNearestPaletteIndex32 [GDI32.203]  Gets palette index for color
495  *
496  * NOTES
497  *    Should index be initialized to CLR_INVALID instead of 0?
498  *
499  * RETURNS
500  *    Success: Index of entry in logical palette
501  *    Failure: CLR_INVALID
502  */
503 UINT32 WINAPI GetNearestPaletteIndex32(
504     HPALETTE32 hpalette, /* [in] Handle of logical color palette */
505     COLORREF color)      /* [in] Color to be matched */
506 {
507     PALETTEOBJ* palObj = (PALETTEOBJ*)GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
508     UINT32 index  = 0;
509
510     if( palObj )
511         index = COLOR_PaletteLookupPixel( palObj->logpalette.palPalEntry, 
512                                           palObj->logpalette.palNumEntries,
513                                           NULL, color, FALSE );
514
515     TRACE(palette,"(%04x,%06lx): returning %d\n", hpalette, color, index );
516     GDI_HEAP_UNLOCK( hpalette );
517     return index;
518 }
519
520
521 /***********************************************************************
522  *           GetNearestColor16   (GDI.154)
523  */
524 COLORREF WINAPI GetNearestColor16( HDC16 hdc, COLORREF color )
525 {
526     return GetNearestColor32( hdc, color );
527 }
528
529
530 /***********************************************************************
531  * GetNearestColor32 [GDI32.202]  Gets a system color to match
532  *
533  * NOTES
534  *    Should this return CLR_INVALID instead of FadeCafe?
535  *
536  * RETURNS
537  *    Success: Color from system palette that corresponds to given color
538  *    Failure: CLR_INVALID
539  */
540 COLORREF WINAPI GetNearestColor32(
541     HDC32 hdc,      /* [in] Handle of device context */
542     COLORREF color) /* [in] Color to be matched */
543 {
544     COLORREF     nearest = 0xFADECAFE;
545     DC          *dc;
546     PALETTEOBJ  *palObj;
547
548     if ( (dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC )) )
549     {
550       palObj = (PALETTEOBJ*) 
551                 GDI_GetObjPtr( (dc->w.hPalette)? dc->w.hPalette
552                                                : STOCK_DEFAULT_PALETTE, PALETTE_MAGIC );
553       if (!palObj) return nearest;
554
555       nearest = COLOR_LookupNearestColor( palObj->logpalette.palPalEntry,
556                                           palObj->logpalette.palNumEntries, color );
557       GDI_HEAP_UNLOCK( dc->w.hPalette );
558     }
559
560     TRACE(palette,"(%06lx): returning %06lx\n", color, nearest );
561     GDI_HEAP_UNLOCK( hdc );    
562     return nearest;
563 }
564
565
566 /***********************************************************************
567  *           PALETTE_GetObject
568  */
569 int PALETTE_GetObject( PALETTEOBJ * palette, int count, LPSTR buffer )
570 {
571     if (count > sizeof(WORD)) count = sizeof(WORD);
572     memcpy( buffer, &palette->logpalette.palNumEntries, count );
573     return count;
574 }
575
576
577 /***********************************************************************
578  *           PALETTE_UnrealizeObject
579  */
580 BOOL32 PALETTE_UnrealizeObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
581 {
582     if (palette->mapping)
583     {
584         free( palette->mapping );
585         palette->mapping = NULL;
586     }
587     if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
588     return TRUE;
589 }
590
591
592 /***********************************************************************
593  *           PALETTE_DeleteObject
594  */
595 BOOL32 PALETTE_DeleteObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
596 {
597     free( palette->mapping );
598     if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
599     return GDI_FreeObject( hpalette );
600 }
601
602
603 /***********************************************************************
604  *           GDISelectPalette    (GDI.361)
605  */
606 HPALETTE16 WINAPI GDISelectPalette( HDC16 hdc, HPALETTE16 hpal, WORD wBkg)
607 {
608     HPALETTE16 prev;
609     DC *dc;
610
611     TRACE(palette, "%04x %04x\n", hdc, hpal );
612     
613     dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
614     if (!dc) 
615     {
616         dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
617         if (!dc) return 0;
618     }
619     prev = dc->w.hPalette;
620     dc->w.hPalette = hpal;
621     GDI_HEAP_UNLOCK( hdc );
622     if (!wBkg) hPrimaryPalette = hpal; 
623     return prev;
624 }
625
626
627 /***********************************************************************
628  *           GDIRealizePalette    (GDI.362)
629  */
630 UINT16 WINAPI GDIRealizePalette( HDC16 hdc )
631 {
632     PALETTEOBJ* palPtr;
633     int realized = 0;
634     DC*         dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
635     if (!dc) 
636     {
637         dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
638         if (!dc) return 0;
639     }
640
641     TRACE(palette, "%04x...\n", hdc );
642     
643     if( dc &&  dc->w.hPalette != hLastRealizedPalette )
644     {
645         if( dc->w.hPalette == STOCK_DEFAULT_PALETTE )
646             return RealizeDefaultPalette( hdc );
647
648         palPtr = (PALETTEOBJ *) GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC );
649
650         if (!palPtr) {
651                 FIXME(palette,"invalid selected palette %04x\n",dc->w.hPalette);
652                 return 0;
653         }
654         
655         realized = COLOR_SetMapping(palPtr,0,palPtr->logpalette.palNumEntries,
656                                     (dc->w.hPalette != hPrimaryPalette) ||
657                                     (dc->w.hPalette == STOCK_DEFAULT_PALETTE));
658         GDI_HEAP_UNLOCK( dc->w.hPalette );
659         hLastRealizedPalette = dc->w.hPalette;
660     }
661     else TRACE(palette, "  skipping (hLastRealizedPalette = %04x)\n",
662                          hLastRealizedPalette);
663     GDI_HEAP_UNLOCK( hdc );
664
665     TRACE(palette, "   realized %i colors.\n", realized );
666     return (UINT16)realized;
667 }
668
669
670 /***********************************************************************
671  *           RealizeDefaultPalette    (GDI.365)
672  */
673 UINT16 WINAPI RealizeDefaultPalette( HDC16 hdc )
674 {
675     DC          *dc;
676     PALETTEOBJ*  palPtr;
677     int          i, index, realized = 0;
678
679     TRACE(palette,"%04x\n", hdc );
680
681     dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
682     if (!dc) 
683     {
684         dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
685         if (!dc) return 0;
686     }
687
688     if ( dc->w.flags & DC_MEMORY ) 
689       {
690         GDI_HEAP_UNLOCK( hdc );
691         return 0;
692       }
693
694     hPrimaryPalette = STOCK_DEFAULT_PALETTE;
695     hLastRealizedPalette = STOCK_DEFAULT_PALETTE;
696
697     palPtr = (PALETTEOBJ*)GDI_GetObjPtr(STOCK_DEFAULT_PALETTE, PALETTE_MAGIC );
698     if (!palPtr) return 0;
699
700     /* lookup is needed to account for SetSystemPaletteUse() stuff */
701
702     for( i = 0; i < 20; i++ )
703        {
704          index = COLOR_LookupSystemPixel(*(COLORREF*)(palPtr->logpalette.palPalEntry + i));
705
706          /* mapping is allocated in COLOR_InitPalette() */
707
708          if( index != palPtr->mapping[i] ) { palPtr->mapping[i]=index; realized++; }
709        }
710     return realized;
711 }
712
713 /***********************************************************************
714  *           IsDCCurrentPalette   (GDI.412)
715  */
716 BOOL16 WINAPI IsDCCurrentPalette(HDC16 hDC)
717 {
718     DC* dc = (DC *)GDI_GetObjPtr( hDC, DC_MAGIC );
719     if (dc) 
720     {
721       GDI_HEAP_UNLOCK( hDC );
722       return dc->w.hPalette == hPrimaryPalette;
723     }
724     return FALSE;
725 }
726
727
728 /***********************************************************************
729  *           SelectPalette16    (USER.282)
730  */
731 HPALETTE16 WINAPI SelectPalette16( HDC16 hDC, HPALETTE16 hPal,
732                                    BOOL16 bForceBackground )
733 {
734     return SelectPalette32( hDC, hPal, bForceBackground );
735 }
736
737
738 /***********************************************************************
739  * SelectPalette32 [GDI32.300]  Selects logical palette into DC
740  *
741  * RETURNS
742  *    Success: Previous logical palette
743  *    Failure: NULL
744  */
745 HPALETTE32 WINAPI SelectPalette32(
746     HDC32 hDC,               /* [in] Handle of device context */
747     HPALETTE32 hPal,         /* [in] Handle of logical color palette */
748     BOOL32 bForceBackground) /* [in] Foreground/background mode */
749 {
750     WORD        wBkgPalette = 1;
751     PALETTEOBJ* lpt = (PALETTEOBJ*) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
752
753     TRACE(palette,"dc=%04x,pal=%04x,force=%i\n", hDC, hPal, bForceBackground);
754     if( !lpt ) return 0;
755
756     TRACE(palette," entries = %d\n", lpt->logpalette.palNumEntries);
757     GDI_HEAP_UNLOCK( hPal );
758
759     if( hPal != STOCK_DEFAULT_PALETTE )
760     {
761         HWND32 hWnd = WindowFromDC32( hDC );
762         HWND32 hActive = GetActiveWindow32();
763         
764         /* set primary palette if it's related to current active */
765
766         if((!hWnd || (hActive == hWnd || IsChild16(hActive,hWnd))) &&
767             !bForceBackground )
768             wBkgPalette = 0;
769     }
770     return GDISelectPalette( hDC, hPal, wBkgPalette);
771 }
772
773
774 /***********************************************************************
775  *           RealizePalette16    (USER.283)
776  */
777 UINT16 WINAPI RealizePalette16( HDC16 hDC )
778 {
779     return RealizePalette32( hDC );
780 }
781
782
783 /***********************************************************************
784  * RealizePalette32 [GDI32.280]  Maps palette entries to system palette
785  *
786  * RETURNS
787  *    Success: Number of entries in logical palette
788  *    Failure: GDI_ERROR
789  */
790 UINT32 WINAPI RealizePalette32(
791     HDC32 hDC) /* [in] Handle of device context */
792 {
793     UINT32 realized = GDIRealizePalette( hDC );
794
795     /* do not send anything if no colors were changed */
796
797     if( IsDCCurrentPalette( hDC ) && realized && 
798         !(COLOR_GetSystemPaletteFlags() & COLOR_VIRTUAL) )
799     {
800         /* Send palette change notification */
801
802         HWND32 hWnd;
803         if( (hWnd = WindowFromDC32( hDC )) )
804             SendMessage16( HWND_BROADCAST, WM_PALETTECHANGED, hWnd, 0L);
805     }
806     return realized;
807 }
808
809
810 /**********************************************************************
811  *            UpdateColors16   (GDI.366)
812  */
813 INT16 WINAPI UpdateColors16( HDC16 hDC )
814 {
815     HWND32 hWnd = WindowFromDC32( hDC );
816
817     /* Docs say that we have to remap current drawable pixel by pixel
818      * but it would take forever given the speed of XGet/PutPixel.
819      */
820     if (hWnd && !(COLOR_GetSystemPaletteFlags() & COLOR_VIRTUAL) ) 
821         InvalidateRect32( hWnd, NULL, FALSE );
822     return 0x666;
823 }
824
825
826 /**********************************************************************
827  * UpdateColors32 [GDI32.359]  Remaps current colors to logical palette
828  *
829  * RETURNS
830  *    Success: TRUE
831  *    Failure: FALSE
832  */
833 BOOL32 WINAPI UpdateColors32(
834     HDC32 hDC) /* [in] Handle of device context */
835 {
836     UpdateColors16( hDC );
837     return TRUE;
838 }
839
840
841 /*********************************************************************
842  *           SetMagicColors16   (GDI.606)
843  */
844 VOID WINAPI SetMagicColors16(HDC16 hDC, COLORREF color, UINT16 index)
845 {
846     FIXME(palette,"(hDC %04x, color %04x, index %04x): stub\n", hDC, (int)color, index);
847
848 }