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 static UINT32 SystemPaletteUse = SYSPAL_STATIC; /* currently not considered */
23 static HPALETTE16 hPrimaryPalette = 0; /* used for WM_PALETTECHANGED */
24 static HPALETTE16 hLastRealizedPalette = 0; /* UnrealizeObject() needs it */
27 /***********************************************************************
30 * Create the system palette.
32 HPALETTE16 PALETTE_Init(void)
38 const PALETTEENTRY* __sysPalTemplate = COLOR_GetSystemPaletteTemplate();
40 /* create default palette (20 system colors) */
42 palPtr = HeapAlloc( GetProcessHeap(), 0,
43 sizeof(LOGPALETTE) + (NB_RESERVED_COLORS-1)*sizeof(PALETTEENTRY));
44 if (!palPtr) return FALSE;
46 palPtr->palVersion = 0x300;
47 palPtr->palNumEntries = NB_RESERVED_COLORS;
48 for( i = 0; i < NB_RESERVED_COLORS; i ++ )
50 palPtr->palPalEntry[i].peRed = __sysPalTemplate[i].peRed;
51 palPtr->palPalEntry[i].peGreen = __sysPalTemplate[i].peGreen;
52 palPtr->palPalEntry[i].peBlue = __sysPalTemplate[i].peBlue;
53 palPtr->palPalEntry[i].peFlags = 0;
55 hpalette = CreatePalette16( palPtr );
57 palObj = (PALETTEOBJ*) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
59 palObj->mapping = xmalloc( sizeof(int) * 20 );
61 GDI_HEAP_UNLOCK( hpalette );
63 HeapFree( GetProcessHeap(), 0, palPtr );
67 /***********************************************************************
68 * PALETTE_ValidateFlags
70 void PALETTE_ValidateFlags(PALETTEENTRY* lpPalE, int size)
74 lpPalE[i].peFlags = PC_SYS_USED | (lpPalE[i].peFlags & 0x07);
78 /***********************************************************************
79 * CreatePalette16 (GDI.360)
81 HPALETTE16 WINAPI CreatePalette16( const LOGPALETTE* palette )
83 return CreatePalette32( palette );
87 /***********************************************************************
88 * CreatePalette32 (GDI32.53)
90 HPALETTE32 WINAPI CreatePalette32( const LOGPALETTE* palette )
92 PALETTEOBJ * palettePtr;
94 int size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
96 TRACE(palette,"%i entries \n",
97 palette->palNumEntries);
99 hpalette = GDI_AllocObject( size + sizeof(int*) +sizeof(GDIOBJHDR) , PALETTE_MAGIC );
100 if (!hpalette) return 0;
102 palettePtr = (PALETTEOBJ *) GDI_HEAP_LOCK( hpalette );
103 memcpy( &palettePtr->logpalette, palette, size );
104 PALETTE_ValidateFlags(palettePtr->logpalette.palPalEntry,
105 palettePtr->logpalette.palNumEntries);
106 palettePtr->mapping = NULL;
107 GDI_HEAP_UNLOCK( hpalette );
109 TRACE(palette," returning %04x\n", hpalette);
113 /***********************************************************************
114 * CreateHalftonePalette (GDI32.47)
116 HPALETTE32 WINAPI CreateHalftonePalette(HDC32 hdc)
118 FIXME(palette,"(%x): empty stub!\n", hdc);
122 /***********************************************************************
123 * GetPaletteEntries16 (GDI.363)
125 UINT16 WINAPI GetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
126 UINT16 count, LPPALETTEENTRY entries )
128 return GetPaletteEntries32( hpalette, start, count, entries );
132 /***********************************************************************
133 * GetPaletteEntries32 (GDI32.209)
135 UINT32 WINAPI GetPaletteEntries32( HPALETTE32 hpalette, UINT32 start,
136 UINT32 count, LPPALETTEENTRY entries )
141 TRACE(palette,"hpal = %04x, %i entries\n",
144 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
145 if (!palPtr) return 0;
147 numEntries = palPtr->logpalette.palNumEntries;
148 if (start >= numEntries)
150 GDI_HEAP_UNLOCK( hpalette );
153 if (start+count > numEntries) count = numEntries - start;
154 memcpy( entries, &palPtr->logpalette.palPalEntry[start],
155 count * sizeof(PALETTEENTRY) );
156 for( numEntries = 0; numEntries < count ; numEntries++ )
157 if (entries[numEntries].peFlags & 0xF0)
158 entries[numEntries].peFlags = 0;
159 GDI_HEAP_UNLOCK( hpalette );
164 /***********************************************************************
165 * SetPaletteEntries16 (GDI.364)
167 UINT16 WINAPI SetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
168 UINT16 count, LPPALETTEENTRY entries )
170 return SetPaletteEntries32( hpalette, start, count, entries );
174 /***********************************************************************
175 * SetPaletteEntries32 (GDI32.326)
177 UINT32 WINAPI SetPaletteEntries32( HPALETTE32 hpalette, UINT32 start,
178 UINT32 count, LPPALETTEENTRY entries )
183 TRACE(palette,"hpal = %04x, %i entries\n",
186 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
187 if (!palPtr) return 0;
189 numEntries = palPtr->logpalette.palNumEntries;
190 if (start >= numEntries)
192 GDI_HEAP_UNLOCK( hpalette );
195 if (start+count > numEntries) count = numEntries - start;
196 memcpy( &palPtr->logpalette.palPalEntry[start], entries,
197 count * sizeof(PALETTEENTRY) );
198 PALETTE_ValidateFlags(palPtr->logpalette.palPalEntry,
199 palPtr->logpalette.palNumEntries);
200 free(palPtr->mapping);
201 palPtr->mapping = NULL;
202 GDI_HEAP_UNLOCK( hpalette );
207 /***********************************************************************
208 * ResizePalette16 (GDI.368)
210 BOOL16 WINAPI ResizePalette16( HPALETTE16 hPal, UINT16 cEntries )
212 return ResizePalette32( hPal, cEntries );
216 /***********************************************************************
217 * ResizePalette32 (GDI32.289)
219 BOOL32 WINAPI ResizePalette32( HPALETTE32 hPal, UINT32 cEntries )
221 PALETTEOBJ * palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
222 UINT32 cPrevEnt, prevVer;
223 int prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
226 TRACE(palette,"hpal = %04x, prev = %i, new = %i\n",
227 hPal, palPtr ? palPtr->logpalette.palNumEntries : -1,
229 if( !palPtr ) return FALSE;
230 cPrevEnt = palPtr->logpalette.palNumEntries;
231 prevVer = palPtr->logpalette.palVersion;
232 prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) +
233 sizeof(int*) + sizeof(GDIOBJHDR);
234 size += sizeof(int*) + sizeof(GDIOBJHDR);
235 mapping = palPtr->mapping;
237 GDI_HEAP_UNLOCK( hPal );
239 hPal = GDI_HEAP_REALLOC( hPal, size );
240 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
241 if( !palPtr ) return FALSE;
244 palPtr->mapping = (int*) xrealloc( mapping, cEntries * sizeof(int) );
245 if( cEntries > cPrevEnt )
248 memset(palPtr->mapping + cPrevEnt, 0, (cEntries - cPrevEnt)*sizeof(int));
249 memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
250 PALETTE_ValidateFlags((PALETTEENTRY*)((BYTE*)palPtr + prevsize),
251 cEntries - cPrevEnt );
253 palPtr->logpalette.palNumEntries = cEntries;
254 palPtr->logpalette.palVersion = prevVer;
255 GDI_HEAP_UNLOCK( hPal );
260 /***********************************************************************
261 * AnimatePalette16 (GDI.367)
263 void WINAPI AnimatePalette16( HPALETTE16 hPal, UINT16 StartIndex,
264 UINT16 NumEntries, LPPALETTEENTRY PaletteColors)
266 AnimatePalette32( hPal, StartIndex, NumEntries, PaletteColors );
270 /***********************************************************************
271 * AnimatePalette32 (GDI32.6)
273 * FIXME: should use existing mapping when animating a primary palette
275 BOOL32 WINAPI AnimatePalette32( HPALETTE32 hPal, UINT32 StartIndex,
276 UINT32 NumEntries, LPPALETTEENTRY PaletteColors)
278 TRACE(palette, "%04x (%i - %i)\n", hPal,
279 StartIndex, StartIndex + NumEntries );
281 if( hPal != STOCK_DEFAULT_PALETTE )
283 PALETTEOBJ* palPtr = (PALETTEOBJ *)GDI_GetObjPtr(hPal, PALETTE_MAGIC);
285 if( (StartIndex + NumEntries) <= palPtr->logpalette.palNumEntries )
288 for( u = 0; u < NumEntries; u++ )
289 palPtr->logpalette.palPalEntry[u + StartIndex] = PaletteColors[u];
290 COLOR_SetMapping(palPtr, StartIndex, NumEntries,
291 hPal != hPrimaryPalette );
292 GDI_HEAP_UNLOCK( hPal );
300 /***********************************************************************
301 * SetSystemPaletteUse16 (GDI.373)
303 UINT16 WINAPI SetSystemPaletteUse16( HDC16 hdc, UINT16 use )
305 return SetSystemPaletteUse32( hdc, use );
309 /***********************************************************************
310 * SetSystemPaletteUse32 (GDI32.335)
312 UINT32 WINAPI SetSystemPaletteUse32( HDC32 hdc, UINT32 use )
314 UINT32 old = SystemPaletteUse;
315 FIXME(palette,"(%04x,%04x): empty stub !!!\n", hdc, use );
316 SystemPaletteUse = use;
321 /***********************************************************************
322 * GetSystemPaletteUse16 (GDI.374)
324 UINT16 WINAPI GetSystemPaletteUse16( HDC16 hdc )
326 return SystemPaletteUse;
330 /***********************************************************************
331 * GetSystemPaletteUse32 (GDI32.223)
333 UINT32 WINAPI GetSystemPaletteUse32( HDC32 hdc )
335 return SystemPaletteUse;
339 /***********************************************************************
340 * GetSystemPaletteEntries16 (GDI.375)
342 UINT16 WINAPI GetSystemPaletteEntries16( HDC16 hdc, UINT16 start, UINT16 count,
343 LPPALETTEENTRY entries )
345 return GetSystemPaletteEntries32( hdc, start, count, entries );
349 /***********************************************************************
350 * GetSystemPaletteEntries32 (GDI32.222)
352 UINT32 WINAPI GetSystemPaletteEntries32( HDC32 hdc, UINT32 start, UINT32 count,
353 LPPALETTEENTRY entries )
358 TRACE(palette, "hdc = %04x, "
359 "cound = %i\n", hdc, count );
361 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
362 if (start >= dc->w.devCaps->sizePalette)
364 GDI_HEAP_UNLOCK( hdc );
367 if (start+count >= dc->w.devCaps->sizePalette)
368 count = dc->w.devCaps->sizePalette - start;
369 for (i = 0; i < count; i++)
371 *(COLORREF*)(entries + i) = COLOR_GetSystemPaletteEntry( start + i );
373 TRACE(palette,"\tidx(%02x) -> RGB(%08lx)\n",
374 start + i, *(COLORREF*)(entries + i) );
376 GDI_HEAP_UNLOCK( hdc );
381 /***********************************************************************
382 * GetNearestPaletteIndex16 (GDI.370)
384 UINT16 WINAPI GetNearestPaletteIndex16( HPALETTE16 hpalette, COLORREF color )
386 return GetNearestPaletteIndex32( hpalette, color );
390 /***********************************************************************
391 * GetNearestPaletteIndex32 (GDI32.203)
393 UINT32 WINAPI GetNearestPaletteIndex32( HPALETTE32 hpalette, COLORREF color )
395 PALETTEOBJ* palObj = (PALETTEOBJ*)GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
399 index = COLOR_PaletteLookupPixel( palObj->logpalette.palPalEntry,
400 palObj->logpalette.palNumEntries,
401 NULL, color, FALSE );
403 TRACE(palette,"(%04x,%06lx): returning %d\n",
404 hpalette, color, index );
405 GDI_HEAP_UNLOCK( hpalette );
410 /***********************************************************************
411 * GetNearestColor16 (GDI.154)
413 COLORREF WINAPI GetNearestColor16( HDC16 hdc, COLORREF color )
415 return GetNearestColor32( hdc, color );
419 /***********************************************************************
420 * GetNearestColor32 (GDI32.202)
422 COLORREF WINAPI GetNearestColor32( HDC32 hdc, COLORREF color )
424 COLORREF nearest = 0xFADECAFE;
428 if ( (dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC )) )
430 palObj = (PALETTEOBJ*)
431 GDI_GetObjPtr( (dc->w.hPalette)? dc->w.hPalette
432 : STOCK_DEFAULT_PALETTE, PALETTE_MAGIC );
434 nearest = COLOR_LookupNearestColor( palObj->logpalette.palPalEntry,
435 palObj->logpalette.palNumEntries, color );
436 GDI_HEAP_UNLOCK( dc->w.hPalette );
439 TRACE(palette,"(%06lx): returning %06lx\n",
441 GDI_HEAP_UNLOCK( hdc );
446 /***********************************************************************
449 int PALETTE_GetObject( PALETTEOBJ * palette, int count, LPSTR buffer )
451 if (count > sizeof(WORD)) count = sizeof(WORD);
452 memcpy( buffer, &palette->logpalette.palNumEntries, count );
457 /***********************************************************************
458 * PALETTE_UnrealizeObject
460 BOOL32 PALETTE_UnrealizeObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
462 if (palette->mapping)
464 free( palette->mapping );
465 palette->mapping = NULL;
467 if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
472 /***********************************************************************
473 * PALETTE_DeleteObject
475 BOOL32 PALETTE_DeleteObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
477 free( palette->mapping );
478 if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
479 return GDI_FreeObject( hpalette );
483 /***********************************************************************
484 * GDISelectPalette (GDI.361)
486 HPALETTE16 WINAPI GDISelectPalette( HDC16 hdc, HPALETTE16 hpal, WORD wBkg)
491 TRACE(palette, "%04x %04x\n", hdc, hpal );
493 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
496 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
499 prev = dc->w.hPalette;
500 dc->w.hPalette = hpal;
501 GDI_HEAP_UNLOCK( hdc );
502 if (!wBkg) hPrimaryPalette = hpal;
507 /***********************************************************************
508 * GDIRealizePalette (GDI.362)
510 UINT16 WINAPI GDIRealizePalette( HDC16 hdc )
514 DC* dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
517 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
521 TRACE(palette, "%04x...\n", hdc );
523 if( dc && dc->w.hPalette != hLastRealizedPalette )
525 if( dc->w.hPalette == STOCK_DEFAULT_PALETTE )
526 return RealizeDefaultPalette( hdc );
528 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC );
530 realized = COLOR_SetMapping(palPtr,0,palPtr->logpalette.palNumEntries,
531 (dc->w.hPalette != hPrimaryPalette) ||
532 (dc->w.hPalette == STOCK_DEFAULT_PALETTE));
533 GDI_HEAP_UNLOCK( dc->w.hPalette );
534 hLastRealizedPalette = dc->w.hPalette;
536 else TRACE(palette, " skipping (hLastRealizedPalette = %04x)\n",
537 hLastRealizedPalette);
538 GDI_HEAP_UNLOCK( hdc );
540 TRACE(palette, " realized %i colors.\n", realized );
541 return (UINT16)realized;
545 /***********************************************************************
546 * RealizeDefaultPalette (GDI.365)
548 UINT16 WINAPI RealizeDefaultPalette( HDC16 hdc )
552 int i, index, realized = 0;
554 TRACE(palette,"%04x\n", hdc );
556 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
559 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
563 if ( dc->w.flags & DC_MEMORY )
565 GDI_HEAP_UNLOCK( hdc );
569 hPrimaryPalette = STOCK_DEFAULT_PALETTE;
570 hLastRealizedPalette = STOCK_DEFAULT_PALETTE;
572 palPtr = (PALETTEOBJ*)GDI_GetObjPtr(STOCK_DEFAULT_PALETTE, PALETTE_MAGIC );
574 /* lookup is needed to account for SetSystemPaletteUse() stuff */
576 for( i = 0; i < 20; i++ )
578 index = COLOR_LookupSystemPixel(*(COLORREF*)(palPtr->logpalette.palPalEntry + i));
580 /* mapping is allocated in COLOR_InitPalette() */
582 if( index != palPtr->mapping[i] ) { palPtr->mapping[i]=index; realized++; }
587 /***********************************************************************
588 * IsDCCurrentPalette (GDI.412)
590 BOOL16 WINAPI IsDCCurrentPalette(HDC16 hDC)
592 DC* dc = (DC *)GDI_GetObjPtr( hDC, DC_MAGIC );
595 GDI_HEAP_UNLOCK( hDC );
596 return dc->w.hPalette == hPrimaryPalette;
602 /***********************************************************************
603 * SelectPalette16 (USER.282)
605 HPALETTE16 WINAPI SelectPalette16( HDC16 hDC, HPALETTE16 hPal,
606 BOOL16 bForceBackground )
608 return SelectPalette32( hDC, hPal, bForceBackground );
612 /***********************************************************************
613 * SelectPalette32 (GDI32.300)
615 HPALETTE32 WINAPI SelectPalette32( HDC32 hDC, HPALETTE32 hPal,
616 BOOL32 bForceBackground )
618 WORD wBkgPalette = 1;
619 PALETTEOBJ* lpt = (PALETTEOBJ*) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
621 TRACE(palette,"dc %04x pal %04x, force=%i \n",
622 hDC, hPal, bForceBackground);
625 TRACE(palette," entries = %d\n",
626 lpt->logpalette.palNumEntries);
627 GDI_HEAP_UNLOCK( hPal );
629 if( hPal != STOCK_DEFAULT_PALETTE )
631 HWND32 hWnd = WindowFromDC32( hDC );
632 HWND32 hActive = GetActiveWindow32();
634 /* set primary palette if it's related to current active */
636 if((!hWnd || (hActive == hWnd || IsChild16(hActive,hWnd))) &&
640 return GDISelectPalette( hDC, hPal, wBkgPalette);
644 /***********************************************************************
645 * RealizePalette16 (USER.283)
647 UINT16 WINAPI RealizePalette16( HDC16 hDC )
649 return RealizePalette32( hDC );
653 /***********************************************************************
654 * RealizePalette32 (GDI32.280)
656 UINT32 WINAPI RealizePalette32( HDC32 hDC )
658 UINT32 realized = GDIRealizePalette( hDC );
660 /* do not send anything if no colors were changed */
662 if( IsDCCurrentPalette( hDC ) && realized &&
663 !(COLOR_GetSystemPaletteFlags() & COLOR_VIRTUAL) )
665 /* Send palette change notification */
668 if( (hWnd = WindowFromDC32( hDC )) )
669 SendMessage16( HWND_BROADCAST, WM_PALETTECHANGED, hWnd, 0L);
675 /**********************************************************************
676 * UpdateColors16 (GDI.366)
678 INT16 WINAPI UpdateColors16( HDC16 hDC )
680 HWND32 hWnd = WindowFromDC32( hDC );
682 /* Docs say that we have to remap current drawable pixel by pixel
683 * but it would take forever given the speed of XGet/PutPixel.
685 if (hWnd && !(COLOR_GetSystemPaletteFlags() & COLOR_VIRTUAL) )
686 InvalidateRect32( hWnd, NULL, FALSE );
691 /**********************************************************************
692 * UpdateColors32 (GDI32.359)
694 BOOL32 WINAPI UpdateColors32( HDC32 hDC )
696 UpdateColors16( hDC );