1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
27 // declare system types in sysdata.hxx
30 #include <vcl/window.hxx>
31 #include <vcl/sysdata.hxx>
32 #include <vcl/svapp.hxx>
33 #include <vcl/syschild.hxx>
36 #include <salinst.hxx>
37 #include <salframe.hxx>
45 #include <comphelper/processfactory.hxx>
48 #include <jvmaccess/virtualmachine.hxx>
49 #include <com/sun/star/java/JavaVirtualMachine.hpp>
50 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
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
;
63 ImplDelData
aDogTag( pWindow
);
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() )
75 pWindow
->ImplGetFrameData()->mbInSysObjToTopHdl
= sal_False
;
76 pWindow
->ImplGetFrameData()->mbInSysObjFocusHdl
= sal_True
;
78 if( aDogTag
.IsDead() )
80 pWindow
->ImplGetFrameData()->mbInSysObjFocusHdl
= sal_False
;
83 case SALOBJ_EVENT_LOSEFOCUS
:
84 // Hintenrum einen LoseFocus ausloesen, das der Status
85 // der Fenster dem entsprechenden Activate-Status
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
) );
95 case SALOBJ_EVENT_TOTOP
:
96 pWindow
->ImplGetFrameData()->mbInSysObjToTopHdl
= sal_True
;
97 if ( !Application::GetFocusWindow() || pWindow
->HasChildPathFocus() )
98 pWindow
->ToTop( TOTOP_NOGRABFOCUS
);
101 if( aDogTag
.IsDead() )
103 pWindow
->GrabFocus();
104 if( aDogTag
.IsDead() )
106 pWindow
->ImplGetFrameData()->mbInSysObjToTopHdl
= sal_False
;
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
);
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()
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();
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();
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
);
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
>());
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
)
224 nRet
= reinterpret_cast< sal_IntPtr
>( GetSystemData()->hWnd
);
226 // FIXME: this is wrong
227 nRet
= reinterpret_cast< sal_IntPtr
>( GetSystemData()->pView
);
228 #elif defined ANDROID
235 nRet
= (sal_IntPtr
) GetSystemData()->aWindow
;
237 #if HAVE_FEATURE_JAVA
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
);
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
& )
309 nRet
= static_cast< sal_IntPtr
>( GetSystemData()->aWindow
);
317 #endif // HAVE_FEATURE_JAVA
323 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */