Removed windows version checks from GetPrinterDriverDirectory tests.
[wine.git] / objects / palette.c
blob4dd2df1108915879ce648fb20061422e4ffea003
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;
457 FIXME("(%p,%04x): stub\n", hdc, use );
458 SystemPaletteUse = use;
459 return old;
463 /***********************************************************************
464 * GetSystemPaletteUse [GDI32.@] Gets state of system palette
466 * RETURNS
467 * Current state of system palette
469 UINT WINAPI GetSystemPaletteUse(
470 HDC hdc) /* [in] Handle of device context */
472 return SystemPaletteUse;
476 /***********************************************************************
477 * GetSystemPaletteEntries [GDI32.@] Gets range of palette entries
479 * RETURNS
480 * Success: Number of entries retrieved from palette
481 * Failure: 0
483 UINT WINAPI GetSystemPaletteEntries(
484 HDC hdc, /* [in] Handle of device context */
485 UINT start, /* [in] Index of first entry to be retrieved */
486 UINT count, /* [in] Number of entries to be retrieved */
487 LPPALETTEENTRY entries) /* [out] Array receiving system-palette entries */
489 UINT ret = 0;
490 DC *dc;
492 TRACE("hdc=%p,start=%i,count=%i\n", hdc,start,count);
494 if ((dc = DC_GetDCPtr( hdc )))
496 if (dc->funcs->pGetSystemPaletteEntries)
497 ret = dc->funcs->pGetSystemPaletteEntries( dc->physDev, start, count, entries );
498 GDI_ReleaseObj( hdc );
500 return ret;
504 /***********************************************************************
505 * GetNearestPaletteIndex [GDI32.@] Gets palette index for color
507 * NOTES
508 * Should index be initialized to CLR_INVALID instead of 0?
510 * RETURNS
511 * Success: Index of entry in logical palette
512 * Failure: CLR_INVALID
514 UINT WINAPI GetNearestPaletteIndex(
515 HPALETTE hpalette, /* [in] Handle of logical color palette */
516 COLORREF color) /* [in] Color to be matched */
518 PALETTEOBJ* palObj = (PALETTEOBJ*)GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
519 UINT index = 0;
521 if( palObj )
523 int i, diff = 0x7fffffff;
524 int r,g,b;
525 PALETTEENTRY* entry = palObj->logpalette.palPalEntry;
527 for( i = 0; i < palObj->logpalette.palNumEntries && diff ; i++, entry++)
529 if (!(entry->peFlags & PC_SYS_USED)) continue;
531 r = entry->peRed - GetRValue(color);
532 g = entry->peGreen - GetGValue(color);
533 b = entry->peBlue - GetBValue(color);
535 r = r*r + g*g + b*b;
537 if( r < diff ) { index = i; diff = r; }
539 GDI_ReleaseObj( hpalette );
541 TRACE("(%p,%06lx): returning %d\n", hpalette, color, index );
542 return index;
546 /***********************************************************************
547 * GetNearestColor [GDI32.@] Gets a system color to match
549 * RETURNS
550 * Success: Color from system palette that corresponds to given color
551 * Failure: CLR_INVALID
553 COLORREF WINAPI GetNearestColor(
554 HDC hdc, /* [in] Handle of device context */
555 COLORREF color) /* [in] Color to be matched */
557 unsigned char spec_type;
558 COLORREF nearest;
559 DC *dc;
561 if (!(dc = DC_GetDCPtr( hdc ))) return CLR_INVALID;
563 if (dc->funcs->pGetNearestColor)
565 nearest = dc->funcs->pGetNearestColor( dc->physDev, color );
566 GDI_ReleaseObj( hdc );
567 return nearest;
570 if (!(GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE))
572 GDI_ReleaseObj( hdc );
573 return color;
576 spec_type = color >> 24;
577 if (spec_type == 1 || spec_type == 2)
579 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
581 UINT index;
582 PALETTEENTRY entry;
583 HPALETTE hpal = dc->hPalette ? dc->hPalette : GetStockObject( DEFAULT_PALETTE );
585 if (spec_type == 2) /* PALETTERGB */
586 index = GetNearestPaletteIndex( hpal, color );
587 else /* PALETTEINDEX */
588 index = LOWORD(color);
590 if (!GetPaletteEntries( hpal, index, 1, &entry ))
592 WARN("RGB(%lx) : idx %d is out of bounds, assuming NULL\n", color, index );
593 if (!GetPaletteEntries( hpal, 0, 1, &entry ))
595 GDI_ReleaseObj( hdc );
596 return CLR_INVALID;
599 color = RGB( entry.peRed, entry.peGreen, entry.peBlue );
601 nearest = color & 0x00ffffff;
602 GDI_ReleaseObj( hdc );
604 TRACE("(%06lx): returning %06lx\n", color, nearest );
605 return nearest;
609 /***********************************************************************
610 * PALETTE_GetObject
612 static INT PALETTE_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
614 PALETTEOBJ *palette = obj;
616 if (count > sizeof(WORD)) count = sizeof(WORD);
617 memcpy( buffer, &palette->logpalette.palNumEntries, count );
618 return count;
622 /***********************************************************************
623 * PALETTE_UnrealizeObject
625 static BOOL PALETTE_UnrealizeObject( HGDIOBJ handle, void *obj )
627 PALETTEOBJ *palette = obj;
629 if (palette->mapping)
631 HeapFree( GetProcessHeap(), 0, palette->mapping );
632 palette->mapping = NULL;
634 if (hLastRealizedPalette == handle)
636 hLastRealizedPalette = 0;
637 pLastRealizedDC = NULL;
639 return TRUE;
643 /***********************************************************************
644 * PALETTE_DeleteObject
646 static BOOL PALETTE_DeleteObject( HGDIOBJ handle, void *obj )
648 PALETTEOBJ *palette = obj;
650 HeapFree( GetProcessHeap(), 0, palette->mapping );
651 if (hLastRealizedPalette == handle)
653 hLastRealizedPalette = 0;
654 pLastRealizedDC = NULL;
656 return GDI_FreeObject( handle, obj );
660 /***********************************************************************
661 * GDISelectPalette (Not a Windows API)
663 HPALETTE WINAPI GDISelectPalette( HDC hdc, HPALETTE hpal, WORD wBkg)
665 HPALETTE prev;
666 DC *dc;
668 TRACE("%p %p\n", hdc, hpal );
670 if (GetObjectType(hpal) != OBJ_PAL)
672 WARN("invalid selected palette %p\n",hpal);
673 return 0;
675 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
676 prev = dc->hPalette;
677 dc->hPalette = hpal;
678 GDI_ReleaseObj( hdc );
679 if (!wBkg) hPrimaryPalette = hpal;
680 return prev;
684 /***********************************************************************
685 * GDIRealizePalette (Not a Windows API)
687 UINT WINAPI GDIRealizePalette( HDC hdc )
689 UINT realized = 0;
690 DC* dc = DC_GetDCPtr( hdc );
692 if (!dc) return 0;
694 TRACE("%p...\n", hdc );
696 if( dc->hPalette == GetStockObject( DEFAULT_PALETTE ))
698 if (dc->funcs->pRealizeDefaultPalette)
699 realized = dc->funcs->pRealizeDefaultPalette( dc->physDev );
701 else if(dc->hPalette != hLastRealizedPalette )
703 if (dc->funcs->pRealizePalette)
704 realized = dc->funcs->pRealizePalette( dc->physDev, dc->hPalette,
705 (dc->hPalette == hPrimaryPalette) );
706 hLastRealizedPalette = dc->hPalette;
707 pLastRealizedDC = dc->funcs;
709 else TRACE(" skipping (hLastRealizedPalette = %p)\n", hLastRealizedPalette);
711 GDI_ReleaseObj( hdc );
712 TRACE(" realized %i colors.\n", realized );
713 return realized;
717 /***********************************************************************
718 * RealizeDefaultPalette (GDI.365)
720 UINT16 WINAPI RealizeDefaultPalette16( HDC16 hdc )
722 UINT16 ret = 0;
723 DC *dc;
725 TRACE("%04x\n", hdc );
727 if (!(dc = DC_GetDCPtr( HDC_32(hdc) ))) return 0;
729 if (dc->funcs->pRealizeDefaultPalette) ret = dc->funcs->pRealizeDefaultPalette( dc->physDev );
730 GDI_ReleaseObj( HDC_32(hdc) );
731 return ret;
734 /***********************************************************************
735 * IsDCCurrentPalette (GDI.412)
737 BOOL16 WINAPI IsDCCurrentPalette16(HDC16 hDC)
739 DC *dc = DC_GetDCPtr( HDC_32(hDC) );
740 if (dc)
742 BOOL bRet = dc->hPalette == hPrimaryPalette;
743 GDI_ReleaseObj( HDC_32(hDC) );
744 return bRet;
746 return FALSE;
750 /***********************************************************************
751 * SelectPalette [GDI32.@] Selects logical palette into DC
753 * RETURNS
754 * Success: Previous logical palette
755 * Failure: NULL
757 HPALETTE WINAPI SelectPalette(
758 HDC hDC, /* [in] Handle of device context */
759 HPALETTE hPal, /* [in] Handle of logical color palette */
760 BOOL bForceBackground) /* [in] Foreground/background mode */
762 return pfnSelectPalette( hDC, hPal, bForceBackground );
766 /***********************************************************************
767 * RealizePalette [GDI32.@] Maps palette entries to system palette
769 * RETURNS
770 * Success: Number of entries in logical palette
771 * Failure: GDI_ERROR
773 UINT WINAPI RealizePalette(
774 HDC hDC) /* [in] Handle of device context */
776 return pfnRealizePalette( hDC );
780 typedef HWND (WINAPI *WindowFromDC_funcptr)( HDC );
781 typedef BOOL (WINAPI *RedrawWindow_funcptr)( HWND, const RECT *, HRGN, UINT );
783 /**********************************************************************
784 * UpdateColors [GDI32.@] Remaps current colors to logical palette
786 * RETURNS
787 * Success: TRUE
788 * Failure: FALSE
790 BOOL WINAPI UpdateColors(
791 HDC hDC) /* [in] Handle of device context */
793 HMODULE mod;
794 int size = GetDeviceCaps( hDC, SIZEPALETTE );
796 if (!size) return 0;
798 mod = GetModuleHandleA("user32.dll");
799 if (mod)
801 WindowFromDC_funcptr pWindowFromDC = (WindowFromDC_funcptr)GetProcAddress(mod,"WindowFromDC");
802 if (pWindowFromDC)
804 HWND hWnd = pWindowFromDC( hDC );
806 /* Docs say that we have to remap current drawable pixel by pixel
807 * but it would take forever given the speed of XGet/PutPixel.
809 if (hWnd && size)
811 RedrawWindow_funcptr pRedrawWindow = GetProcAddress( mod, "RedrawWindow" );
812 if (pRedrawWindow) pRedrawWindow( hWnd, NULL, 0, RDW_INVALIDATE );
816 return 0x666;
820 /*********************************************************************
821 * SetMagicColors (GDI.606)
823 VOID WINAPI SetMagicColors16(HDC16 hDC, COLORREF color, UINT16 index)
825 FIXME("(hDC %04x, color %04x, index %04x): stub\n", hDC, (int)color, index);
829 /**********************************************************************
830 * GetICMProfileA [GDI32.@]
832 * Returns the filename of the specified device context's color
833 * management profile, even if color management is not enabled
834 * for that DC.
836 * RETURNS
837 * TRUE if name copied succesfully OR lpszFilename is NULL
838 * FALSE if the buffer length pointed to by lpcbName is too small
840 * NOTE
841 * The buffer length pointed to by lpcbName is ALWAYS updated to
842 * the length required regardless of other actions this function
843 * may take.
845 * FIXME
846 * How does Windows assign these? Some registry key?
849 #define WINEICM "winefake.icm" /* easy-to-identify fake filename */
851 /*********************************************************************/
853 BOOL WINAPI GetICMProfileA(HDC hDC, LPDWORD lpcbName, LPSTR lpszFilename)
855 DWORD callerLen;
857 FIXME("(%p, %p, %p): partial stub\n", hDC, lpcbName, lpszFilename);
859 callerLen = *lpcbName;
861 /* all 3 behaviors require the required buffer size to be set */
862 *lpcbName = strlen(WINEICM);
864 /* behavior 1: if lpszFilename is NULL, return size of string and no error */
865 if ((DWORD)lpszFilename == (DWORD)0x00000000)
866 return TRUE;
868 /* behavior 2: if buffer size too small, return size of string and error */
869 if (callerLen < strlen(WINEICM))
871 SetLastError(ERROR_INSUFFICIENT_BUFFER);
872 return FALSE;
875 /* behavior 3: if buffer size OK and pointer not NULL, copy and return size */
876 strcpy(lpszFilename, WINEICM);
877 return TRUE;