Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / unotools / source / config / fontoptions.cxx
blobc9fa6ec48f9481bbe7ffafd9828e14ad80c41a83
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 .
20 #include <unotools/fontoptions.hxx>
21 #include <unotools/configitem.hxx>
22 #include <tools/debug.hxx>
23 #include <com/sun/star/uno/Any.hxx>
24 #include <com/sun/star/uno/Sequence.hxx>
26 #include <rtl/instance.hxx>
27 #include "itemholder1.hxx"
29 using namespace ::utl;
30 using namespace ::osl;
31 using namespace ::com::sun::star::uno;
33 #define ROOTNODE_FONT "Office.Common/Font"
35 #define PROPERTYNAME_REPLACEMENTTABLE "Substitution/Replacement"
36 #define PROPERTYNAME_FONTHISTORY "View/History"
37 #define PROPERTYNAME_FONTWYSIWYG "View/ShowFontBoxWYSIWYG"
39 #define PROPERTYHANDLE_REPLACEMENTTABLE 0
40 #define PROPERTYHANDLE_FONTHISTORY 1
41 #define PROPERTYHANDLE_FONTWYSIWYG 2
43 class SvtFontOptions_Impl : public ConfigItem
45 public:
47 SvtFontOptions_Impl();
48 virtual ~SvtFontOptions_Impl() override;
50 /*-****************************************************************************************************
51 @short called for notify of configmanager
52 @descr This method is called from the ConfigManager before the application ends or from the
53 PropertyChangeListener if the sub tree broadcasts changes. You must update your
54 internal values.
56 @seealso baseclass ConfigItem
58 @param "seqPropertyNames" is the list of properties which should be updated.
59 *//*-*****************************************************************************************************/
61 virtual void Notify( const Sequence< OUString >& seqPropertyNames ) override;
63 /*-****************************************************************************************************
64 @short access method to get internal values
65 @descr These method give us a chance to regulate access to our internal values.
66 It's not used in the moment - but it's possible for the feature!
67 *//*-*****************************************************************************************************/
69 bool IsFontHistoryEnabled ( ) const { return m_bFontHistory;}
71 bool IsFontWYSIWYGEnabled ( ) const { return m_bFontWYSIWYG;}
72 void EnableFontWYSIWYG ( bool bState );
74 private:
76 virtual void ImplCommit() override;
78 /*-****************************************************************************************************
79 @short return list of key names of our configuration management which represent our module tree
80 @descr This method returns a static const list of key names. We need it to get needed values from our
81 configuration management.
82 @return A list of needed configuration keys is returned.
83 *//*-*****************************************************************************************************/
85 static Sequence< OUString > impl_GetPropertyNames();
87 private:
89 bool m_bReplacementTable;
90 bool m_bFontHistory;
91 bool m_bFontWYSIWYG;
94 // constructor
96 SvtFontOptions_Impl::SvtFontOptions_Impl()
97 // Init baseclasses first
98 : ConfigItem ( ROOTNODE_FONT )
99 // Init member then.
100 , m_bReplacementTable ( false )
101 , m_bFontHistory ( false )
102 , m_bFontWYSIWYG ( false )
104 // Use our static list of configuration keys to get his values.
105 Sequence< OUString > seqNames = impl_GetPropertyNames ( );
106 Sequence< Any > seqValues = GetProperties ( seqNames );
108 // Safe impossible cases.
109 // We need values from ALL configuration keys.
110 // Follow assignment use order of values in relation to our list of key names!
111 DBG_ASSERT( !(seqNames.getLength()!=seqValues.getLength()), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nI miss some values of configuration keys!\n" );
113 // Copy values from list in right order to our internal member.
114 sal_Int32 nPropertyCount = seqValues.getLength();
115 for( sal_Int32 nProperty=0; nProperty<nPropertyCount; ++nProperty )
117 // Safe impossible cases.
118 // Check any for valid value.
119 DBG_ASSERT( seqValues[nProperty].hasValue(), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nInvalid property value detected!\n" );
120 switch( nProperty )
122 case PROPERTYHANDLE_REPLACEMENTTABLE : {
123 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nWho has changed the value type of \"Office.Common\\Font\\Substitution\\Replacement\"?" );
124 seqValues[nProperty] >>= m_bReplacementTable;
126 break;
127 case PROPERTYHANDLE_FONTHISTORY : {
128 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nWho has changed the value type of \"Office.Common\\Font\\View\\History\"?" );
129 seqValues[nProperty] >>= m_bFontHistory;
131 break;
132 case PROPERTYHANDLE_FONTWYSIWYG : {
133 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nWho has changed the value type of \"Office.Common\\Font\\View\\ShowFontBoxWYSIWYG\"?" );
134 seqValues[nProperty] >>= m_bFontWYSIWYG;
136 break;
140 // Enable notification mechanism of our baseclass.
141 // We need it to get information about changes outside these class on our used configuration keys!
142 EnableNotification( seqNames );
145 // destructor
147 SvtFontOptions_Impl::~SvtFontOptions_Impl()
149 assert(!IsModified()); // should have been committed
152 // public method
154 void SvtFontOptions_Impl::Notify( const Sequence< OUString >& seqPropertyNames )
156 // Use given list of updated properties to get his values from configuration directly!
157 Sequence< Any > seqValues = GetProperties( seqPropertyNames );
158 // Safe impossible cases.
159 // We need values from ALL notified configuration keys.
160 DBG_ASSERT( !(seqPropertyNames.getLength()!=seqValues.getLength()), "SvtFontOptions_Impl::Notify()\nI miss some values of configuration keys!\n" );
161 // Step over list of property names and get right value from coreesponding value list to set it on internal members!
162 sal_Int32 nCount = seqPropertyNames.getLength();
163 for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
165 if( seqPropertyNames[nProperty] == PROPERTYNAME_REPLACEMENTTABLE )
167 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\Font\\Substitution\\Replacement\"?" );
168 seqValues[nProperty] >>= m_bReplacementTable;
170 else
171 if( seqPropertyNames[nProperty] == PROPERTYNAME_FONTHISTORY )
173 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\Font\\View\\History\"?" );
174 seqValues[nProperty] >>= m_bFontHistory;
176 else
177 if( seqPropertyNames[nProperty] == PROPERTYNAME_FONTWYSIWYG )
179 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\Font\\View\\ShowFontBoxWYSIWYG\"?" );
180 seqValues[nProperty] >>= m_bFontWYSIWYG;
182 #if OSL_DEBUG_LEVEL > 0
183 else assert(false && "Unknown property detected ... I can't handle these!");
184 #endif
188 // public method
190 void SvtFontOptions_Impl::ImplCommit()
192 // Get names of supported properties, create a list for values and copy current values to it.
193 Sequence< OUString > seqNames = impl_GetPropertyNames();
194 sal_Int32 nCount = seqNames.getLength();
195 Sequence< Any > seqValues ( nCount );
196 for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
198 switch( nProperty )
200 case PROPERTYHANDLE_REPLACEMENTTABLE : {
201 seqValues[nProperty] <<= m_bReplacementTable;
203 break;
204 case PROPERTYHANDLE_FONTHISTORY : {
205 seqValues[nProperty] <<= m_bFontHistory;
207 break;
208 case PROPERTYHANDLE_FONTWYSIWYG : {
209 seqValues[nProperty] <<= m_bFontWYSIWYG;
211 break;
214 // Set properties in configuration.
215 PutProperties( seqNames, seqValues );
218 // public method
220 void SvtFontOptions_Impl::EnableFontWYSIWYG( bool bState )
222 m_bFontWYSIWYG = bState;
223 SetModified();
226 // private method
228 Sequence< OUString > SvtFontOptions_Impl::impl_GetPropertyNames()
230 return Sequence< OUString >
232 PROPERTYNAME_REPLACEMENTTABLE ,
233 PROPERTYNAME_FONTHISTORY ,
234 PROPERTYNAME_FONTWYSIWYG ,
238 namespace {
240 std::weak_ptr<SvtFontOptions_Impl> g_pFontOptions;
244 SvtFontOptions::SvtFontOptions()
246 // Global access, must be guarded (multithreading!).
247 MutexGuard aGuard( impl_GetOwnStaticMutex() );
249 m_pImpl = g_pFontOptions.lock();
250 if( !m_pImpl )
252 m_pImpl = std::make_shared<SvtFontOptions_Impl>();
253 g_pFontOptions = m_pImpl;
254 ItemHolder1::holdConfigItem(EItem::FontOptions);
258 SvtFontOptions::~SvtFontOptions()
260 // Global access, must be guarded (multithreading!)
261 MutexGuard aGuard( impl_GetOwnStaticMutex() );
263 m_pImpl.reset();
266 // public method
268 bool SvtFontOptions::IsFontHistoryEnabled() const
270 MutexGuard aGuard( impl_GetOwnStaticMutex() );
271 return m_pImpl->IsFontHistoryEnabled();
274 // public method
276 bool SvtFontOptions::IsFontWYSIWYGEnabled() const
278 MutexGuard aGuard( impl_GetOwnStaticMutex() );
279 return m_pImpl->IsFontWYSIWYGEnabled();
282 // public method
284 void SvtFontOptions::EnableFontWYSIWYG( bool bState )
286 MutexGuard aGuard( impl_GetOwnStaticMutex() );
287 m_pImpl->EnableFontWYSIWYG( bState );
290 namespace
292 class theFontOptionsMutex : public rtl::Static<osl::Mutex, theFontOptionsMutex> {};
295 // private method
297 Mutex& SvtFontOptions::impl_GetOwnStaticMutex()
299 return theFontOptionsMutex::get();
302 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */