merge the formfield patch from ooo-build
[ooovba.git] / svtools / source / items1 / ctypeitm.cxx
blob49d90f14eb83310fad550940f7075805ec71ce16
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ctypeitm.cxx,v $
10 * $Revision: 1.7.136.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_svtools.hxx"
33 #include <com/sun/star/uno/Any.hxx>
35 #include <unotools/intlwrapper.hxx>
36 #include <tools/stream.hxx>
37 #include <svtools/ctypeitm.hxx>
39 //============================================================================
40 // The following defines are copied from chaos/source/items/cstritem.cxx:
41 #define CNTSTRINGITEM_STREAM_MAGIC ( (UINT32)0xfefefefe )
42 #define CNTSTRINGITEM_STREAM_SEEKREL (-( (long)( sizeof( UINT32 ) ) ) )
44 //============================================================================
46 // class CntContentTypeItem Implementation.
48 //============================================================================
50 TYPEINIT1_AUTOFACTORY( CntContentTypeItem, CntUnencodedStringItem );
52 #define CONTENT_TYPE_NOT_INIT ( (INetContentType)-1 )
54 //----------------------------------------------------------------------------
55 CntContentTypeItem::CntContentTypeItem()
56 : CntUnencodedStringItem(),
57 _eType( CONTENT_TYPE_NOT_INIT )
61 //----------------------------------------------------------------------------
62 CntContentTypeItem::CntContentTypeItem( USHORT which, const XubString& rType )
63 : CntUnencodedStringItem( which, rType ),
64 _eType( CONTENT_TYPE_NOT_INIT )
68 //----------------------------------------------------------------------------
69 CntContentTypeItem::CntContentTypeItem( USHORT which,
70 const INetContentType eType )
71 : CntUnencodedStringItem( which, INetContentTypes::GetContentType( eType ) ),
72 _eType( eType )
76 //----------------------------------------------------------------------------
77 CntContentTypeItem::CntContentTypeItem( const CntContentTypeItem& rOrig )
78 : CntUnencodedStringItem( rOrig ),
79 _eType( rOrig._eType ),
80 _aPresentation( rOrig._aPresentation )
84 //============================================================================
85 // virtual
86 USHORT CntContentTypeItem::GetVersion(USHORT) const
88 return 1; // because it uses SfxPoolItem::read/writeUnicodeString()
91 //----------------------------------------------------------------------------
92 // virtual
93 SfxPoolItem* CntContentTypeItem::Create( SvStream& rStream,
94 USHORT nItemVersion ) const
96 // CntContentTypeItem used to be derived from CntStringItem, so take that
97 // into account:
98 UniString aValue;
99 readUnicodeString(rStream, aValue, nItemVersion >= 1);
100 UINT32 nMagic = 0;
101 rStream >> nMagic;
102 if (nMagic == CNTSTRINGITEM_STREAM_MAGIC)
104 BOOL bEncrypted = FALSE;
105 rStream >> bEncrypted;
106 DBG_ASSERT(!bEncrypted,
107 "CntContentTypeItem::Create() reads encrypted data");
109 else
110 rStream.SeekRel(CNTSTRINGITEM_STREAM_SEEKREL);
112 return new CntContentTypeItem(Which(), aValue);
115 //----------------------------------------------------------------------------
116 // virtual
117 SvStream & CntContentTypeItem::Store(SvStream & rStream, USHORT) const
119 // CntContentTypeItem used to be derived from CntStringItem, so take that
120 // into account:
121 writeUnicodeString(rStream, GetValue());
122 rStream << CNTSTRINGITEM_STREAM_MAGIC << BOOL(FALSE);
123 return rStream;
126 //----------------------------------------------------------------------------
127 // virtual
128 int CntContentTypeItem::operator==( const SfxPoolItem& rOrig ) const
130 const CntContentTypeItem& rOther = (const CntContentTypeItem&)rOrig;
132 if ( ( _eType != CONTENT_TYPE_NOT_INIT ) &&
133 ( rOther._eType != CONTENT_TYPE_NOT_INIT ) )
134 return _eType == rOther._eType;
135 else
136 return CntUnencodedStringItem::operator==( rOther );
139 //----------------------------------------------------------------------------
140 // virtual
141 SfxPoolItem* CntContentTypeItem::Clone( SfxItemPool* /* pPool */ ) const
143 return new CntContentTypeItem( *this );
146 //----------------------------------------------------------------------------
147 void CntContentTypeItem::SetValue( const XubString& rNewVal )
149 // De-initialize enum type and presentation.
150 _eType = CONTENT_TYPE_NOT_INIT;
151 _aPresentation.Erase();
153 CntUnencodedStringItem::SetValue( rNewVal );
156 //----------------------------------------------------------------------------
157 void CntContentTypeItem::SetPresentation( const XubString& rNewVal )
159 _aPresentation = rNewVal;
162 //----------------------------------------------------------------------------
163 int CntContentTypeItem::Compare( const SfxPoolItem &rWith, const IntlWrapper& rIntlWrapper ) const
165 String aOwnText, aWithText;
166 GetPresentation( SFX_ITEM_PRESENTATION_NAMELESS,
167 SFX_MAPUNIT_APPFONT, SFX_MAPUNIT_APPFONT, aOwnText, &rIntlWrapper );
168 rWith.GetPresentation( SFX_ITEM_PRESENTATION_NAMELESS,
169 SFX_MAPUNIT_APPFONT, SFX_MAPUNIT_APPFONT, aWithText, &rIntlWrapper );
170 return rIntlWrapper.getCollator()->compareString( aOwnText, aWithText );
173 //----------------------------------------------------------------------------
174 SfxItemPresentation CntContentTypeItem::GetPresentation(
175 SfxItemPresentation ePres,
176 SfxMapUnit eCoreMetric,
177 SfxMapUnit ePresMetric,
178 XubString & rText,
179 const IntlWrapper * pIntlWrapper) const
181 if (_aPresentation.Len() == 0)
183 DBG_ASSERT(pIntlWrapper,
184 "CntContentTypeItem::GetPresentation(): No IntlWrapper");
185 if (pIntlWrapper)
186 SAL_CONST_CAST(CntContentTypeItem *, this)->_aPresentation
187 = INetContentTypes::GetPresentation(GetEnumValue(),
188 pIntlWrapper->
189 getLocale());
191 if (_aPresentation.Len() > 0)
193 rText = _aPresentation;
194 return SFX_ITEM_PRESENTATION_COMPLETE;
196 else
197 return CntUnencodedStringItem::GetPresentation(ePres, eCoreMetric,
198 ePresMetric, rText,
199 pIntlWrapper);
202 //----------------------------------------------------------------------------
203 INetContentType CntContentTypeItem::GetEnumValue() const
205 if ( _eType == CONTENT_TYPE_NOT_INIT )
207 // Not yet initialized... Get enum value for string content type.
209 CntContentTypeItem* pVarThis = SAL_CONST_CAST( CntContentTypeItem*, this );
211 pVarThis->_eType = INetContentTypes::GetContentType( GetValue() );
214 return _eType;
217 //----------------------------------------------------------------------------
218 void CntContentTypeItem::SetValue( const INetContentType eType )
220 SetValue( INetContentTypes::GetContentType( eType ) );
222 // Note: SetValue( const String& ....) resets _eType. Set new enum value
223 // after(!) calling it.
224 _eType = eType;
227 //----------------------------------------------------------------------------
228 // virtual
229 BOOL CntContentTypeItem::QueryValue( com::sun::star::uno::Any& rVal,BYTE ) const
231 rVal <<= rtl::OUString(GetValue());
232 return true;
235 //----------------------------------------------------------------------------
236 // virtual
237 BOOL CntContentTypeItem::PutValue( const com::sun::star::uno::Any& rVal,BYTE )
239 rtl::OUString aValue;
240 if ( rVal >>= aValue )
242 // SetValue with an empty string resets the item; so call that
243 // function when PutValue is called with an empty string
244 if (aValue.getLength() == 0)
245 SetValue(aValue);
246 else
247 SetValue(
248 INetContentTypes::RegisterContentType(aValue, UniString()));
249 return true;
252 DBG_ERROR( "CntContentTypeItem::PutValue - Wrong type!" );
253 return false;