Added stub for WIN32S16.EXP1 (most likely LoadPeResource16).
[wine/dcerpc.git] / objects / palette.c
blob1fbdc5c787f888a592439505499d831dfa46d71f
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"
22 DEFAULT_DEBUG_CHANNEL(palette)
24 PALETTE_DRIVER *PALETTE_Driver = NULL;
26 FARPROC pfnSelectPalette = NULL;
27 FARPROC pfnRealizePalette = NULL;
29 static UINT SystemPaletteUse = SYSPAL_STATIC; /* currently not considered */
31 static HPALETTE16 hPrimaryPalette = 0; /* used for WM_PALETTECHANGED */
32 static HPALETTE16 hLastRealizedPalette = 0; /* UnrealizeObject() needs it */
35 /***********************************************************************
36 * PALETTE_Init
38 * Create the system palette.
40 HPALETTE16 PALETTE_Init(void)
42 int i;
43 HPALETTE16 hpalette;
44 LOGPALETTE * palPtr;
45 PALETTEOBJ* palObj;
46 const PALETTEENTRY* __sysPalTemplate = COLOR_GetSystemPaletteTemplate();
48 /* create default palette (20 system colors) */
50 palPtr = HeapAlloc( GetProcessHeap(), 0,
51 sizeof(LOGPALETTE) + (NB_RESERVED_COLORS-1)*sizeof(PALETTEENTRY));
52 if (!palPtr) return FALSE;
54 palPtr->palVersion = 0x300;
55 palPtr->palNumEntries = NB_RESERVED_COLORS;
56 for( i = 0; i < NB_RESERVED_COLORS; i ++ )
58 palPtr->palPalEntry[i].peRed = __sysPalTemplate[i].peRed;
59 palPtr->palPalEntry[i].peGreen = __sysPalTemplate[i].peGreen;
60 palPtr->palPalEntry[i].peBlue = __sysPalTemplate[i].peBlue;
61 palPtr->palPalEntry[i].peFlags = 0;
63 hpalette = CreatePalette16( palPtr );
65 palObj = (PALETTEOBJ*) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
66 if (palObj)
68 palObj->mapping = xmalloc( sizeof(int) * 20 );
70 GDI_HEAP_UNLOCK( hpalette );
72 HeapFree( GetProcessHeap(), 0, palPtr );
75 return hpalette;
78 /***********************************************************************
79 * PALETTE_ValidateFlags
81 void PALETTE_ValidateFlags(PALETTEENTRY* lpPalE, int size)
83 int i = 0;
84 for( ; i<size ; i++ )
85 lpPalE[i].peFlags = PC_SYS_USED | (lpPalE[i].peFlags & 0x07);
89 /***********************************************************************
90 * CreatePalette16 (GDI.360)
92 HPALETTE16 WINAPI CreatePalette16( const LOGPALETTE* palette )
94 return CreatePalette( palette );
98 /***********************************************************************
99 * CreatePalette32 [GDI32.53] Creates a logical color palette
101 * RETURNS
102 * Success: Handle to logical palette
103 * Failure: NULL
105 HPALETTE WINAPI CreatePalette(
106 const LOGPALETTE* palette) /* [in] Pointer to logical color palette */
108 PALETTEOBJ * palettePtr;
109 HPALETTE hpalette;
110 int size;
112 if (!palette) return 0;
113 TRACE("entries=%i\n", palette->palNumEntries);
115 size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
117 hpalette = GDI_AllocObject( size + sizeof(int*) +sizeof(GDIOBJHDR) , PALETTE_MAGIC );
118 if (!hpalette) return 0;
120 palettePtr = (PALETTEOBJ *) GDI_HEAP_LOCK( hpalette );
121 memcpy( &palettePtr->logpalette, palette, size );
122 PALETTE_ValidateFlags(palettePtr->logpalette.palPalEntry,
123 palettePtr->logpalette.palNumEntries);
124 palettePtr->mapping = NULL;
125 GDI_HEAP_UNLOCK( hpalette );
127 TRACE(" returning %04x\n", hpalette);
128 return hpalette;
132 /***********************************************************************
133 * CreateHalftonePalette16 [GDI.?] Creates a halftone palette
135 * RETURNS
136 * Success: Handle to logical halftone palette
137 * Failure: 0
139 HPALETTE16 WINAPI CreateHalftonePalette16(
140 HDC16 hdc) /* [in] Handle to device context */
142 return CreateHalftonePalette(hdc);
146 /***********************************************************************
147 * CreateHalftonePalette32 [GDI32.47] Creates a halftone palette
149 * RETURNS
150 * Success: Handle to logical halftone palette
151 * Failure: 0
153 * FIXME: not truly tested
155 HPALETTE WINAPI CreateHalftonePalette(
156 HDC hdc) /* [in] Handle to device context */
158 int i, r, g, b;
159 struct {
160 WORD Version;
161 WORD NumberOfEntries;
162 PALETTEENTRY aEntries[256];
163 } Palette = {
164 0x300, 256
167 GetSystemPaletteEntries(hdc, 0, 256, Palette.aEntries);
168 return CreatePalette((LOGPALETTE *)&Palette);
170 for (r = 0; r < 6; r++) {
171 for (g = 0; g < 6; g++) {
172 for (b = 0; b < 6; b++) {
173 i = r + g*6 + b*36 + 10;
174 Palette.aEntries[i].peRed = r * 51;
175 Palette.aEntries[i].peGreen = g * 51;
176 Palette.aEntries[i].peBlue = b * 51;
181 for (i = 216; i < 246; i++) {
182 int v = (i - 216) * 8;
183 Palette.aEntries[i].peRed = v;
184 Palette.aEntries[i].peGreen = v;
185 Palette.aEntries[i].peBlue = v;
188 return CreatePalette((LOGPALETTE *)&Palette);
192 /***********************************************************************
193 * GetPaletteEntries16 (GDI.363)
195 UINT16 WINAPI GetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
196 UINT16 count, LPPALETTEENTRY entries )
198 return GetPaletteEntries( hpalette, start, count, entries );
202 /***********************************************************************
203 * GetPaletteEntries32 [GDI32.209] Retrieves palette entries
205 * RETURNS
206 * Success: Number of entries from logical palette
207 * Failure: 0
209 UINT WINAPI GetPaletteEntries(
210 HPALETTE hpalette, /* [in] Handle of logical palette */
211 UINT start, /* [in] First entry to receive */
212 UINT count, /* [in] Number of entries to receive */
213 LPPALETTEENTRY entries) /* [out] Address of array receiving entries */
215 PALETTEOBJ * palPtr;
216 INT numEntries;
218 TRACE("hpal = %04x, count=%i\n", hpalette, count );
220 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
221 if (!palPtr) return 0;
223 numEntries = palPtr->logpalette.palNumEntries;
224 if (start+count > numEntries) count = numEntries - start;
225 if (entries)
227 if (start >= numEntries)
229 GDI_HEAP_UNLOCK( hpalette );
230 return 0;
232 memcpy( entries, &palPtr->logpalette.palPalEntry[start],
233 count * sizeof(PALETTEENTRY) );
234 for( numEntries = 0; numEntries < count ; numEntries++ )
235 if (entries[numEntries].peFlags & 0xF0)
236 entries[numEntries].peFlags = 0;
237 GDI_HEAP_UNLOCK( hpalette );
240 return count;
244 /***********************************************************************
245 * SetPaletteEntries16 (GDI.364)
247 UINT16 WINAPI SetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
248 UINT16 count, LPPALETTEENTRY entries )
250 return SetPaletteEntries( hpalette, start, count, entries );
254 /***********************************************************************
255 * SetPaletteEntries32 [GDI32.326] Sets color values for range in palette
257 * RETURNS
258 * Success: Number of entries that were set
259 * Failure: 0
261 UINT WINAPI SetPaletteEntries(
262 HPALETTE hpalette, /* [in] Handle of logical palette */
263 UINT start, /* [in] Index of first entry to set */
264 UINT count, /* [in] Number of entries to set */
265 LPPALETTEENTRY entries) /* [in] Address of array of structures */
267 PALETTEOBJ * palPtr;
268 INT numEntries;
270 TRACE("hpal=%04x,start=%i,count=%i\n",hpalette,start,count );
272 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
273 if (!palPtr) return 0;
275 numEntries = palPtr->logpalette.palNumEntries;
276 if (start >= numEntries)
278 GDI_HEAP_UNLOCK( hpalette );
279 return 0;
281 if (start+count > numEntries) count = numEntries - start;
282 memcpy( &palPtr->logpalette.palPalEntry[start], entries,
283 count * sizeof(PALETTEENTRY) );
284 PALETTE_ValidateFlags(palPtr->logpalette.palPalEntry,
285 palPtr->logpalette.palNumEntries);
286 free(palPtr->mapping);
287 palPtr->mapping = NULL;
288 GDI_HEAP_UNLOCK( hpalette );
289 return count;
293 /***********************************************************************
294 * ResizePalette16 (GDI.368)
296 BOOL16 WINAPI ResizePalette16( HPALETTE16 hPal, UINT16 cEntries )
298 return ResizePalette( hPal, cEntries );
302 /***********************************************************************
303 * ResizePalette32 [GDI32.289] Resizes logical palette
305 * RETURNS
306 * Success: TRUE
307 * Failure: FALSE
309 BOOL WINAPI ResizePalette(
310 HPALETTE hPal, /* [in] Handle of logical palette */
311 UINT cEntries) /* [in] Number of entries in logical palette */
313 PALETTEOBJ * palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
314 UINT cPrevEnt, prevVer;
315 int prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
316 int* mapping = NULL;
318 TRACE("hpal = %04x, prev = %i, new = %i\n",
319 hPal, palPtr ? palPtr->logpalette.palNumEntries : -1,
320 cEntries );
321 if( !palPtr ) return FALSE;
322 cPrevEnt = palPtr->logpalette.palNumEntries;
323 prevVer = palPtr->logpalette.palVersion;
324 prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) +
325 sizeof(int*) + sizeof(GDIOBJHDR);
326 size += sizeof(int*) + sizeof(GDIOBJHDR);
327 mapping = palPtr->mapping;
329 GDI_HEAP_UNLOCK( hPal );
331 hPal = GDI_HEAP_REALLOC( hPal, size );
332 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
333 if( !palPtr ) return FALSE;
335 if( mapping )
336 palPtr->mapping = (int*) xrealloc( mapping, cEntries * sizeof(int) );
337 if( cEntries > cPrevEnt )
339 if( mapping )
340 memset(palPtr->mapping + cPrevEnt, 0, (cEntries - cPrevEnt)*sizeof(int));
341 memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
342 PALETTE_ValidateFlags((PALETTEENTRY*)((BYTE*)palPtr + prevsize),
343 cEntries - cPrevEnt );
345 palPtr->logpalette.palNumEntries = cEntries;
346 palPtr->logpalette.palVersion = prevVer;
347 GDI_HEAP_UNLOCK( hPal );
348 return TRUE;
352 /***********************************************************************
353 * AnimatePalette16 (GDI.367)
355 void WINAPI AnimatePalette16( HPALETTE16 hPal, UINT16 StartIndex,
356 UINT16 NumEntries, const PALETTEENTRY* PaletteColors)
358 AnimatePalette( hPal, StartIndex, NumEntries, PaletteColors );
362 /***********************************************************************
363 * AnimatePalette32 [GDI32.6] Replaces entries in logical palette
365 * RETURNS
366 * Success: TRUE
367 * Failure: FALSE
369 * FIXME
370 * Should use existing mapping when animating a primary palette
372 BOOL WINAPI AnimatePalette(
373 HPALETTE hPal, /* [in] Handle to logical palette */
374 UINT StartIndex, /* [in] First entry in palette */
375 UINT NumEntries, /* [in] Count of entries in palette */
376 const PALETTEENTRY* PaletteColors) /* [in] Pointer to first replacement */
378 TRACE("%04x (%i - %i)\n", hPal, StartIndex,StartIndex+NumEntries);
380 if( hPal != STOCK_DEFAULT_PALETTE )
382 PALETTEOBJ* palPtr = (PALETTEOBJ *)GDI_GetObjPtr(hPal, PALETTE_MAGIC);
383 if (!palPtr) return FALSE;
385 if( (StartIndex + NumEntries) <= palPtr->logpalette.palNumEntries )
387 UINT u;
388 for( u = 0; u < NumEntries; u++ )
389 palPtr->logpalette.palPalEntry[u + StartIndex] = PaletteColors[u];
390 PALETTE_Driver->
391 pSetMapping(palPtr, StartIndex, NumEntries,
392 hPal != hPrimaryPalette );
393 GDI_HEAP_UNLOCK( hPal );
394 return TRUE;
397 return FALSE;
401 /***********************************************************************
402 * SetSystemPaletteUse16 (GDI.373)
404 UINT16 WINAPI SetSystemPaletteUse16( HDC16 hdc, UINT16 use )
406 return SetSystemPaletteUse( hdc, use );
410 /***********************************************************************
411 * SetSystemPaletteUse32 [GDI32.335]
413 * RETURNS
414 * Success: Previous system palette
415 * Failure: SYSPAL_ERROR
417 UINT WINAPI SetSystemPaletteUse(
418 HDC hdc, /* [in] Handle of device context */
419 UINT use) /* [in] Palette-usage flag */
421 UINT old = SystemPaletteUse;
422 FIXME("(%04x,%04x): stub\n", hdc, use );
423 SystemPaletteUse = use;
424 return old;
428 /***********************************************************************
429 * GetSystemPaletteUse16 (GDI.374)
431 UINT16 WINAPI GetSystemPaletteUse16( HDC16 hdc )
433 return SystemPaletteUse;
437 /***********************************************************************
438 * GetSystemPaletteUse32 [GDI32.223] Gets state of system palette
440 * RETURNS
441 * Current state of system palette
443 UINT WINAPI GetSystemPaletteUse(
444 HDC hdc) /* [in] Handle of device context */
446 return SystemPaletteUse;
450 /***********************************************************************
451 * GetSystemPaletteEntries16 (GDI.375)
453 UINT16 WINAPI GetSystemPaletteEntries16( HDC16 hdc, UINT16 start, UINT16 count,
454 LPPALETTEENTRY entries )
456 return GetSystemPaletteEntries( hdc, start, count, entries );
460 /***********************************************************************
461 * GetSystemPaletteEntries32 [GDI32.222] Gets range of palette entries
463 * RETURNS
464 * Success: Number of entries retrieved from palette
465 * Failure: 0
467 UINT WINAPI GetSystemPaletteEntries(
468 HDC hdc, /* [in] Handle of device context */
469 UINT start, /* [in] Index of first entry to be retrieved */
470 UINT count, /* [in] Number of entries to be retrieved */
471 LPPALETTEENTRY entries) /* [out] Array receiving system-palette entries */
473 UINT i;
474 DC *dc;
476 TRACE("hdc=%04x,start=%i,count=%i\n", hdc,start,count);
478 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
479 if (!entries) return dc->w.devCaps->sizePalette;
480 if (start >= dc->w.devCaps->sizePalette)
482 GDI_HEAP_UNLOCK( hdc );
483 return 0;
485 if (start+count >= dc->w.devCaps->sizePalette)
486 count = dc->w.devCaps->sizePalette - start;
487 for (i = 0; i < count; i++)
489 *(COLORREF*)(entries + i) = COLOR_GetSystemPaletteEntry( start + i );
491 TRACE("\tidx(%02x) -> RGB(%08lx)\n",
492 start + i, *(COLORREF*)(entries + i) );
494 GDI_HEAP_UNLOCK( hdc );
495 return count;
499 /***********************************************************************
500 * GetNearestPaletteIndex16 (GDI.370)
502 UINT16 WINAPI GetNearestPaletteIndex16( HPALETTE16 hpalette, COLORREF color )
504 return GetNearestPaletteIndex( hpalette, color );
508 /***********************************************************************
509 * GetNearestPaletteIndex32 [GDI32.203] Gets palette index for color
511 * NOTES
512 * Should index be initialized to CLR_INVALID instead of 0?
514 * RETURNS
515 * Success: Index of entry in logical palette
516 * Failure: CLR_INVALID
518 UINT WINAPI GetNearestPaletteIndex(
519 HPALETTE hpalette, /* [in] Handle of logical color palette */
520 COLORREF color) /* [in] Color to be matched */
522 PALETTEOBJ* palObj = (PALETTEOBJ*)GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
523 UINT index = 0;
525 if( palObj )
526 index = COLOR_PaletteLookupPixel(palObj->logpalette.palPalEntry,
527 palObj->logpalette.palNumEntries,
528 NULL, color, FALSE );
530 TRACE("(%04x,%06lx): returning %d\n", hpalette, color, index );
531 GDI_HEAP_UNLOCK( hpalette );
532 return index;
536 /***********************************************************************
537 * GetNearestColor16 (GDI.154)
539 COLORREF WINAPI GetNearestColor16( HDC16 hdc, COLORREF color )
541 return GetNearestColor( hdc, color );
545 /***********************************************************************
546 * GetNearestColor32 [GDI32.202] Gets a system color to match
548 * NOTES
549 * Should this return CLR_INVALID instead of FadeCafe?
551 * RETURNS
552 * Success: Color from system palette that corresponds to given color
553 * Failure: CLR_INVALID
555 COLORREF WINAPI GetNearestColor(
556 HDC hdc, /* [in] Handle of device context */
557 COLORREF color) /* [in] Color to be matched */
559 COLORREF nearest = 0xFADECAFE;
560 DC *dc;
561 PALETTEOBJ *palObj;
563 if ( (dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC )) )
565 palObj = (PALETTEOBJ*)
566 GDI_GetObjPtr( (dc->w.hPalette)? dc->w.hPalette
567 : STOCK_DEFAULT_PALETTE, PALETTE_MAGIC );
568 if (!palObj) return nearest;
570 nearest = COLOR_LookupNearestColor( palObj->logpalette.palPalEntry,
571 palObj->logpalette.palNumEntries, color );
572 GDI_HEAP_UNLOCK( dc->w.hPalette );
575 TRACE("(%06lx): returning %06lx\n", color, nearest );
576 GDI_HEAP_UNLOCK( hdc );
577 return nearest;
581 /***********************************************************************
582 * PALETTE_GetObject
584 int PALETTE_GetObject( PALETTEOBJ * palette, int count, LPSTR buffer )
586 if (count > sizeof(WORD)) count = sizeof(WORD);
587 memcpy( buffer, &palette->logpalette.palNumEntries, count );
588 return count;
592 /***********************************************************************
593 * PALETTE_UnrealizeObject
595 BOOL PALETTE_UnrealizeObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
597 if (palette->mapping)
599 free( palette->mapping );
600 palette->mapping = NULL;
602 if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
603 return TRUE;
607 /***********************************************************************
608 * PALETTE_DeleteObject
610 BOOL PALETTE_DeleteObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
612 free( palette->mapping );
613 if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
614 return GDI_FreeObject( hpalette );
618 /***********************************************************************
619 * GDISelectPalette (GDI.361)
621 HPALETTE16 WINAPI GDISelectPalette16( HDC16 hdc, HPALETTE16 hpal, WORD wBkg)
623 HPALETTE16 prev;
624 DC *dc;
626 TRACE("%04x %04x\n", hdc, hpal );
628 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
629 if (!dc)
631 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
632 if (!dc) return 0;
634 prev = dc->w.hPalette;
635 dc->w.hPalette = hpal;
636 GDI_HEAP_UNLOCK( hdc );
637 if (!wBkg) hPrimaryPalette = hpal;
638 return prev;
642 /***********************************************************************
643 * GDIRealizePalette (GDI.362)
645 UINT16 WINAPI GDIRealizePalette16( HDC16 hdc )
647 PALETTEOBJ* palPtr;
648 int realized = 0;
649 DC* dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
650 if (!dc)
652 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
653 if (!dc) return 0;
656 TRACE("%04x...\n", hdc );
658 if( dc && dc->w.hPalette != hLastRealizedPalette )
660 if( dc->w.hPalette == STOCK_DEFAULT_PALETTE )
661 return RealizeDefaultPalette16( hdc );
663 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC );
665 if (!palPtr) {
666 FIXME("invalid selected palette %04x\n",dc->w.hPalette);
667 return 0;
670 realized = PALETTE_Driver->
671 pSetMapping(palPtr,0,palPtr->logpalette.palNumEntries,
672 (dc->w.hPalette != hPrimaryPalette) ||
673 (dc->w.hPalette == STOCK_DEFAULT_PALETTE));
674 GDI_HEAP_UNLOCK( dc->w.hPalette );
675 hLastRealizedPalette = dc->w.hPalette;
677 else TRACE(" skipping (hLastRealizedPalette = %04x)\n",
678 hLastRealizedPalette);
679 GDI_HEAP_UNLOCK( hdc );
681 TRACE(" realized %i colors.\n", realized );
682 return (UINT16)realized;
686 /***********************************************************************
687 * RealizeDefaultPalette (GDI.365)
689 UINT16 WINAPI RealizeDefaultPalette16( HDC16 hdc )
691 DC *dc;
692 PALETTEOBJ* palPtr;
694 TRACE("%04x\n", hdc );
696 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
697 if (!dc)
699 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
700 if (!dc) return 0;
703 if ( dc->w.flags & DC_MEMORY )
705 GDI_HEAP_UNLOCK( hdc );
706 return 0;
709 hPrimaryPalette = STOCK_DEFAULT_PALETTE;
710 hLastRealizedPalette = STOCK_DEFAULT_PALETTE;
712 palPtr = (PALETTEOBJ*)GDI_GetObjPtr(STOCK_DEFAULT_PALETTE, PALETTE_MAGIC );
713 if (!palPtr) return 0;
715 /* lookup is needed to account for SetSystemPaletteUse() stuff */
717 return PALETTE_Driver->pUpdateMapping(palPtr);
720 /***********************************************************************
721 * IsDCCurrentPalette (GDI.412)
723 BOOL16 WINAPI IsDCCurrentPalette16(HDC16 hDC)
725 DC* dc = (DC *)GDI_GetObjPtr( hDC, DC_MAGIC );
726 if (dc)
728 GDI_HEAP_UNLOCK( hDC );
729 return dc->w.hPalette == hPrimaryPalette;
731 return FALSE;
735 /***********************************************************************
736 * SelectPalette16 (USER.282)
738 HPALETTE16 WINAPI SelectPalette16( HDC16 hDC, HPALETTE16 hPal,
739 BOOL16 bForceBackground )
741 return SelectPalette( hDC, hPal, bForceBackground );
745 /***********************************************************************
746 * SelectPalette32 [GDI32.300] Selects logical palette into DC
748 * RETURNS
749 * Success: Previous logical palette
750 * Failure: NULL
752 HPALETTE WINAPI SelectPalette(
753 HDC hDC, /* [in] Handle of device context */
754 HPALETTE hPal, /* [in] Handle of logical color palette */
755 BOOL bForceBackground) /* [in] Foreground/background mode */
757 WORD wBkgPalette = 1;
758 PALETTEOBJ* lpt = (PALETTEOBJ*) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
760 TRACE("dc=%04x,pal=%04x,force=%i\n", hDC, hPal, bForceBackground);
761 if( !lpt ) return 0;
763 TRACE(" entries = %d\n", lpt->logpalette.palNumEntries);
764 GDI_HEAP_UNLOCK( hPal );
766 if( hPal != STOCK_DEFAULT_PALETTE )
768 HWND hWnd = WindowFromDC( hDC );
769 HWND hActive = GetActiveWindow();
771 /* set primary palette if it's related to current active */
773 if((!hWnd || (hActive == hWnd || IsChild16(hActive,hWnd))) &&
774 !bForceBackground )
775 wBkgPalette = 0;
777 return GDISelectPalette16( hDC, hPal, wBkgPalette);
781 /***********************************************************************
782 * RealizePalette16 (USER.283)
784 UINT16 WINAPI RealizePalette16( HDC16 hDC )
786 return RealizePalette( hDC );
790 /***********************************************************************
791 * RealizePalette32 [GDI32.280] Maps palette entries to system palette
793 * RETURNS
794 * Success: Number of entries in logical palette
795 * Failure: GDI_ERROR
797 UINT WINAPI RealizePalette(
798 HDC hDC) /* [in] Handle of device context */
800 DC *dc;
801 UINT realized;
803 if (!(dc = (DC *) GDI_GetObjPtr( hDC, DC_MAGIC ))) return 0;
805 realized = GDIRealizePalette16( hDC );
807 /* do not send anything if no colors were changed */
809 if( IsDCCurrentPalette16( hDC ) && realized &&
810 dc->w.devCaps->sizePalette )
812 /* Send palette change notification */
814 HWND hWnd;
815 if( (hWnd = WindowFromDC( hDC )) )
816 SendMessage16( HWND_BROADCAST, WM_PALETTECHANGED, hWnd, 0L);
819 GDI_HEAP_UNLOCK( hDC );
820 return realized;
824 /**********************************************************************
825 * UpdateColors16 (GDI.366)
827 INT16 WINAPI UpdateColors16( HDC16 hDC )
829 DC *dc;
830 HWND hWnd;
832 if (!(dc = (DC *) GDI_GetObjPtr( hDC, DC_MAGIC ))) return 0;
834 hWnd = WindowFromDC( hDC );
836 /* Docs say that we have to remap current drawable pixel by pixel
837 * but it would take forever given the speed of XGet/PutPixel.
839 if (hWnd && dc->w.devCaps->sizePalette )
840 InvalidateRect( hWnd, NULL, FALSE );
842 GDI_HEAP_UNLOCK( hDC );
844 return 0x666;
848 /**********************************************************************
849 * UpdateColors32 [GDI32.359] Remaps current colors to logical palette
851 * RETURNS
852 * Success: TRUE
853 * Failure: FALSE
855 BOOL WINAPI UpdateColors(
856 HDC hDC) /* [in] Handle of device context */
858 UpdateColors16( hDC );
859 return TRUE;
863 /*********************************************************************
864 * SetMagicColors16 (GDI.606)
866 VOID WINAPI SetMagicColors16(HDC16 hDC, COLORREF color, UINT16 index)
868 FIXME("(hDC %04x, color %04x, index %04x): stub\n", hDC, (int)color, index);