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