xref: /aoo42x/main/svx/source/form/fmmodel.cxx (revision f6e50924)
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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svx.hxx"
26 
27 #include "fmundo.hxx"
28 #include "fmdocumentclassification.hxx"
29 #include "fmcontrollayout.hxx"
30 
31 #include <svx/fmmodel.hxx>
32 #include <svx/fmpage.hxx>
33 #include <svx/svdobj.hxx>
34 #include <tools/debug.hxx>
35 
36 #ifndef SVX_LIGHT
37 #include <sfx2/objsh.hxx>
38 #else
39 class SfxObjectShell;
40 #endif
41 
42 #include <boost/optional.hpp>
43 
44 using ::com::sun::star::uno::Reference;
45 using ::com::sun::star::container::XNameContainer;
46 using namespace svxform;
47 
48 TYPEINIT1(FmFormModel, SdrModel);
49 
50 struct FmFormModelImplData
51 {
52 	FmXUndoEnvironment*		pUndoEnv;
53 	sal_Bool				bOpenInDesignIsDefaulted;
54 	sal_Bool				bMovingPage;
55     ::boost::optional< sal_Bool >
56                             aControlsUseRefDevice;
57 
58 	FmFormModelImplData()
59 		:pUndoEnv( NULL )
60 		,bOpenInDesignIsDefaulted( sal_True )
61 		,bMovingPage( sal_False )
62         ,aControlsUseRefDevice()
63 	{
64 	}
65 };
66 
67 /*************************************************************************
68 |*
69 |* Ctor
70 |*
71 \************************************************************************/
72 FmFormModel::FmFormModel(SfxItemPool* pPool, SfxObjectShell* pPers)
73 			:SdrModel(pPool, pPers, LOADREFCOUNTS)
74 			,m_pImpl(NULL)
75 			,m_pObjShell(0)
76 			,m_bOpenInDesignMode(sal_False)
77 			,m_bAutoControlFocus(sal_False)
78 {
79 #ifndef SVX_LIGHT
80 	m_pImpl = new FmFormModelImplData;
81 	m_pImpl->pUndoEnv = new FmXUndoEnvironment(*this);
82 	m_pImpl->pUndoEnv->acquire();
83 #endif
84 }
85 
86 /*************************************************************************
87 |*
88 |* Ctor
89 |*
90 \************************************************************************/
91 FmFormModel::FmFormModel(const XubString& rPath, SfxItemPool* pPool, SfxObjectShell* pPers)
92 			:SdrModel(rPath, pPool, pPers)
93 			,m_pImpl(NULL)
94 			,m_pObjShell(0)
95 			,m_bOpenInDesignMode(sal_False)
96 			,m_bAutoControlFocus(sal_False)
97 {
98 #ifndef SVX_LIGHT
99 	m_pImpl = new FmFormModelImplData;
100 	m_pImpl->pUndoEnv = new FmXUndoEnvironment(*this);
101 	m_pImpl->pUndoEnv->acquire();
102 #endif
103 }
104 
105 /*************************************************************************
106 |*
107 |* Ctor
108 |*
109 \************************************************************************/
110 FmFormModel::FmFormModel(SfxItemPool* pPool, SfxObjectShell* pPers,
111 						 FASTBOOL bUseExtColorTable
112 						 )
113 			:SdrModel(pPool, pPers, bUseExtColorTable, LOADREFCOUNTS)
114 			,m_pImpl(NULL)
115 			,m_pObjShell(0)
116 			,m_bOpenInDesignMode(sal_False)
117 			,m_bAutoControlFocus(sal_False)
118 {
119 #ifndef SVX_LIGHT
120 	m_pImpl = new FmFormModelImplData;
121 	m_pImpl->pUndoEnv = new FmXUndoEnvironment(*this);
122 	m_pImpl->pUndoEnv->acquire();
123 #endif
124 }
125 
126 /*************************************************************************
127 |*
128 |* Ctor
129 |*
130 \************************************************************************/
131 FmFormModel::FmFormModel(const XubString& rPath, SfxItemPool* pPool, SfxObjectShell* pPers,
132 						 FASTBOOL bUseExtColorTable)
133 			:SdrModel(rPath, pPool, pPers, bUseExtColorTable, LOADREFCOUNTS)
134             ,m_pImpl( NULL )
135 			,m_pObjShell(0)
136 			,m_bOpenInDesignMode(sal_False)
137 			,m_bAutoControlFocus(sal_False)
138 {
139 #ifndef SVX_LIGHT
140 	m_pImpl = new FmFormModelImplData;
141 	m_pImpl->pUndoEnv = new FmXUndoEnvironment(*this);
142 	m_pImpl->pUndoEnv->acquire();
143 #endif
144 }
145 
146 /*************************************************************************
147 |*
148 |* Dtor
149 |*
150 \************************************************************************/
151 FmFormModel::~FmFormModel()
152 {
153 #ifndef SVX_LIGHT
154 	if (m_pObjShell && m_pImpl->pUndoEnv->IsListening(*m_pObjShell))
155 		SetObjectShell(NULL);
156 
157 	ClearUndoBuffer();
158 	// minimale grenze fuer undos
159 	SetMaxUndoActionCount(1);
160 
161 	m_pImpl->pUndoEnv->release();
162 	delete m_pImpl;
163 
164 #endif
165 }
166 
167 /*************************************************************************
168 |*
169 |* Erzeugt eine neue Seite
170 |*
171 \************************************************************************/
172 SdrPage* FmFormModel::AllocPage(FASTBOOL bMasterPage)
173 {
174 	return new FmFormPage(*this, NULL, bMasterPage);
175 }
176 
177 /*************************************************************************
178 |*
179 |* InsertPage
180 |*
181 \************************************************************************/
182 void FmFormModel::InsertPage(SdrPage* pPage, sal_uInt16 nPos)
183 {
184 #ifndef SVX_LIGHT
185 	// hack solange Methode intern
186 	if (m_pObjShell && !m_pImpl->pUndoEnv->IsListening( *m_pObjShell ))
187 		SetObjectShell(m_pObjShell);
188 #endif
189 
190 	SdrModel::InsertPage( pPage, nPos );
191 }
192 
193 /*************************************************************************
194 |*
195 |* MovePage
196 |*
197 \************************************************************************/
198 void FmFormModel::MovePage( sal_uInt16 nPgNum, sal_uInt16 nNewPos )
199 {
200 #ifndef SVX_LIGHT
201 	m_pImpl->bMovingPage = sal_True;
202 		// see InsertPage for this
203 #endif
204 
205 	SdrModel::MovePage( nPgNum, nNewPos );
206 
207 #ifndef SVX_LIGHT
208 	m_pImpl->bMovingPage = sal_False;
209 #endif
210 }
211 
212 /*************************************************************************
213 |*
214 |* RemovePage
215 |*
216 \************************************************************************/
217 SdrPage* FmFormModel::RemovePage(sal_uInt16 nPgNum)
218 {
219     FmFormPage* pToBeRemovedPage = dynamic_cast< FmFormPage* >( GetPage( nPgNum ) );
220     OSL_ENSURE( pToBeRemovedPage, "FmFormModel::RemovePage: *which page*?" );
221 
222 #ifndef SVX_LIGHT
223     if ( pToBeRemovedPage )
224     {
225         Reference< XNameContainer > xForms( pToBeRemovedPage->GetForms( false ) );
226         if ( xForms.is() )
227             m_pImpl->pUndoEnv->RemoveForms( xForms );
228     }
229 #endif
230 
231     FmFormPage* pRemovedPage = (FmFormPage*)SdrModel::RemovePage(nPgNum);
232     OSL_ENSURE( pRemovedPage == pToBeRemovedPage, "FmFormModel::RemovePage: inconsistency!" );
233     return pRemovedPage;
234 }
235 
236 /*************************************************************************
237 |*
238 |* InsertMasterPage
239 |*
240 \************************************************************************/
241 void FmFormModel::InsertMasterPage(SdrPage* pPage, sal_uInt16 nPos)
242 {
243 #ifndef SVX_LIGHT
244 	// hack solange Methode intern
245 	if (m_pObjShell && !m_pImpl->pUndoEnv->IsListening( *m_pObjShell ))
246 		SetObjectShell(m_pObjShell);
247 #endif
248 
249 	SdrModel::InsertMasterPage(pPage, nPos);
250 }
251 
252 /*************************************************************************
253 |*
254 |* RemoveMasterPage
255 |*
256 \************************************************************************/
257 SdrPage* FmFormModel::RemoveMasterPage(sal_uInt16 nPgNum)
258 {
259 	FmFormPage* pPage = (FmFormPage*)SdrModel::RemoveMasterPage(nPgNum);
260 
261 #ifndef SVX_LIGHT
262 	if ( pPage )
263     {
264         Reference< XNameContainer > xForms( pPage->GetForms( false ) );
265         if ( xForms.is() )
266 		    m_pImpl->pUndoEnv->RemoveForms( xForms );
267     }
268 #endif
269 
270 	return pPage;
271 }
272 
273 //------------------------------------------------------------------------
274 SdrLayerID FmFormModel::GetControlExportLayerId( const SdrObject& rObj ) const
275 {
276 	return rObj.GetLayer();
277 }
278 
279 //------------------------------------------------------------------------
280 void FmFormModel::implSetOpenInDesignMode( sal_Bool _bOpenDesignMode, sal_Bool _bForce )
281 {
282 	if( ( _bOpenDesignMode != m_bOpenInDesignMode ) || _bForce )
283 	{
284 		m_bOpenInDesignMode = _bOpenDesignMode;
285 
286 		if ( m_pObjShell )
287 			m_pObjShell->SetModified( sal_True );
288 	}
289 	// no matter if we really did it or not - from now on, it does not count as defaulted anymore
290 	m_pImpl->bOpenInDesignIsDefaulted = sal_False;
291 }
292 
293 //------------------------------------------------------------------------
294 void FmFormModel::SetOpenInDesignMode( sal_Bool bOpenDesignMode )
295 {
296 #ifndef SVX_LIGHT
297 	implSetOpenInDesignMode( bOpenDesignMode, sal_False );
298 #endif
299 }
300 
301 #ifndef SVX_LIGHT
302 //------------------------------------------------------------------------
303 sal_Bool FmFormModel::OpenInDesignModeIsDefaulted( )
304 {
305 	return m_pImpl->bOpenInDesignIsDefaulted;
306 }
307 #endif
308 
309 //------------------------------------------------------------------------
310 sal_Bool FmFormModel::ControlsUseRefDevice() const
311 {
312     if ( !m_pImpl->aControlsUseRefDevice )
313     {
314         DocumentType eDocType = eUnknownDocumentType;
315         if ( m_pObjShell )
316             eDocType = DocumentClassification::classifyHostDocument( m_pObjShell->GetModel() );
317         m_pImpl->aControlsUseRefDevice.reset( ControlLayouter::useDocumentReferenceDevice( eDocType ) );
318     }
319     return *m_pImpl->aControlsUseRefDevice;
320 }
321 
322 //------------------------------------------------------------------------
323 void FmFormModel::SetAutoControlFocus( sal_Bool _bAutoControlFocus )
324 {
325 #ifndef SVX_LIGHT
326 	if( _bAutoControlFocus != m_bAutoControlFocus )
327 	{
328 		m_bAutoControlFocus = _bAutoControlFocus;
329 		m_pObjShell->SetModified( sal_True );
330 	}
331 #endif
332 }
333 
334 //------------------------------------------------------------------------
335 void FmFormModel::SetObjectShell( SfxObjectShell* pShell )
336 {
337 #ifndef SVX_LIGHT
338 	if (pShell == m_pObjShell)
339 		return;
340 
341 	if (m_pObjShell)
342 	{
343 		m_pImpl->pUndoEnv->EndListening( *this );
344 		m_pImpl->pUndoEnv->EndListening( *m_pObjShell );
345 	}
346 
347 	m_pObjShell = pShell;
348 
349 	if (m_pObjShell)
350 	{
351         m_pImpl->pUndoEnv->SetReadOnly( m_pObjShell->IsReadOnly() || m_pObjShell->IsReadOnlyUI(), FmXUndoEnvironment::Accessor() );
352 
353 		if (!m_pImpl->pUndoEnv->IsReadOnly())
354 			 m_pImpl->pUndoEnv->StartListening(*this);
355 
356 		m_pImpl->pUndoEnv->StartListening( *m_pObjShell );
357 	}
358 #endif
359 }
360 
361 //------------------------------------------------------------------------
362 FmXUndoEnvironment&	FmFormModel::GetUndoEnv()
363 {
364 	return *m_pImpl->pUndoEnv;
365 }
366