1 /*
   2  * Copyright (c) 2015, 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 package sun.awt.windows;
  26 
  27 import java.awt.AlphaComposite;
  28 import java.awt.Graphics2D;
  29 import java.awt.Image;
  30 import java.awt.Taskbar.Feature;
  31 import java.awt.TaskbarPeer;
  32 import java.awt.Window;
  33 import java.awt.image.BufferedImage;
  34 import java.awt.image.DataBufferInt;
  35 import sun.awt.AWTAccessor;
  36 import sun.awt.OSInfo;
  37 
  38 final public class WTaskbarPeer implements TaskbarPeer {
  39 
  40     private static boolean supported = false;
  41     private static boolean initExecuted = false;
  42 
  43     private static synchronized void init() {
  44         if (!initExecuted) {
  45             supported = OSInfo.getWindowsVersion()
  46                             .compareTo(OSInfo.WINDOWS_7) >= 0
  47                             && nativeInit();
  48         }
  49         initExecuted = true;
  50     }
  51 
  52     static boolean isTaskbarSupported() {
  53         init();
  54         return supported;
  55     }
  56 
  57     WTaskbarPeer() {
  58         init();
  59     }
  60 
  61     @Override
  62     public boolean isSupported(Feature feature) {
  63         switch(feature) {
  64             case ICON_BADGE_IMAGE_WINDOW:
  65             case PROGRESS_STATE_WINDOW:
  66             case PROGRESS_VALUE_WINDOW:
  67                 return supported;
  68             case USER_ATTENTION_WINDOW:
  69                 return true;
  70             default:
  71                 return false;
  72         }
  73     }
  74 
  75     private static int[] imageToArray(Image image) {
  76         if (image == null) {
  77             return null;
  78         }
  79 
  80         int w = image.getWidth(null);
  81         int h = image.getHeight(null);
  82 
  83         if (w < 0 || h < 0) {
  84             return null;
  85         }
  86 
  87         BufferedImage bimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
  88         Graphics2D g2 = bimg.createGraphics();
  89         g2.setComposite(AlphaComposite.Src);
  90         g2.drawImage(image, 0, 0, null);
  91         g2.dispose();
  92 
  93         return ((DataBufferInt) bimg.getRaster().getDataBuffer()).getData();
  94     }
  95 
  96     @Override
  97     public void setWindowIconBadge(Window window, final Image image) {
  98         WWindowPeer wp = AWTAccessor.getComponentAccessor().getPeer(window);
  99         if (wp != null) {
 100             int w = 0;
 101             int h = 0;
 102             int[] buffer = imageToArray(image);
 103             if (buffer != null) {
 104                 w = image.getWidth(null);
 105                 h = image.getHeight(null);
 106             }
 107             setOverlayIcon(wp.getHWnd(), buffer, w, h);
 108         }
 109     }
 110 
 111     @Override
 112     public void requestWindowUserAttention(Window window) {
 113         WWindowPeer wp = AWTAccessor.getComponentAccessor().getPeer(window);
 114         if (wp != null) {
 115             flashWindow(wp.getHWnd());
 116         }
 117     }
 118 
 119     @Override
 120     public void setWindowProgressValue(Window window, int value) {
 121         WWindowPeer wp = AWTAccessor.getComponentAccessor().getPeer(window);
 122         if (wp != null) {
 123             setProgressValue(wp.getHWnd(), value);
 124         }
 125     }
 126 
 127     @Override
 128     public void setWindowProgressState(Window window, int state) {
 129         WWindowPeer wp = AWTAccessor.getComponentAccessor().getPeer(window);
 130         if (wp != null) {
 131             setProgressState(wp.getHWnd(), state);
 132         }
 133     }
 134 
 135     private static native boolean nativeInit();
 136 
 137     private native void setProgressValue(long hwnd, int value);
 138 
 139     private native void setProgressState(long hwnd, int state);
 140 
 141     private native void setOverlayIcon(long hwnd, int[] badge, int width, int height);
 142 
 143     private native void flashWindow(long hWnd);
 144 
 145 }