author | erikj |
Thu, 07 Jun 2012 18:05:09 -0700 | |
changeset 12813 | c10ab96dcf41 |
parent 7668 | d4a77089c587 |
child 18232 | b538b71fb429 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2000, 2010, 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.h" |
|
27 |
#include "awt_DataTransferer.h" |
|
28 |
#include "awt_DnDDT.h" |
|
29 |
#include "awt_TextComponent.h" |
|
30 |
#include <shlobj.h> |
|
1954 | 31 |
#include <shellapi.h> |
2 | 32 |
#include <sun_awt_windows_WDataTransferer.h> |
33 |
||
34 |
#include "locale_str.h" |
|
35 |
||
36 |
#define GALLOCFLG (GMEM_DDESHARE | GMEM_MOVEABLE | GMEM_ZEROINIT) |
|
37 |
#define WIN_TO_JAVA_PIXEL(r, g, b) (0xFF000000 | (r) << 16 | (g) << 8 | (b) << 0) |
|
38 |
||
39 |
DECLARE_JAVA_CLASS(dataTransfererClazz, "sun/awt/datatransfer/DataTransferer"); |
|
40 |
||
41 |
jobject |
|
42 |
AwtDataTransferer::GetDataTransferer(JNIEnv* env) { |
|
43 |
DECLARE_STATIC_OBJECT_JAVA_METHOD(getInstanceMethodID, dataTransfererClazz, |
|
44 |
"getInstance", |
|
45 |
"()Lsun/awt/datatransfer/DataTransferer;"); |
|
46 |
return env->CallStaticObjectMethod(clazz, getInstanceMethodID); |
|
47 |
} |
|
48 |
||
49 |
jbyteArray |
|
50 |
AwtDataTransferer::ConvertData(JNIEnv* env, jobject source, jobject contents, |
|
51 |
jlong format, jobject formatMap) { |
|
52 |
jobject transferer = GetDataTransferer(env); |
|
53 |
||
54 |
if (!JNU_IsNull(env, transferer)) { |
|
55 |
jbyteArray ret = NULL; |
|
56 |
DECLARE_OBJECT_JAVA_METHOD(convertDataMethodID, dataTransfererClazz, |
|
57 |
"convertData", |
|
58 |
"(Ljava/lang/Object;Ljava/awt/datatransfer/Transferable;JLjava/util/Map;Z)[B"); |
|
59 |
||
60 |
ret = (jbyteArray)env->CallObjectMethod(transferer, convertDataMethodID, |
|
61 |
source, contents, format, |
|
62 |
formatMap, AwtToolkit::IsMainThread()); |
|
63 |
||
64 |
if (!JNU_IsNull(env, safe_ExceptionOccurred(env))) { |
|
65 |
env->ExceptionDescribe(); |
|
66 |
env->ExceptionClear(); |
|
67 |
} |
|
68 |
||
69 |
env->DeleteLocalRef(transferer); |
|
70 |
||
71 |
return ret; |
|
72 |
} else { |
|
73 |
return NULL; |
|
74 |
} |
|
75 |
} |
|
76 |
||
77 |
jobject |
|
78 |
AwtDataTransferer::ConcatData(JNIEnv* env, jobject obj1, jobject obj2) { |
|
79 |
jobject transferer = GetDataTransferer(env); |
|
80 |
||
81 |
if (!JNU_IsNull(env, transferer)) { |
|
82 |
jobject ret = NULL; |
|
83 |
DECLARE_OBJECT_JAVA_METHOD(concatDataMethodID, dataTransfererClazz, |
|
84 |
"concatData", |
|
85 |
"(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); |
|
86 |
||
87 |
ret = env->CallObjectMethod(transferer, concatDataMethodID, obj1, obj2); |
|
88 |
||
89 |
if (!JNU_IsNull(env, safe_ExceptionOccurred(env))) { |
|
90 |
env->ExceptionDescribe(); |
|
91 |
env->ExceptionClear(); |
|
92 |
} |
|
93 |
||
94 |
env->DeleteLocalRef(transferer); |
|
95 |
||
96 |
return ret; |
|
97 |
} else { |
|
98 |
return NULL; |
|
99 |
} |
|
100 |
} |
|
101 |
||
102 |
/** |
|
103 |
* This routine retrieves palette entries from enhanced metafile or |
|
104 |
* a logical color palette, builds appropriate LOGPALETTE structure, |
|
105 |
* writes it into a created Java byte array and returns a local |
|
106 |
* reference to the array. |
|
107 |
* This routine is used for image data transfer. |
|
108 |
* |
|
109 |
* @param hGdiObj - a handle to the GDI object to retrieve palette entries from, |
|
110 |
* it can be a handle to either a logical color palette (OBJ_PAL type) |
|
111 |
* or an enhanced metafile (OBJ_ENHMETAFILE). If it is neither of these |
|
112 |
* types the routine fails(see bFailSafe). |
|
113 |
* @param dwGdiObjType - a type of the passed GDI object. It should be specified |
|
114 |
* if the type of the passed GDI object is known to the caller. Otherwise |
|
115 |
* pass 0. |
|
116 |
* @param bFailSafe - if FALSE, the routine will return NULL in case of failure, |
|
117 |
* otherwise it will return an array with empty LOGPALETTE structure |
|
118 |
* in case of failure. |
|
119 |
* @return a local reference to Java byte array which contains LOGPALETTE |
|
120 |
* structure which defines a logical color palette or a palette of |
|
121 |
* an enhanced metafile. |
|
122 |
*/ |
|
123 |
jbyteArray |
|
124 |
AwtDataTransferer::GetPaletteBytes(HGDIOBJ hGdiObj, DWORD dwGdiObjType, |
|
125 |
BOOL bFailSafe) { |
|
126 |
||
127 |
if (hGdiObj == NULL) { |
|
128 |
dwGdiObjType = 0; |
|
129 |
} else if (dwGdiObjType == 0) { |
|
130 |
dwGdiObjType = ::GetObjectType(hGdiObj); |
|
131 |
} else { |
|
132 |
DASSERT(::GetObjectType(hGdiObj) == dwGdiObjType); |
|
133 |
} |
|
134 |
||
135 |
if (!bFailSafe && dwGdiObjType == 0) { |
|
136 |
return NULL; |
|
137 |
} |
|
138 |
||
139 |
UINT nEntries = 0; |
|
140 |
||
141 |
switch (dwGdiObjType) { |
|
142 |
case OBJ_PAL: |
|
143 |
nEntries = |
|
144 |
::GetPaletteEntries((HPALETTE)hGdiObj, 0, 0, NULL); |
|
145 |
break; |
|
146 |
case OBJ_ENHMETAFILE: |
|
147 |
nEntries = |
|
148 |
::GetEnhMetaFilePaletteEntries((HENHMETAFILE)hGdiObj, 0, NULL); |
|
149 |
break; |
|
150 |
} |
|
151 |
||
152 |
if (!bFailSafe && (nEntries == 0 || nEntries == GDI_ERROR)) { |
|
153 |
return NULL; |
|
154 |
} |
|
155 |
||
156 |
JNIEnv* env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); |
|
157 |
jsize size = sizeof(LOGPALETTE) + nEntries * sizeof(PALETTEENTRY); |
|
158 |
||
159 |
jbyteArray paletteBytes = env->NewByteArray(size); |
|
160 |
if (JNU_IsNull(env, paletteBytes)) { |
|
161 |
throw std::bad_alloc(); |
|
162 |
} |
|
163 |
||
164 |
LOGPALETTE* pLogPalette = |
|
165 |
(LOGPALETTE*)env->GetPrimitiveArrayCritical(paletteBytes, NULL); |
|
166 |
PALETTEENTRY* pPalEntries = (PALETTEENTRY*)pLogPalette->palPalEntry; |
|
167 |
||
168 |
pLogPalette->palVersion = 0x300; |
|
169 |
pLogPalette->palNumEntries = nEntries; |
|
170 |
||
171 |
switch (dwGdiObjType) { |
|
172 |
case OBJ_PAL: |
|
173 |
VERIFY(::GetPaletteEntries((HPALETTE)hGdiObj, 0, nEntries, |
|
174 |
pPalEntries) == nEntries); |
|
175 |
break; |
|
176 |
case OBJ_ENHMETAFILE: |
|
177 |
VERIFY(::GetEnhMetaFilePaletteEntries((HENHMETAFILE)hGdiObj, nEntries, |
|
178 |
pPalEntries) == nEntries); |
|
179 |
break; |
|
180 |
} |
|
181 |
||
182 |
env->ReleasePrimitiveArrayCritical(paletteBytes, pLogPalette, 0); |
|
183 |
||
184 |
return paletteBytes; |
|
185 |
} |
|
186 |
||
187 |
jbyteArray |
|
188 |
AwtDataTransferer::LCIDToTextEncoding(JNIEnv *env, LCID lcid) { |
|
189 |
LANGID langID = LANGIDFROMLCID(lcid); |
|
190 |
const char *encoding = getEncodingFromLangID(langID); |
|
191 |
||
192 |
// Warning C4244. |
|
193 |
// Cast SIZE_T (__int64 on 64-bit/unsigned int on 32-bit) |
|
194 |
// to jsize (long). |
|
195 |
// We assume that the encoding name length cannot exceed INT_MAX. |
|
196 |
jsize length = (jsize)strlen(encoding); |
|
197 |
||
198 |
jbyteArray retval = env->NewByteArray(length); |
|
199 |
if (retval == NULL) { |
|
200 |
throw std::bad_alloc(); |
|
201 |
} |
|
202 |
env->SetByteArrayRegion(retval, 0, length, (jbyte *)encoding); |
|
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
203 |
free((void *)encoding); |
2 | 204 |
return retval; |
205 |
} |
|
206 |
||
207 |
static VOID CALLBACK |
|
208 |
IdleFunc() { |
|
209 |
/* |
|
210 |
* Fix for 4485987 and 4669873. |
|
211 |
* If IdleFunc is a noop, the secondary message pump occasionally occupies |
|
212 |
* all processor time and causes drag freezes. GetQueueStatus is needed to |
|
213 |
* mark all messages that are currently in the queue as old, otherwise |
|
214 |
* WaitMessage will return immediatelly as we selectively get messages from |
|
215 |
* the queue. |
|
216 |
*/ |
|
217 |
::WaitMessage(); |
|
218 |
::GetQueueStatus(QS_ALLINPUT); |
|
219 |
} |
|
220 |
||
221 |
static BOOL CALLBACK |
|
222 |
PeekMessageFunc(MSG& msg) { |
|
223 |
return ::PeekMessage(&msg, NULL, WM_QUIT, WM_QUIT, PM_REMOVE) || |
|
224 |
::PeekMessage(&msg, NULL, WM_AWT_INVOKE_METHOD, WM_AWT_INVOKE_METHOD, PM_REMOVE) || |
|
225 |
::PeekMessage(&msg, NULL, WM_PAINT, WM_PAINT, PM_REMOVE); |
|
226 |
} |
|
227 |
||
228 |
void |
|
229 |
AwtDataTransferer::SecondaryMessageLoop() { |
|
230 |
DASSERT(AwtToolkit::MainThread() == ::GetCurrentThreadId()); |
|
231 |
||
232 |
AwtToolkit::GetInstance().MessageLoop(IdleFunc, |
|
233 |
PeekMessageFunc); |
|
234 |
} |
|
235 |
||
236 |
extern "C" { |
|
237 |
||
238 |
/* |
|
239 |
* Class: sun_awt_datatransfer_DataTransferer |
|
240 |
* Method: draqQueryFile |
|
241 |
* Signature: ([B)[Ljava/lang/String; |
|
242 |
*/ |
|
243 |
JNIEXPORT jobjectArray JNICALL |
|
244 |
Java_sun_awt_windows_WDataTransferer_dragQueryFile |
|
245 |
(JNIEnv *env, jobject obj, jbyteArray bytes) |
|
246 |
{ |
|
247 |
TRY; |
|
248 |
||
249 |
/* |
|
250 |
* Fix for the BugTraq ID 4327064 - inter-jvm DnD crashes the droping jvm. |
|
251 |
* On Win9X DragQueryFile() doesn't accept a pointer to the local help as the first |
|
252 |
* argument, so we should dump the bits into global memory. |
|
253 |
*/ |
|
254 |
UINT size = env->GetArrayLength(bytes); |
|
255 |
HGLOBAL hglobal = NULL; |
|
256 |
jbyte *bBytes = NULL; |
|
257 |
HDROP hdrop = NULL; |
|
258 |
LPTSTR buffer = NULL; |
|
259 |
||
260 |
hglobal = ::GlobalAlloc(GALLOCFLG, size); |
|
261 |
||
262 |
if (hglobal == NULL) { |
|
263 |
throw std::bad_alloc(); |
|
264 |
} |
|
265 |
||
266 |
try { |
|
267 |
||
268 |
bBytes = (jbyte*)::GlobalLock(hglobal); |
|
269 |
env->GetByteArrayRegion(bytes, 0, size, bBytes); |
|
270 |
||
271 |
hdrop = (HDROP)bBytes; |
|
272 |
||
1954 | 273 |
UINT nFilenames = ::DragQueryFile(hdrop, 0xFFFFFFFF, NULL, 0); |
2 | 274 |
|
275 |
jclass str_clazz = env->FindClass("java/lang/String"); |
|
276 |
DASSERT(str_clazz != NULL); |
|
277 |
jobjectArray filenames = env->NewObjectArray(nFilenames, str_clazz, |
|
278 |
NULL); |
|
279 |
if (filenames == NULL) { |
|
280 |
throw std::bad_alloc(); |
|
281 |
} |
|
282 |
||
283 |
UINT bufsize = 512; // in characters, not in bytes |
|
284 |
buffer = (LPTSTR)safe_Malloc(bufsize*sizeof(TCHAR)); |
|
285 |
||
286 |
for (UINT i = 0; i < nFilenames; i++) { |
|
1954 | 287 |
UINT size = ::DragQueryFile(hdrop, i, NULL, 0); |
2 | 288 |
if (size > bufsize) { |
289 |
bufsize = size; |
|
290 |
buffer = (LPTSTR)safe_Realloc(buffer, bufsize*sizeof(TCHAR)); |
|
291 |
} |
|
1954 | 292 |
::DragQueryFile(hdrop, i, buffer, bufsize); |
2 | 293 |
|
294 |
jstring name = JNU_NewStringPlatform(env, buffer); |
|
295 |
if (name == NULL) { |
|
296 |
throw std::bad_alloc(); |
|
297 |
} |
|
298 |
||
299 |
env->SetObjectArrayElement(filenames, i, name); |
|
300 |
} |
|
301 |
||
302 |
free(buffer); |
|
303 |
::GlobalUnlock(hglobal); |
|
304 |
::GlobalFree(hglobal); |
|
305 |
return filenames; |
|
306 |
||
307 |
} catch (std::bad_alloc&) { |
|
308 |
free(buffer); |
|
309 |
::GlobalUnlock(hglobal); |
|
310 |
::GlobalFree(hglobal); |
|
311 |
throw; |
|
312 |
} |
|
313 |
||
314 |
CATCH_BAD_ALLOC_RET(NULL); |
|
315 |
} |
|
316 |
||
317 |
/* |
|
318 |
* Class: sun_awt_windows_WDataTransferer |
|
319 |
* Method: platformImageBytesToImageData |
|
320 |
* Signature: ([BI)[I |
|
321 |
*/ |
|
322 |
JNIEXPORT jintArray JNICALL |
|
323 |
Java_sun_awt_windows_WDataTransferer_platformImageBytesToImageData( |
|
324 |
JNIEnv *env, jobject self, jbyteArray bytes, jlong format) { |
|
325 |
||
326 |
TRY; |
|
327 |
||
328 |
HDC hdc = NULL; |
|
329 |
||
330 |
LOGPALETTE* pLogPalette = NULL; |
|
331 |
WORD uPaletteEntries = 0; |
|
332 |
SIZE_T uOffset = 0; |
|
333 |
HPALETTE hPalette = NULL; |
|
334 |
HPALETTE hOldPalette = NULL; |
|
335 |
||
336 |
BITMAPINFO* pSrcBmi = NULL; |
|
337 |
BITMAPINFOHEADER* pSrcBmih = NULL; |
|
338 |
LPVOID pSrcBits = NULL; |
|
339 |
BITMAPINFO* pDstBmi = NULL; |
|
340 |
BITMAPINFOHEADER* pDstBmih = NULL; |
|
341 |
LPVOID pDstBits = NULL; |
|
342 |
||
343 |
LPBYTE lpEnhMetaFileBits = NULL; |
|
344 |
HENHMETAFILE hEnhMetaFile = NULL; |
|
345 |
||
346 |
HBITMAP hDibSection = NULL; |
|
347 |
HBITMAP hOldBitmap = NULL; |
|
348 |
jintArray buffer = NULL; |
|
349 |
LONG width = 0; |
|
350 |
LONG height = 0; |
|
351 |
int numPixels = 0; |
|
352 |
||
353 |
if (JNU_IsNull(env, bytes)) { |
|
354 |
return NULL; |
|
355 |
} |
|
356 |
||
357 |
jsize size = env->GetArrayLength(bytes); |
|
358 |
if (size == 0) { |
|
359 |
return NULL; |
|
360 |
} |
|
361 |
||
362 |
jbyte* bBytes = (jbyte*)safe_Malloc(size * sizeof(jbyte)); |
|
363 |
||
364 |
try { |
|
365 |
||
366 |
env->GetByteArrayRegion(bytes, 0, size, bBytes); |
|
367 |
||
368 |
pLogPalette = (LOGPALETTE*)bBytes; |
|
369 |
uPaletteEntries = pLogPalette->palNumEntries; |
|
370 |
uOffset = sizeof(LOGPALETTE) + uPaletteEntries * sizeof(PALETTEENTRY); |
|
371 |
DASSERT(uOffset < (SIZE_T)size); |
|
372 |
||
373 |
if (uPaletteEntries == 0) { |
|
374 |
pLogPalette = NULL; |
|
375 |
} |
|
376 |
||
377 |
hdc = ::CreateCompatibleDC(NULL); |
|
378 |
if (hdc == NULL) { |
|
379 |
free(bBytes); |
|
380 |
return NULL; |
|
381 |
} |
|
382 |
||
383 |
switch (format) { |
|
384 |
case CF_DIB: |
|
385 |
||
386 |
pSrcBmi = (BITMAPINFO*)((LPSTR)bBytes + uOffset); |
|
387 |
pSrcBmih = &pSrcBmi->bmiHeader; |
|
388 |
||
389 |
width = pSrcBmih->biWidth; |
|
390 |
height = abs(pSrcBmih->biHeight); |
|
391 |
||
392 |
{ |
|
393 |
DWORD nColorEntries = 0; |
|
394 |
||
395 |
switch (pSrcBmih->biBitCount) { |
|
396 |
case 0: nColorEntries = 0; break; |
|
397 |
case 1: nColorEntries = 2; break; |
|
398 |
case 4: |
|
399 |
case 8: |
|
400 |
nColorEntries = (pSrcBmih->biClrUsed != 0) ? |
|
1976
91e064d8ddb9
6551075: screenshot image taken through clipboard on W2K terminal server is shifted
bae
parents:
1954
diff
changeset
|
401 |
pSrcBmih->biClrUsed : (1 << pSrcBmih->biBitCount); |
2 | 402 |
break; |
403 |
case 16: |
|
404 |
case 24: |
|
405 |
case 32: |
|
406 |
nColorEntries = pSrcBmih->biClrUsed; |
|
407 |
// If biBitCount is 16 or 32 and biCompression is |
|
408 |
// BI_BITFIELDS the color table will be prefixed with |
|
409 |
// three DWORD color masks. |
|
410 |
if (pSrcBmih->biCompression == BI_BITFIELDS && |
|
411 |
(pSrcBmih->biBitCount == 16 || |
|
412 |
pSrcBmih->biBitCount == 32)) { |
|
413 |
nColorEntries += 3; |
|
414 |
} |
|
415 |
break; |
|
416 |
default: |
|
417 |
// The header is probably corrupted. |
|
418 |
// Fail immediatelly to avoid memory access violation. |
|
419 |
free(bBytes); |
|
420 |
::DeleteDC(hdc); |
|
421 |
return NULL; |
|
422 |
} |
|
423 |
||
424 |
pSrcBits = (LPSTR)pSrcBmi + pSrcBmih->biSize |
|
425 |
+ nColorEntries * sizeof(RGBQUAD); |
|
426 |
} |
|
427 |
break; |
|
428 |
case CF_ENHMETAFILE: |
|
429 |
case CF_METAFILEPICT: |
|
430 |
lpEnhMetaFileBits = (BYTE*)bBytes + uOffset; |
|
431 |
// Warning C4244. size is jsize, uOffset is SIZE_T. |
|
432 |
// We assert that size > uOffset, so it is safe to cast to jsize. |
|
433 |
hEnhMetaFile = ::SetEnhMetaFileBits(size - (jsize)uOffset, |
|
434 |
lpEnhMetaFileBits); |
|
435 |
DASSERT(hEnhMetaFile != NULL); |
|
436 |
||
437 |
{ |
|
438 |
UINT uHeaderSize = |
|
439 |
::GetEnhMetaFileHeader(hEnhMetaFile, 0, NULL); |
|
440 |
DASSERT(uHeaderSize != 0); |
|
441 |
ENHMETAHEADER* lpemh = (ENHMETAHEADER*)safe_Malloc(uHeaderSize); |
|
442 |
VERIFY(::GetEnhMetaFileHeader(hEnhMetaFile, uHeaderSize, |
|
443 |
lpemh) == uHeaderSize); |
|
444 |
LPRECTL lpFrame = &lpemh->rclFrame; |
|
445 |
POINT p = { abs(lpFrame->right - lpFrame->left), |
|
446 |
abs(lpFrame->bottom - lpFrame->top) }; |
|
447 |
VERIFY(::SaveDC(hdc)); |
|
448 |
VERIFY(::SetMapMode(hdc, MM_HIMETRIC)); |
|
449 |
VERIFY(::LPtoDP(hdc, &p, 1)); |
|
450 |
VERIFY(::RestoreDC(hdc, -1)); |
|
451 |
width = p.x; |
|
452 |
height = -p.y; |
|
453 |
||
454 |
free(lpemh); |
|
455 |
} |
|
456 |
break; |
|
457 |
default: |
|
458 |
DASSERT(FALSE); // Other formats are not supported yet. |
|
459 |
free(bBytes); |
|
460 |
::DeleteDC(hdc); |
|
461 |
return NULL; |
|
462 |
} |
|
463 |
||
464 |
// JNI doesn't allow to store more than INT_MAX in a single array. |
|
465 |
// We report conversion failure in this case. |
|
466 |
if (width * height > INT_MAX) { |
|
467 |
free(bBytes); |
|
468 |
::DeleteDC(hdc); |
|
469 |
return NULL; |
|
470 |
} |
|
471 |
||
472 |
numPixels = width * height; |
|
473 |
||
474 |
if (pLogPalette != NULL) { |
|
475 |
hPalette = ::CreatePalette(pLogPalette); |
|
476 |
if (hPalette == NULL) { |
|
477 |
free(bBytes); |
|
478 |
::DeleteDC(hdc); |
|
479 |
return NULL; |
|
480 |
} |
|
481 |
hOldPalette = ::SelectPalette(hdc, hPalette, FALSE); |
|
482 |
::RealizePalette(hdc); |
|
483 |
} |
|
484 |
||
485 |
// allocate memory for BITMAPINFO |
|
486 |
pDstBmi = (BITMAPINFO *)safe_Calloc(1, sizeof(BITMAPINFO)); |
|
487 |
pDstBmih = &pDstBmi->bmiHeader; |
|
488 |
||
489 |
static const int BITS_PER_PIXEL = 32; |
|
490 |
||
491 |
// prepare BITMAPINFO for a 32-bit RGB bitmap |
|
492 |
pDstBmih->biSize = sizeof(BITMAPINFOHEADER); |
|
493 |
pDstBmih->biWidth = width; |
|
494 |
pDstBmih->biHeight = -height; // negative height means a top-down DIB |
|
495 |
pDstBmih->biPlanes = 1; |
|
496 |
pDstBmih->biBitCount = BITS_PER_PIXEL; |
|
497 |
pDstBmih->biCompression = BI_RGB; |
|
498 |
// NOTE: MSDN says that biSizeImage may be set to 0 for BI_RGB bitmaps, |
|
499 |
// but this causes CreateDIBSection to allocate zero-size memory block |
|
500 |
// for DIB data. It works okay when biSizeImage is explicitly specified. |
|
501 |
pDstBmih->biSizeImage = width * height * (BITS_PER_PIXEL >> 3); |
|
502 |
||
503 |
hDibSection = ::CreateDIBSection(hdc, (BITMAPINFO*)pDstBmi, |
|
504 |
DIB_RGB_COLORS, &pDstBits, |
|
505 |
NULL, 0); |
|
506 |
||
507 |
if (hDibSection == NULL) { |
|
508 |
free(pDstBmi); pDstBmi = NULL; |
|
509 |
if (hPalette != NULL) { |
|
510 |
VERIFY(::SelectPalette(hdc, hOldPalette, FALSE) != NULL); |
|
511 |
hOldPalette = NULL; |
|
512 |
VERIFY(::DeleteObject(hPalette)); hPalette = NULL; |
|
513 |
} |
|
514 |
VERIFY(::DeleteDC(hdc)); hdc = NULL; |
|
515 |
free(bBytes); bBytes = NULL; |
|
516 |
||
517 |
JNU_ThrowIOException(env, "failed to get drop data"); |
|
518 |
return NULL; |
|
519 |
} |
|
520 |
||
521 |
hOldBitmap = (HBITMAP)::SelectObject(hdc, hDibSection); |
|
522 |
DASSERT(hOldBitmap != NULL); |
|
523 |
||
524 |
switch (format) { |
|
525 |
case CF_DIB: |
|
526 |
VERIFY(::StretchDIBits(hdc, |
|
527 |
0, 0, width, height, |
|
528 |
0, 0, width, height, |
|
529 |
pSrcBits, pSrcBmi, |
|
530 |
DIB_RGB_COLORS, SRCCOPY) != GDI_ERROR); |
|
531 |
break; |
|
532 |
case CF_ENHMETAFILE: |
|
533 |
case CF_METAFILEPICT: { |
|
534 |
RECT rect = { 0, 0, width, height }; |
|
535 |
||
536 |
VERIFY(::PlayEnhMetaFile(hdc, hEnhMetaFile, &rect)); |
|
537 |
VERIFY(::DeleteEnhMetaFile(hEnhMetaFile)); hEnhMetaFile = NULL; |
|
538 |
break; |
|
539 |
} |
|
540 |
default: |
|
541 |
// Other formats are not supported yet. |
|
542 |
DASSERT(FALSE); |
|
543 |
break; |
|
544 |
} |
|
545 |
||
546 |
// convert Win32 pixel format (BGRX) to Java format (ARGB) |
|
547 |
DASSERT(sizeof(jint) == sizeof(RGBQUAD)); |
|
548 |
RGBQUAD* prgbq = (RGBQUAD*)pDstBits; |
|
549 |
for(int nPixel = 0; nPixel < numPixels; nPixel++, prgbq++) { |
|
550 |
jint jpixel = WIN_TO_JAVA_PIXEL(prgbq->rgbRed, |
|
551 |
prgbq->rgbGreen, |
|
552 |
prgbq->rgbBlue); |
|
553 |
// stuff the 32-bit pixel back into the 32-bit RGBQUAD |
|
554 |
*prgbq = *((RGBQUAD*)(&jpixel)); |
|
555 |
} |
|
556 |
||
557 |
buffer = env->NewIntArray(numPixels + 2); |
|
558 |
if (buffer == NULL) { |
|
559 |
throw std::bad_alloc(); |
|
560 |
} |
|
561 |
||
562 |
// copy pixels into Java array |
|
563 |
env->SetIntArrayRegion(buffer, 0, numPixels, (jint*)pDstBits); |
|
564 |
||
565 |
// copy dimensions into Java array |
|
566 |
env->SetIntArrayRegion(buffer, numPixels, 1, (jint*)&width); |
|
567 |
env->SetIntArrayRegion(buffer, numPixels + 1, 1, (jint*)&height); |
|
568 |
||
569 |
VERIFY(::SelectObject(hdc, hOldBitmap) != NULL); hOldBitmap = NULL; |
|
570 |
VERIFY(::DeleteObject(hDibSection)); hDibSection = NULL; |
|
571 |
free(pDstBmi); pDstBmi = NULL; |
|
572 |
if (hPalette != NULL) { |
|
573 |
VERIFY(::SelectPalette(hdc, hOldPalette, FALSE) != NULL); |
|
574 |
hOldPalette = NULL; |
|
575 |
VERIFY(::DeleteObject(hPalette)); hPalette = NULL; |
|
576 |
} |
|
577 |
VERIFY(::DeleteDC(hdc)); hdc = NULL; |
|
578 |
free(bBytes); bBytes = NULL; |
|
579 |
} catch (...) { |
|
580 |
if (hdc != NULL && hOldBitmap != NULL) { |
|
581 |
VERIFY(::SelectObject(hdc, hOldBitmap) != NULL); hOldBitmap = NULL; |
|
582 |
} |
|
583 |
if (hDibSection != NULL) { |
|
584 |
VERIFY(::DeleteObject(hDibSection)); hDibSection = NULL; |
|
585 |
} |
|
586 |
if (pDstBmi != NULL) { |
|
587 |
free(pDstBmi); pDstBmi = NULL; |
|
588 |
} |
|
589 |
if (hPalette != NULL) { |
|
590 |
if (hdc != NULL) { |
|
591 |
VERIFY(::SelectPalette(hdc, hOldPalette, FALSE) != NULL); |
|
592 |
hOldPalette = NULL; |
|
593 |
} |
|
594 |
VERIFY(::DeleteObject(hPalette)); hPalette = NULL; |
|
595 |
} |
|
596 |
if (hdc != NULL) { |
|
597 |
VERIFY(::DeleteDC(hdc)); hdc = NULL; |
|
598 |
} |
|
599 |
if (hEnhMetaFile != NULL) { |
|
600 |
VERIFY(::DeleteEnhMetaFile(hEnhMetaFile)); hEnhMetaFile = NULL; |
|
601 |
} |
|
602 |
if (bBytes != NULL) { |
|
603 |
free(bBytes); bBytes = NULL; |
|
604 |
} |
|
605 |
throw; |
|
606 |
} |
|
607 |
||
608 |
return buffer; |
|
609 |
||
610 |
CATCH_BAD_ALLOC_RET(NULL); |
|
611 |
} |
|
612 |
||
613 |
/* |
|
614 |
* Class: sun_awt_windows_WDataTransferer |
|
615 |
* Method: imageDataToPlatformImageBytes |
|
616 |
* Signature: ([BIII)[B |
|
617 |
*/ |
|
618 |
JNIEXPORT jbyteArray JNICALL |
|
619 |
Java_sun_awt_windows_WDataTransferer_imageDataToPlatformImageBytes(JNIEnv *env, |
|
620 |
jobject self, jbyteArray imageData, |
|
621 |
jint width, jint height, |
|
622 |
jlong format) { |
|
623 |
||
624 |
TRY; |
|
625 |
||
626 |
if (JNU_IsNull(env, imageData)) { |
|
627 |
return NULL; |
|
628 |
} |
|
629 |
||
630 |
UINT size = env->GetArrayLength(imageData); |
|
631 |
if (size == 0) { |
|
632 |
return NULL; |
|
633 |
} |
|
634 |
||
635 |
// In the passed imageData array all lines are padded with zeroes except for |
|
636 |
// the last one, so we have to add one pad size here. |
|
637 |
int mod = (width * 3) % 4; |
|
638 |
int pad = mod > 0 ? 4 - mod : 0; |
|
639 |
int nBytes = sizeof(BITMAPINFO) + size + pad; |
|
640 |
BITMAPINFO* pinfo = (BITMAPINFO*)safe_Calloc(1, nBytes); |
|
641 |
||
642 |
static const int BITS_PER_PIXEL = 24; |
|
643 |
||
644 |
// prepare BITMAPINFO for a 24-bit BGR bitmap |
|
645 |
pinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); |
|
646 |
pinfo->bmiHeader.biWidth = width; |
|
647 |
pinfo->bmiHeader.biHeight = height; // positive height means a bottom-up DIB |
|
648 |
pinfo->bmiHeader.biPlanes = 1; |
|
649 |
pinfo->bmiHeader.biBitCount = BITS_PER_PIXEL; |
|
650 |
pinfo->bmiHeader.biCompression = BI_RGB; |
|
651 |
// NOTE: MSDN says that biSizeImage may be set to 0 for BI_RGB bitmaps, |
|
652 |
// but some programs (e.g. Imaging for Windows NT by Wang Laboratories) |
|
653 |
// don't handle such DIBs correctly, so we specify the size explicitly. |
|
654 |
pinfo->bmiHeader.biSizeImage = size + pad; |
|
655 |
||
656 |
jbyte *array = (jbyte*)((LPSTR)pinfo + sizeof(BITMAPINFOHEADER)); |
|
657 |
env->GetByteArrayRegion(imageData, 0, size, array); |
|
658 |
HRESULT hr = S_OK; |
|
659 |
||
660 |
jbyteArray bytes = NULL; |
|
661 |
switch (format) { |
|
662 |
case CF_DIB: |
|
663 |
bytes = env->NewByteArray(nBytes); |
|
664 |
if( NULL == bytes ) { |
|
665 |
hr = E_OUTOFMEMORY; |
|
666 |
} else { |
|
667 |
env->SetByteArrayRegion(bytes, 0, nBytes, (jbyte*)pinfo); |
|
668 |
} |
|
669 |
break; |
|
670 |
case CF_ENHMETAFILE: |
|
671 |
{ |
|
672 |
HDC hdc = ::GetDC(NULL); |
|
673 |
if( NULL == hdc) { |
|
674 |
hr = HRESULT_FROM_WIN32(::GetLastError()); |
|
675 |
} else { |
|
676 |
POINT p = { width, height }; |
|
677 |
//We are trying to support context-independent metafile. |
|
678 |
//To implement it we have to select correct MM_HIMETRIC map mode. |
|
679 |
VERIFY(::SetMapMode(hdc, MM_HIMETRIC)); |
|
680 |
VERIFY(::DPtoLP(hdc, &p, 1)); |
|
681 |
//In accordance with CreateEnhMetaFile documentation the rectangle have to |
|
682 |
//be normal (left <= right, top <= bottom) |
|
683 |
RECT r = { min(0, p.x), min(0, p.y), max(0, p.x), max(0, p.y) }; |
|
684 |
//Due to inversed row order in source bitmap the destination |
|
685 |
//height have to be negative. |
|
686 |
HDC hemfdc = ::CreateEnhMetaFile(NULL, NULL, &r, NULL); |
|
687 |
if( NULL == hemfdc) { |
|
688 |
hr = HRESULT_FROM_WIN32(::GetLastError()); |
|
689 |
} else { |
|
690 |
int iMFHeight = r.bottom - r.top; |
|
691 |
int iMFWidth = r.right - r.left; |
|
692 |
VERIFY(::SetMapMode(hemfdc, MM_HIMETRIC)); |
|
693 |
if( GDI_ERROR == ::StretchDIBits(hemfdc, |
|
694 |
0, iMFHeight, iMFWidth, -iMFHeight, |
|
695 |
0, 0, width, height, |
|
696 |
(LPVOID)array, pinfo, |
|
697 |
DIB_RGB_COLORS, SRCCOPY)) |
|
698 |
{ |
|
699 |
hr = HRESULT_FROM_WIN32(::GetLastError()); |
|
700 |
} |
|
701 |
HENHMETAFILE hemf = ::CloseEnhMetaFile(hemfdc); |
|
702 |
if( NULL == hemf) { |
|
703 |
hr = HRESULT_FROM_WIN32(::GetLastError()); |
|
704 |
} else { |
|
705 |
if(SUCCEEDED(hr)){ |
|
706 |
UINT uEmfSize = ::GetEnhMetaFileBits(hemf, 0, NULL); |
|
707 |
if( 0 == uEmfSize) { |
|
708 |
hr = HRESULT_FROM_WIN32(::GetLastError()); |
|
709 |
} else { |
|
710 |
LPBYTE lpbEmfBuffer = NULL; |
|
711 |
try { |
|
712 |
lpbEmfBuffer = (LPBYTE)safe_Malloc(uEmfSize); |
|
713 |
VERIFY(::GetEnhMetaFileBits(hemf, uEmfSize, |
|
714 |
lpbEmfBuffer) == uEmfSize); |
|
715 |
bytes = env->NewByteArray(uEmfSize); |
|
716 |
if(NULL == bytes) { |
|
717 |
hr = E_OUTOFMEMORY; |
|
718 |
} else { |
|
719 |
env->SetByteArrayRegion(bytes, 0, uEmfSize, (jbyte*)lpbEmfBuffer); |
|
720 |
} |
|
721 |
} catch (std::bad_alloc &) { |
|
722 |
hr = E_OUTOFMEMORY; |
|
723 |
} |
|
724 |
free(lpbEmfBuffer); |
|
725 |
} |
|
726 |
} |
|
727 |
VERIFY(::DeleteEnhMetaFile(hemf)); |
|
728 |
} |
|
729 |
} |
|
730 |
VERIFY(::ReleaseDC(NULL, hdc)); |
|
731 |
} |
|
732 |
break; |
|
733 |
} |
|
734 |
case CF_METAFILEPICT: |
|
735 |
{ |
|
736 |
HDC hdc = ::GetDC(NULL); |
|
737 |
if( NULL == hdc) { |
|
738 |
hr = HRESULT_FROM_WIN32(::GetLastError()); |
|
739 |
} else { |
|
740 |
POINT p = { width, height }; |
|
741 |
VERIFY(::SetMapMode(hdc, MM_HIMETRIC)); |
|
742 |
VERIFY(::DPtoLP(hdc, &p, 1)); |
|
743 |
RECT r = { min(0, p.x), min(0, p.y), max(0, p.x), max(0, p.y) }; |
|
744 |
HDC hmfdc = ::CreateMetaFile(NULL); |
|
745 |
if( NULL == hmfdc) { |
|
746 |
hr = HRESULT_FROM_WIN32(::GetLastError()); |
|
747 |
} else { |
|
748 |
VERIFY(::SetMapMode(hmfdc, MM_HIMETRIC)); |
|
749 |
int iMFHeight = r.bottom - r.top; |
|
750 |
int iMFWidth = r.right - r.left; |
|
751 |
//The destination Y coordinate (3d parameter in StretchDIBits call) is different for |
|
752 |
//CF_ENHMETAFILE and CF_METAFILEPICT formats due to applying MM_ANISOTROPIC map mode |
|
753 |
//at very last moment. MM_ANISOTROPIC map mode changes the Y-axis direction and can be |
|
754 |
//selected just for metafile header. |
|
755 |
if( GDI_ERROR == ::StretchDIBits(hmfdc, |
|
756 |
0, 0, iMFWidth, -iMFHeight, |
|
757 |
0, 0, width, height, |
|
758 |
(LPVOID)array, pinfo, |
|
759 |
DIB_RGB_COLORS, SRCCOPY)) |
|
760 |
{ |
|
761 |
hr = HRESULT_FROM_WIN32(::GetLastError()); |
|
762 |
} |
|
763 |
HMETAFILE hmf = ::CloseMetaFile(hmfdc); |
|
764 |
if( NULL == hmf) { |
|
765 |
hr = HRESULT_FROM_WIN32(::GetLastError()); |
|
766 |
} else { |
|
767 |
if(SUCCEEDED(hr)){ |
|
768 |
UINT uMfSize = ::GetMetaFileBitsEx(hmf, 0, NULL); |
|
769 |
if( 0 == uMfSize) { |
|
770 |
hr = HRESULT_FROM_WIN32(::GetLastError()); |
|
771 |
} else { |
|
772 |
LPBYTE lpbMfBuffer = NULL; |
|
773 |
try { |
|
774 |
UINT uMfSizeWithHead = uMfSize + sizeof(METAFILEPICT); |
|
775 |
||
776 |
lpbMfBuffer = (LPBYTE)safe_Malloc(uMfSizeWithHead); |
|
777 |
VERIFY(::GetMetaFileBitsEx(hmf, uMfSize, |
|
778 |
lpbMfBuffer + sizeof(METAFILEPICT)) == uMfSize); |
|
779 |
bytes = env->NewByteArray(uMfSizeWithHead); |
|
780 |
if(NULL == bytes) { |
|
781 |
hr = E_OUTOFMEMORY; |
|
782 |
} else { |
|
783 |
LPMETAFILEPICT lpMfp = (LPMETAFILEPICT)lpbMfBuffer; |
|
784 |
lpMfp->mm = MM_ANISOTROPIC; // should use MM_ANISOTROPIC exactly (MSDN) |
|
785 |
lpMfp->xExt = iMFWidth; |
|
786 |
lpMfp->yExt = iMFHeight; |
|
787 |
env->SetByteArrayRegion(bytes, 0, uMfSizeWithHead, (jbyte*)lpbMfBuffer); |
|
788 |
} |
|
789 |
} catch (std::bad_alloc &) { |
|
790 |
hr = E_OUTOFMEMORY; |
|
791 |
} |
|
792 |
free(lpbMfBuffer); |
|
793 |
} |
|
794 |
} |
|
795 |
VERIFY(::DeleteMetaFile(hmf)); |
|
796 |
} |
|
797 |
} |
|
798 |
VERIFY(::ReleaseDC(NULL, hdc)); |
|
799 |
} |
|
800 |
break; |
|
801 |
} |
|
802 |
default: |
|
803 |
DASSERT(FALSE); // Other formats are not supported yet. |
|
804 |
hr = E_NOTIMPL; |
|
805 |
break; |
|
806 |
} |
|
807 |
free(pinfo); |
|
808 |
if(FAILED(hr)){ |
|
809 |
if(E_OUTOFMEMORY == hr) |
|
810 |
throw std::bad_alloc(); |
|
811 |
return NULL; |
|
812 |
} |
|
813 |
return bytes; |
|
814 |
CATCH_BAD_ALLOC_RET(NULL); |
|
815 |
} |
|
816 |
||
817 |
/* |
|
818 |
* Class: sun_awt_windows_WDataTransferer |
|
819 |
* Method: registerClipboardFormat |
|
820 |
* Signature: (Ljava/lang/String;)J |
|
821 |
*/ |
|
822 |
JNIEXPORT jlong JNICALL |
|
823 |
Java_sun_awt_windows_WDataTransferer_registerClipboardFormat(JNIEnv *env, |
|
824 |
jclass cls, |
|
825 |
jstring str) |
|
826 |
{ |
|
827 |
TRY; |
|
828 |
||
829 |
LPCTSTR cStr = JNU_GetStringPlatformChars(env, str, NULL); |
|
830 |
jlong value = ::RegisterClipboardFormat(cStr); |
|
831 |
JNU_ReleaseStringPlatformChars(env, str, cStr); |
|
832 |
||
833 |
return value; |
|
834 |
||
835 |
CATCH_BAD_ALLOC_RET(0); |
|
836 |
} |
|
837 |
||
838 |
/* |
|
839 |
* Class: sun_awt_windows_WDataTransferer |
|
840 |
* Method: getClipboardFormatName |
|
841 |
* Signature: (J)Ljava/lang/String; |
|
842 |
*/ |
|
843 |
JNIEXPORT jstring JNICALL |
|
844 |
Java_sun_awt_windows_WDataTransferer_getClipboardFormatName(JNIEnv *env, |
|
845 |
jclass cls, |
|
846 |
jlong format) |
|
847 |
{ |
|
848 |
TRY; |
|
849 |
||
850 |
LPTSTR buf = new TCHAR[512]; // perhaps a bad idea to limit ourselves to 512 |
|
851 |
VERIFY(::GetClipboardFormatName((UINT)format, buf, 512)); |
|
852 |
jstring name = JNU_NewStringPlatform(env, buf); |
|
853 |
delete [] buf; |
|
854 |
if (name == NULL) { |
|
855 |
throw std::bad_alloc(); |
|
856 |
} |
|
857 |
return name; |
|
858 |
||
859 |
CATCH_BAD_ALLOC_RET(NULL); |
|
860 |
} |
|
861 |
||
862 |
/* |
|
863 |
* Class: sun_awt_windows_WToolkitThreadBlockedHandler |
|
864 |
* Method: startSecondaryEventLoop |
|
865 |
* Signature: ()V; |
|
866 |
*/ |
|
867 |
JNIEXPORT void JNICALL |
|
868 |
Java_sun_awt_windows_WToolkitThreadBlockedHandler_startSecondaryEventLoop(JNIEnv *env, jclass) |
|
869 |
{ |
|
870 |
TRY; |
|
871 |
||
872 |
AwtDataTransferer::SecondaryMessageLoop(); |
|
873 |
||
874 |
CATCH_BAD_ALLOC; |
|
875 |
} |
|
876 |
||
877 |
} |