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 
  26 #include "jni_util.h"
  27 #include "awt.h"
  28 #include <jni.h>
  29 #include "awt_Taskbar.h"
  30 #include "awt_Window.h"
  31 
  32 
  33 #ifdef __cplusplus
  34 extern "C" {
  35 #endif
  36 
  37 /*
  38  * Class:     sun_awt_windows_WTaskbarPeer
  39  * Method:    nativeInit
  40  * Signature: ()Z
  41  */
  42 JNIEXPORT jboolean JNICALL Java_sun_awt_windows_WTaskbarPeer_nativeInit
  43   (JNIEnv *env, jclass)
  44 {
  45     if (SUCCEEDED(::CoCreateInstance(CLSID_TaskbarList, NULL,
  46             CLSCTX_INPROC_SERVER, IID_ITaskbarList, (LPVOID *)&m_Taskbar))) {
  47         return JNI_TRUE;
  48     } else {
  49         return JNI_FALSE;
  50     }
  51 }
  52 
  53 /*
  54  * Class:     sun_awt_windows_WTaskbarPeer
  55  * Method:    setProgressValue
  56  * Signature: (JI)V
  57  */
  58 JNIEXPORT void JNICALL Java_sun_awt_windows_WTaskbarPeer_setProgressValue
  59   (JNIEnv *, jobject, jlong window, jint value)
  60 {
  61     m_Taskbar->SetProgressValue((HWND)window, value, 100);
  62 }
  63 
  64 
  65 
  66 /*
  67  * Class:     sun_awt_windows_WTaskbarPeer
  68  * Method:    setProgressState
  69  * Signature: (JI)V
  70  */
  71 JNIEXPORT void JNICALL Java_sun_awt_windows_WTaskbarPeer_setProgressState
  72   (JNIEnv *env, jobject, jlong window, jobject state)
  73 {
  74     TBPFLAG flag = TBPF_NOPROGRESS;
  75 
  76     static jmethodID nameMID = NULL;
  77     if (!nameMID) {
  78         jclass stateCls = env->FindClass("java/awt/Taskbar$State");
  79         CHECK_NULL(stateCls);
  80         nameMID = env->GetMethodID(stateCls, "name", "()Ljava/lang/String;");
  81         CHECK_NULL(nameMID);
  82     }
  83     jstring value = (jstring) env->CallObjectMethod(state, nameMID);
  84     CHECK_NULL(value);
  85     const char* valueNative = env->GetStringUTFChars(value, 0);
  86     if (valueNative) {
  87         if (strcmp(valueNative, "OFF") == 0) {
  88             flag = TBPF_NOPROGRESS;
  89         } else if (strcmp(valueNative, "NORMAL") == 0) {
  90             flag = TBPF_NORMAL;
  91         } else if (strcmp(valueNative, "PAUSED") == 0) {
  92             flag = TBPF_PAUSED;
  93         } else if (strcmp(valueNative, "INDETERMINATE") == 0) {
  94             flag = TBPF_INDETERMINATE;
  95         } else if (strcmp(valueNative, "ERROR") == 0) {
  96             flag = TBPF_ERROR;
  97         }
  98         env->ReleaseStringUTFChars(value, valueNative);
  99         m_Taskbar->SetProgressState((HWND)window, flag);
 100     }
 101 }
 102 
 103 /*
 104  * Class:     sun_awt_windows_WTaskbarPeer
 105  * Method:    flashWindow
 106  * Signature: (JZ)V
 107  */
 108 JNIEXPORT void JNICALL Java_sun_awt_windows_WTaskbarPeer_flashWindow
 109   (JNIEnv *, jobject, jlong window)
 110 {
 111     ::FlashWindow((HWND) window, TRUE);
 112 }
 113 
 114 /*
 115  * Class:     sun_awt_windows_WTaskbarPeer
 116  * Method:    setOverlayIcon
 117  * Signature: (J[III)V
 118  */
 119 JNIEXPORT void JNICALL Java_sun_awt_windows_WTaskbarPeer_setOverlayIcon
 120   (JNIEnv *env, jobject, jlong window, jintArray buf, jint w, jint h)
 121 {
 122     HICON icon = CreateIconFromRaster(env, buf, w, h);
 123     m_Taskbar->SetOverlayIcon((HWND)window, icon, NULL);
 124     ::DestroyIcon(icon);
 125 }
 126 #ifdef __cplusplus
 127 }
 128 #endif