Moved some more code to the X11 driver.
[wine.git] / objects / palette.c
blob44469fb842629ee0703897768dcd897bda15a811
1 /*
2 * GDI palette objects
4 * Copyright 1993,1994 Alexandre Julliard
5 * Copyright 1996 Alex Korobka
7 * PALETTEOBJ is documented in the Dr. Dobbs Journal May 1993.
8 * Information in the "Undocumented Windows" is incorrect.
9 */
11 #include <stdlib.h>
12 #include <string.h>
13 #include "gdi.h"
14 #include "color.h"
15 #include "palette.h"
16 #include "xmalloc.h"
17 #include "debug.h"
18 #include "wine/winuser16.h"
20 FARPROC32 pfnSelectPalette = NULL;
21 FARPROC32 pfnRealizePalette = NULL;
23 static UINT32 SystemPaletteUse = SYSPAL_STATIC; /* currently not considered */
25 static HPALETTE16 hPrimaryPalette = 0; /* used for WM_PALETTECHANGED */
26 static HPALETTE16 hLastRealizedPalette = 0; /* UnrealizeObject() needs it */
29 /***********************************************************************
30 * PALETTE_Init
32 * Create the system palette.
34 HPALETTE16 PALETTE_Init(void)
36 int i;
37 HPALETTE16 hpalette;
38 LOGPALETTE * palPtr;
39 PALETTEOBJ* palObj;
40 const PALETTEENTRY* __sysPalTemplate = COLOR_GetSystemPaletteTemplate();
42 /* create default palette (20 system colors) */
44 palPtr = HeapAlloc( GetProcessHeap(), 0,
45 sizeof(LOGPALETTE) + (NB_RESERVED_COLORS-1)*sizeof(PALETTEENTRY));
46 if (!palPtr) return FALSE;
48 palPtr->palVersion = 0x300;
49 palPtr->palNumEntries = NB_RESERVED_COLORS;
50 for( i = 0; i < NB_RESERVED_COLORS; i ++ )
52 palPtr->palPalEntry[i].peRed = __sysPalTemplate[i].peRed;
53 palPtr->palPalEntry[i].peGreen = __sysPalTemplate[i].peGreen;
54 palPtr->palPalEntry[i].peBlue = __sysPalTemplate[i].peBlue;
55 palPtr->palPalEntry[i].peFlags = 0;
57 hpalette = CreatePalette16( palPtr );
59 palObj = (PALETTEOBJ*) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
60 if (palObj)
62 palObj->mapping = xmalloc( sizeof(int) * 20 );
64 GDI_HEAP_UNLOCK( hpalette );
66 HeapFree( GetProcessHeap(), 0, palPtr );
69 return hpalette;
72 /***********************************************************************
73 * PALETTE_ValidateFlags
75 void PALETTE_ValidateFlags(PALETTEENTRY* lpPalE, int size)
77 int i = 0;
78 for( ; i<size ; i++ )
79 lpPalE[i].peFlags = PC_SYS_USED | (lpPalE[i].peFlags & 0x07);
83 /***********************************************************************
84 * CreatePalette16 (GDI.360)
86 HPALETTE16 WINAPI CreatePalette16( const LOGPALETTE* palette )
88 return CreatePalette32( palette );
92 /***********************************************************************
93 * CreatePalette32 [GDI32.53] Creates a logical color palette
95 * RETURNS
96 * Success: Handle to logical palette
97 * Failure: NULL
99 HPALETTE32 WINAPI CreatePalette32(
100 const LOGPALETTE* palette) /* [in] Pointer to logical color palette */
102 PALETTEOBJ * palettePtr;
103 HPALETTE32 hpalette;
104 int size;
106 if (!palette) return 0;
107 TRACE(palette,"entries=%i\n", palette->palNumEntries);
109 size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
111 hpalette = GDI_AllocObject( size + sizeof(int*) +sizeof(GDIOBJHDR) , PALETTE_MAGIC );
112 if (!hpalette) return 0;
114 palettePtr = (PALETTEOBJ *) GDI_HEAP_LOCK( hpalette );
115 memcpy( &palettePtr->logpalette, palette, size );
116 PALETTE_ValidateFlags(palettePtr->logpalette.palPalEntry,
117 palettePtr->logpalette.palNumEntries);
118 palettePtr->mapping = NULL;
119 GDI_HEAP_UNLOCK( hpalette );
121 TRACE(palette," returning %04x\n", hpalette);
122 return hpalette;
126 /***********************************************************************
127 * CreateHalftonePalette [GDI32.47] Creates a halftone palette
129 * RETURNS
130 * Success: Handle to logical halftone palette
131 * Failure: 0
133 * FIXME: not truly tested
135 HPALETTE32 WINAPI CreateHalftonePalette(HDC32 hdc) /* [in] Handle to device context */
136 { int r,g,b,i;
137 HPALETTE32 hPalette = 0;
138 int palNumEntries = 216 + NB_RESERVED_COLORS;
140 const PALETTEENTRY* __sysPalTemplate = COLOR_GetSystemPaletteTemplate();
142 LOGPALETTE * pLogPal = (LOGPALETTE*) HeapAlloc( GetProcessHeap(), 0,
143 sizeof(LOGPALETTE) + (palNumEntries-1)*sizeof(PALETTEENTRY));
145 TRACE(palette,"(0x%x)\n", hdc);
147 pLogPal->palVersion = 0x300;
148 pLogPal->palNumEntries = palNumEntries;
150 for( i = 0; i < NB_RESERVED_COLORS; i ++ )
151 { pLogPal->palPalEntry[i].peRed = __sysPalTemplate[i].peRed;
152 pLogPal->palPalEntry[i].peGreen = __sysPalTemplate[i].peGreen;
153 pLogPal->palPalEntry[i].peBlue = __sysPalTemplate[i].peBlue;
154 pLogPal->palPalEntry[i].peFlags = 0;
157 for (r=0; r<6; r++)
158 { for (g=0; g<6; g++)
159 { for (b=0; b<6; b++)
160 { pLogPal->palPalEntry[NB_RESERVED_COLORS+r*36+g*6+b].peRed = r*51;
161 pLogPal->palPalEntry[NB_RESERVED_COLORS+r*36+g*6+b].peGreen = g*51;
162 pLogPal->palPalEntry[NB_RESERVED_COLORS+r*36+g*6+b].peBlue = b*51;
163 pLogPal->palPalEntry[NB_RESERVED_COLORS+r*36+g*6+b].peFlags = 0;
167 hPalette = CreatePalette32 (pLogPal);
169 if (hPalette)
170 { SelectPalette32 (hdc, hPalette, FALSE);
173 HeapFree (GetProcessHeap(), 0, pLogPal);
174 return hPalette;
178 /***********************************************************************
179 * GetPaletteEntries16 (GDI.363)
181 UINT16 WINAPI GetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
182 UINT16 count, LPPALETTEENTRY entries )
184 return GetPaletteEntries32( hpalette, start, count, entries );
188 /***********************************************************************
189 * GetPaletteEntries32 [GDI32.209] Retrieves palette entries
191 * RETURNS
192 * Success: Number of entries from logical palette
193 * Failure: 0
195 UINT32 WINAPI GetPaletteEntries32(
196 HPALETTE32 hpalette, /* [in] Handle of logical palette */
197 UINT32 start, /* [in] First entry to receive */
198 UINT32 count, /* [in] Number of entries to receive */
199 LPPALETTEENTRY entries) /* [out] Address of array receiving entries */
201 PALETTEOBJ * palPtr;
202 INT32 numEntries;
204 TRACE(palette,"hpal = %04x, count=%i\n", hpalette, count );
206 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
207 if (!palPtr) return 0;
209 numEntries = palPtr->logpalette.palNumEntries;
210 if (start+count > numEntries) count = numEntries - start;
211 if (entries)
213 if (start >= numEntries)
215 GDI_HEAP_UNLOCK( hpalette );
216 return 0;
218 memcpy( entries, &palPtr->logpalette.palPalEntry[start],
219 count * sizeof(PALETTEENTRY) );
220 for( numEntries = 0; numEntries < count ; numEntries++ )
221 if (entries[numEntries].peFlags & 0xF0)
222 entries[numEntries].peFlags = 0;
223 GDI_HEAP_UNLOCK( hpalette );
226 return count;
230 /***********************************************************************
231 * SetPaletteEntries16 (GDI.364)
233 UINT16 WINAPI SetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
234 UINT16 count, LPPALETTEENTRY entries )
236 return SetPaletteEntries32( hpalette, start, count, entries );
240 /***********************************************************************
241 * SetPaletteEntries32 [GDI32.326] Sets color values for range in palette
243 * RETURNS
244 * Success: Number of entries that were set
245 * Failure: 0
247 UINT32 WINAPI SetPaletteEntries32(
248 HPALETTE32 hpalette, /* [in] Handle of logical palette */
249 UINT32 start, /* [in] Index of first entry to set */
250 UINT32 count, /* [in] Number of entries to set */
251 LPPALETTEENTRY entries) /* [in] Address of array of structures */
253 PALETTEOBJ * palPtr;
254 INT32 numEntries;
256 TRACE(palette,"hpal=%04x,start=%i,count=%i\n",hpalette,start,count );
258 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
259 if (!palPtr) return 0;
261 numEntries = palPtr->logpalette.palNumEntries;
262 if (start >= numEntries)
264 GDI_HEAP_UNLOCK( hpalette );
265 return 0;
267 if (start+count > numEntries) count = numEntries - start;
268 memcpy( &palPtr->logpalette.palPalEntry[start], entries,
269 count * sizeof(PALETTEENTRY) );
270 PALETTE_ValidateFlags(palPtr->logpalette.palPalEntry,
271 palPtr->logpalette.palNumEntries);
272 free(palPtr->mapping);
273 palPtr->mapping = NULL;
274 GDI_HEAP_UNLOCK( hpalette );
275 return count;
279 /***********************************************************************
280 * ResizePalette16 (GDI.368)
282 BOOL16 WINAPI ResizePalette16( HPALETTE16 hPal, UINT16 cEntries )
284 return ResizePalette32( hPal, cEntries );
288 /***********************************************************************
289 * ResizePalette32 [GDI32.289] Resizes logical palette
291 * RETURNS
292 * Success: TRUE
293 * Failure: FALSE
295 BOOL32 WINAPI ResizePalette32(
296 HPALETTE32 hPal, /* [in] Handle of logical palette */
297 UINT32 cEntries) /* [in] Number of entries in logical palette */
299 PALETTEOBJ * palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
300 UINT32 cPrevEnt, prevVer;
301 int prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
302 int* mapping = NULL;
304 TRACE(palette,"hpal = %04x, prev = %i, new = %i\n",
305 hPal, palPtr ? palPtr->logpalette.palNumEntries : -1,
306 cEntries );
307 if( !palPtr ) return FALSE;
308 cPrevEnt = palPtr->logpalette.palNumEntries;
309 prevVer = palPtr->logpalette.palVersion;
310 prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) +
311 sizeof(int*) + sizeof(GDIOBJHDR);
312 size += sizeof(int*) + sizeof(GDIOBJHDR);
313 mapping = palPtr->mapping;
315 GDI_HEAP_UNLOCK( hPal );
317 hPal = GDI_HEAP_REALLOC( hPal, size );
318 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
319 if( !palPtr ) return FALSE;
321 if( mapping )
322 palPtr->mapping = (int*) xrealloc( mapping, cEntries * sizeof(int) );
323 if( cEntries > cPrevEnt )
325 if( mapping )
326 memset(palPtr->mapping + cPrevEnt, 0, (cEntries - cPrevEnt)*sizeof(int));
327 memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
328 PALETTE_ValidateFlags((PALETTEENTRY*)((BYTE*)palPtr + prevsize),
329 cEntries - cPrevEnt );
331 palPtr->logpalette.palNumEntries = cEntries;
332 palPtr->logpalette.palVersion = prevVer;
333 GDI_HEAP_UNLOCK( hPal );
334 return TRUE;
338 /***********************************************************************
339 * AnimatePalette16 (GDI.367)
341 void WINAPI AnimatePalette16( HPALETTE16 hPal, UINT16 StartIndex,
342 UINT16 NumEntries, const PALETTEENTRY* PaletteColors)
344 AnimatePalette32( hPal, StartIndex, NumEntries, PaletteColors );
348 /***********************************************************************
349 * AnimatePalette32 [GDI32.6] Replaces entries in logical palette
351 * RETURNS
352 * Success: TRUE
353 * Failure: FALSE
355 * FIXME
356 * Should use existing mapping when animating a primary palette
358 BOOL32 WINAPI AnimatePalette32(
359 HPALETTE32 hPal, /* [in] Handle to logical palette */
360 UINT32 StartIndex, /* [in] First entry in palette */
361 UINT32 NumEntries, /* [in] Count of entries in palette */
362 const PALETTEENTRY* PaletteColors) /* [in] Pointer to first replacement */
364 TRACE(palette, "%04x (%i - %i)\n", hPal, StartIndex,StartIndex+NumEntries);
366 if( hPal != STOCK_DEFAULT_PALETTE )
368 PALETTEOBJ* palPtr = (PALETTEOBJ *)GDI_GetObjPtr(hPal, PALETTE_MAGIC);
369 if (!palPtr) return FALSE;
371 if( (StartIndex + NumEntries) <= palPtr->logpalette.palNumEntries )
373 UINT32 u;
374 for( u = 0; u < NumEntries; u++ )
375 palPtr->logpalette.palPalEntry[u + StartIndex] = PaletteColors[u];
376 COLOR_SetMapping(palPtr, StartIndex, NumEntries,
377 hPal != hPrimaryPalette );
378 GDI_HEAP_UNLOCK( hPal );
379 return TRUE;
382 return FALSE;
386 /***********************************************************************
387 * SetSystemPaletteUse16 (GDI.373)
389 UINT16 WINAPI SetSystemPaletteUse16( HDC16 hdc, UINT16 use )
391 return SetSystemPaletteUse32( hdc, use );
395 /***********************************************************************
396 * SetSystemPaletteUse32 [GDI32.335]
398 * RETURNS
399 * Success: Previous system palette
400 * Failure: SYSPAL_ERROR
402 UINT32 WINAPI SetSystemPaletteUse32(
403 HDC32 hdc, /* [in] Handle of device context */
404 UINT32 use) /* [in] Palette-usage flag */
406 UINT32 old = SystemPaletteUse;
407 FIXME(palette,"(%04x,%04x): stub\n", hdc, use );
408 SystemPaletteUse = use;
409 return old;
413 /***********************************************************************
414 * GetSystemPaletteUse16 (GDI.374)
416 UINT16 WINAPI GetSystemPaletteUse16( HDC16 hdc )
418 return SystemPaletteUse;
422 /***********************************************************************
423 * GetSystemPaletteUse32 [GDI32.223] Gets state of system palette
425 * RETURNS
426 * Current state of system palette
428 UINT32 WINAPI GetSystemPaletteUse32(
429 HDC32 hdc) /* [in] Handle of device context */
431 return SystemPaletteUse;
435 /***********************************************************************
436 * GetSystemPaletteEntries16 (GDI.375)
438 UINT16 WINAPI GetSystemPaletteEntries16( HDC16 hdc, UINT16 start, UINT16 count,
439 LPPALETTEENTRY entries )
441 return GetSystemPaletteEntries32( hdc, start, count, entries );
445 /***********************************************************************
446 * GetSystemPaletteEntries32 [GDI32.222] Gets range of palette entries
448 * RETURNS
449 * Success: Number of entries retrieved from palette
450 * Failure: 0
452 UINT32 WINAPI GetSystemPaletteEntries32(
453 HDC32 hdc, /* [in] Handle of device context */
454 UINT32 start, /* [in] Index of first entry to be retrieved */
455 UINT32 count, /* [in] Number of entries to be retrieved */
456 LPPALETTEENTRY entries) /* [out] Array receiving system-palette entries */
458 UINT32 i;
459 DC *dc;
461 TRACE(palette, "hdc=%04x,start=%i,count=%i\n", hdc,start,count);
463 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
464 if (!entries) return COLOR_GetSystemPaletteSize();
465 if (start >= dc->w.devCaps->sizePalette)
467 GDI_HEAP_UNLOCK( hdc );
468 return 0;
470 if (start+count >= dc->w.devCaps->sizePalette)
471 count = dc->w.devCaps->sizePalette - start;
472 for (i = 0; i < count; i++)
474 *(COLORREF*)(entries + i) = COLOR_GetSystemPaletteEntry( start + i );
476 TRACE(palette,"\tidx(%02x) -> RGB(%08lx)\n",
477 start + i, *(COLORREF*)(entries + i) );
479 GDI_HEAP_UNLOCK( hdc );
480 return count;
484 /***********************************************************************
485 * GetNearestPaletteIndex16 (GDI.370)
487 UINT16 WINAPI GetNearestPaletteIndex16( HPALETTE16 hpalette, COLORREF color )
489 return GetNearestPaletteIndex32( hpalette, color );
493 /***********************************************************************
494 * GetNearestPaletteIndex32 [GDI32.203] Gets palette index for color
496 * NOTES
497 * Should index be initialized to CLR_INVALID instead of 0?
499 * RETURNS
500 * Success: Index of entry in logical palette
501 * Failure: CLR_INVALID
503 UINT32 WINAPI GetNearestPaletteIndex32(
504 HPALETTE32 hpalette, /* [in] Handle of logical color palette */
505 COLORREF color) /* [in] Color to be matched */
507 PALETTEOBJ* palObj = (PALETTEOBJ*)GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
508 UINT32 index = 0;
510 if( palObj )
511 index = COLOR_PaletteLookupPixel( palObj->logpalette.palPalEntry,
512 palObj->logpalette.palNumEntries,
513 NULL, color, FALSE );
515 TRACE(palette,"(%04x,%06lx): returning %d\n", hpalette, color, index );
516 GDI_HEAP_UNLOCK( hpalette );
517 return index;
521 /***********************************************************************
522 * GetNearestColor16 (GDI.154)
524 COLORREF WINAPI GetNearestColor16( HDC16 hdc, COLORREF color )
526 return GetNearestColor32( hdc, color );
530 /***********************************************************************
531 * GetNearestColor32 [GDI32.202] Gets a system color to match
533 * NOTES
534 * Should this return CLR_INVALID instead of FadeCafe?
536 * RETURNS
537 * Success: Color from system palette that corresponds to given color
538 * Failure: CLR_INVALID
540 COLORREF WINAPI GetNearestColor32(
541 HDC32 hdc, /* [in] Handle of device context */
542 COLORREF color) /* [in] Color to be matched */
544 COLORREF nearest = 0xFADECAFE;
545 DC *dc;
546 PALETTEOBJ *palObj;
548 if ( (dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC )) )
550 palObj = (PALETTEOBJ*)
551 GDI_GetObjPtr( (dc->w.hPalette)? dc->w.hPalette
552 : STOCK_DEFAULT_PALETTE, PALETTE_MAGIC );
553 if (!palObj) return nearest;
555 nearest = COLOR_LookupNearestColor( palObj->logpalette.palPalEntry,
556 palObj->logpalette.palNumEntries, color );
557 GDI_HEAP_UNLOCK( dc->w.hPalette );
560 TRACE(palette,"(%06lx): returning %06lx\n", color, nearest );
561 GDI_HEAP_UNLOCK( hdc );
562 return nearest;
566 /***********************************************************************
567 * PALETTE_GetObject
569 int PALETTE_GetObject( PALETTEOBJ * palette, int count, LPSTR buffer )
571 if (count > sizeof(WORD)) count = sizeof(WORD);
572 memcpy( buffer, &palette->logpalette.palNumEntries, count );
573 return count;
577 /***********************************************************************
578 * PALETTE_UnrealizeObject
580 BOOL32 PALETTE_UnrealizeObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
582 if (palette->mapping)
584 free( palette->mapping );
585 palette->mapping = NULL;
587 if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
588 return TRUE;
592 /***********************************************************************
593 * PALETTE_DeleteObject
595 BOOL32 PALETTE_DeleteObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
597 free( palette->mapping );
598 if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
599 return GDI_FreeObject( hpalette );
603 /***********************************************************************
604 * GDISelectPalette (GDI.361)
606 HPALETTE16 WINAPI GDISelectPalette( HDC16 hdc, HPALETTE16 hpal, WORD wBkg)
608 HPALETTE16 prev;
609 DC *dc;
611 TRACE(palette, "%04x %04x\n", hdc, hpal );
613 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
614 if (!dc)
616 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
617 if (!dc) return 0;
619 prev = dc->w.hPalette;
620 dc->w.hPalette = hpal;
621 GDI_HEAP_UNLOCK( hdc );
622 if (!wBkg) hPrimaryPalette = hpal;
623 return prev;
627 /***********************************************************************
628 * GDIRealizePalette (GDI.362)
630 UINT16 WINAPI GDIRealizePalette( HDC16 hdc )
632 PALETTEOBJ* palPtr;
633 int realized = 0;
634 DC* dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
635 if (!dc)
637 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
638 if (!dc) return 0;
641 TRACE(palette, "%04x...\n", hdc );
643 if( dc && dc->w.hPalette != hLastRealizedPalette )
645 if( dc->w.hPalette == STOCK_DEFAULT_PALETTE )
646 return RealizeDefaultPalette( hdc );
648 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC );
650 if (!palPtr) {
651 FIXME(palette,"invalid selected palette %04x\n",dc->w.hPalette);
652 return 0;
655 realized = COLOR_SetMapping(palPtr,0,palPtr->logpalette.palNumEntries,
656 (dc->w.hPalette != hPrimaryPalette) ||
657 (dc->w.hPalette == STOCK_DEFAULT_PALETTE));
658 GDI_HEAP_UNLOCK( dc->w.hPalette );
659 hLastRealizedPalette = dc->w.hPalette;
661 else TRACE(palette, " skipping (hLastRealizedPalette = %04x)\n",
662 hLastRealizedPalette);
663 GDI_HEAP_UNLOCK( hdc );
665 TRACE(palette, " realized %i colors.\n", realized );
666 return (UINT16)realized;
670 /***********************************************************************
671 * RealizeDefaultPalette (GDI.365)
673 UINT16 WINAPI RealizeDefaultPalette( HDC16 hdc )
675 DC *dc;
676 PALETTEOBJ* palPtr;
677 int i, index, realized = 0;
679 TRACE(palette,"%04x\n", hdc );
681 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
682 if (!dc)
684 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
685 if (!dc) return 0;
688 if ( dc->w.flags & DC_MEMORY )
690 GDI_HEAP_UNLOCK( hdc );
691 return 0;
694 hPrimaryPalette = STOCK_DEFAULT_PALETTE;
695 hLastRealizedPalette = STOCK_DEFAULT_PALETTE;
697 palPtr = (PALETTEOBJ*)GDI_GetObjPtr(STOCK_DEFAULT_PALETTE, PALETTE_MAGIC );
698 if (!palPtr) return 0;
700 /* lookup is needed to account for SetSystemPaletteUse() stuff */
702 for( i = 0; i < 20; i++ )
704 index = COLOR_LookupSystemPixel(*(COLORREF*)(palPtr->logpalette.palPalEntry + i));
706 /* mapping is allocated in COLOR_InitPalette() */
708 if( index != palPtr->mapping[i] ) { palPtr->mapping[i]=index; realized++; }
710 return realized;
713 /***********************************************************************
714 * IsDCCurrentPalette (GDI.412)
716 BOOL16 WINAPI IsDCCurrentPalette(HDC16 hDC)
718 DC* dc = (DC *)GDI_GetObjPtr( hDC, DC_MAGIC );
719 if (dc)
721 GDI_HEAP_UNLOCK( hDC );
722 return dc->w.hPalette == hPrimaryPalette;
724 return FALSE;
728 /***********************************************************************
729 * SelectPalette16 (USER.282)
731 HPALETTE16 WINAPI SelectPalette16( HDC16 hDC, HPALETTE16 hPal,
732 BOOL16 bForceBackground )
734 return SelectPalette32( hDC, hPal, bForceBackground );
738 /***********************************************************************
739 * SelectPalette32 [GDI32.300] Selects logical palette into DC
741 * RETURNS
742 * Success: Previous logical palette
743 * Failure: NULL
745 HPALETTE32 WINAPI SelectPalette32(
746 HDC32 hDC, /* [in] Handle of device context */
747 HPALETTE32 hPal, /* [in] Handle of logical color palette */
748 BOOL32 bForceBackground) /* [in] Foreground/background mode */
750 WORD wBkgPalette = 1;
751 PALETTEOBJ* lpt = (PALETTEOBJ*) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
753 TRACE(palette,"dc=%04x,pal=%04x,force=%i\n", hDC, hPal, bForceBackground);
754 if( !lpt ) return 0;
756 TRACE(palette," entries = %d\n", lpt->logpalette.palNumEntries);
757 GDI_HEAP_UNLOCK( hPal );
759 if( hPal != STOCK_DEFAULT_PALETTE )
761 HWND32 hWnd = WindowFromDC32( hDC );
762 HWND32 hActive = GetActiveWindow32();
764 /* set primary palette if it's related to current active */
766 if((!hWnd || (hActive == hWnd || IsChild16(hActive,hWnd))) &&
767 !bForceBackground )
768 wBkgPalette = 0;
770 return GDISelectPalette( hDC, hPal, wBkgPalette);
774 /***********************************************************************
775 * RealizePalette16 (USER.283)
777 UINT16 WINAPI RealizePalette16( HDC16 hDC )
779 return RealizePalette32( hDC );
783 /***********************************************************************
784 * RealizePalette32 [GDI32.280] Maps palette entries to system palette
786 * RETURNS
787 * Success: Number of entries in logical palette
788 * Failure: GDI_ERROR
790 UINT32 WINAPI RealizePalette32(
791 HDC32 hDC) /* [in] Handle of device context */
793 UINT32 realized = GDIRealizePalette( hDC );
795 /* do not send anything if no colors were changed */
797 if( IsDCCurrentPalette( hDC ) && realized &&
798 !(COLOR_GetSystemPaletteFlags() & COLOR_VIRTUAL) )
800 /* Send palette change notification */
802 HWND32 hWnd;
803 if( (hWnd = WindowFromDC32( hDC )) )
804 SendMessage16( HWND_BROADCAST, WM_PALETTECHANGED, hWnd, 0L);
806 return realized;
810 /**********************************************************************
811 * UpdateColors16 (GDI.366)
813 INT16 WINAPI UpdateColors16( HDC16 hDC )
815 HWND32 hWnd = WindowFromDC32( hDC );
817 /* Docs say that we have to remap current drawable pixel by pixel
818 * but it would take forever given the speed of XGet/PutPixel.
820 if (hWnd && !(COLOR_GetSystemPaletteFlags() & COLOR_VIRTUAL) )
821 InvalidateRect32( hWnd, NULL, FALSE );
822 return 0x666;
826 /**********************************************************************
827 * UpdateColors32 [GDI32.359] Remaps current colors to logical palette
829 * RETURNS
830 * Success: TRUE
831 * Failure: FALSE
833 BOOL32 WINAPI UpdateColors32(
834 HDC32 hDC) /* [in] Handle of device context */
836 UpdateColors16( hDC );
837 return TRUE;
841 /*********************************************************************
842 * SetMagicColors16 (GDI.606)
844 VOID WINAPI SetMagicColors16(HDC16 hDC, COLORREF color, UINT16 index)
846 FIXME(palette,"(hDC %04x, color %04x, index %04x): stub\n", hDC, (int)color, index);