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.
21 FARPROC32 pfnSelectPalette = NULL;
22 FARPROC32 pfnRealizePalette = NULL;
24 static UINT32 SystemPaletteUse = SYSPAL_STATIC; /* currently not considered */
26 static HPALETTE16 hPrimaryPalette = 0; /* used for WM_PALETTECHANGED */
27 static HPALETTE16 hLastRealizedPalette = 0; /* UnrealizeObject() needs it */
30 /***********************************************************************
33 * Create the system palette.
35 HPALETTE16 PALETTE_Init(void)
41 const PALETTEENTRY* __sysPalTemplate = COLOR_GetSystemPaletteTemplate();
43 /* create default palette (20 system colors) */
45 palPtr = HeapAlloc( GetProcessHeap(), 0,
46 sizeof(LOGPALETTE) + (NB_RESERVED_COLORS-1)*sizeof(PALETTEENTRY));
47 if (!palPtr) return FALSE;
49 palPtr->palVersion = 0x300;
50 palPtr->palNumEntries = NB_RESERVED_COLORS;
51 for( i = 0; i < NB_RESERVED_COLORS; i ++ )
53 palPtr->palPalEntry[i].peRed = __sysPalTemplate[i].peRed;
54 palPtr->palPalEntry[i].peGreen = __sysPalTemplate[i].peGreen;
55 palPtr->palPalEntry[i].peBlue = __sysPalTemplate[i].peBlue;
56 palPtr->palPalEntry[i].peFlags = 0;
58 hpalette = CreatePalette16( palPtr );
60 palObj = (PALETTEOBJ*) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
62 palObj->mapping = xmalloc( sizeof(int) * 20 );
64 GDI_HEAP_UNLOCK( hpalette );
66 HeapFree( GetProcessHeap(), 0, palPtr );
70 /***********************************************************************
71 * PALETTE_ValidateFlags
73 void PALETTE_ValidateFlags(PALETTEENTRY* lpPalE, int size)
77 lpPalE[i].peFlags = PC_SYS_USED | (lpPalE[i].peFlags & 0x07);
81 /***********************************************************************
82 * CreatePalette16 (GDI.360)
84 HPALETTE16 WINAPI CreatePalette16( const LOGPALETTE* palette )
86 return CreatePalette32( palette );
90 /***********************************************************************
91 * CreatePalette32 [GDI32.53] Creates a logical color palette
94 * Success: Handle to logical palette
97 HPALETTE32 WINAPI CreatePalette32(
98 const LOGPALETTE* palette) /* [in] Pointer to logical color palette */
100 PALETTEOBJ * palettePtr;
104 if (!palette) return 0;
105 TRACE(palette,"entries=%i\n", palette->palNumEntries);
107 size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
109 hpalette = GDI_AllocObject( size + sizeof(int*) +sizeof(GDIOBJHDR) , PALETTE_MAGIC );
110 if (!hpalette) return 0;
112 palettePtr = (PALETTEOBJ *) GDI_HEAP_LOCK( hpalette );
113 memcpy( &palettePtr->logpalette, palette, size );
114 PALETTE_ValidateFlags(palettePtr->logpalette.palPalEntry,
115 palettePtr->logpalette.palNumEntries);
116 palettePtr->mapping = NULL;
117 GDI_HEAP_UNLOCK( hpalette );
119 TRACE(palette," returning %04x\n", hpalette);
124 /***********************************************************************
125 * CreateHalftonePalette [GDI32.47] Creates a halftone palette
128 * Success: Handle to logical halftone palette
131 HPALETTE32 WINAPI CreateHalftonePalette(
132 HDC32 hdc) /* [in] Handle to device context */
134 FIXME(palette,"(0x%x): stub\n", hdc);
135 return (HPALETTE32)NULL;
139 /***********************************************************************
140 * GetPaletteEntries16 (GDI.363)
142 UINT16 WINAPI GetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
143 UINT16 count, LPPALETTEENTRY entries )
145 return GetPaletteEntries32( hpalette, start, count, entries );
149 /***********************************************************************
150 * GetPaletteEntries32 [GDI32.209] Retrieves palette entries
153 * Success: Number of entries from logical palette
156 UINT32 WINAPI GetPaletteEntries32(
157 HPALETTE32 hpalette, /* [in] Handle of logical palette */
158 UINT32 start, /* [in] First entry to receive */
159 UINT32 count, /* [in] Number of entries to receive */
160 LPPALETTEENTRY entries) /* [out] Address of array receiving entries */
165 TRACE(palette,"hpal = %04x, count=%i\n", hpalette, count );
167 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
168 if (!palPtr) return 0;
170 numEntries = palPtr->logpalette.palNumEntries;
171 if (start+count > numEntries) count = numEntries - start;
174 if (start >= numEntries)
176 GDI_HEAP_UNLOCK( hpalette );
179 memcpy( entries, &palPtr->logpalette.palPalEntry[start],
180 count * sizeof(PALETTEENTRY) );
181 for( numEntries = 0; numEntries < count ; numEntries++ )
182 if (entries[numEntries].peFlags & 0xF0)
183 entries[numEntries].peFlags = 0;
184 GDI_HEAP_UNLOCK( hpalette );
191 /***********************************************************************
192 * SetPaletteEntries16 (GDI.364)
194 UINT16 WINAPI SetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
195 UINT16 count, LPPALETTEENTRY entries )
197 return SetPaletteEntries32( hpalette, start, count, entries );
201 /***********************************************************************
202 * SetPaletteEntries32 [GDI32.326] Sets color values for range in palette
205 * Success: Number of entries that were set
208 UINT32 WINAPI SetPaletteEntries32(
209 HPALETTE32 hpalette, /* [in] Handle of logical palette */
210 UINT32 start, /* [in] Index of first entry to set */
211 UINT32 count, /* [in] Number of entries to set */
212 LPPALETTEENTRY entries) /* [in] Address of array of structures */
217 TRACE(palette,"hpal=%04x,start=%i,count=%i\n",hpalette,start,count );
219 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
220 if (!palPtr) return 0;
222 numEntries = palPtr->logpalette.palNumEntries;
223 if (start >= numEntries)
225 GDI_HEAP_UNLOCK( hpalette );
228 if (start+count > numEntries) count = numEntries - start;
229 memcpy( &palPtr->logpalette.palPalEntry[start], entries,
230 count * sizeof(PALETTEENTRY) );
231 PALETTE_ValidateFlags(palPtr->logpalette.palPalEntry,
232 palPtr->logpalette.palNumEntries);
233 free(palPtr->mapping);
234 palPtr->mapping = NULL;
235 GDI_HEAP_UNLOCK( hpalette );
240 /***********************************************************************
241 * ResizePalette16 (GDI.368)
243 BOOL16 WINAPI ResizePalette16( HPALETTE16 hPal, UINT16 cEntries )
245 return ResizePalette32( hPal, cEntries );
249 /***********************************************************************
250 * ResizePalette32 [GDI32.289] Resizes logical palette
256 BOOL32 WINAPI ResizePalette32(
257 HPALETTE32 hPal, /* [in] Handle of logical palette */
258 UINT32 cEntries) /* [in] Number of entries in logical palette */
260 PALETTEOBJ * palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
261 UINT32 cPrevEnt, prevVer;
262 int prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
265 TRACE(palette,"hpal = %04x, prev = %i, new = %i\n",
266 hPal, palPtr ? palPtr->logpalette.palNumEntries : -1,
268 if( !palPtr ) return FALSE;
269 cPrevEnt = palPtr->logpalette.palNumEntries;
270 prevVer = palPtr->logpalette.palVersion;
271 prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) +
272 sizeof(int*) + sizeof(GDIOBJHDR);
273 size += sizeof(int*) + sizeof(GDIOBJHDR);
274 mapping = palPtr->mapping;
276 GDI_HEAP_UNLOCK( hPal );
278 hPal = GDI_HEAP_REALLOC( hPal, size );
279 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
280 if( !palPtr ) return FALSE;
283 palPtr->mapping = (int*) xrealloc( mapping, cEntries * sizeof(int) );
284 if( cEntries > cPrevEnt )
287 memset(palPtr->mapping + cPrevEnt, 0, (cEntries - cPrevEnt)*sizeof(int));
288 memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
289 PALETTE_ValidateFlags((PALETTEENTRY*)((BYTE*)palPtr + prevsize),
290 cEntries - cPrevEnt );
292 palPtr->logpalette.palNumEntries = cEntries;
293 palPtr->logpalette.palVersion = prevVer;
294 GDI_HEAP_UNLOCK( hPal );
299 /***********************************************************************
300 * AnimatePalette16 (GDI.367)
302 void WINAPI AnimatePalette16( HPALETTE16 hPal, UINT16 StartIndex,
303 UINT16 NumEntries, const PALETTEENTRY* PaletteColors)
305 AnimatePalette32( hPal, StartIndex, NumEntries, PaletteColors );
309 /***********************************************************************
310 * AnimatePalette32 [GDI32.6] Replaces entries in logical palette
317 * Should use existing mapping when animating a primary palette
319 BOOL32 WINAPI AnimatePalette32(
320 HPALETTE32 hPal, /* [in] Handle to logical palette */
321 UINT32 StartIndex, /* [in] First entry in palette */
322 UINT32 NumEntries, /* [in] Count of entries in palette */
323 const PALETTEENTRY* PaletteColors) /* [in] Pointer to first replacement */
325 TRACE(palette, "%04x (%i - %i)\n", hPal, StartIndex,StartIndex+NumEntries);
327 if( hPal != STOCK_DEFAULT_PALETTE )
329 PALETTEOBJ* palPtr = (PALETTEOBJ *)GDI_GetObjPtr(hPal, PALETTE_MAGIC);
331 if( (StartIndex + NumEntries) <= palPtr->logpalette.palNumEntries )
334 for( u = 0; u < NumEntries; u++ )
335 palPtr->logpalette.palPalEntry[u + StartIndex] = PaletteColors[u];
336 COLOR_SetMapping(palPtr, StartIndex, NumEntries,
337 hPal != hPrimaryPalette );
338 GDI_HEAP_UNLOCK( hPal );
346 /***********************************************************************
347 * SetSystemPaletteUse16 (GDI.373)
349 UINT16 WINAPI SetSystemPaletteUse16( HDC16 hdc, UINT16 use )
351 return SetSystemPaletteUse32( hdc, use );
355 /***********************************************************************
356 * SetSystemPaletteUse32 [GDI32.335]
359 * Success: Previous system palette
360 * Failure: SYSPAL_ERROR
362 UINT32 WINAPI SetSystemPaletteUse32(
363 HDC32 hdc, /* [in] Handle of device context */
364 UINT32 use) /* [in] Palette-usage flag */
366 UINT32 old = SystemPaletteUse;
367 FIXME(palette,"(%04x,%04x): stub\n", hdc, use );
368 SystemPaletteUse = use;
373 /***********************************************************************
374 * GetSystemPaletteUse16 (GDI.374)
376 UINT16 WINAPI GetSystemPaletteUse16( HDC16 hdc )
378 return SystemPaletteUse;
382 /***********************************************************************
383 * GetSystemPaletteUse32 [GDI32.223] Gets state of system palette
386 * Current state of system palette
388 UINT32 WINAPI GetSystemPaletteUse32(
389 HDC32 hdc) /* [in] Handle of device context */
391 return SystemPaletteUse;
395 /***********************************************************************
396 * GetSystemPaletteEntries16 (GDI.375)
398 UINT16 WINAPI GetSystemPaletteEntries16( HDC16 hdc, UINT16 start, UINT16 count,
399 LPPALETTEENTRY entries )
401 return GetSystemPaletteEntries32( hdc, start, count, entries );
405 /***********************************************************************
406 * GetSystemPaletteEntries32 [GDI32.222] Gets range of palette entries
409 * Success: Number of entries retrieved from palette
412 UINT32 WINAPI GetSystemPaletteEntries32(
413 HDC32 hdc, /* [in] Handle of device context */
414 UINT32 start, /* [in] Index of first entry to be retrieved */
415 UINT32 count, /* [in] Number of entries to be retrieved */
416 LPPALETTEENTRY entries) /* [out] Array receiving system-palette entries */
421 TRACE(palette, "hdc=%04x,start=%i,count=%i\n", hdc,start,count);
423 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
424 if (!entries) return COLOR_GetSystemPaletteSize();
425 if (start >= dc->w.devCaps->sizePalette)
427 GDI_HEAP_UNLOCK( hdc );
430 if (start+count >= dc->w.devCaps->sizePalette)
431 count = dc->w.devCaps->sizePalette - start;
432 for (i = 0; i < count; i++)
434 *(COLORREF*)(entries + i) = COLOR_GetSystemPaletteEntry( start + i );
436 TRACE(palette,"\tidx(%02x) -> RGB(%08lx)\n",
437 start + i, *(COLORREF*)(entries + i) );
439 GDI_HEAP_UNLOCK( hdc );
444 /***********************************************************************
445 * GetNearestPaletteIndex16 (GDI.370)
447 UINT16 WINAPI GetNearestPaletteIndex16( HPALETTE16 hpalette, COLORREF color )
449 return GetNearestPaletteIndex32( hpalette, color );
453 /***********************************************************************
454 * GetNearestPaletteIndex32 [GDI32.203] Gets palette index for color
457 * Should index be initialized to CLR_INVALID instead of 0?
460 * Success: Index of entry in logical palette
461 * Failure: CLR_INVALID
463 UINT32 WINAPI GetNearestPaletteIndex32(
464 HPALETTE32 hpalette, /* [in] Handle of logical color palette */
465 COLORREF color) /* [in] Color to be matched */
467 PALETTEOBJ* palObj = (PALETTEOBJ*)GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
471 index = COLOR_PaletteLookupPixel( palObj->logpalette.palPalEntry,
472 palObj->logpalette.palNumEntries,
473 NULL, color, FALSE );
475 TRACE(palette,"(%04x,%06lx): returning %d\n", hpalette, color, index );
476 GDI_HEAP_UNLOCK( hpalette );
481 /***********************************************************************
482 * GetNearestColor16 (GDI.154)
484 COLORREF WINAPI GetNearestColor16( HDC16 hdc, COLORREF color )
486 return GetNearestColor32( hdc, color );
490 /***********************************************************************
491 * GetNearestColor32 [GDI32.202] Gets a system color to match
494 * Should this return CLR_INVALID instead of FadeCafe?
497 * Success: Color from system palette that corresponds to given color
498 * Failure: CLR_INVALID
500 COLORREF WINAPI GetNearestColor32(
501 HDC32 hdc, /* [in] Handle of device context */
502 COLORREF color) /* [in] Color to be matched */
504 COLORREF nearest = 0xFADECAFE;
508 if ( (dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC )) )
510 palObj = (PALETTEOBJ*)
511 GDI_GetObjPtr( (dc->w.hPalette)? dc->w.hPalette
512 : STOCK_DEFAULT_PALETTE, PALETTE_MAGIC );
514 nearest = COLOR_LookupNearestColor( palObj->logpalette.palPalEntry,
515 palObj->logpalette.palNumEntries, color );
516 GDI_HEAP_UNLOCK( dc->w.hPalette );
519 TRACE(palette,"(%06lx): returning %06lx\n", color, nearest );
520 GDI_HEAP_UNLOCK( hdc );
525 /***********************************************************************
528 int PALETTE_GetObject( PALETTEOBJ * palette, int count, LPSTR buffer )
530 if (count > sizeof(WORD)) count = sizeof(WORD);
531 memcpy( buffer, &palette->logpalette.palNumEntries, count );
536 /***********************************************************************
537 * PALETTE_UnrealizeObject
539 BOOL32 PALETTE_UnrealizeObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
541 if (palette->mapping)
543 free( palette->mapping );
544 palette->mapping = NULL;
546 if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
551 /***********************************************************************
552 * PALETTE_DeleteObject
554 BOOL32 PALETTE_DeleteObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
556 free( palette->mapping );
557 if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
558 return GDI_FreeObject( hpalette );
562 /***********************************************************************
563 * GDISelectPalette (GDI.361)
565 HPALETTE16 WINAPI GDISelectPalette( HDC16 hdc, HPALETTE16 hpal, WORD wBkg)
570 TRACE(palette, "%04x %04x\n", hdc, hpal );
572 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
575 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
578 prev = dc->w.hPalette;
579 dc->w.hPalette = hpal;
580 GDI_HEAP_UNLOCK( hdc );
581 if (!wBkg) hPrimaryPalette = hpal;
586 /***********************************************************************
587 * GDIRealizePalette (GDI.362)
589 UINT16 WINAPI GDIRealizePalette( HDC16 hdc )
593 DC* dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
596 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
600 TRACE(palette, "%04x...\n", hdc );
602 if( dc && dc->w.hPalette != hLastRealizedPalette )
604 if( dc->w.hPalette == STOCK_DEFAULT_PALETTE )
605 return RealizeDefaultPalette( hdc );
607 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC );
610 FIXME(palette,"invalid selected palette %04x\n",dc->w.hPalette);
614 realized = COLOR_SetMapping(palPtr,0,palPtr->logpalette.palNumEntries,
615 (dc->w.hPalette != hPrimaryPalette) ||
616 (dc->w.hPalette == STOCK_DEFAULT_PALETTE));
617 GDI_HEAP_UNLOCK( dc->w.hPalette );
618 hLastRealizedPalette = dc->w.hPalette;
620 else TRACE(palette, " skipping (hLastRealizedPalette = %04x)\n",
621 hLastRealizedPalette);
622 GDI_HEAP_UNLOCK( hdc );
624 TRACE(palette, " realized %i colors.\n", realized );
625 return (UINT16)realized;
629 /***********************************************************************
630 * RealizeDefaultPalette (GDI.365)
632 UINT16 WINAPI RealizeDefaultPalette( HDC16 hdc )
636 int i, index, realized = 0;
638 TRACE(palette,"%04x\n", hdc );
640 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
643 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
647 if ( dc->w.flags & DC_MEMORY )
649 GDI_HEAP_UNLOCK( hdc );
653 hPrimaryPalette = STOCK_DEFAULT_PALETTE;
654 hLastRealizedPalette = STOCK_DEFAULT_PALETTE;
656 palPtr = (PALETTEOBJ*)GDI_GetObjPtr(STOCK_DEFAULT_PALETTE, PALETTE_MAGIC );
658 /* lookup is needed to account for SetSystemPaletteUse() stuff */
660 for( i = 0; i < 20; i++ )
662 index = COLOR_LookupSystemPixel(*(COLORREF*)(palPtr->logpalette.palPalEntry + i));
664 /* mapping is allocated in COLOR_InitPalette() */
666 if( index != palPtr->mapping[i] ) { palPtr->mapping[i]=index; realized++; }
671 /***********************************************************************
672 * IsDCCurrentPalette (GDI.412)
674 BOOL16 WINAPI IsDCCurrentPalette(HDC16 hDC)
676 DC* dc = (DC *)GDI_GetObjPtr( hDC, DC_MAGIC );
679 GDI_HEAP_UNLOCK( hDC );
680 return dc->w.hPalette == hPrimaryPalette;
686 /***********************************************************************
687 * SelectPalette16 (USER.282)
689 HPALETTE16 WINAPI SelectPalette16( HDC16 hDC, HPALETTE16 hPal,
690 BOOL16 bForceBackground )
692 return SelectPalette32( hDC, hPal, bForceBackground );
696 /***********************************************************************
697 * SelectPalette32 [GDI32.300] Selects logical palette into DC
700 * Success: Previous logical palette
703 HPALETTE32 WINAPI SelectPalette32(
704 HDC32 hDC, /* [in] Handle of device context */
705 HPALETTE32 hPal, /* [in] Handle of logical color palette */
706 BOOL32 bForceBackground) /* [in] Foreground/background mode */
708 WORD wBkgPalette = 1;
709 PALETTEOBJ* lpt = (PALETTEOBJ*) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
711 TRACE(palette,"dc=%04x,pal=%04x,force=%i\n", hDC, hPal, bForceBackground);
714 TRACE(palette," entries = %d\n", lpt->logpalette.palNumEntries);
715 GDI_HEAP_UNLOCK( hPal );
717 if( hPal != STOCK_DEFAULT_PALETTE )
719 HWND32 hWnd = WindowFromDC32( hDC );
720 HWND32 hActive = GetActiveWindow32();
722 /* set primary palette if it's related to current active */
724 if((!hWnd || (hActive == hWnd || IsChild16(hActive,hWnd))) &&
728 return GDISelectPalette( hDC, hPal, wBkgPalette);
732 /***********************************************************************
733 * RealizePalette16 (USER.283)
735 UINT16 WINAPI RealizePalette16( HDC16 hDC )
737 return RealizePalette32( hDC );
741 /***********************************************************************
742 * RealizePalette32 [GDI32.280] Maps palette entries to system palette
745 * Success: Number of entries in logical palette
748 UINT32 WINAPI RealizePalette32(
749 HDC32 hDC) /* [in] Handle of device context */
751 UINT32 realized = GDIRealizePalette( hDC );
753 /* do not send anything if no colors were changed */
755 if( IsDCCurrentPalette( hDC ) && realized &&
756 !(COLOR_GetSystemPaletteFlags() & COLOR_VIRTUAL) )
758 /* Send palette change notification */
761 if( (hWnd = WindowFromDC32( hDC )) )
762 SendMessage16( HWND_BROADCAST, WM_PALETTECHANGED, hWnd, 0L);
768 /**********************************************************************
769 * UpdateColors16 (GDI.366)
771 INT16 WINAPI UpdateColors16( HDC16 hDC )
773 HWND32 hWnd = WindowFromDC32( hDC );
775 /* Docs say that we have to remap current drawable pixel by pixel
776 * but it would take forever given the speed of XGet/PutPixel.
778 if (hWnd && !(COLOR_GetSystemPaletteFlags() & COLOR_VIRTUAL) )
779 InvalidateRect32( hWnd, NULL, FALSE );
784 /**********************************************************************
785 * UpdateColors32 [GDI32.359] Remaps current colors to logical palette
791 BOOL32 WINAPI UpdateColors32(
792 HDC32 hDC) /* [in] Handle of device context */
794 UpdateColors16( hDC );
799 /*********************************************************************
800 * SetMagicColors16 (GDI.606)
802 VOID WINAPI SetMagicColors16(HDC16 hDC, COLORREF color, UINT16 index)
804 FIXME(palette,"(hDC %04x, color %04x, index %04x): stub\n", hDC, (int)color, index);