GPU-Calc: remove Alloc_Host_Ptr for clmem of NAN vector
[LibreOffice.git] / svtools / source / config / menuoptions.cxx
blob1a320b18e4ab268acf9a7a4eda6253c473abff73
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>
29 #include "itemholder2.hxx"
31 #include <list>
33 //_________________________________________________________________________________________________________________
34 // namespaces
35 //_________________________________________________________________________________________________________________
37 using namespace ::utl ;
38 using namespace ::rtl ;
39 using namespace ::osl ;
40 using namespace ::com::sun::star::uno ;
42 #define ROOTNODE_MENU OUString("Office.Common/View/Menu" )
43 #define DEFAULT_DONTHIDEDISABLEDENTRIES sal_False
44 #define DEFAULT_FOLLOWMOUSE sal_True
45 #define DEFAULT_MENUICONS 2
47 #define PROPERTYNAME_DONTHIDEDISABLEDENTRIES OUString("DontHideDisabledEntry" )
48 #define PROPERTYNAME_FOLLOWMOUSE OUString("FollowMouse" )
49 #define PROPERTYNAME_SHOWICONSINMENUES OUString("ShowIconsInMenues" )
50 #define PROPERTYNAME_SYSTEMICONSINMENUES OUString("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>
61 //_________________________________________________________________________________________________________________
62 // private declarations!
63 //_________________________________________________________________________________________________________________
65 class SvtMenuOptions_Impl : public ConfigItem
67 //-------------------------------------------------------------------------------------------------------------
68 // private member
69 //-------------------------------------------------------------------------------------------------------------
71 private:
72 ::std::list<Link> aList;
73 sal_Bool m_bDontHideDisabledEntries ; /// cache "DontHideDisabledEntries" of Menu section
74 sal_Bool m_bFollowMouse ; /// cache "FollowMouse" of Menu section
75 sal_Int16 m_nMenuIcons ; /// cache "MenuIcons" of Menu section
77 //-------------------------------------------------------------------------------------------------------------
78 // public methods
79 //-------------------------------------------------------------------------------------------------------------
81 public:
83 //---------------------------------------------------------------------------------------------------------
84 // constructor / destructor
85 //---------------------------------------------------------------------------------------------------------
87 SvtMenuOptions_Impl();
88 ~SvtMenuOptions_Impl();
90 void AddListenerLink( const Link& rLink );
91 void RemoveListenerLink( const Link& rLink );
93 //---------------------------------------------------------------------------------------------------------
94 // overloaded methods of baseclass
95 //---------------------------------------------------------------------------------------------------------
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 @return -
108 @onerror -
109 *//*-*****************************************************************************************************/
111 virtual void Notify( const Sequence< OUString >& seqPropertyNames );
113 /*-****************************************************************************************************//**
114 @short write changes to configuration
115 @descr These method writes the changed values into the sub tree
116 and should always called in our destructor to guarantee consistency of config data.
118 @seealso baseclass ConfigItem
120 @param -
121 @return -
123 @onerror -
124 *//*-*****************************************************************************************************/
126 virtual void Commit();
128 //---------------------------------------------------------------------------------------------------------
129 // public interface
130 //---------------------------------------------------------------------------------------------------------
132 /*-****************************************************************************************************//**
133 @short access method to get internal values
134 @descr These methods give us a chance to regulate access to our internal values.
135 It's not used in the moment - but it's possible for the future!
137 @seealso -
139 @param -
140 @return -
142 @onerror -
143 *//*-*****************************************************************************************************/
145 sal_Bool IsEntryHidingEnabled() const
146 { return m_bDontHideDisabledEntries; }
148 sal_Bool IsFollowMouseEnabled() const
149 { return m_bFollowMouse; }
151 sal_Int16 GetMenuIconsState() const
152 { return m_nMenuIcons; }
154 void SetEntryHidingState ( sal_Bool bState )
156 m_bDontHideDisabledEntries = bState;
157 SetModified();
158 for ( ::std::list<Link>::const_iterator iter = aList.begin(); iter != aList.end(); ++iter )
159 iter->Call( this );
160 Commit();
163 void SetFollowMouseState ( sal_Bool bState )
165 m_bFollowMouse = bState;
166 SetModified();
167 for ( ::std::list<Link>::const_iterator iter = aList.begin(); iter != aList.end(); ++iter )
168 iter->Call( this );
169 Commit();
172 void SetMenuIconsState ( sal_Int16 nState )
174 m_nMenuIcons = nState;
175 SetModified();
176 for ( ::std::list<Link>::const_iterator iter = aList.begin(); iter != aList.end(); ++iter )
177 iter->Call( this );
178 Commit();
181 //-------------------------------------------------------------------------------------------------------------
182 // private methods
183 //-------------------------------------------------------------------------------------------------------------
185 private:
187 /*-****************************************************************************************************//**
188 @short return list of fix key names of our configuration management which represent our module tree
189 @descr These methods return a static const list of key names. We need it to get needed values from our
190 configuration management.
192 @seealso -
194 @param -
195 @return A list of needed configuration keys is returned.
197 @onerror -
198 *//*-*****************************************************************************************************/
200 static Sequence< OUString > impl_GetPropertyNames();
203 //*****************************************************************************************************************
204 // constructor
205 //*****************************************************************************************************************
206 SvtMenuOptions_Impl::SvtMenuOptions_Impl()
207 // Init baseclasses first
208 : ConfigItem ( ROOTNODE_MENU )
209 // Init member then.
210 , m_bDontHideDisabledEntries ( DEFAULT_DONTHIDEDISABLEDENTRIES )
211 , m_bFollowMouse ( DEFAULT_FOLLOWMOUSE )
212 , m_nMenuIcons ( DEFAULT_MENUICONS )
214 // Use our static list of configuration keys to get his values.
215 Sequence< OUString > seqNames = impl_GetPropertyNames();
216 Sequence< Any > seqValues = GetProperties( seqNames ) ;
218 // Safe impossible cases.
219 // We need values from ALL configuration keys.
220 // Follow assignment use order of values in relation to our list of key names!
221 DBG_ASSERT( !(seqNames.getLength()!=seqValues.getLength()), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nI miss some values of configuration keys!\n" );
223 sal_Bool bMenuIcons = sal_True;
224 sal_Bool bSystemMenuIcons = sal_True;
225 if (m_nMenuIcons == 2)
226 bMenuIcons = (sal_Bool)(Application::GetSettings().GetStyleSettings().GetPreferredUseImagesInMenus());
227 else
229 bSystemMenuIcons = sal_False;
230 bMenuIcons = m_nMenuIcons ? sal_True : sal_False;
233 // Copy values from list in right order to our internal member.
234 sal_Int32 nPropertyCount = seqValues.getLength() ;
235 sal_Int32 nProperty = 0 ;
236 for( nProperty=0; nProperty<nPropertyCount; ++nProperty )
238 // Safe impossible cases.
239 // Check any for valid value.
240 DBG_ASSERT( !(seqValues[nProperty].hasValue()==sal_False), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nInvalid property value for property detected!\n" );
242 if (!seqValues[nProperty].hasValue())
243 continue;
245 switch( nProperty )
247 case PROPERTYHANDLE_DONTHIDEDISABLEDENTRIES : {
248 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\DontHideDisabledEntry\"?" );
249 seqValues[nProperty] >>= m_bDontHideDisabledEntries;
251 break;
253 case PROPERTYHANDLE_FOLLOWMOUSE : {
254 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\FollowMouse\"?" );
255 seqValues[nProperty] >>= m_bFollowMouse;
257 break;
258 case PROPERTYHANDLE_SHOWICONSINMENUES : {
259 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\ShowIconsInMenues\"?" );
260 seqValues[nProperty] >>= bMenuIcons;
262 break;
263 case PROPERTYHANDLE_SYSTEMICONSINMENUES : {
264 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\IsSystemIconsInMenus\"?" );
265 seqValues[nProperty] >>= bSystemMenuIcons;
267 break;
271 m_nMenuIcons = bSystemMenuIcons ? 2 : bMenuIcons;
273 EnableNotification( seqNames );
276 //*****************************************************************************************************************
277 // destructor
278 //*****************************************************************************************************************
279 SvtMenuOptions_Impl::~SvtMenuOptions_Impl()
281 // Flush data to configuration!
282 // User has no chance to do that.
283 if( IsModified() == sal_True )
285 Commit();
289 //*****************************************************************************************************************
290 // public method
291 //*****************************************************************************************************************
292 void SvtMenuOptions_Impl::Notify( const Sequence< OUString >& seqPropertyNames )
294 // Use given list of updated properties to get his values from configuration directly!
295 Sequence< Any > seqValues = GetProperties( seqPropertyNames );
296 // Safe impossible cases.
297 // We need values from ALL notified configuration keys.
298 DBG_ASSERT( !(seqPropertyNames.getLength()!=seqValues.getLength()), "SvtMenuOptions_Impl::Notify()\nI miss some values of configuration keys!\n" );
300 sal_Bool bMenuSettingsChanged = sal_False;
301 sal_Bool bMenuIcons = sal_True;
302 sal_Bool bSystemMenuIcons = sal_True;
303 if (m_nMenuIcons == 2)
304 bMenuIcons = (sal_Bool)(Application::GetSettings().GetStyleSettings().GetUseImagesInMenus());
305 else
307 bSystemMenuIcons = sal_False;
308 bMenuIcons = m_nMenuIcons ? sal_True : sal_False;
311 // Step over list of property names and get right value from coreesponding value list to set it on internal members!
312 sal_Int32 nCount = seqPropertyNames.getLength();
313 for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
315 if( seqPropertyNames[nProperty] == PROPERTYNAME_DONTHIDEDISABLEDENTRIES )
317 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\View\\Menu\\DontHideDisabledEntry\"?" );
318 seqValues[nProperty] >>= m_bDontHideDisabledEntries;
320 else if( seqPropertyNames[nProperty] == PROPERTYNAME_FOLLOWMOUSE )
322 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\View\\Menu\\FollowMouse\"?" );
323 seqValues[nProperty] >>= m_bFollowMouse;
325 else if( seqPropertyNames[nProperty] == PROPERTYNAME_SHOWICONSINMENUES )
327 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\ShowIconsInMenues\"?" );
328 bMenuSettingsChanged |= seqValues[nProperty] >>= bMenuIcons;
330 else if( seqPropertyNames[nProperty] == PROPERTYNAME_SYSTEMICONSINMENUES )
332 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\IsSystemIconsInMenus\"?" );
333 bMenuSettingsChanged |= seqValues[nProperty] >>= bSystemMenuIcons;
336 #if OSL_DEBUG_LEVEL > 1
337 else DBG_ASSERT( sal_False, "SvtMenuOptions_Impl::Notify()\nUnknown property detected ... I can't handle these!\n" );
338 #endif
341 if ( bMenuSettingsChanged )
342 m_nMenuIcons = bSystemMenuIcons ? 2 : bMenuIcons;
344 for ( ::std::list<Link>::const_iterator iter = aList.begin(); iter != aList.end(); ++iter )
345 iter->Call( this );
348 //*****************************************************************************************************************
349 // public method
350 //*****************************************************************************************************************
351 void SvtMenuOptions_Impl::Commit()
353 // Get names of supported properties, create a list for values and copy current values to it.
354 Sequence< OUString > seqNames = impl_GetPropertyNames();
355 sal_Int32 nCount = seqNames.getLength();
356 Sequence< Any > seqValues ( nCount );
357 for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
359 switch( nProperty )
361 case PROPERTYHANDLE_DONTHIDEDISABLEDENTRIES : {
362 seqValues[nProperty] <<= m_bDontHideDisabledEntries;
364 break;
366 case PROPERTYHANDLE_FOLLOWMOUSE : {
367 seqValues[nProperty] <<= m_bFollowMouse;
369 break;
370 //Output cache of current setting as possibly modified by System Theme for older version
371 case PROPERTYHANDLE_SHOWICONSINMENUES : {
372 sal_Bool bValue = (sal_Bool)(Application::GetSettings().GetStyleSettings().GetUseImagesInMenus());
373 seqValues[nProperty] <<= bValue;
375 break;
376 case PROPERTYHANDLE_SYSTEMICONSINMENUES : {
377 sal_Bool bValue = (m_nMenuIcons == 2 ? sal_True : sal_False) ;
378 seqValues[nProperty] <<= bValue;
380 break;
383 // Set properties in configuration.
384 PutProperties( seqNames, seqValues );
387 //*****************************************************************************************************************
388 // private method
389 //*****************************************************************************************************************
390 Sequence< OUString > SvtMenuOptions_Impl::impl_GetPropertyNames()
392 // Build static list of configuration key names.
393 static const OUString pProperties[] =
395 PROPERTYNAME_DONTHIDEDISABLEDENTRIES ,
396 PROPERTYNAME_FOLLOWMOUSE ,
397 PROPERTYNAME_SHOWICONSINMENUES ,
398 PROPERTYNAME_SYSTEMICONSINMENUES
400 // Initialize return sequence with these list ...
401 static const Sequence< OUString > seqPropertyNames( pProperties, PROPERTYCOUNT );
402 // ... and return it.
403 return seqPropertyNames;
406 void SvtMenuOptions_Impl::AddListenerLink( const Link& rLink )
408 aList.push_back( rLink );
411 void SvtMenuOptions_Impl::RemoveListenerLink( const Link& rLink )
413 for ( ::std::list<Link>::iterator iter = aList.begin(); iter != aList.end(); ++iter )
415 if ( *iter == rLink )
417 aList.erase(iter);
418 break;
423 //*****************************************************************************************************************
424 // initialize static member
425 // DON'T DO IT IN YOUR HEADER!
426 // see definition for further information
427 //*****************************************************************************************************************
428 SvtMenuOptions_Impl* SvtMenuOptions::m_pDataContainer = NULL ;
429 sal_Int32 SvtMenuOptions::m_nRefCount = 0 ;
431 //*****************************************************************************************************************
432 // constructor
433 //*****************************************************************************************************************
434 SvtMenuOptions::SvtMenuOptions()
436 // Global access, must be guarded (multithreading!).
437 MutexGuard aGuard( GetOwnStaticMutex() );
438 // Increase our refcount ...
439 ++m_nRefCount;
440 // ... and initialize our data container only if it not already!
441 if( m_pDataContainer == NULL )
443 m_pDataContainer = new SvtMenuOptions_Impl();
445 svtools::ItemHolder2::holdConfigItem(E_MENUOPTIONS);
449 //*****************************************************************************************************************
450 // destructor
451 //*****************************************************************************************************************
452 SvtMenuOptions::~SvtMenuOptions()
454 // Global access, must be guarded (multithreading!)
455 MutexGuard aGuard( GetOwnStaticMutex() );
456 // Decrease our refcount.
457 --m_nRefCount;
458 // If last instance was deleted ...
459 // we must destroy our static data container!
460 if( m_nRefCount <= 0 )
462 delete m_pDataContainer;
463 m_pDataContainer = NULL;
467 //*****************************************************************************************************************
468 // public method
469 //*****************************************************************************************************************
470 sal_Bool SvtMenuOptions::IsEntryHidingEnabled() const
472 MutexGuard aGuard( GetOwnStaticMutex() );
473 return m_pDataContainer->IsEntryHidingEnabled();
476 //*****************************************************************************************************************
477 // public method
478 //*****************************************************************************************************************
479 sal_Int16 SvtMenuOptions::GetMenuIconsState() const
481 MutexGuard aGuard( GetOwnStaticMutex() );
482 return m_pDataContainer->GetMenuIconsState();
485 //*****************************************************************************************************************
486 // public method
487 //*****************************************************************************************************************
488 void SvtMenuOptions::SetMenuIconsState( sal_Int16 bState )
490 MutexGuard aGuard( GetOwnStaticMutex() );
491 m_pDataContainer->SetMenuIconsState( bState );
494 //*****************************************************************************************************************
495 // private method
496 //*****************************************************************************************************************
497 Mutex& SvtMenuOptions::GetOwnStaticMutex()
499 // Initialize static mutex only for one time!
500 static Mutex* pMutex = NULL;
501 // If these method first called (Mutex not already exist!) ...
502 if( pMutex == NULL )
504 // ... we must create a new one. Protect follow code with the global mutex -
505 // It must be - we create a static variable!
506 MutexGuard aGuard( Mutex::getGlobalMutex() );
507 // We must check our pointer again - because it can be that another instance of our class will be faster than these!
508 if( pMutex == NULL )
510 // Create the new mutex and set it for return on static variable.
511 static Mutex aMutex;
512 pMutex = &aMutex;
515 // Return new created or already existing mutex object.
516 return *pMutex;
519 void SvtMenuOptions::AddListenerLink( const Link& rLink )
521 m_pDataContainer->AddListenerLink( rLink );
524 void SvtMenuOptions::RemoveListenerLink( const Link& rLink )
526 m_pDataContainer->RemoveListenerLink( rLink );
529 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */