1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: clipboarddispatcher.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_forms.hxx"
33 #include "clipboarddispatcher.hxx"
34 #include <svx/editview.hxx>
36 /** === begin UNO includes === **/
37 #include <com/sun/star/lang/DisposedException.hpp>
38 /** === end UNO includes === **/
39 #include <svtools/cliplistener.hxx>
40 #include <svtools/transfer.hxx>
42 //........................................................................
45 //........................................................................
47 using namespace ::com::sun::star::uno
;
48 using namespace ::com::sun::star::frame
;
49 using namespace ::com::sun::star::lang
;
50 using namespace ::com::sun::star::util
;
51 using namespace ::com::sun::star::beans
;
53 //====================================================================
56 static URL
createClipboardURL( OClipboardDispatcher::ClipboardFunc _eFunc
)
61 case OClipboardDispatcher::eCut
:
62 aURL
.Complete
= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:Cut" ) );
64 case OClipboardDispatcher::eCopy
:
65 aURL
.Complete
= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:Copy" ) );
67 case OClipboardDispatcher::ePaste
:
68 aURL
.Complete
= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:Paste" ) );
75 //====================================================================
76 //= OClipboardDispatcher
77 //====================================================================
78 //--------------------------------------------------------------------
79 OClipboardDispatcher::OClipboardDispatcher( EditView
& _rView
, ClipboardFunc _eFunc
)
80 :ORichTextFeatureDispatcher( _rView
, createClipboardURL( _eFunc
) )
82 ,m_bLastKnownEnabled( sal_True
)
86 //--------------------------------------------------------------------
87 sal_Bool
OClipboardDispatcher::implIsEnabled( ) const
89 sal_Bool bEnabled
= sal_False
;
93 bEnabled
= !getEditView()->IsReadOnly() && getEditView()->HasSelection();
97 bEnabled
= getEditView()->HasSelection();
101 bEnabled
= !getEditView()->IsReadOnly();
107 //--------------------------------------------------------------------
108 FeatureStateEvent
OClipboardDispatcher::buildStatusEvent() const
110 FeatureStateEvent
aEvent( ORichTextFeatureDispatcher::buildStatusEvent() );
111 aEvent
.IsEnabled
= implIsEnabled();
115 //--------------------------------------------------------------------
116 void OClipboardDispatcher::invalidateFeatureState_Broadcast()
118 sal_Bool bEnabled
= implIsEnabled();
119 if ( m_bLastKnownEnabled
== bEnabled
)
120 // nothing changed -> no notification
122 m_bLastKnownEnabled
= bEnabled
;
124 ORichTextFeatureDispatcher::invalidateFeatureState_Broadcast();
127 //--------------------------------------------------------------------
128 void SAL_CALL
OClipboardDispatcher::dispatch( const URL
& /*_rURL*/, const Sequence
< PropertyValue
>& /*Arguments*/ ) throw (RuntimeException
)
130 ::osl::MutexGuard
aGuard( m_aMutex
);
131 if ( !getEditView() )
132 throw DisposedException();
137 getEditView()->Cut();
141 getEditView()->Copy();
145 getEditView()->Paste();
150 //====================================================================
151 //= OPasteClipboardDispatcher
152 //====================================================================
153 //--------------------------------------------------------------------
154 OPasteClipboardDispatcher::OPasteClipboardDispatcher( EditView
& _rView
)
155 :OClipboardDispatcher( _rView
, ePaste
)
156 ,m_pClipListener( NULL
)
157 ,m_bPastePossible( sal_False
)
159 m_pClipListener
= new TransferableClipboardListener( LINK( this, OPasteClipboardDispatcher
, OnClipboardChanged
) );
160 m_pClipListener
->acquire();
161 m_pClipListener
->AddRemoveListener( _rView
.GetWindow(), TRUE
);
164 TransferableDataHelper
aDataHelper( TransferableDataHelper::CreateFromSystemClipboard( _rView
.GetWindow() ) );
165 m_bPastePossible
= ( aDataHelper
.HasFormat( SOT_FORMAT_STRING
) || aDataHelper
.HasFormat( SOT_FORMAT_RTF
) );
168 //--------------------------------------------------------------------
169 OPasteClipboardDispatcher::~OPasteClipboardDispatcher()
178 //--------------------------------------------------------------------
179 IMPL_LINK( OPasteClipboardDispatcher
, OnClipboardChanged
, TransferableDataHelper
*, _pDataHelper
)
181 OSL_ENSURE( _pDataHelper
, "OPasteClipboardDispatcher::OnClipboardChanged: ooops!" );
182 m_bPastePossible
= _pDataHelper
->HasFormat( SOT_FORMAT_STRING
)
183 || _pDataHelper
->HasFormat( SOT_FORMAT_RTF
);
190 //--------------------------------------------------------------------
191 void OPasteClipboardDispatcher::disposing( ::osl::ClearableMutexGuard
& _rClearBeforeNotify
)
193 OSL_ENSURE( getEditView() && getEditView()->GetWindow(), "OPasteClipboardDispatcher::disposing: EditView should not (yet) be disfunctional here!" );
194 if ( getEditView() && getEditView()->GetWindow() && m_pClipListener
)
195 m_pClipListener
->AddRemoveListener( getEditView()->GetWindow(), FALSE
);
196 m_pClipListener
->release();
197 m_pClipListener
= NULL
;
199 OClipboardDispatcher::disposing( _rClearBeforeNotify
);
202 //--------------------------------------------------------------------
203 sal_Bool
OPasteClipboardDispatcher::implIsEnabled( ) const
205 return m_bPastePossible
&& OClipboardDispatcher::implIsEnabled();
208 //........................................................................
210 //........................................................................