Correct usage of a scratch array in X11DRV_PolyBezier.
[wine.git] / objects / palette.c
blob0217c944c19373606621ea14e85d8af76d1e0433
1 /*
2 * GDI palette objects
4 * Copyright 1993,1994 Alexandre Julliard
5 * Copyright 1996 Alex Korobka
7 * PALETTEOBJ is documented in the Dr. Dobbs Journal May 1993.
8 * Information in the "Undocumented Windows" is incorrect.
9 */
11 #include <stdlib.h>
12 #include <string.h>
14 #include "winbase.h"
15 #include "wine/winuser16.h"
16 #include "gdi.h"
17 #include "color.h"
18 #include "palette.h"
19 #include "xmalloc.h"
20 #include "debugtools.h"
21 #include "winerror.h"
23 DEFAULT_DEBUG_CHANNEL(palette)
25 PALETTE_DRIVER *PALETTE_Driver = NULL;
27 FARPROC pfnSelectPalette = NULL;
28 FARPROC pfnRealizePalette = NULL;
30 static UINT SystemPaletteUse = SYSPAL_STATIC; /* currently not considered */
32 static HPALETTE16 hPrimaryPalette = 0; /* used for WM_PALETTECHANGED */
33 static HPALETTE16 hLastRealizedPalette = 0; /* UnrealizeObject() needs it */
36 /***********************************************************************
37 * PALETTE_Init
39 * Create the system palette.
41 HPALETTE16 PALETTE_Init(void)
43 int i;
44 HPALETTE16 hpalette;
45 LOGPALETTE * palPtr;
46 PALETTEOBJ* palObj;
47 const PALETTEENTRY* __sysPalTemplate = COLOR_GetSystemPaletteTemplate();
49 /* create default palette (20 system colors) */
51 palPtr = HeapAlloc( GetProcessHeap(), 0,
52 sizeof(LOGPALETTE) + (NB_RESERVED_COLORS-1)*sizeof(PALETTEENTRY));
53 if (!palPtr) return FALSE;
55 palPtr->palVersion = 0x300;
56 palPtr->palNumEntries = NB_RESERVED_COLORS;
57 for( i = 0; i < NB_RESERVED_COLORS; i ++ )
59 palPtr->palPalEntry[i].peRed = __sysPalTemplate[i].peRed;
60 palPtr->palPalEntry[i].peGreen = __sysPalTemplate[i].peGreen;
61 palPtr->palPalEntry[i].peBlue = __sysPalTemplate[i].peBlue;
62 palPtr->palPalEntry[i].peFlags = 0;
64 hpalette = CreatePalette16( palPtr );
66 palObj = (PALETTEOBJ*) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
67 if (palObj)
69 palObj->mapping = xmalloc( sizeof(int) * 20 );
71 GDI_HEAP_UNLOCK( hpalette );
73 HeapFree( GetProcessHeap(), 0, palPtr );
76 return hpalette;
79 /***********************************************************************
80 * PALETTE_ValidateFlags
82 void PALETTE_ValidateFlags(PALETTEENTRY* lpPalE, int size)
84 int i = 0;
85 for( ; i<size ; i++ )
86 lpPalE[i].peFlags = PC_SYS_USED | (lpPalE[i].peFlags & 0x07);
90 /***********************************************************************
91 * CreatePalette16 (GDI.360)
93 HPALETTE16 WINAPI CreatePalette16( const LOGPALETTE* palette )
95 return CreatePalette( palette );
99 /***********************************************************************
100 * CreatePalette32 [GDI32.53] Creates a logical color palette
102 * RETURNS
103 * Success: Handle to logical palette
104 * Failure: NULL
106 HPALETTE WINAPI CreatePalette(
107 const LOGPALETTE* palette) /* [in] Pointer to logical color palette */
109 PALETTEOBJ * palettePtr;
110 HPALETTE hpalette;
111 int size;
113 if (!palette) return 0;
114 TRACE("entries=%i\n", palette->palNumEntries);
116 size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
118 hpalette = GDI_AllocObject( size + sizeof(int*) +sizeof(GDIOBJHDR) , PALETTE_MAGIC );
119 if (!hpalette) return 0;
121 palettePtr = (PALETTEOBJ *) GDI_HEAP_LOCK( hpalette );
122 memcpy( &palettePtr->logpalette, palette, size );
123 PALETTE_ValidateFlags(palettePtr->logpalette.palPalEntry,
124 palettePtr->logpalette.palNumEntries);
125 palettePtr->mapping = NULL;
126 GDI_HEAP_UNLOCK( hpalette );
128 TRACE(" returning %04x\n", hpalette);
129 return hpalette;
133 /***********************************************************************
134 * CreateHalftonePalette16 [GDI.?] Creates a halftone palette
136 * RETURNS
137 * Success: Handle to logical halftone palette
138 * Failure: 0
140 HPALETTE16 WINAPI CreateHalftonePalette16(
141 HDC16 hdc) /* [in] Handle to device context */
143 return CreateHalftonePalette(hdc);
147 /***********************************************************************
148 * CreateHalftonePalette32 [GDI32.47] Creates a halftone palette
150 * RETURNS
151 * Success: Handle to logical halftone palette
152 * Failure: 0
154 * FIXME: not truly tested
156 HPALETTE WINAPI CreateHalftonePalette(
157 HDC hdc) /* [in] Handle to device context */
159 int i, r, g, b;
160 struct {
161 WORD Version;
162 WORD NumberOfEntries;
163 PALETTEENTRY aEntries[256];
164 } Palette = {
165 0x300, 256
168 GetSystemPaletteEntries(hdc, 0, 256, Palette.aEntries);
169 return CreatePalette((LOGPALETTE *)&Palette);
171 for (r = 0; r < 6; r++) {
172 for (g = 0; g < 6; g++) {
173 for (b = 0; b < 6; b++) {
174 i = r + g*6 + b*36 + 10;
175 Palette.aEntries[i].peRed = r * 51;
176 Palette.aEntries[i].peGreen = g * 51;
177 Palette.aEntries[i].peBlue = b * 51;
182 for (i = 216; i < 246; i++) {
183 int v = (i - 216) * 8;
184 Palette.aEntries[i].peRed = v;
185 Palette.aEntries[i].peGreen = v;
186 Palette.aEntries[i].peBlue = v;
189 return CreatePalette((LOGPALETTE *)&Palette);
193 /***********************************************************************
194 * GetPaletteEntries16 (GDI.363)
196 UINT16 WINAPI GetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
197 UINT16 count, LPPALETTEENTRY entries )
199 return GetPaletteEntries( hpalette, start, count, entries );
203 /***********************************************************************
204 * GetPaletteEntries32 [GDI32.209] Retrieves palette entries
206 * RETURNS
207 * Success: Number of entries from logical palette
208 * Failure: 0
210 UINT WINAPI GetPaletteEntries(
211 HPALETTE hpalette, /* [in] Handle of logical palette */
212 UINT start, /* [in] First entry to receive */
213 UINT count, /* [in] Number of entries to receive */
214 LPPALETTEENTRY entries) /* [out] Address of array receiving entries */
216 PALETTEOBJ * palPtr;
217 INT numEntries;
219 TRACE("hpal = %04x, count=%i\n", hpalette, count );
221 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
222 if (!palPtr) return 0;
224 numEntries = palPtr->logpalette.palNumEntries;
225 if (start+count > numEntries) count = numEntries - start;
226 if (entries)
228 if (start >= numEntries)
230 GDI_HEAP_UNLOCK( hpalette );
231 return 0;
233 memcpy( entries, &palPtr->logpalette.palPalEntry[start],
234 count * sizeof(PALETTEENTRY) );
235 for( numEntries = 0; numEntries < count ; numEntries++ )
236 if (entries[numEntries].peFlags & 0xF0)
237 entries[numEntries].peFlags = 0;
238 GDI_HEAP_UNLOCK( hpalette );
241 return count;
245 /***********************************************************************
246 * SetPaletteEntries16 (GDI.364)
248 UINT16 WINAPI SetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
249 UINT16 count, LPPALETTEENTRY entries )
251 return SetPaletteEntries( hpalette, start, count, entries );
255 /***********************************************************************
256 * SetPaletteEntries32 [GDI32.326] Sets color values for range in palette
258 * RETURNS
259 * Success: Number of entries that were set
260 * Failure: 0
262 UINT WINAPI SetPaletteEntries(
263 HPALETTE hpalette, /* [in] Handle of logical palette */
264 UINT start, /* [in] Index of first entry to set */
265 UINT count, /* [in] Number of entries to set */
266 LPPALETTEENTRY entries) /* [in] Address of array of structures */
268 PALETTEOBJ * palPtr;
269 INT numEntries;
271 TRACE("hpal=%04x,start=%i,count=%i\n",hpalette,start,count );
273 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
274 if (!palPtr) return 0;
276 numEntries = palPtr->logpalette.palNumEntries;
277 if (start >= numEntries)
279 GDI_HEAP_UNLOCK( hpalette );
280 return 0;
282 if (start+count > numEntries) count = numEntries - start;
283 memcpy( &palPtr->logpalette.palPalEntry[start], entries,
284 count * sizeof(PALETTEENTRY) );
285 PALETTE_ValidateFlags(palPtr->logpalette.palPalEntry,
286 palPtr->logpalette.palNumEntries);
287 free(palPtr->mapping);
288 palPtr->mapping = NULL;
289 GDI_HEAP_UNLOCK( hpalette );
290 return count;
294 /***********************************************************************
295 * ResizePalette16 (GDI.368)
297 BOOL16 WINAPI ResizePalette16( HPALETTE16 hPal, UINT16 cEntries )
299 return ResizePalette( hPal, cEntries );
303 /***********************************************************************
304 * ResizePalette32 [GDI32.289] Resizes logical palette
306 * RETURNS
307 * Success: TRUE
308 * Failure: FALSE
310 BOOL WINAPI ResizePalette(
311 HPALETTE hPal, /* [in] Handle of logical palette */
312 UINT cEntries) /* [in] Number of entries in logical palette */
314 PALETTEOBJ * palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
315 UINT cPrevEnt, prevVer;
316 int prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
317 int* mapping = NULL;
319 TRACE("hpal = %04x, prev = %i, new = %i\n",
320 hPal, palPtr ? palPtr->logpalette.palNumEntries : -1,
321 cEntries );
322 if( !palPtr ) return FALSE;
323 cPrevEnt = palPtr->logpalette.palNumEntries;
324 prevVer = palPtr->logpalette.palVersion;
325 prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) +
326 sizeof(int*) + sizeof(GDIOBJHDR);
327 size += sizeof(int*) + sizeof(GDIOBJHDR);
328 mapping = palPtr->mapping;
330 GDI_HEAP_UNLOCK( hPal );
332 hPal = GDI_HEAP_REALLOC( hPal, size );
333 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
334 if( !palPtr ) return FALSE;
336 if( mapping )
337 palPtr->mapping = (int*) xrealloc( mapping, cEntries * sizeof(int) );
338 if( cEntries > cPrevEnt )
340 if( mapping )
341 memset(palPtr->mapping + cPrevEnt, 0, (cEntries - cPrevEnt)*sizeof(int));
342 memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
343 PALETTE_ValidateFlags((PALETTEENTRY*)((BYTE*)palPtr + prevsize),
344 cEntries - cPrevEnt );
346 palPtr->logpalette.palNumEntries = cEntries;
347 palPtr->logpalette.palVersion = prevVer;
348 GDI_HEAP_UNLOCK( hPal );
349 return TRUE;
353 /***********************************************************************
354 * AnimatePalette16 (GDI.367)
356 void WINAPI AnimatePalette16( HPALETTE16 hPal, UINT16 StartIndex,
357 UINT16 NumEntries, const PALETTEENTRY* PaletteColors)
359 AnimatePalette( hPal, StartIndex, NumEntries, PaletteColors );
363 /***********************************************************************
364 * AnimatePalette32 [GDI32.6] Replaces entries in logical palette
366 * RETURNS
367 * Success: TRUE
368 * Failure: FALSE
370 * FIXME
371 * Should use existing mapping when animating a primary palette
373 BOOL WINAPI AnimatePalette(
374 HPALETTE hPal, /* [in] Handle to logical palette */
375 UINT StartIndex, /* [in] First entry in palette */
376 UINT NumEntries, /* [in] Count of entries in palette */
377 const PALETTEENTRY* PaletteColors) /* [in] Pointer to first replacement */
379 TRACE("%04x (%i - %i)\n", hPal, StartIndex,StartIndex+NumEntries);
381 if( hPal != STOCK_DEFAULT_PALETTE )
383 PALETTEOBJ* palPtr = (PALETTEOBJ *)GDI_GetObjPtr(hPal, PALETTE_MAGIC);
384 if (!palPtr) return FALSE;
386 if( (StartIndex + NumEntries) <= palPtr->logpalette.palNumEntries )
388 UINT u;
389 for( u = 0; u < NumEntries; u++ )
390 palPtr->logpalette.palPalEntry[u + StartIndex] = PaletteColors[u];
391 PALETTE_Driver->
392 pSetMapping(palPtr, StartIndex, NumEntries,
393 hPal != hPrimaryPalette );
394 GDI_HEAP_UNLOCK( hPal );
395 return TRUE;
398 return FALSE;
402 /***********************************************************************
403 * SetSystemPaletteUse16 (GDI.373)
405 UINT16 WINAPI SetSystemPaletteUse16( HDC16 hdc, UINT16 use )
407 return SetSystemPaletteUse( hdc, use );
411 /***********************************************************************
412 * SetSystemPaletteUse32 [GDI32.335]
414 * RETURNS
415 * Success: Previous system palette
416 * Failure: SYSPAL_ERROR
418 UINT WINAPI SetSystemPaletteUse(
419 HDC hdc, /* [in] Handle of device context */
420 UINT use) /* [in] Palette-usage flag */
422 UINT old = SystemPaletteUse;
423 FIXME("(%04x,%04x): stub\n", hdc, use );
424 SystemPaletteUse = use;
425 return old;
429 /***********************************************************************
430 * GetSystemPaletteUse16 (GDI.374)
432 UINT16 WINAPI GetSystemPaletteUse16( HDC16 hdc )
434 return SystemPaletteUse;
438 /***********************************************************************
439 * GetSystemPaletteUse32 [GDI32.223] Gets state of system palette
441 * RETURNS
442 * Current state of system palette
444 UINT WINAPI GetSystemPaletteUse(
445 HDC hdc) /* [in] Handle of device context */
447 return SystemPaletteUse;
451 /***********************************************************************
452 * GetSystemPaletteEntries16 (GDI.375)
454 UINT16 WINAPI GetSystemPaletteEntries16( HDC16 hdc, UINT16 start, UINT16 count,
455 LPPALETTEENTRY entries )
457 return GetSystemPaletteEntries( hdc, start, count, entries );
461 /***********************************************************************
462 * GetSystemPaletteEntries32 [GDI32.222] Gets range of palette entries
464 * RETURNS
465 * Success: Number of entries retrieved from palette
466 * Failure: 0
468 UINT WINAPI GetSystemPaletteEntries(
469 HDC hdc, /* [in] Handle of device context */
470 UINT start, /* [in] Index of first entry to be retrieved */
471 UINT count, /* [in] Number of entries to be retrieved */
472 LPPALETTEENTRY entries) /* [out] Array receiving system-palette entries */
474 UINT i;
475 DC *dc;
477 TRACE("hdc=%04x,start=%i,count=%i\n", hdc,start,count);
479 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
480 if (!entries) return dc->w.devCaps->sizePalette;
481 if (start >= dc->w.devCaps->sizePalette)
483 GDI_HEAP_UNLOCK( hdc );
484 return 0;
486 if (start+count >= dc->w.devCaps->sizePalette)
487 count = dc->w.devCaps->sizePalette - start;
488 for (i = 0; i < count; i++)
490 *(COLORREF*)(entries + i) = COLOR_GetSystemPaletteEntry( start + i );
492 TRACE("\tidx(%02x) -> RGB(%08lx)\n",
493 start + i, *(COLORREF*)(entries + i) );
495 GDI_HEAP_UNLOCK( hdc );
496 return count;
500 /***********************************************************************
501 * GetNearestPaletteIndex16 (GDI.370)
503 UINT16 WINAPI GetNearestPaletteIndex16( HPALETTE16 hpalette, COLORREF color )
505 return GetNearestPaletteIndex( hpalette, color );
509 /***********************************************************************
510 * GetNearestPaletteIndex32 [GDI32.203] Gets palette index for color
512 * NOTES
513 * Should index be initialized to CLR_INVALID instead of 0?
515 * RETURNS
516 * Success: Index of entry in logical palette
517 * Failure: CLR_INVALID
519 UINT WINAPI GetNearestPaletteIndex(
520 HPALETTE hpalette, /* [in] Handle of logical color palette */
521 COLORREF color) /* [in] Color to be matched */
523 PALETTEOBJ* palObj = (PALETTEOBJ*)GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
524 UINT index = 0;
526 if( palObj )
527 index = COLOR_PaletteLookupPixel(palObj->logpalette.palPalEntry,
528 palObj->logpalette.palNumEntries,
529 NULL, color, FALSE );
531 TRACE("(%04x,%06lx): returning %d\n", hpalette, color, index );
532 GDI_HEAP_UNLOCK( hpalette );
533 return index;
537 /***********************************************************************
538 * GetNearestColor16 (GDI.154)
540 COLORREF WINAPI GetNearestColor16( HDC16 hdc, COLORREF color )
542 return GetNearestColor( hdc, color );
546 /***********************************************************************
547 * GetNearestColor32 [GDI32.202] Gets a system color to match
549 * NOTES
550 * Should this return CLR_INVALID instead of FadeCafe?
552 * RETURNS
553 * Success: Color from system palette that corresponds to given color
554 * Failure: CLR_INVALID
556 COLORREF WINAPI GetNearestColor(
557 HDC hdc, /* [in] Handle of device context */
558 COLORREF color) /* [in] Color to be matched */
560 COLORREF nearest = 0xFADECAFE;
561 DC *dc;
562 PALETTEOBJ *palObj;
564 if ( (dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC )) )
566 palObj = (PALETTEOBJ*)
567 GDI_GetObjPtr( (dc->w.hPalette)? dc->w.hPalette
568 : STOCK_DEFAULT_PALETTE, PALETTE_MAGIC );
569 if (!palObj) return nearest;
571 nearest = COLOR_LookupNearestColor( palObj->logpalette.palPalEntry,
572 palObj->logpalette.palNumEntries, color );
573 GDI_HEAP_UNLOCK( dc->w.hPalette );
576 TRACE("(%06lx): returning %06lx\n", color, nearest );
577 GDI_HEAP_UNLOCK( hdc );
578 return nearest;
582 /***********************************************************************
583 * PALETTE_GetObject
585 int PALETTE_GetObject( PALETTEOBJ * palette, int count, LPSTR buffer )
587 if (count > sizeof(WORD)) count = sizeof(WORD);
588 memcpy( buffer, &palette->logpalette.palNumEntries, count );
589 return count;
593 /***********************************************************************
594 * PALETTE_UnrealizeObject
596 BOOL PALETTE_UnrealizeObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
598 if (palette->mapping)
600 free( palette->mapping );
601 palette->mapping = NULL;
603 if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
604 return TRUE;
608 /***********************************************************************
609 * PALETTE_DeleteObject
611 BOOL PALETTE_DeleteObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
613 free( palette->mapping );
614 if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
615 return GDI_FreeObject( hpalette );
619 /***********************************************************************
620 * GDISelectPalette (GDI.361)
622 HPALETTE16 WINAPI GDISelectPalette16( HDC16 hdc, HPALETTE16 hpal, WORD wBkg)
624 HPALETTE16 prev;
625 DC *dc;
627 TRACE("%04x %04x\n", hdc, hpal );
629 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
630 if (!dc)
632 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
633 if (!dc) return 0;
635 prev = dc->w.hPalette;
636 dc->w.hPalette = hpal;
637 GDI_HEAP_UNLOCK( hdc );
638 if (!wBkg) hPrimaryPalette = hpal;
639 return prev;
643 /***********************************************************************
644 * GDIRealizePalette (GDI.362)
646 UINT16 WINAPI GDIRealizePalette16( HDC16 hdc )
648 PALETTEOBJ* palPtr;
649 int realized = 0;
650 DC* dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
651 if (!dc)
653 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
654 if (!dc) return 0;
657 TRACE("%04x...\n", hdc );
659 if( dc && dc->w.hPalette != hLastRealizedPalette )
661 if( dc->w.hPalette == STOCK_DEFAULT_PALETTE )
662 return RealizeDefaultPalette16( hdc );
664 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC );
666 if (!palPtr) {
667 FIXME("invalid selected palette %04x\n",dc->w.hPalette);
668 return 0;
671 realized = PALETTE_Driver->
672 pSetMapping(palPtr,0,palPtr->logpalette.palNumEntries,
673 (dc->w.hPalette != hPrimaryPalette) ||
674 (dc->w.hPalette == STOCK_DEFAULT_PALETTE));
675 GDI_HEAP_UNLOCK( dc->w.hPalette );
676 hLastRealizedPalette = dc->w.hPalette;
678 else TRACE(" skipping (hLastRealizedPalette = %04x)\n",
679 hLastRealizedPalette);
680 GDI_HEAP_UNLOCK( hdc );
682 TRACE(" realized %i colors.\n", realized );
683 return (UINT16)realized;
687 /***********************************************************************
688 * RealizeDefaultPalette (GDI.365)
690 UINT16 WINAPI RealizeDefaultPalette16( HDC16 hdc )
692 DC *dc;
693 PALETTEOBJ* palPtr;
695 TRACE("%04x\n", hdc );
697 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
698 if (!dc)
700 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
701 if (!dc) return 0;
704 if ( dc->w.flags & DC_MEMORY )
706 GDI_HEAP_UNLOCK( hdc );
707 return 0;
710 hPrimaryPalette = STOCK_DEFAULT_PALETTE;
711 hLastRealizedPalette = STOCK_DEFAULT_PALETTE;
713 palPtr = (PALETTEOBJ*)GDI_GetObjPtr(STOCK_DEFAULT_PALETTE, PALETTE_MAGIC );
714 if (!palPtr) return 0;
716 /* lookup is needed to account for SetSystemPaletteUse() stuff */
718 return PALETTE_Driver->pUpdateMapping(palPtr);
721 /***********************************************************************
722 * IsDCCurrentPalette (GDI.412)
724 BOOL16 WINAPI IsDCCurrentPalette16(HDC16 hDC)
726 DC* dc = (DC *)GDI_GetObjPtr( hDC, DC_MAGIC );
727 if (dc)
729 GDI_HEAP_UNLOCK( hDC );
730 return dc->w.hPalette == hPrimaryPalette;
732 return FALSE;
736 /***********************************************************************
737 * SelectPalette16 (USER.282)
739 HPALETTE16 WINAPI SelectPalette16( HDC16 hDC, HPALETTE16 hPal,
740 BOOL16 bForceBackground )
742 return SelectPalette( hDC, hPal, bForceBackground );
746 /***********************************************************************
747 * SelectPalette32 [GDI32.300] Selects logical palette into DC
749 * RETURNS
750 * Success: Previous logical palette
751 * Failure: NULL
753 HPALETTE WINAPI SelectPalette(
754 HDC hDC, /* [in] Handle of device context */
755 HPALETTE hPal, /* [in] Handle of logical color palette */
756 BOOL bForceBackground) /* [in] Foreground/background mode */
758 WORD wBkgPalette = 1;
759 PALETTEOBJ* lpt = (PALETTEOBJ*) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
761 TRACE("dc=%04x,pal=%04x,force=%i\n", hDC, hPal, bForceBackground);
762 if( !lpt ) return 0;
764 TRACE(" entries = %d\n", lpt->logpalette.palNumEntries);
765 GDI_HEAP_UNLOCK( hPal );
767 if( hPal != STOCK_DEFAULT_PALETTE )
769 HWND hWnd = WindowFromDC( hDC );
770 HWND hActive = GetActiveWindow();
772 /* set primary palette if it's related to current active */
774 if((!hWnd || (hActive == hWnd || IsChild16(hActive,hWnd))) &&
775 !bForceBackground )
776 wBkgPalette = 0;
778 return GDISelectPalette16( hDC, hPal, wBkgPalette);
782 /***********************************************************************
783 * RealizePalette16 (USER.283)
785 UINT16 WINAPI RealizePalette16( HDC16 hDC )
787 return RealizePalette( hDC );
791 /***********************************************************************
792 * RealizePalette32 [GDI32.280] Maps palette entries to system palette
794 * RETURNS
795 * Success: Number of entries in logical palette
796 * Failure: GDI_ERROR
798 UINT WINAPI RealizePalette(
799 HDC hDC) /* [in] Handle of device context */
801 DC *dc;
802 UINT realized;
804 if (!(dc = (DC *) GDI_GetObjPtr( hDC, DC_MAGIC ))) return 0;
806 realized = GDIRealizePalette16( hDC );
808 /* do not send anything if no colors were changed */
810 if( IsDCCurrentPalette16( hDC ) && realized &&
811 dc->w.devCaps->sizePalette )
813 /* Send palette change notification */
815 HWND hWnd;
816 if( (hWnd = WindowFromDC( hDC )) )
817 SendMessage16( HWND_BROADCAST, WM_PALETTECHANGED, hWnd, 0L);
820 GDI_HEAP_UNLOCK( hDC );
821 return realized;
825 /**********************************************************************
826 * UpdateColors16 (GDI.366)
828 INT16 WINAPI UpdateColors16( HDC16 hDC )
830 DC *dc;
831 HWND hWnd;
833 if (!(dc = (DC *) GDI_GetObjPtr( hDC, DC_MAGIC ))) return 0;
835 hWnd = WindowFromDC( hDC );
837 /* Docs say that we have to remap current drawable pixel by pixel
838 * but it would take forever given the speed of XGet/PutPixel.
840 if (hWnd && dc->w.devCaps->sizePalette )
841 InvalidateRect( hWnd, NULL, FALSE );
843 GDI_HEAP_UNLOCK( hDC );
845 return 0x666;
849 /**********************************************************************
850 * UpdateColors32 [GDI32.359] Remaps current colors to logical palette
852 * RETURNS
853 * Success: TRUE
854 * Failure: FALSE
856 BOOL WINAPI UpdateColors(
857 HDC hDC) /* [in] Handle of device context */
859 UpdateColors16( hDC );
860 return TRUE;
864 /*********************************************************************
865 * SetMagicColors16 (GDI.606)
867 VOID WINAPI SetMagicColors16(HDC16 hDC, COLORREF color, UINT16 index)
869 FIXME("(hDC %04x, color %04x, index %04x): stub\n", hDC, (int)color, index);
873 /**********************************************************************
874 * GetICMProfileA [GDI32.316]
876 * Returns the filename of the specified device context's color
877 * management profile, even if color management is not enabled
878 * for that DC.
880 * RETURNS
881 * TRUE if name copied succesfully OR lpszFilename is NULL
882 * FALSE if the buffer length pointed to by lpcbName is too small
884 * NOTE
885 * The buffer length pointed to by lpcbName is ALWAYS updated to
886 * the length required regardless of other actions this function
887 * may take.
889 * FIXME
890 * How does Windows assign these? Some registry key?
893 #define WINEICM "winefake.icm" /* easy-to-identify fake filename */
895 BOOL WINAPI GetICMProfileA(HDC hDC, LPDWORD lpcbName, LPSTR lpszFilename)
897 DWORD callerLen;
899 FIXME("(%04x, %p, %p): partial stub\n", hDC, lpcbName, lpszFilename);
901 callerLen = *lpcbName;
903 /* all 3 behaviors require the required buffer size to be set */
904 *lpcbName = strlen(WINEICM);
906 /* behavior 1: if lpszFilename is NULL, return size of string and no error */
907 if ((DWORD)lpszFilename == (DWORD)0x00000000)
908 return TRUE;
910 /* behavior 2: if buffer size too small, return size of string and error */
911 if (callerLen < strlen(WINEICM))
913 SetLastError(ERROR_INSUFFICIENT_BUFFER);
914 return FALSE;
917 /* behavior 3: if buffer size OK and pointer not NULL, copy and return size */
918 lstrcpyA(lpszFilename, WINEICM);
919 return TRUE;