Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / vcl / source / window / syschild.cxx
blob9f328b6298b797570900465cda3a8eb85941703f
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 .
20 #include <config_features.h>
22 #include <rtl/process.h>
23 #include <rtl/ref.hxx>
25 #include <tools/rc.h>
27 // declare system types in sysdata.hxx
28 #include <svsys.h>
30 #include <vcl/window.hxx>
31 #include <vcl/sysdata.hxx>
32 #include <vcl/svapp.hxx>
33 #include <vcl/syschild.hxx>
35 #include <window.h>
36 #include <salinst.hxx>
37 #include <salframe.hxx>
38 #include <salobj.hxx>
39 #include <svdata.hxx>
41 #if HAVE_FEATURE_JAVA
42 #include <jni.h>
43 #endif
45 #include <comphelper/processfactory.hxx>
47 #if HAVE_FEATURE_JAVA
48 #include <jvmaccess/virtualmachine.hxx>
49 #include <com/sun/star/java/JavaVirtualMachine.hpp>
50 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
51 #endif
53 using namespace ::com::sun::star;
55 // =======================================================================
57 long ImplSysChildProc( void* pInst, SalObject* /* pObject */,
58 sal_uInt16 nEvent, const void* /* pEvent */ )
60 SystemChildWindow* pWindow = (SystemChildWindow*)pInst;
61 long nRet = 0;
63 ImplDelData aDogTag( pWindow );
64 switch ( nEvent )
66 case SALOBJ_EVENT_GETFOCUS:
67 // Focus holen und zwar so, das alle Handler gerufen
68 // werden, als ob dieses Fenster den Focus bekommt,
69 // ohne das der Frame den Focus wieder klaut
70 pWindow->ImplGetFrameData()->mbSysObjFocus = sal_True;
71 pWindow->ImplGetFrameData()->mbInSysObjToTopHdl = sal_True;
72 pWindow->ToTop( TOTOP_NOGRABFOCUS );
73 if( aDogTag.IsDead() )
74 break;
75 pWindow->ImplGetFrameData()->mbInSysObjToTopHdl = sal_False;
76 pWindow->ImplGetFrameData()->mbInSysObjFocusHdl = sal_True;
77 pWindow->GrabFocus();
78 if( aDogTag.IsDead() )
79 break;
80 pWindow->ImplGetFrameData()->mbInSysObjFocusHdl = sal_False;
81 break;
83 case SALOBJ_EVENT_LOSEFOCUS:
84 // Hintenrum einen LoseFocus ausloesen, das der Status
85 // der Fenster dem entsprechenden Activate-Status
86 // entspricht
87 pWindow->ImplGetFrameData()->mbSysObjFocus = sal_False;
88 if ( !pWindow->ImplGetFrameData()->mnFocusId )
90 pWindow->ImplGetFrameData()->mbStartFocusState = sal_True;
91 Application::PostUserEvent( pWindow->ImplGetFrameData()->mnFocusId, LINK( pWindow->ImplGetFrameWindow(), Window, ImplAsyncFocusHdl ) );
93 break;
95 case SALOBJ_EVENT_TOTOP:
96 pWindow->ImplGetFrameData()->mbInSysObjToTopHdl = sal_True;
97 if ( !Application::GetFocusWindow() || pWindow->HasChildPathFocus() )
98 pWindow->ToTop( TOTOP_NOGRABFOCUS );
99 else
100 pWindow->ToTop();
101 if( aDogTag.IsDead() )
102 break;
103 pWindow->GrabFocus();
104 if( aDogTag.IsDead() )
105 break;
106 pWindow->ImplGetFrameData()->mbInSysObjToTopHdl = sal_False;
107 break;
110 return nRet;
113 // =======================================================================
115 void SystemChildWindow::ImplInitSysChild( Window* pParent, WinBits nStyle, SystemWindowData *pData, sal_Bool bShow )
117 mpWindowImpl->mpSysObj = ImplGetSVData()->mpDefInst->CreateObject( pParent->ImplGetFrame(), pData, bShow );
119 Window::ImplInit( pParent, nStyle, NULL );
121 // Wenn es ein richtiges SysChild ist, dann painten wir auch nicht
122 if ( GetSystemData() )
124 mpWindowImpl->mpSysObj->SetCallback( this, ImplSysChildProc );
125 SetParentClipMode( PARENTCLIPMODE_CLIP );
126 SetBackground();
130 // -----------------------------------------------------------------------
132 SystemChildWindow::SystemChildWindow( Window* pParent, WinBits nStyle ) :
133 Window( WINDOW_SYSTEMCHILDWINDOW )
135 ImplInitSysChild( pParent, nStyle, NULL );
138 // -----------------------------------------------------------------------
140 SystemChildWindow::SystemChildWindow( Window* pParent, WinBits nStyle, SystemWindowData *pData, sal_Bool bShow ) :
141 Window( WINDOW_SYSTEMCHILDWINDOW )
143 ImplInitSysChild( pParent, nStyle, pData, bShow );
146 // -----------------------------------------------------------------------
148 SystemChildWindow::~SystemChildWindow()
150 Hide();
151 if ( mpWindowImpl->mpSysObj )
153 ImplGetSVData()->mpDefInst->DestroyObject( mpWindowImpl->mpSysObj );
154 mpWindowImpl->mpSysObj = NULL;
158 // -----------------------------------------------------------------------
160 const SystemEnvData* SystemChildWindow::GetSystemData() const
162 if ( mpWindowImpl->mpSysObj )
163 return mpWindowImpl->mpSysObj->GetSystemData();
164 else
165 return NULL;
168 // -----------------------------------------------------------------------
170 void SystemChildWindow::EnableEraseBackground( sal_Bool bEnable )
172 if ( mpWindowImpl->mpSysObj )
173 mpWindowImpl->mpSysObj->EnableEraseBackground( bEnable );
176 // -----------------------------------------------------------------------
178 void SystemChildWindow::ImplTestJavaException( void* pEnv )
180 #if HAVE_FEATURE_JAVA
181 JNIEnv* pJavaEnv = reinterpret_cast< JNIEnv* >( pEnv );
182 jthrowable jtThrowable = pJavaEnv->ExceptionOccurred();
184 if( jtThrowable )
185 { // is it a java exception ?
186 #if OSL_DEBUG_LEVEL > 1
187 pJavaEnv->ExceptionDescribe();
188 #endif // OSL_DEBUG_LEVEL > 1
189 pJavaEnv->ExceptionClear();
191 jclass jcThrowable = pJavaEnv->FindClass("java/lang/Throwable");
192 jmethodID jmThrowable_getMessage = pJavaEnv->GetMethodID(jcThrowable, "getMessage", "()Ljava/lang/String;");
193 jstring jsMessage = (jstring) pJavaEnv->CallObjectMethod(jtThrowable, jmThrowable_getMessage);
194 OUString ouMessage;
196 if(jsMessage)
198 const jchar * jcMessage = pJavaEnv->GetStringChars(jsMessage, NULL);
199 ouMessage = OUString(jcMessage);
200 pJavaEnv->ReleaseStringChars(jsMessage, jcMessage);
203 throw uno::RuntimeException(ouMessage, uno::Reference<uno::XInterface>());
205 #else
206 (void)pEnv;
207 #endif // HAVE_FEATURE_JAVA
210 void SystemChildWindow::SetForwardKey( sal_Bool bEnable )
212 if ( mpWindowImpl->mpSysObj )
213 mpWindowImpl->mpSysObj->SetForwardKey( bEnable );
216 // -----------------------------------------------------------------------
218 sal_IntPtr SystemChildWindow::GetParentWindowHandle( sal_Bool bUseJava )
220 sal_IntPtr nRet = 0;
222 (void)bUseJava;
223 #if defined WNT
224 nRet = reinterpret_cast< sal_IntPtr >( GetSystemData()->hWnd );
225 #elif defined MACOSX
226 // FIXME: this is wrong
227 nRet = reinterpret_cast< sal_IntPtr >( GetSystemData()->pView );
228 #elif defined ANDROID
229 // Nothing
230 #elif defined IOS
231 // Nothing
232 #elif defined UNX
233 if( !bUseJava )
235 nRet = (sal_IntPtr) GetSystemData()->aWindow;
237 #if HAVE_FEATURE_JAVA
238 else
240 uno::Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext() );
242 if( GetSystemData()->aWindow > 0 )
246 ::rtl::Reference< ::jvmaccess::VirtualMachine > xVM;
247 uno::Reference< java::XJavaVM > xJavaVM = java::JavaVirtualMachine::create(xContext);;
248 uno::Sequence< sal_Int8 > aProcessID( 17 );
250 rtl_getGlobalProcessId( (sal_uInt8*) aProcessID.getArray() );
251 aProcessID[ 16 ] = 0;
252 OSL_ENSURE(sizeof (sal_Int64) >= sizeof (jvmaccess::VirtualMachine *), "Pointer cannot be represented as sal_Int64");
253 sal_Int64 nPointer = reinterpret_cast< sal_Int64 >( static_cast< jvmaccess::VirtualMachine * >(0));
254 xJavaVM->getJavaVM(aProcessID) >>= nPointer;
255 xVM = reinterpret_cast< jvmaccess::VirtualMachine * >(nPointer);
257 if( xVM.is() )
261 ::jvmaccess::VirtualMachine::AttachGuard aVMAttachGuard( xVM );
262 JNIEnv* pEnv = aVMAttachGuard.getEnvironment();
264 jclass jcToolkit = pEnv->FindClass("java/awt/Toolkit");
265 ImplTestJavaException(pEnv);
267 jmethodID jmToolkit_getDefaultToolkit = pEnv->GetStaticMethodID( jcToolkit, "getDefaultToolkit", "()Ljava/awt/Toolkit;" );
268 ImplTestJavaException(pEnv);
270 pEnv->CallStaticObjectMethod(jcToolkit, jmToolkit_getDefaultToolkit);
271 ImplTestJavaException(pEnv);
273 jclass jcMotifAppletViewer = pEnv->FindClass("sun/plugin/navig/motif/MotifAppletViewer");
274 if( pEnv->ExceptionOccurred() )
276 pEnv->ExceptionClear();
278 jcMotifAppletViewer = pEnv->FindClass( "sun/plugin/viewer/MNetscapePluginContext");
279 ImplTestJavaException(pEnv);
282 jclass jcClassLoader = pEnv->FindClass("java/lang/ClassLoader");
283 ImplTestJavaException(pEnv);
285 jmethodID jmClassLoader_loadLibrary = pEnv->GetStaticMethodID( jcClassLoader, "loadLibrary", "(Ljava/lang/Class;Ljava/lang/String;Z)V");
286 ImplTestJavaException(pEnv);
288 jstring jsplugin = pEnv->NewStringUTF("javaplugin_jni");
289 ImplTestJavaException(pEnv);
291 pEnv->CallStaticVoidMethod(jcClassLoader, jmClassLoader_loadLibrary, jcMotifAppletViewer, jsplugin, JNI_FALSE);
292 ImplTestJavaException(pEnv);
294 jmethodID jmMotifAppletViewer_getWidget = pEnv->GetStaticMethodID( jcMotifAppletViewer, "getWidget", "(IIIII)I" );
295 ImplTestJavaException(pEnv);
297 const Size aSize( GetOutputSizePixel() );
298 jint ji_widget = pEnv->CallStaticIntMethod( jcMotifAppletViewer, jmMotifAppletViewer_getWidget,
299 GetSystemData()->aWindow, 0, 0, aSize.Width(), aSize.Height() );
300 ImplTestJavaException(pEnv);
302 nRet = static_cast< sal_IntPtr >( ji_widget );
304 catch( uno::RuntimeException& )
308 if( !nRet )
309 nRet = static_cast< sal_IntPtr >( GetSystemData()->aWindow );
312 catch( ... )
317 #endif // HAVE_FEATURE_JAVA
318 #endif
320 return nRet;
323 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */