001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Dmitry A. Durnev
019: * @version $Revision$
020: */package org.apache.harmony.awt.wtk.windows;
021:
022: import java.awt.Image;
023: import java.awt.image.BufferedImage;
024:
025: import org.apache.harmony.awt.gl.Utils;
026: import org.apache.harmony.awt.nativebridge.Int32Pointer;
027: import org.apache.harmony.awt.nativebridge.NativeBridge;
028: import org.apache.harmony.awt.nativebridge.PointerPointer;
029: import org.apache.harmony.awt.nativebridge.windows.Win32;
030: import org.apache.harmony.awt.nativebridge.windows.WindowsDefs;
031:
032: class WinIcons {
033: static final NativeBridge bridge = NativeBridge.getInstance();
034: static final Win32 win32 = Win32.getInstance();
035:
036: static long createIcon(boolean icon, Image img, int xHotSpot,
037: int yHotSpot) {
038: BufferedImage bufImg = Utils.getBufferedImage(img);
039: if (bufImg == null) {
040: return 0;
041: }
042: int width = img.getWidth(null);
043: int height = img.getHeight(null);
044: int size = width * height;
045:
046: Win32.BITMAPINFO bmi = win32.createBITMAPINFO(false);
047: Win32.BITMAPINFOHEADER bmiHeader = bmi.get_bmiHeader();
048: bmiHeader.set_biSize(bmiHeader.size());
049: bmiHeader.set_biWidth(width);
050: bmiHeader.set_biHeight(-height);
051: bmiHeader.set_biPlanes((short) 1);
052: bmiHeader.set_biBitCount((short) 32);
053: bmiHeader.set_biCompression(WindowsDefs.BI_RGB);
054:
055: long screenDC = win32.GetDC(0);
056:
057: PointerPointer valuesPtr = bridge
058: .createPointerPointer(1, false);
059:
060: long hBMPColorMask = win32.CreateDIBSection(screenDC, bmi,
061: WindowsDefs.DIB_RGB_COLORS, valuesPtr, null, 0);
062:
063: // copy rgb-array from BufferedImage to values array
064: int[] rgb = bufImg.getRGB(0, 0, width, height, null, 0, width);
065: Int32Pointer values = bridge.createInt32Pointer(valuesPtr
066: .get(0));
067: values.set(rgb, 0, size);
068:
069: long hBMPAlphaMask = win32.CreateDIBSection(screenDC, bmi,
070: WindowsDefs.DIB_RGB_COLORS, valuesPtr, null, 0);
071:
072: win32.ReleaseDC(0, screenDC);
073:
074: // set bitmap mask
075: int[] maskArray = new int[size];
076:
077: for (int i = 0; i < size; i++) {
078: if ((rgb[i] & 0xFF000000) != 0) {
079: maskArray[i] = 0xFFFFFF;
080: } else {
081: maskArray[i] = 0;
082: }
083: }
084: values = bridge.createInt32Pointer(valuesPtr.get(0));
085: values.set(maskArray, 0, size);
086:
087: // fill icon info
088: Win32.ICONINFO iconInfo = win32.createICONINFO(false);
089: iconInfo.set_fIcon(icon ? 1 : 0);
090: if (!icon) {
091: iconInfo.set_xHotspot(xHotSpot);
092: iconInfo.set_yHotspot(yHotSpot);
093: }
094: iconInfo.set_hbmMask(hBMPAlphaMask);
095: iconInfo.set_hbmColor(hBMPColorMask);
096:
097: final long hIcon = win32.CreateIconIndirect(iconInfo);
098:
099: win32.DeleteObject(hBMPAlphaMask);
100: win32.DeleteObject(hBMPColorMask);
101:
102: return hIcon;
103: }
104: }
|