Made SELECTOR_ReallocBlock preserve the current selector access rights
[wine/wine64.git] / objects / palette.c
blob245a60abdb78717eaa80af4c18dd305fb3227eb8
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 "gdi.h"
15 #include "color.h"
16 #include "palette.h"
17 #include "xmalloc.h"
18 #include "debug.h"
19 #include "wine/winuser16.h"
21 PALETTE_DRIVER *PALETTE_Driver = NULL;
23 FARPROC pfnSelectPalette = NULL;
24 FARPROC pfnRealizePalette = NULL;
26 static UINT SystemPaletteUse = SYSPAL_STATIC; /* currently not considered */
28 static HPALETTE16 hPrimaryPalette = 0; /* used for WM_PALETTECHANGED */
29 static HPALETTE16 hLastRealizedPalette = 0; /* UnrealizeObject() needs it */
32 /***********************************************************************
33 * PALETTE_Init
35 * Create the system palette.
37 HPALETTE16 PALETTE_Init(void)
39 int i;
40 HPALETTE16 hpalette;
41 LOGPALETTE * palPtr;
42 PALETTEOBJ* palObj;
43 const PALETTEENTRY* __sysPalTemplate = COLOR_GetSystemPaletteTemplate();
45 /* create default palette (20 system colors) */
47 palPtr = HeapAlloc( GetProcessHeap(), 0,
48 sizeof(LOGPALETTE) + (NB_RESERVED_COLORS-1)*sizeof(PALETTEENTRY));
49 if (!palPtr) return FALSE;
51 palPtr->palVersion = 0x300;
52 palPtr->palNumEntries = NB_RESERVED_COLORS;
53 for( i = 0; i < NB_RESERVED_COLORS; i ++ )
55 palPtr->palPalEntry[i].peRed = __sysPalTemplate[i].peRed;
56 palPtr->palPalEntry[i].peGreen = __sysPalTemplate[i].peGreen;
57 palPtr->palPalEntry[i].peBlue = __sysPalTemplate[i].peBlue;
58 palPtr->palPalEntry[i].peFlags = 0;
60 hpalette = CreatePalette16( palPtr );
62 palObj = (PALETTEOBJ*) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
63 if (palObj)
65 palObj->mapping = xmalloc( sizeof(int) * 20 );
67 GDI_HEAP_UNLOCK( hpalette );
69 HeapFree( GetProcessHeap(), 0, palPtr );
72 return hpalette;
75 /***********************************************************************
76 * PALETTE_ValidateFlags
78 void PALETTE_ValidateFlags(PALETTEENTRY* lpPalE, int size)
80 int i = 0;
81 for( ; i<size ; i++ )
82 lpPalE[i].peFlags = PC_SYS_USED | (lpPalE[i].peFlags & 0x07);
86 /***********************************************************************
87 * CreatePalette16 (GDI.360)
89 HPALETTE16 WINAPI CreatePalette16( const LOGPALETTE* palette )
91 return CreatePalette( palette );
95 /***********************************************************************
96 * CreatePalette32 [GDI32.53] Creates a logical color palette
98 * RETURNS
99 * Success: Handle to logical palette
100 * Failure: NULL
102 HPALETTE WINAPI CreatePalette(
103 const LOGPALETTE* palette) /* [in] Pointer to logical color palette */
105 PALETTEOBJ * palettePtr;
106 HPALETTE hpalette;
107 int size;
109 if (!palette) return 0;
110 TRACE(palette,"entries=%i\n", palette->palNumEntries);
112 size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
114 hpalette = GDI_AllocObject( size + sizeof(int*) +sizeof(GDIOBJHDR) , PALETTE_MAGIC );
115 if (!hpalette) return 0;
117 palettePtr = (PALETTEOBJ *) GDI_HEAP_LOCK( hpalette );
118 memcpy( &palettePtr->logpalette, palette, size );
119 PALETTE_ValidateFlags(palettePtr->logpalette.palPalEntry,
120 palettePtr->logpalette.palNumEntries);
121 palettePtr->mapping = NULL;
122 GDI_HEAP_UNLOCK( hpalette );
124 TRACE(palette," returning %04x\n", hpalette);
125 return hpalette;
129 /***********************************************************************
130 * CreateHalftonePalette16 [GDI.?] Creates a halftone palette
132 * RETURNS
133 * Success: Handle to logical halftone palette
134 * Failure: 0
136 HPALETTE16 WINAPI CreateHalftonePalette16(
137 HDC16 hdc) /* [in] Handle to device context */
139 return CreateHalftonePalette(hdc);
143 /***********************************************************************
144 * CreateHalftonePalette32 [GDI32.47] Creates a halftone palette
146 * RETURNS
147 * Success: Handle to logical halftone palette
148 * Failure: 0
150 * FIXME: not truly tested
152 HPALETTE WINAPI CreateHalftonePalette(
153 HDC hdc) /* [in] Handle to device context */
155 int i, r, g, b;
156 struct {
157 WORD Version;
158 WORD NumberOfEntries;
159 PALETTEENTRY aEntries[256];
160 } Palette = {
161 0x300, 256
164 GetSystemPaletteEntries(hdc, 0, 256, Palette.aEntries);
165 return CreatePalette((LOGPALETTE *)&Palette);
167 for (r = 0; r < 6; r++) {
168 for (g = 0; g < 6; g++) {
169 for (b = 0; b < 6; b++) {
170 i = r + g*6 + b*36 + 10;
171 Palette.aEntries[i].peRed = r * 51;
172 Palette.aEntries[i].peGreen = g * 51;
173 Palette.aEntries[i].peBlue = b * 51;
178 for (i = 216; i < 246; i++) {
179 int v = (i - 216) * 8;
180 Palette.aEntries[i].peRed = v;
181 Palette.aEntries[i].peGreen = v;
182 Palette.aEntries[i].peBlue = v;
185 return CreatePalette((LOGPALETTE *)&Palette);
189 /***********************************************************************
190 * GetPaletteEntries16 (GDI.363)
192 UINT16 WINAPI GetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
193 UINT16 count, LPPALETTEENTRY entries )
195 return GetPaletteEntries( hpalette, start, count, entries );
199 /***********************************************************************
200 * GetPaletteEntries32 [GDI32.209] Retrieves palette entries
202 * RETURNS
203 * Success: Number of entries from logical palette
204 * Failure: 0
206 UINT WINAPI GetPaletteEntries(
207 HPALETTE hpalette, /* [in] Handle of logical palette */
208 UINT start, /* [in] First entry to receive */
209 UINT count, /* [in] Number of entries to receive */
210 LPPALETTEENTRY entries) /* [out] Address of array receiving entries */
212 PALETTEOBJ * palPtr;
213 INT numEntries;
215 TRACE(palette,"hpal = %04x, count=%i\n", hpalette, count );
217 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
218 if (!palPtr) return 0;
220 numEntries = palPtr->logpalette.palNumEntries;
221 if (start+count > numEntries) count = numEntries - start;
222 if (entries)
224 if (start >= numEntries)
226 GDI_HEAP_UNLOCK( hpalette );
227 return 0;
229 memcpy( entries, &palPtr->logpalette.palPalEntry[start],
230 count * sizeof(PALETTEENTRY) );
231 for( numEntries = 0; numEntries < count ; numEntries++ )
232 if (entries[numEntries].peFlags & 0xF0)
233 entries[numEntries].peFlags = 0;
234 GDI_HEAP_UNLOCK( hpalette );
237 return count;
241 /***********************************************************************
242 * SetPaletteEntries16 (GDI.364)
244 UINT16 WINAPI SetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
245 UINT16 count, LPPALETTEENTRY entries )
247 return SetPaletteEntries( hpalette, start, count, entries );
251 /***********************************************************************
252 * SetPaletteEntries32 [GDI32.326] Sets color values for range in palette
254 * RETURNS
255 * Success: Number of entries that were set
256 * Failure: 0
258 UINT WINAPI SetPaletteEntries(
259 HPALETTE hpalette, /* [in] Handle of logical palette */
260 UINT start, /* [in] Index of first entry to set */
261 UINT count, /* [in] Number of entries to set */
262 LPPALETTEENTRY entries) /* [in] Address of array of structures */
264 PALETTEOBJ * palPtr;
265 INT numEntries;
267 TRACE(palette,"hpal=%04x,start=%i,count=%i\n",hpalette,start,count );
269 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
270 if (!palPtr) return 0;
272 numEntries = palPtr->logpalette.palNumEntries;
273 if (start >= numEntries)
275 GDI_HEAP_UNLOCK( hpalette );
276 return 0;
278 if (start+count > numEntries) count = numEntries - start;
279 memcpy( &palPtr->logpalette.palPalEntry[start], entries,
280 count * sizeof(PALETTEENTRY) );
281 PALETTE_ValidateFlags(palPtr->logpalette.palPalEntry,
282 palPtr->logpalette.palNumEntries);
283 free(palPtr->mapping);
284 palPtr->mapping = NULL;
285 GDI_HEAP_UNLOCK( hpalette );
286 return count;
290 /***********************************************************************
291 * ResizePalette16 (GDI.368)
293 BOOL16 WINAPI ResizePalette16( HPALETTE16 hPal, UINT16 cEntries )
295 return ResizePalette( hPal, cEntries );
299 /***********************************************************************
300 * ResizePalette32 [GDI32.289] Resizes logical palette
302 * RETURNS
303 * Success: TRUE
304 * Failure: FALSE
306 BOOL WINAPI ResizePalette(
307 HPALETTE hPal, /* [in] Handle of logical palette */
308 UINT cEntries) /* [in] Number of entries in logical palette */
310 PALETTEOBJ * palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
311 UINT cPrevEnt, prevVer;
312 int prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
313 int* mapping = NULL;
315 TRACE(palette,"hpal = %04x, prev = %i, new = %i\n",
316 hPal, palPtr ? palPtr->logpalette.palNumEntries : -1,
317 cEntries );
318 if( !palPtr ) return FALSE;
319 cPrevEnt = palPtr->logpalette.palNumEntries;
320 prevVer = palPtr->logpalette.palVersion;
321 prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) +
322 sizeof(int*) + sizeof(GDIOBJHDR);
323 size += sizeof(int*) + sizeof(GDIOBJHDR);
324 mapping = palPtr->mapping;
326 GDI_HEAP_UNLOCK( hPal );
328 hPal = GDI_HEAP_REALLOC( hPal, size );
329 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
330 if( !palPtr ) return FALSE;
332 if( mapping )
333 palPtr->mapping = (int*) xrealloc( mapping, cEntries * sizeof(int) );
334 if( cEntries > cPrevEnt )
336 if( mapping )
337 memset(palPtr->mapping + cPrevEnt, 0, (cEntries - cPrevEnt)*sizeof(int));
338 memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
339 PALETTE_ValidateFlags((PALETTEENTRY*)((BYTE*)palPtr + prevsize),
340 cEntries - cPrevEnt );
342 palPtr->logpalette.palNumEntries = cEntries;
343 palPtr->logpalette.palVersion = prevVer;
344 GDI_HEAP_UNLOCK( hPal );
345 return TRUE;
349 /***********************************************************************
350 * AnimatePalette16 (GDI.367)
352 void WINAPI AnimatePalette16( HPALETTE16 hPal, UINT16 StartIndex,
353 UINT16 NumEntries, const PALETTEENTRY* PaletteColors)
355 AnimatePalette( hPal, StartIndex, NumEntries, PaletteColors );
359 /***********************************************************************
360 * AnimatePalette32 [GDI32.6] Replaces entries in logical palette
362 * RETURNS
363 * Success: TRUE
364 * Failure: FALSE
366 * FIXME
367 * Should use existing mapping when animating a primary palette
369 BOOL WINAPI AnimatePalette(
370 HPALETTE hPal, /* [in] Handle to logical palette */
371 UINT StartIndex, /* [in] First entry in palette */
372 UINT NumEntries, /* [in] Count of entries in palette */
373 const PALETTEENTRY* PaletteColors) /* [in] Pointer to first replacement */
375 TRACE(palette, "%04x (%i - %i)\n", hPal, StartIndex,StartIndex+NumEntries);
377 if( hPal != STOCK_DEFAULT_PALETTE )
379 PALETTEOBJ* palPtr = (PALETTEOBJ *)GDI_GetObjPtr(hPal, PALETTE_MAGIC);
380 if (!palPtr) return FALSE;
382 if( (StartIndex + NumEntries) <= palPtr->logpalette.palNumEntries )
384 UINT u;
385 for( u = 0; u < NumEntries; u++ )
386 palPtr->logpalette.palPalEntry[u + StartIndex] = PaletteColors[u];
387 PALETTE_Driver->
388 pSetMapping(palPtr, StartIndex, NumEntries,
389 hPal != hPrimaryPalette );
390 GDI_HEAP_UNLOCK( hPal );
391 return TRUE;
394 return FALSE;
398 /***********************************************************************
399 * SetSystemPaletteUse16 (GDI.373)
401 UINT16 WINAPI SetSystemPaletteUse16( HDC16 hdc, UINT16 use )
403 return SetSystemPaletteUse( hdc, use );
407 /***********************************************************************
408 * SetSystemPaletteUse32 [GDI32.335]
410 * RETURNS
411 * Success: Previous system palette
412 * Failure: SYSPAL_ERROR
414 UINT WINAPI SetSystemPaletteUse(
415 HDC hdc, /* [in] Handle of device context */
416 UINT use) /* [in] Palette-usage flag */
418 UINT old = SystemPaletteUse;
419 FIXME(palette,"(%04x,%04x): stub\n", hdc, use );
420 SystemPaletteUse = use;
421 return old;
425 /***********************************************************************
426 * GetSystemPaletteUse16 (GDI.374)
428 UINT16 WINAPI GetSystemPaletteUse16( HDC16 hdc )
430 return SystemPaletteUse;
434 /***********************************************************************
435 * GetSystemPaletteUse32 [GDI32.223] Gets state of system palette
437 * RETURNS
438 * Current state of system palette
440 UINT WINAPI GetSystemPaletteUse(
441 HDC hdc) /* [in] Handle of device context */
443 return SystemPaletteUse;
447 /***********************************************************************
448 * GetSystemPaletteEntries16 (GDI.375)
450 UINT16 WINAPI GetSystemPaletteEntries16( HDC16 hdc, UINT16 start, UINT16 count,
451 LPPALETTEENTRY entries )
453 return GetSystemPaletteEntries( hdc, start, count, entries );
457 /***********************************************************************
458 * GetSystemPaletteEntries32 [GDI32.222] Gets range of palette entries
460 * RETURNS
461 * Success: Number of entries retrieved from palette
462 * Failure: 0
464 UINT WINAPI GetSystemPaletteEntries(
465 HDC hdc, /* [in] Handle of device context */
466 UINT start, /* [in] Index of first entry to be retrieved */
467 UINT count, /* [in] Number of entries to be retrieved */
468 LPPALETTEENTRY entries) /* [out] Array receiving system-palette entries */
470 UINT i;
471 DC *dc;
473 TRACE(palette, "hdc=%04x,start=%i,count=%i\n", hdc,start,count);
475 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
476 if (!entries) return dc->w.devCaps->sizePalette;
477 if (start >= dc->w.devCaps->sizePalette)
479 GDI_HEAP_UNLOCK( hdc );
480 return 0;
482 if (start+count >= dc->w.devCaps->sizePalette)
483 count = dc->w.devCaps->sizePalette - start;
484 for (i = 0; i < count; i++)
486 *(COLORREF*)(entries + i) = COLOR_GetSystemPaletteEntry( start + i );
488 TRACE(palette,"\tidx(%02x) -> RGB(%08lx)\n",
489 start + i, *(COLORREF*)(entries + i) );
491 GDI_HEAP_UNLOCK( hdc );
492 return count;
496 /***********************************************************************
497 * GetNearestPaletteIndex16 (GDI.370)
499 UINT16 WINAPI GetNearestPaletteIndex16( HPALETTE16 hpalette, COLORREF color )
501 return GetNearestPaletteIndex( hpalette, color );
505 /***********************************************************************
506 * GetNearestPaletteIndex32 [GDI32.203] Gets palette index for color
508 * NOTES
509 * Should index be initialized to CLR_INVALID instead of 0?
511 * RETURNS
512 * Success: Index of entry in logical palette
513 * Failure: CLR_INVALID
515 UINT WINAPI GetNearestPaletteIndex(
516 HPALETTE hpalette, /* [in] Handle of logical color palette */
517 COLORREF color) /* [in] Color to be matched */
519 PALETTEOBJ* palObj = (PALETTEOBJ*)GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
520 UINT index = 0;
522 if( palObj )
523 index = COLOR_PaletteLookupPixel(palObj->logpalette.palPalEntry,
524 palObj->logpalette.palNumEntries,
525 NULL, color, FALSE );
527 TRACE(palette,"(%04x,%06lx): returning %d\n", hpalette, color, index );
528 GDI_HEAP_UNLOCK( hpalette );
529 return index;
533 /***********************************************************************
534 * GetNearestColor16 (GDI.154)
536 COLORREF WINAPI GetNearestColor16( HDC16 hdc, COLORREF color )
538 return GetNearestColor( hdc, color );
542 /***********************************************************************
543 * GetNearestColor32 [GDI32.202] Gets a system color to match
545 * NOTES
546 * Should this return CLR_INVALID instead of FadeCafe?
548 * RETURNS
549 * Success: Color from system palette that corresponds to given color
550 * Failure: CLR_INVALID
552 COLORREF WINAPI GetNearestColor(
553 HDC hdc, /* [in] Handle of device context */
554 COLORREF color) /* [in] Color to be matched */
556 COLORREF nearest = 0xFADECAFE;
557 DC *dc;
558 PALETTEOBJ *palObj;
560 if ( (dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC )) )
562 palObj = (PALETTEOBJ*)
563 GDI_GetObjPtr( (dc->w.hPalette)? dc->w.hPalette
564 : STOCK_DEFAULT_PALETTE, PALETTE_MAGIC );
565 if (!palObj) return nearest;
567 nearest = COLOR_LookupNearestColor( palObj->logpalette.palPalEntry,
568 palObj->logpalette.palNumEntries, color );
569 GDI_HEAP_UNLOCK( dc->w.hPalette );
572 TRACE(palette,"(%06lx): returning %06lx\n", color, nearest );
573 GDI_HEAP_UNLOCK( hdc );
574 return nearest;
578 /***********************************************************************
579 * PALETTE_GetObject
581 int PALETTE_GetObject( PALETTEOBJ * palette, int count, LPSTR buffer )
583 if (count > sizeof(WORD)) count = sizeof(WORD);
584 memcpy( buffer, &palette->logpalette.palNumEntries, count );
585 return count;
589 /***********************************************************************
590 * PALETTE_UnrealizeObject
592 BOOL PALETTE_UnrealizeObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
594 if (palette->mapping)
596 free( palette->mapping );
597 palette->mapping = NULL;
599 if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
600 return TRUE;
604 /***********************************************************************
605 * PALETTE_DeleteObject
607 BOOL PALETTE_DeleteObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
609 free( palette->mapping );
610 if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
611 return GDI_FreeObject( hpalette );
615 /***********************************************************************
616 * GDISelectPalette (GDI.361)
618 HPALETTE16 WINAPI GDISelectPalette16( HDC16 hdc, HPALETTE16 hpal, WORD wBkg)
620 HPALETTE16 prev;
621 DC *dc;
623 TRACE(palette, "%04x %04x\n", hdc, hpal );
625 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
626 if (!dc)
628 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
629 if (!dc) return 0;
631 prev = dc->w.hPalette;
632 dc->w.hPalette = hpal;
633 GDI_HEAP_UNLOCK( hdc );
634 if (!wBkg) hPrimaryPalette = hpal;
635 return prev;
639 /***********************************************************************
640 * GDIRealizePalette (GDI.362)
642 UINT16 WINAPI GDIRealizePalette16( HDC16 hdc )
644 PALETTEOBJ* palPtr;
645 int realized = 0;
646 DC* dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
647 if (!dc)
649 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
650 if (!dc) return 0;
653 TRACE(palette, "%04x...\n", hdc );
655 if( dc && dc->w.hPalette != hLastRealizedPalette )
657 if( dc->w.hPalette == STOCK_DEFAULT_PALETTE )
658 return RealizeDefaultPalette16( hdc );
660 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC );
662 if (!palPtr) {
663 FIXME(palette,"invalid selected palette %04x\n",dc->w.hPalette);
664 return 0;
667 realized = PALETTE_Driver->
668 pSetMapping(palPtr,0,palPtr->logpalette.palNumEntries,
669 (dc->w.hPalette != hPrimaryPalette) ||
670 (dc->w.hPalette == STOCK_DEFAULT_PALETTE));
671 GDI_HEAP_UNLOCK( dc->w.hPalette );
672 hLastRealizedPalette = dc->w.hPalette;
674 else TRACE(palette, " skipping (hLastRealizedPalette = %04x)\n",
675 hLastRealizedPalette);
676 GDI_HEAP_UNLOCK( hdc );
678 TRACE(palette, " realized %i colors.\n", realized );
679 return (UINT16)realized;
683 /***********************************************************************
684 * RealizeDefaultPalette (GDI.365)
686 UINT16 WINAPI RealizeDefaultPalette16( HDC16 hdc )
688 DC *dc;
689 PALETTEOBJ* palPtr;
691 TRACE(palette,"%04x\n", hdc );
693 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
694 if (!dc)
696 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
697 if (!dc) return 0;
700 if ( dc->w.flags & DC_MEMORY )
702 GDI_HEAP_UNLOCK( hdc );
703 return 0;
706 hPrimaryPalette = STOCK_DEFAULT_PALETTE;
707 hLastRealizedPalette = STOCK_DEFAULT_PALETTE;
709 palPtr = (PALETTEOBJ*)GDI_GetObjPtr(STOCK_DEFAULT_PALETTE, PALETTE_MAGIC );
710 if (!palPtr) return 0;
712 /* lookup is needed to account for SetSystemPaletteUse() stuff */
714 return PALETTE_Driver->pUpdateMapping(palPtr);
717 /***********************************************************************
718 * IsDCCurrentPalette (GDI.412)
720 BOOL16 WINAPI IsDCCurrentPalette16(HDC16 hDC)
722 DC* dc = (DC *)GDI_GetObjPtr( hDC, DC_MAGIC );
723 if (dc)
725 GDI_HEAP_UNLOCK( hDC );
726 return dc->w.hPalette == hPrimaryPalette;
728 return FALSE;
732 /***********************************************************************
733 * SelectPalette16 (USER.282)
735 HPALETTE16 WINAPI SelectPalette16( HDC16 hDC, HPALETTE16 hPal,
736 BOOL16 bForceBackground )
738 return SelectPalette( hDC, hPal, bForceBackground );
742 /***********************************************************************
743 * SelectPalette32 [GDI32.300] Selects logical palette into DC
745 * RETURNS
746 * Success: Previous logical palette
747 * Failure: NULL
749 HPALETTE WINAPI SelectPalette(
750 HDC hDC, /* [in] Handle of device context */
751 HPALETTE hPal, /* [in] Handle of logical color palette */
752 BOOL bForceBackground) /* [in] Foreground/background mode */
754 WORD wBkgPalette = 1;
755 PALETTEOBJ* lpt = (PALETTEOBJ*) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
757 TRACE(palette,"dc=%04x,pal=%04x,force=%i\n", hDC, hPal, bForceBackground);
758 if( !lpt ) return 0;
760 TRACE(palette," entries = %d\n", lpt->logpalette.palNumEntries);
761 GDI_HEAP_UNLOCK( hPal );
763 if( hPal != STOCK_DEFAULT_PALETTE )
765 HWND hWnd = WindowFromDC( hDC );
766 HWND hActive = GetActiveWindow();
768 /* set primary palette if it's related to current active */
770 if((!hWnd || (hActive == hWnd || IsChild16(hActive,hWnd))) &&
771 !bForceBackground )
772 wBkgPalette = 0;
774 return GDISelectPalette16( hDC, hPal, wBkgPalette);
778 /***********************************************************************
779 * RealizePalette16 (USER.283)
781 UINT16 WINAPI RealizePalette16( HDC16 hDC )
783 return RealizePalette( hDC );
787 /***********************************************************************
788 * RealizePalette32 [GDI32.280] Maps palette entries to system palette
790 * RETURNS
791 * Success: Number of entries in logical palette
792 * Failure: GDI_ERROR
794 UINT WINAPI RealizePalette(
795 HDC hDC) /* [in] Handle of device context */
797 DC *dc;
798 UINT realized;
800 if (!(dc = (DC *) GDI_GetObjPtr( hDC, DC_MAGIC ))) return 0;
802 realized = GDIRealizePalette16( hDC );
804 /* do not send anything if no colors were changed */
806 if( IsDCCurrentPalette16( hDC ) && realized &&
807 dc->w.devCaps->sizePalette )
809 /* Send palette change notification */
811 HWND hWnd;
812 if( (hWnd = WindowFromDC( hDC )) )
813 SendMessage16( HWND_BROADCAST, WM_PALETTECHANGED, hWnd, 0L);
816 GDI_HEAP_UNLOCK( hDC );
817 return realized;
821 /**********************************************************************
822 * UpdateColors16 (GDI.366)
824 INT16 WINAPI UpdateColors16( HDC16 hDC )
826 DC *dc;
827 HWND hWnd;
829 if (!(dc = (DC *) GDI_GetObjPtr( hDC, DC_MAGIC ))) return 0;
831 hWnd = WindowFromDC( hDC );
833 /* Docs say that we have to remap current drawable pixel by pixel
834 * but it would take forever given the speed of XGet/PutPixel.
836 if (hWnd && dc->w.devCaps->sizePalette )
837 InvalidateRect( hWnd, NULL, FALSE );
839 GDI_HEAP_UNLOCK( hDC );
841 return 0x666;
845 /**********************************************************************
846 * UpdateColors32 [GDI32.359] Remaps current colors to logical palette
848 * RETURNS
849 * Success: TRUE
850 * Failure: FALSE
852 BOOL WINAPI UpdateColors(
853 HDC hDC) /* [in] Handle of device context */
855 UpdateColors16( hDC );
856 return TRUE;
860 /*********************************************************************
861 * SetMagicColors16 (GDI.606)
863 VOID WINAPI SetMagicColors16(HDC16 hDC, COLORREF color, UINT16 index)
865 FIXME(palette,"(hDC %04x, color %04x, index %04x): stub\n", hDC, (int)color, index);