Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include <rtl/process.h>
22 : #include <rtl/ref.hxx>
23 :
24 : #include <tools/rc.h>
25 :
26 : // declare system types in sysdata.hxx
27 : #include <svsys.h>
28 :
29 : #include <vcl/window.hxx>
30 : #include <vcl/sysdata.hxx>
31 : #include <vcl/svapp.hxx>
32 : #include <vcl/syschild.hxx>
33 :
34 : #include <window.h>
35 : #include <salinst.hxx>
36 : #include <salframe.hxx>
37 : #include <salobj.hxx>
38 : #include <svdata.hxx>
39 :
40 : #ifdef SOLAR_JAVA
41 : #include <jni.h>
42 : #endif
43 :
44 : #include <comphelper/processfactory.hxx>
45 :
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
51 :
52 : using namespace ::com::sun::star;
53 :
54 : // =======================================================================
55 :
56 0 : long ImplSysChildProc( void* pInst, SalObject* /* pObject */,
57 : sal_uInt16 nEvent, const void* /* pEvent */ )
58 : {
59 0 : SystemChildWindow* pWindow = (SystemChildWindow*)pInst;
60 0 : long nRet = 0;
61 :
62 0 : ImplDelData aDogTag( pWindow );
63 0 : switch ( nEvent )
64 : {
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 0 : pWindow->ImplGetFrameData()->mbSysObjFocus = sal_True;
70 0 : pWindow->ImplGetFrameData()->mbInSysObjToTopHdl = sal_True;
71 0 : pWindow->ToTop( TOTOP_NOGRABFOCUS );
72 0 : if( aDogTag.IsDead() )
73 0 : break;
74 0 : pWindow->ImplGetFrameData()->mbInSysObjToTopHdl = sal_False;
75 0 : pWindow->ImplGetFrameData()->mbInSysObjFocusHdl = sal_True;
76 0 : pWindow->GrabFocus();
77 0 : if( aDogTag.IsDead() )
78 0 : break;
79 0 : pWindow->ImplGetFrameData()->mbInSysObjFocusHdl = sal_False;
80 0 : break;
81 :
82 : case SALOBJ_EVENT_LOSEFOCUS:
83 : // Hintenrum einen LoseFocus ausloesen, das der Status
84 : // der Fenster dem entsprechenden Activate-Status
85 : // entspricht
86 0 : pWindow->ImplGetFrameData()->mbSysObjFocus = sal_False;
87 0 : if ( !pWindow->ImplGetFrameData()->mnFocusId )
88 : {
89 0 : pWindow->ImplGetFrameData()->mbStartFocusState = sal_True;
90 0 : Application::PostUserEvent( pWindow->ImplGetFrameData()->mnFocusId, LINK( pWindow->ImplGetFrameWindow(), Window, ImplAsyncFocusHdl ) );
91 : }
92 0 : break;
93 :
94 : case SALOBJ_EVENT_TOTOP:
95 0 : pWindow->ImplGetFrameData()->mbInSysObjToTopHdl = sal_True;
96 0 : if ( !Application::GetFocusWindow() || pWindow->HasChildPathFocus() )
97 0 : pWindow->ToTop( TOTOP_NOGRABFOCUS );
98 : else
99 0 : pWindow->ToTop();
100 0 : if( aDogTag.IsDead() )
101 0 : break;
102 0 : pWindow->GrabFocus();
103 0 : if( aDogTag.IsDead() )
104 0 : break;
105 0 : pWindow->ImplGetFrameData()->mbInSysObjToTopHdl = sal_False;
106 0 : break;
107 : }
108 :
109 0 : return nRet;
110 : }
111 :
112 : // =======================================================================
113 :
114 0 : void SystemChildWindow::ImplInitSysChild( Window* pParent, WinBits nStyle, SystemWindowData *pData, sal_Bool bShow )
115 : {
116 0 : mpWindowImpl->mpSysObj = ImplGetSVData()->mpDefInst->CreateObject( pParent->ImplGetFrame(), pData, bShow );
117 :
118 0 : Window::ImplInit( pParent, nStyle, NULL );
119 :
120 : // Wenn es ein richtiges SysChild ist, dann painten wir auch nicht
121 0 : if ( GetSystemData() )
122 : {
123 0 : mpWindowImpl->mpSysObj->SetCallback( this, ImplSysChildProc );
124 0 : SetParentClipMode( PARENTCLIPMODE_CLIP );
125 0 : SetBackground();
126 : }
127 0 : }
128 :
129 : // -----------------------------------------------------------------------
130 :
131 0 : SystemChildWindow::SystemChildWindow( Window* pParent, WinBits nStyle ) :
132 0 : Window( WINDOW_SYSTEMCHILDWINDOW )
133 : {
134 0 : ImplInitSysChild( pParent, nStyle, NULL );
135 0 : }
136 :
137 : // -----------------------------------------------------------------------
138 :
139 0 : SystemChildWindow::SystemChildWindow( Window* pParent, WinBits nStyle, SystemWindowData *pData, sal_Bool bShow ) :
140 0 : Window( WINDOW_SYSTEMCHILDWINDOW )
141 : {
142 0 : ImplInitSysChild( pParent, nStyle, pData, bShow );
143 0 : }
144 :
145 : // -----------------------------------------------------------------------
146 :
147 0 : SystemChildWindow::~SystemChildWindow()
148 : {
149 0 : Hide();
150 0 : if ( mpWindowImpl->mpSysObj )
151 : {
152 0 : ImplGetSVData()->mpDefInst->DestroyObject( mpWindowImpl->mpSysObj );
153 0 : mpWindowImpl->mpSysObj = NULL;
154 : }
155 0 : }
156 :
157 : // -----------------------------------------------------------------------
158 :
159 0 : const SystemEnvData* SystemChildWindow::GetSystemData() const
160 : {
161 0 : if ( mpWindowImpl->mpSysObj )
162 0 : return mpWindowImpl->mpSysObj->GetSystemData();
163 : else
164 0 : return NULL;
165 : }
166 :
167 : // -----------------------------------------------------------------------
168 :
169 0 : void SystemChildWindow::EnableEraseBackground( sal_Bool bEnable )
170 : {
171 0 : if ( mpWindowImpl->mpSysObj )
172 0 : mpWindowImpl->mpSysObj->EnableEraseBackground( bEnable );
173 0 : }
174 :
175 : // -----------------------------------------------------------------------
176 :
177 0 : void SystemChildWindow::ImplTestJavaException( void* pEnv )
178 : {
179 : #ifdef SOLAR_JAVA
180 0 : JNIEnv* pJavaEnv = reinterpret_cast< JNIEnv* >( pEnv );
181 0 : jthrowable jtThrowable = pJavaEnv->ExceptionOccurred();
182 :
183 0 : if( jtThrowable )
184 : { // is it a java exception ?
185 : #if OSL_DEBUG_LEVEL > 1
186 : pJavaEnv->ExceptionDescribe();
187 : #endif // OSL_DEBUG_LEVEL > 1
188 0 : pJavaEnv->ExceptionClear();
189 :
190 0 : jclass jcThrowable = pJavaEnv->FindClass("java/lang/Throwable");
191 0 : jmethodID jmThrowable_getMessage = pJavaEnv->GetMethodID(jcThrowable, "getMessage", "()Ljava/lang/String;");
192 0 : jstring jsMessage = (jstring) pJavaEnv->CallObjectMethod(jtThrowable, jmThrowable_getMessage);
193 0 : OUString ouMessage;
194 :
195 0 : if(jsMessage)
196 : {
197 0 : const jchar * jcMessage = pJavaEnv->GetStringChars(jsMessage, NULL);
198 0 : ouMessage = OUString(jcMessage);
199 0 : pJavaEnv->ReleaseStringChars(jsMessage, jcMessage);
200 : }
201 :
202 0 : throw uno::RuntimeException(ouMessage, uno::Reference<uno::XInterface>());
203 : }
204 : #else
205 : (void)pEnv;
206 : #endif // SOLAR_JAVA
207 0 : }
208 :
209 0 : void SystemChildWindow::SetForwardKey( sal_Bool bEnable )
210 : {
211 0 : if ( mpWindowImpl->mpSysObj )
212 0 : mpWindowImpl->mpSysObj->SetForwardKey( bEnable );
213 0 : }
214 :
215 : // -----------------------------------------------------------------------
216 :
217 0 : sal_IntPtr SystemChildWindow::GetParentWindowHandle( sal_Bool bUseJava )
218 : {
219 0 : sal_IntPtr nRet = 0;
220 :
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 0 : if( !bUseJava )
233 : {
234 0 : nRet = (sal_IntPtr) GetSystemData()->aWindow;
235 : }
236 : #ifdef SOLAR_JAVA
237 : else
238 : {
239 0 : uno::Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext() );
240 :
241 0 : if( GetSystemData()->aWindow > 0 )
242 : {
243 : try
244 : {
245 0 : ::rtl::Reference< ::jvmaccess::VirtualMachine > xVM;
246 0 : uno::Reference< java::XJavaVM > xJavaVM = java::JavaVirtualMachine::create(xContext);;
247 0 : uno::Sequence< sal_Int8 > aProcessID( 17 );
248 :
249 0 : rtl_getGlobalProcessId( (sal_uInt8*) aProcessID.getArray() );
250 0 : aProcessID[ 16 ] = 0;
251 : OSL_ENSURE(sizeof (sal_Int64) >= sizeof (jvmaccess::VirtualMachine *), "Pointer cannot be represented as sal_Int64");
252 0 : sal_Int64 nPointer = reinterpret_cast< sal_Int64 >( static_cast< jvmaccess::VirtualMachine * >(0));
253 0 : xJavaVM->getJavaVM(aProcessID) >>= nPointer;
254 0 : xVM = reinterpret_cast< jvmaccess::VirtualMachine * >(nPointer);
255 :
256 0 : if( xVM.is() )
257 : {
258 : try
259 : {
260 0 : ::jvmaccess::VirtualMachine::AttachGuard aVMAttachGuard( xVM );
261 0 : JNIEnv* pEnv = aVMAttachGuard.getEnvironment();
262 :
263 0 : jclass jcToolkit = pEnv->FindClass("java/awt/Toolkit");
264 0 : ImplTestJavaException(pEnv);
265 :
266 0 : jmethodID jmToolkit_getDefaultToolkit = pEnv->GetStaticMethodID( jcToolkit, "getDefaultToolkit", "()Ljava/awt/Toolkit;" );
267 0 : ImplTestJavaException(pEnv);
268 :
269 0 : pEnv->CallStaticObjectMethod(jcToolkit, jmToolkit_getDefaultToolkit);
270 0 : ImplTestJavaException(pEnv);
271 :
272 0 : jclass jcMotifAppletViewer = pEnv->FindClass("sun/plugin/navig/motif/MotifAppletViewer");
273 0 : if( pEnv->ExceptionOccurred() )
274 : {
275 0 : pEnv->ExceptionClear();
276 :
277 0 : jcMotifAppletViewer = pEnv->FindClass( "sun/plugin/viewer/MNetscapePluginContext");
278 0 : ImplTestJavaException(pEnv);
279 : }
280 :
281 0 : jclass jcClassLoader = pEnv->FindClass("java/lang/ClassLoader");
282 0 : ImplTestJavaException(pEnv);
283 :
284 0 : jmethodID jmClassLoader_loadLibrary = pEnv->GetStaticMethodID( jcClassLoader, "loadLibrary", "(Ljava/lang/Class;Ljava/lang/String;Z)V");
285 0 : ImplTestJavaException(pEnv);
286 :
287 0 : jstring jsplugin = pEnv->NewStringUTF("javaplugin_jni");
288 0 : ImplTestJavaException(pEnv);
289 :
290 0 : pEnv->CallStaticVoidMethod(jcClassLoader, jmClassLoader_loadLibrary, jcMotifAppletViewer, jsplugin, JNI_FALSE);
291 0 : ImplTestJavaException(pEnv);
292 :
293 0 : jmethodID jmMotifAppletViewer_getWidget = pEnv->GetStaticMethodID( jcMotifAppletViewer, "getWidget", "(IIIII)I" );
294 0 : ImplTestJavaException(pEnv);
295 :
296 0 : const Size aSize( GetOutputSizePixel() );
297 : jint ji_widget = pEnv->CallStaticIntMethod( jcMotifAppletViewer, jmMotifAppletViewer_getWidget,
298 0 : GetSystemData()->aWindow, 0, 0, aSize.Width(), aSize.Height() );
299 0 : ImplTestJavaException(pEnv);
300 :
301 0 : nRet = static_cast< sal_IntPtr >( ji_widget );
302 : }
303 0 : catch( uno::RuntimeException& )
304 : {
305 : }
306 :
307 0 : if( !nRet )
308 0 : nRet = static_cast< sal_IntPtr >( GetSystemData()->aWindow );
309 0 : }
310 : }
311 0 : catch( ... )
312 : {
313 : }
314 0 : }
315 : }
316 : #endif // SOLAR_JAVA
317 : #endif
318 :
319 0 : return nRet;
320 465 : }
321 :
322 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|