1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "../misc/ImplHelper.hxx"
22 #include <cppuhelper/servicefactory.hxx>
23 #include <com/sun/star/datatransfer/XTransferable.hpp>
24 #include <com/sun/star/datatransfer/clipboard/XClipboardOwner.hpp>
25 #include <com/sun/star/datatransfer/clipboard/XClipboardNotifier.hpp>
26 #include <com/sun/star/datatransfer/clipboard/XClipboardEx.hpp>
27 #include <com/sun/star/datatransfer/clipboard/XFlushableClipboard.hpp>
28 #include <com/sun/star/lang/XComponent.hpp>
29 #include <cppuhelper/implbase.hxx>
30 #include <rtl/ustring.hxx>
31 #include <sal/types.h>
32 #include <osl/diagnose.h>
36 #pragma warning(push,1)
50 #define TEST_CLIPBOARD
51 #define RDB_SYSPATH "d:\\projects\\src623\\dtrans\\wntmsci7\\bin\\applicat.rdb"
52 #define WINCLIPBOARD_SERVICE_NAME L"com.sun.star.datatransfer.clipboard.SystemClipboard"
54 #define EVT_MANUAL_RESET TRUE
55 #define EVT_INIT_NONSIGNALED FALSE
60 using namespace ::std
;
61 using namespace ::cppu
;
62 using namespace ::com::sun::star::datatransfer
;
63 using namespace ::com::sun::star::datatransfer::clipboard
;
64 using namespace ::com::sun::star::uno
;
65 using namespace ::com::sun::star::io
;
66 using namespace ::com::sun::star::lang
;
70 Reference
< XTransferable
> rXTransfRead
;
71 HANDLE g_hEvtThreadWakeup
;
73 class CClipboardListener
: public WeakImplHelper
< XClipboardListener
>
76 ~CClipboardListener( );
80 virtual void SAL_CALL
disposing( const EventObject
& Source
) throw(RuntimeException
);
81 virtual void SAL_CALL
changedContents( const ClipboardEvent
& event
) throw( RuntimeException
);
84 CClipboardListener::~CClipboardListener( )
88 void SAL_CALL
CClipboardListener::disposing( const EventObject
& Source
) throw(RuntimeException
)
93 void SAL_CALL
CClipboardListener::changedContents( const ClipboardEvent
& event
) throw( RuntimeException
)
95 //MessageBox( NULL, TEXT("Clipboard content changed"), TEXT("Info"), MB_OK | MB_ICONINFORMATION );
98 class CTransferable
: public WeakImplHelper
< XClipboardOwner
, XTransferable
>
105 virtual Any SAL_CALL
getTransferData( const DataFlavor
& aFlavor
)
106 throw(UnsupportedFlavorException
, IOException
, RuntimeException
);
108 virtual Sequence
< DataFlavor
> SAL_CALL
getTransferDataFlavors( ) throw(RuntimeException
);
110 virtual sal_Bool SAL_CALL
isDataFlavorSupported( const DataFlavor
& aFlavor
) throw(RuntimeException
);
114 virtual void SAL_CALL
lostOwnership( const Reference
< XClipboard
>& xClipboard
, const Reference
< XTransferable
>& xTrans
)
115 throw(RuntimeException
);
118 Sequence
< DataFlavor
> m_FlavorList
;
124 CTransferable::CTransferable( ) :
126 m_Data( OUString("I bought a new bike!") )
130 //df.MimeType = L"text/plain;charset=utf-16";
131 //df.DataType = cppu::UnoType<OUString>::get();
133 df
.MimeType
= L
"text/plain;charset=Windows1252";
134 df
.DataType
= cppu::UnoType
<Sequence
< sal_Int8
>>::get();
136 m_FlavorList
[0] = df
;
141 Any SAL_CALL
CTransferable::getTransferData( const DataFlavor
& aFlavor
)
142 throw(UnsupportedFlavorException
, IOException
, RuntimeException
)
147 if ( aFlavor.MimeType == m_FlavorList[0].MimeType )
148 anyData = makeAny( m_Data );
150 if ( aFlavor
.MimeType
.equalsIgnoreCase( m_FlavorList
[0].MimeType
) )
155 RTL_TEXTENCODING_ASCII_US
);
157 Sequence
< sal_Int8
> textStream( text
.getLength( ) + 1 );
159 memcpy( textStream
.getArray( ), text
.getStr( ), textStream
.getLength( ) );
161 anyData
= makeAny( textStream
);
164 throw UnsupportedFlavorException( );
169 // getTransferDataFlavors
171 Sequence
< DataFlavor
> SAL_CALL
CTransferable::getTransferDataFlavors( )
172 throw(RuntimeException
)
177 // isDataFlavorSupported
179 sal_Bool SAL_CALL
CTransferable::isDataFlavorSupported( const DataFlavor
& aFlavor
)
180 throw(RuntimeException
)
182 sal_Int32 nLength
= m_FlavorList
.getLength( );
184 for ( sal_Int32 i
= 0; i
< nLength
; ++i
)
185 if ( m_FlavorList
[i
].MimeType
== aFlavor
.MimeType
)
193 void SAL_CALL
CTransferable::lostOwnership(
194 const Reference
< XClipboard
>& xClipboard
, const Reference
< XTransferable
>& xTrans
)
195 throw(RuntimeException
)
197 //MessageBox( NULL, TEXT("No longer clipboard owner"), TEXT("Info"), MB_OK | MB_ICONINFORMATION );
202 int SAL_CALL
main( int nArgc
, char* Argv
[] )
204 // create a multi-threaded apartment; we can test only
205 // with a multithreaded apartment because for a single
206 // threaded apartment we need a message loop to deliver
207 // messages to our XTDataObject
208 //HRESULT hr = CoInitializeEx( NULL, COINIT_MULTITHREADED );
209 (void)CoInitialize( NULL
);
213 LCID lcid
= MAKELCID( MAKELANGID( LANG_GERMAN
, SUBLANG_GERMAN
), SORT_DEFAULT
);
215 BOOL bValid
= IsValidLocale( lcid
, LCID_SUPPORTED
);
216 GetLocaleInfoA( lcid
, LOCALE_IDEFAULTANSICODEPAGE
, buff
, sizeof( buff
) );
218 // get the global service-manager
220 Reference
< XMultiServiceFactory
> g_xFactory( createRegistryServiceFactory( RDB_SYSPATH
) );
222 // Print a message if an error occurred.
223 if ( !g_xFactory
.is( ) )
225 OSL_FAIL("Can't create RegistryServiceFactory");
229 // try to get an Interface to a XFilePicker Service
231 Reference
< XTransferable
> rXTransf( static_cast< XTransferable
* >( new CTransferable
) );
233 Reference
< XClipboard
>xClipboard( g_xFactory
->createInstance( WINCLIPBOARD_SERVICE_NAME
), UNO_QUERY
);
234 if ( !xClipboard
.is( ) )
236 OSL_FAIL( "Error creating Clipboard Service" );
240 Reference
< XClipboardNotifier
> xClipNotifier( xClipboard
, UNO_QUERY
);
241 Reference
< XClipboardListener
> rXClipListener( static_cast< XClipboardListener
* >( new CClipboardListener() ) );
242 xClipNotifier
->addClipboardListener( rXClipListener
);
244 MessageBox( NULL
, TEXT("Go"), TEXT("INFO"), MB_OK
|MB_ICONINFORMATION
);
246 // set new clipboard content
247 xClipboard
->setContents( rXTransf
, Reference
< XClipboardOwner
>( rXTransf
, UNO_QUERY
) );
250 MessageBox( NULL, TEXT("Clear content"), TEXT("INFO"), MB_OK|MB_ICONINFORMATION);
252 Reference< XClipboardOwner > rXClipOwner;
253 Reference< XTransferable > rXEmptyTransf;
254 xClipboard->setContents( rXEmptyTransf, rXClipOwner );
257 MessageBox( NULL
, TEXT("Stop"), TEXT("INFO"), MB_OK
|MB_ICONINFORMATION
);
259 // flush the clipboard content
260 Reference
< XFlushableClipboard
> rXFlushableClip( xClipboard
, UNO_QUERY
);
261 rXFlushableClip
->flushClipboard( );
262 rXFlushableClip
.clear();
264 xClipNotifier
->removeClipboardListener( rXClipListener
);
265 rXClipListener
.clear();
266 xClipNotifier
.clear();
268 // shutdown the service manager
270 // Cast factory to XComponent
271 Reference
< XComponent
> xComponent( g_xFactory
, UNO_QUERY
);
273 if ( !xComponent
.is() )
274 OSL_FAIL("Error shutting down");
276 // Dispose and clear factory
277 xComponent
->dispose();
287 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */