19f62ea84SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 39f62ea84SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 49f62ea84SAndrew Rist * or more contributor license agreements. See the NOTICE file 59f62ea84SAndrew Rist * distributed with this work for additional information 69f62ea84SAndrew Rist * regarding copyright ownership. The ASF licenses this file 79f62ea84SAndrew Rist * to you under the Apache License, Version 2.0 (the 89f62ea84SAndrew Rist * "License"); you may not use this file except in compliance 99f62ea84SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 119f62ea84SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 139f62ea84SAndrew Rist * Unless required by applicable law or agreed to in writing, 149f62ea84SAndrew Rist * software distributed under the License is distributed on an 159f62ea84SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 169f62ea84SAndrew Rist * KIND, either express or implied. See the License for the 179f62ea84SAndrew Rist * specific language governing permissions and limitations 189f62ea84SAndrew Rist * under the License. 19cdf0e10cSrcweir * 209f62ea84SAndrew Rist *************************************************************/ 219f62ea84SAndrew Rist 229f62ea84SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_vcl.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include <rtl/process.h> 28cdf0e10cSrcweir #include <rtl/ref.hxx> 29cdf0e10cSrcweir 30cdf0e10cSrcweir #include <tools/rc.h> 31cdf0e10cSrcweir 32cdf0e10cSrcweir // declare system types in sysdata.hxx 33cdf0e10cSrcweir #include <svsys.h> 34cdf0e10cSrcweir 35cdf0e10cSrcweir #include <vcl/window.hxx> 36cdf0e10cSrcweir #include <vcl/sysdata.hxx> 37cdf0e10cSrcweir #include <vcl/svapp.hxx> 38cdf0e10cSrcweir #include <vcl/syschild.hxx> 39cdf0e10cSrcweir #include <vcl/unohelp.hxx> 40cdf0e10cSrcweir 41cdf0e10cSrcweir #include <window.h> 42cdf0e10cSrcweir #include <salinst.hxx> 43cdf0e10cSrcweir #include <salframe.hxx> 44cdf0e10cSrcweir #include <salobj.hxx> 45cdf0e10cSrcweir #include <svdata.hxx> 46cdf0e10cSrcweir 47cdf0e10cSrcweir #ifdef SOLAR_JAVA 48cdf0e10cSrcweir #include <jni.h> 49cdf0e10cSrcweir #endif 50cdf0e10cSrcweir 51cdf0e10cSrcweir #include <comphelper/processfactory.hxx> 52cdf0e10cSrcweir #include <jvmaccess/virtualmachine.hxx> 53cdf0e10cSrcweir #include <com/sun/star/java/XJavaVM.hpp> 54cdf0e10cSrcweir #include <com/sun/star/java/XJavaThreadRegister_11.hpp> 55cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp> 56cdf0e10cSrcweir 57cdf0e10cSrcweir using namespace ::com::sun::star; 58cdf0e10cSrcweir 59cdf0e10cSrcweir // ======================================================================= 60cdf0e10cSrcweir 61cdf0e10cSrcweir long ImplSysChildProc( void* pInst, SalObject* /* pObject */, 62cdf0e10cSrcweir sal_uInt16 nEvent, const void* /* pEvent */ ) 63cdf0e10cSrcweir { 64cdf0e10cSrcweir SystemChildWindow* pWindow = (SystemChildWindow*)pInst; 65cdf0e10cSrcweir long nRet = 0; 66cdf0e10cSrcweir 67cdf0e10cSrcweir ImplDelData aDogTag( pWindow ); 68cdf0e10cSrcweir switch ( nEvent ) 69cdf0e10cSrcweir { 70cdf0e10cSrcweir case SALOBJ_EVENT_GETFOCUS: 71cdf0e10cSrcweir // Focus holen und zwar so, das alle Handler gerufen 72cdf0e10cSrcweir // werden, als ob dieses Fenster den Focus bekommt, 73cdf0e10cSrcweir // ohne das der Frame den Focus wieder klaut 74cdf0e10cSrcweir pWindow->ImplGetFrameData()->mbSysObjFocus = sal_True; 75cdf0e10cSrcweir pWindow->ImplGetFrameData()->mbInSysObjToTopHdl = sal_True; 76cdf0e10cSrcweir pWindow->ToTop( TOTOP_NOGRABFOCUS ); 77cdf0e10cSrcweir if( aDogTag.IsDead() ) 78cdf0e10cSrcweir break; 79cdf0e10cSrcweir pWindow->ImplGetFrameData()->mbInSysObjToTopHdl = sal_False; 80cdf0e10cSrcweir pWindow->ImplGetFrameData()->mbInSysObjFocusHdl = sal_True; 81cdf0e10cSrcweir pWindow->GrabFocus(); 82cdf0e10cSrcweir if( aDogTag.IsDead() ) 83cdf0e10cSrcweir break; 84cdf0e10cSrcweir pWindow->ImplGetFrameData()->mbInSysObjFocusHdl = sal_False; 85cdf0e10cSrcweir break; 86cdf0e10cSrcweir 87cdf0e10cSrcweir case SALOBJ_EVENT_LOSEFOCUS: 88cdf0e10cSrcweir // Hintenrum einen LoseFocus ausloesen, das der Status 89cdf0e10cSrcweir // der Fenster dem entsprechenden Activate-Status 90cdf0e10cSrcweir // entspricht 91cdf0e10cSrcweir pWindow->ImplGetFrameData()->mbSysObjFocus = sal_False; 92cdf0e10cSrcweir if ( !pWindow->ImplGetFrameData()->mnFocusId ) 93cdf0e10cSrcweir { 94cdf0e10cSrcweir pWindow->ImplGetFrameData()->mbStartFocusState = sal_True; 95cdf0e10cSrcweir Application::PostUserEvent( pWindow->ImplGetFrameData()->mnFocusId, LINK( pWindow->ImplGetFrameWindow(), Window, ImplAsyncFocusHdl ) ); 96cdf0e10cSrcweir } 97cdf0e10cSrcweir break; 98cdf0e10cSrcweir 99cdf0e10cSrcweir case SALOBJ_EVENT_TOTOP: 100cdf0e10cSrcweir pWindow->ImplGetFrameData()->mbInSysObjToTopHdl = sal_True; 101cdf0e10cSrcweir if ( !Application::GetFocusWindow() || pWindow->HasChildPathFocus() ) 102cdf0e10cSrcweir pWindow->ToTop( TOTOP_NOGRABFOCUS ); 103cdf0e10cSrcweir else 104cdf0e10cSrcweir pWindow->ToTop(); 105cdf0e10cSrcweir if( aDogTag.IsDead() ) 106cdf0e10cSrcweir break; 107cdf0e10cSrcweir pWindow->GrabFocus(); 108cdf0e10cSrcweir if( aDogTag.IsDead() ) 109cdf0e10cSrcweir break; 110cdf0e10cSrcweir pWindow->ImplGetFrameData()->mbInSysObjToTopHdl = sal_False; 111cdf0e10cSrcweir break; 112cdf0e10cSrcweir } 113cdf0e10cSrcweir 114cdf0e10cSrcweir return nRet; 115cdf0e10cSrcweir } 116cdf0e10cSrcweir 117cdf0e10cSrcweir // ======================================================================= 118cdf0e10cSrcweir 119cdf0e10cSrcweir void SystemChildWindow::ImplInitSysChild( Window* pParent, WinBits nStyle, SystemWindowData *pData, sal_Bool bShow ) 120cdf0e10cSrcweir { 121cdf0e10cSrcweir mpWindowImpl->mpSysObj = ImplGetSVData()->mpDefInst->CreateObject( pParent->ImplGetFrame(), pData, bShow ); 122cdf0e10cSrcweir 123cdf0e10cSrcweir Window::ImplInit( pParent, nStyle, NULL ); 124cdf0e10cSrcweir 125cdf0e10cSrcweir // Wenn es ein richtiges SysChild ist, dann painten wir auch nicht 126cdf0e10cSrcweir if ( GetSystemData() ) 127cdf0e10cSrcweir { 128cdf0e10cSrcweir mpWindowImpl->mpSysObj->SetCallback( this, ImplSysChildProc ); 129cdf0e10cSrcweir SetParentClipMode( PARENTCLIPMODE_CLIP ); 130cdf0e10cSrcweir SetBackground(); 131cdf0e10cSrcweir } 132cdf0e10cSrcweir } 133cdf0e10cSrcweir 134cdf0e10cSrcweir // ----------------------------------------------------------------------- 135cdf0e10cSrcweir 136cdf0e10cSrcweir SystemChildWindow::SystemChildWindow( Window* pParent, WinBits nStyle ) : 137cdf0e10cSrcweir Window( WINDOW_SYSTEMCHILDWINDOW ) 138cdf0e10cSrcweir { 139cdf0e10cSrcweir ImplInitSysChild( pParent, nStyle, NULL ); 140cdf0e10cSrcweir } 141cdf0e10cSrcweir 142cdf0e10cSrcweir // ----------------------------------------------------------------------- 143cdf0e10cSrcweir 144cdf0e10cSrcweir SystemChildWindow::SystemChildWindow( Window* pParent, WinBits nStyle, SystemWindowData *pData, sal_Bool bShow ) : 145cdf0e10cSrcweir Window( WINDOW_SYSTEMCHILDWINDOW ) 146cdf0e10cSrcweir { 147cdf0e10cSrcweir ImplInitSysChild( pParent, nStyle, pData, bShow ); 148cdf0e10cSrcweir } 149cdf0e10cSrcweir 150cdf0e10cSrcweir // ----------------------------------------------------------------------- 151cdf0e10cSrcweir 152cdf0e10cSrcweir SystemChildWindow::SystemChildWindow( Window* pParent, const ResId& rResId ) : 153cdf0e10cSrcweir Window( WINDOW_SYSTEMCHILDWINDOW ) 154cdf0e10cSrcweir { 155cdf0e10cSrcweir rResId.SetRT( RSC_WINDOW ); 156cdf0e10cSrcweir WinBits nStyle = ImplInitRes( rResId ); 157cdf0e10cSrcweir ImplInitSysChild( pParent, nStyle, NULL ); 158cdf0e10cSrcweir ImplLoadRes( rResId ); 159cdf0e10cSrcweir 160cdf0e10cSrcweir if ( !(nStyle & WB_HIDE) ) 161cdf0e10cSrcweir Show(); 162cdf0e10cSrcweir } 163cdf0e10cSrcweir 164cdf0e10cSrcweir // ----------------------------------------------------------------------- 165cdf0e10cSrcweir 166cdf0e10cSrcweir SystemChildWindow::~SystemChildWindow() 167cdf0e10cSrcweir { 168cdf0e10cSrcweir Hide(); 169cdf0e10cSrcweir if ( mpWindowImpl->mpSysObj ) 170cdf0e10cSrcweir { 171cdf0e10cSrcweir ImplGetSVData()->mpDefInst->DestroyObject( mpWindowImpl->mpSysObj ); 172cdf0e10cSrcweir mpWindowImpl->mpSysObj = NULL; 173cdf0e10cSrcweir } 174cdf0e10cSrcweir } 175cdf0e10cSrcweir 176cdf0e10cSrcweir // ----------------------------------------------------------------------- 177cdf0e10cSrcweir 178cdf0e10cSrcweir const SystemEnvData* SystemChildWindow::GetSystemData() const 179cdf0e10cSrcweir { 180cdf0e10cSrcweir if ( mpWindowImpl->mpSysObj ) 181cdf0e10cSrcweir return mpWindowImpl->mpSysObj->GetSystemData(); 182cdf0e10cSrcweir else 183cdf0e10cSrcweir return NULL; 184cdf0e10cSrcweir } 185cdf0e10cSrcweir 186cdf0e10cSrcweir // ----------------------------------------------------------------------- 187cdf0e10cSrcweir 188cdf0e10cSrcweir void SystemChildWindow::EnableEraseBackground( sal_Bool bEnable ) 189cdf0e10cSrcweir { 190cdf0e10cSrcweir if ( mpWindowImpl->mpSysObj ) 191cdf0e10cSrcweir mpWindowImpl->mpSysObj->EnableEraseBackground( bEnable ); 192cdf0e10cSrcweir } 193cdf0e10cSrcweir 194cdf0e10cSrcweir // ----------------------------------------------------------------------- 195cdf0e10cSrcweir 196cdf0e10cSrcweir sal_Bool SystemChildWindow::IsEraseBackgroundEnabled() 197cdf0e10cSrcweir { 198cdf0e10cSrcweir if ( mpWindowImpl->mpSysObj ) 199cdf0e10cSrcweir return mpWindowImpl->mpSysObj->IsEraseBackgroundEnabled(); 200cdf0e10cSrcweir else 201cdf0e10cSrcweir return sal_False; 202cdf0e10cSrcweir } 203cdf0e10cSrcweir 204cdf0e10cSrcweir // ----------------------------------------------------------------------- 205cdf0e10cSrcweir 206cdf0e10cSrcweir void SystemChildWindow::ImplTestJavaException( void* pEnv ) 207cdf0e10cSrcweir { 208cdf0e10cSrcweir #ifdef SOLAR_JAVA 209cdf0e10cSrcweir JNIEnv* pJavaEnv = reinterpret_cast< JNIEnv* >( pEnv ); 210cdf0e10cSrcweir jthrowable jtThrowable = pJavaEnv->ExceptionOccurred(); 211cdf0e10cSrcweir 212cdf0e10cSrcweir if( jtThrowable ) 213cdf0e10cSrcweir { // is it a java exception ? 214cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1 215cdf0e10cSrcweir pJavaEnv->ExceptionDescribe(); 216cdf0e10cSrcweir #endif // OSL_DEBUG_LEVEL > 1 217cdf0e10cSrcweir pJavaEnv->ExceptionClear(); 218cdf0e10cSrcweir 219cdf0e10cSrcweir jclass jcThrowable = pJavaEnv->FindClass("java/lang/Throwable"); 220cdf0e10cSrcweir jmethodID jmThrowable_getMessage = pJavaEnv->GetMethodID(jcThrowable, "getMessage", "()Ljava/lang/String;"); 221cdf0e10cSrcweir jstring jsMessage = (jstring) pJavaEnv->CallObjectMethod(jtThrowable, jmThrowable_getMessage); 222cdf0e10cSrcweir ::rtl::OUString ouMessage; 223cdf0e10cSrcweir 224cdf0e10cSrcweir if(jsMessage) 225cdf0e10cSrcweir { 226cdf0e10cSrcweir const jchar * jcMessage = pJavaEnv->GetStringChars(jsMessage, NULL); 227cdf0e10cSrcweir ouMessage = ::rtl::OUString(jcMessage); 228cdf0e10cSrcweir pJavaEnv->ReleaseStringChars(jsMessage, jcMessage); 229cdf0e10cSrcweir } 230cdf0e10cSrcweir 231cdf0e10cSrcweir throw uno::RuntimeException(ouMessage, uno::Reference<uno::XInterface>()); 232cdf0e10cSrcweir } 233cdf0e10cSrcweir #endif // SOLAR_JAVA 234cdf0e10cSrcweir } 235cdf0e10cSrcweir 236cdf0e10cSrcweir // ----------------------------------------------------------------------- 237cdf0e10cSrcweir 238cdf0e10cSrcweir sal_IntPtr SystemChildWindow::GetParentWindowHandle( sal_Bool bUseJava ) 239cdf0e10cSrcweir { 240cdf0e10cSrcweir sal_IntPtr nRet = 0; 241cdf0e10cSrcweir 242cdf0e10cSrcweir (void)bUseJava; 243cdf0e10cSrcweir #if defined WNT 244cdf0e10cSrcweir nRet = reinterpret_cast< sal_IntPtr >( GetSystemData()->hWnd ); 245cdf0e10cSrcweir #elif defined QUARTZ 246cdf0e10cSrcweir // FIXME: this is wrong 247*bde8a4bdSHerbert Dürr nRet = reinterpret_cast< sal_IntPtr >( GetSystemData()->mpNSView ); 248cdf0e10cSrcweir #elif defined UNX 249cdf0e10cSrcweir if( !bUseJava ) 250cdf0e10cSrcweir { 251cdf0e10cSrcweir nRet = (sal_IntPtr) GetSystemData()->aWindow; 252cdf0e10cSrcweir } 253cdf0e10cSrcweir #ifdef SOLAR_JAVA 254cdf0e10cSrcweir else 255cdf0e10cSrcweir { 256cdf0e10cSrcweir uno::Reference< lang::XMultiServiceFactory > xFactory( vcl::unohelper::GetMultiServiceFactory() ); 257cdf0e10cSrcweir 258cdf0e10cSrcweir if( xFactory.is() && ( GetSystemData()->aWindow > 0 ) ) 259cdf0e10cSrcweir { 260cdf0e10cSrcweir try 261cdf0e10cSrcweir { 262cdf0e10cSrcweir ::rtl::Reference< ::jvmaccess::VirtualMachine > xVM; 263cdf0e10cSrcweir uno::Reference< java::XJavaVM > xJavaVM( xFactory->createInstance( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.java.JavaVirtualMachine") ) ), uno::UNO_QUERY ); 264cdf0e10cSrcweir uno::Sequence< sal_Int8 > aProcessID( 17 ); 265cdf0e10cSrcweir 266cdf0e10cSrcweir rtl_getGlobalProcessId( (sal_uInt8*) aProcessID.getArray() ); 267cdf0e10cSrcweir aProcessID[ 16 ] = 0; 268cdf0e10cSrcweir OSL_ENSURE(sizeof (sal_Int64) >= sizeof (jvmaccess::VirtualMachine *), "Pointer cannot be represented as sal_Int64"); 269cdf0e10cSrcweir sal_Int64 nPointer = reinterpret_cast< sal_Int64 >( static_cast< jvmaccess::VirtualMachine * >(0)); 270cdf0e10cSrcweir xJavaVM->getJavaVM(aProcessID) >>= nPointer; 271cdf0e10cSrcweir xVM = reinterpret_cast< jvmaccess::VirtualMachine * >(nPointer); 272cdf0e10cSrcweir 273cdf0e10cSrcweir if( xVM.is() ) 274cdf0e10cSrcweir { 275cdf0e10cSrcweir try 276cdf0e10cSrcweir { 277cdf0e10cSrcweir ::jvmaccess::VirtualMachine::AttachGuard aVMAttachGuard( xVM ); 278cdf0e10cSrcweir JNIEnv* pEnv = aVMAttachGuard.getEnvironment(); 279cdf0e10cSrcweir 280cdf0e10cSrcweir jclass jcToolkit = pEnv->FindClass("java/awt/Toolkit"); 281cdf0e10cSrcweir ImplTestJavaException(pEnv); 282cdf0e10cSrcweir 283cdf0e10cSrcweir jmethodID jmToolkit_getDefaultToolkit = pEnv->GetStaticMethodID( jcToolkit, "getDefaultToolkit", "()Ljava/awt/Toolkit;" ); 284cdf0e10cSrcweir ImplTestJavaException(pEnv); 285cdf0e10cSrcweir 286cdf0e10cSrcweir pEnv->CallStaticObjectMethod(jcToolkit, jmToolkit_getDefaultToolkit); 287cdf0e10cSrcweir ImplTestJavaException(pEnv); 288cdf0e10cSrcweir 289cdf0e10cSrcweir jclass jcMotifAppletViewer = pEnv->FindClass("sun/plugin/navig/motif/MotifAppletViewer"); 290cdf0e10cSrcweir if( pEnv->ExceptionOccurred() ) 291cdf0e10cSrcweir { 292cdf0e10cSrcweir pEnv->ExceptionClear(); 293cdf0e10cSrcweir 294cdf0e10cSrcweir jcMotifAppletViewer = pEnv->FindClass( "sun/plugin/viewer/MNetscapePluginContext"); 295cdf0e10cSrcweir ImplTestJavaException(pEnv); 296cdf0e10cSrcweir } 297cdf0e10cSrcweir 298cdf0e10cSrcweir jclass jcClassLoader = pEnv->FindClass("java/lang/ClassLoader"); 299cdf0e10cSrcweir ImplTestJavaException(pEnv); 300cdf0e10cSrcweir 301cdf0e10cSrcweir jmethodID jmClassLoader_loadLibrary = pEnv->GetStaticMethodID( jcClassLoader, "loadLibrary", "(Ljava/lang/Class;Ljava/lang/String;Z)V"); 302cdf0e10cSrcweir ImplTestJavaException(pEnv); 303cdf0e10cSrcweir 304cdf0e10cSrcweir jstring jsplugin = pEnv->NewStringUTF("javaplugin_jni"); 305cdf0e10cSrcweir ImplTestJavaException(pEnv); 306cdf0e10cSrcweir 307cdf0e10cSrcweir pEnv->CallStaticVoidMethod(jcClassLoader, jmClassLoader_loadLibrary, jcMotifAppletViewer, jsplugin, JNI_FALSE); 308cdf0e10cSrcweir ImplTestJavaException(pEnv); 309cdf0e10cSrcweir 310cdf0e10cSrcweir jmethodID jmMotifAppletViewer_getWidget = pEnv->GetStaticMethodID( jcMotifAppletViewer, "getWidget", "(IIIII)I" ); 311cdf0e10cSrcweir ImplTestJavaException(pEnv); 312cdf0e10cSrcweir 313cdf0e10cSrcweir const Size aSize( GetOutputSizePixel() ); 314cdf0e10cSrcweir jint ji_widget = pEnv->CallStaticIntMethod( jcMotifAppletViewer, jmMotifAppletViewer_getWidget, 315cdf0e10cSrcweir GetSystemData()->aWindow, 0, 0, aSize.Width(), aSize.Height() ); 316cdf0e10cSrcweir ImplTestJavaException(pEnv); 317cdf0e10cSrcweir 318cdf0e10cSrcweir nRet = static_cast< sal_IntPtr >( ji_widget ); 319cdf0e10cSrcweir } 320cdf0e10cSrcweir catch( uno::RuntimeException& ) 321cdf0e10cSrcweir { 322cdf0e10cSrcweir } 323cdf0e10cSrcweir 324cdf0e10cSrcweir if( !nRet ) 325cdf0e10cSrcweir nRet = static_cast< sal_IntPtr >( GetSystemData()->aWindow ); 326cdf0e10cSrcweir } 327cdf0e10cSrcweir } 328cdf0e10cSrcweir catch( ... ) 329cdf0e10cSrcweir { 330cdf0e10cSrcweir } 331cdf0e10cSrcweir } 332cdf0e10cSrcweir } 333cdf0e10cSrcweir #endif // SOLAR_JAVA 334cdf0e10cSrcweir #else // WNT || QUARTZ || UNX 335cdf0e10cSrcweir #endif 336cdf0e10cSrcweir 337cdf0e10cSrcweir return nRet; 338cdf0e10cSrcweir } 339