Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame^] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | |
| 19 | #ifndef __RFB_WIN32_LOGPALETTE_H__ |
| 20 | #define __RFB_WIN32_LOGPALETTE_H__ |
| 21 | |
| 22 | #include <windows.h> |
| 23 | #include <rdr/Exception.h> |
| 24 | |
| 25 | namespace rfb { |
| 26 | namespace win32 { |
| 27 | |
| 28 | class LogicalPalette { |
| 29 | public: |
| 30 | LogicalPalette() { |
| 31 | BYTE buf[sizeof(LOGPALETTE)+256*sizeof(PALETTEENTRY)]; |
| 32 | LOGPALETTE* logpal = (LOGPALETTE*)buf; |
| 33 | logpal->palVersion = 0x300; |
| 34 | logpal->palNumEntries = 256; |
| 35 | for (int i=0; i<256;i++) { |
| 36 | logpal->palPalEntry[i].peRed = 0; |
| 37 | logpal->palPalEntry[i].peGreen = 0; |
| 38 | logpal->palPalEntry[i].peBlue = 0; |
| 39 | logpal->palPalEntry[i].peFlags = 0; |
| 40 | } |
| 41 | palette = CreatePalette(logpal); |
| 42 | if (!palette) |
| 43 | throw rdr::SystemException("failed to CreatePalette", GetLastError()); |
| 44 | } |
| 45 | ~LogicalPalette() { |
| 46 | if (palette && !DeleteObject(palette)) |
| 47 | throw rdr::SystemException("del palette failed", GetLastError()); |
| 48 | } |
| 49 | void setEntries(int start, int count, const Colour* cols) { |
| 50 | if (numEntries < count) { |
| 51 | ResizePalette(palette, start+count); |
| 52 | numEntries = start+count; |
| 53 | } |
| 54 | PALETTEENTRY* logpal = new PALETTEENTRY[count]; |
| 55 | for (int i=0; i<count; i++) { |
| 56 | logpal[i].peRed = cols[i].r >> 8; |
| 57 | logpal[i].peGreen = cols[i].g >> 8; |
| 58 | logpal[i].peBlue = cols[i].b >> 8; |
| 59 | logpal[i].peFlags = 0; |
| 60 | } |
| 61 | UnrealizeObject(palette); |
| 62 | SetPaletteEntries(palette, start, count, logpal); |
| 63 | delete [] logpal; |
| 64 | } |
| 65 | HPALETTE getHandle() {return palette;} |
| 66 | protected: |
| 67 | HPALETTE palette; |
| 68 | int numEntries; |
| 69 | }; |
| 70 | |
| 71 | class PaletteSelector { |
| 72 | public: |
| 73 | PaletteSelector(HDC dc, HPALETTE pal) : device(dc), redrawRequired(false) { |
| 74 | oldPal = SelectPalette(dc, pal, FALSE); |
| 75 | redrawRequired = RealizePalette(dc) > 0; |
| 76 | } |
| 77 | ~PaletteSelector() { |
| 78 | if (oldPal) SelectPalette(device, oldPal, TRUE); |
| 79 | } |
| 80 | bool isRedrawRequired() {return redrawRequired;} |
| 81 | protected: |
| 82 | HPALETTE oldPal; |
| 83 | HDC device; |
| 84 | bool redrawRequired; |
| 85 | }; |
| 86 | |
| 87 | }; |
| 88 | }; |
| 89 | |
| 90 | #endif |