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