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 //------------------------------------------------------------------------ 25 // includes 26 //------------------------------------------------------------------------ 27 28 #include <svpm.h> 29 #include <string.h> 30 #include "Os2Clipboard.hxx" 31 32 //------------------------------------------------------------------------ 33 // namespace directives 34 //------------------------------------------------------------------------ 35 36 using namespace com::sun::star::datatransfer; 37 using namespace com::sun::star::datatransfer::clipboard; 38 using namespace com::sun::star::datatransfer::clipboard::RenderingCapabilities; 39 using namespace com::sun::star::lang; 40 using namespace com::sun::star::uno; 41 using namespace cppu; 42 using namespace osl; 43 using namespace rtl; 44 using namespace os2; 45 46 const Type CPPUTYPE_SEQINT8 = getCppuType( ( Sequence< sal_Int8 >* )0 ); 47 const Type CPPUTYPE_OUSTRING = getCppuType( (OUString*)0 ); 48 49 #define DTRANS_OBJ_CLASSNAME "DTRANSOBJWND" 50 51 // ----------------------------------------------------------------------- 52 53 inline void SetWindowPtr( HWND hWnd, Os2Clipboard* pThis ) 54 { 55 WinSetWindowULong( hWnd, QWL_USER, (ULONG)pThis ); 56 } 57 58 inline Os2Clipboard* GetWindowPtr( HWND hWnd ) 59 { 60 return (Os2Clipboard*)WinQueryWindowULong( hWnd, QWL_USER ); 61 } 62 63 // ----------------------------------------------------------------------- 64 65 MRESULT EXPENTRY DtransObjWndProc( HWND hWnd, ULONG nMsg, MPARAM nMP1, MPARAM nMP2 ) 66 { 67 68 switch ( nMsg ) 69 { 70 case WM_DRAWCLIPBOARD: // clipboard content has changed 71 { 72 Os2Clipboard* os2Clipboard = GetWindowPtr( hWnd); 73 if (os2Clipboard) 74 { 75 //MutexGuard aGuard(os2Clipboard->m_aMutex); 76 debug_printf("WM_DRAWCLIPBOARD os2Clipboard %08x\n", os2Clipboard); 77 if (os2Clipboard->m_bInSetClipboardData) 78 { 79 debug_printf("WM_DRAWCLIPBOARD our change\n"); 80 } 81 else 82 { 83 // notify listener for clipboard change 84 debug_printf("WM_DRAWCLIPBOARD notify change\n"); 85 os2Clipboard->notifyAllClipboardListener(); 86 } 87 } 88 } 89 break; 90 } 91 92 return WinDefWindowProc( hWnd, nMsg, nMP1, nMP2 ); 93 } 94 95 // ----------------------------------------------------------------------- 96 97 Os2Clipboard::Os2Clipboard() : 98 m_aMutex(), 99 WeakComponentImplHelper4< XClipboardEx, XClipboardNotifier, XServiceInfo, XInitialization > (m_aMutex), 100 m_bInitialized(sal_False), 101 m_bInSetClipboardData(sal_False) 102 { 103 MutexGuard aGuard(m_aMutex); 104 105 debug_printf("Os2Clipboard::Os2Clipboard\n"); 106 hAB = WinQueryAnchorBlock( HWND_DESKTOP ); 107 hText = 0; 108 hBitmap = 0; 109 110 #if 0 111 // register object class 112 if ( WinRegisterClass( hAB, (PSZ)DTRANS_OBJ_CLASSNAME, 113 (PFNWP)DtransObjWndProc, 0, sizeof(ULONG) )) 114 { 115 APIRET rc; 116 // create object window to get clip viewer messages 117 hObjWnd = WinCreateWindow( HWND_OBJECT, (PCSZ)DTRANS_OBJ_CLASSNAME, 118 (PCSZ)"", 0, 0, 0, 0, 0, 119 HWND_OBJECT, HWND_TOP, 120 222, NULL, NULL); 121 // store pointer 122 SetWindowPtr( hObjWnd, this); 123 // register the viewer window 124 rc = WinOpenClipbrd(hAB); 125 rc = WinSetClipbrdViewer(hAB, hObjWnd); 126 rc = WinCloseClipbrd(hAB); 127 } 128 #endif 129 130 } 131 132 Os2Clipboard::~Os2Clipboard() 133 { 134 debug_printf("Os2Clipboard::~Os2Clipboard\n"); 135 } 136 137 void SAL_CALL Os2Clipboard::initialize( const Sequence< Any >& aArguments ) 138 throw(Exception, RuntimeException) 139 { 140 if (!m_bInitialized) 141 { 142 for (sal_Int32 n = 0, nmax = aArguments.getLength(); n < nmax; n++) 143 if (aArguments[n].getValueType() == getCppuType((OUString *) 0)) 144 { 145 aArguments[0] >>= m_aName; 146 break; 147 } 148 } 149 } 150 151 OUString SAL_CALL Os2Clipboard::getImplementationName() throw( RuntimeException ) 152 { 153 debug_printf("Os2Clipboard::getImplementationName\n"); 154 return OUString::createFromAscii( OS2_CLIPBOARD_IMPL_NAME ); 155 } 156 157 sal_Bool SAL_CALL Os2Clipboard::supportsService( const OUString& ServiceName ) throw( RuntimeException ) 158 { 159 debug_printf("Os2Clipboard::supportsService\n"); 160 Sequence < OUString > SupportedServicesNames = Os2Clipboard_getSupportedServiceNames(); 161 162 for ( sal_Int32 n = SupportedServicesNames.getLength(); n--; ) 163 if (SupportedServicesNames[n].compareTo(ServiceName) == 0) 164 return sal_True; 165 166 return sal_False; 167 } 168 169 Sequence< OUString > SAL_CALL Os2Clipboard::getSupportedServiceNames() throw( RuntimeException ) 170 { 171 debug_printf("Os2Clipboard::getSupportedServiceNames\n"); 172 return Os2Clipboard_getSupportedServiceNames(); 173 } 174 175 Reference< XTransferable > SAL_CALL Os2Clipboard::getContents() throw( RuntimeException ) 176 { 177 debug_printf("Os2Clipboard::getContents\n"); 178 MutexGuard aGuard(m_aMutex); 179 180 // os2 can have only one viewer at time, and we don't get a notification 181 // when the viewer changes. So we need to check handles of clipboard 182 // data and compare with previous handles 183 if (UWinOpenClipbrd(hAB)) { 184 sal_Bool fireChanged = sal_False; 185 ULONG handle = UWinQueryClipbrdData( hAB, UCLIP_CF_UNICODETEXT); 186 if (handle) { 187 if (handle != hText) { 188 hText = handle; 189 fireChanged = sal_True; 190 } 191 } 192 handle = UWinQueryClipbrdData( hAB, UCLIP_CF_BITMAP); 193 if (handle) { 194 if (handle != hBitmap) { 195 hBitmap = handle; 196 fireChanged = sal_True; 197 } 198 } 199 UWinCloseClipbrd( hAB); 200 if (fireChanged) 201 { 202 // notify listener for clipboard change 203 debug_printf("Os2Clipboard::getContents notify change\n"); 204 notifyAllClipboardListener(); 205 } 206 } 207 208 if( ! m_aContents.is() ) 209 m_aContents = new Os2Transferable( static_cast< OWeakObject* >(this) ); 210 211 return m_aContents; 212 } 213 214 void SAL_CALL Os2Clipboard::setContents( const Reference< XTransferable >& xTrans, const Reference< XClipboardOwner >& xClipboardOwner ) throw( RuntimeException ) 215 { 216 debug_printf("Os2Clipboard::setContents\n"); 217 // remember old values for callbacks before setting the new ones. 218 ClearableMutexGuard aGuard(m_aMutex); 219 220 Reference< XClipboardOwner > oldOwner(m_aOwner); 221 m_aOwner = xClipboardOwner; 222 223 Reference< XTransferable > oldContents(m_aContents); 224 m_aContents = xTrans; 225 226 aGuard.clear(); 227 228 // notify old owner on loss of ownership 229 if( oldOwner.is() ) 230 oldOwner->lostOwnership(static_cast < XClipboard * > (this), oldContents); 231 232 // notify all listeners on content changes 233 OInterfaceContainerHelper *pContainer = 234 rBHelper.aLC.getContainer(getCppuType( (Reference < XClipboardListener > *) 0)); 235 if (pContainer) 236 { 237 ClipboardEvent aEvent(static_cast < XClipboard * > (this), m_aContents); 238 OInterfaceIteratorHelper aIterator(*pContainer); 239 240 while (aIterator.hasMoreElements()) 241 { 242 Reference < XClipboardListener > xListener(aIterator.next(), UNO_QUERY); 243 if (xListener.is()) 244 xListener->changedContents(aEvent); 245 } 246 } 247 248 #if OSL_DEBUG_LEVEL>0 249 // dump list of available mimetypes 250 Sequence< DataFlavor > aFlavors( m_aContents->getTransferDataFlavors() ); 251 for( int i = 0; i < aFlavors.getLength(); i++ ) 252 debug_printf("Os2Clipboard::setContents available mimetype: %d %s\n", 253 i, CHAR_POINTER(aFlavors.getConstArray()[i].MimeType)); 254 #endif 255 256 // we can only export text or bitmap 257 DataFlavor nFlavorText( OUString::createFromAscii( "text/plain;charset=utf-16" ), 258 OUString::createFromAscii( "Unicode-Text" ), CPPUTYPE_OUSTRING); 259 DataFlavor nFlavorBitmap( OUString::createFromAscii( "application/x-openoffice-bitmap;windows_formatname=\"Bitmap\"" ), 260 OUString::createFromAscii( "Bitmap" ), CPPUTYPE_DEFAULT); 261 262 // try text transfer data (if any) 263 PSZ pSharedText = NULL; 264 HBITMAP hbm = NULL; 265 try 266 { 267 Any aAny = m_aContents->getTransferData( nFlavorText ); 268 if (aAny.hasValue()) 269 { 270 APIRET rc; 271 // copy unicode text to clipboard 272 OUString aString; 273 aAny >>= aString; 274 // share text 275 rc = DosAllocSharedMem( (PPVOID) &pSharedText, NULL, 276 aString.getLength() * 2 + 2, 277 PAG_WRITE | PAG_COMMIT | OBJ_GIVEABLE | OBJ_ANY); 278 if (!rc) 279 memcpy( pSharedText, aString.getStr(), aString.getLength() * 2 + 2 ); 280 else 281 pSharedText = NULL; 282 debug_printf("Os2Clipboard::setContents SetClipbrdData text done\n"); 283 } 284 } catch ( UnsupportedFlavorException&) { 285 debug_printf("Os2Clipboard::setContents UnsupportedFlavorException (no text)\n"); 286 } 287 288 // try bitmap transfer data (if any) 289 try 290 { 291 Any aAnyB = m_aContents->getTransferData( nFlavorBitmap ); 292 if (aAnyB.hasValue()) 293 { 294 hbm = OOoBmpToOS2Handle( aAnyB); 295 debug_printf("Os2Clipboard::setContents SetClipbrdData bitmap done\n"); 296 } 297 } catch ( UnsupportedFlavorException&) { 298 debug_printf("Os2Clipboard::setContents UnsupportedFlavorException (no bitmap)\n"); 299 } 300 301 // copy to clipboard 302 if ( UWinOpenClipbrd( hAB) && (pSharedText || hbm)) 303 { 304 // set the flag, so we will ignore the next WM_DRAWCLIPBOARD 305 // since we generate it with following code. 306 m_bInSetClipboardData = sal_True; 307 UWinEmptyClipbrd( hAB); 308 // give pointer to clipboard (it will become owner of pSharedText!) 309 if (pSharedText) { 310 UWinSetClipbrdData( hAB, (ULONG) pSharedText, UCLIP_CF_UNICODETEXT, CFI_POINTER); 311 // update internal handle to avoid detection of this text as new data 312 hText = (ULONG)pSharedText; 313 } 314 // give bitmap to clipboard 315 if (hbm) { 316 UWinSetClipbrdData( hAB, (ULONG) hbm, UCLIP_CF_BITMAP, CFI_HANDLE); 317 // update internal handle to avoid detection of this bitmap as new data 318 hBitmap = hbm; 319 } 320 // reset the flag, so we will not ignore next WM_DRAWCLIPBOARD 321 m_bInSetClipboardData = sal_False; 322 UWinCloseClipbrd( hAB); 323 } 324 325 } 326 327 OUString SAL_CALL Os2Clipboard::getName() throw( RuntimeException ) 328 { 329 debug_printf("Os2Clipboard::getName\n"); 330 return m_aName; 331 } 332 333 sal_Int8 SAL_CALL Os2Clipboard::getRenderingCapabilities() throw( RuntimeException ) 334 { 335 debug_printf("Os2Clipboard::getRenderingCapabilities\n"); 336 return Delayed; 337 } 338 339 //======================================================================== 340 // XClipboardNotifier 341 //======================================================================== 342 343 void SAL_CALL Os2Clipboard::addClipboardListener( const Reference< XClipboardListener >& listener ) throw( RuntimeException ) 344 { 345 debug_printf("Os2Clipboard::addClipboardListener\n"); 346 MutexGuard aGuard( rBHelper.rMutex ); 347 OSL_ENSURE( !rBHelper.bInDispose, "do not add listeners in the dispose call" ); 348 OSL_ENSURE( !rBHelper.bDisposed, "object is disposed" ); 349 if (!rBHelper.bInDispose && !rBHelper.bDisposed) 350 rBHelper.aLC.addInterface( getCppuType( (const ::com::sun::star::uno::Reference< XClipboardListener > *) 0), listener ); 351 } 352 353 void SAL_CALL Os2Clipboard::removeClipboardListener( const Reference< XClipboardListener >& listener ) throw( RuntimeException ) 354 { 355 debug_printf("Os2Clipboard::removeClipboardListener\n"); 356 MutexGuard aGuard( rBHelper.rMutex ); 357 OSL_ENSURE( !rBHelper.bDisposed, "object is disposed" ); 358 if (!rBHelper.bInDispose && !rBHelper.bDisposed) 359 rBHelper.aLC.removeInterface( getCppuType( (const Reference< XClipboardListener > *) 0 ), listener ); \ 360 } 361 362 // ------------------------------------------------------------------------ 363 364 void SAL_CALL Os2Clipboard::notifyAllClipboardListener( ) 365 { 366 if ( !rBHelper.bDisposed ) 367 { 368 ClearableMutexGuard aGuard( rBHelper.rMutex ); 369 if ( !rBHelper.bDisposed ) 370 { 371 aGuard.clear( ); 372 373 ClearableMutexGuard aGuard(m_aMutex); 374 // copy member references on stack so they can be called 375 // without having the mutex 376 Reference< XClipboardOwner > xOwner( m_aOwner ); 377 Reference< XTransferable > xTrans( m_aContents ); 378 // clear members 379 m_aOwner.clear(); 380 m_aContents.clear(); 381 // release the mutex 382 aGuard.clear(); 383 384 // inform previous owner of lost ownership 385 if ( xOwner.is() ) 386 xOwner->lostOwnership(static_cast < XClipboard * > (this), m_aContents); 387 388 OInterfaceContainerHelper* pICHelper = rBHelper.aLC.getContainer( 389 getCppuType( ( Reference< XClipboardListener > * ) 0 ) ); 390 391 if ( pICHelper ) 392 { 393 try 394 { 395 OInterfaceIteratorHelper iter(*pICHelper); 396 m_aContents = 0; 397 m_aContents = new Os2Transferable( static_cast< OWeakObject* >(this) ); 398 ClipboardEvent aClipbEvent(static_cast<XClipboard*>(this), m_aContents); 399 400 while(iter.hasMoreElements()) 401 { 402 try 403 { 404 Reference<XClipboardListener> xCBListener(iter.next(), UNO_QUERY); 405 if (xCBListener.is()) 406 xCBListener->changedContents(aClipbEvent); 407 } 408 catch(RuntimeException&) 409 { 410 OSL_ENSURE( false, "RuntimeException caught" ); 411 debug_printf( "RuntimeException caught" ); 412 } 413 } 414 } 415 catch(const ::com::sun::star::lang::DisposedException&) 416 { 417 OSL_ENSURE(false, "Service Manager disposed"); 418 debug_printf( "Service Manager disposed"); 419 420 // no further clipboard changed notifications 421 //m_pImpl->unregisterClipboardViewer(); 422 } 423 424 } // end if 425 } // end if 426 } // end if 427 } 428 429 // ------------------------------------------------------------------------ 430 431 Sequence< OUString > SAL_CALL Os2Clipboard_getSupportedServiceNames() 432 { 433 Sequence< OUString > aRet(1); 434 aRet[0] = OUString::createFromAscii( OS2_CLIPBOARD_SERVICE_NAME ); 435 return aRet; 436 } 437 438 // ------------------------------------------------------------------------ 439 440 Reference< XInterface > SAL_CALL Os2Clipboard_createInstance( 441 const Reference< XMultiServiceFactory > & xMultiServiceFactory) 442 { 443 return Reference < XInterface >( ( OWeakObject * ) new Os2Clipboard()); 444 } 445 446