Added entry for DirectSoundFullDuplexCreate.
[wine] / objects / palette.c
1 /*
2  * GDI palette objects
3  *
4  * Copyright 1993,1994 Alexandre Julliard
5  * Copyright 1996 Alex Korobka
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  * NOTES:
22  * PALETTEOBJ is documented in the Dr. Dobbs Journal May 1993.
23  * Information in the "Undocumented Windows" is incorrect.
24  */
25
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "winbase.h"
30 #include "windef.h"
31 #include "wingdi.h"
32 #include "wownt32.h"
33 #include "wine/winuser16.h"
34 #include "gdi.h"
35 #include "palette.h"
36 #include "wine/debug.h"
37 #include "winerror.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(palette);
40
41 static INT PALETTE_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer );
42 static BOOL PALETTE_UnrealizeObject( HGDIOBJ handle, void *obj );
43 static BOOL PALETTE_DeleteObject( HGDIOBJ handle, void *obj );
44
45 static const struct gdi_obj_funcs palette_funcs =
46 {
47     NULL,                     /* pSelectObject */
48     PALETTE_GetObject,        /* pGetObject16 */
49     PALETTE_GetObject,        /* pGetObjectA */
50     PALETTE_GetObject,        /* pGetObjectW */
51     PALETTE_UnrealizeObject,  /* pUnrealizeObject */
52     PALETTE_DeleteObject      /* pDeleteObject */
53 };
54
55 /* Pointers to USER implementation of SelectPalette/RealizePalette */
56 /* they will be patched by USER on startup */
57 HPALETTE (WINAPI *pfnSelectPalette)(HDC hdc, HPALETTE hpal, WORD bkgnd ) = GDISelectPalette;
58 UINT (WINAPI *pfnRealizePalette)(HDC hdc) = GDIRealizePalette;
59
60 static UINT SystemPaletteUse = SYSPAL_STATIC;  /* currently not considered */
61
62 static HPALETTE hPrimaryPalette = 0; /* used for WM_PALETTECHANGED */
63 static HPALETTE hLastRealizedPalette = 0; /* UnrealizeObject() needs it */
64 static const DC_FUNCTIONS *pLastRealizedDC;
65
66 static const PALETTEENTRY sys_pal_template[NB_RESERVED_COLORS] =
67 {
68     /* first 10 entries in the system palette */
69     /* red  green blue  flags */
70     { 0x00, 0x00, 0x00, 0 },
71     { 0x80, 0x00, 0x00, 0 },
72     { 0x00, 0x80, 0x00, 0 },
73     { 0x80, 0x80, 0x00, 0 },
74     { 0x00, 0x00, 0x80, 0 },
75     { 0x80, 0x00, 0x80, 0 },
76     { 0x00, 0x80, 0x80, 0 },
77     { 0xc0, 0xc0, 0xc0, 0 },
78     { 0xc0, 0xdc, 0xc0, 0 },
79     { 0xa6, 0xca, 0xf0, 0 },
80
81     /* ... c_min/2 dynamic colorcells */
82
83     /* ... gap (for sparse palettes) */
84
85     /* ... c_min/2 dynamic colorcells */
86
87     { 0xff, 0xfb, 0xf0, 0 },
88     { 0xa0, 0xa0, 0xa4, 0 },
89     { 0x80, 0x80, 0x80, 0 },
90     { 0xff, 0x00, 0x00, 0 },
91     { 0x00, 0xff, 0x00, 0 },
92     { 0xff, 0xff, 0x00, 0 },
93     { 0x00, 0x00, 0xff, 0 },
94     { 0xff, 0x00, 0xff, 0 },
95     { 0x00, 0xff, 0xff, 0 },
96     { 0xff, 0xff, 0xff, 0 }     /* last 10 */
97 };
98
99 /***********************************************************************
100  *           PALETTE_Init
101  *
102  * Create the system palette.
103  */
104 HPALETTE PALETTE_Init(void)
105 {
106     HPALETTE          hpalette;
107     LOGPALETTE *        palPtr;
108     PALETTEOBJ*         palObj;
109
110     /* create default palette (20 system colors) */
111
112     palPtr = HeapAlloc( GetProcessHeap(), 0,
113              sizeof(LOGPALETTE) + (NB_RESERVED_COLORS-1)*sizeof(PALETTEENTRY));
114     if (!palPtr) return FALSE;
115
116     palPtr->palVersion = 0x300;
117     palPtr->palNumEntries = NB_RESERVED_COLORS;
118     memcpy( palPtr->palPalEntry, sys_pal_template, sizeof(sys_pal_template) );
119     hpalette = CreatePalette( palPtr );
120     HeapFree( GetProcessHeap(), 0, palPtr );
121
122     palObj = (PALETTEOBJ*) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
123     if (palObj)
124     {
125         if (!(palObj->mapping = HeapAlloc( GetProcessHeap(), 0, sizeof(int) * NB_RESERVED_COLORS )))
126             ERR("Can not create palette mapping -- out of memory!\n");
127         GDI_ReleaseObj( hpalette );
128     }
129     return hpalette;
130 }
131
132 /***********************************************************************
133  *           PALETTE_ValidateFlags
134  */
135 static void PALETTE_ValidateFlags(PALETTEENTRY* lpPalE, int size)
136 {
137     int i = 0;
138     for( ; i<size ; i++ )
139         lpPalE[i].peFlags = PC_SYS_USED | (lpPalE[i].peFlags & 0x07);
140 }
141
142
143 /***********************************************************************
144  * CreatePalette [GDI32.@]  Creates a logical color palette
145  *
146  * RETURNS
147  *    Success: Handle to logical palette
148  *    Failure: NULL
149  */
150 HPALETTE WINAPI CreatePalette(
151     const LOGPALETTE* palette) /* [in] Pointer to logical color palette */
152 {
153     PALETTEOBJ * palettePtr;
154     HPALETTE hpalette;
155     int size;
156
157     if (!palette) return 0;
158     TRACE("entries=%i\n", palette->palNumEntries);
159
160     size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
161
162     if (!(palettePtr = GDI_AllocObject( size + sizeof(int*) +sizeof(GDIOBJHDR),
163                                         PALETTE_MAGIC, (HGDIOBJ *)&hpalette,
164                                         &palette_funcs ))) return 0;
165     memcpy( &palettePtr->logpalette, palette, size );
166     PALETTE_ValidateFlags(palettePtr->logpalette.palPalEntry,
167                           palettePtr->logpalette.palNumEntries);
168     palettePtr->mapping = NULL;
169     GDI_ReleaseObj( hpalette );
170
171     TRACE("   returning %p\n", hpalette);
172     return hpalette;
173 }
174
175
176 /***********************************************************************
177  * CreateHalftonePalette [GDI32.@]  Creates a halftone palette
178  *
179  * RETURNS
180  *    Success: Handle to logical halftone palette
181  *    Failure: 0
182  *
183  * FIXME: This simply creates the halftone palette dirived from runing
184  *        tests on an windows NT machine. this is assuming a color depth
185  *        of greater that 256 color. On a 256 color device the halftone
186  *        palette will be differnt and this funtion will be incorrect
187  */
188 HPALETTE WINAPI CreateHalftonePalette(
189     HDC hdc) /* [in] Handle to device context */
190 {
191     int i;
192     struct {
193         WORD Version;
194         WORD NumberOfEntries;
195         PALETTEENTRY aEntries[256];
196     } Palette;
197
198     Palette.Version = 0x300;
199     Palette.NumberOfEntries = 256;
200     GetSystemPaletteEntries(hdc, 0, 256, Palette.aEntries);
201
202     Palette.NumberOfEntries = 20;
203
204     for (i = 0; i < Palette.NumberOfEntries; i++)
205     {
206         Palette.aEntries[i].peRed=0xff;
207         Palette.aEntries[i].peGreen=0xff;
208         Palette.aEntries[i].peBlue=0xff;
209         Palette.aEntries[i].peFlags=0x00;
210     }
211
212     Palette.aEntries[0].peRed=0x00;
213     Palette.aEntries[0].peBlue=0x00;
214     Palette.aEntries[0].peGreen=0x00;
215
216     /* the first 6 */
217     for (i=1; i <= 6; i++)
218     {
219         Palette.aEntries[i].peRed=(i%2)?0x80:0;
220         Palette.aEntries[i].peGreen=(i==2)?0x80:(i==3)?0x80:(i==6)?0x80:0;
221         Palette.aEntries[i].peBlue=(i>3)?0x80:0;
222     }
223
224     for (i=7;  i <= 12; i++)
225     {
226         switch(i)
227         {
228             case 7:
229                 Palette.aEntries[i].peRed=0xc0;
230                 Palette.aEntries[i].peBlue=0xc0;
231                 Palette.aEntries[i].peGreen=0xc0;
232                 break;
233             case 8:
234                 Palette.aEntries[i].peRed=0xc0;
235                 Palette.aEntries[i].peGreen=0xdc;
236                 Palette.aEntries[i].peBlue=0xc0;
237                 break;
238             case 9:
239                 Palette.aEntries[i].peRed=0xa6;
240                 Palette.aEntries[i].peGreen=0xca;
241                 Palette.aEntries[i].peBlue=0xf0;
242                 break;
243             case 10:
244                 Palette.aEntries[i].peRed=0xff;
245                 Palette.aEntries[i].peGreen=0xfb;
246                 Palette.aEntries[i].peBlue=0xf0;
247                 break;
248             case 11:
249                 Palette.aEntries[i].peRed=0xa0;
250                 Palette.aEntries[i].peGreen=0xa0;
251                 Palette.aEntries[i].peBlue=0xa4;
252                 break;
253             case 12:
254                 Palette.aEntries[i].peRed=0x80;
255                 Palette.aEntries[i].peGreen=0x80;
256                 Palette.aEntries[i].peBlue=0x80;
257         }
258     }
259
260    for (i=13; i <= 18; i++)
261     {
262         Palette.aEntries[i].peRed=(i%2)?0xff:0;
263         Palette.aEntries[i].peGreen=(i==14)?0xff:(i==15)?0xff:(i==18)?0xff:0;
264         Palette.aEntries[i].peBlue=(i>15)?0xff:0x00;
265     }
266
267     return CreatePalette((LOGPALETTE *)&Palette);
268 }
269
270
271 /***********************************************************************
272  * GetPaletteEntries [GDI32.@]  Retrieves palette entries
273  *
274  * RETURNS
275  *    Success: Number of entries from logical palette
276  *    Failure: 0
277  */
278 UINT WINAPI GetPaletteEntries(
279     HPALETTE hpalette,    /* [in]  Handle of logical palette */
280     UINT start,           /* [in]  First entry to receive */
281     UINT count,           /* [in]  Number of entries to receive */
282     LPPALETTEENTRY entries) /* [out] Address of array receiving entries */
283 {
284     PALETTEOBJ * palPtr;
285     UINT numEntries;
286
287     TRACE("hpal = %p, count=%i\n", hpalette, count );
288
289     palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
290     if (!palPtr) return 0;
291
292     /* NOTE: not documented but test show this to be the case */
293     if (count == 0)
294     {
295         int rc = palPtr->logpalette.palNumEntries;
296             GDI_ReleaseObj( hpalette );
297         return rc;
298     }
299
300     numEntries = palPtr->logpalette.palNumEntries;
301     if (start+count > numEntries) count = numEntries - start;
302     if (entries)
303     {
304       if (start >= numEntries)
305       {
306         GDI_ReleaseObj( hpalette );
307         return 0;
308       }
309       memcpy( entries, &palPtr->logpalette.palPalEntry[start],
310               count * sizeof(PALETTEENTRY) );
311       for( numEntries = 0; numEntries < count ; numEntries++ )
312            if (entries[numEntries].peFlags & 0xF0)
313                entries[numEntries].peFlags = 0;
314     }
315
316     GDI_ReleaseObj( hpalette );
317     return count;
318 }
319
320
321 /***********************************************************************
322  * SetPaletteEntries [GDI32.@]  Sets color values for range in palette
323  *
324  * RETURNS
325  *    Success: Number of entries that were set
326  *    Failure: 0
327  */
328 UINT WINAPI SetPaletteEntries(
329     HPALETTE hpalette,    /* [in] Handle of logical palette */
330     UINT start,           /* [in] Index of first entry to set */
331     UINT count,           /* [in] Number of entries to set */
332     const PALETTEENTRY *entries) /* [in] Address of array of structures */
333 {
334     PALETTEOBJ * palPtr;
335     UINT numEntries;
336
337     TRACE("hpal=%p,start=%i,count=%i\n",hpalette,start,count );
338
339     if (hpalette == GetStockObject(DEFAULT_PALETTE)) return 0;
340     palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
341     if (!palPtr) return 0;
342
343     numEntries = palPtr->logpalette.palNumEntries;
344     if (start >= numEntries)
345     {
346       GDI_ReleaseObj( hpalette );
347       return 0;
348     }
349     if (start+count > numEntries) count = numEntries - start;
350     memcpy( &palPtr->logpalette.palPalEntry[start], entries,
351             count * sizeof(PALETTEENTRY) );
352     PALETTE_ValidateFlags(palPtr->logpalette.palPalEntry,
353                           palPtr->logpalette.palNumEntries);
354     UnrealizeObject( hpalette );
355     GDI_ReleaseObj( hpalette );
356     return count;
357 }
358
359
360 /***********************************************************************
361  * ResizePalette [GDI32.@]  Resizes logical palette
362  *
363  * RETURNS
364  *    Success: TRUE
365  *    Failure: FALSE
366  */
367 BOOL WINAPI ResizePalette(
368     HPALETTE hPal, /* [in] Handle of logical palette */
369     UINT cEntries) /* [in] Number of entries in logical palette */
370 {
371     PALETTEOBJ * palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
372     UINT         cPrevEnt, prevVer;
373     int          prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
374     int*         mapping = NULL;
375
376     TRACE("hpal = %p, prev = %i, new = %i\n",
377           hPal, palPtr ? palPtr->logpalette.palNumEntries : -1, 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 [GDI32.@]  Replaces entries in logical palette
418  *
419  * RETURNS
420  *    Success: TRUE
421  *    Failure: FALSE
422  *
423  * FIXME
424  *    Should use existing mapping when animating a primary palette
425  */
426 BOOL WINAPI AnimatePalette(
427     HPALETTE hPal,              /* [in] Handle to logical palette */
428     UINT StartIndex,            /* [in] First entry in palette */
429     UINT NumEntries,            /* [in] Count of entries in palette */
430     const PALETTEENTRY* PaletteColors) /* [in] Pointer to first replacement */
431 {
432     TRACE("%p (%i - %i)\n", hPal, StartIndex,StartIndex+NumEntries);
433
434     if( hPal != GetStockObject(DEFAULT_PALETTE) )
435     {
436         if (!SetPaletteEntries( hPal, StartIndex, NumEntries, PaletteColors )) return FALSE;
437
438         if (pLastRealizedDC && pLastRealizedDC->pRealizePalette)
439             pLastRealizedDC->pRealizePalette( NULL, hPal, hPal == hPrimaryPalette );
440     }
441     return TRUE;
442 }
443
444
445 /***********************************************************************
446  * SetSystemPaletteUse [GDI32.@]
447  *
448  * RETURNS
449  *    Success: Previous system palette
450  *    Failure: SYSPAL_ERROR
451  */
452 UINT WINAPI SetSystemPaletteUse(
453     HDC hdc,  /* [in] Handle of device context */
454     UINT use) /* [in] Palette-usage flag */
455 {
456     UINT old = SystemPaletteUse;
457
458     /* Device doesn't support colour palettes */
459     if (!(GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)) {
460         return SYSPAL_ERROR;
461     }
462
463     switch (use) {
464         case SYSPAL_NOSTATIC:
465         case SYSPAL_NOSTATIC256:        /* WINVER >= 0x0500 */
466         case SYSPAL_STATIC:
467             SystemPaletteUse = use;
468             return old;
469         default:
470             return SYSPAL_ERROR;
471     }
472 }
473
474
475 /***********************************************************************
476  * GetSystemPaletteUse [GDI32.@]  Gets state of system palette
477  *
478  * RETURNS
479  *    Current state of system palette
480  */
481 UINT WINAPI GetSystemPaletteUse(
482     HDC hdc) /* [in] Handle of device context */
483 {
484     return SystemPaletteUse;
485 }
486
487
488 /***********************************************************************
489  * GetSystemPaletteEntries [GDI32.@]  Gets range of palette entries
490  *
491  * RETURNS
492  *    Success: Number of entries retrieved from palette
493  *    Failure: 0
494  */
495 UINT WINAPI GetSystemPaletteEntries(
496     HDC hdc,              /* [in]  Handle of device context */
497     UINT start,           /* [in]  Index of first entry to be retrieved */
498     UINT count,           /* [in]  Number of entries to be retrieved */
499     LPPALETTEENTRY entries) /* [out] Array receiving system-palette entries */
500 {
501     UINT ret = 0;
502     DC *dc;
503
504     TRACE("hdc=%p,start=%i,count=%i\n", hdc,start,count);
505
506     if ((dc = DC_GetDCPtr( hdc )))
507     {
508         if (dc->funcs->pGetSystemPaletteEntries)
509             ret = dc->funcs->pGetSystemPaletteEntries( dc->physDev, start, count, entries );
510         GDI_ReleaseObj( hdc );
511     }
512     return ret;
513 }
514
515
516 /***********************************************************************
517  * GetNearestPaletteIndex [GDI32.@]  Gets palette index for color
518  *
519  * NOTES
520  *    Should index be initialized to CLR_INVALID instead of 0?
521  *
522  * RETURNS
523  *    Success: Index of entry in logical palette
524  *    Failure: CLR_INVALID
525  */
526 UINT WINAPI GetNearestPaletteIndex(
527     HPALETTE hpalette, /* [in] Handle of logical color palette */
528     COLORREF color)      /* [in] Color to be matched */
529 {
530     PALETTEOBJ* palObj = (PALETTEOBJ*)GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
531     UINT index  = 0;
532
533     if( palObj )
534     {
535         int i, diff = 0x7fffffff;
536         int r,g,b;
537         PALETTEENTRY* entry = palObj->logpalette.palPalEntry;
538
539         for( i = 0; i < palObj->logpalette.palNumEntries && diff ; i++, entry++)
540         {
541             if (!(entry->peFlags & PC_SYS_USED)) continue;
542
543             r = entry->peRed - GetRValue(color);
544             g = entry->peGreen - GetGValue(color);
545             b = entry->peBlue - GetBValue(color);
546
547             r = r*r + g*g + b*b;
548
549             if( r < diff ) { index = i; diff = r; }
550         }
551         GDI_ReleaseObj( hpalette );
552     }
553     TRACE("(%p,%06lx): returning %d\n", hpalette, color, index );
554     return index;
555 }
556
557
558 /***********************************************************************
559  * GetNearestColor [GDI32.@]  Gets a system color to match
560  *
561  * RETURNS
562  *    Success: Color from system palette that corresponds to given color
563  *    Failure: CLR_INVALID
564  */
565 COLORREF WINAPI GetNearestColor(
566     HDC hdc,      /* [in] Handle of device context */
567     COLORREF color) /* [in] Color to be matched */
568 {
569     unsigned char spec_type;
570     COLORREF nearest;
571     DC          *dc;
572
573     if (!(dc = DC_GetDCPtr( hdc ))) return CLR_INVALID;
574
575     if (dc->funcs->pGetNearestColor)
576     {
577         nearest = dc->funcs->pGetNearestColor( dc->physDev, color );
578         GDI_ReleaseObj( hdc );
579         return nearest;
580     }
581
582     if (!(GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE))
583     {
584         GDI_ReleaseObj( hdc );
585         return color;
586     }
587
588     spec_type = color >> 24;
589     if (spec_type == 1 || spec_type == 2)
590     {
591         /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
592
593         UINT index;
594         PALETTEENTRY entry;
595         HPALETTE hpal = dc->hPalette ? dc->hPalette : GetStockObject( DEFAULT_PALETTE );
596
597         if (spec_type == 2) /* PALETTERGB */
598             index = GetNearestPaletteIndex( hpal, color );
599         else  /* PALETTEINDEX */
600             index = LOWORD(color);
601
602         if (!GetPaletteEntries( hpal, index, 1, &entry ))
603         {
604             WARN("RGB(%lx) : idx %d is out of bounds, assuming NULL\n", color, index );
605             if (!GetPaletteEntries( hpal, 0, 1, &entry ))
606             {
607                 GDI_ReleaseObj( hdc );
608                 return CLR_INVALID;
609             }
610         }
611         color = RGB( entry.peRed, entry.peGreen, entry.peBlue );
612     }
613     nearest = color & 0x00ffffff;
614     GDI_ReleaseObj( hdc );
615
616     TRACE("(%06lx): returning %06lx\n", color, nearest );
617     return nearest;
618 }
619
620
621 /***********************************************************************
622  *           PALETTE_GetObject
623  */
624 static INT PALETTE_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
625 {
626     PALETTEOBJ *palette = obj;
627
628     if (count > sizeof(WORD)) count = sizeof(WORD);
629     memcpy( buffer, &palette->logpalette.palNumEntries, count );
630     return count;
631 }
632
633
634 /***********************************************************************
635  *           PALETTE_UnrealizeObject
636  */
637 static BOOL PALETTE_UnrealizeObject( HGDIOBJ handle, void *obj )
638 {
639     PALETTEOBJ *palette = obj;
640
641     if (palette->mapping)
642     {
643         HeapFree( GetProcessHeap(), 0, palette->mapping );
644         palette->mapping = NULL;
645     }
646     if (hLastRealizedPalette == handle)
647     {
648         hLastRealizedPalette = 0;
649         pLastRealizedDC = NULL;
650     }
651     return TRUE;
652 }
653
654
655 /***********************************************************************
656  *           PALETTE_DeleteObject
657  */
658 static BOOL PALETTE_DeleteObject( HGDIOBJ handle, void *obj )
659 {
660     PALETTEOBJ *palette = obj;
661
662     HeapFree( GetProcessHeap(), 0, palette->mapping );
663     if (hLastRealizedPalette == handle)
664     {
665         hLastRealizedPalette = 0;
666         pLastRealizedDC = NULL;
667     }
668     return GDI_FreeObject( handle, obj );
669 }
670
671
672 /***********************************************************************
673  *           GDISelectPalette    (Not a Windows API)
674  */
675 HPALETTE WINAPI GDISelectPalette( HDC hdc, HPALETTE hpal, WORD wBkg)
676 {
677     HPALETTE prev;
678     DC *dc;
679
680     TRACE("%p %p\n", hdc, hpal );
681
682     if (GetObjectType(hpal) != OBJ_PAL)
683     {
684       WARN("invalid selected palette %p\n",hpal);
685       return 0;
686     }
687     if (!(dc = DC_GetDCPtr( hdc ))) return 0;
688     prev = dc->hPalette;
689     dc->hPalette = hpal;
690     GDI_ReleaseObj( hdc );
691     if (!wBkg) hPrimaryPalette = hpal;
692     return prev;
693 }
694
695
696 /***********************************************************************
697  *           GDIRealizePalette    (Not a Windows API)
698  */
699 UINT WINAPI GDIRealizePalette( HDC hdc )
700 {
701     UINT realized = 0;
702     DC* dc = DC_GetDCPtr( hdc );
703
704     if (!dc) return 0;
705
706     TRACE("%p...\n", hdc );
707
708     if( dc->hPalette == GetStockObject( DEFAULT_PALETTE ))
709     {
710         if (dc->funcs->pRealizeDefaultPalette)
711             realized = dc->funcs->pRealizeDefaultPalette( dc->physDev );
712     }
713     else if(dc->hPalette != hLastRealizedPalette )
714     {
715         if (dc->funcs->pRealizePalette)
716             realized = dc->funcs->pRealizePalette( dc->physDev, dc->hPalette,
717                                                    (dc->hPalette == hPrimaryPalette) );
718         hLastRealizedPalette = dc->hPalette;
719         pLastRealizedDC = dc->funcs;
720     }
721     else TRACE("  skipping (hLastRealizedPalette = %p)\n", hLastRealizedPalette);
722
723     GDI_ReleaseObj( hdc );
724     TRACE("   realized %i colors.\n", realized );
725     return realized;
726 }
727
728
729 /***********************************************************************
730  *           RealizeDefaultPalette    (GDI.365)
731  */
732 UINT16 WINAPI RealizeDefaultPalette16( HDC16 hdc )
733 {
734     UINT16 ret = 0;
735     DC          *dc;
736
737     TRACE("%04x\n", hdc );
738
739     if (!(dc = DC_GetDCPtr( HDC_32(hdc) ))) return 0;
740
741     if (dc->funcs->pRealizeDefaultPalette) ret = dc->funcs->pRealizeDefaultPalette( dc->physDev );
742     GDI_ReleaseObj( HDC_32(hdc) );
743     return ret;
744 }
745
746 /***********************************************************************
747  *           IsDCCurrentPalette   (GDI.412)
748  */
749 BOOL16 WINAPI IsDCCurrentPalette16(HDC16 hDC)
750 {
751     DC *dc = DC_GetDCPtr( HDC_32(hDC) );
752     if (dc)
753     {
754       BOOL bRet = dc->hPalette == hPrimaryPalette;
755       GDI_ReleaseObj( HDC_32(hDC) );
756       return bRet;
757     }
758     return FALSE;
759 }
760
761
762 /***********************************************************************
763  * SelectPalette [GDI32.@]  Selects logical palette into DC
764  *
765  * RETURNS
766  *    Success: Previous logical palette
767  *    Failure: NULL
768  */
769 HPALETTE WINAPI SelectPalette(
770     HDC hDC,               /* [in] Handle of device context */
771     HPALETTE hPal,         /* [in] Handle of logical color palette */
772     BOOL bForceBackground) /* [in] Foreground/background mode */
773 {
774     return pfnSelectPalette( hDC, hPal, bForceBackground );
775 }
776
777
778 /***********************************************************************
779  * RealizePalette [GDI32.@]  Maps palette entries to system palette
780  *
781  * RETURNS
782  *    Success: Number of entries in logical palette
783  *    Failure: GDI_ERROR
784  */
785 UINT WINAPI RealizePalette(
786     HDC hDC) /* [in] Handle of device context */
787 {
788     return pfnRealizePalette( hDC );
789 }
790
791
792 typedef HWND (WINAPI *WindowFromDC_funcptr)( HDC );
793 typedef BOOL (WINAPI *RedrawWindow_funcptr)( HWND, const RECT *, HRGN, UINT );
794
795 /**********************************************************************
796  * UpdateColors [GDI32.@]  Remaps current colors to logical palette
797  *
798  * RETURNS
799  *    Success: TRUE
800  *    Failure: FALSE
801  */
802 BOOL WINAPI UpdateColors(
803     HDC hDC) /* [in] Handle of device context */
804 {
805     HMODULE mod;
806     int size = GetDeviceCaps( hDC, SIZEPALETTE );
807
808     if (!size) return 0;
809
810     mod = GetModuleHandleA("user32.dll");
811     if (mod)
812     {
813         WindowFromDC_funcptr pWindowFromDC = (WindowFromDC_funcptr)GetProcAddress(mod,"WindowFromDC");
814         if (pWindowFromDC)
815         {
816             HWND hWnd = pWindowFromDC( hDC );
817
818             /* Docs say that we have to remap current drawable pixel by pixel
819              * but it would take forever given the speed of XGet/PutPixel.
820              */
821             if (hWnd && size)
822             {
823                 RedrawWindow_funcptr pRedrawWindow = GetProcAddress( mod, "RedrawWindow" );
824                 if (pRedrawWindow) pRedrawWindow( hWnd, NULL, 0, RDW_INVALIDATE );
825             }
826         }
827     }
828     return 0x666;
829 }
830
831
832 /*********************************************************************
833  *           SetMagicColors   (GDI.606)
834  */
835 VOID WINAPI SetMagicColors16(HDC16 hDC, COLORREF color, UINT16 index)
836 {
837     FIXME("(hDC %04x, color %04x, index %04x): stub\n", hDC, (int)color, index);
838
839 }
840
841 /**********************************************************************
842  * GetICMProfileA [GDI32.@]
843  *
844  * Returns the filename of the specified device context's color
845  * management profile, even if color management is not enabled
846  * for that DC.
847  *
848  * RETURNS
849  *    TRUE if name copied succesfully OR lpszFilename is NULL
850  *    FALSE if the buffer length pointed to by lpcbName is too small
851  *
852  * NOTE
853  *    The buffer length pointed to by lpcbName is ALWAYS updated to
854  *    the length required regardless of other actions this function
855  *    may take.
856  *
857  * FIXME
858  *    How does Windows assign these?  Some registry key?
859  */
860
861 #define WINEICM "winefake.icm"  /* easy-to-identify fake filename */
862
863 /*********************************************************************/
864
865 BOOL WINAPI GetICMProfileA(HDC hDC, LPDWORD lpcbName, LPSTR lpszFilename)
866 {
867     DWORD callerLen;
868
869     FIXME("(%p, %p, %p): partial stub\n", hDC, lpcbName, lpszFilename);
870
871     callerLen = *lpcbName;
872
873     /* all 3 behaviors require the required buffer size to be set */
874     *lpcbName = strlen(WINEICM);
875
876     /* behavior 1: if lpszFilename is NULL, return size of string and no error */
877     if ((DWORD)lpszFilename == (DWORD)0x00000000)
878         return TRUE;
879
880     /* behavior 2: if buffer size too small, return size of string and error */
881     if (callerLen < strlen(WINEICM))
882     {
883         SetLastError(ERROR_INSUFFICIENT_BUFFER);
884         return FALSE;
885     }
886
887     /* behavior 3: if buffer size OK and pointer not NULL, copy and return size */
888     strcpy(lpszFilename, WINEICM);
889     return TRUE;
890 }