1*24acc546SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*24acc546SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*24acc546SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*24acc546SAndrew Rist * distributed with this work for additional information 6*24acc546SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*24acc546SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*24acc546SAndrew Rist * "License"); you may not use this file except in compliance 9*24acc546SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*24acc546SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*24acc546SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*24acc546SAndrew Rist * software distributed under the License is distributed on an 15*24acc546SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*24acc546SAndrew Rist * KIND, either express or implied. See the License for the 17*24acc546SAndrew Rist * specific language governing permissions and limitations 18*24acc546SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*24acc546SAndrew Rist *************************************************************/ 21*24acc546SAndrew Rist 22*24acc546SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_forms.hxx" 26cdf0e10cSrcweir #include "clipboarddispatcher.hxx" 27cdf0e10cSrcweir #include <editeng/editview.hxx> 28cdf0e10cSrcweir 29cdf0e10cSrcweir /** === begin UNO includes === **/ 30cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp> 31cdf0e10cSrcweir /** === end UNO includes === **/ 32cdf0e10cSrcweir #include <svtools/cliplistener.hxx> 33cdf0e10cSrcweir #include <svtools/transfer.hxx> 34cdf0e10cSrcweir 35cdf0e10cSrcweir //........................................................................ 36cdf0e10cSrcweir namespace frm 37cdf0e10cSrcweir { 38cdf0e10cSrcweir //........................................................................ 39cdf0e10cSrcweir 40cdf0e10cSrcweir using namespace ::com::sun::star::uno; 41cdf0e10cSrcweir using namespace ::com::sun::star::frame; 42cdf0e10cSrcweir using namespace ::com::sun::star::lang; 43cdf0e10cSrcweir using namespace ::com::sun::star::util; 44cdf0e10cSrcweir using namespace ::com::sun::star::beans; 45cdf0e10cSrcweir 46cdf0e10cSrcweir //==================================================================== 47cdf0e10cSrcweir namespace 48cdf0e10cSrcweir { createClipboardURL(OClipboardDispatcher::ClipboardFunc _eFunc)49cdf0e10cSrcweir static URL createClipboardURL( OClipboardDispatcher::ClipboardFunc _eFunc ) 50cdf0e10cSrcweir { 51cdf0e10cSrcweir URL aURL; 52cdf0e10cSrcweir switch ( _eFunc ) 53cdf0e10cSrcweir { 54cdf0e10cSrcweir case OClipboardDispatcher::eCut: 55cdf0e10cSrcweir aURL.Complete = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:Cut" ) ); 56cdf0e10cSrcweir break; 57cdf0e10cSrcweir case OClipboardDispatcher::eCopy: 58cdf0e10cSrcweir aURL.Complete = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:Copy" ) ); 59cdf0e10cSrcweir break; 60cdf0e10cSrcweir case OClipboardDispatcher::ePaste: 61cdf0e10cSrcweir aURL.Complete = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:Paste" ) ); 62cdf0e10cSrcweir break; 63cdf0e10cSrcweir } 64cdf0e10cSrcweir return aURL; 65cdf0e10cSrcweir } 66cdf0e10cSrcweir } 67cdf0e10cSrcweir 68cdf0e10cSrcweir //==================================================================== 69cdf0e10cSrcweir //= OClipboardDispatcher 70cdf0e10cSrcweir //==================================================================== 71cdf0e10cSrcweir //-------------------------------------------------------------------- OClipboardDispatcher(EditView & _rView,ClipboardFunc _eFunc)72cdf0e10cSrcweir OClipboardDispatcher::OClipboardDispatcher( EditView& _rView, ClipboardFunc _eFunc ) 73cdf0e10cSrcweir :ORichTextFeatureDispatcher( _rView, createClipboardURL( _eFunc ) ) 74cdf0e10cSrcweir ,m_eFunc( _eFunc ) 75cdf0e10cSrcweir ,m_bLastKnownEnabled( sal_True ) 76cdf0e10cSrcweir { 77cdf0e10cSrcweir } 78cdf0e10cSrcweir 79cdf0e10cSrcweir //-------------------------------------------------------------------- implIsEnabled() const80cdf0e10cSrcweir sal_Bool OClipboardDispatcher::implIsEnabled( ) const 81cdf0e10cSrcweir { 82cdf0e10cSrcweir sal_Bool bEnabled = sal_False; 83cdf0e10cSrcweir switch ( m_eFunc ) 84cdf0e10cSrcweir { 85cdf0e10cSrcweir case eCut: 86cdf0e10cSrcweir bEnabled = !getEditView()->IsReadOnly() && getEditView()->HasSelection(); 87cdf0e10cSrcweir break; 88cdf0e10cSrcweir 89cdf0e10cSrcweir case eCopy: 90cdf0e10cSrcweir bEnabled = getEditView()->HasSelection(); 91cdf0e10cSrcweir break; 92cdf0e10cSrcweir 93cdf0e10cSrcweir case ePaste: 94cdf0e10cSrcweir bEnabled = !getEditView()->IsReadOnly(); 95cdf0e10cSrcweir break; 96cdf0e10cSrcweir } 97cdf0e10cSrcweir return bEnabled; 98cdf0e10cSrcweir } 99cdf0e10cSrcweir 100cdf0e10cSrcweir //-------------------------------------------------------------------- buildStatusEvent() const101cdf0e10cSrcweir FeatureStateEvent OClipboardDispatcher::buildStatusEvent() const 102cdf0e10cSrcweir { 103cdf0e10cSrcweir FeatureStateEvent aEvent( ORichTextFeatureDispatcher::buildStatusEvent() ); 104cdf0e10cSrcweir aEvent.IsEnabled = implIsEnabled(); 105cdf0e10cSrcweir return aEvent; 106cdf0e10cSrcweir } 107cdf0e10cSrcweir 108cdf0e10cSrcweir //-------------------------------------------------------------------- invalidateFeatureState_Broadcast()109cdf0e10cSrcweir void OClipboardDispatcher::invalidateFeatureState_Broadcast() 110cdf0e10cSrcweir { 111cdf0e10cSrcweir sal_Bool bEnabled = implIsEnabled(); 112cdf0e10cSrcweir if ( m_bLastKnownEnabled == bEnabled ) 113cdf0e10cSrcweir // nothing changed -> no notification 114cdf0e10cSrcweir return; 115cdf0e10cSrcweir m_bLastKnownEnabled = bEnabled; 116cdf0e10cSrcweir 117cdf0e10cSrcweir ORichTextFeatureDispatcher::invalidateFeatureState_Broadcast(); 118cdf0e10cSrcweir } 119cdf0e10cSrcweir 120cdf0e10cSrcweir //-------------------------------------------------------------------- dispatch(const URL &,const Sequence<PropertyValue> &)121cdf0e10cSrcweir void SAL_CALL OClipboardDispatcher::dispatch( const URL& /*_rURL*/, const Sequence< PropertyValue >& /*Arguments*/ ) throw (RuntimeException) 122cdf0e10cSrcweir { 123cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 124cdf0e10cSrcweir if ( !getEditView() ) 125cdf0e10cSrcweir throw DisposedException(); 126cdf0e10cSrcweir 127cdf0e10cSrcweir switch ( m_eFunc ) 128cdf0e10cSrcweir { 129cdf0e10cSrcweir case eCut: 130cdf0e10cSrcweir getEditView()->Cut(); 131cdf0e10cSrcweir break; 132cdf0e10cSrcweir 133cdf0e10cSrcweir case eCopy: 134cdf0e10cSrcweir getEditView()->Copy(); 135cdf0e10cSrcweir break; 136cdf0e10cSrcweir 137cdf0e10cSrcweir case ePaste: 138cdf0e10cSrcweir getEditView()->Paste(); 139cdf0e10cSrcweir break; 140cdf0e10cSrcweir } 141cdf0e10cSrcweir } 142cdf0e10cSrcweir 143cdf0e10cSrcweir //==================================================================== 144cdf0e10cSrcweir //= OPasteClipboardDispatcher 145cdf0e10cSrcweir //==================================================================== 146cdf0e10cSrcweir //-------------------------------------------------------------------- OPasteClipboardDispatcher(EditView & _rView)147cdf0e10cSrcweir OPasteClipboardDispatcher::OPasteClipboardDispatcher( EditView& _rView ) 148cdf0e10cSrcweir :OClipboardDispatcher( _rView, ePaste ) 149cdf0e10cSrcweir ,m_pClipListener( NULL ) 150cdf0e10cSrcweir ,m_bPastePossible( sal_False ) 151cdf0e10cSrcweir { 152cdf0e10cSrcweir m_pClipListener = new TransferableClipboardListener( LINK( this, OPasteClipboardDispatcher, OnClipboardChanged ) ); 153cdf0e10cSrcweir m_pClipListener->acquire(); 154cdf0e10cSrcweir m_pClipListener->AddRemoveListener( _rView.GetWindow(), sal_True ); 155cdf0e10cSrcweir 156cdf0e10cSrcweir // initial state 157cdf0e10cSrcweir TransferableDataHelper aDataHelper( TransferableDataHelper::CreateFromSystemClipboard( _rView.GetWindow() ) ); 158cdf0e10cSrcweir m_bPastePossible = ( aDataHelper.HasFormat( SOT_FORMAT_STRING ) || aDataHelper.HasFormat( SOT_FORMAT_RTF ) ); 159cdf0e10cSrcweir } 160cdf0e10cSrcweir 161cdf0e10cSrcweir //-------------------------------------------------------------------- ~OPasteClipboardDispatcher()162cdf0e10cSrcweir OPasteClipboardDispatcher::~OPasteClipboardDispatcher() 163cdf0e10cSrcweir { 164cdf0e10cSrcweir if ( !isDisposed() ) 165cdf0e10cSrcweir { 166cdf0e10cSrcweir acquire(); 167cdf0e10cSrcweir dispose(); 168cdf0e10cSrcweir } 169cdf0e10cSrcweir } 170cdf0e10cSrcweir 171cdf0e10cSrcweir //-------------------------------------------------------------------- IMPL_LINK(OPasteClipboardDispatcher,OnClipboardChanged,TransferableDataHelper *,_pDataHelper)172cdf0e10cSrcweir IMPL_LINK( OPasteClipboardDispatcher, OnClipboardChanged, TransferableDataHelper*, _pDataHelper ) 173cdf0e10cSrcweir { 174cdf0e10cSrcweir OSL_ENSURE( _pDataHelper, "OPasteClipboardDispatcher::OnClipboardChanged: ooops!" ); 175cdf0e10cSrcweir m_bPastePossible = _pDataHelper->HasFormat( SOT_FORMAT_STRING ) 176cdf0e10cSrcweir || _pDataHelper->HasFormat( SOT_FORMAT_RTF ); 177cdf0e10cSrcweir 178cdf0e10cSrcweir invalidate(); 179cdf0e10cSrcweir 180cdf0e10cSrcweir return 0L; 181cdf0e10cSrcweir } 182cdf0e10cSrcweir 183cdf0e10cSrcweir //-------------------------------------------------------------------- disposing(::osl::ClearableMutexGuard & _rClearBeforeNotify)184cdf0e10cSrcweir void OPasteClipboardDispatcher::disposing( ::osl::ClearableMutexGuard& _rClearBeforeNotify ) 185cdf0e10cSrcweir { 186cdf0e10cSrcweir OSL_ENSURE( getEditView() && getEditView()->GetWindow(), "OPasteClipboardDispatcher::disposing: EditView should not (yet) be disfunctional here!" ); 187cdf0e10cSrcweir if ( getEditView() && getEditView()->GetWindow() && m_pClipListener ) 188cdf0e10cSrcweir m_pClipListener->AddRemoveListener( getEditView()->GetWindow(), sal_False ); 189cdf0e10cSrcweir m_pClipListener->release(); 190cdf0e10cSrcweir m_pClipListener = NULL; 191cdf0e10cSrcweir 192cdf0e10cSrcweir OClipboardDispatcher::disposing( _rClearBeforeNotify ); 193cdf0e10cSrcweir } 194cdf0e10cSrcweir 195cdf0e10cSrcweir //-------------------------------------------------------------------- implIsEnabled() const196cdf0e10cSrcweir sal_Bool OPasteClipboardDispatcher::implIsEnabled( ) const 197cdf0e10cSrcweir { 198cdf0e10cSrcweir return m_bPastePossible && OClipboardDispatcher::implIsEnabled(); 199cdf0e10cSrcweir } 200cdf0e10cSrcweir 201cdf0e10cSrcweir //........................................................................ 202cdf0e10cSrcweir } // namespace frm 203cdf0e10cSrcweir //........................................................................ 204