Bump version to 5.0-14
[LibreOffice.git] / svtools / source / config / menuoptions.cxx
blob32d54b2f175c4a51d53f51507cd23e0e92df67ef
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 <svtools/menuoptions.hxx>
22 #include <unotools/configmgr.hxx>
23 #include <unotools/configitem.hxx>
24 #include <tools/debug.hxx>
25 #include <com/sun/star/uno/Any.hxx>
26 #include <com/sun/star/uno/Sequence.hxx>
27 #include <vcl/svapp.hxx>
28 #include <vcl/settings.hxx>
30 #include "itemholder2.hxx"
32 #include <list>
35 // namespaces
38 using namespace ::utl ;
39 using namespace ::osl ;
40 using namespace ::com::sun::star::uno ;
42 #define ROOTNODE_MENU OUString("Office.Common/View/Menu" )
43 #define DEFAULT_DONTHIDEDISABLEDENTRIES false
44 #define DEFAULT_FOLLOWMOUSE true
45 #define DEFAULT_MENUICONS TRISTATE_INDET
47 #define PROPERTYNAME_DONTHIDEDISABLEDENTRIES "DontHideDisabledEntry"
48 #define PROPERTYNAME_FOLLOWMOUSE "FollowMouse"
49 #define PROPERTYNAME_SHOWICONSINMENUES "ShowIconsInMenues"
50 #define PROPERTYNAME_SYSTEMICONSINMENUES "IsSystemIconsInMenus"
52 #define PROPERTYHANDLE_DONTHIDEDISABLEDENTRIES 0
53 #define PROPERTYHANDLE_FOLLOWMOUSE 1
54 #define PROPERTYHANDLE_SHOWICONSINMENUES 2
55 #define PROPERTYHANDLE_SYSTEMICONSINMENUES 3
57 #define PROPERTYCOUNT 4
59 #include <tools/link.hxx>
62 // private declarations!
65 class SvtMenuOptions_Impl : public ConfigItem
68 // private member
71 private:
72 ::std::list<Link<>> aList;
73 bool m_bDontHideDisabledEntries ; /// cache "DontHideDisabledEntries" of Menu section
74 bool m_bFollowMouse ; /// cache "FollowMouse" of Menu section
75 TriState m_eMenuIcons ; /// cache "MenuIcons" of Menu section
78 // public methods
81 public:
84 // constructor / destructor
87 SvtMenuOptions_Impl();
88 virtual ~SvtMenuOptions_Impl();
90 void AddListenerLink( const Link<>& rLink );
91 void RemoveListenerLink( const Link<>& rLink );
94 // override methods of baseclass
97 /*-****************************************************************************************************
98 @short called for notify of configmanager
99 @descr These method is called from the ConfigManager before application ends or from the
100 PropertyChangeListener if the sub tree broadcasts changes. You must update your
101 internal values.
103 @seealso baseclass ConfigItem
105 @param "seqPropertyNames" is the list of properties which should be updated.
106 *//*-*****************************************************************************************************/
108 virtual void Notify( const Sequence< OUString >& seqPropertyNames ) SAL_OVERRIDE;
110 // public interface
113 /*-****************************************************************************************************
114 @short access method to get internal values
115 @descr These methods give us a chance to regulate access to our internal values.
116 It's not used in the moment - but it's possible for the future!
117 *//*-*****************************************************************************************************/
119 bool IsEntryHidingEnabled() const
120 { return m_bDontHideDisabledEntries; }
122 TriState GetMenuIconsState() const
123 { return m_eMenuIcons; }
125 void SetMenuIconsState(TriState eState)
127 m_eMenuIcons = eState;
128 SetModified();
129 for ( ::std::list<Link<>>::const_iterator iter = aList.begin(); iter != aList.end(); ++iter )
130 iter->Call( this );
131 // tdf#93451: don't Commit() here, it's too early
135 // private methods
138 private:
140 virtual void ImplCommit() SAL_OVERRIDE;
142 /*-****************************************************************************************************
143 @short return list of fix key names of our configuration management which represent our module tree
144 @descr These methods return a static const list of key names. We need it to get needed values from our
145 configuration management.
146 @return A list of needed configuration keys is returned.
147 *//*-*****************************************************************************************************/
149 static Sequence< OUString > impl_GetPropertyNames();
153 // constructor
155 SvtMenuOptions_Impl::SvtMenuOptions_Impl()
156 // Init baseclasses first
157 : ConfigItem ( ROOTNODE_MENU )
158 // Init member then.
159 , m_bDontHideDisabledEntries ( DEFAULT_DONTHIDEDISABLEDENTRIES )
160 , m_bFollowMouse ( DEFAULT_FOLLOWMOUSE )
161 , m_eMenuIcons ( DEFAULT_MENUICONS )
163 // Use our static list of configuration keys to get his values.
164 Sequence< OUString > seqNames = impl_GetPropertyNames();
165 Sequence< Any > seqValues = GetProperties( seqNames ) ;
167 // Safe impossible cases.
168 // We need values from ALL configuration keys.
169 // Follow assignment use order of values in relation to our list of key names!
170 DBG_ASSERT( !(seqNames.getLength()!=seqValues.getLength()), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nI miss some values of configuration keys!\n" );
172 bool bMenuIcons = true;
173 bool bSystemMenuIcons = true;
174 if (m_eMenuIcons == TRISTATE_INDET)
175 bMenuIcons = Application::GetSettings().GetStyleSettings().GetPreferredUseImagesInMenus();
176 else
178 bSystemMenuIcons = false;
179 bMenuIcons = m_eMenuIcons != TRISTATE_FALSE;
182 // Copy values from list in right order to our internal member.
183 sal_Int32 nPropertyCount = seqValues.getLength() ;
184 sal_Int32 nProperty = 0 ;
185 for( nProperty=0; nProperty<nPropertyCount; ++nProperty )
187 // Safe impossible cases.
188 // Check any for valid value.
189 DBG_ASSERT( seqValues[nProperty].hasValue(), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nInvalid property value for property detected!\n" );
191 if (!seqValues[nProperty].hasValue())
192 continue;
194 switch( nProperty )
196 case PROPERTYHANDLE_DONTHIDEDISABLEDENTRIES : {
197 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\DontHideDisabledEntry\"?" );
198 seqValues[nProperty] >>= m_bDontHideDisabledEntries;
200 break;
202 case PROPERTYHANDLE_FOLLOWMOUSE : {
203 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\FollowMouse\"?" );
204 seqValues[nProperty] >>= m_bFollowMouse;
206 break;
207 case PROPERTYHANDLE_SHOWICONSINMENUES : {
208 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\ShowIconsInMenues\"?" );
209 seqValues[nProperty] >>= bMenuIcons;
211 break;
212 case PROPERTYHANDLE_SYSTEMICONSINMENUES : {
213 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\IsSystemIconsInMenus\"?" );
214 seqValues[nProperty] >>= bSystemMenuIcons;
216 break;
220 m_eMenuIcons = bSystemMenuIcons ? TRISTATE_INDET : static_cast<TriState>(bMenuIcons);
222 EnableNotification( seqNames );
226 // destructor
228 SvtMenuOptions_Impl::~SvtMenuOptions_Impl()
230 assert(!IsModified()); // should have been committed
234 // public method
236 void SvtMenuOptions_Impl::Notify( const Sequence< OUString >& seqPropertyNames )
238 // Use given list of updated properties to get his values from configuration directly!
239 Sequence< Any > seqValues = GetProperties( seqPropertyNames );
240 // Safe impossible cases.
241 // We need values from ALL notified configuration keys.
242 DBG_ASSERT( !(seqPropertyNames.getLength()!=seqValues.getLength()), "SvtMenuOptions_Impl::Notify()\nI miss some values of configuration keys!\n" );
244 bool bMenuSettingsChanged = false;
245 bool bMenuIcons = true;
246 bool bSystemMenuIcons = true;
247 if (m_eMenuIcons == TRISTATE_INDET)
248 bMenuIcons = Application::GetSettings().GetStyleSettings().GetUseImagesInMenus();
249 else
251 bSystemMenuIcons = false;
252 bMenuIcons = m_eMenuIcons != TRISTATE_FALSE;
255 // Step over list of property names and get right value from coreesponding value list to set it on internal members!
256 sal_Int32 nCount = seqPropertyNames.getLength();
257 for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
259 if( seqPropertyNames[nProperty] == PROPERTYNAME_DONTHIDEDISABLEDENTRIES )
261 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\View\\Menu\\DontHideDisabledEntry\"?" );
262 seqValues[nProperty] >>= m_bDontHideDisabledEntries;
264 else if( seqPropertyNames[nProperty] == PROPERTYNAME_FOLLOWMOUSE )
266 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\View\\Menu\\FollowMouse\"?" );
267 seqValues[nProperty] >>= m_bFollowMouse;
269 else if( seqPropertyNames[nProperty] == PROPERTYNAME_SHOWICONSINMENUES )
271 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\ShowIconsInMenues\"?" );
272 bMenuSettingsChanged |= seqValues[nProperty] >>= bMenuIcons;
274 else if( seqPropertyNames[nProperty] == PROPERTYNAME_SYSTEMICONSINMENUES )
276 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\IsSystemIconsInMenus\"?" );
277 bMenuSettingsChanged |= seqValues[nProperty] >>= bSystemMenuIcons;
280 #if OSL_DEBUG_LEVEL > 1
281 else DBG_ASSERT( sal_False, "SvtMenuOptions_Impl::Notify()\nUnknown property detected ... I can't handle these!\n" );
282 #endif
285 if ( bMenuSettingsChanged )
286 m_eMenuIcons = bSystemMenuIcons ? TRISTATE_INDET : static_cast<TriState>(bMenuIcons);
288 for ( ::std::list<Link<>>::const_iterator iter = aList.begin(); iter != aList.end(); ++iter )
289 iter->Call( this );
293 // public method
295 void SvtMenuOptions_Impl::ImplCommit()
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_DONTHIDEDISABLEDENTRIES : {
306 seqValues[nProperty] <<= m_bDontHideDisabledEntries;
308 break;
310 case PROPERTYHANDLE_FOLLOWMOUSE : {
311 seqValues[nProperty] <<= m_bFollowMouse;
313 break;
314 //Output cache of current setting as possibly modified by System Theme for older version
315 case PROPERTYHANDLE_SHOWICONSINMENUES : {
316 bool bValue = Application::GetSettings().GetStyleSettings().GetUseImagesInMenus();
317 seqValues[nProperty] <<= bValue;
319 break;
320 case PROPERTYHANDLE_SYSTEMICONSINMENUES : {
321 bool bValue = m_eMenuIcons == TRISTATE_INDET;
322 seqValues[nProperty] <<= bValue;
324 break;
327 // Set properties in configuration.
328 PutProperties( seqNames, seqValues );
332 // private method
334 Sequence< OUString > SvtMenuOptions_Impl::impl_GetPropertyNames()
336 // Build static list of configuration key names.
337 static const OUString pProperties[] =
339 OUString(PROPERTYNAME_DONTHIDEDISABLEDENTRIES) ,
340 OUString(PROPERTYNAME_FOLLOWMOUSE) ,
341 OUString(PROPERTYNAME_SHOWICONSINMENUES) ,
342 OUString(PROPERTYNAME_SYSTEMICONSINMENUES)
344 // Initialize return sequence with these list ...
345 static const Sequence< OUString > seqPropertyNames( pProperties, PROPERTYCOUNT );
346 // ... and return it.
347 return seqPropertyNames;
350 void SvtMenuOptions_Impl::AddListenerLink( const Link<>& rLink )
352 aList.push_back( rLink );
355 void SvtMenuOptions_Impl::RemoveListenerLink( const Link<>& rLink )
357 for ( ::std::list<Link<>>::iterator iter = aList.begin(); iter != aList.end(); ++iter )
359 if ( *iter == rLink )
361 aList.erase(iter);
362 break;
368 // initialize static member
369 // DON'T DO IT IN YOUR HEADER!
370 // see definition for further information
372 SvtMenuOptions_Impl* SvtMenuOptions::m_pDataContainer = NULL ;
373 sal_Int32 SvtMenuOptions::m_nRefCount = 0 ;
376 // constructor
378 SvtMenuOptions::SvtMenuOptions()
380 // Global access, must be guarded (multithreading!).
381 MutexGuard aGuard( GetOwnStaticMutex() );
382 // Increase our refcount ...
383 ++m_nRefCount;
384 // ... and initialize our data container only if it not already!
385 if( m_pDataContainer == NULL )
387 m_pDataContainer = new SvtMenuOptions_Impl();
389 svtools::ItemHolder2::holdConfigItem(E_MENUOPTIONS);
394 // destructor
396 SvtMenuOptions::~SvtMenuOptions()
398 // Global access, must be guarded (multithreading!)
399 MutexGuard aGuard( GetOwnStaticMutex() );
400 // Decrease our refcount.
401 --m_nRefCount;
402 // If last instance was deleted ...
403 // we must destroy our static data container!
404 if( m_nRefCount <= 0 )
406 delete m_pDataContainer;
407 m_pDataContainer = NULL;
412 // public method
414 bool SvtMenuOptions::IsEntryHidingEnabled() const
416 MutexGuard aGuard( GetOwnStaticMutex() );
417 return m_pDataContainer->IsEntryHidingEnabled();
421 // public method
423 TriState SvtMenuOptions::GetMenuIconsState() const
425 MutexGuard aGuard( GetOwnStaticMutex() );
426 return m_pDataContainer->GetMenuIconsState();
430 // public method
432 void SvtMenuOptions::SetMenuIconsState(TriState eState)
434 MutexGuard aGuard( GetOwnStaticMutex() );
435 m_pDataContainer->SetMenuIconsState(eState);
439 // private method
441 Mutex& SvtMenuOptions::GetOwnStaticMutex()
443 // Initialize static mutex only for one time!
444 static Mutex* pMutex = NULL;
445 // If these method first called (Mutex not already exist!) ...
446 if( pMutex == NULL )
448 // ... we must create a new one. Protect follow code with the global mutex -
449 // It must be - we create a static variable!
450 MutexGuard aGuard( Mutex::getGlobalMutex() );
451 // We must check our pointer again - because it can be that another instance of our class will be faster than these!
452 if( pMutex == NULL )
454 // Create the new mutex and set it for return on static variable.
455 static Mutex aMutex;
456 pMutex = &aMutex;
459 // Return new created or already existing mutex object.
460 return *pMutex;
463 void SvtMenuOptions::AddListenerLink( const Link<>& rLink )
465 m_pDataContainer->AddListenerLink( rLink );
468 void SvtMenuOptions::RemoveListenerLink( const Link<>& rLink )
470 m_pDataContainer->RemoveListenerLink( rLink );
473 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */