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