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  */
Java_sun_awt_windows_WTaskbarPeer_nativeInit(JNIEnv * env,jclass)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  */
Java_sun_awt_windows_WTaskbarPeer_setProgressValue(JNIEnv *,jobject,jlong window,jint value)58 JNIEXPORT void JNICALL Java_sun_awt_windows_WTaskbarPeer_setProgressValue
59   (JNIEnv *, jobject, jlong window, jint value)
60 {
61     if (value < 0 || value > 100) {
62         m_Taskbar->SetProgressState((HWND)window, TBPF_NOPROGRESS);
63     } else {
64         m_Taskbar->SetProgressValue((HWND)window, value, 100);
65     }
66 }
67 
68 
69 
70 /*
71  * Class:     sun_awt_windows_WTaskbarPeer
72  * Method:    setProgressState
73  * Signature: (JI)V
74  */
Java_sun_awt_windows_WTaskbarPeer_setProgressState(JNIEnv * env,jobject,jlong window,jobject state)75 JNIEXPORT void JNICALL Java_sun_awt_windows_WTaskbarPeer_setProgressState
76   (JNIEnv *env, jobject, jlong window, jobject state)
77 {
78     TBPFLAG flag = TBPF_NOPROGRESS;
79 
80     static jmethodID nameMID = NULL;
81     if (!nameMID) {
82         jclass stateCls = env->FindClass("java/awt/Taskbar$State");
83         CHECK_NULL(stateCls);
84         nameMID = env->GetMethodID(stateCls, "name", "()Ljava/lang/String;");
85         CHECK_NULL(nameMID);
86     }
87     jstring value = (jstring) env->CallObjectMethod(state, nameMID);
88     CHECK_NULL(value);
89     const char* valueNative = env->GetStringUTFChars(value, 0);
90     if (valueNative) {
91         if (strcmp(valueNative, "OFF") == 0) {
92             flag = TBPF_NOPROGRESS;
93         } else if (strcmp(valueNative, "NORMAL") == 0) {
94             flag = TBPF_NORMAL;
95 
96             // Switching from TBPF_INDETERMINATE to TBPF_NORMAL has no effect
97             m_Taskbar->SetProgressState((HWND)window, TBPF_PAUSED);
98         } else if (strcmp(valueNative, "PAUSED") == 0) {
99             flag = TBPF_PAUSED;
100         } else if (strcmp(valueNative, "INDETERMINATE") == 0) {
101             flag = TBPF_INDETERMINATE;
102         } else if (strcmp(valueNative, "ERROR") == 0) {
103             flag = TBPF_ERROR;
104         }
105         env->ReleaseStringUTFChars(value, valueNative);
106         m_Taskbar->SetProgressState((HWND)window, flag);
107     }
108 }
109 
110 /*
111  * Class:     sun_awt_windows_WTaskbarPeer
112  * Method:    flashWindow
113  * Signature: (JZ)V
114  */
Java_sun_awt_windows_WTaskbarPeer_flashWindow(JNIEnv *,jobject,jlong window)115 JNIEXPORT void JNICALL Java_sun_awt_windows_WTaskbarPeer_flashWindow
116   (JNIEnv *, jobject, jlong window)
117 {
118     ::FlashWindow((HWND) window, TRUE);
119 }
120 
121 /*
122  * Class:     sun_awt_windows_WTaskbarPeer
123  * Method:    setOverlayIcon
124  * Signature: (J[III)V
125  */
Java_sun_awt_windows_WTaskbarPeer_setOverlayIcon(JNIEnv * env,jobject,jlong window,jintArray buf,jint w,jint h)126 JNIEXPORT void JNICALL Java_sun_awt_windows_WTaskbarPeer_setOverlayIcon
127   (JNIEnv *env, jobject, jlong window, jintArray buf, jint w, jint h)
128 {
129     HICON icon = CreateIconFromRaster(env, buf, w, h);
130     m_Taskbar->SetOverlayIcon((HWND)window, icon, NULL);
131     ::DestroyIcon(icon);
132 }
133 #ifdef __cplusplus
134 }
135 #endif
136