Release 970914
[wine.git] / objects / palette.c
blob004942554eca27f29c88069f75458999315e3268
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 <X11/Xlib.h>
15 #include "gdi.h"
16 #include "color.h"
17 #include "palette.h"
18 #include "xmalloc.h"
19 #include "stddebug.h"
20 /* #define DEBUG_PALETTE */
21 #include "debug.h"
23 /* lookup pixel among static entries of the system palette */
24 extern int COLOR_LookupSystemPixel(COLORREF);
26 static UINT32 SystemPaletteUse = SYSPAL_STATIC; /* currently not considered */
28 static HPALETTE16 hPrimaryPalette = 0; /* used for WM_PALETTECHANGED */
29 static HPALETTE16 hLastRealizedPalette = 0; /* UnrealizeObject() needs it */
32 /***********************************************************************
33 * PALETTE_Init
35 * Create the system palette.
37 HPALETTE16 PALETTE_Init(void)
39 extern const PALETTEENTRY* COLOR_GetSystemPaletteTemplate(void);
41 int i;
42 HPALETTE16 hpalette;
43 LOGPALETTE * palPtr;
44 PALETTEOBJ* palObj;
45 const PALETTEENTRY* __sysPalTemplate = COLOR_GetSystemPaletteTemplate();
47 /* create default palette (20 system colors) */
49 palPtr = xmalloc( sizeof(LOGPALETTE) + (NB_RESERVED_COLORS-1)*sizeof(PALETTEENTRY) );
50 if (!palPtr) return FALSE;
52 palPtr->palVersion = 0x300;
53 palPtr->palNumEntries = NB_RESERVED_COLORS;
54 for( i = 0; i < NB_RESERVED_COLORS; i ++ )
56 palPtr->palPalEntry[i].peRed = __sysPalTemplate[i].peRed;
57 palPtr->palPalEntry[i].peGreen = __sysPalTemplate[i].peGreen;
58 palPtr->palPalEntry[i].peBlue = __sysPalTemplate[i].peBlue;
59 palPtr->palPalEntry[i].peFlags = 0;
61 hpalette = CreatePalette16( palPtr );
63 palObj = (PALETTEOBJ*) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
65 palObj->mapping = xmalloc( sizeof(int) * 20 );
67 GDI_HEAP_UNLOCK( hpalette );
69 free( palPtr );
70 return hpalette;
73 /***********************************************************************
74 * PALETTE_ValidateFlags
76 void PALETTE_ValidateFlags(PALETTEENTRY* lpPalE, int size)
78 int i = 0;
79 for( ; i<size ; i++ )
80 lpPalE[i].peFlags = PC_SYS_USED | (lpPalE[i].peFlags & 0x07);
84 /***********************************************************************
85 * CreatePalette16 (GDI.360)
87 HPALETTE16 WINAPI CreatePalette16( const LOGPALETTE* palette )
89 return CreatePalette32( palette );
93 /***********************************************************************
94 * CreatePalette32 (GDI32.53)
96 HPALETTE32 WINAPI CreatePalette32( const LOGPALETTE* palette )
98 PALETTEOBJ * palettePtr;
99 HPALETTE32 hpalette;
100 int size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
102 dprintf_palette(stddeb,"CreatePalette: %i entries, ",
103 palette->palNumEntries);
105 hpalette = GDI_AllocObject( size + sizeof(int*) +sizeof(GDIOBJHDR) , PALETTE_MAGIC );
106 if (!hpalette) return 0;
108 palettePtr = (PALETTEOBJ *) GDI_HEAP_LOCK( hpalette );
109 memcpy( &palettePtr->logpalette, palette, size );
110 PALETTE_ValidateFlags(palettePtr->logpalette.palPalEntry,
111 palettePtr->logpalette.palNumEntries);
112 palettePtr->mapping = NULL;
113 GDI_HEAP_UNLOCK( hpalette );
115 dprintf_palette(stddeb,"returning %04x\n", hpalette);
116 return hpalette;
120 /***********************************************************************
121 * GetPaletteEntries16 (GDI.363)
123 UINT16 WINAPI GetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
124 UINT16 count, LPPALETTEENTRY entries )
126 return GetPaletteEntries32( hpalette, start, count, entries );
130 /***********************************************************************
131 * GetPaletteEntries32 (GDI32.209)
133 UINT32 WINAPI GetPaletteEntries32( HPALETTE32 hpalette, UINT32 start,
134 UINT32 count, LPPALETTEENTRY entries )
136 PALETTEOBJ * palPtr;
137 INT32 numEntries;
139 dprintf_palette( stddeb,"GetPaletteEntries: hpal = %04x, %i entries\n",
140 hpalette, count );
142 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
143 if (!palPtr) return 0;
145 numEntries = palPtr->logpalette.palNumEntries;
146 if (start >= numEntries)
148 GDI_HEAP_UNLOCK( hpalette );
149 return 0;
151 if (start+count > numEntries) count = numEntries - start;
152 memcpy( entries, &palPtr->logpalette.palPalEntry[start],
153 count * sizeof(PALETTEENTRY) );
154 for( numEntries = 0; numEntries < count ; numEntries++ )
155 if (entries[numEntries].peFlags & 0xF0)
156 entries[numEntries].peFlags = 0;
157 GDI_HEAP_UNLOCK( hpalette );
158 return count;
162 /***********************************************************************
163 * SetPaletteEntries16 (GDI.364)
165 UINT16 WINAPI SetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
166 UINT16 count, LPPALETTEENTRY entries )
168 return SetPaletteEntries32( hpalette, start, count, entries );
172 /***********************************************************************
173 * SetPaletteEntries32 (GDI32.326)
175 UINT32 WINAPI SetPaletteEntries32( HPALETTE32 hpalette, UINT32 start,
176 UINT32 count, LPPALETTEENTRY entries )
178 PALETTEOBJ * palPtr;
179 INT32 numEntries;
181 dprintf_palette( stddeb,"SetPaletteEntries: hpal = %04x, %i entries\n",
182 hpalette, count );
184 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
185 if (!palPtr) return 0;
187 numEntries = palPtr->logpalette.palNumEntries;
188 if (start >= numEntries)
190 GDI_HEAP_UNLOCK( hpalette );
191 return 0;
193 if (start+count > numEntries) count = numEntries - start;
194 memcpy( &palPtr->logpalette.palPalEntry[start], entries,
195 count * sizeof(PALETTEENTRY) );
196 PALETTE_ValidateFlags(palPtr->logpalette.palPalEntry,
197 palPtr->logpalette.palNumEntries);
198 free(palPtr->mapping);
199 palPtr->mapping = NULL;
200 GDI_HEAP_UNLOCK( hpalette );
201 return count;
205 /***********************************************************************
206 * ResizePalette16 (GDI.368)
208 BOOL16 WINAPI ResizePalette16( HPALETTE16 hPal, UINT16 cEntries )
210 return ResizePalette32( hPal, cEntries );
214 /***********************************************************************
215 * ResizePalette32 (GDI32.289)
217 BOOL32 WINAPI ResizePalette32( HPALETTE32 hPal, UINT32 cEntries )
219 PALETTEOBJ * palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
220 UINT32 cPrevEnt, prevVer;
221 int prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
222 int* mapping = NULL;
224 dprintf_palette(stddeb,"ResizePalette: hpal = %04x, prev = %i, new = %i\n",
225 hPal, palPtr ? palPtr->logpalette.palNumEntries : -1,
226 cEntries );
227 if( !palPtr ) return FALSE;
228 cPrevEnt = palPtr->logpalette.palNumEntries;
229 prevVer = palPtr->logpalette.palVersion;
230 prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) +
231 sizeof(int*) + sizeof(GDIOBJHDR);
232 size += sizeof(int*) + sizeof(GDIOBJHDR);
233 mapping = palPtr->mapping;
235 GDI_HEAP_UNLOCK( hPal );
237 hPal = GDI_HEAP_REALLOC( hPal, size );
238 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
239 if( !palPtr ) return FALSE;
241 if( mapping )
242 palPtr->mapping = (int*) xrealloc( mapping, cEntries * sizeof(int) );
243 if( cEntries > cPrevEnt )
245 if( mapping )
246 memset(palPtr->mapping + cPrevEnt, 0, (cEntries - cPrevEnt)*sizeof(int));
247 memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
248 PALETTE_ValidateFlags((PALETTEENTRY*)((BYTE*)palPtr + prevsize),
249 cEntries - cPrevEnt );
251 palPtr->logpalette.palNumEntries = cEntries;
252 palPtr->logpalette.palVersion = prevVer;
253 GDI_HEAP_UNLOCK( hPal );
254 return TRUE;
258 /***********************************************************************
259 * AnimatePalette16 (GDI.367)
261 void WINAPI AnimatePalette16( HPALETTE16 hPal, UINT16 StartIndex,
262 UINT16 NumEntries, LPPALETTEENTRY PaletteColors)
264 AnimatePalette32( hPal, StartIndex, NumEntries, PaletteColors );
268 /***********************************************************************
269 * AnimatePalette32 (GDI32.6)
271 * FIXME: should use existing mapping when animating a primary palette
273 BOOL32 WINAPI AnimatePalette32( HPALETTE32 hPal, UINT32 StartIndex,
274 UINT32 NumEntries, LPPALETTEENTRY PaletteColors)
276 dprintf_palette(stddeb, "AnimatePalette: %04x (%i - %i)", hPal,
277 StartIndex, StartIndex + NumEntries );
279 if( hPal != STOCK_DEFAULT_PALETTE )
281 PALETTEOBJ* palPtr = (PALETTEOBJ *)GDI_GetObjPtr(hPal, PALETTE_MAGIC);
283 if( (StartIndex + NumEntries) < palPtr->logpalette.palNumEntries )
285 UINT32 u;
286 for( u = 0; u < NumEntries; u++ )
287 palPtr->logpalette.palPalEntry[u + StartIndex] = PaletteColors[u];
288 COLOR_SetMapping(palPtr, StartIndex, NumEntries,
289 hPal != hPrimaryPalette );
290 GDI_HEAP_UNLOCK( hPal );
291 return TRUE;
294 return FALSE;
298 /***********************************************************************
299 * SetSystemPaletteUse16 (GDI.373)
301 UINT16 WINAPI SetSystemPaletteUse16( HDC16 hdc, UINT16 use )
303 return SetSystemPaletteUse32( hdc, use );
307 /***********************************************************************
308 * SetSystemPaletteUse32 (GDI32.335)
310 UINT32 WINAPI SetSystemPaletteUse32( HDC32 hdc, UINT32 use )
312 UINT32 old = SystemPaletteUse;
313 fprintf( stdnimp,"SetSystemPaletteUse(%04x,%04x) // empty stub !!!\n",
314 hdc, use );
315 SystemPaletteUse = use;
316 return old;
320 /***********************************************************************
321 * GetSystemPaletteUse16 (GDI.374)
323 UINT16 WINAPI GetSystemPaletteUse16( HDC16 hdc )
325 return SystemPaletteUse;
329 /***********************************************************************
330 * GetSystemPaletteUse32 (GDI32.223)
332 UINT32 WINAPI GetSystemPaletteUse32( HDC32 hdc )
334 return SystemPaletteUse;
338 /***********************************************************************
339 * GetSystemPaletteEntries16 (GDI.375)
341 UINT16 WINAPI GetSystemPaletteEntries16( HDC16 hdc, UINT16 start, UINT16 count,
342 LPPALETTEENTRY entries )
344 return GetSystemPaletteEntries32( hdc, start, count, entries );
348 /***********************************************************************
349 * GetSystemPaletteEntries32 (GDI32.222)
351 UINT32 WINAPI GetSystemPaletteEntries32( HDC32 hdc, UINT32 start, UINT32 count,
352 LPPALETTEENTRY entries )
354 UINT32 i;
355 DC *dc;
357 dprintf_palette(stddeb,"GetSystemPaletteEntries: hdc = %04x, cound = %i", hdc, count );
359 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
360 if (start >= dc->w.devCaps->sizePalette)
362 GDI_HEAP_UNLOCK( hdc );
363 return 0;
365 if (start+count >= dc->w.devCaps->sizePalette)
366 count = dc->w.devCaps->sizePalette - start;
367 for (i = 0; i < count; i++)
369 *(COLORREF*)(entries + i) = COLOR_GetSystemPaletteEntry( start + i );
371 dprintf_palette( stddeb,"\tidx(%02x) -> RGB(%08lx)\n",
372 start + i, *(COLORREF*)(entries + i) );
374 GDI_HEAP_UNLOCK( hdc );
375 return count;
379 /***********************************************************************
380 * GetNearestPaletteIndex16 (GDI.370)
382 UINT16 WINAPI GetNearestPaletteIndex16( HPALETTE16 hpalette, COLORREF color )
384 return GetNearestPaletteIndex32( hpalette, color );
388 /***********************************************************************
389 * GetNearestPaletteIndex32 (GDI32.203)
391 UINT32 WINAPI GetNearestPaletteIndex32( HPALETTE32 hpalette, COLORREF color )
393 PALETTEOBJ* palObj = (PALETTEOBJ*)GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
394 UINT32 index = 0;
396 if( palObj )
397 index = COLOR_PaletteLookupPixel( palObj->logpalette.palPalEntry,
398 palObj->logpalette.palNumEntries,
399 NULL, color, FALSE );
401 dprintf_palette(stddeb,"GetNearestPaletteIndex(%04x,%06lx): returning %d\n",
402 hpalette, color, index );
403 GDI_HEAP_UNLOCK( hpalette );
404 return index;
408 /***********************************************************************
409 * GetNearestColor16 (GDI.154)
411 COLORREF WINAPI GetNearestColor16( HDC16 hdc, COLORREF color )
413 return GetNearestColor32( hdc, color );
417 /***********************************************************************
418 * GetNearestColor32 (GDI32.202)
420 COLORREF WINAPI GetNearestColor32( HDC32 hdc, COLORREF color )
422 COLORREF nearest = 0xFADECAFE;
423 DC *dc;
424 PALETTEOBJ *palObj;
426 if ( (dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC )) )
428 palObj = (PALETTEOBJ*)
429 GDI_GetObjPtr( (dc->w.hPalette)? dc->w.hPalette
430 : STOCK_DEFAULT_PALETTE, PALETTE_MAGIC );
432 nearest = COLOR_LookupNearestColor( palObj->logpalette.palPalEntry,
433 palObj->logpalette.palNumEntries, color );
434 GDI_HEAP_UNLOCK( dc->w.hPalette );
437 dprintf_palette(stddeb,"GetNearestColor(%06lx): returning %06lx\n",
438 color, nearest );
439 GDI_HEAP_UNLOCK( hdc );
440 return nearest;
444 /***********************************************************************
445 * PALETTE_GetObject
447 int PALETTE_GetObject( PALETTEOBJ * palette, int count, LPSTR buffer )
449 if (count > sizeof(WORD)) count = sizeof(WORD);
450 memcpy( buffer, &palette->logpalette.palNumEntries, count );
451 return count;
455 /***********************************************************************
456 * PALETTE_UnrealizeObject
458 BOOL32 PALETTE_UnrealizeObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
460 if (palette->mapping)
462 free( palette->mapping );
463 palette->mapping = NULL;
465 if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
466 return TRUE;
470 /***********************************************************************
471 * PALETTE_DeleteObject
473 BOOL32 PALETTE_DeleteObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
475 free( palette->mapping );
476 if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
477 return GDI_FreeObject( hpalette );
481 /***********************************************************************
482 * GDISelectPalette (GDI.361)
484 HPALETTE16 WINAPI GDISelectPalette( HDC16 hdc, HPALETTE16 hpal, WORD wBkg)
486 HPALETTE16 prev;
487 DC *dc;
489 dprintf_palette(stddeb, "GDISelectPalette: %04x %04x\n", hdc, hpal );
491 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
492 if (!dc)
494 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
495 if (!dc) return 0;
497 prev = dc->w.hPalette;
498 dc->w.hPalette = hpal;
499 GDI_HEAP_UNLOCK( hdc );
500 if (!wBkg) hPrimaryPalette = hpal;
501 return prev;
505 /***********************************************************************
506 * GDIRealizePalette (GDI.362)
508 UINT16 WINAPI GDIRealizePalette( HDC16 hdc )
510 PALETTEOBJ* palPtr;
511 int realized = 0;
512 DC* dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
513 if (!dc)
515 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
516 if (!dc) return 0;
519 dprintf_palette(stddeb, "GDIRealizePalette: %04x...", hdc );
521 if( dc && dc->w.hPalette != hLastRealizedPalette )
523 if( dc->w.hPalette == STOCK_DEFAULT_PALETTE )
524 return RealizeDefaultPalette( hdc );
526 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC );
528 realized = COLOR_SetMapping(palPtr,0,palPtr->logpalette.palNumEntries,
529 (dc->w.hPalette != hPrimaryPalette) ||
530 (dc->w.hPalette == STOCK_DEFAULT_PALETTE));
531 GDI_HEAP_UNLOCK( dc->w.hPalette );
532 hLastRealizedPalette = dc->w.hPalette;
534 else dprintf_palette(stddeb, " skipping (hLastRealizedPalette = %04x) ",
535 hLastRealizedPalette);
537 GDI_HEAP_UNLOCK( hdc );
538 dprintf_palette(stdnimp, " realized %i colors\n", realized );
539 return (UINT16)realized;
543 /***********************************************************************
544 * RealizeDefaultPalette (GDI.365)
546 UINT16 WINAPI RealizeDefaultPalette( HDC16 hdc )
548 DC *dc;
549 PALETTEOBJ* palPtr;
550 int i, index, realized = 0;
552 dprintf_palette(stddeb,"RealizeDefaultPalette: %04x\n", hdc );
554 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
555 if (!dc)
557 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
558 if (!dc) return 0;
561 if ( dc->w.flags & DC_MEMORY )
563 GDI_HEAP_UNLOCK( hdc );
564 return 0;
567 hPrimaryPalette = STOCK_DEFAULT_PALETTE;
568 hLastRealizedPalette = STOCK_DEFAULT_PALETTE;
570 palPtr = (PALETTEOBJ*)GDI_GetObjPtr(STOCK_DEFAULT_PALETTE, PALETTE_MAGIC );
572 /* lookup is needed to account for SetSystemPaletteUse() stuff */
574 for( i = 0; i < 20; i++ )
576 index = COLOR_LookupSystemPixel(*(COLORREF*)(palPtr->logpalette.palPalEntry + i));
578 /* mapping is allocated in COLOR_InitPalette() */
580 if( index != palPtr->mapping[i] ) { palPtr->mapping[i]=index; realized++; }
582 return realized;
585 /***********************************************************************
586 * IsDCCurrentPalette (GDI.412)
588 BOOL16 WINAPI IsDCCurrentPalette(HDC16 hDC)
590 DC* dc = (DC *)GDI_GetObjPtr( hDC, DC_MAGIC );
591 if (dc)
593 GDI_HEAP_UNLOCK( hDC );
594 return dc->w.hPalette == hPrimaryPalette;
596 return FALSE;
600 /***********************************************************************
601 * SelectPalette16 (USER.282)
603 HPALETTE16 WINAPI SelectPalette16( HDC16 hDC, HPALETTE16 hPal,
604 BOOL16 bForceBackground )
606 return SelectPalette32( hDC, hPal, bForceBackground );
610 /***********************************************************************
611 * SelectPalette32 (GDI32.300)
613 HPALETTE32 WINAPI SelectPalette32( HDC32 hDC, HPALETTE32 hPal,
614 BOOL32 bForceBackground )
616 WORD wBkgPalette = 1;
617 PALETTEOBJ* lpt = (PALETTEOBJ*) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
619 dprintf_palette(stddeb,"SelectPalette: dc %04x pal %04x, force=%i ",
620 hDC, hPal, bForceBackground);
621 if( !lpt ) return 0;
623 dprintf_palette(stddeb," entries = %d\n",
624 lpt->logpalette.palNumEntries);
625 GDI_HEAP_UNLOCK( hPal );
627 if( hPal != STOCK_DEFAULT_PALETTE )
629 HWND32 hWnd = WindowFromDC32( hDC );
630 HWND32 hActive = GetActiveWindow32();
632 /* set primary palette if it's related to current active */
634 if((!hWnd || (hActive == hWnd || IsChild16(hActive,hWnd))) &&
635 !bForceBackground )
636 wBkgPalette = 0;
638 return GDISelectPalette( hDC, hPal, wBkgPalette);
642 /***********************************************************************
643 * RealizePalette16 (USER.283)
645 UINT16 WINAPI RealizePalette16( HDC16 hDC )
647 return RealizePalette32( hDC );
651 /***********************************************************************
652 * RealizePalette32 (GDI32.280)
654 UINT32 WINAPI RealizePalette32( HDC32 hDC )
656 UINT32 realized = GDIRealizePalette( hDC );
658 /* do not send anything if no colors were changed */
660 if( IsDCCurrentPalette( hDC ) && realized &&
661 !(COLOR_GetSystemPaletteFlags() & COLOR_VIRTUAL) )
663 /* Send palette change notification */
665 HWND32 hWnd;
666 if( (hWnd = WindowFromDC32( hDC )) )
667 SendMessage16( HWND_BROADCAST, WM_PALETTECHANGED, hWnd, 0L);
669 return realized;
673 /**********************************************************************
674 * UpdateColors16 (GDI.366)
676 INT16 WINAPI UpdateColors16( HDC16 hDC )
678 HWND32 hWnd = WindowFromDC32( hDC );
680 /* Docs say that we have to remap current drawable pixel by pixel
681 * but it would take forever given the speed of XGet/PutPixel.
683 if (hWnd && !(COLOR_GetSystemPaletteFlags() & COLOR_VIRTUAL) )
684 InvalidateRect32( hWnd, NULL, FALSE );
685 return 0x666;
689 /**********************************************************************
690 * UpdateColors32 (GDI32.359)
692 BOOL32 WINAPI UpdateColors32( HDC32 hDC )
694 UpdateColors16( hDC );
695 return TRUE;