Convert remaining source files to utf-8.
[wine/multimedia.git] / dlls / gdi32 / palette.c
blob1b6a966c971fd0527b5c7a9ccd16de7814d58392
1 /*
2 * GDI palette objects
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
21 * NOTES:
22 * PALETTEOBJ is documented in the Dr. Dobbs Journal May 1993.
23 * Information in the "Undocumented Windows" is incorrect.
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "wownt32.h"
34 #include "wine/winuser16.h"
35 #include "gdi_private.h"
36 #include "wine/debug.h"
37 #include "winerror.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(palette);
41 typedef struct tagPALETTEOBJ
43 GDIOBJHDR header;
44 const DC_FUNCTIONS *funcs; /* DC function table */
45 LOGPALETTE logpalette; /* _MUST_ be the last field */
46 } PALETTEOBJ;
48 static INT PALETTE_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer );
49 static BOOL PALETTE_UnrealizeObject( HGDIOBJ handle, void *obj );
50 static BOOL PALETTE_DeleteObject( HGDIOBJ handle, void *obj );
52 static const struct gdi_obj_funcs palette_funcs =
54 NULL, /* pSelectObject */
55 PALETTE_GetObject, /* pGetObjectA */
56 PALETTE_GetObject, /* pGetObjectW */
57 PALETTE_UnrealizeObject, /* pUnrealizeObject */
58 PALETTE_DeleteObject /* pDeleteObject */
61 /* Pointers to USER implementation of SelectPalette/RealizePalette */
62 /* they will be patched by USER on startup */
63 HPALETTE (WINAPI *pfnSelectPalette)(HDC hdc, HPALETTE hpal, WORD bkgnd ) = GDISelectPalette;
64 UINT (WINAPI *pfnRealizePalette)(HDC hdc) = GDIRealizePalette;
66 static UINT SystemPaletteUse = SYSPAL_STATIC; /* currently not considered */
68 static HPALETTE hPrimaryPalette = 0; /* used for WM_PALETTECHANGED */
69 static HPALETTE hLastRealizedPalette = 0; /* UnrealizeObject() needs it */
71 #define NB_RESERVED_COLORS 20 /* number of fixed colors in system palette */
73 static const PALETTEENTRY sys_pal_template[NB_RESERVED_COLORS] =
75 /* first 10 entries in the system palette */
76 /* red green blue flags */
77 { 0x00, 0x00, 0x00, 0 },
78 { 0x80, 0x00, 0x00, 0 },
79 { 0x00, 0x80, 0x00, 0 },
80 { 0x80, 0x80, 0x00, 0 },
81 { 0x00, 0x00, 0x80, 0 },
82 { 0x80, 0x00, 0x80, 0 },
83 { 0x00, 0x80, 0x80, 0 },
84 { 0xc0, 0xc0, 0xc0, 0 },
85 { 0xc0, 0xdc, 0xc0, 0 },
86 { 0xa6, 0xca, 0xf0, 0 },
88 /* ... c_min/2 dynamic colorcells */
90 /* ... gap (for sparse palettes) */
92 /* ... c_min/2 dynamic colorcells */
94 { 0xff, 0xfb, 0xf0, 0 },
95 { 0xa0, 0xa0, 0xa4, 0 },
96 { 0x80, 0x80, 0x80, 0 },
97 { 0xff, 0x00, 0x00, 0 },
98 { 0x00, 0xff, 0x00, 0 },
99 { 0xff, 0xff, 0x00, 0 },
100 { 0x00, 0x00, 0xff, 0 },
101 { 0xff, 0x00, 0xff, 0 },
102 { 0x00, 0xff, 0xff, 0 },
103 { 0xff, 0xff, 0xff, 0 } /* last 10 */
106 /***********************************************************************
107 * PALETTE_Init
109 * Create the system palette.
111 HPALETTE PALETTE_Init(void)
113 HPALETTE hpalette;
114 LOGPALETTE * palPtr;
116 /* create default palette (20 system colors) */
118 palPtr = HeapAlloc( GetProcessHeap(), 0,
119 sizeof(LOGPALETTE) + (NB_RESERVED_COLORS-1)*sizeof(PALETTEENTRY));
120 if (!palPtr) return FALSE;
122 palPtr->palVersion = 0x300;
123 palPtr->palNumEntries = NB_RESERVED_COLORS;
124 memcpy( palPtr->palPalEntry, sys_pal_template, sizeof(sys_pal_template) );
125 hpalette = CreatePalette( palPtr );
126 HeapFree( GetProcessHeap(), 0, palPtr );
127 return hpalette;
131 /***********************************************************************
132 * CreatePalette [GDI32.@]
134 * Creates a logical color palette.
136 * RETURNS
137 * Success: Handle to logical palette
138 * Failure: NULL
140 HPALETTE WINAPI CreatePalette(
141 const LOGPALETTE* palette) /* [in] Pointer to logical color palette */
143 PALETTEOBJ * palettePtr;
144 HPALETTE hpalette;
145 int size;
147 if (!palette) return 0;
148 TRACE("entries=%i\n", palette->palNumEntries);
150 size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
152 if (!(palettePtr = GDI_AllocObject( size + sizeof(int*) +sizeof(GDIOBJHDR),
153 PALETTE_MAGIC, (HGDIOBJ *)&hpalette,
154 &palette_funcs ))) return 0;
155 memcpy( &palettePtr->logpalette, palette, size );
156 palettePtr->funcs = NULL;
157 GDI_ReleaseObj( hpalette );
159 TRACE(" returning %p\n", hpalette);
160 return hpalette;
164 /***********************************************************************
165 * CreateHalftonePalette [GDI32.@]
167 * Creates a halftone palette.
169 * RETURNS
170 * Success: Handle to logical halftone palette
171 * Failure: 0
173 * FIXME: This simply creates the halftone palette derived from running
174 * tests on a windows NT machine. This is assuming a color depth
175 * of greater that 256 color. On a 256 color device the halftone
176 * palette will be different and this function will be incorrect
178 HPALETTE WINAPI CreateHalftonePalette(
179 HDC hdc) /* [in] Handle to device context */
181 int i;
182 struct {
183 WORD Version;
184 WORD NumberOfEntries;
185 PALETTEENTRY aEntries[256];
186 } Palette;
188 Palette.Version = 0x300;
189 Palette.NumberOfEntries = 256;
190 GetSystemPaletteEntries(hdc, 0, 256, Palette.aEntries);
192 Palette.NumberOfEntries = 20;
194 for (i = 0; i < Palette.NumberOfEntries; i++)
196 Palette.aEntries[i].peRed=0xff;
197 Palette.aEntries[i].peGreen=0xff;
198 Palette.aEntries[i].peBlue=0xff;
199 Palette.aEntries[i].peFlags=0x00;
202 Palette.aEntries[0].peRed=0x00;
203 Palette.aEntries[0].peBlue=0x00;
204 Palette.aEntries[0].peGreen=0x00;
206 /* the first 6 */
207 for (i=1; i <= 6; i++)
209 Palette.aEntries[i].peRed=(i%2)?0x80:0;
210 Palette.aEntries[i].peGreen=(i==2)?0x80:(i==3)?0x80:(i==6)?0x80:0;
211 Palette.aEntries[i].peBlue=(i>3)?0x80:0;
214 for (i=7; i <= 12; i++)
216 switch(i)
218 case 7:
219 Palette.aEntries[i].peRed=0xc0;
220 Palette.aEntries[i].peBlue=0xc0;
221 Palette.aEntries[i].peGreen=0xc0;
222 break;
223 case 8:
224 Palette.aEntries[i].peRed=0xc0;
225 Palette.aEntries[i].peGreen=0xdc;
226 Palette.aEntries[i].peBlue=0xc0;
227 break;
228 case 9:
229 Palette.aEntries[i].peRed=0xa6;
230 Palette.aEntries[i].peGreen=0xca;
231 Palette.aEntries[i].peBlue=0xf0;
232 break;
233 case 10:
234 Palette.aEntries[i].peRed=0xff;
235 Palette.aEntries[i].peGreen=0xfb;
236 Palette.aEntries[i].peBlue=0xf0;
237 break;
238 case 11:
239 Palette.aEntries[i].peRed=0xa0;
240 Palette.aEntries[i].peGreen=0xa0;
241 Palette.aEntries[i].peBlue=0xa4;
242 break;
243 case 12:
244 Palette.aEntries[i].peRed=0x80;
245 Palette.aEntries[i].peGreen=0x80;
246 Palette.aEntries[i].peBlue=0x80;
250 for (i=13; i <= 18; i++)
252 Palette.aEntries[i].peRed=(i%2)?0xff:0;
253 Palette.aEntries[i].peGreen=(i==14)?0xff:(i==15)?0xff:(i==18)?0xff:0;
254 Palette.aEntries[i].peBlue=(i>15)?0xff:0x00;
257 return CreatePalette((LOGPALETTE *)&Palette);
261 /***********************************************************************
262 * GetPaletteEntries [GDI32.@]
264 * Retrieves palette entries.
266 * RETURNS
267 * Success: Number of entries from logical palette
268 * Failure: 0
270 UINT WINAPI GetPaletteEntries(
271 HPALETTE hpalette, /* [in] Handle of logical palette */
272 UINT start, /* [in] First entry to receive */
273 UINT count, /* [in] Number of entries to receive */
274 LPPALETTEENTRY entries) /* [out] Address of array receiving entries */
276 PALETTEOBJ * palPtr;
277 UINT numEntries;
279 TRACE("hpal = %p, count=%i\n", hpalette, count );
281 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
282 if (!palPtr) return 0;
284 /* NOTE: not documented but test show this to be the case */
285 if (count == 0)
287 int rc = palPtr->logpalette.palNumEntries;
288 GDI_ReleaseObj( hpalette );
289 return rc;
292 numEntries = palPtr->logpalette.palNumEntries;
293 if (start+count > numEntries) count = numEntries - start;
294 if (entries)
296 if (start >= numEntries)
298 GDI_ReleaseObj( hpalette );
299 return 0;
301 memcpy( entries, &palPtr->logpalette.palPalEntry[start],
302 count * sizeof(PALETTEENTRY) );
305 GDI_ReleaseObj( hpalette );
306 return count;
310 /***********************************************************************
311 * SetPaletteEntries [GDI32.@]
313 * Sets color values for range in palette.
315 * RETURNS
316 * Success: Number of entries that were set
317 * Failure: 0
319 UINT WINAPI SetPaletteEntries(
320 HPALETTE hpalette, /* [in] Handle of logical palette */
321 UINT start, /* [in] Index of first entry to set */
322 UINT count, /* [in] Number of entries to set */
323 const PALETTEENTRY *entries) /* [in] Address of array of structures */
325 PALETTEOBJ * palPtr;
326 UINT numEntries;
328 TRACE("hpal=%p,start=%i,count=%i\n",hpalette,start,count );
330 if (hpalette == GetStockObject(DEFAULT_PALETTE)) return 0;
331 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
332 if (!palPtr) return 0;
334 numEntries = palPtr->logpalette.palNumEntries;
335 if (start >= numEntries)
337 GDI_ReleaseObj( hpalette );
338 return 0;
340 if (start+count > numEntries) count = numEntries - start;
341 memcpy( &palPtr->logpalette.palPalEntry[start], entries,
342 count * sizeof(PALETTEENTRY) );
343 UnrealizeObject( hpalette );
344 GDI_ReleaseObj( hpalette );
345 return count;
349 /***********************************************************************
350 * ResizePalette [GDI32.@]
352 * Resizes logical palette.
354 * RETURNS
355 * Success: TRUE
356 * Failure: FALSE
358 BOOL WINAPI ResizePalette(
359 HPALETTE hPal, /* [in] Handle of logical palette */
360 UINT cEntries) /* [in] Number of entries in logical palette */
362 PALETTEOBJ * palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
363 UINT cPrevEnt, prevVer;
364 int prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
366 TRACE("hpal = %p, prev = %i, new = %i\n",
367 hPal, palPtr ? palPtr->logpalette.palNumEntries : -1, cEntries );
368 if( !palPtr ) return FALSE;
369 cPrevEnt = palPtr->logpalette.palNumEntries;
370 prevVer = palPtr->logpalette.palVersion;
371 prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) +
372 sizeof(int*) + sizeof(GDIOBJHDR);
373 size += sizeof(int*) + sizeof(GDIOBJHDR);
375 if (!(palPtr = GDI_ReallocObject( size, hPal, palPtr ))) return FALSE;
377 PALETTE_UnrealizeObject( hPal, palPtr );
379 if( cEntries > cPrevEnt ) memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
380 palPtr->logpalette.palNumEntries = cEntries;
381 palPtr->logpalette.palVersion = prevVer;
382 GDI_ReleaseObj( hPal );
383 return TRUE;
387 /***********************************************************************
388 * AnimatePalette [GDI32.@]
390 * Replaces entries in logical palette.
392 * RETURNS
393 * Success: TRUE
394 * Failure: FALSE
396 * FIXME
397 * Should use existing mapping when animating a primary palette
399 BOOL WINAPI AnimatePalette(
400 HPALETTE hPal, /* [in] Handle to logical palette */
401 UINT StartIndex, /* [in] First entry in palette */
402 UINT NumEntries, /* [in] Count of entries in palette */
403 const PALETTEENTRY* PaletteColors) /* [in] Pointer to first replacement */
405 TRACE("%p (%i - %i)\n", hPal, StartIndex,StartIndex+NumEntries);
407 if( hPal != GetStockObject(DEFAULT_PALETTE) )
409 PALETTEOBJ * palPtr;
410 UINT pal_entries;
411 const PALETTEENTRY *pptr = PaletteColors;
413 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
414 if (!palPtr) return 0;
416 pal_entries = palPtr->logpalette.palNumEntries;
417 if (StartIndex >= pal_entries)
419 GDI_ReleaseObj( hPal );
420 return 0;
422 if (StartIndex+NumEntries > pal_entries) NumEntries = pal_entries - StartIndex;
424 for (NumEntries += StartIndex; StartIndex < NumEntries; StartIndex++, pptr++) {
425 /* According to MSDN, only animate PC_RESERVED colours */
426 if (palPtr->logpalette.palPalEntry[StartIndex].peFlags & PC_RESERVED) {
427 TRACE("Animating colour (%d,%d,%d) to (%d,%d,%d)\n",
428 palPtr->logpalette.palPalEntry[StartIndex].peRed,
429 palPtr->logpalette.palPalEntry[StartIndex].peGreen,
430 palPtr->logpalette.palPalEntry[StartIndex].peBlue,
431 pptr->peRed, pptr->peGreen, pptr->peBlue);
432 memcpy( &palPtr->logpalette.palPalEntry[StartIndex], pptr,
433 sizeof(PALETTEENTRY) );
434 } else {
435 TRACE("Not animating entry %d -- not PC_RESERVED\n", StartIndex);
438 if (palPtr->funcs && palPtr->funcs->pRealizePalette)
439 palPtr->funcs->pRealizePalette( NULL, hPal, hPal == hPrimaryPalette );
441 GDI_ReleaseObj( hPal );
443 return TRUE;
447 /***********************************************************************
448 * SetSystemPaletteUse [GDI32.@]
450 * Specify whether the system palette contains 2 or 20 static colors.
452 * RETURNS
453 * Success: Previous system palette
454 * Failure: SYSPAL_ERROR
456 UINT WINAPI SetSystemPaletteUse(
457 HDC hdc, /* [in] Handle of device context */
458 UINT use) /* [in] Palette-usage flag */
460 UINT old = SystemPaletteUse;
462 /* Device doesn't support colour palettes */
463 if (!(GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)) {
464 return SYSPAL_ERROR;
467 switch (use) {
468 case SYSPAL_NOSTATIC:
469 case SYSPAL_NOSTATIC256: /* WINVER >= 0x0500 */
470 case SYSPAL_STATIC:
471 SystemPaletteUse = use;
472 return old;
473 default:
474 return SYSPAL_ERROR;
479 /***********************************************************************
480 * GetSystemPaletteUse [GDI32.@]
482 * Gets state of system palette.
484 * RETURNS
485 * Current state of system palette
487 UINT WINAPI GetSystemPaletteUse(
488 HDC hdc) /* [in] Handle of device context */
490 return SystemPaletteUse;
494 /***********************************************************************
495 * GetSystemPaletteEntries [GDI32.@]
497 * Gets range of palette entries.
499 * RETURNS
500 * Success: Number of entries retrieved from palette
501 * Failure: 0
503 UINT WINAPI GetSystemPaletteEntries(
504 HDC hdc, /* [in] Handle of device context */
505 UINT start, /* [in] Index of first entry to be retrieved */
506 UINT count, /* [in] Number of entries to be retrieved */
507 LPPALETTEENTRY entries) /* [out] Array receiving system-palette entries */
509 UINT ret = 0;
510 DC *dc;
512 TRACE("hdc=%p,start=%i,count=%i\n", hdc,start,count);
514 if ((dc = get_dc_ptr( hdc )))
516 if (dc->funcs->pGetSystemPaletteEntries)
517 ret = dc->funcs->pGetSystemPaletteEntries( dc->physDev, start, count, entries );
518 release_dc_ptr( dc );
520 return ret;
524 /***********************************************************************
525 * GetNearestPaletteIndex [GDI32.@]
527 * Gets palette index for color.
529 * NOTES
530 * Should index be initialized to CLR_INVALID instead of 0?
532 * RETURNS
533 * Success: Index of entry in logical palette
534 * Failure: CLR_INVALID
536 UINT WINAPI GetNearestPaletteIndex(
537 HPALETTE hpalette, /* [in] Handle of logical color palette */
538 COLORREF color) /* [in] Color to be matched */
540 PALETTEOBJ* palObj = (PALETTEOBJ*)GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
541 UINT index = 0;
543 if( palObj )
545 int i, diff = 0x7fffffff;
546 int r,g,b;
547 PALETTEENTRY* entry = palObj->logpalette.palPalEntry;
549 for( i = 0; i < palObj->logpalette.palNumEntries && diff ; i++, entry++)
551 r = entry->peRed - GetRValue(color);
552 g = entry->peGreen - GetGValue(color);
553 b = entry->peBlue - GetBValue(color);
555 r = r*r + g*g + b*b;
557 if( r < diff ) { index = i; diff = r; }
559 GDI_ReleaseObj( hpalette );
561 TRACE("(%p,%06x): returning %d\n", hpalette, color, index );
562 return index;
566 /***********************************************************************
567 * GetNearestColor [GDI32.@]
569 * Gets a system color to match.
571 * RETURNS
572 * Success: Color from system palette that corresponds to given color
573 * Failure: CLR_INVALID
575 COLORREF WINAPI GetNearestColor(
576 HDC hdc, /* [in] Handle of device context */
577 COLORREF color) /* [in] Color to be matched */
579 unsigned char spec_type;
580 COLORREF nearest;
581 DC *dc;
583 if (!(dc = get_dc_ptr( hdc ))) return CLR_INVALID;
585 if (dc->funcs->pGetNearestColor)
587 nearest = dc->funcs->pGetNearestColor( dc->physDev, color );
588 release_dc_ptr( dc );
589 return nearest;
592 if (!(GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE))
594 release_dc_ptr( dc );
595 return color;
598 spec_type = color >> 24;
599 if (spec_type == 1 || spec_type == 2)
601 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
603 UINT index;
604 PALETTEENTRY entry;
605 HPALETTE hpal = dc->hPalette ? dc->hPalette : GetStockObject( DEFAULT_PALETTE );
607 if (spec_type == 2) /* PALETTERGB */
608 index = GetNearestPaletteIndex( hpal, color );
609 else /* PALETTEINDEX */
610 index = LOWORD(color);
612 if (!GetPaletteEntries( hpal, index, 1, &entry ))
614 WARN("RGB(%x) : idx %d is out of bounds, assuming NULL\n", color, index );
615 if (!GetPaletteEntries( hpal, 0, 1, &entry ))
617 release_dc_ptr( dc );
618 return CLR_INVALID;
621 color = RGB( entry.peRed, entry.peGreen, entry.peBlue );
623 nearest = color & 0x00ffffff;
624 release_dc_ptr( dc );
626 TRACE("(%06x): returning %06x\n", color, nearest );
627 return nearest;
631 /***********************************************************************
632 * PALETTE_GetObject
634 static INT PALETTE_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
636 PALETTEOBJ *palette = obj;
638 if( !buffer )
639 return sizeof(WORD);
641 if (count > sizeof(WORD)) count = sizeof(WORD);
642 memcpy( buffer, &palette->logpalette.palNumEntries, count );
643 return count;
647 /***********************************************************************
648 * PALETTE_UnrealizeObject
650 static BOOL PALETTE_UnrealizeObject( HGDIOBJ handle, void *obj )
652 PALETTEOBJ *palette = obj;
654 if (palette->funcs)
656 if (palette->funcs->pUnrealizePalette)
657 palette->funcs->pUnrealizePalette( handle );
658 palette->funcs = NULL;
661 if (InterlockedCompareExchangePointer( (void **)&hLastRealizedPalette, 0, handle ) == handle)
662 TRACE("unrealizing palette %p\n", handle);
664 return TRUE;
668 /***********************************************************************
669 * PALETTE_DeleteObject
671 static BOOL PALETTE_DeleteObject( HGDIOBJ handle, void *obj )
673 PALETTE_UnrealizeObject( handle, obj );
674 return GDI_FreeObject( handle, obj );
678 /***********************************************************************
679 * GDISelectPalette (Not a Windows API)
681 HPALETTE WINAPI GDISelectPalette( HDC hdc, HPALETTE hpal, WORD wBkg)
683 HPALETTE ret;
684 DC *dc;
686 TRACE("%p %p\n", hdc, hpal );
688 if (GetObjectType(hpal) != OBJ_PAL)
690 WARN("invalid selected palette %p\n",hpal);
691 return 0;
693 if (!(dc = get_dc_ptr( hdc ))) return 0;
694 ret = dc->hPalette;
695 if (dc->funcs->pSelectPalette) hpal = dc->funcs->pSelectPalette( dc->physDev, hpal, FALSE );
696 if (hpal)
698 dc->hPalette = hpal;
699 if (!wBkg) hPrimaryPalette = hpal;
701 else ret = 0;
702 release_dc_ptr( dc );
703 return ret;
707 /***********************************************************************
708 * GDIRealizePalette (Not a Windows API)
710 UINT WINAPI GDIRealizePalette( HDC hdc )
712 UINT realized = 0;
713 DC* dc = get_dc_ptr( hdc );
715 if (!dc) return 0;
717 TRACE("%p...\n", hdc );
719 if( dc->hPalette == GetStockObject( DEFAULT_PALETTE ))
721 if (dc->funcs->pRealizeDefaultPalette)
722 realized = dc->funcs->pRealizeDefaultPalette( dc->physDev );
724 else if (InterlockedExchangePointer( (void **)&hLastRealizedPalette, dc->hPalette ) != dc->hPalette)
726 if (dc->funcs->pRealizePalette)
728 PALETTEOBJ *palPtr = GDI_GetObjPtr( dc->hPalette, PALETTE_MAGIC );
729 if (palPtr)
731 realized = dc->funcs->pRealizePalette( dc->physDev, dc->hPalette,
732 (dc->hPalette == hPrimaryPalette) );
733 palPtr->funcs = dc->funcs;
734 GDI_ReleaseObj( dc->hPalette );
738 else TRACE(" skipping (hLastRealizedPalette = %p)\n", hLastRealizedPalette);
740 release_dc_ptr( dc );
741 TRACE(" realized %i colors.\n", realized );
742 return realized;
746 /***********************************************************************
747 * RealizeDefaultPalette (GDI.365)
749 UINT16 WINAPI RealizeDefaultPalette16( HDC16 hdc )
751 UINT16 ret = 0;
752 DC *dc;
754 TRACE("%04x\n", hdc );
756 if (!(dc = get_dc_ptr( HDC_32(hdc) ))) return 0;
758 if (dc->funcs->pRealizeDefaultPalette) ret = dc->funcs->pRealizeDefaultPalette( dc->physDev );
759 release_dc_ptr( dc );
760 return ret;
763 /***********************************************************************
764 * IsDCCurrentPalette (GDI.412)
766 BOOL16 WINAPI IsDCCurrentPalette16(HDC16 hDC)
768 DC *dc = get_dc_ptr( HDC_32(hDC) );
769 if (dc)
771 BOOL bRet = dc->hPalette == hPrimaryPalette;
772 release_dc_ptr( dc );
773 return bRet;
775 return FALSE;
779 /***********************************************************************
780 * SelectPalette [GDI32.@]
782 * Selects logical palette into DC.
784 * RETURNS
785 * Success: Previous logical palette
786 * Failure: NULL
788 HPALETTE WINAPI SelectPalette(
789 HDC hDC, /* [in] Handle of device context */
790 HPALETTE hPal, /* [in] Handle of logical color palette */
791 BOOL bForceBackground) /* [in] Foreground/background mode */
793 return pfnSelectPalette( hDC, hPal, bForceBackground );
797 /***********************************************************************
798 * RealizePalette [GDI32.@]
800 * Maps palette entries to system palette.
802 * RETURNS
803 * Success: Number of entries in logical palette
804 * Failure: GDI_ERROR
806 UINT WINAPI RealizePalette(
807 HDC hDC) /* [in] Handle of device context */
809 return pfnRealizePalette( hDC );
813 typedef HWND (WINAPI *WindowFromDC_funcptr)( HDC );
814 typedef BOOL (WINAPI *RedrawWindow_funcptr)( HWND, const RECT *, HRGN, UINT );
816 /**********************************************************************
817 * UpdateColors [GDI32.@]
819 * Remaps current colors to logical palette.
821 * RETURNS
822 * Success: TRUE
823 * Failure: FALSE
825 BOOL WINAPI UpdateColors(
826 HDC hDC) /* [in] Handle of device context */
828 HMODULE mod;
829 int size = GetDeviceCaps( hDC, SIZEPALETTE );
831 if (!size) return 0;
833 mod = GetModuleHandleA("user32.dll");
834 if (mod)
836 WindowFromDC_funcptr pWindowFromDC = (WindowFromDC_funcptr)GetProcAddress(mod,"WindowFromDC");
837 if (pWindowFromDC)
839 HWND hWnd = pWindowFromDC( hDC );
841 /* Docs say that we have to remap current drawable pixel by pixel
842 * but it would take forever given the speed of XGet/PutPixel.
844 if (hWnd && size)
846 RedrawWindow_funcptr pRedrawWindow = (void *)GetProcAddress( mod, "RedrawWindow" );
847 if (pRedrawWindow) pRedrawWindow( hWnd, NULL, 0, RDW_INVALIDATE );
851 return 0x666;
855 /*********************************************************************
856 * SetMagicColors (GDI.606)
858 VOID WINAPI SetMagicColors16(HDC16 hDC, COLORREF color, UINT16 index)
860 FIXME("(hDC %04x, color %04x, index %04x): stub\n", hDC, (int)color, index);
864 /*********************************************************************
865 * SetMagicColors (GDI32.@)
867 BOOL WINAPI SetMagicColors(HDC hdc, ULONG u1, ULONG u2)
869 FIXME("(%p 0x%08x 0x%08x): stub\n", hdc, u1, u2);
870 return TRUE;