| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/os2/palette.cpp |
| 3 | // Purpose: wxPalette |
| 4 | // Author: AUTHOR |
| 5 | // Modified by: |
| 6 | // Created: ??/??/98 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) AUTHOR |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // For compilers that support precompilation, includes "wx.h". |
| 13 | #include "wx/wxprec.h" |
| 14 | |
| 15 | #ifndef WX_PRECOMP |
| 16 | #include <stdio.h> |
| 17 | #include "wx/string.h" |
| 18 | #include "wx/os2/private.h" |
| 19 | #include "wx/palette.h" |
| 20 | #include "wx/app.h" |
| 21 | #endif |
| 22 | |
| 23 | #define INCL_PM |
| 24 | #define INCL_GPI |
| 25 | |
| 26 | #include "assert.h" |
| 27 | |
| 28 | IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject) |
| 29 | |
| 30 | /* |
| 31 | * Palette |
| 32 | * |
| 33 | */ |
| 34 | |
| 35 | wxPaletteRefData::wxPaletteRefData() |
| 36 | { |
| 37 | m_hPalette = NULLHANDLE; |
| 38 | m_hPS = NULLHANDLE; |
| 39 | } // end of wxPaletteRefData::wxPaletteRefData |
| 40 | |
| 41 | wxPaletteRefData::~wxPaletteRefData() |
| 42 | { |
| 43 | if ( m_hPalette ) |
| 44 | return; |
| 45 | } // end of wxPaletteRefData::~wxPaletteRefData |
| 46 | |
| 47 | wxPalette::wxPalette() |
| 48 | { |
| 49 | } // end of wxPalette::wxPalette |
| 50 | |
| 51 | wxPalette::wxPalette( |
| 52 | int n |
| 53 | , const unsigned char* pRed |
| 54 | , const unsigned char* pGreen |
| 55 | , const unsigned char* pBlue |
| 56 | ) |
| 57 | { |
| 58 | Create( n |
| 59 | ,pRed |
| 60 | ,pGreen |
| 61 | ,pBlue |
| 62 | ); |
| 63 | } // end of wxPalette::wxPalette |
| 64 | |
| 65 | wxPalette::~wxPalette() |
| 66 | { |
| 67 | } // end of wxPalette::~wxPalette |
| 68 | |
| 69 | bool wxPalette::FreeResource( bool WXUNUSED(bForce) ) |
| 70 | { |
| 71 | if ( M_PALETTEDATA && M_PALETTEDATA->m_hPalette) |
| 72 | { |
| 73 | ::GpiSelectPalette(M_PALETTEDATA->m_hPS, NULLHANDLE); |
| 74 | ::GpiDeletePalette((HPAL)M_PALETTEDATA->m_hPalette); |
| 75 | } |
| 76 | return true; |
| 77 | } // end of wxPalette::FreeResource |
| 78 | |
| 79 | bool wxPalette::Create( int n, |
| 80 | const unsigned char* pRed, |
| 81 | const unsigned char* pGreen, |
| 82 | const unsigned char* pBlue ) |
| 83 | { |
| 84 | PULONG pualTable; |
| 85 | |
| 86 | UnRef(); |
| 87 | |
| 88 | m_refData = new wxPaletteRefData; |
| 89 | pualTable = new ULONG[n]; |
| 90 | if (!pualTable) |
| 91 | return false; |
| 92 | |
| 93 | for (int i = 0; i < n; i ++) |
| 94 | { |
| 95 | pualTable[i] = (PC_RESERVED * 16777216) + ((int)pRed[i] * 65536) + ((int)pGreen[i] * 256) + (int)pBlue[i]; |
| 96 | } |
| 97 | M_PALETTEDATA->m_hPalette = (WXHPALETTE)::GpiCreatePalette( vHabmain |
| 98 | ,LCOL_PURECOLOR |
| 99 | ,LCOLF_CONSECRGB |
| 100 | ,(LONG)n |
| 101 | ,pualTable |
| 102 | ); |
| 103 | delete [] pualTable; |
| 104 | return true; |
| 105 | } // end of wxPalette::Create |
| 106 | |
| 107 | int wxPalette::GetPixel( unsigned char cRed, |
| 108 | unsigned char cGreen, |
| 109 | unsigned char cBlue) const |
| 110 | { |
| 111 | bool bFound = false; |
| 112 | PULONG pualTable = NULL; |
| 113 | ULONG ulNumEntries; |
| 114 | ULONG ulRGB = (PC_RESERVED * 16777216) + |
| 115 | ((int)cRed * 65536) + |
| 116 | ((int)cGreen * 256) + |
| 117 | (int)cBlue; |
| 118 | |
| 119 | if (!m_refData) |
| 120 | return wxNOT_FOUND; |
| 121 | |
| 122 | // |
| 123 | // Get number of entries first |
| 124 | // |
| 125 | ulNumEntries = ::GpiQueryPaletteInfo( M_PALETTEDATA->m_hPalette |
| 126 | ,M_PALETTEDATA->m_hPS |
| 127 | ,0 // No options |
| 128 | ,0 // No start index |
| 129 | ,0 // Force return of number entries |
| 130 | ,NULL // No array |
| 131 | ); |
| 132 | |
| 133 | pualTable = new ULONG[ulNumEntries]; |
| 134 | |
| 135 | // |
| 136 | // Now get the entries |
| 137 | // |
| 138 | ulNumEntries = ::GpiQueryPaletteInfo( M_PALETTEDATA->m_hPalette |
| 139 | ,M_PALETTEDATA->m_hPS |
| 140 | ,0 // No options |
| 141 | ,0 // start at 0 |
| 142 | ,ulNumEntries // Force return of number entries |
| 143 | ,pualTable // Palette entry array with RGB values |
| 144 | ); |
| 145 | // |
| 146 | // Now loop through and find the matching entry |
| 147 | // |
| 148 | ULONG i; |
| 149 | for (i = 0; i < ulNumEntries; i++) |
| 150 | { |
| 151 | if (pualTable[i] == ulRGB) |
| 152 | { |
| 153 | bFound = true; |
| 154 | break; |
| 155 | } |
| 156 | } |
| 157 | if (!bFound) |
| 158 | return wxNOT_FOUND; |
| 159 | return (i + 1); |
| 160 | } // end of wxPalette::GetPixel |
| 161 | |
| 162 | bool wxPalette::GetRGB( int nIndex, |
| 163 | unsigned char* pRed, |
| 164 | unsigned char* pGreen, |
| 165 | unsigned char* pBlue) const |
| 166 | { |
| 167 | PULONG pualTable = NULL; |
| 168 | RGB2 vRGB; |
| 169 | ULONG ulNumEntries; |
| 170 | |
| 171 | if (!m_refData) |
| 172 | return false; |
| 173 | |
| 174 | if (nIndex < 0 || nIndex > 255) |
| 175 | return false; |
| 176 | // |
| 177 | // Get number of entries first |
| 178 | // |
| 179 | ulNumEntries = ::GpiQueryPaletteInfo( M_PALETTEDATA->m_hPalette |
| 180 | ,M_PALETTEDATA->m_hPS |
| 181 | ,0 // No options |
| 182 | ,0 // No start index |
| 183 | ,0 // Force return of number entries |
| 184 | ,NULL // No array |
| 185 | ); |
| 186 | |
| 187 | pualTable = new ULONG[ulNumEntries]; |
| 188 | |
| 189 | // |
| 190 | // Now get the entries |
| 191 | // |
| 192 | ulNumEntries = ::GpiQueryPaletteInfo( M_PALETTEDATA->m_hPalette |
| 193 | ,M_PALETTEDATA->m_hPS |
| 194 | ,0 // No options |
| 195 | ,0 // start at 0 |
| 196 | ,ulNumEntries // Force return of number entries |
| 197 | ,pualTable // Palette entry array with RGB values |
| 198 | ); |
| 199 | |
| 200 | memcpy(&vRGB, &pualTable[nIndex], sizeof(RGB2)); |
| 201 | *pBlue = vRGB.bBlue; |
| 202 | *pGreen = vRGB.bGreen; |
| 203 | *pRed = vRGB.bRed; |
| 204 | return true; |
| 205 | } // end of wxPalette::GetRGB |
| 206 | |
| 207 | void wxPalette::SetHPALETTE( |
| 208 | WXHPALETTE hPal |
| 209 | ) |
| 210 | { |
| 211 | if ( !m_refData ) |
| 212 | m_refData = new wxPaletteRefData; |
| 213 | |
| 214 | M_PALETTEDATA->m_hPalette = hPal; |
| 215 | } // end of wxPalette::SetHPALETTE |
| 216 | |
| 217 | void wxPalette::SetPS( |
| 218 | HPS hPS |
| 219 | ) |
| 220 | { |
| 221 | if ( !m_refData ) |
| 222 | m_refData = new wxPaletteRefData; |
| 223 | |
| 224 | ::GpiSelectPalette(M_PALETTEDATA->m_hPS, M_PALETTEDATA->m_hPalette); |
| 225 | M_PALETTEDATA->m_hPS = hPS; |
| 226 | } // end of wxPalette::SetHPALETTE |