vbscript: Added InStr tests.
[wine.git] / dlls / gdi32 / palette.c
blob338d8601ad6fb4592c6ce51dabbadb698dd9c04f
1 /*
2 * GDI palette objects
4 * Copyright 1993,1994 Alexandre Julliard
5 * Copyright 1996 Alex Korobka
7 * 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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * NOTES:
22 * PALETTEOBJ is documented in the Dr. Dobbs Journal May 1993.
23 * Information in the "Undocumented Windows" is incorrect.
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winerror.h"
33 #include "wingdi.h"
34 #include "winuser.h"
36 #include "gdi_private.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(palette);
41 typedef BOOL (*unrealize_function)(HPALETTE);
43 typedef struct tagPALETTEOBJ
45 GDIOBJHDR header;
46 unrealize_function unrealize;
47 WORD version; /* palette version */
48 WORD count; /* count of palette entries */
49 PALETTEENTRY *entries;
50 } PALETTEOBJ;
52 static INT PALETTE_GetObject( HGDIOBJ handle, INT count, LPVOID buffer );
53 static BOOL PALETTE_UnrealizeObject( HGDIOBJ handle );
54 static BOOL PALETTE_DeleteObject( HGDIOBJ handle );
56 static const struct gdi_obj_funcs palette_funcs =
58 NULL, /* pSelectObject */
59 PALETTE_GetObject, /* pGetObjectA */
60 PALETTE_GetObject, /* pGetObjectW */
61 PALETTE_UnrealizeObject, /* pUnrealizeObject */
62 PALETTE_DeleteObject /* pDeleteObject */
65 /* Pointers to USER implementation of SelectPalette/RealizePalette */
66 /* they will be patched by USER on startup */
67 HPALETTE (WINAPI *pfnSelectPalette)(HDC hdc, HPALETTE hpal, WORD bkgnd ) = GDISelectPalette;
68 UINT (WINAPI *pfnRealizePalette)(HDC hdc) = GDIRealizePalette;
70 static UINT SystemPaletteUse = SYSPAL_STATIC; /* currently not considered */
72 static HPALETTE hPrimaryPalette = 0; /* used for WM_PALETTECHANGED */
73 static HPALETTE hLastRealizedPalette = 0; /* UnrealizeObject() needs it */
76 /***********************************************************************
77 * PALETTE_Init
79 * Create the system palette.
81 HPALETTE PALETTE_Init(void)
83 const RGBQUAD *entries = get_default_color_table( 8 );
84 char buffer[FIELD_OFFSET( LOGPALETTE, palPalEntry[20] )];
85 LOGPALETTE *palPtr = (LOGPALETTE *)buffer;
86 int i;
88 /* create default palette (20 system colors) */
90 palPtr->palVersion = 0x300;
91 palPtr->palNumEntries = 20;
92 for (i = 0; i < 10; i++)
94 palPtr->palPalEntry[i].peRed = entries[i].rgbRed;
95 palPtr->palPalEntry[i].peGreen = entries[i].rgbGreen;
96 palPtr->palPalEntry[i].peBlue = entries[i].rgbBlue;
97 palPtr->palPalEntry[i].peFlags = 0;
99 for (i = 10; i < 20; i++)
101 palPtr->palPalEntry[i].peRed = entries[236 + i].rgbRed;
102 palPtr->palPalEntry[i].peGreen = entries[236 + i].rgbGreen;
103 palPtr->palPalEntry[i].peBlue = entries[236 + i].rgbBlue;
104 palPtr->palPalEntry[i].peFlags = 0;
106 return CreatePalette( palPtr );
110 /***********************************************************************
111 * CreatePalette [GDI32.@]
113 * Creates a logical color palette.
115 * RETURNS
116 * Success: Handle to logical palette
117 * Failure: NULL
119 HPALETTE WINAPI CreatePalette(
120 const LOGPALETTE* palette) /* [in] Pointer to logical color palette */
122 PALETTEOBJ * palettePtr;
123 HPALETTE hpalette;
124 int size;
126 if (!palette) return 0;
127 TRACE("entries=%i\n", palette->palNumEntries);
129 if (!(palettePtr = HeapAlloc( GetProcessHeap(), 0, sizeof(*palettePtr) ))) return 0;
130 palettePtr->unrealize = NULL;
131 palettePtr->version = palette->palVersion;
132 palettePtr->count = palette->palNumEntries;
133 size = palettePtr->count * sizeof(*palettePtr->entries);
134 if (!(palettePtr->entries = HeapAlloc( GetProcessHeap(), 0, size )))
136 HeapFree( GetProcessHeap(), 0, palettePtr );
137 return 0;
139 memcpy( palettePtr->entries, palette->palPalEntry, size );
140 if (!(hpalette = alloc_gdi_handle( &palettePtr->header, OBJ_PAL, &palette_funcs )))
142 HeapFree( GetProcessHeap(), 0, palettePtr->entries );
143 HeapFree( GetProcessHeap(), 0, palettePtr );
145 TRACE(" returning %p\n", hpalette);
146 return hpalette;
150 /***********************************************************************
151 * CreateHalftonePalette [GDI32.@]
153 * Creates a halftone palette.
155 * RETURNS
156 * Success: Handle to logical halftone palette
157 * Failure: 0
159 * FIXME: This simply creates the halftone palette derived from running
160 * tests on a windows NT machine. This is assuming a color depth
161 * of greater that 256 color. On a 256 color device the halftone
162 * palette will be different and this function will be incorrect
164 HPALETTE WINAPI CreateHalftonePalette(
165 HDC hdc) /* [in] Handle to device context */
167 const RGBQUAD *entries = get_default_color_table( 8 );
168 char buffer[FIELD_OFFSET( LOGPALETTE, palPalEntry[256] )];
169 LOGPALETTE *pal = (LOGPALETTE *)buffer;
170 int i;
172 pal->palVersion = 0x300;
173 pal->palNumEntries = 256;
174 for (i = 0; i < 256; i++)
176 pal->palPalEntry[i].peRed = entries[i].rgbRed;
177 pal->palPalEntry[i].peGreen = entries[i].rgbGreen;
178 pal->palPalEntry[i].peBlue = entries[i].rgbBlue;
179 pal->palPalEntry[i].peFlags = 0;
181 return CreatePalette( pal );
185 /***********************************************************************
186 * GetPaletteEntries [GDI32.@]
188 * Retrieves palette entries.
190 * RETURNS
191 * Success: Number of entries from logical palette
192 * Failure: 0
194 UINT WINAPI GetPaletteEntries(
195 HPALETTE hpalette, /* [in] Handle of logical palette */
196 UINT start, /* [in] First entry to receive */
197 UINT count, /* [in] Number of entries to receive */
198 LPPALETTEENTRY entries) /* [out] Address of array receiving entries */
200 PALETTEOBJ * palPtr;
201 UINT numEntries;
203 TRACE("hpal = %p, count=%i\n", hpalette, count );
205 palPtr = GDI_GetObjPtr( hpalette, OBJ_PAL );
206 if (!palPtr) return 0;
208 /* NOTE: not documented but test show this to be the case */
209 if (count == 0)
211 count = palPtr->count;
213 else
215 numEntries = palPtr->count;
216 if (start+count > numEntries) count = numEntries - start;
217 if (entries)
219 if (start >= numEntries) count = 0;
220 else memcpy( entries, &palPtr->entries[start], count * sizeof(PALETTEENTRY) );
224 GDI_ReleaseObj( hpalette );
225 return count;
229 /***********************************************************************
230 * SetPaletteEntries [GDI32.@]
232 * Sets color values for range in palette.
234 * RETURNS
235 * Success: Number of entries that were set
236 * Failure: 0
238 UINT WINAPI SetPaletteEntries(
239 HPALETTE hpalette, /* [in] Handle of logical palette */
240 UINT start, /* [in] Index of first entry to set */
241 UINT count, /* [in] Number of entries to set */
242 const PALETTEENTRY *entries) /* [in] Address of array of structures */
244 PALETTEOBJ * palPtr;
245 UINT numEntries;
247 TRACE("hpal=%p,start=%i,count=%i\n",hpalette,start,count );
249 if (hpalette == GetStockObject(DEFAULT_PALETTE)) return 0;
250 palPtr = GDI_GetObjPtr( hpalette, OBJ_PAL );
251 if (!palPtr) return 0;
253 numEntries = palPtr->count;
254 if (start >= numEntries)
256 GDI_ReleaseObj( hpalette );
257 return 0;
259 if (start+count > numEntries) count = numEntries - start;
260 memcpy( &palPtr->entries[start], entries, count * sizeof(PALETTEENTRY) );
261 GDI_ReleaseObj( hpalette );
262 UnrealizeObject( hpalette );
263 return count;
267 /***********************************************************************
268 * ResizePalette [GDI32.@]
270 * Resizes logical palette.
272 * RETURNS
273 * Success: TRUE
274 * Failure: FALSE
276 BOOL WINAPI ResizePalette(
277 HPALETTE hPal, /* [in] Handle of logical palette */
278 UINT cEntries) /* [in] Number of entries in logical palette */
280 PALETTEOBJ * palPtr = GDI_GetObjPtr( hPal, OBJ_PAL );
281 PALETTEENTRY *entries;
283 if( !palPtr ) return FALSE;
284 TRACE("hpal = %p, prev = %i, new = %i\n", hPal, palPtr->count, cEntries );
286 if (!(entries = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
287 palPtr->entries, cEntries * sizeof(*palPtr->entries) )))
289 GDI_ReleaseObj( hPal );
290 return FALSE;
292 palPtr->entries = entries;
293 palPtr->count = cEntries;
295 GDI_ReleaseObj( hPal );
296 PALETTE_UnrealizeObject( hPal );
297 return TRUE;
301 /***********************************************************************
302 * AnimatePalette [GDI32.@]
304 * Replaces entries in logical palette.
306 * RETURNS
307 * Success: TRUE
308 * Failure: FALSE
310 * FIXME
311 * Should use existing mapping when animating a primary palette
313 BOOL WINAPI AnimatePalette(
314 HPALETTE hPal, /* [in] Handle to logical palette */
315 UINT StartIndex, /* [in] First entry in palette */
316 UINT NumEntries, /* [in] Count of entries in palette */
317 const PALETTEENTRY* PaletteColors) /* [in] Pointer to first replacement */
319 TRACE("%p (%i - %i)\n", hPal, StartIndex,StartIndex+NumEntries);
321 if( hPal != GetStockObject(DEFAULT_PALETTE) )
323 PALETTEOBJ * palPtr;
324 UINT pal_entries;
325 const PALETTEENTRY *pptr = PaletteColors;
327 palPtr = GDI_GetObjPtr( hPal, OBJ_PAL );
328 if (!palPtr) return 0;
330 pal_entries = palPtr->count;
331 if (StartIndex >= pal_entries)
333 GDI_ReleaseObj( hPal );
334 return 0;
336 if (StartIndex+NumEntries > pal_entries) NumEntries = pal_entries - StartIndex;
338 for (NumEntries += StartIndex; StartIndex < NumEntries; StartIndex++, pptr++) {
339 /* According to MSDN, only animate PC_RESERVED colours */
340 if (palPtr->entries[StartIndex].peFlags & PC_RESERVED) {
341 TRACE("Animating colour (%d,%d,%d) to (%d,%d,%d)\n",
342 palPtr->entries[StartIndex].peRed,
343 palPtr->entries[StartIndex].peGreen,
344 palPtr->entries[StartIndex].peBlue,
345 pptr->peRed, pptr->peGreen, pptr->peBlue);
346 palPtr->entries[StartIndex] = *pptr;
347 } else {
348 TRACE("Not animating entry %d -- not PC_RESERVED\n", StartIndex);
351 GDI_ReleaseObj( hPal );
352 /* FIXME: check for palette selected in active window */
354 return TRUE;
358 /***********************************************************************
359 * SetSystemPaletteUse [GDI32.@]
361 * Specify whether the system palette contains 2 or 20 static colors.
363 * RETURNS
364 * Success: Previous system palette
365 * Failure: SYSPAL_ERROR
367 UINT WINAPI SetSystemPaletteUse(
368 HDC hdc, /* [in] Handle of device context */
369 UINT use) /* [in] Palette-usage flag */
371 UINT old = SystemPaletteUse;
373 /* Device doesn't support colour palettes */
374 if (!(GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)) {
375 return SYSPAL_ERROR;
378 switch (use) {
379 case SYSPAL_NOSTATIC:
380 case SYSPAL_NOSTATIC256: /* WINVER >= 0x0500 */
381 case SYSPAL_STATIC:
382 SystemPaletteUse = use;
383 return old;
384 default:
385 return SYSPAL_ERROR;
390 /***********************************************************************
391 * GetSystemPaletteUse [GDI32.@]
393 * Gets state of system palette.
395 * RETURNS
396 * Current state of system palette
398 UINT WINAPI GetSystemPaletteUse(
399 HDC hdc) /* [in] Handle of device context */
401 return SystemPaletteUse;
405 /***********************************************************************
406 * GetSystemPaletteEntries [GDI32.@]
408 * Gets range of palette entries.
410 * RETURNS
411 * Success: Number of entries retrieved from palette
412 * Failure: 0
414 UINT WINAPI GetSystemPaletteEntries(
415 HDC hdc, /* [in] Handle of device context */
416 UINT start, /* [in] Index of first entry to be retrieved */
417 UINT count, /* [in] Number of entries to be retrieved */
418 LPPALETTEENTRY entries) /* [out] Array receiving system-palette entries */
420 UINT ret = 0;
421 DC *dc;
423 TRACE("hdc=%p,start=%i,count=%i\n", hdc,start,count);
425 if ((dc = get_dc_ptr( hdc )))
427 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pGetSystemPaletteEntries );
428 ret = physdev->funcs->pGetSystemPaletteEntries( physdev, start, count, entries );
429 release_dc_ptr( dc );
431 return ret;
435 /***********************************************************************
436 * GetNearestPaletteIndex [GDI32.@]
438 * Gets palette index for color.
440 * NOTES
441 * Should index be initialized to CLR_INVALID instead of 0?
443 * RETURNS
444 * Success: Index of entry in logical palette
445 * Failure: CLR_INVALID
447 UINT WINAPI GetNearestPaletteIndex(
448 HPALETTE hpalette, /* [in] Handle of logical color palette */
449 COLORREF color) /* [in] Color to be matched */
451 PALETTEOBJ* palObj = GDI_GetObjPtr( hpalette, OBJ_PAL );
452 UINT index = 0;
454 if( palObj )
456 int i, diff = 0x7fffffff;
457 int r,g,b;
458 PALETTEENTRY* entry = palObj->entries;
460 for( i = 0; i < palObj->count && diff ; i++, entry++)
462 r = entry->peRed - GetRValue(color);
463 g = entry->peGreen - GetGValue(color);
464 b = entry->peBlue - GetBValue(color);
466 r = r*r + g*g + b*b;
468 if( r < diff ) { index = i; diff = r; }
470 GDI_ReleaseObj( hpalette );
472 TRACE("(%p,%06x): returning %d\n", hpalette, color, index );
473 return index;
477 /* null driver fallback implementation for GetNearestColor */
478 COLORREF nulldrv_GetNearestColor( PHYSDEV dev, COLORREF color )
480 unsigned char spec_type;
482 if (!(GetDeviceCaps( dev->hdc, RASTERCAPS ) & RC_PALETTE)) return color;
484 spec_type = color >> 24;
485 if (spec_type == 1 || spec_type == 2)
487 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
488 UINT index;
489 PALETTEENTRY entry;
490 HPALETTE hpal = GetCurrentObject( dev->hdc, OBJ_PAL );
492 if (!hpal) hpal = GetStockObject( DEFAULT_PALETTE );
493 if (spec_type == 2) /* PALETTERGB */
494 index = GetNearestPaletteIndex( hpal, color );
495 else /* PALETTEINDEX */
496 index = LOWORD(color);
498 if (!GetPaletteEntries( hpal, index, 1, &entry ))
500 WARN("RGB(%x) : idx %d is out of bounds, assuming NULL\n", color, index );
501 if (!GetPaletteEntries( hpal, 0, 1, &entry )) return CLR_INVALID;
503 color = RGB( entry.peRed, entry.peGreen, entry.peBlue );
505 return color & 0x00ffffff;
509 /***********************************************************************
510 * GetNearestColor [GDI32.@]
512 * Gets a system color to match.
514 * RETURNS
515 * Success: Color from system palette that corresponds to given color
516 * Failure: CLR_INVALID
518 COLORREF WINAPI GetNearestColor(
519 HDC hdc, /* [in] Handle of device context */
520 COLORREF color) /* [in] Color to be matched */
522 COLORREF nearest = CLR_INVALID;
523 DC *dc;
525 if ((dc = get_dc_ptr( hdc )))
527 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pGetNearestColor );
528 nearest = physdev->funcs->pGetNearestColor( physdev, color );
529 release_dc_ptr( dc );
531 return nearest;
535 /***********************************************************************
536 * PALETTE_GetObject
538 static INT PALETTE_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
540 PALETTEOBJ *palette = GDI_GetObjPtr( handle, OBJ_PAL );
542 if (!palette) return 0;
544 if (buffer)
546 if (count > sizeof(WORD)) count = sizeof(WORD);
547 memcpy( buffer, &palette->count, count );
549 else count = sizeof(WORD);
550 GDI_ReleaseObj( handle );
551 return count;
555 /***********************************************************************
556 * PALETTE_UnrealizeObject
558 static BOOL PALETTE_UnrealizeObject( HGDIOBJ handle )
560 PALETTEOBJ *palette = GDI_GetObjPtr( handle, OBJ_PAL );
562 if (palette)
564 unrealize_function unrealize = palette->unrealize;
565 palette->unrealize = NULL;
566 GDI_ReleaseObj( handle );
567 if (unrealize) unrealize( handle );
570 if (InterlockedCompareExchangePointer( (void **)&hLastRealizedPalette, 0, handle ) == handle)
571 TRACE("unrealizing palette %p\n", handle);
573 return TRUE;
577 /***********************************************************************
578 * PALETTE_DeleteObject
580 static BOOL PALETTE_DeleteObject( HGDIOBJ handle )
582 PALETTEOBJ *obj;
584 PALETTE_UnrealizeObject( handle );
585 if (!(obj = free_gdi_handle( handle ))) return FALSE;
586 HeapFree( GetProcessHeap(), 0, obj->entries );
587 return HeapFree( GetProcessHeap(), 0, obj );
591 /***********************************************************************
592 * GDISelectPalette (Not a Windows API)
594 HPALETTE WINAPI GDISelectPalette( HDC hdc, HPALETTE hpal, WORD wBkg)
596 HPALETTE ret = 0;
597 DC *dc;
599 TRACE("%p %p\n", hdc, hpal );
601 if (GetObjectType(hpal) != OBJ_PAL)
603 WARN("invalid selected palette %p\n",hpal);
604 return 0;
606 if ((dc = get_dc_ptr( hdc )))
608 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSelectPalette );
609 ret = dc->hPalette;
610 if (physdev->funcs->pSelectPalette( physdev, hpal, FALSE ))
612 dc->hPalette = hpal;
613 if (!wBkg) hPrimaryPalette = hpal;
615 else ret = 0;
616 release_dc_ptr( dc );
618 return ret;
622 /***********************************************************************
623 * GDIRealizePalette (Not a Windows API)
625 UINT WINAPI GDIRealizePalette( HDC hdc )
627 UINT realized = 0;
628 DC* dc = get_dc_ptr( hdc );
630 if (!dc) return 0;
632 TRACE("%p...\n", hdc );
634 if( dc->hPalette == GetStockObject( DEFAULT_PALETTE ))
636 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pRealizeDefaultPalette );
637 realized = physdev->funcs->pRealizeDefaultPalette( physdev );
639 else if (InterlockedExchangePointer( (void **)&hLastRealizedPalette, dc->hPalette ) != dc->hPalette)
641 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pRealizePalette );
642 PALETTEOBJ *palPtr = GDI_GetObjPtr( dc->hPalette, OBJ_PAL );
643 if (palPtr)
645 realized = physdev->funcs->pRealizePalette( physdev, dc->hPalette,
646 (dc->hPalette == hPrimaryPalette) );
647 palPtr->unrealize = physdev->funcs->pUnrealizePalette;
648 GDI_ReleaseObj( dc->hPalette );
651 else TRACE(" skipping (hLastRealizedPalette = %p)\n", hLastRealizedPalette);
653 release_dc_ptr( dc );
654 TRACE(" realized %i colors.\n", realized );
655 return realized;
659 /***********************************************************************
660 * SelectPalette [GDI32.@]
662 * Selects logical palette into DC.
664 * RETURNS
665 * Success: Previous logical palette
666 * Failure: NULL
668 HPALETTE WINAPI SelectPalette(
669 HDC hDC, /* [in] Handle of device context */
670 HPALETTE hPal, /* [in] Handle of logical color palette */
671 BOOL bForceBackground) /* [in] Foreground/background mode */
673 return pfnSelectPalette( hDC, hPal, bForceBackground );
677 /***********************************************************************
678 * RealizePalette [GDI32.@]
680 * Maps palette entries to system palette.
682 * RETURNS
683 * Success: Number of entries in logical palette
684 * Failure: GDI_ERROR
686 UINT WINAPI RealizePalette(
687 HDC hDC) /* [in] Handle of device context */
689 return pfnRealizePalette( hDC );
693 typedef HWND (WINAPI *WindowFromDC_funcptr)( HDC );
694 typedef BOOL (WINAPI *RedrawWindow_funcptr)( HWND, const RECT *, HRGN, UINT );
696 /**********************************************************************
697 * UpdateColors [GDI32.@]
699 * Remaps current colors to logical palette.
701 * RETURNS
702 * Success: TRUE
703 * Failure: FALSE
705 BOOL WINAPI UpdateColors(
706 HDC hDC) /* [in] Handle of device context */
708 HMODULE mod;
709 int size = GetDeviceCaps( hDC, SIZEPALETTE );
711 if (!size) return 0;
713 mod = GetModuleHandleA("user32.dll");
714 if (mod)
716 WindowFromDC_funcptr pWindowFromDC = (WindowFromDC_funcptr)GetProcAddress(mod,"WindowFromDC");
717 if (pWindowFromDC)
719 HWND hWnd = pWindowFromDC( hDC );
721 /* Docs say that we have to remap current drawable pixel by pixel
722 * but it would take forever given the speed of XGet/PutPixel.
724 if (hWnd && size)
726 RedrawWindow_funcptr pRedrawWindow = (void *)GetProcAddress( mod, "RedrawWindow" );
727 if (pRedrawWindow) pRedrawWindow( hWnd, NULL, 0, RDW_INVALIDATE );
731 return 0x666;
734 /*********************************************************************
735 * SetMagicColors (GDI32.@)
737 BOOL WINAPI SetMagicColors(HDC hdc, ULONG u1, ULONG u2)
739 FIXME("(%p 0x%08x 0x%08x): stub\n", hdc, u1, u2);
740 return TRUE;