Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / forms / source / richtext / clipboarddispatcher.cxx
blob00d5adeeeb9bd62d6da72bb18f52673b85f8a145
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
28 namespace frm
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;
39 namespace
41 URL createClipboardURL( OClipboardDispatcher::ClipboardFunc _eFunc )
43 URL aURL;
44 switch ( _eFunc )
46 case OClipboardDispatcher::eCut:
47 aURL.Complete = ".uno:Cut";
48 break;
49 case OClipboardDispatcher::eCopy:
50 aURL.Complete = ".uno:Copy";
51 break;
52 case OClipboardDispatcher::ePaste:
53 aURL.Complete = ".uno:Paste";
54 break;
56 return aURL;
60 OClipboardDispatcher::OClipboardDispatcher( EditView& _rView, ClipboardFunc _eFunc )
61 :ORichTextFeatureDispatcher( _rView, createClipboardURL( _eFunc ) )
62 ,m_eFunc( _eFunc )
63 ,m_bLastKnownEnabled( true )
68 bool OClipboardDispatcher::implIsEnabled( ) const
70 bool bEnabled = false;
71 switch ( m_eFunc )
73 case eCut:
74 bEnabled = !getEditView()->IsReadOnly() && getEditView()->HasSelection();
75 break;
77 case eCopy:
78 bEnabled = getEditView()->HasSelection();
79 break;
81 case ePaste:
82 bEnabled = !getEditView()->IsReadOnly();
83 break;
85 return bEnabled;
89 FeatureStateEvent OClipboardDispatcher::buildStatusEvent() const
91 FeatureStateEvent aEvent( ORichTextFeatureDispatcher::buildStatusEvent() );
92 aEvent.IsEnabled = implIsEnabled();
93 return aEvent;
97 void OClipboardDispatcher::invalidateFeatureState_Broadcast()
99 bool bEnabled = implIsEnabled();
100 if ( m_bLastKnownEnabled == bEnabled )
101 // nothing changed -> no notification
102 return;
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();
115 switch ( m_eFunc )
117 case eCut:
118 getEditView()->Cut();
119 break;
121 case eCopy:
122 getEditView()->Copy();
123 break;
125 case ePaste:
126 getEditView()->Paste();
127 break;
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 );
140 // initial state
141 TransferableDataHelper aDataHelper( TransferableDataHelper::CreateFromSystemClipboard( _rView.GetWindow() ) );
142 m_bPastePossible = ( aDataHelper.HasFormat( SotClipboardFormatId::STRING ) || aDataHelper.HasFormat( SotClipboardFormatId::RTF ) );
146 OPasteClipboardDispatcher::~OPasteClipboardDispatcher()
148 if ( !isDisposed() )
150 acquire();
151 dispose();
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 );
162 invalidate();
166 void OPasteClipboardDispatcher::disposing( ::osl::ClearableMutexGuard& _rClearBeforeNotify )
168 OSL_ENSURE( getEditView() && getEditView()->GetWindow(), "OPasteClipboardDispatcher::disposing: EditView should not (yet) be disfunctional here!" );
169 if (m_pClipListener)
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();
188 } // namespace frm
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */