1 /*************************************************************************
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * Copyright 2000, 2010 Oracle and/or its affiliates.
5  *
6  * OpenOffice.org - a multi-platform office productivity suite
7  *
8  * This file is part of OpenOffice.org.
9  *
10  * OpenOffice.org is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License version 3
12  * only, as published by the Free Software Foundation.
13  *
14  * OpenOffice.org is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Lesser General Public License version 3 for more details
18  * (a copy is included in the LICENSE file that accompanied this code).
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * version 3 along with OpenOffice.org.  If not, see
22  * <http://www.openoffice.org/license.html>
23  * for a copy of the LGPLv3 License.
24  *
25  ************************************************************************/
26 
27 #include "precompiled_dbaccess.hxx"
28 
29 #include "dbaundomanager.hxx"
30 #include "singledoccontroller.hxx"
31 #include "browserids.hxx"
32 #include "dbu_misc.hrc"
33 #include "dbustrings.hrc"
34 #include "moduledbu.hxx"
35 
36 /** === begin UNO includes === **/
37 /** === end UNO includes === **/
38 
39 #include <svl/undo.hxx>
40 
41 #include <boost/scoped_ptr.hpp>
42 
43 //......................................................................................................................
44 namespace dbaui
45 {
46 //......................................................................................................................
47 
48 	/** === begin UNO using === **/
49 	using ::com::sun::star::uno::Reference;
50 	using ::com::sun::star::uno::XInterface;
51 	using ::com::sun::star::uno::UNO_QUERY;
52 	using ::com::sun::star::uno::UNO_QUERY_THROW;
53 	using ::com::sun::star::uno::UNO_SET_THROW;
54 	using ::com::sun::star::uno::Exception;
55 	using ::com::sun::star::uno::RuntimeException;
56 	using ::com::sun::star::uno::Any;
57 	using ::com::sun::star::uno::makeAny;
58 	using ::com::sun::star::uno::Sequence;
59 	using ::com::sun::star::uno::Type;
60     using ::com::sun::star::document::XUndoManager;
61     using ::com::sun::star::lang::XMultiServiceFactory;
62     using ::com::sun::star::beans::PropertyValue;
63     using ::com::sun::star::lang::EventObject;
64 	/** === end UNO using === **/
65 
66 	//==================================================================================================================
67 	//= OSingleDocumentController_Data
68 	//==================================================================================================================
69     struct OSingleDocumentController_Data
70     {
71         ::boost::scoped_ptr< UndoManager >  m_pUndoManager;
72 
73         OSingleDocumentController_Data( ::cppu::OWeakObject& i_parent, ::osl::Mutex& i_mutex )
74             :m_pUndoManager( new UndoManager( i_parent, i_mutex ) )
75         {
76         }
77     };
78 
79 	//==================================================================================================================
80 	//= OSingleDocumentController
81 	//==================================================================================================================
82 	//------------------------------------------------------------------------------------------------------------------
83     OSingleDocumentController::OSingleDocumentController( const Reference< XMultiServiceFactory >& _rxORB )
84         :OSingleDocumentController_Base( _rxORB )
85         ,m_pData( new OSingleDocumentController_Data( *this, getMutex() ) )
86     {
87     }
88 
89 	//------------------------------------------------------------------------------------------------------------------
90     OSingleDocumentController::~OSingleDocumentController()
91     {
92     }
93 
94 	// -----------------------------------------------------------------------------
95     void SAL_CALL OSingleDocumentController::disposing()
96     {
97         OSingleDocumentController_Base::disposing();
98 		ClearUndoManager();
99         m_pData->m_pUndoManager->disposing();
100     }
101 
102 	// -----------------------------------------------------------------------------
103 	void SAL_CALL OSingleDocumentController::disposing( const EventObject& i_event ) throw( RuntimeException )
104     {
105         // simply disambiguate
106         OSingleDocumentController_Base::disposing( i_event );
107     }
108 
109 	// -----------------------------------------------------------------------------
110     void OSingleDocumentController::ClearUndoManager()
111     {
112         GetUndoManager().Clear();
113     }
114 
115 	// -----------------------------------------------------------------------------
116 	SfxUndoManager& OSingleDocumentController::GetUndoManager() const
117 	{
118 		return m_pData->m_pUndoManager->GetSfxUndoManager();
119 	}
120 
121     // -----------------------------------------------------------------------------
122 	void OSingleDocumentController::addUndoActionAndInvalidate(SfxUndoAction *_pAction)
123 	{
124 		// add undo action
125 		GetUndoManager().AddUndoAction( _pAction );
126 
127         // when we add an undo action the controller was modified
128 		setModified( sal_True );
129 
130         // now inform me that or states changed
131 		InvalidateFeature( ID_BROWSER_UNDO );
132 		InvalidateFeature( ID_BROWSER_REDO );
133 	}
134 
135     // -----------------------------------------------------------------------------
136     Reference< XUndoManager > SAL_CALL OSingleDocumentController::getUndoManager(  ) throw (RuntimeException)
137     {
138         return m_pData->m_pUndoManager.get();
139     }
140 
141     // -----------------------------------------------------------------------------
142 	FeatureState OSingleDocumentController::GetState(sal_uInt16 _nId) const
143 	{
144 		FeatureState aReturn;
145 		switch ( _nId )
146 		{
147 			case ID_BROWSER_UNDO:
148 				aReturn.bEnabled = isEditable() && GetUndoManager().GetUndoActionCount() != 0;
149 				if ( aReturn.bEnabled )
150 				{
151 					String sUndo(ModuleRes(STR_UNDO_COLON));
152 					sUndo += String(RTL_CONSTASCII_USTRINGPARAM(" "));
153 					sUndo += GetUndoManager().GetUndoActionComment();
154 					aReturn.sTitle = sUndo;
155 				}
156 				break;
157 
158 			case ID_BROWSER_REDO:
159 				aReturn.bEnabled = isEditable() && GetUndoManager().GetRedoActionCount() != 0;
160 				if ( aReturn.bEnabled )
161 				{
162 					String sRedo(ModuleRes(STR_REDO_COLON));
163 					sRedo += String(RTL_CONSTASCII_USTRINGPARAM(" "));
164 					sRedo += GetUndoManager().GetRedoActionComment();
165 					aReturn.sTitle = sRedo;
166 				}
167 				break;
168 
169 			default:
170 				aReturn = OSingleDocumentController_Base::GetState(_nId);
171 		}
172 		return aReturn;
173 	}
174 	// -----------------------------------------------------------------------------
175 	void OSingleDocumentController::Execute( sal_uInt16 _nId, const Sequence< PropertyValue >& _rArgs )
176 	{
177 		switch ( _nId )
178 		{
179 			case ID_BROWSER_UNDO:
180 				GetUndoManager().Undo();
181 				InvalidateFeature( ID_BROWSER_UNDO );
182 				InvalidateFeature( ID_BROWSER_REDO );
183 				break;
184 
185             case ID_BROWSER_REDO:
186 				GetUndoManager().Redo();
187 				InvalidateFeature( ID_BROWSER_UNDO );
188 				InvalidateFeature( ID_BROWSER_REDO );
189 				break;
190 
191             default:
192                 OSingleDocumentController_Base::Execute( _nId, _rArgs );
193                 break;
194 		}
195 		InvalidateFeature(_nId);
196 	}
197 
198 //......................................................................................................................
199 } // namespace dbaui
200 //......................................................................................................................
201