bump product version to 4.1.6.2
[LibreOffice.git] / vcl / source / window / syschild.cxx
blob0a98fc2f4b571419a108fb0b8f2540a72beeeea5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <rtl/process.h>
22 #include <rtl/ref.hxx>
24 #include <tools/rc.h>
26 // declare system types in sysdata.hxx
27 #include <svsys.h>
29 #include <vcl/window.hxx>
30 #include <vcl/sysdata.hxx>
31 #include <vcl/svapp.hxx>
32 #include <vcl/syschild.hxx>
34 #include <window.h>
35 #include <salinst.hxx>
36 #include <salframe.hxx>
37 #include <salobj.hxx>
38 #include <svdata.hxx>
40 #ifdef SOLAR_JAVA
41 #include <jni.h>
42 #endif
44 #include <comphelper/processfactory.hxx>
46 #ifdef SOLAR_JAVA
47 #include <jvmaccess/virtualmachine.hxx>
48 #include <com/sun/star/java/JavaVirtualMachine.hpp>
49 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
50 #endif
52 using namespace ::com::sun::star;
54 // =======================================================================
56 long ImplSysChildProc( void* pInst, SalObject* /* pObject */,
57 sal_uInt16 nEvent, const void* /* pEvent */ )
59 SystemChildWindow* pWindow = (SystemChildWindow*)pInst;
60 long nRet = 0;
62 ImplDelData aDogTag( pWindow );
63 switch ( nEvent )
65 case SALOBJ_EVENT_GETFOCUS:
66 // Focus holen und zwar so, das alle Handler gerufen
67 // werden, als ob dieses Fenster den Focus bekommt,
68 // ohne das der Frame den Focus wieder klaut
69 pWindow->ImplGetFrameData()->mbSysObjFocus = sal_True;
70 pWindow->ImplGetFrameData()->mbInSysObjToTopHdl = sal_True;
71 pWindow->ToTop( TOTOP_NOGRABFOCUS );
72 if( aDogTag.IsDead() )
73 break;
74 pWindow->ImplGetFrameData()->mbInSysObjToTopHdl = sal_False;
75 pWindow->ImplGetFrameData()->mbInSysObjFocusHdl = sal_True;
76 pWindow->GrabFocus();
77 if( aDogTag.IsDead() )
78 break;
79 pWindow->ImplGetFrameData()->mbInSysObjFocusHdl = sal_False;
80 break;
82 case SALOBJ_EVENT_LOSEFOCUS:
83 // Hintenrum einen LoseFocus ausloesen, das der Status
84 // der Fenster dem entsprechenden Activate-Status
85 // entspricht
86 pWindow->ImplGetFrameData()->mbSysObjFocus = sal_False;
87 if ( !pWindow->ImplGetFrameData()->mnFocusId )
89 pWindow->ImplGetFrameData()->mbStartFocusState = sal_True;
90 Application::PostUserEvent( pWindow->ImplGetFrameData()->mnFocusId, LINK( pWindow->ImplGetFrameWindow(), Window, ImplAsyncFocusHdl ) );
92 break;
94 case SALOBJ_EVENT_TOTOP:
95 pWindow->ImplGetFrameData()->mbInSysObjToTopHdl = sal_True;
96 if ( !Application::GetFocusWindow() || pWindow->HasChildPathFocus() )
97 pWindow->ToTop( TOTOP_NOGRABFOCUS );
98 else
99 pWindow->ToTop();
100 if( aDogTag.IsDead() )
101 break;
102 pWindow->GrabFocus();
103 if( aDogTag.IsDead() )
104 break;
105 pWindow->ImplGetFrameData()->mbInSysObjToTopHdl = sal_False;
106 break;
109 return nRet;
112 // =======================================================================
114 void SystemChildWindow::ImplInitSysChild( Window* pParent, WinBits nStyle, SystemWindowData *pData, sal_Bool bShow )
116 mpWindowImpl->mpSysObj = ImplGetSVData()->mpDefInst->CreateObject( pParent->ImplGetFrame(), pData, bShow );
118 Window::ImplInit( pParent, nStyle, NULL );
120 // Wenn es ein richtiges SysChild ist, dann painten wir auch nicht
121 if ( GetSystemData() )
123 mpWindowImpl->mpSysObj->SetCallback( this, ImplSysChildProc );
124 SetParentClipMode( PARENTCLIPMODE_CLIP );
125 SetBackground();
129 // -----------------------------------------------------------------------
131 SystemChildWindow::SystemChildWindow( Window* pParent, WinBits nStyle ) :
132 Window( WINDOW_SYSTEMCHILDWINDOW )
134 ImplInitSysChild( pParent, nStyle, NULL );
137 // -----------------------------------------------------------------------
139 SystemChildWindow::SystemChildWindow( Window* pParent, WinBits nStyle, SystemWindowData *pData, sal_Bool bShow ) :
140 Window( WINDOW_SYSTEMCHILDWINDOW )
142 ImplInitSysChild( pParent, nStyle, pData, bShow );
145 // -----------------------------------------------------------------------
147 SystemChildWindow::~SystemChildWindow()
149 Hide();
150 if ( mpWindowImpl->mpSysObj )
152 ImplGetSVData()->mpDefInst->DestroyObject( mpWindowImpl->mpSysObj );
153 mpWindowImpl->mpSysObj = NULL;
157 // -----------------------------------------------------------------------
159 const SystemEnvData* SystemChildWindow::GetSystemData() const
161 if ( mpWindowImpl->mpSysObj )
162 return mpWindowImpl->mpSysObj->GetSystemData();
163 else
164 return NULL;
167 // -----------------------------------------------------------------------
169 void SystemChildWindow::EnableEraseBackground( sal_Bool bEnable )
171 if ( mpWindowImpl->mpSysObj )
172 mpWindowImpl->mpSysObj->EnableEraseBackground( bEnable );
175 // -----------------------------------------------------------------------
177 void SystemChildWindow::ImplTestJavaException( void* pEnv )
179 #ifdef SOLAR_JAVA
180 JNIEnv* pJavaEnv = reinterpret_cast< JNIEnv* >( pEnv );
181 jthrowable jtThrowable = pJavaEnv->ExceptionOccurred();
183 if( jtThrowable )
184 { // is it a java exception ?
185 #if OSL_DEBUG_LEVEL > 1
186 pJavaEnv->ExceptionDescribe();
187 #endif // OSL_DEBUG_LEVEL > 1
188 pJavaEnv->ExceptionClear();
190 jclass jcThrowable = pJavaEnv->FindClass("java/lang/Throwable");
191 jmethodID jmThrowable_getMessage = pJavaEnv->GetMethodID(jcThrowable, "getMessage", "()Ljava/lang/String;");
192 jstring jsMessage = (jstring) pJavaEnv->CallObjectMethod(jtThrowable, jmThrowable_getMessage);
193 OUString ouMessage;
195 if(jsMessage)
197 const jchar * jcMessage = pJavaEnv->GetStringChars(jsMessage, NULL);
198 ouMessage = OUString(jcMessage);
199 pJavaEnv->ReleaseStringChars(jsMessage, jcMessage);
202 throw uno::RuntimeException(ouMessage, uno::Reference<uno::XInterface>());
204 #else
205 (void)pEnv;
206 #endif // SOLAR_JAVA
209 void SystemChildWindow::SetForwardKey( sal_Bool bEnable )
211 if ( mpWindowImpl->mpSysObj )
212 mpWindowImpl->mpSysObj->SetForwardKey( bEnable );
215 // -----------------------------------------------------------------------
217 sal_IntPtr SystemChildWindow::GetParentWindowHandle( sal_Bool bUseJava )
219 sal_IntPtr nRet = 0;
221 (void)bUseJava;
222 #if defined WNT
223 nRet = reinterpret_cast< sal_IntPtr >( GetSystemData()->hWnd );
224 #elif defined MACOSX
225 // FIXME: this is wrong
226 nRet = reinterpret_cast< sal_IntPtr >( GetSystemData()->pView );
227 #elif defined ANDROID
228 // Nothing
229 #elif defined IOS
230 // Nothing
231 #elif defined UNX
232 if( !bUseJava )
234 nRet = (sal_IntPtr) GetSystemData()->aWindow;
236 #ifdef SOLAR_JAVA
237 else
239 uno::Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext() );
241 if( GetSystemData()->aWindow > 0 )
245 ::rtl::Reference< ::jvmaccess::VirtualMachine > xVM;
246 uno::Reference< java::XJavaVM > xJavaVM = java::JavaVirtualMachine::create(xContext);;
247 uno::Sequence< sal_Int8 > aProcessID( 17 );
249 rtl_getGlobalProcessId( (sal_uInt8*) aProcessID.getArray() );
250 aProcessID[ 16 ] = 0;
251 OSL_ENSURE(sizeof (sal_Int64) >= sizeof (jvmaccess::VirtualMachine *), "Pointer cannot be represented as sal_Int64");
252 sal_Int64 nPointer = reinterpret_cast< sal_Int64 >( static_cast< jvmaccess::VirtualMachine * >(0));
253 xJavaVM->getJavaVM(aProcessID) >>= nPointer;
254 xVM = reinterpret_cast< jvmaccess::VirtualMachine * >(nPointer);
256 if( xVM.is() )
260 ::jvmaccess::VirtualMachine::AttachGuard aVMAttachGuard( xVM );
261 JNIEnv* pEnv = aVMAttachGuard.getEnvironment();
263 jclass jcToolkit = pEnv->FindClass("java/awt/Toolkit");
264 ImplTestJavaException(pEnv);
266 jmethodID jmToolkit_getDefaultToolkit = pEnv->GetStaticMethodID( jcToolkit, "getDefaultToolkit", "()Ljava/awt/Toolkit;" );
267 ImplTestJavaException(pEnv);
269 pEnv->CallStaticObjectMethod(jcToolkit, jmToolkit_getDefaultToolkit);
270 ImplTestJavaException(pEnv);
272 jclass jcMotifAppletViewer = pEnv->FindClass("sun/plugin/navig/motif/MotifAppletViewer");
273 if( pEnv->ExceptionOccurred() )
275 pEnv->ExceptionClear();
277 jcMotifAppletViewer = pEnv->FindClass( "sun/plugin/viewer/MNetscapePluginContext");
278 ImplTestJavaException(pEnv);
281 jclass jcClassLoader = pEnv->FindClass("java/lang/ClassLoader");
282 ImplTestJavaException(pEnv);
284 jmethodID jmClassLoader_loadLibrary = pEnv->GetStaticMethodID( jcClassLoader, "loadLibrary", "(Ljava/lang/Class;Ljava/lang/String;Z)V");
285 ImplTestJavaException(pEnv);
287 jstring jsplugin = pEnv->NewStringUTF("javaplugin_jni");
288 ImplTestJavaException(pEnv);
290 pEnv->CallStaticVoidMethod(jcClassLoader, jmClassLoader_loadLibrary, jcMotifAppletViewer, jsplugin, JNI_FALSE);
291 ImplTestJavaException(pEnv);
293 jmethodID jmMotifAppletViewer_getWidget = pEnv->GetStaticMethodID( jcMotifAppletViewer, "getWidget", "(IIIII)I" );
294 ImplTestJavaException(pEnv);
296 const Size aSize( GetOutputSizePixel() );
297 jint ji_widget = pEnv->CallStaticIntMethod( jcMotifAppletViewer, jmMotifAppletViewer_getWidget,
298 GetSystemData()->aWindow, 0, 0, aSize.Width(), aSize.Height() );
299 ImplTestJavaException(pEnv);
301 nRet = static_cast< sal_IntPtr >( ji_widget );
303 catch( uno::RuntimeException& )
307 if( !nRet )
308 nRet = static_cast< sal_IntPtr >( GetSystemData()->aWindow );
311 catch( ... )
316 #endif // SOLAR_JAVA
317 #endif
319 return nRet;
322 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */