- fixed ELF modules' size information
[wine.git] / objects / palette.c
blobdfca7611ea8a24f934e0c90a1a6a755599945779
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.@]
148 * Creates a logical color palette.
150 * RETURNS
151 * Success: Handle to logical palette
152 * Failure: NULL
154 HPALETTE WINAPI CreatePalette(
155 const LOGPALETTE* palette) /* [in] Pointer to logical color palette */
157 PALETTEOBJ * palettePtr;
158 HPALETTE hpalette;
159 int size;
161 if (!palette) return 0;
162 TRACE("entries=%i\n", palette->palNumEntries);
164 size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
166 if (!(palettePtr = GDI_AllocObject( size + sizeof(int*) +sizeof(GDIOBJHDR),
167 PALETTE_MAGIC, (HGDIOBJ *)&hpalette,
168 &palette_funcs ))) return 0;
169 memcpy( &palettePtr->logpalette, palette, size );
170 PALETTE_ValidateFlags(palettePtr->logpalette.palPalEntry,
171 palettePtr->logpalette.palNumEntries);
172 palettePtr->mapping = NULL;
173 GDI_ReleaseObj( hpalette );
175 TRACE(" returning %p\n", hpalette);
176 return hpalette;
180 /***********************************************************************
181 * CreateHalftonePalette [GDI32.@]
183 * Creates a halftone palette.
185 * RETURNS
186 * Success: Handle to logical halftone palette
187 * Failure: 0
189 * FIXME: This simply creates the halftone palette derived from running
190 * tests on an windows NT machine. this is assuming a color depth
191 * of greater that 256 color. On a 256 color device the halftone
192 * palette will be different and this function will be incorrect
194 HPALETTE WINAPI CreateHalftonePalette(
195 HDC hdc) /* [in] Handle to device context */
197 int i;
198 struct {
199 WORD Version;
200 WORD NumberOfEntries;
201 PALETTEENTRY aEntries[256];
202 } Palette;
204 Palette.Version = 0x300;
205 Palette.NumberOfEntries = 256;
206 GetSystemPaletteEntries(hdc, 0, 256, Palette.aEntries);
208 Palette.NumberOfEntries = 20;
210 for (i = 0; i < Palette.NumberOfEntries; i++)
212 Palette.aEntries[i].peRed=0xff;
213 Palette.aEntries[i].peGreen=0xff;
214 Palette.aEntries[i].peBlue=0xff;
215 Palette.aEntries[i].peFlags=0x00;
218 Palette.aEntries[0].peRed=0x00;
219 Palette.aEntries[0].peBlue=0x00;
220 Palette.aEntries[0].peGreen=0x00;
222 /* the first 6 */
223 for (i=1; i <= 6; i++)
225 Palette.aEntries[i].peRed=(i%2)?0x80:0;
226 Palette.aEntries[i].peGreen=(i==2)?0x80:(i==3)?0x80:(i==6)?0x80:0;
227 Palette.aEntries[i].peBlue=(i>3)?0x80:0;
230 for (i=7; i <= 12; i++)
232 switch(i)
234 case 7:
235 Palette.aEntries[i].peRed=0xc0;
236 Palette.aEntries[i].peBlue=0xc0;
237 Palette.aEntries[i].peGreen=0xc0;
238 break;
239 case 8:
240 Palette.aEntries[i].peRed=0xc0;
241 Palette.aEntries[i].peGreen=0xdc;
242 Palette.aEntries[i].peBlue=0xc0;
243 break;
244 case 9:
245 Palette.aEntries[i].peRed=0xa6;
246 Palette.aEntries[i].peGreen=0xca;
247 Palette.aEntries[i].peBlue=0xf0;
248 break;
249 case 10:
250 Palette.aEntries[i].peRed=0xff;
251 Palette.aEntries[i].peGreen=0xfb;
252 Palette.aEntries[i].peBlue=0xf0;
253 break;
254 case 11:
255 Palette.aEntries[i].peRed=0xa0;
256 Palette.aEntries[i].peGreen=0xa0;
257 Palette.aEntries[i].peBlue=0xa4;
258 break;
259 case 12:
260 Palette.aEntries[i].peRed=0x80;
261 Palette.aEntries[i].peGreen=0x80;
262 Palette.aEntries[i].peBlue=0x80;
266 for (i=13; i <= 18; i++)
268 Palette.aEntries[i].peRed=(i%2)?0xff:0;
269 Palette.aEntries[i].peGreen=(i==14)?0xff:(i==15)?0xff:(i==18)?0xff:0;
270 Palette.aEntries[i].peBlue=(i>15)?0xff:0x00;
273 return CreatePalette((LOGPALETTE *)&Palette);
277 /***********************************************************************
278 * GetPaletteEntries [GDI32.@]
280 * Retrieves palette entries.
282 * RETURNS
283 * Success: Number of entries from logical palette
284 * Failure: 0
286 UINT WINAPI GetPaletteEntries(
287 HPALETTE hpalette, /* [in] Handle of logical palette */
288 UINT start, /* [in] First entry to receive */
289 UINT count, /* [in] Number of entries to receive */
290 LPPALETTEENTRY entries) /* [out] Address of array receiving entries */
292 PALETTEOBJ * palPtr;
293 UINT numEntries;
295 TRACE("hpal = %p, count=%i\n", hpalette, count );
297 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
298 if (!palPtr) return 0;
300 /* NOTE: not documented but test show this to be the case */
301 if (count == 0)
303 int rc = palPtr->logpalette.palNumEntries;
304 GDI_ReleaseObj( hpalette );
305 return rc;
308 numEntries = palPtr->logpalette.palNumEntries;
309 if (start+count > numEntries) count = numEntries - start;
310 if (entries)
312 if (start >= numEntries)
314 GDI_ReleaseObj( hpalette );
315 return 0;
317 memcpy( entries, &palPtr->logpalette.palPalEntry[start],
318 count * sizeof(PALETTEENTRY) );
319 for( numEntries = 0; numEntries < count ; numEntries++ )
320 if (entries[numEntries].peFlags & 0xF0)
321 entries[numEntries].peFlags = 0;
324 GDI_ReleaseObj( hpalette );
325 return count;
329 /***********************************************************************
330 * SetPaletteEntries [GDI32.@]
332 * Sets color values for range in palette.
334 * RETURNS
335 * Success: Number of entries that were set
336 * Failure: 0
338 UINT WINAPI SetPaletteEntries(
339 HPALETTE hpalette, /* [in] Handle of logical palette */
340 UINT start, /* [in] Index of first entry to set */
341 UINT count, /* [in] Number of entries to set */
342 const PALETTEENTRY *entries) /* [in] Address of array of structures */
344 PALETTEOBJ * palPtr;
345 UINT numEntries;
347 TRACE("hpal=%p,start=%i,count=%i\n",hpalette,start,count );
349 if (hpalette == GetStockObject(DEFAULT_PALETTE)) return 0;
350 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
351 if (!palPtr) return 0;
353 numEntries = palPtr->logpalette.palNumEntries;
354 if (start >= numEntries)
356 GDI_ReleaseObj( hpalette );
357 return 0;
359 if (start+count > numEntries) count = numEntries - start;
360 memcpy( &palPtr->logpalette.palPalEntry[start], entries,
361 count * sizeof(PALETTEENTRY) );
362 PALETTE_ValidateFlags(palPtr->logpalette.palPalEntry,
363 palPtr->logpalette.palNumEntries);
364 UnrealizeObject( hpalette );
365 GDI_ReleaseObj( hpalette );
366 return count;
370 /***********************************************************************
371 * ResizePalette [GDI32.@]
373 * Resizes logical palette.
375 * RETURNS
376 * Success: TRUE
377 * Failure: FALSE
379 BOOL WINAPI ResizePalette(
380 HPALETTE hPal, /* [in] Handle of logical palette */
381 UINT cEntries) /* [in] Number of entries in logical palette */
383 PALETTEOBJ * palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
384 UINT cPrevEnt, prevVer;
385 int prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
386 int* mapping = NULL;
388 TRACE("hpal = %p, prev = %i, new = %i\n",
389 hPal, palPtr ? palPtr->logpalette.palNumEntries : -1, cEntries );
390 if( !palPtr ) return FALSE;
391 cPrevEnt = palPtr->logpalette.palNumEntries;
392 prevVer = palPtr->logpalette.palVersion;
393 prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) +
394 sizeof(int*) + sizeof(GDIOBJHDR);
395 size += sizeof(int*) + sizeof(GDIOBJHDR);
396 mapping = palPtr->mapping;
398 if (!(palPtr = GDI_ReallocObject( size, hPal, palPtr ))) return FALSE;
400 if( mapping )
402 int *newMap = (int*) HeapReAlloc(GetProcessHeap(), 0,
403 mapping, cEntries * sizeof(int) );
404 if(newMap == NULL)
406 ERR("Can not resize mapping -- out of memory!\n");
407 GDI_ReleaseObj( hPal );
408 return FALSE;
410 palPtr->mapping = newMap;
413 if( cEntries > cPrevEnt )
415 if( mapping )
416 memset(palPtr->mapping + cPrevEnt, 0, (cEntries - cPrevEnt)*sizeof(int));
417 memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
418 PALETTE_ValidateFlags((PALETTEENTRY*)((BYTE*)palPtr + prevsize),
419 cEntries - cPrevEnt );
421 palPtr->logpalette.palNumEntries = cEntries;
422 palPtr->logpalette.palVersion = prevVer;
423 GDI_ReleaseObj( hPal );
424 return TRUE;
428 /***********************************************************************
429 * AnimatePalette [GDI32.@]
431 * Replaces entries in logical palette.
433 * RETURNS
434 * Success: TRUE
435 * Failure: FALSE
437 * FIXME
438 * Should use existing mapping when animating a primary palette
440 BOOL WINAPI AnimatePalette(
441 HPALETTE hPal, /* [in] Handle to logical palette */
442 UINT StartIndex, /* [in] First entry in palette */
443 UINT NumEntries, /* [in] Count of entries in palette */
444 const PALETTEENTRY* PaletteColors) /* [in] Pointer to first replacement */
446 TRACE("%p (%i - %i)\n", hPal, StartIndex,StartIndex+NumEntries);
448 if( hPal != GetStockObject(DEFAULT_PALETTE) )
450 if (!SetPaletteEntries( hPal, StartIndex, NumEntries, PaletteColors )) return FALSE;
452 if (pLastRealizedDC && pLastRealizedDC->pRealizePalette)
453 pLastRealizedDC->pRealizePalette( NULL, hPal, hPal == hPrimaryPalette );
455 return TRUE;
459 /***********************************************************************
460 * SetSystemPaletteUse [GDI32.@]
462 * RETURNS
463 * Success: Previous system palette
464 * Failure: SYSPAL_ERROR
466 UINT WINAPI SetSystemPaletteUse(
467 HDC hdc, /* [in] Handle of device context */
468 UINT use) /* [in] Palette-usage flag */
470 UINT old = SystemPaletteUse;
472 /* Device doesn't support colour palettes */
473 if (!(GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)) {
474 return SYSPAL_ERROR;
477 switch (use) {
478 case SYSPAL_NOSTATIC:
479 case SYSPAL_NOSTATIC256: /* WINVER >= 0x0500 */
480 case SYSPAL_STATIC:
481 SystemPaletteUse = use;
482 return old;
483 default:
484 return SYSPAL_ERROR;
489 /***********************************************************************
490 * GetSystemPaletteUse [GDI32.@]
492 * Gets state of system palette.
494 * RETURNS
495 * Current state of system palette
497 UINT WINAPI GetSystemPaletteUse(
498 HDC hdc) /* [in] Handle of device context */
500 return SystemPaletteUse;
504 /***********************************************************************
505 * GetSystemPaletteEntries [GDI32.@]
507 * Gets range of palette entries.
509 * RETURNS
510 * Success: Number of entries retrieved from palette
511 * Failure: 0
513 UINT WINAPI GetSystemPaletteEntries(
514 HDC hdc, /* [in] Handle of device context */
515 UINT start, /* [in] Index of first entry to be retrieved */
516 UINT count, /* [in] Number of entries to be retrieved */
517 LPPALETTEENTRY entries) /* [out] Array receiving system-palette entries */
519 UINT ret = 0;
520 DC *dc;
522 TRACE("hdc=%p,start=%i,count=%i\n", hdc,start,count);
524 if ((dc = DC_GetDCPtr( hdc )))
526 if (dc->funcs->pGetSystemPaletteEntries)
527 ret = dc->funcs->pGetSystemPaletteEntries( dc->physDev, start, count, entries );
528 GDI_ReleaseObj( hdc );
530 return ret;
534 /***********************************************************************
535 * GetNearestPaletteIndex [GDI32.@]
537 * Gets palette index for color.
539 * NOTES
540 * Should index be initialized to CLR_INVALID instead of 0?
542 * RETURNS
543 * Success: Index of entry in logical palette
544 * Failure: CLR_INVALID
546 UINT WINAPI GetNearestPaletteIndex(
547 HPALETTE hpalette, /* [in] Handle of logical color palette */
548 COLORREF color) /* [in] Color to be matched */
550 PALETTEOBJ* palObj = (PALETTEOBJ*)GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
551 UINT index = 0;
553 if( palObj )
555 int i, diff = 0x7fffffff;
556 int r,g,b;
557 PALETTEENTRY* entry = palObj->logpalette.palPalEntry;
559 for( i = 0; i < palObj->logpalette.palNumEntries && diff ; i++, entry++)
561 if (!(entry->peFlags & PC_SYS_USED)) continue;
563 r = entry->peRed - GetRValue(color);
564 g = entry->peGreen - GetGValue(color);
565 b = entry->peBlue - GetBValue(color);
567 r = r*r + g*g + b*b;
569 if( r < diff ) { index = i; diff = r; }
571 GDI_ReleaseObj( hpalette );
573 TRACE("(%p,%06lx): returning %d\n", hpalette, color, index );
574 return index;
578 /***********************************************************************
579 * GetNearestColor [GDI32.@]
581 * Gets a system color to match.
583 * RETURNS
584 * Success: Color from system palette that corresponds to given color
585 * Failure: CLR_INVALID
587 COLORREF WINAPI GetNearestColor(
588 HDC hdc, /* [in] Handle of device context */
589 COLORREF color) /* [in] Color to be matched */
591 unsigned char spec_type;
592 COLORREF nearest;
593 DC *dc;
595 if (!(dc = DC_GetDCPtr( hdc ))) return CLR_INVALID;
597 if (dc->funcs->pGetNearestColor)
599 nearest = dc->funcs->pGetNearestColor( dc->physDev, color );
600 GDI_ReleaseObj( hdc );
601 return nearest;
604 if (!(GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE))
606 GDI_ReleaseObj( hdc );
607 return color;
610 spec_type = color >> 24;
611 if (spec_type == 1 || spec_type == 2)
613 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
615 UINT index;
616 PALETTEENTRY entry;
617 HPALETTE hpal = dc->hPalette ? dc->hPalette : GetStockObject( DEFAULT_PALETTE );
619 if (spec_type == 2) /* PALETTERGB */
620 index = GetNearestPaletteIndex( hpal, color );
621 else /* PALETTEINDEX */
622 index = LOWORD(color);
624 if (!GetPaletteEntries( hpal, index, 1, &entry ))
626 WARN("RGB(%lx) : idx %d is out of bounds, assuming NULL\n", color, index );
627 if (!GetPaletteEntries( hpal, 0, 1, &entry ))
629 GDI_ReleaseObj( hdc );
630 return CLR_INVALID;
633 color = RGB( entry.peRed, entry.peGreen, entry.peBlue );
635 nearest = color & 0x00ffffff;
636 GDI_ReleaseObj( hdc );
638 TRACE("(%06lx): returning %06lx\n", color, nearest );
639 return nearest;
643 /***********************************************************************
644 * PALETTE_GetObject
646 static INT PALETTE_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
648 PALETTEOBJ *palette = obj;
650 if( !buffer )
651 return sizeof(WORD);
653 if (count > sizeof(WORD)) count = sizeof(WORD);
654 memcpy( buffer, &palette->logpalette.palNumEntries, count );
655 return count;
659 /***********************************************************************
660 * PALETTE_UnrealizeObject
662 static BOOL PALETTE_UnrealizeObject( HGDIOBJ handle, void *obj )
664 PALETTEOBJ *palette = obj;
666 if (palette->mapping)
668 HeapFree( GetProcessHeap(), 0, palette->mapping );
669 palette->mapping = NULL;
671 if (hLastRealizedPalette == handle)
673 hLastRealizedPalette = 0;
674 pLastRealizedDC = NULL;
676 return TRUE;
680 /***********************************************************************
681 * PALETTE_DeleteObject
683 static BOOL PALETTE_DeleteObject( HGDIOBJ handle, void *obj )
685 PALETTEOBJ *palette = obj;
687 HeapFree( GetProcessHeap(), 0, palette->mapping );
688 if (hLastRealizedPalette == handle)
690 hLastRealizedPalette = 0;
691 pLastRealizedDC = NULL;
693 return GDI_FreeObject( handle, obj );
697 /***********************************************************************
698 * GDISelectPalette (Not a Windows API)
700 HPALETTE WINAPI GDISelectPalette( HDC hdc, HPALETTE hpal, WORD wBkg)
702 HPALETTE ret;
703 DC *dc;
705 TRACE("%p %p\n", hdc, hpal );
707 if (GetObjectType(hpal) != OBJ_PAL)
709 WARN("invalid selected palette %p\n",hpal);
710 return 0;
712 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
713 ret = dc->hPalette;
714 if (dc->funcs->pSelectPalette) hpal = dc->funcs->pSelectPalette( dc->physDev, hpal, FALSE );
715 if (hpal)
717 dc->hPalette = hpal;
718 if (!wBkg) hPrimaryPalette = hpal;
720 else ret = 0;
721 GDI_ReleaseObj( hdc );
722 return ret;
726 /***********************************************************************
727 * GDIRealizePalette (Not a Windows API)
729 UINT WINAPI GDIRealizePalette( HDC hdc )
731 UINT realized = 0;
732 DC* dc = DC_GetDCPtr( hdc );
734 if (!dc) return 0;
736 TRACE("%p...\n", hdc );
738 if( dc->hPalette == GetStockObject( DEFAULT_PALETTE ))
740 if (dc->funcs->pRealizeDefaultPalette)
741 realized = dc->funcs->pRealizeDefaultPalette( dc->physDev );
743 else if(dc->hPalette != hLastRealizedPalette )
745 if (dc->funcs->pRealizePalette)
746 realized = dc->funcs->pRealizePalette( dc->physDev, dc->hPalette,
747 (dc->hPalette == hPrimaryPalette) );
748 hLastRealizedPalette = dc->hPalette;
749 pLastRealizedDC = dc->funcs;
751 else TRACE(" skipping (hLastRealizedPalette = %p)\n", hLastRealizedPalette);
753 GDI_ReleaseObj( hdc );
754 TRACE(" realized %i colors.\n", realized );
755 return realized;
759 /***********************************************************************
760 * RealizeDefaultPalette (GDI.365)
762 UINT16 WINAPI RealizeDefaultPalette16( HDC16 hdc )
764 UINT16 ret = 0;
765 DC *dc;
767 TRACE("%04x\n", hdc );
769 if (!(dc = DC_GetDCPtr( HDC_32(hdc) ))) return 0;
771 if (dc->funcs->pRealizeDefaultPalette) ret = dc->funcs->pRealizeDefaultPalette( dc->physDev );
772 GDI_ReleaseObj( HDC_32(hdc) );
773 return ret;
776 /***********************************************************************
777 * IsDCCurrentPalette (GDI.412)
779 BOOL16 WINAPI IsDCCurrentPalette16(HDC16 hDC)
781 DC *dc = DC_GetDCPtr( HDC_32(hDC) );
782 if (dc)
784 BOOL bRet = dc->hPalette == hPrimaryPalette;
785 GDI_ReleaseObj( HDC_32(hDC) );
786 return bRet;
788 return FALSE;
792 /***********************************************************************
793 * SelectPalette [GDI32.@]
795 * Selects logical palette into DC.
797 * RETURNS
798 * Success: Previous logical palette
799 * Failure: NULL
801 HPALETTE WINAPI SelectPalette(
802 HDC hDC, /* [in] Handle of device context */
803 HPALETTE hPal, /* [in] Handle of logical color palette */
804 BOOL bForceBackground) /* [in] Foreground/background mode */
806 return pfnSelectPalette( hDC, hPal, bForceBackground );
810 /***********************************************************************
811 * RealizePalette [GDI32.@]
813 * Maps palette entries to system palette.
815 * RETURNS
816 * Success: Number of entries in logical palette
817 * Failure: GDI_ERROR
819 UINT WINAPI RealizePalette(
820 HDC hDC) /* [in] Handle of device context */
822 return pfnRealizePalette( hDC );
826 typedef HWND (WINAPI *WindowFromDC_funcptr)( HDC );
827 typedef BOOL (WINAPI *RedrawWindow_funcptr)( HWND, const RECT *, HRGN, UINT );
829 /**********************************************************************
830 * UpdateColors [GDI32.@]
832 * Remaps current colors to logical palette.
834 * RETURNS
835 * Success: TRUE
836 * Failure: FALSE
838 BOOL WINAPI UpdateColors(
839 HDC hDC) /* [in] Handle of device context */
841 HMODULE mod;
842 int size = GetDeviceCaps( hDC, SIZEPALETTE );
844 if (!size) return 0;
846 mod = GetModuleHandleA("user32.dll");
847 if (mod)
849 WindowFromDC_funcptr pWindowFromDC = (WindowFromDC_funcptr)GetProcAddress(mod,"WindowFromDC");
850 if (pWindowFromDC)
852 HWND hWnd = pWindowFromDC( hDC );
854 /* Docs say that we have to remap current drawable pixel by pixel
855 * but it would take forever given the speed of XGet/PutPixel.
857 if (hWnd && size)
859 RedrawWindow_funcptr pRedrawWindow = GetProcAddress( mod, "RedrawWindow" );
860 if (pRedrawWindow) pRedrawWindow( hWnd, NULL, 0, RDW_INVALIDATE );
864 return 0x666;
868 /*********************************************************************
869 * SetMagicColors (GDI.606)
871 VOID WINAPI SetMagicColors16(HDC16 hDC, COLORREF color, UINT16 index)
873 FIXME("(hDC %04x, color %04x, index %04x): stub\n", hDC, (int)color, index);
877 /**********************************************************************
878 * GetICMProfileA [GDI32.@]
880 * Returns the filename of the specified device context's color
881 * management profile, even if color management is not enabled
882 * for that DC.
884 * RETURNS
885 * TRUE if name copied successfully OR lpszFilename is NULL
886 * FALSE if the buffer length pointed to by lpcbName is too small
888 * NOTE
889 * The buffer length pointed to by lpcbName is ALWAYS updated to
890 * the length required regardless of other actions this function
891 * may take.
893 * FIXME
894 * How does Windows assign these? Some registry key?
897 #define WINEICM "winefake.icm" /* easy-to-identify fake filename */
899 /*********************************************************************/
901 BOOL WINAPI GetICMProfileA(HDC hDC, LPDWORD lpcbName, LPSTR lpszFilename)
903 DWORD callerLen;
905 FIXME("(%p, %p, %p): partial stub\n", hDC, lpcbName, lpszFilename);
907 callerLen = *lpcbName;
909 /* all 3 behaviors require the required buffer size to be set */
910 *lpcbName = strlen(WINEICM);
912 /* behavior 1: if lpszFilename is NULL, return size of string and no error */
913 if ((DWORD)lpszFilename == (DWORD)0x00000000)
914 return TRUE;
916 /* behavior 2: if buffer size too small, return size of string and error */
917 if (callerLen < strlen(WINEICM))
919 SetLastError(ERROR_INSUFFICIENT_BUFFER);
920 return FALSE;
923 /* behavior 3: if buffer size OK and pointer not NULL, copy and return size */
924 strcpy(lpszFilename, WINEICM);
925 return TRUE;
928 /**********************************************************************
929 * SetICMProfileA [GDI32.@]
932 BOOL WINAPI SetICMProfileA(HDC hDC, LPSTR lpszFilename)
934 FIXME("hDC %p filename '%s': stub!\n", hDC, debugstr_a(lpszFilename));
935 return TRUE; /* success */