1*96de5490SAndrew Rist /**************************************************************
2*96de5490SAndrew Rist  *
3*96de5490SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*96de5490SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*96de5490SAndrew Rist  * distributed with this work for additional information
6*96de5490SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*96de5490SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*96de5490SAndrew Rist  * "License"); you may not use this file except in compliance
9*96de5490SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*96de5490SAndrew Rist  *
11*96de5490SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*96de5490SAndrew Rist  *
13*96de5490SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*96de5490SAndrew Rist  * software distributed under the License is distributed on an
15*96de5490SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*96de5490SAndrew Rist  * KIND, either express or implied.  See the License for the
17*96de5490SAndrew Rist  * specific language governing permissions and limitations
18*96de5490SAndrew Rist  * under the License.
19*96de5490SAndrew Rist  *
20*96de5490SAndrew Rist  *************************************************************/
21*96de5490SAndrew Rist 
22*96de5490SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "precompiled_dbaccess.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include "dbaundomanager.hxx"
27cdf0e10cSrcweir #include "singledoccontroller.hxx"
28cdf0e10cSrcweir #include "browserids.hxx"
29cdf0e10cSrcweir #include "dbu_misc.hrc"
30cdf0e10cSrcweir #include "dbustrings.hrc"
31cdf0e10cSrcweir #include "moduledbu.hxx"
32cdf0e10cSrcweir 
33cdf0e10cSrcweir /** === begin UNO includes === **/
34cdf0e10cSrcweir /** === end UNO includes === **/
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include <svl/undo.hxx>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #include <boost/scoped_ptr.hpp>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir //......................................................................................................................
41cdf0e10cSrcweir namespace dbaui
42cdf0e10cSrcweir {
43cdf0e10cSrcweir //......................................................................................................................
44cdf0e10cSrcweir 
45cdf0e10cSrcweir 	/** === begin UNO using === **/
46cdf0e10cSrcweir 	using ::com::sun::star::uno::Reference;
47cdf0e10cSrcweir 	using ::com::sun::star::uno::XInterface;
48cdf0e10cSrcweir 	using ::com::sun::star::uno::UNO_QUERY;
49cdf0e10cSrcweir 	using ::com::sun::star::uno::UNO_QUERY_THROW;
50cdf0e10cSrcweir 	using ::com::sun::star::uno::UNO_SET_THROW;
51cdf0e10cSrcweir 	using ::com::sun::star::uno::Exception;
52cdf0e10cSrcweir 	using ::com::sun::star::uno::RuntimeException;
53cdf0e10cSrcweir 	using ::com::sun::star::uno::Any;
54cdf0e10cSrcweir 	using ::com::sun::star::uno::makeAny;
55cdf0e10cSrcweir 	using ::com::sun::star::uno::Sequence;
56cdf0e10cSrcweir 	using ::com::sun::star::uno::Type;
57cdf0e10cSrcweir     using ::com::sun::star::document::XUndoManager;
58cdf0e10cSrcweir     using ::com::sun::star::lang::XMultiServiceFactory;
59cdf0e10cSrcweir     using ::com::sun::star::beans::PropertyValue;
60cdf0e10cSrcweir     using ::com::sun::star::lang::EventObject;
61cdf0e10cSrcweir 	/** === end UNO using === **/
62cdf0e10cSrcweir 
63cdf0e10cSrcweir 	//==================================================================================================================
64cdf0e10cSrcweir 	//= OSingleDocumentController_Data
65cdf0e10cSrcweir 	//==================================================================================================================
66cdf0e10cSrcweir     struct OSingleDocumentController_Data
67cdf0e10cSrcweir     {
68cdf0e10cSrcweir         ::boost::scoped_ptr< UndoManager >  m_pUndoManager;
69cdf0e10cSrcweir 
70cdf0e10cSrcweir         OSingleDocumentController_Data( ::cppu::OWeakObject& i_parent, ::osl::Mutex& i_mutex )
71cdf0e10cSrcweir             :m_pUndoManager( new UndoManager( i_parent, i_mutex ) )
72cdf0e10cSrcweir         {
73cdf0e10cSrcweir         }
74cdf0e10cSrcweir     };
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 	//==================================================================================================================
77cdf0e10cSrcweir 	//= OSingleDocumentController
78cdf0e10cSrcweir 	//==================================================================================================================
79cdf0e10cSrcweir 	//------------------------------------------------------------------------------------------------------------------
80cdf0e10cSrcweir     OSingleDocumentController::OSingleDocumentController( const Reference< XMultiServiceFactory >& _rxORB )
81cdf0e10cSrcweir         :OSingleDocumentController_Base( _rxORB )
82cdf0e10cSrcweir         ,m_pData( new OSingleDocumentController_Data( *this, getMutex() ) )
83cdf0e10cSrcweir     {
84cdf0e10cSrcweir     }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 	//------------------------------------------------------------------------------------------------------------------
87cdf0e10cSrcweir     OSingleDocumentController::~OSingleDocumentController()
88cdf0e10cSrcweir     {
89cdf0e10cSrcweir     }
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
92cdf0e10cSrcweir     void SAL_CALL OSingleDocumentController::disposing()
93cdf0e10cSrcweir     {
94cdf0e10cSrcweir         OSingleDocumentController_Base::disposing();
95cdf0e10cSrcweir 		ClearUndoManager();
96cdf0e10cSrcweir         m_pData->m_pUndoManager->disposing();
97cdf0e10cSrcweir     }
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
100cdf0e10cSrcweir 	void SAL_CALL OSingleDocumentController::disposing( const EventObject& i_event ) throw( RuntimeException )
101cdf0e10cSrcweir     {
102cdf0e10cSrcweir         // simply disambiguate
103cdf0e10cSrcweir         OSingleDocumentController_Base::disposing( i_event );
104cdf0e10cSrcweir     }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
107cdf0e10cSrcweir     void OSingleDocumentController::ClearUndoManager()
108cdf0e10cSrcweir     {
109cdf0e10cSrcweir         GetUndoManager().Clear();
110cdf0e10cSrcweir     }
111cdf0e10cSrcweir 
112cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
113cdf0e10cSrcweir 	SfxUndoManager& OSingleDocumentController::GetUndoManager() const
114cdf0e10cSrcweir 	{
115cdf0e10cSrcweir 		return m_pData->m_pUndoManager->GetSfxUndoManager();
116cdf0e10cSrcweir 	}
117cdf0e10cSrcweir 
118cdf0e10cSrcweir     // -----------------------------------------------------------------------------
119cdf0e10cSrcweir 	void OSingleDocumentController::addUndoActionAndInvalidate(SfxUndoAction *_pAction)
120cdf0e10cSrcweir 	{
121cdf0e10cSrcweir 		// add undo action
122cdf0e10cSrcweir 		GetUndoManager().AddUndoAction( _pAction );
123cdf0e10cSrcweir 
124cdf0e10cSrcweir         // when we add an undo action the controller was modified
125cdf0e10cSrcweir 		setModified( sal_True );
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         // now inform me that or states changed
128cdf0e10cSrcweir 		InvalidateFeature( ID_BROWSER_UNDO );
129cdf0e10cSrcweir 		InvalidateFeature( ID_BROWSER_REDO );
130cdf0e10cSrcweir 	}
131cdf0e10cSrcweir 
132cdf0e10cSrcweir     // -----------------------------------------------------------------------------
133cdf0e10cSrcweir     Reference< XUndoManager > SAL_CALL OSingleDocumentController::getUndoManager(  ) throw (RuntimeException)
134cdf0e10cSrcweir     {
135cdf0e10cSrcweir         return m_pData->m_pUndoManager.get();
136cdf0e10cSrcweir     }
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     // -----------------------------------------------------------------------------
139cdf0e10cSrcweir 	FeatureState OSingleDocumentController::GetState(sal_uInt16 _nId) const
140cdf0e10cSrcweir 	{
141cdf0e10cSrcweir 		FeatureState aReturn;
142cdf0e10cSrcweir 		switch ( _nId )
143cdf0e10cSrcweir 		{
144cdf0e10cSrcweir 			case ID_BROWSER_UNDO:
145cdf0e10cSrcweir 				aReturn.bEnabled = isEditable() && GetUndoManager().GetUndoActionCount() != 0;
146cdf0e10cSrcweir 				if ( aReturn.bEnabled )
147cdf0e10cSrcweir 				{
148cdf0e10cSrcweir 					String sUndo(ModuleRes(STR_UNDO_COLON));
149cdf0e10cSrcweir 					sUndo += String(RTL_CONSTASCII_USTRINGPARAM(" "));
150cdf0e10cSrcweir 					sUndo += GetUndoManager().GetUndoActionComment();
151cdf0e10cSrcweir 					aReturn.sTitle = sUndo;
152cdf0e10cSrcweir 				}
153cdf0e10cSrcweir 				break;
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 			case ID_BROWSER_REDO:
156cdf0e10cSrcweir 				aReturn.bEnabled = isEditable() && GetUndoManager().GetRedoActionCount() != 0;
157cdf0e10cSrcweir 				if ( aReturn.bEnabled )
158cdf0e10cSrcweir 				{
159cdf0e10cSrcweir 					String sRedo(ModuleRes(STR_REDO_COLON));
160cdf0e10cSrcweir 					sRedo += String(RTL_CONSTASCII_USTRINGPARAM(" "));
161cdf0e10cSrcweir 					sRedo += GetUndoManager().GetRedoActionComment();
162cdf0e10cSrcweir 					aReturn.sTitle = sRedo;
163cdf0e10cSrcweir 				}
164cdf0e10cSrcweir 				break;
165cdf0e10cSrcweir 
166cdf0e10cSrcweir 			default:
167cdf0e10cSrcweir 				aReturn = OSingleDocumentController_Base::GetState(_nId);
168cdf0e10cSrcweir 		}
169cdf0e10cSrcweir 		return aReturn;
170cdf0e10cSrcweir 	}
171cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
172cdf0e10cSrcweir 	void OSingleDocumentController::Execute( sal_uInt16 _nId, const Sequence< PropertyValue >& _rArgs )
173cdf0e10cSrcweir 	{
174cdf0e10cSrcweir 		switch ( _nId )
175cdf0e10cSrcweir 		{
176cdf0e10cSrcweir 			case ID_BROWSER_UNDO:
177cdf0e10cSrcweir 				GetUndoManager().Undo();
178cdf0e10cSrcweir 				InvalidateFeature( ID_BROWSER_UNDO );
179cdf0e10cSrcweir 				InvalidateFeature( ID_BROWSER_REDO );
180cdf0e10cSrcweir 				break;
181cdf0e10cSrcweir 
182cdf0e10cSrcweir             case ID_BROWSER_REDO:
183cdf0e10cSrcweir 				GetUndoManager().Redo();
184cdf0e10cSrcweir 				InvalidateFeature( ID_BROWSER_UNDO );
185cdf0e10cSrcweir 				InvalidateFeature( ID_BROWSER_REDO );
186cdf0e10cSrcweir 				break;
187cdf0e10cSrcweir 
188cdf0e10cSrcweir             default:
189cdf0e10cSrcweir                 OSingleDocumentController_Base::Execute( _nId, _rArgs );
190cdf0e10cSrcweir                 break;
191cdf0e10cSrcweir 		}
192cdf0e10cSrcweir 		InvalidateFeature(_nId);
193cdf0e10cSrcweir 	}
194cdf0e10cSrcweir 
195cdf0e10cSrcweir //......................................................................................................................
196cdf0e10cSrcweir } // namespace dbaui
197cdf0e10cSrcweir //......................................................................................................................
198