blob: 9339e32517b72088723d7aa0af4de0f84f41000f [file] [log] [blame]
Alexandre Julliard401710d1993-09-04 10:09:32 +00001/*
2 * GDI palette objects
3 *
Alexandre Julliard8d24ae61994-04-05 21:42:43 +00004 * Copyright 1993,1994 Alexandre Julliard
Alexandre Julliard18f92e71996-07-17 20:02:21 +00005 * Copyright 1996 Alex Korobka
Alexandre Julliard234bc241994-12-10 13:02:28 +00006 *
Alexandre Julliard23946ad1997-06-16 17:43:53 +00007 * PALETTEOBJ is documented in the Dr. Dobbs Journal May 1993.
8 * Information in the "Undocumented Windows" is incorrect.
Alexandre Julliard59730ae1996-03-24 16:20:51 +00009 */
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +000010
Alexandre Julliard401710d1993-09-04 10:09:32 +000011#include <stdlib.h>
Alexandre Julliard8d24ae61994-04-05 21:42:43 +000012#include <string.h>
Patrik Stridvallb87fe2e1999-04-01 08:16:08 +000013
Michael Veksler92ae2191999-05-02 11:39:09 +000014#include "winbase.h"
Jeremy Whited3e22d92000-02-10 19:03:02 +000015#include "windef.h"
16#include "wingdi.h"
Michael Veksler92ae2191999-05-02 11:39:09 +000017#include "wine/winuser16.h"
Alexandre Julliard75d86e11996-11-17 18:59:11 +000018#include "gdi.h"
Alexandre Julliard234bc241994-12-10 13:02:28 +000019#include "color.h"
Alexandre Julliard7cbe6571995-01-09 18:21:16 +000020#include "palette.h"
Alexandre Julliard15657091999-05-23 10:25:25 +000021#include "debugtools.h"
Ian Schmidt31b47211999-09-22 15:12:41 +000022#include "winerror.h"
Alexandre Julliard401710d1993-09-04 10:09:32 +000023
Alexandre Julliard39932162000-09-16 20:57:39 +000024DEFAULT_DEBUG_CHANNEL(palette);
Patrik Stridvallb4b9fae1999-04-19 14:56:29 +000025
Patrik Stridvallb87fe2e1999-04-01 08:16:08 +000026PALETTE_DRIVER *PALETTE_Driver = NULL;
27
Alexandre Julliard39932162000-09-16 20:57:39 +000028/* Pointers to USER implementation of SelectPalette/RealizePalette */
29/* they will be patched by USER on startup */
Alexandre Julliarda3960291999-02-26 11:11:13 +000030FARPROC pfnSelectPalette = NULL;
31FARPROC pfnRealizePalette = NULL;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000032
Alexandre Julliarda3960291999-02-26 11:11:13 +000033static UINT SystemPaletteUse = SYSPAL_STATIC; /* currently not considered */
Alexandre Julliarda2f2e011995-06-06 16:40:35 +000034
Alexandre Julliard18f92e71996-07-17 20:02:21 +000035static HPALETTE16 hPrimaryPalette = 0; /* used for WM_PALETTECHANGED */
36static HPALETTE16 hLastRealizedPalette = 0; /* UnrealizeObject() needs it */
37
Alexandre Julliard902da691995-11-05 14:39:02 +000038
39/***********************************************************************
Alexandre Julliard23946ad1997-06-16 17:43:53 +000040 * PALETTE_Init
41 *
42 * Create the system palette.
43 */
44HPALETTE16 PALETTE_Init(void)
45{
Alexandre Julliard23946ad1997-06-16 17:43:53 +000046 int i;
47 HPALETTE16 hpalette;
48 LOGPALETTE * palPtr;
49 PALETTEOBJ* palObj;
50 const PALETTEENTRY* __sysPalTemplate = COLOR_GetSystemPaletteTemplate();
51
52 /* create default palette (20 system colors) */
53
Alexandre Julliard491502b1997-11-01 19:08:16 +000054 palPtr = HeapAlloc( GetProcessHeap(), 0,
55 sizeof(LOGPALETTE) + (NB_RESERVED_COLORS-1)*sizeof(PALETTEENTRY));
Alexandre Julliard23946ad1997-06-16 17:43:53 +000056 if (!palPtr) return FALSE;
57
58 palPtr->palVersion = 0x300;
59 palPtr->palNumEntries = NB_RESERVED_COLORS;
60 for( i = 0; i < NB_RESERVED_COLORS; i ++ )
61 {
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;
66 }
67 hpalette = CreatePalette16( palPtr );
Dimitrie O. Paun9ad96362000-03-19 14:29:50 +000068 HeapFree( GetProcessHeap(), 0, palPtr );
Alexandre Julliard23946ad1997-06-16 17:43:53 +000069
70 palObj = (PALETTEOBJ*) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
Juergen Schmied58b2f0a1999-01-23 12:09:30 +000071 if (palObj)
72 {
Dimitrie O. Paun9ad96362000-03-19 14:29:50 +000073 if (!(palObj->mapping = HeapAlloc( GetProcessHeap(), 0, sizeof(int) * 20 )))
Francois Gougete76218d2001-05-09 17:31:31 +000074 ERR("Can not create palette mapping -- out of memory!\n");
Alexandre Julliard2a2321b2000-08-19 21:38:55 +000075 GDI_ReleaseObj( hpalette );
Juergen Schmied58b2f0a1999-01-23 12:09:30 +000076 }
Alexandre Julliard23946ad1997-06-16 17:43:53 +000077 return hpalette;
78}
79
80/***********************************************************************
Alexandre Julliard18f92e71996-07-17 20:02:21 +000081 * PALETTE_ValidateFlags
82 */
83void PALETTE_ValidateFlags(PALETTEENTRY* lpPalE, int size)
84{
85 int i = 0;
86 for( ; i<size ; i++ )
87 lpPalE[i].peFlags = PC_SYS_USED | (lpPalE[i].peFlags & 0x07);
88}
89
90
91/***********************************************************************
Patrik Stridvall17fd4e32001-06-28 18:04:41 +000092 * CreatePalette (GDI.360)
Alexandre Julliard401710d1993-09-04 10:09:32 +000093 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +000094HPALETTE16 WINAPI CreatePalette16( const LOGPALETTE* palette )
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +000095{
Alexandre Julliarda3960291999-02-26 11:11:13 +000096 return CreatePalette( palette );
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +000097}
98
99
100/***********************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +0000101 * CreatePalette [GDI32.@] Creates a logical color palette
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000102 *
103 * RETURNS
104 * Success: Handle to logical palette
105 * Failure: NULL
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000106 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000107HPALETTE WINAPI CreatePalette(
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000108 const LOGPALETTE* palette) /* [in] Pointer to logical color palette */
Alexandre Julliard401710d1993-09-04 10:09:32 +0000109{
110 PALETTEOBJ * palettePtr;
Alexandre Julliarda3960291999-02-26 11:11:13 +0000111 HPALETTE hpalette;
Juergen Schmiedc5096151998-11-22 16:16:58 +0000112 int size;
113
114 if (!palette) return 0;
Alexandre Julliard15657091999-05-23 10:25:25 +0000115 TRACE("entries=%i\n", palette->palNumEntries);
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000116
Juergen Schmiedc5096151998-11-22 16:16:58 +0000117 size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
118
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000119 if (!(palettePtr = GDI_AllocObject( size + sizeof(int*) +sizeof(GDIOBJHDR),
120 PALETTE_MAGIC, &hpalette ))) return 0;
Alexandre Julliard401710d1993-09-04 10:09:32 +0000121 memcpy( &palettePtr->logpalette, palette, size );
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000122 PALETTE_ValidateFlags(palettePtr->logpalette.palPalEntry,
123 palettePtr->logpalette.palNumEntries);
124 palettePtr->mapping = NULL;
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000125 GDI_ReleaseObj( hpalette );
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000126
Alexandre Julliard15657091999-05-23 10:25:25 +0000127 TRACE(" returning %04x\n", hpalette);
Alexandre Julliard401710d1993-09-04 10:09:32 +0000128 return hpalette;
129}
130
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000131
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000132/***********************************************************************
Patrik Stridvall17fd4e32001-06-28 18:04:41 +0000133 * CreateHalftonePalette [GDI.529] Creates a halftone palette
Brad Pepers0bdff361999-02-09 15:37:16 +0000134 *
135 * RETURNS
136 * Success: Handle to logical halftone palette
137 * Failure: 0
138 */
139HPALETTE16 WINAPI CreateHalftonePalette16(
140 HDC16 hdc) /* [in] Handle to device context */
141{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000142 return CreateHalftonePalette(hdc);
Patrik Stridvallb87fe2e1999-04-01 08:16:08 +0000143}
Brad Pepers0bdff361999-02-09 15:37:16 +0000144
145
146/***********************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +0000147 * CreateHalftonePalette [GDI32.@] Creates a halftone palette
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000148 *
149 * RETURNS
150 * Success: Handle to logical halftone palette
151 * Failure: 0
Juergen Schmied58b2f0a1999-01-23 12:09:30 +0000152 *
Aric Stewartae7fe2d2002-01-29 02:53:43 +0000153 * FIXME: This simply creates the halftone palette dirived from runing
154 * tests on an windows NT machine. this is assuming a color depth
155 * of greater that 256 color. On a 256 color device the halftone
156 * palette will be differnt and this funtion will be incorrect
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000157 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000158HPALETTE WINAPI CreateHalftonePalette(
159 HDC hdc) /* [in] Handle to device context */
Brad Pepers0bdff361999-02-09 15:37:16 +0000160{
Aric Stewartae7fe2d2002-01-29 02:53:43 +0000161 int i;
Brad Pepers0bdff361999-02-09 15:37:16 +0000162 struct {
163 WORD Version;
164 WORD NumberOfEntries;
165 PALETTEENTRY aEntries[256];
Patrik Stridvallf87b96e2000-10-24 02:20:31 +0000166 } Palette;
Juergen Schmied58b2f0a1999-01-23 12:09:30 +0000167
Patrik Stridvallf87b96e2000-10-24 02:20:31 +0000168 Palette.Version = 0x300;
169 Palette.NumberOfEntries = 256;
Alexandre Julliarda3960291999-02-26 11:11:13 +0000170 GetSystemPaletteEntries(hdc, 0, 256, Palette.aEntries);
Juergen Schmied58b2f0a1999-01-23 12:09:30 +0000171
Aric Stewartae7fe2d2002-01-29 02:53:43 +0000172 Palette.NumberOfEntries = 20;
173
174 for (i = 0; i < Palette.NumberOfEntries; i++)
175 {
176 Palette.aEntries[i].peRed=0xff;
177 Palette.aEntries[i].peGreen=0xff;
178 Palette.aEntries[i].peBlue=0xff;
179 Palette.aEntries[i].peFlags=0x00;
180 }
181
182 Palette.aEntries[0].peRed=0x00;
183 Palette.aEntries[0].peBlue=0x00;
184 Palette.aEntries[0].peGreen=0x00;
185
186 /* the first 6 */
187 for (i=1; i <= 6; i++)
188 {
189 Palette.aEntries[i].peRed=(i%2)?0x80:0;
190 Palette.aEntries[i].peGreen=(i==2)?0x80:(i==3)?0x80:(i==6)?0x80:0;
191 Palette.aEntries[i].peBlue=(i>3)?0x80:0;
192 }
193
194 for (i=7; i <= 12; i++)
195 {
196 switch(i)
197 {
198 case 7:
199 Palette.aEntries[i].peRed=0xc0;
200 Palette.aEntries[i].peBlue=0xc0;
201 Palette.aEntries[i].peGreen=0xc0;
202 break;
203 case 8:
204 Palette.aEntries[i].peRed=0xc0;
205 Palette.aEntries[i].peGreen=0xdc;
206 Palette.aEntries[i].peBlue=0xc0;
207 break;
208 case 9:
209 Palette.aEntries[i].peRed=0xa6;
210 Palette.aEntries[i].peGreen=0xca;
211 Palette.aEntries[i].peBlue=0xf0;
212 break;
213 case 10:
214 Palette.aEntries[i].peRed=0xff;
215 Palette.aEntries[i].peGreen=0xfb;
216 Palette.aEntries[i].peBlue=0xf0;
217 break;
218 case 11:
219 Palette.aEntries[i].peRed=0xa0;
220 Palette.aEntries[i].peGreen=0xa0;
221 Palette.aEntries[i].peBlue=0xa4;
222 break;
223 case 12:
224 Palette.aEntries[i].peRed=0x80;
225 Palette.aEntries[i].peGreen=0x80;
226 Palette.aEntries[i].peBlue=0x80;
227 }
228 }
229
230 for (i=13; i <= 18; i++)
231 {
232 Palette.aEntries[i].peRed=(i%2)?0xff:0;
233 Palette.aEntries[i].peGreen=(i==14)?0xff:(i==15)?0xff:(i==18)?0xff:0;
234 Palette.aEntries[i].peBlue=(i>15)?0xff:0x00;
235 }
236
Alexandre Julliarda3960291999-02-26 11:11:13 +0000237 return CreatePalette((LOGPALETTE *)&Palette);
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000238}
Alexandre Julliard401710d1993-09-04 10:09:32 +0000239
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000240
Alexandre Julliard401710d1993-09-04 10:09:32 +0000241/***********************************************************************
Patrik Stridvall17fd4e32001-06-28 18:04:41 +0000242 * GetPaletteEntries (GDI.363)
Alexandre Julliard401710d1993-09-04 10:09:32 +0000243 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000244UINT16 WINAPI GetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
245 UINT16 count, LPPALETTEENTRY entries )
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000246{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000247 return GetPaletteEntries( hpalette, start, count, entries );
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000248}
249
250
251/***********************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +0000252 * GetPaletteEntries [GDI32.@] Retrieves palette entries
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000253 *
254 * RETURNS
255 * Success: Number of entries from logical palette
256 * Failure: 0
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000257 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000258UINT WINAPI GetPaletteEntries(
259 HPALETTE hpalette, /* [in] Handle of logical palette */
260 UINT start, /* [in] First entry to receive */
261 UINT count, /* [in] Number of entries to receive */
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000262 LPPALETTEENTRY entries) /* [out] Address of array receiving entries */
Alexandre Julliard401710d1993-09-04 10:09:32 +0000263{
264 PALETTEOBJ * palPtr;
Joerg Mayer4d756402001-01-10 22:45:33 +0000265 UINT numEntries;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000266
Alexandre Julliard15657091999-05-23 10:25:25 +0000267 TRACE("hpal = %04x, count=%i\n", hpalette, count );
Alexandre Julliard401710d1993-09-04 10:09:32 +0000268
269 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
270 if (!palPtr) return 0;
Aric Stewartae7fe2d2002-01-29 02:53:43 +0000271
272 /* NOTE: not documented but test show this to be the case */
273 if (count == 0)
274 {
275 int rc = palPtr->logpalette.palNumEntries;
276 GDI_ReleaseObj( hpalette );
277 return rc;
278 }
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000279
Alexandre Julliard401710d1993-09-04 10:09:32 +0000280 numEntries = palPtr->logpalette.palNumEntries;
Alexandre Julliard401710d1993-09-04 10:09:32 +0000281 if (start+count > numEntries) count = numEntries - start;
Alexandre Julliard829fe321998-07-26 14:27:39 +0000282 if (entries)
283 {
284 if (start >= numEntries)
285 {
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000286 GDI_ReleaseObj( hpalette );
Alexandre Julliard829fe321998-07-26 14:27:39 +0000287 return 0;
288 }
289 memcpy( entries, &palPtr->logpalette.palPalEntry[start],
290 count * sizeof(PALETTEENTRY) );
291 for( numEntries = 0; numEntries < count ; numEntries++ )
292 if (entries[numEntries].peFlags & 0xF0)
293 entries[numEntries].peFlags = 0;
Alexandre Julliard829fe321998-07-26 14:27:39 +0000294 }
295
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000296 GDI_ReleaseObj( hpalette );
Alexandre Julliard401710d1993-09-04 10:09:32 +0000297 return count;
298}
299
300
301/***********************************************************************
Patrik Stridvall17fd4e32001-06-28 18:04:41 +0000302 * SetPaletteEntries (GDI.364)
Alexandre Julliard401710d1993-09-04 10:09:32 +0000303 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000304UINT16 WINAPI SetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
305 UINT16 count, LPPALETTEENTRY entries )
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000306{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000307 return SetPaletteEntries( hpalette, start, count, entries );
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000308}
309
310
311/***********************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +0000312 * SetPaletteEntries [GDI32.@] Sets color values for range in palette
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000313 *
314 * RETURNS
315 * Success: Number of entries that were set
316 * Failure: 0
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000317 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000318UINT WINAPI SetPaletteEntries(
319 HPALETTE hpalette, /* [in] Handle of logical palette */
320 UINT start, /* [in] Index of first entry to set */
321 UINT count, /* [in] Number of entries to set */
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000322 LPPALETTEENTRY entries) /* [in] Address of array of structures */
Alexandre Julliard401710d1993-09-04 10:09:32 +0000323{
324 PALETTEOBJ * palPtr;
Joerg Mayer4d756402001-01-10 22:45:33 +0000325 UINT numEntries;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000326
Alexandre Julliard15657091999-05-23 10:25:25 +0000327 TRACE("hpal=%04x,start=%i,count=%i\n",hpalette,start,count );
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000328
Alexandre Julliard401710d1993-09-04 10:09:32 +0000329 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
330 if (!palPtr) return 0;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000331
Alexandre Julliard401710d1993-09-04 10:09:32 +0000332 numEntries = palPtr->logpalette.palNumEntries;
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000333 if (start >= numEntries)
334 {
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000335 GDI_ReleaseObj( hpalette );
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000336 return 0;
337 }
Alexandre Julliard401710d1993-09-04 10:09:32 +0000338 if (start+count > numEntries) count = numEntries - start;
339 memcpy( &palPtr->logpalette.palPalEntry[start], entries,
340 count * sizeof(PALETTEENTRY) );
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000341 PALETTE_ValidateFlags(palPtr->logpalette.palPalEntry,
342 palPtr->logpalette.palNumEntries);
Alexandre Julliarda7116b02000-03-28 20:02:37 +0000343 HeapFree( GetProcessHeap(), 0, palPtr->mapping );
Alexandre Julliardac9c9b01996-07-28 18:50:11 +0000344 palPtr->mapping = NULL;
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000345 GDI_ReleaseObj( hpalette );
Alexandre Julliard401710d1993-09-04 10:09:32 +0000346 return count;
347}
348
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000349
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000350/***********************************************************************
Patrik Stridvall17fd4e32001-06-28 18:04:41 +0000351 * ResizePalette (GDI.368)
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000352 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000353BOOL16 WINAPI ResizePalette16( HPALETTE16 hPal, UINT16 cEntries )
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000354{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000355 return ResizePalette( hPal, cEntries );
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000356}
357
358
359/***********************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +0000360 * ResizePalette [GDI32.@] Resizes logical palette
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000361 *
362 * RETURNS
363 * Success: TRUE
364 * Failure: FALSE
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000365 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000366BOOL WINAPI ResizePalette(
367 HPALETTE hPal, /* [in] Handle of logical palette */
368 UINT cEntries) /* [in] Number of entries in logical palette */
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000369{
Alexandre Julliard75d86e11996-11-17 18:59:11 +0000370 PALETTEOBJ * palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
Alexandre Julliarda3960291999-02-26 11:11:13 +0000371 UINT cPrevEnt, prevVer;
Alexandre Julliard75d86e11996-11-17 18:59:11 +0000372 int prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
373 int* mapping = NULL;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000374
Alexandre Julliard15657091999-05-23 10:25:25 +0000375 TRACE("hpal = %04x, prev = %i, new = %i\n",
Alexandre Julliard889f7421997-04-15 17:19:52 +0000376 hPal, palPtr ? palPtr->logpalette.palNumEntries : -1,
377 cEntries );
Alexandre Julliard75d86e11996-11-17 18:59:11 +0000378 if( !palPtr ) return FALSE;
379 cPrevEnt = palPtr->logpalette.palNumEntries;
380 prevVer = palPtr->logpalette.palVersion;
381 prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) +
382 sizeof(int*) + sizeof(GDIOBJHDR);
383 size += sizeof(int*) + sizeof(GDIOBJHDR);
384 mapping = palPtr->mapping;
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000385
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000386 if (!(palPtr = GDI_ReallocObject( size, hPal, palPtr ))) return FALSE;
Alexandre Julliard75d86e11996-11-17 18:59:11 +0000387
Dimitrie O. Paun9ad96362000-03-19 14:29:50 +0000388 if( mapping )
389 {
390 int *newMap = (int*) HeapReAlloc(GetProcessHeap(), 0,
391 mapping, cEntries * sizeof(int) );
392 if(newMap == NULL)
393 {
Francois Gougete76218d2001-05-09 17:31:31 +0000394 ERR("Can not resize mapping -- out of memory!\n");
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000395 GDI_ReleaseObj( hPal );
Dimitrie O. Paun9ad96362000-03-19 14:29:50 +0000396 return FALSE;
397 }
398 palPtr->mapping = newMap;
399 }
400
Alexandre Julliard75d86e11996-11-17 18:59:11 +0000401 if( cEntries > cPrevEnt )
402 {
403 if( mapping )
404 memset(palPtr->mapping + cPrevEnt, 0, (cEntries - cPrevEnt)*sizeof(int));
405 memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
406 PALETTE_ValidateFlags((PALETTEENTRY*)((BYTE*)palPtr + prevsize),
407 cEntries - cPrevEnt );
408 }
409 palPtr->logpalette.palNumEntries = cEntries;
410 palPtr->logpalette.palVersion = prevVer;
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000411 GDI_ReleaseObj( hPal );
Alexandre Julliard75d86e11996-11-17 18:59:11 +0000412 return TRUE;
Alexandre Julliardade697e1995-11-26 13:59:11 +0000413}
414
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000415
Alexandre Julliardade697e1995-11-26 13:59:11 +0000416/***********************************************************************
Patrik Stridvall17fd4e32001-06-28 18:04:41 +0000417 * AnimatePalette (GDI.367)
Alexandre Julliardade697e1995-11-26 13:59:11 +0000418 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000419void WINAPI AnimatePalette16( HPALETTE16 hPal, UINT16 StartIndex,
François Gouget241c7301998-10-28 10:47:09 +0000420 UINT16 NumEntries, const PALETTEENTRY* PaletteColors)
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000421{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000422 AnimatePalette( hPal, StartIndex, NumEntries, PaletteColors );
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000423}
424
425
426/***********************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +0000427 * AnimatePalette [GDI32.@] Replaces entries in logical palette
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000428 *
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000429 * RETURNS
430 * Success: TRUE
431 * Failure: FALSE
432 *
433 * FIXME
434 * Should use existing mapping when animating a primary palette
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000435 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000436BOOL WINAPI AnimatePalette(
437 HPALETTE hPal, /* [in] Handle to logical palette */
438 UINT StartIndex, /* [in] First entry in palette */
439 UINT NumEntries, /* [in] Count of entries in palette */
François Gouget241c7301998-10-28 10:47:09 +0000440 const PALETTEENTRY* PaletteColors) /* [in] Pointer to first replacement */
Alexandre Julliardade697e1995-11-26 13:59:11 +0000441{
Alexandre Julliard15657091999-05-23 10:25:25 +0000442 TRACE("%04x (%i - %i)\n", hPal, StartIndex,StartIndex+NumEntries);
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000443
Alexandre Julliard4c186362000-10-29 01:22:15 +0000444 if( hPal != GetStockObject(DEFAULT_PALETTE) )
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000445 {
446 PALETTEOBJ* palPtr = (PALETTEOBJ *)GDI_GetObjPtr(hPal, PALETTE_MAGIC);
Juergen Schmied58b2f0a1999-01-23 12:09:30 +0000447 if (!palPtr) return FALSE;
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000448
Alexandre Julliard02e90081998-01-04 17:49:09 +0000449 if( (StartIndex + NumEntries) <= palPtr->logpalette.palNumEntries )
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000450 {
Alexandre Julliarda3960291999-02-26 11:11:13 +0000451 UINT u;
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000452 for( u = 0; u < NumEntries; u++ )
453 palPtr->logpalette.palPalEntry[u + StartIndex] = PaletteColors[u];
Patrik Stridvallb87fe2e1999-04-01 08:16:08 +0000454 PALETTE_Driver->
455 pSetMapping(palPtr, StartIndex, NumEntries,
456 hPal != hPrimaryPalette );
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000457 GDI_ReleaseObj( hPal );
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000458 return TRUE;
459 }
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000460 GDI_ReleaseObj( hPal );
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000461 }
462 return FALSE;
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000463}
Alexandre Julliard401710d1993-09-04 10:09:32 +0000464
Alexandre Julliarda2f2e011995-06-06 16:40:35 +0000465
466/***********************************************************************
Patrik Stridvall17fd4e32001-06-28 18:04:41 +0000467 * SetSystemPaletteUse (GDI.373)
Alexandre Julliard2787be81995-05-22 18:23:01 +0000468 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000469UINT16 WINAPI SetSystemPaletteUse16( HDC16 hdc, UINT16 use )
Alexandre Julliard2787be81995-05-22 18:23:01 +0000470{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000471 return SetSystemPaletteUse( hdc, use );
Alexandre Julliard2787be81995-05-22 18:23:01 +0000472}
473
474
475/***********************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +0000476 * SetSystemPaletteUse [GDI32.@]
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000477 *
478 * RETURNS
479 * Success: Previous system palette
480 * Failure: SYSPAL_ERROR
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000481 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000482UINT WINAPI SetSystemPaletteUse(
483 HDC hdc, /* [in] Handle of device context */
484 UINT use) /* [in] Palette-usage flag */
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000485{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000486 UINT old = SystemPaletteUse;
Alexandre Julliard15657091999-05-23 10:25:25 +0000487 FIXME("(%04x,%04x): stub\n", hdc, use );
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000488 SystemPaletteUse = use;
489 return old;
490}
491
492
493/***********************************************************************
Patrik Stridvall17fd4e32001-06-28 18:04:41 +0000494 * GetSystemPaletteUse (GDI.374)
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000495 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000496UINT16 WINAPI GetSystemPaletteUse16( HDC16 hdc )
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000497{
498 return SystemPaletteUse;
499}
500
501
502/***********************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +0000503 * GetSystemPaletteUse [GDI32.@] Gets state of system palette
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000504 *
505 * RETURNS
506 * Current state of system palette
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000507 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000508UINT WINAPI GetSystemPaletteUse(
509 HDC hdc) /* [in] Handle of device context */
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000510{
511 return SystemPaletteUse;
512}
513
514
515/***********************************************************************
Patrik Stridvall17fd4e32001-06-28 18:04:41 +0000516 * GetSystemPaletteEntries (GDI.375)
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000517 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000518UINT16 WINAPI GetSystemPaletteEntries16( HDC16 hdc, UINT16 start, UINT16 count,
519 LPPALETTEENTRY entries )
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000520{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000521 return GetSystemPaletteEntries( hdc, start, count, entries );
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000522}
523
524
525/***********************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +0000526 * GetSystemPaletteEntries [GDI32.@] Gets range of palette entries
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000527 *
528 * RETURNS
529 * Success: Number of entries retrieved from palette
530 * Failure: 0
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000531 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000532UINT WINAPI GetSystemPaletteEntries(
533 HDC hdc, /* [in] Handle of device context */
534 UINT start, /* [in] Index of first entry to be retrieved */
535 UINT count, /* [in] Number of entries to be retrieved */
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000536 LPPALETTEENTRY entries) /* [out] Array receiving system-palette entries */
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000537{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000538 UINT i;
Alexandre Julliard99bb9f92001-07-28 00:18:02 +0000539 INT sizePalette = GetDeviceCaps( hdc, SIZEPALETTE );
Alexandre Julliardac9c9b01996-07-28 18:50:11 +0000540
Alexandre Julliard15657091999-05-23 10:25:25 +0000541 TRACE("hdc=%04x,start=%i,count=%i\n", hdc,start,count);
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000542
Alexandre Julliard99bb9f92001-07-28 00:18:02 +0000543 if (!entries) return sizePalette;
544 if (start >= sizePalette) return 0;
545 if (start+count >= sizePalette) count = sizePalette - start;
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000546
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000547 for (i = 0; i < count; i++)
548 {
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000549 *(COLORREF*)(entries + i) = COLOR_GetSystemPaletteEntry( start + i );
Alexandre Julliardac9c9b01996-07-28 18:50:11 +0000550
Alexandre Julliard15657091999-05-23 10:25:25 +0000551 TRACE("\tidx(%02x) -> RGB(%08lx)\n",
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000552 start + i, *(COLORREF*)(entries + i) );
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000553 }
554 return count;
555}
556
557
558/***********************************************************************
Patrik Stridvall17fd4e32001-06-28 18:04:41 +0000559 * GetNearestPaletteIndex (GDI.370)
Alexandre Julliard401710d1993-09-04 10:09:32 +0000560 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000561UINT16 WINAPI GetNearestPaletteIndex16( HPALETTE16 hpalette, COLORREF color )
Alexandre Julliard401710d1993-09-04 10:09:32 +0000562{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000563 return GetNearestPaletteIndex( hpalette, color );
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000564}
565
566
567/***********************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +0000568 * GetNearestPaletteIndex [GDI32.@] Gets palette index for color
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000569 *
570 * NOTES
571 * Should index be initialized to CLR_INVALID instead of 0?
572 *
573 * RETURNS
574 * Success: Index of entry in logical palette
575 * Failure: CLR_INVALID
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000576 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000577UINT WINAPI GetNearestPaletteIndex(
578 HPALETTE hpalette, /* [in] Handle of logical color palette */
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000579 COLORREF color) /* [in] Color to be matched */
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000580{
581 PALETTEOBJ* palObj = (PALETTEOBJ*)GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
Alexandre Julliarda3960291999-02-26 11:11:13 +0000582 UINT index = 0;
Alexandre Julliardac9c9b01996-07-28 18:50:11 +0000583
584 if( palObj )
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000585 {
Patrik Stridvallb87fe2e1999-04-01 08:16:08 +0000586 index = COLOR_PaletteLookupPixel(palObj->logpalette.palPalEntry,
587 palObj->logpalette.palNumEntries,
588 NULL, color, FALSE );
Alexandre Julliardac9c9b01996-07-28 18:50:11 +0000589
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000590 GDI_ReleaseObj( hpalette );
591 }
Alexandre Julliard15657091999-05-23 10:25:25 +0000592 TRACE("(%04x,%06lx): returning %d\n", hpalette, color, index );
Alexandre Julliard401710d1993-09-04 10:09:32 +0000593 return index;
594}
595
596
597/***********************************************************************
Patrik Stridvall17fd4e32001-06-28 18:04:41 +0000598 * GetNearestColor (GDI.154)
Alexandre Julliard902da691995-11-05 14:39:02 +0000599 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000600COLORREF WINAPI GetNearestColor16( HDC16 hdc, COLORREF color )
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000601{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000602 return GetNearestColor( hdc, color );
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000603}
604
605
606/***********************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +0000607 * GetNearestColor [GDI32.@] Gets a system color to match
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000608 *
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000609 * RETURNS
610 * Success: Color from system palette that corresponds to given color
611 * Failure: CLR_INVALID
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000612 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000613COLORREF WINAPI GetNearestColor(
614 HDC hdc, /* [in] Handle of device context */
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000615 COLORREF color) /* [in] Color to be matched */
Alexandre Julliard902da691995-11-05 14:39:02 +0000616{
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000617 COLORREF nearest = CLR_INVALID;
Alexandre Julliardac9c9b01996-07-28 18:50:11 +0000618 DC *dc;
619 PALETTEOBJ *palObj;
Alexandre Julliard902da691995-11-05 14:39:02 +0000620
Dave Hawkes314b9fd2002-02-05 19:44:07 +0000621 if(!(GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)) {
622 return color;
623 }
Alexandre Julliard2239abb2000-11-05 02:05:07 +0000624 if ( (dc = DC_GetDCPtr( hdc )) )
Alexandre Julliardac9c9b01996-07-28 18:50:11 +0000625 {
Alexandre Julliard2239abb2000-11-05 02:05:07 +0000626 HPALETTE hpal = (dc->hPalette)? dc->hPalette : GetStockObject( DEFAULT_PALETTE );
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000627 palObj = GDI_GetObjPtr( hpal, PALETTE_MAGIC );
628 if (!palObj) {
629 GDI_ReleaseObj( hdc );
630 return nearest;
631 }
Alexandre Julliardac9c9b01996-07-28 18:50:11 +0000632
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000633 nearest = COLOR_LookupNearestColor( palObj->logpalette.palPalEntry,
634 palObj->logpalette.palNumEntries, color );
635 GDI_ReleaseObj( hpal );
636 GDI_ReleaseObj( hdc );
Alexandre Julliardac9c9b01996-07-28 18:50:11 +0000637 }
638
Alexandre Julliard15657091999-05-23 10:25:25 +0000639 TRACE("(%06lx): returning %06lx\n", color, nearest );
Alexandre Julliard902da691995-11-05 14:39:02 +0000640 return nearest;
641}
642
643
644/***********************************************************************
Alexandre Julliard401710d1993-09-04 10:09:32 +0000645 * PALETTE_GetObject
646 */
647int PALETTE_GetObject( PALETTEOBJ * palette, int count, LPSTR buffer )
648{
649 if (count > sizeof(WORD)) count = sizeof(WORD);
650 memcpy( buffer, &palette->logpalette.palNumEntries, count );
651 return count;
652}
Alexandre Julliard5f721f81994-01-04 20:14:34 +0000653
654
655/***********************************************************************
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000656 * PALETTE_UnrealizeObject
657 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000658BOOL PALETTE_UnrealizeObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000659{
660 if (palette->mapping)
661 {
Alexandre Julliarda7116b02000-03-28 20:02:37 +0000662 HeapFree( GetProcessHeap(), 0, palette->mapping );
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000663 palette->mapping = NULL;
664 }
665 if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
666 return TRUE;
667}
668
669
670/***********************************************************************
671 * PALETTE_DeleteObject
672 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000673BOOL PALETTE_DeleteObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000674{
Alexandre Julliarda7116b02000-03-28 20:02:37 +0000675 HeapFree( GetProcessHeap(), 0, palette->mapping );
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000676 if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000677 return GDI_FreeObject( hpalette, palette );
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000678}
679
680
681/***********************************************************************
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000682 * GDISelectPalette (GDI.361)
683 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000684HPALETTE16 WINAPI GDISelectPalette16( HDC16 hdc, HPALETTE16 hpal, WORD wBkg)
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000685{
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +0000686 HPALETTE16 prev;
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000687 DC *dc;
688
Alexandre Julliard15657091999-05-23 10:25:25 +0000689 TRACE("%04x %04x\n", hdc, hpal );
Alexandre Julliard39932162000-09-16 20:57:39 +0000690
Andreas Mohrea8795b2000-09-29 01:03:57 +0000691 if (GetObjectType(hpal) != OBJ_PAL)
Alexandre Julliard39932162000-09-16 20:57:39 +0000692 {
693 WARN("invalid selected palette %04x\n",hpal);
694 return 0;
695 }
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000696 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
Alexandre Julliard2239abb2000-11-05 02:05:07 +0000697 prev = dc->hPalette;
698 dc->hPalette = hpal;
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000699 GDI_ReleaseObj( hdc );
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000700 if (!wBkg) hPrimaryPalette = hpal;
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000701 return prev;
702}
703
704
705/***********************************************************************
706 * GDIRealizePalette (GDI.362)
707 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000708UINT16 WINAPI GDIRealizePalette16( HDC16 hdc )
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000709{
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +0000710 PALETTEOBJ* palPtr;
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000711 int realized = 0;
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000712 DC* dc = DC_GetDCPtr( hdc );
713
714 if (!dc) return 0;
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +0000715
Alexandre Julliard15657091999-05-23 10:25:25 +0000716 TRACE("%04x...\n", hdc );
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000717
Alexandre Julliard2239abb2000-11-05 02:05:07 +0000718 if(dc->hPalette != hLastRealizedPalette )
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000719 {
Alexandre Julliard2239abb2000-11-05 02:05:07 +0000720 if( dc->hPalette == GetStockObject( DEFAULT_PALETTE )) {
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000721 realized = RealizeDefaultPalette16( hdc );
722 GDI_ReleaseObj( hdc );
723 return (UINT16)realized;
724 }
725
Alexandre Julliardac9c9b01996-07-28 18:50:11 +0000726
Alexandre Julliard2239abb2000-11-05 02:05:07 +0000727 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( dc->hPalette, PALETTE_MAGIC );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000728
729 if (!palPtr) {
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000730 GDI_ReleaseObj( hdc );
Alexandre Julliard2239abb2000-11-05 02:05:07 +0000731 FIXME("invalid selected palette %04x\n",dc->hPalette);
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000732 return 0;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000733 }
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000734
Patrik Stridvallb87fe2e1999-04-01 08:16:08 +0000735 realized = PALETTE_Driver->
736 pSetMapping(palPtr,0,palPtr->logpalette.palNumEntries,
Alexandre Julliard2239abb2000-11-05 02:05:07 +0000737 (dc->hPalette != hPrimaryPalette) ||
738 (dc->hPalette == GetStockObject( DEFAULT_PALETTE )));
739 hLastRealizedPalette = dc->hPalette;
740 GDI_ReleaseObj( dc->hPalette );
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000741 }
Alexandre Julliard15657091999-05-23 10:25:25 +0000742 else TRACE(" skipping (hLastRealizedPalette = %04x)\n",
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000743 hLastRealizedPalette);
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000744 GDI_ReleaseObj( hdc );
Alexandre Julliard54c27111998-03-29 19:44:57 +0000745
Alexandre Julliard15657091999-05-23 10:25:25 +0000746 TRACE(" realized %i colors.\n", realized );
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000747 return (UINT16)realized;
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000748}
749
750
751/***********************************************************************
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000752 * RealizeDefaultPalette (GDI.365)
753 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000754UINT16 WINAPI RealizeDefaultPalette16( HDC16 hdc )
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000755{
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000756 UINT16 ret = 0;
Alexandre Julliardac9c9b01996-07-28 18:50:11 +0000757 DC *dc;
758 PALETTEOBJ* palPtr;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000759
Alexandre Julliard15657091999-05-23 10:25:25 +0000760 TRACE("%04x\n", hdc );
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000761
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000762 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
763
Alexandre Julliard2239abb2000-11-05 02:05:07 +0000764 if (!(dc->flags & DC_MEMORY))
Alexandre Julliardac9c9b01996-07-28 18:50:11 +0000765 {
Alexandre Julliard4c186362000-10-29 01:22:15 +0000766 palPtr = (PALETTEOBJ*)GDI_GetObjPtr( GetStockObject(DEFAULT_PALETTE), PALETTE_MAGIC );
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000767 if (palPtr)
768 {
769 /* lookup is needed to account for SetSystemPaletteUse() stuff */
770 ret = PALETTE_Driver->pUpdateMapping(palPtr);
Alexandre Julliard4c186362000-10-29 01:22:15 +0000771 GDI_ReleaseObj( GetStockObject(DEFAULT_PALETTE) );
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000772 }
Alexandre Julliardac9c9b01996-07-28 18:50:11 +0000773 }
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000774 GDI_ReleaseObj( hdc );
775 return ret;
Alexandre Julliardac9c9b01996-07-28 18:50:11 +0000776}
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000777
778/***********************************************************************
779 * IsDCCurrentPalette (GDI.412)
780 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000781BOOL16 WINAPI IsDCCurrentPalette16(HDC16 hDC)
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000782{
Alexandre Julliard2239abb2000-11-05 02:05:07 +0000783 DC *dc = DC_GetDCPtr( hDC );
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000784 if (dc)
785 {
Alexandre Julliard2239abb2000-11-05 02:05:07 +0000786 BOOL bRet = dc->hPalette == hPrimaryPalette;
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000787 GDI_ReleaseObj( hDC );
788 return bRet;
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000789 }
790 return FALSE;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000791}
792
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000793
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000794/***********************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +0000795 * SelectPalette [GDI32.@] Selects logical palette into DC
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000796 *
797 * RETURNS
798 * Success: Previous logical palette
799 * Failure: NULL
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000800 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000801HPALETTE WINAPI SelectPalette(
802 HDC hDC, /* [in] Handle of device context */
803 HPALETTE hPal, /* [in] Handle of logical color palette */
804 BOOL bForceBackground) /* [in] Foreground/background mode */
Alexandre Julliard5f721f81994-01-04 20:14:34 +0000805{
Alexandre Julliard39932162000-09-16 20:57:39 +0000806 return pfnSelectPalette( hDC, hPal, bForceBackground );
Alexandre Julliard5f721f81994-01-04 20:14:34 +0000807}
808
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000809
Alexandre Julliard5f721f81994-01-04 20:14:34 +0000810/***********************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +0000811 * RealizePalette [GDI32.@] Maps palette entries to system palette
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000812 *
813 * RETURNS
814 * Success: Number of entries in logical palette
815 * Failure: GDI_ERROR
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000816 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000817UINT WINAPI RealizePalette(
818 HDC hDC) /* [in] Handle of device context */
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000819{
Alexandre Julliard39932162000-09-16 20:57:39 +0000820 return pfnRealizePalette( hDC );
Alexandre Julliardca22b331996-07-12 19:02:39 +0000821}
822
823
Patrik Stridvall57e57842002-02-02 18:42:11 +0000824typedef HWND (WINAPI *WindowFromDC_funcptr)( HDC );
825typedef BOOL (WINAPI *RedrawWindow_funcptr)( HWND, const RECT *, HRGN, UINT );
Alexandre Julliardea2a9a82001-02-20 00:55:17 +0000826
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000827/**********************************************************************
Dmitry Timoshkov56a19922001-07-02 01:21:26 +0000828 * UpdateColors [GDI32.@] Remaps current colors to logical palette
829 *
830 * RETURNS
831 * Success: TRUE
832 * Failure: FALSE
Alexandre Julliard5f721f81994-01-04 20:14:34 +0000833 */
Dmitry Timoshkov56a19922001-07-02 01:21:26 +0000834BOOL WINAPI UpdateColors(
835 HDC hDC) /* [in] Handle of device context */
Alexandre Julliard5f721f81994-01-04 20:14:34 +0000836{
Alexandre Julliardea2a9a82001-02-20 00:55:17 +0000837 HMODULE mod;
Alexandre Julliard99bb9f92001-07-28 00:18:02 +0000838 int size = GetDeviceCaps( hDC, SIZEPALETTE );
Patrik Stridvallb87fe2e1999-04-01 08:16:08 +0000839
Alexandre Julliard99bb9f92001-07-28 00:18:02 +0000840 if (!size) return 0;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000841
Alexandre Julliardea2a9a82001-02-20 00:55:17 +0000842 mod = GetModuleHandleA("user32.dll");
843 if (mod)
844 {
845 WindowFromDC_funcptr pWindowFromDC = (WindowFromDC_funcptr)GetProcAddress(mod,"WindowFromDC");
846 if (pWindowFromDC)
847 {
848 HWND hWnd = pWindowFromDC( hDC );
849
850 /* Docs say that we have to remap current drawable pixel by pixel
851 * but it would take forever given the speed of XGet/PutPixel.
852 */
853 if (hWnd && size)
854 {
855 RedrawWindow_funcptr pRedrawWindow = GetProcAddress( mod, "RedrawWindow" );
856 if (pRedrawWindow) pRedrawWindow( hWnd, NULL, 0, RDW_INVALIDATE );
857 }
858 }
Alexandre Julliard4e951ea2000-11-08 22:47:53 +0000859 }
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000860 return 0x666;
Alexandre Julliard5f721f81994-01-04 20:14:34 +0000861}
Alexandre Julliardac9c9b01996-07-28 18:50:11 +0000862
Alexandre Julliard21979011997-03-05 08:22:35 +0000863
864/**********************************************************************
Patrik Stridvall01d5e5b2001-07-02 19:59:40 +0000865 * UpdateColors (GDI.366)
Alexandre Julliard21979011997-03-05 08:22:35 +0000866 */
Dmitry Timoshkov56a19922001-07-02 01:21:26 +0000867INT16 WINAPI UpdateColors16( HDC16 hDC )
Alexandre Julliard21979011997-03-05 08:22:35 +0000868{
Dmitry Timoshkov56a19922001-07-02 01:21:26 +0000869 UpdateColors( hDC );
Alexandre Julliard21979011997-03-05 08:22:35 +0000870 return TRUE;
871}
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000872
Ian Schmidt8150b521998-11-22 12:54:38 +0000873
874/*********************************************************************
Patrik Stridvall17fd4e32001-06-28 18:04:41 +0000875 * SetMagicColors (GDI.606)
Ian Schmidt8150b521998-11-22 12:54:38 +0000876 */
877VOID WINAPI SetMagicColors16(HDC16 hDC, COLORREF color, UINT16 index)
878{
Alexandre Julliard15657091999-05-23 10:25:25 +0000879 FIXME("(hDC %04x, color %04x, index %04x): stub\n", hDC, (int)color, index);
Ian Schmidt8150b521998-11-22 12:54:38 +0000880
881}
Ian Schmidt31b47211999-09-22 15:12:41 +0000882
883/**********************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +0000884 * GetICMProfileA [GDI32.@]
Ian Schmidt31b47211999-09-22 15:12:41 +0000885 *
886 * Returns the filename of the specified device context's color
887 * management profile, even if color management is not enabled
888 * for that DC.
889 *
890 * RETURNS
891 * TRUE if name copied succesfully OR lpszFilename is NULL
892 * FALSE if the buffer length pointed to by lpcbName is too small
893 *
894 * NOTE
895 * The buffer length pointed to by lpcbName is ALWAYS updated to
896 * the length required regardless of other actions this function
897 * may take.
898 *
899 * FIXME
900 * How does Windows assign these? Some registry key?
901 */
902
903#define WINEICM "winefake.icm" /* easy-to-identify fake filename */
904
Patrik Stridvall2b3aa612000-12-01 23:58:28 +0000905/*********************************************************************/
906
Ian Schmidt31b47211999-09-22 15:12:41 +0000907BOOL WINAPI GetICMProfileA(HDC hDC, LPDWORD lpcbName, LPSTR lpszFilename)
908{
909 DWORD callerLen;
910
911 FIXME("(%04x, %p, %p): partial stub\n", hDC, lpcbName, lpszFilename);
912
913 callerLen = *lpcbName;
914
915 /* all 3 behaviors require the required buffer size to be set */
916 *lpcbName = strlen(WINEICM);
917
918 /* behavior 1: if lpszFilename is NULL, return size of string and no error */
919 if ((DWORD)lpszFilename == (DWORD)0x00000000)
920 return TRUE;
921
922 /* behavior 2: if buffer size too small, return size of string and error */
923 if (callerLen < strlen(WINEICM))
924 {
925 SetLastError(ERROR_INSUFFICIENT_BUFFER);
926 return FALSE;
927 }
928
929 /* behavior 3: if buffer size OK and pointer not NULL, copy and return size */
Alexandre Julliardc7e7df82000-08-14 14:41:19 +0000930 strcpy(lpszFilename, WINEICM);
Ian Schmidt31b47211999-09-22 15:12:41 +0000931 return TRUE;
932}