1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "clipboarddispatcher.hxx"
21 #include <editeng/editview.hxx>
23 #include <com/sun/star/lang/DisposedException.hpp>
24 #include <svtools/cliplistener.hxx>
25 #include <svtools/transfer.hxx>
32 using namespace ::com::sun::star::uno
;
33 using namespace ::com::sun::star::frame
;
34 using namespace ::com::sun::star::lang
;
35 using namespace ::com::sun::star::util
;
36 using namespace ::com::sun::star::beans
;
41 URL
createClipboardURL( OClipboardDispatcher::ClipboardFunc _eFunc
)
46 case OClipboardDispatcher::eCut
:
47 aURL
.Complete
= ".uno:Cut";
49 case OClipboardDispatcher::eCopy
:
50 aURL
.Complete
= ".uno:Copy";
52 case OClipboardDispatcher::ePaste
:
53 aURL
.Complete
= ".uno:Paste";
60 OClipboardDispatcher::OClipboardDispatcher( EditView
& _rView
, ClipboardFunc _eFunc
)
61 :ORichTextFeatureDispatcher( _rView
, createClipboardURL( _eFunc
) )
63 ,m_bLastKnownEnabled( true )
68 bool OClipboardDispatcher::implIsEnabled( ) const
70 bool bEnabled
= false;
74 bEnabled
= !getEditView()->IsReadOnly() && getEditView()->HasSelection();
78 bEnabled
= getEditView()->HasSelection();
82 bEnabled
= !getEditView()->IsReadOnly();
89 FeatureStateEvent
OClipboardDispatcher::buildStatusEvent() const
91 FeatureStateEvent
aEvent( ORichTextFeatureDispatcher::buildStatusEvent() );
92 aEvent
.IsEnabled
= implIsEnabled();
97 void OClipboardDispatcher::invalidateFeatureState_Broadcast()
99 bool bEnabled
= implIsEnabled();
100 if ( m_bLastKnownEnabled
== bEnabled
)
101 // nothing changed -> no notification
103 m_bLastKnownEnabled
= bEnabled
;
105 ORichTextFeatureDispatcher::invalidateFeatureState_Broadcast();
109 void SAL_CALL
OClipboardDispatcher::dispatch( const URL
& /*_rURL*/, const Sequence
< PropertyValue
>& /*Arguments*/ ) throw (RuntimeException
, std::exception
)
111 ::osl::MutexGuard
aGuard( m_aMutex
);
112 if ( !getEditView() )
113 throw DisposedException();
118 getEditView()->Cut();
122 getEditView()->Copy();
126 getEditView()->Paste();
131 OPasteClipboardDispatcher::OPasteClipboardDispatcher( EditView
& _rView
)
132 :OClipboardDispatcher( _rView
, ePaste
)
133 ,m_pClipListener( nullptr )
134 ,m_bPastePossible( false )
136 m_pClipListener
= new TransferableClipboardListener( LINK( this, OPasteClipboardDispatcher
, OnClipboardChanged
) );
137 m_pClipListener
->acquire();
138 m_pClipListener
->AddRemoveListener( _rView
.GetWindow(), true );
141 TransferableDataHelper
aDataHelper( TransferableDataHelper::CreateFromSystemClipboard( _rView
.GetWindow() ) );
142 m_bPastePossible
= ( aDataHelper
.HasFormat( SotClipboardFormatId::STRING
) || aDataHelper
.HasFormat( SotClipboardFormatId::RTF
) );
146 OPasteClipboardDispatcher::~OPasteClipboardDispatcher()
156 IMPL_LINK_TYPED( OPasteClipboardDispatcher
, OnClipboardChanged
, TransferableDataHelper
*, _pDataHelper
, void )
158 OSL_ENSURE( _pDataHelper
, "OPasteClipboardDispatcher::OnClipboardChanged: ooops!" );
159 m_bPastePossible
= _pDataHelper
->HasFormat( SotClipboardFormatId::STRING
)
160 || _pDataHelper
->HasFormat( SotClipboardFormatId::RTF
);
166 void OPasteClipboardDispatcher::disposing( ::osl::ClearableMutexGuard
& _rClearBeforeNotify
)
168 OSL_ENSURE( getEditView() && getEditView()->GetWindow(), "OPasteClipboardDispatcher::disposing: EditView should not (yet) be disfunctional here!" );
171 if (getEditView() && getEditView()->GetWindow())
172 m_pClipListener
->AddRemoveListener( getEditView()->GetWindow(), false );
174 m_pClipListener
->release();
175 m_pClipListener
= nullptr;
178 OClipboardDispatcher::disposing( _rClearBeforeNotify
);
182 bool OPasteClipboardDispatcher::implIsEnabled( ) const
184 return m_bPastePossible
&& OClipboardDispatcher::implIsEnabled();
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */