| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: palette.cpp |
| 3 | // Purpose: wxPalette |
| 4 | // Author: Julian Smart |
| 5 | // Modified by: |
| 6 | // Created: 04/01/98 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Julian Smart and Markus Holzem |
| 9 | // Licence: wxWindows license |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #ifdef __GNUG__ |
| 13 | #pragma implementation "palette.h" |
| 14 | #endif |
| 15 | |
| 16 | // For compilers that support precompilation, includes "wx.h". |
| 17 | #include "wx/wxprec.h" |
| 18 | |
| 19 | #ifdef __BORLANDC__ |
| 20 | #pragma hdrstop |
| 21 | #endif |
| 22 | |
| 23 | #ifndef WX_PRECOMP |
| 24 | #include <stdio.h> |
| 25 | #include "wx/setup.h" |
| 26 | #include "wx/palette.h" |
| 27 | #endif |
| 28 | |
| 29 | #include <windows.h> |
| 30 | |
| 31 | #include "assert.h" |
| 32 | |
| 33 | IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject) |
| 34 | |
| 35 | /* |
| 36 | * Palette |
| 37 | * |
| 38 | */ |
| 39 | |
| 40 | wxPaletteRefData::wxPaletteRefData(void) |
| 41 | { |
| 42 | m_hPalette = 0; |
| 43 | } |
| 44 | |
| 45 | wxPaletteRefData::~wxPaletteRefData(void) |
| 46 | { |
| 47 | if ( m_hPalette ) |
| 48 | ::DeleteObject((HPALETTE) m_hPalette); |
| 49 | } |
| 50 | |
| 51 | wxPalette::wxPalette(void) |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue) |
| 56 | { |
| 57 | Create(n, red, green, blue); |
| 58 | } |
| 59 | |
| 60 | wxPalette::~wxPalette(void) |
| 61 | { |
| 62 | // FreeResource(TRUE); |
| 63 | } |
| 64 | |
| 65 | bool wxPalette::FreeResource(bool force) |
| 66 | { |
| 67 | if ( M_PALETTEDATA && M_PALETTEDATA->m_hPalette) |
| 68 | { |
| 69 | DeleteObject((HPALETTE)M_PALETTEDATA->m_hPalette); |
| 70 | } |
| 71 | return TRUE; |
| 72 | } |
| 73 | |
| 74 | bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue) |
| 75 | { |
| 76 | UnRef(); |
| 77 | |
| 78 | #ifdef __WXWINE__ |
| 79 | |
| 80 | return (FALSE); |
| 81 | |
| 82 | #else |
| 83 | |
| 84 | m_refData = new wxPaletteRefData; |
| 85 | |
| 86 | NPLOGPALETTE npPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) + |
| 87 | (WORD)n * sizeof(PALETTEENTRY)); |
| 88 | if (!npPal) |
| 89 | return(FALSE); |
| 90 | |
| 91 | npPal->palVersion = 0x300; |
| 92 | npPal->palNumEntries = n; |
| 93 | |
| 94 | int i; |
| 95 | for (i = 0; i < n; i ++) |
| 96 | { |
| 97 | npPal->palPalEntry[i].peRed = red[i]; |
| 98 | npPal->palPalEntry[i].peGreen = green[i]; |
| 99 | npPal->palPalEntry[i].peBlue = blue[i]; |
| 100 | npPal->palPalEntry[i].peFlags = 0; |
| 101 | } |
| 102 | M_PALETTEDATA->m_hPalette = (WXHPALETTE) CreatePalette((LPLOGPALETTE)npPal); |
| 103 | LocalFree((HANDLE)npPal); |
| 104 | return TRUE; |
| 105 | |
| 106 | #endif |
| 107 | } |
| 108 | |
| 109 | int wxPalette::GetPixel(const unsigned char red, const unsigned char green, const unsigned char blue) const |
| 110 | { |
| 111 | if ( !m_refData ) |
| 112 | return FALSE; |
| 113 | |
| 114 | return ::GetNearestPaletteIndex((HPALETTE) M_PALETTEDATA->m_hPalette, PALETTERGB(red, green, blue)); |
| 115 | } |
| 116 | |
| 117 | bool wxPalette::GetRGB(int index, unsigned char *red, unsigned char *green, unsigned char *blue) const |
| 118 | { |
| 119 | if ( !m_refData ) |
| 120 | return FALSE; |
| 121 | |
| 122 | if (index < 0 || index > 255) |
| 123 | return FALSE; |
| 124 | |
| 125 | PALETTEENTRY entry; |
| 126 | if (::GetPaletteEntries((HPALETTE) M_PALETTEDATA->m_hPalette, index, 1, &entry)) |
| 127 | { |
| 128 | *red = entry.peRed; |
| 129 | *green = entry.peGreen; |
| 130 | *blue = entry.peBlue; |
| 131 | return TRUE; |
| 132 | } else |
| 133 | return FALSE; |
| 134 | } |
| 135 | |
| 136 | void wxPalette::SetHPALETTE(WXHPALETTE pal) |
| 137 | { |
| 138 | if ( !m_refData ) |
| 139 | m_refData = new wxPaletteRefData; |
| 140 | |
| 141 | M_PALETTEDATA->m_hPalette = pal; |
| 142 | } |
| 143 | |