1*48123e16SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*48123e16SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*48123e16SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*48123e16SAndrew Rist  * distributed with this work for additional information
6*48123e16SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*48123e16SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*48123e16SAndrew Rist  * "License"); you may not use this file except in compliance
9*48123e16SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*48123e16SAndrew Rist  *
11*48123e16SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*48123e16SAndrew Rist  *
13*48123e16SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*48123e16SAndrew Rist  * software distributed under the License is distributed on an
15*48123e16SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*48123e16SAndrew Rist  * KIND, either express or implied.  See the License for the
17*48123e16SAndrew Rist  * specific language governing permissions and limitations
18*48123e16SAndrew Rist  * under the License.
19*48123e16SAndrew Rist  *
20*48123e16SAndrew Rist  *************************************************************/
21*48123e16SAndrew Rist 
22*48123e16SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_dtrans.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir //------------------------------------------------------------------------
28cdf0e10cSrcweir // includes
29cdf0e10cSrcweir //------------------------------------------------------------------------
30cdf0e10cSrcweir #include <osl/diagnose.h>
31cdf0e10cSrcweir #include "WinClipbImpl.hxx"
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <systools/win32/comtools.hxx>
34cdf0e10cSrcweir #include "..\..\inc\DtObjFactory.hxx"
35cdf0e10cSrcweir #include "..\dtobj\APNDataObject.hxx"
36cdf0e10cSrcweir #include "WinClipboard.hxx"
37cdf0e10cSrcweir #include <com/sun/star/datatransfer/clipboard/RenderingCapabilities.hpp>
38cdf0e10cSrcweir #include "..\dtobj\XNotifyingDataObject.hxx"
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #if defined _MSC_VER
41cdf0e10cSrcweir #pragma warning(push,1)
42cdf0e10cSrcweir #endif
43cdf0e10cSrcweir #include <windows.h>
44cdf0e10cSrcweir #include <ole2.h>
45cdf0e10cSrcweir #include <objidl.h>
46cdf0e10cSrcweir #if defined _MSC_VER
47cdf0e10cSrcweir #pragma warning(pop)
48cdf0e10cSrcweir #endif
49cdf0e10cSrcweir 
50cdf0e10cSrcweir //------------------------------------------------------------------------
51cdf0e10cSrcweir // namespace directives
52cdf0e10cSrcweir //------------------------------------------------------------------------
53cdf0e10cSrcweir 
54cdf0e10cSrcweir using namespace rtl;
55cdf0e10cSrcweir using namespace osl;
56cdf0e10cSrcweir using namespace std;
57cdf0e10cSrcweir using namespace cppu;
58cdf0e10cSrcweir 
59cdf0e10cSrcweir using namespace com::sun::star::uno;
60cdf0e10cSrcweir using namespace com::sun::star::datatransfer;
61cdf0e10cSrcweir using namespace com::sun::star::datatransfer::clipboard;
62cdf0e10cSrcweir using namespace com::sun::star::datatransfer::clipboard::RenderingCapabilities;
63cdf0e10cSrcweir 
64cdf0e10cSrcweir //------------------------------------------------------------------------
65cdf0e10cSrcweir // deklarations
66cdf0e10cSrcweir //------------------------------------------------------------------------
67cdf0e10cSrcweir 
68cdf0e10cSrcweir // definition of static members
69cdf0e10cSrcweir CWinClipbImpl* CWinClipbImpl::s_pCWinClipbImpl = NULL;
70cdf0e10cSrcweir osl::Mutex     CWinClipbImpl::s_aMutex;
71cdf0e10cSrcweir 
72cdf0e10cSrcweir //------------------------------------------------------------------------
73cdf0e10cSrcweir //
74cdf0e10cSrcweir //------------------------------------------------------------------------
75cdf0e10cSrcweir 
CWinClipbImpl(const OUString & aClipboardName,CWinClipboard * theWinClipboard)76cdf0e10cSrcweir CWinClipbImpl::CWinClipbImpl( const OUString& aClipboardName, CWinClipboard* theWinClipboard ) :
77cdf0e10cSrcweir 	m_itsName( aClipboardName ),
78cdf0e10cSrcweir 	m_pWinClipboard( theWinClipboard ),
79cdf0e10cSrcweir 	m_pCurrentClipContent( NULL )
80cdf0e10cSrcweir {
81cdf0e10cSrcweir 	OSL_ASSERT( NULL != m_pWinClipboard );
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 	// necessary to reassociate from
84cdf0e10cSrcweir 	// the static callback function
85cdf0e10cSrcweir 	s_pCWinClipbImpl = this;
86cdf0e10cSrcweir 	registerClipboardViewer( );
87cdf0e10cSrcweir }
88cdf0e10cSrcweir 
89cdf0e10cSrcweir //------------------------------------------------------------------------
90cdf0e10cSrcweir //
91cdf0e10cSrcweir //------------------------------------------------------------------------
92cdf0e10cSrcweir 
~CWinClipbImpl()93cdf0e10cSrcweir CWinClipbImpl::~CWinClipbImpl( )
94cdf0e10cSrcweir {
95cdf0e10cSrcweir 	ClearableMutexGuard aGuard( s_aMutex );
96cdf0e10cSrcweir 	s_pCWinClipbImpl = NULL;
97cdf0e10cSrcweir 	aGuard.clear( );
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 	unregisterClipboardViewer( );
100cdf0e10cSrcweir }
101cdf0e10cSrcweir 
102cdf0e10cSrcweir //------------------------------------------------------------------------
103cdf0e10cSrcweir // getContent
104cdf0e10cSrcweir //------------------------------------------------------------------------
105cdf0e10cSrcweir 
getContents()106cdf0e10cSrcweir Reference< XTransferable > SAL_CALL CWinClipbImpl::getContents( ) throw( RuntimeException )
107cdf0e10cSrcweir {
108cdf0e10cSrcweir     // use the shotcut or create a transferable from
109cdf0e10cSrcweir     // system clipboard
110cdf0e10cSrcweir     ClearableMutexGuard aGuard( m_ClipContentMutex );
111cdf0e10cSrcweir 
112cdf0e10cSrcweir 	if ( NULL != m_pCurrentClipContent )
113cdf0e10cSrcweir     {
114cdf0e10cSrcweir         return m_pCurrentClipContent->m_XTransferable;
115cdf0e10cSrcweir     }
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     // release the mutex, so that the variable may be
118cdf0e10cSrcweir     // changed by other threads
119cdf0e10cSrcweir     aGuard.clear( );
120cdf0e10cSrcweir 
121cdf0e10cSrcweir     Reference< XTransferable > rClipContent;
122cdf0e10cSrcweir 
123cdf0e10cSrcweir 	// get the current dataobject from clipboard
124cdf0e10cSrcweir 	IDataObjectPtr pIDataObject;
125cdf0e10cSrcweir 	HRESULT hr = m_MtaOleClipboard.getClipboard( &pIDataObject );
126cdf0e10cSrcweir 
127cdf0e10cSrcweir     if ( SUCCEEDED( hr ) )
128cdf0e10cSrcweir     {
129cdf0e10cSrcweir 	    // create an apartment neutral dataobject and initialize it with a
130cdf0e10cSrcweir 		// com smart pointer to the IDataObject from clipboard
131cdf0e10cSrcweir 		IDataObjectPtr pIDo( new CAPNDataObject( pIDataObject ) );
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 		CDTransObjFactory objFactory;
134cdf0e10cSrcweir 
135cdf0e10cSrcweir 		// remeber pIDo destroys itself due to the smart pointer
136cdf0e10cSrcweir 		rClipContent = objFactory.createTransferableFromDataObj( m_pWinClipboard->m_SrvMgr, pIDo );
137cdf0e10cSrcweir     }
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 	return rClipContent;
140cdf0e10cSrcweir }
141cdf0e10cSrcweir 
142cdf0e10cSrcweir //------------------------------------------------------------------------
143cdf0e10cSrcweir // setContent
144cdf0e10cSrcweir //------------------------------------------------------------------------
145cdf0e10cSrcweir 
setContents(const Reference<XTransferable> & xTransferable,const Reference<XClipboardOwner> & xClipboardOwner)146cdf0e10cSrcweir void SAL_CALL CWinClipbImpl::setContents(
147cdf0e10cSrcweir 	const Reference< XTransferable >& xTransferable,
148cdf0e10cSrcweir 	const Reference< XClipboardOwner >& xClipboardOwner )
149cdf0e10cSrcweir 	throw( RuntimeException )
150cdf0e10cSrcweir {
151cdf0e10cSrcweir 	CDTransObjFactory objFactory;
152cdf0e10cSrcweir 	IDataObjectPtr    pIDataObj;
153cdf0e10cSrcweir 
154cdf0e10cSrcweir 	if ( xTransferable.is( ) )
155cdf0e10cSrcweir 	{
156cdf0e10cSrcweir         ClearableMutexGuard aGuard( m_ClipContentMutex );
157cdf0e10cSrcweir 
158cdf0e10cSrcweir 		m_pCurrentClipContent = new CXNotifyingDataObject(
159cdf0e10cSrcweir 			objFactory.createDataObjFromTransferable( m_pWinClipboard->m_SrvMgr , xTransferable ),
160cdf0e10cSrcweir 			xTransferable,
161cdf0e10cSrcweir 			xClipboardOwner,
162cdf0e10cSrcweir 			this );
163cdf0e10cSrcweir 
164cdf0e10cSrcweir         aGuard.clear( );
165cdf0e10cSrcweir 
166cdf0e10cSrcweir 		pIDataObj = IDataObjectPtr( m_pCurrentClipContent );
167cdf0e10cSrcweir 	}
168cdf0e10cSrcweir 
169cdf0e10cSrcweir 	m_MtaOleClipboard.setClipboard(pIDataObj.get());
170cdf0e10cSrcweir }
171cdf0e10cSrcweir 
172cdf0e10cSrcweir //------------------------------------------------------------------------
173cdf0e10cSrcweir //
174cdf0e10cSrcweir //------------------------------------------------------------------------
175cdf0e10cSrcweir 
getName()176cdf0e10cSrcweir OUString SAL_CALL CWinClipbImpl::getName(  ) throw( RuntimeException )
177cdf0e10cSrcweir {
178cdf0e10cSrcweir 	return m_itsName;
179cdf0e10cSrcweir }
180cdf0e10cSrcweir 
181cdf0e10cSrcweir //------------------------------------------------------------------------
182cdf0e10cSrcweir //
183cdf0e10cSrcweir //------------------------------------------------------------------------
184cdf0e10cSrcweir 
getRenderingCapabilities()185cdf0e10cSrcweir sal_Int8 SAL_CALL CWinClipbImpl::getRenderingCapabilities(  ) throw( RuntimeException )
186cdf0e10cSrcweir {
187cdf0e10cSrcweir 	return ( Delayed | Persistant );
188cdf0e10cSrcweir }
189cdf0e10cSrcweir 
190cdf0e10cSrcweir //------------------------------------------------------------------------
191cdf0e10cSrcweir //
192cdf0e10cSrcweir //------------------------------------------------------------------------
193cdf0e10cSrcweir 
flushClipboard()194cdf0e10cSrcweir void SAL_CALL CWinClipbImpl::flushClipboard( ) throw( RuntimeException )
195cdf0e10cSrcweir {
196cdf0e10cSrcweir     // sollte eigentlich hier stehen: ClearableMutexGuard aGuard( m_ClipContentMutex );
197cdf0e10cSrcweir     // geht aber nicht, da FlushClipboard zur�ckruft und das DataObject
198cdf0e10cSrcweir     // freigibt und damit w�rde es einen Deadlock in onReleaseDataObject geben
199cdf0e10cSrcweir     // FlushClipboard mu� synchron sein, damit das runterfahren ggf. erst weitergeht,
200cdf0e10cSrcweir     // wenn alle Clipboard-Formate gerendert wurden
201cdf0e10cSrcweir     // die Abfrage ist n�tig, damit nur geflusht wird, wenn wir wirklich Clipboardowner
202cdf0e10cSrcweir     // sind (ich weiss nicht genau was passiert, wenn man flusht und nicht Clipboard
203cdf0e10cSrcweir     // owner ist).
204cdf0e10cSrcweir     // eventuell kann man aber die Abfrage in den Clipboard STA Thread verlagern, indem
205cdf0e10cSrcweir     // man sich dort das DataObject merkt und vor dem flushen OleIsCurrentClipboard ruft
206cdf0e10cSrcweir 
207cdf0e10cSrcweir 	if ( NULL != m_pCurrentClipContent )
208cdf0e10cSrcweir 		m_MtaOleClipboard.flushClipboard( );
209cdf0e10cSrcweir }
210cdf0e10cSrcweir 
211cdf0e10cSrcweir //------------------------------------------------------------------------
212cdf0e10cSrcweir //
213cdf0e10cSrcweir //------------------------------------------------------------------------
214cdf0e10cSrcweir 
registerClipboardViewer()215cdf0e10cSrcweir void SAL_CALL CWinClipbImpl::registerClipboardViewer( )
216cdf0e10cSrcweir {
217cdf0e10cSrcweir 	m_MtaOleClipboard.registerClipViewer( CWinClipbImpl::onClipboardContentChanged );
218cdf0e10cSrcweir }
219cdf0e10cSrcweir 
220cdf0e10cSrcweir //------------------------------------------------------------------------
221cdf0e10cSrcweir //
222cdf0e10cSrcweir //------------------------------------------------------------------------
223cdf0e10cSrcweir 
unregisterClipboardViewer()224cdf0e10cSrcweir void SAL_CALL CWinClipbImpl::unregisterClipboardViewer( )
225cdf0e10cSrcweir {
226cdf0e10cSrcweir 	m_MtaOleClipboard.registerClipViewer( NULL );
227cdf0e10cSrcweir }
228cdf0e10cSrcweir 
229cdf0e10cSrcweir //------------------------------------------------------------------------
230cdf0e10cSrcweir //
231cdf0e10cSrcweir //------------------------------------------------------------------------
232cdf0e10cSrcweir 
dispose()233cdf0e10cSrcweir void SAL_CALL CWinClipbImpl::dispose() throw( RuntimeException )
234cdf0e10cSrcweir {
235cdf0e10cSrcweir 	OSL_ENSURE( !m_pCurrentClipContent, "Clipboard was not flushed before shutdown!" );
236cdf0e10cSrcweir }
237cdf0e10cSrcweir 
238cdf0e10cSrcweir //------------------------------------------------------------------------
239cdf0e10cSrcweir //
240cdf0e10cSrcweir //------------------------------------------------------------------------
241cdf0e10cSrcweir 
onClipboardContentChanged(void)242cdf0e10cSrcweir void WINAPI CWinClipbImpl::onClipboardContentChanged( void )
243cdf0e10cSrcweir {
244cdf0e10cSrcweir 	MutexGuard aGuard( s_aMutex );
245cdf0e10cSrcweir 
246cdf0e10cSrcweir 	// reassocition to instance through static member
247cdf0e10cSrcweir 	if ( NULL != s_pCWinClipbImpl )
248cdf0e10cSrcweir 		s_pCWinClipbImpl->m_pWinClipboard->notifyAllClipboardListener( );
249cdf0e10cSrcweir }
250cdf0e10cSrcweir 
251cdf0e10cSrcweir //------------------------------------------------------------------------
252cdf0e10cSrcweir //
253cdf0e10cSrcweir //------------------------------------------------------------------------
254cdf0e10cSrcweir 
onReleaseDataObject(CXNotifyingDataObject * theCaller)255cdf0e10cSrcweir void SAL_CALL CWinClipbImpl::onReleaseDataObject( CXNotifyingDataObject* theCaller )
256cdf0e10cSrcweir {
257cdf0e10cSrcweir 	OSL_ASSERT( NULL != theCaller );
258cdf0e10cSrcweir 
259cdf0e10cSrcweir 	if ( theCaller )
260cdf0e10cSrcweir 		theCaller->lostOwnership( );
261cdf0e10cSrcweir 
262cdf0e10cSrcweir 	// if the current caller is the one we currently
263cdf0e10cSrcweir 	// hold, then set it to NULL because an external
264cdf0e10cSrcweir 	// source must be the clipboardowner now
265cdf0e10cSrcweir     MutexGuard aGuard( m_ClipContentMutex );
266cdf0e10cSrcweir 
267cdf0e10cSrcweir 	if ( m_pCurrentClipContent == theCaller )
268cdf0e10cSrcweir 		m_pCurrentClipContent = NULL;
269cdf0e10cSrcweir }
270