Moved more things to the X11 driver.
[wine/multimedia.git] / objects / palette.c
blobe50038d75e8459161b4b5627477ee6abc5724f49
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 );
60 palObj->mapping = xmalloc( sizeof(int) * 20 );
62 GDI_HEAP_UNLOCK( hpalette );
64 HeapFree( GetProcessHeap(), 0, palPtr );
65 return hpalette;
68 /***********************************************************************
69 * PALETTE_ValidateFlags
71 void PALETTE_ValidateFlags(PALETTEENTRY* lpPalE, int size)
73 int i = 0;
74 for( ; i<size ; i++ )
75 lpPalE[i].peFlags = PC_SYS_USED | (lpPalE[i].peFlags & 0x07);
79 /***********************************************************************
80 * CreatePalette16 (GDI.360)
82 HPALETTE16 WINAPI CreatePalette16( const LOGPALETTE* palette )
84 return CreatePalette32( palette );
88 /***********************************************************************
89 * CreatePalette32 [GDI32.53] Creates a logical color palette
91 * RETURNS
92 * Success: Handle to logical palette
93 * Failure: NULL
95 HPALETTE32 WINAPI CreatePalette32(
96 const LOGPALETTE* palette) /* [in] Pointer to logical color palette */
98 PALETTEOBJ * palettePtr;
99 HPALETTE32 hpalette;
100 int size;
102 if (!palette) return 0;
103 TRACE(palette,"entries=%i\n", palette->palNumEntries);
105 size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
107 hpalette = GDI_AllocObject( size + sizeof(int*) +sizeof(GDIOBJHDR) , PALETTE_MAGIC );
108 if (!hpalette) return 0;
110 palettePtr = (PALETTEOBJ *) GDI_HEAP_LOCK( hpalette );
111 memcpy( &palettePtr->logpalette, palette, size );
112 PALETTE_ValidateFlags(palettePtr->logpalette.palPalEntry,
113 palettePtr->logpalette.palNumEntries);
114 palettePtr->mapping = NULL;
115 GDI_HEAP_UNLOCK( hpalette );
117 TRACE(palette," returning %04x\n", hpalette);
118 return hpalette;
122 /***********************************************************************
123 * CreateHalftonePalette [GDI32.47] Creates a halftone palette
125 * RETURNS
126 * Success: Handle to logical halftone palette
127 * Failure: 0
129 HPALETTE32 WINAPI CreateHalftonePalette(
130 HDC32 hdc) /* [in] Handle to device context */
132 FIXME(palette,"(0x%x): stub\n", hdc);
133 return (HPALETTE32)NULL;
137 /***********************************************************************
138 * GetPaletteEntries16 (GDI.363)
140 UINT16 WINAPI GetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
141 UINT16 count, LPPALETTEENTRY entries )
143 return GetPaletteEntries32( hpalette, start, count, entries );
147 /***********************************************************************
148 * GetPaletteEntries32 [GDI32.209] Retrieves palette entries
150 * RETURNS
151 * Success: Number of entries from logical palette
152 * Failure: 0
154 UINT32 WINAPI GetPaletteEntries32(
155 HPALETTE32 hpalette, /* [in] Handle of logical palette */
156 UINT32 start, /* [in] First entry to receive */
157 UINT32 count, /* [in] Number of entries to receive */
158 LPPALETTEENTRY entries) /* [out] Address of array receiving entries */
160 PALETTEOBJ * palPtr;
161 INT32 numEntries;
163 TRACE(palette,"hpal = %04x, count=%i\n", hpalette, count );
165 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
166 if (!palPtr) return 0;
168 numEntries = palPtr->logpalette.palNumEntries;
169 if (start+count > numEntries) count = numEntries - start;
170 if (entries)
172 if (start >= numEntries)
174 GDI_HEAP_UNLOCK( hpalette );
175 return 0;
177 memcpy( entries, &palPtr->logpalette.palPalEntry[start],
178 count * sizeof(PALETTEENTRY) );
179 for( numEntries = 0; numEntries < count ; numEntries++ )
180 if (entries[numEntries].peFlags & 0xF0)
181 entries[numEntries].peFlags = 0;
182 GDI_HEAP_UNLOCK( hpalette );
185 return count;
189 /***********************************************************************
190 * SetPaletteEntries16 (GDI.364)
192 UINT16 WINAPI SetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
193 UINT16 count, LPPALETTEENTRY entries )
195 return SetPaletteEntries32( hpalette, start, count, entries );
199 /***********************************************************************
200 * SetPaletteEntries32 [GDI32.326] Sets color values for range in palette
202 * RETURNS
203 * Success: Number of entries that were set
204 * Failure: 0
206 UINT32 WINAPI SetPaletteEntries32(
207 HPALETTE32 hpalette, /* [in] Handle of logical palette */
208 UINT32 start, /* [in] Index of first entry to set */
209 UINT32 count, /* [in] Number of entries to set */
210 LPPALETTEENTRY entries) /* [in] Address of array of structures */
212 PALETTEOBJ * palPtr;
213 INT32 numEntries;
215 TRACE(palette,"hpal=%04x,start=%i,count=%i\n",hpalette,start,count );
217 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
218 if (!palPtr) return 0;
220 numEntries = palPtr->logpalette.palNumEntries;
221 if (start >= numEntries)
223 GDI_HEAP_UNLOCK( hpalette );
224 return 0;
226 if (start+count > numEntries) count = numEntries - start;
227 memcpy( &palPtr->logpalette.palPalEntry[start], entries,
228 count * sizeof(PALETTEENTRY) );
229 PALETTE_ValidateFlags(palPtr->logpalette.palPalEntry,
230 palPtr->logpalette.palNumEntries);
231 free(palPtr->mapping);
232 palPtr->mapping = NULL;
233 GDI_HEAP_UNLOCK( hpalette );
234 return count;
238 /***********************************************************************
239 * ResizePalette16 (GDI.368)
241 BOOL16 WINAPI ResizePalette16( HPALETTE16 hPal, UINT16 cEntries )
243 return ResizePalette32( hPal, cEntries );
247 /***********************************************************************
248 * ResizePalette32 [GDI32.289] Resizes logical palette
250 * RETURNS
251 * Success: TRUE
252 * Failure: FALSE
254 BOOL32 WINAPI ResizePalette32(
255 HPALETTE32 hPal, /* [in] Handle of logical palette */
256 UINT32 cEntries) /* [in] Number of entries in logical palette */
258 PALETTEOBJ * palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
259 UINT32 cPrevEnt, prevVer;
260 int prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
261 int* mapping = NULL;
263 TRACE(palette,"hpal = %04x, prev = %i, new = %i\n",
264 hPal, palPtr ? palPtr->logpalette.palNumEntries : -1,
265 cEntries );
266 if( !palPtr ) return FALSE;
267 cPrevEnt = palPtr->logpalette.palNumEntries;
268 prevVer = palPtr->logpalette.palVersion;
269 prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) +
270 sizeof(int*) + sizeof(GDIOBJHDR);
271 size += sizeof(int*) + sizeof(GDIOBJHDR);
272 mapping = palPtr->mapping;
274 GDI_HEAP_UNLOCK( hPal );
276 hPal = GDI_HEAP_REALLOC( hPal, size );
277 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
278 if( !palPtr ) return FALSE;
280 if( mapping )
281 palPtr->mapping = (int*) xrealloc( mapping, cEntries * sizeof(int) );
282 if( cEntries > cPrevEnt )
284 if( mapping )
285 memset(palPtr->mapping + cPrevEnt, 0, (cEntries - cPrevEnt)*sizeof(int));
286 memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
287 PALETTE_ValidateFlags((PALETTEENTRY*)((BYTE*)palPtr + prevsize),
288 cEntries - cPrevEnt );
290 palPtr->logpalette.palNumEntries = cEntries;
291 palPtr->logpalette.palVersion = prevVer;
292 GDI_HEAP_UNLOCK( hPal );
293 return TRUE;
297 /***********************************************************************
298 * AnimatePalette16 (GDI.367)
300 void WINAPI AnimatePalette16( HPALETTE16 hPal, UINT16 StartIndex,
301 UINT16 NumEntries, const PALETTEENTRY* PaletteColors)
303 AnimatePalette32( hPal, StartIndex, NumEntries, PaletteColors );
307 /***********************************************************************
308 * AnimatePalette32 [GDI32.6] Replaces entries in logical palette
310 * RETURNS
311 * Success: TRUE
312 * Failure: FALSE
314 * FIXME
315 * Should use existing mapping when animating a primary palette
317 BOOL32 WINAPI AnimatePalette32(
318 HPALETTE32 hPal, /* [in] Handle to logical palette */
319 UINT32 StartIndex, /* [in] First entry in palette */
320 UINT32 NumEntries, /* [in] Count of entries in palette */
321 const PALETTEENTRY* PaletteColors) /* [in] Pointer to first replacement */
323 TRACE(palette, "%04x (%i - %i)\n", hPal, StartIndex,StartIndex+NumEntries);
325 if( hPal != STOCK_DEFAULT_PALETTE )
327 PALETTEOBJ* palPtr = (PALETTEOBJ *)GDI_GetObjPtr(hPal, PALETTE_MAGIC);
329 if( (StartIndex + NumEntries) <= palPtr->logpalette.palNumEntries )
331 UINT32 u;
332 for( u = 0; u < NumEntries; u++ )
333 palPtr->logpalette.palPalEntry[u + StartIndex] = PaletteColors[u];
334 COLOR_SetMapping(palPtr, StartIndex, NumEntries,
335 hPal != hPrimaryPalette );
336 GDI_HEAP_UNLOCK( hPal );
337 return TRUE;
340 return FALSE;
344 /***********************************************************************
345 * SetSystemPaletteUse16 (GDI.373)
347 UINT16 WINAPI SetSystemPaletteUse16( HDC16 hdc, UINT16 use )
349 return SetSystemPaletteUse32( hdc, use );
353 /***********************************************************************
354 * SetSystemPaletteUse32 [GDI32.335]
356 * RETURNS
357 * Success: Previous system palette
358 * Failure: SYSPAL_ERROR
360 UINT32 WINAPI SetSystemPaletteUse32(
361 HDC32 hdc, /* [in] Handle of device context */
362 UINT32 use) /* [in] Palette-usage flag */
364 UINT32 old = SystemPaletteUse;
365 FIXME(palette,"(%04x,%04x): stub\n", hdc, use );
366 SystemPaletteUse = use;
367 return old;
371 /***********************************************************************
372 * GetSystemPaletteUse16 (GDI.374)
374 UINT16 WINAPI GetSystemPaletteUse16( HDC16 hdc )
376 return SystemPaletteUse;
380 /***********************************************************************
381 * GetSystemPaletteUse32 [GDI32.223] Gets state of system palette
383 * RETURNS
384 * Current state of system palette
386 UINT32 WINAPI GetSystemPaletteUse32(
387 HDC32 hdc) /* [in] Handle of device context */
389 return SystemPaletteUse;
393 /***********************************************************************
394 * GetSystemPaletteEntries16 (GDI.375)
396 UINT16 WINAPI GetSystemPaletteEntries16( HDC16 hdc, UINT16 start, UINT16 count,
397 LPPALETTEENTRY entries )
399 return GetSystemPaletteEntries32( hdc, start, count, entries );
403 /***********************************************************************
404 * GetSystemPaletteEntries32 [GDI32.222] Gets range of palette entries
406 * RETURNS
407 * Success: Number of entries retrieved from palette
408 * Failure: 0
410 UINT32 WINAPI GetSystemPaletteEntries32(
411 HDC32 hdc, /* [in] Handle of device context */
412 UINT32 start, /* [in] Index of first entry to be retrieved */
413 UINT32 count, /* [in] Number of entries to be retrieved */
414 LPPALETTEENTRY entries) /* [out] Array receiving system-palette entries */
416 UINT32 i;
417 DC *dc;
419 TRACE(palette, "hdc=%04x,start=%i,count=%i\n", hdc,start,count);
421 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
422 if (!entries) return COLOR_GetSystemPaletteSize();
423 if (start >= dc->w.devCaps->sizePalette)
425 GDI_HEAP_UNLOCK( hdc );
426 return 0;
428 if (start+count >= dc->w.devCaps->sizePalette)
429 count = dc->w.devCaps->sizePalette - start;
430 for (i = 0; i < count; i++)
432 *(COLORREF*)(entries + i) = COLOR_GetSystemPaletteEntry( start + i );
434 TRACE(palette,"\tidx(%02x) -> RGB(%08lx)\n",
435 start + i, *(COLORREF*)(entries + i) );
437 GDI_HEAP_UNLOCK( hdc );
438 return count;
442 /***********************************************************************
443 * GetNearestPaletteIndex16 (GDI.370)
445 UINT16 WINAPI GetNearestPaletteIndex16( HPALETTE16 hpalette, COLORREF color )
447 return GetNearestPaletteIndex32( hpalette, color );
451 /***********************************************************************
452 * GetNearestPaletteIndex32 [GDI32.203] Gets palette index for color
454 * NOTES
455 * Should index be initialized to CLR_INVALID instead of 0?
457 * RETURNS
458 * Success: Index of entry in logical palette
459 * Failure: CLR_INVALID
461 UINT32 WINAPI GetNearestPaletteIndex32(
462 HPALETTE32 hpalette, /* [in] Handle of logical color palette */
463 COLORREF color) /* [in] Color to be matched */
465 PALETTEOBJ* palObj = (PALETTEOBJ*)GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
466 UINT32 index = 0;
468 if( palObj )
469 index = COLOR_PaletteLookupPixel( palObj->logpalette.palPalEntry,
470 palObj->logpalette.palNumEntries,
471 NULL, color, FALSE );
473 TRACE(palette,"(%04x,%06lx): returning %d\n", hpalette, color, index );
474 GDI_HEAP_UNLOCK( hpalette );
475 return index;
479 /***********************************************************************
480 * GetNearestColor16 (GDI.154)
482 COLORREF WINAPI GetNearestColor16( HDC16 hdc, COLORREF color )
484 return GetNearestColor32( hdc, color );
488 /***********************************************************************
489 * GetNearestColor32 [GDI32.202] Gets a system color to match
491 * NOTES
492 * Should this return CLR_INVALID instead of FadeCafe?
494 * RETURNS
495 * Success: Color from system palette that corresponds to given color
496 * Failure: CLR_INVALID
498 COLORREF WINAPI GetNearestColor32(
499 HDC32 hdc, /* [in] Handle of device context */
500 COLORREF color) /* [in] Color to be matched */
502 COLORREF nearest = 0xFADECAFE;
503 DC *dc;
504 PALETTEOBJ *palObj;
506 if ( (dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC )) )
508 palObj = (PALETTEOBJ*)
509 GDI_GetObjPtr( (dc->w.hPalette)? dc->w.hPalette
510 : STOCK_DEFAULT_PALETTE, PALETTE_MAGIC );
512 nearest = COLOR_LookupNearestColor( palObj->logpalette.palPalEntry,
513 palObj->logpalette.palNumEntries, color );
514 GDI_HEAP_UNLOCK( dc->w.hPalette );
517 TRACE(palette,"(%06lx): returning %06lx\n", color, nearest );
518 GDI_HEAP_UNLOCK( hdc );
519 return nearest;
523 /***********************************************************************
524 * PALETTE_GetObject
526 int PALETTE_GetObject( PALETTEOBJ * palette, int count, LPSTR buffer )
528 if (count > sizeof(WORD)) count = sizeof(WORD);
529 memcpy( buffer, &palette->logpalette.palNumEntries, count );
530 return count;
534 /***********************************************************************
535 * PALETTE_UnrealizeObject
537 BOOL32 PALETTE_UnrealizeObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
539 if (palette->mapping)
541 free( palette->mapping );
542 palette->mapping = NULL;
544 if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
545 return TRUE;
549 /***********************************************************************
550 * PALETTE_DeleteObject
552 BOOL32 PALETTE_DeleteObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
554 free( palette->mapping );
555 if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
556 return GDI_FreeObject( hpalette );
560 /***********************************************************************
561 * GDISelectPalette (GDI.361)
563 HPALETTE16 WINAPI GDISelectPalette( HDC16 hdc, HPALETTE16 hpal, WORD wBkg)
565 HPALETTE16 prev;
566 DC *dc;
568 TRACE(palette, "%04x %04x\n", hdc, hpal );
570 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
571 if (!dc)
573 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
574 if (!dc) return 0;
576 prev = dc->w.hPalette;
577 dc->w.hPalette = hpal;
578 GDI_HEAP_UNLOCK( hdc );
579 if (!wBkg) hPrimaryPalette = hpal;
580 return prev;
584 /***********************************************************************
585 * GDIRealizePalette (GDI.362)
587 UINT16 WINAPI GDIRealizePalette( HDC16 hdc )
589 PALETTEOBJ* palPtr;
590 int realized = 0;
591 DC* dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
592 if (!dc)
594 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
595 if (!dc) return 0;
598 TRACE(palette, "%04x...\n", hdc );
600 if( dc && dc->w.hPalette != hLastRealizedPalette )
602 if( dc->w.hPalette == STOCK_DEFAULT_PALETTE )
603 return RealizeDefaultPalette( hdc );
605 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC );
607 if (!palPtr) {
608 FIXME(palette,"invalid selected palette %04x\n",dc->w.hPalette);
609 return 0;
612 realized = COLOR_SetMapping(palPtr,0,palPtr->logpalette.palNumEntries,
613 (dc->w.hPalette != hPrimaryPalette) ||
614 (dc->w.hPalette == STOCK_DEFAULT_PALETTE));
615 GDI_HEAP_UNLOCK( dc->w.hPalette );
616 hLastRealizedPalette = dc->w.hPalette;
618 else TRACE(palette, " skipping (hLastRealizedPalette = %04x)\n",
619 hLastRealizedPalette);
620 GDI_HEAP_UNLOCK( hdc );
622 TRACE(palette, " realized %i colors.\n", realized );
623 return (UINT16)realized;
627 /***********************************************************************
628 * RealizeDefaultPalette (GDI.365)
630 UINT16 WINAPI RealizeDefaultPalette( HDC16 hdc )
632 DC *dc;
633 PALETTEOBJ* palPtr;
634 int i, index, realized = 0;
636 TRACE(palette,"%04x\n", hdc );
638 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
639 if (!dc)
641 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
642 if (!dc) return 0;
645 if ( dc->w.flags & DC_MEMORY )
647 GDI_HEAP_UNLOCK( hdc );
648 return 0;
651 hPrimaryPalette = STOCK_DEFAULT_PALETTE;
652 hLastRealizedPalette = STOCK_DEFAULT_PALETTE;
654 palPtr = (PALETTEOBJ*)GDI_GetObjPtr(STOCK_DEFAULT_PALETTE, PALETTE_MAGIC );
656 /* lookup is needed to account for SetSystemPaletteUse() stuff */
658 for( i = 0; i < 20; i++ )
660 index = COLOR_LookupSystemPixel(*(COLORREF*)(palPtr->logpalette.palPalEntry + i));
662 /* mapping is allocated in COLOR_InitPalette() */
664 if( index != palPtr->mapping[i] ) { palPtr->mapping[i]=index; realized++; }
666 return realized;
669 /***********************************************************************
670 * IsDCCurrentPalette (GDI.412)
672 BOOL16 WINAPI IsDCCurrentPalette(HDC16 hDC)
674 DC* dc = (DC *)GDI_GetObjPtr( hDC, DC_MAGIC );
675 if (dc)
677 GDI_HEAP_UNLOCK( hDC );
678 return dc->w.hPalette == hPrimaryPalette;
680 return FALSE;
684 /***********************************************************************
685 * SelectPalette16 (USER.282)
687 HPALETTE16 WINAPI SelectPalette16( HDC16 hDC, HPALETTE16 hPal,
688 BOOL16 bForceBackground )
690 return SelectPalette32( hDC, hPal, bForceBackground );
694 /***********************************************************************
695 * SelectPalette32 [GDI32.300] Selects logical palette into DC
697 * RETURNS
698 * Success: Previous logical palette
699 * Failure: NULL
701 HPALETTE32 WINAPI SelectPalette32(
702 HDC32 hDC, /* [in] Handle of device context */
703 HPALETTE32 hPal, /* [in] Handle of logical color palette */
704 BOOL32 bForceBackground) /* [in] Foreground/background mode */
706 WORD wBkgPalette = 1;
707 PALETTEOBJ* lpt = (PALETTEOBJ*) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
709 TRACE(palette,"dc=%04x,pal=%04x,force=%i\n", hDC, hPal, bForceBackground);
710 if( !lpt ) return 0;
712 TRACE(palette," entries = %d\n", lpt->logpalette.palNumEntries);
713 GDI_HEAP_UNLOCK( hPal );
715 if( hPal != STOCK_DEFAULT_PALETTE )
717 HWND32 hWnd = WindowFromDC32( hDC );
718 HWND32 hActive = GetActiveWindow32();
720 /* set primary palette if it's related to current active */
722 if((!hWnd || (hActive == hWnd || IsChild16(hActive,hWnd))) &&
723 !bForceBackground )
724 wBkgPalette = 0;
726 return GDISelectPalette( hDC, hPal, wBkgPalette);
730 /***********************************************************************
731 * RealizePalette16 (USER.283)
733 UINT16 WINAPI RealizePalette16( HDC16 hDC )
735 return RealizePalette32( hDC );
739 /***********************************************************************
740 * RealizePalette32 [GDI32.280] Maps palette entries to system palette
742 * RETURNS
743 * Success: Number of entries in logical palette
744 * Failure: GDI_ERROR
746 UINT32 WINAPI RealizePalette32(
747 HDC32 hDC) /* [in] Handle of device context */
749 UINT32 realized = GDIRealizePalette( hDC );
751 /* do not send anything if no colors were changed */
753 if( IsDCCurrentPalette( hDC ) && realized &&
754 !(COLOR_GetSystemPaletteFlags() & COLOR_VIRTUAL) )
756 /* Send palette change notification */
758 HWND32 hWnd;
759 if( (hWnd = WindowFromDC32( hDC )) )
760 SendMessage16( HWND_BROADCAST, WM_PALETTECHANGED, hWnd, 0L);
762 return realized;
766 /**********************************************************************
767 * UpdateColors16 (GDI.366)
769 INT16 WINAPI UpdateColors16( HDC16 hDC )
771 HWND32 hWnd = WindowFromDC32( hDC );
773 /* Docs say that we have to remap current drawable pixel by pixel
774 * but it would take forever given the speed of XGet/PutPixel.
776 if (hWnd && !(COLOR_GetSystemPaletteFlags() & COLOR_VIRTUAL) )
777 InvalidateRect32( hWnd, NULL, FALSE );
778 return 0x666;
782 /**********************************************************************
783 * UpdateColors32 [GDI32.359] Remaps current colors to logical palette
785 * RETURNS
786 * Success: TRUE
787 * Failure: FALSE
789 BOOL32 WINAPI UpdateColors32(
790 HDC32 hDC) /* [in] Handle of device context */
792 UpdateColors16( hDC );
793 return TRUE;
797 /*********************************************************************
798 * SetMagicColors16 (GDI.606)
800 VOID WINAPI SetMagicColors16(HDC16 hDC, COLORREF color, UINT16 index)
802 FIXME(palette,"(hDC %04x, color %04x, index %04x): stub\n", hDC, (int)color, index);