gdi32: Release GDI handles before freeing the object.
[wine/hacks.git] / dlls / gdi32 / palette.c
bloba6584402dd09ad63bbf8dedce0dbad878c8f65f6
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, INT count, LPVOID buffer );
49 static BOOL PALETTE_UnrealizeObject( HGDIOBJ handle );
50 static BOOL PALETTE_DeleteObject( HGDIOBJ handle );
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 = HeapAlloc( GetProcessHeap(), 0,
153 FIELD_OFFSET( PALETTEOBJ, logpalette.palPalEntry[palette->palNumEntries] ))))
154 return 0;
156 memcpy( &palettePtr->logpalette, palette, size );
157 palettePtr->funcs = NULL;
158 if (!(hpalette = alloc_gdi_handle( &palettePtr->header, OBJ_PAL, &palette_funcs )))
159 HeapFree( GetProcessHeap(), 0, palettePtr );
161 TRACE(" returning %p\n", hpalette);
162 return hpalette;
166 /***********************************************************************
167 * CreateHalftonePalette [GDI32.@]
169 * Creates a halftone palette.
171 * RETURNS
172 * Success: Handle to logical halftone palette
173 * Failure: 0
175 * FIXME: This simply creates the halftone palette derived from running
176 * tests on a windows NT machine. This is assuming a color depth
177 * of greater that 256 color. On a 256 color device the halftone
178 * palette will be different and this function will be incorrect
180 HPALETTE WINAPI CreateHalftonePalette(
181 HDC hdc) /* [in] Handle to device context */
183 int i;
184 struct {
185 WORD Version;
186 WORD NumberOfEntries;
187 PALETTEENTRY aEntries[256];
188 } Palette;
190 Palette.Version = 0x300;
191 Palette.NumberOfEntries = 256;
192 GetSystemPaletteEntries(hdc, 0, 256, Palette.aEntries);
194 Palette.NumberOfEntries = 20;
196 for (i = 0; i < Palette.NumberOfEntries; i++)
198 Palette.aEntries[i].peRed=0xff;
199 Palette.aEntries[i].peGreen=0xff;
200 Palette.aEntries[i].peBlue=0xff;
201 Palette.aEntries[i].peFlags=0x00;
204 Palette.aEntries[0].peRed=0x00;
205 Palette.aEntries[0].peBlue=0x00;
206 Palette.aEntries[0].peGreen=0x00;
208 /* the first 6 */
209 for (i=1; i <= 6; i++)
211 Palette.aEntries[i].peRed=(i%2)?0x80:0;
212 Palette.aEntries[i].peGreen=(i==2)?0x80:(i==3)?0x80:(i==6)?0x80:0;
213 Palette.aEntries[i].peBlue=(i>3)?0x80:0;
216 for (i=7; i <= 12; i++)
218 switch(i)
220 case 7:
221 Palette.aEntries[i].peRed=0xc0;
222 Palette.aEntries[i].peBlue=0xc0;
223 Palette.aEntries[i].peGreen=0xc0;
224 break;
225 case 8:
226 Palette.aEntries[i].peRed=0xc0;
227 Palette.aEntries[i].peGreen=0xdc;
228 Palette.aEntries[i].peBlue=0xc0;
229 break;
230 case 9:
231 Palette.aEntries[i].peRed=0xa6;
232 Palette.aEntries[i].peGreen=0xca;
233 Palette.aEntries[i].peBlue=0xf0;
234 break;
235 case 10:
236 Palette.aEntries[i].peRed=0xff;
237 Palette.aEntries[i].peGreen=0xfb;
238 Palette.aEntries[i].peBlue=0xf0;
239 break;
240 case 11:
241 Palette.aEntries[i].peRed=0xa0;
242 Palette.aEntries[i].peGreen=0xa0;
243 Palette.aEntries[i].peBlue=0xa4;
244 break;
245 case 12:
246 Palette.aEntries[i].peRed=0x80;
247 Palette.aEntries[i].peGreen=0x80;
248 Palette.aEntries[i].peBlue=0x80;
252 for (i=13; i <= 18; i++)
254 Palette.aEntries[i].peRed=(i%2)?0xff:0;
255 Palette.aEntries[i].peGreen=(i==14)?0xff:(i==15)?0xff:(i==18)?0xff:0;
256 Palette.aEntries[i].peBlue=(i>15)?0xff:0x00;
259 return CreatePalette((LOGPALETTE *)&Palette);
263 /***********************************************************************
264 * GetPaletteEntries [GDI32.@]
266 * Retrieves palette entries.
268 * RETURNS
269 * Success: Number of entries from logical palette
270 * Failure: 0
272 UINT WINAPI GetPaletteEntries(
273 HPALETTE hpalette, /* [in] Handle of logical palette */
274 UINT start, /* [in] First entry to receive */
275 UINT count, /* [in] Number of entries to receive */
276 LPPALETTEENTRY entries) /* [out] Address of array receiving entries */
278 PALETTEOBJ * palPtr;
279 UINT numEntries;
281 TRACE("hpal = %p, count=%i\n", hpalette, count );
283 palPtr = GDI_GetObjPtr( hpalette, OBJ_PAL );
284 if (!palPtr) return 0;
286 /* NOTE: not documented but test show this to be the case */
287 if (count == 0)
289 int rc = palPtr->logpalette.palNumEntries;
290 GDI_ReleaseObj( hpalette );
291 return rc;
294 numEntries = palPtr->logpalette.palNumEntries;
295 if (start+count > numEntries) count = numEntries - start;
296 if (entries)
298 if (start >= numEntries)
300 GDI_ReleaseObj( hpalette );
301 return 0;
303 memcpy( entries, &palPtr->logpalette.palPalEntry[start],
304 count * sizeof(PALETTEENTRY) );
307 GDI_ReleaseObj( hpalette );
308 return count;
312 /***********************************************************************
313 * SetPaletteEntries [GDI32.@]
315 * Sets color values for range in palette.
317 * RETURNS
318 * Success: Number of entries that were set
319 * Failure: 0
321 UINT WINAPI SetPaletteEntries(
322 HPALETTE hpalette, /* [in] Handle of logical palette */
323 UINT start, /* [in] Index of first entry to set */
324 UINT count, /* [in] Number of entries to set */
325 const PALETTEENTRY *entries) /* [in] Address of array of structures */
327 PALETTEOBJ * palPtr;
328 UINT numEntries;
330 TRACE("hpal=%p,start=%i,count=%i\n",hpalette,start,count );
332 if (hpalette == GetStockObject(DEFAULT_PALETTE)) return 0;
333 palPtr = GDI_GetObjPtr( hpalette, OBJ_PAL );
334 if (!palPtr) return 0;
336 numEntries = palPtr->logpalette.palNumEntries;
337 if (start >= numEntries)
339 GDI_ReleaseObj( hpalette );
340 return 0;
342 if (start+count > numEntries) count = numEntries - start;
343 memcpy( &palPtr->logpalette.palPalEntry[start], entries,
344 count * sizeof(PALETTEENTRY) );
345 UnrealizeObject( hpalette );
346 GDI_ReleaseObj( hpalette );
347 return count;
351 /***********************************************************************
352 * ResizePalette [GDI32.@]
354 * Resizes logical palette.
356 * RETURNS
357 * Success: TRUE
358 * Failure: FALSE
360 BOOL WINAPI ResizePalette(
361 HPALETTE hPal, /* [in] Handle of logical palette */
362 UINT cEntries) /* [in] Number of entries in logical palette */
364 PALETTEOBJ * palPtr = GDI_GetObjPtr( hPal, OBJ_PAL );
365 UINT cPrevEnt, prevVer;
366 int prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
368 TRACE("hpal = %p, prev = %i, new = %i\n",
369 hPal, palPtr ? palPtr->logpalette.palNumEntries : -1, cEntries );
370 if( !palPtr ) return FALSE;
371 cPrevEnt = palPtr->logpalette.palNumEntries;
372 prevVer = palPtr->logpalette.palVersion;
373 prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) +
374 sizeof(int*) + sizeof(GDIOBJHDR);
375 size += sizeof(int*) + sizeof(GDIOBJHDR);
377 if (!(palPtr = GDI_ReallocObject( size, hPal, palPtr ))) return FALSE;
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 PALETTE_UnrealizeObject( hPal );
384 return TRUE;
388 /***********************************************************************
389 * AnimatePalette [GDI32.@]
391 * Replaces entries in logical palette.
393 * RETURNS
394 * Success: TRUE
395 * Failure: FALSE
397 * FIXME
398 * Should use existing mapping when animating a primary palette
400 BOOL WINAPI AnimatePalette(
401 HPALETTE hPal, /* [in] Handle to logical palette */
402 UINT StartIndex, /* [in] First entry in palette */
403 UINT NumEntries, /* [in] Count of entries in palette */
404 const PALETTEENTRY* PaletteColors) /* [in] Pointer to first replacement */
406 TRACE("%p (%i - %i)\n", hPal, StartIndex,StartIndex+NumEntries);
408 if( hPal != GetStockObject(DEFAULT_PALETTE) )
410 PALETTEOBJ * palPtr;
411 UINT pal_entries;
412 const PALETTEENTRY *pptr = PaletteColors;
414 palPtr = GDI_GetObjPtr( hPal, OBJ_PAL );
415 if (!palPtr) return 0;
417 pal_entries = palPtr->logpalette.palNumEntries;
418 if (StartIndex >= pal_entries)
420 GDI_ReleaseObj( hPal );
421 return 0;
423 if (StartIndex+NumEntries > pal_entries) NumEntries = pal_entries - StartIndex;
425 for (NumEntries += StartIndex; StartIndex < NumEntries; StartIndex++, pptr++) {
426 /* According to MSDN, only animate PC_RESERVED colours */
427 if (palPtr->logpalette.palPalEntry[StartIndex].peFlags & PC_RESERVED) {
428 TRACE("Animating colour (%d,%d,%d) to (%d,%d,%d)\n",
429 palPtr->logpalette.palPalEntry[StartIndex].peRed,
430 palPtr->logpalette.palPalEntry[StartIndex].peGreen,
431 palPtr->logpalette.palPalEntry[StartIndex].peBlue,
432 pptr->peRed, pptr->peGreen, pptr->peBlue);
433 memcpy( &palPtr->logpalette.palPalEntry[StartIndex], pptr,
434 sizeof(PALETTEENTRY) );
435 } else {
436 TRACE("Not animating entry %d -- not PC_RESERVED\n", StartIndex);
439 if (palPtr->funcs && palPtr->funcs->pRealizePalette)
440 palPtr->funcs->pRealizePalette( NULL, hPal, hPal == hPrimaryPalette );
442 GDI_ReleaseObj( hPal );
444 return TRUE;
448 /***********************************************************************
449 * SetSystemPaletteUse [GDI32.@]
451 * Specify whether the system palette contains 2 or 20 static colors.
453 * RETURNS
454 * Success: Previous system palette
455 * Failure: SYSPAL_ERROR
457 UINT WINAPI SetSystemPaletteUse(
458 HDC hdc, /* [in] Handle of device context */
459 UINT use) /* [in] Palette-usage flag */
461 UINT old = SystemPaletteUse;
463 /* Device doesn't support colour palettes */
464 if (!(GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)) {
465 return SYSPAL_ERROR;
468 switch (use) {
469 case SYSPAL_NOSTATIC:
470 case SYSPAL_NOSTATIC256: /* WINVER >= 0x0500 */
471 case SYSPAL_STATIC:
472 SystemPaletteUse = use;
473 return old;
474 default:
475 return SYSPAL_ERROR;
480 /***********************************************************************
481 * GetSystemPaletteUse [GDI32.@]
483 * Gets state of system palette.
485 * RETURNS
486 * Current state of system palette
488 UINT WINAPI GetSystemPaletteUse(
489 HDC hdc) /* [in] Handle of device context */
491 return SystemPaletteUse;
495 /***********************************************************************
496 * GetSystemPaletteEntries [GDI32.@]
498 * Gets range of palette entries.
500 * RETURNS
501 * Success: Number of entries retrieved from palette
502 * Failure: 0
504 UINT WINAPI GetSystemPaletteEntries(
505 HDC hdc, /* [in] Handle of device context */
506 UINT start, /* [in] Index of first entry to be retrieved */
507 UINT count, /* [in] Number of entries to be retrieved */
508 LPPALETTEENTRY entries) /* [out] Array receiving system-palette entries */
510 UINT ret = 0;
511 DC *dc;
513 TRACE("hdc=%p,start=%i,count=%i\n", hdc,start,count);
515 if ((dc = get_dc_ptr( hdc )))
517 if (dc->funcs->pGetSystemPaletteEntries)
518 ret = dc->funcs->pGetSystemPaletteEntries( dc->physDev, start, count, entries );
519 release_dc_ptr( dc );
521 return ret;
525 /***********************************************************************
526 * GetNearestPaletteIndex [GDI32.@]
528 * Gets palette index for color.
530 * NOTES
531 * Should index be initialized to CLR_INVALID instead of 0?
533 * RETURNS
534 * Success: Index of entry in logical palette
535 * Failure: CLR_INVALID
537 UINT WINAPI GetNearestPaletteIndex(
538 HPALETTE hpalette, /* [in] Handle of logical color palette */
539 COLORREF color) /* [in] Color to be matched */
541 PALETTEOBJ* palObj = GDI_GetObjPtr( hpalette, OBJ_PAL );
542 UINT index = 0;
544 if( palObj )
546 int i, diff = 0x7fffffff;
547 int r,g,b;
548 PALETTEENTRY* entry = palObj->logpalette.palPalEntry;
550 for( i = 0; i < palObj->logpalette.palNumEntries && diff ; i++, entry++)
552 r = entry->peRed - GetRValue(color);
553 g = entry->peGreen - GetGValue(color);
554 b = entry->peBlue - GetBValue(color);
556 r = r*r + g*g + b*b;
558 if( r < diff ) { index = i; diff = r; }
560 GDI_ReleaseObj( hpalette );
562 TRACE("(%p,%06x): returning %d\n", hpalette, color, index );
563 return index;
567 /***********************************************************************
568 * GetNearestColor [GDI32.@]
570 * Gets a system color to match.
572 * RETURNS
573 * Success: Color from system palette that corresponds to given color
574 * Failure: CLR_INVALID
576 COLORREF WINAPI GetNearestColor(
577 HDC hdc, /* [in] Handle of device context */
578 COLORREF color) /* [in] Color to be matched */
580 unsigned char spec_type;
581 COLORREF nearest;
582 DC *dc;
584 if (!(dc = get_dc_ptr( hdc ))) return CLR_INVALID;
586 if (dc->funcs->pGetNearestColor)
588 nearest = dc->funcs->pGetNearestColor( dc->physDev, color );
589 release_dc_ptr( dc );
590 return nearest;
593 if (!(GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE))
595 release_dc_ptr( dc );
596 return color;
599 spec_type = color >> 24;
600 if (spec_type == 1 || spec_type == 2)
602 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
604 UINT index;
605 PALETTEENTRY entry;
606 HPALETTE hpal = dc->hPalette ? dc->hPalette : GetStockObject( DEFAULT_PALETTE );
608 if (spec_type == 2) /* PALETTERGB */
609 index = GetNearestPaletteIndex( hpal, color );
610 else /* PALETTEINDEX */
611 index = LOWORD(color);
613 if (!GetPaletteEntries( hpal, index, 1, &entry ))
615 WARN("RGB(%x) : idx %d is out of bounds, assuming NULL\n", color, index );
616 if (!GetPaletteEntries( hpal, 0, 1, &entry ))
618 release_dc_ptr( dc );
619 return CLR_INVALID;
622 color = RGB( entry.peRed, entry.peGreen, entry.peBlue );
624 nearest = color & 0x00ffffff;
625 release_dc_ptr( dc );
627 TRACE("(%06x): returning %06x\n", color, nearest );
628 return nearest;
632 /***********************************************************************
633 * PALETTE_GetObject
635 static INT PALETTE_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
637 PALETTEOBJ *palette = GDI_GetObjPtr( handle, OBJ_PAL );
639 if (!palette) return 0;
641 if (buffer)
643 if (count > sizeof(WORD)) count = sizeof(WORD);
644 memcpy( buffer, &palette->logpalette.palNumEntries, count );
646 else count = sizeof(WORD);
647 GDI_ReleaseObj( handle );
648 return count;
652 /***********************************************************************
653 * PALETTE_UnrealizeObject
655 static BOOL PALETTE_UnrealizeObject( HGDIOBJ handle )
657 PALETTEOBJ *palette = GDI_GetObjPtr( handle, OBJ_PAL );
659 if (palette)
661 const DC_FUNCTIONS *funcs = palette->funcs;
662 palette->funcs = NULL;
663 GDI_ReleaseObj( handle );
664 if (funcs && funcs->pUnrealizePalette) funcs->pUnrealizePalette( handle );
667 if (InterlockedCompareExchangePointer( (void **)&hLastRealizedPalette, 0, handle ) == handle)
668 TRACE("unrealizing palette %p\n", handle);
670 return TRUE;
674 /***********************************************************************
675 * PALETTE_DeleteObject
677 static BOOL PALETTE_DeleteObject( HGDIOBJ handle )
679 PALETTEOBJ *obj;
681 PALETTE_UnrealizeObject( handle );
682 if (!(obj = free_gdi_handle( handle ))) return FALSE;
683 return HeapFree( GetProcessHeap(), 0, obj );
687 /***********************************************************************
688 * GDISelectPalette (Not a Windows API)
690 HPALETTE WINAPI GDISelectPalette( HDC hdc, HPALETTE hpal, WORD wBkg)
692 HPALETTE ret;
693 DC *dc;
695 TRACE("%p %p\n", hdc, hpal );
697 if (GetObjectType(hpal) != OBJ_PAL)
699 WARN("invalid selected palette %p\n",hpal);
700 return 0;
702 if (!(dc = get_dc_ptr( hdc ))) return 0;
703 ret = dc->hPalette;
704 if (dc->funcs->pSelectPalette) hpal = dc->funcs->pSelectPalette( dc->physDev, hpal, FALSE );
705 if (hpal)
707 dc->hPalette = hpal;
708 if (!wBkg) hPrimaryPalette = hpal;
710 else ret = 0;
711 release_dc_ptr( dc );
712 return ret;
716 /***********************************************************************
717 * GDIRealizePalette (Not a Windows API)
719 UINT WINAPI GDIRealizePalette( HDC hdc )
721 UINT realized = 0;
722 DC* dc = get_dc_ptr( hdc );
724 if (!dc) return 0;
726 TRACE("%p...\n", hdc );
728 if( dc->hPalette == GetStockObject( DEFAULT_PALETTE ))
730 if (dc->funcs->pRealizeDefaultPalette)
731 realized = dc->funcs->pRealizeDefaultPalette( dc->physDev );
733 else if (InterlockedExchangePointer( (void **)&hLastRealizedPalette, dc->hPalette ) != dc->hPalette)
735 if (dc->funcs->pRealizePalette)
737 PALETTEOBJ *palPtr = GDI_GetObjPtr( dc->hPalette, OBJ_PAL );
738 if (palPtr)
740 realized = dc->funcs->pRealizePalette( dc->physDev, dc->hPalette,
741 (dc->hPalette == hPrimaryPalette) );
742 palPtr->funcs = dc->funcs;
743 GDI_ReleaseObj( dc->hPalette );
747 else TRACE(" skipping (hLastRealizedPalette = %p)\n", hLastRealizedPalette);
749 release_dc_ptr( dc );
750 TRACE(" realized %i colors.\n", realized );
751 return realized;
755 /***********************************************************************
756 * RealizeDefaultPalette (GDI.365)
758 UINT16 WINAPI RealizeDefaultPalette16( HDC16 hdc )
760 UINT16 ret = 0;
761 DC *dc;
763 TRACE("%04x\n", hdc );
765 if (!(dc = get_dc_ptr( HDC_32(hdc) ))) return 0;
767 if (dc->funcs->pRealizeDefaultPalette) ret = dc->funcs->pRealizeDefaultPalette( dc->physDev );
768 release_dc_ptr( dc );
769 return ret;
772 /***********************************************************************
773 * IsDCCurrentPalette (GDI.412)
775 BOOL16 WINAPI IsDCCurrentPalette16(HDC16 hDC)
777 DC *dc = get_dc_ptr( HDC_32(hDC) );
778 if (dc)
780 BOOL bRet = dc->hPalette == hPrimaryPalette;
781 release_dc_ptr( dc );
782 return bRet;
784 return FALSE;
788 /***********************************************************************
789 * SelectPalette [GDI32.@]
791 * Selects logical palette into DC.
793 * RETURNS
794 * Success: Previous logical palette
795 * Failure: NULL
797 HPALETTE WINAPI SelectPalette(
798 HDC hDC, /* [in] Handle of device context */
799 HPALETTE hPal, /* [in] Handle of logical color palette */
800 BOOL bForceBackground) /* [in] Foreground/background mode */
802 return pfnSelectPalette( hDC, hPal, bForceBackground );
806 /***********************************************************************
807 * RealizePalette [GDI32.@]
809 * Maps palette entries to system palette.
811 * RETURNS
812 * Success: Number of entries in logical palette
813 * Failure: GDI_ERROR
815 UINT WINAPI RealizePalette(
816 HDC hDC) /* [in] Handle of device context */
818 return pfnRealizePalette( hDC );
822 typedef HWND (WINAPI *WindowFromDC_funcptr)( HDC );
823 typedef BOOL (WINAPI *RedrawWindow_funcptr)( HWND, const RECT *, HRGN, UINT );
825 /**********************************************************************
826 * UpdateColors [GDI32.@]
828 * Remaps current colors to logical palette.
830 * RETURNS
831 * Success: TRUE
832 * Failure: FALSE
834 BOOL WINAPI UpdateColors(
835 HDC hDC) /* [in] Handle of device context */
837 HMODULE mod;
838 int size = GetDeviceCaps( hDC, SIZEPALETTE );
840 if (!size) return 0;
842 mod = GetModuleHandleA("user32.dll");
843 if (mod)
845 WindowFromDC_funcptr pWindowFromDC = (WindowFromDC_funcptr)GetProcAddress(mod,"WindowFromDC");
846 if (pWindowFromDC)
848 HWND hWnd = pWindowFromDC( hDC );
850 /* Docs say that we have to remap current drawable pixel by pixel
851 * but it would take forever given the speed of XGet/PutPixel.
853 if (hWnd && size)
855 RedrawWindow_funcptr pRedrawWindow = (void *)GetProcAddress( mod, "RedrawWindow" );
856 if (pRedrawWindow) pRedrawWindow( hWnd, NULL, 0, RDW_INVALIDATE );
860 return 0x666;
864 /*********************************************************************
865 * SetMagicColors (GDI.606)
867 VOID WINAPI SetMagicColors16(HDC16 hDC, COLORREF color, UINT16 index)
869 FIXME("(hDC %04x, color %04x, index %04x): stub\n", hDC, (int)color, index);
873 /*********************************************************************
874 * SetMagicColors (GDI32.@)
876 BOOL WINAPI SetMagicColors(HDC hdc, ULONG u1, ULONG u2)
878 FIXME("(%p 0x%08x 0x%08x): stub\n", hdc, u1, u2);
879 return TRUE;