Release 980301
[wine/hacks.git] / objects / palette.c
blobd11c2cd435976cbc38cdeb30277d623a38055032
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 "ts_xlib.h"
15 #include "gdi.h"
16 #include "color.h"
17 #include "palette.h"
18 #include "xmalloc.h"
19 #include "debug.h"
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 /***********************************************************************
28 * PALETTE_Init
30 * Create the system palette.
32 HPALETTE16 PALETTE_Init(void)
34 int i;
35 HPALETTE16 hpalette;
36 LOGPALETTE * palPtr;
37 PALETTEOBJ* palObj;
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 );
64 return hpalette;
67 /***********************************************************************
68 * PALETTE_ValidateFlags
70 void PALETTE_ValidateFlags(PALETTEENTRY* lpPalE, int size)
72 int i = 0;
73 for( ; i<size ; i++ )
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;
93 HPALETTE32 hpalette;
94 int size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
96 dprintf_info(palette,"CreatePalette: %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 dprintf_info(palette," returning %04x\n", hpalette);
110 return hpalette;
113 /***********************************************************************
114 * CreateHalftonePalette (GDI32.47)
116 HPALETTE32 WINAPI CreateHalftonePalette(HDC32 hdc)
118 fprintf(stdnimp,"CreateHalftonePalette: empty stub!\n");
119 return NULL;
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 )
138 PALETTEOBJ * palPtr;
139 INT32 numEntries;
141 dprintf_info(palette,"GetPaletteEntries: hpal = %04x, %i entries\n",
142 hpalette, count );
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 );
151 return 0;
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 );
160 return count;
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 )
180 PALETTEOBJ * palPtr;
181 INT32 numEntries;
183 dprintf_info(palette,"SetPaletteEntries: hpal = %04x, %i entries\n",
184 hpalette, count );
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 );
193 return 0;
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 );
203 return count;
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);
224 int* mapping = NULL;
226 dprintf_info(palette,"ResizePalette: hpal = %04x, prev = %i, new = %i\n",
227 hPal, palPtr ? palPtr->logpalette.palNumEntries : -1,
228 cEntries );
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;
243 if( mapping )
244 palPtr->mapping = (int*) xrealloc( mapping, cEntries * sizeof(int) );
245 if( cEntries > cPrevEnt )
247 if( mapping )
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 );
256 return TRUE;
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 dprintf_info(palette, "AnimatePalette: %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 )
287 UINT32 u;
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 );
293 return TRUE;
296 return FALSE;
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 fprintf( stdnimp,"SetSystemPaletteUse(%04x,%04x) // empty stub !!!\n",
316 hdc, use );
317 SystemPaletteUse = use;
318 return old;
322 /***********************************************************************
323 * GetSystemPaletteUse16 (GDI.374)
325 UINT16 WINAPI GetSystemPaletteUse16( HDC16 hdc )
327 return SystemPaletteUse;
331 /***********************************************************************
332 * GetSystemPaletteUse32 (GDI32.223)
334 UINT32 WINAPI GetSystemPaletteUse32( HDC32 hdc )
336 return SystemPaletteUse;
340 /***********************************************************************
341 * GetSystemPaletteEntries16 (GDI.375)
343 UINT16 WINAPI GetSystemPaletteEntries16( HDC16 hdc, UINT16 start, UINT16 count,
344 LPPALETTEENTRY entries )
346 return GetSystemPaletteEntries32( hdc, start, count, entries );
350 /***********************************************************************
351 * GetSystemPaletteEntries32 (GDI32.222)
353 UINT32 WINAPI GetSystemPaletteEntries32( HDC32 hdc, UINT32 start, UINT32 count,
354 LPPALETTEENTRY entries )
356 UINT32 i;
357 DC *dc;
359 dprintf_info(palette, "GetSystemPaletteEntries: hdc = %04x, "
360 "cound = %i\n", hdc, count );
362 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
363 if (start >= dc->w.devCaps->sizePalette)
365 GDI_HEAP_UNLOCK( hdc );
366 return 0;
368 if (start+count >= dc->w.devCaps->sizePalette)
369 count = dc->w.devCaps->sizePalette - start;
370 for (i = 0; i < count; i++)
372 *(COLORREF*)(entries + i) = COLOR_GetSystemPaletteEntry( start + i );
374 dprintf_info(palette,"\tidx(%02x) -> RGB(%08lx)\n",
375 start + i, *(COLORREF*)(entries + i) );
377 GDI_HEAP_UNLOCK( hdc );
378 return count;
382 /***********************************************************************
383 * GetNearestPaletteIndex16 (GDI.370)
385 UINT16 WINAPI GetNearestPaletteIndex16( HPALETTE16 hpalette, COLORREF color )
387 return GetNearestPaletteIndex32( hpalette, color );
391 /***********************************************************************
392 * GetNearestPaletteIndex32 (GDI32.203)
394 UINT32 WINAPI GetNearestPaletteIndex32( HPALETTE32 hpalette, COLORREF color )
396 PALETTEOBJ* palObj = (PALETTEOBJ*)GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
397 UINT32 index = 0;
399 if( palObj )
400 index = COLOR_PaletteLookupPixel( palObj->logpalette.palPalEntry,
401 palObj->logpalette.palNumEntries,
402 NULL, color, FALSE );
404 dprintf_info(palette,"GetNearestPaletteIndex(%04x,%06lx): returning %d\n",
405 hpalette, color, index );
406 GDI_HEAP_UNLOCK( hpalette );
407 return index;
411 /***********************************************************************
412 * GetNearestColor16 (GDI.154)
414 COLORREF WINAPI GetNearestColor16( HDC16 hdc, COLORREF color )
416 return GetNearestColor32( hdc, color );
420 /***********************************************************************
421 * GetNearestColor32 (GDI32.202)
423 COLORREF WINAPI GetNearestColor32( HDC32 hdc, COLORREF color )
425 COLORREF nearest = 0xFADECAFE;
426 DC *dc;
427 PALETTEOBJ *palObj;
429 if ( (dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC )) )
431 palObj = (PALETTEOBJ*)
432 GDI_GetObjPtr( (dc->w.hPalette)? dc->w.hPalette
433 : STOCK_DEFAULT_PALETTE, PALETTE_MAGIC );
435 nearest = COLOR_LookupNearestColor( palObj->logpalette.palPalEntry,
436 palObj->logpalette.palNumEntries, color );
437 GDI_HEAP_UNLOCK( dc->w.hPalette );
440 dprintf_info(palette,"GetNearestColor(%06lx): returning %06lx\n",
441 color, nearest );
442 GDI_HEAP_UNLOCK( hdc );
443 return nearest;
447 /***********************************************************************
448 * PALETTE_GetObject
450 int PALETTE_GetObject( PALETTEOBJ * palette, int count, LPSTR buffer )
452 if (count > sizeof(WORD)) count = sizeof(WORD);
453 memcpy( buffer, &palette->logpalette.palNumEntries, count );
454 return count;
458 /***********************************************************************
459 * PALETTE_UnrealizeObject
461 BOOL32 PALETTE_UnrealizeObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
463 if (palette->mapping)
465 free( palette->mapping );
466 palette->mapping = NULL;
468 if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
469 return TRUE;
473 /***********************************************************************
474 * PALETTE_DeleteObject
476 BOOL32 PALETTE_DeleteObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
478 free( palette->mapping );
479 if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
480 return GDI_FreeObject( hpalette );
484 /***********************************************************************
485 * GDISelectPalette (GDI.361)
487 HPALETTE16 WINAPI GDISelectPalette( HDC16 hdc, HPALETTE16 hpal, WORD wBkg)
489 HPALETTE16 prev;
490 DC *dc;
492 dprintf_info(palette, "GDISelectPalette: %04x %04x\n", hdc, hpal );
494 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
495 if (!dc)
497 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
498 if (!dc) return 0;
500 prev = dc->w.hPalette;
501 dc->w.hPalette = hpal;
502 GDI_HEAP_UNLOCK( hdc );
503 if (!wBkg) hPrimaryPalette = hpal;
504 return prev;
508 /***********************************************************************
509 * GDIRealizePalette (GDI.362)
511 UINT16 WINAPI GDIRealizePalette( HDC16 hdc )
513 PALETTEOBJ* palPtr;
514 int realized = 0;
515 DC* dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
516 if (!dc)
518 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
519 if (!dc) return 0;
522 dprintf_info(palette, "GDIRealizePalette: %04x...\n", hdc );
524 if( dc && dc->w.hPalette != hLastRealizedPalette )
526 if( dc->w.hPalette == STOCK_DEFAULT_PALETTE )
527 return RealizeDefaultPalette( hdc );
529 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC );
531 realized = COLOR_SetMapping(palPtr,0,palPtr->logpalette.palNumEntries,
532 (dc->w.hPalette != hPrimaryPalette) ||
533 (dc->w.hPalette == STOCK_DEFAULT_PALETTE));
534 GDI_HEAP_UNLOCK( dc->w.hPalette );
535 hLastRealizedPalette = dc->w.hPalette;
537 else dprintf_info(palette, " skipping (hLastRealizedPalette = %04x)\n",
538 hLastRealizedPalette);
540 GDI_HEAP_UNLOCK( hdc );
541 dprintf_fixme(palette, " realized %i colors - not impmenented?\n", realized );
542 return (UINT16)realized;
546 /***********************************************************************
547 * RealizeDefaultPalette (GDI.365)
549 UINT16 WINAPI RealizeDefaultPalette( HDC16 hdc )
551 DC *dc;
552 PALETTEOBJ* palPtr;
553 int i, index, realized = 0;
555 dprintf_info(palette,"RealizeDefaultPalette: %04x\n", hdc );
557 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
558 if (!dc)
560 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
561 if (!dc) return 0;
564 if ( dc->w.flags & DC_MEMORY )
566 GDI_HEAP_UNLOCK( hdc );
567 return 0;
570 hPrimaryPalette = STOCK_DEFAULT_PALETTE;
571 hLastRealizedPalette = STOCK_DEFAULT_PALETTE;
573 palPtr = (PALETTEOBJ*)GDI_GetObjPtr(STOCK_DEFAULT_PALETTE, PALETTE_MAGIC );
575 /* lookup is needed to account for SetSystemPaletteUse() stuff */
577 for( i = 0; i < 20; i++ )
579 index = COLOR_LookupSystemPixel(*(COLORREF*)(palPtr->logpalette.palPalEntry + i));
581 /* mapping is allocated in COLOR_InitPalette() */
583 if( index != palPtr->mapping[i] ) { palPtr->mapping[i]=index; realized++; }
585 return realized;
588 /***********************************************************************
589 * IsDCCurrentPalette (GDI.412)
591 BOOL16 WINAPI IsDCCurrentPalette(HDC16 hDC)
593 DC* dc = (DC *)GDI_GetObjPtr( hDC, DC_MAGIC );
594 if (dc)
596 GDI_HEAP_UNLOCK( hDC );
597 return dc->w.hPalette == hPrimaryPalette;
599 return FALSE;
603 /***********************************************************************
604 * SelectPalette16 (USER.282)
606 HPALETTE16 WINAPI SelectPalette16( HDC16 hDC, HPALETTE16 hPal,
607 BOOL16 bForceBackground )
609 return SelectPalette32( hDC, hPal, bForceBackground );
613 /***********************************************************************
614 * SelectPalette32 (GDI32.300)
616 HPALETTE32 WINAPI SelectPalette32( HDC32 hDC, HPALETTE32 hPal,
617 BOOL32 bForceBackground )
619 WORD wBkgPalette = 1;
620 PALETTEOBJ* lpt = (PALETTEOBJ*) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
622 dprintf_info(palette,"SelectPalette: dc %04x pal %04x, force=%i \n",
623 hDC, hPal, bForceBackground);
624 if( !lpt ) return 0;
626 dprintf_info(palette," entries = %d\n",
627 lpt->logpalette.palNumEntries);
628 GDI_HEAP_UNLOCK( hPal );
630 if( hPal != STOCK_DEFAULT_PALETTE )
632 HWND32 hWnd = WindowFromDC32( hDC );
633 HWND32 hActive = GetActiveWindow32();
635 /* set primary palette if it's related to current active */
637 if((!hWnd || (hActive == hWnd || IsChild16(hActive,hWnd))) &&
638 !bForceBackground )
639 wBkgPalette = 0;
641 return GDISelectPalette( hDC, hPal, wBkgPalette);
645 /***********************************************************************
646 * RealizePalette16 (USER.283)
648 UINT16 WINAPI RealizePalette16( HDC16 hDC )
650 return RealizePalette32( hDC );
654 /***********************************************************************
655 * RealizePalette32 (GDI32.280)
657 UINT32 WINAPI RealizePalette32( HDC32 hDC )
659 UINT32 realized = GDIRealizePalette( hDC );
661 /* do not send anything if no colors were changed */
663 if( IsDCCurrentPalette( hDC ) && realized &&
664 !(COLOR_GetSystemPaletteFlags() & COLOR_VIRTUAL) )
666 /* Send palette change notification */
668 HWND32 hWnd;
669 if( (hWnd = WindowFromDC32( hDC )) )
670 SendMessage16( HWND_BROADCAST, WM_PALETTECHANGED, hWnd, 0L);
672 return realized;
676 /**********************************************************************
677 * UpdateColors16 (GDI.366)
679 INT16 WINAPI UpdateColors16( HDC16 hDC )
681 HWND32 hWnd = WindowFromDC32( hDC );
683 /* Docs say that we have to remap current drawable pixel by pixel
684 * but it would take forever given the speed of XGet/PutPixel.
686 if (hWnd && !(COLOR_GetSystemPaletteFlags() & COLOR_VIRTUAL) )
687 InvalidateRect32( hWnd, NULL, FALSE );
688 return 0x666;
692 /**********************************************************************
693 * UpdateColors32 (GDI32.359)
695 BOOL32 WINAPI UpdateColors32( HDC32 hDC )
697 UpdateColors16( hDC );
698 return TRUE;