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