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.
20 /* #define DEBUG_PALETTE */
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 /***********************************************************************
35 * Create the system palette.
37 HPALETTE16
PALETTE_Init(void)
39 extern const PALETTEENTRY
* COLOR_GetSystemPaletteTemplate(void);
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
);
73 /***********************************************************************
74 * PALETTE_ValidateFlags
76 void PALETTE_ValidateFlags(PALETTEENTRY
* lpPalE
, int size
)
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
;
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
);
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
)
139 dprintf_palette( stddeb
,"GetPaletteEntries: hpal = %04x, %i entries\n",
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
);
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
);
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
)
181 dprintf_palette( stddeb
,"SetPaletteEntries: hpal = %04x, %i entries\n",
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
);
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
);
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
);
224 dprintf_palette(stddeb
,"ResizePalette: hpal = %04x, prev = %i, new = %i\n",
225 hPal
, palPtr
? palPtr
->logpalette
.palNumEntries
: -1,
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
;
242 palPtr
->mapping
= (int*) xrealloc( mapping
, cEntries
* sizeof(int) );
243 if( cEntries
> cPrevEnt
)
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
);
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
)
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
);
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",
315 SystemPaletteUse
= use
;
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
)
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
);
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
);
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
);
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
);
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;
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",
439 GDI_HEAP_UNLOCK( hdc
);
444 /***********************************************************************
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
);
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;
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
)
489 dprintf_palette(stddeb
, "GDISelectPalette: %04x %04x\n", hdc
, hpal
);
491 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
494 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
497 prev
= dc
->w
.hPalette
;
498 dc
->w
.hPalette
= hpal
;
499 GDI_HEAP_UNLOCK( hdc
);
500 if (!wBkg
) hPrimaryPalette
= hpal
;
505 /***********************************************************************
506 * GDIRealizePalette (GDI.362)
508 UINT16 WINAPI
GDIRealizePalette( HDC16 hdc
)
512 DC
* dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
515 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
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
)
550 int i
, index
, realized
= 0;
552 dprintf_palette(stddeb
,"RealizeDefaultPalette: %04x\n", hdc
);
554 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
557 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
561 if ( dc
->w
.flags
& DC_MEMORY
)
563 GDI_HEAP_UNLOCK( hdc
);
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
++; }
585 /***********************************************************************
586 * IsDCCurrentPalette (GDI.412)
588 BOOL16 WINAPI
IsDCCurrentPalette(HDC16 hDC
)
590 DC
* dc
= (DC
*)GDI_GetObjPtr( hDC
, DC_MAGIC
);
593 GDI_HEAP_UNLOCK( hDC
);
594 return dc
->w
.hPalette
== hPrimaryPalette
;
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
);
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
))) &&
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 */
666 if( (hWnd
= WindowFromDC32( hDC
)) )
667 SendMessage16( HWND_BROADCAST
, WM_PALETTECHANGED
, hWnd
, 0L);
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
);
689 /**********************************************************************
690 * UpdateColors32 (GDI32.359)
692 BOOL32 WINAPI
UpdateColors32( HDC32 hDC
)
694 UpdateColors16( hDC
);