merge the formfield patch from ooo-build
[ooovba.git] / sc / source / filter / excel / xlroot.cxx
blob0b6af9da2e63aba363d30a53f9987b5b4e2700a6
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xlroot.cxx,v $
10 * $Revision: 1.32.88.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
33 #include "xlroot.hxx"
34 #include <com/sun/star/i18n/ScriptType.hpp>
35 #include <vcl/svapp.hxx>
36 #include <svtools/stritem.hxx>
37 #include <svtools/languageoptions.hxx>
38 #include <svtools/useroptions.hxx>
39 #include <sfx2/objsh.hxx>
40 #include <sfx2/printer.hxx>
41 #include <sfx2/docfile.hxx>
42 #include <vcl/font.hxx>
43 #include <svx/editstat.hxx>
44 #include "scitems.hxx"
45 #include <svx/eeitem.hxx>
46 #include "document.hxx"
47 #include "docpool.hxx"
48 #include "docuno.hxx"
49 #include "editutil.hxx"
50 #include "drwlayer.hxx"
51 #include "scextopt.hxx"
52 #include "patattr.hxx"
53 #include "fapihelper.hxx"
54 #include "xlconst.hxx"
55 #include "xlstyle.hxx"
56 #include "xlchart.hxx"
57 #include "xltracer.hxx"
59 #include "root.hxx"
61 namespace ApiScriptType = ::com::sun::star::i18n::ScriptType;
63 using ::rtl::OUString;
65 // Global data ================================================================
67 #ifdef DBG_UTIL
68 XclDebugObjCounter::~XclDebugObjCounter()
70 DBG_ASSERT( mnObjCnt == 0, "XclDebugObjCounter::~XclDebugObjCounter - wrong root object count" );
72 #endif
74 // ----------------------------------------------------------------------------
76 XclRootData::XclRootData( XclBiff eBiff, SfxMedium& rMedium,
77 SotStorageRef xRootStrg, ScDocument& rDoc, rtl_TextEncoding eTextEnc, bool bExport ) :
78 meBiff( eBiff ),
79 meOutput( EXC_OUTPUT_BINARY ),
80 mrMedium( rMedium ),
81 mxRootStrg( xRootStrg ),
82 mrDoc( rDoc ),
83 maDefPassword( CREATE_STRING( "VelvetSweatshop" ) ),
84 meTextEnc( eTextEnc ),
85 meSysLang( Application::GetSettings().GetLanguage() ),
86 meDocLang( Application::GetSettings().GetLanguage() ),
87 meUILang( Application::GetSettings().GetUILanguage() ),
88 mnDefApiScript( ApiScriptType::LATIN ),
89 maScMaxPos( MAXCOL, MAXROW, MAXTAB ),
90 maXclMaxPos( EXC_MAXCOL2, EXC_MAXROW2, EXC_MAXTAB2 ),
91 maMaxPos( EXC_MAXCOL2, EXC_MAXROW2, EXC_MAXTAB2 ),
92 mxFontPropSetHlp( new XclFontPropSetHelper ),
93 mxChPropSetHlp( new XclChPropSetHelper ),
94 mxRD( new RootData ),//!
95 mnCharWidth( 110 ),
96 mnScTab( 0 ),
97 mbExport( bExport )
99 // user name
100 maUserName = SvtUserOptions().GetLastName();
101 if( maUserName.Len() == 0 )
102 maUserName = CREATE_STRING( "Calc" );
104 // default script type, e.g. for empty cells
105 switch( ScGlobal::GetDefaultScriptType() )
107 case SCRIPTTYPE_LATIN: mnDefApiScript = ApiScriptType::LATIN; break;
108 case SCRIPTTYPE_ASIAN: mnDefApiScript = ApiScriptType::ASIAN; break;
109 case SCRIPTTYPE_COMPLEX: mnDefApiScript = ApiScriptType::COMPLEX; break;
110 default: DBG_ERRORFILE( "XclRootData::XclRootData - unknown script type" );
113 // maximum cell position
114 switch( meBiff )
116 case EXC_BIFF2: maXclMaxPos.Set( EXC_MAXCOL2, EXC_MAXROW2, EXC_MAXTAB2 ); break;
117 case EXC_BIFF3: maXclMaxPos.Set( EXC_MAXCOL3, EXC_MAXROW3, EXC_MAXTAB3 ); break;
118 case EXC_BIFF4: maXclMaxPos.Set( EXC_MAXCOL4, EXC_MAXROW4, EXC_MAXTAB4 ); break;
119 case EXC_BIFF5: maXclMaxPos.Set( EXC_MAXCOL5, EXC_MAXROW5, EXC_MAXTAB5 ); break;
120 case EXC_BIFF8: maXclMaxPos.Set( EXC_MAXCOL8, EXC_MAXROW8, EXC_MAXTAB8 ); break;
121 default: DBG_ERROR_BIFF();
123 maMaxPos.SetCol( ::std::min( maScMaxPos.Col(), maXclMaxPos.Col() ) );
124 maMaxPos.SetRow( ::std::min( maScMaxPos.Row(), maXclMaxPos.Row() ) );
125 maMaxPos.SetTab( ::std::min( maScMaxPos.Tab(), maXclMaxPos.Tab() ) );
127 // document URL and path
128 if( const SfxItemSet* pItemSet = mrMedium.GetItemSet() )
129 if( const SfxStringItem* pItem = static_cast< const SfxStringItem* >( pItemSet->GetItem( SID_FILE_NAME ) ) )
130 maDocUrl = pItem->GetValue();
131 maBasePath = maDocUrl.Copy( 0, maDocUrl.SearchBackward( '/' ) + 1 );
133 // extended document options - always own object, try to copy existing data from document
134 if( const ScExtDocOptions* pOldDocOpt = mrDoc.GetExtDocOptions() )
135 mxExtDocOpt.reset( new ScExtDocOptions( *pOldDocOpt ) );
136 else
137 mxExtDocOpt.reset( new ScExtDocOptions );
140 XclRootData::~XclRootData()
144 // ----------------------------------------------------------------------------
146 XclRoot::XclRoot( XclRootData& rRootData ) :
147 mrData( rRootData )
149 #ifdef DBG_UTIL
150 ++mrData.mnObjCnt;
151 #endif
153 // filter tracer
154 // do not use CREATE_OUSTRING for conditional expression
155 mrData.mxTracer.reset( new XclTracer( GetDocUrl(), OUString::createFromAscii(
156 IsExport() ? "Office.Tracing/Export/Excel" : "Office.Tracing/Import/Excel" ) ) );
159 XclRoot::XclRoot( const XclRoot& rRoot ) :
160 mrData( rRoot.mrData )
162 #ifdef DBG_UTIL
163 ++mrData.mnObjCnt;
164 #endif
167 XclRoot::~XclRoot()
169 #ifdef DBG_UTIL
170 --mrData.mnObjCnt;
171 #endif
174 XclRoot& XclRoot::operator=( const XclRoot& rRoot )
176 (void)rRoot; // avoid compiler warning
177 // allowed for assignment in derived classes - but test if the same root data is used
178 DBG_ASSERT( &mrData == &rRoot.mrData, "XclRoot::operator= - incompatible root data" );
179 return *this;
182 void XclRoot::SetTextEncoding( rtl_TextEncoding eTextEnc )
184 if( eTextEnc != RTL_TEXTENCODING_DONTKNOW )
185 mrData.meTextEnc = eTextEnc;
188 void XclRoot::SetCharWidth( const XclFontData& rFontData )
190 mrData.mnCharWidth = 0;
191 if( OutputDevice* pPrinter = GetPrinter() )
193 Font aFont( rFontData.maName, Size( 0, rFontData.mnHeight ) );
194 aFont.SetFamily( rFontData.GetScFamily( GetTextEncoding() ) );
195 aFont.SetCharSet( rFontData.GetFontEncoding() );
196 aFont.SetWeight( rFontData.GetScWeight() );
197 pPrinter->SetFont( aFont );
198 mrData.mnCharWidth = pPrinter->GetTextWidth( String( '0' ) );
200 if( mrData.mnCharWidth <= 0 )
202 // #i48717# Win98 with HP LaserJet returns 0
203 DBG_ERRORFILE( "XclRoot::SetCharWidth - invalid character width (no printer?)" );
204 mrData.mnCharWidth = 11 * rFontData.mnHeight / 20;
208 String XclRoot::RequestPassword( ::comphelper::IDocPasswordVerifier& rVerifier ) const
210 ::std::vector< OUString > aDefaultPasswords;
211 aDefaultPasswords.push_back( mrData.maDefPassword );
212 return ScfApiHelper::QueryPasswordForMedium( mrData.mrMedium, rVerifier, &aDefaultPasswords );
215 bool XclRoot::HasVbaStorage() const
217 SotStorageRef xRootStrg = GetRootStorage();
218 return xRootStrg.Is() && xRootStrg->IsContained( EXC_STORAGE_VBA_PROJECT );
221 SotStorageRef XclRoot::OpenStorage( SotStorageRef xStrg, const String& rStrgName ) const
223 return mrData.mbExport ?
224 ScfTools::OpenStorageWrite( xStrg, rStrgName ) :
225 ScfTools::OpenStorageRead( xStrg, rStrgName );
228 SotStorageRef XclRoot::OpenStorage( const String& rStrgName ) const
230 return OpenStorage( GetRootStorage(), rStrgName );
233 SotStorageStreamRef XclRoot::OpenStream( SotStorageRef xStrg, const String& rStrmName ) const
235 return mrData.mbExport ?
236 ScfTools::OpenStorageStreamWrite( xStrg, rStrmName ) :
237 ScfTools::OpenStorageStreamRead( xStrg, rStrmName );
240 SotStorageStreamRef XclRoot::OpenStream( const String& rStrmName ) const
242 return OpenStream( GetRootStorage(), rStrmName );
245 SfxObjectShell* XclRoot::GetDocShell() const
247 return GetDoc().GetDocumentShell();
250 ScModelObj* XclRoot::GetDocModelObj() const
252 SfxObjectShell* pDocShell = GetDocShell();
253 return pDocShell ? ScModelObj::getImplementation( pDocShell->GetModel() ) : 0;
256 OutputDevice* XclRoot::GetPrinter() const
258 return GetDoc().GetRefDevice();
261 ScStyleSheetPool& XclRoot::GetStyleSheetPool() const
263 return *GetDoc().GetStyleSheetPool();
266 ScRangeName& XclRoot::GetNamedRanges() const
268 return *GetDoc().GetRangeName();
271 ScDBCollection& XclRoot::GetDatabaseRanges() const
273 return *GetDoc().GetDBCollection();
276 SdrPage* XclRoot::GetSdrPage( SCTAB nScTab ) const
278 return ((nScTab >= 0) && GetDoc().GetDrawLayer()) ?
279 GetDoc().GetDrawLayer()->GetPage( static_cast< sal_uInt16 >( nScTab ) ) : 0;
282 SvNumberFormatter& XclRoot::GetFormatter() const
284 return *GetDoc().GetFormatTable();
287 DateTime XclRoot::GetNullDate() const
289 return *GetFormatter().GetNullDate();
292 double XclRoot::GetDoubleFromDateTime( const DateTime& rDateTime ) const
294 double fValue = rDateTime - GetNullDate();
295 // adjust dates before 1900-03-01 to get correct time values in the range [0.0,1.0)
296 if( rDateTime < DateTime( Date( 1, 3, 1900 ) ) )
297 fValue -= 1.0;
298 return fValue;
301 DateTime XclRoot::GetDateTimeFromDouble( double fValue ) const
303 DateTime aDateTime = GetNullDate() + fValue;
304 // adjust dates before 1900-03-01 to get correct time values
305 if( aDateTime < DateTime( Date( 1, 3, 1900 ) ) )
306 aDateTime += 1L;
307 return aDateTime;
310 ScEditEngineDefaulter& XclRoot::GetEditEngine() const
312 if( !mrData.mxEditEngine.get() )
314 mrData.mxEditEngine.reset( new ScEditEngineDefaulter( GetDoc().GetEnginePool() ) );
315 ScEditEngineDefaulter& rEE = *mrData.mxEditEngine;
316 rEE.SetRefMapMode( MAP_100TH_MM );
317 rEE.SetEditTextObjectPool( GetDoc().GetEditPool() );
318 rEE.SetUpdateMode( FALSE );
319 rEE.EnableUndo( FALSE );
320 rEE.SetControlWord( rEE.GetControlWord() & ~EE_CNTRL_ALLOWBIGOBJS );
322 return *mrData.mxEditEngine;
325 ScHeaderEditEngine& XclRoot::GetHFEditEngine() const
327 if( !mrData.mxHFEditEngine.get() )
329 mrData.mxHFEditEngine.reset( new ScHeaderEditEngine( EditEngine::CreatePool(), TRUE ) );
330 ScHeaderEditEngine& rEE = *mrData.mxHFEditEngine;
331 rEE.SetRefMapMode( MAP_TWIP ); // headers/footers use twips as default metric
332 rEE.SetUpdateMode( FALSE );
333 rEE.EnableUndo( FALSE );
334 rEE.SetControlWord( rEE.GetControlWord() & ~EE_CNTRL_ALLOWBIGOBJS );
336 // set Calc header/footer defaults
337 SfxItemSet* pEditSet = new SfxItemSet( rEE.GetEmptyItemSet() );
338 SfxItemSet aItemSet( *GetDoc().GetPool(), ATTR_PATTERN_START, ATTR_PATTERN_END );
339 ScPatternAttr::FillToEditItemSet( *pEditSet, aItemSet );
340 // FillToEditItemSet() adjusts font height to 1/100th mm, we need twips
341 pEditSet->Put( aItemSet.Get( ATTR_FONT_HEIGHT ), EE_CHAR_FONTHEIGHT );
342 pEditSet->Put( aItemSet.Get( ATTR_CJK_FONT_HEIGHT ), EE_CHAR_FONTHEIGHT_CJK );
343 pEditSet->Put( aItemSet.Get( ATTR_CTL_FONT_HEIGHT ), EE_CHAR_FONTHEIGHT_CTL );
344 rEE.SetDefaults( pEditSet ); // takes ownership
346 return *mrData.mxHFEditEngine;
349 EditEngine& XclRoot::GetDrawEditEngine() const
351 if( !mrData.mxDrawEditEng.get() )
353 mrData.mxDrawEditEng.reset( new EditEngine( &GetDoc().GetDrawLayer()->GetItemPool() ) );
354 EditEngine& rEE = *mrData.mxDrawEditEng;
355 rEE.SetRefMapMode( MAP_100TH_MM );
356 rEE.SetUpdateMode( FALSE );
357 rEE.EnableUndo( FALSE );
358 rEE.SetControlWord( rEE.GetControlWord() & ~EE_CNTRL_ALLOWBIGOBJS );
360 return *mrData.mxDrawEditEng;
363 XclFontPropSetHelper& XclRoot::GetFontPropSetHelper() const
365 return *mrData.mxFontPropSetHlp;
368 XclChPropSetHelper& XclRoot::GetChartPropSetHelper() const
370 return *mrData.mxChPropSetHlp;
373 ScExtDocOptions& XclRoot::GetExtDocOptions() const
375 return *mrData.mxExtDocOpt;
378 XclTracer& XclRoot::GetTracer() const
380 return *mrData.mxTracer;
383 // ============================================================================