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