bump product version to 5.0.4.1
[LibreOffice.git] / editeng / source / items / flditem.cxx
blob5df2649ca3dad7f1fdad7887993090f1bbf5ebcf
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <comphelper/string.hxx>
21 #include <vcl/metaact.hxx>
22 #include <svl/zforlist.hxx>
23 #include <tools/urlobj.hxx>
25 #include <unotools/localfilehelper.hxx>
27 #include <editeng/flditem.hxx>
28 #include <editeng/measfld.hxx>
29 #include "editeng/unonames.hxx"
31 #include <tools/tenccvt.hxx>
33 #include <com/sun/star/beans/XPropertySet.hpp>
34 #include <com/sun/star/text/XTextContent.hpp>
35 #include <com/sun/star/text/FilenameDisplayFormat.hpp>
36 #include <com/sun/star/util/DateTime.hpp>
38 using namespace com::sun::star;
40 SvxFieldData* SvxFieldData::Create(const uno::Reference<text::XTextContent>& xTextContent)
42 uno::Reference<beans::XPropertySet> xPropSet(xTextContent, uno::UNO_QUERY);
43 if (!xPropSet.is())
44 return NULL;
46 // we do not support these fields from Writer, so make sure we do not throw
47 // here - see fdo#63436 how to possibly extend Writer to make use of this
48 uno::Any aAny;
49 try {
50 aAny = xPropSet->getPropertyValue(UNO_TC_PROP_TEXTFIELD_TYPE);
51 if ( !aAny.has<sal_Int32>() )
52 return NULL;
54 sal_Int32 nFieldType = aAny.get<sal_Int32>();
56 switch (nFieldType)
58 case text::textfield::Type::TIME:
59 case text::textfield::Type::EXTENDED_TIME:
60 case text::textfield::Type::DATE:
62 bool bIsDate = false;
63 xPropSet->getPropertyValue(UNO_TC_PROP_IS_DATE) >>= bIsDate;
65 if (bIsDate)
67 util::DateTime aDateTime = xPropSet->getPropertyValue(UNO_TC_PROP_DATE_TIME).get<util::DateTime>();
68 Date aDate(aDateTime.Day, aDateTime.Month, aDateTime.Year);
69 bool bIsFixed = false;
70 xPropSet->getPropertyValue(UNO_TC_PROP_IS_FIXED) >>= bIsFixed;
72 SvxDateField* pData = new SvxDateField(aDate, bIsFixed ? SVXDATETYPE_FIX : SVXDATETYPE_VAR);
73 sal_Int32 nNumFmt = -1;
74 xPropSet->getPropertyValue(UNO_TC_PROP_NUMFORMAT) >>= nNumFmt;
75 if (nNumFmt >= SVXDATEFORMAT_APPDEFAULT && nNumFmt <= SVXDATEFORMAT_F)
76 pData->SetFormat(static_cast<SvxDateFormat>(nNumFmt));
78 return pData;
81 if (nFieldType != text::textfield::Type::TIME)
83 util::DateTime aDateTime = xPropSet->getPropertyValue(UNO_TC_PROP_DATE_TIME).get<util::DateTime>();
84 tools::Time aTime(aDateTime);
86 bool bIsFixed = false;
87 xPropSet->getPropertyValue(UNO_TC_PROP_IS_FIXED) >>= bIsFixed;
89 SvxExtTimeField* pData = new SvxExtTimeField(aTime, bIsFixed ? SVXTIMETYPE_FIX : SVXTIMETYPE_VAR);
91 sal_Int32 nNumFmt = -1;
92 xPropSet->getPropertyValue(UNO_TC_PROP_NUMFORMAT) >>= nNumFmt;
93 if (nNumFmt >= SVXTIMEFORMAT_APPDEFAULT && nNumFmt <= SVXTIMEFORMAT_AM_HMSH)
94 pData->SetFormat(static_cast<SvxTimeFormat>(nNumFmt));
96 return pData;
99 return new SvxTimeField();
101 case text::textfield::Type::URL:
103 OUString aRep, aTarget, aURL;
104 sal_Int16 nFmt = -1;
105 xPropSet->getPropertyValue(UNO_TC_PROP_URL_REPRESENTATION) >>= aRep;
106 xPropSet->getPropertyValue(UNO_TC_PROP_URL_TARGET) >>= aTarget;
107 xPropSet->getPropertyValue(UNO_TC_PROP_URL) >>= aURL;
108 xPropSet->getPropertyValue(UNO_TC_PROP_URL_FORMAT) >>= nFmt;
109 SvxURLField* pData = new SvxURLField(aURL, aRep, aRep.isEmpty() ? SVXURLFORMAT_URL : SVXURLFORMAT_REPR);
110 pData->SetTargetFrame(aTarget);
111 if (nFmt >= SVXURLFORMAT_APPDEFAULT && nFmt <= SVXURLFORMAT_REPR)
112 pData->SetFormat(static_cast<SvxURLFormat>(nFmt));
114 return pData;
116 case text::textfield::Type::PAGE:
117 return new SvxPageField();
118 case text::textfield::Type::PAGES:
119 return new SvxPagesField();
120 case text::textfield::Type::PAGE_NAME:
121 return new SvxPageTitleField();
122 case text::textfield::Type::DOCINFO_TITLE:
123 return new SvxFileField();
124 case text::textfield::Type::TABLE:
126 sal_Int32 nTab = 0;
127 xPropSet->getPropertyValue(UNO_TC_PROP_TABLE_POSITION) >>= nTab;
128 return new SvxTableField(nTab);
130 case text::textfield::Type::EXTENDED_FILE:
132 OUString aPresentation;
133 bool bIsFixed = false;
134 sal_Int16 nFmt = text::FilenameDisplayFormat::FULL;
135 xPropSet->getPropertyValue(UNO_TC_PROP_IS_FIXED) >>= bIsFixed;
136 xPropSet->getPropertyValue(UNO_TC_PROP_CURRENT_PRESENTATION) >>= aPresentation;
137 xPropSet->getPropertyValue(UNO_TC_PROP_FILE_FORMAT) >>= nFmt;
139 SvxFileFormat eFmt = SVXFILEFORMAT_NAME_EXT;
140 switch (nFmt)
142 case text::FilenameDisplayFormat::FULL: eFmt = SVXFILEFORMAT_FULLPATH; break;
143 case text::FilenameDisplayFormat::PATH: eFmt = SVXFILEFORMAT_PATH; break;
144 case text::FilenameDisplayFormat::NAME: eFmt = SVXFILEFORMAT_NAME; break;
145 default:;
148 // pass fixed attribute to constructor
149 return new SvxExtFileField(
150 aPresentation, bIsFixed ? SVXFILETYPE_FIX : SVXFILETYPE_VAR, eFmt);
152 case text::textfield::Type::AUTHOR:
154 bool bIsFixed = false;
155 bool bFullName = false;
156 sal_Int16 nFmt = -1;
157 OUString aPresentation, aContent, aFirstName, aLastName;
158 xPropSet->getPropertyValue(UNO_TC_PROP_IS_FIXED) >>= bIsFixed;
159 xPropSet->getPropertyValue(UNO_TC_PROP_AUTHOR_FULLNAME) >>= bFullName;
160 xPropSet->getPropertyValue(UNO_TC_PROP_CURRENT_PRESENTATION) >>= aPresentation;
161 xPropSet->getPropertyValue(UNO_TC_PROP_AUTHOR_CONTENT) >>= aContent;
162 xPropSet->getPropertyValue(UNO_TC_PROP_AUTHOR_FORMAT) >>= nFmt;
164 // do we have CurrentPresentation given? Mimic behaviour of
165 // writer, which means: prefer CurrentPresentation over Content
166 // if both are given.
167 if (!aPresentation.isEmpty())
168 aContent = aPresentation;
170 sal_Int32 nPos = aContent.lastIndexOf(' ', 0);
171 if (nPos > 0)
173 aFirstName = aContent.copy(0, nPos);
174 aLastName = aContent.copy(nPos + 1);
176 else
178 aLastName = aContent;
181 // #92009# pass fixed attribute to constructor
182 SvxAuthorField* pData = new SvxAuthorField(
183 aFirstName, aLastName, OUString(), bIsFixed ? SVXAUTHORTYPE_FIX : SVXAUTHORTYPE_VAR);
185 if (!bFullName)
187 pData->SetFormat(SVXAUTHORFORMAT_SHORTNAME);
189 else if (nFmt >= SVXAUTHORFORMAT_FULLNAME || nFmt <= SVXAUTHORFORMAT_SHORTNAME)
191 pData->SetFormat(static_cast<SvxAuthorFormat>(nFmt));
194 return pData;
196 case text::textfield::Type::MEASURE:
198 SdrMeasureFieldKind eKind = SDRMEASUREFIELD_VALUE;
199 sal_Int16 nTmp = -1;
200 xPropSet->getPropertyValue(UNO_TC_PROP_MEASURE_KIND) >>= nTmp;
201 if (nTmp == static_cast<sal_Int16>(SDRMEASUREFIELD_UNIT) ||
202 nTmp == static_cast<sal_Int16>(SDRMEASUREFIELD_ROTA90BLANCS))
203 eKind = static_cast<SdrMeasureFieldKind>(nTmp);
205 return new SdrMeasureField(eKind);
207 case text::textfield::Type::PRESENTATION_HEADER:
208 return new SvxHeaderField();
209 case text::textfield::Type::PRESENTATION_FOOTER:
210 return new SvxFooterField();
211 case text::textfield::Type::PRESENTATION_DATE_TIME:
212 return new SvxDateTimeField();
213 default:
216 } catch ( const beans::UnknownPropertyException& )
218 return NULL;
221 return NULL;
224 TYPEINIT1( SvxFieldItem, SfxPoolItem );
226 SV_IMPL_PERSIST1( SvxFieldData, SvPersistBase );
230 SvxFieldData::SvxFieldData()
236 SvxFieldData::~SvxFieldData()
242 SvxFieldData* SvxFieldData::Clone() const
244 return new SvxFieldData;
249 bool SvxFieldData::operator==( const SvxFieldData& rFld ) const
251 DBG_ASSERT( Type() == rFld.Type(), "==: Different Types" );
252 (void)rFld;
253 return true; // Basic class is always the same.
258 void SvxFieldData::Load( SvPersistStream & /*rStm*/ )
264 void SvxFieldData::Save( SvPersistStream & /*rStm*/ )
269 MetaAction* SvxFieldData::createBeginComment() const
271 return new MetaCommentAction( "FIELD_SEQ_BEGIN" );
274 MetaAction* SvxFieldData::createEndComment()
276 return new MetaCommentAction( "FIELD_SEQ_END" );
281 SvxFieldItem::SvxFieldItem( SvxFieldData* pFld, const sal_uInt16 nId ) :
282 SfxPoolItem( nId )
284 pField = pFld; // belongs directly to the item
289 SvxFieldItem::SvxFieldItem( const SvxFieldData& rField, const sal_uInt16 nId ) :
290 SfxPoolItem( nId )
292 pField = rField.Clone();
297 SvxFieldItem::SvxFieldItem( const SvxFieldItem& rItem ) :
298 SfxPoolItem ( rItem )
300 pField = rItem.GetField() ? rItem.GetField()->Clone() : 0;
305 SvxFieldItem::~SvxFieldItem()
307 delete pField;
312 SfxPoolItem* SvxFieldItem::Clone( SfxItemPool* ) const
314 return new SvxFieldItem(*this);
319 SfxPoolItem* SvxFieldItem::Create( SvStream& rStrm, sal_uInt16 ) const
321 SvxFieldData* pData = 0;
322 SvPersistStream aPStrm( GetClassManager(), &rStrm );
323 aPStrm >> pData;
325 if( aPStrm.IsEof() )
326 aPStrm.SetError( SVSTREAM_GENERALERROR );
328 if ( aPStrm.GetError() == ERRCODE_IO_NOFACTORY )
329 aPStrm.ResetError(); // Actually a code for that not all were read Attr ...
331 return new SvxFieldItem( pData, Which() );
336 SvStream& SvxFieldItem::Store( SvStream& rStrm, sal_uInt16 /*nItemVersion*/ ) const
338 DBG_ASSERT( pField, "SvxFieldItem::Store: Field?!" );
339 SvPersistStream aPStrm( GetClassManager(), &rStrm );
340 // The reset error in the above Create method did not exist in 3.1,
341 // therefore newer items can not be saved for 3.x-exports!
342 if ( ( rStrm.GetVersion() <= SOFFICE_FILEFORMAT_31 ) && pField &&
343 pField->GetClassId() == 50 /* SdrMeasureField */ )
345 // SvxFieldData not enough, because not registered on ClassMgr.
346 SvxURLField aDummyData;
347 WriteSvPersistBase( aPStrm , &aDummyData );
349 else
350 WriteSvPersistBase( aPStrm, pField );
352 return rStrm;
357 bool SvxFieldItem::operator==( const SfxPoolItem& rItem ) const
359 DBG_ASSERT( SfxPoolItem::operator==( rItem ), "unequal which or type" );
361 const SvxFieldData* pOtherFld = static_cast<const SvxFieldItem&>(rItem).GetField();
362 if ( !pField && !pOtherFld )
363 return true;
365 if ( ( !pField && pOtherFld ) || ( pField && !pOtherFld ) )
366 return false;
368 return ( ( pField->Type() == pOtherFld->Type() )
369 && ( *pField == *pOtherFld ) );
373 // The following are the derivatives of SvxFieldData ...
376 SV_IMPL_PERSIST1( SvxDateField, SvxFieldData );
380 SvxDateField::SvxDateField()
382 nFixDate = Date( Date::SYSTEM ).GetDate();
383 eType = SVXDATETYPE_VAR;
384 eFormat = SVXDATEFORMAT_STDSMALL;
389 SvxDateField::SvxDateField( const Date& rDate, SvxDateType eT, SvxDateFormat eF )
391 nFixDate = rDate.GetDate();
392 eType = eT;
393 eFormat = eF;
398 SvxFieldData* SvxDateField::Clone() const
400 return new SvxDateField( *this );
405 bool SvxDateField::operator==( const SvxFieldData& rOther ) const
407 if ( rOther.Type() != Type() )
408 return false;
410 const SvxDateField& rOtherFld = static_cast<const SvxDateField&>(rOther);
411 return ( ( nFixDate == rOtherFld.nFixDate ) &&
412 ( eType == rOtherFld.eType ) &&
413 ( eFormat == rOtherFld.eFormat ) );
418 void SvxDateField::Load( SvPersistStream & rStm )
420 sal_uInt16 nType, nFormat;
422 rStm.ReadUInt32( nFixDate );
423 rStm.ReadUInt16( nType );
424 rStm.ReadUInt16( nFormat );
426 eType = (SvxDateType)nType;
427 eFormat= (SvxDateFormat)nFormat;
432 void SvxDateField::Save( SvPersistStream & rStm )
434 rStm.WriteUInt32( nFixDate );
435 rStm.WriteUInt16( eType );
436 rStm.WriteUInt16( eFormat );
441 OUString SvxDateField::GetFormatted( SvNumberFormatter& rFormatter, LanguageType eLang ) const
443 Date aDate( Date::EMPTY );
444 if ( eType == SVXDATETYPE_FIX )
445 aDate.SetDate( nFixDate );
446 else
447 aDate = Date( Date::SYSTEM ); // current date
449 return GetFormatted( aDate, eFormat, rFormatter, eLang );
452 OUString SvxDateField::GetFormatted( Date& aDate, SvxDateFormat eFormat, SvNumberFormatter& rFormatter, LanguageType eLang )
454 if ( eFormat == SVXDATEFORMAT_SYSTEM )
456 OSL_FAIL( "SVXDATEFORMAT_SYSTEM not implemented!" );
457 eFormat = SVXDATEFORMAT_STDSMALL;
459 else if ( eFormat == SVXDATEFORMAT_APPDEFAULT )
461 OSL_FAIL( "SVXDATEFORMAT_APPDEFAULT: take them from where? ");
462 eFormat = SVXDATEFORMAT_STDSMALL;
465 sal_uLong nFormatKey;
467 switch( eFormat )
469 case SVXDATEFORMAT_STDSMALL:
470 // short
471 nFormatKey = rFormatter.GetFormatIndex( NF_DATE_SYSTEM_SHORT, eLang );
472 break;
473 case SVXDATEFORMAT_STDBIG:
474 // long
475 nFormatKey = rFormatter.GetFormatIndex( NF_DATE_SYSTEM_LONG, eLang );
476 break;
477 case SVXDATEFORMAT_A:
478 // 13.02.96
479 nFormatKey = rFormatter.GetFormatIndex( NF_DATE_SYS_DDMMYY, eLang );
480 break;
481 case SVXDATEFORMAT_B:
482 // 13.02.1996
483 nFormatKey = rFormatter.GetFormatIndex( NF_DATE_SYS_DDMMYYYY, eLang );
484 break;
485 case SVXDATEFORMAT_C:
486 // 13. Feb 1996
487 nFormatKey = rFormatter.GetFormatIndex( NF_DATE_SYS_DMMMYYYY, eLang );
488 break;
489 case SVXDATEFORMAT_D:
490 // 13. February 1996
491 nFormatKey = rFormatter.GetFormatIndex( NF_DATE_SYS_DMMMMYYYY, eLang );
492 break;
493 case SVXDATEFORMAT_E:
494 // The, 13. February 1996
495 nFormatKey = rFormatter.GetFormatIndex( NF_DATE_SYS_NNDMMMMYYYY, eLang );
496 break;
497 case SVXDATEFORMAT_F:
498 // Tuesday, 13. February 1996
499 nFormatKey = rFormatter.GetFormatIndex( NF_DATE_SYS_NNNNDMMMMYYYY, eLang );
500 break;
501 default:
502 nFormatKey = rFormatter.GetStandardFormat( css::util::NumberFormat::DATE, eLang );
505 double fDiffDate = aDate - *(rFormatter.GetNullDate());
506 OUString aStr;
507 Color* pColor = NULL;
508 rFormatter.GetOutputString( fDiffDate, nFormatKey, aStr, &pColor );
509 return aStr;
512 MetaAction* SvxDateField::createBeginComment() const
514 return new MetaCommentAction( "FIELD_SEQ_BEGIN" );
517 SV_IMPL_PERSIST1( SvxURLField, SvxFieldData );
521 SvxURLField::SvxURLField()
523 eFormat = SVXURLFORMAT_URL;
528 SvxURLField::SvxURLField( const OUString& rURL, const OUString& rRepres, SvxURLFormat eFmt )
529 : aURL( rURL ), aRepresentation( rRepres )
531 eFormat = eFmt;
536 SvxFieldData* SvxURLField::Clone() const
538 return new SvxURLField( *this );
543 bool SvxURLField::operator==( const SvxFieldData& rOther ) const
545 if ( rOther.Type() != Type() )
546 return false;
548 const SvxURLField& rOtherFld = static_cast<const SvxURLField&>(rOther);
549 return ( ( eFormat == rOtherFld.eFormat ) &&
550 ( aURL == rOtherFld.aURL ) &&
551 ( aRepresentation == rOtherFld.aRepresentation ) &&
552 ( aTargetFrame == rOtherFld.aTargetFrame ) );
557 static void write_unicode( SvPersistStream & rStm, const OUString& rString )
559 sal_uInt16 nL = sal::static_int_cast<sal_uInt16>(rString.getLength());
560 rStm.WriteUInt16( nL );
561 //endian specific?, yipes!
562 rStm.Write( rString.getStr(), nL*sizeof(sal_Unicode) );
565 static OUString read_unicode( SvPersistStream & rStm )
567 rtl_uString *pStr = NULL;
568 sal_uInt16 nL = 0;
569 rStm.ReadUInt16( nL );
570 const size_t nMaxRecords = rStm.remainingSize() / sizeof(sal_Unicode);
571 if (nL > nMaxRecords)
573 SAL_WARN("editeng", "Parsing error: " << nMaxRecords <<
574 " max possible entries, but " << nL << " claimed, truncating");
575 nL = nMaxRecords;
577 if (nL)
579 pStr = rtl_uString_alloc(nL);
580 //endian specific?, yipes!
581 rStm.Read(pStr->buffer, nL*sizeof(sal_Unicode));
583 //take ownership of buffer and return, otherwise return empty string
584 return pStr ? OUString(pStr, SAL_NO_ACQUIRE) : OUString();
587 void SvxURLField::Load( SvPersistStream & rStm )
589 sal_uInt16 nFormat = 0;
591 rStm.ReadUInt16( nFormat );
592 eFormat= (SvxURLFormat)nFormat;
594 aURL = read_unicode( rStm );
595 aRepresentation = read_unicode( rStm );
596 aTargetFrame = read_unicode( rStm );
601 void SvxURLField::Save( SvPersistStream & rStm )
603 rStm.WriteUInt16( eFormat );
605 write_unicode( rStm, aURL );
606 write_unicode( rStm, aRepresentation );
607 write_unicode( rStm, aTargetFrame );
610 MetaAction* SvxURLField::createBeginComment() const
612 // #i46618# Adding target URL to metafile comment
613 return new MetaCommentAction( "FIELD_SEQ_BEGIN",
615 reinterpret_cast<const sal_uInt8*>(aURL.getStr()),
616 2*aURL.getLength() );
620 // SvxPageTitleField methods
623 SV_IMPL_PERSIST1( SvxPageTitleField, SvxFieldData );
625 SvxPageTitleField::SvxPageTitleField() {}
627 SvxFieldData* SvxPageTitleField::Clone() const
629 return new SvxPageTitleField();
632 bool SvxPageTitleField::operator==( const SvxFieldData& rCmp ) const
634 return ( rCmp.Type() == TYPE(SvxPageTitleField) );
637 void SvxPageTitleField::Load( SvPersistStream & /*rStm*/ )
641 void SvxPageTitleField::Save( SvPersistStream & /*rStm*/ )
645 MetaAction* SvxPageTitleField::createBeginComment() const
647 return new MetaCommentAction( "FIELD_SEQ_BEGIN;PageTitleField" );
651 // SvxPagesField
653 // The fields that were removed from Calc:
656 SV_IMPL_PERSIST1( SvxPageField, SvxFieldData );
658 SvxPageField::SvxPageField() {}
660 SvxFieldData* SvxPageField::Clone() const
662 return new SvxPageField; // empty
665 bool SvxPageField::operator==( const SvxFieldData& rCmp ) const
667 return ( rCmp.Type() == TYPE(SvxPageField) );
670 void SvxPageField::Load( SvPersistStream & /*rStm*/ )
674 void SvxPageField::Save( SvPersistStream & /*rStm*/ )
678 MetaAction* SvxPageField::createBeginComment() const
680 return new MetaCommentAction( "FIELD_SEQ_BEGIN;PageField" );
684 SV_IMPL_PERSIST1( SvxPagesField, SvxFieldData );
686 SvxPagesField::SvxPagesField() {}
688 SvxFieldData* SvxPagesField::Clone() const
690 return new SvxPagesField; // empty
693 bool SvxPagesField::operator==( const SvxFieldData& rCmp ) const
695 return ( rCmp.Type() == TYPE(SvxPagesField) );
698 void SvxPagesField::Load( SvPersistStream & /*rStm*/ )
702 void SvxPagesField::Save( SvPersistStream & /*rStm*/ )
706 SV_IMPL_PERSIST1( SvxTimeField, SvxFieldData );
708 SvxTimeField::SvxTimeField() {}
710 SvxFieldData* SvxTimeField::Clone() const
712 return new SvxTimeField; // empty
715 bool SvxTimeField::operator==( const SvxFieldData& rCmp ) const
717 return ( rCmp.Type() == TYPE(SvxTimeField) );
720 void SvxTimeField::Load( SvPersistStream & /*rStm*/ )
724 void SvxTimeField::Save( SvPersistStream & /*rStm*/ )
728 MetaAction* SvxTimeField::createBeginComment() const
730 return new MetaCommentAction( "FIELD_SEQ_BEGIN" );
733 SV_IMPL_PERSIST1( SvxFileField, SvxFieldData );
735 SvxFileField::SvxFileField() {}
737 SvxFieldData* SvxFileField::Clone() const
739 return new SvxFileField; // empty
742 bool SvxFileField::operator==( const SvxFieldData& rCmp ) const
744 return ( rCmp.Type() == TYPE(SvxFileField) );
747 void SvxFileField::Load( SvPersistStream & /*rStm*/ )
751 void SvxFileField::Save( SvPersistStream & /*rStm*/ )
755 SV_IMPL_PERSIST1( SvxTableField, SvxFieldData );
757 SvxTableField::SvxTableField() : mnTab(0) {}
759 SvxTableField::SvxTableField(int nTab) : mnTab(nTab) {}
761 void SvxTableField::SetTab(int nTab)
763 mnTab = nTab;
767 SvxFieldData* SvxTableField::Clone() const
769 return new SvxTableField(mnTab);
772 bool SvxTableField::operator==( const SvxFieldData& rCmp ) const
774 if (rCmp.Type() != TYPE(SvxTableField))
775 return false;
777 return mnTab == static_cast<const SvxTableField&>(rCmp).mnTab;
780 void SvxTableField::Load( SvPersistStream & /*rStm*/ )
784 void SvxTableField::Save( SvPersistStream & /*rStm*/ )
789 // SvxExtTimeField
792 SV_IMPL_PERSIST1( SvxExtTimeField, SvxFieldData );
796 SvxExtTimeField::SvxExtTimeField()
797 : m_nFixTime( tools::Time(tools::Time::SYSTEM).GetTime() )
799 eType = SVXTIMETYPE_VAR;
800 eFormat = SVXTIMEFORMAT_STANDARD;
805 SvxExtTimeField::SvxExtTimeField( const tools::Time& rTime, SvxTimeType eT, SvxTimeFormat eF )
806 : m_nFixTime( rTime.GetTime() )
808 eType = eT;
809 eFormat = eF;
814 SvxFieldData* SvxExtTimeField::Clone() const
816 return new SvxExtTimeField( *this );
821 bool SvxExtTimeField::operator==( const SvxFieldData& rOther ) const
823 if ( rOther.Type() != Type() )
824 return false;
826 const SvxExtTimeField& rOtherFld = static_cast<const SvxExtTimeField&>(rOther);
827 return ((m_nFixTime == rOtherFld.m_nFixTime) &&
828 ( eType == rOtherFld.eType ) &&
829 ( eFormat == rOtherFld.eFormat ) );
834 void SvxExtTimeField::Load( SvPersistStream & rStm )
836 sal_uInt16 nType, nFormat;
838 rStm.ReadInt64(m_nFixTime);
839 rStm.ReadUInt16( nType );
840 rStm.ReadUInt16( nFormat );
842 eType = (SvxTimeType) nType;
843 eFormat= (SvxTimeFormat) nFormat;
848 void SvxExtTimeField::Save( SvPersistStream & rStm )
850 rStm.WriteInt64(m_nFixTime);
851 rStm.WriteUInt16( eType );
852 rStm.WriteUInt16( eFormat );
857 OUString SvxExtTimeField::GetFormatted( SvNumberFormatter& rFormatter, LanguageType eLang ) const
859 tools::Time aTime( tools::Time::EMPTY );
860 if ( eType == SVXTIMETYPE_FIX )
861 aTime.SetTime(m_nFixTime);
862 else
863 aTime = tools::Time( tools::Time::SYSTEM ); // current time
864 return GetFormatted( aTime, eFormat, rFormatter, eLang );
867 OUString SvxExtTimeField::GetFormatted( tools::Time& aTime, SvxTimeFormat eFormat, SvNumberFormatter& rFormatter, LanguageType eLang )
869 switch( eFormat )
871 case SVXTIMEFORMAT_SYSTEM :
872 OSL_FAIL( "SVXTIMEFORMAT_SYSTEM: not implemented" );
873 eFormat = SVXTIMEFORMAT_STANDARD;
874 break;
875 case SVXTIMEFORMAT_APPDEFAULT :
876 OSL_FAIL( "SVXTIMEFORMAT_APPDEFAULT: not implemented" );
877 eFormat = SVXTIMEFORMAT_STANDARD;
878 break;
879 default: ;//prevent warning
882 sal_uInt32 nFormatKey;
884 switch( eFormat )
886 case SVXTIMEFORMAT_12_HM:
887 nFormatKey = rFormatter.GetFormatIndex( NF_TIME_HHMMAMPM, eLang );
888 break;
889 case SVXTIMEFORMAT_12_HMSH:
891 // no builtin format available, try to insert or reuse
892 OUString aFormatCode( "HH:MM:SS.00 AM/PM" );
893 sal_Int32 nCheckPos;
894 short nType;
895 rFormatter.PutandConvertEntry( aFormatCode, nCheckPos, nType,
896 nFormatKey, LANGUAGE_ENGLISH_US, eLang );
897 DBG_ASSERT( nCheckPos == 0, "SVXTIMEFORMAT_12_HMSH: could not insert format code" );
898 if ( nCheckPos )
900 nFormatKey = rFormatter.GetFormatIndex( NF_TIME_HH_MMSS00, eLang );
902 break;
904 case SVXTIMEFORMAT_24_HM:
905 nFormatKey = rFormatter.GetFormatIndex( NF_TIME_HHMM, eLang );
906 break;
907 case SVXTIMEFORMAT_24_HMSH:
908 nFormatKey = rFormatter.GetFormatIndex( NF_TIME_HH_MMSS00, eLang );
909 break;
910 case SVXTIMEFORMAT_12_HMS:
911 nFormatKey = rFormatter.GetFormatIndex( NF_TIME_HHMMSSAMPM, eLang );
912 break;
913 case SVXTIMEFORMAT_24_HMS:
914 nFormatKey = rFormatter.GetFormatIndex( NF_TIME_HHMMSS, eLang );
915 break;
916 case SVXTIMEFORMAT_STANDARD:
917 default:
918 nFormatKey = rFormatter.GetStandardFormat( css::util::NumberFormat::TIME, eLang );
921 double fFracTime = aTime.GetTimeInDays();
922 OUString aStr;
923 Color* pColor = NULL;
924 rFormatter.GetOutputString( fFracTime, nFormatKey, aStr, &pColor );
925 return aStr;
928 MetaAction* SvxExtTimeField::createBeginComment() const
930 return new MetaCommentAction( "FIELD_SEQ_BEGIN" );
934 // SvxExtFileField
937 SV_IMPL_PERSIST1( SvxExtFileField, SvxFieldData );
941 SvxExtFileField::SvxExtFileField()
943 eType = SVXFILETYPE_VAR;
944 eFormat = SVXFILEFORMAT_FULLPATH;
949 SvxExtFileField::SvxExtFileField( const OUString& rStr, SvxFileType eT, SvxFileFormat eF )
951 aFile = rStr;
952 eType = eT;
953 eFormat = eF;
958 SvxFieldData* SvxExtFileField::Clone() const
960 return new SvxExtFileField( *this );
965 bool SvxExtFileField::operator==( const SvxFieldData& rOther ) const
967 if ( rOther.Type() != Type() )
968 return false;
970 const SvxExtFileField& rOtherFld = static_cast<const SvxExtFileField&>(rOther);
971 return ( ( aFile == rOtherFld.aFile ) &&
972 ( eType == rOtherFld.eType ) &&
973 ( eFormat == rOtherFld.eFormat ) );
978 void SvxExtFileField::Load( SvPersistStream & rStm )
980 sal_uInt16 nType, nFormat;
982 // UNICODE: rStm >> aFile;
983 aFile = rStm.ReadUniOrByteString(rStm.GetStreamCharSet());
985 rStm.ReadUInt16( nType );
986 rStm.ReadUInt16( nFormat );
988 eType = (SvxFileType) nType;
989 eFormat= (SvxFileFormat) nFormat;
994 void SvxExtFileField::Save( SvPersistStream & rStm )
996 // UNICODE: rStm << aFile;
997 rStm.WriteUniOrByteString(aFile, rStm.GetStreamCharSet());
999 rStm.WriteUInt16( eType );
1000 rStm.WriteUInt16( eFormat );
1005 OUString SvxExtFileField::GetFormatted() const
1007 OUString aString;
1009 INetURLObject aURLObj( aFile );
1011 if( INetProtocol::NotValid == aURLObj.GetProtocol() )
1013 // invalid? try to interpret string as system file name
1014 OUString aURLStr;
1016 ::utl::LocalFileHelper::ConvertPhysicalNameToURL( aFile, aURLStr );
1018 aURLObj.SetURL( aURLStr );
1021 // #92009# Be somewhat liberate when trying to
1022 // get formatted content out of the FileField
1023 if( INetProtocol::NotValid == aURLObj.GetProtocol() )
1025 // still not valid? Then output as is
1026 aString = aFile;
1028 else if( INetProtocol::File == aURLObj.GetProtocol() )
1030 switch( eFormat )
1032 case SVXFILEFORMAT_FULLPATH:
1033 aString = aURLObj.getFSysPath(INetURLObject::FSYS_DETECT);
1034 break;
1036 case SVXFILEFORMAT_PATH:
1037 aURLObj.removeSegment(INetURLObject::LAST_SEGMENT, false);
1038 // #101742# Leave trailing slash at the pathname
1039 aURLObj.setFinalSlash();
1040 aString = aURLObj.getFSysPath(INetURLObject::FSYS_DETECT);
1041 break;
1043 case SVXFILEFORMAT_NAME:
1044 aString = aURLObj.getBase(INetURLObject::LAST_SEGMENT,true,INetURLObject::DECODE_UNAMBIGUOUS);
1045 break;
1047 case SVXFILEFORMAT_NAME_EXT:
1048 aString = aURLObj.getName(INetURLObject::LAST_SEGMENT,true,INetURLObject::DECODE_UNAMBIGUOUS);
1049 break;
1052 else
1054 switch( eFormat )
1056 case SVXFILEFORMAT_FULLPATH:
1057 aString = aURLObj.GetMainURL( INetURLObject::DECODE_TO_IURI );
1058 break;
1060 case SVXFILEFORMAT_PATH:
1061 aURLObj.removeSegment(INetURLObject::LAST_SEGMENT, false);
1062 // #101742# Leave trailing slash at the pathname
1063 aURLObj.setFinalSlash();
1064 aString = aURLObj.GetMainURL( INetURLObject::DECODE_TO_IURI );
1065 break;
1067 case SVXFILEFORMAT_NAME:
1068 aString = aURLObj.getBase();
1069 break;
1071 case SVXFILEFORMAT_NAME_EXT:
1072 aString = aURLObj.getName();
1073 break;
1077 return aString;
1081 // SvxAuthorField
1084 SV_IMPL_PERSIST1( SvxAuthorField, SvxFieldData );
1088 SvxAuthorField::SvxAuthorField()
1090 eType = SVXAUTHORTYPE_VAR;
1091 eFormat = SVXAUTHORFORMAT_FULLNAME;
1096 SvxAuthorField::SvxAuthorField( const OUString& rFirstName,
1097 const OUString& rLastName,
1098 const OUString& rShortName,
1099 SvxAuthorType eT, SvxAuthorFormat eF )
1101 aName = rLastName;
1102 aFirstName = rFirstName;
1103 aShortName = rShortName;
1104 eType = eT;
1105 eFormat = eF;
1110 SvxFieldData* SvxAuthorField::Clone() const
1112 return new SvxAuthorField( *this );
1117 bool SvxAuthorField::operator==( const SvxFieldData& rOther ) const
1119 if ( rOther.Type() != Type() )
1120 return false;
1122 const SvxAuthorField& rOtherFld = static_cast<const SvxAuthorField&>(rOther);
1123 return ( ( aName == rOtherFld.aName ) &&
1124 ( aFirstName == rOtherFld.aFirstName ) &&
1125 ( aShortName == rOtherFld.aShortName ) &&
1126 ( eType == rOtherFld.eType ) &&
1127 ( eFormat == rOtherFld.eFormat ) );
1132 void SvxAuthorField::Load( SvPersistStream & rStm )
1134 sal_uInt16 nType = 0, nFormat = 0;
1136 aName = read_unicode( rStm );
1137 aFirstName = read_unicode( rStm );
1138 aShortName = read_unicode( rStm );
1140 rStm.ReadUInt16( nType );
1141 rStm.ReadUInt16( nFormat );
1143 eType = (SvxAuthorType) nType;
1144 eFormat= (SvxAuthorFormat) nFormat;
1149 void SvxAuthorField::Save( SvPersistStream & rStm )
1151 write_unicode( rStm, aName );
1152 write_unicode( rStm, aFirstName );
1153 write_unicode( rStm, aShortName );
1155 rStm.WriteUInt16( eType );
1156 rStm.WriteUInt16( eFormat );
1161 OUString SvxAuthorField::GetFormatted() const
1163 OUString aString;
1165 switch( eFormat )
1167 case SVXAUTHORFORMAT_FULLNAME:
1168 aString = aFirstName + " " + aName;
1169 break;
1170 case SVXAUTHORFORMAT_NAME:
1171 aString = aName;
1172 break;
1174 case SVXAUTHORFORMAT_FIRSTNAME:
1175 aString = aFirstName;
1176 break;
1178 case SVXAUTHORFORMAT_SHORTNAME:
1179 aString = aShortName;
1180 break;
1183 return aString;
1186 static SvClassManager* pClassMgr=0;
1188 SvClassManager& SvxFieldItem::GetClassManager()
1190 if ( !pClassMgr )
1192 pClassMgr = new SvClassManager;
1193 pClassMgr->Register(SvxFieldData::StaticClassId(), SvxFieldData::CreateInstance);
1194 pClassMgr->Register(SvxURLField::StaticClassId(), SvxURLField::CreateInstance);
1195 pClassMgr->Register(SvxDateField::StaticClassId(), SvxDateField::CreateInstance);
1196 pClassMgr->Register(SvxPageField::StaticClassId(), SvxPageField::CreateInstance);
1197 pClassMgr->Register(SvxPageTitleField::StaticClassId(), SvxPageTitleField::CreateInstance);
1198 pClassMgr->Register(SvxTimeField::StaticClassId(), SvxTimeField::CreateInstance);
1199 pClassMgr->Register(SvxExtTimeField::StaticClassId(), SvxExtTimeField::CreateInstance);
1200 pClassMgr->Register(SvxExtFileField::StaticClassId(), SvxExtFileField::CreateInstance);
1201 pClassMgr->Register(SvxAuthorField::StaticClassId(), SvxAuthorField::CreateInstance);
1204 return *pClassMgr;
1209 SV_IMPL_PERSIST1( SvxHeaderField, SvxFieldData );
1211 SvxHeaderField::SvxHeaderField() {}
1213 SvxFieldData* SvxHeaderField::Clone() const
1215 return new SvxHeaderField; // empty
1218 bool SvxHeaderField::operator==( const SvxFieldData& rCmp ) const
1220 return ( rCmp.Type() == TYPE(SvxHeaderField) );
1223 void SvxHeaderField::Load( SvPersistStream & /*rStm*/ )
1227 void SvxHeaderField::Save( SvPersistStream & /*rStm*/ )
1233 SV_IMPL_PERSIST1( SvxFooterField, SvxFieldData );
1234 SvxFooterField::SvxFooterField() {}
1236 SvxFieldData* SvxFooterField::Clone() const
1238 return new SvxFooterField; // empty
1241 bool SvxFooterField::operator==( const SvxFieldData& rCmp ) const
1243 return ( rCmp.Type() == TYPE(SvxFooterField) );
1246 void SvxFooterField::Load( SvPersistStream & /*rStm*/ )
1250 void SvxFooterField::Save( SvPersistStream & /*rStm*/ )
1256 SV_IMPL_PERSIST1( SvxDateTimeField, SvxFieldData );
1258 SvxFieldData* SvxDateTimeField::Clone() const
1260 return new SvxDateTimeField; // empty
1263 bool SvxDateTimeField::operator==( const SvxFieldData& rCmp ) const
1265 return ( rCmp.Type() == TYPE(SvxDateTimeField) );
1268 void SvxDateTimeField::Load( SvPersistStream & /*rStm*/ )
1272 void SvxDateTimeField::Save( SvPersistStream & /*rStm*/ )
1276 SvxDateTimeField::SvxDateTimeField() {}
1278 OUString SvxDateTimeField::GetFormatted(
1279 Date& rDate, tools::Time& rTime, int eFormat, SvNumberFormatter& rFormatter, LanguageType eLanguage )
1281 OUString aRet;
1283 SvxDateFormat eDateFormat = (SvxDateFormat)(eFormat & 0x0f);
1285 if(eDateFormat)
1287 aRet = SvxDateField::GetFormatted( rDate, eDateFormat, rFormatter, eLanguage );
1290 SvxTimeFormat eTimeFormat = (SvxTimeFormat)((eFormat >> 4) & 0x0f);
1292 if(eTimeFormat)
1294 OUStringBuffer aBuf(aRet);
1296 if (!aRet.isEmpty())
1297 aBuf.append(' ');
1299 aBuf.append(
1300 SvxExtTimeField::GetFormatted(rTime, eTimeFormat, rFormatter, eLanguage));
1302 aRet = aBuf.makeStringAndClear();
1305 return aRet;
1308 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */