New debug scheme with explicit debug channels declaration.
[wine/hacks.git] / objects / palette.c
blobcc51a5af453d0d8dda8a6f17f1e49f20a45ae4f2
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 DEFAULT_DEBUG_CHANNEL(palette)
23 PALETTE_DRIVER *PALETTE_Driver = NULL;
25 FARPROC pfnSelectPalette = NULL;
26 FARPROC pfnRealizePalette = NULL;
28 static UINT SystemPaletteUse = SYSPAL_STATIC; /* currently not considered */
30 static HPALETTE16 hPrimaryPalette = 0; /* used for WM_PALETTECHANGED */
31 static HPALETTE16 hLastRealizedPalette = 0; /* UnrealizeObject() needs it */
34 /***********************************************************************
35 * PALETTE_Init
37 * Create the system palette.
39 HPALETTE16 PALETTE_Init(void)
41 int i;
42 HPALETTE16 hpalette;
43 LOGPALETTE * palPtr;
44 PALETTEOBJ* palObj;
45 const PALETTEENTRY* __sysPalTemplate = COLOR_GetSystemPaletteTemplate();
47 /* create default palette (20 system colors) */
49 palPtr = HeapAlloc( GetProcessHeap(), 0,
50 sizeof(LOGPALETTE) + (NB_RESERVED_COLORS-1)*sizeof(PALETTEENTRY));
51 if (!palPtr) return FALSE;
53 palPtr->palVersion = 0x300;
54 palPtr->palNumEntries = NB_RESERVED_COLORS;
55 for( i = 0; i < NB_RESERVED_COLORS; i ++ )
57 palPtr->palPalEntry[i].peRed = __sysPalTemplate[i].peRed;
58 palPtr->palPalEntry[i].peGreen = __sysPalTemplate[i].peGreen;
59 palPtr->palPalEntry[i].peBlue = __sysPalTemplate[i].peBlue;
60 palPtr->palPalEntry[i].peFlags = 0;
62 hpalette = CreatePalette16( palPtr );
64 palObj = (PALETTEOBJ*) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
65 if (palObj)
67 palObj->mapping = xmalloc( sizeof(int) * 20 );
69 GDI_HEAP_UNLOCK( hpalette );
71 HeapFree( GetProcessHeap(), 0, palPtr );
74 return hpalette;
77 /***********************************************************************
78 * PALETTE_ValidateFlags
80 void PALETTE_ValidateFlags(PALETTEENTRY* lpPalE, int size)
82 int i = 0;
83 for( ; i<size ; i++ )
84 lpPalE[i].peFlags = PC_SYS_USED | (lpPalE[i].peFlags & 0x07);
88 /***********************************************************************
89 * CreatePalette16 (GDI.360)
91 HPALETTE16 WINAPI CreatePalette16( const LOGPALETTE* palette )
93 return CreatePalette( palette );
97 /***********************************************************************
98 * CreatePalette32 [GDI32.53] Creates a logical color palette
100 * RETURNS
101 * Success: Handle to logical palette
102 * Failure: NULL
104 HPALETTE WINAPI CreatePalette(
105 const LOGPALETTE* palette) /* [in] Pointer to logical color palette */
107 PALETTEOBJ * palettePtr;
108 HPALETTE hpalette;
109 int size;
111 if (!palette) return 0;
112 TRACE(palette,"entries=%i\n", palette->palNumEntries);
114 size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
116 hpalette = GDI_AllocObject( size + sizeof(int*) +sizeof(GDIOBJHDR) , PALETTE_MAGIC );
117 if (!hpalette) return 0;
119 palettePtr = (PALETTEOBJ *) GDI_HEAP_LOCK( hpalette );
120 memcpy( &palettePtr->logpalette, palette, size );
121 PALETTE_ValidateFlags(palettePtr->logpalette.palPalEntry,
122 palettePtr->logpalette.palNumEntries);
123 palettePtr->mapping = NULL;
124 GDI_HEAP_UNLOCK( hpalette );
126 TRACE(palette," returning %04x\n", hpalette);
127 return hpalette;
131 /***********************************************************************
132 * CreateHalftonePalette16 [GDI.?] Creates a halftone palette
134 * RETURNS
135 * Success: Handle to logical halftone palette
136 * Failure: 0
138 HPALETTE16 WINAPI CreateHalftonePalette16(
139 HDC16 hdc) /* [in] Handle to device context */
141 return CreateHalftonePalette(hdc);
145 /***********************************************************************
146 * CreateHalftonePalette32 [GDI32.47] Creates a halftone palette
148 * RETURNS
149 * Success: Handle to logical halftone palette
150 * Failure: 0
152 * FIXME: not truly tested
154 HPALETTE WINAPI CreateHalftonePalette(
155 HDC hdc) /* [in] Handle to device context */
157 int i, r, g, b;
158 struct {
159 WORD Version;
160 WORD NumberOfEntries;
161 PALETTEENTRY aEntries[256];
162 } Palette = {
163 0x300, 256
166 GetSystemPaletteEntries(hdc, 0, 256, Palette.aEntries);
167 return CreatePalette((LOGPALETTE *)&Palette);
169 for (r = 0; r < 6; r++) {
170 for (g = 0; g < 6; g++) {
171 for (b = 0; b < 6; b++) {
172 i = r + g*6 + b*36 + 10;
173 Palette.aEntries[i].peRed = r * 51;
174 Palette.aEntries[i].peGreen = g * 51;
175 Palette.aEntries[i].peBlue = b * 51;
180 for (i = 216; i < 246; i++) {
181 int v = (i - 216) * 8;
182 Palette.aEntries[i].peRed = v;
183 Palette.aEntries[i].peGreen = v;
184 Palette.aEntries[i].peBlue = v;
187 return CreatePalette((LOGPALETTE *)&Palette);
191 /***********************************************************************
192 * GetPaletteEntries16 (GDI.363)
194 UINT16 WINAPI GetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
195 UINT16 count, LPPALETTEENTRY entries )
197 return GetPaletteEntries( hpalette, start, count, entries );
201 /***********************************************************************
202 * GetPaletteEntries32 [GDI32.209] Retrieves palette entries
204 * RETURNS
205 * Success: Number of entries from logical palette
206 * Failure: 0
208 UINT WINAPI GetPaletteEntries(
209 HPALETTE hpalette, /* [in] Handle of logical palette */
210 UINT start, /* [in] First entry to receive */
211 UINT count, /* [in] Number of entries to receive */
212 LPPALETTEENTRY entries) /* [out] Address of array receiving entries */
214 PALETTEOBJ * palPtr;
215 INT numEntries;
217 TRACE(palette,"hpal = %04x, count=%i\n", hpalette, count );
219 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
220 if (!palPtr) return 0;
222 numEntries = palPtr->logpalette.palNumEntries;
223 if (start+count > numEntries) count = numEntries - start;
224 if (entries)
226 if (start >= numEntries)
228 GDI_HEAP_UNLOCK( hpalette );
229 return 0;
231 memcpy( entries, &palPtr->logpalette.palPalEntry[start],
232 count * sizeof(PALETTEENTRY) );
233 for( numEntries = 0; numEntries < count ; numEntries++ )
234 if (entries[numEntries].peFlags & 0xF0)
235 entries[numEntries].peFlags = 0;
236 GDI_HEAP_UNLOCK( hpalette );
239 return count;
243 /***********************************************************************
244 * SetPaletteEntries16 (GDI.364)
246 UINT16 WINAPI SetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
247 UINT16 count, LPPALETTEENTRY entries )
249 return SetPaletteEntries( hpalette, start, count, entries );
253 /***********************************************************************
254 * SetPaletteEntries32 [GDI32.326] Sets color values for range in palette
256 * RETURNS
257 * Success: Number of entries that were set
258 * Failure: 0
260 UINT WINAPI SetPaletteEntries(
261 HPALETTE hpalette, /* [in] Handle of logical palette */
262 UINT start, /* [in] Index of first entry to set */
263 UINT count, /* [in] Number of entries to set */
264 LPPALETTEENTRY entries) /* [in] Address of array of structures */
266 PALETTEOBJ * palPtr;
267 INT numEntries;
269 TRACE(palette,"hpal=%04x,start=%i,count=%i\n",hpalette,start,count );
271 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
272 if (!palPtr) return 0;
274 numEntries = palPtr->logpalette.palNumEntries;
275 if (start >= numEntries)
277 GDI_HEAP_UNLOCK( hpalette );
278 return 0;
280 if (start+count > numEntries) count = numEntries - start;
281 memcpy( &palPtr->logpalette.palPalEntry[start], entries,
282 count * sizeof(PALETTEENTRY) );
283 PALETTE_ValidateFlags(palPtr->logpalette.palPalEntry,
284 palPtr->logpalette.palNumEntries);
285 free(palPtr->mapping);
286 palPtr->mapping = NULL;
287 GDI_HEAP_UNLOCK( hpalette );
288 return count;
292 /***********************************************************************
293 * ResizePalette16 (GDI.368)
295 BOOL16 WINAPI ResizePalette16( HPALETTE16 hPal, UINT16 cEntries )
297 return ResizePalette( hPal, cEntries );
301 /***********************************************************************
302 * ResizePalette32 [GDI32.289] Resizes logical palette
304 * RETURNS
305 * Success: TRUE
306 * Failure: FALSE
308 BOOL WINAPI ResizePalette(
309 HPALETTE hPal, /* [in] Handle of logical palette */
310 UINT cEntries) /* [in] Number of entries in logical palette */
312 PALETTEOBJ * palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
313 UINT cPrevEnt, prevVer;
314 int prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
315 int* mapping = NULL;
317 TRACE(palette,"hpal = %04x, prev = %i, new = %i\n",
318 hPal, palPtr ? palPtr->logpalette.palNumEntries : -1,
319 cEntries );
320 if( !palPtr ) return FALSE;
321 cPrevEnt = palPtr->logpalette.palNumEntries;
322 prevVer = palPtr->logpalette.palVersion;
323 prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) +
324 sizeof(int*) + sizeof(GDIOBJHDR);
325 size += sizeof(int*) + sizeof(GDIOBJHDR);
326 mapping = palPtr->mapping;
328 GDI_HEAP_UNLOCK( hPal );
330 hPal = GDI_HEAP_REALLOC( hPal, size );
331 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
332 if( !palPtr ) return FALSE;
334 if( mapping )
335 palPtr->mapping = (int*) xrealloc( mapping, cEntries * sizeof(int) );
336 if( cEntries > cPrevEnt )
338 if( mapping )
339 memset(palPtr->mapping + cPrevEnt, 0, (cEntries - cPrevEnt)*sizeof(int));
340 memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
341 PALETTE_ValidateFlags((PALETTEENTRY*)((BYTE*)palPtr + prevsize),
342 cEntries - cPrevEnt );
344 palPtr->logpalette.palNumEntries = cEntries;
345 palPtr->logpalette.palVersion = prevVer;
346 GDI_HEAP_UNLOCK( hPal );
347 return TRUE;
351 /***********************************************************************
352 * AnimatePalette16 (GDI.367)
354 void WINAPI AnimatePalette16( HPALETTE16 hPal, UINT16 StartIndex,
355 UINT16 NumEntries, const PALETTEENTRY* PaletteColors)
357 AnimatePalette( hPal, StartIndex, NumEntries, PaletteColors );
361 /***********************************************************************
362 * AnimatePalette32 [GDI32.6] Replaces entries in logical palette
364 * RETURNS
365 * Success: TRUE
366 * Failure: FALSE
368 * FIXME
369 * Should use existing mapping when animating a primary palette
371 BOOL WINAPI AnimatePalette(
372 HPALETTE hPal, /* [in] Handle to logical palette */
373 UINT StartIndex, /* [in] First entry in palette */
374 UINT NumEntries, /* [in] Count of entries in palette */
375 const PALETTEENTRY* PaletteColors) /* [in] Pointer to first replacement */
377 TRACE(palette, "%04x (%i - %i)\n", hPal, StartIndex,StartIndex+NumEntries);
379 if( hPal != STOCK_DEFAULT_PALETTE )
381 PALETTEOBJ* palPtr = (PALETTEOBJ *)GDI_GetObjPtr(hPal, PALETTE_MAGIC);
382 if (!palPtr) return FALSE;
384 if( (StartIndex + NumEntries) <= palPtr->logpalette.palNumEntries )
386 UINT u;
387 for( u = 0; u < NumEntries; u++ )
388 palPtr->logpalette.palPalEntry[u + StartIndex] = PaletteColors[u];
389 PALETTE_Driver->
390 pSetMapping(palPtr, StartIndex, NumEntries,
391 hPal != hPrimaryPalette );
392 GDI_HEAP_UNLOCK( hPal );
393 return TRUE;
396 return FALSE;
400 /***********************************************************************
401 * SetSystemPaletteUse16 (GDI.373)
403 UINT16 WINAPI SetSystemPaletteUse16( HDC16 hdc, UINT16 use )
405 return SetSystemPaletteUse( hdc, use );
409 /***********************************************************************
410 * SetSystemPaletteUse32 [GDI32.335]
412 * RETURNS
413 * Success: Previous system palette
414 * Failure: SYSPAL_ERROR
416 UINT WINAPI SetSystemPaletteUse(
417 HDC hdc, /* [in] Handle of device context */
418 UINT use) /* [in] Palette-usage flag */
420 UINT old = SystemPaletteUse;
421 FIXME(palette,"(%04x,%04x): stub\n", hdc, use );
422 SystemPaletteUse = use;
423 return old;
427 /***********************************************************************
428 * GetSystemPaletteUse16 (GDI.374)
430 UINT16 WINAPI GetSystemPaletteUse16( HDC16 hdc )
432 return SystemPaletteUse;
436 /***********************************************************************
437 * GetSystemPaletteUse32 [GDI32.223] Gets state of system palette
439 * RETURNS
440 * Current state of system palette
442 UINT WINAPI GetSystemPaletteUse(
443 HDC hdc) /* [in] Handle of device context */
445 return SystemPaletteUse;
449 /***********************************************************************
450 * GetSystemPaletteEntries16 (GDI.375)
452 UINT16 WINAPI GetSystemPaletteEntries16( HDC16 hdc, UINT16 start, UINT16 count,
453 LPPALETTEENTRY entries )
455 return GetSystemPaletteEntries( hdc, start, count, entries );
459 /***********************************************************************
460 * GetSystemPaletteEntries32 [GDI32.222] Gets range of palette entries
462 * RETURNS
463 * Success: Number of entries retrieved from palette
464 * Failure: 0
466 UINT WINAPI GetSystemPaletteEntries(
467 HDC hdc, /* [in] Handle of device context */
468 UINT start, /* [in] Index of first entry to be retrieved */
469 UINT count, /* [in] Number of entries to be retrieved */
470 LPPALETTEENTRY entries) /* [out] Array receiving system-palette entries */
472 UINT i;
473 DC *dc;
475 TRACE(palette, "hdc=%04x,start=%i,count=%i\n", hdc,start,count);
477 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
478 if (!entries) return dc->w.devCaps->sizePalette;
479 if (start >= dc->w.devCaps->sizePalette)
481 GDI_HEAP_UNLOCK( hdc );
482 return 0;
484 if (start+count >= dc->w.devCaps->sizePalette)
485 count = dc->w.devCaps->sizePalette - start;
486 for (i = 0; i < count; i++)
488 *(COLORREF*)(entries + i) = COLOR_GetSystemPaletteEntry( start + i );
490 TRACE(palette,"\tidx(%02x) -> RGB(%08lx)\n",
491 start + i, *(COLORREF*)(entries + i) );
493 GDI_HEAP_UNLOCK( hdc );
494 return count;
498 /***********************************************************************
499 * GetNearestPaletteIndex16 (GDI.370)
501 UINT16 WINAPI GetNearestPaletteIndex16( HPALETTE16 hpalette, COLORREF color )
503 return GetNearestPaletteIndex( hpalette, color );
507 /***********************************************************************
508 * GetNearestPaletteIndex32 [GDI32.203] Gets palette index for color
510 * NOTES
511 * Should index be initialized to CLR_INVALID instead of 0?
513 * RETURNS
514 * Success: Index of entry in logical palette
515 * Failure: CLR_INVALID
517 UINT WINAPI GetNearestPaletteIndex(
518 HPALETTE hpalette, /* [in] Handle of logical color palette */
519 COLORREF color) /* [in] Color to be matched */
521 PALETTEOBJ* palObj = (PALETTEOBJ*)GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
522 UINT index = 0;
524 if( palObj )
525 index = COLOR_PaletteLookupPixel(palObj->logpalette.palPalEntry,
526 palObj->logpalette.palNumEntries,
527 NULL, color, FALSE );
529 TRACE(palette,"(%04x,%06lx): returning %d\n", hpalette, color, index );
530 GDI_HEAP_UNLOCK( hpalette );
531 return index;
535 /***********************************************************************
536 * GetNearestColor16 (GDI.154)
538 COLORREF WINAPI GetNearestColor16( HDC16 hdc, COLORREF color )
540 return GetNearestColor( hdc, color );
544 /***********************************************************************
545 * GetNearestColor32 [GDI32.202] Gets a system color to match
547 * NOTES
548 * Should this return CLR_INVALID instead of FadeCafe?
550 * RETURNS
551 * Success: Color from system palette that corresponds to given color
552 * Failure: CLR_INVALID
554 COLORREF WINAPI GetNearestColor(
555 HDC hdc, /* [in] Handle of device context */
556 COLORREF color) /* [in] Color to be matched */
558 COLORREF nearest = 0xFADECAFE;
559 DC *dc;
560 PALETTEOBJ *palObj;
562 if ( (dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC )) )
564 palObj = (PALETTEOBJ*)
565 GDI_GetObjPtr( (dc->w.hPalette)? dc->w.hPalette
566 : STOCK_DEFAULT_PALETTE, PALETTE_MAGIC );
567 if (!palObj) return nearest;
569 nearest = COLOR_LookupNearestColor( palObj->logpalette.palPalEntry,
570 palObj->logpalette.palNumEntries, color );
571 GDI_HEAP_UNLOCK( dc->w.hPalette );
574 TRACE(palette,"(%06lx): returning %06lx\n", color, nearest );
575 GDI_HEAP_UNLOCK( hdc );
576 return nearest;
580 /***********************************************************************
581 * PALETTE_GetObject
583 int PALETTE_GetObject( PALETTEOBJ * palette, int count, LPSTR buffer )
585 if (count > sizeof(WORD)) count = sizeof(WORD);
586 memcpy( buffer, &palette->logpalette.palNumEntries, count );
587 return count;
591 /***********************************************************************
592 * PALETTE_UnrealizeObject
594 BOOL PALETTE_UnrealizeObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
596 if (palette->mapping)
598 free( palette->mapping );
599 palette->mapping = NULL;
601 if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
602 return TRUE;
606 /***********************************************************************
607 * PALETTE_DeleteObject
609 BOOL PALETTE_DeleteObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
611 free( palette->mapping );
612 if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
613 return GDI_FreeObject( hpalette );
617 /***********************************************************************
618 * GDISelectPalette (GDI.361)
620 HPALETTE16 WINAPI GDISelectPalette16( HDC16 hdc, HPALETTE16 hpal, WORD wBkg)
622 HPALETTE16 prev;
623 DC *dc;
625 TRACE(palette, "%04x %04x\n", hdc, hpal );
627 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
628 if (!dc)
630 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
631 if (!dc) return 0;
633 prev = dc->w.hPalette;
634 dc->w.hPalette = hpal;
635 GDI_HEAP_UNLOCK( hdc );
636 if (!wBkg) hPrimaryPalette = hpal;
637 return prev;
641 /***********************************************************************
642 * GDIRealizePalette (GDI.362)
644 UINT16 WINAPI GDIRealizePalette16( HDC16 hdc )
646 PALETTEOBJ* palPtr;
647 int realized = 0;
648 DC* dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
649 if (!dc)
651 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
652 if (!dc) return 0;
655 TRACE(palette, "%04x...\n", hdc );
657 if( dc && dc->w.hPalette != hLastRealizedPalette )
659 if( dc->w.hPalette == STOCK_DEFAULT_PALETTE )
660 return RealizeDefaultPalette16( hdc );
662 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC );
664 if (!palPtr) {
665 FIXME(palette,"invalid selected palette %04x\n",dc->w.hPalette);
666 return 0;
669 realized = PALETTE_Driver->
670 pSetMapping(palPtr,0,palPtr->logpalette.palNumEntries,
671 (dc->w.hPalette != hPrimaryPalette) ||
672 (dc->w.hPalette == STOCK_DEFAULT_PALETTE));
673 GDI_HEAP_UNLOCK( dc->w.hPalette );
674 hLastRealizedPalette = dc->w.hPalette;
676 else TRACE(palette, " skipping (hLastRealizedPalette = %04x)\n",
677 hLastRealizedPalette);
678 GDI_HEAP_UNLOCK( hdc );
680 TRACE(palette, " realized %i colors.\n", realized );
681 return (UINT16)realized;
685 /***********************************************************************
686 * RealizeDefaultPalette (GDI.365)
688 UINT16 WINAPI RealizeDefaultPalette16( HDC16 hdc )
690 DC *dc;
691 PALETTEOBJ* palPtr;
693 TRACE(palette,"%04x\n", hdc );
695 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
696 if (!dc)
698 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
699 if (!dc) return 0;
702 if ( dc->w.flags & DC_MEMORY )
704 GDI_HEAP_UNLOCK( hdc );
705 return 0;
708 hPrimaryPalette = STOCK_DEFAULT_PALETTE;
709 hLastRealizedPalette = STOCK_DEFAULT_PALETTE;
711 palPtr = (PALETTEOBJ*)GDI_GetObjPtr(STOCK_DEFAULT_PALETTE, PALETTE_MAGIC );
712 if (!palPtr) return 0;
714 /* lookup is needed to account for SetSystemPaletteUse() stuff */
716 return PALETTE_Driver->pUpdateMapping(palPtr);
719 /***********************************************************************
720 * IsDCCurrentPalette (GDI.412)
722 BOOL16 WINAPI IsDCCurrentPalette16(HDC16 hDC)
724 DC* dc = (DC *)GDI_GetObjPtr( hDC, DC_MAGIC );
725 if (dc)
727 GDI_HEAP_UNLOCK( hDC );
728 return dc->w.hPalette == hPrimaryPalette;
730 return FALSE;
734 /***********************************************************************
735 * SelectPalette16 (USER.282)
737 HPALETTE16 WINAPI SelectPalette16( HDC16 hDC, HPALETTE16 hPal,
738 BOOL16 bForceBackground )
740 return SelectPalette( hDC, hPal, bForceBackground );
744 /***********************************************************************
745 * SelectPalette32 [GDI32.300] Selects logical palette into DC
747 * RETURNS
748 * Success: Previous logical palette
749 * Failure: NULL
751 HPALETTE WINAPI SelectPalette(
752 HDC hDC, /* [in] Handle of device context */
753 HPALETTE hPal, /* [in] Handle of logical color palette */
754 BOOL bForceBackground) /* [in] Foreground/background mode */
756 WORD wBkgPalette = 1;
757 PALETTEOBJ* lpt = (PALETTEOBJ*) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
759 TRACE(palette,"dc=%04x,pal=%04x,force=%i\n", hDC, hPal, bForceBackground);
760 if( !lpt ) return 0;
762 TRACE(palette," entries = %d\n", lpt->logpalette.palNumEntries);
763 GDI_HEAP_UNLOCK( hPal );
765 if( hPal != STOCK_DEFAULT_PALETTE )
767 HWND hWnd = WindowFromDC( hDC );
768 HWND hActive = GetActiveWindow();
770 /* set primary palette if it's related to current active */
772 if((!hWnd || (hActive == hWnd || IsChild16(hActive,hWnd))) &&
773 !bForceBackground )
774 wBkgPalette = 0;
776 return GDISelectPalette16( hDC, hPal, wBkgPalette);
780 /***********************************************************************
781 * RealizePalette16 (USER.283)
783 UINT16 WINAPI RealizePalette16( HDC16 hDC )
785 return RealizePalette( hDC );
789 /***********************************************************************
790 * RealizePalette32 [GDI32.280] Maps palette entries to system palette
792 * RETURNS
793 * Success: Number of entries in logical palette
794 * Failure: GDI_ERROR
796 UINT WINAPI RealizePalette(
797 HDC hDC) /* [in] Handle of device context */
799 DC *dc;
800 UINT realized;
802 if (!(dc = (DC *) GDI_GetObjPtr( hDC, DC_MAGIC ))) return 0;
804 realized = GDIRealizePalette16( hDC );
806 /* do not send anything if no colors were changed */
808 if( IsDCCurrentPalette16( hDC ) && realized &&
809 dc->w.devCaps->sizePalette )
811 /* Send palette change notification */
813 HWND hWnd;
814 if( (hWnd = WindowFromDC( hDC )) )
815 SendMessage16( HWND_BROADCAST, WM_PALETTECHANGED, hWnd, 0L);
818 GDI_HEAP_UNLOCK( hDC );
819 return realized;
823 /**********************************************************************
824 * UpdateColors16 (GDI.366)
826 INT16 WINAPI UpdateColors16( HDC16 hDC )
828 DC *dc;
829 HWND hWnd;
831 if (!(dc = (DC *) GDI_GetObjPtr( hDC, DC_MAGIC ))) return 0;
833 hWnd = WindowFromDC( hDC );
835 /* Docs say that we have to remap current drawable pixel by pixel
836 * but it would take forever given the speed of XGet/PutPixel.
838 if (hWnd && dc->w.devCaps->sizePalette )
839 InvalidateRect( hWnd, NULL, FALSE );
841 GDI_HEAP_UNLOCK( hDC );
843 return 0x666;
847 /**********************************************************************
848 * UpdateColors32 [GDI32.359] Remaps current colors to logical palette
850 * RETURNS
851 * Success: TRUE
852 * Failure: FALSE
854 BOOL WINAPI UpdateColors(
855 HDC hDC) /* [in] Handle of device context */
857 UpdateColors16( hDC );
858 return TRUE;
862 /*********************************************************************
863 * SetMagicColors16 (GDI.606)
865 VOID WINAPI SetMagicColors16(HDC16 hDC, COLORREF color, UINT16 index)
867 FIXME(palette,"(hDC %04x, color %04x, index %04x): stub\n", hDC, (int)color, index);