2 * ReactOS W32 Subsystem
3 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 // FIXME: Use PXLATEOBJ logicalToSystem instead of int *mapping
24 int COLOR_gapStart
= 256;
25 int COLOR_gapEnd
= -1;
26 int COLOR_gapFilled
= 0;
30 static HPALETTE hPrimaryPalette
= 0; // used for WM_PALETTECHANGED
32 //static HPALETTE hLastRealizedPalette = 0; // UnrealizeObject() needs it
35 static UINT SystemPaletteUse
= SYSPAL_STATIC
; /* currently not considered */
37 const PALETTEENTRY COLOR_sysPalTemplate
[NB_RESERVED_COLORS
] =
39 // first 10 entries in the system palette
40 // red green blue flags
41 { 0x00, 0x00, 0x00, PC_SYS_USED
},
42 { 0x80, 0x00, 0x00, PC_SYS_USED
},
43 { 0x00, 0x80, 0x00, PC_SYS_USED
},
44 { 0x80, 0x80, 0x00, PC_SYS_USED
},
45 { 0x00, 0x00, 0x80, PC_SYS_USED
},
46 { 0x80, 0x00, 0x80, PC_SYS_USED
},
47 { 0x00, 0x80, 0x80, PC_SYS_USED
},
48 { 0xc0, 0xc0, 0xc0, PC_SYS_USED
},
49 { 0xc0, 0xdc, 0xc0, PC_SYS_USED
},
50 { 0xd4, 0xd0, 0xc7, PC_SYS_USED
},
52 // ... c_min/2 dynamic colorcells
53 // ... gap (for sparse palettes)
54 // ... c_min/2 dynamic colorcells
56 { 0xff, 0xfb, 0xf0, PC_SYS_USED
},
57 { 0x3a, 0x6e, 0xa5, PC_SYS_USED
},
58 { 0x80, 0x80, 0x80, PC_SYS_USED
},
59 { 0xff, 0x00, 0x00, PC_SYS_USED
},
60 { 0x00, 0xff, 0x00, PC_SYS_USED
},
61 { 0xff, 0xff, 0x00, PC_SYS_USED
},
62 { 0x00, 0x00, 0xff, PC_SYS_USED
},
63 { 0xff, 0x00, 0xff, PC_SYS_USED
},
64 { 0x00, 0xff, 0xff, PC_SYS_USED
},
65 { 0xff, 0xff, 0xff, PC_SYS_USED
} // last 10
68 const PALETTEENTRY
* FASTCALL
COLOR_GetSystemPaletteTemplate(void)
70 return (const PALETTEENTRY
*)&COLOR_sysPalTemplate
;
73 BOOL STDCALL
NtGdiAnimatePalette(HPALETTE hPal
, UINT StartIndex
,
74 UINT NumEntries
, CONST PPALETTEENTRY PaletteColors
)
76 if( hPal
!= NtGdiGetStockObject(DEFAULT_PALETTE
) )
83 const PALETTEENTRY
*pptr
= PaletteColors
;
85 palPtr
= (PPALGDI
)PALETTE_LockPalette(hPal
);
86 if (!palPtr
) return FALSE
;
88 pal_entries
= palPtr
->NumColors
;
89 if (StartIndex
>= pal_entries
)
91 PALETTE_UnlockPalette(hPal
);
94 if (StartIndex
+NumEntries
> pal_entries
) NumEntries
= pal_entries
- StartIndex
;
96 for (NumEntries
+= StartIndex
; StartIndex
< NumEntries
; StartIndex
++, pptr
++) {
97 /* According to MSDN, only animate PC_RESERVED colours */
98 if (palPtr
->IndexedColors
[StartIndex
].peFlags
& PC_RESERVED
) {
99 memcpy( &palPtr
->IndexedColors
[StartIndex
], pptr
,
100 sizeof(PALETTEENTRY
) );
101 PALETTE_ValidateFlags(&palPtr
->IndexedColors
[StartIndex
], 1);
105 PALETTE_UnlockPalette(hPal
);
107 /* Immediately apply the new palette if current window uses it */
108 hHwd
= NtUserGetDesktopWindow();
109 hDC
= (HDC
)NtUserGetWindowDC(hHwd
);
113 if (dc
->w
.hPalette
== hPal
)
116 NtGdiRealizePalette(hDC
);
121 NtUserReleaseDC(hHwd
,hDC
);
126 HPALETTE STDCALL
NtGdiCreateHalftonePalette(HDC hDC
)
131 WORD NumberOfEntries
;
132 PALETTEENTRY aEntries
[256];
135 Palette
.Version
= 0x300;
136 Palette
.NumberOfEntries
= 256;
137 if (NtGdiGetSystemPaletteEntries(hDC
, 0, 256, Palette
.aEntries
) == 0)
142 for (r
= 0; r
< 6; r
++)
143 for (g
= 0; g
< 6; g
++)
144 for (b
= 0; b
< 6; b
++)
146 i
= r
+ g
*6 + b
*36 + 10;
147 Palette
.aEntries
[i
].peRed
= r
* 51;
148 Palette
.aEntries
[i
].peGreen
= g
* 51;
149 Palette
.aEntries
[i
].peBlue
= b
* 51;
152 for (i
= 216; i
< 246; i
++)
154 int v
= (i
- 216) << 3;
155 Palette
.aEntries
[i
].peRed
= v
;
156 Palette
.aEntries
[i
].peGreen
= v
;
157 Palette
.aEntries
[i
].peBlue
= v
;
160 return NtGdiCreatePalette((LOGPALETTE
*)&Palette
);
163 HPALETTE STDCALL
NtGdiCreatePalette(CONST PLOGPALETTE palette
)
167 HPALETTE NewPalette
= PALETTE_AllocPalette(
169 palette
->palNumEntries
,
170 (PULONG
)palette
->palPalEntry
,
173 PalGDI
= (PPALGDI
) PALETTE_LockPalette(NewPalette
);
174 /* FIXME - Handle PalGDI == NULL!!!! */
176 PALETTE_ValidateFlags(PalGDI
->IndexedColors
, PalGDI
->NumColors
);
177 PalGDI
->logicalToSystem
= NULL
;
179 PALETTE_UnlockPalette(NewPalette
);
184 BOOL STDCALL
NtGdiGetColorAdjustment(HDC hDC
,
185 LPCOLORADJUSTMENT ca
)
191 unsigned short GetNumberOfBits(unsigned int dwMask
)
193 unsigned short wBits
;
194 for (wBits
= 0; dwMask
; dwMask
= dwMask
& (dwMask
- 1))
199 COLORREF STDCALL
NtGdiGetNearestColor(HDC hDC
, COLORREF Color
)
201 COLORREF nearest
= CLR_INVALID
;
204 LONG RBits
, GBits
, BBits
;
209 HPALETTE hpal
= dc
->w
.hPalette
;
210 palGDI
= (PPALGDI
) PALETTE_LockPalette(hpal
);
217 switch (palGDI
->Mode
)
220 nearest
= COLOR_LookupNearestColor(palGDI
->IndexedColors
,
221 palGDI
->NumColors
, Color
);
228 RBits
= 8 - GetNumberOfBits(palGDI
->RedMask
);
229 GBits
= 8 - GetNumberOfBits(palGDI
->GreenMask
);
230 BBits
= 8 - GetNumberOfBits(palGDI
->BlueMask
);
232 (GetRValue(Color
) >> RBits
) << RBits
,
233 (GetGValue(Color
) >> GBits
) << GBits
,
234 (GetBValue(Color
) >> BBits
) << BBits
);
237 PALETTE_UnlockPalette(hpal
);
244 UINT STDCALL
NtGdiGetNearestPaletteIndex(HPALETTE hpal
,
247 PPALGDI palGDI
= (PPALGDI
) PALETTE_LockPalette(hpal
);
252 /* Return closest match for the given RGB color */
253 index
= COLOR_PaletteLookupPixel(palGDI
->IndexedColors
, palGDI
->NumColors
, NULL
, Color
, FALSE
);
254 PALETTE_UnlockPalette(hpal
);
260 UINT STDCALL
NtGdiGetPaletteEntries(HPALETTE hpal
,
268 palGDI
= (PPALGDI
) PALETTE_LockPalette(hpal
);
274 numEntries
= palGDI
->NumColors
;
275 if (numEntries
< StartIndex
+ Entries
)
277 Entries
= numEntries
- StartIndex
;
281 if (numEntries
<= StartIndex
)
283 PALETTE_UnlockPalette(hpal
);
286 memcpy(pe
, palGDI
->IndexedColors
+ StartIndex
, Entries
* sizeof(PALETTEENTRY
));
287 for (numEntries
= 0; numEntries
< Entries
; numEntries
++)
289 if (pe
[numEntries
].peFlags
& 0xF0)
291 pe
[numEntries
].peFlags
= 0;
296 PALETTE_UnlockPalette(hpal
);
300 UINT STDCALL
NtGdiGetSystemPaletteEntries(HDC hDC
,
308 if (!(dc = AccessUserObject(hdc))) return 0;
312 Entries = dc->GDIInfo->ulNumPalReg;
316 if (StartIndex >= dc->GDIInfo->ulNumPalReg)
322 if (StartIndex + Entries >= dc->GDIInfo->ulNumPalReg) Entries = dc->GDIInfo->ulNumPalReg - StartIndex;
324 for (i = 0; i < Entries; i++)
326 *(COLORREF*)(entries + i) = COLOR_GetSystemPaletteEntry(StartIndex + i);
330 // GDI_ReleaseObj(hdc);
332 // FIXME UNIMPLEMENTED;
336 UINT STDCALL
NtGdiGetSystemPaletteUse(HDC hDC
)
338 return SystemPaletteUse
;
342 The RealizePalette function modifies the palette for the device associated with the specified device context. If the device context is a memory DC, the color table for the bitmap selected into the DC is modified. If the device context is a display DC, the physical palette for that device is modified.
344 A logical palette is a buffer between color-intensive applications and the system, allowing these applications to use as many colors as needed without interfering with colors displayed by other windows.
346 1= IF DRAWING TO A DEVICE
347 -- If it is a paletted bitmap, and is not an identity palette, then an XLATEOBJ is created between the logical palette and
349 -- If it is an RGB palette, then an XLATEOBJ is created between the RGB values and the system palette.
351 2= IF DRAWING TO A MEMORY DC\BITMAP
352 -- If it is a paletted bitmap, and is not an identity palette, then an XLATEOBJ is created between the logical palette and
354 -- If it is an RGB palette, then an XLATEOBJ is created between the RGB values and the dc palette.
356 UINT STDCALL
NtGdiRealizePalette(HDC hDC
)
359 * This function doesn't do any real work now and there's plenty
360 * of bugd in it (calling SetPalette for high/true-color modes,
361 * using DEFAULT_PALETTE instead of the device palette, ...).
364 DPRINT1("NtGdiRealizePalette is unimplemented\n");
367 PALOBJ
*palPtr
, *sysPtr
;
368 PPALGDI palGDI
, sysGDI
;
371 HPALETTE systemPalette
;
374 USHORT sysMode
, palMode
;
380 SurfObj
= (SURFOBJ
*)AccessUserObject((ULONG
)dc
->Surface
);
381 systemPalette
= NtGdiGetStockObject((INT
)DEFAULT_PALETTE
);
382 palGDI
= PALETTE_LockPalette(dc
->w
.hPalette
);
383 palPtr
= (PALOBJ
*) palGDI
;
384 /* FIXME - Handle palGDI == NULL!!!! */
386 // Step 1: Create mapping of system palette\DC palette
388 realized
= PALETTE_SetMapping(palPtr
, 0, palGDI
->NumColors
,
389 (dc
->w
.hPalette
!= hPrimaryPalette
) ||
390 (dc
->w
.hPalette
== NtGdiGetStockObject(DEFAULT_PALETTE
)));
395 sysGDI
= PALETTE_LockPalette(systemPalette
);
396 sysPtr
= (PALOBJ
*) sysGDI
;
397 /* FIXME - Handle sysGDI == NULL!!!!! */
400 // The RealizePalette function modifies the palette for the device associated with the specified device context. If the
401 // device context is a memory DC, the color table for the bitmap selected into the DC is modified. If the device
402 // context is a display DC, the physical palette for that device is modified.
403 if(dc
->w
.flags
== DC_MEMORY
)
406 DbgPrint("win32k: realizepalette unimplemented step 2 for DC_MEMORY");
408 if(GDIDEVFUNCS(SurfObj
).SetPalette
)
410 ASSERT(sysGDI
->NumColors
<= 256);
411 success
= GDIDEVFUNCS(SurfObj
).SetPalette(
412 dc
->PDev
, sysPtr
, 0, 0, sysGDI
->NumColors
);
416 // need to pass this to IntEngCreateXlate with palettes unlocked
417 sysMode
= sysGDI
->Mode
;
418 palMode
= palGDI
->Mode
;
419 PALETTE_UnlockPalette(systemPalette
);
420 PALETTE_UnlockPalette(dc
->w
.hPalette
);
422 // Step 3: Create the XLATEOBJ for device managed DCs
423 if(dc
->w
.flags
!= DC_MEMORY
)
426 palGDI
->logicalToSystem
= IntEngCreateXlate(sysMode
, palMode
, systemPalette
, dc
->w
.hPalette
);
435 BOOL STDCALL
NtGdiResizePalette(HPALETTE hpal
,
438 /* PALOBJ *palPtr = (PALOBJ*)AccessUserObject(hPal);
439 UINT cPrevEnt, prevVer;
440 INT prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
441 XLATEOBJ *XlateObj = NULL;
443 if(!palPtr) return FALSE;
444 cPrevEnt = palPtr->logpalette->palNumEntries;
445 prevVer = palPtr->logpalette->palVersion;
446 prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) + sizeof(int*) + sizeof(GDIOBJHDR);
447 size += sizeof(int*) + sizeof(GDIOBJHDR);
448 XlateObj = palPtr->logicalToSystem;
450 if (!(palPtr = GDI_ReallocObject(size, hPal, palPtr))) return FALSE;
454 XLATEOBJ *NewXlateObj = (int*) HeapReAlloc(GetProcessHeap(), 0, XlateObj, cEntries * sizeof(int));
455 if(NewXlateObj == NULL)
457 ERR("Can not resize logicalToSystem -- out of memory!");
458 GDI_ReleaseObj( hPal );
461 palPtr->logicalToSystem = NewXlateObj;
464 if(cEntries > cPrevEnt)
466 if(XlateObj) memset(palPtr->logicalToSystem + cPrevEnt, 0, (cEntries - cPrevEnt)*sizeof(int));
467 memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
468 PALETTE_ValidateFlags((PALETTEENTRY*)((BYTE*)palPtr + prevsize), cEntries - cPrevEnt );
470 palPtr->logpalette->palNumEntries = cEntries;
471 palPtr->logpalette->palVersion = prevVer;
472 // GDI_ReleaseObj( hPal );
480 * Select logical palette into device context.
481 * \param hDC handle to the device context
482 * \param hpal handle to the palette
483 * \param ForceBackground If this value is FALSE the logical palette will be copied to the device palette only when the applicatioon
484 * is in the foreground. If this value is TRUE then map the colors in the logical palette to the device
485 * palette colors in the best way.
486 * \return old palette
488 * \todo implement ForceBackground == TRUE
490 HPALETTE STDCALL
NtGdiSelectPalette(HDC hDC
,
492 BOOL ForceBackground
)
495 HPALETTE oldPal
= NULL
;
498 // FIXME: mark the palette as a [fore\back]ground pal
502 /* Check if this is a valid palette handle */
503 PalGDI
= PALETTE_LockPalette(hpal
);
506 /* Is this a valid palette for this depth? */
507 if ((dc
->w
.bitsPerPixel
<= 8 && PAL_INDEXED
== PalGDI
->Mode
)
508 || (8 < dc
->w
.bitsPerPixel
&& PAL_INDEXED
!= PalGDI
->Mode
))
510 PALETTE_UnlockPalette(hpal
);
511 oldPal
= dc
->w
.hPalette
;
512 dc
->w
.hPalette
= hpal
;
516 PALETTE_UnlockPalette(hpal
);
530 BOOL STDCALL
NtGdiSetColorAdjustment(HDC hDC
,
531 CONST LPCOLORADJUSTMENT ca
)
537 UINT STDCALL
NtGdiSetPaletteEntries(HPALETTE hpal
,
540 CONST LPPALETTEENTRY pe
)
545 palGDI
= PALETTE_LockPalette(hpal
);
546 if (!palGDI
) return 0;
548 numEntries
= palGDI
->NumColors
;
549 if (Start
>= numEntries
)
551 PALETTE_UnlockPalette(hpal
);
554 if (numEntries
< Start
+ Entries
)
556 Entries
= numEntries
- Start
;
558 memcpy(palGDI
->IndexedColors
+ Start
, pe
, Entries
* sizeof(PALETTEENTRY
));
559 PALETTE_ValidateFlags(palGDI
->IndexedColors
, palGDI
->NumColors
);
560 ExFreePool(palGDI
->logicalToSystem
);
561 palGDI
->logicalToSystem
= NULL
;
562 PALETTE_UnlockPalette(hpal
);
568 NtGdiSetSystemPaletteUse(HDC hDC
, UINT Usage
)
570 UINT old
= SystemPaletteUse
;
572 /* Device doesn't support colour palettes */
573 if (!(NtGdiGetDeviceCaps(hDC
, RASTERCAPS
) & RC_PALETTE
)) {
579 case SYSPAL_NOSTATIC
:
580 case SYSPAL_NOSTATIC256
:
582 SystemPaletteUse
= Usage
;
594 NtGdiUnrealizeObject(HGDIOBJ hgdiobj
)
601 NtGdiUpdateColors(HDC hDC
)
605 hWnd
= (HWND
)NtUserCallOneParam((DWORD
)hDC
, ONEPARAM_ROUTINE_WINDOWFROMDC
);
608 SetLastWin32Error(ERROR_INVALID_WINDOW_HANDLE
);
611 return NtUserRedrawWindow(hWnd
, NULL
, 0, RDW_INVALIDATE
);
614 INT STDCALL
COLOR_PaletteLookupPixel(PALETTEENTRY
*palPalEntry
, INT size
,
615 XLATEOBJ
*XlateObj
, COLORREF col
, BOOL skipReserved
)
617 int i
, best
= 0, diff
= 0x7fffffff;
620 for( i
= 0; i
< size
&& diff
; i
++ )
623 if(!(palPalEntry
[i
].peFlags
& PC_SYS_USED
) || (skipReserved
&& palPalEntry
[i
].peFlags
& PC_SYS_RESERVED
))
627 r
= abs((SHORT
)palPalEntry
[i
].peRed
- GetRValue(col
));
628 g
= abs((SHORT
)palPalEntry
[i
].peGreen
- GetGValue(col
));
629 b
= abs((SHORT
)palPalEntry
[i
].peBlue
- GetBValue(col
));
633 if( r
< diff
) { best
= i
; diff
= r
; }
636 if (XlateObj
== NULL
)
639 return (XlateObj
->pulXlate
) ? (INT
)XlateObj
->pulXlate
[best
] : best
;
642 COLORREF STDCALL
COLOR_LookupNearestColor( PALETTEENTRY
* palPalEntry
, int size
, COLORREF color
)
646 index
= COLOR_PaletteLookupPixel(palPalEntry
, size
, NULL
, color
, FALSE
);
648 palPalEntry
[index
].peRed
,
649 palPalEntry
[index
].peGreen
,
650 palPalEntry
[index
].peBlue
);
653 int STDCALL
COLOR_PaletteLookupExactIndex( PALETTEENTRY
* palPalEntry
, int size
,
657 BYTE r
= GetRValue(col
), g
= GetGValue(col
), b
= GetBValue(col
);
658 for( i
= 0; i
< size
; i
++ )
660 if( palPalEntry
[i
].peFlags
& PC_SYS_USED
) /* skips gap */
661 if(palPalEntry
[i
].peRed
== r
&& palPalEntry
[i
].peGreen
== g
&& palPalEntry
[i
].peBlue
== b
) return i
;