Update ooo320-m1
[ooovba.git] / forms / source / richtext / clipboarddispatcher.cxx
blobf3dc922108002a7d9bff709b58682063351b3c5e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: clipboarddispatcher.cxx,v $
10 * $Revision: 1.6 $
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 //........................................................................
43 namespace frm
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 //====================================================================
54 namespace
56 static URL createClipboardURL( OClipboardDispatcher::ClipboardFunc _eFunc )
58 URL aURL;
59 switch ( _eFunc )
61 case OClipboardDispatcher::eCut:
62 aURL.Complete = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:Cut" ) );
63 break;
64 case OClipboardDispatcher::eCopy:
65 aURL.Complete = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:Copy" ) );
66 break;
67 case OClipboardDispatcher::ePaste:
68 aURL.Complete = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:Paste" ) );
69 break;
71 return aURL;
75 //====================================================================
76 //= OClipboardDispatcher
77 //====================================================================
78 //--------------------------------------------------------------------
79 OClipboardDispatcher::OClipboardDispatcher( EditView& _rView, ClipboardFunc _eFunc )
80 :ORichTextFeatureDispatcher( _rView, createClipboardURL( _eFunc ) )
81 ,m_eFunc( _eFunc )
82 ,m_bLastKnownEnabled( sal_True )
86 //--------------------------------------------------------------------
87 sal_Bool OClipboardDispatcher::implIsEnabled( ) const
89 sal_Bool bEnabled = sal_False;
90 switch ( m_eFunc )
92 case eCut:
93 bEnabled = !getEditView()->IsReadOnly() && getEditView()->HasSelection();
94 break;
96 case eCopy:
97 bEnabled = getEditView()->HasSelection();
98 break;
100 case ePaste:
101 bEnabled = !getEditView()->IsReadOnly();
102 break;
104 return bEnabled;
107 //--------------------------------------------------------------------
108 FeatureStateEvent OClipboardDispatcher::buildStatusEvent() const
110 FeatureStateEvent aEvent( ORichTextFeatureDispatcher::buildStatusEvent() );
111 aEvent.IsEnabled = implIsEnabled();
112 return aEvent;
115 //--------------------------------------------------------------------
116 void OClipboardDispatcher::invalidateFeatureState_Broadcast()
118 sal_Bool bEnabled = implIsEnabled();
119 if ( m_bLastKnownEnabled == bEnabled )
120 // nothing changed -> no notification
121 return;
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();
134 switch ( m_eFunc )
136 case eCut:
137 getEditView()->Cut();
138 break;
140 case eCopy:
141 getEditView()->Copy();
142 break;
144 case ePaste:
145 getEditView()->Paste();
146 break;
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 );
163 // initial state
164 TransferableDataHelper aDataHelper( TransferableDataHelper::CreateFromSystemClipboard( _rView.GetWindow() ) );
165 m_bPastePossible = ( aDataHelper.HasFormat( SOT_FORMAT_STRING ) || aDataHelper.HasFormat( SOT_FORMAT_RTF ) );
168 //--------------------------------------------------------------------
169 OPasteClipboardDispatcher::~OPasteClipboardDispatcher()
171 if ( !isDisposed() )
173 acquire();
174 dispose();
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 );
185 invalidate();
187 return 0L;
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 //........................................................................
209 } // namespace frm
210 //........................................................................