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>
27 //........................................................................
30 //........................................................................
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
;
38 //====================================================================
41 static URL
createClipboardURL( OClipboardDispatcher::ClipboardFunc _eFunc
)
46 case OClipboardDispatcher::eCut
:
47 aURL
.Complete
= OUString( ".uno:Cut" );
49 case OClipboardDispatcher::eCopy
:
50 aURL
.Complete
= OUString( ".uno:Copy" );
52 case OClipboardDispatcher::ePaste
:
53 aURL
.Complete
= OUString( ".uno:Paste" );
60 //====================================================================
61 //= OClipboardDispatcher
62 //====================================================================
63 //--------------------------------------------------------------------
64 OClipboardDispatcher::OClipboardDispatcher( EditView
& _rView
, ClipboardFunc _eFunc
)
65 :ORichTextFeatureDispatcher( _rView
, createClipboardURL( _eFunc
) )
67 ,m_bLastKnownEnabled( sal_True
)
71 //--------------------------------------------------------------------
72 sal_Bool
OClipboardDispatcher::implIsEnabled( ) const
74 sal_Bool bEnabled
= sal_False
;
78 bEnabled
= !getEditView()->IsReadOnly() && getEditView()->HasSelection();
82 bEnabled
= getEditView()->HasSelection();
86 bEnabled
= !getEditView()->IsReadOnly();
92 //--------------------------------------------------------------------
93 FeatureStateEvent
OClipboardDispatcher::buildStatusEvent() const
95 FeatureStateEvent
aEvent( ORichTextFeatureDispatcher::buildStatusEvent() );
96 aEvent
.IsEnabled
= implIsEnabled();
100 //--------------------------------------------------------------------
101 void OClipboardDispatcher::invalidateFeatureState_Broadcast()
103 sal_Bool bEnabled
= implIsEnabled();
104 if ( m_bLastKnownEnabled
== bEnabled
)
105 // nothing changed -> no notification
107 m_bLastKnownEnabled
= bEnabled
;
109 ORichTextFeatureDispatcher::invalidateFeatureState_Broadcast();
112 //--------------------------------------------------------------------
113 void SAL_CALL
OClipboardDispatcher::dispatch( const URL
& /*_rURL*/, const Sequence
< PropertyValue
>& /*Arguments*/ ) throw (RuntimeException
)
115 ::osl::MutexGuard
aGuard( m_aMutex
);
116 if ( !getEditView() )
117 throw DisposedException();
122 getEditView()->Cut();
126 getEditView()->Copy();
130 getEditView()->Paste();
135 //====================================================================
136 //= OPasteClipboardDispatcher
137 //====================================================================
138 //--------------------------------------------------------------------
139 OPasteClipboardDispatcher::OPasteClipboardDispatcher( EditView
& _rView
)
140 :OClipboardDispatcher( _rView
, ePaste
)
141 ,m_pClipListener( NULL
)
142 ,m_bPastePossible( sal_False
)
144 m_pClipListener
= new TransferableClipboardListener( LINK( this, OPasteClipboardDispatcher
, OnClipboardChanged
) );
145 m_pClipListener
->acquire();
146 m_pClipListener
->AddRemoveListener( _rView
.GetWindow(), sal_True
);
149 TransferableDataHelper
aDataHelper( TransferableDataHelper::CreateFromSystemClipboard( _rView
.GetWindow() ) );
150 m_bPastePossible
= ( aDataHelper
.HasFormat( SOT_FORMAT_STRING
) || aDataHelper
.HasFormat( SOT_FORMAT_RTF
) );
153 //--------------------------------------------------------------------
154 OPasteClipboardDispatcher::~OPasteClipboardDispatcher()
163 //--------------------------------------------------------------------
164 IMPL_LINK( OPasteClipboardDispatcher
, OnClipboardChanged
, TransferableDataHelper
*, _pDataHelper
)
166 OSL_ENSURE( _pDataHelper
, "OPasteClipboardDispatcher::OnClipboardChanged: ooops!" );
167 m_bPastePossible
= _pDataHelper
->HasFormat( SOT_FORMAT_STRING
)
168 || _pDataHelper
->HasFormat( SOT_FORMAT_RTF
);
175 //--------------------------------------------------------------------
176 void OPasteClipboardDispatcher::disposing( ::osl::ClearableMutexGuard
& _rClearBeforeNotify
)
178 OSL_ENSURE( getEditView() && getEditView()->GetWindow(), "OPasteClipboardDispatcher::disposing: EditView should not (yet) be disfunctional here!" );
179 if ( getEditView() && getEditView()->GetWindow() && m_pClipListener
)
180 m_pClipListener
->AddRemoveListener( getEditView()->GetWindow(), sal_False
);
181 m_pClipListener
->release();
182 m_pClipListener
= NULL
;
184 OClipboardDispatcher::disposing( _rClearBeforeNotify
);
187 //--------------------------------------------------------------------
188 sal_Bool
OPasteClipboardDispatcher::implIsEnabled( ) const
190 return m_bPastePossible
&& OClipboardDispatcher::implIsEnabled();
193 //........................................................................
195 //........................................................................
197 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */