xref: /AOO41X/main/dtrans/source/win32/workbench/test_wincb.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
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_dtrans.hxx"
30 
31 
32 //_________________________________________________________________________________________________________________________
33 //	interface includes
34 //_________________________________________________________________________________________________________________________
35 
36 
37 #include "..\misc\ImplHelper.hxx"
38 
39 //_________________________________________________________________________________________________________________________
40 //	other includes
41 //_________________________________________________________________________________________________________________________
42 #include <cppuhelper/servicefactory.hxx>
43 #include <com/sun/star/datatransfer/XTransferable.hpp>
44 #include <com/sun/star/datatransfer/clipboard/XClipboardOwner.hpp>
45 #include <com/sun/star/datatransfer/clipboard/XClipboardNotifier.hpp>
46 #include <com/sun/star/datatransfer/clipboard/XClipboardEx.hpp>
47 #include <com/sun/star/datatransfer/clipboard/XFlushableClipboard.hpp>
48 #include <com/sun/star/lang/XComponent.hpp>
49 #include <cppuhelper/implbase1.hxx>
50 #include <cppuhelper/implbase2.hxx>
51 #include <rtl/ustring.hxx>
52 #include <sal/types.h>
53 #include <osl/diagnose.h>
54 
55 #include <stdio.h>
56 #if defined _MSC_VER
57 #pragma warning(push,1)
58 #endif
59 #include <windows.h>
60 #include <objbase.h>
61 #if defined _MSC_VER
62 #pragma warning(pop)
63 #endif
64 
65 #include <memory>
66 
67 #include <process.h>
68 
69 //-------------------------------------------------------------
70 // my defines
71 //-------------------------------------------------------------
72 
73 #define TEST_CLIPBOARD
74 #define RDB_SYSPATH  "d:\\projects\\src623\\dtrans\\wntmsci7\\bin\\applicat.rdb"
75 #define WINCLIPBOARD_SERVICE_NAME L"com.sun.star.datatransfer.clipboard.SystemClipboard"
76 #define  WRITE_CB
77 #define EVT_MANUAL_RESET     TRUE
78 #define EVT_INIT_NONSIGNALED FALSE
79 #define EVT_NONAME           ""
80 
81 //------------------------------------------------------------
82 //	namesapces
83 //------------------------------------------------------------
84 
85 using namespace	::rtl;
86 using namespace ::std;
87 using namespace ::cppu;
88 using namespace ::com::sun::star::datatransfer;
89 using namespace ::com::sun::star::datatransfer::clipboard;
90 using namespace ::com::sun::star::uno;
91 using namespace ::com::sun::star::io;
92 using namespace	::com::sun::star::lang;
93 
94 //------------------------------------------------------------
95 //	globales
96 //------------------------------------------------------------
97 
98 Reference< XTransferable > rXTransfRead;
99 HANDLE	g_hEvtThreadWakeup;
100 
101 //------------------------------------------------------------
102 //
103 //------------------------------------------------------------
104 
105 class CClipboardListener : public WeakImplHelper1 < XClipboardListener >
106 {
107 public:
108 	~CClipboardListener( );
109 
110 	//-------------------------------------------------
111 	// XClipboardListener
112 	//-------------------------------------------------
113 
114 	virtual void SAL_CALL disposing( const EventObject& Source ) throw(RuntimeException);
115 	virtual void SAL_CALL changedContents( const ClipboardEvent& event ) throw( RuntimeException );
116 };
117 
118 CClipboardListener::~CClipboardListener( )
119 {
120 }
121 
122 void SAL_CALL CClipboardListener::disposing( const EventObject& Source ) throw(RuntimeException)
123 {
124 
125 }
126 
127 void SAL_CALL CClipboardListener::changedContents( const ClipboardEvent& event ) throw( RuntimeException )
128 {
129 	//MessageBox( NULL, TEXT("Clipboard content changed"), TEXT("Info"), MB_OK | MB_ICONINFORMATION );
130 }
131 
132 //------------------------------------------------------------
133 //
134 //------------------------------------------------------------
135 
136 class CTransferable : public WeakImplHelper2< XClipboardOwner, XTransferable >
137 {
138 public:
139 	CTransferable( );
140 
141 	//-------------------------------------------------
142 	// XTransferable
143 	//-------------------------------------------------
144 
145 	virtual Any SAL_CALL getTransferData( const DataFlavor& aFlavor )
146 		throw(UnsupportedFlavorException, IOException, RuntimeException);
147 
148     virtual Sequence< DataFlavor > SAL_CALL getTransferDataFlavors(  ) throw(RuntimeException);
149 
150 	virtual sal_Bool SAL_CALL isDataFlavorSupported( const DataFlavor& aFlavor ) throw(RuntimeException);
151 
152 	//-------------------------------------------------
153 	// XClipboardOwner
154 	//-------------------------------------------------
155 
156 	virtual void SAL_CALL lostOwnership( const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans )
157 		throw(RuntimeException);
158 
159 private:
160 	Sequence< DataFlavor > m_FlavorList;
161 	OUString               m_Data;
162 };
163 
164 //----------------------------------------------------------------
165 //	ctor
166 //----------------------------------------------------------------
167 
168 CTransferable::CTransferable( ) :
169 	m_FlavorList( 1 ),
170 	m_Data( OUString::createFromAscii( "Ich habe mir ein neues Fahrrad gekauft!" ) )
171 {
172 	DataFlavor df;
173 
174 	//df.MimeType = L"text/plain;charset=utf-16";
175 	//df.DataType = getCppuType( ( OUString* )0 );
176 
177 	df.MimeType = L"text/plain;charset=Windows1252";
178 	df.DataType = getCppuType( (Sequence< sal_Int8 >*)0 );
179 
180 	m_FlavorList[0] = df;
181 }
182 
183 //----------------------------------------------------------------
184 //	getTransferData
185 //----------------------------------------------------------------
186 
187 Any SAL_CALL CTransferable::getTransferData( const DataFlavor& aFlavor )
188 	throw(UnsupportedFlavorException, IOException, RuntimeException)
189 {
190 	Any anyData;
191 
192 	/*
193 	if ( aFlavor.MimeType == m_FlavorList[0].MimeType )
194 		anyData = makeAny( m_Data );
195 	*/
196 	if ( aFlavor.MimeType.equalsIgnoreCase( m_FlavorList[0].MimeType ) )
197 	{
198 		OString text(
199 			m_Data.getStr( ),
200 			m_Data.getLength( ),
201 			RTL_TEXTENCODING_ASCII_US );
202 
203 		Sequence< sal_Int8 > textStream( text.getLength( ) + 1 );
204 
205 		rtl_copyMemory( textStream.getArray( ), text.getStr( ), textStream.getLength( ) );
206 
207 		anyData = makeAny( textStream );
208 	}
209 	else
210 		throw UnsupportedFlavorException( );
211 
212 	return anyData;
213 }
214 
215 //----------------------------------------------------------------
216 //	getTransferDataFlavors
217 //----------------------------------------------------------------
218 
219 Sequence< DataFlavor > SAL_CALL CTransferable::getTransferDataFlavors(  )
220 	throw(RuntimeException)
221 {
222 	return m_FlavorList;
223 }
224 
225 //----------------------------------------------------------------
226 //	isDataFlavorSupported
227 //----------------------------------------------------------------
228 
229 sal_Bool SAL_CALL CTransferable::isDataFlavorSupported( const DataFlavor& aFlavor )
230 	throw(RuntimeException)
231 {
232 	sal_Int32 nLength = m_FlavorList.getLength( );
233 
234 	for ( sal_Int32 i = 0; i < nLength; ++i )
235 		if ( m_FlavorList[i].MimeType == aFlavor.MimeType )
236 			return sal_True;
237 
238 	return sal_False;
239 }
240 
241 //----------------------------------------------------------------
242 //	lostOwnership
243 //----------------------------------------------------------------
244 
245 void SAL_CALL CTransferable::lostOwnership(
246 	const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans )
247 	throw(RuntimeException)
248 {
249 	//MessageBox( NULL, TEXT("No longer clipboard owner"), TEXT("Info"), MB_OK | MB_ICONINFORMATION );
250 }
251 
252 //----------------------------------------------------------------
253 //	main
254 //----------------------------------------------------------------
255 
256 int SAL_CALL main( int nArgc, char* Argv[] )
257 {
258 	// create a multi-threaded apartment; we can test only
259 	// with a multithreaded apartment because for a single
260 	// threaded apartment we need a message loop to deliver
261 	// messages to our XTDataObject
262 	//HRESULT hr = CoInitializeEx( NULL, COINIT_MULTITHREADED );
263 	HRESULT hr = CoInitialize( NULL );
264 
265 	char buff[6];
266 
267 	LCID lcid = MAKELCID( MAKELANGID( LANG_GERMAN, SUBLANG_GERMAN ), SORT_DEFAULT );
268 
269 	BOOL bValid = IsValidLocale( lcid, LCID_SUPPORTED );
270 	GetLocaleInfoA( lcid, LOCALE_IDEFAULTANSICODEPAGE, buff, sizeof( buff ) );
271 
272 	//-------------------------------------------------
273 	// get the global service-manager
274 	//-------------------------------------------------
275 
276 	OUString rdbName = OUString( RTL_CONSTASCII_USTRINGPARAM( RDB_SYSPATH ) );
277 	Reference< XMultiServiceFactory > g_xFactory( createRegistryServiceFactory( rdbName ) );
278 
279 	// Print a message if an error occured.
280 	if ( !g_xFactory.is( ) )
281 	{
282 		OSL_ENSURE(sal_False, "Can't create RegistryServiceFactory");
283 		return(-1);
284 	}
285 
286 	//-------------------------------------------------
287 	// try to get an Interface to a XFilePicker Service
288 	//-------------------------------------------------
289 
290 	Reference< XTransferable > rXTransf( static_cast< XTransferable* >( new CTransferable ) );
291 
292 	Reference< XClipboard >
293 		xClipboard( g_xFactory->createInstance( OUString( WINCLIPBOARD_SERVICE_NAME ) ), UNO_QUERY );
294 	if ( !xClipboard.is( ) )
295 	{
296 		OSL_ENSURE( sal_False, "Error creating Clipboard Service" );
297 		return(-1);
298 	}
299 
300 	Reference< XClipboardNotifier > xClipNotifier( xClipboard, UNO_QUERY );
301 	Reference< XClipboardListener > rXClipListener( static_cast< XClipboardListener* >( new CClipboardListener() ) );
302 	xClipNotifier->addClipboardListener( rXClipListener );
303 
304 	MessageBox( NULL, TEXT("Go"), TEXT("INFO"), MB_OK|MB_ICONINFORMATION);
305 
306 	// set new clipboard content
307 	xClipboard->setContents( rXTransf, Reference< XClipboardOwner >( rXTransf, UNO_QUERY )  );
308 
309 	/*
310 	MessageBox( NULL, TEXT("Clear content"), TEXT("INFO"), MB_OK|MB_ICONINFORMATION);
311 
312 	Reference< XClipboardOwner > rXClipOwner;
313 	Reference< XTransferable >   rXEmptyTransf;
314 	xClipboard->setContents( rXEmptyTransf, rXClipOwner );
315 	*/
316 
317 	MessageBox( NULL, TEXT("Stop"), TEXT("INFO"), MB_OK|MB_ICONINFORMATION);
318 
319 	// flush the clipboard content
320 	Reference< XFlushableClipboard > rXFlushableClip( xClipboard, UNO_QUERY );
321 	rXFlushableClip->flushClipboard( );
322 	rXFlushableClip = Reference< XFlushableClipboard >( );
323 
324 	xClipNotifier->removeClipboardListener( rXClipListener );
325 	rXClipListener = Reference< XClipboardListener >( );
326 	xClipNotifier  = Reference< XClipboardNotifier >( );
327 
328 	//--------------------------------------------------
329 	// shutdown the service manager
330 	//--------------------------------------------------
331 
332 	// Cast factory to XComponent
333 	Reference< XComponent > xComponent( g_xFactory, UNO_QUERY );
334 
335 	if ( !xComponent.is() )
336 		OSL_ENSURE(sal_False, "Error shuting down");
337 
338 	// Dispose and clear factory
339 	xComponent->dispose();
340 	xComponent = Reference< XComponent >( );
341 
342 	g_xFactory.clear();
343 	g_xFactory = Reference< XMultiServiceFactory >();
344 
345 	CoUninitialize( );
346 
347 	return 0;
348 }
349