workaround clang-14 bug
[LibreOffice.git] / linguistic / workben / sprophelp.cxx
blobf6326ba55feef5c29aa1ec12c527cfa66f45c156
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 .
21 #include <sal/macros.h>
22 #include "linguistic/misc.hxx"
24 #include "sprophelp.hxx"
25 #include "linguistic/lngprops.hxx"
27 #include <com/sun/star/linguistic2/LinguServiceEvent.hpp>
28 #include <com/sun/star/linguistic2/LinguServiceEventFlags.hpp>
29 #include <com/sun/star/linguistic2/XLinguServiceEventListener.hpp>
30 #include <com/sun/star/beans/XPropertySet.hpp>
31 #include <osl/mutex.hxx>
33 using namespace osl;
34 using namespace com::sun::star;
35 using namespace com::sun::star::beans;
36 using namespace com::sun::star::lang;
37 using namespace com::sun::star::uno;
38 using namespace com::sun::star::linguistic2;
39 using namespace linguistic;
42 PropertyChgHelper::PropertyChgHelper(
43 const Reference< XInterface > & rxSource,
44 Reference< XPropertySet > &rxPropSet,
45 const char *pPropNames[], sal_uInt16 nPropCount )
46 : aPropNames(nPropCount)
47 , xMyEvtObj(rxSource)
48 , aLngSvcEvtListeners(GetLinguMutex())
49 , xPropSet(rxPropSet)
51 OUString *pName = aPropNames.getArray();
52 for (sal_Int32 i = 0; i < nPropCount; ++i)
54 pName[i] = OUString::createFromAscii( pPropNames[i] );
59 PropertyChgHelper::PropertyChgHelper( const PropertyChgHelper &rHelper ) :
60 aPropNames(rHelper.aPropNames),
61 xMyEvtObj(rHelper.xMyEvtObj),
62 aLngSvcEvtListeners (GetLinguMutex()),
63 xPropSet(rHelper.xPropSet)
65 AddAsPropListener();
70 PropertyChgHelper::~PropertyChgHelper()
75 void PropertyChgHelper::AddAsPropListener()
77 if (xPropSet.is())
79 sal_Int32 nLen = aPropNames.getLength();
80 const OUString *pPropName = aPropNames.getConstArray();
81 for (sal_Int32 i = 0; i < nLen; ++i)
83 if (pPropName[i].getLength())
84 xPropSet->addPropertyChangeListener( pPropName[i], this );
89 void PropertyChgHelper::RemoveAsPropListener()
91 if (xPropSet.is())
93 sal_Int32 nLen = aPropNames.getLength();
94 const OUString *pPropName = aPropNames.getConstArray();
95 for (sal_Int32 i = 0; i < nLen; ++i)
97 if (pPropName[i].getLength())
98 xPropSet->removePropertyChangeListener( pPropName[i], this );
104 void PropertyChgHelper::LaunchEvent( const LinguServiceEvent &rEvt )
106 cppu::OInterfaceIteratorHelper aIt( aLngSvcEvtListeners );
107 while (aIt.hasMoreElements())
109 Reference< XLinguServiceEventListener > xRef( aIt.next(), UNO_QUERY );
110 if (xRef.is())
111 xRef->processLinguServiceEvent( rEvt );
116 void SAL_CALL PropertyChgHelper::disposing( const EventObject& rSource )
117 throw(RuntimeException)
119 MutexGuard aGuard( GetLinguMutex() );
120 if (rSource.Source == xPropSet)
122 RemoveAsPropListener();
123 xPropSet = NULL;
124 aPropNames.realloc( 0 );
129 sal_Bool SAL_CALL
130 PropertyChgHelper::addLinguServiceEventListener(
131 const Reference< XLinguServiceEventListener >& rxListener )
132 throw(RuntimeException)
134 MutexGuard aGuard( GetLinguMutex() );
136 sal_Bool bRes = sal_False;
137 if (rxListener.is())
139 sal_Int32 nCount = aLngSvcEvtListeners.getLength();
140 bRes = aLngSvcEvtListeners.addInterface( rxListener ) != nCount;
142 return bRes;
146 sal_Bool SAL_CALL
147 PropertyChgHelper::removeLinguServiceEventListener(
148 const Reference< XLinguServiceEventListener >& rxListener )
149 throw(RuntimeException)
151 MutexGuard aGuard( GetLinguMutex() );
153 sal_Bool bRes = sal_False;
154 if (rxListener.is())
156 sal_Int32 nCount = aLngSvcEvtListeners.getLength();
157 bRes = aLngSvcEvtListeners.removeInterface( rxListener ) != nCount;
159 return bRes;
163 static const char *aSP[] =
165 UPN_IS_IGNORE_CONTROL_CHARACTERS,
166 UPN_IS_USE_DICTIONARY_LIST,
167 UPN_IS_SPELL_UPPER_CASE,
168 UPN_IS_SPELL_WITH_DIGITS,
169 UPN_IS_SPELL_CAPITALIZATION,
170 UPN_IS_SPELL_CLOSED_COMPOUND,
171 UPN_IS_SPELL_HYPHENATED_COMPOUND
175 PropertyHelper_Spell::PropertyHelper_Spell(
176 const Reference< XInterface > & rxSource,
177 Reference< XPropertySet > &rxPropSet ) :
178 PropertyChgHelper ( rxSource, rxPropSet, aSP, SAL_N_ELEMENTS(aSP) )
180 SetDefault();
181 sal_Int32 nLen = GetPropNames().getLength();
182 if (rxPropSet.is() && nLen)
184 const OUString *pPropName = GetPropNames().getConstArray();
185 for (sal_Int32 i = 0; i < nLen; ++i)
187 sal_Bool *pbVal = NULL,
188 *pbResVal = NULL;
190 if (OUString( UPN_IS_IGNORE_CONTROL_CHARACTERS ) == pPropName[i])
192 pbVal = &bIsIgnoreControlCharacters;
193 pbResVal = &bResIsIgnoreControlCharacters;
195 else if (OUString( UPN_IS_USE_DICTIONARY_LIST ) == pPropName[i])
197 pbVal = &bIsUseDictionaryList;
198 pbResVal = &bResIsUseDictionaryList;
200 else if (OUString( UPN_IS_SPELL_UPPER_CASE ) == pPropName[i])
202 pbVal = &bIsSpellUpperCase;
203 pbResVal = &bResIsSpellUpperCase;
205 else if (OUString( UPN_IS_SPELL_WITH_DIGITS ) == pPropName[i])
207 pbVal = &bIsSpellWithDigits;
208 pbResVal = &bResIsSpellWithDigits;
210 else if (OUString( UPN_IS_SPELL_CAPITALIZATION ) == pPropName[i])
212 pbVal = &bIsSpellCapitalization;
213 pbResVal = &bResIsSpellCapitalization;
215 else if (OUString( UPN_IS_SPELL_CLOSED_COMPOUND ) == pPropName[i])
217 pbVal = &bIsSpellClosedCompound;
218 pbResVal = &bResIsSpellClosedCompound;
220 else if (OUString( UPN_IS_SPELL_HYPHENATED_COMPOUND ) == pPropName[i])
222 pbVal = &bIsSpellHyphenatedCompound;
223 pbResVal = &bResIsSpellHyphenatedCompound;
226 if (pbVal && pbResVal)
228 rxPropSet->getPropertyValue( pPropName[i] ) >>= *pbVal;
229 *pbResVal = *pbVal;
236 PropertyHelper_Spell::~PropertyHelper_Spell()
241 void PropertyHelper_Spell::SetDefault()
243 bResIsIgnoreControlCharacters = bIsIgnoreControlCharacters = sal_True;
244 bResIsUseDictionaryList = bIsUseDictionaryList = sal_True;
245 bResIsSpellUpperCase = bIsSpellUpperCase = sal_False;
246 bResIsSpellWithDigits = bIsSpellWithDigits = sal_False;
247 bResIsSpellCapitalization = bIsSpellCapitalization = sal_True;
248 bResIsSpellClosedCompound = bIsSpellClosedCompound = sal_True;
249 bResIsSpellHyphenatedCompound = bIsSpellHyphenatedCompound = sal_True;
253 void SAL_CALL
254 PropertyHelper_Spell::propertyChange( const PropertyChangeEvent& rEvt )
255 throw(RuntimeException)
257 MutexGuard aGuard( GetLinguMutex() );
259 if (GetPropSet().is() && rEvt.Source == GetPropSet())
261 sal_Int16 nLngSvcFlags = 0;
262 sal_Bool bSCWA = sal_False, // SPELL_CORRECT_WORDS_AGAIN ?
263 bSWWA = sal_False; // SPELL_WRONG_WORDS_AGAIN ?
265 sal_Bool *pbVal = NULL;
266 switch (rEvt.PropertyHandle)
268 case UPH_IS_IGNORE_CONTROL_CHARACTERS :
270 pbVal = &bIsIgnoreControlCharacters;
271 break;
273 case UPH_IS_USE_DICTIONARY_LIST :
275 pbVal = &bIsUseDictionaryList;
276 bSCWA = bSWWA = sal_True;
277 break;
279 case UPH_IS_SPELL_UPPER_CASE :
281 pbVal = &bIsSpellUpperCase;
282 bSCWA = sal_False == *pbVal; // sal_False->sal_True change?
283 bSWWA = !bSCWA; // sal_True->sal_False change?
284 break;
286 case UPH_IS_SPELL_WITH_DIGITS :
288 pbVal = &bIsSpellWithDigits;
289 bSCWA = sal_False == *pbVal; // sal_False->sal_True change?
290 bSWWA = !bSCWA; // sal_True->sal_False change?
291 break;
293 case UPH_IS_SPELL_CAPITALIZATION :
295 pbVal = &bIsSpellCapitalization;
296 bSCWA = sal_False == *pbVal; // sal_False->sal_True change?
297 bSWWA = !bSCWA; // sal_True->sal_False change?
298 break;
300 case UPH_IS_SPELL_CLOSED_COMPOUND :
302 pbVal = &bIsSpellClosedCompound;
303 bSCWA = sal_False == *pbVal; // sal_False->sal_True change?
304 bSWWA = !bSCWA; // sal_True->sal_False change?
305 break;
307 case UPH_IS_SPELL_HYPHENATED_COMPOUND :
309 pbVal = &bIsSpellHyphenatedCompound;
310 bSCWA = sal_False == *pbVal; // sal_False->sal_True change?
311 bSWWA = !bSCWA; // sal_True->sal_False change?
312 break;
315 default:
316 OSL_FAIL( "unknown property" );
318 if (pbVal)
319 rEvt.NewValue >>= *pbVal;
321 if (bSCWA)
322 nLngSvcFlags |= LinguServiceEventFlags::SPELL_CORRECT_WORDS_AGAIN;
323 if (bSWWA)
324 nLngSvcFlags |= LinguServiceEventFlags::SPELL_WRONG_WORDS_AGAIN;
325 if (nLngSvcFlags)
327 LinguServiceEvent aEvt( GetEvtObj(), nLngSvcFlags );
328 LaunchEvent( aEvt );
334 void PropertyHelper_Spell::SetTmpPropVals( const PropertyValues &rPropVals )
336 // set return value to default value unless there is an
337 // explicitly supplied temporary value
338 bResIsIgnoreControlCharacters = bIsIgnoreControlCharacters;
339 bResIsUseDictionaryList = bIsUseDictionaryList;
340 bResIsSpellUpperCase = bIsSpellUpperCase;
341 bResIsSpellWithDigits = bIsSpellWithDigits;
342 bResIsSpellCapitalization = bIsSpellCapitalization;
343 bResIsSpellClosedCompound = bIsSpellClosedCompound;
344 bResIsSpellHyphenatedCompound = bIsSpellHyphenatedCompound;
346 sal_Int32 nLen = rPropVals.getLength();
347 if (nLen)
349 const PropertyValue *pVal = rPropVals.getConstArray();
350 for (sal_Int32 i = 0; i < nLen; ++i)
352 sal_Bool *pbResVal = NULL;
353 switch (pVal[i].Handle)
355 case UPH_IS_IGNORE_CONTROL_CHARACTERS : pbResVal = &bResIsIgnoreControlCharacters; break;
356 case UPH_IS_USE_DICTIONARY_LIST : pbResVal = &bResIsUseDictionaryList; break;
357 case UPH_IS_SPELL_UPPER_CASE : pbResVal = &bResIsSpellUpperCase; break;
358 case UPH_IS_SPELL_WITH_DIGITS : pbResVal = &bResIsSpellWithDigits; break;
359 case UPH_IS_SPELL_CAPITALIZATION : pbResVal = &bResIsSpellCapitalization; break;
360 case UPH_IS_SPELL_CLOSED_COMPOUND : pbResVal = &bResIsSpellClosedCompound; break;
361 case UPH_IS_SPELL_HYPHENATED_COMPOUND : pbResVal = &bResIsSpellHyphenatedCompound; break;
362 default:
363 OSL_FAIL( "unknown property" );
365 if (pbResVal)
366 pVal[i].Value >>= *pbResVal;
372 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */