Be more strict about checks (especially in RVA translations).
[wine.git] / objects / palette.c
blobc21e912fe5961ec6125e0fd557e3874997a63eeb
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 <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.h"
36 #include "gdi_private.h"
37 #include "palette.h"
38 #include "wine/debug.h"
39 #include "winerror.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(palette);
43 static INT PALETTE_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer );
44 static BOOL PALETTE_UnrealizeObject( HGDIOBJ handle, void *obj );
45 static BOOL PALETTE_DeleteObject( HGDIOBJ handle, void *obj );
47 static const struct gdi_obj_funcs palette_funcs =
49 NULL, /* pSelectObject */
50 PALETTE_GetObject, /* pGetObject16 */
51 PALETTE_GetObject, /* pGetObjectA */
52 PALETTE_GetObject, /* pGetObjectW */
53 PALETTE_UnrealizeObject, /* pUnrealizeObject */
54 PALETTE_DeleteObject /* pDeleteObject */
57 /* Pointers to USER implementation of SelectPalette/RealizePalette */
58 /* they will be patched by USER on startup */
59 HPALETTE (WINAPI *pfnSelectPalette)(HDC hdc, HPALETTE hpal, WORD bkgnd ) = GDISelectPalette;
60 UINT (WINAPI *pfnRealizePalette)(HDC hdc) = GDIRealizePalette;
62 static UINT SystemPaletteUse = SYSPAL_STATIC; /* currently not considered */
64 static HPALETTE hPrimaryPalette = 0; /* used for WM_PALETTECHANGED */
65 static HPALETTE hLastRealizedPalette = 0; /* UnrealizeObject() needs it */
66 static const DC_FUNCTIONS *pLastRealizedDC;
68 static const PALETTEENTRY sys_pal_template[NB_RESERVED_COLORS] =
70 /* first 10 entries in the system palette */
71 /* red green blue flags */
72 { 0x00, 0x00, 0x00, 0 },
73 { 0x80, 0x00, 0x00, 0 },
74 { 0x00, 0x80, 0x00, 0 },
75 { 0x80, 0x80, 0x00, 0 },
76 { 0x00, 0x00, 0x80, 0 },
77 { 0x80, 0x00, 0x80, 0 },
78 { 0x00, 0x80, 0x80, 0 },
79 { 0xc0, 0xc0, 0xc0, 0 },
80 { 0xc0, 0xdc, 0xc0, 0 },
81 { 0xa6, 0xca, 0xf0, 0 },
83 /* ... c_min/2 dynamic colorcells */
85 /* ... gap (for sparse palettes) */
87 /* ... c_min/2 dynamic colorcells */
89 { 0xff, 0xfb, 0xf0, 0 },
90 { 0xa0, 0xa0, 0xa4, 0 },
91 { 0x80, 0x80, 0x80, 0 },
92 { 0xff, 0x00, 0x00, 0 },
93 { 0x00, 0xff, 0x00, 0 },
94 { 0xff, 0xff, 0x00, 0 },
95 { 0x00, 0x00, 0xff, 0 },
96 { 0xff, 0x00, 0xff, 0 },
97 { 0x00, 0xff, 0xff, 0 },
98 { 0xff, 0xff, 0xff, 0 } /* last 10 */
101 /***********************************************************************
102 * PALETTE_Init
104 * Create the system palette.
106 HPALETTE PALETTE_Init(void)
108 HPALETTE hpalette;
109 LOGPALETTE * palPtr;
110 PALETTEOBJ* palObj;
112 /* create default palette (20 system colors) */
114 palPtr = HeapAlloc( GetProcessHeap(), 0,
115 sizeof(LOGPALETTE) + (NB_RESERVED_COLORS-1)*sizeof(PALETTEENTRY));
116 if (!palPtr) return FALSE;
118 palPtr->palVersion = 0x300;
119 palPtr->palNumEntries = NB_RESERVED_COLORS;
120 memcpy( palPtr->palPalEntry, sys_pal_template, sizeof(sys_pal_template) );
121 hpalette = CreatePalette( palPtr );
122 HeapFree( GetProcessHeap(), 0, palPtr );
124 palObj = (PALETTEOBJ*) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
125 if (palObj)
127 if (!(palObj->mapping = HeapAlloc( GetProcessHeap(), 0, sizeof(int) * NB_RESERVED_COLORS )))
128 ERR("Can not create palette mapping -- out of memory!\n");
129 GDI_ReleaseObj( hpalette );
131 return hpalette;
134 /***********************************************************************
135 * PALETTE_ValidateFlags
137 static void PALETTE_ValidateFlags(PALETTEENTRY* lpPalE, int size)
139 int i = 0;
140 for( ; i<size ; i++ )
141 lpPalE[i].peFlags = PC_SYS_USED | (lpPalE[i].peFlags & 0x07);
145 /***********************************************************************
146 * CreatePalette [GDI32.@] Creates a logical color palette
148 * RETURNS
149 * Success: Handle to logical palette
150 * Failure: NULL
152 HPALETTE WINAPI CreatePalette(
153 const LOGPALETTE* palette) /* [in] Pointer to logical color palette */
155 PALETTEOBJ * palettePtr;
156 HPALETTE hpalette;
157 int size;
159 if (!palette) return 0;
160 TRACE("entries=%i\n", palette->palNumEntries);
162 size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
164 if (!(palettePtr = GDI_AllocObject( size + sizeof(int*) +sizeof(GDIOBJHDR),
165 PALETTE_MAGIC, (HGDIOBJ *)&hpalette,
166 &palette_funcs ))) return 0;
167 memcpy( &palettePtr->logpalette, palette, size );
168 PALETTE_ValidateFlags(palettePtr->logpalette.palPalEntry,
169 palettePtr->logpalette.palNumEntries);
170 palettePtr->mapping = NULL;
171 GDI_ReleaseObj( hpalette );
173 TRACE(" returning %p\n", hpalette);
174 return hpalette;
178 /***********************************************************************
179 * CreateHalftonePalette [GDI32.@] Creates a halftone palette
181 * RETURNS
182 * Success: Handle to logical halftone palette
183 * Failure: 0
185 * FIXME: This simply creates the halftone palette dirived from runing
186 * tests on an windows NT machine. this is assuming a color depth
187 * of greater that 256 color. On a 256 color device the halftone
188 * palette will be differnt and this funtion will be incorrect
190 HPALETTE WINAPI CreateHalftonePalette(
191 HDC hdc) /* [in] Handle to device context */
193 int i;
194 struct {
195 WORD Version;
196 WORD NumberOfEntries;
197 PALETTEENTRY aEntries[256];
198 } Palette;
200 Palette.Version = 0x300;
201 Palette.NumberOfEntries = 256;
202 GetSystemPaletteEntries(hdc, 0, 256, Palette.aEntries);
204 Palette.NumberOfEntries = 20;
206 for (i = 0; i < Palette.NumberOfEntries; i++)
208 Palette.aEntries[i].peRed=0xff;
209 Palette.aEntries[i].peGreen=0xff;
210 Palette.aEntries[i].peBlue=0xff;
211 Palette.aEntries[i].peFlags=0x00;
214 Palette.aEntries[0].peRed=0x00;
215 Palette.aEntries[0].peBlue=0x00;
216 Palette.aEntries[0].peGreen=0x00;
218 /* the first 6 */
219 for (i=1; i <= 6; i++)
221 Palette.aEntries[i].peRed=(i%2)?0x80:0;
222 Palette.aEntries[i].peGreen=(i==2)?0x80:(i==3)?0x80:(i==6)?0x80:0;
223 Palette.aEntries[i].peBlue=(i>3)?0x80:0;
226 for (i=7; i <= 12; i++)
228 switch(i)
230 case 7:
231 Palette.aEntries[i].peRed=0xc0;
232 Palette.aEntries[i].peBlue=0xc0;
233 Palette.aEntries[i].peGreen=0xc0;
234 break;
235 case 8:
236 Palette.aEntries[i].peRed=0xc0;
237 Palette.aEntries[i].peGreen=0xdc;
238 Palette.aEntries[i].peBlue=0xc0;
239 break;
240 case 9:
241 Palette.aEntries[i].peRed=0xa6;
242 Palette.aEntries[i].peGreen=0xca;
243 Palette.aEntries[i].peBlue=0xf0;
244 break;
245 case 10:
246 Palette.aEntries[i].peRed=0xff;
247 Palette.aEntries[i].peGreen=0xfb;
248 Palette.aEntries[i].peBlue=0xf0;
249 break;
250 case 11:
251 Palette.aEntries[i].peRed=0xa0;
252 Palette.aEntries[i].peGreen=0xa0;
253 Palette.aEntries[i].peBlue=0xa4;
254 break;
255 case 12:
256 Palette.aEntries[i].peRed=0x80;
257 Palette.aEntries[i].peGreen=0x80;
258 Palette.aEntries[i].peBlue=0x80;
262 for (i=13; i <= 18; i++)
264 Palette.aEntries[i].peRed=(i%2)?0xff:0;
265 Palette.aEntries[i].peGreen=(i==14)?0xff:(i==15)?0xff:(i==18)?0xff:0;
266 Palette.aEntries[i].peBlue=(i>15)?0xff:0x00;
269 return CreatePalette((LOGPALETTE *)&Palette);
273 /***********************************************************************
274 * GetPaletteEntries [GDI32.@] Retrieves palette entries
276 * RETURNS
277 * Success: Number of entries from logical palette
278 * Failure: 0
280 UINT WINAPI GetPaletteEntries(
281 HPALETTE hpalette, /* [in] Handle of logical palette */
282 UINT start, /* [in] First entry to receive */
283 UINT count, /* [in] Number of entries to receive */
284 LPPALETTEENTRY entries) /* [out] Address of array receiving entries */
286 PALETTEOBJ * palPtr;
287 UINT numEntries;
289 TRACE("hpal = %p, count=%i\n", hpalette, count );
291 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
292 if (!palPtr) return 0;
294 /* NOTE: not documented but test show this to be the case */
295 if (count == 0)
297 int rc = palPtr->logpalette.palNumEntries;
298 GDI_ReleaseObj( hpalette );
299 return rc;
302 numEntries = palPtr->logpalette.palNumEntries;
303 if (start+count > numEntries) count = numEntries - start;
304 if (entries)
306 if (start >= numEntries)
308 GDI_ReleaseObj( hpalette );
309 return 0;
311 memcpy( entries, &palPtr->logpalette.palPalEntry[start],
312 count * sizeof(PALETTEENTRY) );
313 for( numEntries = 0; numEntries < count ; numEntries++ )
314 if (entries[numEntries].peFlags & 0xF0)
315 entries[numEntries].peFlags = 0;
318 GDI_ReleaseObj( hpalette );
319 return count;
323 /***********************************************************************
324 * SetPaletteEntries [GDI32.@] Sets color values for range in palette
326 * RETURNS
327 * Success: Number of entries that were set
328 * Failure: 0
330 UINT WINAPI SetPaletteEntries(
331 HPALETTE hpalette, /* [in] Handle of logical palette */
332 UINT start, /* [in] Index of first entry to set */
333 UINT count, /* [in] Number of entries to set */
334 const PALETTEENTRY *entries) /* [in] Address of array of structures */
336 PALETTEOBJ * palPtr;
337 UINT numEntries;
339 TRACE("hpal=%p,start=%i,count=%i\n",hpalette,start,count );
341 if (hpalette == GetStockObject(DEFAULT_PALETTE)) return 0;
342 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
343 if (!palPtr) return 0;
345 numEntries = palPtr->logpalette.palNumEntries;
346 if (start >= numEntries)
348 GDI_ReleaseObj( hpalette );
349 return 0;
351 if (start+count > numEntries) count = numEntries - start;
352 memcpy( &palPtr->logpalette.palPalEntry[start], entries,
353 count * sizeof(PALETTEENTRY) );
354 PALETTE_ValidateFlags(palPtr->logpalette.palPalEntry,
355 palPtr->logpalette.palNumEntries);
356 UnrealizeObject( hpalette );
357 GDI_ReleaseObj( hpalette );
358 return count;
362 /***********************************************************************
363 * ResizePalette [GDI32.@] Resizes logical palette
365 * RETURNS
366 * Success: TRUE
367 * Failure: FALSE
369 BOOL WINAPI ResizePalette(
370 HPALETTE hPal, /* [in] Handle of logical palette */
371 UINT cEntries) /* [in] Number of entries in logical palette */
373 PALETTEOBJ * palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
374 UINT cPrevEnt, prevVer;
375 int prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
376 int* mapping = NULL;
378 TRACE("hpal = %p, prev = %i, new = %i\n",
379 hPal, palPtr ? palPtr->logpalette.palNumEntries : -1, cEntries );
380 if( !palPtr ) return FALSE;
381 cPrevEnt = palPtr->logpalette.palNumEntries;
382 prevVer = palPtr->logpalette.palVersion;
383 prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) +
384 sizeof(int*) + sizeof(GDIOBJHDR);
385 size += sizeof(int*) + sizeof(GDIOBJHDR);
386 mapping = palPtr->mapping;
388 if (!(palPtr = GDI_ReallocObject( size, hPal, palPtr ))) return FALSE;
390 if( mapping )
392 int *newMap = (int*) HeapReAlloc(GetProcessHeap(), 0,
393 mapping, cEntries * sizeof(int) );
394 if(newMap == NULL)
396 ERR("Can not resize mapping -- out of memory!\n");
397 GDI_ReleaseObj( hPal );
398 return FALSE;
400 palPtr->mapping = newMap;
403 if( cEntries > cPrevEnt )
405 if( mapping )
406 memset(palPtr->mapping + cPrevEnt, 0, (cEntries - cPrevEnt)*sizeof(int));
407 memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
408 PALETTE_ValidateFlags((PALETTEENTRY*)((BYTE*)palPtr + prevsize),
409 cEntries - cPrevEnt );
411 palPtr->logpalette.palNumEntries = cEntries;
412 palPtr->logpalette.palVersion = prevVer;
413 GDI_ReleaseObj( hPal );
414 return TRUE;
418 /***********************************************************************
419 * AnimatePalette [GDI32.@] Replaces entries in logical palette
421 * RETURNS
422 * Success: TRUE
423 * Failure: FALSE
425 * FIXME
426 * Should use existing mapping when animating a primary palette
428 BOOL WINAPI AnimatePalette(
429 HPALETTE hPal, /* [in] Handle to logical palette */
430 UINT StartIndex, /* [in] First entry in palette */
431 UINT NumEntries, /* [in] Count of entries in palette */
432 const PALETTEENTRY* PaletteColors) /* [in] Pointer to first replacement */
434 TRACE("%p (%i - %i)\n", hPal, StartIndex,StartIndex+NumEntries);
436 if( hPal != GetStockObject(DEFAULT_PALETTE) )
438 if (!SetPaletteEntries( hPal, StartIndex, NumEntries, PaletteColors )) return FALSE;
440 if (pLastRealizedDC && pLastRealizedDC->pRealizePalette)
441 pLastRealizedDC->pRealizePalette( NULL, hPal, hPal == hPrimaryPalette );
443 return TRUE;
447 /***********************************************************************
448 * SetSystemPaletteUse [GDI32.@]
450 * RETURNS
451 * Success: Previous system palette
452 * Failure: SYSPAL_ERROR
454 UINT WINAPI SetSystemPaletteUse(
455 HDC hdc, /* [in] Handle of device context */
456 UINT use) /* [in] Palette-usage flag */
458 UINT old = SystemPaletteUse;
460 /* Device doesn't support colour palettes */
461 if (!(GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)) {
462 return SYSPAL_ERROR;
465 switch (use) {
466 case SYSPAL_NOSTATIC:
467 case SYSPAL_NOSTATIC256: /* WINVER >= 0x0500 */
468 case SYSPAL_STATIC:
469 SystemPaletteUse = use;
470 return old;
471 default:
472 return SYSPAL_ERROR;
477 /***********************************************************************
478 * GetSystemPaletteUse [GDI32.@] Gets state of system palette
480 * RETURNS
481 * Current state of system palette
483 UINT WINAPI GetSystemPaletteUse(
484 HDC hdc) /* [in] Handle of device context */
486 return SystemPaletteUse;
490 /***********************************************************************
491 * GetSystemPaletteEntries [GDI32.@] Gets range of palette entries
493 * RETURNS
494 * Success: Number of entries retrieved from palette
495 * Failure: 0
497 UINT WINAPI GetSystemPaletteEntries(
498 HDC hdc, /* [in] Handle of device context */
499 UINT start, /* [in] Index of first entry to be retrieved */
500 UINT count, /* [in] Number of entries to be retrieved */
501 LPPALETTEENTRY entries) /* [out] Array receiving system-palette entries */
503 UINT ret = 0;
504 DC *dc;
506 TRACE("hdc=%p,start=%i,count=%i\n", hdc,start,count);
508 if ((dc = DC_GetDCPtr( hdc )))
510 if (dc->funcs->pGetSystemPaletteEntries)
511 ret = dc->funcs->pGetSystemPaletteEntries( dc->physDev, start, count, entries );
512 GDI_ReleaseObj( hdc );
514 return ret;
518 /***********************************************************************
519 * GetNearestPaletteIndex [GDI32.@] Gets palette index for color
521 * NOTES
522 * Should index be initialized to CLR_INVALID instead of 0?
524 * RETURNS
525 * Success: Index of entry in logical palette
526 * Failure: CLR_INVALID
528 UINT WINAPI GetNearestPaletteIndex(
529 HPALETTE hpalette, /* [in] Handle of logical color palette */
530 COLORREF color) /* [in] Color to be matched */
532 PALETTEOBJ* palObj = (PALETTEOBJ*)GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
533 UINT index = 0;
535 if( palObj )
537 int i, diff = 0x7fffffff;
538 int r,g,b;
539 PALETTEENTRY* entry = palObj->logpalette.palPalEntry;
541 for( i = 0; i < palObj->logpalette.palNumEntries && diff ; i++, entry++)
543 if (!(entry->peFlags & PC_SYS_USED)) continue;
545 r = entry->peRed - GetRValue(color);
546 g = entry->peGreen - GetGValue(color);
547 b = entry->peBlue - GetBValue(color);
549 r = r*r + g*g + b*b;
551 if( r < diff ) { index = i; diff = r; }
553 GDI_ReleaseObj( hpalette );
555 TRACE("(%p,%06lx): returning %d\n", hpalette, color, index );
556 return index;
560 /***********************************************************************
561 * GetNearestColor [GDI32.@] Gets a system color to match
563 * RETURNS
564 * Success: Color from system palette that corresponds to given color
565 * Failure: CLR_INVALID
567 COLORREF WINAPI GetNearestColor(
568 HDC hdc, /* [in] Handle of device context */
569 COLORREF color) /* [in] Color to be matched */
571 unsigned char spec_type;
572 COLORREF nearest;
573 DC *dc;
575 if (!(dc = DC_GetDCPtr( hdc ))) return CLR_INVALID;
577 if (dc->funcs->pGetNearestColor)
579 nearest = dc->funcs->pGetNearestColor( dc->physDev, color );
580 GDI_ReleaseObj( hdc );
581 return nearest;
584 if (!(GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE))
586 GDI_ReleaseObj( hdc );
587 return color;
590 spec_type = color >> 24;
591 if (spec_type == 1 || spec_type == 2)
593 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
595 UINT index;
596 PALETTEENTRY entry;
597 HPALETTE hpal = dc->hPalette ? dc->hPalette : GetStockObject( DEFAULT_PALETTE );
599 if (spec_type == 2) /* PALETTERGB */
600 index = GetNearestPaletteIndex( hpal, color );
601 else /* PALETTEINDEX */
602 index = LOWORD(color);
604 if (!GetPaletteEntries( hpal, index, 1, &entry ))
606 WARN("RGB(%lx) : idx %d is out of bounds, assuming NULL\n", color, index );
607 if (!GetPaletteEntries( hpal, 0, 1, &entry ))
609 GDI_ReleaseObj( hdc );
610 return CLR_INVALID;
613 color = RGB( entry.peRed, entry.peGreen, entry.peBlue );
615 nearest = color & 0x00ffffff;
616 GDI_ReleaseObj( hdc );
618 TRACE("(%06lx): returning %06lx\n", color, nearest );
619 return nearest;
623 /***********************************************************************
624 * PALETTE_GetObject
626 static INT PALETTE_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
628 PALETTEOBJ *palette = obj;
630 if( !buffer )
631 return sizeof(WORD);
633 if (count > sizeof(WORD)) count = sizeof(WORD);
634 memcpy( buffer, &palette->logpalette.palNumEntries, count );
635 return count;
639 /***********************************************************************
640 * PALETTE_UnrealizeObject
642 static BOOL PALETTE_UnrealizeObject( HGDIOBJ handle, void *obj )
644 PALETTEOBJ *palette = obj;
646 if (palette->mapping)
648 HeapFree( GetProcessHeap(), 0, palette->mapping );
649 palette->mapping = NULL;
651 if (hLastRealizedPalette == handle)
653 hLastRealizedPalette = 0;
654 pLastRealizedDC = NULL;
656 return TRUE;
660 /***********************************************************************
661 * PALETTE_DeleteObject
663 static BOOL PALETTE_DeleteObject( HGDIOBJ handle, void *obj )
665 PALETTEOBJ *palette = obj;
667 HeapFree( GetProcessHeap(), 0, palette->mapping );
668 if (hLastRealizedPalette == handle)
670 hLastRealizedPalette = 0;
671 pLastRealizedDC = NULL;
673 return GDI_FreeObject( handle, obj );
677 /***********************************************************************
678 * GDISelectPalette (Not a Windows API)
680 HPALETTE WINAPI GDISelectPalette( HDC hdc, HPALETTE hpal, WORD wBkg)
682 HPALETTE ret;
683 DC *dc;
685 TRACE("%p %p\n", hdc, hpal );
687 if (GetObjectType(hpal) != OBJ_PAL)
689 WARN("invalid selected palette %p\n",hpal);
690 return 0;
692 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
693 ret = dc->hPalette;
694 if (dc->funcs->pSelectPalette) hpal = dc->funcs->pSelectPalette( dc->physDev, hpal, FALSE );
695 if (hpal)
697 dc->hPalette = hpal;
698 if (!wBkg) hPrimaryPalette = hpal;
700 else ret = 0;
701 GDI_ReleaseObj( hdc );
702 return ret;
706 /***********************************************************************
707 * GDIRealizePalette (Not a Windows API)
709 UINT WINAPI GDIRealizePalette( HDC hdc )
711 UINT realized = 0;
712 DC* dc = DC_GetDCPtr( hdc );
714 if (!dc) return 0;
716 TRACE("%p...\n", hdc );
718 if( dc->hPalette == GetStockObject( DEFAULT_PALETTE ))
720 if (dc->funcs->pRealizeDefaultPalette)
721 realized = dc->funcs->pRealizeDefaultPalette( dc->physDev );
723 else if(dc->hPalette != hLastRealizedPalette )
725 if (dc->funcs->pRealizePalette)
726 realized = dc->funcs->pRealizePalette( dc->physDev, dc->hPalette,
727 (dc->hPalette == hPrimaryPalette) );
728 hLastRealizedPalette = dc->hPalette;
729 pLastRealizedDC = dc->funcs;
731 else TRACE(" skipping (hLastRealizedPalette = %p)\n", hLastRealizedPalette);
733 GDI_ReleaseObj( hdc );
734 TRACE(" realized %i colors.\n", realized );
735 return realized;
739 /***********************************************************************
740 * RealizeDefaultPalette (GDI.365)
742 UINT16 WINAPI RealizeDefaultPalette16( HDC16 hdc )
744 UINT16 ret = 0;
745 DC *dc;
747 TRACE("%04x\n", hdc );
749 if (!(dc = DC_GetDCPtr( HDC_32(hdc) ))) return 0;
751 if (dc->funcs->pRealizeDefaultPalette) ret = dc->funcs->pRealizeDefaultPalette( dc->physDev );
752 GDI_ReleaseObj( HDC_32(hdc) );
753 return ret;
756 /***********************************************************************
757 * IsDCCurrentPalette (GDI.412)
759 BOOL16 WINAPI IsDCCurrentPalette16(HDC16 hDC)
761 DC *dc = DC_GetDCPtr( HDC_32(hDC) );
762 if (dc)
764 BOOL bRet = dc->hPalette == hPrimaryPalette;
765 GDI_ReleaseObj( HDC_32(hDC) );
766 return bRet;
768 return FALSE;
772 /***********************************************************************
773 * SelectPalette [GDI32.@] Selects logical palette into DC
775 * RETURNS
776 * Success: Previous logical palette
777 * Failure: NULL
779 HPALETTE WINAPI SelectPalette(
780 HDC hDC, /* [in] Handle of device context */
781 HPALETTE hPal, /* [in] Handle of logical color palette */
782 BOOL bForceBackground) /* [in] Foreground/background mode */
784 return pfnSelectPalette( hDC, hPal, bForceBackground );
788 /***********************************************************************
789 * RealizePalette [GDI32.@] Maps palette entries to system palette
791 * RETURNS
792 * Success: Number of entries in logical palette
793 * Failure: GDI_ERROR
795 UINT WINAPI RealizePalette(
796 HDC hDC) /* [in] Handle of device context */
798 return pfnRealizePalette( hDC );
802 typedef HWND (WINAPI *WindowFromDC_funcptr)( HDC );
803 typedef BOOL (WINAPI *RedrawWindow_funcptr)( HWND, const RECT *, HRGN, UINT );
805 /**********************************************************************
806 * UpdateColors [GDI32.@] Remaps current colors to logical palette
808 * RETURNS
809 * Success: TRUE
810 * Failure: FALSE
812 BOOL WINAPI UpdateColors(
813 HDC hDC) /* [in] Handle of device context */
815 HMODULE mod;
816 int size = GetDeviceCaps( hDC, SIZEPALETTE );
818 if (!size) return 0;
820 mod = GetModuleHandleA("user32.dll");
821 if (mod)
823 WindowFromDC_funcptr pWindowFromDC = (WindowFromDC_funcptr)GetProcAddress(mod,"WindowFromDC");
824 if (pWindowFromDC)
826 HWND hWnd = pWindowFromDC( hDC );
828 /* Docs say that we have to remap current drawable pixel by pixel
829 * but it would take forever given the speed of XGet/PutPixel.
831 if (hWnd && size)
833 RedrawWindow_funcptr pRedrawWindow = GetProcAddress( mod, "RedrawWindow" );
834 if (pRedrawWindow) pRedrawWindow( hWnd, NULL, 0, RDW_INVALIDATE );
838 return 0x666;
842 /*********************************************************************
843 * SetMagicColors (GDI.606)
845 VOID WINAPI SetMagicColors16(HDC16 hDC, COLORREF color, UINT16 index)
847 FIXME("(hDC %04x, color %04x, index %04x): stub\n", hDC, (int)color, index);
851 /**********************************************************************
852 * GetICMProfileA [GDI32.@]
854 * Returns the filename of the specified device context's color
855 * management profile, even if color management is not enabled
856 * for that DC.
858 * RETURNS
859 * TRUE if name copied successfully OR lpszFilename is NULL
860 * FALSE if the buffer length pointed to by lpcbName is too small
862 * NOTE
863 * The buffer length pointed to by lpcbName is ALWAYS updated to
864 * the length required regardless of other actions this function
865 * may take.
867 * FIXME
868 * How does Windows assign these? Some registry key?
871 #define WINEICM "winefake.icm" /* easy-to-identify fake filename */
873 /*********************************************************************/
875 BOOL WINAPI GetICMProfileA(HDC hDC, LPDWORD lpcbName, LPSTR lpszFilename)
877 DWORD callerLen;
879 FIXME("(%p, %p, %p): partial stub\n", hDC, lpcbName, lpszFilename);
881 callerLen = *lpcbName;
883 /* all 3 behaviors require the required buffer size to be set */
884 *lpcbName = strlen(WINEICM);
886 /* behavior 1: if lpszFilename is NULL, return size of string and no error */
887 if ((DWORD)lpszFilename == (DWORD)0x00000000)
888 return TRUE;
890 /* behavior 2: if buffer size too small, return size of string and error */
891 if (callerLen < strlen(WINEICM))
893 SetLastError(ERROR_INSUFFICIENT_BUFFER);
894 return FALSE;
897 /* behavior 3: if buffer size OK and pointer not NULL, copy and return size */
898 strcpy(lpszFilename, WINEICM);
899 return TRUE;