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