1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: test_wincb.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dtrans.hxx"
35 //_________________________________________________________________________________________________________________________
37 //_________________________________________________________________________________________________________________________
40 #include "..\misc\ImplHelper.hxx"
42 //_________________________________________________________________________________________________________________________
44 //_________________________________________________________________________________________________________________________
45 #include <cppuhelper/servicefactory.hxx>
46 #include <com/sun/star/datatransfer/XTransferable.hpp>
47 #include <com/sun/star/datatransfer/clipboard/XClipboardOwner.hpp>
48 #include <com/sun/star/datatransfer/clipboard/XClipboardNotifier.hpp>
49 #include <com/sun/star/datatransfer/clipboard/XClipboardEx.hpp>
50 #include <com/sun/star/datatransfer/clipboard/XFlushableClipboard.hpp>
51 #include <com/sun/star/lang/XComponent.hpp>
52 #include <cppuhelper/implbase1.hxx>
53 #include <cppuhelper/implbase2.hxx>
54 #include <rtl/ustring.hxx>
55 #include <sal/types.h>
56 #include <osl/diagnose.h>
60 #pragma warning(push,1)
72 //-------------------------------------------------------------
74 //-------------------------------------------------------------
76 #define TEST_CLIPBOARD
77 #define RDB_SYSPATH "d:\\projects\\src623\\dtrans\\wntmsci7\\bin\\applicat.rdb"
78 #define WINCLIPBOARD_SERVICE_NAME L"com.sun.star.datatransfer.clipboard.SystemClipboard"
80 #define EVT_MANUAL_RESET TRUE
81 #define EVT_INIT_NONSIGNALED FALSE
84 //------------------------------------------------------------
86 //------------------------------------------------------------
88 using namespace ::rtl
;
89 using namespace ::std
;
90 using namespace ::cppu
;
91 using namespace ::com::sun::star::datatransfer
;
92 using namespace ::com::sun::star::datatransfer::clipboard
;
93 using namespace ::com::sun::star::uno
;
94 using namespace ::com::sun::star::io
;
95 using namespace ::com::sun::star::lang
;
97 //------------------------------------------------------------
99 //------------------------------------------------------------
101 Reference
< XTransferable
> rXTransfRead
;
102 HANDLE g_hEvtThreadWakeup
;
104 //------------------------------------------------------------
106 //------------------------------------------------------------
108 class CClipboardListener
: public WeakImplHelper1
< XClipboardListener
>
111 ~CClipboardListener( );
113 //-------------------------------------------------
114 // XClipboardListener
115 //-------------------------------------------------
117 virtual void SAL_CALL
disposing( const EventObject
& Source
) throw(RuntimeException
);
118 virtual void SAL_CALL
changedContents( const ClipboardEvent
& event
) throw( RuntimeException
);
121 CClipboardListener::~CClipboardListener( )
125 void SAL_CALL
CClipboardListener::disposing( const EventObject
& Source
) throw(RuntimeException
)
130 void SAL_CALL
CClipboardListener::changedContents( const ClipboardEvent
& event
) throw( RuntimeException
)
132 //MessageBox( NULL, TEXT("Clipboard content changed"), TEXT("Info"), MB_OK | MB_ICONINFORMATION );
135 //------------------------------------------------------------
137 //------------------------------------------------------------
139 class CTransferable
: public WeakImplHelper2
< XClipboardOwner
, XTransferable
>
144 //-------------------------------------------------
146 //-------------------------------------------------
148 virtual Any SAL_CALL
getTransferData( const DataFlavor
& aFlavor
)
149 throw(UnsupportedFlavorException
, IOException
, RuntimeException
);
151 virtual Sequence
< DataFlavor
> SAL_CALL
getTransferDataFlavors( ) throw(RuntimeException
);
153 virtual sal_Bool SAL_CALL
isDataFlavorSupported( const DataFlavor
& aFlavor
) throw(RuntimeException
);
155 //-------------------------------------------------
157 //-------------------------------------------------
159 virtual void SAL_CALL
lostOwnership( const Reference
< XClipboard
>& xClipboard
, const Reference
< XTransferable
>& xTrans
)
160 throw(RuntimeException
);
163 Sequence
< DataFlavor
> m_FlavorList
;
167 //----------------------------------------------------------------
169 //----------------------------------------------------------------
171 CTransferable::CTransferable( ) :
173 m_Data( OUString::createFromAscii( "Ich habe mir ein neues Fahrrad gekauft!" ) )
177 //df.MimeType = L"text/plain;charset=utf-16";
178 //df.DataType = getCppuType( ( OUString* )0 );
180 df
.MimeType
= L
"text/plain;charset=Windows1252";
181 df
.DataType
= getCppuType( (Sequence
< sal_Int8
>*)0 );
183 m_FlavorList
[0] = df
;
186 //----------------------------------------------------------------
188 //----------------------------------------------------------------
190 Any SAL_CALL
CTransferable::getTransferData( const DataFlavor
& aFlavor
)
191 throw(UnsupportedFlavorException
, IOException
, RuntimeException
)
196 if ( aFlavor.MimeType == m_FlavorList[0].MimeType )
197 anyData = makeAny( m_Data );
199 if ( aFlavor
.MimeType
.equalsIgnoreCase( m_FlavorList
[0].MimeType
) )
204 RTL_TEXTENCODING_ASCII_US
);
206 Sequence
< sal_Int8
> textStream( text
.getLength( ) + 1 );
208 rtl_copyMemory( textStream
.getArray( ), text
.getStr( ), textStream
.getLength( ) );
210 anyData
= makeAny( textStream
);
213 throw UnsupportedFlavorException( );
218 //----------------------------------------------------------------
219 // getTransferDataFlavors
220 //----------------------------------------------------------------
222 Sequence
< DataFlavor
> SAL_CALL
CTransferable::getTransferDataFlavors( )
223 throw(RuntimeException
)
228 //----------------------------------------------------------------
229 // isDataFlavorSupported
230 //----------------------------------------------------------------
232 sal_Bool SAL_CALL
CTransferable::isDataFlavorSupported( const DataFlavor
& aFlavor
)
233 throw(RuntimeException
)
235 sal_Int32 nLength
= m_FlavorList
.getLength( );
237 for ( sal_Int32 i
= 0; i
< nLength
; ++i
)
238 if ( m_FlavorList
[i
].MimeType
== aFlavor
.MimeType
)
244 //----------------------------------------------------------------
246 //----------------------------------------------------------------
248 void SAL_CALL
CTransferable::lostOwnership(
249 const Reference
< XClipboard
>& xClipboard
, const Reference
< XTransferable
>& xTrans
)
250 throw(RuntimeException
)
252 //MessageBox( NULL, TEXT("No longer clipboard owner"), TEXT("Info"), MB_OK | MB_ICONINFORMATION );
255 //----------------------------------------------------------------
257 //----------------------------------------------------------------
259 int SAL_CALL
main( int nArgc
, char* Argv
[] )
261 // create a multi-threaded apartment; we can test only
262 // with a multithreaded apartment because for a single
263 // threaded apartment we need a message loop to deliver
264 // messages to our XTDataObject
265 //HRESULT hr = CoInitializeEx( NULL, COINIT_MULTITHREADED );
266 HRESULT hr
= CoInitialize( NULL
);
270 LCID lcid
= MAKELCID( MAKELANGID( LANG_GERMAN
, SUBLANG_GERMAN
), SORT_DEFAULT
);
272 BOOL bValid
= IsValidLocale( lcid
, LCID_SUPPORTED
);
273 GetLocaleInfoA( lcid
, LOCALE_IDEFAULTANSICODEPAGE
, buff
, sizeof( buff
) );
275 //-------------------------------------------------
276 // get the global service-manager
277 //-------------------------------------------------
279 OUString rdbName
= OUString( RTL_CONSTASCII_USTRINGPARAM( RDB_SYSPATH
) );
280 Reference
< XMultiServiceFactory
> g_xFactory( createRegistryServiceFactory( rdbName
) );
282 // Print a message if an error occured.
283 if ( !g_xFactory
.is( ) )
285 OSL_ENSURE(sal_False
, "Can't create RegistryServiceFactory");
289 //-------------------------------------------------
290 // try to get an Interface to a XFilePicker Service
291 //-------------------------------------------------
293 Reference
< XTransferable
> rXTransf( static_cast< XTransferable
* >( new CTransferable
) );
295 Reference
< XClipboard
>
296 xClipboard( g_xFactory
->createInstance( OUString( WINCLIPBOARD_SERVICE_NAME
) ), UNO_QUERY
);
297 if ( !xClipboard
.is( ) )
299 OSL_ENSURE( sal_False
, "Error creating Clipboard Service" );
303 Reference
< XClipboardNotifier
> xClipNotifier( xClipboard
, UNO_QUERY
);
304 Reference
< XClipboardListener
> rXClipListener( static_cast< XClipboardListener
* >( new CClipboardListener() ) );
305 xClipNotifier
->addClipboardListener( rXClipListener
);
307 MessageBox( NULL
, TEXT("Go"), TEXT("INFO"), MB_OK
|MB_ICONINFORMATION
);
309 // set new clipboard content
310 xClipboard
->setContents( rXTransf
, Reference
< XClipboardOwner
>( rXTransf
, UNO_QUERY
) );
313 MessageBox( NULL, TEXT("Clear content"), TEXT("INFO"), MB_OK|MB_ICONINFORMATION);
315 Reference< XClipboardOwner > rXClipOwner;
316 Reference< XTransferable > rXEmptyTransf;
317 xClipboard->setContents( rXEmptyTransf, rXClipOwner );
320 MessageBox( NULL
, TEXT("Stop"), TEXT("INFO"), MB_OK
|MB_ICONINFORMATION
);
322 // flush the clipboard content
323 Reference
< XFlushableClipboard
> rXFlushableClip( xClipboard
, UNO_QUERY
);
324 rXFlushableClip
->flushClipboard( );
325 rXFlushableClip
= Reference
< XFlushableClipboard
>( );
327 xClipNotifier
->removeClipboardListener( rXClipListener
);
328 rXClipListener
= Reference
< XClipboardListener
>( );
329 xClipNotifier
= Reference
< XClipboardNotifier
>( );
331 //--------------------------------------------------
332 // shutdown the service manager
333 //--------------------------------------------------
335 // Cast factory to XComponent
336 Reference
< XComponent
> xComponent( g_xFactory
, UNO_QUERY
);
338 if ( !xComponent
.is() )
339 OSL_ENSURE(sal_False
, "Error shuting down");
341 // Dispose and clear factory
342 xComponent
->dispose();
343 xComponent
= Reference
< XComponent
>( );
346 g_xFactory
= Reference
< XMultiServiceFactory
>();