Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / vcl / source / window / syschild.cxx
blob752bf43009bc9a66f166475e0d92951a266132bd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <rtl/process.h>
31 #include <rtl/ref.hxx>
33 #include <tools/rc.h>
35 // declare system types in sysdata.hxx
36 #include <svsys.h>
38 #include <vcl/window.hxx>
39 #include <vcl/sysdata.hxx>
40 #include <vcl/svapp.hxx>
41 #include <vcl/syschild.hxx>
42 #include <vcl/unohelp.hxx>
44 #include <window.h>
45 #include <salinst.hxx>
46 #include <salframe.hxx>
47 #include <salobj.hxx>
48 #include <svdata.hxx>
50 #ifdef SOLAR_JAVA
51 #include <jni.h>
52 #endif
54 #include <comphelper/processfactory.hxx>
55 #include <jvmaccess/virtualmachine.hxx>
56 #include <com/sun/star/java/XJavaVM.hpp>
57 #include <com/sun/star/java/XJavaThreadRegister_11.hpp>
58 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
60 using namespace ::com::sun::star;
62 // =======================================================================
64 long ImplSysChildProc( void* pInst, SalObject* /* pObject */,
65 sal_uInt16 nEvent, const void* /* pEvent */ )
67 SystemChildWindow* pWindow = (SystemChildWindow*)pInst;
68 long nRet = 0;
70 ImplDelData aDogTag( pWindow );
71 switch ( nEvent )
73 case SALOBJ_EVENT_GETFOCUS:
74 // Focus holen und zwar so, das alle Handler gerufen
75 // werden, als ob dieses Fenster den Focus bekommt,
76 // ohne das der Frame den Focus wieder klaut
77 pWindow->ImplGetFrameData()->mbSysObjFocus = sal_True;
78 pWindow->ImplGetFrameData()->mbInSysObjToTopHdl = sal_True;
79 pWindow->ToTop( TOTOP_NOGRABFOCUS );
80 if( aDogTag.IsDead() )
81 break;
82 pWindow->ImplGetFrameData()->mbInSysObjToTopHdl = sal_False;
83 pWindow->ImplGetFrameData()->mbInSysObjFocusHdl = sal_True;
84 pWindow->GrabFocus();
85 if( aDogTag.IsDead() )
86 break;
87 pWindow->ImplGetFrameData()->mbInSysObjFocusHdl = sal_False;
88 break;
90 case SALOBJ_EVENT_LOSEFOCUS:
91 // Hintenrum einen LoseFocus ausloesen, das der Status
92 // der Fenster dem entsprechenden Activate-Status
93 // entspricht
94 pWindow->ImplGetFrameData()->mbSysObjFocus = sal_False;
95 if ( !pWindow->ImplGetFrameData()->mnFocusId )
97 pWindow->ImplGetFrameData()->mbStartFocusState = sal_True;
98 Application::PostUserEvent( pWindow->ImplGetFrameData()->mnFocusId, LINK( pWindow->ImplGetFrameWindow(), Window, ImplAsyncFocusHdl ) );
100 break;
102 case SALOBJ_EVENT_TOTOP:
103 pWindow->ImplGetFrameData()->mbInSysObjToTopHdl = sal_True;
104 if ( !Application::GetFocusWindow() || pWindow->HasChildPathFocus() )
105 pWindow->ToTop( TOTOP_NOGRABFOCUS );
106 else
107 pWindow->ToTop();
108 if( aDogTag.IsDead() )
109 break;
110 pWindow->GrabFocus();
111 if( aDogTag.IsDead() )
112 break;
113 pWindow->ImplGetFrameData()->mbInSysObjToTopHdl = sal_False;
114 break;
117 return nRet;
120 // =======================================================================
122 void SystemChildWindow::ImplInitSysChild( Window* pParent, WinBits nStyle, SystemWindowData *pData, sal_Bool bShow )
124 mpWindowImpl->mpSysObj = ImplGetSVData()->mpDefInst->CreateObject( pParent->ImplGetFrame(), pData, bShow );
126 Window::ImplInit( pParent, nStyle, NULL );
128 // Wenn es ein richtiges SysChild ist, dann painten wir auch nicht
129 if ( GetSystemData() )
131 mpWindowImpl->mpSysObj->SetCallback( this, ImplSysChildProc );
132 SetParentClipMode( PARENTCLIPMODE_CLIP );
133 SetBackground();
137 // -----------------------------------------------------------------------
139 SystemChildWindow::SystemChildWindow( Window* pParent, WinBits nStyle ) :
140 Window( WINDOW_SYSTEMCHILDWINDOW )
142 ImplInitSysChild( pParent, nStyle, NULL );
145 // -----------------------------------------------------------------------
147 SystemChildWindow::SystemChildWindow( Window* pParent, WinBits nStyle, SystemWindowData *pData, sal_Bool bShow ) :
148 Window( WINDOW_SYSTEMCHILDWINDOW )
150 ImplInitSysChild( pParent, nStyle, pData, bShow );
153 // -----------------------------------------------------------------------
155 SystemChildWindow::SystemChildWindow( Window* pParent, const ResId& rResId ) :
156 Window( WINDOW_SYSTEMCHILDWINDOW )
158 rResId.SetRT( RSC_WINDOW );
159 WinBits nStyle = ImplInitRes( rResId );
160 ImplInitSysChild( pParent, nStyle, NULL );
161 ImplLoadRes( rResId );
163 if ( !(nStyle & WB_HIDE) )
164 Show();
167 // -----------------------------------------------------------------------
169 SystemChildWindow::~SystemChildWindow()
171 Hide();
172 if ( mpWindowImpl->mpSysObj )
174 ImplGetSVData()->mpDefInst->DestroyObject( mpWindowImpl->mpSysObj );
175 mpWindowImpl->mpSysObj = NULL;
179 // -----------------------------------------------------------------------
181 const SystemEnvData* SystemChildWindow::GetSystemData() const
183 if ( mpWindowImpl->mpSysObj )
184 return mpWindowImpl->mpSysObj->GetSystemData();
185 else
186 return NULL;
189 // -----------------------------------------------------------------------
191 void SystemChildWindow::EnableEraseBackground( sal_Bool bEnable )
193 if ( mpWindowImpl->mpSysObj )
194 mpWindowImpl->mpSysObj->EnableEraseBackground( bEnable );
197 // -----------------------------------------------------------------------
199 void SystemChildWindow::ImplTestJavaException( void* pEnv )
201 #ifdef SOLAR_JAVA
202 JNIEnv* pJavaEnv = reinterpret_cast< JNIEnv* >( pEnv );
203 jthrowable jtThrowable = pJavaEnv->ExceptionOccurred();
205 if( jtThrowable )
206 { // is it a java exception ?
207 #if OSL_DEBUG_LEVEL > 1
208 pJavaEnv->ExceptionDescribe();
209 #endif // OSL_DEBUG_LEVEL > 1
210 pJavaEnv->ExceptionClear();
212 jclass jcThrowable = pJavaEnv->FindClass("java/lang/Throwable");
213 jmethodID jmThrowable_getMessage = pJavaEnv->GetMethodID(jcThrowable, "getMessage", "()Ljava/lang/String;");
214 jstring jsMessage = (jstring) pJavaEnv->CallObjectMethod(jtThrowable, jmThrowable_getMessage);
215 ::rtl::OUString ouMessage;
217 if(jsMessage)
219 const jchar * jcMessage = pJavaEnv->GetStringChars(jsMessage, NULL);
220 ouMessage = ::rtl::OUString(jcMessage);
221 pJavaEnv->ReleaseStringChars(jsMessage, jcMessage);
224 throw uno::RuntimeException(ouMessage, uno::Reference<uno::XInterface>());
226 #else
227 (void)pEnv;
228 #endif // SOLAR_JAVA
231 void SystemChildWindow::SetForwardKey( sal_Bool bEnable )
233 if ( mpWindowImpl->mpSysObj )
234 mpWindowImpl->mpSysObj->SetForwardKey( bEnable );
237 // -----------------------------------------------------------------------
239 sal_IntPtr SystemChildWindow::GetParentWindowHandle( sal_Bool bUseJava )
241 sal_IntPtr nRet = 0;
243 (void)bUseJava;
244 #if defined WNT
245 nRet = reinterpret_cast< sal_IntPtr >( GetSystemData()->hWnd );
246 (void)bUseJava;
247 #elif defined QUARTZ
248 // FIXME: this is wrong
249 nRet = reinterpret_cast< sal_IntPtr >( GetSystemData()->pView );
250 (void)bUseJava;
251 #elif defined IOS
252 // FIXME: this is wrong
253 nRet = reinterpret_cast< sal_IntPtr >( GetSystemData()->pView );
254 #elif defined UNX
255 if( !bUseJava )
257 nRet = (sal_IntPtr) GetSystemData()->aWindow;
259 #ifdef SOLAR_JAVA
260 else
262 uno::Reference< lang::XMultiServiceFactory > xFactory( vcl::unohelper::GetMultiServiceFactory() );
264 if( xFactory.is() && ( GetSystemData()->aWindow > 0 ) )
268 ::rtl::Reference< ::jvmaccess::VirtualMachine > xVM;
269 uno::Reference< java::XJavaVM > xJavaVM( xFactory->createInstance( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.java.JavaVirtualMachine") ) ), uno::UNO_QUERY );
270 uno::Sequence< sal_Int8 > aProcessID( 17 );
272 rtl_getGlobalProcessId( (sal_uInt8*) aProcessID.getArray() );
273 aProcessID[ 16 ] = 0;
274 OSL_ENSURE(sizeof (sal_Int64) >= sizeof (jvmaccess::VirtualMachine *), "Pointer cannot be represented as sal_Int64");
275 sal_Int64 nPointer = reinterpret_cast< sal_Int64 >( static_cast< jvmaccess::VirtualMachine * >(0));
276 xJavaVM->getJavaVM(aProcessID) >>= nPointer;
277 xVM = reinterpret_cast< jvmaccess::VirtualMachine * >(nPointer);
279 if( xVM.is() )
283 ::jvmaccess::VirtualMachine::AttachGuard aVMAttachGuard( xVM );
284 JNIEnv* pEnv = aVMAttachGuard.getEnvironment();
286 jclass jcToolkit = pEnv->FindClass("java/awt/Toolkit");
287 ImplTestJavaException(pEnv);
289 jmethodID jmToolkit_getDefaultToolkit = pEnv->GetStaticMethodID( jcToolkit, "getDefaultToolkit", "()Ljava/awt/Toolkit;" );
290 ImplTestJavaException(pEnv);
292 pEnv->CallStaticObjectMethod(jcToolkit, jmToolkit_getDefaultToolkit);
293 ImplTestJavaException(pEnv);
295 jclass jcMotifAppletViewer = pEnv->FindClass("sun/plugin/navig/motif/MotifAppletViewer");
296 if( pEnv->ExceptionOccurred() )
298 pEnv->ExceptionClear();
300 jcMotifAppletViewer = pEnv->FindClass( "sun/plugin/viewer/MNetscapePluginContext");
301 ImplTestJavaException(pEnv);
304 jclass jcClassLoader = pEnv->FindClass("java/lang/ClassLoader");
305 ImplTestJavaException(pEnv);
307 jmethodID jmClassLoader_loadLibrary = pEnv->GetStaticMethodID( jcClassLoader, "loadLibrary", "(Ljava/lang/Class;Ljava/lang/String;Z)V");
308 ImplTestJavaException(pEnv);
310 jstring jsplugin = pEnv->NewStringUTF("javaplugin_jni");
311 ImplTestJavaException(pEnv);
313 pEnv->CallStaticVoidMethod(jcClassLoader, jmClassLoader_loadLibrary, jcMotifAppletViewer, jsplugin, JNI_FALSE);
314 ImplTestJavaException(pEnv);
316 jmethodID jmMotifAppletViewer_getWidget = pEnv->GetStaticMethodID( jcMotifAppletViewer, "getWidget", "(IIIII)I" );
317 ImplTestJavaException(pEnv);
319 const Size aSize( GetOutputSizePixel() );
320 jint ji_widget = pEnv->CallStaticIntMethod( jcMotifAppletViewer, jmMotifAppletViewer_getWidget,
321 GetSystemData()->aWindow, 0, 0, aSize.Width(), aSize.Height() );
322 ImplTestJavaException(pEnv);
324 nRet = static_cast< sal_IntPtr >( ji_widget );
326 catch( uno::RuntimeException& )
330 if( !nRet )
331 nRet = static_cast< sal_IntPtr >( GetSystemData()->aWindow );
334 catch( ... )
339 #endif // SOLAR_JAVA
340 #else // WNT || QUARTZ || UNX
341 #endif
343 return nRet;
346 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */