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