1 /*
   2  * Copyright (c) 2009, 2020, 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_ole.h"
  27 #include "awt_DCHolder.h"       // main symbols
  28 
  29 
  30 ////////////////////////
  31 // struct DCHolder
  32 
  33 DCHolder::DCHolder()
  34 : m_hMemoryDC(NULL),
  35     m_iWidth(0),
  36     m_iHeight(0),
  37     m_bForImage(FALSE),
  38     m_hBitmap(NULL),
  39     m_hOldBitmap(NULL),
  40     m_pPoints(NULL)
  41 {}
  42 
  43 void DCHolder::Create(
  44     HDC hRelDC,
  45     int iWidth,
  46     int iHeght,
  47     BOOL bForImage
  48 ){
  49     OLE_DECL
  50     m_iWidth = iWidth;
  51     m_iHeight = iHeght;
  52     m_bForImage = bForImage;
  53     m_hMemoryDC = ::CreateCompatibleDC(hRelDC);
  54     //NB: can not throw an error in non-safe stack!!! Just conversion and logging!
  55     //OLE_WINERROR2HR just set OLE_HR without any throw!
  56     if (!m_hMemoryDC) {
  57         OLE_THROW_LASTERROR(_T("CreateCompatibleDC"))
  58     }
  59     m_hBitmap = m_bForImage
  60         ? CreateJavaContextBitmap(hRelDC, m_iWidth, m_iHeight, &m_pPoints)
  61         : ::CreateCompatibleBitmap(hRelDC, m_iWidth, m_iHeight);
  62     if (!m_hBitmap) {
  63         OLE_THROW_LASTERROR(_T("CreateCompatibleBitmap"))
  64     }
  65     m_hOldBitmap = (HBITMAP)::SelectObject(m_hMemoryDC, m_hBitmap);
  66     if (!m_hOldBitmap) {
  67         OLE_THROW_LASTERROR(_T("SelectBMObject"))
  68     }
  69 }
  70 
  71 DCHolder::~DCHolder(){
  72     if (m_hOldBitmap) {
  73         ::SelectObject(m_hMemoryDC, m_hOldBitmap);
  74     }
  75     if (m_hBitmap) {
  76         ::DeleteObject(m_hBitmap);
  77     }
  78     if (m_hMemoryDC) {
  79         ::DeleteDC(m_hMemoryDC);
  80     }
  81 };
  82 
  83 
  84 HBITMAP DCHolder::CreateJavaContextBitmap(
  85     HDC hdc,
  86     int iWidth,
  87     int iHeight,
  88     void **ppPoints)
  89 {
  90     BITMAPINFO    bitmapInfo = {0};
  91     bitmapInfo.bmiHeader.biWidth = iWidth;
  92     bitmapInfo.bmiHeader.biHeight = -iHeight;
  93     bitmapInfo.bmiHeader.biPlanes = 1;
  94     bitmapInfo.bmiHeader.biBitCount = 32;
  95     bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  96     bitmapInfo.bmiHeader.biCompression = BI_RGB;
  97 
  98     return ::CreateDIBSection(
  99         hdc,
 100         (BITMAPINFO *)&bitmapInfo,
 101         DIB_RGB_COLORS,
 102         (void **)ppPoints,
 103         NULL,
 104         0
 105     );
 106 }