2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 1996, 2007, Oracle and/or its affiliates. All rights reserved.
|
2
|
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
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
24 |
*/
|
|
25 |
|
|
26 |
#include "awt_PopupMenu.h"
|
|
27 |
|
|
28 |
#include "awt_Event.h"
|
|
29 |
#include "awt_Window.h"
|
|
30 |
|
|
31 |
#include <sun_awt_windows_WPopupMenuPeer.h>
|
|
32 |
#include <java_awt_Event.h>
|
|
33 |
|
|
34 |
/* IMPORTANT! Read the README.JNI file for notes on JNI converted AWT code.
|
|
35 |
*/
|
|
36 |
|
|
37 |
/***********************************************************************/
|
|
38 |
// struct for _Show method
|
|
39 |
struct ShowStruct {
|
|
40 |
jobject self;
|
|
41 |
jobject event;
|
|
42 |
};
|
|
43 |
|
|
44 |
/************************************************************************
|
|
45 |
* AwtPopupMenu class methods
|
|
46 |
*/
|
|
47 |
|
|
48 |
AwtPopupMenu::AwtPopupMenu() {
|
|
49 |
m_parent = NULL;
|
|
50 |
}
|
|
51 |
|
|
52 |
AwtPopupMenu::~AwtPopupMenu()
|
|
53 |
{
|
|
54 |
}
|
|
55 |
|
|
56 |
void AwtPopupMenu::Dispose()
|
|
57 |
{
|
|
58 |
m_parent = NULL;
|
|
59 |
|
|
60 |
AwtMenu::Dispose();
|
|
61 |
}
|
|
62 |
|
|
63 |
LPCTSTR AwtPopupMenu::GetClassName() {
|
|
64 |
return TEXT("SunAwtPopupMenu");
|
|
65 |
}
|
|
66 |
|
|
67 |
/* Create a new AwtPopupMenu object and menu. */
|
|
68 |
AwtPopupMenu* AwtPopupMenu::Create(jobject self, AwtComponent* parent)
|
|
69 |
{
|
|
70 |
JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
|
|
71 |
|
|
72 |
jobject target = NULL;
|
|
73 |
AwtPopupMenu* popupMenu = NULL;
|
|
74 |
|
|
75 |
try {
|
|
76 |
if (env->EnsureLocalCapacity(1) < 0) {
|
|
77 |
return NULL;
|
|
78 |
}
|
|
79 |
|
|
80 |
target = env->GetObjectField(self, AwtObject::targetID);
|
|
81 |
JNI_CHECK_NULL_GOTO(target, "null target", done);
|
|
82 |
|
|
83 |
popupMenu = new AwtPopupMenu();
|
|
84 |
|
|
85 |
SetLastError(0);
|
|
86 |
HMENU hMenu = ::CreatePopupMenu();
|
|
87 |
// fix for 5088782
|
|
88 |
if (!CheckMenuCreation(env, self, hMenu))
|
|
89 |
{
|
|
90 |
env->DeleteLocalRef(target);
|
|
91 |
return NULL;
|
|
92 |
}
|
|
93 |
|
|
94 |
popupMenu->SetHMenu(hMenu);
|
|
95 |
|
|
96 |
popupMenu->LinkObjects(env, self);
|
|
97 |
popupMenu->SetParent(parent);
|
|
98 |
} catch (...) {
|
|
99 |
env->DeleteLocalRef(target);
|
|
100 |
throw;
|
|
101 |
}
|
|
102 |
|
|
103 |
done:
|
|
104 |
env->DeleteLocalRef(target);
|
|
105 |
return popupMenu;
|
|
106 |
}
|
|
107 |
|
|
108 |
void AwtPopupMenu::Show(JNIEnv *env, jobject event, BOOL isTrayIconPopup)
|
|
109 |
{
|
|
110 |
/*
|
|
111 |
* For not TrayIcon popup.
|
|
112 |
* Convert the event's XY to absolute coordinates. The XY is
|
|
113 |
* relative to the origin component, which is passed by PopupMenu
|
|
114 |
* as the event's target.
|
|
115 |
*/
|
|
116 |
if (env->EnsureLocalCapacity(2) < 0) {
|
|
117 |
env->DeleteGlobalRef(event);
|
|
118 |
return;
|
|
119 |
}
|
|
120 |
jobject origin = (env)->GetObjectField(event, AwtEvent::targetID);
|
|
121 |
jobject peerOrigin = GetPeerForTarget(env, origin);
|
|
122 |
PDATA pData;
|
|
123 |
JNI_CHECK_PEER_GOTO(peerOrigin, done);
|
|
124 |
{
|
|
125 |
AwtComponent* awtOrigin = (AwtComponent*)pData;
|
|
126 |
POINT pt;
|
|
127 |
UINT flags = 0;
|
|
128 |
pt.x = (env)->GetIntField(event, AwtEvent::xID);
|
|
129 |
pt.y = (env)->GetIntField(event, AwtEvent::yID);
|
|
130 |
|
|
131 |
if (!isTrayIconPopup) {
|
|
132 |
::MapWindowPoints(awtOrigin->GetHWnd(), 0, (LPPOINT)&pt, 1);
|
|
133 |
|
|
134 |
// Adjust to account for the Inset values
|
|
135 |
RECT rctInsets;
|
|
136 |
awtOrigin->GetInsets(&rctInsets);
|
|
137 |
pt.x -= rctInsets.left;
|
|
138 |
pt.y -= rctInsets.top;
|
|
139 |
|
|
140 |
flags = TPM_LEFTALIGN | TPM_RIGHTBUTTON;
|
|
141 |
|
|
142 |
} else {
|
|
143 |
::SetForegroundWindow(awtOrigin->GetHWnd());
|
|
144 |
|
|
145 |
flags = TPM_NONOTIFY | TPM_RIGHTALIGN | TPM_RIGHTBUTTON | TPM_BOTTOMALIGN;
|
|
146 |
}
|
|
147 |
|
|
148 |
/* Invoke the popup. */
|
|
149 |
::TrackPopupMenu(GetHMenu(), flags, pt.x, pt.y, 0, awtOrigin->GetHWnd(), NULL);
|
|
150 |
|
|
151 |
if (isTrayIconPopup) {
|
|
152 |
::PostMessage(awtOrigin->GetHWnd(), WM_NULL, 0, 0);
|
|
153 |
}
|
|
154 |
}
|
|
155 |
done:
|
|
156 |
env->DeleteLocalRef(origin);
|
|
157 |
env->DeleteLocalRef(peerOrigin);
|
|
158 |
env->DeleteGlobalRef(event);
|
|
159 |
}
|
|
160 |
|
|
161 |
void AwtPopupMenu::_Show(void *param)
|
|
162 |
{
|
|
163 |
JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
|
|
164 |
|
|
165 |
static jclass popupMenuCls;
|
|
166 |
if (popupMenuCls == NULL) {
|
|
167 |
jclass popupMenuClsLocal =
|
|
168 |
env->FindClass("java/awt/PopupMenu");
|
|
169 |
if (!popupMenuClsLocal) {
|
|
170 |
/* exception already thrown */
|
|
171 |
ShowStruct *ss = (ShowStruct*)param;
|
|
172 |
if (ss->self != NULL) {
|
|
173 |
env->DeleteGlobalRef(ss->self);
|
|
174 |
}
|
|
175 |
delete ss;
|
|
176 |
return;
|
|
177 |
}
|
|
178 |
popupMenuCls = (jclass)env->NewGlobalRef(popupMenuClsLocal);
|
|
179 |
env->DeleteLocalRef(popupMenuClsLocal);
|
|
180 |
}
|
|
181 |
|
|
182 |
static jfieldID isTrayIconPopupID;
|
|
183 |
if (isTrayIconPopupID == NULL) {
|
|
184 |
isTrayIconPopupID = env->GetFieldID(popupMenuCls, "isTrayIconPopup", "Z");
|
|
185 |
DASSERT(isTrayIconPopupID);
|
|
186 |
}
|
|
187 |
|
|
188 |
ShowStruct *ss = (ShowStruct*)param;
|
|
189 |
if (ss->self != NULL) {
|
|
190 |
PDATA pData = JNI_GET_PDATA(ss->self);
|
|
191 |
if (pData) {
|
|
192 |
AwtPopupMenu *p = (AwtPopupMenu *)pData;
|
|
193 |
jobject target = p->GetTarget(env);
|
|
194 |
BOOL isTrayIconPopup = env->GetBooleanField(target, isTrayIconPopupID);
|
|
195 |
env->DeleteLocalRef(target);
|
|
196 |
p->Show(env, ss->event, isTrayIconPopup);
|
|
197 |
}
|
|
198 |
env->DeleteGlobalRef(ss->self);
|
|
199 |
}
|
|
200 |
delete ss;
|
|
201 |
}
|
|
202 |
|
|
203 |
void AwtPopupMenu::AddItem(AwtMenuItem *item)
|
|
204 |
{
|
|
205 |
AwtMenu::AddItem(item);
|
|
206 |
if (GetMenuContainer() != NULL) return;
|
|
207 |
JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
|
|
208 |
if (env->EnsureLocalCapacity(1) < 0) {
|
|
209 |
return;
|
|
210 |
}
|
|
211 |
jobject target = GetTarget(env);
|
|
212 |
if (!(jboolean)env->GetBooleanField(target, AwtMenuItem::enabledID)) {
|
|
213 |
item->Enable(FALSE);
|
|
214 |
}
|
|
215 |
env->DeleteLocalRef(target);
|
|
216 |
}
|
|
217 |
|
|
218 |
void AwtPopupMenu::Enable(BOOL isEnabled)
|
|
219 |
{
|
|
220 |
AwtMenu *menu = GetMenuContainer();
|
|
221 |
if (menu != NULL) {
|
|
222 |
AwtMenu::Enable(isEnabled);
|
|
223 |
return;
|
|
224 |
}
|
|
225 |
JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
|
|
226 |
if (env->EnsureLocalCapacity(1) < 0) {
|
|
227 |
return;
|
|
228 |
}
|
|
229 |
jobject target = GetTarget(env);
|
|
230 |
int nCount = CountItem(target);
|
|
231 |
for (int i = 0; i < nCount; ++i) {
|
|
232 |
AwtMenuItem *item = GetItem(target,i);
|
|
233 |
jobject jitem = item->GetTarget(env);
|
|
234 |
BOOL bItemEnabled = isEnabled && (jboolean)env->GetBooleanField(jitem,
|
|
235 |
AwtMenuItem::enabledID);
|
1954
|
236 |
jstring labelStr = static_cast<jstring>(env->GetObjectField(jitem, AwtMenuItem::labelID));
|
|
237 |
LPCWSTR labelStrW = JNU_GetStringPlatformChars(env, labelStr, NULL);
|
|
238 |
if (labelStrW && wcscmp(labelStrW, L"-") != 0) {
|
2
|
239 |
item->Enable(bItemEnabled);
|
|
240 |
}
|
1954
|
241 |
JNU_ReleaseStringPlatformChars(env, labelStr, labelStrW);
|
|
242 |
env->DeleteLocalRef(labelStr);
|
2
|
243 |
env->DeleteLocalRef(jitem);
|
|
244 |
}
|
|
245 |
env->DeleteLocalRef(target);
|
|
246 |
}
|
|
247 |
|
|
248 |
BOOL AwtPopupMenu::IsDisabledAndPopup()
|
|
249 |
{
|
|
250 |
if (GetMenuContainer() != NULL) return FALSE;
|
|
251 |
JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
|
|
252 |
if (env->EnsureLocalCapacity(1) < 0) {
|
|
253 |
return FALSE;
|
|
254 |
}
|
|
255 |
jobject target = GetTarget(env);
|
|
256 |
BOOL bEnabled = (jboolean)env->GetBooleanField(target,
|
|
257 |
AwtMenuItem::enabledID);
|
|
258 |
env->DeleteLocalRef(target);
|
|
259 |
return !bEnabled;
|
|
260 |
}
|
|
261 |
|
|
262 |
/************************************************************************
|
|
263 |
* WPopupMenuPeer native methods
|
|
264 |
*/
|
|
265 |
|
|
266 |
extern "C" {
|
|
267 |
|
|
268 |
/*
|
|
269 |
* Class: sun_awt_windows_WPopupMenuPeer
|
|
270 |
* Method: createMenu
|
|
271 |
* Signature: (Lsun/awt/windows/WComponentPeer;)V
|
|
272 |
*/
|
|
273 |
JNIEXPORT void JNICALL
|
|
274 |
Java_sun_awt_windows_WPopupMenuPeer_createMenu(JNIEnv *env, jobject self,
|
|
275 |
jobject parent)
|
|
276 |
{
|
|
277 |
TRY;
|
|
278 |
|
|
279 |
PDATA pData;
|
|
280 |
JNI_CHECK_PEER_RETURN(parent);
|
|
281 |
AwtComponent* awtParent = (AwtComponent *)pData;
|
|
282 |
AwtToolkit::CreateComponent(
|
|
283 |
self, awtParent, (AwtToolkit::ComponentFactory)AwtPopupMenu::Create, FALSE);
|
|
284 |
JNI_CHECK_PEER_CREATION_RETURN(self);
|
|
285 |
|
|
286 |
CATCH_BAD_ALLOC;
|
|
287 |
}
|
|
288 |
|
|
289 |
/*
|
|
290 |
* Class: sun_awt_windows_WPopupMenuPeer
|
|
291 |
* Method: _show
|
|
292 |
* Signature: (Ljava/awt/Event;)V
|
|
293 |
*/
|
|
294 |
JNIEXPORT void JNICALL
|
|
295 |
Java_sun_awt_windows_WPopupMenuPeer__1show(JNIEnv *env, jobject self,
|
|
296 |
jobject event)
|
|
297 |
{
|
|
298 |
TRY;
|
|
299 |
|
|
300 |
ShowStruct *ss = new ShowStruct;
|
|
301 |
ss->self = env->NewGlobalRef(self);
|
|
302 |
ss->event = env->NewGlobalRef(event);
|
|
303 |
|
|
304 |
// fix for 6268046: invoke the function without CriticalSection's synchronization
|
|
305 |
AwtToolkit::GetInstance().InvokeFunction(AwtPopupMenu::_Show, ss);
|
|
306 |
// global ref is deleted in _Show() and ss is deleted in Show()
|
|
307 |
|
|
308 |
CATCH_BAD_ALLOC;
|
|
309 |
}
|
|
310 |
|
|
311 |
} /* extern "C" */
|