merge the formfield patch from ooo-build
[ooovba.git] / svtools / source / config / fontoptions.cxx
blobcc347c8541add33fa495267b32dde3c666d4b512
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: fontoptions.cxx,v $
10 * $Revision: 1.10 $
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 #ifndef GCC
34 #endif
36 //_________________________________________________________________________________________________________________
37 // includes
38 //_________________________________________________________________________________________________________________
40 #include <svtools/fontoptions.hxx>
41 #include <unotools/configmgr.hxx>
42 #include <unotools/configitem.hxx>
43 #include <tools/debug.hxx>
44 #include <com/sun/star/uno/Any.hxx>
45 #include <com/sun/star/uno/Sequence.hxx>
47 #include <rtl/logfile.hxx>
48 #include "itemholder1.hxx"
50 //_________________________________________________________________________________________________________________
51 // namespaces
52 //_________________________________________________________________________________________________________________
54 using namespace ::utl ;
55 using namespace ::rtl ;
56 using namespace ::osl ;
57 using namespace ::com::sun::star::uno ;
59 //_________________________________________________________________________________________________________________
60 // const
61 //_________________________________________________________________________________________________________________
63 #define ROOTNODE_FONT OUString(RTL_CONSTASCII_USTRINGPARAM("Office.Common/Font" ))
65 #define PROPERTYNAME_REPLACEMENTTABLE OUString(RTL_CONSTASCII_USTRINGPARAM("Substitution/Replacement" ))
66 #define PROPERTYNAME_FONTHISTORY OUString(RTL_CONSTASCII_USTRINGPARAM("View/History" ))
67 #define PROPERTYNAME_FONTWYSIWYG OUString(RTL_CONSTASCII_USTRINGPARAM("View/ShowFontBoxWYSIWYG" ))
69 #define PROPERTYHANDLE_REPLACEMENTTABLE 0
70 #define PROPERTYHANDLE_FONTHISTORY 1
71 #define PROPERTYHANDLE_FONTWYSIWYG 2
73 #define PROPERTYCOUNT 3
75 //_________________________________________________________________________________________________________________
76 // private declarations!
77 //_________________________________________________________________________________________________________________
79 class SvtFontOptions_Impl : public ConfigItem
81 //-------------------------------------------------------------------------------------------------------------
82 // public methods
83 //-------------------------------------------------------------------------------------------------------------
85 public:
87 //---------------------------------------------------------------------------------------------------------
88 // constructor / destructor
89 //---------------------------------------------------------------------------------------------------------
91 SvtFontOptions_Impl();
92 ~SvtFontOptions_Impl();
94 //---------------------------------------------------------------------------------------------------------
95 // overloaded methods of baseclass
96 //---------------------------------------------------------------------------------------------------------
98 /*-****************************************************************************************************//**
99 @short called for notify of configmanager
100 @descr These method is called from the ConfigManager before application ends or from the
101 PropertyChangeListener if the sub tree broadcasts changes. You must update your
102 internal values.
104 @seealso baseclass ConfigItem
106 @param "seqPropertyNames" is the list of properties which should be updated.
107 @return -
109 @onerror -
110 *//*-*****************************************************************************************************/
112 virtual void Notify( const Sequence< OUString >& seqPropertyNames );
114 /*-****************************************************************************************************//**
115 @short write changes to configuration
116 @descr These method writes the changed values into the sub tree
117 and should always called in our destructor to guarantee consistency of config data.
119 @seealso baseclass ConfigItem
121 @param -
122 @return -
124 @onerror -
125 *//*-*****************************************************************************************************/
127 virtual void Commit();
129 //---------------------------------------------------------------------------------------------------------
130 // public interface
131 //---------------------------------------------------------------------------------------------------------
133 /*-****************************************************************************************************//**
134 @short access method to get internal values
135 @descr These method give us a chance to regulate acces to ouer internal values.
136 It's not used in the moment - but it's possible for the feature!
138 @seealso -
140 @param -
141 @return -
143 @onerror -
144 *//*-*****************************************************************************************************/
146 sal_Bool IsReplacementTableEnabled ( ) const ;
147 void EnableReplacementTable ( sal_Bool bState ) ;
149 sal_Bool IsFontHistoryEnabled ( ) const ;
150 void EnableFontHistory ( sal_Bool bState ) ;
152 sal_Bool IsFontWYSIWYGEnabled ( ) const ;
153 void EnableFontWYSIWYG ( sal_Bool bState ) ;
155 //-------------------------------------------------------------------------------------------------------------
156 // private methods
157 //-------------------------------------------------------------------------------------------------------------
159 private:
161 /*-****************************************************************************************************//**
162 @short return list of key names of ouer configuration management which represent oue module tree
163 @descr These methods return a static const list of key names. We need it to get needed values from our
164 configuration management.
166 @seealso -
168 @param -
169 @return A list of needed configuration keys is returned.
171 @onerror -
172 *//*-*****************************************************************************************************/
174 static Sequence< OUString > impl_GetPropertyNames();
176 //-------------------------------------------------------------------------------------------------------------
177 // private member
178 //-------------------------------------------------------------------------------------------------------------
180 private:
182 sal_Bool m_bReplacementTable ;
183 sal_Bool m_bFontHistory ;
184 sal_Bool m_bFontWYSIWYG ;
187 //_________________________________________________________________________________________________________________
188 // definitions
189 //_________________________________________________________________________________________________________________
191 //*****************************************************************************************************************
192 // constructor
193 //*****************************************************************************************************************
194 SvtFontOptions_Impl::SvtFontOptions_Impl()
195 // Init baseclasses first
196 : ConfigItem ( ROOTNODE_FONT )
197 // Init member then.
198 , m_bReplacementTable ( sal_False )
199 , m_bFontHistory ( sal_False )
200 , m_bFontWYSIWYG ( sal_False )
202 // Use our static list of configuration keys to get his values.
203 Sequence< OUString > seqNames = impl_GetPropertyNames ( );
204 Sequence< Any > seqValues = GetProperties ( seqNames );
206 // Safe impossible cases.
207 // We need values from ALL configuration keys.
208 // Follow assignment use order of values in relation to our list of key names!
209 DBG_ASSERT( !(seqNames.getLength()!=seqValues.getLength()), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nI miss some values of configuration keys!\n" );
211 // Copy values from list in right order to ouer internal member.
212 sal_Int32 nPropertyCount = seqValues.getLength();
213 for( sal_Int32 nProperty=0; nProperty<nPropertyCount; ++nProperty )
215 // Safe impossible cases.
216 // Check any for valid value.
217 DBG_ASSERT( !(seqValues[nProperty].hasValue()==sal_False), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nInvalid property value detected!\n" );
218 switch( nProperty )
220 case PROPERTYHANDLE_REPLACEMENTTABLE : {
221 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nWho has changed the value type of \"Office.Common\\Font\\Substitution\\Replacement\"?" );
222 seqValues[nProperty] >>= m_bReplacementTable;
224 break;
225 case PROPERTYHANDLE_FONTHISTORY : {
226 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nWho has changed the value type of \"Office.Common\\Font\\View\\History\"?" );
227 seqValues[nProperty] >>= m_bFontHistory;
229 break;
230 case PROPERTYHANDLE_FONTWYSIWYG : {
231 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nWho has changed the value type of \"Office.Common\\Font\\View\\ShowFontBoxWYSIWYG\"?" );
232 seqValues[nProperty] >>= m_bFontWYSIWYG;
234 break;
238 // Enable notification mechanism of ouer baseclass.
239 // We need it to get information about changes outside these class on ouer used configuration keys!
240 EnableNotification( seqNames );
243 //*****************************************************************************************************************
244 // destructor
245 //*****************************************************************************************************************
246 SvtFontOptions_Impl::~SvtFontOptions_Impl()
248 // We must save our current values .. if user forget it!
249 if( IsModified() == sal_True )
251 Commit();
255 //*****************************************************************************************************************
256 // public method
257 //*****************************************************************************************************************
258 void SvtFontOptions_Impl::Notify( const Sequence< OUString >& seqPropertyNames )
260 // Use given list of updated properties to get his values from configuration directly!
261 Sequence< Any > seqValues = GetProperties( seqPropertyNames );
262 // Safe impossible cases.
263 // We need values from ALL notified configuration keys.
264 DBG_ASSERT( !(seqPropertyNames.getLength()!=seqValues.getLength()), "SvtFontOptions_Impl::Notify()\nI miss some values of configuration keys!\n" );
265 // Step over list of property names and get right value from coreesponding value list to set it on internal members!
266 sal_Int32 nCount = seqPropertyNames.getLength();
267 for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
269 if( seqPropertyNames[nProperty] == PROPERTYNAME_REPLACEMENTTABLE )
271 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\Font\\Substitution\\Replacement\"?" );
272 seqValues[nProperty] >>= m_bReplacementTable;
274 else
275 if( seqPropertyNames[nProperty] == PROPERTYNAME_FONTHISTORY )
277 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\Font\\View\\History\"?" );
278 seqValues[nProperty] >>= m_bFontHistory;
280 else
281 if( seqPropertyNames[nProperty] == PROPERTYNAME_FONTWYSIWYG )
283 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\Font\\View\\ShowFontBoxWYSIWYG\"?" );
284 seqValues[nProperty] >>= m_bFontWYSIWYG;
286 #if OSL_DEBUG_LEVEL > 1
287 else DBG_ASSERT( sal_False, "SvtFontOptions_Impl::Notify()\nUnkown property detected ... I can't handle these!\n" );
288 #endif
292 //*****************************************************************************************************************
293 // public method
294 //*****************************************************************************************************************
295 void SvtFontOptions_Impl::Commit()
297 // Get names of supported properties, create a list for values and copy current values to it.
298 Sequence< OUString > seqNames = impl_GetPropertyNames();
299 sal_Int32 nCount = seqNames.getLength();
300 Sequence< Any > seqValues ( nCount );
301 for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
303 switch( nProperty )
305 case PROPERTYHANDLE_REPLACEMENTTABLE : {
306 seqValues[nProperty] <<= m_bReplacementTable;
308 break;
309 case PROPERTYHANDLE_FONTHISTORY : {
310 seqValues[nProperty] <<= m_bFontHistory;
312 break;
313 case PROPERTYHANDLE_FONTWYSIWYG : {
314 seqValues[nProperty] <<= m_bFontWYSIWYG;
316 break;
319 // Set properties in configuration.
320 PutProperties( seqNames, seqValues );
323 //*****************************************************************************************************************
324 // public method
325 //*****************************************************************************************************************
326 sal_Bool SvtFontOptions_Impl::IsReplacementTableEnabled() const
328 return m_bReplacementTable;
331 //*****************************************************************************************************************
332 // public method
333 //*****************************************************************************************************************
334 void SvtFontOptions_Impl::EnableReplacementTable( sal_Bool bState )
336 m_bReplacementTable = bState;
337 SetModified();
340 //*****************************************************************************************************************
341 // public method
342 //*****************************************************************************************************************
343 sal_Bool SvtFontOptions_Impl::IsFontHistoryEnabled() const
345 return m_bFontHistory;
348 //*****************************************************************************************************************
349 // public method
350 //*****************************************************************************************************************
351 void SvtFontOptions_Impl::EnableFontHistory( sal_Bool bState )
353 m_bFontHistory = bState;
354 SetModified();
357 //*****************************************************************************************************************
358 // public method
359 //*****************************************************************************************************************
360 sal_Bool SvtFontOptions_Impl::IsFontWYSIWYGEnabled() const
362 return m_bFontWYSIWYG;
365 //*****************************************************************************************************************
366 // public method
367 //*****************************************************************************************************************
368 void SvtFontOptions_Impl::EnableFontWYSIWYG( sal_Bool bState )
370 m_bFontWYSIWYG = bState;
371 SetModified();
374 //*****************************************************************************************************************
375 // private method
376 //*****************************************************************************************************************
377 Sequence< OUString > SvtFontOptions_Impl::impl_GetPropertyNames()
379 // Build static list of configuration key names.
380 static const OUString pProperties[] =
382 PROPERTYNAME_REPLACEMENTTABLE ,
383 PROPERTYNAME_FONTHISTORY ,
384 PROPERTYNAME_FONTWYSIWYG ,
386 // Initialize return sequence with these list ...
387 static const Sequence< OUString > seqPropertyNames( pProperties, PROPERTYCOUNT );
388 // ... and return it.
389 return seqPropertyNames;
392 //*****************************************************************************************************************
393 // initialize static member
394 // DON'T DO IT IN YOUR HEADER!
395 // see definition for further informations
396 //*****************************************************************************************************************
397 SvtFontOptions_Impl* SvtFontOptions::m_pDataContainer = NULL ;
398 sal_Int32 SvtFontOptions::m_nRefCount = 0 ;
400 //*****************************************************************************************************************
401 // constructor
402 //*****************************************************************************************************************
403 SvtFontOptions::SvtFontOptions()
405 // Global access, must be guarded (multithreading!).
406 MutexGuard aGuard( impl_GetOwnStaticMutex() );
407 // Increase ouer refcount ...
408 ++m_nRefCount;
409 // ... and initialize ouer data container only if it not already exist!
410 if( m_pDataContainer == NULL )
412 RTL_LOGFILE_CONTEXT(aLog, "svtools ( ??? ) ::SvtFontOptions_Impl::ctor()");
413 m_pDataContainer = new SvtFontOptions_Impl;
415 ItemHolder1::holdConfigItem(E_FONTOPTIONS);
419 //*****************************************************************************************************************
420 // destructor
421 //*****************************************************************************************************************
422 SvtFontOptions::~SvtFontOptions()
424 // Global access, must be guarded (multithreading!)
425 MutexGuard aGuard( impl_GetOwnStaticMutex() );
426 // Decrease ouer refcount.
427 --m_nRefCount;
428 // If last instance was deleted ...
429 // we must destroy ouer static data container!
430 if( m_nRefCount <= 0 )
432 delete m_pDataContainer;
433 m_pDataContainer = NULL;
437 //*****************************************************************************************************************
438 // public method
439 //*****************************************************************************************************************
440 sal_Bool SvtFontOptions::IsReplacementTableEnabled() const
442 MutexGuard aGuard( impl_GetOwnStaticMutex() );
443 return m_pDataContainer->IsReplacementTableEnabled();
446 //*****************************************************************************************************************
447 // public method
448 //*****************************************************************************************************************
449 void SvtFontOptions::EnableReplacementTable( sal_Bool bState )
451 MutexGuard aGuard( impl_GetOwnStaticMutex() );
452 m_pDataContainer->EnableReplacementTable( bState );
455 //*****************************************************************************************************************
456 // public method
457 //*****************************************************************************************************************
458 sal_Bool SvtFontOptions::IsFontHistoryEnabled() const
460 MutexGuard aGuard( impl_GetOwnStaticMutex() );
461 return m_pDataContainer->IsFontHistoryEnabled();
464 //*****************************************************************************************************************
465 // public method
466 //*****************************************************************************************************************
467 void SvtFontOptions::EnableFontHistory( sal_Bool bState )
469 MutexGuard aGuard( impl_GetOwnStaticMutex() );
470 m_pDataContainer->EnableFontHistory( bState );
473 //*****************************************************************************************************************
474 // public method
475 //*****************************************************************************************************************
476 sal_Bool SvtFontOptions::IsFontWYSIWYGEnabled() const
478 MutexGuard aGuard( impl_GetOwnStaticMutex() );
479 return m_pDataContainer->IsFontWYSIWYGEnabled();
482 //*****************************************************************************************************************
483 // public method
484 //*****************************************************************************************************************
485 void SvtFontOptions::EnableFontWYSIWYG( sal_Bool bState )
487 MutexGuard aGuard( impl_GetOwnStaticMutex() );
488 m_pDataContainer->EnableFontWYSIWYG( bState );
491 //*****************************************************************************************************************
492 // private method
493 //*****************************************************************************************************************
494 Mutex& SvtFontOptions::impl_GetOwnStaticMutex()
496 // Initialize static mutex only for one time!
497 static Mutex* pMutex = NULL;
498 // If these method first called (Mutex not already exist!) ...
499 if( pMutex == NULL )
501 // ... we must create a new one. Protect follow code with the global mutex -
502 // It must be - we create a static variable!
503 MutexGuard aGuard( Mutex::getGlobalMutex() );
504 // We must check our pointer again - because it can be that another instance of ouer class will be fastr then these!
505 if( pMutex == NULL )
507 // Create the new mutex and set it for return on static variable.
508 static Mutex aMutex;
509 pMutex = &aMutex;
512 // Return new created or already existing mutex object.
513 return *pMutex;