xref: /aoo41x/main/vcl/source/window/syschild.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_vcl.hxx"
30 
31 #include <rtl/process.h>
32 #include <rtl/ref.hxx>
33 
34 #include <tools/rc.h>
35 
36 // declare system types in sysdata.hxx
37 #include <svsys.h>
38 
39 #include <vcl/window.hxx>
40 #include <vcl/sysdata.hxx>
41 #include <vcl/svapp.hxx>
42 #include <vcl/syschild.hxx>
43 #include <vcl/unohelp.hxx>
44 
45 #include <window.h>
46 #include <salinst.hxx>
47 #include <salframe.hxx>
48 #include <salobj.hxx>
49 #include <svdata.hxx>
50 
51 #ifdef SOLAR_JAVA
52 #include <jni.h>
53 #endif
54 
55 #include <comphelper/processfactory.hxx>
56 #include <jvmaccess/virtualmachine.hxx>
57 #include <com/sun/star/java/XJavaVM.hpp>
58 #include <com/sun/star/java/XJavaThreadRegister_11.hpp>
59 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
60 
61 using namespace ::com::sun::star;
62 
63 // =======================================================================
64 
65 long ImplSysChildProc( void* pInst, SalObject* /* pObject */,
66 					   sal_uInt16 nEvent, const void* /* pEvent */ )
67 {
68 	SystemChildWindow* pWindow = (SystemChildWindow*)pInst;
69 	long nRet = 0;
70 
71     ImplDelData aDogTag( pWindow );
72 	switch ( nEvent )
73 	{
74 		case SALOBJ_EVENT_GETFOCUS:
75 			// Focus holen und zwar so, das alle Handler gerufen
76 			// werden, als ob dieses Fenster den Focus bekommt,
77 			// ohne das der Frame den Focus wieder klaut
78 			pWindow->ImplGetFrameData()->mbSysObjFocus = sal_True;
79 			pWindow->ImplGetFrameData()->mbInSysObjToTopHdl = sal_True;
80 			pWindow->ToTop( TOTOP_NOGRABFOCUS );
81 			if( aDogTag.IsDead() )
82 			    break;
83 			pWindow->ImplGetFrameData()->mbInSysObjToTopHdl = sal_False;
84 			pWindow->ImplGetFrameData()->mbInSysObjFocusHdl = sal_True;
85 			pWindow->GrabFocus();
86 			if( aDogTag.IsDead() )
87 			    break;
88 			pWindow->ImplGetFrameData()->mbInSysObjFocusHdl = sal_False;
89 			break;
90 
91 		case SALOBJ_EVENT_LOSEFOCUS:
92 			// Hintenrum einen LoseFocus ausloesen, das der Status
93 			// der Fenster dem entsprechenden Activate-Status
94 			// entspricht
95 			pWindow->ImplGetFrameData()->mbSysObjFocus = sal_False;
96 			if ( !pWindow->ImplGetFrameData()->mnFocusId )
97 			{
98 				pWindow->ImplGetFrameData()->mbStartFocusState = sal_True;
99 				Application::PostUserEvent( pWindow->ImplGetFrameData()->mnFocusId, LINK( pWindow->ImplGetFrameWindow(), Window, ImplAsyncFocusHdl ) );
100 			}
101 			break;
102 
103 		case SALOBJ_EVENT_TOTOP:
104 			pWindow->ImplGetFrameData()->mbInSysObjToTopHdl = sal_True;
105 			if ( !Application::GetFocusWindow() || pWindow->HasChildPathFocus() )
106 				pWindow->ToTop( TOTOP_NOGRABFOCUS );
107 			else
108 				pWindow->ToTop();
109 			if( aDogTag.IsDead() )
110 			    break;
111 			pWindow->GrabFocus();
112 			if( aDogTag.IsDead() )
113 			    break;
114 			pWindow->ImplGetFrameData()->mbInSysObjToTopHdl = sal_False;
115 			break;
116 	}
117 
118 	return nRet;
119 }
120 
121 // =======================================================================
122 
123 void SystemChildWindow::ImplInitSysChild( Window* pParent, WinBits nStyle, SystemWindowData *pData, sal_Bool bShow )
124 {
125 	mpWindowImpl->mpSysObj = ImplGetSVData()->mpDefInst->CreateObject( pParent->ImplGetFrame(), pData, bShow );
126 
127 	Window::ImplInit( pParent, nStyle, NULL );
128 
129 	// Wenn es ein richtiges SysChild ist, dann painten wir auch nicht
130 	if ( GetSystemData() )
131 	{
132 		mpWindowImpl->mpSysObj->SetCallback( this, ImplSysChildProc );
133 		SetParentClipMode( PARENTCLIPMODE_CLIP );
134 		SetBackground();
135 	}
136 }
137 
138 // -----------------------------------------------------------------------
139 
140 SystemChildWindow::SystemChildWindow( Window* pParent, WinBits nStyle ) :
141 	Window( WINDOW_SYSTEMCHILDWINDOW )
142 {
143 	ImplInitSysChild( pParent, nStyle, NULL );
144 }
145 
146 // -----------------------------------------------------------------------
147 
148 SystemChildWindow::SystemChildWindow( Window* pParent, WinBits nStyle, SystemWindowData *pData, sal_Bool bShow ) :
149 	Window( WINDOW_SYSTEMCHILDWINDOW )
150 {
151 	ImplInitSysChild( pParent, nStyle, pData, bShow );
152 }
153 
154 // -----------------------------------------------------------------------
155 
156 SystemChildWindow::SystemChildWindow( Window* pParent, const ResId& rResId ) :
157 	Window( WINDOW_SYSTEMCHILDWINDOW )
158 {
159 	rResId.SetRT( RSC_WINDOW );
160 	WinBits nStyle = ImplInitRes( rResId );
161 	ImplInitSysChild( pParent, nStyle, NULL );
162 	ImplLoadRes( rResId );
163 
164 	if ( !(nStyle & WB_HIDE) )
165 		Show();
166 }
167 
168 // -----------------------------------------------------------------------
169 
170 SystemChildWindow::~SystemChildWindow()
171 {
172 	Hide();
173 	if ( mpWindowImpl->mpSysObj )
174 	{
175 		ImplGetSVData()->mpDefInst->DestroyObject( mpWindowImpl->mpSysObj );
176 		mpWindowImpl->mpSysObj = NULL;
177 	}
178 }
179 
180 // -----------------------------------------------------------------------
181 
182 const SystemEnvData* SystemChildWindow::GetSystemData() const
183 {
184 	if ( mpWindowImpl->mpSysObj )
185 		return mpWindowImpl->mpSysObj->GetSystemData();
186 	else
187 		return NULL;
188 }
189 
190 // -----------------------------------------------------------------------
191 
192 void SystemChildWindow::EnableEraseBackground( sal_Bool bEnable )
193 {
194 	if ( mpWindowImpl->mpSysObj )
195 		mpWindowImpl->mpSysObj->EnableEraseBackground( bEnable );
196 }
197 
198 // -----------------------------------------------------------------------
199 
200 sal_Bool SystemChildWindow::IsEraseBackgroundEnabled()
201 {
202 	if ( mpWindowImpl->mpSysObj )
203 		return mpWindowImpl->mpSysObj->IsEraseBackgroundEnabled();
204     else
205         return sal_False;
206 }
207 
208 // -----------------------------------------------------------------------
209 
210 void SystemChildWindow::ImplTestJavaException( void* pEnv )
211 {
212 #ifdef SOLAR_JAVA
213     JNIEnv*     pJavaEnv = reinterpret_cast< JNIEnv* >( pEnv );
214     jthrowable  jtThrowable = pJavaEnv->ExceptionOccurred();
215 
216     if( jtThrowable )
217     { // is it a java exception ?
218 #if OSL_DEBUG_LEVEL > 1
219         pJavaEnv->ExceptionDescribe();
220 #endif // OSL_DEBUG_LEVEL > 1
221         pJavaEnv->ExceptionClear();
222 
223         jclass          jcThrowable = pJavaEnv->FindClass("java/lang/Throwable");
224         jmethodID       jmThrowable_getMessage = pJavaEnv->GetMethodID(jcThrowable, "getMessage", "()Ljava/lang/String;");
225         jstring         jsMessage = (jstring) pJavaEnv->CallObjectMethod(jtThrowable, jmThrowable_getMessage);
226             ::rtl::OUString ouMessage;
227 
228             if(jsMessage)
229             {
230                 const jchar * jcMessage = pJavaEnv->GetStringChars(jsMessage, NULL);
231                 ouMessage = ::rtl::OUString(jcMessage);
232                 pJavaEnv->ReleaseStringChars(jsMessage, jcMessage);
233             }
234 
235             throw uno::RuntimeException(ouMessage, uno::Reference<uno::XInterface>());
236     }
237 #endif // SOLAR_JAVA
238 }
239 
240 // -----------------------------------------------------------------------
241 
242 sal_IntPtr SystemChildWindow::GetParentWindowHandle( sal_Bool bUseJava )
243 {
244     sal_IntPtr nRet = 0;
245 
246     (void)bUseJava;
247 #if defined WNT
248     nRet = reinterpret_cast< sal_IntPtr >( GetSystemData()->hWnd );
249 #elif defined QUARTZ
250     // FIXME: this is wrong
251     nRet = reinterpret_cast< sal_IntPtr >( GetSystemData()->pView );
252 #elif defined UNX
253     if( !bUseJava )
254     {
255         nRet = (sal_IntPtr) GetSystemData()->aWindow;
256     }
257 #ifdef SOLAR_JAVA
258     else
259     {
260         uno::Reference< lang::XMultiServiceFactory > xFactory( vcl::unohelper::GetMultiServiceFactory() );
261 
262         if( xFactory.is() && ( GetSystemData()->aWindow > 0 ) )
263         {
264             try
265             {
266                     ::rtl::Reference< ::jvmaccess::VirtualMachine > xVM;
267                     uno::Reference< java::XJavaVM >                 xJavaVM( xFactory->createInstance( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.java.JavaVirtualMachine") ) ), uno::UNO_QUERY );
268                     uno::Sequence< sal_Int8 >                       aProcessID( 17 );
269 
270                     rtl_getGlobalProcessId( (sal_uInt8*) aProcessID.getArray() );
271                     aProcessID[ 16 ] = 0;
272                     OSL_ENSURE(sizeof (sal_Int64) >= sizeof (jvmaccess::VirtualMachine *), "Pointer cannot be represented as sal_Int64");
273                     sal_Int64 nPointer = reinterpret_cast< sal_Int64 >( static_cast< jvmaccess::VirtualMachine * >(0));
274                     xJavaVM->getJavaVM(aProcessID) >>= nPointer;
275                     xVM = reinterpret_cast< jvmaccess::VirtualMachine * >(nPointer);
276 
277                     if( xVM.is() )
278                     {
279                         try
280                         {
281                                 ::jvmaccess::VirtualMachine::AttachGuard    aVMAttachGuard( xVM );
282                                 JNIEnv*                                     pEnv = aVMAttachGuard.getEnvironment();
283 
284                                 jclass jcToolkit = pEnv->FindClass("java/awt/Toolkit");
285                                 ImplTestJavaException(pEnv);
286 
287                                 jmethodID jmToolkit_getDefaultToolkit = pEnv->GetStaticMethodID( jcToolkit, "getDefaultToolkit", "()Ljava/awt/Toolkit;" );
288                                 ImplTestJavaException(pEnv);
289 
290                                 pEnv->CallStaticObjectMethod(jcToolkit, jmToolkit_getDefaultToolkit);
291                                 ImplTestJavaException(pEnv);
292 
293                                 jclass jcMotifAppletViewer = pEnv->FindClass("sun/plugin/navig/motif/MotifAppletViewer");
294                                 if( pEnv->ExceptionOccurred() )
295                                 {
296                                     pEnv->ExceptionClear();
297 
298                                     jcMotifAppletViewer = pEnv->FindClass( "sun/plugin/viewer/MNetscapePluginContext");
299                                     ImplTestJavaException(pEnv);
300                                 }
301 
302                                 jclass jcClassLoader = pEnv->FindClass("java/lang/ClassLoader");
303                                 ImplTestJavaException(pEnv);
304 
305                                 jmethodID jmClassLoader_loadLibrary = pEnv->GetStaticMethodID( jcClassLoader, "loadLibrary", "(Ljava/lang/Class;Ljava/lang/String;Z)V");
306                                 ImplTestJavaException(pEnv);
307 
308                                 jstring jsplugin = pEnv->NewStringUTF("javaplugin_jni");
309                                 ImplTestJavaException(pEnv);
310 
311                                 pEnv->CallStaticVoidMethod(jcClassLoader, jmClassLoader_loadLibrary, jcMotifAppletViewer, jsplugin, JNI_FALSE);
312                                 ImplTestJavaException(pEnv);
313 
314                                 jmethodID jmMotifAppletViewer_getWidget = pEnv->GetStaticMethodID( jcMotifAppletViewer, "getWidget", "(IIIII)I" );
315                                 ImplTestJavaException(pEnv);
316 
317                                 const Size aSize( GetOutputSizePixel() );
318                                 jint ji_widget = pEnv->CallStaticIntMethod( jcMotifAppletViewer, jmMotifAppletViewer_getWidget,
319                                         GetSystemData()->aWindow, 0, 0, aSize.Width(), aSize.Height() );
320                                 ImplTestJavaException(pEnv);
321 
322                                 nRet = static_cast< sal_IntPtr >( ji_widget );
323                         }
324                         catch( uno::RuntimeException& )
325                         {
326                         }
327 
328                         if( !nRet )
329                             nRet = static_cast< sal_IntPtr >( GetSystemData()->aWindow );
330                     }
331             }
332             catch( ... )
333             {
334             }
335         }
336     }
337 #endif // SOLAR_JAVA
338 #else // WNT || QUARTZ || UNX
339 #endif
340 
341     return nRet;
342 }
343