xref: /AOO41X/main/forms/source/component/EditBase.cxx (revision 24acc54625a85f778a4f966495e8f4cd9d7b247c)
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_forms.hxx"
26 #include "EditBase.hxx"
27 #include "property.hxx"
28 #ifndef _FRM_PROPERTY_HRC_
29 #include "property.hrc"
30 #endif
31 #include "services.hxx"
32 #include <tools/debug.hxx>
33 #include <comphelper/basicio.hxx>
34 #include <cppuhelper/queryinterface.hxx>
35 #include "frm_resource.hxx"
36 #ifndef _FRM_RESOURCE_HRC_
37 #include "frm_resource.hrc"
38 #endif
39 
40 //.........................................................................
41 namespace frm
42 {
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star::sdb;
45 using namespace ::com::sun::star::sdbc;
46 using namespace ::com::sun::star::sdbcx;
47 using namespace ::com::sun::star::beans;
48 using namespace ::com::sun::star::container;
49 using namespace ::com::sun::star::form;
50 using namespace ::com::sun::star::awt;
51 using namespace ::com::sun::star::io;
52 using namespace ::com::sun::star::lang;
53 using namespace ::com::sun::star::util;
54 
55 const sal_uInt16 DEFAULT_LONG    =  0x0001;
56 const sal_uInt16 DEFAULT_DOUBLE  =  0x0002;
57 const sal_uInt16 FILTERPROPOSAL  =  0x0004;
58 
DBG_NAME(OEditBaseModel)59 DBG_NAME( OEditBaseModel )
60 //------------------------------------------------------------------
61 OEditBaseModel::OEditBaseModel( const Reference< XMultiServiceFactory >& _rxFactory, const ::rtl::OUString& rUnoControlModelName,
62         const ::rtl::OUString& rDefault, const sal_Bool _bSupportExternalBinding, const sal_Bool _bSupportsValidation )
63     :OBoundControlModel( _rxFactory, rUnoControlModelName, rDefault, sal_True, _bSupportExternalBinding, _bSupportsValidation )
64     ,m_nLastReadVersion(0)
65     ,m_bEmptyIsNull(sal_True)
66     ,m_bFilterProposal(sal_False)
67 {
68     DBG_CTOR( OEditBaseModel, NULL );
69 }
70 
71 //------------------------------------------------------------------
OEditBaseModel(const OEditBaseModel * _pOriginal,const Reference<XMultiServiceFactory> & _rxFactory)72 OEditBaseModel::OEditBaseModel( const OEditBaseModel* _pOriginal, const Reference< XMultiServiceFactory >& _rxFactory )
73      :OBoundControlModel( _pOriginal, _rxFactory )
74      ,m_nLastReadVersion(0)
75 {
76     DBG_CTOR( OEditBaseModel, NULL );
77 
78     m_bFilterProposal = _pOriginal->m_bFilterProposal;
79     m_bEmptyIsNull = _pOriginal->m_bEmptyIsNull;
80     m_aDefault = _pOriginal->m_aDefault;
81     m_aDefaultText = _pOriginal->m_aDefaultText;
82 }
83 
84 //------------------------------------------------------------------
~OEditBaseModel()85 OEditBaseModel::~OEditBaseModel( )
86 {
87     DBG_DTOR( OEditBaseModel, NULL );
88 }
89 
90 // XPersist
91 //------------------------------------------------------------------------------
write(const Reference<XObjectOutputStream> & _rxOutStream)92 void OEditBaseModel::write(const Reference<XObjectOutputStream>& _rxOutStream) throw ( ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
93 {
94     OBoundControlModel::write(_rxOutStream);
95 
96     // Version
97     sal_uInt16 nVersionId = 0x0005;
98     DBG_ASSERT((getPersistenceFlags() & ~PF_SPECIAL_FLAGS) == 0,
99         "OEditBaseModel::write : invalid special version flags !");
100         // please don't use other flags, older versions can't interpret them !
101 
102     nVersionId |= getPersistenceFlags();
103     _rxOutStream->writeShort(nVersionId);
104 
105     // Name
106     _rxOutStream->writeShort(0);    // obsolete
107     _rxOutStream << m_aDefaultText;
108 
109     // Maskierung fuer any
110     sal_uInt16 nAnyMask = 0;
111     if (m_aDefault.getValueType().getTypeClass() == TypeClass_LONG)
112         nAnyMask |= DEFAULT_LONG;
113     else if (m_aDefault.getValueType().getTypeClass() == TypeClass_DOUBLE)
114         nAnyMask |= DEFAULT_DOUBLE;
115 
116     if (m_bFilterProposal)  // da boolean, kein Wert speichern
117         nAnyMask |= FILTERPROPOSAL;
118 
119     _rxOutStream->writeBoolean(m_bEmptyIsNull);
120     _rxOutStream->writeShort(nAnyMask);
121 
122     if ((nAnyMask & DEFAULT_LONG) == DEFAULT_LONG)
123         _rxOutStream->writeLong(getINT32(m_aDefault));
124     else if ((nAnyMask & DEFAULT_DOUBLE) == DEFAULT_DOUBLE)
125         _rxOutStream->writeDouble(getDouble(m_aDefault));
126 
127     // since version 5 we write the help text
128     writeHelpTextCompatibly(_rxOutStream);
129     // (that's potentially bad : at the time I added the above line we had two derived classes : OEditModel and
130     // OFormattedModel. The first one does not have an own version handling, so it can't write the help text itself,
131     // the second one does it's own writing (reading) after calling our method, so normally we shouldn't write any
132     // additional members as this is not compatible to older office versions.
133     // We decided to place the writing of the help text here as it seems the less worse alternative. There is no delivered
134     // office version including formatted controls (and thus the OFormattedModel), and the OFormattedModel::read seems
135     // robust against this change (as it will read a wrong and unknown file version and thus set it's members to defaults).
136 
137     if ((nVersionId & PF_HANDLE_COMMON_PROPS) != 0)
138         writeCommonEditProperties(_rxOutStream);
139 
140     // !!! properties common to all OEditBaseModel derived classes should be written in writeCommonEditProperties !!!
141 }
142 
143 //------------------------------------------------------------------------------
getPersistenceFlags() const144 sal_uInt16 OEditBaseModel::getPersistenceFlags() const
145 {
146     return PF_HANDLE_COMMON_PROPS;
147 }
148 
149 //------------------------------------------------------------------------------
read(const Reference<XObjectInputStream> & _rxInStream)150 void OEditBaseModel::read(const Reference<XObjectInputStream>& _rxInStream) throw ( ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
151 {
152     OBoundControlModel::read(_rxInStream);
153     ::osl::MutexGuard aGuard(m_aMutex);
154 
155     // Version eigene Versionsnummer
156     sal_uInt16 nVersion = _rxInStream->readShort();
157     m_nLastReadVersion = nVersion;
158 
159     sal_Bool bHandleCommonProps = (nVersion & PF_HANDLE_COMMON_PROPS) != 0;
160     nVersion = nVersion & ~PF_SPECIAL_FLAGS;
161 
162     // obsolete
163     _rxInStream->readShort();
164 
165     _rxInStream >> m_aDefaultText;
166 
167     if (nVersion >= 0x0003)
168     {
169         m_bEmptyIsNull = _rxInStream->readBoolean();
170 
171         sal_uInt16 nAnyMask = _rxInStream->readShort();
172         if ((nAnyMask & DEFAULT_LONG) == DEFAULT_LONG)
173         {
174             sal_Int32 nValue = _rxInStream->readLong();
175             m_aDefault <<= (sal_Int32)nValue;
176         }
177         else if ((nAnyMask & DEFAULT_DOUBLE) == DEFAULT_DOUBLE)
178         {
179             double fValue = _rxInStream->readDouble();
180             m_aDefault <<= (double)fValue;
181         }
182 
183         if ((nAnyMask & FILTERPROPOSAL) == FILTERPROPOSAL)
184             m_bFilterProposal = sal_True;
185     }
186 
187     if (nVersion > 4)
188         readHelpTextCompatibly(_rxInStream);
189 
190     if (bHandleCommonProps)
191         readCommonEditProperties(_rxInStream);
192 
193     // Nach dem Lesen die Defaultwerte anzeigen
194     if ( getControlSource().getLength() )
195         // (not if we don't have a control source - the "State" property acts like it is persistent, then)
196         resetNoBroadcast();
197 };
198 
199 //------------------------------------------------------------------------------
defaultCommonEditProperties()200 void OEditBaseModel::defaultCommonEditProperties()
201 {
202     OBoundControlModel::defaultCommonProperties();
203     // no own common properties at the moment
204 }
205 
206 //------------------------------------------------------------------------------
readCommonEditProperties(const Reference<XObjectInputStream> & _rxInStream)207 void OEditBaseModel::readCommonEditProperties(const Reference<XObjectInputStream>& _rxInStream)
208 {
209     sal_Int32 nLen = _rxInStream->readLong();
210 
211     Reference<XMarkableStream>  xMark(_rxInStream, UNO_QUERY);
212     DBG_ASSERT(xMark.is(), "OBoundControlModel::readCommonProperties : can only work with markable streams !");
213     sal_Int32 nMark = xMark->createMark();
214 
215     // read properties common to all OBoundControlModels
216     OBoundControlModel::readCommonProperties(_rxInStream);
217 
218     // read properties common to all OEditBaseModels
219 
220     // skip the remaining bytes
221     xMark->jumpToMark(nMark);
222     _rxInStream->skipBytes(nLen);
223     xMark->deleteMark(nMark);
224 }
225 
226 //------------------------------------------------------------------------------
writeCommonEditProperties(const Reference<XObjectOutputStream> & _rxOutStream)227 void OEditBaseModel::writeCommonEditProperties(const Reference<XObjectOutputStream>& _rxOutStream)
228 {
229     Reference<XMarkableStream>  xMark(_rxOutStream, UNO_QUERY);
230     DBG_ASSERT(xMark.is(), "OEditBaseModel::writeCommonProperties : can only work with markable streams !");
231     sal_Int32 nMark = xMark->createMark();
232 
233     // a placeholder where we will write the overall length (later in this method)
234     sal_Int32 nLen = 0;
235     _rxOutStream->writeLong(nLen);
236 
237     // write properties common to all OBoundControlModels
238     OBoundControlModel::writeCommonProperties(_rxOutStream);
239 
240     // write properties common to all OEditBaseModels
241 
242     // close the block - write the correct length at the beginning
243     nLen = xMark->offsetToMark(nMark) - sizeof(nLen);
244     xMark->jumpToMark(nMark);
245     _rxOutStream->writeLong(nLen);
246     xMark->jumpToFurthest();
247     xMark->deleteMark(nMark);
248 }
249 
250 //------------------------------------------------------------------------------
getFastPropertyValue(Any & rValue,sal_Int32 nHandle) const251 void OEditBaseModel::getFastPropertyValue( Any& rValue, sal_Int32 nHandle ) const
252 {
253     switch (nHandle)
254     {
255         case PROPERTY_ID_EMPTY_IS_NULL:
256             rValue <<= (sal_Bool)m_bEmptyIsNull;
257             break;
258         case PROPERTY_ID_FILTERPROPOSAL:
259             rValue <<= (sal_Bool)m_bFilterProposal;
260             break;
261         case PROPERTY_ID_DEFAULT_TEXT:
262             rValue <<= m_aDefaultText;
263             break;
264         case PROPERTY_ID_DEFAULT_VALUE:
265         case PROPERTY_ID_DEFAULT_DATE:
266         case PROPERTY_ID_DEFAULT_TIME:
267             rValue = m_aDefault;
268             break;
269         default:
270             OBoundControlModel::getFastPropertyValue(rValue, nHandle);
271     }
272 }
273 
274 //------------------------------------------------------------------------------
convertFastPropertyValue(Any & rConvertedValue,Any & rOldValue,sal_Int32 nHandle,const Any & rValue)275 sal_Bool OEditBaseModel::convertFastPropertyValue( Any& rConvertedValue, Any& rOldValue,
276                                             sal_Int32 nHandle, const Any& rValue ) throw( IllegalArgumentException )
277 {
278     sal_Bool bModified(sal_False);
279     switch (nHandle)
280     {
281         case PROPERTY_ID_EMPTY_IS_NULL:
282             bModified = tryPropertyValue(rConvertedValue, rOldValue, rValue, m_bEmptyIsNull);
283             break;
284         case PROPERTY_ID_FILTERPROPOSAL:
285             bModified = tryPropertyValue(rConvertedValue, rOldValue, rValue, m_bFilterProposal);
286             break;
287         case PROPERTY_ID_DEFAULT_TEXT:
288             bModified = tryPropertyValue(rConvertedValue, rOldValue, rValue, m_aDefaultText);
289             break;
290         case PROPERTY_ID_DEFAULT_VALUE:
291             bModified = tryPropertyValue(rConvertedValue, rOldValue, rValue, m_aDefault, ::getCppuType((const double*)0));
292             break;
293         case PROPERTY_ID_DEFAULT_DATE:
294         case PROPERTY_ID_DEFAULT_TIME:
295             bModified = tryPropertyValue(rConvertedValue, rOldValue, rValue, m_aDefault, ::getCppuType((const sal_Int32*)0));
296             break;
297         default:
298             bModified = OBoundControlModel::convertFastPropertyValue(
299                                             rConvertedValue,
300                                             rOldValue,
301                                             nHandle,
302                                             rValue);
303     }
304     return bModified;
305 }
306 
307 //------------------------------------------------------------------------------
setFastPropertyValue_NoBroadcast(sal_Int32 nHandle,const Any & rValue)308 void OEditBaseModel::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const Any& rValue ) throw ( ::com::sun::star::uno::Exception)
309 {
310     switch (nHandle)
311     {
312         case PROPERTY_ID_EMPTY_IS_NULL:
313             DBG_ASSERT(rValue.getValueType().getTypeClass() == TypeClass_BOOLEAN, "invalid type" );
314             m_bEmptyIsNull = getBOOL(rValue);
315             break;
316         case PROPERTY_ID_FILTERPROPOSAL:
317             DBG_ASSERT(rValue.getValueType().getTypeClass() == TypeClass_BOOLEAN, "invalid type" );
318             m_bFilterProposal = getBOOL(rValue);
319             break;
320         // Aenderung der defaultwerte fuehrt zu reset
321         case PROPERTY_ID_DEFAULT_TEXT:
322             DBG_ASSERT(rValue.getValueType().getTypeClass() == TypeClass_STRING, "invalid type" );
323             rValue >>= m_aDefaultText;
324             resetNoBroadcast();
325             break;
326         case PROPERTY_ID_DEFAULT_VALUE:
327         case PROPERTY_ID_DEFAULT_DATE:
328         case PROPERTY_ID_DEFAULT_TIME:
329             m_aDefault = rValue;
330             resetNoBroadcast();
331             break;
332         default:
333             OBoundControlModel::setFastPropertyValue_NoBroadcast(nHandle, rValue );
334     }
335 }
336 
337 //XPropertyState
338 //------------------------------------------------------------------------------
getPropertyDefaultByHandle(sal_Int32 nHandle) const339 Any OEditBaseModel::getPropertyDefaultByHandle( sal_Int32 nHandle ) const
340 {
341     switch (nHandle)
342     {
343         case PROPERTY_ID_DEFAULT_TEXT:
344             return makeAny(::rtl::OUString());
345         case PROPERTY_ID_FILTERPROPOSAL:
346             return Any(makeAny((sal_Bool)sal_False));
347         case PROPERTY_ID_DEFAULT_VALUE:
348         case PROPERTY_ID_DEFAULT_DATE:
349         case PROPERTY_ID_DEFAULT_TIME:
350             return Any();
351         default:
352             return OBoundControlModel::getPropertyDefaultByHandle(nHandle);
353     }
354 }
355 
356 //.........................................................................
357 }
358 //.........................................................................
359 
360