Make the test pass on NT4 too.
[wine/multimedia.git] / objects / palette.c
blobb4ab4d49091c026a28bfcdd30a07c6e9bdce7695
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * NOTES:
22 * PALETTEOBJ is documented in the Dr. Dobbs Journal May 1993.
23 * Information in the "Undocumented Windows" is incorrect.
26 #include <stdlib.h>
27 #include <string.h>
29 #include "winbase.h"
30 #include "windef.h"
31 #include "wingdi.h"
32 #include "wownt32.h"
33 #include "wine/winuser16.h"
34 #include "gdi.h"
35 #include "palette.h"
36 #include "wine/debug.h"
37 #include "winerror.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(palette);
41 static INT PALETTE_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer );
42 static BOOL PALETTE_UnrealizeObject( HGDIOBJ handle, void *obj );
43 static BOOL PALETTE_DeleteObject( HGDIOBJ handle, void *obj );
45 static const struct gdi_obj_funcs palette_funcs =
47 NULL, /* pSelectObject */
48 PALETTE_GetObject, /* pGetObject16 */
49 PALETTE_GetObject, /* pGetObjectA */
50 PALETTE_GetObject, /* pGetObjectW */
51 PALETTE_UnrealizeObject, /* pUnrealizeObject */
52 PALETTE_DeleteObject /* pDeleteObject */
55 /* Pointers to USER implementation of SelectPalette/RealizePalette */
56 /* they will be patched by USER on startup */
57 HPALETTE (WINAPI *pfnSelectPalette)(HDC hdc, HPALETTE hpal, WORD bkgnd ) = GDISelectPalette;
58 UINT (WINAPI *pfnRealizePalette)(HDC hdc) = GDIRealizePalette;
60 static UINT SystemPaletteUse = SYSPAL_STATIC; /* currently not considered */
62 static HPALETTE hPrimaryPalette = 0; /* used for WM_PALETTECHANGED */
63 static HPALETTE hLastRealizedPalette = 0; /* UnrealizeObject() needs it */
64 static const DC_FUNCTIONS *pLastRealizedDC;
66 static const PALETTEENTRY sys_pal_template[NB_RESERVED_COLORS] =
68 /* first 10 entries in the system palette */
69 /* red green blue flags */
70 { 0x00, 0x00, 0x00, 0 },
71 { 0x80, 0x00, 0x00, 0 },
72 { 0x00, 0x80, 0x00, 0 },
73 { 0x80, 0x80, 0x00, 0 },
74 { 0x00, 0x00, 0x80, 0 },
75 { 0x80, 0x00, 0x80, 0 },
76 { 0x00, 0x80, 0x80, 0 },
77 { 0xc0, 0xc0, 0xc0, 0 },
78 { 0xc0, 0xdc, 0xc0, 0 },
79 { 0xa6, 0xca, 0xf0, 0 },
81 /* ... c_min/2 dynamic colorcells */
83 /* ... gap (for sparse palettes) */
85 /* ... c_min/2 dynamic colorcells */
87 { 0xff, 0xfb, 0xf0, 0 },
88 { 0xa0, 0xa0, 0xa4, 0 },
89 { 0x80, 0x80, 0x80, 0 },
90 { 0xff, 0x00, 0x00, 0 },
91 { 0x00, 0xff, 0x00, 0 },
92 { 0xff, 0xff, 0x00, 0 },
93 { 0x00, 0x00, 0xff, 0 },
94 { 0xff, 0x00, 0xff, 0 },
95 { 0x00, 0xff, 0xff, 0 },
96 { 0xff, 0xff, 0xff, 0 } /* last 10 */
99 /***********************************************************************
100 * PALETTE_Init
102 * Create the system palette.
104 HPALETTE PALETTE_Init(void)
106 HPALETTE hpalette;
107 LOGPALETTE * palPtr;
108 PALETTEOBJ* palObj;
110 /* create default palette (20 system colors) */
112 palPtr = HeapAlloc( GetProcessHeap(), 0,
113 sizeof(LOGPALETTE) + (NB_RESERVED_COLORS-1)*sizeof(PALETTEENTRY));
114 if (!palPtr) return FALSE;
116 palPtr->palVersion = 0x300;
117 palPtr->palNumEntries = NB_RESERVED_COLORS;
118 memcpy( palPtr->palPalEntry, sys_pal_template, sizeof(sys_pal_template) );
119 hpalette = CreatePalette( palPtr );
120 HeapFree( GetProcessHeap(), 0, palPtr );
122 palObj = (PALETTEOBJ*) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
123 if (palObj)
125 if (!(palObj->mapping = HeapAlloc( GetProcessHeap(), 0, sizeof(int) * NB_RESERVED_COLORS )))
126 ERR("Can not create palette mapping -- out of memory!\n");
127 GDI_ReleaseObj( hpalette );
129 return hpalette;
132 /***********************************************************************
133 * PALETTE_ValidateFlags
135 static void PALETTE_ValidateFlags(PALETTEENTRY* lpPalE, int size)
137 int i = 0;
138 for( ; i<size ; i++ )
139 lpPalE[i].peFlags = PC_SYS_USED | (lpPalE[i].peFlags & 0x07);
143 /***********************************************************************
144 * CreatePalette [GDI32.@] Creates a logical color palette
146 * RETURNS
147 * Success: Handle to logical palette
148 * Failure: NULL
150 HPALETTE WINAPI CreatePalette(
151 const LOGPALETTE* palette) /* [in] Pointer to logical color palette */
153 PALETTEOBJ * palettePtr;
154 HPALETTE hpalette;
155 int size;
157 if (!palette) return 0;
158 TRACE("entries=%i\n", palette->palNumEntries);
160 size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
162 if (!(palettePtr = GDI_AllocObject( size + sizeof(int*) +sizeof(GDIOBJHDR),
163 PALETTE_MAGIC, (HGDIOBJ *)&hpalette,
164 &palette_funcs ))) return 0;
165 memcpy( &palettePtr->logpalette, palette, size );
166 PALETTE_ValidateFlags(palettePtr->logpalette.palPalEntry,
167 palettePtr->logpalette.palNumEntries);
168 palettePtr->mapping = NULL;
169 GDI_ReleaseObj( hpalette );
171 TRACE(" returning %p\n", hpalette);
172 return hpalette;
176 /***********************************************************************
177 * CreateHalftonePalette [GDI32.@] Creates a halftone palette
179 * RETURNS
180 * Success: Handle to logical halftone palette
181 * Failure: 0
183 * FIXME: This simply creates the halftone palette dirived from runing
184 * tests on an windows NT machine. this is assuming a color depth
185 * of greater that 256 color. On a 256 color device the halftone
186 * palette will be differnt and this funtion will be incorrect
188 HPALETTE WINAPI CreateHalftonePalette(
189 HDC hdc) /* [in] Handle to device context */
191 int i;
192 struct {
193 WORD Version;
194 WORD NumberOfEntries;
195 PALETTEENTRY aEntries[256];
196 } Palette;
198 Palette.Version = 0x300;
199 Palette.NumberOfEntries = 256;
200 GetSystemPaletteEntries(hdc, 0, 256, Palette.aEntries);
202 Palette.NumberOfEntries = 20;
204 for (i = 0; i < Palette.NumberOfEntries; i++)
206 Palette.aEntries[i].peRed=0xff;
207 Palette.aEntries[i].peGreen=0xff;
208 Palette.aEntries[i].peBlue=0xff;
209 Palette.aEntries[i].peFlags=0x00;
212 Palette.aEntries[0].peRed=0x00;
213 Palette.aEntries[0].peBlue=0x00;
214 Palette.aEntries[0].peGreen=0x00;
216 /* the first 6 */
217 for (i=1; i <= 6; i++)
219 Palette.aEntries[i].peRed=(i%2)?0x80:0;
220 Palette.aEntries[i].peGreen=(i==2)?0x80:(i==3)?0x80:(i==6)?0x80:0;
221 Palette.aEntries[i].peBlue=(i>3)?0x80:0;
224 for (i=7; i <= 12; i++)
226 switch(i)
228 case 7:
229 Palette.aEntries[i].peRed=0xc0;
230 Palette.aEntries[i].peBlue=0xc0;
231 Palette.aEntries[i].peGreen=0xc0;
232 break;
233 case 8:
234 Palette.aEntries[i].peRed=0xc0;
235 Palette.aEntries[i].peGreen=0xdc;
236 Palette.aEntries[i].peBlue=0xc0;
237 break;
238 case 9:
239 Palette.aEntries[i].peRed=0xa6;
240 Palette.aEntries[i].peGreen=0xca;
241 Palette.aEntries[i].peBlue=0xf0;
242 break;
243 case 10:
244 Palette.aEntries[i].peRed=0xff;
245 Palette.aEntries[i].peGreen=0xfb;
246 Palette.aEntries[i].peBlue=0xf0;
247 break;
248 case 11:
249 Palette.aEntries[i].peRed=0xa0;
250 Palette.aEntries[i].peGreen=0xa0;
251 Palette.aEntries[i].peBlue=0xa4;
252 break;
253 case 12:
254 Palette.aEntries[i].peRed=0x80;
255 Palette.aEntries[i].peGreen=0x80;
256 Palette.aEntries[i].peBlue=0x80;
260 for (i=13; i <= 18; i++)
262 Palette.aEntries[i].peRed=(i%2)?0xff:0;
263 Palette.aEntries[i].peGreen=(i==14)?0xff:(i==15)?0xff:(i==18)?0xff:0;
264 Palette.aEntries[i].peBlue=(i>15)?0xff:0x00;
267 return CreatePalette((LOGPALETTE *)&Palette);
271 /***********************************************************************
272 * GetPaletteEntries [GDI32.@] Retrieves palette entries
274 * RETURNS
275 * Success: Number of entries from logical palette
276 * Failure: 0
278 UINT WINAPI GetPaletteEntries(
279 HPALETTE hpalette, /* [in] Handle of logical palette */
280 UINT start, /* [in] First entry to receive */
281 UINT count, /* [in] Number of entries to receive */
282 LPPALETTEENTRY entries) /* [out] Address of array receiving entries */
284 PALETTEOBJ * palPtr;
285 UINT numEntries;
287 TRACE("hpal = %p, count=%i\n", hpalette, count );
289 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
290 if (!palPtr) return 0;
292 /* NOTE: not documented but test show this to be the case */
293 if (count == 0)
295 int rc = palPtr->logpalette.palNumEntries;
296 GDI_ReleaseObj( hpalette );
297 return rc;
300 numEntries = palPtr->logpalette.palNumEntries;
301 if (start+count > numEntries) count = numEntries - start;
302 if (entries)
304 if (start >= numEntries)
306 GDI_ReleaseObj( hpalette );
307 return 0;
309 memcpy( entries, &palPtr->logpalette.palPalEntry[start],
310 count * sizeof(PALETTEENTRY) );
311 for( numEntries = 0; numEntries < count ; numEntries++ )
312 if (entries[numEntries].peFlags & 0xF0)
313 entries[numEntries].peFlags = 0;
316 GDI_ReleaseObj( hpalette );
317 return count;
321 /***********************************************************************
322 * SetPaletteEntries [GDI32.@] Sets color values for range in palette
324 * RETURNS
325 * Success: Number of entries that were set
326 * Failure: 0
328 UINT WINAPI SetPaletteEntries(
329 HPALETTE hpalette, /* [in] Handle of logical palette */
330 UINT start, /* [in] Index of first entry to set */
331 UINT count, /* [in] Number of entries to set */
332 const PALETTEENTRY *entries) /* [in] Address of array of structures */
334 PALETTEOBJ * palPtr;
335 UINT numEntries;
337 TRACE("hpal=%p,start=%i,count=%i\n",hpalette,start,count );
339 if (hpalette == GetStockObject(DEFAULT_PALETTE)) return 0;
340 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
341 if (!palPtr) return 0;
343 numEntries = palPtr->logpalette.palNumEntries;
344 if (start >= numEntries)
346 GDI_ReleaseObj( hpalette );
347 return 0;
349 if (start+count > numEntries) count = numEntries - start;
350 memcpy( &palPtr->logpalette.palPalEntry[start], entries,
351 count * sizeof(PALETTEENTRY) );
352 PALETTE_ValidateFlags(palPtr->logpalette.palPalEntry,
353 palPtr->logpalette.palNumEntries);
354 UnrealizeObject( hpalette );
355 GDI_ReleaseObj( hpalette );
356 return count;
360 /***********************************************************************
361 * ResizePalette [GDI32.@] Resizes logical palette
363 * RETURNS
364 * Success: TRUE
365 * Failure: FALSE
367 BOOL WINAPI ResizePalette(
368 HPALETTE hPal, /* [in] Handle of logical palette */
369 UINT cEntries) /* [in] Number of entries in logical palette */
371 PALETTEOBJ * palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
372 UINT cPrevEnt, prevVer;
373 int prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
374 int* mapping = NULL;
376 TRACE("hpal = %p, prev = %i, new = %i\n",
377 hPal, palPtr ? palPtr->logpalette.palNumEntries : -1, cEntries );
378 if( !palPtr ) return FALSE;
379 cPrevEnt = palPtr->logpalette.palNumEntries;
380 prevVer = palPtr->logpalette.palVersion;
381 prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) +
382 sizeof(int*) + sizeof(GDIOBJHDR);
383 size += sizeof(int*) + sizeof(GDIOBJHDR);
384 mapping = palPtr->mapping;
386 if (!(palPtr = GDI_ReallocObject( size, hPal, palPtr ))) return FALSE;
388 if( mapping )
390 int *newMap = (int*) HeapReAlloc(GetProcessHeap(), 0,
391 mapping, cEntries * sizeof(int) );
392 if(newMap == NULL)
394 ERR("Can not resize mapping -- out of memory!\n");
395 GDI_ReleaseObj( hPal );
396 return FALSE;
398 palPtr->mapping = newMap;
401 if( cEntries > cPrevEnt )
403 if( mapping )
404 memset(palPtr->mapping + cPrevEnt, 0, (cEntries - cPrevEnt)*sizeof(int));
405 memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
406 PALETTE_ValidateFlags((PALETTEENTRY*)((BYTE*)palPtr + prevsize),
407 cEntries - cPrevEnt );
409 palPtr->logpalette.palNumEntries = cEntries;
410 palPtr->logpalette.palVersion = prevVer;
411 GDI_ReleaseObj( hPal );
412 return TRUE;
416 /***********************************************************************
417 * AnimatePalette [GDI32.@] Replaces entries in logical palette
419 * RETURNS
420 * Success: TRUE
421 * Failure: FALSE
423 * FIXME
424 * Should use existing mapping when animating a primary palette
426 BOOL WINAPI AnimatePalette(
427 HPALETTE hPal, /* [in] Handle to logical palette */
428 UINT StartIndex, /* [in] First entry in palette */
429 UINT NumEntries, /* [in] Count of entries in palette */
430 const PALETTEENTRY* PaletteColors) /* [in] Pointer to first replacement */
432 TRACE("%p (%i - %i)\n", hPal, StartIndex,StartIndex+NumEntries);
434 if( hPal != GetStockObject(DEFAULT_PALETTE) )
436 if (!SetPaletteEntries( hPal, StartIndex, NumEntries, PaletteColors )) return FALSE;
438 if (pLastRealizedDC && pLastRealizedDC->pRealizePalette)
439 pLastRealizedDC->pRealizePalette( NULL, hPal, hPal == hPrimaryPalette );
441 return TRUE;
445 /***********************************************************************
446 * SetSystemPaletteUse [GDI32.@]
448 * RETURNS
449 * Success: Previous system palette
450 * Failure: SYSPAL_ERROR
452 UINT WINAPI SetSystemPaletteUse(
453 HDC hdc, /* [in] Handle of device context */
454 UINT use) /* [in] Palette-usage flag */
456 UINT old = SystemPaletteUse;
458 /* Device doesn't support colour palettes */
459 if (!(GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)) {
460 return SYSPAL_ERROR;
463 switch (use) {
464 case SYSPAL_NOSTATIC:
465 case SYSPAL_NOSTATIC256: /* WINVER >= 0x0500 */
466 case SYSPAL_STATIC:
467 SystemPaletteUse = use;
468 return old;
469 default:
470 return SYSPAL_ERROR;
475 /***********************************************************************
476 * GetSystemPaletteUse [GDI32.@] Gets state of system palette
478 * RETURNS
479 * Current state of system palette
481 UINT WINAPI GetSystemPaletteUse(
482 HDC hdc) /* [in] Handle of device context */
484 return SystemPaletteUse;
488 /***********************************************************************
489 * GetSystemPaletteEntries [GDI32.@] Gets range of palette entries
491 * RETURNS
492 * Success: Number of entries retrieved from palette
493 * Failure: 0
495 UINT WINAPI GetSystemPaletteEntries(
496 HDC hdc, /* [in] Handle of device context */
497 UINT start, /* [in] Index of first entry to be retrieved */
498 UINT count, /* [in] Number of entries to be retrieved */
499 LPPALETTEENTRY entries) /* [out] Array receiving system-palette entries */
501 UINT ret = 0;
502 DC *dc;
504 TRACE("hdc=%p,start=%i,count=%i\n", hdc,start,count);
506 if ((dc = DC_GetDCPtr( hdc )))
508 if (dc->funcs->pGetSystemPaletteEntries)
509 ret = dc->funcs->pGetSystemPaletteEntries( dc->physDev, start, count, entries );
510 GDI_ReleaseObj( hdc );
512 return ret;
516 /***********************************************************************
517 * GetNearestPaletteIndex [GDI32.@] Gets palette index for color
519 * NOTES
520 * Should index be initialized to CLR_INVALID instead of 0?
522 * RETURNS
523 * Success: Index of entry in logical palette
524 * Failure: CLR_INVALID
526 UINT WINAPI GetNearestPaletteIndex(
527 HPALETTE hpalette, /* [in] Handle of logical color palette */
528 COLORREF color) /* [in] Color to be matched */
530 PALETTEOBJ* palObj = (PALETTEOBJ*)GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
531 UINT index = 0;
533 if( palObj )
535 int i, diff = 0x7fffffff;
536 int r,g,b;
537 PALETTEENTRY* entry = palObj->logpalette.palPalEntry;
539 for( i = 0; i < palObj->logpalette.palNumEntries && diff ; i++, entry++)
541 if (!(entry->peFlags & PC_SYS_USED)) continue;
543 r = entry->peRed - GetRValue(color);
544 g = entry->peGreen - GetGValue(color);
545 b = entry->peBlue - GetBValue(color);
547 r = r*r + g*g + b*b;
549 if( r < diff ) { index = i; diff = r; }
551 GDI_ReleaseObj( hpalette );
553 TRACE("(%p,%06lx): returning %d\n", hpalette, color, index );
554 return index;
558 /***********************************************************************
559 * GetNearestColor [GDI32.@] Gets a system color to match
561 * RETURNS
562 * Success: Color from system palette that corresponds to given color
563 * Failure: CLR_INVALID
565 COLORREF WINAPI GetNearestColor(
566 HDC hdc, /* [in] Handle of device context */
567 COLORREF color) /* [in] Color to be matched */
569 unsigned char spec_type;
570 COLORREF nearest;
571 DC *dc;
573 if (!(dc = DC_GetDCPtr( hdc ))) return CLR_INVALID;
575 if (dc->funcs->pGetNearestColor)
577 nearest = dc->funcs->pGetNearestColor( dc->physDev, color );
578 GDI_ReleaseObj( hdc );
579 return nearest;
582 if (!(GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE))
584 GDI_ReleaseObj( hdc );
585 return color;
588 spec_type = color >> 24;
589 if (spec_type == 1 || spec_type == 2)
591 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
593 UINT index;
594 PALETTEENTRY entry;
595 HPALETTE hpal = dc->hPalette ? dc->hPalette : GetStockObject( DEFAULT_PALETTE );
597 if (spec_type == 2) /* PALETTERGB */
598 index = GetNearestPaletteIndex( hpal, color );
599 else /* PALETTEINDEX */
600 index = LOWORD(color);
602 if (!GetPaletteEntries( hpal, index, 1, &entry ))
604 WARN("RGB(%lx) : idx %d is out of bounds, assuming NULL\n", color, index );
605 if (!GetPaletteEntries( hpal, 0, 1, &entry ))
607 GDI_ReleaseObj( hdc );
608 return CLR_INVALID;
611 color = RGB( entry.peRed, entry.peGreen, entry.peBlue );
613 nearest = color & 0x00ffffff;
614 GDI_ReleaseObj( hdc );
616 TRACE("(%06lx): returning %06lx\n", color, nearest );
617 return nearest;
621 /***********************************************************************
622 * PALETTE_GetObject
624 static INT PALETTE_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
626 PALETTEOBJ *palette = obj;
628 if (count > sizeof(WORD)) count = sizeof(WORD);
629 memcpy( buffer, &palette->logpalette.palNumEntries, count );
630 return count;
634 /***********************************************************************
635 * PALETTE_UnrealizeObject
637 static BOOL PALETTE_UnrealizeObject( HGDIOBJ handle, void *obj )
639 PALETTEOBJ *palette = obj;
641 if (palette->mapping)
643 HeapFree( GetProcessHeap(), 0, palette->mapping );
644 palette->mapping = NULL;
646 if (hLastRealizedPalette == handle)
648 hLastRealizedPalette = 0;
649 pLastRealizedDC = NULL;
651 return TRUE;
655 /***********************************************************************
656 * PALETTE_DeleteObject
658 static BOOL PALETTE_DeleteObject( HGDIOBJ handle, void *obj )
660 PALETTEOBJ *palette = obj;
662 HeapFree( GetProcessHeap(), 0, palette->mapping );
663 if (hLastRealizedPalette == handle)
665 hLastRealizedPalette = 0;
666 pLastRealizedDC = NULL;
668 return GDI_FreeObject( handle, obj );
672 /***********************************************************************
673 * GDISelectPalette (Not a Windows API)
675 HPALETTE WINAPI GDISelectPalette( HDC hdc, HPALETTE hpal, WORD wBkg)
677 HPALETTE prev;
678 DC *dc;
680 TRACE("%p %p\n", hdc, hpal );
682 if (GetObjectType(hpal) != OBJ_PAL)
684 WARN("invalid selected palette %p\n",hpal);
685 return 0;
687 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
688 prev = dc->hPalette;
689 dc->hPalette = hpal;
690 GDI_ReleaseObj( hdc );
691 if (!wBkg) hPrimaryPalette = hpal;
692 return prev;
696 /***********************************************************************
697 * GDIRealizePalette (Not a Windows API)
699 UINT WINAPI GDIRealizePalette( HDC hdc )
701 UINT realized = 0;
702 DC* dc = DC_GetDCPtr( hdc );
704 if (!dc) return 0;
706 TRACE("%p...\n", hdc );
708 if( dc->hPalette == GetStockObject( DEFAULT_PALETTE ))
710 if (dc->funcs->pRealizeDefaultPalette)
711 realized = dc->funcs->pRealizeDefaultPalette( dc->physDev );
713 else if(dc->hPalette != hLastRealizedPalette )
715 if (dc->funcs->pRealizePalette)
716 realized = dc->funcs->pRealizePalette( dc->physDev, dc->hPalette,
717 (dc->hPalette == hPrimaryPalette) );
718 hLastRealizedPalette = dc->hPalette;
719 pLastRealizedDC = dc->funcs;
721 else TRACE(" skipping (hLastRealizedPalette = %p)\n", hLastRealizedPalette);
723 GDI_ReleaseObj( hdc );
724 TRACE(" realized %i colors.\n", realized );
725 return realized;
729 /***********************************************************************
730 * RealizeDefaultPalette (GDI.365)
732 UINT16 WINAPI RealizeDefaultPalette16( HDC16 hdc )
734 UINT16 ret = 0;
735 DC *dc;
737 TRACE("%04x\n", hdc );
739 if (!(dc = DC_GetDCPtr( HDC_32(hdc) ))) return 0;
741 if (dc->funcs->pRealizeDefaultPalette) ret = dc->funcs->pRealizeDefaultPalette( dc->physDev );
742 GDI_ReleaseObj( HDC_32(hdc) );
743 return ret;
746 /***********************************************************************
747 * IsDCCurrentPalette (GDI.412)
749 BOOL16 WINAPI IsDCCurrentPalette16(HDC16 hDC)
751 DC *dc = DC_GetDCPtr( HDC_32(hDC) );
752 if (dc)
754 BOOL bRet = dc->hPalette == hPrimaryPalette;
755 GDI_ReleaseObj( HDC_32(hDC) );
756 return bRet;
758 return FALSE;
762 /***********************************************************************
763 * SelectPalette [GDI32.@] Selects logical palette into DC
765 * RETURNS
766 * Success: Previous logical palette
767 * Failure: NULL
769 HPALETTE WINAPI SelectPalette(
770 HDC hDC, /* [in] Handle of device context */
771 HPALETTE hPal, /* [in] Handle of logical color palette */
772 BOOL bForceBackground) /* [in] Foreground/background mode */
774 return pfnSelectPalette( hDC, hPal, bForceBackground );
778 /***********************************************************************
779 * RealizePalette [GDI32.@] Maps palette entries to system palette
781 * RETURNS
782 * Success: Number of entries in logical palette
783 * Failure: GDI_ERROR
785 UINT WINAPI RealizePalette(
786 HDC hDC) /* [in] Handle of device context */
788 return pfnRealizePalette( hDC );
792 typedef HWND (WINAPI *WindowFromDC_funcptr)( HDC );
793 typedef BOOL (WINAPI *RedrawWindow_funcptr)( HWND, const RECT *, HRGN, UINT );
795 /**********************************************************************
796 * UpdateColors [GDI32.@] Remaps current colors to logical palette
798 * RETURNS
799 * Success: TRUE
800 * Failure: FALSE
802 BOOL WINAPI UpdateColors(
803 HDC hDC) /* [in] Handle of device context */
805 HMODULE mod;
806 int size = GetDeviceCaps( hDC, SIZEPALETTE );
808 if (!size) return 0;
810 mod = GetModuleHandleA("user32.dll");
811 if (mod)
813 WindowFromDC_funcptr pWindowFromDC = (WindowFromDC_funcptr)GetProcAddress(mod,"WindowFromDC");
814 if (pWindowFromDC)
816 HWND hWnd = pWindowFromDC( hDC );
818 /* Docs say that we have to remap current drawable pixel by pixel
819 * but it would take forever given the speed of XGet/PutPixel.
821 if (hWnd && size)
823 RedrawWindow_funcptr pRedrawWindow = GetProcAddress( mod, "RedrawWindow" );
824 if (pRedrawWindow) pRedrawWindow( hWnd, NULL, 0, RDW_INVALIDATE );
828 return 0x666;
832 /*********************************************************************
833 * SetMagicColors (GDI.606)
835 VOID WINAPI SetMagicColors16(HDC16 hDC, COLORREF color, UINT16 index)
837 FIXME("(hDC %04x, color %04x, index %04x): stub\n", hDC, (int)color, index);
841 /**********************************************************************
842 * GetICMProfileA [GDI32.@]
844 * Returns the filename of the specified device context's color
845 * management profile, even if color management is not enabled
846 * for that DC.
848 * RETURNS
849 * TRUE if name copied succesfully OR lpszFilename is NULL
850 * FALSE if the buffer length pointed to by lpcbName is too small
852 * NOTE
853 * The buffer length pointed to by lpcbName is ALWAYS updated to
854 * the length required regardless of other actions this function
855 * may take.
857 * FIXME
858 * How does Windows assign these? Some registry key?
861 #define WINEICM "winefake.icm" /* easy-to-identify fake filename */
863 /*********************************************************************/
865 BOOL WINAPI GetICMProfileA(HDC hDC, LPDWORD lpcbName, LPSTR lpszFilename)
867 DWORD callerLen;
869 FIXME("(%p, %p, %p): partial stub\n", hDC, lpcbName, lpszFilename);
871 callerLen = *lpcbName;
873 /* all 3 behaviors require the required buffer size to be set */
874 *lpcbName = strlen(WINEICM);
876 /* behavior 1: if lpszFilename is NULL, return size of string and no error */
877 if ((DWORD)lpszFilename == (DWORD)0x00000000)
878 return TRUE;
880 /* behavior 2: if buffer size too small, return size of string and error */
881 if (callerLen < strlen(WINEICM))
883 SetLastError(ERROR_INSUFFICIENT_BUFFER);
884 return FALSE;
887 /* behavior 3: if buffer size OK and pointer not NULL, copy and return size */
888 strcpy(lpszFilename, WINEICM);
889 return TRUE;