blob: a406a67b960f63ed36015624bd5af55beaa0849b [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 Julliard0799c1a2002-03-09 23:29:33 +00007 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * NOTES:
Alexandre Julliard23946ad1997-06-16 17:43:53 +000022 * PALETTEOBJ is documented in the Dr. Dobbs Journal May 1993.
23 * Information in the "Undocumented Windows" is incorrect.
Alexandre Julliard59730ae1996-03-24 16:20:51 +000024 */
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +000025
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000026#include <stdarg.h>
Alexandre Julliard401710d1993-09-04 10:09:32 +000027#include <stdlib.h>
Alexandre Julliard8d24ae61994-04-05 21:42:43 +000028#include <string.h>
Patrik Stridvallb87fe2e1999-04-01 08:16:08 +000029
Jeremy Whited3e22d92000-02-10 19:03:02 +000030#include "windef.h"
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000031#include "winbase.h"
Jeremy Whited3e22d92000-02-10 19:03:02 +000032#include "wingdi.h"
Michael Stefaniuc28a632a2002-11-21 21:50:04 +000033#include "wownt32.h"
Michael Veksler92ae2191999-05-02 11:39:09 +000034#include "wine/winuser16.h"
Alexandre Julliard75d86e11996-11-17 18:59:11 +000035#include "gdi.h"
Alexandre Julliard6ec42c02004-01-15 00:35:38 +000036#include "gdi_private.h"
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000037#include "wine/debug.h"
Ian Schmidt31b47211999-09-22 15:12:41 +000038#include "winerror.h"
Alexandre Julliard401710d1993-09-04 10:09:32 +000039
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000040WINE_DEFAULT_DEBUG_CHANNEL(palette);
Patrik Stridvallb4b9fae1999-04-19 14:56:29 +000041
Alexandre Julliardd8a92442002-05-31 18:43:22 +000042static INT PALETTE_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer );
43static BOOL PALETTE_UnrealizeObject( HGDIOBJ handle, void *obj );
44static BOOL PALETTE_DeleteObject( HGDIOBJ handle, void *obj );
45
46static const struct gdi_obj_funcs palette_funcs =
47{
48 NULL, /* pSelectObject */
49 PALETTE_GetObject, /* pGetObject16 */
50 PALETTE_GetObject, /* pGetObjectA */
51 PALETTE_GetObject, /* pGetObjectW */
52 PALETTE_UnrealizeObject, /* pUnrealizeObject */
53 PALETTE_DeleteObject /* pDeleteObject */
54};
55
Alexandre Julliard39932162000-09-16 20:57:39 +000056/* Pointers to USER implementation of SelectPalette/RealizePalette */
57/* they will be patched by USER on startup */
Alexandre Julliard0ccb9fe2002-10-18 04:05:49 +000058HPALETTE (WINAPI *pfnSelectPalette)(HDC hdc, HPALETTE hpal, WORD bkgnd ) = GDISelectPalette;
59UINT (WINAPI *pfnRealizePalette)(HDC hdc) = GDIRealizePalette;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000060
Alexandre Julliarda3960291999-02-26 11:11:13 +000061static UINT SystemPaletteUse = SYSPAL_STATIC; /* currently not considered */
Alexandre Julliarda2f2e011995-06-06 16:40:35 +000062
Alexandre Julliardd8a92442002-05-31 18:43:22 +000063static HPALETTE hPrimaryPalette = 0; /* used for WM_PALETTECHANGED */
64static HPALETTE hLastRealizedPalette = 0; /* UnrealizeObject() needs it */
Alexandre Julliard376e6502002-06-02 21:40:24 +000065static const DC_FUNCTIONS *pLastRealizedDC;
Alexandre Julliard18f92e71996-07-17 20:02:21 +000066
Alexandre Julliard376e6502002-06-02 21:40:24 +000067static const PALETTEENTRY sys_pal_template[NB_RESERVED_COLORS] =
68{
69 /* first 10 entries in the system palette */
70 /* red green blue flags */
71 { 0x00, 0x00, 0x00, 0 },
72 { 0x80, 0x00, 0x00, 0 },
73 { 0x00, 0x80, 0x00, 0 },
74 { 0x80, 0x80, 0x00, 0 },
75 { 0x00, 0x00, 0x80, 0 },
76 { 0x80, 0x00, 0x80, 0 },
77 { 0x00, 0x80, 0x80, 0 },
78 { 0xc0, 0xc0, 0xc0, 0 },
79 { 0xc0, 0xdc, 0xc0, 0 },
80 { 0xa6, 0xca, 0xf0, 0 },
81
82 /* ... c_min/2 dynamic colorcells */
83
84 /* ... gap (for sparse palettes) */
85
86 /* ... c_min/2 dynamic colorcells */
87
88 { 0xff, 0xfb, 0xf0, 0 },
89 { 0xa0, 0xa0, 0xa4, 0 },
90 { 0x80, 0x80, 0x80, 0 },
91 { 0xff, 0x00, 0x00, 0 },
92 { 0x00, 0xff, 0x00, 0 },
93 { 0xff, 0xff, 0x00, 0 },
94 { 0x00, 0x00, 0xff, 0 },
95 { 0xff, 0x00, 0xff, 0 },
96 { 0x00, 0xff, 0xff, 0 },
97 { 0xff, 0xff, 0xff, 0 } /* last 10 */
98};
Alexandre Julliard902da691995-11-05 14:39:02 +000099
100/***********************************************************************
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000101 * PALETTE_Init
102 *
103 * Create the system palette.
104 */
Alexandre Julliard73755972002-07-31 19:26:03 +0000105HPALETTE PALETTE_Init(void)
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000106{
Alexandre Julliard73755972002-07-31 19:26:03 +0000107 HPALETTE hpalette;
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000108 LOGPALETTE * palPtr;
109 PALETTEOBJ* palObj;
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000110
111 /* create default palette (20 system colors) */
112
Alexandre Julliard491502b1997-11-01 19:08:16 +0000113 palPtr = HeapAlloc( GetProcessHeap(), 0,
114 sizeof(LOGPALETTE) + (NB_RESERVED_COLORS-1)*sizeof(PALETTEENTRY));
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000115 if (!palPtr) return FALSE;
116
117 palPtr->palVersion = 0x300;
118 palPtr->palNumEntries = NB_RESERVED_COLORS;
Alexandre Julliard376e6502002-06-02 21:40:24 +0000119 memcpy( palPtr->palPalEntry, sys_pal_template, sizeof(sys_pal_template) );
Alexandre Julliard73755972002-07-31 19:26:03 +0000120 hpalette = CreatePalette( palPtr );
Dimitrie O. Paun9ad96362000-03-19 14:29:50 +0000121 HeapFree( GetProcessHeap(), 0, palPtr );
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000122
123 palObj = (PALETTEOBJ*) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
Juergen Schmied58b2f0a1999-01-23 12:09:30 +0000124 if (palObj)
125 {
Alexandre Julliard376e6502002-06-02 21:40:24 +0000126 if (!(palObj->mapping = HeapAlloc( GetProcessHeap(), 0, sizeof(int) * NB_RESERVED_COLORS )))
Francois Gougete76218d2001-05-09 17:31:31 +0000127 ERR("Can not create palette mapping -- out of memory!\n");
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000128 GDI_ReleaseObj( hpalette );
Juergen Schmied58b2f0a1999-01-23 12:09:30 +0000129 }
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000130 return hpalette;
131}
132
133/***********************************************************************
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000134 * PALETTE_ValidateFlags
135 */
Alexandre Julliard376e6502002-06-02 21:40:24 +0000136static void PALETTE_ValidateFlags(PALETTEENTRY* lpPalE, int size)
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000137{
138 int i = 0;
139 for( ; i<size ; i++ )
140 lpPalE[i].peFlags = PC_SYS_USED | (lpPalE[i].peFlags & 0x07);
141}
142
143
144/***********************************************************************
Jon Griffiths783a3952004-02-09 20:47:42 +0000145 * CreatePalette [GDI32.@]
146 *
147 * Creates a logical color palette.
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000148 *
149 * RETURNS
150 * Success: Handle to logical palette
151 * Failure: NULL
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000152 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000153HPALETTE WINAPI CreatePalette(
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000154 const LOGPALETTE* palette) /* [in] Pointer to logical color palette */
Alexandre Julliard401710d1993-09-04 10:09:32 +0000155{
156 PALETTEOBJ * palettePtr;
Alexandre Julliarda3960291999-02-26 11:11:13 +0000157 HPALETTE hpalette;
Juergen Schmiedc5096151998-11-22 16:16:58 +0000158 int size;
Vincent Béron9a624912002-05-31 23:06:46 +0000159
Juergen Schmiedc5096151998-11-22 16:16:58 +0000160 if (!palette) return 0;
Alexandre Julliard15657091999-05-23 10:25:25 +0000161 TRACE("entries=%i\n", palette->palNumEntries);
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000162
Juergen Schmiedc5096151998-11-22 16:16:58 +0000163 size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
164
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000165 if (!(palettePtr = GDI_AllocObject( size + sizeof(int*) +sizeof(GDIOBJHDR),
Michael Stefaniuc28a632a2002-11-21 21:50:04 +0000166 PALETTE_MAGIC, (HGDIOBJ *)&hpalette,
167 &palette_funcs ))) return 0;
Alexandre Julliard401710d1993-09-04 10:09:32 +0000168 memcpy( &palettePtr->logpalette, palette, size );
Vincent Béron9a624912002-05-31 23:06:46 +0000169 PALETTE_ValidateFlags(palettePtr->logpalette.palPalEntry,
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000170 palettePtr->logpalette.palNumEntries);
171 palettePtr->mapping = NULL;
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000172 GDI_ReleaseObj( hpalette );
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000173
Alexandre Julliard547cdc22002-11-22 22:16:53 +0000174 TRACE(" returning %p\n", hpalette);
Alexandre Julliard401710d1993-09-04 10:09:32 +0000175 return hpalette;
176}
177
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000178
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000179/***********************************************************************
Jon Griffiths783a3952004-02-09 20:47:42 +0000180 * CreateHalftonePalette [GDI32.@]
181 *
182 * Creates a halftone palette.
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000183 *
184 * RETURNS
185 * Success: Handle to logical halftone palette
186 * Failure: 0
Juergen Schmied58b2f0a1999-01-23 12:09:30 +0000187 *
Jon Griffiths783a3952004-02-09 20:47:42 +0000188 * FIXME: This simply creates the halftone palette derived from running
Aric Stewartae7fe2d2002-01-29 02:53:43 +0000189 * tests on an windows NT machine. this is assuming a color depth
190 * of greater that 256 color. On a 256 color device the halftone
Jon Griffiths783a3952004-02-09 20:47:42 +0000191 * palette will be different and this function will be incorrect
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000192 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000193HPALETTE WINAPI CreateHalftonePalette(
194 HDC hdc) /* [in] Handle to device context */
Brad Pepers0bdff361999-02-09 15:37:16 +0000195{
Aric Stewartae7fe2d2002-01-29 02:53:43 +0000196 int i;
Brad Pepers0bdff361999-02-09 15:37:16 +0000197 struct {
198 WORD Version;
199 WORD NumberOfEntries;
200 PALETTEENTRY aEntries[256];
Patrik Stridvallf87b96e2000-10-24 02:20:31 +0000201 } Palette;
Juergen Schmied58b2f0a1999-01-23 12:09:30 +0000202
Patrik Stridvallf87b96e2000-10-24 02:20:31 +0000203 Palette.Version = 0x300;
204 Palette.NumberOfEntries = 256;
Alexandre Julliarda3960291999-02-26 11:11:13 +0000205 GetSystemPaletteEntries(hdc, 0, 256, Palette.aEntries);
Juergen Schmied58b2f0a1999-01-23 12:09:30 +0000206
Aric Stewartae7fe2d2002-01-29 02:53:43 +0000207 Palette.NumberOfEntries = 20;
208
209 for (i = 0; i < Palette.NumberOfEntries; i++)
210 {
211 Palette.aEntries[i].peRed=0xff;
212 Palette.aEntries[i].peGreen=0xff;
213 Palette.aEntries[i].peBlue=0xff;
214 Palette.aEntries[i].peFlags=0x00;
215 }
216
217 Palette.aEntries[0].peRed=0x00;
218 Palette.aEntries[0].peBlue=0x00;
219 Palette.aEntries[0].peGreen=0x00;
220
221 /* the first 6 */
222 for (i=1; i <= 6; i++)
223 {
224 Palette.aEntries[i].peRed=(i%2)?0x80:0;
225 Palette.aEntries[i].peGreen=(i==2)?0x80:(i==3)?0x80:(i==6)?0x80:0;
226 Palette.aEntries[i].peBlue=(i>3)?0x80:0;
227 }
Vincent Béron9a624912002-05-31 23:06:46 +0000228
Aric Stewartae7fe2d2002-01-29 02:53:43 +0000229 for (i=7; i <= 12; i++)
230 {
231 switch(i)
232 {
233 case 7:
234 Palette.aEntries[i].peRed=0xc0;
235 Palette.aEntries[i].peBlue=0xc0;
236 Palette.aEntries[i].peGreen=0xc0;
237 break;
238 case 8:
239 Palette.aEntries[i].peRed=0xc0;
240 Palette.aEntries[i].peGreen=0xdc;
241 Palette.aEntries[i].peBlue=0xc0;
242 break;
243 case 9:
244 Palette.aEntries[i].peRed=0xa6;
245 Palette.aEntries[i].peGreen=0xca;
246 Palette.aEntries[i].peBlue=0xf0;
247 break;
Vincent Béron9a624912002-05-31 23:06:46 +0000248 case 10:
Aric Stewartae7fe2d2002-01-29 02:53:43 +0000249 Palette.aEntries[i].peRed=0xff;
250 Palette.aEntries[i].peGreen=0xfb;
251 Palette.aEntries[i].peBlue=0xf0;
252 break;
253 case 11:
254 Palette.aEntries[i].peRed=0xa0;
255 Palette.aEntries[i].peGreen=0xa0;
256 Palette.aEntries[i].peBlue=0xa4;
257 break;
258 case 12:
259 Palette.aEntries[i].peRed=0x80;
260 Palette.aEntries[i].peGreen=0x80;
261 Palette.aEntries[i].peBlue=0x80;
262 }
263 }
264
265 for (i=13; i <= 18; i++)
266 {
267 Palette.aEntries[i].peRed=(i%2)?0xff:0;
268 Palette.aEntries[i].peGreen=(i==14)?0xff:(i==15)?0xff:(i==18)?0xff:0;
269 Palette.aEntries[i].peBlue=(i>15)?0xff:0x00;
270 }
271
Alexandre Julliarda3960291999-02-26 11:11:13 +0000272 return CreatePalette((LOGPALETTE *)&Palette);
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000273}
Alexandre Julliard401710d1993-09-04 10:09:32 +0000274
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000275
Alexandre Julliard401710d1993-09-04 10:09:32 +0000276/***********************************************************************
Jon Griffiths783a3952004-02-09 20:47:42 +0000277 * GetPaletteEntries [GDI32.@]
278 *
279 * Retrieves palette entries.
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000280 *
281 * RETURNS
282 * Success: Number of entries from logical palette
283 * Failure: 0
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000284 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000285UINT WINAPI GetPaletteEntries(
286 HPALETTE hpalette, /* [in] Handle of logical palette */
287 UINT start, /* [in] First entry to receive */
288 UINT count, /* [in] Number of entries to receive */
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000289 LPPALETTEENTRY entries) /* [out] Address of array receiving entries */
Alexandre Julliard401710d1993-09-04 10:09:32 +0000290{
291 PALETTEOBJ * palPtr;
Joerg Mayer4d756402001-01-10 22:45:33 +0000292 UINT numEntries;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000293
Alexandre Julliard547cdc22002-11-22 22:16:53 +0000294 TRACE("hpal = %p, count=%i\n", hpalette, count );
Vincent Béron9a624912002-05-31 23:06:46 +0000295
Alexandre Julliard401710d1993-09-04 10:09:32 +0000296 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
297 if (!palPtr) return 0;
Vincent Béron9a624912002-05-31 23:06:46 +0000298
Aric Stewartae7fe2d2002-01-29 02:53:43 +0000299 /* NOTE: not documented but test show this to be the case */
300 if (count == 0)
Vincent Béron9a624912002-05-31 23:06:46 +0000301 {
Aric Stewartae7fe2d2002-01-29 02:53:43 +0000302 int rc = palPtr->logpalette.palNumEntries;
303 GDI_ReleaseObj( hpalette );
304 return rc;
305 }
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000306
Alexandre Julliard401710d1993-09-04 10:09:32 +0000307 numEntries = palPtr->logpalette.palNumEntries;
Alexandre Julliard401710d1993-09-04 10:09:32 +0000308 if (start+count > numEntries) count = numEntries - start;
Alexandre Julliard829fe321998-07-26 14:27:39 +0000309 if (entries)
Vincent Béron9a624912002-05-31 23:06:46 +0000310 {
311 if (start >= numEntries)
Alexandre Julliard829fe321998-07-26 14:27:39 +0000312 {
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000313 GDI_ReleaseObj( hpalette );
Alexandre Julliard829fe321998-07-26 14:27:39 +0000314 return 0;
315 }
316 memcpy( entries, &palPtr->logpalette.palPalEntry[start],
317 count * sizeof(PALETTEENTRY) );
318 for( numEntries = 0; numEntries < count ; numEntries++ )
319 if (entries[numEntries].peFlags & 0xF0)
320 entries[numEntries].peFlags = 0;
Alexandre Julliard829fe321998-07-26 14:27:39 +0000321 }
322
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000323 GDI_ReleaseObj( hpalette );
Alexandre Julliard401710d1993-09-04 10:09:32 +0000324 return count;
325}
326
327
328/***********************************************************************
Jon Griffiths783a3952004-02-09 20:47:42 +0000329 * SetPaletteEntries [GDI32.@]
330 *
331 * Sets color values for range in palette.
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000332 *
333 * RETURNS
334 * Success: Number of entries that were set
335 * Failure: 0
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000336 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000337UINT WINAPI SetPaletteEntries(
338 HPALETTE hpalette, /* [in] Handle of logical palette */
339 UINT start, /* [in] Index of first entry to set */
340 UINT count, /* [in] Number of entries to set */
Alexandre Julliard376e6502002-06-02 21:40:24 +0000341 const PALETTEENTRY *entries) /* [in] Address of array of structures */
Alexandre Julliard401710d1993-09-04 10:09:32 +0000342{
343 PALETTEOBJ * palPtr;
Joerg Mayer4d756402001-01-10 22:45:33 +0000344 UINT numEntries;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000345
Alexandre Julliard547cdc22002-11-22 22:16:53 +0000346 TRACE("hpal=%p,start=%i,count=%i\n",hpalette,start,count );
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000347
Alexandre Julliard376e6502002-06-02 21:40:24 +0000348 if (hpalette == GetStockObject(DEFAULT_PALETTE)) return 0;
Alexandre Julliard401710d1993-09-04 10:09:32 +0000349 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
350 if (!palPtr) return 0;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000351
Alexandre Julliard401710d1993-09-04 10:09:32 +0000352 numEntries = palPtr->logpalette.palNumEntries;
Vincent Béron9a624912002-05-31 23:06:46 +0000353 if (start >= numEntries)
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000354 {
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000355 GDI_ReleaseObj( hpalette );
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000356 return 0;
357 }
Alexandre Julliard401710d1993-09-04 10:09:32 +0000358 if (start+count > numEntries) count = numEntries - start;
359 memcpy( &palPtr->logpalette.palPalEntry[start], entries,
360 count * sizeof(PALETTEENTRY) );
Vincent Béron9a624912002-05-31 23:06:46 +0000361 PALETTE_ValidateFlags(palPtr->logpalette.palPalEntry,
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000362 palPtr->logpalette.palNumEntries);
Alexandre Julliard376e6502002-06-02 21:40:24 +0000363 UnrealizeObject( hpalette );
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000364 GDI_ReleaseObj( hpalette );
Alexandre Julliard401710d1993-09-04 10:09:32 +0000365 return count;
366}
367
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000368
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000369/***********************************************************************
Jon Griffiths783a3952004-02-09 20:47:42 +0000370 * ResizePalette [GDI32.@]
371 *
372 * Resizes logical palette.
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000373 *
374 * RETURNS
375 * Success: TRUE
376 * Failure: FALSE
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000377 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000378BOOL WINAPI ResizePalette(
379 HPALETTE hPal, /* [in] Handle of logical palette */
380 UINT cEntries) /* [in] Number of entries in logical palette */
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000381{
Alexandre Julliard75d86e11996-11-17 18:59:11 +0000382 PALETTEOBJ * palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
Alexandre Julliarda3960291999-02-26 11:11:13 +0000383 UINT cPrevEnt, prevVer;
Alexandre Julliard75d86e11996-11-17 18:59:11 +0000384 int prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
385 int* mapping = NULL;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000386
Alexandre Julliard547cdc22002-11-22 22:16:53 +0000387 TRACE("hpal = %p, prev = %i, new = %i\n",
388 hPal, palPtr ? palPtr->logpalette.palNumEntries : -1, cEntries );
Alexandre Julliard75d86e11996-11-17 18:59:11 +0000389 if( !palPtr ) return FALSE;
390 cPrevEnt = palPtr->logpalette.palNumEntries;
391 prevVer = palPtr->logpalette.palVersion;
392 prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) +
393 sizeof(int*) + sizeof(GDIOBJHDR);
394 size += sizeof(int*) + sizeof(GDIOBJHDR);
395 mapping = palPtr->mapping;
Vincent Béron9a624912002-05-31 23:06:46 +0000396
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000397 if (!(palPtr = GDI_ReallocObject( size, hPal, palPtr ))) return FALSE;
Alexandre Julliard75d86e11996-11-17 18:59:11 +0000398
Vincent Béron9a624912002-05-31 23:06:46 +0000399 if( mapping )
Dimitrie O. Paun9ad96362000-03-19 14:29:50 +0000400 {
Vincent Béron9a624912002-05-31 23:06:46 +0000401 int *newMap = (int*) HeapReAlloc(GetProcessHeap(), 0,
Dimitrie O. Paun9ad96362000-03-19 14:29:50 +0000402 mapping, cEntries * sizeof(int) );
Vincent Béron9a624912002-05-31 23:06:46 +0000403 if(newMap == NULL)
Dimitrie O. Paun9ad96362000-03-19 14:29:50 +0000404 {
Francois Gougete76218d2001-05-09 17:31:31 +0000405 ERR("Can not resize mapping -- out of memory!\n");
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000406 GDI_ReleaseObj( hPal );
Dimitrie O. Paun9ad96362000-03-19 14:29:50 +0000407 return FALSE;
408 }
409 palPtr->mapping = newMap;
410 }
411
Vincent Béron9a624912002-05-31 23:06:46 +0000412 if( cEntries > cPrevEnt )
Alexandre Julliard75d86e11996-11-17 18:59:11 +0000413 {
414 if( mapping )
415 memset(palPtr->mapping + cPrevEnt, 0, (cEntries - cPrevEnt)*sizeof(int));
416 memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
Vincent Béron9a624912002-05-31 23:06:46 +0000417 PALETTE_ValidateFlags((PALETTEENTRY*)((BYTE*)palPtr + prevsize),
Alexandre Julliard75d86e11996-11-17 18:59:11 +0000418 cEntries - cPrevEnt );
419 }
420 palPtr->logpalette.palNumEntries = cEntries;
421 palPtr->logpalette.palVersion = prevVer;
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000422 GDI_ReleaseObj( hPal );
Alexandre Julliard75d86e11996-11-17 18:59:11 +0000423 return TRUE;
Alexandre Julliardade697e1995-11-26 13:59:11 +0000424}
425
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000426
Alexandre Julliardade697e1995-11-26 13:59:11 +0000427/***********************************************************************
Jon Griffiths783a3952004-02-09 20:47:42 +0000428 * AnimatePalette [GDI32.@]
429 *
430 * Replaces entries in logical palette.
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000431 *
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000432 * RETURNS
433 * Success: TRUE
434 * Failure: FALSE
435 *
436 * FIXME
437 * Should use existing mapping when animating a primary palette
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000438 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000439BOOL WINAPI AnimatePalette(
440 HPALETTE hPal, /* [in] Handle to logical palette */
441 UINT StartIndex, /* [in] First entry in palette */
442 UINT NumEntries, /* [in] Count of entries in palette */
François Gouget241c7301998-10-28 10:47:09 +0000443 const PALETTEENTRY* PaletteColors) /* [in] Pointer to first replacement */
Alexandre Julliardade697e1995-11-26 13:59:11 +0000444{
Alexandre Julliard547cdc22002-11-22 22:16:53 +0000445 TRACE("%p (%i - %i)\n", hPal, StartIndex,StartIndex+NumEntries);
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000446
Alexandre Julliard4c186362000-10-29 01:22:15 +0000447 if( hPal != GetStockObject(DEFAULT_PALETTE) )
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000448 {
Alexandre Julliard376e6502002-06-02 21:40:24 +0000449 if (!SetPaletteEntries( hPal, StartIndex, NumEntries, PaletteColors )) return FALSE;
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000450
Alexandre Julliard376e6502002-06-02 21:40:24 +0000451 if (pLastRealizedDC && pLastRealizedDC->pRealizePalette)
452 pLastRealizedDC->pRealizePalette( NULL, hPal, hPal == hPrimaryPalette );
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000453 }
Alexandre Julliard376e6502002-06-02 21:40:24 +0000454 return TRUE;
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000455}
Alexandre Julliard401710d1993-09-04 10:09:32 +0000456
Alexandre Julliarda2f2e011995-06-06 16:40:35 +0000457
458/***********************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +0000459 * SetSystemPaletteUse [GDI32.@]
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000460 *
461 * RETURNS
462 * Success: Previous system palette
463 * Failure: SYSPAL_ERROR
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000464 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000465UINT WINAPI SetSystemPaletteUse(
466 HDC hdc, /* [in] Handle of device context */
467 UINT use) /* [in] Palette-usage flag */
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000468{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000469 UINT old = SystemPaletteUse;
Andrew John Hughes6f929652003-02-01 00:36:38 +0000470
471 /* Device doesn't support colour palettes */
472 if (!(GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)) {
473 return SYSPAL_ERROR;
474 }
475
476 switch (use) {
477 case SYSPAL_NOSTATIC:
478 case SYSPAL_NOSTATIC256: /* WINVER >= 0x0500 */
479 case SYSPAL_STATIC:
480 SystemPaletteUse = use;
481 return old;
482 default:
483 return SYSPAL_ERROR;
484 }
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000485}
486
487
488/***********************************************************************
Jon Griffiths783a3952004-02-09 20:47:42 +0000489 * GetSystemPaletteUse [GDI32.@]
490 *
491 * Gets state of system palette.
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000492 *
493 * RETURNS
494 * Current state of system palette
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000495 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000496UINT WINAPI GetSystemPaletteUse(
497 HDC hdc) /* [in] Handle of device context */
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000498{
499 return SystemPaletteUse;
500}
501
502
503/***********************************************************************
Jon Griffiths783a3952004-02-09 20:47:42 +0000504 * GetSystemPaletteEntries [GDI32.@]
505 *
506 * Gets range of palette entries.
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000507 *
508 * RETURNS
509 * Success: Number of entries retrieved from palette
510 * Failure: 0
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000511 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000512UINT WINAPI GetSystemPaletteEntries(
513 HDC hdc, /* [in] Handle of device context */
514 UINT start, /* [in] Index of first entry to be retrieved */
515 UINT count, /* [in] Number of entries to be retrieved */
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000516 LPPALETTEENTRY entries) /* [out] Array receiving system-palette entries */
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000517{
Alexandre Julliard376e6502002-06-02 21:40:24 +0000518 UINT ret = 0;
519 DC *dc;
Alexandre Julliardac9c9b01996-07-28 18:50:11 +0000520
Alexandre Julliard547cdc22002-11-22 22:16:53 +0000521 TRACE("hdc=%p,start=%i,count=%i\n", hdc,start,count);
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000522
Alexandre Julliard376e6502002-06-02 21:40:24 +0000523 if ((dc = DC_GetDCPtr( hdc )))
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000524 {
Alexandre Julliard376e6502002-06-02 21:40:24 +0000525 if (dc->funcs->pGetSystemPaletteEntries)
526 ret = dc->funcs->pGetSystemPaletteEntries( dc->physDev, start, count, entries );
527 GDI_ReleaseObj( hdc );
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000528 }
Alexandre Julliard376e6502002-06-02 21:40:24 +0000529 return ret;
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000530}
531
532
533/***********************************************************************
Jon Griffiths783a3952004-02-09 20:47:42 +0000534 * GetNearestPaletteIndex [GDI32.@]
535 *
536 * Gets palette index for color.
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000537 *
538 * NOTES
539 * Should index be initialized to CLR_INVALID instead of 0?
540 *
541 * RETURNS
542 * Success: Index of entry in logical palette
543 * Failure: CLR_INVALID
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000544 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000545UINT WINAPI GetNearestPaletteIndex(
546 HPALETTE hpalette, /* [in] Handle of logical color palette */
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000547 COLORREF color) /* [in] Color to be matched */
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000548{
549 PALETTEOBJ* palObj = (PALETTEOBJ*)GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
Alexandre Julliarda3960291999-02-26 11:11:13 +0000550 UINT index = 0;
Alexandre Julliardac9c9b01996-07-28 18:50:11 +0000551
552 if( palObj )
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000553 {
Alexandre Julliard376e6502002-06-02 21:40:24 +0000554 int i, diff = 0x7fffffff;
555 int r,g,b;
556 PALETTEENTRY* entry = palObj->logpalette.palPalEntry;
Alexandre Julliardac9c9b01996-07-28 18:50:11 +0000557
Alexandre Julliard376e6502002-06-02 21:40:24 +0000558 for( i = 0; i < palObj->logpalette.palNumEntries && diff ; i++, entry++)
559 {
560 if (!(entry->peFlags & PC_SYS_USED)) continue;
561
562 r = entry->peRed - GetRValue(color);
563 g = entry->peGreen - GetGValue(color);
564 b = entry->peBlue - GetBValue(color);
565
566 r = r*r + g*g + b*b;
567
568 if( r < diff ) { index = i; diff = r; }
569 }
570 GDI_ReleaseObj( hpalette );
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000571 }
Alexandre Julliard547cdc22002-11-22 22:16:53 +0000572 TRACE("(%p,%06lx): returning %d\n", hpalette, color, index );
Alexandre Julliard401710d1993-09-04 10:09:32 +0000573 return index;
574}
575
576
577/***********************************************************************
Jon Griffiths783a3952004-02-09 20:47:42 +0000578 * GetNearestColor [GDI32.@]
579 *
580 * Gets a system color to match.
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000581 *
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000582 * RETURNS
583 * Success: Color from system palette that corresponds to given color
584 * Failure: CLR_INVALID
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000585 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000586COLORREF WINAPI GetNearestColor(
587 HDC hdc, /* [in] Handle of device context */
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000588 COLORREF color) /* [in] Color to be matched */
Alexandre Julliard902da691995-11-05 14:39:02 +0000589{
Alexandre Julliard376e6502002-06-02 21:40:24 +0000590 unsigned char spec_type;
591 COLORREF nearest;
Alexandre Julliardac9c9b01996-07-28 18:50:11 +0000592 DC *dc;
Alexandre Julliard902da691995-11-05 14:39:02 +0000593
Alexandre Julliard376e6502002-06-02 21:40:24 +0000594 if (!(dc = DC_GetDCPtr( hdc ))) return CLR_INVALID;
595
596 if (dc->funcs->pGetNearestColor)
597 {
598 nearest = dc->funcs->pGetNearestColor( dc->physDev, color );
599 GDI_ReleaseObj( hdc );
600 return nearest;
601 }
602
603 if (!(GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE))
604 {
605 GDI_ReleaseObj( hdc );
Dave Hawkes314b9fd2002-02-05 19:44:07 +0000606 return color;
607 }
Alexandre Julliardac9c9b01996-07-28 18:50:11 +0000608
Alexandre Julliard376e6502002-06-02 21:40:24 +0000609 spec_type = color >> 24;
610 if (spec_type == 1 || spec_type == 2)
611 {
612 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
613
614 UINT index;
615 PALETTEENTRY entry;
616 HPALETTE hpal = dc->hPalette ? dc->hPalette : GetStockObject( DEFAULT_PALETTE );
617
618 if (spec_type == 2) /* PALETTERGB */
619 index = GetNearestPaletteIndex( hpal, color );
620 else /* PALETTEINDEX */
621 index = LOWORD(color);
622
623 if (!GetPaletteEntries( hpal, index, 1, &entry ))
624 {
625 WARN("RGB(%lx) : idx %d is out of bounds, assuming NULL\n", color, index );
626 if (!GetPaletteEntries( hpal, 0, 1, &entry ))
627 {
628 GDI_ReleaseObj( hdc );
629 return CLR_INVALID;
630 }
631 }
632 color = RGB( entry.peRed, entry.peGreen, entry.peBlue );
Alexandre Julliardac9c9b01996-07-28 18:50:11 +0000633 }
Alexandre Julliard376e6502002-06-02 21:40:24 +0000634 nearest = color & 0x00ffffff;
635 GDI_ReleaseObj( hdc );
Alexandre Julliardac9c9b01996-07-28 18:50:11 +0000636
Alexandre Julliard15657091999-05-23 10:25:25 +0000637 TRACE("(%06lx): returning %06lx\n", color, nearest );
Alexandre Julliard902da691995-11-05 14:39:02 +0000638 return nearest;
639}
640
641
642/***********************************************************************
Alexandre Julliard401710d1993-09-04 10:09:32 +0000643 * PALETTE_GetObject
644 */
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000645static INT PALETTE_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
Alexandre Julliard401710d1993-09-04 10:09:32 +0000646{
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000647 PALETTEOBJ *palette = obj;
648
Mike McCormack369116d2003-07-26 20:39:46 +0000649 if( !buffer )
650 return sizeof(WORD);
651
Alexandre Julliard401710d1993-09-04 10:09:32 +0000652 if (count > sizeof(WORD)) count = sizeof(WORD);
653 memcpy( buffer, &palette->logpalette.palNumEntries, count );
654 return count;
655}
Alexandre Julliard5f721f81994-01-04 20:14:34 +0000656
657
658/***********************************************************************
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000659 * PALETTE_UnrealizeObject
660 */
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000661static BOOL PALETTE_UnrealizeObject( HGDIOBJ handle, void *obj )
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000662{
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000663 PALETTEOBJ *palette = obj;
664
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000665 if (palette->mapping)
666 {
Alexandre Julliarda7116b02000-03-28 20:02:37 +0000667 HeapFree( GetProcessHeap(), 0, palette->mapping );
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000668 palette->mapping = NULL;
669 }
Alexandre Julliard376e6502002-06-02 21:40:24 +0000670 if (hLastRealizedPalette == handle)
671 {
672 hLastRealizedPalette = 0;
673 pLastRealizedDC = NULL;
674 }
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000675 return TRUE;
676}
677
678
679/***********************************************************************
680 * PALETTE_DeleteObject
681 */
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000682static BOOL PALETTE_DeleteObject( HGDIOBJ handle, void *obj )
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000683{
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000684 PALETTEOBJ *palette = obj;
685
Alexandre Julliarda7116b02000-03-28 20:02:37 +0000686 HeapFree( GetProcessHeap(), 0, palette->mapping );
Alexandre Julliard376e6502002-06-02 21:40:24 +0000687 if (hLastRealizedPalette == handle)
688 {
689 hLastRealizedPalette = 0;
690 pLastRealizedDC = NULL;
691 }
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000692 return GDI_FreeObject( handle, obj );
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000693}
694
695
696/***********************************************************************
Alexandre Julliarda0b32d72002-08-29 01:55:16 +0000697 * GDISelectPalette (Not a Windows API)
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000698 */
Alexandre Julliarda0b32d72002-08-29 01:55:16 +0000699HPALETTE WINAPI GDISelectPalette( HDC hdc, HPALETTE hpal, WORD wBkg)
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000700{
Dave Belanger549e2472003-08-30 00:15:12 +0000701 HPALETTE ret;
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000702 DC *dc;
703
Alexandre Julliard547cdc22002-11-22 22:16:53 +0000704 TRACE("%p %p\n", hdc, hpal );
Alexandre Julliard39932162000-09-16 20:57:39 +0000705
Andreas Mohrea8795b2000-09-29 01:03:57 +0000706 if (GetObjectType(hpal) != OBJ_PAL)
Alexandre Julliard39932162000-09-16 20:57:39 +0000707 {
Alexandre Julliard547cdc22002-11-22 22:16:53 +0000708 WARN("invalid selected palette %p\n",hpal);
Alexandre Julliard39932162000-09-16 20:57:39 +0000709 return 0;
710 }
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000711 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
Dave Belanger549e2472003-08-30 00:15:12 +0000712 ret = dc->hPalette;
713 if (dc->funcs->pSelectPalette) hpal = dc->funcs->pSelectPalette( dc->physDev, hpal, FALSE );
714 if (hpal)
715 {
716 dc->hPalette = hpal;
717 if (!wBkg) hPrimaryPalette = hpal;
718 }
719 else ret = 0;
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000720 GDI_ReleaseObj( hdc );
Dave Belanger549e2472003-08-30 00:15:12 +0000721 return ret;
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000722}
723
724
725/***********************************************************************
Alexandre Julliarda0b32d72002-08-29 01:55:16 +0000726 * GDIRealizePalette (Not a Windows API)
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000727 */
Alexandre Julliarda0b32d72002-08-29 01:55:16 +0000728UINT WINAPI GDIRealizePalette( HDC hdc )
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000729{
Alexandre Julliard376e6502002-06-02 21:40:24 +0000730 UINT realized = 0;
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000731 DC* dc = DC_GetDCPtr( hdc );
732
733 if (!dc) return 0;
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +0000734
Alexandre Julliard547cdc22002-11-22 22:16:53 +0000735 TRACE("%p...\n", hdc );
Vincent Béron9a624912002-05-31 23:06:46 +0000736
Alexandre Julliard376e6502002-06-02 21:40:24 +0000737 if( dc->hPalette == GetStockObject( DEFAULT_PALETTE ))
738 {
Alexandre Julliarda0b32d72002-08-29 01:55:16 +0000739 if (dc->funcs->pRealizeDefaultPalette)
740 realized = dc->funcs->pRealizeDefaultPalette( dc->physDev );
Alexandre Julliard376e6502002-06-02 21:40:24 +0000741 }
Alexandre Julliarda0b32d72002-08-29 01:55:16 +0000742 else if(dc->hPalette != hLastRealizedPalette )
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000743 {
Alexandre Julliard376e6502002-06-02 21:40:24 +0000744 if (dc->funcs->pRealizePalette)
745 realized = dc->funcs->pRealizePalette( dc->physDev, dc->hPalette,
746 (dc->hPalette == hPrimaryPalette) );
747 hLastRealizedPalette = dc->hPalette;
748 pLastRealizedDC = dc->funcs;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000749 }
Alexandre Julliard547cdc22002-11-22 22:16:53 +0000750 else TRACE(" skipping (hLastRealizedPalette = %p)\n", hLastRealizedPalette);
Alexandre Julliard54c27111998-03-29 19:44:57 +0000751
Alexandre Julliarda0b32d72002-08-29 01:55:16 +0000752 GDI_ReleaseObj( hdc );
Alexandre Julliard15657091999-05-23 10:25:25 +0000753 TRACE(" realized %i colors.\n", realized );
Alexandre Julliarda0b32d72002-08-29 01:55:16 +0000754 return realized;
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000755}
756
757
758/***********************************************************************
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000759 * RealizeDefaultPalette (GDI.365)
760 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000761UINT16 WINAPI RealizeDefaultPalette16( HDC16 hdc )
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000762{
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000763 UINT16 ret = 0;
Alexandre Julliardac9c9b01996-07-28 18:50:11 +0000764 DC *dc;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000765
Alexandre Julliard15657091999-05-23 10:25:25 +0000766 TRACE("%04x\n", hdc );
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000767
Michael Stefaniuc28a632a2002-11-21 21:50:04 +0000768 if (!(dc = DC_GetDCPtr( HDC_32(hdc) ))) return 0;
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000769
Alexandre Julliard376e6502002-06-02 21:40:24 +0000770 if (dc->funcs->pRealizeDefaultPalette) ret = dc->funcs->pRealizeDefaultPalette( dc->physDev );
Michael Stefaniuc28a632a2002-11-21 21:50:04 +0000771 GDI_ReleaseObj( HDC_32(hdc) );
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000772 return ret;
Alexandre Julliardac9c9b01996-07-28 18:50:11 +0000773}
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000774
775/***********************************************************************
776 * IsDCCurrentPalette (GDI.412)
777 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000778BOOL16 WINAPI IsDCCurrentPalette16(HDC16 hDC)
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000779{
Michael Stefaniuc28a632a2002-11-21 21:50:04 +0000780 DC *dc = DC_GetDCPtr( HDC_32(hDC) );
Vincent Béron9a624912002-05-31 23:06:46 +0000781 if (dc)
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000782 {
Alexandre Julliard2239abb2000-11-05 02:05:07 +0000783 BOOL bRet = dc->hPalette == hPrimaryPalette;
Michael Stefaniuc28a632a2002-11-21 21:50:04 +0000784 GDI_ReleaseObj( HDC_32(hDC) );
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000785 return bRet;
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000786 }
787 return FALSE;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000788}
789
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000790
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000791/***********************************************************************
Jon Griffiths783a3952004-02-09 20:47:42 +0000792 * SelectPalette [GDI32.@]
793 *
794 * Selects logical palette into DC.
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000795 *
796 * RETURNS
797 * Success: Previous logical palette
798 * Failure: NULL
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000799 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000800HPALETTE WINAPI SelectPalette(
801 HDC hDC, /* [in] Handle of device context */
802 HPALETTE hPal, /* [in] Handle of logical color palette */
803 BOOL bForceBackground) /* [in] Foreground/background mode */
Alexandre Julliard5f721f81994-01-04 20:14:34 +0000804{
Alexandre Julliard39932162000-09-16 20:57:39 +0000805 return pfnSelectPalette( hDC, hPal, bForceBackground );
Alexandre Julliard5f721f81994-01-04 20:14:34 +0000806}
807
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000808
Alexandre Julliard5f721f81994-01-04 20:14:34 +0000809/***********************************************************************
Jon Griffiths783a3952004-02-09 20:47:42 +0000810 * RealizePalette [GDI32.@]
811 *
812 * Maps palette entries to system palette.
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000813 *
814 * RETURNS
815 * Success: Number of entries in logical palette
816 * Failure: GDI_ERROR
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000817 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000818UINT WINAPI RealizePalette(
819 HDC hDC) /* [in] Handle of device context */
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000820{
Alexandre Julliard39932162000-09-16 20:57:39 +0000821 return pfnRealizePalette( hDC );
Alexandre Julliardca22b331996-07-12 19:02:39 +0000822}
823
824
Patrik Stridvall57e57842002-02-02 18:42:11 +0000825typedef HWND (WINAPI *WindowFromDC_funcptr)( HDC );
826typedef BOOL (WINAPI *RedrawWindow_funcptr)( HWND, const RECT *, HRGN, UINT );
Alexandre Julliardea2a9a82001-02-20 00:55:17 +0000827
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000828/**********************************************************************
Jon Griffiths783a3952004-02-09 20:47:42 +0000829 * UpdateColors [GDI32.@]
830 *
831 * Remaps current colors to logical palette.
Dmitry Timoshkov56a19922001-07-02 01:21:26 +0000832 *
833 * RETURNS
834 * Success: TRUE
835 * Failure: FALSE
Alexandre Julliard5f721f81994-01-04 20:14:34 +0000836 */
Dmitry Timoshkov56a19922001-07-02 01:21:26 +0000837BOOL WINAPI UpdateColors(
838 HDC hDC) /* [in] Handle of device context */
Alexandre Julliard5f721f81994-01-04 20:14:34 +0000839{
Alexandre Julliardea2a9a82001-02-20 00:55:17 +0000840 HMODULE mod;
Alexandre Julliard99bb9f92001-07-28 00:18:02 +0000841 int size = GetDeviceCaps( hDC, SIZEPALETTE );
Patrik Stridvallb87fe2e1999-04-01 08:16:08 +0000842
Alexandre Julliard99bb9f92001-07-28 00:18:02 +0000843 if (!size) return 0;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000844
Alexandre Julliardea2a9a82001-02-20 00:55:17 +0000845 mod = GetModuleHandleA("user32.dll");
846 if (mod)
847 {
848 WindowFromDC_funcptr pWindowFromDC = (WindowFromDC_funcptr)GetProcAddress(mod,"WindowFromDC");
849 if (pWindowFromDC)
850 {
851 HWND hWnd = pWindowFromDC( hDC );
852
853 /* Docs say that we have to remap current drawable pixel by pixel
854 * but it would take forever given the speed of XGet/PutPixel.
855 */
856 if (hWnd && size)
857 {
858 RedrawWindow_funcptr pRedrawWindow = GetProcAddress( mod, "RedrawWindow" );
859 if (pRedrawWindow) pRedrawWindow( hWnd, NULL, 0, RDW_INVALIDATE );
860 }
861 }
Alexandre Julliard4e951ea2000-11-08 22:47:53 +0000862 }
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000863 return 0x666;
Alexandre Julliard5f721f81994-01-04 20:14:34 +0000864}
Alexandre Julliardac9c9b01996-07-28 18:50:11 +0000865
Alexandre Julliard21979011997-03-05 08:22:35 +0000866
Ian Schmidt8150b521998-11-22 12:54:38 +0000867/*********************************************************************
Patrik Stridvall17fd4e32001-06-28 18:04:41 +0000868 * SetMagicColors (GDI.606)
Ian Schmidt8150b521998-11-22 12:54:38 +0000869 */
870VOID WINAPI SetMagicColors16(HDC16 hDC, COLORREF color, UINT16 index)
871{
Alexandre Julliard15657091999-05-23 10:25:25 +0000872 FIXME("(hDC %04x, color %04x, index %04x): stub\n", hDC, (int)color, index);
Ian Schmidt8150b521998-11-22 12:54:38 +0000873
874}
Ian Schmidt31b47211999-09-22 15:12:41 +0000875
876/**********************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +0000877 * GetICMProfileA [GDI32.@]
Ian Schmidt31b47211999-09-22 15:12:41 +0000878 *
879 * Returns the filename of the specified device context's color
880 * management profile, even if color management is not enabled
881 * for that DC.
882 *
883 * RETURNS
Francois Gouget14a5d162003-07-02 04:37:26 +0000884 * TRUE if name copied successfully OR lpszFilename is NULL
Ian Schmidt31b47211999-09-22 15:12:41 +0000885 * FALSE if the buffer length pointed to by lpcbName is too small
886 *
887 * NOTE
888 * The buffer length pointed to by lpcbName is ALWAYS updated to
889 * the length required regardless of other actions this function
890 * may take.
891 *
892 * FIXME
893 * How does Windows assign these? Some registry key?
894 */
895
896#define WINEICM "winefake.icm" /* easy-to-identify fake filename */
897
Patrik Stridvall2b3aa612000-12-01 23:58:28 +0000898/*********************************************************************/
899
Ian Schmidt31b47211999-09-22 15:12:41 +0000900BOOL WINAPI GetICMProfileA(HDC hDC, LPDWORD lpcbName, LPSTR lpszFilename)
901{
902 DWORD callerLen;
903
Alexandre Julliard547cdc22002-11-22 22:16:53 +0000904 FIXME("(%p, %p, %p): partial stub\n", hDC, lpcbName, lpszFilename);
Ian Schmidt31b47211999-09-22 15:12:41 +0000905
906 callerLen = *lpcbName;
907
908 /* all 3 behaviors require the required buffer size to be set */
909 *lpcbName = strlen(WINEICM);
910
911 /* behavior 1: if lpszFilename is NULL, return size of string and no error */
912 if ((DWORD)lpszFilename == (DWORD)0x00000000)
913 return TRUE;
Vincent Béron9a624912002-05-31 23:06:46 +0000914
Ian Schmidt31b47211999-09-22 15:12:41 +0000915 /* behavior 2: if buffer size too small, return size of string and error */
Vincent Béron9a624912002-05-31 23:06:46 +0000916 if (callerLen < strlen(WINEICM))
Ian Schmidt31b47211999-09-22 15:12:41 +0000917 {
918 SetLastError(ERROR_INSUFFICIENT_BUFFER);
919 return FALSE;
920 }
921
922 /* behavior 3: if buffer size OK and pointer not NULL, copy and return size */
Alexandre Julliardc7e7df82000-08-14 14:41:19 +0000923 strcpy(lpszFilename, WINEICM);
Ian Schmidt31b47211999-09-22 15:12:41 +0000924 return TRUE;
925}
Rein Klazes8677fbf2004-02-20 01:08:16 +0000926
927/**********************************************************************
928 * SetICMProfileA [GDI32.@]
929 *
930 */
931BOOL WINAPI SetICMProfileA(HDC hDC, LPSTR lpszFilename)
932{
933 FIXME("hDC %p filename '%s': stub!\n", hDC, debugstr_a(lpszFilename));
934 return TRUE; /* success */
935}