Back to index

lightning-sunbird  0.9+nobinonly
nsPaletteOS2.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is an API for using the OS/2 Palette Manager.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * IBM Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2002
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #include "nsPaletteOS2.h"
00038 #include "nsGfxDefs.h"
00039 #ifdef DEBUG
00040 #include <stdio.h>
00041 #endif
00042 
00043 HPAL nsPaletteOS2::hGlobalPalette = NULLHANDLE;
00044 BOOL nsPaletteOS2::fPaletteInitialized = FALSE;
00045 ULONG nsPaletteOS2::aulTable[256];
00046 
00047 #define NUM_SYS_COLORS 24
00048 
00049 typedef struct _MYRGB {
00050   BYTE red;
00051   BYTE green;
00052   BYTE blue;
00053 } MYRGB;
00054 
00055 MYRGB sysColors[NUM_SYS_COLORS] =
00056 {
00057   0x00, 0x00, 0x00,   // CLR_BLACK
00058   0x00, 0x00, 0x80,   // CLR_DARKBLUE
00059   0x00, 0x80, 0x00,   // CLR_DARKGREEN
00060   0x00, 0x80, 0x80,   // CLR_DARKCYAN
00061   0x80, 0x00, 0x00,   // CLR_DARKRED
00062   0x80, 0x00, 0x80,   // CLR_DARKPINK
00063   0x80, 0x80, 0x00,   // CLR_BROWN
00064   0x80, 0x80, 0x80,   // CLR_DARKGRAY
00065   0xCC, 0xCC, 0xCC,   // CLR_PALEGRAY
00066   0x00, 0x00, 0xFF,   // CLR_BLUE
00067   0x00, 0xFF, 0x00,   // CLR_GREEN
00068   0x00, 0xFF, 0xFF,   // CLR_CYAN
00069   0xFF, 0x00, 0x00,   // CLR_RED
00070   0xFF, 0x00, 0xFF,   // CLR_PINK
00071   0xFF, 0xFF, 0x00,   // CLR_YELLOW
00072   0xFE, 0xFE, 0xFE,   // CLR_OFFWHITE - can only use white at index 255
00073 
00074   0xC0, 0xC0, 0xC0,   // Gray (Windows)
00075   0xFF, 0xFB, 0xF0,   // Pale Yellow (Windows)
00076   0xC0, 0xDC, 0xC0,   // Pale Green (Windows)
00077   0xA4, 0xC8, 0xF0,   // Light Blue (Windows)
00078   0xA4, 0xA0, 0xA4,   // Medium Gray (Windows)
00079 
00080   0xFF, 0xFF, 0xE4,   // Tooltip color - see nsLookAndFeel.cpp
00081 
00082   0x71, 0x71, 0x71,   // Interpolated color for entryfields
00083   0xEF, 0xEF, 0xEF    // Interpolated color for entryfields
00084 
00085 };
00086 
00087 void nsPaletteOS2::InitializeGlobalPalette()
00088 {
00089   fPaletteInitialized = TRUE;
00090   LONG lCaps;
00091   HPS hps = ::WinGetScreenPS(HWND_DESKTOP);
00092   HDC hdc = ::GpiQueryDevice (hps);
00093   ::DevQueryCaps(hdc, CAPS_ADDITIONAL_GRAPHICS, 1, &lCaps);
00094   ::WinReleasePS(hps);
00095 
00096   if (lCaps & CAPS_PALETTE_MANAGER) {
00097     /* Create the color table */
00098     int i,j,k,l, ulCurTableEntry = 0;
00099   
00100     /* First add the system colors */
00101     for (i = 0; i < NUM_SYS_COLORS; i++) {
00102       aulTable[ulCurTableEntry] = MK_RGB(sysColors[i].red, sysColors[i].green, sysColors[i].blue);
00103       ulCurTableEntry++;
00104     }
00105   
00106     /* Then put the color cube into the table, excluding */
00107     /* any entry that is also in the system color table */
00108     for (i=0x00;i <= 0xff;i+=0x33) {
00109       for (j=0x00;j <= 0xff;j+=0x33) {
00110         for (k=0x00;k <= 0xff ;k+=0x33) {
00111           for (l=0;l<ulCurTableEntry;l++) {
00112             if (aulTable[l] == MK_RGB(i, j, k))
00113               break;
00114           }
00115           if (l == ulCurTableEntry) {
00116             aulTable[ulCurTableEntry] = MK_RGB(i, j, k);
00117             ulCurTableEntry++;
00118           }
00119         }
00120       }
00121     }
00122   
00123     /* Back current table entry up one so we overwrite the white that was written */
00124     /* by the color cube */
00125     ulCurTableEntry--;
00126   
00127     /* Then fudge the rest of the table to have entries that are neither white */
00128     /* nor black - we'll use an offwhite */
00129     while (ulCurTableEntry < 255) {
00130       aulTable[ulCurTableEntry] = MK_RGB(254, 254, 254);
00131       ulCurTableEntry++;
00132     }
00133   
00134     /* Finally, make the last entry white */
00135     aulTable[ulCurTableEntry] = MK_RGB(255, 255, 255);
00136   
00137 #ifdef DEBUG_mikek
00138     for (i=0;i<256 ;i++ )
00139       printf("Entry[%d] in 256 color table is %x\n", i, aulTable[i]);
00140 #endif
00141 
00142     /* Create the palette */
00143     hGlobalPalette = ::GpiCreatePalette ((HAB)0, 0,
00144                                          LCOLF_CONSECRGB, 256, aulTable);
00145   }
00146 }
00147 
00148 void nsPaletteOS2::FreeGlobalPalette()
00149 {
00150   if (hGlobalPalette) {
00151     GpiDeletePalette(hGlobalPalette);
00152     hGlobalPalette = NULLHANDLE;
00153   }
00154 }
00155 
00156 void nsPaletteOS2::SelectGlobalPalette(HPS hps, HWND hwnd)
00157 {
00158   if (!fPaletteInitialized)
00159     InitializeGlobalPalette();
00160   if (hGlobalPalette) {
00161     GpiSelectPalette(hps, hGlobalPalette);
00162     if (hwnd != NULLHANDLE) {
00163       ULONG cclr;
00164       WinRealizePalette(hwnd, hps, &cclr);
00165     }
00166   }
00167 }
00168 
00169 LONG nsPaletteOS2::QueryColorIndex(LONG lColor)
00170 {
00171   for (int i=0;i<256;i++) {
00172      if (lColor == aulTable[i]) {
00173         return i;
00174      }
00175   }
00176   return 0;
00177 }