Make GetDeviceCaps16 (hdc, NUMCOLORS) not return -1 for 16 bits programs.
[wine/hacks.git] / objects / palette.c
blob6b1c2eea323d8b6918adfa60f922d1030b7aa777
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 "windef.h"
16 #include "wingdi.h"
17 #include "wine/winuser16.h"
18 #include "gdi.h"
19 #include "color.h"
20 #include "dc.h"
21 #include "palette.h"
22 #include "debugtools.h"
23 #include "callback.h"
24 #include "winerror.h"
26 DEFAULT_DEBUG_CHANNEL(palette)
28 PALETTE_DRIVER *PALETTE_Driver = NULL;
30 FARPROC pfnSelectPalette = NULL;
31 FARPROC pfnRealizePalette = NULL;
33 static UINT SystemPaletteUse = SYSPAL_STATIC; /* currently not considered */
35 static HPALETTE16 hPrimaryPalette = 0; /* used for WM_PALETTECHANGED */
36 static HPALETTE16 hLastRealizedPalette = 0; /* UnrealizeObject() needs it */
39 /***********************************************************************
40 * PALETTE_Init
42 * Create the system palette.
44 HPALETTE16 PALETTE_Init(void)
46 int i;
47 HPALETTE16 hpalette;
48 LOGPALETTE * palPtr;
49 PALETTEOBJ* palObj;
50 const PALETTEENTRY* __sysPalTemplate = COLOR_GetSystemPaletteTemplate();
52 /* create default palette (20 system colors) */
54 palPtr = HeapAlloc( GetProcessHeap(), 0,
55 sizeof(LOGPALETTE) + (NB_RESERVED_COLORS-1)*sizeof(PALETTEENTRY));
56 if (!palPtr) return FALSE;
58 palPtr->palVersion = 0x300;
59 palPtr->palNumEntries = NB_RESERVED_COLORS;
60 for( i = 0; i < NB_RESERVED_COLORS; i ++ )
62 palPtr->palPalEntry[i].peRed = __sysPalTemplate[i].peRed;
63 palPtr->palPalEntry[i].peGreen = __sysPalTemplate[i].peGreen;
64 palPtr->palPalEntry[i].peBlue = __sysPalTemplate[i].peBlue;
65 palPtr->palPalEntry[i].peFlags = 0;
67 hpalette = CreatePalette16( palPtr );
68 HeapFree( GetProcessHeap(), 0, palPtr );
70 palObj = (PALETTEOBJ*) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
71 if (palObj)
73 if (!(palObj->mapping = HeapAlloc( GetProcessHeap(), 0, sizeof(int) * 20 )))
74 ERR("Can not create palette mapping -- out of memory!");
75 GDI_ReleaseObj( hpalette );
77 return hpalette;
80 /***********************************************************************
81 * PALETTE_ValidateFlags
83 void PALETTE_ValidateFlags(PALETTEENTRY* lpPalE, int size)
85 int i = 0;
86 for( ; i<size ; i++ )
87 lpPalE[i].peFlags = PC_SYS_USED | (lpPalE[i].peFlags & 0x07);
91 /***********************************************************************
92 * CreatePalette16 (GDI.360)
94 HPALETTE16 WINAPI CreatePalette16( const LOGPALETTE* palette )
96 return CreatePalette( palette );
100 /***********************************************************************
101 * CreatePalette [GDI32.53] Creates a logical color palette
103 * RETURNS
104 * Success: Handle to logical palette
105 * Failure: NULL
107 HPALETTE WINAPI CreatePalette(
108 const LOGPALETTE* palette) /* [in] Pointer to logical color palette */
110 PALETTEOBJ * palettePtr;
111 HPALETTE hpalette;
112 int size;
114 if (!palette) return 0;
115 TRACE("entries=%i\n", palette->palNumEntries);
117 size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
119 if (!(palettePtr = GDI_AllocObject( size + sizeof(int*) +sizeof(GDIOBJHDR),
120 PALETTE_MAGIC, &hpalette ))) return 0;
121 memcpy( &palettePtr->logpalette, palette, size );
122 PALETTE_ValidateFlags(palettePtr->logpalette.palPalEntry,
123 palettePtr->logpalette.palNumEntries);
124 palettePtr->mapping = NULL;
125 GDI_ReleaseObj( 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 * CreateHalftonePalette [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 * GetPaletteEntries [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_ReleaseObj( 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;
239 GDI_ReleaseObj( 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 * SetPaletteEntries [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_ReleaseObj( 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 HeapFree( GetProcessHeap(), 0, palPtr->mapping );
287 palPtr->mapping = NULL;
288 GDI_ReleaseObj( hpalette );
289 return count;
293 /***********************************************************************
294 * ResizePalette16 (GDI.368)
296 BOOL16 WINAPI ResizePalette16( HPALETTE16 hPal, UINT16 cEntries )
298 return ResizePalette( hPal, cEntries );
302 /***********************************************************************
303 * ResizePalette [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 if (!(palPtr = GDI_ReallocObject( size, hPal, palPtr ))) return FALSE;
331 if( mapping )
333 int *newMap = (int*) HeapReAlloc(GetProcessHeap(), 0,
334 mapping, cEntries * sizeof(int) );
335 if(newMap == NULL)
337 ERR("Can not resize mapping -- out of memory!");
338 GDI_ReleaseObj( hPal );
339 return FALSE;
341 palPtr->mapping = newMap;
344 if( cEntries > cPrevEnt )
346 if( mapping )
347 memset(palPtr->mapping + cPrevEnt, 0, (cEntries - cPrevEnt)*sizeof(int));
348 memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
349 PALETTE_ValidateFlags((PALETTEENTRY*)((BYTE*)palPtr + prevsize),
350 cEntries - cPrevEnt );
352 palPtr->logpalette.palNumEntries = cEntries;
353 palPtr->logpalette.palVersion = prevVer;
354 GDI_ReleaseObj( hPal );
355 return TRUE;
359 /***********************************************************************
360 * AnimatePalette16 (GDI.367)
362 void WINAPI AnimatePalette16( HPALETTE16 hPal, UINT16 StartIndex,
363 UINT16 NumEntries, const PALETTEENTRY* PaletteColors)
365 AnimatePalette( hPal, StartIndex, NumEntries, PaletteColors );
369 /***********************************************************************
370 * AnimatePalette [GDI32.6] Replaces entries in logical palette
372 * RETURNS
373 * Success: TRUE
374 * Failure: FALSE
376 * FIXME
377 * Should use existing mapping when animating a primary palette
379 BOOL WINAPI AnimatePalette(
380 HPALETTE hPal, /* [in] Handle to logical palette */
381 UINT StartIndex, /* [in] First entry in palette */
382 UINT NumEntries, /* [in] Count of entries in palette */
383 const PALETTEENTRY* PaletteColors) /* [in] Pointer to first replacement */
385 TRACE("%04x (%i - %i)\n", hPal, StartIndex,StartIndex+NumEntries);
387 if( hPal != STOCK_DEFAULT_PALETTE )
389 PALETTEOBJ* palPtr = (PALETTEOBJ *)GDI_GetObjPtr(hPal, PALETTE_MAGIC);
390 if (!palPtr) return FALSE;
392 if( (StartIndex + NumEntries) <= palPtr->logpalette.palNumEntries )
394 UINT u;
395 for( u = 0; u < NumEntries; u++ )
396 palPtr->logpalette.palPalEntry[u + StartIndex] = PaletteColors[u];
397 PALETTE_Driver->
398 pSetMapping(palPtr, StartIndex, NumEntries,
399 hPal != hPrimaryPalette );
400 GDI_ReleaseObj( hPal );
401 return TRUE;
403 GDI_ReleaseObj( hPal );
405 return FALSE;
409 /***********************************************************************
410 * SetSystemPaletteUse16 (GDI.373)
412 UINT16 WINAPI SetSystemPaletteUse16( HDC16 hdc, UINT16 use )
414 return SetSystemPaletteUse( hdc, use );
418 /***********************************************************************
419 * SetSystemPaletteUse [GDI32.335]
421 * RETURNS
422 * Success: Previous system palette
423 * Failure: SYSPAL_ERROR
425 UINT WINAPI SetSystemPaletteUse(
426 HDC hdc, /* [in] Handle of device context */
427 UINT use) /* [in] Palette-usage flag */
429 UINT old = SystemPaletteUse;
430 FIXME("(%04x,%04x): stub\n", hdc, use );
431 SystemPaletteUse = use;
432 return old;
436 /***********************************************************************
437 * GetSystemPaletteUse16 (GDI.374)
439 UINT16 WINAPI GetSystemPaletteUse16( HDC16 hdc )
441 return SystemPaletteUse;
445 /***********************************************************************
446 * GetSystemPaletteUse [GDI32.223] Gets state of system palette
448 * RETURNS
449 * Current state of system palette
451 UINT WINAPI GetSystemPaletteUse(
452 HDC hdc) /* [in] Handle of device context */
454 return SystemPaletteUse;
458 /***********************************************************************
459 * GetSystemPaletteEntries16 (GDI.375)
461 UINT16 WINAPI GetSystemPaletteEntries16( HDC16 hdc, UINT16 start, UINT16 count,
462 LPPALETTEENTRY entries )
464 return GetSystemPaletteEntries( hdc, start, count, entries );
468 /***********************************************************************
469 * GetSystemPaletteEntries [GDI32.222] Gets range of palette entries
471 * RETURNS
472 * Success: Number of entries retrieved from palette
473 * Failure: 0
475 UINT WINAPI GetSystemPaletteEntries(
476 HDC hdc, /* [in] Handle of device context */
477 UINT start, /* [in] Index of first entry to be retrieved */
478 UINT count, /* [in] Number of entries to be retrieved */
479 LPPALETTEENTRY entries) /* [out] Array receiving system-palette entries */
481 UINT i;
482 DC *dc;
484 TRACE("hdc=%04x,start=%i,count=%i\n", hdc,start,count);
486 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
488 if (!entries)
490 count = dc->w.devCaps->sizePalette;
491 goto done;
494 if (start >= dc->w.devCaps->sizePalette)
496 count = 0;
497 goto done;
500 if (start+count >= dc->w.devCaps->sizePalette)
501 count = dc->w.devCaps->sizePalette - start;
502 for (i = 0; i < count; i++)
504 *(COLORREF*)(entries + i) = COLOR_GetSystemPaletteEntry( start + i );
506 TRACE("\tidx(%02x) -> RGB(%08lx)\n",
507 start + i, *(COLORREF*)(entries + i) );
509 done:
510 GDI_ReleaseObj( hdc );
511 return count;
515 /***********************************************************************
516 * GetNearestPaletteIndex16 (GDI.370)
518 UINT16 WINAPI GetNearestPaletteIndex16( HPALETTE16 hpalette, COLORREF color )
520 return GetNearestPaletteIndex( hpalette, color );
524 /***********************************************************************
525 * GetNearestPaletteIndex [GDI32.203] Gets palette index for color
527 * NOTES
528 * Should index be initialized to CLR_INVALID instead of 0?
530 * RETURNS
531 * Success: Index of entry in logical palette
532 * Failure: CLR_INVALID
534 UINT WINAPI GetNearestPaletteIndex(
535 HPALETTE hpalette, /* [in] Handle of logical color palette */
536 COLORREF color) /* [in] Color to be matched */
538 PALETTEOBJ* palObj = (PALETTEOBJ*)GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
539 UINT index = 0;
541 if( palObj )
543 index = COLOR_PaletteLookupPixel(palObj->logpalette.palPalEntry,
544 palObj->logpalette.palNumEntries,
545 NULL, color, FALSE );
547 GDI_ReleaseObj( hpalette );
549 TRACE("(%04x,%06lx): returning %d\n", hpalette, color, index );
550 return index;
554 /***********************************************************************
555 * GetNearestColor16 (GDI.154)
557 COLORREF WINAPI GetNearestColor16( HDC16 hdc, COLORREF color )
559 return GetNearestColor( hdc, color );
563 /***********************************************************************
564 * GetNearestColor [GDI32.202] Gets a system color to match
566 * RETURNS
567 * Success: Color from system palette that corresponds to given color
568 * Failure: CLR_INVALID
570 COLORREF WINAPI GetNearestColor(
571 HDC hdc, /* [in] Handle of device context */
572 COLORREF color) /* [in] Color to be matched */
574 COLORREF nearest = CLR_INVALID;
575 DC *dc;
576 PALETTEOBJ *palObj;
578 if ( (dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC )) )
580 HPALETTE hpal = (dc->w.hPalette)? dc->w.hPalette : STOCK_DEFAULT_PALETTE;
581 palObj = GDI_GetObjPtr( hpal, PALETTE_MAGIC );
582 if (!palObj) {
583 GDI_ReleaseObj( hdc );
584 return nearest;
587 nearest = COLOR_LookupNearestColor( palObj->logpalette.palPalEntry,
588 palObj->logpalette.palNumEntries, color );
589 GDI_ReleaseObj( hpal );
590 GDI_ReleaseObj( hdc );
593 TRACE("(%06lx): returning %06lx\n", color, nearest );
594 return nearest;
598 /***********************************************************************
599 * PALETTE_GetObject
601 int PALETTE_GetObject( PALETTEOBJ * palette, int count, LPSTR buffer )
603 if (count > sizeof(WORD)) count = sizeof(WORD);
604 memcpy( buffer, &palette->logpalette.palNumEntries, count );
605 return count;
609 /***********************************************************************
610 * PALETTE_UnrealizeObject
612 BOOL PALETTE_UnrealizeObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
614 if (palette->mapping)
616 HeapFree( GetProcessHeap(), 0, palette->mapping );
617 palette->mapping = NULL;
619 if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
620 return TRUE;
624 /***********************************************************************
625 * PALETTE_DeleteObject
627 BOOL PALETTE_DeleteObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
629 HeapFree( GetProcessHeap(), 0, palette->mapping );
630 if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
631 return GDI_FreeObject( hpalette, palette );
635 /***********************************************************************
636 * GDISelectPalette (GDI.361)
638 HPALETTE16 WINAPI GDISelectPalette16( HDC16 hdc, HPALETTE16 hpal, WORD wBkg)
640 HPALETTE16 prev;
641 DC *dc;
643 TRACE("%04x %04x\n", hdc, hpal );
645 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
646 prev = dc->w.hPalette;
647 dc->w.hPalette = hpal;
648 GDI_ReleaseObj( hdc );
649 if (!wBkg) hPrimaryPalette = hpal;
650 return prev;
654 /***********************************************************************
655 * GDIRealizePalette (GDI.362)
657 UINT16 WINAPI GDIRealizePalette16( HDC16 hdc )
659 PALETTEOBJ* palPtr;
660 int realized = 0;
661 DC* dc = DC_GetDCPtr( hdc );
663 if (!dc) return 0;
665 TRACE("%04x...\n", hdc );
667 if(dc->w.hPalette != hLastRealizedPalette )
669 if( dc->w.hPalette == STOCK_DEFAULT_PALETTE ) {
670 realized = RealizeDefaultPalette16( hdc );
671 GDI_ReleaseObj( hdc );
672 return (UINT16)realized;
676 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC );
678 if (!palPtr) {
679 GDI_ReleaseObj( hdc );
680 FIXME("invalid selected palette %04x\n",dc->w.hPalette);
681 return 0;
684 realized = PALETTE_Driver->
685 pSetMapping(palPtr,0,palPtr->logpalette.palNumEntries,
686 (dc->w.hPalette != hPrimaryPalette) ||
687 (dc->w.hPalette == STOCK_DEFAULT_PALETTE));
688 hLastRealizedPalette = dc->w.hPalette;
689 GDI_ReleaseObj( dc->w.hPalette );
691 else TRACE(" skipping (hLastRealizedPalette = %04x)\n",
692 hLastRealizedPalette);
693 GDI_ReleaseObj( hdc );
695 TRACE(" realized %i colors.\n", realized );
696 return (UINT16)realized;
700 /***********************************************************************
701 * RealizeDefaultPalette (GDI.365)
703 UINT16 WINAPI RealizeDefaultPalette16( HDC16 hdc )
705 UINT16 ret = 0;
706 DC *dc;
707 PALETTEOBJ* palPtr;
709 TRACE("%04x\n", hdc );
711 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
713 if (!(dc->w.flags & DC_MEMORY))
715 palPtr = (PALETTEOBJ*)GDI_GetObjPtr(STOCK_DEFAULT_PALETTE, PALETTE_MAGIC );
716 if (palPtr)
718 /* lookup is needed to account for SetSystemPaletteUse() stuff */
719 ret = PALETTE_Driver->pUpdateMapping(palPtr);
720 GDI_ReleaseObj( STOCK_DEFAULT_PALETTE );
723 GDI_ReleaseObj( hdc );
724 return ret;
727 /***********************************************************************
728 * IsDCCurrentPalette (GDI.412)
730 BOOL16 WINAPI IsDCCurrentPalette16(HDC16 hDC)
732 DC* dc = (DC *)GDI_GetObjPtr( hDC, DC_MAGIC );
733 if (dc)
735 BOOL bRet = dc->w.hPalette == hPrimaryPalette;
736 GDI_ReleaseObj( hDC );
737 return bRet;
739 return FALSE;
743 /***********************************************************************
744 * SelectPalette [GDI32.300] Selects logical palette into DC
746 * RETURNS
747 * Success: Previous logical palette
748 * Failure: NULL
750 HPALETTE WINAPI SelectPalette(
751 HDC hDC, /* [in] Handle of device context */
752 HPALETTE hPal, /* [in] Handle of logical color palette */
753 BOOL bForceBackground) /* [in] Foreground/background mode */
755 WORD wBkgPalette = 1;
757 if (!bForceBackground && (hPal != STOCK_DEFAULT_PALETTE))
759 HWND hwnd = Callout.WindowFromDC( hDC );
760 if (hwnd)
762 HWND hForeground = Callout.GetForegroundWindow();
763 /* set primary palette if it's related to current active */
764 if (hForeground == hwnd || Callout.IsChild(hForeground,hwnd)) wBkgPalette = 0;
767 return GDISelectPalette16( hDC, hPal, wBkgPalette);
771 /***********************************************************************
772 * RealizePalette [GDI32.280] Maps palette entries to system palette
774 * RETURNS
775 * Success: Number of entries in logical palette
776 * Failure: GDI_ERROR
778 UINT WINAPI RealizePalette(
779 HDC hDC) /* [in] Handle of device context */
781 DC *dc;
782 UINT realized;
784 if (!(dc = (DC *) GDI_GetObjPtr( hDC, DC_MAGIC ))) return 0;
786 realized = GDIRealizePalette16( hDC );
788 /* do not send anything if no colors were changed */
790 if( IsDCCurrentPalette16( hDC ) && realized &&
791 dc->w.devCaps->sizePalette )
793 /* Send palette change notification */
795 HWND hWnd;
796 GDI_ReleaseObj( hDC );
797 if( (hWnd = Callout.WindowFromDC( hDC )) )
798 Callout.SendMessageA( HWND_BROADCAST, WM_PALETTECHANGED, hWnd, 0L);
800 else GDI_ReleaseObj( hDC );
802 return realized;
806 /**********************************************************************
807 * UpdateColors16 (GDI.366)
809 INT16 WINAPI UpdateColors16( HDC16 hDC )
811 DC *dc;
812 HWND hWnd;
813 int size;
815 if (!(dc = (DC *) GDI_GetObjPtr( hDC, DC_MAGIC ))) return 0;
816 size = dc->w.devCaps->sizePalette;
817 GDI_ReleaseObj( hDC );
818 hWnd = Callout.WindowFromDC( hDC );
820 /* Docs say that we have to remap current drawable pixel by pixel
821 * but it would take forever given the speed of XGet/PutPixel.
823 if (hWnd && size)
824 Callout.RedrawWindow( hWnd, NULL, 0, RDW_INVALIDATE );
826 return 0x666;
830 /**********************************************************************
831 * UpdateColors [GDI32.359] Remaps current colors to logical palette
833 * RETURNS
834 * Success: TRUE
835 * Failure: FALSE
837 BOOL WINAPI UpdateColors(
838 HDC hDC) /* [in] Handle of device context */
840 UpdateColors16( hDC );
841 return TRUE;
845 /*********************************************************************
846 * SetMagicColors16 (GDI.606)
848 VOID WINAPI SetMagicColors16(HDC16 hDC, COLORREF color, UINT16 index)
850 FIXME("(hDC %04x, color %04x, index %04x): stub\n", hDC, (int)color, index);
854 /**********************************************************************
855 * GetICMProfileA [GDI32.316]
857 * Returns the filename of the specified device context's color
858 * management profile, even if color management is not enabled
859 * for that DC.
861 * RETURNS
862 * TRUE if name copied succesfully OR lpszFilename is NULL
863 * FALSE if the buffer length pointed to by lpcbName is too small
865 * NOTE
866 * The buffer length pointed to by lpcbName is ALWAYS updated to
867 * the length required regardless of other actions this function
868 * may take.
870 * FIXME
871 * How does Windows assign these? Some registry key?
874 #define WINEICM "winefake.icm" /* easy-to-identify fake filename */
876 BOOL WINAPI GetICMProfileA(HDC hDC, LPDWORD lpcbName, LPSTR lpszFilename)
878 DWORD callerLen;
880 FIXME("(%04x, %p, %p): partial stub\n", hDC, lpcbName, lpszFilename);
882 callerLen = *lpcbName;
884 /* all 3 behaviors require the required buffer size to be set */
885 *lpcbName = strlen(WINEICM);
887 /* behavior 1: if lpszFilename is NULL, return size of string and no error */
888 if ((DWORD)lpszFilename == (DWORD)0x00000000)
889 return TRUE;
891 /* behavior 2: if buffer size too small, return size of string and error */
892 if (callerLen < strlen(WINEICM))
894 SetLastError(ERROR_INSUFFICIENT_BUFFER);
895 return FALSE;
898 /* behavior 3: if buffer size OK and pointer not NULL, copy and return size */
899 strcpy(lpszFilename, WINEICM);
900 return TRUE;