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_toolkit.hxx"
30 #include <com/sun/star/lang/SystemDependent.hpp>
31 #include <com/sun/star/awt/SystemDependentXWindow.hpp>
32 
33 #ifdef WNT
34 #include <tools/prewin.h>
35 #include <windows.h>
36 #include <tools/postwin.h>
37 #elif defined ( QUARTZ )
38 #include "premac.h"
39 #include <Cocoa/Cocoa.h>
40 #include "postmac.h"
41 #endif
42 
43 #include <vcl/syschild.hxx>
44 #include <vcl/sysdata.hxx>
45 #include <cppuhelper/typeprovider.hxx>
46 #include <comphelper/sequence.hxx>
47 
48 #include <toolkit/awt/vclxtopwindow.hxx>
49 #include <toolkit/awt/vclxmenu.hxx>
50 #include <toolkit/helper/macros.hxx>
51 
52 #include <vcl/wrkwin.hxx>
53 #include <vcl/syswin.hxx>
54 #include <vcl/menu.hxx>
55 #include <vcl/svapp.hxx>
56 
57 #include <tools/debug.hxx>
58 
59 using ::com::sun::star::uno::RuntimeException;
60 using ::com::sun::star::uno::Sequence;
61 using ::com::sun::star::uno::Type;
62 using ::com::sun::star::uno::Any;
63 using ::com::sun::star::lang::IndexOutOfBoundsException;
64 
65 VCLXTopWindow_Base::VCLXTopWindow_Base( const bool _bSupportSystemWindowPeer )
66     :m_bWHWND( _bSupportSystemWindowPeer )
67 {
68 }
69 
70 VCLXTopWindow_Base::~VCLXTopWindow_Base()
71 {
72 }
73 
74 Any VCLXTopWindow_Base::queryInterface( const Type & rType ) throw(RuntimeException)
75 {
76     ::com::sun::star::uno::Any aRet( VCLXTopWindow_XBase::queryInterface( rType ) );
77 
78     // do not expose XSystemDependentWindowPeer if we do not have a system window handle
79     if ( !aRet.hasValue() && m_bWHWND )
80         aRet = VCLXTopWindow_SBase::queryInterface( rType );
81 
82     return aRet;
83 }
84 
85 Sequence< Type > VCLXTopWindow_Base::getTypes() throw(RuntimeException)
86 {
87     Sequence< Type > aTypes( VCLXTopWindow_XBase::getTypes() );
88     if ( m_bWHWND )
89         aTypes = ::comphelper::concatSequences( aTypes, VCLXTopWindow_SBase::getTypes() );
90     return aTypes;
91 }
92 
93 ::com::sun::star::uno::Any VCLXTopWindow_Base::getWindowHandle( const ::com::sun::star::uno::Sequence< sal_Int8 >& /*ProcessId*/, sal_Int16 SystemType ) throw(::com::sun::star::uno::RuntimeException)
94 {
95 	::vos::OGuard aGuard( GetMutexImpl() );
96 
97 	// TODO, check the process id
98 	::com::sun::star::uno::Any aRet;
99 	Window* pWindow = GetWindowImpl();
100 	if ( pWindow )
101 	{
102 		const SystemEnvData* pSysData = ((SystemWindow *)pWindow)->GetSystemData();
103 		if( pSysData )
104 		{
105 #if (defined WNT)
106 			if( SystemType == ::com::sun::star::lang::SystemDependent::SYSTEM_WIN32 )
107 			{
108 				 aRet <<= (sal_Int32)pSysData->hWnd;
109 			}
110 #elif (defined OS2)
111 			if( SystemType == ::com::sun::star::lang::SystemDependent::SYSTEM_OS2 )
112 			{
113 				 aRet <<= (sal_Int32)pSysData->hWnd;
114 			}
115 #elif (defined QUARTZ)
116 			if( SystemType == ::com::sun::star::lang::SystemDependent::SYSTEM_MAC )
117 			{
118 				 aRet <<= (sal_IntPtr)pSysData->pView;
119 			}
120 #elif (defined UNX)
121 			if( SystemType == ::com::sun::star::lang::SystemDependent::SYSTEM_XWINDOW )
122 			{
123 				::com::sun::star::awt::SystemDependentXWindow aSD;
124 				aSD.DisplayPointer = sal::static_int_cast< sal_Int64 >(reinterpret_cast< sal_IntPtr >(pSysData->pDisplay));
125 				aSD.WindowHandle = pSysData->aWindow;
126 				aRet <<= aSD;
127 			}
128 #endif
129 		}
130 	}
131 	return aRet;
132 }
133 
134 void VCLXTopWindow_Base::addTopWindowListener( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XTopWindowListener >& rxListener ) throw(::com::sun::star::uno::RuntimeException)
135 {
136 	::vos::OGuard aGuard( GetMutexImpl() );
137 
138 	GetTopWindowListenersImpl().addInterface( rxListener );
139 }
140 
141 void VCLXTopWindow_Base::removeTopWindowListener( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XTopWindowListener >& rxListener ) throw(::com::sun::star::uno::RuntimeException)
142 {
143 	::vos::OGuard aGuard( GetMutexImpl() );
144 
145 	GetTopWindowListenersImpl().removeInterface( rxListener );
146 }
147 
148 void VCLXTopWindow_Base::toFront(  ) throw(::com::sun::star::uno::RuntimeException)
149 {
150 	::vos::OGuard aGuard( GetMutexImpl() );
151 
152 	Window* pWindow = GetWindowImpl();
153 	if ( pWindow )
154 		((WorkWindow*)pWindow)->ToTop( TOTOP_RESTOREWHENMIN );
155 }
156 
157 void VCLXTopWindow_Base::toBack(  ) throw(::com::sun::star::uno::RuntimeException)
158 {
159 #if 0 // Not possible in VCL...
160 
161 	::vos::OGuard aGuard( GetMutexImpl() );
162 
163 	Window* pWindow = GetWindowImpl();
164 	if ( pWindow )
165 	{
166 		((WorkWindow*)pWindow)->ToBack();
167 	}
168 #endif
169 }
170 
171 void VCLXTopWindow_Base::setMenuBar( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XMenuBar >& rxMenu ) throw(::com::sun::star::uno::RuntimeException)
172 {
173 	::vos::OGuard aGuard( GetMutexImpl() );
174 
175 	SystemWindow* pWindow = (SystemWindow*) GetWindowImpl();
176 	if ( pWindow )
177 	{
178 		pWindow->SetMenuBar( NULL );
179 		if ( rxMenu.is() )
180 		{
181 			VCLXMenu* pMenu = VCLXMenu::GetImplementation( rxMenu );
182 			if ( pMenu && !pMenu->IsPopupMenu() )
183 				pWindow->SetMenuBar( (MenuBar*) pMenu->GetMenu() );
184 		}
185 	}
186 	mxMenuBar = rxMenu;
187 }
188 
189 //--------------------------------------------------------------------
190 ::sal_Bool SAL_CALL VCLXTopWindow_Base::getIsMaximized() throw (RuntimeException)
191 {
192 	::vos::OGuard aGuard( GetMutexImpl() );
193 
194 	const WorkWindow* pWindow = dynamic_cast< const WorkWindow* >( GetWindowImpl() );
195 	if ( !pWindow )
196         return sal_False;
197 
198     return pWindow->IsMaximized();
199 }
200 
201 //--------------------------------------------------------------------
202 void SAL_CALL VCLXTopWindow_Base::setIsMaximized( ::sal_Bool _ismaximized ) throw (RuntimeException)
203 {
204 	::vos::OGuard aGuard( GetMutexImpl() );
205 
206 	WorkWindow* pWindow = dynamic_cast< WorkWindow* >( GetWindowImpl() );
207 	if ( !pWindow )
208         return;
209 
210     pWindow->Maximize( _ismaximized );
211 }
212 
213 //--------------------------------------------------------------------
214 ::sal_Bool SAL_CALL VCLXTopWindow_Base::getIsMinimized() throw (RuntimeException)
215 {
216 	::vos::OGuard aGuard( GetMutexImpl() );
217 
218 	const WorkWindow* pWindow = dynamic_cast< const WorkWindow* >( GetWindowImpl() );
219 	if ( !pWindow )
220         return sal_False;
221 
222     return pWindow->IsMinimized();
223 }
224 
225 //--------------------------------------------------------------------
226 void SAL_CALL VCLXTopWindow_Base::setIsMinimized( ::sal_Bool _isMinimized ) throw (RuntimeException)
227 {
228 	::vos::OGuard aGuard( GetMutexImpl() );
229 
230 	WorkWindow* pWindow = dynamic_cast< WorkWindow* >( GetWindowImpl() );
231 	if ( !pWindow )
232         return;
233 
234     _isMinimized ? pWindow->Minimize() : pWindow->Restore();
235 }
236 
237 //--------------------------------------------------------------------
238 ::sal_Int32 SAL_CALL VCLXTopWindow_Base::getDisplay() throw (RuntimeException)
239 {
240 	::vos::OGuard aGuard( GetMutexImpl() );
241 
242 	const SystemWindow* pWindow = dynamic_cast< const SystemWindow* >( GetWindowImpl() );
243 	if ( !pWindow )
244         return 0;
245 
246     return pWindow->GetScreenNumber();
247 }
248 
249 //--------------------------------------------------------------------
250 void SAL_CALL VCLXTopWindow_Base::setDisplay( ::sal_Int32 _display ) throw (RuntimeException, IndexOutOfBoundsException)
251 {
252 	::vos::OGuard aGuard( GetMutexImpl() );
253 
254     if ( ( _display < 0 ) || ( _display >= (sal_Int32)Application::GetScreenCount() ) )
255         throw IndexOutOfBoundsException();
256 
257 	SystemWindow* pWindow = dynamic_cast< SystemWindow* >( GetWindowImpl() );
258 	if ( !pWindow )
259         return;
260 
261     pWindow->SetScreenNumber( _display );
262 }
263 
264 //	----------------------------------------------------
265 //	class VCLXTopWindow
266 //	----------------------------------------------------
267 
268 void VCLXTopWindow::ImplGetPropertyIds( std::list< sal_uInt16 > &rIds )
269 {
270     VCLXContainer::ImplGetPropertyIds( rIds );
271 }
272 
273 VCLXTopWindow::VCLXTopWindow(bool bWHWND)
274     : VCLXTopWindow_Base( bWHWND )
275 {
276 }
277 
278 VCLXTopWindow::~VCLXTopWindow()
279 {
280 }
281 
282 vos::IMutex& VCLXTopWindow::GetMutexImpl()
283 {
284     return VCLXContainer::GetMutex();
285 }
286 
287 Window* VCLXTopWindow::GetWindowImpl()
288 {
289     return VCLXContainer::GetWindow();
290 }
291 
292 ::cppu::OInterfaceContainerHelper& VCLXTopWindow::GetTopWindowListenersImpl()
293 {
294     return GetTopWindowListeners();
295 }
296 
297 // ::com::sun::star::uno::XInterface
298 ::com::sun::star::uno::Any VCLXTopWindow::queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException)
299 {
300     ::com::sun::star::uno::Any aRet( VCLXTopWindow_Base::queryInterface( rType ) );
301 
302     if ( !aRet.hasValue() )
303         aRet = VCLXContainer::queryInterface( rType );
304 
305     return aRet;
306 }
307 
308 ::com::sun::star::uno::Sequence< sal_Int8 > VCLXTopWindow::getImplementationId() throw(::com::sun::star::uno::RuntimeException)
309 {
310     static ::cppu::OImplementationId* pId = NULL;
311     static ::cppu::OImplementationId* pIdWithHandle = NULL;
312     if ( isSystemDependentWindowPeer() )
313     {
314         if( !pIdWithHandle )
315         {
316             ::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() );
317             if( !pIdWithHandle )
318             {
319                 static ::cppu::OImplementationId idWithHandle( sal_False );
320                 pIdWithHandle = &idWithHandle;
321             }
322         }
323 
324         return (*pIdWithHandle).getImplementationId();
325     }
326     else
327     {
328         if( !pId )
329         {
330             ::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() );
331             if( !pId )
332             {
333                 static ::cppu::OImplementationId id( sal_False );
334                 pId = &id;
335             }
336         }
337 
338         return (*pId).getImplementationId();
339     }
340 }
341 
342 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > VCLXTopWindow::getTypes() throw(::com::sun::star::uno::RuntimeException)
343 {
344     return ::comphelper::concatSequences( VCLXTopWindow_Base::getTypes(), VCLXContainer::getTypes() );
345 }
346