home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / prefs / nsdlg / src / bitmpbtn.cpp next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.9 KB  |  199 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include <windows.h>
  20. #include <windowsx.h>
  21. #include <assert.h>
  22. #include "bitmpbtn.h"
  23. #include "dlgutil.h"
  24.  
  25. #ifndef _WIN32
  26. // Loads a bitmap and returns the handle. Retrieves the color of the
  27. // first pixel in the image and replaces that entry in the color table
  28. // with COLOR_3DFACE
  29. HBITMAP WINAPI
  30. LoadTransparentBitmap(HINSTANCE hInstance, UINT nResID)
  31. {
  32.     // Find the resource
  33.     HRSRC    hrsrc = FindResource(hInstance, MAKEINTRESOURCE(nResID), RT_BITMAP);
  34.  
  35.     assert(hrsrc);
  36.     if (!hrsrc)
  37.         return NULL;
  38.  
  39.     // Get a handle for the resource
  40.     HGLOBAL    hglb = LoadResource(hInstance, hrsrc);
  41.     if (!hglb)
  42.         return NULL;
  43.  
  44.     // Get a pointer to the BITMAPINFOHEADER
  45.     LPBITMAPINFOHEADER    lpbi = (LPBITMAPINFOHEADER)LockResource(hglb);
  46.  
  47.     // We expect a 4-bpp image only
  48.     assert(lpbi->biBitCount == 4);
  49.     if (lpbi->biBitCount != 4) {
  50.         UnlockResource(hglb);
  51.         FreeResource(hglb);
  52.         return NULL;
  53.     }
  54.  
  55.     // Get a pointer to the color table
  56.     LPRGBQUAD    pColors = (LPRGBQUAD)((LPSTR)lpbi + (WORD)lpbi->biSize);
  57.  
  58.     // Look at the first pixel and get the palette index
  59.     UINT    nClrUsed = lpbi->biClrUsed == 0 ? 16 : (UINT)lpbi->biClrUsed;
  60.     LPBYTE    lpBits = (LPBYTE)(pColors + nClrUsed);
  61.  
  62.     // Munge the color table entry
  63.     COLORREF    clrBtnFace = GetSysColor(COLOR_BTNFACE);
  64.     int            nIndex = *lpBits & 0xF;
  65.  
  66.     pColors[nIndex].rgbRed = GetRValue(clrBtnFace);
  67.     pColors[nIndex].rgbGreen = GetGValue(clrBtnFace);
  68.     pColors[nIndex].rgbBlue = GetBValue(clrBtnFace);
  69.  
  70.     // Create the DDB
  71.     HBITMAP        hBitmap;
  72.     HDC            hDC = GetDC(NULL);
  73.  
  74.     hBitmap = CreateDIBitmap(hDC, lpbi, CBM_INIT, lpBits,
  75.         (LPBITMAPINFO)lpbi, DIB_RGB_COLORS);
  76.     ReleaseDC(NULL, hDC);
  77.  
  78.     // Release the resource
  79.     UnlockResource(hglb);
  80.     FreeResource(hglb);
  81.  
  82.     return hBitmap;
  83. }
  84. #endif
  85.  
  86. // Sizes the button to fit the bitmap
  87. void WINAPI
  88. SizeToFitBitmapButton(HWND hwndCtl, HBITMAP hBitmap)
  89. {
  90.     BITMAP    bm;
  91.  
  92.     GetObject(hBitmap, sizeof(bm), &bm);
  93.     SetWindowPos(hwndCtl, NULL, 0, 0, bm.bmWidth + 4, bm.bmHeight + 4,
  94.         SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
  95. }
  96.  
  97. #define ROP_PSDPxax  0x00B8074AL
  98.  
  99. // Draws the bitmap in a disabled appearance. This routine assumes the
  100. // bitmap has already been selected into the memory DC
  101. static void NEAR
  102. DisabledBitBlt(HDC hDestDC, RECT &r, HDC hMemDC)
  103. {
  104.     HDC            hMonoDC;
  105.     HBITMAP        hMonoBitmap, hOldBitmap;
  106.     HBRUSH        hOldBrush;
  107.     int            nWidth = r.right - r.left;
  108.     int            nHeight = r.bottom - r.top;
  109. #ifndef _WIN32
  110.     HBRUSH        hShadowBrush = CreateSolidBrush(GetSysColor(COLOR_BTNSHADOW));
  111. #endif
  112.  
  113.     // Create a monochrome bitmap and HDC
  114.     hMonoBitmap = CreateBitmap(nWidth, nHeight, 1, 1, NULL);
  115.     hMonoDC = CreateCompatibleDC(NULL);
  116.     hOldBitmap = SelectBitmap(hMonoDC, hMonoBitmap);
  117.  
  118.     // Create a mask where the button face color is 1 and everything else is 0
  119.     SetBkColor(hMemDC, GetSysColor(COLOR_BTNFACE));
  120.     BitBlt(hMonoDC, 0, 0, nWidth, nHeight, hMemDC, 0, 0, SRCCOPY);
  121.  
  122.     SetTextColor(hDestDC, 0L);                  // 0's in mono -> 0 (for ROP)
  123.     SetBkColor(hDestDC, (COLORREF)0x00FFFFFFL); // 1's in mono -> 1
  124.     
  125.     // Every place the mask has a 0 pixel draw with button highlight color
  126. #ifdef _WIN32
  127.     hOldBrush = SelectBrush(hDestDC, GetSysColorBrush(COLOR_BTNHILIGHT));
  128. #else
  129.     hOldBrush = SelectBrush(hDestDC, GetStockObject(WHITE_BRUSH));
  130. #endif
  131.     BitBlt(hDestDC, r.left + 1, r.top + 1, nWidth - 1, nHeight - 1, hMonoDC,
  132.         0, 0, ROP_PSDPxax);
  133.  
  134.     // Every place the mask has a 0 pixel draw with button shadow color
  135. #ifdef _WIN32
  136.     SelectBrush(hDestDC, GetSysColorBrush(COLOR_BTNSHADOW));
  137. #else
  138.     SelectBrush(hDestDC, hShadowBrush);
  139. #endif
  140.     BitBlt(hDestDC, r.left, r.top, nWidth, nHeight, hMonoDC,
  141.         0, 0, ROP_PSDPxax);
  142.  
  143.     // Restore the destination HDC
  144.     SelectBrush(hDestDC, hOldBrush);
  145. #ifndef _WIN32
  146.     DeleteObject(hShadowBrush);
  147. #endif
  148.  
  149.     // Destroy the GDI objects
  150.     SelectBitmap(hMonoDC, hOldBitmap);
  151.     DeleteBitmap(hMonoBitmap);
  152.     DeleteDC(hMonoDC);
  153. }
  154.  
  155. void WINAPI
  156. DrawBitmapButton(LPDRAWITEMSTRUCT lpdis, HBITMAP hBitmap, HDC hMemDC)
  157. {
  158.     UINT    uState;
  159.     
  160.     // Draw the button borders
  161. #ifdef _WIN32
  162.     uState = DFCS_BUTTONPUSH | DFCS_ADJUSTRECT;
  163.  
  164.     if (lpdis->itemState & ODS_SELECTED)
  165.         uState |= DFCS_PUSHED;
  166.     DrawFrameControl(lpdis->hDC, &lpdis->rcItem, DFC_BUTTON, uState);
  167. #else
  168.     uState = DPBCS_ADJUSTRECT;
  169.     
  170.     if (lpdis->itemState & ODS_SELECTED)
  171.         uState |= DPBCS_PUSHED;
  172.     DrawPushButtonControl(lpdis->hDC, &lpdis->rcItem, uState);
  173. #endif
  174.  
  175.     // If the button's selected, then shift everything down and over to the right
  176.     if (lpdis->itemState & ODS_SELECTED)
  177.         OffsetRect(&lpdis->rcItem, 1, 1);
  178.     
  179.     // Draw the bitmap
  180.     HBITMAP    hOldBitmap = SelectBitmap(hMemDC, hBitmap);
  181.  
  182.     if (lpdis->itemState & ODS_DISABLED)
  183.         DisabledBitBlt(lpdis->hDC, lpdis->rcItem, hMemDC);
  184.     else
  185.         BitBlt(lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top, lpdis->rcItem.right -
  186.             lpdis->rcItem.left, lpdis->rcItem.bottom - lpdis->rcItem.top, hMemDC,
  187.             0, 0, SRCCOPY);
  188.     
  189.     // Resore the memory DC
  190.     SelectBitmap(hMemDC, hOldBitmap);
  191.  
  192.     // Draw the focus rect if requested
  193.     if ((lpdis->itemAction & ODA_FOCUS) || (lpdis->itemState & ODS_FOCUS)) {
  194.         lpdis->rcItem.right--;
  195.         lpdis->rcItem.bottom--;
  196.         DrawFocusRect(lpdis->hDC, &lpdis->rcItem);
  197.     }
  198. }
  199.