LibreOffice Module dtrans (master)  1
test_wincb.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include "../misc/ImplHelper.hxx"
21 
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>
33 
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
43 
44 #include <memory>
45 
46 #include <process.h>
47 
48 // my defines
49 
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 ""
57 
58 // namespaces
59 
60 using namespace ::std;
61 using namespace ::cppu;
62 using namespace ::com::sun::star::datatransfer;
64 using namespace ::com::sun::star::uno;
65 using namespace ::com::sun::star::io;
66 using namespace ::com::sun::star::lang;
67 
68 // globales
69 
70 Reference< XTransferable > rXTransfRead;
72 
73 class CClipboardListener : public WeakImplHelper < XClipboardListener >
74 {
75 public:
77 
78  // XClipboardListener
79 
80  virtual void SAL_CALL disposing( const EventObject& Source ) throw(RuntimeException);
81  virtual void SAL_CALL changedContents( const ClipboardEvent& event ) throw( RuntimeException );
82 };
83 
85 {
86 }
87 
88 void SAL_CALL CClipboardListener::disposing( const EventObject& Source ) throw(RuntimeException)
89 {
90 
91 }
92 
93 void SAL_CALL CClipboardListener::changedContents( const ClipboardEvent& event ) throw( RuntimeException )
94 {
95  //MessageBox( NULL, TEXT("Clipboard content changed"), TEXT("Info"), MB_OK | MB_ICONINFORMATION );
96 }
97 
98 class CTransferable : public WeakImplHelper< XClipboardOwner, XTransferable >
99 {
100 public:
101  CTransferable( );
102 
103  // XTransferable
104 
105  virtual Any SAL_CALL getTransferData( const DataFlavor& aFlavor )
106  throw(UnsupportedFlavorException, IOException, RuntimeException);
107 
108  virtual Sequence< DataFlavor > SAL_CALL getTransferDataFlavors( ) throw(RuntimeException);
109 
110  virtual sal_Bool SAL_CALL isDataFlavorSupported( const DataFlavor& aFlavor ) throw(RuntimeException);
111 
112  // XClipboardOwner
113 
114  virtual void SAL_CALL lostOwnership( const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans )
115  throw(RuntimeException);
116 
117 private:
118  Sequence< DataFlavor > m_FlavorList;
119  OUString m_Data;
120 };
121 
122 // ctor
123 
125  m_FlavorList( 1 ),
126  m_Data( OUString("I bought a new bike!") )
127 {
128  DataFlavor df;
129 
130  //df.MimeType = L"text/plain;charset=utf-16";
131  //df.DataType = cppu::UnoType<OUString>::get();
132 
133  df.MimeType = L"text/plain;charset=Windows1252";
134  df.DataType = cppu::UnoType<Sequence< sal_Int8 >>::get();
135 
136  m_FlavorList[0] = df;
137 }
138 
139 // getTransferData
140 
141 Any SAL_CALL CTransferable::getTransferData( const DataFlavor& aFlavor )
142  throw(UnsupportedFlavorException, IOException, RuntimeException)
143 {
144  Any anyData;
145 
146  /*
147  if ( aFlavor.MimeType == m_FlavorList[0].MimeType )
148  anyData = makeAny( m_Data );
149  */
150  if ( aFlavor.MimeType.equalsIgnoreCase( m_FlavorList[0].MimeType ) )
151  {
152  OString text(
153  m_Data.getStr( ),
154  m_Data.getLength( ),
155  RTL_TEXTENCODING_ASCII_US );
156 
157  Sequence< sal_Int8 > textStream( text.getLength( ) + 1 );
158 
159  memcpy( textStream.getArray( ), text.getStr( ), textStream.getLength( ) );
160 
161  anyData = makeAny( textStream );
162  }
163  else
164  throw UnsupportedFlavorException( );
165 
166  return anyData;
167 }
168 
169 // getTransferDataFlavors
170 
171 Sequence< DataFlavor > SAL_CALL CTransferable::getTransferDataFlavors( )
172  throw(RuntimeException)
173 {
174  return m_FlavorList;
175 }
176 
177 // isDataFlavorSupported
178 
179 sal_Bool SAL_CALL CTransferable::isDataFlavorSupported( const DataFlavor& aFlavor )
180  throw(RuntimeException)
181 {
182  sal_Int32 nLength = m_FlavorList.getLength( );
183 
184  for ( sal_Int32 i = 0; i < nLength; ++i )
185  if ( m_FlavorList[i].MimeType == aFlavor.MimeType )
186  return sal_True;
187 
188  return sal_False;
189 }
190 
191 // lostOwnership
192 
194  const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans )
195  throw(RuntimeException)
196 {
197  //MessageBox( NULL, TEXT("No longer clipboard owner"), TEXT("Info"), MB_OK | MB_ICONINFORMATION );
198 }
199 
200 // main
201 
202 int SAL_CALL main( int nArgc, char* Argv[] )
203 {
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 );
210 
211  char buff[6];
212 
213  LCID lcid = MAKELCID( MAKELANGID( LANG_GERMAN, SUBLANG_GERMAN ), SORT_DEFAULT );
214 
215  BOOL bValid = IsValidLocale( lcid, LCID_SUPPORTED );
216  GetLocaleInfoA( lcid, LOCALE_IDEFAULTANSICODEPAGE, buff, sizeof( buff ) );
217 
218  // get the global service-manager
219 
220  Reference< XMultiServiceFactory > g_xFactory( createRegistryServiceFactory( RDB_SYSPATH ) );
221 
222  // Print a message if an error occurred.
223  if ( !g_xFactory.is( ) )
224  {
225  OSL_FAIL("Can't create RegistryServiceFactory");
226  return(-1);
227  }
228 
229  // try to get an Interface to a XFilePicker Service
230 
231  Reference< XTransferable > rXTransf( static_cast< XTransferable* >( new CTransferable ) );
232 
233  Reference< XClipboard >xClipboard( g_xFactory->createInstance( WINCLIPBOARD_SERVICE_NAME ), UNO_QUERY );
234  if ( !xClipboard.is( ) )
235  {
236  OSL_FAIL( "Error creating Clipboard Service" );
237  return(-1);
238  }
239 
240  Reference< XClipboardNotifier > xClipNotifier( xClipboard, UNO_QUERY );
241  Reference< XClipboardListener > rXClipListener( static_cast< XClipboardListener* >( new CClipboardListener() ) );
242  xClipNotifier->addClipboardListener( rXClipListener );
243 
244  MessageBox( NULL, TEXT("Go"), TEXT("INFO"), MB_OK|MB_ICONINFORMATION);
245 
246  // set new clipboard content
247  xClipboard->setContents( rXTransf, Reference< XClipboardOwner >( rXTransf, UNO_QUERY ) );
248 
249  /*
250  MessageBox( NULL, TEXT("Clear content"), TEXT("INFO"), MB_OK|MB_ICONINFORMATION);
251 
252  Reference< XClipboardOwner > rXClipOwner;
253  Reference< XTransferable > rXEmptyTransf;
254  xClipboard->setContents( rXEmptyTransf, rXClipOwner );
255  */
256 
257  MessageBox( NULL, TEXT("Stop"), TEXT("INFO"), MB_OK|MB_ICONINFORMATION);
258 
259  // flush the clipboard content
260  Reference< XFlushableClipboard > rXFlushableClip( xClipboard, UNO_QUERY );
261  rXFlushableClip->flushClipboard( );
262  rXFlushableClip.clear();
263 
264  xClipNotifier->removeClipboardListener( rXClipListener );
265  rXClipListener.clear();
266  xClipNotifier.clear();
267 
268  // shutdown the service manager
269 
270  // Cast factory to XComponent
271  Reference< XComponent > xComponent( g_xFactory, UNO_QUERY );
272 
273  if ( !xComponent.is() )
274  OSL_FAIL("Error shutting down");
275 
276  // Dispose and clear factory
277  xComponent->dispose();
278  xComponent.clear();
279 
280  g_xFactory.clear();
281 
282  CoUninitialize( );
283 
284  return 0;
285 }
286 
287 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
HANDLE g_hEvtThreadWakeup
Definition: test_wincb.cxx:71
virtual void SAL_CALL lostOwnership(const Reference< XClipboard > &xClipboard, const Reference< XTransferable > &xTrans)
Definition: test_wincb.cxx:193
Reference< XTransferable > rXTransfRead
Definition: test_wincb.cxx:70
SAL_DLLPUBLIC_EXPORT css::uno::Reference< css::lang::XMultiServiceFactory > SAL_CALL createRegistryServiceFactory(OUString const &, OUString const &, sal_Bool, OUString const &)
return NULL
const wchar_t *typedef BOOL
OUString m_Data
Definition: test_wincb.cxx:119
css::uno::Reference< css::lang::XComponent > xComponent
virtual Sequence< DataFlavor > SAL_CALL getTransferDataFlavors()
Definition: test_wincb.cxx:171
int i
virtual void SAL_CALL disposing(const EventObject &Source)
Definition: test_wincb.cxx:88
#define sal_True
unsigned char sal_Bool
virtual void SAL_CALL changedContents(const ClipboardEvent &event)
Definition: test_wincb.cxx:93
Reference< XMultiServiceFactory > g_xFactory
virtual sal_Bool SAL_CALL isDataFlavorSupported(const DataFlavor &aFlavor)
Definition: test_wincb.cxx:179
#define WINCLIPBOARD_SERVICE_NAME
Definition: test_wincb.cxx:52
#define sal_False
#define RDB_SYSPATH
Definition: test_wincb.cxx:51
def text(shape, st)
int SAL_CALL main(int nArgc, char *Argv[])
Definition: test_wincb.cxx:202
def MessageBox
virtual Any SAL_CALL getTransferData(const DataFlavor &aFlavor)
Definition: test_wincb.cxx:141
Sequence< DataFlavor > m_FlavorList
Definition: test_wincb.cxx:118
sal_Int32 const nLength
TEXT
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo
css::uno::Any SAL_CALL makeAny(const SharedUNOComponent< INTERFACE, COMPONENT > &value)