4 * Copyright 1993,1994 Alexandre Julliard
5 * Copyright 1996 Alex Korobka
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * PALETTEOBJ is documented in the Dr. Dobbs Journal May 1993.
23 * Information in the "Undocumented Windows" is incorrect.
36 #include "gdi_private.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(palette
);
41 typedef BOOL (CDECL
*unrealize_function
)(HPALETTE
);
43 typedef struct tagPALETTEOBJ
45 unrealize_function unrealize
;
46 WORD version
; /* palette version */
47 WORD count
; /* count of palette entries */
48 PALETTEENTRY
*entries
;
51 static INT
PALETTE_GetObject( HGDIOBJ handle
, INT count
, LPVOID buffer
);
52 static BOOL
PALETTE_UnrealizeObject( HGDIOBJ handle
);
53 static BOOL
PALETTE_DeleteObject( HGDIOBJ handle
);
55 static const struct gdi_obj_funcs palette_funcs
=
57 NULL
, /* pSelectObject */
58 PALETTE_GetObject
, /* pGetObjectA */
59 PALETTE_GetObject
, /* pGetObjectW */
60 PALETTE_UnrealizeObject
, /* pUnrealizeObject */
61 PALETTE_DeleteObject
/* pDeleteObject */
64 /* Pointers to USER implementation of SelectPalette/RealizePalette */
65 /* they will be patched by USER on startup */
66 HPALETTE (WINAPI
*pfnSelectPalette
)(HDC hdc
, HPALETTE hpal
, WORD bkgnd
) = GDISelectPalette
;
67 UINT (WINAPI
*pfnRealizePalette
)(HDC hdc
) = GDIRealizePalette
;
69 static UINT SystemPaletteUse
= SYSPAL_STATIC
; /* currently not considered */
71 static HPALETTE hPrimaryPalette
= 0; /* used for WM_PALETTECHANGED */
72 static HPALETTE hLastRealizedPalette
= 0; /* UnrealizeObject() needs it */
75 /***********************************************************************
78 * Create the system palette.
80 HPALETTE
PALETTE_Init(void)
82 const RGBQUAD
*entries
= get_default_color_table( 8 );
83 char buffer
[FIELD_OFFSET( LOGPALETTE
, palPalEntry
[20] )];
84 LOGPALETTE
*palPtr
= (LOGPALETTE
*)buffer
;
87 /* create default palette (20 system colors) */
89 palPtr
->palVersion
= 0x300;
90 palPtr
->palNumEntries
= 20;
91 for (i
= 0; i
< 20; i
++)
93 palPtr
->palPalEntry
[i
].peRed
= entries
[i
< 10 ? i
: 236 + i
].rgbRed
;
94 palPtr
->palPalEntry
[i
].peGreen
= entries
[i
< 10 ? i
: 236 + i
].rgbGreen
;
95 palPtr
->palPalEntry
[i
].peBlue
= entries
[i
< 10 ? i
: 236 + i
].rgbBlue
;
96 palPtr
->palPalEntry
[i
].peFlags
= 0;
98 return CreatePalette( palPtr
);
102 /***********************************************************************
103 * CreatePalette [GDI32.@]
105 * Creates a logical color palette.
108 * Success: Handle to logical palette
111 HPALETTE WINAPI
CreatePalette(
112 const LOGPALETTE
* palette
) /* [in] Pointer to logical color palette */
114 PALETTEOBJ
* palettePtr
;
118 if (!palette
) return 0;
119 TRACE("entries=%i\n", palette
->palNumEntries
);
121 if (!(palettePtr
= HeapAlloc( GetProcessHeap(), 0, sizeof(*palettePtr
) ))) return 0;
122 palettePtr
->unrealize
= NULL
;
123 palettePtr
->version
= palette
->palVersion
;
124 palettePtr
->count
= palette
->palNumEntries
;
125 size
= palettePtr
->count
* sizeof(*palettePtr
->entries
);
126 if (!(palettePtr
->entries
= HeapAlloc( GetProcessHeap(), 0, size
)))
128 HeapFree( GetProcessHeap(), 0, palettePtr
);
131 memcpy( palettePtr
->entries
, palette
->palPalEntry
, size
);
132 if (!(hpalette
= alloc_gdi_handle( palettePtr
, OBJ_PAL
, &palette_funcs
)))
134 HeapFree( GetProcessHeap(), 0, palettePtr
->entries
);
135 HeapFree( GetProcessHeap(), 0, palettePtr
);
137 TRACE(" returning %p\n", hpalette
);
142 /***********************************************************************
143 * CreateHalftonePalette [GDI32.@]
145 * Creates a halftone palette.
148 * Success: Handle to logical halftone palette
151 * FIXME: This simply creates the halftone palette derived from running
152 * tests on a windows NT machine. This is assuming a color depth
153 * of greater that 256 color. On a 256 color device the halftone
154 * palette will be different and this function will be incorrect
156 HPALETTE WINAPI
CreateHalftonePalette(
157 HDC hdc
) /* [in] Handle to device context */
159 const RGBQUAD
*entries
= get_default_color_table( 8 );
160 char buffer
[FIELD_OFFSET( LOGPALETTE
, palPalEntry
[256] )];
161 LOGPALETTE
*pal
= (LOGPALETTE
*)buffer
;
164 pal
->palVersion
= 0x300;
165 pal
->palNumEntries
= 256;
166 for (i
= 0; i
< 256; i
++)
168 pal
->palPalEntry
[i
].peRed
= entries
[i
].rgbRed
;
169 pal
->palPalEntry
[i
].peGreen
= entries
[i
].rgbGreen
;
170 pal
->palPalEntry
[i
].peBlue
= entries
[i
].rgbBlue
;
171 pal
->palPalEntry
[i
].peFlags
= 0;
173 return CreatePalette( pal
);
177 /***********************************************************************
178 * GetPaletteEntries [GDI32.@]
180 * Retrieves palette entries.
183 * Success: Number of entries from logical palette
186 UINT WINAPI
GetPaletteEntries(
187 HPALETTE hpalette
, /* [in] Handle of logical palette */
188 UINT start
, /* [in] First entry to receive */
189 UINT count
, /* [in] Number of entries to receive */
190 LPPALETTEENTRY entries
) /* [out] Address of array receiving entries */
195 TRACE("hpal = %p, count=%i\n", hpalette
, count
);
197 palPtr
= GDI_GetObjPtr( hpalette
, OBJ_PAL
);
198 if (!palPtr
) return 0;
200 /* NOTE: not documented but test show this to be the case */
203 count
= palPtr
->count
;
207 numEntries
= palPtr
->count
;
208 if (start
+count
> numEntries
) count
= numEntries
- start
;
211 if (start
>= numEntries
) count
= 0;
212 else memcpy( entries
, &palPtr
->entries
[start
], count
* sizeof(PALETTEENTRY
) );
216 GDI_ReleaseObj( hpalette
);
221 /***********************************************************************
222 * SetPaletteEntries [GDI32.@]
224 * Sets color values for range in palette.
227 * Success: Number of entries that were set
230 UINT WINAPI
SetPaletteEntries(
231 HPALETTE hpalette
, /* [in] Handle of logical palette */
232 UINT start
, /* [in] Index of first entry to set */
233 UINT count
, /* [in] Number of entries to set */
234 const PALETTEENTRY
*entries
) /* [in] Address of array of structures */
239 TRACE("hpal=%p,start=%i,count=%i\n",hpalette
,start
,count
);
241 hpalette
= get_full_gdi_handle( hpalette
);
242 if (hpalette
== GetStockObject(DEFAULT_PALETTE
)) return 0;
243 palPtr
= GDI_GetObjPtr( hpalette
, OBJ_PAL
);
244 if (!palPtr
) return 0;
246 numEntries
= palPtr
->count
;
247 if (start
>= numEntries
)
249 GDI_ReleaseObj( hpalette
);
252 if (start
+count
> numEntries
) count
= numEntries
- start
;
253 memcpy( &palPtr
->entries
[start
], entries
, count
* sizeof(PALETTEENTRY
) );
254 GDI_ReleaseObj( hpalette
);
255 UnrealizeObject( hpalette
);
260 /***********************************************************************
261 * ResizePalette [GDI32.@]
263 * Resizes logical palette.
269 BOOL WINAPI
ResizePalette(
270 HPALETTE hPal
, /* [in] Handle of logical palette */
271 UINT cEntries
) /* [in] Number of entries in logical palette */
273 PALETTEOBJ
* palPtr
= GDI_GetObjPtr( hPal
, OBJ_PAL
);
274 PALETTEENTRY
*entries
;
276 if( !palPtr
) return FALSE
;
277 TRACE("hpal = %p, prev = %i, new = %i\n", hPal
, palPtr
->count
, cEntries
);
279 if (!(entries
= HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
280 palPtr
->entries
, cEntries
* sizeof(*palPtr
->entries
) )))
282 GDI_ReleaseObj( hPal
);
285 palPtr
->entries
= entries
;
286 palPtr
->count
= cEntries
;
288 GDI_ReleaseObj( hPal
);
289 PALETTE_UnrealizeObject( hPal
);
294 /***********************************************************************
295 * AnimatePalette [GDI32.@]
297 * Replaces entries in logical palette.
304 * Should use existing mapping when animating a primary palette
306 BOOL WINAPI
AnimatePalette(
307 HPALETTE hPal
, /* [in] Handle to logical palette */
308 UINT StartIndex
, /* [in] First entry in palette */
309 UINT NumEntries
, /* [in] Count of entries in palette */
310 const PALETTEENTRY
* PaletteColors
) /* [in] Pointer to first replacement */
312 TRACE("%p (%i - %i)\n", hPal
, StartIndex
,StartIndex
+NumEntries
);
314 hPal
= get_full_gdi_handle( hPal
);
315 if( hPal
!= GetStockObject(DEFAULT_PALETTE
) )
319 const PALETTEENTRY
*pptr
= PaletteColors
;
321 palPtr
= GDI_GetObjPtr( hPal
, OBJ_PAL
);
322 if (!palPtr
) return FALSE
;
324 pal_entries
= palPtr
->count
;
325 if (StartIndex
>= pal_entries
)
327 GDI_ReleaseObj( hPal
);
330 if (StartIndex
+NumEntries
> pal_entries
) NumEntries
= pal_entries
- StartIndex
;
332 for (NumEntries
+= StartIndex
; StartIndex
< NumEntries
; StartIndex
++, pptr
++) {
333 /* According to MSDN, only animate PC_RESERVED colours */
334 if (palPtr
->entries
[StartIndex
].peFlags
& PC_RESERVED
) {
335 TRACE("Animating colour (%d,%d,%d) to (%d,%d,%d)\n",
336 palPtr
->entries
[StartIndex
].peRed
,
337 palPtr
->entries
[StartIndex
].peGreen
,
338 palPtr
->entries
[StartIndex
].peBlue
,
339 pptr
->peRed
, pptr
->peGreen
, pptr
->peBlue
);
340 palPtr
->entries
[StartIndex
] = *pptr
;
342 TRACE("Not animating entry %d -- not PC_RESERVED\n", StartIndex
);
345 GDI_ReleaseObj( hPal
);
346 /* FIXME: check for palette selected in active window */
352 /***********************************************************************
353 * SetSystemPaletteUse [GDI32.@]
355 * Specify whether the system palette contains 2 or 20 static colors.
358 * Success: Previous system palette
359 * Failure: SYSPAL_ERROR
361 UINT WINAPI
SetSystemPaletteUse(
362 HDC hdc
, /* [in] Handle of device context */
363 UINT use
) /* [in] Palette-usage flag */
365 UINT old
= SystemPaletteUse
;
367 /* Device doesn't support colour palettes */
368 if (!(GetDeviceCaps(hdc
, RASTERCAPS
) & RC_PALETTE
)) {
373 case SYSPAL_NOSTATIC
:
374 case SYSPAL_NOSTATIC256
: /* WINVER >= 0x0500 */
376 SystemPaletteUse
= use
;
384 /***********************************************************************
385 * GetSystemPaletteUse [GDI32.@]
387 * Gets state of system palette.
390 * Current state of system palette
392 UINT WINAPI
GetSystemPaletteUse(
393 HDC hdc
) /* [in] Handle of device context */
395 return SystemPaletteUse
;
399 /***********************************************************************
400 * GetSystemPaletteEntries [GDI32.@]
402 * Gets range of palette entries.
405 * Success: Number of entries retrieved from palette
408 UINT WINAPI
GetSystemPaletteEntries(
409 HDC hdc
, /* [in] Handle of device context */
410 UINT start
, /* [in] Index of first entry to be retrieved */
411 UINT count
, /* [in] Number of entries to be retrieved */
412 LPPALETTEENTRY entries
) /* [out] Array receiving system-palette entries */
417 TRACE("hdc=%p,start=%i,count=%i\n", hdc
,start
,count
);
419 if ((dc
= get_dc_ptr( hdc
)))
421 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pGetSystemPaletteEntries
);
422 ret
= physdev
->funcs
->pGetSystemPaletteEntries( physdev
, start
, count
, entries
);
423 release_dc_ptr( dc
);
429 /* null driver fallback implementation for GetSystemPaletteEntries */
430 UINT CDECL
nulldrv_GetSystemPaletteEntries( PHYSDEV dev
, UINT start
, UINT count
, PALETTEENTRY
*entries
)
432 if (entries
&& start
< 256)
435 const RGBQUAD
*default_entries
;
437 if (start
+ count
> 256) count
= 256 - start
;
439 default_entries
= get_default_color_table( 8 );
440 for (i
= 0; i
< count
; ++i
)
442 if (start
+ i
< 10 || start
+ i
>= 246)
444 entries
[i
].peRed
= default_entries
[start
+ i
].rgbRed
;
445 entries
[i
].peGreen
= default_entries
[start
+ i
].rgbGreen
;
446 entries
[i
].peBlue
= default_entries
[start
+ i
].rgbBlue
;
450 entries
[i
].peRed
= 0;
451 entries
[i
].peGreen
= 0;
452 entries
[i
].peBlue
= 0;
454 entries
[i
].peFlags
= 0;
460 /***********************************************************************
461 * GetNearestPaletteIndex [GDI32.@]
463 * Gets palette index for color.
466 * Should index be initialized to CLR_INVALID instead of 0?
469 * Success: Index of entry in logical palette
470 * Failure: CLR_INVALID
472 UINT WINAPI
GetNearestPaletteIndex(
473 HPALETTE hpalette
, /* [in] Handle of logical color palette */
474 COLORREF color
) /* [in] Color to be matched */
476 PALETTEOBJ
* palObj
= GDI_GetObjPtr( hpalette
, OBJ_PAL
);
481 int i
, diff
= 0x7fffffff;
483 PALETTEENTRY
* entry
= palObj
->entries
;
485 for( i
= 0; i
< palObj
->count
&& diff
; i
++, entry
++)
487 r
= entry
->peRed
- GetRValue(color
);
488 g
= entry
->peGreen
- GetGValue(color
);
489 b
= entry
->peBlue
- GetBValue(color
);
493 if( r
< diff
) { index
= i
; diff
= r
; }
495 GDI_ReleaseObj( hpalette
);
497 TRACE("(%p,%06x): returning %d\n", hpalette
, color
, index
);
502 /* null driver fallback implementation for GetNearestColor */
503 COLORREF CDECL
nulldrv_GetNearestColor( PHYSDEV dev
, COLORREF color
)
505 unsigned char spec_type
;
506 DC
*dc
= get_nulldrv_dc( dev
);
508 if (!(GetDeviceCaps( dev
->hdc
, RASTERCAPS
) & RC_PALETTE
)) return color
;
510 spec_type
= color
>> 24;
511 if (spec_type
== 1 || spec_type
== 2)
513 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
516 HPALETTE hpal
= dc
->hPalette
;
518 if (!hpal
) hpal
= GetStockObject( DEFAULT_PALETTE
);
519 if (spec_type
== 2) /* PALETTERGB */
520 index
= GetNearestPaletteIndex( hpal
, color
);
521 else /* PALETTEINDEX */
522 index
= LOWORD(color
);
524 if (!GetPaletteEntries( hpal
, index
, 1, &entry
))
526 WARN("RGB(%x) : idx %d is out of bounds, assuming NULL\n", color
, index
);
527 if (!GetPaletteEntries( hpal
, 0, 1, &entry
)) return CLR_INVALID
;
529 color
= RGB( entry
.peRed
, entry
.peGreen
, entry
.peBlue
);
531 return color
& 0x00ffffff;
535 /***********************************************************************
536 * GetNearestColor [GDI32.@]
538 * Gets a system color to match.
541 * Success: Color from system palette that corresponds to given color
542 * Failure: CLR_INVALID
544 COLORREF WINAPI
GetNearestColor(
545 HDC hdc
, /* [in] Handle of device context */
546 COLORREF color
) /* [in] Color to be matched */
548 COLORREF nearest
= CLR_INVALID
;
551 if ((dc
= get_dc_ptr( hdc
)))
553 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pGetNearestColor
);
554 nearest
= physdev
->funcs
->pGetNearestColor( physdev
, color
);
555 release_dc_ptr( dc
);
561 /***********************************************************************
564 static INT
PALETTE_GetObject( HGDIOBJ handle
, INT count
, LPVOID buffer
)
566 PALETTEOBJ
*palette
= GDI_GetObjPtr( handle
, OBJ_PAL
);
568 if (!palette
) return 0;
572 if (count
> sizeof(WORD
)) count
= sizeof(WORD
);
573 memcpy( buffer
, &palette
->count
, count
);
575 else count
= sizeof(WORD
);
576 GDI_ReleaseObj( handle
);
581 /***********************************************************************
582 * PALETTE_UnrealizeObject
584 static BOOL
PALETTE_UnrealizeObject( HGDIOBJ handle
)
586 PALETTEOBJ
*palette
= GDI_GetObjPtr( handle
, OBJ_PAL
);
590 unrealize_function unrealize
= palette
->unrealize
;
591 palette
->unrealize
= NULL
;
592 GDI_ReleaseObj( handle
);
593 if (unrealize
) unrealize( handle
);
596 if (InterlockedCompareExchangePointer( (void **)&hLastRealizedPalette
, 0, handle
) == handle
)
597 TRACE("unrealizing palette %p\n", handle
);
603 /***********************************************************************
604 * PALETTE_DeleteObject
606 static BOOL
PALETTE_DeleteObject( HGDIOBJ handle
)
610 PALETTE_UnrealizeObject( handle
);
611 if (!(obj
= free_gdi_handle( handle
))) return FALSE
;
612 HeapFree( GetProcessHeap(), 0, obj
->entries
);
613 HeapFree( GetProcessHeap(), 0, obj
);
618 /***********************************************************************
619 * GDISelectPalette (Not a Windows API)
621 HPALETTE WINAPI
GDISelectPalette( HDC hdc
, HPALETTE hpal
, WORD wBkg
)
626 TRACE("%p %p\n", hdc
, hpal
);
628 hpal
= get_full_gdi_handle( hpal
);
629 if (GetObjectType(hpal
) != OBJ_PAL
)
631 WARN("invalid selected palette %p\n",hpal
);
634 if ((dc
= get_dc_ptr( hdc
)))
636 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pSelectPalette
);
638 if (physdev
->funcs
->pSelectPalette( physdev
, hpal
, FALSE
))
641 if (!wBkg
) hPrimaryPalette
= hpal
;
644 release_dc_ptr( dc
);
650 /***********************************************************************
651 * GDIRealizePalette (Not a Windows API)
653 UINT WINAPI
GDIRealizePalette( HDC hdc
)
656 DC
* dc
= get_dc_ptr( hdc
);
660 TRACE("%p...\n", hdc
);
662 if( dc
->hPalette
== GetStockObject( DEFAULT_PALETTE
))
664 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pRealizeDefaultPalette
);
665 realized
= physdev
->funcs
->pRealizeDefaultPalette( physdev
);
667 else if (InterlockedExchangePointer( (void **)&hLastRealizedPalette
, dc
->hPalette
) != dc
->hPalette
)
669 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pRealizePalette
);
670 PALETTEOBJ
*palPtr
= GDI_GetObjPtr( dc
->hPalette
, OBJ_PAL
);
673 realized
= physdev
->funcs
->pRealizePalette( physdev
, dc
->hPalette
,
674 (dc
->hPalette
== hPrimaryPalette
) );
675 palPtr
->unrealize
= physdev
->funcs
->pUnrealizePalette
;
676 GDI_ReleaseObj( dc
->hPalette
);
679 else TRACE(" skipping (hLastRealizedPalette = %p)\n", hLastRealizedPalette
);
681 release_dc_ptr( dc
);
682 TRACE(" realized %i colors.\n", realized
);
687 /***********************************************************************
688 * SelectPalette [GDI32.@]
690 * Selects logical palette into DC.
693 * Success: Previous logical palette
696 HPALETTE WINAPI
SelectPalette(
697 HDC hDC
, /* [in] Handle of device context */
698 HPALETTE hPal
, /* [in] Handle of logical color palette */
699 BOOL bForceBackground
) /* [in] Foreground/background mode */
701 return pfnSelectPalette( hDC
, hPal
, bForceBackground
);
705 /***********************************************************************
706 * RealizePalette [GDI32.@]
708 * Maps palette entries to system palette.
711 * Success: Number of entries in logical palette
714 UINT WINAPI
RealizePalette(
715 HDC hDC
) /* [in] Handle of device context */
717 return pfnRealizePalette( hDC
);
721 typedef HWND (WINAPI
*WindowFromDC_funcptr
)( HDC
);
722 typedef BOOL (WINAPI
*RedrawWindow_funcptr
)( HWND
, const RECT
*, HRGN
, UINT
);
724 /**********************************************************************
725 * UpdateColors [GDI32.@]
727 * Remaps current colors to logical palette.
733 BOOL WINAPI
UpdateColors(
734 HDC hDC
) /* [in] Handle of device context */
737 int size
= GetDeviceCaps( hDC
, SIZEPALETTE
);
739 if (!size
) return FALSE
;
741 mod
= GetModuleHandleA("user32.dll");
744 WindowFromDC_funcptr pWindowFromDC
= (WindowFromDC_funcptr
)GetProcAddress(mod
,"WindowFromDC");
747 HWND hWnd
= pWindowFromDC( hDC
);
749 /* Docs say that we have to remap current drawable pixel by pixel
750 * but it would take forever given the speed of XGet/PutPixel.
754 RedrawWindow_funcptr pRedrawWindow
= (void *)GetProcAddress( mod
, "RedrawWindow" );
755 if (pRedrawWindow
) pRedrawWindow( hWnd
, NULL
, 0, RDW_INVALIDATE
);
762 /*********************************************************************
763 * SetMagicColors (GDI32.@)
765 BOOL WINAPI
SetMagicColors(HDC hdc
, ULONG u1
, ULONG u2
)
767 FIXME("(%p 0x%08x 0x%08x): stub\n", hdc
, u1
, u2
);