Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / dtrans / source / win32 / workbench / test_wincb.cxx
blob8a2f25fd4ccc659feda9c736a31d3a511f739337
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
34 #include <stdio.h>
35 #if defined _MSC_VER
36 #pragma warning(push,1)
37 #endif
38 #include <windows.h>
39 #include <objbase.h>
40 #if defined _MSC_VER
41 #pragma warning(pop)
42 #endif
44 #include <memory>
46 #include <process.h>
48 // my defines
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"
53 #define WRITE_CB
54 #define EVT_MANUAL_RESET TRUE
55 #define EVT_INIT_NONSIGNALED FALSE
56 #define EVT_NONAME ""
58 // namespaces
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;
68 // globales
70 Reference< XTransferable > rXTransfRead;
71 HANDLE g_hEvtThreadWakeup;
73 class CClipboardListener : public WeakImplHelper < XClipboardListener >
75 public:
76 ~CClipboardListener( );
78 // XClipboardListener
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 >
100 public:
101 CTransferable( );
103 // 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);
112 // XClipboardOwner
114 virtual void SAL_CALL lostOwnership( const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans )
115 throw(RuntimeException);
117 private:
118 Sequence< DataFlavor > m_FlavorList;
119 OUString m_Data;
122 // ctor
124 CTransferable::CTransferable( ) :
125 m_FlavorList( 1 ),
126 m_Data( OUString("I bought a new bike!") )
128 DataFlavor df;
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;
139 // getTransferData
141 Any SAL_CALL CTransferable::getTransferData( const DataFlavor& aFlavor )
142 throw(UnsupportedFlavorException, IOException, RuntimeException)
144 Any anyData;
147 if ( aFlavor.MimeType == m_FlavorList[0].MimeType )
148 anyData = makeAny( m_Data );
150 if ( aFlavor.MimeType.equalsIgnoreCase( m_FlavorList[0].MimeType ) )
152 OString text(
153 m_Data.getStr( ),
154 m_Data.getLength( ),
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 );
163 else
164 throw UnsupportedFlavorException( );
166 return anyData;
169 // getTransferDataFlavors
171 Sequence< DataFlavor > SAL_CALL CTransferable::getTransferDataFlavors( )
172 throw(RuntimeException)
174 return m_FlavorList;
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 )
186 return sal_True;
188 return sal_False;
191 // lostOwnership
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 );
200 // main
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 );
211 char buff[6];
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");
226 return(-1);
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" );
237 return(-1);
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();
278 xComponent.clear();
280 g_xFactory.clear();
282 CoUninitialize( );
284 return 0;
287 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */