1 /*
   2  * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include "awt.h"
  27 #include "awt_ole.h"
  28 #include "awt_DCHolder.h"       // main symbols
  29 
  30 
  31 ////////////////////////
  32 // struct DCHolder
  33 
  34 DCHolder::DCHolder()
  35 : m_hMemoryDC(NULL),
  36     m_iWidth(0),
  37     m_iHeight(0),
  38     m_bForImage(FALSE),
  39     m_hBitmap(NULL),
  40     m_hOldBitmap(NULL),
  41     m_pPoints(NULL)
  42 {}
  43 
  44 void DCHolder::Create(
  45     HDC hRelDC,
  46     int iWidth,
  47     int iHeght,
  48     BOOL bForImage
  49 ){
  50     OLE_DECL
  51     m_iWidth = iWidth;
  52     m_iHeight = iHeght;
  53     m_bForImage = bForImage;
  54     m_hMemoryDC = ::CreateCompatibleDC(hRelDC);
  55     //NB: can not throw an error in non-safe stack!!! Just conversion and logging!
  56     //OLE_WINERROR2HR just set OLE_HR without any throw!
  57     if (!m_hMemoryDC) {
  58         OLE_THROW_LASTERROR(_T("CreateCompatibleDC"))
  59     }
  60     m_hBitmap = m_bForImage
  61         ? CreateJavaContextBitmap(hRelDC, m_iWidth, m_iHeight, &m_pPoints)
  62         : ::CreateCompatibleBitmap(hRelDC, m_iWidth, m_iHeight);
  63     if (!m_hBitmap) {
  64         OLE_THROW_LASTERROR(_T("CreateCompatibleBitmap"))
  65     }
  66     m_hOldBitmap = (HBITMAP)::SelectObject(m_hMemoryDC, m_hBitmap);
  67     if (!m_hOldBitmap) {
  68         OLE_THROW_LASTERROR(_T("SelectBMObject"))
  69     }
  70 }
  71 
  72 DCHolder::~DCHolder(){
  73     if (m_hOldBitmap) {
  74         ::SelectObject(m_hMemoryDC, m_hOldBitmap);
  75     }
  76     if (m_hBitmap) {
  77         ::DeleteObject(m_hBitmap);
  78     }
  79     if (m_hMemoryDC) {
  80         ::DeleteDC(m_hMemoryDC);
  81     }
  82 };
  83 
  84 
  85 HBITMAP DCHolder::CreateJavaContextBitmap(
  86     HDC hdc,
  87     int iWidth,
  88     int iHeight,
  89     void **ppPoints)
  90 {
  91     BITMAPINFO    bitmapInfo = {0};
  92     bitmapInfo.bmiHeader.biWidth = iWidth;
  93     bitmapInfo.bmiHeader.biHeight = -iHeight;
  94     bitmapInfo.bmiHeader.biPlanes = 1;
  95     bitmapInfo.bmiHeader.biBitCount = 32;
  96     bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  97     bitmapInfo.bmiHeader.biCompression = BI_RGB;
  98 
  99     return ::CreateDIBSection(
 100         hdc,
 101         (BITMAPINFO *)&bitmapInfo,
 102         DIB_RGB_COLORS,
 103         (void **)ppPoints,
 104         NULL,
 105         0
 106     );
 107 }