nss: upgrade to release 3.73
[LibreOffice.git] / forms / source / richtext / clipboarddispatcher.cxx
blob70313edb33c7079b750e0472d50e083dd702f449
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 <vcl/transfer.hxx>
26 #include <osl/diagnose.h>
29 namespace frm
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;
40 namespace
42 URL createClipboardURL( OClipboardDispatcher::ClipboardFunc _eFunc )
44 URL aURL;
45 switch ( _eFunc )
47 case OClipboardDispatcher::eCut:
48 aURL.Complete = ".uno:Cut";
49 break;
50 case OClipboardDispatcher::eCopy:
51 aURL.Complete = ".uno:Copy";
52 break;
53 case OClipboardDispatcher::ePaste:
54 aURL.Complete = ".uno:Paste";
55 break;
57 return aURL;
61 OClipboardDispatcher::OClipboardDispatcher( EditView& _rView, ClipboardFunc _eFunc )
62 :ORichTextFeatureDispatcher( _rView, createClipboardURL( _eFunc ) )
63 ,m_eFunc( _eFunc )
64 ,m_bLastKnownEnabled( true )
69 bool OClipboardDispatcher::implIsEnabled( ) const
71 bool bEnabled = false;
72 switch ( m_eFunc )
74 case eCut:
75 bEnabled = !getEditView()->IsReadOnly() && getEditView()->HasSelection();
76 break;
78 case eCopy:
79 bEnabled = getEditView()->HasSelection();
80 break;
82 case ePaste:
83 bEnabled = !getEditView()->IsReadOnly();
84 break;
86 return bEnabled;
90 FeatureStateEvent OClipboardDispatcher::buildStatusEvent() const
92 FeatureStateEvent aEvent( ORichTextFeatureDispatcher::buildStatusEvent() );
93 aEvent.IsEnabled = implIsEnabled();
94 return aEvent;
98 void OClipboardDispatcher::invalidateFeatureState_Broadcast()
100 bool bEnabled = implIsEnabled();
101 if ( m_bLastKnownEnabled == bEnabled )
102 // nothing changed -> no notification
103 return;
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();
116 switch ( m_eFunc )
118 case eCut:
119 getEditView()->Cut();
120 break;
122 case eCopy:
123 getEditView()->Copy();
124 break;
126 case ePaste:
127 getEditView()->Paste();
128 break;
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() );
139 // initial state
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()
148 if ( !isDisposed() )
150 acquire();
151 dispose();
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 );
163 invalidate();
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();
188 } // namespace frm
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */