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 <vcl/transfer.hxx>
26 #include <osl/diagnose.h>
33 using namespace ::com::sun::star::uno
;
34 using namespace ::com::sun::star::frame
;
35 using namespace ::com::sun::star::lang
;
36 using namespace ::com::sun::star::util
;
37 using namespace ::com::sun::star::beans
;
42 URL
createClipboardURL( OClipboardDispatcher::ClipboardFunc _eFunc
)
47 case OClipboardDispatcher::eCut
:
48 aURL
.Complete
= ".uno:Cut";
50 case OClipboardDispatcher::eCopy
:
51 aURL
.Complete
= ".uno:Copy";
53 case OClipboardDispatcher::ePaste
:
54 aURL
.Complete
= ".uno:Paste";
61 OClipboardDispatcher::OClipboardDispatcher( EditView
& _rView
, ClipboardFunc _eFunc
)
62 :ORichTextFeatureDispatcher( _rView
, createClipboardURL( _eFunc
) )
64 ,m_bLastKnownEnabled( true )
69 bool OClipboardDispatcher::implIsEnabled( ) const
71 bool bEnabled
= false;
75 bEnabled
= !getEditView()->IsReadOnly() && getEditView()->HasSelection();
79 bEnabled
= getEditView()->HasSelection();
83 bEnabled
= !getEditView()->IsReadOnly();
90 FeatureStateEvent
OClipboardDispatcher::buildStatusEvent() const
92 FeatureStateEvent
aEvent( ORichTextFeatureDispatcher::buildStatusEvent() );
93 aEvent
.IsEnabled
= implIsEnabled();
98 void OClipboardDispatcher::invalidateFeatureState_Broadcast()
100 bool bEnabled
= implIsEnabled();
101 if ( m_bLastKnownEnabled
== bEnabled
)
102 // nothing changed -> no notification
104 m_bLastKnownEnabled
= bEnabled
;
106 ORichTextFeatureDispatcher::invalidateFeatureState_Broadcast();
110 void SAL_CALL
OClipboardDispatcher::dispatch( const URL
& /*_rURL*/, const Sequence
< PropertyValue
>& /*Arguments*/ )
112 ::osl::MutexGuard
aGuard( m_aMutex
);
113 if ( !getEditView() )
114 throw DisposedException();
119 getEditView()->Cut();
123 getEditView()->Copy();
127 getEditView()->Paste();
132 OPasteClipboardDispatcher::OPasteClipboardDispatcher( EditView
& _rView
)
133 :OClipboardDispatcher( _rView
, ePaste
)
134 ,m_bPastePossible( false )
136 m_pClipListener
= new TransferableClipboardListener( LINK( this, OPasteClipboardDispatcher
, OnClipboardChanged
) );
137 m_pClipListener
->AddListener( _rView
.GetWindow() );
140 TransferableDataHelper
aDataHelper( TransferableDataHelper::CreateFromSystemClipboard( _rView
.GetWindow() ) );
141 m_bPastePossible
= ( aDataHelper
.HasFormat( SotClipboardFormatId::STRING
) ||
142 aDataHelper
.HasFormat( SotClipboardFormatId::RTF
) || aDataHelper
.HasFormat( SotClipboardFormatId::RICHTEXT
) );
146 OPasteClipboardDispatcher::~OPasteClipboardDispatcher()
156 IMPL_LINK( OPasteClipboardDispatcher
, OnClipboardChanged
, TransferableDataHelper
*, _pDataHelper
, void )
158 OSL_ENSURE( _pDataHelper
, "OPasteClipboardDispatcher::OnClipboardChanged: ooops!" );
159 m_bPastePossible
= _pDataHelper
->HasFormat( SotClipboardFormatId::STRING
)
160 || _pDataHelper
->HasFormat( SotClipboardFormatId::RTF
)
161 || _pDataHelper
->HasFormat( SotClipboardFormatId::RICHTEXT
);
167 void OPasteClipboardDispatcher::disposing( ::osl::ClearableMutexGuard
& _rClearBeforeNotify
)
169 OSL_ENSURE( getEditView() && getEditView()->GetWindow(), "OPasteClipboardDispatcher::disposing: EditView should not (yet) be disfunctional here!" );
170 if (m_pClipListener
.is())
172 if (getEditView() && getEditView()->GetWindow())
173 m_pClipListener
->RemoveListener( getEditView()->GetWindow() );
175 m_pClipListener
.clear();
178 OClipboardDispatcher::disposing( _rClearBeforeNotify
);
182 bool OPasteClipboardDispatcher::implIsEnabled( ) const
184 return m_bPastePossible
&& OClipboardDispatcher::implIsEnabled();
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */