merge the formfield patch from ooo-build
[ooovba.git] / linguistic / source / lngprophelp.cxx
blobc233fdcff24534b441085ba364c8028f0ae00623
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: prophelp.cxx,v $
10 * $Revision: 1.6 $
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_linguistic.hxx"
34 #include <tools/debug.hxx>
36 #include <com/sun/star/linguistic2/LinguServiceEvent.hpp>
37 #include <com/sun/star/linguistic2/LinguServiceEventFlags.hpp>
38 #include <com/sun/star/linguistic2/XLinguServiceEventListener.hpp>
39 #include <com/sun/star/beans/XPropertySet.hpp>
40 #include <osl/mutex.hxx>
42 #include <misc.hxx>
43 #include <lngprops.hxx>
45 #include <lngprophelp.hxx>
48 //using namespace utl;
49 using namespace osl;
50 using namespace rtl;
51 using namespace com::sun::star;
52 using namespace com::sun::star::beans;
53 using namespace com::sun::star::lang;
54 using namespace com::sun::star::uno;
55 using namespace com::sun::star::linguistic2;
56 using namespace linguistic;
58 namespace linguistic
61 ///////////////////////////////////////////////////////////////////////////
63 static const char *aCH[] =
65 UPN_IS_IGNORE_CONTROL_CHARACTERS,
66 UPN_IS_USE_DICTIONARY_LIST,
69 static int nCHCount = sizeof(aCH) / sizeof(aCH[0]);
72 PropertyChgHelper::PropertyChgHelper(
73 const Reference< XInterface > &rxSource,
74 Reference< XPropertySet > &rxPropSet,
75 int nAllowedEvents ) :
76 PropertyChgHelperBase(),
77 aPropNames (nCHCount),
78 xMyEvtObj (rxSource),
79 aLngSvcEvtListeners (GetLinguMutex()),
80 xPropSet (rxPropSet),
81 nEvtFlags (nAllowedEvents)
83 OUString *pName = aPropNames.getArray();
84 for (INT32 i = 0; i < nCHCount; ++i)
86 pName[i] = A2OU( aCH[i] );
89 SetDefaultValues();
93 PropertyChgHelper::PropertyChgHelper( const PropertyChgHelper &rHelper ) :
94 PropertyChgHelperBase(),
95 aLngSvcEvtListeners (GetLinguMutex())
97 RemoveAsPropListener();
98 aPropNames = rHelper.aPropNames;
99 xMyEvtObj = rHelper.xMyEvtObj;
100 xPropSet = rHelper.xPropSet;
101 nEvtFlags = rHelper.nEvtFlags;
102 AddAsPropListener();
104 SetDefaultValues();
105 GetCurrentValues();
109 PropertyChgHelper::~PropertyChgHelper()
114 void PropertyChgHelper::AddPropNames( const char *pNewNames[], INT32 nCount )
116 if (pNewNames && nCount)
118 INT32 nLen = GetPropNames().getLength();
119 GetPropNames().realloc( nLen + nCount );
120 OUString *pName = GetPropNames().getArray();
121 for (INT32 i = 0; i < nCount; ++i)
123 pName[ nLen + i ] = A2OU( pNewNames[ i ] );
129 void PropertyChgHelper::SetDefaultValues()
131 bResIsIgnoreControlCharacters = bIsIgnoreControlCharacters = TRUE;
132 bResIsUseDictionaryList = bIsUseDictionaryList = TRUE;
136 void PropertyChgHelper::GetCurrentValues()
138 INT32 nLen = GetPropNames().getLength();
139 if (GetPropSet().is() && nLen)
141 const OUString *pPropName = GetPropNames().getConstArray();
142 for (INT32 i = 0; i < nLen; ++i)
144 BOOL *pbVal = NULL,
145 *pbResVal = NULL;
147 if (pPropName[i].equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( UPN_IS_IGNORE_CONTROL_CHARACTERS ) ))
149 pbVal = &bIsIgnoreControlCharacters;
150 pbResVal = &bResIsIgnoreControlCharacters;
152 else if (pPropName[i].equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( UPN_IS_USE_DICTIONARY_LIST ) ))
154 pbVal = &bIsUseDictionaryList;
155 pbResVal = &bResIsUseDictionaryList;
158 if (pbVal && pbResVal)
160 GetPropSet()->getPropertyValue( pPropName[i] ) >>= *pbVal;
161 *pbResVal = *pbVal;
168 void PropertyChgHelper::SetTmpPropVals( const PropertyValues &rPropVals )
170 // return value is default value unless there is an explicitly supplied
171 // temporary value
172 bResIsIgnoreControlCharacters = bIsIgnoreControlCharacters;
173 bResIsUseDictionaryList = bIsUseDictionaryList;
175 INT32 nLen = rPropVals.getLength();
176 if (nLen)
178 const PropertyValue *pVal = rPropVals.getConstArray();
179 for (INT32 i = 0; i < nLen; ++i)
181 BOOL *pbResVal = NULL;
182 switch (pVal[i].Handle)
184 case UPH_IS_IGNORE_CONTROL_CHARACTERS :
185 pbResVal = &bResIsIgnoreControlCharacters; break;
186 case UPH_IS_USE_DICTIONARY_LIST :
187 pbResVal = &bResIsUseDictionaryList; break;
188 default:
190 //DBG_ASSERT( 0, "unknown property" );
192 if (pbResVal)
193 pVal[i].Value >>= *pbResVal;
199 BOOL PropertyChgHelper::propertyChange_Impl( const PropertyChangeEvent& rEvt )
201 BOOL bRes = FALSE;
203 if (GetPropSet().is() && rEvt.Source == GetPropSet())
205 INT16 nLngSvcFlags = (nEvtFlags & AE_HYPHENATOR) ?
206 LinguServiceEventFlags::HYPHENATE_AGAIN : 0;
207 BOOL bSCWA = FALSE, // SPELL_CORRECT_WORDS_AGAIN ?
208 bSWWA = FALSE; // SPELL_WRONG_WORDS_AGAIN ?
210 BOOL *pbVal = NULL;
211 switch (rEvt.PropertyHandle)
213 case UPH_IS_IGNORE_CONTROL_CHARACTERS :
215 pbVal = &bIsIgnoreControlCharacters;
216 nLngSvcFlags = 0;
217 break;
219 case UPH_IS_USE_DICTIONARY_LIST :
221 pbVal = &bIsUseDictionaryList;
222 bSCWA = bSWWA = TRUE;
223 break;
225 default:
227 bRes = FALSE;
228 //DBG_ASSERT( 0, "unknown property" );
231 if (pbVal)
232 rEvt.NewValue >>= *pbVal;
234 bRes = 0 != pbVal; // sth changed?
235 if (bRes)
237 BOOL bSpellEvts = (nEvtFlags & AE_SPELLCHECKER) ? TRUE : FALSE;
238 if (bSCWA && bSpellEvts)
239 nLngSvcFlags |= LinguServiceEventFlags::SPELL_CORRECT_WORDS_AGAIN;
240 if (bSWWA && bSpellEvts)
241 nLngSvcFlags |= LinguServiceEventFlags::SPELL_WRONG_WORDS_AGAIN;
242 if (nLngSvcFlags)
244 LinguServiceEvent aEvt( GetEvtObj(), nLngSvcFlags );
245 LaunchEvent( aEvt );
250 return bRes;
254 void SAL_CALL
255 PropertyChgHelper::propertyChange( const PropertyChangeEvent& rEvt )
256 throw(RuntimeException)
258 MutexGuard aGuard( GetLinguMutex() );
259 propertyChange_Impl( rEvt );
263 void PropertyChgHelper::AddAsPropListener()
265 if (xPropSet.is())
267 INT32 nLen = aPropNames.getLength();
268 const OUString *pPropName = aPropNames.getConstArray();
269 for (INT32 i = 0; i < nLen; ++i)
271 if (pPropName[i].getLength())
272 xPropSet->addPropertyChangeListener( pPropName[i], this );
277 void PropertyChgHelper::RemoveAsPropListener()
279 if (xPropSet.is())
281 INT32 nLen = aPropNames.getLength();
282 const OUString *pPropName = aPropNames.getConstArray();
283 for (INT32 i = 0; i < nLen; ++i)
285 if (pPropName[i].getLength())
286 xPropSet->removePropertyChangeListener( pPropName[i], this );
292 void PropertyChgHelper::LaunchEvent( const LinguServiceEvent &rEvt )
294 cppu::OInterfaceIteratorHelper aIt( aLngSvcEvtListeners );
295 while (aIt.hasMoreElements())
297 Reference< XLinguServiceEventListener > xRef( aIt.next(), UNO_QUERY );
298 if (xRef.is())
299 xRef->processLinguServiceEvent( rEvt );
304 void SAL_CALL PropertyChgHelper::disposing( const EventObject& rSource )
305 throw(RuntimeException)
307 MutexGuard aGuard( GetLinguMutex() );
308 if (rSource.Source == xPropSet)
310 RemoveAsPropListener();
311 xPropSet = NULL;
312 aPropNames.realloc( 0 );
317 sal_Bool SAL_CALL
318 PropertyChgHelper::addLinguServiceEventListener(
319 const Reference< XLinguServiceEventListener >& rxListener )
320 throw(RuntimeException)
322 MutexGuard aGuard( GetLinguMutex() );
324 BOOL bRes = FALSE;
325 if (rxListener.is())
327 INT32 nCount = aLngSvcEvtListeners.getLength();
328 bRes = aLngSvcEvtListeners.addInterface( rxListener ) != nCount;
330 return bRes;
334 sal_Bool SAL_CALL
335 PropertyChgHelper::removeLinguServiceEventListener(
336 const Reference< XLinguServiceEventListener >& rxListener )
337 throw(RuntimeException)
339 MutexGuard aGuard( GetLinguMutex() );
341 BOOL bRes = FALSE;
342 if (rxListener.is())
344 INT32 nCount = aLngSvcEvtListeners.getLength();
345 bRes = aLngSvcEvtListeners.removeInterface( rxListener ) != nCount;
347 return bRes;
350 ///////////////////////////////////////////////////////////////////////////
353 PropertyHelper_Thes::PropertyHelper_Thes(
354 const Reference< XInterface > &rxSource,
355 Reference< XPropertySet > &rxPropSet ) :
356 PropertyChgHelper ( rxSource, rxPropSet, 0 )
358 SetDefaultValues();
359 GetCurrentValues();
363 PropertyHelper_Thes::~PropertyHelper_Thes()
368 void SAL_CALL
369 PropertyHelper_Thes::propertyChange( const PropertyChangeEvent& rEvt )
370 throw(RuntimeException)
372 MutexGuard aGuard( GetLinguMutex() );
373 PropertyChgHelper::propertyChange_Impl( rEvt );
377 ///////////////////////////////////////////////////////////////////////////
379 // list of properties from the property set to be used
380 // and listened to
381 static const char *aSP[] =
383 UPN_IS_SPELL_UPPER_CASE,
384 UPN_IS_SPELL_WITH_DIGITS,
385 UPN_IS_SPELL_CAPITALIZATION
389 PropertyHelper_Spell::PropertyHelper_Spell(
390 const Reference< XInterface > & rxSource,
391 Reference< XPropertySet > &rxPropSet ) :
392 PropertyChgHelper ( rxSource, rxPropSet, AE_SPELLCHECKER )
394 AddPropNames( aSP, sizeof(aSP) / sizeof(aSP[0]) );
395 SetDefaultValues();
396 GetCurrentValues();
398 nResMaxNumberOfSuggestions = GetDefaultNumberOfSuggestions();
402 PropertyHelper_Spell::~PropertyHelper_Spell()
407 void PropertyHelper_Spell::SetDefaultValues()
409 PropertyChgHelper::SetDefaultValues();
411 bResIsSpellUpperCase = bIsSpellUpperCase = FALSE;
412 bResIsSpellWithDigits = bIsSpellWithDigits = FALSE;
413 bResIsSpellCapitalization = bIsSpellCapitalization = TRUE;
417 void PropertyHelper_Spell::GetCurrentValues()
419 PropertyChgHelper::GetCurrentValues();
421 INT32 nLen = GetPropNames().getLength();
422 if (GetPropSet().is() && nLen)
424 const OUString *pPropName = GetPropNames().getConstArray();
425 for (INT32 i = 0; i < nLen; ++i)
427 BOOL *pbVal = NULL,
428 *pbResVal = NULL;
430 if (pPropName[i].equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( UPN_IS_SPELL_UPPER_CASE ) ))
432 pbVal = &bIsSpellUpperCase;
433 pbResVal = &bResIsSpellUpperCase;
435 else if (pPropName[i].equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( UPN_IS_SPELL_WITH_DIGITS ) ))
437 pbVal = &bIsSpellWithDigits;
438 pbResVal = &bResIsSpellWithDigits;
440 else if (pPropName[i].equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( UPN_IS_SPELL_CAPITALIZATION ) ))
442 pbVal = &bIsSpellCapitalization;
443 pbResVal = &bResIsSpellCapitalization;
446 if (pbVal && pbResVal)
448 GetPropSet()->getPropertyValue( pPropName[i] ) >>= *pbVal;
449 *pbResVal = *pbVal;
456 BOOL PropertyHelper_Spell::propertyChange_Impl( const PropertyChangeEvent& rEvt )
458 BOOL bRes = PropertyChgHelper::propertyChange_Impl( rEvt );
460 if (!bRes && GetPropSet().is() && rEvt.Source == GetPropSet())
462 INT16 nLngSvcFlags = 0;
463 BOOL bSCWA = FALSE, // SPELL_CORRECT_WORDS_AGAIN ?
464 bSWWA = FALSE; // SPELL_WRONG_WORDS_AGAIN ?
466 BOOL *pbVal = NULL;
467 switch (rEvt.PropertyHandle)
469 case UPH_IS_SPELL_UPPER_CASE :
471 pbVal = &bIsSpellUpperCase;
472 bSCWA = FALSE == *pbVal; // FALSE->TRUE change?
473 bSWWA = !bSCWA; // TRUE->FALSE change?
474 break;
476 case UPH_IS_SPELL_WITH_DIGITS :
478 pbVal = &bIsSpellWithDigits;
479 bSCWA = FALSE == *pbVal; // FALSE->TRUE change?
480 bSWWA = !bSCWA; // TRUE->FALSE change?
481 break;
483 case UPH_IS_SPELL_CAPITALIZATION :
485 pbVal = &bIsSpellCapitalization;
486 bSCWA = FALSE == *pbVal; // FALSE->TRUE change?
487 bSWWA = !bSCWA; // TRUE->FALSE change?
488 break;
490 default:
491 DBG_ASSERT( 0, "unknown property" );
493 if (pbVal)
494 rEvt.NewValue >>= *pbVal;
496 bRes = (pbVal != 0);
497 if (bRes)
499 if (bSCWA)
500 nLngSvcFlags |= LinguServiceEventFlags::SPELL_CORRECT_WORDS_AGAIN;
501 if (bSWWA)
502 nLngSvcFlags |= LinguServiceEventFlags::SPELL_WRONG_WORDS_AGAIN;
503 if (nLngSvcFlags)
505 LinguServiceEvent aEvt( GetEvtObj(), nLngSvcFlags );
506 LaunchEvent( aEvt );
511 return bRes;
515 void SAL_CALL
516 PropertyHelper_Spell::propertyChange( const PropertyChangeEvent& rEvt )
517 throw(RuntimeException)
519 MutexGuard aGuard( GetLinguMutex() );
520 propertyChange_Impl( rEvt );
524 void PropertyHelper_Spell::SetTmpPropVals( const PropertyValues &rPropVals )
526 PropertyChgHelper::SetTmpPropVals( rPropVals );
528 // return value is default value unless there is an explicitly supplied
529 // temporary value
530 nResMaxNumberOfSuggestions = GetDefaultNumberOfSuggestions();
531 bResIsSpellWithDigits = bIsSpellWithDigits;
532 bResIsSpellCapitalization = bIsSpellCapitalization;
534 INT32 nLen = rPropVals.getLength();
535 if (nLen)
537 const PropertyValue *pVal = rPropVals.getConstArray();
538 for (INT32 i = 0; i < nLen; ++i)
540 if (pVal[i].Name.equalsAscii( UPN_MAX_NUMBER_OF_SUGGESTIONS ))
542 pVal[i].Value >>= nResMaxNumberOfSuggestions;
544 else
546 BOOL *pbResVal = NULL;
547 switch (pVal[i].Handle)
549 case UPH_IS_SPELL_UPPER_CASE : pbResVal = &bResIsSpellUpperCase; break;
550 case UPH_IS_SPELL_WITH_DIGITS : pbResVal = &bResIsSpellWithDigits; break;
551 case UPH_IS_SPELL_CAPITALIZATION : pbResVal = &bResIsSpellCapitalization; break;
552 default:
553 DBG_ASSERT( 0, "unknown property" );
555 if (pbResVal)
556 pVal[i].Value >>= *pbResVal;
562 INT16 PropertyHelper_Spell::GetDefaultNumberOfSuggestions() const
564 return 16;
567 ///////////////////////////////////////////////////////////////////////////
569 static const char *aHP[] =
571 UPN_HYPH_MIN_LEADING,
572 UPN_HYPH_MIN_TRAILING,
573 UPN_HYPH_MIN_WORD_LENGTH
577 PropertyHelper_Hyphen::PropertyHelper_Hyphen(
578 const Reference< XInterface > & rxSource,
579 Reference< XPropertySet > &rxPropSet ) :
580 PropertyChgHelper ( rxSource, rxPropSet, AE_HYPHENATOR )
582 AddPropNames( aHP, sizeof(aHP) / sizeof(aHP[0]) );
583 SetDefaultValues();
584 GetCurrentValues();
588 PropertyHelper_Hyphen::~PropertyHelper_Hyphen()
593 void PropertyHelper_Hyphen::SetDefaultValues()
595 PropertyChgHelper::SetDefaultValues();
597 nResHyphMinLeading = nHyphMinLeading = 2;
598 nResHyphMinTrailing = nHyphMinTrailing = 2;
599 nResHyphMinWordLength = nHyphMinWordLength = 0;
603 void PropertyHelper_Hyphen::GetCurrentValues()
605 PropertyChgHelper::GetCurrentValues();
607 INT32 nLen = GetPropNames().getLength();
608 if (GetPropSet().is() && nLen)
610 const OUString *pPropName = GetPropNames().getConstArray();
611 for (INT32 i = 0; i < nLen; ++i)
613 INT16 *pnVal = NULL,
614 *pnResVal = NULL;
616 if (pPropName[i].equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( UPN_HYPH_MIN_LEADING ) ))
618 pnVal = &nHyphMinLeading;
619 pnResVal = &nResHyphMinLeading;
621 else if (pPropName[i].equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( UPN_HYPH_MIN_TRAILING ) ))
623 pnVal = &nHyphMinTrailing;
624 pnResVal = &nResHyphMinTrailing;
626 else if (pPropName[i].equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( UPN_HYPH_MIN_WORD_LENGTH ) ))
628 pnVal = &nHyphMinWordLength;
629 pnResVal = &nResHyphMinWordLength;
632 if (pnVal && pnResVal)
634 GetPropSet()->getPropertyValue( pPropName[i] ) >>= *pnVal;
635 *pnResVal = *pnVal;
642 BOOL PropertyHelper_Hyphen::propertyChange_Impl( const PropertyChangeEvent& rEvt )
644 BOOL bRes = PropertyChgHelper::propertyChange_Impl( rEvt );
646 if (!bRes && GetPropSet().is() && rEvt.Source == GetPropSet())
648 INT16 nLngSvcFlags = LinguServiceEventFlags::HYPHENATE_AGAIN;
650 INT16 *pnVal = NULL;
651 switch (rEvt.PropertyHandle)
653 case UPH_HYPH_MIN_LEADING : pnVal = &nHyphMinLeading; break;
654 case UPH_HYPH_MIN_TRAILING : pnVal = &nHyphMinTrailing; break;
655 case UPH_HYPH_MIN_WORD_LENGTH : pnVal = &nHyphMinWordLength; break;
656 default:
657 DBG_ASSERT( 0, "unknown property" );
659 if (pnVal)
660 rEvt.NewValue >>= *pnVal;
662 bRes = (pnVal != 0);
663 if (bRes)
665 if (nLngSvcFlags)
667 LinguServiceEvent aEvt( GetEvtObj(), nLngSvcFlags );
668 LaunchEvent( aEvt );
673 return bRes;
677 void SAL_CALL
678 PropertyHelper_Hyphen::propertyChange( const PropertyChangeEvent& rEvt )
679 throw(RuntimeException)
681 MutexGuard aGuard( GetLinguMutex() );
682 propertyChange_Impl( rEvt );
686 void PropertyHelper_Hyphen::SetTmpPropVals( const PropertyValues &rPropVals )
688 PropertyChgHelper::SetTmpPropVals( rPropVals );
690 // return value is default value unless there is an explicitly supplied
691 // temporary value
692 nResHyphMinLeading = nHyphMinLeading;
693 nResHyphMinTrailing = nHyphMinTrailing;
694 nResHyphMinWordLength = nHyphMinWordLength;
696 INT32 nLen = rPropVals.getLength();
697 if (nLen)
699 const PropertyValue *pVal = rPropVals.getConstArray();
700 for (INT32 i = 0; i < nLen; ++i)
702 INT16 *pnResVal = NULL;
703 switch (pVal[i].Handle)
705 case UPH_HYPH_MIN_LEADING : pnResVal = &nResHyphMinLeading; break;
706 case UPH_HYPH_MIN_TRAILING : pnResVal = &nResHyphMinTrailing; break;
707 case UPH_HYPH_MIN_WORD_LENGTH : pnResVal = &nResHyphMinWordLength; break;
708 default:
709 DBG_ASSERT( 0, "unknown property" );
711 if (pnResVal)
712 pVal[i].Value >>= *pnResVal;
717 ///////////////////////////////////////////////////////////////////////////
719 } // namespace linguistic