Correct tests involved with processing the LVIF_DI_SETITEM flag.
[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: This simply creates the halftone palette dirived from runing
154  *        tests on an windows NT machine. this is assuming a color depth
155  *        of greater that 256 color. On a 256 color device the halftone
156  *        palette will be differnt and this funtion will be incorrect
157  */
158 HPALETTE WINAPI CreateHalftonePalette(
159     HDC hdc) /* [in] Handle to device context */
160 {
161     int i;
162     struct {
163         WORD Version;
164         WORD NumberOfEntries;
165         PALETTEENTRY aEntries[256];
166     } Palette;
167
168     Palette.Version = 0x300;
169     Palette.NumberOfEntries = 256;
170     GetSystemPaletteEntries(hdc, 0, 256, Palette.aEntries);
171
172     Palette.NumberOfEntries = 20;
173
174     for (i = 0; i < Palette.NumberOfEntries; i++)
175     {
176         Palette.aEntries[i].peRed=0xff;
177         Palette.aEntries[i].peGreen=0xff;
178         Palette.aEntries[i].peBlue=0xff;
179         Palette.aEntries[i].peFlags=0x00;
180     }
181
182     Palette.aEntries[0].peRed=0x00;
183     Palette.aEntries[0].peBlue=0x00;
184     Palette.aEntries[0].peGreen=0x00;
185
186     /* the first 6 */
187     for (i=1; i <= 6; i++)
188     {
189         Palette.aEntries[i].peRed=(i%2)?0x80:0;
190         Palette.aEntries[i].peGreen=(i==2)?0x80:(i==3)?0x80:(i==6)?0x80:0;
191         Palette.aEntries[i].peBlue=(i>3)?0x80:0;
192     }
193     
194     for (i=7;  i <= 12; i++)
195     {
196         switch(i)
197         {
198             case 7:
199                 Palette.aEntries[i].peRed=0xc0;
200                 Palette.aEntries[i].peBlue=0xc0;
201                 Palette.aEntries[i].peGreen=0xc0;
202                 break;
203             case 8:
204                 Palette.aEntries[i].peRed=0xc0;
205                 Palette.aEntries[i].peGreen=0xdc;
206                 Palette.aEntries[i].peBlue=0xc0;
207                 break;
208             case 9:
209                 Palette.aEntries[i].peRed=0xa6;
210                 Palette.aEntries[i].peGreen=0xca;
211                 Palette.aEntries[i].peBlue=0xf0;
212                 break;
213             case 10:    
214                 Palette.aEntries[i].peRed=0xff;
215                 Palette.aEntries[i].peGreen=0xfb;
216                 Palette.aEntries[i].peBlue=0xf0;
217                 break;
218             case 11:
219                 Palette.aEntries[i].peRed=0xa0;
220                 Palette.aEntries[i].peGreen=0xa0;
221                 Palette.aEntries[i].peBlue=0xa4;
222                 break;
223             case 12:
224                 Palette.aEntries[i].peRed=0x80;
225                 Palette.aEntries[i].peGreen=0x80;
226                 Palette.aEntries[i].peBlue=0x80;
227         }
228     }
229
230    for (i=13; i <= 18; i++)
231     {
232         Palette.aEntries[i].peRed=(i%2)?0xff:0;
233         Palette.aEntries[i].peGreen=(i==14)?0xff:(i==15)?0xff:(i==18)?0xff:0;
234         Palette.aEntries[i].peBlue=(i>15)?0xff:0x00;
235     }
236
237     return CreatePalette((LOGPALETTE *)&Palette);
238 }
239
240
241 /***********************************************************************
242  *           GetPaletteEntries    (GDI.363)
243  */
244 UINT16 WINAPI GetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
245                                    UINT16 count, LPPALETTEENTRY entries )
246 {
247     return GetPaletteEntries( hpalette, start, count, entries );
248 }
249
250
251 /***********************************************************************
252  * GetPaletteEntries [GDI32.@]  Retrieves palette entries
253  *
254  * RETURNS
255  *    Success: Number of entries from logical palette
256  *    Failure: 0
257  */
258 UINT WINAPI GetPaletteEntries(
259     HPALETTE hpalette,    /* [in]  Handle of logical palette */
260     UINT start,           /* [in]  First entry to receive */
261     UINT count,           /* [in]  Number of entries to receive */
262     LPPALETTEENTRY entries) /* [out] Address of array receiving entries */
263 {
264     PALETTEOBJ * palPtr;
265     UINT numEntries;
266
267     TRACE("hpal = %04x, count=%i\n", hpalette, count );
268         
269     palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
270     if (!palPtr) return 0;
271     
272     /* NOTE: not documented but test show this to be the case */
273     if (count == 0)
274     {   
275         int rc = palPtr->logpalette.palNumEntries;
276             GDI_ReleaseObj( hpalette );
277         return rc;
278     }
279
280     numEntries = palPtr->logpalette.palNumEntries;
281     if (start+count > numEntries) count = numEntries - start;
282     if (entries)
283     { 
284       if (start >= numEntries) 
285       {
286         GDI_ReleaseObj( hpalette );
287         return 0;
288       }
289       memcpy( entries, &palPtr->logpalette.palPalEntry[start],
290               count * sizeof(PALETTEENTRY) );
291       for( numEntries = 0; numEntries < count ; numEntries++ )
292            if (entries[numEntries].peFlags & 0xF0)
293                entries[numEntries].peFlags = 0;
294     }
295
296     GDI_ReleaseObj( hpalette );
297     return count;
298 }
299
300
301 /***********************************************************************
302  *           SetPaletteEntries    (GDI.364)
303  */
304 UINT16 WINAPI SetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
305                                    UINT16 count, LPPALETTEENTRY entries )
306 {
307     return SetPaletteEntries( hpalette, start, count, entries );
308 }
309
310
311 /***********************************************************************
312  * SetPaletteEntries [GDI32.@]  Sets color values for range in palette
313  *
314  * RETURNS
315  *    Success: Number of entries that were set
316  *    Failure: 0
317  */
318 UINT WINAPI SetPaletteEntries(
319     HPALETTE hpalette,    /* [in] Handle of logical palette */
320     UINT start,           /* [in] Index of first entry to set */
321     UINT count,           /* [in] Number of entries to set */
322     LPPALETTEENTRY entries) /* [in] Address of array of structures */
323 {
324     PALETTEOBJ * palPtr;
325     UINT numEntries;
326
327     TRACE("hpal=%04x,start=%i,count=%i\n",hpalette,start,count );
328
329     palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
330     if (!palPtr) return 0;
331
332     numEntries = palPtr->logpalette.palNumEntries;
333     if (start >= numEntries) 
334     {
335       GDI_ReleaseObj( hpalette );
336       return 0;
337     }
338     if (start+count > numEntries) count = numEntries - start;
339     memcpy( &palPtr->logpalette.palPalEntry[start], entries,
340             count * sizeof(PALETTEENTRY) );
341     PALETTE_ValidateFlags(palPtr->logpalette.palPalEntry, 
342                           palPtr->logpalette.palNumEntries);
343     HeapFree( GetProcessHeap(), 0, palPtr->mapping );
344     palPtr->mapping = NULL;
345     GDI_ReleaseObj( hpalette );
346     return count;
347 }
348
349
350 /***********************************************************************
351  *           ResizePalette   (GDI.368)
352  */
353 BOOL16 WINAPI ResizePalette16( HPALETTE16 hPal, UINT16 cEntries )
354 {
355     return ResizePalette( hPal, cEntries );
356 }
357
358
359 /***********************************************************************
360  * ResizePalette [GDI32.@]  Resizes logical palette
361  *
362  * RETURNS
363  *    Success: TRUE
364  *    Failure: FALSE
365  */
366 BOOL WINAPI ResizePalette(
367     HPALETTE hPal, /* [in] Handle of logical palette */
368     UINT cEntries) /* [in] Number of entries in logical palette */
369 {
370     PALETTEOBJ * palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
371     UINT         cPrevEnt, prevVer;
372     int          prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
373     int*         mapping = NULL;
374
375     TRACE("hpal = %04x, prev = %i, new = %i\n",
376                     hPal, palPtr ? palPtr->logpalette.palNumEntries : -1,
377                     cEntries );
378     if( !palPtr ) return FALSE;
379     cPrevEnt = palPtr->logpalette.palNumEntries;
380     prevVer = palPtr->logpalette.palVersion;
381     prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) +
382                                         sizeof(int*) + sizeof(GDIOBJHDR);
383     size += sizeof(int*) + sizeof(GDIOBJHDR);
384     mapping = palPtr->mapping;
385     
386     if (!(palPtr = GDI_ReallocObject( size, hPal, palPtr ))) return FALSE;
387
388     if( mapping ) 
389     {
390         int *newMap = (int*) HeapReAlloc(GetProcessHeap(), 0, 
391                                     mapping, cEntries * sizeof(int) );
392         if(newMap == NULL) 
393         {
394             ERR("Can not resize mapping -- out of memory!\n");
395             GDI_ReleaseObj( hPal );
396             return FALSE;
397         }
398         palPtr->mapping = newMap;
399     }
400
401     if( cEntries > cPrevEnt ) 
402     {
403         if( mapping )
404             memset(palPtr->mapping + cPrevEnt, 0, (cEntries - cPrevEnt)*sizeof(int));
405         memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
406         PALETTE_ValidateFlags((PALETTEENTRY*)((BYTE*)palPtr + prevsize), 
407                                                      cEntries - cPrevEnt );
408     }
409     palPtr->logpalette.palNumEntries = cEntries;
410     palPtr->logpalette.palVersion = prevVer;
411     GDI_ReleaseObj( hPal );
412     return TRUE;
413 }
414
415
416 /***********************************************************************
417  *           AnimatePalette   (GDI.367)
418  */
419 void WINAPI AnimatePalette16( HPALETTE16 hPal, UINT16 StartIndex,
420                               UINT16 NumEntries, const PALETTEENTRY* PaletteColors)
421 {
422     AnimatePalette( hPal, StartIndex, NumEntries, PaletteColors );
423 }
424
425
426 /***********************************************************************
427  * AnimatePalette [GDI32.@]  Replaces entries in logical palette
428  *
429  * RETURNS
430  *    Success: TRUE
431  *    Failure: FALSE
432  *
433  * FIXME
434  *    Should use existing mapping when animating a primary palette
435  */
436 BOOL WINAPI AnimatePalette(
437     HPALETTE hPal,              /* [in] Handle to logical palette */
438     UINT StartIndex,            /* [in] First entry in palette */
439     UINT NumEntries,            /* [in] Count of entries in palette */
440     const PALETTEENTRY* PaletteColors) /* [in] Pointer to first replacement */
441 {
442     TRACE("%04x (%i - %i)\n", hPal, StartIndex,StartIndex+NumEntries);
443
444     if( hPal != GetStockObject(DEFAULT_PALETTE) )
445     {
446         PALETTEOBJ* palPtr = (PALETTEOBJ *)GDI_GetObjPtr(hPal, PALETTE_MAGIC);
447         if (!palPtr) return FALSE;
448
449         if( (StartIndex + NumEntries) <= palPtr->logpalette.palNumEntries )
450         {
451             UINT u;
452             for( u = 0; u < NumEntries; u++ )
453                 palPtr->logpalette.palPalEntry[u + StartIndex] = PaletteColors[u];
454             PALETTE_Driver->
455               pSetMapping(palPtr, StartIndex, NumEntries,
456                           hPal != hPrimaryPalette );
457             GDI_ReleaseObj( hPal );
458             return TRUE;
459         }
460         GDI_ReleaseObj( hPal );
461     }
462     return FALSE;
463 }
464
465
466 /***********************************************************************
467  *           SetSystemPaletteUse   (GDI.373)
468  */
469 UINT16 WINAPI SetSystemPaletteUse16( HDC16 hdc, UINT16 use )
470 {
471     return SetSystemPaletteUse( hdc, use );
472 }
473
474
475 /***********************************************************************
476  * SetSystemPaletteUse [GDI32.@]
477  *
478  * RETURNS
479  *    Success: Previous system palette
480  *    Failure: SYSPAL_ERROR
481  */
482 UINT WINAPI SetSystemPaletteUse(
483     HDC hdc,  /* [in] Handle of device context */
484     UINT use) /* [in] Palette-usage flag */
485 {
486     UINT old = SystemPaletteUse;
487     FIXME("(%04x,%04x): stub\n", hdc, use );
488     SystemPaletteUse = use;
489     return old;
490 }
491
492
493 /***********************************************************************
494  *           GetSystemPaletteUse   (GDI.374)
495  */
496 UINT16 WINAPI GetSystemPaletteUse16( HDC16 hdc )
497 {
498     return SystemPaletteUse;
499 }
500
501
502 /***********************************************************************
503  * GetSystemPaletteUse [GDI32.@]  Gets state of system palette
504  *
505  * RETURNS
506  *    Current state of system palette
507  */
508 UINT WINAPI GetSystemPaletteUse(
509     HDC hdc) /* [in] Handle of device context */
510 {
511     return SystemPaletteUse;
512 }
513
514
515 /***********************************************************************
516  *           GetSystemPaletteEntries   (GDI.375)
517  */
518 UINT16 WINAPI GetSystemPaletteEntries16( HDC16 hdc, UINT16 start, UINT16 count,
519                                          LPPALETTEENTRY entries )
520 {
521     return GetSystemPaletteEntries( hdc, start, count, entries );
522 }
523
524
525 /***********************************************************************
526  * GetSystemPaletteEntries [GDI32.@]  Gets range of palette entries
527  *
528  * RETURNS
529  *    Success: Number of entries retrieved from palette
530  *    Failure: 0
531  */
532 UINT WINAPI GetSystemPaletteEntries(
533     HDC hdc,              /* [in]  Handle of device context */
534     UINT start,           /* [in]  Index of first entry to be retrieved */
535     UINT count,           /* [in]  Number of entries to be retrieved */
536     LPPALETTEENTRY entries) /* [out] Array receiving system-palette entries */
537 {
538     UINT i;
539     INT sizePalette = GetDeviceCaps( hdc, SIZEPALETTE );
540
541     TRACE("hdc=%04x,start=%i,count=%i\n", hdc,start,count);
542
543     if (!entries) return sizePalette;
544     if (start >= sizePalette) return 0;
545     if (start+count >= sizePalette) count = sizePalette - start;
546
547     for (i = 0; i < count; i++)
548     {
549         *(COLORREF*)(entries + i) = COLOR_GetSystemPaletteEntry( start + i );
550
551         TRACE("\tidx(%02x) -> RGB(%08lx)\n",
552                          start + i, *(COLORREF*)(entries + i) );
553     }
554     return count;
555 }
556
557
558 /***********************************************************************
559  *           GetNearestPaletteIndex   (GDI.370)
560  */
561 UINT16 WINAPI GetNearestPaletteIndex16( HPALETTE16 hpalette, COLORREF color )
562 {
563     return GetNearestPaletteIndex( hpalette, color );
564 }
565
566
567 /***********************************************************************
568  * GetNearestPaletteIndex [GDI32.@]  Gets palette index for color
569  *
570  * NOTES
571  *    Should index be initialized to CLR_INVALID instead of 0?
572  *
573  * RETURNS
574  *    Success: Index of entry in logical palette
575  *    Failure: CLR_INVALID
576  */
577 UINT WINAPI GetNearestPaletteIndex(
578     HPALETTE hpalette, /* [in] Handle of logical color palette */
579     COLORREF color)      /* [in] Color to be matched */
580 {
581     PALETTEOBJ* palObj = (PALETTEOBJ*)GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
582     UINT index  = 0;
583
584     if( palObj )
585     {
586       index = COLOR_PaletteLookupPixel(palObj->logpalette.palPalEntry, 
587                                        palObj->logpalette.palNumEntries,
588                                        NULL, color, FALSE );
589
590       GDI_ReleaseObj( hpalette );
591     }
592     TRACE("(%04x,%06lx): returning %d\n", hpalette, color, index );
593     return index;
594 }
595
596
597 /***********************************************************************
598  *           GetNearestColor   (GDI.154)
599  */
600 COLORREF WINAPI GetNearestColor16( HDC16 hdc, COLORREF color )
601 {
602     return GetNearestColor( hdc, color );
603 }
604
605
606 /***********************************************************************
607  * GetNearestColor [GDI32.@]  Gets a system color to match
608  *
609  * RETURNS
610  *    Success: Color from system palette that corresponds to given color
611  *    Failure: CLR_INVALID
612  */
613 COLORREF WINAPI GetNearestColor(
614     HDC hdc,      /* [in] Handle of device context */
615     COLORREF color) /* [in] Color to be matched */
616 {
617     COLORREF     nearest = CLR_INVALID;
618     DC          *dc;
619     PALETTEOBJ  *palObj;
620
621     if(!(GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)) {
622         return color;
623     }
624     if ( (dc = DC_GetDCPtr( hdc )) )
625     {
626         HPALETTE hpal = (dc->hPalette)? dc->hPalette : GetStockObject( DEFAULT_PALETTE );
627         palObj = GDI_GetObjPtr( hpal, PALETTE_MAGIC );
628         if (!palObj) {
629             GDI_ReleaseObj( hdc );
630             return nearest;
631         }
632
633         nearest = COLOR_LookupNearestColor( palObj->logpalette.palPalEntry,
634                                             palObj->logpalette.palNumEntries, color );
635         GDI_ReleaseObj( hpal );
636         GDI_ReleaseObj( hdc );
637     }
638
639     TRACE("(%06lx): returning %06lx\n", color, nearest );
640     return nearest;
641 }
642
643
644 /***********************************************************************
645  *           PALETTE_GetObject
646  */
647 int PALETTE_GetObject( PALETTEOBJ * palette, int count, LPSTR buffer )
648 {
649     if (count > sizeof(WORD)) count = sizeof(WORD);
650     memcpy( buffer, &palette->logpalette.palNumEntries, count );
651     return count;
652 }
653
654
655 /***********************************************************************
656  *           PALETTE_UnrealizeObject
657  */
658 BOOL PALETTE_UnrealizeObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
659 {
660     if (palette->mapping)
661     {
662         HeapFree( GetProcessHeap(), 0, palette->mapping );
663         palette->mapping = NULL;
664     }
665     if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
666     return TRUE;
667 }
668
669
670 /***********************************************************************
671  *           PALETTE_DeleteObject
672  */
673 BOOL PALETTE_DeleteObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
674 {
675     HeapFree( GetProcessHeap(), 0, palette->mapping );
676     if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
677     return GDI_FreeObject( hpalette, palette );
678 }
679
680
681 /***********************************************************************
682  *           GDISelectPalette    (GDI.361)
683  */
684 HPALETTE16 WINAPI GDISelectPalette16( HDC16 hdc, HPALETTE16 hpal, WORD wBkg)
685 {
686     HPALETTE16 prev;
687     DC *dc;
688
689     TRACE("%04x %04x\n", hdc, hpal );
690
691     if (GetObjectType(hpal) != OBJ_PAL)
692     {
693       WARN("invalid selected palette %04x\n",hpal);
694       return 0;
695     }
696     if (!(dc = DC_GetDCPtr( hdc ))) return 0;
697     prev = dc->hPalette;
698     dc->hPalette = hpal;
699     GDI_ReleaseObj( hdc );
700     if (!wBkg) hPrimaryPalette = hpal; 
701     return prev;
702 }
703
704
705 /***********************************************************************
706  *           GDIRealizePalette    (GDI.362)
707  */
708 UINT16 WINAPI GDIRealizePalette16( HDC16 hdc )
709 {
710     PALETTEOBJ* palPtr;
711     int realized = 0;
712     DC* dc = DC_GetDCPtr( hdc );
713
714     if (!dc) return 0;
715
716     TRACE("%04x...\n", hdc );
717     
718     if(dc->hPalette != hLastRealizedPalette )
719     {
720         if( dc->hPalette == GetStockObject( DEFAULT_PALETTE )) {
721             realized = RealizeDefaultPalette16( hdc );
722             GDI_ReleaseObj( hdc );
723             return (UINT16)realized;
724         }
725
726
727         palPtr = (PALETTEOBJ *) GDI_GetObjPtr( dc->hPalette, PALETTE_MAGIC );
728
729         if (!palPtr) {
730             GDI_ReleaseObj( hdc );
731             FIXME("invalid selected palette %04x\n",dc->hPalette);
732             return 0;
733         }
734         
735         realized = PALETTE_Driver->
736           pSetMapping(palPtr,0,palPtr->logpalette.palNumEntries,
737                       (dc->hPalette != hPrimaryPalette) ||
738                       (dc->hPalette == GetStockObject( DEFAULT_PALETTE )));
739         hLastRealizedPalette = dc->hPalette;
740         GDI_ReleaseObj( dc->hPalette );
741     }
742     else TRACE("  skipping (hLastRealizedPalette = %04x)\n",
743                          hLastRealizedPalette);
744     GDI_ReleaseObj( hdc );
745
746     TRACE("   realized %i colors.\n", realized );
747     return (UINT16)realized;
748 }
749
750
751 /***********************************************************************
752  *           RealizeDefaultPalette    (GDI.365)
753  */
754 UINT16 WINAPI RealizeDefaultPalette16( HDC16 hdc )
755 {
756     UINT16 ret = 0;
757     DC          *dc;
758     PALETTEOBJ*  palPtr;
759
760     TRACE("%04x\n", hdc );
761
762     if (!(dc = DC_GetDCPtr( hdc ))) return 0;
763
764     if (!(dc->flags & DC_MEMORY))
765     {
766         palPtr = (PALETTEOBJ*)GDI_GetObjPtr( GetStockObject(DEFAULT_PALETTE), PALETTE_MAGIC );
767         if (palPtr)
768         {
769             /* lookup is needed to account for SetSystemPaletteUse() stuff */
770             ret = PALETTE_Driver->pUpdateMapping(palPtr);
771             GDI_ReleaseObj( GetStockObject(DEFAULT_PALETTE) );
772         }
773     }
774     GDI_ReleaseObj( hdc );
775     return ret;
776 }
777
778 /***********************************************************************
779  *           IsDCCurrentPalette   (GDI.412)
780  */
781 BOOL16 WINAPI IsDCCurrentPalette16(HDC16 hDC)
782 {
783     DC *dc = DC_GetDCPtr( hDC );
784     if (dc) 
785     {
786       BOOL bRet = dc->hPalette == hPrimaryPalette;
787       GDI_ReleaseObj( hDC );
788       return bRet;
789     }
790     return FALSE;
791 }
792
793
794 /***********************************************************************
795  * SelectPalette [GDI32.@]  Selects logical palette into DC
796  *
797  * RETURNS
798  *    Success: Previous logical palette
799  *    Failure: NULL
800  */
801 HPALETTE WINAPI SelectPalette(
802     HDC hDC,               /* [in] Handle of device context */
803     HPALETTE hPal,         /* [in] Handle of logical color palette */
804     BOOL bForceBackground) /* [in] Foreground/background mode */
805 {
806     return pfnSelectPalette( hDC, hPal, bForceBackground );
807 }
808
809
810 /***********************************************************************
811  * RealizePalette [GDI32.@]  Maps palette entries to system palette
812  *
813  * RETURNS
814  *    Success: Number of entries in logical palette
815  *    Failure: GDI_ERROR
816  */
817 UINT WINAPI RealizePalette(
818     HDC hDC) /* [in] Handle of device context */
819 {
820     return pfnRealizePalette( hDC );
821 }
822
823
824 typedef HWND (WINAPI *WindowFromDC_funcptr)( HDC );
825 typedef BOOL (WINAPI *RedrawWindow_funcptr)( HWND, const RECT *, HRGN, UINT );
826
827 /**********************************************************************
828  * UpdateColors [GDI32.@]  Remaps current colors to logical palette
829  *
830  * RETURNS
831  *    Success: TRUE
832  *    Failure: FALSE
833  */
834 BOOL WINAPI UpdateColors(
835     HDC hDC) /* [in] Handle of device context */
836 {
837     HMODULE mod;
838     int size = GetDeviceCaps( hDC, SIZEPALETTE );
839
840     if (!size) return 0;
841
842     mod = GetModuleHandleA("user32.dll");
843     if (mod)
844     {
845         WindowFromDC_funcptr pWindowFromDC = (WindowFromDC_funcptr)GetProcAddress(mod,"WindowFromDC");
846         if (pWindowFromDC)
847         {
848             HWND hWnd = pWindowFromDC( hDC );
849
850             /* Docs say that we have to remap current drawable pixel by pixel
851              * but it would take forever given the speed of XGet/PutPixel.
852              */
853             if (hWnd && size)
854             {
855                 RedrawWindow_funcptr pRedrawWindow = GetProcAddress( mod, "RedrawWindow" );
856                 if (pRedrawWindow) pRedrawWindow( hWnd, NULL, 0, RDW_INVALIDATE );
857             }
858         }
859     }
860     return 0x666;
861 }
862
863
864 /**********************************************************************
865  *            UpdateColors   (GDI.366)
866  */
867 INT16 WINAPI UpdateColors16( HDC16 hDC )
868 {
869     UpdateColors( hDC );
870     return TRUE;
871 }
872
873
874 /*********************************************************************
875  *           SetMagicColors   (GDI.606)
876  */
877 VOID WINAPI SetMagicColors16(HDC16 hDC, COLORREF color, UINT16 index)
878 {
879     FIXME("(hDC %04x, color %04x, index %04x): stub\n", hDC, (int)color, index);
880
881 }
882
883 /**********************************************************************
884  * GetICMProfileA [GDI32.@]
885  *
886  * Returns the filename of the specified device context's color
887  * management profile, even if color management is not enabled
888  * for that DC.
889  *
890  * RETURNS
891  *    TRUE if name copied succesfully OR lpszFilename is NULL
892  *    FALSE if the buffer length pointed to by lpcbName is too small
893  *
894  * NOTE
895  *    The buffer length pointed to by lpcbName is ALWAYS updated to
896  *    the length required regardless of other actions this function
897  *    may take.
898  *
899  * FIXME
900  *    How does Windows assign these?  Some registry key?
901  */
902
903 #define WINEICM "winefake.icm"  /* easy-to-identify fake filename */
904
905 /*********************************************************************/
906
907 BOOL WINAPI GetICMProfileA(HDC hDC, LPDWORD lpcbName, LPSTR lpszFilename)
908 {
909     DWORD callerLen;
910
911     FIXME("(%04x, %p, %p): partial stub\n", hDC, lpcbName, lpszFilename);
912
913     callerLen = *lpcbName;
914
915     /* all 3 behaviors require the required buffer size to be set */
916     *lpcbName = strlen(WINEICM);
917
918     /* behavior 1: if lpszFilename is NULL, return size of string and no error */
919     if ((DWORD)lpszFilename == (DWORD)0x00000000)
920         return TRUE;
921     
922     /* behavior 2: if buffer size too small, return size of string and error */
923     if (callerLen < strlen(WINEICM)) 
924     {
925         SetLastError(ERROR_INSUFFICIENT_BUFFER);
926         return FALSE;
927     }
928
929     /* behavior 3: if buffer size OK and pointer not NULL, copy and return size */
930     strcpy(lpszFilename, WINEICM);
931     return TRUE;
932 }