bump product version to 4.1.6.2
[LibreOffice.git] / forms / source / richtext / clipboarddispatcher.cxx
blob7f299b855ca1118490090348d2b9c9690de5c65e
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>
27 //........................................................................
28 namespace frm
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 //====================================================================
39 namespace
41 static URL createClipboardURL( OClipboardDispatcher::ClipboardFunc _eFunc )
43 URL aURL;
44 switch ( _eFunc )
46 case OClipboardDispatcher::eCut:
47 aURL.Complete = OUString( ".uno:Cut" );
48 break;
49 case OClipboardDispatcher::eCopy:
50 aURL.Complete = OUString( ".uno:Copy" );
51 break;
52 case OClipboardDispatcher::ePaste:
53 aURL.Complete = OUString( ".uno:Paste" );
54 break;
56 return aURL;
60 //====================================================================
61 //= OClipboardDispatcher
62 //====================================================================
63 //--------------------------------------------------------------------
64 OClipboardDispatcher::OClipboardDispatcher( EditView& _rView, ClipboardFunc _eFunc )
65 :ORichTextFeatureDispatcher( _rView, createClipboardURL( _eFunc ) )
66 ,m_eFunc( _eFunc )
67 ,m_bLastKnownEnabled( sal_True )
71 //--------------------------------------------------------------------
72 sal_Bool OClipboardDispatcher::implIsEnabled( ) const
74 sal_Bool bEnabled = sal_False;
75 switch ( m_eFunc )
77 case eCut:
78 bEnabled = !getEditView()->IsReadOnly() && getEditView()->HasSelection();
79 break;
81 case eCopy:
82 bEnabled = getEditView()->HasSelection();
83 break;
85 case ePaste:
86 bEnabled = !getEditView()->IsReadOnly();
87 break;
89 return bEnabled;
92 //--------------------------------------------------------------------
93 FeatureStateEvent OClipboardDispatcher::buildStatusEvent() const
95 FeatureStateEvent aEvent( ORichTextFeatureDispatcher::buildStatusEvent() );
96 aEvent.IsEnabled = implIsEnabled();
97 return aEvent;
100 //--------------------------------------------------------------------
101 void OClipboardDispatcher::invalidateFeatureState_Broadcast()
103 sal_Bool bEnabled = implIsEnabled();
104 if ( m_bLastKnownEnabled == bEnabled )
105 // nothing changed -> no notification
106 return;
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();
119 switch ( m_eFunc )
121 case eCut:
122 getEditView()->Cut();
123 break;
125 case eCopy:
126 getEditView()->Copy();
127 break;
129 case ePaste:
130 getEditView()->Paste();
131 break;
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 );
148 // initial state
149 TransferableDataHelper aDataHelper( TransferableDataHelper::CreateFromSystemClipboard( _rView.GetWindow() ) );
150 m_bPastePossible = ( aDataHelper.HasFormat( SOT_FORMAT_STRING ) || aDataHelper.HasFormat( SOT_FORMAT_RTF ) );
153 //--------------------------------------------------------------------
154 OPasteClipboardDispatcher::~OPasteClipboardDispatcher()
156 if ( !isDisposed() )
158 acquire();
159 dispose();
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 );
170 invalidate();
172 return 0L;
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 //........................................................................
194 } // namespace frm
195 //........................................................................
197 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */