Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "clipboarddispatcher.hxx"
21 : #include <editeng/editview.hxx>
22 :
23 : #include <com/sun/star/lang/DisposedException.hpp>
24 : #include <svtools/cliplistener.hxx>
25 : #include <svtools/transfer.hxx>
26 :
27 :
28 : namespace frm
29 : {
30 :
31 :
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;
37 :
38 :
39 : namespace
40 : {
41 0 : static URL createClipboardURL( OClipboardDispatcher::ClipboardFunc _eFunc )
42 : {
43 0 : URL aURL;
44 0 : switch ( _eFunc )
45 : {
46 : case OClipboardDispatcher::eCut:
47 0 : aURL.Complete = ".uno:Cut";
48 0 : break;
49 : case OClipboardDispatcher::eCopy:
50 0 : aURL.Complete = ".uno:Copy";
51 0 : break;
52 : case OClipboardDispatcher::ePaste:
53 0 : aURL.Complete = ".uno:Paste";
54 0 : break;
55 : }
56 0 : return aURL;
57 : }
58 : }
59 :
60 0 : OClipboardDispatcher::OClipboardDispatcher( EditView& _rView, ClipboardFunc _eFunc )
61 : :ORichTextFeatureDispatcher( _rView, createClipboardURL( _eFunc ) )
62 : ,m_eFunc( _eFunc )
63 0 : ,m_bLastKnownEnabled( true )
64 : {
65 0 : }
66 :
67 :
68 0 : bool OClipboardDispatcher::implIsEnabled( ) const
69 : {
70 0 : bool bEnabled = false;
71 0 : switch ( m_eFunc )
72 : {
73 : case eCut:
74 0 : bEnabled = !getEditView()->IsReadOnly() && getEditView()->HasSelection();
75 0 : break;
76 :
77 : case eCopy:
78 0 : bEnabled = getEditView()->HasSelection();
79 0 : break;
80 :
81 : case ePaste:
82 0 : bEnabled = !getEditView()->IsReadOnly();
83 0 : break;
84 : }
85 0 : return bEnabled;
86 : }
87 :
88 :
89 0 : FeatureStateEvent OClipboardDispatcher::buildStatusEvent() const
90 : {
91 0 : FeatureStateEvent aEvent( ORichTextFeatureDispatcher::buildStatusEvent() );
92 0 : aEvent.IsEnabled = implIsEnabled();
93 0 : return aEvent;
94 : }
95 :
96 :
97 0 : void OClipboardDispatcher::invalidateFeatureState_Broadcast()
98 : {
99 0 : bool bEnabled = implIsEnabled();
100 0 : if ( m_bLastKnownEnabled == bEnabled )
101 : // nothing changed -> no notification
102 0 : return;
103 0 : m_bLastKnownEnabled = bEnabled;
104 :
105 0 : ORichTextFeatureDispatcher::invalidateFeatureState_Broadcast();
106 : }
107 :
108 :
109 0 : void SAL_CALL OClipboardDispatcher::dispatch( const URL& /*_rURL*/, const Sequence< PropertyValue >& /*Arguments*/ ) throw (RuntimeException, std::exception)
110 : {
111 0 : ::osl::MutexGuard aGuard( m_aMutex );
112 0 : if ( !getEditView() )
113 0 : throw DisposedException();
114 :
115 0 : switch ( m_eFunc )
116 : {
117 : case eCut:
118 0 : getEditView()->Cut();
119 0 : break;
120 :
121 : case eCopy:
122 0 : getEditView()->Copy();
123 0 : break;
124 :
125 : case ePaste:
126 0 : getEditView()->Paste();
127 0 : break;
128 0 : }
129 0 : }
130 :
131 0 : OPasteClipboardDispatcher::OPasteClipboardDispatcher( EditView& _rView )
132 : :OClipboardDispatcher( _rView, ePaste )
133 : ,m_pClipListener( NULL )
134 0 : ,m_bPastePossible( false )
135 : {
136 0 : m_pClipListener = new TransferableClipboardListener( LINK( this, OPasteClipboardDispatcher, OnClipboardChanged ) );
137 0 : m_pClipListener->acquire();
138 0 : m_pClipListener->AddRemoveListener( _rView.GetWindow(), true );
139 :
140 : // initial state
141 0 : TransferableDataHelper aDataHelper( TransferableDataHelper::CreateFromSystemClipboard( _rView.GetWindow() ) );
142 0 : m_bPastePossible = ( aDataHelper.HasFormat( SotClipboardFormatId::STRING ) || aDataHelper.HasFormat( SotClipboardFormatId::RTF ) );
143 0 : }
144 :
145 :
146 0 : OPasteClipboardDispatcher::~OPasteClipboardDispatcher()
147 : {
148 0 : if ( !isDisposed() )
149 : {
150 0 : acquire();
151 0 : dispose();
152 : }
153 0 : }
154 :
155 :
156 0 : IMPL_LINK( OPasteClipboardDispatcher, OnClipboardChanged, TransferableDataHelper*, _pDataHelper )
157 : {
158 : OSL_ENSURE( _pDataHelper, "OPasteClipboardDispatcher::OnClipboardChanged: ooops!" );
159 0 : m_bPastePossible = _pDataHelper->HasFormat( SotClipboardFormatId::STRING )
160 0 : || _pDataHelper->HasFormat( SotClipboardFormatId::RTF );
161 :
162 0 : invalidate();
163 :
164 0 : return 0L;
165 : }
166 :
167 :
168 0 : void OPasteClipboardDispatcher::disposing( ::osl::ClearableMutexGuard& _rClearBeforeNotify )
169 : {
170 : OSL_ENSURE( getEditView() && getEditView()->GetWindow(), "OPasteClipboardDispatcher::disposing: EditView should not (yet) be disfunctional here!" );
171 0 : if (m_pClipListener)
172 : {
173 0 : if (getEditView() && getEditView()->GetWindow())
174 0 : m_pClipListener->AddRemoveListener( getEditView()->GetWindow(), false );
175 :
176 0 : m_pClipListener->release();
177 0 : m_pClipListener = NULL;
178 : }
179 :
180 0 : OClipboardDispatcher::disposing( _rClearBeforeNotify );
181 0 : }
182 :
183 :
184 0 : bool OPasteClipboardDispatcher::implIsEnabled( ) const
185 : {
186 0 : return m_bPastePossible && OClipboardDispatcher::implIsEnabled();
187 : }
188 :
189 :
190 : } // namespace frm
191 :
192 :
193 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|