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