Change SW_??? options on list box scrolling so that they match what
[wine/testsucceed.git] / objects / palette.c
blob223ecf559dd0a7b97db1afb896553a0cb45533da
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"
19 FARPROC32 pfnSelectPalette = NULL;
20 FARPROC32 pfnRealizePalette = NULL;
22 static UINT32 SystemPaletteUse = SYSPAL_STATIC; /* currently not considered */
24 static HPALETTE16 hPrimaryPalette = 0; /* used for WM_PALETTECHANGED */
25 static HPALETTE16 hLastRealizedPalette = 0; /* UnrealizeObject() needs it */
28 /***********************************************************************
29 * PALETTE_Init
31 * Create the system palette.
33 HPALETTE16 PALETTE_Init(void)
35 int i;
36 HPALETTE16 hpalette;
37 LOGPALETTE * palPtr;
38 PALETTEOBJ* palObj;
39 const PALETTEENTRY* __sysPalTemplate = COLOR_GetSystemPaletteTemplate();
41 /* create default palette (20 system colors) */
43 palPtr = HeapAlloc( GetProcessHeap(), 0,
44 sizeof(LOGPALETTE) + (NB_RESERVED_COLORS-1)*sizeof(PALETTEENTRY));
45 if (!palPtr) return FALSE;
47 palPtr->palVersion = 0x300;
48 palPtr->palNumEntries = NB_RESERVED_COLORS;
49 for( i = 0; i < NB_RESERVED_COLORS; i ++ )
51 palPtr->palPalEntry[i].peRed = __sysPalTemplate[i].peRed;
52 palPtr->palPalEntry[i].peGreen = __sysPalTemplate[i].peGreen;
53 palPtr->palPalEntry[i].peBlue = __sysPalTemplate[i].peBlue;
54 palPtr->palPalEntry[i].peFlags = 0;
56 hpalette = CreatePalette16( palPtr );
58 palObj = (PALETTEOBJ*) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
59 if (palObj)
61 palObj->mapping = xmalloc( sizeof(int) * 20 );
63 GDI_HEAP_UNLOCK( hpalette );
65 HeapFree( GetProcessHeap(), 0, palPtr );
68 return hpalette;
71 /***********************************************************************
72 * PALETTE_ValidateFlags
74 void PALETTE_ValidateFlags(PALETTEENTRY* lpPalE, int size)
76 int i = 0;
77 for( ; i<size ; i++ )
78 lpPalE[i].peFlags = PC_SYS_USED | (lpPalE[i].peFlags & 0x07);
82 /***********************************************************************
83 * CreatePalette16 (GDI.360)
85 HPALETTE16 WINAPI CreatePalette16( const LOGPALETTE* palette )
87 return CreatePalette32( palette );
91 /***********************************************************************
92 * CreatePalette32 [GDI32.53] Creates a logical color palette
94 * RETURNS
95 * Success: Handle to logical palette
96 * Failure: NULL
98 HPALETTE32 WINAPI CreatePalette32(
99 const LOGPALETTE* palette) /* [in] Pointer to logical color palette */
101 PALETTEOBJ * palettePtr;
102 HPALETTE32 hpalette;
103 int size;
105 if (!palette) return 0;
106 TRACE(palette,"entries=%i\n", palette->palNumEntries);
108 size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
110 hpalette = GDI_AllocObject( size + sizeof(int*) +sizeof(GDIOBJHDR) , PALETTE_MAGIC );
111 if (!hpalette) return 0;
113 palettePtr = (PALETTEOBJ *) GDI_HEAP_LOCK( hpalette );
114 memcpy( &palettePtr->logpalette, palette, size );
115 PALETTE_ValidateFlags(palettePtr->logpalette.palPalEntry,
116 palettePtr->logpalette.palNumEntries);
117 palettePtr->mapping = NULL;
118 GDI_HEAP_UNLOCK( hpalette );
120 TRACE(palette," returning %04x\n", hpalette);
121 return hpalette;
125 /***********************************************************************
126 * CreateHalftonePalette [GDI32.47] Creates a halftone palette
128 * RETURNS
129 * Success: Handle to logical halftone palette
130 * Failure: 0
132 * FIXME: not truly tested
134 HPALETTE32 WINAPI CreateHalftonePalette(HDC32 hdc) /* [in] Handle to device context */
135 { int r,g,b,i;
136 HPALETTE32 hPalette = 0;
137 int palNumEntries = 216 + NB_RESERVED_COLORS;
139 const PALETTEENTRY* __sysPalTemplate = COLOR_GetSystemPaletteTemplate();
141 LOGPALETTE * pLogPal = (LOGPALETTE*) HeapAlloc( GetProcessHeap(), 0,
142 sizeof(LOGPALETTE) + (palNumEntries-1)*sizeof(PALETTEENTRY));
144 TRACE(palette,"(0x%x)\n", hdc);
146 pLogPal->palVersion = 0x300;
147 pLogPal->palNumEntries = palNumEntries;
149 for( i = 0; i < NB_RESERVED_COLORS; i ++ )
150 { pLogPal->palPalEntry[i].peRed = __sysPalTemplate[i].peRed;
151 pLogPal->palPalEntry[i].peGreen = __sysPalTemplate[i].peGreen;
152 pLogPal->palPalEntry[i].peBlue = __sysPalTemplate[i].peBlue;
153 pLogPal->palPalEntry[i].peFlags = 0;
156 for (r=0; r<6; r++)
157 { for (g=0; g<6; g++)
158 { for (b=0; b<6; b++)
159 { pLogPal->palPalEntry[NB_RESERVED_COLORS+r*36+g*6+b].peRed = r*51;
160 pLogPal->palPalEntry[NB_RESERVED_COLORS+r*36+g*6+b].peGreen = g*51;
161 pLogPal->palPalEntry[NB_RESERVED_COLORS+r*36+g*6+b].peBlue = b*51;
162 pLogPal->palPalEntry[NB_RESERVED_COLORS+r*36+g*6+b].peFlags = 0;
166 hPalette = CreatePalette32 (pLogPal);
168 if (hPalette)
169 { SelectPalette32 (hdc, hPalette, FALSE);
172 HeapFree (GetProcessHeap(), 0, pLogPal);
173 return hPalette;
177 /***********************************************************************
178 * GetPaletteEntries16 (GDI.363)
180 UINT16 WINAPI GetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
181 UINT16 count, LPPALETTEENTRY entries )
183 return GetPaletteEntries32( hpalette, start, count, entries );
187 /***********************************************************************
188 * GetPaletteEntries32 [GDI32.209] Retrieves palette entries
190 * RETURNS
191 * Success: Number of entries from logical palette
192 * Failure: 0
194 UINT32 WINAPI GetPaletteEntries32(
195 HPALETTE32 hpalette, /* [in] Handle of logical palette */
196 UINT32 start, /* [in] First entry to receive */
197 UINT32 count, /* [in] Number of entries to receive */
198 LPPALETTEENTRY entries) /* [out] Address of array receiving entries */
200 PALETTEOBJ * palPtr;
201 INT32 numEntries;
203 TRACE(palette,"hpal = %04x, count=%i\n", hpalette, count );
205 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
206 if (!palPtr) return 0;
208 numEntries = palPtr->logpalette.palNumEntries;
209 if (start+count > numEntries) count = numEntries - start;
210 if (entries)
212 if (start >= numEntries)
214 GDI_HEAP_UNLOCK( hpalette );
215 return 0;
217 memcpy( entries, &palPtr->logpalette.palPalEntry[start],
218 count * sizeof(PALETTEENTRY) );
219 for( numEntries = 0; numEntries < count ; numEntries++ )
220 if (entries[numEntries].peFlags & 0xF0)
221 entries[numEntries].peFlags = 0;
222 GDI_HEAP_UNLOCK( hpalette );
225 return count;
229 /***********************************************************************
230 * SetPaletteEntries16 (GDI.364)
232 UINT16 WINAPI SetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
233 UINT16 count, LPPALETTEENTRY entries )
235 return SetPaletteEntries32( hpalette, start, count, entries );
239 /***********************************************************************
240 * SetPaletteEntries32 [GDI32.326] Sets color values for range in palette
242 * RETURNS
243 * Success: Number of entries that were set
244 * Failure: 0
246 UINT32 WINAPI SetPaletteEntries32(
247 HPALETTE32 hpalette, /* [in] Handle of logical palette */
248 UINT32 start, /* [in] Index of first entry to set */
249 UINT32 count, /* [in] Number of entries to set */
250 LPPALETTEENTRY entries) /* [in] Address of array of structures */
252 PALETTEOBJ * palPtr;
253 INT32 numEntries;
255 TRACE(palette,"hpal=%04x,start=%i,count=%i\n",hpalette,start,count );
257 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
258 if (!palPtr) return 0;
260 numEntries = palPtr->logpalette.palNumEntries;
261 if (start >= numEntries)
263 GDI_HEAP_UNLOCK( hpalette );
264 return 0;
266 if (start+count > numEntries) count = numEntries - start;
267 memcpy( &palPtr->logpalette.palPalEntry[start], entries,
268 count * sizeof(PALETTEENTRY) );
269 PALETTE_ValidateFlags(palPtr->logpalette.palPalEntry,
270 palPtr->logpalette.palNumEntries);
271 free(palPtr->mapping);
272 palPtr->mapping = NULL;
273 GDI_HEAP_UNLOCK( hpalette );
274 return count;
278 /***********************************************************************
279 * ResizePalette16 (GDI.368)
281 BOOL16 WINAPI ResizePalette16( HPALETTE16 hPal, UINT16 cEntries )
283 return ResizePalette32( hPal, cEntries );
287 /***********************************************************************
288 * ResizePalette32 [GDI32.289] Resizes logical palette
290 * RETURNS
291 * Success: TRUE
292 * Failure: FALSE
294 BOOL32 WINAPI ResizePalette32(
295 HPALETTE32 hPal, /* [in] Handle of logical palette */
296 UINT32 cEntries) /* [in] Number of entries in logical palette */
298 PALETTEOBJ * palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
299 UINT32 cPrevEnt, prevVer;
300 int prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
301 int* mapping = NULL;
303 TRACE(palette,"hpal = %04x, prev = %i, new = %i\n",
304 hPal, palPtr ? palPtr->logpalette.palNumEntries : -1,
305 cEntries );
306 if( !palPtr ) return FALSE;
307 cPrevEnt = palPtr->logpalette.palNumEntries;
308 prevVer = palPtr->logpalette.palVersion;
309 prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) +
310 sizeof(int*) + sizeof(GDIOBJHDR);
311 size += sizeof(int*) + sizeof(GDIOBJHDR);
312 mapping = palPtr->mapping;
314 GDI_HEAP_UNLOCK( hPal );
316 hPal = GDI_HEAP_REALLOC( hPal, size );
317 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
318 if( !palPtr ) return FALSE;
320 if( mapping )
321 palPtr->mapping = (int*) xrealloc( mapping, cEntries * sizeof(int) );
322 if( cEntries > cPrevEnt )
324 if( mapping )
325 memset(palPtr->mapping + cPrevEnt, 0, (cEntries - cPrevEnt)*sizeof(int));
326 memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
327 PALETTE_ValidateFlags((PALETTEENTRY*)((BYTE*)palPtr + prevsize),
328 cEntries - cPrevEnt );
330 palPtr->logpalette.palNumEntries = cEntries;
331 palPtr->logpalette.palVersion = prevVer;
332 GDI_HEAP_UNLOCK( hPal );
333 return TRUE;
337 /***********************************************************************
338 * AnimatePalette16 (GDI.367)
340 void WINAPI AnimatePalette16( HPALETTE16 hPal, UINT16 StartIndex,
341 UINT16 NumEntries, const PALETTEENTRY* PaletteColors)
343 AnimatePalette32( hPal, StartIndex, NumEntries, PaletteColors );
347 /***********************************************************************
348 * AnimatePalette32 [GDI32.6] Replaces entries in logical palette
350 * RETURNS
351 * Success: TRUE
352 * Failure: FALSE
354 * FIXME
355 * Should use existing mapping when animating a primary palette
357 BOOL32 WINAPI AnimatePalette32(
358 HPALETTE32 hPal, /* [in] Handle to logical palette */
359 UINT32 StartIndex, /* [in] First entry in palette */
360 UINT32 NumEntries, /* [in] Count of entries in palette */
361 const PALETTEENTRY* PaletteColors) /* [in] Pointer to first replacement */
363 TRACE(palette, "%04x (%i - %i)\n", hPal, StartIndex,StartIndex+NumEntries);
365 if( hPal != STOCK_DEFAULT_PALETTE )
367 PALETTEOBJ* palPtr = (PALETTEOBJ *)GDI_GetObjPtr(hPal, PALETTE_MAGIC);
368 if (!palPtr) return FALSE;
370 if( (StartIndex + NumEntries) <= palPtr->logpalette.palNumEntries )
372 UINT32 u;
373 for( u = 0; u < NumEntries; u++ )
374 palPtr->logpalette.palPalEntry[u + StartIndex] = PaletteColors[u];
375 COLOR_SetMapping(palPtr, StartIndex, NumEntries,
376 hPal != hPrimaryPalette );
377 GDI_HEAP_UNLOCK( hPal );
378 return TRUE;
381 return FALSE;
385 /***********************************************************************
386 * SetSystemPaletteUse16 (GDI.373)
388 UINT16 WINAPI SetSystemPaletteUse16( HDC16 hdc, UINT16 use )
390 return SetSystemPaletteUse32( hdc, use );
394 /***********************************************************************
395 * SetSystemPaletteUse32 [GDI32.335]
397 * RETURNS
398 * Success: Previous system palette
399 * Failure: SYSPAL_ERROR
401 UINT32 WINAPI SetSystemPaletteUse32(
402 HDC32 hdc, /* [in] Handle of device context */
403 UINT32 use) /* [in] Palette-usage flag */
405 UINT32 old = SystemPaletteUse;
406 FIXME(palette,"(%04x,%04x): stub\n", hdc, use );
407 SystemPaletteUse = use;
408 return old;
412 /***********************************************************************
413 * GetSystemPaletteUse16 (GDI.374)
415 UINT16 WINAPI GetSystemPaletteUse16( HDC16 hdc )
417 return SystemPaletteUse;
421 /***********************************************************************
422 * GetSystemPaletteUse32 [GDI32.223] Gets state of system palette
424 * RETURNS
425 * Current state of system palette
427 UINT32 WINAPI GetSystemPaletteUse32(
428 HDC32 hdc) /* [in] Handle of device context */
430 return SystemPaletteUse;
434 /***********************************************************************
435 * GetSystemPaletteEntries16 (GDI.375)
437 UINT16 WINAPI GetSystemPaletteEntries16( HDC16 hdc, UINT16 start, UINT16 count,
438 LPPALETTEENTRY entries )
440 return GetSystemPaletteEntries32( hdc, start, count, entries );
444 /***********************************************************************
445 * GetSystemPaletteEntries32 [GDI32.222] Gets range of palette entries
447 * RETURNS
448 * Success: Number of entries retrieved from palette
449 * Failure: 0
451 UINT32 WINAPI GetSystemPaletteEntries32(
452 HDC32 hdc, /* [in] Handle of device context */
453 UINT32 start, /* [in] Index of first entry to be retrieved */
454 UINT32 count, /* [in] Number of entries to be retrieved */
455 LPPALETTEENTRY entries) /* [out] Array receiving system-palette entries */
457 UINT32 i;
458 DC *dc;
460 TRACE(palette, "hdc=%04x,start=%i,count=%i\n", hdc,start,count);
462 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
463 if (!entries) return COLOR_GetSystemPaletteSize();
464 if (start >= dc->w.devCaps->sizePalette)
466 GDI_HEAP_UNLOCK( hdc );
467 return 0;
469 if (start+count >= dc->w.devCaps->sizePalette)
470 count = dc->w.devCaps->sizePalette - start;
471 for (i = 0; i < count; i++)
473 *(COLORREF*)(entries + i) = COLOR_GetSystemPaletteEntry( start + i );
475 TRACE(palette,"\tidx(%02x) -> RGB(%08lx)\n",
476 start + i, *(COLORREF*)(entries + i) );
478 GDI_HEAP_UNLOCK( hdc );
479 return count;
483 /***********************************************************************
484 * GetNearestPaletteIndex16 (GDI.370)
486 UINT16 WINAPI GetNearestPaletteIndex16( HPALETTE16 hpalette, COLORREF color )
488 return GetNearestPaletteIndex32( hpalette, color );
492 /***********************************************************************
493 * GetNearestPaletteIndex32 [GDI32.203] Gets palette index for color
495 * NOTES
496 * Should index be initialized to CLR_INVALID instead of 0?
498 * RETURNS
499 * Success: Index of entry in logical palette
500 * Failure: CLR_INVALID
502 UINT32 WINAPI GetNearestPaletteIndex32(
503 HPALETTE32 hpalette, /* [in] Handle of logical color palette */
504 COLORREF color) /* [in] Color to be matched */
506 PALETTEOBJ* palObj = (PALETTEOBJ*)GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
507 UINT32 index = 0;
509 if( palObj )
510 index = COLOR_PaletteLookupPixel( palObj->logpalette.palPalEntry,
511 palObj->logpalette.palNumEntries,
512 NULL, color, FALSE );
514 TRACE(palette,"(%04x,%06lx): returning %d\n", hpalette, color, index );
515 GDI_HEAP_UNLOCK( hpalette );
516 return index;
520 /***********************************************************************
521 * GetNearestColor16 (GDI.154)
523 COLORREF WINAPI GetNearestColor16( HDC16 hdc, COLORREF color )
525 return GetNearestColor32( hdc, color );
529 /***********************************************************************
530 * GetNearestColor32 [GDI32.202] Gets a system color to match
532 * NOTES
533 * Should this return CLR_INVALID instead of FadeCafe?
535 * RETURNS
536 * Success: Color from system palette that corresponds to given color
537 * Failure: CLR_INVALID
539 COLORREF WINAPI GetNearestColor32(
540 HDC32 hdc, /* [in] Handle of device context */
541 COLORREF color) /* [in] Color to be matched */
543 COLORREF nearest = 0xFADECAFE;
544 DC *dc;
545 PALETTEOBJ *palObj;
547 if ( (dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC )) )
549 palObj = (PALETTEOBJ*)
550 GDI_GetObjPtr( (dc->w.hPalette)? dc->w.hPalette
551 : STOCK_DEFAULT_PALETTE, PALETTE_MAGIC );
552 if (!palObj) return nearest;
554 nearest = COLOR_LookupNearestColor( palObj->logpalette.palPalEntry,
555 palObj->logpalette.palNumEntries, color );
556 GDI_HEAP_UNLOCK( dc->w.hPalette );
559 TRACE(palette,"(%06lx): returning %06lx\n", color, nearest );
560 GDI_HEAP_UNLOCK( hdc );
561 return nearest;
565 /***********************************************************************
566 * PALETTE_GetObject
568 int PALETTE_GetObject( PALETTEOBJ * palette, int count, LPSTR buffer )
570 if (count > sizeof(WORD)) count = sizeof(WORD);
571 memcpy( buffer, &palette->logpalette.palNumEntries, count );
572 return count;
576 /***********************************************************************
577 * PALETTE_UnrealizeObject
579 BOOL32 PALETTE_UnrealizeObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
581 if (palette->mapping)
583 free( palette->mapping );
584 palette->mapping = NULL;
586 if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
587 return TRUE;
591 /***********************************************************************
592 * PALETTE_DeleteObject
594 BOOL32 PALETTE_DeleteObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
596 free( palette->mapping );
597 if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
598 return GDI_FreeObject( hpalette );
602 /***********************************************************************
603 * GDISelectPalette (GDI.361)
605 HPALETTE16 WINAPI GDISelectPalette( HDC16 hdc, HPALETTE16 hpal, WORD wBkg)
607 HPALETTE16 prev;
608 DC *dc;
610 TRACE(palette, "%04x %04x\n", hdc, hpal );
612 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
613 if (!dc)
615 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
616 if (!dc) return 0;
618 prev = dc->w.hPalette;
619 dc->w.hPalette = hpal;
620 GDI_HEAP_UNLOCK( hdc );
621 if (!wBkg) hPrimaryPalette = hpal;
622 return prev;
626 /***********************************************************************
627 * GDIRealizePalette (GDI.362)
629 UINT16 WINAPI GDIRealizePalette( HDC16 hdc )
631 PALETTEOBJ* palPtr;
632 int realized = 0;
633 DC* dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
634 if (!dc)
636 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
637 if (!dc) return 0;
640 TRACE(palette, "%04x...\n", hdc );
642 if( dc && dc->w.hPalette != hLastRealizedPalette )
644 if( dc->w.hPalette == STOCK_DEFAULT_PALETTE )
645 return RealizeDefaultPalette( hdc );
647 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC );
649 if (!palPtr) {
650 FIXME(palette,"invalid selected palette %04x\n",dc->w.hPalette);
651 return 0;
654 realized = COLOR_SetMapping(palPtr,0,palPtr->logpalette.palNumEntries,
655 (dc->w.hPalette != hPrimaryPalette) ||
656 (dc->w.hPalette == STOCK_DEFAULT_PALETTE));
657 GDI_HEAP_UNLOCK( dc->w.hPalette );
658 hLastRealizedPalette = dc->w.hPalette;
660 else TRACE(palette, " skipping (hLastRealizedPalette = %04x)\n",
661 hLastRealizedPalette);
662 GDI_HEAP_UNLOCK( hdc );
664 TRACE(palette, " realized %i colors.\n", realized );
665 return (UINT16)realized;
669 /***********************************************************************
670 * RealizeDefaultPalette (GDI.365)
672 UINT16 WINAPI RealizeDefaultPalette( HDC16 hdc )
674 DC *dc;
675 PALETTEOBJ* palPtr;
676 int i, index, realized = 0;
678 TRACE(palette,"%04x\n", hdc );
680 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
681 if (!dc)
683 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
684 if (!dc) return 0;
687 if ( dc->w.flags & DC_MEMORY )
689 GDI_HEAP_UNLOCK( hdc );
690 return 0;
693 hPrimaryPalette = STOCK_DEFAULT_PALETTE;
694 hLastRealizedPalette = STOCK_DEFAULT_PALETTE;
696 palPtr = (PALETTEOBJ*)GDI_GetObjPtr(STOCK_DEFAULT_PALETTE, PALETTE_MAGIC );
697 if (!palPtr) return 0;
699 /* lookup is needed to account for SetSystemPaletteUse() stuff */
701 for( i = 0; i < 20; i++ )
703 index = COLOR_LookupSystemPixel(*(COLORREF*)(palPtr->logpalette.palPalEntry + i));
705 /* mapping is allocated in COLOR_InitPalette() */
707 if( index != palPtr->mapping[i] ) { palPtr->mapping[i]=index; realized++; }
709 return realized;
712 /***********************************************************************
713 * IsDCCurrentPalette (GDI.412)
715 BOOL16 WINAPI IsDCCurrentPalette(HDC16 hDC)
717 DC* dc = (DC *)GDI_GetObjPtr( hDC, DC_MAGIC );
718 if (dc)
720 GDI_HEAP_UNLOCK( hDC );
721 return dc->w.hPalette == hPrimaryPalette;
723 return FALSE;
727 /***********************************************************************
728 * SelectPalette16 (USER.282)
730 HPALETTE16 WINAPI SelectPalette16( HDC16 hDC, HPALETTE16 hPal,
731 BOOL16 bForceBackground )
733 return SelectPalette32( hDC, hPal, bForceBackground );
737 /***********************************************************************
738 * SelectPalette32 [GDI32.300] Selects logical palette into DC
740 * RETURNS
741 * Success: Previous logical palette
742 * Failure: NULL
744 HPALETTE32 WINAPI SelectPalette32(
745 HDC32 hDC, /* [in] Handle of device context */
746 HPALETTE32 hPal, /* [in] Handle of logical color palette */
747 BOOL32 bForceBackground) /* [in] Foreground/background mode */
749 WORD wBkgPalette = 1;
750 PALETTEOBJ* lpt = (PALETTEOBJ*) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
752 TRACE(palette,"dc=%04x,pal=%04x,force=%i\n", hDC, hPal, bForceBackground);
753 if( !lpt ) return 0;
755 TRACE(palette," entries = %d\n", lpt->logpalette.palNumEntries);
756 GDI_HEAP_UNLOCK( hPal );
758 if( hPal != STOCK_DEFAULT_PALETTE )
760 HWND32 hWnd = WindowFromDC32( hDC );
761 HWND32 hActive = GetActiveWindow32();
763 /* set primary palette if it's related to current active */
765 if((!hWnd || (hActive == hWnd || IsChild16(hActive,hWnd))) &&
766 !bForceBackground )
767 wBkgPalette = 0;
769 return GDISelectPalette( hDC, hPal, wBkgPalette);
773 /***********************************************************************
774 * RealizePalette16 (USER.283)
776 UINT16 WINAPI RealizePalette16( HDC16 hDC )
778 return RealizePalette32( hDC );
782 /***********************************************************************
783 * RealizePalette32 [GDI32.280] Maps palette entries to system palette
785 * RETURNS
786 * Success: Number of entries in logical palette
787 * Failure: GDI_ERROR
789 UINT32 WINAPI RealizePalette32(
790 HDC32 hDC) /* [in] Handle of device context */
792 UINT32 realized = GDIRealizePalette( hDC );
794 /* do not send anything if no colors were changed */
796 if( IsDCCurrentPalette( hDC ) && realized &&
797 !(COLOR_GetSystemPaletteFlags() & COLOR_VIRTUAL) )
799 /* Send palette change notification */
801 HWND32 hWnd;
802 if( (hWnd = WindowFromDC32( hDC )) )
803 SendMessage16( HWND_BROADCAST, WM_PALETTECHANGED, hWnd, 0L);
805 return realized;
809 /**********************************************************************
810 * UpdateColors16 (GDI.366)
812 INT16 WINAPI UpdateColors16( HDC16 hDC )
814 HWND32 hWnd = WindowFromDC32( hDC );
816 /* Docs say that we have to remap current drawable pixel by pixel
817 * but it would take forever given the speed of XGet/PutPixel.
819 if (hWnd && !(COLOR_GetSystemPaletteFlags() & COLOR_VIRTUAL) )
820 InvalidateRect32( hWnd, NULL, FALSE );
821 return 0x666;
825 /**********************************************************************
826 * UpdateColors32 [GDI32.359] Remaps current colors to logical palette
828 * RETURNS
829 * Success: TRUE
830 * Failure: FALSE
832 BOOL32 WINAPI UpdateColors32(
833 HDC32 hDC) /* [in] Handle of device context */
835 UpdateColors16( hDC );
836 return TRUE;
840 /*********************************************************************
841 * SetMagicColors16 (GDI.606)
843 VOID WINAPI SetMagicColors16(HDC16 hDC, COLORREF color, UINT16 index)
845 FIXME(palette,"(hDC %04x, color %04x, index %04x): stub\n", hDC, (int)color, index);