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