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