merge the formfield patch from ooo-build
[ooovba.git] / vcl / source / window / javachild.cxx
blob2a72973f0bed6145868c9444409c5696b5fb4e3b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: javachild.cxx,v $
10 * $Revision: 1.12 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_vcl.hxx"
35 #ifdef SOLAR_JAVA
36 #include <jni.h>
37 #endif
38 #include <comphelper/processfactory.hxx>
40 #include <vcl/unohelp.hxx>
41 #include <rtl/process.h>
42 #include <rtl/ref.hxx>
43 #include <jvmaccess/virtualmachine.hxx>
44 #include <com/sun/star/java/XJavaVM.hpp>
45 #include <com/sun/star/java/XJavaThreadRegister_11.hpp>
46 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
48 #ifndef _SV_SVSYS_HXX
49 #include <svsys.h>
50 #endif
51 #include <vcl/salinst.hxx>
52 #include <vcl/salframe.hxx>
53 #include <vcl/window.hxx>
54 #include <vcl/salobj.hxx>
55 #include <vcl/javachild.hxx>
56 #include <vcl/svdata.hxx>
57 #include <vcl/sysdata.hxx>
59 using namespace ::com::sun::star;
61 // -------------------
62 // - JavaChildWindow -
63 // -------------------
65 JavaChildWindow::JavaChildWindow( Window* pParent, WinBits nStyle ) :
66 SystemChildWindow( pParent, nStyle )
70 // -----------------------------------------------------------------------
72 JavaChildWindow::JavaChildWindow( Window* pParent, const ResId& rResId ) :
73 SystemChildWindow( pParent, rResId )
77 // -----------------------------------------------------------------------
79 JavaChildWindow::~JavaChildWindow()
83 // -----------------------------------------------------------------------
85 void JavaChildWindow::implTestJavaException( void* pEnv )
87 #ifdef SOLAR_JAVA
88 JNIEnv* pJavaEnv = reinterpret_cast< JNIEnv* >( pEnv );
89 jthrowable jtThrowable = pJavaEnv->ExceptionOccurred();
91 if( jtThrowable )
92 { // is it a java exception ?
93 #if OSL_DEBUG_LEVEL > 1
94 pJavaEnv->ExceptionDescribe();
95 #endif // OSL_DEBUG_LEVEL > 1
96 pJavaEnv->ExceptionClear();
98 jclass jcThrowable = pJavaEnv->FindClass("java/lang/Throwable");
99 jmethodID jmThrowable_getMessage = pJavaEnv->GetMethodID(jcThrowable, "getMessage", "()Ljava/lang/String;");
100 jstring jsMessage = (jstring) pJavaEnv->CallObjectMethod(jtThrowable, jmThrowable_getMessage);
101 ::rtl::OUString ouMessage;
103 if(jsMessage)
105 const jchar * jcMessage = pJavaEnv->GetStringChars(jsMessage, NULL);
106 ouMessage = ::rtl::OUString(jcMessage);
107 pJavaEnv->ReleaseStringChars(jsMessage, jcMessage);
110 throw uno::RuntimeException(ouMessage, uno::Reference<uno::XInterface>());
112 #endif // SOLAR_JAVA
115 // -----------------------------------------------------------------------
117 sal_IntPtr JavaChildWindow::getParentWindowHandleForJava()
119 sal_IntPtr nRet = 0;
121 #if defined WNT
122 nRet = reinterpret_cast< sal_IntPtr >( GetSystemData()->hWnd );
123 #elif defined QUARTZ
124 // FIXME: this is wrong
125 nRet = reinterpret_cast< sal_IntPtr >( GetSystemData()->pView );
126 #elif defined UNX
127 #ifdef SOLAR_JAVA
128 uno::Reference< lang::XMultiServiceFactory > xFactory( vcl::unohelper::GetMultiServiceFactory() );
130 if( xFactory.is() && ( GetSystemData()->aWindow > 0 ) )
134 ::rtl::Reference< ::jvmaccess::VirtualMachine > xVM;
135 uno::Reference< java::XJavaVM > xJavaVM( xFactory->createInstance( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.java.JavaVirtualMachine") ) ), uno::UNO_QUERY );
136 uno::Sequence< sal_Int8 > aProcessID( 17 );
138 rtl_getGlobalProcessId( (sal_uInt8*) aProcessID.getArray() );
139 aProcessID[ 16 ] = 0;
140 OSL_ENSURE(sizeof (sal_Int64) >= sizeof (jvmaccess::VirtualMachine *), "Pointer cannot be represented as sal_Int64");
141 sal_Int64 nPointer = reinterpret_cast< sal_Int64 >( static_cast< jvmaccess::VirtualMachine * >(0));
142 xJavaVM->getJavaVM(aProcessID) >>= nPointer;
143 xVM = reinterpret_cast< jvmaccess::VirtualMachine * >(nPointer);
145 if( xVM.is() )
149 ::jvmaccess::VirtualMachine::AttachGuard aVMAttachGuard( xVM );
150 JNIEnv* pEnv = aVMAttachGuard.getEnvironment();
152 jclass jcToolkit = pEnv->FindClass("java/awt/Toolkit");
153 implTestJavaException(pEnv);
155 jmethodID jmToolkit_getDefaultToolkit = pEnv->GetStaticMethodID( jcToolkit, "getDefaultToolkit", "()Ljava/awt/Toolkit;" );
156 implTestJavaException(pEnv);
158 pEnv->CallStaticObjectMethod(jcToolkit, jmToolkit_getDefaultToolkit);
159 implTestJavaException(pEnv);
161 jclass jcMotifAppletViewer = pEnv->FindClass("sun/plugin/navig/motif/MotifAppletViewer");
162 if( pEnv->ExceptionOccurred() )
164 pEnv->ExceptionClear();
166 jcMotifAppletViewer = pEnv->FindClass( "sun/plugin/viewer/MNetscapePluginContext");
167 implTestJavaException(pEnv);
170 jclass jcClassLoader = pEnv->FindClass("java/lang/ClassLoader");
171 implTestJavaException(pEnv);
173 jmethodID jmClassLoader_loadLibrary = pEnv->GetStaticMethodID( jcClassLoader, "loadLibrary", "(Ljava/lang/Class;Ljava/lang/String;Z)V");
174 implTestJavaException(pEnv);
176 jstring jsplugin = pEnv->NewStringUTF("javaplugin_jni");
177 implTestJavaException(pEnv);
179 pEnv->CallStaticVoidMethod(jcClassLoader, jmClassLoader_loadLibrary, jcMotifAppletViewer, jsplugin, JNI_FALSE);
180 implTestJavaException(pEnv);
182 jmethodID jmMotifAppletViewer_getWidget = pEnv->GetStaticMethodID( jcMotifAppletViewer, "getWidget", "(IIIII)I" );
183 implTestJavaException(pEnv);
185 const Size aSize( GetOutputSizePixel() );
186 jint ji_widget = pEnv->CallStaticIntMethod( jcMotifAppletViewer, jmMotifAppletViewer_getWidget,
187 GetSystemData()->aWindow, 0, 0, aSize.Width(), aSize.Height() );
188 implTestJavaException(pEnv);
190 nRet = static_cast< sal_IntPtr >( ji_widget );
192 catch( uno::RuntimeException& )
196 if( !nRet )
197 nRet = static_cast< sal_IntPtr >( GetSystemData()->aWindow );
200 catch( ... )
204 #endif // SOLAR_JAVA
205 #else // WNT || QUARTZ || UNX
206 // TBD
207 #endif
209 return nRet;